blob: 32c3bfa679a0997e66a7dc1bb810ab40defe1fdc [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
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +000079 unsigned RA; //Return Address
Andrew Lenharth304d0f32005-01-22 23:41:55 +000080 public:
81 AlphaTargetLowering(TargetMachine &TM) : TargetLowering(TM) {
82 // Set up the TargetLowering object.
Andrew Lenharth3d65d312005-01-27 03:49:45 +000083 //I am having problems with shr n ubyte 1
Andrew Lenharth879ef222005-02-02 17:00:21 +000084 setShiftAmountType(MVT::i64);
85 setSetCCResultType(MVT::i64);
Andrew Lenharthd3355e22005-04-07 20:11:32 +000086 setSetCCResultContents(ZeroOrOneSetCCResult);
Misha Brukman4633f1c2005-04-21 23:13:11 +000087
Andrew Lenharth304d0f32005-01-22 23:41:55 +000088 addRegisterClass(MVT::i64, Alpha::GPRCRegisterClass);
89 addRegisterClass(MVT::f64, Alpha::FPRCRegisterClass);
Andrew Lenharth3d65d312005-01-27 03:49:45 +000090 addRegisterClass(MVT::f32, Alpha::FPRCRegisterClass);
Misha Brukman4633f1c2005-04-21 23:13:11 +000091
Chris Lattnerda4d4692005-04-09 03:22:37 +000092 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000093 setOperationAction(ISD::EXTLOAD , MVT::i1 , Promote);
94 setOperationAction(ISD::EXTLOAD , MVT::f32 , Promote);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000095
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000096 setOperationAction(ISD::ZEXTLOAD , MVT::i1 , Expand);
97 setOperationAction(ISD::ZEXTLOAD , MVT::i32 , Expand);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000098
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000099 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
100 setOperationAction(ISD::SEXTLOAD , MVT::i8 , Expand);
101 setOperationAction(ISD::SEXTLOAD , MVT::i16 , Expand);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000102
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000103 setOperationAction(ISD::SREM , MVT::f32 , Expand);
104 setOperationAction(ISD::SREM , MVT::f64 , Expand);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000105
Andrew Lenharth59009192005-05-04 19:12:09 +0000106 if (!EnableAlphaCT) {
107 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
108 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
Andrew Lenharthb5884d32005-05-04 19:25:37 +0000109 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
Andrew Lenharth59009192005-05-04 19:12:09 +0000110 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000111
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000112 //If this didn't legalize into a div....
113 // setOperationAction(ISD::SREM , MVT::i64, Expand);
114 // setOperationAction(ISD::UREM , MVT::i64, Expand);
115
116 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
117 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
118 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
Andrew Lenharth9818c052005-02-05 13:19:12 +0000119
Chris Lattner17234b72005-04-30 04:26:06 +0000120 // We don't support sin/cos/sqrt
121 setOperationAction(ISD::FSIN , MVT::f64, Expand);
122 setOperationAction(ISD::FCOS , MVT::f64, Expand);
123 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
124 setOperationAction(ISD::FSIN , MVT::f32, Expand);
125 setOperationAction(ISD::FCOS , MVT::f32, Expand);
126 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
127
Andrew Lenharth33819132005-03-04 20:09:23 +0000128 //Doesn't work yet
Chris Lattner17234b72005-04-30 04:26:06 +0000129 setOperationAction(ISD::SETCC, MVT::f32, Promote);
Andrew Lenharth572af902005-02-14 05:41:43 +0000130
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000131 //Try a couple things with a custom expander
132 //setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
133
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000134 computeRegisterProperties();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000135
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000136 addLegalFPImmediate(+0.0); //F31
137 addLegalFPImmediate(-0.0); //-F31
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000138 }
139
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000140 /// LowerOperation - Provide custom lowering hooks for some operations.
141 ///
142 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
143
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000144 /// LowerArguments - This hook must be implemented to indicate how we should
145 /// lower the arguments for the specified function, into the specified DAG.
146 virtual std::vector<SDOperand>
147 LowerArguments(Function &F, SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000148
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000149 /// LowerCallTo - This hook lowers an abstract call to a function into an
150 /// actual call.
151 virtual std::pair<SDOperand, SDOperand>
Chris Lattnerc57f6822005-05-12 19:56:45 +0000152 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattneradf6a962005-05-13 18:50:42 +0000153 bool isTailCall, SDOperand Callee, ArgListTy &Args,
154 SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000155
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000156 virtual std::pair<SDOperand, SDOperand>
Andrew Lenharth558bc882005-06-18 18:34:52 +0000157 LowerVAStart(SDOperand Chain, SelectionDAG &DAG, SDOperand Dest);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000158
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000159 virtual std::pair<SDOperand,SDOperand>
Andrew Lenharth558bc882005-06-18 18:34:52 +0000160 LowerVAArgNext(SDOperand Chain, SDOperand VAList,
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000161 const Type *ArgTy, SelectionDAG &DAG);
162
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000163 std::pair<SDOperand,SDOperand>
164 LowerVACopy(SDOperand Chain, SDOperand Src, SDOperand Dest,
165 SelectionDAG &DAG);
166
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000167 virtual std::pair<SDOperand, SDOperand>
168 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
169 SelectionDAG &DAG);
170
171 void restoreGP(MachineBasicBlock* BB)
172 {
173 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
174 }
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000175 void restoreRA(MachineBasicBlock* BB)
176 {
177 BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(RA).addReg(RA);
178 }
179
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000180 };
181}
182
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000183/// LowerOperation - Provide custom lowering hooks for some operations.
184///
185SDOperand AlphaTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
186 MachineFunction &MF = DAG.getMachineFunction();
187 switch (Op.getOpcode()) {
188 default: assert(0 && "Should not custom lower this!");
Misha Brukmanb8ee91a2005-06-06 17:39:46 +0000189#if 0
190 case ISD::SINT_TO_FP:
191 {
192 assert (Op.getOperand(0).getValueType() == MVT::i64
193 && "only quads can be loaded from");
194 SDOperand SRC;
195 if (EnableAlphaFTOI)
196 {
197 std::vector<MVT::ValueType> RTs;
198 RTs.push_back(Op.getValueType());
199 std::vector<SDOperand> Ops;
200 Ops.push_back(Op.getOperand(0));
201 SRC = DAG.getNode(AlphaISD::ITOF, RTs, Ops);
202 } else {
203 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
204 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
205 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
206 Op.getOperand(0), StackSlot, DAG.getSrcValue(NULL));
207 SRC = DAG.getLoad(Op.getValueType(), Store.getValue(0), StackSlot,
208 DAG.getSrcValue(NULL));
209 }
210 std::vector<MVT::ValueType> RTs;
211 RTs.push_back(Op.getValueType());
212 std::vector<SDOperand> Ops;
213 Ops.push_back(SRC);
214 return DAG.getNode(AlphaISD::CVTQ, RTs, Ops);
215 }
216#endif
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000217 }
Misha Brukmanb8ee91a2005-06-06 17:39:46 +0000218 return SDOperand();
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000219}
220
221
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000222/// AddLiveIn - This helper function adds the specified physical register to the
223/// MachineFunction as a live in value. It also creates a corresponding virtual
224/// register for it.
225static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
226 TargetRegisterClass *RC) {
227 assert(RC->contains(PReg) && "Not the correct regclass!");
228 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
229 MF.addLiveIn(PReg, VReg);
230 return VReg;
231}
232
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000233//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
234
235//For now, just use variable size stack frame format
236
237//In a standard call, the first six items are passed in registers $16
238//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
239//of argument-to-register correspondence.) The remaining items are
240//collected in a memory argument list that is a naturally aligned
241//array of quadwords. In a standard call, this list, if present, must
242//be passed at 0(SP).
Misha Brukman7847fca2005-04-22 17:54:37 +0000243//7 ... n 0(SP) ... (n-7)*8(SP)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000244
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000245// //#define FP $15
246// //#define RA $26
247// //#define PV $27
248// //#define GP $29
249// //#define SP $30
Misha Brukman4633f1c2005-04-21 23:13:11 +0000250
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000251std::vector<SDOperand>
Misha Brukman4633f1c2005-04-21 23:13:11 +0000252AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000253{
254 std::vector<SDOperand> ArgValues;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000255
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000256 MachineFunction &MF = DAG.getMachineFunction();
Andrew Lenharth05380342005-02-07 05:07:00 +0000257 MachineFrameInfo*MFI = MF.getFrameInfo();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000258
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000259 MachineBasicBlock& BB = MF.front();
260
261 //Handle the return address
262 //BuildMI(&BB, Alpha::IDEF, 0, Alpha::R26);
263
Misha Brukman4633f1c2005-04-21 23:13:11 +0000264 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000265 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000266 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000267 Alpha::F19, Alpha::F20, Alpha::F21};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000268 int count = 0;
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000269
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000270 GP = AddLiveIn(MF, Alpha::R29, getRegClassFor(MVT::i64));
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000271 RA = AddLiveIn(MF, Alpha::R26, getRegClassFor(MVT::i64));
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000272
Chris Lattnere4d5c442005-03-15 04:54:21 +0000273 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000274 {
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000275 SDOperand argt;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000276 if (count < 6) {
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000277 unsigned Vreg;
278 MVT::ValueType VT = getValueType(I->getType());
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000279 switch (VT) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000280 default:
281 std::cerr << "Unknown Type " << VT << "\n";
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000282 abort();
283 case MVT::f64:
284 case MVT::f32:
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000285 args_float[count] = AddLiveIn(MF,args_float[count], getRegClassFor(VT));
286 argt = DAG.getCopyFromReg(args_float[count], VT, DAG.getRoot());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000287 break;
288 case MVT::i1:
289 case MVT::i8:
290 case MVT::i16:
291 case MVT::i32:
292 case MVT::i64:
Andrew Lenharth591ec572005-05-31 18:42:18 +0000293 args_int[count] = AddLiveIn(MF, args_int[count], getRegClassFor(MVT::i64));
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000294 argt = DAG.getCopyFromReg(args_int[count], VT, DAG.getRoot());
Andrew Lenharth14f30c92005-05-31 18:37:16 +0000295 if (VT != MVT::i64)
296 argt = DAG.getNode(ISD::TRUNCATE, VT, argt);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000297 break;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000298 }
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000299 DAG.setRoot(argt.getValue(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000300 } else { //more args
301 // Create the frame index object for this incoming parameter...
302 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000303
304 // Create the SelectionDAG nodes corresponding to a load
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000305 //from this parameter
306 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000307 argt = DAG.getLoad(getValueType(I->getType()),
308 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000309 }
Andrew Lenharth032f2352005-02-22 21:59:48 +0000310 ++count;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000311 ArgValues.push_back(argt);
312 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000313
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000314 // If the functions takes variable number of arguments, copy all regs to stack
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000315 if (F.isVarArg()) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000316 VarArgsOffset = count * 8;
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000317 std::vector<SDOperand> LS;
318 for (int i = 0; i < 6; ++i) {
319 if (args_int[i] < 1024)
320 args_int[i] = AddLiveIn(MF,args_int[i], getRegClassFor(MVT::i64));
321 SDOperand argt = DAG.getCopyFromReg(args_int[i], MVT::i64, DAG.getRoot());
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000322 int FI = MFI->CreateFixedObject(8, -8 * (6 - i));
Andrew Lenharth558bc882005-06-18 18:34:52 +0000323 if (i == 0) VarArgsBase = FI;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000324 SDOperand 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)));
326
327 if (args_float[i] < 1024)
328 args_float[i] = AddLiveIn(MF,args_float[i], getRegClassFor(MVT::f64));
329 argt = DAG.getCopyFromReg(args_float[i], MVT::f64, DAG.getRoot());
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000330 FI = MFI->CreateFixedObject(8, - 8 * (12 - i));
331 SDFI = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000332 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt, SDFI, DAG.getSrcValue(NULL)));
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000333 }
334
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000335 //Set up a token factor with all the stack traffic
336 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, LS));
337 }
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000338
339 // Finally, inform the code generator which regs we return values in.
340 switch (getValueType(F.getReturnType())) {
341 default: assert(0 && "Unknown type!");
342 case MVT::isVoid: break;
343 case MVT::i1:
344 case MVT::i8:
345 case MVT::i16:
346 case MVT::i32:
347 case MVT::i64:
348 MF.addLiveOut(Alpha::R0);
349 break;
350 case MVT::f32:
351 case MVT::f64:
352 MF.addLiveOut(Alpha::F0);
353 break;
354 }
355
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000356 //return the arguments
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000357 return ArgValues;
358}
359
360std::pair<SDOperand, SDOperand>
361AlphaTargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000362 const Type *RetTy, bool isVarArg,
Chris Lattneradf6a962005-05-13 18:50:42 +0000363 unsigned CallingConv, bool isTailCall,
Misha Brukman7847fca2005-04-22 17:54:37 +0000364 SDOperand Callee, ArgListTy &Args,
365 SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000366 int NumBytes = 0;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000367 if (Args.size() > 6)
368 NumBytes = (Args.size() - 6) * 8;
369
Chris Lattner16cd04d2005-05-12 23:24:06 +0000370 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000371 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000372 std::vector<SDOperand> args_to_use;
373 for (unsigned i = 0, e = Args.size(); i != e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000374 {
375 switch (getValueType(Args[i].second)) {
376 default: assert(0 && "Unexpected ValueType for argument!");
377 case MVT::i1:
378 case MVT::i8:
379 case MVT::i16:
380 case MVT::i32:
381 // Promote the integer to 64 bits. If the input type is signed use a
382 // sign extend, otherwise use a zero extend.
383 if (Args[i].second->isSigned())
384 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
385 else
386 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
387 break;
388 case MVT::i64:
389 case MVT::f64:
390 case MVT::f32:
391 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000392 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000393 args_to_use.push_back(Args[i].first);
394 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000395
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000396 std::vector<MVT::ValueType> RetVals;
397 MVT::ValueType RetTyVT = getValueType(RetTy);
398 if (RetTyVT != MVT::isVoid)
399 RetVals.push_back(RetTyVT);
400 RetVals.push_back(MVT::Other);
401
Misha Brukman4633f1c2005-04-21 23:13:11 +0000402 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000403 Chain, Callee, args_to_use), 0);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000404 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner16cd04d2005-05-12 23:24:06 +0000405 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000406 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000407 return std::make_pair(TheCall, Chain);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000408}
409
410std::pair<SDOperand, SDOperand>
Andrew Lenharth558bc882005-06-18 18:34:52 +0000411AlphaTargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG, SDOperand Dest) {
412 // vastart just stores the address of the VarArgsBase and VarArgsOffset
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000413 SDOperand FR = DAG.getFrameIndex(VarArgsBase, MVT::i64);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000414 SDOperand S1 = DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, Dest, DAG.getSrcValue(NULL));
415 SDOperand SA2 = DAG.getNode(ISD::ADD, MVT::i64, Dest, DAG.getConstant(8, MVT::i64));
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000416 SDOperand S2 = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, S1,
Andrew Lenharth558bc882005-06-18 18:34:52 +0000417 DAG.getConstant(VarArgsOffset, MVT::i64), SA2,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000418 DAG.getSrcValue(NULL), MVT::i32);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000419 return std::make_pair(S2, S2);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000420}
421
422std::pair<SDOperand,SDOperand> AlphaTargetLowering::
Andrew Lenharth558bc882005-06-18 18:34:52 +0000423LowerVAArgNext(SDOperand Chain, SDOperand VAList,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000424 const Type *ArgTy, SelectionDAG &DAG) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000425 SDOperand Base = DAG.getLoad(MVT::i64, Chain, VAList, DAG.getSrcValue(NULL));
426 SDOperand Tmp = DAG.getNode(ISD::ADD, MVT::i64, VAList,
427 DAG.getConstant(8, MVT::i64));
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000428 SDOperand Offset = DAG.getNode(ISD::SEXTLOAD, MVT::i64, Base.getValue(1), Tmp,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000429 DAG.getSrcValue(NULL), MVT::i32);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000430 SDOperand DataPtr = DAG.getNode(ISD::ADD, MVT::i64, Base, Offset);
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000431 if (ArgTy->isFloatingPoint())
432 {
433 //if fp && Offset < 6*8, then subtract 6*8 from DataPtr
434 SDOperand FPDataPtr = DAG.getNode(ISD::SUB, MVT::i64, DataPtr,
435 DAG.getConstant(8*6, MVT::i64));
436 SDOperand CC = DAG.getSetCC(ISD::SETLT, MVT::i64,
437 Offset, DAG.getConstant(8*6, MVT::i64));
438 DataPtr = DAG.getNode(ISD::SELECT, MVT::i64, CC, FPDataPtr, DataPtr);
439 }
440
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000441 SDOperand Result;
442 if (ArgTy == Type::IntTy)
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000443 Result = DAG.getNode(ISD::SEXTLOAD, MVT::i64, Offset.getValue(1), DataPtr,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000444 DAG.getSrcValue(NULL), MVT::i32);
445 else if (ArgTy == Type::UIntTy)
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000446 Result = DAG.getNode(ISD::ZEXTLOAD, MVT::i64, Offset.getValue(1), DataPtr,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000447 DAG.getSrcValue(NULL), MVT::i32);
448 else
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000449 Result = DAG.getLoad(getValueType(ArgTy), Offset.getValue(1), DataPtr,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000450 DAG.getSrcValue(NULL));
451
Andrew Lenharth558bc882005-06-18 18:34:52 +0000452 SDOperand NewOffset = DAG.getNode(ISD::ADD, MVT::i64, Offset,
453 DAG.getConstant(8, MVT::i64));
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000454 SDOperand Update = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Result.getValue(1), NewOffset,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000455 Tmp, DAG.getSrcValue(NULL), MVT::i32);
456 Result = DAG.getNode(ISD::TRUNCATE, getValueType(ArgTy), Result);
457
Andrew Lenharth558bc882005-06-18 18:34:52 +0000458 return std::make_pair(Result, Update);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000459}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000460
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000461std::pair<SDOperand,SDOperand> AlphaTargetLowering::
462LowerVACopy(SDOperand Chain, SDOperand Src, SDOperand Dest,
463 SelectionDAG &DAG) {
464 //Default to returning the input list
465 SDOperand Val = DAG.getLoad(getPointerTy(), Chain, Src, DAG.getSrcValue(NULL));
466 SDOperand Result = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
467 Val, Dest, DAG.getSrcValue(NULL));
468 SDOperand NP = DAG.getNode(ISD::ADD, MVT::i64, Src,
469 DAG.getConstant(8, MVT::i64));
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000470 Val = DAG.getNode(ISD::SEXTLOAD, MVT::i64, Result, NP, DAG.getSrcValue(NULL),
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000471 MVT::i32);
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000472 SDOperand NPD = DAG.getNode(ISD::ADD, MVT::i64, Dest,
473 DAG.getConstant(8, MVT::i64));
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000474 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Val.getValue(1),
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000475 Val, NPD, DAG.getSrcValue(NULL), MVT::i32);
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000476 return std::make_pair(Result, Result);
477}
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000478
479std::pair<SDOperand, SDOperand> AlphaTargetLowering::
480LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
481 SelectionDAG &DAG) {
482 abort();
483}
484
485
486
487
488
489namespace {
490
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000491//===--------------------------------------------------------------------===//
492/// ISel - Alpha specific code to select Alpha machine instructions for
493/// SelectionDAG operations.
494//===--------------------------------------------------------------------===//
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000495class AlphaISel : public SelectionDAGISel {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000496
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000497 /// AlphaLowering - This object fully describes how to lower LLVM code to an
498 /// Alpha-specific SelectionDAG.
499 AlphaTargetLowering AlphaLowering;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000500
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000501 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
502 // for sdiv and udiv until it is put into the future
503 // dag combiner.
504
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000505 /// ExprMap - As shared expressions are codegen'd, we keep track of which
506 /// vreg the value is produced in, so we only emit one copy of each compiled
507 /// tree.
508 static const unsigned notIn = (unsigned)(-1);
509 std::map<SDOperand, unsigned> ExprMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000510
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000511 //CCInvMap sometimes (SetNE) we have the inverse CC code for free
512 std::map<SDOperand, unsigned> CCInvMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000513
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000514 int count_ins;
515 int count_outs;
516 bool has_sym;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000517 int max_depth;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000518
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000519public:
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000520 AlphaISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering), AlphaLowering(TM)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000521 {}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000522
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000523 /// InstructionSelectBasicBlock - This callback is invoked by
524 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
525 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
Andrew Lenharth032f2352005-02-22 21:59:48 +0000526 DEBUG(BB->dump());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000527 count_ins = 0;
528 count_outs = 0;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000529 max_depth = 0;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000530 has_sym = false;
531
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000532 // Codegen the basic block.
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000533 ISelDAG = &DAG;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000534 max_depth = DAG.getRoot().getNodeDepth();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000535 Select(DAG.getRoot());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000536
537 if(has_sym)
538 ++count_ins;
539 if(EnableAlphaCount)
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000540 std::cerr << "COUNT: " << BB->getParent()->getFunction ()->getName() << " "
541 << BB->getNumber() << " "
542 << max_depth << " "
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000543 << count_ins << " "
544 << count_outs << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000545
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000546 // Clear state used for selection.
547 ExprMap.clear();
548 CCInvMap.clear();
549 }
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000550
551 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000552
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000553 unsigned SelectExpr(SDOperand N);
554 unsigned SelectExprFP(SDOperand N, unsigned Result);
555 void Select(SDOperand N);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000556
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000557 void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
558 void SelectBranchCC(SDOperand N);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000559 void MoveFP2Int(unsigned src, unsigned dst, bool isDouble);
560 void MoveInt2FP(unsigned src, unsigned dst, bool isDouble);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000561 //returns whether the sense of the comparison was inverted
562 bool SelectFPSetCC(SDOperand N, unsigned dst);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000563
564 // dag -> dag expanders for integer divide by constant
565 SDOperand BuildSDIVSequence(SDOperand N);
566 SDOperand BuildUDIVSequence(SDOperand N);
567
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000568};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000569}
570
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000571void AlphaISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000572 // If this function has live-in values, emit the copies from pregs to vregs at
573 // the top of the function, before anything else.
574 MachineBasicBlock *BB = MF.begin();
575 if (MF.livein_begin() != MF.livein_end()) {
576 SSARegMap *RegMap = MF.getSSARegMap();
577 for (MachineFunction::livein_iterator LI = MF.livein_begin(),
578 E = MF.livein_end(); LI != E; ++LI) {
579 const TargetRegisterClass *RC = RegMap->getRegClass(LI->second);
580 if (RC == Alpha::GPRCRegisterClass) {
581 BuildMI(BB, Alpha::BIS, 2, LI->second).addReg(LI->first).addReg(LI->first);
582 } else if (RC == Alpha::FPRCRegisterClass) {
583 BuildMI(BB, Alpha::CPYS, 2, LI->second).addReg(LI->first).addReg(LI->first);
584 } else {
585 assert(0 && "Unknown regclass!");
586 }
587 }
588 }
589}
590
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000591//Find the offset of the arg in it's parent's function
592static int getValueOffset(const Value* v)
593{
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000594 static int uniqneg = -1;
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000595 if (v == NULL)
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000596 return uniqneg--;
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000597
598 const Instruction* itarget = dyn_cast<Instruction>(v);
599 const BasicBlock* btarget = itarget->getParent();
600 const Function* ftarget = btarget->getParent();
601
602 //offset due to earlier BBs
603 int i = 0;
604 for(Function::const_iterator ii = ftarget->begin(); &*ii != btarget; ++ii)
605 i += ii->size();
606
607 for(BasicBlock::const_iterator ii = btarget->begin(); &*ii != itarget; ++ii)
608 ++i;
609
610 return i;
611}
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000612//Find the offset of the function in it's module
613static int getFunctionOffset(const Function* fun)
614{
615 const Module* M = fun->getParent();
616
617 //offset due to earlier BBs
618 int i = 0;
619 for(Module::const_iterator ii = M->begin(); &*ii != fun; ++ii)
620 ++i;
621
622 return i;
623}
624
625static int getUID()
626{
627 static int id = 0;
628 return ++id;
629}
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000630
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000631//Factorize a number using the list of constants
632static bool factorize(int v[], int res[], int size, uint64_t c)
633{
634 bool cont = true;
635 while (c != 1 && cont)
636 {
637 cont = false;
638 for(int i = 0; i < size; ++i)
639 {
640 if (c % v[i] == 0)
641 {
642 c /= v[i];
643 ++res[i];
644 cont=true;
645 }
646 }
647 }
648 return c == 1;
649}
650
651
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000652//Shamelessly adapted from PPC32
Misha Brukman4633f1c2005-04-21 23:13:11 +0000653// Structure used to return the necessary information to codegen an SDIV as
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000654// a multiply.
655struct ms {
656 int64_t m; // magic number
657 int64_t s; // shift amount
658};
659
660struct mu {
661 uint64_t m; // magic number
662 int64_t a; // add indicator
663 int64_t s; // shift amount
664};
665
666/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukman4633f1c2005-04-21 23:13:11 +0000667/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000668/// or -1.
669static struct ms magic(int64_t d) {
670 int64_t p;
671 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
672 const uint64_t two63 = 9223372036854775808ULL; // 2^63
673 struct ms mag;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000674
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000675 ad = abs(d);
676 t = two63 + ((uint64_t)d >> 63);
677 anc = t - 1 - t%ad; // absolute value of nc
Andrew Lenharth320174f2005-04-07 17:19:16 +0000678 p = 63; // initialize p
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000679 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
680 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
681 q2 = two63/ad; // initialize q2 = 2p/abs(d)
682 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
683 do {
684 p = p + 1;
685 q1 = 2*q1; // update q1 = 2p/abs(nc)
686 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
687 if (r1 >= anc) { // must be unsigned comparison
688 q1 = q1 + 1;
689 r1 = r1 - anc;
690 }
691 q2 = 2*q2; // update q2 = 2p/abs(d)
692 r2 = 2*r2; // update r2 = rem(2p/abs(d))
693 if (r2 >= ad) { // must be unsigned comparison
694 q2 = q2 + 1;
695 r2 = r2 - ad;
696 }
697 delta = ad - r2;
698 } while (q1 < delta || (q1 == delta && r1 == 0));
699
700 mag.m = q2 + 1;
701 if (d < 0) mag.m = -mag.m; // resulting magic number
702 mag.s = p - 64; // resulting shift
703 return mag;
704}
705
706/// magicu - calculate the magic numbers required to codegen an integer udiv as
707/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
708static struct mu magicu(uint64_t d)
709{
710 int64_t p;
711 uint64_t nc, delta, q1, r1, q2, r2;
712 struct mu magu;
713 magu.a = 0; // initialize "add" indicator
714 nc = - 1 - (-d)%d;
Andrew Lenharth320174f2005-04-07 17:19:16 +0000715 p = 63; // initialize p
716 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
717 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
718 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
719 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000720 do {
721 p = p + 1;
722 if (r1 >= nc - r1 ) {
723 q1 = 2*q1 + 1; // update q1
724 r1 = 2*r1 - nc; // update r1
725 }
726 else {
727 q1 = 2*q1; // update q1
728 r1 = 2*r1; // update r1
729 }
730 if (r2 + 1 >= d - r2) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000731 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000732 q2 = 2*q2 + 1; // update q2
733 r2 = 2*r2 + 1 - d; // update r2
734 }
735 else {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000736 if (q2 >= 0x8000000000000000ull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000737 q2 = 2*q2; // update q2
738 r2 = 2*r2 + 1; // update r2
739 }
740 delta = d - 1 - r2;
741 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
742 magu.m = q2 + 1; // resulting magic number
Andrew Lenharth320174f2005-04-07 17:19:16 +0000743 magu.s = p - 64; // resulting shift
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000744 return magu;
745}
746
747/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
748/// return a DAG expression to select that will generate the same value by
749/// multiplying by a magic number. See:
750/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000751SDOperand AlphaISel::BuildSDIVSequence(SDOperand N) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000752 int64_t d = (int64_t)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000753 ms magics = magic(d);
754 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000755 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000756 ISelDAG->getConstant(magics.m, MVT::i64));
757 // If d > 0 and m < 0, add the numerator
758 if (d > 0 && magics.m < 0)
759 Q = ISelDAG->getNode(ISD::ADD, MVT::i64, Q, N.getOperand(0));
760 // If d < 0 and m > 0, subtract the numerator.
761 if (d < 0 && magics.m > 0)
762 Q = ISelDAG->getNode(ISD::SUB, MVT::i64, Q, N.getOperand(0));
763 // Shift right algebraic if shift value is nonzero
764 if (magics.s > 0)
Misha Brukman4633f1c2005-04-21 23:13:11 +0000765 Q = ISelDAG->getNode(ISD::SRA, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000766 ISelDAG->getConstant(magics.s, MVT::i64));
767 // Extract the sign bit and add it to the quotient
Misha Brukman4633f1c2005-04-21 23:13:11 +0000768 SDOperand T =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000769 ISelDAG->getNode(ISD::SRL, MVT::i64, Q, ISelDAG->getConstant(63, MVT::i64));
770 return ISelDAG->getNode(ISD::ADD, MVT::i64, Q, T);
771}
772
773/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
774/// return a DAG expression to select that will generate the same value by
775/// multiplying by a magic number. See:
776/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000777SDOperand AlphaISel::BuildUDIVSequence(SDOperand N) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000778 unsigned d =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000779 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
780 mu magics = magicu(d);
781 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000782 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000783 ISelDAG->getConstant(magics.m, MVT::i64));
784 if (magics.a == 0) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000785 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000786 ISelDAG->getConstant(magics.s, MVT::i64));
787 } else {
788 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i64, N.getOperand(0), Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000789 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000790 ISelDAG->getConstant(1, MVT::i64));
791 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i64, NPQ, Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000792 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000793 ISelDAG->getConstant(magics.s-1, MVT::i64));
794 }
795 return Q;
796}
797
Andrew Lenhartha565c272005-04-06 22:03:13 +0000798//From PPC32
799/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
800/// returns zero when the input is not exactly a power of two.
801static unsigned ExactLog2(uint64_t Val) {
802 if (Val == 0 || (Val & (Val-1))) return 0;
803 unsigned Count = 0;
804 while (Val != 1) {
805 Val >>= 1;
806 ++Count;
807 }
808 return Count;
809}
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000810
811
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000812//These describe LDAx
Andrew Lenharthc0513832005-03-29 19:24:04 +0000813static const int IMM_LOW = -32768;
814static const int IMM_HIGH = 32767;
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000815static const int IMM_MULT = 65536;
816
817static long getUpper16(long l)
818{
819 long y = l / IMM_MULT;
820 if (l % IMM_MULT > IMM_HIGH)
821 ++y;
822 return y;
823}
824
825static long getLower16(long l)
826{
827 long h = getUpper16(l);
828 return l - h * IMM_MULT;
829}
830
Andrew Lenharth65838902005-02-06 16:22:15 +0000831static unsigned GetSymVersion(unsigned opcode)
832{
833 switch (opcode) {
834 default: assert(0 && "unknown load or store"); return 0;
835 case Alpha::LDQ: return Alpha::LDQ_SYM;
836 case Alpha::LDS: return Alpha::LDS_SYM;
837 case Alpha::LDT: return Alpha::LDT_SYM;
838 case Alpha::LDL: return Alpha::LDL_SYM;
839 case Alpha::LDBU: return Alpha::LDBU_SYM;
840 case Alpha::LDWU: return Alpha::LDWU_SYM;
841 case Alpha::LDW: return Alpha::LDW_SYM;
842 case Alpha::LDB: return Alpha::LDB_SYM;
843 case Alpha::STQ: return Alpha::STQ_SYM;
844 case Alpha::STS: return Alpha::STS_SYM;
845 case Alpha::STT: return Alpha::STT_SYM;
846 case Alpha::STL: return Alpha::STL_SYM;
847 case Alpha::STW: return Alpha::STW_SYM;
848 case Alpha::STB: return Alpha::STB_SYM;
849 }
850}
851
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000852void AlphaISel::MoveFP2Int(unsigned src, unsigned dst, bool isDouble)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000853{
854 unsigned Opc;
855 if (EnableAlphaFTOI) {
856 Opc = isDouble ? Alpha::FTOIT : Alpha::FTOIS;
857 BuildMI(BB, Opc, 1, dst).addReg(src);
858 } else {
859 //The hard way:
860 // Spill the integer to memory and reload it from there.
861 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
862 MachineFunction *F = BB->getParent();
863 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
864
865 Opc = isDouble ? Alpha::STT : Alpha::STS;
866 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
867 Opc = isDouble ? Alpha::LDQ : Alpha::LDL;
868 BuildMI(BB, Alpha::LDQ, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
869 }
870}
871
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000872void AlphaISel::MoveInt2FP(unsigned src, unsigned dst, bool isDouble)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000873{
874 unsigned Opc;
875 if (EnableAlphaFTOI) {
876 Opc = isDouble?Alpha::ITOFT:Alpha::ITOFS;
877 BuildMI(BB, Opc, 1, dst).addReg(src);
878 } else {
879 //The hard way:
880 // Spill the integer to memory and reload it from there.
881 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
882 MachineFunction *F = BB->getParent();
883 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
884
885 Opc = isDouble ? Alpha::STQ : Alpha::STL;
886 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
887 Opc = isDouble ? Alpha::LDT : Alpha::LDS;
888 BuildMI(BB, Opc, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
889 }
890}
891
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000892bool AlphaISel::SelectFPSetCC(SDOperand N, unsigned dst)
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000893{
894 SDNode *Node = N.Val;
895 unsigned Opc, Tmp1, Tmp2, Tmp3;
896 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node);
897
898 //assert(SetCC->getOperand(0).getValueType() != MVT::f32 && "SetCC f32 should have been promoted");
899 bool rev = false;
900 bool inv = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000901
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000902 switch (SetCC->getCondition()) {
903 default: Node->dump(); assert(0 && "Unknown FP comparison!");
904 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
905 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
906 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
907 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
908 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
909 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
910 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000911
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000912 //FIXME: check for constant 0.0
913 ConstantFPSDNode *CN;
914 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
915 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
916 Tmp1 = Alpha::F31;
917 else
918 Tmp1 = SelectExpr(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000919
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000920 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
921 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
922 Tmp2 = Alpha::F31;
923 else
Chris Lattner9c9183a2005-04-30 04:44:07 +0000924 Tmp2 = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000925
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000926 //Can only compare doubles, and dag won't promote for me
927 if (SetCC->getOperand(0).getValueType() == MVT::f32)
928 {
929 //assert(0 && "Setcc On float?\n");
930 std::cerr << "Setcc on float!\n";
931 Tmp3 = MakeReg(MVT::f64);
932 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
933 Tmp1 = Tmp3;
934 }
935 if (SetCC->getOperand(1).getValueType() == MVT::f32)
936 {
937 //assert (0 && "Setcc On float?\n");
938 std::cerr << "Setcc on float!\n";
939 Tmp3 = MakeReg(MVT::f64);
940 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp2);
941 Tmp2 = Tmp3;
942 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000943
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000944 if (rev) std::swap(Tmp1, Tmp2);
945 //do the comparison
946 BuildMI(BB, Opc, 2, dst).addReg(Tmp1).addReg(Tmp2);
947 return inv;
948}
949
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000950//Check to see if the load is a constant offset from a base register
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000951void AlphaISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000952{
953 unsigned opcode = N.getOpcode();
954 if (opcode == ISD::ADD) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000955 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000956 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
957 { //Normal imm add
958 Reg = SelectExpr(N.getOperand(0));
959 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
960 return;
961 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000962 else if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000963 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 32767)
964 {
965 Reg = SelectExpr(N.getOperand(1));
966 offset = cast<ConstantSDNode>(N.getOperand(0))->getValue();
967 return;
968 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000969 }
970 Reg = SelectExpr(N);
971 offset = 0;
972 return;
973}
974
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000975void AlphaISel::SelectBranchCC(SDOperand N)
Andrew Lenharth445171a2005-02-08 00:40:03 +0000976{
977 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000978 MachineBasicBlock *Dest =
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000979 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
980 unsigned Opc = Alpha::WTF;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000981
Andrew Lenharth445171a2005-02-08 00:40:03 +0000982 Select(N.getOperand(0)); //chain
983 SDOperand CC = N.getOperand(1);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000984
Andrew Lenharth445171a2005-02-08 00:40:03 +0000985 if (CC.getOpcode() == ISD::SETCC)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000986 {
987 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
988 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
989 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000990 bool LeftZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
991 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
Andrew Lenharth09552bf2005-06-08 18:02:21 +0000992 bool RightZero = SetCC->getOperand(1).getOpcode() == ISD::Constant &&
993 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000994 bool isNE = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000995
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000996 //Fix up CC
997 ISD::CondCode cCode= SetCC->getCondition();
998 if (LeftZero && !RightZero) //Swap Operands
999 cCode = ISD::getSetCCSwappedOperands(cCode);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001000
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001001 if(cCode == ISD::SETNE)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001002 isNE = true;
Andrew Lenharth445171a2005-02-08 00:40:03 +00001003
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001004 if (LeftZero || RightZero) {
Andrew Lenharth09552bf2005-06-08 18:02:21 +00001005 switch (cCode) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001006 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
1007 case ISD::SETEQ: Opc = Alpha::BEQ; break;
1008 case ISD::SETLT: Opc = Alpha::BLT; break;
1009 case ISD::SETLE: Opc = Alpha::BLE; break;
1010 case ISD::SETGT: Opc = Alpha::BGT; break;
1011 case ISD::SETGE: Opc = Alpha::BGE; break;
1012 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
1013 case ISD::SETUGT: Opc = Alpha::BNE; break;
1014 case ISD::SETULE: Opc = Alpha::BEQ; break; //Technically you could have this CC
1015 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
1016 case ISD::SETNE: Opc = Alpha::BNE; break;
1017 }
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001018 unsigned Tmp1;
1019 if(LeftZero && !RightZero) //swap Operands
1020 Tmp1 = SelectExpr(SetCC->getOperand(1)); //Cond
1021 else
1022 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001023 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
1024 return;
1025 } else {
1026 unsigned Tmp1 = SelectExpr(CC);
1027 if (isNE)
1028 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
1029 else
1030 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001031 return;
1032 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001033 } else { //FP
1034 //Any comparison between 2 values should be codegened as an folded branch, as moving
1035 //CC to the integer register is very expensive
1036 //for a cmp b: c = a - b;
1037 //a = b: c = 0
1038 //a < b: c < 0
1039 //a > b: c > 0
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001040
1041 bool invTest = false;
1042 unsigned Tmp3;
1043
1044 ConstantFPSDNode *CN;
1045 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
1046 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1047 Tmp3 = SelectExpr(SetCC->getOperand(0));
1048 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
1049 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1050 {
1051 Tmp3 = SelectExpr(SetCC->getOperand(1));
1052 invTest = true;
1053 }
1054 else
1055 {
1056 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
1057 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
1058 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
1059 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1060 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1061 .addReg(Tmp1).addReg(Tmp2);
1062 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001063
1064 switch (SetCC->getCondition()) {
1065 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001066 case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
1067 case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
1068 case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
1069 case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
1070 case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
1071 case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001072 }
1073 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001074 return;
1075 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001076 abort(); //Should never be reached
1077 } else {
1078 //Giveup and do the stupid thing
1079 unsigned Tmp1 = SelectExpr(CC);
1080 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
1081 return;
1082 }
Andrew Lenharth445171a2005-02-08 00:40:03 +00001083 abort(); //Should never be reached
1084}
1085
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001086unsigned AlphaISel::SelectExprFP(SDOperand N, unsigned Result)
Andrew Lenharth40831c52005-01-28 06:57:18 +00001087{
1088 unsigned Tmp1, Tmp2, Tmp3;
1089 unsigned Opc = 0;
1090 SDNode *Node = N.Val;
1091 MVT::ValueType DestType = N.getValueType();
1092 unsigned opcode = N.getOpcode();
1093
1094 switch (opcode) {
1095 default:
1096 Node->dump();
1097 assert(0 && "Node not handled!\n");
Andrew Lenharth2c594352005-01-29 15:42:07 +00001098
Andrew Lenharth7332f3e2005-04-02 19:11:07 +00001099 case ISD::UNDEF: {
1100 BuildMI(BB, Alpha::IDEF, 0, Result);
1101 return Result;
1102 }
1103
Andrew Lenharth30b46d42005-04-02 19:04:58 +00001104 case ISD::FNEG:
1105 if(ISD::FABS == N.getOperand(0).getOpcode())
1106 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001107 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1108 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +00001109 } else {
Misha Brukman7847fca2005-04-22 17:54:37 +00001110 Tmp1 = SelectExpr(N.getOperand(0));
1111 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +00001112 }
1113 return Result;
1114
1115 case ISD::FABS:
1116 Tmp1 = SelectExpr(N.getOperand(0));
1117 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
1118 return Result;
1119
Andrew Lenharth9818c052005-02-05 13:19:12 +00001120 case ISD::SELECT:
1121 {
Andrew Lenharth45859692005-03-03 21:47:53 +00001122 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1123 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
1124 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
1125
1126 SDOperand CC = N.getOperand(0);
1127 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
1128
Misha Brukman4633f1c2005-04-21 23:13:11 +00001129 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth45859692005-03-03 21:47:53 +00001130 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
1131 { //FP Setcc -> Select yay!
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001132
1133
Andrew Lenharth45859692005-03-03 21:47:53 +00001134 //for a cmp b: c = a - b;
1135 //a = b: c = 0
1136 //a < b: c < 0
1137 //a > b: c > 0
Misha Brukman4633f1c2005-04-21 23:13:11 +00001138
Andrew Lenharth45859692005-03-03 21:47:53 +00001139 bool invTest = false;
1140 unsigned Tmp3;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001141
Andrew Lenharth45859692005-03-03 21:47:53 +00001142 ConstantFPSDNode *CN;
1143 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
1144 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1145 Tmp3 = SelectExpr(SetCC->getOperand(0));
1146 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
1147 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1148 {
1149 Tmp3 = SelectExpr(SetCC->getOperand(1));
1150 invTest = true;
1151 }
1152 else
1153 {
1154 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
1155 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
1156 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
1157 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1158 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1159 .addReg(Tmp1).addReg(Tmp2);
1160 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001161
Andrew Lenharth45859692005-03-03 21:47:53 +00001162 switch (SetCC->getCondition()) {
1163 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
1164 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNE : Alpha::FCMOVEQ; break;
1165 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGT : Alpha::FCMOVLT; break;
1166 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGE : Alpha::FCMOVLE; break;
1167 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLT : Alpha::FCMOVGT; break;
1168 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLE : Alpha::FCMOVGE; break;
1169 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQ : Alpha::FCMOVNE; break;
1170 }
Andrew Lenharth33819132005-03-04 20:09:23 +00001171 BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
Andrew Lenharth45859692005-03-03 21:47:53 +00001172 return Result;
1173 }
1174 else
1175 {
1176 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001177 BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV).addReg(Tmp1);
1178// // Spill the cond to memory and reload it from there.
1179// unsigned Tmp4 = MakeReg(MVT::f64);
1180// MoveIntFP(Tmp1, Tmp4, true);
1181// //now ideally, we don't have to do anything to the flag...
1182// // Get the condition into the zero flag.
1183// BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
Andrew Lenharth45859692005-03-03 21:47:53 +00001184 return Result;
1185 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001186 }
1187
Andrew Lenharthc1faced2005-02-01 01:37:24 +00001188 case ISD::FP_ROUND:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001189 assert (DestType == MVT::f32 &&
1190 N.getOperand(0).getValueType() == MVT::f64 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001191 "only f64 to f32 conversion supported here");
Andrew Lenharthc1faced2005-02-01 01:37:24 +00001192 Tmp1 = SelectExpr(N.getOperand(0));
1193 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
1194 return Result;
1195
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001196 case ISD::FP_EXTEND:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001197 assert (DestType == MVT::f64 &&
1198 N.getOperand(0).getValueType() == MVT::f32 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001199 "only f32 to f64 conversion supported here");
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001200 Tmp1 = SelectExpr(N.getOperand(0));
1201 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
1202 return Result;
1203
Andrew Lenharth2c594352005-01-29 15:42:07 +00001204 case ISD::CopyFromReg:
1205 {
1206 // Make sure we generate both values.
1207 if (Result != notIn)
1208 ExprMap[N.getValue(1)] = notIn; // Generate the token
1209 else
1210 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001211
Andrew Lenharth2c594352005-01-29 15:42:07 +00001212 SDOperand Chain = N.getOperand(0);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001213
Andrew Lenharth2c594352005-01-29 15:42:07 +00001214 Select(Chain);
1215 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1216 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1217 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
1218 return Result;
1219 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001220
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001221 case ISD::LOAD:
1222 {
1223 // Make sure we generate both values.
1224 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001225 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001226 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001227 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001228
Andrew Lenharth29219162005-02-07 06:31:44 +00001229 DestType = N.getValue(0).getValueType();
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001230
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001231 SDOperand Chain = N.getOperand(0);
1232 SDOperand Address = N.getOperand(1);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001233 Select(Chain);
Andrew Lenharth65838902005-02-06 16:22:15 +00001234 Opc = DestType == MVT::f64 ? Alpha::LDT : Alpha::LDS;
1235
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +00001236 if (EnableAlphaLSMark)
1237 {
1238 int i = getValueOffset(dyn_cast<SrcValueSDNode>(N.getOperand(2))->getValue());
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001239 int j = getFunctionOffset(BB->getParent()->getFunction());
1240 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +00001241 }
1242
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001243 if (Address.getOpcode() == ISD::GlobalAddress) {
1244 AlphaLowering.restoreGP(BB);
1245 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001246 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001247 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1248 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001249 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001250 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001251 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001252 has_sym = true;
Andrew Lenharth97127a12005-02-05 17:41:39 +00001253 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001254 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001255 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001256 BuildMI(BB, Opc, 2, Result)
1257 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1258 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001259 } else {
1260 long offset;
1261 SelectAddr(Address, Tmp1, offset);
1262 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1263 }
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001264 return Result;
1265 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001266 case ISD::ConstantFP:
1267 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
1268 if (CN->isExactlyValue(+0.0)) {
1269 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001270 } else if ( CN->isExactlyValue(-0.0)) {
1271 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001272 } else {
1273 abort();
1274 }
1275 }
1276 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001277
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001278 case ISD::SDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001279 case ISD::MUL:
1280 case ISD::ADD:
1281 case ISD::SUB:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001282 switch( opcode ) {
1283 case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS; break;
1284 case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS; break;
1285 case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS; break;
1286 case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS; break;
1287 };
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001288
1289 ConstantFPSDNode *CN;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001290 if (opcode == ISD::SUB
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001291 && (CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
1292 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1293 {
1294 Tmp2 = SelectExpr(N.getOperand(1));
1295 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp2).addReg(Tmp2);
1296 } else {
1297 Tmp1 = SelectExpr(N.getOperand(0));
1298 Tmp2 = SelectExpr(N.getOperand(1));
1299 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1300 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001301 return Result;
1302
Andrew Lenharth2c594352005-01-29 15:42:07 +00001303 case ISD::EXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001304 {
1305 //include a conversion sequence for float loads to double
1306 if (Result != notIn)
1307 ExprMap[N.getValue(1)] = notIn; // Generate the token
1308 else
1309 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001310
Andrew Lenhartha549deb2005-02-07 05:33:15 +00001311 Tmp1 = MakeReg(MVT::f32);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001312
1313 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001314 "EXTLOAD not from f32");
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001315 assert(Node->getValueType(0) == MVT::f64 && "EXTLOAD not to f64");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001316
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001317 SDOperand Chain = N.getOperand(0);
1318 SDOperand Address = N.getOperand(1);
1319 Select(Chain);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001320
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001321 if (Address.getOpcode() == ISD::GlobalAddress) {
1322 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001323 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001324 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1325 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001326 else if (ConstantPoolSDNode *CP =
1327 dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001328 {
1329 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001330 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001331 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addConstantPoolIndex(CP->getIndex());
1332 }
1333 else if(Address.getOpcode() == ISD::FrameIndex) {
1334 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
Andrew Lenharth032f2352005-02-22 21:59:48 +00001335 BuildMI(BB, Alpha::LDS, 2, Tmp1)
1336 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1337 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001338 } else {
1339 long offset;
1340 SelectAddr(Address, Tmp2, offset);
1341 BuildMI(BB, Alpha::LDS, 1, Tmp1).addImm(offset).addReg(Tmp2);
1342 }
Andrew Lenharth29219162005-02-07 06:31:44 +00001343 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001344 return Result;
1345 }
Andrew Lenharth2c594352005-01-29 15:42:07 +00001346
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001347 case ISD::UINT_TO_FP:
Andrew Lenharth69520ed2005-05-26 18:18:34 +00001348 {
1349 //FIXME: First test if we will have problems with the sign bit before doing the slow thing
1350 assert (N.getOperand(0).getValueType() == MVT::i64
1351 && "only quads can be loaded from");
1352 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1353 Tmp2 = MakeReg(MVT::i64);
1354 BuildMI(BB, Alpha::SRL, 2, Tmp2).addReg(Tmp1).addImm(1);
1355 Tmp3 = MakeReg(MVT::i64);
1356 BuildMI(BB, Alpha::CMPLT, 2, Tmp3).addReg(Tmp1).addReg(Alpha::R31);
1357 unsigned Tmp4 = MakeReg(MVT::f64), Tmp5 = MakeReg(MVT::f64), Tmp6 = MakeReg(MVT::f64);
1358 MoveInt2FP(Tmp1, Tmp4, true);
1359 MoveInt2FP(Tmp2, Tmp5, true);
1360 MoveInt2FP(Tmp3, Tmp6, true);
1361 Tmp1 = MakeReg(MVT::f64);
1362 Tmp2 = MakeReg(MVT::f64);
1363 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
1364 BuildMI(BB, Opc, 1, Tmp1).addReg(Tmp4);
1365 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp5);
1366 Tmp3 = MakeReg(MVT::f64);
1367 BuildMI(BB, Alpha::ADDT, 2, Tmp3).addReg(Tmp2).addReg(Tmp2);
1368 //Ok, now tmp1 had the plain covereted
1369 //tmp3 has the reduced converted and added
1370 //tmp6 has the conditional to use
1371 BuildMI(BB, Alpha::FCMOVNE, 3, Result).addReg(Tmp1).addReg(Tmp3).addReg(Tmp6);
1372 return Result;
1373 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001374 case ISD::SINT_TO_FP:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001375 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001376 assert (N.getOperand(0).getValueType() == MVT::i64
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001377 && "only quads can be loaded from");
Andrew Lenharth40831c52005-01-28 06:57:18 +00001378 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001379 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001380 MoveInt2FP(Tmp1, Tmp2, true);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001381 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
1382 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001383 return Result;
1384 }
1385 }
1386 assert(0 && "should not get here");
1387 return 0;
1388}
1389
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001390unsigned AlphaISel::SelectExpr(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001391 unsigned Result;
Andrew Lenharth2966e842005-04-07 18:15:28 +00001392 unsigned Tmp1, Tmp2 = 0, Tmp3;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001393 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001394 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001395
1396 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001397 MVT::ValueType DestType = N.getValueType();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001398
1399 unsigned &Reg = ExprMap[N];
1400 if (Reg) return Reg;
1401
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001402 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::TAILCALL)
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001403 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001404 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001405 else {
1406 // If this is a call instruction, make sure to prepare ALL of the result
1407 // values as well as the chain.
1408 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001409 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001410 else {
1411 Result = MakeReg(Node->getValueType(0));
1412 ExprMap[N.getValue(0)] = Result;
1413 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1414 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001415 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001416 }
1417 }
1418
Andrew Lenharth50d91d72005-04-30 14:19:13 +00001419 if ((DestType == MVT::f64 || DestType == MVT::f32 ||
1420 (
1421 (opcode == ISD::LOAD || opcode == ISD::CopyFromReg ||
1422 opcode == ISD::EXTLOAD) &&
1423 (N.getValue(0).getValueType() == MVT::f32 ||
1424 N.getValue(0).getValueType() == MVT::f64)
1425 ))
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001426 && opcode != ISD::CALL && opcode != ISD::TAILCALL
Andrew Lenharth06342c32005-02-07 06:21:37 +00001427 )
Andrew Lenharth40831c52005-01-28 06:57:18 +00001428 return SelectExprFP(N, Result);
1429
1430 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001431 default:
1432 Node->dump();
1433 assert(0 && "Node not handled!\n");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001434
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001435 case ISD::CTPOP:
1436 case ISD::CTTZ:
1437 case ISD::CTLZ:
1438 Opc = opcode == ISD::CTPOP ? Alpha::CTPOP :
1439 (opcode == ISD::CTTZ ? Alpha::CTTZ : Alpha::CTLZ);
1440 Tmp1 = SelectExpr(N.getOperand(0));
1441 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1442 return Result;
1443
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001444 case ISD::MULHU:
1445 Tmp1 = SelectExpr(N.getOperand(0));
1446 Tmp2 = SelectExpr(N.getOperand(1));
1447 BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth706be912005-04-07 13:55:53 +00001448 return Result;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001449 case ISD::MULHS:
1450 {
1451 //MULHU - Ra<63>*Rb - Rb<63>*Ra
1452 Tmp1 = SelectExpr(N.getOperand(0));
1453 Tmp2 = SelectExpr(N.getOperand(1));
1454 Tmp3 = MakeReg(MVT::i64);
1455 BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1456 unsigned V1 = MakeReg(MVT::i64);
1457 unsigned V2 = MakeReg(MVT::i64);
1458 BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31).addReg(Tmp1);
1459 BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31).addReg(Tmp2);
1460 unsigned IRes = MakeReg(MVT::i64);
1461 BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
1462 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
1463 return Result;
1464 }
Andrew Lenharth7332f3e2005-04-02 19:11:07 +00001465 case ISD::UNDEF: {
1466 BuildMI(BB, Alpha::IDEF, 0, Result);
1467 return Result;
1468 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001469
Andrew Lenharth032f2352005-02-22 21:59:48 +00001470 case ISD::DYNAMIC_STACKALLOC:
1471 // Generate both result values.
Andrew Lenharth3a7118d2005-02-23 17:33:42 +00001472 if (Result != notIn)
1473 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth032f2352005-02-22 21:59:48 +00001474 else
1475 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1476
1477 // FIXME: We are currently ignoring the requested alignment for handling
1478 // greater than the stack alignment. This will need to be revisited at some
1479 // point. Align = N.getOperand(2);
1480
1481 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1482 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1483 std::cerr << "Cannot allocate stack object with greater alignment than"
1484 << " the stack alignment yet!";
1485 abort();
1486 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001487
Andrew Lenharth032f2352005-02-22 21:59:48 +00001488 Select(N.getOperand(0));
1489 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1490 {
1491 if (CN->getValue() < 32000)
1492 {
1493 BuildMI(BB, Alpha::LDA, 2, Alpha::R30)
1494 .addImm(-CN->getValue()).addReg(Alpha::R30);
1495 } else {
1496 Tmp1 = SelectExpr(N.getOperand(1));
1497 // Subtract size from stack pointer, thereby allocating some space.
1498 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1499 }
1500 } else {
1501 Tmp1 = SelectExpr(N.getOperand(1));
1502 // Subtract size from stack pointer, thereby allocating some space.
1503 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1504 }
1505
1506 // Put a pointer to the space into the result register, by copying the stack
1507 // pointer.
Andrew Lenharth7bc47022005-02-22 23:29:25 +00001508 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
Andrew Lenharth032f2352005-02-22 21:59:48 +00001509 return Result;
1510
Andrew Lenharth33819132005-03-04 20:09:23 +00001511// case ISD::ConstantPool:
1512// Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1513// AlphaLowering.restoreGP(BB);
1514// BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(Tmp1);
1515// return Result;
Andrew Lenharth2c594352005-01-29 15:42:07 +00001516
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001517 case ISD::FrameIndex:
Andrew Lenharth032f2352005-02-22 21:59:48 +00001518 BuildMI(BB, Alpha::LDA, 2, Result)
1519 .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
1520 .addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001521 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001522
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001523 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001524 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001525 case ISD::SEXTLOAD:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001526 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001527 {
1528 // Make sure we generate both values.
1529 if (Result != notIn)
1530 ExprMap[N.getValue(1)] = notIn; // Generate the token
1531 else
1532 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001533
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001534 SDOperand Chain = N.getOperand(0);
1535 SDOperand Address = N.getOperand(1);
1536 Select(Chain);
1537
Misha Brukman4633f1c2005-04-21 23:13:11 +00001538 assert(Node->getValueType(0) == MVT::i64 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001539 "Unknown type to sign extend to.");
Andrew Lenharth03824012005-02-07 05:55:55 +00001540 if (opcode == ISD::LOAD)
1541 Opc = Alpha::LDQ;
1542 else
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001543 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
1544 default: Node->dump(); assert(0 && "Bad sign extend!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001545 case MVT::i32: Opc = Alpha::LDL;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001546 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001547 case MVT::i16: Opc = Alpha::LDWU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001548 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001549 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Misha Brukman4633f1c2005-04-21 23:13:11 +00001550 case MVT::i8: Opc = Alpha::LDBU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001551 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001552 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001553
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001554 if (EnableAlphaLSMark)
1555 {
1556 int i = getValueOffset(dyn_cast<SrcValueSDNode>(N.getOperand(2))->getValue());
1557 int j = getFunctionOffset(BB->getParent()->getFunction());
1558 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
1559 }
1560
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001561 if (Address.getOpcode() == ISD::GlobalAddress) {
1562 AlphaLowering.restoreGP(BB);
1563 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001564 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001565 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1566 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001567 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1568 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001569 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001570 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001571 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001572 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001573 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001574 BuildMI(BB, Opc, 2, Result)
1575 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1576 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001577 } else {
1578 long offset;
1579 SelectAddr(Address, Tmp1, offset);
1580 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1581 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001582 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001583 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001584
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001585 case ISD::GlobalAddress:
1586 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001587 has_sym = true;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001588 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
1589 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
1590 return Result;
1591
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001592 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001593 case ISD::CALL:
1594 {
1595 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001596
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001597 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001598 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001599
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001600 //grab the arguments
1601 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001602 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001603 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001604 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001605
Andrew Lenharth684f2292005-01-30 00:35:27 +00001606 //in reg args
1607 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001608 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001609 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001610 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +00001611 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001612 Alpha::F19, Alpha::F20, Alpha::F21};
1613 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001614 default:
1615 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001616 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001617 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001618 N.getOperand(i+2).getValueType() << "\n";
1619 assert(0 && "Unknown value type for call");
1620 case MVT::i1:
1621 case MVT::i8:
1622 case MVT::i16:
1623 case MVT::i32:
1624 case MVT::i64:
1625 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1626 break;
1627 case MVT::f32:
1628 case MVT::f64:
1629 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1630 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001631 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001632 }
Andrew Lenharth684f2292005-01-30 00:35:27 +00001633 //in mem args
1634 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001635 {
1636 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001637 default:
1638 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001639 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001640 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001641 N.getOperand(i+2).getValueType() << "\n";
1642 assert(0 && "Unknown value type for call");
1643 case MVT::i1:
1644 case MVT::i8:
1645 case MVT::i16:
1646 case MVT::i32:
1647 case MVT::i64:
1648 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1649 break;
1650 case MVT::f32:
1651 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1652 break;
1653 case MVT::f64:
1654 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1655 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001656 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001657 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001658 //build the right kind of call
1659 if (GlobalAddressSDNode *GASD =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001660 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001661 {
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001662 if (GASD->getGlobal()->isExternal()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001663 //use safe calling convention
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001664 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001665 has_sym = true;
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001666 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal());
1667 } else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001668 //use PC relative branch call
Andrew Lenharth1e0d9bd2005-04-14 17:34:20 +00001669 AlphaLowering.restoreGP(BB);
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001670 BuildMI(BB, Alpha::BSR, 1, Alpha::R26).addGlobalAddress(GASD->getGlobal(),true);
1671 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001672 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001673 else if (ExternalSymbolSDNode *ESSDN =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001674 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001675 {
1676 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001677 has_sym = true;
Andrew Lenharthba05ad62005-03-30 18:22:52 +00001678 BuildMI(BB, Alpha::CALL, 1).addExternalSymbol(ESSDN->getSymbol(), true);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001679 } else {
1680 //no need to restore GP as we are doing an indirect call
1681 Tmp1 = SelectExpr(N.getOperand(1));
1682 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
1683 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
1684 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001685
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001686 //push the result into a virtual register
Misha Brukman4633f1c2005-04-21 23:13:11 +00001687
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001688 switch (Node->getValueType(0)) {
1689 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001690 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001691 case MVT::i1:
1692 case MVT::i8:
1693 case MVT::i16:
1694 case MVT::i32:
1695 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001696 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
1697 break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001698 case MVT::f32:
1699 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001700 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
1701 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001702 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001703 return Result+N.ResNo;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001704 }
1705
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001706 case ISD::SIGN_EXTEND_INREG:
1707 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001708 //do SDIV opt for all levels of ints if not dividing by a constant
1709 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
1710 && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001711 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001712 unsigned Tmp4 = MakeReg(MVT::f64);
1713 unsigned Tmp5 = MakeReg(MVT::f64);
1714 unsigned Tmp6 = MakeReg(MVT::f64);
1715 unsigned Tmp7 = MakeReg(MVT::f64);
1716 unsigned Tmp8 = MakeReg(MVT::f64);
1717 unsigned Tmp9 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001718
1719 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1720 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1721 MoveInt2FP(Tmp1, Tmp4, true);
1722 MoveInt2FP(Tmp2, Tmp5, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001723 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Tmp4);
1724 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Tmp5);
1725 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
1726 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Tmp8);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001727 MoveFP2Int(Tmp9, Result, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001728 return Result;
1729 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001730
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001731 //Alpha has instructions for a bunch of signed 32 bit stuff
1732 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001733 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001734 switch (N.getOperand(0).getOpcode()) {
1735 case ISD::ADD:
1736 case ISD::SUB:
1737 case ISD::MUL:
1738 {
1739 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
1740 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
1741 //FIXME: first check for Scaled Adds and Subs!
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001742 ConstantSDNode* CSD = NULL;
1743 if(!isMul && N.getOperand(0).getOperand(0).getOpcode() == ISD::SHL &&
1744 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(0).getOperand(1))) &&
1745 (CSD->getValue() == 2 || CSD->getValue() == 3))
1746 {
1747 bool use4 = CSD->getValue() == 2;
1748 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1749 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1750 BuildMI(BB, isAdd?(use4?Alpha::S4ADDL:Alpha::S8ADDL):(use4?Alpha::S4SUBL:Alpha::S8SUBL),
1751 2,Result).addReg(Tmp1).addReg(Tmp2);
1752 }
1753 else if(isAdd && N.getOperand(0).getOperand(1).getOpcode() == ISD::SHL &&
1754 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1).getOperand(1))) &&
1755 (CSD->getValue() == 2 || CSD->getValue() == 3))
1756 {
1757 bool use4 = CSD->getValue() == 2;
1758 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1759 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1760 BuildMI(BB, use4?Alpha::S4ADDL:Alpha::S8ADDL, 2,Result).addReg(Tmp1).addReg(Tmp2);
1761 }
1762 else if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001763 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
1764 { //Normal imm add/sub
1765 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001766 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001767 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
1768 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001769 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001770 else
1771 { //Normal add/sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001772 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULL : Alpha::SUBL);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001773 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001774 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001775 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1776 }
1777 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001778 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001779 default: break; //Fall Though;
1780 }
1781 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001782 Tmp1 = SelectExpr(N.getOperand(0));
1783 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001784 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001785 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001786 {
1787 default:
1788 Node->dump();
1789 assert(0 && "Sign Extend InReg not there yet");
1790 break;
1791 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001792 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001793 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001794 break;
1795 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001796 case MVT::i16:
1797 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
1798 break;
1799 case MVT::i8:
1800 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
1801 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +00001802 case MVT::i1:
1803 Tmp2 = MakeReg(MVT::i64);
1804 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001805 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +00001806 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001807 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001808 return Result;
1809 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001810
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001811 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001812 {
1813 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1814 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1815 bool isConst1 = false;
1816 bool isConst2 = false;
1817 int dir;
Misha Brukman7847fca2005-04-22 17:54:37 +00001818
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001819 //Tmp1 = SelectExpr(N.getOperand(0));
1820 if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001821 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
1822 isConst1 = true;
1823 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001824 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1825 isConst2 = true;
1826
1827 switch (SetCC->getCondition()) {
1828 default: Node->dump(); assert(0 && "Unknown integer comparison!");
1829 case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001830 case ISD::SETLT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001831 Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001832 case ISD::SETLE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001833 Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001834 case ISD::SETGT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001835 Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001836 case ISD::SETGE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001837 Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001838 case ISD::SETULT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001839 Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001840 case ISD::SETUGT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001841 Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001842 case ISD::SETULE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001843 Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001844 case ISD::SETUGE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001845 Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001846 case ISD::SETNE: {//Handle this one special
1847 //std::cerr << "Alpha does not have a setne.\n";
1848 //abort();
1849 Tmp1 = SelectExpr(N.getOperand(0));
1850 Tmp2 = SelectExpr(N.getOperand(1));
1851 Tmp3 = MakeReg(MVT::i64);
1852 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001853 //Remeber we have the Inv for this CC
1854 CCInvMap[N] = Tmp3;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001855 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001856 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001857 return Result;
1858 }
1859 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001860 if (dir == 1) {
1861 Tmp1 = SelectExpr(N.getOperand(0));
1862 if (isConst2) {
1863 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1864 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1865 } else {
1866 Tmp2 = SelectExpr(N.getOperand(1));
1867 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1868 }
1869 } else if (dir == 2) {
1870 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001871 if (isConst1) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001872 Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1873 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1874 } else {
1875 Tmp2 = SelectExpr(N.getOperand(0));
1876 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1877 }
1878 } else { //dir == 0
1879 if (isConst1) {
1880 Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1881 Tmp2 = SelectExpr(N.getOperand(1));
1882 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
1883 } else if (isConst2) {
1884 Tmp1 = SelectExpr(N.getOperand(0));
1885 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1886 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
1887 } else {
1888 Tmp1 = SelectExpr(N.getOperand(0));
1889 Tmp2 = SelectExpr(N.getOperand(1));
1890 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1891 }
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001892 }
1893 } else {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001894 //do the comparison
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001895 Tmp1 = MakeReg(MVT::f64);
1896 bool inv = SelectFPSetCC(N, Tmp1);
1897
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001898 //now arrange for Result (int) to have a 1 or 0
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001899 Tmp2 = MakeReg(MVT::i64);
1900 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001901 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001902 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001903 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001904 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001905 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001906 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001907
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001908 case ISD::CopyFromReg:
1909 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001910 ++count_ins;
1911
Andrew Lenharth40831c52005-01-28 06:57:18 +00001912 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001913 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001914 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001915 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001916 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001917
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001918 SDOperand Chain = N.getOperand(0);
1919
1920 Select(Chain);
1921 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1922 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1923 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1924 return Result;
1925 }
1926
Misha Brukman4633f1c2005-04-21 23:13:11 +00001927 //Most of the plain arithmetic and logic share the same form, and the same
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001928 //constant immediate test
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001929 case ISD::XOR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001930 //Match Not
1931 if (N.getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001932 cast<ConstantSDNode>(N.getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001933 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001934 Tmp1 = SelectExpr(N.getOperand(0));
1935 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1936 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001937 }
1938 //Fall through
1939 case ISD::AND:
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001940 //handle zap
1941 if (opcode == ISD::AND && N.getOperand(1).getOpcode() == ISD::Constant)
1942 {
1943 uint64_t k = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1944 unsigned int build = 0;
1945 for(int i = 0; i < 8; ++i)
1946 {
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001947 if ((k & 0x00FF) == 0x00FF)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001948 build |= 1 << i;
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001949 else if ((k & 0x00FF) != 0)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001950 { build = 0; break; }
1951 k >>= 8;
1952 }
1953 if (build)
1954 {
1955 Tmp1 = SelectExpr(N.getOperand(0));
1956 BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1957 return Result;
1958 }
1959 }
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001960 case ISD::OR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001961 //Check operand(0) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001962 if (N.getOperand(0).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001963 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001964 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001965 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001966 switch(opcode) {
1967 case ISD::AND: Opc = Alpha::BIC; break;
1968 case ISD::OR: Opc = Alpha::ORNOT; break;
1969 case ISD::XOR: Opc = Alpha::EQV; break;
1970 }
1971 Tmp1 = SelectExpr(N.getOperand(1));
1972 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1973 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1974 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001975 }
1976 //Check operand(1) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001977 if (N.getOperand(1).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001978 N.getOperand(1).getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001979 cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001980 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001981 switch(opcode) {
1982 case ISD::AND: Opc = Alpha::BIC; break;
1983 case ISD::OR: Opc = Alpha::ORNOT; break;
1984 case ISD::XOR: Opc = Alpha::EQV; break;
1985 }
1986 Tmp1 = SelectExpr(N.getOperand(0));
1987 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1988 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1989 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001990 }
1991 //Fall through
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001992 case ISD::SHL:
1993 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001994 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001995 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001996 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1997 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001998 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001999 {
2000 switch(opcode) {
2001 case ISD::AND: Opc = Alpha::ANDi; break;
2002 case ISD::OR: Opc = Alpha::BISi; break;
2003 case ISD::XOR: Opc = Alpha::XORi; break;
2004 case ISD::SHL: Opc = Alpha::SLi; break;
2005 case ISD::SRL: Opc = Alpha::SRLi; break;
2006 case ISD::SRA: Opc = Alpha::SRAi; break;
2007 case ISD::MUL: Opc = Alpha::MULQi; break;
2008 };
2009 Tmp1 = SelectExpr(N.getOperand(0));
2010 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
2011 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
2012 } else {
2013 switch(opcode) {
2014 case ISD::AND: Opc = Alpha::AND; break;
2015 case ISD::OR: Opc = Alpha::BIS; break;
2016 case ISD::XOR: Opc = Alpha::XOR; break;
2017 case ISD::SHL: Opc = Alpha::SL; break;
2018 case ISD::SRL: Opc = Alpha::SRL; break;
2019 case ISD::SRA: Opc = Alpha::SRA; break;
2020 case ISD::MUL: Opc = Alpha::MULQ; break;
2021 };
2022 Tmp1 = SelectExpr(N.getOperand(0));
2023 Tmp2 = SelectExpr(N.getOperand(1));
2024 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2025 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00002026 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002027
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002028 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002029 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00002030 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00002031 bool isAdd = opcode == ISD::ADD;
2032
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002033 //first check for Scaled Adds and Subs!
2034 //Valid for add and sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002035 ConstantSDNode* CSD = NULL;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002036 if(N.getOperand(0).getOpcode() == ISD::SHL &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002037 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
2038 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002039 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002040 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002041 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002042 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) && CSD->getValue() <= 255)
2043 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
2044 2, Result).addReg(Tmp2).addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00002045 else {
2046 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002047 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
2048 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00002049 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002050 }
2051 //Position prevents subs
Andrew Lenharth273a1f92005-04-07 14:18:13 +00002052 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002053 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) &&
2054 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002055 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002056 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002057 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002058 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(0))) && CSD->getValue() <= 255)
2059 BuildMI(BB, use4?Alpha::S4ADDQi:Alpha::S8ADDQi, 2, Result).addReg(Tmp2)
2060 .addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00002061 else {
2062 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002063 BuildMI(BB, use4?Alpha::S4ADDQ:Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00002064 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002065 }
2066 //small addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002067 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
2068 CSD->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002069 { //Normal imm add/sub
2070 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
2071 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002072 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CSD->getValue());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002073 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002074 //larger addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002075 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
2076 CSD->getSignExtended() <= 32767 &&
2077 CSD->getSignExtended() >= -32767)
Andrew Lenharth74d00d82005-03-02 17:23:03 +00002078 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002079 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002080 Tmp2 = (long)CSD->getSignExtended();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002081 if (!isAdd)
2082 Tmp2 = -Tmp2;
2083 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002084 }
2085 //give up and do the operation
2086 else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002087 //Normal add/sub
2088 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
2089 Tmp1 = SelectExpr(N.getOperand(0));
2090 Tmp2 = SelectExpr(N.getOperand(1));
2091 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2092 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002093 return Result;
2094 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002095
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00002096 case ISD::SDIV:
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002097 {
Andrew Lenhartha565c272005-04-06 22:03:13 +00002098 ConstantSDNode* CSD;
2099 //check if we can convert into a shift!
2100 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
2101 (int64_t)CSD->getSignExtended() != 0 &&
2102 ExactLog2(abs((int64_t)CSD->getSignExtended())) != 0)
2103 {
2104 unsigned k = ExactLog2(abs(CSD->getSignExtended()));
2105 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenhartha565c272005-04-06 22:03:13 +00002106 if (k == 1)
2107 Tmp2 = Tmp1;
2108 else
2109 {
2110 Tmp2 = MakeReg(MVT::i64);
2111 BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
2112 }
2113 Tmp3 = MakeReg(MVT::i64);
2114 BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
2115 unsigned Tmp4 = MakeReg(MVT::i64);
2116 BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
2117 if ((int64_t)CSD->getSignExtended() > 0)
2118 BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
2119 else
2120 {
2121 unsigned Tmp5 = MakeReg(MVT::i64);
2122 BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
2123 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
2124 }
2125 return Result;
2126 }
2127 }
2128 //Else fall through
2129
2130 case ISD::UDIV:
2131 {
2132 ConstantSDNode* CSD;
2133 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
2134 ((int64_t)CSD->getSignExtended() >= 2 ||
2135 (int64_t)CSD->getSignExtended() <= -2))
2136 {
2137 // If this is a divide by constant, we can emit code using some magic
2138 // constants to implement it as a multiply instead.
2139 ExprMap.erase(N);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002140 if (opcode == ISD::SDIV)
Andrew Lenhartha565c272005-04-06 22:03:13 +00002141 return SelectExpr(BuildSDIVSequence(N));
2142 else
2143 return SelectExpr(BuildUDIVSequence(N));
2144 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002145 }
2146 //else fall though
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002147 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +00002148 case ISD::SREM:
Misha Brukman4633f1c2005-04-21 23:13:11 +00002149 //FIXME: alpha really doesn't support any of these operations,
Andrew Lenharth40831c52005-01-28 06:57:18 +00002150 // the ops are expanded into special library calls with
2151 // special calling conventions
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002152 //Restore GP because it is a call after all...
Andrew Lenharth40831c52005-01-28 06:57:18 +00002153 switch(opcode) {
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00002154 case ISD::UREM: Opc = Alpha::REMQU; break;
2155 case ISD::SREM: Opc = Alpha::REMQ; break;
2156 case ISD::UDIV: Opc = Alpha::DIVQU; break;
2157 case ISD::SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002158 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002159 Tmp1 = SelectExpr(N.getOperand(0));
2160 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth33819132005-03-04 20:09:23 +00002161 //set up regs explicitly (helps Reg alloc)
2162 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002163 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00002164 AlphaLowering.restoreGP(BB);
Andrew Lenharth33819132005-03-04 20:09:23 +00002165 BuildMI(BB, Opc, 2).addReg(Alpha::R24).addReg(Alpha::R25);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002166 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002167 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002168
Andrew Lenharthe76797c2005-02-01 20:40:27 +00002169 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002170 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002171 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002172 assert (DestType == MVT::i64 && "only quads can be loaded to");
2173 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00002174 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002175 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002176 if (SrcType == MVT::f32)
Misha Brukman7847fca2005-04-22 17:54:37 +00002177 {
2178 Tmp2 = MakeReg(MVT::f64);
2179 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
2180 Tmp1 = Tmp2;
2181 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002182 Tmp2 = MakeReg(MVT::f64);
2183 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00002184 MoveFP2Int(Tmp2, Result, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002185
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002186 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002187 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002188
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002189 case ISD::SELECT:
2190 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00002191 //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 +00002192 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002193 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2194 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002195 // Get the condition into the zero flag.
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002196 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002197
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002198 SDOperand CC = N.getOperand(0);
2199 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
2200
Misha Brukman4633f1c2005-04-21 23:13:11 +00002201 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002202 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
2203 { //FP Setcc -> Int Select
Misha Brukman7847fca2005-04-22 17:54:37 +00002204 Tmp1 = MakeReg(MVT::f64);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002205 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2206 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Misha Brukman7847fca2005-04-22 17:54:37 +00002207 bool inv = SelectFPSetCC(CC, Tmp1);
2208 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
2209 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
2210 return Result;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002211 }
2212 if (CC.getOpcode() == ISD::SETCC) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002213 //Int SetCC -> Select
2214 //Dropping the CC is only useful if we are comparing to 0
2215 if((SetCC->getOperand(1).getOpcode() == ISD::Constant &&
2216 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0) ||
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002217 (SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2218 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0))
2219 {
2220 //figure out a few things
2221 bool LeftZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2222 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
2223 bool RightZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2224 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
2225 bool LeftConst = N.getOperand(1).getOpcode() == ISD::Constant &&
2226 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255;
2227 bool RightConst = N.getOperand(2).getOpcode() == ISD::Constant &&
2228 cast<ConstantSDNode>(N.getOperand(2))->getValue() <= 255;
2229 bool useImm = LeftConst || RightConst;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002230
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002231 //Fix up CC
2232 ISD::CondCode cCode= SetCC->getCondition();
2233 if (RightConst && !LeftConst) //Invert sense to get Imm field right
2234 cCode = ISD::getSetCCInverse(cCode, true);
2235 if (LeftZero && !RightZero) //Swap Operands
2236 cCode = ISD::getSetCCSwappedOperands(cCode);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002237
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002238 //Choose the CMOV
2239 switch (cCode) {
2240 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
2241 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
2242 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
2243 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
2244 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
2245 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
2246 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
2247 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2248 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break; //Technically you could have this CC
2249 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
2250 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2251 }
2252 if(LeftZero && !RightZero) //swap Operands
2253 Tmp1 = SelectExpr(SetCC->getOperand(1)); //Cond
2254 else
2255 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
2256
2257 if (LeftConst) {
2258 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2259 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
Misha Brukman7847fca2005-04-22 17:54:37 +00002260 .addImm(cast<ConstantSDNode>(N.getOperand(1))->getValue())
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002261 .addReg(Tmp1);
2262 } else if (RightConst) {
2263 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
2264 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
Misha Brukman7847fca2005-04-22 17:54:37 +00002265 .addImm(cast<ConstantSDNode>(N.getOperand(2))->getValue())
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002266 .addReg(Tmp1);
2267 } else {
2268 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2269 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2270 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
2271 }
2272 return Result;
2273 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002274 //Otherwise, fall though
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002275 }
2276 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002277 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2278 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthe76797c2005-02-01 20:40:27 +00002279 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002280
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002281 return Result;
2282 }
2283
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002284 case ISD::Constant:
2285 {
Andrew Lenharthc0513832005-03-29 19:24:04 +00002286 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002287 if (val <= IMM_HIGH && val >= IMM_LOW) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002288 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002289 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002290 else if (val <= (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT &&
2291 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
2292 Tmp1 = MakeReg(MVT::i64);
2293 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val)).addReg(Alpha::R31);
2294 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002295 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002296 else {
2297 MachineConstantPool *CP = BB->getParent()->getConstantPool();
2298 ConstantUInt *C = ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
2299 unsigned CPI = CP->getConstantPoolIndex(C);
2300 AlphaLowering.restoreGP(BB);
2301 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CPI);
2302 }
2303 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002304 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002305 }
2306
2307 return 0;
2308}
2309
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002310void AlphaISel::Select(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002311 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00002312 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002313
Nate Begeman85fdeb22005-03-24 04:39:54 +00002314 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002315 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002316
2317 SDNode *Node = N.Val;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002318
Andrew Lenharth760270d2005-02-07 23:02:23 +00002319 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002320
2321 default:
2322 Node->dump(); std::cerr << "\n";
2323 assert(0 && "Node not handled yet!");
2324
2325 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00002326 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002327 return;
2328 }
2329
2330 case ISD::BR: {
2331 MachineBasicBlock *Dest =
2332 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2333
2334 Select(N.getOperand(0));
2335 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
2336 return;
2337 }
2338
2339 case ISD::ImplicitDef:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002340 ++count_ins;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002341 Select(N.getOperand(0));
2342 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
2343 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002344
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002345 case ISD::EntryToken: return; // Noop
2346
2347 case ISD::TokenFactor:
2348 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2349 Select(Node->getOperand(i));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002350
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002351 //N.Val->dump(); std::cerr << "\n";
2352 //assert(0 && "Node not handled yet!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00002353
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002354 return;
2355
2356 case ISD::CopyToReg:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002357 ++count_outs;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002358 Select(N.getOperand(0));
2359 Tmp1 = SelectExpr(N.getOperand(1));
2360 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002361
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002362 if (Tmp1 != Tmp2) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002363 if (N.getOperand(1).getValueType() == MVT::f64 ||
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002364 N.getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00002365 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2366 else
2367 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002368 }
2369 return;
2370
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002371 case ISD::RET:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002372 ++count_outs;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002373 switch (N.getNumOperands()) {
2374 default:
2375 std::cerr << N.getNumOperands() << "\n";
2376 for (unsigned i = 0; i < N.getNumOperands(); ++i)
2377 std::cerr << N.getOperand(i).getValueType() << "\n";
2378 Node->dump();
2379 assert(0 && "Unknown return instruction!");
2380 case 2:
2381 Select(N.getOperand(0));
2382 Tmp1 = SelectExpr(N.getOperand(1));
2383 switch (N.getOperand(1).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002384 default: Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002385 assert(0 && "All other types should have been promoted!!");
2386 case MVT::f64:
2387 case MVT::f32:
2388 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
2389 break;
2390 case MVT::i32:
2391 case MVT::i64:
2392 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
2393 break;
2394 }
2395 break;
2396 case 1:
2397 Select(N.getOperand(0));
2398 break;
2399 }
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +00002400 AlphaLowering.restoreRA(BB);
2401 BuildMI(BB, Alpha::RET, 1, Alpha::R31).addReg(Alpha::R26); // Just emit a 'ret' instruction
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002402 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002403
Misha Brukman4633f1c2005-04-21 23:13:11 +00002404 case ISD::TRUNCSTORE:
2405 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002406 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002407 SDOperand Chain = N.getOperand(0);
2408 SDOperand Value = N.getOperand(1);
2409 SDOperand Address = N.getOperand(2);
2410 Select(Chain);
2411
2412 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00002413
2414 if (opcode == ISD::STORE) {
2415 switch(Value.getValueType()) {
2416 default: assert(0 && "unknown Type in store");
2417 case MVT::i64: Opc = Alpha::STQ; break;
2418 case MVT::f64: Opc = Alpha::STT; break;
2419 case MVT::f32: Opc = Alpha::STS; break;
2420 }
2421 } else { //ISD::TRUNCSTORE
2422 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2423 default: assert(0 && "unknown Type in store");
2424 case MVT::i1: //FIXME: DAG does not promote this load
2425 case MVT::i8: Opc = Alpha::STB; break;
2426 case MVT::i16: Opc = Alpha::STW; break;
2427 case MVT::i32: Opc = Alpha::STL; break;
2428 }
Andrew Lenharth65838902005-02-06 16:22:15 +00002429 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00002430
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002431 if (EnableAlphaLSMark)
2432 {
2433 int i = getValueOffset(dyn_cast<SrcValueSDNode>(N.getOperand(3))->getValue());
2434 int j = getFunctionOffset(BB->getParent()->getFunction());
2435 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
2436 }
2437
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002438 if (Address.getOpcode() == ISD::GlobalAddress)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002439 {
2440 AlphaLowering.restoreGP(BB);
2441 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002442 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002443 BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
2444 }
Andrew Lenharth05380342005-02-07 05:07:00 +00002445 else if(Address.getOpcode() == ISD::FrameIndex)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002446 {
Andrew Lenharth032f2352005-02-22 21:59:48 +00002447 BuildMI(BB, Opc, 3).addReg(Tmp1)
2448 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
2449 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002450 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002451 else
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002452 {
2453 long offset;
2454 SelectAddr(Address, Tmp2, offset);
2455 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2456 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002457 return;
2458 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002459
2460 case ISD::EXTLOAD:
2461 case ISD::SEXTLOAD:
2462 case ISD::ZEXTLOAD:
2463 case ISD::LOAD:
2464 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002465 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002466 case ISD::CALL:
Andrew Lenharth032f2352005-02-22 21:59:48 +00002467 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002468 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002469 SelectExpr(N);
2470 return;
2471
Chris Lattner16cd04d2005-05-12 23:24:06 +00002472 case ISD::CALLSEQ_START:
2473 case ISD::CALLSEQ_END:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002474 Select(N.getOperand(0));
2475 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002476
Chris Lattner16cd04d2005-05-12 23:24:06 +00002477 Opc = N.getOpcode() == ISD::CALLSEQ_START ? Alpha::ADJUSTSTACKDOWN :
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002478 Alpha::ADJUSTSTACKUP;
2479 BuildMI(BB, Opc, 1).addImm(Tmp1);
2480 return;
Andrew Lenharth95762122005-03-31 21:24:06 +00002481
2482 case ISD::PCMARKER:
2483 Select(N.getOperand(0)); //Chain
2484 BuildMI(BB, Alpha::PCLABEL, 2).addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
2485 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002486 }
2487 assert(0 && "Should not be reached!");
2488}
2489
2490
2491/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
2492/// into a machine code representation using pattern matching and a machine
2493/// description file.
2494///
2495FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002496 return new AlphaISel(TM);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002497}
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002498