blob: 5092df1fc330e01dea6f9b794a6ed290b9dc08bf [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!");
174// case ISD::SINT_TO_FP:
175// {
176// assert (Op.getOperand(0).getValueType() == MVT::i64
177// && "only quads can be loaded from");
178// SDOperand SRC;
179// if (EnableAlphaFTOI)
180// {
181// std::vector<MVT::ValueType> RTs;
182// RTs.push_back(Op.getValueType());
183// std::vector<SDOperand> Ops;
184// Ops.push_back(Op.getOperand(0));
185// SRC = DAG.getNode(AlphaISD::ITOF, RTs, Ops);
186// } else {
187// int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
188// SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
189// SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
190// Op.getOperand(0), StackSlot, DAG.getSrcValue(NULL));
191// SRC = DAG.getLoad(Op.getValueType(), Store.getValue(0), StackSlot,
192// DAG.getSrcValue(NULL));
193// }
194// std::vector<MVT::ValueType> RTs;
195// RTs.push_back(Op.getValueType());
196// std::vector<SDOperand> Ops;
197// Ops.push_back(SRC);
198// return DAG.getNode(AlphaISD::CVTQ, RTs, Ops);
199// }
200 }
201}
202
203
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000204/// AddLiveIn - This helper function adds the specified physical register to the
205/// MachineFunction as a live in value. It also creates a corresponding virtual
206/// register for it.
207static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
208 TargetRegisterClass *RC) {
209 assert(RC->contains(PReg) && "Not the correct regclass!");
210 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
211 MF.addLiveIn(PReg, VReg);
212 return VReg;
213}
214
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000215//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
216
217//For now, just use variable size stack frame format
218
219//In a standard call, the first six items are passed in registers $16
220//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
221//of argument-to-register correspondence.) The remaining items are
222//collected in a memory argument list that is a naturally aligned
223//array of quadwords. In a standard call, this list, if present, must
224//be passed at 0(SP).
Misha Brukman7847fca2005-04-22 17:54:37 +0000225//7 ... n 0(SP) ... (n-7)*8(SP)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000226
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000227// //#define FP $15
228// //#define RA $26
229// //#define PV $27
230// //#define GP $29
231// //#define SP $30
Misha Brukman4633f1c2005-04-21 23:13:11 +0000232
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000233std::vector<SDOperand>
Misha Brukman4633f1c2005-04-21 23:13:11 +0000234AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000235{
236 std::vector<SDOperand> ArgValues;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000237
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000238 MachineFunction &MF = DAG.getMachineFunction();
Andrew Lenharth05380342005-02-07 05:07:00 +0000239 MachineFrameInfo*MFI = MF.getFrameInfo();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000240
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000241 MachineBasicBlock& BB = MF.front();
242
243 //Handle the return address
244 //BuildMI(&BB, Alpha::IDEF, 0, Alpha::R26);
245
Misha Brukman4633f1c2005-04-21 23:13:11 +0000246 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000247 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000248 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000249 Alpha::F19, Alpha::F20, Alpha::F21};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000250 int count = 0;
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000251
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000252 GP = AddLiveIn(MF, Alpha::R29, getRegClassFor(MVT::i64));
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000253
Chris Lattnere4d5c442005-03-15 04:54:21 +0000254 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000255 {
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000256 SDOperand argt;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000257 if (count < 6) {
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000258 unsigned Vreg;
259 MVT::ValueType VT = getValueType(I->getType());
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000260 switch (VT) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000261 default:
262 std::cerr << "Unknown Type " << VT << "\n";
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000263 abort();
264 case MVT::f64:
265 case MVT::f32:
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000266 args_float[count] = AddLiveIn(MF,args_float[count], getRegClassFor(VT));
267 argt = DAG.getCopyFromReg(args_float[count], VT, DAG.getRoot());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000268 break;
269 case MVT::i1:
270 case MVT::i8:
271 case MVT::i16:
272 case MVT::i32:
273 case MVT::i64:
Andrew Lenharth591ec572005-05-31 18:42:18 +0000274 args_int[count] = AddLiveIn(MF, args_int[count], getRegClassFor(MVT::i64));
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000275 argt = DAG.getCopyFromReg(args_int[count], VT, DAG.getRoot());
Andrew Lenharth14f30c92005-05-31 18:37:16 +0000276 if (VT != MVT::i64)
277 argt = DAG.getNode(ISD::TRUNCATE, VT, argt);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000278 break;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000279 }
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000280 DAG.setRoot(argt.getValue(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000281 } else { //more args
282 // Create the frame index object for this incoming parameter...
283 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000284
285 // Create the SelectionDAG nodes corresponding to a load
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000286 //from this parameter
287 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000288 argt = DAG.getLoad(getValueType(I->getType()),
289 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000290 }
Andrew Lenharth032f2352005-02-22 21:59:48 +0000291 ++count;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000292 ArgValues.push_back(argt);
293 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000294
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000295 // If the functions takes variable number of arguments, copy all regs to stack
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000296 if (F.isVarArg()) {
297 std::vector<SDOperand> LS;
298 for (int i = 0; i < 6; ++i) {
299 if (args_int[i] < 1024)
300 args_int[i] = AddLiveIn(MF,args_int[i], getRegClassFor(MVT::i64));
301 SDOperand argt = DAG.getCopyFromReg(args_int[i], MVT::i64, DAG.getRoot());
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000302 int FI = MFI->CreateFixedObject(8, -8 * (6 - i));
303 SDOperand SDFI = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000304 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt, SDFI, DAG.getSrcValue(NULL)));
305
306 if (args_float[i] < 1024)
307 args_float[i] = AddLiveIn(MF,args_float[i], getRegClassFor(MVT::f64));
308 argt = DAG.getCopyFromReg(args_float[i], MVT::f64, DAG.getRoot());
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000309 FI = MFI->CreateFixedObject(8, - 8 * (12 - i));
310 SDFI = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000311 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt, SDFI, DAG.getSrcValue(NULL)));
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000312 }
313
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000314 //Set up a token factor with all the stack traffic
315 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, LS));
316 }
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000317
318 // Finally, inform the code generator which regs we return values in.
319 switch (getValueType(F.getReturnType())) {
320 default: assert(0 && "Unknown type!");
321 case MVT::isVoid: break;
322 case MVT::i1:
323 case MVT::i8:
324 case MVT::i16:
325 case MVT::i32:
326 case MVT::i64:
327 MF.addLiveOut(Alpha::R0);
328 break;
329 case MVT::f32:
330 case MVT::f64:
331 MF.addLiveOut(Alpha::F0);
332 break;
333 }
334
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000335 //return the arguments
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000336 return ArgValues;
337}
338
339std::pair<SDOperand, SDOperand>
340AlphaTargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000341 const Type *RetTy, bool isVarArg,
Chris Lattneradf6a962005-05-13 18:50:42 +0000342 unsigned CallingConv, bool isTailCall,
Misha Brukman7847fca2005-04-22 17:54:37 +0000343 SDOperand Callee, ArgListTy &Args,
344 SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000345 int NumBytes = 0;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000346 if (Args.size() > 6)
347 NumBytes = (Args.size() - 6) * 8;
348
Chris Lattner16cd04d2005-05-12 23:24:06 +0000349 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000350 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000351 std::vector<SDOperand> args_to_use;
352 for (unsigned i = 0, e = Args.size(); i != e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000353 {
354 switch (getValueType(Args[i].second)) {
355 default: assert(0 && "Unexpected ValueType for argument!");
356 case MVT::i1:
357 case MVT::i8:
358 case MVT::i16:
359 case MVT::i32:
360 // Promote the integer to 64 bits. If the input type is signed use a
361 // sign extend, otherwise use a zero extend.
362 if (Args[i].second->isSigned())
363 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
364 else
365 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
366 break;
367 case MVT::i64:
368 case MVT::f64:
369 case MVT::f32:
370 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000371 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000372 args_to_use.push_back(Args[i].first);
373 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000374
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000375 std::vector<MVT::ValueType> RetVals;
376 MVT::ValueType RetTyVT = getValueType(RetTy);
377 if (RetTyVT != MVT::isVoid)
378 RetVals.push_back(RetTyVT);
379 RetVals.push_back(MVT::Other);
380
Misha Brukman4633f1c2005-04-21 23:13:11 +0000381 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000382 Chain, Callee, args_to_use), 0);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000383 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner16cd04d2005-05-12 23:24:06 +0000384 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000385 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000386 return std::make_pair(TheCall, Chain);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000387}
388
389std::pair<SDOperand, SDOperand>
390AlphaTargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
391 //vastart just returns the address of the VarArgsFrameIndex slot.
392 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64), Chain);
393}
394
395std::pair<SDOperand,SDOperand> AlphaTargetLowering::
396LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000397 const Type *ArgTy, SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000398 abort();
399}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000400
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000401
402std::pair<SDOperand, SDOperand> AlphaTargetLowering::
403LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
404 SelectionDAG &DAG) {
405 abort();
406}
407
408
409
410
411
412namespace {
413
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000414//===--------------------------------------------------------------------===//
415/// ISel - Alpha specific code to select Alpha machine instructions for
416/// SelectionDAG operations.
417//===--------------------------------------------------------------------===//
418class ISel : public SelectionDAGISel {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000419
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000420 /// AlphaLowering - This object fully describes how to lower LLVM code to an
421 /// Alpha-specific SelectionDAG.
422 AlphaTargetLowering AlphaLowering;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000423
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000424 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
425 // for sdiv and udiv until it is put into the future
426 // dag combiner.
427
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000428 /// ExprMap - As shared expressions are codegen'd, we keep track of which
429 /// vreg the value is produced in, so we only emit one copy of each compiled
430 /// tree.
431 static const unsigned notIn = (unsigned)(-1);
432 std::map<SDOperand, unsigned> ExprMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000433
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000434 //CCInvMap sometimes (SetNE) we have the inverse CC code for free
435 std::map<SDOperand, unsigned> CCInvMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000436
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000437 int count_ins;
438 int count_outs;
439 bool has_sym;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000440 int max_depth;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000441
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000442public:
Misha Brukman4633f1c2005-04-21 23:13:11 +0000443 ISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering), AlphaLowering(TM)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000444 {}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000445
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000446 /// InstructionSelectBasicBlock - This callback is invoked by
447 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
448 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
Andrew Lenharth032f2352005-02-22 21:59:48 +0000449 DEBUG(BB->dump());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000450 count_ins = 0;
451 count_outs = 0;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000452 max_depth = 0;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000453 has_sym = false;
454
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000455 // Codegen the basic block.
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000456 ISelDAG = &DAG;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000457 max_depth = DAG.getRoot().getNodeDepth();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000458 Select(DAG.getRoot());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000459
460 if(has_sym)
461 ++count_ins;
462 if(EnableAlphaCount)
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000463 std::cerr << "COUNT: " << BB->getParent()->getFunction ()->getName() << " "
464 << BB->getNumber() << " "
465 << max_depth << " "
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000466 << count_ins << " "
467 << count_outs << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000468
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000469 // Clear state used for selection.
470 ExprMap.clear();
471 CCInvMap.clear();
472 }
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000473
474 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000475
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000476 unsigned SelectExpr(SDOperand N);
477 unsigned SelectExprFP(SDOperand N, unsigned Result);
478 void Select(SDOperand N);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000479
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000480 void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
481 void SelectBranchCC(SDOperand N);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000482 void MoveFP2Int(unsigned src, unsigned dst, bool isDouble);
483 void MoveInt2FP(unsigned src, unsigned dst, bool isDouble);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000484 //returns whether the sense of the comparison was inverted
485 bool SelectFPSetCC(SDOperand N, unsigned dst);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000486
487 // dag -> dag expanders for integer divide by constant
488 SDOperand BuildSDIVSequence(SDOperand N);
489 SDOperand BuildUDIVSequence(SDOperand N);
490
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000491};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000492}
493
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000494void ISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
495 // If this function has live-in values, emit the copies from pregs to vregs at
496 // the top of the function, before anything else.
497 MachineBasicBlock *BB = MF.begin();
498 if (MF.livein_begin() != MF.livein_end()) {
499 SSARegMap *RegMap = MF.getSSARegMap();
500 for (MachineFunction::livein_iterator LI = MF.livein_begin(),
501 E = MF.livein_end(); LI != E; ++LI) {
502 const TargetRegisterClass *RC = RegMap->getRegClass(LI->second);
503 if (RC == Alpha::GPRCRegisterClass) {
504 BuildMI(BB, Alpha::BIS, 2, LI->second).addReg(LI->first).addReg(LI->first);
505 } else if (RC == Alpha::FPRCRegisterClass) {
506 BuildMI(BB, Alpha::CPYS, 2, LI->second).addReg(LI->first).addReg(LI->first);
507 } else {
508 assert(0 && "Unknown regclass!");
509 }
510 }
511 }
512}
513
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000514//Factorize a number using the list of constants
515static bool factorize(int v[], int res[], int size, uint64_t c)
516{
517 bool cont = true;
518 while (c != 1 && cont)
519 {
520 cont = false;
521 for(int i = 0; i < size; ++i)
522 {
523 if (c % v[i] == 0)
524 {
525 c /= v[i];
526 ++res[i];
527 cont=true;
528 }
529 }
530 }
531 return c == 1;
532}
533
534
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000535//Shamelessly adapted from PPC32
Misha Brukman4633f1c2005-04-21 23:13:11 +0000536// Structure used to return the necessary information to codegen an SDIV as
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000537// a multiply.
538struct ms {
539 int64_t m; // magic number
540 int64_t s; // shift amount
541};
542
543struct mu {
544 uint64_t m; // magic number
545 int64_t a; // add indicator
546 int64_t s; // shift amount
547};
548
549/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukman4633f1c2005-04-21 23:13:11 +0000550/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000551/// or -1.
552static struct ms magic(int64_t d) {
553 int64_t p;
554 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
555 const uint64_t two63 = 9223372036854775808ULL; // 2^63
556 struct ms mag;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000557
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000558 ad = abs(d);
559 t = two63 + ((uint64_t)d >> 63);
560 anc = t - 1 - t%ad; // absolute value of nc
Andrew Lenharth320174f2005-04-07 17:19:16 +0000561 p = 63; // initialize p
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000562 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
563 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
564 q2 = two63/ad; // initialize q2 = 2p/abs(d)
565 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
566 do {
567 p = p + 1;
568 q1 = 2*q1; // update q1 = 2p/abs(nc)
569 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
570 if (r1 >= anc) { // must be unsigned comparison
571 q1 = q1 + 1;
572 r1 = r1 - anc;
573 }
574 q2 = 2*q2; // update q2 = 2p/abs(d)
575 r2 = 2*r2; // update r2 = rem(2p/abs(d))
576 if (r2 >= ad) { // must be unsigned comparison
577 q2 = q2 + 1;
578 r2 = r2 - ad;
579 }
580 delta = ad - r2;
581 } while (q1 < delta || (q1 == delta && r1 == 0));
582
583 mag.m = q2 + 1;
584 if (d < 0) mag.m = -mag.m; // resulting magic number
585 mag.s = p - 64; // resulting shift
586 return mag;
587}
588
589/// magicu - calculate the magic numbers required to codegen an integer udiv as
590/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
591static struct mu magicu(uint64_t d)
592{
593 int64_t p;
594 uint64_t nc, delta, q1, r1, q2, r2;
595 struct mu magu;
596 magu.a = 0; // initialize "add" indicator
597 nc = - 1 - (-d)%d;
Andrew Lenharth320174f2005-04-07 17:19:16 +0000598 p = 63; // initialize p
599 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
600 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
601 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
602 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000603 do {
604 p = p + 1;
605 if (r1 >= nc - r1 ) {
606 q1 = 2*q1 + 1; // update q1
607 r1 = 2*r1 - nc; // update r1
608 }
609 else {
610 q1 = 2*q1; // update q1
611 r1 = 2*r1; // update r1
612 }
613 if (r2 + 1 >= d - r2) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000614 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000615 q2 = 2*q2 + 1; // update q2
616 r2 = 2*r2 + 1 - d; // update r2
617 }
618 else {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000619 if (q2 >= 0x8000000000000000ull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000620 q2 = 2*q2; // update q2
621 r2 = 2*r2 + 1; // update r2
622 }
623 delta = d - 1 - r2;
624 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
625 magu.m = q2 + 1; // resulting magic number
Andrew Lenharth320174f2005-04-07 17:19:16 +0000626 magu.s = p - 64; // resulting shift
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000627 return magu;
628}
629
630/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
631/// return a DAG expression to select that will generate the same value by
632/// multiplying by a magic number. See:
633/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
634SDOperand ISel::BuildSDIVSequence(SDOperand N) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000635 int64_t d = (int64_t)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000636 ms magics = magic(d);
637 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000638 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000639 ISelDAG->getConstant(magics.m, MVT::i64));
640 // If d > 0 and m < 0, add the numerator
641 if (d > 0 && magics.m < 0)
642 Q = ISelDAG->getNode(ISD::ADD, MVT::i64, Q, N.getOperand(0));
643 // If d < 0 and m > 0, subtract the numerator.
644 if (d < 0 && magics.m > 0)
645 Q = ISelDAG->getNode(ISD::SUB, MVT::i64, Q, N.getOperand(0));
646 // Shift right algebraic if shift value is nonzero
647 if (magics.s > 0)
Misha Brukman4633f1c2005-04-21 23:13:11 +0000648 Q = ISelDAG->getNode(ISD::SRA, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000649 ISelDAG->getConstant(magics.s, MVT::i64));
650 // Extract the sign bit and add it to the quotient
Misha Brukman4633f1c2005-04-21 23:13:11 +0000651 SDOperand T =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000652 ISelDAG->getNode(ISD::SRL, MVT::i64, Q, ISelDAG->getConstant(63, MVT::i64));
653 return ISelDAG->getNode(ISD::ADD, MVT::i64, Q, T);
654}
655
656/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
657/// return a DAG expression to select that will generate the same value by
658/// multiplying by a magic number. See:
659/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
660SDOperand ISel::BuildUDIVSequence(SDOperand N) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000661 unsigned d =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000662 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
663 mu magics = magicu(d);
664 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000665 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000666 ISelDAG->getConstant(magics.m, MVT::i64));
667 if (magics.a == 0) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000668 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000669 ISelDAG->getConstant(magics.s, MVT::i64));
670 } else {
671 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i64, N.getOperand(0), Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000672 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000673 ISelDAG->getConstant(1, MVT::i64));
674 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i64, NPQ, Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000675 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000676 ISelDAG->getConstant(magics.s-1, MVT::i64));
677 }
678 return Q;
679}
680
Andrew Lenhartha565c272005-04-06 22:03:13 +0000681//From PPC32
682/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
683/// returns zero when the input is not exactly a power of two.
684static unsigned ExactLog2(uint64_t Val) {
685 if (Val == 0 || (Val & (Val-1))) return 0;
686 unsigned Count = 0;
687 while (Val != 1) {
688 Val >>= 1;
689 ++Count;
690 }
691 return Count;
692}
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000693
694
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000695//These describe LDAx
Andrew Lenharthc0513832005-03-29 19:24:04 +0000696static const int IMM_LOW = -32768;
697static const int IMM_HIGH = 32767;
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000698static const int IMM_MULT = 65536;
699
700static long getUpper16(long l)
701{
702 long y = l / IMM_MULT;
703 if (l % IMM_MULT > IMM_HIGH)
704 ++y;
705 return y;
706}
707
708static long getLower16(long l)
709{
710 long h = getUpper16(l);
711 return l - h * IMM_MULT;
712}
713
Andrew Lenharth65838902005-02-06 16:22:15 +0000714static unsigned GetSymVersion(unsigned opcode)
715{
716 switch (opcode) {
717 default: assert(0 && "unknown load or store"); return 0;
718 case Alpha::LDQ: return Alpha::LDQ_SYM;
719 case Alpha::LDS: return Alpha::LDS_SYM;
720 case Alpha::LDT: return Alpha::LDT_SYM;
721 case Alpha::LDL: return Alpha::LDL_SYM;
722 case Alpha::LDBU: return Alpha::LDBU_SYM;
723 case Alpha::LDWU: return Alpha::LDWU_SYM;
724 case Alpha::LDW: return Alpha::LDW_SYM;
725 case Alpha::LDB: return Alpha::LDB_SYM;
726 case Alpha::STQ: return Alpha::STQ_SYM;
727 case Alpha::STS: return Alpha::STS_SYM;
728 case Alpha::STT: return Alpha::STT_SYM;
729 case Alpha::STL: return Alpha::STL_SYM;
730 case Alpha::STW: return Alpha::STW_SYM;
731 case Alpha::STB: return Alpha::STB_SYM;
732 }
733}
734
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000735void ISel::MoveFP2Int(unsigned src, unsigned dst, bool isDouble)
736{
737 unsigned Opc;
738 if (EnableAlphaFTOI) {
739 Opc = isDouble ? Alpha::FTOIT : Alpha::FTOIS;
740 BuildMI(BB, Opc, 1, dst).addReg(src);
741 } else {
742 //The hard way:
743 // Spill the integer to memory and reload it from there.
744 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
745 MachineFunction *F = BB->getParent();
746 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
747
748 Opc = isDouble ? Alpha::STT : Alpha::STS;
749 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
750 Opc = isDouble ? Alpha::LDQ : Alpha::LDL;
751 BuildMI(BB, Alpha::LDQ, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
752 }
753}
754
755void ISel::MoveInt2FP(unsigned src, unsigned dst, bool isDouble)
756{
757 unsigned Opc;
758 if (EnableAlphaFTOI) {
759 Opc = isDouble?Alpha::ITOFT:Alpha::ITOFS;
760 BuildMI(BB, Opc, 1, dst).addReg(src);
761 } else {
762 //The hard way:
763 // Spill the integer to memory and reload it from there.
764 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
765 MachineFunction *F = BB->getParent();
766 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
767
768 Opc = isDouble ? Alpha::STQ : Alpha::STL;
769 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
770 Opc = isDouble ? Alpha::LDT : Alpha::LDS;
771 BuildMI(BB, Opc, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
772 }
773}
774
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000775bool ISel::SelectFPSetCC(SDOperand N, unsigned dst)
776{
777 SDNode *Node = N.Val;
778 unsigned Opc, Tmp1, Tmp2, Tmp3;
779 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node);
780
781 //assert(SetCC->getOperand(0).getValueType() != MVT::f32 && "SetCC f32 should have been promoted");
782 bool rev = false;
783 bool inv = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000784
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000785 switch (SetCC->getCondition()) {
786 default: Node->dump(); assert(0 && "Unknown FP comparison!");
787 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
788 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
789 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
790 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
791 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
792 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
793 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000794
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000795 //FIXME: check for constant 0.0
796 ConstantFPSDNode *CN;
797 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
798 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
799 Tmp1 = Alpha::F31;
800 else
801 Tmp1 = SelectExpr(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000802
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000803 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
804 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
805 Tmp2 = Alpha::F31;
806 else
Chris Lattner9c9183a2005-04-30 04:44:07 +0000807 Tmp2 = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000808
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000809 //Can only compare doubles, and dag won't promote for me
810 if (SetCC->getOperand(0).getValueType() == MVT::f32)
811 {
812 //assert(0 && "Setcc On float?\n");
813 std::cerr << "Setcc on float!\n";
814 Tmp3 = MakeReg(MVT::f64);
815 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
816 Tmp1 = Tmp3;
817 }
818 if (SetCC->getOperand(1).getValueType() == MVT::f32)
819 {
820 //assert (0 && "Setcc On float?\n");
821 std::cerr << "Setcc on float!\n";
822 Tmp3 = MakeReg(MVT::f64);
823 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp2);
824 Tmp2 = Tmp3;
825 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000826
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000827 if (rev) std::swap(Tmp1, Tmp2);
828 //do the comparison
829 BuildMI(BB, Opc, 2, dst).addReg(Tmp1).addReg(Tmp2);
830 return inv;
831}
832
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000833//Check to see if the load is a constant offset from a base register
834void ISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
835{
836 unsigned opcode = N.getOpcode();
837 if (opcode == ISD::ADD) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000838 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000839 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
840 { //Normal imm add
841 Reg = SelectExpr(N.getOperand(0));
842 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
843 return;
844 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000845 else if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000846 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 32767)
847 {
848 Reg = SelectExpr(N.getOperand(1));
849 offset = cast<ConstantSDNode>(N.getOperand(0))->getValue();
850 return;
851 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000852 }
853 Reg = SelectExpr(N);
854 offset = 0;
855 return;
856}
857
Andrew Lenharth445171a2005-02-08 00:40:03 +0000858void ISel::SelectBranchCC(SDOperand N)
859{
860 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000861 MachineBasicBlock *Dest =
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000862 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
863 unsigned Opc = Alpha::WTF;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000864
Andrew Lenharth445171a2005-02-08 00:40:03 +0000865 Select(N.getOperand(0)); //chain
866 SDOperand CC = N.getOperand(1);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000867
Andrew Lenharth445171a2005-02-08 00:40:03 +0000868 if (CC.getOpcode() == ISD::SETCC)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000869 {
870 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
871 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
872 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000873 bool LeftZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
874 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
875 bool RightZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
876 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000877 bool isNE = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000878
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000879 //Fix up CC
880 ISD::CondCode cCode= SetCC->getCondition();
881 if (LeftZero && !RightZero) //Swap Operands
882 cCode = ISD::getSetCCSwappedOperands(cCode);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000883
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000884 if(cCode == ISD::SETNE)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000885 isNE = true;
Andrew Lenharth445171a2005-02-08 00:40:03 +0000886
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000887 if (LeftZero || RightZero) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000888 switch (SetCC->getCondition()) {
889 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
890 case ISD::SETEQ: Opc = Alpha::BEQ; break;
891 case ISD::SETLT: Opc = Alpha::BLT; break;
892 case ISD::SETLE: Opc = Alpha::BLE; break;
893 case ISD::SETGT: Opc = Alpha::BGT; break;
894 case ISD::SETGE: Opc = Alpha::BGE; break;
895 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
896 case ISD::SETUGT: Opc = Alpha::BNE; break;
897 case ISD::SETULE: Opc = Alpha::BEQ; break; //Technically you could have this CC
898 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
899 case ISD::SETNE: Opc = Alpha::BNE; break;
900 }
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000901 unsigned Tmp1;
902 if(LeftZero && !RightZero) //swap Operands
903 Tmp1 = SelectExpr(SetCC->getOperand(1)); //Cond
904 else
905 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000906 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
907 return;
908 } else {
909 unsigned Tmp1 = SelectExpr(CC);
910 if (isNE)
911 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
912 else
913 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000914 return;
915 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000916 } else { //FP
917 //Any comparison between 2 values should be codegened as an folded branch, as moving
918 //CC to the integer register is very expensive
919 //for a cmp b: c = a - b;
920 //a = b: c = 0
921 //a < b: c < 0
922 //a > b: c > 0
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000923
924 bool invTest = false;
925 unsigned Tmp3;
926
927 ConstantFPSDNode *CN;
928 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
929 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
930 Tmp3 = SelectExpr(SetCC->getOperand(0));
931 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
932 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
933 {
934 Tmp3 = SelectExpr(SetCC->getOperand(1));
935 invTest = true;
936 }
937 else
938 {
939 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
940 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
941 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
942 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
943 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
944 .addReg(Tmp1).addReg(Tmp2);
945 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000946
947 switch (SetCC->getCondition()) {
948 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000949 case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
950 case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
951 case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
952 case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
953 case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
954 case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000955 }
956 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000957 return;
958 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000959 abort(); //Should never be reached
960 } else {
961 //Giveup and do the stupid thing
962 unsigned Tmp1 = SelectExpr(CC);
963 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
964 return;
965 }
Andrew Lenharth445171a2005-02-08 00:40:03 +0000966 abort(); //Should never be reached
967}
968
Andrew Lenharth40831c52005-01-28 06:57:18 +0000969unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
970{
971 unsigned Tmp1, Tmp2, Tmp3;
972 unsigned Opc = 0;
973 SDNode *Node = N.Val;
974 MVT::ValueType DestType = N.getValueType();
975 unsigned opcode = N.getOpcode();
976
977 switch (opcode) {
978 default:
979 Node->dump();
980 assert(0 && "Node not handled!\n");
Andrew Lenharth2c594352005-01-29 15:42:07 +0000981
Andrew Lenharth7332f3e2005-04-02 19:11:07 +0000982 case ISD::UNDEF: {
983 BuildMI(BB, Alpha::IDEF, 0, Result);
984 return Result;
985 }
986
Andrew Lenharth30b46d42005-04-02 19:04:58 +0000987 case ISD::FNEG:
988 if(ISD::FABS == N.getOperand(0).getOpcode())
989 {
Misha Brukman7847fca2005-04-22 17:54:37 +0000990 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
991 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +0000992 } else {
Misha Brukman7847fca2005-04-22 17:54:37 +0000993 Tmp1 = SelectExpr(N.getOperand(0));
994 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +0000995 }
996 return Result;
997
998 case ISD::FABS:
999 Tmp1 = SelectExpr(N.getOperand(0));
1000 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
1001 return Result;
1002
Andrew Lenharth9818c052005-02-05 13:19:12 +00001003 case ISD::SELECT:
1004 {
Andrew Lenharth45859692005-03-03 21:47:53 +00001005 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1006 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
1007 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
1008
1009 SDOperand CC = N.getOperand(0);
1010 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
1011
Misha Brukman4633f1c2005-04-21 23:13:11 +00001012 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth45859692005-03-03 21:47:53 +00001013 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
1014 { //FP Setcc -> Select yay!
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001015
1016
Andrew Lenharth45859692005-03-03 21:47:53 +00001017 //for a cmp b: c = a - b;
1018 //a = b: c = 0
1019 //a < b: c < 0
1020 //a > b: c > 0
Misha Brukman4633f1c2005-04-21 23:13:11 +00001021
Andrew Lenharth45859692005-03-03 21:47:53 +00001022 bool invTest = false;
1023 unsigned Tmp3;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001024
Andrew Lenharth45859692005-03-03 21:47:53 +00001025 ConstantFPSDNode *CN;
1026 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
1027 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1028 Tmp3 = SelectExpr(SetCC->getOperand(0));
1029 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
1030 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1031 {
1032 Tmp3 = SelectExpr(SetCC->getOperand(1));
1033 invTest = true;
1034 }
1035 else
1036 {
1037 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
1038 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
1039 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
1040 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1041 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1042 .addReg(Tmp1).addReg(Tmp2);
1043 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001044
Andrew Lenharth45859692005-03-03 21:47:53 +00001045 switch (SetCC->getCondition()) {
1046 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
1047 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNE : Alpha::FCMOVEQ; break;
1048 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGT : Alpha::FCMOVLT; break;
1049 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGE : Alpha::FCMOVLE; break;
1050 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLT : Alpha::FCMOVGT; break;
1051 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLE : Alpha::FCMOVGE; break;
1052 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQ : Alpha::FCMOVNE; break;
1053 }
Andrew Lenharth33819132005-03-04 20:09:23 +00001054 BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
Andrew Lenharth45859692005-03-03 21:47:53 +00001055 return Result;
1056 }
1057 else
1058 {
1059 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001060 BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV).addReg(Tmp1);
1061// // Spill the cond to memory and reload it from there.
1062// unsigned Tmp4 = MakeReg(MVT::f64);
1063// MoveIntFP(Tmp1, Tmp4, true);
1064// //now ideally, we don't have to do anything to the flag...
1065// // Get the condition into the zero flag.
1066// BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
Andrew Lenharth45859692005-03-03 21:47:53 +00001067 return Result;
1068 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001069 }
1070
Andrew Lenharthc1faced2005-02-01 01:37:24 +00001071 case ISD::FP_ROUND:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001072 assert (DestType == MVT::f32 &&
1073 N.getOperand(0).getValueType() == MVT::f64 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001074 "only f64 to f32 conversion supported here");
Andrew Lenharthc1faced2005-02-01 01:37:24 +00001075 Tmp1 = SelectExpr(N.getOperand(0));
1076 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
1077 return Result;
1078
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001079 case ISD::FP_EXTEND:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001080 assert (DestType == MVT::f64 &&
1081 N.getOperand(0).getValueType() == MVT::f32 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001082 "only f32 to f64 conversion supported here");
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001083 Tmp1 = SelectExpr(N.getOperand(0));
1084 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
1085 return Result;
1086
Andrew Lenharth2c594352005-01-29 15:42:07 +00001087 case ISD::CopyFromReg:
1088 {
1089 // Make sure we generate both values.
1090 if (Result != notIn)
1091 ExprMap[N.getValue(1)] = notIn; // Generate the token
1092 else
1093 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001094
Andrew Lenharth2c594352005-01-29 15:42:07 +00001095 SDOperand Chain = N.getOperand(0);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001096
Andrew Lenharth2c594352005-01-29 15:42:07 +00001097 Select(Chain);
1098 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1099 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1100 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
1101 return Result;
1102 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001103
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001104 case ISD::LOAD:
1105 {
1106 // Make sure we generate both values.
1107 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001108 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001109 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001110 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001111
Andrew Lenharth29219162005-02-07 06:31:44 +00001112 DestType = N.getValue(0).getValueType();
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001113
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001114 SDOperand Chain = N.getOperand(0);
1115 SDOperand Address = N.getOperand(1);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001116 Select(Chain);
Andrew Lenharth65838902005-02-06 16:22:15 +00001117 Opc = DestType == MVT::f64 ? Alpha::LDT : Alpha::LDS;
1118
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001119 if (Address.getOpcode() == ISD::GlobalAddress) {
1120 AlphaLowering.restoreGP(BB);
1121 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001122 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001123 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1124 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001125 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001126 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001127 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001128 has_sym = true;
Andrew Lenharth97127a12005-02-05 17:41:39 +00001129 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001130 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001131 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001132 BuildMI(BB, Opc, 2, Result)
1133 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1134 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001135 } else {
1136 long offset;
1137 SelectAddr(Address, Tmp1, offset);
1138 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1139 }
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001140 return Result;
1141 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001142 case ISD::ConstantFP:
1143 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
1144 if (CN->isExactlyValue(+0.0)) {
1145 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001146 } else if ( CN->isExactlyValue(-0.0)) {
1147 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001148 } else {
1149 abort();
1150 }
1151 }
1152 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001153
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001154 case ISD::SDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001155 case ISD::MUL:
1156 case ISD::ADD:
1157 case ISD::SUB:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001158 switch( opcode ) {
1159 case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS; break;
1160 case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS; break;
1161 case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS; break;
1162 case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS; break;
1163 };
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001164
1165 ConstantFPSDNode *CN;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001166 if (opcode == ISD::SUB
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001167 && (CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
1168 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1169 {
1170 Tmp2 = SelectExpr(N.getOperand(1));
1171 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp2).addReg(Tmp2);
1172 } else {
1173 Tmp1 = SelectExpr(N.getOperand(0));
1174 Tmp2 = SelectExpr(N.getOperand(1));
1175 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1176 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001177 return Result;
1178
Andrew Lenharth2c594352005-01-29 15:42:07 +00001179 case ISD::EXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001180 {
1181 //include a conversion sequence for float loads to double
1182 if (Result != notIn)
1183 ExprMap[N.getValue(1)] = notIn; // Generate the token
1184 else
1185 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001186
Andrew Lenhartha549deb2005-02-07 05:33:15 +00001187 Tmp1 = MakeReg(MVT::f32);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001188
1189 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001190 "EXTLOAD not from f32");
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001191 assert(Node->getValueType(0) == MVT::f64 && "EXTLOAD not to f64");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001192
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001193 SDOperand Chain = N.getOperand(0);
1194 SDOperand Address = N.getOperand(1);
1195 Select(Chain);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001196
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001197 if (Address.getOpcode() == ISD::GlobalAddress) {
1198 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001199 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001200 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1201 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001202 else if (ConstantPoolSDNode *CP =
1203 dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001204 {
1205 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001206 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001207 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addConstantPoolIndex(CP->getIndex());
1208 }
1209 else if(Address.getOpcode() == ISD::FrameIndex) {
1210 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
Andrew Lenharth032f2352005-02-22 21:59:48 +00001211 BuildMI(BB, Alpha::LDS, 2, Tmp1)
1212 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1213 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001214 } else {
1215 long offset;
1216 SelectAddr(Address, Tmp2, offset);
1217 BuildMI(BB, Alpha::LDS, 1, Tmp1).addImm(offset).addReg(Tmp2);
1218 }
Andrew Lenharth29219162005-02-07 06:31:44 +00001219 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001220 return Result;
1221 }
Andrew Lenharth2c594352005-01-29 15:42:07 +00001222
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001223 case ISD::UINT_TO_FP:
Andrew Lenharth69520ed2005-05-26 18:18:34 +00001224 {
1225 //FIXME: First test if we will have problems with the sign bit before doing the slow thing
1226 assert (N.getOperand(0).getValueType() == MVT::i64
1227 && "only quads can be loaded from");
1228 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1229 Tmp2 = MakeReg(MVT::i64);
1230 BuildMI(BB, Alpha::SRL, 2, Tmp2).addReg(Tmp1).addImm(1);
1231 Tmp3 = MakeReg(MVT::i64);
1232 BuildMI(BB, Alpha::CMPLT, 2, Tmp3).addReg(Tmp1).addReg(Alpha::R31);
1233 unsigned Tmp4 = MakeReg(MVT::f64), Tmp5 = MakeReg(MVT::f64), Tmp6 = MakeReg(MVT::f64);
1234 MoveInt2FP(Tmp1, Tmp4, true);
1235 MoveInt2FP(Tmp2, Tmp5, true);
1236 MoveInt2FP(Tmp3, Tmp6, true);
1237 Tmp1 = MakeReg(MVT::f64);
1238 Tmp2 = MakeReg(MVT::f64);
1239 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
1240 BuildMI(BB, Opc, 1, Tmp1).addReg(Tmp4);
1241 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp5);
1242 Tmp3 = MakeReg(MVT::f64);
1243 BuildMI(BB, Alpha::ADDT, 2, Tmp3).addReg(Tmp2).addReg(Tmp2);
1244 //Ok, now tmp1 had the plain covereted
1245 //tmp3 has the reduced converted and added
1246 //tmp6 has the conditional to use
1247 BuildMI(BB, Alpha::FCMOVNE, 3, Result).addReg(Tmp1).addReg(Tmp3).addReg(Tmp6);
1248 return Result;
1249 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001250 case ISD::SINT_TO_FP:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001251 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001252 assert (N.getOperand(0).getValueType() == MVT::i64
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001253 && "only quads can be loaded from");
Andrew Lenharth40831c52005-01-28 06:57:18 +00001254 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001255 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001256 MoveInt2FP(Tmp1, Tmp2, true);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001257 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
1258 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001259 return Result;
1260 }
1261 }
1262 assert(0 && "should not get here");
1263 return 0;
1264}
1265
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001266unsigned ISel::SelectExpr(SDOperand N) {
1267 unsigned Result;
Andrew Lenharth2966e842005-04-07 18:15:28 +00001268 unsigned Tmp1, Tmp2 = 0, Tmp3;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001269 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001270 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001271
1272 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001273 MVT::ValueType DestType = N.getValueType();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001274
1275 unsigned &Reg = ExprMap[N];
1276 if (Reg) return Reg;
1277
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001278 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::TAILCALL)
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001279 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001280 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001281 else {
1282 // If this is a call instruction, make sure to prepare ALL of the result
1283 // values as well as the chain.
1284 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001285 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001286 else {
1287 Result = MakeReg(Node->getValueType(0));
1288 ExprMap[N.getValue(0)] = Result;
1289 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1290 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001291 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001292 }
1293 }
1294
Andrew Lenharth50d91d72005-04-30 14:19:13 +00001295 if ((DestType == MVT::f64 || DestType == MVT::f32 ||
1296 (
1297 (opcode == ISD::LOAD || opcode == ISD::CopyFromReg ||
1298 opcode == ISD::EXTLOAD) &&
1299 (N.getValue(0).getValueType() == MVT::f32 ||
1300 N.getValue(0).getValueType() == MVT::f64)
1301 ))
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001302 && opcode != ISD::CALL && opcode != ISD::TAILCALL
Andrew Lenharth06342c32005-02-07 06:21:37 +00001303 )
Andrew Lenharth40831c52005-01-28 06:57:18 +00001304 return SelectExprFP(N, Result);
1305
1306 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001307 default:
1308 Node->dump();
1309 assert(0 && "Node not handled!\n");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001310
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001311 case ISD::CTPOP:
1312 case ISD::CTTZ:
1313 case ISD::CTLZ:
1314 Opc = opcode == ISD::CTPOP ? Alpha::CTPOP :
1315 (opcode == ISD::CTTZ ? Alpha::CTTZ : Alpha::CTLZ);
1316 Tmp1 = SelectExpr(N.getOperand(0));
1317 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1318 return Result;
1319
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001320 case ISD::MULHU:
1321 Tmp1 = SelectExpr(N.getOperand(0));
1322 Tmp2 = SelectExpr(N.getOperand(1));
1323 BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth706be912005-04-07 13:55:53 +00001324 return Result;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001325 case ISD::MULHS:
1326 {
1327 //MULHU - Ra<63>*Rb - Rb<63>*Ra
1328 Tmp1 = SelectExpr(N.getOperand(0));
1329 Tmp2 = SelectExpr(N.getOperand(1));
1330 Tmp3 = MakeReg(MVT::i64);
1331 BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1332 unsigned V1 = MakeReg(MVT::i64);
1333 unsigned V2 = MakeReg(MVT::i64);
1334 BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31).addReg(Tmp1);
1335 BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31).addReg(Tmp2);
1336 unsigned IRes = MakeReg(MVT::i64);
1337 BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
1338 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
1339 return Result;
1340 }
Andrew Lenharth7332f3e2005-04-02 19:11:07 +00001341 case ISD::UNDEF: {
1342 BuildMI(BB, Alpha::IDEF, 0, Result);
1343 return Result;
1344 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001345
Andrew Lenharth032f2352005-02-22 21:59:48 +00001346 case ISD::DYNAMIC_STACKALLOC:
1347 // Generate both result values.
Andrew Lenharth3a7118d2005-02-23 17:33:42 +00001348 if (Result != notIn)
1349 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth032f2352005-02-22 21:59:48 +00001350 else
1351 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1352
1353 // FIXME: We are currently ignoring the requested alignment for handling
1354 // greater than the stack alignment. This will need to be revisited at some
1355 // point. Align = N.getOperand(2);
1356
1357 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1358 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1359 std::cerr << "Cannot allocate stack object with greater alignment than"
1360 << " the stack alignment yet!";
1361 abort();
1362 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001363
Andrew Lenharth032f2352005-02-22 21:59:48 +00001364 Select(N.getOperand(0));
1365 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1366 {
1367 if (CN->getValue() < 32000)
1368 {
1369 BuildMI(BB, Alpha::LDA, 2, Alpha::R30)
1370 .addImm(-CN->getValue()).addReg(Alpha::R30);
1371 } else {
1372 Tmp1 = SelectExpr(N.getOperand(1));
1373 // Subtract size from stack pointer, thereby allocating some space.
1374 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1375 }
1376 } else {
1377 Tmp1 = SelectExpr(N.getOperand(1));
1378 // Subtract size from stack pointer, thereby allocating some space.
1379 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1380 }
1381
1382 // Put a pointer to the space into the result register, by copying the stack
1383 // pointer.
Andrew Lenharth7bc47022005-02-22 23:29:25 +00001384 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
Andrew Lenharth032f2352005-02-22 21:59:48 +00001385 return Result;
1386
Andrew Lenharth33819132005-03-04 20:09:23 +00001387// case ISD::ConstantPool:
1388// Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1389// AlphaLowering.restoreGP(BB);
1390// BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(Tmp1);
1391// return Result;
Andrew Lenharth2c594352005-01-29 15:42:07 +00001392
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001393 case ISD::FrameIndex:
Andrew Lenharth032f2352005-02-22 21:59:48 +00001394 BuildMI(BB, Alpha::LDA, 2, Result)
1395 .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
1396 .addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001397 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001398
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001399 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001400 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001401 case ISD::SEXTLOAD:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001402 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001403 {
1404 // Make sure we generate both values.
1405 if (Result != notIn)
1406 ExprMap[N.getValue(1)] = notIn; // Generate the token
1407 else
1408 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001409
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001410 SDOperand Chain = N.getOperand(0);
1411 SDOperand Address = N.getOperand(1);
1412 Select(Chain);
1413
Misha Brukman4633f1c2005-04-21 23:13:11 +00001414 assert(Node->getValueType(0) == MVT::i64 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001415 "Unknown type to sign extend to.");
Andrew Lenharth03824012005-02-07 05:55:55 +00001416 if (opcode == ISD::LOAD)
1417 Opc = Alpha::LDQ;
1418 else
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001419 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
1420 default: Node->dump(); assert(0 && "Bad sign extend!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001421 case MVT::i32: Opc = Alpha::LDL;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001422 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001423 case MVT::i16: Opc = Alpha::LDWU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001424 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001425 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Misha Brukman4633f1c2005-04-21 23:13:11 +00001426 case MVT::i8: Opc = Alpha::LDBU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001427 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001428 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001429
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001430 if (Address.getOpcode() == ISD::GlobalAddress) {
1431 AlphaLowering.restoreGP(BB);
1432 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001433 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001434 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1435 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001436 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1437 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001438 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001439 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001440 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001441 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001442 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001443 BuildMI(BB, Opc, 2, Result)
1444 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1445 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001446 } else {
1447 long offset;
1448 SelectAddr(Address, Tmp1, offset);
1449 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1450 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001451 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001452 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001453
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001454 case ISD::GlobalAddress:
1455 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001456 has_sym = true;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001457 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
1458 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
1459 return Result;
1460
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001461 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001462 case ISD::CALL:
1463 {
1464 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001465
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001466 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001467 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001468
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001469 //grab the arguments
1470 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001471 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001472 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001473 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001474
Andrew Lenharth684f2292005-01-30 00:35:27 +00001475 //in reg args
1476 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001477 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001478 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001479 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +00001480 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001481 Alpha::F19, Alpha::F20, Alpha::F21};
1482 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001483 default:
1484 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001485 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001486 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001487 N.getOperand(i+2).getValueType() << "\n";
1488 assert(0 && "Unknown value type for call");
1489 case MVT::i1:
1490 case MVT::i8:
1491 case MVT::i16:
1492 case MVT::i32:
1493 case MVT::i64:
1494 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1495 break;
1496 case MVT::f32:
1497 case MVT::f64:
1498 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1499 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001500 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001501 }
Andrew Lenharth684f2292005-01-30 00:35:27 +00001502 //in mem args
1503 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001504 {
1505 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001506 default:
1507 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001508 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001509 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001510 N.getOperand(i+2).getValueType() << "\n";
1511 assert(0 && "Unknown value type for call");
1512 case MVT::i1:
1513 case MVT::i8:
1514 case MVT::i16:
1515 case MVT::i32:
1516 case MVT::i64:
1517 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1518 break;
1519 case MVT::f32:
1520 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1521 break;
1522 case MVT::f64:
1523 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1524 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001525 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001526 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001527 //build the right kind of call
1528 if (GlobalAddressSDNode *GASD =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001529 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001530 {
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001531 if (GASD->getGlobal()->isExternal()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001532 //use safe calling convention
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001533 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001534 has_sym = true;
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001535 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal());
1536 } else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001537 //use PC relative branch call
Andrew Lenharth1e0d9bd2005-04-14 17:34:20 +00001538 AlphaLowering.restoreGP(BB);
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001539 BuildMI(BB, Alpha::BSR, 1, Alpha::R26).addGlobalAddress(GASD->getGlobal(),true);
1540 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001541 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001542 else if (ExternalSymbolSDNode *ESSDN =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001543 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001544 {
1545 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001546 has_sym = true;
Andrew Lenharthba05ad62005-03-30 18:22:52 +00001547 BuildMI(BB, Alpha::CALL, 1).addExternalSymbol(ESSDN->getSymbol(), true);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001548 } else {
1549 //no need to restore GP as we are doing an indirect call
1550 Tmp1 = SelectExpr(N.getOperand(1));
1551 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
1552 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
1553 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001554
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001555 //push the result into a virtual register
Misha Brukman4633f1c2005-04-21 23:13:11 +00001556
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001557 switch (Node->getValueType(0)) {
1558 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001559 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001560 case MVT::i1:
1561 case MVT::i8:
1562 case MVT::i16:
1563 case MVT::i32:
1564 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001565 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
1566 break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001567 case MVT::f32:
1568 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001569 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
1570 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001571 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001572 return Result+N.ResNo;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001573 }
1574
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001575 case ISD::SIGN_EXTEND_INREG:
1576 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001577 //do SDIV opt for all levels of ints if not dividing by a constant
1578 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
1579 && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001580 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001581 unsigned Tmp4 = MakeReg(MVT::f64);
1582 unsigned Tmp5 = MakeReg(MVT::f64);
1583 unsigned Tmp6 = MakeReg(MVT::f64);
1584 unsigned Tmp7 = MakeReg(MVT::f64);
1585 unsigned Tmp8 = MakeReg(MVT::f64);
1586 unsigned Tmp9 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001587
1588 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1589 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1590 MoveInt2FP(Tmp1, Tmp4, true);
1591 MoveInt2FP(Tmp2, Tmp5, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001592 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Tmp4);
1593 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Tmp5);
1594 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
1595 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Tmp8);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001596 MoveFP2Int(Tmp9, Result, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001597 return Result;
1598 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001599
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001600 //Alpha has instructions for a bunch of signed 32 bit stuff
1601 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001602 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001603 switch (N.getOperand(0).getOpcode()) {
1604 case ISD::ADD:
1605 case ISD::SUB:
1606 case ISD::MUL:
1607 {
1608 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
1609 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
1610 //FIXME: first check for Scaled Adds and Subs!
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001611 ConstantSDNode* CSD = NULL;
1612 if(!isMul && N.getOperand(0).getOperand(0).getOpcode() == ISD::SHL &&
1613 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(0).getOperand(1))) &&
1614 (CSD->getValue() == 2 || CSD->getValue() == 3))
1615 {
1616 bool use4 = CSD->getValue() == 2;
1617 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1618 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1619 BuildMI(BB, isAdd?(use4?Alpha::S4ADDL:Alpha::S8ADDL):(use4?Alpha::S4SUBL:Alpha::S8SUBL),
1620 2,Result).addReg(Tmp1).addReg(Tmp2);
1621 }
1622 else if(isAdd && N.getOperand(0).getOperand(1).getOpcode() == ISD::SHL &&
1623 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1).getOperand(1))) &&
1624 (CSD->getValue() == 2 || CSD->getValue() == 3))
1625 {
1626 bool use4 = CSD->getValue() == 2;
1627 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1628 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1629 BuildMI(BB, use4?Alpha::S4ADDL:Alpha::S8ADDL, 2,Result).addReg(Tmp1).addReg(Tmp2);
1630 }
1631 else if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001632 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
1633 { //Normal imm add/sub
1634 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001635 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001636 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
1637 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001638 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001639 else
1640 { //Normal add/sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001641 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULL : Alpha::SUBL);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001642 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001643 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001644 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1645 }
1646 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001647 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001648 default: break; //Fall Though;
1649 }
1650 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001651 Tmp1 = SelectExpr(N.getOperand(0));
1652 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001653 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001654 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001655 {
1656 default:
1657 Node->dump();
1658 assert(0 && "Sign Extend InReg not there yet");
1659 break;
1660 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001661 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001662 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001663 break;
1664 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001665 case MVT::i16:
1666 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
1667 break;
1668 case MVT::i8:
1669 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
1670 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +00001671 case MVT::i1:
1672 Tmp2 = MakeReg(MVT::i64);
1673 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001674 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +00001675 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001676 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001677 return Result;
1678 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001679
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001680 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001681 {
1682 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1683 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1684 bool isConst1 = false;
1685 bool isConst2 = false;
1686 int dir;
Misha Brukman7847fca2005-04-22 17:54:37 +00001687
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001688 //Tmp1 = SelectExpr(N.getOperand(0));
1689 if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001690 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
1691 isConst1 = true;
1692 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001693 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1694 isConst2 = true;
1695
1696 switch (SetCC->getCondition()) {
1697 default: Node->dump(); assert(0 && "Unknown integer comparison!");
1698 case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001699 case ISD::SETLT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001700 Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001701 case ISD::SETLE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001702 Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001703 case ISD::SETGT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001704 Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001705 case ISD::SETGE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001706 Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001707 case ISD::SETULT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001708 Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001709 case ISD::SETUGT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001710 Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001711 case ISD::SETULE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001712 Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001713 case ISD::SETUGE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001714 Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001715 case ISD::SETNE: {//Handle this one special
1716 //std::cerr << "Alpha does not have a setne.\n";
1717 //abort();
1718 Tmp1 = SelectExpr(N.getOperand(0));
1719 Tmp2 = SelectExpr(N.getOperand(1));
1720 Tmp3 = MakeReg(MVT::i64);
1721 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001722 //Remeber we have the Inv for this CC
1723 CCInvMap[N] = Tmp3;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001724 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001725 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001726 return Result;
1727 }
1728 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001729 if (dir == 1) {
1730 Tmp1 = SelectExpr(N.getOperand(0));
1731 if (isConst2) {
1732 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1733 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1734 } else {
1735 Tmp2 = SelectExpr(N.getOperand(1));
1736 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1737 }
1738 } else if (dir == 2) {
1739 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001740 if (isConst1) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001741 Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1742 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1743 } else {
1744 Tmp2 = SelectExpr(N.getOperand(0));
1745 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1746 }
1747 } else { //dir == 0
1748 if (isConst1) {
1749 Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1750 Tmp2 = SelectExpr(N.getOperand(1));
1751 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
1752 } else if (isConst2) {
1753 Tmp1 = SelectExpr(N.getOperand(0));
1754 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1755 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
1756 } else {
1757 Tmp1 = SelectExpr(N.getOperand(0));
1758 Tmp2 = SelectExpr(N.getOperand(1));
1759 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1760 }
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001761 }
1762 } else {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001763 //do the comparison
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001764 Tmp1 = MakeReg(MVT::f64);
1765 bool inv = SelectFPSetCC(N, Tmp1);
1766
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001767 //now arrange for Result (int) to have a 1 or 0
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001768 Tmp2 = MakeReg(MVT::i64);
1769 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001770 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001771 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001772 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001773 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001774 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001775 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001776
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001777 case ISD::CopyFromReg:
1778 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001779 ++count_ins;
1780
Andrew Lenharth40831c52005-01-28 06:57:18 +00001781 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001782 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001783 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001784 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001785 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001786
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001787 SDOperand Chain = N.getOperand(0);
1788
1789 Select(Chain);
1790 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1791 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1792 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1793 return Result;
1794 }
1795
Misha Brukman4633f1c2005-04-21 23:13:11 +00001796 //Most of the plain arithmetic and logic share the same form, and the same
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001797 //constant immediate test
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001798 case ISD::XOR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001799 //Match Not
1800 if (N.getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001801 cast<ConstantSDNode>(N.getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001802 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001803 Tmp1 = SelectExpr(N.getOperand(0));
1804 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1805 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001806 }
1807 //Fall through
1808 case ISD::AND:
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001809 //handle zap
1810 if (opcode == ISD::AND && N.getOperand(1).getOpcode() == ISD::Constant)
1811 {
1812 uint64_t k = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1813 unsigned int build = 0;
1814 for(int i = 0; i < 8; ++i)
1815 {
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001816 if ((k & 0x00FF) == 0x00FF)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001817 build |= 1 << i;
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001818 else if ((k & 0x00FF) != 0)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001819 { build = 0; break; }
1820 k >>= 8;
1821 }
1822 if (build)
1823 {
1824 Tmp1 = SelectExpr(N.getOperand(0));
1825 BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1826 return Result;
1827 }
1828 }
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001829 case ISD::OR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001830 //Check operand(0) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001831 if (N.getOperand(0).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001832 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001833 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001834 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001835 switch(opcode) {
1836 case ISD::AND: Opc = Alpha::BIC; break;
1837 case ISD::OR: Opc = Alpha::ORNOT; break;
1838 case ISD::XOR: Opc = Alpha::EQV; break;
1839 }
1840 Tmp1 = SelectExpr(N.getOperand(1));
1841 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1842 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1843 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001844 }
1845 //Check operand(1) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001846 if (N.getOperand(1).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001847 N.getOperand(1).getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001848 cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001849 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001850 switch(opcode) {
1851 case ISD::AND: Opc = Alpha::BIC; break;
1852 case ISD::OR: Opc = Alpha::ORNOT; break;
1853 case ISD::XOR: Opc = Alpha::EQV; break;
1854 }
1855 Tmp1 = SelectExpr(N.getOperand(0));
1856 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1857 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1858 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001859 }
1860 //Fall through
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001861 case ISD::SHL:
1862 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001863 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001864 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001865 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1866 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001867 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001868 {
1869 switch(opcode) {
1870 case ISD::AND: Opc = Alpha::ANDi; break;
1871 case ISD::OR: Opc = Alpha::BISi; break;
1872 case ISD::XOR: Opc = Alpha::XORi; break;
1873 case ISD::SHL: Opc = Alpha::SLi; break;
1874 case ISD::SRL: Opc = Alpha::SRLi; break;
1875 case ISD::SRA: Opc = Alpha::SRAi; break;
1876 case ISD::MUL: Opc = Alpha::MULQi; break;
1877 };
1878 Tmp1 = SelectExpr(N.getOperand(0));
1879 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1880 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1881 } else {
1882 switch(opcode) {
1883 case ISD::AND: Opc = Alpha::AND; break;
1884 case ISD::OR: Opc = Alpha::BIS; break;
1885 case ISD::XOR: Opc = Alpha::XOR; break;
1886 case ISD::SHL: Opc = Alpha::SL; break;
1887 case ISD::SRL: Opc = Alpha::SRL; break;
1888 case ISD::SRA: Opc = Alpha::SRA; break;
1889 case ISD::MUL: Opc = Alpha::MULQ; break;
1890 };
1891 Tmp1 = SelectExpr(N.getOperand(0));
1892 Tmp2 = SelectExpr(N.getOperand(1));
1893 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1894 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001895 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001896
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001897 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001898 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001899 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001900 bool isAdd = opcode == ISD::ADD;
1901
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001902 //first check for Scaled Adds and Subs!
1903 //Valid for add and sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001904 ConstantSDNode* CSD = NULL;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001905 if(N.getOperand(0).getOpcode() == ISD::SHL &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001906 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
1907 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001908 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001909 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001910 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001911 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) && CSD->getValue() <= 255)
1912 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1913 2, Result).addReg(Tmp2).addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001914 else {
1915 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001916 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1917 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001918 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001919 }
1920 //Position prevents subs
Andrew Lenharth273a1f92005-04-07 14:18:13 +00001921 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001922 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) &&
1923 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001924 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001925 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001926 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001927 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(0))) && CSD->getValue() <= 255)
1928 BuildMI(BB, use4?Alpha::S4ADDQi:Alpha::S8ADDQi, 2, Result).addReg(Tmp2)
1929 .addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001930 else {
1931 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001932 BuildMI(BB, use4?Alpha::S4ADDQ:Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001933 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001934 }
1935 //small addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001936 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
1937 CSD->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001938 { //Normal imm add/sub
1939 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1940 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001941 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CSD->getValue());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001942 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001943 //larger addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001944 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
1945 CSD->getSignExtended() <= 32767 &&
1946 CSD->getSignExtended() >= -32767)
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001947 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001948 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001949 Tmp2 = (long)CSD->getSignExtended();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001950 if (!isAdd)
1951 Tmp2 = -Tmp2;
1952 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001953 }
1954 //give up and do the operation
1955 else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001956 //Normal add/sub
1957 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1958 Tmp1 = SelectExpr(N.getOperand(0));
1959 Tmp2 = SelectExpr(N.getOperand(1));
1960 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1961 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001962 return Result;
1963 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001964
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001965 case ISD::SDIV:
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001966 {
Andrew Lenhartha565c272005-04-06 22:03:13 +00001967 ConstantSDNode* CSD;
1968 //check if we can convert into a shift!
1969 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
1970 (int64_t)CSD->getSignExtended() != 0 &&
1971 ExactLog2(abs((int64_t)CSD->getSignExtended())) != 0)
1972 {
1973 unsigned k = ExactLog2(abs(CSD->getSignExtended()));
1974 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenhartha565c272005-04-06 22:03:13 +00001975 if (k == 1)
1976 Tmp2 = Tmp1;
1977 else
1978 {
1979 Tmp2 = MakeReg(MVT::i64);
1980 BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
1981 }
1982 Tmp3 = MakeReg(MVT::i64);
1983 BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
1984 unsigned Tmp4 = MakeReg(MVT::i64);
1985 BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
1986 if ((int64_t)CSD->getSignExtended() > 0)
1987 BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
1988 else
1989 {
1990 unsigned Tmp5 = MakeReg(MVT::i64);
1991 BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
1992 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
1993 }
1994 return Result;
1995 }
1996 }
1997 //Else fall through
1998
1999 case ISD::UDIV:
2000 {
2001 ConstantSDNode* CSD;
2002 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
2003 ((int64_t)CSD->getSignExtended() >= 2 ||
2004 (int64_t)CSD->getSignExtended() <= -2))
2005 {
2006 // If this is a divide by constant, we can emit code using some magic
2007 // constants to implement it as a multiply instead.
2008 ExprMap.erase(N);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002009 if (opcode == ISD::SDIV)
Andrew Lenhartha565c272005-04-06 22:03:13 +00002010 return SelectExpr(BuildSDIVSequence(N));
2011 else
2012 return SelectExpr(BuildUDIVSequence(N));
2013 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002014 }
2015 //else fall though
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002016 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +00002017 case ISD::SREM:
Misha Brukman4633f1c2005-04-21 23:13:11 +00002018 //FIXME: alpha really doesn't support any of these operations,
Andrew Lenharth40831c52005-01-28 06:57:18 +00002019 // the ops are expanded into special library calls with
2020 // special calling conventions
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002021 //Restore GP because it is a call after all...
Andrew Lenharth40831c52005-01-28 06:57:18 +00002022 switch(opcode) {
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00002023 case ISD::UREM: Opc = Alpha::REMQU; break;
2024 case ISD::SREM: Opc = Alpha::REMQ; break;
2025 case ISD::UDIV: Opc = Alpha::DIVQU; break;
2026 case ISD::SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002027 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002028 Tmp1 = SelectExpr(N.getOperand(0));
2029 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth33819132005-03-04 20:09:23 +00002030 //set up regs explicitly (helps Reg alloc)
2031 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002032 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00002033 AlphaLowering.restoreGP(BB);
Andrew Lenharth33819132005-03-04 20:09:23 +00002034 BuildMI(BB, Opc, 2).addReg(Alpha::R24).addReg(Alpha::R25);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002035 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002036 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002037
Andrew Lenharthe76797c2005-02-01 20:40:27 +00002038 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002039 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002040 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002041 assert (DestType == MVT::i64 && "only quads can be loaded to");
2042 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00002043 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002044 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002045 if (SrcType == MVT::f32)
Misha Brukman7847fca2005-04-22 17:54:37 +00002046 {
2047 Tmp2 = MakeReg(MVT::f64);
2048 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
2049 Tmp1 = Tmp2;
2050 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002051 Tmp2 = MakeReg(MVT::f64);
2052 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00002053 MoveFP2Int(Tmp2, Result, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002054
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002055 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002056 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002057
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002058 case ISD::SELECT:
2059 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00002060 //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 +00002061 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002062 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2063 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002064 // Get the condition into the zero flag.
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002065 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002066
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002067 SDOperand CC = N.getOperand(0);
2068 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
2069
Misha Brukman4633f1c2005-04-21 23:13:11 +00002070 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002071 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
2072 { //FP Setcc -> Int Select
Misha Brukman7847fca2005-04-22 17:54:37 +00002073 Tmp1 = MakeReg(MVT::f64);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002074 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2075 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Misha Brukman7847fca2005-04-22 17:54:37 +00002076 bool inv = SelectFPSetCC(CC, Tmp1);
2077 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
2078 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
2079 return Result;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002080 }
2081 if (CC.getOpcode() == ISD::SETCC) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002082 //Int SetCC -> Select
2083 //Dropping the CC is only useful if we are comparing to 0
2084 if((SetCC->getOperand(1).getOpcode() == ISD::Constant &&
2085 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0) ||
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002086 (SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2087 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0))
2088 {
2089 //figure out a few things
2090 bool LeftZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2091 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
2092 bool RightZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2093 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
2094 bool LeftConst = N.getOperand(1).getOpcode() == ISD::Constant &&
2095 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255;
2096 bool RightConst = N.getOperand(2).getOpcode() == ISD::Constant &&
2097 cast<ConstantSDNode>(N.getOperand(2))->getValue() <= 255;
2098 bool useImm = LeftConst || RightConst;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002099
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002100 //Fix up CC
2101 ISD::CondCode cCode= SetCC->getCondition();
2102 if (RightConst && !LeftConst) //Invert sense to get Imm field right
2103 cCode = ISD::getSetCCInverse(cCode, true);
2104 if (LeftZero && !RightZero) //Swap Operands
2105 cCode = ISD::getSetCCSwappedOperands(cCode);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002106
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002107 //Choose the CMOV
2108 switch (cCode) {
2109 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
2110 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
2111 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
2112 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
2113 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
2114 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
2115 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
2116 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2117 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break; //Technically you could have this CC
2118 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
2119 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2120 }
2121 if(LeftZero && !RightZero) //swap Operands
2122 Tmp1 = SelectExpr(SetCC->getOperand(1)); //Cond
2123 else
2124 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
2125
2126 if (LeftConst) {
2127 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2128 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
Misha Brukman7847fca2005-04-22 17:54:37 +00002129 .addImm(cast<ConstantSDNode>(N.getOperand(1))->getValue())
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002130 .addReg(Tmp1);
2131 } else if (RightConst) {
2132 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
2133 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
Misha Brukman7847fca2005-04-22 17:54:37 +00002134 .addImm(cast<ConstantSDNode>(N.getOperand(2))->getValue())
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002135 .addReg(Tmp1);
2136 } else {
2137 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2138 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2139 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
2140 }
2141 return Result;
2142 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002143 //Otherwise, fall though
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002144 }
2145 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002146 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2147 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthe76797c2005-02-01 20:40:27 +00002148 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002149
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002150 return Result;
2151 }
2152
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002153 case ISD::Constant:
2154 {
Andrew Lenharthc0513832005-03-29 19:24:04 +00002155 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002156 if (val <= IMM_HIGH && val >= IMM_LOW) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002157 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002158 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002159 else if (val <= (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT &&
2160 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
2161 Tmp1 = MakeReg(MVT::i64);
2162 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val)).addReg(Alpha::R31);
2163 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002164 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002165 else {
2166 MachineConstantPool *CP = BB->getParent()->getConstantPool();
2167 ConstantUInt *C = ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
2168 unsigned CPI = CP->getConstantPoolIndex(C);
2169 AlphaLowering.restoreGP(BB);
2170 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CPI);
2171 }
2172 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002173 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002174 }
2175
2176 return 0;
2177}
2178
2179void ISel::Select(SDOperand N) {
2180 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00002181 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002182
Nate Begeman85fdeb22005-03-24 04:39:54 +00002183 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002184 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002185
2186 SDNode *Node = N.Val;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002187
Andrew Lenharth760270d2005-02-07 23:02:23 +00002188 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002189
2190 default:
2191 Node->dump(); std::cerr << "\n";
2192 assert(0 && "Node not handled yet!");
2193
2194 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00002195 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002196 return;
2197 }
2198
2199 case ISD::BR: {
2200 MachineBasicBlock *Dest =
2201 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2202
2203 Select(N.getOperand(0));
2204 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
2205 return;
2206 }
2207
2208 case ISD::ImplicitDef:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002209 ++count_ins;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002210 Select(N.getOperand(0));
2211 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
2212 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002213
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002214 case ISD::EntryToken: return; // Noop
2215
2216 case ISD::TokenFactor:
2217 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2218 Select(Node->getOperand(i));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002219
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002220 //N.Val->dump(); std::cerr << "\n";
2221 //assert(0 && "Node not handled yet!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00002222
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002223 return;
2224
2225 case ISD::CopyToReg:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002226 ++count_outs;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002227 Select(N.getOperand(0));
2228 Tmp1 = SelectExpr(N.getOperand(1));
2229 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002230
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002231 if (Tmp1 != Tmp2) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002232 if (N.getOperand(1).getValueType() == MVT::f64 ||
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002233 N.getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00002234 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2235 else
2236 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002237 }
2238 return;
2239
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002240 case ISD::RET:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002241 ++count_outs;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002242 switch (N.getNumOperands()) {
2243 default:
2244 std::cerr << N.getNumOperands() << "\n";
2245 for (unsigned i = 0; i < N.getNumOperands(); ++i)
2246 std::cerr << N.getOperand(i).getValueType() << "\n";
2247 Node->dump();
2248 assert(0 && "Unknown return instruction!");
2249 case 2:
2250 Select(N.getOperand(0));
2251 Tmp1 = SelectExpr(N.getOperand(1));
2252 switch (N.getOperand(1).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002253 default: Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002254 assert(0 && "All other types should have been promoted!!");
2255 case MVT::f64:
2256 case MVT::f32:
2257 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
2258 break;
2259 case MVT::i32:
2260 case MVT::i64:
2261 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
2262 break;
2263 }
2264 break;
2265 case 1:
2266 Select(N.getOperand(0));
2267 break;
2268 }
2269 //Tmp2 = AlphaLowering.getRetAddr();
2270 //BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(Tmp2).addReg(Tmp2);
2271 BuildMI(BB, Alpha::RETURN, 0); // Just emit a 'ret' instruction
2272 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002273
Misha Brukman4633f1c2005-04-21 23:13:11 +00002274 case ISD::TRUNCSTORE:
2275 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002276 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002277 SDOperand Chain = N.getOperand(0);
2278 SDOperand Value = N.getOperand(1);
2279 SDOperand Address = N.getOperand(2);
2280 Select(Chain);
2281
2282 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00002283
2284 if (opcode == ISD::STORE) {
2285 switch(Value.getValueType()) {
2286 default: assert(0 && "unknown Type in store");
2287 case MVT::i64: Opc = Alpha::STQ; break;
2288 case MVT::f64: Opc = Alpha::STT; break;
2289 case MVT::f32: Opc = Alpha::STS; break;
2290 }
2291 } else { //ISD::TRUNCSTORE
2292 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2293 default: assert(0 && "unknown Type in store");
2294 case MVT::i1: //FIXME: DAG does not promote this load
2295 case MVT::i8: Opc = Alpha::STB; break;
2296 case MVT::i16: Opc = Alpha::STW; break;
2297 case MVT::i32: Opc = Alpha::STL; break;
2298 }
Andrew Lenharth65838902005-02-06 16:22:15 +00002299 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00002300
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002301 if (Address.getOpcode() == ISD::GlobalAddress)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002302 {
2303 AlphaLowering.restoreGP(BB);
2304 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002305 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002306 BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
2307 }
Andrew Lenharth05380342005-02-07 05:07:00 +00002308 else if(Address.getOpcode() == ISD::FrameIndex)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002309 {
Andrew Lenharth032f2352005-02-22 21:59:48 +00002310 BuildMI(BB, Opc, 3).addReg(Tmp1)
2311 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
2312 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002313 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002314 else
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002315 {
2316 long offset;
2317 SelectAddr(Address, Tmp2, offset);
2318 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2319 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002320 return;
2321 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002322
2323 case ISD::EXTLOAD:
2324 case ISD::SEXTLOAD:
2325 case ISD::ZEXTLOAD:
2326 case ISD::LOAD:
2327 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002328 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002329 case ISD::CALL:
Andrew Lenharth032f2352005-02-22 21:59:48 +00002330 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002331 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002332 SelectExpr(N);
2333 return;
2334
Chris Lattner16cd04d2005-05-12 23:24:06 +00002335 case ISD::CALLSEQ_START:
2336 case ISD::CALLSEQ_END:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002337 Select(N.getOperand(0));
2338 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002339
Chris Lattner16cd04d2005-05-12 23:24:06 +00002340 Opc = N.getOpcode() == ISD::CALLSEQ_START ? Alpha::ADJUSTSTACKDOWN :
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002341 Alpha::ADJUSTSTACKUP;
2342 BuildMI(BB, Opc, 1).addImm(Tmp1);
2343 return;
Andrew Lenharth95762122005-03-31 21:24:06 +00002344
2345 case ISD::PCMARKER:
2346 Select(N.getOperand(0)); //Chain
2347 BuildMI(BB, Alpha::PCLABEL, 2).addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
2348 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002349 }
2350 assert(0 && "Should not be reached!");
2351}
2352
2353
2354/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
2355/// into a machine code representation using pattern matching and a machine
2356/// description file.
2357///
2358FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002359 return new ISel(TM);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002360}
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002361