blob: c5060d39eb0a299787869a1b2d13945113545ef2 [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}
Andrew Lenharthfe895e32005-06-27 17:15:36 +0000855static unsigned GetRelVersion(unsigned opcode)
856{
857 switch (opcode) {
858 default: assert(0 && "unknown load or store"); return 0;
859 case Alpha::LDQ: return Alpha::LDQr;
860 case Alpha::LDS: return Alpha::LDSr;
861 case Alpha::LDT: return Alpha::LDTr;
862 case Alpha::LDL: return Alpha::LDLr;
863 case Alpha::LDBU: return Alpha::LDBUr;
864 case Alpha::LDWU: return Alpha::LDWUr;
865 }
866}
Andrew Lenharth65838902005-02-06 16:22:15 +0000867
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000868void AlphaISel::MoveFP2Int(unsigned src, unsigned dst, bool isDouble)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000869{
870 unsigned Opc;
871 if (EnableAlphaFTOI) {
872 Opc = isDouble ? Alpha::FTOIT : Alpha::FTOIS;
873 BuildMI(BB, Opc, 1, dst).addReg(src);
874 } else {
875 //The hard way:
876 // Spill the integer to memory and reload it from there.
877 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
878 MachineFunction *F = BB->getParent();
879 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
880
881 Opc = isDouble ? Alpha::STT : Alpha::STS;
882 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
883 Opc = isDouble ? Alpha::LDQ : Alpha::LDL;
884 BuildMI(BB, Alpha::LDQ, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
885 }
886}
887
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000888void AlphaISel::MoveInt2FP(unsigned src, unsigned dst, bool isDouble)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000889{
890 unsigned Opc;
891 if (EnableAlphaFTOI) {
892 Opc = isDouble?Alpha::ITOFT:Alpha::ITOFS;
893 BuildMI(BB, Opc, 1, dst).addReg(src);
894 } else {
895 //The hard way:
896 // Spill the integer to memory and reload it from there.
897 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
898 MachineFunction *F = BB->getParent();
899 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
900
901 Opc = isDouble ? Alpha::STQ : Alpha::STL;
902 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
903 Opc = isDouble ? Alpha::LDT : Alpha::LDS;
904 BuildMI(BB, Opc, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
905 }
906}
907
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000908bool AlphaISel::SelectFPSetCC(SDOperand N, unsigned dst)
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000909{
910 SDNode *Node = N.Val;
911 unsigned Opc, Tmp1, Tmp2, Tmp3;
912 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node);
913
914 //assert(SetCC->getOperand(0).getValueType() != MVT::f32 && "SetCC f32 should have been promoted");
915 bool rev = false;
916 bool inv = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000917
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000918 switch (SetCC->getCondition()) {
919 default: Node->dump(); assert(0 && "Unknown FP comparison!");
920 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
921 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
922 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
923 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
924 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
925 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
926 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000927
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000928 ConstantFPSDNode *CN;
929 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
930 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
931 Tmp1 = Alpha::F31;
932 else
933 Tmp1 = SelectExpr(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000934
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000935 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
936 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
937 Tmp2 = Alpha::F31;
938 else
Chris Lattner9c9183a2005-04-30 04:44:07 +0000939 Tmp2 = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000940
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000941 //Can only compare doubles, and dag won't promote for me
942 if (SetCC->getOperand(0).getValueType() == MVT::f32)
943 {
944 //assert(0 && "Setcc On float?\n");
945 std::cerr << "Setcc on float!\n";
946 Tmp3 = MakeReg(MVT::f64);
947 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
948 Tmp1 = Tmp3;
949 }
950 if (SetCC->getOperand(1).getValueType() == MVT::f32)
951 {
952 //assert (0 && "Setcc On float?\n");
953 std::cerr << "Setcc on float!\n";
954 Tmp3 = MakeReg(MVT::f64);
955 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp2);
956 Tmp2 = Tmp3;
957 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000958
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000959 if (rev) std::swap(Tmp1, Tmp2);
960 //do the comparison
961 BuildMI(BB, Opc, 2, dst).addReg(Tmp1).addReg(Tmp2);
962 return inv;
963}
964
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000965//Check to see if the load is a constant offset from a base register
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000966void AlphaISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000967{
968 unsigned opcode = N.getOpcode();
Andrew Lenharth694c2982005-06-26 23:01:11 +0000969 if (opcode == ISD::ADD && N.getOperand(1).getOpcode() == ISD::Constant &&
970 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
971 { //Normal imm add
972 Reg = SelectExpr(N.getOperand(0));
973 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
974 return;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000975 }
976 Reg = SelectExpr(N);
977 offset = 0;
978 return;
979}
980
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000981void AlphaISel::SelectBranchCC(SDOperand N)
Andrew Lenharth445171a2005-02-08 00:40:03 +0000982{
983 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000984 MachineBasicBlock *Dest =
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000985 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
986 unsigned Opc = Alpha::WTF;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000987
Andrew Lenharth445171a2005-02-08 00:40:03 +0000988 Select(N.getOperand(0)); //chain
989 SDOperand CC = N.getOperand(1);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000990
Andrew Lenharth445171a2005-02-08 00:40:03 +0000991 if (CC.getOpcode() == ISD::SETCC)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000992 {
993 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
994 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
995 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharth09552bf2005-06-08 18:02:21 +0000996 bool RightZero = SetCC->getOperand(1).getOpcode() == ISD::Constant &&
997 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000998 bool isNE = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000999
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001000 //Fix up CC
1001 ISD::CondCode cCode= SetCC->getCondition();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001002
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001003 if(cCode == ISD::SETNE)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001004 isNE = true;
Andrew Lenharth445171a2005-02-08 00:40:03 +00001005
Andrew Lenharth694c2982005-06-26 23:01:11 +00001006 if (RightZero) {
Andrew Lenharth09552bf2005-06-08 18:02:21 +00001007 switch (cCode) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001008 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
1009 case ISD::SETEQ: Opc = Alpha::BEQ; break;
1010 case ISD::SETLT: Opc = Alpha::BLT; break;
1011 case ISD::SETLE: Opc = Alpha::BLE; break;
1012 case ISD::SETGT: Opc = Alpha::BGT; break;
1013 case ISD::SETGE: Opc = Alpha::BGE; break;
1014 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
1015 case ISD::SETUGT: Opc = Alpha::BNE; break;
1016 case ISD::SETULE: Opc = Alpha::BEQ; break; //Technically you could have this CC
1017 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
1018 case ISD::SETNE: Opc = Alpha::BNE; break;
1019 }
Andrew Lenharth694c2982005-06-26 23:01:11 +00001020 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001021 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
1022 return;
1023 } else {
1024 unsigned Tmp1 = SelectExpr(CC);
1025 if (isNE)
1026 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
1027 else
1028 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001029 return;
1030 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001031 } else { //FP
1032 //Any comparison between 2 values should be codegened as an folded branch, as moving
1033 //CC to the integer register is very expensive
1034 //for a cmp b: c = a - b;
1035 //a = b: c = 0
1036 //a < b: c < 0
1037 //a > b: c > 0
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001038
1039 bool invTest = false;
1040 unsigned Tmp3;
1041
1042 ConstantFPSDNode *CN;
1043 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
1044 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1045 Tmp3 = SelectExpr(SetCC->getOperand(0));
1046 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
1047 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1048 {
1049 Tmp3 = SelectExpr(SetCC->getOperand(1));
1050 invTest = true;
1051 }
1052 else
1053 {
1054 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
1055 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
1056 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
1057 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1058 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1059 .addReg(Tmp1).addReg(Tmp2);
1060 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001061
1062 switch (SetCC->getCondition()) {
1063 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001064 case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
1065 case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
1066 case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
1067 case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
1068 case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
1069 case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001070 }
1071 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001072 return;
1073 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001074 abort(); //Should never be reached
1075 } else {
1076 //Giveup and do the stupid thing
1077 unsigned Tmp1 = SelectExpr(CC);
1078 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
1079 return;
1080 }
Andrew Lenharth445171a2005-02-08 00:40:03 +00001081 abort(); //Should never be reached
1082}
1083
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001084unsigned AlphaISel::SelectExprFP(SDOperand N, unsigned Result)
Andrew Lenharth40831c52005-01-28 06:57:18 +00001085{
1086 unsigned Tmp1, Tmp2, Tmp3;
1087 unsigned Opc = 0;
1088 SDNode *Node = N.Val;
1089 MVT::ValueType DestType = N.getValueType();
1090 unsigned opcode = N.getOpcode();
1091
1092 switch (opcode) {
1093 default:
1094 Node->dump();
1095 assert(0 && "Node not handled!\n");
Andrew Lenharth2c594352005-01-29 15:42:07 +00001096
Andrew Lenharth7332f3e2005-04-02 19:11:07 +00001097 case ISD::UNDEF: {
1098 BuildMI(BB, Alpha::IDEF, 0, Result);
1099 return Result;
1100 }
1101
Andrew Lenharth30b46d42005-04-02 19:04:58 +00001102 case ISD::FNEG:
1103 if(ISD::FABS == N.getOperand(0).getOpcode())
1104 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001105 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1106 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +00001107 } else {
Misha Brukman7847fca2005-04-22 17:54:37 +00001108 Tmp1 = SelectExpr(N.getOperand(0));
1109 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +00001110 }
1111 return Result;
1112
1113 case ISD::FABS:
1114 Tmp1 = SelectExpr(N.getOperand(0));
1115 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
1116 return Result;
1117
Andrew Lenharth9818c052005-02-05 13:19:12 +00001118 case ISD::SELECT:
1119 {
Andrew Lenharth45859692005-03-03 21:47:53 +00001120 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1121 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
1122 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
1123
1124 SDOperand CC = N.getOperand(0);
1125 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
1126
Misha Brukman4633f1c2005-04-21 23:13:11 +00001127 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth45859692005-03-03 21:47:53 +00001128 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
1129 { //FP Setcc -> Select yay!
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001130
1131
Andrew Lenharth45859692005-03-03 21:47:53 +00001132 //for a cmp b: c = a - b;
1133 //a = b: c = 0
1134 //a < b: c < 0
1135 //a > b: c > 0
Misha Brukman4633f1c2005-04-21 23:13:11 +00001136
Andrew Lenharth45859692005-03-03 21:47:53 +00001137 bool invTest = false;
1138 unsigned Tmp3;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001139
Andrew Lenharth45859692005-03-03 21:47:53 +00001140 ConstantFPSDNode *CN;
1141 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
1142 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1143 Tmp3 = SelectExpr(SetCC->getOperand(0));
1144 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
1145 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1146 {
1147 Tmp3 = SelectExpr(SetCC->getOperand(1));
1148 invTest = true;
1149 }
1150 else
1151 {
1152 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
1153 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
1154 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
1155 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1156 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1157 .addReg(Tmp1).addReg(Tmp2);
1158 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001159
Andrew Lenharth45859692005-03-03 21:47:53 +00001160 switch (SetCC->getCondition()) {
1161 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
1162 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNE : Alpha::FCMOVEQ; break;
1163 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGT : Alpha::FCMOVLT; break;
1164 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGE : Alpha::FCMOVLE; break;
1165 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLT : Alpha::FCMOVGT; break;
1166 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLE : Alpha::FCMOVGE; break;
1167 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQ : Alpha::FCMOVNE; break;
1168 }
Andrew Lenharth33819132005-03-04 20:09:23 +00001169 BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
Andrew Lenharth45859692005-03-03 21:47:53 +00001170 return Result;
1171 }
1172 else
1173 {
1174 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001175 BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV).addReg(Tmp1);
1176// // Spill the cond to memory and reload it from there.
1177// unsigned Tmp4 = MakeReg(MVT::f64);
1178// MoveIntFP(Tmp1, Tmp4, true);
1179// //now ideally, we don't have to do anything to the flag...
1180// // Get the condition into the zero flag.
1181// BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
Andrew Lenharth45859692005-03-03 21:47:53 +00001182 return Result;
1183 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001184 }
1185
Andrew Lenharthc1faced2005-02-01 01:37:24 +00001186 case ISD::FP_ROUND:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001187 assert (DestType == MVT::f32 &&
1188 N.getOperand(0).getValueType() == MVT::f64 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001189 "only f64 to f32 conversion supported here");
Andrew Lenharthc1faced2005-02-01 01:37:24 +00001190 Tmp1 = SelectExpr(N.getOperand(0));
1191 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
1192 return Result;
1193
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001194 case ISD::FP_EXTEND:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001195 assert (DestType == MVT::f64 &&
1196 N.getOperand(0).getValueType() == MVT::f32 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001197 "only f32 to f64 conversion supported here");
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001198 Tmp1 = SelectExpr(N.getOperand(0));
1199 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
1200 return Result;
1201
Andrew Lenharth2c594352005-01-29 15:42:07 +00001202 case ISD::CopyFromReg:
1203 {
1204 // Make sure we generate both values.
1205 if (Result != notIn)
1206 ExprMap[N.getValue(1)] = notIn; // Generate the token
1207 else
1208 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001209
Andrew Lenharth2c594352005-01-29 15:42:07 +00001210 SDOperand Chain = N.getOperand(0);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001211
Andrew Lenharth2c594352005-01-29 15:42:07 +00001212 Select(Chain);
1213 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1214 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1215 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
1216 return Result;
1217 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001218
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001219 case ISD::LOAD:
1220 {
1221 // Make sure we generate both values.
1222 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001223 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001224 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001225 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001226
Andrew Lenharth29219162005-02-07 06:31:44 +00001227 DestType = N.getValue(0).getValueType();
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001228
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001229 SDOperand Chain = N.getOperand(0);
1230 SDOperand Address = N.getOperand(1);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001231 Select(Chain);
Andrew Lenharth65838902005-02-06 16:22:15 +00001232 Opc = DestType == MVT::f64 ? Alpha::LDT : Alpha::LDS;
1233
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +00001234 if (EnableAlphaLSMark)
1235 {
1236 int i = getValueOffset(dyn_cast<SrcValueSDNode>(N.getOperand(2))->getValue());
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001237 int j = getFunctionOffset(BB->getParent()->getFunction());
1238 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +00001239 }
1240
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001241 if (Address.getOpcode() == ISD::GlobalAddress) {
1242 AlphaLowering.restoreGP(BB);
1243 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001244 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001245 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1246 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001247 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001248 AlphaLowering.restoreGP(BB);
Andrew Lenharthfe895e32005-06-27 17:15:36 +00001249 Opc = GetRelVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001250 has_sym = true;
Andrew Lenharthfe895e32005-06-27 17:15:36 +00001251 Tmp1 = MakeReg(MVT::i64);
1252 BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CP->getIndex()).addReg(Alpha::R29);
1253 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CP->getIndex()).addReg(Tmp1);
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001254 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001255 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001256 BuildMI(BB, Opc, 2, Result)
1257 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1258 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001259 } else {
1260 long offset;
1261 SelectAddr(Address, Tmp1, offset);
1262 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1263 }
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001264 return Result;
1265 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001266 case ISD::ConstantFP:
1267 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
1268 if (CN->isExactlyValue(+0.0)) {
1269 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001270 } else if ( CN->isExactlyValue(-0.0)) {
1271 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001272 } else {
1273 abort();
1274 }
1275 }
1276 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001277
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001278 case ISD::SDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001279 case ISD::MUL:
1280 case ISD::ADD:
1281 case ISD::SUB:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001282 switch( opcode ) {
1283 case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS; break;
1284 case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS; break;
1285 case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS; break;
1286 case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS; break;
1287 };
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001288
1289 ConstantFPSDNode *CN;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001290 if (opcode == ISD::SUB
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001291 && (CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
1292 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1293 {
1294 Tmp2 = SelectExpr(N.getOperand(1));
1295 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp2).addReg(Tmp2);
1296 } else {
1297 Tmp1 = SelectExpr(N.getOperand(0));
1298 Tmp2 = SelectExpr(N.getOperand(1));
1299 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1300 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001301 return Result;
1302
Andrew Lenharth2c594352005-01-29 15:42:07 +00001303 case ISD::EXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001304 {
1305 //include a conversion sequence for float loads to double
1306 if (Result != notIn)
1307 ExprMap[N.getValue(1)] = notIn; // Generate the token
1308 else
1309 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001310
Andrew Lenhartha549deb2005-02-07 05:33:15 +00001311 Tmp1 = MakeReg(MVT::f32);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001312
1313 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001314 "EXTLOAD not from f32");
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001315 assert(Node->getValueType(0) == MVT::f64 && "EXTLOAD not to f64");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001316
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001317 SDOperand Chain = N.getOperand(0);
1318 SDOperand Address = N.getOperand(1);
1319 Select(Chain);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001320
Andrew Lenharthb72bcbb2005-06-27 16:40:26 +00001321 if (EnableAlphaLSMark)
1322 {
1323 int i = getValueOffset(dyn_cast<SrcValueSDNode>(N.getOperand(2))->getValue());
1324 int j = getFunctionOffset(BB->getParent()->getFunction());
1325 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
1326 }
1327
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001328 if (Address.getOpcode() == ISD::GlobalAddress) {
1329 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001330 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001331 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1332 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001333 else if (ConstantPoolSDNode *CP =
1334 dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001335 {
1336 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001337 has_sym = true;
Andrew Lenharthfe895e32005-06-27 17:15:36 +00001338 Tmp2 = MakeReg(MVT::i64);
1339 BuildMI(BB, Alpha::LDAHr, 2, Tmp2).addConstantPoolIndex(CP->getIndex()).addReg(Alpha::R29);
1340 BuildMI(BB, Alpha::LDSr, 2, Tmp1).addConstantPoolIndex(CP->getIndex()).addReg(Tmp2);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001341 }
1342 else if(Address.getOpcode() == ISD::FrameIndex) {
1343 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
Andrew Lenharth032f2352005-02-22 21:59:48 +00001344 BuildMI(BB, Alpha::LDS, 2, Tmp1)
1345 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1346 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001347 } else {
1348 long offset;
1349 SelectAddr(Address, Tmp2, offset);
1350 BuildMI(BB, Alpha::LDS, 1, Tmp1).addImm(offset).addReg(Tmp2);
1351 }
Andrew Lenharth29219162005-02-07 06:31:44 +00001352 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001353 return Result;
1354 }
Andrew Lenharth2c594352005-01-29 15:42:07 +00001355
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001356 case ISD::SINT_TO_FP:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001357 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001358 assert (N.getOperand(0).getValueType() == MVT::i64
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001359 && "only quads can be loaded from");
Andrew Lenharth40831c52005-01-28 06:57:18 +00001360 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001361 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001362 MoveInt2FP(Tmp1, Tmp2, true);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001363 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
1364 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001365 return Result;
1366 }
1367 }
1368 assert(0 && "should not get here");
1369 return 0;
1370}
1371
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001372unsigned AlphaISel::SelectExpr(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001373 unsigned Result;
Andrew Lenharth2966e842005-04-07 18:15:28 +00001374 unsigned Tmp1, Tmp2 = 0, Tmp3;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001375 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001376 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001377
1378 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001379 MVT::ValueType DestType = N.getValueType();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001380
1381 unsigned &Reg = ExprMap[N];
1382 if (Reg) return Reg;
1383
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001384 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::TAILCALL)
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001385 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001386 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001387 else {
1388 // If this is a call instruction, make sure to prepare ALL of the result
1389 // values as well as the chain.
1390 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001391 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001392 else {
1393 Result = MakeReg(Node->getValueType(0));
1394 ExprMap[N.getValue(0)] = Result;
1395 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1396 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001397 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001398 }
1399 }
1400
Andrew Lenharth50d91d72005-04-30 14:19:13 +00001401 if ((DestType == MVT::f64 || DestType == MVT::f32 ||
1402 (
1403 (opcode == ISD::LOAD || opcode == ISD::CopyFromReg ||
1404 opcode == ISD::EXTLOAD) &&
1405 (N.getValue(0).getValueType() == MVT::f32 ||
1406 N.getValue(0).getValueType() == MVT::f64)
1407 ))
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001408 && opcode != ISD::CALL && opcode != ISD::TAILCALL
Andrew Lenharth06342c32005-02-07 06:21:37 +00001409 )
Andrew Lenharth40831c52005-01-28 06:57:18 +00001410 return SelectExprFP(N, Result);
1411
1412 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001413 default:
1414 Node->dump();
1415 assert(0 && "Node not handled!\n");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001416
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001417 case ISD::CTPOP:
1418 case ISD::CTTZ:
1419 case ISD::CTLZ:
1420 Opc = opcode == ISD::CTPOP ? Alpha::CTPOP :
1421 (opcode == ISD::CTTZ ? Alpha::CTTZ : Alpha::CTLZ);
1422 Tmp1 = SelectExpr(N.getOperand(0));
1423 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1424 return Result;
1425
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001426 case ISD::MULHU:
1427 Tmp1 = SelectExpr(N.getOperand(0));
1428 Tmp2 = SelectExpr(N.getOperand(1));
1429 BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth706be912005-04-07 13:55:53 +00001430 return Result;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001431 case ISD::MULHS:
1432 {
1433 //MULHU - Ra<63>*Rb - Rb<63>*Ra
1434 Tmp1 = SelectExpr(N.getOperand(0));
1435 Tmp2 = SelectExpr(N.getOperand(1));
1436 Tmp3 = MakeReg(MVT::i64);
1437 BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1438 unsigned V1 = MakeReg(MVT::i64);
1439 unsigned V2 = MakeReg(MVT::i64);
1440 BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31).addReg(Tmp1);
1441 BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31).addReg(Tmp2);
1442 unsigned IRes = MakeReg(MVT::i64);
1443 BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
1444 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
1445 return Result;
1446 }
Andrew Lenharth7332f3e2005-04-02 19:11:07 +00001447 case ISD::UNDEF: {
1448 BuildMI(BB, Alpha::IDEF, 0, Result);
1449 return Result;
1450 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001451
Andrew Lenharth032f2352005-02-22 21:59:48 +00001452 case ISD::DYNAMIC_STACKALLOC:
1453 // Generate both result values.
Andrew Lenharth3a7118d2005-02-23 17:33:42 +00001454 if (Result != notIn)
1455 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth032f2352005-02-22 21:59:48 +00001456 else
1457 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1458
1459 // FIXME: We are currently ignoring the requested alignment for handling
1460 // greater than the stack alignment. This will need to be revisited at some
1461 // point. Align = N.getOperand(2);
1462
1463 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1464 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1465 std::cerr << "Cannot allocate stack object with greater alignment than"
1466 << " the stack alignment yet!";
1467 abort();
1468 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001469
Andrew Lenharth032f2352005-02-22 21:59:48 +00001470 Select(N.getOperand(0));
1471 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1472 {
1473 if (CN->getValue() < 32000)
1474 {
1475 BuildMI(BB, Alpha::LDA, 2, Alpha::R30)
1476 .addImm(-CN->getValue()).addReg(Alpha::R30);
1477 } else {
1478 Tmp1 = SelectExpr(N.getOperand(1));
1479 // Subtract size from stack pointer, thereby allocating some space.
1480 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1481 }
1482 } else {
1483 Tmp1 = SelectExpr(N.getOperand(1));
1484 // Subtract size from stack pointer, thereby allocating some space.
1485 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1486 }
1487
1488 // Put a pointer to the space into the result register, by copying the stack
1489 // pointer.
Andrew Lenharth7bc47022005-02-22 23:29:25 +00001490 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
Andrew Lenharth032f2352005-02-22 21:59:48 +00001491 return Result;
1492
Andrew Lenharth33819132005-03-04 20:09:23 +00001493// case ISD::ConstantPool:
1494// Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1495// AlphaLowering.restoreGP(BB);
1496// BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(Tmp1);
1497// return Result;
Andrew Lenharth2c594352005-01-29 15:42:07 +00001498
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001499 case ISD::FrameIndex:
Andrew Lenharth032f2352005-02-22 21:59:48 +00001500 BuildMI(BB, Alpha::LDA, 2, Result)
1501 .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
1502 .addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001503 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001504
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001505 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001506 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001507 case ISD::SEXTLOAD:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001508 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001509 {
1510 // Make sure we generate both values.
1511 if (Result != notIn)
1512 ExprMap[N.getValue(1)] = notIn; // Generate the token
1513 else
1514 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001515
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001516 SDOperand Chain = N.getOperand(0);
1517 SDOperand Address = N.getOperand(1);
1518 Select(Chain);
1519
Misha Brukman4633f1c2005-04-21 23:13:11 +00001520 assert(Node->getValueType(0) == MVT::i64 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001521 "Unknown type to sign extend to.");
Andrew Lenharth03824012005-02-07 05:55:55 +00001522 if (opcode == ISD::LOAD)
1523 Opc = Alpha::LDQ;
1524 else
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001525 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
1526 default: Node->dump(); assert(0 && "Bad sign extend!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001527 case MVT::i32: Opc = Alpha::LDL;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001528 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001529 case MVT::i16: Opc = Alpha::LDWU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001530 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001531 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Misha Brukman4633f1c2005-04-21 23:13:11 +00001532 case MVT::i8: Opc = Alpha::LDBU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001533 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001534 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001535
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001536 if (EnableAlphaLSMark)
1537 {
1538 int i = getValueOffset(dyn_cast<SrcValueSDNode>(N.getOperand(2))->getValue());
1539 int j = getFunctionOffset(BB->getParent()->getFunction());
1540 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
1541 }
1542
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001543 if (Address.getOpcode() == ISD::GlobalAddress) {
1544 AlphaLowering.restoreGP(BB);
1545 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001546 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001547 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1548 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001549 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1550 AlphaLowering.restoreGP(BB);
Andrew Lenharthfe895e32005-06-27 17:15:36 +00001551 Opc = GetRelVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001552 has_sym = true;
Andrew Lenharthfe895e32005-06-27 17:15:36 +00001553 Tmp1 = MakeReg(MVT::i64);
1554 BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CP->getIndex()).addReg(Alpha::R29);
1555 BuildMI(BB, Opc, 2, Result).addConstantPoolIndex(CP->getIndex()).addReg(Tmp1);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001556 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001557 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001558 BuildMI(BB, Opc, 2, Result)
1559 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1560 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001561 } else {
1562 long offset;
1563 SelectAddr(Address, Tmp1, offset);
1564 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1565 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001566 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001567 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001568
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001569 case ISD::GlobalAddress:
1570 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001571 has_sym = true;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001572 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
1573 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
1574 return Result;
1575
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001576 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001577 case ISD::CALL:
1578 {
1579 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001580
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001581 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001582 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001583
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001584 //grab the arguments
1585 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001586 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001587 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001588 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001589
Andrew Lenharth684f2292005-01-30 00:35:27 +00001590 //in reg args
1591 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001592 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001593 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001594 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +00001595 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001596 Alpha::F19, Alpha::F20, Alpha::F21};
1597 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001598 default:
1599 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001600 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001601 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001602 N.getOperand(i+2).getValueType() << "\n";
1603 assert(0 && "Unknown value type for call");
1604 case MVT::i1:
1605 case MVT::i8:
1606 case MVT::i16:
1607 case MVT::i32:
1608 case MVT::i64:
1609 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1610 break;
1611 case MVT::f32:
1612 case MVT::f64:
1613 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1614 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001615 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001616 }
Andrew Lenharth684f2292005-01-30 00:35:27 +00001617 //in mem args
1618 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001619 {
1620 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001621 default:
1622 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001623 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001624 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001625 N.getOperand(i+2).getValueType() << "\n";
1626 assert(0 && "Unknown value type for call");
1627 case MVT::i1:
1628 case MVT::i8:
1629 case MVT::i16:
1630 case MVT::i32:
1631 case MVT::i64:
1632 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1633 break;
1634 case MVT::f32:
1635 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1636 break;
1637 case MVT::f64:
1638 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1639 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001640 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001641 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001642 //build the right kind of call
1643 if (GlobalAddressSDNode *GASD =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001644 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001645 {
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001646 if (GASD->getGlobal()->isExternal()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001647 //use safe calling convention
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001648 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001649 has_sym = true;
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001650 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal());
1651 } else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001652 //use PC relative branch call
Andrew Lenharth1e0d9bd2005-04-14 17:34:20 +00001653 AlphaLowering.restoreGP(BB);
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001654 BuildMI(BB, Alpha::BSR, 1, Alpha::R26).addGlobalAddress(GASD->getGlobal(),true);
1655 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001656 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001657 else if (ExternalSymbolSDNode *ESSDN =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001658 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001659 {
1660 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001661 has_sym = true;
Andrew Lenharthba05ad62005-03-30 18:22:52 +00001662 BuildMI(BB, Alpha::CALL, 1).addExternalSymbol(ESSDN->getSymbol(), true);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001663 } else {
1664 //no need to restore GP as we are doing an indirect call
1665 Tmp1 = SelectExpr(N.getOperand(1));
1666 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
1667 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
1668 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001669
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001670 //push the result into a virtual register
Misha Brukman4633f1c2005-04-21 23:13:11 +00001671
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001672 switch (Node->getValueType(0)) {
1673 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001674 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001675 case MVT::i1:
1676 case MVT::i8:
1677 case MVT::i16:
1678 case MVT::i32:
1679 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001680 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
1681 break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001682 case MVT::f32:
1683 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001684 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
1685 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001686 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001687 return Result+N.ResNo;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001688 }
1689
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001690 case ISD::SIGN_EXTEND_INREG:
1691 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001692 //do SDIV opt for all levels of ints if not dividing by a constant
1693 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
1694 && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001695 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001696 unsigned Tmp4 = MakeReg(MVT::f64);
1697 unsigned Tmp5 = MakeReg(MVT::f64);
1698 unsigned Tmp6 = MakeReg(MVT::f64);
1699 unsigned Tmp7 = MakeReg(MVT::f64);
1700 unsigned Tmp8 = MakeReg(MVT::f64);
1701 unsigned Tmp9 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001702
1703 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1704 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1705 MoveInt2FP(Tmp1, Tmp4, true);
1706 MoveInt2FP(Tmp2, Tmp5, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001707 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Tmp4);
1708 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Tmp5);
1709 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
1710 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Tmp8);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001711 MoveFP2Int(Tmp9, Result, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001712 return Result;
1713 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001714
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001715 //Alpha has instructions for a bunch of signed 32 bit stuff
1716 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001717 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001718 switch (N.getOperand(0).getOpcode()) {
1719 case ISD::ADD:
1720 case ISD::SUB:
1721 case ISD::MUL:
1722 {
1723 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
1724 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
1725 //FIXME: first check for Scaled Adds and Subs!
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001726 ConstantSDNode* CSD = NULL;
1727 if(!isMul && N.getOperand(0).getOperand(0).getOpcode() == ISD::SHL &&
1728 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(0).getOperand(1))) &&
1729 (CSD->getValue() == 2 || CSD->getValue() == 3))
1730 {
1731 bool use4 = CSD->getValue() == 2;
1732 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1733 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1734 BuildMI(BB, isAdd?(use4?Alpha::S4ADDL:Alpha::S8ADDL):(use4?Alpha::S4SUBL:Alpha::S8SUBL),
1735 2,Result).addReg(Tmp1).addReg(Tmp2);
1736 }
1737 else if(isAdd && N.getOperand(0).getOperand(1).getOpcode() == ISD::SHL &&
1738 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1).getOperand(1))) &&
1739 (CSD->getValue() == 2 || CSD->getValue() == 3))
1740 {
1741 bool use4 = CSD->getValue() == 2;
1742 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1743 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1744 BuildMI(BB, use4?Alpha::S4ADDL:Alpha::S8ADDL, 2,Result).addReg(Tmp1).addReg(Tmp2);
1745 }
1746 else if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001747 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
1748 { //Normal imm add/sub
1749 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001750 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001751 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
1752 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001753 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001754 else
1755 { //Normal add/sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001756 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULL : Alpha::SUBL);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001757 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001758 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001759 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1760 }
1761 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001762 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001763 default: break; //Fall Though;
1764 }
1765 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001766 Tmp1 = SelectExpr(N.getOperand(0));
1767 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001768 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001769 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001770 {
1771 default:
1772 Node->dump();
1773 assert(0 && "Sign Extend InReg not there yet");
1774 break;
1775 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001776 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001777 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001778 break;
1779 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001780 case MVT::i16:
1781 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
1782 break;
1783 case MVT::i8:
1784 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
1785 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +00001786 case MVT::i1:
1787 Tmp2 = MakeReg(MVT::i64);
1788 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001789 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +00001790 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001791 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001792 return Result;
1793 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001794
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001795 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001796 {
1797 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1798 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
Andrew Lenharth694c2982005-06-26 23:01:11 +00001799 bool isConst = false;
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001800 int dir;
Misha Brukman7847fca2005-04-22 17:54:37 +00001801
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001802 //Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001803 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001804 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth694c2982005-06-26 23:01:11 +00001805 isConst = true;
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001806
1807 switch (SetCC->getCondition()) {
1808 default: Node->dump(); assert(0 && "Unknown integer comparison!");
Andrew Lenharth694c2982005-06-26 23:01:11 +00001809 case ISD::SETEQ:
1810 Opc = isConst ? Alpha::CMPEQi : Alpha::CMPEQ; dir=1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001811 case ISD::SETLT:
Andrew Lenharth694c2982005-06-26 23:01:11 +00001812 Opc = isConst ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001813 case ISD::SETLE:
Andrew Lenharth694c2982005-06-26 23:01:11 +00001814 Opc = isConst ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
1815 case ISD::SETGT: Opc = Alpha::CMPLT; dir = 2; break;
1816 case ISD::SETGE: Opc = Alpha::CMPLE; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001817 case ISD::SETULT:
Andrew Lenharth694c2982005-06-26 23:01:11 +00001818 Opc = isConst ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
1819 case ISD::SETUGT: Opc = Alpha::CMPULT; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001820 case ISD::SETULE:
Andrew Lenharth694c2982005-06-26 23:01:11 +00001821 Opc = isConst ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
1822 case ISD::SETUGE: Opc = Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001823 case ISD::SETNE: {//Handle this one special
1824 //std::cerr << "Alpha does not have a setne.\n";
1825 //abort();
1826 Tmp1 = SelectExpr(N.getOperand(0));
1827 Tmp2 = SelectExpr(N.getOperand(1));
1828 Tmp3 = MakeReg(MVT::i64);
1829 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001830 //Remeber we have the Inv for this CC
1831 CCInvMap[N] = Tmp3;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001832 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001833 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001834 return Result;
1835 }
1836 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001837 if (dir == 1) {
1838 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth694c2982005-06-26 23:01:11 +00001839 if (isConst) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001840 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1841 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1842 } else {
1843 Tmp2 = SelectExpr(N.getOperand(1));
1844 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1845 }
Andrew Lenharth694c2982005-06-26 23:01:11 +00001846 } else { //if (dir == 2) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001847 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth694c2982005-06-26 23:01:11 +00001848 Tmp2 = SelectExpr(N.getOperand(0));
1849 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001850 }
1851 } else {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001852 //do the comparison
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001853 Tmp1 = MakeReg(MVT::f64);
1854 bool inv = SelectFPSetCC(N, Tmp1);
1855
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001856 //now arrange for Result (int) to have a 1 or 0
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001857 Tmp2 = MakeReg(MVT::i64);
1858 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001859 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001860 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001861 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001862 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001863 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001864 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001865
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001866 case ISD::CopyFromReg:
1867 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001868 ++count_ins;
1869
Andrew Lenharth40831c52005-01-28 06:57:18 +00001870 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001871 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001872 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001873 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001874 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001875
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001876 SDOperand Chain = N.getOperand(0);
1877
1878 Select(Chain);
1879 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1880 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1881 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1882 return Result;
1883 }
1884
Misha Brukman4633f1c2005-04-21 23:13:11 +00001885 //Most of the plain arithmetic and logic share the same form, and the same
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001886 //constant immediate test
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001887 case ISD::XOR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001888 //Match Not
1889 if (N.getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001890 cast<ConstantSDNode>(N.getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001891 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001892 Tmp1 = SelectExpr(N.getOperand(0));
1893 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1894 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001895 }
1896 //Fall through
1897 case ISD::AND:
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001898 //handle zap
1899 if (opcode == ISD::AND && N.getOperand(1).getOpcode() == ISD::Constant)
1900 {
1901 uint64_t k = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1902 unsigned int build = 0;
1903 for(int i = 0; i < 8; ++i)
1904 {
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001905 if ((k & 0x00FF) == 0x00FF)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001906 build |= 1 << i;
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001907 else if ((k & 0x00FF) != 0)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001908 { build = 0; break; }
1909 k >>= 8;
1910 }
1911 if (build)
1912 {
1913 Tmp1 = SelectExpr(N.getOperand(0));
1914 BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1915 return Result;
1916 }
1917 }
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001918 case ISD::OR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001919 //Check operand(0) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001920 if (N.getOperand(0).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001921 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001922 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001923 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001924 switch(opcode) {
1925 case ISD::AND: Opc = Alpha::BIC; break;
1926 case ISD::OR: Opc = Alpha::ORNOT; break;
1927 case ISD::XOR: Opc = Alpha::EQV; break;
1928 }
1929 Tmp1 = SelectExpr(N.getOperand(1));
1930 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1931 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1932 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001933 }
1934 //Check operand(1) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001935 if (N.getOperand(1).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001936 N.getOperand(1).getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001937 cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001938 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001939 switch(opcode) {
1940 case ISD::AND: Opc = Alpha::BIC; break;
1941 case ISD::OR: Opc = Alpha::ORNOT; break;
1942 case ISD::XOR: Opc = Alpha::EQV; break;
1943 }
1944 Tmp1 = SelectExpr(N.getOperand(0));
1945 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1946 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1947 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001948 }
1949 //Fall through
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001950 case ISD::SHL:
1951 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001952 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001953 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001954 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1955 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001956 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001957 {
1958 switch(opcode) {
1959 case ISD::AND: Opc = Alpha::ANDi; break;
1960 case ISD::OR: Opc = Alpha::BISi; break;
1961 case ISD::XOR: Opc = Alpha::XORi; break;
1962 case ISD::SHL: Opc = Alpha::SLi; break;
1963 case ISD::SRL: Opc = Alpha::SRLi; break;
1964 case ISD::SRA: Opc = Alpha::SRAi; break;
1965 case ISD::MUL: Opc = Alpha::MULQi; break;
1966 };
1967 Tmp1 = SelectExpr(N.getOperand(0));
1968 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1969 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1970 } else {
1971 switch(opcode) {
1972 case ISD::AND: Opc = Alpha::AND; break;
1973 case ISD::OR: Opc = Alpha::BIS; break;
1974 case ISD::XOR: Opc = Alpha::XOR; break;
1975 case ISD::SHL: Opc = Alpha::SL; break;
1976 case ISD::SRL: Opc = Alpha::SRL; break;
1977 case ISD::SRA: Opc = Alpha::SRA; break;
1978 case ISD::MUL: Opc = Alpha::MULQ; break;
1979 };
1980 Tmp1 = SelectExpr(N.getOperand(0));
1981 Tmp2 = SelectExpr(N.getOperand(1));
1982 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1983 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001984 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001985
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001986 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001987 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001988 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001989 bool isAdd = opcode == ISD::ADD;
1990
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001991 //first check for Scaled Adds and Subs!
1992 //Valid for add and sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001993 ConstantSDNode* CSD = NULL;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001994 if(N.getOperand(0).getOpcode() == ISD::SHL &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001995 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
1996 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001997 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001998 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001999 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002000 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) && CSD->getValue() <= 255)
2001 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
2002 2, Result).addReg(Tmp2).addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00002003 else {
2004 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002005 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
2006 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00002007 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002008 }
2009 //Position prevents subs
Andrew Lenharth273a1f92005-04-07 14:18:13 +00002010 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002011 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) &&
2012 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002013 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002014 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002015 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002016 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(0))) && CSD->getValue() <= 255)
2017 BuildMI(BB, use4?Alpha::S4ADDQi:Alpha::S8ADDQi, 2, Result).addReg(Tmp2)
2018 .addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00002019 else {
2020 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002021 BuildMI(BB, use4?Alpha::S4ADDQ:Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00002022 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002023 }
2024 //small addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002025 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
2026 CSD->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002027 { //Normal imm add/sub
2028 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
2029 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002030 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CSD->getValue());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002031 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002032 //larger addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002033 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
2034 CSD->getSignExtended() <= 32767 &&
2035 CSD->getSignExtended() >= -32767)
Andrew Lenharth74d00d82005-03-02 17:23:03 +00002036 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002037 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002038 Tmp2 = (long)CSD->getSignExtended();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002039 if (!isAdd)
2040 Tmp2 = -Tmp2;
2041 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002042 }
2043 //give up and do the operation
2044 else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002045 //Normal add/sub
2046 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
2047 Tmp1 = SelectExpr(N.getOperand(0));
2048 Tmp2 = SelectExpr(N.getOperand(1));
2049 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2050 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002051 return Result;
2052 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002053
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00002054 case ISD::SDIV:
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002055 {
Andrew Lenhartha565c272005-04-06 22:03:13 +00002056 ConstantSDNode* CSD;
2057 //check if we can convert into a shift!
2058 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
2059 (int64_t)CSD->getSignExtended() != 0 &&
2060 ExactLog2(abs((int64_t)CSD->getSignExtended())) != 0)
2061 {
2062 unsigned k = ExactLog2(abs(CSD->getSignExtended()));
2063 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenhartha565c272005-04-06 22:03:13 +00002064 if (k == 1)
2065 Tmp2 = Tmp1;
2066 else
2067 {
2068 Tmp2 = MakeReg(MVT::i64);
2069 BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
2070 }
2071 Tmp3 = MakeReg(MVT::i64);
2072 BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
2073 unsigned Tmp4 = MakeReg(MVT::i64);
2074 BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
2075 if ((int64_t)CSD->getSignExtended() > 0)
2076 BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
2077 else
2078 {
2079 unsigned Tmp5 = MakeReg(MVT::i64);
2080 BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
2081 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
2082 }
2083 return Result;
2084 }
2085 }
2086 //Else fall through
2087
2088 case ISD::UDIV:
2089 {
2090 ConstantSDNode* CSD;
2091 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
2092 ((int64_t)CSD->getSignExtended() >= 2 ||
2093 (int64_t)CSD->getSignExtended() <= -2))
2094 {
2095 // If this is a divide by constant, we can emit code using some magic
2096 // constants to implement it as a multiply instead.
2097 ExprMap.erase(N);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002098 if (opcode == ISD::SDIV)
Andrew Lenhartha565c272005-04-06 22:03:13 +00002099 return SelectExpr(BuildSDIVSequence(N));
2100 else
2101 return SelectExpr(BuildUDIVSequence(N));
2102 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002103 }
2104 //else fall though
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002105 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +00002106 case ISD::SREM:
Misha Brukman4633f1c2005-04-21 23:13:11 +00002107 //FIXME: alpha really doesn't support any of these operations,
Andrew Lenharth40831c52005-01-28 06:57:18 +00002108 // the ops are expanded into special library calls with
2109 // special calling conventions
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002110 //Restore GP because it is a call after all...
Andrew Lenharth40831c52005-01-28 06:57:18 +00002111 switch(opcode) {
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00002112 case ISD::UREM: Opc = Alpha::REMQU; break;
2113 case ISD::SREM: Opc = Alpha::REMQ; break;
2114 case ISD::UDIV: Opc = Alpha::DIVQU; break;
2115 case ISD::SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002116 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002117 Tmp1 = SelectExpr(N.getOperand(0));
2118 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth33819132005-03-04 20:09:23 +00002119 //set up regs explicitly (helps Reg alloc)
2120 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002121 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00002122 AlphaLowering.restoreGP(BB);
Andrew Lenharth33819132005-03-04 20:09:23 +00002123 BuildMI(BB, Opc, 2).addReg(Alpha::R24).addReg(Alpha::R25);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002124 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002125 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002126
Andrew Lenharthe76797c2005-02-01 20:40:27 +00002127 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002128 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002129 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002130 assert (DestType == MVT::i64 && "only quads can be loaded to");
2131 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00002132 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002133 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002134 if (SrcType == MVT::f32)
Misha Brukman7847fca2005-04-22 17:54:37 +00002135 {
2136 Tmp2 = MakeReg(MVT::f64);
2137 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
2138 Tmp1 = Tmp2;
2139 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002140 Tmp2 = MakeReg(MVT::f64);
2141 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00002142 MoveFP2Int(Tmp2, Result, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002143
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002144 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002145 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002146
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002147 case ISD::SELECT:
2148 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00002149 //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 +00002150 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002151 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2152 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002153 // Get the condition into the zero flag.
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002154 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002155
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002156 SDOperand CC = N.getOperand(0);
2157 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
2158
Misha Brukman4633f1c2005-04-21 23:13:11 +00002159 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002160 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
2161 { //FP Setcc -> Int Select
Misha Brukman7847fca2005-04-22 17:54:37 +00002162 Tmp1 = MakeReg(MVT::f64);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002163 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2164 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Misha Brukman7847fca2005-04-22 17:54:37 +00002165 bool inv = SelectFPSetCC(CC, Tmp1);
2166 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
2167 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
2168 return Result;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002169 }
2170 if (CC.getOpcode() == ISD::SETCC) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002171 //Int SetCC -> Select
2172 //Dropping the CC is only useful if we are comparing to 0
2173 if((SetCC->getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth694c2982005-06-26 23:01:11 +00002174 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0))
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002175 {
2176 //figure out a few things
Andrew Lenharth694c2982005-06-26 23:01:11 +00002177 bool useImm = N.getOperand(2).getOpcode() == ISD::Constant &&
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002178 cast<ConstantSDNode>(N.getOperand(2))->getValue() <= 255;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002179
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002180 //Fix up CC
2181 ISD::CondCode cCode= SetCC->getCondition();
Andrew Lenharth694c2982005-06-26 23:01:11 +00002182 if (useImm) //Invert sense to get Imm field right
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002183 cCode = ISD::getSetCCInverse(cCode, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002184
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002185 //Choose the CMOV
2186 switch (cCode) {
2187 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
2188 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
2189 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
2190 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
2191 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
2192 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
2193 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
2194 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2195 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break; //Technically you could have this CC
2196 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
2197 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2198 }
Andrew Lenharth694c2982005-06-26 23:01:11 +00002199 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002200
Andrew Lenharth694c2982005-06-26 23:01:11 +00002201 if (useImm) {
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002202 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
2203 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
Misha Brukman7847fca2005-04-22 17:54:37 +00002204 .addImm(cast<ConstantSDNode>(N.getOperand(2))->getValue())
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002205 .addReg(Tmp1);
2206 } else {
2207 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2208 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2209 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
2210 }
2211 return Result;
2212 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002213 //Otherwise, fall though
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002214 }
2215 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002216 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2217 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthe76797c2005-02-01 20:40:27 +00002218 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002219
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002220 return Result;
2221 }
2222
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002223 case ISD::Constant:
2224 {
Andrew Lenharthc0513832005-03-29 19:24:04 +00002225 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002226 if (val <= IMM_HIGH && val >= IMM_LOW) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002227 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002228 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002229 else if (val <= (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT &&
2230 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
2231 Tmp1 = MakeReg(MVT::i64);
2232 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val)).addReg(Alpha::R31);
2233 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002234 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002235 else {
2236 MachineConstantPool *CP = BB->getParent()->getConstantPool();
2237 ConstantUInt *C = ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
2238 unsigned CPI = CP->getConstantPoolIndex(C);
2239 AlphaLowering.restoreGP(BB);
Andrew Lenharthfe895e32005-06-27 17:15:36 +00002240 has_sym = true;
2241 Tmp1 = MakeReg(MVT::i64);
2242 BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CPI).addReg(Alpha::R29);
2243 BuildMI(BB, Alpha::LDQr, 2, Result).addConstantPoolIndex(CPI).addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002244 }
2245 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002246 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002247 }
2248
2249 return 0;
2250}
2251
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002252void AlphaISel::Select(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002253 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00002254 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002255
Nate Begeman85fdeb22005-03-24 04:39:54 +00002256 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002257 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002258
2259 SDNode *Node = N.Val;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002260
Andrew Lenharth760270d2005-02-07 23:02:23 +00002261 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002262
2263 default:
2264 Node->dump(); std::cerr << "\n";
2265 assert(0 && "Node not handled yet!");
2266
2267 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00002268 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002269 return;
2270 }
2271
2272 case ISD::BR: {
2273 MachineBasicBlock *Dest =
2274 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2275
2276 Select(N.getOperand(0));
2277 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
2278 return;
2279 }
2280
2281 case ISD::ImplicitDef:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002282 ++count_ins;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002283 Select(N.getOperand(0));
2284 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
2285 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002286
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002287 case ISD::EntryToken: return; // Noop
2288
2289 case ISD::TokenFactor:
2290 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2291 Select(Node->getOperand(i));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002292
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002293 //N.Val->dump(); std::cerr << "\n";
2294 //assert(0 && "Node not handled yet!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00002295
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002296 return;
2297
2298 case ISD::CopyToReg:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002299 ++count_outs;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002300 Select(N.getOperand(0));
2301 Tmp1 = SelectExpr(N.getOperand(1));
2302 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002303
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002304 if (Tmp1 != Tmp2) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002305 if (N.getOperand(1).getValueType() == MVT::f64 ||
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002306 N.getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00002307 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2308 else
2309 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002310 }
2311 return;
2312
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002313 case ISD::RET:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002314 ++count_outs;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002315 switch (N.getNumOperands()) {
2316 default:
2317 std::cerr << N.getNumOperands() << "\n";
2318 for (unsigned i = 0; i < N.getNumOperands(); ++i)
2319 std::cerr << N.getOperand(i).getValueType() << "\n";
2320 Node->dump();
2321 assert(0 && "Unknown return instruction!");
2322 case 2:
2323 Select(N.getOperand(0));
2324 Tmp1 = SelectExpr(N.getOperand(1));
2325 switch (N.getOperand(1).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002326 default: Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002327 assert(0 && "All other types should have been promoted!!");
2328 case MVT::f64:
2329 case MVT::f32:
2330 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
2331 break;
2332 case MVT::i32:
2333 case MVT::i64:
2334 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
2335 break;
2336 }
2337 break;
2338 case 1:
2339 Select(N.getOperand(0));
2340 break;
2341 }
Andrew Lenharth3b918072005-06-27 15:36:48 +00002342 BuildMI(BB, Alpha::RET, 1, Alpha::R31).addReg(AlphaLowering.getRA()); // Just emit a 'ret' instruction
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002343 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002344
Misha Brukman4633f1c2005-04-21 23:13:11 +00002345 case ISD::TRUNCSTORE:
2346 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002347 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002348 SDOperand Chain = N.getOperand(0);
2349 SDOperand Value = N.getOperand(1);
2350 SDOperand Address = N.getOperand(2);
2351 Select(Chain);
2352
2353 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00002354
2355 if (opcode == ISD::STORE) {
2356 switch(Value.getValueType()) {
2357 default: assert(0 && "unknown Type in store");
2358 case MVT::i64: Opc = Alpha::STQ; break;
2359 case MVT::f64: Opc = Alpha::STT; break;
2360 case MVT::f32: Opc = Alpha::STS; break;
2361 }
2362 } else { //ISD::TRUNCSTORE
2363 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2364 default: assert(0 && "unknown Type in store");
2365 case MVT::i1: //FIXME: DAG does not promote this load
2366 case MVT::i8: Opc = Alpha::STB; break;
2367 case MVT::i16: Opc = Alpha::STW; break;
2368 case MVT::i32: Opc = Alpha::STL; break;
2369 }
Andrew Lenharth65838902005-02-06 16:22:15 +00002370 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00002371
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002372 if (EnableAlphaLSMark)
2373 {
2374 int i = getValueOffset(dyn_cast<SrcValueSDNode>(N.getOperand(3))->getValue());
2375 int j = getFunctionOffset(BB->getParent()->getFunction());
2376 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
2377 }
2378
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002379 if (Address.getOpcode() == ISD::GlobalAddress)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002380 {
2381 AlphaLowering.restoreGP(BB);
2382 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002383 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002384 BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
2385 }
Andrew Lenharth05380342005-02-07 05:07:00 +00002386 else if(Address.getOpcode() == ISD::FrameIndex)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002387 {
Andrew Lenharth032f2352005-02-22 21:59:48 +00002388 BuildMI(BB, Opc, 3).addReg(Tmp1)
2389 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
2390 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002391 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002392 else
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002393 {
2394 long offset;
2395 SelectAddr(Address, Tmp2, offset);
2396 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2397 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002398 return;
2399 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002400
2401 case ISD::EXTLOAD:
2402 case ISD::SEXTLOAD:
2403 case ISD::ZEXTLOAD:
2404 case ISD::LOAD:
2405 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002406 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002407 case ISD::CALL:
Andrew Lenharth032f2352005-02-22 21:59:48 +00002408 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002409 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002410 SelectExpr(N);
2411 return;
2412
Chris Lattner16cd04d2005-05-12 23:24:06 +00002413 case ISD::CALLSEQ_START:
2414 case ISD::CALLSEQ_END:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002415 Select(N.getOperand(0));
2416 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002417
Chris Lattner16cd04d2005-05-12 23:24:06 +00002418 Opc = N.getOpcode() == ISD::CALLSEQ_START ? Alpha::ADJUSTSTACKDOWN :
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002419 Alpha::ADJUSTSTACKUP;
2420 BuildMI(BB, Opc, 1).addImm(Tmp1);
2421 return;
Andrew Lenharth95762122005-03-31 21:24:06 +00002422
2423 case ISD::PCMARKER:
2424 Select(N.getOperand(0)); //Chain
2425 BuildMI(BB, Alpha::PCLABEL, 2).addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
2426 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002427 }
2428 assert(0 && "Should not be reached!");
2429}
2430
2431
2432/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
2433/// into a machine code representation using pattern matching and a machine
2434/// description file.
2435///
2436FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002437 return new AlphaISel(TM);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002438}
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002439