blob: f2ed9862fedd31f5d44eef57c72b2fbfed342622 [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
79 public:
80 AlphaTargetLowering(TargetMachine &TM) : TargetLowering(TM) {
81 // Set up the TargetLowering object.
Andrew Lenharth3d65d312005-01-27 03:49:45 +000082 //I am having problems with shr n ubyte 1
Andrew Lenharth879ef222005-02-02 17:00:21 +000083 setShiftAmountType(MVT::i64);
84 setSetCCResultType(MVT::i64);
Andrew Lenharthd3355e22005-04-07 20:11:32 +000085 setSetCCResultContents(ZeroOrOneSetCCResult);
Misha Brukman4633f1c2005-04-21 23:13:11 +000086
Andrew Lenharth304d0f32005-01-22 23:41:55 +000087 addRegisterClass(MVT::i64, Alpha::GPRCRegisterClass);
88 addRegisterClass(MVT::f64, Alpha::FPRCRegisterClass);
Andrew Lenharth3d65d312005-01-27 03:49:45 +000089 addRegisterClass(MVT::f32, Alpha::FPRCRegisterClass);
Misha Brukman4633f1c2005-04-21 23:13:11 +000090
Chris Lattnerda4d4692005-04-09 03:22:37 +000091 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000092 setOperationAction(ISD::EXTLOAD , MVT::i1 , Promote);
93 setOperationAction(ISD::EXTLOAD , MVT::f32 , Promote);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000094
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000095 setOperationAction(ISD::ZEXTLOAD , MVT::i1 , Expand);
96 setOperationAction(ISD::ZEXTLOAD , MVT::i32 , Expand);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000097
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000098 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
99 setOperationAction(ISD::SEXTLOAD , MVT::i8 , Expand);
100 setOperationAction(ISD::SEXTLOAD , MVT::i16 , Expand);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000101
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000102 setOperationAction(ISD::SREM , MVT::f32 , Expand);
103 setOperationAction(ISD::SREM , MVT::f64 , Expand);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000104
Andrew Lenharth59009192005-05-04 19:12:09 +0000105 if (!EnableAlphaCT) {
106 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
107 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
Andrew Lenharthb5884d32005-05-04 19:25:37 +0000108 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
Andrew Lenharth59009192005-05-04 19:12:09 +0000109 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000110
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000111 //If this didn't legalize into a div....
112 // setOperationAction(ISD::SREM , MVT::i64, Expand);
113 // setOperationAction(ISD::UREM , MVT::i64, Expand);
114
115 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
116 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
117 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
Andrew Lenharth9818c052005-02-05 13:19:12 +0000118
Chris Lattner17234b72005-04-30 04:26:06 +0000119 // We don't support sin/cos/sqrt
120 setOperationAction(ISD::FSIN , MVT::f64, Expand);
121 setOperationAction(ISD::FCOS , MVT::f64, Expand);
122 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
123 setOperationAction(ISD::FSIN , MVT::f32, Expand);
124 setOperationAction(ISD::FCOS , MVT::f32, Expand);
125 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
126
Andrew Lenharth33819132005-03-04 20:09:23 +0000127 //Doesn't work yet
Chris Lattner17234b72005-04-30 04:26:06 +0000128 setOperationAction(ISD::SETCC, MVT::f32, Promote);
Andrew Lenharth572af902005-02-14 05:41:43 +0000129
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000130 //Try a couple things with a custom expander
131 //setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
132
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000133 computeRegisterProperties();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000134
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000135 addLegalFPImmediate(+0.0); //F31
136 addLegalFPImmediate(-0.0); //-F31
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000137 }
138
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000139 /// LowerOperation - Provide custom lowering hooks for some operations.
140 ///
141 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
142
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000143 /// LowerArguments - This hook must be implemented to indicate how we should
144 /// lower the arguments for the specified function, into the specified DAG.
145 virtual std::vector<SDOperand>
146 LowerArguments(Function &F, SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000147
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000148 /// LowerCallTo - This hook lowers an abstract call to a function into an
149 /// actual call.
150 virtual std::pair<SDOperand, SDOperand>
Chris Lattnerc57f6822005-05-12 19:56:45 +0000151 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattneradf6a962005-05-13 18:50:42 +0000152 bool isTailCall, SDOperand Callee, ArgListTy &Args,
153 SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000154
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000155 virtual std::pair<SDOperand, SDOperand>
Andrew Lenharth558bc882005-06-18 18:34:52 +0000156 LowerVAStart(SDOperand Chain, SelectionDAG &DAG, SDOperand Dest);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000157
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000158 virtual std::pair<SDOperand,SDOperand>
Andrew Lenharth558bc882005-06-18 18:34:52 +0000159 LowerVAArgNext(SDOperand Chain, SDOperand VAList,
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000160 const Type *ArgTy, SelectionDAG &DAG);
161
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000162 std::pair<SDOperand,SDOperand>
163 LowerVACopy(SDOperand Chain, SDOperand Src, SDOperand Dest,
164 SelectionDAG &DAG);
165
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000166 virtual std::pair<SDOperand, SDOperand>
167 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
168 SelectionDAG &DAG);
169
170 void restoreGP(MachineBasicBlock* BB)
171 {
172 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
173 }
174 };
175}
176
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000177/// LowerOperation - Provide custom lowering hooks for some operations.
178///
179SDOperand AlphaTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
180 MachineFunction &MF = DAG.getMachineFunction();
181 switch (Op.getOpcode()) {
182 default: assert(0 && "Should not custom lower this!");
Misha Brukmanb8ee91a2005-06-06 17:39:46 +0000183#if 0
184 case ISD::SINT_TO_FP:
185 {
186 assert (Op.getOperand(0).getValueType() == MVT::i64
187 && "only quads can be loaded from");
188 SDOperand SRC;
189 if (EnableAlphaFTOI)
190 {
191 std::vector<MVT::ValueType> RTs;
192 RTs.push_back(Op.getValueType());
193 std::vector<SDOperand> Ops;
194 Ops.push_back(Op.getOperand(0));
195 SRC = DAG.getNode(AlphaISD::ITOF, RTs, Ops);
196 } else {
197 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
198 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
199 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
200 Op.getOperand(0), StackSlot, DAG.getSrcValue(NULL));
201 SRC = DAG.getLoad(Op.getValueType(), Store.getValue(0), StackSlot,
202 DAG.getSrcValue(NULL));
203 }
204 std::vector<MVT::ValueType> RTs;
205 RTs.push_back(Op.getValueType());
206 std::vector<SDOperand> Ops;
207 Ops.push_back(SRC);
208 return DAG.getNode(AlphaISD::CVTQ, RTs, Ops);
209 }
210#endif
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000211 }
Misha Brukmanb8ee91a2005-06-06 17:39:46 +0000212 return SDOperand();
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000213}
214
215
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000216/// AddLiveIn - This helper function adds the specified physical register to the
217/// MachineFunction as a live in value. It also creates a corresponding virtual
218/// register for it.
219static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
220 TargetRegisterClass *RC) {
221 assert(RC->contains(PReg) && "Not the correct regclass!");
222 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
223 MF.addLiveIn(PReg, VReg);
224 return VReg;
225}
226
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000227//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
228
229//For now, just use variable size stack frame format
230
231//In a standard call, the first six items are passed in registers $16
232//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
233//of argument-to-register correspondence.) The remaining items are
234//collected in a memory argument list that is a naturally aligned
235//array of quadwords. In a standard call, this list, if present, must
236//be passed at 0(SP).
Misha Brukman7847fca2005-04-22 17:54:37 +0000237//7 ... n 0(SP) ... (n-7)*8(SP)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000238
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000239// //#define FP $15
240// //#define RA $26
241// //#define PV $27
242// //#define GP $29
243// //#define SP $30
Misha Brukman4633f1c2005-04-21 23:13:11 +0000244
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000245std::vector<SDOperand>
Misha Brukman4633f1c2005-04-21 23:13:11 +0000246AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000247{
248 std::vector<SDOperand> ArgValues;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000249
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000250 MachineFunction &MF = DAG.getMachineFunction();
Andrew Lenharth05380342005-02-07 05:07:00 +0000251 MachineFrameInfo*MFI = MF.getFrameInfo();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000252
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000253 MachineBasicBlock& BB = MF.front();
254
255 //Handle the return address
256 //BuildMI(&BB, Alpha::IDEF, 0, Alpha::R26);
257
Misha Brukman4633f1c2005-04-21 23:13:11 +0000258 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000259 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000260 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000261 Alpha::F19, Alpha::F20, Alpha::F21};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000262 int count = 0;
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000263
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000264 GP = AddLiveIn(MF, Alpha::R29, getRegClassFor(MVT::i64));
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000265
Chris Lattnere4d5c442005-03-15 04:54:21 +0000266 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000267 {
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000268 SDOperand argt;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000269 if (count < 6) {
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000270 unsigned Vreg;
271 MVT::ValueType VT = getValueType(I->getType());
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000272 switch (VT) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000273 default:
274 std::cerr << "Unknown Type " << VT << "\n";
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000275 abort();
276 case MVT::f64:
277 case MVT::f32:
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000278 args_float[count] = AddLiveIn(MF,args_float[count], getRegClassFor(VT));
279 argt = DAG.getCopyFromReg(args_float[count], VT, DAG.getRoot());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000280 break;
281 case MVT::i1:
282 case MVT::i8:
283 case MVT::i16:
284 case MVT::i32:
285 case MVT::i64:
Andrew Lenharth591ec572005-05-31 18:42:18 +0000286 args_int[count] = AddLiveIn(MF, args_int[count], getRegClassFor(MVT::i64));
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000287 argt = DAG.getCopyFromReg(args_int[count], VT, DAG.getRoot());
Andrew Lenharth14f30c92005-05-31 18:37:16 +0000288 if (VT != MVT::i64)
289 argt = DAG.getNode(ISD::TRUNCATE, VT, argt);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000290 break;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000291 }
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000292 DAG.setRoot(argt.getValue(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000293 } else { //more args
294 // Create the frame index object for this incoming parameter...
295 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000296
297 // Create the SelectionDAG nodes corresponding to a load
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000298 //from this parameter
299 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000300 argt = DAG.getLoad(getValueType(I->getType()),
301 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000302 }
Andrew Lenharth032f2352005-02-22 21:59:48 +0000303 ++count;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000304 ArgValues.push_back(argt);
305 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000306
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000307 // If the functions takes variable number of arguments, copy all regs to stack
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000308 if (F.isVarArg()) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000309 VarArgsOffset = count * 8;
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000310 std::vector<SDOperand> LS;
311 for (int i = 0; i < 6; ++i) {
312 if (args_int[i] < 1024)
313 args_int[i] = AddLiveIn(MF,args_int[i], getRegClassFor(MVT::i64));
314 SDOperand argt = DAG.getCopyFromReg(args_int[i], MVT::i64, DAG.getRoot());
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000315 int FI = MFI->CreateFixedObject(8, -8 * (6 - i));
Andrew Lenharth558bc882005-06-18 18:34:52 +0000316 if (i == 0) VarArgsBase = FI;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000317 SDOperand SDFI = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000318 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt, SDFI, DAG.getSrcValue(NULL)));
319
320 if (args_float[i] < 1024)
321 args_float[i] = AddLiveIn(MF,args_float[i], getRegClassFor(MVT::f64));
322 argt = DAG.getCopyFromReg(args_float[i], MVT::f64, DAG.getRoot());
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000323 FI = MFI->CreateFixedObject(8, - 8 * (12 - i));
324 SDFI = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000325 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt, SDFI, DAG.getSrcValue(NULL)));
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000326 }
327
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000328 //Set up a token factor with all the stack traffic
329 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, LS));
330 }
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000331
332 // Finally, inform the code generator which regs we return values in.
333 switch (getValueType(F.getReturnType())) {
334 default: assert(0 && "Unknown type!");
335 case MVT::isVoid: break;
336 case MVT::i1:
337 case MVT::i8:
338 case MVT::i16:
339 case MVT::i32:
340 case MVT::i64:
341 MF.addLiveOut(Alpha::R0);
342 break;
343 case MVT::f32:
344 case MVT::f64:
345 MF.addLiveOut(Alpha::F0);
346 break;
347 }
348
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000349 //return the arguments
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000350 return ArgValues;
351}
352
353std::pair<SDOperand, SDOperand>
354AlphaTargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000355 const Type *RetTy, bool isVarArg,
Chris Lattneradf6a962005-05-13 18:50:42 +0000356 unsigned CallingConv, bool isTailCall,
Misha Brukman7847fca2005-04-22 17:54:37 +0000357 SDOperand Callee, ArgListTy &Args,
358 SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000359 int NumBytes = 0;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000360 if (Args.size() > 6)
361 NumBytes = (Args.size() - 6) * 8;
362
Chris Lattner16cd04d2005-05-12 23:24:06 +0000363 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000364 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000365 std::vector<SDOperand> args_to_use;
366 for (unsigned i = 0, e = Args.size(); i != e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000367 {
368 switch (getValueType(Args[i].second)) {
369 default: assert(0 && "Unexpected ValueType for argument!");
370 case MVT::i1:
371 case MVT::i8:
372 case MVT::i16:
373 case MVT::i32:
374 // Promote the integer to 64 bits. If the input type is signed use a
375 // sign extend, otherwise use a zero extend.
376 if (Args[i].second->isSigned())
377 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
378 else
379 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
380 break;
381 case MVT::i64:
382 case MVT::f64:
383 case MVT::f32:
384 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000385 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000386 args_to_use.push_back(Args[i].first);
387 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000388
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000389 std::vector<MVT::ValueType> RetVals;
390 MVT::ValueType RetTyVT = getValueType(RetTy);
391 if (RetTyVT != MVT::isVoid)
392 RetVals.push_back(RetTyVT);
393 RetVals.push_back(MVT::Other);
394
Misha Brukman4633f1c2005-04-21 23:13:11 +0000395 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000396 Chain, Callee, args_to_use), 0);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000397 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner16cd04d2005-05-12 23:24:06 +0000398 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000399 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000400 return std::make_pair(TheCall, Chain);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000401}
402
403std::pair<SDOperand, SDOperand>
Andrew Lenharth558bc882005-06-18 18:34:52 +0000404AlphaTargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG, SDOperand Dest) {
405 // vastart just stores the address of the VarArgsBase and VarArgsOffset
406 SDOperand FR = DAG.getFrameIndex(VarArgsBase, MVT::i32);
407 SDOperand S1 = DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, Dest, DAG.getSrcValue(NULL));
408 SDOperand SA2 = DAG.getNode(ISD::ADD, MVT::i64, Dest, DAG.getConstant(8, MVT::i64));
409 SDOperand S2 = DAG.getNode(ISD::STORE, MVT::Other, S1,
410 DAG.getConstant(VarArgsOffset, MVT::i64), SA2,
411 DAG.getSrcValue(NULL));
412
413 return std::make_pair(S2, S2);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000414}
415
416std::pair<SDOperand,SDOperand> AlphaTargetLowering::
Andrew Lenharth558bc882005-06-18 18:34:52 +0000417LowerVAArgNext(SDOperand Chain, SDOperand VAList,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000418 const Type *ArgTy, SelectionDAG &DAG) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000419 //FIXME: For now, ignore FP
420 SDOperand Base = DAG.getLoad(MVT::i64, Chain, VAList, DAG.getSrcValue(NULL));
421 SDOperand Tmp = DAG.getNode(ISD::ADD, MVT::i64, VAList,
422 DAG.getConstant(8, MVT::i64));
423 SDOperand Offset = DAG.getLoad(MVT::i64, Chain, Tmp, DAG.getSrcValue(NULL));
424 SDOperand DataPtr = DAG.getNode(ISD::ADD, MVT::i64, Base, Offset);
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000425 if (ArgTy->isFloatingPoint())
426 {
427 //if fp && Offset < 6*8, then subtract 6*8 from DataPtr
428 SDOperand FPDataPtr = DAG.getNode(ISD::SUB, MVT::i64, DataPtr,
429 DAG.getConstant(8*6, MVT::i64));
430 SDOperand CC = DAG.getSetCC(ISD::SETLT, MVT::i64,
431 Offset, DAG.getConstant(8*6, MVT::i64));
432 DataPtr = DAG.getNode(ISD::SELECT, MVT::i64, CC, FPDataPtr, DataPtr);
433 }
434
435 SDOperand Result = DAG.getLoad(getValueType(ArgTy), Chain, DataPtr,
Andrew Lenharth558bc882005-06-18 18:34:52 +0000436 DAG.getSrcValue(NULL));
437 SDOperand NewOffset = DAG.getNode(ISD::ADD, MVT::i64, Offset,
438 DAG.getConstant(8, MVT::i64));
439 SDOperand Update = DAG.getNode(ISD::STORE, MVT::Other, Result, NewOffset,
440 Tmp, DAG.getSrcValue(NULL));
441 return std::make_pair(Result, Update);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000442}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000443
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000444std::pair<SDOperand,SDOperand> AlphaTargetLowering::
445LowerVACopy(SDOperand Chain, SDOperand Src, SDOperand Dest,
446 SelectionDAG &DAG) {
447 //Default to returning the input list
448 SDOperand Val = DAG.getLoad(getPointerTy(), Chain, Src, DAG.getSrcValue(NULL));
449 SDOperand Result = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
450 Val, Dest, DAG.getSrcValue(NULL));
451 SDOperand NP = DAG.getNode(ISD::ADD, MVT::i64, Src,
452 DAG.getConstant(8, MVT::i64));
453 Val = DAG.getLoad(MVT::i32, Chain, NP, DAG.getSrcValue(NULL));
454 Result = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
455 Val, Dest, DAG.getSrcValue(NULL));
456 return std::make_pair(Result, Result);
457}
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000458
459std::pair<SDOperand, SDOperand> AlphaTargetLowering::
460LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
461 SelectionDAG &DAG) {
462 abort();
463}
464
465
466
467
468
469namespace {
470
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000471//===--------------------------------------------------------------------===//
472/// ISel - Alpha specific code to select Alpha machine instructions for
473/// SelectionDAG operations.
474//===--------------------------------------------------------------------===//
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000475class AlphaISel : public SelectionDAGISel {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000476
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000477 /// AlphaLowering - This object fully describes how to lower LLVM code to an
478 /// Alpha-specific SelectionDAG.
479 AlphaTargetLowering AlphaLowering;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000480
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000481 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
482 // for sdiv and udiv until it is put into the future
483 // dag combiner.
484
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000485 /// ExprMap - As shared expressions are codegen'd, we keep track of which
486 /// vreg the value is produced in, so we only emit one copy of each compiled
487 /// tree.
488 static const unsigned notIn = (unsigned)(-1);
489 std::map<SDOperand, unsigned> ExprMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000490
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000491 //CCInvMap sometimes (SetNE) we have the inverse CC code for free
492 std::map<SDOperand, unsigned> CCInvMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000493
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000494 int count_ins;
495 int count_outs;
496 bool has_sym;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000497 int max_depth;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000498
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000499public:
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000500 AlphaISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering), AlphaLowering(TM)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000501 {}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000502
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000503 /// InstructionSelectBasicBlock - This callback is invoked by
504 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
505 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
Andrew Lenharth032f2352005-02-22 21:59:48 +0000506 DEBUG(BB->dump());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000507 count_ins = 0;
508 count_outs = 0;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000509 max_depth = 0;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000510 has_sym = false;
511
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000512 // Codegen the basic block.
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000513 ISelDAG = &DAG;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000514 max_depth = DAG.getRoot().getNodeDepth();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000515 Select(DAG.getRoot());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000516
517 if(has_sym)
518 ++count_ins;
519 if(EnableAlphaCount)
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000520 std::cerr << "COUNT: " << BB->getParent()->getFunction ()->getName() << " "
521 << BB->getNumber() << " "
522 << max_depth << " "
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000523 << count_ins << " "
524 << count_outs << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000525
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000526 // Clear state used for selection.
527 ExprMap.clear();
528 CCInvMap.clear();
529 }
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000530
531 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000532
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000533 unsigned SelectExpr(SDOperand N);
534 unsigned SelectExprFP(SDOperand N, unsigned Result);
535 void Select(SDOperand N);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000536
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000537 void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
538 void SelectBranchCC(SDOperand N);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000539 void MoveFP2Int(unsigned src, unsigned dst, bool isDouble);
540 void MoveInt2FP(unsigned src, unsigned dst, bool isDouble);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000541 //returns whether the sense of the comparison was inverted
542 bool SelectFPSetCC(SDOperand N, unsigned dst);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000543
544 // dag -> dag expanders for integer divide by constant
545 SDOperand BuildSDIVSequence(SDOperand N);
546 SDOperand BuildUDIVSequence(SDOperand N);
547
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000548};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000549}
550
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000551void AlphaISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000552 // If this function has live-in values, emit the copies from pregs to vregs at
553 // the top of the function, before anything else.
554 MachineBasicBlock *BB = MF.begin();
555 if (MF.livein_begin() != MF.livein_end()) {
556 SSARegMap *RegMap = MF.getSSARegMap();
557 for (MachineFunction::livein_iterator LI = MF.livein_begin(),
558 E = MF.livein_end(); LI != E; ++LI) {
559 const TargetRegisterClass *RC = RegMap->getRegClass(LI->second);
560 if (RC == Alpha::GPRCRegisterClass) {
561 BuildMI(BB, Alpha::BIS, 2, LI->second).addReg(LI->first).addReg(LI->first);
562 } else if (RC == Alpha::FPRCRegisterClass) {
563 BuildMI(BB, Alpha::CPYS, 2, LI->second).addReg(LI->first).addReg(LI->first);
564 } else {
565 assert(0 && "Unknown regclass!");
566 }
567 }
568 }
569}
570
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000571//Find the offset of the arg in it's parent's function
572static int getValueOffset(const Value* v)
573{
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000574 static int uniqneg = -1;
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000575 if (v == NULL)
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000576 return uniqneg--;
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000577
578 const Instruction* itarget = dyn_cast<Instruction>(v);
579 const BasicBlock* btarget = itarget->getParent();
580 const Function* ftarget = btarget->getParent();
581
582 //offset due to earlier BBs
583 int i = 0;
584 for(Function::const_iterator ii = ftarget->begin(); &*ii != btarget; ++ii)
585 i += ii->size();
586
587 for(BasicBlock::const_iterator ii = btarget->begin(); &*ii != itarget; ++ii)
588 ++i;
589
590 return i;
591}
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000592//Find the offset of the function in it's module
593static int getFunctionOffset(const Function* fun)
594{
595 const Module* M = fun->getParent();
596
597 //offset due to earlier BBs
598 int i = 0;
599 for(Module::const_iterator ii = M->begin(); &*ii != fun; ++ii)
600 ++i;
601
602 return i;
603}
604
605static int getUID()
606{
607 static int id = 0;
608 return ++id;
609}
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000610
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000611//Factorize a number using the list of constants
612static bool factorize(int v[], int res[], int size, uint64_t c)
613{
614 bool cont = true;
615 while (c != 1 && cont)
616 {
617 cont = false;
618 for(int i = 0; i < size; ++i)
619 {
620 if (c % v[i] == 0)
621 {
622 c /= v[i];
623 ++res[i];
624 cont=true;
625 }
626 }
627 }
628 return c == 1;
629}
630
631
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000632//Shamelessly adapted from PPC32
Misha Brukman4633f1c2005-04-21 23:13:11 +0000633// Structure used to return the necessary information to codegen an SDIV as
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000634// a multiply.
635struct ms {
636 int64_t m; // magic number
637 int64_t s; // shift amount
638};
639
640struct mu {
641 uint64_t m; // magic number
642 int64_t a; // add indicator
643 int64_t s; // shift amount
644};
645
646/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukman4633f1c2005-04-21 23:13:11 +0000647/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000648/// or -1.
649static struct ms magic(int64_t d) {
650 int64_t p;
651 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
652 const uint64_t two63 = 9223372036854775808ULL; // 2^63
653 struct ms mag;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000654
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000655 ad = abs(d);
656 t = two63 + ((uint64_t)d >> 63);
657 anc = t - 1 - t%ad; // absolute value of nc
Andrew Lenharth320174f2005-04-07 17:19:16 +0000658 p = 63; // initialize p
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000659 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
660 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
661 q2 = two63/ad; // initialize q2 = 2p/abs(d)
662 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
663 do {
664 p = p + 1;
665 q1 = 2*q1; // update q1 = 2p/abs(nc)
666 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
667 if (r1 >= anc) { // must be unsigned comparison
668 q1 = q1 + 1;
669 r1 = r1 - anc;
670 }
671 q2 = 2*q2; // update q2 = 2p/abs(d)
672 r2 = 2*r2; // update r2 = rem(2p/abs(d))
673 if (r2 >= ad) { // must be unsigned comparison
674 q2 = q2 + 1;
675 r2 = r2 - ad;
676 }
677 delta = ad - r2;
678 } while (q1 < delta || (q1 == delta && r1 == 0));
679
680 mag.m = q2 + 1;
681 if (d < 0) mag.m = -mag.m; // resulting magic number
682 mag.s = p - 64; // resulting shift
683 return mag;
684}
685
686/// magicu - calculate the magic numbers required to codegen an integer udiv as
687/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
688static struct mu magicu(uint64_t d)
689{
690 int64_t p;
691 uint64_t nc, delta, q1, r1, q2, r2;
692 struct mu magu;
693 magu.a = 0; // initialize "add" indicator
694 nc = - 1 - (-d)%d;
Andrew Lenharth320174f2005-04-07 17:19:16 +0000695 p = 63; // initialize p
696 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
697 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
698 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
699 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000700 do {
701 p = p + 1;
702 if (r1 >= nc - r1 ) {
703 q1 = 2*q1 + 1; // update q1
704 r1 = 2*r1 - nc; // update r1
705 }
706 else {
707 q1 = 2*q1; // update q1
708 r1 = 2*r1; // update r1
709 }
710 if (r2 + 1 >= d - r2) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000711 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000712 q2 = 2*q2 + 1; // update q2
713 r2 = 2*r2 + 1 - d; // update r2
714 }
715 else {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000716 if (q2 >= 0x8000000000000000ull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000717 q2 = 2*q2; // update q2
718 r2 = 2*r2 + 1; // update r2
719 }
720 delta = d - 1 - r2;
721 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
722 magu.m = q2 + 1; // resulting magic number
Andrew Lenharth320174f2005-04-07 17:19:16 +0000723 magu.s = p - 64; // resulting shift
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000724 return magu;
725}
726
727/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
728/// return a DAG expression to select that will generate the same value by
729/// multiplying by a magic number. See:
730/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000731SDOperand AlphaISel::BuildSDIVSequence(SDOperand N) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000732 int64_t d = (int64_t)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000733 ms magics = magic(d);
734 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000735 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000736 ISelDAG->getConstant(magics.m, MVT::i64));
737 // If d > 0 and m < 0, add the numerator
738 if (d > 0 && magics.m < 0)
739 Q = ISelDAG->getNode(ISD::ADD, MVT::i64, Q, N.getOperand(0));
740 // If d < 0 and m > 0, subtract the numerator.
741 if (d < 0 && magics.m > 0)
742 Q = ISelDAG->getNode(ISD::SUB, MVT::i64, Q, N.getOperand(0));
743 // Shift right algebraic if shift value is nonzero
744 if (magics.s > 0)
Misha Brukman4633f1c2005-04-21 23:13:11 +0000745 Q = ISelDAG->getNode(ISD::SRA, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000746 ISelDAG->getConstant(magics.s, MVT::i64));
747 // Extract the sign bit and add it to the quotient
Misha Brukman4633f1c2005-04-21 23:13:11 +0000748 SDOperand T =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000749 ISelDAG->getNode(ISD::SRL, MVT::i64, Q, ISelDAG->getConstant(63, MVT::i64));
750 return ISelDAG->getNode(ISD::ADD, MVT::i64, Q, T);
751}
752
753/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
754/// return a DAG expression to select that will generate the same value by
755/// multiplying by a magic number. See:
756/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000757SDOperand AlphaISel::BuildUDIVSequence(SDOperand N) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000758 unsigned d =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000759 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
760 mu magics = magicu(d);
761 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000762 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000763 ISelDAG->getConstant(magics.m, MVT::i64));
764 if (magics.a == 0) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000765 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000766 ISelDAG->getConstant(magics.s, MVT::i64));
767 } else {
768 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i64, N.getOperand(0), Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000769 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000770 ISelDAG->getConstant(1, MVT::i64));
771 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i64, NPQ, Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000772 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000773 ISelDAG->getConstant(magics.s-1, MVT::i64));
774 }
775 return Q;
776}
777
Andrew Lenhartha565c272005-04-06 22:03:13 +0000778//From PPC32
779/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
780/// returns zero when the input is not exactly a power of two.
781static unsigned ExactLog2(uint64_t Val) {
782 if (Val == 0 || (Val & (Val-1))) return 0;
783 unsigned Count = 0;
784 while (Val != 1) {
785 Val >>= 1;
786 ++Count;
787 }
788 return Count;
789}
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000790
791
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000792//These describe LDAx
Andrew Lenharthc0513832005-03-29 19:24:04 +0000793static const int IMM_LOW = -32768;
794static const int IMM_HIGH = 32767;
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000795static const int IMM_MULT = 65536;
796
797static long getUpper16(long l)
798{
799 long y = l / IMM_MULT;
800 if (l % IMM_MULT > IMM_HIGH)
801 ++y;
802 return y;
803}
804
805static long getLower16(long l)
806{
807 long h = getUpper16(l);
808 return l - h * IMM_MULT;
809}
810
Andrew Lenharth65838902005-02-06 16:22:15 +0000811static unsigned GetSymVersion(unsigned opcode)
812{
813 switch (opcode) {
814 default: assert(0 && "unknown load or store"); return 0;
815 case Alpha::LDQ: return Alpha::LDQ_SYM;
816 case Alpha::LDS: return Alpha::LDS_SYM;
817 case Alpha::LDT: return Alpha::LDT_SYM;
818 case Alpha::LDL: return Alpha::LDL_SYM;
819 case Alpha::LDBU: return Alpha::LDBU_SYM;
820 case Alpha::LDWU: return Alpha::LDWU_SYM;
821 case Alpha::LDW: return Alpha::LDW_SYM;
822 case Alpha::LDB: return Alpha::LDB_SYM;
823 case Alpha::STQ: return Alpha::STQ_SYM;
824 case Alpha::STS: return Alpha::STS_SYM;
825 case Alpha::STT: return Alpha::STT_SYM;
826 case Alpha::STL: return Alpha::STL_SYM;
827 case Alpha::STW: return Alpha::STW_SYM;
828 case Alpha::STB: return Alpha::STB_SYM;
829 }
830}
831
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000832void AlphaISel::MoveFP2Int(unsigned src, unsigned dst, bool isDouble)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000833{
834 unsigned Opc;
835 if (EnableAlphaFTOI) {
836 Opc = isDouble ? Alpha::FTOIT : Alpha::FTOIS;
837 BuildMI(BB, Opc, 1, dst).addReg(src);
838 } else {
839 //The hard way:
840 // Spill the integer to memory and reload it from there.
841 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
842 MachineFunction *F = BB->getParent();
843 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
844
845 Opc = isDouble ? Alpha::STT : Alpha::STS;
846 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
847 Opc = isDouble ? Alpha::LDQ : Alpha::LDL;
848 BuildMI(BB, Alpha::LDQ, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
849 }
850}
851
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000852void AlphaISel::MoveInt2FP(unsigned src, unsigned dst, bool isDouble)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000853{
854 unsigned Opc;
855 if (EnableAlphaFTOI) {
856 Opc = isDouble?Alpha::ITOFT:Alpha::ITOFS;
857 BuildMI(BB, Opc, 1, dst).addReg(src);
858 } else {
859 //The hard way:
860 // Spill the integer to memory and reload it from there.
861 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
862 MachineFunction *F = BB->getParent();
863 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
864
865 Opc = isDouble ? Alpha::STQ : Alpha::STL;
866 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
867 Opc = isDouble ? Alpha::LDT : Alpha::LDS;
868 BuildMI(BB, Opc, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
869 }
870}
871
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000872bool AlphaISel::SelectFPSetCC(SDOperand N, unsigned dst)
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000873{
874 SDNode *Node = N.Val;
875 unsigned Opc, Tmp1, Tmp2, Tmp3;
876 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node);
877
878 //assert(SetCC->getOperand(0).getValueType() != MVT::f32 && "SetCC f32 should have been promoted");
879 bool rev = false;
880 bool inv = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000881
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000882 switch (SetCC->getCondition()) {
883 default: Node->dump(); assert(0 && "Unknown FP comparison!");
884 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
885 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
886 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
887 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
888 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
889 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
890 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000891
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000892 //FIXME: check for constant 0.0
893 ConstantFPSDNode *CN;
894 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
895 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
896 Tmp1 = Alpha::F31;
897 else
898 Tmp1 = SelectExpr(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000899
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000900 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
901 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
902 Tmp2 = Alpha::F31;
903 else
Chris Lattner9c9183a2005-04-30 04:44:07 +0000904 Tmp2 = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000905
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000906 //Can only compare doubles, and dag won't promote for me
907 if (SetCC->getOperand(0).getValueType() == MVT::f32)
908 {
909 //assert(0 && "Setcc On float?\n");
910 std::cerr << "Setcc on float!\n";
911 Tmp3 = MakeReg(MVT::f64);
912 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
913 Tmp1 = Tmp3;
914 }
915 if (SetCC->getOperand(1).getValueType() == MVT::f32)
916 {
917 //assert (0 && "Setcc On float?\n");
918 std::cerr << "Setcc on float!\n";
919 Tmp3 = MakeReg(MVT::f64);
920 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp2);
921 Tmp2 = Tmp3;
922 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000923
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000924 if (rev) std::swap(Tmp1, Tmp2);
925 //do the comparison
926 BuildMI(BB, Opc, 2, dst).addReg(Tmp1).addReg(Tmp2);
927 return inv;
928}
929
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000930//Check to see if the load is a constant offset from a base register
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000931void AlphaISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000932{
933 unsigned opcode = N.getOpcode();
934 if (opcode == ISD::ADD) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000935 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000936 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
937 { //Normal imm add
938 Reg = SelectExpr(N.getOperand(0));
939 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
940 return;
941 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000942 else if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000943 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 32767)
944 {
945 Reg = SelectExpr(N.getOperand(1));
946 offset = cast<ConstantSDNode>(N.getOperand(0))->getValue();
947 return;
948 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000949 }
950 Reg = SelectExpr(N);
951 offset = 0;
952 return;
953}
954
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000955void AlphaISel::SelectBranchCC(SDOperand N)
Andrew Lenharth445171a2005-02-08 00:40:03 +0000956{
957 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000958 MachineBasicBlock *Dest =
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000959 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
960 unsigned Opc = Alpha::WTF;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000961
Andrew Lenharth445171a2005-02-08 00:40:03 +0000962 Select(N.getOperand(0)); //chain
963 SDOperand CC = N.getOperand(1);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000964
Andrew Lenharth445171a2005-02-08 00:40:03 +0000965 if (CC.getOpcode() == ISD::SETCC)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000966 {
967 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
968 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
969 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000970 bool LeftZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
971 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
Andrew Lenharth09552bf2005-06-08 18:02:21 +0000972 bool RightZero = SetCC->getOperand(1).getOpcode() == ISD::Constant &&
973 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000974 bool isNE = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000975
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000976 //Fix up CC
977 ISD::CondCode cCode= SetCC->getCondition();
978 if (LeftZero && !RightZero) //Swap Operands
979 cCode = ISD::getSetCCSwappedOperands(cCode);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000980
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000981 if(cCode == ISD::SETNE)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000982 isNE = true;
Andrew Lenharth445171a2005-02-08 00:40:03 +0000983
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000984 if (LeftZero || RightZero) {
Andrew Lenharth09552bf2005-06-08 18:02:21 +0000985 switch (cCode) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000986 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
987 case ISD::SETEQ: Opc = Alpha::BEQ; break;
988 case ISD::SETLT: Opc = Alpha::BLT; break;
989 case ISD::SETLE: Opc = Alpha::BLE; break;
990 case ISD::SETGT: Opc = Alpha::BGT; break;
991 case ISD::SETGE: Opc = Alpha::BGE; break;
992 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
993 case ISD::SETUGT: Opc = Alpha::BNE; break;
994 case ISD::SETULE: Opc = Alpha::BEQ; break; //Technically you could have this CC
995 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
996 case ISD::SETNE: Opc = Alpha::BNE; break;
997 }
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000998 unsigned Tmp1;
999 if(LeftZero && !RightZero) //swap Operands
1000 Tmp1 = SelectExpr(SetCC->getOperand(1)); //Cond
1001 else
1002 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001003 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
1004 return;
1005 } else {
1006 unsigned Tmp1 = SelectExpr(CC);
1007 if (isNE)
1008 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
1009 else
1010 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001011 return;
1012 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001013 } else { //FP
1014 //Any comparison between 2 values should be codegened as an folded branch, as moving
1015 //CC to the integer register is very expensive
1016 //for a cmp b: c = a - b;
1017 //a = b: c = 0
1018 //a < b: c < 0
1019 //a > b: c > 0
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001020
1021 bool invTest = false;
1022 unsigned Tmp3;
1023
1024 ConstantFPSDNode *CN;
1025 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
1026 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1027 Tmp3 = SelectExpr(SetCC->getOperand(0));
1028 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
1029 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1030 {
1031 Tmp3 = SelectExpr(SetCC->getOperand(1));
1032 invTest = true;
1033 }
1034 else
1035 {
1036 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
1037 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
1038 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
1039 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1040 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1041 .addReg(Tmp1).addReg(Tmp2);
1042 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001043
1044 switch (SetCC->getCondition()) {
1045 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001046 case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
1047 case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
1048 case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
1049 case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
1050 case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
1051 case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001052 }
1053 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001054 return;
1055 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001056 abort(); //Should never be reached
1057 } else {
1058 //Giveup and do the stupid thing
1059 unsigned Tmp1 = SelectExpr(CC);
1060 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
1061 return;
1062 }
Andrew Lenharth445171a2005-02-08 00:40:03 +00001063 abort(); //Should never be reached
1064}
1065
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001066unsigned AlphaISel::SelectExprFP(SDOperand N, unsigned Result)
Andrew Lenharth40831c52005-01-28 06:57:18 +00001067{
1068 unsigned Tmp1, Tmp2, Tmp3;
1069 unsigned Opc = 0;
1070 SDNode *Node = N.Val;
1071 MVT::ValueType DestType = N.getValueType();
1072 unsigned opcode = N.getOpcode();
1073
1074 switch (opcode) {
1075 default:
1076 Node->dump();
1077 assert(0 && "Node not handled!\n");
Andrew Lenharth2c594352005-01-29 15:42:07 +00001078
Andrew Lenharth7332f3e2005-04-02 19:11:07 +00001079 case ISD::UNDEF: {
1080 BuildMI(BB, Alpha::IDEF, 0, Result);
1081 return Result;
1082 }
1083
Andrew Lenharth30b46d42005-04-02 19:04:58 +00001084 case ISD::FNEG:
1085 if(ISD::FABS == N.getOperand(0).getOpcode())
1086 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001087 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1088 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +00001089 } else {
Misha Brukman7847fca2005-04-22 17:54:37 +00001090 Tmp1 = SelectExpr(N.getOperand(0));
1091 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +00001092 }
1093 return Result;
1094
1095 case ISD::FABS:
1096 Tmp1 = SelectExpr(N.getOperand(0));
1097 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
1098 return Result;
1099
Andrew Lenharth9818c052005-02-05 13:19:12 +00001100 case ISD::SELECT:
1101 {
Andrew Lenharth45859692005-03-03 21:47:53 +00001102 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1103 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
1104 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
1105
1106 SDOperand CC = N.getOperand(0);
1107 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
1108
Misha Brukman4633f1c2005-04-21 23:13:11 +00001109 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth45859692005-03-03 21:47:53 +00001110 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
1111 { //FP Setcc -> Select yay!
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001112
1113
Andrew Lenharth45859692005-03-03 21:47:53 +00001114 //for a cmp b: c = a - b;
1115 //a = b: c = 0
1116 //a < b: c < 0
1117 //a > b: c > 0
Misha Brukman4633f1c2005-04-21 23:13:11 +00001118
Andrew Lenharth45859692005-03-03 21:47:53 +00001119 bool invTest = false;
1120 unsigned Tmp3;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001121
Andrew Lenharth45859692005-03-03 21:47:53 +00001122 ConstantFPSDNode *CN;
1123 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
1124 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1125 Tmp3 = SelectExpr(SetCC->getOperand(0));
1126 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
1127 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1128 {
1129 Tmp3 = SelectExpr(SetCC->getOperand(1));
1130 invTest = true;
1131 }
1132 else
1133 {
1134 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
1135 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
1136 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
1137 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1138 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1139 .addReg(Tmp1).addReg(Tmp2);
1140 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001141
Andrew Lenharth45859692005-03-03 21:47:53 +00001142 switch (SetCC->getCondition()) {
1143 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
1144 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNE : Alpha::FCMOVEQ; break;
1145 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGT : Alpha::FCMOVLT; break;
1146 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGE : Alpha::FCMOVLE; break;
1147 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLT : Alpha::FCMOVGT; break;
1148 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLE : Alpha::FCMOVGE; break;
1149 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQ : Alpha::FCMOVNE; break;
1150 }
Andrew Lenharth33819132005-03-04 20:09:23 +00001151 BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
Andrew Lenharth45859692005-03-03 21:47:53 +00001152 return Result;
1153 }
1154 else
1155 {
1156 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001157 BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV).addReg(Tmp1);
1158// // Spill the cond to memory and reload it from there.
1159// unsigned Tmp4 = MakeReg(MVT::f64);
1160// MoveIntFP(Tmp1, Tmp4, true);
1161// //now ideally, we don't have to do anything to the flag...
1162// // Get the condition into the zero flag.
1163// BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
Andrew Lenharth45859692005-03-03 21:47:53 +00001164 return Result;
1165 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001166 }
1167
Andrew Lenharthc1faced2005-02-01 01:37:24 +00001168 case ISD::FP_ROUND:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001169 assert (DestType == MVT::f32 &&
1170 N.getOperand(0).getValueType() == MVT::f64 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001171 "only f64 to f32 conversion supported here");
Andrew Lenharthc1faced2005-02-01 01:37:24 +00001172 Tmp1 = SelectExpr(N.getOperand(0));
1173 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
1174 return Result;
1175
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001176 case ISD::FP_EXTEND:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001177 assert (DestType == MVT::f64 &&
1178 N.getOperand(0).getValueType() == MVT::f32 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001179 "only f32 to f64 conversion supported here");
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001180 Tmp1 = SelectExpr(N.getOperand(0));
1181 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
1182 return Result;
1183
Andrew Lenharth2c594352005-01-29 15:42:07 +00001184 case ISD::CopyFromReg:
1185 {
1186 // Make sure we generate both values.
1187 if (Result != notIn)
1188 ExprMap[N.getValue(1)] = notIn; // Generate the token
1189 else
1190 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001191
Andrew Lenharth2c594352005-01-29 15:42:07 +00001192 SDOperand Chain = N.getOperand(0);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001193
Andrew Lenharth2c594352005-01-29 15:42:07 +00001194 Select(Chain);
1195 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1196 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1197 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
1198 return Result;
1199 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001200
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001201 case ISD::LOAD:
1202 {
1203 // Make sure we generate both values.
1204 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001205 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001206 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001207 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001208
Andrew Lenharth29219162005-02-07 06:31:44 +00001209 DestType = N.getValue(0).getValueType();
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001210
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001211 SDOperand Chain = N.getOperand(0);
1212 SDOperand Address = N.getOperand(1);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001213 Select(Chain);
Andrew Lenharth65838902005-02-06 16:22:15 +00001214 Opc = DestType == MVT::f64 ? Alpha::LDT : Alpha::LDS;
1215
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +00001216 if (EnableAlphaLSMark)
1217 {
1218 int i = getValueOffset(dyn_cast<SrcValueSDNode>(N.getOperand(2))->getValue());
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001219 int j = getFunctionOffset(BB->getParent()->getFunction());
1220 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +00001221 }
1222
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001223 if (Address.getOpcode() == ISD::GlobalAddress) {
1224 AlphaLowering.restoreGP(BB);
1225 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001226 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001227 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1228 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001229 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001230 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001231 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001232 has_sym = true;
Andrew Lenharth97127a12005-02-05 17:41:39 +00001233 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001234 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001235 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001236 BuildMI(BB, Opc, 2, Result)
1237 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1238 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001239 } else {
1240 long offset;
1241 SelectAddr(Address, Tmp1, offset);
1242 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1243 }
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001244 return Result;
1245 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001246 case ISD::ConstantFP:
1247 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
1248 if (CN->isExactlyValue(+0.0)) {
1249 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001250 } else if ( CN->isExactlyValue(-0.0)) {
1251 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001252 } else {
1253 abort();
1254 }
1255 }
1256 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001257
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001258 case ISD::SDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001259 case ISD::MUL:
1260 case ISD::ADD:
1261 case ISD::SUB:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001262 switch( opcode ) {
1263 case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS; break;
1264 case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS; break;
1265 case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS; break;
1266 case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS; break;
1267 };
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001268
1269 ConstantFPSDNode *CN;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001270 if (opcode == ISD::SUB
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001271 && (CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
1272 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1273 {
1274 Tmp2 = SelectExpr(N.getOperand(1));
1275 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp2).addReg(Tmp2);
1276 } else {
1277 Tmp1 = SelectExpr(N.getOperand(0));
1278 Tmp2 = SelectExpr(N.getOperand(1));
1279 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1280 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001281 return Result;
1282
Andrew Lenharth2c594352005-01-29 15:42:07 +00001283 case ISD::EXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001284 {
1285 //include a conversion sequence for float loads to double
1286 if (Result != notIn)
1287 ExprMap[N.getValue(1)] = notIn; // Generate the token
1288 else
1289 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001290
Andrew Lenhartha549deb2005-02-07 05:33:15 +00001291 Tmp1 = MakeReg(MVT::f32);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001292
1293 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001294 "EXTLOAD not from f32");
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001295 assert(Node->getValueType(0) == MVT::f64 && "EXTLOAD not to f64");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001296
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001297 SDOperand Chain = N.getOperand(0);
1298 SDOperand Address = N.getOperand(1);
1299 Select(Chain);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001300
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001301 if (Address.getOpcode() == ISD::GlobalAddress) {
1302 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001303 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001304 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1305 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001306 else if (ConstantPoolSDNode *CP =
1307 dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001308 {
1309 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001310 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001311 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addConstantPoolIndex(CP->getIndex());
1312 }
1313 else if(Address.getOpcode() == ISD::FrameIndex) {
1314 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
Andrew Lenharth032f2352005-02-22 21:59:48 +00001315 BuildMI(BB, Alpha::LDS, 2, Tmp1)
1316 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1317 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001318 } else {
1319 long offset;
1320 SelectAddr(Address, Tmp2, offset);
1321 BuildMI(BB, Alpha::LDS, 1, Tmp1).addImm(offset).addReg(Tmp2);
1322 }
Andrew Lenharth29219162005-02-07 06:31:44 +00001323 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001324 return Result;
1325 }
Andrew Lenharth2c594352005-01-29 15:42:07 +00001326
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001327 case ISD::UINT_TO_FP:
Andrew Lenharth69520ed2005-05-26 18:18:34 +00001328 {
1329 //FIXME: First test if we will have problems with the sign bit before doing the slow thing
1330 assert (N.getOperand(0).getValueType() == MVT::i64
1331 && "only quads can be loaded from");
1332 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1333 Tmp2 = MakeReg(MVT::i64);
1334 BuildMI(BB, Alpha::SRL, 2, Tmp2).addReg(Tmp1).addImm(1);
1335 Tmp3 = MakeReg(MVT::i64);
1336 BuildMI(BB, Alpha::CMPLT, 2, Tmp3).addReg(Tmp1).addReg(Alpha::R31);
1337 unsigned Tmp4 = MakeReg(MVT::f64), Tmp5 = MakeReg(MVT::f64), Tmp6 = MakeReg(MVT::f64);
1338 MoveInt2FP(Tmp1, Tmp4, true);
1339 MoveInt2FP(Tmp2, Tmp5, true);
1340 MoveInt2FP(Tmp3, Tmp6, true);
1341 Tmp1 = MakeReg(MVT::f64);
1342 Tmp2 = MakeReg(MVT::f64);
1343 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
1344 BuildMI(BB, Opc, 1, Tmp1).addReg(Tmp4);
1345 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp5);
1346 Tmp3 = MakeReg(MVT::f64);
1347 BuildMI(BB, Alpha::ADDT, 2, Tmp3).addReg(Tmp2).addReg(Tmp2);
1348 //Ok, now tmp1 had the plain covereted
1349 //tmp3 has the reduced converted and added
1350 //tmp6 has the conditional to use
1351 BuildMI(BB, Alpha::FCMOVNE, 3, Result).addReg(Tmp1).addReg(Tmp3).addReg(Tmp6);
1352 return Result;
1353 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001354 case ISD::SINT_TO_FP:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001355 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001356 assert (N.getOperand(0).getValueType() == MVT::i64
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001357 && "only quads can be loaded from");
Andrew Lenharth40831c52005-01-28 06:57:18 +00001358 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001359 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001360 MoveInt2FP(Tmp1, Tmp2, true);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001361 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
1362 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001363 return Result;
1364 }
1365 }
1366 assert(0 && "should not get here");
1367 return 0;
1368}
1369
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001370unsigned AlphaISel::SelectExpr(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001371 unsigned Result;
Andrew Lenharth2966e842005-04-07 18:15:28 +00001372 unsigned Tmp1, Tmp2 = 0, Tmp3;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001373 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001374 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001375
1376 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001377 MVT::ValueType DestType = N.getValueType();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001378
1379 unsigned &Reg = ExprMap[N];
1380 if (Reg) return Reg;
1381
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001382 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::TAILCALL)
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001383 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001384 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001385 else {
1386 // If this is a call instruction, make sure to prepare ALL of the result
1387 // values as well as the chain.
1388 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001389 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001390 else {
1391 Result = MakeReg(Node->getValueType(0));
1392 ExprMap[N.getValue(0)] = Result;
1393 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1394 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001395 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001396 }
1397 }
1398
Andrew Lenharth50d91d72005-04-30 14:19:13 +00001399 if ((DestType == MVT::f64 || DestType == MVT::f32 ||
1400 (
1401 (opcode == ISD::LOAD || opcode == ISD::CopyFromReg ||
1402 opcode == ISD::EXTLOAD) &&
1403 (N.getValue(0).getValueType() == MVT::f32 ||
1404 N.getValue(0).getValueType() == MVT::f64)
1405 ))
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001406 && opcode != ISD::CALL && opcode != ISD::TAILCALL
Andrew Lenharth06342c32005-02-07 06:21:37 +00001407 )
Andrew Lenharth40831c52005-01-28 06:57:18 +00001408 return SelectExprFP(N, Result);
1409
1410 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001411 default:
1412 Node->dump();
1413 assert(0 && "Node not handled!\n");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001414
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001415 case ISD::CTPOP:
1416 case ISD::CTTZ:
1417 case ISD::CTLZ:
1418 Opc = opcode == ISD::CTPOP ? Alpha::CTPOP :
1419 (opcode == ISD::CTTZ ? Alpha::CTTZ : Alpha::CTLZ);
1420 Tmp1 = SelectExpr(N.getOperand(0));
1421 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1422 return Result;
1423
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001424 case ISD::MULHU:
1425 Tmp1 = SelectExpr(N.getOperand(0));
1426 Tmp2 = SelectExpr(N.getOperand(1));
1427 BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth706be912005-04-07 13:55:53 +00001428 return Result;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001429 case ISD::MULHS:
1430 {
1431 //MULHU - Ra<63>*Rb - Rb<63>*Ra
1432 Tmp1 = SelectExpr(N.getOperand(0));
1433 Tmp2 = SelectExpr(N.getOperand(1));
1434 Tmp3 = MakeReg(MVT::i64);
1435 BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1436 unsigned V1 = MakeReg(MVT::i64);
1437 unsigned V2 = MakeReg(MVT::i64);
1438 BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31).addReg(Tmp1);
1439 BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31).addReg(Tmp2);
1440 unsigned IRes = MakeReg(MVT::i64);
1441 BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
1442 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
1443 return Result;
1444 }
Andrew Lenharth7332f3e2005-04-02 19:11:07 +00001445 case ISD::UNDEF: {
1446 BuildMI(BB, Alpha::IDEF, 0, Result);
1447 return Result;
1448 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001449
Andrew Lenharth032f2352005-02-22 21:59:48 +00001450 case ISD::DYNAMIC_STACKALLOC:
1451 // Generate both result values.
Andrew Lenharth3a7118d2005-02-23 17:33:42 +00001452 if (Result != notIn)
1453 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth032f2352005-02-22 21:59:48 +00001454 else
1455 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1456
1457 // FIXME: We are currently ignoring the requested alignment for handling
1458 // greater than the stack alignment. This will need to be revisited at some
1459 // point. Align = N.getOperand(2);
1460
1461 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1462 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1463 std::cerr << "Cannot allocate stack object with greater alignment than"
1464 << " the stack alignment yet!";
1465 abort();
1466 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001467
Andrew Lenharth032f2352005-02-22 21:59:48 +00001468 Select(N.getOperand(0));
1469 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1470 {
1471 if (CN->getValue() < 32000)
1472 {
1473 BuildMI(BB, Alpha::LDA, 2, Alpha::R30)
1474 .addImm(-CN->getValue()).addReg(Alpha::R30);
1475 } else {
1476 Tmp1 = SelectExpr(N.getOperand(1));
1477 // Subtract size from stack pointer, thereby allocating some space.
1478 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1479 }
1480 } else {
1481 Tmp1 = SelectExpr(N.getOperand(1));
1482 // Subtract size from stack pointer, thereby allocating some space.
1483 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1484 }
1485
1486 // Put a pointer to the space into the result register, by copying the stack
1487 // pointer.
Andrew Lenharth7bc47022005-02-22 23:29:25 +00001488 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
Andrew Lenharth032f2352005-02-22 21:59:48 +00001489 return Result;
1490
Andrew Lenharth33819132005-03-04 20:09:23 +00001491// case ISD::ConstantPool:
1492// Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1493// AlphaLowering.restoreGP(BB);
1494// BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(Tmp1);
1495// return Result;
Andrew Lenharth2c594352005-01-29 15:42:07 +00001496
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001497 case ISD::FrameIndex:
Andrew Lenharth032f2352005-02-22 21:59:48 +00001498 BuildMI(BB, Alpha::LDA, 2, Result)
1499 .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
1500 .addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001501 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001502
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001503 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001504 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001505 case ISD::SEXTLOAD:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001506 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001507 {
1508 // Make sure we generate both values.
1509 if (Result != notIn)
1510 ExprMap[N.getValue(1)] = notIn; // Generate the token
1511 else
1512 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001513
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001514 SDOperand Chain = N.getOperand(0);
1515 SDOperand Address = N.getOperand(1);
1516 Select(Chain);
1517
Misha Brukman4633f1c2005-04-21 23:13:11 +00001518 assert(Node->getValueType(0) == MVT::i64 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001519 "Unknown type to sign extend to.");
Andrew Lenharth03824012005-02-07 05:55:55 +00001520 if (opcode == ISD::LOAD)
1521 Opc = Alpha::LDQ;
1522 else
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001523 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
1524 default: Node->dump(); assert(0 && "Bad sign extend!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001525 case MVT::i32: Opc = Alpha::LDL;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001526 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001527 case MVT::i16: Opc = Alpha::LDWU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001528 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001529 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Misha Brukman4633f1c2005-04-21 23:13:11 +00001530 case MVT::i8: Opc = Alpha::LDBU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001531 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001532 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001533
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001534 if (EnableAlphaLSMark)
1535 {
1536 int i = getValueOffset(dyn_cast<SrcValueSDNode>(N.getOperand(2))->getValue());
1537 int j = getFunctionOffset(BB->getParent()->getFunction());
1538 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
1539 }
1540
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001541 if (Address.getOpcode() == ISD::GlobalAddress) {
1542 AlphaLowering.restoreGP(BB);
1543 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001544 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001545 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1546 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001547 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1548 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001549 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001550 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001551 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001552 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001553 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001554 BuildMI(BB, Opc, 2, Result)
1555 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1556 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001557 } else {
1558 long offset;
1559 SelectAddr(Address, Tmp1, offset);
1560 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1561 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001562 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001563 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001564
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001565 case ISD::GlobalAddress:
1566 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001567 has_sym = true;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001568 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
1569 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
1570 return Result;
1571
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001572 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001573 case ISD::CALL:
1574 {
1575 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001576
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001577 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001578 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001579
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001580 //grab the arguments
1581 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001582 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001583 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001584 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001585
Andrew Lenharth684f2292005-01-30 00:35:27 +00001586 //in reg args
1587 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001588 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001589 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001590 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +00001591 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001592 Alpha::F19, Alpha::F20, Alpha::F21};
1593 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001594 default:
1595 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001596 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001597 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001598 N.getOperand(i+2).getValueType() << "\n";
1599 assert(0 && "Unknown value type for call");
1600 case MVT::i1:
1601 case MVT::i8:
1602 case MVT::i16:
1603 case MVT::i32:
1604 case MVT::i64:
1605 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1606 break;
1607 case MVT::f32:
1608 case MVT::f64:
1609 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1610 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001611 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001612 }
Andrew Lenharth684f2292005-01-30 00:35:27 +00001613 //in mem args
1614 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001615 {
1616 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001617 default:
1618 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001619 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001620 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001621 N.getOperand(i+2).getValueType() << "\n";
1622 assert(0 && "Unknown value type for call");
1623 case MVT::i1:
1624 case MVT::i8:
1625 case MVT::i16:
1626 case MVT::i32:
1627 case MVT::i64:
1628 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1629 break;
1630 case MVT::f32:
1631 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1632 break;
1633 case MVT::f64:
1634 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1635 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001636 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001637 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001638 //build the right kind of call
1639 if (GlobalAddressSDNode *GASD =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001640 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001641 {
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001642 if (GASD->getGlobal()->isExternal()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001643 //use safe calling convention
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001644 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001645 has_sym = true;
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001646 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal());
1647 } else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001648 //use PC relative branch call
Andrew Lenharth1e0d9bd2005-04-14 17:34:20 +00001649 AlphaLowering.restoreGP(BB);
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001650 BuildMI(BB, Alpha::BSR, 1, Alpha::R26).addGlobalAddress(GASD->getGlobal(),true);
1651 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001652 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001653 else if (ExternalSymbolSDNode *ESSDN =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001654 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001655 {
1656 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001657 has_sym = true;
Andrew Lenharthba05ad62005-03-30 18:22:52 +00001658 BuildMI(BB, Alpha::CALL, 1).addExternalSymbol(ESSDN->getSymbol(), true);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001659 } else {
1660 //no need to restore GP as we are doing an indirect call
1661 Tmp1 = SelectExpr(N.getOperand(1));
1662 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
1663 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
1664 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001665
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001666 //push the result into a virtual register
Misha Brukman4633f1c2005-04-21 23:13:11 +00001667
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001668 switch (Node->getValueType(0)) {
1669 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001670 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001671 case MVT::i1:
1672 case MVT::i8:
1673 case MVT::i16:
1674 case MVT::i32:
1675 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001676 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
1677 break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001678 case MVT::f32:
1679 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001680 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
1681 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001682 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001683 return Result+N.ResNo;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001684 }
1685
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001686 case ISD::SIGN_EXTEND_INREG:
1687 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001688 //do SDIV opt for all levels of ints if not dividing by a constant
1689 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
1690 && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001691 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001692 unsigned Tmp4 = MakeReg(MVT::f64);
1693 unsigned Tmp5 = MakeReg(MVT::f64);
1694 unsigned Tmp6 = MakeReg(MVT::f64);
1695 unsigned Tmp7 = MakeReg(MVT::f64);
1696 unsigned Tmp8 = MakeReg(MVT::f64);
1697 unsigned Tmp9 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001698
1699 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1700 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1701 MoveInt2FP(Tmp1, Tmp4, true);
1702 MoveInt2FP(Tmp2, Tmp5, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001703 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Tmp4);
1704 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Tmp5);
1705 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
1706 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Tmp8);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001707 MoveFP2Int(Tmp9, Result, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001708 return Result;
1709 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001710
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001711 //Alpha has instructions for a bunch of signed 32 bit stuff
1712 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001713 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001714 switch (N.getOperand(0).getOpcode()) {
1715 case ISD::ADD:
1716 case ISD::SUB:
1717 case ISD::MUL:
1718 {
1719 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
1720 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
1721 //FIXME: first check for Scaled Adds and Subs!
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001722 ConstantSDNode* CSD = NULL;
1723 if(!isMul && N.getOperand(0).getOperand(0).getOpcode() == ISD::SHL &&
1724 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(0).getOperand(1))) &&
1725 (CSD->getValue() == 2 || CSD->getValue() == 3))
1726 {
1727 bool use4 = CSD->getValue() == 2;
1728 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1729 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1730 BuildMI(BB, isAdd?(use4?Alpha::S4ADDL:Alpha::S8ADDL):(use4?Alpha::S4SUBL:Alpha::S8SUBL),
1731 2,Result).addReg(Tmp1).addReg(Tmp2);
1732 }
1733 else if(isAdd && N.getOperand(0).getOperand(1).getOpcode() == ISD::SHL &&
1734 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1).getOperand(1))) &&
1735 (CSD->getValue() == 2 || CSD->getValue() == 3))
1736 {
1737 bool use4 = CSD->getValue() == 2;
1738 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1739 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1740 BuildMI(BB, use4?Alpha::S4ADDL:Alpha::S8ADDL, 2,Result).addReg(Tmp1).addReg(Tmp2);
1741 }
1742 else if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001743 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
1744 { //Normal imm add/sub
1745 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001746 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001747 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
1748 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001749 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001750 else
1751 { //Normal add/sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001752 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULL : Alpha::SUBL);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001753 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001754 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001755 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1756 }
1757 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001758 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001759 default: break; //Fall Though;
1760 }
1761 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001762 Tmp1 = SelectExpr(N.getOperand(0));
1763 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001764 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001765 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001766 {
1767 default:
1768 Node->dump();
1769 assert(0 && "Sign Extend InReg not there yet");
1770 break;
1771 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001772 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001773 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001774 break;
1775 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001776 case MVT::i16:
1777 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
1778 break;
1779 case MVT::i8:
1780 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
1781 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +00001782 case MVT::i1:
1783 Tmp2 = MakeReg(MVT::i64);
1784 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001785 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +00001786 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001787 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001788 return Result;
1789 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001790
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001791 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001792 {
1793 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1794 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1795 bool isConst1 = false;
1796 bool isConst2 = false;
1797 int dir;
Misha Brukman7847fca2005-04-22 17:54:37 +00001798
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001799 //Tmp1 = SelectExpr(N.getOperand(0));
1800 if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001801 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
1802 isConst1 = true;
1803 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001804 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1805 isConst2 = true;
1806
1807 switch (SetCC->getCondition()) {
1808 default: Node->dump(); assert(0 && "Unknown integer comparison!");
1809 case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001810 case ISD::SETLT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001811 Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001812 case ISD::SETLE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001813 Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001814 case ISD::SETGT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001815 Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001816 case ISD::SETGE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001817 Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001818 case ISD::SETULT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001819 Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001820 case ISD::SETUGT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001821 Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001822 case ISD::SETULE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001823 Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001824 case ISD::SETUGE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001825 Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001826 case ISD::SETNE: {//Handle this one special
1827 //std::cerr << "Alpha does not have a setne.\n";
1828 //abort();
1829 Tmp1 = SelectExpr(N.getOperand(0));
1830 Tmp2 = SelectExpr(N.getOperand(1));
1831 Tmp3 = MakeReg(MVT::i64);
1832 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001833 //Remeber we have the Inv for this CC
1834 CCInvMap[N] = Tmp3;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001835 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001836 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001837 return Result;
1838 }
1839 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001840 if (dir == 1) {
1841 Tmp1 = SelectExpr(N.getOperand(0));
1842 if (isConst2) {
1843 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1844 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1845 } else {
1846 Tmp2 = SelectExpr(N.getOperand(1));
1847 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1848 }
1849 } else if (dir == 2) {
1850 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001851 if (isConst1) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001852 Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1853 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1854 } else {
1855 Tmp2 = SelectExpr(N.getOperand(0));
1856 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1857 }
1858 } else { //dir == 0
1859 if (isConst1) {
1860 Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1861 Tmp2 = SelectExpr(N.getOperand(1));
1862 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
1863 } else if (isConst2) {
1864 Tmp1 = SelectExpr(N.getOperand(0));
1865 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1866 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
1867 } else {
1868 Tmp1 = SelectExpr(N.getOperand(0));
1869 Tmp2 = SelectExpr(N.getOperand(1));
1870 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1871 }
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001872 }
1873 } else {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001874 //do the comparison
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001875 Tmp1 = MakeReg(MVT::f64);
1876 bool inv = SelectFPSetCC(N, Tmp1);
1877
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001878 //now arrange for Result (int) to have a 1 or 0
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001879 Tmp2 = MakeReg(MVT::i64);
1880 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001881 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001882 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001883 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001884 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001885 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001886 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001887
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001888 case ISD::CopyFromReg:
1889 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001890 ++count_ins;
1891
Andrew Lenharth40831c52005-01-28 06:57:18 +00001892 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001893 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001894 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001895 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001896 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001897
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001898 SDOperand Chain = N.getOperand(0);
1899
1900 Select(Chain);
1901 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1902 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1903 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1904 return Result;
1905 }
1906
Misha Brukman4633f1c2005-04-21 23:13:11 +00001907 //Most of the plain arithmetic and logic share the same form, and the same
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001908 //constant immediate test
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001909 case ISD::XOR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001910 //Match Not
1911 if (N.getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001912 cast<ConstantSDNode>(N.getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001913 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001914 Tmp1 = SelectExpr(N.getOperand(0));
1915 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1916 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001917 }
1918 //Fall through
1919 case ISD::AND:
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001920 //handle zap
1921 if (opcode == ISD::AND && N.getOperand(1).getOpcode() == ISD::Constant)
1922 {
1923 uint64_t k = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1924 unsigned int build = 0;
1925 for(int i = 0; i < 8; ++i)
1926 {
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001927 if ((k & 0x00FF) == 0x00FF)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001928 build |= 1 << i;
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001929 else if ((k & 0x00FF) != 0)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001930 { build = 0; break; }
1931 k >>= 8;
1932 }
1933 if (build)
1934 {
1935 Tmp1 = SelectExpr(N.getOperand(0));
1936 BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1937 return Result;
1938 }
1939 }
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001940 case ISD::OR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001941 //Check operand(0) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001942 if (N.getOperand(0).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001943 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001944 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001945 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001946 switch(opcode) {
1947 case ISD::AND: Opc = Alpha::BIC; break;
1948 case ISD::OR: Opc = Alpha::ORNOT; break;
1949 case ISD::XOR: Opc = Alpha::EQV; break;
1950 }
1951 Tmp1 = SelectExpr(N.getOperand(1));
1952 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1953 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1954 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001955 }
1956 //Check operand(1) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001957 if (N.getOperand(1).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001958 N.getOperand(1).getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001959 cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001960 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001961 switch(opcode) {
1962 case ISD::AND: Opc = Alpha::BIC; break;
1963 case ISD::OR: Opc = Alpha::ORNOT; break;
1964 case ISD::XOR: Opc = Alpha::EQV; break;
1965 }
1966 Tmp1 = SelectExpr(N.getOperand(0));
1967 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1968 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1969 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001970 }
1971 //Fall through
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001972 case ISD::SHL:
1973 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001974 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001975 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001976 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1977 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001978 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001979 {
1980 switch(opcode) {
1981 case ISD::AND: Opc = Alpha::ANDi; break;
1982 case ISD::OR: Opc = Alpha::BISi; break;
1983 case ISD::XOR: Opc = Alpha::XORi; break;
1984 case ISD::SHL: Opc = Alpha::SLi; break;
1985 case ISD::SRL: Opc = Alpha::SRLi; break;
1986 case ISD::SRA: Opc = Alpha::SRAi; break;
1987 case ISD::MUL: Opc = Alpha::MULQi; break;
1988 };
1989 Tmp1 = SelectExpr(N.getOperand(0));
1990 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1991 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1992 } else {
1993 switch(opcode) {
1994 case ISD::AND: Opc = Alpha::AND; break;
1995 case ISD::OR: Opc = Alpha::BIS; break;
1996 case ISD::XOR: Opc = Alpha::XOR; break;
1997 case ISD::SHL: Opc = Alpha::SL; break;
1998 case ISD::SRL: Opc = Alpha::SRL; break;
1999 case ISD::SRA: Opc = Alpha::SRA; break;
2000 case ISD::MUL: Opc = Alpha::MULQ; break;
2001 };
2002 Tmp1 = SelectExpr(N.getOperand(0));
2003 Tmp2 = SelectExpr(N.getOperand(1));
2004 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2005 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00002006 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002007
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002008 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002009 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00002010 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00002011 bool isAdd = opcode == ISD::ADD;
2012
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002013 //first check for Scaled Adds and Subs!
2014 //Valid for add and sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002015 ConstantSDNode* CSD = NULL;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002016 if(N.getOperand(0).getOpcode() == ISD::SHL &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002017 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
2018 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002019 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002020 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002021 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002022 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) && CSD->getValue() <= 255)
2023 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
2024 2, Result).addReg(Tmp2).addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00002025 else {
2026 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002027 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
2028 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00002029 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002030 }
2031 //Position prevents subs
Andrew Lenharth273a1f92005-04-07 14:18:13 +00002032 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002033 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) &&
2034 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002035 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002036 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002037 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002038 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(0))) && CSD->getValue() <= 255)
2039 BuildMI(BB, use4?Alpha::S4ADDQi:Alpha::S8ADDQi, 2, Result).addReg(Tmp2)
2040 .addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00002041 else {
2042 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002043 BuildMI(BB, use4?Alpha::S4ADDQ:Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00002044 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002045 }
2046 //small addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002047 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
2048 CSD->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002049 { //Normal imm add/sub
2050 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
2051 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002052 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CSD->getValue());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002053 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002054 //larger addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002055 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
2056 CSD->getSignExtended() <= 32767 &&
2057 CSD->getSignExtended() >= -32767)
Andrew Lenharth74d00d82005-03-02 17:23:03 +00002058 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002059 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002060 Tmp2 = (long)CSD->getSignExtended();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002061 if (!isAdd)
2062 Tmp2 = -Tmp2;
2063 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002064 }
2065 //give up and do the operation
2066 else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002067 //Normal add/sub
2068 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
2069 Tmp1 = SelectExpr(N.getOperand(0));
2070 Tmp2 = SelectExpr(N.getOperand(1));
2071 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2072 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002073 return Result;
2074 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002075
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00002076 case ISD::SDIV:
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002077 {
Andrew Lenhartha565c272005-04-06 22:03:13 +00002078 ConstantSDNode* CSD;
2079 //check if we can convert into a shift!
2080 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
2081 (int64_t)CSD->getSignExtended() != 0 &&
2082 ExactLog2(abs((int64_t)CSD->getSignExtended())) != 0)
2083 {
2084 unsigned k = ExactLog2(abs(CSD->getSignExtended()));
2085 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenhartha565c272005-04-06 22:03:13 +00002086 if (k == 1)
2087 Tmp2 = Tmp1;
2088 else
2089 {
2090 Tmp2 = MakeReg(MVT::i64);
2091 BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
2092 }
2093 Tmp3 = MakeReg(MVT::i64);
2094 BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
2095 unsigned Tmp4 = MakeReg(MVT::i64);
2096 BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
2097 if ((int64_t)CSD->getSignExtended() > 0)
2098 BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
2099 else
2100 {
2101 unsigned Tmp5 = MakeReg(MVT::i64);
2102 BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
2103 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
2104 }
2105 return Result;
2106 }
2107 }
2108 //Else fall through
2109
2110 case ISD::UDIV:
2111 {
2112 ConstantSDNode* CSD;
2113 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
2114 ((int64_t)CSD->getSignExtended() >= 2 ||
2115 (int64_t)CSD->getSignExtended() <= -2))
2116 {
2117 // If this is a divide by constant, we can emit code using some magic
2118 // constants to implement it as a multiply instead.
2119 ExprMap.erase(N);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002120 if (opcode == ISD::SDIV)
Andrew Lenhartha565c272005-04-06 22:03:13 +00002121 return SelectExpr(BuildSDIVSequence(N));
2122 else
2123 return SelectExpr(BuildUDIVSequence(N));
2124 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002125 }
2126 //else fall though
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002127 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +00002128 case ISD::SREM:
Misha Brukman4633f1c2005-04-21 23:13:11 +00002129 //FIXME: alpha really doesn't support any of these operations,
Andrew Lenharth40831c52005-01-28 06:57:18 +00002130 // the ops are expanded into special library calls with
2131 // special calling conventions
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002132 //Restore GP because it is a call after all...
Andrew Lenharth40831c52005-01-28 06:57:18 +00002133 switch(opcode) {
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00002134 case ISD::UREM: Opc = Alpha::REMQU; break;
2135 case ISD::SREM: Opc = Alpha::REMQ; break;
2136 case ISD::UDIV: Opc = Alpha::DIVQU; break;
2137 case ISD::SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002138 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002139 Tmp1 = SelectExpr(N.getOperand(0));
2140 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth33819132005-03-04 20:09:23 +00002141 //set up regs explicitly (helps Reg alloc)
2142 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002143 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00002144 AlphaLowering.restoreGP(BB);
Andrew Lenharth33819132005-03-04 20:09:23 +00002145 BuildMI(BB, Opc, 2).addReg(Alpha::R24).addReg(Alpha::R25);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002146 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002147 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002148
Andrew Lenharthe76797c2005-02-01 20:40:27 +00002149 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002150 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002151 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002152 assert (DestType == MVT::i64 && "only quads can be loaded to");
2153 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00002154 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002155 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002156 if (SrcType == MVT::f32)
Misha Brukman7847fca2005-04-22 17:54:37 +00002157 {
2158 Tmp2 = MakeReg(MVT::f64);
2159 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
2160 Tmp1 = Tmp2;
2161 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002162 Tmp2 = MakeReg(MVT::f64);
2163 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00002164 MoveFP2Int(Tmp2, Result, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002165
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002166 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002167 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002168
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002169 case ISD::SELECT:
2170 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00002171 //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 +00002172 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002173 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2174 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002175 // Get the condition into the zero flag.
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002176 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002177
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002178 SDOperand CC = N.getOperand(0);
2179 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
2180
Misha Brukman4633f1c2005-04-21 23:13:11 +00002181 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002182 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
2183 { //FP Setcc -> Int Select
Misha Brukman7847fca2005-04-22 17:54:37 +00002184 Tmp1 = MakeReg(MVT::f64);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002185 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2186 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Misha Brukman7847fca2005-04-22 17:54:37 +00002187 bool inv = SelectFPSetCC(CC, Tmp1);
2188 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
2189 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
2190 return Result;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002191 }
2192 if (CC.getOpcode() == ISD::SETCC) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002193 //Int SetCC -> Select
2194 //Dropping the CC is only useful if we are comparing to 0
2195 if((SetCC->getOperand(1).getOpcode() == ISD::Constant &&
2196 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0) ||
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002197 (SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2198 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0))
2199 {
2200 //figure out a few things
2201 bool LeftZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2202 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
2203 bool RightZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2204 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
2205 bool LeftConst = N.getOperand(1).getOpcode() == ISD::Constant &&
2206 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255;
2207 bool RightConst = N.getOperand(2).getOpcode() == ISD::Constant &&
2208 cast<ConstantSDNode>(N.getOperand(2))->getValue() <= 255;
2209 bool useImm = LeftConst || RightConst;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002210
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002211 //Fix up CC
2212 ISD::CondCode cCode= SetCC->getCondition();
2213 if (RightConst && !LeftConst) //Invert sense to get Imm field right
2214 cCode = ISD::getSetCCInverse(cCode, true);
2215 if (LeftZero && !RightZero) //Swap Operands
2216 cCode = ISD::getSetCCSwappedOperands(cCode);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002217
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002218 //Choose the CMOV
2219 switch (cCode) {
2220 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
2221 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
2222 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
2223 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
2224 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
2225 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
2226 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
2227 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2228 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break; //Technically you could have this CC
2229 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
2230 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2231 }
2232 if(LeftZero && !RightZero) //swap Operands
2233 Tmp1 = SelectExpr(SetCC->getOperand(1)); //Cond
2234 else
2235 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
2236
2237 if (LeftConst) {
2238 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2239 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
Misha Brukman7847fca2005-04-22 17:54:37 +00002240 .addImm(cast<ConstantSDNode>(N.getOperand(1))->getValue())
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002241 .addReg(Tmp1);
2242 } else if (RightConst) {
2243 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
2244 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
Misha Brukman7847fca2005-04-22 17:54:37 +00002245 .addImm(cast<ConstantSDNode>(N.getOperand(2))->getValue())
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002246 .addReg(Tmp1);
2247 } else {
2248 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2249 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2250 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
2251 }
2252 return Result;
2253 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002254 //Otherwise, fall though
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002255 }
2256 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002257 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2258 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthe76797c2005-02-01 20:40:27 +00002259 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002260
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002261 return Result;
2262 }
2263
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002264 case ISD::Constant:
2265 {
Andrew Lenharthc0513832005-03-29 19:24:04 +00002266 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002267 if (val <= IMM_HIGH && val >= IMM_LOW) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002268 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002269 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002270 else if (val <= (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT &&
2271 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
2272 Tmp1 = MakeReg(MVT::i64);
2273 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val)).addReg(Alpha::R31);
2274 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002275 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002276 else {
2277 MachineConstantPool *CP = BB->getParent()->getConstantPool();
2278 ConstantUInt *C = ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
2279 unsigned CPI = CP->getConstantPoolIndex(C);
2280 AlphaLowering.restoreGP(BB);
2281 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CPI);
2282 }
2283 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002284 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002285 }
2286
2287 return 0;
2288}
2289
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002290void AlphaISel::Select(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002291 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00002292 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002293
Nate Begeman85fdeb22005-03-24 04:39:54 +00002294 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002295 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002296
2297 SDNode *Node = N.Val;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002298
Andrew Lenharth760270d2005-02-07 23:02:23 +00002299 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002300
2301 default:
2302 Node->dump(); std::cerr << "\n";
2303 assert(0 && "Node not handled yet!");
2304
2305 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00002306 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002307 return;
2308 }
2309
2310 case ISD::BR: {
2311 MachineBasicBlock *Dest =
2312 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2313
2314 Select(N.getOperand(0));
2315 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
2316 return;
2317 }
2318
2319 case ISD::ImplicitDef:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002320 ++count_ins;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002321 Select(N.getOperand(0));
2322 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
2323 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002324
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002325 case ISD::EntryToken: return; // Noop
2326
2327 case ISD::TokenFactor:
2328 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2329 Select(Node->getOperand(i));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002330
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002331 //N.Val->dump(); std::cerr << "\n";
2332 //assert(0 && "Node not handled yet!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00002333
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002334 return;
2335
2336 case ISD::CopyToReg:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002337 ++count_outs;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002338 Select(N.getOperand(0));
2339 Tmp1 = SelectExpr(N.getOperand(1));
2340 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002341
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002342 if (Tmp1 != Tmp2) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002343 if (N.getOperand(1).getValueType() == MVT::f64 ||
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002344 N.getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00002345 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2346 else
2347 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002348 }
2349 return;
2350
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002351 case ISD::RET:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002352 ++count_outs;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002353 switch (N.getNumOperands()) {
2354 default:
2355 std::cerr << N.getNumOperands() << "\n";
2356 for (unsigned i = 0; i < N.getNumOperands(); ++i)
2357 std::cerr << N.getOperand(i).getValueType() << "\n";
2358 Node->dump();
2359 assert(0 && "Unknown return instruction!");
2360 case 2:
2361 Select(N.getOperand(0));
2362 Tmp1 = SelectExpr(N.getOperand(1));
2363 switch (N.getOperand(1).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002364 default: Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002365 assert(0 && "All other types should have been promoted!!");
2366 case MVT::f64:
2367 case MVT::f32:
2368 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
2369 break;
2370 case MVT::i32:
2371 case MVT::i64:
2372 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
2373 break;
2374 }
2375 break;
2376 case 1:
2377 Select(N.getOperand(0));
2378 break;
2379 }
2380 //Tmp2 = AlphaLowering.getRetAddr();
2381 //BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(Tmp2).addReg(Tmp2);
2382 BuildMI(BB, Alpha::RETURN, 0); // Just emit a 'ret' instruction
2383 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002384
Misha Brukman4633f1c2005-04-21 23:13:11 +00002385 case ISD::TRUNCSTORE:
2386 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002387 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002388 SDOperand Chain = N.getOperand(0);
2389 SDOperand Value = N.getOperand(1);
2390 SDOperand Address = N.getOperand(2);
2391 Select(Chain);
2392
2393 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00002394
2395 if (opcode == ISD::STORE) {
2396 switch(Value.getValueType()) {
2397 default: assert(0 && "unknown Type in store");
2398 case MVT::i64: Opc = Alpha::STQ; break;
2399 case MVT::f64: Opc = Alpha::STT; break;
2400 case MVT::f32: Opc = Alpha::STS; break;
2401 }
2402 } else { //ISD::TRUNCSTORE
2403 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2404 default: assert(0 && "unknown Type in store");
2405 case MVT::i1: //FIXME: DAG does not promote this load
2406 case MVT::i8: Opc = Alpha::STB; break;
2407 case MVT::i16: Opc = Alpha::STW; break;
2408 case MVT::i32: Opc = Alpha::STL; break;
2409 }
Andrew Lenharth65838902005-02-06 16:22:15 +00002410 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00002411
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002412 if (EnableAlphaLSMark)
2413 {
2414 int i = getValueOffset(dyn_cast<SrcValueSDNode>(N.getOperand(3))->getValue());
2415 int j = getFunctionOffset(BB->getParent()->getFunction());
2416 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
2417 }
2418
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002419 if (Address.getOpcode() == ISD::GlobalAddress)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002420 {
2421 AlphaLowering.restoreGP(BB);
2422 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002423 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002424 BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
2425 }
Andrew Lenharth05380342005-02-07 05:07:00 +00002426 else if(Address.getOpcode() == ISD::FrameIndex)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002427 {
Andrew Lenharth032f2352005-02-22 21:59:48 +00002428 BuildMI(BB, Opc, 3).addReg(Tmp1)
2429 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
2430 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002431 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002432 else
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002433 {
2434 long offset;
2435 SelectAddr(Address, Tmp2, offset);
2436 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2437 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002438 return;
2439 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002440
2441 case ISD::EXTLOAD:
2442 case ISD::SEXTLOAD:
2443 case ISD::ZEXTLOAD:
2444 case ISD::LOAD:
2445 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002446 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002447 case ISD::CALL:
Andrew Lenharth032f2352005-02-22 21:59:48 +00002448 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002449 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002450 SelectExpr(N);
2451 return;
2452
Chris Lattner16cd04d2005-05-12 23:24:06 +00002453 case ISD::CALLSEQ_START:
2454 case ISD::CALLSEQ_END:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002455 Select(N.getOperand(0));
2456 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002457
Chris Lattner16cd04d2005-05-12 23:24:06 +00002458 Opc = N.getOpcode() == ISD::CALLSEQ_START ? Alpha::ADJUSTSTACKDOWN :
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002459 Alpha::ADJUSTSTACKUP;
2460 BuildMI(BB, Opc, 1).addImm(Tmp1);
2461 return;
Andrew Lenharth95762122005-03-31 21:24:06 +00002462
2463 case ISD::PCMARKER:
2464 Select(N.getOperand(0)); //Chain
2465 BuildMI(BB, Alpha::PCLABEL, 2).addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
2466 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002467 }
2468 assert(0 && "Should not be reached!");
2469}
2470
2471
2472/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
2473/// into a machine code representation using pattern matching and a machine
2474/// description file.
2475///
2476FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002477 return new AlphaISel(TM);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002478}
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002479