blob: fbd7bb1911ef93d0912a8ae2ebd59f72a1c19220 [file] [log] [blame]
Misha Brukman2a8350a2005-02-05 02:24:26 +00001//===- AlphaISelPattern.cpp - A pattern matching inst selector for Alpha --===//
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for Alpha.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Alpha.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000015#include "AlphaRegisterInfo.h"
16#include "llvm/Constants.h" // FIXME: REMOVE
17#include "llvm/Function.h"
18#include "llvm/CodeGen/MachineInstrBuilder.h"
19#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
20#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SelectionDAGISel.h"
24#include "llvm/CodeGen/SSARegMap.h"
25#include "llvm/Target/TargetData.h"
26#include "llvm/Target/TargetLowering.h"
27#include "llvm/Support/MathExtras.h"
28#include "llvm/ADT/Statistic.h"
Andrew Lenharth032f2352005-02-22 21:59:48 +000029#include "llvm/Support/Debug.h"
Andrew Lenharth95762122005-03-31 21:24:06 +000030#include "llvm/Support/CommandLine.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000031#include <set>
Andrew Lenharth684f2292005-01-30 00:35:27 +000032#include <algorithm>
Andrew Lenharth304d0f32005-01-22 23:41:55 +000033using namespace llvm;
34
Andrew Lenharth95762122005-03-31 21:24:06 +000035namespace llvm {
36 cl::opt<bool> EnableAlphaIDIV("enable-alpha-intfpdiv",
37 cl::desc("Use the FP div instruction for integer div when possible"),
38 cl::Hidden);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +000039 cl::opt<bool> EnableAlphaFTOI("enable-alpha-ftoi",
Andrew Lenharth5e99dd92005-03-31 22:02:25 +000040 cl::desc("Enable use of ftoi* and itof* instructions (ev6 and higher)"),
Andrew Lenharth95762122005-03-31 21:24:06 +000041 cl::Hidden);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +000042 cl::opt<bool> EnableAlphaCount("enable-alpha-count",
43 cl::desc("Print estimates on live ins and outs"),
44 cl::Hidden);
Andrew Lenharth95762122005-03-31 21:24:06 +000045}
46
Andrew Lenharth304d0f32005-01-22 23:41:55 +000047//===----------------------------------------------------------------------===//
48// AlphaTargetLowering - Alpha Implementation of the TargetLowering interface
49namespace {
50 class AlphaTargetLowering : public TargetLowering {
51 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
52 unsigned GP; //GOT vreg
53 public:
54 AlphaTargetLowering(TargetMachine &TM) : TargetLowering(TM) {
55 // Set up the TargetLowering object.
Andrew Lenharth3d65d312005-01-27 03:49:45 +000056 //I am having problems with shr n ubyte 1
Andrew Lenharth879ef222005-02-02 17:00:21 +000057 setShiftAmountType(MVT::i64);
58 setSetCCResultType(MVT::i64);
Andrew Lenharthd3355e22005-04-07 20:11:32 +000059 setSetCCResultContents(ZeroOrOneSetCCResult);
Andrew Lenharth3d65d312005-01-27 03:49:45 +000060
Andrew Lenharth304d0f32005-01-22 23:41:55 +000061 addRegisterClass(MVT::i64, Alpha::GPRCRegisterClass);
62 addRegisterClass(MVT::f64, Alpha::FPRCRegisterClass);
Andrew Lenharth3d65d312005-01-27 03:49:45 +000063 addRegisterClass(MVT::f32, Alpha::FPRCRegisterClass);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000064
Chris Lattnerda4d4692005-04-09 03:22:37 +000065 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000066 setOperationAction(ISD::EXTLOAD , MVT::i1 , Promote);
67 setOperationAction(ISD::EXTLOAD , MVT::f32 , Promote);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000068
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000069 setOperationAction(ISD::ZEXTLOAD , MVT::i1 , Expand);
70 setOperationAction(ISD::ZEXTLOAD , MVT::i32 , Expand);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000071
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000072 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
73 setOperationAction(ISD::SEXTLOAD , MVT::i8 , Expand);
74 setOperationAction(ISD::SEXTLOAD , MVT::i16 , Expand);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000075
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000076 setOperationAction(ISD::SREM , MVT::f32 , Expand);
77 setOperationAction(ISD::SREM , MVT::f64 , Expand);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +000078
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000079 //If this didn't legalize into a div....
80 // setOperationAction(ISD::SREM , MVT::i64, Expand);
81 // setOperationAction(ISD::UREM , MVT::i64, Expand);
82
83 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
84 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
85 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
Andrew Lenharth9818c052005-02-05 13:19:12 +000086
Andrew Lenharth33819132005-03-04 20:09:23 +000087 //Doesn't work yet
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000088 setOperationAction(ISD::SETCC , MVT::f32, Promote);
Andrew Lenharth572af902005-02-14 05:41:43 +000089
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000090 computeRegisterProperties();
Andrew Lenharth304d0f32005-01-22 23:41:55 +000091
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000092 addLegalFPImmediate(+0.0); //F31
93 addLegalFPImmediate(-0.0); //-F31
Andrew Lenharth304d0f32005-01-22 23:41:55 +000094 }
95
96 /// LowerArguments - This hook must be implemented to indicate how we should
97 /// lower the arguments for the specified function, into the specified DAG.
98 virtual std::vector<SDOperand>
99 LowerArguments(Function &F, SelectionDAG &DAG);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000100
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000101 /// LowerCallTo - This hook lowers an abstract call to a function into an
102 /// actual call.
103 virtual std::pair<SDOperand, SDOperand>
Nate Begeman8e21e712005-03-26 01:29:23 +0000104 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
105 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000106
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000107 virtual std::pair<SDOperand, SDOperand>
108 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000109
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000110 virtual std::pair<SDOperand,SDOperand>
111 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
112 const Type *ArgTy, SelectionDAG &DAG);
113
114 virtual std::pair<SDOperand, SDOperand>
115 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
116 SelectionDAG &DAG);
117
118 void restoreGP(MachineBasicBlock* BB)
119 {
120 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
121 }
122 };
123}
124
125//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
126
127//For now, just use variable size stack frame format
128
129//In a standard call, the first six items are passed in registers $16
130//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
131//of argument-to-register correspondence.) The remaining items are
132//collected in a memory argument list that is a naturally aligned
133//array of quadwords. In a standard call, this list, if present, must
134//be passed at 0(SP).
135//7 ... n 0(SP) ... (n-7)*8(SP)
136
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000137// //#define FP $15
138// //#define RA $26
139// //#define PV $27
140// //#define GP $29
141// //#define SP $30
142
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000143std::vector<SDOperand>
144AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
145{
146 std::vector<SDOperand> ArgValues;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000147 std::vector<SDOperand> LS;
148 SDOperand Chain = DAG.getRoot();
149
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000150 // assert(0 && "TODO");
151 MachineFunction &MF = DAG.getMachineFunction();
Andrew Lenharth05380342005-02-07 05:07:00 +0000152 MachineFrameInfo*MFI = MF.getFrameInfo();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000153
154 GP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
155 MachineBasicBlock& BB = MF.front();
156
157 //Handle the return address
158 //BuildMI(&BB, Alpha::IDEF, 0, Alpha::R26);
159
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000160 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
161 Alpha::R19, Alpha::R20, Alpha::R21};
162 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
163 Alpha::F19, Alpha::F20, Alpha::F21};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000164 int count = 0;
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000165
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000166 //Def incoming registers
167 {
168 Function::arg_iterator I = F.arg_begin();
169 Function::arg_iterator E = F.arg_end();
170 for (int i = 0; i < 6; ++i)
171 {
172 if (F.isVarArg()) {
Andrew Lenharth0b04b5d2005-04-12 17:47:57 +0000173 MF.addLiveIn(args_int[i]);
174 MF.addLiveIn(args_float[i]);
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000175// BuildMI(&BB, Alpha::IDEF, 0, args_int[i]);
176// BuildMI(&BB, Alpha::IDEF, 0, args_float[i]);
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000177 } else if (I != E)
178 {
179 if(MVT::isInteger(getValueType(I->getType())))
Andrew Lenharth0b04b5d2005-04-12 17:47:57 +0000180 MF.addLiveIn(args_int[i]);
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000181// BuildMI(&BB, Alpha::IDEF, 0, args_int[i]);
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000182 else
Andrew Lenharth0b04b5d2005-04-12 17:47:57 +0000183 MF.addLiveIn(args_float[i]);
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000184// BuildMI(&BB, Alpha::IDEF, 0, args_float[i]);
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000185 ++I;
186 }
187 }
188 }
189
Andrew Lenharth0b04b5d2005-04-12 17:47:57 +0000190 MF.addLiveIn(Alpha::R29);
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000191// BuildMI(&BB, Alpha::IDEF, 0, Alpha::R29);
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000192 BuildMI(&BB, Alpha::BIS, 2, GP).addReg(Alpha::R29).addReg(Alpha::R29);
193
Chris Lattnere4d5c442005-03-15 04:54:21 +0000194 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000195 {
196 SDOperand newroot, argt;
197 if (count < 6) {
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000198 unsigned Vreg;
199 MVT::ValueType VT = getValueType(I->getType());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000200 switch (getValueType(I->getType())) {
201 default:
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000202 std::cerr << "Unknown Type " << VT << "\n";
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000203 abort();
204 case MVT::f64:
205 case MVT::f32:
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000206 Vreg = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(VT));
207 BuildMI(&BB, Alpha::CPYS, 2, Vreg).addReg(args_float[count]).addReg(args_float[count]);
208 argt = newroot = DAG.getCopyFromReg(Vreg,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000209 getValueType(I->getType()),
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000210 Chain);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000211 break;
212 case MVT::i1:
213 case MVT::i8:
214 case MVT::i16:
215 case MVT::i32:
216 case MVT::i64:
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000217 Vreg = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
218 BuildMI(&BB, Alpha::BIS, 2, Vreg).addReg(args_int[count]).addReg(args_int[count]);
219 argt = newroot = DAG.getCopyFromReg(Vreg, MVT::i64, Chain);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000220 if (getValueType(I->getType()) != MVT::i64)
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000221 argt = DAG.getNode(ISD::TRUNCATE, getValueType(I->getType()), newroot);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000222 break;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000223 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000224 } else { //more args
225 // Create the frame index object for this incoming parameter...
226 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
227
228 // Create the SelectionDAG nodes corresponding to a load
229 //from this parameter
230 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
231 argt = newroot = DAG.getLoad(getValueType(I->getType()),
232 DAG.getEntryNode(), FIN);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000233 }
Andrew Lenharth032f2352005-02-22 21:59:48 +0000234 ++count;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000235 LS.push_back(newroot.getValue(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000236 ArgValues.push_back(argt);
237 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000238
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000239 // If the functions takes variable number of arguments, copy all regs to stack
240 if (F.isVarArg())
241 for (int i = 0; i < 6; ++i)
242 {
243 unsigned Vreg = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
244 BuildMI(&BB, Alpha::BIS, 2, Vreg).addReg(args_int[i]).addReg(args_int[i]);
245 SDOperand argt = DAG.getCopyFromReg(Vreg, MVT::i64, Chain);
246 int FI = MFI->CreateFixedObject(8, -8 * (6 - i));
247 SDOperand SDFI = DAG.getFrameIndex(FI, MVT::i64);
248 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, argt, SDFI));
249
250 Vreg = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::f64));
251 BuildMI(&BB, Alpha::CPYS, 2, Vreg).addReg(args_float[i]).addReg(args_float[i]);
252 argt = DAG.getCopyFromReg(Vreg, MVT::f64, Chain);
253 FI = MFI->CreateFixedObject(8, - 8 * (12 - i));
254 SDFI = DAG.getFrameIndex(FI, MVT::i64);
255 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, argt, SDFI));
256 }
257
258 // If the function takes variable number of arguments, make a frame index for
259 // the start of the first arg value... for expansion of llvm.va_start.
260 // if (F.isVarArg())
261 // VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
262
263 //Set up a token factor with all the stack traffic
264 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, LS));
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000265
266 // Finally, inform the code generator which regs we return values in.
267 switch (getValueType(F.getReturnType())) {
268 default: assert(0 && "Unknown type!");
269 case MVT::isVoid: break;
270 case MVT::i1:
271 case MVT::i8:
272 case MVT::i16:
273 case MVT::i32:
274 case MVT::i64:
275 MF.addLiveOut(Alpha::R0);
276 break;
277 case MVT::f32:
278 case MVT::f64:
279 MF.addLiveOut(Alpha::F0);
280 break;
281 }
282
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000283 //return the arguments
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000284 return ArgValues;
285}
286
287std::pair<SDOperand, SDOperand>
288AlphaTargetLowering::LowerCallTo(SDOperand Chain,
Nate Begeman8e21e712005-03-26 01:29:23 +0000289 const Type *RetTy, bool isVarArg,
290 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000291 int NumBytes = 0;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000292 if (Args.size() > 6)
293 NumBytes = (Args.size() - 6) * 8;
294
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000295 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
296 DAG.getConstant(NumBytes, getPointerTy()));
297 std::vector<SDOperand> args_to_use;
298 for (unsigned i = 0, e = Args.size(); i != e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000299 {
300 switch (getValueType(Args[i].second)) {
301 default: assert(0 && "Unexpected ValueType for argument!");
302 case MVT::i1:
303 case MVT::i8:
304 case MVT::i16:
305 case MVT::i32:
306 // Promote the integer to 64 bits. If the input type is signed use a
307 // sign extend, otherwise use a zero extend.
308 if (Args[i].second->isSigned())
309 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
310 else
311 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
312 break;
313 case MVT::i64:
314 case MVT::f64:
315 case MVT::f32:
316 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000317 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000318 args_to_use.push_back(Args[i].first);
319 }
Andrew Lenharth40831c52005-01-28 06:57:18 +0000320
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000321 std::vector<MVT::ValueType> RetVals;
322 MVT::ValueType RetTyVT = getValueType(RetTy);
323 if (RetTyVT != MVT::isVoid)
324 RetVals.push_back(RetTyVT);
325 RetVals.push_back(MVT::Other);
326
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000327 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
328 Chain, Callee, args_to_use), 0);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000329 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
330 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
331 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000332 return std::make_pair(TheCall, Chain);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000333}
334
335std::pair<SDOperand, SDOperand>
336AlphaTargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
337 //vastart just returns the address of the VarArgsFrameIndex slot.
338 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64), Chain);
339}
340
341std::pair<SDOperand,SDOperand> AlphaTargetLowering::
342LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000343 const Type *ArgTy, SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000344 abort();
345}
346
347
348std::pair<SDOperand, SDOperand> AlphaTargetLowering::
349LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
350 SelectionDAG &DAG) {
351 abort();
352}
353
354
355
356
357
358namespace {
359
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000360//===--------------------------------------------------------------------===//
361/// ISel - Alpha specific code to select Alpha machine instructions for
362/// SelectionDAG operations.
363//===--------------------------------------------------------------------===//
364class ISel : public SelectionDAGISel {
365
366 /// AlphaLowering - This object fully describes how to lower LLVM code to an
367 /// Alpha-specific SelectionDAG.
368 AlphaTargetLowering AlphaLowering;
369
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000370 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
371 // for sdiv and udiv until it is put into the future
372 // dag combiner.
373
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000374 /// ExprMap - As shared expressions are codegen'd, we keep track of which
375 /// vreg the value is produced in, so we only emit one copy of each compiled
376 /// tree.
377 static const unsigned notIn = (unsigned)(-1);
378 std::map<SDOperand, unsigned> ExprMap;
379
380 //CCInvMap sometimes (SetNE) we have the inverse CC code for free
381 std::map<SDOperand, unsigned> CCInvMap;
382
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000383 int count_ins;
384 int count_outs;
385 bool has_sym;
386
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000387public:
388 ISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering), AlphaLowering(TM)
389 {}
390
391 /// InstructionSelectBasicBlock - This callback is invoked by
392 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
393 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
Andrew Lenharth032f2352005-02-22 21:59:48 +0000394 DEBUG(BB->dump());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000395 count_ins = 0;
396 count_outs = 0;
397 has_sym = false;
398
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000399 // Codegen the basic block.
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000400 ISelDAG = &DAG;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000401 Select(DAG.getRoot());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000402
403 if(has_sym)
404 ++count_ins;
405 if(EnableAlphaCount)
406 std::cerr << "COUNT: " << BB->getParent()->getFunction ()->getName() << " "
407 << BB->getNumber() << " "
408 << count_ins << " "
409 << count_outs << "\n";
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000410
411 // Clear state used for selection.
412 ExprMap.clear();
413 CCInvMap.clear();
414 }
415
416 unsigned SelectExpr(SDOperand N);
417 unsigned SelectExprFP(SDOperand N, unsigned Result);
418 void Select(SDOperand N);
419
420 void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
421 void SelectBranchCC(SDOperand N);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000422 void MoveFP2Int(unsigned src, unsigned dst, bool isDouble);
423 void MoveInt2FP(unsigned src, unsigned dst, bool isDouble);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000424 //returns whether the sense of the comparison was inverted
425 bool SelectFPSetCC(SDOperand N, unsigned dst);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000426
427 // dag -> dag expanders for integer divide by constant
428 SDOperand BuildSDIVSequence(SDOperand N);
429 SDOperand BuildUDIVSequence(SDOperand N);
430
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000431};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000432}
433
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000434//Factorize a number using the list of constants
435static bool factorize(int v[], int res[], int size, uint64_t c)
436{
437 bool cont = true;
438 while (c != 1 && cont)
439 {
440 cont = false;
441 for(int i = 0; i < size; ++i)
442 {
443 if (c % v[i] == 0)
444 {
445 c /= v[i];
446 ++res[i];
447 cont=true;
448 }
449 }
450 }
451 return c == 1;
452}
453
454
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000455//Shamelessly adapted from PPC32
456// Structure used to return the necessary information to codegen an SDIV as
457// a multiply.
458struct ms {
459 int64_t m; // magic number
460 int64_t s; // shift amount
461};
462
463struct mu {
464 uint64_t m; // magic number
465 int64_t a; // add indicator
466 int64_t s; // shift amount
467};
468
469/// magic - calculate the magic numbers required to codegen an integer sdiv as
470/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
471/// or -1.
472static struct ms magic(int64_t d) {
473 int64_t p;
474 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
475 const uint64_t two63 = 9223372036854775808ULL; // 2^63
476 struct ms mag;
477
478 ad = abs(d);
479 t = two63 + ((uint64_t)d >> 63);
480 anc = t - 1 - t%ad; // absolute value of nc
Andrew Lenharth320174f2005-04-07 17:19:16 +0000481 p = 63; // initialize p
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000482 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
483 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
484 q2 = two63/ad; // initialize q2 = 2p/abs(d)
485 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
486 do {
487 p = p + 1;
488 q1 = 2*q1; // update q1 = 2p/abs(nc)
489 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
490 if (r1 >= anc) { // must be unsigned comparison
491 q1 = q1 + 1;
492 r1 = r1 - anc;
493 }
494 q2 = 2*q2; // update q2 = 2p/abs(d)
495 r2 = 2*r2; // update r2 = rem(2p/abs(d))
496 if (r2 >= ad) { // must be unsigned comparison
497 q2 = q2 + 1;
498 r2 = r2 - ad;
499 }
500 delta = ad - r2;
501 } while (q1 < delta || (q1 == delta && r1 == 0));
502
503 mag.m = q2 + 1;
504 if (d < 0) mag.m = -mag.m; // resulting magic number
505 mag.s = p - 64; // resulting shift
506 return mag;
507}
508
509/// magicu - calculate the magic numbers required to codegen an integer udiv as
510/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
511static struct mu magicu(uint64_t d)
512{
513 int64_t p;
514 uint64_t nc, delta, q1, r1, q2, r2;
515 struct mu magu;
516 magu.a = 0; // initialize "add" indicator
517 nc = - 1 - (-d)%d;
Andrew Lenharth320174f2005-04-07 17:19:16 +0000518 p = 63; // initialize p
519 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
520 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
521 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
522 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000523 do {
524 p = p + 1;
525 if (r1 >= nc - r1 ) {
526 q1 = 2*q1 + 1; // update q1
527 r1 = 2*r1 - nc; // update r1
528 }
529 else {
530 q1 = 2*q1; // update q1
531 r1 = 2*r1; // update r1
532 }
533 if (r2 + 1 >= d - r2) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000534 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000535 q2 = 2*q2 + 1; // update q2
536 r2 = 2*r2 + 1 - d; // update r2
537 }
538 else {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000539 if (q2 >= 0x8000000000000000ull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000540 q2 = 2*q2; // update q2
541 r2 = 2*r2 + 1; // update r2
542 }
543 delta = d - 1 - r2;
544 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
545 magu.m = q2 + 1; // resulting magic number
Andrew Lenharth320174f2005-04-07 17:19:16 +0000546 magu.s = p - 64; // resulting shift
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000547 return magu;
548}
549
550/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
551/// return a DAG expression to select that will generate the same value by
552/// multiplying by a magic number. See:
553/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
554SDOperand ISel::BuildSDIVSequence(SDOperand N) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000555 int64_t d = (int64_t)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000556 ms magics = magic(d);
557 // Multiply the numerator (operand 0) by the magic value
558 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i64, N.getOperand(0),
559 ISelDAG->getConstant(magics.m, MVT::i64));
560 // If d > 0 and m < 0, add the numerator
561 if (d > 0 && magics.m < 0)
562 Q = ISelDAG->getNode(ISD::ADD, MVT::i64, Q, N.getOperand(0));
563 // If d < 0 and m > 0, subtract the numerator.
564 if (d < 0 && magics.m > 0)
565 Q = ISelDAG->getNode(ISD::SUB, MVT::i64, Q, N.getOperand(0));
566 // Shift right algebraic if shift value is nonzero
567 if (magics.s > 0)
568 Q = ISelDAG->getNode(ISD::SRA, MVT::i64, Q,
569 ISelDAG->getConstant(magics.s, MVT::i64));
570 // Extract the sign bit and add it to the quotient
571 SDOperand T =
572 ISelDAG->getNode(ISD::SRL, MVT::i64, Q, ISelDAG->getConstant(63, MVT::i64));
573 return ISelDAG->getNode(ISD::ADD, MVT::i64, Q, T);
574}
575
576/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
577/// return a DAG expression to select that will generate the same value by
578/// multiplying by a magic number. See:
579/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
580SDOperand ISel::BuildUDIVSequence(SDOperand N) {
581 unsigned d =
582 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
583 mu magics = magicu(d);
584 // Multiply the numerator (operand 0) by the magic value
585 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i64, N.getOperand(0),
586 ISelDAG->getConstant(magics.m, MVT::i64));
587 if (magics.a == 0) {
588 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, Q,
589 ISelDAG->getConstant(magics.s, MVT::i64));
590 } else {
591 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i64, N.getOperand(0), Q);
592 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
593 ISelDAG->getConstant(1, MVT::i64));
594 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i64, NPQ, Q);
595 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
596 ISelDAG->getConstant(magics.s-1, MVT::i64));
597 }
598 return Q;
599}
600
Andrew Lenhartha565c272005-04-06 22:03:13 +0000601//From PPC32
602/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
603/// returns zero when the input is not exactly a power of two.
604static unsigned ExactLog2(uint64_t Val) {
605 if (Val == 0 || (Val & (Val-1))) return 0;
606 unsigned Count = 0;
607 while (Val != 1) {
608 Val >>= 1;
609 ++Count;
610 }
611 return Count;
612}
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000613
614
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000615//These describe LDAx
Andrew Lenharthc0513832005-03-29 19:24:04 +0000616static const int IMM_LOW = -32768;
617static const int IMM_HIGH = 32767;
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000618static const int IMM_MULT = 65536;
619
620static long getUpper16(long l)
621{
622 long y = l / IMM_MULT;
623 if (l % IMM_MULT > IMM_HIGH)
624 ++y;
625 return y;
626}
627
628static long getLower16(long l)
629{
630 long h = getUpper16(l);
631 return l - h * IMM_MULT;
632}
633
Andrew Lenharth65838902005-02-06 16:22:15 +0000634static unsigned GetSymVersion(unsigned opcode)
635{
636 switch (opcode) {
637 default: assert(0 && "unknown load or store"); return 0;
638 case Alpha::LDQ: return Alpha::LDQ_SYM;
639 case Alpha::LDS: return Alpha::LDS_SYM;
640 case Alpha::LDT: return Alpha::LDT_SYM;
641 case Alpha::LDL: return Alpha::LDL_SYM;
642 case Alpha::LDBU: return Alpha::LDBU_SYM;
643 case Alpha::LDWU: return Alpha::LDWU_SYM;
644 case Alpha::LDW: return Alpha::LDW_SYM;
645 case Alpha::LDB: return Alpha::LDB_SYM;
646 case Alpha::STQ: return Alpha::STQ_SYM;
647 case Alpha::STS: return Alpha::STS_SYM;
648 case Alpha::STT: return Alpha::STT_SYM;
649 case Alpha::STL: return Alpha::STL_SYM;
650 case Alpha::STW: return Alpha::STW_SYM;
651 case Alpha::STB: return Alpha::STB_SYM;
652 }
653}
654
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000655void ISel::MoveFP2Int(unsigned src, unsigned dst, bool isDouble)
656{
657 unsigned Opc;
658 if (EnableAlphaFTOI) {
659 Opc = isDouble ? Alpha::FTOIT : Alpha::FTOIS;
660 BuildMI(BB, Opc, 1, dst).addReg(src);
661 } else {
662 //The hard way:
663 // Spill the integer to memory and reload it from there.
664 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
665 MachineFunction *F = BB->getParent();
666 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
667
668 Opc = isDouble ? Alpha::STT : Alpha::STS;
669 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
670 Opc = isDouble ? Alpha::LDQ : Alpha::LDL;
671 BuildMI(BB, Alpha::LDQ, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
672 }
673}
674
675void ISel::MoveInt2FP(unsigned src, unsigned dst, bool isDouble)
676{
677 unsigned Opc;
678 if (EnableAlphaFTOI) {
679 Opc = isDouble?Alpha::ITOFT:Alpha::ITOFS;
680 BuildMI(BB, Opc, 1, dst).addReg(src);
681 } else {
682 //The hard way:
683 // Spill the integer to memory and reload it from there.
684 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
685 MachineFunction *F = BB->getParent();
686 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
687
688 Opc = isDouble ? Alpha::STQ : Alpha::STL;
689 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
690 Opc = isDouble ? Alpha::LDT : Alpha::LDS;
691 BuildMI(BB, Opc, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
692 }
693}
694
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000695bool ISel::SelectFPSetCC(SDOperand N, unsigned dst)
696{
697 SDNode *Node = N.Val;
698 unsigned Opc, Tmp1, Tmp2, Tmp3;
699 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node);
700
701 //assert(SetCC->getOperand(0).getValueType() != MVT::f32 && "SetCC f32 should have been promoted");
702 bool rev = false;
703 bool inv = false;
704
705 switch (SetCC->getCondition()) {
706 default: Node->dump(); assert(0 && "Unknown FP comparison!");
707 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
708 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
709 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
710 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
711 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
712 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
713 }
714
715 //FIXME: check for constant 0.0
716 ConstantFPSDNode *CN;
717 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
718 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
719 Tmp1 = Alpha::F31;
720 else
721 Tmp1 = SelectExpr(N.getOperand(0));
722
723 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
724 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
725 Tmp2 = Alpha::F31;
726 else
727 Tmp2 = SelectExpr(N.getOperand(1));
728
729 //Can only compare doubles, and dag won't promote for me
730 if (SetCC->getOperand(0).getValueType() == MVT::f32)
731 {
732 //assert(0 && "Setcc On float?\n");
733 std::cerr << "Setcc on float!\n";
734 Tmp3 = MakeReg(MVT::f64);
735 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
736 Tmp1 = Tmp3;
737 }
738 if (SetCC->getOperand(1).getValueType() == MVT::f32)
739 {
740 //assert (0 && "Setcc On float?\n");
741 std::cerr << "Setcc on float!\n";
742 Tmp3 = MakeReg(MVT::f64);
743 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp2);
744 Tmp2 = Tmp3;
745 }
746
747 if (rev) std::swap(Tmp1, Tmp2);
748 //do the comparison
749 BuildMI(BB, Opc, 2, dst).addReg(Tmp1).addReg(Tmp2);
750 return inv;
751}
752
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000753//Check to see if the load is a constant offset from a base register
754void ISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
755{
756 unsigned opcode = N.getOpcode();
757 if (opcode == ISD::ADD) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000758 if(N.getOperand(1).getOpcode() == ISD::Constant &&
759 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
760 { //Normal imm add
761 Reg = SelectExpr(N.getOperand(0));
762 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
763 return;
764 }
765 else if(N.getOperand(0).getOpcode() == ISD::Constant &&
766 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 32767)
767 {
768 Reg = SelectExpr(N.getOperand(1));
769 offset = cast<ConstantSDNode>(N.getOperand(0))->getValue();
770 return;
771 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000772 }
773 Reg = SelectExpr(N);
774 offset = 0;
775 return;
776}
777
Andrew Lenharth445171a2005-02-08 00:40:03 +0000778void ISel::SelectBranchCC(SDOperand N)
779{
780 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000781 MachineBasicBlock *Dest =
782 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
783 unsigned Opc = Alpha::WTF;
784
Andrew Lenharth445171a2005-02-08 00:40:03 +0000785 Select(N.getOperand(0)); //chain
786 SDOperand CC = N.getOperand(1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000787
Andrew Lenharth445171a2005-02-08 00:40:03 +0000788 if (CC.getOpcode() == ISD::SETCC)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000789 {
790 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
791 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
792 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000793 bool LeftZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
794 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
795 bool RightZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
796 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000797 bool isNE = false;
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000798
799 //Fix up CC
800 ISD::CondCode cCode= SetCC->getCondition();
801 if (LeftZero && !RightZero) //Swap Operands
802 cCode = ISD::getSetCCSwappedOperands(cCode);
803
804 if(cCode == ISD::SETNE)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000805 isNE = true;
Andrew Lenharth445171a2005-02-08 00:40:03 +0000806
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000807 if (LeftZero || RightZero) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000808 switch (SetCC->getCondition()) {
809 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
810 case ISD::SETEQ: Opc = Alpha::BEQ; break;
811 case ISD::SETLT: Opc = Alpha::BLT; break;
812 case ISD::SETLE: Opc = Alpha::BLE; break;
813 case ISD::SETGT: Opc = Alpha::BGT; break;
814 case ISD::SETGE: Opc = Alpha::BGE; break;
815 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
816 case ISD::SETUGT: Opc = Alpha::BNE; break;
817 case ISD::SETULE: Opc = Alpha::BEQ; break; //Technically you could have this CC
818 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
819 case ISD::SETNE: Opc = Alpha::BNE; break;
820 }
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000821 unsigned Tmp1;
822 if(LeftZero && !RightZero) //swap Operands
823 Tmp1 = SelectExpr(SetCC->getOperand(1)); //Cond
824 else
825 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000826 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
827 return;
828 } else {
829 unsigned Tmp1 = SelectExpr(CC);
830 if (isNE)
831 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
832 else
833 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000834 return;
835 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000836 } else { //FP
837 //Any comparison between 2 values should be codegened as an folded branch, as moving
838 //CC to the integer register is very expensive
839 //for a cmp b: c = a - b;
840 //a = b: c = 0
841 //a < b: c < 0
842 //a > b: c > 0
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000843
844 bool invTest = false;
845 unsigned Tmp3;
846
847 ConstantFPSDNode *CN;
848 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
849 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
850 Tmp3 = SelectExpr(SetCC->getOperand(0));
851 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
852 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
853 {
854 Tmp3 = SelectExpr(SetCC->getOperand(1));
855 invTest = true;
856 }
857 else
858 {
859 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
860 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
861 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
862 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
863 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
864 .addReg(Tmp1).addReg(Tmp2);
865 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000866
867 switch (SetCC->getCondition()) {
868 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000869 case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
870 case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
871 case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
872 case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
873 case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
874 case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000875 }
876 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000877 return;
878 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000879 abort(); //Should never be reached
880 } else {
881 //Giveup and do the stupid thing
882 unsigned Tmp1 = SelectExpr(CC);
883 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
884 return;
885 }
Andrew Lenharth445171a2005-02-08 00:40:03 +0000886 abort(); //Should never be reached
887}
888
Andrew Lenharth40831c52005-01-28 06:57:18 +0000889unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
890{
891 unsigned Tmp1, Tmp2, Tmp3;
892 unsigned Opc = 0;
893 SDNode *Node = N.Val;
894 MVT::ValueType DestType = N.getValueType();
895 unsigned opcode = N.getOpcode();
896
897 switch (opcode) {
898 default:
899 Node->dump();
900 assert(0 && "Node not handled!\n");
Andrew Lenharth2c594352005-01-29 15:42:07 +0000901
Andrew Lenharth7332f3e2005-04-02 19:11:07 +0000902 case ISD::UNDEF: {
903 BuildMI(BB, Alpha::IDEF, 0, Result);
904 return Result;
905 }
906
Andrew Lenharth30b46d42005-04-02 19:04:58 +0000907 case ISD::FNEG:
908 if(ISD::FABS == N.getOperand(0).getOpcode())
909 {
910 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth7332f3e2005-04-02 19:11:07 +0000911 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +0000912 } else {
913 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth7332f3e2005-04-02 19:11:07 +0000914 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +0000915 }
916 return Result;
917
918 case ISD::FABS:
919 Tmp1 = SelectExpr(N.getOperand(0));
920 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
921 return Result;
922
Andrew Lenharth9818c052005-02-05 13:19:12 +0000923 case ISD::SELECT:
924 {
Andrew Lenharth45859692005-03-03 21:47:53 +0000925 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
926 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
927 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
928
929 SDOperand CC = N.getOperand(0);
930 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
931
932 if (CC.getOpcode() == ISD::SETCC &&
933 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
934 { //FP Setcc -> Select yay!
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000935
936
Andrew Lenharth45859692005-03-03 21:47:53 +0000937 //for a cmp b: c = a - b;
938 //a = b: c = 0
939 //a < b: c < 0
940 //a > b: c > 0
941
942 bool invTest = false;
943 unsigned Tmp3;
944
945 ConstantFPSDNode *CN;
946 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
947 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
948 Tmp3 = SelectExpr(SetCC->getOperand(0));
949 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
950 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
951 {
952 Tmp3 = SelectExpr(SetCC->getOperand(1));
953 invTest = true;
954 }
955 else
956 {
957 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
958 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
959 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
960 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
961 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
962 .addReg(Tmp1).addReg(Tmp2);
963 }
964
965 switch (SetCC->getCondition()) {
966 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
967 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNE : Alpha::FCMOVEQ; break;
968 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGT : Alpha::FCMOVLT; break;
969 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGE : Alpha::FCMOVLE; break;
970 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLT : Alpha::FCMOVGT; break;
971 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLE : Alpha::FCMOVGE; break;
972 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQ : Alpha::FCMOVNE; break;
973 }
Andrew Lenharth33819132005-03-04 20:09:23 +0000974 BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
Andrew Lenharth45859692005-03-03 21:47:53 +0000975 return Result;
976 }
977 else
978 {
979 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000980 BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV).addReg(Tmp1);
981// // Spill the cond to memory and reload it from there.
982// unsigned Tmp4 = MakeReg(MVT::f64);
983// MoveIntFP(Tmp1, Tmp4, true);
984// //now ideally, we don't have to do anything to the flag...
985// // Get the condition into the zero flag.
986// BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
Andrew Lenharth45859692005-03-03 21:47:53 +0000987 return Result;
988 }
Andrew Lenharth9818c052005-02-05 13:19:12 +0000989 }
990
Andrew Lenharthc1faced2005-02-01 01:37:24 +0000991 case ISD::FP_ROUND:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000992 assert (DestType == MVT::f32 &&
993 N.getOperand(0).getValueType() == MVT::f64 &&
994 "only f64 to f32 conversion supported here");
Andrew Lenharthc1faced2005-02-01 01:37:24 +0000995 Tmp1 = SelectExpr(N.getOperand(0));
996 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
997 return Result;
998
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000999 case ISD::FP_EXTEND:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001000 assert (DestType == MVT::f64 &&
1001 N.getOperand(0).getValueType() == MVT::f32 &&
1002 "only f32 to f64 conversion supported here");
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001003 Tmp1 = SelectExpr(N.getOperand(0));
1004 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
1005 return Result;
1006
Andrew Lenharth2c594352005-01-29 15:42:07 +00001007 case ISD::CopyFromReg:
1008 {
1009 // Make sure we generate both values.
1010 if (Result != notIn)
1011 ExprMap[N.getValue(1)] = notIn; // Generate the token
1012 else
1013 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1014
1015 SDOperand Chain = N.getOperand(0);
1016
1017 Select(Chain);
1018 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1019 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1020 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
1021 return Result;
1022 }
1023
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001024 case ISD::LOAD:
1025 {
1026 // Make sure we generate both values.
1027 if (Result != notIn)
1028 ExprMap[N.getValue(1)] = notIn; // Generate the token
1029 else
1030 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001031
Andrew Lenharth29219162005-02-07 06:31:44 +00001032 DestType = N.getValue(0).getValueType();
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001033
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001034 SDOperand Chain = N.getOperand(0);
1035 SDOperand Address = N.getOperand(1);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001036 Select(Chain);
Andrew Lenharth65838902005-02-06 16:22:15 +00001037 Opc = DestType == MVT::f64 ? Alpha::LDT : Alpha::LDS;
1038
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001039 if (Address.getOpcode() == ISD::GlobalAddress) {
1040 AlphaLowering.restoreGP(BB);
1041 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001042 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001043 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1044 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001045 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001046 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001047 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001048 has_sym = true;
Andrew Lenharth97127a12005-02-05 17:41:39 +00001049 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001050 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001051 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001052 BuildMI(BB, Opc, 2, Result)
1053 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1054 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001055 } else {
1056 long offset;
1057 SelectAddr(Address, Tmp1, offset);
1058 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1059 }
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001060 return Result;
1061 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001062 case ISD::ConstantFP:
1063 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
1064 if (CN->isExactlyValue(+0.0)) {
1065 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001066 } else if ( CN->isExactlyValue(-0.0)) {
1067 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001068 } else {
1069 abort();
1070 }
1071 }
1072 return Result;
1073
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001074 case ISD::SDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001075 case ISD::MUL:
1076 case ISD::ADD:
1077 case ISD::SUB:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001078 switch( opcode ) {
1079 case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS; break;
1080 case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS; break;
1081 case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS; break;
1082 case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS; break;
1083 };
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001084
1085 ConstantFPSDNode *CN;
1086 if (opcode == ISD::SUB
1087 && (CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
1088 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1089 {
1090 Tmp2 = SelectExpr(N.getOperand(1));
1091 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp2).addReg(Tmp2);
1092 } else {
1093 Tmp1 = SelectExpr(N.getOperand(0));
1094 Tmp2 = SelectExpr(N.getOperand(1));
1095 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1096 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001097 return Result;
1098
Andrew Lenharth2c594352005-01-29 15:42:07 +00001099 case ISD::EXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001100 {
1101 //include a conversion sequence for float loads to double
1102 if (Result != notIn)
1103 ExprMap[N.getValue(1)] = notIn; // Generate the token
1104 else
1105 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1106
Andrew Lenhartha549deb2005-02-07 05:33:15 +00001107 Tmp1 = MakeReg(MVT::f32);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001108
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001109 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
1110 "EXTLOAD not from f32");
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001111 assert(Node->getValueType(0) == MVT::f64 && "EXTLOAD not to f64");
1112
1113 SDOperand Chain = N.getOperand(0);
1114 SDOperand Address = N.getOperand(1);
1115 Select(Chain);
1116
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001117 if (Address.getOpcode() == ISD::GlobalAddress) {
1118 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001119 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001120 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1121 }
1122 else if (ConstantPoolSDNode *CP =
1123 dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
1124 {
1125 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001126 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001127 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addConstantPoolIndex(CP->getIndex());
1128 }
1129 else if(Address.getOpcode() == ISD::FrameIndex) {
1130 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
Andrew Lenharth032f2352005-02-22 21:59:48 +00001131 BuildMI(BB, Alpha::LDS, 2, Tmp1)
1132 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1133 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001134 } else {
1135 long offset;
1136 SelectAddr(Address, Tmp2, offset);
1137 BuildMI(BB, Alpha::LDS, 1, Tmp1).addImm(offset).addReg(Tmp2);
1138 }
Andrew Lenharth29219162005-02-07 06:31:44 +00001139 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001140 return Result;
1141 }
Andrew Lenharth2c594352005-01-29 15:42:07 +00001142
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001143 case ISD::UINT_TO_FP:
1144 case ISD::SINT_TO_FP:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001145 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001146 assert (N.getOperand(0).getValueType() == MVT::i64
1147 && "only quads can be loaded from");
Andrew Lenharth40831c52005-01-28 06:57:18 +00001148 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001149 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001150 MoveInt2FP(Tmp1, Tmp2, true);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001151 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
1152 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001153 return Result;
1154 }
1155 }
1156 assert(0 && "should not get here");
1157 return 0;
1158}
1159
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001160unsigned ISel::SelectExpr(SDOperand N) {
1161 unsigned Result;
Andrew Lenharth2966e842005-04-07 18:15:28 +00001162 unsigned Tmp1, Tmp2 = 0, Tmp3;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001163 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001164 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001165
1166 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001167 MVT::ValueType DestType = N.getValueType();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001168
1169 unsigned &Reg = ExprMap[N];
1170 if (Reg) return Reg;
1171
1172 if (N.getOpcode() != ISD::CALL)
1173 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001174 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001175 else {
1176 // If this is a call instruction, make sure to prepare ALL of the result
1177 // values as well as the chain.
1178 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001179 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001180 else {
1181 Result = MakeReg(Node->getValueType(0));
1182 ExprMap[N.getValue(0)] = Result;
1183 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1184 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001185 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001186 }
1187 }
1188
Andrew Lenharth22088bb2005-02-02 15:05:33 +00001189 if (DestType == MVT::f64 || DestType == MVT::f32 ||
Andrew Lenharth06342c32005-02-07 06:21:37 +00001190 (
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001191 (opcode == ISD::LOAD || opcode == ISD::CopyFromReg ||
1192 opcode == ISD::EXTLOAD) &&
1193 (N.getValue(0).getValueType() == MVT::f32 ||
1194 N.getValue(0).getValueType() == MVT::f64)
Andrew Lenharth06342c32005-02-07 06:21:37 +00001195 )
1196 )
Andrew Lenharth40831c52005-01-28 06:57:18 +00001197 return SelectExprFP(N, Result);
1198
1199 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001200 default:
1201 Node->dump();
1202 assert(0 && "Node not handled!\n");
1203
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001204 case ISD::MULHU:
1205 Tmp1 = SelectExpr(N.getOperand(0));
1206 Tmp2 = SelectExpr(N.getOperand(1));
1207 BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth706be912005-04-07 13:55:53 +00001208 return Result;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001209 case ISD::MULHS:
1210 {
1211 //MULHU - Ra<63>*Rb - Rb<63>*Ra
1212 Tmp1 = SelectExpr(N.getOperand(0));
1213 Tmp2 = SelectExpr(N.getOperand(1));
1214 Tmp3 = MakeReg(MVT::i64);
1215 BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1216 unsigned V1 = MakeReg(MVT::i64);
1217 unsigned V2 = MakeReg(MVT::i64);
1218 BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31).addReg(Tmp1);
1219 BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31).addReg(Tmp2);
1220 unsigned IRes = MakeReg(MVT::i64);
1221 BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
1222 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
1223 return Result;
1224 }
Andrew Lenharth7332f3e2005-04-02 19:11:07 +00001225 case ISD::UNDEF: {
1226 BuildMI(BB, Alpha::IDEF, 0, Result);
1227 return Result;
1228 }
1229
Andrew Lenharth032f2352005-02-22 21:59:48 +00001230 case ISD::DYNAMIC_STACKALLOC:
1231 // Generate both result values.
Andrew Lenharth3a7118d2005-02-23 17:33:42 +00001232 if (Result != notIn)
1233 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth032f2352005-02-22 21:59:48 +00001234 else
1235 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1236
1237 // FIXME: We are currently ignoring the requested alignment for handling
1238 // greater than the stack alignment. This will need to be revisited at some
1239 // point. Align = N.getOperand(2);
1240
1241 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1242 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1243 std::cerr << "Cannot allocate stack object with greater alignment than"
1244 << " the stack alignment yet!";
1245 abort();
1246 }
1247
1248 Select(N.getOperand(0));
1249 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1250 {
1251 if (CN->getValue() < 32000)
1252 {
1253 BuildMI(BB, Alpha::LDA, 2, Alpha::R30)
1254 .addImm(-CN->getValue()).addReg(Alpha::R30);
1255 } else {
1256 Tmp1 = SelectExpr(N.getOperand(1));
1257 // Subtract size from stack pointer, thereby allocating some space.
1258 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1259 }
1260 } else {
1261 Tmp1 = SelectExpr(N.getOperand(1));
1262 // Subtract size from stack pointer, thereby allocating some space.
1263 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1264 }
1265
1266 // Put a pointer to the space into the result register, by copying the stack
1267 // pointer.
Andrew Lenharth7bc47022005-02-22 23:29:25 +00001268 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
Andrew Lenharth032f2352005-02-22 21:59:48 +00001269 return Result;
1270
Andrew Lenharth33819132005-03-04 20:09:23 +00001271// case ISD::ConstantPool:
1272// Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1273// AlphaLowering.restoreGP(BB);
1274// BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(Tmp1);
1275// return Result;
Andrew Lenharth2c594352005-01-29 15:42:07 +00001276
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001277 case ISD::FrameIndex:
Andrew Lenharth032f2352005-02-22 21:59:48 +00001278 BuildMI(BB, Alpha::LDA, 2, Result)
1279 .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
1280 .addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001281 return Result;
1282
1283 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001284 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001285 case ISD::SEXTLOAD:
Andrew Lenhartha549deb2005-02-07 05:33:15 +00001286 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001287 {
1288 // Make sure we generate both values.
1289 if (Result != notIn)
1290 ExprMap[N.getValue(1)] = notIn; // Generate the token
1291 else
1292 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001293
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001294 SDOperand Chain = N.getOperand(0);
1295 SDOperand Address = N.getOperand(1);
1296 Select(Chain);
1297
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001298 assert(Node->getValueType(0) == MVT::i64 &&
1299 "Unknown type to sign extend to.");
Andrew Lenharth03824012005-02-07 05:55:55 +00001300 if (opcode == ISD::LOAD)
1301 Opc = Alpha::LDQ;
1302 else
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001303 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
1304 default: Node->dump(); assert(0 && "Bad sign extend!");
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001305 case MVT::i32: Opc = Alpha::LDL;
1306 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
1307 case MVT::i16: Opc = Alpha::LDWU;
1308 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001309 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001310 case MVT::i8: Opc = Alpha::LDBU;
1311 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001312 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001313
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001314 if (Address.getOpcode() == ISD::GlobalAddress) {
1315 AlphaLowering.restoreGP(BB);
1316 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001317 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001318 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1319 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001320 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1321 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001322 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001323 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001324 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001325 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001326 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001327 BuildMI(BB, Opc, 2, Result)
1328 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1329 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001330 } else {
1331 long offset;
1332 SelectAddr(Address, Tmp1, offset);
1333 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1334 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001335 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001336 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001337
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001338 case ISD::GlobalAddress:
1339 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001340 has_sym = true;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001341 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
1342 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
1343 return Result;
1344
1345 case ISD::CALL:
1346 {
1347 Select(N.getOperand(0));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001348
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001349 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001350 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001351
1352 //grab the arguments
1353 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001354 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001355 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001356 argvregs.push_back(SelectExpr(N.getOperand(i)));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001357
Andrew Lenharth684f2292005-01-30 00:35:27 +00001358 //in reg args
1359 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001360 {
1361 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
1362 Alpha::R19, Alpha::R20, Alpha::R21};
1363 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
1364 Alpha::F19, Alpha::F20, Alpha::F21};
1365 switch(N.getOperand(i+2).getValueType()) {
1366 default:
1367 Node->dump();
1368 N.getOperand(i).Val->dump();
1369 std::cerr << "Type for " << i << " is: " <<
1370 N.getOperand(i+2).getValueType() << "\n";
1371 assert(0 && "Unknown value type for call");
1372 case MVT::i1:
1373 case MVT::i8:
1374 case MVT::i16:
1375 case MVT::i32:
1376 case MVT::i64:
1377 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1378 break;
1379 case MVT::f32:
1380 case MVT::f64:
1381 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1382 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001383 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001384 }
Andrew Lenharth684f2292005-01-30 00:35:27 +00001385 //in mem args
1386 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001387 {
1388 switch(N.getOperand(i+2).getValueType()) {
1389 default:
1390 Node->dump();
1391 N.getOperand(i).Val->dump();
1392 std::cerr << "Type for " << i << " is: " <<
1393 N.getOperand(i+2).getValueType() << "\n";
1394 assert(0 && "Unknown value type for call");
1395 case MVT::i1:
1396 case MVT::i8:
1397 case MVT::i16:
1398 case MVT::i32:
1399 case MVT::i64:
1400 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1401 break;
1402 case MVT::f32:
1403 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1404 break;
1405 case MVT::f64:
1406 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1407 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001408 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001409 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001410 //build the right kind of call
1411 if (GlobalAddressSDNode *GASD =
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001412 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001413 {
Andrew Lenharth3e315922005-02-10 20:10:38 +00001414 //if (GASD->getGlobal()->isExternal()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001415 //use safe calling convention
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001416 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001417 has_sym = true;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001418 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal(),true);
Andrew Lenharth3e315922005-02-10 20:10:38 +00001419 //} else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001420 //use PC relative branch call
Andrew Lenharth3e315922005-02-10 20:10:38 +00001421 //BuildMI(BB, Alpha::BSR, 1, Alpha::R26).addGlobalAddress(GASD->getGlobal(),true);
1422 //}
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001423 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001424 else if (ExternalSymbolSDNode *ESSDN =
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001425 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001426 {
1427 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001428 has_sym = true;
Andrew Lenharthba05ad62005-03-30 18:22:52 +00001429 BuildMI(BB, Alpha::CALL, 1).addExternalSymbol(ESSDN->getSymbol(), true);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001430 } else {
1431 //no need to restore GP as we are doing an indirect call
1432 Tmp1 = SelectExpr(N.getOperand(1));
1433 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
1434 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
1435 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001436
1437 //push the result into a virtual register
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001438
1439 switch (Node->getValueType(0)) {
1440 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001441 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001442 case MVT::i1:
1443 case MVT::i8:
1444 case MVT::i16:
1445 case MVT::i32:
1446 case MVT::i64:
1447 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
1448 break;
1449 case MVT::f32:
1450 case MVT::f64:
1451 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
1452 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001453 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001454 return Result+N.ResNo;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001455 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001456
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001457 case ISD::SIGN_EXTEND_INREG:
1458 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001459 //do SDIV opt for all levels of ints if not dividing by a constant
1460 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
1461 && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001462 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001463 unsigned Tmp4 = MakeReg(MVT::f64);
1464 unsigned Tmp5 = MakeReg(MVT::f64);
1465 unsigned Tmp6 = MakeReg(MVT::f64);
1466 unsigned Tmp7 = MakeReg(MVT::f64);
1467 unsigned Tmp8 = MakeReg(MVT::f64);
1468 unsigned Tmp9 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001469
1470 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1471 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1472 MoveInt2FP(Tmp1, Tmp4, true);
1473 MoveInt2FP(Tmp2, Tmp5, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001474 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Tmp4);
1475 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Tmp5);
1476 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
1477 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Tmp8);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001478 MoveFP2Int(Tmp9, Result, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001479 return Result;
1480 }
1481
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001482 //Alpha has instructions for a bunch of signed 32 bit stuff
1483 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001484 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001485 switch (N.getOperand(0).getOpcode()) {
1486 case ISD::ADD:
1487 case ISD::SUB:
1488 case ISD::MUL:
1489 {
1490 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
1491 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
1492 //FIXME: first check for Scaled Adds and Subs!
1493 if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1494 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
1495 { //Normal imm add/sub
1496 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth093f3272005-02-12 21:11:17 +00001497 //if the value was really originally a i32, skip the up conversion
1498 if (N.getOperand(0).getOperand(0).getOpcode() == ISD::SIGN_EXTEND_INREG &&
1499 dyn_cast<MVTSDNode>(N.getOperand(0).getOperand(0).Val)
1500 ->getExtraValueType() == MVT::i32)
1501 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1502 else
1503 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001504 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
1505 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001506 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001507 else
1508 { //Normal add/sub
1509 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULLi : Alpha::SUBL);
Andrew Lenharth093f3272005-02-12 21:11:17 +00001510 //if the value was really originally a i32, skip the up conversion
1511 if (N.getOperand(0).getOperand(0).getOpcode() == ISD::SIGN_EXTEND_INREG &&
1512 dyn_cast<MVTSDNode>(N.getOperand(0).getOperand(0).Val)
1513 ->getExtraValueType() == MVT::i32)
1514 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1515 else
1516 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1517 //if the value was really originally a i32, skip the up conversion
1518 if (N.getOperand(0).getOperand(1).getOpcode() == ISD::SIGN_EXTEND_INREG &&
1519 dyn_cast<MVTSDNode>(N.getOperand(0).getOperand(1).Val)
1520 ->getExtraValueType() == MVT::i32)
1521 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1522 else
1523 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1524
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001525 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001526 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1527 }
1528 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001529 }
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001530 case ISD::SEXTLOAD:
1531 //SelectionDag isn't deleting the signextend after sextloads
1532 Reg = Result = SelectExpr(N.getOperand(0));
1533 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001534 default: break; //Fall Though;
1535 }
1536 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001537 Tmp1 = SelectExpr(N.getOperand(0));
1538 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001539 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001540 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001541 {
1542 default:
1543 Node->dump();
1544 assert(0 && "Sign Extend InReg not there yet");
1545 break;
1546 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001547 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001548 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001549 break;
1550 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001551 case MVT::i16:
1552 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
1553 break;
1554 case MVT::i8:
1555 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
1556 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +00001557 case MVT::i1:
1558 Tmp2 = MakeReg(MVT::i64);
1559 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001560 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +00001561 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001562 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001563 return Result;
1564 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001565
1566 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001567 {
1568 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1569 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1570 bool isConst1 = false;
1571 bool isConst2 = false;
1572 int dir;
Andrew Lenharth9818c052005-02-05 13:19:12 +00001573
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001574 //Tmp1 = SelectExpr(N.getOperand(0));
1575 if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001576 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
1577 isConst1 = true;
1578 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001579 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1580 isConst2 = true;
1581
1582 switch (SetCC->getCondition()) {
1583 default: Node->dump(); assert(0 && "Unknown integer comparison!");
1584 case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001585 case ISD::SETLT:
1586 Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
1587 case ISD::SETLE:
1588 Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
1589 case ISD::SETGT:
1590 Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
1591 case ISD::SETGE:
1592 Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
1593 case ISD::SETULT:
1594 Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
1595 case ISD::SETUGT:
1596 Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
1597 case ISD::SETULE:
1598 Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
1599 case ISD::SETUGE:
1600 Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001601 case ISD::SETNE: {//Handle this one special
1602 //std::cerr << "Alpha does not have a setne.\n";
1603 //abort();
1604 Tmp1 = SelectExpr(N.getOperand(0));
1605 Tmp2 = SelectExpr(N.getOperand(1));
1606 Tmp3 = MakeReg(MVT::i64);
1607 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001608 //Remeber we have the Inv for this CC
1609 CCInvMap[N] = Tmp3;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001610 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001611 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001612 return Result;
1613 }
1614 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001615 if (dir == 1) {
1616 Tmp1 = SelectExpr(N.getOperand(0));
1617 if (isConst2) {
1618 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1619 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1620 } else {
1621 Tmp2 = SelectExpr(N.getOperand(1));
1622 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1623 }
1624 } else if (dir == 2) {
1625 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001626 if (isConst1) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001627 Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1628 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1629 } else {
1630 Tmp2 = SelectExpr(N.getOperand(0));
1631 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1632 }
1633 } else { //dir == 0
1634 if (isConst1) {
1635 Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1636 Tmp2 = SelectExpr(N.getOperand(1));
1637 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
1638 } else if (isConst2) {
1639 Tmp1 = SelectExpr(N.getOperand(0));
1640 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1641 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
1642 } else {
1643 Tmp1 = SelectExpr(N.getOperand(0));
1644 Tmp2 = SelectExpr(N.getOperand(1));
1645 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1646 }
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001647 }
1648 } else {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001649 //do the comparison
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001650 Tmp1 = MakeReg(MVT::f64);
1651 bool inv = SelectFPSetCC(N, Tmp1);
1652
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001653 //now arrange for Result (int) to have a 1 or 0
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001654 Tmp2 = MakeReg(MVT::i64);
1655 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001656 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001657 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001658 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001659 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001660 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001661 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001662
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001663 case ISD::CopyFromReg:
1664 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001665 ++count_ins;
1666
Andrew Lenharth40831c52005-01-28 06:57:18 +00001667 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001668 if (Result != notIn)
1669 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001670 else
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001671 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth40831c52005-01-28 06:57:18 +00001672
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001673 SDOperand Chain = N.getOperand(0);
1674
1675 Select(Chain);
1676 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1677 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1678 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1679 return Result;
1680 }
1681
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001682 //Most of the plain arithmetic and logic share the same form, and the same
1683 //constant immediate test
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001684 case ISD::XOR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001685 //Match Not
1686 if (N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001687 cast<ConstantSDNode>(N.getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001688 {
1689 Tmp1 = SelectExpr(N.getOperand(0));
1690 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1691 return Result;
1692 }
1693 //Fall through
1694 case ISD::AND:
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001695 //handle zap
1696 if (opcode == ISD::AND && N.getOperand(1).getOpcode() == ISD::Constant)
1697 {
1698 uint64_t k = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1699 unsigned int build = 0;
1700 for(int i = 0; i < 8; ++i)
1701 {
1702 if (k & 0xFF == 0xFF)
1703 build |= 1 << i;
1704 else if (k & 0xFF != 0)
1705 { build = 0; break; }
1706 k >>= 8;
1707 }
1708 if (build)
1709 {
1710 Tmp1 = SelectExpr(N.getOperand(0));
1711 BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1712 return Result;
1713 }
1714 }
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001715 case ISD::OR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001716 //Check operand(0) == Not
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001717 if (N.getOperand(0).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001718 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001719 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001720 {
1721 switch(opcode) {
1722 case ISD::AND: Opc = Alpha::BIC; break;
1723 case ISD::OR: Opc = Alpha::ORNOT; break;
1724 case ISD::XOR: Opc = Alpha::EQV; break;
1725 }
1726 Tmp1 = SelectExpr(N.getOperand(1));
1727 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1728 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1729 return Result;
1730 }
1731 //Check operand(1) == Not
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001732 if (N.getOperand(1).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001733 N.getOperand(1).getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001734 cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001735 {
1736 switch(opcode) {
1737 case ISD::AND: Opc = Alpha::BIC; break;
1738 case ISD::OR: Opc = Alpha::ORNOT; break;
1739 case ISD::XOR: Opc = Alpha::EQV; break;
1740 }
1741 Tmp1 = SelectExpr(N.getOperand(0));
1742 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1743 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1744 return Result;
1745 }
1746 //Fall through
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001747 case ISD::SHL:
1748 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001749 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001750 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001751 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1752 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001753 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001754 {
1755 switch(opcode) {
1756 case ISD::AND: Opc = Alpha::ANDi; break;
1757 case ISD::OR: Opc = Alpha::BISi; break;
1758 case ISD::XOR: Opc = Alpha::XORi; break;
1759 case ISD::SHL: Opc = Alpha::SLi; break;
1760 case ISD::SRL: Opc = Alpha::SRLi; break;
1761 case ISD::SRA: Opc = Alpha::SRAi; break;
1762 case ISD::MUL: Opc = Alpha::MULQi; break;
1763 };
1764 Tmp1 = SelectExpr(N.getOperand(0));
1765 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1766 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1767 } else {
1768 switch(opcode) {
1769 case ISD::AND: Opc = Alpha::AND; break;
1770 case ISD::OR: Opc = Alpha::BIS; break;
1771 case ISD::XOR: Opc = Alpha::XOR; break;
1772 case ISD::SHL: Opc = Alpha::SL; break;
1773 case ISD::SRL: Opc = Alpha::SRL; break;
1774 case ISD::SRA: Opc = Alpha::SRA; break;
1775 case ISD::MUL: Opc = Alpha::MULQ; break;
1776 };
1777 Tmp1 = SelectExpr(N.getOperand(0));
1778 Tmp2 = SelectExpr(N.getOperand(1));
1779 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1780 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001781 return Result;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001782
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001783 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001784 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001785 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001786 bool isAdd = opcode == ISD::ADD;
1787
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001788 //first check for Scaled Adds and Subs!
1789 //Valid for add and sub
1790 if(N.getOperand(0).getOpcode() == ISD::SHL &&
1791 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1792 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() == 2)
1793 {
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001794 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001795 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1796 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1797 BuildMI(BB, isAdd?Alpha::S4ADDQi:Alpha::S4SUBQi, 2, Result).addReg(Tmp2)
1798 .addImm(cast<ConstantSDNode>(N.getOperand(1))->getValue());
1799 else {
1800 Tmp1 = SelectExpr(N.getOperand(1));
1801 BuildMI(BB, isAdd?Alpha::S4ADDQ:Alpha::S4SUBQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
1802 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001803 }
1804 else if(N.getOperand(0).getOpcode() == ISD::SHL &&
1805 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
1806 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() == 3)
1807 {
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001808 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001809 if (N.getOperand(1).getOpcode() == ISD::Constant &&
1810 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1811 BuildMI(BB, isAdd?Alpha::S8ADDQi:Alpha::S8SUBQi, 2, Result).addReg(Tmp2)
1812 .addImm(cast<ConstantSDNode>(N.getOperand(1))->getValue());
1813 else {
1814 Tmp1 = SelectExpr(N.getOperand(1));
1815 BuildMI(BB, isAdd?Alpha::S8ADDQ:Alpha::S8SUBQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
1816 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001817 }
1818 //Position prevents subs
1819 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &
1820 N.getOperand(1).getOperand(1).getOpcode() == ISD::Constant &&
1821 cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getValue() == 2)
1822 {
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001823 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001824 if (N.getOperand(0).getOpcode() == ISD::Constant &&
1825 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
1826 BuildMI(BB, Alpha::S4ADDQi, 2, Result).addReg(Tmp2)
1827 .addImm(cast<ConstantSDNode>(N.getOperand(0))->getValue());
1828 else {
1829 Tmp1 = SelectExpr(N.getOperand(0));
1830 BuildMI(BB, Alpha::S4ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
1831 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001832 }
Andrew Lenharth273a1f92005-04-07 14:18:13 +00001833 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001834 N.getOperand(1).getOperand(1).getOpcode() == ISD::Constant &&
1835 cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getValue() == 3)
1836 {
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001837 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001838 if (N.getOperand(0).getOpcode() == ISD::Constant &&
1839 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
1840 BuildMI(BB, Alpha::S8ADDQi, 2, Result).addReg(Tmp2)
1841 .addImm(cast<ConstantSDNode>(N.getOperand(0))->getValue());
1842 else {
1843 Tmp1 = SelectExpr(N.getOperand(0));
1844 BuildMI(BB, Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
1845 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001846 }
1847 //small addi
1848 else if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001849 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001850 { //Normal imm add/sub
1851 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1852 Tmp1 = SelectExpr(N.getOperand(0));
1853 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1854 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1855 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001856 //larger addi
Andrew Lenharth40831c52005-01-28 06:57:18 +00001857 else if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001858 (cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767 ||
1859 (long)cast<ConstantSDNode>(N.getOperand(1))->getValue() >= -32767))
1860 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001861 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001862 Tmp2 = (long)cast<ConstantSDNode>(N.getOperand(1))->getValue();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001863 if (!isAdd)
1864 Tmp2 = -Tmp2;
1865 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001866 }
1867 //give up and do the operation
1868 else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001869 //Normal add/sub
1870 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1871 Tmp1 = SelectExpr(N.getOperand(0));
1872 Tmp2 = SelectExpr(N.getOperand(1));
1873 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1874 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001875 return Result;
1876 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001877
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001878 case ISD::SDIV:
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001879 {
Andrew Lenhartha565c272005-04-06 22:03:13 +00001880 ConstantSDNode* CSD;
1881 //check if we can convert into a shift!
1882 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
1883 (int64_t)CSD->getSignExtended() != 0 &&
1884 ExactLog2(abs((int64_t)CSD->getSignExtended())) != 0)
1885 {
1886 unsigned k = ExactLog2(abs(CSD->getSignExtended()));
1887 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenhartha565c272005-04-06 22:03:13 +00001888 if (k == 1)
1889 Tmp2 = Tmp1;
1890 else
1891 {
1892 Tmp2 = MakeReg(MVT::i64);
1893 BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
1894 }
1895 Tmp3 = MakeReg(MVT::i64);
1896 BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
1897 unsigned Tmp4 = MakeReg(MVT::i64);
1898 BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
1899 if ((int64_t)CSD->getSignExtended() > 0)
1900 BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
1901 else
1902 {
1903 unsigned Tmp5 = MakeReg(MVT::i64);
1904 BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
1905 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
1906 }
1907 return Result;
1908 }
1909 }
1910 //Else fall through
1911
1912 case ISD::UDIV:
1913 {
1914 ConstantSDNode* CSD;
1915 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
1916 ((int64_t)CSD->getSignExtended() >= 2 ||
1917 (int64_t)CSD->getSignExtended() <= -2))
1918 {
1919 // If this is a divide by constant, we can emit code using some magic
1920 // constants to implement it as a multiply instead.
1921 ExprMap.erase(N);
1922 if (opcode == ISD::SDIV)
1923 return SelectExpr(BuildSDIVSequence(N));
1924 else
1925 return SelectExpr(BuildUDIVSequence(N));
1926 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001927 }
1928 //else fall though
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001929 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +00001930 case ISD::SREM:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001931 //FIXME: alpha really doesn't support any of these operations,
1932 // the ops are expanded into special library calls with
1933 // special calling conventions
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001934 //Restore GP because it is a call after all...
Andrew Lenharth40831c52005-01-28 06:57:18 +00001935 switch(opcode) {
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001936 case ISD::UREM: Opc = Alpha::REMQU; break;
1937 case ISD::SREM: Opc = Alpha::REMQ; break;
1938 case ISD::UDIV: Opc = Alpha::DIVQU; break;
1939 case ISD::SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001940 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001941 Tmp1 = SelectExpr(N.getOperand(0));
1942 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth33819132005-03-04 20:09:23 +00001943 //set up regs explicitly (helps Reg alloc)
1944 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
1945 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001946 AlphaLowering.restoreGP(BB);
Andrew Lenharth33819132005-03-04 20:09:23 +00001947 BuildMI(BB, Opc, 2).addReg(Alpha::R24).addReg(Alpha::R25);
1948 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001949 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001950
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001951 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001952 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001953 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001954 assert (DestType == MVT::i64 && "only quads can be loaded to");
1955 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00001956 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001957 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001958 if (SrcType == MVT::f32)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001959 {
1960 Tmp2 = MakeReg(MVT::f64);
1961 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
1962 Tmp1 = Tmp2;
1963 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001964 Tmp2 = MakeReg(MVT::f64);
1965 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001966 MoveFP2Int(Tmp2, Result, true);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001967
1968 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001969 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001970
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001971 case ISD::SELECT:
1972 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001973 //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 +00001974 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001975 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1976 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001977 // Get the condition into the zero flag.
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001978 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001979
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001980 SDOperand CC = N.getOperand(0);
1981 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
1982
1983 if (CC.getOpcode() == ISD::SETCC &&
1984 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
1985 { //FP Setcc -> Int Select
1986 Tmp1 = MakeReg(MVT::f64);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001987 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1988 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001989 bool inv = SelectFPSetCC(CC, Tmp1);
1990 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
1991 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
1992 return Result;
1993 }
1994 if (CC.getOpcode() == ISD::SETCC) {
1995 //Int SetCC -> Select
1996 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001997 if((SetCC->getOperand(1).getOpcode() == ISD::Constant &&
1998 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0) ||
1999 (SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2000 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0))
2001 {
2002 //figure out a few things
2003 bool LeftZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2004 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
2005 bool RightZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2006 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
2007 bool LeftConst = N.getOperand(1).getOpcode() == ISD::Constant &&
2008 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255;
2009 bool RightConst = N.getOperand(2).getOpcode() == ISD::Constant &&
2010 cast<ConstantSDNode>(N.getOperand(2))->getValue() <= 255;
2011 bool useImm = LeftConst || RightConst;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002012
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002013 //Fix up CC
2014 ISD::CondCode cCode= SetCC->getCondition();
2015 if (RightConst && !LeftConst) //Invert sense to get Imm field right
2016 cCode = ISD::getSetCCInverse(cCode, true);
2017 if (LeftZero && !RightZero) //Swap Operands
2018 cCode = ISD::getSetCCSwappedOperands(cCode);
2019
2020 //Choose the CMOV
2021 switch (cCode) {
2022 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
2023 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
2024 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
2025 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
2026 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
2027 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
2028 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
2029 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2030 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break; //Technically you could have this CC
2031 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
2032 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2033 }
2034 if(LeftZero && !RightZero) //swap Operands
2035 Tmp1 = SelectExpr(SetCC->getOperand(1)); //Cond
2036 else
2037 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
2038
2039 if (LeftConst) {
2040 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2041 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
2042 .addImm(cast<ConstantSDNode>(N.getOperand(1))->getValue())
2043 .addReg(Tmp1);
2044 } else if (RightConst) {
2045 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
2046 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
2047 .addImm(cast<ConstantSDNode>(N.getOperand(2))->getValue())
2048 .addReg(Tmp1);
2049 } else {
2050 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2051 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2052 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
2053 }
2054 return Result;
2055 }
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002056 //Otherwise, fall though
2057 }
2058 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002059 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2060 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthe76797c2005-02-01 20:40:27 +00002061 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002062
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002063 return Result;
2064 }
2065
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002066 case ISD::Constant:
2067 {
Andrew Lenharthc0513832005-03-29 19:24:04 +00002068 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002069 if (val <= IMM_HIGH && val >= IMM_LOW) {
2070 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
2071 }
2072 else if (val <= (int64_t)IMM_HIGH + (int64_t)IMM_HIGH * (int64_t)IMM_MULT &&
2073 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
2074 Tmp1 = MakeReg(MVT::i64);
2075 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val)).addReg(Alpha::R31);
2076 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
2077 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002078 else {
2079 MachineConstantPool *CP = BB->getParent()->getConstantPool();
2080 ConstantUInt *C = ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
2081 unsigned CPI = CP->getConstantPoolIndex(C);
2082 AlphaLowering.restoreGP(BB);
2083 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CPI);
2084 }
2085 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002086 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002087 }
2088
2089 return 0;
2090}
2091
2092void ISel::Select(SDOperand N) {
2093 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00002094 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002095
Nate Begeman85fdeb22005-03-24 04:39:54 +00002096 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002097 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002098
2099 SDNode *Node = N.Val;
Andrew Lenharth760270d2005-02-07 23:02:23 +00002100
Andrew Lenharth760270d2005-02-07 23:02:23 +00002101 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002102
2103 default:
2104 Node->dump(); std::cerr << "\n";
2105 assert(0 && "Node not handled yet!");
2106
2107 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00002108 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002109 return;
2110 }
2111
2112 case ISD::BR: {
2113 MachineBasicBlock *Dest =
2114 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2115
2116 Select(N.getOperand(0));
2117 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
2118 return;
2119 }
2120
2121 case ISD::ImplicitDef:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002122 ++count_ins;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002123 Select(N.getOperand(0));
2124 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
2125 return;
2126
2127 case ISD::EntryToken: return; // Noop
2128
2129 case ISD::TokenFactor:
2130 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2131 Select(Node->getOperand(i));
2132
2133 //N.Val->dump(); std::cerr << "\n";
2134 //assert(0 && "Node not handled yet!");
2135
2136 return;
2137
2138 case ISD::CopyToReg:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002139 ++count_outs;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002140 Select(N.getOperand(0));
2141 Tmp1 = SelectExpr(N.getOperand(1));
2142 Tmp2 = cast<RegSDNode>(N)->getReg();
2143
2144 if (Tmp1 != Tmp2) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002145 if (N.getOperand(1).getValueType() == MVT::f64 ||
2146 N.getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00002147 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2148 else
2149 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002150 }
2151 return;
2152
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002153 case ISD::RET:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002154 ++count_outs;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002155 switch (N.getNumOperands()) {
2156 default:
2157 std::cerr << N.getNumOperands() << "\n";
2158 for (unsigned i = 0; i < N.getNumOperands(); ++i)
2159 std::cerr << N.getOperand(i).getValueType() << "\n";
2160 Node->dump();
2161 assert(0 && "Unknown return instruction!");
2162 case 2:
2163 Select(N.getOperand(0));
2164 Tmp1 = SelectExpr(N.getOperand(1));
2165 switch (N.getOperand(1).getValueType()) {
2166 default: Node->dump();
2167 assert(0 && "All other types should have been promoted!!");
2168 case MVT::f64:
2169 case MVT::f32:
2170 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
2171 break;
2172 case MVT::i32:
2173 case MVT::i64:
2174 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
2175 break;
2176 }
2177 break;
2178 case 1:
2179 Select(N.getOperand(0));
2180 break;
2181 }
2182 //Tmp2 = AlphaLowering.getRetAddr();
2183 //BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(Tmp2).addReg(Tmp2);
2184 BuildMI(BB, Alpha::RETURN, 0); // Just emit a 'ret' instruction
2185 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002186
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00002187 case ISD::TRUNCSTORE:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002188 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002189 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002190 SDOperand Chain = N.getOperand(0);
2191 SDOperand Value = N.getOperand(1);
2192 SDOperand Address = N.getOperand(2);
2193 Select(Chain);
2194
2195 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00002196
2197 if (opcode == ISD::STORE) {
2198 switch(Value.getValueType()) {
2199 default: assert(0 && "unknown Type in store");
2200 case MVT::i64: Opc = Alpha::STQ; break;
2201 case MVT::f64: Opc = Alpha::STT; break;
2202 case MVT::f32: Opc = Alpha::STS; break;
2203 }
2204 } else { //ISD::TRUNCSTORE
2205 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2206 default: assert(0 && "unknown Type in store");
2207 case MVT::i1: //FIXME: DAG does not promote this load
2208 case MVT::i8: Opc = Alpha::STB; break;
2209 case MVT::i16: Opc = Alpha::STW; break;
2210 case MVT::i32: Opc = Alpha::STL; break;
2211 }
Andrew Lenharth65838902005-02-06 16:22:15 +00002212 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00002213
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002214 if (Address.getOpcode() == ISD::GlobalAddress)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002215 {
2216 AlphaLowering.restoreGP(BB);
2217 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002218 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002219 BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
2220 }
Andrew Lenharth05380342005-02-07 05:07:00 +00002221 else if(Address.getOpcode() == ISD::FrameIndex)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002222 {
Andrew Lenharth032f2352005-02-22 21:59:48 +00002223 BuildMI(BB, Opc, 3).addReg(Tmp1)
2224 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
2225 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002226 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002227 else
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002228 {
2229 long offset;
2230 SelectAddr(Address, Tmp2, offset);
2231 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2232 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002233 return;
2234 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002235
2236 case ISD::EXTLOAD:
2237 case ISD::SEXTLOAD:
2238 case ISD::ZEXTLOAD:
2239 case ISD::LOAD:
2240 case ISD::CopyFromReg:
2241 case ISD::CALL:
Andrew Lenharth032f2352005-02-22 21:59:48 +00002242 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002243 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002244 SelectExpr(N);
2245 return;
2246
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002247 case ISD::ADJCALLSTACKDOWN:
2248 case ISD::ADJCALLSTACKUP:
2249 Select(N.getOperand(0));
2250 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2251
2252 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? Alpha::ADJUSTSTACKDOWN :
2253 Alpha::ADJUSTSTACKUP;
2254 BuildMI(BB, Opc, 1).addImm(Tmp1);
2255 return;
Andrew Lenharth95762122005-03-31 21:24:06 +00002256
2257 case ISD::PCMARKER:
2258 Select(N.getOperand(0)); //Chain
2259 BuildMI(BB, Alpha::PCLABEL, 2).addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
2260 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002261 }
2262 assert(0 && "Should not be reached!");
2263}
2264
2265
2266/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
2267/// into a machine code representation using pattern matching and a machine
2268/// description file.
2269///
2270FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
2271 return new ISel(TM);
2272}