blob: 537f15ccd33911f1ef57be5bca18144d807b86fd [file] [log] [blame]
Misha Brukman2a8350a2005-02-05 02:24:26 +00001//===- AlphaISelPattern.cpp - A pattern matching inst selector for Alpha --===//
Misha Brukman4633f1c2005-04-21 23:13:11 +00002//
Andrew Lenharth304d0f32005-01-22 23:41:55 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman4633f1c2005-04-21 23:13:11 +00007//
Andrew Lenharth304d0f32005-01-22 23:41:55 +00008//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for Alpha.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Alpha.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000015#include "AlphaRegisterInfo.h"
16#include "llvm/Constants.h" // FIXME: REMOVE
17#include "llvm/Function.h"
Andrew Lenharthb69f3422005-06-22 17:19:45 +000018#include "llvm/Module.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000019#include "llvm/CodeGen/MachineInstrBuilder.h"
20#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/SelectionDAG.h"
24#include "llvm/CodeGen/SelectionDAGISel.h"
25#include "llvm/CodeGen/SSARegMap.h"
26#include "llvm/Target/TargetData.h"
27#include "llvm/Target/TargetLowering.h"
28#include "llvm/Support/MathExtras.h"
29#include "llvm/ADT/Statistic.h"
Andrew Lenharth032f2352005-02-22 21:59:48 +000030#include "llvm/Support/Debug.h"
Andrew Lenharth95762122005-03-31 21:24:06 +000031#include "llvm/Support/CommandLine.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000032#include <set>
Andrew Lenharth684f2292005-01-30 00:35:27 +000033#include <algorithm>
Andrew Lenharth304d0f32005-01-22 23:41:55 +000034using namespace llvm;
35
Andrew Lenharth95762122005-03-31 21:24:06 +000036namespace llvm {
Misha Brukman4633f1c2005-04-21 23:13:11 +000037 cl::opt<bool> EnableAlphaIDIV("enable-alpha-intfpdiv",
38 cl::desc("Use the FP div instruction for integer div when possible"),
Andrew Lenharth95762122005-03-31 21:24:06 +000039 cl::Hidden);
Andrew Lenharth59009192005-05-04 19:12:09 +000040 cl::opt<bool> EnableAlphaFTOI("enable-alpha-FTOI",
Misha Brukman4633f1c2005-04-21 23:13:11 +000041 cl::desc("Enable use of ftoi* and itof* instructions (ev6 and higher)"),
Andrew Lenharth95762122005-03-31 21:24:06 +000042 cl::Hidden);
Andrew Lenharth59009192005-05-04 19:12:09 +000043 cl::opt<bool> EnableAlphaCT("enable-alpha-CT",
44 cl::desc("Enable use of the ctpop, ctlz, and cttz instructions"),
45 cl::Hidden);
Misha Brukman4633f1c2005-04-21 23:13:11 +000046 cl::opt<bool> EnableAlphaCount("enable-alpha-count",
47 cl::desc("Print estimates on live ins and outs"),
Andrew Lenhartha32b9e32005-04-08 17:28:49 +000048 cl::Hidden);
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +000049 cl::opt<bool> EnableAlphaLSMark("enable-alpha-lsmark",
Misha Brukman5e96a3a2005-06-06 19:08:04 +000050 cl::desc("Emit symbols to correlate Mem ops to LLVM Values"),
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +000051 cl::Hidden);
Andrew Lenharth95762122005-03-31 21:24:06 +000052}
53
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +000054namespace {
55 // Alpha Specific DAG Nodes
56 namespace AlphaISD {
57 enum NodeType {
58 // Start the numbering where the builtin ops leave off.
59 FIRST_NUMBER = ISD::BUILTIN_OP_END,
60
61 //Convert an int bit pattern in an FP reg to a Double or Float
62 //Has a dest type and a source
63 CVTQ,
64 //Move an Ireg to a FPreg
65 ITOF,
66 //Move a FPreg to an Ireg
67 FTOI,
68 };
69 }
70}
71
Andrew Lenharth304d0f32005-01-22 23:41:55 +000072//===----------------------------------------------------------------------===//
73// AlphaTargetLowering - Alpha Implementation of the TargetLowering interface
74namespace {
75 class AlphaTargetLowering : public TargetLowering {
Andrew Lenharth558bc882005-06-18 18:34:52 +000076 int VarArgsOffset; // What is the offset to the first vaarg
77 int VarArgsBase; // What is the base FrameIndex
Andrew Lenharth304d0f32005-01-22 23:41:55 +000078 unsigned GP; //GOT vreg
79 public:
80 AlphaTargetLowering(TargetMachine &TM) : TargetLowering(TM) {
81 // Set up the TargetLowering object.
Andrew Lenharth3d65d312005-01-27 03:49:45 +000082 //I am having problems with shr n ubyte 1
Andrew Lenharth879ef222005-02-02 17:00:21 +000083 setShiftAmountType(MVT::i64);
84 setSetCCResultType(MVT::i64);
Andrew Lenharthd3355e22005-04-07 20:11:32 +000085 setSetCCResultContents(ZeroOrOneSetCCResult);
Misha Brukman4633f1c2005-04-21 23:13:11 +000086
Andrew Lenharth304d0f32005-01-22 23:41:55 +000087 addRegisterClass(MVT::i64, Alpha::GPRCRegisterClass);
88 addRegisterClass(MVT::f64, Alpha::FPRCRegisterClass);
Andrew Lenharth3d65d312005-01-27 03:49:45 +000089 addRegisterClass(MVT::f32, Alpha::FPRCRegisterClass);
Misha Brukman4633f1c2005-04-21 23:13:11 +000090
Chris Lattnerda4d4692005-04-09 03:22:37 +000091 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000092 setOperationAction(ISD::EXTLOAD , MVT::i1 , Promote);
93 setOperationAction(ISD::EXTLOAD , MVT::f32 , Promote);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000094
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000095 setOperationAction(ISD::ZEXTLOAD , MVT::i1 , Expand);
96 setOperationAction(ISD::ZEXTLOAD , MVT::i32 , Expand);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000097
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000098 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
99 setOperationAction(ISD::SEXTLOAD , MVT::i8 , Expand);
100 setOperationAction(ISD::SEXTLOAD , MVT::i16 , Expand);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000101
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000102 setOperationAction(ISD::SREM , MVT::f32 , Expand);
103 setOperationAction(ISD::SREM , MVT::f64 , Expand);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000104
Andrew Lenharth59009192005-05-04 19:12:09 +0000105 if (!EnableAlphaCT) {
106 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
107 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
Andrew Lenharthb5884d32005-05-04 19:25:37 +0000108 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
Andrew Lenharth59009192005-05-04 19:12:09 +0000109 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000110
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000111 //If this didn't legalize into a div....
112 // setOperationAction(ISD::SREM , MVT::i64, Expand);
113 // setOperationAction(ISD::UREM , MVT::i64, Expand);
114
115 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
116 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
117 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
Andrew Lenharth9818c052005-02-05 13:19:12 +0000118
Chris Lattner17234b72005-04-30 04:26:06 +0000119 // We don't support sin/cos/sqrt
120 setOperationAction(ISD::FSIN , MVT::f64, Expand);
121 setOperationAction(ISD::FCOS , MVT::f64, Expand);
122 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
123 setOperationAction(ISD::FSIN , MVT::f32, Expand);
124 setOperationAction(ISD::FCOS , MVT::f32, Expand);
125 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
126
Andrew Lenharth33819132005-03-04 20:09:23 +0000127 //Doesn't work yet
Chris Lattner17234b72005-04-30 04:26:06 +0000128 setOperationAction(ISD::SETCC, MVT::f32, Promote);
Andrew Lenharth572af902005-02-14 05:41:43 +0000129
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000130 //Try a couple things with a custom expander
131 //setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
132
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000133 computeRegisterProperties();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000134
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000135 addLegalFPImmediate(+0.0); //F31
136 addLegalFPImmediate(-0.0); //-F31
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000137 }
138
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000139 /// LowerOperation - Provide custom lowering hooks for some operations.
140 ///
141 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
142
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000143 /// LowerArguments - This hook must be implemented to indicate how we should
144 /// lower the arguments for the specified function, into the specified DAG.
145 virtual std::vector<SDOperand>
146 LowerArguments(Function &F, SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000147
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000148 /// LowerCallTo - This hook lowers an abstract call to a function into an
149 /// actual call.
150 virtual std::pair<SDOperand, SDOperand>
Chris Lattnerc57f6822005-05-12 19:56:45 +0000151 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattneradf6a962005-05-13 18:50:42 +0000152 bool isTailCall, SDOperand Callee, ArgListTy &Args,
153 SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000154
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000155 virtual std::pair<SDOperand, SDOperand>
Andrew Lenharth558bc882005-06-18 18:34:52 +0000156 LowerVAStart(SDOperand Chain, SelectionDAG &DAG, SDOperand Dest);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000157
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000158 virtual std::pair<SDOperand,SDOperand>
Andrew Lenharth558bc882005-06-18 18:34:52 +0000159 LowerVAArgNext(SDOperand Chain, SDOperand VAList,
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000160 const Type *ArgTy, SelectionDAG &DAG);
161
162 virtual std::pair<SDOperand, SDOperand>
163 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
164 SelectionDAG &DAG);
165
166 void restoreGP(MachineBasicBlock* BB)
167 {
168 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
169 }
170 };
171}
172
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000173/// LowerOperation - Provide custom lowering hooks for some operations.
174///
175SDOperand AlphaTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
176 MachineFunction &MF = DAG.getMachineFunction();
177 switch (Op.getOpcode()) {
178 default: assert(0 && "Should not custom lower this!");
Misha Brukmanb8ee91a2005-06-06 17:39:46 +0000179#if 0
180 case ISD::SINT_TO_FP:
181 {
182 assert (Op.getOperand(0).getValueType() == MVT::i64
183 && "only quads can be loaded from");
184 SDOperand SRC;
185 if (EnableAlphaFTOI)
186 {
187 std::vector<MVT::ValueType> RTs;
188 RTs.push_back(Op.getValueType());
189 std::vector<SDOperand> Ops;
190 Ops.push_back(Op.getOperand(0));
191 SRC = DAG.getNode(AlphaISD::ITOF, RTs, Ops);
192 } else {
193 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
194 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
195 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
196 Op.getOperand(0), StackSlot, DAG.getSrcValue(NULL));
197 SRC = DAG.getLoad(Op.getValueType(), Store.getValue(0), StackSlot,
198 DAG.getSrcValue(NULL));
199 }
200 std::vector<MVT::ValueType> RTs;
201 RTs.push_back(Op.getValueType());
202 std::vector<SDOperand> Ops;
203 Ops.push_back(SRC);
204 return DAG.getNode(AlphaISD::CVTQ, RTs, Ops);
205 }
206#endif
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000207 }
Misha Brukmanb8ee91a2005-06-06 17:39:46 +0000208 return SDOperand();
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000209}
210
211
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000212/// AddLiveIn - This helper function adds the specified physical register to the
213/// MachineFunction as a live in value. It also creates a corresponding virtual
214/// register for it.
215static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
216 TargetRegisterClass *RC) {
217 assert(RC->contains(PReg) && "Not the correct regclass!");
218 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
219 MF.addLiveIn(PReg, VReg);
220 return VReg;
221}
222
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000223//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
224
225//For now, just use variable size stack frame format
226
227//In a standard call, the first six items are passed in registers $16
228//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
229//of argument-to-register correspondence.) The remaining items are
230//collected in a memory argument list that is a naturally aligned
231//array of quadwords. In a standard call, this list, if present, must
232//be passed at 0(SP).
Misha Brukman7847fca2005-04-22 17:54:37 +0000233//7 ... n 0(SP) ... (n-7)*8(SP)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000234
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000235// //#define FP $15
236// //#define RA $26
237// //#define PV $27
238// //#define GP $29
239// //#define SP $30
Misha Brukman4633f1c2005-04-21 23:13:11 +0000240
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000241std::vector<SDOperand>
Misha Brukman4633f1c2005-04-21 23:13:11 +0000242AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000243{
244 std::vector<SDOperand> ArgValues;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000245
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000246 MachineFunction &MF = DAG.getMachineFunction();
Andrew Lenharth05380342005-02-07 05:07:00 +0000247 MachineFrameInfo*MFI = MF.getFrameInfo();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000248
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000249 MachineBasicBlock& BB = MF.front();
250
251 //Handle the return address
252 //BuildMI(&BB, Alpha::IDEF, 0, Alpha::R26);
253
Misha Brukman4633f1c2005-04-21 23:13:11 +0000254 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000255 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000256 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000257 Alpha::F19, Alpha::F20, Alpha::F21};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000258 int count = 0;
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000259
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000260 GP = AddLiveIn(MF, Alpha::R29, getRegClassFor(MVT::i64));
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000261
Chris Lattnere4d5c442005-03-15 04:54:21 +0000262 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000263 {
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000264 SDOperand argt;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000265 if (count < 6) {
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000266 unsigned Vreg;
267 MVT::ValueType VT = getValueType(I->getType());
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000268 switch (VT) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000269 default:
270 std::cerr << "Unknown Type " << VT << "\n";
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000271 abort();
272 case MVT::f64:
273 case MVT::f32:
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000274 args_float[count] = AddLiveIn(MF,args_float[count], getRegClassFor(VT));
275 argt = DAG.getCopyFromReg(args_float[count], VT, DAG.getRoot());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000276 break;
277 case MVT::i1:
278 case MVT::i8:
279 case MVT::i16:
280 case MVT::i32:
281 case MVT::i64:
Andrew Lenharth591ec572005-05-31 18:42:18 +0000282 args_int[count] = AddLiveIn(MF, args_int[count], getRegClassFor(MVT::i64));
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000283 argt = DAG.getCopyFromReg(args_int[count], VT, DAG.getRoot());
Andrew Lenharth14f30c92005-05-31 18:37:16 +0000284 if (VT != MVT::i64)
285 argt = DAG.getNode(ISD::TRUNCATE, VT, argt);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000286 break;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000287 }
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000288 DAG.setRoot(argt.getValue(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000289 } else { //more args
290 // Create the frame index object for this incoming parameter...
291 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000292
293 // Create the SelectionDAG nodes corresponding to a load
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000294 //from this parameter
295 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000296 argt = DAG.getLoad(getValueType(I->getType()),
297 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000298 }
Andrew Lenharth032f2352005-02-22 21:59:48 +0000299 ++count;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000300 ArgValues.push_back(argt);
301 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000302
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000303 // If the functions takes variable number of arguments, copy all regs to stack
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000304 if (F.isVarArg()) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000305 VarArgsOffset = count * 8;
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000306 std::vector<SDOperand> LS;
307 for (int i = 0; i < 6; ++i) {
308 if (args_int[i] < 1024)
309 args_int[i] = AddLiveIn(MF,args_int[i], getRegClassFor(MVT::i64));
310 SDOperand argt = DAG.getCopyFromReg(args_int[i], MVT::i64, DAG.getRoot());
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000311 int FI = MFI->CreateFixedObject(8, -8 * (6 - i));
Andrew Lenharth558bc882005-06-18 18:34:52 +0000312 if (i == 0) VarArgsBase = FI;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000313 SDOperand SDFI = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000314 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt, SDFI, DAG.getSrcValue(NULL)));
315
316 if (args_float[i] < 1024)
317 args_float[i] = AddLiveIn(MF,args_float[i], getRegClassFor(MVT::f64));
318 argt = DAG.getCopyFromReg(args_float[i], MVT::f64, DAG.getRoot());
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000319 FI = MFI->CreateFixedObject(8, - 8 * (12 - i));
320 SDFI = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000321 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt, SDFI, DAG.getSrcValue(NULL)));
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000322 }
323
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000324 //Set up a token factor with all the stack traffic
325 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, LS));
326 }
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000327
328 // Finally, inform the code generator which regs we return values in.
329 switch (getValueType(F.getReturnType())) {
330 default: assert(0 && "Unknown type!");
331 case MVT::isVoid: break;
332 case MVT::i1:
333 case MVT::i8:
334 case MVT::i16:
335 case MVT::i32:
336 case MVT::i64:
337 MF.addLiveOut(Alpha::R0);
338 break;
339 case MVT::f32:
340 case MVT::f64:
341 MF.addLiveOut(Alpha::F0);
342 break;
343 }
344
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000345 //return the arguments
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000346 return ArgValues;
347}
348
349std::pair<SDOperand, SDOperand>
350AlphaTargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000351 const Type *RetTy, bool isVarArg,
Chris Lattneradf6a962005-05-13 18:50:42 +0000352 unsigned CallingConv, bool isTailCall,
Misha Brukman7847fca2005-04-22 17:54:37 +0000353 SDOperand Callee, ArgListTy &Args,
354 SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000355 int NumBytes = 0;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000356 if (Args.size() > 6)
357 NumBytes = (Args.size() - 6) * 8;
358
Chris Lattner16cd04d2005-05-12 23:24:06 +0000359 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000360 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000361 std::vector<SDOperand> args_to_use;
362 for (unsigned i = 0, e = Args.size(); i != e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000363 {
364 switch (getValueType(Args[i].second)) {
365 default: assert(0 && "Unexpected ValueType for argument!");
366 case MVT::i1:
367 case MVT::i8:
368 case MVT::i16:
369 case MVT::i32:
370 // Promote the integer to 64 bits. If the input type is signed use a
371 // sign extend, otherwise use a zero extend.
372 if (Args[i].second->isSigned())
373 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
374 else
375 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
376 break;
377 case MVT::i64:
378 case MVT::f64:
379 case MVT::f32:
380 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000381 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000382 args_to_use.push_back(Args[i].first);
383 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000384
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000385 std::vector<MVT::ValueType> RetVals;
386 MVT::ValueType RetTyVT = getValueType(RetTy);
387 if (RetTyVT != MVT::isVoid)
388 RetVals.push_back(RetTyVT);
389 RetVals.push_back(MVT::Other);
390
Misha Brukman4633f1c2005-04-21 23:13:11 +0000391 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000392 Chain, Callee, args_to_use), 0);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000393 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner16cd04d2005-05-12 23:24:06 +0000394 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000395 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000396 return std::make_pair(TheCall, Chain);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000397}
398
399std::pair<SDOperand, SDOperand>
Andrew Lenharth558bc882005-06-18 18:34:52 +0000400AlphaTargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG, SDOperand Dest) {
401 // vastart just stores the address of the VarArgsBase and VarArgsOffset
402 SDOperand FR = DAG.getFrameIndex(VarArgsBase, MVT::i32);
403 SDOperand S1 = DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, Dest, DAG.getSrcValue(NULL));
404 SDOperand SA2 = DAG.getNode(ISD::ADD, MVT::i64, Dest, DAG.getConstant(8, MVT::i64));
405 SDOperand S2 = DAG.getNode(ISD::STORE, MVT::Other, S1,
406 DAG.getConstant(VarArgsOffset, MVT::i64), SA2,
407 DAG.getSrcValue(NULL));
408
409 return std::make_pair(S2, S2);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000410}
411
412std::pair<SDOperand,SDOperand> AlphaTargetLowering::
Andrew Lenharth558bc882005-06-18 18:34:52 +0000413LowerVAArgNext(SDOperand Chain, SDOperand VAList,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000414 const Type *ArgTy, SelectionDAG &DAG) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000415 //FIXME: For now, ignore FP
416 SDOperand Base = DAG.getLoad(MVT::i64, Chain, VAList, DAG.getSrcValue(NULL));
417 SDOperand Tmp = DAG.getNode(ISD::ADD, MVT::i64, VAList,
418 DAG.getConstant(8, MVT::i64));
419 SDOperand Offset = DAG.getLoad(MVT::i64, Chain, Tmp, DAG.getSrcValue(NULL));
420 SDOperand DataPtr = DAG.getNode(ISD::ADD, MVT::i64, Base, Offset);
421 SDOperand Result = DAG.getLoad(MVT::i64, Chain, DataPtr,
422 DAG.getSrcValue(NULL));
423 SDOperand NewOffset = DAG.getNode(ISD::ADD, MVT::i64, Offset,
424 DAG.getConstant(8, MVT::i64));
425 SDOperand Update = DAG.getNode(ISD::STORE, MVT::Other, Result, NewOffset,
426 Tmp, DAG.getSrcValue(NULL));
427 return std::make_pair(Result, Update);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000428}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000429
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000430
431std::pair<SDOperand, SDOperand> AlphaTargetLowering::
432LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
433 SelectionDAG &DAG) {
434 abort();
435}
436
437
438
439
440
441namespace {
442
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000443//===--------------------------------------------------------------------===//
444/// ISel - Alpha specific code to select Alpha machine instructions for
445/// SelectionDAG operations.
446//===--------------------------------------------------------------------===//
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000447class AlphaISel : public SelectionDAGISel {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000448
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000449 /// AlphaLowering - This object fully describes how to lower LLVM code to an
450 /// Alpha-specific SelectionDAG.
451 AlphaTargetLowering AlphaLowering;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000452
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000453 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
454 // for sdiv and udiv until it is put into the future
455 // dag combiner.
456
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000457 /// ExprMap - As shared expressions are codegen'd, we keep track of which
458 /// vreg the value is produced in, so we only emit one copy of each compiled
459 /// tree.
460 static const unsigned notIn = (unsigned)(-1);
461 std::map<SDOperand, unsigned> ExprMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000462
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000463 //CCInvMap sometimes (SetNE) we have the inverse CC code for free
464 std::map<SDOperand, unsigned> CCInvMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000465
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000466 int count_ins;
467 int count_outs;
468 bool has_sym;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000469 int max_depth;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000470
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000471public:
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000472 AlphaISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering), AlphaLowering(TM)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000473 {}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000474
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000475 /// InstructionSelectBasicBlock - This callback is invoked by
476 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
477 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
Andrew Lenharth032f2352005-02-22 21:59:48 +0000478 DEBUG(BB->dump());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000479 count_ins = 0;
480 count_outs = 0;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000481 max_depth = 0;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000482 has_sym = false;
483
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000484 // Codegen the basic block.
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000485 ISelDAG = &DAG;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000486 max_depth = DAG.getRoot().getNodeDepth();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000487 Select(DAG.getRoot());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000488
489 if(has_sym)
490 ++count_ins;
491 if(EnableAlphaCount)
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000492 std::cerr << "COUNT: " << BB->getParent()->getFunction ()->getName() << " "
493 << BB->getNumber() << " "
494 << max_depth << " "
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000495 << count_ins << " "
496 << count_outs << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000497
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000498 // Clear state used for selection.
499 ExprMap.clear();
500 CCInvMap.clear();
501 }
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000502
503 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000504
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000505 unsigned SelectExpr(SDOperand N);
506 unsigned SelectExprFP(SDOperand N, unsigned Result);
507 void Select(SDOperand N);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000508
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000509 void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
510 void SelectBranchCC(SDOperand N);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000511 void MoveFP2Int(unsigned src, unsigned dst, bool isDouble);
512 void MoveInt2FP(unsigned src, unsigned dst, bool isDouble);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000513 //returns whether the sense of the comparison was inverted
514 bool SelectFPSetCC(SDOperand N, unsigned dst);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000515
516 // dag -> dag expanders for integer divide by constant
517 SDOperand BuildSDIVSequence(SDOperand N);
518 SDOperand BuildUDIVSequence(SDOperand N);
519
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000520};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000521}
522
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000523void AlphaISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000524 // If this function has live-in values, emit the copies from pregs to vregs at
525 // the top of the function, before anything else.
526 MachineBasicBlock *BB = MF.begin();
527 if (MF.livein_begin() != MF.livein_end()) {
528 SSARegMap *RegMap = MF.getSSARegMap();
529 for (MachineFunction::livein_iterator LI = MF.livein_begin(),
530 E = MF.livein_end(); LI != E; ++LI) {
531 const TargetRegisterClass *RC = RegMap->getRegClass(LI->second);
532 if (RC == Alpha::GPRCRegisterClass) {
533 BuildMI(BB, Alpha::BIS, 2, LI->second).addReg(LI->first).addReg(LI->first);
534 } else if (RC == Alpha::FPRCRegisterClass) {
535 BuildMI(BB, Alpha::CPYS, 2, LI->second).addReg(LI->first).addReg(LI->first);
536 } else {
537 assert(0 && "Unknown regclass!");
538 }
539 }
540 }
541}
542
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000543//Find the offset of the arg in it's parent's function
544static int getValueOffset(const Value* v)
545{
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000546 static int uniqneg = -1;
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000547 if (v == NULL)
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000548 return uniqneg--;
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000549
550 const Instruction* itarget = dyn_cast<Instruction>(v);
551 const BasicBlock* btarget = itarget->getParent();
552 const Function* ftarget = btarget->getParent();
553
554 //offset due to earlier BBs
555 int i = 0;
556 for(Function::const_iterator ii = ftarget->begin(); &*ii != btarget; ++ii)
557 i += ii->size();
558
559 for(BasicBlock::const_iterator ii = btarget->begin(); &*ii != itarget; ++ii)
560 ++i;
561
562 return i;
563}
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000564//Find the offset of the function in it's module
565static int getFunctionOffset(const Function* fun)
566{
567 const Module* M = fun->getParent();
568
569 //offset due to earlier BBs
570 int i = 0;
571 for(Module::const_iterator ii = M->begin(); &*ii != fun; ++ii)
572 ++i;
573
574 return i;
575}
576
577static int getUID()
578{
579 static int id = 0;
580 return ++id;
581}
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000582
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000583//Factorize a number using the list of constants
584static bool factorize(int v[], int res[], int size, uint64_t c)
585{
586 bool cont = true;
587 while (c != 1 && cont)
588 {
589 cont = false;
590 for(int i = 0; i < size; ++i)
591 {
592 if (c % v[i] == 0)
593 {
594 c /= v[i];
595 ++res[i];
596 cont=true;
597 }
598 }
599 }
600 return c == 1;
601}
602
603
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000604//Shamelessly adapted from PPC32
Misha Brukman4633f1c2005-04-21 23:13:11 +0000605// Structure used to return the necessary information to codegen an SDIV as
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000606// a multiply.
607struct ms {
608 int64_t m; // magic number
609 int64_t s; // shift amount
610};
611
612struct mu {
613 uint64_t m; // magic number
614 int64_t a; // add indicator
615 int64_t s; // shift amount
616};
617
618/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukman4633f1c2005-04-21 23:13:11 +0000619/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000620/// or -1.
621static struct ms magic(int64_t d) {
622 int64_t p;
623 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
624 const uint64_t two63 = 9223372036854775808ULL; // 2^63
625 struct ms mag;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000626
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000627 ad = abs(d);
628 t = two63 + ((uint64_t)d >> 63);
629 anc = t - 1 - t%ad; // absolute value of nc
Andrew Lenharth320174f2005-04-07 17:19:16 +0000630 p = 63; // initialize p
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000631 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
632 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
633 q2 = two63/ad; // initialize q2 = 2p/abs(d)
634 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
635 do {
636 p = p + 1;
637 q1 = 2*q1; // update q1 = 2p/abs(nc)
638 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
639 if (r1 >= anc) { // must be unsigned comparison
640 q1 = q1 + 1;
641 r1 = r1 - anc;
642 }
643 q2 = 2*q2; // update q2 = 2p/abs(d)
644 r2 = 2*r2; // update r2 = rem(2p/abs(d))
645 if (r2 >= ad) { // must be unsigned comparison
646 q2 = q2 + 1;
647 r2 = r2 - ad;
648 }
649 delta = ad - r2;
650 } while (q1 < delta || (q1 == delta && r1 == 0));
651
652 mag.m = q2 + 1;
653 if (d < 0) mag.m = -mag.m; // resulting magic number
654 mag.s = p - 64; // resulting shift
655 return mag;
656}
657
658/// magicu - calculate the magic numbers required to codegen an integer udiv as
659/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
660static struct mu magicu(uint64_t d)
661{
662 int64_t p;
663 uint64_t nc, delta, q1, r1, q2, r2;
664 struct mu magu;
665 magu.a = 0; // initialize "add" indicator
666 nc = - 1 - (-d)%d;
Andrew Lenharth320174f2005-04-07 17:19:16 +0000667 p = 63; // initialize p
668 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
669 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
670 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
671 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000672 do {
673 p = p + 1;
674 if (r1 >= nc - r1 ) {
675 q1 = 2*q1 + 1; // update q1
676 r1 = 2*r1 - nc; // update r1
677 }
678 else {
679 q1 = 2*q1; // update q1
680 r1 = 2*r1; // update r1
681 }
682 if (r2 + 1 >= d - r2) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000683 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000684 q2 = 2*q2 + 1; // update q2
685 r2 = 2*r2 + 1 - d; // update r2
686 }
687 else {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000688 if (q2 >= 0x8000000000000000ull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000689 q2 = 2*q2; // update q2
690 r2 = 2*r2 + 1; // update r2
691 }
692 delta = d - 1 - r2;
693 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
694 magu.m = q2 + 1; // resulting magic number
Andrew Lenharth320174f2005-04-07 17:19:16 +0000695 magu.s = p - 64; // resulting shift
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000696 return magu;
697}
698
699/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
700/// return a DAG expression to select that will generate the same value by
701/// multiplying by a magic number. See:
702/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000703SDOperand AlphaISel::BuildSDIVSequence(SDOperand N) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000704 int64_t d = (int64_t)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000705 ms magics = magic(d);
706 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000707 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000708 ISelDAG->getConstant(magics.m, MVT::i64));
709 // If d > 0 and m < 0, add the numerator
710 if (d > 0 && magics.m < 0)
711 Q = ISelDAG->getNode(ISD::ADD, MVT::i64, Q, N.getOperand(0));
712 // If d < 0 and m > 0, subtract the numerator.
713 if (d < 0 && magics.m > 0)
714 Q = ISelDAG->getNode(ISD::SUB, MVT::i64, Q, N.getOperand(0));
715 // Shift right algebraic if shift value is nonzero
716 if (magics.s > 0)
Misha Brukman4633f1c2005-04-21 23:13:11 +0000717 Q = ISelDAG->getNode(ISD::SRA, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000718 ISelDAG->getConstant(magics.s, MVT::i64));
719 // Extract the sign bit and add it to the quotient
Misha Brukman4633f1c2005-04-21 23:13:11 +0000720 SDOperand T =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000721 ISelDAG->getNode(ISD::SRL, MVT::i64, Q, ISelDAG->getConstant(63, MVT::i64));
722 return ISelDAG->getNode(ISD::ADD, MVT::i64, Q, T);
723}
724
725/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
726/// return a DAG expression to select that will generate the same value by
727/// multiplying by a magic number. See:
728/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000729SDOperand AlphaISel::BuildUDIVSequence(SDOperand N) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000730 unsigned d =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000731 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
732 mu magics = magicu(d);
733 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000734 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000735 ISelDAG->getConstant(magics.m, MVT::i64));
736 if (magics.a == 0) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000737 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000738 ISelDAG->getConstant(magics.s, MVT::i64));
739 } else {
740 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i64, N.getOperand(0), Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000741 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000742 ISelDAG->getConstant(1, MVT::i64));
743 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i64, NPQ, Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000744 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000745 ISelDAG->getConstant(magics.s-1, MVT::i64));
746 }
747 return Q;
748}
749
Andrew Lenhartha565c272005-04-06 22:03:13 +0000750//From PPC32
751/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
752/// returns zero when the input is not exactly a power of two.
753static unsigned ExactLog2(uint64_t Val) {
754 if (Val == 0 || (Val & (Val-1))) return 0;
755 unsigned Count = 0;
756 while (Val != 1) {
757 Val >>= 1;
758 ++Count;
759 }
760 return Count;
761}
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000762
763
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000764//These describe LDAx
Andrew Lenharthc0513832005-03-29 19:24:04 +0000765static const int IMM_LOW = -32768;
766static const int IMM_HIGH = 32767;
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000767static const int IMM_MULT = 65536;
768
769static long getUpper16(long l)
770{
771 long y = l / IMM_MULT;
772 if (l % IMM_MULT > IMM_HIGH)
773 ++y;
774 return y;
775}
776
777static long getLower16(long l)
778{
779 long h = getUpper16(l);
780 return l - h * IMM_MULT;
781}
782
Andrew Lenharth65838902005-02-06 16:22:15 +0000783static unsigned GetSymVersion(unsigned opcode)
784{
785 switch (opcode) {
786 default: assert(0 && "unknown load or store"); return 0;
787 case Alpha::LDQ: return Alpha::LDQ_SYM;
788 case Alpha::LDS: return Alpha::LDS_SYM;
789 case Alpha::LDT: return Alpha::LDT_SYM;
790 case Alpha::LDL: return Alpha::LDL_SYM;
791 case Alpha::LDBU: return Alpha::LDBU_SYM;
792 case Alpha::LDWU: return Alpha::LDWU_SYM;
793 case Alpha::LDW: return Alpha::LDW_SYM;
794 case Alpha::LDB: return Alpha::LDB_SYM;
795 case Alpha::STQ: return Alpha::STQ_SYM;
796 case Alpha::STS: return Alpha::STS_SYM;
797 case Alpha::STT: return Alpha::STT_SYM;
798 case Alpha::STL: return Alpha::STL_SYM;
799 case Alpha::STW: return Alpha::STW_SYM;
800 case Alpha::STB: return Alpha::STB_SYM;
801 }
802}
803
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000804void AlphaISel::MoveFP2Int(unsigned src, unsigned dst, bool isDouble)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000805{
806 unsigned Opc;
807 if (EnableAlphaFTOI) {
808 Opc = isDouble ? Alpha::FTOIT : Alpha::FTOIS;
809 BuildMI(BB, Opc, 1, dst).addReg(src);
810 } else {
811 //The hard way:
812 // Spill the integer to memory and reload it from there.
813 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
814 MachineFunction *F = BB->getParent();
815 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
816
817 Opc = isDouble ? Alpha::STT : Alpha::STS;
818 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
819 Opc = isDouble ? Alpha::LDQ : Alpha::LDL;
820 BuildMI(BB, Alpha::LDQ, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
821 }
822}
823
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000824void AlphaISel::MoveInt2FP(unsigned src, unsigned dst, bool isDouble)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000825{
826 unsigned Opc;
827 if (EnableAlphaFTOI) {
828 Opc = isDouble?Alpha::ITOFT:Alpha::ITOFS;
829 BuildMI(BB, Opc, 1, dst).addReg(src);
830 } else {
831 //The hard way:
832 // Spill the integer to memory and reload it from there.
833 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
834 MachineFunction *F = BB->getParent();
835 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
836
837 Opc = isDouble ? Alpha::STQ : Alpha::STL;
838 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
839 Opc = isDouble ? Alpha::LDT : Alpha::LDS;
840 BuildMI(BB, Opc, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
841 }
842}
843
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000844bool AlphaISel::SelectFPSetCC(SDOperand N, unsigned dst)
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000845{
846 SDNode *Node = N.Val;
847 unsigned Opc, Tmp1, Tmp2, Tmp3;
848 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node);
849
850 //assert(SetCC->getOperand(0).getValueType() != MVT::f32 && "SetCC f32 should have been promoted");
851 bool rev = false;
852 bool inv = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000853
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000854 switch (SetCC->getCondition()) {
855 default: Node->dump(); assert(0 && "Unknown FP comparison!");
856 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
857 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
858 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
859 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
860 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
861 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
862 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000863
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000864 //FIXME: check for constant 0.0
865 ConstantFPSDNode *CN;
866 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
867 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
868 Tmp1 = Alpha::F31;
869 else
870 Tmp1 = SelectExpr(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000871
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000872 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
873 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
874 Tmp2 = Alpha::F31;
875 else
Chris Lattner9c9183a2005-04-30 04:44:07 +0000876 Tmp2 = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000877
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000878 //Can only compare doubles, and dag won't promote for me
879 if (SetCC->getOperand(0).getValueType() == MVT::f32)
880 {
881 //assert(0 && "Setcc On float?\n");
882 std::cerr << "Setcc on float!\n";
883 Tmp3 = MakeReg(MVT::f64);
884 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
885 Tmp1 = Tmp3;
886 }
887 if (SetCC->getOperand(1).getValueType() == MVT::f32)
888 {
889 //assert (0 && "Setcc On float?\n");
890 std::cerr << "Setcc on float!\n";
891 Tmp3 = MakeReg(MVT::f64);
892 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp2);
893 Tmp2 = Tmp3;
894 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000895
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000896 if (rev) std::swap(Tmp1, Tmp2);
897 //do the comparison
898 BuildMI(BB, Opc, 2, dst).addReg(Tmp1).addReg(Tmp2);
899 return inv;
900}
901
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000902//Check to see if the load is a constant offset from a base register
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000903void AlphaISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000904{
905 unsigned opcode = N.getOpcode();
906 if (opcode == ISD::ADD) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000907 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000908 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
909 { //Normal imm add
910 Reg = SelectExpr(N.getOperand(0));
911 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
912 return;
913 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000914 else if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000915 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 32767)
916 {
917 Reg = SelectExpr(N.getOperand(1));
918 offset = cast<ConstantSDNode>(N.getOperand(0))->getValue();
919 return;
920 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000921 }
922 Reg = SelectExpr(N);
923 offset = 0;
924 return;
925}
926
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000927void AlphaISel::SelectBranchCC(SDOperand N)
Andrew Lenharth445171a2005-02-08 00:40:03 +0000928{
929 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000930 MachineBasicBlock *Dest =
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000931 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
932 unsigned Opc = Alpha::WTF;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000933
Andrew Lenharth445171a2005-02-08 00:40:03 +0000934 Select(N.getOperand(0)); //chain
935 SDOperand CC = N.getOperand(1);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000936
Andrew Lenharth445171a2005-02-08 00:40:03 +0000937 if (CC.getOpcode() == ISD::SETCC)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000938 {
939 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
940 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
941 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000942 bool LeftZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
943 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
Andrew Lenharth09552bf2005-06-08 18:02:21 +0000944 bool RightZero = SetCC->getOperand(1).getOpcode() == ISD::Constant &&
945 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000946 bool isNE = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000947
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000948 //Fix up CC
949 ISD::CondCode cCode= SetCC->getCondition();
950 if (LeftZero && !RightZero) //Swap Operands
951 cCode = ISD::getSetCCSwappedOperands(cCode);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000952
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000953 if(cCode == ISD::SETNE)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000954 isNE = true;
Andrew Lenharth445171a2005-02-08 00:40:03 +0000955
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000956 if (LeftZero || RightZero) {
Andrew Lenharth09552bf2005-06-08 18:02:21 +0000957 switch (cCode) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000958 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
959 case ISD::SETEQ: Opc = Alpha::BEQ; break;
960 case ISD::SETLT: Opc = Alpha::BLT; break;
961 case ISD::SETLE: Opc = Alpha::BLE; break;
962 case ISD::SETGT: Opc = Alpha::BGT; break;
963 case ISD::SETGE: Opc = Alpha::BGE; break;
964 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
965 case ISD::SETUGT: Opc = Alpha::BNE; break;
966 case ISD::SETULE: Opc = Alpha::BEQ; break; //Technically you could have this CC
967 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
968 case ISD::SETNE: Opc = Alpha::BNE; break;
969 }
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000970 unsigned Tmp1;
971 if(LeftZero && !RightZero) //swap Operands
972 Tmp1 = SelectExpr(SetCC->getOperand(1)); //Cond
973 else
974 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000975 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
976 return;
977 } else {
978 unsigned Tmp1 = SelectExpr(CC);
979 if (isNE)
980 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
981 else
982 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000983 return;
984 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000985 } else { //FP
986 //Any comparison between 2 values should be codegened as an folded branch, as moving
987 //CC to the integer register is very expensive
988 //for a cmp b: c = a - b;
989 //a = b: c = 0
990 //a < b: c < 0
991 //a > b: c > 0
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000992
993 bool invTest = false;
994 unsigned Tmp3;
995
996 ConstantFPSDNode *CN;
997 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
998 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
999 Tmp3 = SelectExpr(SetCC->getOperand(0));
1000 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
1001 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1002 {
1003 Tmp3 = SelectExpr(SetCC->getOperand(1));
1004 invTest = true;
1005 }
1006 else
1007 {
1008 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
1009 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
1010 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
1011 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1012 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1013 .addReg(Tmp1).addReg(Tmp2);
1014 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001015
1016 switch (SetCC->getCondition()) {
1017 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001018 case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
1019 case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
1020 case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
1021 case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
1022 case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
1023 case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001024 }
1025 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001026 return;
1027 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001028 abort(); //Should never be reached
1029 } else {
1030 //Giveup and do the stupid thing
1031 unsigned Tmp1 = SelectExpr(CC);
1032 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
1033 return;
1034 }
Andrew Lenharth445171a2005-02-08 00:40:03 +00001035 abort(); //Should never be reached
1036}
1037
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001038unsigned AlphaISel::SelectExprFP(SDOperand N, unsigned Result)
Andrew Lenharth40831c52005-01-28 06:57:18 +00001039{
1040 unsigned Tmp1, Tmp2, Tmp3;
1041 unsigned Opc = 0;
1042 SDNode *Node = N.Val;
1043 MVT::ValueType DestType = N.getValueType();
1044 unsigned opcode = N.getOpcode();
1045
1046 switch (opcode) {
1047 default:
1048 Node->dump();
1049 assert(0 && "Node not handled!\n");
Andrew Lenharth2c594352005-01-29 15:42:07 +00001050
Andrew Lenharth7332f3e2005-04-02 19:11:07 +00001051 case ISD::UNDEF: {
1052 BuildMI(BB, Alpha::IDEF, 0, Result);
1053 return Result;
1054 }
1055
Andrew Lenharth30b46d42005-04-02 19:04:58 +00001056 case ISD::FNEG:
1057 if(ISD::FABS == N.getOperand(0).getOpcode())
1058 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001059 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1060 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +00001061 } else {
Misha Brukman7847fca2005-04-22 17:54:37 +00001062 Tmp1 = SelectExpr(N.getOperand(0));
1063 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +00001064 }
1065 return Result;
1066
1067 case ISD::FABS:
1068 Tmp1 = SelectExpr(N.getOperand(0));
1069 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
1070 return Result;
1071
Andrew Lenharth9818c052005-02-05 13:19:12 +00001072 case ISD::SELECT:
1073 {
Andrew Lenharth45859692005-03-03 21:47:53 +00001074 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1075 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
1076 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
1077
1078 SDOperand CC = N.getOperand(0);
1079 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
1080
Misha Brukman4633f1c2005-04-21 23:13:11 +00001081 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth45859692005-03-03 21:47:53 +00001082 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
1083 { //FP Setcc -> Select yay!
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001084
1085
Andrew Lenharth45859692005-03-03 21:47:53 +00001086 //for a cmp b: c = a - b;
1087 //a = b: c = 0
1088 //a < b: c < 0
1089 //a > b: c > 0
Misha Brukman4633f1c2005-04-21 23:13:11 +00001090
Andrew Lenharth45859692005-03-03 21:47:53 +00001091 bool invTest = false;
1092 unsigned Tmp3;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001093
Andrew Lenharth45859692005-03-03 21:47:53 +00001094 ConstantFPSDNode *CN;
1095 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
1096 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1097 Tmp3 = SelectExpr(SetCC->getOperand(0));
1098 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
1099 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1100 {
1101 Tmp3 = SelectExpr(SetCC->getOperand(1));
1102 invTest = true;
1103 }
1104 else
1105 {
1106 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
1107 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
1108 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
1109 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1110 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1111 .addReg(Tmp1).addReg(Tmp2);
1112 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001113
Andrew Lenharth45859692005-03-03 21:47:53 +00001114 switch (SetCC->getCondition()) {
1115 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
1116 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNE : Alpha::FCMOVEQ; break;
1117 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGT : Alpha::FCMOVLT; break;
1118 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGE : Alpha::FCMOVLE; break;
1119 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLT : Alpha::FCMOVGT; break;
1120 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLE : Alpha::FCMOVGE; break;
1121 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQ : Alpha::FCMOVNE; break;
1122 }
Andrew Lenharth33819132005-03-04 20:09:23 +00001123 BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
Andrew Lenharth45859692005-03-03 21:47:53 +00001124 return Result;
1125 }
1126 else
1127 {
1128 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001129 BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV).addReg(Tmp1);
1130// // Spill the cond to memory and reload it from there.
1131// unsigned Tmp4 = MakeReg(MVT::f64);
1132// MoveIntFP(Tmp1, Tmp4, true);
1133// //now ideally, we don't have to do anything to the flag...
1134// // Get the condition into the zero flag.
1135// BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
Andrew Lenharth45859692005-03-03 21:47:53 +00001136 return Result;
1137 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001138 }
1139
Andrew Lenharthc1faced2005-02-01 01:37:24 +00001140 case ISD::FP_ROUND:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001141 assert (DestType == MVT::f32 &&
1142 N.getOperand(0).getValueType() == MVT::f64 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001143 "only f64 to f32 conversion supported here");
Andrew Lenharthc1faced2005-02-01 01:37:24 +00001144 Tmp1 = SelectExpr(N.getOperand(0));
1145 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
1146 return Result;
1147
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001148 case ISD::FP_EXTEND:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001149 assert (DestType == MVT::f64 &&
1150 N.getOperand(0).getValueType() == MVT::f32 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001151 "only f32 to f64 conversion supported here");
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001152 Tmp1 = SelectExpr(N.getOperand(0));
1153 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
1154 return Result;
1155
Andrew Lenharth2c594352005-01-29 15:42:07 +00001156 case ISD::CopyFromReg:
1157 {
1158 // Make sure we generate both values.
1159 if (Result != notIn)
1160 ExprMap[N.getValue(1)] = notIn; // Generate the token
1161 else
1162 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001163
Andrew Lenharth2c594352005-01-29 15:42:07 +00001164 SDOperand Chain = N.getOperand(0);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001165
Andrew Lenharth2c594352005-01-29 15:42:07 +00001166 Select(Chain);
1167 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1168 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1169 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
1170 return Result;
1171 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001172
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001173 case ISD::LOAD:
1174 {
1175 // Make sure we generate both values.
1176 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001177 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001178 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001179 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001180
Andrew Lenharth29219162005-02-07 06:31:44 +00001181 DestType = N.getValue(0).getValueType();
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001182
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001183 SDOperand Chain = N.getOperand(0);
1184 SDOperand Address = N.getOperand(1);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001185 Select(Chain);
Andrew Lenharth65838902005-02-06 16:22:15 +00001186 Opc = DestType == MVT::f64 ? Alpha::LDT : Alpha::LDS;
1187
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +00001188 if (EnableAlphaLSMark)
1189 {
1190 int i = getValueOffset(dyn_cast<SrcValueSDNode>(N.getOperand(2))->getValue());
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001191 int j = getFunctionOffset(BB->getParent()->getFunction());
1192 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +00001193 }
1194
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001195 if (Address.getOpcode() == ISD::GlobalAddress) {
1196 AlphaLowering.restoreGP(BB);
1197 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001198 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001199 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1200 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001201 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001202 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001203 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001204 has_sym = true;
Andrew Lenharth97127a12005-02-05 17:41:39 +00001205 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001206 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001207 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001208 BuildMI(BB, Opc, 2, Result)
1209 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1210 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001211 } else {
1212 long offset;
1213 SelectAddr(Address, Tmp1, offset);
1214 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1215 }
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001216 return Result;
1217 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001218 case ISD::ConstantFP:
1219 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
1220 if (CN->isExactlyValue(+0.0)) {
1221 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001222 } else if ( CN->isExactlyValue(-0.0)) {
1223 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001224 } else {
1225 abort();
1226 }
1227 }
1228 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001229
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001230 case ISD::SDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001231 case ISD::MUL:
1232 case ISD::ADD:
1233 case ISD::SUB:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001234 switch( opcode ) {
1235 case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS; break;
1236 case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS; break;
1237 case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS; break;
1238 case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS; break;
1239 };
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001240
1241 ConstantFPSDNode *CN;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001242 if (opcode == ISD::SUB
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001243 && (CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
1244 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1245 {
1246 Tmp2 = SelectExpr(N.getOperand(1));
1247 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp2).addReg(Tmp2);
1248 } else {
1249 Tmp1 = SelectExpr(N.getOperand(0));
1250 Tmp2 = SelectExpr(N.getOperand(1));
1251 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1252 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001253 return Result;
1254
Andrew Lenharth2c594352005-01-29 15:42:07 +00001255 case ISD::EXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001256 {
1257 //include a conversion sequence for float loads to double
1258 if (Result != notIn)
1259 ExprMap[N.getValue(1)] = notIn; // Generate the token
1260 else
1261 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001262
Andrew Lenhartha549deb2005-02-07 05:33:15 +00001263 Tmp1 = MakeReg(MVT::f32);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001264
1265 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001266 "EXTLOAD not from f32");
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001267 assert(Node->getValueType(0) == MVT::f64 && "EXTLOAD not to f64");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001268
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001269 SDOperand Chain = N.getOperand(0);
1270 SDOperand Address = N.getOperand(1);
1271 Select(Chain);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001272
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001273 if (Address.getOpcode() == ISD::GlobalAddress) {
1274 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001275 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001276 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1277 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001278 else if (ConstantPoolSDNode *CP =
1279 dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001280 {
1281 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001282 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001283 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addConstantPoolIndex(CP->getIndex());
1284 }
1285 else if(Address.getOpcode() == ISD::FrameIndex) {
1286 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
Andrew Lenharth032f2352005-02-22 21:59:48 +00001287 BuildMI(BB, Alpha::LDS, 2, Tmp1)
1288 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1289 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001290 } else {
1291 long offset;
1292 SelectAddr(Address, Tmp2, offset);
1293 BuildMI(BB, Alpha::LDS, 1, Tmp1).addImm(offset).addReg(Tmp2);
1294 }
Andrew Lenharth29219162005-02-07 06:31:44 +00001295 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001296 return Result;
1297 }
Andrew Lenharth2c594352005-01-29 15:42:07 +00001298
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001299 case ISD::UINT_TO_FP:
Andrew Lenharth69520ed2005-05-26 18:18:34 +00001300 {
1301 //FIXME: First test if we will have problems with the sign bit before doing the slow thing
1302 assert (N.getOperand(0).getValueType() == MVT::i64
1303 && "only quads can be loaded from");
1304 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1305 Tmp2 = MakeReg(MVT::i64);
1306 BuildMI(BB, Alpha::SRL, 2, Tmp2).addReg(Tmp1).addImm(1);
1307 Tmp3 = MakeReg(MVT::i64);
1308 BuildMI(BB, Alpha::CMPLT, 2, Tmp3).addReg(Tmp1).addReg(Alpha::R31);
1309 unsigned Tmp4 = MakeReg(MVT::f64), Tmp5 = MakeReg(MVT::f64), Tmp6 = MakeReg(MVT::f64);
1310 MoveInt2FP(Tmp1, Tmp4, true);
1311 MoveInt2FP(Tmp2, Tmp5, true);
1312 MoveInt2FP(Tmp3, Tmp6, true);
1313 Tmp1 = MakeReg(MVT::f64);
1314 Tmp2 = MakeReg(MVT::f64);
1315 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
1316 BuildMI(BB, Opc, 1, Tmp1).addReg(Tmp4);
1317 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp5);
1318 Tmp3 = MakeReg(MVT::f64);
1319 BuildMI(BB, Alpha::ADDT, 2, Tmp3).addReg(Tmp2).addReg(Tmp2);
1320 //Ok, now tmp1 had the plain covereted
1321 //tmp3 has the reduced converted and added
1322 //tmp6 has the conditional to use
1323 BuildMI(BB, Alpha::FCMOVNE, 3, Result).addReg(Tmp1).addReg(Tmp3).addReg(Tmp6);
1324 return Result;
1325 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001326 case ISD::SINT_TO_FP:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001327 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001328 assert (N.getOperand(0).getValueType() == MVT::i64
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001329 && "only quads can be loaded from");
Andrew Lenharth40831c52005-01-28 06:57:18 +00001330 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001331 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001332 MoveInt2FP(Tmp1, Tmp2, true);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001333 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
1334 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001335 return Result;
1336 }
1337 }
1338 assert(0 && "should not get here");
1339 return 0;
1340}
1341
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001342unsigned AlphaISel::SelectExpr(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001343 unsigned Result;
Andrew Lenharth2966e842005-04-07 18:15:28 +00001344 unsigned Tmp1, Tmp2 = 0, Tmp3;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001345 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001346 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001347
1348 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001349 MVT::ValueType DestType = N.getValueType();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001350
1351 unsigned &Reg = ExprMap[N];
1352 if (Reg) return Reg;
1353
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001354 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::TAILCALL)
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001355 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001356 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001357 else {
1358 // If this is a call instruction, make sure to prepare ALL of the result
1359 // values as well as the chain.
1360 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001361 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001362 else {
1363 Result = MakeReg(Node->getValueType(0));
1364 ExprMap[N.getValue(0)] = Result;
1365 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1366 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001367 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001368 }
1369 }
1370
Andrew Lenharth50d91d72005-04-30 14:19:13 +00001371 if ((DestType == MVT::f64 || DestType == MVT::f32 ||
1372 (
1373 (opcode == ISD::LOAD || opcode == ISD::CopyFromReg ||
1374 opcode == ISD::EXTLOAD) &&
1375 (N.getValue(0).getValueType() == MVT::f32 ||
1376 N.getValue(0).getValueType() == MVT::f64)
1377 ))
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001378 && opcode != ISD::CALL && opcode != ISD::TAILCALL
Andrew Lenharth06342c32005-02-07 06:21:37 +00001379 )
Andrew Lenharth40831c52005-01-28 06:57:18 +00001380 return SelectExprFP(N, Result);
1381
1382 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001383 default:
1384 Node->dump();
1385 assert(0 && "Node not handled!\n");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001386
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001387 case ISD::CTPOP:
1388 case ISD::CTTZ:
1389 case ISD::CTLZ:
1390 Opc = opcode == ISD::CTPOP ? Alpha::CTPOP :
1391 (opcode == ISD::CTTZ ? Alpha::CTTZ : Alpha::CTLZ);
1392 Tmp1 = SelectExpr(N.getOperand(0));
1393 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1394 return Result;
1395
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001396 case ISD::MULHU:
1397 Tmp1 = SelectExpr(N.getOperand(0));
1398 Tmp2 = SelectExpr(N.getOperand(1));
1399 BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth706be912005-04-07 13:55:53 +00001400 return Result;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001401 case ISD::MULHS:
1402 {
1403 //MULHU - Ra<63>*Rb - Rb<63>*Ra
1404 Tmp1 = SelectExpr(N.getOperand(0));
1405 Tmp2 = SelectExpr(N.getOperand(1));
1406 Tmp3 = MakeReg(MVT::i64);
1407 BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1408 unsigned V1 = MakeReg(MVT::i64);
1409 unsigned V2 = MakeReg(MVT::i64);
1410 BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31).addReg(Tmp1);
1411 BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31).addReg(Tmp2);
1412 unsigned IRes = MakeReg(MVT::i64);
1413 BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
1414 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
1415 return Result;
1416 }
Andrew Lenharth7332f3e2005-04-02 19:11:07 +00001417 case ISD::UNDEF: {
1418 BuildMI(BB, Alpha::IDEF, 0, Result);
1419 return Result;
1420 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001421
Andrew Lenharth032f2352005-02-22 21:59:48 +00001422 case ISD::DYNAMIC_STACKALLOC:
1423 // Generate both result values.
Andrew Lenharth3a7118d2005-02-23 17:33:42 +00001424 if (Result != notIn)
1425 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth032f2352005-02-22 21:59:48 +00001426 else
1427 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1428
1429 // FIXME: We are currently ignoring the requested alignment for handling
1430 // greater than the stack alignment. This will need to be revisited at some
1431 // point. Align = N.getOperand(2);
1432
1433 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1434 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1435 std::cerr << "Cannot allocate stack object with greater alignment than"
1436 << " the stack alignment yet!";
1437 abort();
1438 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001439
Andrew Lenharth032f2352005-02-22 21:59:48 +00001440 Select(N.getOperand(0));
1441 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1442 {
1443 if (CN->getValue() < 32000)
1444 {
1445 BuildMI(BB, Alpha::LDA, 2, Alpha::R30)
1446 .addImm(-CN->getValue()).addReg(Alpha::R30);
1447 } else {
1448 Tmp1 = SelectExpr(N.getOperand(1));
1449 // Subtract size from stack pointer, thereby allocating some space.
1450 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1451 }
1452 } else {
1453 Tmp1 = SelectExpr(N.getOperand(1));
1454 // Subtract size from stack pointer, thereby allocating some space.
1455 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1456 }
1457
1458 // Put a pointer to the space into the result register, by copying the stack
1459 // pointer.
Andrew Lenharth7bc47022005-02-22 23:29:25 +00001460 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
Andrew Lenharth032f2352005-02-22 21:59:48 +00001461 return Result;
1462
Andrew Lenharth33819132005-03-04 20:09:23 +00001463// case ISD::ConstantPool:
1464// Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1465// AlphaLowering.restoreGP(BB);
1466// BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(Tmp1);
1467// return Result;
Andrew Lenharth2c594352005-01-29 15:42:07 +00001468
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001469 case ISD::FrameIndex:
Andrew Lenharth032f2352005-02-22 21:59:48 +00001470 BuildMI(BB, Alpha::LDA, 2, Result)
1471 .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
1472 .addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001473 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001474
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001475 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001476 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001477 case ISD::SEXTLOAD:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001478 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001479 {
1480 // Make sure we generate both values.
1481 if (Result != notIn)
1482 ExprMap[N.getValue(1)] = notIn; // Generate the token
1483 else
1484 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001485
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001486 SDOperand Chain = N.getOperand(0);
1487 SDOperand Address = N.getOperand(1);
1488 Select(Chain);
1489
Misha Brukman4633f1c2005-04-21 23:13:11 +00001490 assert(Node->getValueType(0) == MVT::i64 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001491 "Unknown type to sign extend to.");
Andrew Lenharth03824012005-02-07 05:55:55 +00001492 if (opcode == ISD::LOAD)
1493 Opc = Alpha::LDQ;
1494 else
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001495 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
1496 default: Node->dump(); assert(0 && "Bad sign extend!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001497 case MVT::i32: Opc = Alpha::LDL;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001498 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001499 case MVT::i16: Opc = Alpha::LDWU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001500 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001501 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Misha Brukman4633f1c2005-04-21 23:13:11 +00001502 case MVT::i8: Opc = Alpha::LDBU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001503 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001504 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001505
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001506 if (EnableAlphaLSMark)
1507 {
1508 int i = getValueOffset(dyn_cast<SrcValueSDNode>(N.getOperand(2))->getValue());
1509 int j = getFunctionOffset(BB->getParent()->getFunction());
1510 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
1511 }
1512
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001513 if (Address.getOpcode() == ISD::GlobalAddress) {
1514 AlphaLowering.restoreGP(BB);
1515 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001516 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001517 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1518 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001519 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1520 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001521 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001522 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001523 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001524 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001525 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001526 BuildMI(BB, Opc, 2, Result)
1527 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1528 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001529 } else {
1530 long offset;
1531 SelectAddr(Address, Tmp1, offset);
1532 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1533 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001534 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001535 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001536
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001537 case ISD::GlobalAddress:
1538 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001539 has_sym = true;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001540 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
1541 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
1542 return Result;
1543
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001544 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001545 case ISD::CALL:
1546 {
1547 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001548
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001549 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001550 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001551
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001552 //grab the arguments
1553 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001554 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001555 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001556 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001557
Andrew Lenharth684f2292005-01-30 00:35:27 +00001558 //in reg args
1559 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001560 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001561 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001562 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +00001563 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001564 Alpha::F19, Alpha::F20, Alpha::F21};
1565 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001566 default:
1567 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001568 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001569 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001570 N.getOperand(i+2).getValueType() << "\n";
1571 assert(0 && "Unknown value type for call");
1572 case MVT::i1:
1573 case MVT::i8:
1574 case MVT::i16:
1575 case MVT::i32:
1576 case MVT::i64:
1577 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1578 break;
1579 case MVT::f32:
1580 case MVT::f64:
1581 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1582 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001583 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001584 }
Andrew Lenharth684f2292005-01-30 00:35:27 +00001585 //in mem args
1586 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001587 {
1588 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001589 default:
1590 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001591 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001592 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001593 N.getOperand(i+2).getValueType() << "\n";
1594 assert(0 && "Unknown value type for call");
1595 case MVT::i1:
1596 case MVT::i8:
1597 case MVT::i16:
1598 case MVT::i32:
1599 case MVT::i64:
1600 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1601 break;
1602 case MVT::f32:
1603 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1604 break;
1605 case MVT::f64:
1606 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1607 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001608 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001609 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001610 //build the right kind of call
1611 if (GlobalAddressSDNode *GASD =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001612 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001613 {
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001614 if (GASD->getGlobal()->isExternal()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001615 //use safe calling convention
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001616 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001617 has_sym = true;
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001618 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal());
1619 } else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001620 //use PC relative branch call
Andrew Lenharth1e0d9bd2005-04-14 17:34:20 +00001621 AlphaLowering.restoreGP(BB);
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001622 BuildMI(BB, Alpha::BSR, 1, Alpha::R26).addGlobalAddress(GASD->getGlobal(),true);
1623 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001624 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001625 else if (ExternalSymbolSDNode *ESSDN =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001626 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001627 {
1628 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001629 has_sym = true;
Andrew Lenharthba05ad62005-03-30 18:22:52 +00001630 BuildMI(BB, Alpha::CALL, 1).addExternalSymbol(ESSDN->getSymbol(), true);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001631 } else {
1632 //no need to restore GP as we are doing an indirect call
1633 Tmp1 = SelectExpr(N.getOperand(1));
1634 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
1635 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
1636 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001637
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001638 //push the result into a virtual register
Misha Brukman4633f1c2005-04-21 23:13:11 +00001639
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001640 switch (Node->getValueType(0)) {
1641 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001642 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001643 case MVT::i1:
1644 case MVT::i8:
1645 case MVT::i16:
1646 case MVT::i32:
1647 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001648 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
1649 break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001650 case MVT::f32:
1651 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001652 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
1653 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001654 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001655 return Result+N.ResNo;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001656 }
1657
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001658 case ISD::SIGN_EXTEND_INREG:
1659 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001660 //do SDIV opt for all levels of ints if not dividing by a constant
1661 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
1662 && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001663 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001664 unsigned Tmp4 = MakeReg(MVT::f64);
1665 unsigned Tmp5 = MakeReg(MVT::f64);
1666 unsigned Tmp6 = MakeReg(MVT::f64);
1667 unsigned Tmp7 = MakeReg(MVT::f64);
1668 unsigned Tmp8 = MakeReg(MVT::f64);
1669 unsigned Tmp9 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001670
1671 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1672 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1673 MoveInt2FP(Tmp1, Tmp4, true);
1674 MoveInt2FP(Tmp2, Tmp5, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001675 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Tmp4);
1676 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Tmp5);
1677 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
1678 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Tmp8);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001679 MoveFP2Int(Tmp9, Result, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001680 return Result;
1681 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001682
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001683 //Alpha has instructions for a bunch of signed 32 bit stuff
1684 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001685 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001686 switch (N.getOperand(0).getOpcode()) {
1687 case ISD::ADD:
1688 case ISD::SUB:
1689 case ISD::MUL:
1690 {
1691 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
1692 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
1693 //FIXME: first check for Scaled Adds and Subs!
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001694 ConstantSDNode* CSD = NULL;
1695 if(!isMul && N.getOperand(0).getOperand(0).getOpcode() == ISD::SHL &&
1696 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(0).getOperand(1))) &&
1697 (CSD->getValue() == 2 || CSD->getValue() == 3))
1698 {
1699 bool use4 = CSD->getValue() == 2;
1700 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1701 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1702 BuildMI(BB, isAdd?(use4?Alpha::S4ADDL:Alpha::S8ADDL):(use4?Alpha::S4SUBL:Alpha::S8SUBL),
1703 2,Result).addReg(Tmp1).addReg(Tmp2);
1704 }
1705 else if(isAdd && N.getOperand(0).getOperand(1).getOpcode() == ISD::SHL &&
1706 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1).getOperand(1))) &&
1707 (CSD->getValue() == 2 || CSD->getValue() == 3))
1708 {
1709 bool use4 = CSD->getValue() == 2;
1710 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1711 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1712 BuildMI(BB, use4?Alpha::S4ADDL:Alpha::S8ADDL, 2,Result).addReg(Tmp1).addReg(Tmp2);
1713 }
1714 else if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001715 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
1716 { //Normal imm add/sub
1717 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001718 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001719 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
1720 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001721 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001722 else
1723 { //Normal add/sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001724 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULL : Alpha::SUBL);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001725 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001726 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001727 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1728 }
1729 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001730 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001731 default: break; //Fall Though;
1732 }
1733 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001734 Tmp1 = SelectExpr(N.getOperand(0));
1735 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001736 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001737 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001738 {
1739 default:
1740 Node->dump();
1741 assert(0 && "Sign Extend InReg not there yet");
1742 break;
1743 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001744 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001745 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001746 break;
1747 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001748 case MVT::i16:
1749 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
1750 break;
1751 case MVT::i8:
1752 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
1753 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +00001754 case MVT::i1:
1755 Tmp2 = MakeReg(MVT::i64);
1756 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001757 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +00001758 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001759 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001760 return Result;
1761 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001762
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001763 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001764 {
1765 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1766 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1767 bool isConst1 = false;
1768 bool isConst2 = false;
1769 int dir;
Misha Brukman7847fca2005-04-22 17:54:37 +00001770
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001771 //Tmp1 = SelectExpr(N.getOperand(0));
1772 if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001773 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
1774 isConst1 = true;
1775 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001776 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1777 isConst2 = true;
1778
1779 switch (SetCC->getCondition()) {
1780 default: Node->dump(); assert(0 && "Unknown integer comparison!");
1781 case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001782 case ISD::SETLT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001783 Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001784 case ISD::SETLE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001785 Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001786 case ISD::SETGT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001787 Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001788 case ISD::SETGE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001789 Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001790 case ISD::SETULT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001791 Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001792 case ISD::SETUGT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001793 Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001794 case ISD::SETULE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001795 Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001796 case ISD::SETUGE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001797 Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001798 case ISD::SETNE: {//Handle this one special
1799 //std::cerr << "Alpha does not have a setne.\n";
1800 //abort();
1801 Tmp1 = SelectExpr(N.getOperand(0));
1802 Tmp2 = SelectExpr(N.getOperand(1));
1803 Tmp3 = MakeReg(MVT::i64);
1804 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001805 //Remeber we have the Inv for this CC
1806 CCInvMap[N] = Tmp3;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001807 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001808 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001809 return Result;
1810 }
1811 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001812 if (dir == 1) {
1813 Tmp1 = SelectExpr(N.getOperand(0));
1814 if (isConst2) {
1815 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1816 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1817 } else {
1818 Tmp2 = SelectExpr(N.getOperand(1));
1819 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1820 }
1821 } else if (dir == 2) {
1822 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001823 if (isConst1) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001824 Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1825 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1826 } else {
1827 Tmp2 = SelectExpr(N.getOperand(0));
1828 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1829 }
1830 } else { //dir == 0
1831 if (isConst1) {
1832 Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1833 Tmp2 = SelectExpr(N.getOperand(1));
1834 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
1835 } else if (isConst2) {
1836 Tmp1 = SelectExpr(N.getOperand(0));
1837 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1838 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
1839 } else {
1840 Tmp1 = SelectExpr(N.getOperand(0));
1841 Tmp2 = SelectExpr(N.getOperand(1));
1842 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1843 }
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001844 }
1845 } else {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001846 //do the comparison
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001847 Tmp1 = MakeReg(MVT::f64);
1848 bool inv = SelectFPSetCC(N, Tmp1);
1849
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001850 //now arrange for Result (int) to have a 1 or 0
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001851 Tmp2 = MakeReg(MVT::i64);
1852 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001853 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001854 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001855 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001856 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001857 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001858 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001859
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001860 case ISD::CopyFromReg:
1861 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001862 ++count_ins;
1863
Andrew Lenharth40831c52005-01-28 06:57:18 +00001864 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001865 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001866 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001867 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001868 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001869
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001870 SDOperand Chain = N.getOperand(0);
1871
1872 Select(Chain);
1873 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1874 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1875 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1876 return Result;
1877 }
1878
Misha Brukman4633f1c2005-04-21 23:13:11 +00001879 //Most of the plain arithmetic and logic share the same form, and the same
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001880 //constant immediate test
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001881 case ISD::XOR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001882 //Match Not
1883 if (N.getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001884 cast<ConstantSDNode>(N.getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001885 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001886 Tmp1 = SelectExpr(N.getOperand(0));
1887 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1888 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001889 }
1890 //Fall through
1891 case ISD::AND:
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001892 //handle zap
1893 if (opcode == ISD::AND && N.getOperand(1).getOpcode() == ISD::Constant)
1894 {
1895 uint64_t k = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1896 unsigned int build = 0;
1897 for(int i = 0; i < 8; ++i)
1898 {
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001899 if ((k & 0x00FF) == 0x00FF)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001900 build |= 1 << i;
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001901 else if ((k & 0x00FF) != 0)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001902 { build = 0; break; }
1903 k >>= 8;
1904 }
1905 if (build)
1906 {
1907 Tmp1 = SelectExpr(N.getOperand(0));
1908 BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1909 return Result;
1910 }
1911 }
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001912 case ISD::OR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001913 //Check operand(0) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001914 if (N.getOperand(0).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001915 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001916 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001917 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001918 switch(opcode) {
1919 case ISD::AND: Opc = Alpha::BIC; break;
1920 case ISD::OR: Opc = Alpha::ORNOT; break;
1921 case ISD::XOR: Opc = Alpha::EQV; break;
1922 }
1923 Tmp1 = SelectExpr(N.getOperand(1));
1924 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1925 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1926 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001927 }
1928 //Check operand(1) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001929 if (N.getOperand(1).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001930 N.getOperand(1).getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001931 cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001932 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001933 switch(opcode) {
1934 case ISD::AND: Opc = Alpha::BIC; break;
1935 case ISD::OR: Opc = Alpha::ORNOT; break;
1936 case ISD::XOR: Opc = Alpha::EQV; break;
1937 }
1938 Tmp1 = SelectExpr(N.getOperand(0));
1939 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1940 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1941 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001942 }
1943 //Fall through
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001944 case ISD::SHL:
1945 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001946 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001947 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001948 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1949 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001950 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001951 {
1952 switch(opcode) {
1953 case ISD::AND: Opc = Alpha::ANDi; break;
1954 case ISD::OR: Opc = Alpha::BISi; break;
1955 case ISD::XOR: Opc = Alpha::XORi; break;
1956 case ISD::SHL: Opc = Alpha::SLi; break;
1957 case ISD::SRL: Opc = Alpha::SRLi; break;
1958 case ISD::SRA: Opc = Alpha::SRAi; break;
1959 case ISD::MUL: Opc = Alpha::MULQi; break;
1960 };
1961 Tmp1 = SelectExpr(N.getOperand(0));
1962 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1963 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1964 } else {
1965 switch(opcode) {
1966 case ISD::AND: Opc = Alpha::AND; break;
1967 case ISD::OR: Opc = Alpha::BIS; break;
1968 case ISD::XOR: Opc = Alpha::XOR; break;
1969 case ISD::SHL: Opc = Alpha::SL; break;
1970 case ISD::SRL: Opc = Alpha::SRL; break;
1971 case ISD::SRA: Opc = Alpha::SRA; break;
1972 case ISD::MUL: Opc = Alpha::MULQ; break;
1973 };
1974 Tmp1 = SelectExpr(N.getOperand(0));
1975 Tmp2 = SelectExpr(N.getOperand(1));
1976 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1977 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001978 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001979
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001980 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001981 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001982 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001983 bool isAdd = opcode == ISD::ADD;
1984
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001985 //first check for Scaled Adds and Subs!
1986 //Valid for add and sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001987 ConstantSDNode* CSD = NULL;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001988 if(N.getOperand(0).getOpcode() == ISD::SHL &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001989 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
1990 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001991 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001992 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001993 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001994 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) && CSD->getValue() <= 255)
1995 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1996 2, Result).addReg(Tmp2).addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001997 else {
1998 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001999 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
2000 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00002001 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002002 }
2003 //Position prevents subs
Andrew Lenharth273a1f92005-04-07 14:18:13 +00002004 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002005 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) &&
2006 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002007 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002008 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002009 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002010 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(0))) && CSD->getValue() <= 255)
2011 BuildMI(BB, use4?Alpha::S4ADDQi:Alpha::S8ADDQi, 2, Result).addReg(Tmp2)
2012 .addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00002013 else {
2014 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002015 BuildMI(BB, use4?Alpha::S4ADDQ:Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00002016 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002017 }
2018 //small addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002019 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
2020 CSD->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002021 { //Normal imm add/sub
2022 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
2023 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002024 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CSD->getValue());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002025 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002026 //larger addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002027 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
2028 CSD->getSignExtended() <= 32767 &&
2029 CSD->getSignExtended() >= -32767)
Andrew Lenharth74d00d82005-03-02 17:23:03 +00002030 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002031 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002032 Tmp2 = (long)CSD->getSignExtended();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002033 if (!isAdd)
2034 Tmp2 = -Tmp2;
2035 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002036 }
2037 //give up and do the operation
2038 else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002039 //Normal add/sub
2040 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
2041 Tmp1 = SelectExpr(N.getOperand(0));
2042 Tmp2 = SelectExpr(N.getOperand(1));
2043 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2044 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002045 return Result;
2046 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002047
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00002048 case ISD::SDIV:
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002049 {
Andrew Lenhartha565c272005-04-06 22:03:13 +00002050 ConstantSDNode* CSD;
2051 //check if we can convert into a shift!
2052 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
2053 (int64_t)CSD->getSignExtended() != 0 &&
2054 ExactLog2(abs((int64_t)CSD->getSignExtended())) != 0)
2055 {
2056 unsigned k = ExactLog2(abs(CSD->getSignExtended()));
2057 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenhartha565c272005-04-06 22:03:13 +00002058 if (k == 1)
2059 Tmp2 = Tmp1;
2060 else
2061 {
2062 Tmp2 = MakeReg(MVT::i64);
2063 BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
2064 }
2065 Tmp3 = MakeReg(MVT::i64);
2066 BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
2067 unsigned Tmp4 = MakeReg(MVT::i64);
2068 BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
2069 if ((int64_t)CSD->getSignExtended() > 0)
2070 BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
2071 else
2072 {
2073 unsigned Tmp5 = MakeReg(MVT::i64);
2074 BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
2075 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
2076 }
2077 return Result;
2078 }
2079 }
2080 //Else fall through
2081
2082 case ISD::UDIV:
2083 {
2084 ConstantSDNode* CSD;
2085 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
2086 ((int64_t)CSD->getSignExtended() >= 2 ||
2087 (int64_t)CSD->getSignExtended() <= -2))
2088 {
2089 // If this is a divide by constant, we can emit code using some magic
2090 // constants to implement it as a multiply instead.
2091 ExprMap.erase(N);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002092 if (opcode == ISD::SDIV)
Andrew Lenhartha565c272005-04-06 22:03:13 +00002093 return SelectExpr(BuildSDIVSequence(N));
2094 else
2095 return SelectExpr(BuildUDIVSequence(N));
2096 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002097 }
2098 //else fall though
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002099 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +00002100 case ISD::SREM:
Misha Brukman4633f1c2005-04-21 23:13:11 +00002101 //FIXME: alpha really doesn't support any of these operations,
Andrew Lenharth40831c52005-01-28 06:57:18 +00002102 // the ops are expanded into special library calls with
2103 // special calling conventions
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002104 //Restore GP because it is a call after all...
Andrew Lenharth40831c52005-01-28 06:57:18 +00002105 switch(opcode) {
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00002106 case ISD::UREM: Opc = Alpha::REMQU; break;
2107 case ISD::SREM: Opc = Alpha::REMQ; break;
2108 case ISD::UDIV: Opc = Alpha::DIVQU; break;
2109 case ISD::SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002110 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002111 Tmp1 = SelectExpr(N.getOperand(0));
2112 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth33819132005-03-04 20:09:23 +00002113 //set up regs explicitly (helps Reg alloc)
2114 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002115 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00002116 AlphaLowering.restoreGP(BB);
Andrew Lenharth33819132005-03-04 20:09:23 +00002117 BuildMI(BB, Opc, 2).addReg(Alpha::R24).addReg(Alpha::R25);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002118 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002119 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002120
Andrew Lenharthe76797c2005-02-01 20:40:27 +00002121 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002122 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002123 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002124 assert (DestType == MVT::i64 && "only quads can be loaded to");
2125 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00002126 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002127 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002128 if (SrcType == MVT::f32)
Misha Brukman7847fca2005-04-22 17:54:37 +00002129 {
2130 Tmp2 = MakeReg(MVT::f64);
2131 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
2132 Tmp1 = Tmp2;
2133 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002134 Tmp2 = MakeReg(MVT::f64);
2135 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00002136 MoveFP2Int(Tmp2, Result, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002137
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002138 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002139 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002140
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002141 case ISD::SELECT:
2142 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00002143 //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 +00002144 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002145 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2146 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002147 // Get the condition into the zero flag.
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002148 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002149
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002150 SDOperand CC = N.getOperand(0);
2151 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
2152
Misha Brukman4633f1c2005-04-21 23:13:11 +00002153 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002154 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
2155 { //FP Setcc -> Int Select
Misha Brukman7847fca2005-04-22 17:54:37 +00002156 Tmp1 = MakeReg(MVT::f64);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002157 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2158 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Misha Brukman7847fca2005-04-22 17:54:37 +00002159 bool inv = SelectFPSetCC(CC, Tmp1);
2160 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
2161 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
2162 return Result;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002163 }
2164 if (CC.getOpcode() == ISD::SETCC) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002165 //Int SetCC -> Select
2166 //Dropping the CC is only useful if we are comparing to 0
2167 if((SetCC->getOperand(1).getOpcode() == ISD::Constant &&
2168 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0) ||
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002169 (SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2170 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0))
2171 {
2172 //figure out a few things
2173 bool LeftZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2174 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
2175 bool RightZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2176 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
2177 bool LeftConst = N.getOperand(1).getOpcode() == ISD::Constant &&
2178 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255;
2179 bool RightConst = N.getOperand(2).getOpcode() == ISD::Constant &&
2180 cast<ConstantSDNode>(N.getOperand(2))->getValue() <= 255;
2181 bool useImm = LeftConst || RightConst;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002182
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002183 //Fix up CC
2184 ISD::CondCode cCode= SetCC->getCondition();
2185 if (RightConst && !LeftConst) //Invert sense to get Imm field right
2186 cCode = ISD::getSetCCInverse(cCode, true);
2187 if (LeftZero && !RightZero) //Swap Operands
2188 cCode = ISD::getSetCCSwappedOperands(cCode);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002189
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002190 //Choose the CMOV
2191 switch (cCode) {
2192 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
2193 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
2194 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
2195 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
2196 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
2197 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
2198 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
2199 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2200 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break; //Technically you could have this CC
2201 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
2202 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2203 }
2204 if(LeftZero && !RightZero) //swap Operands
2205 Tmp1 = SelectExpr(SetCC->getOperand(1)); //Cond
2206 else
2207 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
2208
2209 if (LeftConst) {
2210 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2211 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
Misha Brukman7847fca2005-04-22 17:54:37 +00002212 .addImm(cast<ConstantSDNode>(N.getOperand(1))->getValue())
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002213 .addReg(Tmp1);
2214 } else if (RightConst) {
2215 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
2216 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
Misha Brukman7847fca2005-04-22 17:54:37 +00002217 .addImm(cast<ConstantSDNode>(N.getOperand(2))->getValue())
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002218 .addReg(Tmp1);
2219 } else {
2220 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2221 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2222 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
2223 }
2224 return Result;
2225 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002226 //Otherwise, fall though
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002227 }
2228 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002229 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2230 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthe76797c2005-02-01 20:40:27 +00002231 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002232
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002233 return Result;
2234 }
2235
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002236 case ISD::Constant:
2237 {
Andrew Lenharthc0513832005-03-29 19:24:04 +00002238 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002239 if (val <= IMM_HIGH && val >= IMM_LOW) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002240 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002241 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002242 else if (val <= (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT &&
2243 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
2244 Tmp1 = MakeReg(MVT::i64);
2245 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val)).addReg(Alpha::R31);
2246 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002247 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002248 else {
2249 MachineConstantPool *CP = BB->getParent()->getConstantPool();
2250 ConstantUInt *C = ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
2251 unsigned CPI = CP->getConstantPoolIndex(C);
2252 AlphaLowering.restoreGP(BB);
2253 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CPI);
2254 }
2255 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002256 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002257 }
2258
2259 return 0;
2260}
2261
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002262void AlphaISel::Select(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002263 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00002264 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002265
Nate Begeman85fdeb22005-03-24 04:39:54 +00002266 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002267 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002268
2269 SDNode *Node = N.Val;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002270
Andrew Lenharth760270d2005-02-07 23:02:23 +00002271 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002272
2273 default:
2274 Node->dump(); std::cerr << "\n";
2275 assert(0 && "Node not handled yet!");
2276
2277 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00002278 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002279 return;
2280 }
2281
2282 case ISD::BR: {
2283 MachineBasicBlock *Dest =
2284 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2285
2286 Select(N.getOperand(0));
2287 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
2288 return;
2289 }
2290
2291 case ISD::ImplicitDef:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002292 ++count_ins;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002293 Select(N.getOperand(0));
2294 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
2295 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002296
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002297 case ISD::EntryToken: return; // Noop
2298
2299 case ISD::TokenFactor:
2300 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2301 Select(Node->getOperand(i));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002302
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002303 //N.Val->dump(); std::cerr << "\n";
2304 //assert(0 && "Node not handled yet!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00002305
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002306 return;
2307
2308 case ISD::CopyToReg:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002309 ++count_outs;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002310 Select(N.getOperand(0));
2311 Tmp1 = SelectExpr(N.getOperand(1));
2312 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002313
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002314 if (Tmp1 != Tmp2) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002315 if (N.getOperand(1).getValueType() == MVT::f64 ||
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002316 N.getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00002317 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2318 else
2319 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002320 }
2321 return;
2322
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002323 case ISD::RET:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002324 ++count_outs;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002325 switch (N.getNumOperands()) {
2326 default:
2327 std::cerr << N.getNumOperands() << "\n";
2328 for (unsigned i = 0; i < N.getNumOperands(); ++i)
2329 std::cerr << N.getOperand(i).getValueType() << "\n";
2330 Node->dump();
2331 assert(0 && "Unknown return instruction!");
2332 case 2:
2333 Select(N.getOperand(0));
2334 Tmp1 = SelectExpr(N.getOperand(1));
2335 switch (N.getOperand(1).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002336 default: Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002337 assert(0 && "All other types should have been promoted!!");
2338 case MVT::f64:
2339 case MVT::f32:
2340 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
2341 break;
2342 case MVT::i32:
2343 case MVT::i64:
2344 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
2345 break;
2346 }
2347 break;
2348 case 1:
2349 Select(N.getOperand(0));
2350 break;
2351 }
2352 //Tmp2 = AlphaLowering.getRetAddr();
2353 //BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(Tmp2).addReg(Tmp2);
2354 BuildMI(BB, Alpha::RETURN, 0); // Just emit a 'ret' instruction
2355 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002356
Misha Brukman4633f1c2005-04-21 23:13:11 +00002357 case ISD::TRUNCSTORE:
2358 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002359 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002360 SDOperand Chain = N.getOperand(0);
2361 SDOperand Value = N.getOperand(1);
2362 SDOperand Address = N.getOperand(2);
2363 Select(Chain);
2364
2365 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00002366
2367 if (opcode == ISD::STORE) {
2368 switch(Value.getValueType()) {
2369 default: assert(0 && "unknown Type in store");
2370 case MVT::i64: Opc = Alpha::STQ; break;
2371 case MVT::f64: Opc = Alpha::STT; break;
2372 case MVT::f32: Opc = Alpha::STS; break;
2373 }
2374 } else { //ISD::TRUNCSTORE
2375 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2376 default: assert(0 && "unknown Type in store");
2377 case MVT::i1: //FIXME: DAG does not promote this load
2378 case MVT::i8: Opc = Alpha::STB; break;
2379 case MVT::i16: Opc = Alpha::STW; break;
2380 case MVT::i32: Opc = Alpha::STL; break;
2381 }
Andrew Lenharth65838902005-02-06 16:22:15 +00002382 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00002383
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002384 if (EnableAlphaLSMark)
2385 {
2386 int i = getValueOffset(dyn_cast<SrcValueSDNode>(N.getOperand(3))->getValue());
2387 int j = getFunctionOffset(BB->getParent()->getFunction());
2388 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
2389 }
2390
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002391 if (Address.getOpcode() == ISD::GlobalAddress)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002392 {
2393 AlphaLowering.restoreGP(BB);
2394 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002395 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002396 BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
2397 }
Andrew Lenharth05380342005-02-07 05:07:00 +00002398 else if(Address.getOpcode() == ISD::FrameIndex)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002399 {
Andrew Lenharth032f2352005-02-22 21:59:48 +00002400 BuildMI(BB, Opc, 3).addReg(Tmp1)
2401 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
2402 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002403 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002404 else
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002405 {
2406 long offset;
2407 SelectAddr(Address, Tmp2, offset);
2408 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2409 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002410 return;
2411 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002412
2413 case ISD::EXTLOAD:
2414 case ISD::SEXTLOAD:
2415 case ISD::ZEXTLOAD:
2416 case ISD::LOAD:
2417 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002418 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002419 case ISD::CALL:
Andrew Lenharth032f2352005-02-22 21:59:48 +00002420 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002421 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002422 SelectExpr(N);
2423 return;
2424
Chris Lattner16cd04d2005-05-12 23:24:06 +00002425 case ISD::CALLSEQ_START:
2426 case ISD::CALLSEQ_END:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002427 Select(N.getOperand(0));
2428 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002429
Chris Lattner16cd04d2005-05-12 23:24:06 +00002430 Opc = N.getOpcode() == ISD::CALLSEQ_START ? Alpha::ADJUSTSTACKDOWN :
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002431 Alpha::ADJUSTSTACKUP;
2432 BuildMI(BB, Opc, 1).addImm(Tmp1);
2433 return;
Andrew Lenharth95762122005-03-31 21:24:06 +00002434
2435 case ISD::PCMARKER:
2436 Select(N.getOperand(0)); //Chain
2437 BuildMI(BB, Alpha::PCLABEL, 2).addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
2438 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002439 }
2440 assert(0 && "Should not be reached!");
2441}
2442
2443
2444/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
2445/// into a machine code representation using pattern matching and a machine
2446/// description file.
2447///
2448FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002449 return new AlphaISel(TM);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002450}
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002451