blob: de135cb3c2e1c2bf97c12100988665b0f9797293 [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"
18#include "llvm/CodeGen/MachineInstrBuilder.h"
19#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
20#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SelectionDAGISel.h"
24#include "llvm/CodeGen/SSARegMap.h"
25#include "llvm/Target/TargetData.h"
26#include "llvm/Target/TargetLowering.h"
27#include "llvm/Support/MathExtras.h"
28#include "llvm/ADT/Statistic.h"
Andrew Lenharth032f2352005-02-22 21:59:48 +000029#include "llvm/Support/Debug.h"
Andrew Lenharth95762122005-03-31 21:24:06 +000030#include "llvm/Support/CommandLine.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000031#include <set>
Andrew Lenharth684f2292005-01-30 00:35:27 +000032#include <algorithm>
Andrew Lenharth304d0f32005-01-22 23:41:55 +000033using namespace llvm;
34
Andrew Lenharth95762122005-03-31 21:24:06 +000035namespace llvm {
Misha Brukman4633f1c2005-04-21 23:13:11 +000036 cl::opt<bool> EnableAlphaIDIV("enable-alpha-intfpdiv",
37 cl::desc("Use the FP div instruction for integer div when possible"),
Andrew Lenharth95762122005-03-31 21:24:06 +000038 cl::Hidden);
Andrew Lenharth59009192005-05-04 19:12:09 +000039 cl::opt<bool> EnableAlphaFTOI("enable-alpha-FTOI",
Misha Brukman4633f1c2005-04-21 23:13:11 +000040 cl::desc("Enable use of ftoi* and itof* instructions (ev6 and higher)"),
Andrew Lenharth95762122005-03-31 21:24:06 +000041 cl::Hidden);
Andrew Lenharth59009192005-05-04 19:12:09 +000042 cl::opt<bool> EnableAlphaCT("enable-alpha-CT",
43 cl::desc("Enable use of the ctpop, ctlz, and cttz instructions"),
44 cl::Hidden);
Misha Brukman4633f1c2005-04-21 23:13:11 +000045 cl::opt<bool> EnableAlphaCount("enable-alpha-count",
46 cl::desc("Print estimates on live ins and outs"),
Andrew Lenhartha32b9e32005-04-08 17:28:49 +000047 cl::Hidden);
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +000048 cl::opt<bool> EnableAlphaLSMark("enable-alpha-lsmark",
Misha Brukman5e96a3a2005-06-06 19:08:04 +000049 cl::desc("Emit symbols to correlate Mem ops to LLVM Values"),
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +000050 cl::Hidden);
Andrew Lenharth95762122005-03-31 21:24:06 +000051}
52
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +000053namespace {
54 // Alpha Specific DAG Nodes
55 namespace AlphaISD {
56 enum NodeType {
57 // Start the numbering where the builtin ops leave off.
58 FIRST_NUMBER = ISD::BUILTIN_OP_END,
59
60 //Convert an int bit pattern in an FP reg to a Double or Float
61 //Has a dest type and a source
62 CVTQ,
63 //Move an Ireg to a FPreg
64 ITOF,
65 //Move a FPreg to an Ireg
66 FTOI,
67 };
68 }
69}
70
Andrew Lenharth304d0f32005-01-22 23:41:55 +000071//===----------------------------------------------------------------------===//
72// AlphaTargetLowering - Alpha Implementation of the TargetLowering interface
73namespace {
74 class AlphaTargetLowering : public TargetLowering {
Andrew Lenharth558bc882005-06-18 18:34:52 +000075 int VarArgsOffset; // What is the offset to the first vaarg
76 int VarArgsBase; // What is the base FrameIndex
Andrew Lenharth304d0f32005-01-22 23:41:55 +000077 unsigned GP; //GOT vreg
78 public:
79 AlphaTargetLowering(TargetMachine &TM) : TargetLowering(TM) {
80 // Set up the TargetLowering object.
Andrew Lenharth3d65d312005-01-27 03:49:45 +000081 //I am having problems with shr n ubyte 1
Andrew Lenharth879ef222005-02-02 17:00:21 +000082 setShiftAmountType(MVT::i64);
83 setSetCCResultType(MVT::i64);
Andrew Lenharthd3355e22005-04-07 20:11:32 +000084 setSetCCResultContents(ZeroOrOneSetCCResult);
Misha Brukman4633f1c2005-04-21 23:13:11 +000085
Andrew Lenharth304d0f32005-01-22 23:41:55 +000086 addRegisterClass(MVT::i64, Alpha::GPRCRegisterClass);
87 addRegisterClass(MVT::f64, Alpha::FPRCRegisterClass);
Andrew Lenharth3d65d312005-01-27 03:49:45 +000088 addRegisterClass(MVT::f32, Alpha::FPRCRegisterClass);
Misha Brukman4633f1c2005-04-21 23:13:11 +000089
Chris Lattnerda4d4692005-04-09 03:22:37 +000090 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000091 setOperationAction(ISD::EXTLOAD , MVT::i1 , Promote);
92 setOperationAction(ISD::EXTLOAD , MVT::f32 , Promote);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000093
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000094 setOperationAction(ISD::ZEXTLOAD , MVT::i1 , Expand);
95 setOperationAction(ISD::ZEXTLOAD , MVT::i32 , Expand);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000096
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000097 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
98 setOperationAction(ISD::SEXTLOAD , MVT::i8 , Expand);
99 setOperationAction(ISD::SEXTLOAD , MVT::i16 , Expand);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000100
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000101 setOperationAction(ISD::SREM , MVT::f32 , Expand);
102 setOperationAction(ISD::SREM , MVT::f64 , Expand);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000103
Andrew Lenharth59009192005-05-04 19:12:09 +0000104 if (!EnableAlphaCT) {
105 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
106 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
Andrew Lenharthb5884d32005-05-04 19:25:37 +0000107 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
Andrew Lenharth59009192005-05-04 19:12:09 +0000108 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000109
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000110 //If this didn't legalize into a div....
111 // setOperationAction(ISD::SREM , MVT::i64, Expand);
112 // setOperationAction(ISD::UREM , MVT::i64, Expand);
113
114 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
115 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
116 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
Andrew Lenharth9818c052005-02-05 13:19:12 +0000117
Chris Lattner17234b72005-04-30 04:26:06 +0000118 // We don't support sin/cos/sqrt
119 setOperationAction(ISD::FSIN , MVT::f64, Expand);
120 setOperationAction(ISD::FCOS , MVT::f64, Expand);
121 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
122 setOperationAction(ISD::FSIN , MVT::f32, Expand);
123 setOperationAction(ISD::FCOS , MVT::f32, Expand);
124 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
125
Andrew Lenharth33819132005-03-04 20:09:23 +0000126 //Doesn't work yet
Chris Lattner17234b72005-04-30 04:26:06 +0000127 setOperationAction(ISD::SETCC, MVT::f32, Promote);
Andrew Lenharth572af902005-02-14 05:41:43 +0000128
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000129 //Try a couple things with a custom expander
130 //setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
131
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000132 computeRegisterProperties();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000133
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000134 addLegalFPImmediate(+0.0); //F31
135 addLegalFPImmediate(-0.0); //-F31
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000136 }
137
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000138 /// LowerOperation - Provide custom lowering hooks for some operations.
139 ///
140 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
141
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000142 /// LowerArguments - This hook must be implemented to indicate how we should
143 /// lower the arguments for the specified function, into the specified DAG.
144 virtual std::vector<SDOperand>
145 LowerArguments(Function &F, SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000146
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000147 /// LowerCallTo - This hook lowers an abstract call to a function into an
148 /// actual call.
149 virtual std::pair<SDOperand, SDOperand>
Chris Lattnerc57f6822005-05-12 19:56:45 +0000150 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattneradf6a962005-05-13 18:50:42 +0000151 bool isTailCall, SDOperand Callee, ArgListTy &Args,
152 SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000153
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000154 virtual std::pair<SDOperand, SDOperand>
Andrew Lenharth558bc882005-06-18 18:34:52 +0000155 LowerVAStart(SDOperand Chain, SelectionDAG &DAG, SDOperand Dest);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000156
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000157 virtual std::pair<SDOperand,SDOperand>
Andrew Lenharth558bc882005-06-18 18:34:52 +0000158 LowerVAArgNext(SDOperand Chain, SDOperand VAList,
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000159 const Type *ArgTy, SelectionDAG &DAG);
160
161 virtual std::pair<SDOperand, SDOperand>
162 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
163 SelectionDAG &DAG);
164
165 void restoreGP(MachineBasicBlock* BB)
166 {
167 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
168 }
169 };
170}
171
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000172/// LowerOperation - Provide custom lowering hooks for some operations.
173///
174SDOperand AlphaTargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
175 MachineFunction &MF = DAG.getMachineFunction();
176 switch (Op.getOpcode()) {
177 default: assert(0 && "Should not custom lower this!");
Misha Brukmanb8ee91a2005-06-06 17:39:46 +0000178#if 0
179 case ISD::SINT_TO_FP:
180 {
181 assert (Op.getOperand(0).getValueType() == MVT::i64
182 && "only quads can be loaded from");
183 SDOperand SRC;
184 if (EnableAlphaFTOI)
185 {
186 std::vector<MVT::ValueType> RTs;
187 RTs.push_back(Op.getValueType());
188 std::vector<SDOperand> Ops;
189 Ops.push_back(Op.getOperand(0));
190 SRC = DAG.getNode(AlphaISD::ITOF, RTs, Ops);
191 } else {
192 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
193 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
194 SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
195 Op.getOperand(0), StackSlot, DAG.getSrcValue(NULL));
196 SRC = DAG.getLoad(Op.getValueType(), Store.getValue(0), StackSlot,
197 DAG.getSrcValue(NULL));
198 }
199 std::vector<MVT::ValueType> RTs;
200 RTs.push_back(Op.getValueType());
201 std::vector<SDOperand> Ops;
202 Ops.push_back(SRC);
203 return DAG.getNode(AlphaISD::CVTQ, RTs, Ops);
204 }
205#endif
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000206 }
Misha Brukmanb8ee91a2005-06-06 17:39:46 +0000207 return SDOperand();
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +0000208}
209
210
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000211/// AddLiveIn - This helper function adds the specified physical register to the
212/// MachineFunction as a live in value. It also creates a corresponding virtual
213/// register for it.
214static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
215 TargetRegisterClass *RC) {
216 assert(RC->contains(PReg) && "Not the correct regclass!");
217 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
218 MF.addLiveIn(PReg, VReg);
219 return VReg;
220}
221
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000222//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
223
224//For now, just use variable size stack frame format
225
226//In a standard call, the first six items are passed in registers $16
227//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
228//of argument-to-register correspondence.) The remaining items are
229//collected in a memory argument list that is a naturally aligned
230//array of quadwords. In a standard call, this list, if present, must
231//be passed at 0(SP).
Misha Brukman7847fca2005-04-22 17:54:37 +0000232//7 ... n 0(SP) ... (n-7)*8(SP)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000233
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000234// //#define FP $15
235// //#define RA $26
236// //#define PV $27
237// //#define GP $29
238// //#define SP $30
Misha Brukman4633f1c2005-04-21 23:13:11 +0000239
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000240std::vector<SDOperand>
Misha Brukman4633f1c2005-04-21 23:13:11 +0000241AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000242{
243 std::vector<SDOperand> ArgValues;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000244
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000245 MachineFunction &MF = DAG.getMachineFunction();
Andrew Lenharth05380342005-02-07 05:07:00 +0000246 MachineFrameInfo*MFI = MF.getFrameInfo();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000247
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000248 MachineBasicBlock& BB = MF.front();
249
250 //Handle the return address
251 //BuildMI(&BB, Alpha::IDEF, 0, Alpha::R26);
252
Misha Brukman4633f1c2005-04-21 23:13:11 +0000253 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000254 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000255 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000256 Alpha::F19, Alpha::F20, Alpha::F21};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000257 int count = 0;
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000258
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000259 GP = AddLiveIn(MF, Alpha::R29, getRegClassFor(MVT::i64));
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000260
Chris Lattnere4d5c442005-03-15 04:54:21 +0000261 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000262 {
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000263 SDOperand argt;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000264 if (count < 6) {
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000265 unsigned Vreg;
266 MVT::ValueType VT = getValueType(I->getType());
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000267 switch (VT) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000268 default:
269 std::cerr << "Unknown Type " << VT << "\n";
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000270 abort();
271 case MVT::f64:
272 case MVT::f32:
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000273 args_float[count] = AddLiveIn(MF,args_float[count], getRegClassFor(VT));
274 argt = DAG.getCopyFromReg(args_float[count], VT, DAG.getRoot());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000275 break;
276 case MVT::i1:
277 case MVT::i8:
278 case MVT::i16:
279 case MVT::i32:
280 case MVT::i64:
Andrew Lenharth591ec572005-05-31 18:42:18 +0000281 args_int[count] = AddLiveIn(MF, args_int[count], getRegClassFor(MVT::i64));
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000282 argt = DAG.getCopyFromReg(args_int[count], VT, DAG.getRoot());
Andrew Lenharth14f30c92005-05-31 18:37:16 +0000283 if (VT != MVT::i64)
284 argt = DAG.getNode(ISD::TRUNCATE, VT, argt);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000285 break;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000286 }
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000287 DAG.setRoot(argt.getValue(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000288 } else { //more args
289 // Create the frame index object for this incoming parameter...
290 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000291
292 // Create the SelectionDAG nodes corresponding to a load
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000293 //from this parameter
294 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000295 argt = DAG.getLoad(getValueType(I->getType()),
296 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000297 }
Andrew Lenharth032f2352005-02-22 21:59:48 +0000298 ++count;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000299 ArgValues.push_back(argt);
300 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000301
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000302 // If the functions takes variable number of arguments, copy all regs to stack
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000303 if (F.isVarArg()) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000304 VarArgsOffset = count * 8;
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000305 std::vector<SDOperand> LS;
306 for (int i = 0; i < 6; ++i) {
307 if (args_int[i] < 1024)
308 args_int[i] = AddLiveIn(MF,args_int[i], getRegClassFor(MVT::i64));
309 SDOperand argt = DAG.getCopyFromReg(args_int[i], MVT::i64, DAG.getRoot());
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000310 int FI = MFI->CreateFixedObject(8, -8 * (6 - i));
Andrew Lenharth558bc882005-06-18 18:34:52 +0000311 if (i == 0) VarArgsBase = FI;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000312 SDOperand SDFI = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000313 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt, SDFI, DAG.getSrcValue(NULL)));
314
315 if (args_float[i] < 1024)
316 args_float[i] = AddLiveIn(MF,args_float[i], getRegClassFor(MVT::f64));
317 argt = DAG.getCopyFromReg(args_float[i], MVT::f64, DAG.getRoot());
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000318 FI = MFI->CreateFixedObject(8, - 8 * (12 - i));
319 SDFI = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000320 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, DAG.getRoot(), argt, SDFI, DAG.getSrcValue(NULL)));
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000321 }
322
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000323 //Set up a token factor with all the stack traffic
324 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, LS));
325 }
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000326
327 // Finally, inform the code generator which regs we return values in.
328 switch (getValueType(F.getReturnType())) {
329 default: assert(0 && "Unknown type!");
330 case MVT::isVoid: break;
331 case MVT::i1:
332 case MVT::i8:
333 case MVT::i16:
334 case MVT::i32:
335 case MVT::i64:
336 MF.addLiveOut(Alpha::R0);
337 break;
338 case MVT::f32:
339 case MVT::f64:
340 MF.addLiveOut(Alpha::F0);
341 break;
342 }
343
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000344 //return the arguments
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000345 return ArgValues;
346}
347
348std::pair<SDOperand, SDOperand>
349AlphaTargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000350 const Type *RetTy, bool isVarArg,
Chris Lattneradf6a962005-05-13 18:50:42 +0000351 unsigned CallingConv, bool isTailCall,
Misha Brukman7847fca2005-04-22 17:54:37 +0000352 SDOperand Callee, ArgListTy &Args,
353 SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000354 int NumBytes = 0;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000355 if (Args.size() > 6)
356 NumBytes = (Args.size() - 6) * 8;
357
Chris Lattner16cd04d2005-05-12 23:24:06 +0000358 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000359 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000360 std::vector<SDOperand> args_to_use;
361 for (unsigned i = 0, e = Args.size(); i != e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000362 {
363 switch (getValueType(Args[i].second)) {
364 default: assert(0 && "Unexpected ValueType for argument!");
365 case MVT::i1:
366 case MVT::i8:
367 case MVT::i16:
368 case MVT::i32:
369 // Promote the integer to 64 bits. If the input type is signed use a
370 // sign extend, otherwise use a zero extend.
371 if (Args[i].second->isSigned())
372 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
373 else
374 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
375 break;
376 case MVT::i64:
377 case MVT::f64:
378 case MVT::f32:
379 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000380 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000381 args_to_use.push_back(Args[i].first);
382 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000383
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000384 std::vector<MVT::ValueType> RetVals;
385 MVT::ValueType RetTyVT = getValueType(RetTy);
386 if (RetTyVT != MVT::isVoid)
387 RetVals.push_back(RetTyVT);
388 RetVals.push_back(MVT::Other);
389
Misha Brukman4633f1c2005-04-21 23:13:11 +0000390 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000391 Chain, Callee, args_to_use), 0);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000392 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner16cd04d2005-05-12 23:24:06 +0000393 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000394 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000395 return std::make_pair(TheCall, Chain);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000396}
397
398std::pair<SDOperand, SDOperand>
Andrew Lenharth558bc882005-06-18 18:34:52 +0000399AlphaTargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG, SDOperand Dest) {
400 // vastart just stores the address of the VarArgsBase and VarArgsOffset
401 SDOperand FR = DAG.getFrameIndex(VarArgsBase, MVT::i32);
402 SDOperand S1 = DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, Dest, DAG.getSrcValue(NULL));
403 SDOperand SA2 = DAG.getNode(ISD::ADD, MVT::i64, Dest, DAG.getConstant(8, MVT::i64));
404 SDOperand S2 = DAG.getNode(ISD::STORE, MVT::Other, S1,
405 DAG.getConstant(VarArgsOffset, MVT::i64), SA2,
406 DAG.getSrcValue(NULL));
407
408 return std::make_pair(S2, S2);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000409}
410
411std::pair<SDOperand,SDOperand> AlphaTargetLowering::
Andrew Lenharth558bc882005-06-18 18:34:52 +0000412LowerVAArgNext(SDOperand Chain, SDOperand VAList,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000413 const Type *ArgTy, SelectionDAG &DAG) {
Andrew Lenharth558bc882005-06-18 18:34:52 +0000414 //FIXME: For now, ignore FP
415 SDOperand Base = DAG.getLoad(MVT::i64, Chain, VAList, DAG.getSrcValue(NULL));
416 SDOperand Tmp = DAG.getNode(ISD::ADD, MVT::i64, VAList,
417 DAG.getConstant(8, MVT::i64));
418 SDOperand Offset = DAG.getLoad(MVT::i64, Chain, Tmp, DAG.getSrcValue(NULL));
419 SDOperand DataPtr = DAG.getNode(ISD::ADD, MVT::i64, Base, Offset);
420 SDOperand Result = DAG.getLoad(MVT::i64, Chain, DataPtr,
421 DAG.getSrcValue(NULL));
422 SDOperand NewOffset = DAG.getNode(ISD::ADD, MVT::i64, Offset,
423 DAG.getConstant(8, MVT::i64));
424 SDOperand Update = DAG.getNode(ISD::STORE, MVT::Other, Result, NewOffset,
425 Tmp, DAG.getSrcValue(NULL));
426 return std::make_pair(Result, Update);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000427}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000428
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000429
430std::pair<SDOperand, SDOperand> AlphaTargetLowering::
431LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
432 SelectionDAG &DAG) {
433 abort();
434}
435
436
437
438
439
440namespace {
441
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000442//===--------------------------------------------------------------------===//
443/// ISel - Alpha specific code to select Alpha machine instructions for
444/// SelectionDAG operations.
445//===--------------------------------------------------------------------===//
446class ISel : public SelectionDAGISel {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000447
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000448 /// AlphaLowering - This object fully describes how to lower LLVM code to an
449 /// Alpha-specific SelectionDAG.
450 AlphaTargetLowering AlphaLowering;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000451
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000452 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
453 // for sdiv and udiv until it is put into the future
454 // dag combiner.
455
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000456 /// ExprMap - As shared expressions are codegen'd, we keep track of which
457 /// vreg the value is produced in, so we only emit one copy of each compiled
458 /// tree.
459 static const unsigned notIn = (unsigned)(-1);
460 std::map<SDOperand, unsigned> ExprMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000461
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000462 //CCInvMap sometimes (SetNE) we have the inverse CC code for free
463 std::map<SDOperand, unsigned> CCInvMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000464
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000465 int count_ins;
466 int count_outs;
467 bool has_sym;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000468 int max_depth;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000469
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000470public:
Misha Brukman4633f1c2005-04-21 23:13:11 +0000471 ISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering), AlphaLowering(TM)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000472 {}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000473
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000474 /// InstructionSelectBasicBlock - This callback is invoked by
475 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
476 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
Andrew Lenharth032f2352005-02-22 21:59:48 +0000477 DEBUG(BB->dump());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000478 count_ins = 0;
479 count_outs = 0;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000480 max_depth = 0;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000481 has_sym = false;
482
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000483 // Codegen the basic block.
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000484 ISelDAG = &DAG;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000485 max_depth = DAG.getRoot().getNodeDepth();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000486 Select(DAG.getRoot());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000487
488 if(has_sym)
489 ++count_ins;
490 if(EnableAlphaCount)
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000491 std::cerr << "COUNT: " << BB->getParent()->getFunction ()->getName() << " "
492 << BB->getNumber() << " "
493 << max_depth << " "
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000494 << count_ins << " "
495 << count_outs << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000496
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000497 // Clear state used for selection.
498 ExprMap.clear();
499 CCInvMap.clear();
500 }
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000501
502 virtual void EmitFunctionEntryCode(Function &Fn, MachineFunction &MF);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000503
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000504 unsigned SelectExpr(SDOperand N);
505 unsigned SelectExprFP(SDOperand N, unsigned Result);
506 void Select(SDOperand N);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000507
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000508 void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
509 void SelectBranchCC(SDOperand N);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000510 void MoveFP2Int(unsigned src, unsigned dst, bool isDouble);
511 void MoveInt2FP(unsigned src, unsigned dst, bool isDouble);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000512 //returns whether the sense of the comparison was inverted
513 bool SelectFPSetCC(SDOperand N, unsigned dst);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000514
515 // dag -> dag expanders for integer divide by constant
516 SDOperand BuildSDIVSequence(SDOperand N);
517 SDOperand BuildUDIVSequence(SDOperand N);
518
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000519};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000520}
521
Andrew Lenharthfd5e4b72005-05-31 18:35:43 +0000522void ISel::EmitFunctionEntryCode(Function &Fn, MachineFunction &MF) {
523 // If this function has live-in values, emit the copies from pregs to vregs at
524 // the top of the function, before anything else.
525 MachineBasicBlock *BB = MF.begin();
526 if (MF.livein_begin() != MF.livein_end()) {
527 SSARegMap *RegMap = MF.getSSARegMap();
528 for (MachineFunction::livein_iterator LI = MF.livein_begin(),
529 E = MF.livein_end(); LI != E; ++LI) {
530 const TargetRegisterClass *RC = RegMap->getRegClass(LI->second);
531 if (RC == Alpha::GPRCRegisterClass) {
532 BuildMI(BB, Alpha::BIS, 2, LI->second).addReg(LI->first).addReg(LI->first);
533 } else if (RC == Alpha::FPRCRegisterClass) {
534 BuildMI(BB, Alpha::CPYS, 2, LI->second).addReg(LI->first).addReg(LI->first);
535 } else {
536 assert(0 && "Unknown regclass!");
537 }
538 }
539 }
540}
541
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000542//Find the offset of the arg in it's parent's function
543static int getValueOffset(const Value* v)
544{
545 if (v == NULL)
546 return 0;
547
548 const Instruction* itarget = dyn_cast<Instruction>(v);
549 const BasicBlock* btarget = itarget->getParent();
550 const Function* ftarget = btarget->getParent();
551
552 //offset due to earlier BBs
553 int i = 0;
554 for(Function::const_iterator ii = ftarget->begin(); &*ii != btarget; ++ii)
555 i += ii->size();
556
557 for(BasicBlock::const_iterator ii = btarget->begin(); &*ii != itarget; ++ii)
558 ++i;
559
560 return i;
561}
562
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000563//Factorize a number using the list of constants
564static bool factorize(int v[], int res[], int size, uint64_t c)
565{
566 bool cont = true;
567 while (c != 1 && cont)
568 {
569 cont = false;
570 for(int i = 0; i < size; ++i)
571 {
572 if (c % v[i] == 0)
573 {
574 c /= v[i];
575 ++res[i];
576 cont=true;
577 }
578 }
579 }
580 return c == 1;
581}
582
583
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000584//Shamelessly adapted from PPC32
Misha Brukman4633f1c2005-04-21 23:13:11 +0000585// Structure used to return the necessary information to codegen an SDIV as
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000586// a multiply.
587struct ms {
588 int64_t m; // magic number
589 int64_t s; // shift amount
590};
591
592struct mu {
593 uint64_t m; // magic number
594 int64_t a; // add indicator
595 int64_t s; // shift amount
596};
597
598/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukman4633f1c2005-04-21 23:13:11 +0000599/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000600/// or -1.
601static struct ms magic(int64_t d) {
602 int64_t p;
603 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
604 const uint64_t two63 = 9223372036854775808ULL; // 2^63
605 struct ms mag;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000606
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000607 ad = abs(d);
608 t = two63 + ((uint64_t)d >> 63);
609 anc = t - 1 - t%ad; // absolute value of nc
Andrew Lenharth320174f2005-04-07 17:19:16 +0000610 p = 63; // initialize p
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000611 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
612 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
613 q2 = two63/ad; // initialize q2 = 2p/abs(d)
614 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
615 do {
616 p = p + 1;
617 q1 = 2*q1; // update q1 = 2p/abs(nc)
618 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
619 if (r1 >= anc) { // must be unsigned comparison
620 q1 = q1 + 1;
621 r1 = r1 - anc;
622 }
623 q2 = 2*q2; // update q2 = 2p/abs(d)
624 r2 = 2*r2; // update r2 = rem(2p/abs(d))
625 if (r2 >= ad) { // must be unsigned comparison
626 q2 = q2 + 1;
627 r2 = r2 - ad;
628 }
629 delta = ad - r2;
630 } while (q1 < delta || (q1 == delta && r1 == 0));
631
632 mag.m = q2 + 1;
633 if (d < 0) mag.m = -mag.m; // resulting magic number
634 mag.s = p - 64; // resulting shift
635 return mag;
636}
637
638/// magicu - calculate the magic numbers required to codegen an integer udiv as
639/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
640static struct mu magicu(uint64_t d)
641{
642 int64_t p;
643 uint64_t nc, delta, q1, r1, q2, r2;
644 struct mu magu;
645 magu.a = 0; // initialize "add" indicator
646 nc = - 1 - (-d)%d;
Andrew Lenharth320174f2005-04-07 17:19:16 +0000647 p = 63; // initialize p
648 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
649 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
650 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
651 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000652 do {
653 p = p + 1;
654 if (r1 >= nc - r1 ) {
655 q1 = 2*q1 + 1; // update q1
656 r1 = 2*r1 - nc; // update r1
657 }
658 else {
659 q1 = 2*q1; // update q1
660 r1 = 2*r1; // update r1
661 }
662 if (r2 + 1 >= d - r2) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000663 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000664 q2 = 2*q2 + 1; // update q2
665 r2 = 2*r2 + 1 - d; // update r2
666 }
667 else {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000668 if (q2 >= 0x8000000000000000ull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000669 q2 = 2*q2; // update q2
670 r2 = 2*r2 + 1; // update r2
671 }
672 delta = d - 1 - r2;
673 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
674 magu.m = q2 + 1; // resulting magic number
Andrew Lenharth320174f2005-04-07 17:19:16 +0000675 magu.s = p - 64; // resulting shift
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000676 return magu;
677}
678
679/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
680/// return a DAG expression to select that will generate the same value by
681/// multiplying by a magic number. See:
682/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
683SDOperand ISel::BuildSDIVSequence(SDOperand N) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000684 int64_t d = (int64_t)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000685 ms magics = magic(d);
686 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000687 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000688 ISelDAG->getConstant(magics.m, MVT::i64));
689 // If d > 0 and m < 0, add the numerator
690 if (d > 0 && magics.m < 0)
691 Q = ISelDAG->getNode(ISD::ADD, MVT::i64, Q, N.getOperand(0));
692 // If d < 0 and m > 0, subtract the numerator.
693 if (d < 0 && magics.m > 0)
694 Q = ISelDAG->getNode(ISD::SUB, MVT::i64, Q, N.getOperand(0));
695 // Shift right algebraic if shift value is nonzero
696 if (magics.s > 0)
Misha Brukman4633f1c2005-04-21 23:13:11 +0000697 Q = ISelDAG->getNode(ISD::SRA, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000698 ISelDAG->getConstant(magics.s, MVT::i64));
699 // Extract the sign bit and add it to the quotient
Misha Brukman4633f1c2005-04-21 23:13:11 +0000700 SDOperand T =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000701 ISelDAG->getNode(ISD::SRL, MVT::i64, Q, ISelDAG->getConstant(63, MVT::i64));
702 return ISelDAG->getNode(ISD::ADD, MVT::i64, Q, T);
703}
704
705/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
706/// return a DAG expression to select that will generate the same value by
707/// multiplying by a magic number. See:
708/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
709SDOperand ISel::BuildUDIVSequence(SDOperand N) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000710 unsigned d =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000711 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
712 mu magics = magicu(d);
713 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000714 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000715 ISelDAG->getConstant(magics.m, MVT::i64));
716 if (magics.a == 0) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000717 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000718 ISelDAG->getConstant(magics.s, MVT::i64));
719 } else {
720 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i64, N.getOperand(0), Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000721 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000722 ISelDAG->getConstant(1, MVT::i64));
723 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i64, NPQ, Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000724 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000725 ISelDAG->getConstant(magics.s-1, MVT::i64));
726 }
727 return Q;
728}
729
Andrew Lenhartha565c272005-04-06 22:03:13 +0000730//From PPC32
731/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
732/// returns zero when the input is not exactly a power of two.
733static unsigned ExactLog2(uint64_t Val) {
734 if (Val == 0 || (Val & (Val-1))) return 0;
735 unsigned Count = 0;
736 while (Val != 1) {
737 Val >>= 1;
738 ++Count;
739 }
740 return Count;
741}
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000742
743
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000744//These describe LDAx
Andrew Lenharthc0513832005-03-29 19:24:04 +0000745static const int IMM_LOW = -32768;
746static const int IMM_HIGH = 32767;
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000747static const int IMM_MULT = 65536;
748
749static long getUpper16(long l)
750{
751 long y = l / IMM_MULT;
752 if (l % IMM_MULT > IMM_HIGH)
753 ++y;
754 return y;
755}
756
757static long getLower16(long l)
758{
759 long h = getUpper16(l);
760 return l - h * IMM_MULT;
761}
762
Andrew Lenharth65838902005-02-06 16:22:15 +0000763static unsigned GetSymVersion(unsigned opcode)
764{
765 switch (opcode) {
766 default: assert(0 && "unknown load or store"); return 0;
767 case Alpha::LDQ: return Alpha::LDQ_SYM;
768 case Alpha::LDS: return Alpha::LDS_SYM;
769 case Alpha::LDT: return Alpha::LDT_SYM;
770 case Alpha::LDL: return Alpha::LDL_SYM;
771 case Alpha::LDBU: return Alpha::LDBU_SYM;
772 case Alpha::LDWU: return Alpha::LDWU_SYM;
773 case Alpha::LDW: return Alpha::LDW_SYM;
774 case Alpha::LDB: return Alpha::LDB_SYM;
775 case Alpha::STQ: return Alpha::STQ_SYM;
776 case Alpha::STS: return Alpha::STS_SYM;
777 case Alpha::STT: return Alpha::STT_SYM;
778 case Alpha::STL: return Alpha::STL_SYM;
779 case Alpha::STW: return Alpha::STW_SYM;
780 case Alpha::STB: return Alpha::STB_SYM;
781 }
782}
783
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000784void ISel::MoveFP2Int(unsigned src, unsigned dst, bool isDouble)
785{
786 unsigned Opc;
787 if (EnableAlphaFTOI) {
788 Opc = isDouble ? Alpha::FTOIT : Alpha::FTOIS;
789 BuildMI(BB, Opc, 1, dst).addReg(src);
790 } else {
791 //The hard way:
792 // Spill the integer to memory and reload it from there.
793 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
794 MachineFunction *F = BB->getParent();
795 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
796
797 Opc = isDouble ? Alpha::STT : Alpha::STS;
798 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
799 Opc = isDouble ? Alpha::LDQ : Alpha::LDL;
800 BuildMI(BB, Alpha::LDQ, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
801 }
802}
803
804void ISel::MoveInt2FP(unsigned src, unsigned dst, bool isDouble)
805{
806 unsigned Opc;
807 if (EnableAlphaFTOI) {
808 Opc = isDouble?Alpha::ITOFT:Alpha::ITOFS;
809 BuildMI(BB, Opc, 1, dst).addReg(src);
810 } else {
811 //The hard way:
812 // Spill the integer to memory and reload it from there.
813 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
814 MachineFunction *F = BB->getParent();
815 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
816
817 Opc = isDouble ? Alpha::STQ : Alpha::STL;
818 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
819 Opc = isDouble ? Alpha::LDT : Alpha::LDS;
820 BuildMI(BB, Opc, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
821 }
822}
823
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000824bool ISel::SelectFPSetCC(SDOperand N, unsigned dst)
825{
826 SDNode *Node = N.Val;
827 unsigned Opc, Tmp1, Tmp2, Tmp3;
828 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node);
829
830 //assert(SetCC->getOperand(0).getValueType() != MVT::f32 && "SetCC f32 should have been promoted");
831 bool rev = false;
832 bool inv = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000833
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000834 switch (SetCC->getCondition()) {
835 default: Node->dump(); assert(0 && "Unknown FP comparison!");
836 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
837 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
838 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
839 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
840 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
841 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
842 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000843
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000844 //FIXME: check for constant 0.0
845 ConstantFPSDNode *CN;
846 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
847 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
848 Tmp1 = Alpha::F31;
849 else
850 Tmp1 = SelectExpr(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000851
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000852 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
853 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
854 Tmp2 = Alpha::F31;
855 else
Chris Lattner9c9183a2005-04-30 04:44:07 +0000856 Tmp2 = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000857
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000858 //Can only compare doubles, and dag won't promote for me
859 if (SetCC->getOperand(0).getValueType() == MVT::f32)
860 {
861 //assert(0 && "Setcc On float?\n");
862 std::cerr << "Setcc on float!\n";
863 Tmp3 = MakeReg(MVT::f64);
864 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
865 Tmp1 = Tmp3;
866 }
867 if (SetCC->getOperand(1).getValueType() == MVT::f32)
868 {
869 //assert (0 && "Setcc On float?\n");
870 std::cerr << "Setcc on float!\n";
871 Tmp3 = MakeReg(MVT::f64);
872 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp2);
873 Tmp2 = Tmp3;
874 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000875
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000876 if (rev) std::swap(Tmp1, Tmp2);
877 //do the comparison
878 BuildMI(BB, Opc, 2, dst).addReg(Tmp1).addReg(Tmp2);
879 return inv;
880}
881
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000882//Check to see if the load is a constant offset from a base register
883void ISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
884{
885 unsigned opcode = N.getOpcode();
886 if (opcode == ISD::ADD) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000887 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000888 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
889 { //Normal imm add
890 Reg = SelectExpr(N.getOperand(0));
891 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
892 return;
893 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000894 else if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000895 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 32767)
896 {
897 Reg = SelectExpr(N.getOperand(1));
898 offset = cast<ConstantSDNode>(N.getOperand(0))->getValue();
899 return;
900 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000901 }
902 Reg = SelectExpr(N);
903 offset = 0;
904 return;
905}
906
Andrew Lenharth445171a2005-02-08 00:40:03 +0000907void ISel::SelectBranchCC(SDOperand N)
908{
909 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000910 MachineBasicBlock *Dest =
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000911 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
912 unsigned Opc = Alpha::WTF;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000913
Andrew Lenharth445171a2005-02-08 00:40:03 +0000914 Select(N.getOperand(0)); //chain
915 SDOperand CC = N.getOperand(1);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000916
Andrew Lenharth445171a2005-02-08 00:40:03 +0000917 if (CC.getOpcode() == ISD::SETCC)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000918 {
919 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
920 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
921 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000922 bool LeftZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
923 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
Andrew Lenharth09552bf2005-06-08 18:02:21 +0000924 bool RightZero = SetCC->getOperand(1).getOpcode() == ISD::Constant &&
925 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000926 bool isNE = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000927
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000928 //Fix up CC
929 ISD::CondCode cCode= SetCC->getCondition();
930 if (LeftZero && !RightZero) //Swap Operands
931 cCode = ISD::getSetCCSwappedOperands(cCode);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000932
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000933 if(cCode == ISD::SETNE)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000934 isNE = true;
Andrew Lenharth445171a2005-02-08 00:40:03 +0000935
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000936 if (LeftZero || RightZero) {
Andrew Lenharth09552bf2005-06-08 18:02:21 +0000937 switch (cCode) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000938 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
939 case ISD::SETEQ: Opc = Alpha::BEQ; break;
940 case ISD::SETLT: Opc = Alpha::BLT; break;
941 case ISD::SETLE: Opc = Alpha::BLE; break;
942 case ISD::SETGT: Opc = Alpha::BGT; break;
943 case ISD::SETGE: Opc = Alpha::BGE; break;
944 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
945 case ISD::SETUGT: Opc = Alpha::BNE; break;
946 case ISD::SETULE: Opc = Alpha::BEQ; break; //Technically you could have this CC
947 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
948 case ISD::SETNE: Opc = Alpha::BNE; break;
949 }
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000950 unsigned Tmp1;
951 if(LeftZero && !RightZero) //swap Operands
952 Tmp1 = SelectExpr(SetCC->getOperand(1)); //Cond
953 else
954 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000955 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
956 return;
957 } else {
958 unsigned Tmp1 = SelectExpr(CC);
959 if (isNE)
960 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
961 else
962 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000963 return;
964 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000965 } else { //FP
966 //Any comparison between 2 values should be codegened as an folded branch, as moving
967 //CC to the integer register is very expensive
968 //for a cmp b: c = a - b;
969 //a = b: c = 0
970 //a < b: c < 0
971 //a > b: c > 0
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000972
973 bool invTest = false;
974 unsigned Tmp3;
975
976 ConstantFPSDNode *CN;
977 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
978 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
979 Tmp3 = SelectExpr(SetCC->getOperand(0));
980 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
981 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
982 {
983 Tmp3 = SelectExpr(SetCC->getOperand(1));
984 invTest = true;
985 }
986 else
987 {
988 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
989 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
990 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
991 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
992 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
993 .addReg(Tmp1).addReg(Tmp2);
994 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000995
996 switch (SetCC->getCondition()) {
997 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000998 case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
999 case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
1000 case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
1001 case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
1002 case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
1003 case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001004 }
1005 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001006 return;
1007 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001008 abort(); //Should never be reached
1009 } else {
1010 //Giveup and do the stupid thing
1011 unsigned Tmp1 = SelectExpr(CC);
1012 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
1013 return;
1014 }
Andrew Lenharth445171a2005-02-08 00:40:03 +00001015 abort(); //Should never be reached
1016}
1017
Andrew Lenharth40831c52005-01-28 06:57:18 +00001018unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
1019{
1020 unsigned Tmp1, Tmp2, Tmp3;
1021 unsigned Opc = 0;
1022 SDNode *Node = N.Val;
1023 MVT::ValueType DestType = N.getValueType();
1024 unsigned opcode = N.getOpcode();
1025
1026 switch (opcode) {
1027 default:
1028 Node->dump();
1029 assert(0 && "Node not handled!\n");
Andrew Lenharth2c594352005-01-29 15:42:07 +00001030
Andrew Lenharth7332f3e2005-04-02 19:11:07 +00001031 case ISD::UNDEF: {
1032 BuildMI(BB, Alpha::IDEF, 0, Result);
1033 return Result;
1034 }
1035
Andrew Lenharth30b46d42005-04-02 19:04:58 +00001036 case ISD::FNEG:
1037 if(ISD::FABS == N.getOperand(0).getOpcode())
1038 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001039 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1040 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +00001041 } else {
Misha Brukman7847fca2005-04-22 17:54:37 +00001042 Tmp1 = SelectExpr(N.getOperand(0));
1043 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +00001044 }
1045 return Result;
1046
1047 case ISD::FABS:
1048 Tmp1 = SelectExpr(N.getOperand(0));
1049 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
1050 return Result;
1051
Andrew Lenharth9818c052005-02-05 13:19:12 +00001052 case ISD::SELECT:
1053 {
Andrew Lenharth45859692005-03-03 21:47:53 +00001054 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1055 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
1056 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
1057
1058 SDOperand CC = N.getOperand(0);
1059 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
1060
Misha Brukman4633f1c2005-04-21 23:13:11 +00001061 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth45859692005-03-03 21:47:53 +00001062 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
1063 { //FP Setcc -> Select yay!
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001064
1065
Andrew Lenharth45859692005-03-03 21:47:53 +00001066 //for a cmp b: c = a - b;
1067 //a = b: c = 0
1068 //a < b: c < 0
1069 //a > b: c > 0
Misha Brukman4633f1c2005-04-21 23:13:11 +00001070
Andrew Lenharth45859692005-03-03 21:47:53 +00001071 bool invTest = false;
1072 unsigned Tmp3;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001073
Andrew Lenharth45859692005-03-03 21:47:53 +00001074 ConstantFPSDNode *CN;
1075 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
1076 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1077 Tmp3 = SelectExpr(SetCC->getOperand(0));
1078 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
1079 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1080 {
1081 Tmp3 = SelectExpr(SetCC->getOperand(1));
1082 invTest = true;
1083 }
1084 else
1085 {
1086 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
1087 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
1088 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
1089 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1090 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1091 .addReg(Tmp1).addReg(Tmp2);
1092 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001093
Andrew Lenharth45859692005-03-03 21:47:53 +00001094 switch (SetCC->getCondition()) {
1095 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
1096 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNE : Alpha::FCMOVEQ; break;
1097 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGT : Alpha::FCMOVLT; break;
1098 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGE : Alpha::FCMOVLE; break;
1099 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLT : Alpha::FCMOVGT; break;
1100 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLE : Alpha::FCMOVGE; break;
1101 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQ : Alpha::FCMOVNE; break;
1102 }
Andrew Lenharth33819132005-03-04 20:09:23 +00001103 BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
Andrew Lenharth45859692005-03-03 21:47:53 +00001104 return Result;
1105 }
1106 else
1107 {
1108 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001109 BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV).addReg(Tmp1);
1110// // Spill the cond to memory and reload it from there.
1111// unsigned Tmp4 = MakeReg(MVT::f64);
1112// MoveIntFP(Tmp1, Tmp4, true);
1113// //now ideally, we don't have to do anything to the flag...
1114// // Get the condition into the zero flag.
1115// BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
Andrew Lenharth45859692005-03-03 21:47:53 +00001116 return Result;
1117 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001118 }
1119
Andrew Lenharthc1faced2005-02-01 01:37:24 +00001120 case ISD::FP_ROUND:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001121 assert (DestType == MVT::f32 &&
1122 N.getOperand(0).getValueType() == MVT::f64 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001123 "only f64 to f32 conversion supported here");
Andrew Lenharthc1faced2005-02-01 01:37:24 +00001124 Tmp1 = SelectExpr(N.getOperand(0));
1125 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
1126 return Result;
1127
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001128 case ISD::FP_EXTEND:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001129 assert (DestType == MVT::f64 &&
1130 N.getOperand(0).getValueType() == MVT::f32 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001131 "only f32 to f64 conversion supported here");
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001132 Tmp1 = SelectExpr(N.getOperand(0));
1133 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
1134 return Result;
1135
Andrew Lenharth2c594352005-01-29 15:42:07 +00001136 case ISD::CopyFromReg:
1137 {
1138 // Make sure we generate both values.
1139 if (Result != notIn)
1140 ExprMap[N.getValue(1)] = notIn; // Generate the token
1141 else
1142 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001143
Andrew Lenharth2c594352005-01-29 15:42:07 +00001144 SDOperand Chain = N.getOperand(0);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001145
Andrew Lenharth2c594352005-01-29 15:42:07 +00001146 Select(Chain);
1147 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1148 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1149 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
1150 return Result;
1151 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001152
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001153 case ISD::LOAD:
1154 {
1155 // Make sure we generate both values.
1156 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001157 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001158 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001159 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001160
Andrew Lenharth29219162005-02-07 06:31:44 +00001161 DestType = N.getValue(0).getValueType();
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001162
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001163 SDOperand Chain = N.getOperand(0);
1164 SDOperand Address = N.getOperand(1);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001165 Select(Chain);
Andrew Lenharth65838902005-02-06 16:22:15 +00001166 Opc = DestType == MVT::f64 ? Alpha::LDT : Alpha::LDS;
1167
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +00001168 if (EnableAlphaLSMark)
1169 {
1170 int i = getValueOffset(dyn_cast<SrcValueSDNode>(N.getOperand(2))->getValue());
1171 BuildMI(BB, Alpha::MEMLABEL, 2).addImm(i);
1172 }
1173
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001174 if (Address.getOpcode() == ISD::GlobalAddress) {
1175 AlphaLowering.restoreGP(BB);
1176 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001177 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001178 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1179 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001180 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001181 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001182 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001183 has_sym = true;
Andrew Lenharth97127a12005-02-05 17:41:39 +00001184 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001185 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001186 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001187 BuildMI(BB, Opc, 2, Result)
1188 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1189 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001190 } else {
1191 long offset;
1192 SelectAddr(Address, Tmp1, offset);
1193 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1194 }
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001195 return Result;
1196 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001197 case ISD::ConstantFP:
1198 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
1199 if (CN->isExactlyValue(+0.0)) {
1200 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001201 } else if ( CN->isExactlyValue(-0.0)) {
1202 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001203 } else {
1204 abort();
1205 }
1206 }
1207 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001208
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001209 case ISD::SDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001210 case ISD::MUL:
1211 case ISD::ADD:
1212 case ISD::SUB:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001213 switch( opcode ) {
1214 case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS; break;
1215 case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS; break;
1216 case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS; break;
1217 case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS; break;
1218 };
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001219
1220 ConstantFPSDNode *CN;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001221 if (opcode == ISD::SUB
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001222 && (CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
1223 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1224 {
1225 Tmp2 = SelectExpr(N.getOperand(1));
1226 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp2).addReg(Tmp2);
1227 } else {
1228 Tmp1 = SelectExpr(N.getOperand(0));
1229 Tmp2 = SelectExpr(N.getOperand(1));
1230 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1231 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001232 return Result;
1233
Andrew Lenharth2c594352005-01-29 15:42:07 +00001234 case ISD::EXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001235 {
1236 //include a conversion sequence for float loads to double
1237 if (Result != notIn)
1238 ExprMap[N.getValue(1)] = notIn; // Generate the token
1239 else
1240 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001241
Andrew Lenhartha549deb2005-02-07 05:33:15 +00001242 Tmp1 = MakeReg(MVT::f32);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001243
1244 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001245 "EXTLOAD not from f32");
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001246 assert(Node->getValueType(0) == MVT::f64 && "EXTLOAD not to f64");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001247
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001248 SDOperand Chain = N.getOperand(0);
1249 SDOperand Address = N.getOperand(1);
1250 Select(Chain);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001251
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001252 if (Address.getOpcode() == ISD::GlobalAddress) {
1253 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001254 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001255 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1256 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001257 else if (ConstantPoolSDNode *CP =
1258 dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001259 {
1260 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001261 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001262 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addConstantPoolIndex(CP->getIndex());
1263 }
1264 else if(Address.getOpcode() == ISD::FrameIndex) {
1265 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
Andrew Lenharth032f2352005-02-22 21:59:48 +00001266 BuildMI(BB, Alpha::LDS, 2, Tmp1)
1267 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1268 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001269 } else {
1270 long offset;
1271 SelectAddr(Address, Tmp2, offset);
1272 BuildMI(BB, Alpha::LDS, 1, Tmp1).addImm(offset).addReg(Tmp2);
1273 }
Andrew Lenharth29219162005-02-07 06:31:44 +00001274 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001275 return Result;
1276 }
Andrew Lenharth2c594352005-01-29 15:42:07 +00001277
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001278 case ISD::UINT_TO_FP:
Andrew Lenharth69520ed2005-05-26 18:18:34 +00001279 {
1280 //FIXME: First test if we will have problems with the sign bit before doing the slow thing
1281 assert (N.getOperand(0).getValueType() == MVT::i64
1282 && "only quads can be loaded from");
1283 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1284 Tmp2 = MakeReg(MVT::i64);
1285 BuildMI(BB, Alpha::SRL, 2, Tmp2).addReg(Tmp1).addImm(1);
1286 Tmp3 = MakeReg(MVT::i64);
1287 BuildMI(BB, Alpha::CMPLT, 2, Tmp3).addReg(Tmp1).addReg(Alpha::R31);
1288 unsigned Tmp4 = MakeReg(MVT::f64), Tmp5 = MakeReg(MVT::f64), Tmp6 = MakeReg(MVT::f64);
1289 MoveInt2FP(Tmp1, Tmp4, true);
1290 MoveInt2FP(Tmp2, Tmp5, true);
1291 MoveInt2FP(Tmp3, Tmp6, true);
1292 Tmp1 = MakeReg(MVT::f64);
1293 Tmp2 = MakeReg(MVT::f64);
1294 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
1295 BuildMI(BB, Opc, 1, Tmp1).addReg(Tmp4);
1296 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp5);
1297 Tmp3 = MakeReg(MVT::f64);
1298 BuildMI(BB, Alpha::ADDT, 2, Tmp3).addReg(Tmp2).addReg(Tmp2);
1299 //Ok, now tmp1 had the plain covereted
1300 //tmp3 has the reduced converted and added
1301 //tmp6 has the conditional to use
1302 BuildMI(BB, Alpha::FCMOVNE, 3, Result).addReg(Tmp1).addReg(Tmp3).addReg(Tmp6);
1303 return Result;
1304 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001305 case ISD::SINT_TO_FP:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001306 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001307 assert (N.getOperand(0).getValueType() == MVT::i64
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001308 && "only quads can be loaded from");
Andrew Lenharth40831c52005-01-28 06:57:18 +00001309 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001310 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001311 MoveInt2FP(Tmp1, Tmp2, true);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001312 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
1313 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001314 return Result;
1315 }
1316 }
1317 assert(0 && "should not get here");
1318 return 0;
1319}
1320
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001321unsigned ISel::SelectExpr(SDOperand N) {
1322 unsigned Result;
Andrew Lenharth2966e842005-04-07 18:15:28 +00001323 unsigned Tmp1, Tmp2 = 0, Tmp3;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001324 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001325 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001326
1327 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001328 MVT::ValueType DestType = N.getValueType();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001329
1330 unsigned &Reg = ExprMap[N];
1331 if (Reg) return Reg;
1332
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001333 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::TAILCALL)
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001334 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001335 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001336 else {
1337 // If this is a call instruction, make sure to prepare ALL of the result
1338 // values as well as the chain.
1339 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001340 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001341 else {
1342 Result = MakeReg(Node->getValueType(0));
1343 ExprMap[N.getValue(0)] = Result;
1344 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1345 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001346 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001347 }
1348 }
1349
Andrew Lenharth50d91d72005-04-30 14:19:13 +00001350 if ((DestType == MVT::f64 || DestType == MVT::f32 ||
1351 (
1352 (opcode == ISD::LOAD || opcode == ISD::CopyFromReg ||
1353 opcode == ISD::EXTLOAD) &&
1354 (N.getValue(0).getValueType() == MVT::f32 ||
1355 N.getValue(0).getValueType() == MVT::f64)
1356 ))
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001357 && opcode != ISD::CALL && opcode != ISD::TAILCALL
Andrew Lenharth06342c32005-02-07 06:21:37 +00001358 )
Andrew Lenharth40831c52005-01-28 06:57:18 +00001359 return SelectExprFP(N, Result);
1360
1361 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001362 default:
1363 Node->dump();
1364 assert(0 && "Node not handled!\n");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001365
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001366 case ISD::CTPOP:
1367 case ISD::CTTZ:
1368 case ISD::CTLZ:
1369 Opc = opcode == ISD::CTPOP ? Alpha::CTPOP :
1370 (opcode == ISD::CTTZ ? Alpha::CTTZ : Alpha::CTLZ);
1371 Tmp1 = SelectExpr(N.getOperand(0));
1372 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1373 return Result;
1374
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001375 case ISD::MULHU:
1376 Tmp1 = SelectExpr(N.getOperand(0));
1377 Tmp2 = SelectExpr(N.getOperand(1));
1378 BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth706be912005-04-07 13:55:53 +00001379 return Result;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001380 case ISD::MULHS:
1381 {
1382 //MULHU - Ra<63>*Rb - Rb<63>*Ra
1383 Tmp1 = SelectExpr(N.getOperand(0));
1384 Tmp2 = SelectExpr(N.getOperand(1));
1385 Tmp3 = MakeReg(MVT::i64);
1386 BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1387 unsigned V1 = MakeReg(MVT::i64);
1388 unsigned V2 = MakeReg(MVT::i64);
1389 BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31).addReg(Tmp1);
1390 BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31).addReg(Tmp2);
1391 unsigned IRes = MakeReg(MVT::i64);
1392 BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
1393 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
1394 return Result;
1395 }
Andrew Lenharth7332f3e2005-04-02 19:11:07 +00001396 case ISD::UNDEF: {
1397 BuildMI(BB, Alpha::IDEF, 0, Result);
1398 return Result;
1399 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001400
Andrew Lenharth032f2352005-02-22 21:59:48 +00001401 case ISD::DYNAMIC_STACKALLOC:
1402 // Generate both result values.
Andrew Lenharth3a7118d2005-02-23 17:33:42 +00001403 if (Result != notIn)
1404 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth032f2352005-02-22 21:59:48 +00001405 else
1406 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1407
1408 // FIXME: We are currently ignoring the requested alignment for handling
1409 // greater than the stack alignment. This will need to be revisited at some
1410 // point. Align = N.getOperand(2);
1411
1412 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1413 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1414 std::cerr << "Cannot allocate stack object with greater alignment than"
1415 << " the stack alignment yet!";
1416 abort();
1417 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001418
Andrew Lenharth032f2352005-02-22 21:59:48 +00001419 Select(N.getOperand(0));
1420 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1421 {
1422 if (CN->getValue() < 32000)
1423 {
1424 BuildMI(BB, Alpha::LDA, 2, Alpha::R30)
1425 .addImm(-CN->getValue()).addReg(Alpha::R30);
1426 } else {
1427 Tmp1 = SelectExpr(N.getOperand(1));
1428 // Subtract size from stack pointer, thereby allocating some space.
1429 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1430 }
1431 } else {
1432 Tmp1 = SelectExpr(N.getOperand(1));
1433 // Subtract size from stack pointer, thereby allocating some space.
1434 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1435 }
1436
1437 // Put a pointer to the space into the result register, by copying the stack
1438 // pointer.
Andrew Lenharth7bc47022005-02-22 23:29:25 +00001439 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
Andrew Lenharth032f2352005-02-22 21:59:48 +00001440 return Result;
1441
Andrew Lenharth33819132005-03-04 20:09:23 +00001442// case ISD::ConstantPool:
1443// Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1444// AlphaLowering.restoreGP(BB);
1445// BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(Tmp1);
1446// return Result;
Andrew Lenharth2c594352005-01-29 15:42:07 +00001447
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001448 case ISD::FrameIndex:
Andrew Lenharth032f2352005-02-22 21:59:48 +00001449 BuildMI(BB, Alpha::LDA, 2, Result)
1450 .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
1451 .addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001452 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001453
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001454 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001455 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001456 case ISD::SEXTLOAD:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001457 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001458 {
1459 // Make sure we generate both values.
1460 if (Result != notIn)
1461 ExprMap[N.getValue(1)] = notIn; // Generate the token
1462 else
1463 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001464
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001465 SDOperand Chain = N.getOperand(0);
1466 SDOperand Address = N.getOperand(1);
1467 Select(Chain);
1468
Misha Brukman4633f1c2005-04-21 23:13:11 +00001469 assert(Node->getValueType(0) == MVT::i64 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001470 "Unknown type to sign extend to.");
Andrew Lenharth03824012005-02-07 05:55:55 +00001471 if (opcode == ISD::LOAD)
1472 Opc = Alpha::LDQ;
1473 else
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001474 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
1475 default: Node->dump(); assert(0 && "Bad sign extend!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001476 case MVT::i32: Opc = Alpha::LDL;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001477 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001478 case MVT::i16: Opc = Alpha::LDWU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001479 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001480 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Misha Brukman4633f1c2005-04-21 23:13:11 +00001481 case MVT::i8: Opc = Alpha::LDBU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001482 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001483 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001484
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001485 if (Address.getOpcode() == ISD::GlobalAddress) {
1486 AlphaLowering.restoreGP(BB);
1487 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001488 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001489 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1490 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001491 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1492 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001493 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001494 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001495 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001496 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001497 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001498 BuildMI(BB, Opc, 2, Result)
1499 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1500 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001501 } else {
1502 long offset;
1503 SelectAddr(Address, Tmp1, offset);
1504 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1505 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001506 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001507 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001508
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001509 case ISD::GlobalAddress:
1510 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001511 has_sym = true;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001512 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
1513 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
1514 return Result;
1515
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001516 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001517 case ISD::CALL:
1518 {
1519 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001520
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001521 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001522 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001523
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001524 //grab the arguments
1525 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001526 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001527 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001528 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001529
Andrew Lenharth684f2292005-01-30 00:35:27 +00001530 //in reg args
1531 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001532 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001533 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001534 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +00001535 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001536 Alpha::F19, Alpha::F20, Alpha::F21};
1537 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001538 default:
1539 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001540 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001541 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001542 N.getOperand(i+2).getValueType() << "\n";
1543 assert(0 && "Unknown value type for call");
1544 case MVT::i1:
1545 case MVT::i8:
1546 case MVT::i16:
1547 case MVT::i32:
1548 case MVT::i64:
1549 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1550 break;
1551 case MVT::f32:
1552 case MVT::f64:
1553 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1554 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001555 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001556 }
Andrew Lenharth684f2292005-01-30 00:35:27 +00001557 //in mem args
1558 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001559 {
1560 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001561 default:
1562 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001563 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001564 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001565 N.getOperand(i+2).getValueType() << "\n";
1566 assert(0 && "Unknown value type for call");
1567 case MVT::i1:
1568 case MVT::i8:
1569 case MVT::i16:
1570 case MVT::i32:
1571 case MVT::i64:
1572 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1573 break;
1574 case MVT::f32:
1575 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1576 break;
1577 case MVT::f64:
1578 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1579 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001580 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001581 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001582 //build the right kind of call
1583 if (GlobalAddressSDNode *GASD =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001584 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001585 {
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001586 if (GASD->getGlobal()->isExternal()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001587 //use safe calling convention
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001588 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001589 has_sym = true;
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001590 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal());
1591 } else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001592 //use PC relative branch call
Andrew Lenharth1e0d9bd2005-04-14 17:34:20 +00001593 AlphaLowering.restoreGP(BB);
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001594 BuildMI(BB, Alpha::BSR, 1, Alpha::R26).addGlobalAddress(GASD->getGlobal(),true);
1595 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001596 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001597 else if (ExternalSymbolSDNode *ESSDN =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001598 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001599 {
1600 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001601 has_sym = true;
Andrew Lenharthba05ad62005-03-30 18:22:52 +00001602 BuildMI(BB, Alpha::CALL, 1).addExternalSymbol(ESSDN->getSymbol(), true);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001603 } else {
1604 //no need to restore GP as we are doing an indirect call
1605 Tmp1 = SelectExpr(N.getOperand(1));
1606 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
1607 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
1608 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001609
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001610 //push the result into a virtual register
Misha Brukman4633f1c2005-04-21 23:13:11 +00001611
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001612 switch (Node->getValueType(0)) {
1613 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001614 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001615 case MVT::i1:
1616 case MVT::i8:
1617 case MVT::i16:
1618 case MVT::i32:
1619 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001620 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
1621 break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001622 case MVT::f32:
1623 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001624 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
1625 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001626 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001627 return Result+N.ResNo;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001628 }
1629
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001630 case ISD::SIGN_EXTEND_INREG:
1631 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001632 //do SDIV opt for all levels of ints if not dividing by a constant
1633 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
1634 && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001635 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001636 unsigned Tmp4 = MakeReg(MVT::f64);
1637 unsigned Tmp5 = MakeReg(MVT::f64);
1638 unsigned Tmp6 = MakeReg(MVT::f64);
1639 unsigned Tmp7 = MakeReg(MVT::f64);
1640 unsigned Tmp8 = MakeReg(MVT::f64);
1641 unsigned Tmp9 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001642
1643 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1644 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1645 MoveInt2FP(Tmp1, Tmp4, true);
1646 MoveInt2FP(Tmp2, Tmp5, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001647 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Tmp4);
1648 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Tmp5);
1649 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
1650 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Tmp8);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001651 MoveFP2Int(Tmp9, Result, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001652 return Result;
1653 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001654
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001655 //Alpha has instructions for a bunch of signed 32 bit stuff
1656 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001657 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001658 switch (N.getOperand(0).getOpcode()) {
1659 case ISD::ADD:
1660 case ISD::SUB:
1661 case ISD::MUL:
1662 {
1663 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
1664 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
1665 //FIXME: first check for Scaled Adds and Subs!
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001666 ConstantSDNode* CSD = NULL;
1667 if(!isMul && N.getOperand(0).getOperand(0).getOpcode() == ISD::SHL &&
1668 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(0).getOperand(1))) &&
1669 (CSD->getValue() == 2 || CSD->getValue() == 3))
1670 {
1671 bool use4 = CSD->getValue() == 2;
1672 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1673 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1674 BuildMI(BB, isAdd?(use4?Alpha::S4ADDL:Alpha::S8ADDL):(use4?Alpha::S4SUBL:Alpha::S8SUBL),
1675 2,Result).addReg(Tmp1).addReg(Tmp2);
1676 }
1677 else if(isAdd && N.getOperand(0).getOperand(1).getOpcode() == ISD::SHL &&
1678 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1).getOperand(1))) &&
1679 (CSD->getValue() == 2 || CSD->getValue() == 3))
1680 {
1681 bool use4 = CSD->getValue() == 2;
1682 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1683 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1684 BuildMI(BB, use4?Alpha::S4ADDL:Alpha::S8ADDL, 2,Result).addReg(Tmp1).addReg(Tmp2);
1685 }
1686 else if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001687 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
1688 { //Normal imm add/sub
1689 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001690 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001691 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
1692 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001693 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001694 else
1695 { //Normal add/sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001696 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULL : Alpha::SUBL);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001697 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001698 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001699 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1700 }
1701 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001702 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001703 default: break; //Fall Though;
1704 }
1705 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001706 Tmp1 = SelectExpr(N.getOperand(0));
1707 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001708 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001709 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001710 {
1711 default:
1712 Node->dump();
1713 assert(0 && "Sign Extend InReg not there yet");
1714 break;
1715 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001716 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001717 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001718 break;
1719 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001720 case MVT::i16:
1721 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
1722 break;
1723 case MVT::i8:
1724 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
1725 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +00001726 case MVT::i1:
1727 Tmp2 = MakeReg(MVT::i64);
1728 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001729 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +00001730 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001731 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001732 return Result;
1733 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001734
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001735 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001736 {
1737 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1738 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1739 bool isConst1 = false;
1740 bool isConst2 = false;
1741 int dir;
Misha Brukman7847fca2005-04-22 17:54:37 +00001742
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001743 //Tmp1 = SelectExpr(N.getOperand(0));
1744 if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001745 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
1746 isConst1 = true;
1747 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001748 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1749 isConst2 = true;
1750
1751 switch (SetCC->getCondition()) {
1752 default: Node->dump(); assert(0 && "Unknown integer comparison!");
1753 case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001754 case ISD::SETLT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001755 Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001756 case ISD::SETLE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001757 Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001758 case ISD::SETGT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001759 Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001760 case ISD::SETGE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001761 Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001762 case ISD::SETULT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001763 Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001764 case ISD::SETUGT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001765 Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001766 case ISD::SETULE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001767 Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001768 case ISD::SETUGE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001769 Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001770 case ISD::SETNE: {//Handle this one special
1771 //std::cerr << "Alpha does not have a setne.\n";
1772 //abort();
1773 Tmp1 = SelectExpr(N.getOperand(0));
1774 Tmp2 = SelectExpr(N.getOperand(1));
1775 Tmp3 = MakeReg(MVT::i64);
1776 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001777 //Remeber we have the Inv for this CC
1778 CCInvMap[N] = Tmp3;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001779 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001780 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001781 return Result;
1782 }
1783 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001784 if (dir == 1) {
1785 Tmp1 = SelectExpr(N.getOperand(0));
1786 if (isConst2) {
1787 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1788 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1789 } else {
1790 Tmp2 = SelectExpr(N.getOperand(1));
1791 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1792 }
1793 } else if (dir == 2) {
1794 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001795 if (isConst1) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001796 Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1797 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1798 } else {
1799 Tmp2 = SelectExpr(N.getOperand(0));
1800 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1801 }
1802 } else { //dir == 0
1803 if (isConst1) {
1804 Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1805 Tmp2 = SelectExpr(N.getOperand(1));
1806 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
1807 } else if (isConst2) {
1808 Tmp1 = SelectExpr(N.getOperand(0));
1809 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1810 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
1811 } else {
1812 Tmp1 = SelectExpr(N.getOperand(0));
1813 Tmp2 = SelectExpr(N.getOperand(1));
1814 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1815 }
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001816 }
1817 } else {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001818 //do the comparison
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001819 Tmp1 = MakeReg(MVT::f64);
1820 bool inv = SelectFPSetCC(N, Tmp1);
1821
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001822 //now arrange for Result (int) to have a 1 or 0
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001823 Tmp2 = MakeReg(MVT::i64);
1824 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001825 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001826 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001827 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001828 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001829 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001830 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001831
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001832 case ISD::CopyFromReg:
1833 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001834 ++count_ins;
1835
Andrew Lenharth40831c52005-01-28 06:57:18 +00001836 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001837 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001838 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001839 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001840 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001841
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001842 SDOperand Chain = N.getOperand(0);
1843
1844 Select(Chain);
1845 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1846 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1847 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1848 return Result;
1849 }
1850
Misha Brukman4633f1c2005-04-21 23:13:11 +00001851 //Most of the plain arithmetic and logic share the same form, and the same
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001852 //constant immediate test
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001853 case ISD::XOR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001854 //Match Not
1855 if (N.getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001856 cast<ConstantSDNode>(N.getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001857 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001858 Tmp1 = SelectExpr(N.getOperand(0));
1859 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1860 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001861 }
1862 //Fall through
1863 case ISD::AND:
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001864 //handle zap
1865 if (opcode == ISD::AND && N.getOperand(1).getOpcode() == ISD::Constant)
1866 {
1867 uint64_t k = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1868 unsigned int build = 0;
1869 for(int i = 0; i < 8; ++i)
1870 {
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001871 if ((k & 0x00FF) == 0x00FF)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001872 build |= 1 << i;
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001873 else if ((k & 0x00FF) != 0)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001874 { build = 0; break; }
1875 k >>= 8;
1876 }
1877 if (build)
1878 {
1879 Tmp1 = SelectExpr(N.getOperand(0));
1880 BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1881 return Result;
1882 }
1883 }
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001884 case ISD::OR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001885 //Check operand(0) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001886 if (N.getOperand(0).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001887 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001888 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001889 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001890 switch(opcode) {
1891 case ISD::AND: Opc = Alpha::BIC; break;
1892 case ISD::OR: Opc = Alpha::ORNOT; break;
1893 case ISD::XOR: Opc = Alpha::EQV; break;
1894 }
1895 Tmp1 = SelectExpr(N.getOperand(1));
1896 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1897 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1898 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001899 }
1900 //Check operand(1) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001901 if (N.getOperand(1).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001902 N.getOperand(1).getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001903 cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001904 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001905 switch(opcode) {
1906 case ISD::AND: Opc = Alpha::BIC; break;
1907 case ISD::OR: Opc = Alpha::ORNOT; break;
1908 case ISD::XOR: Opc = Alpha::EQV; break;
1909 }
1910 Tmp1 = SelectExpr(N.getOperand(0));
1911 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1912 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1913 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001914 }
1915 //Fall through
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001916 case ISD::SHL:
1917 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001918 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001919 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001920 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1921 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001922 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001923 {
1924 switch(opcode) {
1925 case ISD::AND: Opc = Alpha::ANDi; break;
1926 case ISD::OR: Opc = Alpha::BISi; break;
1927 case ISD::XOR: Opc = Alpha::XORi; break;
1928 case ISD::SHL: Opc = Alpha::SLi; break;
1929 case ISD::SRL: Opc = Alpha::SRLi; break;
1930 case ISD::SRA: Opc = Alpha::SRAi; break;
1931 case ISD::MUL: Opc = Alpha::MULQi; break;
1932 };
1933 Tmp1 = SelectExpr(N.getOperand(0));
1934 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1935 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1936 } else {
1937 switch(opcode) {
1938 case ISD::AND: Opc = Alpha::AND; break;
1939 case ISD::OR: Opc = Alpha::BIS; break;
1940 case ISD::XOR: Opc = Alpha::XOR; break;
1941 case ISD::SHL: Opc = Alpha::SL; break;
1942 case ISD::SRL: Opc = Alpha::SRL; break;
1943 case ISD::SRA: Opc = Alpha::SRA; break;
1944 case ISD::MUL: Opc = Alpha::MULQ; break;
1945 };
1946 Tmp1 = SelectExpr(N.getOperand(0));
1947 Tmp2 = SelectExpr(N.getOperand(1));
1948 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1949 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001950 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001951
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001952 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001953 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001954 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001955 bool isAdd = opcode == ISD::ADD;
1956
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001957 //first check for Scaled Adds and Subs!
1958 //Valid for add and sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001959 ConstantSDNode* CSD = NULL;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001960 if(N.getOperand(0).getOpcode() == ISD::SHL &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001961 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
1962 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001963 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001964 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001965 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001966 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) && CSD->getValue() <= 255)
1967 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1968 2, Result).addReg(Tmp2).addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001969 else {
1970 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001971 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1972 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001973 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001974 }
1975 //Position prevents subs
Andrew Lenharth273a1f92005-04-07 14:18:13 +00001976 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001977 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) &&
1978 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001979 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001980 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001981 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001982 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(0))) && CSD->getValue() <= 255)
1983 BuildMI(BB, use4?Alpha::S4ADDQi:Alpha::S8ADDQi, 2, Result).addReg(Tmp2)
1984 .addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001985 else {
1986 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001987 BuildMI(BB, use4?Alpha::S4ADDQ:Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001988 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001989 }
1990 //small addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001991 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
1992 CSD->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001993 { //Normal imm add/sub
1994 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1995 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001996 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CSD->getValue());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001997 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001998 //larger addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001999 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
2000 CSD->getSignExtended() <= 32767 &&
2001 CSD->getSignExtended() >= -32767)
Andrew Lenharth74d00d82005-03-02 17:23:03 +00002002 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002003 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002004 Tmp2 = (long)CSD->getSignExtended();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002005 if (!isAdd)
2006 Tmp2 = -Tmp2;
2007 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002008 }
2009 //give up and do the operation
2010 else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002011 //Normal add/sub
2012 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
2013 Tmp1 = SelectExpr(N.getOperand(0));
2014 Tmp2 = SelectExpr(N.getOperand(1));
2015 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
2016 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002017 return Result;
2018 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002019
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00002020 case ISD::SDIV:
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002021 {
Andrew Lenhartha565c272005-04-06 22:03:13 +00002022 ConstantSDNode* CSD;
2023 //check if we can convert into a shift!
2024 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
2025 (int64_t)CSD->getSignExtended() != 0 &&
2026 ExactLog2(abs((int64_t)CSD->getSignExtended())) != 0)
2027 {
2028 unsigned k = ExactLog2(abs(CSD->getSignExtended()));
2029 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenhartha565c272005-04-06 22:03:13 +00002030 if (k == 1)
2031 Tmp2 = Tmp1;
2032 else
2033 {
2034 Tmp2 = MakeReg(MVT::i64);
2035 BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
2036 }
2037 Tmp3 = MakeReg(MVT::i64);
2038 BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
2039 unsigned Tmp4 = MakeReg(MVT::i64);
2040 BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
2041 if ((int64_t)CSD->getSignExtended() > 0)
2042 BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
2043 else
2044 {
2045 unsigned Tmp5 = MakeReg(MVT::i64);
2046 BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
2047 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
2048 }
2049 return Result;
2050 }
2051 }
2052 //Else fall through
2053
2054 case ISD::UDIV:
2055 {
2056 ConstantSDNode* CSD;
2057 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
2058 ((int64_t)CSD->getSignExtended() >= 2 ||
2059 (int64_t)CSD->getSignExtended() <= -2))
2060 {
2061 // If this is a divide by constant, we can emit code using some magic
2062 // constants to implement it as a multiply instead.
2063 ExprMap.erase(N);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002064 if (opcode == ISD::SDIV)
Andrew Lenhartha565c272005-04-06 22:03:13 +00002065 return SelectExpr(BuildSDIVSequence(N));
2066 else
2067 return SelectExpr(BuildUDIVSequence(N));
2068 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00002069 }
2070 //else fall though
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002071 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +00002072 case ISD::SREM:
Misha Brukman4633f1c2005-04-21 23:13:11 +00002073 //FIXME: alpha really doesn't support any of these operations,
Andrew Lenharth40831c52005-01-28 06:57:18 +00002074 // the ops are expanded into special library calls with
2075 // special calling conventions
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002076 //Restore GP because it is a call after all...
Andrew Lenharth40831c52005-01-28 06:57:18 +00002077 switch(opcode) {
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00002078 case ISD::UREM: Opc = Alpha::REMQU; break;
2079 case ISD::SREM: Opc = Alpha::REMQ; break;
2080 case ISD::UDIV: Opc = Alpha::DIVQU; break;
2081 case ISD::SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002082 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002083 Tmp1 = SelectExpr(N.getOperand(0));
2084 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth33819132005-03-04 20:09:23 +00002085 //set up regs explicitly (helps Reg alloc)
2086 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002087 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00002088 AlphaLowering.restoreGP(BB);
Andrew Lenharth33819132005-03-04 20:09:23 +00002089 BuildMI(BB, Opc, 2).addReg(Alpha::R24).addReg(Alpha::R25);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002090 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002091 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002092
Andrew Lenharthe76797c2005-02-01 20:40:27 +00002093 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002094 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002095 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002096 assert (DestType == MVT::i64 && "only quads can be loaded to");
2097 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00002098 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002099 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002100 if (SrcType == MVT::f32)
Misha Brukman7847fca2005-04-22 17:54:37 +00002101 {
2102 Tmp2 = MakeReg(MVT::f64);
2103 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
2104 Tmp1 = Tmp2;
2105 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002106 Tmp2 = MakeReg(MVT::f64);
2107 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00002108 MoveFP2Int(Tmp2, Result, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002109
Andrew Lenharth7efadce2005-01-31 01:44:26 +00002110 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002111 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002112
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002113 case ISD::SELECT:
2114 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00002115 //FIXME: look at parent to decide if intCC can be folded, or if setCC(FP) and can save stack use
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002116 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002117 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2118 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002119 // Get the condition into the zero flag.
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002120 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002121
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002122 SDOperand CC = N.getOperand(0);
2123 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
2124
Misha Brukman4633f1c2005-04-21 23:13:11 +00002125 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002126 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
2127 { //FP Setcc -> Int Select
Misha Brukman7847fca2005-04-22 17:54:37 +00002128 Tmp1 = MakeReg(MVT::f64);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002129 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2130 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Misha Brukman7847fca2005-04-22 17:54:37 +00002131 bool inv = SelectFPSetCC(CC, Tmp1);
2132 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
2133 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
2134 return Result;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002135 }
2136 if (CC.getOpcode() == ISD::SETCC) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002137 //Int SetCC -> Select
2138 //Dropping the CC is only useful if we are comparing to 0
2139 if((SetCC->getOperand(1).getOpcode() == ISD::Constant &&
2140 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0) ||
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002141 (SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2142 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0))
2143 {
2144 //figure out a few things
2145 bool LeftZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2146 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
2147 bool RightZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2148 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
2149 bool LeftConst = N.getOperand(1).getOpcode() == ISD::Constant &&
2150 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255;
2151 bool RightConst = N.getOperand(2).getOpcode() == ISD::Constant &&
2152 cast<ConstantSDNode>(N.getOperand(2))->getValue() <= 255;
2153 bool useImm = LeftConst || RightConst;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002154
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002155 //Fix up CC
2156 ISD::CondCode cCode= SetCC->getCondition();
2157 if (RightConst && !LeftConst) //Invert sense to get Imm field right
2158 cCode = ISD::getSetCCInverse(cCode, true);
2159 if (LeftZero && !RightZero) //Swap Operands
2160 cCode = ISD::getSetCCSwappedOperands(cCode);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002161
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002162 //Choose the CMOV
2163 switch (cCode) {
2164 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
2165 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
2166 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
2167 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
2168 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
2169 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
2170 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
2171 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2172 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break; //Technically you could have this CC
2173 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
2174 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2175 }
2176 if(LeftZero && !RightZero) //swap Operands
2177 Tmp1 = SelectExpr(SetCC->getOperand(1)); //Cond
2178 else
2179 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
2180
2181 if (LeftConst) {
2182 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2183 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
Misha Brukman7847fca2005-04-22 17:54:37 +00002184 .addImm(cast<ConstantSDNode>(N.getOperand(1))->getValue())
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002185 .addReg(Tmp1);
2186 } else if (RightConst) {
2187 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
2188 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
Misha Brukman7847fca2005-04-22 17:54:37 +00002189 .addImm(cast<ConstantSDNode>(N.getOperand(2))->getValue())
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002190 .addReg(Tmp1);
2191 } else {
2192 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2193 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2194 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
2195 }
2196 return Result;
2197 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002198 //Otherwise, fall though
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002199 }
2200 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002201 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2202 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthe76797c2005-02-01 20:40:27 +00002203 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002204
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002205 return Result;
2206 }
2207
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002208 case ISD::Constant:
2209 {
Andrew Lenharthc0513832005-03-29 19:24:04 +00002210 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002211 if (val <= IMM_HIGH && val >= IMM_LOW) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002212 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002213 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002214 else if (val <= (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT &&
2215 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
2216 Tmp1 = MakeReg(MVT::i64);
2217 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val)).addReg(Alpha::R31);
2218 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002219 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002220 else {
2221 MachineConstantPool *CP = BB->getParent()->getConstantPool();
2222 ConstantUInt *C = ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
2223 unsigned CPI = CP->getConstantPoolIndex(C);
2224 AlphaLowering.restoreGP(BB);
2225 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CPI);
2226 }
2227 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002228 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002229 }
2230
2231 return 0;
2232}
2233
2234void ISel::Select(SDOperand N) {
2235 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00002236 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002237
Nate Begeman85fdeb22005-03-24 04:39:54 +00002238 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002239 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002240
2241 SDNode *Node = N.Val;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002242
Andrew Lenharth760270d2005-02-07 23:02:23 +00002243 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002244
2245 default:
2246 Node->dump(); std::cerr << "\n";
2247 assert(0 && "Node not handled yet!");
2248
2249 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00002250 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002251 return;
2252 }
2253
2254 case ISD::BR: {
2255 MachineBasicBlock *Dest =
2256 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2257
2258 Select(N.getOperand(0));
2259 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
2260 return;
2261 }
2262
2263 case ISD::ImplicitDef:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002264 ++count_ins;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002265 Select(N.getOperand(0));
2266 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
2267 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002268
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002269 case ISD::EntryToken: return; // Noop
2270
2271 case ISD::TokenFactor:
2272 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2273 Select(Node->getOperand(i));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002274
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002275 //N.Val->dump(); std::cerr << "\n";
2276 //assert(0 && "Node not handled yet!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00002277
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002278 return;
2279
2280 case ISD::CopyToReg:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002281 ++count_outs;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002282 Select(N.getOperand(0));
2283 Tmp1 = SelectExpr(N.getOperand(1));
2284 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002285
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002286 if (Tmp1 != Tmp2) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002287 if (N.getOperand(1).getValueType() == MVT::f64 ||
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002288 N.getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00002289 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2290 else
2291 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002292 }
2293 return;
2294
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002295 case ISD::RET:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002296 ++count_outs;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002297 switch (N.getNumOperands()) {
2298 default:
2299 std::cerr << N.getNumOperands() << "\n";
2300 for (unsigned i = 0; i < N.getNumOperands(); ++i)
2301 std::cerr << N.getOperand(i).getValueType() << "\n";
2302 Node->dump();
2303 assert(0 && "Unknown return instruction!");
2304 case 2:
2305 Select(N.getOperand(0));
2306 Tmp1 = SelectExpr(N.getOperand(1));
2307 switch (N.getOperand(1).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002308 default: Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002309 assert(0 && "All other types should have been promoted!!");
2310 case MVT::f64:
2311 case MVT::f32:
2312 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
2313 break;
2314 case MVT::i32:
2315 case MVT::i64:
2316 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
2317 break;
2318 }
2319 break;
2320 case 1:
2321 Select(N.getOperand(0));
2322 break;
2323 }
2324 //Tmp2 = AlphaLowering.getRetAddr();
2325 //BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(Tmp2).addReg(Tmp2);
2326 BuildMI(BB, Alpha::RETURN, 0); // Just emit a 'ret' instruction
2327 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002328
Misha Brukman4633f1c2005-04-21 23:13:11 +00002329 case ISD::TRUNCSTORE:
2330 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002331 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002332 SDOperand Chain = N.getOperand(0);
2333 SDOperand Value = N.getOperand(1);
2334 SDOperand Address = N.getOperand(2);
2335 Select(Chain);
2336
2337 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00002338
2339 if (opcode == ISD::STORE) {
2340 switch(Value.getValueType()) {
2341 default: assert(0 && "unknown Type in store");
2342 case MVT::i64: Opc = Alpha::STQ; break;
2343 case MVT::f64: Opc = Alpha::STT; break;
2344 case MVT::f32: Opc = Alpha::STS; break;
2345 }
2346 } else { //ISD::TRUNCSTORE
2347 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2348 default: assert(0 && "unknown Type in store");
2349 case MVT::i1: //FIXME: DAG does not promote this load
2350 case MVT::i8: Opc = Alpha::STB; break;
2351 case MVT::i16: Opc = Alpha::STW; break;
2352 case MVT::i32: Opc = Alpha::STL; break;
2353 }
Andrew Lenharth65838902005-02-06 16:22:15 +00002354 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00002355
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002356 if (Address.getOpcode() == ISD::GlobalAddress)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002357 {
2358 AlphaLowering.restoreGP(BB);
2359 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002360 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002361 BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
2362 }
Andrew Lenharth05380342005-02-07 05:07:00 +00002363 else if(Address.getOpcode() == ISD::FrameIndex)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002364 {
Andrew Lenharth032f2352005-02-22 21:59:48 +00002365 BuildMI(BB, Opc, 3).addReg(Tmp1)
2366 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
2367 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002368 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002369 else
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002370 {
2371 long offset;
2372 SelectAddr(Address, Tmp2, offset);
2373 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2374 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002375 return;
2376 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002377
2378 case ISD::EXTLOAD:
2379 case ISD::SEXTLOAD:
2380 case ISD::ZEXTLOAD:
2381 case ISD::LOAD:
2382 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002383 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002384 case ISD::CALL:
Andrew Lenharth032f2352005-02-22 21:59:48 +00002385 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002386 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002387 SelectExpr(N);
2388 return;
2389
Chris Lattner16cd04d2005-05-12 23:24:06 +00002390 case ISD::CALLSEQ_START:
2391 case ISD::CALLSEQ_END:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002392 Select(N.getOperand(0));
2393 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002394
Chris Lattner16cd04d2005-05-12 23:24:06 +00002395 Opc = N.getOpcode() == ISD::CALLSEQ_START ? Alpha::ADJUSTSTACKDOWN :
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002396 Alpha::ADJUSTSTACKUP;
2397 BuildMI(BB, Opc, 1).addImm(Tmp1);
2398 return;
Andrew Lenharth95762122005-03-31 21:24:06 +00002399
2400 case ISD::PCMARKER:
2401 Select(N.getOperand(0)); //Chain
2402 BuildMI(BB, Alpha::PCLABEL, 2).addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
2403 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002404 }
2405 assert(0 && "Should not be reached!");
2406}
2407
2408
2409/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
2410/// into a machine code representation using pattern matching and a machine
2411/// description file.
2412///
2413FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002414 return new ISel(TM);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002415}
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002416