blob: aa4ed3b13c3c826b0f68b3f626cd1b3f18826e54 [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);
85 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
86 }
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>
Nate Begeman8e21e712005-03-26 01:29:23 +0000121 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
122 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000123
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000124 virtual std::pair<SDOperand, SDOperand>
125 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000126
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000127 virtual std::pair<SDOperand,SDOperand>
128 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
129 const Type *ArgTy, SelectionDAG &DAG);
130
131 virtual std::pair<SDOperand, SDOperand>
132 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
133 SelectionDAG &DAG);
134
135 void restoreGP(MachineBasicBlock* BB)
136 {
137 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
138 }
139 };
140}
141
142//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
143
144//For now, just use variable size stack frame format
145
146//In a standard call, the first six items are passed in registers $16
147//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
148//of argument-to-register correspondence.) The remaining items are
149//collected in a memory argument list that is a naturally aligned
150//array of quadwords. In a standard call, this list, if present, must
151//be passed at 0(SP).
Misha Brukman7847fca2005-04-22 17:54:37 +0000152//7 ... n 0(SP) ... (n-7)*8(SP)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000153
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000154// //#define FP $15
155// //#define RA $26
156// //#define PV $27
157// //#define GP $29
158// //#define SP $30
Misha Brukman4633f1c2005-04-21 23:13:11 +0000159
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000160std::vector<SDOperand>
Misha Brukman4633f1c2005-04-21 23:13:11 +0000161AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000162{
163 std::vector<SDOperand> ArgValues;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000164 std::vector<SDOperand> LS;
165 SDOperand Chain = DAG.getRoot();
166
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000167 // assert(0 && "TODO");
168 MachineFunction &MF = DAG.getMachineFunction();
Andrew Lenharth05380342005-02-07 05:07:00 +0000169 MachineFrameInfo*MFI = MF.getFrameInfo();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000170
171 GP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
172 MachineBasicBlock& BB = MF.front();
173
174 //Handle the return address
175 //BuildMI(&BB, Alpha::IDEF, 0, Alpha::R26);
176
Misha Brukman4633f1c2005-04-21 23:13:11 +0000177 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000178 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000179 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000180 Alpha::F19, Alpha::F20, Alpha::F21};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000181 int count = 0;
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000182
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000183 //Def incoming registers
184 {
185 Function::arg_iterator I = F.arg_begin();
186 Function::arg_iterator E = F.arg_end();
187 for (int i = 0; i < 6; ++i)
188 {
189 if (F.isVarArg()) {
Andrew Lenharth0b04b5d2005-04-12 17:47:57 +0000190 MF.addLiveIn(args_int[i]);
191 MF.addLiveIn(args_float[i]);
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000192// BuildMI(&BB, Alpha::IDEF, 0, args_int[i]);
193// BuildMI(&BB, Alpha::IDEF, 0, args_float[i]);
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000194 } else if (I != E)
195 {
196 if(MVT::isInteger(getValueType(I->getType())))
Andrew Lenharth0b04b5d2005-04-12 17:47:57 +0000197 MF.addLiveIn(args_int[i]);
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000198// BuildMI(&BB, Alpha::IDEF, 0, args_int[i]);
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000199 else
Andrew Lenharth0b04b5d2005-04-12 17:47:57 +0000200 MF.addLiveIn(args_float[i]);
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000201// BuildMI(&BB, Alpha::IDEF, 0, args_float[i]);
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000202 ++I;
203 }
204 }
205 }
206
Andrew Lenharth0b04b5d2005-04-12 17:47:57 +0000207 MF.addLiveIn(Alpha::R29);
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000208// BuildMI(&BB, Alpha::IDEF, 0, Alpha::R29);
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000209 BuildMI(&BB, Alpha::BIS, 2, GP).addReg(Alpha::R29).addReg(Alpha::R29);
210
Chris Lattnere4d5c442005-03-15 04:54:21 +0000211 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000212 {
213 SDOperand newroot, argt;
214 if (count < 6) {
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000215 unsigned Vreg;
216 MVT::ValueType VT = getValueType(I->getType());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000217 switch (getValueType(I->getType())) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000218 default:
219 std::cerr << "Unknown Type " << VT << "\n";
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000220 abort();
221 case MVT::f64:
222 case MVT::f32:
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000223 Vreg = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(VT));
224 BuildMI(&BB, Alpha::CPYS, 2, Vreg).addReg(args_float[count]).addReg(args_float[count]);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000225 argt = newroot = DAG.getCopyFromReg(Vreg,
226 getValueType(I->getType()),
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000227 Chain);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000228 break;
229 case MVT::i1:
230 case MVT::i8:
231 case MVT::i16:
232 case MVT::i32:
233 case MVT::i64:
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000234 Vreg = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
235 BuildMI(&BB, Alpha::BIS, 2, Vreg).addReg(args_int[count]).addReg(args_int[count]);
236 argt = newroot = DAG.getCopyFromReg(Vreg, MVT::i64, Chain);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000237 if (getValueType(I->getType()) != MVT::i64)
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000238 argt = DAG.getNode(ISD::TRUNCATE, getValueType(I->getType()), newroot);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000239 break;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000240 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000241 } else { //more args
242 // Create the frame index object for this incoming parameter...
243 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000244
245 // Create the SelectionDAG nodes corresponding to a load
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000246 //from this parameter
247 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000248 argt = newroot = DAG.getLoad(getValueType(I->getType()),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000249 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000250 }
Andrew Lenharth032f2352005-02-22 21:59:48 +0000251 ++count;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000252 LS.push_back(newroot.getValue(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000253 ArgValues.push_back(argt);
254 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000255
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000256 // If the functions takes variable number of arguments, copy all regs to stack
Misha Brukman4633f1c2005-04-21 23:13:11 +0000257 if (F.isVarArg())
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000258 for (int i = 0; i < 6; ++i)
259 {
260 unsigned Vreg = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
261 BuildMI(&BB, Alpha::BIS, 2, Vreg).addReg(args_int[i]).addReg(args_int[i]);
262 SDOperand argt = DAG.getCopyFromReg(Vreg, MVT::i64, Chain);
263 int FI = MFI->CreateFixedObject(8, -8 * (6 - i));
264 SDOperand SDFI = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000265 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, argt, SDFI, DAG.getSrcValue(NULL)));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000266
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000267 Vreg = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::f64));
268 BuildMI(&BB, Alpha::CPYS, 2, Vreg).addReg(args_float[i]).addReg(args_float[i]);
269 argt = DAG.getCopyFromReg(Vreg, MVT::f64, Chain);
270 FI = MFI->CreateFixedObject(8, - 8 * (12 - i));
271 SDFI = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000272 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, argt, SDFI, DAG.getSrcValue(NULL)));
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000273 }
274
275 // If the function takes variable number of arguments, make a frame index for
276 // the start of the first arg value... for expansion of llvm.va_start.
277 // if (F.isVarArg())
278 // VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
279
280 //Set up a token factor with all the stack traffic
281 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, LS));
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000282
283 // Finally, inform the code generator which regs we return values in.
284 switch (getValueType(F.getReturnType())) {
285 default: assert(0 && "Unknown type!");
286 case MVT::isVoid: break;
287 case MVT::i1:
288 case MVT::i8:
289 case MVT::i16:
290 case MVT::i32:
291 case MVT::i64:
292 MF.addLiveOut(Alpha::R0);
293 break;
294 case MVT::f32:
295 case MVT::f64:
296 MF.addLiveOut(Alpha::F0);
297 break;
298 }
299
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000300 //return the arguments
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000301 return ArgValues;
302}
303
304std::pair<SDOperand, SDOperand>
305AlphaTargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000306 const Type *RetTy, bool isVarArg,
307 SDOperand Callee, ArgListTy &Args,
308 SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000309 int NumBytes = 0;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000310 if (Args.size() > 6)
311 NumBytes = (Args.size() - 6) * 8;
312
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000313 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000314 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000315 std::vector<SDOperand> args_to_use;
316 for (unsigned i = 0, e = Args.size(); i != e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000317 {
318 switch (getValueType(Args[i].second)) {
319 default: assert(0 && "Unexpected ValueType for argument!");
320 case MVT::i1:
321 case MVT::i8:
322 case MVT::i16:
323 case MVT::i32:
324 // Promote the integer to 64 bits. If the input type is signed use a
325 // sign extend, otherwise use a zero extend.
326 if (Args[i].second->isSigned())
327 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
328 else
329 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
330 break;
331 case MVT::i64:
332 case MVT::f64:
333 case MVT::f32:
334 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000335 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000336 args_to_use.push_back(Args[i].first);
337 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000338
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000339 std::vector<MVT::ValueType> RetVals;
340 MVT::ValueType RetTyVT = getValueType(RetTy);
341 if (RetTyVT != MVT::isVoid)
342 RetVals.push_back(RetTyVT);
343 RetVals.push_back(MVT::Other);
344
Misha Brukman4633f1c2005-04-21 23:13:11 +0000345 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000346 Chain, Callee, args_to_use), 0);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000347 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
348 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
349 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000350 return std::make_pair(TheCall, Chain);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000351}
352
353std::pair<SDOperand, SDOperand>
354AlphaTargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
355 //vastart just returns the address of the VarArgsFrameIndex slot.
356 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64), Chain);
357}
358
359std::pair<SDOperand,SDOperand> AlphaTargetLowering::
360LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000361 const Type *ArgTy, SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000362 abort();
363}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000364
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000365
366std::pair<SDOperand, SDOperand> AlphaTargetLowering::
367LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
368 SelectionDAG &DAG) {
369 abort();
370}
371
372
373
374
375
376namespace {
377
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000378//===--------------------------------------------------------------------===//
379/// ISel - Alpha specific code to select Alpha machine instructions for
380/// SelectionDAG operations.
381//===--------------------------------------------------------------------===//
382class ISel : public SelectionDAGISel {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000383
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000384 /// AlphaLowering - This object fully describes how to lower LLVM code to an
385 /// Alpha-specific SelectionDAG.
386 AlphaTargetLowering AlphaLowering;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000387
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000388 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
389 // for sdiv and udiv until it is put into the future
390 // dag combiner.
391
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000392 /// ExprMap - As shared expressions are codegen'd, we keep track of which
393 /// vreg the value is produced in, so we only emit one copy of each compiled
394 /// tree.
395 static const unsigned notIn = (unsigned)(-1);
396 std::map<SDOperand, unsigned> ExprMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000397
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000398 //CCInvMap sometimes (SetNE) we have the inverse CC code for free
399 std::map<SDOperand, unsigned> CCInvMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000400
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000401 int count_ins;
402 int count_outs;
403 bool has_sym;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000404 int max_depth;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000405
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000406public:
Misha Brukman4633f1c2005-04-21 23:13:11 +0000407 ISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering), AlphaLowering(TM)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000408 {}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000409
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000410 /// InstructionSelectBasicBlock - This callback is invoked by
411 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
412 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
Andrew Lenharth032f2352005-02-22 21:59:48 +0000413 DEBUG(BB->dump());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000414 count_ins = 0;
415 count_outs = 0;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000416 max_depth = 0;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000417 has_sym = false;
418
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000419 // Codegen the basic block.
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000420 ISelDAG = &DAG;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000421 max_depth = DAG.getRoot().getNodeDepth();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000422 Select(DAG.getRoot());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000423
424 if(has_sym)
425 ++count_ins;
426 if(EnableAlphaCount)
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000427 std::cerr << "COUNT: " << BB->getParent()->getFunction ()->getName() << " "
428 << BB->getNumber() << " "
429 << max_depth << " "
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000430 << count_ins << " "
431 << count_outs << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000432
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000433 // Clear state used for selection.
434 ExprMap.clear();
435 CCInvMap.clear();
436 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000437
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000438 unsigned SelectExpr(SDOperand N);
439 unsigned SelectExprFP(SDOperand N, unsigned Result);
440 void Select(SDOperand N);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000441
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000442 void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
443 void SelectBranchCC(SDOperand N);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000444 void MoveFP2Int(unsigned src, unsigned dst, bool isDouble);
445 void MoveInt2FP(unsigned src, unsigned dst, bool isDouble);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000446 //returns whether the sense of the comparison was inverted
447 bool SelectFPSetCC(SDOperand N, unsigned dst);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000448
449 // dag -> dag expanders for integer divide by constant
450 SDOperand BuildSDIVSequence(SDOperand N);
451 SDOperand BuildUDIVSequence(SDOperand N);
452
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000453};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000454}
455
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000456//Factorize a number using the list of constants
457static bool factorize(int v[], int res[], int size, uint64_t c)
458{
459 bool cont = true;
460 while (c != 1 && cont)
461 {
462 cont = false;
463 for(int i = 0; i < size; ++i)
464 {
465 if (c % v[i] == 0)
466 {
467 c /= v[i];
468 ++res[i];
469 cont=true;
470 }
471 }
472 }
473 return c == 1;
474}
475
476
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000477//Shamelessly adapted from PPC32
Misha Brukman4633f1c2005-04-21 23:13:11 +0000478// Structure used to return the necessary information to codegen an SDIV as
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000479// a multiply.
480struct ms {
481 int64_t m; // magic number
482 int64_t s; // shift amount
483};
484
485struct mu {
486 uint64_t m; // magic number
487 int64_t a; // add indicator
488 int64_t s; // shift amount
489};
490
491/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukman4633f1c2005-04-21 23:13:11 +0000492/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000493/// or -1.
494static struct ms magic(int64_t d) {
495 int64_t p;
496 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
497 const uint64_t two63 = 9223372036854775808ULL; // 2^63
498 struct ms mag;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000499
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000500 ad = abs(d);
501 t = two63 + ((uint64_t)d >> 63);
502 anc = t - 1 - t%ad; // absolute value of nc
Andrew Lenharth320174f2005-04-07 17:19:16 +0000503 p = 63; // initialize p
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000504 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
505 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
506 q2 = two63/ad; // initialize q2 = 2p/abs(d)
507 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
508 do {
509 p = p + 1;
510 q1 = 2*q1; // update q1 = 2p/abs(nc)
511 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
512 if (r1 >= anc) { // must be unsigned comparison
513 q1 = q1 + 1;
514 r1 = r1 - anc;
515 }
516 q2 = 2*q2; // update q2 = 2p/abs(d)
517 r2 = 2*r2; // update r2 = rem(2p/abs(d))
518 if (r2 >= ad) { // must be unsigned comparison
519 q2 = q2 + 1;
520 r2 = r2 - ad;
521 }
522 delta = ad - r2;
523 } while (q1 < delta || (q1 == delta && r1 == 0));
524
525 mag.m = q2 + 1;
526 if (d < 0) mag.m = -mag.m; // resulting magic number
527 mag.s = p - 64; // resulting shift
528 return mag;
529}
530
531/// magicu - calculate the magic numbers required to codegen an integer udiv as
532/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
533static struct mu magicu(uint64_t d)
534{
535 int64_t p;
536 uint64_t nc, delta, q1, r1, q2, r2;
537 struct mu magu;
538 magu.a = 0; // initialize "add" indicator
539 nc = - 1 - (-d)%d;
Andrew Lenharth320174f2005-04-07 17:19:16 +0000540 p = 63; // initialize p
541 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
542 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
543 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
544 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000545 do {
546 p = p + 1;
547 if (r1 >= nc - r1 ) {
548 q1 = 2*q1 + 1; // update q1
549 r1 = 2*r1 - nc; // update r1
550 }
551 else {
552 q1 = 2*q1; // update q1
553 r1 = 2*r1; // update r1
554 }
555 if (r2 + 1 >= d - r2) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000556 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000557 q2 = 2*q2 + 1; // update q2
558 r2 = 2*r2 + 1 - d; // update r2
559 }
560 else {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000561 if (q2 >= 0x8000000000000000ull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000562 q2 = 2*q2; // update q2
563 r2 = 2*r2 + 1; // update r2
564 }
565 delta = d - 1 - r2;
566 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
567 magu.m = q2 + 1; // resulting magic number
Andrew Lenharth320174f2005-04-07 17:19:16 +0000568 magu.s = p - 64; // resulting shift
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000569 return magu;
570}
571
572/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
573/// return a DAG expression to select that will generate the same value by
574/// multiplying by a magic number. See:
575/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
576SDOperand ISel::BuildSDIVSequence(SDOperand N) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000577 int64_t d = (int64_t)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000578 ms magics = magic(d);
579 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000580 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000581 ISelDAG->getConstant(magics.m, MVT::i64));
582 // If d > 0 and m < 0, add the numerator
583 if (d > 0 && magics.m < 0)
584 Q = ISelDAG->getNode(ISD::ADD, MVT::i64, Q, N.getOperand(0));
585 // If d < 0 and m > 0, subtract the numerator.
586 if (d < 0 && magics.m > 0)
587 Q = ISelDAG->getNode(ISD::SUB, MVT::i64, Q, N.getOperand(0));
588 // Shift right algebraic if shift value is nonzero
589 if (magics.s > 0)
Misha Brukman4633f1c2005-04-21 23:13:11 +0000590 Q = ISelDAG->getNode(ISD::SRA, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000591 ISelDAG->getConstant(magics.s, MVT::i64));
592 // Extract the sign bit and add it to the quotient
Misha Brukman4633f1c2005-04-21 23:13:11 +0000593 SDOperand T =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000594 ISelDAG->getNode(ISD::SRL, MVT::i64, Q, ISelDAG->getConstant(63, MVT::i64));
595 return ISelDAG->getNode(ISD::ADD, MVT::i64, Q, T);
596}
597
598/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
599/// return a DAG expression to select that will generate the same value by
600/// multiplying by a magic number. See:
601/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
602SDOperand ISel::BuildUDIVSequence(SDOperand N) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000603 unsigned d =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000604 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
605 mu magics = magicu(d);
606 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000607 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000608 ISelDAG->getConstant(magics.m, MVT::i64));
609 if (magics.a == 0) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000610 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000611 ISelDAG->getConstant(magics.s, MVT::i64));
612 } else {
613 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i64, N.getOperand(0), Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000614 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000615 ISelDAG->getConstant(1, MVT::i64));
616 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i64, NPQ, Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000617 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000618 ISelDAG->getConstant(magics.s-1, MVT::i64));
619 }
620 return Q;
621}
622
Andrew Lenhartha565c272005-04-06 22:03:13 +0000623//From PPC32
624/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
625/// returns zero when the input is not exactly a power of two.
626static unsigned ExactLog2(uint64_t Val) {
627 if (Val == 0 || (Val & (Val-1))) return 0;
628 unsigned Count = 0;
629 while (Val != 1) {
630 Val >>= 1;
631 ++Count;
632 }
633 return Count;
634}
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000635
636
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000637//These describe LDAx
Andrew Lenharthc0513832005-03-29 19:24:04 +0000638static const int IMM_LOW = -32768;
639static const int IMM_HIGH = 32767;
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000640static const int IMM_MULT = 65536;
641
642static long getUpper16(long l)
643{
644 long y = l / IMM_MULT;
645 if (l % IMM_MULT > IMM_HIGH)
646 ++y;
647 return y;
648}
649
650static long getLower16(long l)
651{
652 long h = getUpper16(l);
653 return l - h * IMM_MULT;
654}
655
Andrew Lenharth65838902005-02-06 16:22:15 +0000656static unsigned GetSymVersion(unsigned opcode)
657{
658 switch (opcode) {
659 default: assert(0 && "unknown load or store"); return 0;
660 case Alpha::LDQ: return Alpha::LDQ_SYM;
661 case Alpha::LDS: return Alpha::LDS_SYM;
662 case Alpha::LDT: return Alpha::LDT_SYM;
663 case Alpha::LDL: return Alpha::LDL_SYM;
664 case Alpha::LDBU: return Alpha::LDBU_SYM;
665 case Alpha::LDWU: return Alpha::LDWU_SYM;
666 case Alpha::LDW: return Alpha::LDW_SYM;
667 case Alpha::LDB: return Alpha::LDB_SYM;
668 case Alpha::STQ: return Alpha::STQ_SYM;
669 case Alpha::STS: return Alpha::STS_SYM;
670 case Alpha::STT: return Alpha::STT_SYM;
671 case Alpha::STL: return Alpha::STL_SYM;
672 case Alpha::STW: return Alpha::STW_SYM;
673 case Alpha::STB: return Alpha::STB_SYM;
674 }
675}
676
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000677void ISel::MoveFP2Int(unsigned src, unsigned dst, bool isDouble)
678{
679 unsigned Opc;
680 if (EnableAlphaFTOI) {
681 Opc = isDouble ? Alpha::FTOIT : Alpha::FTOIS;
682 BuildMI(BB, Opc, 1, dst).addReg(src);
683 } else {
684 //The hard way:
685 // Spill the integer to memory and reload it from there.
686 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
687 MachineFunction *F = BB->getParent();
688 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
689
690 Opc = isDouble ? Alpha::STT : Alpha::STS;
691 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
692 Opc = isDouble ? Alpha::LDQ : Alpha::LDL;
693 BuildMI(BB, Alpha::LDQ, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
694 }
695}
696
697void ISel::MoveInt2FP(unsigned src, unsigned dst, bool isDouble)
698{
699 unsigned Opc;
700 if (EnableAlphaFTOI) {
701 Opc = isDouble?Alpha::ITOFT:Alpha::ITOFS;
702 BuildMI(BB, Opc, 1, dst).addReg(src);
703 } else {
704 //The hard way:
705 // Spill the integer to memory and reload it from there.
706 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
707 MachineFunction *F = BB->getParent();
708 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
709
710 Opc = isDouble ? Alpha::STQ : Alpha::STL;
711 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
712 Opc = isDouble ? Alpha::LDT : Alpha::LDS;
713 BuildMI(BB, Opc, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
714 }
715}
716
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000717bool ISel::SelectFPSetCC(SDOperand N, unsigned dst)
718{
719 SDNode *Node = N.Val;
720 unsigned Opc, Tmp1, Tmp2, Tmp3;
721 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node);
722
723 //assert(SetCC->getOperand(0).getValueType() != MVT::f32 && "SetCC f32 should have been promoted");
724 bool rev = false;
725 bool inv = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000726
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000727 switch (SetCC->getCondition()) {
728 default: Node->dump(); assert(0 && "Unknown FP comparison!");
729 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
730 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
731 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
732 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
733 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
734 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
735 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000736
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000737 //FIXME: check for constant 0.0
738 ConstantFPSDNode *CN;
739 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
740 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
741 Tmp1 = Alpha::F31;
742 else
743 Tmp1 = SelectExpr(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000744
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000745 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
746 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
747 Tmp2 = Alpha::F31;
748 else
Chris Lattner9c9183a2005-04-30 04:44:07 +0000749 Tmp2 = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000750
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000751 //Can only compare doubles, and dag won't promote for me
752 if (SetCC->getOperand(0).getValueType() == MVT::f32)
753 {
754 //assert(0 && "Setcc On float?\n");
755 std::cerr << "Setcc on float!\n";
756 Tmp3 = MakeReg(MVT::f64);
757 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
758 Tmp1 = Tmp3;
759 }
760 if (SetCC->getOperand(1).getValueType() == MVT::f32)
761 {
762 //assert (0 && "Setcc On float?\n");
763 std::cerr << "Setcc on float!\n";
764 Tmp3 = MakeReg(MVT::f64);
765 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp2);
766 Tmp2 = Tmp3;
767 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000768
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000769 if (rev) std::swap(Tmp1, Tmp2);
770 //do the comparison
771 BuildMI(BB, Opc, 2, dst).addReg(Tmp1).addReg(Tmp2);
772 return inv;
773}
774
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000775//Check to see if the load is a constant offset from a base register
776void ISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
777{
778 unsigned opcode = N.getOpcode();
779 if (opcode == ISD::ADD) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000780 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000781 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
782 { //Normal imm add
783 Reg = SelectExpr(N.getOperand(0));
784 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
785 return;
786 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000787 else if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000788 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 32767)
789 {
790 Reg = SelectExpr(N.getOperand(1));
791 offset = cast<ConstantSDNode>(N.getOperand(0))->getValue();
792 return;
793 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000794 }
795 Reg = SelectExpr(N);
796 offset = 0;
797 return;
798}
799
Andrew Lenharth445171a2005-02-08 00:40:03 +0000800void ISel::SelectBranchCC(SDOperand N)
801{
802 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000803 MachineBasicBlock *Dest =
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000804 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
805 unsigned Opc = Alpha::WTF;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000806
Andrew Lenharth445171a2005-02-08 00:40:03 +0000807 Select(N.getOperand(0)); //chain
808 SDOperand CC = N.getOperand(1);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000809
Andrew Lenharth445171a2005-02-08 00:40:03 +0000810 if (CC.getOpcode() == ISD::SETCC)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000811 {
812 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
813 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
814 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000815 bool LeftZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
816 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
817 bool RightZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
818 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000819 bool isNE = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000820
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000821 //Fix up CC
822 ISD::CondCode cCode= SetCC->getCondition();
823 if (LeftZero && !RightZero) //Swap Operands
824 cCode = ISD::getSetCCSwappedOperands(cCode);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000825
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000826 if(cCode == ISD::SETNE)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000827 isNE = true;
Andrew Lenharth445171a2005-02-08 00:40:03 +0000828
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000829 if (LeftZero || RightZero) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000830 switch (SetCC->getCondition()) {
831 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
832 case ISD::SETEQ: Opc = Alpha::BEQ; break;
833 case ISD::SETLT: Opc = Alpha::BLT; break;
834 case ISD::SETLE: Opc = Alpha::BLE; break;
835 case ISD::SETGT: Opc = Alpha::BGT; break;
836 case ISD::SETGE: Opc = Alpha::BGE; break;
837 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
838 case ISD::SETUGT: Opc = Alpha::BNE; break;
839 case ISD::SETULE: Opc = Alpha::BEQ; break; //Technically you could have this CC
840 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
841 case ISD::SETNE: Opc = Alpha::BNE; break;
842 }
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000843 unsigned Tmp1;
844 if(LeftZero && !RightZero) //swap Operands
845 Tmp1 = SelectExpr(SetCC->getOperand(1)); //Cond
846 else
847 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000848 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
849 return;
850 } else {
851 unsigned Tmp1 = SelectExpr(CC);
852 if (isNE)
853 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
854 else
855 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000856 return;
857 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000858 } else { //FP
859 //Any comparison between 2 values should be codegened as an folded branch, as moving
860 //CC to the integer register is very expensive
861 //for a cmp b: c = a - b;
862 //a = b: c = 0
863 //a < b: c < 0
864 //a > b: c > 0
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000865
866 bool invTest = false;
867 unsigned Tmp3;
868
869 ConstantFPSDNode *CN;
870 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
871 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
872 Tmp3 = SelectExpr(SetCC->getOperand(0));
873 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
874 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
875 {
876 Tmp3 = SelectExpr(SetCC->getOperand(1));
877 invTest = true;
878 }
879 else
880 {
881 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
882 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
883 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
884 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
885 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
886 .addReg(Tmp1).addReg(Tmp2);
887 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000888
889 switch (SetCC->getCondition()) {
890 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000891 case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
892 case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
893 case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
894 case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
895 case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
896 case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000897 }
898 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000899 return;
900 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000901 abort(); //Should never be reached
902 } else {
903 //Giveup and do the stupid thing
904 unsigned Tmp1 = SelectExpr(CC);
905 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
906 return;
907 }
Andrew Lenharth445171a2005-02-08 00:40:03 +0000908 abort(); //Should never be reached
909}
910
Andrew Lenharth40831c52005-01-28 06:57:18 +0000911unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
912{
913 unsigned Tmp1, Tmp2, Tmp3;
914 unsigned Opc = 0;
915 SDNode *Node = N.Val;
916 MVT::ValueType DestType = N.getValueType();
917 unsigned opcode = N.getOpcode();
918
919 switch (opcode) {
920 default:
921 Node->dump();
922 assert(0 && "Node not handled!\n");
Andrew Lenharth2c594352005-01-29 15:42:07 +0000923
Andrew Lenharth7332f3e2005-04-02 19:11:07 +0000924 case ISD::UNDEF: {
925 BuildMI(BB, Alpha::IDEF, 0, Result);
926 return Result;
927 }
928
Andrew Lenharth30b46d42005-04-02 19:04:58 +0000929 case ISD::FNEG:
930 if(ISD::FABS == N.getOperand(0).getOpcode())
931 {
Misha Brukman7847fca2005-04-22 17:54:37 +0000932 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
933 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +0000934 } else {
Misha Brukman7847fca2005-04-22 17:54:37 +0000935 Tmp1 = SelectExpr(N.getOperand(0));
936 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +0000937 }
938 return Result;
939
940 case ISD::FABS:
941 Tmp1 = SelectExpr(N.getOperand(0));
942 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
943 return Result;
944
Andrew Lenharth9818c052005-02-05 13:19:12 +0000945 case ISD::SELECT:
946 {
Andrew Lenharth45859692005-03-03 21:47:53 +0000947 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
948 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
949 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
950
951 SDOperand CC = N.getOperand(0);
952 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
953
Misha Brukman4633f1c2005-04-21 23:13:11 +0000954 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth45859692005-03-03 21:47:53 +0000955 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
956 { //FP Setcc -> Select yay!
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000957
958
Andrew Lenharth45859692005-03-03 21:47:53 +0000959 //for a cmp b: c = a - b;
960 //a = b: c = 0
961 //a < b: c < 0
962 //a > b: c > 0
Misha Brukman4633f1c2005-04-21 23:13:11 +0000963
Andrew Lenharth45859692005-03-03 21:47:53 +0000964 bool invTest = false;
965 unsigned Tmp3;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000966
Andrew Lenharth45859692005-03-03 21:47:53 +0000967 ConstantFPSDNode *CN;
968 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
969 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
970 Tmp3 = SelectExpr(SetCC->getOperand(0));
971 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
972 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
973 {
974 Tmp3 = SelectExpr(SetCC->getOperand(1));
975 invTest = true;
976 }
977 else
978 {
979 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
980 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
981 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
982 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
983 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
984 .addReg(Tmp1).addReg(Tmp2);
985 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000986
Andrew Lenharth45859692005-03-03 21:47:53 +0000987 switch (SetCC->getCondition()) {
988 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
989 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNE : Alpha::FCMOVEQ; break;
990 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGT : Alpha::FCMOVLT; break;
991 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGE : Alpha::FCMOVLE; break;
992 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLT : Alpha::FCMOVGT; break;
993 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLE : Alpha::FCMOVGE; break;
994 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQ : Alpha::FCMOVNE; break;
995 }
Andrew Lenharth33819132005-03-04 20:09:23 +0000996 BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
Andrew Lenharth45859692005-03-03 21:47:53 +0000997 return Result;
998 }
999 else
1000 {
1001 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001002 BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV).addReg(Tmp1);
1003// // Spill the cond to memory and reload it from there.
1004// unsigned Tmp4 = MakeReg(MVT::f64);
1005// MoveIntFP(Tmp1, Tmp4, true);
1006// //now ideally, we don't have to do anything to the flag...
1007// // Get the condition into the zero flag.
1008// BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
Andrew Lenharth45859692005-03-03 21:47:53 +00001009 return Result;
1010 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001011 }
1012
Andrew Lenharthc1faced2005-02-01 01:37:24 +00001013 case ISD::FP_ROUND:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001014 assert (DestType == MVT::f32 &&
1015 N.getOperand(0).getValueType() == MVT::f64 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001016 "only f64 to f32 conversion supported here");
Andrew Lenharthc1faced2005-02-01 01:37:24 +00001017 Tmp1 = SelectExpr(N.getOperand(0));
1018 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
1019 return Result;
1020
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001021 case ISD::FP_EXTEND:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001022 assert (DestType == MVT::f64 &&
1023 N.getOperand(0).getValueType() == MVT::f32 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001024 "only f32 to f64 conversion supported here");
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001025 Tmp1 = SelectExpr(N.getOperand(0));
1026 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
1027 return Result;
1028
Andrew Lenharth2c594352005-01-29 15:42:07 +00001029 case ISD::CopyFromReg:
1030 {
1031 // Make sure we generate both values.
1032 if (Result != notIn)
1033 ExprMap[N.getValue(1)] = notIn; // Generate the token
1034 else
1035 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001036
Andrew Lenharth2c594352005-01-29 15:42:07 +00001037 SDOperand Chain = N.getOperand(0);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001038
Andrew Lenharth2c594352005-01-29 15:42:07 +00001039 Select(Chain);
1040 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1041 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1042 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
1043 return Result;
1044 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001045
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001046 case ISD::LOAD:
1047 {
1048 // Make sure we generate both values.
1049 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001050 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001051 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001052 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001053
Andrew Lenharth29219162005-02-07 06:31:44 +00001054 DestType = N.getValue(0).getValueType();
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001055
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001056 SDOperand Chain = N.getOperand(0);
1057 SDOperand Address = N.getOperand(1);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001058 Select(Chain);
Andrew Lenharth65838902005-02-06 16:22:15 +00001059 Opc = DestType == MVT::f64 ? Alpha::LDT : Alpha::LDS;
1060
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001061 if (Address.getOpcode() == ISD::GlobalAddress) {
1062 AlphaLowering.restoreGP(BB);
1063 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001064 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001065 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1066 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001067 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001068 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001069 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001070 has_sym = true;
Andrew Lenharth97127a12005-02-05 17:41:39 +00001071 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001072 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001073 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001074 BuildMI(BB, Opc, 2, Result)
1075 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1076 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001077 } else {
1078 long offset;
1079 SelectAddr(Address, Tmp1, offset);
1080 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1081 }
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001082 return Result;
1083 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001084 case ISD::ConstantFP:
1085 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
1086 if (CN->isExactlyValue(+0.0)) {
1087 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001088 } else if ( CN->isExactlyValue(-0.0)) {
1089 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001090 } else {
1091 abort();
1092 }
1093 }
1094 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001095
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001096 case ISD::SDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001097 case ISD::MUL:
1098 case ISD::ADD:
1099 case ISD::SUB:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001100 switch( opcode ) {
1101 case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS; break;
1102 case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS; break;
1103 case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS; break;
1104 case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS; break;
1105 };
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001106
1107 ConstantFPSDNode *CN;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001108 if (opcode == ISD::SUB
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001109 && (CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
1110 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1111 {
1112 Tmp2 = SelectExpr(N.getOperand(1));
1113 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp2).addReg(Tmp2);
1114 } else {
1115 Tmp1 = SelectExpr(N.getOperand(0));
1116 Tmp2 = SelectExpr(N.getOperand(1));
1117 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1118 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001119 return Result;
1120
Andrew Lenharth2c594352005-01-29 15:42:07 +00001121 case ISD::EXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001122 {
1123 //include a conversion sequence for float loads to double
1124 if (Result != notIn)
1125 ExprMap[N.getValue(1)] = notIn; // Generate the token
1126 else
1127 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001128
Andrew Lenhartha549deb2005-02-07 05:33:15 +00001129 Tmp1 = MakeReg(MVT::f32);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001130
1131 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001132 "EXTLOAD not from f32");
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001133 assert(Node->getValueType(0) == MVT::f64 && "EXTLOAD not to f64");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001134
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001135 SDOperand Chain = N.getOperand(0);
1136 SDOperand Address = N.getOperand(1);
1137 Select(Chain);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001138
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001139 if (Address.getOpcode() == ISD::GlobalAddress) {
1140 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001141 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001142 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1143 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001144 else if (ConstantPoolSDNode *CP =
1145 dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001146 {
1147 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001148 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001149 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addConstantPoolIndex(CP->getIndex());
1150 }
1151 else if(Address.getOpcode() == ISD::FrameIndex) {
1152 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
Andrew Lenharth032f2352005-02-22 21:59:48 +00001153 BuildMI(BB, Alpha::LDS, 2, Tmp1)
1154 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1155 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001156 } else {
1157 long offset;
1158 SelectAddr(Address, Tmp2, offset);
1159 BuildMI(BB, Alpha::LDS, 1, Tmp1).addImm(offset).addReg(Tmp2);
1160 }
Andrew Lenharth29219162005-02-07 06:31:44 +00001161 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001162 return Result;
1163 }
Andrew Lenharth2c594352005-01-29 15:42:07 +00001164
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001165 case ISD::UINT_TO_FP:
1166 case ISD::SINT_TO_FP:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001167 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001168 assert (N.getOperand(0).getValueType() == MVT::i64
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001169 && "only quads can be loaded from");
Andrew Lenharth40831c52005-01-28 06:57:18 +00001170 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001171 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001172 MoveInt2FP(Tmp1, Tmp2, true);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001173 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
1174 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001175 return Result;
1176 }
1177 }
1178 assert(0 && "should not get here");
1179 return 0;
1180}
1181
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001182unsigned ISel::SelectExpr(SDOperand N) {
1183 unsigned Result;
Andrew Lenharth2966e842005-04-07 18:15:28 +00001184 unsigned Tmp1, Tmp2 = 0, Tmp3;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001185 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001186 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001187
1188 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001189 MVT::ValueType DestType = N.getValueType();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001190
1191 unsigned &Reg = ExprMap[N];
1192 if (Reg) return Reg;
1193
1194 if (N.getOpcode() != ISD::CALL)
1195 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001196 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001197 else {
1198 // If this is a call instruction, make sure to prepare ALL of the result
1199 // values as well as the chain.
1200 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001201 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001202 else {
1203 Result = MakeReg(Node->getValueType(0));
1204 ExprMap[N.getValue(0)] = Result;
1205 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1206 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001207 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001208 }
1209 }
1210
Andrew Lenharth50d91d72005-04-30 14:19:13 +00001211 if ((DestType == MVT::f64 || DestType == MVT::f32 ||
1212 (
1213 (opcode == ISD::LOAD || opcode == ISD::CopyFromReg ||
1214 opcode == ISD::EXTLOAD) &&
1215 (N.getValue(0).getValueType() == MVT::f32 ||
1216 N.getValue(0).getValueType() == MVT::f64)
1217 ))
1218 && opcode != ISD::CALL
Andrew Lenharth06342c32005-02-07 06:21:37 +00001219 )
Andrew Lenharth40831c52005-01-28 06:57:18 +00001220 return SelectExprFP(N, Result);
1221
1222 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001223 default:
1224 Node->dump();
1225 assert(0 && "Node not handled!\n");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001226
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001227 case ISD::CTPOP:
1228 case ISD::CTTZ:
1229 case ISD::CTLZ:
1230 Opc = opcode == ISD::CTPOP ? Alpha::CTPOP :
1231 (opcode == ISD::CTTZ ? Alpha::CTTZ : Alpha::CTLZ);
1232 Tmp1 = SelectExpr(N.getOperand(0));
1233 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1234 return Result;
1235
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001236 case ISD::MULHU:
1237 Tmp1 = SelectExpr(N.getOperand(0));
1238 Tmp2 = SelectExpr(N.getOperand(1));
1239 BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth706be912005-04-07 13:55:53 +00001240 return Result;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001241 case ISD::MULHS:
1242 {
1243 //MULHU - Ra<63>*Rb - Rb<63>*Ra
1244 Tmp1 = SelectExpr(N.getOperand(0));
1245 Tmp2 = SelectExpr(N.getOperand(1));
1246 Tmp3 = MakeReg(MVT::i64);
1247 BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1248 unsigned V1 = MakeReg(MVT::i64);
1249 unsigned V2 = MakeReg(MVT::i64);
1250 BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31).addReg(Tmp1);
1251 BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31).addReg(Tmp2);
1252 unsigned IRes = MakeReg(MVT::i64);
1253 BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
1254 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
1255 return Result;
1256 }
Andrew Lenharth7332f3e2005-04-02 19:11:07 +00001257 case ISD::UNDEF: {
1258 BuildMI(BB, Alpha::IDEF, 0, Result);
1259 return Result;
1260 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001261
Andrew Lenharth032f2352005-02-22 21:59:48 +00001262 case ISD::DYNAMIC_STACKALLOC:
1263 // Generate both result values.
Andrew Lenharth3a7118d2005-02-23 17:33:42 +00001264 if (Result != notIn)
1265 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth032f2352005-02-22 21:59:48 +00001266 else
1267 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1268
1269 // FIXME: We are currently ignoring the requested alignment for handling
1270 // greater than the stack alignment. This will need to be revisited at some
1271 // point. Align = N.getOperand(2);
1272
1273 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1274 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1275 std::cerr << "Cannot allocate stack object with greater alignment than"
1276 << " the stack alignment yet!";
1277 abort();
1278 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001279
Andrew Lenharth032f2352005-02-22 21:59:48 +00001280 Select(N.getOperand(0));
1281 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1282 {
1283 if (CN->getValue() < 32000)
1284 {
1285 BuildMI(BB, Alpha::LDA, 2, Alpha::R30)
1286 .addImm(-CN->getValue()).addReg(Alpha::R30);
1287 } else {
1288 Tmp1 = SelectExpr(N.getOperand(1));
1289 // Subtract size from stack pointer, thereby allocating some space.
1290 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1291 }
1292 } else {
1293 Tmp1 = SelectExpr(N.getOperand(1));
1294 // Subtract size from stack pointer, thereby allocating some space.
1295 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1296 }
1297
1298 // Put a pointer to the space into the result register, by copying the stack
1299 // pointer.
Andrew Lenharth7bc47022005-02-22 23:29:25 +00001300 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
Andrew Lenharth032f2352005-02-22 21:59:48 +00001301 return Result;
1302
Andrew Lenharth33819132005-03-04 20:09:23 +00001303// case ISD::ConstantPool:
1304// Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1305// AlphaLowering.restoreGP(BB);
1306// BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(Tmp1);
1307// return Result;
Andrew Lenharth2c594352005-01-29 15:42:07 +00001308
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001309 case ISD::FrameIndex:
Andrew Lenharth032f2352005-02-22 21:59:48 +00001310 BuildMI(BB, Alpha::LDA, 2, Result)
1311 .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
1312 .addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001313 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001314
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001315 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001316 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001317 case ISD::SEXTLOAD:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001318 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001319 {
1320 // Make sure we generate both values.
1321 if (Result != notIn)
1322 ExprMap[N.getValue(1)] = notIn; // Generate the token
1323 else
1324 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001325
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001326 SDOperand Chain = N.getOperand(0);
1327 SDOperand Address = N.getOperand(1);
1328 Select(Chain);
1329
Misha Brukman4633f1c2005-04-21 23:13:11 +00001330 assert(Node->getValueType(0) == MVT::i64 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001331 "Unknown type to sign extend to.");
Andrew Lenharth03824012005-02-07 05:55:55 +00001332 if (opcode == ISD::LOAD)
1333 Opc = Alpha::LDQ;
1334 else
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001335 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
1336 default: Node->dump(); assert(0 && "Bad sign extend!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001337 case MVT::i32: Opc = Alpha::LDL;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001338 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001339 case MVT::i16: Opc = Alpha::LDWU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001340 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001341 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Misha Brukman4633f1c2005-04-21 23:13:11 +00001342 case MVT::i8: Opc = Alpha::LDBU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001343 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001344 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001345
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001346 if (Address.getOpcode() == ISD::GlobalAddress) {
1347 AlphaLowering.restoreGP(BB);
1348 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001349 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001350 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1351 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001352 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1353 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001354 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001355 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001356 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001357 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001358 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001359 BuildMI(BB, Opc, 2, Result)
1360 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1361 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001362 } else {
1363 long offset;
1364 SelectAddr(Address, Tmp1, offset);
1365 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1366 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001367 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001368 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001369
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001370 case ISD::GlobalAddress:
1371 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001372 has_sym = true;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001373 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
1374 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
1375 return Result;
1376
1377 case ISD::CALL:
1378 {
1379 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001380
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001381 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001382 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001383
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001384 //grab the arguments
1385 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001386 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001387 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001388 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001389
Andrew Lenharth684f2292005-01-30 00:35:27 +00001390 //in reg args
1391 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001392 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001393 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001394 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +00001395 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001396 Alpha::F19, Alpha::F20, Alpha::F21};
1397 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001398 default:
1399 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001400 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001401 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001402 N.getOperand(i+2).getValueType() << "\n";
1403 assert(0 && "Unknown value type for call");
1404 case MVT::i1:
1405 case MVT::i8:
1406 case MVT::i16:
1407 case MVT::i32:
1408 case MVT::i64:
1409 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1410 break;
1411 case MVT::f32:
1412 case MVT::f64:
1413 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1414 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001415 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001416 }
Andrew Lenharth684f2292005-01-30 00:35:27 +00001417 //in mem args
1418 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001419 {
1420 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001421 default:
1422 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001423 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001424 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001425 N.getOperand(i+2).getValueType() << "\n";
1426 assert(0 && "Unknown value type for call");
1427 case MVT::i1:
1428 case MVT::i8:
1429 case MVT::i16:
1430 case MVT::i32:
1431 case MVT::i64:
1432 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1433 break;
1434 case MVT::f32:
1435 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1436 break;
1437 case MVT::f64:
1438 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1439 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001440 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001441 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001442 //build the right kind of call
1443 if (GlobalAddressSDNode *GASD =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001444 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001445 {
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001446 if (GASD->getGlobal()->isExternal()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001447 //use safe calling convention
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001448 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001449 has_sym = true;
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001450 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal());
1451 } else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001452 //use PC relative branch call
Andrew Lenharth1e0d9bd2005-04-14 17:34:20 +00001453 AlphaLowering.restoreGP(BB);
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001454 BuildMI(BB, Alpha::BSR, 1, Alpha::R26).addGlobalAddress(GASD->getGlobal(),true);
1455 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001456 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001457 else if (ExternalSymbolSDNode *ESSDN =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001458 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001459 {
1460 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001461 has_sym = true;
Andrew Lenharthba05ad62005-03-30 18:22:52 +00001462 BuildMI(BB, Alpha::CALL, 1).addExternalSymbol(ESSDN->getSymbol(), true);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001463 } else {
1464 //no need to restore GP as we are doing an indirect call
1465 Tmp1 = SelectExpr(N.getOperand(1));
1466 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
1467 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
1468 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001469
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001470 //push the result into a virtual register
Misha Brukman4633f1c2005-04-21 23:13:11 +00001471
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001472 switch (Node->getValueType(0)) {
1473 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001474 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001475 case MVT::i1:
1476 case MVT::i8:
1477 case MVT::i16:
1478 case MVT::i32:
1479 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001480 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
1481 break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001482 case MVT::f32:
1483 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001484 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
1485 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001486 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001487 return Result+N.ResNo;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001488 }
1489
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001490 case ISD::SIGN_EXTEND_INREG:
1491 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001492 //do SDIV opt for all levels of ints if not dividing by a constant
1493 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
1494 && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001495 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001496 unsigned Tmp4 = MakeReg(MVT::f64);
1497 unsigned Tmp5 = MakeReg(MVT::f64);
1498 unsigned Tmp6 = MakeReg(MVT::f64);
1499 unsigned Tmp7 = MakeReg(MVT::f64);
1500 unsigned Tmp8 = MakeReg(MVT::f64);
1501 unsigned Tmp9 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001502
1503 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1504 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1505 MoveInt2FP(Tmp1, Tmp4, true);
1506 MoveInt2FP(Tmp2, Tmp5, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001507 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Tmp4);
1508 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Tmp5);
1509 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
1510 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Tmp8);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001511 MoveFP2Int(Tmp9, Result, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001512 return Result;
1513 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001514
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001515 //Alpha has instructions for a bunch of signed 32 bit stuff
1516 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001517 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001518 switch (N.getOperand(0).getOpcode()) {
1519 case ISD::ADD:
1520 case ISD::SUB:
1521 case ISD::MUL:
1522 {
1523 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
1524 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
1525 //FIXME: first check for Scaled Adds and Subs!
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001526 ConstantSDNode* CSD = NULL;
1527 if(!isMul && N.getOperand(0).getOperand(0).getOpcode() == ISD::SHL &&
1528 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(0).getOperand(1))) &&
1529 (CSD->getValue() == 2 || CSD->getValue() == 3))
1530 {
1531 bool use4 = CSD->getValue() == 2;
1532 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1533 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1534 BuildMI(BB, isAdd?(use4?Alpha::S4ADDL:Alpha::S8ADDL):(use4?Alpha::S4SUBL:Alpha::S8SUBL),
1535 2,Result).addReg(Tmp1).addReg(Tmp2);
1536 }
1537 else if(isAdd && N.getOperand(0).getOperand(1).getOpcode() == ISD::SHL &&
1538 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1).getOperand(1))) &&
1539 (CSD->getValue() == 2 || CSD->getValue() == 3))
1540 {
1541 bool use4 = CSD->getValue() == 2;
1542 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1543 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1544 BuildMI(BB, use4?Alpha::S4ADDL:Alpha::S8ADDL, 2,Result).addReg(Tmp1).addReg(Tmp2);
1545 }
1546 else if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001547 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
1548 { //Normal imm add/sub
1549 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001550 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001551 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
1552 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001553 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001554 else
1555 { //Normal add/sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001556 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULL : Alpha::SUBL);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001557 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001558 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001559 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1560 }
1561 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001562 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001563 default: break; //Fall Though;
1564 }
1565 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001566 Tmp1 = SelectExpr(N.getOperand(0));
1567 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001568 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001569 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001570 {
1571 default:
1572 Node->dump();
1573 assert(0 && "Sign Extend InReg not there yet");
1574 break;
1575 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001576 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001577 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001578 break;
1579 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001580 case MVT::i16:
1581 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
1582 break;
1583 case MVT::i8:
1584 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
1585 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +00001586 case MVT::i1:
1587 Tmp2 = MakeReg(MVT::i64);
1588 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001589 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +00001590 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001591 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001592 return Result;
1593 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001594
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001595 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001596 {
1597 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1598 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1599 bool isConst1 = false;
1600 bool isConst2 = false;
1601 int dir;
Misha Brukman7847fca2005-04-22 17:54:37 +00001602
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001603 //Tmp1 = SelectExpr(N.getOperand(0));
1604 if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001605 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
1606 isConst1 = true;
1607 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001608 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1609 isConst2 = true;
1610
1611 switch (SetCC->getCondition()) {
1612 default: Node->dump(); assert(0 && "Unknown integer comparison!");
1613 case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001614 case ISD::SETLT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001615 Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001616 case ISD::SETLE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001617 Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001618 case ISD::SETGT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001619 Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001620 case ISD::SETGE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001621 Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001622 case ISD::SETULT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001623 Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001624 case ISD::SETUGT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001625 Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001626 case ISD::SETULE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001627 Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001628 case ISD::SETUGE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001629 Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001630 case ISD::SETNE: {//Handle this one special
1631 //std::cerr << "Alpha does not have a setne.\n";
1632 //abort();
1633 Tmp1 = SelectExpr(N.getOperand(0));
1634 Tmp2 = SelectExpr(N.getOperand(1));
1635 Tmp3 = MakeReg(MVT::i64);
1636 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001637 //Remeber we have the Inv for this CC
1638 CCInvMap[N] = Tmp3;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001639 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001640 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001641 return Result;
1642 }
1643 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001644 if (dir == 1) {
1645 Tmp1 = SelectExpr(N.getOperand(0));
1646 if (isConst2) {
1647 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1648 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1649 } else {
1650 Tmp2 = SelectExpr(N.getOperand(1));
1651 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1652 }
1653 } else if (dir == 2) {
1654 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001655 if (isConst1) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001656 Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1657 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1658 } else {
1659 Tmp2 = SelectExpr(N.getOperand(0));
1660 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1661 }
1662 } else { //dir == 0
1663 if (isConst1) {
1664 Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1665 Tmp2 = SelectExpr(N.getOperand(1));
1666 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
1667 } else if (isConst2) {
1668 Tmp1 = SelectExpr(N.getOperand(0));
1669 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1670 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
1671 } else {
1672 Tmp1 = SelectExpr(N.getOperand(0));
1673 Tmp2 = SelectExpr(N.getOperand(1));
1674 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1675 }
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001676 }
1677 } else {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001678 //do the comparison
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001679 Tmp1 = MakeReg(MVT::f64);
1680 bool inv = SelectFPSetCC(N, Tmp1);
1681
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001682 //now arrange for Result (int) to have a 1 or 0
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001683 Tmp2 = MakeReg(MVT::i64);
1684 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001685 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001686 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001687 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001688 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001689 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001690 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001691
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001692 case ISD::CopyFromReg:
1693 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001694 ++count_ins;
1695
Andrew Lenharth40831c52005-01-28 06:57:18 +00001696 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001697 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001698 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001699 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001700 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001701
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001702 SDOperand Chain = N.getOperand(0);
1703
1704 Select(Chain);
1705 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1706 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1707 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1708 return Result;
1709 }
1710
Misha Brukman4633f1c2005-04-21 23:13:11 +00001711 //Most of the plain arithmetic and logic share the same form, and the same
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001712 //constant immediate test
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001713 case ISD::XOR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001714 //Match Not
1715 if (N.getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001716 cast<ConstantSDNode>(N.getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001717 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001718 Tmp1 = SelectExpr(N.getOperand(0));
1719 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1720 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001721 }
1722 //Fall through
1723 case ISD::AND:
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001724 //handle zap
1725 if (opcode == ISD::AND && N.getOperand(1).getOpcode() == ISD::Constant)
1726 {
1727 uint64_t k = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1728 unsigned int build = 0;
1729 for(int i = 0; i < 8; ++i)
1730 {
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001731 if ((k & 0x00FF) == 0x00FF)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001732 build |= 1 << i;
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001733 else if ((k & 0x00FF) != 0)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001734 { build = 0; break; }
1735 k >>= 8;
1736 }
1737 if (build)
1738 {
1739 Tmp1 = SelectExpr(N.getOperand(0));
1740 BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1741 return Result;
1742 }
1743 }
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001744 case ISD::OR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001745 //Check operand(0) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001746 if (N.getOperand(0).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001747 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001748 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001749 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001750 switch(opcode) {
1751 case ISD::AND: Opc = Alpha::BIC; break;
1752 case ISD::OR: Opc = Alpha::ORNOT; break;
1753 case ISD::XOR: Opc = Alpha::EQV; break;
1754 }
1755 Tmp1 = SelectExpr(N.getOperand(1));
1756 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1757 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1758 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001759 }
1760 //Check operand(1) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001761 if (N.getOperand(1).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001762 N.getOperand(1).getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001763 cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001764 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001765 switch(opcode) {
1766 case ISD::AND: Opc = Alpha::BIC; break;
1767 case ISD::OR: Opc = Alpha::ORNOT; break;
1768 case ISD::XOR: Opc = Alpha::EQV; break;
1769 }
1770 Tmp1 = SelectExpr(N.getOperand(0));
1771 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1772 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1773 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001774 }
1775 //Fall through
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001776 case ISD::SHL:
1777 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001778 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001779 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001780 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1781 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001782 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001783 {
1784 switch(opcode) {
1785 case ISD::AND: Opc = Alpha::ANDi; break;
1786 case ISD::OR: Opc = Alpha::BISi; break;
1787 case ISD::XOR: Opc = Alpha::XORi; break;
1788 case ISD::SHL: Opc = Alpha::SLi; break;
1789 case ISD::SRL: Opc = Alpha::SRLi; break;
1790 case ISD::SRA: Opc = Alpha::SRAi; break;
1791 case ISD::MUL: Opc = Alpha::MULQi; break;
1792 };
1793 Tmp1 = SelectExpr(N.getOperand(0));
1794 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1795 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1796 } else {
1797 switch(opcode) {
1798 case ISD::AND: Opc = Alpha::AND; break;
1799 case ISD::OR: Opc = Alpha::BIS; break;
1800 case ISD::XOR: Opc = Alpha::XOR; break;
1801 case ISD::SHL: Opc = Alpha::SL; break;
1802 case ISD::SRL: Opc = Alpha::SRL; break;
1803 case ISD::SRA: Opc = Alpha::SRA; break;
1804 case ISD::MUL: Opc = Alpha::MULQ; break;
1805 };
1806 Tmp1 = SelectExpr(N.getOperand(0));
1807 Tmp2 = SelectExpr(N.getOperand(1));
1808 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1809 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001810 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001811
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001812 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001813 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001814 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001815 bool isAdd = opcode == ISD::ADD;
1816
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001817 //first check for Scaled Adds and Subs!
1818 //Valid for add and sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001819 ConstantSDNode* CSD = NULL;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001820 if(N.getOperand(0).getOpcode() == ISD::SHL &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001821 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
1822 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001823 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001824 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001825 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001826 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) && CSD->getValue() <= 255)
1827 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1828 2, Result).addReg(Tmp2).addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001829 else {
1830 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001831 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1832 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001833 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001834 }
1835 //Position prevents subs
Andrew Lenharth273a1f92005-04-07 14:18:13 +00001836 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001837 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) &&
1838 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001839 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001840 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001841 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001842 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(0))) && CSD->getValue() <= 255)
1843 BuildMI(BB, use4?Alpha::S4ADDQi:Alpha::S8ADDQi, 2, Result).addReg(Tmp2)
1844 .addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001845 else {
1846 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001847 BuildMI(BB, use4?Alpha::S4ADDQ:Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001848 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001849 }
1850 //small addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001851 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
1852 CSD->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001853 { //Normal imm add/sub
1854 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1855 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001856 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CSD->getValue());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001857 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001858 //larger addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001859 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
1860 CSD->getSignExtended() <= 32767 &&
1861 CSD->getSignExtended() >= -32767)
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001862 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001863 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001864 Tmp2 = (long)CSD->getSignExtended();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001865 if (!isAdd)
1866 Tmp2 = -Tmp2;
1867 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001868 }
1869 //give up and do the operation
1870 else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001871 //Normal add/sub
1872 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1873 Tmp1 = SelectExpr(N.getOperand(0));
1874 Tmp2 = SelectExpr(N.getOperand(1));
1875 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1876 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001877 return Result;
1878 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001879
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001880 case ISD::SDIV:
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001881 {
Andrew Lenhartha565c272005-04-06 22:03:13 +00001882 ConstantSDNode* CSD;
1883 //check if we can convert into a shift!
1884 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
1885 (int64_t)CSD->getSignExtended() != 0 &&
1886 ExactLog2(abs((int64_t)CSD->getSignExtended())) != 0)
1887 {
1888 unsigned k = ExactLog2(abs(CSD->getSignExtended()));
1889 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenhartha565c272005-04-06 22:03:13 +00001890 if (k == 1)
1891 Tmp2 = Tmp1;
1892 else
1893 {
1894 Tmp2 = MakeReg(MVT::i64);
1895 BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
1896 }
1897 Tmp3 = MakeReg(MVT::i64);
1898 BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
1899 unsigned Tmp4 = MakeReg(MVT::i64);
1900 BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
1901 if ((int64_t)CSD->getSignExtended() > 0)
1902 BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
1903 else
1904 {
1905 unsigned Tmp5 = MakeReg(MVT::i64);
1906 BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
1907 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
1908 }
1909 return Result;
1910 }
1911 }
1912 //Else fall through
1913
1914 case ISD::UDIV:
1915 {
1916 ConstantSDNode* CSD;
1917 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
1918 ((int64_t)CSD->getSignExtended() >= 2 ||
1919 (int64_t)CSD->getSignExtended() <= -2))
1920 {
1921 // If this is a divide by constant, we can emit code using some magic
1922 // constants to implement it as a multiply instead.
1923 ExprMap.erase(N);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001924 if (opcode == ISD::SDIV)
Andrew Lenhartha565c272005-04-06 22:03:13 +00001925 return SelectExpr(BuildSDIVSequence(N));
1926 else
1927 return SelectExpr(BuildUDIVSequence(N));
1928 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001929 }
1930 //else fall though
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001931 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +00001932 case ISD::SREM:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001933 //FIXME: alpha really doesn't support any of these operations,
Andrew Lenharth40831c52005-01-28 06:57:18 +00001934 // the ops are expanded into special library calls with
1935 // special calling conventions
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001936 //Restore GP because it is a call after all...
Andrew Lenharth40831c52005-01-28 06:57:18 +00001937 switch(opcode) {
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001938 case ISD::UREM: Opc = Alpha::REMQU; break;
1939 case ISD::SREM: Opc = Alpha::REMQ; break;
1940 case ISD::UDIV: Opc = Alpha::DIVQU; break;
1941 case ISD::SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001942 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001943 Tmp1 = SelectExpr(N.getOperand(0));
1944 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth33819132005-03-04 20:09:23 +00001945 //set up regs explicitly (helps Reg alloc)
1946 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001947 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001948 AlphaLowering.restoreGP(BB);
Andrew Lenharth33819132005-03-04 20:09:23 +00001949 BuildMI(BB, Opc, 2).addReg(Alpha::R24).addReg(Alpha::R25);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001950 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001951 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001952
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001953 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001954 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001955 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001956 assert (DestType == MVT::i64 && "only quads can be loaded to");
1957 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00001958 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001959 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001960 if (SrcType == MVT::f32)
Misha Brukman7847fca2005-04-22 17:54:37 +00001961 {
1962 Tmp2 = MakeReg(MVT::f64);
1963 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
1964 Tmp1 = Tmp2;
1965 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001966 Tmp2 = MakeReg(MVT::f64);
1967 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001968 MoveFP2Int(Tmp2, Result, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001969
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001970 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001971 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001972
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001973 case ISD::SELECT:
1974 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001975 //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 +00001976 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001977 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1978 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001979 // Get the condition into the zero flag.
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001980 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001981
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001982 SDOperand CC = N.getOperand(0);
1983 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
1984
Misha Brukman4633f1c2005-04-21 23:13:11 +00001985 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001986 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
1987 { //FP Setcc -> Int Select
Misha Brukman7847fca2005-04-22 17:54:37 +00001988 Tmp1 = MakeReg(MVT::f64);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001989 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1990 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Misha Brukman7847fca2005-04-22 17:54:37 +00001991 bool inv = SelectFPSetCC(CC, Tmp1);
1992 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
1993 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
1994 return Result;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001995 }
1996 if (CC.getOpcode() == ISD::SETCC) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001997 //Int SetCC -> Select
1998 //Dropping the CC is only useful if we are comparing to 0
1999 if((SetCC->getOperand(1).getOpcode() == ISD::Constant &&
2000 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0) ||
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002001 (SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2002 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0))
2003 {
2004 //figure out a few things
2005 bool LeftZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2006 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
2007 bool RightZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2008 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
2009 bool LeftConst = N.getOperand(1).getOpcode() == ISD::Constant &&
2010 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255;
2011 bool RightConst = N.getOperand(2).getOpcode() == ISD::Constant &&
2012 cast<ConstantSDNode>(N.getOperand(2))->getValue() <= 255;
2013 bool useImm = LeftConst || RightConst;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002014
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002015 //Fix up CC
2016 ISD::CondCode cCode= SetCC->getCondition();
2017 if (RightConst && !LeftConst) //Invert sense to get Imm field right
2018 cCode = ISD::getSetCCInverse(cCode, true);
2019 if (LeftZero && !RightZero) //Swap Operands
2020 cCode = ISD::getSetCCSwappedOperands(cCode);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002021
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002022 //Choose the CMOV
2023 switch (cCode) {
2024 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
2025 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
2026 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
2027 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
2028 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
2029 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
2030 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
2031 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2032 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break; //Technically you could have this CC
2033 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
2034 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2035 }
2036 if(LeftZero && !RightZero) //swap Operands
2037 Tmp1 = SelectExpr(SetCC->getOperand(1)); //Cond
2038 else
2039 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
2040
2041 if (LeftConst) {
2042 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2043 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
Misha Brukman7847fca2005-04-22 17:54:37 +00002044 .addImm(cast<ConstantSDNode>(N.getOperand(1))->getValue())
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002045 .addReg(Tmp1);
2046 } else if (RightConst) {
2047 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
2048 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
Misha Brukman7847fca2005-04-22 17:54:37 +00002049 .addImm(cast<ConstantSDNode>(N.getOperand(2))->getValue())
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002050 .addReg(Tmp1);
2051 } else {
2052 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2053 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2054 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
2055 }
2056 return Result;
2057 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002058 //Otherwise, fall though
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002059 }
2060 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002061 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2062 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthe76797c2005-02-01 20:40:27 +00002063 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002064
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002065 return Result;
2066 }
2067
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002068 case ISD::Constant:
2069 {
Andrew Lenharthc0513832005-03-29 19:24:04 +00002070 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002071 if (val <= IMM_HIGH && val >= IMM_LOW) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002072 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002073 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002074 else if (val <= (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT &&
2075 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
2076 Tmp1 = MakeReg(MVT::i64);
2077 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val)).addReg(Alpha::R31);
2078 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002079 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002080 else {
2081 MachineConstantPool *CP = BB->getParent()->getConstantPool();
2082 ConstantUInt *C = ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
2083 unsigned CPI = CP->getConstantPoolIndex(C);
2084 AlphaLowering.restoreGP(BB);
2085 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CPI);
2086 }
2087 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002088 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002089 }
2090
2091 return 0;
2092}
2093
2094void ISel::Select(SDOperand N) {
2095 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00002096 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002097
Nate Begeman85fdeb22005-03-24 04:39:54 +00002098 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002099 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002100
2101 SDNode *Node = N.Val;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002102
Andrew Lenharth760270d2005-02-07 23:02:23 +00002103 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002104
2105 default:
2106 Node->dump(); std::cerr << "\n";
2107 assert(0 && "Node not handled yet!");
2108
2109 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00002110 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002111 return;
2112 }
2113
2114 case ISD::BR: {
2115 MachineBasicBlock *Dest =
2116 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2117
2118 Select(N.getOperand(0));
2119 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
2120 return;
2121 }
2122
2123 case ISD::ImplicitDef:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002124 ++count_ins;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002125 Select(N.getOperand(0));
2126 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
2127 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002128
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002129 case ISD::EntryToken: return; // Noop
2130
2131 case ISD::TokenFactor:
2132 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2133 Select(Node->getOperand(i));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002134
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002135 //N.Val->dump(); std::cerr << "\n";
2136 //assert(0 && "Node not handled yet!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00002137
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002138 return;
2139
2140 case ISD::CopyToReg:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002141 ++count_outs;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002142 Select(N.getOperand(0));
2143 Tmp1 = SelectExpr(N.getOperand(1));
2144 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002145
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002146 if (Tmp1 != Tmp2) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002147 if (N.getOperand(1).getValueType() == MVT::f64 ||
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002148 N.getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00002149 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2150 else
2151 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002152 }
2153 return;
2154
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002155 case ISD::RET:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002156 ++count_outs;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002157 switch (N.getNumOperands()) {
2158 default:
2159 std::cerr << N.getNumOperands() << "\n";
2160 for (unsigned i = 0; i < N.getNumOperands(); ++i)
2161 std::cerr << N.getOperand(i).getValueType() << "\n";
2162 Node->dump();
2163 assert(0 && "Unknown return instruction!");
2164 case 2:
2165 Select(N.getOperand(0));
2166 Tmp1 = SelectExpr(N.getOperand(1));
2167 switch (N.getOperand(1).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002168 default: Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002169 assert(0 && "All other types should have been promoted!!");
2170 case MVT::f64:
2171 case MVT::f32:
2172 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
2173 break;
2174 case MVT::i32:
2175 case MVT::i64:
2176 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
2177 break;
2178 }
2179 break;
2180 case 1:
2181 Select(N.getOperand(0));
2182 break;
2183 }
2184 //Tmp2 = AlphaLowering.getRetAddr();
2185 //BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(Tmp2).addReg(Tmp2);
2186 BuildMI(BB, Alpha::RETURN, 0); // Just emit a 'ret' instruction
2187 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002188
Misha Brukman4633f1c2005-04-21 23:13:11 +00002189 case ISD::TRUNCSTORE:
2190 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002191 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002192 SDOperand Chain = N.getOperand(0);
2193 SDOperand Value = N.getOperand(1);
2194 SDOperand Address = N.getOperand(2);
2195 Select(Chain);
2196
2197 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00002198
2199 if (opcode == ISD::STORE) {
2200 switch(Value.getValueType()) {
2201 default: assert(0 && "unknown Type in store");
2202 case MVT::i64: Opc = Alpha::STQ; break;
2203 case MVT::f64: Opc = Alpha::STT; break;
2204 case MVT::f32: Opc = Alpha::STS; break;
2205 }
2206 } else { //ISD::TRUNCSTORE
2207 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2208 default: assert(0 && "unknown Type in store");
2209 case MVT::i1: //FIXME: DAG does not promote this load
2210 case MVT::i8: Opc = Alpha::STB; break;
2211 case MVT::i16: Opc = Alpha::STW; break;
2212 case MVT::i32: Opc = Alpha::STL; break;
2213 }
Andrew Lenharth65838902005-02-06 16:22:15 +00002214 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00002215
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002216 if (Address.getOpcode() == ISD::GlobalAddress)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002217 {
2218 AlphaLowering.restoreGP(BB);
2219 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002220 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002221 BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
2222 }
Andrew Lenharth05380342005-02-07 05:07:00 +00002223 else if(Address.getOpcode() == ISD::FrameIndex)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002224 {
Andrew Lenharth032f2352005-02-22 21:59:48 +00002225 BuildMI(BB, Opc, 3).addReg(Tmp1)
2226 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
2227 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002228 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002229 else
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002230 {
2231 long offset;
2232 SelectAddr(Address, Tmp2, offset);
2233 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2234 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002235 return;
2236 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002237
2238 case ISD::EXTLOAD:
2239 case ISD::SEXTLOAD:
2240 case ISD::ZEXTLOAD:
2241 case ISD::LOAD:
2242 case ISD::CopyFromReg:
2243 case ISD::CALL:
Andrew Lenharth032f2352005-02-22 21:59:48 +00002244 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002245 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002246 SelectExpr(N);
2247 return;
2248
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002249 case ISD::ADJCALLSTACKDOWN:
2250 case ISD::ADJCALLSTACKUP:
2251 Select(N.getOperand(0));
2252 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002253
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002254 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? Alpha::ADJUSTSTACKDOWN :
2255 Alpha::ADJUSTSTACKUP;
2256 BuildMI(BB, Opc, 1).addImm(Tmp1);
2257 return;
Andrew Lenharth95762122005-03-31 21:24:06 +00002258
2259 case ISD::PCMARKER:
2260 Select(N.getOperand(0)); //Chain
2261 BuildMI(BB, Alpha::PCLABEL, 2).addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
2262 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002263 }
2264 assert(0 && "Should not be reached!");
2265}
2266
2267
2268/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
2269/// into a machine code representation using pattern matching and a machine
2270/// description file.
2271///
2272FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002273 return new ISel(TM);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002274}
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002275