blob: 1fd03d342cd3d172cf3e66c332b2ccf8ef24679b [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
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000162 std::pair<SDOperand,SDOperand>
163 LowerVACopy(SDOperand Chain, SDOperand Src, SDOperand Dest,
164 SelectionDAG &DAG);
165
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000166 virtual std::pair<SDOperand, SDOperand>
167 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
168 SelectionDAG &DAG);
169
170 void restoreGP(MachineBasicBlock* BB)
171 {
172 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
173 }
174 };
175}
176
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000177/// LowerOperation - Provide custom lowering hooks for some operations.
178///
179SDOperand AlphaTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
180 MachineFunction &MF = DAG.getMachineFunction();
181 switch (Op.getOpcode()) {
182 default: assert(0 && "Should not custom lower this!");
Misha Brukmanb8ee91a2005-06-06 17:39:46 +0000183#if 0
184 case ISD::SINT_TO_FP:
185 {
186 assert (Op.getOperand(0).getValueType() == MVT::i64
187 && "only quads can be loaded from");
188 SDOperand SRC;
189 if (EnableAlphaFTOI)
190 {
191 std::vector<MVT::ValueType> RTs;
192 RTs.push_back(Op.getValueType());
193 std::vector<SDOperand> Ops;
194 Ops.push_back(Op.getOperand(0));
195 SRC = DAG.getNode(AlphaISD::ITOF, RTs, Ops);
196 } else {
197 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
198 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
199 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
200 Op.getOperand(0), StackSlot, DAG.getSrcValue(NULL));
201 SRC = DAG.getLoad(Op.getValueType(), Store.getValue(0), StackSlot,
202 DAG.getSrcValue(NULL));
203 }
204 std::vector<MVT::ValueType> RTs;
205 RTs.push_back(Op.getValueType());
206 std::vector<SDOperand> Ops;
207 Ops.push_back(SRC);
208 return DAG.getNode(AlphaISD::CVTQ, RTs, Ops);
209 }
210#endif
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000211 }
Misha Brukmanb8ee91a2005-06-06 17:39:46 +0000212 return SDOperand();
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000213}
214
215
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000216/// AddLiveIn - This helper function adds the specified physical register to the
217/// MachineFunction as a live in value. It also creates a corresponding virtual
218/// register for it.
219static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
220 TargetRegisterClass *RC) {
221 assert(RC->contains(PReg) && "Not the correct regclass!");
222 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
223 MF.addLiveIn(PReg, VReg);
224 return VReg;
225}
226
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000227//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
228
229//For now, just use variable size stack frame format
230
231//In a standard call, the first six items are passed in registers $16
232//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
233//of argument-to-register correspondence.) The remaining items are
234//collected in a memory argument list that is a naturally aligned
235//array of quadwords. In a standard call, this list, if present, must
236//be passed at 0(SP).
Misha Brukman7847fca2005-04-22 17:54:37 +0000237//7 ... n 0(SP) ... (n-7)*8(SP)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000238
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000239// //#define FP $15
240// //#define RA $26
241// //#define PV $27
242// //#define GP $29
243// //#define SP $30
Misha Brukman4633f1c2005-04-21 23:13:11 +0000244
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000245std::vector<SDOperand>
Misha Brukman4633f1c2005-04-21 23:13:11 +0000246AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000247{
248 std::vector<SDOperand> ArgValues;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000249
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000250 MachineFunction &MF = DAG.getMachineFunction();
Andrew Lenharth05380342005-02-07 05:07:00 +0000251 MachineFrameInfo*MFI = MF.getFrameInfo();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000252
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000253 MachineBasicBlock& BB = MF.front();
254
255 //Handle the return address
256 //BuildMI(&BB, Alpha::IDEF, 0, Alpha::R26);
257
Misha Brukman4633f1c2005-04-21 23:13:11 +0000258 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000259 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000260 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000261 Alpha::F19, Alpha::F20, Alpha::F21};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000262 int count = 0;
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000263
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000264 GP = AddLiveIn(MF, Alpha::R29, getRegClassFor(MVT::i64));
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000265
Chris Lattnere4d5c442005-03-15 04:54:21 +0000266 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000267 {
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000268 SDOperand argt;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000269 if (count < 6) {
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000270 unsigned Vreg;
271 MVT::ValueType VT = getValueType(I->getType());
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000272 switch (VT) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000273 default:
274 std::cerr << "Unknown Type " << VT << "\n";
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000275 abort();
276 case MVT::f64:
277 case MVT::f32:
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000278 args_float[count] = AddLiveIn(MF,args_float[count], getRegClassFor(VT));
279 argt = DAG.getCopyFromReg(args_float[count], VT, DAG.getRoot());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000280 break;
281 case MVT::i1:
282 case MVT::i8:
283 case MVT::i16:
284 case MVT::i32:
285 case MVT::i64:
Andrew Lenharth591ec572005-05-31 18:42:18 +0000286 args_int[count] = AddLiveIn(MF, args_int[count], getRegClassFor(MVT::i64));
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000287 argt = DAG.getCopyFromReg(args_int[count], VT, DAG.getRoot());
Andrew Lenharth14f30c92005-05-31 18:37:16 +0000288 if (VT != MVT::i64)
289 argt = DAG.getNode(ISD::TRUNCATE, VT, argt);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000290 break;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000291 }
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000292 DAG.setRoot(argt.getValue(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000293 } else { //more args
294 // Create the frame index object for this incoming parameter...
295 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000296
297 // Create the SelectionDAG nodes corresponding to a load
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000298 //from this parameter
299 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000300 argt = DAG.getLoad(getValueType(I->getType()),
301 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000302 }
Andrew Lenharth032f2352005-02-22 21:59:48 +0000303 ++count;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000304 ArgValues.push_back(argt);
305 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000306
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000307 // If the functions takes variable number of arguments, copy all regs to stack
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000308 if (F.isVarArg()) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000309 VarArgsOffset = count * 8;
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000310 std::vector<SDOperand> LS;
311 for (int i = 0; i < 6; ++i) {
312 if (args_int[i] < 1024)
313 args_int[i] = AddLiveIn(MF,args_int[i], getRegClassFor(MVT::i64));
314 SDOperand argt = DAG.getCopyFromReg(args_int[i], MVT::i64, DAG.getRoot());
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000315 int FI = MFI->CreateFixedObject(8, -8 * (6 - i));
Andrew Lenharth558bc882005-06-18 18:34:52 +0000316 if (i == 0) VarArgsBase = FI;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000317 SDOperand SDFI = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000318 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt, SDFI, DAG.getSrcValue(NULL)));
319
320 if (args_float[i] < 1024)
321 args_float[i] = AddLiveIn(MF,args_float[i], getRegClassFor(MVT::f64));
322 argt = DAG.getCopyFromReg(args_float[i], MVT::f64, DAG.getRoot());
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000323 FI = MFI->CreateFixedObject(8, - 8 * (12 - i));
324 SDFI = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000325 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt, SDFI, DAG.getSrcValue(NULL)));
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000326 }
327
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000328 //Set up a token factor with all the stack traffic
329 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, LS));
330 }
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000331
332 // Finally, inform the code generator which regs we return values in.
333 switch (getValueType(F.getReturnType())) {
334 default: assert(0 && "Unknown type!");
335 case MVT::isVoid: break;
336 case MVT::i1:
337 case MVT::i8:
338 case MVT::i16:
339 case MVT::i32:
340 case MVT::i64:
341 MF.addLiveOut(Alpha::R0);
342 break;
343 case MVT::f32:
344 case MVT::f64:
345 MF.addLiveOut(Alpha::F0);
346 break;
347 }
348
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000349 //return the arguments
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000350 return ArgValues;
351}
352
353std::pair<SDOperand, SDOperand>
354AlphaTargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000355 const Type *RetTy, bool isVarArg,
Chris Lattneradf6a962005-05-13 18:50:42 +0000356 unsigned CallingConv, bool isTailCall,
Misha Brukman7847fca2005-04-22 17:54:37 +0000357 SDOperand Callee, ArgListTy &Args,
358 SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000359 int NumBytes = 0;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000360 if (Args.size() > 6)
361 NumBytes = (Args.size() - 6) * 8;
362
Chris Lattner16cd04d2005-05-12 23:24:06 +0000363 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000364 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000365 std::vector<SDOperand> args_to_use;
366 for (unsigned i = 0, e = Args.size(); i != e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000367 {
368 switch (getValueType(Args[i].second)) {
369 default: assert(0 && "Unexpected ValueType for argument!");
370 case MVT::i1:
371 case MVT::i8:
372 case MVT::i16:
373 case MVT::i32:
374 // Promote the integer to 64 bits. If the input type is signed use a
375 // sign extend, otherwise use a zero extend.
376 if (Args[i].second->isSigned())
377 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
378 else
379 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
380 break;
381 case MVT::i64:
382 case MVT::f64:
383 case MVT::f32:
384 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000385 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000386 args_to_use.push_back(Args[i].first);
387 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000388
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000389 std::vector<MVT::ValueType> RetVals;
390 MVT::ValueType RetTyVT = getValueType(RetTy);
391 if (RetTyVT != MVT::isVoid)
392 RetVals.push_back(RetTyVT);
393 RetVals.push_back(MVT::Other);
394
Misha Brukman4633f1c2005-04-21 23:13:11 +0000395 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000396 Chain, Callee, args_to_use), 0);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000397 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner16cd04d2005-05-12 23:24:06 +0000398 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000399 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000400 return std::make_pair(TheCall, Chain);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000401}
402
403std::pair<SDOperand, SDOperand>
Andrew Lenharth558bc882005-06-18 18:34:52 +0000404AlphaTargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG, SDOperand Dest) {
405 // vastart just stores the address of the VarArgsBase and VarArgsOffset
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000406 SDOperand FR = DAG.getFrameIndex(VarArgsBase, MVT::i64);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000407 SDOperand S1 = DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, Dest, DAG.getSrcValue(NULL));
408 SDOperand SA2 = DAG.getNode(ISD::ADD, MVT::i64, Dest, DAG.getConstant(8, MVT::i64));
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000409 SDOperand S2 = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, S1,
Andrew Lenharth558bc882005-06-18 18:34:52 +0000410 DAG.getConstant(VarArgsOffset, MVT::i64), SA2,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000411 DAG.getSrcValue(NULL), MVT::i32);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000412 return std::make_pair(S2, S2);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000413}
414
415std::pair<SDOperand,SDOperand> AlphaTargetLowering::
Andrew Lenharth558bc882005-06-18 18:34:52 +0000416LowerVAArgNext(SDOperand Chain, SDOperand VAList,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000417 const Type *ArgTy, SelectionDAG &DAG) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000418 SDOperand Base = DAG.getLoad(MVT::i64, Chain, VAList, DAG.getSrcValue(NULL));
419 SDOperand Tmp = DAG.getNode(ISD::ADD, MVT::i64, VAList,
420 DAG.getConstant(8, MVT::i64));
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000421 SDOperand Offset = DAG.getNode(ISD::SEXTLOAD, MVT::i64, Chain, Tmp,
422 DAG.getSrcValue(NULL), MVT::i32);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000423 SDOperand DataPtr = DAG.getNode(ISD::ADD, MVT::i64, Base, Offset);
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000424 if (ArgTy->isFloatingPoint())
425 {
426 //if fp && Offset < 6*8, then subtract 6*8 from DataPtr
427 SDOperand FPDataPtr = DAG.getNode(ISD::SUB, MVT::i64, DataPtr,
428 DAG.getConstant(8*6, MVT::i64));
429 SDOperand CC = DAG.getSetCC(ISD::SETLT, MVT::i64,
430 Offset, DAG.getConstant(8*6, MVT::i64));
431 DataPtr = DAG.getNode(ISD::SELECT, MVT::i64, CC, FPDataPtr, DataPtr);
432 }
433
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000434 SDOperand Result;
435 if (ArgTy == Type::IntTy)
436 Result = DAG.getNode(ISD::SEXTLOAD, MVT::i64, Chain, DataPtr,
437 DAG.getSrcValue(NULL), MVT::i32);
438 else if (ArgTy == Type::UIntTy)
439 Result = DAG.getNode(ISD::ZEXTLOAD, MVT::i64, Chain, DataPtr,
440 DAG.getSrcValue(NULL), MVT::i32);
441 else
442 Result = DAG.getLoad(getValueType(ArgTy), Chain, DataPtr,
443 DAG.getSrcValue(NULL));
444
Andrew Lenharth558bc882005-06-18 18:34:52 +0000445 SDOperand NewOffset = DAG.getNode(ISD::ADD, MVT::i64, Offset,
446 DAG.getConstant(8, MVT::i64));
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000447 SDOperand Update = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Result, NewOffset,
448 Tmp, DAG.getSrcValue(NULL), MVT::i32);
449 Result = DAG.getNode(ISD::TRUNCATE, getValueType(ArgTy), Result);
450
Andrew Lenharth558bc882005-06-18 18:34:52 +0000451 return std::make_pair(Result, Update);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000452}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000453
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000454std::pair<SDOperand,SDOperand> AlphaTargetLowering::
455LowerVACopy(SDOperand Chain, SDOperand Src, SDOperand Dest,
456 SelectionDAG &DAG) {
457 //Default to returning the input list
458 SDOperand Val = DAG.getLoad(getPointerTy(), Chain, Src, DAG.getSrcValue(NULL));
459 SDOperand Result = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
460 Val, Dest, DAG.getSrcValue(NULL));
461 SDOperand NP = DAG.getNode(ISD::ADD, MVT::i64, Src,
462 DAG.getConstant(8, MVT::i64));
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000463 Val = DAG.getNode(ISD::SEXTLOAD, MVT::i64, Chain, NP, DAG.getSrcValue(NULL),
464 MVT::i32);
465 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Val.getValue(1),
466 Val, Dest, DAG.getSrcValue(NULL), MVT::i32);
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000467 return std::make_pair(Result, Result);
468}
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000469
470std::pair<SDOperand, SDOperand> AlphaTargetLowering::
471LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
472 SelectionDAG &DAG) {
473 abort();
474}
475
476
477
478
479
480namespace {
481
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000482//===--------------------------------------------------------------------===//
483/// ISel - Alpha specific code to select Alpha machine instructions for
484/// SelectionDAG operations.
485//===--------------------------------------------------------------------===//
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000486class AlphaISel : public SelectionDAGISel {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000487
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000488 /// AlphaLowering - This object fully describes how to lower LLVM code to an
489 /// Alpha-specific SelectionDAG.
490 AlphaTargetLowering AlphaLowering;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000491
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000492 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
493 // for sdiv and udiv until it is put into the future
494 // dag combiner.
495
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000496 /// ExprMap - As shared expressions are codegen'd, we keep track of which
497 /// vreg the value is produced in, so we only emit one copy of each compiled
498 /// tree.
499 static const unsigned notIn = (unsigned)(-1);
500 std::map<SDOperand, unsigned> ExprMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000501
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000502 //CCInvMap sometimes (SetNE) we have the inverse CC code for free
503 std::map<SDOperand, unsigned> CCInvMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000504
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000505 int count_ins;
506 int count_outs;
507 bool has_sym;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000508 int max_depth;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000509
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000510public:
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000511 AlphaISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering), AlphaLowering(TM)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000512 {}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000513
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000514 /// InstructionSelectBasicBlock - This callback is invoked by
515 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
516 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
Andrew Lenharth032f2352005-02-22 21:59:48 +0000517 DEBUG(BB->dump());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000518 count_ins = 0;
519 count_outs = 0;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000520 max_depth = 0;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000521 has_sym = false;
522
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000523 // Codegen the basic block.
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000524 ISelDAG = &DAG;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000525 max_depth = DAG.getRoot().getNodeDepth();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000526 Select(DAG.getRoot());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000527
528 if(has_sym)
529 ++count_ins;
530 if(EnableAlphaCount)
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000531 std::cerr << "COUNT: " << BB->getParent()->getFunction ()->getName() << " "
532 << BB->getNumber() << " "
533 << max_depth << " "
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000534 << count_ins << " "
535 << count_outs << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000536
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000537 // Clear state used for selection.
538 ExprMap.clear();
539 CCInvMap.clear();
540 }
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000541
542 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000543
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000544 unsigned SelectExpr(SDOperand N);
545 unsigned SelectExprFP(SDOperand N, unsigned Result);
546 void Select(SDOperand N);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000547
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000548 void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
549 void SelectBranchCC(SDOperand N);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000550 void MoveFP2Int(unsigned src, unsigned dst, bool isDouble);
551 void MoveInt2FP(unsigned src, unsigned dst, bool isDouble);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000552 //returns whether the sense of the comparison was inverted
553 bool SelectFPSetCC(SDOperand N, unsigned dst);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000554
555 // dag -> dag expanders for integer divide by constant
556 SDOperand BuildSDIVSequence(SDOperand N);
557 SDOperand BuildUDIVSequence(SDOperand N);
558
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000559};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000560}
561
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000562void AlphaISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000563 // If this function has live-in values, emit the copies from pregs to vregs at
564 // the top of the function, before anything else.
565 MachineBasicBlock *BB = MF.begin();
566 if (MF.livein_begin() != MF.livein_end()) {
567 SSARegMap *RegMap = MF.getSSARegMap();
568 for (MachineFunction::livein_iterator LI = MF.livein_begin(),
569 E = MF.livein_end(); LI != E; ++LI) {
570 const TargetRegisterClass *RC = RegMap->getRegClass(LI->second);
571 if (RC == Alpha::GPRCRegisterClass) {
572 BuildMI(BB, Alpha::BIS, 2, LI->second).addReg(LI->first).addReg(LI->first);
573 } else if (RC == Alpha::FPRCRegisterClass) {
574 BuildMI(BB, Alpha::CPYS, 2, LI->second).addReg(LI->first).addReg(LI->first);
575 } else {
576 assert(0 && "Unknown regclass!");
577 }
578 }
579 }
580}
581
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000582//Find the offset of the arg in it's parent's function
583static int getValueOffset(const Value* v)
584{
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000585 static int uniqneg = -1;
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000586 if (v == NULL)
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000587 return uniqneg--;
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000588
589 const Instruction* itarget = dyn_cast<Instruction>(v);
590 const BasicBlock* btarget = itarget->getParent();
591 const Function* ftarget = btarget->getParent();
592
593 //offset due to earlier BBs
594 int i = 0;
595 for(Function::const_iterator ii = ftarget->begin(); &*ii != btarget; ++ii)
596 i += ii->size();
597
598 for(BasicBlock::const_iterator ii = btarget->begin(); &*ii != itarget; ++ii)
599 ++i;
600
601 return i;
602}
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000603//Find the offset of the function in it's module
604static int getFunctionOffset(const Function* fun)
605{
606 const Module* M = fun->getParent();
607
608 //offset due to earlier BBs
609 int i = 0;
610 for(Module::const_iterator ii = M->begin(); &*ii != fun; ++ii)
611 ++i;
612
613 return i;
614}
615
616static int getUID()
617{
618 static int id = 0;
619 return ++id;
620}
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000621
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000622//Factorize a number using the list of constants
623static bool factorize(int v[], int res[], int size, uint64_t c)
624{
625 bool cont = true;
626 while (c != 1 && cont)
627 {
628 cont = false;
629 for(int i = 0; i < size; ++i)
630 {
631 if (c % v[i] == 0)
632 {
633 c /= v[i];
634 ++res[i];
635 cont=true;
636 }
637 }
638 }
639 return c == 1;
640}
641
642
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000643//Shamelessly adapted from PPC32
Misha Brukman4633f1c2005-04-21 23:13:11 +0000644// Structure used to return the necessary information to codegen an SDIV as
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000645// a multiply.
646struct ms {
647 int64_t m; // magic number
648 int64_t s; // shift amount
649};
650
651struct mu {
652 uint64_t m; // magic number
653 int64_t a; // add indicator
654 int64_t s; // shift amount
655};
656
657/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukman4633f1c2005-04-21 23:13:11 +0000658/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000659/// or -1.
660static struct ms magic(int64_t d) {
661 int64_t p;
662 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
663 const uint64_t two63 = 9223372036854775808ULL; // 2^63
664 struct ms mag;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000665
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000666 ad = abs(d);
667 t = two63 + ((uint64_t)d >> 63);
668 anc = t - 1 - t%ad; // absolute value of nc
Andrew Lenharth320174f2005-04-07 17:19:16 +0000669 p = 63; // initialize p
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000670 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
671 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
672 q2 = two63/ad; // initialize q2 = 2p/abs(d)
673 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
674 do {
675 p = p + 1;
676 q1 = 2*q1; // update q1 = 2p/abs(nc)
677 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
678 if (r1 >= anc) { // must be unsigned comparison
679 q1 = q1 + 1;
680 r1 = r1 - anc;
681 }
682 q2 = 2*q2; // update q2 = 2p/abs(d)
683 r2 = 2*r2; // update r2 = rem(2p/abs(d))
684 if (r2 >= ad) { // must be unsigned comparison
685 q2 = q2 + 1;
686 r2 = r2 - ad;
687 }
688 delta = ad - r2;
689 } while (q1 < delta || (q1 == delta && r1 == 0));
690
691 mag.m = q2 + 1;
692 if (d < 0) mag.m = -mag.m; // resulting magic number
693 mag.s = p - 64; // resulting shift
694 return mag;
695}
696
697/// magicu - calculate the magic numbers required to codegen an integer udiv as
698/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
699static struct mu magicu(uint64_t d)
700{
701 int64_t p;
702 uint64_t nc, delta, q1, r1, q2, r2;
703 struct mu magu;
704 magu.a = 0; // initialize "add" indicator
705 nc = - 1 - (-d)%d;
Andrew Lenharth320174f2005-04-07 17:19:16 +0000706 p = 63; // initialize p
707 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
708 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
709 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
710 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000711 do {
712 p = p + 1;
713 if (r1 >= nc - r1 ) {
714 q1 = 2*q1 + 1; // update q1
715 r1 = 2*r1 - nc; // update r1
716 }
717 else {
718 q1 = 2*q1; // update q1
719 r1 = 2*r1; // update r1
720 }
721 if (r2 + 1 >= d - r2) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000722 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000723 q2 = 2*q2 + 1; // update q2
724 r2 = 2*r2 + 1 - d; // update r2
725 }
726 else {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000727 if (q2 >= 0x8000000000000000ull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000728 q2 = 2*q2; // update q2
729 r2 = 2*r2 + 1; // update r2
730 }
731 delta = d - 1 - r2;
732 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
733 magu.m = q2 + 1; // resulting magic number
Andrew Lenharth320174f2005-04-07 17:19:16 +0000734 magu.s = p - 64; // resulting shift
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000735 return magu;
736}
737
738/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
739/// return a DAG expression to select that will generate the same value by
740/// multiplying by a magic number. See:
741/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000742SDOperand AlphaISel::BuildSDIVSequence(SDOperand N) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000743 int64_t d = (int64_t)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000744 ms magics = magic(d);
745 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000746 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000747 ISelDAG->getConstant(magics.m, MVT::i64));
748 // If d > 0 and m < 0, add the numerator
749 if (d > 0 && magics.m < 0)
750 Q = ISelDAG->getNode(ISD::ADD, MVT::i64, Q, N.getOperand(0));
751 // If d < 0 and m > 0, subtract the numerator.
752 if (d < 0 && magics.m > 0)
753 Q = ISelDAG->getNode(ISD::SUB, MVT::i64, Q, N.getOperand(0));
754 // Shift right algebraic if shift value is nonzero
755 if (magics.s > 0)
Misha Brukman4633f1c2005-04-21 23:13:11 +0000756 Q = ISelDAG->getNode(ISD::SRA, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000757 ISelDAG->getConstant(magics.s, MVT::i64));
758 // Extract the sign bit and add it to the quotient
Misha Brukman4633f1c2005-04-21 23:13:11 +0000759 SDOperand T =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000760 ISelDAG->getNode(ISD::SRL, MVT::i64, Q, ISelDAG->getConstant(63, MVT::i64));
761 return ISelDAG->getNode(ISD::ADD, MVT::i64, Q, T);
762}
763
764/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
765/// return a DAG expression to select that will generate the same value by
766/// multiplying by a magic number. See:
767/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000768SDOperand AlphaISel::BuildUDIVSequence(SDOperand N) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000769 unsigned d =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000770 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
771 mu magics = magicu(d);
772 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000773 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000774 ISelDAG->getConstant(magics.m, MVT::i64));
775 if (magics.a == 0) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000776 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000777 ISelDAG->getConstant(magics.s, MVT::i64));
778 } else {
779 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i64, N.getOperand(0), Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000780 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000781 ISelDAG->getConstant(1, MVT::i64));
782 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i64, NPQ, Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000783 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000784 ISelDAG->getConstant(magics.s-1, MVT::i64));
785 }
786 return Q;
787}
788
Andrew Lenhartha565c272005-04-06 22:03:13 +0000789//From PPC32
790/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
791/// returns zero when the input is not exactly a power of two.
792static unsigned ExactLog2(uint64_t Val) {
793 if (Val == 0 || (Val & (Val-1))) return 0;
794 unsigned Count = 0;
795 while (Val != 1) {
796 Val >>= 1;
797 ++Count;
798 }
799 return Count;
800}
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000801
802
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000803//These describe LDAx
Andrew Lenharthc0513832005-03-29 19:24:04 +0000804static const int IMM_LOW = -32768;
805static const int IMM_HIGH = 32767;
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000806static const int IMM_MULT = 65536;
807
808static long getUpper16(long l)
809{
810 long y = l / IMM_MULT;
811 if (l % IMM_MULT > IMM_HIGH)
812 ++y;
813 return y;
814}
815
816static long getLower16(long l)
817{
818 long h = getUpper16(l);
819 return l - h * IMM_MULT;
820}
821
Andrew Lenharth65838902005-02-06 16:22:15 +0000822static unsigned GetSymVersion(unsigned opcode)
823{
824 switch (opcode) {
825 default: assert(0 && "unknown load or store"); return 0;
826 case Alpha::LDQ: return Alpha::LDQ_SYM;
827 case Alpha::LDS: return Alpha::LDS_SYM;
828 case Alpha::LDT: return Alpha::LDT_SYM;
829 case Alpha::LDL: return Alpha::LDL_SYM;
830 case Alpha::LDBU: return Alpha::LDBU_SYM;
831 case Alpha::LDWU: return Alpha::LDWU_SYM;
832 case Alpha::LDW: return Alpha::LDW_SYM;
833 case Alpha::LDB: return Alpha::LDB_SYM;
834 case Alpha::STQ: return Alpha::STQ_SYM;
835 case Alpha::STS: return Alpha::STS_SYM;
836 case Alpha::STT: return Alpha::STT_SYM;
837 case Alpha::STL: return Alpha::STL_SYM;
838 case Alpha::STW: return Alpha::STW_SYM;
839 case Alpha::STB: return Alpha::STB_SYM;
840 }
841}
842
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000843void AlphaISel::MoveFP2Int(unsigned src, unsigned dst, bool isDouble)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000844{
845 unsigned Opc;
846 if (EnableAlphaFTOI) {
847 Opc = isDouble ? Alpha::FTOIT : Alpha::FTOIS;
848 BuildMI(BB, Opc, 1, dst).addReg(src);
849 } else {
850 //The hard way:
851 // Spill the integer to memory and reload it from there.
852 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
853 MachineFunction *F = BB->getParent();
854 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
855
856 Opc = isDouble ? Alpha::STT : Alpha::STS;
857 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
858 Opc = isDouble ? Alpha::LDQ : Alpha::LDL;
859 BuildMI(BB, Alpha::LDQ, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
860 }
861}
862
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000863void AlphaISel::MoveInt2FP(unsigned src, unsigned dst, bool isDouble)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000864{
865 unsigned Opc;
866 if (EnableAlphaFTOI) {
867 Opc = isDouble?Alpha::ITOFT:Alpha::ITOFS;
868 BuildMI(BB, Opc, 1, dst).addReg(src);
869 } else {
870 //The hard way:
871 // Spill the integer to memory and reload it from there.
872 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
873 MachineFunction *F = BB->getParent();
874 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
875
876 Opc = isDouble ? Alpha::STQ : Alpha::STL;
877 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
878 Opc = isDouble ? Alpha::LDT : Alpha::LDS;
879 BuildMI(BB, Opc, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
880 }
881}
882
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000883bool AlphaISel::SelectFPSetCC(SDOperand N, unsigned dst)
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000884{
885 SDNode *Node = N.Val;
886 unsigned Opc, Tmp1, Tmp2, Tmp3;
887 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node);
888
889 //assert(SetCC->getOperand(0).getValueType() != MVT::f32 && "SetCC f32 should have been promoted");
890 bool rev = false;
891 bool inv = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000892
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000893 switch (SetCC->getCondition()) {
894 default: Node->dump(); assert(0 && "Unknown FP comparison!");
895 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
896 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
897 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
898 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
899 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
900 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
901 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000902
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000903 //FIXME: check for constant 0.0
904 ConstantFPSDNode *CN;
905 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
906 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
907 Tmp1 = Alpha::F31;
908 else
909 Tmp1 = SelectExpr(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000910
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000911 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
912 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
913 Tmp2 = Alpha::F31;
914 else
Chris Lattner9c9183a2005-04-30 04:44:07 +0000915 Tmp2 = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000916
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000917 //Can only compare doubles, and dag won't promote for me
918 if (SetCC->getOperand(0).getValueType() == MVT::f32)
919 {
920 //assert(0 && "Setcc On float?\n");
921 std::cerr << "Setcc on float!\n";
922 Tmp3 = MakeReg(MVT::f64);
923 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
924 Tmp1 = Tmp3;
925 }
926 if (SetCC->getOperand(1).getValueType() == MVT::f32)
927 {
928 //assert (0 && "Setcc On float?\n");
929 std::cerr << "Setcc on float!\n";
930 Tmp3 = MakeReg(MVT::f64);
931 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp2);
932 Tmp2 = Tmp3;
933 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000934
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000935 if (rev) std::swap(Tmp1, Tmp2);
936 //do the comparison
937 BuildMI(BB, Opc, 2, dst).addReg(Tmp1).addReg(Tmp2);
938 return inv;
939}
940
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000941//Check to see if the load is a constant offset from a base register
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000942void AlphaISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000943{
944 unsigned opcode = N.getOpcode();
945 if (opcode == ISD::ADD) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000946 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000947 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
948 { //Normal imm add
949 Reg = SelectExpr(N.getOperand(0));
950 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
951 return;
952 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000953 else if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000954 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 32767)
955 {
956 Reg = SelectExpr(N.getOperand(1));
957 offset = cast<ConstantSDNode>(N.getOperand(0))->getValue();
958 return;
959 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000960 }
961 Reg = SelectExpr(N);
962 offset = 0;
963 return;
964}
965
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000966void AlphaISel::SelectBranchCC(SDOperand N)
Andrew Lenharth445171a2005-02-08 00:40:03 +0000967{
968 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000969 MachineBasicBlock *Dest =
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000970 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
971 unsigned Opc = Alpha::WTF;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000972
Andrew Lenharth445171a2005-02-08 00:40:03 +0000973 Select(N.getOperand(0)); //chain
974 SDOperand CC = N.getOperand(1);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000975
Andrew Lenharth445171a2005-02-08 00:40:03 +0000976 if (CC.getOpcode() == ISD::SETCC)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000977 {
978 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
979 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
980 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000981 bool LeftZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
982 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
Andrew Lenharth09552bf2005-06-08 18:02:21 +0000983 bool RightZero = SetCC->getOperand(1).getOpcode() == ISD::Constant &&
984 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000985 bool isNE = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000986
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000987 //Fix up CC
988 ISD::CondCode cCode= SetCC->getCondition();
989 if (LeftZero && !RightZero) //Swap Operands
990 cCode = ISD::getSetCCSwappedOperands(cCode);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000991
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000992 if(cCode == ISD::SETNE)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000993 isNE = true;
Andrew Lenharth445171a2005-02-08 00:40:03 +0000994
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000995 if (LeftZero || RightZero) {
Andrew Lenharth09552bf2005-06-08 18:02:21 +0000996 switch (cCode) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000997 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
998 case ISD::SETEQ: Opc = Alpha::BEQ; break;
999 case ISD::SETLT: Opc = Alpha::BLT; break;
1000 case ISD::SETLE: Opc = Alpha::BLE; break;
1001 case ISD::SETGT: Opc = Alpha::BGT; break;
1002 case ISD::SETGE: Opc = Alpha::BGE; break;
1003 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
1004 case ISD::SETUGT: Opc = Alpha::BNE; break;
1005 case ISD::SETULE: Opc = Alpha::BEQ; break; //Technically you could have this CC
1006 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
1007 case ISD::SETNE: Opc = Alpha::BNE; break;
1008 }
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001009 unsigned Tmp1;
1010 if(LeftZero && !RightZero) //swap Operands
1011 Tmp1 = SelectExpr(SetCC->getOperand(1)); //Cond
1012 else
1013 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001014 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
1015 return;
1016 } else {
1017 unsigned Tmp1 = SelectExpr(CC);
1018 if (isNE)
1019 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
1020 else
1021 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001022 return;
1023 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001024 } else { //FP
1025 //Any comparison between 2 values should be codegened as an folded branch, as moving
1026 //CC to the integer register is very expensive
1027 //for a cmp b: c = a - b;
1028 //a = b: c = 0
1029 //a < b: c < 0
1030 //a > b: c > 0
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001031
1032 bool invTest = false;
1033 unsigned Tmp3;
1034
1035 ConstantFPSDNode *CN;
1036 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
1037 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1038 Tmp3 = SelectExpr(SetCC->getOperand(0));
1039 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
1040 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1041 {
1042 Tmp3 = SelectExpr(SetCC->getOperand(1));
1043 invTest = true;
1044 }
1045 else
1046 {
1047 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
1048 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
1049 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
1050 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1051 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1052 .addReg(Tmp1).addReg(Tmp2);
1053 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001054
1055 switch (SetCC->getCondition()) {
1056 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001057 case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
1058 case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
1059 case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
1060 case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
1061 case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
1062 case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001063 }
1064 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001065 return;
1066 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001067 abort(); //Should never be reached
1068 } else {
1069 //Giveup and do the stupid thing
1070 unsigned Tmp1 = SelectExpr(CC);
1071 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
1072 return;
1073 }
Andrew Lenharth445171a2005-02-08 00:40:03 +00001074 abort(); //Should never be reached
1075}
1076
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001077unsigned AlphaISel::SelectExprFP(SDOperand N, unsigned Result)
Andrew Lenharth40831c52005-01-28 06:57:18 +00001078{
1079 unsigned Tmp1, Tmp2, Tmp3;
1080 unsigned Opc = 0;
1081 SDNode *Node = N.Val;
1082 MVT::ValueType DestType = N.getValueType();
1083 unsigned opcode = N.getOpcode();
1084
1085 switch (opcode) {
1086 default:
1087 Node->dump();
1088 assert(0 && "Node not handled!\n");
Andrew Lenharth2c594352005-01-29 15:42:07 +00001089
Andrew Lenharth7332f3e2005-04-02 19:11:07 +00001090 case ISD::UNDEF: {
1091 BuildMI(BB, Alpha::IDEF, 0, Result);
1092 return Result;
1093 }
1094
Andrew Lenharth30b46d42005-04-02 19:04:58 +00001095 case ISD::FNEG:
1096 if(ISD::FABS == N.getOperand(0).getOpcode())
1097 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001098 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1099 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +00001100 } else {
Misha Brukman7847fca2005-04-22 17:54:37 +00001101 Tmp1 = SelectExpr(N.getOperand(0));
1102 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +00001103 }
1104 return Result;
1105
1106 case ISD::FABS:
1107 Tmp1 = SelectExpr(N.getOperand(0));
1108 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
1109 return Result;
1110
Andrew Lenharth9818c052005-02-05 13:19:12 +00001111 case ISD::SELECT:
1112 {
Andrew Lenharth45859692005-03-03 21:47:53 +00001113 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1114 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
1115 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
1116
1117 SDOperand CC = N.getOperand(0);
1118 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
1119
Misha Brukman4633f1c2005-04-21 23:13:11 +00001120 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth45859692005-03-03 21:47:53 +00001121 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
1122 { //FP Setcc -> Select yay!
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001123
1124
Andrew Lenharth45859692005-03-03 21:47:53 +00001125 //for a cmp b: c = a - b;
1126 //a = b: c = 0
1127 //a < b: c < 0
1128 //a > b: c > 0
Misha Brukman4633f1c2005-04-21 23:13:11 +00001129
Andrew Lenharth45859692005-03-03 21:47:53 +00001130 bool invTest = false;
1131 unsigned Tmp3;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001132
Andrew Lenharth45859692005-03-03 21:47:53 +00001133 ConstantFPSDNode *CN;
1134 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
1135 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1136 Tmp3 = SelectExpr(SetCC->getOperand(0));
1137 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
1138 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1139 {
1140 Tmp3 = SelectExpr(SetCC->getOperand(1));
1141 invTest = true;
1142 }
1143 else
1144 {
1145 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
1146 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
1147 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
1148 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1149 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1150 .addReg(Tmp1).addReg(Tmp2);
1151 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001152
Andrew Lenharth45859692005-03-03 21:47:53 +00001153 switch (SetCC->getCondition()) {
1154 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
1155 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNE : Alpha::FCMOVEQ; break;
1156 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGT : Alpha::FCMOVLT; break;
1157 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGE : Alpha::FCMOVLE; break;
1158 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLT : Alpha::FCMOVGT; break;
1159 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLE : Alpha::FCMOVGE; break;
1160 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQ : Alpha::FCMOVNE; break;
1161 }
Andrew Lenharth33819132005-03-04 20:09:23 +00001162 BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
Andrew Lenharth45859692005-03-03 21:47:53 +00001163 return Result;
1164 }
1165 else
1166 {
1167 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001168 BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV).addReg(Tmp1);
1169// // Spill the cond to memory and reload it from there.
1170// unsigned Tmp4 = MakeReg(MVT::f64);
1171// MoveIntFP(Tmp1, Tmp4, true);
1172// //now ideally, we don't have to do anything to the flag...
1173// // Get the condition into the zero flag.
1174// BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
Andrew Lenharth45859692005-03-03 21:47:53 +00001175 return Result;
1176 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001177 }
1178
Andrew Lenharthc1faced2005-02-01 01:37:24 +00001179 case ISD::FP_ROUND:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001180 assert (DestType == MVT::f32 &&
1181 N.getOperand(0).getValueType() == MVT::f64 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001182 "only f64 to f32 conversion supported here");
Andrew Lenharthc1faced2005-02-01 01:37:24 +00001183 Tmp1 = SelectExpr(N.getOperand(0));
1184 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
1185 return Result;
1186
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001187 case ISD::FP_EXTEND:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001188 assert (DestType == MVT::f64 &&
1189 N.getOperand(0).getValueType() == MVT::f32 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001190 "only f32 to f64 conversion supported here");
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001191 Tmp1 = SelectExpr(N.getOperand(0));
1192 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
1193 return Result;
1194
Andrew Lenharth2c594352005-01-29 15:42:07 +00001195 case ISD::CopyFromReg:
1196 {
1197 // Make sure we generate both values.
1198 if (Result != notIn)
1199 ExprMap[N.getValue(1)] = notIn; // Generate the token
1200 else
1201 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001202
Andrew Lenharth2c594352005-01-29 15:42:07 +00001203 SDOperand Chain = N.getOperand(0);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001204
Andrew Lenharth2c594352005-01-29 15:42:07 +00001205 Select(Chain);
1206 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1207 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1208 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
1209 return Result;
1210 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001211
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001212 case ISD::LOAD:
1213 {
1214 // Make sure we generate both values.
1215 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001216 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001217 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001218 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001219
Andrew Lenharth29219162005-02-07 06:31:44 +00001220 DestType = N.getValue(0).getValueType();
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001221
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001222 SDOperand Chain = N.getOperand(0);
1223 SDOperand Address = N.getOperand(1);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001224 Select(Chain);
Andrew Lenharth65838902005-02-06 16:22:15 +00001225 Opc = DestType == MVT::f64 ? Alpha::LDT : Alpha::LDS;
1226
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +00001227 if (EnableAlphaLSMark)
1228 {
1229 int i = getValueOffset(dyn_cast<SrcValueSDNode>(N.getOperand(2))->getValue());
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001230 int j = getFunctionOffset(BB->getParent()->getFunction());
1231 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +00001232 }
1233
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001234 if (Address.getOpcode() == ISD::GlobalAddress) {
1235 AlphaLowering.restoreGP(BB);
1236 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001237 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001238 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1239 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001240 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001241 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001242 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001243 has_sym = true;
Andrew Lenharth97127a12005-02-05 17:41:39 +00001244 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001245 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001246 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001247 BuildMI(BB, Opc, 2, Result)
1248 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1249 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001250 } else {
1251 long offset;
1252 SelectAddr(Address, Tmp1, offset);
1253 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1254 }
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001255 return Result;
1256 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001257 case ISD::ConstantFP:
1258 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
1259 if (CN->isExactlyValue(+0.0)) {
1260 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001261 } else if ( CN->isExactlyValue(-0.0)) {
1262 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001263 } else {
1264 abort();
1265 }
1266 }
1267 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001268
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001269 case ISD::SDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001270 case ISD::MUL:
1271 case ISD::ADD:
1272 case ISD::SUB:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001273 switch( opcode ) {
1274 case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS; break;
1275 case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS; break;
1276 case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS; break;
1277 case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS; break;
1278 };
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001279
1280 ConstantFPSDNode *CN;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001281 if (opcode == ISD::SUB
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001282 && (CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
1283 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1284 {
1285 Tmp2 = SelectExpr(N.getOperand(1));
1286 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp2).addReg(Tmp2);
1287 } else {
1288 Tmp1 = SelectExpr(N.getOperand(0));
1289 Tmp2 = SelectExpr(N.getOperand(1));
1290 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1291 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001292 return Result;
1293
Andrew Lenharth2c594352005-01-29 15:42:07 +00001294 case ISD::EXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001295 {
1296 //include a conversion sequence for float loads to double
1297 if (Result != notIn)
1298 ExprMap[N.getValue(1)] = notIn; // Generate the token
1299 else
1300 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001301
Andrew Lenhartha549deb2005-02-07 05:33:15 +00001302 Tmp1 = MakeReg(MVT::f32);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001303
1304 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001305 "EXTLOAD not from f32");
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001306 assert(Node->getValueType(0) == MVT::f64 && "EXTLOAD not to f64");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001307
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001308 SDOperand Chain = N.getOperand(0);
1309 SDOperand Address = N.getOperand(1);
1310 Select(Chain);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001311
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001312 if (Address.getOpcode() == ISD::GlobalAddress) {
1313 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001314 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001315 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1316 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001317 else if (ConstantPoolSDNode *CP =
1318 dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001319 {
1320 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001321 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001322 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addConstantPoolIndex(CP->getIndex());
1323 }
1324 else if(Address.getOpcode() == ISD::FrameIndex) {
1325 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
Andrew Lenharth032f2352005-02-22 21:59:48 +00001326 BuildMI(BB, Alpha::LDS, 2, Tmp1)
1327 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1328 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001329 } else {
1330 long offset;
1331 SelectAddr(Address, Tmp2, offset);
1332 BuildMI(BB, Alpha::LDS, 1, Tmp1).addImm(offset).addReg(Tmp2);
1333 }
Andrew Lenharth29219162005-02-07 06:31:44 +00001334 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001335 return Result;
1336 }
Andrew Lenharth2c594352005-01-29 15:42:07 +00001337
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001338 case ISD::UINT_TO_FP:
Andrew Lenharth69520ed2005-05-26 18:18:34 +00001339 {
1340 //FIXME: First test if we will have problems with the sign bit before doing the slow thing
1341 assert (N.getOperand(0).getValueType() == MVT::i64
1342 && "only quads can be loaded from");
1343 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1344 Tmp2 = MakeReg(MVT::i64);
1345 BuildMI(BB, Alpha::SRL, 2, Tmp2).addReg(Tmp1).addImm(1);
1346 Tmp3 = MakeReg(MVT::i64);
1347 BuildMI(BB, Alpha::CMPLT, 2, Tmp3).addReg(Tmp1).addReg(Alpha::R31);
1348 unsigned Tmp4 = MakeReg(MVT::f64), Tmp5 = MakeReg(MVT::f64), Tmp6 = MakeReg(MVT::f64);
1349 MoveInt2FP(Tmp1, Tmp4, true);
1350 MoveInt2FP(Tmp2, Tmp5, true);
1351 MoveInt2FP(Tmp3, Tmp6, true);
1352 Tmp1 = MakeReg(MVT::f64);
1353 Tmp2 = MakeReg(MVT::f64);
1354 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
1355 BuildMI(BB, Opc, 1, Tmp1).addReg(Tmp4);
1356 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp5);
1357 Tmp3 = MakeReg(MVT::f64);
1358 BuildMI(BB, Alpha::ADDT, 2, Tmp3).addReg(Tmp2).addReg(Tmp2);
1359 //Ok, now tmp1 had the plain covereted
1360 //tmp3 has the reduced converted and added
1361 //tmp6 has the conditional to use
1362 BuildMI(BB, Alpha::FCMOVNE, 3, Result).addReg(Tmp1).addReg(Tmp3).addReg(Tmp6);
1363 return Result;
1364 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001365 case ISD::SINT_TO_FP:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001366 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001367 assert (N.getOperand(0).getValueType() == MVT::i64
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001368 && "only quads can be loaded from");
Andrew Lenharth40831c52005-01-28 06:57:18 +00001369 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001370 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001371 MoveInt2FP(Tmp1, Tmp2, true);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001372 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
1373 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001374 return Result;
1375 }
1376 }
1377 assert(0 && "should not get here");
1378 return 0;
1379}
1380
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001381unsigned AlphaISel::SelectExpr(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001382 unsigned Result;
Andrew Lenharth2966e842005-04-07 18:15:28 +00001383 unsigned Tmp1, Tmp2 = 0, Tmp3;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001384 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001385 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001386
1387 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001388 MVT::ValueType DestType = N.getValueType();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001389
1390 unsigned &Reg = ExprMap[N];
1391 if (Reg) return Reg;
1392
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001393 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::TAILCALL)
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001394 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001395 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001396 else {
1397 // If this is a call instruction, make sure to prepare ALL of the result
1398 // values as well as the chain.
1399 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001400 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001401 else {
1402 Result = MakeReg(Node->getValueType(0));
1403 ExprMap[N.getValue(0)] = Result;
1404 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1405 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001406 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001407 }
1408 }
1409
Andrew Lenharth50d91d72005-04-30 14:19:13 +00001410 if ((DestType == MVT::f64 || DestType == MVT::f32 ||
1411 (
1412 (opcode == ISD::LOAD || opcode == ISD::CopyFromReg ||
1413 opcode == ISD::EXTLOAD) &&
1414 (N.getValue(0).getValueType() == MVT::f32 ||
1415 N.getValue(0).getValueType() == MVT::f64)
1416 ))
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001417 && opcode != ISD::CALL && opcode != ISD::TAILCALL
Andrew Lenharth06342c32005-02-07 06:21:37 +00001418 )
Andrew Lenharth40831c52005-01-28 06:57:18 +00001419 return SelectExprFP(N, Result);
1420
1421 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001422 default:
1423 Node->dump();
1424 assert(0 && "Node not handled!\n");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001425
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001426 case ISD::CTPOP:
1427 case ISD::CTTZ:
1428 case ISD::CTLZ:
1429 Opc = opcode == ISD::CTPOP ? Alpha::CTPOP :
1430 (opcode == ISD::CTTZ ? Alpha::CTTZ : Alpha::CTLZ);
1431 Tmp1 = SelectExpr(N.getOperand(0));
1432 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1433 return Result;
1434
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001435 case ISD::MULHU:
1436 Tmp1 = SelectExpr(N.getOperand(0));
1437 Tmp2 = SelectExpr(N.getOperand(1));
1438 BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth706be912005-04-07 13:55:53 +00001439 return Result;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001440 case ISD::MULHS:
1441 {
1442 //MULHU - Ra<63>*Rb - Rb<63>*Ra
1443 Tmp1 = SelectExpr(N.getOperand(0));
1444 Tmp2 = SelectExpr(N.getOperand(1));
1445 Tmp3 = MakeReg(MVT::i64);
1446 BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1447 unsigned V1 = MakeReg(MVT::i64);
1448 unsigned V2 = MakeReg(MVT::i64);
1449 BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31).addReg(Tmp1);
1450 BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31).addReg(Tmp2);
1451 unsigned IRes = MakeReg(MVT::i64);
1452 BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
1453 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
1454 return Result;
1455 }
Andrew Lenharth7332f3e2005-04-02 19:11:07 +00001456 case ISD::UNDEF: {
1457 BuildMI(BB, Alpha::IDEF, 0, Result);
1458 return Result;
1459 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001460
Andrew Lenharth032f2352005-02-22 21:59:48 +00001461 case ISD::DYNAMIC_STACKALLOC:
1462 // Generate both result values.
Andrew Lenharth3a7118d2005-02-23 17:33:42 +00001463 if (Result != notIn)
1464 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth032f2352005-02-22 21:59:48 +00001465 else
1466 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1467
1468 // FIXME: We are currently ignoring the requested alignment for handling
1469 // greater than the stack alignment. This will need to be revisited at some
1470 // point. Align = N.getOperand(2);
1471
1472 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1473 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1474 std::cerr << "Cannot allocate stack object with greater alignment than"
1475 << " the stack alignment yet!";
1476 abort();
1477 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001478
Andrew Lenharth032f2352005-02-22 21:59:48 +00001479 Select(N.getOperand(0));
1480 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1481 {
1482 if (CN->getValue() < 32000)
1483 {
1484 BuildMI(BB, Alpha::LDA, 2, Alpha::R30)
1485 .addImm(-CN->getValue()).addReg(Alpha::R30);
1486 } else {
1487 Tmp1 = SelectExpr(N.getOperand(1));
1488 // Subtract size from stack pointer, thereby allocating some space.
1489 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1490 }
1491 } else {
1492 Tmp1 = SelectExpr(N.getOperand(1));
1493 // Subtract size from stack pointer, thereby allocating some space.
1494 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1495 }
1496
1497 // Put a pointer to the space into the result register, by copying the stack
1498 // pointer.
Andrew Lenharth7bc47022005-02-22 23:29:25 +00001499 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
Andrew Lenharth032f2352005-02-22 21:59:48 +00001500 return Result;
1501
Andrew Lenharth33819132005-03-04 20:09:23 +00001502// case ISD::ConstantPool:
1503// Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1504// AlphaLowering.restoreGP(BB);
1505// BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(Tmp1);
1506// return Result;
Andrew Lenharth2c594352005-01-29 15:42:07 +00001507
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001508 case ISD::FrameIndex:
Andrew Lenharth032f2352005-02-22 21:59:48 +00001509 BuildMI(BB, Alpha::LDA, 2, Result)
1510 .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
1511 .addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001512 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001513
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001514 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001515 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001516 case ISD::SEXTLOAD:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001517 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001518 {
1519 // Make sure we generate both values.
1520 if (Result != notIn)
1521 ExprMap[N.getValue(1)] = notIn; // Generate the token
1522 else
1523 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001524
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001525 SDOperand Chain = N.getOperand(0);
1526 SDOperand Address = N.getOperand(1);
1527 Select(Chain);
1528
Misha Brukman4633f1c2005-04-21 23:13:11 +00001529 assert(Node->getValueType(0) == MVT::i64 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001530 "Unknown type to sign extend to.");
Andrew Lenharth03824012005-02-07 05:55:55 +00001531 if (opcode == ISD::LOAD)
1532 Opc = Alpha::LDQ;
1533 else
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001534 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
1535 default: Node->dump(); assert(0 && "Bad sign extend!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001536 case MVT::i32: Opc = Alpha::LDL;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001537 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001538 case MVT::i16: Opc = Alpha::LDWU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001539 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001540 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Misha Brukman4633f1c2005-04-21 23:13:11 +00001541 case MVT::i8: Opc = Alpha::LDBU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001542 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001543 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001544
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001545 if (EnableAlphaLSMark)
1546 {
1547 int i = getValueOffset(dyn_cast<SrcValueSDNode>(N.getOperand(2))->getValue());
1548 int j = getFunctionOffset(BB->getParent()->getFunction());
1549 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
1550 }
1551
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001552 if (Address.getOpcode() == ISD::GlobalAddress) {
1553 AlphaLowering.restoreGP(BB);
1554 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001555 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001556 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1557 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001558 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1559 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001560 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001561 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001562 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001563 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001564 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001565 BuildMI(BB, Opc, 2, Result)
1566 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1567 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001568 } else {
1569 long offset;
1570 SelectAddr(Address, Tmp1, offset);
1571 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1572 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001573 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001574 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001575
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001576 case ISD::GlobalAddress:
1577 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001578 has_sym = true;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001579 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
1580 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
1581 return Result;
1582
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001583 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001584 case ISD::CALL:
1585 {
1586 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001587
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001588 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001589 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001590
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001591 //grab the arguments
1592 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001593 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001594 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001595 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001596
Andrew Lenharth684f2292005-01-30 00:35:27 +00001597 //in reg args
1598 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001599 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001600 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001601 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +00001602 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001603 Alpha::F19, Alpha::F20, Alpha::F21};
1604 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001605 default:
1606 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001607 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001608 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001609 N.getOperand(i+2).getValueType() << "\n";
1610 assert(0 && "Unknown value type for call");
1611 case MVT::i1:
1612 case MVT::i8:
1613 case MVT::i16:
1614 case MVT::i32:
1615 case MVT::i64:
1616 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1617 break;
1618 case MVT::f32:
1619 case MVT::f64:
1620 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1621 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001622 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001623 }
Andrew Lenharth684f2292005-01-30 00:35:27 +00001624 //in mem args
1625 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001626 {
1627 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001628 default:
1629 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001630 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001631 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001632 N.getOperand(i+2).getValueType() << "\n";
1633 assert(0 && "Unknown value type for call");
1634 case MVT::i1:
1635 case MVT::i8:
1636 case MVT::i16:
1637 case MVT::i32:
1638 case MVT::i64:
1639 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1640 break;
1641 case MVT::f32:
1642 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1643 break;
1644 case MVT::f64:
1645 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1646 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001647 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001648 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001649 //build the right kind of call
1650 if (GlobalAddressSDNode *GASD =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001651 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001652 {
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001653 if (GASD->getGlobal()->isExternal()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001654 //use safe calling convention
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001655 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001656 has_sym = true;
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001657 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal());
1658 } else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001659 //use PC relative branch call
Andrew Lenharth1e0d9bd2005-04-14 17:34:20 +00001660 AlphaLowering.restoreGP(BB);
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001661 BuildMI(BB, Alpha::BSR, 1, Alpha::R26).addGlobalAddress(GASD->getGlobal(),true);
1662 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001663 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001664 else if (ExternalSymbolSDNode *ESSDN =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001665 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001666 {
1667 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001668 has_sym = true;
Andrew Lenharthba05ad62005-03-30 18:22:52 +00001669 BuildMI(BB, Alpha::CALL, 1).addExternalSymbol(ESSDN->getSymbol(), true);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001670 } else {
1671 //no need to restore GP as we are doing an indirect call
1672 Tmp1 = SelectExpr(N.getOperand(1));
1673 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
1674 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
1675 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001676
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001677 //push the result into a virtual register
Misha Brukman4633f1c2005-04-21 23:13:11 +00001678
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001679 switch (Node->getValueType(0)) {
1680 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001681 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001682 case MVT::i1:
1683 case MVT::i8:
1684 case MVT::i16:
1685 case MVT::i32:
1686 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001687 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
1688 break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001689 case MVT::f32:
1690 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001691 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
1692 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001693 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001694 return Result+N.ResNo;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001695 }
1696
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001697 case ISD::SIGN_EXTEND_INREG:
1698 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001699 //do SDIV opt for all levels of ints if not dividing by a constant
1700 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
1701 && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001702 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001703 unsigned Tmp4 = MakeReg(MVT::f64);
1704 unsigned Tmp5 = MakeReg(MVT::f64);
1705 unsigned Tmp6 = MakeReg(MVT::f64);
1706 unsigned Tmp7 = MakeReg(MVT::f64);
1707 unsigned Tmp8 = MakeReg(MVT::f64);
1708 unsigned Tmp9 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001709
1710 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1711 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1712 MoveInt2FP(Tmp1, Tmp4, true);
1713 MoveInt2FP(Tmp2, Tmp5, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001714 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Tmp4);
1715 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Tmp5);
1716 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
1717 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Tmp8);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001718 MoveFP2Int(Tmp9, Result, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001719 return Result;
1720 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001721
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001722 //Alpha has instructions for a bunch of signed 32 bit stuff
1723 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001724 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001725 switch (N.getOperand(0).getOpcode()) {
1726 case ISD::ADD:
1727 case ISD::SUB:
1728 case ISD::MUL:
1729 {
1730 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
1731 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
1732 //FIXME: first check for Scaled Adds and Subs!
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001733 ConstantSDNode* CSD = NULL;
1734 if(!isMul && N.getOperand(0).getOperand(0).getOpcode() == ISD::SHL &&
1735 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(0).getOperand(1))) &&
1736 (CSD->getValue() == 2 || CSD->getValue() == 3))
1737 {
1738 bool use4 = CSD->getValue() == 2;
1739 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1740 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1741 BuildMI(BB, isAdd?(use4?Alpha::S4ADDL:Alpha::S8ADDL):(use4?Alpha::S4SUBL:Alpha::S8SUBL),
1742 2,Result).addReg(Tmp1).addReg(Tmp2);
1743 }
1744 else if(isAdd && N.getOperand(0).getOperand(1).getOpcode() == ISD::SHL &&
1745 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1).getOperand(1))) &&
1746 (CSD->getValue() == 2 || CSD->getValue() == 3))
1747 {
1748 bool use4 = CSD->getValue() == 2;
1749 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1750 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1751 BuildMI(BB, use4?Alpha::S4ADDL:Alpha::S8ADDL, 2,Result).addReg(Tmp1).addReg(Tmp2);
1752 }
1753 else if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001754 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
1755 { //Normal imm add/sub
1756 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001757 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001758 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
1759 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001760 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001761 else
1762 { //Normal add/sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001763 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULL : Alpha::SUBL);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001764 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001765 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001766 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1767 }
1768 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001769 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001770 default: break; //Fall Though;
1771 }
1772 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001773 Tmp1 = SelectExpr(N.getOperand(0));
1774 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001775 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001776 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001777 {
1778 default:
1779 Node->dump();
1780 assert(0 && "Sign Extend InReg not there yet");
1781 break;
1782 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001783 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001784 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001785 break;
1786 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001787 case MVT::i16:
1788 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
1789 break;
1790 case MVT::i8:
1791 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
1792 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +00001793 case MVT::i1:
1794 Tmp2 = MakeReg(MVT::i64);
1795 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001796 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +00001797 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001798 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001799 return Result;
1800 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001801
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001802 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001803 {
1804 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1805 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1806 bool isConst1 = false;
1807 bool isConst2 = false;
1808 int dir;
Misha Brukman7847fca2005-04-22 17:54:37 +00001809
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001810 //Tmp1 = SelectExpr(N.getOperand(0));
1811 if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001812 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
1813 isConst1 = true;
1814 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001815 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1816 isConst2 = true;
1817
1818 switch (SetCC->getCondition()) {
1819 default: Node->dump(); assert(0 && "Unknown integer comparison!");
1820 case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001821 case ISD::SETLT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001822 Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001823 case ISD::SETLE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001824 Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001825 case ISD::SETGT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001826 Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001827 case ISD::SETGE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001828 Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001829 case ISD::SETULT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001830 Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001831 case ISD::SETUGT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001832 Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001833 case ISD::SETULE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001834 Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001835 case ISD::SETUGE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001836 Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001837 case ISD::SETNE: {//Handle this one special
1838 //std::cerr << "Alpha does not have a setne.\n";
1839 //abort();
1840 Tmp1 = SelectExpr(N.getOperand(0));
1841 Tmp2 = SelectExpr(N.getOperand(1));
1842 Tmp3 = MakeReg(MVT::i64);
1843 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001844 //Remeber we have the Inv for this CC
1845 CCInvMap[N] = Tmp3;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001846 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001847 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001848 return Result;
1849 }
1850 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001851 if (dir == 1) {
1852 Tmp1 = SelectExpr(N.getOperand(0));
1853 if (isConst2) {
1854 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1855 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1856 } else {
1857 Tmp2 = SelectExpr(N.getOperand(1));
1858 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1859 }
1860 } else if (dir == 2) {
1861 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001862 if (isConst1) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001863 Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1864 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1865 } else {
1866 Tmp2 = SelectExpr(N.getOperand(0));
1867 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1868 }
1869 } else { //dir == 0
1870 if (isConst1) {
1871 Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1872 Tmp2 = SelectExpr(N.getOperand(1));
1873 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
1874 } else if (isConst2) {
1875 Tmp1 = SelectExpr(N.getOperand(0));
1876 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1877 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
1878 } else {
1879 Tmp1 = SelectExpr(N.getOperand(0));
1880 Tmp2 = SelectExpr(N.getOperand(1));
1881 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1882 }
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001883 }
1884 } else {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001885 //do the comparison
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001886 Tmp1 = MakeReg(MVT::f64);
1887 bool inv = SelectFPSetCC(N, Tmp1);
1888
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001889 //now arrange for Result (int) to have a 1 or 0
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001890 Tmp2 = MakeReg(MVT::i64);
1891 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001892 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001893 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001894 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001895 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001896 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001897 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001898
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001899 case ISD::CopyFromReg:
1900 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001901 ++count_ins;
1902
Andrew Lenharth40831c52005-01-28 06:57:18 +00001903 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001904 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001905 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001906 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001907 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001908
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001909 SDOperand Chain = N.getOperand(0);
1910
1911 Select(Chain);
1912 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1913 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1914 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1915 return Result;
1916 }
1917
Misha Brukman4633f1c2005-04-21 23:13:11 +00001918 //Most of the plain arithmetic and logic share the same form, and the same
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001919 //constant immediate test
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001920 case ISD::XOR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001921 //Match Not
1922 if (N.getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001923 cast<ConstantSDNode>(N.getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001924 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001925 Tmp1 = SelectExpr(N.getOperand(0));
1926 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1927 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001928 }
1929 //Fall through
1930 case ISD::AND:
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001931 //handle zap
1932 if (opcode == ISD::AND && N.getOperand(1).getOpcode() == ISD::Constant)
1933 {
1934 uint64_t k = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1935 unsigned int build = 0;
1936 for(int i = 0; i < 8; ++i)
1937 {
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001938 if ((k & 0x00FF) == 0x00FF)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001939 build |= 1 << i;
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001940 else if ((k & 0x00FF) != 0)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001941 { build = 0; break; }
1942 k >>= 8;
1943 }
1944 if (build)
1945 {
1946 Tmp1 = SelectExpr(N.getOperand(0));
1947 BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1948 return Result;
1949 }
1950 }
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001951 case ISD::OR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001952 //Check operand(0) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001953 if (N.getOperand(0).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001954 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001955 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001956 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001957 switch(opcode) {
1958 case ISD::AND: Opc = Alpha::BIC; break;
1959 case ISD::OR: Opc = Alpha::ORNOT; break;
1960 case ISD::XOR: Opc = Alpha::EQV; break;
1961 }
1962 Tmp1 = SelectExpr(N.getOperand(1));
1963 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1964 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1965 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001966 }
1967 //Check operand(1) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001968 if (N.getOperand(1).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001969 N.getOperand(1).getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001970 cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001971 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001972 switch(opcode) {
1973 case ISD::AND: Opc = Alpha::BIC; break;
1974 case ISD::OR: Opc = Alpha::ORNOT; break;
1975 case ISD::XOR: Opc = Alpha::EQV; break;
1976 }
1977 Tmp1 = SelectExpr(N.getOperand(0));
1978 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1979 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1980 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001981 }
1982 //Fall through
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001983 case ISD::SHL:
1984 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001985 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001986 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001987 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1988 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001989 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001990 {
1991 switch(opcode) {
1992 case ISD::AND: Opc = Alpha::ANDi; break;
1993 case ISD::OR: Opc = Alpha::BISi; break;
1994 case ISD::XOR: Opc = Alpha::XORi; break;
1995 case ISD::SHL: Opc = Alpha::SLi; break;
1996 case ISD::SRL: Opc = Alpha::SRLi; break;
1997 case ISD::SRA: Opc = Alpha::SRAi; break;
1998 case ISD::MUL: Opc = Alpha::MULQi; break;
1999 };
2000 Tmp1 = SelectExpr(N.getOperand(0));
2001 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2002 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
2003 } else {
2004 switch(opcode) {
2005 case ISD::AND: Opc = Alpha::AND; break;
2006 case ISD::OR: Opc = Alpha::BIS; break;
2007 case ISD::XOR: Opc = Alpha::XOR; break;
2008 case ISD::SHL: Opc = Alpha::SL; break;
2009 case ISD::SRL: Opc = Alpha::SRL; break;
2010 case ISD::SRA: Opc = Alpha::SRA; break;
2011 case ISD::MUL: Opc = Alpha::MULQ; break;
2012 };
2013 Tmp1 = SelectExpr(N.getOperand(0));
2014 Tmp2 = SelectExpr(N.getOperand(1));
2015 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2016 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00002017 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002018
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002019 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002020 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00002021 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00002022 bool isAdd = opcode == ISD::ADD;
2023
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002024 //first check for Scaled Adds and Subs!
2025 //Valid for add and sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002026 ConstantSDNode* CSD = NULL;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002027 if(N.getOperand(0).getOpcode() == ISD::SHL &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002028 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
2029 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002030 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002031 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002032 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002033 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) && CSD->getValue() <= 255)
2034 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
2035 2, Result).addReg(Tmp2).addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00002036 else {
2037 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002038 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
2039 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00002040 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002041 }
2042 //Position prevents subs
Andrew Lenharth273a1f92005-04-07 14:18:13 +00002043 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002044 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) &&
2045 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002046 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002047 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002048 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002049 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(0))) && CSD->getValue() <= 255)
2050 BuildMI(BB, use4?Alpha::S4ADDQi:Alpha::S8ADDQi, 2, Result).addReg(Tmp2)
2051 .addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00002052 else {
2053 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002054 BuildMI(BB, use4?Alpha::S4ADDQ:Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00002055 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002056 }
2057 //small addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002058 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
2059 CSD->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002060 { //Normal imm add/sub
2061 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
2062 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002063 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CSD->getValue());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002064 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002065 //larger addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002066 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
2067 CSD->getSignExtended() <= 32767 &&
2068 CSD->getSignExtended() >= -32767)
Andrew Lenharth74d00d82005-03-02 17:23:03 +00002069 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002070 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002071 Tmp2 = (long)CSD->getSignExtended();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002072 if (!isAdd)
2073 Tmp2 = -Tmp2;
2074 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002075 }
2076 //give up and do the operation
2077 else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002078 //Normal add/sub
2079 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
2080 Tmp1 = SelectExpr(N.getOperand(0));
2081 Tmp2 = SelectExpr(N.getOperand(1));
2082 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2083 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002084 return Result;
2085 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002086
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00002087 case ISD::SDIV:
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002088 {
Andrew Lenhartha565c272005-04-06 22:03:13 +00002089 ConstantSDNode* CSD;
2090 //check if we can convert into a shift!
2091 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
2092 (int64_t)CSD->getSignExtended() != 0 &&
2093 ExactLog2(abs((int64_t)CSD->getSignExtended())) != 0)
2094 {
2095 unsigned k = ExactLog2(abs(CSD->getSignExtended()));
2096 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenhartha565c272005-04-06 22:03:13 +00002097 if (k == 1)
2098 Tmp2 = Tmp1;
2099 else
2100 {
2101 Tmp2 = MakeReg(MVT::i64);
2102 BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
2103 }
2104 Tmp3 = MakeReg(MVT::i64);
2105 BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
2106 unsigned Tmp4 = MakeReg(MVT::i64);
2107 BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
2108 if ((int64_t)CSD->getSignExtended() > 0)
2109 BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
2110 else
2111 {
2112 unsigned Tmp5 = MakeReg(MVT::i64);
2113 BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
2114 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
2115 }
2116 return Result;
2117 }
2118 }
2119 //Else fall through
2120
2121 case ISD::UDIV:
2122 {
2123 ConstantSDNode* CSD;
2124 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
2125 ((int64_t)CSD->getSignExtended() >= 2 ||
2126 (int64_t)CSD->getSignExtended() <= -2))
2127 {
2128 // If this is a divide by constant, we can emit code using some magic
2129 // constants to implement it as a multiply instead.
2130 ExprMap.erase(N);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002131 if (opcode == ISD::SDIV)
Andrew Lenhartha565c272005-04-06 22:03:13 +00002132 return SelectExpr(BuildSDIVSequence(N));
2133 else
2134 return SelectExpr(BuildUDIVSequence(N));
2135 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002136 }
2137 //else fall though
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002138 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +00002139 case ISD::SREM:
Misha Brukman4633f1c2005-04-21 23:13:11 +00002140 //FIXME: alpha really doesn't support any of these operations,
Andrew Lenharth40831c52005-01-28 06:57:18 +00002141 // the ops are expanded into special library calls with
2142 // special calling conventions
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002143 //Restore GP because it is a call after all...
Andrew Lenharth40831c52005-01-28 06:57:18 +00002144 switch(opcode) {
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00002145 case ISD::UREM: Opc = Alpha::REMQU; break;
2146 case ISD::SREM: Opc = Alpha::REMQ; break;
2147 case ISD::UDIV: Opc = Alpha::DIVQU; break;
2148 case ISD::SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002149 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002150 Tmp1 = SelectExpr(N.getOperand(0));
2151 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth33819132005-03-04 20:09:23 +00002152 //set up regs explicitly (helps Reg alloc)
2153 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002154 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00002155 AlphaLowering.restoreGP(BB);
Andrew Lenharth33819132005-03-04 20:09:23 +00002156 BuildMI(BB, Opc, 2).addReg(Alpha::R24).addReg(Alpha::R25);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002157 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002158 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002159
Andrew Lenharthe76797c2005-02-01 20:40:27 +00002160 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002161 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002162 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002163 assert (DestType == MVT::i64 && "only quads can be loaded to");
2164 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00002165 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002166 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002167 if (SrcType == MVT::f32)
Misha Brukman7847fca2005-04-22 17:54:37 +00002168 {
2169 Tmp2 = MakeReg(MVT::f64);
2170 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
2171 Tmp1 = Tmp2;
2172 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002173 Tmp2 = MakeReg(MVT::f64);
2174 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00002175 MoveFP2Int(Tmp2, Result, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002176
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002177 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002178 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002179
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002180 case ISD::SELECT:
2181 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00002182 //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 +00002183 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002184 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2185 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002186 // Get the condition into the zero flag.
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002187 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002188
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002189 SDOperand CC = N.getOperand(0);
2190 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
2191
Misha Brukman4633f1c2005-04-21 23:13:11 +00002192 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002193 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
2194 { //FP Setcc -> Int Select
Misha Brukman7847fca2005-04-22 17:54:37 +00002195 Tmp1 = MakeReg(MVT::f64);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002196 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2197 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Misha Brukman7847fca2005-04-22 17:54:37 +00002198 bool inv = SelectFPSetCC(CC, Tmp1);
2199 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
2200 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
2201 return Result;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002202 }
2203 if (CC.getOpcode() == ISD::SETCC) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002204 //Int SetCC -> Select
2205 //Dropping the CC is only useful if we are comparing to 0
2206 if((SetCC->getOperand(1).getOpcode() == ISD::Constant &&
2207 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0) ||
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002208 (SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2209 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0))
2210 {
2211 //figure out a few things
2212 bool LeftZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2213 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
2214 bool RightZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2215 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
2216 bool LeftConst = N.getOperand(1).getOpcode() == ISD::Constant &&
2217 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255;
2218 bool RightConst = N.getOperand(2).getOpcode() == ISD::Constant &&
2219 cast<ConstantSDNode>(N.getOperand(2))->getValue() <= 255;
2220 bool useImm = LeftConst || RightConst;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002221
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002222 //Fix up CC
2223 ISD::CondCode cCode= SetCC->getCondition();
2224 if (RightConst && !LeftConst) //Invert sense to get Imm field right
2225 cCode = ISD::getSetCCInverse(cCode, true);
2226 if (LeftZero && !RightZero) //Swap Operands
2227 cCode = ISD::getSetCCSwappedOperands(cCode);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002228
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002229 //Choose the CMOV
2230 switch (cCode) {
2231 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
2232 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
2233 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
2234 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
2235 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
2236 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
2237 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
2238 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2239 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break; //Technically you could have this CC
2240 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
2241 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2242 }
2243 if(LeftZero && !RightZero) //swap Operands
2244 Tmp1 = SelectExpr(SetCC->getOperand(1)); //Cond
2245 else
2246 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
2247
2248 if (LeftConst) {
2249 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2250 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
Misha Brukman7847fca2005-04-22 17:54:37 +00002251 .addImm(cast<ConstantSDNode>(N.getOperand(1))->getValue())
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002252 .addReg(Tmp1);
2253 } else if (RightConst) {
2254 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
2255 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
Misha Brukman7847fca2005-04-22 17:54:37 +00002256 .addImm(cast<ConstantSDNode>(N.getOperand(2))->getValue())
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002257 .addReg(Tmp1);
2258 } else {
2259 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2260 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2261 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
2262 }
2263 return Result;
2264 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002265 //Otherwise, fall though
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002266 }
2267 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002268 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2269 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthe76797c2005-02-01 20:40:27 +00002270 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002271
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002272 return Result;
2273 }
2274
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002275 case ISD::Constant:
2276 {
Andrew Lenharthc0513832005-03-29 19:24:04 +00002277 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002278 if (val <= IMM_HIGH && val >= IMM_LOW) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002279 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002280 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002281 else if (val <= (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT &&
2282 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
2283 Tmp1 = MakeReg(MVT::i64);
2284 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val)).addReg(Alpha::R31);
2285 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002286 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002287 else {
2288 MachineConstantPool *CP = BB->getParent()->getConstantPool();
2289 ConstantUInt *C = ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
2290 unsigned CPI = CP->getConstantPoolIndex(C);
2291 AlphaLowering.restoreGP(BB);
2292 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CPI);
2293 }
2294 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002295 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002296 }
2297
2298 return 0;
2299}
2300
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002301void AlphaISel::Select(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002302 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00002303 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002304
Nate Begeman85fdeb22005-03-24 04:39:54 +00002305 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002306 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002307
2308 SDNode *Node = N.Val;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002309
Andrew Lenharth760270d2005-02-07 23:02:23 +00002310 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002311
2312 default:
2313 Node->dump(); std::cerr << "\n";
2314 assert(0 && "Node not handled yet!");
2315
2316 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00002317 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002318 return;
2319 }
2320
2321 case ISD::BR: {
2322 MachineBasicBlock *Dest =
2323 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2324
2325 Select(N.getOperand(0));
2326 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
2327 return;
2328 }
2329
2330 case ISD::ImplicitDef:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002331 ++count_ins;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002332 Select(N.getOperand(0));
2333 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
2334 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002335
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002336 case ISD::EntryToken: return; // Noop
2337
2338 case ISD::TokenFactor:
2339 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2340 Select(Node->getOperand(i));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002341
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002342 //N.Val->dump(); std::cerr << "\n";
2343 //assert(0 && "Node not handled yet!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00002344
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002345 return;
2346
2347 case ISD::CopyToReg:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002348 ++count_outs;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002349 Select(N.getOperand(0));
2350 Tmp1 = SelectExpr(N.getOperand(1));
2351 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002352
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002353 if (Tmp1 != Tmp2) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002354 if (N.getOperand(1).getValueType() == MVT::f64 ||
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002355 N.getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00002356 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2357 else
2358 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002359 }
2360 return;
2361
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002362 case ISD::RET:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002363 ++count_outs;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002364 switch (N.getNumOperands()) {
2365 default:
2366 std::cerr << N.getNumOperands() << "\n";
2367 for (unsigned i = 0; i < N.getNumOperands(); ++i)
2368 std::cerr << N.getOperand(i).getValueType() << "\n";
2369 Node->dump();
2370 assert(0 && "Unknown return instruction!");
2371 case 2:
2372 Select(N.getOperand(0));
2373 Tmp1 = SelectExpr(N.getOperand(1));
2374 switch (N.getOperand(1).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002375 default: Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002376 assert(0 && "All other types should have been promoted!!");
2377 case MVT::f64:
2378 case MVT::f32:
2379 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
2380 break;
2381 case MVT::i32:
2382 case MVT::i64:
2383 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
2384 break;
2385 }
2386 break;
2387 case 1:
2388 Select(N.getOperand(0));
2389 break;
2390 }
2391 //Tmp2 = AlphaLowering.getRetAddr();
2392 //BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(Tmp2).addReg(Tmp2);
2393 BuildMI(BB, Alpha::RETURN, 0); // Just emit a 'ret' instruction
2394 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002395
Misha Brukman4633f1c2005-04-21 23:13:11 +00002396 case ISD::TRUNCSTORE:
2397 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002398 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002399 SDOperand Chain = N.getOperand(0);
2400 SDOperand Value = N.getOperand(1);
2401 SDOperand Address = N.getOperand(2);
2402 Select(Chain);
2403
2404 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00002405
2406 if (opcode == ISD::STORE) {
2407 switch(Value.getValueType()) {
2408 default: assert(0 && "unknown Type in store");
2409 case MVT::i64: Opc = Alpha::STQ; break;
2410 case MVT::f64: Opc = Alpha::STT; break;
2411 case MVT::f32: Opc = Alpha::STS; break;
2412 }
2413 } else { //ISD::TRUNCSTORE
2414 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2415 default: assert(0 && "unknown Type in store");
2416 case MVT::i1: //FIXME: DAG does not promote this load
2417 case MVT::i8: Opc = Alpha::STB; break;
2418 case MVT::i16: Opc = Alpha::STW; break;
2419 case MVT::i32: Opc = Alpha::STL; break;
2420 }
Andrew Lenharth65838902005-02-06 16:22:15 +00002421 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00002422
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002423 if (EnableAlphaLSMark)
2424 {
2425 int i = getValueOffset(dyn_cast<SrcValueSDNode>(N.getOperand(3))->getValue());
2426 int j = getFunctionOffset(BB->getParent()->getFunction());
2427 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
2428 }
2429
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002430 if (Address.getOpcode() == ISD::GlobalAddress)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002431 {
2432 AlphaLowering.restoreGP(BB);
2433 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002434 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002435 BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
2436 }
Andrew Lenharth05380342005-02-07 05:07:00 +00002437 else if(Address.getOpcode() == ISD::FrameIndex)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002438 {
Andrew Lenharth032f2352005-02-22 21:59:48 +00002439 BuildMI(BB, Opc, 3).addReg(Tmp1)
2440 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
2441 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002442 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002443 else
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002444 {
2445 long offset;
2446 SelectAddr(Address, Tmp2, offset);
2447 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2448 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002449 return;
2450 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002451
2452 case ISD::EXTLOAD:
2453 case ISD::SEXTLOAD:
2454 case ISD::ZEXTLOAD:
2455 case ISD::LOAD:
2456 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002457 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002458 case ISD::CALL:
Andrew Lenharth032f2352005-02-22 21:59:48 +00002459 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002460 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002461 SelectExpr(N);
2462 return;
2463
Chris Lattner16cd04d2005-05-12 23:24:06 +00002464 case ISD::CALLSEQ_START:
2465 case ISD::CALLSEQ_END:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002466 Select(N.getOperand(0));
2467 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002468
Chris Lattner16cd04d2005-05-12 23:24:06 +00002469 Opc = N.getOpcode() == ISD::CALLSEQ_START ? Alpha::ADJUSTSTACKDOWN :
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002470 Alpha::ADJUSTSTACKUP;
2471 BuildMI(BB, Opc, 1).addImm(Tmp1);
2472 return;
Andrew Lenharth95762122005-03-31 21:24:06 +00002473
2474 case ISD::PCMARKER:
2475 Select(N.getOperand(0)); //Chain
2476 BuildMI(BB, Alpha::PCLABEL, 2).addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
2477 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002478 }
2479 assert(0 && "Should not be reached!");
2480}
2481
2482
2483/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
2484/// into a machine code representation using pattern matching and a machine
2485/// description file.
2486///
2487FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002488 return new AlphaISel(TM);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002489}
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002490