blob: 7314f7eed714de18a38dab69d8c624ddbfdfb475 [file] [log] [blame]
Misha Brukman2a8350a2005-02-05 02:24:26 +00001//===- AlphaISelPattern.cpp - A pattern matching inst selector for Alpha --===//
Misha Brukman4633f1c2005-04-21 23:13:11 +00002//
Andrew Lenharth304d0f32005-01-22 23:41:55 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman4633f1c2005-04-21 23:13:11 +00007//
Andrew Lenharth304d0f32005-01-22 23:41:55 +00008//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for Alpha.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Alpha.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000015#include "AlphaRegisterInfo.h"
16#include "llvm/Constants.h" // FIXME: REMOVE
17#include "llvm/Function.h"
18#include "llvm/CodeGen/MachineInstrBuilder.h"
19#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
20#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SelectionDAGISel.h"
24#include "llvm/CodeGen/SSARegMap.h"
25#include "llvm/Target/TargetData.h"
26#include "llvm/Target/TargetLowering.h"
27#include "llvm/Support/MathExtras.h"
28#include "llvm/ADT/Statistic.h"
Andrew Lenharth032f2352005-02-22 21:59:48 +000029#include "llvm/Support/Debug.h"
Andrew Lenharth95762122005-03-31 21:24:06 +000030#include "llvm/Support/CommandLine.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000031#include <set>
Andrew Lenharth684f2292005-01-30 00:35:27 +000032#include <algorithm>
Andrew Lenharth304d0f32005-01-22 23:41:55 +000033using namespace llvm;
34
Andrew Lenharth95762122005-03-31 21:24:06 +000035namespace llvm {
Misha Brukman4633f1c2005-04-21 23:13:11 +000036 cl::opt<bool> EnableAlphaIDIV("enable-alpha-intfpdiv",
37 cl::desc("Use the FP div instruction for integer div when possible"),
Andrew Lenharth95762122005-03-31 21:24:06 +000038 cl::Hidden);
Andrew Lenharth59009192005-05-04 19:12:09 +000039 cl::opt<bool> EnableAlphaFTOI("enable-alpha-FTOI",
Misha Brukman4633f1c2005-04-21 23:13:11 +000040 cl::desc("Enable use of ftoi* and itof* instructions (ev6 and higher)"),
Andrew Lenharth95762122005-03-31 21:24:06 +000041 cl::Hidden);
Andrew Lenharth59009192005-05-04 19:12:09 +000042 cl::opt<bool> EnableAlphaCT("enable-alpha-CT",
43 cl::desc("Enable use of the ctpop, ctlz, and cttz instructions"),
44 cl::Hidden);
Misha Brukman4633f1c2005-04-21 23:13:11 +000045 cl::opt<bool> EnableAlphaCount("enable-alpha-count",
46 cl::desc("Print estimates on live ins and outs"),
Andrew Lenhartha32b9e32005-04-08 17:28:49 +000047 cl::Hidden);
Andrew Lenharth95762122005-03-31 21:24:06 +000048}
49
Andrew Lenharth304d0f32005-01-22 23:41:55 +000050//===----------------------------------------------------------------------===//
51// AlphaTargetLowering - Alpha Implementation of the TargetLowering interface
52namespace {
53 class AlphaTargetLowering : public TargetLowering {
54 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
55 unsigned GP; //GOT vreg
56 public:
57 AlphaTargetLowering(TargetMachine &TM) : TargetLowering(TM) {
58 // Set up the TargetLowering object.
Andrew Lenharth3d65d312005-01-27 03:49:45 +000059 //I am having problems with shr n ubyte 1
Andrew Lenharth879ef222005-02-02 17:00:21 +000060 setShiftAmountType(MVT::i64);
61 setSetCCResultType(MVT::i64);
Andrew Lenharthd3355e22005-04-07 20:11:32 +000062 setSetCCResultContents(ZeroOrOneSetCCResult);
Misha Brukman4633f1c2005-04-21 23:13:11 +000063
Andrew Lenharth304d0f32005-01-22 23:41:55 +000064 addRegisterClass(MVT::i64, Alpha::GPRCRegisterClass);
65 addRegisterClass(MVT::f64, Alpha::FPRCRegisterClass);
Andrew Lenharth3d65d312005-01-27 03:49:45 +000066 addRegisterClass(MVT::f32, Alpha::FPRCRegisterClass);
Misha Brukman4633f1c2005-04-21 23:13:11 +000067
Chris Lattnerda4d4692005-04-09 03:22:37 +000068 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000069 setOperationAction(ISD::EXTLOAD , MVT::i1 , Promote);
70 setOperationAction(ISD::EXTLOAD , MVT::f32 , Promote);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000071
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000072 setOperationAction(ISD::ZEXTLOAD , MVT::i1 , Expand);
73 setOperationAction(ISD::ZEXTLOAD , MVT::i32 , Expand);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000074
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000075 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
76 setOperationAction(ISD::SEXTLOAD , MVT::i8 , Expand);
77 setOperationAction(ISD::SEXTLOAD , MVT::i16 , Expand);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000078
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000079 setOperationAction(ISD::SREM , MVT::f32 , Expand);
80 setOperationAction(ISD::SREM , MVT::f64 , Expand);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +000081
Andrew Lenharth59009192005-05-04 19:12:09 +000082 if (!EnableAlphaCT) {
83 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
84 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
Andrew Lenharthb5884d32005-05-04 19:25:37 +000085 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
Andrew Lenharth59009192005-05-04 19:12:09 +000086 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +000087
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000088 //If this didn't legalize into a div....
89 // setOperationAction(ISD::SREM , MVT::i64, Expand);
90 // setOperationAction(ISD::UREM , MVT::i64, Expand);
91
92 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
93 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
94 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
Andrew Lenharth9818c052005-02-05 13:19:12 +000095
Chris Lattner17234b72005-04-30 04:26:06 +000096 // We don't support sin/cos/sqrt
97 setOperationAction(ISD::FSIN , MVT::f64, Expand);
98 setOperationAction(ISD::FCOS , MVT::f64, Expand);
99 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
100 setOperationAction(ISD::FSIN , MVT::f32, Expand);
101 setOperationAction(ISD::FCOS , MVT::f32, Expand);
102 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
103
Andrew Lenharth33819132005-03-04 20:09:23 +0000104 //Doesn't work yet
Chris Lattner17234b72005-04-30 04:26:06 +0000105 setOperationAction(ISD::SETCC, MVT::f32, Promote);
Andrew Lenharth572af902005-02-14 05:41:43 +0000106
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000107 computeRegisterProperties();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000108
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000109 addLegalFPImmediate(+0.0); //F31
110 addLegalFPImmediate(-0.0); //-F31
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000111 }
112
113 /// LowerArguments - This hook must be implemented to indicate how we should
114 /// lower the arguments for the specified function, into the specified DAG.
115 virtual std::vector<SDOperand>
116 LowerArguments(Function &F, SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000117
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000118 /// LowerCallTo - This hook lowers an abstract call to a function into an
119 /// actual call.
120 virtual std::pair<SDOperand, SDOperand>
Chris Lattnerc57f6822005-05-12 19:56:45 +0000121 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Chris Lattneradf6a962005-05-13 18:50:42 +0000122 bool isTailCall, SDOperand Callee, ArgListTy &Args,
123 SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000124
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000125 virtual std::pair<SDOperand, SDOperand>
126 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000127
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000128 virtual std::pair<SDOperand,SDOperand>
129 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
130 const Type *ArgTy, SelectionDAG &DAG);
131
132 virtual std::pair<SDOperand, SDOperand>
133 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
134 SelectionDAG &DAG);
135
136 void restoreGP(MachineBasicBlock* BB)
137 {
138 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
139 }
140 };
141}
142
143//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
144
145//For now, just use variable size stack frame format
146
147//In a standard call, the first six items are passed in registers $16
148//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
149//of argument-to-register correspondence.) The remaining items are
150//collected in a memory argument list that is a naturally aligned
151//array of quadwords. In a standard call, this list, if present, must
152//be passed at 0(SP).
Misha Brukman7847fca2005-04-22 17:54:37 +0000153//7 ... n 0(SP) ... (n-7)*8(SP)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000154
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000155// //#define FP $15
156// //#define RA $26
157// //#define PV $27
158// //#define GP $29
159// //#define SP $30
Misha Brukman4633f1c2005-04-21 23:13:11 +0000160
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000161std::vector<SDOperand>
Misha Brukman4633f1c2005-04-21 23:13:11 +0000162AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000163{
164 std::vector<SDOperand> ArgValues;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000165 std::vector<SDOperand> LS;
166 SDOperand Chain = DAG.getRoot();
167
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000168 // assert(0 && "TODO");
169 MachineFunction &MF = DAG.getMachineFunction();
Andrew Lenharth05380342005-02-07 05:07:00 +0000170 MachineFrameInfo*MFI = MF.getFrameInfo();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000171
172 GP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
173 MachineBasicBlock& BB = MF.front();
174
175 //Handle the return address
176 //BuildMI(&BB, Alpha::IDEF, 0, Alpha::R26);
177
Misha Brukman4633f1c2005-04-21 23:13:11 +0000178 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000179 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000180 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000181 Alpha::F19, Alpha::F20, Alpha::F21};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000182 int count = 0;
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000183
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000184 //Def incoming registers
185 {
186 Function::arg_iterator I = F.arg_begin();
187 Function::arg_iterator E = F.arg_end();
188 for (int i = 0; i < 6; ++i)
189 {
190 if (F.isVarArg()) {
Andrew Lenharth0b04b5d2005-04-12 17:47:57 +0000191 MF.addLiveIn(args_int[i]);
192 MF.addLiveIn(args_float[i]);
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000193// BuildMI(&BB, Alpha::IDEF, 0, args_int[i]);
194// BuildMI(&BB, Alpha::IDEF, 0, args_float[i]);
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000195 } else if (I != E)
196 {
197 if(MVT::isInteger(getValueType(I->getType())))
Andrew Lenharth0b04b5d2005-04-12 17:47:57 +0000198 MF.addLiveIn(args_int[i]);
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000199// BuildMI(&BB, Alpha::IDEF, 0, args_int[i]);
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000200 else
Andrew Lenharth0b04b5d2005-04-12 17:47:57 +0000201 MF.addLiveIn(args_float[i]);
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000202// BuildMI(&BB, Alpha::IDEF, 0, args_float[i]);
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000203 ++I;
204 }
205 }
206 }
207
Andrew Lenharth0b04b5d2005-04-12 17:47:57 +0000208 MF.addLiveIn(Alpha::R29);
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000209// BuildMI(&BB, Alpha::IDEF, 0, Alpha::R29);
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000210 BuildMI(&BB, Alpha::BIS, 2, GP).addReg(Alpha::R29).addReg(Alpha::R29);
211
Chris Lattnere4d5c442005-03-15 04:54:21 +0000212 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000213 {
214 SDOperand newroot, argt;
215 if (count < 6) {
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000216 unsigned Vreg;
217 MVT::ValueType VT = getValueType(I->getType());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000218 switch (getValueType(I->getType())) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000219 default:
220 std::cerr << "Unknown Type " << VT << "\n";
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000221 abort();
222 case MVT::f64:
223 case MVT::f32:
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000224 Vreg = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(VT));
225 BuildMI(&BB, Alpha::CPYS, 2, Vreg).addReg(args_float[count]).addReg(args_float[count]);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000226 argt = newroot = DAG.getCopyFromReg(Vreg,
227 getValueType(I->getType()),
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000228 Chain);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000229 break;
230 case MVT::i1:
231 case MVT::i8:
232 case MVT::i16:
233 case MVT::i32:
234 case MVT::i64:
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000235 Vreg = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
236 BuildMI(&BB, Alpha::BIS, 2, Vreg).addReg(args_int[count]).addReg(args_int[count]);
237 argt = newroot = DAG.getCopyFromReg(Vreg, MVT::i64, Chain);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000238 if (getValueType(I->getType()) != MVT::i64)
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000239 argt = DAG.getNode(ISD::TRUNCATE, getValueType(I->getType()), newroot);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000240 break;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000241 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000242 } else { //more args
243 // Create the frame index object for this incoming parameter...
244 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000245
246 // Create the SelectionDAG nodes corresponding to a load
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000247 //from this parameter
248 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000249 argt = newroot = DAG.getLoad(getValueType(I->getType()),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000250 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000251 }
Andrew Lenharth032f2352005-02-22 21:59:48 +0000252 ++count;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000253 LS.push_back(newroot.getValue(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000254 ArgValues.push_back(argt);
255 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000256
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000257 // If the functions takes variable number of arguments, copy all regs to stack
Misha Brukman4633f1c2005-04-21 23:13:11 +0000258 if (F.isVarArg())
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000259 for (int i = 0; i < 6; ++i)
260 {
261 unsigned Vreg = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
262 BuildMI(&BB, Alpha::BIS, 2, Vreg).addReg(args_int[i]).addReg(args_int[i]);
263 SDOperand argt = DAG.getCopyFromReg(Vreg, MVT::i64, Chain);
264 int FI = MFI->CreateFixedObject(8, -8 * (6 - i));
265 SDOperand SDFI = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000266 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, argt, SDFI, DAG.getSrcValue(NULL)));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000267
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000268 Vreg = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::f64));
269 BuildMI(&BB, Alpha::CPYS, 2, Vreg).addReg(args_float[i]).addReg(args_float[i]);
270 argt = DAG.getCopyFromReg(Vreg, MVT::f64, Chain);
271 FI = MFI->CreateFixedObject(8, - 8 * (12 - i));
272 SDFI = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000273 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, argt, SDFI, DAG.getSrcValue(NULL)));
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000274 }
275
276 // If the function takes variable number of arguments, make a frame index for
277 // the start of the first arg value... for expansion of llvm.va_start.
278 // if (F.isVarArg())
279 // VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
280
281 //Set up a token factor with all the stack traffic
282 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, LS));
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000283
284 // Finally, inform the code generator which regs we return values in.
285 switch (getValueType(F.getReturnType())) {
286 default: assert(0 && "Unknown type!");
287 case MVT::isVoid: break;
288 case MVT::i1:
289 case MVT::i8:
290 case MVT::i16:
291 case MVT::i32:
292 case MVT::i64:
293 MF.addLiveOut(Alpha::R0);
294 break;
295 case MVT::f32:
296 case MVT::f64:
297 MF.addLiveOut(Alpha::F0);
298 break;
299 }
300
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000301 //return the arguments
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000302 return ArgValues;
303}
304
305std::pair<SDOperand, SDOperand>
306AlphaTargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000307 const Type *RetTy, bool isVarArg,
Chris Lattneradf6a962005-05-13 18:50:42 +0000308 unsigned CallingConv, bool isTailCall,
Misha Brukman7847fca2005-04-22 17:54:37 +0000309 SDOperand Callee, ArgListTy &Args,
310 SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000311 int NumBytes = 0;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000312 if (Args.size() > 6)
313 NumBytes = (Args.size() - 6) * 8;
314
Chris Lattner16cd04d2005-05-12 23:24:06 +0000315 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000316 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000317 std::vector<SDOperand> args_to_use;
318 for (unsigned i = 0, e = Args.size(); i != e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000319 {
320 switch (getValueType(Args[i].second)) {
321 default: assert(0 && "Unexpected ValueType for argument!");
322 case MVT::i1:
323 case MVT::i8:
324 case MVT::i16:
325 case MVT::i32:
326 // Promote the integer to 64 bits. If the input type is signed use a
327 // sign extend, otherwise use a zero extend.
328 if (Args[i].second->isSigned())
329 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
330 else
331 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
332 break;
333 case MVT::i64:
334 case MVT::f64:
335 case MVT::f32:
336 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000337 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000338 args_to_use.push_back(Args[i].first);
339 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000340
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000341 std::vector<MVT::ValueType> RetVals;
342 MVT::ValueType RetTyVT = getValueType(RetTy);
343 if (RetTyVT != MVT::isVoid)
344 RetVals.push_back(RetTyVT);
345 RetVals.push_back(MVT::Other);
346
Misha Brukman4633f1c2005-04-21 23:13:11 +0000347 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000348 Chain, Callee, args_to_use), 0);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000349 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner16cd04d2005-05-12 23:24:06 +0000350 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000351 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000352 return std::make_pair(TheCall, Chain);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000353}
354
355std::pair<SDOperand, SDOperand>
356AlphaTargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
357 //vastart just returns the address of the VarArgsFrameIndex slot.
358 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64), Chain);
359}
360
361std::pair<SDOperand,SDOperand> AlphaTargetLowering::
362LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000363 const Type *ArgTy, SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000364 abort();
365}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000366
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000367
368std::pair<SDOperand, SDOperand> AlphaTargetLowering::
369LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
370 SelectionDAG &DAG) {
371 abort();
372}
373
374
375
376
377
378namespace {
379
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000380//===--------------------------------------------------------------------===//
381/// ISel - Alpha specific code to select Alpha machine instructions for
382/// SelectionDAG operations.
383//===--------------------------------------------------------------------===//
384class ISel : public SelectionDAGISel {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000385
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000386 /// AlphaLowering - This object fully describes how to lower LLVM code to an
387 /// Alpha-specific SelectionDAG.
388 AlphaTargetLowering AlphaLowering;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000389
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000390 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
391 // for sdiv and udiv until it is put into the future
392 // dag combiner.
393
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000394 /// ExprMap - As shared expressions are codegen'd, we keep track of which
395 /// vreg the value is produced in, so we only emit one copy of each compiled
396 /// tree.
397 static const unsigned notIn = (unsigned)(-1);
398 std::map<SDOperand, unsigned> ExprMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000399
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000400 //CCInvMap sometimes (SetNE) we have the inverse CC code for free
401 std::map<SDOperand, unsigned> CCInvMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000402
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000403 int count_ins;
404 int count_outs;
405 bool has_sym;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000406 int max_depth;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000407
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000408public:
Misha Brukman4633f1c2005-04-21 23:13:11 +0000409 ISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering), AlphaLowering(TM)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000410 {}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000411
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000412 /// InstructionSelectBasicBlock - This callback is invoked by
413 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
414 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
Andrew Lenharth032f2352005-02-22 21:59:48 +0000415 DEBUG(BB->dump());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000416 count_ins = 0;
417 count_outs = 0;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000418 max_depth = 0;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000419 has_sym = false;
420
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000421 // Codegen the basic block.
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000422 ISelDAG = &DAG;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000423 max_depth = DAG.getRoot().getNodeDepth();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000424 Select(DAG.getRoot());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000425
426 if(has_sym)
427 ++count_ins;
428 if(EnableAlphaCount)
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000429 std::cerr << "COUNT: " << BB->getParent()->getFunction ()->getName() << " "
430 << BB->getNumber() << " "
431 << max_depth << " "
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000432 << count_ins << " "
433 << count_outs << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000434
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000435 // Clear state used for selection.
436 ExprMap.clear();
437 CCInvMap.clear();
438 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000439
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000440 unsigned SelectExpr(SDOperand N);
441 unsigned SelectExprFP(SDOperand N, unsigned Result);
442 void Select(SDOperand N);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000443
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000444 void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
445 void SelectBranchCC(SDOperand N);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000446 void MoveFP2Int(unsigned src, unsigned dst, bool isDouble);
447 void MoveInt2FP(unsigned src, unsigned dst, bool isDouble);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000448 //returns whether the sense of the comparison was inverted
449 bool SelectFPSetCC(SDOperand N, unsigned dst);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000450
451 // dag -> dag expanders for integer divide by constant
452 SDOperand BuildSDIVSequence(SDOperand N);
453 SDOperand BuildUDIVSequence(SDOperand N);
454
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000455};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000456}
457
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000458//Factorize a number using the list of constants
459static bool factorize(int v[], int res[], int size, uint64_t c)
460{
461 bool cont = true;
462 while (c != 1 && cont)
463 {
464 cont = false;
465 for(int i = 0; i < size; ++i)
466 {
467 if (c % v[i] == 0)
468 {
469 c /= v[i];
470 ++res[i];
471 cont=true;
472 }
473 }
474 }
475 return c == 1;
476}
477
478
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000479//Shamelessly adapted from PPC32
Misha Brukman4633f1c2005-04-21 23:13:11 +0000480// Structure used to return the necessary information to codegen an SDIV as
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000481// a multiply.
482struct ms {
483 int64_t m; // magic number
484 int64_t s; // shift amount
485};
486
487struct mu {
488 uint64_t m; // magic number
489 int64_t a; // add indicator
490 int64_t s; // shift amount
491};
492
493/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukman4633f1c2005-04-21 23:13:11 +0000494/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000495/// or -1.
496static struct ms magic(int64_t d) {
497 int64_t p;
498 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
499 const uint64_t two63 = 9223372036854775808ULL; // 2^63
500 struct ms mag;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000501
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000502 ad = abs(d);
503 t = two63 + ((uint64_t)d >> 63);
504 anc = t - 1 - t%ad; // absolute value of nc
Andrew Lenharth320174f2005-04-07 17:19:16 +0000505 p = 63; // initialize p
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000506 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
507 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
508 q2 = two63/ad; // initialize q2 = 2p/abs(d)
509 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
510 do {
511 p = p + 1;
512 q1 = 2*q1; // update q1 = 2p/abs(nc)
513 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
514 if (r1 >= anc) { // must be unsigned comparison
515 q1 = q1 + 1;
516 r1 = r1 - anc;
517 }
518 q2 = 2*q2; // update q2 = 2p/abs(d)
519 r2 = 2*r2; // update r2 = rem(2p/abs(d))
520 if (r2 >= ad) { // must be unsigned comparison
521 q2 = q2 + 1;
522 r2 = r2 - ad;
523 }
524 delta = ad - r2;
525 } while (q1 < delta || (q1 == delta && r1 == 0));
526
527 mag.m = q2 + 1;
528 if (d < 0) mag.m = -mag.m; // resulting magic number
529 mag.s = p - 64; // resulting shift
530 return mag;
531}
532
533/// magicu - calculate the magic numbers required to codegen an integer udiv as
534/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
535static struct mu magicu(uint64_t d)
536{
537 int64_t p;
538 uint64_t nc, delta, q1, r1, q2, r2;
539 struct mu magu;
540 magu.a = 0; // initialize "add" indicator
541 nc = - 1 - (-d)%d;
Andrew Lenharth320174f2005-04-07 17:19:16 +0000542 p = 63; // initialize p
543 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
544 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
545 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
546 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000547 do {
548 p = p + 1;
549 if (r1 >= nc - r1 ) {
550 q1 = 2*q1 + 1; // update q1
551 r1 = 2*r1 - nc; // update r1
552 }
553 else {
554 q1 = 2*q1; // update q1
555 r1 = 2*r1; // update r1
556 }
557 if (r2 + 1 >= d - r2) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000558 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000559 q2 = 2*q2 + 1; // update q2
560 r2 = 2*r2 + 1 - d; // update r2
561 }
562 else {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000563 if (q2 >= 0x8000000000000000ull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000564 q2 = 2*q2; // update q2
565 r2 = 2*r2 + 1; // update r2
566 }
567 delta = d - 1 - r2;
568 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
569 magu.m = q2 + 1; // resulting magic number
Andrew Lenharth320174f2005-04-07 17:19:16 +0000570 magu.s = p - 64; // resulting shift
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000571 return magu;
572}
573
574/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
575/// return a DAG expression to select that will generate the same value by
576/// multiplying by a magic number. See:
577/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
578SDOperand ISel::BuildSDIVSequence(SDOperand N) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000579 int64_t d = (int64_t)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000580 ms magics = magic(d);
581 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000582 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000583 ISelDAG->getConstant(magics.m, MVT::i64));
584 // If d > 0 and m < 0, add the numerator
585 if (d > 0 && magics.m < 0)
586 Q = ISelDAG->getNode(ISD::ADD, MVT::i64, Q, N.getOperand(0));
587 // If d < 0 and m > 0, subtract the numerator.
588 if (d < 0 && magics.m > 0)
589 Q = ISelDAG->getNode(ISD::SUB, MVT::i64, Q, N.getOperand(0));
590 // Shift right algebraic if shift value is nonzero
591 if (magics.s > 0)
Misha Brukman4633f1c2005-04-21 23:13:11 +0000592 Q = ISelDAG->getNode(ISD::SRA, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000593 ISelDAG->getConstant(magics.s, MVT::i64));
594 // Extract the sign bit and add it to the quotient
Misha Brukman4633f1c2005-04-21 23:13:11 +0000595 SDOperand T =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000596 ISelDAG->getNode(ISD::SRL, MVT::i64, Q, ISelDAG->getConstant(63, MVT::i64));
597 return ISelDAG->getNode(ISD::ADD, MVT::i64, Q, T);
598}
599
600/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
601/// return a DAG expression to select that will generate the same value by
602/// multiplying by a magic number. See:
603/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
604SDOperand ISel::BuildUDIVSequence(SDOperand N) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000605 unsigned d =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000606 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
607 mu magics = magicu(d);
608 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000609 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000610 ISelDAG->getConstant(magics.m, MVT::i64));
611 if (magics.a == 0) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000612 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000613 ISelDAG->getConstant(magics.s, MVT::i64));
614 } else {
615 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i64, N.getOperand(0), Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000616 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000617 ISelDAG->getConstant(1, MVT::i64));
618 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i64, NPQ, Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000619 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000620 ISelDAG->getConstant(magics.s-1, MVT::i64));
621 }
622 return Q;
623}
624
Andrew Lenhartha565c272005-04-06 22:03:13 +0000625//From PPC32
626/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
627/// returns zero when the input is not exactly a power of two.
628static unsigned ExactLog2(uint64_t Val) {
629 if (Val == 0 || (Val & (Val-1))) return 0;
630 unsigned Count = 0;
631 while (Val != 1) {
632 Val >>= 1;
633 ++Count;
634 }
635 return Count;
636}
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000637
638
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000639//These describe LDAx
Andrew Lenharthc0513832005-03-29 19:24:04 +0000640static const int IMM_LOW = -32768;
641static const int IMM_HIGH = 32767;
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000642static const int IMM_MULT = 65536;
643
644static long getUpper16(long l)
645{
646 long y = l / IMM_MULT;
647 if (l % IMM_MULT > IMM_HIGH)
648 ++y;
649 return y;
650}
651
652static long getLower16(long l)
653{
654 long h = getUpper16(l);
655 return l - h * IMM_MULT;
656}
657
Andrew Lenharth65838902005-02-06 16:22:15 +0000658static unsigned GetSymVersion(unsigned opcode)
659{
660 switch (opcode) {
661 default: assert(0 && "unknown load or store"); return 0;
662 case Alpha::LDQ: return Alpha::LDQ_SYM;
663 case Alpha::LDS: return Alpha::LDS_SYM;
664 case Alpha::LDT: return Alpha::LDT_SYM;
665 case Alpha::LDL: return Alpha::LDL_SYM;
666 case Alpha::LDBU: return Alpha::LDBU_SYM;
667 case Alpha::LDWU: return Alpha::LDWU_SYM;
668 case Alpha::LDW: return Alpha::LDW_SYM;
669 case Alpha::LDB: return Alpha::LDB_SYM;
670 case Alpha::STQ: return Alpha::STQ_SYM;
671 case Alpha::STS: return Alpha::STS_SYM;
672 case Alpha::STT: return Alpha::STT_SYM;
673 case Alpha::STL: return Alpha::STL_SYM;
674 case Alpha::STW: return Alpha::STW_SYM;
675 case Alpha::STB: return Alpha::STB_SYM;
676 }
677}
678
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000679void ISel::MoveFP2Int(unsigned src, unsigned dst, bool isDouble)
680{
681 unsigned Opc;
682 if (EnableAlphaFTOI) {
683 Opc = isDouble ? Alpha::FTOIT : Alpha::FTOIS;
684 BuildMI(BB, Opc, 1, dst).addReg(src);
685 } else {
686 //The hard way:
687 // Spill the integer to memory and reload it from there.
688 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
689 MachineFunction *F = BB->getParent();
690 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
691
692 Opc = isDouble ? Alpha::STT : Alpha::STS;
693 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
694 Opc = isDouble ? Alpha::LDQ : Alpha::LDL;
695 BuildMI(BB, Alpha::LDQ, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
696 }
697}
698
699void ISel::MoveInt2FP(unsigned src, unsigned dst, bool isDouble)
700{
701 unsigned Opc;
702 if (EnableAlphaFTOI) {
703 Opc = isDouble?Alpha::ITOFT:Alpha::ITOFS;
704 BuildMI(BB, Opc, 1, dst).addReg(src);
705 } else {
706 //The hard way:
707 // Spill the integer to memory and reload it from there.
708 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
709 MachineFunction *F = BB->getParent();
710 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
711
712 Opc = isDouble ? Alpha::STQ : Alpha::STL;
713 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
714 Opc = isDouble ? Alpha::LDT : Alpha::LDS;
715 BuildMI(BB, Opc, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
716 }
717}
718
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000719bool ISel::SelectFPSetCC(SDOperand N, unsigned dst)
720{
721 SDNode *Node = N.Val;
722 unsigned Opc, Tmp1, Tmp2, Tmp3;
723 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node);
724
725 //assert(SetCC->getOperand(0).getValueType() != MVT::f32 && "SetCC f32 should have been promoted");
726 bool rev = false;
727 bool inv = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000728
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000729 switch (SetCC->getCondition()) {
730 default: Node->dump(); assert(0 && "Unknown FP comparison!");
731 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
732 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
733 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
734 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
735 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
736 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
737 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000738
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000739 //FIXME: check for constant 0.0
740 ConstantFPSDNode *CN;
741 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
742 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
743 Tmp1 = Alpha::F31;
744 else
745 Tmp1 = SelectExpr(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000746
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000747 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
748 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
749 Tmp2 = Alpha::F31;
750 else
Chris Lattner9c9183a2005-04-30 04:44:07 +0000751 Tmp2 = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000752
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000753 //Can only compare doubles, and dag won't promote for me
754 if (SetCC->getOperand(0).getValueType() == MVT::f32)
755 {
756 //assert(0 && "Setcc On float?\n");
757 std::cerr << "Setcc on float!\n";
758 Tmp3 = MakeReg(MVT::f64);
759 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
760 Tmp1 = Tmp3;
761 }
762 if (SetCC->getOperand(1).getValueType() == MVT::f32)
763 {
764 //assert (0 && "Setcc On float?\n");
765 std::cerr << "Setcc on float!\n";
766 Tmp3 = MakeReg(MVT::f64);
767 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp2);
768 Tmp2 = Tmp3;
769 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000770
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000771 if (rev) std::swap(Tmp1, Tmp2);
772 //do the comparison
773 BuildMI(BB, Opc, 2, dst).addReg(Tmp1).addReg(Tmp2);
774 return inv;
775}
776
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000777//Check to see if the load is a constant offset from a base register
778void ISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
779{
780 unsigned opcode = N.getOpcode();
781 if (opcode == ISD::ADD) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000782 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000783 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
784 { //Normal imm add
785 Reg = SelectExpr(N.getOperand(0));
786 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
787 return;
788 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000789 else if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000790 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 32767)
791 {
792 Reg = SelectExpr(N.getOperand(1));
793 offset = cast<ConstantSDNode>(N.getOperand(0))->getValue();
794 return;
795 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000796 }
797 Reg = SelectExpr(N);
798 offset = 0;
799 return;
800}
801
Andrew Lenharth445171a2005-02-08 00:40:03 +0000802void ISel::SelectBranchCC(SDOperand N)
803{
804 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000805 MachineBasicBlock *Dest =
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000806 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
807 unsigned Opc = Alpha::WTF;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000808
Andrew Lenharth445171a2005-02-08 00:40:03 +0000809 Select(N.getOperand(0)); //chain
810 SDOperand CC = N.getOperand(1);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000811
Andrew Lenharth445171a2005-02-08 00:40:03 +0000812 if (CC.getOpcode() == ISD::SETCC)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000813 {
814 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
815 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
816 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000817 bool LeftZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
818 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
819 bool RightZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
820 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000821 bool isNE = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000822
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000823 //Fix up CC
824 ISD::CondCode cCode= SetCC->getCondition();
825 if (LeftZero && !RightZero) //Swap Operands
826 cCode = ISD::getSetCCSwappedOperands(cCode);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000827
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000828 if(cCode == ISD::SETNE)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000829 isNE = true;
Andrew Lenharth445171a2005-02-08 00:40:03 +0000830
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000831 if (LeftZero || RightZero) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000832 switch (SetCC->getCondition()) {
833 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
834 case ISD::SETEQ: Opc = Alpha::BEQ; break;
835 case ISD::SETLT: Opc = Alpha::BLT; break;
836 case ISD::SETLE: Opc = Alpha::BLE; break;
837 case ISD::SETGT: Opc = Alpha::BGT; break;
838 case ISD::SETGE: Opc = Alpha::BGE; break;
839 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
840 case ISD::SETUGT: Opc = Alpha::BNE; break;
841 case ISD::SETULE: Opc = Alpha::BEQ; break; //Technically you could have this CC
842 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
843 case ISD::SETNE: Opc = Alpha::BNE; break;
844 }
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000845 unsigned Tmp1;
846 if(LeftZero && !RightZero) //swap Operands
847 Tmp1 = SelectExpr(SetCC->getOperand(1)); //Cond
848 else
849 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000850 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
851 return;
852 } else {
853 unsigned Tmp1 = SelectExpr(CC);
854 if (isNE)
855 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
856 else
857 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000858 return;
859 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000860 } else { //FP
861 //Any comparison between 2 values should be codegened as an folded branch, as moving
862 //CC to the integer register is very expensive
863 //for a cmp b: c = a - b;
864 //a = b: c = 0
865 //a < b: c < 0
866 //a > b: c > 0
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000867
868 bool invTest = false;
869 unsigned Tmp3;
870
871 ConstantFPSDNode *CN;
872 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
873 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
874 Tmp3 = SelectExpr(SetCC->getOperand(0));
875 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
876 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
877 {
878 Tmp3 = SelectExpr(SetCC->getOperand(1));
879 invTest = true;
880 }
881 else
882 {
883 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
884 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
885 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
886 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
887 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
888 .addReg(Tmp1).addReg(Tmp2);
889 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000890
891 switch (SetCC->getCondition()) {
892 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000893 case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
894 case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
895 case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
896 case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
897 case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
898 case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000899 }
900 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000901 return;
902 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000903 abort(); //Should never be reached
904 } else {
905 //Giveup and do the stupid thing
906 unsigned Tmp1 = SelectExpr(CC);
907 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
908 return;
909 }
Andrew Lenharth445171a2005-02-08 00:40:03 +0000910 abort(); //Should never be reached
911}
912
Andrew Lenharth40831c52005-01-28 06:57:18 +0000913unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
914{
915 unsigned Tmp1, Tmp2, Tmp3;
916 unsigned Opc = 0;
917 SDNode *Node = N.Val;
918 MVT::ValueType DestType = N.getValueType();
919 unsigned opcode = N.getOpcode();
920
921 switch (opcode) {
922 default:
923 Node->dump();
924 assert(0 && "Node not handled!\n");
Andrew Lenharth2c594352005-01-29 15:42:07 +0000925
Andrew Lenharth7332f3e2005-04-02 19:11:07 +0000926 case ISD::UNDEF: {
927 BuildMI(BB, Alpha::IDEF, 0, Result);
928 return Result;
929 }
930
Andrew Lenharth30b46d42005-04-02 19:04:58 +0000931 case ISD::FNEG:
932 if(ISD::FABS == N.getOperand(0).getOpcode())
933 {
Misha Brukman7847fca2005-04-22 17:54:37 +0000934 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
935 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +0000936 } else {
Misha Brukman7847fca2005-04-22 17:54:37 +0000937 Tmp1 = SelectExpr(N.getOperand(0));
938 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +0000939 }
940 return Result;
941
942 case ISD::FABS:
943 Tmp1 = SelectExpr(N.getOperand(0));
944 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
945 return Result;
946
Andrew Lenharth9818c052005-02-05 13:19:12 +0000947 case ISD::SELECT:
948 {
Andrew Lenharth45859692005-03-03 21:47:53 +0000949 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
950 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
951 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
952
953 SDOperand CC = N.getOperand(0);
954 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
955
Misha Brukman4633f1c2005-04-21 23:13:11 +0000956 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth45859692005-03-03 21:47:53 +0000957 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
958 { //FP Setcc -> Select yay!
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000959
960
Andrew Lenharth45859692005-03-03 21:47:53 +0000961 //for a cmp b: c = a - b;
962 //a = b: c = 0
963 //a < b: c < 0
964 //a > b: c > 0
Misha Brukman4633f1c2005-04-21 23:13:11 +0000965
Andrew Lenharth45859692005-03-03 21:47:53 +0000966 bool invTest = false;
967 unsigned Tmp3;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000968
Andrew Lenharth45859692005-03-03 21:47:53 +0000969 ConstantFPSDNode *CN;
970 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
971 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
972 Tmp3 = SelectExpr(SetCC->getOperand(0));
973 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
974 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
975 {
976 Tmp3 = SelectExpr(SetCC->getOperand(1));
977 invTest = true;
978 }
979 else
980 {
981 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
982 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
983 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
984 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
985 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
986 .addReg(Tmp1).addReg(Tmp2);
987 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000988
Andrew Lenharth45859692005-03-03 21:47:53 +0000989 switch (SetCC->getCondition()) {
990 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
991 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNE : Alpha::FCMOVEQ; break;
992 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGT : Alpha::FCMOVLT; break;
993 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGE : Alpha::FCMOVLE; break;
994 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLT : Alpha::FCMOVGT; break;
995 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLE : Alpha::FCMOVGE; break;
996 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQ : Alpha::FCMOVNE; break;
997 }
Andrew Lenharth33819132005-03-04 20:09:23 +0000998 BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
Andrew Lenharth45859692005-03-03 21:47:53 +0000999 return Result;
1000 }
1001 else
1002 {
1003 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001004 BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV).addReg(Tmp1);
1005// // Spill the cond to memory and reload it from there.
1006// unsigned Tmp4 = MakeReg(MVT::f64);
1007// MoveIntFP(Tmp1, Tmp4, true);
1008// //now ideally, we don't have to do anything to the flag...
1009// // Get the condition into the zero flag.
1010// BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
Andrew Lenharth45859692005-03-03 21:47:53 +00001011 return Result;
1012 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001013 }
1014
Andrew Lenharthc1faced2005-02-01 01:37:24 +00001015 case ISD::FP_ROUND:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001016 assert (DestType == MVT::f32 &&
1017 N.getOperand(0).getValueType() == MVT::f64 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001018 "only f64 to f32 conversion supported here");
Andrew Lenharthc1faced2005-02-01 01:37:24 +00001019 Tmp1 = SelectExpr(N.getOperand(0));
1020 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
1021 return Result;
1022
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001023 case ISD::FP_EXTEND:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001024 assert (DestType == MVT::f64 &&
1025 N.getOperand(0).getValueType() == MVT::f32 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001026 "only f32 to f64 conversion supported here");
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001027 Tmp1 = SelectExpr(N.getOperand(0));
1028 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
1029 return Result;
1030
Andrew Lenharth2c594352005-01-29 15:42:07 +00001031 case ISD::CopyFromReg:
1032 {
1033 // Make sure we generate both values.
1034 if (Result != notIn)
1035 ExprMap[N.getValue(1)] = notIn; // Generate the token
1036 else
1037 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001038
Andrew Lenharth2c594352005-01-29 15:42:07 +00001039 SDOperand Chain = N.getOperand(0);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001040
Andrew Lenharth2c594352005-01-29 15:42:07 +00001041 Select(Chain);
1042 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1043 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1044 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
1045 return Result;
1046 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001047
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001048 case ISD::LOAD:
1049 {
1050 // Make sure we generate both values.
1051 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001052 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001053 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001054 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001055
Andrew Lenharth29219162005-02-07 06:31:44 +00001056 DestType = N.getValue(0).getValueType();
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001057
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001058 SDOperand Chain = N.getOperand(0);
1059 SDOperand Address = N.getOperand(1);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001060 Select(Chain);
Andrew Lenharth65838902005-02-06 16:22:15 +00001061 Opc = DestType == MVT::f64 ? Alpha::LDT : Alpha::LDS;
1062
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001063 if (Address.getOpcode() == ISD::GlobalAddress) {
1064 AlphaLowering.restoreGP(BB);
1065 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001066 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001067 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1068 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001069 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001070 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001071 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001072 has_sym = true;
Andrew Lenharth97127a12005-02-05 17:41:39 +00001073 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001074 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001075 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001076 BuildMI(BB, Opc, 2, Result)
1077 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1078 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001079 } else {
1080 long offset;
1081 SelectAddr(Address, Tmp1, offset);
1082 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1083 }
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001084 return Result;
1085 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001086 case ISD::ConstantFP:
1087 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
1088 if (CN->isExactlyValue(+0.0)) {
1089 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001090 } else if ( CN->isExactlyValue(-0.0)) {
1091 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001092 } else {
1093 abort();
1094 }
1095 }
1096 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001097
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001098 case ISD::SDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001099 case ISD::MUL:
1100 case ISD::ADD:
1101 case ISD::SUB:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001102 switch( opcode ) {
1103 case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS; break;
1104 case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS; break;
1105 case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS; break;
1106 case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS; break;
1107 };
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001108
1109 ConstantFPSDNode *CN;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001110 if (opcode == ISD::SUB
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001111 && (CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
1112 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1113 {
1114 Tmp2 = SelectExpr(N.getOperand(1));
1115 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp2).addReg(Tmp2);
1116 } else {
1117 Tmp1 = SelectExpr(N.getOperand(0));
1118 Tmp2 = SelectExpr(N.getOperand(1));
1119 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1120 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001121 return Result;
1122
Andrew Lenharth2c594352005-01-29 15:42:07 +00001123 case ISD::EXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001124 {
1125 //include a conversion sequence for float loads to double
1126 if (Result != notIn)
1127 ExprMap[N.getValue(1)] = notIn; // Generate the token
1128 else
1129 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001130
Andrew Lenhartha549deb2005-02-07 05:33:15 +00001131 Tmp1 = MakeReg(MVT::f32);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001132
1133 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001134 "EXTLOAD not from f32");
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001135 assert(Node->getValueType(0) == MVT::f64 && "EXTLOAD not to f64");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001136
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001137 SDOperand Chain = N.getOperand(0);
1138 SDOperand Address = N.getOperand(1);
1139 Select(Chain);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001140
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001141 if (Address.getOpcode() == ISD::GlobalAddress) {
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).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1145 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001146 else if (ConstantPoolSDNode *CP =
1147 dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001148 {
1149 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001150 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001151 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addConstantPoolIndex(CP->getIndex());
1152 }
1153 else if(Address.getOpcode() == ISD::FrameIndex) {
1154 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
Andrew Lenharth032f2352005-02-22 21:59:48 +00001155 BuildMI(BB, Alpha::LDS, 2, Tmp1)
1156 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1157 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001158 } else {
1159 long offset;
1160 SelectAddr(Address, Tmp2, offset);
1161 BuildMI(BB, Alpha::LDS, 1, Tmp1).addImm(offset).addReg(Tmp2);
1162 }
Andrew Lenharth29219162005-02-07 06:31:44 +00001163 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001164 return Result;
1165 }
Andrew Lenharth2c594352005-01-29 15:42:07 +00001166
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001167 case ISD::UINT_TO_FP:
Andrew Lenharth69520ed2005-05-26 18:18:34 +00001168 {
1169 //FIXME: First test if we will have problems with the sign bit before doing the slow thing
1170 assert (N.getOperand(0).getValueType() == MVT::i64
1171 && "only quads can be loaded from");
1172 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1173 Tmp2 = MakeReg(MVT::i64);
1174 BuildMI(BB, Alpha::SRL, 2, Tmp2).addReg(Tmp1).addImm(1);
1175 Tmp3 = MakeReg(MVT::i64);
1176 BuildMI(BB, Alpha::CMPLT, 2, Tmp3).addReg(Tmp1).addReg(Alpha::R31);
1177 unsigned Tmp4 = MakeReg(MVT::f64), Tmp5 = MakeReg(MVT::f64), Tmp6 = MakeReg(MVT::f64);
1178 MoveInt2FP(Tmp1, Tmp4, true);
1179 MoveInt2FP(Tmp2, Tmp5, true);
1180 MoveInt2FP(Tmp3, Tmp6, true);
1181 Tmp1 = MakeReg(MVT::f64);
1182 Tmp2 = MakeReg(MVT::f64);
1183 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
1184 BuildMI(BB, Opc, 1, Tmp1).addReg(Tmp4);
1185 BuildMI(BB, Opc, 1, Tmp2).addReg(Tmp5);
1186 Tmp3 = MakeReg(MVT::f64);
1187 BuildMI(BB, Alpha::ADDT, 2, Tmp3).addReg(Tmp2).addReg(Tmp2);
1188 //Ok, now tmp1 had the plain covereted
1189 //tmp3 has the reduced converted and added
1190 //tmp6 has the conditional to use
1191 BuildMI(BB, Alpha::FCMOVNE, 3, Result).addReg(Tmp1).addReg(Tmp3).addReg(Tmp6);
1192 return Result;
1193 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001194 case ISD::SINT_TO_FP:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001195 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001196 assert (N.getOperand(0).getValueType() == MVT::i64
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001197 && "only quads can be loaded from");
Andrew Lenharth40831c52005-01-28 06:57:18 +00001198 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001199 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001200 MoveInt2FP(Tmp1, Tmp2, true);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001201 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
1202 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001203 return Result;
1204 }
1205 }
1206 assert(0 && "should not get here");
1207 return 0;
1208}
1209
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001210unsigned ISel::SelectExpr(SDOperand N) {
1211 unsigned Result;
Andrew Lenharth2966e842005-04-07 18:15:28 +00001212 unsigned Tmp1, Tmp2 = 0, Tmp3;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001213 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001214 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001215
1216 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001217 MVT::ValueType DestType = N.getValueType();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001218
1219 unsigned &Reg = ExprMap[N];
1220 if (Reg) return Reg;
1221
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001222 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::TAILCALL)
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001223 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001224 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001225 else {
1226 // If this is a call instruction, make sure to prepare ALL of the result
1227 // values as well as the chain.
1228 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001229 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001230 else {
1231 Result = MakeReg(Node->getValueType(0));
1232 ExprMap[N.getValue(0)] = Result;
1233 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1234 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001235 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001236 }
1237 }
1238
Andrew Lenharth50d91d72005-04-30 14:19:13 +00001239 if ((DestType == MVT::f64 || DestType == MVT::f32 ||
1240 (
1241 (opcode == ISD::LOAD || opcode == ISD::CopyFromReg ||
1242 opcode == ISD::EXTLOAD) &&
1243 (N.getValue(0).getValueType() == MVT::f32 ||
1244 N.getValue(0).getValueType() == MVT::f64)
1245 ))
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001246 && opcode != ISD::CALL && opcode != ISD::TAILCALL
Andrew Lenharth06342c32005-02-07 06:21:37 +00001247 )
Andrew Lenharth40831c52005-01-28 06:57:18 +00001248 return SelectExprFP(N, Result);
1249
1250 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001251 default:
1252 Node->dump();
1253 assert(0 && "Node not handled!\n");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001254
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001255 case ISD::CTPOP:
1256 case ISD::CTTZ:
1257 case ISD::CTLZ:
1258 Opc = opcode == ISD::CTPOP ? Alpha::CTPOP :
1259 (opcode == ISD::CTTZ ? Alpha::CTTZ : Alpha::CTLZ);
1260 Tmp1 = SelectExpr(N.getOperand(0));
1261 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1262 return Result;
1263
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001264 case ISD::MULHU:
1265 Tmp1 = SelectExpr(N.getOperand(0));
1266 Tmp2 = SelectExpr(N.getOperand(1));
1267 BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth706be912005-04-07 13:55:53 +00001268 return Result;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001269 case ISD::MULHS:
1270 {
1271 //MULHU - Ra<63>*Rb - Rb<63>*Ra
1272 Tmp1 = SelectExpr(N.getOperand(0));
1273 Tmp2 = SelectExpr(N.getOperand(1));
1274 Tmp3 = MakeReg(MVT::i64);
1275 BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1276 unsigned V1 = MakeReg(MVT::i64);
1277 unsigned V2 = MakeReg(MVT::i64);
1278 BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31).addReg(Tmp1);
1279 BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31).addReg(Tmp2);
1280 unsigned IRes = MakeReg(MVT::i64);
1281 BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
1282 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
1283 return Result;
1284 }
Andrew Lenharth7332f3e2005-04-02 19:11:07 +00001285 case ISD::UNDEF: {
1286 BuildMI(BB, Alpha::IDEF, 0, Result);
1287 return Result;
1288 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001289
Andrew Lenharth032f2352005-02-22 21:59:48 +00001290 case ISD::DYNAMIC_STACKALLOC:
1291 // Generate both result values.
Andrew Lenharth3a7118d2005-02-23 17:33:42 +00001292 if (Result != notIn)
1293 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth032f2352005-02-22 21:59:48 +00001294 else
1295 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1296
1297 // FIXME: We are currently ignoring the requested alignment for handling
1298 // greater than the stack alignment. This will need to be revisited at some
1299 // point. Align = N.getOperand(2);
1300
1301 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1302 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1303 std::cerr << "Cannot allocate stack object with greater alignment than"
1304 << " the stack alignment yet!";
1305 abort();
1306 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001307
Andrew Lenharth032f2352005-02-22 21:59:48 +00001308 Select(N.getOperand(0));
1309 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1310 {
1311 if (CN->getValue() < 32000)
1312 {
1313 BuildMI(BB, Alpha::LDA, 2, Alpha::R30)
1314 .addImm(-CN->getValue()).addReg(Alpha::R30);
1315 } else {
1316 Tmp1 = SelectExpr(N.getOperand(1));
1317 // Subtract size from stack pointer, thereby allocating some space.
1318 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1319 }
1320 } else {
1321 Tmp1 = SelectExpr(N.getOperand(1));
1322 // Subtract size from stack pointer, thereby allocating some space.
1323 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1324 }
1325
1326 // Put a pointer to the space into the result register, by copying the stack
1327 // pointer.
Andrew Lenharth7bc47022005-02-22 23:29:25 +00001328 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
Andrew Lenharth032f2352005-02-22 21:59:48 +00001329 return Result;
1330
Andrew Lenharth33819132005-03-04 20:09:23 +00001331// case ISD::ConstantPool:
1332// Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1333// AlphaLowering.restoreGP(BB);
1334// BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(Tmp1);
1335// return Result;
Andrew Lenharth2c594352005-01-29 15:42:07 +00001336
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001337 case ISD::FrameIndex:
Andrew Lenharth032f2352005-02-22 21:59:48 +00001338 BuildMI(BB, Alpha::LDA, 2, Result)
1339 .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
1340 .addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001341 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001342
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001343 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001344 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001345 case ISD::SEXTLOAD:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001346 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001347 {
1348 // Make sure we generate both values.
1349 if (Result != notIn)
1350 ExprMap[N.getValue(1)] = notIn; // Generate the token
1351 else
1352 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001353
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001354 SDOperand Chain = N.getOperand(0);
1355 SDOperand Address = N.getOperand(1);
1356 Select(Chain);
1357
Misha Brukman4633f1c2005-04-21 23:13:11 +00001358 assert(Node->getValueType(0) == MVT::i64 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001359 "Unknown type to sign extend to.");
Andrew Lenharth03824012005-02-07 05:55:55 +00001360 if (opcode == ISD::LOAD)
1361 Opc = Alpha::LDQ;
1362 else
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001363 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
1364 default: Node->dump(); assert(0 && "Bad sign extend!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001365 case MVT::i32: Opc = Alpha::LDL;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001366 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001367 case MVT::i16: Opc = Alpha::LDWU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001368 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001369 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Misha Brukman4633f1c2005-04-21 23:13:11 +00001370 case MVT::i8: Opc = Alpha::LDBU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001371 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001372 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001373
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001374 if (Address.getOpcode() == ISD::GlobalAddress) {
1375 AlphaLowering.restoreGP(BB);
1376 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001377 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001378 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1379 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001380 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1381 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001382 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001383 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001384 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001385 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001386 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001387 BuildMI(BB, Opc, 2, Result)
1388 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1389 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001390 } else {
1391 long offset;
1392 SelectAddr(Address, Tmp1, offset);
1393 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1394 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001395 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001396 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001397
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001398 case ISD::GlobalAddress:
1399 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001400 has_sym = true;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001401 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
1402 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
1403 return Result;
1404
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001405 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001406 case ISD::CALL:
1407 {
1408 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001409
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001410 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001411 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001412
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001413 //grab the arguments
1414 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001415 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001416 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001417 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001418
Andrew Lenharth684f2292005-01-30 00:35:27 +00001419 //in reg args
1420 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001421 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001422 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001423 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +00001424 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001425 Alpha::F19, Alpha::F20, Alpha::F21};
1426 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001427 default:
1428 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001429 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001430 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001431 N.getOperand(i+2).getValueType() << "\n";
1432 assert(0 && "Unknown value type for call");
1433 case MVT::i1:
1434 case MVT::i8:
1435 case MVT::i16:
1436 case MVT::i32:
1437 case MVT::i64:
1438 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1439 break;
1440 case MVT::f32:
1441 case MVT::f64:
1442 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1443 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001444 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001445 }
Andrew Lenharth684f2292005-01-30 00:35:27 +00001446 //in mem args
1447 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001448 {
1449 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001450 default:
1451 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001452 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001453 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001454 N.getOperand(i+2).getValueType() << "\n";
1455 assert(0 && "Unknown value type for call");
1456 case MVT::i1:
1457 case MVT::i8:
1458 case MVT::i16:
1459 case MVT::i32:
1460 case MVT::i64:
1461 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1462 break;
1463 case MVT::f32:
1464 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1465 break;
1466 case MVT::f64:
1467 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1468 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001469 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001470 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001471 //build the right kind of call
1472 if (GlobalAddressSDNode *GASD =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001473 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001474 {
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001475 if (GASD->getGlobal()->isExternal()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001476 //use safe calling convention
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001477 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001478 has_sym = true;
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001479 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal());
1480 } else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001481 //use PC relative branch call
Andrew Lenharth1e0d9bd2005-04-14 17:34:20 +00001482 AlphaLowering.restoreGP(BB);
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001483 BuildMI(BB, Alpha::BSR, 1, Alpha::R26).addGlobalAddress(GASD->getGlobal(),true);
1484 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001485 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001486 else if (ExternalSymbolSDNode *ESSDN =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001487 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001488 {
1489 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001490 has_sym = true;
Andrew Lenharthba05ad62005-03-30 18:22:52 +00001491 BuildMI(BB, Alpha::CALL, 1).addExternalSymbol(ESSDN->getSymbol(), true);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001492 } else {
1493 //no need to restore GP as we are doing an indirect call
1494 Tmp1 = SelectExpr(N.getOperand(1));
1495 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
1496 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
1497 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001498
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001499 //push the result into a virtual register
Misha Brukman4633f1c2005-04-21 23:13:11 +00001500
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001501 switch (Node->getValueType(0)) {
1502 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001503 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001504 case MVT::i1:
1505 case MVT::i8:
1506 case MVT::i16:
1507 case MVT::i32:
1508 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001509 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
1510 break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001511 case MVT::f32:
1512 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001513 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
1514 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001515 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001516 return Result+N.ResNo;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001517 }
1518
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001519 case ISD::SIGN_EXTEND_INREG:
1520 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001521 //do SDIV opt for all levels of ints if not dividing by a constant
1522 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
1523 && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001524 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001525 unsigned Tmp4 = MakeReg(MVT::f64);
1526 unsigned Tmp5 = MakeReg(MVT::f64);
1527 unsigned Tmp6 = MakeReg(MVT::f64);
1528 unsigned Tmp7 = MakeReg(MVT::f64);
1529 unsigned Tmp8 = MakeReg(MVT::f64);
1530 unsigned Tmp9 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001531
1532 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1533 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1534 MoveInt2FP(Tmp1, Tmp4, true);
1535 MoveInt2FP(Tmp2, Tmp5, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001536 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Tmp4);
1537 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Tmp5);
1538 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
1539 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Tmp8);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001540 MoveFP2Int(Tmp9, Result, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001541 return Result;
1542 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001543
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001544 //Alpha has instructions for a bunch of signed 32 bit stuff
1545 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001546 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001547 switch (N.getOperand(0).getOpcode()) {
1548 case ISD::ADD:
1549 case ISD::SUB:
1550 case ISD::MUL:
1551 {
1552 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
1553 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
1554 //FIXME: first check for Scaled Adds and Subs!
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001555 ConstantSDNode* CSD = NULL;
1556 if(!isMul && N.getOperand(0).getOperand(0).getOpcode() == ISD::SHL &&
1557 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(0).getOperand(1))) &&
1558 (CSD->getValue() == 2 || CSD->getValue() == 3))
1559 {
1560 bool use4 = CSD->getValue() == 2;
1561 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1562 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1563 BuildMI(BB, isAdd?(use4?Alpha::S4ADDL:Alpha::S8ADDL):(use4?Alpha::S4SUBL:Alpha::S8SUBL),
1564 2,Result).addReg(Tmp1).addReg(Tmp2);
1565 }
1566 else if(isAdd && N.getOperand(0).getOperand(1).getOpcode() == ISD::SHL &&
1567 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1).getOperand(1))) &&
1568 (CSD->getValue() == 2 || CSD->getValue() == 3))
1569 {
1570 bool use4 = CSD->getValue() == 2;
1571 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1572 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1573 BuildMI(BB, use4?Alpha::S4ADDL:Alpha::S8ADDL, 2,Result).addReg(Tmp1).addReg(Tmp2);
1574 }
1575 else if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001576 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
1577 { //Normal imm add/sub
1578 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001579 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001580 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
1581 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001582 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001583 else
1584 { //Normal add/sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001585 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULL : Alpha::SUBL);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001586 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001587 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001588 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1589 }
1590 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001591 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001592 default: break; //Fall Though;
1593 }
1594 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001595 Tmp1 = SelectExpr(N.getOperand(0));
1596 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001597 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001598 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001599 {
1600 default:
1601 Node->dump();
1602 assert(0 && "Sign Extend InReg not there yet");
1603 break;
1604 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001605 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001606 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001607 break;
1608 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001609 case MVT::i16:
1610 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
1611 break;
1612 case MVT::i8:
1613 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
1614 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +00001615 case MVT::i1:
1616 Tmp2 = MakeReg(MVT::i64);
1617 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001618 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +00001619 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001620 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001621 return Result;
1622 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001623
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001624 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001625 {
1626 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1627 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1628 bool isConst1 = false;
1629 bool isConst2 = false;
1630 int dir;
Misha Brukman7847fca2005-04-22 17:54:37 +00001631
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001632 //Tmp1 = SelectExpr(N.getOperand(0));
1633 if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001634 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
1635 isConst1 = true;
1636 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001637 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1638 isConst2 = true;
1639
1640 switch (SetCC->getCondition()) {
1641 default: Node->dump(); assert(0 && "Unknown integer comparison!");
1642 case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001643 case ISD::SETLT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001644 Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001645 case ISD::SETLE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001646 Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001647 case ISD::SETGT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001648 Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001649 case ISD::SETGE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001650 Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001651 case ISD::SETULT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001652 Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001653 case ISD::SETUGT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001654 Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001655 case ISD::SETULE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001656 Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001657 case ISD::SETUGE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001658 Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001659 case ISD::SETNE: {//Handle this one special
1660 //std::cerr << "Alpha does not have a setne.\n";
1661 //abort();
1662 Tmp1 = SelectExpr(N.getOperand(0));
1663 Tmp2 = SelectExpr(N.getOperand(1));
1664 Tmp3 = MakeReg(MVT::i64);
1665 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001666 //Remeber we have the Inv for this CC
1667 CCInvMap[N] = Tmp3;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001668 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001669 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001670 return Result;
1671 }
1672 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001673 if (dir == 1) {
1674 Tmp1 = SelectExpr(N.getOperand(0));
1675 if (isConst2) {
1676 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1677 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1678 } else {
1679 Tmp2 = SelectExpr(N.getOperand(1));
1680 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1681 }
1682 } else if (dir == 2) {
1683 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001684 if (isConst1) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001685 Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1686 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1687 } else {
1688 Tmp2 = SelectExpr(N.getOperand(0));
1689 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1690 }
1691 } else { //dir == 0
1692 if (isConst1) {
1693 Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1694 Tmp2 = SelectExpr(N.getOperand(1));
1695 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
1696 } else if (isConst2) {
1697 Tmp1 = SelectExpr(N.getOperand(0));
1698 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1699 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
1700 } else {
1701 Tmp1 = SelectExpr(N.getOperand(0));
1702 Tmp2 = SelectExpr(N.getOperand(1));
1703 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1704 }
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001705 }
1706 } else {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001707 //do the comparison
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001708 Tmp1 = MakeReg(MVT::f64);
1709 bool inv = SelectFPSetCC(N, Tmp1);
1710
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001711 //now arrange for Result (int) to have a 1 or 0
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001712 Tmp2 = MakeReg(MVT::i64);
1713 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001714 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001715 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001716 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001717 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001718 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001719 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001720
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001721 case ISD::CopyFromReg:
1722 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001723 ++count_ins;
1724
Andrew Lenharth40831c52005-01-28 06:57:18 +00001725 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001726 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001727 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001728 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001729 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001730
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001731 SDOperand Chain = N.getOperand(0);
1732
1733 Select(Chain);
1734 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1735 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1736 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1737 return Result;
1738 }
1739
Misha Brukman4633f1c2005-04-21 23:13:11 +00001740 //Most of the plain arithmetic and logic share the same form, and the same
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001741 //constant immediate test
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001742 case ISD::XOR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001743 //Match Not
1744 if (N.getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001745 cast<ConstantSDNode>(N.getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001746 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001747 Tmp1 = SelectExpr(N.getOperand(0));
1748 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1749 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001750 }
1751 //Fall through
1752 case ISD::AND:
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001753 //handle zap
1754 if (opcode == ISD::AND && N.getOperand(1).getOpcode() == ISD::Constant)
1755 {
1756 uint64_t k = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1757 unsigned int build = 0;
1758 for(int i = 0; i < 8; ++i)
1759 {
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001760 if ((k & 0x00FF) == 0x00FF)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001761 build |= 1 << i;
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001762 else if ((k & 0x00FF) != 0)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001763 { build = 0; break; }
1764 k >>= 8;
1765 }
1766 if (build)
1767 {
1768 Tmp1 = SelectExpr(N.getOperand(0));
1769 BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1770 return Result;
1771 }
1772 }
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001773 case ISD::OR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001774 //Check operand(0) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001775 if (N.getOperand(0).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001776 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001777 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001778 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001779 switch(opcode) {
1780 case ISD::AND: Opc = Alpha::BIC; break;
1781 case ISD::OR: Opc = Alpha::ORNOT; break;
1782 case ISD::XOR: Opc = Alpha::EQV; break;
1783 }
1784 Tmp1 = SelectExpr(N.getOperand(1));
1785 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1786 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1787 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001788 }
1789 //Check operand(1) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001790 if (N.getOperand(1).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001791 N.getOperand(1).getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001792 cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001793 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001794 switch(opcode) {
1795 case ISD::AND: Opc = Alpha::BIC; break;
1796 case ISD::OR: Opc = Alpha::ORNOT; break;
1797 case ISD::XOR: Opc = Alpha::EQV; break;
1798 }
1799 Tmp1 = SelectExpr(N.getOperand(0));
1800 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1801 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1802 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001803 }
1804 //Fall through
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001805 case ISD::SHL:
1806 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001807 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001808 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001809 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1810 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001811 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001812 {
1813 switch(opcode) {
1814 case ISD::AND: Opc = Alpha::ANDi; break;
1815 case ISD::OR: Opc = Alpha::BISi; break;
1816 case ISD::XOR: Opc = Alpha::XORi; break;
1817 case ISD::SHL: Opc = Alpha::SLi; break;
1818 case ISD::SRL: Opc = Alpha::SRLi; break;
1819 case ISD::SRA: Opc = Alpha::SRAi; break;
1820 case ISD::MUL: Opc = Alpha::MULQi; break;
1821 };
1822 Tmp1 = SelectExpr(N.getOperand(0));
1823 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1824 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1825 } else {
1826 switch(opcode) {
1827 case ISD::AND: Opc = Alpha::AND; break;
1828 case ISD::OR: Opc = Alpha::BIS; break;
1829 case ISD::XOR: Opc = Alpha::XOR; break;
1830 case ISD::SHL: Opc = Alpha::SL; break;
1831 case ISD::SRL: Opc = Alpha::SRL; break;
1832 case ISD::SRA: Opc = Alpha::SRA; break;
1833 case ISD::MUL: Opc = Alpha::MULQ; break;
1834 };
1835 Tmp1 = SelectExpr(N.getOperand(0));
1836 Tmp2 = SelectExpr(N.getOperand(1));
1837 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1838 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001839 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001840
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001841 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001842 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001843 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001844 bool isAdd = opcode == ISD::ADD;
1845
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001846 //first check for Scaled Adds and Subs!
1847 //Valid for add and sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001848 ConstantSDNode* CSD = NULL;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001849 if(N.getOperand(0).getOpcode() == ISD::SHL &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001850 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
1851 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001852 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001853 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001854 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001855 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) && CSD->getValue() <= 255)
1856 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1857 2, Result).addReg(Tmp2).addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001858 else {
1859 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001860 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1861 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001862 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001863 }
1864 //Position prevents subs
Andrew Lenharth273a1f92005-04-07 14:18:13 +00001865 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001866 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) &&
1867 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001868 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001869 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001870 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001871 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(0))) && CSD->getValue() <= 255)
1872 BuildMI(BB, use4?Alpha::S4ADDQi:Alpha::S8ADDQi, 2, Result).addReg(Tmp2)
1873 .addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001874 else {
1875 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001876 BuildMI(BB, use4?Alpha::S4ADDQ:Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001877 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001878 }
1879 //small addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001880 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
1881 CSD->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001882 { //Normal imm add/sub
1883 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1884 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001885 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CSD->getValue());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001886 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001887 //larger addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001888 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
1889 CSD->getSignExtended() <= 32767 &&
1890 CSD->getSignExtended() >= -32767)
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001891 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001892 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001893 Tmp2 = (long)CSD->getSignExtended();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001894 if (!isAdd)
1895 Tmp2 = -Tmp2;
1896 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001897 }
1898 //give up and do the operation
1899 else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001900 //Normal add/sub
1901 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1902 Tmp1 = SelectExpr(N.getOperand(0));
1903 Tmp2 = SelectExpr(N.getOperand(1));
1904 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1905 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001906 return Result;
1907 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001908
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001909 case ISD::SDIV:
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001910 {
Andrew Lenhartha565c272005-04-06 22:03:13 +00001911 ConstantSDNode* CSD;
1912 //check if we can convert into a shift!
1913 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
1914 (int64_t)CSD->getSignExtended() != 0 &&
1915 ExactLog2(abs((int64_t)CSD->getSignExtended())) != 0)
1916 {
1917 unsigned k = ExactLog2(abs(CSD->getSignExtended()));
1918 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenhartha565c272005-04-06 22:03:13 +00001919 if (k == 1)
1920 Tmp2 = Tmp1;
1921 else
1922 {
1923 Tmp2 = MakeReg(MVT::i64);
1924 BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
1925 }
1926 Tmp3 = MakeReg(MVT::i64);
1927 BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
1928 unsigned Tmp4 = MakeReg(MVT::i64);
1929 BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
1930 if ((int64_t)CSD->getSignExtended() > 0)
1931 BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
1932 else
1933 {
1934 unsigned Tmp5 = MakeReg(MVT::i64);
1935 BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
1936 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
1937 }
1938 return Result;
1939 }
1940 }
1941 //Else fall through
1942
1943 case ISD::UDIV:
1944 {
1945 ConstantSDNode* CSD;
1946 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
1947 ((int64_t)CSD->getSignExtended() >= 2 ||
1948 (int64_t)CSD->getSignExtended() <= -2))
1949 {
1950 // If this is a divide by constant, we can emit code using some magic
1951 // constants to implement it as a multiply instead.
1952 ExprMap.erase(N);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001953 if (opcode == ISD::SDIV)
Andrew Lenhartha565c272005-04-06 22:03:13 +00001954 return SelectExpr(BuildSDIVSequence(N));
1955 else
1956 return SelectExpr(BuildUDIVSequence(N));
1957 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001958 }
1959 //else fall though
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001960 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +00001961 case ISD::SREM:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001962 //FIXME: alpha really doesn't support any of these operations,
Andrew Lenharth40831c52005-01-28 06:57:18 +00001963 // the ops are expanded into special library calls with
1964 // special calling conventions
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001965 //Restore GP because it is a call after all...
Andrew Lenharth40831c52005-01-28 06:57:18 +00001966 switch(opcode) {
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001967 case ISD::UREM: Opc = Alpha::REMQU; break;
1968 case ISD::SREM: Opc = Alpha::REMQ; break;
1969 case ISD::UDIV: Opc = Alpha::DIVQU; break;
1970 case ISD::SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001971 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001972 Tmp1 = SelectExpr(N.getOperand(0));
1973 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth33819132005-03-04 20:09:23 +00001974 //set up regs explicitly (helps Reg alloc)
1975 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001976 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001977 AlphaLowering.restoreGP(BB);
Andrew Lenharth33819132005-03-04 20:09:23 +00001978 BuildMI(BB, Opc, 2).addReg(Alpha::R24).addReg(Alpha::R25);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001979 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001980 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001981
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001982 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001983 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001984 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001985 assert (DestType == MVT::i64 && "only quads can be loaded to");
1986 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00001987 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001988 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001989 if (SrcType == MVT::f32)
Misha Brukman7847fca2005-04-22 17:54:37 +00001990 {
1991 Tmp2 = MakeReg(MVT::f64);
1992 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
1993 Tmp1 = Tmp2;
1994 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001995 Tmp2 = MakeReg(MVT::f64);
1996 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001997 MoveFP2Int(Tmp2, Result, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001998
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001999 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002000 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00002001
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002002 case ISD::SELECT:
2003 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00002004 //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 +00002005 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002006 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2007 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002008 // Get the condition into the zero flag.
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002009 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002010
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002011 SDOperand CC = N.getOperand(0);
2012 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
2013
Misha Brukman4633f1c2005-04-21 23:13:11 +00002014 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002015 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
2016 { //FP Setcc -> Int Select
Misha Brukman7847fca2005-04-22 17:54:37 +00002017 Tmp1 = MakeReg(MVT::f64);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002018 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2019 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Misha Brukman7847fca2005-04-22 17:54:37 +00002020 bool inv = SelectFPSetCC(CC, Tmp1);
2021 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
2022 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
2023 return Result;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002024 }
2025 if (CC.getOpcode() == ISD::SETCC) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002026 //Int SetCC -> Select
2027 //Dropping the CC is only useful if we are comparing to 0
2028 if((SetCC->getOperand(1).getOpcode() == ISD::Constant &&
2029 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0) ||
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002030 (SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2031 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0))
2032 {
2033 //figure out a few things
2034 bool LeftZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2035 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
2036 bool RightZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2037 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
2038 bool LeftConst = N.getOperand(1).getOpcode() == ISD::Constant &&
2039 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255;
2040 bool RightConst = N.getOperand(2).getOpcode() == ISD::Constant &&
2041 cast<ConstantSDNode>(N.getOperand(2))->getValue() <= 255;
2042 bool useImm = LeftConst || RightConst;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002043
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002044 //Fix up CC
2045 ISD::CondCode cCode= SetCC->getCondition();
2046 if (RightConst && !LeftConst) //Invert sense to get Imm field right
2047 cCode = ISD::getSetCCInverse(cCode, true);
2048 if (LeftZero && !RightZero) //Swap Operands
2049 cCode = ISD::getSetCCSwappedOperands(cCode);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002050
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002051 //Choose the CMOV
2052 switch (cCode) {
2053 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
2054 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
2055 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
2056 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
2057 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
2058 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
2059 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
2060 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2061 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break; //Technically you could have this CC
2062 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
2063 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2064 }
2065 if(LeftZero && !RightZero) //swap Operands
2066 Tmp1 = SelectExpr(SetCC->getOperand(1)); //Cond
2067 else
2068 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
2069
2070 if (LeftConst) {
2071 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2072 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
Misha Brukman7847fca2005-04-22 17:54:37 +00002073 .addImm(cast<ConstantSDNode>(N.getOperand(1))->getValue())
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002074 .addReg(Tmp1);
2075 } else if (RightConst) {
2076 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
2077 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
Misha Brukman7847fca2005-04-22 17:54:37 +00002078 .addImm(cast<ConstantSDNode>(N.getOperand(2))->getValue())
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002079 .addReg(Tmp1);
2080 } else {
2081 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2082 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2083 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
2084 }
2085 return Result;
2086 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002087 //Otherwise, fall though
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002088 }
2089 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002090 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2091 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthe76797c2005-02-01 20:40:27 +00002092 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002093
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002094 return Result;
2095 }
2096
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002097 case ISD::Constant:
2098 {
Andrew Lenharthc0513832005-03-29 19:24:04 +00002099 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002100 if (val <= IMM_HIGH && val >= IMM_LOW) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002101 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002102 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002103 else if (val <= (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT &&
2104 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
2105 Tmp1 = MakeReg(MVT::i64);
2106 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val)).addReg(Alpha::R31);
2107 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002108 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002109 else {
2110 MachineConstantPool *CP = BB->getParent()->getConstantPool();
2111 ConstantUInt *C = ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
2112 unsigned CPI = CP->getConstantPoolIndex(C);
2113 AlphaLowering.restoreGP(BB);
2114 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CPI);
2115 }
2116 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002117 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002118 }
2119
2120 return 0;
2121}
2122
2123void ISel::Select(SDOperand N) {
2124 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00002125 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002126
Nate Begeman85fdeb22005-03-24 04:39:54 +00002127 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002128 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002129
2130 SDNode *Node = N.Val;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002131
Andrew Lenharth760270d2005-02-07 23:02:23 +00002132 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002133
2134 default:
2135 Node->dump(); std::cerr << "\n";
2136 assert(0 && "Node not handled yet!");
2137
2138 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00002139 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002140 return;
2141 }
2142
2143 case ISD::BR: {
2144 MachineBasicBlock *Dest =
2145 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2146
2147 Select(N.getOperand(0));
2148 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
2149 return;
2150 }
2151
2152 case ISD::ImplicitDef:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002153 ++count_ins;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002154 Select(N.getOperand(0));
2155 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
2156 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002157
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002158 case ISD::EntryToken: return; // Noop
2159
2160 case ISD::TokenFactor:
2161 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2162 Select(Node->getOperand(i));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002163
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002164 //N.Val->dump(); std::cerr << "\n";
2165 //assert(0 && "Node not handled yet!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00002166
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002167 return;
2168
2169 case ISD::CopyToReg:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002170 ++count_outs;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002171 Select(N.getOperand(0));
2172 Tmp1 = SelectExpr(N.getOperand(1));
2173 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002174
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002175 if (Tmp1 != Tmp2) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002176 if (N.getOperand(1).getValueType() == MVT::f64 ||
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002177 N.getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00002178 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2179 else
2180 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002181 }
2182 return;
2183
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002184 case ISD::RET:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002185 ++count_outs;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002186 switch (N.getNumOperands()) {
2187 default:
2188 std::cerr << N.getNumOperands() << "\n";
2189 for (unsigned i = 0; i < N.getNumOperands(); ++i)
2190 std::cerr << N.getOperand(i).getValueType() << "\n";
2191 Node->dump();
2192 assert(0 && "Unknown return instruction!");
2193 case 2:
2194 Select(N.getOperand(0));
2195 Tmp1 = SelectExpr(N.getOperand(1));
2196 switch (N.getOperand(1).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002197 default: Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002198 assert(0 && "All other types should have been promoted!!");
2199 case MVT::f64:
2200 case MVT::f32:
2201 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
2202 break;
2203 case MVT::i32:
2204 case MVT::i64:
2205 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
2206 break;
2207 }
2208 break;
2209 case 1:
2210 Select(N.getOperand(0));
2211 break;
2212 }
2213 //Tmp2 = AlphaLowering.getRetAddr();
2214 //BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(Tmp2).addReg(Tmp2);
2215 BuildMI(BB, Alpha::RETURN, 0); // Just emit a 'ret' instruction
2216 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002217
Misha Brukman4633f1c2005-04-21 23:13:11 +00002218 case ISD::TRUNCSTORE:
2219 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002220 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002221 SDOperand Chain = N.getOperand(0);
2222 SDOperand Value = N.getOperand(1);
2223 SDOperand Address = N.getOperand(2);
2224 Select(Chain);
2225
2226 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00002227
2228 if (opcode == ISD::STORE) {
2229 switch(Value.getValueType()) {
2230 default: assert(0 && "unknown Type in store");
2231 case MVT::i64: Opc = Alpha::STQ; break;
2232 case MVT::f64: Opc = Alpha::STT; break;
2233 case MVT::f32: Opc = Alpha::STS; break;
2234 }
2235 } else { //ISD::TRUNCSTORE
2236 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2237 default: assert(0 && "unknown Type in store");
2238 case MVT::i1: //FIXME: DAG does not promote this load
2239 case MVT::i8: Opc = Alpha::STB; break;
2240 case MVT::i16: Opc = Alpha::STW; break;
2241 case MVT::i32: Opc = Alpha::STL; break;
2242 }
Andrew Lenharth65838902005-02-06 16:22:15 +00002243 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00002244
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002245 if (Address.getOpcode() == ISD::GlobalAddress)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002246 {
2247 AlphaLowering.restoreGP(BB);
2248 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002249 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002250 BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
2251 }
Andrew Lenharth05380342005-02-07 05:07:00 +00002252 else if(Address.getOpcode() == ISD::FrameIndex)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002253 {
Andrew Lenharth032f2352005-02-22 21:59:48 +00002254 BuildMI(BB, Opc, 3).addReg(Tmp1)
2255 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
2256 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002257 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002258 else
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002259 {
2260 long offset;
2261 SelectAddr(Address, Tmp2, offset);
2262 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2263 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002264 return;
2265 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002266
2267 case ISD::EXTLOAD:
2268 case ISD::SEXTLOAD:
2269 case ISD::ZEXTLOAD:
2270 case ISD::LOAD:
2271 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002272 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002273 case ISD::CALL:
Andrew Lenharth032f2352005-02-22 21:59:48 +00002274 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002275 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002276 SelectExpr(N);
2277 return;
2278
Chris Lattner16cd04d2005-05-12 23:24:06 +00002279 case ISD::CALLSEQ_START:
2280 case ISD::CALLSEQ_END:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002281 Select(N.getOperand(0));
2282 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002283
Chris Lattner16cd04d2005-05-12 23:24:06 +00002284 Opc = N.getOpcode() == ISD::CALLSEQ_START ? Alpha::ADJUSTSTACKDOWN :
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002285 Alpha::ADJUSTSTACKUP;
2286 BuildMI(BB, Opc, 1).addImm(Tmp1);
2287 return;
Andrew Lenharth95762122005-03-31 21:24:06 +00002288
2289 case ISD::PCMARKER:
2290 Select(N.getOperand(0)); //Chain
2291 BuildMI(BB, Alpha::PCLABEL, 2).addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
2292 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002293 }
2294 assert(0 && "Should not be reached!");
2295}
2296
2297
2298/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
2299/// into a machine code representation using pattern matching and a machine
2300/// description file.
2301///
2302FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002303 return new ISel(TM);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002304}
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002305