blob: ff5d268bf75297e2b037558f5b9c9e9b9f28d305 [file] [log] [blame]
Misha Brukman2a8350a2005-02-05 02:24:26 +00001//===- AlphaISelPattern.cpp - A pattern matching inst selector for Alpha --===//
Misha Brukman4633f1c2005-04-21 23:13:11 +00002//
Andrew Lenharth304d0f32005-01-22 23:41:55 +00003// 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.
Misha Brukman4633f1c2005-04-21 23:13:11 +00007//
Andrew Lenharth304d0f32005-01-22 23:41:55 +00008//===----------------------------------------------------------------------===//
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 {
Misha Brukman4633f1c2005-04-21 23:13:11 +000036 cl::opt<bool> EnableAlphaIDIV("enable-alpha-intfpdiv",
37 cl::desc("Use the FP div instruction for integer div when possible"),
Andrew Lenharth95762122005-03-31 21:24:06 +000038 cl::Hidden);
Andrew Lenharth59009192005-05-04 19:12:09 +000039 cl::opt<bool> EnableAlphaFTOI("enable-alpha-FTOI",
Misha Brukman4633f1c2005-04-21 23:13:11 +000040 cl::desc("Enable use of ftoi* and itof* instructions (ev6 and higher)"),
Andrew Lenharth95762122005-03-31 21:24:06 +000041 cl::Hidden);
Andrew Lenharth59009192005-05-04 19:12:09 +000042 cl::opt<bool> EnableAlphaCT("enable-alpha-CT",
43 cl::desc("Enable use of the ctpop, ctlz, and cttz instructions"),
44 cl::Hidden);
Misha Brukman4633f1c2005-04-21 23:13:11 +000045 cl::opt<bool> EnableAlphaCount("enable-alpha-count",
46 cl::desc("Print estimates on live ins and outs"),
Andrew Lenhartha32b9e32005-04-08 17:28:49 +000047 cl::Hidden);
Andrew Lenharth95762122005-03-31 21:24:06 +000048}
49
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +000050namespace {
51 // Alpha Specific DAG Nodes
52 namespace AlphaISD {
53 enum NodeType {
54 // Start the numbering where the builtin ops leave off.
55 FIRST_NUMBER = ISD::BUILTIN_OP_END,
56
57 //Convert an int bit pattern in an FP reg to a Double or Float
58 //Has a dest type and a source
59 CVTQ,
60 //Move an Ireg to a FPreg
61 ITOF,
62 //Move a FPreg to an Ireg
63 FTOI,
64 };
65 }
66}
67
Andrew Lenharth304d0f32005-01-22 23:41:55 +000068//===----------------------------------------------------------------------===//
69// AlphaTargetLowering - Alpha Implementation of the TargetLowering interface
70namespace {
71 class AlphaTargetLowering : public TargetLowering {
72 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
73 unsigned GP; //GOT vreg
74 public:
75 AlphaTargetLowering(TargetMachine &TM) : TargetLowering(TM) {
76 // Set up the TargetLowering object.
Andrew Lenharth3d65d312005-01-27 03:49:45 +000077 //I am having problems with shr n ubyte 1
Andrew Lenharth879ef222005-02-02 17:00:21 +000078 setShiftAmountType(MVT::i64);
79 setSetCCResultType(MVT::i64);
Andrew Lenharthd3355e22005-04-07 20:11:32 +000080 setSetCCResultContents(ZeroOrOneSetCCResult);
Misha Brukman4633f1c2005-04-21 23:13:11 +000081
Andrew Lenharth304d0f32005-01-22 23:41:55 +000082 addRegisterClass(MVT::i64, Alpha::GPRCRegisterClass);
83 addRegisterClass(MVT::f64, Alpha::FPRCRegisterClass);
Andrew Lenharth3d65d312005-01-27 03:49:45 +000084 addRegisterClass(MVT::f32, Alpha::FPRCRegisterClass);
Misha Brukman4633f1c2005-04-21 23:13:11 +000085
Chris Lattnerda4d4692005-04-09 03:22:37 +000086 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000087 setOperationAction(ISD::EXTLOAD , MVT::i1 , Promote);
88 setOperationAction(ISD::EXTLOAD , MVT::f32 , Promote);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000089
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000090 setOperationAction(ISD::ZEXTLOAD , MVT::i1 , Expand);
91 setOperationAction(ISD::ZEXTLOAD , MVT::i32 , Expand);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000092
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000093 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
94 setOperationAction(ISD::SEXTLOAD , MVT::i8 , Expand);
95 setOperationAction(ISD::SEXTLOAD , MVT::i16 , Expand);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000096
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000097 setOperationAction(ISD::SREM , MVT::f32 , Expand);
98 setOperationAction(ISD::SREM , MVT::f64 , Expand);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +000099
Andrew Lenharth59009192005-05-04 19:12:09 +0000100 if (!EnableAlphaCT) {
101 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
102 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
Andrew Lenharthb5884d32005-05-04 19:25:37 +0000103 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
Andrew Lenharth59009192005-05-04 19:12:09 +0000104 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000105
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000106 //If this didn't legalize into a div....
107 // setOperationAction(ISD::SREM , MVT::i64, Expand);
108 // setOperationAction(ISD::UREM , MVT::i64, Expand);
109
110 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
111 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
112 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
Andrew Lenharth9818c052005-02-05 13:19:12 +0000113
Chris Lattner17234b72005-04-30 04:26:06 +0000114 // We don't support sin/cos/sqrt
115 setOperationAction(ISD::FSIN , MVT::f64, Expand);
116 setOperationAction(ISD::FCOS , MVT::f64, Expand);
117 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
118 setOperationAction(ISD::FSIN , MVT::f32, Expand);
119 setOperationAction(ISD::FCOS , MVT::f32, Expand);
120 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
121
Andrew Lenharth33819132005-03-04 20:09:23 +0000122 //Doesn't work yet
Chris Lattner17234b72005-04-30 04:26:06 +0000123 setOperationAction(ISD::SETCC, MVT::f32, Promote);
Andrew Lenharth572af902005-02-14 05:41:43 +0000124
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000125 //Try a couple things with a custom expander
126 //setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
127
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000128 computeRegisterProperties();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000129
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000130 addLegalFPImmediate(+0.0); //F31
131 addLegalFPImmediate(-0.0); //-F31
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000132 }
133
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000134 /// LowerOperation - Provide custom lowering hooks for some operations.
135 ///
136 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
137
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000138 /// LowerArguments - This hook must be implemented to indicate how we should
139 /// lower the arguments for the specified function, into the specified DAG.
140 virtual std::vector<SDOperand>
141 LowerArguments(Function &F, SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000142
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000143 /// LowerCallTo - This hook lowers an abstract call to a function into an
144 /// actual call.
145 virtual std::pair<SDOperand, SDOperand>
Chris Lattnerc57f6822005-05-12 19:56:45 +0000146 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattneradf6a962005-05-13 18:50:42 +0000147 bool isTailCall, SDOperand Callee, ArgListTy &Args,
148 SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000149
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000150 virtual std::pair<SDOperand, SDOperand>
151 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000152
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000153 virtual std::pair<SDOperand,SDOperand>
154 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
155 const Type *ArgTy, SelectionDAG &DAG);
156
157 virtual std::pair<SDOperand, SDOperand>
158 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
159 SelectionDAG &DAG);
160
161 void restoreGP(MachineBasicBlock* BB)
162 {
163 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
164 }
165 };
166}
167
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000168/// LowerOperation - Provide custom lowering hooks for some operations.
169///
170SDOperand AlphaTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
171 MachineFunction &MF = DAG.getMachineFunction();
172 switch (Op.getOpcode()) {
173 default: assert(0 && "Should not custom lower this!");
Misha Brukmanb8ee91a2005-06-06 17:39:46 +0000174#if 0
175 case ISD::SINT_TO_FP:
176 {
177 assert (Op.getOperand(0).getValueType() == MVT::i64
178 && "only quads can be loaded from");
179 SDOperand SRC;
180 if (EnableAlphaFTOI)
181 {
182 std::vector<MVT::ValueType> RTs;
183 RTs.push_back(Op.getValueType());
184 std::vector<SDOperand> Ops;
185 Ops.push_back(Op.getOperand(0));
186 SRC = DAG.getNode(AlphaISD::ITOF, RTs, Ops);
187 } else {
188 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
189 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
190 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
191 Op.getOperand(0), StackSlot, DAG.getSrcValue(NULL));
192 SRC = DAG.getLoad(Op.getValueType(), Store.getValue(0), StackSlot,
193 DAG.getSrcValue(NULL));
194 }
195 std::vector<MVT::ValueType> RTs;
196 RTs.push_back(Op.getValueType());
197 std::vector<SDOperand> Ops;
198 Ops.push_back(SRC);
199 return DAG.getNode(AlphaISD::CVTQ, RTs, Ops);
200 }
201#endif
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000202 }
Misha Brukmanb8ee91a2005-06-06 17:39:46 +0000203 return SDOperand();
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000204}
205
206
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000207/// AddLiveIn - This helper function adds the specified physical register to the
208/// MachineFunction as a live in value. It also creates a corresponding virtual
209/// register for it.
210static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
211 TargetRegisterClass *RC) {
212 assert(RC->contains(PReg) && "Not the correct regclass!");
213 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
214 MF.addLiveIn(PReg, VReg);
215 return VReg;
216}
217
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000218//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
219
220//For now, just use variable size stack frame format
221
222//In a standard call, the first six items are passed in registers $16
223//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
224//of argument-to-register correspondence.) The remaining items are
225//collected in a memory argument list that is a naturally aligned
226//array of quadwords. In a standard call, this list, if present, must
227//be passed at 0(SP).
Misha Brukman7847fca2005-04-22 17:54:37 +0000228//7 ... n 0(SP) ... (n-7)*8(SP)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000229
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000230// //#define FP $15
231// //#define RA $26
232// //#define PV $27
233// //#define GP $29
234// //#define SP $30
Misha Brukman4633f1c2005-04-21 23:13:11 +0000235
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000236std::vector<SDOperand>
Misha Brukman4633f1c2005-04-21 23:13:11 +0000237AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000238{
239 std::vector<SDOperand> ArgValues;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000240
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000241 MachineFunction &MF = DAG.getMachineFunction();
Andrew Lenharth05380342005-02-07 05:07:00 +0000242 MachineFrameInfo*MFI = MF.getFrameInfo();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000243
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000244 MachineBasicBlock& BB = MF.front();
245
246 //Handle the return address
247 //BuildMI(&BB, Alpha::IDEF, 0, Alpha::R26);
248
Misha Brukman4633f1c2005-04-21 23:13:11 +0000249 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000250 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000251 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000252 Alpha::F19, Alpha::F20, Alpha::F21};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000253 int count = 0;
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000254
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000255 GP = AddLiveIn(MF, Alpha::R29, getRegClassFor(MVT::i64));
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000256
Chris Lattnere4d5c442005-03-15 04:54:21 +0000257 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000258 {
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000259 SDOperand argt;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000260 if (count < 6) {
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000261 unsigned Vreg;
262 MVT::ValueType VT = getValueType(I->getType());
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000263 switch (VT) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000264 default:
265 std::cerr << "Unknown Type " << VT << "\n";
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000266 abort();
267 case MVT::f64:
268 case MVT::f32:
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000269 args_float[count] = AddLiveIn(MF,args_float[count], getRegClassFor(VT));
270 argt = DAG.getCopyFromReg(args_float[count], VT, DAG.getRoot());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000271 break;
272 case MVT::i1:
273 case MVT::i8:
274 case MVT::i16:
275 case MVT::i32:
276 case MVT::i64:
Andrew Lenharth591ec572005-05-31 18:42:18 +0000277 args_int[count] = AddLiveIn(MF, args_int[count], getRegClassFor(MVT::i64));
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000278 argt = DAG.getCopyFromReg(args_int[count], VT, DAG.getRoot());
Andrew Lenharth14f30c92005-05-31 18:37:16 +0000279 if (VT != MVT::i64)
280 argt = DAG.getNode(ISD::TRUNCATE, VT, argt);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000281 break;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000282 }
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000283 DAG.setRoot(argt.getValue(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000284 } else { //more args
285 // Create the frame index object for this incoming parameter...
286 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000287
288 // Create the SelectionDAG nodes corresponding to a load
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000289 //from this parameter
290 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000291 argt = DAG.getLoad(getValueType(I->getType()),
292 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000293 }
Andrew Lenharth032f2352005-02-22 21:59:48 +0000294 ++count;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000295 ArgValues.push_back(argt);
296 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000297
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000298 // If the functions takes variable number of arguments, copy all regs to stack
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000299 if (F.isVarArg()) {
300 std::vector<SDOperand> LS;
301 for (int i = 0; i < 6; ++i) {
302 if (args_int[i] < 1024)
303 args_int[i] = AddLiveIn(MF,args_int[i], getRegClassFor(MVT::i64));
304 SDOperand argt = DAG.getCopyFromReg(args_int[i], MVT::i64, DAG.getRoot());
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000305 int FI = MFI->CreateFixedObject(8, -8 * (6 - i));
306 SDOperand SDFI = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000307 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt, SDFI, DAG.getSrcValue(NULL)));
308
309 if (args_float[i] < 1024)
310 args_float[i] = AddLiveIn(MF,args_float[i], getRegClassFor(MVT::f64));
311 argt = DAG.getCopyFromReg(args_float[i], MVT::f64, DAG.getRoot());
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000312 FI = MFI->CreateFixedObject(8, - 8 * (12 - i));
313 SDFI = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000314 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt, SDFI, DAG.getSrcValue(NULL)));
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000315 }
316
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000317 //Set up a token factor with all the stack traffic
318 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, LS));
319 }
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000320
321 // Finally, inform the code generator which regs we return values in.
322 switch (getValueType(F.getReturnType())) {
323 default: assert(0 && "Unknown type!");
324 case MVT::isVoid: break;
325 case MVT::i1:
326 case MVT::i8:
327 case MVT::i16:
328 case MVT::i32:
329 case MVT::i64:
330 MF.addLiveOut(Alpha::R0);
331 break;
332 case MVT::f32:
333 case MVT::f64:
334 MF.addLiveOut(Alpha::F0);
335 break;
336 }
337
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000338 //return the arguments
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000339 return ArgValues;
340}
341
342std::pair<SDOperand, SDOperand>
343AlphaTargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000344 const Type *RetTy, bool isVarArg,
Chris Lattneradf6a962005-05-13 18:50:42 +0000345 unsigned CallingConv, bool isTailCall,
Misha Brukman7847fca2005-04-22 17:54:37 +0000346 SDOperand Callee, ArgListTy &Args,
347 SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000348 int NumBytes = 0;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000349 if (Args.size() > 6)
350 NumBytes = (Args.size() - 6) * 8;
351
Chris Lattner16cd04d2005-05-12 23:24:06 +0000352 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000353 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000354 std::vector<SDOperand> args_to_use;
355 for (unsigned i = 0, e = Args.size(); i != e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000356 {
357 switch (getValueType(Args[i].second)) {
358 default: assert(0 && "Unexpected ValueType for argument!");
359 case MVT::i1:
360 case MVT::i8:
361 case MVT::i16:
362 case MVT::i32:
363 // Promote the integer to 64 bits. If the input type is signed use a
364 // sign extend, otherwise use a zero extend.
365 if (Args[i].second->isSigned())
366 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
367 else
368 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
369 break;
370 case MVT::i64:
371 case MVT::f64:
372 case MVT::f32:
373 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000374 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000375 args_to_use.push_back(Args[i].first);
376 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000377
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000378 std::vector<MVT::ValueType> RetVals;
379 MVT::ValueType RetTyVT = getValueType(RetTy);
380 if (RetTyVT != MVT::isVoid)
381 RetVals.push_back(RetTyVT);
382 RetVals.push_back(MVT::Other);
383
Misha Brukman4633f1c2005-04-21 23:13:11 +0000384 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000385 Chain, Callee, args_to_use), 0);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000386 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner16cd04d2005-05-12 23:24:06 +0000387 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000388 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000389 return std::make_pair(TheCall, Chain);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000390}
391
392std::pair<SDOperand, SDOperand>
393AlphaTargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
394 //vastart just returns the address of the VarArgsFrameIndex slot.
395 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64), Chain);
396}
397
398std::pair<SDOperand,SDOperand> AlphaTargetLowering::
399LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000400 const Type *ArgTy, SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000401 abort();
402}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000403
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000404
405std::pair<SDOperand, SDOperand> AlphaTargetLowering::
406LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
407 SelectionDAG &DAG) {
408 abort();
409}
410
411
412
413
414
415namespace {
416
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000417//===--------------------------------------------------------------------===//
418/// ISel - Alpha specific code to select Alpha machine instructions for
419/// SelectionDAG operations.
420//===--------------------------------------------------------------------===//
421class ISel : public SelectionDAGISel {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000422
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000423 /// AlphaLowering - This object fully describes how to lower LLVM code to an
424 /// Alpha-specific SelectionDAG.
425 AlphaTargetLowering AlphaLowering;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000426
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000427 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
428 // for sdiv and udiv until it is put into the future
429 // dag combiner.
430
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000431 /// ExprMap - As shared expressions are codegen'd, we keep track of which
432 /// vreg the value is produced in, so we only emit one copy of each compiled
433 /// tree.
434 static const unsigned notIn = (unsigned)(-1);
435 std::map<SDOperand, unsigned> ExprMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000436
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000437 //CCInvMap sometimes (SetNE) we have the inverse CC code for free
438 std::map<SDOperand, unsigned> CCInvMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000439
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000440 int count_ins;
441 int count_outs;
442 bool has_sym;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000443 int max_depth;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000444
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000445public:
Misha Brukman4633f1c2005-04-21 23:13:11 +0000446 ISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering), AlphaLowering(TM)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000447 {}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000448
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000449 /// InstructionSelectBasicBlock - This callback is invoked by
450 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
451 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
Andrew Lenharth032f2352005-02-22 21:59:48 +0000452 DEBUG(BB->dump());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000453 count_ins = 0;
454 count_outs = 0;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000455 max_depth = 0;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000456 has_sym = false;
457
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000458 // Codegen the basic block.
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000459 ISelDAG = &DAG;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000460 max_depth = DAG.getRoot().getNodeDepth();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000461 Select(DAG.getRoot());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000462
463 if(has_sym)
464 ++count_ins;
465 if(EnableAlphaCount)
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000466 std::cerr << "COUNT: " << BB->getParent()->getFunction ()->getName() << " "
467 << BB->getNumber() << " "
468 << max_depth << " "
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000469 << count_ins << " "
470 << count_outs << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000471
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000472 // Clear state used for selection.
473 ExprMap.clear();
474 CCInvMap.clear();
475 }
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000476
477 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000478
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000479 unsigned SelectExpr(SDOperand N);
480 unsigned SelectExprFP(SDOperand N, unsigned Result);
481 void Select(SDOperand N);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000482
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000483 void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
484 void SelectBranchCC(SDOperand N);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000485 void MoveFP2Int(unsigned src, unsigned dst, bool isDouble);
486 void MoveInt2FP(unsigned src, unsigned dst, bool isDouble);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000487 //returns whether the sense of the comparison was inverted
488 bool SelectFPSetCC(SDOperand N, unsigned dst);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000489
490 // dag -> dag expanders for integer divide by constant
491 SDOperand BuildSDIVSequence(SDOperand N);
492 SDOperand BuildUDIVSequence(SDOperand N);
493
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000494};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000495}
496
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000497void ISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
498 // If this function has live-in values, emit the copies from pregs to vregs at
499 // the top of the function, before anything else.
500 MachineBasicBlock *BB = MF.begin();
501 if (MF.livein_begin() != MF.livein_end()) {
502 SSARegMap *RegMap = MF.getSSARegMap();
503 for (MachineFunction::livein_iterator LI = MF.livein_begin(),
504 E = MF.livein_end(); LI != E; ++LI) {
505 const TargetRegisterClass *RC = RegMap->getRegClass(LI->second);
506 if (RC == Alpha::GPRCRegisterClass) {
507 BuildMI(BB, Alpha::BIS, 2, LI->second).addReg(LI->first).addReg(LI->first);
508 } else if (RC == Alpha::FPRCRegisterClass) {
509 BuildMI(BB, Alpha::CPYS, 2, LI->second).addReg(LI->first).addReg(LI->first);
510 } else {
511 assert(0 && "Unknown regclass!");
512 }
513 }
514 }
515}
516
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000517//Factorize a number using the list of constants
518static bool factorize(int v[], int res[], int size, uint64_t c)
519{
520 bool cont = true;
521 while (c != 1 && cont)
522 {
523 cont = false;
524 for(int i = 0; i < size; ++i)
525 {
526 if (c % v[i] == 0)
527 {
528 c /= v[i];
529 ++res[i];
530 cont=true;
531 }
532 }
533 }
534 return c == 1;
535}
536
537
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000538//Shamelessly adapted from PPC32
Misha Brukman4633f1c2005-04-21 23:13:11 +0000539// Structure used to return the necessary information to codegen an SDIV as
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000540// a multiply.
541struct ms {
542 int64_t m; // magic number
543 int64_t s; // shift amount
544};
545
546struct mu {
547 uint64_t m; // magic number
548 int64_t a; // add indicator
549 int64_t s; // shift amount
550};
551
552/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukman4633f1c2005-04-21 23:13:11 +0000553/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000554/// or -1.
555static struct ms magic(int64_t d) {
556 int64_t p;
557 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
558 const uint64_t two63 = 9223372036854775808ULL; // 2^63
559 struct ms mag;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000560
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000561 ad = abs(d);
562 t = two63 + ((uint64_t)d >> 63);
563 anc = t - 1 - t%ad; // absolute value of nc
Andrew Lenharth320174f2005-04-07 17:19:16 +0000564 p = 63; // initialize p
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000565 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
566 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
567 q2 = two63/ad; // initialize q2 = 2p/abs(d)
568 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
569 do {
570 p = p + 1;
571 q1 = 2*q1; // update q1 = 2p/abs(nc)
572 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
573 if (r1 >= anc) { // must be unsigned comparison
574 q1 = q1 + 1;
575 r1 = r1 - anc;
576 }
577 q2 = 2*q2; // update q2 = 2p/abs(d)
578 r2 = 2*r2; // update r2 = rem(2p/abs(d))
579 if (r2 >= ad) { // must be unsigned comparison
580 q2 = q2 + 1;
581 r2 = r2 - ad;
582 }
583 delta = ad - r2;
584 } while (q1 < delta || (q1 == delta && r1 == 0));
585
586 mag.m = q2 + 1;
587 if (d < 0) mag.m = -mag.m; // resulting magic number
588 mag.s = p - 64; // resulting shift
589 return mag;
590}
591
592/// magicu - calculate the magic numbers required to codegen an integer udiv as
593/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
594static struct mu magicu(uint64_t d)
595{
596 int64_t p;
597 uint64_t nc, delta, q1, r1, q2, r2;
598 struct mu magu;
599 magu.a = 0; // initialize "add" indicator
600 nc = - 1 - (-d)%d;
Andrew Lenharth320174f2005-04-07 17:19:16 +0000601 p = 63; // initialize p
602 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
603 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
604 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
605 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000606 do {
607 p = p + 1;
608 if (r1 >= nc - r1 ) {
609 q1 = 2*q1 + 1; // update q1
610 r1 = 2*r1 - nc; // update r1
611 }
612 else {
613 q1 = 2*q1; // update q1
614 r1 = 2*r1; // update r1
615 }
616 if (r2 + 1 >= d - r2) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000617 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000618 q2 = 2*q2 + 1; // update q2
619 r2 = 2*r2 + 1 - d; // update r2
620 }
621 else {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000622 if (q2 >= 0x8000000000000000ull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000623 q2 = 2*q2; // update q2
624 r2 = 2*r2 + 1; // update r2
625 }
626 delta = d - 1 - r2;
627 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
628 magu.m = q2 + 1; // resulting magic number
Andrew Lenharth320174f2005-04-07 17:19:16 +0000629 magu.s = p - 64; // resulting shift
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000630 return magu;
631}
632
633/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
634/// return a DAG expression to select that will generate the same value by
635/// multiplying by a magic number. See:
636/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
637SDOperand ISel::BuildSDIVSequence(SDOperand N) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000638 int64_t d = (int64_t)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000639 ms magics = magic(d);
640 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000641 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000642 ISelDAG->getConstant(magics.m, MVT::i64));
643 // If d > 0 and m < 0, add the numerator
644 if (d > 0 && magics.m < 0)
645 Q = ISelDAG->getNode(ISD::ADD, MVT::i64, Q, N.getOperand(0));
646 // If d < 0 and m > 0, subtract the numerator.
647 if (d < 0 && magics.m > 0)
648 Q = ISelDAG->getNode(ISD::SUB, MVT::i64, Q, N.getOperand(0));
649 // Shift right algebraic if shift value is nonzero
650 if (magics.s > 0)
Misha Brukman4633f1c2005-04-21 23:13:11 +0000651 Q = ISelDAG->getNode(ISD::SRA, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000652 ISelDAG->getConstant(magics.s, MVT::i64));
653 // Extract the sign bit and add it to the quotient
Misha Brukman4633f1c2005-04-21 23:13:11 +0000654 SDOperand T =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000655 ISelDAG->getNode(ISD::SRL, MVT::i64, Q, ISelDAG->getConstant(63, MVT::i64));
656 return ISelDAG->getNode(ISD::ADD, MVT::i64, Q, T);
657}
658
659/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
660/// return a DAG expression to select that will generate the same value by
661/// multiplying by a magic number. See:
662/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
663SDOperand ISel::BuildUDIVSequence(SDOperand N) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000664 unsigned d =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000665 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
666 mu magics = magicu(d);
667 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000668 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000669 ISelDAG->getConstant(magics.m, MVT::i64));
670 if (magics.a == 0) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000671 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000672 ISelDAG->getConstant(magics.s, MVT::i64));
673 } else {
674 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i64, N.getOperand(0), Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000675 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000676 ISelDAG->getConstant(1, MVT::i64));
677 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i64, NPQ, Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000678 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000679 ISelDAG->getConstant(magics.s-1, MVT::i64));
680 }
681 return Q;
682}
683
Andrew Lenhartha565c272005-04-06 22:03:13 +0000684//From PPC32
685/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
686/// returns zero when the input is not exactly a power of two.
687static unsigned ExactLog2(uint64_t Val) {
688 if (Val == 0 || (Val & (Val-1))) return 0;
689 unsigned Count = 0;
690 while (Val != 1) {
691 Val >>= 1;
692 ++Count;
693 }
694 return Count;
695}
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000696
697
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000698//These describe LDAx
Andrew Lenharthc0513832005-03-29 19:24:04 +0000699static const int IMM_LOW = -32768;
700static const int IMM_HIGH = 32767;
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000701static const int IMM_MULT = 65536;
702
703static long getUpper16(long l)
704{
705 long y = l / IMM_MULT;
706 if (l % IMM_MULT > IMM_HIGH)
707 ++y;
708 return y;
709}
710
711static long getLower16(long l)
712{
713 long h = getUpper16(l);
714 return l - h * IMM_MULT;
715}
716
Andrew Lenharth65838902005-02-06 16:22:15 +0000717static unsigned GetSymVersion(unsigned opcode)
718{
719 switch (opcode) {
720 default: assert(0 && "unknown load or store"); return 0;
721 case Alpha::LDQ: return Alpha::LDQ_SYM;
722 case Alpha::LDS: return Alpha::LDS_SYM;
723 case Alpha::LDT: return Alpha::LDT_SYM;
724 case Alpha::LDL: return Alpha::LDL_SYM;
725 case Alpha::LDBU: return Alpha::LDBU_SYM;
726 case Alpha::LDWU: return Alpha::LDWU_SYM;
727 case Alpha::LDW: return Alpha::LDW_SYM;
728 case Alpha::LDB: return Alpha::LDB_SYM;
729 case Alpha::STQ: return Alpha::STQ_SYM;
730 case Alpha::STS: return Alpha::STS_SYM;
731 case Alpha::STT: return Alpha::STT_SYM;
732 case Alpha::STL: return Alpha::STL_SYM;
733 case Alpha::STW: return Alpha::STW_SYM;
734 case Alpha::STB: return Alpha::STB_SYM;
735 }
736}
737
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000738void ISel::MoveFP2Int(unsigned src, unsigned dst, bool isDouble)
739{
740 unsigned Opc;
741 if (EnableAlphaFTOI) {
742 Opc = isDouble ? Alpha::FTOIT : Alpha::FTOIS;
743 BuildMI(BB, Opc, 1, dst).addReg(src);
744 } else {
745 //The hard way:
746 // Spill the integer to memory and reload it from there.
747 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
748 MachineFunction *F = BB->getParent();
749 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
750
751 Opc = isDouble ? Alpha::STT : Alpha::STS;
752 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
753 Opc = isDouble ? Alpha::LDQ : Alpha::LDL;
754 BuildMI(BB, Alpha::LDQ, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
755 }
756}
757
758void ISel::MoveInt2FP(unsigned src, unsigned dst, bool isDouble)
759{
760 unsigned Opc;
761 if (EnableAlphaFTOI) {
762 Opc = isDouble?Alpha::ITOFT:Alpha::ITOFS;
763 BuildMI(BB, Opc, 1, dst).addReg(src);
764 } else {
765 //The hard way:
766 // Spill the integer to memory and reload it from there.
767 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
768 MachineFunction *F = BB->getParent();
769 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
770
771 Opc = isDouble ? Alpha::STQ : Alpha::STL;
772 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
773 Opc = isDouble ? Alpha::LDT : Alpha::LDS;
774 BuildMI(BB, Opc, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
775 }
776}
777
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000778bool ISel::SelectFPSetCC(SDOperand N, unsigned dst)
779{
780 SDNode *Node = N.Val;
781 unsigned Opc, Tmp1, Tmp2, Tmp3;
782 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node);
783
784 //assert(SetCC->getOperand(0).getValueType() != MVT::f32 && "SetCC f32 should have been promoted");
785 bool rev = false;
786 bool inv = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000787
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000788 switch (SetCC->getCondition()) {
789 default: Node->dump(); assert(0 && "Unknown FP comparison!");
790 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
791 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
792 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
793 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
794 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
795 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
796 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000797
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000798 //FIXME: check for constant 0.0
799 ConstantFPSDNode *CN;
800 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
801 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
802 Tmp1 = Alpha::F31;
803 else
804 Tmp1 = SelectExpr(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000805
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000806 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
807 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
808 Tmp2 = Alpha::F31;
809 else
Chris Lattner9c9183a2005-04-30 04:44:07 +0000810 Tmp2 = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000811
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000812 //Can only compare doubles, and dag won't promote for me
813 if (SetCC->getOperand(0).getValueType() == MVT::f32)
814 {
815 //assert(0 && "Setcc On float?\n");
816 std::cerr << "Setcc on float!\n";
817 Tmp3 = MakeReg(MVT::f64);
818 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
819 Tmp1 = Tmp3;
820 }
821 if (SetCC->getOperand(1).getValueType() == MVT::f32)
822 {
823 //assert (0 && "Setcc On float?\n");
824 std::cerr << "Setcc on float!\n";
825 Tmp3 = MakeReg(MVT::f64);
826 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp2);
827 Tmp2 = Tmp3;
828 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000829
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000830 if (rev) std::swap(Tmp1, Tmp2);
831 //do the comparison
832 BuildMI(BB, Opc, 2, dst).addReg(Tmp1).addReg(Tmp2);
833 return inv;
834}
835
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000836//Check to see if the load is a constant offset from a base register
837void ISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
838{
839 unsigned opcode = N.getOpcode();
840 if (opcode == ISD::ADD) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000841 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000842 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
843 { //Normal imm add
844 Reg = SelectExpr(N.getOperand(0));
845 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
846 return;
847 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000848 else if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000849 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 32767)
850 {
851 Reg = SelectExpr(N.getOperand(1));
852 offset = cast<ConstantSDNode>(N.getOperand(0))->getValue();
853 return;
854 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000855 }
856 Reg = SelectExpr(N);
857 offset = 0;
858 return;
859}
860
Andrew Lenharth445171a2005-02-08 00:40:03 +0000861void ISel::SelectBranchCC(SDOperand N)
862{
863 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000864 MachineBasicBlock *Dest =
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000865 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
866 unsigned Opc = Alpha::WTF;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000867
Andrew Lenharth445171a2005-02-08 00:40:03 +0000868 Select(N.getOperand(0)); //chain
869 SDOperand CC = N.getOperand(1);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000870
Andrew Lenharth445171a2005-02-08 00:40:03 +0000871 if (CC.getOpcode() == ISD::SETCC)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000872 {
873 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
874 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
875 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000876 bool LeftZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
877 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
878 bool RightZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
879 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000880 bool isNE = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000881
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000882 //Fix up CC
883 ISD::CondCode cCode= SetCC->getCondition();
884 if (LeftZero && !RightZero) //Swap Operands
885 cCode = ISD::getSetCCSwappedOperands(cCode);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000886
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000887 if(cCode == ISD::SETNE)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000888 isNE = true;
Andrew Lenharth445171a2005-02-08 00:40:03 +0000889
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000890 if (LeftZero || RightZero) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000891 switch (SetCC->getCondition()) {
892 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
893 case ISD::SETEQ: Opc = Alpha::BEQ; break;
894 case ISD::SETLT: Opc = Alpha::BLT; break;
895 case ISD::SETLE: Opc = Alpha::BLE; break;
896 case ISD::SETGT: Opc = Alpha::BGT; break;
897 case ISD::SETGE: Opc = Alpha::BGE; break;
898 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
899 case ISD::SETUGT: Opc = Alpha::BNE; break;
900 case ISD::SETULE: Opc = Alpha::BEQ; break; //Technically you could have this CC
901 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
902 case ISD::SETNE: Opc = Alpha::BNE; break;
903 }
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000904 unsigned Tmp1;
905 if(LeftZero && !RightZero) //swap Operands
906 Tmp1 = SelectExpr(SetCC->getOperand(1)); //Cond
907 else
908 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000909 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
910 return;
911 } else {
912 unsigned Tmp1 = SelectExpr(CC);
913 if (isNE)
914 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
915 else
916 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000917 return;
918 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000919 } else { //FP
920 //Any comparison between 2 values should be codegened as an folded branch, as moving
921 //CC to the integer register is very expensive
922 //for a cmp b: c = a - b;
923 //a = b: c = 0
924 //a < b: c < 0
925 //a > b: c > 0
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000926
927 bool invTest = false;
928 unsigned Tmp3;
929
930 ConstantFPSDNode *CN;
931 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
932 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
933 Tmp3 = SelectExpr(SetCC->getOperand(0));
934 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
935 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
936 {
937 Tmp3 = SelectExpr(SetCC->getOperand(1));
938 invTest = true;
939 }
940 else
941 {
942 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
943 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
944 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
945 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
946 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
947 .addReg(Tmp1).addReg(Tmp2);
948 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000949
950 switch (SetCC->getCondition()) {
951 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000952 case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
953 case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
954 case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
955 case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
956 case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
957 case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000958 }
959 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000960 return;
961 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000962 abort(); //Should never be reached
963 } else {
964 //Giveup and do the stupid thing
965 unsigned Tmp1 = SelectExpr(CC);
966 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
967 return;
968 }
Andrew Lenharth445171a2005-02-08 00:40:03 +0000969 abort(); //Should never be reached
970}
971
Andrew Lenharth40831c52005-01-28 06:57:18 +0000972unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
973{
974 unsigned Tmp1, Tmp2, Tmp3;
975 unsigned Opc = 0;
976 SDNode *Node = N.Val;
977 MVT::ValueType DestType = N.getValueType();
978 unsigned opcode = N.getOpcode();
979
980 switch (opcode) {
981 default:
982 Node->dump();
983 assert(0 && "Node not handled!\n");
Andrew Lenharth2c594352005-01-29 15:42:07 +0000984
Andrew Lenharth7332f3e2005-04-02 19:11:07 +0000985 case ISD::UNDEF: {
986 BuildMI(BB, Alpha::IDEF, 0, Result);
987 return Result;
988 }
989
Andrew Lenharth30b46d42005-04-02 19:04:58 +0000990 case ISD::FNEG:
991 if(ISD::FABS == N.getOperand(0).getOpcode())
992 {
Misha Brukman7847fca2005-04-22 17:54:37 +0000993 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
994 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +0000995 } else {
Misha Brukman7847fca2005-04-22 17:54:37 +0000996 Tmp1 = SelectExpr(N.getOperand(0));
997 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +0000998 }
999 return Result;
1000
1001 case ISD::FABS:
1002 Tmp1 = SelectExpr(N.getOperand(0));
1003 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
1004 return Result;
1005
Andrew Lenharth9818c052005-02-05 13:19:12 +00001006 case ISD::SELECT:
1007 {
Andrew Lenharth45859692005-03-03 21:47:53 +00001008 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1009 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
1010 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
1011
1012 SDOperand CC = N.getOperand(0);
1013 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
1014
Misha Brukman4633f1c2005-04-21 23:13:11 +00001015 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth45859692005-03-03 21:47:53 +00001016 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
1017 { //FP Setcc -> Select yay!
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001018
1019
Andrew Lenharth45859692005-03-03 21:47:53 +00001020 //for a cmp b: c = a - b;
1021 //a = b: c = 0
1022 //a < b: c < 0
1023 //a > b: c > 0
Misha Brukman4633f1c2005-04-21 23:13:11 +00001024
Andrew Lenharth45859692005-03-03 21:47:53 +00001025 bool invTest = false;
1026 unsigned Tmp3;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001027
Andrew Lenharth45859692005-03-03 21:47:53 +00001028 ConstantFPSDNode *CN;
1029 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
1030 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1031 Tmp3 = SelectExpr(SetCC->getOperand(0));
1032 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
1033 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1034 {
1035 Tmp3 = SelectExpr(SetCC->getOperand(1));
1036 invTest = true;
1037 }
1038 else
1039 {
1040 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
1041 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
1042 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
1043 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1044 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1045 .addReg(Tmp1).addReg(Tmp2);
1046 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001047
Andrew Lenharth45859692005-03-03 21:47:53 +00001048 switch (SetCC->getCondition()) {
1049 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
1050 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNE : Alpha::FCMOVEQ; break;
1051 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGT : Alpha::FCMOVLT; break;
1052 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGE : Alpha::FCMOVLE; break;
1053 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLT : Alpha::FCMOVGT; break;
1054 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLE : Alpha::FCMOVGE; break;
1055 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQ : Alpha::FCMOVNE; break;
1056 }
Andrew Lenharth33819132005-03-04 20:09:23 +00001057 BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
Andrew Lenharth45859692005-03-03 21:47:53 +00001058 return Result;
1059 }
1060 else
1061 {
1062 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001063 BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV).addReg(Tmp1);
1064// // Spill the cond to memory and reload it from there.
1065// unsigned Tmp4 = MakeReg(MVT::f64);
1066// MoveIntFP(Tmp1, Tmp4, true);
1067// //now ideally, we don't have to do anything to the flag...
1068// // Get the condition into the zero flag.
1069// BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
Andrew Lenharth45859692005-03-03 21:47:53 +00001070 return Result;
1071 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001072 }
1073
Andrew Lenharthc1faced2005-02-01 01:37:24 +00001074 case ISD::FP_ROUND:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001075 assert (DestType == MVT::f32 &&
1076 N.getOperand(0).getValueType() == MVT::f64 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001077 "only f64 to f32 conversion supported here");
Andrew Lenharthc1faced2005-02-01 01:37:24 +00001078 Tmp1 = SelectExpr(N.getOperand(0));
1079 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
1080 return Result;
1081
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001082 case ISD::FP_EXTEND:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001083 assert (DestType == MVT::f64 &&
1084 N.getOperand(0).getValueType() == MVT::f32 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001085 "only f32 to f64 conversion supported here");
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001086 Tmp1 = SelectExpr(N.getOperand(0));
1087 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
1088 return Result;
1089
Andrew Lenharth2c594352005-01-29 15:42:07 +00001090 case ISD::CopyFromReg:
1091 {
1092 // Make sure we generate both values.
1093 if (Result != notIn)
1094 ExprMap[N.getValue(1)] = notIn; // Generate the token
1095 else
1096 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001097
Andrew Lenharth2c594352005-01-29 15:42:07 +00001098 SDOperand Chain = N.getOperand(0);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001099
Andrew Lenharth2c594352005-01-29 15:42:07 +00001100 Select(Chain);
1101 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1102 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1103 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
1104 return Result;
1105 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001106
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001107 case ISD::LOAD:
1108 {
1109 // Make sure we generate both values.
1110 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001111 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001112 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001113 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001114
Andrew Lenharth29219162005-02-07 06:31:44 +00001115 DestType = N.getValue(0).getValueType();
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001116
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001117 SDOperand Chain = N.getOperand(0);
1118 SDOperand Address = N.getOperand(1);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001119 Select(Chain);
Andrew Lenharth65838902005-02-06 16:22:15 +00001120 Opc = DestType == MVT::f64 ? Alpha::LDT : Alpha::LDS;
1121
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001122 if (Address.getOpcode() == ISD::GlobalAddress) {
1123 AlphaLowering.restoreGP(BB);
1124 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001125 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001126 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1127 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001128 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001129 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001130 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001131 has_sym = true;
Andrew Lenharth97127a12005-02-05 17:41:39 +00001132 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001133 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001134 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001135 BuildMI(BB, Opc, 2, Result)
1136 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1137 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001138 } else {
1139 long offset;
1140 SelectAddr(Address, Tmp1, offset);
1141 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1142 }
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001143 return Result;
1144 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001145 case ISD::ConstantFP:
1146 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
1147 if (CN->isExactlyValue(+0.0)) {
1148 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001149 } else if ( CN->isExactlyValue(-0.0)) {
1150 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001151 } else {
1152 abort();
1153 }
1154 }
1155 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001156
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001157 case ISD::SDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001158 case ISD::MUL:
1159 case ISD::ADD:
1160 case ISD::SUB:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001161 switch( opcode ) {
1162 case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS; break;
1163 case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS; break;
1164 case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS; break;
1165 case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS; break;
1166 };
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001167
1168 ConstantFPSDNode *CN;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001169 if (opcode == ISD::SUB
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001170 && (CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
1171 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1172 {
1173 Tmp2 = SelectExpr(N.getOperand(1));
1174 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp2).addReg(Tmp2);
1175 } else {
1176 Tmp1 = SelectExpr(N.getOperand(0));
1177 Tmp2 = SelectExpr(N.getOperand(1));
1178 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1179 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001180 return Result;
1181
Andrew Lenharth2c594352005-01-29 15:42:07 +00001182 case ISD::EXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001183 {
1184 //include a conversion sequence for float loads to double
1185 if (Result != notIn)
1186 ExprMap[N.getValue(1)] = notIn; // Generate the token
1187 else
1188 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001189
Andrew Lenhartha549deb2005-02-07 05:33:15 +00001190 Tmp1 = MakeReg(MVT::f32);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001191
1192 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001193 "EXTLOAD not from f32");
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001194 assert(Node->getValueType(0) == MVT::f64 && "EXTLOAD not to f64");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001195
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001196 SDOperand Chain = N.getOperand(0);
1197 SDOperand Address = N.getOperand(1);
1198 Select(Chain);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001199
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001200 if (Address.getOpcode() == ISD::GlobalAddress) {
1201 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001202 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001203 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1204 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001205 else if (ConstantPoolSDNode *CP =
1206 dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001207 {
1208 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001209 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001210 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addConstantPoolIndex(CP->getIndex());
1211 }
1212 else if(Address.getOpcode() == ISD::FrameIndex) {
1213 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
Andrew Lenharth032f2352005-02-22 21:59:48 +00001214 BuildMI(BB, Alpha::LDS, 2, Tmp1)
1215 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1216 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001217 } else {
1218 long offset;
1219 SelectAddr(Address, Tmp2, offset);
1220 BuildMI(BB, Alpha::LDS, 1, Tmp1).addImm(offset).addReg(Tmp2);
1221 }
Andrew Lenharth29219162005-02-07 06:31:44 +00001222 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001223 return Result;
1224 }
Andrew Lenharth2c594352005-01-29 15:42:07 +00001225
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001226 case ISD::UINT_TO_FP:
Andrew Lenharth69520ed2005-05-26 18:18:34 +00001227 {
1228 //FIXME: First test if we will have problems with the sign bit before doing the slow thing
1229 assert (N.getOperand(0).getValueType() == MVT::i64
1230 && "only quads can be loaded from");
1231 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1232 Tmp2 = MakeReg(MVT::i64);
1233 BuildMI(BB, Alpha::SRL, 2, Tmp2).addReg(Tmp1).addImm(1);
1234 Tmp3 = MakeReg(MVT::i64);
1235 BuildMI(BB, Alpha::CMPLT, 2, Tmp3).addReg(Tmp1).addReg(Alpha::R31);
1236 unsigned Tmp4 = MakeReg(MVT::f64), Tmp5 = MakeReg(MVT::f64), Tmp6 = MakeReg(MVT::f64);
1237 MoveInt2FP(Tmp1, Tmp4, true);
1238 MoveInt2FP(Tmp2, Tmp5, true);
1239 MoveInt2FP(Tmp3, Tmp6, true);
1240 Tmp1 = MakeReg(MVT::f64);
1241 Tmp2 = MakeReg(MVT::f64);
1242 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
1243 BuildMI(BB, Opc, 1, Tmp1).addReg(Tmp4);
1244 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp5);
1245 Tmp3 = MakeReg(MVT::f64);
1246 BuildMI(BB, Alpha::ADDT, 2, Tmp3).addReg(Tmp2).addReg(Tmp2);
1247 //Ok, now tmp1 had the plain covereted
1248 //tmp3 has the reduced converted and added
1249 //tmp6 has the conditional to use
1250 BuildMI(BB, Alpha::FCMOVNE, 3, Result).addReg(Tmp1).addReg(Tmp3).addReg(Tmp6);
1251 return Result;
1252 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001253 case ISD::SINT_TO_FP:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001254 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001255 assert (N.getOperand(0).getValueType() == MVT::i64
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001256 && "only quads can be loaded from");
Andrew Lenharth40831c52005-01-28 06:57:18 +00001257 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001258 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001259 MoveInt2FP(Tmp1, Tmp2, true);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001260 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
1261 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001262 return Result;
1263 }
1264 }
1265 assert(0 && "should not get here");
1266 return 0;
1267}
1268
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001269unsigned ISel::SelectExpr(SDOperand N) {
1270 unsigned Result;
Andrew Lenharth2966e842005-04-07 18:15:28 +00001271 unsigned Tmp1, Tmp2 = 0, Tmp3;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001272 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001273 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001274
1275 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001276 MVT::ValueType DestType = N.getValueType();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001277
1278 unsigned &Reg = ExprMap[N];
1279 if (Reg) return Reg;
1280
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001281 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::TAILCALL)
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001282 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001283 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001284 else {
1285 // If this is a call instruction, make sure to prepare ALL of the result
1286 // values as well as the chain.
1287 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001288 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001289 else {
1290 Result = MakeReg(Node->getValueType(0));
1291 ExprMap[N.getValue(0)] = Result;
1292 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1293 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001294 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001295 }
1296 }
1297
Andrew Lenharth50d91d72005-04-30 14:19:13 +00001298 if ((DestType == MVT::f64 || DestType == MVT::f32 ||
1299 (
1300 (opcode == ISD::LOAD || opcode == ISD::CopyFromReg ||
1301 opcode == ISD::EXTLOAD) &&
1302 (N.getValue(0).getValueType() == MVT::f32 ||
1303 N.getValue(0).getValueType() == MVT::f64)
1304 ))
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001305 && opcode != ISD::CALL && opcode != ISD::TAILCALL
Andrew Lenharth06342c32005-02-07 06:21:37 +00001306 )
Andrew Lenharth40831c52005-01-28 06:57:18 +00001307 return SelectExprFP(N, Result);
1308
1309 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001310 default:
1311 Node->dump();
1312 assert(0 && "Node not handled!\n");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001313
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001314 case ISD::CTPOP:
1315 case ISD::CTTZ:
1316 case ISD::CTLZ:
1317 Opc = opcode == ISD::CTPOP ? Alpha::CTPOP :
1318 (opcode == ISD::CTTZ ? Alpha::CTTZ : Alpha::CTLZ);
1319 Tmp1 = SelectExpr(N.getOperand(0));
1320 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1321 return Result;
1322
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001323 case ISD::MULHU:
1324 Tmp1 = SelectExpr(N.getOperand(0));
1325 Tmp2 = SelectExpr(N.getOperand(1));
1326 BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth706be912005-04-07 13:55:53 +00001327 return Result;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001328 case ISD::MULHS:
1329 {
1330 //MULHU - Ra<63>*Rb - Rb<63>*Ra
1331 Tmp1 = SelectExpr(N.getOperand(0));
1332 Tmp2 = SelectExpr(N.getOperand(1));
1333 Tmp3 = MakeReg(MVT::i64);
1334 BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1335 unsigned V1 = MakeReg(MVT::i64);
1336 unsigned V2 = MakeReg(MVT::i64);
1337 BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31).addReg(Tmp1);
1338 BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31).addReg(Tmp2);
1339 unsigned IRes = MakeReg(MVT::i64);
1340 BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
1341 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
1342 return Result;
1343 }
Andrew Lenharth7332f3e2005-04-02 19:11:07 +00001344 case ISD::UNDEF: {
1345 BuildMI(BB, Alpha::IDEF, 0, Result);
1346 return Result;
1347 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001348
Andrew Lenharth032f2352005-02-22 21:59:48 +00001349 case ISD::DYNAMIC_STACKALLOC:
1350 // Generate both result values.
Andrew Lenharth3a7118d2005-02-23 17:33:42 +00001351 if (Result != notIn)
1352 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth032f2352005-02-22 21:59:48 +00001353 else
1354 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1355
1356 // FIXME: We are currently ignoring the requested alignment for handling
1357 // greater than the stack alignment. This will need to be revisited at some
1358 // point. Align = N.getOperand(2);
1359
1360 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1361 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1362 std::cerr << "Cannot allocate stack object with greater alignment than"
1363 << " the stack alignment yet!";
1364 abort();
1365 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001366
Andrew Lenharth032f2352005-02-22 21:59:48 +00001367 Select(N.getOperand(0));
1368 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1369 {
1370 if (CN->getValue() < 32000)
1371 {
1372 BuildMI(BB, Alpha::LDA, 2, Alpha::R30)
1373 .addImm(-CN->getValue()).addReg(Alpha::R30);
1374 } else {
1375 Tmp1 = SelectExpr(N.getOperand(1));
1376 // Subtract size from stack pointer, thereby allocating some space.
1377 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1378 }
1379 } else {
1380 Tmp1 = SelectExpr(N.getOperand(1));
1381 // Subtract size from stack pointer, thereby allocating some space.
1382 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1383 }
1384
1385 // Put a pointer to the space into the result register, by copying the stack
1386 // pointer.
Andrew Lenharth7bc47022005-02-22 23:29:25 +00001387 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
Andrew Lenharth032f2352005-02-22 21:59:48 +00001388 return Result;
1389
Andrew Lenharth33819132005-03-04 20:09:23 +00001390// case ISD::ConstantPool:
1391// Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1392// AlphaLowering.restoreGP(BB);
1393// BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(Tmp1);
1394// return Result;
Andrew Lenharth2c594352005-01-29 15:42:07 +00001395
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001396 case ISD::FrameIndex:
Andrew Lenharth032f2352005-02-22 21:59:48 +00001397 BuildMI(BB, Alpha::LDA, 2, Result)
1398 .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
1399 .addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001400 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001401
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001402 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001403 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001404 case ISD::SEXTLOAD:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001405 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001406 {
1407 // Make sure we generate both values.
1408 if (Result != notIn)
1409 ExprMap[N.getValue(1)] = notIn; // Generate the token
1410 else
1411 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001412
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001413 SDOperand Chain = N.getOperand(0);
1414 SDOperand Address = N.getOperand(1);
1415 Select(Chain);
1416
Misha Brukman4633f1c2005-04-21 23:13:11 +00001417 assert(Node->getValueType(0) == MVT::i64 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001418 "Unknown type to sign extend to.");
Andrew Lenharth03824012005-02-07 05:55:55 +00001419 if (opcode == ISD::LOAD)
1420 Opc = Alpha::LDQ;
1421 else
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001422 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
1423 default: Node->dump(); assert(0 && "Bad sign extend!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001424 case MVT::i32: Opc = Alpha::LDL;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001425 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001426 case MVT::i16: Opc = Alpha::LDWU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001427 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001428 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Misha Brukman4633f1c2005-04-21 23:13:11 +00001429 case MVT::i8: Opc = Alpha::LDBU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001430 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001431 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001432
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001433 if (Address.getOpcode() == ISD::GlobalAddress) {
1434 AlphaLowering.restoreGP(BB);
1435 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001436 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001437 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1438 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001439 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1440 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001441 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001442 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001443 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001444 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001445 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001446 BuildMI(BB, Opc, 2, Result)
1447 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1448 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001449 } else {
1450 long offset;
1451 SelectAddr(Address, Tmp1, offset);
1452 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1453 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001454 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001455 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001456
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001457 case ISD::GlobalAddress:
1458 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001459 has_sym = true;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001460 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
1461 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
1462 return Result;
1463
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001464 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001465 case ISD::CALL:
1466 {
1467 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001468
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001469 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001470 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001471
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001472 //grab the arguments
1473 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001474 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001475 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001476 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001477
Andrew Lenharth684f2292005-01-30 00:35:27 +00001478 //in reg args
1479 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001480 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001481 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001482 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +00001483 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001484 Alpha::F19, Alpha::F20, Alpha::F21};
1485 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001486 default:
1487 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001488 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001489 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001490 N.getOperand(i+2).getValueType() << "\n";
1491 assert(0 && "Unknown value type for call");
1492 case MVT::i1:
1493 case MVT::i8:
1494 case MVT::i16:
1495 case MVT::i32:
1496 case MVT::i64:
1497 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1498 break;
1499 case MVT::f32:
1500 case MVT::f64:
1501 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1502 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001503 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001504 }
Andrew Lenharth684f2292005-01-30 00:35:27 +00001505 //in mem args
1506 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001507 {
1508 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001509 default:
1510 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001511 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001512 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001513 N.getOperand(i+2).getValueType() << "\n";
1514 assert(0 && "Unknown value type for call");
1515 case MVT::i1:
1516 case MVT::i8:
1517 case MVT::i16:
1518 case MVT::i32:
1519 case MVT::i64:
1520 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1521 break;
1522 case MVT::f32:
1523 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1524 break;
1525 case MVT::f64:
1526 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1527 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001528 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001529 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001530 //build the right kind of call
1531 if (GlobalAddressSDNode *GASD =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001532 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001533 {
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001534 if (GASD->getGlobal()->isExternal()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001535 //use safe calling convention
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001536 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001537 has_sym = true;
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001538 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal());
1539 } else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001540 //use PC relative branch call
Andrew Lenharth1e0d9bd2005-04-14 17:34:20 +00001541 AlphaLowering.restoreGP(BB);
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001542 BuildMI(BB, Alpha::BSR, 1, Alpha::R26).addGlobalAddress(GASD->getGlobal(),true);
1543 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001544 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001545 else if (ExternalSymbolSDNode *ESSDN =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001546 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001547 {
1548 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001549 has_sym = true;
Andrew Lenharthba05ad62005-03-30 18:22:52 +00001550 BuildMI(BB, Alpha::CALL, 1).addExternalSymbol(ESSDN->getSymbol(), true);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001551 } else {
1552 //no need to restore GP as we are doing an indirect call
1553 Tmp1 = SelectExpr(N.getOperand(1));
1554 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
1555 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
1556 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001557
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001558 //push the result into a virtual register
Misha Brukman4633f1c2005-04-21 23:13:11 +00001559
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001560 switch (Node->getValueType(0)) {
1561 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001562 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001563 case MVT::i1:
1564 case MVT::i8:
1565 case MVT::i16:
1566 case MVT::i32:
1567 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001568 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
1569 break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001570 case MVT::f32:
1571 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001572 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
1573 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001574 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001575 return Result+N.ResNo;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001576 }
1577
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001578 case ISD::SIGN_EXTEND_INREG:
1579 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001580 //do SDIV opt for all levels of ints if not dividing by a constant
1581 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
1582 && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001583 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001584 unsigned Tmp4 = MakeReg(MVT::f64);
1585 unsigned Tmp5 = MakeReg(MVT::f64);
1586 unsigned Tmp6 = MakeReg(MVT::f64);
1587 unsigned Tmp7 = MakeReg(MVT::f64);
1588 unsigned Tmp8 = MakeReg(MVT::f64);
1589 unsigned Tmp9 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001590
1591 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1592 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1593 MoveInt2FP(Tmp1, Tmp4, true);
1594 MoveInt2FP(Tmp2, Tmp5, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001595 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Tmp4);
1596 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Tmp5);
1597 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
1598 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Tmp8);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001599 MoveFP2Int(Tmp9, Result, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001600 return Result;
1601 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001602
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001603 //Alpha has instructions for a bunch of signed 32 bit stuff
1604 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001605 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001606 switch (N.getOperand(0).getOpcode()) {
1607 case ISD::ADD:
1608 case ISD::SUB:
1609 case ISD::MUL:
1610 {
1611 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
1612 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
1613 //FIXME: first check for Scaled Adds and Subs!
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001614 ConstantSDNode* CSD = NULL;
1615 if(!isMul && N.getOperand(0).getOperand(0).getOpcode() == ISD::SHL &&
1616 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(0).getOperand(1))) &&
1617 (CSD->getValue() == 2 || CSD->getValue() == 3))
1618 {
1619 bool use4 = CSD->getValue() == 2;
1620 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1621 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1622 BuildMI(BB, isAdd?(use4?Alpha::S4ADDL:Alpha::S8ADDL):(use4?Alpha::S4SUBL:Alpha::S8SUBL),
1623 2,Result).addReg(Tmp1).addReg(Tmp2);
1624 }
1625 else if(isAdd && N.getOperand(0).getOperand(1).getOpcode() == ISD::SHL &&
1626 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1).getOperand(1))) &&
1627 (CSD->getValue() == 2 || CSD->getValue() == 3))
1628 {
1629 bool use4 = CSD->getValue() == 2;
1630 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1631 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1632 BuildMI(BB, use4?Alpha::S4ADDL:Alpha::S8ADDL, 2,Result).addReg(Tmp1).addReg(Tmp2);
1633 }
1634 else if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001635 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
1636 { //Normal imm add/sub
1637 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001638 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001639 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
1640 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001641 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001642 else
1643 { //Normal add/sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001644 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULL : Alpha::SUBL);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001645 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001646 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001647 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1648 }
1649 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001650 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001651 default: break; //Fall Though;
1652 }
1653 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001654 Tmp1 = SelectExpr(N.getOperand(0));
1655 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001656 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001657 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001658 {
1659 default:
1660 Node->dump();
1661 assert(0 && "Sign Extend InReg not there yet");
1662 break;
1663 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001664 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001665 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001666 break;
1667 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001668 case MVT::i16:
1669 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
1670 break;
1671 case MVT::i8:
1672 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
1673 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +00001674 case MVT::i1:
1675 Tmp2 = MakeReg(MVT::i64);
1676 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001677 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +00001678 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001679 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001680 return Result;
1681 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001682
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001683 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001684 {
1685 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1686 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1687 bool isConst1 = false;
1688 bool isConst2 = false;
1689 int dir;
Misha Brukman7847fca2005-04-22 17:54:37 +00001690
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001691 //Tmp1 = SelectExpr(N.getOperand(0));
1692 if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001693 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
1694 isConst1 = true;
1695 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001696 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1697 isConst2 = true;
1698
1699 switch (SetCC->getCondition()) {
1700 default: Node->dump(); assert(0 && "Unknown integer comparison!");
1701 case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001702 case ISD::SETLT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001703 Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001704 case ISD::SETLE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001705 Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001706 case ISD::SETGT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001707 Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001708 case ISD::SETGE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001709 Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001710 case ISD::SETULT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001711 Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001712 case ISD::SETUGT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001713 Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001714 case ISD::SETULE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001715 Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001716 case ISD::SETUGE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001717 Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001718 case ISD::SETNE: {//Handle this one special
1719 //std::cerr << "Alpha does not have a setne.\n";
1720 //abort();
1721 Tmp1 = SelectExpr(N.getOperand(0));
1722 Tmp2 = SelectExpr(N.getOperand(1));
1723 Tmp3 = MakeReg(MVT::i64);
1724 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001725 //Remeber we have the Inv for this CC
1726 CCInvMap[N] = Tmp3;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001727 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001728 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001729 return Result;
1730 }
1731 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001732 if (dir == 1) {
1733 Tmp1 = SelectExpr(N.getOperand(0));
1734 if (isConst2) {
1735 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1736 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1737 } else {
1738 Tmp2 = SelectExpr(N.getOperand(1));
1739 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1740 }
1741 } else if (dir == 2) {
1742 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001743 if (isConst1) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001744 Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1745 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1746 } else {
1747 Tmp2 = SelectExpr(N.getOperand(0));
1748 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1749 }
1750 } else { //dir == 0
1751 if (isConst1) {
1752 Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1753 Tmp2 = SelectExpr(N.getOperand(1));
1754 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
1755 } else if (isConst2) {
1756 Tmp1 = SelectExpr(N.getOperand(0));
1757 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1758 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
1759 } else {
1760 Tmp1 = SelectExpr(N.getOperand(0));
1761 Tmp2 = SelectExpr(N.getOperand(1));
1762 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1763 }
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001764 }
1765 } else {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001766 //do the comparison
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001767 Tmp1 = MakeReg(MVT::f64);
1768 bool inv = SelectFPSetCC(N, Tmp1);
1769
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001770 //now arrange for Result (int) to have a 1 or 0
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001771 Tmp2 = MakeReg(MVT::i64);
1772 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001773 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001774 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001775 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001776 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001777 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001778 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001779
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001780 case ISD::CopyFromReg:
1781 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001782 ++count_ins;
1783
Andrew Lenharth40831c52005-01-28 06:57:18 +00001784 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001785 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001786 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001787 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001788 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001789
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001790 SDOperand Chain = N.getOperand(0);
1791
1792 Select(Chain);
1793 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1794 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1795 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1796 return Result;
1797 }
1798
Misha Brukman4633f1c2005-04-21 23:13:11 +00001799 //Most of the plain arithmetic and logic share the same form, and the same
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001800 //constant immediate test
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001801 case ISD::XOR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001802 //Match Not
1803 if (N.getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001804 cast<ConstantSDNode>(N.getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001805 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001806 Tmp1 = SelectExpr(N.getOperand(0));
1807 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1808 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001809 }
1810 //Fall through
1811 case ISD::AND:
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001812 //handle zap
1813 if (opcode == ISD::AND && N.getOperand(1).getOpcode() == ISD::Constant)
1814 {
1815 uint64_t k = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1816 unsigned int build = 0;
1817 for(int i = 0; i < 8; ++i)
1818 {
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001819 if ((k & 0x00FF) == 0x00FF)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001820 build |= 1 << i;
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001821 else if ((k & 0x00FF) != 0)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001822 { build = 0; break; }
1823 k >>= 8;
1824 }
1825 if (build)
1826 {
1827 Tmp1 = SelectExpr(N.getOperand(0));
1828 BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1829 return Result;
1830 }
1831 }
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001832 case ISD::OR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001833 //Check operand(0) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001834 if (N.getOperand(0).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001835 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001836 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001837 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001838 switch(opcode) {
1839 case ISD::AND: Opc = Alpha::BIC; break;
1840 case ISD::OR: Opc = Alpha::ORNOT; break;
1841 case ISD::XOR: Opc = Alpha::EQV; break;
1842 }
1843 Tmp1 = SelectExpr(N.getOperand(1));
1844 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1845 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1846 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001847 }
1848 //Check operand(1) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001849 if (N.getOperand(1).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001850 N.getOperand(1).getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001851 cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001852 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001853 switch(opcode) {
1854 case ISD::AND: Opc = Alpha::BIC; break;
1855 case ISD::OR: Opc = Alpha::ORNOT; break;
1856 case ISD::XOR: Opc = Alpha::EQV; break;
1857 }
1858 Tmp1 = SelectExpr(N.getOperand(0));
1859 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1860 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1861 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001862 }
1863 //Fall through
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001864 case ISD::SHL:
1865 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001866 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001867 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001868 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1869 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001870 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001871 {
1872 switch(opcode) {
1873 case ISD::AND: Opc = Alpha::ANDi; break;
1874 case ISD::OR: Opc = Alpha::BISi; break;
1875 case ISD::XOR: Opc = Alpha::XORi; break;
1876 case ISD::SHL: Opc = Alpha::SLi; break;
1877 case ISD::SRL: Opc = Alpha::SRLi; break;
1878 case ISD::SRA: Opc = Alpha::SRAi; break;
1879 case ISD::MUL: Opc = Alpha::MULQi; break;
1880 };
1881 Tmp1 = SelectExpr(N.getOperand(0));
1882 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1883 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1884 } else {
1885 switch(opcode) {
1886 case ISD::AND: Opc = Alpha::AND; break;
1887 case ISD::OR: Opc = Alpha::BIS; break;
1888 case ISD::XOR: Opc = Alpha::XOR; break;
1889 case ISD::SHL: Opc = Alpha::SL; break;
1890 case ISD::SRL: Opc = Alpha::SRL; break;
1891 case ISD::SRA: Opc = Alpha::SRA; break;
1892 case ISD::MUL: Opc = Alpha::MULQ; break;
1893 };
1894 Tmp1 = SelectExpr(N.getOperand(0));
1895 Tmp2 = SelectExpr(N.getOperand(1));
1896 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1897 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001898 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001899
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001900 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001901 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001902 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001903 bool isAdd = opcode == ISD::ADD;
1904
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001905 //first check for Scaled Adds and Subs!
1906 //Valid for add and sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001907 ConstantSDNode* CSD = NULL;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001908 if(N.getOperand(0).getOpcode() == ISD::SHL &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001909 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
1910 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001911 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001912 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001913 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001914 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) && CSD->getValue() <= 255)
1915 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1916 2, Result).addReg(Tmp2).addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001917 else {
1918 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001919 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1920 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001921 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001922 }
1923 //Position prevents subs
Andrew Lenharth273a1f92005-04-07 14:18:13 +00001924 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001925 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) &&
1926 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001927 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001928 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001929 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001930 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(0))) && CSD->getValue() <= 255)
1931 BuildMI(BB, use4?Alpha::S4ADDQi:Alpha::S8ADDQi, 2, Result).addReg(Tmp2)
1932 .addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001933 else {
1934 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001935 BuildMI(BB, use4?Alpha::S4ADDQ:Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001936 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001937 }
1938 //small addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001939 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
1940 CSD->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001941 { //Normal imm add/sub
1942 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1943 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001944 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CSD->getValue());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001945 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001946 //larger addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001947 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
1948 CSD->getSignExtended() <= 32767 &&
1949 CSD->getSignExtended() >= -32767)
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001950 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001951 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001952 Tmp2 = (long)CSD->getSignExtended();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001953 if (!isAdd)
1954 Tmp2 = -Tmp2;
1955 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001956 }
1957 //give up and do the operation
1958 else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001959 //Normal add/sub
1960 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1961 Tmp1 = SelectExpr(N.getOperand(0));
1962 Tmp2 = SelectExpr(N.getOperand(1));
1963 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1964 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001965 return Result;
1966 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001967
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001968 case ISD::SDIV:
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001969 {
Andrew Lenhartha565c272005-04-06 22:03:13 +00001970 ConstantSDNode* CSD;
1971 //check if we can convert into a shift!
1972 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
1973 (int64_t)CSD->getSignExtended() != 0 &&
1974 ExactLog2(abs((int64_t)CSD->getSignExtended())) != 0)
1975 {
1976 unsigned k = ExactLog2(abs(CSD->getSignExtended()));
1977 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenhartha565c272005-04-06 22:03:13 +00001978 if (k == 1)
1979 Tmp2 = Tmp1;
1980 else
1981 {
1982 Tmp2 = MakeReg(MVT::i64);
1983 BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
1984 }
1985 Tmp3 = MakeReg(MVT::i64);
1986 BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
1987 unsigned Tmp4 = MakeReg(MVT::i64);
1988 BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
1989 if ((int64_t)CSD->getSignExtended() > 0)
1990 BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
1991 else
1992 {
1993 unsigned Tmp5 = MakeReg(MVT::i64);
1994 BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
1995 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
1996 }
1997 return Result;
1998 }
1999 }
2000 //Else fall through
2001
2002 case ISD::UDIV:
2003 {
2004 ConstantSDNode* CSD;
2005 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
2006 ((int64_t)CSD->getSignExtended() >= 2 ||
2007 (int64_t)CSD->getSignExtended() <= -2))
2008 {
2009 // If this is a divide by constant, we can emit code using some magic
2010 // constants to implement it as a multiply instead.
2011 ExprMap.erase(N);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002012 if (opcode == ISD::SDIV)
Andrew Lenhartha565c272005-04-06 22:03:13 +00002013 return SelectExpr(BuildSDIVSequence(N));
2014 else
2015 return SelectExpr(BuildUDIVSequence(N));
2016 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002017 }
2018 //else fall though
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002019 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +00002020 case ISD::SREM:
Misha Brukman4633f1c2005-04-21 23:13:11 +00002021 //FIXME: alpha really doesn't support any of these operations,
Andrew Lenharth40831c52005-01-28 06:57:18 +00002022 // the ops are expanded into special library calls with
2023 // special calling conventions
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002024 //Restore GP because it is a call after all...
Andrew Lenharth40831c52005-01-28 06:57:18 +00002025 switch(opcode) {
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00002026 case ISD::UREM: Opc = Alpha::REMQU; break;
2027 case ISD::SREM: Opc = Alpha::REMQ; break;
2028 case ISD::UDIV: Opc = Alpha::DIVQU; break;
2029 case ISD::SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002030 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002031 Tmp1 = SelectExpr(N.getOperand(0));
2032 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth33819132005-03-04 20:09:23 +00002033 //set up regs explicitly (helps Reg alloc)
2034 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002035 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00002036 AlphaLowering.restoreGP(BB);
Andrew Lenharth33819132005-03-04 20:09:23 +00002037 BuildMI(BB, Opc, 2).addReg(Alpha::R24).addReg(Alpha::R25);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002038 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002039 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002040
Andrew Lenharthe76797c2005-02-01 20:40:27 +00002041 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002042 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002043 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002044 assert (DestType == MVT::i64 && "only quads can be loaded to");
2045 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00002046 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002047 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002048 if (SrcType == MVT::f32)
Misha Brukman7847fca2005-04-22 17:54:37 +00002049 {
2050 Tmp2 = MakeReg(MVT::f64);
2051 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
2052 Tmp1 = Tmp2;
2053 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002054 Tmp2 = MakeReg(MVT::f64);
2055 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00002056 MoveFP2Int(Tmp2, Result, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002057
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002058 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002059 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002060
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002061 case ISD::SELECT:
2062 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00002063 //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 +00002064 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002065 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2066 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002067 // Get the condition into the zero flag.
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002068 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002069
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002070 SDOperand CC = N.getOperand(0);
2071 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
2072
Misha Brukman4633f1c2005-04-21 23:13:11 +00002073 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002074 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
2075 { //FP Setcc -> Int Select
Misha Brukman7847fca2005-04-22 17:54:37 +00002076 Tmp1 = MakeReg(MVT::f64);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002077 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2078 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Misha Brukman7847fca2005-04-22 17:54:37 +00002079 bool inv = SelectFPSetCC(CC, Tmp1);
2080 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
2081 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
2082 return Result;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002083 }
2084 if (CC.getOpcode() == ISD::SETCC) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002085 //Int SetCC -> Select
2086 //Dropping the CC is only useful if we are comparing to 0
2087 if((SetCC->getOperand(1).getOpcode() == ISD::Constant &&
2088 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0) ||
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002089 (SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2090 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0))
2091 {
2092 //figure out a few things
2093 bool LeftZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2094 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
2095 bool RightZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2096 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
2097 bool LeftConst = N.getOperand(1).getOpcode() == ISD::Constant &&
2098 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255;
2099 bool RightConst = N.getOperand(2).getOpcode() == ISD::Constant &&
2100 cast<ConstantSDNode>(N.getOperand(2))->getValue() <= 255;
2101 bool useImm = LeftConst || RightConst;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002102
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002103 //Fix up CC
2104 ISD::CondCode cCode= SetCC->getCondition();
2105 if (RightConst && !LeftConst) //Invert sense to get Imm field right
2106 cCode = ISD::getSetCCInverse(cCode, true);
2107 if (LeftZero && !RightZero) //Swap Operands
2108 cCode = ISD::getSetCCSwappedOperands(cCode);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002109
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002110 //Choose the CMOV
2111 switch (cCode) {
2112 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
2113 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
2114 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
2115 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
2116 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
2117 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
2118 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
2119 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2120 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break; //Technically you could have this CC
2121 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
2122 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2123 }
2124 if(LeftZero && !RightZero) //swap Operands
2125 Tmp1 = SelectExpr(SetCC->getOperand(1)); //Cond
2126 else
2127 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
2128
2129 if (LeftConst) {
2130 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2131 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
Misha Brukman7847fca2005-04-22 17:54:37 +00002132 .addImm(cast<ConstantSDNode>(N.getOperand(1))->getValue())
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002133 .addReg(Tmp1);
2134 } else if (RightConst) {
2135 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
2136 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
Misha Brukman7847fca2005-04-22 17:54:37 +00002137 .addImm(cast<ConstantSDNode>(N.getOperand(2))->getValue())
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002138 .addReg(Tmp1);
2139 } else {
2140 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2141 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2142 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
2143 }
2144 return Result;
2145 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002146 //Otherwise, fall though
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002147 }
2148 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002149 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2150 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthe76797c2005-02-01 20:40:27 +00002151 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002152
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002153 return Result;
2154 }
2155
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002156 case ISD::Constant:
2157 {
Andrew Lenharthc0513832005-03-29 19:24:04 +00002158 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002159 if (val <= IMM_HIGH && val >= IMM_LOW) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002160 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002161 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002162 else if (val <= (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT &&
2163 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
2164 Tmp1 = MakeReg(MVT::i64);
2165 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val)).addReg(Alpha::R31);
2166 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002167 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002168 else {
2169 MachineConstantPool *CP = BB->getParent()->getConstantPool();
2170 ConstantUInt *C = ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
2171 unsigned CPI = CP->getConstantPoolIndex(C);
2172 AlphaLowering.restoreGP(BB);
2173 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CPI);
2174 }
2175 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002176 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002177 }
2178
2179 return 0;
2180}
2181
2182void ISel::Select(SDOperand N) {
2183 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00002184 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002185
Nate Begeman85fdeb22005-03-24 04:39:54 +00002186 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002187 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002188
2189 SDNode *Node = N.Val;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002190
Andrew Lenharth760270d2005-02-07 23:02:23 +00002191 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002192
2193 default:
2194 Node->dump(); std::cerr << "\n";
2195 assert(0 && "Node not handled yet!");
2196
2197 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00002198 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002199 return;
2200 }
2201
2202 case ISD::BR: {
2203 MachineBasicBlock *Dest =
2204 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2205
2206 Select(N.getOperand(0));
2207 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
2208 return;
2209 }
2210
2211 case ISD::ImplicitDef:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002212 ++count_ins;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002213 Select(N.getOperand(0));
2214 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
2215 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002216
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002217 case ISD::EntryToken: return; // Noop
2218
2219 case ISD::TokenFactor:
2220 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2221 Select(Node->getOperand(i));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002222
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002223 //N.Val->dump(); std::cerr << "\n";
2224 //assert(0 && "Node not handled yet!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00002225
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002226 return;
2227
2228 case ISD::CopyToReg:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002229 ++count_outs;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002230 Select(N.getOperand(0));
2231 Tmp1 = SelectExpr(N.getOperand(1));
2232 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002233
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002234 if (Tmp1 != Tmp2) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002235 if (N.getOperand(1).getValueType() == MVT::f64 ||
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002236 N.getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00002237 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2238 else
2239 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002240 }
2241 return;
2242
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002243 case ISD::RET:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002244 ++count_outs;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002245 switch (N.getNumOperands()) {
2246 default:
2247 std::cerr << N.getNumOperands() << "\n";
2248 for (unsigned i = 0; i < N.getNumOperands(); ++i)
2249 std::cerr << N.getOperand(i).getValueType() << "\n";
2250 Node->dump();
2251 assert(0 && "Unknown return instruction!");
2252 case 2:
2253 Select(N.getOperand(0));
2254 Tmp1 = SelectExpr(N.getOperand(1));
2255 switch (N.getOperand(1).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002256 default: Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002257 assert(0 && "All other types should have been promoted!!");
2258 case MVT::f64:
2259 case MVT::f32:
2260 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
2261 break;
2262 case MVT::i32:
2263 case MVT::i64:
2264 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
2265 break;
2266 }
2267 break;
2268 case 1:
2269 Select(N.getOperand(0));
2270 break;
2271 }
2272 //Tmp2 = AlphaLowering.getRetAddr();
2273 //BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(Tmp2).addReg(Tmp2);
2274 BuildMI(BB, Alpha::RETURN, 0); // Just emit a 'ret' instruction
2275 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002276
Misha Brukman4633f1c2005-04-21 23:13:11 +00002277 case ISD::TRUNCSTORE:
2278 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002279 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002280 SDOperand Chain = N.getOperand(0);
2281 SDOperand Value = N.getOperand(1);
2282 SDOperand Address = N.getOperand(2);
2283 Select(Chain);
2284
2285 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00002286
2287 if (opcode == ISD::STORE) {
2288 switch(Value.getValueType()) {
2289 default: assert(0 && "unknown Type in store");
2290 case MVT::i64: Opc = Alpha::STQ; break;
2291 case MVT::f64: Opc = Alpha::STT; break;
2292 case MVT::f32: Opc = Alpha::STS; break;
2293 }
2294 } else { //ISD::TRUNCSTORE
2295 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2296 default: assert(0 && "unknown Type in store");
2297 case MVT::i1: //FIXME: DAG does not promote this load
2298 case MVT::i8: Opc = Alpha::STB; break;
2299 case MVT::i16: Opc = Alpha::STW; break;
2300 case MVT::i32: Opc = Alpha::STL; break;
2301 }
Andrew Lenharth65838902005-02-06 16:22:15 +00002302 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00002303
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002304 if (Address.getOpcode() == ISD::GlobalAddress)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002305 {
2306 AlphaLowering.restoreGP(BB);
2307 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002308 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002309 BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
2310 }
Andrew Lenharth05380342005-02-07 05:07:00 +00002311 else if(Address.getOpcode() == ISD::FrameIndex)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002312 {
Andrew Lenharth032f2352005-02-22 21:59:48 +00002313 BuildMI(BB, Opc, 3).addReg(Tmp1)
2314 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
2315 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002316 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002317 else
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002318 {
2319 long offset;
2320 SelectAddr(Address, Tmp2, offset);
2321 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2322 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002323 return;
2324 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002325
2326 case ISD::EXTLOAD:
2327 case ISD::SEXTLOAD:
2328 case ISD::ZEXTLOAD:
2329 case ISD::LOAD:
2330 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002331 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002332 case ISD::CALL:
Andrew Lenharth032f2352005-02-22 21:59:48 +00002333 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002334 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002335 SelectExpr(N);
2336 return;
2337
Chris Lattner16cd04d2005-05-12 23:24:06 +00002338 case ISD::CALLSEQ_START:
2339 case ISD::CALLSEQ_END:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002340 Select(N.getOperand(0));
2341 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002342
Chris Lattner16cd04d2005-05-12 23:24:06 +00002343 Opc = N.getOpcode() == ISD::CALLSEQ_START ? Alpha::ADJUSTSTACKDOWN :
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002344 Alpha::ADJUSTSTACKUP;
2345 BuildMI(BB, Opc, 1).addImm(Tmp1);
2346 return;
Andrew Lenharth95762122005-03-31 21:24:06 +00002347
2348 case ISD::PCMARKER:
2349 Select(N.getOperand(0)); //Chain
2350 BuildMI(BB, Alpha::PCLABEL, 2).addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
2351 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002352 }
2353 assert(0 && "Should not be reached!");
2354}
2355
2356
2357/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
2358/// into a machine code representation using pattern matching and a machine
2359/// description file.
2360///
2361FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002362 return new ISel(TM);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002363}
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002364