blob: c0ae2acf97ce7012edacef8567ecf14a54162fd6 [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));
286 argt = DAG.getCopyFromReg(args_float[count], VT, DAG.getRoot());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000287 break;
288 case MVT::i1:
289 case MVT::i8:
290 case MVT::i16:
291 case MVT::i32:
292 case MVT::i64:
Jeff Cohen00b168892005-07-27 06:12:32 +0000293 args_int[count] = AddLiveIn(MF, args_int[count],
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000294 getRegClassFor(MVT::i64));
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000295 argt = DAG.getCopyFromReg(args_int[count], VT, DAG.getRoot());
Andrew Lenharth14f30c92005-05-31 18:37:16 +0000296 if (VT != MVT::i64)
297 argt = DAG.getNode(ISD::TRUNCATE, VT, argt);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000298 break;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000299 }
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000300 DAG.setRoot(argt.getValue(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000301 } else { //more args
302 // Create the frame index object for this incoming parameter...
303 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000304
305 // Create the SelectionDAG nodes corresponding to a load
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000306 //from this parameter
307 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000308 argt = DAG.getLoad(getValueType(I->getType()),
309 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000310 }
Andrew Lenharth032f2352005-02-22 21:59:48 +0000311 ++count;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000312 ArgValues.push_back(argt);
313 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000314
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000315 // If the functions takes variable number of arguments, copy all regs to stack
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000316 if (F.isVarArg()) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000317 VarArgsOffset = count * 8;
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000318 std::vector<SDOperand> LS;
319 for (int i = 0; i < 6; ++i) {
320 if (args_int[i] < 1024)
321 args_int[i] = AddLiveIn(MF,args_int[i], getRegClassFor(MVT::i64));
322 SDOperand argt = DAG.getCopyFromReg(args_int[i], MVT::i64, DAG.getRoot());
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000323 int FI = MFI->CreateFixedObject(8, -8 * (6 - i));
Andrew Lenharth558bc882005-06-18 18:34:52 +0000324 if (i == 0) VarArgsBase = FI;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000325 SDOperand SDFI = DAG.getFrameIndex(FI, MVT::i64);
Jeff Cohen00b168892005-07-27 06:12:32 +0000326 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt,
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000327 SDFI, DAG.getSrcValue(NULL)));
Jeff Cohen00b168892005-07-27 06:12:32 +0000328
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000329 if (args_float[i] < 1024)
330 args_float[i] = AddLiveIn(MF,args_float[i], getRegClassFor(MVT::f64));
331 argt = DAG.getCopyFromReg(args_float[i], MVT::f64, DAG.getRoot());
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000332 FI = MFI->CreateFixedObject(8, - 8 * (12 - i));
333 SDFI = DAG.getFrameIndex(FI, MVT::i64);
Jeff Cohen00b168892005-07-27 06:12:32 +0000334 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt,
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000335 SDFI, DAG.getSrcValue(NULL)));
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000336 }
337
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000338 //Set up a token factor with all the stack traffic
339 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, LS));
340 }
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000341
342 // Finally, inform the code generator which regs we return values in.
343 switch (getValueType(F.getReturnType())) {
344 default: assert(0 && "Unknown type!");
345 case MVT::isVoid: break;
346 case MVT::i1:
347 case MVT::i8:
348 case MVT::i16:
349 case MVT::i32:
350 case MVT::i64:
351 MF.addLiveOut(Alpha::R0);
352 break;
353 case MVT::f32:
354 case MVT::f64:
355 MF.addLiveOut(Alpha::F0);
356 break;
357 }
358
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000359 //return the arguments
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000360 return ArgValues;
361}
362
363std::pair<SDOperand, SDOperand>
364AlphaTargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000365 const Type *RetTy, bool isVarArg,
Chris Lattneradf6a962005-05-13 18:50:42 +0000366 unsigned CallingConv, bool isTailCall,
Jeff Cohen00b168892005-07-27 06:12:32 +0000367 SDOperand Callee, ArgListTy &Args,
Misha Brukman7847fca2005-04-22 17:54:37 +0000368 SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000369 int NumBytes = 0;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000370 if (Args.size() > 6)
371 NumBytes = (Args.size() - 6) * 8;
372
Chris Lattner16cd04d2005-05-12 23:24:06 +0000373 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000374 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000375 std::vector<SDOperand> args_to_use;
376 for (unsigned i = 0, e = Args.size(); i != e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000377 {
378 switch (getValueType(Args[i].second)) {
379 default: assert(0 && "Unexpected ValueType for argument!");
380 case MVT::i1:
381 case MVT::i8:
382 case MVT::i16:
383 case MVT::i32:
384 // Promote the integer to 64 bits. If the input type is signed use a
385 // sign extend, otherwise use a zero extend.
386 if (Args[i].second->isSigned())
387 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
388 else
389 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
390 break;
391 case MVT::i64:
392 case MVT::f64:
393 case MVT::f32:
394 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000395 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000396 args_to_use.push_back(Args[i].first);
397 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000398
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000399 std::vector<MVT::ValueType> RetVals;
400 MVT::ValueType RetTyVT = getValueType(RetTy);
401 if (RetTyVT != MVT::isVoid)
402 RetVals.push_back(RetTyVT);
403 RetVals.push_back(MVT::Other);
404
Misha Brukman4633f1c2005-04-21 23:13:11 +0000405 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000406 Chain, Callee, args_to_use), 0);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000407 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner16cd04d2005-05-12 23:24:06 +0000408 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000409 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000410 return std::make_pair(TheCall, Chain);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000411}
412
Chris Lattnere0fe2252005-07-05 19:58:54 +0000413SDOperand AlphaTargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP,
414 Value *VAListV, SelectionDAG &DAG) {
415 // vastart stores the address of the VarArgsBase and VarArgsOffset
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000416 SDOperand FR = DAG.getFrameIndex(VarArgsBase, MVT::i64);
Jeff Cohen00b168892005-07-27 06:12:32 +0000417 SDOperand S1 = DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP,
Chris Lattnere0fe2252005-07-05 19:58:54 +0000418 DAG.getSrcValue(VAListV));
Jeff Cohen00b168892005-07-27 06:12:32 +0000419 SDOperand SA2 = DAG.getNode(ISD::ADD, MVT::i64, VAListP,
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000420 DAG.getConstant(8, MVT::i64));
Jeff Cohen00b168892005-07-27 06:12:32 +0000421 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, S1,
422 DAG.getConstant(VarArgsOffset, MVT::i64), SA2,
Chris Lattner9fadb4c2005-07-10 00:29:18 +0000423 DAG.getSrcValue(VAListV, 8), DAG.getValueType(MVT::i32));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000424}
425
426std::pair<SDOperand,SDOperand> AlphaTargetLowering::
Chris Lattnere0fe2252005-07-05 19:58:54 +0000427LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
428 const Type *ArgTy, SelectionDAG &DAG) {
429 SDOperand Base = DAG.getLoad(MVT::i64, Chain, VAListP,
430 DAG.getSrcValue(VAListV));
Jeff Cohen00b168892005-07-27 06:12:32 +0000431 SDOperand Tmp = DAG.getNode(ISD::ADD, MVT::i64, VAListP,
Andrew Lenharth558bc882005-06-18 18:34:52 +0000432 DAG.getConstant(8, MVT::i64));
Jeff Cohen00b168892005-07-27 06:12:32 +0000433 SDOperand Offset = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Base.getValue(1),
Chris Lattnerbce81ae2005-07-10 01:56:13 +0000434 Tmp, DAG.getSrcValue(VAListV, 8), MVT::i32);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000435 SDOperand DataPtr = DAG.getNode(ISD::ADD, MVT::i64, Base, Offset);
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000436 if (ArgTy->isFloatingPoint())
437 {
438 //if fp && Offset < 6*8, then subtract 6*8 from DataPtr
439 SDOperand FPDataPtr = DAG.getNode(ISD::SUB, MVT::i64, DataPtr,
Chris Lattnere0fe2252005-07-05 19:58:54 +0000440 DAG.getConstant(8*6, MVT::i64));
Chris Lattner88ac32c2005-08-09 20:21:10 +0000441 SDOperand CC = DAG.getSetCC(MVT::i64, Offset,
442 DAG.getConstant(8*6, MVT::i64), ISD::SETLT);
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000443 DataPtr = DAG.getNode(ISD::SELECT, MVT::i64, CC, FPDataPtr, DataPtr);
444 }
445
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000446 SDOperand Result;
447 if (ArgTy == Type::IntTy)
Chris Lattnerbce81ae2005-07-10 01:56:13 +0000448 Result = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Offset.getValue(1),
449 DataPtr, DAG.getSrcValue(NULL), MVT::i32);
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000450 else if (ArgTy == Type::UIntTy)
Chris Lattnerbce81ae2005-07-10 01:56:13 +0000451 Result = DAG.getExtLoad(ISD::ZEXTLOAD, MVT::i64, Offset.getValue(1),
452 DataPtr, DAG.getSrcValue(NULL), MVT::i32);
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000453 else
Jeff Cohen00b168892005-07-27 06:12:32 +0000454 Result = DAG.getLoad(getValueType(ArgTy), Offset.getValue(1), DataPtr,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000455 DAG.getSrcValue(NULL));
456
Jeff Cohen00b168892005-07-27 06:12:32 +0000457 SDOperand NewOffset = DAG.getNode(ISD::ADD, MVT::i64, Offset,
Andrew Lenharth558bc882005-06-18 18:34:52 +0000458 DAG.getConstant(8, MVT::i64));
Jeff Cohen00b168892005-07-27 06:12:32 +0000459 SDOperand Update = DAG.getNode(ISD::TRUNCSTORE, MVT::Other,
460 Result.getValue(1), NewOffset,
Chris Lattner9fadb4c2005-07-10 00:29:18 +0000461 Tmp, DAG.getSrcValue(VAListV, 8),
462 DAG.getValueType(MVT::i32));
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000463 Result = DAG.getNode(ISD::TRUNCATE, getValueType(ArgTy), Result);
464
Andrew Lenharth558bc882005-06-18 18:34:52 +0000465 return std::make_pair(Result, Update);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000466}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000467
Chris Lattnere0fe2252005-07-05 19:58:54 +0000468
469SDOperand AlphaTargetLowering::
470LowerVACopy(SDOperand Chain, SDOperand SrcP, Value *SrcV, SDOperand DestP,
471 Value *DestV, SelectionDAG &DAG) {
Jeff Cohen00b168892005-07-27 06:12:32 +0000472 SDOperand Val = DAG.getLoad(getPointerTy(), Chain, SrcP,
Chris Lattnere0fe2252005-07-05 19:58:54 +0000473 DAG.getSrcValue(SrcV));
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000474 SDOperand Result = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
Chris Lattnere0fe2252005-07-05 19:58:54 +0000475 Val, DestP, DAG.getSrcValue(DestV));
Jeff Cohen00b168892005-07-27 06:12:32 +0000476 SDOperand NP = DAG.getNode(ISD::ADD, MVT::i64, SrcP,
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000477 DAG.getConstant(8, MVT::i64));
Chris Lattnerbce81ae2005-07-10 01:56:13 +0000478 Val = DAG.getExtLoad(ISD::SEXTLOAD, MVT::i64, Result, NP,
479 DAG.getSrcValue(SrcV, 8), MVT::i32);
Jeff Cohen00b168892005-07-27 06:12:32 +0000480 SDOperand NPD = DAG.getNode(ISD::ADD, MVT::i64, DestP,
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000481 DAG.getConstant(8, MVT::i64));
Chris Lattnere0fe2252005-07-05 19:58:54 +0000482 return DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Val.getValue(1),
Chris Lattner9fadb4c2005-07-10 00:29:18 +0000483 Val, NPD, DAG.getSrcValue(DestV, 8),
484 DAG.getValueType(MVT::i32));
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000485}
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000486
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000487namespace {
488
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000489//===--------------------------------------------------------------------===//
490/// ISel - Alpha specific code to select Alpha machine instructions for
491/// SelectionDAG operations.
492//===--------------------------------------------------------------------===//
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000493class AlphaISel : public SelectionDAGISel {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000494
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000495 /// AlphaLowering - This object fully describes how to lower LLVM code to an
496 /// Alpha-specific SelectionDAG.
497 AlphaTargetLowering AlphaLowering;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000498
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000499 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
500 // for sdiv and udiv until it is put into the future
501 // dag combiner.
502
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000503 /// ExprMap - As shared expressions are codegen'd, we keep track of which
504 /// vreg the value is produced in, so we only emit one copy of each compiled
505 /// tree.
506 static const unsigned notIn = (unsigned)(-1);
507 std::map<SDOperand, unsigned> ExprMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000508
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000509 //CCInvMap sometimes (SetNE) we have the inverse CC code for free
510 std::map<SDOperand, unsigned> CCInvMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000511
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000512 int count_ins;
513 int count_outs;
514 bool has_sym;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000515 int max_depth;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000516
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000517public:
Jeff Cohen00b168892005-07-27 06:12:32 +0000518 AlphaISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering),
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000519 AlphaLowering(TM)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000520 {}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000521
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000522 /// InstructionSelectBasicBlock - This callback is invoked by
523 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
524 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
Andrew Lenharth032f2352005-02-22 21:59:48 +0000525 DEBUG(BB->dump());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000526 count_ins = 0;
527 count_outs = 0;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000528 max_depth = 0;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000529 has_sym = false;
530
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000531 // Codegen the basic block.
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000532 ISelDAG = &DAG;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000533 max_depth = DAG.getRoot().getNodeDepth();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000534 Select(DAG.getRoot());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000535
536 if(has_sym)
537 ++count_ins;
538 if(EnableAlphaCount)
Jeff Cohen00b168892005-07-27 06:12:32 +0000539 std::cerr << "COUNT: "
540 << BB->getParent()->getFunction ()->getName() << " "
541 << BB->getNumber() << " "
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000542 << max_depth << " "
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000543 << count_ins << " "
544 << count_outs << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000545
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000546 // Clear state used for selection.
547 ExprMap.clear();
548 CCInvMap.clear();
549 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000550
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000551 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000552
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000553 unsigned SelectExpr(SDOperand N);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000554 void Select(SDOperand N);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000555
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000556 void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
557 void SelectBranchCC(SDOperand N);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000558 void MoveFP2Int(unsigned src, unsigned dst, bool isDouble);
559 void MoveInt2FP(unsigned src, unsigned dst, bool isDouble);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000560 //returns whether the sense of the comparison was inverted
561 bool SelectFPSetCC(SDOperand N, unsigned dst);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000562
563 // dag -> dag expanders for integer divide by constant
564 SDOperand BuildSDIVSequence(SDOperand N);
565 SDOperand BuildUDIVSequence(SDOperand N);
566
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000567};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000568}
569
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000570void AlphaISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000571 // If this function has live-in values, emit the copies from pregs to vregs at
572 // the top of the function, before anything else.
573 MachineBasicBlock *BB = MF.begin();
574 if (MF.livein_begin() != MF.livein_end()) {
575 SSARegMap *RegMap = MF.getSSARegMap();
576 for (MachineFunction::livein_iterator LI = MF.livein_begin(),
577 E = MF.livein_end(); LI != E; ++LI) {
578 const TargetRegisterClass *RC = RegMap->getRegClass(LI->second);
579 if (RC == Alpha::GPRCRegisterClass) {
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000580 BuildMI(BB, Alpha::BIS, 2, LI->second).addReg(LI->first)
581 .addReg(LI->first);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000582 } else if (RC == Alpha::FPRCRegisterClass) {
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000583 BuildMI(BB, Alpha::CPYS, 2, LI->second).addReg(LI->first)
584 .addReg(LI->first);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000585 } else {
586 assert(0 && "Unknown regclass!");
587 }
588 }
589 }
590}
591
Andrew Lenharthd2284272005-08-15 14:31:37 +0000592static bool isSIntImmediate(SDOperand N, int64_t& Imm) {
593 // test for constant
594 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
595 // retrieve value
596 Imm = CN->getSignExtended();
597 // passes muster
598 return true;
599 }
600 // not a constant
601 return false;
602}
603
604// isSIntImmediateBounded - This method tests to see if a constant operand
605// bounded s.t. low <= Imm <= high
606// If so Imm will receive the 64 bit value.
607static bool isSIntImmediateBounded(SDOperand N, int64_t& Imm,
608 int64_t low, int64_t high) {
609 if (isSIntImmediate(N, Imm) && Imm <= high && Imm >= high)
610 return true;
611 return false;
612}
613static bool isUIntImmediate(SDOperand N, uint64_t& Imm) {
614 // test for constant
615 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
616 // retrieve value
617 Imm = (uint64_t)CN->getValue();
618 // passes muster
619 return true;
620 }
621 // not a constant
622 return false;
623}
624
625static bool isUIntImmediateBounded(SDOperand N, uint64_t& Imm,
626 uint64_t low, uint64_t high) {
627 if (isUIntImmediate(N, Imm) && Imm <= high && Imm >= high)
628 return true;
629 return false;
630}
631
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000632static void getValueInfo(const Value* v, int& type, int& fun, int& offset)
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000633{
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000634 fun = type = offset = 0;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000635 if (v == NULL) {
636 type = 0;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000637 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(v)) {
638 type = 1;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000639 const Module* M = GV->getParent();
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000640 for(Module::const_global_iterator ii = M->global_begin(); &*ii != GV; ++ii)
641 ++offset;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000642 } else if (const Argument* Arg = dyn_cast<Argument>(v)) {
643 type = 2;
644 const Function* F = Arg->getParent();
645 const Module* M = F->getParent();
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000646 for(Module::const_iterator ii = M->begin(); &*ii != F; ++ii)
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000647 ++fun;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000648 for(Function::const_arg_iterator ii = F->arg_begin(); &*ii != Arg; ++ii)
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000649 ++offset;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000650 } else if (const Instruction* I = dyn_cast<Instruction>(v)) {
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +0000651 assert(dyn_cast<PointerType>(I->getType()));
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000652 type = 3;
653 const BasicBlock* bb = I->getParent();
654 const Function* F = bb->getParent();
655 const Module* M = F->getParent();
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000656 for(Module::const_iterator ii = M->begin(); &*ii != F; ++ii)
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000657 ++fun;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000658 for(Function::const_iterator ii = F->begin(); &*ii != bb; ++ii)
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000659 offset += ii->size();
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000660 for(BasicBlock::const_iterator ii = bb->begin(); &*ii != I; ++ii)
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000661 ++offset;
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +0000662 } else if (const Constant* C = dyn_cast<Constant>(v)) {
663 //Don't know how to look these up yet
664 type = 0;
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +0000665 } else {
666 assert(0 && "Error in value marking");
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000667 }
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000668 //type = 4: register spilling
669 //type = 5: global address loading or constant loading
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000670}
671
672static int getUID()
673{
674 static int id = 0;
675 return ++id;
676}
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000677
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000678//Factorize a number using the list of constants
679static bool factorize(int v[], int res[], int size, uint64_t c)
680{
681 bool cont = true;
682 while (c != 1 && cont)
683 {
684 cont = false;
685 for(int i = 0; i < size; ++i)
686 {
687 if (c % v[i] == 0)
688 {
689 c /= v[i];
690 ++res[i];
691 cont=true;
692 }
693 }
694 }
695 return c == 1;
696}
697
698
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000699//Shamelessly adapted from PPC32
Misha Brukman4633f1c2005-04-21 23:13:11 +0000700// Structure used to return the necessary information to codegen an SDIV as
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000701// a multiply.
702struct ms {
703 int64_t m; // magic number
704 int64_t s; // shift amount
705};
706
707struct mu {
708 uint64_t m; // magic number
709 int64_t a; // add indicator
710 int64_t s; // shift amount
711};
712
713/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukman4633f1c2005-04-21 23:13:11 +0000714/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000715/// or -1.
716static struct ms magic(int64_t d) {
717 int64_t p;
718 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
719 const uint64_t two63 = 9223372036854775808ULL; // 2^63
720 struct ms mag;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000721
Andrew Lenharth01c8f6e2005-08-01 17:47:28 +0000722 ad = llabs(d);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000723 t = two63 + ((uint64_t)d >> 63);
724 anc = t - 1 - t%ad; // absolute value of nc
Andrew Lenharth320174f2005-04-07 17:19:16 +0000725 p = 63; // initialize p
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000726 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
727 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
728 q2 = two63/ad; // initialize q2 = 2p/abs(d)
729 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
730 do {
731 p = p + 1;
732 q1 = 2*q1; // update q1 = 2p/abs(nc)
733 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
734 if (r1 >= anc) { // must be unsigned comparison
735 q1 = q1 + 1;
736 r1 = r1 - anc;
737 }
738 q2 = 2*q2; // update q2 = 2p/abs(d)
739 r2 = 2*r2; // update r2 = rem(2p/abs(d))
740 if (r2 >= ad) { // must be unsigned comparison
741 q2 = q2 + 1;
742 r2 = r2 - ad;
743 }
744 delta = ad - r2;
745 } while (q1 < delta || (q1 == delta && r1 == 0));
746
747 mag.m = q2 + 1;
748 if (d < 0) mag.m = -mag.m; // resulting magic number
749 mag.s = p - 64; // resulting shift
750 return mag;
751}
752
753/// magicu - calculate the magic numbers required to codegen an integer udiv as
754/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
755static struct mu magicu(uint64_t d)
756{
757 int64_t p;
758 uint64_t nc, delta, q1, r1, q2, r2;
759 struct mu magu;
760 magu.a = 0; // initialize "add" indicator
761 nc = - 1 - (-d)%d;
Andrew Lenharth320174f2005-04-07 17:19:16 +0000762 p = 63; // initialize p
763 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
764 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
765 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
766 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000767 do {
768 p = p + 1;
769 if (r1 >= nc - r1 ) {
770 q1 = 2*q1 + 1; // update q1
771 r1 = 2*r1 - nc; // update r1
772 }
773 else {
774 q1 = 2*q1; // update q1
775 r1 = 2*r1; // update r1
776 }
777 if (r2 + 1 >= d - r2) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000778 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000779 q2 = 2*q2 + 1; // update q2
780 r2 = 2*r2 + 1 - d; // update r2
781 }
782 else {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000783 if (q2 >= 0x8000000000000000ull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000784 q2 = 2*q2; // update q2
785 r2 = 2*r2 + 1; // update r2
786 }
787 delta = d - 1 - r2;
788 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
789 magu.m = q2 + 1; // resulting magic number
Andrew Lenharth320174f2005-04-07 17:19:16 +0000790 magu.s = p - 64; // resulting shift
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000791 return magu;
792}
793
794/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
795/// return a DAG expression to select that will generate the same value by
796/// multiplying by a magic number. See:
797/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000798SDOperand AlphaISel::BuildSDIVSequence(SDOperand N) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000799 int64_t d = (int64_t)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000800 ms magics = magic(d);
801 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000802 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000803 ISelDAG->getConstant(magics.m, MVT::i64));
804 // If d > 0 and m < 0, add the numerator
805 if (d > 0 && magics.m < 0)
806 Q = ISelDAG->getNode(ISD::ADD, MVT::i64, Q, N.getOperand(0));
807 // If d < 0 and m > 0, subtract the numerator.
808 if (d < 0 && magics.m > 0)
809 Q = ISelDAG->getNode(ISD::SUB, MVT::i64, Q, N.getOperand(0));
810 // Shift right algebraic if shift value is nonzero
811 if (magics.s > 0)
Misha Brukman4633f1c2005-04-21 23:13:11 +0000812 Q = ISelDAG->getNode(ISD::SRA, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000813 ISelDAG->getConstant(magics.s, MVT::i64));
814 // Extract the sign bit and add it to the quotient
Misha Brukman4633f1c2005-04-21 23:13:11 +0000815 SDOperand T =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000816 ISelDAG->getNode(ISD::SRL, MVT::i64, Q, ISelDAG->getConstant(63, MVT::i64));
817 return ISelDAG->getNode(ISD::ADD, MVT::i64, Q, T);
818}
819
820/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
821/// return a DAG expression to select that will generate the same value by
822/// multiplying by a magic number. See:
823/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000824SDOperand AlphaISel::BuildUDIVSequence(SDOperand N) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000825 unsigned d =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000826 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
827 mu magics = magicu(d);
828 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000829 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000830 ISelDAG->getConstant(magics.m, MVT::i64));
831 if (magics.a == 0) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000832 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000833 ISelDAG->getConstant(magics.s, MVT::i64));
834 } else {
835 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i64, N.getOperand(0), Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000836 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000837 ISelDAG->getConstant(1, MVT::i64));
838 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i64, NPQ, Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000839 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000840 ISelDAG->getConstant(magics.s-1, MVT::i64));
841 }
842 return Q;
843}
844
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000845//These describe LDAx
Andrew Lenharthc0513832005-03-29 19:24:04 +0000846static const int IMM_LOW = -32768;
847static const int IMM_HIGH = 32767;
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000848static const int IMM_MULT = 65536;
849
850static long getUpper16(long l)
851{
852 long y = l / IMM_MULT;
853 if (l % IMM_MULT > IMM_HIGH)
854 ++y;
855 return y;
856}
857
858static long getLower16(long l)
859{
860 long h = getUpper16(l);
861 return l - h * IMM_MULT;
862}
863
Andrew Lenharthfe895e32005-06-27 17:15:36 +0000864static unsigned GetRelVersion(unsigned opcode)
865{
866 switch (opcode) {
867 default: assert(0 && "unknown load or store"); return 0;
868 case Alpha::LDQ: return Alpha::LDQr;
869 case Alpha::LDS: return Alpha::LDSr;
870 case Alpha::LDT: return Alpha::LDTr;
871 case Alpha::LDL: return Alpha::LDLr;
872 case Alpha::LDBU: return Alpha::LDBUr;
873 case Alpha::LDWU: return Alpha::LDWUr;
Andrew Lenharthfce587e2005-06-29 00:39:17 +0000874 case Alpha::STB: return Alpha::STBr;
875 case Alpha::STW: return Alpha::STWr;
876 case Alpha::STL: return Alpha::STLr;
877 case Alpha::STQ: return Alpha::STQr;
878 case Alpha::STS: return Alpha::STSr;
879 case Alpha::STT: return Alpha::STTr;
880
Andrew Lenharthfe895e32005-06-27 17:15:36 +0000881 }
882}
Andrew Lenharth65838902005-02-06 16:22:15 +0000883
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000884void AlphaISel::MoveFP2Int(unsigned src, unsigned dst, bool isDouble)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000885{
886 unsigned Opc;
887 if (EnableAlphaFTOI) {
888 Opc = isDouble ? Alpha::FTOIT : Alpha::FTOIS;
Andrew Lenharth98169be2005-07-28 18:14:47 +0000889 BuildMI(BB, Opc, 1, dst).addReg(src).addReg(Alpha::F31);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000890 } else {
891 //The hard way:
892 // Spill the integer to memory and reload it from there.
893 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
894 MachineFunction *F = BB->getParent();
895 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
896
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000897 if (EnableAlphaLSMark)
898 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
899 .addImm(getUID());
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000900 Opc = isDouble ? Alpha::STT : Alpha::STS;
901 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000902
903 if (EnableAlphaLSMark)
904 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
905 .addImm(getUID());
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000906 Opc = isDouble ? Alpha::LDQ : Alpha::LDL;
907 BuildMI(BB, Alpha::LDQ, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
908 }
909}
910
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000911void AlphaISel::MoveInt2FP(unsigned src, unsigned dst, bool isDouble)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000912{
913 unsigned Opc;
914 if (EnableAlphaFTOI) {
915 Opc = isDouble?Alpha::ITOFT:Alpha::ITOFS;
Andrew Lenharth98169be2005-07-28 18:14:47 +0000916 BuildMI(BB, Opc, 1, dst).addReg(src).addReg(Alpha::R31);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000917 } else {
918 //The hard way:
919 // Spill the integer to memory and reload it from there.
920 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
921 MachineFunction *F = BB->getParent();
922 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
923
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000924 if (EnableAlphaLSMark)
925 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
926 .addImm(getUID());
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000927 Opc = isDouble ? Alpha::STQ : Alpha::STL;
928 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000929
930 if (EnableAlphaLSMark)
931 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
932 .addImm(getUID());
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000933 Opc = isDouble ? Alpha::LDT : Alpha::LDS;
934 BuildMI(BB, Opc, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
935 }
936}
937
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000938bool AlphaISel::SelectFPSetCC(SDOperand N, unsigned dst)
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000939{
Chris Lattner88ac32c2005-08-09 20:21:10 +0000940 SDNode *SetCC = N.Val;
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000941 unsigned Opc, Tmp1, Tmp2, Tmp3;
Chris Lattner88ac32c2005-08-09 20:21:10 +0000942 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC->getOperand(2))->get();
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000943 bool rev = false;
944 bool inv = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000945
Chris Lattner88ac32c2005-08-09 20:21:10 +0000946 switch (CC) {
947 default: SetCC->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000948 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
949 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
950 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
951 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
952 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
953 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
954 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000955
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000956 ConstantFPSDNode *CN;
957 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
958 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
959 Tmp1 = Alpha::F31;
960 else
961 Tmp1 = SelectExpr(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000962
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000963 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
964 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
965 Tmp2 = Alpha::F31;
966 else
Chris Lattner9c9183a2005-04-30 04:44:07 +0000967 Tmp2 = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000968
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000969 //Can only compare doubles, and dag won't promote for me
970 if (SetCC->getOperand(0).getValueType() == MVT::f32)
971 {
972 //assert(0 && "Setcc On float?\n");
973 std::cerr << "Setcc on float!\n";
974 Tmp3 = MakeReg(MVT::f64);
Andrew Lenharth98169be2005-07-28 18:14:47 +0000975 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000976 Tmp1 = Tmp3;
977 }
978 if (SetCC->getOperand(1).getValueType() == MVT::f32)
979 {
980 //assert (0 && "Setcc On float?\n");
981 std::cerr << "Setcc on float!\n";
982 Tmp3 = MakeReg(MVT::f64);
Andrew Lenharth98169be2005-07-28 18:14:47 +0000983 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Alpha::F31).addReg(Tmp2);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000984 Tmp2 = Tmp3;
985 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000986
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000987 if (rev) std::swap(Tmp1, Tmp2);
988 //do the comparison
989 BuildMI(BB, Opc, 2, dst).addReg(Tmp1).addReg(Tmp2);
990 return inv;
991}
992
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000993//Check to see if the load is a constant offset from a base register
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000994void AlphaISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000995{
996 unsigned opcode = N.getOpcode();
Andrew Lenharth694c2982005-06-26 23:01:11 +0000997 if (opcode == ISD::ADD && N.getOperand(1).getOpcode() == ISD::Constant &&
998 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
999 { //Normal imm add
1000 Reg = SelectExpr(N.getOperand(0));
1001 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1002 return;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001003 }
1004 Reg = SelectExpr(N);
1005 offset = 0;
1006 return;
1007}
1008
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001009void AlphaISel::SelectBranchCC(SDOperand N)
Andrew Lenharth445171a2005-02-08 00:40:03 +00001010{
1011 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001012 MachineBasicBlock *Dest =
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001013 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
1014 unsigned Opc = Alpha::WTF;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001015
Andrew Lenharth445171a2005-02-08 00:40:03 +00001016 Select(N.getOperand(0)); //chain
1017 SDOperand CC = N.getOperand(1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001018
Andrew Lenharth445171a2005-02-08 00:40:03 +00001019 if (CC.getOpcode() == ISD::SETCC)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001020 {
Chris Lattner88ac32c2005-08-09 20:21:10 +00001021 ISD::CondCode cCode= cast<CondCodeSDNode>(CC.getOperand(2))->get();
1022 if (MVT::isInteger(CC.getOperand(0).getValueType())) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001023 //Dropping the CC is only useful if we are comparing to 0
Chris Lattner88ac32c2005-08-09 20:21:10 +00001024 bool RightZero = CC.getOperand(1).getOpcode() == ISD::Constant &&
1025 cast<ConstantSDNode>(CC.getOperand(1))->getValue() == 0;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001026 bool isNE = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001027
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001028 //Fix up CC
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001029 if(cCode == ISD::SETNE)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001030 isNE = true;
Andrew Lenharth445171a2005-02-08 00:40:03 +00001031
Andrew Lenharth694c2982005-06-26 23:01:11 +00001032 if (RightZero) {
Andrew Lenharth09552bf2005-06-08 18:02:21 +00001033 switch (cCode) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001034 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
1035 case ISD::SETEQ: Opc = Alpha::BEQ; break;
1036 case ISD::SETLT: Opc = Alpha::BLT; break;
1037 case ISD::SETLE: Opc = Alpha::BLE; break;
1038 case ISD::SETGT: Opc = Alpha::BGT; break;
1039 case ISD::SETGE: Opc = Alpha::BGE; break;
1040 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
1041 case ISD::SETUGT: Opc = Alpha::BNE; break;
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001042 //Technically you could have this CC
1043 case ISD::SETULE: Opc = Alpha::BEQ; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001044 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
1045 case ISD::SETNE: Opc = Alpha::BNE; break;
1046 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001047 unsigned Tmp1 = SelectExpr(CC.getOperand(0)); //Cond
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001048 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
1049 return;
1050 } else {
1051 unsigned Tmp1 = SelectExpr(CC);
1052 if (isNE)
1053 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
1054 else
1055 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001056 return;
1057 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001058 } else { //FP
Jeff Cohen00b168892005-07-27 06:12:32 +00001059 //Any comparison between 2 values should be codegened as an folded
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001060 //branch, as moving CC to the integer register is very expensive
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001061 //for a cmp b: c = a - b;
1062 //a = b: c = 0
1063 //a < b: c < 0
1064 //a > b: c > 0
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001065
1066 bool invTest = false;
1067 unsigned Tmp3;
1068
1069 ConstantFPSDNode *CN;
Chris Lattner88ac32c2005-08-09 20:21:10 +00001070 if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(1)))
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001071 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
Chris Lattner88ac32c2005-08-09 20:21:10 +00001072 Tmp3 = SelectExpr(CC.getOperand(0));
1073 else if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(0)))
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001074 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1075 {
Chris Lattner88ac32c2005-08-09 20:21:10 +00001076 Tmp3 = SelectExpr(CC.getOperand(1));
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001077 invTest = true;
1078 }
1079 else
1080 {
Chris Lattner88ac32c2005-08-09 20:21:10 +00001081 unsigned Tmp1 = SelectExpr(CC.getOperand(0));
1082 unsigned Tmp2 = SelectExpr(CC.getOperand(1));
1083 bool isD = CC.getOperand(0).getValueType() == MVT::f64;
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001084 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1085 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1086 .addReg(Tmp1).addReg(Tmp2);
1087 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001088
Chris Lattner88ac32c2005-08-09 20:21:10 +00001089 switch (cCode) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001090 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001091 case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
1092 case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
1093 case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
1094 case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
1095 case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
1096 case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001097 }
1098 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001099 return;
1100 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001101 abort(); //Should never be reached
1102 } else {
1103 //Giveup and do the stupid thing
1104 unsigned Tmp1 = SelectExpr(CC);
1105 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
1106 return;
1107 }
Andrew Lenharth445171a2005-02-08 00:40:03 +00001108 abort(); //Should never be reached
1109}
1110
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001111unsigned AlphaISel::SelectExpr(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001112 unsigned Result;
Andrew Lenharth2966e842005-04-07 18:15:28 +00001113 unsigned Tmp1, Tmp2 = 0, Tmp3;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001114 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001115 unsigned opcode = N.getOpcode();
Andrew Lenharthd2284272005-08-15 14:31:37 +00001116 int64_t SImm;
1117 uint64_t UImm;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001118
1119 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001120 MVT::ValueType DestType = N.getValueType();
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001121 bool isFP = DestType == MVT::f64 || DestType == MVT::f32;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001122
1123 unsigned &Reg = ExprMap[N];
1124 if (Reg) return Reg;
1125
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001126 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::TAILCALL)
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001127 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001128 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001129 else {
1130 // If this is a call instruction, make sure to prepare ALL of the result
1131 // values as well as the chain.
1132 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001133 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001134 else {
1135 Result = MakeReg(Node->getValueType(0));
1136 ExprMap[N.getValue(0)] = Result;
1137 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1138 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001139 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001140 }
1141 }
1142
Andrew Lenharth40831c52005-01-28 06:57:18 +00001143 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001144 default:
1145 Node->dump();
1146 assert(0 && "Node not handled!\n");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001147
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001148 case ISD::CTPOP:
1149 case ISD::CTTZ:
1150 case ISD::CTLZ:
1151 Opc = opcode == ISD::CTPOP ? Alpha::CTPOP :
1152 (opcode == ISD::CTTZ ? Alpha::CTTZ : Alpha::CTLZ);
1153 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthf3f951a2005-07-22 20:50:29 +00001154 BuildMI(BB, Opc, 1, Result).addReg(Alpha::R31).addReg(Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001155 return Result;
1156
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001157 case ISD::MULHU:
1158 Tmp1 = SelectExpr(N.getOperand(0));
1159 Tmp2 = SelectExpr(N.getOperand(1));
1160 BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth706be912005-04-07 13:55:53 +00001161 return Result;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001162 case ISD::MULHS:
1163 {
1164 //MULHU - Ra<63>*Rb - Rb<63>*Ra
1165 Tmp1 = SelectExpr(N.getOperand(0));
1166 Tmp2 = SelectExpr(N.getOperand(1));
1167 Tmp3 = MakeReg(MVT::i64);
1168 BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1169 unsigned V1 = MakeReg(MVT::i64);
1170 unsigned V2 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001171 BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31)
1172 .addReg(Tmp1);
1173 BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31)
1174 .addReg(Tmp2);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001175 unsigned IRes = MakeReg(MVT::i64);
1176 BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
1177 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
1178 return Result;
1179 }
Andrew Lenharth7332f3e2005-04-02 19:11:07 +00001180 case ISD::UNDEF: {
1181 BuildMI(BB, Alpha::IDEF, 0, Result);
1182 return Result;
1183 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001184
Andrew Lenharth032f2352005-02-22 21:59:48 +00001185 case ISD::DYNAMIC_STACKALLOC:
1186 // Generate both result values.
Andrew Lenharth3a7118d2005-02-23 17:33:42 +00001187 if (Result != notIn)
1188 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth032f2352005-02-22 21:59:48 +00001189 else
1190 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1191
1192 // FIXME: We are currently ignoring the requested alignment for handling
1193 // greater than the stack alignment. This will need to be revisited at some
1194 // point. Align = N.getOperand(2);
1195
1196 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1197 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1198 std::cerr << "Cannot allocate stack object with greater alignment than"
1199 << " the stack alignment yet!";
1200 abort();
1201 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001202
Andrew Lenharth032f2352005-02-22 21:59:48 +00001203 Select(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001204 if (isSIntImmediateBounded(N.getOperand(1), SImm, 0, 32767))
1205 BuildMI(BB, Alpha::LDA, 2, Alpha::R30).addImm(-SImm).addReg(Alpha::R30);
1206 else {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001207 Tmp1 = SelectExpr(N.getOperand(1));
1208 // Subtract size from stack pointer, thereby allocating some space.
1209 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1210 }
1211
1212 // Put a pointer to the space into the result register, by copying the stack
1213 // pointer.
Andrew Lenharth7bc47022005-02-22 23:29:25 +00001214 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
Andrew Lenharth032f2352005-02-22 21:59:48 +00001215 return Result;
1216
Andrew Lenharth02c318e2005-06-27 21:02:56 +00001217 case ISD::ConstantPool:
1218 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1219 AlphaLowering.restoreGP(BB);
1220 Tmp2 = MakeReg(MVT::i64);
1221 BuildMI(BB, Alpha::LDAHr, 2, Tmp2).addConstantPoolIndex(Tmp1)
1222 .addReg(Alpha::R29);
1223 BuildMI(BB, Alpha::LDAr, 2, Result).addConstantPoolIndex(Tmp1)
1224 .addReg(Tmp2);
1225 return Result;
Andrew Lenharth2c594352005-01-29 15:42:07 +00001226
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001227 case ISD::FrameIndex:
Andrew Lenharth032f2352005-02-22 21:59:48 +00001228 BuildMI(BB, Alpha::LDA, 2, Result)
1229 .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
1230 .addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001231 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001232
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001233 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001234 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001235 case ISD::SEXTLOAD:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001236 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001237 {
1238 // Make sure we generate both values.
1239 if (Result != notIn)
1240 ExprMap[N.getValue(1)] = notIn; // Generate the token
1241 else
1242 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001243
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001244 SDOperand Chain = N.getOperand(0);
1245 SDOperand Address = N.getOperand(1);
1246 Select(Chain);
1247
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001248 bool fpext = true;
1249
Andrew Lenharth03824012005-02-07 05:55:55 +00001250 if (opcode == ISD::LOAD)
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001251 switch (Node->getValueType(0)) {
1252 default: Node->dump(); assert(0 && "Bad load!");
1253 case MVT::i64: Opc = Alpha::LDQ; break;
1254 case MVT::f64: Opc = Alpha::LDT; break;
1255 case MVT::f32: Opc = Alpha::LDS; break;
1256 }
Andrew Lenharth03824012005-02-07 05:55:55 +00001257 else
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001258 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001259 default: Node->dump(); assert(0 && "Bad sign extend!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001260 case MVT::i32: Opc = Alpha::LDL;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001261 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001262 case MVT::i16: Opc = Alpha::LDWU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001263 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001264 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Misha Brukman4633f1c2005-04-21 23:13:11 +00001265 case MVT::i8: Opc = Alpha::LDBU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001266 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001267 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001268
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001269 int i, j, k;
1270 if (EnableAlphaLSMark)
1271 getValueInfo(dyn_cast<SrcValueSDNode>(N.getOperand(2))->getValue(),
1272 i, j, k);
1273
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001274 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Address);
1275 if (GASD && !GASD->getGlobal()->isExternal()) {
1276 Tmp1 = MakeReg(MVT::i64);
1277 AlphaLowering.restoreGP(BB);
1278 BuildMI(BB, Alpha::LDAHr, 2, Tmp1)
1279 .addGlobalAddress(GASD->getGlobal()).addReg(Alpha::R29);
1280 if (EnableAlphaLSMark)
1281 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1282 .addImm(getUID());
1283 BuildMI(BB, GetRelVersion(Opc), 2, Result)
1284 .addGlobalAddress(GASD->getGlobal()).addReg(Tmp1);
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001285 } else if (ConstantPoolSDNode *CP =
1286 dyn_cast<ConstantPoolSDNode>(Address)) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001287 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001288 has_sym = true;
Andrew Lenharthfe895e32005-06-27 17:15:36 +00001289 Tmp1 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001290 BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CP->getIndex())
1291 .addReg(Alpha::R29);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001292 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001293 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1294 .addImm(getUID());
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001295 BuildMI(BB, GetRelVersion(Opc), 2, Result)
1296 .addConstantPoolIndex(CP->getIndex()).addReg(Tmp1);
1297 } else if(Address.getOpcode() == ISD::FrameIndex) {
1298 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001299 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1300 .addImm(getUID());
Andrew Lenharth032f2352005-02-22 21:59:48 +00001301 BuildMI(BB, Opc, 2, Result)
1302 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1303 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001304 } else {
1305 long offset;
1306 SelectAddr(Address, Tmp1, offset);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001307 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001308 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1309 .addImm(getUID());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001310 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1311 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001312 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001313 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001314
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001315 case ISD::GlobalAddress:
1316 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001317 has_sym = true;
Jeff Cohen00b168892005-07-27 06:12:32 +00001318
Andrew Lenharth2f5bca52005-07-03 20:06:13 +00001319 Reg = Result = MakeReg(MVT::i64);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001320
1321 if (EnableAlphaLSMark)
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001322 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001323 .addImm(getUID());
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001324
1325 BuildMI(BB, Alpha::LDQl, 2, Result)
Andrew Lenharthc95d9842005-06-27 21:11:40 +00001326 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal())
1327 .addReg(Alpha::R29);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001328 return Result;
1329
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001330 case ISD::ExternalSymbol:
1331 AlphaLowering.restoreGP(BB);
1332 has_sym = true;
1333
Andrew Lenharth2f5bca52005-07-03 20:06:13 +00001334 Reg = Result = MakeReg(MVT::i64);
1335
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001336 if (EnableAlphaLSMark)
1337 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
1338 .addImm(getUID());
1339
1340 BuildMI(BB, Alpha::LDQl, 2, Result)
1341 .addExternalSymbol(cast<ExternalSymbolSDNode>(N)->getSymbol())
1342 .addReg(Alpha::R29);
1343 return Result;
1344
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001345 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001346 case ISD::CALL:
1347 {
1348 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001349
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001350 // The chain for this call is now lowered.
Andrew Lenharthf3f951a2005-07-22 20:50:29 +00001351 ExprMap[N.getValue(Node->getNumValues()-1)] = notIn;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001352
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001353 //grab the arguments
1354 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001355 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001356 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001357 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001358
Andrew Lenharth684f2292005-01-30 00:35:27 +00001359 //in reg args
1360 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001361 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001362 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001363 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +00001364 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001365 Alpha::F19, Alpha::F20, Alpha::F21};
1366 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001367 default:
1368 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001369 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001370 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001371 N.getOperand(i+2).getValueType() << "\n";
1372 assert(0 && "Unknown value type for call");
1373 case MVT::i1:
1374 case MVT::i8:
1375 case MVT::i16:
1376 case MVT::i32:
1377 case MVT::i64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001378 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i])
1379 .addReg(argvregs[i]);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001380 break;
1381 case MVT::f32:
1382 case MVT::f64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001383 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i])
1384 .addReg(argvregs[i]);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001385 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001386 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001387 }
Andrew Lenharth684f2292005-01-30 00:35:27 +00001388 //in mem args
1389 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001390 {
1391 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001392 default:
1393 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001394 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001395 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001396 N.getOperand(i+2).getValueType() << "\n";
1397 assert(0 && "Unknown value type for call");
1398 case MVT::i1:
1399 case MVT::i8:
1400 case MVT::i16:
1401 case MVT::i32:
1402 case MVT::i64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001403 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
1404 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001405 break;
1406 case MVT::f32:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001407 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
1408 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001409 break;
1410 case MVT::f64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001411 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
1412 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001413 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001414 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001415 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001416 //build the right kind of call
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001417 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(N.getOperand(1));
1418 if (GASD && !GASD->getGlobal()->isExternal()) {
1419 //use PC relative branch call
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001420 AlphaLowering.restoreGP(BB);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001421 BuildMI(BB, Alpha::BSR, 1, Alpha::R26)
1422 .addGlobalAddress(GASD->getGlobal(),true);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001423 } else {
1424 //no need to restore GP as we are doing an indirect call
1425 Tmp1 = SelectExpr(N.getOperand(1));
1426 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
1427 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
1428 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001429
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001430 //push the result into a virtual register
Misha Brukman4633f1c2005-04-21 23:13:11 +00001431
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001432 switch (Node->getValueType(0)) {
1433 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001434 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001435 case MVT::i1:
1436 case MVT::i8:
1437 case MVT::i16:
1438 case MVT::i32:
1439 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001440 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
1441 break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001442 case MVT::f32:
1443 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001444 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
1445 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001446 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001447 return Result+N.ResNo;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001448 }
1449
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001450 case ISD::SIGN_EXTEND_INREG:
1451 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001452 //do SDIV opt for all levels of ints if not dividing by a constant
1453 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
1454 && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001455 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001456 unsigned Tmp4 = MakeReg(MVT::f64);
1457 unsigned Tmp5 = MakeReg(MVT::f64);
1458 unsigned Tmp6 = MakeReg(MVT::f64);
1459 unsigned Tmp7 = MakeReg(MVT::f64);
1460 unsigned Tmp8 = MakeReg(MVT::f64);
1461 unsigned Tmp9 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001462
1463 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1464 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1465 MoveInt2FP(Tmp1, Tmp4, true);
1466 MoveInt2FP(Tmp2, Tmp5, true);
Andrew Lenharth98169be2005-07-28 18:14:47 +00001467 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Alpha::F31).addReg(Tmp4);
1468 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Alpha::F31).addReg(Tmp5);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001469 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
Andrew Lenharth98169be2005-07-28 18:14:47 +00001470 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Alpha::F31).addReg(Tmp8);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001471 MoveFP2Int(Tmp9, Result, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001472 return Result;
1473 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001474
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001475 //Alpha has instructions for a bunch of signed 32 bit stuff
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001476 if(cast<VTSDNode>(Node->getOperand(1))->getVT() == MVT::i32) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001477 switch (N.getOperand(0).getOpcode()) {
1478 case ISD::ADD:
1479 case ISD::SUB:
1480 case ISD::MUL:
1481 {
1482 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
1483 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
1484 //FIXME: first check for Scaled Adds and Subs!
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001485 if(!isMul && N.getOperand(0).getOperand(0).getOpcode() == ISD::SHL &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001486 isSIntImmediateBounded(N.getOperand(0).getOperand(0).getOperand(1), SImm, 2, 3))
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001487 {
Andrew Lenharthd2284272005-08-15 14:31:37 +00001488 bool use4 = SImm == 2;
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001489 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1490 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1491 BuildMI(BB, isAdd?(use4?Alpha::S4ADDL:Alpha::S8ADDL):(use4?Alpha::S4SUBL:Alpha::S8SUBL),
1492 2,Result).addReg(Tmp1).addReg(Tmp2);
1493 }
1494 else if(isAdd && N.getOperand(0).getOperand(1).getOpcode() == ISD::SHL &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001495 isSIntImmediateBounded(N.getOperand(0).getOperand(1).getOperand(1), SImm, 2, 3))
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001496 {
Andrew Lenharthd2284272005-08-15 14:31:37 +00001497 bool use4 = SImm == 2;
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001498 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1499 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1500 BuildMI(BB, use4?Alpha::S4ADDL:Alpha::S8ADDL, 2,Result).addReg(Tmp1).addReg(Tmp2);
1501 }
Andrew Lenharthd2284272005-08-15 14:31:37 +00001502 else if(isSIntImmediateBounded(N.getOperand(0).getOperand(1), SImm, 0, 255))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001503 { //Normal imm add/sub
1504 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001505 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001506 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001507 }
Andrew Lenharthd2284272005-08-15 14:31:37 +00001508 else if(!isMul && isSIntImmediate(N.getOperand(0).getOperand(1), SImm) &&
1509 (((SImm << 32) >> 32) >= -255) && (((SImm << 32) >> 32) <= 0))
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001510 { //handle canonicalization
1511 Opc = isAdd ? Alpha::SUBLi : Alpha::ADDLi;
1512 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001513 SImm = 0 - ((SImm << 32) >> 32);
1514 assert(SImm >= 0 && SImm <= 255);
1515 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001516 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001517 else
1518 { //Normal add/sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001519 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULL : Alpha::SUBL);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001520 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001521 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001522 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1523 }
1524 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001525 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001526 default: break; //Fall Though;
1527 }
1528 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001529 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001530 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001531 switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001532 default:
1533 Node->dump();
1534 assert(0 && "Sign Extend InReg not there yet");
1535 break;
1536 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001537 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001538 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001539 break;
1540 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001541 case MVT::i16:
Andrew Lenharthf3f951a2005-07-22 20:50:29 +00001542 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Alpha::R31).addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001543 break;
1544 case MVT::i8:
Andrew Lenharthf3f951a2005-07-22 20:50:29 +00001545 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Alpha::R31).addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001546 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +00001547 case MVT::i1:
1548 Tmp2 = MakeReg(MVT::i64);
1549 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001550 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +00001551 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001552 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001553 return Result;
1554 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001555
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001556 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001557 {
Chris Lattner88ac32c2005-08-09 20:21:10 +00001558 ISD::CondCode CC = cast<CondCodeSDNode>(N.getOperand(2))->get();
1559 if (MVT::isInteger(N.getOperand(0).getValueType())) {
1560 bool isConst = false;
1561 int dir;
Misha Brukman7847fca2005-04-22 17:54:37 +00001562
Chris Lattner88ac32c2005-08-09 20:21:10 +00001563 //Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001564 if(isSIntImmediate(N.getOperand(1), SImm) && SImm <= 255 && SImm >= 0)
Chris Lattner88ac32c2005-08-09 20:21:10 +00001565 isConst = true;
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001566
Chris Lattner88ac32c2005-08-09 20:21:10 +00001567 switch (CC) {
1568 default: Node->dump(); assert(0 && "Unknown integer comparison!");
1569 case ISD::SETEQ:
1570 Opc = isConst ? Alpha::CMPEQi : Alpha::CMPEQ; dir=1; break;
1571 case ISD::SETLT:
1572 Opc = isConst ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
1573 case ISD::SETLE:
1574 Opc = isConst ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
1575 case ISD::SETGT: Opc = Alpha::CMPLT; dir = 2; break;
1576 case ISD::SETGE: Opc = Alpha::CMPLE; dir = 2; break;
1577 case ISD::SETULT:
1578 Opc = isConst ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
1579 case ISD::SETUGT: Opc = Alpha::CMPULT; dir = 2; break;
1580 case ISD::SETULE:
1581 Opc = isConst ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
1582 case ISD::SETUGE: Opc = Alpha::CMPULE; dir = 2; break;
1583 case ISD::SETNE: {//Handle this one special
1584 //std::cerr << "Alpha does not have a setne.\n";
1585 //abort();
1586 Tmp1 = SelectExpr(N.getOperand(0));
1587 Tmp2 = SelectExpr(N.getOperand(1));
1588 Tmp3 = MakeReg(MVT::i64);
1589 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1590 //Remeber we have the Inv for this CC
1591 CCInvMap[N] = Tmp3;
1592 //and invert
1593 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
1594 return Result;
1595 }
1596 }
1597 if (dir == 1) {
1598 Tmp1 = SelectExpr(N.getOperand(0));
1599 if (isConst) {
Andrew Lenharthd2284272005-08-15 14:31:37 +00001600 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Chris Lattner88ac32c2005-08-09 20:21:10 +00001601 } else {
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001602 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth694c2982005-06-26 23:01:11 +00001603 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001604 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001605 } else { //if (dir == 2) {
1606 Tmp1 = SelectExpr(N.getOperand(1));
1607 Tmp2 = SelectExpr(N.getOperand(0));
1608 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001609 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001610 } else {
1611 //do the comparison
1612 Tmp1 = MakeReg(MVT::f64);
1613 bool inv = SelectFPSetCC(N, Tmp1);
1614
1615 //now arrange for Result (int) to have a 1 or 0
1616 Tmp2 = MakeReg(MVT::i64);
1617 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
1618 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
1619 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
Andrew Lenharth9818c052005-02-05 13:19:12 +00001620 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001621 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001622 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001623
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001624 case ISD::CopyFromReg:
1625 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001626 ++count_ins;
1627
Andrew Lenharth40831c52005-01-28 06:57:18 +00001628 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001629 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001630 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001631 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001632 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001633
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001634 SDOperand Chain = N.getOperand(0);
1635
1636 Select(Chain);
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001637 unsigned r = cast<RegSDNode>(Node)->getReg();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001638 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
Andrew Lenharth619fb522005-07-04 20:07:21 +00001639 if (MVT::isFloatingPoint(N.getValue(0).getValueType()))
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001640 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
1641 else
1642 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001643 return Result;
1644 }
1645
Misha Brukman4633f1c2005-04-21 23:13:11 +00001646 //Most of the plain arithmetic and logic share the same form, and the same
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001647 //constant immediate test
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001648 case ISD::XOR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001649 //Match Not
Andrew Lenharthd2284272005-08-15 14:31:37 +00001650 if (isSIntImmediate(N.getOperand(1), SImm) && SImm == -1) {
1651 Tmp1 = SelectExpr(N.getOperand(0));
1652 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1653 return Result;
1654 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001655 //Fall through
1656 case ISD::AND:
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001657 //handle zap
Andrew Lenharthd2284272005-08-15 14:31:37 +00001658 if (opcode == ISD::AND && isUIntImmediate(N.getOperand(1), UImm))
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001659 {
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001660 unsigned int build = 0;
1661 for(int i = 0; i < 8; ++i)
1662 {
Andrew Lenharthd2284272005-08-15 14:31:37 +00001663 if ((UImm & 0x00FF) == 0x00FF)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001664 build |= 1 << i;
Andrew Lenharthd2284272005-08-15 14:31:37 +00001665 else if ((UImm & 0x00FF) != 0)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001666 { build = 0; break; }
Andrew Lenharthd2284272005-08-15 14:31:37 +00001667 UImm >>= 8;
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001668 }
1669 if (build)
1670 {
1671 Tmp1 = SelectExpr(N.getOperand(0));
1672 BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1673 return Result;
1674 }
1675 }
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001676 case ISD::OR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001677 //Check operand(0) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001678 if (N.getOperand(0).getOpcode() == ISD::XOR &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001679 isSIntImmediate(N.getOperand(0).getOperand(1), SImm) && SImm == -1) {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001680 switch(opcode) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001681 case ISD::AND: Opc = Alpha::BIC; break;
1682 case ISD::OR: Opc = Alpha::ORNOT; break;
1683 case ISD::XOR: Opc = Alpha::EQV; break;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001684 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001685 Tmp1 = SelectExpr(N.getOperand(1));
1686 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1687 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1688 return Result;
1689 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001690 //Check operand(1) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001691 if (N.getOperand(1).getOpcode() == ISD::XOR &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001692 isSIntImmediate(N.getOperand(1).getOperand(1), SImm) && SImm == -1) {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001693 switch(opcode) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001694 case ISD::AND: Opc = Alpha::BIC; break;
1695 case ISD::OR: Opc = Alpha::ORNOT; break;
1696 case ISD::XOR: Opc = Alpha::EQV; break;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001697 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001698 Tmp1 = SelectExpr(N.getOperand(0));
1699 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1700 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1701 return Result;
1702 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001703 //Fall through
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001704 case ISD::SHL:
1705 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001706 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001707 case ISD::MUL:
Andrew Lenharthd2284272005-08-15 14:31:37 +00001708 if(isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255)) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001709 switch(opcode) {
1710 case ISD::AND: Opc = Alpha::ANDi; break;
1711 case ISD::OR: Opc = Alpha::BISi; break;
1712 case ISD::XOR: Opc = Alpha::XORi; break;
1713 case ISD::SHL: Opc = Alpha::SLi; break;
1714 case ISD::SRL: Opc = Alpha::SRLi; break;
1715 case ISD::SRA: Opc = Alpha::SRAi; break;
1716 case ISD::MUL: Opc = Alpha::MULQi; break;
1717 };
1718 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001719 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001720 } else {
1721 switch(opcode) {
1722 case ISD::AND: Opc = Alpha::AND; break;
1723 case ISD::OR: Opc = Alpha::BIS; break;
1724 case ISD::XOR: Opc = Alpha::XOR; break;
1725 case ISD::SHL: Opc = Alpha::SL; break;
1726 case ISD::SRL: Opc = Alpha::SRL; break;
1727 case ISD::SRA: Opc = Alpha::SRA; break;
Jeff Cohen00b168892005-07-27 06:12:32 +00001728 case ISD::MUL:
1729 Opc = isFP ? (DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS)
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001730 : Alpha::MULQ;
1731 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001732 };
1733 Tmp1 = SelectExpr(N.getOperand(0));
1734 Tmp2 = SelectExpr(N.getOperand(1));
1735 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1736 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001737 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001738
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001739 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001740 case ISD::SUB:
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001741 if (isFP) {
1742 ConstantFPSDNode *CN;
1743 if (opcode == ISD::ADD)
1744 Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS;
1745 else
1746 Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS;
1747 if (opcode == ISD::SUB
1748 && (CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
1749 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1750 {
1751 Tmp2 = SelectExpr(N.getOperand(1));
1752 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp2).addReg(Tmp2);
1753 } else {
1754 Tmp1 = SelectExpr(N.getOperand(0));
1755 Tmp2 = SelectExpr(N.getOperand(1));
1756 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1757 }
1758 return Result;
1759 } else {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001760 bool isAdd = opcode == ISD::ADD;
1761
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001762 //first check for Scaled Adds and Subs!
1763 //Valid for add and sub
Andrew Lenharthd2284272005-08-15 14:31:37 +00001764 if(N.getOperand(0).getOpcode() == ISD::SHL &&
1765 isSIntImmediate(N.getOperand(0).getOperand(1), SImm) &&
1766 (SImm == 2 || SImm == 3)) {
1767 bool use4 = SImm == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001768 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001769 if (isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255))
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001770 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
Andrew Lenharthd2284272005-08-15 14:31:37 +00001771 2, Result).addReg(Tmp2).addImm(SImm);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001772 else {
1773 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001774 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1775 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001776 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001777 }
1778 //Position prevents subs
Andrew Lenharth273a1f92005-04-07 14:18:13 +00001779 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001780 isSIntImmediate(N.getOperand(1).getOperand(1), SImm) &&
1781 (SImm == 2 || SImm == 3)) {
1782 bool use4 = SImm == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001783 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001784 if (isSIntImmediateBounded(N.getOperand(0), SImm, 0, 255))
1785 BuildMI(BB, use4?Alpha::S4ADDQi:Alpha::S8ADDQi, 2, Result).addReg(Tmp2).addImm(SImm);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001786 else {
1787 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001788 BuildMI(BB, use4?Alpha::S4ADDQ:Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001789 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001790 }
1791 //small addi
Andrew Lenharthd2284272005-08-15 14:31:37 +00001792 else if(isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001793 { //Normal imm add/sub
1794 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1795 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001796 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001797 }
Andrew Lenharthd2284272005-08-15 14:31:37 +00001798 else if(isSIntImmediateBounded(N.getOperand(1), SImm, -255, 0))
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001799 { //inverted imm add/sub
1800 Opc = isAdd ? Alpha::SUBQi : Alpha::ADDQi;
1801 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001802 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(-SImm);
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001803 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001804 //larger addi
Andrew Lenharthd2284272005-08-15 14:31:37 +00001805 else if(isSIntImmediateBounded(N.getOperand(1), SImm, -32767, 32767))
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001806 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001807 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001808 if (!isAdd)
Andrew Lenharthd2284272005-08-15 14:31:37 +00001809 SImm = -SImm;
1810 BuildMI(BB, Alpha::LDA, 2, Result).addImm(SImm).addReg(Tmp1);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001811 }
1812 //give up and do the operation
1813 else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001814 //Normal add/sub
1815 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1816 Tmp1 = SelectExpr(N.getOperand(0));
1817 Tmp2 = SelectExpr(N.getOperand(1));
1818 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1819 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001820 return Result;
1821 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001822
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001823 case ISD::SDIV:
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001824 if (isFP) {
1825 Tmp1 = SelectExpr(N.getOperand(0));
1826 Tmp2 = SelectExpr(N.getOperand(1));
1827 BuildMI(BB, DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS, 2, Result)
1828 .addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth0cab3752005-06-29 13:35:05 +00001829 return Result;
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001830 } else {
Andrew Lenhartha565c272005-04-06 22:03:13 +00001831 //check if we can convert into a shift!
Andrew Lenharthd2284272005-08-15 14:31:37 +00001832 if (isSIntImmediate(N.getOperand(1), SImm) &&
1833 SImm != 0 && isPowerOf2_64(llabs(SImm))) {
1834 unsigned k = Log2_64(llabs(SImm));
Andrew Lenhartha565c272005-04-06 22:03:13 +00001835 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenhartha565c272005-04-06 22:03:13 +00001836 if (k == 1)
1837 Tmp2 = Tmp1;
1838 else
1839 {
1840 Tmp2 = MakeReg(MVT::i64);
1841 BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
1842 }
1843 Tmp3 = MakeReg(MVT::i64);
1844 BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
1845 unsigned Tmp4 = MakeReg(MVT::i64);
1846 BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharthd2284272005-08-15 14:31:37 +00001847 if (SImm > 0)
Andrew Lenhartha565c272005-04-06 22:03:13 +00001848 BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
1849 else
1850 {
1851 unsigned Tmp5 = MakeReg(MVT::i64);
1852 BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
1853 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
1854 }
1855 return Result;
1856 }
1857 }
1858 //Else fall through
1859
1860 case ISD::UDIV:
1861 {
Andrew Lenharthd2284272005-08-15 14:31:37 +00001862 if (isSIntImmediate(N.getOperand(1), SImm) && (SImm >= 2 || SImm <= -2))
Andrew Lenhartha565c272005-04-06 22:03:13 +00001863 {
1864 // If this is a divide by constant, we can emit code using some magic
1865 // constants to implement it as a multiply instead.
1866 ExprMap.erase(N);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001867 if (opcode == ISD::SDIV)
Andrew Lenhartha565c272005-04-06 22:03:13 +00001868 return SelectExpr(BuildSDIVSequence(N));
1869 else
1870 return SelectExpr(BuildUDIVSequence(N));
1871 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001872 }
1873 //else fall though
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001874 case ISD::UREM:
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001875 case ISD::SREM: {
1876 const char* opstr = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001877 switch(opcode) {
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001878 case ISD::UREM: opstr = "__remqu"; break;
1879 case ISD::SREM: opstr = "__remq"; break;
1880 case ISD::UDIV: opstr = "__divqu"; break;
1881 case ISD::SDIV: opstr = "__divq"; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001882 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001883 Tmp1 = SelectExpr(N.getOperand(0));
1884 Tmp2 = SelectExpr(N.getOperand(1));
Jeff Cohen00b168892005-07-27 06:12:32 +00001885 SDOperand Addr =
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001886 ISelDAG->getExternalSymbol(opstr, AlphaLowering.getPointerTy());
1887 Tmp3 = SelectExpr(Addr);
Andrew Lenharth33819132005-03-04 20:09:23 +00001888 //set up regs explicitly (helps Reg alloc)
1889 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001890 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001891 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp3).addReg(Tmp3);
1892 BuildMI(BB, Alpha::JSRs, 2, Alpha::R23).addReg(Alpha::R27).addImm(0);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001893 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001894 return Result;
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001895 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001896
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001897 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001898 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001899 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001900 assert (DestType == MVT::i64 && "only quads can be loaded to");
1901 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00001902 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001903 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001904 if (SrcType == MVT::f32)
Misha Brukman7847fca2005-04-22 17:54:37 +00001905 {
1906 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth98169be2005-07-28 18:14:47 +00001907 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Alpha::F31).addReg(Tmp1);
Misha Brukman7847fca2005-04-22 17:54:37 +00001908 Tmp1 = Tmp2;
1909 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001910 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth98169be2005-07-28 18:14:47 +00001911 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001912 MoveFP2Int(Tmp2, Result, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001913
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001914 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001915 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001916
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001917 case ISD::SELECT:
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001918 if (isFP) {
1919 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1920 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
1921 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
1922
1923 SDOperand CC = N.getOperand(0);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001924
Chris Lattner88ac32c2005-08-09 20:21:10 +00001925 if (CC.getOpcode() == ISD::SETCC &&
1926 !MVT::isInteger(CC.getOperand(0).getValueType())) {
1927 //FP Setcc -> Select yay!
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001928
Jeff Cohen00b168892005-07-27 06:12:32 +00001929
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001930 //for a cmp b: c = a - b;
1931 //a = b: c = 0
1932 //a < b: c < 0
1933 //a > b: c > 0
1934
1935 bool invTest = false;
1936 unsigned Tmp3;
1937
1938 ConstantFPSDNode *CN;
Chris Lattner88ac32c2005-08-09 20:21:10 +00001939 if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(1)))
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001940 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
Chris Lattner88ac32c2005-08-09 20:21:10 +00001941 Tmp3 = SelectExpr(CC.getOperand(0));
1942 else if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(0)))
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001943 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1944 {
Chris Lattner88ac32c2005-08-09 20:21:10 +00001945 Tmp3 = SelectExpr(CC.getOperand(1));
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001946 invTest = true;
1947 }
1948 else
1949 {
Chris Lattner88ac32c2005-08-09 20:21:10 +00001950 unsigned Tmp1 = SelectExpr(CC.getOperand(0));
1951 unsigned Tmp2 = SelectExpr(CC.getOperand(1));
1952 bool isD = CC.getOperand(0).getValueType() == MVT::f64;
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001953 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1954 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1955 .addReg(Tmp1).addReg(Tmp2);
1956 }
1957
Chris Lattner88ac32c2005-08-09 20:21:10 +00001958 switch (cast<CondCodeSDNode>(CC.getOperand(2))->get()) {
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001959 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
1960 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNE : Alpha::FCMOVEQ; break;
1961 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGT : Alpha::FCMOVLT; break;
1962 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGE : Alpha::FCMOVLE; break;
1963 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLT : Alpha::FCMOVGT; break;
1964 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLE : Alpha::FCMOVGE; break;
1965 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQ : Alpha::FCMOVNE; break;
1966 }
1967 BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
1968 return Result;
1969 }
1970 else
1971 {
1972 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1973 BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV)
1974 .addReg(Tmp1);
1975// // Spill the cond to memory and reload it from there.
1976// unsigned Tmp4 = MakeReg(MVT::f64);
1977// MoveIntFP(Tmp1, Tmp4, true);
1978// //now ideally, we don't have to do anything to the flag...
1979// // Get the condition into the zero flag.
1980// BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
1981 return Result;
Jeff Cohen00b168892005-07-27 06:12:32 +00001982 }
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001983 } else {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001984 //FIXME: look at parent to decide if intCC can be folded, or if setCC(FP)
1985 //and can save stack use
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001986 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001987 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1988 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001989 // Get the condition into the zero flag.
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001990 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001991
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001992 SDOperand CC = N.getOperand(0);
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001993
Misha Brukman4633f1c2005-04-21 23:13:11 +00001994 if (CC.getOpcode() == ISD::SETCC &&
Chris Lattner88ac32c2005-08-09 20:21:10 +00001995 !MVT::isInteger(CC.getOperand(0).getValueType()))
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001996 { //FP Setcc -> Int Select
Misha Brukman7847fca2005-04-22 17:54:37 +00001997 Tmp1 = MakeReg(MVT::f64);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001998 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1999 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Misha Brukman7847fca2005-04-22 17:54:37 +00002000 bool inv = SelectFPSetCC(CC, Tmp1);
2001 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
2002 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
2003 return Result;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002004 }
2005 if (CC.getOpcode() == ISD::SETCC) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002006 //Int SetCC -> Select
2007 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharthd2284272005-08-15 14:31:37 +00002008 if(isSIntImmediateBounded(CC.getOperand(1), SImm, 0, 0)) {
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002009 //figure out a few things
Andrew Lenharthd2284272005-08-15 14:31:37 +00002010 bool useImm = isSIntImmediateBounded(N.getOperand(2), SImm, 0, 255);
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002011
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002012 //Fix up CC
Chris Lattner88ac32c2005-08-09 20:21:10 +00002013 ISD::CondCode cCode= cast<CondCodeSDNode>(CC.getOperand(2))->get();
Andrew Lenharth694c2982005-06-26 23:01:11 +00002014 if (useImm) //Invert sense to get Imm field right
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002015 cCode = ISD::getSetCCInverse(cCode, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002016
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002017 //Choose the CMOV
2018 switch (cCode) {
2019 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002020 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
2021 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
2022 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
2023 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
2024 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
2025 case ISD::SETULT: assert(0 && "unsigned < 0 is never true"); break;
2026 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2027 //Technically you could have this CC
2028 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
2029 case ISD::SETUGE: assert(0 && "unsgined >= 0 is always true"); break;
2030 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002031 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00002032 Tmp1 = SelectExpr(CC.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002033
Andrew Lenharth694c2982005-06-26 23:01:11 +00002034 if (useImm) {
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002035 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
Andrew Lenharthd2284272005-08-15 14:31:37 +00002036 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addImm(SImm).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002037 } else {
2038 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2039 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2040 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
2041 }
2042 return Result;
2043 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002044 //Otherwise, fall though
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002045 }
2046 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002047 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2048 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002049 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3)
2050 .addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002051
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002052 return Result;
2053 }
2054
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002055 case ISD::Constant:
2056 {
Andrew Lenharthc0513832005-03-29 19:24:04 +00002057 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharth6b137d82005-07-22 22:24:01 +00002058 int zero_extend_top = 0;
Andrew Lenharthf075cac2005-07-23 07:46:48 +00002059 if (val > 0 && (val & 0xFFFFFFFF00000000ULL) == 0 &&
Andrew Lenharth6b137d82005-07-22 22:24:01 +00002060 ((int32_t)val < 0)) {
2061 //try a small load and zero extend
2062 val = (int32_t)val;
2063 zero_extend_top = 15;
2064 }
2065
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002066 if (val <= IMM_HIGH && val >= IMM_LOW) {
Andrew Lenharth6b137d82005-07-22 22:24:01 +00002067 if(!zero_extend_top)
2068 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
2069 else {
2070 Tmp1 = MakeReg(MVT::i64);
2071 BuildMI(BB, Alpha::LDA, 2, Tmp1).addImm(val).addReg(Alpha::R31);
2072 BuildMI(BB, Alpha::ZAPNOT, 2, Result).addReg(Tmp1).addImm(zero_extend_top);
2073 }
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002074 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002075 else if (val <= (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT &&
2076 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
2077 Tmp1 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002078 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val))
2079 .addReg(Alpha::R31);
Andrew Lenharth6b137d82005-07-22 22:24:01 +00002080 if (!zero_extend_top)
2081 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
2082 else {
2083 Tmp3 = MakeReg(MVT::i64);
2084 BuildMI(BB, Alpha::LDA, 2, Tmp3).addImm(getLower16(val)).addReg(Tmp1);
2085 BuildMI(BB, Alpha::ZAPNOT, 2, Result).addReg(Tmp3).addImm(zero_extend_top);
2086 }
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002087 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002088 else {
Andrew Lenharth6b137d82005-07-22 22:24:01 +00002089 //re-get the val since we are going to mem anyway
2090 val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002091 MachineConstantPool *CP = BB->getParent()->getConstantPool();
Jeff Cohen00b168892005-07-27 06:12:32 +00002092 ConstantUInt *C =
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002093 ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002094 unsigned CPI = CP->getConstantPoolIndex(C);
2095 AlphaLowering.restoreGP(BB);
Andrew Lenharthfe895e32005-06-27 17:15:36 +00002096 has_sym = true;
2097 Tmp1 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002098 BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CPI)
2099 .addReg(Alpha::R29);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00002100 if (EnableAlphaLSMark)
2101 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
2102 .addImm(getUID());
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002103 BuildMI(BB, Alpha::LDQr, 2, Result).addConstantPoolIndex(CPI)
2104 .addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002105 }
2106 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002107 }
Andrew Lenharthf4da9452005-06-29 12:49:51 +00002108 case ISD::FNEG:
2109 if(ISD::FABS == N.getOperand(0).getOpcode())
2110 {
2111 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
2112 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
2113 } else {
2114 Tmp1 = SelectExpr(N.getOperand(0));
2115 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp1).addReg(Tmp1);
2116 }
2117 return Result;
2118
2119 case ISD::FABS:
2120 Tmp1 = SelectExpr(N.getOperand(0));
2121 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
2122 return Result;
2123
2124 case ISD::FP_ROUND:
2125 assert (DestType == MVT::f32 &&
2126 N.getOperand(0).getValueType() == MVT::f64 &&
2127 "only f64 to f32 conversion supported here");
2128 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth98169be2005-07-28 18:14:47 +00002129 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00002130 return Result;
2131
2132 case ISD::FP_EXTEND:
2133 assert (DestType == MVT::f64 &&
2134 N.getOperand(0).getValueType() == MVT::f32 &&
2135 "only f32 to f64 conversion supported here");
2136 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth98169be2005-07-28 18:14:47 +00002137 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00002138 return Result;
2139
2140 case ISD::ConstantFP:
2141 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
2142 if (CN->isExactlyValue(+0.0)) {
2143 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31)
2144 .addReg(Alpha::F31);
2145 } else if ( CN->isExactlyValue(-0.0)) {
2146 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31)
2147 .addReg(Alpha::F31);
2148 } else {
2149 abort();
2150 }
2151 }
2152 return Result;
2153
2154 case ISD::SINT_TO_FP:
2155 {
2156 assert (N.getOperand(0).getValueType() == MVT::i64
2157 && "only quads can be loaded from");
2158 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
2159 Tmp2 = MakeReg(MVT::f64);
2160 MoveInt2FP(Tmp1, Tmp2, true);
2161 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
Andrew Lenharth98169be2005-07-28 18:14:47 +00002162 BuildMI(BB, Opc, 1, Result).addReg(Alpha::F31).addReg(Tmp2);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00002163 return Result;
2164 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002165 }
2166
2167 return 0;
2168}
2169
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002170void AlphaISel::Select(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002171 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00002172 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002173
Nate Begeman85fdeb22005-03-24 04:39:54 +00002174 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002175 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002176
2177 SDNode *Node = N.Val;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002178
Andrew Lenharth760270d2005-02-07 23:02:23 +00002179 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002180
2181 default:
2182 Node->dump(); std::cerr << "\n";
2183 assert(0 && "Node not handled yet!");
2184
2185 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00002186 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002187 return;
2188 }
2189
2190 case ISD::BR: {
2191 MachineBasicBlock *Dest =
2192 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2193
2194 Select(N.getOperand(0));
2195 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
2196 return;
2197 }
2198
2199 case ISD::ImplicitDef:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002200 ++count_ins;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002201 Select(N.getOperand(0));
2202 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
2203 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002204
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002205 case ISD::EntryToken: return; // Noop
2206
2207 case ISD::TokenFactor:
2208 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2209 Select(Node->getOperand(i));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002210
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002211 //N.Val->dump(); std::cerr << "\n";
2212 //assert(0 && "Node not handled yet!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00002213
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002214 return;
2215
2216 case ISD::CopyToReg:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002217 ++count_outs;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002218 Select(N.getOperand(0));
2219 Tmp1 = SelectExpr(N.getOperand(1));
2220 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002221
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002222 if (Tmp1 != Tmp2) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002223 if (N.getOperand(1).getValueType() == MVT::f64 ||
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002224 N.getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00002225 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2226 else
2227 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002228 }
2229 return;
2230
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002231 case ISD::RET:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002232 ++count_outs;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002233 switch (N.getNumOperands()) {
2234 default:
2235 std::cerr << N.getNumOperands() << "\n";
2236 for (unsigned i = 0; i < N.getNumOperands(); ++i)
2237 std::cerr << N.getOperand(i).getValueType() << "\n";
2238 Node->dump();
2239 assert(0 && "Unknown return instruction!");
2240 case 2:
2241 Select(N.getOperand(0));
2242 Tmp1 = SelectExpr(N.getOperand(1));
2243 switch (N.getOperand(1).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002244 default: Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002245 assert(0 && "All other types should have been promoted!!");
2246 case MVT::f64:
2247 case MVT::f32:
2248 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
2249 break;
2250 case MVT::i32:
2251 case MVT::i64:
2252 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
2253 break;
2254 }
2255 break;
2256 case 1:
2257 Select(N.getOperand(0));
2258 break;
2259 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002260 // Just emit a 'ret' instruction
Andrew Lenharth6968bff2005-06-27 23:24:11 +00002261 AlphaLowering.restoreRA(BB);
Andrew Lenharthf3f951a2005-07-22 20:50:29 +00002262 BuildMI(BB, Alpha::RET, 2, Alpha::R31).addReg(Alpha::R26).addImm(1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002263 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002264
Misha Brukman4633f1c2005-04-21 23:13:11 +00002265 case ISD::TRUNCSTORE:
2266 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002267 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002268 SDOperand Chain = N.getOperand(0);
2269 SDOperand Value = N.getOperand(1);
2270 SDOperand Address = N.getOperand(2);
2271 Select(Chain);
2272
2273 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00002274
2275 if (opcode == ISD::STORE) {
2276 switch(Value.getValueType()) {
2277 default: assert(0 && "unknown Type in store");
2278 case MVT::i64: Opc = Alpha::STQ; break;
2279 case MVT::f64: Opc = Alpha::STT; break;
2280 case MVT::f32: Opc = Alpha::STS; break;
2281 }
2282 } else { //ISD::TRUNCSTORE
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002283 switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) {
Andrew Lenharth760270d2005-02-07 23:02:23 +00002284 default: assert(0 && "unknown Type in store");
2285 case MVT::i1: //FIXME: DAG does not promote this load
2286 case MVT::i8: Opc = Alpha::STB; break;
2287 case MVT::i16: Opc = Alpha::STW; break;
2288 case MVT::i32: Opc = Alpha::STL; break;
2289 }
Andrew Lenharth65838902005-02-06 16:22:15 +00002290 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00002291
Andrew Lenharth06ef8842005-06-29 18:54:02 +00002292 int i, j, k;
Jeff Cohen00b168892005-07-27 06:12:32 +00002293 if (EnableAlphaLSMark)
2294 getValueInfo(cast<SrcValueSDNode>(N.getOperand(3))->getValue(),
Andrew Lenharth06ef8842005-06-29 18:54:02 +00002295 i, j, k);
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002296
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00002297 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Address);
2298 if (GASD && !GASD->getGlobal()->isExternal()) {
2299 Tmp2 = MakeReg(MVT::i64);
2300 AlphaLowering.restoreGP(BB);
2301 BuildMI(BB, Alpha::LDAHr, 2, Tmp2)
2302 .addGlobalAddress(GASD->getGlobal()).addReg(Alpha::R29);
2303 if (EnableAlphaLSMark)
2304 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
2305 .addImm(getUID());
2306 BuildMI(BB, GetRelVersion(Opc), 3).addReg(Tmp1)
2307 .addGlobalAddress(GASD->getGlobal()).addReg(Tmp2);
Andrew Lenharthfce587e2005-06-29 00:39:17 +00002308 } else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00002309 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00002310 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
2311 .addImm(getUID());
Andrew Lenharth032f2352005-02-22 21:59:48 +00002312 BuildMI(BB, Opc, 3).addReg(Tmp1)
2313 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
2314 .addReg(Alpha::F31);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00002315 } else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002316 long offset;
2317 SelectAddr(Address, Tmp2, offset);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00002318 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00002319 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
2320 .addImm(getUID());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002321 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2322 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002323 return;
2324 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002325
2326 case ISD::EXTLOAD:
2327 case ISD::SEXTLOAD:
2328 case ISD::ZEXTLOAD:
2329 case ISD::LOAD:
2330 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002331 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002332 case ISD::CALL:
Andrew Lenharth032f2352005-02-22 21:59:48 +00002333 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002334 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002335 SelectExpr(N);
2336 return;
2337
Chris Lattner16cd04d2005-05-12 23:24:06 +00002338 case ISD::CALLSEQ_START:
2339 case ISD::CALLSEQ_END:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002340 Select(N.getOperand(0));
2341 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002342
Chris Lattner16cd04d2005-05-12 23:24:06 +00002343 Opc = N.getOpcode() == ISD::CALLSEQ_START ? Alpha::ADJUSTSTACKDOWN :
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002344 Alpha::ADJUSTSTACKUP;
2345 BuildMI(BB, Opc, 1).addImm(Tmp1);
2346 return;
Andrew Lenharth95762122005-03-31 21:24:06 +00002347
2348 case ISD::PCMARKER:
2349 Select(N.getOperand(0)); //Chain
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002350 BuildMI(BB, Alpha::PCLABEL, 2)
2351 .addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
Andrew Lenharth95762122005-03-31 21:24:06 +00002352 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002353 }
2354 assert(0 && "Should not be reached!");
2355}
2356
2357
2358/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
2359/// into a machine code representation using pattern matching and a machine
2360/// description file.
2361///
2362FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002363 return new AlphaISel(TM);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002364}
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002365