blob: e559852d2b9c71a8179e07d1d4a6f361507e23f1 [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",
Andrew Lenharthd4653b12005-06-27 17:39:17 +000038 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",
Andrew Lenharthd4653b12005-06-27 17:39:17 +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",
Andrew Lenharthd4653b12005-06-27 17:39:17 +000044 cl::desc("Enable use of the ctpop, ctlz, and cttz instructions"),
Andrew Lenharth59009192005-05-04 19:12:09 +000045 cl::Hidden);
Misha Brukman4633f1c2005-04-21 23:13:11 +000046 cl::opt<bool> EnableAlphaCount("enable-alpha-count",
Andrew Lenharthd4653b12005-06-27 17:39:17 +000047 cl::desc("Print estimates on live ins and outs"),
48 cl::Hidden);
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +000049 cl::opt<bool> EnableAlphaLSMark("enable-alpha-lsmark",
Andrew Lenharthd4653b12005-06-27 17:39:17 +000050 cl::desc("Emit symbols to correlate Mem ops to LLVM Values"),
51 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
Jeff Cohen00b168892005-07-27 06:12:32 +000067 FTOI,
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +000068 };
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);
Nate Begeman7cbd5252005-08-16 19:49:35 +000093 setOperationAction(ISD::BRTWOWAY_CC, MVT::Other, Expand);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000094
Andrew Lenharthec151362005-06-26 22:23:06 +000095 setOperationAction(ISD::EXTLOAD, MVT::i1, Promote);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +000096 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
Jeff Cohen00b168892005-07-27 06:12:32 +000097
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +000098 setOperationAction(ISD::ZEXTLOAD, MVT::i1, Promote);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +000099 setOperationAction(ISD::ZEXTLOAD, MVT::i32, Expand);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000100
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +0000101 setOperationAction(ISD::SEXTLOAD, MVT::i1, Promote);
Andrew Lenharthec151362005-06-26 22:23:06 +0000102 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
103 setOperationAction(ISD::SEXTLOAD, MVT::i16, Expand);
104
105 setOperationAction(ISD::SREM, MVT::f32, Expand);
106 setOperationAction(ISD::SREM, MVT::f64, Expand);
107
108 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000109
Andrew Lenharth59009192005-05-04 19:12:09 +0000110 if (!EnableAlphaCT) {
111 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
112 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
Andrew Lenharthb5884d32005-05-04 19:25:37 +0000113 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
Andrew Lenharth59009192005-05-04 19:12:09 +0000114 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000115
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000116 //If this didn't legalize into a div....
117 // setOperationAction(ISD::SREM , MVT::i64, Expand);
118 // setOperationAction(ISD::UREM , MVT::i64, Expand);
119
120 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
121 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
122 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
Andrew Lenharth9818c052005-02-05 13:19:12 +0000123
Chris Lattner17234b72005-04-30 04:26:06 +0000124 // We don't support sin/cos/sqrt
125 setOperationAction(ISD::FSIN , MVT::f64, Expand);
126 setOperationAction(ISD::FCOS , MVT::f64, Expand);
127 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
128 setOperationAction(ISD::FSIN , MVT::f32, Expand);
129 setOperationAction(ISD::FCOS , MVT::f32, Expand);
130 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
131
Andrew Lenharth33819132005-03-04 20:09:23 +0000132 //Doesn't work yet
Chris Lattner17234b72005-04-30 04:26:06 +0000133 setOperationAction(ISD::SETCC, MVT::f32, Promote);
Andrew Lenharth572af902005-02-14 05:41:43 +0000134
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000135 //Try a couple things with a custom expander
136 //setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
137
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000138 computeRegisterProperties();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000139
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000140 addLegalFPImmediate(+0.0); //F31
141 addLegalFPImmediate(-0.0); //-F31
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000142 }
143
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000144 /// LowerOperation - Provide custom lowering hooks for some operations.
145 ///
146 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
147
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000148 /// LowerArguments - This hook must be implemented to indicate how we should
149 /// lower the arguments for the specified function, into the specified DAG.
150 virtual std::vector<SDOperand>
151 LowerArguments(Function &F, SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000152
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000153 /// LowerCallTo - This hook lowers an abstract call to a function into an
154 /// actual call.
155 virtual std::pair<SDOperand, SDOperand>
Chris Lattnerc57f6822005-05-12 19:56:45 +0000156 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattneradf6a962005-05-13 18:50:42 +0000157 bool isTailCall, SDOperand Callee, ArgListTy &Args,
158 SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000159
Chris Lattnere0fe2252005-07-05 19:58:54 +0000160 virtual SDOperand LowerVAStart(SDOperand Chain, SDOperand VAListP,
161 Value *VAListV, SelectionDAG &DAG);
162 virtual SDOperand LowerVACopy(SDOperand Chain, SDOperand SrcP, Value *SrcV,
163 SDOperand DestP, Value *DestV,
164 SelectionDAG &DAG);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000165 virtual std::pair<SDOperand,SDOperand>
Chris Lattnere0fe2252005-07-05 19:58:54 +0000166 LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
167 const Type *ArgTy, SelectionDAG &DAG);
Jeff Cohen00b168892005-07-27 06:12:32 +0000168
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000169 void restoreGP(MachineBasicBlock* BB)
170 {
171 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
172 }
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000173 void restoreRA(MachineBasicBlock* BB)
174 {
175 BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(RA).addReg(RA);
176 }
Andrew Lenharth3b918072005-06-27 15:36:48 +0000177 unsigned getRA()
178 {
179 return RA;
180 }
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000181
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000182 };
183}
184
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000185/// LowerOperation - Provide custom lowering hooks for some operations.
186///
187SDOperand AlphaTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
188 MachineFunction &MF = DAG.getMachineFunction();
189 switch (Op.getOpcode()) {
190 default: assert(0 && "Should not custom lower this!");
Misha Brukmanb8ee91a2005-06-06 17:39:46 +0000191#if 0
192 case ISD::SINT_TO_FP:
193 {
194 assert (Op.getOperand(0).getValueType() == MVT::i64
195 && "only quads can be loaded from");
196 SDOperand SRC;
197 if (EnableAlphaFTOI)
198 {
199 std::vector<MVT::ValueType> RTs;
200 RTs.push_back(Op.getValueType());
201 std::vector<SDOperand> Ops;
202 Ops.push_back(Op.getOperand(0));
203 SRC = DAG.getNode(AlphaISD::ITOF, RTs, Ops);
204 } else {
205 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
206 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Jeff Cohen00b168892005-07-27 06:12:32 +0000207 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other,
208 DAG.getEntryNode(), Op.getOperand(0),
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000209 StackSlot, DAG.getSrcValue(NULL));
Misha Brukmanb8ee91a2005-06-06 17:39:46 +0000210 SRC = DAG.getLoad(Op.getValueType(), Store.getValue(0), StackSlot,
211 DAG.getSrcValue(NULL));
212 }
213 std::vector<MVT::ValueType> RTs;
214 RTs.push_back(Op.getValueType());
215 std::vector<SDOperand> Ops;
216 Ops.push_back(SRC);
217 return DAG.getNode(AlphaISD::CVTQ, RTs, Ops);
218 }
219#endif
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000220 }
Misha Brukmanb8ee91a2005-06-06 17:39:46 +0000221 return SDOperand();
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000222}
223
224
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000225/// AddLiveIn - This helper function adds the specified physical register to the
226/// MachineFunction as a live in value. It also creates a corresponding virtual
227/// register for it.
228static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
229 TargetRegisterClass *RC) {
230 assert(RC->contains(PReg) && "Not the correct regclass!");
231 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
232 MF.addLiveIn(PReg, VReg);
233 return VReg;
234}
235
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000236//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
237
238//For now, just use variable size stack frame format
239
240//In a standard call, the first six items are passed in registers $16
241//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
242//of argument-to-register correspondence.) The remaining items are
243//collected in a memory argument list that is a naturally aligned
244//array of quadwords. In a standard call, this list, if present, must
245//be passed at 0(SP).
Misha Brukman7847fca2005-04-22 17:54:37 +0000246//7 ... n 0(SP) ... (n-7)*8(SP)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000247
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000248// //#define FP $15
249// //#define RA $26
250// //#define PV $27
251// //#define GP $29
252// //#define SP $30
Misha Brukman4633f1c2005-04-21 23:13:11 +0000253
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000254std::vector<SDOperand>
Misha Brukman4633f1c2005-04-21 23:13:11 +0000255AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000256{
257 std::vector<SDOperand> ArgValues;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000258
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000259 MachineFunction &MF = DAG.getMachineFunction();
Andrew Lenharth05380342005-02-07 05:07:00 +0000260 MachineFrameInfo*MFI = MF.getFrameInfo();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000261
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000262 MachineBasicBlock& BB = MF.front();
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));
Chris Lattner707ebc52005-08-16 21:56:37 +0000286 argt = DAG.getCopyFromReg(DAG.getRoot(), args_float[count], VT);
Chris Lattner9d6c45b2005-08-17 17:08:24 +0000287 DAG.setRoot(argt.getValue(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000288 break;
289 case MVT::i1:
290 case MVT::i8:
291 case MVT::i16:
292 case MVT::i32:
293 case MVT::i64:
Jeff Cohen00b168892005-07-27 06:12:32 +0000294 args_int[count] = AddLiveIn(MF, args_int[count],
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000295 getRegClassFor(MVT::i64));
Chris Lattner9d6c45b2005-08-17 17:08:24 +0000296 argt = DAG.getCopyFromReg(DAG.getRoot(), args_int[count], MVT::i64);
297 DAG.setRoot(argt.getValue(1));
Andrew Lenharth14f30c92005-05-31 18:37:16 +0000298 if (VT != MVT::i64)
299 argt = DAG.getNode(ISD::TRUNCATE, VT, argt);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000300 break;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000301 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000302 } else { //more args
303 // Create the frame index object for this incoming parameter...
304 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000305
306 // Create the SelectionDAG nodes corresponding to a load
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000307 //from this parameter
308 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000309 argt = DAG.getLoad(getValueType(I->getType()),
310 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000311 }
Andrew Lenharth032f2352005-02-22 21:59:48 +0000312 ++count;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000313 ArgValues.push_back(argt);
314 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000315
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000316 // If the functions takes variable number of arguments, copy all regs to stack
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000317 if (F.isVarArg()) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000318 VarArgsOffset = count * 8;
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000319 std::vector<SDOperand> LS;
320 for (int i = 0; i < 6; ++i) {
321 if (args_int[i] < 1024)
322 args_int[i] = AddLiveIn(MF,args_int[i], getRegClassFor(MVT::i64));
Chris Lattner707ebc52005-08-16 21:56:37 +0000323 SDOperand argt = DAG.getCopyFromReg(DAG.getRoot(), args_int[i], MVT::i64);
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000324 int FI = MFI->CreateFixedObject(8, -8 * (6 - i));
Andrew Lenharth558bc882005-06-18 18:34:52 +0000325 if (i == 0) VarArgsBase = FI;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000326 SDOperand SDFI = DAG.getFrameIndex(FI, MVT::i64);
Jeff Cohen00b168892005-07-27 06:12:32 +0000327 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt,
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000328 SDFI, DAG.getSrcValue(NULL)));
Jeff Cohen00b168892005-07-27 06:12:32 +0000329
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000330 if (args_float[i] < 1024)
331 args_float[i] = AddLiveIn(MF,args_float[i], getRegClassFor(MVT::f64));
Chris Lattner707ebc52005-08-16 21:56:37 +0000332 argt = DAG.getCopyFromReg(DAG.getRoot(), args_float[i], MVT::f64);
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000333 FI = MFI->CreateFixedObject(8, - 8 * (12 - i));
334 SDFI = DAG.getFrameIndex(FI, MVT::i64);
Jeff Cohen00b168892005-07-27 06:12:32 +0000335 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt,
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000336 SDFI, DAG.getSrcValue(NULL)));
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000337 }
338
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000339 //Set up a token factor with all the stack traffic
340 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, LS));
341 }
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000342
343 // Finally, inform the code generator which regs we return values in.
344 switch (getValueType(F.getReturnType())) {
345 default: assert(0 && "Unknown type!");
346 case MVT::isVoid: break;
347 case MVT::i1:
348 case MVT::i8:
349 case MVT::i16:
350 case MVT::i32:
351 case MVT::i64:
352 MF.addLiveOut(Alpha::R0);
353 break;
354 case MVT::f32:
355 case MVT::f64:
356 MF.addLiveOut(Alpha::F0);
357 break;
358 }
359
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000360 //return the arguments
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000361 return ArgValues;
362}
363
364std::pair<SDOperand, SDOperand>
365AlphaTargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000366 const Type *RetTy, bool isVarArg,
Chris Lattneradf6a962005-05-13 18:50:42 +0000367 unsigned CallingConv, bool isTailCall,
Jeff Cohen00b168892005-07-27 06:12:32 +0000368 SDOperand Callee, ArgListTy &Args,
Misha Brukman7847fca2005-04-22 17:54:37 +0000369 SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000370 int NumBytes = 0;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000371 if (Args.size() > 6)
372 NumBytes = (Args.size() - 6) * 8;
373
Chris Lattner16cd04d2005-05-12 23:24:06 +0000374 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000375 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000376 std::vector<SDOperand> args_to_use;
377 for (unsigned i = 0, e = Args.size(); i != e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000378 {
379 switch (getValueType(Args[i].second)) {
380 default: assert(0 && "Unexpected ValueType for argument!");
381 case MVT::i1:
382 case MVT::i8:
383 case MVT::i16:
384 case MVT::i32:
385 // Promote the integer to 64 bits. If the input type is signed use a
386 // sign extend, otherwise use a zero extend.
387 if (Args[i].second->isSigned())
388 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
389 else
390 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
391 break;
392 case MVT::i64:
393 case MVT::f64:
394 case MVT::f32:
395 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000396 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000397 args_to_use.push_back(Args[i].first);
398 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000399
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000400 std::vector<MVT::ValueType> RetVals;
401 MVT::ValueType RetTyVT = getValueType(RetTy);
402 if (RetTyVT != MVT::isVoid)
403 RetVals.push_back(RetTyVT);
404 RetVals.push_back(MVT::Other);
405
Misha Brukman4633f1c2005-04-21 23:13:11 +0000406 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000407 Chain, Callee, args_to_use), 0);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000408 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner16cd04d2005-05-12 23:24:06 +0000409 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000410 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000411 return std::make_pair(TheCall, Chain);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000412}
413
Chris Lattnere0fe2252005-07-05 19:58:54 +0000414SDOperand AlphaTargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
415 Value *VAListV, SelectionDAG &DAG) {
416 // vastart stores the address of the VarArgsBase and VarArgsOffset
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000417 SDOperand FR = DAG.getFrameIndex(VarArgsBase, MVT::i64);
Jeff Cohen00b168892005-07-27 06:12:32 +0000418 SDOperand S1 = DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
Chris Lattnere0fe2252005-07-05 19:58:54 +0000419 DAG.getSrcValue(VAListV));
Jeff Cohen00b168892005-07-27 06:12:32 +0000420 SDOperand SA2 = DAG.getNode(ISD::ADD, MVT::i64, VAListP,
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000421 DAG.getConstant(8, MVT::i64));
Jeff Cohen00b168892005-07-27 06:12:32 +0000422 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, S1,
423 DAG.getConstant(VarArgsOffset, MVT::i64), SA2,
Chris Lattner9fadb4c2005-07-10 00:29:18 +0000424 DAG.getSrcValue(VAListV, 8), DAG.getValueType(MVT::i32));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000425}
426
427std::pair<SDOperand,SDOperand> AlphaTargetLowering::
Chris Lattnere0fe2252005-07-05 19:58:54 +0000428LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
429 const Type *ArgTy, SelectionDAG &DAG) {
430 SDOperand Base = DAG.getLoad(MVT::i64, Chain, VAListP,
431 DAG.getSrcValue(VAListV));
Jeff Cohen00b168892005-07-27 06:12:32 +0000432 SDOperand Tmp = DAG.getNode(ISD::ADD, MVT::i64, VAListP,
Andrew Lenharth558bc882005-06-18 18:34:52 +0000433 DAG.getConstant(8, MVT::i64));
Jeff Cohen00b168892005-07-27 06:12:32 +0000434 SDOperand Offset = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Base.getValue(1),
Chris Lattnerbce81ae2005-07-10 01:56:13 +0000435 Tmp, DAG.getSrcValue(VAListV, 8), MVT::i32);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000436 SDOperand DataPtr = DAG.getNode(ISD::ADD, MVT::i64, Base, Offset);
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000437 if (ArgTy->isFloatingPoint())
438 {
439 //if fp && Offset < 6*8, then subtract 6*8 from DataPtr
440 SDOperand FPDataPtr = DAG.getNode(ISD::SUB, MVT::i64, DataPtr,
Chris Lattnere0fe2252005-07-05 19:58:54 +0000441 DAG.getConstant(8*6, MVT::i64));
Chris Lattner88ac32c2005-08-09 20:21:10 +0000442 SDOperand CC = DAG.getSetCC(MVT::i64, Offset,
443 DAG.getConstant(8*6, MVT::i64), ISD::SETLT);
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000444 DataPtr = DAG.getNode(ISD::SELECT, MVT::i64, CC, FPDataPtr, DataPtr);
445 }
446
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000447 SDOperand Result;
448 if (ArgTy == Type::IntTy)
Chris Lattnerbce81ae2005-07-10 01:56:13 +0000449 Result = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Offset.getValue(1),
450 DataPtr, DAG.getSrcValue(NULL), MVT::i32);
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000451 else if (ArgTy == Type::UIntTy)
Chris Lattnerbce81ae2005-07-10 01:56:13 +0000452 Result = DAG.getExtLoad(ISD::ZEXTLOAD, MVT::i64, Offset.getValue(1),
453 DataPtr, DAG.getSrcValue(NULL), MVT::i32);
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000454 else
Jeff Cohen00b168892005-07-27 06:12:32 +0000455 Result = DAG.getLoad(getValueType(ArgTy), Offset.getValue(1), DataPtr,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000456 DAG.getSrcValue(NULL));
457
Jeff Cohen00b168892005-07-27 06:12:32 +0000458 SDOperand NewOffset = DAG.getNode(ISD::ADD, MVT::i64, Offset,
Andrew Lenharth558bc882005-06-18 18:34:52 +0000459 DAG.getConstant(8, MVT::i64));
Jeff Cohen00b168892005-07-27 06:12:32 +0000460 SDOperand Update = DAG.getNode(ISD::TRUNCSTORE, MVT::Other,
461 Result.getValue(1), NewOffset,
Chris Lattner9fadb4c2005-07-10 00:29:18 +0000462 Tmp, DAG.getSrcValue(VAListV, 8),
463 DAG.getValueType(MVT::i32));
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000464 Result = DAG.getNode(ISD::TRUNCATE, getValueType(ArgTy), Result);
465
Andrew Lenharth558bc882005-06-18 18:34:52 +0000466 return std::make_pair(Result, Update);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000467}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000468
Chris Lattnere0fe2252005-07-05 19:58:54 +0000469
470SDOperand AlphaTargetLowering::
471LowerVACopy(SDOperand Chain, SDOperand SrcP, Value *SrcV, SDOperand DestP,
472 Value *DestV, SelectionDAG &DAG) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000473 SDOperand Val = DAG.getLoad(getPointerTy(), Chain, SrcP,
Chris Lattnere0fe2252005-07-05 19:58:54 +0000474 DAG.getSrcValue(SrcV));
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000475 SDOperand Result = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
Chris Lattnere0fe2252005-07-05 19:58:54 +0000476 Val, DestP, DAG.getSrcValue(DestV));
Jeff Cohen00b168892005-07-27 06:12:32 +0000477 SDOperand NP = DAG.getNode(ISD::ADD, MVT::i64, SrcP,
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000478 DAG.getConstant(8, MVT::i64));
Chris Lattnerbce81ae2005-07-10 01:56:13 +0000479 Val = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Result, NP,
480 DAG.getSrcValue(SrcV, 8), MVT::i32);
Jeff Cohen00b168892005-07-27 06:12:32 +0000481 SDOperand NPD = DAG.getNode(ISD::ADD, MVT::i64, DestP,
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000482 DAG.getConstant(8, MVT::i64));
Chris Lattnere0fe2252005-07-05 19:58:54 +0000483 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Val.getValue(1),
Chris Lattner9fadb4c2005-07-10 00:29:18 +0000484 Val, NPD, DAG.getSrcValue(DestV, 8),
485 DAG.getValueType(MVT::i32));
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000486}
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000487
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000488namespace {
489
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000490//===--------------------------------------------------------------------===//
491/// ISel - Alpha specific code to select Alpha machine instructions for
492/// SelectionDAG operations.
493//===--------------------------------------------------------------------===//
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000494class AlphaISel : public SelectionDAGISel {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000495
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000496 /// AlphaLowering - This object fully describes how to lower LLVM code to an
497 /// Alpha-specific SelectionDAG.
498 AlphaTargetLowering AlphaLowering;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000499
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000500 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
501 // for sdiv and udiv until it is put into the future
502 // dag combiner.
503
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000504 /// ExprMap - As shared expressions are codegen'd, we keep track of which
505 /// vreg the value is produced in, so we only emit one copy of each compiled
506 /// tree.
507 static const unsigned notIn = (unsigned)(-1);
508 std::map<SDOperand, unsigned> ExprMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000509
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000510 //CCInvMap sometimes (SetNE) we have the inverse CC code for free
511 std::map<SDOperand, unsigned> CCInvMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000512
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000513 int count_ins;
514 int count_outs;
515 bool has_sym;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000516 int max_depth;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000517
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000518public:
Jeff Cohen00b168892005-07-27 06:12:32 +0000519 AlphaISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering),
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000520 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)
Jeff Cohen00b168892005-07-27 06:12:32 +0000540 std::cerr << "COUNT: "
541 << BB->getParent()->getFunction ()->getName() << " "
542 << BB->getNumber() << " "
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000543 << max_depth << " "
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000544 << count_ins << " "
545 << count_outs << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000546
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000547 // Clear state used for selection.
548 ExprMap.clear();
549 CCInvMap.clear();
550 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000551
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000552 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000553
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000554 unsigned SelectExpr(SDOperand N);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000555 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) {
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000581 BuildMI(BB, Alpha::BIS, 2, LI->second).addReg(LI->first)
582 .addReg(LI->first);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000583 } else if (RC == Alpha::FPRCRegisterClass) {
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000584 BuildMI(BB, Alpha::CPYS, 2, LI->second).addReg(LI->first)
585 .addReg(LI->first);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000586 } else {
587 assert(0 && "Unknown regclass!");
588 }
589 }
590 }
591}
592
Andrew Lenharthd2284272005-08-15 14:31:37 +0000593static bool isSIntImmediate(SDOperand N, int64_t& Imm) {
594 // test for constant
595 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
596 // retrieve value
597 Imm = CN->getSignExtended();
598 // passes muster
599 return true;
600 }
601 // not a constant
602 return false;
603}
604
605// isSIntImmediateBounded - This method tests to see if a constant operand
606// bounded s.t. low <= Imm <= high
607// If so Imm will receive the 64 bit value.
608static bool isSIntImmediateBounded(SDOperand N, int64_t& Imm,
609 int64_t low, int64_t high) {
Andrew Lenharth035b8ab2005-08-17 00:47:24 +0000610 if (isSIntImmediate(N, Imm) && Imm <= high && Imm >= low)
Andrew Lenharthd2284272005-08-15 14:31:37 +0000611 return true;
612 return false;
613}
614static bool isUIntImmediate(SDOperand N, uint64_t& Imm) {
615 // test for constant
616 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
617 // retrieve value
618 Imm = (uint64_t)CN->getValue();
619 // passes muster
620 return true;
621 }
622 // not a constant
623 return false;
624}
625
626static bool isUIntImmediateBounded(SDOperand N, uint64_t& Imm,
627 uint64_t low, uint64_t high) {
Andrew Lenharth035b8ab2005-08-17 00:47:24 +0000628 if (isUIntImmediate(N, Imm) && Imm <= high && Imm >= low)
Andrew Lenharthd2284272005-08-15 14:31:37 +0000629 return true;
630 return false;
631}
632
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000633static void getValueInfo(const Value* v, int& type, int& fun, int& offset)
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000634{
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000635 fun = type = offset = 0;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000636 if (v == NULL) {
637 type = 0;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000638 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(v)) {
639 type = 1;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000640 const Module* M = GV->getParent();
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000641 for(Module::const_global_iterator ii = M->global_begin(); &*ii != GV; ++ii)
642 ++offset;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000643 } else if (const Argument* Arg = dyn_cast<Argument>(v)) {
644 type = 2;
645 const Function* F = Arg->getParent();
646 const Module* M = F->getParent();
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000647 for(Module::const_iterator ii = M->begin(); &*ii != F; ++ii)
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000648 ++fun;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000649 for(Function::const_arg_iterator ii = F->arg_begin(); &*ii != Arg; ++ii)
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000650 ++offset;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000651 } else if (const Instruction* I = dyn_cast<Instruction>(v)) {
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +0000652 assert(dyn_cast<PointerType>(I->getType()));
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000653 type = 3;
654 const BasicBlock* bb = I->getParent();
655 const Function* F = bb->getParent();
656 const Module* M = F->getParent();
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000657 for(Module::const_iterator ii = M->begin(); &*ii != F; ++ii)
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000658 ++fun;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000659 for(Function::const_iterator ii = F->begin(); &*ii != bb; ++ii)
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000660 offset += ii->size();
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000661 for(BasicBlock::const_iterator ii = bb->begin(); &*ii != I; ++ii)
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000662 ++offset;
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +0000663 } else if (const Constant* C = dyn_cast<Constant>(v)) {
664 //Don't know how to look these up yet
665 type = 0;
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +0000666 } else {
667 assert(0 && "Error in value marking");
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000668 }
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000669 //type = 4: register spilling
670 //type = 5: global address loading or constant loading
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000671}
672
673static int getUID()
674{
675 static int id = 0;
676 return ++id;
677}
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000678
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000679//Factorize a number using the list of constants
680static bool factorize(int v[], int res[], int size, uint64_t c)
681{
682 bool cont = true;
683 while (c != 1 && cont)
684 {
685 cont = false;
686 for(int i = 0; i < size; ++i)
687 {
688 if (c % v[i] == 0)
689 {
690 c /= v[i];
691 ++res[i];
692 cont=true;
693 }
694 }
695 }
696 return c == 1;
697}
698
699
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000700//Shamelessly adapted from PPC32
Misha Brukman4633f1c2005-04-21 23:13:11 +0000701// Structure used to return the necessary information to codegen an SDIV as
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000702// a multiply.
703struct ms {
704 int64_t m; // magic number
705 int64_t s; // shift amount
706};
707
708struct mu {
709 uint64_t m; // magic number
710 int64_t a; // add indicator
711 int64_t s; // shift amount
712};
713
714/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukman4633f1c2005-04-21 23:13:11 +0000715/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000716/// or -1.
717static struct ms magic(int64_t d) {
718 int64_t p;
719 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
720 const uint64_t two63 = 9223372036854775808ULL; // 2^63
721 struct ms mag;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000722
Andrew Lenharth01c8f6e2005-08-01 17:47:28 +0000723 ad = llabs(d);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000724 t = two63 + ((uint64_t)d >> 63);
725 anc = t - 1 - t%ad; // absolute value of nc
Andrew Lenharth320174f2005-04-07 17:19:16 +0000726 p = 63; // initialize p
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000727 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
728 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
729 q2 = two63/ad; // initialize q2 = 2p/abs(d)
730 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
731 do {
732 p = p + 1;
733 q1 = 2*q1; // update q1 = 2p/abs(nc)
734 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
735 if (r1 >= anc) { // must be unsigned comparison
736 q1 = q1 + 1;
737 r1 = r1 - anc;
738 }
739 q2 = 2*q2; // update q2 = 2p/abs(d)
740 r2 = 2*r2; // update r2 = rem(2p/abs(d))
741 if (r2 >= ad) { // must be unsigned comparison
742 q2 = q2 + 1;
743 r2 = r2 - ad;
744 }
745 delta = ad - r2;
746 } while (q1 < delta || (q1 == delta && r1 == 0));
747
748 mag.m = q2 + 1;
749 if (d < 0) mag.m = -mag.m; // resulting magic number
750 mag.s = p - 64; // resulting shift
751 return mag;
752}
753
754/// magicu - calculate the magic numbers required to codegen an integer udiv as
755/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
756static struct mu magicu(uint64_t d)
757{
758 int64_t p;
759 uint64_t nc, delta, q1, r1, q2, r2;
760 struct mu magu;
761 magu.a = 0; // initialize "add" indicator
762 nc = - 1 - (-d)%d;
Andrew Lenharth320174f2005-04-07 17:19:16 +0000763 p = 63; // initialize p
764 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
765 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
766 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
767 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000768 do {
769 p = p + 1;
770 if (r1 >= nc - r1 ) {
771 q1 = 2*q1 + 1; // update q1
772 r1 = 2*r1 - nc; // update r1
773 }
774 else {
775 q1 = 2*q1; // update q1
776 r1 = 2*r1; // update r1
777 }
778 if (r2 + 1 >= d - r2) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000779 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000780 q2 = 2*q2 + 1; // update q2
781 r2 = 2*r2 + 1 - d; // update r2
782 }
783 else {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000784 if (q2 >= 0x8000000000000000ull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000785 q2 = 2*q2; // update q2
786 r2 = 2*r2 + 1; // update r2
787 }
788 delta = d - 1 - r2;
789 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
790 magu.m = q2 + 1; // resulting magic number
Andrew Lenharth320174f2005-04-07 17:19:16 +0000791 magu.s = p - 64; // resulting shift
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000792 return magu;
793}
794
795/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
796/// return a DAG expression to select that will generate the same value by
797/// multiplying by a magic number. See:
798/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000799SDOperand AlphaISel::BuildSDIVSequence(SDOperand N) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000800 int64_t d = (int64_t)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000801 ms magics = magic(d);
802 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000803 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000804 ISelDAG->getConstant(magics.m, MVT::i64));
805 // If d > 0 and m < 0, add the numerator
806 if (d > 0 && magics.m < 0)
807 Q = ISelDAG->getNode(ISD::ADD, MVT::i64, Q, N.getOperand(0));
808 // If d < 0 and m > 0, subtract the numerator.
809 if (d < 0 && magics.m > 0)
810 Q = ISelDAG->getNode(ISD::SUB, MVT::i64, Q, N.getOperand(0));
811 // Shift right algebraic if shift value is nonzero
812 if (magics.s > 0)
Misha Brukman4633f1c2005-04-21 23:13:11 +0000813 Q = ISelDAG->getNode(ISD::SRA, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000814 ISelDAG->getConstant(magics.s, MVT::i64));
815 // Extract the sign bit and add it to the quotient
Misha Brukman4633f1c2005-04-21 23:13:11 +0000816 SDOperand T =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000817 ISelDAG->getNode(ISD::SRL, MVT::i64, Q, ISelDAG->getConstant(63, MVT::i64));
818 return ISelDAG->getNode(ISD::ADD, MVT::i64, Q, T);
819}
820
821/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
822/// return a DAG expression to select that will generate the same value by
823/// multiplying by a magic number. See:
824/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000825SDOperand AlphaISel::BuildUDIVSequence(SDOperand N) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000826 unsigned d =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000827 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
828 mu magics = magicu(d);
829 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000830 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000831 ISelDAG->getConstant(magics.m, MVT::i64));
832 if (magics.a == 0) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000833 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000834 ISelDAG->getConstant(magics.s, MVT::i64));
835 } else {
836 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i64, N.getOperand(0), Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000837 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000838 ISelDAG->getConstant(1, MVT::i64));
839 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i64, NPQ, Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000840 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000841 ISelDAG->getConstant(magics.s-1, MVT::i64));
842 }
843 return Q;
844}
845
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000846//These describe LDAx
Andrew Lenharthc0513832005-03-29 19:24:04 +0000847static const int IMM_LOW = -32768;
848static const int IMM_HIGH = 32767;
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000849static const int IMM_MULT = 65536;
850
851static long getUpper16(long l)
852{
853 long y = l / IMM_MULT;
854 if (l % IMM_MULT > IMM_HIGH)
855 ++y;
856 return y;
857}
858
859static long getLower16(long l)
860{
861 long h = getUpper16(l);
862 return l - h * IMM_MULT;
863}
864
Andrew Lenharthfe895e32005-06-27 17:15:36 +0000865static unsigned GetRelVersion(unsigned opcode)
866{
867 switch (opcode) {
868 default: assert(0 && "unknown load or store"); return 0;
869 case Alpha::LDQ: return Alpha::LDQr;
870 case Alpha::LDS: return Alpha::LDSr;
871 case Alpha::LDT: return Alpha::LDTr;
872 case Alpha::LDL: return Alpha::LDLr;
873 case Alpha::LDBU: return Alpha::LDBUr;
874 case Alpha::LDWU: return Alpha::LDWUr;
Andrew Lenharthfce587e2005-06-29 00:39:17 +0000875 case Alpha::STB: return Alpha::STBr;
876 case Alpha::STW: return Alpha::STWr;
877 case Alpha::STL: return Alpha::STLr;
878 case Alpha::STQ: return Alpha::STQr;
879 case Alpha::STS: return Alpha::STSr;
880 case Alpha::STT: return Alpha::STTr;
881
Andrew Lenharthfe895e32005-06-27 17:15:36 +0000882 }
883}
Andrew Lenharth65838902005-02-06 16:22:15 +0000884
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000885void AlphaISel::MoveFP2Int(unsigned src, unsigned dst, bool isDouble)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000886{
887 unsigned Opc;
888 if (EnableAlphaFTOI) {
889 Opc = isDouble ? Alpha::FTOIT : Alpha::FTOIS;
Andrew Lenharth98169be2005-07-28 18:14:47 +0000890 BuildMI(BB, Opc, 1, dst).addReg(src).addReg(Alpha::F31);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000891 } else {
892 //The hard way:
893 // Spill the integer to memory and reload it from there.
894 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
895 MachineFunction *F = BB->getParent();
896 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
897
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000898 if (EnableAlphaLSMark)
899 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
900 .addImm(getUID());
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000901 Opc = isDouble ? Alpha::STT : Alpha::STS;
902 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000903
904 if (EnableAlphaLSMark)
905 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
906 .addImm(getUID());
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000907 Opc = isDouble ? Alpha::LDQ : Alpha::LDL;
908 BuildMI(BB, Alpha::LDQ, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
909 }
910}
911
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000912void AlphaISel::MoveInt2FP(unsigned src, unsigned dst, bool isDouble)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000913{
914 unsigned Opc;
915 if (EnableAlphaFTOI) {
916 Opc = isDouble?Alpha::ITOFT:Alpha::ITOFS;
Andrew Lenharth98169be2005-07-28 18:14:47 +0000917 BuildMI(BB, Opc, 1, dst).addReg(src).addReg(Alpha::R31);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000918 } else {
919 //The hard way:
920 // Spill the integer to memory and reload it from there.
921 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
922 MachineFunction *F = BB->getParent();
923 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
924
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000925 if (EnableAlphaLSMark)
926 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
927 .addImm(getUID());
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000928 Opc = isDouble ? Alpha::STQ : Alpha::STL;
929 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000930
931 if (EnableAlphaLSMark)
932 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
933 .addImm(getUID());
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000934 Opc = isDouble ? Alpha::LDT : Alpha::LDS;
935 BuildMI(BB, Opc, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
936 }
937}
938
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000939bool AlphaISel::SelectFPSetCC(SDOperand N, unsigned dst)
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000940{
Chris Lattner88ac32c2005-08-09 20:21:10 +0000941 SDNode *SetCC = N.Val;
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000942 unsigned Opc, Tmp1, Tmp2, Tmp3;
Chris Lattner88ac32c2005-08-09 20:21:10 +0000943 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC->getOperand(2))->get();
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000944 bool rev = false;
945 bool inv = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000946
Chris Lattner88ac32c2005-08-09 20:21:10 +0000947 switch (CC) {
948 default: SetCC->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000949 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
950 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
951 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
952 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
953 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
954 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
955 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000956
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000957 ConstantFPSDNode *CN;
958 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
959 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
960 Tmp1 = Alpha::F31;
961 else
962 Tmp1 = SelectExpr(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000963
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000964 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
965 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
966 Tmp2 = Alpha::F31;
967 else
Chris Lattner9c9183a2005-04-30 04:44:07 +0000968 Tmp2 = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000969
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000970 //Can only compare doubles, and dag won't promote for me
971 if (SetCC->getOperand(0).getValueType() == MVT::f32)
972 {
973 //assert(0 && "Setcc On float?\n");
974 std::cerr << "Setcc on float!\n";
975 Tmp3 = MakeReg(MVT::f64);
Andrew Lenharth98169be2005-07-28 18:14:47 +0000976 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000977 Tmp1 = Tmp3;
978 }
979 if (SetCC->getOperand(1).getValueType() == MVT::f32)
980 {
981 //assert (0 && "Setcc On float?\n");
982 std::cerr << "Setcc on float!\n";
983 Tmp3 = MakeReg(MVT::f64);
Andrew Lenharth98169be2005-07-28 18:14:47 +0000984 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Alpha::F31).addReg(Tmp2);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000985 Tmp2 = Tmp3;
986 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000987
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000988 if (rev) std::swap(Tmp1, Tmp2);
989 //do the comparison
990 BuildMI(BB, Opc, 2, dst).addReg(Tmp1).addReg(Tmp2);
991 return inv;
992}
993
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000994//Check to see if the load is a constant offset from a base register
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000995void AlphaISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000996{
997 unsigned opcode = N.getOpcode();
Andrew Lenharth694c2982005-06-26 23:01:11 +0000998 if (opcode == ISD::ADD && N.getOperand(1).getOpcode() == ISD::Constant &&
999 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
1000 { //Normal imm add
1001 Reg = SelectExpr(N.getOperand(0));
1002 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1003 return;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001004 }
1005 Reg = SelectExpr(N);
1006 offset = 0;
1007 return;
1008}
1009
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001010void AlphaISel::SelectBranchCC(SDOperand N)
Andrew Lenharth445171a2005-02-08 00:40:03 +00001011{
1012 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001013 MachineBasicBlock *Dest =
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001014 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
1015 unsigned Opc = Alpha::WTF;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001016
Andrew Lenharth445171a2005-02-08 00:40:03 +00001017 Select(N.getOperand(0)); //chain
1018 SDOperand CC = N.getOperand(1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001019
Andrew Lenharth445171a2005-02-08 00:40:03 +00001020 if (CC.getOpcode() == ISD::SETCC)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001021 {
Chris Lattner88ac32c2005-08-09 20:21:10 +00001022 ISD::CondCode cCode= cast<CondCodeSDNode>(CC.getOperand(2))->get();
1023 if (MVT::isInteger(CC.getOperand(0).getValueType())) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001024 //Dropping the CC is only useful if we are comparing to 0
Chris Lattner88ac32c2005-08-09 20:21:10 +00001025 bool RightZero = CC.getOperand(1).getOpcode() == ISD::Constant &&
1026 cast<ConstantSDNode>(CC.getOperand(1))->getValue() == 0;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001027 bool isNE = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001028
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001029 //Fix up CC
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001030 if(cCode == ISD::SETNE)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001031 isNE = true;
Andrew Lenharth445171a2005-02-08 00:40:03 +00001032
Andrew Lenharth694c2982005-06-26 23:01:11 +00001033 if (RightZero) {
Andrew Lenharth09552bf2005-06-08 18:02:21 +00001034 switch (cCode) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001035 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
1036 case ISD::SETEQ: Opc = Alpha::BEQ; break;
1037 case ISD::SETLT: Opc = Alpha::BLT; break;
1038 case ISD::SETLE: Opc = Alpha::BLE; break;
1039 case ISD::SETGT: Opc = Alpha::BGT; break;
1040 case ISD::SETGE: Opc = Alpha::BGE; break;
1041 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
1042 case ISD::SETUGT: Opc = Alpha::BNE; break;
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001043 //Technically you could have this CC
1044 case ISD::SETULE: Opc = Alpha::BEQ; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001045 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
1046 case ISD::SETNE: Opc = Alpha::BNE; break;
1047 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001048 unsigned Tmp1 = SelectExpr(CC.getOperand(0)); //Cond
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001049 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
1050 return;
1051 } else {
1052 unsigned Tmp1 = SelectExpr(CC);
1053 if (isNE)
1054 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
1055 else
1056 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001057 return;
1058 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001059 } else { //FP
Jeff Cohen00b168892005-07-27 06:12:32 +00001060 //Any comparison between 2 values should be codegened as an folded
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001061 //branch, as moving CC to the integer register is very expensive
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001062 //for a cmp b: c = a - b;
1063 //a = b: c = 0
1064 //a < b: c < 0
1065 //a > b: c > 0
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001066
1067 bool invTest = false;
1068 unsigned Tmp3;
1069
1070 ConstantFPSDNode *CN;
Chris Lattner88ac32c2005-08-09 20:21:10 +00001071 if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(1)))
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001072 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
Chris Lattner88ac32c2005-08-09 20:21:10 +00001073 Tmp3 = SelectExpr(CC.getOperand(0));
1074 else if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(0)))
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001075 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1076 {
Chris Lattner88ac32c2005-08-09 20:21:10 +00001077 Tmp3 = SelectExpr(CC.getOperand(1));
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001078 invTest = true;
1079 }
1080 else
1081 {
Chris Lattner88ac32c2005-08-09 20:21:10 +00001082 unsigned Tmp1 = SelectExpr(CC.getOperand(0));
1083 unsigned Tmp2 = SelectExpr(CC.getOperand(1));
1084 bool isD = CC.getOperand(0).getValueType() == MVT::f64;
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001085 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1086 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1087 .addReg(Tmp1).addReg(Tmp2);
1088 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001089
Chris Lattner88ac32c2005-08-09 20:21:10 +00001090 switch (cCode) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001091 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001092 case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
1093 case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
1094 case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
1095 case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
1096 case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
1097 case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001098 }
1099 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001100 return;
1101 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001102 abort(); //Should never be reached
1103 } else {
1104 //Giveup and do the stupid thing
1105 unsigned Tmp1 = SelectExpr(CC);
1106 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
1107 return;
1108 }
Andrew Lenharth445171a2005-02-08 00:40:03 +00001109 abort(); //Should never be reached
1110}
1111
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001112unsigned AlphaISel::SelectExpr(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001113 unsigned Result;
Andrew Lenharth2966e842005-04-07 18:15:28 +00001114 unsigned Tmp1, Tmp2 = 0, Tmp3;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001115 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001116 unsigned opcode = N.getOpcode();
Andrew Lenharthd2284272005-08-15 14:31:37 +00001117 int64_t SImm;
1118 uint64_t UImm;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001119
1120 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001121 MVT::ValueType DestType = N.getValueType();
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001122 bool isFP = DestType == MVT::f64 || DestType == MVT::f32;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001123
1124 unsigned &Reg = ExprMap[N];
1125 if (Reg) return Reg;
1126
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001127 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::TAILCALL)
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001128 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001129 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001130 else {
1131 // If this is a call instruction, make sure to prepare ALL of the result
1132 // values as well as the chain.
1133 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001134 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001135 else {
1136 Result = MakeReg(Node->getValueType(0));
1137 ExprMap[N.getValue(0)] = Result;
1138 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1139 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001140 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001141 }
1142 }
1143
Andrew Lenharth40831c52005-01-28 06:57:18 +00001144 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001145 default:
1146 Node->dump();
1147 assert(0 && "Node not handled!\n");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001148
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001149 case ISD::CTPOP:
1150 case ISD::CTTZ:
1151 case ISD::CTLZ:
1152 Opc = opcode == ISD::CTPOP ? Alpha::CTPOP :
1153 (opcode == ISD::CTTZ ? Alpha::CTTZ : Alpha::CTLZ);
1154 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthf3f951a2005-07-22 20:50:29 +00001155 BuildMI(BB, Opc, 1, Result).addReg(Alpha::R31).addReg(Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001156 return Result;
1157
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001158 case ISD::MULHU:
1159 Tmp1 = SelectExpr(N.getOperand(0));
1160 Tmp2 = SelectExpr(N.getOperand(1));
1161 BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth706be912005-04-07 13:55:53 +00001162 return Result;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001163 case ISD::MULHS:
1164 {
1165 //MULHU - Ra<63>*Rb - Rb<63>*Ra
1166 Tmp1 = SelectExpr(N.getOperand(0));
1167 Tmp2 = SelectExpr(N.getOperand(1));
1168 Tmp3 = MakeReg(MVT::i64);
1169 BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1170 unsigned V1 = MakeReg(MVT::i64);
1171 unsigned V2 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001172 BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31)
1173 .addReg(Tmp1);
1174 BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31)
1175 .addReg(Tmp2);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001176 unsigned IRes = MakeReg(MVT::i64);
1177 BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
1178 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
1179 return Result;
1180 }
Andrew Lenharth7332f3e2005-04-02 19:11:07 +00001181 case ISD::UNDEF: {
1182 BuildMI(BB, Alpha::IDEF, 0, Result);
1183 return Result;
1184 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001185
Andrew Lenharth032f2352005-02-22 21:59:48 +00001186 case ISD::DYNAMIC_STACKALLOC:
1187 // Generate both result values.
Andrew Lenharth3a7118d2005-02-23 17:33:42 +00001188 if (Result != notIn)
1189 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth032f2352005-02-22 21:59:48 +00001190 else
1191 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1192
1193 // FIXME: We are currently ignoring the requested alignment for handling
1194 // greater than the stack alignment. This will need to be revisited at some
1195 // point. Align = N.getOperand(2);
1196
1197 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1198 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1199 std::cerr << "Cannot allocate stack object with greater alignment than"
1200 << " the stack alignment yet!";
1201 abort();
1202 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001203
Andrew Lenharth032f2352005-02-22 21:59:48 +00001204 Select(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001205 if (isSIntImmediateBounded(N.getOperand(1), SImm, 0, 32767))
1206 BuildMI(BB, Alpha::LDA, 2, Alpha::R30).addImm(-SImm).addReg(Alpha::R30);
1207 else {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001208 Tmp1 = SelectExpr(N.getOperand(1));
1209 // Subtract size from stack pointer, thereby allocating some space.
1210 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1211 }
1212
1213 // Put a pointer to the space into the result register, by copying the stack
1214 // pointer.
Andrew Lenharth7bc47022005-02-22 23:29:25 +00001215 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
Andrew Lenharth032f2352005-02-22 21:59:48 +00001216 return Result;
1217
Andrew Lenharth02c318e2005-06-27 21:02:56 +00001218 case ISD::ConstantPool:
1219 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1220 AlphaLowering.restoreGP(BB);
1221 Tmp2 = MakeReg(MVT::i64);
1222 BuildMI(BB, Alpha::LDAHr, 2, Tmp2).addConstantPoolIndex(Tmp1)
1223 .addReg(Alpha::R29);
1224 BuildMI(BB, Alpha::LDAr, 2, Result).addConstantPoolIndex(Tmp1)
1225 .addReg(Tmp2);
1226 return Result;
Andrew Lenharth2c594352005-01-29 15:42:07 +00001227
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001228 case ISD::FrameIndex:
Andrew Lenharth032f2352005-02-22 21:59:48 +00001229 BuildMI(BB, Alpha::LDA, 2, Result)
1230 .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
1231 .addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001232 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001233
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001234 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001235 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001236 case ISD::SEXTLOAD:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001237 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001238 {
1239 // Make sure we generate both values.
1240 if (Result != notIn)
1241 ExprMap[N.getValue(1)] = notIn; // Generate the token
1242 else
1243 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001244
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001245 SDOperand Chain = N.getOperand(0);
1246 SDOperand Address = N.getOperand(1);
1247 Select(Chain);
1248
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001249 bool fpext = true;
1250
Andrew Lenharth03824012005-02-07 05:55:55 +00001251 if (opcode == ISD::LOAD)
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001252 switch (Node->getValueType(0)) {
1253 default: Node->dump(); assert(0 && "Bad load!");
1254 case MVT::i64: Opc = Alpha::LDQ; break;
1255 case MVT::f64: Opc = Alpha::LDT; break;
1256 case MVT::f32: Opc = Alpha::LDS; break;
1257 }
Andrew Lenharth03824012005-02-07 05:55:55 +00001258 else
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001259 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001260 default: Node->dump(); assert(0 && "Bad sign extend!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001261 case MVT::i32: Opc = Alpha::LDL;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001262 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001263 case MVT::i16: Opc = Alpha::LDWU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001264 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001265 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Misha Brukman4633f1c2005-04-21 23:13:11 +00001266 case MVT::i8: Opc = Alpha::LDBU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001267 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001268 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001269
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001270 int i, j, k;
1271 if (EnableAlphaLSMark)
1272 getValueInfo(dyn_cast<SrcValueSDNode>(N.getOperand(2))->getValue(),
1273 i, j, k);
1274
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001275 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Address);
1276 if (GASD && !GASD->getGlobal()->isExternal()) {
1277 Tmp1 = MakeReg(MVT::i64);
1278 AlphaLowering.restoreGP(BB);
1279 BuildMI(BB, Alpha::LDAHr, 2, Tmp1)
1280 .addGlobalAddress(GASD->getGlobal()).addReg(Alpha::R29);
1281 if (EnableAlphaLSMark)
1282 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1283 .addImm(getUID());
1284 BuildMI(BB, GetRelVersion(Opc), 2, Result)
1285 .addGlobalAddress(GASD->getGlobal()).addReg(Tmp1);
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001286 } else if (ConstantPoolSDNode *CP =
1287 dyn_cast<ConstantPoolSDNode>(Address)) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001288 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001289 has_sym = true;
Andrew Lenharthfe895e32005-06-27 17:15:36 +00001290 Tmp1 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001291 BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CP->getIndex())
1292 .addReg(Alpha::R29);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001293 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001294 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1295 .addImm(getUID());
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001296 BuildMI(BB, GetRelVersion(Opc), 2, Result)
1297 .addConstantPoolIndex(CP->getIndex()).addReg(Tmp1);
1298 } else if(Address.getOpcode() == ISD::FrameIndex) {
1299 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001300 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1301 .addImm(getUID());
Andrew Lenharth032f2352005-02-22 21:59:48 +00001302 BuildMI(BB, Opc, 2, Result)
1303 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1304 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001305 } else {
1306 long offset;
1307 SelectAddr(Address, Tmp1, offset);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001308 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001309 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1310 .addImm(getUID());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001311 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1312 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001313 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001314 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001315
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001316 case ISD::GlobalAddress:
1317 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001318 has_sym = true;
Jeff Cohen00b168892005-07-27 06:12:32 +00001319
Andrew Lenharth2f5bca52005-07-03 20:06:13 +00001320 Reg = Result = MakeReg(MVT::i64);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001321
1322 if (EnableAlphaLSMark)
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001323 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001324 .addImm(getUID());
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001325
1326 BuildMI(BB, Alpha::LDQl, 2, Result)
Andrew Lenharthc95d9842005-06-27 21:11:40 +00001327 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal())
1328 .addReg(Alpha::R29);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001329 return Result;
1330
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001331 case ISD::ExternalSymbol:
1332 AlphaLowering.restoreGP(BB);
1333 has_sym = true;
1334
Andrew Lenharth2f5bca52005-07-03 20:06:13 +00001335 Reg = Result = MakeReg(MVT::i64);
1336
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001337 if (EnableAlphaLSMark)
1338 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
1339 .addImm(getUID());
1340
1341 BuildMI(BB, Alpha::LDQl, 2, Result)
1342 .addExternalSymbol(cast<ExternalSymbolSDNode>(N)->getSymbol())
1343 .addReg(Alpha::R29);
1344 return Result;
1345
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001346 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001347 case ISD::CALL:
1348 {
1349 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001350
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001351 // The chain for this call is now lowered.
Andrew Lenharthf3f951a2005-07-22 20:50:29 +00001352 ExprMap[N.getValue(Node->getNumValues()-1)] = notIn;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001353
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001354 //grab the arguments
1355 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001356 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001357 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001358 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001359
Andrew Lenharth684f2292005-01-30 00:35:27 +00001360 //in reg args
1361 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001362 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001363 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001364 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +00001365 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001366 Alpha::F19, Alpha::F20, Alpha::F21};
1367 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001368 default:
1369 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001370 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001371 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001372 N.getOperand(i+2).getValueType() << "\n";
1373 assert(0 && "Unknown value type for call");
1374 case MVT::i1:
1375 case MVT::i8:
1376 case MVT::i16:
1377 case MVT::i32:
1378 case MVT::i64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001379 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i])
1380 .addReg(argvregs[i]);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001381 break;
1382 case MVT::f32:
1383 case MVT::f64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001384 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i])
1385 .addReg(argvregs[i]);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001386 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001387 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001388 }
Andrew Lenharth684f2292005-01-30 00:35:27 +00001389 //in mem args
1390 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001391 {
1392 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001393 default:
1394 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001395 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001396 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001397 N.getOperand(i+2).getValueType() << "\n";
1398 assert(0 && "Unknown value type for call");
1399 case MVT::i1:
1400 case MVT::i8:
1401 case MVT::i16:
1402 case MVT::i32:
1403 case MVT::i64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001404 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
1405 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001406 break;
1407 case MVT::f32:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001408 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
1409 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001410 break;
1411 case MVT::f64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001412 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
1413 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001414 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001415 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001416 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001417 //build the right kind of call
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001418 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(N.getOperand(1));
1419 if (GASD && !GASD->getGlobal()->isExternal()) {
1420 //use PC relative branch call
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001421 AlphaLowering.restoreGP(BB);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001422 BuildMI(BB, Alpha::BSR, 1, Alpha::R26)
1423 .addGlobalAddress(GASD->getGlobal(),true);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001424 } else {
1425 //no need to restore GP as we are doing an indirect call
1426 Tmp1 = SelectExpr(N.getOperand(1));
1427 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
1428 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
1429 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001430
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001431 //push the result into a virtual register
Misha Brukman4633f1c2005-04-21 23:13:11 +00001432
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001433 switch (Node->getValueType(0)) {
1434 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001435 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001436 case MVT::i1:
1437 case MVT::i8:
1438 case MVT::i16:
1439 case MVT::i32:
1440 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001441 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
1442 break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001443 case MVT::f32:
1444 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001445 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
1446 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001447 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001448 return Result+N.ResNo;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001449 }
1450
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001451 case ISD::SIGN_EXTEND_INREG:
1452 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001453 //do SDIV opt for all levels of ints if not dividing by a constant
1454 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
1455 && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001456 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001457 unsigned Tmp4 = MakeReg(MVT::f64);
1458 unsigned Tmp5 = MakeReg(MVT::f64);
1459 unsigned Tmp6 = MakeReg(MVT::f64);
1460 unsigned Tmp7 = MakeReg(MVT::f64);
1461 unsigned Tmp8 = MakeReg(MVT::f64);
1462 unsigned Tmp9 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001463
1464 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1465 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1466 MoveInt2FP(Tmp1, Tmp4, true);
1467 MoveInt2FP(Tmp2, Tmp5, true);
Andrew Lenharth98169be2005-07-28 18:14:47 +00001468 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Alpha::F31).addReg(Tmp4);
1469 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Alpha::F31).addReg(Tmp5);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001470 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
Andrew Lenharth98169be2005-07-28 18:14:47 +00001471 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Alpha::F31).addReg(Tmp8);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001472 MoveFP2Int(Tmp9, Result, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001473 return Result;
1474 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001475
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001476 //Alpha has instructions for a bunch of signed 32 bit stuff
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001477 if(cast<VTSDNode>(Node->getOperand(1))->getVT() == MVT::i32) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001478 switch (N.getOperand(0).getOpcode()) {
1479 case ISD::ADD:
1480 case ISD::SUB:
1481 case ISD::MUL:
1482 {
1483 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
1484 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
1485 //FIXME: first check for Scaled Adds and Subs!
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001486 if(!isMul && N.getOperand(0).getOperand(0).getOpcode() == ISD::SHL &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001487 isSIntImmediateBounded(N.getOperand(0).getOperand(0).getOperand(1), SImm, 2, 3))
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001488 {
Andrew Lenharthd2284272005-08-15 14:31:37 +00001489 bool use4 = SImm == 2;
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001490 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1491 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1492 BuildMI(BB, isAdd?(use4?Alpha::S4ADDL:Alpha::S8ADDL):(use4?Alpha::S4SUBL:Alpha::S8SUBL),
1493 2,Result).addReg(Tmp1).addReg(Tmp2);
1494 }
1495 else if(isAdd && N.getOperand(0).getOperand(1).getOpcode() == ISD::SHL &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001496 isSIntImmediateBounded(N.getOperand(0).getOperand(1).getOperand(1), SImm, 2, 3))
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001497 {
Andrew Lenharthd2284272005-08-15 14:31:37 +00001498 bool use4 = SImm == 2;
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001499 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1500 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1501 BuildMI(BB, use4?Alpha::S4ADDL:Alpha::S8ADDL, 2,Result).addReg(Tmp1).addReg(Tmp2);
1502 }
Andrew Lenharthd2284272005-08-15 14:31:37 +00001503 else if(isSIntImmediateBounded(N.getOperand(0).getOperand(1), SImm, 0, 255))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001504 { //Normal imm add/sub
1505 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001506 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001507 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001508 }
Andrew Lenharthd2284272005-08-15 14:31:37 +00001509 else if(!isMul && isSIntImmediate(N.getOperand(0).getOperand(1), SImm) &&
1510 (((SImm << 32) >> 32) >= -255) && (((SImm << 32) >> 32) <= 0))
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001511 { //handle canonicalization
1512 Opc = isAdd ? Alpha::SUBLi : Alpha::ADDLi;
1513 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001514 SImm = 0 - ((SImm << 32) >> 32);
1515 assert(SImm >= 0 && SImm <= 255);
1516 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001517 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001518 else
1519 { //Normal add/sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001520 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULL : Alpha::SUBL);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001521 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001522 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001523 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1524 }
1525 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001526 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001527 default: break; //Fall Though;
1528 }
1529 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001530 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001531 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001532 switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001533 default:
1534 Node->dump();
1535 assert(0 && "Sign Extend InReg not there yet");
1536 break;
1537 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001538 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001539 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001540 break;
1541 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001542 case MVT::i16:
Andrew Lenharthf3f951a2005-07-22 20:50:29 +00001543 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Alpha::R31).addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001544 break;
1545 case MVT::i8:
Andrew Lenharthf3f951a2005-07-22 20:50:29 +00001546 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Alpha::R31).addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001547 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +00001548 case MVT::i1:
1549 Tmp2 = MakeReg(MVT::i64);
1550 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001551 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +00001552 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001553 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001554 return Result;
1555 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001556
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001557 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001558 {
Chris Lattner88ac32c2005-08-09 20:21:10 +00001559 ISD::CondCode CC = cast<CondCodeSDNode>(N.getOperand(2))->get();
1560 if (MVT::isInteger(N.getOperand(0).getValueType())) {
1561 bool isConst = false;
1562 int dir;
Misha Brukman7847fca2005-04-22 17:54:37 +00001563
Chris Lattner88ac32c2005-08-09 20:21:10 +00001564 //Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001565 if(isSIntImmediate(N.getOperand(1), SImm) && SImm <= 255 && SImm >= 0)
Chris Lattner88ac32c2005-08-09 20:21:10 +00001566 isConst = true;
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001567
Chris Lattner88ac32c2005-08-09 20:21:10 +00001568 switch (CC) {
1569 default: Node->dump(); assert(0 && "Unknown integer comparison!");
1570 case ISD::SETEQ:
1571 Opc = isConst ? Alpha::CMPEQi : Alpha::CMPEQ; dir=1; break;
1572 case ISD::SETLT:
1573 Opc = isConst ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
1574 case ISD::SETLE:
1575 Opc = isConst ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
1576 case ISD::SETGT: Opc = Alpha::CMPLT; dir = 2; break;
1577 case ISD::SETGE: Opc = Alpha::CMPLE; dir = 2; break;
1578 case ISD::SETULT:
1579 Opc = isConst ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
1580 case ISD::SETUGT: Opc = Alpha::CMPULT; dir = 2; break;
1581 case ISD::SETULE:
1582 Opc = isConst ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
1583 case ISD::SETUGE: Opc = Alpha::CMPULE; dir = 2; break;
1584 case ISD::SETNE: {//Handle this one special
1585 //std::cerr << "Alpha does not have a setne.\n";
1586 //abort();
1587 Tmp1 = SelectExpr(N.getOperand(0));
1588 Tmp2 = SelectExpr(N.getOperand(1));
1589 Tmp3 = MakeReg(MVT::i64);
1590 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1591 //Remeber we have the Inv for this CC
1592 CCInvMap[N] = Tmp3;
1593 //and invert
1594 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
1595 return Result;
1596 }
1597 }
1598 if (dir == 1) {
1599 Tmp1 = SelectExpr(N.getOperand(0));
1600 if (isConst) {
Andrew Lenharthd2284272005-08-15 14:31:37 +00001601 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Chris Lattner88ac32c2005-08-09 20:21:10 +00001602 } else {
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001603 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth694c2982005-06-26 23:01:11 +00001604 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001605 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001606 } else { //if (dir == 2) {
1607 Tmp1 = SelectExpr(N.getOperand(1));
1608 Tmp2 = SelectExpr(N.getOperand(0));
1609 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001610 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001611 } else {
1612 //do the comparison
1613 Tmp1 = MakeReg(MVT::f64);
1614 bool inv = SelectFPSetCC(N, Tmp1);
1615
1616 //now arrange for Result (int) to have a 1 or 0
1617 Tmp2 = MakeReg(MVT::i64);
1618 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
1619 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
1620 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
Andrew Lenharth9818c052005-02-05 13:19:12 +00001621 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001622 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001623 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001624
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001625 case ISD::CopyFromReg:
1626 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001627 ++count_ins;
1628
Andrew Lenharth40831c52005-01-28 06:57:18 +00001629 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001630 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001631 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001632 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001633 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001634
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001635 SDOperand Chain = N.getOperand(0);
1636
1637 Select(Chain);
Chris Lattner707ebc52005-08-16 21:56:37 +00001638 unsigned r = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001639 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
Andrew Lenharth619fb522005-07-04 20:07:21 +00001640 if (MVT::isFloatingPoint(N.getValue(0).getValueType()))
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001641 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
1642 else
1643 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001644 return Result;
1645 }
1646
Misha Brukman4633f1c2005-04-21 23:13:11 +00001647 //Most of the plain arithmetic and logic share the same form, and the same
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001648 //constant immediate test
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001649 case ISD::XOR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001650 //Match Not
Andrew Lenharthd2284272005-08-15 14:31:37 +00001651 if (isSIntImmediate(N.getOperand(1), SImm) && SImm == -1) {
1652 Tmp1 = SelectExpr(N.getOperand(0));
1653 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1654 return Result;
1655 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001656 //Fall through
1657 case ISD::AND:
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001658 //handle zap
Andrew Lenharthd2284272005-08-15 14:31:37 +00001659 if (opcode == ISD::AND && isUIntImmediate(N.getOperand(1), UImm))
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001660 {
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001661 unsigned int build = 0;
1662 for(int i = 0; i < 8; ++i)
1663 {
Andrew Lenharthd2284272005-08-15 14:31:37 +00001664 if ((UImm & 0x00FF) == 0x00FF)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001665 build |= 1 << i;
Andrew Lenharthd2284272005-08-15 14:31:37 +00001666 else if ((UImm & 0x00FF) != 0)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001667 { build = 0; break; }
Andrew Lenharthd2284272005-08-15 14:31:37 +00001668 UImm >>= 8;
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001669 }
1670 if (build)
1671 {
1672 Tmp1 = SelectExpr(N.getOperand(0));
1673 BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1674 return Result;
1675 }
1676 }
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001677 case ISD::OR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001678 //Check operand(0) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001679 if (N.getOperand(0).getOpcode() == ISD::XOR &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001680 isSIntImmediate(N.getOperand(0).getOperand(1), SImm) && SImm == -1) {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001681 switch(opcode) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001682 case ISD::AND: Opc = Alpha::BIC; break;
1683 case ISD::OR: Opc = Alpha::ORNOT; break;
1684 case ISD::XOR: Opc = Alpha::EQV; break;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001685 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001686 Tmp1 = SelectExpr(N.getOperand(1));
1687 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1688 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1689 return Result;
1690 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001691 //Check operand(1) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001692 if (N.getOperand(1).getOpcode() == ISD::XOR &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001693 isSIntImmediate(N.getOperand(1).getOperand(1), SImm) && SImm == -1) {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001694 switch(opcode) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001695 case ISD::AND: Opc = Alpha::BIC; break;
1696 case ISD::OR: Opc = Alpha::ORNOT; break;
1697 case ISD::XOR: Opc = Alpha::EQV; break;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001698 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001699 Tmp1 = SelectExpr(N.getOperand(0));
1700 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1701 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1702 return Result;
1703 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001704 //Fall through
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001705 case ISD::SHL:
1706 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001707 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001708 case ISD::MUL:
Andrew Lenharthd2284272005-08-15 14:31:37 +00001709 if(isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255)) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001710 switch(opcode) {
1711 case ISD::AND: Opc = Alpha::ANDi; break;
1712 case ISD::OR: Opc = Alpha::BISi; break;
1713 case ISD::XOR: Opc = Alpha::XORi; break;
1714 case ISD::SHL: Opc = Alpha::SLi; break;
1715 case ISD::SRL: Opc = Alpha::SRLi; break;
1716 case ISD::SRA: Opc = Alpha::SRAi; break;
1717 case ISD::MUL: Opc = Alpha::MULQi; break;
1718 };
1719 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001720 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001721 } else {
1722 switch(opcode) {
1723 case ISD::AND: Opc = Alpha::AND; break;
1724 case ISD::OR: Opc = Alpha::BIS; break;
1725 case ISD::XOR: Opc = Alpha::XOR; break;
1726 case ISD::SHL: Opc = Alpha::SL; break;
1727 case ISD::SRL: Opc = Alpha::SRL; break;
1728 case ISD::SRA: Opc = Alpha::SRA; break;
Jeff Cohen00b168892005-07-27 06:12:32 +00001729 case ISD::MUL:
1730 Opc = isFP ? (DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS)
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001731 : Alpha::MULQ;
1732 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001733 };
1734 Tmp1 = SelectExpr(N.getOperand(0));
1735 Tmp2 = SelectExpr(N.getOperand(1));
1736 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1737 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001738 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001739
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001740 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001741 case ISD::SUB:
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001742 if (isFP) {
1743 ConstantFPSDNode *CN;
1744 if (opcode == ISD::ADD)
1745 Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS;
1746 else
1747 Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS;
1748 if (opcode == ISD::SUB
1749 && (CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
1750 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1751 {
1752 Tmp2 = SelectExpr(N.getOperand(1));
1753 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp2).addReg(Tmp2);
1754 } else {
1755 Tmp1 = SelectExpr(N.getOperand(0));
1756 Tmp2 = SelectExpr(N.getOperand(1));
1757 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1758 }
1759 return Result;
1760 } else {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001761 bool isAdd = opcode == ISD::ADD;
1762
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001763 //first check for Scaled Adds and Subs!
1764 //Valid for add and sub
Andrew Lenharthd2284272005-08-15 14:31:37 +00001765 if(N.getOperand(0).getOpcode() == ISD::SHL &&
1766 isSIntImmediate(N.getOperand(0).getOperand(1), SImm) &&
1767 (SImm == 2 || SImm == 3)) {
1768 bool use4 = SImm == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001769 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001770 if (isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255))
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001771 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
Andrew Lenharthd2284272005-08-15 14:31:37 +00001772 2, Result).addReg(Tmp2).addImm(SImm);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001773 else {
1774 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001775 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1776 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001777 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001778 }
1779 //Position prevents subs
Andrew Lenharth273a1f92005-04-07 14:18:13 +00001780 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001781 isSIntImmediate(N.getOperand(1).getOperand(1), SImm) &&
1782 (SImm == 2 || SImm == 3)) {
1783 bool use4 = SImm == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001784 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001785 if (isSIntImmediateBounded(N.getOperand(0), SImm, 0, 255))
1786 BuildMI(BB, use4?Alpha::S4ADDQi:Alpha::S8ADDQi, 2, Result).addReg(Tmp2).addImm(SImm);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001787 else {
1788 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001789 BuildMI(BB, use4?Alpha::S4ADDQ:Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001790 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001791 }
1792 //small addi
Andrew Lenharthd2284272005-08-15 14:31:37 +00001793 else if(isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001794 { //Normal imm add/sub
1795 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1796 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001797 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001798 }
Andrew Lenharthd2284272005-08-15 14:31:37 +00001799 else if(isSIntImmediateBounded(N.getOperand(1), SImm, -255, 0))
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001800 { //inverted imm add/sub
1801 Opc = isAdd ? Alpha::SUBQi : Alpha::ADDQi;
1802 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001803 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(-SImm);
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001804 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001805 //larger addi
Andrew Lenharthd2284272005-08-15 14:31:37 +00001806 else if(isSIntImmediateBounded(N.getOperand(1), SImm, -32767, 32767))
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001807 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001808 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001809 if (!isAdd)
Andrew Lenharthd2284272005-08-15 14:31:37 +00001810 SImm = -SImm;
1811 BuildMI(BB, Alpha::LDA, 2, Result).addImm(SImm).addReg(Tmp1);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001812 }
1813 //give up and do the operation
1814 else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001815 //Normal add/sub
1816 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1817 Tmp1 = SelectExpr(N.getOperand(0));
1818 Tmp2 = SelectExpr(N.getOperand(1));
1819 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1820 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001821 return Result;
1822 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001823
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001824 case ISD::SDIV:
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001825 if (isFP) {
1826 Tmp1 = SelectExpr(N.getOperand(0));
1827 Tmp2 = SelectExpr(N.getOperand(1));
1828 BuildMI(BB, DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS, 2, Result)
1829 .addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth0cab3752005-06-29 13:35:05 +00001830 return Result;
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001831 } else {
Andrew Lenhartha565c272005-04-06 22:03:13 +00001832 //check if we can convert into a shift!
Andrew Lenharthd2284272005-08-15 14:31:37 +00001833 if (isSIntImmediate(N.getOperand(1), SImm) &&
1834 SImm != 0 && isPowerOf2_64(llabs(SImm))) {
1835 unsigned k = Log2_64(llabs(SImm));
Andrew Lenhartha565c272005-04-06 22:03:13 +00001836 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenhartha565c272005-04-06 22:03:13 +00001837 if (k == 1)
1838 Tmp2 = Tmp1;
1839 else
1840 {
1841 Tmp2 = MakeReg(MVT::i64);
1842 BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
1843 }
1844 Tmp3 = MakeReg(MVT::i64);
1845 BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
1846 unsigned Tmp4 = MakeReg(MVT::i64);
1847 BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharthd2284272005-08-15 14:31:37 +00001848 if (SImm > 0)
Andrew Lenhartha565c272005-04-06 22:03:13 +00001849 BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
1850 else
1851 {
1852 unsigned Tmp5 = MakeReg(MVT::i64);
1853 BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
1854 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
1855 }
1856 return Result;
1857 }
1858 }
1859 //Else fall through
1860
1861 case ISD::UDIV:
1862 {
Andrew Lenharthd2284272005-08-15 14:31:37 +00001863 if (isSIntImmediate(N.getOperand(1), SImm) && (SImm >= 2 || SImm <= -2))
Andrew Lenhartha565c272005-04-06 22:03:13 +00001864 {
1865 // If this is a divide by constant, we can emit code using some magic
1866 // constants to implement it as a multiply instead.
1867 ExprMap.erase(N);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001868 if (opcode == ISD::SDIV)
Andrew Lenhartha565c272005-04-06 22:03:13 +00001869 return SelectExpr(BuildSDIVSequence(N));
1870 else
1871 return SelectExpr(BuildUDIVSequence(N));
1872 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001873 }
1874 //else fall though
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001875 case ISD::UREM:
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001876 case ISD::SREM: {
1877 const char* opstr = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001878 switch(opcode) {
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001879 case ISD::UREM: opstr = "__remqu"; break;
1880 case ISD::SREM: opstr = "__remq"; break;
1881 case ISD::UDIV: opstr = "__divqu"; break;
1882 case ISD::SDIV: opstr = "__divq"; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001883 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001884 Tmp1 = SelectExpr(N.getOperand(0));
1885 Tmp2 = SelectExpr(N.getOperand(1));
Jeff Cohen00b168892005-07-27 06:12:32 +00001886 SDOperand Addr =
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001887 ISelDAG->getExternalSymbol(opstr, AlphaLowering.getPointerTy());
1888 Tmp3 = SelectExpr(Addr);
Andrew Lenharth33819132005-03-04 20:09:23 +00001889 //set up regs explicitly (helps Reg alloc)
1890 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001891 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001892 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp3).addReg(Tmp3);
1893 BuildMI(BB, Alpha::JSRs, 2, Alpha::R23).addReg(Alpha::R27).addImm(0);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001894 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001895 return Result;
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001896 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001897
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001898 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001899 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001900 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001901 assert (DestType == MVT::i64 && "only quads can be loaded to");
1902 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00001903 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001904 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001905 if (SrcType == MVT::f32)
Misha Brukman7847fca2005-04-22 17:54:37 +00001906 {
1907 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth98169be2005-07-28 18:14:47 +00001908 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Alpha::F31).addReg(Tmp1);
Misha Brukman7847fca2005-04-22 17:54:37 +00001909 Tmp1 = Tmp2;
1910 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001911 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth98169be2005-07-28 18:14:47 +00001912 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001913 MoveFP2Int(Tmp2, Result, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001914
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001915 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001916 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001917
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001918 case ISD::SELECT:
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001919 if (isFP) {
1920 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1921 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
1922 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
1923
1924 SDOperand CC = N.getOperand(0);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001925
Chris Lattner88ac32c2005-08-09 20:21:10 +00001926 if (CC.getOpcode() == ISD::SETCC &&
1927 !MVT::isInteger(CC.getOperand(0).getValueType())) {
1928 //FP Setcc -> Select yay!
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001929
Jeff Cohen00b168892005-07-27 06:12:32 +00001930
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001931 //for a cmp b: c = a - b;
1932 //a = b: c = 0
1933 //a < b: c < 0
1934 //a > b: c > 0
1935
1936 bool invTest = false;
1937 unsigned Tmp3;
1938
1939 ConstantFPSDNode *CN;
Chris Lattner88ac32c2005-08-09 20:21:10 +00001940 if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(1)))
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001941 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
Chris Lattner88ac32c2005-08-09 20:21:10 +00001942 Tmp3 = SelectExpr(CC.getOperand(0));
1943 else if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(0)))
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001944 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1945 {
Chris Lattner88ac32c2005-08-09 20:21:10 +00001946 Tmp3 = SelectExpr(CC.getOperand(1));
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001947 invTest = true;
1948 }
1949 else
1950 {
Chris Lattner88ac32c2005-08-09 20:21:10 +00001951 unsigned Tmp1 = SelectExpr(CC.getOperand(0));
1952 unsigned Tmp2 = SelectExpr(CC.getOperand(1));
1953 bool isD = CC.getOperand(0).getValueType() == MVT::f64;
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001954 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1955 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1956 .addReg(Tmp1).addReg(Tmp2);
1957 }
1958
Chris Lattner88ac32c2005-08-09 20:21:10 +00001959 switch (cast<CondCodeSDNode>(CC.getOperand(2))->get()) {
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001960 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
1961 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNE : Alpha::FCMOVEQ; break;
1962 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGT : Alpha::FCMOVLT; break;
1963 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGE : Alpha::FCMOVLE; break;
1964 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLT : Alpha::FCMOVGT; break;
1965 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLE : Alpha::FCMOVGE; break;
1966 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQ : Alpha::FCMOVNE; break;
1967 }
1968 BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
1969 return Result;
1970 }
1971 else
1972 {
1973 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1974 BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV)
1975 .addReg(Tmp1);
1976// // Spill the cond to memory and reload it from there.
1977// unsigned Tmp4 = MakeReg(MVT::f64);
1978// MoveIntFP(Tmp1, Tmp4, true);
1979// //now ideally, we don't have to do anything to the flag...
1980// // Get the condition into the zero flag.
1981// BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
1982 return Result;
Jeff Cohen00b168892005-07-27 06:12:32 +00001983 }
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001984 } else {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001985 //FIXME: look at parent to decide if intCC can be folded, or if setCC(FP)
1986 //and can save stack use
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001987 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001988 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1989 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001990 // Get the condition into the zero flag.
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001991 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001992
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001993 SDOperand CC = N.getOperand(0);
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001994
Misha Brukman4633f1c2005-04-21 23:13:11 +00001995 if (CC.getOpcode() == ISD::SETCC &&
Chris Lattner88ac32c2005-08-09 20:21:10 +00001996 !MVT::isInteger(CC.getOperand(0).getValueType()))
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001997 { //FP Setcc -> Int Select
Misha Brukman7847fca2005-04-22 17:54:37 +00001998 Tmp1 = MakeReg(MVT::f64);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001999 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2000 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Misha Brukman7847fca2005-04-22 17:54:37 +00002001 bool inv = SelectFPSetCC(CC, Tmp1);
2002 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
2003 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
2004 return Result;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002005 }
2006 if (CC.getOpcode() == ISD::SETCC) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002007 //Int SetCC -> Select
2008 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharthd2284272005-08-15 14:31:37 +00002009 if(isSIntImmediateBounded(CC.getOperand(1), SImm, 0, 0)) {
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002010 //figure out a few things
Andrew Lenharthd2284272005-08-15 14:31:37 +00002011 bool useImm = isSIntImmediateBounded(N.getOperand(2), SImm, 0, 255);
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002012
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002013 //Fix up CC
Chris Lattner88ac32c2005-08-09 20:21:10 +00002014 ISD::CondCode cCode= cast<CondCodeSDNode>(CC.getOperand(2))->get();
Andrew Lenharth694c2982005-06-26 23:01:11 +00002015 if (useImm) //Invert sense to get Imm field right
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002016 cCode = ISD::getSetCCInverse(cCode, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002017
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002018 //Choose the CMOV
2019 switch (cCode) {
2020 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002021 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
2022 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
2023 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
2024 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
2025 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
2026 case ISD::SETULT: assert(0 && "unsigned < 0 is never true"); break;
2027 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2028 //Technically you could have this CC
2029 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
2030 case ISD::SETUGE: assert(0 && "unsgined >= 0 is always true"); break;
2031 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002032 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00002033 Tmp1 = SelectExpr(CC.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002034
Andrew Lenharth694c2982005-06-26 23:01:11 +00002035 if (useImm) {
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002036 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
Andrew Lenharthd2284272005-08-15 14:31:37 +00002037 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addImm(SImm).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002038 } else {
2039 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2040 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2041 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
2042 }
2043 return Result;
2044 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002045 //Otherwise, fall though
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002046 }
2047 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002048 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2049 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002050 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3)
2051 .addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002052
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002053 return Result;
2054 }
2055
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002056 case ISD::Constant:
2057 {
Andrew Lenharthc0513832005-03-29 19:24:04 +00002058 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharth6b137d82005-07-22 22:24:01 +00002059 int zero_extend_top = 0;
Andrew Lenharthf075cac2005-07-23 07:46:48 +00002060 if (val > 0 && (val & 0xFFFFFFFF00000000ULL) == 0 &&
Andrew Lenharth6b137d82005-07-22 22:24:01 +00002061 ((int32_t)val < 0)) {
2062 //try a small load and zero extend
2063 val = (int32_t)val;
2064 zero_extend_top = 15;
2065 }
2066
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002067 if (val <= IMM_HIGH && val >= IMM_LOW) {
Andrew Lenharth6b137d82005-07-22 22:24:01 +00002068 if(!zero_extend_top)
2069 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
2070 else {
2071 Tmp1 = MakeReg(MVT::i64);
2072 BuildMI(BB, Alpha::LDA, 2, Tmp1).addImm(val).addReg(Alpha::R31);
2073 BuildMI(BB, Alpha::ZAPNOT, 2, Result).addReg(Tmp1).addImm(zero_extend_top);
2074 }
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002075 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002076 else if (val <= (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT &&
2077 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
2078 Tmp1 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002079 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val))
2080 .addReg(Alpha::R31);
Andrew Lenharth6b137d82005-07-22 22:24:01 +00002081 if (!zero_extend_top)
2082 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
2083 else {
2084 Tmp3 = MakeReg(MVT::i64);
2085 BuildMI(BB, Alpha::LDA, 2, Tmp3).addImm(getLower16(val)).addReg(Tmp1);
2086 BuildMI(BB, Alpha::ZAPNOT, 2, Result).addReg(Tmp3).addImm(zero_extend_top);
2087 }
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002088 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002089 else {
Andrew Lenharth6b137d82005-07-22 22:24:01 +00002090 //re-get the val since we are going to mem anyway
2091 val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002092 MachineConstantPool *CP = BB->getParent()->getConstantPool();
Jeff Cohen00b168892005-07-27 06:12:32 +00002093 ConstantUInt *C =
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002094 ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002095 unsigned CPI = CP->getConstantPoolIndex(C);
2096 AlphaLowering.restoreGP(BB);
Andrew Lenharthfe895e32005-06-27 17:15:36 +00002097 has_sym = true;
2098 Tmp1 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002099 BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CPI)
2100 .addReg(Alpha::R29);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00002101 if (EnableAlphaLSMark)
2102 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
2103 .addImm(getUID());
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002104 BuildMI(BB, Alpha::LDQr, 2, Result).addConstantPoolIndex(CPI)
2105 .addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002106 }
2107 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002108 }
Andrew Lenharthf4da9452005-06-29 12:49:51 +00002109 case ISD::FNEG:
2110 if(ISD::FABS == N.getOperand(0).getOpcode())
2111 {
2112 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
2113 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
2114 } else {
2115 Tmp1 = SelectExpr(N.getOperand(0));
2116 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp1).addReg(Tmp1);
2117 }
2118 return Result;
2119
2120 case ISD::FABS:
2121 Tmp1 = SelectExpr(N.getOperand(0));
2122 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
2123 return Result;
2124
2125 case ISD::FP_ROUND:
2126 assert (DestType == MVT::f32 &&
2127 N.getOperand(0).getValueType() == MVT::f64 &&
2128 "only f64 to f32 conversion supported here");
2129 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth98169be2005-07-28 18:14:47 +00002130 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00002131 return Result;
2132
2133 case ISD::FP_EXTEND:
2134 assert (DestType == MVT::f64 &&
2135 N.getOperand(0).getValueType() == MVT::f32 &&
2136 "only f32 to f64 conversion supported here");
2137 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth98169be2005-07-28 18:14:47 +00002138 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00002139 return Result;
2140
2141 case ISD::ConstantFP:
2142 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
2143 if (CN->isExactlyValue(+0.0)) {
2144 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31)
2145 .addReg(Alpha::F31);
2146 } else if ( CN->isExactlyValue(-0.0)) {
2147 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31)
2148 .addReg(Alpha::F31);
2149 } else {
2150 abort();
2151 }
2152 }
2153 return Result;
2154
2155 case ISD::SINT_TO_FP:
2156 {
2157 assert (N.getOperand(0).getValueType() == MVT::i64
2158 && "only quads can be loaded from");
2159 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
2160 Tmp2 = MakeReg(MVT::f64);
2161 MoveInt2FP(Tmp1, Tmp2, true);
2162 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
Andrew Lenharth98169be2005-07-28 18:14:47 +00002163 BuildMI(BB, Opc, 1, Result).addReg(Alpha::F31).addReg(Tmp2);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00002164 return Result;
2165 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002166 }
2167
2168 return 0;
2169}
2170
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002171void AlphaISel::Select(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002172 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00002173 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002174
Nate Begeman85fdeb22005-03-24 04:39:54 +00002175 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002176 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002177
2178 SDNode *Node = N.Val;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002179
Andrew Lenharth760270d2005-02-07 23:02:23 +00002180 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002181
2182 default:
2183 Node->dump(); std::cerr << "\n";
2184 assert(0 && "Node not handled yet!");
2185
2186 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00002187 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002188 return;
2189 }
2190
2191 case ISD::BR: {
2192 MachineBasicBlock *Dest =
2193 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2194
2195 Select(N.getOperand(0));
2196 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
2197 return;
2198 }
2199
2200 case ISD::ImplicitDef:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002201 ++count_ins;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002202 Select(N.getOperand(0));
Chris Lattner707ebc52005-08-16 21:56:37 +00002203 BuildMI(BB, Alpha::IDEF, 0,
2204 cast<RegisterSDNode>(N.getOperand(1))->getReg());
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002205 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002206
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002207 case ISD::EntryToken: return; // Noop
2208
2209 case ISD::TokenFactor:
2210 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2211 Select(Node->getOperand(i));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002212
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002213 //N.Val->dump(); std::cerr << "\n";
2214 //assert(0 && "Node not handled yet!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00002215
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002216 return;
2217
2218 case ISD::CopyToReg:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002219 ++count_outs;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002220 Select(N.getOperand(0));
Chris Lattner707ebc52005-08-16 21:56:37 +00002221 Tmp1 = SelectExpr(N.getOperand(2));
2222 Tmp2 = cast<RegisterSDNode>(N.getOperand(1))->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002223
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002224 if (Tmp1 != Tmp2) {
Chris Lattner707ebc52005-08-16 21:56:37 +00002225 if (N.getOperand(2).getValueType() == MVT::f64 ||
2226 N.getOperand(2).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00002227 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2228 else
2229 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002230 }
2231 return;
2232
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002233 case ISD::RET:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002234 ++count_outs;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002235 switch (N.getNumOperands()) {
2236 default:
2237 std::cerr << N.getNumOperands() << "\n";
2238 for (unsigned i = 0; i < N.getNumOperands(); ++i)
2239 std::cerr << N.getOperand(i).getValueType() << "\n";
2240 Node->dump();
2241 assert(0 && "Unknown return instruction!");
2242 case 2:
2243 Select(N.getOperand(0));
2244 Tmp1 = SelectExpr(N.getOperand(1));
2245 switch (N.getOperand(1).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002246 default: Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002247 assert(0 && "All other types should have been promoted!!");
2248 case MVT::f64:
2249 case MVT::f32:
2250 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
2251 break;
2252 case MVT::i32:
2253 case MVT::i64:
2254 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
2255 break;
2256 }
2257 break;
2258 case 1:
2259 Select(N.getOperand(0));
2260 break;
2261 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002262 // Just emit a 'ret' instruction
Andrew Lenharth6968bff2005-06-27 23:24:11 +00002263 AlphaLowering.restoreRA(BB);
Andrew Lenharthf3f951a2005-07-22 20:50:29 +00002264 BuildMI(BB, Alpha::RET, 2, Alpha::R31).addReg(Alpha::R26).addImm(1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002265 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002266
Misha Brukman4633f1c2005-04-21 23:13:11 +00002267 case ISD::TRUNCSTORE:
2268 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002269 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002270 SDOperand Chain = N.getOperand(0);
2271 SDOperand Value = N.getOperand(1);
2272 SDOperand Address = N.getOperand(2);
2273 Select(Chain);
2274
2275 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00002276
2277 if (opcode == ISD::STORE) {
2278 switch(Value.getValueType()) {
2279 default: assert(0 && "unknown Type in store");
2280 case MVT::i64: Opc = Alpha::STQ; break;
2281 case MVT::f64: Opc = Alpha::STT; break;
2282 case MVT::f32: Opc = Alpha::STS; break;
2283 }
2284 } else { //ISD::TRUNCSTORE
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002285 switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) {
Andrew Lenharth760270d2005-02-07 23:02:23 +00002286 default: assert(0 && "unknown Type in store");
2287 case MVT::i1: //FIXME: DAG does not promote this load
2288 case MVT::i8: Opc = Alpha::STB; break;
2289 case MVT::i16: Opc = Alpha::STW; break;
2290 case MVT::i32: Opc = Alpha::STL; break;
2291 }
Andrew Lenharth65838902005-02-06 16:22:15 +00002292 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00002293
Andrew Lenharth06ef8842005-06-29 18:54:02 +00002294 int i, j, k;
Jeff Cohen00b168892005-07-27 06:12:32 +00002295 if (EnableAlphaLSMark)
2296 getValueInfo(cast<SrcValueSDNode>(N.getOperand(3))->getValue(),
Andrew Lenharth06ef8842005-06-29 18:54:02 +00002297 i, j, k);
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002298
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00002299 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Address);
2300 if (GASD && !GASD->getGlobal()->isExternal()) {
2301 Tmp2 = MakeReg(MVT::i64);
2302 AlphaLowering.restoreGP(BB);
2303 BuildMI(BB, Alpha::LDAHr, 2, Tmp2)
2304 .addGlobalAddress(GASD->getGlobal()).addReg(Alpha::R29);
2305 if (EnableAlphaLSMark)
2306 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
2307 .addImm(getUID());
2308 BuildMI(BB, GetRelVersion(Opc), 3).addReg(Tmp1)
2309 .addGlobalAddress(GASD->getGlobal()).addReg(Tmp2);
Andrew Lenharthfce587e2005-06-29 00:39:17 +00002310 } else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00002311 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00002312 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
2313 .addImm(getUID());
Andrew Lenharth032f2352005-02-22 21:59:48 +00002314 BuildMI(BB, Opc, 3).addReg(Tmp1)
2315 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
2316 .addReg(Alpha::F31);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00002317 } else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002318 long offset;
2319 SelectAddr(Address, Tmp2, offset);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00002320 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00002321 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
2322 .addImm(getUID());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002323 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2324 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002325 return;
2326 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002327
2328 case ISD::EXTLOAD:
2329 case ISD::SEXTLOAD:
2330 case ISD::ZEXTLOAD:
2331 case ISD::LOAD:
2332 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002333 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002334 case ISD::CALL:
Andrew Lenharth032f2352005-02-22 21:59:48 +00002335 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002336 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002337 SelectExpr(N);
2338 return;
2339
Chris Lattner16cd04d2005-05-12 23:24:06 +00002340 case ISD::CALLSEQ_START:
2341 case ISD::CALLSEQ_END:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002342 Select(N.getOperand(0));
2343 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002344
Chris Lattner16cd04d2005-05-12 23:24:06 +00002345 Opc = N.getOpcode() == ISD::CALLSEQ_START ? Alpha::ADJUSTSTACKDOWN :
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002346 Alpha::ADJUSTSTACKUP;
2347 BuildMI(BB, Opc, 1).addImm(Tmp1);
2348 return;
Andrew Lenharth95762122005-03-31 21:24:06 +00002349
2350 case ISD::PCMARKER:
2351 Select(N.getOperand(0)); //Chain
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002352 BuildMI(BB, Alpha::PCLABEL, 2)
2353 .addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
Andrew Lenharth95762122005-03-31 21:24:06 +00002354 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002355 }
2356 assert(0 && "Should not be reached!");
2357}
2358
2359
2360/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
2361/// into a machine code representation using pattern matching and a machine
2362/// description file.
2363///
2364FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002365 return new AlphaISel(TM);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002366}
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002367