blob: 59c59981f87684f6d97aac87f9cebd9850dd4e2f [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 Lenharth304d0f32005-01-22 23:41:55 +000050//===----------------------------------------------------------------------===//
51// AlphaTargetLowering - Alpha Implementation of the TargetLowering interface
52namespace {
53 class AlphaTargetLowering : public TargetLowering {
54 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
55 unsigned GP; //GOT vreg
56 public:
57 AlphaTargetLowering(TargetMachine &TM) : TargetLowering(TM) {
58 // Set up the TargetLowering object.
Andrew Lenharth3d65d312005-01-27 03:49:45 +000059 //I am having problems with shr n ubyte 1
Andrew Lenharth879ef222005-02-02 17:00:21 +000060 setShiftAmountType(MVT::i64);
61 setSetCCResultType(MVT::i64);
Andrew Lenharthd3355e22005-04-07 20:11:32 +000062 setSetCCResultContents(ZeroOrOneSetCCResult);
Misha Brukman4633f1c2005-04-21 23:13:11 +000063
Andrew Lenharth304d0f32005-01-22 23:41:55 +000064 addRegisterClass(MVT::i64, Alpha::GPRCRegisterClass);
65 addRegisterClass(MVT::f64, Alpha::FPRCRegisterClass);
Andrew Lenharth3d65d312005-01-27 03:49:45 +000066 addRegisterClass(MVT::f32, Alpha::FPRCRegisterClass);
Misha Brukman4633f1c2005-04-21 23:13:11 +000067
Chris Lattnerda4d4692005-04-09 03:22:37 +000068 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000069 setOperationAction(ISD::EXTLOAD , MVT::i1 , Promote);
70 setOperationAction(ISD::EXTLOAD , MVT::f32 , Promote);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000071
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000072 setOperationAction(ISD::ZEXTLOAD , MVT::i1 , Expand);
73 setOperationAction(ISD::ZEXTLOAD , MVT::i32 , Expand);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000074
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000075 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
76 setOperationAction(ISD::SEXTLOAD , MVT::i8 , Expand);
77 setOperationAction(ISD::SEXTLOAD , MVT::i16 , Expand);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000078
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000079 setOperationAction(ISD::SREM , MVT::f32 , Expand);
80 setOperationAction(ISD::SREM , MVT::f64 , Expand);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +000081
Andrew Lenharth59009192005-05-04 19:12:09 +000082 if (!EnableAlphaCT) {
83 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
84 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
Andrew Lenharthb5884d32005-05-04 19:25:37 +000085 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
Andrew Lenharth59009192005-05-04 19:12:09 +000086 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +000087
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000088 //If this didn't legalize into a div....
89 // setOperationAction(ISD::SREM , MVT::i64, Expand);
90 // setOperationAction(ISD::UREM , MVT::i64, Expand);
91
92 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
93 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
94 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
Andrew Lenharth9818c052005-02-05 13:19:12 +000095
Chris Lattner17234b72005-04-30 04:26:06 +000096 // We don't support sin/cos/sqrt
97 setOperationAction(ISD::FSIN , MVT::f64, Expand);
98 setOperationAction(ISD::FCOS , MVT::f64, Expand);
99 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
100 setOperationAction(ISD::FSIN , MVT::f32, Expand);
101 setOperationAction(ISD::FCOS , MVT::f32, Expand);
102 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
103
Andrew Lenharth33819132005-03-04 20:09:23 +0000104 //Doesn't work yet
Chris Lattner17234b72005-04-30 04:26:06 +0000105 setOperationAction(ISD::SETCC, MVT::f32, Promote);
Andrew Lenharth572af902005-02-14 05:41:43 +0000106
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000107 computeRegisterProperties();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000108
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000109 addLegalFPImmediate(+0.0); //F31
110 addLegalFPImmediate(-0.0); //-F31
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000111 }
112
113 /// LowerArguments - This hook must be implemented to indicate how we should
114 /// lower the arguments for the specified function, into the specified DAG.
115 virtual std::vector<SDOperand>
116 LowerArguments(Function &F, SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000117
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000118 /// LowerCallTo - This hook lowers an abstract call to a function into an
119 /// actual call.
120 virtual std::pair<SDOperand, SDOperand>
Chris Lattnerc57f6822005-05-12 19:56:45 +0000121 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattneradf6a962005-05-13 18:50:42 +0000122 bool isTailCall, SDOperand Callee, ArgListTy &Args,
123 SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000124
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000125 virtual std::pair<SDOperand, SDOperand>
126 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000127
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000128 virtual std::pair<SDOperand,SDOperand>
129 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
130 const Type *ArgTy, SelectionDAG &DAG);
131
132 virtual std::pair<SDOperand, SDOperand>
133 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
134 SelectionDAG &DAG);
135
136 void restoreGP(MachineBasicBlock* BB)
137 {
138 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
139 }
140 };
141}
142
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000143/// AddLiveIn - This helper function adds the specified physical register to the
144/// MachineFunction as a live in value. It also creates a corresponding virtual
145/// register for it.
146static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
147 TargetRegisterClass *RC) {
148 assert(RC->contains(PReg) && "Not the correct regclass!");
149 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
150 MF.addLiveIn(PReg, VReg);
151 return VReg;
152}
153
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000154//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
155
156//For now, just use variable size stack frame format
157
158//In a standard call, the first six items are passed in registers $16
159//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
160//of argument-to-register correspondence.) The remaining items are
161//collected in a memory argument list that is a naturally aligned
162//array of quadwords. In a standard call, this list, if present, must
163//be passed at 0(SP).
Misha Brukman7847fca2005-04-22 17:54:37 +0000164//7 ... n 0(SP) ... (n-7)*8(SP)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000165
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000166// //#define FP $15
167// //#define RA $26
168// //#define PV $27
169// //#define GP $29
170// //#define SP $30
Misha Brukman4633f1c2005-04-21 23:13:11 +0000171
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000172std::vector<SDOperand>
Misha Brukman4633f1c2005-04-21 23:13:11 +0000173AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000174{
175 std::vector<SDOperand> ArgValues;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000176
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000177 MachineFunction &MF = DAG.getMachineFunction();
Andrew Lenharth05380342005-02-07 05:07:00 +0000178 MachineFrameInfo*MFI = MF.getFrameInfo();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000179
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000180 MachineBasicBlock& BB = MF.front();
181
182 //Handle the return address
183 //BuildMI(&BB, Alpha::IDEF, 0, Alpha::R26);
184
Misha Brukman4633f1c2005-04-21 23:13:11 +0000185 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000186 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000187 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000188 Alpha::F19, Alpha::F20, Alpha::F21};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000189 int count = 0;
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000190
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000191 GP = AddLiveIn(MF, Alpha::R29, getRegClassFor(MVT::i64));
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000192
Chris Lattnere4d5c442005-03-15 04:54:21 +0000193 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000194 {
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000195 SDOperand argt;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000196 if (count < 6) {
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000197 unsigned Vreg;
198 MVT::ValueType VT = getValueType(I->getType());
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000199 switch (VT) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000200 default:
201 std::cerr << "Unknown Type " << VT << "\n";
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000202 abort();
203 case MVT::f64:
204 case MVT::f32:
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000205 args_float[count] = AddLiveIn(MF,args_float[count], getRegClassFor(VT));
206 argt = DAG.getCopyFromReg(args_float[count], VT, DAG.getRoot());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000207 break;
208 case MVT::i1:
209 case MVT::i8:
210 case MVT::i16:
211 case MVT::i32:
212 case MVT::i64:
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000213 args_int[count] = AddLiveIn(MF, args_int[count], getRegClassFor(VT));
214 argt = DAG.getCopyFromReg(args_int[count], VT, DAG.getRoot());
215 // if (VT != MVT::i64)
216 // argt = DAG.getNode(ISD::TRUNCATE, VT, argt);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000217 break;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000218 }
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000219 DAG.setRoot(argt.getValue(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000220 } else { //more args
221 // Create the frame index object for this incoming parameter...
222 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000223
224 // Create the SelectionDAG nodes corresponding to a load
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000225 //from this parameter
226 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000227 argt = DAG.getLoad(getValueType(I->getType()),
228 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000229 }
Andrew Lenharth032f2352005-02-22 21:59:48 +0000230 ++count;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000231 ArgValues.push_back(argt);
232 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000233
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000234 // If the functions takes variable number of arguments, copy all regs to stack
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000235 if (F.isVarArg()) {
236 std::vector<SDOperand> LS;
237 for (int i = 0; i < 6; ++i) {
238 if (args_int[i] < 1024)
239 args_int[i] = AddLiveIn(MF,args_int[i], getRegClassFor(MVT::i64));
240 SDOperand argt = DAG.getCopyFromReg(args_int[i], MVT::i64, DAG.getRoot());
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000241 int FI = MFI->CreateFixedObject(8, -8 * (6 - i));
242 SDOperand SDFI = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000243 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt, SDFI, DAG.getSrcValue(NULL)));
244
245 if (args_float[i] < 1024)
246 args_float[i] = AddLiveIn(MF,args_float[i], getRegClassFor(MVT::f64));
247 argt = DAG.getCopyFromReg(args_float[i], MVT::f64, DAG.getRoot());
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000248 FI = MFI->CreateFixedObject(8, - 8 * (12 - i));
249 SDFI = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000250 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt, SDFI, DAG.getSrcValue(NULL)));
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000251 }
252
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000253 //Set up a token factor with all the stack traffic
254 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, LS));
255 }
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000256
257 // Finally, inform the code generator which regs we return values in.
258 switch (getValueType(F.getReturnType())) {
259 default: assert(0 && "Unknown type!");
260 case MVT::isVoid: break;
261 case MVT::i1:
262 case MVT::i8:
263 case MVT::i16:
264 case MVT::i32:
265 case MVT::i64:
266 MF.addLiveOut(Alpha::R0);
267 break;
268 case MVT::f32:
269 case MVT::f64:
270 MF.addLiveOut(Alpha::F0);
271 break;
272 }
273
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000274 //return the arguments
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000275 return ArgValues;
276}
277
278std::pair<SDOperand, SDOperand>
279AlphaTargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000280 const Type *RetTy, bool isVarArg,
Chris Lattneradf6a962005-05-13 18:50:42 +0000281 unsigned CallingConv, bool isTailCall,
Misha Brukman7847fca2005-04-22 17:54:37 +0000282 SDOperand Callee, ArgListTy &Args,
283 SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000284 int NumBytes = 0;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000285 if (Args.size() > 6)
286 NumBytes = (Args.size() - 6) * 8;
287
Chris Lattner16cd04d2005-05-12 23:24:06 +0000288 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000289 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000290 std::vector<SDOperand> args_to_use;
291 for (unsigned i = 0, e = Args.size(); i != e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000292 {
293 switch (getValueType(Args[i].second)) {
294 default: assert(0 && "Unexpected ValueType for argument!");
295 case MVT::i1:
296 case MVT::i8:
297 case MVT::i16:
298 case MVT::i32:
299 // Promote the integer to 64 bits. If the input type is signed use a
300 // sign extend, otherwise use a zero extend.
301 if (Args[i].second->isSigned())
302 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
303 else
304 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
305 break;
306 case MVT::i64:
307 case MVT::f64:
308 case MVT::f32:
309 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000310 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000311 args_to_use.push_back(Args[i].first);
312 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000313
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000314 std::vector<MVT::ValueType> RetVals;
315 MVT::ValueType RetTyVT = getValueType(RetTy);
316 if (RetTyVT != MVT::isVoid)
317 RetVals.push_back(RetTyVT);
318 RetVals.push_back(MVT::Other);
319
Misha Brukman4633f1c2005-04-21 23:13:11 +0000320 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000321 Chain, Callee, args_to_use), 0);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000322 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner16cd04d2005-05-12 23:24:06 +0000323 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000324 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000325 return std::make_pair(TheCall, Chain);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000326}
327
328std::pair<SDOperand, SDOperand>
329AlphaTargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
330 //vastart just returns the address of the VarArgsFrameIndex slot.
331 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64), Chain);
332}
333
334std::pair<SDOperand,SDOperand> AlphaTargetLowering::
335LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000336 const Type *ArgTy, SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000337 abort();
338}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000339
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000340
341std::pair<SDOperand, SDOperand> AlphaTargetLowering::
342LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
343 SelectionDAG &DAG) {
344 abort();
345}
346
347
348
349
350
351namespace {
352
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000353//===--------------------------------------------------------------------===//
354/// ISel - Alpha specific code to select Alpha machine instructions for
355/// SelectionDAG operations.
356//===--------------------------------------------------------------------===//
357class ISel : public SelectionDAGISel {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000358
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000359 /// AlphaLowering - This object fully describes how to lower LLVM code to an
360 /// Alpha-specific SelectionDAG.
361 AlphaTargetLowering AlphaLowering;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000362
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000363 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
364 // for sdiv and udiv until it is put into the future
365 // dag combiner.
366
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000367 /// ExprMap - As shared expressions are codegen'd, we keep track of which
368 /// vreg the value is produced in, so we only emit one copy of each compiled
369 /// tree.
370 static const unsigned notIn = (unsigned)(-1);
371 std::map<SDOperand, unsigned> ExprMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000372
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000373 //CCInvMap sometimes (SetNE) we have the inverse CC code for free
374 std::map<SDOperand, unsigned> CCInvMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000375
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000376 int count_ins;
377 int count_outs;
378 bool has_sym;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000379 int max_depth;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000380
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000381public:
Misha Brukman4633f1c2005-04-21 23:13:11 +0000382 ISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering), AlphaLowering(TM)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000383 {}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000384
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000385 /// InstructionSelectBasicBlock - This callback is invoked by
386 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
387 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
Andrew Lenharth032f2352005-02-22 21:59:48 +0000388 DEBUG(BB->dump());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000389 count_ins = 0;
390 count_outs = 0;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000391 max_depth = 0;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000392 has_sym = false;
393
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000394 // Codegen the basic block.
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000395 ISelDAG = &DAG;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000396 max_depth = DAG.getRoot().getNodeDepth();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000397 Select(DAG.getRoot());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000398
399 if(has_sym)
400 ++count_ins;
401 if(EnableAlphaCount)
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000402 std::cerr << "COUNT: " << BB->getParent()->getFunction ()->getName() << " "
403 << BB->getNumber() << " "
404 << max_depth << " "
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000405 << count_ins << " "
406 << count_outs << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000407
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000408 // Clear state used for selection.
409 ExprMap.clear();
410 CCInvMap.clear();
411 }
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000412
413 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000414
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000415 unsigned SelectExpr(SDOperand N);
416 unsigned SelectExprFP(SDOperand N, unsigned Result);
417 void Select(SDOperand N);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000418
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000419 void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
420 void SelectBranchCC(SDOperand N);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000421 void MoveFP2Int(unsigned src, unsigned dst, bool isDouble);
422 void MoveInt2FP(unsigned src, unsigned dst, bool isDouble);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000423 //returns whether the sense of the comparison was inverted
424 bool SelectFPSetCC(SDOperand N, unsigned dst);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000425
426 // dag -> dag expanders for integer divide by constant
427 SDOperand BuildSDIVSequence(SDOperand N);
428 SDOperand BuildUDIVSequence(SDOperand N);
429
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000430};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000431}
432
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000433void ISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
434 // If this function has live-in values, emit the copies from pregs to vregs at
435 // the top of the function, before anything else.
436 MachineBasicBlock *BB = MF.begin();
437 if (MF.livein_begin() != MF.livein_end()) {
438 SSARegMap *RegMap = MF.getSSARegMap();
439 for (MachineFunction::livein_iterator LI = MF.livein_begin(),
440 E = MF.livein_end(); LI != E; ++LI) {
441 const TargetRegisterClass *RC = RegMap->getRegClass(LI->second);
442 if (RC == Alpha::GPRCRegisterClass) {
443 BuildMI(BB, Alpha::BIS, 2, LI->second).addReg(LI->first).addReg(LI->first);
444 } else if (RC == Alpha::FPRCRegisterClass) {
445 BuildMI(BB, Alpha::CPYS, 2, LI->second).addReg(LI->first).addReg(LI->first);
446 } else {
447 assert(0 && "Unknown regclass!");
448 }
449 }
450 }
451}
452
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000453//Factorize a number using the list of constants
454static bool factorize(int v[], int res[], int size, uint64_t c)
455{
456 bool cont = true;
457 while (c != 1 && cont)
458 {
459 cont = false;
460 for(int i = 0; i < size; ++i)
461 {
462 if (c % v[i] == 0)
463 {
464 c /= v[i];
465 ++res[i];
466 cont=true;
467 }
468 }
469 }
470 return c == 1;
471}
472
473
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000474//Shamelessly adapted from PPC32
Misha Brukman4633f1c2005-04-21 23:13:11 +0000475// Structure used to return the necessary information to codegen an SDIV as
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000476// a multiply.
477struct ms {
478 int64_t m; // magic number
479 int64_t s; // shift amount
480};
481
482struct mu {
483 uint64_t m; // magic number
484 int64_t a; // add indicator
485 int64_t s; // shift amount
486};
487
488/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukman4633f1c2005-04-21 23:13:11 +0000489/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000490/// or -1.
491static struct ms magic(int64_t d) {
492 int64_t p;
493 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
494 const uint64_t two63 = 9223372036854775808ULL; // 2^63
495 struct ms mag;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000496
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000497 ad = abs(d);
498 t = two63 + ((uint64_t)d >> 63);
499 anc = t - 1 - t%ad; // absolute value of nc
Andrew Lenharth320174f2005-04-07 17:19:16 +0000500 p = 63; // initialize p
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000501 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
502 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
503 q2 = two63/ad; // initialize q2 = 2p/abs(d)
504 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
505 do {
506 p = p + 1;
507 q1 = 2*q1; // update q1 = 2p/abs(nc)
508 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
509 if (r1 >= anc) { // must be unsigned comparison
510 q1 = q1 + 1;
511 r1 = r1 - anc;
512 }
513 q2 = 2*q2; // update q2 = 2p/abs(d)
514 r2 = 2*r2; // update r2 = rem(2p/abs(d))
515 if (r2 >= ad) { // must be unsigned comparison
516 q2 = q2 + 1;
517 r2 = r2 - ad;
518 }
519 delta = ad - r2;
520 } while (q1 < delta || (q1 == delta && r1 == 0));
521
522 mag.m = q2 + 1;
523 if (d < 0) mag.m = -mag.m; // resulting magic number
524 mag.s = p - 64; // resulting shift
525 return mag;
526}
527
528/// magicu - calculate the magic numbers required to codegen an integer udiv as
529/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
530static struct mu magicu(uint64_t d)
531{
532 int64_t p;
533 uint64_t nc, delta, q1, r1, q2, r2;
534 struct mu magu;
535 magu.a = 0; // initialize "add" indicator
536 nc = - 1 - (-d)%d;
Andrew Lenharth320174f2005-04-07 17:19:16 +0000537 p = 63; // initialize p
538 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
539 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
540 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
541 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000542 do {
543 p = p + 1;
544 if (r1 >= nc - r1 ) {
545 q1 = 2*q1 + 1; // update q1
546 r1 = 2*r1 - nc; // update r1
547 }
548 else {
549 q1 = 2*q1; // update q1
550 r1 = 2*r1; // update r1
551 }
552 if (r2 + 1 >= d - r2) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000553 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000554 q2 = 2*q2 + 1; // update q2
555 r2 = 2*r2 + 1 - d; // update r2
556 }
557 else {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000558 if (q2 >= 0x8000000000000000ull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000559 q2 = 2*q2; // update q2
560 r2 = 2*r2 + 1; // update r2
561 }
562 delta = d - 1 - r2;
563 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
564 magu.m = q2 + 1; // resulting magic number
Andrew Lenharth320174f2005-04-07 17:19:16 +0000565 magu.s = p - 64; // resulting shift
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000566 return magu;
567}
568
569/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
570/// return a DAG expression to select that will generate the same value by
571/// multiplying by a magic number. See:
572/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
573SDOperand ISel::BuildSDIVSequence(SDOperand N) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000574 int64_t d = (int64_t)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000575 ms magics = magic(d);
576 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000577 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000578 ISelDAG->getConstant(magics.m, MVT::i64));
579 // If d > 0 and m < 0, add the numerator
580 if (d > 0 && magics.m < 0)
581 Q = ISelDAG->getNode(ISD::ADD, MVT::i64, Q, N.getOperand(0));
582 // If d < 0 and m > 0, subtract the numerator.
583 if (d < 0 && magics.m > 0)
584 Q = ISelDAG->getNode(ISD::SUB, MVT::i64, Q, N.getOperand(0));
585 // Shift right algebraic if shift value is nonzero
586 if (magics.s > 0)
Misha Brukman4633f1c2005-04-21 23:13:11 +0000587 Q = ISelDAG->getNode(ISD::SRA, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000588 ISelDAG->getConstant(magics.s, MVT::i64));
589 // Extract the sign bit and add it to the quotient
Misha Brukman4633f1c2005-04-21 23:13:11 +0000590 SDOperand T =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000591 ISelDAG->getNode(ISD::SRL, MVT::i64, Q, ISelDAG->getConstant(63, MVT::i64));
592 return ISelDAG->getNode(ISD::ADD, MVT::i64, Q, T);
593}
594
595/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
596/// return a DAG expression to select that will generate the same value by
597/// multiplying by a magic number. See:
598/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
599SDOperand ISel::BuildUDIVSequence(SDOperand N) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000600 unsigned d =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000601 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
602 mu magics = magicu(d);
603 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000604 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000605 ISelDAG->getConstant(magics.m, MVT::i64));
606 if (magics.a == 0) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000607 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000608 ISelDAG->getConstant(magics.s, MVT::i64));
609 } else {
610 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i64, N.getOperand(0), Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000611 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000612 ISelDAG->getConstant(1, MVT::i64));
613 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i64, NPQ, Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000614 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000615 ISelDAG->getConstant(magics.s-1, MVT::i64));
616 }
617 return Q;
618}
619
Andrew Lenhartha565c272005-04-06 22:03:13 +0000620//From PPC32
621/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
622/// returns zero when the input is not exactly a power of two.
623static unsigned ExactLog2(uint64_t Val) {
624 if (Val == 0 || (Val & (Val-1))) return 0;
625 unsigned Count = 0;
626 while (Val != 1) {
627 Val >>= 1;
628 ++Count;
629 }
630 return Count;
631}
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000632
633
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000634//These describe LDAx
Andrew Lenharthc0513832005-03-29 19:24:04 +0000635static const int IMM_LOW = -32768;
636static const int IMM_HIGH = 32767;
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000637static const int IMM_MULT = 65536;
638
639static long getUpper16(long l)
640{
641 long y = l / IMM_MULT;
642 if (l % IMM_MULT > IMM_HIGH)
643 ++y;
644 return y;
645}
646
647static long getLower16(long l)
648{
649 long h = getUpper16(l);
650 return l - h * IMM_MULT;
651}
652
Andrew Lenharth65838902005-02-06 16:22:15 +0000653static unsigned GetSymVersion(unsigned opcode)
654{
655 switch (opcode) {
656 default: assert(0 && "unknown load or store"); return 0;
657 case Alpha::LDQ: return Alpha::LDQ_SYM;
658 case Alpha::LDS: return Alpha::LDS_SYM;
659 case Alpha::LDT: return Alpha::LDT_SYM;
660 case Alpha::LDL: return Alpha::LDL_SYM;
661 case Alpha::LDBU: return Alpha::LDBU_SYM;
662 case Alpha::LDWU: return Alpha::LDWU_SYM;
663 case Alpha::LDW: return Alpha::LDW_SYM;
664 case Alpha::LDB: return Alpha::LDB_SYM;
665 case Alpha::STQ: return Alpha::STQ_SYM;
666 case Alpha::STS: return Alpha::STS_SYM;
667 case Alpha::STT: return Alpha::STT_SYM;
668 case Alpha::STL: return Alpha::STL_SYM;
669 case Alpha::STW: return Alpha::STW_SYM;
670 case Alpha::STB: return Alpha::STB_SYM;
671 }
672}
673
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000674void ISel::MoveFP2Int(unsigned src, unsigned dst, bool isDouble)
675{
676 unsigned Opc;
677 if (EnableAlphaFTOI) {
678 Opc = isDouble ? Alpha::FTOIT : Alpha::FTOIS;
679 BuildMI(BB, Opc, 1, dst).addReg(src);
680 } else {
681 //The hard way:
682 // Spill the integer to memory and reload it from there.
683 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
684 MachineFunction *F = BB->getParent();
685 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
686
687 Opc = isDouble ? Alpha::STT : Alpha::STS;
688 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
689 Opc = isDouble ? Alpha::LDQ : Alpha::LDL;
690 BuildMI(BB, Alpha::LDQ, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
691 }
692}
693
694void ISel::MoveInt2FP(unsigned src, unsigned dst, bool isDouble)
695{
696 unsigned Opc;
697 if (EnableAlphaFTOI) {
698 Opc = isDouble?Alpha::ITOFT:Alpha::ITOFS;
699 BuildMI(BB, Opc, 1, dst).addReg(src);
700 } else {
701 //The hard way:
702 // Spill the integer to memory and reload it from there.
703 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
704 MachineFunction *F = BB->getParent();
705 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
706
707 Opc = isDouble ? Alpha::STQ : Alpha::STL;
708 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
709 Opc = isDouble ? Alpha::LDT : Alpha::LDS;
710 BuildMI(BB, Opc, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
711 }
712}
713
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000714bool ISel::SelectFPSetCC(SDOperand N, unsigned dst)
715{
716 SDNode *Node = N.Val;
717 unsigned Opc, Tmp1, Tmp2, Tmp3;
718 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node);
719
720 //assert(SetCC->getOperand(0).getValueType() != MVT::f32 && "SetCC f32 should have been promoted");
721 bool rev = false;
722 bool inv = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000723
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000724 switch (SetCC->getCondition()) {
725 default: Node->dump(); assert(0 && "Unknown FP comparison!");
726 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
727 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
728 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
729 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
730 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
731 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
732 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000733
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000734 //FIXME: check for constant 0.0
735 ConstantFPSDNode *CN;
736 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
737 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
738 Tmp1 = Alpha::F31;
739 else
740 Tmp1 = SelectExpr(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000741
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000742 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
743 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
744 Tmp2 = Alpha::F31;
745 else
Chris Lattner9c9183a2005-04-30 04:44:07 +0000746 Tmp2 = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000747
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000748 //Can only compare doubles, and dag won't promote for me
749 if (SetCC->getOperand(0).getValueType() == MVT::f32)
750 {
751 //assert(0 && "Setcc On float?\n");
752 std::cerr << "Setcc on float!\n";
753 Tmp3 = MakeReg(MVT::f64);
754 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
755 Tmp1 = Tmp3;
756 }
757 if (SetCC->getOperand(1).getValueType() == MVT::f32)
758 {
759 //assert (0 && "Setcc On float?\n");
760 std::cerr << "Setcc on float!\n";
761 Tmp3 = MakeReg(MVT::f64);
762 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp2);
763 Tmp2 = Tmp3;
764 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000765
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000766 if (rev) std::swap(Tmp1, Tmp2);
767 //do the comparison
768 BuildMI(BB, Opc, 2, dst).addReg(Tmp1).addReg(Tmp2);
769 return inv;
770}
771
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000772//Check to see if the load is a constant offset from a base register
773void ISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
774{
775 unsigned opcode = N.getOpcode();
776 if (opcode == ISD::ADD) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000777 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000778 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
779 { //Normal imm add
780 Reg = SelectExpr(N.getOperand(0));
781 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
782 return;
783 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000784 else if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000785 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 32767)
786 {
787 Reg = SelectExpr(N.getOperand(1));
788 offset = cast<ConstantSDNode>(N.getOperand(0))->getValue();
789 return;
790 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000791 }
792 Reg = SelectExpr(N);
793 offset = 0;
794 return;
795}
796
Andrew Lenharth445171a2005-02-08 00:40:03 +0000797void ISel::SelectBranchCC(SDOperand N)
798{
799 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000800 MachineBasicBlock *Dest =
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000801 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
802 unsigned Opc = Alpha::WTF;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000803
Andrew Lenharth445171a2005-02-08 00:40:03 +0000804 Select(N.getOperand(0)); //chain
805 SDOperand CC = N.getOperand(1);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000806
Andrew Lenharth445171a2005-02-08 00:40:03 +0000807 if (CC.getOpcode() == ISD::SETCC)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000808 {
809 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
810 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
811 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000812 bool LeftZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
813 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
814 bool RightZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
815 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000816 bool isNE = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000817
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000818 //Fix up CC
819 ISD::CondCode cCode= SetCC->getCondition();
820 if (LeftZero && !RightZero) //Swap Operands
821 cCode = ISD::getSetCCSwappedOperands(cCode);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000822
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000823 if(cCode == ISD::SETNE)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000824 isNE = true;
Andrew Lenharth445171a2005-02-08 00:40:03 +0000825
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000826 if (LeftZero || RightZero) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000827 switch (SetCC->getCondition()) {
828 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
829 case ISD::SETEQ: Opc = Alpha::BEQ; break;
830 case ISD::SETLT: Opc = Alpha::BLT; break;
831 case ISD::SETLE: Opc = Alpha::BLE; break;
832 case ISD::SETGT: Opc = Alpha::BGT; break;
833 case ISD::SETGE: Opc = Alpha::BGE; break;
834 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
835 case ISD::SETUGT: Opc = Alpha::BNE; break;
836 case ISD::SETULE: Opc = Alpha::BEQ; break; //Technically you could have this CC
837 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
838 case ISD::SETNE: Opc = Alpha::BNE; break;
839 }
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000840 unsigned Tmp1;
841 if(LeftZero && !RightZero) //swap Operands
842 Tmp1 = SelectExpr(SetCC->getOperand(1)); //Cond
843 else
844 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000845 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
846 return;
847 } else {
848 unsigned Tmp1 = SelectExpr(CC);
849 if (isNE)
850 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
851 else
852 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000853 return;
854 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000855 } else { //FP
856 //Any comparison between 2 values should be codegened as an folded branch, as moving
857 //CC to the integer register is very expensive
858 //for a cmp b: c = a - b;
859 //a = b: c = 0
860 //a < b: c < 0
861 //a > b: c > 0
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000862
863 bool invTest = false;
864 unsigned Tmp3;
865
866 ConstantFPSDNode *CN;
867 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
868 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
869 Tmp3 = SelectExpr(SetCC->getOperand(0));
870 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
871 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
872 {
873 Tmp3 = SelectExpr(SetCC->getOperand(1));
874 invTest = true;
875 }
876 else
877 {
878 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
879 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
880 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
881 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
882 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
883 .addReg(Tmp1).addReg(Tmp2);
884 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000885
886 switch (SetCC->getCondition()) {
887 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000888 case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
889 case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
890 case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
891 case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
892 case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
893 case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000894 }
895 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000896 return;
897 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000898 abort(); //Should never be reached
899 } else {
900 //Giveup and do the stupid thing
901 unsigned Tmp1 = SelectExpr(CC);
902 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
903 return;
904 }
Andrew Lenharth445171a2005-02-08 00:40:03 +0000905 abort(); //Should never be reached
906}
907
Andrew Lenharth40831c52005-01-28 06:57:18 +0000908unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
909{
910 unsigned Tmp1, Tmp2, Tmp3;
911 unsigned Opc = 0;
912 SDNode *Node = N.Val;
913 MVT::ValueType DestType = N.getValueType();
914 unsigned opcode = N.getOpcode();
915
916 switch (opcode) {
917 default:
918 Node->dump();
919 assert(0 && "Node not handled!\n");
Andrew Lenharth2c594352005-01-29 15:42:07 +0000920
Andrew Lenharth7332f3e2005-04-02 19:11:07 +0000921 case ISD::UNDEF: {
922 BuildMI(BB, Alpha::IDEF, 0, Result);
923 return Result;
924 }
925
Andrew Lenharth30b46d42005-04-02 19:04:58 +0000926 case ISD::FNEG:
927 if(ISD::FABS == N.getOperand(0).getOpcode())
928 {
Misha Brukman7847fca2005-04-22 17:54:37 +0000929 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
930 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +0000931 } else {
Misha Brukman7847fca2005-04-22 17:54:37 +0000932 Tmp1 = SelectExpr(N.getOperand(0));
933 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +0000934 }
935 return Result;
936
937 case ISD::FABS:
938 Tmp1 = SelectExpr(N.getOperand(0));
939 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
940 return Result;
941
Andrew Lenharth9818c052005-02-05 13:19:12 +0000942 case ISD::SELECT:
943 {
Andrew Lenharth45859692005-03-03 21:47:53 +0000944 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
945 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
946 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
947
948 SDOperand CC = N.getOperand(0);
949 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
950
Misha Brukman4633f1c2005-04-21 23:13:11 +0000951 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth45859692005-03-03 21:47:53 +0000952 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
953 { //FP Setcc -> Select yay!
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000954
955
Andrew Lenharth45859692005-03-03 21:47:53 +0000956 //for a cmp b: c = a - b;
957 //a = b: c = 0
958 //a < b: c < 0
959 //a > b: c > 0
Misha Brukman4633f1c2005-04-21 23:13:11 +0000960
Andrew Lenharth45859692005-03-03 21:47:53 +0000961 bool invTest = false;
962 unsigned Tmp3;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000963
Andrew Lenharth45859692005-03-03 21:47:53 +0000964 ConstantFPSDNode *CN;
965 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
966 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
967 Tmp3 = SelectExpr(SetCC->getOperand(0));
968 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
969 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
970 {
971 Tmp3 = SelectExpr(SetCC->getOperand(1));
972 invTest = true;
973 }
974 else
975 {
976 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
977 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
978 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
979 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
980 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
981 .addReg(Tmp1).addReg(Tmp2);
982 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000983
Andrew Lenharth45859692005-03-03 21:47:53 +0000984 switch (SetCC->getCondition()) {
985 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
986 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNE : Alpha::FCMOVEQ; break;
987 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGT : Alpha::FCMOVLT; break;
988 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGE : Alpha::FCMOVLE; break;
989 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLT : Alpha::FCMOVGT; break;
990 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLE : Alpha::FCMOVGE; break;
991 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQ : Alpha::FCMOVNE; break;
992 }
Andrew Lenharth33819132005-03-04 20:09:23 +0000993 BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
Andrew Lenharth45859692005-03-03 21:47:53 +0000994 return Result;
995 }
996 else
997 {
998 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000999 BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV).addReg(Tmp1);
1000// // Spill the cond to memory and reload it from there.
1001// unsigned Tmp4 = MakeReg(MVT::f64);
1002// MoveIntFP(Tmp1, Tmp4, true);
1003// //now ideally, we don't have to do anything to the flag...
1004// // Get the condition into the zero flag.
1005// BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
Andrew Lenharth45859692005-03-03 21:47:53 +00001006 return Result;
1007 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001008 }
1009
Andrew Lenharthc1faced2005-02-01 01:37:24 +00001010 case ISD::FP_ROUND:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001011 assert (DestType == MVT::f32 &&
1012 N.getOperand(0).getValueType() == MVT::f64 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001013 "only f64 to f32 conversion supported here");
Andrew Lenharthc1faced2005-02-01 01:37:24 +00001014 Tmp1 = SelectExpr(N.getOperand(0));
1015 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
1016 return Result;
1017
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001018 case ISD::FP_EXTEND:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001019 assert (DestType == MVT::f64 &&
1020 N.getOperand(0).getValueType() == MVT::f32 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001021 "only f32 to f64 conversion supported here");
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001022 Tmp1 = SelectExpr(N.getOperand(0));
1023 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
1024 return Result;
1025
Andrew Lenharth2c594352005-01-29 15:42:07 +00001026 case ISD::CopyFromReg:
1027 {
1028 // Make sure we generate both values.
1029 if (Result != notIn)
1030 ExprMap[N.getValue(1)] = notIn; // Generate the token
1031 else
1032 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001033
Andrew Lenharth2c594352005-01-29 15:42:07 +00001034 SDOperand Chain = N.getOperand(0);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001035
Andrew Lenharth2c594352005-01-29 15:42:07 +00001036 Select(Chain);
1037 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1038 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1039 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
1040 return Result;
1041 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001042
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001043 case ISD::LOAD:
1044 {
1045 // Make sure we generate both values.
1046 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001047 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001048 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001049 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001050
Andrew Lenharth29219162005-02-07 06:31:44 +00001051 DestType = N.getValue(0).getValueType();
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001052
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001053 SDOperand Chain = N.getOperand(0);
1054 SDOperand Address = N.getOperand(1);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001055 Select(Chain);
Andrew Lenharth65838902005-02-06 16:22:15 +00001056 Opc = DestType == MVT::f64 ? Alpha::LDT : Alpha::LDS;
1057
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001058 if (Address.getOpcode() == ISD::GlobalAddress) {
1059 AlphaLowering.restoreGP(BB);
1060 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001061 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001062 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1063 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001064 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001065 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001066 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001067 has_sym = true;
Andrew Lenharth97127a12005-02-05 17:41:39 +00001068 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001069 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001070 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001071 BuildMI(BB, Opc, 2, Result)
1072 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1073 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001074 } else {
1075 long offset;
1076 SelectAddr(Address, Tmp1, offset);
1077 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1078 }
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001079 return Result;
1080 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001081 case ISD::ConstantFP:
1082 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
1083 if (CN->isExactlyValue(+0.0)) {
1084 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001085 } else if ( CN->isExactlyValue(-0.0)) {
1086 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001087 } else {
1088 abort();
1089 }
1090 }
1091 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001092
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001093 case ISD::SDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001094 case ISD::MUL:
1095 case ISD::ADD:
1096 case ISD::SUB:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001097 switch( opcode ) {
1098 case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS; break;
1099 case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS; break;
1100 case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS; break;
1101 case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS; break;
1102 };
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001103
1104 ConstantFPSDNode *CN;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001105 if (opcode == ISD::SUB
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001106 && (CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
1107 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1108 {
1109 Tmp2 = SelectExpr(N.getOperand(1));
1110 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp2).addReg(Tmp2);
1111 } else {
1112 Tmp1 = SelectExpr(N.getOperand(0));
1113 Tmp2 = SelectExpr(N.getOperand(1));
1114 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1115 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001116 return Result;
1117
Andrew Lenharth2c594352005-01-29 15:42:07 +00001118 case ISD::EXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001119 {
1120 //include a conversion sequence for float loads to double
1121 if (Result != notIn)
1122 ExprMap[N.getValue(1)] = notIn; // Generate the token
1123 else
1124 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001125
Andrew Lenhartha549deb2005-02-07 05:33:15 +00001126 Tmp1 = MakeReg(MVT::f32);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001127
1128 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001129 "EXTLOAD not from f32");
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001130 assert(Node->getValueType(0) == MVT::f64 && "EXTLOAD not to f64");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001131
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001132 SDOperand Chain = N.getOperand(0);
1133 SDOperand Address = N.getOperand(1);
1134 Select(Chain);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001135
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001136 if (Address.getOpcode() == ISD::GlobalAddress) {
1137 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001138 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001139 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1140 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001141 else if (ConstantPoolSDNode *CP =
1142 dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001143 {
1144 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001145 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001146 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addConstantPoolIndex(CP->getIndex());
1147 }
1148 else if(Address.getOpcode() == ISD::FrameIndex) {
1149 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
Andrew Lenharth032f2352005-02-22 21:59:48 +00001150 BuildMI(BB, Alpha::LDS, 2, Tmp1)
1151 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1152 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001153 } else {
1154 long offset;
1155 SelectAddr(Address, Tmp2, offset);
1156 BuildMI(BB, Alpha::LDS, 1, Tmp1).addImm(offset).addReg(Tmp2);
1157 }
Andrew Lenharth29219162005-02-07 06:31:44 +00001158 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001159 return Result;
1160 }
Andrew Lenharth2c594352005-01-29 15:42:07 +00001161
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001162 case ISD::UINT_TO_FP:
Andrew Lenharth69520ed2005-05-26 18:18:34 +00001163 {
1164 //FIXME: First test if we will have problems with the sign bit before doing the slow thing
1165 assert (N.getOperand(0).getValueType() == MVT::i64
1166 && "only quads can be loaded from");
1167 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1168 Tmp2 = MakeReg(MVT::i64);
1169 BuildMI(BB, Alpha::SRL, 2, Tmp2).addReg(Tmp1).addImm(1);
1170 Tmp3 = MakeReg(MVT::i64);
1171 BuildMI(BB, Alpha::CMPLT, 2, Tmp3).addReg(Tmp1).addReg(Alpha::R31);
1172 unsigned Tmp4 = MakeReg(MVT::f64), Tmp5 = MakeReg(MVT::f64), Tmp6 = MakeReg(MVT::f64);
1173 MoveInt2FP(Tmp1, Tmp4, true);
1174 MoveInt2FP(Tmp2, Tmp5, true);
1175 MoveInt2FP(Tmp3, Tmp6, true);
1176 Tmp1 = MakeReg(MVT::f64);
1177 Tmp2 = MakeReg(MVT::f64);
1178 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
1179 BuildMI(BB, Opc, 1, Tmp1).addReg(Tmp4);
1180 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp5);
1181 Tmp3 = MakeReg(MVT::f64);
1182 BuildMI(BB, Alpha::ADDT, 2, Tmp3).addReg(Tmp2).addReg(Tmp2);
1183 //Ok, now tmp1 had the plain covereted
1184 //tmp3 has the reduced converted and added
1185 //tmp6 has the conditional to use
1186 BuildMI(BB, Alpha::FCMOVNE, 3, Result).addReg(Tmp1).addReg(Tmp3).addReg(Tmp6);
1187 return Result;
1188 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001189 case ISD::SINT_TO_FP:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001190 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001191 assert (N.getOperand(0).getValueType() == MVT::i64
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001192 && "only quads can be loaded from");
Andrew Lenharth40831c52005-01-28 06:57:18 +00001193 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001194 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001195 MoveInt2FP(Tmp1, Tmp2, true);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001196 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
1197 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001198 return Result;
1199 }
1200 }
1201 assert(0 && "should not get here");
1202 return 0;
1203}
1204
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001205unsigned ISel::SelectExpr(SDOperand N) {
1206 unsigned Result;
Andrew Lenharth2966e842005-04-07 18:15:28 +00001207 unsigned Tmp1, Tmp2 = 0, Tmp3;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001208 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001209 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001210
1211 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001212 MVT::ValueType DestType = N.getValueType();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001213
1214 unsigned &Reg = ExprMap[N];
1215 if (Reg) return Reg;
1216
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001217 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::TAILCALL)
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001218 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001219 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001220 else {
1221 // If this is a call instruction, make sure to prepare ALL of the result
1222 // values as well as the chain.
1223 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001224 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001225 else {
1226 Result = MakeReg(Node->getValueType(0));
1227 ExprMap[N.getValue(0)] = Result;
1228 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1229 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001230 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001231 }
1232 }
1233
Andrew Lenharth50d91d72005-04-30 14:19:13 +00001234 if ((DestType == MVT::f64 || DestType == MVT::f32 ||
1235 (
1236 (opcode == ISD::LOAD || opcode == ISD::CopyFromReg ||
1237 opcode == ISD::EXTLOAD) &&
1238 (N.getValue(0).getValueType() == MVT::f32 ||
1239 N.getValue(0).getValueType() == MVT::f64)
1240 ))
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001241 && opcode != ISD::CALL && opcode != ISD::TAILCALL
Andrew Lenharth06342c32005-02-07 06:21:37 +00001242 )
Andrew Lenharth40831c52005-01-28 06:57:18 +00001243 return SelectExprFP(N, Result);
1244
1245 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001246 default:
1247 Node->dump();
1248 assert(0 && "Node not handled!\n");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001249
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001250 case ISD::CTPOP:
1251 case ISD::CTTZ:
1252 case ISD::CTLZ:
1253 Opc = opcode == ISD::CTPOP ? Alpha::CTPOP :
1254 (opcode == ISD::CTTZ ? Alpha::CTTZ : Alpha::CTLZ);
1255 Tmp1 = SelectExpr(N.getOperand(0));
1256 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1257 return Result;
1258
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001259 case ISD::MULHU:
1260 Tmp1 = SelectExpr(N.getOperand(0));
1261 Tmp2 = SelectExpr(N.getOperand(1));
1262 BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth706be912005-04-07 13:55:53 +00001263 return Result;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001264 case ISD::MULHS:
1265 {
1266 //MULHU - Ra<63>*Rb - Rb<63>*Ra
1267 Tmp1 = SelectExpr(N.getOperand(0));
1268 Tmp2 = SelectExpr(N.getOperand(1));
1269 Tmp3 = MakeReg(MVT::i64);
1270 BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1271 unsigned V1 = MakeReg(MVT::i64);
1272 unsigned V2 = MakeReg(MVT::i64);
1273 BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31).addReg(Tmp1);
1274 BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31).addReg(Tmp2);
1275 unsigned IRes = MakeReg(MVT::i64);
1276 BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
1277 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
1278 return Result;
1279 }
Andrew Lenharth7332f3e2005-04-02 19:11:07 +00001280 case ISD::UNDEF: {
1281 BuildMI(BB, Alpha::IDEF, 0, Result);
1282 return Result;
1283 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001284
Andrew Lenharth032f2352005-02-22 21:59:48 +00001285 case ISD::DYNAMIC_STACKALLOC:
1286 // Generate both result values.
Andrew Lenharth3a7118d2005-02-23 17:33:42 +00001287 if (Result != notIn)
1288 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth032f2352005-02-22 21:59:48 +00001289 else
1290 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1291
1292 // FIXME: We are currently ignoring the requested alignment for handling
1293 // greater than the stack alignment. This will need to be revisited at some
1294 // point. Align = N.getOperand(2);
1295
1296 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1297 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1298 std::cerr << "Cannot allocate stack object with greater alignment than"
1299 << " the stack alignment yet!";
1300 abort();
1301 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001302
Andrew Lenharth032f2352005-02-22 21:59:48 +00001303 Select(N.getOperand(0));
1304 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1305 {
1306 if (CN->getValue() < 32000)
1307 {
1308 BuildMI(BB, Alpha::LDA, 2, Alpha::R30)
1309 .addImm(-CN->getValue()).addReg(Alpha::R30);
1310 } else {
1311 Tmp1 = SelectExpr(N.getOperand(1));
1312 // Subtract size from stack pointer, thereby allocating some space.
1313 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1314 }
1315 } else {
1316 Tmp1 = SelectExpr(N.getOperand(1));
1317 // Subtract size from stack pointer, thereby allocating some space.
1318 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1319 }
1320
1321 // Put a pointer to the space into the result register, by copying the stack
1322 // pointer.
Andrew Lenharth7bc47022005-02-22 23:29:25 +00001323 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
Andrew Lenharth032f2352005-02-22 21:59:48 +00001324 return Result;
1325
Andrew Lenharth33819132005-03-04 20:09:23 +00001326// case ISD::ConstantPool:
1327// Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1328// AlphaLowering.restoreGP(BB);
1329// BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(Tmp1);
1330// return Result;
Andrew Lenharth2c594352005-01-29 15:42:07 +00001331
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001332 case ISD::FrameIndex:
Andrew Lenharth032f2352005-02-22 21:59:48 +00001333 BuildMI(BB, Alpha::LDA, 2, Result)
1334 .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
1335 .addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001336 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001337
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001338 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001339 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001340 case ISD::SEXTLOAD:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001341 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001342 {
1343 // Make sure we generate both values.
1344 if (Result != notIn)
1345 ExprMap[N.getValue(1)] = notIn; // Generate the token
1346 else
1347 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001348
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001349 SDOperand Chain = N.getOperand(0);
1350 SDOperand Address = N.getOperand(1);
1351 Select(Chain);
1352
Misha Brukman4633f1c2005-04-21 23:13:11 +00001353 assert(Node->getValueType(0) == MVT::i64 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001354 "Unknown type to sign extend to.");
Andrew Lenharth03824012005-02-07 05:55:55 +00001355 if (opcode == ISD::LOAD)
1356 Opc = Alpha::LDQ;
1357 else
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001358 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
1359 default: Node->dump(); assert(0 && "Bad sign extend!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001360 case MVT::i32: Opc = Alpha::LDL;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001361 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001362 case MVT::i16: Opc = Alpha::LDWU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001363 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001364 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Misha Brukman4633f1c2005-04-21 23:13:11 +00001365 case MVT::i8: Opc = Alpha::LDBU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001366 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001367 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001368
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001369 if (Address.getOpcode() == ISD::GlobalAddress) {
1370 AlphaLowering.restoreGP(BB);
1371 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001372 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001373 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1374 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001375 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1376 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001377 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001378 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001379 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001380 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001381 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001382 BuildMI(BB, Opc, 2, Result)
1383 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1384 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001385 } else {
1386 long offset;
1387 SelectAddr(Address, Tmp1, offset);
1388 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1389 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001390 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001391 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001392
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001393 case ISD::GlobalAddress:
1394 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001395 has_sym = true;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001396 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
1397 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
1398 return Result;
1399
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001400 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001401 case ISD::CALL:
1402 {
1403 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001404
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001405 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001406 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001407
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001408 //grab the arguments
1409 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001410 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001411 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001412 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001413
Andrew Lenharth684f2292005-01-30 00:35:27 +00001414 //in reg args
1415 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001416 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001417 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001418 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +00001419 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001420 Alpha::F19, Alpha::F20, Alpha::F21};
1421 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001422 default:
1423 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001424 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001425 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001426 N.getOperand(i+2).getValueType() << "\n";
1427 assert(0 && "Unknown value type for call");
1428 case MVT::i1:
1429 case MVT::i8:
1430 case MVT::i16:
1431 case MVT::i32:
1432 case MVT::i64:
1433 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1434 break;
1435 case MVT::f32:
1436 case MVT::f64:
1437 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1438 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001439 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001440 }
Andrew Lenharth684f2292005-01-30 00:35:27 +00001441 //in mem args
1442 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001443 {
1444 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001445 default:
1446 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001447 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001448 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001449 N.getOperand(i+2).getValueType() << "\n";
1450 assert(0 && "Unknown value type for call");
1451 case MVT::i1:
1452 case MVT::i8:
1453 case MVT::i16:
1454 case MVT::i32:
1455 case MVT::i64:
1456 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1457 break;
1458 case MVT::f32:
1459 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1460 break;
1461 case MVT::f64:
1462 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1463 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001464 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001465 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001466 //build the right kind of call
1467 if (GlobalAddressSDNode *GASD =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001468 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001469 {
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001470 if (GASD->getGlobal()->isExternal()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001471 //use safe calling convention
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001472 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001473 has_sym = true;
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001474 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal());
1475 } else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001476 //use PC relative branch call
Andrew Lenharth1e0d9bd2005-04-14 17:34:20 +00001477 AlphaLowering.restoreGP(BB);
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001478 BuildMI(BB, Alpha::BSR, 1, Alpha::R26).addGlobalAddress(GASD->getGlobal(),true);
1479 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001480 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001481 else if (ExternalSymbolSDNode *ESSDN =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001482 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001483 {
1484 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001485 has_sym = true;
Andrew Lenharthba05ad62005-03-30 18:22:52 +00001486 BuildMI(BB, Alpha::CALL, 1).addExternalSymbol(ESSDN->getSymbol(), true);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001487 } else {
1488 //no need to restore GP as we are doing an indirect call
1489 Tmp1 = SelectExpr(N.getOperand(1));
1490 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
1491 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
1492 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001493
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001494 //push the result into a virtual register
Misha Brukman4633f1c2005-04-21 23:13:11 +00001495
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001496 switch (Node->getValueType(0)) {
1497 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001498 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001499 case MVT::i1:
1500 case MVT::i8:
1501 case MVT::i16:
1502 case MVT::i32:
1503 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001504 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
1505 break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001506 case MVT::f32:
1507 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001508 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
1509 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001510 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001511 return Result+N.ResNo;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001512 }
1513
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001514 case ISD::SIGN_EXTEND_INREG:
1515 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001516 //do SDIV opt for all levels of ints if not dividing by a constant
1517 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
1518 && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001519 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001520 unsigned Tmp4 = MakeReg(MVT::f64);
1521 unsigned Tmp5 = MakeReg(MVT::f64);
1522 unsigned Tmp6 = MakeReg(MVT::f64);
1523 unsigned Tmp7 = MakeReg(MVT::f64);
1524 unsigned Tmp8 = MakeReg(MVT::f64);
1525 unsigned Tmp9 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001526
1527 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1528 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1529 MoveInt2FP(Tmp1, Tmp4, true);
1530 MoveInt2FP(Tmp2, Tmp5, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001531 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Tmp4);
1532 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Tmp5);
1533 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
1534 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Tmp8);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001535 MoveFP2Int(Tmp9, Result, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001536 return Result;
1537 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001538
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001539 //Alpha has instructions for a bunch of signed 32 bit stuff
1540 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001541 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001542 switch (N.getOperand(0).getOpcode()) {
1543 case ISD::ADD:
1544 case ISD::SUB:
1545 case ISD::MUL:
1546 {
1547 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
1548 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
1549 //FIXME: first check for Scaled Adds and Subs!
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001550 ConstantSDNode* CSD = NULL;
1551 if(!isMul && N.getOperand(0).getOperand(0).getOpcode() == ISD::SHL &&
1552 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(0).getOperand(1))) &&
1553 (CSD->getValue() == 2 || CSD->getValue() == 3))
1554 {
1555 bool use4 = CSD->getValue() == 2;
1556 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1557 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1558 BuildMI(BB, isAdd?(use4?Alpha::S4ADDL:Alpha::S8ADDL):(use4?Alpha::S4SUBL:Alpha::S8SUBL),
1559 2,Result).addReg(Tmp1).addReg(Tmp2);
1560 }
1561 else if(isAdd && N.getOperand(0).getOperand(1).getOpcode() == ISD::SHL &&
1562 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1).getOperand(1))) &&
1563 (CSD->getValue() == 2 || CSD->getValue() == 3))
1564 {
1565 bool use4 = CSD->getValue() == 2;
1566 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1567 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1568 BuildMI(BB, use4?Alpha::S4ADDL:Alpha::S8ADDL, 2,Result).addReg(Tmp1).addReg(Tmp2);
1569 }
1570 else if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001571 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
1572 { //Normal imm add/sub
1573 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001574 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001575 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
1576 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001577 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001578 else
1579 { //Normal add/sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001580 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULL : Alpha::SUBL);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001581 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001582 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001583 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1584 }
1585 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001586 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001587 default: break; //Fall Though;
1588 }
1589 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001590 Tmp1 = SelectExpr(N.getOperand(0));
1591 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001592 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001593 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001594 {
1595 default:
1596 Node->dump();
1597 assert(0 && "Sign Extend InReg not there yet");
1598 break;
1599 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001600 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001601 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001602 break;
1603 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001604 case MVT::i16:
1605 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
1606 break;
1607 case MVT::i8:
1608 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
1609 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +00001610 case MVT::i1:
1611 Tmp2 = MakeReg(MVT::i64);
1612 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001613 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +00001614 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001615 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001616 return Result;
1617 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001618
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001619 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001620 {
1621 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1622 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1623 bool isConst1 = false;
1624 bool isConst2 = false;
1625 int dir;
Misha Brukman7847fca2005-04-22 17:54:37 +00001626
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001627 //Tmp1 = SelectExpr(N.getOperand(0));
1628 if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001629 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
1630 isConst1 = true;
1631 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001632 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1633 isConst2 = true;
1634
1635 switch (SetCC->getCondition()) {
1636 default: Node->dump(); assert(0 && "Unknown integer comparison!");
1637 case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001638 case ISD::SETLT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001639 Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001640 case ISD::SETLE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001641 Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001642 case ISD::SETGT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001643 Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001644 case ISD::SETGE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001645 Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001646 case ISD::SETULT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001647 Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001648 case ISD::SETUGT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001649 Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001650 case ISD::SETULE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001651 Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001652 case ISD::SETUGE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001653 Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001654 case ISD::SETNE: {//Handle this one special
1655 //std::cerr << "Alpha does not have a setne.\n";
1656 //abort();
1657 Tmp1 = SelectExpr(N.getOperand(0));
1658 Tmp2 = SelectExpr(N.getOperand(1));
1659 Tmp3 = MakeReg(MVT::i64);
1660 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001661 //Remeber we have the Inv for this CC
1662 CCInvMap[N] = Tmp3;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001663 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001664 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001665 return Result;
1666 }
1667 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001668 if (dir == 1) {
1669 Tmp1 = SelectExpr(N.getOperand(0));
1670 if (isConst2) {
1671 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1672 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1673 } else {
1674 Tmp2 = SelectExpr(N.getOperand(1));
1675 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1676 }
1677 } else if (dir == 2) {
1678 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001679 if (isConst1) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001680 Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1681 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1682 } else {
1683 Tmp2 = SelectExpr(N.getOperand(0));
1684 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1685 }
1686 } else { //dir == 0
1687 if (isConst1) {
1688 Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1689 Tmp2 = SelectExpr(N.getOperand(1));
1690 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
1691 } else if (isConst2) {
1692 Tmp1 = SelectExpr(N.getOperand(0));
1693 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1694 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
1695 } else {
1696 Tmp1 = SelectExpr(N.getOperand(0));
1697 Tmp2 = SelectExpr(N.getOperand(1));
1698 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1699 }
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001700 }
1701 } else {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001702 //do the comparison
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001703 Tmp1 = MakeReg(MVT::f64);
1704 bool inv = SelectFPSetCC(N, Tmp1);
1705
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001706 //now arrange for Result (int) to have a 1 or 0
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001707 Tmp2 = MakeReg(MVT::i64);
1708 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001709 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001710 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001711 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001712 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001713 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001714 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001715
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001716 case ISD::CopyFromReg:
1717 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001718 ++count_ins;
1719
Andrew Lenharth40831c52005-01-28 06:57:18 +00001720 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001721 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001722 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001723 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001724 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001725
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001726 SDOperand Chain = N.getOperand(0);
1727
1728 Select(Chain);
1729 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1730 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1731 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1732 return Result;
1733 }
1734
Misha Brukman4633f1c2005-04-21 23:13:11 +00001735 //Most of the plain arithmetic and logic share the same form, and the same
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001736 //constant immediate test
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001737 case ISD::XOR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001738 //Match Not
1739 if (N.getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001740 cast<ConstantSDNode>(N.getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001741 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001742 Tmp1 = SelectExpr(N.getOperand(0));
1743 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1744 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001745 }
1746 //Fall through
1747 case ISD::AND:
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001748 //handle zap
1749 if (opcode == ISD::AND && N.getOperand(1).getOpcode() == ISD::Constant)
1750 {
1751 uint64_t k = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1752 unsigned int build = 0;
1753 for(int i = 0; i < 8; ++i)
1754 {
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001755 if ((k & 0x00FF) == 0x00FF)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001756 build |= 1 << i;
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001757 else if ((k & 0x00FF) != 0)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001758 { build = 0; break; }
1759 k >>= 8;
1760 }
1761 if (build)
1762 {
1763 Tmp1 = SelectExpr(N.getOperand(0));
1764 BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1765 return Result;
1766 }
1767 }
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001768 case ISD::OR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001769 //Check operand(0) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001770 if (N.getOperand(0).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001771 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001772 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001773 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001774 switch(opcode) {
1775 case ISD::AND: Opc = Alpha::BIC; break;
1776 case ISD::OR: Opc = Alpha::ORNOT; break;
1777 case ISD::XOR: Opc = Alpha::EQV; break;
1778 }
1779 Tmp1 = SelectExpr(N.getOperand(1));
1780 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1781 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1782 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001783 }
1784 //Check operand(1) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001785 if (N.getOperand(1).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001786 N.getOperand(1).getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001787 cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001788 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001789 switch(opcode) {
1790 case ISD::AND: Opc = Alpha::BIC; break;
1791 case ISD::OR: Opc = Alpha::ORNOT; break;
1792 case ISD::XOR: Opc = Alpha::EQV; break;
1793 }
1794 Tmp1 = SelectExpr(N.getOperand(0));
1795 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1796 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1797 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001798 }
1799 //Fall through
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001800 case ISD::SHL:
1801 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001802 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001803 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001804 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1805 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001806 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001807 {
1808 switch(opcode) {
1809 case ISD::AND: Opc = Alpha::ANDi; break;
1810 case ISD::OR: Opc = Alpha::BISi; break;
1811 case ISD::XOR: Opc = Alpha::XORi; break;
1812 case ISD::SHL: Opc = Alpha::SLi; break;
1813 case ISD::SRL: Opc = Alpha::SRLi; break;
1814 case ISD::SRA: Opc = Alpha::SRAi; break;
1815 case ISD::MUL: Opc = Alpha::MULQi; break;
1816 };
1817 Tmp1 = SelectExpr(N.getOperand(0));
1818 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1819 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1820 } else {
1821 switch(opcode) {
1822 case ISD::AND: Opc = Alpha::AND; break;
1823 case ISD::OR: Opc = Alpha::BIS; break;
1824 case ISD::XOR: Opc = Alpha::XOR; break;
1825 case ISD::SHL: Opc = Alpha::SL; break;
1826 case ISD::SRL: Opc = Alpha::SRL; break;
1827 case ISD::SRA: Opc = Alpha::SRA; break;
1828 case ISD::MUL: Opc = Alpha::MULQ; break;
1829 };
1830 Tmp1 = SelectExpr(N.getOperand(0));
1831 Tmp2 = SelectExpr(N.getOperand(1));
1832 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1833 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001834 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001835
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001836 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001837 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001838 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001839 bool isAdd = opcode == ISD::ADD;
1840
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001841 //first check for Scaled Adds and Subs!
1842 //Valid for add and sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001843 ConstantSDNode* CSD = NULL;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001844 if(N.getOperand(0).getOpcode() == ISD::SHL &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001845 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
1846 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001847 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001848 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001849 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001850 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) && CSD->getValue() <= 255)
1851 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1852 2, Result).addReg(Tmp2).addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001853 else {
1854 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001855 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1856 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001857 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001858 }
1859 //Position prevents subs
Andrew Lenharth273a1f92005-04-07 14:18:13 +00001860 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001861 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) &&
1862 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001863 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001864 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001865 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001866 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(0))) && CSD->getValue() <= 255)
1867 BuildMI(BB, use4?Alpha::S4ADDQi:Alpha::S8ADDQi, 2, Result).addReg(Tmp2)
1868 .addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001869 else {
1870 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001871 BuildMI(BB, use4?Alpha::S4ADDQ:Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001872 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001873 }
1874 //small addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001875 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
1876 CSD->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001877 { //Normal imm add/sub
1878 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1879 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001880 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CSD->getValue());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001881 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001882 //larger addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001883 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
1884 CSD->getSignExtended() <= 32767 &&
1885 CSD->getSignExtended() >= -32767)
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001886 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001887 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001888 Tmp2 = (long)CSD->getSignExtended();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001889 if (!isAdd)
1890 Tmp2 = -Tmp2;
1891 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001892 }
1893 //give up and do the operation
1894 else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001895 //Normal add/sub
1896 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1897 Tmp1 = SelectExpr(N.getOperand(0));
1898 Tmp2 = SelectExpr(N.getOperand(1));
1899 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1900 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001901 return Result;
1902 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001903
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001904 case ISD::SDIV:
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001905 {
Andrew Lenhartha565c272005-04-06 22:03:13 +00001906 ConstantSDNode* CSD;
1907 //check if we can convert into a shift!
1908 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
1909 (int64_t)CSD->getSignExtended() != 0 &&
1910 ExactLog2(abs((int64_t)CSD->getSignExtended())) != 0)
1911 {
1912 unsigned k = ExactLog2(abs(CSD->getSignExtended()));
1913 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenhartha565c272005-04-06 22:03:13 +00001914 if (k == 1)
1915 Tmp2 = Tmp1;
1916 else
1917 {
1918 Tmp2 = MakeReg(MVT::i64);
1919 BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
1920 }
1921 Tmp3 = MakeReg(MVT::i64);
1922 BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
1923 unsigned Tmp4 = MakeReg(MVT::i64);
1924 BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
1925 if ((int64_t)CSD->getSignExtended() > 0)
1926 BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
1927 else
1928 {
1929 unsigned Tmp5 = MakeReg(MVT::i64);
1930 BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
1931 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
1932 }
1933 return Result;
1934 }
1935 }
1936 //Else fall through
1937
1938 case ISD::UDIV:
1939 {
1940 ConstantSDNode* CSD;
1941 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
1942 ((int64_t)CSD->getSignExtended() >= 2 ||
1943 (int64_t)CSD->getSignExtended() <= -2))
1944 {
1945 // If this is a divide by constant, we can emit code using some magic
1946 // constants to implement it as a multiply instead.
1947 ExprMap.erase(N);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001948 if (opcode == ISD::SDIV)
Andrew Lenhartha565c272005-04-06 22:03:13 +00001949 return SelectExpr(BuildSDIVSequence(N));
1950 else
1951 return SelectExpr(BuildUDIVSequence(N));
1952 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001953 }
1954 //else fall though
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001955 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +00001956 case ISD::SREM:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001957 //FIXME: alpha really doesn't support any of these operations,
Andrew Lenharth40831c52005-01-28 06:57:18 +00001958 // the ops are expanded into special library calls with
1959 // special calling conventions
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001960 //Restore GP because it is a call after all...
Andrew Lenharth40831c52005-01-28 06:57:18 +00001961 switch(opcode) {
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001962 case ISD::UREM: Opc = Alpha::REMQU; break;
1963 case ISD::SREM: Opc = Alpha::REMQ; break;
1964 case ISD::UDIV: Opc = Alpha::DIVQU; break;
1965 case ISD::SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001966 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001967 Tmp1 = SelectExpr(N.getOperand(0));
1968 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth33819132005-03-04 20:09:23 +00001969 //set up regs explicitly (helps Reg alloc)
1970 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001971 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001972 AlphaLowering.restoreGP(BB);
Andrew Lenharth33819132005-03-04 20:09:23 +00001973 BuildMI(BB, Opc, 2).addReg(Alpha::R24).addReg(Alpha::R25);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001974 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001975 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001976
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001977 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001978 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001979 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001980 assert (DestType == MVT::i64 && "only quads can be loaded to");
1981 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00001982 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001983 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001984 if (SrcType == MVT::f32)
Misha Brukman7847fca2005-04-22 17:54:37 +00001985 {
1986 Tmp2 = MakeReg(MVT::f64);
1987 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
1988 Tmp1 = Tmp2;
1989 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001990 Tmp2 = MakeReg(MVT::f64);
1991 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001992 MoveFP2Int(Tmp2, Result, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001993
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001994 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001995 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001996
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001997 case ISD::SELECT:
1998 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001999 //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 +00002000 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002001 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2002 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002003 // Get the condition into the zero flag.
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002004 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002005
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002006 SDOperand CC = N.getOperand(0);
2007 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
2008
Misha Brukman4633f1c2005-04-21 23:13:11 +00002009 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002010 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
2011 { //FP Setcc -> Int Select
Misha Brukman7847fca2005-04-22 17:54:37 +00002012 Tmp1 = MakeReg(MVT::f64);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002013 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2014 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Misha Brukman7847fca2005-04-22 17:54:37 +00002015 bool inv = SelectFPSetCC(CC, Tmp1);
2016 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
2017 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
2018 return Result;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002019 }
2020 if (CC.getOpcode() == ISD::SETCC) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002021 //Int SetCC -> Select
2022 //Dropping the CC is only useful if we are comparing to 0
2023 if((SetCC->getOperand(1).getOpcode() == ISD::Constant &&
2024 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0) ||
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002025 (SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2026 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0))
2027 {
2028 //figure out a few things
2029 bool LeftZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2030 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
2031 bool RightZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2032 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
2033 bool LeftConst = N.getOperand(1).getOpcode() == ISD::Constant &&
2034 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255;
2035 bool RightConst = N.getOperand(2).getOpcode() == ISD::Constant &&
2036 cast<ConstantSDNode>(N.getOperand(2))->getValue() <= 255;
2037 bool useImm = LeftConst || RightConst;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002038
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002039 //Fix up CC
2040 ISD::CondCode cCode= SetCC->getCondition();
2041 if (RightConst && !LeftConst) //Invert sense to get Imm field right
2042 cCode = ISD::getSetCCInverse(cCode, true);
2043 if (LeftZero && !RightZero) //Swap Operands
2044 cCode = ISD::getSetCCSwappedOperands(cCode);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002045
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002046 //Choose the CMOV
2047 switch (cCode) {
2048 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
2049 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
2050 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
2051 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
2052 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
2053 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
2054 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
2055 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2056 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break; //Technically you could have this CC
2057 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
2058 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2059 }
2060 if(LeftZero && !RightZero) //swap Operands
2061 Tmp1 = SelectExpr(SetCC->getOperand(1)); //Cond
2062 else
2063 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
2064
2065 if (LeftConst) {
2066 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2067 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
Misha Brukman7847fca2005-04-22 17:54:37 +00002068 .addImm(cast<ConstantSDNode>(N.getOperand(1))->getValue())
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002069 .addReg(Tmp1);
2070 } else if (RightConst) {
2071 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
2072 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
Misha Brukman7847fca2005-04-22 17:54:37 +00002073 .addImm(cast<ConstantSDNode>(N.getOperand(2))->getValue())
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002074 .addReg(Tmp1);
2075 } else {
2076 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2077 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2078 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
2079 }
2080 return Result;
2081 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002082 //Otherwise, fall though
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002083 }
2084 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002085 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2086 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthe76797c2005-02-01 20:40:27 +00002087 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002088
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002089 return Result;
2090 }
2091
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002092 case ISD::Constant:
2093 {
Andrew Lenharthc0513832005-03-29 19:24:04 +00002094 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002095 if (val <= IMM_HIGH && val >= IMM_LOW) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002096 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002097 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002098 else if (val <= (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT &&
2099 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
2100 Tmp1 = MakeReg(MVT::i64);
2101 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val)).addReg(Alpha::R31);
2102 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002103 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002104 else {
2105 MachineConstantPool *CP = BB->getParent()->getConstantPool();
2106 ConstantUInt *C = ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
2107 unsigned CPI = CP->getConstantPoolIndex(C);
2108 AlphaLowering.restoreGP(BB);
2109 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CPI);
2110 }
2111 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002112 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002113 }
2114
2115 return 0;
2116}
2117
2118void ISel::Select(SDOperand N) {
2119 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00002120 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002121
Nate Begeman85fdeb22005-03-24 04:39:54 +00002122 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002123 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002124
2125 SDNode *Node = N.Val;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002126
Andrew Lenharth760270d2005-02-07 23:02:23 +00002127 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002128
2129 default:
2130 Node->dump(); std::cerr << "\n";
2131 assert(0 && "Node not handled yet!");
2132
2133 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00002134 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002135 return;
2136 }
2137
2138 case ISD::BR: {
2139 MachineBasicBlock *Dest =
2140 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2141
2142 Select(N.getOperand(0));
2143 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
2144 return;
2145 }
2146
2147 case ISD::ImplicitDef:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002148 ++count_ins;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002149 Select(N.getOperand(0));
2150 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
2151 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002152
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002153 case ISD::EntryToken: return; // Noop
2154
2155 case ISD::TokenFactor:
2156 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2157 Select(Node->getOperand(i));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002158
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002159 //N.Val->dump(); std::cerr << "\n";
2160 //assert(0 && "Node not handled yet!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00002161
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002162 return;
2163
2164 case ISD::CopyToReg:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002165 ++count_outs;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002166 Select(N.getOperand(0));
2167 Tmp1 = SelectExpr(N.getOperand(1));
2168 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002169
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002170 if (Tmp1 != Tmp2) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002171 if (N.getOperand(1).getValueType() == MVT::f64 ||
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002172 N.getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00002173 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2174 else
2175 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002176 }
2177 return;
2178
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002179 case ISD::RET:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002180 ++count_outs;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002181 switch (N.getNumOperands()) {
2182 default:
2183 std::cerr << N.getNumOperands() << "\n";
2184 for (unsigned i = 0; i < N.getNumOperands(); ++i)
2185 std::cerr << N.getOperand(i).getValueType() << "\n";
2186 Node->dump();
2187 assert(0 && "Unknown return instruction!");
2188 case 2:
2189 Select(N.getOperand(0));
2190 Tmp1 = SelectExpr(N.getOperand(1));
2191 switch (N.getOperand(1).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002192 default: Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002193 assert(0 && "All other types should have been promoted!!");
2194 case MVT::f64:
2195 case MVT::f32:
2196 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
2197 break;
2198 case MVT::i32:
2199 case MVT::i64:
2200 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
2201 break;
2202 }
2203 break;
2204 case 1:
2205 Select(N.getOperand(0));
2206 break;
2207 }
2208 //Tmp2 = AlphaLowering.getRetAddr();
2209 //BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(Tmp2).addReg(Tmp2);
2210 BuildMI(BB, Alpha::RETURN, 0); // Just emit a 'ret' instruction
2211 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002212
Misha Brukman4633f1c2005-04-21 23:13:11 +00002213 case ISD::TRUNCSTORE:
2214 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002215 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002216 SDOperand Chain = N.getOperand(0);
2217 SDOperand Value = N.getOperand(1);
2218 SDOperand Address = N.getOperand(2);
2219 Select(Chain);
2220
2221 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00002222
2223 if (opcode == ISD::STORE) {
2224 switch(Value.getValueType()) {
2225 default: assert(0 && "unknown Type in store");
2226 case MVT::i64: Opc = Alpha::STQ; break;
2227 case MVT::f64: Opc = Alpha::STT; break;
2228 case MVT::f32: Opc = Alpha::STS; break;
2229 }
2230 } else { //ISD::TRUNCSTORE
2231 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2232 default: assert(0 && "unknown Type in store");
2233 case MVT::i1: //FIXME: DAG does not promote this load
2234 case MVT::i8: Opc = Alpha::STB; break;
2235 case MVT::i16: Opc = Alpha::STW; break;
2236 case MVT::i32: Opc = Alpha::STL; break;
2237 }
Andrew Lenharth65838902005-02-06 16:22:15 +00002238 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00002239
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002240 if (Address.getOpcode() == ISD::GlobalAddress)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002241 {
2242 AlphaLowering.restoreGP(BB);
2243 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002244 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002245 BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
2246 }
Andrew Lenharth05380342005-02-07 05:07:00 +00002247 else if(Address.getOpcode() == ISD::FrameIndex)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002248 {
Andrew Lenharth032f2352005-02-22 21:59:48 +00002249 BuildMI(BB, Opc, 3).addReg(Tmp1)
2250 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
2251 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002252 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002253 else
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002254 {
2255 long offset;
2256 SelectAddr(Address, Tmp2, offset);
2257 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2258 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002259 return;
2260 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002261
2262 case ISD::EXTLOAD:
2263 case ISD::SEXTLOAD:
2264 case ISD::ZEXTLOAD:
2265 case ISD::LOAD:
2266 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002267 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002268 case ISD::CALL:
Andrew Lenharth032f2352005-02-22 21:59:48 +00002269 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002270 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002271 SelectExpr(N);
2272 return;
2273
Chris Lattner16cd04d2005-05-12 23:24:06 +00002274 case ISD::CALLSEQ_START:
2275 case ISD::CALLSEQ_END:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002276 Select(N.getOperand(0));
2277 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002278
Chris Lattner16cd04d2005-05-12 23:24:06 +00002279 Opc = N.getOpcode() == ISD::CALLSEQ_START ? Alpha::ADJUSTSTACKDOWN :
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002280 Alpha::ADJUSTSTACKUP;
2281 BuildMI(BB, Opc, 1).addImm(Tmp1);
2282 return;
Andrew Lenharth95762122005-03-31 21:24:06 +00002283
2284 case ISD::PCMARKER:
2285 Select(N.getOperand(0)); //Chain
2286 BuildMI(BB, Alpha::PCLABEL, 2).addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
2287 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002288 }
2289 assert(0 && "Should not be reached!");
2290}
2291
2292
2293/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
2294/// into a machine code representation using pattern matching and a machine
2295/// description file.
2296///
2297FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002298 return new ISel(TM);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002299}
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002300