blob: c30cbd77bdfb16d866d9ed09f836bdb9aa2599f2 [file] [log] [blame]
Misha Brukman2a8350a2005-02-05 02:24:26 +00001//===- AlphaISelPattern.cpp - A pattern matching inst selector for Alpha --===//
Misha Brukman4633f1c2005-04-21 23:13:11 +00002//
Andrew Lenharth304d0f32005-01-22 23:41:55 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman4633f1c2005-04-21 23:13:11 +00007//
Andrew Lenharth304d0f32005-01-22 23:41:55 +00008//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for Alpha.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Alpha.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000015#include "AlphaRegisterInfo.h"
16#include "llvm/Constants.h" // FIXME: REMOVE
17#include "llvm/Function.h"
Andrew Lenharthb69f3422005-06-22 17:19:45 +000018#include "llvm/Module.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000019#include "llvm/CodeGen/MachineInstrBuilder.h"
20#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/SelectionDAG.h"
24#include "llvm/CodeGen/SelectionDAGISel.h"
25#include "llvm/CodeGen/SSARegMap.h"
26#include "llvm/Target/TargetData.h"
27#include "llvm/Target/TargetLowering.h"
28#include "llvm/Support/MathExtras.h"
29#include "llvm/ADT/Statistic.h"
Andrew Lenharth032f2352005-02-22 21:59:48 +000030#include "llvm/Support/Debug.h"
Andrew Lenharth95762122005-03-31 21:24:06 +000031#include "llvm/Support/CommandLine.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000032#include <set>
Andrew Lenharth684f2292005-01-30 00:35:27 +000033#include <algorithm>
Andrew Lenharth304d0f32005-01-22 23:41:55 +000034using namespace llvm;
35
Andrew Lenharth95762122005-03-31 21:24:06 +000036namespace llvm {
Misha Brukman4633f1c2005-04-21 23:13:11 +000037 cl::opt<bool> EnableAlphaIDIV("enable-alpha-intfpdiv",
38 cl::desc("Use the FP div instruction for integer div when possible"),
Andrew Lenharth95762122005-03-31 21:24:06 +000039 cl::Hidden);
Andrew Lenharth59009192005-05-04 19:12:09 +000040 cl::opt<bool> EnableAlphaFTOI("enable-alpha-FTOI",
Misha Brukman4633f1c2005-04-21 23:13:11 +000041 cl::desc("Enable use of ftoi* and itof* instructions (ev6 and higher)"),
Andrew Lenharth95762122005-03-31 21:24:06 +000042 cl::Hidden);
Andrew Lenharth59009192005-05-04 19:12:09 +000043 cl::opt<bool> EnableAlphaCT("enable-alpha-CT",
44 cl::desc("Enable use of the ctpop, ctlz, and cttz instructions"),
45 cl::Hidden);
Misha Brukman4633f1c2005-04-21 23:13:11 +000046 cl::opt<bool> EnableAlphaCount("enable-alpha-count",
47 cl::desc("Print estimates on live ins and outs"),
Andrew Lenhartha32b9e32005-04-08 17:28:49 +000048 cl::Hidden);
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +000049 cl::opt<bool> EnableAlphaLSMark("enable-alpha-lsmark",
Misha Brukman5e96a3a2005-06-06 19:08:04 +000050 cl::desc("Emit symbols to correlate Mem ops to LLVM Values"),
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +000051 cl::Hidden);
Andrew Lenharth95762122005-03-31 21:24:06 +000052}
53
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +000054namespace {
55 // Alpha Specific DAG Nodes
56 namespace AlphaISD {
57 enum NodeType {
58 // Start the numbering where the builtin ops leave off.
59 FIRST_NUMBER = ISD::BUILTIN_OP_END,
60
61 //Convert an int bit pattern in an FP reg to a Double or Float
62 //Has a dest type and a source
63 CVTQ,
64 //Move an Ireg to a FPreg
65 ITOF,
66 //Move a FPreg to an Ireg
67 FTOI,
68 };
69 }
70}
71
Andrew Lenharth304d0f32005-01-22 23:41:55 +000072//===----------------------------------------------------------------------===//
73// AlphaTargetLowering - Alpha Implementation of the TargetLowering interface
74namespace {
75 class AlphaTargetLowering : public TargetLowering {
Andrew Lenharth558bc882005-06-18 18:34:52 +000076 int VarArgsOffset; // What is the offset to the first vaarg
77 int VarArgsBase; // What is the base FrameIndex
Andrew Lenharth304d0f32005-01-22 23:41:55 +000078 unsigned GP; //GOT vreg
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +000079 unsigned RA; //Return Address
Andrew Lenharth304d0f32005-01-22 23:41:55 +000080 public:
81 AlphaTargetLowering(TargetMachine &TM) : TargetLowering(TM) {
82 // Set up the TargetLowering object.
Andrew Lenharth3d65d312005-01-27 03:49:45 +000083 //I am having problems with shr n ubyte 1
Andrew Lenharth879ef222005-02-02 17:00:21 +000084 setShiftAmountType(MVT::i64);
85 setSetCCResultType(MVT::i64);
Andrew Lenharthd3355e22005-04-07 20:11:32 +000086 setSetCCResultContents(ZeroOrOneSetCCResult);
Misha Brukman4633f1c2005-04-21 23:13:11 +000087
Andrew Lenharth304d0f32005-01-22 23:41:55 +000088 addRegisterClass(MVT::i64, Alpha::GPRCRegisterClass);
89 addRegisterClass(MVT::f64, Alpha::FPRCRegisterClass);
Andrew Lenharth3d65d312005-01-27 03:49:45 +000090 addRegisterClass(MVT::f32, Alpha::FPRCRegisterClass);
Misha Brukman4633f1c2005-04-21 23:13:11 +000091
Chris Lattnerda4d4692005-04-09 03:22:37 +000092 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000093
Andrew Lenharthec151362005-06-26 22:23:06 +000094 setOperationAction(ISD::EXTLOAD, MVT::i1, Promote);
95 setOperationAction(ISD::EXTLOAD, MVT::f32, Promote);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000096
Andrew Lenharthec151362005-06-26 22:23:06 +000097 setOperationAction(ISD::ZEXTLOAD, MVT::i1 , Expand);
98 setOperationAction(ISD::ZEXTLOAD, MVT::i32 , Expand);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000099
Andrew Lenharthec151362005-06-26 22:23:06 +0000100 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
101 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
102 setOperationAction(ISD::SEXTLOAD, MVT::i16, Expand);
103
104 setOperationAction(ISD::SREM, MVT::f32, Expand);
105 setOperationAction(ISD::SREM, MVT::f64, Expand);
106
107 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000108
Andrew Lenharth59009192005-05-04 19:12:09 +0000109 if (!EnableAlphaCT) {
110 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
111 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
Andrew Lenharthb5884d32005-05-04 19:25:37 +0000112 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
Andrew Lenharth59009192005-05-04 19:12:09 +0000113 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000114
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000115 //If this didn't legalize into a div....
116 // setOperationAction(ISD::SREM , MVT::i64, Expand);
117 // setOperationAction(ISD::UREM , MVT::i64, Expand);
118
119 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
120 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
121 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
Andrew Lenharth9818c052005-02-05 13:19:12 +0000122
Chris Lattner17234b72005-04-30 04:26:06 +0000123 // We don't support sin/cos/sqrt
124 setOperationAction(ISD::FSIN , MVT::f64, Expand);
125 setOperationAction(ISD::FCOS , MVT::f64, Expand);
126 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
127 setOperationAction(ISD::FSIN , MVT::f32, Expand);
128 setOperationAction(ISD::FCOS , MVT::f32, Expand);
129 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
130
Andrew Lenharth33819132005-03-04 20:09:23 +0000131 //Doesn't work yet
Chris Lattner17234b72005-04-30 04:26:06 +0000132 setOperationAction(ISD::SETCC, MVT::f32, Promote);
Andrew Lenharth572af902005-02-14 05:41:43 +0000133
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000134 //Try a couple things with a custom expander
135 //setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
136
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000137 computeRegisterProperties();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000138
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000139 addLegalFPImmediate(+0.0); //F31
140 addLegalFPImmediate(-0.0); //-F31
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000141 }
142
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000143 /// LowerOperation - Provide custom lowering hooks for some operations.
144 ///
145 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
146
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000147 /// LowerArguments - This hook must be implemented to indicate how we should
148 /// lower the arguments for the specified function, into the specified DAG.
149 virtual std::vector<SDOperand>
150 LowerArguments(Function &F, SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000151
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000152 /// LowerCallTo - This hook lowers an abstract call to a function into an
153 /// actual call.
154 virtual std::pair<SDOperand, SDOperand>
Chris Lattnerc57f6822005-05-12 19:56:45 +0000155 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattneradf6a962005-05-13 18:50:42 +0000156 bool isTailCall, SDOperand Callee, ArgListTy &Args,
157 SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000158
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000159 virtual std::pair<SDOperand, SDOperand>
Andrew Lenharth558bc882005-06-18 18:34:52 +0000160 LowerVAStart(SDOperand Chain, SelectionDAG &DAG, SDOperand Dest);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000161
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000162 virtual std::pair<SDOperand,SDOperand>
Andrew Lenharth558bc882005-06-18 18:34:52 +0000163 LowerVAArgNext(SDOperand Chain, SDOperand VAList,
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000164 const Type *ArgTy, SelectionDAG &DAG);
165
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000166 std::pair<SDOperand,SDOperand>
167 LowerVACopy(SDOperand Chain, SDOperand Src, SDOperand Dest,
168 SelectionDAG &DAG);
169
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000170 virtual std::pair<SDOperand, SDOperand>
171 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
172 SelectionDAG &DAG);
173
174 void restoreGP(MachineBasicBlock* BB)
175 {
176 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
177 }
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000178 void restoreRA(MachineBasicBlock* BB)
179 {
180 BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(RA).addReg(RA);
181 }
182
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000183 };
184}
185
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000186/// LowerOperation - Provide custom lowering hooks for some operations.
187///
188SDOperand AlphaTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
189 MachineFunction &MF = DAG.getMachineFunction();
190 switch (Op.getOpcode()) {
191 default: assert(0 && "Should not custom lower this!");
Misha Brukmanb8ee91a2005-06-06 17:39:46 +0000192#if 0
193 case ISD::SINT_TO_FP:
194 {
195 assert (Op.getOperand(0).getValueType() == MVT::i64
196 && "only quads can be loaded from");
197 SDOperand SRC;
198 if (EnableAlphaFTOI)
199 {
200 std::vector<MVT::ValueType> RTs;
201 RTs.push_back(Op.getValueType());
202 std::vector<SDOperand> Ops;
203 Ops.push_back(Op.getOperand(0));
204 SRC = DAG.getNode(AlphaISD::ITOF, RTs, Ops);
205 } else {
206 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
207 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
208 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
209 Op.getOperand(0), StackSlot, DAG.getSrcValue(NULL));
210 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
264 //Handle the return address
265 //BuildMI(&BB, Alpha::IDEF, 0, Alpha::R26);
266
Misha Brukman4633f1c2005-04-21 23:13:11 +0000267 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000268 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000269 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000270 Alpha::F19, Alpha::F20, Alpha::F21};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000271 int count = 0;
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000272
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000273 GP = AddLiveIn(MF, Alpha::R29, getRegClassFor(MVT::i64));
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000274 RA = AddLiveIn(MF, Alpha::R26, getRegClassFor(MVT::i64));
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000275
Chris Lattnere4d5c442005-03-15 04:54:21 +0000276 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000277 {
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000278 SDOperand argt;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000279 if (count < 6) {
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000280 unsigned Vreg;
281 MVT::ValueType VT = getValueType(I->getType());
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000282 switch (VT) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000283 default:
284 std::cerr << "Unknown Type " << VT << "\n";
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000285 abort();
286 case MVT::f64:
287 case MVT::f32:
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000288 args_float[count] = AddLiveIn(MF,args_float[count], getRegClassFor(VT));
289 argt = DAG.getCopyFromReg(args_float[count], VT, DAG.getRoot());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000290 break;
291 case MVT::i1:
292 case MVT::i8:
293 case MVT::i16:
294 case MVT::i32:
295 case MVT::i64:
Andrew Lenharth591ec572005-05-31 18:42:18 +0000296 args_int[count] = AddLiveIn(MF, args_int[count], getRegClassFor(MVT::i64));
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000297 argt = DAG.getCopyFromReg(args_int[count], VT, DAG.getRoot());
Andrew Lenharth14f30c92005-05-31 18:37:16 +0000298 if (VT != MVT::i64)
299 argt = DAG.getNode(ISD::TRUNCATE, VT, argt);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000300 break;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000301 }
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000302 DAG.setRoot(argt.getValue(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000303 } else { //more args
304 // Create the frame index object for this incoming parameter...
305 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000306
307 // Create the SelectionDAG nodes corresponding to a load
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000308 //from this parameter
309 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000310 argt = DAG.getLoad(getValueType(I->getType()),
311 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000312 }
Andrew Lenharth032f2352005-02-22 21:59:48 +0000313 ++count;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000314 ArgValues.push_back(argt);
315 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000316
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000317 // If the functions takes variable number of arguments, copy all regs to stack
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000318 if (F.isVarArg()) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000319 VarArgsOffset = count * 8;
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000320 std::vector<SDOperand> LS;
321 for (int i = 0; i < 6; ++i) {
322 if (args_int[i] < 1024)
323 args_int[i] = AddLiveIn(MF,args_int[i], getRegClassFor(MVT::i64));
324 SDOperand argt = DAG.getCopyFromReg(args_int[i], MVT::i64, DAG.getRoot());
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000325 int FI = MFI->CreateFixedObject(8, -8 * (6 - i));
Andrew Lenharth558bc882005-06-18 18:34:52 +0000326 if (i == 0) VarArgsBase = FI;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000327 SDOperand SDFI = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000328 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt, SDFI, DAG.getSrcValue(NULL)));
329
330 if (args_float[i] < 1024)
331 args_float[i] = AddLiveIn(MF,args_float[i], getRegClassFor(MVT::f64));
332 argt = DAG.getCopyFromReg(args_float[i], MVT::f64, DAG.getRoot());
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000333 FI = MFI->CreateFixedObject(8, - 8 * (12 - i));
334 SDFI = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000335 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt, 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,
Misha Brukman7847fca2005-04-22 17:54:37 +0000367 SDOperand Callee, ArgListTy &Args,
368 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
413std::pair<SDOperand, SDOperand>
Andrew Lenharth558bc882005-06-18 18:34:52 +0000414AlphaTargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG, SDOperand Dest) {
415 // vastart just stores the address of the VarArgsBase and VarArgsOffset
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000416 SDOperand FR = DAG.getFrameIndex(VarArgsBase, MVT::i64);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000417 SDOperand S1 = DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, Dest, DAG.getSrcValue(NULL));
418 SDOperand SA2 = DAG.getNode(ISD::ADD, MVT::i64, Dest, DAG.getConstant(8, MVT::i64));
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000419 SDOperand S2 = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, S1,
Andrew Lenharth558bc882005-06-18 18:34:52 +0000420 DAG.getConstant(VarArgsOffset, MVT::i64), SA2,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000421 DAG.getSrcValue(NULL), MVT::i32);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000422 return std::make_pair(S2, S2);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000423}
424
425std::pair<SDOperand,SDOperand> AlphaTargetLowering::
Andrew Lenharth558bc882005-06-18 18:34:52 +0000426LowerVAArgNext(SDOperand Chain, SDOperand VAList,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000427 const Type *ArgTy, SelectionDAG &DAG) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000428 SDOperand Base = DAG.getLoad(MVT::i64, Chain, VAList, DAG.getSrcValue(NULL));
429 SDOperand Tmp = DAG.getNode(ISD::ADD, MVT::i64, VAList,
430 DAG.getConstant(8, MVT::i64));
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000431 SDOperand Offset = DAG.getNode(ISD::SEXTLOAD, MVT::i64, Base.getValue(1), Tmp,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000432 DAG.getSrcValue(NULL), MVT::i32);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000433 SDOperand DataPtr = DAG.getNode(ISD::ADD, MVT::i64, Base, Offset);
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000434 if (ArgTy->isFloatingPoint())
435 {
436 //if fp && Offset < 6*8, then subtract 6*8 from DataPtr
437 SDOperand FPDataPtr = DAG.getNode(ISD::SUB, MVT::i64, DataPtr,
438 DAG.getConstant(8*6, MVT::i64));
439 SDOperand CC = DAG.getSetCC(ISD::SETLT, MVT::i64,
440 Offset, DAG.getConstant(8*6, MVT::i64));
441 DataPtr = DAG.getNode(ISD::SELECT, MVT::i64, CC, FPDataPtr, DataPtr);
442 }
443
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000444 SDOperand Result;
445 if (ArgTy == Type::IntTy)
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000446 Result = DAG.getNode(ISD::SEXTLOAD, MVT::i64, Offset.getValue(1), DataPtr,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000447 DAG.getSrcValue(NULL), MVT::i32);
448 else if (ArgTy == Type::UIntTy)
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000449 Result = DAG.getNode(ISD::ZEXTLOAD, MVT::i64, Offset.getValue(1), DataPtr,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000450 DAG.getSrcValue(NULL), MVT::i32);
451 else
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000452 Result = DAG.getLoad(getValueType(ArgTy), Offset.getValue(1), DataPtr,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000453 DAG.getSrcValue(NULL));
454
Andrew Lenharth558bc882005-06-18 18:34:52 +0000455 SDOperand NewOffset = DAG.getNode(ISD::ADD, MVT::i64, Offset,
456 DAG.getConstant(8, MVT::i64));
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000457 SDOperand Update = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Result.getValue(1), NewOffset,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000458 Tmp, DAG.getSrcValue(NULL), MVT::i32);
459 Result = DAG.getNode(ISD::TRUNCATE, getValueType(ArgTy), Result);
460
Andrew Lenharth558bc882005-06-18 18:34:52 +0000461 return std::make_pair(Result, Update);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000462}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000463
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000464std::pair<SDOperand,SDOperand> AlphaTargetLowering::
465LowerVACopy(SDOperand Chain, SDOperand Src, SDOperand Dest,
466 SelectionDAG &DAG) {
467 //Default to returning the input list
468 SDOperand Val = DAG.getLoad(getPointerTy(), Chain, Src, DAG.getSrcValue(NULL));
469 SDOperand Result = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
470 Val, Dest, DAG.getSrcValue(NULL));
471 SDOperand NP = DAG.getNode(ISD::ADD, MVT::i64, Src,
472 DAG.getConstant(8, MVT::i64));
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000473 Val = DAG.getNode(ISD::SEXTLOAD, MVT::i64, Result, NP, DAG.getSrcValue(NULL),
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000474 MVT::i32);
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000475 SDOperand NPD = DAG.getNode(ISD::ADD, MVT::i64, Dest,
476 DAG.getConstant(8, MVT::i64));
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000477 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Val.getValue(1),
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000478 Val, NPD, DAG.getSrcValue(NULL), MVT::i32);
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000479 return std::make_pair(Result, Result);
480}
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000481
482std::pair<SDOperand, SDOperand> AlphaTargetLowering::
483LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
484 SelectionDAG &DAG) {
485 abort();
486}
487
488
489
490
491
492namespace {
493
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000494//===--------------------------------------------------------------------===//
495/// ISel - Alpha specific code to select Alpha machine instructions for
496/// SelectionDAG operations.
497//===--------------------------------------------------------------------===//
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000498class AlphaISel : public SelectionDAGISel {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000499
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000500 /// AlphaLowering - This object fully describes how to lower LLVM code to an
501 /// Alpha-specific SelectionDAG.
502 AlphaTargetLowering AlphaLowering;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000503
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000504 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
505 // for sdiv and udiv until it is put into the future
506 // dag combiner.
507
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000508 /// ExprMap - As shared expressions are codegen'd, we keep track of which
509 /// vreg the value is produced in, so we only emit one copy of each compiled
510 /// tree.
511 static const unsigned notIn = (unsigned)(-1);
512 std::map<SDOperand, unsigned> ExprMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000513
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000514 //CCInvMap sometimes (SetNE) we have the inverse CC code for free
515 std::map<SDOperand, unsigned> CCInvMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000516
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000517 int count_ins;
518 int count_outs;
519 bool has_sym;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000520 int max_depth;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000521
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000522public:
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000523 AlphaISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering), AlphaLowering(TM)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000524 {}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000525
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000526 /// InstructionSelectBasicBlock - This callback is invoked by
527 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
528 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
Andrew Lenharth032f2352005-02-22 21:59:48 +0000529 DEBUG(BB->dump());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000530 count_ins = 0;
531 count_outs = 0;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000532 max_depth = 0;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000533 has_sym = false;
534
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000535 // Codegen the basic block.
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000536 ISelDAG = &DAG;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000537 max_depth = DAG.getRoot().getNodeDepth();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000538 Select(DAG.getRoot());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000539
540 if(has_sym)
541 ++count_ins;
542 if(EnableAlphaCount)
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000543 std::cerr << "COUNT: " << BB->getParent()->getFunction ()->getName() << " "
544 << BB->getNumber() << " "
545 << max_depth << " "
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000546 << count_ins << " "
547 << count_outs << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000548
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000549 // Clear state used for selection.
550 ExprMap.clear();
551 CCInvMap.clear();
552 }
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000553
554 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000555
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000556 unsigned SelectExpr(SDOperand N);
557 unsigned SelectExprFP(SDOperand N, unsigned Result);
558 void Select(SDOperand N);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000559
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000560 void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
561 void SelectBranchCC(SDOperand N);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000562 void MoveFP2Int(unsigned src, unsigned dst, bool isDouble);
563 void MoveInt2FP(unsigned src, unsigned dst, bool isDouble);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000564 //returns whether the sense of the comparison was inverted
565 bool SelectFPSetCC(SDOperand N, unsigned dst);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000566
567 // dag -> dag expanders for integer divide by constant
568 SDOperand BuildSDIVSequence(SDOperand N);
569 SDOperand BuildUDIVSequence(SDOperand N);
570
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000571};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000572}
573
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000574void AlphaISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000575 // If this function has live-in values, emit the copies from pregs to vregs at
576 // the top of the function, before anything else.
577 MachineBasicBlock *BB = MF.begin();
578 if (MF.livein_begin() != MF.livein_end()) {
579 SSARegMap *RegMap = MF.getSSARegMap();
580 for (MachineFunction::livein_iterator LI = MF.livein_begin(),
581 E = MF.livein_end(); LI != E; ++LI) {
582 const TargetRegisterClass *RC = RegMap->getRegClass(LI->second);
583 if (RC == Alpha::GPRCRegisterClass) {
584 BuildMI(BB, Alpha::BIS, 2, LI->second).addReg(LI->first).addReg(LI->first);
585 } else if (RC == Alpha::FPRCRegisterClass) {
586 BuildMI(BB, Alpha::CPYS, 2, LI->second).addReg(LI->first).addReg(LI->first);
587 } else {
588 assert(0 && "Unknown regclass!");
589 }
590 }
591 }
592}
593
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000594//Find the offset of the arg in it's parent's function
595static int getValueOffset(const Value* v)
596{
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000597 static int uniqneg = -1;
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000598 if (v == NULL)
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000599 return uniqneg--;
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000600
601 const Instruction* itarget = dyn_cast<Instruction>(v);
602 const BasicBlock* btarget = itarget->getParent();
603 const Function* ftarget = btarget->getParent();
604
605 //offset due to earlier BBs
606 int i = 0;
607 for(Function::const_iterator ii = ftarget->begin(); &*ii != btarget; ++ii)
608 i += ii->size();
609
610 for(BasicBlock::const_iterator ii = btarget->begin(); &*ii != itarget; ++ii)
611 ++i;
612
613 return i;
614}
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000615//Find the offset of the function in it's module
616static int getFunctionOffset(const Function* fun)
617{
618 const Module* M = fun->getParent();
619
620 //offset due to earlier BBs
621 int i = 0;
622 for(Module::const_iterator ii = M->begin(); &*ii != fun; ++ii)
623 ++i;
624
625 return i;
626}
627
628static int getUID()
629{
630 static int id = 0;
631 return ++id;
632}
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000633
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000634//Factorize a number using the list of constants
635static bool factorize(int v[], int res[], int size, uint64_t c)
636{
637 bool cont = true;
638 while (c != 1 && cont)
639 {
640 cont = false;
641 for(int i = 0; i < size; ++i)
642 {
643 if (c % v[i] == 0)
644 {
645 c /= v[i];
646 ++res[i];
647 cont=true;
648 }
649 }
650 }
651 return c == 1;
652}
653
654
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000655//Shamelessly adapted from PPC32
Misha Brukman4633f1c2005-04-21 23:13:11 +0000656// Structure used to return the necessary information to codegen an SDIV as
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000657// a multiply.
658struct ms {
659 int64_t m; // magic number
660 int64_t s; // shift amount
661};
662
663struct mu {
664 uint64_t m; // magic number
665 int64_t a; // add indicator
666 int64_t s; // shift amount
667};
668
669/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukman4633f1c2005-04-21 23:13:11 +0000670/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000671/// or -1.
672static struct ms magic(int64_t d) {
673 int64_t p;
674 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
675 const uint64_t two63 = 9223372036854775808ULL; // 2^63
676 struct ms mag;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000677
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000678 ad = abs(d);
679 t = two63 + ((uint64_t)d >> 63);
680 anc = t - 1 - t%ad; // absolute value of nc
Andrew Lenharth320174f2005-04-07 17:19:16 +0000681 p = 63; // initialize p
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000682 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
683 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
684 q2 = two63/ad; // initialize q2 = 2p/abs(d)
685 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
686 do {
687 p = p + 1;
688 q1 = 2*q1; // update q1 = 2p/abs(nc)
689 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
690 if (r1 >= anc) { // must be unsigned comparison
691 q1 = q1 + 1;
692 r1 = r1 - anc;
693 }
694 q2 = 2*q2; // update q2 = 2p/abs(d)
695 r2 = 2*r2; // update r2 = rem(2p/abs(d))
696 if (r2 >= ad) { // must be unsigned comparison
697 q2 = q2 + 1;
698 r2 = r2 - ad;
699 }
700 delta = ad - r2;
701 } while (q1 < delta || (q1 == delta && r1 == 0));
702
703 mag.m = q2 + 1;
704 if (d < 0) mag.m = -mag.m; // resulting magic number
705 mag.s = p - 64; // resulting shift
706 return mag;
707}
708
709/// magicu - calculate the magic numbers required to codegen an integer udiv as
710/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
711static struct mu magicu(uint64_t d)
712{
713 int64_t p;
714 uint64_t nc, delta, q1, r1, q2, r2;
715 struct mu magu;
716 magu.a = 0; // initialize "add" indicator
717 nc = - 1 - (-d)%d;
Andrew Lenharth320174f2005-04-07 17:19:16 +0000718 p = 63; // initialize p
719 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
720 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
721 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
722 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000723 do {
724 p = p + 1;
725 if (r1 >= nc - r1 ) {
726 q1 = 2*q1 + 1; // update q1
727 r1 = 2*r1 - nc; // update r1
728 }
729 else {
730 q1 = 2*q1; // update q1
731 r1 = 2*r1; // update r1
732 }
733 if (r2 + 1 >= d - r2) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000734 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000735 q2 = 2*q2 + 1; // update q2
736 r2 = 2*r2 + 1 - d; // update r2
737 }
738 else {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000739 if (q2 >= 0x8000000000000000ull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000740 q2 = 2*q2; // update q2
741 r2 = 2*r2 + 1; // update r2
742 }
743 delta = d - 1 - r2;
744 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
745 magu.m = q2 + 1; // resulting magic number
Andrew Lenharth320174f2005-04-07 17:19:16 +0000746 magu.s = p - 64; // resulting shift
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000747 return magu;
748}
749
750/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
751/// return a DAG expression to select that will generate the same value by
752/// multiplying by a magic number. See:
753/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000754SDOperand AlphaISel::BuildSDIVSequence(SDOperand N) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000755 int64_t d = (int64_t)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000756 ms magics = magic(d);
757 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000758 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000759 ISelDAG->getConstant(magics.m, MVT::i64));
760 // If d > 0 and m < 0, add the numerator
761 if (d > 0 && magics.m < 0)
762 Q = ISelDAG->getNode(ISD::ADD, MVT::i64, Q, N.getOperand(0));
763 // If d < 0 and m > 0, subtract the numerator.
764 if (d < 0 && magics.m > 0)
765 Q = ISelDAG->getNode(ISD::SUB, MVT::i64, Q, N.getOperand(0));
766 // Shift right algebraic if shift value is nonzero
767 if (magics.s > 0)
Misha Brukman4633f1c2005-04-21 23:13:11 +0000768 Q = ISelDAG->getNode(ISD::SRA, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000769 ISelDAG->getConstant(magics.s, MVT::i64));
770 // Extract the sign bit and add it to the quotient
Misha Brukman4633f1c2005-04-21 23:13:11 +0000771 SDOperand T =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000772 ISelDAG->getNode(ISD::SRL, MVT::i64, Q, ISelDAG->getConstant(63, MVT::i64));
773 return ISelDAG->getNode(ISD::ADD, MVT::i64, Q, T);
774}
775
776/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
777/// return a DAG expression to select that will generate the same value by
778/// multiplying by a magic number. See:
779/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000780SDOperand AlphaISel::BuildUDIVSequence(SDOperand N) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000781 unsigned d =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000782 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
783 mu magics = magicu(d);
784 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000785 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000786 ISelDAG->getConstant(magics.m, MVT::i64));
787 if (magics.a == 0) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000788 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000789 ISelDAG->getConstant(magics.s, MVT::i64));
790 } else {
791 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i64, N.getOperand(0), Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000792 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000793 ISelDAG->getConstant(1, MVT::i64));
794 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i64, NPQ, Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000795 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000796 ISelDAG->getConstant(magics.s-1, MVT::i64));
797 }
798 return Q;
799}
800
Andrew Lenhartha565c272005-04-06 22:03:13 +0000801//From PPC32
802/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
803/// returns zero when the input is not exactly a power of two.
804static unsigned ExactLog2(uint64_t Val) {
805 if (Val == 0 || (Val & (Val-1))) return 0;
806 unsigned Count = 0;
807 while (Val != 1) {
808 Val >>= 1;
809 ++Count;
810 }
811 return Count;
812}
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000813
814
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000815//These describe LDAx
Andrew Lenharthc0513832005-03-29 19:24:04 +0000816static const int IMM_LOW = -32768;
817static const int IMM_HIGH = 32767;
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000818static const int IMM_MULT = 65536;
819
820static long getUpper16(long l)
821{
822 long y = l / IMM_MULT;
823 if (l % IMM_MULT > IMM_HIGH)
824 ++y;
825 return y;
826}
827
828static long getLower16(long l)
829{
830 long h = getUpper16(l);
831 return l - h * IMM_MULT;
832}
833
Andrew Lenharth65838902005-02-06 16:22:15 +0000834static unsigned GetSymVersion(unsigned opcode)
835{
836 switch (opcode) {
837 default: assert(0 && "unknown load or store"); return 0;
838 case Alpha::LDQ: return Alpha::LDQ_SYM;
839 case Alpha::LDS: return Alpha::LDS_SYM;
840 case Alpha::LDT: return Alpha::LDT_SYM;
841 case Alpha::LDL: return Alpha::LDL_SYM;
842 case Alpha::LDBU: return Alpha::LDBU_SYM;
843 case Alpha::LDWU: return Alpha::LDWU_SYM;
844 case Alpha::LDW: return Alpha::LDW_SYM;
845 case Alpha::LDB: return Alpha::LDB_SYM;
846 case Alpha::STQ: return Alpha::STQ_SYM;
847 case Alpha::STS: return Alpha::STS_SYM;
848 case Alpha::STT: return Alpha::STT_SYM;
849 case Alpha::STL: return Alpha::STL_SYM;
850 case Alpha::STW: return Alpha::STW_SYM;
851 case Alpha::STB: return Alpha::STB_SYM;
852 }
853}
854
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000855void AlphaISel::MoveFP2Int(unsigned src, unsigned dst, bool isDouble)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000856{
857 unsigned Opc;
858 if (EnableAlphaFTOI) {
859 Opc = isDouble ? Alpha::FTOIT : Alpha::FTOIS;
860 BuildMI(BB, Opc, 1, dst).addReg(src);
861 } else {
862 //The hard way:
863 // Spill the integer to memory and reload it from there.
864 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
865 MachineFunction *F = BB->getParent();
866 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
867
868 Opc = isDouble ? Alpha::STT : Alpha::STS;
869 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
870 Opc = isDouble ? Alpha::LDQ : Alpha::LDL;
871 BuildMI(BB, Alpha::LDQ, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
872 }
873}
874
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000875void AlphaISel::MoveInt2FP(unsigned src, unsigned dst, bool isDouble)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000876{
877 unsigned Opc;
878 if (EnableAlphaFTOI) {
879 Opc = isDouble?Alpha::ITOFT:Alpha::ITOFS;
880 BuildMI(BB, Opc, 1, dst).addReg(src);
881 } else {
882 //The hard way:
883 // Spill the integer to memory and reload it from there.
884 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
885 MachineFunction *F = BB->getParent();
886 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
887
888 Opc = isDouble ? Alpha::STQ : Alpha::STL;
889 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
890 Opc = isDouble ? Alpha::LDT : Alpha::LDS;
891 BuildMI(BB, Opc, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
892 }
893}
894
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000895bool AlphaISel::SelectFPSetCC(SDOperand N, unsigned dst)
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000896{
897 SDNode *Node = N.Val;
898 unsigned Opc, Tmp1, Tmp2, Tmp3;
899 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node);
900
901 //assert(SetCC->getOperand(0).getValueType() != MVT::f32 && "SetCC f32 should have been promoted");
902 bool rev = false;
903 bool inv = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000904
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000905 switch (SetCC->getCondition()) {
906 default: Node->dump(); assert(0 && "Unknown FP comparison!");
907 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
908 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
909 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
910 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
911 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
912 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
913 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000914
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000915 //FIXME: check for constant 0.0
916 ConstantFPSDNode *CN;
917 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
918 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
919 Tmp1 = Alpha::F31;
920 else
921 Tmp1 = SelectExpr(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000922
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000923 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
924 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
925 Tmp2 = Alpha::F31;
926 else
Chris Lattner9c9183a2005-04-30 04:44:07 +0000927 Tmp2 = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000928
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000929 //Can only compare doubles, and dag won't promote for me
930 if (SetCC->getOperand(0).getValueType() == MVT::f32)
931 {
932 //assert(0 && "Setcc On float?\n");
933 std::cerr << "Setcc on float!\n";
934 Tmp3 = MakeReg(MVT::f64);
935 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
936 Tmp1 = Tmp3;
937 }
938 if (SetCC->getOperand(1).getValueType() == MVT::f32)
939 {
940 //assert (0 && "Setcc On float?\n");
941 std::cerr << "Setcc on float!\n";
942 Tmp3 = MakeReg(MVT::f64);
943 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp2);
944 Tmp2 = Tmp3;
945 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000946
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000947 if (rev) std::swap(Tmp1, Tmp2);
948 //do the comparison
949 BuildMI(BB, Opc, 2, dst).addReg(Tmp1).addReg(Tmp2);
950 return inv;
951}
952
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000953//Check to see if the load is a constant offset from a base register
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000954void AlphaISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000955{
956 unsigned opcode = N.getOpcode();
957 if (opcode == ISD::ADD) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000958 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000959 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
960 { //Normal imm add
961 Reg = SelectExpr(N.getOperand(0));
962 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
963 return;
964 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000965 else if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000966 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 32767)
967 {
968 Reg = SelectExpr(N.getOperand(1));
969 offset = cast<ConstantSDNode>(N.getOperand(0))->getValue();
970 return;
971 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000972 }
973 Reg = SelectExpr(N);
974 offset = 0;
975 return;
976}
977
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000978void AlphaISel::SelectBranchCC(SDOperand N)
Andrew Lenharth445171a2005-02-08 00:40:03 +0000979{
980 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000981 MachineBasicBlock *Dest =
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000982 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
983 unsigned Opc = Alpha::WTF;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000984
Andrew Lenharth445171a2005-02-08 00:40:03 +0000985 Select(N.getOperand(0)); //chain
986 SDOperand CC = N.getOperand(1);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000987
Andrew Lenharth445171a2005-02-08 00:40:03 +0000988 if (CC.getOpcode() == ISD::SETCC)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000989 {
990 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
991 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
992 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000993 bool LeftZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
994 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
Andrew Lenharth09552bf2005-06-08 18:02:21 +0000995 bool RightZero = SetCC->getOperand(1).getOpcode() == ISD::Constant &&
996 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000997 bool isNE = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000998
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000999 //Fix up CC
1000 ISD::CondCode cCode= SetCC->getCondition();
1001 if (LeftZero && !RightZero) //Swap Operands
1002 cCode = ISD::getSetCCSwappedOperands(cCode);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001003
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001004 if(cCode == ISD::SETNE)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001005 isNE = true;
Andrew Lenharth445171a2005-02-08 00:40:03 +00001006
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001007 if (LeftZero || RightZero) {
Andrew Lenharth09552bf2005-06-08 18:02:21 +00001008 switch (cCode) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001009 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
1010 case ISD::SETEQ: Opc = Alpha::BEQ; break;
1011 case ISD::SETLT: Opc = Alpha::BLT; break;
1012 case ISD::SETLE: Opc = Alpha::BLE; break;
1013 case ISD::SETGT: Opc = Alpha::BGT; break;
1014 case ISD::SETGE: Opc = Alpha::BGE; break;
1015 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
1016 case ISD::SETUGT: Opc = Alpha::BNE; break;
1017 case ISD::SETULE: Opc = Alpha::BEQ; break; //Technically you could have this CC
1018 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
1019 case ISD::SETNE: Opc = Alpha::BNE; break;
1020 }
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001021 unsigned Tmp1;
1022 if(LeftZero && !RightZero) //swap Operands
1023 Tmp1 = SelectExpr(SetCC->getOperand(1)); //Cond
1024 else
1025 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001026 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
1027 return;
1028 } else {
1029 unsigned Tmp1 = SelectExpr(CC);
1030 if (isNE)
1031 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
1032 else
1033 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001034 return;
1035 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001036 } else { //FP
1037 //Any comparison between 2 values should be codegened as an folded branch, as moving
1038 //CC to the integer register is very expensive
1039 //for a cmp b: c = a - b;
1040 //a = b: c = 0
1041 //a < b: c < 0
1042 //a > b: c > 0
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001043
1044 bool invTest = false;
1045 unsigned Tmp3;
1046
1047 ConstantFPSDNode *CN;
1048 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
1049 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1050 Tmp3 = SelectExpr(SetCC->getOperand(0));
1051 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
1052 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1053 {
1054 Tmp3 = SelectExpr(SetCC->getOperand(1));
1055 invTest = true;
1056 }
1057 else
1058 {
1059 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
1060 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
1061 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
1062 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1063 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1064 .addReg(Tmp1).addReg(Tmp2);
1065 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001066
1067 switch (SetCC->getCondition()) {
1068 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001069 case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
1070 case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
1071 case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
1072 case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
1073 case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
1074 case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001075 }
1076 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001077 return;
1078 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001079 abort(); //Should never be reached
1080 } else {
1081 //Giveup and do the stupid thing
1082 unsigned Tmp1 = SelectExpr(CC);
1083 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
1084 return;
1085 }
Andrew Lenharth445171a2005-02-08 00:40:03 +00001086 abort(); //Should never be reached
1087}
1088
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001089unsigned AlphaISel::SelectExprFP(SDOperand N, unsigned Result)
Andrew Lenharth40831c52005-01-28 06:57:18 +00001090{
1091 unsigned Tmp1, Tmp2, Tmp3;
1092 unsigned Opc = 0;
1093 SDNode *Node = N.Val;
1094 MVT::ValueType DestType = N.getValueType();
1095 unsigned opcode = N.getOpcode();
1096
1097 switch (opcode) {
1098 default:
1099 Node->dump();
1100 assert(0 && "Node not handled!\n");
Andrew Lenharth2c594352005-01-29 15:42:07 +00001101
Andrew Lenharth7332f3e2005-04-02 19:11:07 +00001102 case ISD::UNDEF: {
1103 BuildMI(BB, Alpha::IDEF, 0, Result);
1104 return Result;
1105 }
1106
Andrew Lenharth30b46d42005-04-02 19:04:58 +00001107 case ISD::FNEG:
1108 if(ISD::FABS == N.getOperand(0).getOpcode())
1109 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001110 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1111 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +00001112 } else {
Misha Brukman7847fca2005-04-22 17:54:37 +00001113 Tmp1 = SelectExpr(N.getOperand(0));
1114 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +00001115 }
1116 return Result;
1117
1118 case ISD::FABS:
1119 Tmp1 = SelectExpr(N.getOperand(0));
1120 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
1121 return Result;
1122
Andrew Lenharth9818c052005-02-05 13:19:12 +00001123 case ISD::SELECT:
1124 {
Andrew Lenharth45859692005-03-03 21:47:53 +00001125 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1126 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
1127 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
1128
1129 SDOperand CC = N.getOperand(0);
1130 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
1131
Misha Brukman4633f1c2005-04-21 23:13:11 +00001132 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth45859692005-03-03 21:47:53 +00001133 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
1134 { //FP Setcc -> Select yay!
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001135
1136
Andrew Lenharth45859692005-03-03 21:47:53 +00001137 //for a cmp b: c = a - b;
1138 //a = b: c = 0
1139 //a < b: c < 0
1140 //a > b: c > 0
Misha Brukman4633f1c2005-04-21 23:13:11 +00001141
Andrew Lenharth45859692005-03-03 21:47:53 +00001142 bool invTest = false;
1143 unsigned Tmp3;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001144
Andrew Lenharth45859692005-03-03 21:47:53 +00001145 ConstantFPSDNode *CN;
1146 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
1147 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1148 Tmp3 = SelectExpr(SetCC->getOperand(0));
1149 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
1150 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1151 {
1152 Tmp3 = SelectExpr(SetCC->getOperand(1));
1153 invTest = true;
1154 }
1155 else
1156 {
1157 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
1158 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
1159 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
1160 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1161 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1162 .addReg(Tmp1).addReg(Tmp2);
1163 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001164
Andrew Lenharth45859692005-03-03 21:47:53 +00001165 switch (SetCC->getCondition()) {
1166 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
1167 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNE : Alpha::FCMOVEQ; break;
1168 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGT : Alpha::FCMOVLT; break;
1169 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGE : Alpha::FCMOVLE; break;
1170 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLT : Alpha::FCMOVGT; break;
1171 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLE : Alpha::FCMOVGE; break;
1172 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQ : Alpha::FCMOVNE; break;
1173 }
Andrew Lenharth33819132005-03-04 20:09:23 +00001174 BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
Andrew Lenharth45859692005-03-03 21:47:53 +00001175 return Result;
1176 }
1177 else
1178 {
1179 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001180 BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV).addReg(Tmp1);
1181// // Spill the cond to memory and reload it from there.
1182// unsigned Tmp4 = MakeReg(MVT::f64);
1183// MoveIntFP(Tmp1, Tmp4, true);
1184// //now ideally, we don't have to do anything to the flag...
1185// // Get the condition into the zero flag.
1186// BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
Andrew Lenharth45859692005-03-03 21:47:53 +00001187 return Result;
1188 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001189 }
1190
Andrew Lenharthc1faced2005-02-01 01:37:24 +00001191 case ISD::FP_ROUND:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001192 assert (DestType == MVT::f32 &&
1193 N.getOperand(0).getValueType() == MVT::f64 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001194 "only f64 to f32 conversion supported here");
Andrew Lenharthc1faced2005-02-01 01:37:24 +00001195 Tmp1 = SelectExpr(N.getOperand(0));
1196 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
1197 return Result;
1198
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001199 case ISD::FP_EXTEND:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001200 assert (DestType == MVT::f64 &&
1201 N.getOperand(0).getValueType() == MVT::f32 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001202 "only f32 to f64 conversion supported here");
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001203 Tmp1 = SelectExpr(N.getOperand(0));
1204 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
1205 return Result;
1206
Andrew Lenharth2c594352005-01-29 15:42:07 +00001207 case ISD::CopyFromReg:
1208 {
1209 // Make sure we generate both values.
1210 if (Result != notIn)
1211 ExprMap[N.getValue(1)] = notIn; // Generate the token
1212 else
1213 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001214
Andrew Lenharth2c594352005-01-29 15:42:07 +00001215 SDOperand Chain = N.getOperand(0);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001216
Andrew Lenharth2c594352005-01-29 15:42:07 +00001217 Select(Chain);
1218 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1219 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1220 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
1221 return Result;
1222 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001223
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001224 case ISD::LOAD:
1225 {
1226 // Make sure we generate both values.
1227 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001228 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001229 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001230 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001231
Andrew Lenharth29219162005-02-07 06:31:44 +00001232 DestType = N.getValue(0).getValueType();
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001233
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001234 SDOperand Chain = N.getOperand(0);
1235 SDOperand Address = N.getOperand(1);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001236 Select(Chain);
Andrew Lenharth65838902005-02-06 16:22:15 +00001237 Opc = DestType == MVT::f64 ? Alpha::LDT : Alpha::LDS;
1238
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +00001239 if (EnableAlphaLSMark)
1240 {
1241 int i = getValueOffset(dyn_cast<SrcValueSDNode>(N.getOperand(2))->getValue());
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001242 int j = getFunctionOffset(BB->getParent()->getFunction());
1243 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +00001244 }
1245
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001246 if (Address.getOpcode() == ISD::GlobalAddress) {
1247 AlphaLowering.restoreGP(BB);
1248 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001249 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001250 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1251 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001252 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001253 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001254 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001255 has_sym = true;
Andrew Lenharth97127a12005-02-05 17:41:39 +00001256 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001257 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001258 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001259 BuildMI(BB, Opc, 2, Result)
1260 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1261 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001262 } else {
1263 long offset;
1264 SelectAddr(Address, Tmp1, offset);
1265 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1266 }
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001267 return Result;
1268 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001269 case ISD::ConstantFP:
1270 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
1271 if (CN->isExactlyValue(+0.0)) {
1272 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001273 } else if ( CN->isExactlyValue(-0.0)) {
1274 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001275 } else {
1276 abort();
1277 }
1278 }
1279 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001280
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001281 case ISD::SDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001282 case ISD::MUL:
1283 case ISD::ADD:
1284 case ISD::SUB:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001285 switch( opcode ) {
1286 case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS; break;
1287 case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS; break;
1288 case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS; break;
1289 case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS; break;
1290 };
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001291
1292 ConstantFPSDNode *CN;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001293 if (opcode == ISD::SUB
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001294 && (CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
1295 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1296 {
1297 Tmp2 = SelectExpr(N.getOperand(1));
1298 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp2).addReg(Tmp2);
1299 } else {
1300 Tmp1 = SelectExpr(N.getOperand(0));
1301 Tmp2 = SelectExpr(N.getOperand(1));
1302 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1303 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001304 return Result;
1305
Andrew Lenharth2c594352005-01-29 15:42:07 +00001306 case ISD::EXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001307 {
1308 //include a conversion sequence for float loads to double
1309 if (Result != notIn)
1310 ExprMap[N.getValue(1)] = notIn; // Generate the token
1311 else
1312 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001313
Andrew Lenhartha549deb2005-02-07 05:33:15 +00001314 Tmp1 = MakeReg(MVT::f32);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001315
1316 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001317 "EXTLOAD not from f32");
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001318 assert(Node->getValueType(0) == MVT::f64 && "EXTLOAD not to f64");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001319
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001320 SDOperand Chain = N.getOperand(0);
1321 SDOperand Address = N.getOperand(1);
1322 Select(Chain);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001323
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001324 if (Address.getOpcode() == ISD::GlobalAddress) {
1325 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001326 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001327 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1328 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001329 else if (ConstantPoolSDNode *CP =
1330 dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001331 {
1332 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001333 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001334 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addConstantPoolIndex(CP->getIndex());
1335 }
1336 else if(Address.getOpcode() == ISD::FrameIndex) {
1337 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
Andrew Lenharth032f2352005-02-22 21:59:48 +00001338 BuildMI(BB, Alpha::LDS, 2, Tmp1)
1339 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1340 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001341 } else {
1342 long offset;
1343 SelectAddr(Address, Tmp2, offset);
1344 BuildMI(BB, Alpha::LDS, 1, Tmp1).addImm(offset).addReg(Tmp2);
1345 }
Andrew Lenharth29219162005-02-07 06:31:44 +00001346 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001347 return Result;
1348 }
Andrew Lenharth2c594352005-01-29 15:42:07 +00001349
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001350 case ISD::SINT_TO_FP:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001351 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001352 assert (N.getOperand(0).getValueType() == MVT::i64
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001353 && "only quads can be loaded from");
Andrew Lenharth40831c52005-01-28 06:57:18 +00001354 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001355 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001356 MoveInt2FP(Tmp1, Tmp2, true);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001357 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
1358 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001359 return Result;
1360 }
1361 }
1362 assert(0 && "should not get here");
1363 return 0;
1364}
1365
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001366unsigned AlphaISel::SelectExpr(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001367 unsigned Result;
Andrew Lenharth2966e842005-04-07 18:15:28 +00001368 unsigned Tmp1, Tmp2 = 0, Tmp3;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001369 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001370 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001371
1372 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001373 MVT::ValueType DestType = N.getValueType();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001374
1375 unsigned &Reg = ExprMap[N];
1376 if (Reg) return Reg;
1377
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001378 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::TAILCALL)
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001379 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001380 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001381 else {
1382 // If this is a call instruction, make sure to prepare ALL of the result
1383 // values as well as the chain.
1384 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001385 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001386 else {
1387 Result = MakeReg(Node->getValueType(0));
1388 ExprMap[N.getValue(0)] = Result;
1389 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1390 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001391 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001392 }
1393 }
1394
Andrew Lenharth50d91d72005-04-30 14:19:13 +00001395 if ((DestType == MVT::f64 || DestType == MVT::f32 ||
1396 (
1397 (opcode == ISD::LOAD || opcode == ISD::CopyFromReg ||
1398 opcode == ISD::EXTLOAD) &&
1399 (N.getValue(0).getValueType() == MVT::f32 ||
1400 N.getValue(0).getValueType() == MVT::f64)
1401 ))
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001402 && opcode != ISD::CALL && opcode != ISD::TAILCALL
Andrew Lenharth06342c32005-02-07 06:21:37 +00001403 )
Andrew Lenharth40831c52005-01-28 06:57:18 +00001404 return SelectExprFP(N, Result);
1405
1406 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001407 default:
1408 Node->dump();
1409 assert(0 && "Node not handled!\n");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001410
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001411 case ISD::CTPOP:
1412 case ISD::CTTZ:
1413 case ISD::CTLZ:
1414 Opc = opcode == ISD::CTPOP ? Alpha::CTPOP :
1415 (opcode == ISD::CTTZ ? Alpha::CTTZ : Alpha::CTLZ);
1416 Tmp1 = SelectExpr(N.getOperand(0));
1417 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1418 return Result;
1419
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001420 case ISD::MULHU:
1421 Tmp1 = SelectExpr(N.getOperand(0));
1422 Tmp2 = SelectExpr(N.getOperand(1));
1423 BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth706be912005-04-07 13:55:53 +00001424 return Result;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001425 case ISD::MULHS:
1426 {
1427 //MULHU - Ra<63>*Rb - Rb<63>*Ra
1428 Tmp1 = SelectExpr(N.getOperand(0));
1429 Tmp2 = SelectExpr(N.getOperand(1));
1430 Tmp3 = MakeReg(MVT::i64);
1431 BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1432 unsigned V1 = MakeReg(MVT::i64);
1433 unsigned V2 = MakeReg(MVT::i64);
1434 BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31).addReg(Tmp1);
1435 BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31).addReg(Tmp2);
1436 unsigned IRes = MakeReg(MVT::i64);
1437 BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
1438 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
1439 return Result;
1440 }
Andrew Lenharth7332f3e2005-04-02 19:11:07 +00001441 case ISD::UNDEF: {
1442 BuildMI(BB, Alpha::IDEF, 0, Result);
1443 return Result;
1444 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001445
Andrew Lenharth032f2352005-02-22 21:59:48 +00001446 case ISD::DYNAMIC_STACKALLOC:
1447 // Generate both result values.
Andrew Lenharth3a7118d2005-02-23 17:33:42 +00001448 if (Result != notIn)
1449 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth032f2352005-02-22 21:59:48 +00001450 else
1451 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1452
1453 // FIXME: We are currently ignoring the requested alignment for handling
1454 // greater than the stack alignment. This will need to be revisited at some
1455 // point. Align = N.getOperand(2);
1456
1457 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1458 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1459 std::cerr << "Cannot allocate stack object with greater alignment than"
1460 << " the stack alignment yet!";
1461 abort();
1462 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001463
Andrew Lenharth032f2352005-02-22 21:59:48 +00001464 Select(N.getOperand(0));
1465 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1466 {
1467 if (CN->getValue() < 32000)
1468 {
1469 BuildMI(BB, Alpha::LDA, 2, Alpha::R30)
1470 .addImm(-CN->getValue()).addReg(Alpha::R30);
1471 } else {
1472 Tmp1 = SelectExpr(N.getOperand(1));
1473 // Subtract size from stack pointer, thereby allocating some space.
1474 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1475 }
1476 } else {
1477 Tmp1 = SelectExpr(N.getOperand(1));
1478 // Subtract size from stack pointer, thereby allocating some space.
1479 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1480 }
1481
1482 // Put a pointer to the space into the result register, by copying the stack
1483 // pointer.
Andrew Lenharth7bc47022005-02-22 23:29:25 +00001484 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
Andrew Lenharth032f2352005-02-22 21:59:48 +00001485 return Result;
1486
Andrew Lenharth33819132005-03-04 20:09:23 +00001487// case ISD::ConstantPool:
1488// Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1489// AlphaLowering.restoreGP(BB);
1490// BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(Tmp1);
1491// return Result;
Andrew Lenharth2c594352005-01-29 15:42:07 +00001492
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001493 case ISD::FrameIndex:
Andrew Lenharth032f2352005-02-22 21:59:48 +00001494 BuildMI(BB, Alpha::LDA, 2, Result)
1495 .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
1496 .addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001497 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001498
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001499 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001500 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001501 case ISD::SEXTLOAD:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001502 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001503 {
1504 // Make sure we generate both values.
1505 if (Result != notIn)
1506 ExprMap[N.getValue(1)] = notIn; // Generate the token
1507 else
1508 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001509
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001510 SDOperand Chain = N.getOperand(0);
1511 SDOperand Address = N.getOperand(1);
1512 Select(Chain);
1513
Misha Brukman4633f1c2005-04-21 23:13:11 +00001514 assert(Node->getValueType(0) == MVT::i64 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001515 "Unknown type to sign extend to.");
Andrew Lenharth03824012005-02-07 05:55:55 +00001516 if (opcode == ISD::LOAD)
1517 Opc = Alpha::LDQ;
1518 else
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001519 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
1520 default: Node->dump(); assert(0 && "Bad sign extend!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001521 case MVT::i32: Opc = Alpha::LDL;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001522 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001523 case MVT::i16: Opc = Alpha::LDWU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001524 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001525 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Misha Brukman4633f1c2005-04-21 23:13:11 +00001526 case MVT::i8: Opc = Alpha::LDBU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001527 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001528 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001529
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001530 if (EnableAlphaLSMark)
1531 {
1532 int i = getValueOffset(dyn_cast<SrcValueSDNode>(N.getOperand(2))->getValue());
1533 int j = getFunctionOffset(BB->getParent()->getFunction());
1534 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
1535 }
1536
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001537 if (Address.getOpcode() == ISD::GlobalAddress) {
1538 AlphaLowering.restoreGP(BB);
1539 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001540 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001541 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1542 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001543 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1544 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001545 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001546 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001547 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001548 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001549 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001550 BuildMI(BB, Opc, 2, Result)
1551 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1552 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001553 } else {
1554 long offset;
1555 SelectAddr(Address, Tmp1, offset);
1556 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1557 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001558 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001559 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001560
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001561 case ISD::GlobalAddress:
1562 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001563 has_sym = true;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001564 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
1565 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
1566 return Result;
1567
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001568 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001569 case ISD::CALL:
1570 {
1571 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001572
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001573 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001574 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001575
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001576 //grab the arguments
1577 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001578 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001579 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001580 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001581
Andrew Lenharth684f2292005-01-30 00:35:27 +00001582 //in reg args
1583 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001584 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001585 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001586 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +00001587 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001588 Alpha::F19, Alpha::F20, Alpha::F21};
1589 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001590 default:
1591 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001592 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001593 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001594 N.getOperand(i+2).getValueType() << "\n";
1595 assert(0 && "Unknown value type for call");
1596 case MVT::i1:
1597 case MVT::i8:
1598 case MVT::i16:
1599 case MVT::i32:
1600 case MVT::i64:
1601 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1602 break;
1603 case MVT::f32:
1604 case MVT::f64:
1605 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1606 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001607 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001608 }
Andrew Lenharth684f2292005-01-30 00:35:27 +00001609 //in mem args
1610 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001611 {
1612 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001613 default:
1614 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001615 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001616 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001617 N.getOperand(i+2).getValueType() << "\n";
1618 assert(0 && "Unknown value type for call");
1619 case MVT::i1:
1620 case MVT::i8:
1621 case MVT::i16:
1622 case MVT::i32:
1623 case MVT::i64:
1624 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1625 break;
1626 case MVT::f32:
1627 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1628 break;
1629 case MVT::f64:
1630 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1631 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001632 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001633 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001634 //build the right kind of call
1635 if (GlobalAddressSDNode *GASD =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001636 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001637 {
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001638 if (GASD->getGlobal()->isExternal()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001639 //use safe calling convention
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001640 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001641 has_sym = true;
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001642 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal());
1643 } else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001644 //use PC relative branch call
Andrew Lenharth1e0d9bd2005-04-14 17:34:20 +00001645 AlphaLowering.restoreGP(BB);
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001646 BuildMI(BB, Alpha::BSR, 1, Alpha::R26).addGlobalAddress(GASD->getGlobal(),true);
1647 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001648 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001649 else if (ExternalSymbolSDNode *ESSDN =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001650 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001651 {
1652 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001653 has_sym = true;
Andrew Lenharthba05ad62005-03-30 18:22:52 +00001654 BuildMI(BB, Alpha::CALL, 1).addExternalSymbol(ESSDN->getSymbol(), true);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001655 } else {
1656 //no need to restore GP as we are doing an indirect call
1657 Tmp1 = SelectExpr(N.getOperand(1));
1658 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
1659 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
1660 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001661
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001662 //push the result into a virtual register
Misha Brukman4633f1c2005-04-21 23:13:11 +00001663
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001664 switch (Node->getValueType(0)) {
1665 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001666 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001667 case MVT::i1:
1668 case MVT::i8:
1669 case MVT::i16:
1670 case MVT::i32:
1671 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001672 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
1673 break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001674 case MVT::f32:
1675 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001676 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
1677 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001678 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001679 return Result+N.ResNo;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001680 }
1681
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001682 case ISD::SIGN_EXTEND_INREG:
1683 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001684 //do SDIV opt for all levels of ints if not dividing by a constant
1685 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
1686 && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001687 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001688 unsigned Tmp4 = MakeReg(MVT::f64);
1689 unsigned Tmp5 = MakeReg(MVT::f64);
1690 unsigned Tmp6 = MakeReg(MVT::f64);
1691 unsigned Tmp7 = MakeReg(MVT::f64);
1692 unsigned Tmp8 = MakeReg(MVT::f64);
1693 unsigned Tmp9 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001694
1695 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1696 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1697 MoveInt2FP(Tmp1, Tmp4, true);
1698 MoveInt2FP(Tmp2, Tmp5, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001699 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Tmp4);
1700 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Tmp5);
1701 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
1702 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Tmp8);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001703 MoveFP2Int(Tmp9, Result, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001704 return Result;
1705 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001706
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001707 //Alpha has instructions for a bunch of signed 32 bit stuff
1708 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001709 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001710 switch (N.getOperand(0).getOpcode()) {
1711 case ISD::ADD:
1712 case ISD::SUB:
1713 case ISD::MUL:
1714 {
1715 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
1716 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
1717 //FIXME: first check for Scaled Adds and Subs!
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001718 ConstantSDNode* CSD = NULL;
1719 if(!isMul && N.getOperand(0).getOperand(0).getOpcode() == ISD::SHL &&
1720 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(0).getOperand(1))) &&
1721 (CSD->getValue() == 2 || CSD->getValue() == 3))
1722 {
1723 bool use4 = CSD->getValue() == 2;
1724 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1725 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1726 BuildMI(BB, isAdd?(use4?Alpha::S4ADDL:Alpha::S8ADDL):(use4?Alpha::S4SUBL:Alpha::S8SUBL),
1727 2,Result).addReg(Tmp1).addReg(Tmp2);
1728 }
1729 else if(isAdd && N.getOperand(0).getOperand(1).getOpcode() == ISD::SHL &&
1730 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1).getOperand(1))) &&
1731 (CSD->getValue() == 2 || CSD->getValue() == 3))
1732 {
1733 bool use4 = CSD->getValue() == 2;
1734 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1735 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1736 BuildMI(BB, use4?Alpha::S4ADDL:Alpha::S8ADDL, 2,Result).addReg(Tmp1).addReg(Tmp2);
1737 }
1738 else if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001739 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
1740 { //Normal imm add/sub
1741 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001742 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001743 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
1744 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001745 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001746 else
1747 { //Normal add/sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001748 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULL : Alpha::SUBL);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001749 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001750 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001751 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1752 }
1753 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001754 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001755 default: break; //Fall Though;
1756 }
1757 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001758 Tmp1 = SelectExpr(N.getOperand(0));
1759 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001760 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001761 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001762 {
1763 default:
1764 Node->dump();
1765 assert(0 && "Sign Extend InReg not there yet");
1766 break;
1767 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001768 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001769 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001770 break;
1771 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001772 case MVT::i16:
1773 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
1774 break;
1775 case MVT::i8:
1776 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
1777 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +00001778 case MVT::i1:
1779 Tmp2 = MakeReg(MVT::i64);
1780 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001781 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +00001782 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001783 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001784 return Result;
1785 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001786
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001787 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001788 {
1789 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1790 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1791 bool isConst1 = false;
1792 bool isConst2 = false;
1793 int dir;
Misha Brukman7847fca2005-04-22 17:54:37 +00001794
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001795 //Tmp1 = SelectExpr(N.getOperand(0));
1796 if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001797 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
1798 isConst1 = true;
1799 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001800 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1801 isConst2 = true;
1802
1803 switch (SetCC->getCondition()) {
1804 default: Node->dump(); assert(0 && "Unknown integer comparison!");
1805 case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001806 case ISD::SETLT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001807 Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001808 case ISD::SETLE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001809 Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001810 case ISD::SETGT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001811 Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001812 case ISD::SETGE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001813 Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001814 case ISD::SETULT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001815 Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001816 case ISD::SETUGT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001817 Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001818 case ISD::SETULE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001819 Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001820 case ISD::SETUGE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001821 Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001822 case ISD::SETNE: {//Handle this one special
1823 //std::cerr << "Alpha does not have a setne.\n";
1824 //abort();
1825 Tmp1 = SelectExpr(N.getOperand(0));
1826 Tmp2 = SelectExpr(N.getOperand(1));
1827 Tmp3 = MakeReg(MVT::i64);
1828 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001829 //Remeber we have the Inv for this CC
1830 CCInvMap[N] = Tmp3;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001831 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001832 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001833 return Result;
1834 }
1835 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001836 if (dir == 1) {
1837 Tmp1 = SelectExpr(N.getOperand(0));
1838 if (isConst2) {
1839 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1840 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1841 } else {
1842 Tmp2 = SelectExpr(N.getOperand(1));
1843 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1844 }
1845 } else if (dir == 2) {
1846 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001847 if (isConst1) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001848 Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1849 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1850 } else {
1851 Tmp2 = SelectExpr(N.getOperand(0));
1852 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1853 }
1854 } else { //dir == 0
1855 if (isConst1) {
1856 Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1857 Tmp2 = SelectExpr(N.getOperand(1));
1858 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
1859 } else if (isConst2) {
1860 Tmp1 = SelectExpr(N.getOperand(0));
1861 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1862 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
1863 } else {
1864 Tmp1 = SelectExpr(N.getOperand(0));
1865 Tmp2 = SelectExpr(N.getOperand(1));
1866 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1867 }
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001868 }
1869 } else {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001870 //do the comparison
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001871 Tmp1 = MakeReg(MVT::f64);
1872 bool inv = SelectFPSetCC(N, Tmp1);
1873
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001874 //now arrange for Result (int) to have a 1 or 0
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001875 Tmp2 = MakeReg(MVT::i64);
1876 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001877 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001878 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001879 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001880 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001881 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001882 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001883
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001884 case ISD::CopyFromReg:
1885 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001886 ++count_ins;
1887
Andrew Lenharth40831c52005-01-28 06:57:18 +00001888 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001889 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001890 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001891 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001892 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001893
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001894 SDOperand Chain = N.getOperand(0);
1895
1896 Select(Chain);
1897 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1898 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1899 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1900 return Result;
1901 }
1902
Misha Brukman4633f1c2005-04-21 23:13:11 +00001903 //Most of the plain arithmetic and logic share the same form, and the same
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001904 //constant immediate test
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001905 case ISD::XOR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001906 //Match Not
1907 if (N.getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001908 cast<ConstantSDNode>(N.getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001909 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001910 Tmp1 = SelectExpr(N.getOperand(0));
1911 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1912 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001913 }
1914 //Fall through
1915 case ISD::AND:
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001916 //handle zap
1917 if (opcode == ISD::AND && N.getOperand(1).getOpcode() == ISD::Constant)
1918 {
1919 uint64_t k = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1920 unsigned int build = 0;
1921 for(int i = 0; i < 8; ++i)
1922 {
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001923 if ((k & 0x00FF) == 0x00FF)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001924 build |= 1 << i;
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001925 else if ((k & 0x00FF) != 0)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001926 { build = 0; break; }
1927 k >>= 8;
1928 }
1929 if (build)
1930 {
1931 Tmp1 = SelectExpr(N.getOperand(0));
1932 BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1933 return Result;
1934 }
1935 }
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001936 case ISD::OR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001937 //Check operand(0) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001938 if (N.getOperand(0).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001939 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001940 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001941 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001942 switch(opcode) {
1943 case ISD::AND: Opc = Alpha::BIC; break;
1944 case ISD::OR: Opc = Alpha::ORNOT; break;
1945 case ISD::XOR: Opc = Alpha::EQV; break;
1946 }
1947 Tmp1 = SelectExpr(N.getOperand(1));
1948 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1949 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1950 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001951 }
1952 //Check operand(1) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001953 if (N.getOperand(1).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001954 N.getOperand(1).getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001955 cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001956 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001957 switch(opcode) {
1958 case ISD::AND: Opc = Alpha::BIC; break;
1959 case ISD::OR: Opc = Alpha::ORNOT; break;
1960 case ISD::XOR: Opc = Alpha::EQV; break;
1961 }
1962 Tmp1 = SelectExpr(N.getOperand(0));
1963 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1964 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1965 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001966 }
1967 //Fall through
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001968 case ISD::SHL:
1969 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001970 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001971 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001972 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1973 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001974 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001975 {
1976 switch(opcode) {
1977 case ISD::AND: Opc = Alpha::ANDi; break;
1978 case ISD::OR: Opc = Alpha::BISi; break;
1979 case ISD::XOR: Opc = Alpha::XORi; break;
1980 case ISD::SHL: Opc = Alpha::SLi; break;
1981 case ISD::SRL: Opc = Alpha::SRLi; break;
1982 case ISD::SRA: Opc = Alpha::SRAi; break;
1983 case ISD::MUL: Opc = Alpha::MULQi; break;
1984 };
1985 Tmp1 = SelectExpr(N.getOperand(0));
1986 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1987 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1988 } else {
1989 switch(opcode) {
1990 case ISD::AND: Opc = Alpha::AND; break;
1991 case ISD::OR: Opc = Alpha::BIS; break;
1992 case ISD::XOR: Opc = Alpha::XOR; break;
1993 case ISD::SHL: Opc = Alpha::SL; break;
1994 case ISD::SRL: Opc = Alpha::SRL; break;
1995 case ISD::SRA: Opc = Alpha::SRA; break;
1996 case ISD::MUL: Opc = Alpha::MULQ; break;
1997 };
1998 Tmp1 = SelectExpr(N.getOperand(0));
1999 Tmp2 = SelectExpr(N.getOperand(1));
2000 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2001 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00002002 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002003
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002004 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002005 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00002006 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00002007 bool isAdd = opcode == ISD::ADD;
2008
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002009 //first check for Scaled Adds and Subs!
2010 //Valid for add and sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002011 ConstantSDNode* CSD = NULL;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002012 if(N.getOperand(0).getOpcode() == ISD::SHL &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002013 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
2014 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002015 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002016 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002017 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002018 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) && CSD->getValue() <= 255)
2019 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
2020 2, Result).addReg(Tmp2).addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00002021 else {
2022 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002023 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
2024 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00002025 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002026 }
2027 //Position prevents subs
Andrew Lenharth273a1f92005-04-07 14:18:13 +00002028 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002029 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) &&
2030 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002031 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002032 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002033 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002034 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(0))) && CSD->getValue() <= 255)
2035 BuildMI(BB, use4?Alpha::S4ADDQi:Alpha::S8ADDQi, 2, Result).addReg(Tmp2)
2036 .addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00002037 else {
2038 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002039 BuildMI(BB, use4?Alpha::S4ADDQ:Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00002040 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002041 }
2042 //small addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002043 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
2044 CSD->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002045 { //Normal imm add/sub
2046 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
2047 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002048 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CSD->getValue());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002049 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002050 //larger addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002051 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
2052 CSD->getSignExtended() <= 32767 &&
2053 CSD->getSignExtended() >= -32767)
Andrew Lenharth74d00d82005-03-02 17:23:03 +00002054 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002055 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002056 Tmp2 = (long)CSD->getSignExtended();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002057 if (!isAdd)
2058 Tmp2 = -Tmp2;
2059 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002060 }
2061 //give up and do the operation
2062 else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002063 //Normal add/sub
2064 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
2065 Tmp1 = SelectExpr(N.getOperand(0));
2066 Tmp2 = SelectExpr(N.getOperand(1));
2067 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2068 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002069 return Result;
2070 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002071
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00002072 case ISD::SDIV:
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002073 {
Andrew Lenhartha565c272005-04-06 22:03:13 +00002074 ConstantSDNode* CSD;
2075 //check if we can convert into a shift!
2076 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
2077 (int64_t)CSD->getSignExtended() != 0 &&
2078 ExactLog2(abs((int64_t)CSD->getSignExtended())) != 0)
2079 {
2080 unsigned k = ExactLog2(abs(CSD->getSignExtended()));
2081 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenhartha565c272005-04-06 22:03:13 +00002082 if (k == 1)
2083 Tmp2 = Tmp1;
2084 else
2085 {
2086 Tmp2 = MakeReg(MVT::i64);
2087 BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
2088 }
2089 Tmp3 = MakeReg(MVT::i64);
2090 BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
2091 unsigned Tmp4 = MakeReg(MVT::i64);
2092 BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
2093 if ((int64_t)CSD->getSignExtended() > 0)
2094 BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
2095 else
2096 {
2097 unsigned Tmp5 = MakeReg(MVT::i64);
2098 BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
2099 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
2100 }
2101 return Result;
2102 }
2103 }
2104 //Else fall through
2105
2106 case ISD::UDIV:
2107 {
2108 ConstantSDNode* CSD;
2109 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
2110 ((int64_t)CSD->getSignExtended() >= 2 ||
2111 (int64_t)CSD->getSignExtended() <= -2))
2112 {
2113 // If this is a divide by constant, we can emit code using some magic
2114 // constants to implement it as a multiply instead.
2115 ExprMap.erase(N);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002116 if (opcode == ISD::SDIV)
Andrew Lenhartha565c272005-04-06 22:03:13 +00002117 return SelectExpr(BuildSDIVSequence(N));
2118 else
2119 return SelectExpr(BuildUDIVSequence(N));
2120 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002121 }
2122 //else fall though
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002123 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +00002124 case ISD::SREM:
Misha Brukman4633f1c2005-04-21 23:13:11 +00002125 //FIXME: alpha really doesn't support any of these operations,
Andrew Lenharth40831c52005-01-28 06:57:18 +00002126 // the ops are expanded into special library calls with
2127 // special calling conventions
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002128 //Restore GP because it is a call after all...
Andrew Lenharth40831c52005-01-28 06:57:18 +00002129 switch(opcode) {
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00002130 case ISD::UREM: Opc = Alpha::REMQU; break;
2131 case ISD::SREM: Opc = Alpha::REMQ; break;
2132 case ISD::UDIV: Opc = Alpha::DIVQU; break;
2133 case ISD::SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002134 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002135 Tmp1 = SelectExpr(N.getOperand(0));
2136 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth33819132005-03-04 20:09:23 +00002137 //set up regs explicitly (helps Reg alloc)
2138 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002139 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00002140 AlphaLowering.restoreGP(BB);
Andrew Lenharth33819132005-03-04 20:09:23 +00002141 BuildMI(BB, Opc, 2).addReg(Alpha::R24).addReg(Alpha::R25);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002142 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002143 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002144
Andrew Lenharthe76797c2005-02-01 20:40:27 +00002145 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002146 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002147 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002148 assert (DestType == MVT::i64 && "only quads can be loaded to");
2149 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00002150 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002151 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002152 if (SrcType == MVT::f32)
Misha Brukman7847fca2005-04-22 17:54:37 +00002153 {
2154 Tmp2 = MakeReg(MVT::f64);
2155 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
2156 Tmp1 = Tmp2;
2157 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002158 Tmp2 = MakeReg(MVT::f64);
2159 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00002160 MoveFP2Int(Tmp2, Result, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002161
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002162 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002163 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002164
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002165 case ISD::SELECT:
2166 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00002167 //FIXME: look at parent to decide if intCC can be folded, or if setCC(FP) and can save stack use
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002168 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002169 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2170 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002171 // Get the condition into the zero flag.
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002172 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002173
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002174 SDOperand CC = N.getOperand(0);
2175 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
2176
Misha Brukman4633f1c2005-04-21 23:13:11 +00002177 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002178 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
2179 { //FP Setcc -> Int Select
Misha Brukman7847fca2005-04-22 17:54:37 +00002180 Tmp1 = MakeReg(MVT::f64);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002181 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2182 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Misha Brukman7847fca2005-04-22 17:54:37 +00002183 bool inv = SelectFPSetCC(CC, Tmp1);
2184 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
2185 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
2186 return Result;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002187 }
2188 if (CC.getOpcode() == ISD::SETCC) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002189 //Int SetCC -> Select
2190 //Dropping the CC is only useful if we are comparing to 0
2191 if((SetCC->getOperand(1).getOpcode() == ISD::Constant &&
2192 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0) ||
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002193 (SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2194 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0))
2195 {
2196 //figure out a few things
2197 bool LeftZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2198 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
2199 bool RightZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2200 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
2201 bool LeftConst = N.getOperand(1).getOpcode() == ISD::Constant &&
2202 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255;
2203 bool RightConst = N.getOperand(2).getOpcode() == ISD::Constant &&
2204 cast<ConstantSDNode>(N.getOperand(2))->getValue() <= 255;
2205 bool useImm = LeftConst || RightConst;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002206
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002207 //Fix up CC
2208 ISD::CondCode cCode= SetCC->getCondition();
2209 if (RightConst && !LeftConst) //Invert sense to get Imm field right
2210 cCode = ISD::getSetCCInverse(cCode, true);
2211 if (LeftZero && !RightZero) //Swap Operands
2212 cCode = ISD::getSetCCSwappedOperands(cCode);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002213
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002214 //Choose the CMOV
2215 switch (cCode) {
2216 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
2217 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
2218 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
2219 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
2220 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
2221 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
2222 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
2223 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2224 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break; //Technically you could have this CC
2225 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
2226 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2227 }
2228 if(LeftZero && !RightZero) //swap Operands
2229 Tmp1 = SelectExpr(SetCC->getOperand(1)); //Cond
2230 else
2231 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
2232
2233 if (LeftConst) {
2234 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2235 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
Misha Brukman7847fca2005-04-22 17:54:37 +00002236 .addImm(cast<ConstantSDNode>(N.getOperand(1))->getValue())
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002237 .addReg(Tmp1);
2238 } else if (RightConst) {
2239 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
2240 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
Misha Brukman7847fca2005-04-22 17:54:37 +00002241 .addImm(cast<ConstantSDNode>(N.getOperand(2))->getValue())
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002242 .addReg(Tmp1);
2243 } else {
2244 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2245 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2246 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
2247 }
2248 return Result;
2249 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002250 //Otherwise, fall though
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002251 }
2252 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002253 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2254 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthe76797c2005-02-01 20:40:27 +00002255 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002256
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002257 return Result;
2258 }
2259
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002260 case ISD::Constant:
2261 {
Andrew Lenharthc0513832005-03-29 19:24:04 +00002262 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002263 if (val <= IMM_HIGH && val >= IMM_LOW) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002264 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002265 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002266 else if (val <= (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT &&
2267 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
2268 Tmp1 = MakeReg(MVT::i64);
2269 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val)).addReg(Alpha::R31);
2270 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002271 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002272 else {
2273 MachineConstantPool *CP = BB->getParent()->getConstantPool();
2274 ConstantUInt *C = ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
2275 unsigned CPI = CP->getConstantPoolIndex(C);
2276 AlphaLowering.restoreGP(BB);
2277 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CPI);
2278 }
2279 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002280 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002281 }
2282
2283 return 0;
2284}
2285
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002286void AlphaISel::Select(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002287 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00002288 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002289
Nate Begeman85fdeb22005-03-24 04:39:54 +00002290 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002291 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002292
2293 SDNode *Node = N.Val;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002294
Andrew Lenharth760270d2005-02-07 23:02:23 +00002295 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002296
2297 default:
2298 Node->dump(); std::cerr << "\n";
2299 assert(0 && "Node not handled yet!");
2300
2301 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00002302 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002303 return;
2304 }
2305
2306 case ISD::BR: {
2307 MachineBasicBlock *Dest =
2308 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2309
2310 Select(N.getOperand(0));
2311 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
2312 return;
2313 }
2314
2315 case ISD::ImplicitDef:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002316 ++count_ins;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002317 Select(N.getOperand(0));
2318 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
2319 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002320
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002321 case ISD::EntryToken: return; // Noop
2322
2323 case ISD::TokenFactor:
2324 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2325 Select(Node->getOperand(i));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002326
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002327 //N.Val->dump(); std::cerr << "\n";
2328 //assert(0 && "Node not handled yet!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00002329
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002330 return;
2331
2332 case ISD::CopyToReg:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002333 ++count_outs;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002334 Select(N.getOperand(0));
2335 Tmp1 = SelectExpr(N.getOperand(1));
2336 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002337
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002338 if (Tmp1 != Tmp2) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002339 if (N.getOperand(1).getValueType() == MVT::f64 ||
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002340 N.getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00002341 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2342 else
2343 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002344 }
2345 return;
2346
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002347 case ISD::RET:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002348 ++count_outs;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002349 switch (N.getNumOperands()) {
2350 default:
2351 std::cerr << N.getNumOperands() << "\n";
2352 for (unsigned i = 0; i < N.getNumOperands(); ++i)
2353 std::cerr << N.getOperand(i).getValueType() << "\n";
2354 Node->dump();
2355 assert(0 && "Unknown return instruction!");
2356 case 2:
2357 Select(N.getOperand(0));
2358 Tmp1 = SelectExpr(N.getOperand(1));
2359 switch (N.getOperand(1).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002360 default: Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002361 assert(0 && "All other types should have been promoted!!");
2362 case MVT::f64:
2363 case MVT::f32:
2364 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
2365 break;
2366 case MVT::i32:
2367 case MVT::i64:
2368 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
2369 break;
2370 }
2371 break;
2372 case 1:
2373 Select(N.getOperand(0));
2374 break;
2375 }
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +00002376 AlphaLowering.restoreRA(BB);
2377 BuildMI(BB, Alpha::RET, 1, Alpha::R31).addReg(Alpha::R26); // Just emit a 'ret' instruction
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002378 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002379
Misha Brukman4633f1c2005-04-21 23:13:11 +00002380 case ISD::TRUNCSTORE:
2381 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002382 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002383 SDOperand Chain = N.getOperand(0);
2384 SDOperand Value = N.getOperand(1);
2385 SDOperand Address = N.getOperand(2);
2386 Select(Chain);
2387
2388 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00002389
2390 if (opcode == ISD::STORE) {
2391 switch(Value.getValueType()) {
2392 default: assert(0 && "unknown Type in store");
2393 case MVT::i64: Opc = Alpha::STQ; break;
2394 case MVT::f64: Opc = Alpha::STT; break;
2395 case MVT::f32: Opc = Alpha::STS; break;
2396 }
2397 } else { //ISD::TRUNCSTORE
2398 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2399 default: assert(0 && "unknown Type in store");
2400 case MVT::i1: //FIXME: DAG does not promote this load
2401 case MVT::i8: Opc = Alpha::STB; break;
2402 case MVT::i16: Opc = Alpha::STW; break;
2403 case MVT::i32: Opc = Alpha::STL; break;
2404 }
Andrew Lenharth65838902005-02-06 16:22:15 +00002405 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00002406
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002407 if (EnableAlphaLSMark)
2408 {
2409 int i = getValueOffset(dyn_cast<SrcValueSDNode>(N.getOperand(3))->getValue());
2410 int j = getFunctionOffset(BB->getParent()->getFunction());
2411 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
2412 }
2413
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002414 if (Address.getOpcode() == ISD::GlobalAddress)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002415 {
2416 AlphaLowering.restoreGP(BB);
2417 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002418 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002419 BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
2420 }
Andrew Lenharth05380342005-02-07 05:07:00 +00002421 else if(Address.getOpcode() == ISD::FrameIndex)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002422 {
Andrew Lenharth032f2352005-02-22 21:59:48 +00002423 BuildMI(BB, Opc, 3).addReg(Tmp1)
2424 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
2425 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002426 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002427 else
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002428 {
2429 long offset;
2430 SelectAddr(Address, Tmp2, offset);
2431 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2432 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002433 return;
2434 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002435
2436 case ISD::EXTLOAD:
2437 case ISD::SEXTLOAD:
2438 case ISD::ZEXTLOAD:
2439 case ISD::LOAD:
2440 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002441 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002442 case ISD::CALL:
Andrew Lenharth032f2352005-02-22 21:59:48 +00002443 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002444 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002445 SelectExpr(N);
2446 return;
2447
Chris Lattner16cd04d2005-05-12 23:24:06 +00002448 case ISD::CALLSEQ_START:
2449 case ISD::CALLSEQ_END:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002450 Select(N.getOperand(0));
2451 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002452
Chris Lattner16cd04d2005-05-12 23:24:06 +00002453 Opc = N.getOpcode() == ISD::CALLSEQ_START ? Alpha::ADJUSTSTACKDOWN :
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002454 Alpha::ADJUSTSTACKUP;
2455 BuildMI(BB, Opc, 1).addImm(Tmp1);
2456 return;
Andrew Lenharth95762122005-03-31 21:24:06 +00002457
2458 case ISD::PCMARKER:
2459 Select(N.getOperand(0)); //Chain
2460 BuildMI(BB, Alpha::PCLABEL, 2).addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
2461 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002462 }
2463 assert(0 && "Should not be reached!");
2464}
2465
2466
2467/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
2468/// into a machine code representation using pattern matching and a machine
2469/// description file.
2470///
2471FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002472 return new AlphaISel(TM);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002473}
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002474