blob: f1a9191a25d61c0909ed6e141ed29f2948259b7a [file] [log] [blame]
Misha Brukman2a8350a2005-02-05 02:24:26 +00001//===- AlphaISelPattern.cpp - A pattern matching inst selector for Alpha --===//
Misha Brukman4633f1c2005-04-21 23:13:11 +00002//
Andrew Lenharth304d0f32005-01-22 23:41:55 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman4633f1c2005-04-21 23:13:11 +00007//
Andrew Lenharth304d0f32005-01-22 23:41:55 +00008//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for Alpha.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Alpha.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000015#include "AlphaRegisterInfo.h"
16#include "llvm/Constants.h" // FIXME: REMOVE
17#include "llvm/Function.h"
Andrew Lenharthb69f3422005-06-22 17:19:45 +000018#include "llvm/Module.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000019#include "llvm/CodeGen/MachineInstrBuilder.h"
20#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
21#include "llvm/CodeGen/MachineFunction.h"
22#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/SelectionDAG.h"
24#include "llvm/CodeGen/SelectionDAGISel.h"
25#include "llvm/CodeGen/SSARegMap.h"
26#include "llvm/Target/TargetData.h"
27#include "llvm/Target/TargetLowering.h"
28#include "llvm/Support/MathExtras.h"
29#include "llvm/ADT/Statistic.h"
Andrew Lenharth032f2352005-02-22 21:59:48 +000030#include "llvm/Support/Debug.h"
Andrew Lenharth95762122005-03-31 21:24:06 +000031#include "llvm/Support/CommandLine.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000032#include <set>
Andrew Lenharth684f2292005-01-30 00:35:27 +000033#include <algorithm>
Andrew Lenharth304d0f32005-01-22 23:41:55 +000034using namespace llvm;
35
Andrew Lenharth95762122005-03-31 21:24:06 +000036namespace llvm {
Misha Brukman4633f1c2005-04-21 23:13:11 +000037 cl::opt<bool> EnableAlphaIDIV("enable-alpha-intfpdiv",
38 cl::desc("Use the FP div instruction for integer div when possible"),
Andrew Lenharth95762122005-03-31 21:24:06 +000039 cl::Hidden);
Andrew Lenharth59009192005-05-04 19:12:09 +000040 cl::opt<bool> EnableAlphaFTOI("enable-alpha-FTOI",
Misha Brukman4633f1c2005-04-21 23:13:11 +000041 cl::desc("Enable use of ftoi* and itof* instructions (ev6 and higher)"),
Andrew Lenharth95762122005-03-31 21:24:06 +000042 cl::Hidden);
Andrew Lenharth59009192005-05-04 19:12:09 +000043 cl::opt<bool> EnableAlphaCT("enable-alpha-CT",
44 cl::desc("Enable use of the ctpop, ctlz, and cttz instructions"),
45 cl::Hidden);
Misha Brukman4633f1c2005-04-21 23:13:11 +000046 cl::opt<bool> EnableAlphaCount("enable-alpha-count",
47 cl::desc("Print estimates on live ins and outs"),
Andrew Lenhartha32b9e32005-04-08 17:28:49 +000048 cl::Hidden);
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +000049 cl::opt<bool> EnableAlphaLSMark("enable-alpha-lsmark",
Misha Brukman5e96a3a2005-06-06 19:08:04 +000050 cl::desc("Emit symbols to correlate Mem ops to LLVM Values"),
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +000051 cl::Hidden);
Andrew Lenharth95762122005-03-31 21:24:06 +000052}
53
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +000054namespace {
55 // Alpha Specific DAG Nodes
56 namespace AlphaISD {
57 enum NodeType {
58 // Start the numbering where the builtin ops leave off.
59 FIRST_NUMBER = ISD::BUILTIN_OP_END,
60
61 //Convert an int bit pattern in an FP reg to a Double or Float
62 //Has a dest type and a source
63 CVTQ,
64 //Move an Ireg to a FPreg
65 ITOF,
66 //Move a FPreg to an Ireg
67 FTOI,
68 };
69 }
70}
71
Andrew Lenharth304d0f32005-01-22 23:41:55 +000072//===----------------------------------------------------------------------===//
73// AlphaTargetLowering - Alpha Implementation of the TargetLowering interface
74namespace {
75 class AlphaTargetLowering : public TargetLowering {
Andrew Lenharth558bc882005-06-18 18:34:52 +000076 int VarArgsOffset; // What is the offset to the first vaarg
77 int VarArgsBase; // What is the base FrameIndex
Andrew Lenharth304d0f32005-01-22 23:41:55 +000078 unsigned GP; //GOT vreg
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +000079 unsigned RA; //Return Address
Andrew Lenharth304d0f32005-01-22 23:41:55 +000080 public:
81 AlphaTargetLowering(TargetMachine &TM) : TargetLowering(TM) {
82 // Set up the TargetLowering object.
Andrew Lenharth3d65d312005-01-27 03:49:45 +000083 //I am having problems with shr n ubyte 1
Andrew Lenharth879ef222005-02-02 17:00:21 +000084 setShiftAmountType(MVT::i64);
85 setSetCCResultType(MVT::i64);
Andrew Lenharthd3355e22005-04-07 20:11:32 +000086 setSetCCResultContents(ZeroOrOneSetCCResult);
Misha Brukman4633f1c2005-04-21 23:13:11 +000087
Andrew Lenharth304d0f32005-01-22 23:41:55 +000088 addRegisterClass(MVT::i64, Alpha::GPRCRegisterClass);
89 addRegisterClass(MVT::f64, Alpha::FPRCRegisterClass);
Andrew Lenharth3d65d312005-01-27 03:49:45 +000090 addRegisterClass(MVT::f32, Alpha::FPRCRegisterClass);
Misha Brukman4633f1c2005-04-21 23:13:11 +000091
Chris Lattnerda4d4692005-04-09 03:22:37 +000092 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000093
Andrew Lenharthec151362005-06-26 22:23:06 +000094 setOperationAction(ISD::EXTLOAD, MVT::i1, Promote);
95 setOperationAction(ISD::EXTLOAD, MVT::f32, Promote);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000096
Andrew Lenharthec151362005-06-26 22:23:06 +000097 setOperationAction(ISD::ZEXTLOAD, MVT::i1 , Expand);
98 setOperationAction(ISD::ZEXTLOAD, MVT::i32 , Expand);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000099
Andrew Lenharthec151362005-06-26 22:23:06 +0000100 setOperationAction(ISD::SEXTLOAD, MVT::i1, Expand);
101 setOperationAction(ISD::SEXTLOAD, MVT::i8, Expand);
102 setOperationAction(ISD::SEXTLOAD, MVT::i16, Expand);
103
104 setOperationAction(ISD::SREM, MVT::f32, Expand);
105 setOperationAction(ISD::SREM, MVT::f64, Expand);
106
107 setOperationAction(ISD::UINT_TO_FP, MVT::i64, Expand);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000108
Andrew Lenharth59009192005-05-04 19:12:09 +0000109 if (!EnableAlphaCT) {
110 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
111 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
Andrew Lenharthb5884d32005-05-04 19:25:37 +0000112 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
Andrew Lenharth59009192005-05-04 19:12:09 +0000113 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000114
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000115 //If this didn't legalize into a div....
116 // setOperationAction(ISD::SREM , MVT::i64, Expand);
117 // setOperationAction(ISD::UREM , MVT::i64, Expand);
118
119 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
120 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
121 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
Andrew Lenharth9818c052005-02-05 13:19:12 +0000122
Chris Lattner17234b72005-04-30 04:26:06 +0000123 // We don't support sin/cos/sqrt
124 setOperationAction(ISD::FSIN , MVT::f64, Expand);
125 setOperationAction(ISD::FCOS , MVT::f64, Expand);
126 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
127 setOperationAction(ISD::FSIN , MVT::f32, Expand);
128 setOperationAction(ISD::FCOS , MVT::f32, Expand);
129 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
130
Andrew Lenharth33819132005-03-04 20:09:23 +0000131 //Doesn't work yet
Chris Lattner17234b72005-04-30 04:26:06 +0000132 setOperationAction(ISD::SETCC, MVT::f32, Promote);
Andrew Lenharth572af902005-02-14 05:41:43 +0000133
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000134 //Try a couple things with a custom expander
135 //setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
136
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000137 computeRegisterProperties();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000138
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000139 addLegalFPImmediate(+0.0); //F31
140 addLegalFPImmediate(-0.0); //-F31
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000141 }
142
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000143 /// LowerOperation - Provide custom lowering hooks for some operations.
144 ///
145 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
146
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000147 /// LowerArguments - This hook must be implemented to indicate how we should
148 /// lower the arguments for the specified function, into the specified DAG.
149 virtual std::vector<SDOperand>
150 LowerArguments(Function &F, SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000151
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000152 /// LowerCallTo - This hook lowers an abstract call to a function into an
153 /// actual call.
154 virtual std::pair<SDOperand, SDOperand>
Chris Lattnerc57f6822005-05-12 19:56:45 +0000155 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattneradf6a962005-05-13 18:50:42 +0000156 bool isTailCall, SDOperand Callee, ArgListTy &Args,
157 SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000158
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000159 virtual std::pair<SDOperand, SDOperand>
Andrew Lenharth558bc882005-06-18 18:34:52 +0000160 LowerVAStart(SDOperand Chain, SelectionDAG &DAG, SDOperand Dest);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000161
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000162 virtual std::pair<SDOperand,SDOperand>
Andrew Lenharth558bc882005-06-18 18:34:52 +0000163 LowerVAArgNext(SDOperand Chain, SDOperand VAList,
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000164 const Type *ArgTy, SelectionDAG &DAG);
165
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000166 std::pair<SDOperand,SDOperand>
167 LowerVACopy(SDOperand Chain, SDOperand Src, SDOperand Dest,
168 SelectionDAG &DAG);
169
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000170 virtual std::pair<SDOperand, SDOperand>
171 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
172 SelectionDAG &DAG);
173
174 void restoreGP(MachineBasicBlock* BB)
175 {
176 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
177 }
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000178 void restoreRA(MachineBasicBlock* BB)
179 {
180 BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(RA).addReg(RA);
181 }
Andrew Lenharth3b918072005-06-27 15:36:48 +0000182 unsigned getRA()
183 {
184 return RA;
185 }
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000186
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000187 };
188}
189
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000190/// LowerOperation - Provide custom lowering hooks for some operations.
191///
192SDOperand AlphaTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
193 MachineFunction &MF = DAG.getMachineFunction();
194 switch (Op.getOpcode()) {
195 default: assert(0 && "Should not custom lower this!");
Misha Brukmanb8ee91a2005-06-06 17:39:46 +0000196#if 0
197 case ISD::SINT_TO_FP:
198 {
199 assert (Op.getOperand(0).getValueType() == MVT::i64
200 && "only quads can be loaded from");
201 SDOperand SRC;
202 if (EnableAlphaFTOI)
203 {
204 std::vector<MVT::ValueType> RTs;
205 RTs.push_back(Op.getValueType());
206 std::vector<SDOperand> Ops;
207 Ops.push_back(Op.getOperand(0));
208 SRC = DAG.getNode(AlphaISD::ITOF, RTs, Ops);
209 } else {
210 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
211 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
212 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
213 Op.getOperand(0), StackSlot, DAG.getSrcValue(NULL));
214 SRC = DAG.getLoad(Op.getValueType(), Store.getValue(0), StackSlot,
215 DAG.getSrcValue(NULL));
216 }
217 std::vector<MVT::ValueType> RTs;
218 RTs.push_back(Op.getValueType());
219 std::vector<SDOperand> Ops;
220 Ops.push_back(SRC);
221 return DAG.getNode(AlphaISD::CVTQ, RTs, Ops);
222 }
223#endif
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000224 }
Misha Brukmanb8ee91a2005-06-06 17:39:46 +0000225 return SDOperand();
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000226}
227
228
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000229/// AddLiveIn - This helper function adds the specified physical register to the
230/// MachineFunction as a live in value. It also creates a corresponding virtual
231/// register for it.
232static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
233 TargetRegisterClass *RC) {
234 assert(RC->contains(PReg) && "Not the correct regclass!");
235 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
236 MF.addLiveIn(PReg, VReg);
237 return VReg;
238}
239
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000240//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
241
242//For now, just use variable size stack frame format
243
244//In a standard call, the first six items are passed in registers $16
245//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
246//of argument-to-register correspondence.) The remaining items are
247//collected in a memory argument list that is a naturally aligned
248//array of quadwords. In a standard call, this list, if present, must
249//be passed at 0(SP).
Misha Brukman7847fca2005-04-22 17:54:37 +0000250//7 ... n 0(SP) ... (n-7)*8(SP)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000251
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000252// //#define FP $15
253// //#define RA $26
254// //#define PV $27
255// //#define GP $29
256// //#define SP $30
Misha Brukman4633f1c2005-04-21 23:13:11 +0000257
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000258std::vector<SDOperand>
Misha Brukman4633f1c2005-04-21 23:13:11 +0000259AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000260{
261 std::vector<SDOperand> ArgValues;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000262
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000263 MachineFunction &MF = DAG.getMachineFunction();
Andrew Lenharth05380342005-02-07 05:07:00 +0000264 MachineFrameInfo*MFI = MF.getFrameInfo();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000265
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000266 MachineBasicBlock& BB = MF.front();
267
Misha Brukman4633f1c2005-04-21 23:13:11 +0000268 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000269 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000270 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000271 Alpha::F19, Alpha::F20, Alpha::F21};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000272 int count = 0;
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000273
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000274 GP = AddLiveIn(MF, Alpha::R29, getRegClassFor(MVT::i64));
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000275 RA = AddLiveIn(MF, Alpha::R26, getRegClassFor(MVT::i64));
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000276
Chris Lattnere4d5c442005-03-15 04:54:21 +0000277 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000278 {
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000279 SDOperand argt;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000280 if (count < 6) {
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000281 unsigned Vreg;
282 MVT::ValueType VT = getValueType(I->getType());
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000283 switch (VT) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000284 default:
285 std::cerr << "Unknown Type " << VT << "\n";
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000286 abort();
287 case MVT::f64:
288 case MVT::f32:
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000289 args_float[count] = AddLiveIn(MF,args_float[count], getRegClassFor(VT));
290 argt = DAG.getCopyFromReg(args_float[count], VT, DAG.getRoot());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000291 break;
292 case MVT::i1:
293 case MVT::i8:
294 case MVT::i16:
295 case MVT::i32:
296 case MVT::i64:
Andrew Lenharth591ec572005-05-31 18:42:18 +0000297 args_int[count] = AddLiveIn(MF, args_int[count], getRegClassFor(MVT::i64));
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000298 argt = DAG.getCopyFromReg(args_int[count], VT, DAG.getRoot());
Andrew Lenharth14f30c92005-05-31 18:37:16 +0000299 if (VT != MVT::i64)
300 argt = DAG.getNode(ISD::TRUNCATE, VT, argt);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000301 break;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000302 }
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000303 DAG.setRoot(argt.getValue(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000304 } else { //more args
305 // Create the frame index object for this incoming parameter...
306 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000307
308 // Create the SelectionDAG nodes corresponding to a load
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000309 //from this parameter
310 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000311 argt = DAG.getLoad(getValueType(I->getType()),
312 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000313 }
Andrew Lenharth032f2352005-02-22 21:59:48 +0000314 ++count;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000315 ArgValues.push_back(argt);
316 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000317
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000318 // If the functions takes variable number of arguments, copy all regs to stack
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000319 if (F.isVarArg()) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000320 VarArgsOffset = count * 8;
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000321 std::vector<SDOperand> LS;
322 for (int i = 0; i < 6; ++i) {
323 if (args_int[i] < 1024)
324 args_int[i] = AddLiveIn(MF,args_int[i], getRegClassFor(MVT::i64));
325 SDOperand argt = DAG.getCopyFromReg(args_int[i], MVT::i64, DAG.getRoot());
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000326 int FI = MFI->CreateFixedObject(8, -8 * (6 - i));
Andrew Lenharth558bc882005-06-18 18:34:52 +0000327 if (i == 0) VarArgsBase = FI;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000328 SDOperand SDFI = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000329 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt, SDFI, DAG.getSrcValue(NULL)));
330
331 if (args_float[i] < 1024)
332 args_float[i] = AddLiveIn(MF,args_float[i], getRegClassFor(MVT::f64));
333 argt = DAG.getCopyFromReg(args_float[i], MVT::f64, DAG.getRoot());
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000334 FI = MFI->CreateFixedObject(8, - 8 * (12 - i));
335 SDFI = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000336 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt, SDFI, DAG.getSrcValue(NULL)));
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000337 }
338
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000339 //Set up a token factor with all the stack traffic
340 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, LS));
341 }
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000342
343 // Finally, inform the code generator which regs we return values in.
344 switch (getValueType(F.getReturnType())) {
345 default: assert(0 && "Unknown type!");
346 case MVT::isVoid: break;
347 case MVT::i1:
348 case MVT::i8:
349 case MVT::i16:
350 case MVT::i32:
351 case MVT::i64:
352 MF.addLiveOut(Alpha::R0);
353 break;
354 case MVT::f32:
355 case MVT::f64:
356 MF.addLiveOut(Alpha::F0);
357 break;
358 }
359
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000360 //return the arguments
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000361 return ArgValues;
362}
363
364std::pair<SDOperand, SDOperand>
365AlphaTargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000366 const Type *RetTy, bool isVarArg,
Chris Lattneradf6a962005-05-13 18:50:42 +0000367 unsigned CallingConv, bool isTailCall,
Misha Brukman7847fca2005-04-22 17:54:37 +0000368 SDOperand Callee, ArgListTy &Args,
369 SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000370 int NumBytes = 0;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000371 if (Args.size() > 6)
372 NumBytes = (Args.size() - 6) * 8;
373
Chris Lattner16cd04d2005-05-12 23:24:06 +0000374 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000375 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000376 std::vector<SDOperand> args_to_use;
377 for (unsigned i = 0, e = Args.size(); i != e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000378 {
379 switch (getValueType(Args[i].second)) {
380 default: assert(0 && "Unexpected ValueType for argument!");
381 case MVT::i1:
382 case MVT::i8:
383 case MVT::i16:
384 case MVT::i32:
385 // Promote the integer to 64 bits. If the input type is signed use a
386 // sign extend, otherwise use a zero extend.
387 if (Args[i].second->isSigned())
388 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
389 else
390 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
391 break;
392 case MVT::i64:
393 case MVT::f64:
394 case MVT::f32:
395 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000396 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000397 args_to_use.push_back(Args[i].first);
398 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000399
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000400 std::vector<MVT::ValueType> RetVals;
401 MVT::ValueType RetTyVT = getValueType(RetTy);
402 if (RetTyVT != MVT::isVoid)
403 RetVals.push_back(RetTyVT);
404 RetVals.push_back(MVT::Other);
405
Misha Brukman4633f1c2005-04-21 23:13:11 +0000406 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000407 Chain, Callee, args_to_use), 0);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000408 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner16cd04d2005-05-12 23:24:06 +0000409 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000410 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000411 return std::make_pair(TheCall, Chain);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000412}
413
414std::pair<SDOperand, SDOperand>
Andrew Lenharth558bc882005-06-18 18:34:52 +0000415AlphaTargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG, SDOperand Dest) {
416 // vastart just stores the address of the VarArgsBase and VarArgsOffset
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000417 SDOperand FR = DAG.getFrameIndex(VarArgsBase, MVT::i64);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000418 SDOperand S1 = DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, Dest, DAG.getSrcValue(NULL));
419 SDOperand SA2 = DAG.getNode(ISD::ADD, MVT::i64, Dest, DAG.getConstant(8, MVT::i64));
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000420 SDOperand S2 = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, S1,
Andrew Lenharth558bc882005-06-18 18:34:52 +0000421 DAG.getConstant(VarArgsOffset, MVT::i64), SA2,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000422 DAG.getSrcValue(NULL), MVT::i32);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000423 return std::make_pair(S2, S2);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000424}
425
426std::pair<SDOperand,SDOperand> AlphaTargetLowering::
Andrew Lenharth558bc882005-06-18 18:34:52 +0000427LowerVAArgNext(SDOperand Chain, SDOperand VAList,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000428 const Type *ArgTy, SelectionDAG &DAG) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000429 SDOperand Base = DAG.getLoad(MVT::i64, Chain, VAList, DAG.getSrcValue(NULL));
430 SDOperand Tmp = DAG.getNode(ISD::ADD, MVT::i64, VAList,
431 DAG.getConstant(8, MVT::i64));
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000432 SDOperand Offset = DAG.getNode(ISD::SEXTLOAD, MVT::i64, Base.getValue(1), Tmp,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000433 DAG.getSrcValue(NULL), MVT::i32);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000434 SDOperand DataPtr = DAG.getNode(ISD::ADD, MVT::i64, Base, Offset);
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000435 if (ArgTy->isFloatingPoint())
436 {
437 //if fp && Offset < 6*8, then subtract 6*8 from DataPtr
438 SDOperand FPDataPtr = DAG.getNode(ISD::SUB, MVT::i64, DataPtr,
439 DAG.getConstant(8*6, MVT::i64));
440 SDOperand CC = DAG.getSetCC(ISD::SETLT, MVT::i64,
441 Offset, DAG.getConstant(8*6, MVT::i64));
442 DataPtr = DAG.getNode(ISD::SELECT, MVT::i64, CC, FPDataPtr, DataPtr);
443 }
444
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000445 SDOperand Result;
446 if (ArgTy == Type::IntTy)
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000447 Result = DAG.getNode(ISD::SEXTLOAD, MVT::i64, Offset.getValue(1), DataPtr,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000448 DAG.getSrcValue(NULL), MVT::i32);
449 else if (ArgTy == Type::UIntTy)
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000450 Result = DAG.getNode(ISD::ZEXTLOAD, MVT::i64, Offset.getValue(1), DataPtr,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000451 DAG.getSrcValue(NULL), MVT::i32);
452 else
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000453 Result = DAG.getLoad(getValueType(ArgTy), Offset.getValue(1), DataPtr,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000454 DAG.getSrcValue(NULL));
455
Andrew Lenharth558bc882005-06-18 18:34:52 +0000456 SDOperand NewOffset = DAG.getNode(ISD::ADD, MVT::i64, Offset,
457 DAG.getConstant(8, MVT::i64));
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000458 SDOperand Update = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Result.getValue(1), NewOffset,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000459 Tmp, DAG.getSrcValue(NULL), MVT::i32);
460 Result = DAG.getNode(ISD::TRUNCATE, getValueType(ArgTy), Result);
461
Andrew Lenharth558bc882005-06-18 18:34:52 +0000462 return std::make_pair(Result, Update);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000463}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000464
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000465std::pair<SDOperand,SDOperand> AlphaTargetLowering::
466LowerVACopy(SDOperand Chain, SDOperand Src, SDOperand Dest,
467 SelectionDAG &DAG) {
468 //Default to returning the input list
469 SDOperand Val = DAG.getLoad(getPointerTy(), Chain, Src, DAG.getSrcValue(NULL));
470 SDOperand Result = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
471 Val, Dest, DAG.getSrcValue(NULL));
472 SDOperand NP = DAG.getNode(ISD::ADD, MVT::i64, Src,
473 DAG.getConstant(8, MVT::i64));
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000474 Val = DAG.getNode(ISD::SEXTLOAD, MVT::i64, Result, NP, DAG.getSrcValue(NULL),
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000475 MVT::i32);
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000476 SDOperand NPD = DAG.getNode(ISD::ADD, MVT::i64, Dest,
477 DAG.getConstant(8, MVT::i64));
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000478 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Val.getValue(1),
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000479 Val, NPD, DAG.getSrcValue(NULL), MVT::i32);
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000480 return std::make_pair(Result, Result);
481}
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000482
483std::pair<SDOperand, SDOperand> AlphaTargetLowering::
484LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
485 SelectionDAG &DAG) {
486 abort();
487}
488
489
490
491
492
493namespace {
494
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000495//===--------------------------------------------------------------------===//
496/// ISel - Alpha specific code to select Alpha machine instructions for
497/// SelectionDAG operations.
498//===--------------------------------------------------------------------===//
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000499class AlphaISel : public SelectionDAGISel {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000500
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000501 /// AlphaLowering - This object fully describes how to lower LLVM code to an
502 /// Alpha-specific SelectionDAG.
503 AlphaTargetLowering AlphaLowering;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000504
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000505 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
506 // for sdiv and udiv until it is put into the future
507 // dag combiner.
508
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000509 /// ExprMap - As shared expressions are codegen'd, we keep track of which
510 /// vreg the value is produced in, so we only emit one copy of each compiled
511 /// tree.
512 static const unsigned notIn = (unsigned)(-1);
513 std::map<SDOperand, unsigned> ExprMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000514
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000515 //CCInvMap sometimes (SetNE) we have the inverse CC code for free
516 std::map<SDOperand, unsigned> CCInvMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000517
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000518 int count_ins;
519 int count_outs;
520 bool has_sym;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000521 int max_depth;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000522
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000523public:
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000524 AlphaISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering), AlphaLowering(TM)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000525 {}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000526
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000527 /// InstructionSelectBasicBlock - This callback is invoked by
528 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
529 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
Andrew Lenharth032f2352005-02-22 21:59:48 +0000530 DEBUG(BB->dump());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000531 count_ins = 0;
532 count_outs = 0;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000533 max_depth = 0;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000534 has_sym = false;
535
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000536 // Codegen the basic block.
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000537 ISelDAG = &DAG;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000538 max_depth = DAG.getRoot().getNodeDepth();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000539 Select(DAG.getRoot());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000540
541 if(has_sym)
542 ++count_ins;
543 if(EnableAlphaCount)
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000544 std::cerr << "COUNT: " << BB->getParent()->getFunction ()->getName() << " "
545 << BB->getNumber() << " "
546 << max_depth << " "
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000547 << count_ins << " "
548 << count_outs << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000549
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000550 // Clear state used for selection.
551 ExprMap.clear();
552 CCInvMap.clear();
553 }
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000554
555 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000556
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000557 unsigned SelectExpr(SDOperand N);
558 unsigned SelectExprFP(SDOperand N, unsigned Result);
559 void Select(SDOperand N);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000560
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000561 void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
562 void SelectBranchCC(SDOperand N);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000563 void MoveFP2Int(unsigned src, unsigned dst, bool isDouble);
564 void MoveInt2FP(unsigned src, unsigned dst, bool isDouble);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000565 //returns whether the sense of the comparison was inverted
566 bool SelectFPSetCC(SDOperand N, unsigned dst);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000567
568 // dag -> dag expanders for integer divide by constant
569 SDOperand BuildSDIVSequence(SDOperand N);
570 SDOperand BuildUDIVSequence(SDOperand N);
571
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000572};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000573}
574
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000575void AlphaISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000576 // If this function has live-in values, emit the copies from pregs to vregs at
577 // the top of the function, before anything else.
578 MachineBasicBlock *BB = MF.begin();
579 if (MF.livein_begin() != MF.livein_end()) {
580 SSARegMap *RegMap = MF.getSSARegMap();
581 for (MachineFunction::livein_iterator LI = MF.livein_begin(),
582 E = MF.livein_end(); LI != E; ++LI) {
583 const TargetRegisterClass *RC = RegMap->getRegClass(LI->second);
584 if (RC == Alpha::GPRCRegisterClass) {
585 BuildMI(BB, Alpha::BIS, 2, LI->second).addReg(LI->first).addReg(LI->first);
586 } else if (RC == Alpha::FPRCRegisterClass) {
587 BuildMI(BB, Alpha::CPYS, 2, LI->second).addReg(LI->first).addReg(LI->first);
588 } else {
589 assert(0 && "Unknown regclass!");
590 }
591 }
592 }
593}
594
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000595//Find the offset of the arg in it's parent's function
596static int getValueOffset(const Value* v)
597{
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000598 static int uniqneg = -1;
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000599 if (v == NULL)
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000600 return uniqneg--;
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000601
602 const Instruction* itarget = dyn_cast<Instruction>(v);
603 const BasicBlock* btarget = itarget->getParent();
604 const Function* ftarget = btarget->getParent();
605
606 //offset due to earlier BBs
607 int i = 0;
608 for(Function::const_iterator ii = ftarget->begin(); &*ii != btarget; ++ii)
609 i += ii->size();
610
611 for(BasicBlock::const_iterator ii = btarget->begin(); &*ii != itarget; ++ii)
612 ++i;
613
614 return i;
615}
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000616//Find the offset of the function in it's module
617static int getFunctionOffset(const Function* fun)
618{
619 const Module* M = fun->getParent();
620
621 //offset due to earlier BBs
622 int i = 0;
623 for(Module::const_iterator ii = M->begin(); &*ii != fun; ++ii)
624 ++i;
625
626 return i;
627}
628
629static int getUID()
630{
631 static int id = 0;
632 return ++id;
633}
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000634
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000635//Factorize a number using the list of constants
636static bool factorize(int v[], int res[], int size, uint64_t c)
637{
638 bool cont = true;
639 while (c != 1 && cont)
640 {
641 cont = false;
642 for(int i = 0; i < size; ++i)
643 {
644 if (c % v[i] == 0)
645 {
646 c /= v[i];
647 ++res[i];
648 cont=true;
649 }
650 }
651 }
652 return c == 1;
653}
654
655
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000656//Shamelessly adapted from PPC32
Misha Brukman4633f1c2005-04-21 23:13:11 +0000657// Structure used to return the necessary information to codegen an SDIV as
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000658// a multiply.
659struct ms {
660 int64_t m; // magic number
661 int64_t s; // shift amount
662};
663
664struct mu {
665 uint64_t m; // magic number
666 int64_t a; // add indicator
667 int64_t s; // shift amount
668};
669
670/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukman4633f1c2005-04-21 23:13:11 +0000671/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000672/// or -1.
673static struct ms magic(int64_t d) {
674 int64_t p;
675 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
676 const uint64_t two63 = 9223372036854775808ULL; // 2^63
677 struct ms mag;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000678
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000679 ad = abs(d);
680 t = two63 + ((uint64_t)d >> 63);
681 anc = t - 1 - t%ad; // absolute value of nc
Andrew Lenharth320174f2005-04-07 17:19:16 +0000682 p = 63; // initialize p
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000683 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
684 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
685 q2 = two63/ad; // initialize q2 = 2p/abs(d)
686 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
687 do {
688 p = p + 1;
689 q1 = 2*q1; // update q1 = 2p/abs(nc)
690 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
691 if (r1 >= anc) { // must be unsigned comparison
692 q1 = q1 + 1;
693 r1 = r1 - anc;
694 }
695 q2 = 2*q2; // update q2 = 2p/abs(d)
696 r2 = 2*r2; // update r2 = rem(2p/abs(d))
697 if (r2 >= ad) { // must be unsigned comparison
698 q2 = q2 + 1;
699 r2 = r2 - ad;
700 }
701 delta = ad - r2;
702 } while (q1 < delta || (q1 == delta && r1 == 0));
703
704 mag.m = q2 + 1;
705 if (d < 0) mag.m = -mag.m; // resulting magic number
706 mag.s = p - 64; // resulting shift
707 return mag;
708}
709
710/// magicu - calculate the magic numbers required to codegen an integer udiv as
711/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
712static struct mu magicu(uint64_t d)
713{
714 int64_t p;
715 uint64_t nc, delta, q1, r1, q2, r2;
716 struct mu magu;
717 magu.a = 0; // initialize "add" indicator
718 nc = - 1 - (-d)%d;
Andrew Lenharth320174f2005-04-07 17:19:16 +0000719 p = 63; // initialize p
720 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
721 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
722 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
723 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000724 do {
725 p = p + 1;
726 if (r1 >= nc - r1 ) {
727 q1 = 2*q1 + 1; // update q1
728 r1 = 2*r1 - nc; // update r1
729 }
730 else {
731 q1 = 2*q1; // update q1
732 r1 = 2*r1; // update r1
733 }
734 if (r2 + 1 >= d - r2) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000735 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000736 q2 = 2*q2 + 1; // update q2
737 r2 = 2*r2 + 1 - d; // update r2
738 }
739 else {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000740 if (q2 >= 0x8000000000000000ull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000741 q2 = 2*q2; // update q2
742 r2 = 2*r2 + 1; // update r2
743 }
744 delta = d - 1 - r2;
745 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
746 magu.m = q2 + 1; // resulting magic number
Andrew Lenharth320174f2005-04-07 17:19:16 +0000747 magu.s = p - 64; // resulting shift
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000748 return magu;
749}
750
751/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
752/// return a DAG expression to select that will generate the same value by
753/// multiplying by a magic number. See:
754/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000755SDOperand AlphaISel::BuildSDIVSequence(SDOperand N) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000756 int64_t d = (int64_t)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000757 ms magics = magic(d);
758 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000759 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000760 ISelDAG->getConstant(magics.m, MVT::i64));
761 // If d > 0 and m < 0, add the numerator
762 if (d > 0 && magics.m < 0)
763 Q = ISelDAG->getNode(ISD::ADD, MVT::i64, Q, N.getOperand(0));
764 // If d < 0 and m > 0, subtract the numerator.
765 if (d < 0 && magics.m > 0)
766 Q = ISelDAG->getNode(ISD::SUB, MVT::i64, Q, N.getOperand(0));
767 // Shift right algebraic if shift value is nonzero
768 if (magics.s > 0)
Misha Brukman4633f1c2005-04-21 23:13:11 +0000769 Q = ISelDAG->getNode(ISD::SRA, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000770 ISelDAG->getConstant(magics.s, MVT::i64));
771 // Extract the sign bit and add it to the quotient
Misha Brukman4633f1c2005-04-21 23:13:11 +0000772 SDOperand T =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000773 ISelDAG->getNode(ISD::SRL, MVT::i64, Q, ISelDAG->getConstant(63, MVT::i64));
774 return ISelDAG->getNode(ISD::ADD, MVT::i64, Q, T);
775}
776
777/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
778/// return a DAG expression to select that will generate the same value by
779/// multiplying by a magic number. See:
780/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000781SDOperand AlphaISel::BuildUDIVSequence(SDOperand N) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000782 unsigned d =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000783 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
784 mu magics = magicu(d);
785 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000786 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000787 ISelDAG->getConstant(magics.m, MVT::i64));
788 if (magics.a == 0) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000789 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000790 ISelDAG->getConstant(magics.s, MVT::i64));
791 } else {
792 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i64, N.getOperand(0), Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000793 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000794 ISelDAG->getConstant(1, MVT::i64));
795 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i64, NPQ, Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000796 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000797 ISelDAG->getConstant(magics.s-1, MVT::i64));
798 }
799 return Q;
800}
801
Andrew Lenhartha565c272005-04-06 22:03:13 +0000802//From PPC32
803/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
804/// returns zero when the input is not exactly a power of two.
805static unsigned ExactLog2(uint64_t Val) {
806 if (Val == 0 || (Val & (Val-1))) return 0;
807 unsigned Count = 0;
808 while (Val != 1) {
809 Val >>= 1;
810 ++Count;
811 }
812 return Count;
813}
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000814
815
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000816//These describe LDAx
Andrew Lenharthc0513832005-03-29 19:24:04 +0000817static const int IMM_LOW = -32768;
818static const int IMM_HIGH = 32767;
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000819static const int IMM_MULT = 65536;
820
821static long getUpper16(long l)
822{
823 long y = l / IMM_MULT;
824 if (l % IMM_MULT > IMM_HIGH)
825 ++y;
826 return y;
827}
828
829static long getLower16(long l)
830{
831 long h = getUpper16(l);
832 return l - h * IMM_MULT;
833}
834
Andrew Lenharth65838902005-02-06 16:22:15 +0000835static unsigned GetSymVersion(unsigned opcode)
836{
837 switch (opcode) {
838 default: assert(0 && "unknown load or store"); return 0;
839 case Alpha::LDQ: return Alpha::LDQ_SYM;
840 case Alpha::LDS: return Alpha::LDS_SYM;
841 case Alpha::LDT: return Alpha::LDT_SYM;
842 case Alpha::LDL: return Alpha::LDL_SYM;
843 case Alpha::LDBU: return Alpha::LDBU_SYM;
844 case Alpha::LDWU: return Alpha::LDWU_SYM;
845 case Alpha::LDW: return Alpha::LDW_SYM;
846 case Alpha::LDB: return Alpha::LDB_SYM;
847 case Alpha::STQ: return Alpha::STQ_SYM;
848 case Alpha::STS: return Alpha::STS_SYM;
849 case Alpha::STT: return Alpha::STT_SYM;
850 case Alpha::STL: return Alpha::STL_SYM;
851 case Alpha::STW: return Alpha::STW_SYM;
852 case Alpha::STB: return Alpha::STB_SYM;
853 }
854}
855
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000856void AlphaISel::MoveFP2Int(unsigned src, unsigned dst, bool isDouble)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000857{
858 unsigned Opc;
859 if (EnableAlphaFTOI) {
860 Opc = isDouble ? Alpha::FTOIT : Alpha::FTOIS;
861 BuildMI(BB, Opc, 1, dst).addReg(src);
862 } else {
863 //The hard way:
864 // Spill the integer to memory and reload it from there.
865 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
866 MachineFunction *F = BB->getParent();
867 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
868
869 Opc = isDouble ? Alpha::STT : Alpha::STS;
870 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
871 Opc = isDouble ? Alpha::LDQ : Alpha::LDL;
872 BuildMI(BB, Alpha::LDQ, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
873 }
874}
875
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000876void AlphaISel::MoveInt2FP(unsigned src, unsigned dst, bool isDouble)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000877{
878 unsigned Opc;
879 if (EnableAlphaFTOI) {
880 Opc = isDouble?Alpha::ITOFT:Alpha::ITOFS;
881 BuildMI(BB, Opc, 1, dst).addReg(src);
882 } else {
883 //The hard way:
884 // Spill the integer to memory and reload it from there.
885 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
886 MachineFunction *F = BB->getParent();
887 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
888
889 Opc = isDouble ? Alpha::STQ : Alpha::STL;
890 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
891 Opc = isDouble ? Alpha::LDT : Alpha::LDS;
892 BuildMI(BB, Opc, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
893 }
894}
895
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000896bool AlphaISel::SelectFPSetCC(SDOperand N, unsigned dst)
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000897{
898 SDNode *Node = N.Val;
899 unsigned Opc, Tmp1, Tmp2, Tmp3;
900 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node);
901
902 //assert(SetCC->getOperand(0).getValueType() != MVT::f32 && "SetCC f32 should have been promoted");
903 bool rev = false;
904 bool inv = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000905
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000906 switch (SetCC->getCondition()) {
907 default: Node->dump(); assert(0 && "Unknown FP comparison!");
908 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
909 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
910 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
911 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
912 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
913 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
914 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000915
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000916 ConstantFPSDNode *CN;
917 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
918 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
919 Tmp1 = Alpha::F31;
920 else
921 Tmp1 = SelectExpr(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000922
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000923 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
924 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
925 Tmp2 = Alpha::F31;
926 else
Chris Lattner9c9183a2005-04-30 04:44:07 +0000927 Tmp2 = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000928
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000929 //Can only compare doubles, and dag won't promote for me
930 if (SetCC->getOperand(0).getValueType() == MVT::f32)
931 {
932 //assert(0 && "Setcc On float?\n");
933 std::cerr << "Setcc on float!\n";
934 Tmp3 = MakeReg(MVT::f64);
935 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
936 Tmp1 = Tmp3;
937 }
938 if (SetCC->getOperand(1).getValueType() == MVT::f32)
939 {
940 //assert (0 && "Setcc On float?\n");
941 std::cerr << "Setcc on float!\n";
942 Tmp3 = MakeReg(MVT::f64);
943 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp2);
944 Tmp2 = Tmp3;
945 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000946
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000947 if (rev) std::swap(Tmp1, Tmp2);
948 //do the comparison
949 BuildMI(BB, Opc, 2, dst).addReg(Tmp1).addReg(Tmp2);
950 return inv;
951}
952
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000953//Check to see if the load is a constant offset from a base register
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000954void AlphaISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000955{
956 unsigned opcode = N.getOpcode();
Andrew Lenharth694c2982005-06-26 23:01:11 +0000957 if (opcode == ISD::ADD && N.getOperand(1).getOpcode() == ISD::Constant &&
958 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
959 { //Normal imm add
960 Reg = SelectExpr(N.getOperand(0));
961 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
962 return;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000963 }
964 Reg = SelectExpr(N);
965 offset = 0;
966 return;
967}
968
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000969void AlphaISel::SelectBranchCC(SDOperand N)
Andrew Lenharth445171a2005-02-08 00:40:03 +0000970{
971 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000972 MachineBasicBlock *Dest =
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000973 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
974 unsigned Opc = Alpha::WTF;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000975
Andrew Lenharth445171a2005-02-08 00:40:03 +0000976 Select(N.getOperand(0)); //chain
977 SDOperand CC = N.getOperand(1);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000978
Andrew Lenharth445171a2005-02-08 00:40:03 +0000979 if (CC.getOpcode() == ISD::SETCC)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000980 {
981 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
982 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
983 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharth09552bf2005-06-08 18:02:21 +0000984 bool RightZero = SetCC->getOperand(1).getOpcode() == ISD::Constant &&
985 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000986 bool isNE = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000987
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000988 //Fix up CC
989 ISD::CondCode cCode= SetCC->getCondition();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000990
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000991 if(cCode == ISD::SETNE)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000992 isNE = true;
Andrew Lenharth445171a2005-02-08 00:40:03 +0000993
Andrew Lenharth694c2982005-06-26 23:01:11 +0000994 if (RightZero) {
Andrew Lenharth09552bf2005-06-08 18:02:21 +0000995 switch (cCode) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000996 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
997 case ISD::SETEQ: Opc = Alpha::BEQ; break;
998 case ISD::SETLT: Opc = Alpha::BLT; break;
999 case ISD::SETLE: Opc = Alpha::BLE; break;
1000 case ISD::SETGT: Opc = Alpha::BGT; break;
1001 case ISD::SETGE: Opc = Alpha::BGE; break;
1002 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
1003 case ISD::SETUGT: Opc = Alpha::BNE; break;
1004 case ISD::SETULE: Opc = Alpha::BEQ; break; //Technically you could have this CC
1005 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
1006 case ISD::SETNE: Opc = Alpha::BNE; break;
1007 }
Andrew Lenharth694c2982005-06-26 23:01:11 +00001008 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001009 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
1010 return;
1011 } else {
1012 unsigned Tmp1 = SelectExpr(CC);
1013 if (isNE)
1014 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
1015 else
1016 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001017 return;
1018 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001019 } else { //FP
1020 //Any comparison between 2 values should be codegened as an folded branch, as moving
1021 //CC to the integer register is very expensive
1022 //for a cmp b: c = a - b;
1023 //a = b: c = 0
1024 //a < b: c < 0
1025 //a > b: c > 0
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001026
1027 bool invTest = false;
1028 unsigned Tmp3;
1029
1030 ConstantFPSDNode *CN;
1031 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
1032 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1033 Tmp3 = SelectExpr(SetCC->getOperand(0));
1034 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
1035 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1036 {
1037 Tmp3 = SelectExpr(SetCC->getOperand(1));
1038 invTest = true;
1039 }
1040 else
1041 {
1042 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
1043 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
1044 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
1045 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1046 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1047 .addReg(Tmp1).addReg(Tmp2);
1048 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001049
1050 switch (SetCC->getCondition()) {
1051 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001052 case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
1053 case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
1054 case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
1055 case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
1056 case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
1057 case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001058 }
1059 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001060 return;
1061 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001062 abort(); //Should never be reached
1063 } else {
1064 //Giveup and do the stupid thing
1065 unsigned Tmp1 = SelectExpr(CC);
1066 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
1067 return;
1068 }
Andrew Lenharth445171a2005-02-08 00:40:03 +00001069 abort(); //Should never be reached
1070}
1071
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001072unsigned AlphaISel::SelectExprFP(SDOperand N, unsigned Result)
Andrew Lenharth40831c52005-01-28 06:57:18 +00001073{
1074 unsigned Tmp1, Tmp2, Tmp3;
1075 unsigned Opc = 0;
1076 SDNode *Node = N.Val;
1077 MVT::ValueType DestType = N.getValueType();
1078 unsigned opcode = N.getOpcode();
1079
1080 switch (opcode) {
1081 default:
1082 Node->dump();
1083 assert(0 && "Node not handled!\n");
Andrew Lenharth2c594352005-01-29 15:42:07 +00001084
Andrew Lenharth7332f3e2005-04-02 19:11:07 +00001085 case ISD::UNDEF: {
1086 BuildMI(BB, Alpha::IDEF, 0, Result);
1087 return Result;
1088 }
1089
Andrew Lenharth30b46d42005-04-02 19:04:58 +00001090 case ISD::FNEG:
1091 if(ISD::FABS == N.getOperand(0).getOpcode())
1092 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001093 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1094 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +00001095 } else {
Misha Brukman7847fca2005-04-22 17:54:37 +00001096 Tmp1 = SelectExpr(N.getOperand(0));
1097 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +00001098 }
1099 return Result;
1100
1101 case ISD::FABS:
1102 Tmp1 = SelectExpr(N.getOperand(0));
1103 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
1104 return Result;
1105
Andrew Lenharth9818c052005-02-05 13:19:12 +00001106 case ISD::SELECT:
1107 {
Andrew Lenharth45859692005-03-03 21:47:53 +00001108 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1109 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
1110 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
1111
1112 SDOperand CC = N.getOperand(0);
1113 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
1114
Misha Brukman4633f1c2005-04-21 23:13:11 +00001115 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth45859692005-03-03 21:47:53 +00001116 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
1117 { //FP Setcc -> Select yay!
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001118
1119
Andrew Lenharth45859692005-03-03 21:47:53 +00001120 //for a cmp b: c = a - b;
1121 //a = b: c = 0
1122 //a < b: c < 0
1123 //a > b: c > 0
Misha Brukman4633f1c2005-04-21 23:13:11 +00001124
Andrew Lenharth45859692005-03-03 21:47:53 +00001125 bool invTest = false;
1126 unsigned Tmp3;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001127
Andrew Lenharth45859692005-03-03 21:47:53 +00001128 ConstantFPSDNode *CN;
1129 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
1130 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1131 Tmp3 = SelectExpr(SetCC->getOperand(0));
1132 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
1133 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1134 {
1135 Tmp3 = SelectExpr(SetCC->getOperand(1));
1136 invTest = true;
1137 }
1138 else
1139 {
1140 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
1141 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
1142 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
1143 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1144 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1145 .addReg(Tmp1).addReg(Tmp2);
1146 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001147
Andrew Lenharth45859692005-03-03 21:47:53 +00001148 switch (SetCC->getCondition()) {
1149 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
1150 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNE : Alpha::FCMOVEQ; break;
1151 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGT : Alpha::FCMOVLT; break;
1152 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGE : Alpha::FCMOVLE; break;
1153 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLT : Alpha::FCMOVGT; break;
1154 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLE : Alpha::FCMOVGE; break;
1155 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQ : Alpha::FCMOVNE; break;
1156 }
Andrew Lenharth33819132005-03-04 20:09:23 +00001157 BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
Andrew Lenharth45859692005-03-03 21:47:53 +00001158 return Result;
1159 }
1160 else
1161 {
1162 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001163 BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV).addReg(Tmp1);
1164// // Spill the cond to memory and reload it from there.
1165// unsigned Tmp4 = MakeReg(MVT::f64);
1166// MoveIntFP(Tmp1, Tmp4, true);
1167// //now ideally, we don't have to do anything to the flag...
1168// // Get the condition into the zero flag.
1169// BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
Andrew Lenharth45859692005-03-03 21:47:53 +00001170 return Result;
1171 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001172 }
1173
Andrew Lenharthc1faced2005-02-01 01:37:24 +00001174 case ISD::FP_ROUND:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001175 assert (DestType == MVT::f32 &&
1176 N.getOperand(0).getValueType() == MVT::f64 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001177 "only f64 to f32 conversion supported here");
Andrew Lenharthc1faced2005-02-01 01:37:24 +00001178 Tmp1 = SelectExpr(N.getOperand(0));
1179 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
1180 return Result;
1181
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001182 case ISD::FP_EXTEND:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001183 assert (DestType == MVT::f64 &&
1184 N.getOperand(0).getValueType() == MVT::f32 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001185 "only f32 to f64 conversion supported here");
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001186 Tmp1 = SelectExpr(N.getOperand(0));
1187 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
1188 return Result;
1189
Andrew Lenharth2c594352005-01-29 15:42:07 +00001190 case ISD::CopyFromReg:
1191 {
1192 // Make sure we generate both values.
1193 if (Result != notIn)
1194 ExprMap[N.getValue(1)] = notIn; // Generate the token
1195 else
1196 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001197
Andrew Lenharth2c594352005-01-29 15:42:07 +00001198 SDOperand Chain = N.getOperand(0);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001199
Andrew Lenharth2c594352005-01-29 15:42:07 +00001200 Select(Chain);
1201 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1202 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1203 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
1204 return Result;
1205 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001206
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001207 case ISD::LOAD:
1208 {
1209 // Make sure we generate both values.
1210 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001211 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001212 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001213 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001214
Andrew Lenharth29219162005-02-07 06:31:44 +00001215 DestType = N.getValue(0).getValueType();
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001216
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001217 SDOperand Chain = N.getOperand(0);
1218 SDOperand Address = N.getOperand(1);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001219 Select(Chain);
Andrew Lenharth65838902005-02-06 16:22:15 +00001220 Opc = DestType == MVT::f64 ? Alpha::LDT : Alpha::LDS;
1221
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +00001222 if (EnableAlphaLSMark)
1223 {
1224 int i = getValueOffset(dyn_cast<SrcValueSDNode>(N.getOperand(2))->getValue());
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001225 int j = getFunctionOffset(BB->getParent()->getFunction());
1226 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +00001227 }
1228
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001229 if (Address.getOpcode() == ISD::GlobalAddress) {
1230 AlphaLowering.restoreGP(BB);
1231 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001232 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001233 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1234 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001235 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001236 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001237 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001238 has_sym = true;
Andrew Lenharth97127a12005-02-05 17:41:39 +00001239 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001240 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001241 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001242 BuildMI(BB, Opc, 2, Result)
1243 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1244 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001245 } else {
1246 long offset;
1247 SelectAddr(Address, Tmp1, offset);
1248 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1249 }
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001250 return Result;
1251 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001252 case ISD::ConstantFP:
1253 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
1254 if (CN->isExactlyValue(+0.0)) {
1255 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001256 } else if ( CN->isExactlyValue(-0.0)) {
1257 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001258 } else {
1259 abort();
1260 }
1261 }
1262 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001263
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001264 case ISD::SDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001265 case ISD::MUL:
1266 case ISD::ADD:
1267 case ISD::SUB:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001268 switch( opcode ) {
1269 case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS; break;
1270 case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS; break;
1271 case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS; break;
1272 case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS; break;
1273 };
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001274
1275 ConstantFPSDNode *CN;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001276 if (opcode == ISD::SUB
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001277 && (CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
1278 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1279 {
1280 Tmp2 = SelectExpr(N.getOperand(1));
1281 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp2).addReg(Tmp2);
1282 } else {
1283 Tmp1 = SelectExpr(N.getOperand(0));
1284 Tmp2 = SelectExpr(N.getOperand(1));
1285 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1286 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001287 return Result;
1288
Andrew Lenharth2c594352005-01-29 15:42:07 +00001289 case ISD::EXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001290 {
1291 //include a conversion sequence for float loads to double
1292 if (Result != notIn)
1293 ExprMap[N.getValue(1)] = notIn; // Generate the token
1294 else
1295 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001296
Andrew Lenhartha549deb2005-02-07 05:33:15 +00001297 Tmp1 = MakeReg(MVT::f32);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001298
1299 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001300 "EXTLOAD not from f32");
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001301 assert(Node->getValueType(0) == MVT::f64 && "EXTLOAD not to f64");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001302
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001303 SDOperand Chain = N.getOperand(0);
1304 SDOperand Address = N.getOperand(1);
1305 Select(Chain);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001306
Andrew Lenharthb72bcbb2005-06-27 16:40:26 +00001307 if (EnableAlphaLSMark)
1308 {
1309 int i = getValueOffset(dyn_cast<SrcValueSDNode>(N.getOperand(2))->getValue());
1310 int j = getFunctionOffset(BB->getParent()->getFunction());
1311 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
1312 }
1313
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001314 if (Address.getOpcode() == ISD::GlobalAddress) {
1315 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001316 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001317 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1318 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001319 else if (ConstantPoolSDNode *CP =
1320 dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001321 {
1322 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001323 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001324 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addConstantPoolIndex(CP->getIndex());
1325 }
1326 else if(Address.getOpcode() == ISD::FrameIndex) {
1327 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
Andrew Lenharth032f2352005-02-22 21:59:48 +00001328 BuildMI(BB, Alpha::LDS, 2, Tmp1)
1329 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1330 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001331 } else {
1332 long offset;
1333 SelectAddr(Address, Tmp2, offset);
1334 BuildMI(BB, Alpha::LDS, 1, Tmp1).addImm(offset).addReg(Tmp2);
1335 }
Andrew Lenharth29219162005-02-07 06:31:44 +00001336 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001337 return Result;
1338 }
Andrew Lenharth2c594352005-01-29 15:42:07 +00001339
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001340 case ISD::SINT_TO_FP:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001341 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001342 assert (N.getOperand(0).getValueType() == MVT::i64
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001343 && "only quads can be loaded from");
Andrew Lenharth40831c52005-01-28 06:57:18 +00001344 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001345 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001346 MoveInt2FP(Tmp1, Tmp2, true);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001347 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
1348 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001349 return Result;
1350 }
1351 }
1352 assert(0 && "should not get here");
1353 return 0;
1354}
1355
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001356unsigned AlphaISel::SelectExpr(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001357 unsigned Result;
Andrew Lenharth2966e842005-04-07 18:15:28 +00001358 unsigned Tmp1, Tmp2 = 0, Tmp3;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001359 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001360 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001361
1362 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001363 MVT::ValueType DestType = N.getValueType();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001364
1365 unsigned &Reg = ExprMap[N];
1366 if (Reg) return Reg;
1367
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001368 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::TAILCALL)
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001369 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001370 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001371 else {
1372 // If this is a call instruction, make sure to prepare ALL of the result
1373 // values as well as the chain.
1374 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001375 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001376 else {
1377 Result = MakeReg(Node->getValueType(0));
1378 ExprMap[N.getValue(0)] = Result;
1379 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1380 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001381 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001382 }
1383 }
1384
Andrew Lenharth50d91d72005-04-30 14:19:13 +00001385 if ((DestType == MVT::f64 || DestType == MVT::f32 ||
1386 (
1387 (opcode == ISD::LOAD || opcode == ISD::CopyFromReg ||
1388 opcode == ISD::EXTLOAD) &&
1389 (N.getValue(0).getValueType() == MVT::f32 ||
1390 N.getValue(0).getValueType() == MVT::f64)
1391 ))
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001392 && opcode != ISD::CALL && opcode != ISD::TAILCALL
Andrew Lenharth06342c32005-02-07 06:21:37 +00001393 )
Andrew Lenharth40831c52005-01-28 06:57:18 +00001394 return SelectExprFP(N, Result);
1395
1396 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001397 default:
1398 Node->dump();
1399 assert(0 && "Node not handled!\n");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001400
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001401 case ISD::CTPOP:
1402 case ISD::CTTZ:
1403 case ISD::CTLZ:
1404 Opc = opcode == ISD::CTPOP ? Alpha::CTPOP :
1405 (opcode == ISD::CTTZ ? Alpha::CTTZ : Alpha::CTLZ);
1406 Tmp1 = SelectExpr(N.getOperand(0));
1407 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1408 return Result;
1409
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001410 case ISD::MULHU:
1411 Tmp1 = SelectExpr(N.getOperand(0));
1412 Tmp2 = SelectExpr(N.getOperand(1));
1413 BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth706be912005-04-07 13:55:53 +00001414 return Result;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001415 case ISD::MULHS:
1416 {
1417 //MULHU - Ra<63>*Rb - Rb<63>*Ra
1418 Tmp1 = SelectExpr(N.getOperand(0));
1419 Tmp2 = SelectExpr(N.getOperand(1));
1420 Tmp3 = MakeReg(MVT::i64);
1421 BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1422 unsigned V1 = MakeReg(MVT::i64);
1423 unsigned V2 = MakeReg(MVT::i64);
1424 BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31).addReg(Tmp1);
1425 BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31).addReg(Tmp2);
1426 unsigned IRes = MakeReg(MVT::i64);
1427 BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
1428 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
1429 return Result;
1430 }
Andrew Lenharth7332f3e2005-04-02 19:11:07 +00001431 case ISD::UNDEF: {
1432 BuildMI(BB, Alpha::IDEF, 0, Result);
1433 return Result;
1434 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001435
Andrew Lenharth032f2352005-02-22 21:59:48 +00001436 case ISD::DYNAMIC_STACKALLOC:
1437 // Generate both result values.
Andrew Lenharth3a7118d2005-02-23 17:33:42 +00001438 if (Result != notIn)
1439 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth032f2352005-02-22 21:59:48 +00001440 else
1441 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1442
1443 // FIXME: We are currently ignoring the requested alignment for handling
1444 // greater than the stack alignment. This will need to be revisited at some
1445 // point. Align = N.getOperand(2);
1446
1447 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1448 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1449 std::cerr << "Cannot allocate stack object with greater alignment than"
1450 << " the stack alignment yet!";
1451 abort();
1452 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001453
Andrew Lenharth032f2352005-02-22 21:59:48 +00001454 Select(N.getOperand(0));
1455 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1456 {
1457 if (CN->getValue() < 32000)
1458 {
1459 BuildMI(BB, Alpha::LDA, 2, Alpha::R30)
1460 .addImm(-CN->getValue()).addReg(Alpha::R30);
1461 } else {
1462 Tmp1 = SelectExpr(N.getOperand(1));
1463 // Subtract size from stack pointer, thereby allocating some space.
1464 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1465 }
1466 } else {
1467 Tmp1 = SelectExpr(N.getOperand(1));
1468 // Subtract size from stack pointer, thereby allocating some space.
1469 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1470 }
1471
1472 // Put a pointer to the space into the result register, by copying the stack
1473 // pointer.
Andrew Lenharth7bc47022005-02-22 23:29:25 +00001474 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
Andrew Lenharth032f2352005-02-22 21:59:48 +00001475 return Result;
1476
Andrew Lenharth33819132005-03-04 20:09:23 +00001477// case ISD::ConstantPool:
1478// Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1479// AlphaLowering.restoreGP(BB);
1480// BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(Tmp1);
1481// return Result;
Andrew Lenharth2c594352005-01-29 15:42:07 +00001482
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001483 case ISD::FrameIndex:
Andrew Lenharth032f2352005-02-22 21:59:48 +00001484 BuildMI(BB, Alpha::LDA, 2, Result)
1485 .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
1486 .addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001487 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001488
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001489 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001490 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001491 case ISD::SEXTLOAD:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001492 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001493 {
1494 // Make sure we generate both values.
1495 if (Result != notIn)
1496 ExprMap[N.getValue(1)] = notIn; // Generate the token
1497 else
1498 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001499
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001500 SDOperand Chain = N.getOperand(0);
1501 SDOperand Address = N.getOperand(1);
1502 Select(Chain);
1503
Misha Brukman4633f1c2005-04-21 23:13:11 +00001504 assert(Node->getValueType(0) == MVT::i64 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001505 "Unknown type to sign extend to.");
Andrew Lenharth03824012005-02-07 05:55:55 +00001506 if (opcode == ISD::LOAD)
1507 Opc = Alpha::LDQ;
1508 else
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001509 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
1510 default: Node->dump(); assert(0 && "Bad sign extend!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001511 case MVT::i32: Opc = Alpha::LDL;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001512 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001513 case MVT::i16: Opc = Alpha::LDWU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001514 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001515 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Misha Brukman4633f1c2005-04-21 23:13:11 +00001516 case MVT::i8: Opc = Alpha::LDBU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001517 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001518 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001519
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001520 if (EnableAlphaLSMark)
1521 {
1522 int i = getValueOffset(dyn_cast<SrcValueSDNode>(N.getOperand(2))->getValue());
1523 int j = getFunctionOffset(BB->getParent()->getFunction());
1524 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
1525 }
1526
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001527 if (Address.getOpcode() == ISD::GlobalAddress) {
1528 AlphaLowering.restoreGP(BB);
1529 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001530 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001531 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1532 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001533 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1534 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001535 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001536 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001537 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001538 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001539 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001540 BuildMI(BB, Opc, 2, Result)
1541 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1542 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001543 } else {
1544 long offset;
1545 SelectAddr(Address, Tmp1, offset);
1546 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1547 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001548 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001549 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001550
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001551 case ISD::GlobalAddress:
1552 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001553 has_sym = true;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001554 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
1555 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
1556 return Result;
1557
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001558 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001559 case ISD::CALL:
1560 {
1561 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001562
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001563 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001564 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001565
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001566 //grab the arguments
1567 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001568 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001569 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001570 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001571
Andrew Lenharth684f2292005-01-30 00:35:27 +00001572 //in reg args
1573 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001574 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001575 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001576 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +00001577 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001578 Alpha::F19, Alpha::F20, Alpha::F21};
1579 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001580 default:
1581 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001582 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001583 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001584 N.getOperand(i+2).getValueType() << "\n";
1585 assert(0 && "Unknown value type for call");
1586 case MVT::i1:
1587 case MVT::i8:
1588 case MVT::i16:
1589 case MVT::i32:
1590 case MVT::i64:
1591 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1592 break;
1593 case MVT::f32:
1594 case MVT::f64:
1595 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1596 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001597 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001598 }
Andrew Lenharth684f2292005-01-30 00:35:27 +00001599 //in mem args
1600 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001601 {
1602 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001603 default:
1604 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001605 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001606 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001607 N.getOperand(i+2).getValueType() << "\n";
1608 assert(0 && "Unknown value type for call");
1609 case MVT::i1:
1610 case MVT::i8:
1611 case MVT::i16:
1612 case MVT::i32:
1613 case MVT::i64:
1614 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1615 break;
1616 case MVT::f32:
1617 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1618 break;
1619 case MVT::f64:
1620 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1621 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001622 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001623 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001624 //build the right kind of call
1625 if (GlobalAddressSDNode *GASD =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001626 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001627 {
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001628 if (GASD->getGlobal()->isExternal()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001629 //use safe calling convention
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001630 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001631 has_sym = true;
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001632 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal());
1633 } else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001634 //use PC relative branch call
Andrew Lenharth1e0d9bd2005-04-14 17:34:20 +00001635 AlphaLowering.restoreGP(BB);
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001636 BuildMI(BB, Alpha::BSR, 1, Alpha::R26).addGlobalAddress(GASD->getGlobal(),true);
1637 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001638 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001639 else if (ExternalSymbolSDNode *ESSDN =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001640 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001641 {
1642 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001643 has_sym = true;
Andrew Lenharthba05ad62005-03-30 18:22:52 +00001644 BuildMI(BB, Alpha::CALL, 1).addExternalSymbol(ESSDN->getSymbol(), true);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001645 } else {
1646 //no need to restore GP as we are doing an indirect call
1647 Tmp1 = SelectExpr(N.getOperand(1));
1648 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
1649 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
1650 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001651
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001652 //push the result into a virtual register
Misha Brukman4633f1c2005-04-21 23:13:11 +00001653
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001654 switch (Node->getValueType(0)) {
1655 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001656 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001657 case MVT::i1:
1658 case MVT::i8:
1659 case MVT::i16:
1660 case MVT::i32:
1661 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001662 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
1663 break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001664 case MVT::f32:
1665 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001666 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
1667 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001668 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001669 return Result+N.ResNo;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001670 }
1671
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001672 case ISD::SIGN_EXTEND_INREG:
1673 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001674 //do SDIV opt for all levels of ints if not dividing by a constant
1675 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
1676 && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001677 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001678 unsigned Tmp4 = MakeReg(MVT::f64);
1679 unsigned Tmp5 = MakeReg(MVT::f64);
1680 unsigned Tmp6 = MakeReg(MVT::f64);
1681 unsigned Tmp7 = MakeReg(MVT::f64);
1682 unsigned Tmp8 = MakeReg(MVT::f64);
1683 unsigned Tmp9 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001684
1685 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1686 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1687 MoveInt2FP(Tmp1, Tmp4, true);
1688 MoveInt2FP(Tmp2, Tmp5, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001689 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Tmp4);
1690 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Tmp5);
1691 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
1692 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Tmp8);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001693 MoveFP2Int(Tmp9, Result, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001694 return Result;
1695 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001696
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001697 //Alpha has instructions for a bunch of signed 32 bit stuff
1698 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001699 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001700 switch (N.getOperand(0).getOpcode()) {
1701 case ISD::ADD:
1702 case ISD::SUB:
1703 case ISD::MUL:
1704 {
1705 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
1706 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
1707 //FIXME: first check for Scaled Adds and Subs!
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001708 ConstantSDNode* CSD = NULL;
1709 if(!isMul && N.getOperand(0).getOperand(0).getOpcode() == ISD::SHL &&
1710 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(0).getOperand(1))) &&
1711 (CSD->getValue() == 2 || CSD->getValue() == 3))
1712 {
1713 bool use4 = CSD->getValue() == 2;
1714 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1715 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1716 BuildMI(BB, isAdd?(use4?Alpha::S4ADDL:Alpha::S8ADDL):(use4?Alpha::S4SUBL:Alpha::S8SUBL),
1717 2,Result).addReg(Tmp1).addReg(Tmp2);
1718 }
1719 else if(isAdd && N.getOperand(0).getOperand(1).getOpcode() == ISD::SHL &&
1720 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1).getOperand(1))) &&
1721 (CSD->getValue() == 2 || CSD->getValue() == 3))
1722 {
1723 bool use4 = CSD->getValue() == 2;
1724 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1725 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1726 BuildMI(BB, use4?Alpha::S4ADDL:Alpha::S8ADDL, 2,Result).addReg(Tmp1).addReg(Tmp2);
1727 }
1728 else if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001729 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
1730 { //Normal imm add/sub
1731 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001732 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001733 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
1734 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001735 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001736 else
1737 { //Normal add/sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001738 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULL : Alpha::SUBL);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001739 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001740 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001741 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1742 }
1743 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001744 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001745 default: break; //Fall Though;
1746 }
1747 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001748 Tmp1 = SelectExpr(N.getOperand(0));
1749 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001750 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001751 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001752 {
1753 default:
1754 Node->dump();
1755 assert(0 && "Sign Extend InReg not there yet");
1756 break;
1757 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001758 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001759 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001760 break;
1761 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001762 case MVT::i16:
1763 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
1764 break;
1765 case MVT::i8:
1766 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
1767 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +00001768 case MVT::i1:
1769 Tmp2 = MakeReg(MVT::i64);
1770 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001771 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +00001772 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001773 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001774 return Result;
1775 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001776
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001777 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001778 {
1779 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1780 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
Andrew Lenharth694c2982005-06-26 23:01:11 +00001781 bool isConst = false;
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001782 int dir;
Misha Brukman7847fca2005-04-22 17:54:37 +00001783
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001784 //Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001785 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001786 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth694c2982005-06-26 23:01:11 +00001787 isConst = true;
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001788
1789 switch (SetCC->getCondition()) {
1790 default: Node->dump(); assert(0 && "Unknown integer comparison!");
Andrew Lenharth694c2982005-06-26 23:01:11 +00001791 case ISD::SETEQ:
1792 Opc = isConst ? Alpha::CMPEQi : Alpha::CMPEQ; dir=1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001793 case ISD::SETLT:
Andrew Lenharth694c2982005-06-26 23:01:11 +00001794 Opc = isConst ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001795 case ISD::SETLE:
Andrew Lenharth694c2982005-06-26 23:01:11 +00001796 Opc = isConst ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
1797 case ISD::SETGT: Opc = Alpha::CMPLT; dir = 2; break;
1798 case ISD::SETGE: Opc = Alpha::CMPLE; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001799 case ISD::SETULT:
Andrew Lenharth694c2982005-06-26 23:01:11 +00001800 Opc = isConst ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
1801 case ISD::SETUGT: Opc = Alpha::CMPULT; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001802 case ISD::SETULE:
Andrew Lenharth694c2982005-06-26 23:01:11 +00001803 Opc = isConst ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
1804 case ISD::SETUGE: Opc = Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001805 case ISD::SETNE: {//Handle this one special
1806 //std::cerr << "Alpha does not have a setne.\n";
1807 //abort();
1808 Tmp1 = SelectExpr(N.getOperand(0));
1809 Tmp2 = SelectExpr(N.getOperand(1));
1810 Tmp3 = MakeReg(MVT::i64);
1811 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001812 //Remeber we have the Inv for this CC
1813 CCInvMap[N] = Tmp3;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001814 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001815 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001816 return Result;
1817 }
1818 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001819 if (dir == 1) {
1820 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth694c2982005-06-26 23:01:11 +00001821 if (isConst) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001822 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1823 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1824 } else {
1825 Tmp2 = SelectExpr(N.getOperand(1));
1826 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1827 }
Andrew Lenharth694c2982005-06-26 23:01:11 +00001828 } else { //if (dir == 2) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001829 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth694c2982005-06-26 23:01:11 +00001830 Tmp2 = SelectExpr(N.getOperand(0));
1831 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001832 }
1833 } else {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001834 //do the comparison
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001835 Tmp1 = MakeReg(MVT::f64);
1836 bool inv = SelectFPSetCC(N, Tmp1);
1837
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001838 //now arrange for Result (int) to have a 1 or 0
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001839 Tmp2 = MakeReg(MVT::i64);
1840 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001841 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001842 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001843 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001844 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001845 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001846 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001847
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001848 case ISD::CopyFromReg:
1849 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001850 ++count_ins;
1851
Andrew Lenharth40831c52005-01-28 06:57:18 +00001852 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001853 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001854 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001855 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001856 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001857
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001858 SDOperand Chain = N.getOperand(0);
1859
1860 Select(Chain);
1861 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1862 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1863 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1864 return Result;
1865 }
1866
Misha Brukman4633f1c2005-04-21 23:13:11 +00001867 //Most of the plain arithmetic and logic share the same form, and the same
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001868 //constant immediate test
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001869 case ISD::XOR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001870 //Match Not
1871 if (N.getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001872 cast<ConstantSDNode>(N.getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001873 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001874 Tmp1 = SelectExpr(N.getOperand(0));
1875 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1876 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001877 }
1878 //Fall through
1879 case ISD::AND:
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001880 //handle zap
1881 if (opcode == ISD::AND && N.getOperand(1).getOpcode() == ISD::Constant)
1882 {
1883 uint64_t k = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1884 unsigned int build = 0;
1885 for(int i = 0; i < 8; ++i)
1886 {
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001887 if ((k & 0x00FF) == 0x00FF)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001888 build |= 1 << i;
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001889 else if ((k & 0x00FF) != 0)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001890 { build = 0; break; }
1891 k >>= 8;
1892 }
1893 if (build)
1894 {
1895 Tmp1 = SelectExpr(N.getOperand(0));
1896 BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1897 return Result;
1898 }
1899 }
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001900 case ISD::OR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001901 //Check operand(0) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001902 if (N.getOperand(0).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001903 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001904 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001905 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001906 switch(opcode) {
1907 case ISD::AND: Opc = Alpha::BIC; break;
1908 case ISD::OR: Opc = Alpha::ORNOT; break;
1909 case ISD::XOR: Opc = Alpha::EQV; break;
1910 }
1911 Tmp1 = SelectExpr(N.getOperand(1));
1912 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1913 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1914 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001915 }
1916 //Check operand(1) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001917 if (N.getOperand(1).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001918 N.getOperand(1).getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001919 cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001920 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001921 switch(opcode) {
1922 case ISD::AND: Opc = Alpha::BIC; break;
1923 case ISD::OR: Opc = Alpha::ORNOT; break;
1924 case ISD::XOR: Opc = Alpha::EQV; break;
1925 }
1926 Tmp1 = SelectExpr(N.getOperand(0));
1927 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1928 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1929 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001930 }
1931 //Fall through
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001932 case ISD::SHL:
1933 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001934 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001935 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001936 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1937 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001938 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001939 {
1940 switch(opcode) {
1941 case ISD::AND: Opc = Alpha::ANDi; break;
1942 case ISD::OR: Opc = Alpha::BISi; break;
1943 case ISD::XOR: Opc = Alpha::XORi; break;
1944 case ISD::SHL: Opc = Alpha::SLi; break;
1945 case ISD::SRL: Opc = Alpha::SRLi; break;
1946 case ISD::SRA: Opc = Alpha::SRAi; break;
1947 case ISD::MUL: Opc = Alpha::MULQi; break;
1948 };
1949 Tmp1 = SelectExpr(N.getOperand(0));
1950 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1951 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1952 } else {
1953 switch(opcode) {
1954 case ISD::AND: Opc = Alpha::AND; break;
1955 case ISD::OR: Opc = Alpha::BIS; break;
1956 case ISD::XOR: Opc = Alpha::XOR; break;
1957 case ISD::SHL: Opc = Alpha::SL; break;
1958 case ISD::SRL: Opc = Alpha::SRL; break;
1959 case ISD::SRA: Opc = Alpha::SRA; break;
1960 case ISD::MUL: Opc = Alpha::MULQ; break;
1961 };
1962 Tmp1 = SelectExpr(N.getOperand(0));
1963 Tmp2 = SelectExpr(N.getOperand(1));
1964 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1965 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001966 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001967
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001968 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001969 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001970 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001971 bool isAdd = opcode == ISD::ADD;
1972
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001973 //first check for Scaled Adds and Subs!
1974 //Valid for add and sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001975 ConstantSDNode* CSD = NULL;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001976 if(N.getOperand(0).getOpcode() == ISD::SHL &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001977 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
1978 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001979 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001980 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001981 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001982 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) && CSD->getValue() <= 255)
1983 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1984 2, Result).addReg(Tmp2).addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001985 else {
1986 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001987 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1988 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001989 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001990 }
1991 //Position prevents subs
Andrew Lenharth273a1f92005-04-07 14:18:13 +00001992 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001993 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) &&
1994 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001995 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001996 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001997 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001998 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(0))) && CSD->getValue() <= 255)
1999 BuildMI(BB, use4?Alpha::S4ADDQi:Alpha::S8ADDQi, 2, Result).addReg(Tmp2)
2000 .addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00002001 else {
2002 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002003 BuildMI(BB, use4?Alpha::S4ADDQ:Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00002004 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002005 }
2006 //small addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002007 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
2008 CSD->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002009 { //Normal imm add/sub
2010 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
2011 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002012 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CSD->getValue());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002013 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002014 //larger addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002015 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
2016 CSD->getSignExtended() <= 32767 &&
2017 CSD->getSignExtended() >= -32767)
Andrew Lenharth74d00d82005-03-02 17:23:03 +00002018 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002019 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002020 Tmp2 = (long)CSD->getSignExtended();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002021 if (!isAdd)
2022 Tmp2 = -Tmp2;
2023 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002024 }
2025 //give up and do the operation
2026 else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002027 //Normal add/sub
2028 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
2029 Tmp1 = SelectExpr(N.getOperand(0));
2030 Tmp2 = SelectExpr(N.getOperand(1));
2031 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2032 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002033 return Result;
2034 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002035
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00002036 case ISD::SDIV:
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002037 {
Andrew Lenhartha565c272005-04-06 22:03:13 +00002038 ConstantSDNode* CSD;
2039 //check if we can convert into a shift!
2040 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
2041 (int64_t)CSD->getSignExtended() != 0 &&
2042 ExactLog2(abs((int64_t)CSD->getSignExtended())) != 0)
2043 {
2044 unsigned k = ExactLog2(abs(CSD->getSignExtended()));
2045 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenhartha565c272005-04-06 22:03:13 +00002046 if (k == 1)
2047 Tmp2 = Tmp1;
2048 else
2049 {
2050 Tmp2 = MakeReg(MVT::i64);
2051 BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
2052 }
2053 Tmp3 = MakeReg(MVT::i64);
2054 BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
2055 unsigned Tmp4 = MakeReg(MVT::i64);
2056 BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
2057 if ((int64_t)CSD->getSignExtended() > 0)
2058 BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
2059 else
2060 {
2061 unsigned Tmp5 = MakeReg(MVT::i64);
2062 BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
2063 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
2064 }
2065 return Result;
2066 }
2067 }
2068 //Else fall through
2069
2070 case ISD::UDIV:
2071 {
2072 ConstantSDNode* CSD;
2073 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
2074 ((int64_t)CSD->getSignExtended() >= 2 ||
2075 (int64_t)CSD->getSignExtended() <= -2))
2076 {
2077 // If this is a divide by constant, we can emit code using some magic
2078 // constants to implement it as a multiply instead.
2079 ExprMap.erase(N);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002080 if (opcode == ISD::SDIV)
Andrew Lenhartha565c272005-04-06 22:03:13 +00002081 return SelectExpr(BuildSDIVSequence(N));
2082 else
2083 return SelectExpr(BuildUDIVSequence(N));
2084 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002085 }
2086 //else fall though
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002087 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +00002088 case ISD::SREM:
Misha Brukman4633f1c2005-04-21 23:13:11 +00002089 //FIXME: alpha really doesn't support any of these operations,
Andrew Lenharth40831c52005-01-28 06:57:18 +00002090 // the ops are expanded into special library calls with
2091 // special calling conventions
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002092 //Restore GP because it is a call after all...
Andrew Lenharth40831c52005-01-28 06:57:18 +00002093 switch(opcode) {
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00002094 case ISD::UREM: Opc = Alpha::REMQU; break;
2095 case ISD::SREM: Opc = Alpha::REMQ; break;
2096 case ISD::UDIV: Opc = Alpha::DIVQU; break;
2097 case ISD::SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002098 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002099 Tmp1 = SelectExpr(N.getOperand(0));
2100 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth33819132005-03-04 20:09:23 +00002101 //set up regs explicitly (helps Reg alloc)
2102 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002103 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00002104 AlphaLowering.restoreGP(BB);
Andrew Lenharth33819132005-03-04 20:09:23 +00002105 BuildMI(BB, Opc, 2).addReg(Alpha::R24).addReg(Alpha::R25);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002106 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002107 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002108
Andrew Lenharthe76797c2005-02-01 20:40:27 +00002109 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002110 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002111 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002112 assert (DestType == MVT::i64 && "only quads can be loaded to");
2113 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00002114 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002115 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002116 if (SrcType == MVT::f32)
Misha Brukman7847fca2005-04-22 17:54:37 +00002117 {
2118 Tmp2 = MakeReg(MVT::f64);
2119 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
2120 Tmp1 = Tmp2;
2121 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002122 Tmp2 = MakeReg(MVT::f64);
2123 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00002124 MoveFP2Int(Tmp2, Result, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002125
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002126 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002127 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002128
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002129 case ISD::SELECT:
2130 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00002131 //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 +00002132 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002133 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2134 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002135 // Get the condition into the zero flag.
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002136 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002137
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002138 SDOperand CC = N.getOperand(0);
2139 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
2140
Misha Brukman4633f1c2005-04-21 23:13:11 +00002141 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002142 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
2143 { //FP Setcc -> Int Select
Misha Brukman7847fca2005-04-22 17:54:37 +00002144 Tmp1 = MakeReg(MVT::f64);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002145 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2146 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Misha Brukman7847fca2005-04-22 17:54:37 +00002147 bool inv = SelectFPSetCC(CC, Tmp1);
2148 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
2149 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
2150 return Result;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002151 }
2152 if (CC.getOpcode() == ISD::SETCC) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002153 //Int SetCC -> Select
2154 //Dropping the CC is only useful if we are comparing to 0
2155 if((SetCC->getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth694c2982005-06-26 23:01:11 +00002156 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0))
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002157 {
2158 //figure out a few things
Andrew Lenharth694c2982005-06-26 23:01:11 +00002159 bool useImm = N.getOperand(2).getOpcode() == ISD::Constant &&
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002160 cast<ConstantSDNode>(N.getOperand(2))->getValue() <= 255;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002161
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002162 //Fix up CC
2163 ISD::CondCode cCode= SetCC->getCondition();
Andrew Lenharth694c2982005-06-26 23:01:11 +00002164 if (useImm) //Invert sense to get Imm field right
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002165 cCode = ISD::getSetCCInverse(cCode, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002166
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002167 //Choose the CMOV
2168 switch (cCode) {
2169 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
2170 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
2171 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
2172 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
2173 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
2174 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
2175 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
2176 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2177 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break; //Technically you could have this CC
2178 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
2179 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2180 }
Andrew Lenharth694c2982005-06-26 23:01:11 +00002181 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002182
Andrew Lenharth694c2982005-06-26 23:01:11 +00002183 if (useImm) {
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002184 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
2185 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
Misha Brukman7847fca2005-04-22 17:54:37 +00002186 .addImm(cast<ConstantSDNode>(N.getOperand(2))->getValue())
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002187 .addReg(Tmp1);
2188 } else {
2189 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2190 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2191 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
2192 }
2193 return Result;
2194 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002195 //Otherwise, fall though
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002196 }
2197 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002198 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2199 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthe76797c2005-02-01 20:40:27 +00002200 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002201
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002202 return Result;
2203 }
2204
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002205 case ISD::Constant:
2206 {
Andrew Lenharthc0513832005-03-29 19:24:04 +00002207 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002208 if (val <= IMM_HIGH && val >= IMM_LOW) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002209 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002210 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002211 else if (val <= (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT &&
2212 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
2213 Tmp1 = MakeReg(MVT::i64);
2214 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val)).addReg(Alpha::R31);
2215 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002216 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002217 else {
2218 MachineConstantPool *CP = BB->getParent()->getConstantPool();
2219 ConstantUInt *C = ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
2220 unsigned CPI = CP->getConstantPoolIndex(C);
2221 AlphaLowering.restoreGP(BB);
2222 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CPI);
2223 }
2224 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002225 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002226 }
2227
2228 return 0;
2229}
2230
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002231void AlphaISel::Select(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002232 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00002233 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002234
Nate Begeman85fdeb22005-03-24 04:39:54 +00002235 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002236 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002237
2238 SDNode *Node = N.Val;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002239
Andrew Lenharth760270d2005-02-07 23:02:23 +00002240 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002241
2242 default:
2243 Node->dump(); std::cerr << "\n";
2244 assert(0 && "Node not handled yet!");
2245
2246 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00002247 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002248 return;
2249 }
2250
2251 case ISD::BR: {
2252 MachineBasicBlock *Dest =
2253 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2254
2255 Select(N.getOperand(0));
2256 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
2257 return;
2258 }
2259
2260 case ISD::ImplicitDef:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002261 ++count_ins;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002262 Select(N.getOperand(0));
2263 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
2264 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002265
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002266 case ISD::EntryToken: return; // Noop
2267
2268 case ISD::TokenFactor:
2269 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2270 Select(Node->getOperand(i));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002271
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002272 //N.Val->dump(); std::cerr << "\n";
2273 //assert(0 && "Node not handled yet!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00002274
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002275 return;
2276
2277 case ISD::CopyToReg:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002278 ++count_outs;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002279 Select(N.getOperand(0));
2280 Tmp1 = SelectExpr(N.getOperand(1));
2281 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002282
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002283 if (Tmp1 != Tmp2) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002284 if (N.getOperand(1).getValueType() == MVT::f64 ||
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002285 N.getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00002286 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2287 else
2288 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002289 }
2290 return;
2291
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002292 case ISD::RET:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002293 ++count_outs;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002294 switch (N.getNumOperands()) {
2295 default:
2296 std::cerr << N.getNumOperands() << "\n";
2297 for (unsigned i = 0; i < N.getNumOperands(); ++i)
2298 std::cerr << N.getOperand(i).getValueType() << "\n";
2299 Node->dump();
2300 assert(0 && "Unknown return instruction!");
2301 case 2:
2302 Select(N.getOperand(0));
2303 Tmp1 = SelectExpr(N.getOperand(1));
2304 switch (N.getOperand(1).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002305 default: Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002306 assert(0 && "All other types should have been promoted!!");
2307 case MVT::f64:
2308 case MVT::f32:
2309 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
2310 break;
2311 case MVT::i32:
2312 case MVT::i64:
2313 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
2314 break;
2315 }
2316 break;
2317 case 1:
2318 Select(N.getOperand(0));
2319 break;
2320 }
Andrew Lenharth3b918072005-06-27 15:36:48 +00002321 BuildMI(BB, Alpha::RET, 1, Alpha::R31).addReg(AlphaLowering.getRA()); // Just emit a 'ret' instruction
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002322 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002323
Misha Brukman4633f1c2005-04-21 23:13:11 +00002324 case ISD::TRUNCSTORE:
2325 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002326 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002327 SDOperand Chain = N.getOperand(0);
2328 SDOperand Value = N.getOperand(1);
2329 SDOperand Address = N.getOperand(2);
2330 Select(Chain);
2331
2332 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00002333
2334 if (opcode == ISD::STORE) {
2335 switch(Value.getValueType()) {
2336 default: assert(0 && "unknown Type in store");
2337 case MVT::i64: Opc = Alpha::STQ; break;
2338 case MVT::f64: Opc = Alpha::STT; break;
2339 case MVT::f32: Opc = Alpha::STS; break;
2340 }
2341 } else { //ISD::TRUNCSTORE
2342 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2343 default: assert(0 && "unknown Type in store");
2344 case MVT::i1: //FIXME: DAG does not promote this load
2345 case MVT::i8: Opc = Alpha::STB; break;
2346 case MVT::i16: Opc = Alpha::STW; break;
2347 case MVT::i32: Opc = Alpha::STL; break;
2348 }
Andrew Lenharth65838902005-02-06 16:22:15 +00002349 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00002350
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002351 if (EnableAlphaLSMark)
2352 {
2353 int i = getValueOffset(dyn_cast<SrcValueSDNode>(N.getOperand(3))->getValue());
2354 int j = getFunctionOffset(BB->getParent()->getFunction());
2355 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
2356 }
2357
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002358 if (Address.getOpcode() == ISD::GlobalAddress)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002359 {
2360 AlphaLowering.restoreGP(BB);
2361 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002362 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002363 BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
2364 }
Andrew Lenharth05380342005-02-07 05:07:00 +00002365 else if(Address.getOpcode() == ISD::FrameIndex)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002366 {
Andrew Lenharth032f2352005-02-22 21:59:48 +00002367 BuildMI(BB, Opc, 3).addReg(Tmp1)
2368 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
2369 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002370 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002371 else
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002372 {
2373 long offset;
2374 SelectAddr(Address, Tmp2, offset);
2375 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2376 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002377 return;
2378 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002379
2380 case ISD::EXTLOAD:
2381 case ISD::SEXTLOAD:
2382 case ISD::ZEXTLOAD:
2383 case ISD::LOAD:
2384 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002385 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002386 case ISD::CALL:
Andrew Lenharth032f2352005-02-22 21:59:48 +00002387 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002388 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002389 SelectExpr(N);
2390 return;
2391
Chris Lattner16cd04d2005-05-12 23:24:06 +00002392 case ISD::CALLSEQ_START:
2393 case ISD::CALLSEQ_END:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002394 Select(N.getOperand(0));
2395 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002396
Chris Lattner16cd04d2005-05-12 23:24:06 +00002397 Opc = N.getOpcode() == ISD::CALLSEQ_START ? Alpha::ADJUSTSTACKDOWN :
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002398 Alpha::ADJUSTSTACKUP;
2399 BuildMI(BB, Opc, 1).addImm(Tmp1);
2400 return;
Andrew Lenharth95762122005-03-31 21:24:06 +00002401
2402 case ISD::PCMARKER:
2403 Select(N.getOperand(0)); //Chain
2404 BuildMI(BB, Alpha::PCLABEL, 2).addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
2405 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002406 }
2407 assert(0 && "Should not be reached!");
2408}
2409
2410
2411/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
2412/// into a machine code representation using pattern matching and a machine
2413/// description file.
2414///
2415FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002416 return new AlphaISel(TM);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002417}
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002418