blob: 53d8a9e561ca75941c61ed473cd036292a1c4a9a [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);
Misha Brukman4633f1c2005-04-21 23:13:11 +000039 cl::opt<bool> EnableAlphaFTOI("enable-alpha-ftoi",
40 cl::desc("Enable use of ftoi* and itof* instructions (ev6 and higher)"),
Andrew Lenharth95762122005-03-31 21:24:06 +000041 cl::Hidden);
Misha Brukman4633f1c2005-04-21 23:13:11 +000042 cl::opt<bool> EnableAlphaCount("enable-alpha-count",
43 cl::desc("Print estimates on live ins and outs"),
Andrew Lenhartha32b9e32005-04-08 17:28:49 +000044 cl::Hidden);
Andrew Lenharth95762122005-03-31 21:24:06 +000045}
46
Andrew Lenharth304d0f32005-01-22 23:41:55 +000047//===----------------------------------------------------------------------===//
48// AlphaTargetLowering - Alpha Implementation of the TargetLowering interface
49namespace {
50 class AlphaTargetLowering : public TargetLowering {
51 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
52 unsigned GP; //GOT vreg
53 public:
54 AlphaTargetLowering(TargetMachine &TM) : TargetLowering(TM) {
55 // Set up the TargetLowering object.
Andrew Lenharth3d65d312005-01-27 03:49:45 +000056 //I am having problems with shr n ubyte 1
Andrew Lenharth879ef222005-02-02 17:00:21 +000057 setShiftAmountType(MVT::i64);
58 setSetCCResultType(MVT::i64);
Andrew Lenharthd3355e22005-04-07 20:11:32 +000059 setSetCCResultContents(ZeroOrOneSetCCResult);
Misha Brukman4633f1c2005-04-21 23:13:11 +000060
Andrew Lenharth304d0f32005-01-22 23:41:55 +000061 addRegisterClass(MVT::i64, Alpha::GPRCRegisterClass);
62 addRegisterClass(MVT::f64, Alpha::FPRCRegisterClass);
Andrew Lenharth3d65d312005-01-27 03:49:45 +000063 addRegisterClass(MVT::f32, Alpha::FPRCRegisterClass);
Misha Brukman4633f1c2005-04-21 23:13:11 +000064
Chris Lattnerda4d4692005-04-09 03:22:37 +000065 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000066 setOperationAction(ISD::EXTLOAD , MVT::i1 , Promote);
67 setOperationAction(ISD::EXTLOAD , MVT::f32 , Promote);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000068
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000069 setOperationAction(ISD::ZEXTLOAD , MVT::i1 , Expand);
70 setOperationAction(ISD::ZEXTLOAD , MVT::i32 , Expand);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000071
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000072 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
73 setOperationAction(ISD::SEXTLOAD , MVT::i8 , Expand);
74 setOperationAction(ISD::SEXTLOAD , MVT::i16 , Expand);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000075
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000076 setOperationAction(ISD::SREM , MVT::f32 , Expand);
77 setOperationAction(ISD::SREM , MVT::f64 , Expand);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +000078
Andrew Lenharth691ef2b2005-05-03 17:19:30 +000079 // setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
80 // setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
81 // setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
82
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000083 //If this didn't legalize into a div....
84 // setOperationAction(ISD::SREM , MVT::i64, Expand);
85 // setOperationAction(ISD::UREM , MVT::i64, Expand);
86
87 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
88 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
89 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
Andrew Lenharth9818c052005-02-05 13:19:12 +000090
Chris Lattner17234b72005-04-30 04:26:06 +000091 // We don't support sin/cos/sqrt
92 setOperationAction(ISD::FSIN , MVT::f64, Expand);
93 setOperationAction(ISD::FCOS , MVT::f64, Expand);
94 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
95 setOperationAction(ISD::FSIN , MVT::f32, Expand);
96 setOperationAction(ISD::FCOS , MVT::f32, Expand);
97 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
98
Andrew Lenharth33819132005-03-04 20:09:23 +000099 //Doesn't work yet
Chris Lattner17234b72005-04-30 04:26:06 +0000100 setOperationAction(ISD::SETCC, MVT::f32, Promote);
Andrew Lenharth572af902005-02-14 05:41:43 +0000101
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000102 computeRegisterProperties();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000103
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000104 addLegalFPImmediate(+0.0); //F31
105 addLegalFPImmediate(-0.0); //-F31
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000106 }
107
108 /// LowerArguments - This hook must be implemented to indicate how we should
109 /// lower the arguments for the specified function, into the specified DAG.
110 virtual std::vector<SDOperand>
111 LowerArguments(Function &F, SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000112
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000113 /// LowerCallTo - This hook lowers an abstract call to a function into an
114 /// actual call.
115 virtual std::pair<SDOperand, SDOperand>
Nate Begeman8e21e712005-03-26 01:29:23 +0000116 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
117 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000118
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000119 virtual std::pair<SDOperand, SDOperand>
120 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000121
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000122 virtual std::pair<SDOperand,SDOperand>
123 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
124 const Type *ArgTy, SelectionDAG &DAG);
125
126 virtual std::pair<SDOperand, SDOperand>
127 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
128 SelectionDAG &DAG);
129
130 void restoreGP(MachineBasicBlock* BB)
131 {
132 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
133 }
134 };
135}
136
137//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
138
139//For now, just use variable size stack frame format
140
141//In a standard call, the first six items are passed in registers $16
142//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
143//of argument-to-register correspondence.) The remaining items are
144//collected in a memory argument list that is a naturally aligned
145//array of quadwords. In a standard call, this list, if present, must
146//be passed at 0(SP).
Misha Brukman7847fca2005-04-22 17:54:37 +0000147//7 ... n 0(SP) ... (n-7)*8(SP)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000148
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000149// //#define FP $15
150// //#define RA $26
151// //#define PV $27
152// //#define GP $29
153// //#define SP $30
Misha Brukman4633f1c2005-04-21 23:13:11 +0000154
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000155std::vector<SDOperand>
Misha Brukman4633f1c2005-04-21 23:13:11 +0000156AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000157{
158 std::vector<SDOperand> ArgValues;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000159 std::vector<SDOperand> LS;
160 SDOperand Chain = DAG.getRoot();
161
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000162 // assert(0 && "TODO");
163 MachineFunction &MF = DAG.getMachineFunction();
Andrew Lenharth05380342005-02-07 05:07:00 +0000164 MachineFrameInfo*MFI = MF.getFrameInfo();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000165
166 GP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
167 MachineBasicBlock& BB = MF.front();
168
169 //Handle the return address
170 //BuildMI(&BB, Alpha::IDEF, 0, Alpha::R26);
171
Misha Brukman4633f1c2005-04-21 23:13:11 +0000172 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000173 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000174 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000175 Alpha::F19, Alpha::F20, Alpha::F21};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000176 int count = 0;
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000177
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000178 //Def incoming registers
179 {
180 Function::arg_iterator I = F.arg_begin();
181 Function::arg_iterator E = F.arg_end();
182 for (int i = 0; i < 6; ++i)
183 {
184 if (F.isVarArg()) {
Andrew Lenharth0b04b5d2005-04-12 17:47:57 +0000185 MF.addLiveIn(args_int[i]);
186 MF.addLiveIn(args_float[i]);
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000187// BuildMI(&BB, Alpha::IDEF, 0, args_int[i]);
188// BuildMI(&BB, Alpha::IDEF, 0, args_float[i]);
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000189 } else if (I != E)
190 {
191 if(MVT::isInteger(getValueType(I->getType())))
Andrew Lenharth0b04b5d2005-04-12 17:47:57 +0000192 MF.addLiveIn(args_int[i]);
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000193// BuildMI(&BB, Alpha::IDEF, 0, args_int[i]);
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000194 else
Andrew Lenharth0b04b5d2005-04-12 17:47:57 +0000195 MF.addLiveIn(args_float[i]);
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000196// BuildMI(&BB, Alpha::IDEF, 0, args_float[i]);
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000197 ++I;
198 }
199 }
200 }
201
Andrew Lenharth0b04b5d2005-04-12 17:47:57 +0000202 MF.addLiveIn(Alpha::R29);
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000203// BuildMI(&BB, Alpha::IDEF, 0, Alpha::R29);
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000204 BuildMI(&BB, Alpha::BIS, 2, GP).addReg(Alpha::R29).addReg(Alpha::R29);
205
Chris Lattnere4d5c442005-03-15 04:54:21 +0000206 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000207 {
208 SDOperand newroot, argt;
209 if (count < 6) {
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000210 unsigned Vreg;
211 MVT::ValueType VT = getValueType(I->getType());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000212 switch (getValueType(I->getType())) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000213 default:
214 std::cerr << "Unknown Type " << VT << "\n";
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000215 abort();
216 case MVT::f64:
217 case MVT::f32:
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000218 Vreg = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(VT));
219 BuildMI(&BB, Alpha::CPYS, 2, Vreg).addReg(args_float[count]).addReg(args_float[count]);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000220 argt = newroot = DAG.getCopyFromReg(Vreg,
221 getValueType(I->getType()),
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000222 Chain);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000223 break;
224 case MVT::i1:
225 case MVT::i8:
226 case MVT::i16:
227 case MVT::i32:
228 case MVT::i64:
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000229 Vreg = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
230 BuildMI(&BB, Alpha::BIS, 2, Vreg).addReg(args_int[count]).addReg(args_int[count]);
231 argt = newroot = DAG.getCopyFromReg(Vreg, MVT::i64, Chain);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000232 if (getValueType(I->getType()) != MVT::i64)
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000233 argt = DAG.getNode(ISD::TRUNCATE, getValueType(I->getType()), newroot);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000234 break;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000235 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000236 } else { //more args
237 // Create the frame index object for this incoming parameter...
238 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000239
240 // Create the SelectionDAG nodes corresponding to a load
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000241 //from this parameter
242 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000243 argt = newroot = DAG.getLoad(getValueType(I->getType()),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000244 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000245 }
Andrew Lenharth032f2352005-02-22 21:59:48 +0000246 ++count;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000247 LS.push_back(newroot.getValue(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000248 ArgValues.push_back(argt);
249 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000250
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000251 // If the functions takes variable number of arguments, copy all regs to stack
Misha Brukman4633f1c2005-04-21 23:13:11 +0000252 if (F.isVarArg())
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000253 for (int i = 0; i < 6; ++i)
254 {
255 unsigned Vreg = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
256 BuildMI(&BB, Alpha::BIS, 2, Vreg).addReg(args_int[i]).addReg(args_int[i]);
257 SDOperand argt = DAG.getCopyFromReg(Vreg, MVT::i64, Chain);
258 int FI = MFI->CreateFixedObject(8, -8 * (6 - i));
259 SDOperand SDFI = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000260 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, argt, SDFI, DAG.getSrcValue(NULL)));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000261
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000262 Vreg = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::f64));
263 BuildMI(&BB, Alpha::CPYS, 2, Vreg).addReg(args_float[i]).addReg(args_float[i]);
264 argt = DAG.getCopyFromReg(Vreg, MVT::f64, Chain);
265 FI = MFI->CreateFixedObject(8, - 8 * (12 - i));
266 SDFI = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000267 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, argt, SDFI, DAG.getSrcValue(NULL)));
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000268 }
269
270 // If the function takes variable number of arguments, make a frame index for
271 // the start of the first arg value... for expansion of llvm.va_start.
272 // if (F.isVarArg())
273 // VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
274
275 //Set up a token factor with all the stack traffic
276 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, LS));
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000277
278 // Finally, inform the code generator which regs we return values in.
279 switch (getValueType(F.getReturnType())) {
280 default: assert(0 && "Unknown type!");
281 case MVT::isVoid: break;
282 case MVT::i1:
283 case MVT::i8:
284 case MVT::i16:
285 case MVT::i32:
286 case MVT::i64:
287 MF.addLiveOut(Alpha::R0);
288 break;
289 case MVT::f32:
290 case MVT::f64:
291 MF.addLiveOut(Alpha::F0);
292 break;
293 }
294
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000295 //return the arguments
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000296 return ArgValues;
297}
298
299std::pair<SDOperand, SDOperand>
300AlphaTargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000301 const Type *RetTy, bool isVarArg,
302 SDOperand Callee, ArgListTy &Args,
303 SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000304 int NumBytes = 0;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000305 if (Args.size() > 6)
306 NumBytes = (Args.size() - 6) * 8;
307
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000308 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000309 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000310 std::vector<SDOperand> args_to_use;
311 for (unsigned i = 0, e = Args.size(); i != e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000312 {
313 switch (getValueType(Args[i].second)) {
314 default: assert(0 && "Unexpected ValueType for argument!");
315 case MVT::i1:
316 case MVT::i8:
317 case MVT::i16:
318 case MVT::i32:
319 // Promote the integer to 64 bits. If the input type is signed use a
320 // sign extend, otherwise use a zero extend.
321 if (Args[i].second->isSigned())
322 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
323 else
324 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
325 break;
326 case MVT::i64:
327 case MVT::f64:
328 case MVT::f32:
329 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000330 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000331 args_to_use.push_back(Args[i].first);
332 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000333
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000334 std::vector<MVT::ValueType> RetVals;
335 MVT::ValueType RetTyVT = getValueType(RetTy);
336 if (RetTyVT != MVT::isVoid)
337 RetVals.push_back(RetTyVT);
338 RetVals.push_back(MVT::Other);
339
Misha Brukman4633f1c2005-04-21 23:13:11 +0000340 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000341 Chain, Callee, args_to_use), 0);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000342 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
343 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
344 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000345 return std::make_pair(TheCall, Chain);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000346}
347
348std::pair<SDOperand, SDOperand>
349AlphaTargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
350 //vastart just returns the address of the VarArgsFrameIndex slot.
351 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64), Chain);
352}
353
354std::pair<SDOperand,SDOperand> AlphaTargetLowering::
355LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000356 const Type *ArgTy, SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000357 abort();
358}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000359
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000360
361std::pair<SDOperand, SDOperand> AlphaTargetLowering::
362LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
363 SelectionDAG &DAG) {
364 abort();
365}
366
367
368
369
370
371namespace {
372
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000373//===--------------------------------------------------------------------===//
374/// ISel - Alpha specific code to select Alpha machine instructions for
375/// SelectionDAG operations.
376//===--------------------------------------------------------------------===//
377class ISel : public SelectionDAGISel {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000378
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000379 /// AlphaLowering - This object fully describes how to lower LLVM code to an
380 /// Alpha-specific SelectionDAG.
381 AlphaTargetLowering AlphaLowering;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000382
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000383 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
384 // for sdiv and udiv until it is put into the future
385 // dag combiner.
386
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000387 /// ExprMap - As shared expressions are codegen'd, we keep track of which
388 /// vreg the value is produced in, so we only emit one copy of each compiled
389 /// tree.
390 static const unsigned notIn = (unsigned)(-1);
391 std::map<SDOperand, unsigned> ExprMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000392
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000393 //CCInvMap sometimes (SetNE) we have the inverse CC code for free
394 std::map<SDOperand, unsigned> CCInvMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000395
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000396 int count_ins;
397 int count_outs;
398 bool has_sym;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000399 int max_depth;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000400
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000401public:
Misha Brukman4633f1c2005-04-21 23:13:11 +0000402 ISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering), AlphaLowering(TM)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000403 {}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000404
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000405 /// InstructionSelectBasicBlock - This callback is invoked by
406 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
407 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
Andrew Lenharth032f2352005-02-22 21:59:48 +0000408 DEBUG(BB->dump());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000409 count_ins = 0;
410 count_outs = 0;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000411 max_depth = 0;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000412 has_sym = false;
413
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000414 // Codegen the basic block.
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000415 ISelDAG = &DAG;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000416 max_depth = DAG.getRoot().getNodeDepth();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000417 Select(DAG.getRoot());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000418
419 if(has_sym)
420 ++count_ins;
421 if(EnableAlphaCount)
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000422 std::cerr << "COUNT: " << BB->getParent()->getFunction ()->getName() << " "
423 << BB->getNumber() << " "
424 << max_depth << " "
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000425 << count_ins << " "
426 << count_outs << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000427
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000428 // Clear state used for selection.
429 ExprMap.clear();
430 CCInvMap.clear();
431 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000432
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000433 unsigned SelectExpr(SDOperand N);
434 unsigned SelectExprFP(SDOperand N, unsigned Result);
435 void Select(SDOperand N);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000436
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000437 void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
438 void SelectBranchCC(SDOperand N);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000439 void MoveFP2Int(unsigned src, unsigned dst, bool isDouble);
440 void MoveInt2FP(unsigned src, unsigned dst, bool isDouble);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000441 //returns whether the sense of the comparison was inverted
442 bool SelectFPSetCC(SDOperand N, unsigned dst);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000443
444 // dag -> dag expanders for integer divide by constant
445 SDOperand BuildSDIVSequence(SDOperand N);
446 SDOperand BuildUDIVSequence(SDOperand N);
447
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000448};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000449}
450
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000451//Factorize a number using the list of constants
452static bool factorize(int v[], int res[], int size, uint64_t c)
453{
454 bool cont = true;
455 while (c != 1 && cont)
456 {
457 cont = false;
458 for(int i = 0; i < size; ++i)
459 {
460 if (c % v[i] == 0)
461 {
462 c /= v[i];
463 ++res[i];
464 cont=true;
465 }
466 }
467 }
468 return c == 1;
469}
470
471
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000472//Shamelessly adapted from PPC32
Misha Brukman4633f1c2005-04-21 23:13:11 +0000473// Structure used to return the necessary information to codegen an SDIV as
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000474// a multiply.
475struct ms {
476 int64_t m; // magic number
477 int64_t s; // shift amount
478};
479
480struct mu {
481 uint64_t m; // magic number
482 int64_t a; // add indicator
483 int64_t s; // shift amount
484};
485
486/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukman4633f1c2005-04-21 23:13:11 +0000487/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000488/// or -1.
489static struct ms magic(int64_t d) {
490 int64_t p;
491 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
492 const uint64_t two63 = 9223372036854775808ULL; // 2^63
493 struct ms mag;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000494
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000495 ad = abs(d);
496 t = two63 + ((uint64_t)d >> 63);
497 anc = t - 1 - t%ad; // absolute value of nc
Andrew Lenharth320174f2005-04-07 17:19:16 +0000498 p = 63; // initialize p
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000499 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
500 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
501 q2 = two63/ad; // initialize q2 = 2p/abs(d)
502 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
503 do {
504 p = p + 1;
505 q1 = 2*q1; // update q1 = 2p/abs(nc)
506 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
507 if (r1 >= anc) { // must be unsigned comparison
508 q1 = q1 + 1;
509 r1 = r1 - anc;
510 }
511 q2 = 2*q2; // update q2 = 2p/abs(d)
512 r2 = 2*r2; // update r2 = rem(2p/abs(d))
513 if (r2 >= ad) { // must be unsigned comparison
514 q2 = q2 + 1;
515 r2 = r2 - ad;
516 }
517 delta = ad - r2;
518 } while (q1 < delta || (q1 == delta && r1 == 0));
519
520 mag.m = q2 + 1;
521 if (d < 0) mag.m = -mag.m; // resulting magic number
522 mag.s = p - 64; // resulting shift
523 return mag;
524}
525
526/// magicu - calculate the magic numbers required to codegen an integer udiv as
527/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
528static struct mu magicu(uint64_t d)
529{
530 int64_t p;
531 uint64_t nc, delta, q1, r1, q2, r2;
532 struct mu magu;
533 magu.a = 0; // initialize "add" indicator
534 nc = - 1 - (-d)%d;
Andrew Lenharth320174f2005-04-07 17:19:16 +0000535 p = 63; // initialize p
536 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
537 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
538 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
539 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000540 do {
541 p = p + 1;
542 if (r1 >= nc - r1 ) {
543 q1 = 2*q1 + 1; // update q1
544 r1 = 2*r1 - nc; // update r1
545 }
546 else {
547 q1 = 2*q1; // update q1
548 r1 = 2*r1; // update r1
549 }
550 if (r2 + 1 >= d - r2) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000551 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000552 q2 = 2*q2 + 1; // update q2
553 r2 = 2*r2 + 1 - d; // update r2
554 }
555 else {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000556 if (q2 >= 0x8000000000000000ull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000557 q2 = 2*q2; // update q2
558 r2 = 2*r2 + 1; // update r2
559 }
560 delta = d - 1 - r2;
561 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
562 magu.m = q2 + 1; // resulting magic number
Andrew Lenharth320174f2005-04-07 17:19:16 +0000563 magu.s = p - 64; // resulting shift
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000564 return magu;
565}
566
567/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
568/// return a DAG expression to select that will generate the same value by
569/// multiplying by a magic number. See:
570/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
571SDOperand ISel::BuildSDIVSequence(SDOperand N) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000572 int64_t d = (int64_t)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000573 ms magics = magic(d);
574 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000575 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000576 ISelDAG->getConstant(magics.m, MVT::i64));
577 // If d > 0 and m < 0, add the numerator
578 if (d > 0 && magics.m < 0)
579 Q = ISelDAG->getNode(ISD::ADD, MVT::i64, Q, N.getOperand(0));
580 // If d < 0 and m > 0, subtract the numerator.
581 if (d < 0 && magics.m > 0)
582 Q = ISelDAG->getNode(ISD::SUB, MVT::i64, Q, N.getOperand(0));
583 // Shift right algebraic if shift value is nonzero
584 if (magics.s > 0)
Misha Brukman4633f1c2005-04-21 23:13:11 +0000585 Q = ISelDAG->getNode(ISD::SRA, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000586 ISelDAG->getConstant(magics.s, MVT::i64));
587 // Extract the sign bit and add it to the quotient
Misha Brukman4633f1c2005-04-21 23:13:11 +0000588 SDOperand T =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000589 ISelDAG->getNode(ISD::SRL, MVT::i64, Q, ISelDAG->getConstant(63, MVT::i64));
590 return ISelDAG->getNode(ISD::ADD, MVT::i64, Q, T);
591}
592
593/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
594/// return a DAG expression to select that will generate the same value by
595/// multiplying by a magic number. See:
596/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
597SDOperand ISel::BuildUDIVSequence(SDOperand N) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000598 unsigned d =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000599 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
600 mu magics = magicu(d);
601 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000602 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000603 ISelDAG->getConstant(magics.m, MVT::i64));
604 if (magics.a == 0) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000605 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000606 ISelDAG->getConstant(magics.s, MVT::i64));
607 } else {
608 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i64, N.getOperand(0), Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000609 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000610 ISelDAG->getConstant(1, MVT::i64));
611 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i64, NPQ, Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000612 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000613 ISelDAG->getConstant(magics.s-1, MVT::i64));
614 }
615 return Q;
616}
617
Andrew Lenhartha565c272005-04-06 22:03:13 +0000618//From PPC32
619/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
620/// returns zero when the input is not exactly a power of two.
621static unsigned ExactLog2(uint64_t Val) {
622 if (Val == 0 || (Val & (Val-1))) return 0;
623 unsigned Count = 0;
624 while (Val != 1) {
625 Val >>= 1;
626 ++Count;
627 }
628 return Count;
629}
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000630
631
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000632//These describe LDAx
Andrew Lenharthc0513832005-03-29 19:24:04 +0000633static const int IMM_LOW = -32768;
634static const int IMM_HIGH = 32767;
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000635static const int IMM_MULT = 65536;
636
637static long getUpper16(long l)
638{
639 long y = l / IMM_MULT;
640 if (l % IMM_MULT > IMM_HIGH)
641 ++y;
642 return y;
643}
644
645static long getLower16(long l)
646{
647 long h = getUpper16(l);
648 return l - h * IMM_MULT;
649}
650
Andrew Lenharth65838902005-02-06 16:22:15 +0000651static unsigned GetSymVersion(unsigned opcode)
652{
653 switch (opcode) {
654 default: assert(0 && "unknown load or store"); return 0;
655 case Alpha::LDQ: return Alpha::LDQ_SYM;
656 case Alpha::LDS: return Alpha::LDS_SYM;
657 case Alpha::LDT: return Alpha::LDT_SYM;
658 case Alpha::LDL: return Alpha::LDL_SYM;
659 case Alpha::LDBU: return Alpha::LDBU_SYM;
660 case Alpha::LDWU: return Alpha::LDWU_SYM;
661 case Alpha::LDW: return Alpha::LDW_SYM;
662 case Alpha::LDB: return Alpha::LDB_SYM;
663 case Alpha::STQ: return Alpha::STQ_SYM;
664 case Alpha::STS: return Alpha::STS_SYM;
665 case Alpha::STT: return Alpha::STT_SYM;
666 case Alpha::STL: return Alpha::STL_SYM;
667 case Alpha::STW: return Alpha::STW_SYM;
668 case Alpha::STB: return Alpha::STB_SYM;
669 }
670}
671
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000672void ISel::MoveFP2Int(unsigned src, unsigned dst, bool isDouble)
673{
674 unsigned Opc;
675 if (EnableAlphaFTOI) {
676 Opc = isDouble ? Alpha::FTOIT : Alpha::FTOIS;
677 BuildMI(BB, Opc, 1, dst).addReg(src);
678 } else {
679 //The hard way:
680 // Spill the integer to memory and reload it from there.
681 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
682 MachineFunction *F = BB->getParent();
683 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
684
685 Opc = isDouble ? Alpha::STT : Alpha::STS;
686 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
687 Opc = isDouble ? Alpha::LDQ : Alpha::LDL;
688 BuildMI(BB, Alpha::LDQ, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
689 }
690}
691
692void ISel::MoveInt2FP(unsigned src, unsigned dst, bool isDouble)
693{
694 unsigned Opc;
695 if (EnableAlphaFTOI) {
696 Opc = isDouble?Alpha::ITOFT:Alpha::ITOFS;
697 BuildMI(BB, Opc, 1, dst).addReg(src);
698 } else {
699 //The hard way:
700 // Spill the integer to memory and reload it from there.
701 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
702 MachineFunction *F = BB->getParent();
703 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
704
705 Opc = isDouble ? Alpha::STQ : Alpha::STL;
706 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
707 Opc = isDouble ? Alpha::LDT : Alpha::LDS;
708 BuildMI(BB, Opc, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
709 }
710}
711
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000712bool ISel::SelectFPSetCC(SDOperand N, unsigned dst)
713{
714 SDNode *Node = N.Val;
715 unsigned Opc, Tmp1, Tmp2, Tmp3;
716 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node);
717
718 //assert(SetCC->getOperand(0).getValueType() != MVT::f32 && "SetCC f32 should have been promoted");
719 bool rev = false;
720 bool inv = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000721
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000722 switch (SetCC->getCondition()) {
723 default: Node->dump(); assert(0 && "Unknown FP comparison!");
724 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
725 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
726 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
727 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
728 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
729 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
730 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000731
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000732 //FIXME: check for constant 0.0
733 ConstantFPSDNode *CN;
734 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
735 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
736 Tmp1 = Alpha::F31;
737 else
738 Tmp1 = SelectExpr(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000739
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000740 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
741 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
742 Tmp2 = Alpha::F31;
743 else
Chris Lattner9c9183a2005-04-30 04:44:07 +0000744 Tmp2 = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000745
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000746 //Can only compare doubles, and dag won't promote for me
747 if (SetCC->getOperand(0).getValueType() == MVT::f32)
748 {
749 //assert(0 && "Setcc On float?\n");
750 std::cerr << "Setcc on float!\n";
751 Tmp3 = MakeReg(MVT::f64);
752 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
753 Tmp1 = Tmp3;
754 }
755 if (SetCC->getOperand(1).getValueType() == MVT::f32)
756 {
757 //assert (0 && "Setcc On float?\n");
758 std::cerr << "Setcc on float!\n";
759 Tmp3 = MakeReg(MVT::f64);
760 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp2);
761 Tmp2 = Tmp3;
762 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000763
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000764 if (rev) std::swap(Tmp1, Tmp2);
765 //do the comparison
766 BuildMI(BB, Opc, 2, dst).addReg(Tmp1).addReg(Tmp2);
767 return inv;
768}
769
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000770//Check to see if the load is a constant offset from a base register
771void ISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
772{
773 unsigned opcode = N.getOpcode();
774 if (opcode == ISD::ADD) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000775 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000776 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
777 { //Normal imm add
778 Reg = SelectExpr(N.getOperand(0));
779 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
780 return;
781 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000782 else if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000783 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 32767)
784 {
785 Reg = SelectExpr(N.getOperand(1));
786 offset = cast<ConstantSDNode>(N.getOperand(0))->getValue();
787 return;
788 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000789 }
790 Reg = SelectExpr(N);
791 offset = 0;
792 return;
793}
794
Andrew Lenharth445171a2005-02-08 00:40:03 +0000795void ISel::SelectBranchCC(SDOperand N)
796{
797 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000798 MachineBasicBlock *Dest =
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000799 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
800 unsigned Opc = Alpha::WTF;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000801
Andrew Lenharth445171a2005-02-08 00:40:03 +0000802 Select(N.getOperand(0)); //chain
803 SDOperand CC = N.getOperand(1);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000804
Andrew Lenharth445171a2005-02-08 00:40:03 +0000805 if (CC.getOpcode() == ISD::SETCC)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000806 {
807 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
808 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
809 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000810 bool LeftZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
811 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
812 bool RightZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
813 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000814 bool isNE = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000815
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000816 //Fix up CC
817 ISD::CondCode cCode= SetCC->getCondition();
818 if (LeftZero && !RightZero) //Swap Operands
819 cCode = ISD::getSetCCSwappedOperands(cCode);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000820
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000821 if(cCode == ISD::SETNE)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000822 isNE = true;
Andrew Lenharth445171a2005-02-08 00:40:03 +0000823
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000824 if (LeftZero || RightZero) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000825 switch (SetCC->getCondition()) {
826 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
827 case ISD::SETEQ: Opc = Alpha::BEQ; break;
828 case ISD::SETLT: Opc = Alpha::BLT; break;
829 case ISD::SETLE: Opc = Alpha::BLE; break;
830 case ISD::SETGT: Opc = Alpha::BGT; break;
831 case ISD::SETGE: Opc = Alpha::BGE; break;
832 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
833 case ISD::SETUGT: Opc = Alpha::BNE; break;
834 case ISD::SETULE: Opc = Alpha::BEQ; break; //Technically you could have this CC
835 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
836 case ISD::SETNE: Opc = Alpha::BNE; break;
837 }
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000838 unsigned Tmp1;
839 if(LeftZero && !RightZero) //swap Operands
840 Tmp1 = SelectExpr(SetCC->getOperand(1)); //Cond
841 else
842 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000843 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
844 return;
845 } else {
846 unsigned Tmp1 = SelectExpr(CC);
847 if (isNE)
848 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
849 else
850 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000851 return;
852 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000853 } else { //FP
854 //Any comparison between 2 values should be codegened as an folded branch, as moving
855 //CC to the integer register is very expensive
856 //for a cmp b: c = a - b;
857 //a = b: c = 0
858 //a < b: c < 0
859 //a > b: c > 0
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000860
861 bool invTest = false;
862 unsigned Tmp3;
863
864 ConstantFPSDNode *CN;
865 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
866 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
867 Tmp3 = SelectExpr(SetCC->getOperand(0));
868 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
869 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
870 {
871 Tmp3 = SelectExpr(SetCC->getOperand(1));
872 invTest = true;
873 }
874 else
875 {
876 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
877 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
878 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
879 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
880 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
881 .addReg(Tmp1).addReg(Tmp2);
882 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000883
884 switch (SetCC->getCondition()) {
885 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000886 case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
887 case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
888 case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
889 case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
890 case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
891 case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000892 }
893 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000894 return;
895 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000896 abort(); //Should never be reached
897 } else {
898 //Giveup and do the stupid thing
899 unsigned Tmp1 = SelectExpr(CC);
900 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
901 return;
902 }
Andrew Lenharth445171a2005-02-08 00:40:03 +0000903 abort(); //Should never be reached
904}
905
Andrew Lenharth40831c52005-01-28 06:57:18 +0000906unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
907{
908 unsigned Tmp1, Tmp2, Tmp3;
909 unsigned Opc = 0;
910 SDNode *Node = N.Val;
911 MVT::ValueType DestType = N.getValueType();
912 unsigned opcode = N.getOpcode();
913
914 switch (opcode) {
915 default:
916 Node->dump();
917 assert(0 && "Node not handled!\n");
Andrew Lenharth2c594352005-01-29 15:42:07 +0000918
Andrew Lenharth7332f3e2005-04-02 19:11:07 +0000919 case ISD::UNDEF: {
920 BuildMI(BB, Alpha::IDEF, 0, Result);
921 return Result;
922 }
923
Andrew Lenharth30b46d42005-04-02 19:04:58 +0000924 case ISD::FNEG:
925 if(ISD::FABS == N.getOperand(0).getOpcode())
926 {
Misha Brukman7847fca2005-04-22 17:54:37 +0000927 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
928 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +0000929 } else {
Misha Brukman7847fca2005-04-22 17:54:37 +0000930 Tmp1 = SelectExpr(N.getOperand(0));
931 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +0000932 }
933 return Result;
934
935 case ISD::FABS:
936 Tmp1 = SelectExpr(N.getOperand(0));
937 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
938 return Result;
939
Andrew Lenharth9818c052005-02-05 13:19:12 +0000940 case ISD::SELECT:
941 {
Andrew Lenharth45859692005-03-03 21:47:53 +0000942 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
943 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
944 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
945
946 SDOperand CC = N.getOperand(0);
947 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
948
Misha Brukman4633f1c2005-04-21 23:13:11 +0000949 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth45859692005-03-03 21:47:53 +0000950 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
951 { //FP Setcc -> Select yay!
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000952
953
Andrew Lenharth45859692005-03-03 21:47:53 +0000954 //for a cmp b: c = a - b;
955 //a = b: c = 0
956 //a < b: c < 0
957 //a > b: c > 0
Misha Brukman4633f1c2005-04-21 23:13:11 +0000958
Andrew Lenharth45859692005-03-03 21:47:53 +0000959 bool invTest = false;
960 unsigned Tmp3;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000961
Andrew Lenharth45859692005-03-03 21:47:53 +0000962 ConstantFPSDNode *CN;
963 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
964 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
965 Tmp3 = SelectExpr(SetCC->getOperand(0));
966 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
967 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
968 {
969 Tmp3 = SelectExpr(SetCC->getOperand(1));
970 invTest = true;
971 }
972 else
973 {
974 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
975 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
976 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
977 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
978 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
979 .addReg(Tmp1).addReg(Tmp2);
980 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000981
Andrew Lenharth45859692005-03-03 21:47:53 +0000982 switch (SetCC->getCondition()) {
983 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
984 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNE : Alpha::FCMOVEQ; break;
985 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGT : Alpha::FCMOVLT; break;
986 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGE : Alpha::FCMOVLE; break;
987 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLT : Alpha::FCMOVGT; break;
988 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLE : Alpha::FCMOVGE; break;
989 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQ : Alpha::FCMOVNE; break;
990 }
Andrew Lenharth33819132005-03-04 20:09:23 +0000991 BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
Andrew Lenharth45859692005-03-03 21:47:53 +0000992 return Result;
993 }
994 else
995 {
996 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000997 BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV).addReg(Tmp1);
998// // Spill the cond to memory and reload it from there.
999// unsigned Tmp4 = MakeReg(MVT::f64);
1000// MoveIntFP(Tmp1, Tmp4, true);
1001// //now ideally, we don't have to do anything to the flag...
1002// // Get the condition into the zero flag.
1003// BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
Andrew Lenharth45859692005-03-03 21:47:53 +00001004 return Result;
1005 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001006 }
1007
Andrew Lenharthc1faced2005-02-01 01:37:24 +00001008 case ISD::FP_ROUND:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001009 assert (DestType == MVT::f32 &&
1010 N.getOperand(0).getValueType() == MVT::f64 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001011 "only f64 to f32 conversion supported here");
Andrew Lenharthc1faced2005-02-01 01:37:24 +00001012 Tmp1 = SelectExpr(N.getOperand(0));
1013 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
1014 return Result;
1015
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001016 case ISD::FP_EXTEND:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001017 assert (DestType == MVT::f64 &&
1018 N.getOperand(0).getValueType() == MVT::f32 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001019 "only f32 to f64 conversion supported here");
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001020 Tmp1 = SelectExpr(N.getOperand(0));
1021 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
1022 return Result;
1023
Andrew Lenharth2c594352005-01-29 15:42:07 +00001024 case ISD::CopyFromReg:
1025 {
1026 // Make sure we generate both values.
1027 if (Result != notIn)
1028 ExprMap[N.getValue(1)] = notIn; // Generate the token
1029 else
1030 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001031
Andrew Lenharth2c594352005-01-29 15:42:07 +00001032 SDOperand Chain = N.getOperand(0);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001033
Andrew Lenharth2c594352005-01-29 15:42:07 +00001034 Select(Chain);
1035 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1036 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1037 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
1038 return Result;
1039 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001040
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001041 case ISD::LOAD:
1042 {
1043 // Make sure we generate both values.
1044 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001045 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001046 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001047 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001048
Andrew Lenharth29219162005-02-07 06:31:44 +00001049 DestType = N.getValue(0).getValueType();
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001050
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001051 SDOperand Chain = N.getOperand(0);
1052 SDOperand Address = N.getOperand(1);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001053 Select(Chain);
Andrew Lenharth65838902005-02-06 16:22:15 +00001054 Opc = DestType == MVT::f64 ? Alpha::LDT : Alpha::LDS;
1055
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001056 if (Address.getOpcode() == ISD::GlobalAddress) {
1057 AlphaLowering.restoreGP(BB);
1058 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001059 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001060 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1061 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001062 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001063 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001064 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001065 has_sym = true;
Andrew Lenharth97127a12005-02-05 17:41:39 +00001066 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001067 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001068 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001069 BuildMI(BB, Opc, 2, Result)
1070 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1071 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001072 } else {
1073 long offset;
1074 SelectAddr(Address, Tmp1, offset);
1075 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1076 }
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001077 return Result;
1078 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001079 case ISD::ConstantFP:
1080 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
1081 if (CN->isExactlyValue(+0.0)) {
1082 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001083 } else if ( CN->isExactlyValue(-0.0)) {
1084 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001085 } else {
1086 abort();
1087 }
1088 }
1089 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001090
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001091 case ISD::SDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001092 case ISD::MUL:
1093 case ISD::ADD:
1094 case ISD::SUB:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001095 switch( opcode ) {
1096 case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS; break;
1097 case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS; break;
1098 case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS; break;
1099 case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS; break;
1100 };
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001101
1102 ConstantFPSDNode *CN;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001103 if (opcode == ISD::SUB
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001104 && (CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
1105 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1106 {
1107 Tmp2 = SelectExpr(N.getOperand(1));
1108 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp2).addReg(Tmp2);
1109 } else {
1110 Tmp1 = SelectExpr(N.getOperand(0));
1111 Tmp2 = SelectExpr(N.getOperand(1));
1112 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1113 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001114 return Result;
1115
Andrew Lenharth2c594352005-01-29 15:42:07 +00001116 case ISD::EXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001117 {
1118 //include a conversion sequence for float loads to double
1119 if (Result != notIn)
1120 ExprMap[N.getValue(1)] = notIn; // Generate the token
1121 else
1122 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001123
Andrew Lenhartha549deb2005-02-07 05:33:15 +00001124 Tmp1 = MakeReg(MVT::f32);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001125
1126 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001127 "EXTLOAD not from f32");
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001128 assert(Node->getValueType(0) == MVT::f64 && "EXTLOAD not to f64");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001129
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001130 SDOperand Chain = N.getOperand(0);
1131 SDOperand Address = N.getOperand(1);
1132 Select(Chain);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001133
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001134 if (Address.getOpcode() == ISD::GlobalAddress) {
1135 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001136 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001137 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1138 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001139 else if (ConstantPoolSDNode *CP =
1140 dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001141 {
1142 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001143 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001144 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addConstantPoolIndex(CP->getIndex());
1145 }
1146 else if(Address.getOpcode() == ISD::FrameIndex) {
1147 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
Andrew Lenharth032f2352005-02-22 21:59:48 +00001148 BuildMI(BB, Alpha::LDS, 2, Tmp1)
1149 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1150 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001151 } else {
1152 long offset;
1153 SelectAddr(Address, Tmp2, offset);
1154 BuildMI(BB, Alpha::LDS, 1, Tmp1).addImm(offset).addReg(Tmp2);
1155 }
Andrew Lenharth29219162005-02-07 06:31:44 +00001156 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001157 return Result;
1158 }
Andrew Lenharth2c594352005-01-29 15:42:07 +00001159
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001160 case ISD::UINT_TO_FP:
1161 case ISD::SINT_TO_FP:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001162 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001163 assert (N.getOperand(0).getValueType() == MVT::i64
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001164 && "only quads can be loaded from");
Andrew Lenharth40831c52005-01-28 06:57:18 +00001165 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001166 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001167 MoveInt2FP(Tmp1, Tmp2, true);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001168 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
1169 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001170 return Result;
1171 }
1172 }
1173 assert(0 && "should not get here");
1174 return 0;
1175}
1176
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001177unsigned ISel::SelectExpr(SDOperand N) {
1178 unsigned Result;
Andrew Lenharth2966e842005-04-07 18:15:28 +00001179 unsigned Tmp1, Tmp2 = 0, Tmp3;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001180 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001181 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001182
1183 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001184 MVT::ValueType DestType = N.getValueType();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001185
1186 unsigned &Reg = ExprMap[N];
1187 if (Reg) return Reg;
1188
1189 if (N.getOpcode() != ISD::CALL)
1190 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001191 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001192 else {
1193 // If this is a call instruction, make sure to prepare ALL of the result
1194 // values as well as the chain.
1195 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001196 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001197 else {
1198 Result = MakeReg(Node->getValueType(0));
1199 ExprMap[N.getValue(0)] = Result;
1200 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1201 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001202 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001203 }
1204 }
1205
Andrew Lenharth50d91d72005-04-30 14:19:13 +00001206 if ((DestType == MVT::f64 || DestType == MVT::f32 ||
1207 (
1208 (opcode == ISD::LOAD || opcode == ISD::CopyFromReg ||
1209 opcode == ISD::EXTLOAD) &&
1210 (N.getValue(0).getValueType() == MVT::f32 ||
1211 N.getValue(0).getValueType() == MVT::f64)
1212 ))
1213 && opcode != ISD::CALL
Andrew Lenharth06342c32005-02-07 06:21:37 +00001214 )
Andrew Lenharth40831c52005-01-28 06:57:18 +00001215 return SelectExprFP(N, Result);
1216
1217 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001218 default:
1219 Node->dump();
1220 assert(0 && "Node not handled!\n");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001221
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001222 case ISD::CTPOP:
1223 case ISD::CTTZ:
1224 case ISD::CTLZ:
1225 Opc = opcode == ISD::CTPOP ? Alpha::CTPOP :
1226 (opcode == ISD::CTTZ ? Alpha::CTTZ : Alpha::CTLZ);
1227 Tmp1 = SelectExpr(N.getOperand(0));
1228 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1229 return Result;
1230
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001231 case ISD::MULHU:
1232 Tmp1 = SelectExpr(N.getOperand(0));
1233 Tmp2 = SelectExpr(N.getOperand(1));
1234 BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth706be912005-04-07 13:55:53 +00001235 return Result;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001236 case ISD::MULHS:
1237 {
1238 //MULHU - Ra<63>*Rb - Rb<63>*Ra
1239 Tmp1 = SelectExpr(N.getOperand(0));
1240 Tmp2 = SelectExpr(N.getOperand(1));
1241 Tmp3 = MakeReg(MVT::i64);
1242 BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1243 unsigned V1 = MakeReg(MVT::i64);
1244 unsigned V2 = MakeReg(MVT::i64);
1245 BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31).addReg(Tmp1);
1246 BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31).addReg(Tmp2);
1247 unsigned IRes = MakeReg(MVT::i64);
1248 BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
1249 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
1250 return Result;
1251 }
Andrew Lenharth7332f3e2005-04-02 19:11:07 +00001252 case ISD::UNDEF: {
1253 BuildMI(BB, Alpha::IDEF, 0, Result);
1254 return Result;
1255 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001256
Andrew Lenharth032f2352005-02-22 21:59:48 +00001257 case ISD::DYNAMIC_STACKALLOC:
1258 // Generate both result values.
Andrew Lenharth3a7118d2005-02-23 17:33:42 +00001259 if (Result != notIn)
1260 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth032f2352005-02-22 21:59:48 +00001261 else
1262 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1263
1264 // FIXME: We are currently ignoring the requested alignment for handling
1265 // greater than the stack alignment. This will need to be revisited at some
1266 // point. Align = N.getOperand(2);
1267
1268 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1269 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1270 std::cerr << "Cannot allocate stack object with greater alignment than"
1271 << " the stack alignment yet!";
1272 abort();
1273 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001274
Andrew Lenharth032f2352005-02-22 21:59:48 +00001275 Select(N.getOperand(0));
1276 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1277 {
1278 if (CN->getValue() < 32000)
1279 {
1280 BuildMI(BB, Alpha::LDA, 2, Alpha::R30)
1281 .addImm(-CN->getValue()).addReg(Alpha::R30);
1282 } else {
1283 Tmp1 = SelectExpr(N.getOperand(1));
1284 // Subtract size from stack pointer, thereby allocating some space.
1285 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1286 }
1287 } else {
1288 Tmp1 = SelectExpr(N.getOperand(1));
1289 // Subtract size from stack pointer, thereby allocating some space.
1290 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1291 }
1292
1293 // Put a pointer to the space into the result register, by copying the stack
1294 // pointer.
Andrew Lenharth7bc47022005-02-22 23:29:25 +00001295 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
Andrew Lenharth032f2352005-02-22 21:59:48 +00001296 return Result;
1297
Andrew Lenharth33819132005-03-04 20:09:23 +00001298// case ISD::ConstantPool:
1299// Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1300// AlphaLowering.restoreGP(BB);
1301// BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(Tmp1);
1302// return Result;
Andrew Lenharth2c594352005-01-29 15:42:07 +00001303
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001304 case ISD::FrameIndex:
Andrew Lenharth032f2352005-02-22 21:59:48 +00001305 BuildMI(BB, Alpha::LDA, 2, Result)
1306 .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
1307 .addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001308 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001309
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001310 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001311 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001312 case ISD::SEXTLOAD:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001313 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001314 {
1315 // Make sure we generate both values.
1316 if (Result != notIn)
1317 ExprMap[N.getValue(1)] = notIn; // Generate the token
1318 else
1319 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001320
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001321 SDOperand Chain = N.getOperand(0);
1322 SDOperand Address = N.getOperand(1);
1323 Select(Chain);
1324
Misha Brukman4633f1c2005-04-21 23:13:11 +00001325 assert(Node->getValueType(0) == MVT::i64 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001326 "Unknown type to sign extend to.");
Andrew Lenharth03824012005-02-07 05:55:55 +00001327 if (opcode == ISD::LOAD)
1328 Opc = Alpha::LDQ;
1329 else
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001330 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
1331 default: Node->dump(); assert(0 && "Bad sign extend!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001332 case MVT::i32: Opc = Alpha::LDL;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001333 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001334 case MVT::i16: Opc = Alpha::LDWU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001335 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001336 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Misha Brukman4633f1c2005-04-21 23:13:11 +00001337 case MVT::i8: Opc = Alpha::LDBU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001338 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001339 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001340
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001341 if (Address.getOpcode() == ISD::GlobalAddress) {
1342 AlphaLowering.restoreGP(BB);
1343 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001344 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001345 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1346 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001347 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1348 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001349 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001350 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001351 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001352 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001353 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001354 BuildMI(BB, Opc, 2, Result)
1355 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1356 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001357 } else {
1358 long offset;
1359 SelectAddr(Address, Tmp1, offset);
1360 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1361 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001362 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001363 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001364
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001365 case ISD::GlobalAddress:
1366 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001367 has_sym = true;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001368 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
1369 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
1370 return Result;
1371
1372 case ISD::CALL:
1373 {
1374 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001375
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001376 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001377 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001378
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001379 //grab the arguments
1380 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001381 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001382 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001383 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001384
Andrew Lenharth684f2292005-01-30 00:35:27 +00001385 //in reg args
1386 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001387 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001388 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001389 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +00001390 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001391 Alpha::F19, Alpha::F20, Alpha::F21};
1392 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001393 default:
1394 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001395 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001396 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001397 N.getOperand(i+2).getValueType() << "\n";
1398 assert(0 && "Unknown value type for call");
1399 case MVT::i1:
1400 case MVT::i8:
1401 case MVT::i16:
1402 case MVT::i32:
1403 case MVT::i64:
1404 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1405 break;
1406 case MVT::f32:
1407 case MVT::f64:
1408 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1409 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001410 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001411 }
Andrew Lenharth684f2292005-01-30 00:35:27 +00001412 //in mem args
1413 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001414 {
1415 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001416 default:
1417 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001418 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001419 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001420 N.getOperand(i+2).getValueType() << "\n";
1421 assert(0 && "Unknown value type for call");
1422 case MVT::i1:
1423 case MVT::i8:
1424 case MVT::i16:
1425 case MVT::i32:
1426 case MVT::i64:
1427 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1428 break;
1429 case MVT::f32:
1430 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1431 break;
1432 case MVT::f64:
1433 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1434 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001435 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001436 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001437 //build the right kind of call
1438 if (GlobalAddressSDNode *GASD =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001439 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001440 {
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001441 if (GASD->getGlobal()->isExternal()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001442 //use safe calling convention
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001443 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001444 has_sym = true;
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001445 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal());
1446 } else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001447 //use PC relative branch call
Andrew Lenharth1e0d9bd2005-04-14 17:34:20 +00001448 AlphaLowering.restoreGP(BB);
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001449 BuildMI(BB, Alpha::BSR, 1, Alpha::R26).addGlobalAddress(GASD->getGlobal(),true);
1450 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001451 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001452 else if (ExternalSymbolSDNode *ESSDN =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001453 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001454 {
1455 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001456 has_sym = true;
Andrew Lenharthba05ad62005-03-30 18:22:52 +00001457 BuildMI(BB, Alpha::CALL, 1).addExternalSymbol(ESSDN->getSymbol(), true);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001458 } else {
1459 //no need to restore GP as we are doing an indirect call
1460 Tmp1 = SelectExpr(N.getOperand(1));
1461 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
1462 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
1463 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001464
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001465 //push the result into a virtual register
Misha Brukman4633f1c2005-04-21 23:13:11 +00001466
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001467 switch (Node->getValueType(0)) {
1468 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001469 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001470 case MVT::i1:
1471 case MVT::i8:
1472 case MVT::i16:
1473 case MVT::i32:
1474 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001475 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
1476 break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001477 case MVT::f32:
1478 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001479 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
1480 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001481 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001482 return Result+N.ResNo;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001483 }
1484
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001485 case ISD::SIGN_EXTEND_INREG:
1486 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001487 //do SDIV opt for all levels of ints if not dividing by a constant
1488 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
1489 && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001490 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001491 unsigned Tmp4 = MakeReg(MVT::f64);
1492 unsigned Tmp5 = MakeReg(MVT::f64);
1493 unsigned Tmp6 = MakeReg(MVT::f64);
1494 unsigned Tmp7 = MakeReg(MVT::f64);
1495 unsigned Tmp8 = MakeReg(MVT::f64);
1496 unsigned Tmp9 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001497
1498 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1499 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1500 MoveInt2FP(Tmp1, Tmp4, true);
1501 MoveInt2FP(Tmp2, Tmp5, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001502 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Tmp4);
1503 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Tmp5);
1504 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
1505 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Tmp8);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001506 MoveFP2Int(Tmp9, Result, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001507 return Result;
1508 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001509
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001510 //Alpha has instructions for a bunch of signed 32 bit stuff
1511 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001512 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001513 switch (N.getOperand(0).getOpcode()) {
1514 case ISD::ADD:
1515 case ISD::SUB:
1516 case ISD::MUL:
1517 {
1518 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
1519 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
1520 //FIXME: first check for Scaled Adds and Subs!
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001521 ConstantSDNode* CSD = NULL;
1522 if(!isMul && N.getOperand(0).getOperand(0).getOpcode() == ISD::SHL &&
1523 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(0).getOperand(1))) &&
1524 (CSD->getValue() == 2 || CSD->getValue() == 3))
1525 {
1526 bool use4 = CSD->getValue() == 2;
1527 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1528 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1529 BuildMI(BB, isAdd?(use4?Alpha::S4ADDL:Alpha::S8ADDL):(use4?Alpha::S4SUBL:Alpha::S8SUBL),
1530 2,Result).addReg(Tmp1).addReg(Tmp2);
1531 }
1532 else if(isAdd && N.getOperand(0).getOperand(1).getOpcode() == ISD::SHL &&
1533 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1).getOperand(1))) &&
1534 (CSD->getValue() == 2 || CSD->getValue() == 3))
1535 {
1536 bool use4 = CSD->getValue() == 2;
1537 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1538 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1539 BuildMI(BB, use4?Alpha::S4ADDL:Alpha::S8ADDL, 2,Result).addReg(Tmp1).addReg(Tmp2);
1540 }
1541 else if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001542 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
1543 { //Normal imm add/sub
1544 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001545 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001546 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
1547 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001548 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001549 else
1550 { //Normal add/sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001551 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULL : Alpha::SUBL);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001552 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001553 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001554 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1555 }
1556 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001557 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001558 default: break; //Fall Though;
1559 }
1560 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001561 Tmp1 = SelectExpr(N.getOperand(0));
1562 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001563 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001564 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001565 {
1566 default:
1567 Node->dump();
1568 assert(0 && "Sign Extend InReg not there yet");
1569 break;
1570 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001571 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001572 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001573 break;
1574 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001575 case MVT::i16:
1576 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
1577 break;
1578 case MVT::i8:
1579 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
1580 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +00001581 case MVT::i1:
1582 Tmp2 = MakeReg(MVT::i64);
1583 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001584 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +00001585 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001586 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001587 return Result;
1588 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001589
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001590 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001591 {
1592 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1593 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1594 bool isConst1 = false;
1595 bool isConst2 = false;
1596 int dir;
Misha Brukman7847fca2005-04-22 17:54:37 +00001597
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001598 //Tmp1 = SelectExpr(N.getOperand(0));
1599 if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001600 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
1601 isConst1 = true;
1602 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001603 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1604 isConst2 = true;
1605
1606 switch (SetCC->getCondition()) {
1607 default: Node->dump(); assert(0 && "Unknown integer comparison!");
1608 case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001609 case ISD::SETLT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001610 Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001611 case ISD::SETLE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001612 Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001613 case ISD::SETGT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001614 Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001615 case ISD::SETGE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001616 Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001617 case ISD::SETULT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001618 Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001619 case ISD::SETUGT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001620 Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001621 case ISD::SETULE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001622 Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001623 case ISD::SETUGE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001624 Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001625 case ISD::SETNE: {//Handle this one special
1626 //std::cerr << "Alpha does not have a setne.\n";
1627 //abort();
1628 Tmp1 = SelectExpr(N.getOperand(0));
1629 Tmp2 = SelectExpr(N.getOperand(1));
1630 Tmp3 = MakeReg(MVT::i64);
1631 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001632 //Remeber we have the Inv for this CC
1633 CCInvMap[N] = Tmp3;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001634 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001635 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001636 return Result;
1637 }
1638 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001639 if (dir == 1) {
1640 Tmp1 = SelectExpr(N.getOperand(0));
1641 if (isConst2) {
1642 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1643 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1644 } else {
1645 Tmp2 = SelectExpr(N.getOperand(1));
1646 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1647 }
1648 } else if (dir == 2) {
1649 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001650 if (isConst1) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001651 Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1652 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1653 } else {
1654 Tmp2 = SelectExpr(N.getOperand(0));
1655 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1656 }
1657 } else { //dir == 0
1658 if (isConst1) {
1659 Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1660 Tmp2 = SelectExpr(N.getOperand(1));
1661 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
1662 } else if (isConst2) {
1663 Tmp1 = SelectExpr(N.getOperand(0));
1664 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1665 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
1666 } else {
1667 Tmp1 = SelectExpr(N.getOperand(0));
1668 Tmp2 = SelectExpr(N.getOperand(1));
1669 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1670 }
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001671 }
1672 } else {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001673 //do the comparison
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001674 Tmp1 = MakeReg(MVT::f64);
1675 bool inv = SelectFPSetCC(N, Tmp1);
1676
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001677 //now arrange for Result (int) to have a 1 or 0
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001678 Tmp2 = MakeReg(MVT::i64);
1679 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001680 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001681 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001682 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001683 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001684 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001685 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001686
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001687 case ISD::CopyFromReg:
1688 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001689 ++count_ins;
1690
Andrew Lenharth40831c52005-01-28 06:57:18 +00001691 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001692 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001693 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001694 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001695 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001696
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001697 SDOperand Chain = N.getOperand(0);
1698
1699 Select(Chain);
1700 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1701 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1702 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1703 return Result;
1704 }
1705
Misha Brukman4633f1c2005-04-21 23:13:11 +00001706 //Most of the plain arithmetic and logic share the same form, and the same
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001707 //constant immediate test
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001708 case ISD::XOR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001709 //Match Not
1710 if (N.getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001711 cast<ConstantSDNode>(N.getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001712 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001713 Tmp1 = SelectExpr(N.getOperand(0));
1714 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1715 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001716 }
1717 //Fall through
1718 case ISD::AND:
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001719 //handle zap
1720 if (opcode == ISD::AND && N.getOperand(1).getOpcode() == ISD::Constant)
1721 {
1722 uint64_t k = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1723 unsigned int build = 0;
1724 for(int i = 0; i < 8; ++i)
1725 {
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001726 if ((k & 0x00FF) == 0x00FF)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001727 build |= 1 << i;
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001728 else if ((k & 0x00FF) != 0)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001729 { build = 0; break; }
1730 k >>= 8;
1731 }
1732 if (build)
1733 {
1734 Tmp1 = SelectExpr(N.getOperand(0));
1735 BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1736 return Result;
1737 }
1738 }
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001739 case ISD::OR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001740 //Check operand(0) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001741 if (N.getOperand(0).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001742 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001743 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001744 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001745 switch(opcode) {
1746 case ISD::AND: Opc = Alpha::BIC; break;
1747 case ISD::OR: Opc = Alpha::ORNOT; break;
1748 case ISD::XOR: Opc = Alpha::EQV; break;
1749 }
1750 Tmp1 = SelectExpr(N.getOperand(1));
1751 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1752 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1753 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001754 }
1755 //Check operand(1) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001756 if (N.getOperand(1).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001757 N.getOperand(1).getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001758 cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001759 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001760 switch(opcode) {
1761 case ISD::AND: Opc = Alpha::BIC; break;
1762 case ISD::OR: Opc = Alpha::ORNOT; break;
1763 case ISD::XOR: Opc = Alpha::EQV; break;
1764 }
1765 Tmp1 = SelectExpr(N.getOperand(0));
1766 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1767 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1768 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001769 }
1770 //Fall through
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001771 case ISD::SHL:
1772 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001773 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001774 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001775 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1776 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001777 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001778 {
1779 switch(opcode) {
1780 case ISD::AND: Opc = Alpha::ANDi; break;
1781 case ISD::OR: Opc = Alpha::BISi; break;
1782 case ISD::XOR: Opc = Alpha::XORi; break;
1783 case ISD::SHL: Opc = Alpha::SLi; break;
1784 case ISD::SRL: Opc = Alpha::SRLi; break;
1785 case ISD::SRA: Opc = Alpha::SRAi; break;
1786 case ISD::MUL: Opc = Alpha::MULQi; break;
1787 };
1788 Tmp1 = SelectExpr(N.getOperand(0));
1789 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1790 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1791 } else {
1792 switch(opcode) {
1793 case ISD::AND: Opc = Alpha::AND; break;
1794 case ISD::OR: Opc = Alpha::BIS; break;
1795 case ISD::XOR: Opc = Alpha::XOR; break;
1796 case ISD::SHL: Opc = Alpha::SL; break;
1797 case ISD::SRL: Opc = Alpha::SRL; break;
1798 case ISD::SRA: Opc = Alpha::SRA; break;
1799 case ISD::MUL: Opc = Alpha::MULQ; break;
1800 };
1801 Tmp1 = SelectExpr(N.getOperand(0));
1802 Tmp2 = SelectExpr(N.getOperand(1));
1803 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1804 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001805 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001806
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001807 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001808 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001809 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001810 bool isAdd = opcode == ISD::ADD;
1811
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001812 //first check for Scaled Adds and Subs!
1813 //Valid for add and sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001814 ConstantSDNode* CSD = NULL;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001815 if(N.getOperand(0).getOpcode() == ISD::SHL &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001816 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
1817 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001818 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001819 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001820 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001821 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) && CSD->getValue() <= 255)
1822 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1823 2, Result).addReg(Tmp2).addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001824 else {
1825 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001826 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1827 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001828 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001829 }
1830 //Position prevents subs
Andrew Lenharth273a1f92005-04-07 14:18:13 +00001831 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001832 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) &&
1833 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001834 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001835 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001836 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001837 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(0))) && CSD->getValue() <= 255)
1838 BuildMI(BB, use4?Alpha::S4ADDQi:Alpha::S8ADDQi, 2, Result).addReg(Tmp2)
1839 .addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001840 else {
1841 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001842 BuildMI(BB, use4?Alpha::S4ADDQ:Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001843 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001844 }
1845 //small addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001846 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
1847 CSD->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001848 { //Normal imm add/sub
1849 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1850 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001851 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CSD->getValue());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001852 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001853 //larger addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001854 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
1855 CSD->getSignExtended() <= 32767 &&
1856 CSD->getSignExtended() >= -32767)
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001857 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001858 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001859 Tmp2 = (long)CSD->getSignExtended();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001860 if (!isAdd)
1861 Tmp2 = -Tmp2;
1862 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001863 }
1864 //give up and do the operation
1865 else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001866 //Normal add/sub
1867 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1868 Tmp1 = SelectExpr(N.getOperand(0));
1869 Tmp2 = SelectExpr(N.getOperand(1));
1870 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1871 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001872 return Result;
1873 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001874
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001875 case ISD::SDIV:
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001876 {
Andrew Lenhartha565c272005-04-06 22:03:13 +00001877 ConstantSDNode* CSD;
1878 //check if we can convert into a shift!
1879 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
1880 (int64_t)CSD->getSignExtended() != 0 &&
1881 ExactLog2(abs((int64_t)CSD->getSignExtended())) != 0)
1882 {
1883 unsigned k = ExactLog2(abs(CSD->getSignExtended()));
1884 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenhartha565c272005-04-06 22:03:13 +00001885 if (k == 1)
1886 Tmp2 = Tmp1;
1887 else
1888 {
1889 Tmp2 = MakeReg(MVT::i64);
1890 BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
1891 }
1892 Tmp3 = MakeReg(MVT::i64);
1893 BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
1894 unsigned Tmp4 = MakeReg(MVT::i64);
1895 BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
1896 if ((int64_t)CSD->getSignExtended() > 0)
1897 BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
1898 else
1899 {
1900 unsigned Tmp5 = MakeReg(MVT::i64);
1901 BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
1902 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
1903 }
1904 return Result;
1905 }
1906 }
1907 //Else fall through
1908
1909 case ISD::UDIV:
1910 {
1911 ConstantSDNode* CSD;
1912 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
1913 ((int64_t)CSD->getSignExtended() >= 2 ||
1914 (int64_t)CSD->getSignExtended() <= -2))
1915 {
1916 // If this is a divide by constant, we can emit code using some magic
1917 // constants to implement it as a multiply instead.
1918 ExprMap.erase(N);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001919 if (opcode == ISD::SDIV)
Andrew Lenhartha565c272005-04-06 22:03:13 +00001920 return SelectExpr(BuildSDIVSequence(N));
1921 else
1922 return SelectExpr(BuildUDIVSequence(N));
1923 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001924 }
1925 //else fall though
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001926 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +00001927 case ISD::SREM:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001928 //FIXME: alpha really doesn't support any of these operations,
Andrew Lenharth40831c52005-01-28 06:57:18 +00001929 // the ops are expanded into special library calls with
1930 // special calling conventions
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001931 //Restore GP because it is a call after all...
Andrew Lenharth40831c52005-01-28 06:57:18 +00001932 switch(opcode) {
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001933 case ISD::UREM: Opc = Alpha::REMQU; break;
1934 case ISD::SREM: Opc = Alpha::REMQ; break;
1935 case ISD::UDIV: Opc = Alpha::DIVQU; break;
1936 case ISD::SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001937 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001938 Tmp1 = SelectExpr(N.getOperand(0));
1939 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth33819132005-03-04 20:09:23 +00001940 //set up regs explicitly (helps Reg alloc)
1941 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001942 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001943 AlphaLowering.restoreGP(BB);
Andrew Lenharth33819132005-03-04 20:09:23 +00001944 BuildMI(BB, Opc, 2).addReg(Alpha::R24).addReg(Alpha::R25);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001945 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001946 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001947
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001948 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001949 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001950 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001951 assert (DestType == MVT::i64 && "only quads can be loaded to");
1952 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00001953 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001954 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001955 if (SrcType == MVT::f32)
Misha Brukman7847fca2005-04-22 17:54:37 +00001956 {
1957 Tmp2 = MakeReg(MVT::f64);
1958 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
1959 Tmp1 = Tmp2;
1960 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001961 Tmp2 = MakeReg(MVT::f64);
1962 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001963 MoveFP2Int(Tmp2, Result, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001964
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001965 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001966 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001967
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001968 case ISD::SELECT:
1969 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001970 //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 +00001971 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001972 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1973 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001974 // Get the condition into the zero flag.
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001975 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001976
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001977 SDOperand CC = N.getOperand(0);
1978 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
1979
Misha Brukman4633f1c2005-04-21 23:13:11 +00001980 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001981 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
1982 { //FP Setcc -> Int Select
Misha Brukman7847fca2005-04-22 17:54:37 +00001983 Tmp1 = MakeReg(MVT::f64);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001984 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1985 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Misha Brukman7847fca2005-04-22 17:54:37 +00001986 bool inv = SelectFPSetCC(CC, Tmp1);
1987 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
1988 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
1989 return Result;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001990 }
1991 if (CC.getOpcode() == ISD::SETCC) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001992 //Int SetCC -> Select
1993 //Dropping the CC is only useful if we are comparing to 0
1994 if((SetCC->getOperand(1).getOpcode() == ISD::Constant &&
1995 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0) ||
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001996 (SetCC->getOperand(0).getOpcode() == ISD::Constant &&
1997 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0))
1998 {
1999 //figure out a few things
2000 bool LeftZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2001 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
2002 bool RightZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2003 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
2004 bool LeftConst = N.getOperand(1).getOpcode() == ISD::Constant &&
2005 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255;
2006 bool RightConst = N.getOperand(2).getOpcode() == ISD::Constant &&
2007 cast<ConstantSDNode>(N.getOperand(2))->getValue() <= 255;
2008 bool useImm = LeftConst || RightConst;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002009
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002010 //Fix up CC
2011 ISD::CondCode cCode= SetCC->getCondition();
2012 if (RightConst && !LeftConst) //Invert sense to get Imm field right
2013 cCode = ISD::getSetCCInverse(cCode, true);
2014 if (LeftZero && !RightZero) //Swap Operands
2015 cCode = ISD::getSetCCSwappedOperands(cCode);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002016
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002017 //Choose the CMOV
2018 switch (cCode) {
2019 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
2020 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
2021 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
2022 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
2023 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
2024 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
2025 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
2026 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2027 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break; //Technically you could have this CC
2028 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
2029 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2030 }
2031 if(LeftZero && !RightZero) //swap Operands
2032 Tmp1 = SelectExpr(SetCC->getOperand(1)); //Cond
2033 else
2034 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
2035
2036 if (LeftConst) {
2037 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2038 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
Misha Brukman7847fca2005-04-22 17:54:37 +00002039 .addImm(cast<ConstantSDNode>(N.getOperand(1))->getValue())
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002040 .addReg(Tmp1);
2041 } else if (RightConst) {
2042 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
2043 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
Misha Brukman7847fca2005-04-22 17:54:37 +00002044 .addImm(cast<ConstantSDNode>(N.getOperand(2))->getValue())
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002045 .addReg(Tmp1);
2046 } else {
2047 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2048 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2049 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
2050 }
2051 return Result;
2052 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002053 //Otherwise, fall though
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002054 }
2055 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002056 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2057 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthe76797c2005-02-01 20:40:27 +00002058 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002059
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002060 return Result;
2061 }
2062
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002063 case ISD::Constant:
2064 {
Andrew Lenharthc0513832005-03-29 19:24:04 +00002065 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002066 if (val <= IMM_HIGH && val >= IMM_LOW) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002067 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002068 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002069 else if (val <= (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT &&
2070 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
2071 Tmp1 = MakeReg(MVT::i64);
2072 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val)).addReg(Alpha::R31);
2073 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002074 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002075 else {
2076 MachineConstantPool *CP = BB->getParent()->getConstantPool();
2077 ConstantUInt *C = ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
2078 unsigned CPI = CP->getConstantPoolIndex(C);
2079 AlphaLowering.restoreGP(BB);
2080 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CPI);
2081 }
2082 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002083 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002084 }
2085
2086 return 0;
2087}
2088
2089void ISel::Select(SDOperand N) {
2090 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00002091 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002092
Nate Begeman85fdeb22005-03-24 04:39:54 +00002093 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002094 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002095
2096 SDNode *Node = N.Val;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002097
Andrew Lenharth760270d2005-02-07 23:02:23 +00002098 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002099
2100 default:
2101 Node->dump(); std::cerr << "\n";
2102 assert(0 && "Node not handled yet!");
2103
2104 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00002105 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002106 return;
2107 }
2108
2109 case ISD::BR: {
2110 MachineBasicBlock *Dest =
2111 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2112
2113 Select(N.getOperand(0));
2114 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
2115 return;
2116 }
2117
2118 case ISD::ImplicitDef:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002119 ++count_ins;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002120 Select(N.getOperand(0));
2121 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
2122 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002123
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002124 case ISD::EntryToken: return; // Noop
2125
2126 case ISD::TokenFactor:
2127 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2128 Select(Node->getOperand(i));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002129
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002130 //N.Val->dump(); std::cerr << "\n";
2131 //assert(0 && "Node not handled yet!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00002132
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002133 return;
2134
2135 case ISD::CopyToReg:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002136 ++count_outs;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002137 Select(N.getOperand(0));
2138 Tmp1 = SelectExpr(N.getOperand(1));
2139 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002140
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002141 if (Tmp1 != Tmp2) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002142 if (N.getOperand(1).getValueType() == MVT::f64 ||
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002143 N.getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00002144 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2145 else
2146 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002147 }
2148 return;
2149
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002150 case ISD::RET:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002151 ++count_outs;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002152 switch (N.getNumOperands()) {
2153 default:
2154 std::cerr << N.getNumOperands() << "\n";
2155 for (unsigned i = 0; i < N.getNumOperands(); ++i)
2156 std::cerr << N.getOperand(i).getValueType() << "\n";
2157 Node->dump();
2158 assert(0 && "Unknown return instruction!");
2159 case 2:
2160 Select(N.getOperand(0));
2161 Tmp1 = SelectExpr(N.getOperand(1));
2162 switch (N.getOperand(1).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002163 default: Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002164 assert(0 && "All other types should have been promoted!!");
2165 case MVT::f64:
2166 case MVT::f32:
2167 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
2168 break;
2169 case MVT::i32:
2170 case MVT::i64:
2171 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
2172 break;
2173 }
2174 break;
2175 case 1:
2176 Select(N.getOperand(0));
2177 break;
2178 }
2179 //Tmp2 = AlphaLowering.getRetAddr();
2180 //BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(Tmp2).addReg(Tmp2);
2181 BuildMI(BB, Alpha::RETURN, 0); // Just emit a 'ret' instruction
2182 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002183
Misha Brukman4633f1c2005-04-21 23:13:11 +00002184 case ISD::TRUNCSTORE:
2185 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002186 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002187 SDOperand Chain = N.getOperand(0);
2188 SDOperand Value = N.getOperand(1);
2189 SDOperand Address = N.getOperand(2);
2190 Select(Chain);
2191
2192 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00002193
2194 if (opcode == ISD::STORE) {
2195 switch(Value.getValueType()) {
2196 default: assert(0 && "unknown Type in store");
2197 case MVT::i64: Opc = Alpha::STQ; break;
2198 case MVT::f64: Opc = Alpha::STT; break;
2199 case MVT::f32: Opc = Alpha::STS; break;
2200 }
2201 } else { //ISD::TRUNCSTORE
2202 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2203 default: assert(0 && "unknown Type in store");
2204 case MVT::i1: //FIXME: DAG does not promote this load
2205 case MVT::i8: Opc = Alpha::STB; break;
2206 case MVT::i16: Opc = Alpha::STW; break;
2207 case MVT::i32: Opc = Alpha::STL; break;
2208 }
Andrew Lenharth65838902005-02-06 16:22:15 +00002209 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00002210
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002211 if (Address.getOpcode() == ISD::GlobalAddress)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002212 {
2213 AlphaLowering.restoreGP(BB);
2214 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002215 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002216 BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
2217 }
Andrew Lenharth05380342005-02-07 05:07:00 +00002218 else if(Address.getOpcode() == ISD::FrameIndex)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002219 {
Andrew Lenharth032f2352005-02-22 21:59:48 +00002220 BuildMI(BB, Opc, 3).addReg(Tmp1)
2221 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
2222 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002223 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002224 else
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002225 {
2226 long offset;
2227 SelectAddr(Address, Tmp2, offset);
2228 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2229 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002230 return;
2231 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002232
2233 case ISD::EXTLOAD:
2234 case ISD::SEXTLOAD:
2235 case ISD::ZEXTLOAD:
2236 case ISD::LOAD:
2237 case ISD::CopyFromReg:
2238 case ISD::CALL:
Andrew Lenharth032f2352005-02-22 21:59:48 +00002239 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002240 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002241 SelectExpr(N);
2242 return;
2243
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002244 case ISD::ADJCALLSTACKDOWN:
2245 case ISD::ADJCALLSTACKUP:
2246 Select(N.getOperand(0));
2247 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002248
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002249 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? Alpha::ADJUSTSTACKDOWN :
2250 Alpha::ADJUSTSTACKUP;
2251 BuildMI(BB, Opc, 1).addImm(Tmp1);
2252 return;
Andrew Lenharth95762122005-03-31 21:24:06 +00002253
2254 case ISD::PCMARKER:
2255 Select(N.getOperand(0)); //Chain
2256 BuildMI(BB, Alpha::PCLABEL, 2).addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
2257 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002258 }
2259 assert(0 && "Should not be reached!");
2260}
2261
2262
2263/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
2264/// into a machine code representation using pattern matching and a machine
2265/// description file.
2266///
2267FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002268 return new ISel(TM);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002269}
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002270