blob: d7dab5d0a5c1d9f0d6f3d945564aac15d4b32c29 [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
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001196 if (N.getOpcode() != ISD::CALL && N.getOpcode() != ISD::TAILCALL)
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001197 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 ))
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001220 && opcode != ISD::CALL && opcode != ISD::TAILCALL
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
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001379 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001380 case ISD::CALL:
1381 {
1382 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001383
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001384 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001385 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001386
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001387 //grab the arguments
1388 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001389 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001390 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001391 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001392
Andrew Lenharth684f2292005-01-30 00:35:27 +00001393 //in reg args
1394 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001395 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001396 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001397 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +00001398 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001399 Alpha::F19, Alpha::F20, Alpha::F21};
1400 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001401 default:
1402 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001403 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001404 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001405 N.getOperand(i+2).getValueType() << "\n";
1406 assert(0 && "Unknown value type for call");
1407 case MVT::i1:
1408 case MVT::i8:
1409 case MVT::i16:
1410 case MVT::i32:
1411 case MVT::i64:
1412 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1413 break;
1414 case MVT::f32:
1415 case MVT::f64:
1416 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1417 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001418 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001419 }
Andrew Lenharth684f2292005-01-30 00:35:27 +00001420 //in mem args
1421 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001422 {
1423 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001424 default:
1425 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001426 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001427 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001428 N.getOperand(i+2).getValueType() << "\n";
1429 assert(0 && "Unknown value type for call");
1430 case MVT::i1:
1431 case MVT::i8:
1432 case MVT::i16:
1433 case MVT::i32:
1434 case MVT::i64:
1435 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1436 break;
1437 case MVT::f32:
1438 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1439 break;
1440 case MVT::f64:
1441 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1442 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001443 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001444 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001445 //build the right kind of call
1446 if (GlobalAddressSDNode *GASD =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001447 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001448 {
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001449 if (GASD->getGlobal()->isExternal()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001450 //use safe calling convention
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001451 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001452 has_sym = true;
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001453 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal());
1454 } else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001455 //use PC relative branch call
Andrew Lenharth1e0d9bd2005-04-14 17:34:20 +00001456 AlphaLowering.restoreGP(BB);
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001457 BuildMI(BB, Alpha::BSR, 1, Alpha::R26).addGlobalAddress(GASD->getGlobal(),true);
1458 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001459 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001460 else if (ExternalSymbolSDNode *ESSDN =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001461 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001462 {
1463 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001464 has_sym = true;
Andrew Lenharthba05ad62005-03-30 18:22:52 +00001465 BuildMI(BB, Alpha::CALL, 1).addExternalSymbol(ESSDN->getSymbol(), true);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001466 } else {
1467 //no need to restore GP as we are doing an indirect call
1468 Tmp1 = SelectExpr(N.getOperand(1));
1469 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
1470 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
1471 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001472
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001473 //push the result into a virtual register
Misha Brukman4633f1c2005-04-21 23:13:11 +00001474
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001475 switch (Node->getValueType(0)) {
1476 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001477 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001478 case MVT::i1:
1479 case MVT::i8:
1480 case MVT::i16:
1481 case MVT::i32:
1482 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001483 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
1484 break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001485 case MVT::f32:
1486 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001487 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
1488 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001489 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001490 return Result+N.ResNo;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001491 }
1492
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001493 case ISD::SIGN_EXTEND_INREG:
1494 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001495 //do SDIV opt for all levels of ints if not dividing by a constant
1496 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
1497 && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001498 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001499 unsigned Tmp4 = MakeReg(MVT::f64);
1500 unsigned Tmp5 = MakeReg(MVT::f64);
1501 unsigned Tmp6 = MakeReg(MVT::f64);
1502 unsigned Tmp7 = MakeReg(MVT::f64);
1503 unsigned Tmp8 = MakeReg(MVT::f64);
1504 unsigned Tmp9 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001505
1506 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1507 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1508 MoveInt2FP(Tmp1, Tmp4, true);
1509 MoveInt2FP(Tmp2, Tmp5, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001510 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Tmp4);
1511 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Tmp5);
1512 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
1513 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Tmp8);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001514 MoveFP2Int(Tmp9, Result, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001515 return Result;
1516 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001517
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001518 //Alpha has instructions for a bunch of signed 32 bit stuff
1519 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001520 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001521 switch (N.getOperand(0).getOpcode()) {
1522 case ISD::ADD:
1523 case ISD::SUB:
1524 case ISD::MUL:
1525 {
1526 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
1527 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
1528 //FIXME: first check for Scaled Adds and Subs!
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001529 ConstantSDNode* CSD = NULL;
1530 if(!isMul && N.getOperand(0).getOperand(0).getOpcode() == ISD::SHL &&
1531 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(0).getOperand(1))) &&
1532 (CSD->getValue() == 2 || CSD->getValue() == 3))
1533 {
1534 bool use4 = CSD->getValue() == 2;
1535 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1536 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1537 BuildMI(BB, isAdd?(use4?Alpha::S4ADDL:Alpha::S8ADDL):(use4?Alpha::S4SUBL:Alpha::S8SUBL),
1538 2,Result).addReg(Tmp1).addReg(Tmp2);
1539 }
1540 else if(isAdd && N.getOperand(0).getOperand(1).getOpcode() == ISD::SHL &&
1541 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1).getOperand(1))) &&
1542 (CSD->getValue() == 2 || CSD->getValue() == 3))
1543 {
1544 bool use4 = CSD->getValue() == 2;
1545 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1546 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1547 BuildMI(BB, use4?Alpha::S4ADDL:Alpha::S8ADDL, 2,Result).addReg(Tmp1).addReg(Tmp2);
1548 }
1549 else if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001550 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
1551 { //Normal imm add/sub
1552 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001553 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001554 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
1555 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001556 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001557 else
1558 { //Normal add/sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001559 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULL : Alpha::SUBL);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001560 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001561 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001562 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1563 }
1564 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001565 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001566 default: break; //Fall Though;
1567 }
1568 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001569 Tmp1 = SelectExpr(N.getOperand(0));
1570 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001571 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001572 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001573 {
1574 default:
1575 Node->dump();
1576 assert(0 && "Sign Extend InReg not there yet");
1577 break;
1578 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001579 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001580 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001581 break;
1582 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001583 case MVT::i16:
1584 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
1585 break;
1586 case MVT::i8:
1587 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
1588 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +00001589 case MVT::i1:
1590 Tmp2 = MakeReg(MVT::i64);
1591 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001592 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +00001593 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001594 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001595 return Result;
1596 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001597
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001598 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001599 {
1600 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1601 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1602 bool isConst1 = false;
1603 bool isConst2 = false;
1604 int dir;
Misha Brukman7847fca2005-04-22 17:54:37 +00001605
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001606 //Tmp1 = SelectExpr(N.getOperand(0));
1607 if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001608 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
1609 isConst1 = true;
1610 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001611 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1612 isConst2 = true;
1613
1614 switch (SetCC->getCondition()) {
1615 default: Node->dump(); assert(0 && "Unknown integer comparison!");
1616 case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001617 case ISD::SETLT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001618 Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001619 case ISD::SETLE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001620 Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001621 case ISD::SETGT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001622 Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001623 case ISD::SETGE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001624 Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001625 case ISD::SETULT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001626 Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001627 case ISD::SETUGT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001628 Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001629 case ISD::SETULE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001630 Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001631 case ISD::SETUGE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001632 Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001633 case ISD::SETNE: {//Handle this one special
1634 //std::cerr << "Alpha does not have a setne.\n";
1635 //abort();
1636 Tmp1 = SelectExpr(N.getOperand(0));
1637 Tmp2 = SelectExpr(N.getOperand(1));
1638 Tmp3 = MakeReg(MVT::i64);
1639 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001640 //Remeber we have the Inv for this CC
1641 CCInvMap[N] = Tmp3;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001642 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001643 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001644 return Result;
1645 }
1646 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001647 if (dir == 1) {
1648 Tmp1 = SelectExpr(N.getOperand(0));
1649 if (isConst2) {
1650 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1651 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1652 } else {
1653 Tmp2 = SelectExpr(N.getOperand(1));
1654 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1655 }
1656 } else if (dir == 2) {
1657 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001658 if (isConst1) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001659 Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1660 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1661 } else {
1662 Tmp2 = SelectExpr(N.getOperand(0));
1663 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1664 }
1665 } else { //dir == 0
1666 if (isConst1) {
1667 Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1668 Tmp2 = SelectExpr(N.getOperand(1));
1669 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
1670 } else if (isConst2) {
1671 Tmp1 = SelectExpr(N.getOperand(0));
1672 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1673 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
1674 } else {
1675 Tmp1 = SelectExpr(N.getOperand(0));
1676 Tmp2 = SelectExpr(N.getOperand(1));
1677 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1678 }
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001679 }
1680 } else {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001681 //do the comparison
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001682 Tmp1 = MakeReg(MVT::f64);
1683 bool inv = SelectFPSetCC(N, Tmp1);
1684
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001685 //now arrange for Result (int) to have a 1 or 0
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001686 Tmp2 = MakeReg(MVT::i64);
1687 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001688 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001689 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001690 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001691 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001692 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001693 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001694
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001695 case ISD::CopyFromReg:
1696 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001697 ++count_ins;
1698
Andrew Lenharth40831c52005-01-28 06:57:18 +00001699 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001700 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001701 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001702 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001703 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001704
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001705 SDOperand Chain = N.getOperand(0);
1706
1707 Select(Chain);
1708 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1709 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1710 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1711 return Result;
1712 }
1713
Misha Brukman4633f1c2005-04-21 23:13:11 +00001714 //Most of the plain arithmetic and logic share the same form, and the same
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001715 //constant immediate test
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001716 case ISD::XOR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001717 //Match Not
1718 if (N.getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001719 cast<ConstantSDNode>(N.getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001720 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001721 Tmp1 = SelectExpr(N.getOperand(0));
1722 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1723 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001724 }
1725 //Fall through
1726 case ISD::AND:
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001727 //handle zap
1728 if (opcode == ISD::AND && N.getOperand(1).getOpcode() == ISD::Constant)
1729 {
1730 uint64_t k = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1731 unsigned int build = 0;
1732 for(int i = 0; i < 8; ++i)
1733 {
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001734 if ((k & 0x00FF) == 0x00FF)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001735 build |= 1 << i;
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001736 else if ((k & 0x00FF) != 0)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001737 { build = 0; break; }
1738 k >>= 8;
1739 }
1740 if (build)
1741 {
1742 Tmp1 = SelectExpr(N.getOperand(0));
1743 BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1744 return Result;
1745 }
1746 }
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001747 case ISD::OR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001748 //Check operand(0) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001749 if (N.getOperand(0).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001750 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001751 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001752 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001753 switch(opcode) {
1754 case ISD::AND: Opc = Alpha::BIC; break;
1755 case ISD::OR: Opc = Alpha::ORNOT; break;
1756 case ISD::XOR: Opc = Alpha::EQV; break;
1757 }
1758 Tmp1 = SelectExpr(N.getOperand(1));
1759 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1760 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1761 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001762 }
1763 //Check operand(1) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001764 if (N.getOperand(1).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001765 N.getOperand(1).getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001766 cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001767 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001768 switch(opcode) {
1769 case ISD::AND: Opc = Alpha::BIC; break;
1770 case ISD::OR: Opc = Alpha::ORNOT; break;
1771 case ISD::XOR: Opc = Alpha::EQV; break;
1772 }
1773 Tmp1 = SelectExpr(N.getOperand(0));
1774 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1775 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1776 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001777 }
1778 //Fall through
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001779 case ISD::SHL:
1780 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001781 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001782 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001783 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1784 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001785 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001786 {
1787 switch(opcode) {
1788 case ISD::AND: Opc = Alpha::ANDi; break;
1789 case ISD::OR: Opc = Alpha::BISi; break;
1790 case ISD::XOR: Opc = Alpha::XORi; break;
1791 case ISD::SHL: Opc = Alpha::SLi; break;
1792 case ISD::SRL: Opc = Alpha::SRLi; break;
1793 case ISD::SRA: Opc = Alpha::SRAi; break;
1794 case ISD::MUL: Opc = Alpha::MULQi; break;
1795 };
1796 Tmp1 = SelectExpr(N.getOperand(0));
1797 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1798 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1799 } else {
1800 switch(opcode) {
1801 case ISD::AND: Opc = Alpha::AND; break;
1802 case ISD::OR: Opc = Alpha::BIS; break;
1803 case ISD::XOR: Opc = Alpha::XOR; break;
1804 case ISD::SHL: Opc = Alpha::SL; break;
1805 case ISD::SRL: Opc = Alpha::SRL; break;
1806 case ISD::SRA: Opc = Alpha::SRA; break;
1807 case ISD::MUL: Opc = Alpha::MULQ; break;
1808 };
1809 Tmp1 = SelectExpr(N.getOperand(0));
1810 Tmp2 = SelectExpr(N.getOperand(1));
1811 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1812 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001813 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001814
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001815 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001816 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001817 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001818 bool isAdd = opcode == ISD::ADD;
1819
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001820 //first check for Scaled Adds and Subs!
1821 //Valid for add and sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001822 ConstantSDNode* CSD = NULL;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001823 if(N.getOperand(0).getOpcode() == ISD::SHL &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001824 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
1825 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001826 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001827 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001828 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001829 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) && CSD->getValue() <= 255)
1830 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1831 2, Result).addReg(Tmp2).addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001832 else {
1833 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001834 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1835 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001836 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001837 }
1838 //Position prevents subs
Andrew Lenharth273a1f92005-04-07 14:18:13 +00001839 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001840 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) &&
1841 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001842 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001843 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001844 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001845 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(0))) && CSD->getValue() <= 255)
1846 BuildMI(BB, use4?Alpha::S4ADDQi:Alpha::S8ADDQi, 2, Result).addReg(Tmp2)
1847 .addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001848 else {
1849 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001850 BuildMI(BB, use4?Alpha::S4ADDQ:Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001851 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001852 }
1853 //small addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001854 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
1855 CSD->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001856 { //Normal imm add/sub
1857 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1858 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001859 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CSD->getValue());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001860 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001861 //larger addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001862 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
1863 CSD->getSignExtended() <= 32767 &&
1864 CSD->getSignExtended() >= -32767)
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001865 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001866 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001867 Tmp2 = (long)CSD->getSignExtended();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001868 if (!isAdd)
1869 Tmp2 = -Tmp2;
1870 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001871 }
1872 //give up and do the operation
1873 else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001874 //Normal add/sub
1875 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1876 Tmp1 = SelectExpr(N.getOperand(0));
1877 Tmp2 = SelectExpr(N.getOperand(1));
1878 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1879 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001880 return Result;
1881 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001882
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001883 case ISD::SDIV:
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001884 {
Andrew Lenhartha565c272005-04-06 22:03:13 +00001885 ConstantSDNode* CSD;
1886 //check if we can convert into a shift!
1887 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
1888 (int64_t)CSD->getSignExtended() != 0 &&
1889 ExactLog2(abs((int64_t)CSD->getSignExtended())) != 0)
1890 {
1891 unsigned k = ExactLog2(abs(CSD->getSignExtended()));
1892 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenhartha565c272005-04-06 22:03:13 +00001893 if (k == 1)
1894 Tmp2 = Tmp1;
1895 else
1896 {
1897 Tmp2 = MakeReg(MVT::i64);
1898 BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
1899 }
1900 Tmp3 = MakeReg(MVT::i64);
1901 BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
1902 unsigned Tmp4 = MakeReg(MVT::i64);
1903 BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
1904 if ((int64_t)CSD->getSignExtended() > 0)
1905 BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
1906 else
1907 {
1908 unsigned Tmp5 = MakeReg(MVT::i64);
1909 BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
1910 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
1911 }
1912 return Result;
1913 }
1914 }
1915 //Else fall through
1916
1917 case ISD::UDIV:
1918 {
1919 ConstantSDNode* CSD;
1920 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
1921 ((int64_t)CSD->getSignExtended() >= 2 ||
1922 (int64_t)CSD->getSignExtended() <= -2))
1923 {
1924 // If this is a divide by constant, we can emit code using some magic
1925 // constants to implement it as a multiply instead.
1926 ExprMap.erase(N);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001927 if (opcode == ISD::SDIV)
Andrew Lenhartha565c272005-04-06 22:03:13 +00001928 return SelectExpr(BuildSDIVSequence(N));
1929 else
1930 return SelectExpr(BuildUDIVSequence(N));
1931 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001932 }
1933 //else fall though
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001934 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +00001935 case ISD::SREM:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001936 //FIXME: alpha really doesn't support any of these operations,
Andrew Lenharth40831c52005-01-28 06:57:18 +00001937 // the ops are expanded into special library calls with
1938 // special calling conventions
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001939 //Restore GP because it is a call after all...
Andrew Lenharth40831c52005-01-28 06:57:18 +00001940 switch(opcode) {
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001941 case ISD::UREM: Opc = Alpha::REMQU; break;
1942 case ISD::SREM: Opc = Alpha::REMQ; break;
1943 case ISD::UDIV: Opc = Alpha::DIVQU; break;
1944 case ISD::SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001945 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001946 Tmp1 = SelectExpr(N.getOperand(0));
1947 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth33819132005-03-04 20:09:23 +00001948 //set up regs explicitly (helps Reg alloc)
1949 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001950 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001951 AlphaLowering.restoreGP(BB);
Andrew Lenharth33819132005-03-04 20:09:23 +00001952 BuildMI(BB, Opc, 2).addReg(Alpha::R24).addReg(Alpha::R25);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001953 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001954 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001955
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001956 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001957 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001958 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001959 assert (DestType == MVT::i64 && "only quads can be loaded to");
1960 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00001961 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001962 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001963 if (SrcType == MVT::f32)
Misha Brukman7847fca2005-04-22 17:54:37 +00001964 {
1965 Tmp2 = MakeReg(MVT::f64);
1966 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
1967 Tmp1 = Tmp2;
1968 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001969 Tmp2 = MakeReg(MVT::f64);
1970 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001971 MoveFP2Int(Tmp2, Result, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001972
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001973 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001974 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001975
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001976 case ISD::SELECT:
1977 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001978 //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 +00001979 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001980 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1981 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001982 // Get the condition into the zero flag.
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001983 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001984
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001985 SDOperand CC = N.getOperand(0);
1986 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
1987
Misha Brukman4633f1c2005-04-21 23:13:11 +00001988 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001989 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
1990 { //FP Setcc -> Int Select
Misha Brukman7847fca2005-04-22 17:54:37 +00001991 Tmp1 = MakeReg(MVT::f64);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001992 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1993 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Misha Brukman7847fca2005-04-22 17:54:37 +00001994 bool inv = SelectFPSetCC(CC, Tmp1);
1995 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
1996 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
1997 return Result;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001998 }
1999 if (CC.getOpcode() == ISD::SETCC) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002000 //Int SetCC -> Select
2001 //Dropping the CC is only useful if we are comparing to 0
2002 if((SetCC->getOperand(1).getOpcode() == ISD::Constant &&
2003 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0) ||
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002004 (SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2005 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0))
2006 {
2007 //figure out a few things
2008 bool LeftZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2009 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
2010 bool RightZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2011 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
2012 bool LeftConst = N.getOperand(1).getOpcode() == ISD::Constant &&
2013 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255;
2014 bool RightConst = N.getOperand(2).getOpcode() == ISD::Constant &&
2015 cast<ConstantSDNode>(N.getOperand(2))->getValue() <= 255;
2016 bool useImm = LeftConst || RightConst;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002017
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002018 //Fix up CC
2019 ISD::CondCode cCode= SetCC->getCondition();
2020 if (RightConst && !LeftConst) //Invert sense to get Imm field right
2021 cCode = ISD::getSetCCInverse(cCode, true);
2022 if (LeftZero && !RightZero) //Swap Operands
2023 cCode = ISD::getSetCCSwappedOperands(cCode);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002024
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002025 //Choose the CMOV
2026 switch (cCode) {
2027 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
2028 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
2029 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
2030 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
2031 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
2032 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
2033 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
2034 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2035 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break; //Technically you could have this CC
2036 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
2037 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2038 }
2039 if(LeftZero && !RightZero) //swap Operands
2040 Tmp1 = SelectExpr(SetCC->getOperand(1)); //Cond
2041 else
2042 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
2043
2044 if (LeftConst) {
2045 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2046 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
Misha Brukman7847fca2005-04-22 17:54:37 +00002047 .addImm(cast<ConstantSDNode>(N.getOperand(1))->getValue())
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002048 .addReg(Tmp1);
2049 } else if (RightConst) {
2050 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
2051 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
Misha Brukman7847fca2005-04-22 17:54:37 +00002052 .addImm(cast<ConstantSDNode>(N.getOperand(2))->getValue())
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002053 .addReg(Tmp1);
2054 } else {
2055 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2056 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2057 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
2058 }
2059 return Result;
2060 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002061 //Otherwise, fall though
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002062 }
2063 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002064 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2065 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthe76797c2005-02-01 20:40:27 +00002066 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002067
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002068 return Result;
2069 }
2070
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002071 case ISD::Constant:
2072 {
Andrew Lenharthc0513832005-03-29 19:24:04 +00002073 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002074 if (val <= IMM_HIGH && val >= IMM_LOW) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002075 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002076 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002077 else if (val <= (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT &&
2078 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
2079 Tmp1 = MakeReg(MVT::i64);
2080 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val)).addReg(Alpha::R31);
2081 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002082 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002083 else {
2084 MachineConstantPool *CP = BB->getParent()->getConstantPool();
2085 ConstantUInt *C = ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
2086 unsigned CPI = CP->getConstantPoolIndex(C);
2087 AlphaLowering.restoreGP(BB);
2088 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CPI);
2089 }
2090 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002091 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002092 }
2093
2094 return 0;
2095}
2096
2097void ISel::Select(SDOperand N) {
2098 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00002099 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002100
Nate Begeman85fdeb22005-03-24 04:39:54 +00002101 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002102 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002103
2104 SDNode *Node = N.Val;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002105
Andrew Lenharth760270d2005-02-07 23:02:23 +00002106 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002107
2108 default:
2109 Node->dump(); std::cerr << "\n";
2110 assert(0 && "Node not handled yet!");
2111
2112 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00002113 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002114 return;
2115 }
2116
2117 case ISD::BR: {
2118 MachineBasicBlock *Dest =
2119 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2120
2121 Select(N.getOperand(0));
2122 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
2123 return;
2124 }
2125
2126 case ISD::ImplicitDef:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002127 ++count_ins;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002128 Select(N.getOperand(0));
2129 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
2130 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002131
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002132 case ISD::EntryToken: return; // Noop
2133
2134 case ISD::TokenFactor:
2135 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2136 Select(Node->getOperand(i));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002137
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002138 //N.Val->dump(); std::cerr << "\n";
2139 //assert(0 && "Node not handled yet!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00002140
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002141 return;
2142
2143 case ISD::CopyToReg:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002144 ++count_outs;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002145 Select(N.getOperand(0));
2146 Tmp1 = SelectExpr(N.getOperand(1));
2147 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002148
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002149 if (Tmp1 != Tmp2) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002150 if (N.getOperand(1).getValueType() == MVT::f64 ||
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002151 N.getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00002152 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2153 else
2154 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002155 }
2156 return;
2157
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002158 case ISD::RET:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002159 ++count_outs;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002160 switch (N.getNumOperands()) {
2161 default:
2162 std::cerr << N.getNumOperands() << "\n";
2163 for (unsigned i = 0; i < N.getNumOperands(); ++i)
2164 std::cerr << N.getOperand(i).getValueType() << "\n";
2165 Node->dump();
2166 assert(0 && "Unknown return instruction!");
2167 case 2:
2168 Select(N.getOperand(0));
2169 Tmp1 = SelectExpr(N.getOperand(1));
2170 switch (N.getOperand(1).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002171 default: Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002172 assert(0 && "All other types should have been promoted!!");
2173 case MVT::f64:
2174 case MVT::f32:
2175 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
2176 break;
2177 case MVT::i32:
2178 case MVT::i64:
2179 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
2180 break;
2181 }
2182 break;
2183 case 1:
2184 Select(N.getOperand(0));
2185 break;
2186 }
2187 //Tmp2 = AlphaLowering.getRetAddr();
2188 //BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(Tmp2).addReg(Tmp2);
2189 BuildMI(BB, Alpha::RETURN, 0); // Just emit a 'ret' instruction
2190 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002191
Misha Brukman4633f1c2005-04-21 23:13:11 +00002192 case ISD::TRUNCSTORE:
2193 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002194 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002195 SDOperand Chain = N.getOperand(0);
2196 SDOperand Value = N.getOperand(1);
2197 SDOperand Address = N.getOperand(2);
2198 Select(Chain);
2199
2200 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00002201
2202 if (opcode == ISD::STORE) {
2203 switch(Value.getValueType()) {
2204 default: assert(0 && "unknown Type in store");
2205 case MVT::i64: Opc = Alpha::STQ; break;
2206 case MVT::f64: Opc = Alpha::STT; break;
2207 case MVT::f32: Opc = Alpha::STS; break;
2208 }
2209 } else { //ISD::TRUNCSTORE
2210 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2211 default: assert(0 && "unknown Type in store");
2212 case MVT::i1: //FIXME: DAG does not promote this load
2213 case MVT::i8: Opc = Alpha::STB; break;
2214 case MVT::i16: Opc = Alpha::STW; break;
2215 case MVT::i32: Opc = Alpha::STL; break;
2216 }
Andrew Lenharth65838902005-02-06 16:22:15 +00002217 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00002218
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002219 if (Address.getOpcode() == ISD::GlobalAddress)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002220 {
2221 AlphaLowering.restoreGP(BB);
2222 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002223 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002224 BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
2225 }
Andrew Lenharth05380342005-02-07 05:07:00 +00002226 else if(Address.getOpcode() == ISD::FrameIndex)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002227 {
Andrew Lenharth032f2352005-02-22 21:59:48 +00002228 BuildMI(BB, Opc, 3).addReg(Tmp1)
2229 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
2230 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002231 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002232 else
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002233 {
2234 long offset;
2235 SelectAddr(Address, Tmp2, offset);
2236 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2237 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002238 return;
2239 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002240
2241 case ISD::EXTLOAD:
2242 case ISD::SEXTLOAD:
2243 case ISD::ZEXTLOAD:
2244 case ISD::LOAD:
2245 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00002246 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002247 case ISD::CALL:
Andrew Lenharth032f2352005-02-22 21:59:48 +00002248 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002249 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002250 SelectExpr(N);
2251 return;
2252
Chris Lattner16cd04d2005-05-12 23:24:06 +00002253 case ISD::CALLSEQ_START:
2254 case ISD::CALLSEQ_END:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002255 Select(N.getOperand(0));
2256 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002257
Chris Lattner16cd04d2005-05-12 23:24:06 +00002258 Opc = N.getOpcode() == ISD::CALLSEQ_START ? Alpha::ADJUSTSTACKDOWN :
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002259 Alpha::ADJUSTSTACKUP;
2260 BuildMI(BB, Opc, 1).addImm(Tmp1);
2261 return;
Andrew Lenharth95762122005-03-31 21:24:06 +00002262
2263 case ISD::PCMARKER:
2264 Select(N.getOperand(0)); //Chain
2265 BuildMI(BB, Alpha::PCLABEL, 2).addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
2266 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002267 }
2268 assert(0 && "Should not be reached!");
2269}
2270
2271
2272/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
2273/// into a machine code representation using pattern matching and a machine
2274/// description file.
2275///
2276FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002277 return new ISel(TM);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002278}
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002279