blob: 9c9b3d6db08fd8e37ca1c742f41ab5000cb5d70a [file] [log] [blame]
Misha Brukman2a8350a2005-02-05 02:24:26 +00001//===- AlphaISelPattern.cpp - A pattern matching inst selector for Alpha --===//
Misha Brukman4633f1c2005-04-21 23:13:11 +00002//
Andrew Lenharth304d0f32005-01-22 23:41:55 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman4633f1c2005-04-21 23:13:11 +00007//
Andrew Lenharth304d0f32005-01-22 23:41:55 +00008//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for Alpha.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Alpha.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000015#include "AlphaRegisterInfo.h"
16#include "llvm/Constants.h" // FIXME: REMOVE
17#include "llvm/Function.h"
Andrew Lenharthb69f3422005-06-22 17:19:45 +000018#include "llvm/Module.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000019#include "llvm/CodeGen/MachineInstrBuilder.h"
20#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/SelectionDAG.h"
24#include "llvm/CodeGen/SelectionDAGISel.h"
25#include "llvm/CodeGen/SSARegMap.h"
26#include "llvm/Target/TargetData.h"
27#include "llvm/Target/TargetLowering.h"
28#include "llvm/Support/MathExtras.h"
29#include "llvm/ADT/Statistic.h"
Andrew Lenharth032f2352005-02-22 21:59:48 +000030#include "llvm/Support/Debug.h"
Andrew Lenharth95762122005-03-31 21:24:06 +000031#include "llvm/Support/CommandLine.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000032#include <set>
Andrew Lenharth684f2292005-01-30 00:35:27 +000033#include <algorithm>
Andrew Lenharth304d0f32005-01-22 23:41:55 +000034using namespace llvm;
35
Andrew Lenharth95762122005-03-31 21:24:06 +000036namespace llvm {
Misha Brukman4633f1c2005-04-21 23:13:11 +000037 cl::opt<bool> EnableAlphaIDIV("enable-alpha-intfpdiv",
38 cl::desc("Use the FP div instruction for integer div when possible"),
Andrew Lenharth95762122005-03-31 21:24:06 +000039 cl::Hidden);
Andrew Lenharth59009192005-05-04 19:12:09 +000040 cl::opt<bool> EnableAlphaFTOI("enable-alpha-FTOI",
Misha Brukman4633f1c2005-04-21 23:13:11 +000041 cl::desc("Enable use of ftoi* and itof* instructions (ev6 and higher)"),
Andrew Lenharth95762122005-03-31 21:24:06 +000042 cl::Hidden);
Andrew Lenharth59009192005-05-04 19:12:09 +000043 cl::opt<bool> EnableAlphaCT("enable-alpha-CT",
44 cl::desc("Enable use of the ctpop, ctlz, and cttz instructions"),
45 cl::Hidden);
Misha Brukman4633f1c2005-04-21 23:13:11 +000046 cl::opt<bool> EnableAlphaCount("enable-alpha-count",
47 cl::desc("Print estimates on live ins and outs"),
Andrew Lenhartha32b9e32005-04-08 17:28:49 +000048 cl::Hidden);
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +000049 cl::opt<bool> EnableAlphaLSMark("enable-alpha-lsmark",
Misha Brukman5e96a3a2005-06-06 19:08:04 +000050 cl::desc("Emit symbols to correlate Mem ops to LLVM Values"),
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +000051 cl::Hidden);
Andrew Lenharth95762122005-03-31 21:24:06 +000052}
53
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +000054namespace {
55 // Alpha Specific DAG Nodes
56 namespace AlphaISD {
57 enum NodeType {
58 // Start the numbering where the builtin ops leave off.
59 FIRST_NUMBER = ISD::BUILTIN_OP_END,
60
61 //Convert an int bit pattern in an FP reg to a Double or Float
62 //Has a dest type and a source
63 CVTQ,
64 //Move an Ireg to a FPreg
65 ITOF,
66 //Move a FPreg to an Ireg
67 FTOI,
68 };
69 }
70}
71
Andrew Lenharth304d0f32005-01-22 23:41:55 +000072//===----------------------------------------------------------------------===//
73// AlphaTargetLowering - Alpha Implementation of the TargetLowering interface
74namespace {
75 class AlphaTargetLowering : public TargetLowering {
Andrew Lenharth558bc882005-06-18 18:34:52 +000076 int VarArgsOffset; // What is the offset to the first vaarg
77 int VarArgsBase; // What is the base FrameIndex
Andrew Lenharth304d0f32005-01-22 23:41:55 +000078 unsigned GP; //GOT vreg
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +000079 unsigned RA; //Return Address
Andrew Lenharth304d0f32005-01-22 23:41:55 +000080 public:
81 AlphaTargetLowering(TargetMachine &TM) : TargetLowering(TM) {
82 // Set up the TargetLowering object.
Andrew Lenharth3d65d312005-01-27 03:49:45 +000083 //I am having problems with shr n ubyte 1
Andrew Lenharth879ef222005-02-02 17:00:21 +000084 setShiftAmountType(MVT::i64);
85 setSetCCResultType(MVT::i64);
Andrew Lenharthd3355e22005-04-07 20:11:32 +000086 setSetCCResultContents(ZeroOrOneSetCCResult);
Misha Brukman4633f1c2005-04-21 23:13:11 +000087
Andrew Lenharth304d0f32005-01-22 23:41:55 +000088 addRegisterClass(MVT::i64, Alpha::GPRCRegisterClass);
89 addRegisterClass(MVT::f64, Alpha::FPRCRegisterClass);
Andrew Lenharth3d65d312005-01-27 03:49:45 +000090 addRegisterClass(MVT::f32, Alpha::FPRCRegisterClass);
Misha Brukman4633f1c2005-04-21 23:13:11 +000091
Chris Lattnerda4d4692005-04-09 03:22:37 +000092 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000093
Andrew Lenharthec151362005-06-26 22:23:06 +000094 setOperationAction(ISD::EXTLOAD, MVT::i1, Promote);
95 setOperationAction(ISD::EXTLOAD, MVT::f32, Promote);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000096
Andrew Lenharthec151362005-06-26 22:23:06 +000097 setOperationAction(ISD::ZEXTLOAD, MVT::i1 , Expand);
98 setOperationAction(ISD::ZEXTLOAD, MVT::i32 , Expand);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000099
Andrew Lenharthec151362005-06-26 22:23:06 +0000100 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
101 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
102 setOperationAction(ISD::SEXTLOAD, MVT::i16, Expand);
103
104 setOperationAction(ISD::SREM, MVT::f32, Expand);
105 setOperationAction(ISD::SREM, MVT::f64, Expand);
106
107 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000108
Andrew Lenharth59009192005-05-04 19:12:09 +0000109 if (!EnableAlphaCT) {
110 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
111 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
Andrew Lenharthb5884d32005-05-04 19:25:37 +0000112 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
Andrew Lenharth59009192005-05-04 19:12:09 +0000113 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000114
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000115 //If this didn't legalize into a div....
116 // setOperationAction(ISD::SREM , MVT::i64, Expand);
117 // setOperationAction(ISD::UREM , MVT::i64, Expand);
118
119 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
120 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
121 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
Andrew Lenharth9818c052005-02-05 13:19:12 +0000122
Chris Lattner17234b72005-04-30 04:26:06 +0000123 // We don't support sin/cos/sqrt
124 setOperationAction(ISD::FSIN , MVT::f64, Expand);
125 setOperationAction(ISD::FCOS , MVT::f64, Expand);
126 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
127 setOperationAction(ISD::FSIN , MVT::f32, Expand);
128 setOperationAction(ISD::FCOS , MVT::f32, Expand);
129 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
130
Andrew Lenharth33819132005-03-04 20:09:23 +0000131 //Doesn't work yet
Chris Lattner17234b72005-04-30 04:26:06 +0000132 setOperationAction(ISD::SETCC, MVT::f32, Promote);
Andrew Lenharth572af902005-02-14 05:41:43 +0000133
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000134 //Try a couple things with a custom expander
135 //setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
136
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000137 computeRegisterProperties();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000138
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000139 addLegalFPImmediate(+0.0); //F31
140 addLegalFPImmediate(-0.0); //-F31
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000141 }
142
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000143 /// LowerOperation - Provide custom lowering hooks for some operations.
144 ///
145 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
146
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000147 /// LowerArguments - This hook must be implemented to indicate how we should
148 /// lower the arguments for the specified function, into the specified DAG.
149 virtual std::vector<SDOperand>
150 LowerArguments(Function &F, SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000151
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000152 /// LowerCallTo - This hook lowers an abstract call to a function into an
153 /// actual call.
154 virtual std::pair<SDOperand, SDOperand>
Chris Lattnerc57f6822005-05-12 19:56:45 +0000155 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattneradf6a962005-05-13 18:50:42 +0000156 bool isTailCall, SDOperand Callee, ArgListTy &Args,
157 SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000158
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000159 virtual std::pair<SDOperand, SDOperand>
Andrew Lenharth558bc882005-06-18 18:34:52 +0000160 LowerVAStart(SDOperand Chain, SelectionDAG &DAG, SDOperand Dest);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000161
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000162 virtual std::pair<SDOperand,SDOperand>
Andrew Lenharth558bc882005-06-18 18:34:52 +0000163 LowerVAArgNext(SDOperand Chain, SDOperand VAList,
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000164 const Type *ArgTy, SelectionDAG &DAG);
165
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000166 std::pair<SDOperand,SDOperand>
167 LowerVACopy(SDOperand Chain, SDOperand Src, SDOperand Dest,
168 SelectionDAG &DAG);
169
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000170 virtual std::pair<SDOperand, SDOperand>
171 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
172 SelectionDAG &DAG);
173
174 void restoreGP(MachineBasicBlock* BB)
175 {
176 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
177 }
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000178 void restoreRA(MachineBasicBlock* BB)
179 {
180 BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(RA).addReg(RA);
181 }
Andrew Lenharth3b918072005-06-27 15:36:48 +0000182 unsigned getRA()
183 {
184 return RA;
185 }
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000186
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000187 };
188}
189
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000190/// LowerOperation - Provide custom lowering hooks for some operations.
191///
192SDOperand AlphaTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
193 MachineFunction &MF = DAG.getMachineFunction();
194 switch (Op.getOpcode()) {
195 default: assert(0 && "Should not custom lower this!");
Misha Brukmanb8ee91a2005-06-06 17:39:46 +0000196#if 0
197 case ISD::SINT_TO_FP:
198 {
199 assert (Op.getOperand(0).getValueType() == MVT::i64
200 && "only quads can be loaded from");
201 SDOperand SRC;
202 if (EnableAlphaFTOI)
203 {
204 std::vector<MVT::ValueType> RTs;
205 RTs.push_back(Op.getValueType());
206 std::vector<SDOperand> Ops;
207 Ops.push_back(Op.getOperand(0));
208 SRC = DAG.getNode(AlphaISD::ITOF, RTs, Ops);
209 } else {
210 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
211 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
212 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
213 Op.getOperand(0), StackSlot, DAG.getSrcValue(NULL));
214 SRC = DAG.getLoad(Op.getValueType(), Store.getValue(0), StackSlot,
215 DAG.getSrcValue(NULL));
216 }
217 std::vector<MVT::ValueType> RTs;
218 RTs.push_back(Op.getValueType());
219 std::vector<SDOperand> Ops;
220 Ops.push_back(SRC);
221 return DAG.getNode(AlphaISD::CVTQ, RTs, Ops);
222 }
223#endif
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000224 }
Misha Brukmanb8ee91a2005-06-06 17:39:46 +0000225 return SDOperand();
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000226}
227
228
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000229/// AddLiveIn - This helper function adds the specified physical register to the
230/// MachineFunction as a live in value. It also creates a corresponding virtual
231/// register for it.
232static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
233 TargetRegisterClass *RC) {
234 assert(RC->contains(PReg) && "Not the correct regclass!");
235 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
236 MF.addLiveIn(PReg, VReg);
237 return VReg;
238}
239
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000240//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
241
242//For now, just use variable size stack frame format
243
244//In a standard call, the first six items are passed in registers $16
245//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
246//of argument-to-register correspondence.) The remaining items are
247//collected in a memory argument list that is a naturally aligned
248//array of quadwords. In a standard call, this list, if present, must
249//be passed at 0(SP).
Misha Brukman7847fca2005-04-22 17:54:37 +0000250//7 ... n 0(SP) ... (n-7)*8(SP)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000251
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000252// //#define FP $15
253// //#define RA $26
254// //#define PV $27
255// //#define GP $29
256// //#define SP $30
Misha Brukman4633f1c2005-04-21 23:13:11 +0000257
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000258std::vector<SDOperand>
Misha Brukman4633f1c2005-04-21 23:13:11 +0000259AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000260{
261 std::vector<SDOperand> ArgValues;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000262
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000263 MachineFunction &MF = DAG.getMachineFunction();
Andrew Lenharth05380342005-02-07 05:07:00 +0000264 MachineFrameInfo*MFI = MF.getFrameInfo();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000265
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000266 MachineBasicBlock& BB = MF.front();
267
268 //Handle the return address
269 //BuildMI(&BB, Alpha::IDEF, 0, Alpha::R26);
270
Misha Brukman4633f1c2005-04-21 23:13:11 +0000271 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000272 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000273 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000274 Alpha::F19, Alpha::F20, Alpha::F21};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000275 int count = 0;
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000276
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000277 GP = AddLiveIn(MF, Alpha::R29, getRegClassFor(MVT::i64));
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000278 RA = AddLiveIn(MF, Alpha::R26, getRegClassFor(MVT::i64));
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000279
Chris Lattnere4d5c442005-03-15 04:54:21 +0000280 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000281 {
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000282 SDOperand argt;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000283 if (count < 6) {
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000284 unsigned Vreg;
285 MVT::ValueType VT = getValueType(I->getType());
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000286 switch (VT) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000287 default:
288 std::cerr << "Unknown Type " << VT << "\n";
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000289 abort();
290 case MVT::f64:
291 case MVT::f32:
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000292 args_float[count] = AddLiveIn(MF,args_float[count], getRegClassFor(VT));
293 argt = DAG.getCopyFromReg(args_float[count], VT, DAG.getRoot());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000294 break;
295 case MVT::i1:
296 case MVT::i8:
297 case MVT::i16:
298 case MVT::i32:
299 case MVT::i64:
Andrew Lenharth591ec572005-05-31 18:42:18 +0000300 args_int[count] = AddLiveIn(MF, args_int[count], getRegClassFor(MVT::i64));
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000301 argt = DAG.getCopyFromReg(args_int[count], VT, DAG.getRoot());
Andrew Lenharth14f30c92005-05-31 18:37:16 +0000302 if (VT != MVT::i64)
303 argt = DAG.getNode(ISD::TRUNCATE, VT, argt);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000304 break;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000305 }
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000306 DAG.setRoot(argt.getValue(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000307 } else { //more args
308 // Create the frame index object for this incoming parameter...
309 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000310
311 // Create the SelectionDAG nodes corresponding to a load
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000312 //from this parameter
313 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000314 argt = DAG.getLoad(getValueType(I->getType()),
315 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000316 }
Andrew Lenharth032f2352005-02-22 21:59:48 +0000317 ++count;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000318 ArgValues.push_back(argt);
319 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000320
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000321 // If the functions takes variable number of arguments, copy all regs to stack
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000322 if (F.isVarArg()) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000323 VarArgsOffset = count * 8;
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000324 std::vector<SDOperand> LS;
325 for (int i = 0; i < 6; ++i) {
326 if (args_int[i] < 1024)
327 args_int[i] = AddLiveIn(MF,args_int[i], getRegClassFor(MVT::i64));
328 SDOperand argt = DAG.getCopyFromReg(args_int[i], MVT::i64, DAG.getRoot());
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000329 int FI = MFI->CreateFixedObject(8, -8 * (6 - i));
Andrew Lenharth558bc882005-06-18 18:34:52 +0000330 if (i == 0) VarArgsBase = FI;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000331 SDOperand SDFI = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000332 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt, SDFI, DAG.getSrcValue(NULL)));
333
334 if (args_float[i] < 1024)
335 args_float[i] = AddLiveIn(MF,args_float[i], getRegClassFor(MVT::f64));
336 argt = DAG.getCopyFromReg(args_float[i], MVT::f64, DAG.getRoot());
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000337 FI = MFI->CreateFixedObject(8, - 8 * (12 - i));
338 SDFI = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000339 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt, SDFI, DAG.getSrcValue(NULL)));
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000340 }
341
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000342 //Set up a token factor with all the stack traffic
343 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, LS));
344 }
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000345
346 // Finally, inform the code generator which regs we return values in.
347 switch (getValueType(F.getReturnType())) {
348 default: assert(0 && "Unknown type!");
349 case MVT::isVoid: break;
350 case MVT::i1:
351 case MVT::i8:
352 case MVT::i16:
353 case MVT::i32:
354 case MVT::i64:
355 MF.addLiveOut(Alpha::R0);
356 break;
357 case MVT::f32:
358 case MVT::f64:
359 MF.addLiveOut(Alpha::F0);
360 break;
361 }
362
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000363 //return the arguments
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000364 return ArgValues;
365}
366
367std::pair<SDOperand, SDOperand>
368AlphaTargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000369 const Type *RetTy, bool isVarArg,
Chris Lattneradf6a962005-05-13 18:50:42 +0000370 unsigned CallingConv, bool isTailCall,
Misha Brukman7847fca2005-04-22 17:54:37 +0000371 SDOperand Callee, ArgListTy &Args,
372 SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000373 int NumBytes = 0;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000374 if (Args.size() > 6)
375 NumBytes = (Args.size() - 6) * 8;
376
Chris Lattner16cd04d2005-05-12 23:24:06 +0000377 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000378 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000379 std::vector<SDOperand> args_to_use;
380 for (unsigned i = 0, e = Args.size(); i != e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000381 {
382 switch (getValueType(Args[i].second)) {
383 default: assert(0 && "Unexpected ValueType for argument!");
384 case MVT::i1:
385 case MVT::i8:
386 case MVT::i16:
387 case MVT::i32:
388 // Promote the integer to 64 bits. If the input type is signed use a
389 // sign extend, otherwise use a zero extend.
390 if (Args[i].second->isSigned())
391 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
392 else
393 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
394 break;
395 case MVT::i64:
396 case MVT::f64:
397 case MVT::f32:
398 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000399 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000400 args_to_use.push_back(Args[i].first);
401 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000402
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000403 std::vector<MVT::ValueType> RetVals;
404 MVT::ValueType RetTyVT = getValueType(RetTy);
405 if (RetTyVT != MVT::isVoid)
406 RetVals.push_back(RetTyVT);
407 RetVals.push_back(MVT::Other);
408
Misha Brukman4633f1c2005-04-21 23:13:11 +0000409 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000410 Chain, Callee, args_to_use), 0);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000411 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner16cd04d2005-05-12 23:24:06 +0000412 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000413 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000414 return std::make_pair(TheCall, Chain);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000415}
416
417std::pair<SDOperand, SDOperand>
Andrew Lenharth558bc882005-06-18 18:34:52 +0000418AlphaTargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG, SDOperand Dest) {
419 // vastart just stores the address of the VarArgsBase and VarArgsOffset
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000420 SDOperand FR = DAG.getFrameIndex(VarArgsBase, MVT::i64);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000421 SDOperand S1 = DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, Dest, DAG.getSrcValue(NULL));
422 SDOperand SA2 = DAG.getNode(ISD::ADD, MVT::i64, Dest, DAG.getConstant(8, MVT::i64));
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000423 SDOperand S2 = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, S1,
Andrew Lenharth558bc882005-06-18 18:34:52 +0000424 DAG.getConstant(VarArgsOffset, MVT::i64), SA2,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000425 DAG.getSrcValue(NULL), MVT::i32);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000426 return std::make_pair(S2, S2);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000427}
428
429std::pair<SDOperand,SDOperand> AlphaTargetLowering::
Andrew Lenharth558bc882005-06-18 18:34:52 +0000430LowerVAArgNext(SDOperand Chain, SDOperand VAList,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000431 const Type *ArgTy, SelectionDAG &DAG) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000432 SDOperand Base = DAG.getLoad(MVT::i64, Chain, VAList, DAG.getSrcValue(NULL));
433 SDOperand Tmp = DAG.getNode(ISD::ADD, MVT::i64, VAList,
434 DAG.getConstant(8, MVT::i64));
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000435 SDOperand Offset = DAG.getNode(ISD::SEXTLOAD, MVT::i64, Base.getValue(1), Tmp,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000436 DAG.getSrcValue(NULL), MVT::i32);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000437 SDOperand DataPtr = DAG.getNode(ISD::ADD, MVT::i64, Base, Offset);
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000438 if (ArgTy->isFloatingPoint())
439 {
440 //if fp && Offset < 6*8, then subtract 6*8 from DataPtr
441 SDOperand FPDataPtr = DAG.getNode(ISD::SUB, MVT::i64, DataPtr,
442 DAG.getConstant(8*6, MVT::i64));
443 SDOperand CC = DAG.getSetCC(ISD::SETLT, MVT::i64,
444 Offset, DAG.getConstant(8*6, MVT::i64));
445 DataPtr = DAG.getNode(ISD::SELECT, MVT::i64, CC, FPDataPtr, DataPtr);
446 }
447
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000448 SDOperand Result;
449 if (ArgTy == Type::IntTy)
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000450 Result = DAG.getNode(ISD::SEXTLOAD, MVT::i64, Offset.getValue(1), DataPtr,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000451 DAG.getSrcValue(NULL), MVT::i32);
452 else if (ArgTy == Type::UIntTy)
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000453 Result = DAG.getNode(ISD::ZEXTLOAD, MVT::i64, Offset.getValue(1), DataPtr,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000454 DAG.getSrcValue(NULL), MVT::i32);
455 else
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000456 Result = DAG.getLoad(getValueType(ArgTy), Offset.getValue(1), DataPtr,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000457 DAG.getSrcValue(NULL));
458
Andrew Lenharth558bc882005-06-18 18:34:52 +0000459 SDOperand NewOffset = DAG.getNode(ISD::ADD, MVT::i64, Offset,
460 DAG.getConstant(8, MVT::i64));
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000461 SDOperand Update = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Result.getValue(1), NewOffset,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000462 Tmp, DAG.getSrcValue(NULL), MVT::i32);
463 Result = DAG.getNode(ISD::TRUNCATE, getValueType(ArgTy), Result);
464
Andrew Lenharth558bc882005-06-18 18:34:52 +0000465 return std::make_pair(Result, Update);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000466}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000467
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000468std::pair<SDOperand,SDOperand> AlphaTargetLowering::
469LowerVACopy(SDOperand Chain, SDOperand Src, SDOperand Dest,
470 SelectionDAG &DAG) {
471 //Default to returning the input list
472 SDOperand Val = DAG.getLoad(getPointerTy(), Chain, Src, DAG.getSrcValue(NULL));
473 SDOperand Result = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
474 Val, Dest, DAG.getSrcValue(NULL));
475 SDOperand NP = DAG.getNode(ISD::ADD, MVT::i64, Src,
476 DAG.getConstant(8, MVT::i64));
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000477 Val = DAG.getNode(ISD::SEXTLOAD, MVT::i64, Result, NP, DAG.getSrcValue(NULL),
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000478 MVT::i32);
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000479 SDOperand NPD = DAG.getNode(ISD::ADD, MVT::i64, Dest,
480 DAG.getConstant(8, MVT::i64));
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000481 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Val.getValue(1),
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000482 Val, NPD, DAG.getSrcValue(NULL), MVT::i32);
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000483 return std::make_pair(Result, Result);
484}
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000485
486std::pair<SDOperand, SDOperand> AlphaTargetLowering::
487LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
488 SelectionDAG &DAG) {
489 abort();
490}
491
492
493
494
495
496namespace {
497
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000498//===--------------------------------------------------------------------===//
499/// ISel - Alpha specific code to select Alpha machine instructions for
500/// SelectionDAG operations.
501//===--------------------------------------------------------------------===//
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000502class AlphaISel : public SelectionDAGISel {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000503
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000504 /// AlphaLowering - This object fully describes how to lower LLVM code to an
505 /// Alpha-specific SelectionDAG.
506 AlphaTargetLowering AlphaLowering;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000507
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000508 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
509 // for sdiv and udiv until it is put into the future
510 // dag combiner.
511
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000512 /// ExprMap - As shared expressions are codegen'd, we keep track of which
513 /// vreg the value is produced in, so we only emit one copy of each compiled
514 /// tree.
515 static const unsigned notIn = (unsigned)(-1);
516 std::map<SDOperand, unsigned> ExprMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000517
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000518 //CCInvMap sometimes (SetNE) we have the inverse CC code for free
519 std::map<SDOperand, unsigned> CCInvMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000520
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000521 int count_ins;
522 int count_outs;
523 bool has_sym;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000524 int max_depth;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000525
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000526public:
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000527 AlphaISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering), AlphaLowering(TM)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000528 {}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000529
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000530 /// InstructionSelectBasicBlock - This callback is invoked by
531 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
532 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
Andrew Lenharth032f2352005-02-22 21:59:48 +0000533 DEBUG(BB->dump());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000534 count_ins = 0;
535 count_outs = 0;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000536 max_depth = 0;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000537 has_sym = false;
538
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000539 // Codegen the basic block.
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000540 ISelDAG = &DAG;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000541 max_depth = DAG.getRoot().getNodeDepth();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000542 Select(DAG.getRoot());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000543
544 if(has_sym)
545 ++count_ins;
546 if(EnableAlphaCount)
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000547 std::cerr << "COUNT: " << BB->getParent()->getFunction ()->getName() << " "
548 << BB->getNumber() << " "
549 << max_depth << " "
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000550 << count_ins << " "
551 << count_outs << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000552
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000553 // Clear state used for selection.
554 ExprMap.clear();
555 CCInvMap.clear();
556 }
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000557
558 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000559
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000560 unsigned SelectExpr(SDOperand N);
561 unsigned SelectExprFP(SDOperand N, unsigned Result);
562 void Select(SDOperand N);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000563
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000564 void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
565 void SelectBranchCC(SDOperand N);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000566 void MoveFP2Int(unsigned src, unsigned dst, bool isDouble);
567 void MoveInt2FP(unsigned src, unsigned dst, bool isDouble);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000568 //returns whether the sense of the comparison was inverted
569 bool SelectFPSetCC(SDOperand N, unsigned dst);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000570
571 // dag -> dag expanders for integer divide by constant
572 SDOperand BuildSDIVSequence(SDOperand N);
573 SDOperand BuildUDIVSequence(SDOperand N);
574
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000575};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000576}
577
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000578void AlphaISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000579 // If this function has live-in values, emit the copies from pregs to vregs at
580 // the top of the function, before anything else.
581 MachineBasicBlock *BB = MF.begin();
582 if (MF.livein_begin() != MF.livein_end()) {
583 SSARegMap *RegMap = MF.getSSARegMap();
584 for (MachineFunction::livein_iterator LI = MF.livein_begin(),
585 E = MF.livein_end(); LI != E; ++LI) {
586 const TargetRegisterClass *RC = RegMap->getRegClass(LI->second);
587 if (RC == Alpha::GPRCRegisterClass) {
588 BuildMI(BB, Alpha::BIS, 2, LI->second).addReg(LI->first).addReg(LI->first);
589 } else if (RC == Alpha::FPRCRegisterClass) {
590 BuildMI(BB, Alpha::CPYS, 2, LI->second).addReg(LI->first).addReg(LI->first);
591 } else {
592 assert(0 && "Unknown regclass!");
593 }
594 }
595 }
596}
597
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000598//Find the offset of the arg in it's parent's function
599static int getValueOffset(const Value* v)
600{
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000601 static int uniqneg = -1;
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000602 if (v == NULL)
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000603 return uniqneg--;
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000604
605 const Instruction* itarget = dyn_cast<Instruction>(v);
606 const BasicBlock* btarget = itarget->getParent();
607 const Function* ftarget = btarget->getParent();
608
609 //offset due to earlier BBs
610 int i = 0;
611 for(Function::const_iterator ii = ftarget->begin(); &*ii != btarget; ++ii)
612 i += ii->size();
613
614 for(BasicBlock::const_iterator ii = btarget->begin(); &*ii != itarget; ++ii)
615 ++i;
616
617 return i;
618}
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000619//Find the offset of the function in it's module
620static int getFunctionOffset(const Function* fun)
621{
622 const Module* M = fun->getParent();
623
624 //offset due to earlier BBs
625 int i = 0;
626 for(Module::const_iterator ii = M->begin(); &*ii != fun; ++ii)
627 ++i;
628
629 return i;
630}
631
632static int getUID()
633{
634 static int id = 0;
635 return ++id;
636}
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000637
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000638//Factorize a number using the list of constants
639static bool factorize(int v[], int res[], int size, uint64_t c)
640{
641 bool cont = true;
642 while (c != 1 && cont)
643 {
644 cont = false;
645 for(int i = 0; i < size; ++i)
646 {
647 if (c % v[i] == 0)
648 {
649 c /= v[i];
650 ++res[i];
651 cont=true;
652 }
653 }
654 }
655 return c == 1;
656}
657
658
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000659//Shamelessly adapted from PPC32
Misha Brukman4633f1c2005-04-21 23:13:11 +0000660// Structure used to return the necessary information to codegen an SDIV as
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000661// a multiply.
662struct ms {
663 int64_t m; // magic number
664 int64_t s; // shift amount
665};
666
667struct mu {
668 uint64_t m; // magic number
669 int64_t a; // add indicator
670 int64_t s; // shift amount
671};
672
673/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukman4633f1c2005-04-21 23:13:11 +0000674/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000675/// or -1.
676static struct ms magic(int64_t d) {
677 int64_t p;
678 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
679 const uint64_t two63 = 9223372036854775808ULL; // 2^63
680 struct ms mag;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000681
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000682 ad = abs(d);
683 t = two63 + ((uint64_t)d >> 63);
684 anc = t - 1 - t%ad; // absolute value of nc
Andrew Lenharth320174f2005-04-07 17:19:16 +0000685 p = 63; // initialize p
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000686 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
687 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
688 q2 = two63/ad; // initialize q2 = 2p/abs(d)
689 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
690 do {
691 p = p + 1;
692 q1 = 2*q1; // update q1 = 2p/abs(nc)
693 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
694 if (r1 >= anc) { // must be unsigned comparison
695 q1 = q1 + 1;
696 r1 = r1 - anc;
697 }
698 q2 = 2*q2; // update q2 = 2p/abs(d)
699 r2 = 2*r2; // update r2 = rem(2p/abs(d))
700 if (r2 >= ad) { // must be unsigned comparison
701 q2 = q2 + 1;
702 r2 = r2 - ad;
703 }
704 delta = ad - r2;
705 } while (q1 < delta || (q1 == delta && r1 == 0));
706
707 mag.m = q2 + 1;
708 if (d < 0) mag.m = -mag.m; // resulting magic number
709 mag.s = p - 64; // resulting shift
710 return mag;
711}
712
713/// magicu - calculate the magic numbers required to codegen an integer udiv as
714/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
715static struct mu magicu(uint64_t d)
716{
717 int64_t p;
718 uint64_t nc, delta, q1, r1, q2, r2;
719 struct mu magu;
720 magu.a = 0; // initialize "add" indicator
721 nc = - 1 - (-d)%d;
Andrew Lenharth320174f2005-04-07 17:19:16 +0000722 p = 63; // initialize p
723 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
724 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
725 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
726 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000727 do {
728 p = p + 1;
729 if (r1 >= nc - r1 ) {
730 q1 = 2*q1 + 1; // update q1
731 r1 = 2*r1 - nc; // update r1
732 }
733 else {
734 q1 = 2*q1; // update q1
735 r1 = 2*r1; // update r1
736 }
737 if (r2 + 1 >= d - r2) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000738 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000739 q2 = 2*q2 + 1; // update q2
740 r2 = 2*r2 + 1 - d; // update r2
741 }
742 else {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000743 if (q2 >= 0x8000000000000000ull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000744 q2 = 2*q2; // update q2
745 r2 = 2*r2 + 1; // update r2
746 }
747 delta = d - 1 - r2;
748 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
749 magu.m = q2 + 1; // resulting magic number
Andrew Lenharth320174f2005-04-07 17:19:16 +0000750 magu.s = p - 64; // resulting shift
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000751 return magu;
752}
753
754/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
755/// return a DAG expression to select that will generate the same value by
756/// multiplying by a magic number. See:
757/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000758SDOperand AlphaISel::BuildSDIVSequence(SDOperand N) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000759 int64_t d = (int64_t)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000760 ms magics = magic(d);
761 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000762 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000763 ISelDAG->getConstant(magics.m, MVT::i64));
764 // If d > 0 and m < 0, add the numerator
765 if (d > 0 && magics.m < 0)
766 Q = ISelDAG->getNode(ISD::ADD, MVT::i64, Q, N.getOperand(0));
767 // If d < 0 and m > 0, subtract the numerator.
768 if (d < 0 && magics.m > 0)
769 Q = ISelDAG->getNode(ISD::SUB, MVT::i64, Q, N.getOperand(0));
770 // Shift right algebraic if shift value is nonzero
771 if (magics.s > 0)
Misha Brukman4633f1c2005-04-21 23:13:11 +0000772 Q = ISelDAG->getNode(ISD::SRA, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000773 ISelDAG->getConstant(magics.s, MVT::i64));
774 // Extract the sign bit and add it to the quotient
Misha Brukman4633f1c2005-04-21 23:13:11 +0000775 SDOperand T =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000776 ISelDAG->getNode(ISD::SRL, MVT::i64, Q, ISelDAG->getConstant(63, MVT::i64));
777 return ISelDAG->getNode(ISD::ADD, MVT::i64, Q, T);
778}
779
780/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
781/// return a DAG expression to select that will generate the same value by
782/// multiplying by a magic number. See:
783/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000784SDOperand AlphaISel::BuildUDIVSequence(SDOperand N) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000785 unsigned d =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000786 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
787 mu magics = magicu(d);
788 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000789 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000790 ISelDAG->getConstant(magics.m, MVT::i64));
791 if (magics.a == 0) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000792 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000793 ISelDAG->getConstant(magics.s, MVT::i64));
794 } else {
795 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i64, N.getOperand(0), Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000796 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000797 ISelDAG->getConstant(1, MVT::i64));
798 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i64, NPQ, Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000799 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000800 ISelDAG->getConstant(magics.s-1, MVT::i64));
801 }
802 return Q;
803}
804
Andrew Lenhartha565c272005-04-06 22:03:13 +0000805//From PPC32
806/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
807/// returns zero when the input is not exactly a power of two.
808static unsigned ExactLog2(uint64_t Val) {
809 if (Val == 0 || (Val & (Val-1))) return 0;
810 unsigned Count = 0;
811 while (Val != 1) {
812 Val >>= 1;
813 ++Count;
814 }
815 return Count;
816}
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000817
818
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000819//These describe LDAx
Andrew Lenharthc0513832005-03-29 19:24:04 +0000820static const int IMM_LOW = -32768;
821static const int IMM_HIGH = 32767;
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000822static const int IMM_MULT = 65536;
823
824static long getUpper16(long l)
825{
826 long y = l / IMM_MULT;
827 if (l % IMM_MULT > IMM_HIGH)
828 ++y;
829 return y;
830}
831
832static long getLower16(long l)
833{
834 long h = getUpper16(l);
835 return l - h * IMM_MULT;
836}
837
Andrew Lenharth65838902005-02-06 16:22:15 +0000838static unsigned GetSymVersion(unsigned opcode)
839{
840 switch (opcode) {
841 default: assert(0 && "unknown load or store"); return 0;
842 case Alpha::LDQ: return Alpha::LDQ_SYM;
843 case Alpha::LDS: return Alpha::LDS_SYM;
844 case Alpha::LDT: return Alpha::LDT_SYM;
845 case Alpha::LDL: return Alpha::LDL_SYM;
846 case Alpha::LDBU: return Alpha::LDBU_SYM;
847 case Alpha::LDWU: return Alpha::LDWU_SYM;
848 case Alpha::LDW: return Alpha::LDW_SYM;
849 case Alpha::LDB: return Alpha::LDB_SYM;
850 case Alpha::STQ: return Alpha::STQ_SYM;
851 case Alpha::STS: return Alpha::STS_SYM;
852 case Alpha::STT: return Alpha::STT_SYM;
853 case Alpha::STL: return Alpha::STL_SYM;
854 case Alpha::STW: return Alpha::STW_SYM;
855 case Alpha::STB: return Alpha::STB_SYM;
856 }
857}
858
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000859void AlphaISel::MoveFP2Int(unsigned src, unsigned dst, bool isDouble)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000860{
861 unsigned Opc;
862 if (EnableAlphaFTOI) {
863 Opc = isDouble ? Alpha::FTOIT : Alpha::FTOIS;
864 BuildMI(BB, Opc, 1, dst).addReg(src);
865 } else {
866 //The hard way:
867 // Spill the integer to memory and reload it from there.
868 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
869 MachineFunction *F = BB->getParent();
870 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
871
872 Opc = isDouble ? Alpha::STT : Alpha::STS;
873 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
874 Opc = isDouble ? Alpha::LDQ : Alpha::LDL;
875 BuildMI(BB, Alpha::LDQ, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
876 }
877}
878
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000879void AlphaISel::MoveInt2FP(unsigned src, unsigned dst, bool isDouble)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000880{
881 unsigned Opc;
882 if (EnableAlphaFTOI) {
883 Opc = isDouble?Alpha::ITOFT:Alpha::ITOFS;
884 BuildMI(BB, Opc, 1, dst).addReg(src);
885 } else {
886 //The hard way:
887 // Spill the integer to memory and reload it from there.
888 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
889 MachineFunction *F = BB->getParent();
890 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
891
892 Opc = isDouble ? Alpha::STQ : Alpha::STL;
893 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
894 Opc = isDouble ? Alpha::LDT : Alpha::LDS;
895 BuildMI(BB, Opc, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
896 }
897}
898
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000899bool AlphaISel::SelectFPSetCC(SDOperand N, unsigned dst)
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000900{
901 SDNode *Node = N.Val;
902 unsigned Opc, Tmp1, Tmp2, Tmp3;
903 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node);
904
905 //assert(SetCC->getOperand(0).getValueType() != MVT::f32 && "SetCC f32 should have been promoted");
906 bool rev = false;
907 bool inv = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000908
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000909 switch (SetCC->getCondition()) {
910 default: Node->dump(); assert(0 && "Unknown FP comparison!");
911 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
912 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
913 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
914 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
915 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
916 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
917 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000918
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000919 ConstantFPSDNode *CN;
920 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
921 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
922 Tmp1 = Alpha::F31;
923 else
924 Tmp1 = SelectExpr(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000925
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000926 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
927 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
928 Tmp2 = Alpha::F31;
929 else
Chris Lattner9c9183a2005-04-30 04:44:07 +0000930 Tmp2 = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000931
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000932 //Can only compare doubles, and dag won't promote for me
933 if (SetCC->getOperand(0).getValueType() == MVT::f32)
934 {
935 //assert(0 && "Setcc On float?\n");
936 std::cerr << "Setcc on float!\n";
937 Tmp3 = MakeReg(MVT::f64);
938 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
939 Tmp1 = Tmp3;
940 }
941 if (SetCC->getOperand(1).getValueType() == MVT::f32)
942 {
943 //assert (0 && "Setcc On float?\n");
944 std::cerr << "Setcc on float!\n";
945 Tmp3 = MakeReg(MVT::f64);
946 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp2);
947 Tmp2 = Tmp3;
948 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000949
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000950 if (rev) std::swap(Tmp1, Tmp2);
951 //do the comparison
952 BuildMI(BB, Opc, 2, dst).addReg(Tmp1).addReg(Tmp2);
953 return inv;
954}
955
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000956//Check to see if the load is a constant offset from a base register
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000957void AlphaISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000958{
959 unsigned opcode = N.getOpcode();
Andrew Lenharth694c2982005-06-26 23:01:11 +0000960 if (opcode == ISD::ADD && N.getOperand(1).getOpcode() == ISD::Constant &&
961 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
962 { //Normal imm add
963 Reg = SelectExpr(N.getOperand(0));
964 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
965 return;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000966 }
967 Reg = SelectExpr(N);
968 offset = 0;
969 return;
970}
971
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000972void AlphaISel::SelectBranchCC(SDOperand N)
Andrew Lenharth445171a2005-02-08 00:40:03 +0000973{
974 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000975 MachineBasicBlock *Dest =
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000976 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
977 unsigned Opc = Alpha::WTF;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000978
Andrew Lenharth445171a2005-02-08 00:40:03 +0000979 Select(N.getOperand(0)); //chain
980 SDOperand CC = N.getOperand(1);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000981
Andrew Lenharth445171a2005-02-08 00:40:03 +0000982 if (CC.getOpcode() == ISD::SETCC)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000983 {
984 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
985 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
986 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharth09552bf2005-06-08 18:02:21 +0000987 bool RightZero = SetCC->getOperand(1).getOpcode() == ISD::Constant &&
988 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000989 bool isNE = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000990
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000991 //Fix up CC
992 ISD::CondCode cCode= SetCC->getCondition();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000993
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000994 if(cCode == ISD::SETNE)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000995 isNE = true;
Andrew Lenharth445171a2005-02-08 00:40:03 +0000996
Andrew Lenharth694c2982005-06-26 23:01:11 +0000997 if (RightZero) {
Andrew Lenharth09552bf2005-06-08 18:02:21 +0000998 switch (cCode) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000999 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
1000 case ISD::SETEQ: Opc = Alpha::BEQ; break;
1001 case ISD::SETLT: Opc = Alpha::BLT; break;
1002 case ISD::SETLE: Opc = Alpha::BLE; break;
1003 case ISD::SETGT: Opc = Alpha::BGT; break;
1004 case ISD::SETGE: Opc = Alpha::BGE; break;
1005 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
1006 case ISD::SETUGT: Opc = Alpha::BNE; break;
1007 case ISD::SETULE: Opc = Alpha::BEQ; break; //Technically you could have this CC
1008 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
1009 case ISD::SETNE: Opc = Alpha::BNE; break;
1010 }
Andrew Lenharth694c2982005-06-26 23:01:11 +00001011 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001012 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
1013 return;
1014 } else {
1015 unsigned Tmp1 = SelectExpr(CC);
1016 if (isNE)
1017 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
1018 else
1019 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001020 return;
1021 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001022 } else { //FP
1023 //Any comparison between 2 values should be codegened as an folded branch, as moving
1024 //CC to the integer register is very expensive
1025 //for a cmp b: c = a - b;
1026 //a = b: c = 0
1027 //a < b: c < 0
1028 //a > b: c > 0
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001029
1030 bool invTest = false;
1031 unsigned Tmp3;
1032
1033 ConstantFPSDNode *CN;
1034 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
1035 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1036 Tmp3 = SelectExpr(SetCC->getOperand(0));
1037 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
1038 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1039 {
1040 Tmp3 = SelectExpr(SetCC->getOperand(1));
1041 invTest = true;
1042 }
1043 else
1044 {
1045 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
1046 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
1047 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
1048 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1049 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1050 .addReg(Tmp1).addReg(Tmp2);
1051 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001052
1053 switch (SetCC->getCondition()) {
1054 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001055 case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
1056 case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
1057 case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
1058 case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
1059 case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
1060 case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001061 }
1062 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001063 return;
1064 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001065 abort(); //Should never be reached
1066 } else {
1067 //Giveup and do the stupid thing
1068 unsigned Tmp1 = SelectExpr(CC);
1069 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
1070 return;
1071 }
Andrew Lenharth445171a2005-02-08 00:40:03 +00001072 abort(); //Should never be reached
1073}
1074
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001075unsigned AlphaISel::SelectExprFP(SDOperand N, unsigned Result)
Andrew Lenharth40831c52005-01-28 06:57:18 +00001076{
1077 unsigned Tmp1, Tmp2, Tmp3;
1078 unsigned Opc = 0;
1079 SDNode *Node = N.Val;
1080 MVT::ValueType DestType = N.getValueType();
1081 unsigned opcode = N.getOpcode();
1082
1083 switch (opcode) {
1084 default:
1085 Node->dump();
1086 assert(0 && "Node not handled!\n");
Andrew Lenharth2c594352005-01-29 15:42:07 +00001087
Andrew Lenharth7332f3e2005-04-02 19:11:07 +00001088 case ISD::UNDEF: {
1089 BuildMI(BB, Alpha::IDEF, 0, Result);
1090 return Result;
1091 }
1092
Andrew Lenharth30b46d42005-04-02 19:04:58 +00001093 case ISD::FNEG:
1094 if(ISD::FABS == N.getOperand(0).getOpcode())
1095 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001096 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1097 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +00001098 } else {
Misha Brukman7847fca2005-04-22 17:54:37 +00001099 Tmp1 = SelectExpr(N.getOperand(0));
1100 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +00001101 }
1102 return Result;
1103
1104 case ISD::FABS:
1105 Tmp1 = SelectExpr(N.getOperand(0));
1106 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
1107 return Result;
1108
Andrew Lenharth9818c052005-02-05 13:19:12 +00001109 case ISD::SELECT:
1110 {
Andrew Lenharth45859692005-03-03 21:47:53 +00001111 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1112 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
1113 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
1114
1115 SDOperand CC = N.getOperand(0);
1116 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
1117
Misha Brukman4633f1c2005-04-21 23:13:11 +00001118 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth45859692005-03-03 21:47:53 +00001119 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
1120 { //FP Setcc -> Select yay!
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001121
1122
Andrew Lenharth45859692005-03-03 21:47:53 +00001123 //for a cmp b: c = a - b;
1124 //a = b: c = 0
1125 //a < b: c < 0
1126 //a > b: c > 0
Misha Brukman4633f1c2005-04-21 23:13:11 +00001127
Andrew Lenharth45859692005-03-03 21:47:53 +00001128 bool invTest = false;
1129 unsigned Tmp3;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001130
Andrew Lenharth45859692005-03-03 21:47:53 +00001131 ConstantFPSDNode *CN;
1132 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
1133 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1134 Tmp3 = SelectExpr(SetCC->getOperand(0));
1135 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
1136 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1137 {
1138 Tmp3 = SelectExpr(SetCC->getOperand(1));
1139 invTest = true;
1140 }
1141 else
1142 {
1143 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
1144 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
1145 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
1146 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1147 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1148 .addReg(Tmp1).addReg(Tmp2);
1149 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001150
Andrew Lenharth45859692005-03-03 21:47:53 +00001151 switch (SetCC->getCondition()) {
1152 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
1153 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNE : Alpha::FCMOVEQ; break;
1154 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGT : Alpha::FCMOVLT; break;
1155 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGE : Alpha::FCMOVLE; break;
1156 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLT : Alpha::FCMOVGT; break;
1157 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLE : Alpha::FCMOVGE; break;
1158 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQ : Alpha::FCMOVNE; break;
1159 }
Andrew Lenharth33819132005-03-04 20:09:23 +00001160 BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
Andrew Lenharth45859692005-03-03 21:47:53 +00001161 return Result;
1162 }
1163 else
1164 {
1165 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001166 BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV).addReg(Tmp1);
1167// // Spill the cond to memory and reload it from there.
1168// unsigned Tmp4 = MakeReg(MVT::f64);
1169// MoveIntFP(Tmp1, Tmp4, true);
1170// //now ideally, we don't have to do anything to the flag...
1171// // Get the condition into the zero flag.
1172// BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
Andrew Lenharth45859692005-03-03 21:47:53 +00001173 return Result;
1174 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001175 }
1176
Andrew Lenharthc1faced2005-02-01 01:37:24 +00001177 case ISD::FP_ROUND:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001178 assert (DestType == MVT::f32 &&
1179 N.getOperand(0).getValueType() == MVT::f64 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001180 "only f64 to f32 conversion supported here");
Andrew Lenharthc1faced2005-02-01 01:37:24 +00001181 Tmp1 = SelectExpr(N.getOperand(0));
1182 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
1183 return Result;
1184
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001185 case ISD::FP_EXTEND:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001186 assert (DestType == MVT::f64 &&
1187 N.getOperand(0).getValueType() == MVT::f32 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001188 "only f32 to f64 conversion supported here");
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001189 Tmp1 = SelectExpr(N.getOperand(0));
1190 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
1191 return Result;
1192
Andrew Lenharth2c594352005-01-29 15:42:07 +00001193 case ISD::CopyFromReg:
1194 {
1195 // Make sure we generate both values.
1196 if (Result != notIn)
1197 ExprMap[N.getValue(1)] = notIn; // Generate the token
1198 else
1199 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001200
Andrew Lenharth2c594352005-01-29 15:42:07 +00001201 SDOperand Chain = N.getOperand(0);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001202
Andrew Lenharth2c594352005-01-29 15:42:07 +00001203 Select(Chain);
1204 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1205 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1206 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
1207 return Result;
1208 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001209
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001210 case ISD::LOAD:
1211 {
1212 // Make sure we generate both values.
1213 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001214 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001215 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001216 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001217
Andrew Lenharth29219162005-02-07 06:31:44 +00001218 DestType = N.getValue(0).getValueType();
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001219
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001220 SDOperand Chain = N.getOperand(0);
1221 SDOperand Address = N.getOperand(1);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001222 Select(Chain);
Andrew Lenharth65838902005-02-06 16:22:15 +00001223 Opc = DestType == MVT::f64 ? Alpha::LDT : Alpha::LDS;
1224
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +00001225 if (EnableAlphaLSMark)
1226 {
1227 int i = getValueOffset(dyn_cast<SrcValueSDNode>(N.getOperand(2))->getValue());
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001228 int j = getFunctionOffset(BB->getParent()->getFunction());
1229 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +00001230 }
1231
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001232 if (Address.getOpcode() == ISD::GlobalAddress) {
1233 AlphaLowering.restoreGP(BB);
1234 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001235 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001236 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1237 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001238 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001239 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001240 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001241 has_sym = true;
Andrew Lenharth97127a12005-02-05 17:41:39 +00001242 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001243 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001244 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001245 BuildMI(BB, Opc, 2, Result)
1246 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1247 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001248 } else {
1249 long offset;
1250 SelectAddr(Address, Tmp1, offset);
1251 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1252 }
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001253 return Result;
1254 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001255 case ISD::ConstantFP:
1256 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
1257 if (CN->isExactlyValue(+0.0)) {
1258 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001259 } else if ( CN->isExactlyValue(-0.0)) {
1260 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001261 } else {
1262 abort();
1263 }
1264 }
1265 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001266
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001267 case ISD::SDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001268 case ISD::MUL:
1269 case ISD::ADD:
1270 case ISD::SUB:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001271 switch( opcode ) {
1272 case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS; break;
1273 case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS; break;
1274 case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS; break;
1275 case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS; break;
1276 };
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001277
1278 ConstantFPSDNode *CN;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001279 if (opcode == ISD::SUB
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001280 && (CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
1281 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1282 {
1283 Tmp2 = SelectExpr(N.getOperand(1));
1284 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp2).addReg(Tmp2);
1285 } else {
1286 Tmp1 = SelectExpr(N.getOperand(0));
1287 Tmp2 = SelectExpr(N.getOperand(1));
1288 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1289 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001290 return Result;
1291
Andrew Lenharth2c594352005-01-29 15:42:07 +00001292 case ISD::EXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001293 {
1294 //include a conversion sequence for float loads to double
1295 if (Result != notIn)
1296 ExprMap[N.getValue(1)] = notIn; // Generate the token
1297 else
1298 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001299
Andrew Lenhartha549deb2005-02-07 05:33:15 +00001300 Tmp1 = MakeReg(MVT::f32);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001301
1302 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001303 "EXTLOAD not from f32");
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001304 assert(Node->getValueType(0) == MVT::f64 && "EXTLOAD not to f64");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001305
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001306 SDOperand Chain = N.getOperand(0);
1307 SDOperand Address = N.getOperand(1);
1308 Select(Chain);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001309
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001310 if (Address.getOpcode() == ISD::GlobalAddress) {
1311 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001312 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001313 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1314 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001315 else if (ConstantPoolSDNode *CP =
1316 dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001317 {
1318 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001319 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001320 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addConstantPoolIndex(CP->getIndex());
1321 }
1322 else if(Address.getOpcode() == ISD::FrameIndex) {
1323 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
Andrew Lenharth032f2352005-02-22 21:59:48 +00001324 BuildMI(BB, Alpha::LDS, 2, Tmp1)
1325 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1326 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001327 } else {
1328 long offset;
1329 SelectAddr(Address, Tmp2, offset);
1330 BuildMI(BB, Alpha::LDS, 1, Tmp1).addImm(offset).addReg(Tmp2);
1331 }
Andrew Lenharth29219162005-02-07 06:31:44 +00001332 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001333 return Result;
1334 }
Andrew Lenharth2c594352005-01-29 15:42:07 +00001335
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001336 case ISD::SINT_TO_FP:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001337 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001338 assert (N.getOperand(0).getValueType() == MVT::i64
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001339 && "only quads can be loaded from");
Andrew Lenharth40831c52005-01-28 06:57:18 +00001340 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001341 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001342 MoveInt2FP(Tmp1, Tmp2, true);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001343 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
1344 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001345 return Result;
1346 }
1347 }
1348 assert(0 && "should not get here");
1349 return 0;
1350}
1351
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001352unsigned AlphaISel::SelectExpr(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001353 unsigned Result;
Andrew Lenharth2966e842005-04-07 18:15:28 +00001354 unsigned Tmp1, Tmp2 = 0, Tmp3;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001355 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001356 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001357
1358 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001359 MVT::ValueType DestType = N.getValueType();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001360
1361 unsigned &Reg = ExprMap[N];
1362 if (Reg) return Reg;
1363
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001364 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::TAILCALL)
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001365 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001366 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001367 else {
1368 // If this is a call instruction, make sure to prepare ALL of the result
1369 // values as well as the chain.
1370 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001371 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001372 else {
1373 Result = MakeReg(Node->getValueType(0));
1374 ExprMap[N.getValue(0)] = Result;
1375 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1376 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001377 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001378 }
1379 }
1380
Andrew Lenharth50d91d72005-04-30 14:19:13 +00001381 if ((DestType == MVT::f64 || DestType == MVT::f32 ||
1382 (
1383 (opcode == ISD::LOAD || opcode == ISD::CopyFromReg ||
1384 opcode == ISD::EXTLOAD) &&
1385 (N.getValue(0).getValueType() == MVT::f32 ||
1386 N.getValue(0).getValueType() == MVT::f64)
1387 ))
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001388 && opcode != ISD::CALL && opcode != ISD::TAILCALL
Andrew Lenharth06342c32005-02-07 06:21:37 +00001389 )
Andrew Lenharth40831c52005-01-28 06:57:18 +00001390 return SelectExprFP(N, Result);
1391
1392 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001393 default:
1394 Node->dump();
1395 assert(0 && "Node not handled!\n");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001396
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001397 case ISD::CTPOP:
1398 case ISD::CTTZ:
1399 case ISD::CTLZ:
1400 Opc = opcode == ISD::CTPOP ? Alpha::CTPOP :
1401 (opcode == ISD::CTTZ ? Alpha::CTTZ : Alpha::CTLZ);
1402 Tmp1 = SelectExpr(N.getOperand(0));
1403 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1404 return Result;
1405
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001406 case ISD::MULHU:
1407 Tmp1 = SelectExpr(N.getOperand(0));
1408 Tmp2 = SelectExpr(N.getOperand(1));
1409 BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth706be912005-04-07 13:55:53 +00001410 return Result;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001411 case ISD::MULHS:
1412 {
1413 //MULHU - Ra<63>*Rb - Rb<63>*Ra
1414 Tmp1 = SelectExpr(N.getOperand(0));
1415 Tmp2 = SelectExpr(N.getOperand(1));
1416 Tmp3 = MakeReg(MVT::i64);
1417 BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1418 unsigned V1 = MakeReg(MVT::i64);
1419 unsigned V2 = MakeReg(MVT::i64);
1420 BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31).addReg(Tmp1);
1421 BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31).addReg(Tmp2);
1422 unsigned IRes = MakeReg(MVT::i64);
1423 BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
1424 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
1425 return Result;
1426 }
Andrew Lenharth7332f3e2005-04-02 19:11:07 +00001427 case ISD::UNDEF: {
1428 BuildMI(BB, Alpha::IDEF, 0, Result);
1429 return Result;
1430 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001431
Andrew Lenharth032f2352005-02-22 21:59:48 +00001432 case ISD::DYNAMIC_STACKALLOC:
1433 // Generate both result values.
Andrew Lenharth3a7118d2005-02-23 17:33:42 +00001434 if (Result != notIn)
1435 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth032f2352005-02-22 21:59:48 +00001436 else
1437 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1438
1439 // FIXME: We are currently ignoring the requested alignment for handling
1440 // greater than the stack alignment. This will need to be revisited at some
1441 // point. Align = N.getOperand(2);
1442
1443 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1444 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1445 std::cerr << "Cannot allocate stack object with greater alignment than"
1446 << " the stack alignment yet!";
1447 abort();
1448 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001449
Andrew Lenharth032f2352005-02-22 21:59:48 +00001450 Select(N.getOperand(0));
1451 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1452 {
1453 if (CN->getValue() < 32000)
1454 {
1455 BuildMI(BB, Alpha::LDA, 2, Alpha::R30)
1456 .addImm(-CN->getValue()).addReg(Alpha::R30);
1457 } else {
1458 Tmp1 = SelectExpr(N.getOperand(1));
1459 // Subtract size from stack pointer, thereby allocating some space.
1460 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1461 }
1462 } else {
1463 Tmp1 = SelectExpr(N.getOperand(1));
1464 // Subtract size from stack pointer, thereby allocating some space.
1465 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1466 }
1467
1468 // Put a pointer to the space into the result register, by copying the stack
1469 // pointer.
Andrew Lenharth7bc47022005-02-22 23:29:25 +00001470 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
Andrew Lenharth032f2352005-02-22 21:59:48 +00001471 return Result;
1472
Andrew Lenharth33819132005-03-04 20:09:23 +00001473// case ISD::ConstantPool:
1474// Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1475// AlphaLowering.restoreGP(BB);
1476// BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(Tmp1);
1477// return Result;
Andrew Lenharth2c594352005-01-29 15:42:07 +00001478
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001479 case ISD::FrameIndex:
Andrew Lenharth032f2352005-02-22 21:59:48 +00001480 BuildMI(BB, Alpha::LDA, 2, Result)
1481 .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
1482 .addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001483 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001484
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001485 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001486 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001487 case ISD::SEXTLOAD:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001488 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001489 {
1490 // Make sure we generate both values.
1491 if (Result != notIn)
1492 ExprMap[N.getValue(1)] = notIn; // Generate the token
1493 else
1494 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001495
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001496 SDOperand Chain = N.getOperand(0);
1497 SDOperand Address = N.getOperand(1);
1498 Select(Chain);
1499
Misha Brukman4633f1c2005-04-21 23:13:11 +00001500 assert(Node->getValueType(0) == MVT::i64 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001501 "Unknown type to sign extend to.");
Andrew Lenharth03824012005-02-07 05:55:55 +00001502 if (opcode == ISD::LOAD)
1503 Opc = Alpha::LDQ;
1504 else
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001505 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
1506 default: Node->dump(); assert(0 && "Bad sign extend!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001507 case MVT::i32: Opc = Alpha::LDL;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001508 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001509 case MVT::i16: Opc = Alpha::LDWU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001510 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001511 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Misha Brukman4633f1c2005-04-21 23:13:11 +00001512 case MVT::i8: Opc = Alpha::LDBU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001513 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001514 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001515
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001516 if (EnableAlphaLSMark)
1517 {
1518 int i = getValueOffset(dyn_cast<SrcValueSDNode>(N.getOperand(2))->getValue());
1519 int j = getFunctionOffset(BB->getParent()->getFunction());
1520 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
1521 }
1522
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001523 if (Address.getOpcode() == ISD::GlobalAddress) {
1524 AlphaLowering.restoreGP(BB);
1525 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001526 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001527 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1528 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001529 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1530 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001531 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001532 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001533 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001534 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001535 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001536 BuildMI(BB, Opc, 2, Result)
1537 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1538 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001539 } else {
1540 long offset;
1541 SelectAddr(Address, Tmp1, offset);
1542 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1543 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001544 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001545 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001546
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001547 case ISD::GlobalAddress:
1548 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001549 has_sym = true;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001550 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
1551 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
1552 return Result;
1553
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001554 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001555 case ISD::CALL:
1556 {
1557 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001558
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001559 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001560 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001561
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001562 //grab the arguments
1563 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001564 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001565 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001566 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001567
Andrew Lenharth684f2292005-01-30 00:35:27 +00001568 //in reg args
1569 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001570 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001571 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001572 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +00001573 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001574 Alpha::F19, Alpha::F20, Alpha::F21};
1575 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001576 default:
1577 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001578 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001579 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001580 N.getOperand(i+2).getValueType() << "\n";
1581 assert(0 && "Unknown value type for call");
1582 case MVT::i1:
1583 case MVT::i8:
1584 case MVT::i16:
1585 case MVT::i32:
1586 case MVT::i64:
1587 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1588 break;
1589 case MVT::f32:
1590 case MVT::f64:
1591 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1592 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001593 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001594 }
Andrew Lenharth684f2292005-01-30 00:35:27 +00001595 //in mem args
1596 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001597 {
1598 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001599 default:
1600 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001601 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001602 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001603 N.getOperand(i+2).getValueType() << "\n";
1604 assert(0 && "Unknown value type for call");
1605 case MVT::i1:
1606 case MVT::i8:
1607 case MVT::i16:
1608 case MVT::i32:
1609 case MVT::i64:
1610 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1611 break;
1612 case MVT::f32:
1613 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1614 break;
1615 case MVT::f64:
1616 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1617 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001618 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001619 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001620 //build the right kind of call
1621 if (GlobalAddressSDNode *GASD =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001622 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001623 {
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001624 if (GASD->getGlobal()->isExternal()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001625 //use safe calling convention
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001626 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001627 has_sym = true;
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001628 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal());
1629 } else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001630 //use PC relative branch call
Andrew Lenharth1e0d9bd2005-04-14 17:34:20 +00001631 AlphaLowering.restoreGP(BB);
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001632 BuildMI(BB, Alpha::BSR, 1, Alpha::R26).addGlobalAddress(GASD->getGlobal(),true);
1633 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001634 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001635 else if (ExternalSymbolSDNode *ESSDN =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001636 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001637 {
1638 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001639 has_sym = true;
Andrew Lenharthba05ad62005-03-30 18:22:52 +00001640 BuildMI(BB, Alpha::CALL, 1).addExternalSymbol(ESSDN->getSymbol(), true);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001641 } else {
1642 //no need to restore GP as we are doing an indirect call
1643 Tmp1 = SelectExpr(N.getOperand(1));
1644 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
1645 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
1646 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001647
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001648 //push the result into a virtual register
Misha Brukman4633f1c2005-04-21 23:13:11 +00001649
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001650 switch (Node->getValueType(0)) {
1651 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001652 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001653 case MVT::i1:
1654 case MVT::i8:
1655 case MVT::i16:
1656 case MVT::i32:
1657 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001658 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
1659 break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001660 case MVT::f32:
1661 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001662 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
1663 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001664 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001665 return Result+N.ResNo;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001666 }
1667
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001668 case ISD::SIGN_EXTEND_INREG:
1669 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001670 //do SDIV opt for all levels of ints if not dividing by a constant
1671 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
1672 && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001673 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001674 unsigned Tmp4 = MakeReg(MVT::f64);
1675 unsigned Tmp5 = MakeReg(MVT::f64);
1676 unsigned Tmp6 = MakeReg(MVT::f64);
1677 unsigned Tmp7 = MakeReg(MVT::f64);
1678 unsigned Tmp8 = MakeReg(MVT::f64);
1679 unsigned Tmp9 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001680
1681 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1682 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1683 MoveInt2FP(Tmp1, Tmp4, true);
1684 MoveInt2FP(Tmp2, Tmp5, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001685 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Tmp4);
1686 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Tmp5);
1687 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
1688 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Tmp8);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001689 MoveFP2Int(Tmp9, Result, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001690 return Result;
1691 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001692
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001693 //Alpha has instructions for a bunch of signed 32 bit stuff
1694 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001695 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001696 switch (N.getOperand(0).getOpcode()) {
1697 case ISD::ADD:
1698 case ISD::SUB:
1699 case ISD::MUL:
1700 {
1701 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
1702 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
1703 //FIXME: first check for Scaled Adds and Subs!
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001704 ConstantSDNode* CSD = NULL;
1705 if(!isMul && N.getOperand(0).getOperand(0).getOpcode() == ISD::SHL &&
1706 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(0).getOperand(1))) &&
1707 (CSD->getValue() == 2 || CSD->getValue() == 3))
1708 {
1709 bool use4 = CSD->getValue() == 2;
1710 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1711 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1712 BuildMI(BB, isAdd?(use4?Alpha::S4ADDL:Alpha::S8ADDL):(use4?Alpha::S4SUBL:Alpha::S8SUBL),
1713 2,Result).addReg(Tmp1).addReg(Tmp2);
1714 }
1715 else if(isAdd && N.getOperand(0).getOperand(1).getOpcode() == ISD::SHL &&
1716 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1).getOperand(1))) &&
1717 (CSD->getValue() == 2 || CSD->getValue() == 3))
1718 {
1719 bool use4 = CSD->getValue() == 2;
1720 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1721 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1722 BuildMI(BB, use4?Alpha::S4ADDL:Alpha::S8ADDL, 2,Result).addReg(Tmp1).addReg(Tmp2);
1723 }
1724 else if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001725 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
1726 { //Normal imm add/sub
1727 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001728 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001729 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
1730 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001731 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001732 else
1733 { //Normal add/sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001734 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULL : Alpha::SUBL);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001735 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001736 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001737 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1738 }
1739 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001740 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001741 default: break; //Fall Though;
1742 }
1743 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001744 Tmp1 = SelectExpr(N.getOperand(0));
1745 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001746 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001747 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001748 {
1749 default:
1750 Node->dump();
1751 assert(0 && "Sign Extend InReg not there yet");
1752 break;
1753 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001754 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001755 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001756 break;
1757 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001758 case MVT::i16:
1759 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
1760 break;
1761 case MVT::i8:
1762 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
1763 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +00001764 case MVT::i1:
1765 Tmp2 = MakeReg(MVT::i64);
1766 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001767 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +00001768 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001769 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001770 return Result;
1771 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001772
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001773 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001774 {
1775 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1776 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
Andrew Lenharth694c2982005-06-26 23:01:11 +00001777 bool isConst = false;
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001778 int dir;
Misha Brukman7847fca2005-04-22 17:54:37 +00001779
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001780 //Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001781 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001782 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth694c2982005-06-26 23:01:11 +00001783 isConst = true;
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001784
1785 switch (SetCC->getCondition()) {
1786 default: Node->dump(); assert(0 && "Unknown integer comparison!");
Andrew Lenharth694c2982005-06-26 23:01:11 +00001787 case ISD::SETEQ:
1788 Opc = isConst ? Alpha::CMPEQi : Alpha::CMPEQ; dir=1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001789 case ISD::SETLT:
Andrew Lenharth694c2982005-06-26 23:01:11 +00001790 Opc = isConst ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001791 case ISD::SETLE:
Andrew Lenharth694c2982005-06-26 23:01:11 +00001792 Opc = isConst ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
1793 case ISD::SETGT: Opc = Alpha::CMPLT; dir = 2; break;
1794 case ISD::SETGE: Opc = Alpha::CMPLE; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001795 case ISD::SETULT:
Andrew Lenharth694c2982005-06-26 23:01:11 +00001796 Opc = isConst ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
1797 case ISD::SETUGT: Opc = Alpha::CMPULT; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001798 case ISD::SETULE:
Andrew Lenharth694c2982005-06-26 23:01:11 +00001799 Opc = isConst ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
1800 case ISD::SETUGE: Opc = Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001801 case ISD::SETNE: {//Handle this one special
1802 //std::cerr << "Alpha does not have a setne.\n";
1803 //abort();
1804 Tmp1 = SelectExpr(N.getOperand(0));
1805 Tmp2 = SelectExpr(N.getOperand(1));
1806 Tmp3 = MakeReg(MVT::i64);
1807 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001808 //Remeber we have the Inv for this CC
1809 CCInvMap[N] = Tmp3;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001810 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001811 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001812 return Result;
1813 }
1814 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001815 if (dir == 1) {
1816 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth694c2982005-06-26 23:01:11 +00001817 if (isConst) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001818 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1819 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1820 } else {
1821 Tmp2 = SelectExpr(N.getOperand(1));
1822 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1823 }
Andrew Lenharth694c2982005-06-26 23:01:11 +00001824 } else { //if (dir == 2) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001825 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth694c2982005-06-26 23:01:11 +00001826 Tmp2 = SelectExpr(N.getOperand(0));
1827 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001828 }
1829 } else {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001830 //do the comparison
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001831 Tmp1 = MakeReg(MVT::f64);
1832 bool inv = SelectFPSetCC(N, Tmp1);
1833
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001834 //now arrange for Result (int) to have a 1 or 0
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001835 Tmp2 = MakeReg(MVT::i64);
1836 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001837 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001838 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001839 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001840 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001841 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001842 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001843
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001844 case ISD::CopyFromReg:
1845 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001846 ++count_ins;
1847
Andrew Lenharth40831c52005-01-28 06:57:18 +00001848 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001849 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001850 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001851 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001852 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001853
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001854 SDOperand Chain = N.getOperand(0);
1855
1856 Select(Chain);
1857 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1858 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1859 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1860 return Result;
1861 }
1862
Misha Brukman4633f1c2005-04-21 23:13:11 +00001863 //Most of the plain arithmetic and logic share the same form, and the same
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001864 //constant immediate test
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001865 case ISD::XOR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001866 //Match Not
1867 if (N.getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001868 cast<ConstantSDNode>(N.getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001869 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001870 Tmp1 = SelectExpr(N.getOperand(0));
1871 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1872 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001873 }
1874 //Fall through
1875 case ISD::AND:
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001876 //handle zap
1877 if (opcode == ISD::AND && N.getOperand(1).getOpcode() == ISD::Constant)
1878 {
1879 uint64_t k = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1880 unsigned int build = 0;
1881 for(int i = 0; i < 8; ++i)
1882 {
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001883 if ((k & 0x00FF) == 0x00FF)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001884 build |= 1 << i;
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001885 else if ((k & 0x00FF) != 0)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001886 { build = 0; break; }
1887 k >>= 8;
1888 }
1889 if (build)
1890 {
1891 Tmp1 = SelectExpr(N.getOperand(0));
1892 BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1893 return Result;
1894 }
1895 }
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001896 case ISD::OR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001897 //Check operand(0) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001898 if (N.getOperand(0).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001899 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001900 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001901 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001902 switch(opcode) {
1903 case ISD::AND: Opc = Alpha::BIC; break;
1904 case ISD::OR: Opc = Alpha::ORNOT; break;
1905 case ISD::XOR: Opc = Alpha::EQV; break;
1906 }
1907 Tmp1 = SelectExpr(N.getOperand(1));
1908 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1909 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1910 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001911 }
1912 //Check operand(1) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001913 if (N.getOperand(1).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001914 N.getOperand(1).getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001915 cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001916 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001917 switch(opcode) {
1918 case ISD::AND: Opc = Alpha::BIC; break;
1919 case ISD::OR: Opc = Alpha::ORNOT; break;
1920 case ISD::XOR: Opc = Alpha::EQV; break;
1921 }
1922 Tmp1 = SelectExpr(N.getOperand(0));
1923 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1924 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1925 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001926 }
1927 //Fall through
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001928 case ISD::SHL:
1929 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001930 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001931 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001932 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1933 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001934 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001935 {
1936 switch(opcode) {
1937 case ISD::AND: Opc = Alpha::ANDi; break;
1938 case ISD::OR: Opc = Alpha::BISi; break;
1939 case ISD::XOR: Opc = Alpha::XORi; break;
1940 case ISD::SHL: Opc = Alpha::SLi; break;
1941 case ISD::SRL: Opc = Alpha::SRLi; break;
1942 case ISD::SRA: Opc = Alpha::SRAi; break;
1943 case ISD::MUL: Opc = Alpha::MULQi; break;
1944 };
1945 Tmp1 = SelectExpr(N.getOperand(0));
1946 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1947 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1948 } else {
1949 switch(opcode) {
1950 case ISD::AND: Opc = Alpha::AND; break;
1951 case ISD::OR: Opc = Alpha::BIS; break;
1952 case ISD::XOR: Opc = Alpha::XOR; break;
1953 case ISD::SHL: Opc = Alpha::SL; break;
1954 case ISD::SRL: Opc = Alpha::SRL; break;
1955 case ISD::SRA: Opc = Alpha::SRA; break;
1956 case ISD::MUL: Opc = Alpha::MULQ; break;
1957 };
1958 Tmp1 = SelectExpr(N.getOperand(0));
1959 Tmp2 = SelectExpr(N.getOperand(1));
1960 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1961 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001962 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001963
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001964 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001965 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001966 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001967 bool isAdd = opcode == ISD::ADD;
1968
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001969 //first check for Scaled Adds and Subs!
1970 //Valid for add and sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001971 ConstantSDNode* CSD = NULL;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001972 if(N.getOperand(0).getOpcode() == ISD::SHL &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001973 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
1974 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001975 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001976 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001977 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001978 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) && CSD->getValue() <= 255)
1979 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1980 2, Result).addReg(Tmp2).addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001981 else {
1982 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001983 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1984 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001985 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001986 }
1987 //Position prevents subs
Andrew Lenharth273a1f92005-04-07 14:18:13 +00001988 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001989 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) &&
1990 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001991 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001992 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001993 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001994 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(0))) && CSD->getValue() <= 255)
1995 BuildMI(BB, use4?Alpha::S4ADDQi:Alpha::S8ADDQi, 2, Result).addReg(Tmp2)
1996 .addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001997 else {
1998 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001999 BuildMI(BB, use4?Alpha::S4ADDQ:Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00002000 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002001 }
2002 //small addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002003 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
2004 CSD->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002005 { //Normal imm add/sub
2006 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
2007 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002008 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CSD->getValue());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002009 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002010 //larger addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002011 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
2012 CSD->getSignExtended() <= 32767 &&
2013 CSD->getSignExtended() >= -32767)
Andrew Lenharth74d00d82005-03-02 17:23:03 +00002014 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002015 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002016 Tmp2 = (long)CSD->getSignExtended();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002017 if (!isAdd)
2018 Tmp2 = -Tmp2;
2019 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002020 }
2021 //give up and do the operation
2022 else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002023 //Normal add/sub
2024 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
2025 Tmp1 = SelectExpr(N.getOperand(0));
2026 Tmp2 = SelectExpr(N.getOperand(1));
2027 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2028 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002029 return Result;
2030 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002031
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00002032 case ISD::SDIV:
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002033 {
Andrew Lenhartha565c272005-04-06 22:03:13 +00002034 ConstantSDNode* CSD;
2035 //check if we can convert into a shift!
2036 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
2037 (int64_t)CSD->getSignExtended() != 0 &&
2038 ExactLog2(abs((int64_t)CSD->getSignExtended())) != 0)
2039 {
2040 unsigned k = ExactLog2(abs(CSD->getSignExtended()));
2041 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenhartha565c272005-04-06 22:03:13 +00002042 if (k == 1)
2043 Tmp2 = Tmp1;
2044 else
2045 {
2046 Tmp2 = MakeReg(MVT::i64);
2047 BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
2048 }
2049 Tmp3 = MakeReg(MVT::i64);
2050 BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
2051 unsigned Tmp4 = MakeReg(MVT::i64);
2052 BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
2053 if ((int64_t)CSD->getSignExtended() > 0)
2054 BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
2055 else
2056 {
2057 unsigned Tmp5 = MakeReg(MVT::i64);
2058 BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
2059 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
2060 }
2061 return Result;
2062 }
2063 }
2064 //Else fall through
2065
2066 case ISD::UDIV:
2067 {
2068 ConstantSDNode* CSD;
2069 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
2070 ((int64_t)CSD->getSignExtended() >= 2 ||
2071 (int64_t)CSD->getSignExtended() <= -2))
2072 {
2073 // If this is a divide by constant, we can emit code using some magic
2074 // constants to implement it as a multiply instead.
2075 ExprMap.erase(N);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002076 if (opcode == ISD::SDIV)
Andrew Lenhartha565c272005-04-06 22:03:13 +00002077 return SelectExpr(BuildSDIVSequence(N));
2078 else
2079 return SelectExpr(BuildUDIVSequence(N));
2080 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002081 }
2082 //else fall though
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002083 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +00002084 case ISD::SREM:
Misha Brukman4633f1c2005-04-21 23:13:11 +00002085 //FIXME: alpha really doesn't support any of these operations,
Andrew Lenharth40831c52005-01-28 06:57:18 +00002086 // the ops are expanded into special library calls with
2087 // special calling conventions
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002088 //Restore GP because it is a call after all...
Andrew Lenharth40831c52005-01-28 06:57:18 +00002089 switch(opcode) {
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00002090 case ISD::UREM: Opc = Alpha::REMQU; break;
2091 case ISD::SREM: Opc = Alpha::REMQ; break;
2092 case ISD::UDIV: Opc = Alpha::DIVQU; break;
2093 case ISD::SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002094 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002095 Tmp1 = SelectExpr(N.getOperand(0));
2096 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth33819132005-03-04 20:09:23 +00002097 //set up regs explicitly (helps Reg alloc)
2098 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002099 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00002100 AlphaLowering.restoreGP(BB);
Andrew Lenharth33819132005-03-04 20:09:23 +00002101 BuildMI(BB, Opc, 2).addReg(Alpha::R24).addReg(Alpha::R25);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002102 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002103 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002104
Andrew Lenharthe76797c2005-02-01 20:40:27 +00002105 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002106 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002107 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002108 assert (DestType == MVT::i64 && "only quads can be loaded to");
2109 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00002110 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002111 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002112 if (SrcType == MVT::f32)
Misha Brukman7847fca2005-04-22 17:54:37 +00002113 {
2114 Tmp2 = MakeReg(MVT::f64);
2115 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
2116 Tmp1 = Tmp2;
2117 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002118 Tmp2 = MakeReg(MVT::f64);
2119 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00002120 MoveFP2Int(Tmp2, Result, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002121
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002122 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002123 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002124
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002125 case ISD::SELECT:
2126 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00002127 //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 +00002128 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002129 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2130 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002131 // Get the condition into the zero flag.
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002132 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002133
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002134 SDOperand CC = N.getOperand(0);
2135 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
2136
Misha Brukman4633f1c2005-04-21 23:13:11 +00002137 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002138 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
2139 { //FP Setcc -> Int Select
Misha Brukman7847fca2005-04-22 17:54:37 +00002140 Tmp1 = MakeReg(MVT::f64);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002141 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2142 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Misha Brukman7847fca2005-04-22 17:54:37 +00002143 bool inv = SelectFPSetCC(CC, Tmp1);
2144 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
2145 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
2146 return Result;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002147 }
2148 if (CC.getOpcode() == ISD::SETCC) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002149 //Int SetCC -> Select
2150 //Dropping the CC is only useful if we are comparing to 0
2151 if((SetCC->getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth694c2982005-06-26 23:01:11 +00002152 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0))
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002153 {
2154 //figure out a few things
Andrew Lenharth694c2982005-06-26 23:01:11 +00002155 bool useImm = N.getOperand(2).getOpcode() == ISD::Constant &&
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002156 cast<ConstantSDNode>(N.getOperand(2))->getValue() <= 255;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002157
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002158 //Fix up CC
2159 ISD::CondCode cCode= SetCC->getCondition();
Andrew Lenharth694c2982005-06-26 23:01:11 +00002160 if (useImm) //Invert sense to get Imm field right
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002161 cCode = ISD::getSetCCInverse(cCode, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002162
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002163 //Choose the CMOV
2164 switch (cCode) {
2165 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
2166 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
2167 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
2168 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
2169 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
2170 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
2171 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
2172 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2173 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break; //Technically you could have this CC
2174 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
2175 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2176 }
Andrew Lenharth694c2982005-06-26 23:01:11 +00002177 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002178
Andrew Lenharth694c2982005-06-26 23:01:11 +00002179 if (useImm) {
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002180 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
2181 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
Misha Brukman7847fca2005-04-22 17:54:37 +00002182 .addImm(cast<ConstantSDNode>(N.getOperand(2))->getValue())
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002183 .addReg(Tmp1);
2184 } else {
2185 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2186 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2187 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
2188 }
2189 return Result;
2190 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002191 //Otherwise, fall though
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002192 }
2193 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002194 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2195 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthe76797c2005-02-01 20:40:27 +00002196 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002197
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002198 return Result;
2199 }
2200
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002201 case ISD::Constant:
2202 {
Andrew Lenharthc0513832005-03-29 19:24:04 +00002203 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002204 if (val <= IMM_HIGH && val >= IMM_LOW) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002205 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002206 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002207 else if (val <= (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT &&
2208 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
2209 Tmp1 = MakeReg(MVT::i64);
2210 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val)).addReg(Alpha::R31);
2211 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002212 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002213 else {
2214 MachineConstantPool *CP = BB->getParent()->getConstantPool();
2215 ConstantUInt *C = ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
2216 unsigned CPI = CP->getConstantPoolIndex(C);
2217 AlphaLowering.restoreGP(BB);
2218 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CPI);
2219 }
2220 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002221 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002222 }
2223
2224 return 0;
2225}
2226
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002227void AlphaISel::Select(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002228 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00002229 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002230
Nate Begeman85fdeb22005-03-24 04:39:54 +00002231 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002232 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002233
2234 SDNode *Node = N.Val;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002235
Andrew Lenharth760270d2005-02-07 23:02:23 +00002236 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002237
2238 default:
2239 Node->dump(); std::cerr << "\n";
2240 assert(0 && "Node not handled yet!");
2241
2242 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00002243 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002244 return;
2245 }
2246
2247 case ISD::BR: {
2248 MachineBasicBlock *Dest =
2249 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2250
2251 Select(N.getOperand(0));
2252 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
2253 return;
2254 }
2255
2256 case ISD::ImplicitDef:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002257 ++count_ins;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002258 Select(N.getOperand(0));
2259 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
2260 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002261
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002262 case ISD::EntryToken: return; // Noop
2263
2264 case ISD::TokenFactor:
2265 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2266 Select(Node->getOperand(i));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002267
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002268 //N.Val->dump(); std::cerr << "\n";
2269 //assert(0 && "Node not handled yet!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00002270
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002271 return;
2272
2273 case ISD::CopyToReg:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002274 ++count_outs;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002275 Select(N.getOperand(0));
2276 Tmp1 = SelectExpr(N.getOperand(1));
2277 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002278
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002279 if (Tmp1 != Tmp2) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002280 if (N.getOperand(1).getValueType() == MVT::f64 ||
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002281 N.getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00002282 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2283 else
2284 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002285 }
2286 return;
2287
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002288 case ISD::RET:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002289 ++count_outs;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002290 switch (N.getNumOperands()) {
2291 default:
2292 std::cerr << N.getNumOperands() << "\n";
2293 for (unsigned i = 0; i < N.getNumOperands(); ++i)
2294 std::cerr << N.getOperand(i).getValueType() << "\n";
2295 Node->dump();
2296 assert(0 && "Unknown return instruction!");
2297 case 2:
2298 Select(N.getOperand(0));
2299 Tmp1 = SelectExpr(N.getOperand(1));
2300 switch (N.getOperand(1).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002301 default: Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002302 assert(0 && "All other types should have been promoted!!");
2303 case MVT::f64:
2304 case MVT::f32:
2305 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
2306 break;
2307 case MVT::i32:
2308 case MVT::i64:
2309 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
2310 break;
2311 }
2312 break;
2313 case 1:
2314 Select(N.getOperand(0));
2315 break;
2316 }
Andrew Lenharth3b918072005-06-27 15:36:48 +00002317 BuildMI(BB, Alpha::RET, 1, Alpha::R31).addReg(AlphaLowering.getRA()); // Just emit a 'ret' instruction
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002318 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002319
Misha Brukman4633f1c2005-04-21 23:13:11 +00002320 case ISD::TRUNCSTORE:
2321 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002322 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002323 SDOperand Chain = N.getOperand(0);
2324 SDOperand Value = N.getOperand(1);
2325 SDOperand Address = N.getOperand(2);
2326 Select(Chain);
2327
2328 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00002329
2330 if (opcode == ISD::STORE) {
2331 switch(Value.getValueType()) {
2332 default: assert(0 && "unknown Type in store");
2333 case MVT::i64: Opc = Alpha::STQ; break;
2334 case MVT::f64: Opc = Alpha::STT; break;
2335 case MVT::f32: Opc = Alpha::STS; break;
2336 }
2337 } else { //ISD::TRUNCSTORE
2338 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2339 default: assert(0 && "unknown Type in store");
2340 case MVT::i1: //FIXME: DAG does not promote this load
2341 case MVT::i8: Opc = Alpha::STB; break;
2342 case MVT::i16: Opc = Alpha::STW; break;
2343 case MVT::i32: Opc = Alpha::STL; break;
2344 }
Andrew Lenharth65838902005-02-06 16:22:15 +00002345 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00002346
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002347 if (EnableAlphaLSMark)
2348 {
2349 int i = getValueOffset(dyn_cast<SrcValueSDNode>(N.getOperand(3))->getValue());
2350 int j = getFunctionOffset(BB->getParent()->getFunction());
2351 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
2352 }
2353
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002354 if (Address.getOpcode() == ISD::GlobalAddress)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002355 {
2356 AlphaLowering.restoreGP(BB);
2357 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002358 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002359 BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
2360 }
Andrew Lenharth05380342005-02-07 05:07:00 +00002361 else if(Address.getOpcode() == ISD::FrameIndex)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002362 {
Andrew Lenharth032f2352005-02-22 21:59:48 +00002363 BuildMI(BB, Opc, 3).addReg(Tmp1)
2364 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
2365 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002366 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002367 else
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002368 {
2369 long offset;
2370 SelectAddr(Address, Tmp2, offset);
2371 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2372 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002373 return;
2374 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002375
2376 case ISD::EXTLOAD:
2377 case ISD::SEXTLOAD:
2378 case ISD::ZEXTLOAD:
2379 case ISD::LOAD:
2380 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002381 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002382 case ISD::CALL:
Andrew Lenharth032f2352005-02-22 21:59:48 +00002383 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002384 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002385 SelectExpr(N);
2386 return;
2387
Chris Lattner16cd04d2005-05-12 23:24:06 +00002388 case ISD::CALLSEQ_START:
2389 case ISD::CALLSEQ_END:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002390 Select(N.getOperand(0));
2391 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002392
Chris Lattner16cd04d2005-05-12 23:24:06 +00002393 Opc = N.getOpcode() == ISD::CALLSEQ_START ? Alpha::ADJUSTSTACKDOWN :
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002394 Alpha::ADJUSTSTACKUP;
2395 BuildMI(BB, Opc, 1).addImm(Tmp1);
2396 return;
Andrew Lenharth95762122005-03-31 21:24:06 +00002397
2398 case ISD::PCMARKER:
2399 Select(N.getOperand(0)); //Chain
2400 BuildMI(BB, Alpha::PCLABEL, 2).addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
2401 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002402 }
2403 assert(0 && "Should not be reached!");
2404}
2405
2406
2407/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
2408/// into a machine code representation using pattern matching and a machine
2409/// description file.
2410///
2411FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002412 return new AlphaISel(TM);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002413}
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002414