blob: cf5245d19c3914e1a42952e4c1c28c4f169a1400 [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:
Chris Lattner5839bf22005-08-26 17:15:30 +00001219 Tmp1 = BB->getParent()->getConstantPool()->
1220 getConstantPoolIndex(cast<ConstantPoolSDNode>(N)->get());
Andrew Lenharth02c318e2005-06-27 21:02:56 +00001221 AlphaLowering.restoreGP(BB);
1222 Tmp2 = MakeReg(MVT::i64);
1223 BuildMI(BB, Alpha::LDAHr, 2, Tmp2).addConstantPoolIndex(Tmp1)
1224 .addReg(Alpha::R29);
1225 BuildMI(BB, Alpha::LDAr, 2, Result).addConstantPoolIndex(Tmp1)
1226 .addReg(Tmp2);
1227 return Result;
Andrew Lenharth2c594352005-01-29 15:42:07 +00001228
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001229 case ISD::FrameIndex:
Andrew Lenharth032f2352005-02-22 21:59:48 +00001230 BuildMI(BB, Alpha::LDA, 2, Result)
1231 .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
1232 .addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001233 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001234
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001235 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001236 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001237 case ISD::SEXTLOAD:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001238 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001239 {
1240 // Make sure we generate both values.
1241 if (Result != notIn)
1242 ExprMap[N.getValue(1)] = notIn; // Generate the token
1243 else
1244 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001245
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001246 SDOperand Chain = N.getOperand(0);
1247 SDOperand Address = N.getOperand(1);
1248 Select(Chain);
1249
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001250 bool fpext = true;
1251
Andrew Lenharth03824012005-02-07 05:55:55 +00001252 if (opcode == ISD::LOAD)
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001253 switch (Node->getValueType(0)) {
1254 default: Node->dump(); assert(0 && "Bad load!");
1255 case MVT::i64: Opc = Alpha::LDQ; break;
1256 case MVT::f64: Opc = Alpha::LDT; break;
1257 case MVT::f32: Opc = Alpha::LDS; break;
1258 }
Andrew Lenharth03824012005-02-07 05:55:55 +00001259 else
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001260 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001261 default: Node->dump(); assert(0 && "Bad sign extend!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001262 case MVT::i32: Opc = Alpha::LDL;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001263 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001264 case MVT::i16: Opc = Alpha::LDWU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001265 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001266 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Misha Brukman4633f1c2005-04-21 23:13:11 +00001267 case MVT::i8: Opc = Alpha::LDBU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001268 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001269 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001270
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001271 int i, j, k;
1272 if (EnableAlphaLSMark)
1273 getValueInfo(dyn_cast<SrcValueSDNode>(N.getOperand(2))->getValue(),
1274 i, j, k);
1275
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001276 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Address);
1277 if (GASD && !GASD->getGlobal()->isExternal()) {
1278 Tmp1 = MakeReg(MVT::i64);
1279 AlphaLowering.restoreGP(BB);
1280 BuildMI(BB, Alpha::LDAHr, 2, Tmp1)
1281 .addGlobalAddress(GASD->getGlobal()).addReg(Alpha::R29);
1282 if (EnableAlphaLSMark)
1283 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1284 .addImm(getUID());
1285 BuildMI(BB, GetRelVersion(Opc), 2, Result)
1286 .addGlobalAddress(GASD->getGlobal()).addReg(Tmp1);
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001287 } else if (ConstantPoolSDNode *CP =
1288 dyn_cast<ConstantPoolSDNode>(Address)) {
Chris Lattner5839bf22005-08-26 17:15:30 +00001289 unsigned CPIdx = BB->getParent()->getConstantPool()->
1290 getConstantPoolIndex(CP->get());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001291 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001292 has_sym = true;
Andrew Lenharthfe895e32005-06-27 17:15:36 +00001293 Tmp1 = MakeReg(MVT::i64);
Chris Lattner5839bf22005-08-26 17:15:30 +00001294 BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CPIdx)
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001295 .addReg(Alpha::R29);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001296 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001297 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1298 .addImm(getUID());
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001299 BuildMI(BB, GetRelVersion(Opc), 2, Result)
Chris Lattner5839bf22005-08-26 17:15:30 +00001300 .addConstantPoolIndex(CPIdx).addReg(Tmp1);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001301 } else if(Address.getOpcode() == ISD::FrameIndex) {
1302 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001303 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1304 .addImm(getUID());
Andrew Lenharth032f2352005-02-22 21:59:48 +00001305 BuildMI(BB, Opc, 2, Result)
1306 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1307 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001308 } else {
1309 long offset;
1310 SelectAddr(Address, Tmp1, offset);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001311 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001312 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1313 .addImm(getUID());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001314 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1315 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001316 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001317 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001318
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001319 case ISD::GlobalAddress:
1320 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001321 has_sym = true;
Jeff Cohen00b168892005-07-27 06:12:32 +00001322
Andrew Lenharth2f5bca52005-07-03 20:06:13 +00001323 Reg = Result = MakeReg(MVT::i64);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001324
1325 if (EnableAlphaLSMark)
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001326 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001327 .addImm(getUID());
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001328
1329 BuildMI(BB, Alpha::LDQl, 2, Result)
Andrew Lenharthc95d9842005-06-27 21:11:40 +00001330 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal())
1331 .addReg(Alpha::R29);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001332 return Result;
1333
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001334 case ISD::ExternalSymbol:
1335 AlphaLowering.restoreGP(BB);
1336 has_sym = true;
1337
Andrew Lenharth2f5bca52005-07-03 20:06:13 +00001338 Reg = Result = MakeReg(MVT::i64);
1339
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001340 if (EnableAlphaLSMark)
1341 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
1342 .addImm(getUID());
1343
1344 BuildMI(BB, Alpha::LDQl, 2, Result)
1345 .addExternalSymbol(cast<ExternalSymbolSDNode>(N)->getSymbol())
1346 .addReg(Alpha::R29);
1347 return Result;
1348
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001349 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001350 case ISD::CALL:
1351 {
1352 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001353
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001354 // The chain for this call is now lowered.
Andrew Lenharthf3f951a2005-07-22 20:50:29 +00001355 ExprMap[N.getValue(Node->getNumValues()-1)] = notIn;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001356
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001357 //grab the arguments
1358 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001359 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001360 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001361 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001362
Andrew Lenharth684f2292005-01-30 00:35:27 +00001363 //in reg args
1364 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001365 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001366 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001367 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +00001368 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001369 Alpha::F19, Alpha::F20, Alpha::F21};
1370 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001371 default:
1372 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001373 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001374 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001375 N.getOperand(i+2).getValueType() << "\n";
1376 assert(0 && "Unknown value type for call");
1377 case MVT::i1:
1378 case MVT::i8:
1379 case MVT::i16:
1380 case MVT::i32:
1381 case MVT::i64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001382 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i])
1383 .addReg(argvregs[i]);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001384 break;
1385 case MVT::f32:
1386 case MVT::f64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001387 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i])
1388 .addReg(argvregs[i]);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001389 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001390 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001391 }
Andrew Lenharth684f2292005-01-30 00:35:27 +00001392 //in mem args
1393 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001394 {
1395 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001396 default:
1397 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001398 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001399 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001400 N.getOperand(i+2).getValueType() << "\n";
1401 assert(0 && "Unknown value type for call");
1402 case MVT::i1:
1403 case MVT::i8:
1404 case MVT::i16:
1405 case MVT::i32:
1406 case MVT::i64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001407 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
1408 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001409 break;
1410 case MVT::f32:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001411 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
1412 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001413 break;
1414 case MVT::f64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001415 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
1416 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001417 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001418 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001419 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001420 //build the right kind of call
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001421 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(N.getOperand(1));
1422 if (GASD && !GASD->getGlobal()->isExternal()) {
1423 //use PC relative branch call
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001424 AlphaLowering.restoreGP(BB);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001425 BuildMI(BB, Alpha::BSR, 1, Alpha::R26)
1426 .addGlobalAddress(GASD->getGlobal(),true);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001427 } else {
1428 //no need to restore GP as we are doing an indirect call
1429 Tmp1 = SelectExpr(N.getOperand(1));
1430 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
1431 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
1432 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001433
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001434 //push the result into a virtual register
Misha Brukman4633f1c2005-04-21 23:13:11 +00001435
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001436 switch (Node->getValueType(0)) {
1437 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001438 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001439 case MVT::i1:
1440 case MVT::i8:
1441 case MVT::i16:
1442 case MVT::i32:
1443 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001444 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
1445 break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001446 case MVT::f32:
1447 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001448 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
1449 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001450 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001451 return Result+N.ResNo;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001452 }
1453
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001454 case ISD::SIGN_EXTEND_INREG:
1455 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001456 //do SDIV opt for all levels of ints if not dividing by a constant
1457 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
1458 && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001459 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001460 unsigned Tmp4 = MakeReg(MVT::f64);
1461 unsigned Tmp5 = MakeReg(MVT::f64);
1462 unsigned Tmp6 = MakeReg(MVT::f64);
1463 unsigned Tmp7 = MakeReg(MVT::f64);
1464 unsigned Tmp8 = MakeReg(MVT::f64);
1465 unsigned Tmp9 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001466
1467 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1468 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1469 MoveInt2FP(Tmp1, Tmp4, true);
1470 MoveInt2FP(Tmp2, Tmp5, true);
Andrew Lenharth98169be2005-07-28 18:14:47 +00001471 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Alpha::F31).addReg(Tmp4);
1472 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Alpha::F31).addReg(Tmp5);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001473 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
Andrew Lenharth98169be2005-07-28 18:14:47 +00001474 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Alpha::F31).addReg(Tmp8);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001475 MoveFP2Int(Tmp9, Result, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001476 return Result;
1477 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001478
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001479 //Alpha has instructions for a bunch of signed 32 bit stuff
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001480 if(cast<VTSDNode>(Node->getOperand(1))->getVT() == MVT::i32) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001481 switch (N.getOperand(0).getOpcode()) {
1482 case ISD::ADD:
1483 case ISD::SUB:
1484 case ISD::MUL:
1485 {
1486 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
1487 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
1488 //FIXME: first check for Scaled Adds and Subs!
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001489 if(!isMul && N.getOperand(0).getOperand(0).getOpcode() == ISD::SHL &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001490 isSIntImmediateBounded(N.getOperand(0).getOperand(0).getOperand(1), SImm, 2, 3))
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001491 {
Andrew Lenharthd2284272005-08-15 14:31:37 +00001492 bool use4 = SImm == 2;
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001493 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1494 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1495 BuildMI(BB, isAdd?(use4?Alpha::S4ADDL:Alpha::S8ADDL):(use4?Alpha::S4SUBL:Alpha::S8SUBL),
1496 2,Result).addReg(Tmp1).addReg(Tmp2);
1497 }
1498 else if(isAdd && N.getOperand(0).getOperand(1).getOpcode() == ISD::SHL &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001499 isSIntImmediateBounded(N.getOperand(0).getOperand(1).getOperand(1), SImm, 2, 3))
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001500 {
Andrew Lenharthd2284272005-08-15 14:31:37 +00001501 bool use4 = SImm == 2;
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001502 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1503 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1504 BuildMI(BB, use4?Alpha::S4ADDL:Alpha::S8ADDL, 2,Result).addReg(Tmp1).addReg(Tmp2);
1505 }
Andrew Lenharthd2284272005-08-15 14:31:37 +00001506 else if(isSIntImmediateBounded(N.getOperand(0).getOperand(1), SImm, 0, 255))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001507 { //Normal imm add/sub
1508 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001509 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001510 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001511 }
Andrew Lenharthd2284272005-08-15 14:31:37 +00001512 else if(!isMul && isSIntImmediate(N.getOperand(0).getOperand(1), SImm) &&
1513 (((SImm << 32) >> 32) >= -255) && (((SImm << 32) >> 32) <= 0))
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001514 { //handle canonicalization
1515 Opc = isAdd ? Alpha::SUBLi : Alpha::ADDLi;
1516 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001517 SImm = 0 - ((SImm << 32) >> 32);
1518 assert(SImm >= 0 && SImm <= 255);
1519 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001520 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001521 else
1522 { //Normal add/sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001523 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULL : Alpha::SUBL);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001524 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001525 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001526 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1527 }
1528 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001529 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001530 default: break; //Fall Though;
1531 }
1532 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001533 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001534 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001535 switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001536 default:
1537 Node->dump();
1538 assert(0 && "Sign Extend InReg not there yet");
1539 break;
1540 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001541 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001542 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001543 break;
1544 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001545 case MVT::i16:
Andrew Lenharthf3f951a2005-07-22 20:50:29 +00001546 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Alpha::R31).addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001547 break;
1548 case MVT::i8:
Andrew Lenharthf3f951a2005-07-22 20:50:29 +00001549 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Alpha::R31).addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001550 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +00001551 case MVT::i1:
1552 Tmp2 = MakeReg(MVT::i64);
1553 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001554 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +00001555 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001556 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001557 return Result;
1558 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001559
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001560 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001561 {
Chris Lattner88ac32c2005-08-09 20:21:10 +00001562 ISD::CondCode CC = cast<CondCodeSDNode>(N.getOperand(2))->get();
1563 if (MVT::isInteger(N.getOperand(0).getValueType())) {
1564 bool isConst = false;
1565 int dir;
Misha Brukman7847fca2005-04-22 17:54:37 +00001566
Chris Lattner88ac32c2005-08-09 20:21:10 +00001567 //Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001568 if(isSIntImmediate(N.getOperand(1), SImm) && SImm <= 255 && SImm >= 0)
Chris Lattner88ac32c2005-08-09 20:21:10 +00001569 isConst = true;
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001570
Chris Lattner88ac32c2005-08-09 20:21:10 +00001571 switch (CC) {
1572 default: Node->dump(); assert(0 && "Unknown integer comparison!");
1573 case ISD::SETEQ:
1574 Opc = isConst ? Alpha::CMPEQi : Alpha::CMPEQ; dir=1; break;
1575 case ISD::SETLT:
1576 Opc = isConst ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
1577 case ISD::SETLE:
1578 Opc = isConst ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
1579 case ISD::SETGT: Opc = Alpha::CMPLT; dir = 2; break;
1580 case ISD::SETGE: Opc = Alpha::CMPLE; dir = 2; break;
1581 case ISD::SETULT:
1582 Opc = isConst ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
1583 case ISD::SETUGT: Opc = Alpha::CMPULT; dir = 2; break;
1584 case ISD::SETULE:
1585 Opc = isConst ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
1586 case ISD::SETUGE: Opc = Alpha::CMPULE; dir = 2; break;
1587 case ISD::SETNE: {//Handle this one special
1588 //std::cerr << "Alpha does not have a setne.\n";
1589 //abort();
1590 Tmp1 = SelectExpr(N.getOperand(0));
1591 Tmp2 = SelectExpr(N.getOperand(1));
1592 Tmp3 = MakeReg(MVT::i64);
1593 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1594 //Remeber we have the Inv for this CC
1595 CCInvMap[N] = Tmp3;
1596 //and invert
1597 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
1598 return Result;
1599 }
1600 }
1601 if (dir == 1) {
1602 Tmp1 = SelectExpr(N.getOperand(0));
1603 if (isConst) {
Andrew Lenharthd2284272005-08-15 14:31:37 +00001604 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Chris Lattner88ac32c2005-08-09 20:21:10 +00001605 } else {
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001606 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth694c2982005-06-26 23:01:11 +00001607 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001608 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001609 } else { //if (dir == 2) {
1610 Tmp1 = SelectExpr(N.getOperand(1));
1611 Tmp2 = SelectExpr(N.getOperand(0));
1612 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001613 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001614 } else {
1615 //do the comparison
1616 Tmp1 = MakeReg(MVT::f64);
1617 bool inv = SelectFPSetCC(N, Tmp1);
1618
1619 //now arrange for Result (int) to have a 1 or 0
1620 Tmp2 = MakeReg(MVT::i64);
1621 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
1622 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
1623 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
Andrew Lenharth9818c052005-02-05 13:19:12 +00001624 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001625 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001626 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001627
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001628 case ISD::CopyFromReg:
1629 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001630 ++count_ins;
1631
Andrew Lenharth40831c52005-01-28 06:57:18 +00001632 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001633 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001634 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001635 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001636 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001637
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001638 SDOperand Chain = N.getOperand(0);
1639
1640 Select(Chain);
Chris Lattner707ebc52005-08-16 21:56:37 +00001641 unsigned r = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001642 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
Andrew Lenharth619fb522005-07-04 20:07:21 +00001643 if (MVT::isFloatingPoint(N.getValue(0).getValueType()))
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001644 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
1645 else
1646 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001647 return Result;
1648 }
1649
Misha Brukman4633f1c2005-04-21 23:13:11 +00001650 //Most of the plain arithmetic and logic share the same form, and the same
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001651 //constant immediate test
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001652 case ISD::XOR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001653 //Match Not
Andrew Lenharthd2284272005-08-15 14:31:37 +00001654 if (isSIntImmediate(N.getOperand(1), SImm) && SImm == -1) {
1655 Tmp1 = SelectExpr(N.getOperand(0));
1656 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1657 return Result;
1658 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001659 //Fall through
1660 case ISD::AND:
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001661 //handle zap
Andrew Lenharthd2284272005-08-15 14:31:37 +00001662 if (opcode == ISD::AND && isUIntImmediate(N.getOperand(1), UImm))
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001663 {
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001664 unsigned int build = 0;
1665 for(int i = 0; i < 8; ++i)
1666 {
Andrew Lenharthd2284272005-08-15 14:31:37 +00001667 if ((UImm & 0x00FF) == 0x00FF)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001668 build |= 1 << i;
Andrew Lenharthd2284272005-08-15 14:31:37 +00001669 else if ((UImm & 0x00FF) != 0)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001670 { build = 0; break; }
Andrew Lenharthd2284272005-08-15 14:31:37 +00001671 UImm >>= 8;
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001672 }
1673 if (build)
1674 {
1675 Tmp1 = SelectExpr(N.getOperand(0));
1676 BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1677 return Result;
1678 }
1679 }
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001680 case ISD::OR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001681 //Check operand(0) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001682 if (N.getOperand(0).getOpcode() == ISD::XOR &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001683 isSIntImmediate(N.getOperand(0).getOperand(1), SImm) && SImm == -1) {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001684 switch(opcode) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001685 case ISD::AND: Opc = Alpha::BIC; break;
1686 case ISD::OR: Opc = Alpha::ORNOT; break;
1687 case ISD::XOR: Opc = Alpha::EQV; break;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001688 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001689 Tmp1 = SelectExpr(N.getOperand(1));
1690 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1691 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1692 return Result;
1693 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001694 //Check operand(1) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001695 if (N.getOperand(1).getOpcode() == ISD::XOR &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001696 isSIntImmediate(N.getOperand(1).getOperand(1), SImm) && SImm == -1) {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001697 switch(opcode) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001698 case ISD::AND: Opc = Alpha::BIC; break;
1699 case ISD::OR: Opc = Alpha::ORNOT; break;
1700 case ISD::XOR: Opc = Alpha::EQV; break;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001701 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001702 Tmp1 = SelectExpr(N.getOperand(0));
1703 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1704 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1705 return Result;
1706 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001707 //Fall through
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001708 case ISD::SHL:
1709 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001710 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001711 case ISD::MUL:
Andrew Lenharthd2284272005-08-15 14:31:37 +00001712 if(isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255)) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001713 switch(opcode) {
1714 case ISD::AND: Opc = Alpha::ANDi; break;
1715 case ISD::OR: Opc = Alpha::BISi; break;
1716 case ISD::XOR: Opc = Alpha::XORi; break;
1717 case ISD::SHL: Opc = Alpha::SLi; break;
1718 case ISD::SRL: Opc = Alpha::SRLi; break;
1719 case ISD::SRA: Opc = Alpha::SRAi; break;
1720 case ISD::MUL: Opc = Alpha::MULQi; break;
1721 };
1722 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001723 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001724 } else {
1725 switch(opcode) {
1726 case ISD::AND: Opc = Alpha::AND; break;
1727 case ISD::OR: Opc = Alpha::BIS; break;
1728 case ISD::XOR: Opc = Alpha::XOR; break;
1729 case ISD::SHL: Opc = Alpha::SL; break;
1730 case ISD::SRL: Opc = Alpha::SRL; break;
1731 case ISD::SRA: Opc = Alpha::SRA; break;
Jeff Cohen00b168892005-07-27 06:12:32 +00001732 case ISD::MUL:
1733 Opc = isFP ? (DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS)
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001734 : Alpha::MULQ;
1735 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001736 };
1737 Tmp1 = SelectExpr(N.getOperand(0));
1738 Tmp2 = SelectExpr(N.getOperand(1));
1739 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1740 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001741 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001742
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001743 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001744 case ISD::SUB:
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001745 if (isFP) {
1746 ConstantFPSDNode *CN;
1747 if (opcode == ISD::ADD)
1748 Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS;
1749 else
1750 Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS;
1751 if (opcode == ISD::SUB
1752 && (CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
1753 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1754 {
1755 Tmp2 = SelectExpr(N.getOperand(1));
1756 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp2).addReg(Tmp2);
1757 } else {
1758 Tmp1 = SelectExpr(N.getOperand(0));
1759 Tmp2 = SelectExpr(N.getOperand(1));
1760 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1761 }
1762 return Result;
1763 } else {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001764 bool isAdd = opcode == ISD::ADD;
1765
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001766 //first check for Scaled Adds and Subs!
1767 //Valid for add and sub
Andrew Lenharthd2284272005-08-15 14:31:37 +00001768 if(N.getOperand(0).getOpcode() == ISD::SHL &&
1769 isSIntImmediate(N.getOperand(0).getOperand(1), SImm) &&
1770 (SImm == 2 || SImm == 3)) {
1771 bool use4 = SImm == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001772 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001773 if (isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255))
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001774 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
Andrew Lenharthd2284272005-08-15 14:31:37 +00001775 2, Result).addReg(Tmp2).addImm(SImm);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001776 else {
1777 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001778 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1779 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001780 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001781 }
1782 //Position prevents subs
Andrew Lenharth273a1f92005-04-07 14:18:13 +00001783 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001784 isSIntImmediate(N.getOperand(1).getOperand(1), SImm) &&
1785 (SImm == 2 || SImm == 3)) {
1786 bool use4 = SImm == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001787 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001788 if (isSIntImmediateBounded(N.getOperand(0), SImm, 0, 255))
1789 BuildMI(BB, use4?Alpha::S4ADDQi:Alpha::S8ADDQi, 2, Result).addReg(Tmp2).addImm(SImm);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001790 else {
1791 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001792 BuildMI(BB, use4?Alpha::S4ADDQ:Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001793 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001794 }
1795 //small addi
Andrew Lenharthd2284272005-08-15 14:31:37 +00001796 else if(isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001797 { //Normal imm add/sub
1798 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1799 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001800 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001801 }
Andrew Lenharthd2284272005-08-15 14:31:37 +00001802 else if(isSIntImmediateBounded(N.getOperand(1), SImm, -255, 0))
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001803 { //inverted imm add/sub
1804 Opc = isAdd ? Alpha::SUBQi : Alpha::ADDQi;
1805 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001806 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(-SImm);
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001807 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001808 //larger addi
Andrew Lenharthd2284272005-08-15 14:31:37 +00001809 else if(isSIntImmediateBounded(N.getOperand(1), SImm, -32767, 32767))
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001810 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001811 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001812 if (!isAdd)
Andrew Lenharthd2284272005-08-15 14:31:37 +00001813 SImm = -SImm;
1814 BuildMI(BB, Alpha::LDA, 2, Result).addImm(SImm).addReg(Tmp1);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001815 }
1816 //give up and do the operation
1817 else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001818 //Normal add/sub
1819 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1820 Tmp1 = SelectExpr(N.getOperand(0));
1821 Tmp2 = SelectExpr(N.getOperand(1));
1822 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1823 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001824 return Result;
1825 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001826
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001827 case ISD::SDIV:
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001828 if (isFP) {
1829 Tmp1 = SelectExpr(N.getOperand(0));
1830 Tmp2 = SelectExpr(N.getOperand(1));
1831 BuildMI(BB, DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS, 2, Result)
1832 .addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth0cab3752005-06-29 13:35:05 +00001833 return Result;
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001834 } else {
Andrew Lenhartha565c272005-04-06 22:03:13 +00001835 //check if we can convert into a shift!
Andrew Lenharthd2284272005-08-15 14:31:37 +00001836 if (isSIntImmediate(N.getOperand(1), SImm) &&
1837 SImm != 0 && isPowerOf2_64(llabs(SImm))) {
1838 unsigned k = Log2_64(llabs(SImm));
Andrew Lenhartha565c272005-04-06 22:03:13 +00001839 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenhartha565c272005-04-06 22:03:13 +00001840 if (k == 1)
1841 Tmp2 = Tmp1;
1842 else
1843 {
1844 Tmp2 = MakeReg(MVT::i64);
1845 BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
1846 }
1847 Tmp3 = MakeReg(MVT::i64);
1848 BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
1849 unsigned Tmp4 = MakeReg(MVT::i64);
1850 BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharthd2284272005-08-15 14:31:37 +00001851 if (SImm > 0)
Andrew Lenhartha565c272005-04-06 22:03:13 +00001852 BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
1853 else
1854 {
1855 unsigned Tmp5 = MakeReg(MVT::i64);
1856 BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
1857 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
1858 }
1859 return Result;
1860 }
1861 }
1862 //Else fall through
1863
1864 case ISD::UDIV:
1865 {
Andrew Lenharthd2284272005-08-15 14:31:37 +00001866 if (isSIntImmediate(N.getOperand(1), SImm) && (SImm >= 2 || SImm <= -2))
Andrew Lenhartha565c272005-04-06 22:03:13 +00001867 {
1868 // If this is a divide by constant, we can emit code using some magic
1869 // constants to implement it as a multiply instead.
1870 ExprMap.erase(N);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001871 if (opcode == ISD::SDIV)
Andrew Lenhartha565c272005-04-06 22:03:13 +00001872 return SelectExpr(BuildSDIVSequence(N));
1873 else
1874 return SelectExpr(BuildUDIVSequence(N));
1875 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001876 }
1877 //else fall though
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001878 case ISD::UREM:
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001879 case ISD::SREM: {
1880 const char* opstr = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001881 switch(opcode) {
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001882 case ISD::UREM: opstr = "__remqu"; break;
1883 case ISD::SREM: opstr = "__remq"; break;
1884 case ISD::UDIV: opstr = "__divqu"; break;
1885 case ISD::SDIV: opstr = "__divq"; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001886 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001887 Tmp1 = SelectExpr(N.getOperand(0));
1888 Tmp2 = SelectExpr(N.getOperand(1));
Jeff Cohen00b168892005-07-27 06:12:32 +00001889 SDOperand Addr =
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001890 ISelDAG->getExternalSymbol(opstr, AlphaLowering.getPointerTy());
1891 Tmp3 = SelectExpr(Addr);
Andrew Lenharth33819132005-03-04 20:09:23 +00001892 //set up regs explicitly (helps Reg alloc)
1893 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001894 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001895 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp3).addReg(Tmp3);
1896 BuildMI(BB, Alpha::JSRs, 2, Alpha::R23).addReg(Alpha::R27).addImm(0);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001897 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001898 return Result;
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001899 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001900
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001901 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001902 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001903 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001904 assert (DestType == MVT::i64 && "only quads can be loaded to");
1905 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00001906 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001907 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001908 if (SrcType == MVT::f32)
Misha Brukman7847fca2005-04-22 17:54:37 +00001909 {
1910 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth98169be2005-07-28 18:14:47 +00001911 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Alpha::F31).addReg(Tmp1);
Misha Brukman7847fca2005-04-22 17:54:37 +00001912 Tmp1 = Tmp2;
1913 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001914 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth98169be2005-07-28 18:14:47 +00001915 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001916 MoveFP2Int(Tmp2, Result, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001917
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001918 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001919 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001920
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001921 case ISD::SELECT:
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001922 if (isFP) {
1923 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1924 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
1925 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
1926
1927 SDOperand CC = N.getOperand(0);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001928
Chris Lattner88ac32c2005-08-09 20:21:10 +00001929 if (CC.getOpcode() == ISD::SETCC &&
1930 !MVT::isInteger(CC.getOperand(0).getValueType())) {
1931 //FP Setcc -> Select yay!
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001932
Jeff Cohen00b168892005-07-27 06:12:32 +00001933
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001934 //for a cmp b: c = a - b;
1935 //a = b: c = 0
1936 //a < b: c < 0
1937 //a > b: c > 0
1938
1939 bool invTest = false;
1940 unsigned Tmp3;
1941
1942 ConstantFPSDNode *CN;
Chris Lattner88ac32c2005-08-09 20:21:10 +00001943 if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(1)))
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001944 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
Chris Lattner88ac32c2005-08-09 20:21:10 +00001945 Tmp3 = SelectExpr(CC.getOperand(0));
1946 else if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(0)))
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001947 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1948 {
Chris Lattner88ac32c2005-08-09 20:21:10 +00001949 Tmp3 = SelectExpr(CC.getOperand(1));
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001950 invTest = true;
1951 }
1952 else
1953 {
Chris Lattner88ac32c2005-08-09 20:21:10 +00001954 unsigned Tmp1 = SelectExpr(CC.getOperand(0));
1955 unsigned Tmp2 = SelectExpr(CC.getOperand(1));
1956 bool isD = CC.getOperand(0).getValueType() == MVT::f64;
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001957 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1958 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1959 .addReg(Tmp1).addReg(Tmp2);
1960 }
1961
Chris Lattner88ac32c2005-08-09 20:21:10 +00001962 switch (cast<CondCodeSDNode>(CC.getOperand(2))->get()) {
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001963 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
1964 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNE : Alpha::FCMOVEQ; break;
1965 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGT : Alpha::FCMOVLT; break;
1966 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGE : Alpha::FCMOVLE; break;
1967 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLT : Alpha::FCMOVGT; break;
1968 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLE : Alpha::FCMOVGE; break;
1969 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQ : Alpha::FCMOVNE; break;
1970 }
1971 BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
1972 return Result;
1973 }
1974 else
1975 {
1976 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1977 BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV)
1978 .addReg(Tmp1);
1979// // Spill the cond to memory and reload it from there.
1980// unsigned Tmp4 = MakeReg(MVT::f64);
1981// MoveIntFP(Tmp1, Tmp4, true);
1982// //now ideally, we don't have to do anything to the flag...
1983// // Get the condition into the zero flag.
1984// BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
1985 return Result;
Jeff Cohen00b168892005-07-27 06:12:32 +00001986 }
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001987 } else {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001988 //FIXME: look at parent to decide if intCC can be folded, or if setCC(FP)
1989 //and can save stack use
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001990 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001991 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1992 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001993 // Get the condition into the zero flag.
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001994 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001995
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001996 SDOperand CC = N.getOperand(0);
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001997
Misha Brukman4633f1c2005-04-21 23:13:11 +00001998 if (CC.getOpcode() == ISD::SETCC &&
Chris Lattner88ac32c2005-08-09 20:21:10 +00001999 !MVT::isInteger(CC.getOperand(0).getValueType()))
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002000 { //FP Setcc -> Int Select
Misha Brukman7847fca2005-04-22 17:54:37 +00002001 Tmp1 = MakeReg(MVT::f64);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002002 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2003 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Misha Brukman7847fca2005-04-22 17:54:37 +00002004 bool inv = SelectFPSetCC(CC, Tmp1);
2005 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
2006 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
2007 return Result;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002008 }
2009 if (CC.getOpcode() == ISD::SETCC) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002010 //Int SetCC -> Select
2011 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharthd2284272005-08-15 14:31:37 +00002012 if(isSIntImmediateBounded(CC.getOperand(1), SImm, 0, 0)) {
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002013 //figure out a few things
Andrew Lenharthd2284272005-08-15 14:31:37 +00002014 bool useImm = isSIntImmediateBounded(N.getOperand(2), SImm, 0, 255);
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002015
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002016 //Fix up CC
Chris Lattner88ac32c2005-08-09 20:21:10 +00002017 ISD::CondCode cCode= cast<CondCodeSDNode>(CC.getOperand(2))->get();
Andrew Lenharth694c2982005-06-26 23:01:11 +00002018 if (useImm) //Invert sense to get Imm field right
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002019 cCode = ISD::getSetCCInverse(cCode, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002020
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002021 //Choose the CMOV
2022 switch (cCode) {
2023 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002024 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
2025 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
2026 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
2027 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
2028 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
2029 case ISD::SETULT: assert(0 && "unsigned < 0 is never true"); break;
2030 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2031 //Technically you could have this CC
2032 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
2033 case ISD::SETUGE: assert(0 && "unsgined >= 0 is always true"); break;
2034 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002035 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00002036 Tmp1 = SelectExpr(CC.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002037
Andrew Lenharth694c2982005-06-26 23:01:11 +00002038 if (useImm) {
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002039 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
Andrew Lenharthd2284272005-08-15 14:31:37 +00002040 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addImm(SImm).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002041 } else {
2042 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2043 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2044 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
2045 }
2046 return Result;
2047 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002048 //Otherwise, fall though
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002049 }
2050 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002051 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2052 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002053 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3)
2054 .addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002055
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002056 return Result;
2057 }
2058
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002059 case ISD::Constant:
2060 {
Andrew Lenharthc0513832005-03-29 19:24:04 +00002061 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharth6b137d82005-07-22 22:24:01 +00002062 int zero_extend_top = 0;
Andrew Lenharthf075cac2005-07-23 07:46:48 +00002063 if (val > 0 && (val & 0xFFFFFFFF00000000ULL) == 0 &&
Andrew Lenharth6b137d82005-07-22 22:24:01 +00002064 ((int32_t)val < 0)) {
2065 //try a small load and zero extend
2066 val = (int32_t)val;
2067 zero_extend_top = 15;
2068 }
2069
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002070 if (val <= IMM_HIGH && val >= IMM_LOW) {
Andrew Lenharth6b137d82005-07-22 22:24:01 +00002071 if(!zero_extend_top)
2072 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
2073 else {
2074 Tmp1 = MakeReg(MVT::i64);
2075 BuildMI(BB, Alpha::LDA, 2, Tmp1).addImm(val).addReg(Alpha::R31);
2076 BuildMI(BB, Alpha::ZAPNOT, 2, Result).addReg(Tmp1).addImm(zero_extend_top);
2077 }
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002078 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002079 else if (val <= (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT &&
2080 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
2081 Tmp1 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002082 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val))
2083 .addReg(Alpha::R31);
Andrew Lenharth6b137d82005-07-22 22:24:01 +00002084 if (!zero_extend_top)
2085 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
2086 else {
2087 Tmp3 = MakeReg(MVT::i64);
2088 BuildMI(BB, Alpha::LDA, 2, Tmp3).addImm(getLower16(val)).addReg(Tmp1);
2089 BuildMI(BB, Alpha::ZAPNOT, 2, Result).addReg(Tmp3).addImm(zero_extend_top);
2090 }
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002091 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002092 else {
Andrew Lenharth6b137d82005-07-22 22:24:01 +00002093 //re-get the val since we are going to mem anyway
2094 val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002095 MachineConstantPool *CP = BB->getParent()->getConstantPool();
Jeff Cohen00b168892005-07-27 06:12:32 +00002096 ConstantUInt *C =
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002097 ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002098 unsigned CPI = CP->getConstantPoolIndex(C);
2099 AlphaLowering.restoreGP(BB);
Andrew Lenharthfe895e32005-06-27 17:15:36 +00002100 has_sym = true;
2101 Tmp1 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002102 BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CPI)
2103 .addReg(Alpha::R29);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00002104 if (EnableAlphaLSMark)
2105 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
2106 .addImm(getUID());
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002107 BuildMI(BB, Alpha::LDQr, 2, Result).addConstantPoolIndex(CPI)
2108 .addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002109 }
2110 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002111 }
Andrew Lenharthf4da9452005-06-29 12:49:51 +00002112 case ISD::FNEG:
2113 if(ISD::FABS == N.getOperand(0).getOpcode())
2114 {
2115 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
2116 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
2117 } else {
2118 Tmp1 = SelectExpr(N.getOperand(0));
2119 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp1).addReg(Tmp1);
2120 }
2121 return Result;
2122
2123 case ISD::FABS:
2124 Tmp1 = SelectExpr(N.getOperand(0));
2125 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
2126 return Result;
2127
2128 case ISD::FP_ROUND:
2129 assert (DestType == MVT::f32 &&
2130 N.getOperand(0).getValueType() == MVT::f64 &&
2131 "only f64 to f32 conversion supported here");
2132 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth98169be2005-07-28 18:14:47 +00002133 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00002134 return Result;
2135
2136 case ISD::FP_EXTEND:
2137 assert (DestType == MVT::f64 &&
2138 N.getOperand(0).getValueType() == MVT::f32 &&
2139 "only f32 to f64 conversion supported here");
2140 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth98169be2005-07-28 18:14:47 +00002141 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00002142 return Result;
2143
2144 case ISD::ConstantFP:
2145 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
2146 if (CN->isExactlyValue(+0.0)) {
2147 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31)
2148 .addReg(Alpha::F31);
2149 } else if ( CN->isExactlyValue(-0.0)) {
2150 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31)
2151 .addReg(Alpha::F31);
2152 } else {
2153 abort();
2154 }
2155 }
2156 return Result;
2157
2158 case ISD::SINT_TO_FP:
2159 {
2160 assert (N.getOperand(0).getValueType() == MVT::i64
2161 && "only quads can be loaded from");
2162 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
2163 Tmp2 = MakeReg(MVT::f64);
2164 MoveInt2FP(Tmp1, Tmp2, true);
2165 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
Andrew Lenharth98169be2005-07-28 18:14:47 +00002166 BuildMI(BB, Opc, 1, Result).addReg(Alpha::F31).addReg(Tmp2);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00002167 return Result;
2168 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002169 }
2170
2171 return 0;
2172}
2173
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002174void AlphaISel::Select(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002175 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00002176 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002177
Nate Begeman85fdeb22005-03-24 04:39:54 +00002178 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002179 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002180
2181 SDNode *Node = N.Val;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002182
Andrew Lenharth760270d2005-02-07 23:02:23 +00002183 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002184
2185 default:
2186 Node->dump(); std::cerr << "\n";
2187 assert(0 && "Node not handled yet!");
2188
2189 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00002190 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002191 return;
2192 }
2193
2194 case ISD::BR: {
2195 MachineBasicBlock *Dest =
2196 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2197
2198 Select(N.getOperand(0));
2199 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
2200 return;
2201 }
2202
2203 case ISD::ImplicitDef:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002204 ++count_ins;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002205 Select(N.getOperand(0));
Chris Lattner707ebc52005-08-16 21:56:37 +00002206 BuildMI(BB, Alpha::IDEF, 0,
2207 cast<RegisterSDNode>(N.getOperand(1))->getReg());
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002208 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002209
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002210 case ISD::EntryToken: return; // Noop
2211
2212 case ISD::TokenFactor:
2213 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2214 Select(Node->getOperand(i));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002215
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002216 //N.Val->dump(); std::cerr << "\n";
2217 //assert(0 && "Node not handled yet!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00002218
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002219 return;
2220
2221 case ISD::CopyToReg:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002222 ++count_outs;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002223 Select(N.getOperand(0));
Chris Lattner707ebc52005-08-16 21:56:37 +00002224 Tmp1 = SelectExpr(N.getOperand(2));
2225 Tmp2 = cast<RegisterSDNode>(N.getOperand(1))->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002226
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002227 if (Tmp1 != Tmp2) {
Chris Lattner707ebc52005-08-16 21:56:37 +00002228 if (N.getOperand(2).getValueType() == MVT::f64 ||
2229 N.getOperand(2).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00002230 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2231 else
2232 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002233 }
2234 return;
2235
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002236 case ISD::RET:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002237 ++count_outs;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002238 switch (N.getNumOperands()) {
2239 default:
2240 std::cerr << N.getNumOperands() << "\n";
2241 for (unsigned i = 0; i < N.getNumOperands(); ++i)
2242 std::cerr << N.getOperand(i).getValueType() << "\n";
2243 Node->dump();
2244 assert(0 && "Unknown return instruction!");
2245 case 2:
2246 Select(N.getOperand(0));
2247 Tmp1 = SelectExpr(N.getOperand(1));
2248 switch (N.getOperand(1).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002249 default: Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002250 assert(0 && "All other types should have been promoted!!");
2251 case MVT::f64:
2252 case MVT::f32:
2253 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
2254 break;
2255 case MVT::i32:
2256 case MVT::i64:
2257 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
2258 break;
2259 }
2260 break;
2261 case 1:
2262 Select(N.getOperand(0));
2263 break;
2264 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002265 // Just emit a 'ret' instruction
Andrew Lenharth6968bff2005-06-27 23:24:11 +00002266 AlphaLowering.restoreRA(BB);
Andrew Lenharthf3f951a2005-07-22 20:50:29 +00002267 BuildMI(BB, Alpha::RET, 2, Alpha::R31).addReg(Alpha::R26).addImm(1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002268 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002269
Misha Brukman4633f1c2005-04-21 23:13:11 +00002270 case ISD::TRUNCSTORE:
2271 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002272 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002273 SDOperand Chain = N.getOperand(0);
2274 SDOperand Value = N.getOperand(1);
2275 SDOperand Address = N.getOperand(2);
2276 Select(Chain);
2277
2278 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00002279
2280 if (opcode == ISD::STORE) {
2281 switch(Value.getValueType()) {
2282 default: assert(0 && "unknown Type in store");
2283 case MVT::i64: Opc = Alpha::STQ; break;
2284 case MVT::f64: Opc = Alpha::STT; break;
2285 case MVT::f32: Opc = Alpha::STS; break;
2286 }
2287 } else { //ISD::TRUNCSTORE
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002288 switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) {
Andrew Lenharth760270d2005-02-07 23:02:23 +00002289 default: assert(0 && "unknown Type in store");
2290 case MVT::i1: //FIXME: DAG does not promote this load
2291 case MVT::i8: Opc = Alpha::STB; break;
2292 case MVT::i16: Opc = Alpha::STW; break;
2293 case MVT::i32: Opc = Alpha::STL; break;
2294 }
Andrew Lenharth65838902005-02-06 16:22:15 +00002295 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00002296
Andrew Lenharth06ef8842005-06-29 18:54:02 +00002297 int i, j, k;
Jeff Cohen00b168892005-07-27 06:12:32 +00002298 if (EnableAlphaLSMark)
2299 getValueInfo(cast<SrcValueSDNode>(N.getOperand(3))->getValue(),
Andrew Lenharth06ef8842005-06-29 18:54:02 +00002300 i, j, k);
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002301
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00002302 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Address);
2303 if (GASD && !GASD->getGlobal()->isExternal()) {
2304 Tmp2 = MakeReg(MVT::i64);
2305 AlphaLowering.restoreGP(BB);
2306 BuildMI(BB, Alpha::LDAHr, 2, Tmp2)
2307 .addGlobalAddress(GASD->getGlobal()).addReg(Alpha::R29);
2308 if (EnableAlphaLSMark)
2309 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
2310 .addImm(getUID());
2311 BuildMI(BB, GetRelVersion(Opc), 3).addReg(Tmp1)
2312 .addGlobalAddress(GASD->getGlobal()).addReg(Tmp2);
Andrew Lenharthfce587e2005-06-29 00:39:17 +00002313 } else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00002314 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00002315 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
2316 .addImm(getUID());
Andrew Lenharth032f2352005-02-22 21:59:48 +00002317 BuildMI(BB, Opc, 3).addReg(Tmp1)
2318 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
2319 .addReg(Alpha::F31);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00002320 } else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002321 long offset;
2322 SelectAddr(Address, Tmp2, offset);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00002323 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00002324 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
2325 .addImm(getUID());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002326 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2327 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002328 return;
2329 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002330
2331 case ISD::EXTLOAD:
2332 case ISD::SEXTLOAD:
2333 case ISD::ZEXTLOAD:
2334 case ISD::LOAD:
2335 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002336 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002337 case ISD::CALL:
Andrew Lenharth032f2352005-02-22 21:59:48 +00002338 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002339 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002340 SelectExpr(N);
2341 return;
2342
Chris Lattner16cd04d2005-05-12 23:24:06 +00002343 case ISD::CALLSEQ_START:
2344 case ISD::CALLSEQ_END:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002345 Select(N.getOperand(0));
2346 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002347
Chris Lattner16cd04d2005-05-12 23:24:06 +00002348 Opc = N.getOpcode() == ISD::CALLSEQ_START ? Alpha::ADJUSTSTACKDOWN :
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002349 Alpha::ADJUSTSTACKUP;
2350 BuildMI(BB, Opc, 1).addImm(Tmp1);
2351 return;
Andrew Lenharth95762122005-03-31 21:24:06 +00002352
2353 case ISD::PCMARKER:
2354 Select(N.getOperand(0)); //Chain
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002355 BuildMI(BB, Alpha::PCLABEL, 2)
2356 .addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
Andrew Lenharth95762122005-03-31 21:24:06 +00002357 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002358 }
2359 assert(0 && "Should not be reached!");
2360}
2361
2362
2363/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
2364/// into a machine code representation using pattern matching and a machine
2365/// description file.
2366///
2367FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002368 return new AlphaISel(TM);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002369}
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002370