blob: 28930cefb4373e78d395b325abe3ac7eaa1b04e2 [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:
1168 case ISD::SINT_TO_FP:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001169 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001170 assert (N.getOperand(0).getValueType() == MVT::i64
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001171 && "only quads can be loaded from");
Andrew Lenharth40831c52005-01-28 06:57:18 +00001172 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001173 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001174 MoveInt2FP(Tmp1, Tmp2, true);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001175 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
1176 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001177 return Result;
1178 }
1179 }
1180 assert(0 && "should not get here");
1181 return 0;
1182}
1183
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001184unsigned ISel::SelectExpr(SDOperand N) {
1185 unsigned Result;
Andrew Lenharth2966e842005-04-07 18:15:28 +00001186 unsigned Tmp1, Tmp2 = 0, Tmp3;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001187 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001188 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001189
1190 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001191 MVT::ValueType DestType = N.getValueType();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001192
1193 unsigned &Reg = ExprMap[N];
1194 if (Reg) return Reg;
1195
1196 if (N.getOpcode() != ISD::CALL)
1197 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001198 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001199 else {
1200 // If this is a call instruction, make sure to prepare ALL of the result
1201 // values as well as the chain.
1202 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001203 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001204 else {
1205 Result = MakeReg(Node->getValueType(0));
1206 ExprMap[N.getValue(0)] = Result;
1207 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1208 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001209 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001210 }
1211 }
1212
Andrew Lenharth50d91d72005-04-30 14:19:13 +00001213 if ((DestType == MVT::f64 || DestType == MVT::f32 ||
1214 (
1215 (opcode == ISD::LOAD || opcode == ISD::CopyFromReg ||
1216 opcode == ISD::EXTLOAD) &&
1217 (N.getValue(0).getValueType() == MVT::f32 ||
1218 N.getValue(0).getValueType() == MVT::f64)
1219 ))
1220 && opcode != ISD::CALL
Andrew Lenharth06342c32005-02-07 06:21:37 +00001221 )
Andrew Lenharth40831c52005-01-28 06:57:18 +00001222 return SelectExprFP(N, Result);
1223
1224 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001225 default:
1226 Node->dump();
1227 assert(0 && "Node not handled!\n");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001228
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001229 case ISD::CTPOP:
1230 case ISD::CTTZ:
1231 case ISD::CTLZ:
1232 Opc = opcode == ISD::CTPOP ? Alpha::CTPOP :
1233 (opcode == ISD::CTTZ ? Alpha::CTTZ : Alpha::CTLZ);
1234 Tmp1 = SelectExpr(N.getOperand(0));
1235 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1236 return Result;
1237
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001238 case ISD::MULHU:
1239 Tmp1 = SelectExpr(N.getOperand(0));
1240 Tmp2 = SelectExpr(N.getOperand(1));
1241 BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth706be912005-04-07 13:55:53 +00001242 return Result;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001243 case ISD::MULHS:
1244 {
1245 //MULHU - Ra<63>*Rb - Rb<63>*Ra
1246 Tmp1 = SelectExpr(N.getOperand(0));
1247 Tmp2 = SelectExpr(N.getOperand(1));
1248 Tmp3 = MakeReg(MVT::i64);
1249 BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1250 unsigned V1 = MakeReg(MVT::i64);
1251 unsigned V2 = MakeReg(MVT::i64);
1252 BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31).addReg(Tmp1);
1253 BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31).addReg(Tmp2);
1254 unsigned IRes = MakeReg(MVT::i64);
1255 BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
1256 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
1257 return Result;
1258 }
Andrew Lenharth7332f3e2005-04-02 19:11:07 +00001259 case ISD::UNDEF: {
1260 BuildMI(BB, Alpha::IDEF, 0, Result);
1261 return Result;
1262 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001263
Andrew Lenharth032f2352005-02-22 21:59:48 +00001264 case ISD::DYNAMIC_STACKALLOC:
1265 // Generate both result values.
Andrew Lenharth3a7118d2005-02-23 17:33:42 +00001266 if (Result != notIn)
1267 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth032f2352005-02-22 21:59:48 +00001268 else
1269 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1270
1271 // FIXME: We are currently ignoring the requested alignment for handling
1272 // greater than the stack alignment. This will need to be revisited at some
1273 // point. Align = N.getOperand(2);
1274
1275 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1276 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1277 std::cerr << "Cannot allocate stack object with greater alignment than"
1278 << " the stack alignment yet!";
1279 abort();
1280 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001281
Andrew Lenharth032f2352005-02-22 21:59:48 +00001282 Select(N.getOperand(0));
1283 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1284 {
1285 if (CN->getValue() < 32000)
1286 {
1287 BuildMI(BB, Alpha::LDA, 2, Alpha::R30)
1288 .addImm(-CN->getValue()).addReg(Alpha::R30);
1289 } else {
1290 Tmp1 = SelectExpr(N.getOperand(1));
1291 // Subtract size from stack pointer, thereby allocating some space.
1292 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1293 }
1294 } else {
1295 Tmp1 = SelectExpr(N.getOperand(1));
1296 // Subtract size from stack pointer, thereby allocating some space.
1297 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1298 }
1299
1300 // Put a pointer to the space into the result register, by copying the stack
1301 // pointer.
Andrew Lenharth7bc47022005-02-22 23:29:25 +00001302 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
Andrew Lenharth032f2352005-02-22 21:59:48 +00001303 return Result;
1304
Andrew Lenharth33819132005-03-04 20:09:23 +00001305// case ISD::ConstantPool:
1306// Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1307// AlphaLowering.restoreGP(BB);
1308// BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(Tmp1);
1309// return Result;
Andrew Lenharth2c594352005-01-29 15:42:07 +00001310
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001311 case ISD::FrameIndex:
Andrew Lenharth032f2352005-02-22 21:59:48 +00001312 BuildMI(BB, Alpha::LDA, 2, Result)
1313 .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
1314 .addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001315 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001316
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001317 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001318 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001319 case ISD::SEXTLOAD:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001320 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001321 {
1322 // Make sure we generate both values.
1323 if (Result != notIn)
1324 ExprMap[N.getValue(1)] = notIn; // Generate the token
1325 else
1326 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001327
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001328 SDOperand Chain = N.getOperand(0);
1329 SDOperand Address = N.getOperand(1);
1330 Select(Chain);
1331
Misha Brukman4633f1c2005-04-21 23:13:11 +00001332 assert(Node->getValueType(0) == MVT::i64 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001333 "Unknown type to sign extend to.");
Andrew Lenharth03824012005-02-07 05:55:55 +00001334 if (opcode == ISD::LOAD)
1335 Opc = Alpha::LDQ;
1336 else
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001337 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
1338 default: Node->dump(); assert(0 && "Bad sign extend!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001339 case MVT::i32: Opc = Alpha::LDL;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001340 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001341 case MVT::i16: Opc = Alpha::LDWU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001342 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001343 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Misha Brukman4633f1c2005-04-21 23:13:11 +00001344 case MVT::i8: Opc = Alpha::LDBU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001345 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001346 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001347
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001348 if (Address.getOpcode() == ISD::GlobalAddress) {
1349 AlphaLowering.restoreGP(BB);
1350 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001351 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001352 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1353 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001354 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1355 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001356 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001357 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001358 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001359 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001360 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001361 BuildMI(BB, Opc, 2, Result)
1362 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1363 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001364 } else {
1365 long offset;
1366 SelectAddr(Address, Tmp1, offset);
1367 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1368 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001369 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001370 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001371
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001372 case ISD::GlobalAddress:
1373 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001374 has_sym = true;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001375 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
1376 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
1377 return Result;
1378
1379 case ISD::CALL:
1380 {
1381 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001382
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001383 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001384 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001385
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001386 //grab the arguments
1387 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001388 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001389 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001390 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001391
Andrew Lenharth684f2292005-01-30 00:35:27 +00001392 //in reg args
1393 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001394 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001395 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001396 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +00001397 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001398 Alpha::F19, Alpha::F20, Alpha::F21};
1399 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001400 default:
1401 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001402 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001403 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001404 N.getOperand(i+2).getValueType() << "\n";
1405 assert(0 && "Unknown value type for call");
1406 case MVT::i1:
1407 case MVT::i8:
1408 case MVT::i16:
1409 case MVT::i32:
1410 case MVT::i64:
1411 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1412 break;
1413 case MVT::f32:
1414 case MVT::f64:
1415 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1416 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001417 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001418 }
Andrew Lenharth684f2292005-01-30 00:35:27 +00001419 //in mem args
1420 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001421 {
1422 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001423 default:
1424 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001425 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001426 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001427 N.getOperand(i+2).getValueType() << "\n";
1428 assert(0 && "Unknown value type for call");
1429 case MVT::i1:
1430 case MVT::i8:
1431 case MVT::i16:
1432 case MVT::i32:
1433 case MVT::i64:
1434 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1435 break;
1436 case MVT::f32:
1437 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1438 break;
1439 case MVT::f64:
1440 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1441 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001442 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001443 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001444 //build the right kind of call
1445 if (GlobalAddressSDNode *GASD =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001446 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001447 {
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001448 if (GASD->getGlobal()->isExternal()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001449 //use safe calling convention
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001450 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001451 has_sym = true;
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001452 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal());
1453 } else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001454 //use PC relative branch call
Andrew Lenharth1e0d9bd2005-04-14 17:34:20 +00001455 AlphaLowering.restoreGP(BB);
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001456 BuildMI(BB, Alpha::BSR, 1, Alpha::R26).addGlobalAddress(GASD->getGlobal(),true);
1457 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001458 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001459 else if (ExternalSymbolSDNode *ESSDN =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001460 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001461 {
1462 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001463 has_sym = true;
Andrew Lenharthba05ad62005-03-30 18:22:52 +00001464 BuildMI(BB, Alpha::CALL, 1).addExternalSymbol(ESSDN->getSymbol(), true);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001465 } else {
1466 //no need to restore GP as we are doing an indirect call
1467 Tmp1 = SelectExpr(N.getOperand(1));
1468 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
1469 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
1470 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001471
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001472 //push the result into a virtual register
Misha Brukman4633f1c2005-04-21 23:13:11 +00001473
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001474 switch (Node->getValueType(0)) {
1475 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001476 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001477 case MVT::i1:
1478 case MVT::i8:
1479 case MVT::i16:
1480 case MVT::i32:
1481 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001482 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
1483 break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001484 case MVT::f32:
1485 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001486 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
1487 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001488 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001489 return Result+N.ResNo;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001490 }
1491
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001492 case ISD::SIGN_EXTEND_INREG:
1493 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001494 //do SDIV opt for all levels of ints if not dividing by a constant
1495 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
1496 && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001497 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001498 unsigned Tmp4 = MakeReg(MVT::f64);
1499 unsigned Tmp5 = MakeReg(MVT::f64);
1500 unsigned Tmp6 = MakeReg(MVT::f64);
1501 unsigned Tmp7 = MakeReg(MVT::f64);
1502 unsigned Tmp8 = MakeReg(MVT::f64);
1503 unsigned Tmp9 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001504
1505 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1506 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1507 MoveInt2FP(Tmp1, Tmp4, true);
1508 MoveInt2FP(Tmp2, Tmp5, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001509 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Tmp4);
1510 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Tmp5);
1511 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
1512 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Tmp8);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001513 MoveFP2Int(Tmp9, Result, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001514 return Result;
1515 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001516
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001517 //Alpha has instructions for a bunch of signed 32 bit stuff
1518 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001519 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001520 switch (N.getOperand(0).getOpcode()) {
1521 case ISD::ADD:
1522 case ISD::SUB:
1523 case ISD::MUL:
1524 {
1525 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
1526 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
1527 //FIXME: first check for Scaled Adds and Subs!
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001528 ConstantSDNode* CSD = NULL;
1529 if(!isMul && N.getOperand(0).getOperand(0).getOpcode() == ISD::SHL &&
1530 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(0).getOperand(1))) &&
1531 (CSD->getValue() == 2 || CSD->getValue() == 3))
1532 {
1533 bool use4 = CSD->getValue() == 2;
1534 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1535 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1536 BuildMI(BB, isAdd?(use4?Alpha::S4ADDL:Alpha::S8ADDL):(use4?Alpha::S4SUBL:Alpha::S8SUBL),
1537 2,Result).addReg(Tmp1).addReg(Tmp2);
1538 }
1539 else if(isAdd && N.getOperand(0).getOperand(1).getOpcode() == ISD::SHL &&
1540 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1).getOperand(1))) &&
1541 (CSD->getValue() == 2 || CSD->getValue() == 3))
1542 {
1543 bool use4 = CSD->getValue() == 2;
1544 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1545 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1546 BuildMI(BB, use4?Alpha::S4ADDL:Alpha::S8ADDL, 2,Result).addReg(Tmp1).addReg(Tmp2);
1547 }
1548 else if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001549 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
1550 { //Normal imm add/sub
1551 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001552 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001553 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
1554 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001555 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001556 else
1557 { //Normal add/sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001558 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULL : Alpha::SUBL);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001559 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001560 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001561 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1562 }
1563 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001564 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001565 default: break; //Fall Though;
1566 }
1567 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001568 Tmp1 = SelectExpr(N.getOperand(0));
1569 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001570 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001571 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001572 {
1573 default:
1574 Node->dump();
1575 assert(0 && "Sign Extend InReg not there yet");
1576 break;
1577 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001578 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001579 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001580 break;
1581 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001582 case MVT::i16:
1583 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
1584 break;
1585 case MVT::i8:
1586 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
1587 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +00001588 case MVT::i1:
1589 Tmp2 = MakeReg(MVT::i64);
1590 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001591 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +00001592 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001593 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001594 return Result;
1595 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001596
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001597 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001598 {
1599 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1600 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1601 bool isConst1 = false;
1602 bool isConst2 = false;
1603 int dir;
Misha Brukman7847fca2005-04-22 17:54:37 +00001604
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001605 //Tmp1 = SelectExpr(N.getOperand(0));
1606 if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001607 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
1608 isConst1 = true;
1609 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001610 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1611 isConst2 = true;
1612
1613 switch (SetCC->getCondition()) {
1614 default: Node->dump(); assert(0 && "Unknown integer comparison!");
1615 case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001616 case ISD::SETLT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001617 Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001618 case ISD::SETLE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001619 Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001620 case ISD::SETGT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001621 Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001622 case ISD::SETGE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001623 Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001624 case ISD::SETULT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001625 Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001626 case ISD::SETUGT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001627 Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001628 case ISD::SETULE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001629 Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001630 case ISD::SETUGE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001631 Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001632 case ISD::SETNE: {//Handle this one special
1633 //std::cerr << "Alpha does not have a setne.\n";
1634 //abort();
1635 Tmp1 = SelectExpr(N.getOperand(0));
1636 Tmp2 = SelectExpr(N.getOperand(1));
1637 Tmp3 = MakeReg(MVT::i64);
1638 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001639 //Remeber we have the Inv for this CC
1640 CCInvMap[N] = Tmp3;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001641 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001642 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001643 return Result;
1644 }
1645 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001646 if (dir == 1) {
1647 Tmp1 = SelectExpr(N.getOperand(0));
1648 if (isConst2) {
1649 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1650 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1651 } else {
1652 Tmp2 = SelectExpr(N.getOperand(1));
1653 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1654 }
1655 } else if (dir == 2) {
1656 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001657 if (isConst1) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001658 Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1659 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1660 } else {
1661 Tmp2 = SelectExpr(N.getOperand(0));
1662 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1663 }
1664 } else { //dir == 0
1665 if (isConst1) {
1666 Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1667 Tmp2 = SelectExpr(N.getOperand(1));
1668 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
1669 } else if (isConst2) {
1670 Tmp1 = SelectExpr(N.getOperand(0));
1671 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1672 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
1673 } else {
1674 Tmp1 = SelectExpr(N.getOperand(0));
1675 Tmp2 = SelectExpr(N.getOperand(1));
1676 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1677 }
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001678 }
1679 } else {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001680 //do the comparison
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001681 Tmp1 = MakeReg(MVT::f64);
1682 bool inv = SelectFPSetCC(N, Tmp1);
1683
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001684 //now arrange for Result (int) to have a 1 or 0
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001685 Tmp2 = MakeReg(MVT::i64);
1686 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001687 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001688 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001689 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001690 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001691 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001692 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001693
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001694 case ISD::CopyFromReg:
1695 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001696 ++count_ins;
1697
Andrew Lenharth40831c52005-01-28 06:57:18 +00001698 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001699 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001700 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001701 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001702 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001703
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001704 SDOperand Chain = N.getOperand(0);
1705
1706 Select(Chain);
1707 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1708 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1709 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1710 return Result;
1711 }
1712
Misha Brukman4633f1c2005-04-21 23:13:11 +00001713 //Most of the plain arithmetic and logic share the same form, and the same
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001714 //constant immediate test
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001715 case ISD::XOR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001716 //Match Not
1717 if (N.getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001718 cast<ConstantSDNode>(N.getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001719 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001720 Tmp1 = SelectExpr(N.getOperand(0));
1721 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1722 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001723 }
1724 //Fall through
1725 case ISD::AND:
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001726 //handle zap
1727 if (opcode == ISD::AND && N.getOperand(1).getOpcode() == ISD::Constant)
1728 {
1729 uint64_t k = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1730 unsigned int build = 0;
1731 for(int i = 0; i < 8; ++i)
1732 {
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001733 if ((k & 0x00FF) == 0x00FF)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001734 build |= 1 << i;
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001735 else if ((k & 0x00FF) != 0)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001736 { build = 0; break; }
1737 k >>= 8;
1738 }
1739 if (build)
1740 {
1741 Tmp1 = SelectExpr(N.getOperand(0));
1742 BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1743 return Result;
1744 }
1745 }
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001746 case ISD::OR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001747 //Check operand(0) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001748 if (N.getOperand(0).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001749 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001750 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001751 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001752 switch(opcode) {
1753 case ISD::AND: Opc = Alpha::BIC; break;
1754 case ISD::OR: Opc = Alpha::ORNOT; break;
1755 case ISD::XOR: Opc = Alpha::EQV; break;
1756 }
1757 Tmp1 = SelectExpr(N.getOperand(1));
1758 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1759 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1760 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001761 }
1762 //Check operand(1) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001763 if (N.getOperand(1).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001764 N.getOperand(1).getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001765 cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001766 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001767 switch(opcode) {
1768 case ISD::AND: Opc = Alpha::BIC; break;
1769 case ISD::OR: Opc = Alpha::ORNOT; break;
1770 case ISD::XOR: Opc = Alpha::EQV; break;
1771 }
1772 Tmp1 = SelectExpr(N.getOperand(0));
1773 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1774 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1775 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001776 }
1777 //Fall through
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001778 case ISD::SHL:
1779 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001780 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001781 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001782 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1783 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001784 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001785 {
1786 switch(opcode) {
1787 case ISD::AND: Opc = Alpha::ANDi; break;
1788 case ISD::OR: Opc = Alpha::BISi; break;
1789 case ISD::XOR: Opc = Alpha::XORi; break;
1790 case ISD::SHL: Opc = Alpha::SLi; break;
1791 case ISD::SRL: Opc = Alpha::SRLi; break;
1792 case ISD::SRA: Opc = Alpha::SRAi; break;
1793 case ISD::MUL: Opc = Alpha::MULQi; break;
1794 };
1795 Tmp1 = SelectExpr(N.getOperand(0));
1796 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1797 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1798 } else {
1799 switch(opcode) {
1800 case ISD::AND: Opc = Alpha::AND; break;
1801 case ISD::OR: Opc = Alpha::BIS; break;
1802 case ISD::XOR: Opc = Alpha::XOR; break;
1803 case ISD::SHL: Opc = Alpha::SL; break;
1804 case ISD::SRL: Opc = Alpha::SRL; break;
1805 case ISD::SRA: Opc = Alpha::SRA; break;
1806 case ISD::MUL: Opc = Alpha::MULQ; break;
1807 };
1808 Tmp1 = SelectExpr(N.getOperand(0));
1809 Tmp2 = SelectExpr(N.getOperand(1));
1810 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1811 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001812 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001813
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001814 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001815 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001816 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001817 bool isAdd = opcode == ISD::ADD;
1818
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001819 //first check for Scaled Adds and Subs!
1820 //Valid for add and sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001821 ConstantSDNode* CSD = NULL;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001822 if(N.getOperand(0).getOpcode() == ISD::SHL &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001823 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
1824 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001825 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001826 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001827 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001828 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) && CSD->getValue() <= 255)
1829 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1830 2, Result).addReg(Tmp2).addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001831 else {
1832 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001833 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1834 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001835 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001836 }
1837 //Position prevents subs
Andrew Lenharth273a1f92005-04-07 14:18:13 +00001838 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001839 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) &&
1840 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001841 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001842 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001843 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001844 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(0))) && CSD->getValue() <= 255)
1845 BuildMI(BB, use4?Alpha::S4ADDQi:Alpha::S8ADDQi, 2, Result).addReg(Tmp2)
1846 .addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001847 else {
1848 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001849 BuildMI(BB, use4?Alpha::S4ADDQ:Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001850 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001851 }
1852 //small addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001853 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
1854 CSD->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001855 { //Normal imm add/sub
1856 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1857 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001858 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CSD->getValue());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001859 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001860 //larger addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001861 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
1862 CSD->getSignExtended() <= 32767 &&
1863 CSD->getSignExtended() >= -32767)
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001864 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001865 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001866 Tmp2 = (long)CSD->getSignExtended();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001867 if (!isAdd)
1868 Tmp2 = -Tmp2;
1869 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001870 }
1871 //give up and do the operation
1872 else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001873 //Normal add/sub
1874 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1875 Tmp1 = SelectExpr(N.getOperand(0));
1876 Tmp2 = SelectExpr(N.getOperand(1));
1877 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1878 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001879 return Result;
1880 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001881
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001882 case ISD::SDIV:
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001883 {
Andrew Lenhartha565c272005-04-06 22:03:13 +00001884 ConstantSDNode* CSD;
1885 //check if we can convert into a shift!
1886 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
1887 (int64_t)CSD->getSignExtended() != 0 &&
1888 ExactLog2(abs((int64_t)CSD->getSignExtended())) != 0)
1889 {
1890 unsigned k = ExactLog2(abs(CSD->getSignExtended()));
1891 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenhartha565c272005-04-06 22:03:13 +00001892 if (k == 1)
1893 Tmp2 = Tmp1;
1894 else
1895 {
1896 Tmp2 = MakeReg(MVT::i64);
1897 BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
1898 }
1899 Tmp3 = MakeReg(MVT::i64);
1900 BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
1901 unsigned Tmp4 = MakeReg(MVT::i64);
1902 BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
1903 if ((int64_t)CSD->getSignExtended() > 0)
1904 BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
1905 else
1906 {
1907 unsigned Tmp5 = MakeReg(MVT::i64);
1908 BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
1909 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
1910 }
1911 return Result;
1912 }
1913 }
1914 //Else fall through
1915
1916 case ISD::UDIV:
1917 {
1918 ConstantSDNode* CSD;
1919 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
1920 ((int64_t)CSD->getSignExtended() >= 2 ||
1921 (int64_t)CSD->getSignExtended() <= -2))
1922 {
1923 // If this is a divide by constant, we can emit code using some magic
1924 // constants to implement it as a multiply instead.
1925 ExprMap.erase(N);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001926 if (opcode == ISD::SDIV)
Andrew Lenhartha565c272005-04-06 22:03:13 +00001927 return SelectExpr(BuildSDIVSequence(N));
1928 else
1929 return SelectExpr(BuildUDIVSequence(N));
1930 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001931 }
1932 //else fall though
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001933 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +00001934 case ISD::SREM:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001935 //FIXME: alpha really doesn't support any of these operations,
Andrew Lenharth40831c52005-01-28 06:57:18 +00001936 // the ops are expanded into special library calls with
1937 // special calling conventions
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001938 //Restore GP because it is a call after all...
Andrew Lenharth40831c52005-01-28 06:57:18 +00001939 switch(opcode) {
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001940 case ISD::UREM: Opc = Alpha::REMQU; break;
1941 case ISD::SREM: Opc = Alpha::REMQ; break;
1942 case ISD::UDIV: Opc = Alpha::DIVQU; break;
1943 case ISD::SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001944 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001945 Tmp1 = SelectExpr(N.getOperand(0));
1946 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth33819132005-03-04 20:09:23 +00001947 //set up regs explicitly (helps Reg alloc)
1948 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001949 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001950 AlphaLowering.restoreGP(BB);
Andrew Lenharth33819132005-03-04 20:09:23 +00001951 BuildMI(BB, Opc, 2).addReg(Alpha::R24).addReg(Alpha::R25);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001952 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001953 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001954
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001955 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001956 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001957 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001958 assert (DestType == MVT::i64 && "only quads can be loaded to");
1959 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00001960 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001961 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001962 if (SrcType == MVT::f32)
Misha Brukman7847fca2005-04-22 17:54:37 +00001963 {
1964 Tmp2 = MakeReg(MVT::f64);
1965 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
1966 Tmp1 = Tmp2;
1967 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001968 Tmp2 = MakeReg(MVT::f64);
1969 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001970 MoveFP2Int(Tmp2, Result, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001971
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001972 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001973 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001974
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001975 case ISD::SELECT:
1976 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001977 //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 +00001978 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001979 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1980 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001981 // Get the condition into the zero flag.
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001982 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001983
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001984 SDOperand CC = N.getOperand(0);
1985 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
1986
Misha Brukman4633f1c2005-04-21 23:13:11 +00001987 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001988 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
1989 { //FP Setcc -> Int Select
Misha Brukman7847fca2005-04-22 17:54:37 +00001990 Tmp1 = MakeReg(MVT::f64);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001991 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1992 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Misha Brukman7847fca2005-04-22 17:54:37 +00001993 bool inv = SelectFPSetCC(CC, Tmp1);
1994 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
1995 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
1996 return Result;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001997 }
1998 if (CC.getOpcode() == ISD::SETCC) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001999 //Int SetCC -> Select
2000 //Dropping the CC is only useful if we are comparing to 0
2001 if((SetCC->getOperand(1).getOpcode() == ISD::Constant &&
2002 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0) ||
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002003 (SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2004 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0))
2005 {
2006 //figure out a few things
2007 bool LeftZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2008 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
2009 bool RightZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2010 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
2011 bool LeftConst = N.getOperand(1).getOpcode() == ISD::Constant &&
2012 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255;
2013 bool RightConst = N.getOperand(2).getOpcode() == ISD::Constant &&
2014 cast<ConstantSDNode>(N.getOperand(2))->getValue() <= 255;
2015 bool useImm = LeftConst || RightConst;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002016
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002017 //Fix up CC
2018 ISD::CondCode cCode= SetCC->getCondition();
2019 if (RightConst && !LeftConst) //Invert sense to get Imm field right
2020 cCode = ISD::getSetCCInverse(cCode, true);
2021 if (LeftZero && !RightZero) //Swap Operands
2022 cCode = ISD::getSetCCSwappedOperands(cCode);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002023
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002024 //Choose the CMOV
2025 switch (cCode) {
2026 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
2027 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
2028 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
2029 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
2030 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
2031 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
2032 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
2033 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2034 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break; //Technically you could have this CC
2035 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
2036 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2037 }
2038 if(LeftZero && !RightZero) //swap Operands
2039 Tmp1 = SelectExpr(SetCC->getOperand(1)); //Cond
2040 else
2041 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
2042
2043 if (LeftConst) {
2044 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2045 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
Misha Brukman7847fca2005-04-22 17:54:37 +00002046 .addImm(cast<ConstantSDNode>(N.getOperand(1))->getValue())
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002047 .addReg(Tmp1);
2048 } else if (RightConst) {
2049 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
2050 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
Misha Brukman7847fca2005-04-22 17:54:37 +00002051 .addImm(cast<ConstantSDNode>(N.getOperand(2))->getValue())
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002052 .addReg(Tmp1);
2053 } else {
2054 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2055 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2056 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
2057 }
2058 return Result;
2059 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002060 //Otherwise, fall though
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002061 }
2062 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002063 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2064 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthe76797c2005-02-01 20:40:27 +00002065 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002066
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002067 return Result;
2068 }
2069
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002070 case ISD::Constant:
2071 {
Andrew Lenharthc0513832005-03-29 19:24:04 +00002072 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002073 if (val <= IMM_HIGH && val >= IMM_LOW) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002074 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002075 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002076 else if (val <= (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT &&
2077 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
2078 Tmp1 = MakeReg(MVT::i64);
2079 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val)).addReg(Alpha::R31);
2080 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002081 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002082 else {
2083 MachineConstantPool *CP = BB->getParent()->getConstantPool();
2084 ConstantUInt *C = ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
2085 unsigned CPI = CP->getConstantPoolIndex(C);
2086 AlphaLowering.restoreGP(BB);
2087 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CPI);
2088 }
2089 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002090 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002091 }
2092
2093 return 0;
2094}
2095
2096void ISel::Select(SDOperand N) {
2097 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00002098 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002099
Nate Begeman85fdeb22005-03-24 04:39:54 +00002100 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002101 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002102
2103 SDNode *Node = N.Val;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002104
Andrew Lenharth760270d2005-02-07 23:02:23 +00002105 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002106
2107 default:
2108 Node->dump(); std::cerr << "\n";
2109 assert(0 && "Node not handled yet!");
2110
2111 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00002112 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002113 return;
2114 }
2115
2116 case ISD::BR: {
2117 MachineBasicBlock *Dest =
2118 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2119
2120 Select(N.getOperand(0));
2121 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
2122 return;
2123 }
2124
2125 case ISD::ImplicitDef:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002126 ++count_ins;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002127 Select(N.getOperand(0));
2128 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
2129 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002130
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002131 case ISD::EntryToken: return; // Noop
2132
2133 case ISD::TokenFactor:
2134 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2135 Select(Node->getOperand(i));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002136
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002137 //N.Val->dump(); std::cerr << "\n";
2138 //assert(0 && "Node not handled yet!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00002139
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002140 return;
2141
2142 case ISD::CopyToReg:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002143 ++count_outs;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002144 Select(N.getOperand(0));
2145 Tmp1 = SelectExpr(N.getOperand(1));
2146 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002147
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002148 if (Tmp1 != Tmp2) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002149 if (N.getOperand(1).getValueType() == MVT::f64 ||
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002150 N.getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00002151 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2152 else
2153 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002154 }
2155 return;
2156
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002157 case ISD::RET:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002158 ++count_outs;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002159 switch (N.getNumOperands()) {
2160 default:
2161 std::cerr << N.getNumOperands() << "\n";
2162 for (unsigned i = 0; i < N.getNumOperands(); ++i)
2163 std::cerr << N.getOperand(i).getValueType() << "\n";
2164 Node->dump();
2165 assert(0 && "Unknown return instruction!");
2166 case 2:
2167 Select(N.getOperand(0));
2168 Tmp1 = SelectExpr(N.getOperand(1));
2169 switch (N.getOperand(1).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002170 default: Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002171 assert(0 && "All other types should have been promoted!!");
2172 case MVT::f64:
2173 case MVT::f32:
2174 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
2175 break;
2176 case MVT::i32:
2177 case MVT::i64:
2178 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
2179 break;
2180 }
2181 break;
2182 case 1:
2183 Select(N.getOperand(0));
2184 break;
2185 }
2186 //Tmp2 = AlphaLowering.getRetAddr();
2187 //BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(Tmp2).addReg(Tmp2);
2188 BuildMI(BB, Alpha::RETURN, 0); // Just emit a 'ret' instruction
2189 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002190
Misha Brukman4633f1c2005-04-21 23:13:11 +00002191 case ISD::TRUNCSTORE:
2192 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002193 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002194 SDOperand Chain = N.getOperand(0);
2195 SDOperand Value = N.getOperand(1);
2196 SDOperand Address = N.getOperand(2);
2197 Select(Chain);
2198
2199 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00002200
2201 if (opcode == ISD::STORE) {
2202 switch(Value.getValueType()) {
2203 default: assert(0 && "unknown Type in store");
2204 case MVT::i64: Opc = Alpha::STQ; break;
2205 case MVT::f64: Opc = Alpha::STT; break;
2206 case MVT::f32: Opc = Alpha::STS; break;
2207 }
2208 } else { //ISD::TRUNCSTORE
2209 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2210 default: assert(0 && "unknown Type in store");
2211 case MVT::i1: //FIXME: DAG does not promote this load
2212 case MVT::i8: Opc = Alpha::STB; break;
2213 case MVT::i16: Opc = Alpha::STW; break;
2214 case MVT::i32: Opc = Alpha::STL; break;
2215 }
Andrew Lenharth65838902005-02-06 16:22:15 +00002216 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00002217
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002218 if (Address.getOpcode() == ISD::GlobalAddress)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002219 {
2220 AlphaLowering.restoreGP(BB);
2221 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002222 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002223 BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
2224 }
Andrew Lenharth05380342005-02-07 05:07:00 +00002225 else if(Address.getOpcode() == ISD::FrameIndex)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002226 {
Andrew Lenharth032f2352005-02-22 21:59:48 +00002227 BuildMI(BB, Opc, 3).addReg(Tmp1)
2228 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
2229 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002230 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002231 else
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002232 {
2233 long offset;
2234 SelectAddr(Address, Tmp2, offset);
2235 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2236 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002237 return;
2238 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002239
2240 case ISD::EXTLOAD:
2241 case ISD::SEXTLOAD:
2242 case ISD::ZEXTLOAD:
2243 case ISD::LOAD:
2244 case ISD::CopyFromReg:
2245 case ISD::CALL:
Andrew Lenharth032f2352005-02-22 21:59:48 +00002246 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002247 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002248 SelectExpr(N);
2249 return;
2250
Chris Lattner16cd04d2005-05-12 23:24:06 +00002251 case ISD::CALLSEQ_START:
2252 case ISD::CALLSEQ_END:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002253 Select(N.getOperand(0));
2254 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002255
Chris Lattner16cd04d2005-05-12 23:24:06 +00002256 Opc = N.getOpcode() == ISD::CALLSEQ_START ? Alpha::ADJUSTSTACKDOWN :
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002257 Alpha::ADJUSTSTACKUP;
2258 BuildMI(BB, Opc, 1).addImm(Tmp1);
2259 return;
Andrew Lenharth95762122005-03-31 21:24:06 +00002260
2261 case ISD::PCMARKER:
2262 Select(N.getOperand(0)); //Chain
2263 BuildMI(BB, Alpha::PCLABEL, 2).addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
2264 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002265 }
2266 assert(0 && "Should not be reached!");
2267}
2268
2269
2270/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
2271/// into a machine code representation using pattern matching and a machine
2272/// description file.
2273///
2274FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002275 return new ISel(TM);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002276}
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002277