blob: e2610c440642ef1268c6c843fd477440a495b52c [file] [log] [blame]
Misha Brukman2a8350a2005-02-05 02:24:26 +00001//===- AlphaISelPattern.cpp - A pattern matching inst selector for Alpha --===//
Misha Brukman4633f1c2005-04-21 23:13:11 +00002//
Andrew Lenharth304d0f32005-01-22 23:41:55 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman4633f1c2005-04-21 23:13:11 +00007//
Andrew Lenharth304d0f32005-01-22 23:41:55 +00008//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for Alpha.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Alpha.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000015#include "AlphaRegisterInfo.h"
16#include "llvm/Constants.h" // FIXME: REMOVE
17#include "llvm/Function.h"
Andrew Lenharthb69f3422005-06-22 17:19:45 +000018#include "llvm/Module.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000019#include "llvm/CodeGen/MachineInstrBuilder.h"
20#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/SelectionDAG.h"
24#include "llvm/CodeGen/SelectionDAGISel.h"
25#include "llvm/CodeGen/SSARegMap.h"
26#include "llvm/Target/TargetData.h"
27#include "llvm/Target/TargetLowering.h"
28#include "llvm/Support/MathExtras.h"
29#include "llvm/ADT/Statistic.h"
Andrew Lenharth032f2352005-02-22 21:59:48 +000030#include "llvm/Support/Debug.h"
Andrew Lenharth95762122005-03-31 21:24:06 +000031#include "llvm/Support/CommandLine.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000032#include <set>
Andrew Lenharth684f2292005-01-30 00:35:27 +000033#include <algorithm>
Andrew Lenharth304d0f32005-01-22 23:41:55 +000034using namespace llvm;
35
Andrew Lenharth95762122005-03-31 21:24:06 +000036namespace llvm {
Misha Brukman4633f1c2005-04-21 23:13:11 +000037 cl::opt<bool> EnableAlphaIDIV("enable-alpha-intfpdiv",
38 cl::desc("Use the FP div instruction for integer div when possible"),
Andrew Lenharth95762122005-03-31 21:24:06 +000039 cl::Hidden);
Andrew Lenharth59009192005-05-04 19:12:09 +000040 cl::opt<bool> EnableAlphaFTOI("enable-alpha-FTOI",
Misha Brukman4633f1c2005-04-21 23:13:11 +000041 cl::desc("Enable use of ftoi* and itof* instructions (ev6 and higher)"),
Andrew Lenharth95762122005-03-31 21:24:06 +000042 cl::Hidden);
Andrew Lenharth59009192005-05-04 19:12:09 +000043 cl::opt<bool> EnableAlphaCT("enable-alpha-CT",
44 cl::desc("Enable use of the ctpop, ctlz, and cttz instructions"),
45 cl::Hidden);
Misha Brukman4633f1c2005-04-21 23:13:11 +000046 cl::opt<bool> EnableAlphaCount("enable-alpha-count",
47 cl::desc("Print estimates on live ins and outs"),
Andrew Lenhartha32b9e32005-04-08 17:28:49 +000048 cl::Hidden);
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +000049 cl::opt<bool> EnableAlphaLSMark("enable-alpha-lsmark",
Misha Brukman5e96a3a2005-06-06 19:08:04 +000050 cl::desc("Emit symbols to correlate Mem ops to LLVM Values"),
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +000051 cl::Hidden);
Andrew Lenharth95762122005-03-31 21:24:06 +000052}
53
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +000054namespace {
55 // Alpha Specific DAG Nodes
56 namespace AlphaISD {
57 enum NodeType {
58 // Start the numbering where the builtin ops leave off.
59 FIRST_NUMBER = ISD::BUILTIN_OP_END,
60
61 //Convert an int bit pattern in an FP reg to a Double or Float
62 //Has a dest type and a source
63 CVTQ,
64 //Move an Ireg to a FPreg
65 ITOF,
66 //Move a FPreg to an Ireg
67 FTOI,
68 };
69 }
70}
71
Andrew Lenharth304d0f32005-01-22 23:41:55 +000072//===----------------------------------------------------------------------===//
73// AlphaTargetLowering - Alpha Implementation of the TargetLowering interface
74namespace {
75 class AlphaTargetLowering : public TargetLowering {
Andrew Lenharth558bc882005-06-18 18:34:52 +000076 int VarArgsOffset; // What is the offset to the first vaarg
77 int VarArgsBase; // What is the base FrameIndex
Andrew Lenharth304d0f32005-01-22 23:41:55 +000078 unsigned GP; //GOT vreg
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +000079 unsigned RA; //Return Address
Andrew Lenharth304d0f32005-01-22 23:41:55 +000080 public:
81 AlphaTargetLowering(TargetMachine &TM) : TargetLowering(TM) {
82 // Set up the TargetLowering object.
Andrew Lenharth3d65d312005-01-27 03:49:45 +000083 //I am having problems with shr n ubyte 1
Andrew Lenharth879ef222005-02-02 17:00:21 +000084 setShiftAmountType(MVT::i64);
85 setSetCCResultType(MVT::i64);
Andrew Lenharthd3355e22005-04-07 20:11:32 +000086 setSetCCResultContents(ZeroOrOneSetCCResult);
Misha Brukman4633f1c2005-04-21 23:13:11 +000087
Andrew Lenharth304d0f32005-01-22 23:41:55 +000088 addRegisterClass(MVT::i64, Alpha::GPRCRegisterClass);
89 addRegisterClass(MVT::f64, Alpha::FPRCRegisterClass);
Andrew Lenharth3d65d312005-01-27 03:49:45 +000090 addRegisterClass(MVT::f32, Alpha::FPRCRegisterClass);
Misha Brukman4633f1c2005-04-21 23:13:11 +000091
Chris Lattnerda4d4692005-04-09 03:22:37 +000092 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000093
Andrew Lenharthec151362005-06-26 22:23:06 +000094 setOperationAction(ISD::EXTLOAD, MVT::i1, Promote);
95 setOperationAction(ISD::EXTLOAD, MVT::f32, Promote);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000096
Andrew Lenharthec151362005-06-26 22:23:06 +000097 setOperationAction(ISD::ZEXTLOAD, MVT::i1 , Expand);
98 setOperationAction(ISD::ZEXTLOAD, MVT::i32 , Expand);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000099
Andrew Lenharthec151362005-06-26 22:23:06 +0000100 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
101 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
102 setOperationAction(ISD::SEXTLOAD, MVT::i16, Expand);
103
104 setOperationAction(ISD::SREM, MVT::f32, Expand);
105 setOperationAction(ISD::SREM, MVT::f64, Expand);
106
107 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000108
Andrew Lenharth59009192005-05-04 19:12:09 +0000109 if (!EnableAlphaCT) {
110 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
111 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
Andrew Lenharthb5884d32005-05-04 19:25:37 +0000112 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
Andrew Lenharth59009192005-05-04 19:12:09 +0000113 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000114
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000115 //If this didn't legalize into a div....
116 // setOperationAction(ISD::SREM , MVT::i64, Expand);
117 // setOperationAction(ISD::UREM , MVT::i64, Expand);
118
119 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
120 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
121 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
Andrew Lenharth9818c052005-02-05 13:19:12 +0000122
Chris Lattner17234b72005-04-30 04:26:06 +0000123 // We don't support sin/cos/sqrt
124 setOperationAction(ISD::FSIN , MVT::f64, Expand);
125 setOperationAction(ISD::FCOS , MVT::f64, Expand);
126 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
127 setOperationAction(ISD::FSIN , MVT::f32, Expand);
128 setOperationAction(ISD::FCOS , MVT::f32, Expand);
129 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
130
Andrew Lenharth33819132005-03-04 20:09:23 +0000131 //Doesn't work yet
Chris Lattner17234b72005-04-30 04:26:06 +0000132 setOperationAction(ISD::SETCC, MVT::f32, Promote);
Andrew Lenharth572af902005-02-14 05:41:43 +0000133
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000134 //Try a couple things with a custom expander
135 //setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
136
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000137 computeRegisterProperties();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000138
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000139 addLegalFPImmediate(+0.0); //F31
140 addLegalFPImmediate(-0.0); //-F31
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000141 }
142
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000143 /// LowerOperation - Provide custom lowering hooks for some operations.
144 ///
145 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
146
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000147 /// LowerArguments - This hook must be implemented to indicate how we should
148 /// lower the arguments for the specified function, into the specified DAG.
149 virtual std::vector<SDOperand>
150 LowerArguments(Function &F, SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000151
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000152 /// LowerCallTo - This hook lowers an abstract call to a function into an
153 /// actual call.
154 virtual std::pair<SDOperand, SDOperand>
Chris Lattnerc57f6822005-05-12 19:56:45 +0000155 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattneradf6a962005-05-13 18:50:42 +0000156 bool isTailCall, SDOperand Callee, ArgListTy &Args,
157 SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000158
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000159 virtual std::pair<SDOperand, SDOperand>
Andrew Lenharth558bc882005-06-18 18:34:52 +0000160 LowerVAStart(SDOperand Chain, SelectionDAG &DAG, SDOperand Dest);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000161
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000162 virtual std::pair<SDOperand,SDOperand>
Andrew Lenharth558bc882005-06-18 18:34:52 +0000163 LowerVAArgNext(SDOperand Chain, SDOperand VAList,
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000164 const Type *ArgTy, SelectionDAG &DAG);
165
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000166 std::pair<SDOperand,SDOperand>
167 LowerVACopy(SDOperand Chain, SDOperand Src, SDOperand Dest,
168 SelectionDAG &DAG);
169
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000170 virtual std::pair<SDOperand, SDOperand>
171 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
172 SelectionDAG &DAG);
173
174 void restoreGP(MachineBasicBlock* BB)
175 {
176 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
177 }
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000178 void restoreRA(MachineBasicBlock* BB)
179 {
180 BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(RA).addReg(RA);
181 }
182
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000183 };
184}
185
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000186/// LowerOperation - Provide custom lowering hooks for some operations.
187///
188SDOperand AlphaTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
189 MachineFunction &MF = DAG.getMachineFunction();
190 switch (Op.getOpcode()) {
191 default: assert(0 && "Should not custom lower this!");
Misha Brukmanb8ee91a2005-06-06 17:39:46 +0000192#if 0
193 case ISD::SINT_TO_FP:
194 {
195 assert (Op.getOperand(0).getValueType() == MVT::i64
196 && "only quads can be loaded from");
197 SDOperand SRC;
198 if (EnableAlphaFTOI)
199 {
200 std::vector<MVT::ValueType> RTs;
201 RTs.push_back(Op.getValueType());
202 std::vector<SDOperand> Ops;
203 Ops.push_back(Op.getOperand(0));
204 SRC = DAG.getNode(AlphaISD::ITOF, RTs, Ops);
205 } else {
206 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
207 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
208 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
209 Op.getOperand(0), StackSlot, DAG.getSrcValue(NULL));
210 SRC = DAG.getLoad(Op.getValueType(), Store.getValue(0), StackSlot,
211 DAG.getSrcValue(NULL));
212 }
213 std::vector<MVT::ValueType> RTs;
214 RTs.push_back(Op.getValueType());
215 std::vector<SDOperand> Ops;
216 Ops.push_back(SRC);
217 return DAG.getNode(AlphaISD::CVTQ, RTs, Ops);
218 }
219#endif
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000220 }
Misha Brukmanb8ee91a2005-06-06 17:39:46 +0000221 return SDOperand();
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000222}
223
224
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000225/// AddLiveIn - This helper function adds the specified physical register to the
226/// MachineFunction as a live in value. It also creates a corresponding virtual
227/// register for it.
228static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
229 TargetRegisterClass *RC) {
230 assert(RC->contains(PReg) && "Not the correct regclass!");
231 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
232 MF.addLiveIn(PReg, VReg);
233 return VReg;
234}
235
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000236//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
237
238//For now, just use variable size stack frame format
239
240//In a standard call, the first six items are passed in registers $16
241//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
242//of argument-to-register correspondence.) The remaining items are
243//collected in a memory argument list that is a naturally aligned
244//array of quadwords. In a standard call, this list, if present, must
245//be passed at 0(SP).
Misha Brukman7847fca2005-04-22 17:54:37 +0000246//7 ... n 0(SP) ... (n-7)*8(SP)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000247
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000248// //#define FP $15
249// //#define RA $26
250// //#define PV $27
251// //#define GP $29
252// //#define SP $30
Misha Brukman4633f1c2005-04-21 23:13:11 +0000253
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000254std::vector<SDOperand>
Misha Brukman4633f1c2005-04-21 23:13:11 +0000255AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000256{
257 std::vector<SDOperand> ArgValues;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000258
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000259 MachineFunction &MF = DAG.getMachineFunction();
Andrew Lenharth05380342005-02-07 05:07:00 +0000260 MachineFrameInfo*MFI = MF.getFrameInfo();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000261
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000262 MachineBasicBlock& BB = MF.front();
263
264 //Handle the return address
265 //BuildMI(&BB, Alpha::IDEF, 0, Alpha::R26);
266
Misha Brukman4633f1c2005-04-21 23:13:11 +0000267 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000268 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000269 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000270 Alpha::F19, Alpha::F20, Alpha::F21};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000271 int count = 0;
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000272
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000273 GP = AddLiveIn(MF, Alpha::R29, getRegClassFor(MVT::i64));
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000274 RA = AddLiveIn(MF, Alpha::R26, getRegClassFor(MVT::i64));
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000275
Chris Lattnere4d5c442005-03-15 04:54:21 +0000276 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000277 {
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000278 SDOperand argt;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000279 if (count < 6) {
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000280 unsigned Vreg;
281 MVT::ValueType VT = getValueType(I->getType());
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000282 switch (VT) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000283 default:
284 std::cerr << "Unknown Type " << VT << "\n";
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000285 abort();
286 case MVT::f64:
287 case MVT::f32:
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000288 args_float[count] = AddLiveIn(MF,args_float[count], getRegClassFor(VT));
289 argt = DAG.getCopyFromReg(args_float[count], VT, DAG.getRoot());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000290 break;
291 case MVT::i1:
292 case MVT::i8:
293 case MVT::i16:
294 case MVT::i32:
295 case MVT::i64:
Andrew Lenharth591ec572005-05-31 18:42:18 +0000296 args_int[count] = AddLiveIn(MF, args_int[count], getRegClassFor(MVT::i64));
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000297 argt = DAG.getCopyFromReg(args_int[count], VT, DAG.getRoot());
Andrew Lenharth14f30c92005-05-31 18:37:16 +0000298 if (VT != MVT::i64)
299 argt = DAG.getNode(ISD::TRUNCATE, VT, argt);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000300 break;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000301 }
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000302 DAG.setRoot(argt.getValue(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000303 } else { //more args
304 // Create the frame index object for this incoming parameter...
305 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000306
307 // Create the SelectionDAG nodes corresponding to a load
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000308 //from this parameter
309 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000310 argt = DAG.getLoad(getValueType(I->getType()),
311 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000312 }
Andrew Lenharth032f2352005-02-22 21:59:48 +0000313 ++count;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000314 ArgValues.push_back(argt);
315 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000316
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000317 // If the functions takes variable number of arguments, copy all regs to stack
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000318 if (F.isVarArg()) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000319 VarArgsOffset = count * 8;
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000320 std::vector<SDOperand> LS;
321 for (int i = 0; i < 6; ++i) {
322 if (args_int[i] < 1024)
323 args_int[i] = AddLiveIn(MF,args_int[i], getRegClassFor(MVT::i64));
324 SDOperand argt = DAG.getCopyFromReg(args_int[i], MVT::i64, DAG.getRoot());
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000325 int FI = MFI->CreateFixedObject(8, -8 * (6 - i));
Andrew Lenharth558bc882005-06-18 18:34:52 +0000326 if (i == 0) VarArgsBase = FI;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000327 SDOperand SDFI = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000328 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt, SDFI, DAG.getSrcValue(NULL)));
329
330 if (args_float[i] < 1024)
331 args_float[i] = AddLiveIn(MF,args_float[i], getRegClassFor(MVT::f64));
332 argt = DAG.getCopyFromReg(args_float[i], MVT::f64, DAG.getRoot());
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000333 FI = MFI->CreateFixedObject(8, - 8 * (12 - i));
334 SDFI = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000335 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt, SDFI, DAG.getSrcValue(NULL)));
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000336 }
337
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000338 //Set up a token factor with all the stack traffic
339 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, LS));
340 }
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000341
342 // Finally, inform the code generator which regs we return values in.
343 switch (getValueType(F.getReturnType())) {
344 default: assert(0 && "Unknown type!");
345 case MVT::isVoid: break;
346 case MVT::i1:
347 case MVT::i8:
348 case MVT::i16:
349 case MVT::i32:
350 case MVT::i64:
351 MF.addLiveOut(Alpha::R0);
352 break;
353 case MVT::f32:
354 case MVT::f64:
355 MF.addLiveOut(Alpha::F0);
356 break;
357 }
358
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000359 //return the arguments
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000360 return ArgValues;
361}
362
363std::pair<SDOperand, SDOperand>
364AlphaTargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000365 const Type *RetTy, bool isVarArg,
Chris Lattneradf6a962005-05-13 18:50:42 +0000366 unsigned CallingConv, bool isTailCall,
Misha Brukman7847fca2005-04-22 17:54:37 +0000367 SDOperand Callee, ArgListTy &Args,
368 SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000369 int NumBytes = 0;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000370 if (Args.size() > 6)
371 NumBytes = (Args.size() - 6) * 8;
372
Chris Lattner16cd04d2005-05-12 23:24:06 +0000373 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000374 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000375 std::vector<SDOperand> args_to_use;
376 for (unsigned i = 0, e = Args.size(); i != e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000377 {
378 switch (getValueType(Args[i].second)) {
379 default: assert(0 && "Unexpected ValueType for argument!");
380 case MVT::i1:
381 case MVT::i8:
382 case MVT::i16:
383 case MVT::i32:
384 // Promote the integer to 64 bits. If the input type is signed use a
385 // sign extend, otherwise use a zero extend.
386 if (Args[i].second->isSigned())
387 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
388 else
389 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
390 break;
391 case MVT::i64:
392 case MVT::f64:
393 case MVT::f32:
394 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000395 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000396 args_to_use.push_back(Args[i].first);
397 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000398
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000399 std::vector<MVT::ValueType> RetVals;
400 MVT::ValueType RetTyVT = getValueType(RetTy);
401 if (RetTyVT != MVT::isVoid)
402 RetVals.push_back(RetTyVT);
403 RetVals.push_back(MVT::Other);
404
Misha Brukman4633f1c2005-04-21 23:13:11 +0000405 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000406 Chain, Callee, args_to_use), 0);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000407 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner16cd04d2005-05-12 23:24:06 +0000408 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000409 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000410 return std::make_pair(TheCall, Chain);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000411}
412
413std::pair<SDOperand, SDOperand>
Andrew Lenharth558bc882005-06-18 18:34:52 +0000414AlphaTargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG, SDOperand Dest) {
415 // vastart just stores the address of the VarArgsBase and VarArgsOffset
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000416 SDOperand FR = DAG.getFrameIndex(VarArgsBase, MVT::i64);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000417 SDOperand S1 = DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, Dest, DAG.getSrcValue(NULL));
418 SDOperand SA2 = DAG.getNode(ISD::ADD, MVT::i64, Dest, DAG.getConstant(8, MVT::i64));
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000419 SDOperand S2 = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, S1,
Andrew Lenharth558bc882005-06-18 18:34:52 +0000420 DAG.getConstant(VarArgsOffset, MVT::i64), SA2,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000421 DAG.getSrcValue(NULL), MVT::i32);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000422 return std::make_pair(S2, S2);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000423}
424
425std::pair<SDOperand,SDOperand> AlphaTargetLowering::
Andrew Lenharth558bc882005-06-18 18:34:52 +0000426LowerVAArgNext(SDOperand Chain, SDOperand VAList,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000427 const Type *ArgTy, SelectionDAG &DAG) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000428 SDOperand Base = DAG.getLoad(MVT::i64, Chain, VAList, DAG.getSrcValue(NULL));
429 SDOperand Tmp = DAG.getNode(ISD::ADD, MVT::i64, VAList,
430 DAG.getConstant(8, MVT::i64));
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000431 SDOperand Offset = DAG.getNode(ISD::SEXTLOAD, MVT::i64, Base.getValue(1), Tmp,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000432 DAG.getSrcValue(NULL), MVT::i32);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000433 SDOperand DataPtr = DAG.getNode(ISD::ADD, MVT::i64, Base, Offset);
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000434 if (ArgTy->isFloatingPoint())
435 {
436 //if fp && Offset < 6*8, then subtract 6*8 from DataPtr
437 SDOperand FPDataPtr = DAG.getNode(ISD::SUB, MVT::i64, DataPtr,
438 DAG.getConstant(8*6, MVT::i64));
439 SDOperand CC = DAG.getSetCC(ISD::SETLT, MVT::i64,
440 Offset, DAG.getConstant(8*6, MVT::i64));
441 DataPtr = DAG.getNode(ISD::SELECT, MVT::i64, CC, FPDataPtr, DataPtr);
442 }
443
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000444 SDOperand Result;
445 if (ArgTy == Type::IntTy)
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000446 Result = DAG.getNode(ISD::SEXTLOAD, MVT::i64, Offset.getValue(1), DataPtr,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000447 DAG.getSrcValue(NULL), MVT::i32);
448 else if (ArgTy == Type::UIntTy)
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000449 Result = DAG.getNode(ISD::ZEXTLOAD, MVT::i64, Offset.getValue(1), DataPtr,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000450 DAG.getSrcValue(NULL), MVT::i32);
451 else
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000452 Result = DAG.getLoad(getValueType(ArgTy), Offset.getValue(1), DataPtr,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000453 DAG.getSrcValue(NULL));
454
Andrew Lenharth558bc882005-06-18 18:34:52 +0000455 SDOperand NewOffset = DAG.getNode(ISD::ADD, MVT::i64, Offset,
456 DAG.getConstant(8, MVT::i64));
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000457 SDOperand Update = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Result.getValue(1), NewOffset,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000458 Tmp, DAG.getSrcValue(NULL), MVT::i32);
459 Result = DAG.getNode(ISD::TRUNCATE, getValueType(ArgTy), Result);
460
Andrew Lenharth558bc882005-06-18 18:34:52 +0000461 return std::make_pair(Result, Update);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000462}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000463
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000464std::pair<SDOperand,SDOperand> AlphaTargetLowering::
465LowerVACopy(SDOperand Chain, SDOperand Src, SDOperand Dest,
466 SelectionDAG &DAG) {
467 //Default to returning the input list
468 SDOperand Val = DAG.getLoad(getPointerTy(), Chain, Src, DAG.getSrcValue(NULL));
469 SDOperand Result = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
470 Val, Dest, DAG.getSrcValue(NULL));
471 SDOperand NP = DAG.getNode(ISD::ADD, MVT::i64, Src,
472 DAG.getConstant(8, MVT::i64));
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000473 Val = DAG.getNode(ISD::SEXTLOAD, MVT::i64, Result, NP, DAG.getSrcValue(NULL),
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000474 MVT::i32);
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000475 SDOperand NPD = DAG.getNode(ISD::ADD, MVT::i64, Dest,
476 DAG.getConstant(8, MVT::i64));
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000477 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Val.getValue(1),
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000478 Val, NPD, DAG.getSrcValue(NULL), MVT::i32);
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000479 return std::make_pair(Result, Result);
480}
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000481
482std::pair<SDOperand, SDOperand> AlphaTargetLowering::
483LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
484 SelectionDAG &DAG) {
485 abort();
486}
487
488
489
490
491
492namespace {
493
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000494//===--------------------------------------------------------------------===//
495/// ISel - Alpha specific code to select Alpha machine instructions for
496/// SelectionDAG operations.
497//===--------------------------------------------------------------------===//
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000498class AlphaISel : public SelectionDAGISel {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000499
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000500 /// AlphaLowering - This object fully describes how to lower LLVM code to an
501 /// Alpha-specific SelectionDAG.
502 AlphaTargetLowering AlphaLowering;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000503
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000504 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
505 // for sdiv and udiv until it is put into the future
506 // dag combiner.
507
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000508 /// ExprMap - As shared expressions are codegen'd, we keep track of which
509 /// vreg the value is produced in, so we only emit one copy of each compiled
510 /// tree.
511 static const unsigned notIn = (unsigned)(-1);
512 std::map<SDOperand, unsigned> ExprMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000513
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000514 //CCInvMap sometimes (SetNE) we have the inverse CC code for free
515 std::map<SDOperand, unsigned> CCInvMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000516
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000517 int count_ins;
518 int count_outs;
519 bool has_sym;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000520 int max_depth;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000521
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000522public:
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000523 AlphaISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering), AlphaLowering(TM)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000524 {}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000525
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000526 /// InstructionSelectBasicBlock - This callback is invoked by
527 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
528 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
Andrew Lenharth032f2352005-02-22 21:59:48 +0000529 DEBUG(BB->dump());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000530 count_ins = 0;
531 count_outs = 0;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000532 max_depth = 0;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000533 has_sym = false;
534
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000535 // Codegen the basic block.
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000536 ISelDAG = &DAG;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000537 max_depth = DAG.getRoot().getNodeDepth();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000538 Select(DAG.getRoot());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000539
540 if(has_sym)
541 ++count_ins;
542 if(EnableAlphaCount)
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000543 std::cerr << "COUNT: " << BB->getParent()->getFunction ()->getName() << " "
544 << BB->getNumber() << " "
545 << max_depth << " "
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000546 << count_ins << " "
547 << count_outs << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000548
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000549 // Clear state used for selection.
550 ExprMap.clear();
551 CCInvMap.clear();
552 }
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000553
554 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000555
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000556 unsigned SelectExpr(SDOperand N);
557 unsigned SelectExprFP(SDOperand N, unsigned Result);
558 void Select(SDOperand N);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000559
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000560 void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
561 void SelectBranchCC(SDOperand N);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000562 void MoveFP2Int(unsigned src, unsigned dst, bool isDouble);
563 void MoveInt2FP(unsigned src, unsigned dst, bool isDouble);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000564 //returns whether the sense of the comparison was inverted
565 bool SelectFPSetCC(SDOperand N, unsigned dst);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000566
567 // dag -> dag expanders for integer divide by constant
568 SDOperand BuildSDIVSequence(SDOperand N);
569 SDOperand BuildUDIVSequence(SDOperand N);
570
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000571};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000572}
573
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000574void AlphaISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000575 // If this function has live-in values, emit the copies from pregs to vregs at
576 // the top of the function, before anything else.
577 MachineBasicBlock *BB = MF.begin();
578 if (MF.livein_begin() != MF.livein_end()) {
579 SSARegMap *RegMap = MF.getSSARegMap();
580 for (MachineFunction::livein_iterator LI = MF.livein_begin(),
581 E = MF.livein_end(); LI != E; ++LI) {
582 const TargetRegisterClass *RC = RegMap->getRegClass(LI->second);
583 if (RC == Alpha::GPRCRegisterClass) {
584 BuildMI(BB, Alpha::BIS, 2, LI->second).addReg(LI->first).addReg(LI->first);
585 } else if (RC == Alpha::FPRCRegisterClass) {
586 BuildMI(BB, Alpha::CPYS, 2, LI->second).addReg(LI->first).addReg(LI->first);
587 } else {
588 assert(0 && "Unknown regclass!");
589 }
590 }
591 }
592}
593
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000594//Find the offset of the arg in it's parent's function
595static int getValueOffset(const Value* v)
596{
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000597 static int uniqneg = -1;
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000598 if (v == NULL)
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000599 return uniqneg--;
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000600
601 const Instruction* itarget = dyn_cast<Instruction>(v);
602 const BasicBlock* btarget = itarget->getParent();
603 const Function* ftarget = btarget->getParent();
604
605 //offset due to earlier BBs
606 int i = 0;
607 for(Function::const_iterator ii = ftarget->begin(); &*ii != btarget; ++ii)
608 i += ii->size();
609
610 for(BasicBlock::const_iterator ii = btarget->begin(); &*ii != itarget; ++ii)
611 ++i;
612
613 return i;
614}
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000615//Find the offset of the function in it's module
616static int getFunctionOffset(const Function* fun)
617{
618 const Module* M = fun->getParent();
619
620 //offset due to earlier BBs
621 int i = 0;
622 for(Module::const_iterator ii = M->begin(); &*ii != fun; ++ii)
623 ++i;
624
625 return i;
626}
627
628static int getUID()
629{
630 static int id = 0;
631 return ++id;
632}
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000633
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000634//Factorize a number using the list of constants
635static bool factorize(int v[], int res[], int size, uint64_t c)
636{
637 bool cont = true;
638 while (c != 1 && cont)
639 {
640 cont = false;
641 for(int i = 0; i < size; ++i)
642 {
643 if (c % v[i] == 0)
644 {
645 c /= v[i];
646 ++res[i];
647 cont=true;
648 }
649 }
650 }
651 return c == 1;
652}
653
654
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000655//Shamelessly adapted from PPC32
Misha Brukman4633f1c2005-04-21 23:13:11 +0000656// Structure used to return the necessary information to codegen an SDIV as
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000657// a multiply.
658struct ms {
659 int64_t m; // magic number
660 int64_t s; // shift amount
661};
662
663struct mu {
664 uint64_t m; // magic number
665 int64_t a; // add indicator
666 int64_t s; // shift amount
667};
668
669/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukman4633f1c2005-04-21 23:13:11 +0000670/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000671/// or -1.
672static struct ms magic(int64_t d) {
673 int64_t p;
674 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
675 const uint64_t two63 = 9223372036854775808ULL; // 2^63
676 struct ms mag;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000677
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000678 ad = abs(d);
679 t = two63 + ((uint64_t)d >> 63);
680 anc = t - 1 - t%ad; // absolute value of nc
Andrew Lenharth320174f2005-04-07 17:19:16 +0000681 p = 63; // initialize p
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000682 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
683 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
684 q2 = two63/ad; // initialize q2 = 2p/abs(d)
685 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
686 do {
687 p = p + 1;
688 q1 = 2*q1; // update q1 = 2p/abs(nc)
689 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
690 if (r1 >= anc) { // must be unsigned comparison
691 q1 = q1 + 1;
692 r1 = r1 - anc;
693 }
694 q2 = 2*q2; // update q2 = 2p/abs(d)
695 r2 = 2*r2; // update r2 = rem(2p/abs(d))
696 if (r2 >= ad) { // must be unsigned comparison
697 q2 = q2 + 1;
698 r2 = r2 - ad;
699 }
700 delta = ad - r2;
701 } while (q1 < delta || (q1 == delta && r1 == 0));
702
703 mag.m = q2 + 1;
704 if (d < 0) mag.m = -mag.m; // resulting magic number
705 mag.s = p - 64; // resulting shift
706 return mag;
707}
708
709/// magicu - calculate the magic numbers required to codegen an integer udiv as
710/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
711static struct mu magicu(uint64_t d)
712{
713 int64_t p;
714 uint64_t nc, delta, q1, r1, q2, r2;
715 struct mu magu;
716 magu.a = 0; // initialize "add" indicator
717 nc = - 1 - (-d)%d;
Andrew Lenharth320174f2005-04-07 17:19:16 +0000718 p = 63; // initialize p
719 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
720 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
721 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
722 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000723 do {
724 p = p + 1;
725 if (r1 >= nc - r1 ) {
726 q1 = 2*q1 + 1; // update q1
727 r1 = 2*r1 - nc; // update r1
728 }
729 else {
730 q1 = 2*q1; // update q1
731 r1 = 2*r1; // update r1
732 }
733 if (r2 + 1 >= d - r2) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000734 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000735 q2 = 2*q2 + 1; // update q2
736 r2 = 2*r2 + 1 - d; // update r2
737 }
738 else {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000739 if (q2 >= 0x8000000000000000ull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000740 q2 = 2*q2; // update q2
741 r2 = 2*r2 + 1; // update r2
742 }
743 delta = d - 1 - r2;
744 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
745 magu.m = q2 + 1; // resulting magic number
Andrew Lenharth320174f2005-04-07 17:19:16 +0000746 magu.s = p - 64; // resulting shift
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000747 return magu;
748}
749
750/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
751/// return a DAG expression to select that will generate the same value by
752/// multiplying by a magic number. See:
753/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000754SDOperand AlphaISel::BuildSDIVSequence(SDOperand N) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000755 int64_t d = (int64_t)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000756 ms magics = magic(d);
757 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000758 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000759 ISelDAG->getConstant(magics.m, MVT::i64));
760 // If d > 0 and m < 0, add the numerator
761 if (d > 0 && magics.m < 0)
762 Q = ISelDAG->getNode(ISD::ADD, MVT::i64, Q, N.getOperand(0));
763 // If d < 0 and m > 0, subtract the numerator.
764 if (d < 0 && magics.m > 0)
765 Q = ISelDAG->getNode(ISD::SUB, MVT::i64, Q, N.getOperand(0));
766 // Shift right algebraic if shift value is nonzero
767 if (magics.s > 0)
Misha Brukman4633f1c2005-04-21 23:13:11 +0000768 Q = ISelDAG->getNode(ISD::SRA, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000769 ISelDAG->getConstant(magics.s, MVT::i64));
770 // Extract the sign bit and add it to the quotient
Misha Brukman4633f1c2005-04-21 23:13:11 +0000771 SDOperand T =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000772 ISelDAG->getNode(ISD::SRL, MVT::i64, Q, ISelDAG->getConstant(63, MVT::i64));
773 return ISelDAG->getNode(ISD::ADD, MVT::i64, Q, T);
774}
775
776/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
777/// return a DAG expression to select that will generate the same value by
778/// multiplying by a magic number. See:
779/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000780SDOperand AlphaISel::BuildUDIVSequence(SDOperand N) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000781 unsigned d =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000782 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
783 mu magics = magicu(d);
784 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000785 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000786 ISelDAG->getConstant(magics.m, MVT::i64));
787 if (magics.a == 0) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000788 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000789 ISelDAG->getConstant(magics.s, MVT::i64));
790 } else {
791 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i64, N.getOperand(0), Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000792 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000793 ISelDAG->getConstant(1, MVT::i64));
794 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i64, NPQ, Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000795 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000796 ISelDAG->getConstant(magics.s-1, MVT::i64));
797 }
798 return Q;
799}
800
Andrew Lenhartha565c272005-04-06 22:03:13 +0000801//From PPC32
802/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
803/// returns zero when the input is not exactly a power of two.
804static unsigned ExactLog2(uint64_t Val) {
805 if (Val == 0 || (Val & (Val-1))) return 0;
806 unsigned Count = 0;
807 while (Val != 1) {
808 Val >>= 1;
809 ++Count;
810 }
811 return Count;
812}
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000813
814
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000815//These describe LDAx
Andrew Lenharthc0513832005-03-29 19:24:04 +0000816static const int IMM_LOW = -32768;
817static const int IMM_HIGH = 32767;
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000818static const int IMM_MULT = 65536;
819
820static long getUpper16(long l)
821{
822 long y = l / IMM_MULT;
823 if (l % IMM_MULT > IMM_HIGH)
824 ++y;
825 return y;
826}
827
828static long getLower16(long l)
829{
830 long h = getUpper16(l);
831 return l - h * IMM_MULT;
832}
833
Andrew Lenharth65838902005-02-06 16:22:15 +0000834static unsigned GetSymVersion(unsigned opcode)
835{
836 switch (opcode) {
837 default: assert(0 && "unknown load or store"); return 0;
838 case Alpha::LDQ: return Alpha::LDQ_SYM;
839 case Alpha::LDS: return Alpha::LDS_SYM;
840 case Alpha::LDT: return Alpha::LDT_SYM;
841 case Alpha::LDL: return Alpha::LDL_SYM;
842 case Alpha::LDBU: return Alpha::LDBU_SYM;
843 case Alpha::LDWU: return Alpha::LDWU_SYM;
844 case Alpha::LDW: return Alpha::LDW_SYM;
845 case Alpha::LDB: return Alpha::LDB_SYM;
846 case Alpha::STQ: return Alpha::STQ_SYM;
847 case Alpha::STS: return Alpha::STS_SYM;
848 case Alpha::STT: return Alpha::STT_SYM;
849 case Alpha::STL: return Alpha::STL_SYM;
850 case Alpha::STW: return Alpha::STW_SYM;
851 case Alpha::STB: return Alpha::STB_SYM;
852 }
853}
854
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000855void AlphaISel::MoveFP2Int(unsigned src, unsigned dst, bool isDouble)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000856{
857 unsigned Opc;
858 if (EnableAlphaFTOI) {
859 Opc = isDouble ? Alpha::FTOIT : Alpha::FTOIS;
860 BuildMI(BB, Opc, 1, dst).addReg(src);
861 } else {
862 //The hard way:
863 // Spill the integer to memory and reload it from there.
864 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
865 MachineFunction *F = BB->getParent();
866 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
867
868 Opc = isDouble ? Alpha::STT : Alpha::STS;
869 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
870 Opc = isDouble ? Alpha::LDQ : Alpha::LDL;
871 BuildMI(BB, Alpha::LDQ, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
872 }
873}
874
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000875void AlphaISel::MoveInt2FP(unsigned src, unsigned dst, bool isDouble)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000876{
877 unsigned Opc;
878 if (EnableAlphaFTOI) {
879 Opc = isDouble?Alpha::ITOFT:Alpha::ITOFS;
880 BuildMI(BB, Opc, 1, dst).addReg(src);
881 } else {
882 //The hard way:
883 // Spill the integer to memory and reload it from there.
884 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
885 MachineFunction *F = BB->getParent();
886 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
887
888 Opc = isDouble ? Alpha::STQ : Alpha::STL;
889 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
890 Opc = isDouble ? Alpha::LDT : Alpha::LDS;
891 BuildMI(BB, Opc, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
892 }
893}
894
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000895bool AlphaISel::SelectFPSetCC(SDOperand N, unsigned dst)
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000896{
897 SDNode *Node = N.Val;
898 unsigned Opc, Tmp1, Tmp2, Tmp3;
899 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node);
900
901 //assert(SetCC->getOperand(0).getValueType() != MVT::f32 && "SetCC f32 should have been promoted");
902 bool rev = false;
903 bool inv = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000904
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000905 switch (SetCC->getCondition()) {
906 default: Node->dump(); assert(0 && "Unknown FP comparison!");
907 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
908 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
909 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
910 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
911 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
912 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
913 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000914
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000915 ConstantFPSDNode *CN;
916 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
917 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
918 Tmp1 = Alpha::F31;
919 else
920 Tmp1 = SelectExpr(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000921
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000922 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
923 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
924 Tmp2 = Alpha::F31;
925 else
Chris Lattner9c9183a2005-04-30 04:44:07 +0000926 Tmp2 = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000927
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000928 //Can only compare doubles, and dag won't promote for me
929 if (SetCC->getOperand(0).getValueType() == MVT::f32)
930 {
931 //assert(0 && "Setcc On float?\n");
932 std::cerr << "Setcc on float!\n";
933 Tmp3 = MakeReg(MVT::f64);
934 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
935 Tmp1 = Tmp3;
936 }
937 if (SetCC->getOperand(1).getValueType() == MVT::f32)
938 {
939 //assert (0 && "Setcc On float?\n");
940 std::cerr << "Setcc on float!\n";
941 Tmp3 = MakeReg(MVT::f64);
942 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp2);
943 Tmp2 = Tmp3;
944 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000945
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000946 if (rev) std::swap(Tmp1, Tmp2);
947 //do the comparison
948 BuildMI(BB, Opc, 2, dst).addReg(Tmp1).addReg(Tmp2);
949 return inv;
950}
951
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000952//Check to see if the load is a constant offset from a base register
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000953void AlphaISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000954{
955 unsigned opcode = N.getOpcode();
Andrew Lenharth694c2982005-06-26 23:01:11 +0000956 if (opcode == ISD::ADD && N.getOperand(1).getOpcode() == ISD::Constant &&
957 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
958 { //Normal imm add
959 Reg = SelectExpr(N.getOperand(0));
960 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
961 return;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000962 }
963 Reg = SelectExpr(N);
964 offset = 0;
965 return;
966}
967
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000968void AlphaISel::SelectBranchCC(SDOperand N)
Andrew Lenharth445171a2005-02-08 00:40:03 +0000969{
970 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000971 MachineBasicBlock *Dest =
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000972 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
973 unsigned Opc = Alpha::WTF;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000974
Andrew Lenharth445171a2005-02-08 00:40:03 +0000975 Select(N.getOperand(0)); //chain
976 SDOperand CC = N.getOperand(1);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000977
Andrew Lenharth445171a2005-02-08 00:40:03 +0000978 if (CC.getOpcode() == ISD::SETCC)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000979 {
980 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
981 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
982 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharth09552bf2005-06-08 18:02:21 +0000983 bool RightZero = SetCC->getOperand(1).getOpcode() == ISD::Constant &&
984 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000985 bool isNE = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000986
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000987 //Fix up CC
988 ISD::CondCode cCode= SetCC->getCondition();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000989
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000990 if(cCode == ISD::SETNE)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000991 isNE = true;
Andrew Lenharth445171a2005-02-08 00:40:03 +0000992
Andrew Lenharth694c2982005-06-26 23:01:11 +0000993 if (RightZero) {
Andrew Lenharth09552bf2005-06-08 18:02:21 +0000994 switch (cCode) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000995 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
996 case ISD::SETEQ: Opc = Alpha::BEQ; break;
997 case ISD::SETLT: Opc = Alpha::BLT; break;
998 case ISD::SETLE: Opc = Alpha::BLE; break;
999 case ISD::SETGT: Opc = Alpha::BGT; break;
1000 case ISD::SETGE: Opc = Alpha::BGE; break;
1001 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
1002 case ISD::SETUGT: Opc = Alpha::BNE; break;
1003 case ISD::SETULE: Opc = Alpha::BEQ; break; //Technically you could have this CC
1004 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
1005 case ISD::SETNE: Opc = Alpha::BNE; break;
1006 }
Andrew Lenharth694c2982005-06-26 23:01:11 +00001007 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001008 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
1009 return;
1010 } else {
1011 unsigned Tmp1 = SelectExpr(CC);
1012 if (isNE)
1013 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
1014 else
1015 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001016 return;
1017 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001018 } else { //FP
1019 //Any comparison between 2 values should be codegened as an folded branch, as moving
1020 //CC to the integer register is very expensive
1021 //for a cmp b: c = a - b;
1022 //a = b: c = 0
1023 //a < b: c < 0
1024 //a > b: c > 0
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001025
1026 bool invTest = false;
1027 unsigned Tmp3;
1028
1029 ConstantFPSDNode *CN;
1030 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
1031 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1032 Tmp3 = SelectExpr(SetCC->getOperand(0));
1033 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
1034 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1035 {
1036 Tmp3 = SelectExpr(SetCC->getOperand(1));
1037 invTest = true;
1038 }
1039 else
1040 {
1041 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
1042 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
1043 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
1044 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1045 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1046 .addReg(Tmp1).addReg(Tmp2);
1047 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001048
1049 switch (SetCC->getCondition()) {
1050 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001051 case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
1052 case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
1053 case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
1054 case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
1055 case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
1056 case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001057 }
1058 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001059 return;
1060 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001061 abort(); //Should never be reached
1062 } else {
1063 //Giveup and do the stupid thing
1064 unsigned Tmp1 = SelectExpr(CC);
1065 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
1066 return;
1067 }
Andrew Lenharth445171a2005-02-08 00:40:03 +00001068 abort(); //Should never be reached
1069}
1070
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001071unsigned AlphaISel::SelectExprFP(SDOperand N, unsigned Result)
Andrew Lenharth40831c52005-01-28 06:57:18 +00001072{
1073 unsigned Tmp1, Tmp2, Tmp3;
1074 unsigned Opc = 0;
1075 SDNode *Node = N.Val;
1076 MVT::ValueType DestType = N.getValueType();
1077 unsigned opcode = N.getOpcode();
1078
1079 switch (opcode) {
1080 default:
1081 Node->dump();
1082 assert(0 && "Node not handled!\n");
Andrew Lenharth2c594352005-01-29 15:42:07 +00001083
Andrew Lenharth7332f3e2005-04-02 19:11:07 +00001084 case ISD::UNDEF: {
1085 BuildMI(BB, Alpha::IDEF, 0, Result);
1086 return Result;
1087 }
1088
Andrew Lenharth30b46d42005-04-02 19:04:58 +00001089 case ISD::FNEG:
1090 if(ISD::FABS == N.getOperand(0).getOpcode())
1091 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001092 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1093 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +00001094 } else {
Misha Brukman7847fca2005-04-22 17:54:37 +00001095 Tmp1 = SelectExpr(N.getOperand(0));
1096 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +00001097 }
1098 return Result;
1099
1100 case ISD::FABS:
1101 Tmp1 = SelectExpr(N.getOperand(0));
1102 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
1103 return Result;
1104
Andrew Lenharth9818c052005-02-05 13:19:12 +00001105 case ISD::SELECT:
1106 {
Andrew Lenharth45859692005-03-03 21:47:53 +00001107 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1108 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
1109 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
1110
1111 SDOperand CC = N.getOperand(0);
1112 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
1113
Misha Brukman4633f1c2005-04-21 23:13:11 +00001114 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth45859692005-03-03 21:47:53 +00001115 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
1116 { //FP Setcc -> Select yay!
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001117
1118
Andrew Lenharth45859692005-03-03 21:47:53 +00001119 //for a cmp b: c = a - b;
1120 //a = b: c = 0
1121 //a < b: c < 0
1122 //a > b: c > 0
Misha Brukman4633f1c2005-04-21 23:13:11 +00001123
Andrew Lenharth45859692005-03-03 21:47:53 +00001124 bool invTest = false;
1125 unsigned Tmp3;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001126
Andrew Lenharth45859692005-03-03 21:47:53 +00001127 ConstantFPSDNode *CN;
1128 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
1129 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1130 Tmp3 = SelectExpr(SetCC->getOperand(0));
1131 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
1132 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1133 {
1134 Tmp3 = SelectExpr(SetCC->getOperand(1));
1135 invTest = true;
1136 }
1137 else
1138 {
1139 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
1140 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
1141 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
1142 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1143 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1144 .addReg(Tmp1).addReg(Tmp2);
1145 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001146
Andrew Lenharth45859692005-03-03 21:47:53 +00001147 switch (SetCC->getCondition()) {
1148 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
1149 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNE : Alpha::FCMOVEQ; break;
1150 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGT : Alpha::FCMOVLT; break;
1151 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGE : Alpha::FCMOVLE; break;
1152 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLT : Alpha::FCMOVGT; break;
1153 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLE : Alpha::FCMOVGE; break;
1154 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQ : Alpha::FCMOVNE; break;
1155 }
Andrew Lenharth33819132005-03-04 20:09:23 +00001156 BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
Andrew Lenharth45859692005-03-03 21:47:53 +00001157 return Result;
1158 }
1159 else
1160 {
1161 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001162 BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV).addReg(Tmp1);
1163// // Spill the cond to memory and reload it from there.
1164// unsigned Tmp4 = MakeReg(MVT::f64);
1165// MoveIntFP(Tmp1, Tmp4, true);
1166// //now ideally, we don't have to do anything to the flag...
1167// // Get the condition into the zero flag.
1168// BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
Andrew Lenharth45859692005-03-03 21:47:53 +00001169 return Result;
1170 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001171 }
1172
Andrew Lenharthc1faced2005-02-01 01:37:24 +00001173 case ISD::FP_ROUND:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001174 assert (DestType == MVT::f32 &&
1175 N.getOperand(0).getValueType() == MVT::f64 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001176 "only f64 to f32 conversion supported here");
Andrew Lenharthc1faced2005-02-01 01:37:24 +00001177 Tmp1 = SelectExpr(N.getOperand(0));
1178 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
1179 return Result;
1180
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001181 case ISD::FP_EXTEND:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001182 assert (DestType == MVT::f64 &&
1183 N.getOperand(0).getValueType() == MVT::f32 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001184 "only f32 to f64 conversion supported here");
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001185 Tmp1 = SelectExpr(N.getOperand(0));
1186 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
1187 return Result;
1188
Andrew Lenharth2c594352005-01-29 15:42:07 +00001189 case ISD::CopyFromReg:
1190 {
1191 // Make sure we generate both values.
1192 if (Result != notIn)
1193 ExprMap[N.getValue(1)] = notIn; // Generate the token
1194 else
1195 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001196
Andrew Lenharth2c594352005-01-29 15:42:07 +00001197 SDOperand Chain = N.getOperand(0);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001198
Andrew Lenharth2c594352005-01-29 15:42:07 +00001199 Select(Chain);
1200 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1201 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1202 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
1203 return Result;
1204 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001205
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001206 case ISD::LOAD:
1207 {
1208 // Make sure we generate both values.
1209 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001210 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001211 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001212 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001213
Andrew Lenharth29219162005-02-07 06:31:44 +00001214 DestType = N.getValue(0).getValueType();
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001215
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001216 SDOperand Chain = N.getOperand(0);
1217 SDOperand Address = N.getOperand(1);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001218 Select(Chain);
Andrew Lenharth65838902005-02-06 16:22:15 +00001219 Opc = DestType == MVT::f64 ? Alpha::LDT : Alpha::LDS;
1220
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +00001221 if (EnableAlphaLSMark)
1222 {
1223 int i = getValueOffset(dyn_cast<SrcValueSDNode>(N.getOperand(2))->getValue());
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001224 int j = getFunctionOffset(BB->getParent()->getFunction());
1225 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +00001226 }
1227
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001228 if (Address.getOpcode() == ISD::GlobalAddress) {
1229 AlphaLowering.restoreGP(BB);
1230 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001231 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001232 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1233 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001234 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001235 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001236 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001237 has_sym = true;
Andrew Lenharth97127a12005-02-05 17:41:39 +00001238 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001239 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001240 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001241 BuildMI(BB, Opc, 2, Result)
1242 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1243 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001244 } else {
1245 long offset;
1246 SelectAddr(Address, Tmp1, offset);
1247 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1248 }
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001249 return Result;
1250 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001251 case ISD::ConstantFP:
1252 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
1253 if (CN->isExactlyValue(+0.0)) {
1254 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001255 } else if ( CN->isExactlyValue(-0.0)) {
1256 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001257 } else {
1258 abort();
1259 }
1260 }
1261 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001262
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001263 case ISD::SDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001264 case ISD::MUL:
1265 case ISD::ADD:
1266 case ISD::SUB:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001267 switch( opcode ) {
1268 case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS; break;
1269 case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS; break;
1270 case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS; break;
1271 case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS; break;
1272 };
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001273
1274 ConstantFPSDNode *CN;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001275 if (opcode == ISD::SUB
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001276 && (CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
1277 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1278 {
1279 Tmp2 = SelectExpr(N.getOperand(1));
1280 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp2).addReg(Tmp2);
1281 } else {
1282 Tmp1 = SelectExpr(N.getOperand(0));
1283 Tmp2 = SelectExpr(N.getOperand(1));
1284 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1285 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001286 return Result;
1287
Andrew Lenharth2c594352005-01-29 15:42:07 +00001288 case ISD::EXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001289 {
1290 //include a conversion sequence for float loads to double
1291 if (Result != notIn)
1292 ExprMap[N.getValue(1)] = notIn; // Generate the token
1293 else
1294 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001295
Andrew Lenhartha549deb2005-02-07 05:33:15 +00001296 Tmp1 = MakeReg(MVT::f32);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001297
1298 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001299 "EXTLOAD not from f32");
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001300 assert(Node->getValueType(0) == MVT::f64 && "EXTLOAD not to f64");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001301
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001302 SDOperand Chain = N.getOperand(0);
1303 SDOperand Address = N.getOperand(1);
1304 Select(Chain);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001305
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001306 if (Address.getOpcode() == ISD::GlobalAddress) {
1307 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001308 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001309 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1310 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001311 else if (ConstantPoolSDNode *CP =
1312 dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001313 {
1314 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001315 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001316 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addConstantPoolIndex(CP->getIndex());
1317 }
1318 else if(Address.getOpcode() == ISD::FrameIndex) {
1319 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
Andrew Lenharth032f2352005-02-22 21:59:48 +00001320 BuildMI(BB, Alpha::LDS, 2, Tmp1)
1321 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1322 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001323 } else {
1324 long offset;
1325 SelectAddr(Address, Tmp2, offset);
1326 BuildMI(BB, Alpha::LDS, 1, Tmp1).addImm(offset).addReg(Tmp2);
1327 }
Andrew Lenharth29219162005-02-07 06:31:44 +00001328 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001329 return Result;
1330 }
Andrew Lenharth2c594352005-01-29 15:42:07 +00001331
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001332 case ISD::SINT_TO_FP:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001333 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001334 assert (N.getOperand(0).getValueType() == MVT::i64
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001335 && "only quads can be loaded from");
Andrew Lenharth40831c52005-01-28 06:57:18 +00001336 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001337 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001338 MoveInt2FP(Tmp1, Tmp2, true);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001339 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
1340 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001341 return Result;
1342 }
1343 }
1344 assert(0 && "should not get here");
1345 return 0;
1346}
1347
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001348unsigned AlphaISel::SelectExpr(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001349 unsigned Result;
Andrew Lenharth2966e842005-04-07 18:15:28 +00001350 unsigned Tmp1, Tmp2 = 0, Tmp3;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001351 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001352 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001353
1354 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001355 MVT::ValueType DestType = N.getValueType();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001356
1357 unsigned &Reg = ExprMap[N];
1358 if (Reg) return Reg;
1359
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001360 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::TAILCALL)
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001361 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001362 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001363 else {
1364 // If this is a call instruction, make sure to prepare ALL of the result
1365 // values as well as the chain.
1366 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001367 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001368 else {
1369 Result = MakeReg(Node->getValueType(0));
1370 ExprMap[N.getValue(0)] = Result;
1371 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1372 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001373 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001374 }
1375 }
1376
Andrew Lenharth50d91d72005-04-30 14:19:13 +00001377 if ((DestType == MVT::f64 || DestType == MVT::f32 ||
1378 (
1379 (opcode == ISD::LOAD || opcode == ISD::CopyFromReg ||
1380 opcode == ISD::EXTLOAD) &&
1381 (N.getValue(0).getValueType() == MVT::f32 ||
1382 N.getValue(0).getValueType() == MVT::f64)
1383 ))
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001384 && opcode != ISD::CALL && opcode != ISD::TAILCALL
Andrew Lenharth06342c32005-02-07 06:21:37 +00001385 )
Andrew Lenharth40831c52005-01-28 06:57:18 +00001386 return SelectExprFP(N, Result);
1387
1388 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001389 default:
1390 Node->dump();
1391 assert(0 && "Node not handled!\n");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001392
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001393 case ISD::CTPOP:
1394 case ISD::CTTZ:
1395 case ISD::CTLZ:
1396 Opc = opcode == ISD::CTPOP ? Alpha::CTPOP :
1397 (opcode == ISD::CTTZ ? Alpha::CTTZ : Alpha::CTLZ);
1398 Tmp1 = SelectExpr(N.getOperand(0));
1399 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1400 return Result;
1401
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001402 case ISD::MULHU:
1403 Tmp1 = SelectExpr(N.getOperand(0));
1404 Tmp2 = SelectExpr(N.getOperand(1));
1405 BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth706be912005-04-07 13:55:53 +00001406 return Result;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001407 case ISD::MULHS:
1408 {
1409 //MULHU - Ra<63>*Rb - Rb<63>*Ra
1410 Tmp1 = SelectExpr(N.getOperand(0));
1411 Tmp2 = SelectExpr(N.getOperand(1));
1412 Tmp3 = MakeReg(MVT::i64);
1413 BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1414 unsigned V1 = MakeReg(MVT::i64);
1415 unsigned V2 = MakeReg(MVT::i64);
1416 BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31).addReg(Tmp1);
1417 BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31).addReg(Tmp2);
1418 unsigned IRes = MakeReg(MVT::i64);
1419 BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
1420 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
1421 return Result;
1422 }
Andrew Lenharth7332f3e2005-04-02 19:11:07 +00001423 case ISD::UNDEF: {
1424 BuildMI(BB, Alpha::IDEF, 0, Result);
1425 return Result;
1426 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001427
Andrew Lenharth032f2352005-02-22 21:59:48 +00001428 case ISD::DYNAMIC_STACKALLOC:
1429 // Generate both result values.
Andrew Lenharth3a7118d2005-02-23 17:33:42 +00001430 if (Result != notIn)
1431 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth032f2352005-02-22 21:59:48 +00001432 else
1433 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1434
1435 // FIXME: We are currently ignoring the requested alignment for handling
1436 // greater than the stack alignment. This will need to be revisited at some
1437 // point. Align = N.getOperand(2);
1438
1439 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1440 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1441 std::cerr << "Cannot allocate stack object with greater alignment than"
1442 << " the stack alignment yet!";
1443 abort();
1444 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001445
Andrew Lenharth032f2352005-02-22 21:59:48 +00001446 Select(N.getOperand(0));
1447 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1448 {
1449 if (CN->getValue() < 32000)
1450 {
1451 BuildMI(BB, Alpha::LDA, 2, Alpha::R30)
1452 .addImm(-CN->getValue()).addReg(Alpha::R30);
1453 } else {
1454 Tmp1 = SelectExpr(N.getOperand(1));
1455 // Subtract size from stack pointer, thereby allocating some space.
1456 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1457 }
1458 } else {
1459 Tmp1 = SelectExpr(N.getOperand(1));
1460 // Subtract size from stack pointer, thereby allocating some space.
1461 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1462 }
1463
1464 // Put a pointer to the space into the result register, by copying the stack
1465 // pointer.
Andrew Lenharth7bc47022005-02-22 23:29:25 +00001466 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
Andrew Lenharth032f2352005-02-22 21:59:48 +00001467 return Result;
1468
Andrew Lenharth33819132005-03-04 20:09:23 +00001469// case ISD::ConstantPool:
1470// Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1471// AlphaLowering.restoreGP(BB);
1472// BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(Tmp1);
1473// return Result;
Andrew Lenharth2c594352005-01-29 15:42:07 +00001474
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001475 case ISD::FrameIndex:
Andrew Lenharth032f2352005-02-22 21:59:48 +00001476 BuildMI(BB, Alpha::LDA, 2, Result)
1477 .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
1478 .addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001479 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001480
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001481 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001482 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001483 case ISD::SEXTLOAD:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001484 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001485 {
1486 // Make sure we generate both values.
1487 if (Result != notIn)
1488 ExprMap[N.getValue(1)] = notIn; // Generate the token
1489 else
1490 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001491
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001492 SDOperand Chain = N.getOperand(0);
1493 SDOperand Address = N.getOperand(1);
1494 Select(Chain);
1495
Misha Brukman4633f1c2005-04-21 23:13:11 +00001496 assert(Node->getValueType(0) == MVT::i64 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001497 "Unknown type to sign extend to.");
Andrew Lenharth03824012005-02-07 05:55:55 +00001498 if (opcode == ISD::LOAD)
1499 Opc = Alpha::LDQ;
1500 else
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001501 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
1502 default: Node->dump(); assert(0 && "Bad sign extend!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001503 case MVT::i32: Opc = Alpha::LDL;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001504 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001505 case MVT::i16: Opc = Alpha::LDWU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001506 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001507 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Misha Brukman4633f1c2005-04-21 23:13:11 +00001508 case MVT::i8: Opc = Alpha::LDBU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001509 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001510 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001511
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001512 if (EnableAlphaLSMark)
1513 {
1514 int i = getValueOffset(dyn_cast<SrcValueSDNode>(N.getOperand(2))->getValue());
1515 int j = getFunctionOffset(BB->getParent()->getFunction());
1516 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
1517 }
1518
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001519 if (Address.getOpcode() == ISD::GlobalAddress) {
1520 AlphaLowering.restoreGP(BB);
1521 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001522 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001523 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1524 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001525 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1526 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001527 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001528 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001529 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001530 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001531 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001532 BuildMI(BB, Opc, 2, Result)
1533 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1534 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001535 } else {
1536 long offset;
1537 SelectAddr(Address, Tmp1, offset);
1538 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1539 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001540 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001541 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001542
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001543 case ISD::GlobalAddress:
1544 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001545 has_sym = true;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001546 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
1547 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
1548 return Result;
1549
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001550 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001551 case ISD::CALL:
1552 {
1553 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001554
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001555 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001556 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001557
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001558 //grab the arguments
1559 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001560 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001561 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001562 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001563
Andrew Lenharth684f2292005-01-30 00:35:27 +00001564 //in reg args
1565 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001566 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001567 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001568 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +00001569 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001570 Alpha::F19, Alpha::F20, Alpha::F21};
1571 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001572 default:
1573 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001574 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001575 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001576 N.getOperand(i+2).getValueType() << "\n";
1577 assert(0 && "Unknown value type for call");
1578 case MVT::i1:
1579 case MVT::i8:
1580 case MVT::i16:
1581 case MVT::i32:
1582 case MVT::i64:
1583 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1584 break;
1585 case MVT::f32:
1586 case MVT::f64:
1587 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1588 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001589 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001590 }
Andrew Lenharth684f2292005-01-30 00:35:27 +00001591 //in mem args
1592 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001593 {
1594 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001595 default:
1596 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001597 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001598 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001599 N.getOperand(i+2).getValueType() << "\n";
1600 assert(0 && "Unknown value type for call");
1601 case MVT::i1:
1602 case MVT::i8:
1603 case MVT::i16:
1604 case MVT::i32:
1605 case MVT::i64:
1606 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1607 break;
1608 case MVT::f32:
1609 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1610 break;
1611 case MVT::f64:
1612 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1613 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001614 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001615 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001616 //build the right kind of call
1617 if (GlobalAddressSDNode *GASD =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001618 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001619 {
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001620 if (GASD->getGlobal()->isExternal()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001621 //use safe calling convention
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001622 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001623 has_sym = true;
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001624 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal());
1625 } else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001626 //use PC relative branch call
Andrew Lenharth1e0d9bd2005-04-14 17:34:20 +00001627 AlphaLowering.restoreGP(BB);
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001628 BuildMI(BB, Alpha::BSR, 1, Alpha::R26).addGlobalAddress(GASD->getGlobal(),true);
1629 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001630 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001631 else if (ExternalSymbolSDNode *ESSDN =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001632 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001633 {
1634 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001635 has_sym = true;
Andrew Lenharthba05ad62005-03-30 18:22:52 +00001636 BuildMI(BB, Alpha::CALL, 1).addExternalSymbol(ESSDN->getSymbol(), true);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001637 } else {
1638 //no need to restore GP as we are doing an indirect call
1639 Tmp1 = SelectExpr(N.getOperand(1));
1640 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
1641 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
1642 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001643
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001644 //push the result into a virtual register
Misha Brukman4633f1c2005-04-21 23:13:11 +00001645
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001646 switch (Node->getValueType(0)) {
1647 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001648 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001649 case MVT::i1:
1650 case MVT::i8:
1651 case MVT::i16:
1652 case MVT::i32:
1653 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001654 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
1655 break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001656 case MVT::f32:
1657 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001658 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
1659 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001660 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001661 return Result+N.ResNo;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001662 }
1663
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001664 case ISD::SIGN_EXTEND_INREG:
1665 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001666 //do SDIV opt for all levels of ints if not dividing by a constant
1667 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
1668 && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001669 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001670 unsigned Tmp4 = MakeReg(MVT::f64);
1671 unsigned Tmp5 = MakeReg(MVT::f64);
1672 unsigned Tmp6 = MakeReg(MVT::f64);
1673 unsigned Tmp7 = MakeReg(MVT::f64);
1674 unsigned Tmp8 = MakeReg(MVT::f64);
1675 unsigned Tmp9 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001676
1677 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1678 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1679 MoveInt2FP(Tmp1, Tmp4, true);
1680 MoveInt2FP(Tmp2, Tmp5, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001681 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Tmp4);
1682 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Tmp5);
1683 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
1684 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Tmp8);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001685 MoveFP2Int(Tmp9, Result, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001686 return Result;
1687 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001688
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001689 //Alpha has instructions for a bunch of signed 32 bit stuff
1690 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001691 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001692 switch (N.getOperand(0).getOpcode()) {
1693 case ISD::ADD:
1694 case ISD::SUB:
1695 case ISD::MUL:
1696 {
1697 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
1698 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
1699 //FIXME: first check for Scaled Adds and Subs!
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001700 ConstantSDNode* CSD = NULL;
1701 if(!isMul && N.getOperand(0).getOperand(0).getOpcode() == ISD::SHL &&
1702 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(0).getOperand(1))) &&
1703 (CSD->getValue() == 2 || CSD->getValue() == 3))
1704 {
1705 bool use4 = CSD->getValue() == 2;
1706 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1707 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1708 BuildMI(BB, isAdd?(use4?Alpha::S4ADDL:Alpha::S8ADDL):(use4?Alpha::S4SUBL:Alpha::S8SUBL),
1709 2,Result).addReg(Tmp1).addReg(Tmp2);
1710 }
1711 else if(isAdd && N.getOperand(0).getOperand(1).getOpcode() == ISD::SHL &&
1712 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1).getOperand(1))) &&
1713 (CSD->getValue() == 2 || CSD->getValue() == 3))
1714 {
1715 bool use4 = CSD->getValue() == 2;
1716 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1717 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1718 BuildMI(BB, use4?Alpha::S4ADDL:Alpha::S8ADDL, 2,Result).addReg(Tmp1).addReg(Tmp2);
1719 }
1720 else if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001721 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
1722 { //Normal imm add/sub
1723 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001724 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001725 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
1726 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001727 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001728 else
1729 { //Normal add/sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001730 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULL : Alpha::SUBL);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001731 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001732 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001733 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1734 }
1735 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001736 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001737 default: break; //Fall Though;
1738 }
1739 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001740 Tmp1 = SelectExpr(N.getOperand(0));
1741 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001742 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001743 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001744 {
1745 default:
1746 Node->dump();
1747 assert(0 && "Sign Extend InReg not there yet");
1748 break;
1749 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001750 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001751 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001752 break;
1753 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001754 case MVT::i16:
1755 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
1756 break;
1757 case MVT::i8:
1758 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
1759 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +00001760 case MVT::i1:
1761 Tmp2 = MakeReg(MVT::i64);
1762 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001763 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +00001764 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001765 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001766 return Result;
1767 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001768
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001769 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001770 {
1771 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1772 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
Andrew Lenharth694c2982005-06-26 23:01:11 +00001773 bool isConst = false;
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001774 int dir;
Misha Brukman7847fca2005-04-22 17:54:37 +00001775
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001776 //Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001777 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001778 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth694c2982005-06-26 23:01:11 +00001779 isConst = true;
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001780
1781 switch (SetCC->getCondition()) {
1782 default: Node->dump(); assert(0 && "Unknown integer comparison!");
Andrew Lenharth694c2982005-06-26 23:01:11 +00001783 case ISD::SETEQ:
1784 Opc = isConst ? Alpha::CMPEQi : Alpha::CMPEQ; dir=1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001785 case ISD::SETLT:
Andrew Lenharth694c2982005-06-26 23:01:11 +00001786 Opc = isConst ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001787 case ISD::SETLE:
Andrew Lenharth694c2982005-06-26 23:01:11 +00001788 Opc = isConst ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
1789 case ISD::SETGT: Opc = Alpha::CMPLT; dir = 2; break;
1790 case ISD::SETGE: Opc = Alpha::CMPLE; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001791 case ISD::SETULT:
Andrew Lenharth694c2982005-06-26 23:01:11 +00001792 Opc = isConst ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
1793 case ISD::SETUGT: Opc = Alpha::CMPULT; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001794 case ISD::SETULE:
Andrew Lenharth694c2982005-06-26 23:01:11 +00001795 Opc = isConst ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
1796 case ISD::SETUGE: Opc = Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001797 case ISD::SETNE: {//Handle this one special
1798 //std::cerr << "Alpha does not have a setne.\n";
1799 //abort();
1800 Tmp1 = SelectExpr(N.getOperand(0));
1801 Tmp2 = SelectExpr(N.getOperand(1));
1802 Tmp3 = MakeReg(MVT::i64);
1803 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001804 //Remeber we have the Inv for this CC
1805 CCInvMap[N] = Tmp3;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001806 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001807 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001808 return Result;
1809 }
1810 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001811 if (dir == 1) {
1812 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth694c2982005-06-26 23:01:11 +00001813 if (isConst) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001814 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1815 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1816 } else {
1817 Tmp2 = SelectExpr(N.getOperand(1));
1818 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1819 }
Andrew Lenharth694c2982005-06-26 23:01:11 +00001820 } else { //if (dir == 2) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001821 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth694c2982005-06-26 23:01:11 +00001822 Tmp2 = SelectExpr(N.getOperand(0));
1823 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001824 }
1825 } else {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001826 //do the comparison
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001827 Tmp1 = MakeReg(MVT::f64);
1828 bool inv = SelectFPSetCC(N, Tmp1);
1829
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001830 //now arrange for Result (int) to have a 1 or 0
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001831 Tmp2 = MakeReg(MVT::i64);
1832 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001833 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001834 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001835 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001836 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001837 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001838 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001839
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001840 case ISD::CopyFromReg:
1841 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001842 ++count_ins;
1843
Andrew Lenharth40831c52005-01-28 06:57:18 +00001844 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001845 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001846 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001847 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001848 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001849
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001850 SDOperand Chain = N.getOperand(0);
1851
1852 Select(Chain);
1853 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1854 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1855 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1856 return Result;
1857 }
1858
Misha Brukman4633f1c2005-04-21 23:13:11 +00001859 //Most of the plain arithmetic and logic share the same form, and the same
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001860 //constant immediate test
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001861 case ISD::XOR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001862 //Match Not
1863 if (N.getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001864 cast<ConstantSDNode>(N.getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001865 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001866 Tmp1 = SelectExpr(N.getOperand(0));
1867 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1868 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001869 }
1870 //Fall through
1871 case ISD::AND:
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001872 //handle zap
1873 if (opcode == ISD::AND && N.getOperand(1).getOpcode() == ISD::Constant)
1874 {
1875 uint64_t k = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1876 unsigned int build = 0;
1877 for(int i = 0; i < 8; ++i)
1878 {
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001879 if ((k & 0x00FF) == 0x00FF)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001880 build |= 1 << i;
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001881 else if ((k & 0x00FF) != 0)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001882 { build = 0; break; }
1883 k >>= 8;
1884 }
1885 if (build)
1886 {
1887 Tmp1 = SelectExpr(N.getOperand(0));
1888 BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1889 return Result;
1890 }
1891 }
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001892 case ISD::OR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001893 //Check operand(0) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001894 if (N.getOperand(0).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001895 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001896 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001897 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001898 switch(opcode) {
1899 case ISD::AND: Opc = Alpha::BIC; break;
1900 case ISD::OR: Opc = Alpha::ORNOT; break;
1901 case ISD::XOR: Opc = Alpha::EQV; break;
1902 }
1903 Tmp1 = SelectExpr(N.getOperand(1));
1904 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1905 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1906 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001907 }
1908 //Check operand(1) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001909 if (N.getOperand(1).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001910 N.getOperand(1).getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001911 cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001912 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001913 switch(opcode) {
1914 case ISD::AND: Opc = Alpha::BIC; break;
1915 case ISD::OR: Opc = Alpha::ORNOT; break;
1916 case ISD::XOR: Opc = Alpha::EQV; break;
1917 }
1918 Tmp1 = SelectExpr(N.getOperand(0));
1919 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1920 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1921 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001922 }
1923 //Fall through
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001924 case ISD::SHL:
1925 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001926 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001927 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001928 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1929 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001930 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001931 {
1932 switch(opcode) {
1933 case ISD::AND: Opc = Alpha::ANDi; break;
1934 case ISD::OR: Opc = Alpha::BISi; break;
1935 case ISD::XOR: Opc = Alpha::XORi; break;
1936 case ISD::SHL: Opc = Alpha::SLi; break;
1937 case ISD::SRL: Opc = Alpha::SRLi; break;
1938 case ISD::SRA: Opc = Alpha::SRAi; break;
1939 case ISD::MUL: Opc = Alpha::MULQi; break;
1940 };
1941 Tmp1 = SelectExpr(N.getOperand(0));
1942 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1943 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1944 } else {
1945 switch(opcode) {
1946 case ISD::AND: Opc = Alpha::AND; break;
1947 case ISD::OR: Opc = Alpha::BIS; break;
1948 case ISD::XOR: Opc = Alpha::XOR; break;
1949 case ISD::SHL: Opc = Alpha::SL; break;
1950 case ISD::SRL: Opc = Alpha::SRL; break;
1951 case ISD::SRA: Opc = Alpha::SRA; break;
1952 case ISD::MUL: Opc = Alpha::MULQ; break;
1953 };
1954 Tmp1 = SelectExpr(N.getOperand(0));
1955 Tmp2 = SelectExpr(N.getOperand(1));
1956 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1957 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001958 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001959
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001960 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001961 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001962 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001963 bool isAdd = opcode == ISD::ADD;
1964
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001965 //first check for Scaled Adds and Subs!
1966 //Valid for add and sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001967 ConstantSDNode* CSD = NULL;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001968 if(N.getOperand(0).getOpcode() == ISD::SHL &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001969 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
1970 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001971 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001972 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001973 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001974 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) && CSD->getValue() <= 255)
1975 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1976 2, Result).addReg(Tmp2).addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001977 else {
1978 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001979 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1980 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001981 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001982 }
1983 //Position prevents subs
Andrew Lenharth273a1f92005-04-07 14:18:13 +00001984 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001985 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) &&
1986 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001987 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001988 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001989 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001990 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(0))) && CSD->getValue() <= 255)
1991 BuildMI(BB, use4?Alpha::S4ADDQi:Alpha::S8ADDQi, 2, Result).addReg(Tmp2)
1992 .addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001993 else {
1994 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001995 BuildMI(BB, use4?Alpha::S4ADDQ:Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001996 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001997 }
1998 //small addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001999 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
2000 CSD->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002001 { //Normal imm add/sub
2002 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
2003 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002004 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CSD->getValue());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002005 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002006 //larger addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002007 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
2008 CSD->getSignExtended() <= 32767 &&
2009 CSD->getSignExtended() >= -32767)
Andrew Lenharth74d00d82005-03-02 17:23:03 +00002010 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002011 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002012 Tmp2 = (long)CSD->getSignExtended();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002013 if (!isAdd)
2014 Tmp2 = -Tmp2;
2015 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002016 }
2017 //give up and do the operation
2018 else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002019 //Normal add/sub
2020 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
2021 Tmp1 = SelectExpr(N.getOperand(0));
2022 Tmp2 = SelectExpr(N.getOperand(1));
2023 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2024 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002025 return Result;
2026 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002027
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00002028 case ISD::SDIV:
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002029 {
Andrew Lenhartha565c272005-04-06 22:03:13 +00002030 ConstantSDNode* CSD;
2031 //check if we can convert into a shift!
2032 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
2033 (int64_t)CSD->getSignExtended() != 0 &&
2034 ExactLog2(abs((int64_t)CSD->getSignExtended())) != 0)
2035 {
2036 unsigned k = ExactLog2(abs(CSD->getSignExtended()));
2037 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenhartha565c272005-04-06 22:03:13 +00002038 if (k == 1)
2039 Tmp2 = Tmp1;
2040 else
2041 {
2042 Tmp2 = MakeReg(MVT::i64);
2043 BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
2044 }
2045 Tmp3 = MakeReg(MVT::i64);
2046 BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
2047 unsigned Tmp4 = MakeReg(MVT::i64);
2048 BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
2049 if ((int64_t)CSD->getSignExtended() > 0)
2050 BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
2051 else
2052 {
2053 unsigned Tmp5 = MakeReg(MVT::i64);
2054 BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
2055 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
2056 }
2057 return Result;
2058 }
2059 }
2060 //Else fall through
2061
2062 case ISD::UDIV:
2063 {
2064 ConstantSDNode* CSD;
2065 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
2066 ((int64_t)CSD->getSignExtended() >= 2 ||
2067 (int64_t)CSD->getSignExtended() <= -2))
2068 {
2069 // If this is a divide by constant, we can emit code using some magic
2070 // constants to implement it as a multiply instead.
2071 ExprMap.erase(N);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002072 if (opcode == ISD::SDIV)
Andrew Lenhartha565c272005-04-06 22:03:13 +00002073 return SelectExpr(BuildSDIVSequence(N));
2074 else
2075 return SelectExpr(BuildUDIVSequence(N));
2076 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002077 }
2078 //else fall though
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002079 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +00002080 case ISD::SREM:
Misha Brukman4633f1c2005-04-21 23:13:11 +00002081 //FIXME: alpha really doesn't support any of these operations,
Andrew Lenharth40831c52005-01-28 06:57:18 +00002082 // the ops are expanded into special library calls with
2083 // special calling conventions
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002084 //Restore GP because it is a call after all...
Andrew Lenharth40831c52005-01-28 06:57:18 +00002085 switch(opcode) {
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00002086 case ISD::UREM: Opc = Alpha::REMQU; break;
2087 case ISD::SREM: Opc = Alpha::REMQ; break;
2088 case ISD::UDIV: Opc = Alpha::DIVQU; break;
2089 case ISD::SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002090 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002091 Tmp1 = SelectExpr(N.getOperand(0));
2092 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth33819132005-03-04 20:09:23 +00002093 //set up regs explicitly (helps Reg alloc)
2094 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002095 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00002096 AlphaLowering.restoreGP(BB);
Andrew Lenharth33819132005-03-04 20:09:23 +00002097 BuildMI(BB, Opc, 2).addReg(Alpha::R24).addReg(Alpha::R25);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002098 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002099 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002100
Andrew Lenharthe76797c2005-02-01 20:40:27 +00002101 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002102 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002103 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002104 assert (DestType == MVT::i64 && "only quads can be loaded to");
2105 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00002106 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002107 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002108 if (SrcType == MVT::f32)
Misha Brukman7847fca2005-04-22 17:54:37 +00002109 {
2110 Tmp2 = MakeReg(MVT::f64);
2111 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
2112 Tmp1 = Tmp2;
2113 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002114 Tmp2 = MakeReg(MVT::f64);
2115 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00002116 MoveFP2Int(Tmp2, Result, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002117
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002118 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002119 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002120
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002121 case ISD::SELECT:
2122 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00002123 //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 +00002124 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002125 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2126 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002127 // Get the condition into the zero flag.
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002128 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002129
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002130 SDOperand CC = N.getOperand(0);
2131 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
2132
Misha Brukman4633f1c2005-04-21 23:13:11 +00002133 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002134 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
2135 { //FP Setcc -> Int Select
Misha Brukman7847fca2005-04-22 17:54:37 +00002136 Tmp1 = MakeReg(MVT::f64);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002137 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2138 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Misha Brukman7847fca2005-04-22 17:54:37 +00002139 bool inv = SelectFPSetCC(CC, Tmp1);
2140 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
2141 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
2142 return Result;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002143 }
2144 if (CC.getOpcode() == ISD::SETCC) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002145 //Int SetCC -> Select
2146 //Dropping the CC is only useful if we are comparing to 0
2147 if((SetCC->getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth694c2982005-06-26 23:01:11 +00002148 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0))
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002149 {
2150 //figure out a few things
Andrew Lenharth694c2982005-06-26 23:01:11 +00002151 bool useImm = N.getOperand(2).getOpcode() == ISD::Constant &&
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002152 cast<ConstantSDNode>(N.getOperand(2))->getValue() <= 255;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002153
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002154 //Fix up CC
2155 ISD::CondCode cCode= SetCC->getCondition();
Andrew Lenharth694c2982005-06-26 23:01:11 +00002156 if (useImm) //Invert sense to get Imm field right
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002157 cCode = ISD::getSetCCInverse(cCode, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002158
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002159 //Choose the CMOV
2160 switch (cCode) {
2161 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
2162 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
2163 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
2164 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
2165 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
2166 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
2167 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
2168 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2169 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break; //Technically you could have this CC
2170 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
2171 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2172 }
Andrew Lenharth694c2982005-06-26 23:01:11 +00002173 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002174
Andrew Lenharth694c2982005-06-26 23:01:11 +00002175 if (useImm) {
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002176 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
2177 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
Misha Brukman7847fca2005-04-22 17:54:37 +00002178 .addImm(cast<ConstantSDNode>(N.getOperand(2))->getValue())
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002179 .addReg(Tmp1);
2180 } else {
2181 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2182 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2183 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
2184 }
2185 return Result;
2186 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002187 //Otherwise, fall though
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002188 }
2189 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002190 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2191 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthe76797c2005-02-01 20:40:27 +00002192 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002193
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002194 return Result;
2195 }
2196
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002197 case ISD::Constant:
2198 {
Andrew Lenharthc0513832005-03-29 19:24:04 +00002199 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002200 if (val <= IMM_HIGH && val >= IMM_LOW) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002201 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002202 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002203 else if (val <= (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT &&
2204 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
2205 Tmp1 = MakeReg(MVT::i64);
2206 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val)).addReg(Alpha::R31);
2207 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002208 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002209 else {
2210 MachineConstantPool *CP = BB->getParent()->getConstantPool();
2211 ConstantUInt *C = ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
2212 unsigned CPI = CP->getConstantPoolIndex(C);
2213 AlphaLowering.restoreGP(BB);
2214 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CPI);
2215 }
2216 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002217 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002218 }
2219
2220 return 0;
2221}
2222
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002223void AlphaISel::Select(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002224 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00002225 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002226
Nate Begeman85fdeb22005-03-24 04:39:54 +00002227 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002228 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002229
2230 SDNode *Node = N.Val;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002231
Andrew Lenharth760270d2005-02-07 23:02:23 +00002232 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002233
2234 default:
2235 Node->dump(); std::cerr << "\n";
2236 assert(0 && "Node not handled yet!");
2237
2238 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00002239 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002240 return;
2241 }
2242
2243 case ISD::BR: {
2244 MachineBasicBlock *Dest =
2245 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2246
2247 Select(N.getOperand(0));
2248 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
2249 return;
2250 }
2251
2252 case ISD::ImplicitDef:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002253 ++count_ins;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002254 Select(N.getOperand(0));
2255 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
2256 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002257
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002258 case ISD::EntryToken: return; // Noop
2259
2260 case ISD::TokenFactor:
2261 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2262 Select(Node->getOperand(i));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002263
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002264 //N.Val->dump(); std::cerr << "\n";
2265 //assert(0 && "Node not handled yet!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00002266
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002267 return;
2268
2269 case ISD::CopyToReg:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002270 ++count_outs;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002271 Select(N.getOperand(0));
2272 Tmp1 = SelectExpr(N.getOperand(1));
2273 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002274
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002275 if (Tmp1 != Tmp2) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002276 if (N.getOperand(1).getValueType() == MVT::f64 ||
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002277 N.getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00002278 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2279 else
2280 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002281 }
2282 return;
2283
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002284 case ISD::RET:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002285 ++count_outs;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002286 switch (N.getNumOperands()) {
2287 default:
2288 std::cerr << N.getNumOperands() << "\n";
2289 for (unsigned i = 0; i < N.getNumOperands(); ++i)
2290 std::cerr << N.getOperand(i).getValueType() << "\n";
2291 Node->dump();
2292 assert(0 && "Unknown return instruction!");
2293 case 2:
2294 Select(N.getOperand(0));
2295 Tmp1 = SelectExpr(N.getOperand(1));
2296 switch (N.getOperand(1).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002297 default: Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002298 assert(0 && "All other types should have been promoted!!");
2299 case MVT::f64:
2300 case MVT::f32:
2301 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
2302 break;
2303 case MVT::i32:
2304 case MVT::i64:
2305 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
2306 break;
2307 }
2308 break;
2309 case 1:
2310 Select(N.getOperand(0));
2311 break;
2312 }
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +00002313 AlphaLowering.restoreRA(BB);
2314 BuildMI(BB, Alpha::RET, 1, Alpha::R31).addReg(Alpha::R26); // Just emit a 'ret' instruction
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002315 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002316
Misha Brukman4633f1c2005-04-21 23:13:11 +00002317 case ISD::TRUNCSTORE:
2318 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002319 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002320 SDOperand Chain = N.getOperand(0);
2321 SDOperand Value = N.getOperand(1);
2322 SDOperand Address = N.getOperand(2);
2323 Select(Chain);
2324
2325 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00002326
2327 if (opcode == ISD::STORE) {
2328 switch(Value.getValueType()) {
2329 default: assert(0 && "unknown Type in store");
2330 case MVT::i64: Opc = Alpha::STQ; break;
2331 case MVT::f64: Opc = Alpha::STT; break;
2332 case MVT::f32: Opc = Alpha::STS; break;
2333 }
2334 } else { //ISD::TRUNCSTORE
2335 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2336 default: assert(0 && "unknown Type in store");
2337 case MVT::i1: //FIXME: DAG does not promote this load
2338 case MVT::i8: Opc = Alpha::STB; break;
2339 case MVT::i16: Opc = Alpha::STW; break;
2340 case MVT::i32: Opc = Alpha::STL; break;
2341 }
Andrew Lenharth65838902005-02-06 16:22:15 +00002342 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00002343
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002344 if (EnableAlphaLSMark)
2345 {
2346 int i = getValueOffset(dyn_cast<SrcValueSDNode>(N.getOperand(3))->getValue());
2347 int j = getFunctionOffset(BB->getParent()->getFunction());
2348 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
2349 }
2350
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002351 if (Address.getOpcode() == ISD::GlobalAddress)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002352 {
2353 AlphaLowering.restoreGP(BB);
2354 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002355 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002356 BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
2357 }
Andrew Lenharth05380342005-02-07 05:07:00 +00002358 else if(Address.getOpcode() == ISD::FrameIndex)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002359 {
Andrew Lenharth032f2352005-02-22 21:59:48 +00002360 BuildMI(BB, Opc, 3).addReg(Tmp1)
2361 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
2362 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002363 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002364 else
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002365 {
2366 long offset;
2367 SelectAddr(Address, Tmp2, offset);
2368 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2369 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002370 return;
2371 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002372
2373 case ISD::EXTLOAD:
2374 case ISD::SEXTLOAD:
2375 case ISD::ZEXTLOAD:
2376 case ISD::LOAD:
2377 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002378 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002379 case ISD::CALL:
Andrew Lenharth032f2352005-02-22 21:59:48 +00002380 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002381 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002382 SelectExpr(N);
2383 return;
2384
Chris Lattner16cd04d2005-05-12 23:24:06 +00002385 case ISD::CALLSEQ_START:
2386 case ISD::CALLSEQ_END:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002387 Select(N.getOperand(0));
2388 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002389
Chris Lattner16cd04d2005-05-12 23:24:06 +00002390 Opc = N.getOpcode() == ISD::CALLSEQ_START ? Alpha::ADJUSTSTACKDOWN :
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002391 Alpha::ADJUSTSTACKUP;
2392 BuildMI(BB, Opc, 1).addImm(Tmp1);
2393 return;
Andrew Lenharth95762122005-03-31 21:24:06 +00002394
2395 case ISD::PCMARKER:
2396 Select(N.getOperand(0)); //Chain
2397 BuildMI(BB, Alpha::PCLABEL, 2).addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
2398 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002399 }
2400 assert(0 && "Should not be reached!");
2401}
2402
2403
2404/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
2405/// into a machine code representation using pattern matching and a machine
2406/// description file.
2407///
2408FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002409 return new AlphaISel(TM);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002410}
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002411