blob: b4e94936bdaa47d098dabd2e1e5744be77f96037 [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",
Andrew Lenharthd4653b12005-06-27 17:39:17 +000038 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",
Andrew Lenharthd4653b12005-06-27 17:39:17 +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",
Andrew Lenharthd4653b12005-06-27 17:39:17 +000044 cl::desc("Enable use of the ctpop, ctlz, and cttz instructions"),
Andrew Lenharth59009192005-05-04 19:12:09 +000045 cl::Hidden);
Misha Brukman4633f1c2005-04-21 23:13:11 +000046 cl::opt<bool> EnableAlphaCount("enable-alpha-count",
Andrew Lenharthd4653b12005-06-27 17:39:17 +000047 cl::desc("Print estimates on live ins and outs"),
48 cl::Hidden);
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +000049 cl::opt<bool> EnableAlphaLSMark("enable-alpha-lsmark",
Andrew Lenharthd4653b12005-06-27 17:39:17 +000050 cl::desc("Emit symbols to correlate Mem ops to LLVM Values"),
51 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);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +000095 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
Andrew Lenharth6968bff2005-06-27 23:24:11 +000096
Andrew Lenharthc7989ce2005-06-29 00:31:08 +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());
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000212 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other,
213 DAG.getEntryNode(), Op.getOperand(0),
214 StackSlot, DAG.getSrcValue(NULL));
Misha Brukmanb8ee91a2005-06-06 17:39:46 +0000215 SRC = DAG.getLoad(Op.getValueType(), Store.getValue(0), StackSlot,
216 DAG.getSrcValue(NULL));
217 }
218 std::vector<MVT::ValueType> RTs;
219 RTs.push_back(Op.getValueType());
220 std::vector<SDOperand> Ops;
221 Ops.push_back(SRC);
222 return DAG.getNode(AlphaISD::CVTQ, RTs, Ops);
223 }
224#endif
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000225 }
Misha Brukmanb8ee91a2005-06-06 17:39:46 +0000226 return SDOperand();
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000227}
228
229
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000230/// AddLiveIn - This helper function adds the specified physical register to the
231/// MachineFunction as a live in value. It also creates a corresponding virtual
232/// register for it.
233static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
234 TargetRegisterClass *RC) {
235 assert(RC->contains(PReg) && "Not the correct regclass!");
236 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
237 MF.addLiveIn(PReg, VReg);
238 return VReg;
239}
240
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000241//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
242
243//For now, just use variable size stack frame format
244
245//In a standard call, the first six items are passed in registers $16
246//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
247//of argument-to-register correspondence.) The remaining items are
248//collected in a memory argument list that is a naturally aligned
249//array of quadwords. In a standard call, this list, if present, must
250//be passed at 0(SP).
Misha Brukman7847fca2005-04-22 17:54:37 +0000251//7 ... n 0(SP) ... (n-7)*8(SP)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000252
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000253// //#define FP $15
254// //#define RA $26
255// //#define PV $27
256// //#define GP $29
257// //#define SP $30
Misha Brukman4633f1c2005-04-21 23:13:11 +0000258
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000259std::vector<SDOperand>
Misha Brukman4633f1c2005-04-21 23:13:11 +0000260AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000261{
262 std::vector<SDOperand> ArgValues;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000263
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000264 MachineFunction &MF = DAG.getMachineFunction();
Andrew Lenharth05380342005-02-07 05:07:00 +0000265 MachineFrameInfo*MFI = MF.getFrameInfo();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000266
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000267 MachineBasicBlock& BB = MF.front();
268
Misha Brukman4633f1c2005-04-21 23:13:11 +0000269 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000270 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000271 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000272 Alpha::F19, Alpha::F20, Alpha::F21};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000273 int count = 0;
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000274
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000275 GP = AddLiveIn(MF, Alpha::R29, getRegClassFor(MVT::i64));
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000276 RA = AddLiveIn(MF, Alpha::R26, getRegClassFor(MVT::i64));
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000277
Chris Lattnere4d5c442005-03-15 04:54:21 +0000278 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000279 {
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000280 SDOperand argt;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000281 if (count < 6) {
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000282 unsigned Vreg;
283 MVT::ValueType VT = getValueType(I->getType());
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000284 switch (VT) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000285 default:
286 std::cerr << "Unknown Type " << VT << "\n";
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000287 abort();
288 case MVT::f64:
289 case MVT::f32:
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000290 args_float[count] = AddLiveIn(MF,args_float[count], getRegClassFor(VT));
291 argt = DAG.getCopyFromReg(args_float[count], VT, DAG.getRoot());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000292 break;
293 case MVT::i1:
294 case MVT::i8:
295 case MVT::i16:
296 case MVT::i32:
297 case MVT::i64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000298 args_int[count] = AddLiveIn(MF, args_int[count],
299 getRegClassFor(MVT::i64));
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000300 argt = DAG.getCopyFromReg(args_int[count], VT, DAG.getRoot());
Andrew Lenharth14f30c92005-05-31 18:37:16 +0000301 if (VT != MVT::i64)
302 argt = DAG.getNode(ISD::TRUNCATE, VT, argt);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000303 break;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000304 }
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000305 DAG.setRoot(argt.getValue(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000306 } else { //more args
307 // Create the frame index object for this incoming parameter...
308 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000309
310 // Create the SelectionDAG nodes corresponding to a load
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000311 //from this parameter
312 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000313 argt = DAG.getLoad(getValueType(I->getType()),
314 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000315 }
Andrew Lenharth032f2352005-02-22 21:59:48 +0000316 ++count;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000317 ArgValues.push_back(argt);
318 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000319
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000320 // If the functions takes variable number of arguments, copy all regs to stack
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000321 if (F.isVarArg()) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000322 VarArgsOffset = count * 8;
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000323 std::vector<SDOperand> LS;
324 for (int i = 0; i < 6; ++i) {
325 if (args_int[i] < 1024)
326 args_int[i] = AddLiveIn(MF,args_int[i], getRegClassFor(MVT::i64));
327 SDOperand argt = DAG.getCopyFromReg(args_int[i], MVT::i64, DAG.getRoot());
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000328 int FI = MFI->CreateFixedObject(8, -8 * (6 - i));
Andrew Lenharth558bc882005-06-18 18:34:52 +0000329 if (i == 0) VarArgsBase = FI;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000330 SDOperand SDFI = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000331 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt,
332 SDFI, DAG.getSrcValue(NULL)));
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000333
334 if (args_float[i] < 1024)
335 args_float[i] = AddLiveIn(MF,args_float[i], getRegClassFor(MVT::f64));
336 argt = DAG.getCopyFromReg(args_float[i], MVT::f64, DAG.getRoot());
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000337 FI = MFI->CreateFixedObject(8, - 8 * (12 - i));
338 SDFI = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000339 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt,
340 SDFI, DAG.getSrcValue(NULL)));
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000341 }
342
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000343 //Set up a token factor with all the stack traffic
344 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, LS));
345 }
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000346
347 // Finally, inform the code generator which regs we return values in.
348 switch (getValueType(F.getReturnType())) {
349 default: assert(0 && "Unknown type!");
350 case MVT::isVoid: break;
351 case MVT::i1:
352 case MVT::i8:
353 case MVT::i16:
354 case MVT::i32:
355 case MVT::i64:
356 MF.addLiveOut(Alpha::R0);
357 break;
358 case MVT::f32:
359 case MVT::f64:
360 MF.addLiveOut(Alpha::F0);
361 break;
362 }
363
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000364 //return the arguments
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000365 return ArgValues;
366}
367
368std::pair<SDOperand, SDOperand>
369AlphaTargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000370 const Type *RetTy, bool isVarArg,
Chris Lattneradf6a962005-05-13 18:50:42 +0000371 unsigned CallingConv, bool isTailCall,
Misha Brukman7847fca2005-04-22 17:54:37 +0000372 SDOperand Callee, ArgListTy &Args,
373 SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000374 int NumBytes = 0;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000375 if (Args.size() > 6)
376 NumBytes = (Args.size() - 6) * 8;
377
Chris Lattner16cd04d2005-05-12 23:24:06 +0000378 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000379 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000380 std::vector<SDOperand> args_to_use;
381 for (unsigned i = 0, e = Args.size(); i != e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000382 {
383 switch (getValueType(Args[i].second)) {
384 default: assert(0 && "Unexpected ValueType for argument!");
385 case MVT::i1:
386 case MVT::i8:
387 case MVT::i16:
388 case MVT::i32:
389 // Promote the integer to 64 bits. If the input type is signed use a
390 // sign extend, otherwise use a zero extend.
391 if (Args[i].second->isSigned())
392 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
393 else
394 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
395 break;
396 case MVT::i64:
397 case MVT::f64:
398 case MVT::f32:
399 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000400 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000401 args_to_use.push_back(Args[i].first);
402 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000403
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000404 std::vector<MVT::ValueType> RetVals;
405 MVT::ValueType RetTyVT = getValueType(RetTy);
406 if (RetTyVT != MVT::isVoid)
407 RetVals.push_back(RetTyVT);
408 RetVals.push_back(MVT::Other);
409
Misha Brukman4633f1c2005-04-21 23:13:11 +0000410 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000411 Chain, Callee, args_to_use), 0);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000412 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner16cd04d2005-05-12 23:24:06 +0000413 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000414 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000415 return std::make_pair(TheCall, Chain);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000416}
417
418std::pair<SDOperand, SDOperand>
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000419AlphaTargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG,
420 SDOperand Dest) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000421 // vastart just stores the address of the VarArgsBase and VarArgsOffset
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000422 SDOperand FR = DAG.getFrameIndex(VarArgsBase, MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000423 SDOperand S1 = DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, Dest,
424 DAG.getSrcValue(NULL));
425 SDOperand SA2 = DAG.getNode(ISD::ADD, MVT::i64, Dest,
426 DAG.getConstant(8, MVT::i64));
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000427 SDOperand S2 = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, S1,
Andrew Lenharth558bc882005-06-18 18:34:52 +0000428 DAG.getConstant(VarArgsOffset, MVT::i64), SA2,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000429 DAG.getSrcValue(NULL), MVT::i32);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000430 return std::make_pair(S2, S2);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000431}
432
433std::pair<SDOperand,SDOperand> AlphaTargetLowering::
Andrew Lenharth558bc882005-06-18 18:34:52 +0000434LowerVAArgNext(SDOperand Chain, SDOperand VAList,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000435 const Type *ArgTy, SelectionDAG &DAG) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000436 SDOperand Base = DAG.getLoad(MVT::i64, Chain, VAList, DAG.getSrcValue(NULL));
437 SDOperand Tmp = DAG.getNode(ISD::ADD, MVT::i64, VAList,
438 DAG.getConstant(8, MVT::i64));
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000439 SDOperand Offset = DAG.getNode(ISD::SEXTLOAD, MVT::i64, Base.getValue(1),
440 Tmp, DAG.getSrcValue(NULL), MVT::i32);
Andrew Lenharth558bc882005-06-18 18:34:52 +0000441 SDOperand DataPtr = DAG.getNode(ISD::ADD, MVT::i64, Base, Offset);
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000442 if (ArgTy->isFloatingPoint())
443 {
444 //if fp && Offset < 6*8, then subtract 6*8 from DataPtr
445 SDOperand FPDataPtr = DAG.getNode(ISD::SUB, MVT::i64, DataPtr,
446 DAG.getConstant(8*6, MVT::i64));
447 SDOperand CC = DAG.getSetCC(ISD::SETLT, MVT::i64,
448 Offset, DAG.getConstant(8*6, MVT::i64));
449 DataPtr = DAG.getNode(ISD::SELECT, MVT::i64, CC, FPDataPtr, DataPtr);
450 }
451
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000452 SDOperand Result;
453 if (ArgTy == Type::IntTy)
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000454 Result = DAG.getNode(ISD::SEXTLOAD, MVT::i64, Offset.getValue(1), DataPtr,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000455 DAG.getSrcValue(NULL), MVT::i32);
456 else if (ArgTy == Type::UIntTy)
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000457 Result = DAG.getNode(ISD::ZEXTLOAD, MVT::i64, Offset.getValue(1), DataPtr,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000458 DAG.getSrcValue(NULL), MVT::i32);
459 else
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000460 Result = DAG.getLoad(getValueType(ArgTy), Offset.getValue(1), DataPtr,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000461 DAG.getSrcValue(NULL));
462
Andrew Lenharth558bc882005-06-18 18:34:52 +0000463 SDOperand NewOffset = DAG.getNode(ISD::ADD, MVT::i64, Offset,
464 DAG.getConstant(8, MVT::i64));
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000465 SDOperand Update = DAG.getNode(ISD::TRUNCSTORE, MVT::Other,
466 Result.getValue(1), NewOffset,
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000467 Tmp, DAG.getSrcValue(NULL), MVT::i32);
468 Result = DAG.getNode(ISD::TRUNCATE, getValueType(ArgTy), Result);
469
Andrew Lenharth558bc882005-06-18 18:34:52 +0000470 return std::make_pair(Result, Update);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000471}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000472
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000473std::pair<SDOperand,SDOperand> AlphaTargetLowering::
474LowerVACopy(SDOperand Chain, SDOperand Src, SDOperand Dest,
475 SelectionDAG &DAG) {
476 //Default to returning the input list
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000477 SDOperand Val = DAG.getLoad(getPointerTy(), Chain, Src,
478 DAG.getSrcValue(NULL));
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000479 SDOperand Result = DAG.getNode(ISD::STORE, MVT::Other, Val.getValue(1),
480 Val, Dest, DAG.getSrcValue(NULL));
481 SDOperand NP = DAG.getNode(ISD::ADD, MVT::i64, Src,
482 DAG.getConstant(8, MVT::i64));
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000483 Val = DAG.getNode(ISD::SEXTLOAD, MVT::i64, Result, NP, DAG.getSrcValue(NULL),
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000484 MVT::i32);
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000485 SDOperand NPD = DAG.getNode(ISD::ADD, MVT::i64, Dest,
486 DAG.getConstant(8, MVT::i64));
Andrew Lenhartha9e39e22005-06-23 16:48:51 +0000487 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Val.getValue(1),
Andrew Lenharth3f5aa1c2005-06-23 23:42:05 +0000488 Val, NPD, DAG.getSrcValue(NULL), MVT::i32);
Andrew Lenharthcdf233d2005-06-22 23:04:28 +0000489 return std::make_pair(Result, Result);
490}
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000491
492std::pair<SDOperand, SDOperand> AlphaTargetLowering::
493LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
494 SelectionDAG &DAG) {
495 abort();
496}
497
498
499
500
501
502namespace {
503
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000504//===--------------------------------------------------------------------===//
505/// ISel - Alpha specific code to select Alpha machine instructions for
506/// SelectionDAG operations.
507//===--------------------------------------------------------------------===//
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000508class AlphaISel : public SelectionDAGISel {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000509
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000510 /// AlphaLowering - This object fully describes how to lower LLVM code to an
511 /// Alpha-specific SelectionDAG.
512 AlphaTargetLowering AlphaLowering;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000513
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000514 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
515 // for sdiv and udiv until it is put into the future
516 // dag combiner.
517
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000518 /// ExprMap - As shared expressions are codegen'd, we keep track of which
519 /// vreg the value is produced in, so we only emit one copy of each compiled
520 /// tree.
521 static const unsigned notIn = (unsigned)(-1);
522 std::map<SDOperand, unsigned> ExprMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000523
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000524 //CCInvMap sometimes (SetNE) we have the inverse CC code for free
525 std::map<SDOperand, unsigned> CCInvMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000526
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000527 int count_ins;
528 int count_outs;
529 bool has_sym;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000530 int max_depth;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000531
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000532public:
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000533 AlphaISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering),
534 AlphaLowering(TM)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000535 {}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000536
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000537 /// InstructionSelectBasicBlock - This callback is invoked by
538 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
539 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
Andrew Lenharth032f2352005-02-22 21:59:48 +0000540 DEBUG(BB->dump());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000541 count_ins = 0;
542 count_outs = 0;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000543 max_depth = 0;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000544 has_sym = false;
545
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000546 // Codegen the basic block.
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000547 ISelDAG = &DAG;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000548 max_depth = DAG.getRoot().getNodeDepth();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000549 Select(DAG.getRoot());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000550
551 if(has_sym)
552 ++count_ins;
553 if(EnableAlphaCount)
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000554 std::cerr << "COUNT: "
555 << BB->getParent()->getFunction ()->getName() << " "
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000556 << BB->getNumber() << " "
557 << max_depth << " "
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000558 << count_ins << " "
559 << count_outs << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000560
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000561 // Clear state used for selection.
562 ExprMap.clear();
563 CCInvMap.clear();
564 }
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000565
566 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000567
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000568 unsigned SelectExpr(SDOperand N);
569 unsigned SelectExprFP(SDOperand N, unsigned Result);
570 void Select(SDOperand N);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000571
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000572 void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
573 void SelectBranchCC(SDOperand N);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000574 void MoveFP2Int(unsigned src, unsigned dst, bool isDouble);
575 void MoveInt2FP(unsigned src, unsigned dst, bool isDouble);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000576 //returns whether the sense of the comparison was inverted
577 bool SelectFPSetCC(SDOperand N, unsigned dst);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000578
579 // dag -> dag expanders for integer divide by constant
580 SDOperand BuildSDIVSequence(SDOperand N);
581 SDOperand BuildUDIVSequence(SDOperand N);
582
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000583};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000584}
585
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000586void AlphaISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000587 // If this function has live-in values, emit the copies from pregs to vregs at
588 // the top of the function, before anything else.
589 MachineBasicBlock *BB = MF.begin();
590 if (MF.livein_begin() != MF.livein_end()) {
591 SSARegMap *RegMap = MF.getSSARegMap();
592 for (MachineFunction::livein_iterator LI = MF.livein_begin(),
593 E = MF.livein_end(); LI != E; ++LI) {
594 const TargetRegisterClass *RC = RegMap->getRegClass(LI->second);
595 if (RC == Alpha::GPRCRegisterClass) {
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000596 BuildMI(BB, Alpha::BIS, 2, LI->second).addReg(LI->first)
597 .addReg(LI->first);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000598 } else if (RC == Alpha::FPRCRegisterClass) {
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000599 BuildMI(BB, Alpha::CPYS, 2, LI->second).addReg(LI->first)
600 .addReg(LI->first);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000601 } else {
602 assert(0 && "Unknown regclass!");
603 }
604 }
605 }
606}
607
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000608//Find the offset of the arg in it's parent's function
609static int getValueOffset(const Value* v)
610{
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000611 static int uniqneg = -1;
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000612 if (v == NULL)
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000613 return uniqneg--;
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000614
615 const Instruction* itarget = dyn_cast<Instruction>(v);
616 const BasicBlock* btarget = itarget->getParent();
617 const Function* ftarget = btarget->getParent();
618
619 //offset due to earlier BBs
620 int i = 0;
621 for(Function::const_iterator ii = ftarget->begin(); &*ii != btarget; ++ii)
622 i += ii->size();
623
624 for(BasicBlock::const_iterator ii = btarget->begin(); &*ii != itarget; ++ii)
625 ++i;
626
627 return i;
628}
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000629//Find the offset of the function in it's module
630static int getFunctionOffset(const Function* fun)
631{
632 const Module* M = fun->getParent();
633
634 //offset due to earlier BBs
635 int i = 0;
636 for(Module::const_iterator ii = M->begin(); &*ii != fun; ++ii)
637 ++i;
638
639 return i;
640}
641
642static int getUID()
643{
644 static int id = 0;
645 return ++id;
646}
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000647
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000648//Factorize a number using the list of constants
649static bool factorize(int v[], int res[], int size, uint64_t c)
650{
651 bool cont = true;
652 while (c != 1 && cont)
653 {
654 cont = false;
655 for(int i = 0; i < size; ++i)
656 {
657 if (c % v[i] == 0)
658 {
659 c /= v[i];
660 ++res[i];
661 cont=true;
662 }
663 }
664 }
665 return c == 1;
666}
667
668
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000669//Shamelessly adapted from PPC32
Misha Brukman4633f1c2005-04-21 23:13:11 +0000670// Structure used to return the necessary information to codegen an SDIV as
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000671// a multiply.
672struct ms {
673 int64_t m; // magic number
674 int64_t s; // shift amount
675};
676
677struct mu {
678 uint64_t m; // magic number
679 int64_t a; // add indicator
680 int64_t s; // shift amount
681};
682
683/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukman4633f1c2005-04-21 23:13:11 +0000684/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000685/// or -1.
686static struct ms magic(int64_t d) {
687 int64_t p;
688 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
689 const uint64_t two63 = 9223372036854775808ULL; // 2^63
690 struct ms mag;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000691
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000692 ad = abs(d);
693 t = two63 + ((uint64_t)d >> 63);
694 anc = t - 1 - t%ad; // absolute value of nc
Andrew Lenharth320174f2005-04-07 17:19:16 +0000695 p = 63; // initialize p
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000696 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
697 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
698 q2 = two63/ad; // initialize q2 = 2p/abs(d)
699 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
700 do {
701 p = p + 1;
702 q1 = 2*q1; // update q1 = 2p/abs(nc)
703 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
704 if (r1 >= anc) { // must be unsigned comparison
705 q1 = q1 + 1;
706 r1 = r1 - anc;
707 }
708 q2 = 2*q2; // update q2 = 2p/abs(d)
709 r2 = 2*r2; // update r2 = rem(2p/abs(d))
710 if (r2 >= ad) { // must be unsigned comparison
711 q2 = q2 + 1;
712 r2 = r2 - ad;
713 }
714 delta = ad - r2;
715 } while (q1 < delta || (q1 == delta && r1 == 0));
716
717 mag.m = q2 + 1;
718 if (d < 0) mag.m = -mag.m; // resulting magic number
719 mag.s = p - 64; // resulting shift
720 return mag;
721}
722
723/// magicu - calculate the magic numbers required to codegen an integer udiv as
724/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
725static struct mu magicu(uint64_t d)
726{
727 int64_t p;
728 uint64_t nc, delta, q1, r1, q2, r2;
729 struct mu magu;
730 magu.a = 0; // initialize "add" indicator
731 nc = - 1 - (-d)%d;
Andrew Lenharth320174f2005-04-07 17:19:16 +0000732 p = 63; // initialize p
733 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
734 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
735 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
736 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000737 do {
738 p = p + 1;
739 if (r1 >= nc - r1 ) {
740 q1 = 2*q1 + 1; // update q1
741 r1 = 2*r1 - nc; // update r1
742 }
743 else {
744 q1 = 2*q1; // update q1
745 r1 = 2*r1; // update r1
746 }
747 if (r2 + 1 >= d - r2) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000748 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000749 q2 = 2*q2 + 1; // update q2
750 r2 = 2*r2 + 1 - d; // update r2
751 }
752 else {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000753 if (q2 >= 0x8000000000000000ull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000754 q2 = 2*q2; // update q2
755 r2 = 2*r2 + 1; // update r2
756 }
757 delta = d - 1 - r2;
758 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
759 magu.m = q2 + 1; // resulting magic number
Andrew Lenharth320174f2005-04-07 17:19:16 +0000760 magu.s = p - 64; // resulting shift
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000761 return magu;
762}
763
764/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
765/// return a DAG expression to select that will generate the same value by
766/// multiplying by a magic number. See:
767/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000768SDOperand AlphaISel::BuildSDIVSequence(SDOperand N) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000769 int64_t d = (int64_t)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000770 ms magics = magic(d);
771 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000772 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000773 ISelDAG->getConstant(magics.m, MVT::i64));
774 // If d > 0 and m < 0, add the numerator
775 if (d > 0 && magics.m < 0)
776 Q = ISelDAG->getNode(ISD::ADD, MVT::i64, Q, N.getOperand(0));
777 // If d < 0 and m > 0, subtract the numerator.
778 if (d < 0 && magics.m > 0)
779 Q = ISelDAG->getNode(ISD::SUB, MVT::i64, Q, N.getOperand(0));
780 // Shift right algebraic if shift value is nonzero
781 if (magics.s > 0)
Misha Brukman4633f1c2005-04-21 23:13:11 +0000782 Q = ISelDAG->getNode(ISD::SRA, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000783 ISelDAG->getConstant(magics.s, MVT::i64));
784 // Extract the sign bit and add it to the quotient
Misha Brukman4633f1c2005-04-21 23:13:11 +0000785 SDOperand T =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000786 ISelDAG->getNode(ISD::SRL, MVT::i64, Q, ISelDAG->getConstant(63, MVT::i64));
787 return ISelDAG->getNode(ISD::ADD, MVT::i64, Q, T);
788}
789
790/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
791/// return a DAG expression to select that will generate the same value by
792/// multiplying by a magic number. See:
793/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000794SDOperand AlphaISel::BuildUDIVSequence(SDOperand N) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000795 unsigned d =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000796 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
797 mu magics = magicu(d);
798 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000799 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000800 ISelDAG->getConstant(magics.m, MVT::i64));
801 if (magics.a == 0) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000802 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000803 ISelDAG->getConstant(magics.s, MVT::i64));
804 } else {
805 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i64, N.getOperand(0), Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000806 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000807 ISelDAG->getConstant(1, MVT::i64));
808 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i64, NPQ, Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000809 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000810 ISelDAG->getConstant(magics.s-1, MVT::i64));
811 }
812 return Q;
813}
814
Andrew Lenhartha565c272005-04-06 22:03:13 +0000815//From PPC32
816/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
817/// returns zero when the input is not exactly a power of two.
818static unsigned ExactLog2(uint64_t Val) {
819 if (Val == 0 || (Val & (Val-1))) return 0;
820 unsigned Count = 0;
821 while (Val != 1) {
822 Val >>= 1;
823 ++Count;
824 }
825 return Count;
826}
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000827
828
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000829//These describe LDAx
Andrew Lenharthc0513832005-03-29 19:24:04 +0000830static const int IMM_LOW = -32768;
831static const int IMM_HIGH = 32767;
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000832static const int IMM_MULT = 65536;
833
834static long getUpper16(long l)
835{
836 long y = l / IMM_MULT;
837 if (l % IMM_MULT > IMM_HIGH)
838 ++y;
839 return y;
840}
841
842static long getLower16(long l)
843{
844 long h = getUpper16(l);
845 return l - h * IMM_MULT;
846}
847
Andrew Lenharthfe895e32005-06-27 17:15:36 +0000848static unsigned GetRelVersion(unsigned opcode)
849{
850 switch (opcode) {
851 default: assert(0 && "unknown load or store"); return 0;
852 case Alpha::LDQ: return Alpha::LDQr;
853 case Alpha::LDS: return Alpha::LDSr;
854 case Alpha::LDT: return Alpha::LDTr;
855 case Alpha::LDL: return Alpha::LDLr;
856 case Alpha::LDBU: return Alpha::LDBUr;
857 case Alpha::LDWU: return Alpha::LDWUr;
Andrew Lenharthfce587e2005-06-29 00:39:17 +0000858 case Alpha::STB: return Alpha::STBr;
859 case Alpha::STW: return Alpha::STWr;
860 case Alpha::STL: return Alpha::STLr;
861 case Alpha::STQ: return Alpha::STQr;
862 case Alpha::STS: return Alpha::STSr;
863 case Alpha::STT: return Alpha::STTr;
864
Andrew Lenharthfe895e32005-06-27 17:15:36 +0000865 }
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
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000914 bool rev = false;
915 bool inv = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000916
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000917 switch (SetCC->getCondition()) {
918 default: Node->dump(); assert(0 && "Unknown FP comparison!");
919 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
920 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
921 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
922 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
923 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
924 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
925 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000926
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000927 ConstantFPSDNode *CN;
928 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
929 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
930 Tmp1 = Alpha::F31;
931 else
932 Tmp1 = SelectExpr(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000933
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000934 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
935 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
936 Tmp2 = Alpha::F31;
937 else
Chris Lattner9c9183a2005-04-30 04:44:07 +0000938 Tmp2 = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000939
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000940 //Can only compare doubles, and dag won't promote for me
941 if (SetCC->getOperand(0).getValueType() == MVT::f32)
942 {
943 //assert(0 && "Setcc On float?\n");
944 std::cerr << "Setcc on float!\n";
945 Tmp3 = MakeReg(MVT::f64);
946 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
947 Tmp1 = Tmp3;
948 }
949 if (SetCC->getOperand(1).getValueType() == MVT::f32)
950 {
951 //assert (0 && "Setcc On float?\n");
952 std::cerr << "Setcc on float!\n";
953 Tmp3 = MakeReg(MVT::f64);
954 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp2);
955 Tmp2 = Tmp3;
956 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000957
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000958 if (rev) std::swap(Tmp1, Tmp2);
959 //do the comparison
960 BuildMI(BB, Opc, 2, dst).addReg(Tmp1).addReg(Tmp2);
961 return inv;
962}
963
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000964//Check to see if the load is a constant offset from a base register
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000965void AlphaISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000966{
967 unsigned opcode = N.getOpcode();
Andrew Lenharth694c2982005-06-26 23:01:11 +0000968 if (opcode == ISD::ADD && N.getOperand(1).getOpcode() == ISD::Constant &&
969 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
970 { //Normal imm add
971 Reg = SelectExpr(N.getOperand(0));
972 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
973 return;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000974 }
975 Reg = SelectExpr(N);
976 offset = 0;
977 return;
978}
979
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000980void AlphaISel::SelectBranchCC(SDOperand N)
Andrew Lenharth445171a2005-02-08 00:40:03 +0000981{
982 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000983 MachineBasicBlock *Dest =
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000984 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
985 unsigned Opc = Alpha::WTF;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000986
Andrew Lenharth445171a2005-02-08 00:40:03 +0000987 Select(N.getOperand(0)); //chain
988 SDOperand CC = N.getOperand(1);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000989
Andrew Lenharth445171a2005-02-08 00:40:03 +0000990 if (CC.getOpcode() == ISD::SETCC)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000991 {
992 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
993 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
994 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharth09552bf2005-06-08 18:02:21 +0000995 bool RightZero = SetCC->getOperand(1).getOpcode() == ISD::Constant &&
996 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000997 bool isNE = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000998
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000999 //Fix up CC
1000 ISD::CondCode cCode= SetCC->getCondition();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001001
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001002 if(cCode == ISD::SETNE)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001003 isNE = true;
Andrew Lenharth445171a2005-02-08 00:40:03 +00001004
Andrew Lenharth694c2982005-06-26 23:01:11 +00001005 if (RightZero) {
Andrew Lenharth09552bf2005-06-08 18:02:21 +00001006 switch (cCode) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001007 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
1008 case ISD::SETEQ: Opc = Alpha::BEQ; break;
1009 case ISD::SETLT: Opc = Alpha::BLT; break;
1010 case ISD::SETLE: Opc = Alpha::BLE; break;
1011 case ISD::SETGT: Opc = Alpha::BGT; break;
1012 case ISD::SETGE: Opc = Alpha::BGE; break;
1013 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
1014 case ISD::SETUGT: Opc = Alpha::BNE; break;
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001015 //Technically you could have this CC
1016 case ISD::SETULE: Opc = Alpha::BEQ; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001017 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
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001032 //Any comparison between 2 values should be codegened as an folded
1033 //branch, as moving CC to the integer register is very expensive
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001034 //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 Lenharthd4653b12005-06-27 17:39:17 +00001175 BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV)
1176 .addReg(Tmp1);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001177// // Spill the cond to memory and reload it from there.
1178// unsigned Tmp4 = MakeReg(MVT::f64);
1179// MoveIntFP(Tmp1, Tmp4, true);
1180// //now ideally, we don't have to do anything to the flag...
1181// // Get the condition into the zero flag.
1182// BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
Andrew Lenharth45859692005-03-03 21:47:53 +00001183 return Result;
1184 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001185 }
1186
Andrew Lenharthc1faced2005-02-01 01:37:24 +00001187 case ISD::FP_ROUND:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001188 assert (DestType == MVT::f32 &&
1189 N.getOperand(0).getValueType() == MVT::f64 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001190 "only f64 to f32 conversion supported here");
Andrew Lenharthc1faced2005-02-01 01:37:24 +00001191 Tmp1 = SelectExpr(N.getOperand(0));
1192 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
1193 return Result;
1194
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001195 case ISD::FP_EXTEND:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001196 assert (DestType == MVT::f64 &&
1197 N.getOperand(0).getValueType() == MVT::f32 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001198 "only f32 to f64 conversion supported here");
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001199 Tmp1 = SelectExpr(N.getOperand(0));
1200 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
1201 return Result;
1202
Andrew Lenharth40831c52005-01-28 06:57:18 +00001203 case ISD::ConstantFP:
1204 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
1205 if (CN->isExactlyValue(+0.0)) {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001206 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31)
1207 .addReg(Alpha::F31);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001208 } else if ( CN->isExactlyValue(-0.0)) {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001209 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31)
1210 .addReg(Alpha::F31);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001211 } else {
1212 abort();
1213 }
1214 }
1215 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001216
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001217 case ISD::SDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001218 case ISD::MUL:
1219 case ISD::ADD:
1220 case ISD::SUB:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001221 switch( opcode ) {
1222 case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS;
1223 break;
1224 case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS;
1225 break;
1226 case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS;
1227 break;
1228 case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS;
1229 break;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001230 };
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001231
1232 ConstantFPSDNode *CN;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001233 if (opcode == ISD::SUB
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001234 && (CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
1235 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1236 {
1237 Tmp2 = SelectExpr(N.getOperand(1));
1238 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp2).addReg(Tmp2);
1239 } else {
1240 Tmp1 = SelectExpr(N.getOperand(0));
1241 Tmp2 = SelectExpr(N.getOperand(1));
1242 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1243 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001244 return Result;
1245
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001246 case ISD::SINT_TO_FP:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001247 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001248 assert (N.getOperand(0).getValueType() == MVT::i64
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001249 && "only quads can be loaded from");
Andrew Lenharth40831c52005-01-28 06:57:18 +00001250 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001251 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001252 MoveInt2FP(Tmp1, Tmp2, true);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001253 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
1254 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001255 return Result;
1256 }
1257 }
1258 assert(0 && "should not get here");
1259 return 0;
1260}
1261
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001262unsigned AlphaISel::SelectExpr(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001263 unsigned Result;
Andrew Lenharth2966e842005-04-07 18:15:28 +00001264 unsigned Tmp1, Tmp2 = 0, Tmp3;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001265 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001266 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001267
1268 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001269 MVT::ValueType DestType = N.getValueType();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001270
1271 unsigned &Reg = ExprMap[N];
1272 if (Reg) return Reg;
1273
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001274 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::TAILCALL)
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001275 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001276 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001277 else {
1278 // If this is a call instruction, make sure to prepare ALL of the result
1279 // values as well as the chain.
1280 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001281 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001282 else {
1283 Result = MakeReg(Node->getValueType(0));
1284 ExprMap[N.getValue(0)] = Result;
1285 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1286 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001287 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001288 }
1289 }
1290
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001291 if ((DestType == MVT::f64 || DestType == MVT::f32)
Andrew Lenharthba5dc442005-06-29 12:23:34 +00001292 && opcode != ISD::CALL && opcode != ISD::TAILCALL
1293 && opcode != ISD::CopyFromReg && opcode != ISD::LOAD)
Andrew Lenharth40831c52005-01-28 06:57:18 +00001294 return SelectExprFP(N, Result);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001295
Andrew Lenharth40831c52005-01-28 06:57:18 +00001296 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001297 default:
1298 Node->dump();
1299 assert(0 && "Node not handled!\n");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001300
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001301 case ISD::CTPOP:
1302 case ISD::CTTZ:
1303 case ISD::CTLZ:
1304 Opc = opcode == ISD::CTPOP ? Alpha::CTPOP :
1305 (opcode == ISD::CTTZ ? Alpha::CTTZ : Alpha::CTLZ);
1306 Tmp1 = SelectExpr(N.getOperand(0));
1307 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1308 return Result;
1309
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001310 case ISD::MULHU:
1311 Tmp1 = SelectExpr(N.getOperand(0));
1312 Tmp2 = SelectExpr(N.getOperand(1));
1313 BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth706be912005-04-07 13:55:53 +00001314 return Result;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001315 case ISD::MULHS:
1316 {
1317 //MULHU - Ra<63>*Rb - Rb<63>*Ra
1318 Tmp1 = SelectExpr(N.getOperand(0));
1319 Tmp2 = SelectExpr(N.getOperand(1));
1320 Tmp3 = MakeReg(MVT::i64);
1321 BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1322 unsigned V1 = MakeReg(MVT::i64);
1323 unsigned V2 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001324 BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31)
1325 .addReg(Tmp1);
1326 BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31)
1327 .addReg(Tmp2);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001328 unsigned IRes = MakeReg(MVT::i64);
1329 BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
1330 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
1331 return Result;
1332 }
Andrew Lenharth7332f3e2005-04-02 19:11:07 +00001333 case ISD::UNDEF: {
1334 BuildMI(BB, Alpha::IDEF, 0, Result);
1335 return Result;
1336 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001337
Andrew Lenharth032f2352005-02-22 21:59:48 +00001338 case ISD::DYNAMIC_STACKALLOC:
1339 // Generate both result values.
Andrew Lenharth3a7118d2005-02-23 17:33:42 +00001340 if (Result != notIn)
1341 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth032f2352005-02-22 21:59:48 +00001342 else
1343 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1344
1345 // FIXME: We are currently ignoring the requested alignment for handling
1346 // greater than the stack alignment. This will need to be revisited at some
1347 // point. Align = N.getOperand(2);
1348
1349 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1350 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1351 std::cerr << "Cannot allocate stack object with greater alignment than"
1352 << " the stack alignment yet!";
1353 abort();
1354 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001355
Andrew Lenharth032f2352005-02-22 21:59:48 +00001356 Select(N.getOperand(0));
1357 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1358 {
1359 if (CN->getValue() < 32000)
1360 {
1361 BuildMI(BB, Alpha::LDA, 2, Alpha::R30)
1362 .addImm(-CN->getValue()).addReg(Alpha::R30);
1363 } else {
1364 Tmp1 = SelectExpr(N.getOperand(1));
1365 // Subtract size from stack pointer, thereby allocating some space.
1366 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1367 }
1368 } else {
1369 Tmp1 = SelectExpr(N.getOperand(1));
1370 // Subtract size from stack pointer, thereby allocating some space.
1371 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1372 }
1373
1374 // Put a pointer to the space into the result register, by copying the stack
1375 // pointer.
Andrew Lenharth7bc47022005-02-22 23:29:25 +00001376 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
Andrew Lenharth032f2352005-02-22 21:59:48 +00001377 return Result;
1378
Andrew Lenharth02c318e2005-06-27 21:02:56 +00001379 case ISD::ConstantPool:
1380 Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1381 AlphaLowering.restoreGP(BB);
1382 Tmp2 = MakeReg(MVT::i64);
1383 BuildMI(BB, Alpha::LDAHr, 2, Tmp2).addConstantPoolIndex(Tmp1)
1384 .addReg(Alpha::R29);
1385 BuildMI(BB, Alpha::LDAr, 2, Result).addConstantPoolIndex(Tmp1)
1386 .addReg(Tmp2);
1387 return Result;
Andrew Lenharth2c594352005-01-29 15:42:07 +00001388
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001389 case ISD::FrameIndex:
Andrew Lenharth032f2352005-02-22 21:59:48 +00001390 BuildMI(BB, Alpha::LDA, 2, Result)
1391 .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
1392 .addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001393 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001394
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001395 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001396 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001397 case ISD::SEXTLOAD:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001398 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001399 {
1400 // Make sure we generate both values.
1401 if (Result != notIn)
1402 ExprMap[N.getValue(1)] = notIn; // Generate the token
1403 else
1404 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001405
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001406 SDOperand Chain = N.getOperand(0);
1407 SDOperand Address = N.getOperand(1);
1408 Select(Chain);
1409
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001410 bool fpext = true;
1411
Andrew Lenharth03824012005-02-07 05:55:55 +00001412 if (opcode == ISD::LOAD)
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001413 switch (Node->getValueType(0)) {
1414 default: Node->dump(); assert(0 && "Bad load!");
1415 case MVT::i64: Opc = Alpha::LDQ; break;
1416 case MVT::f64: Opc = Alpha::LDT; break;
1417 case MVT::f32: Opc = Alpha::LDS; break;
1418 }
Andrew Lenharth03824012005-02-07 05:55:55 +00001419 else
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001420 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
1421 default: Node->dump(); assert(0 && "Bad sign extend!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001422 case MVT::i32: Opc = Alpha::LDL;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001423 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001424 case MVT::i16: Opc = Alpha::LDWU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001425 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001426 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Misha Brukman4633f1c2005-04-21 23:13:11 +00001427 case MVT::i8: Opc = Alpha::LDBU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001428 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001429 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001430
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001431 int i = 0, j = 0;
1432 if (EnableAlphaLSMark) {
1433 i = getValueOffset(dyn_cast<SrcValueSDNode>(N.getOperand(2))
1434 ->getValue());
1435 j = getFunctionOffset(BB->getParent()->getFunction());
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001436 }
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001437 if (GlobalAddressSDNode *GASD =
1438 dyn_cast<GlobalAddressSDNode>(Address)) {
1439 if (GASD->getGlobal()->isExternal()) {
1440 Tmp1 = SelectExpr(Address);
1441 if (EnableAlphaLSMark)
1442 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
1443 BuildMI(BB, Opc, 2, Result).addImm(0).addReg(Tmp1);
1444 } else {
1445 Tmp1 = MakeReg(MVT::i64);
1446 AlphaLowering.restoreGP(BB);
1447 BuildMI(BB, Alpha::LDAHr, 2, Tmp1)
1448 .addGlobalAddress(GASD->getGlobal()).addReg(Alpha::R29);
1449 if (EnableAlphaLSMark)
1450 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
1451 BuildMI(BB, GetRelVersion(Opc), 2, Result)
1452 .addGlobalAddress(GASD->getGlobal()).addReg(Tmp1);
1453 }
1454 } else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001455 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001456 has_sym = true;
Andrew Lenharthfe895e32005-06-27 17:15:36 +00001457 Tmp1 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001458 BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CP->getIndex())
1459 .addReg(Alpha::R29);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001460 if (EnableAlphaLSMark)
1461 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
1462 BuildMI(BB, GetRelVersion(Opc), 2, Result)
1463 .addConstantPoolIndex(CP->getIndex()).addReg(Tmp1);
1464 } else if(Address.getOpcode() == ISD::FrameIndex) {
1465 if (EnableAlphaLSMark)
1466 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
Andrew Lenharth032f2352005-02-22 21:59:48 +00001467 BuildMI(BB, Opc, 2, Result)
1468 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1469 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001470 } else {
1471 long offset;
1472 SelectAddr(Address, Tmp1, offset);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001473 if (EnableAlphaLSMark)
1474 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001475 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1476 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001477 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001478 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001479
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001480 case ISD::GlobalAddress:
1481 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001482 has_sym = true;
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001483
1484 if (EnableAlphaLSMark)
1485 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(0).addImm(0).addImm(getUID());
1486
1487 BuildMI(BB, Alpha::LDQl, 2, Result)
Andrew Lenharthc95d9842005-06-27 21:11:40 +00001488 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal())
1489 .addReg(Alpha::R29);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001490 return Result;
1491
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001492 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001493 case ISD::CALL:
1494 {
1495 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001496
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001497 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001498 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001499
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001500 //grab the arguments
1501 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001502 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001503 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001504 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001505
Andrew Lenharth684f2292005-01-30 00:35:27 +00001506 //in reg args
1507 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001508 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001509 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001510 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +00001511 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001512 Alpha::F19, Alpha::F20, Alpha::F21};
1513 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001514 default:
1515 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001516 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001517 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001518 N.getOperand(i+2).getValueType() << "\n";
1519 assert(0 && "Unknown value type for call");
1520 case MVT::i1:
1521 case MVT::i8:
1522 case MVT::i16:
1523 case MVT::i32:
1524 case MVT::i64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001525 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i])
1526 .addReg(argvregs[i]);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001527 break;
1528 case MVT::f32:
1529 case MVT::f64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001530 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i])
1531 .addReg(argvregs[i]);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001532 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001533 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001534 }
Andrew Lenharth684f2292005-01-30 00:35:27 +00001535 //in mem args
1536 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001537 {
1538 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001539 default:
1540 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001541 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001542 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001543 N.getOperand(i+2).getValueType() << "\n";
1544 assert(0 && "Unknown value type for call");
1545 case MVT::i1:
1546 case MVT::i8:
1547 case MVT::i16:
1548 case MVT::i32:
1549 case MVT::i64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001550 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
1551 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001552 break;
1553 case MVT::f32:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001554 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
1555 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001556 break;
1557 case MVT::f64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001558 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
1559 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001560 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001561 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001562 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001563 //build the right kind of call
1564 if (GlobalAddressSDNode *GASD =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001565 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001566 {
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001567 if (GASD->getGlobal()->isExternal()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001568 //use safe calling convention
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001569 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001570 has_sym = true;
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001571 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal());
1572 } else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001573 //use PC relative branch call
Andrew Lenharth1e0d9bd2005-04-14 17:34:20 +00001574 AlphaLowering.restoreGP(BB);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001575 BuildMI(BB, Alpha::BSR, 1, Alpha::R26)
1576 .addGlobalAddress(GASD->getGlobal(),true);
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001577 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001578 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001579 else if (ExternalSymbolSDNode *ESSDN =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001580 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001581 {
1582 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001583 has_sym = true;
Andrew Lenharthba05ad62005-03-30 18:22:52 +00001584 BuildMI(BB, Alpha::CALL, 1).addExternalSymbol(ESSDN->getSymbol(), true);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001585 } else {
1586 //no need to restore GP as we are doing an indirect call
1587 Tmp1 = SelectExpr(N.getOperand(1));
1588 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
1589 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
1590 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001591
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001592 //push the result into a virtual register
Misha Brukman4633f1c2005-04-21 23:13:11 +00001593
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001594 switch (Node->getValueType(0)) {
1595 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001596 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001597 case MVT::i1:
1598 case MVT::i8:
1599 case MVT::i16:
1600 case MVT::i32:
1601 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001602 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
1603 break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001604 case MVT::f32:
1605 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001606 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
1607 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001608 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001609 return Result+N.ResNo;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001610 }
1611
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001612 case ISD::SIGN_EXTEND_INREG:
1613 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001614 //do SDIV opt for all levels of ints if not dividing by a constant
1615 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
1616 && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001617 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001618 unsigned Tmp4 = MakeReg(MVT::f64);
1619 unsigned Tmp5 = MakeReg(MVT::f64);
1620 unsigned Tmp6 = MakeReg(MVT::f64);
1621 unsigned Tmp7 = MakeReg(MVT::f64);
1622 unsigned Tmp8 = MakeReg(MVT::f64);
1623 unsigned Tmp9 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001624
1625 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1626 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1627 MoveInt2FP(Tmp1, Tmp4, true);
1628 MoveInt2FP(Tmp2, Tmp5, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001629 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Tmp4);
1630 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Tmp5);
1631 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
1632 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Tmp8);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001633 MoveFP2Int(Tmp9, Result, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001634 return Result;
1635 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001636
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001637 //Alpha has instructions for a bunch of signed 32 bit stuff
1638 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001639 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001640 switch (N.getOperand(0).getOpcode()) {
1641 case ISD::ADD:
1642 case ISD::SUB:
1643 case ISD::MUL:
1644 {
1645 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
1646 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
1647 //FIXME: first check for Scaled Adds and Subs!
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001648 ConstantSDNode* CSD = NULL;
1649 if(!isMul && N.getOperand(0).getOperand(0).getOpcode() == ISD::SHL &&
1650 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(0).getOperand(1))) &&
1651 (CSD->getValue() == 2 || CSD->getValue() == 3))
1652 {
1653 bool use4 = CSD->getValue() == 2;
1654 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1655 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1656 BuildMI(BB, isAdd?(use4?Alpha::S4ADDL:Alpha::S8ADDL):(use4?Alpha::S4SUBL:Alpha::S8SUBL),
1657 2,Result).addReg(Tmp1).addReg(Tmp2);
1658 }
1659 else if(isAdd && N.getOperand(0).getOperand(1).getOpcode() == ISD::SHL &&
1660 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1).getOperand(1))) &&
1661 (CSD->getValue() == 2 || CSD->getValue() == 3))
1662 {
1663 bool use4 = CSD->getValue() == 2;
1664 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1665 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1666 BuildMI(BB, use4?Alpha::S4ADDL:Alpha::S8ADDL, 2,Result).addReg(Tmp1).addReg(Tmp2);
1667 }
1668 else if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001669 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
1670 { //Normal imm add/sub
1671 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001672 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001673 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
1674 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001675 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001676 else
1677 { //Normal add/sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001678 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULL : Alpha::SUBL);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001679 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001680 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001681 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1682 }
1683 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001684 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001685 default: break; //Fall Though;
1686 }
1687 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001688 Tmp1 = SelectExpr(N.getOperand(0));
1689 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001690 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001691 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001692 {
1693 default:
1694 Node->dump();
1695 assert(0 && "Sign Extend InReg not there yet");
1696 break;
1697 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001698 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001699 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001700 break;
1701 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001702 case MVT::i16:
1703 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
1704 break;
1705 case MVT::i8:
1706 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
1707 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +00001708 case MVT::i1:
1709 Tmp2 = MakeReg(MVT::i64);
1710 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001711 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +00001712 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001713 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001714 return Result;
1715 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001716
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001717 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001718 {
1719 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1720 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
Andrew Lenharth694c2982005-06-26 23:01:11 +00001721 bool isConst = false;
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001722 int dir;
Misha Brukman7847fca2005-04-22 17:54:37 +00001723
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001724 //Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001725 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001726 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth694c2982005-06-26 23:01:11 +00001727 isConst = true;
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001728
1729 switch (SetCC->getCondition()) {
1730 default: Node->dump(); assert(0 && "Unknown integer comparison!");
Andrew Lenharth694c2982005-06-26 23:01:11 +00001731 case ISD::SETEQ:
1732 Opc = isConst ? Alpha::CMPEQi : Alpha::CMPEQ; dir=1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001733 case ISD::SETLT:
Andrew Lenharth694c2982005-06-26 23:01:11 +00001734 Opc = isConst ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001735 case ISD::SETLE:
Andrew Lenharth694c2982005-06-26 23:01:11 +00001736 Opc = isConst ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
1737 case ISD::SETGT: Opc = Alpha::CMPLT; dir = 2; break;
1738 case ISD::SETGE: Opc = Alpha::CMPLE; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001739 case ISD::SETULT:
Andrew Lenharth694c2982005-06-26 23:01:11 +00001740 Opc = isConst ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
1741 case ISD::SETUGT: Opc = Alpha::CMPULT; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001742 case ISD::SETULE:
Andrew Lenharth694c2982005-06-26 23:01:11 +00001743 Opc = isConst ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
1744 case ISD::SETUGE: Opc = Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001745 case ISD::SETNE: {//Handle this one special
1746 //std::cerr << "Alpha does not have a setne.\n";
1747 //abort();
1748 Tmp1 = SelectExpr(N.getOperand(0));
1749 Tmp2 = SelectExpr(N.getOperand(1));
1750 Tmp3 = MakeReg(MVT::i64);
1751 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001752 //Remeber we have the Inv for this CC
1753 CCInvMap[N] = Tmp3;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001754 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001755 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001756 return Result;
1757 }
1758 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001759 if (dir == 1) {
1760 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth694c2982005-06-26 23:01:11 +00001761 if (isConst) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001762 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1763 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1764 } else {
1765 Tmp2 = SelectExpr(N.getOperand(1));
1766 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1767 }
Andrew Lenharth694c2982005-06-26 23:01:11 +00001768 } else { //if (dir == 2) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001769 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth694c2982005-06-26 23:01:11 +00001770 Tmp2 = SelectExpr(N.getOperand(0));
1771 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001772 }
1773 } else {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001774 //do the comparison
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001775 Tmp1 = MakeReg(MVT::f64);
1776 bool inv = SelectFPSetCC(N, Tmp1);
1777
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001778 //now arrange for Result (int) to have a 1 or 0
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001779 Tmp2 = MakeReg(MVT::i64);
1780 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001781 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001782 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001783 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001784 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001785 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001786 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001787
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001788 case ISD::CopyFromReg:
1789 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001790 ++count_ins;
1791
Andrew Lenharth40831c52005-01-28 06:57:18 +00001792 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001793 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001794 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001795 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001796 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001797
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001798 SDOperand Chain = N.getOperand(0);
1799
1800 Select(Chain);
1801 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1802 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001803 if (DestType == MVT::f32 || DestType == MVT::f64)
1804 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
1805 else
1806 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001807 return Result;
1808 }
1809
Misha Brukman4633f1c2005-04-21 23:13:11 +00001810 //Most of the plain arithmetic and logic share the same form, and the same
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001811 //constant immediate test
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001812 case ISD::XOR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001813 //Match Not
1814 if (N.getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001815 cast<ConstantSDNode>(N.getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001816 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001817 Tmp1 = SelectExpr(N.getOperand(0));
1818 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1819 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001820 }
1821 //Fall through
1822 case ISD::AND:
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001823 //handle zap
1824 if (opcode == ISD::AND && N.getOperand(1).getOpcode() == ISD::Constant)
1825 {
1826 uint64_t k = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1827 unsigned int build = 0;
1828 for(int i = 0; i < 8; ++i)
1829 {
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001830 if ((k & 0x00FF) == 0x00FF)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001831 build |= 1 << i;
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001832 else if ((k & 0x00FF) != 0)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001833 { build = 0; break; }
1834 k >>= 8;
1835 }
1836 if (build)
1837 {
1838 Tmp1 = SelectExpr(N.getOperand(0));
1839 BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1840 return Result;
1841 }
1842 }
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001843 case ISD::OR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001844 //Check operand(0) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001845 if (N.getOperand(0).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001846 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001847 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getSignExtended()
1848 == -1) {
1849 switch(opcode) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001850 case ISD::AND: Opc = Alpha::BIC; break;
1851 case ISD::OR: Opc = Alpha::ORNOT; break;
1852 case ISD::XOR: Opc = Alpha::EQV; break;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001853 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001854 Tmp1 = SelectExpr(N.getOperand(1));
1855 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1856 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1857 return Result;
1858 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001859 //Check operand(1) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001860 if (N.getOperand(1).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001861 N.getOperand(1).getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001862 cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getSignExtended()
1863 == -1) {
1864 switch(opcode) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001865 case ISD::AND: Opc = Alpha::BIC; break;
1866 case ISD::OR: Opc = Alpha::ORNOT; break;
1867 case ISD::XOR: Opc = Alpha::EQV; break;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001868 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001869 Tmp1 = SelectExpr(N.getOperand(0));
1870 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1871 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1872 return Result;
1873 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001874 //Fall through
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001875 case ISD::SHL:
1876 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001877 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001878 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001879 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1880 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001881 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001882 {
1883 switch(opcode) {
1884 case ISD::AND: Opc = Alpha::ANDi; break;
1885 case ISD::OR: Opc = Alpha::BISi; break;
1886 case ISD::XOR: Opc = Alpha::XORi; break;
1887 case ISD::SHL: Opc = Alpha::SLi; break;
1888 case ISD::SRL: Opc = Alpha::SRLi; break;
1889 case ISD::SRA: Opc = Alpha::SRAi; break;
1890 case ISD::MUL: Opc = Alpha::MULQi; break;
1891 };
1892 Tmp1 = SelectExpr(N.getOperand(0));
1893 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1894 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1895 } else {
1896 switch(opcode) {
1897 case ISD::AND: Opc = Alpha::AND; break;
1898 case ISD::OR: Opc = Alpha::BIS; break;
1899 case ISD::XOR: Opc = Alpha::XOR; break;
1900 case ISD::SHL: Opc = Alpha::SL; break;
1901 case ISD::SRL: Opc = Alpha::SRL; break;
1902 case ISD::SRA: Opc = Alpha::SRA; break;
1903 case ISD::MUL: Opc = Alpha::MULQ; break;
1904 };
1905 Tmp1 = SelectExpr(N.getOperand(0));
1906 Tmp2 = SelectExpr(N.getOperand(1));
1907 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1908 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001909 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001910
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001911 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001912 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001913 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001914 bool isAdd = opcode == ISD::ADD;
1915
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001916 //first check for Scaled Adds and Subs!
1917 //Valid for add and sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001918 ConstantSDNode* CSD = NULL;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001919 if(N.getOperand(0).getOpcode() == ISD::SHL &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001920 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
1921 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001922 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001923 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001924 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001925 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) && CSD->getValue() <= 255)
1926 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1927 2, Result).addReg(Tmp2).addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001928 else {
1929 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001930 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1931 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001932 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001933 }
1934 //Position prevents subs
Andrew Lenharth273a1f92005-04-07 14:18:13 +00001935 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001936 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) &&
1937 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001938 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001939 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001940 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001941 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(0))) && CSD->getValue() <= 255)
1942 BuildMI(BB, use4?Alpha::S4ADDQi:Alpha::S8ADDQi, 2, Result).addReg(Tmp2)
1943 .addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001944 else {
1945 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001946 BuildMI(BB, use4?Alpha::S4ADDQ:Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001947 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001948 }
1949 //small addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001950 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
1951 CSD->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001952 { //Normal imm add/sub
1953 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1954 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001955 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CSD->getValue());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001956 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001957 //larger addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001958 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
1959 CSD->getSignExtended() <= 32767 &&
1960 CSD->getSignExtended() >= -32767)
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001961 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001962 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001963 Tmp2 = (long)CSD->getSignExtended();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001964 if (!isAdd)
1965 Tmp2 = -Tmp2;
1966 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001967 }
1968 //give up and do the operation
1969 else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001970 //Normal add/sub
1971 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1972 Tmp1 = SelectExpr(N.getOperand(0));
1973 Tmp2 = SelectExpr(N.getOperand(1));
1974 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1975 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001976 return Result;
1977 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001978
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001979 case ISD::SDIV:
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001980 {
Andrew Lenhartha565c272005-04-06 22:03:13 +00001981 ConstantSDNode* CSD;
1982 //check if we can convert into a shift!
1983 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
1984 (int64_t)CSD->getSignExtended() != 0 &&
1985 ExactLog2(abs((int64_t)CSD->getSignExtended())) != 0)
1986 {
1987 unsigned k = ExactLog2(abs(CSD->getSignExtended()));
1988 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenhartha565c272005-04-06 22:03:13 +00001989 if (k == 1)
1990 Tmp2 = Tmp1;
1991 else
1992 {
1993 Tmp2 = MakeReg(MVT::i64);
1994 BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
1995 }
1996 Tmp3 = MakeReg(MVT::i64);
1997 BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
1998 unsigned Tmp4 = MakeReg(MVT::i64);
1999 BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
2000 if ((int64_t)CSD->getSignExtended() > 0)
2001 BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
2002 else
2003 {
2004 unsigned Tmp5 = MakeReg(MVT::i64);
2005 BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
2006 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
2007 }
2008 return Result;
2009 }
2010 }
2011 //Else fall through
2012
2013 case ISD::UDIV:
2014 {
2015 ConstantSDNode* CSD;
2016 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
2017 ((int64_t)CSD->getSignExtended() >= 2 ||
2018 (int64_t)CSD->getSignExtended() <= -2))
2019 {
2020 // If this is a divide by constant, we can emit code using some magic
2021 // constants to implement it as a multiply instead.
2022 ExprMap.erase(N);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002023 if (opcode == ISD::SDIV)
Andrew Lenhartha565c272005-04-06 22:03:13 +00002024 return SelectExpr(BuildSDIVSequence(N));
2025 else
2026 return SelectExpr(BuildUDIVSequence(N));
2027 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002028 }
2029 //else fall though
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002030 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +00002031 case ISD::SREM:
Misha Brukman4633f1c2005-04-21 23:13:11 +00002032 //FIXME: alpha really doesn't support any of these operations,
Andrew Lenharth40831c52005-01-28 06:57:18 +00002033 // the ops are expanded into special library calls with
2034 // special calling conventions
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002035 //Restore GP because it is a call after all...
Andrew Lenharth40831c52005-01-28 06:57:18 +00002036 switch(opcode) {
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00002037 case ISD::UREM: Opc = Alpha::REMQU; break;
2038 case ISD::SREM: Opc = Alpha::REMQ; break;
2039 case ISD::UDIV: Opc = Alpha::DIVQU; break;
2040 case ISD::SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002041 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002042 Tmp1 = SelectExpr(N.getOperand(0));
2043 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth33819132005-03-04 20:09:23 +00002044 //set up regs explicitly (helps Reg alloc)
2045 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002046 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00002047 AlphaLowering.restoreGP(BB);
Andrew Lenharth33819132005-03-04 20:09:23 +00002048 BuildMI(BB, Opc, 2).addReg(Alpha::R24).addReg(Alpha::R25);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002049 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002050 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002051
Andrew Lenharthe76797c2005-02-01 20:40:27 +00002052 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002053 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002054 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002055 assert (DestType == MVT::i64 && "only quads can be loaded to");
2056 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00002057 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002058 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002059 if (SrcType == MVT::f32)
Misha Brukman7847fca2005-04-22 17:54:37 +00002060 {
2061 Tmp2 = MakeReg(MVT::f64);
2062 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
2063 Tmp1 = Tmp2;
2064 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002065 Tmp2 = MakeReg(MVT::f64);
2066 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00002067 MoveFP2Int(Tmp2, Result, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002068
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002069 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002070 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002071
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002072 case ISD::SELECT:
2073 {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002074 //FIXME: look at parent to decide if intCC can be folded, or if setCC(FP)
2075 //and can save stack use
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002076 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002077 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2078 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002079 // Get the condition into the zero flag.
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002080 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002081
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002082 SDOperand CC = N.getOperand(0);
2083 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
2084
Misha Brukman4633f1c2005-04-21 23:13:11 +00002085 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002086 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
2087 { //FP Setcc -> Int Select
Misha Brukman7847fca2005-04-22 17:54:37 +00002088 Tmp1 = MakeReg(MVT::f64);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002089 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2090 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Misha Brukman7847fca2005-04-22 17:54:37 +00002091 bool inv = SelectFPSetCC(CC, Tmp1);
2092 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
2093 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
2094 return Result;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002095 }
2096 if (CC.getOpcode() == ISD::SETCC) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002097 //Int SetCC -> Select
2098 //Dropping the CC is only useful if we are comparing to 0
2099 if((SetCC->getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth694c2982005-06-26 23:01:11 +00002100 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0))
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002101 {
2102 //figure out a few things
Andrew Lenharth694c2982005-06-26 23:01:11 +00002103 bool useImm = N.getOperand(2).getOpcode() == ISD::Constant &&
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002104 cast<ConstantSDNode>(N.getOperand(2))->getValue() <= 255;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002105
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002106 //Fix up CC
2107 ISD::CondCode cCode= SetCC->getCondition();
Andrew Lenharth694c2982005-06-26 23:01:11 +00002108 if (useImm) //Invert sense to get Imm field right
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002109 cCode = ISD::getSetCCInverse(cCode, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002110
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002111 //Choose the CMOV
2112 switch (cCode) {
2113 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002114 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
2115 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
2116 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
2117 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
2118 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
2119 case ISD::SETULT: assert(0 && "unsigned < 0 is never true"); break;
2120 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2121 //Technically you could have this CC
2122 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
2123 case ISD::SETUGE: assert(0 && "unsgined >= 0 is always true"); break;
2124 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002125 }
Andrew Lenharth694c2982005-06-26 23:01:11 +00002126 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002127
Andrew Lenharth694c2982005-06-26 23:01:11 +00002128 if (useImm) {
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002129 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
2130 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
Misha Brukman7847fca2005-04-22 17:54:37 +00002131 .addImm(cast<ConstantSDNode>(N.getOperand(2))->getValue())
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002132 .addReg(Tmp1);
2133 } else {
2134 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2135 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2136 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
2137 }
2138 return Result;
2139 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002140 //Otherwise, fall though
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002141 }
2142 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002143 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2144 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002145 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3)
2146 .addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002147
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002148 return Result;
2149 }
2150
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002151 case ISD::Constant:
2152 {
Andrew Lenharthc0513832005-03-29 19:24:04 +00002153 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002154 if (val <= IMM_HIGH && val >= IMM_LOW) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002155 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002156 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002157 else if (val <= (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT &&
2158 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
2159 Tmp1 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002160 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val))
2161 .addReg(Alpha::R31);
Misha Brukman7847fca2005-04-22 17:54:37 +00002162 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002163 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002164 else {
2165 MachineConstantPool *CP = BB->getParent()->getConstantPool();
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002166 ConstantUInt *C =
2167 ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002168 unsigned CPI = CP->getConstantPoolIndex(C);
2169 AlphaLowering.restoreGP(BB);
Andrew Lenharthfe895e32005-06-27 17:15:36 +00002170 has_sym = true;
2171 Tmp1 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002172 BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CPI)
2173 .addReg(Alpha::R29);
2174 BuildMI(BB, Alpha::LDQr, 2, Result).addConstantPoolIndex(CPI)
2175 .addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002176 }
2177 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002178 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002179 }
2180
2181 return 0;
2182}
2183
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002184void AlphaISel::Select(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002185 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00002186 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002187
Nate Begeman85fdeb22005-03-24 04:39:54 +00002188 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002189 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002190
2191 SDNode *Node = N.Val;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002192
Andrew Lenharth760270d2005-02-07 23:02:23 +00002193 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002194
2195 default:
2196 Node->dump(); std::cerr << "\n";
2197 assert(0 && "Node not handled yet!");
2198
2199 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00002200 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002201 return;
2202 }
2203
2204 case ISD::BR: {
2205 MachineBasicBlock *Dest =
2206 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2207
2208 Select(N.getOperand(0));
2209 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
2210 return;
2211 }
2212
2213 case ISD::ImplicitDef:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002214 ++count_ins;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002215 Select(N.getOperand(0));
2216 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
2217 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002218
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002219 case ISD::EntryToken: return; // Noop
2220
2221 case ISD::TokenFactor:
2222 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2223 Select(Node->getOperand(i));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002224
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002225 //N.Val->dump(); std::cerr << "\n";
2226 //assert(0 && "Node not handled yet!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00002227
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002228 return;
2229
2230 case ISD::CopyToReg:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002231 ++count_outs;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002232 Select(N.getOperand(0));
2233 Tmp1 = SelectExpr(N.getOperand(1));
2234 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002235
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002236 if (Tmp1 != Tmp2) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002237 if (N.getOperand(1).getValueType() == MVT::f64 ||
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002238 N.getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00002239 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2240 else
2241 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002242 }
2243 return;
2244
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002245 case ISD::RET:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002246 ++count_outs;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002247 switch (N.getNumOperands()) {
2248 default:
2249 std::cerr << N.getNumOperands() << "\n";
2250 for (unsigned i = 0; i < N.getNumOperands(); ++i)
2251 std::cerr << N.getOperand(i).getValueType() << "\n";
2252 Node->dump();
2253 assert(0 && "Unknown return instruction!");
2254 case 2:
2255 Select(N.getOperand(0));
2256 Tmp1 = SelectExpr(N.getOperand(1));
2257 switch (N.getOperand(1).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002258 default: Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002259 assert(0 && "All other types should have been promoted!!");
2260 case MVT::f64:
2261 case MVT::f32:
2262 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
2263 break;
2264 case MVT::i32:
2265 case MVT::i64:
2266 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
2267 break;
2268 }
2269 break;
2270 case 1:
2271 Select(N.getOperand(0));
2272 break;
2273 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002274 // Just emit a 'ret' instruction
Andrew Lenharth6968bff2005-06-27 23:24:11 +00002275 AlphaLowering.restoreRA(BB);
2276 BuildMI(BB, Alpha::RET, 1, Alpha::R31).addReg(Alpha::R26);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002277 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002278
Misha Brukman4633f1c2005-04-21 23:13:11 +00002279 case ISD::TRUNCSTORE:
2280 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002281 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002282 SDOperand Chain = N.getOperand(0);
2283 SDOperand Value = N.getOperand(1);
2284 SDOperand Address = N.getOperand(2);
2285 Select(Chain);
2286
2287 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00002288
2289 if (opcode == ISD::STORE) {
2290 switch(Value.getValueType()) {
2291 default: assert(0 && "unknown Type in store");
2292 case MVT::i64: Opc = Alpha::STQ; break;
2293 case MVT::f64: Opc = Alpha::STT; break;
2294 case MVT::f32: Opc = Alpha::STS; break;
2295 }
2296 } else { //ISD::TRUNCSTORE
2297 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2298 default: assert(0 && "unknown Type in store");
2299 case MVT::i1: //FIXME: DAG does not promote this load
2300 case MVT::i8: Opc = Alpha::STB; break;
2301 case MVT::i16: Opc = Alpha::STW; break;
2302 case MVT::i32: Opc = Alpha::STL; break;
2303 }
Andrew Lenharth65838902005-02-06 16:22:15 +00002304 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00002305
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00002306 int i = 0, j = 0;
2307 if (EnableAlphaLSMark) {
2308 i =
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002309 getValueOffset(dyn_cast<SrcValueSDNode>(N.getOperand(3))->getValue());
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00002310 j = getFunctionOffset(BB->getParent()->getFunction());
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002311 }
2312
Andrew Lenharthfce587e2005-06-29 00:39:17 +00002313 if (GlobalAddressSDNode *GASD =
2314 dyn_cast<GlobalAddressSDNode>(Address)) {
2315 if (GASD->getGlobal()->isExternal()) {
2316 Tmp2 = SelectExpr(Address);
2317 if (EnableAlphaLSMark)
2318 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
2319 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(0).addReg(Tmp2);
2320 } else {
2321 Tmp2 = MakeReg(MVT::i64);
2322 AlphaLowering.restoreGP(BB);
2323 BuildMI(BB, Alpha::LDAHr, 2, Tmp2)
2324 .addGlobalAddress(GASD->getGlobal()).addReg(Alpha::R29);
2325 if (EnableAlphaLSMark)
2326 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
2327 BuildMI(BB, GetRelVersion(Opc), 3).addReg(Tmp1)
2328 .addGlobalAddress(GASD->getGlobal()).addReg(Tmp2);
2329 }
2330 } else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00002331 if (EnableAlphaLSMark)
2332 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
Andrew Lenharth032f2352005-02-22 21:59:48 +00002333 BuildMI(BB, Opc, 3).addReg(Tmp1)
2334 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
2335 .addReg(Alpha::F31);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00002336 } else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002337 long offset;
2338 SelectAddr(Address, Tmp2, offset);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00002339 if (EnableAlphaLSMark)
2340 BuildMI(BB, Alpha::MEMLABEL, 3).addImm(j).addImm(i).addImm(getUID());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002341 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2342 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002343 return;
2344 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002345
2346 case ISD::EXTLOAD:
2347 case ISD::SEXTLOAD:
2348 case ISD::ZEXTLOAD:
2349 case ISD::LOAD:
2350 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002351 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002352 case ISD::CALL:
Andrew Lenharth032f2352005-02-22 21:59:48 +00002353 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002354 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002355 SelectExpr(N);
2356 return;
2357
Chris Lattner16cd04d2005-05-12 23:24:06 +00002358 case ISD::CALLSEQ_START:
2359 case ISD::CALLSEQ_END:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002360 Select(N.getOperand(0));
2361 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002362
Chris Lattner16cd04d2005-05-12 23:24:06 +00002363 Opc = N.getOpcode() == ISD::CALLSEQ_START ? Alpha::ADJUSTSTACKDOWN :
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002364 Alpha::ADJUSTSTACKUP;
2365 BuildMI(BB, Opc, 1).addImm(Tmp1);
2366 return;
Andrew Lenharth95762122005-03-31 21:24:06 +00002367
2368 case ISD::PCMARKER:
2369 Select(N.getOperand(0)); //Chain
Andrew Lenharthd4653b12005-06-27 17:39:17 +00002370 BuildMI(BB, Alpha::PCLABEL, 2)
2371 .addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
Andrew Lenharth95762122005-03-31 21:24:06 +00002372 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002373 }
2374 assert(0 && "Should not be reached!");
2375}
2376
2377
2378/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
2379/// into a machine code representation using pattern matching and a machine
2380/// description file.
2381///
2382FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
Andrew Lenharthb69f3422005-06-22 17:19:45 +00002383 return new AlphaISel(TM);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002384}
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002385