blob: 9a9e587c75220f8123aad16d0022180787875118 [file] [log] [blame]
Misha Brukman2a8350a2005-02-05 02:24:26 +00001//===- AlphaISelPattern.cpp - A pattern matching inst selector for Alpha --===//
Misha Brukman4633f1c2005-04-21 23:13:11 +00002//
Andrew Lenharth304d0f32005-01-22 23:41:55 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman4633f1c2005-04-21 23:13:11 +00007//
Andrew Lenharth304d0f32005-01-22 23:41:55 +00008//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for Alpha.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Alpha.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000015#include "AlphaRegisterInfo.h"
16#include "llvm/Constants.h" // FIXME: REMOVE
17#include "llvm/Function.h"
18#include "llvm/CodeGen/MachineInstrBuilder.h"
19#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
20#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/MachineFrameInfo.h"
22#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SelectionDAGISel.h"
24#include "llvm/CodeGen/SSARegMap.h"
25#include "llvm/Target/TargetData.h"
26#include "llvm/Target/TargetLowering.h"
27#include "llvm/Support/MathExtras.h"
28#include "llvm/ADT/Statistic.h"
Andrew Lenharth032f2352005-02-22 21:59:48 +000029#include "llvm/Support/Debug.h"
Andrew Lenharth95762122005-03-31 21:24:06 +000030#include "llvm/Support/CommandLine.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000031#include <set>
Andrew Lenharth684f2292005-01-30 00:35:27 +000032#include <algorithm>
Andrew Lenharth304d0f32005-01-22 23:41:55 +000033using namespace llvm;
34
Andrew Lenharth95762122005-03-31 21:24:06 +000035namespace llvm {
Misha Brukman4633f1c2005-04-21 23:13:11 +000036 cl::opt<bool> EnableAlphaIDIV("enable-alpha-intfpdiv",
37 cl::desc("Use the FP div instruction for integer div when possible"),
Andrew Lenharth95762122005-03-31 21:24:06 +000038 cl::Hidden);
Andrew Lenharth59009192005-05-04 19:12:09 +000039 cl::opt<bool> EnableAlphaFTOI("enable-alpha-FTOI",
Misha Brukman4633f1c2005-04-21 23:13:11 +000040 cl::desc("Enable use of ftoi* and itof* instructions (ev6 and higher)"),
Andrew Lenharth95762122005-03-31 21:24:06 +000041 cl::Hidden);
Andrew Lenharth59009192005-05-04 19:12:09 +000042 cl::opt<bool> EnableAlphaCT("enable-alpha-CT",
43 cl::desc("Enable use of the ctpop, ctlz, and cttz instructions"),
44 cl::Hidden);
Misha Brukman4633f1c2005-04-21 23:13:11 +000045 cl::opt<bool> EnableAlphaCount("enable-alpha-count",
46 cl::desc("Print estimates on live ins and outs"),
Andrew Lenhartha32b9e32005-04-08 17:28:49 +000047 cl::Hidden);
Andrew Lenharth95762122005-03-31 21:24:06 +000048}
49
Andrew Lenharth304d0f32005-01-22 23:41:55 +000050//===----------------------------------------------------------------------===//
51// AlphaTargetLowering - Alpha Implementation of the TargetLowering interface
52namespace {
53 class AlphaTargetLowering : public TargetLowering {
54 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
55 unsigned GP; //GOT vreg
56 public:
57 AlphaTargetLowering(TargetMachine &TM) : TargetLowering(TM) {
58 // Set up the TargetLowering object.
Andrew Lenharth3d65d312005-01-27 03:49:45 +000059 //I am having problems with shr n ubyte 1
Andrew Lenharth879ef222005-02-02 17:00:21 +000060 setShiftAmountType(MVT::i64);
61 setSetCCResultType(MVT::i64);
Andrew Lenharthd3355e22005-04-07 20:11:32 +000062 setSetCCResultContents(ZeroOrOneSetCCResult);
Misha Brukman4633f1c2005-04-21 23:13:11 +000063
Andrew Lenharth304d0f32005-01-22 23:41:55 +000064 addRegisterClass(MVT::i64, Alpha::GPRCRegisterClass);
65 addRegisterClass(MVT::f64, Alpha::FPRCRegisterClass);
Andrew Lenharth3d65d312005-01-27 03:49:45 +000066 addRegisterClass(MVT::f32, Alpha::FPRCRegisterClass);
Misha Brukman4633f1c2005-04-21 23:13:11 +000067
Chris Lattnerda4d4692005-04-09 03:22:37 +000068 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000069 setOperationAction(ISD::EXTLOAD , MVT::i1 , Promote);
70 setOperationAction(ISD::EXTLOAD , MVT::f32 , Promote);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000071
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000072 setOperationAction(ISD::ZEXTLOAD , MVT::i1 , Expand);
73 setOperationAction(ISD::ZEXTLOAD , MVT::i32 , Expand);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000074
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000075 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
76 setOperationAction(ISD::SEXTLOAD , MVT::i8 , Expand);
77 setOperationAction(ISD::SEXTLOAD , MVT::i16 , Expand);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000078
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000079 setOperationAction(ISD::SREM , MVT::f32 , Expand);
80 setOperationAction(ISD::SREM , MVT::f64 , Expand);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +000081
Andrew Lenharth59009192005-05-04 19:12:09 +000082 if (!EnableAlphaCT) {
83 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
84 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
Andrew Lenharthb5884d32005-05-04 19:25:37 +000085 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
Andrew Lenharth59009192005-05-04 19:12:09 +000086 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +000087
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000088 //If this didn't legalize into a div....
89 // setOperationAction(ISD::SREM , MVT::i64, Expand);
90 // setOperationAction(ISD::UREM , MVT::i64, Expand);
91
92 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
93 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
94 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
Andrew Lenharth9818c052005-02-05 13:19:12 +000095
Chris Lattner17234b72005-04-30 04:26:06 +000096 // We don't support sin/cos/sqrt
97 setOperationAction(ISD::FSIN , MVT::f64, Expand);
98 setOperationAction(ISD::FCOS , MVT::f64, Expand);
99 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
100 setOperationAction(ISD::FSIN , MVT::f32, Expand);
101 setOperationAction(ISD::FCOS , MVT::f32, Expand);
102 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
103
Andrew Lenharth33819132005-03-04 20:09:23 +0000104 //Doesn't work yet
Chris Lattner17234b72005-04-30 04:26:06 +0000105 setOperationAction(ISD::SETCC, MVT::f32, Promote);
Andrew Lenharth572af902005-02-14 05:41:43 +0000106
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000107 computeRegisterProperties();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000108
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000109 addLegalFPImmediate(+0.0); //F31
110 addLegalFPImmediate(-0.0); //-F31
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000111 }
112
113 /// LowerArguments - This hook must be implemented to indicate how we should
114 /// lower the arguments for the specified function, into the specified DAG.
115 virtual std::vector<SDOperand>
116 LowerArguments(Function &F, SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000117
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000118 /// LowerCallTo - This hook lowers an abstract call to a function into an
119 /// actual call.
120 virtual std::pair<SDOperand, SDOperand>
Chris Lattnerc57f6822005-05-12 19:56:45 +0000121 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg, unsigned CC,
Nate Begeman8e21e712005-03-26 01:29:23 +0000122 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,
Chris Lattnerc57f6822005-05-12 19:56:45 +0000307 unsigned CallingConv,
Misha Brukman7847fca2005-04-22 17:54:37 +0000308 SDOperand Callee, ArgListTy &Args,
309 SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000310 int NumBytes = 0;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000311 if (Args.size() > 6)
312 NumBytes = (Args.size() - 6) * 8;
313
Chris Lattner16cd04d2005-05-12 23:24:06 +0000314 Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000315 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000316 std::vector<SDOperand> args_to_use;
317 for (unsigned i = 0, e = Args.size(); i != e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000318 {
319 switch (getValueType(Args[i].second)) {
320 default: assert(0 && "Unexpected ValueType for argument!");
321 case MVT::i1:
322 case MVT::i8:
323 case MVT::i16:
324 case MVT::i32:
325 // Promote the integer to 64 bits. If the input type is signed use a
326 // sign extend, otherwise use a zero extend.
327 if (Args[i].second->isSigned())
328 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
329 else
330 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
331 break;
332 case MVT::i64:
333 case MVT::f64:
334 case MVT::f32:
335 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000336 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000337 args_to_use.push_back(Args[i].first);
338 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000339
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000340 std::vector<MVT::ValueType> RetVals;
341 MVT::ValueType RetTyVT = getValueType(RetTy);
342 if (RetTyVT != MVT::isVoid)
343 RetVals.push_back(RetTyVT);
344 RetVals.push_back(MVT::Other);
345
Misha Brukman4633f1c2005-04-21 23:13:11 +0000346 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000347 Chain, Callee, args_to_use), 0);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000348 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
Chris Lattner16cd04d2005-05-12 23:24:06 +0000349 Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, Chain,
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000350 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000351 return std::make_pair(TheCall, Chain);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000352}
353
354std::pair<SDOperand, SDOperand>
355AlphaTargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
356 //vastart just returns the address of the VarArgsFrameIndex slot.
357 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64), Chain);
358}
359
360std::pair<SDOperand,SDOperand> AlphaTargetLowering::
361LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000362 const Type *ArgTy, SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000363 abort();
364}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000365
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000366
367std::pair<SDOperand, SDOperand> AlphaTargetLowering::
368LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
369 SelectionDAG &DAG) {
370 abort();
371}
372
373
374
375
376
377namespace {
378
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000379//===--------------------------------------------------------------------===//
380/// ISel - Alpha specific code to select Alpha machine instructions for
381/// SelectionDAG operations.
382//===--------------------------------------------------------------------===//
383class ISel : public SelectionDAGISel {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000384
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000385 /// AlphaLowering - This object fully describes how to lower LLVM code to an
386 /// Alpha-specific SelectionDAG.
387 AlphaTargetLowering AlphaLowering;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000388
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000389 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
390 // for sdiv and udiv until it is put into the future
391 // dag combiner.
392
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000393 /// ExprMap - As shared expressions are codegen'd, we keep track of which
394 /// vreg the value is produced in, so we only emit one copy of each compiled
395 /// tree.
396 static const unsigned notIn = (unsigned)(-1);
397 std::map<SDOperand, unsigned> ExprMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000398
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000399 //CCInvMap sometimes (SetNE) we have the inverse CC code for free
400 std::map<SDOperand, unsigned> CCInvMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000401
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000402 int count_ins;
403 int count_outs;
404 bool has_sym;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000405 int max_depth;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000406
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000407public:
Misha Brukman4633f1c2005-04-21 23:13:11 +0000408 ISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering), AlphaLowering(TM)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000409 {}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000410
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000411 /// InstructionSelectBasicBlock - This callback is invoked by
412 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
413 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
Andrew Lenharth032f2352005-02-22 21:59:48 +0000414 DEBUG(BB->dump());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000415 count_ins = 0;
416 count_outs = 0;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000417 max_depth = 0;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000418 has_sym = false;
419
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000420 // Codegen the basic block.
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000421 ISelDAG = &DAG;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000422 max_depth = DAG.getRoot().getNodeDepth();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000423 Select(DAG.getRoot());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000424
425 if(has_sym)
426 ++count_ins;
427 if(EnableAlphaCount)
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000428 std::cerr << "COUNT: " << BB->getParent()->getFunction ()->getName() << " "
429 << BB->getNumber() << " "
430 << max_depth << " "
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000431 << count_ins << " "
432 << count_outs << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000433
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000434 // Clear state used for selection.
435 ExprMap.clear();
436 CCInvMap.clear();
437 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000438
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000439 unsigned SelectExpr(SDOperand N);
440 unsigned SelectExprFP(SDOperand N, unsigned Result);
441 void Select(SDOperand N);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000442
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000443 void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
444 void SelectBranchCC(SDOperand N);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000445 void MoveFP2Int(unsigned src, unsigned dst, bool isDouble);
446 void MoveInt2FP(unsigned src, unsigned dst, bool isDouble);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000447 //returns whether the sense of the comparison was inverted
448 bool SelectFPSetCC(SDOperand N, unsigned dst);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000449
450 // dag -> dag expanders for integer divide by constant
451 SDOperand BuildSDIVSequence(SDOperand N);
452 SDOperand BuildUDIVSequence(SDOperand N);
453
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000454};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000455}
456
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000457//Factorize a number using the list of constants
458static bool factorize(int v[], int res[], int size, uint64_t c)
459{
460 bool cont = true;
461 while (c != 1 && cont)
462 {
463 cont = false;
464 for(int i = 0; i < size; ++i)
465 {
466 if (c % v[i] == 0)
467 {
468 c /= v[i];
469 ++res[i];
470 cont=true;
471 }
472 }
473 }
474 return c == 1;
475}
476
477
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000478//Shamelessly adapted from PPC32
Misha Brukman4633f1c2005-04-21 23:13:11 +0000479// Structure used to return the necessary information to codegen an SDIV as
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000480// a multiply.
481struct ms {
482 int64_t m; // magic number
483 int64_t s; // shift amount
484};
485
486struct mu {
487 uint64_t m; // magic number
488 int64_t a; // add indicator
489 int64_t s; // shift amount
490};
491
492/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukman4633f1c2005-04-21 23:13:11 +0000493/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000494/// or -1.
495static struct ms magic(int64_t d) {
496 int64_t p;
497 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
498 const uint64_t two63 = 9223372036854775808ULL; // 2^63
499 struct ms mag;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000500
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000501 ad = abs(d);
502 t = two63 + ((uint64_t)d >> 63);
503 anc = t - 1 - t%ad; // absolute value of nc
Andrew Lenharth320174f2005-04-07 17:19:16 +0000504 p = 63; // initialize p
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000505 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
506 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
507 q2 = two63/ad; // initialize q2 = 2p/abs(d)
508 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
509 do {
510 p = p + 1;
511 q1 = 2*q1; // update q1 = 2p/abs(nc)
512 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
513 if (r1 >= anc) { // must be unsigned comparison
514 q1 = q1 + 1;
515 r1 = r1 - anc;
516 }
517 q2 = 2*q2; // update q2 = 2p/abs(d)
518 r2 = 2*r2; // update r2 = rem(2p/abs(d))
519 if (r2 >= ad) { // must be unsigned comparison
520 q2 = q2 + 1;
521 r2 = r2 - ad;
522 }
523 delta = ad - r2;
524 } while (q1 < delta || (q1 == delta && r1 == 0));
525
526 mag.m = q2 + 1;
527 if (d < 0) mag.m = -mag.m; // resulting magic number
528 mag.s = p - 64; // resulting shift
529 return mag;
530}
531
532/// magicu - calculate the magic numbers required to codegen an integer udiv as
533/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
534static struct mu magicu(uint64_t d)
535{
536 int64_t p;
537 uint64_t nc, delta, q1, r1, q2, r2;
538 struct mu magu;
539 magu.a = 0; // initialize "add" indicator
540 nc = - 1 - (-d)%d;
Andrew Lenharth320174f2005-04-07 17:19:16 +0000541 p = 63; // initialize p
542 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
543 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
544 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
545 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000546 do {
547 p = p + 1;
548 if (r1 >= nc - r1 ) {
549 q1 = 2*q1 + 1; // update q1
550 r1 = 2*r1 - nc; // update r1
551 }
552 else {
553 q1 = 2*q1; // update q1
554 r1 = 2*r1; // update r1
555 }
556 if (r2 + 1 >= d - r2) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000557 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000558 q2 = 2*q2 + 1; // update q2
559 r2 = 2*r2 + 1 - d; // update r2
560 }
561 else {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000562 if (q2 >= 0x8000000000000000ull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000563 q2 = 2*q2; // update q2
564 r2 = 2*r2 + 1; // update r2
565 }
566 delta = d - 1 - r2;
567 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
568 magu.m = q2 + 1; // resulting magic number
Andrew Lenharth320174f2005-04-07 17:19:16 +0000569 magu.s = p - 64; // resulting shift
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000570 return magu;
571}
572
573/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
574/// return a DAG expression to select that will generate the same value by
575/// multiplying by a magic number. See:
576/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
577SDOperand ISel::BuildSDIVSequence(SDOperand N) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000578 int64_t d = (int64_t)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000579 ms magics = magic(d);
580 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000581 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000582 ISelDAG->getConstant(magics.m, MVT::i64));
583 // If d > 0 and m < 0, add the numerator
584 if (d > 0 && magics.m < 0)
585 Q = ISelDAG->getNode(ISD::ADD, MVT::i64, Q, N.getOperand(0));
586 // If d < 0 and m > 0, subtract the numerator.
587 if (d < 0 && magics.m > 0)
588 Q = ISelDAG->getNode(ISD::SUB, MVT::i64, Q, N.getOperand(0));
589 // Shift right algebraic if shift value is nonzero
590 if (magics.s > 0)
Misha Brukman4633f1c2005-04-21 23:13:11 +0000591 Q = ISelDAG->getNode(ISD::SRA, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000592 ISelDAG->getConstant(magics.s, MVT::i64));
593 // Extract the sign bit and add it to the quotient
Misha Brukman4633f1c2005-04-21 23:13:11 +0000594 SDOperand T =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000595 ISelDAG->getNode(ISD::SRL, MVT::i64, Q, ISelDAG->getConstant(63, MVT::i64));
596 return ISelDAG->getNode(ISD::ADD, MVT::i64, Q, T);
597}
598
599/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
600/// return a DAG expression to select that will generate the same value by
601/// multiplying by a magic number. See:
602/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
603SDOperand ISel::BuildUDIVSequence(SDOperand N) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000604 unsigned d =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000605 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
606 mu magics = magicu(d);
607 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000608 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000609 ISelDAG->getConstant(magics.m, MVT::i64));
610 if (magics.a == 0) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000611 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000612 ISelDAG->getConstant(magics.s, MVT::i64));
613 } else {
614 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i64, N.getOperand(0), Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000615 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000616 ISelDAG->getConstant(1, MVT::i64));
617 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i64, NPQ, Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000618 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000619 ISelDAG->getConstant(magics.s-1, MVT::i64));
620 }
621 return Q;
622}
623
Andrew Lenhartha565c272005-04-06 22:03:13 +0000624//From PPC32
625/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
626/// returns zero when the input is not exactly a power of two.
627static unsigned ExactLog2(uint64_t Val) {
628 if (Val == 0 || (Val & (Val-1))) return 0;
629 unsigned Count = 0;
630 while (Val != 1) {
631 Val >>= 1;
632 ++Count;
633 }
634 return Count;
635}
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000636
637
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000638//These describe LDAx
Andrew Lenharthc0513832005-03-29 19:24:04 +0000639static const int IMM_LOW = -32768;
640static const int IMM_HIGH = 32767;
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000641static const int IMM_MULT = 65536;
642
643static long getUpper16(long l)
644{
645 long y = l / IMM_MULT;
646 if (l % IMM_MULT > IMM_HIGH)
647 ++y;
648 return y;
649}
650
651static long getLower16(long l)
652{
653 long h = getUpper16(l);
654 return l - h * IMM_MULT;
655}
656
Andrew Lenharth65838902005-02-06 16:22:15 +0000657static unsigned GetSymVersion(unsigned opcode)
658{
659 switch (opcode) {
660 default: assert(0 && "unknown load or store"); return 0;
661 case Alpha::LDQ: return Alpha::LDQ_SYM;
662 case Alpha::LDS: return Alpha::LDS_SYM;
663 case Alpha::LDT: return Alpha::LDT_SYM;
664 case Alpha::LDL: return Alpha::LDL_SYM;
665 case Alpha::LDBU: return Alpha::LDBU_SYM;
666 case Alpha::LDWU: return Alpha::LDWU_SYM;
667 case Alpha::LDW: return Alpha::LDW_SYM;
668 case Alpha::LDB: return Alpha::LDB_SYM;
669 case Alpha::STQ: return Alpha::STQ_SYM;
670 case Alpha::STS: return Alpha::STS_SYM;
671 case Alpha::STT: return Alpha::STT_SYM;
672 case Alpha::STL: return Alpha::STL_SYM;
673 case Alpha::STW: return Alpha::STW_SYM;
674 case Alpha::STB: return Alpha::STB_SYM;
675 }
676}
677
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000678void ISel::MoveFP2Int(unsigned src, unsigned dst, bool isDouble)
679{
680 unsigned Opc;
681 if (EnableAlphaFTOI) {
682 Opc = isDouble ? Alpha::FTOIT : Alpha::FTOIS;
683 BuildMI(BB, Opc, 1, dst).addReg(src);
684 } else {
685 //The hard way:
686 // Spill the integer to memory and reload it from there.
687 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
688 MachineFunction *F = BB->getParent();
689 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
690
691 Opc = isDouble ? Alpha::STT : Alpha::STS;
692 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
693 Opc = isDouble ? Alpha::LDQ : Alpha::LDL;
694 BuildMI(BB, Alpha::LDQ, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
695 }
696}
697
698void ISel::MoveInt2FP(unsigned src, unsigned dst, bool isDouble)
699{
700 unsigned Opc;
701 if (EnableAlphaFTOI) {
702 Opc = isDouble?Alpha::ITOFT:Alpha::ITOFS;
703 BuildMI(BB, Opc, 1, dst).addReg(src);
704 } else {
705 //The hard way:
706 // Spill the integer to memory and reload it from there.
707 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
708 MachineFunction *F = BB->getParent();
709 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
710
711 Opc = isDouble ? Alpha::STQ : Alpha::STL;
712 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
713 Opc = isDouble ? Alpha::LDT : Alpha::LDS;
714 BuildMI(BB, Opc, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
715 }
716}
717
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000718bool ISel::SelectFPSetCC(SDOperand N, unsigned dst)
719{
720 SDNode *Node = N.Val;
721 unsigned Opc, Tmp1, Tmp2, Tmp3;
722 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node);
723
724 //assert(SetCC->getOperand(0).getValueType() != MVT::f32 && "SetCC f32 should have been promoted");
725 bool rev = false;
726 bool inv = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000727
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000728 switch (SetCC->getCondition()) {
729 default: Node->dump(); assert(0 && "Unknown FP comparison!");
730 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
731 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
732 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
733 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
734 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
735 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
736 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000737
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000738 //FIXME: check for constant 0.0
739 ConstantFPSDNode *CN;
740 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
741 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
742 Tmp1 = Alpha::F31;
743 else
744 Tmp1 = SelectExpr(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000745
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000746 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
747 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
748 Tmp2 = Alpha::F31;
749 else
Chris Lattner9c9183a2005-04-30 04:44:07 +0000750 Tmp2 = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000751
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000752 //Can only compare doubles, and dag won't promote for me
753 if (SetCC->getOperand(0).getValueType() == MVT::f32)
754 {
755 //assert(0 && "Setcc On float?\n");
756 std::cerr << "Setcc on float!\n";
757 Tmp3 = MakeReg(MVT::f64);
758 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
759 Tmp1 = Tmp3;
760 }
761 if (SetCC->getOperand(1).getValueType() == MVT::f32)
762 {
763 //assert (0 && "Setcc On float?\n");
764 std::cerr << "Setcc on float!\n";
765 Tmp3 = MakeReg(MVT::f64);
766 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp2);
767 Tmp2 = Tmp3;
768 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000769
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000770 if (rev) std::swap(Tmp1, Tmp2);
771 //do the comparison
772 BuildMI(BB, Opc, 2, dst).addReg(Tmp1).addReg(Tmp2);
773 return inv;
774}
775
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000776//Check to see if the load is a constant offset from a base register
777void ISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
778{
779 unsigned opcode = N.getOpcode();
780 if (opcode == ISD::ADD) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000781 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000782 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
783 { //Normal imm add
784 Reg = SelectExpr(N.getOperand(0));
785 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
786 return;
787 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000788 else if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000789 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 32767)
790 {
791 Reg = SelectExpr(N.getOperand(1));
792 offset = cast<ConstantSDNode>(N.getOperand(0))->getValue();
793 return;
794 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000795 }
796 Reg = SelectExpr(N);
797 offset = 0;
798 return;
799}
800
Andrew Lenharth445171a2005-02-08 00:40:03 +0000801void ISel::SelectBranchCC(SDOperand N)
802{
803 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000804 MachineBasicBlock *Dest =
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000805 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
806 unsigned Opc = Alpha::WTF;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000807
Andrew Lenharth445171a2005-02-08 00:40:03 +0000808 Select(N.getOperand(0)); //chain
809 SDOperand CC = N.getOperand(1);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000810
Andrew Lenharth445171a2005-02-08 00:40:03 +0000811 if (CC.getOpcode() == ISD::SETCC)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000812 {
813 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
814 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
815 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000816 bool LeftZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
817 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
818 bool RightZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
819 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000820 bool isNE = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000821
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000822 //Fix up CC
823 ISD::CondCode cCode= SetCC->getCondition();
824 if (LeftZero && !RightZero) //Swap Operands
825 cCode = ISD::getSetCCSwappedOperands(cCode);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000826
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000827 if(cCode == ISD::SETNE)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000828 isNE = true;
Andrew Lenharth445171a2005-02-08 00:40:03 +0000829
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000830 if (LeftZero || RightZero) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000831 switch (SetCC->getCondition()) {
832 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
833 case ISD::SETEQ: Opc = Alpha::BEQ; break;
834 case ISD::SETLT: Opc = Alpha::BLT; break;
835 case ISD::SETLE: Opc = Alpha::BLE; break;
836 case ISD::SETGT: Opc = Alpha::BGT; break;
837 case ISD::SETGE: Opc = Alpha::BGE; break;
838 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
839 case ISD::SETUGT: Opc = Alpha::BNE; break;
840 case ISD::SETULE: Opc = Alpha::BEQ; break; //Technically you could have this CC
841 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
842 case ISD::SETNE: Opc = Alpha::BNE; break;
843 }
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000844 unsigned Tmp1;
845 if(LeftZero && !RightZero) //swap Operands
846 Tmp1 = SelectExpr(SetCC->getOperand(1)); //Cond
847 else
848 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000849 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
850 return;
851 } else {
852 unsigned Tmp1 = SelectExpr(CC);
853 if (isNE)
854 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
855 else
856 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000857 return;
858 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000859 } else { //FP
860 //Any comparison between 2 values should be codegened as an folded branch, as moving
861 //CC to the integer register is very expensive
862 //for a cmp b: c = a - b;
863 //a = b: c = 0
864 //a < b: c < 0
865 //a > b: c > 0
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000866
867 bool invTest = false;
868 unsigned Tmp3;
869
870 ConstantFPSDNode *CN;
871 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
872 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
873 Tmp3 = SelectExpr(SetCC->getOperand(0));
874 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
875 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
876 {
877 Tmp3 = SelectExpr(SetCC->getOperand(1));
878 invTest = true;
879 }
880 else
881 {
882 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
883 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
884 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
885 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
886 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
887 .addReg(Tmp1).addReg(Tmp2);
888 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000889
890 switch (SetCC->getCondition()) {
891 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000892 case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
893 case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
894 case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
895 case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
896 case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
897 case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000898 }
899 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000900 return;
901 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000902 abort(); //Should never be reached
903 } else {
904 //Giveup and do the stupid thing
905 unsigned Tmp1 = SelectExpr(CC);
906 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
907 return;
908 }
Andrew Lenharth445171a2005-02-08 00:40:03 +0000909 abort(); //Should never be reached
910}
911
Andrew Lenharth40831c52005-01-28 06:57:18 +0000912unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
913{
914 unsigned Tmp1, Tmp2, Tmp3;
915 unsigned Opc = 0;
916 SDNode *Node = N.Val;
917 MVT::ValueType DestType = N.getValueType();
918 unsigned opcode = N.getOpcode();
919
920 switch (opcode) {
921 default:
922 Node->dump();
923 assert(0 && "Node not handled!\n");
Andrew Lenharth2c594352005-01-29 15:42:07 +0000924
Andrew Lenharth7332f3e2005-04-02 19:11:07 +0000925 case ISD::UNDEF: {
926 BuildMI(BB, Alpha::IDEF, 0, Result);
927 return Result;
928 }
929
Andrew Lenharth30b46d42005-04-02 19:04:58 +0000930 case ISD::FNEG:
931 if(ISD::FABS == N.getOperand(0).getOpcode())
932 {
Misha Brukman7847fca2005-04-22 17:54:37 +0000933 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
934 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +0000935 } else {
Misha Brukman7847fca2005-04-22 17:54:37 +0000936 Tmp1 = SelectExpr(N.getOperand(0));
937 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +0000938 }
939 return Result;
940
941 case ISD::FABS:
942 Tmp1 = SelectExpr(N.getOperand(0));
943 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
944 return Result;
945
Andrew Lenharth9818c052005-02-05 13:19:12 +0000946 case ISD::SELECT:
947 {
Andrew Lenharth45859692005-03-03 21:47:53 +0000948 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
949 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
950 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
951
952 SDOperand CC = N.getOperand(0);
953 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
954
Misha Brukman4633f1c2005-04-21 23:13:11 +0000955 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth45859692005-03-03 21:47:53 +0000956 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
957 { //FP Setcc -> Select yay!
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000958
959
Andrew Lenharth45859692005-03-03 21:47:53 +0000960 //for a cmp b: c = a - b;
961 //a = b: c = 0
962 //a < b: c < 0
963 //a > b: c > 0
Misha Brukman4633f1c2005-04-21 23:13:11 +0000964
Andrew Lenharth45859692005-03-03 21:47:53 +0000965 bool invTest = false;
966 unsigned Tmp3;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000967
Andrew Lenharth45859692005-03-03 21:47:53 +0000968 ConstantFPSDNode *CN;
969 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
970 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
971 Tmp3 = SelectExpr(SetCC->getOperand(0));
972 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
973 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
974 {
975 Tmp3 = SelectExpr(SetCC->getOperand(1));
976 invTest = true;
977 }
978 else
979 {
980 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
981 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
982 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
983 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
984 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
985 .addReg(Tmp1).addReg(Tmp2);
986 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000987
Andrew Lenharth45859692005-03-03 21:47:53 +0000988 switch (SetCC->getCondition()) {
989 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
990 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNE : Alpha::FCMOVEQ; break;
991 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGT : Alpha::FCMOVLT; break;
992 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGE : Alpha::FCMOVLE; break;
993 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLT : Alpha::FCMOVGT; break;
994 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLE : Alpha::FCMOVGE; break;
995 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQ : Alpha::FCMOVNE; break;
996 }
Andrew Lenharth33819132005-03-04 20:09:23 +0000997 BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
Andrew Lenharth45859692005-03-03 21:47:53 +0000998 return Result;
999 }
1000 else
1001 {
1002 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001003 BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV).addReg(Tmp1);
1004// // Spill the cond to memory and reload it from there.
1005// unsigned Tmp4 = MakeReg(MVT::f64);
1006// MoveIntFP(Tmp1, Tmp4, true);
1007// //now ideally, we don't have to do anything to the flag...
1008// // Get the condition into the zero flag.
1009// BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
Andrew Lenharth45859692005-03-03 21:47:53 +00001010 return Result;
1011 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001012 }
1013
Andrew Lenharthc1faced2005-02-01 01:37:24 +00001014 case ISD::FP_ROUND:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001015 assert (DestType == MVT::f32 &&
1016 N.getOperand(0).getValueType() == MVT::f64 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001017 "only f64 to f32 conversion supported here");
Andrew Lenharthc1faced2005-02-01 01:37:24 +00001018 Tmp1 = SelectExpr(N.getOperand(0));
1019 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
1020 return Result;
1021
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001022 case ISD::FP_EXTEND:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001023 assert (DestType == MVT::f64 &&
1024 N.getOperand(0).getValueType() == MVT::f32 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001025 "only f32 to f64 conversion supported here");
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001026 Tmp1 = SelectExpr(N.getOperand(0));
1027 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
1028 return Result;
1029
Andrew Lenharth2c594352005-01-29 15:42:07 +00001030 case ISD::CopyFromReg:
1031 {
1032 // Make sure we generate both values.
1033 if (Result != notIn)
1034 ExprMap[N.getValue(1)] = notIn; // Generate the token
1035 else
1036 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001037
Andrew Lenharth2c594352005-01-29 15:42:07 +00001038 SDOperand Chain = N.getOperand(0);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001039
Andrew Lenharth2c594352005-01-29 15:42:07 +00001040 Select(Chain);
1041 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1042 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1043 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
1044 return Result;
1045 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001046
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001047 case ISD::LOAD:
1048 {
1049 // Make sure we generate both values.
1050 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001051 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001052 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001053 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001054
Andrew Lenharth29219162005-02-07 06:31:44 +00001055 DestType = N.getValue(0).getValueType();
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001056
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001057 SDOperand Chain = N.getOperand(0);
1058 SDOperand Address = N.getOperand(1);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001059 Select(Chain);
Andrew Lenharth65838902005-02-06 16:22:15 +00001060 Opc = DestType == MVT::f64 ? Alpha::LDT : Alpha::LDS;
1061
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001062 if (Address.getOpcode() == ISD::GlobalAddress) {
1063 AlphaLowering.restoreGP(BB);
1064 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001065 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001066 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1067 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001068 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001069 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001070 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001071 has_sym = true;
Andrew Lenharth97127a12005-02-05 17:41:39 +00001072 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001073 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001074 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001075 BuildMI(BB, Opc, 2, Result)
1076 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1077 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001078 } else {
1079 long offset;
1080 SelectAddr(Address, Tmp1, offset);
1081 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1082 }
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001083 return Result;
1084 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001085 case ISD::ConstantFP:
1086 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
1087 if (CN->isExactlyValue(+0.0)) {
1088 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001089 } else if ( CN->isExactlyValue(-0.0)) {
1090 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001091 } else {
1092 abort();
1093 }
1094 }
1095 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001096
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001097 case ISD::SDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001098 case ISD::MUL:
1099 case ISD::ADD:
1100 case ISD::SUB:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001101 switch( opcode ) {
1102 case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS; break;
1103 case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS; break;
1104 case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS; break;
1105 case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS; break;
1106 };
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001107
1108 ConstantFPSDNode *CN;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001109 if (opcode == ISD::SUB
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001110 && (CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
1111 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1112 {
1113 Tmp2 = SelectExpr(N.getOperand(1));
1114 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp2).addReg(Tmp2);
1115 } else {
1116 Tmp1 = SelectExpr(N.getOperand(0));
1117 Tmp2 = SelectExpr(N.getOperand(1));
1118 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1119 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001120 return Result;
1121
Andrew Lenharth2c594352005-01-29 15:42:07 +00001122 case ISD::EXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001123 {
1124 //include a conversion sequence for float loads to double
1125 if (Result != notIn)
1126 ExprMap[N.getValue(1)] = notIn; // Generate the token
1127 else
1128 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001129
Andrew Lenhartha549deb2005-02-07 05:33:15 +00001130 Tmp1 = MakeReg(MVT::f32);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001131
1132 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001133 "EXTLOAD not from f32");
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001134 assert(Node->getValueType(0) == MVT::f64 && "EXTLOAD not to f64");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001135
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001136 SDOperand Chain = N.getOperand(0);
1137 SDOperand Address = N.getOperand(1);
1138 Select(Chain);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001139
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001140 if (Address.getOpcode() == ISD::GlobalAddress) {
1141 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001142 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001143 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1144 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001145 else if (ConstantPoolSDNode *CP =
1146 dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001147 {
1148 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001149 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001150 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addConstantPoolIndex(CP->getIndex());
1151 }
1152 else if(Address.getOpcode() == ISD::FrameIndex) {
1153 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
Andrew Lenharth032f2352005-02-22 21:59:48 +00001154 BuildMI(BB, Alpha::LDS, 2, Tmp1)
1155 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1156 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001157 } else {
1158 long offset;
1159 SelectAddr(Address, Tmp2, offset);
1160 BuildMI(BB, Alpha::LDS, 1, Tmp1).addImm(offset).addReg(Tmp2);
1161 }
Andrew Lenharth29219162005-02-07 06:31:44 +00001162 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001163 return Result;
1164 }
Andrew Lenharth2c594352005-01-29 15:42:07 +00001165
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001166 case ISD::UINT_TO_FP:
1167 case ISD::SINT_TO_FP:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001168 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001169 assert (N.getOperand(0).getValueType() == MVT::i64
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001170 && "only quads can be loaded from");
Andrew Lenharth40831c52005-01-28 06:57:18 +00001171 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001172 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001173 MoveInt2FP(Tmp1, Tmp2, true);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001174 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
1175 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001176 return Result;
1177 }
1178 }
1179 assert(0 && "should not get here");
1180 return 0;
1181}
1182
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001183unsigned ISel::SelectExpr(SDOperand N) {
1184 unsigned Result;
Andrew Lenharth2966e842005-04-07 18:15:28 +00001185 unsigned Tmp1, Tmp2 = 0, Tmp3;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001186 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001187 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001188
1189 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001190 MVT::ValueType DestType = N.getValueType();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001191
1192 unsigned &Reg = ExprMap[N];
1193 if (Reg) return Reg;
1194
1195 if (N.getOpcode() != ISD::CALL)
1196 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001197 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001198 else {
1199 // If this is a call instruction, make sure to prepare ALL of the result
1200 // values as well as the chain.
1201 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001202 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001203 else {
1204 Result = MakeReg(Node->getValueType(0));
1205 ExprMap[N.getValue(0)] = Result;
1206 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1207 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001208 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001209 }
1210 }
1211
Andrew Lenharth50d91d72005-04-30 14:19:13 +00001212 if ((DestType == MVT::f64 || DestType == MVT::f32 ||
1213 (
1214 (opcode == ISD::LOAD || opcode == ISD::CopyFromReg ||
1215 opcode == ISD::EXTLOAD) &&
1216 (N.getValue(0).getValueType() == MVT::f32 ||
1217 N.getValue(0).getValueType() == MVT::f64)
1218 ))
1219 && opcode != ISD::CALL
Andrew Lenharth06342c32005-02-07 06:21:37 +00001220 )
Andrew Lenharth40831c52005-01-28 06:57:18 +00001221 return SelectExprFP(N, Result);
1222
1223 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001224 default:
1225 Node->dump();
1226 assert(0 && "Node not handled!\n");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001227
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001228 case ISD::CTPOP:
1229 case ISD::CTTZ:
1230 case ISD::CTLZ:
1231 Opc = opcode == ISD::CTPOP ? Alpha::CTPOP :
1232 (opcode == ISD::CTTZ ? Alpha::CTTZ : Alpha::CTLZ);
1233 Tmp1 = SelectExpr(N.getOperand(0));
1234 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
1235 return Result;
1236
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001237 case ISD::MULHU:
1238 Tmp1 = SelectExpr(N.getOperand(0));
1239 Tmp2 = SelectExpr(N.getOperand(1));
1240 BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth706be912005-04-07 13:55:53 +00001241 return Result;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001242 case ISD::MULHS:
1243 {
1244 //MULHU - Ra<63>*Rb - Rb<63>*Ra
1245 Tmp1 = SelectExpr(N.getOperand(0));
1246 Tmp2 = SelectExpr(N.getOperand(1));
1247 Tmp3 = MakeReg(MVT::i64);
1248 BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1249 unsigned V1 = MakeReg(MVT::i64);
1250 unsigned V2 = MakeReg(MVT::i64);
1251 BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31).addReg(Tmp1);
1252 BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31).addReg(Tmp2);
1253 unsigned IRes = MakeReg(MVT::i64);
1254 BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
1255 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
1256 return Result;
1257 }
Andrew Lenharth7332f3e2005-04-02 19:11:07 +00001258 case ISD::UNDEF: {
1259 BuildMI(BB, Alpha::IDEF, 0, Result);
1260 return Result;
1261 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001262
Andrew Lenharth032f2352005-02-22 21:59:48 +00001263 case ISD::DYNAMIC_STACKALLOC:
1264 // Generate both result values.
Andrew Lenharth3a7118d2005-02-23 17:33:42 +00001265 if (Result != notIn)
1266 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth032f2352005-02-22 21:59:48 +00001267 else
1268 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1269
1270 // FIXME: We are currently ignoring the requested alignment for handling
1271 // greater than the stack alignment. This will need to be revisited at some
1272 // point. Align = N.getOperand(2);
1273
1274 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1275 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1276 std::cerr << "Cannot allocate stack object with greater alignment than"
1277 << " the stack alignment yet!";
1278 abort();
1279 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001280
Andrew Lenharth032f2352005-02-22 21:59:48 +00001281 Select(N.getOperand(0));
1282 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1283 {
1284 if (CN->getValue() < 32000)
1285 {
1286 BuildMI(BB, Alpha::LDA, 2, Alpha::R30)
1287 .addImm(-CN->getValue()).addReg(Alpha::R30);
1288 } else {
1289 Tmp1 = SelectExpr(N.getOperand(1));
1290 // Subtract size from stack pointer, thereby allocating some space.
1291 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1292 }
1293 } else {
1294 Tmp1 = SelectExpr(N.getOperand(1));
1295 // Subtract size from stack pointer, thereby allocating some space.
1296 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1297 }
1298
1299 // Put a pointer to the space into the result register, by copying the stack
1300 // pointer.
Andrew Lenharth7bc47022005-02-22 23:29:25 +00001301 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
Andrew Lenharth032f2352005-02-22 21:59:48 +00001302 return Result;
1303
Andrew Lenharth33819132005-03-04 20:09:23 +00001304// case ISD::ConstantPool:
1305// Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1306// AlphaLowering.restoreGP(BB);
1307// BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(Tmp1);
1308// return Result;
Andrew Lenharth2c594352005-01-29 15:42:07 +00001309
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001310 case ISD::FrameIndex:
Andrew Lenharth032f2352005-02-22 21:59:48 +00001311 BuildMI(BB, Alpha::LDA, 2, Result)
1312 .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
1313 .addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001314 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001315
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001316 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001317 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001318 case ISD::SEXTLOAD:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001319 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001320 {
1321 // Make sure we generate both values.
1322 if (Result != notIn)
1323 ExprMap[N.getValue(1)] = notIn; // Generate the token
1324 else
1325 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001326
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001327 SDOperand Chain = N.getOperand(0);
1328 SDOperand Address = N.getOperand(1);
1329 Select(Chain);
1330
Misha Brukman4633f1c2005-04-21 23:13:11 +00001331 assert(Node->getValueType(0) == MVT::i64 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001332 "Unknown type to sign extend to.");
Andrew Lenharth03824012005-02-07 05:55:55 +00001333 if (opcode == ISD::LOAD)
1334 Opc = Alpha::LDQ;
1335 else
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001336 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
1337 default: Node->dump(); assert(0 && "Bad sign extend!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001338 case MVT::i32: Opc = Alpha::LDL;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001339 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001340 case MVT::i16: Opc = Alpha::LDWU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001341 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001342 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Misha Brukman4633f1c2005-04-21 23:13:11 +00001343 case MVT::i8: Opc = Alpha::LDBU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001344 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001345 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001346
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001347 if (Address.getOpcode() == ISD::GlobalAddress) {
1348 AlphaLowering.restoreGP(BB);
1349 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001350 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001351 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1352 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001353 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1354 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001355 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001356 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001357 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001358 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001359 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001360 BuildMI(BB, Opc, 2, Result)
1361 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1362 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001363 } else {
1364 long offset;
1365 SelectAddr(Address, Tmp1, offset);
1366 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1367 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001368 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001369 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001370
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001371 case ISD::GlobalAddress:
1372 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001373 has_sym = true;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001374 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
1375 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
1376 return Result;
1377
1378 case ISD::CALL:
1379 {
1380 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001381
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001382 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001383 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001384
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001385 //grab the arguments
1386 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001387 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001388 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001389 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001390
Andrew Lenharth684f2292005-01-30 00:35:27 +00001391 //in reg args
1392 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001393 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001394 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001395 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +00001396 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001397 Alpha::F19, Alpha::F20, Alpha::F21};
1398 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001399 default:
1400 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001401 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001402 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001403 N.getOperand(i+2).getValueType() << "\n";
1404 assert(0 && "Unknown value type for call");
1405 case MVT::i1:
1406 case MVT::i8:
1407 case MVT::i16:
1408 case MVT::i32:
1409 case MVT::i64:
1410 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1411 break;
1412 case MVT::f32:
1413 case MVT::f64:
1414 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1415 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001416 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001417 }
Andrew Lenharth684f2292005-01-30 00:35:27 +00001418 //in mem args
1419 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001420 {
1421 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001422 default:
1423 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001424 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001425 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001426 N.getOperand(i+2).getValueType() << "\n";
1427 assert(0 && "Unknown value type for call");
1428 case MVT::i1:
1429 case MVT::i8:
1430 case MVT::i16:
1431 case MVT::i32:
1432 case MVT::i64:
1433 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1434 break;
1435 case MVT::f32:
1436 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1437 break;
1438 case MVT::f64:
1439 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1440 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001441 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001442 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001443 //build the right kind of call
1444 if (GlobalAddressSDNode *GASD =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001445 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001446 {
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001447 if (GASD->getGlobal()->isExternal()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001448 //use safe calling convention
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001449 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001450 has_sym = true;
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001451 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal());
1452 } else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001453 //use PC relative branch call
Andrew Lenharth1e0d9bd2005-04-14 17:34:20 +00001454 AlphaLowering.restoreGP(BB);
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001455 BuildMI(BB, Alpha::BSR, 1, Alpha::R26).addGlobalAddress(GASD->getGlobal(),true);
1456 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001457 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001458 else if (ExternalSymbolSDNode *ESSDN =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001459 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001460 {
1461 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001462 has_sym = true;
Andrew Lenharthba05ad62005-03-30 18:22:52 +00001463 BuildMI(BB, Alpha::CALL, 1).addExternalSymbol(ESSDN->getSymbol(), true);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001464 } else {
1465 //no need to restore GP as we are doing an indirect call
1466 Tmp1 = SelectExpr(N.getOperand(1));
1467 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
1468 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
1469 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001470
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001471 //push the result into a virtual register
Misha Brukman4633f1c2005-04-21 23:13:11 +00001472
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001473 switch (Node->getValueType(0)) {
1474 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001475 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001476 case MVT::i1:
1477 case MVT::i8:
1478 case MVT::i16:
1479 case MVT::i32:
1480 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001481 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
1482 break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001483 case MVT::f32:
1484 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001485 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
1486 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001487 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001488 return Result+N.ResNo;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001489 }
1490
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001491 case ISD::SIGN_EXTEND_INREG:
1492 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001493 //do SDIV opt for all levels of ints if not dividing by a constant
1494 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
1495 && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001496 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001497 unsigned Tmp4 = MakeReg(MVT::f64);
1498 unsigned Tmp5 = MakeReg(MVT::f64);
1499 unsigned Tmp6 = MakeReg(MVT::f64);
1500 unsigned Tmp7 = MakeReg(MVT::f64);
1501 unsigned Tmp8 = MakeReg(MVT::f64);
1502 unsigned Tmp9 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001503
1504 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1505 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1506 MoveInt2FP(Tmp1, Tmp4, true);
1507 MoveInt2FP(Tmp2, Tmp5, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001508 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Tmp4);
1509 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Tmp5);
1510 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
1511 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Tmp8);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001512 MoveFP2Int(Tmp9, Result, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001513 return Result;
1514 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001515
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001516 //Alpha has instructions for a bunch of signed 32 bit stuff
1517 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001518 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001519 switch (N.getOperand(0).getOpcode()) {
1520 case ISD::ADD:
1521 case ISD::SUB:
1522 case ISD::MUL:
1523 {
1524 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
1525 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
1526 //FIXME: first check for Scaled Adds and Subs!
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001527 ConstantSDNode* CSD = NULL;
1528 if(!isMul && N.getOperand(0).getOperand(0).getOpcode() == ISD::SHL &&
1529 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(0).getOperand(1))) &&
1530 (CSD->getValue() == 2 || CSD->getValue() == 3))
1531 {
1532 bool use4 = CSD->getValue() == 2;
1533 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1534 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1535 BuildMI(BB, isAdd?(use4?Alpha::S4ADDL:Alpha::S8ADDL):(use4?Alpha::S4SUBL:Alpha::S8SUBL),
1536 2,Result).addReg(Tmp1).addReg(Tmp2);
1537 }
1538 else if(isAdd && N.getOperand(0).getOperand(1).getOpcode() == ISD::SHL &&
1539 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1).getOperand(1))) &&
1540 (CSD->getValue() == 2 || CSD->getValue() == 3))
1541 {
1542 bool use4 = CSD->getValue() == 2;
1543 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1544 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1545 BuildMI(BB, use4?Alpha::S4ADDL:Alpha::S8ADDL, 2,Result).addReg(Tmp1).addReg(Tmp2);
1546 }
1547 else if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001548 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
1549 { //Normal imm add/sub
1550 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001551 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001552 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
1553 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001554 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001555 else
1556 { //Normal add/sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001557 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULL : Alpha::SUBL);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001558 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001559 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001560 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1561 }
1562 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001563 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001564 default: break; //Fall Though;
1565 }
1566 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001567 Tmp1 = SelectExpr(N.getOperand(0));
1568 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001569 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001570 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001571 {
1572 default:
1573 Node->dump();
1574 assert(0 && "Sign Extend InReg not there yet");
1575 break;
1576 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001577 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001578 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001579 break;
1580 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001581 case MVT::i16:
1582 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
1583 break;
1584 case MVT::i8:
1585 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
1586 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +00001587 case MVT::i1:
1588 Tmp2 = MakeReg(MVT::i64);
1589 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001590 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +00001591 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001592 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001593 return Result;
1594 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001595
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001596 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001597 {
1598 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1599 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1600 bool isConst1 = false;
1601 bool isConst2 = false;
1602 int dir;
Misha Brukman7847fca2005-04-22 17:54:37 +00001603
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001604 //Tmp1 = SelectExpr(N.getOperand(0));
1605 if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001606 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
1607 isConst1 = true;
1608 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001609 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1610 isConst2 = true;
1611
1612 switch (SetCC->getCondition()) {
1613 default: Node->dump(); assert(0 && "Unknown integer comparison!");
1614 case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001615 case ISD::SETLT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001616 Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001617 case ISD::SETLE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001618 Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001619 case ISD::SETGT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001620 Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001621 case ISD::SETGE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001622 Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001623 case ISD::SETULT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001624 Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001625 case ISD::SETUGT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001626 Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001627 case ISD::SETULE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001628 Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001629 case ISD::SETUGE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001630 Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001631 case ISD::SETNE: {//Handle this one special
1632 //std::cerr << "Alpha does not have a setne.\n";
1633 //abort();
1634 Tmp1 = SelectExpr(N.getOperand(0));
1635 Tmp2 = SelectExpr(N.getOperand(1));
1636 Tmp3 = MakeReg(MVT::i64);
1637 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001638 //Remeber we have the Inv for this CC
1639 CCInvMap[N] = Tmp3;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001640 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001641 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001642 return Result;
1643 }
1644 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001645 if (dir == 1) {
1646 Tmp1 = SelectExpr(N.getOperand(0));
1647 if (isConst2) {
1648 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1649 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1650 } else {
1651 Tmp2 = SelectExpr(N.getOperand(1));
1652 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1653 }
1654 } else if (dir == 2) {
1655 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001656 if (isConst1) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001657 Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1658 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1659 } else {
1660 Tmp2 = SelectExpr(N.getOperand(0));
1661 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1662 }
1663 } else { //dir == 0
1664 if (isConst1) {
1665 Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1666 Tmp2 = SelectExpr(N.getOperand(1));
1667 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
1668 } else if (isConst2) {
1669 Tmp1 = SelectExpr(N.getOperand(0));
1670 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1671 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
1672 } else {
1673 Tmp1 = SelectExpr(N.getOperand(0));
1674 Tmp2 = SelectExpr(N.getOperand(1));
1675 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1676 }
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001677 }
1678 } else {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001679 //do the comparison
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001680 Tmp1 = MakeReg(MVT::f64);
1681 bool inv = SelectFPSetCC(N, Tmp1);
1682
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001683 //now arrange for Result (int) to have a 1 or 0
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001684 Tmp2 = MakeReg(MVT::i64);
1685 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001686 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001687 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001688 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001689 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001690 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001691 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001692
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001693 case ISD::CopyFromReg:
1694 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001695 ++count_ins;
1696
Andrew Lenharth40831c52005-01-28 06:57:18 +00001697 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001698 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001699 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001700 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001701 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001702
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001703 SDOperand Chain = N.getOperand(0);
1704
1705 Select(Chain);
1706 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1707 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1708 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1709 return Result;
1710 }
1711
Misha Brukman4633f1c2005-04-21 23:13:11 +00001712 //Most of the plain arithmetic and logic share the same form, and the same
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001713 //constant immediate test
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001714 case ISD::XOR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001715 //Match Not
1716 if (N.getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001717 cast<ConstantSDNode>(N.getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001718 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001719 Tmp1 = SelectExpr(N.getOperand(0));
1720 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1721 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001722 }
1723 //Fall through
1724 case ISD::AND:
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001725 //handle zap
1726 if (opcode == ISD::AND && N.getOperand(1).getOpcode() == ISD::Constant)
1727 {
1728 uint64_t k = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1729 unsigned int build = 0;
1730 for(int i = 0; i < 8; ++i)
1731 {
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001732 if ((k & 0x00FF) == 0x00FF)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001733 build |= 1 << i;
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001734 else if ((k & 0x00FF) != 0)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001735 { build = 0; break; }
1736 k >>= 8;
1737 }
1738 if (build)
1739 {
1740 Tmp1 = SelectExpr(N.getOperand(0));
1741 BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1742 return Result;
1743 }
1744 }
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001745 case ISD::OR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001746 //Check operand(0) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001747 if (N.getOperand(0).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001748 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001749 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001750 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001751 switch(opcode) {
1752 case ISD::AND: Opc = Alpha::BIC; break;
1753 case ISD::OR: Opc = Alpha::ORNOT; break;
1754 case ISD::XOR: Opc = Alpha::EQV; break;
1755 }
1756 Tmp1 = SelectExpr(N.getOperand(1));
1757 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1758 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1759 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001760 }
1761 //Check operand(1) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001762 if (N.getOperand(1).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001763 N.getOperand(1).getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001764 cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001765 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001766 switch(opcode) {
1767 case ISD::AND: Opc = Alpha::BIC; break;
1768 case ISD::OR: Opc = Alpha::ORNOT; break;
1769 case ISD::XOR: Opc = Alpha::EQV; break;
1770 }
1771 Tmp1 = SelectExpr(N.getOperand(0));
1772 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1773 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1774 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001775 }
1776 //Fall through
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001777 case ISD::SHL:
1778 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001779 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001780 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001781 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1782 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001783 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001784 {
1785 switch(opcode) {
1786 case ISD::AND: Opc = Alpha::ANDi; break;
1787 case ISD::OR: Opc = Alpha::BISi; break;
1788 case ISD::XOR: Opc = Alpha::XORi; break;
1789 case ISD::SHL: Opc = Alpha::SLi; break;
1790 case ISD::SRL: Opc = Alpha::SRLi; break;
1791 case ISD::SRA: Opc = Alpha::SRAi; break;
1792 case ISD::MUL: Opc = Alpha::MULQi; break;
1793 };
1794 Tmp1 = SelectExpr(N.getOperand(0));
1795 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1796 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1797 } else {
1798 switch(opcode) {
1799 case ISD::AND: Opc = Alpha::AND; break;
1800 case ISD::OR: Opc = Alpha::BIS; break;
1801 case ISD::XOR: Opc = Alpha::XOR; break;
1802 case ISD::SHL: Opc = Alpha::SL; break;
1803 case ISD::SRL: Opc = Alpha::SRL; break;
1804 case ISD::SRA: Opc = Alpha::SRA; break;
1805 case ISD::MUL: Opc = Alpha::MULQ; break;
1806 };
1807 Tmp1 = SelectExpr(N.getOperand(0));
1808 Tmp2 = SelectExpr(N.getOperand(1));
1809 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1810 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001811 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001812
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001813 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001814 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001815 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001816 bool isAdd = opcode == ISD::ADD;
1817
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001818 //first check for Scaled Adds and Subs!
1819 //Valid for add and sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001820 ConstantSDNode* CSD = NULL;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001821 if(N.getOperand(0).getOpcode() == ISD::SHL &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001822 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
1823 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001824 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001825 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001826 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001827 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) && CSD->getValue() <= 255)
1828 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1829 2, Result).addReg(Tmp2).addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001830 else {
1831 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001832 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1833 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001834 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001835 }
1836 //Position prevents subs
Andrew Lenharth273a1f92005-04-07 14:18:13 +00001837 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001838 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) &&
1839 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001840 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001841 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001842 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001843 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(0))) && CSD->getValue() <= 255)
1844 BuildMI(BB, use4?Alpha::S4ADDQi:Alpha::S8ADDQi, 2, Result).addReg(Tmp2)
1845 .addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001846 else {
1847 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001848 BuildMI(BB, use4?Alpha::S4ADDQ:Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001849 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001850 }
1851 //small addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001852 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
1853 CSD->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001854 { //Normal imm add/sub
1855 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1856 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001857 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CSD->getValue());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001858 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001859 //larger addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001860 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
1861 CSD->getSignExtended() <= 32767 &&
1862 CSD->getSignExtended() >= -32767)
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001863 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001864 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001865 Tmp2 = (long)CSD->getSignExtended();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001866 if (!isAdd)
1867 Tmp2 = -Tmp2;
1868 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001869 }
1870 //give up and do the operation
1871 else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001872 //Normal add/sub
1873 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1874 Tmp1 = SelectExpr(N.getOperand(0));
1875 Tmp2 = SelectExpr(N.getOperand(1));
1876 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1877 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001878 return Result;
1879 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001880
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001881 case ISD::SDIV:
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001882 {
Andrew Lenhartha565c272005-04-06 22:03:13 +00001883 ConstantSDNode* CSD;
1884 //check if we can convert into a shift!
1885 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
1886 (int64_t)CSD->getSignExtended() != 0 &&
1887 ExactLog2(abs((int64_t)CSD->getSignExtended())) != 0)
1888 {
1889 unsigned k = ExactLog2(abs(CSD->getSignExtended()));
1890 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenhartha565c272005-04-06 22:03:13 +00001891 if (k == 1)
1892 Tmp2 = Tmp1;
1893 else
1894 {
1895 Tmp2 = MakeReg(MVT::i64);
1896 BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
1897 }
1898 Tmp3 = MakeReg(MVT::i64);
1899 BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
1900 unsigned Tmp4 = MakeReg(MVT::i64);
1901 BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
1902 if ((int64_t)CSD->getSignExtended() > 0)
1903 BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
1904 else
1905 {
1906 unsigned Tmp5 = MakeReg(MVT::i64);
1907 BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
1908 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
1909 }
1910 return Result;
1911 }
1912 }
1913 //Else fall through
1914
1915 case ISD::UDIV:
1916 {
1917 ConstantSDNode* CSD;
1918 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
1919 ((int64_t)CSD->getSignExtended() >= 2 ||
1920 (int64_t)CSD->getSignExtended() <= -2))
1921 {
1922 // If this is a divide by constant, we can emit code using some magic
1923 // constants to implement it as a multiply instead.
1924 ExprMap.erase(N);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001925 if (opcode == ISD::SDIV)
Andrew Lenhartha565c272005-04-06 22:03:13 +00001926 return SelectExpr(BuildSDIVSequence(N));
1927 else
1928 return SelectExpr(BuildUDIVSequence(N));
1929 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001930 }
1931 //else fall though
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001932 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +00001933 case ISD::SREM:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001934 //FIXME: alpha really doesn't support any of these operations,
Andrew Lenharth40831c52005-01-28 06:57:18 +00001935 // the ops are expanded into special library calls with
1936 // special calling conventions
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001937 //Restore GP because it is a call after all...
Andrew Lenharth40831c52005-01-28 06:57:18 +00001938 switch(opcode) {
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001939 case ISD::UREM: Opc = Alpha::REMQU; break;
1940 case ISD::SREM: Opc = Alpha::REMQ; break;
1941 case ISD::UDIV: Opc = Alpha::DIVQU; break;
1942 case ISD::SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001943 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001944 Tmp1 = SelectExpr(N.getOperand(0));
1945 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth33819132005-03-04 20:09:23 +00001946 //set up regs explicitly (helps Reg alloc)
1947 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001948 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001949 AlphaLowering.restoreGP(BB);
Andrew Lenharth33819132005-03-04 20:09:23 +00001950 BuildMI(BB, Opc, 2).addReg(Alpha::R24).addReg(Alpha::R25);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001951 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001952 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001953
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001954 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001955 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001956 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001957 assert (DestType == MVT::i64 && "only quads can be loaded to");
1958 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00001959 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001960 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001961 if (SrcType == MVT::f32)
Misha Brukman7847fca2005-04-22 17:54:37 +00001962 {
1963 Tmp2 = MakeReg(MVT::f64);
1964 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
1965 Tmp1 = Tmp2;
1966 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001967 Tmp2 = MakeReg(MVT::f64);
1968 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001969 MoveFP2Int(Tmp2, Result, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001970
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001971 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001972 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001973
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001974 case ISD::SELECT:
1975 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001976 //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 +00001977 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001978 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1979 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001980 // Get the condition into the zero flag.
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001981 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001982
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001983 SDOperand CC = N.getOperand(0);
1984 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
1985
Misha Brukman4633f1c2005-04-21 23:13:11 +00001986 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001987 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
1988 { //FP Setcc -> Int Select
Misha Brukman7847fca2005-04-22 17:54:37 +00001989 Tmp1 = MakeReg(MVT::f64);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001990 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1991 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Misha Brukman7847fca2005-04-22 17:54:37 +00001992 bool inv = SelectFPSetCC(CC, Tmp1);
1993 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
1994 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
1995 return Result;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001996 }
1997 if (CC.getOpcode() == ISD::SETCC) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001998 //Int SetCC -> Select
1999 //Dropping the CC is only useful if we are comparing to 0
2000 if((SetCC->getOperand(1).getOpcode() == ISD::Constant &&
2001 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0) ||
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002002 (SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2003 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0))
2004 {
2005 //figure out a few things
2006 bool LeftZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2007 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
2008 bool RightZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
2009 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
2010 bool LeftConst = N.getOperand(1).getOpcode() == ISD::Constant &&
2011 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255;
2012 bool RightConst = N.getOperand(2).getOpcode() == ISD::Constant &&
2013 cast<ConstantSDNode>(N.getOperand(2))->getValue() <= 255;
2014 bool useImm = LeftConst || RightConst;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002015
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002016 //Fix up CC
2017 ISD::CondCode cCode= SetCC->getCondition();
2018 if (RightConst && !LeftConst) //Invert sense to get Imm field right
2019 cCode = ISD::getSetCCInverse(cCode, true);
2020 if (LeftZero && !RightZero) //Swap Operands
2021 cCode = ISD::getSetCCSwappedOperands(cCode);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002022
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002023 //Choose the CMOV
2024 switch (cCode) {
2025 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
2026 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
2027 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
2028 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
2029 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
2030 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
2031 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
2032 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2033 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break; //Technically you could have this CC
2034 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
2035 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2036 }
2037 if(LeftZero && !RightZero) //swap Operands
2038 Tmp1 = SelectExpr(SetCC->getOperand(1)); //Cond
2039 else
2040 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
2041
2042 if (LeftConst) {
2043 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2044 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
Misha Brukman7847fca2005-04-22 17:54:37 +00002045 .addImm(cast<ConstantSDNode>(N.getOperand(1))->getValue())
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002046 .addReg(Tmp1);
2047 } else if (RightConst) {
2048 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
2049 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
Misha Brukman7847fca2005-04-22 17:54:37 +00002050 .addImm(cast<ConstantSDNode>(N.getOperand(2))->getValue())
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002051 .addReg(Tmp1);
2052 } else {
2053 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2054 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2055 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
2056 }
2057 return Result;
2058 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002059 //Otherwise, fall though
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002060 }
2061 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002062 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2063 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthe76797c2005-02-01 20:40:27 +00002064 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002065
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002066 return Result;
2067 }
2068
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002069 case ISD::Constant:
2070 {
Andrew Lenharthc0513832005-03-29 19:24:04 +00002071 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002072 if (val <= IMM_HIGH && val >= IMM_LOW) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002073 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002074 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002075 else if (val <= (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT &&
2076 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
2077 Tmp1 = MakeReg(MVT::i64);
2078 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val)).addReg(Alpha::R31);
2079 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002080 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002081 else {
2082 MachineConstantPool *CP = BB->getParent()->getConstantPool();
2083 ConstantUInt *C = ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
2084 unsigned CPI = CP->getConstantPoolIndex(C);
2085 AlphaLowering.restoreGP(BB);
2086 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CPI);
2087 }
2088 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002089 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002090 }
2091
2092 return 0;
2093}
2094
2095void ISel::Select(SDOperand N) {
2096 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00002097 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002098
Nate Begeman85fdeb22005-03-24 04:39:54 +00002099 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002100 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002101
2102 SDNode *Node = N.Val;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002103
Andrew Lenharth760270d2005-02-07 23:02:23 +00002104 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002105
2106 default:
2107 Node->dump(); std::cerr << "\n";
2108 assert(0 && "Node not handled yet!");
2109
2110 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00002111 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002112 return;
2113 }
2114
2115 case ISD::BR: {
2116 MachineBasicBlock *Dest =
2117 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2118
2119 Select(N.getOperand(0));
2120 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
2121 return;
2122 }
2123
2124 case ISD::ImplicitDef:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002125 ++count_ins;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002126 Select(N.getOperand(0));
2127 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
2128 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002129
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002130 case ISD::EntryToken: return; // Noop
2131
2132 case ISD::TokenFactor:
2133 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2134 Select(Node->getOperand(i));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002135
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002136 //N.Val->dump(); std::cerr << "\n";
2137 //assert(0 && "Node not handled yet!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00002138
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002139 return;
2140
2141 case ISD::CopyToReg:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002142 ++count_outs;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002143 Select(N.getOperand(0));
2144 Tmp1 = SelectExpr(N.getOperand(1));
2145 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002146
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002147 if (Tmp1 != Tmp2) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002148 if (N.getOperand(1).getValueType() == MVT::f64 ||
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002149 N.getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00002150 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2151 else
2152 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002153 }
2154 return;
2155
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002156 case ISD::RET:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002157 ++count_outs;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002158 switch (N.getNumOperands()) {
2159 default:
2160 std::cerr << N.getNumOperands() << "\n";
2161 for (unsigned i = 0; i < N.getNumOperands(); ++i)
2162 std::cerr << N.getOperand(i).getValueType() << "\n";
2163 Node->dump();
2164 assert(0 && "Unknown return instruction!");
2165 case 2:
2166 Select(N.getOperand(0));
2167 Tmp1 = SelectExpr(N.getOperand(1));
2168 switch (N.getOperand(1).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002169 default: Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002170 assert(0 && "All other types should have been promoted!!");
2171 case MVT::f64:
2172 case MVT::f32:
2173 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
2174 break;
2175 case MVT::i32:
2176 case MVT::i64:
2177 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
2178 break;
2179 }
2180 break;
2181 case 1:
2182 Select(N.getOperand(0));
2183 break;
2184 }
2185 //Tmp2 = AlphaLowering.getRetAddr();
2186 //BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(Tmp2).addReg(Tmp2);
2187 BuildMI(BB, Alpha::RETURN, 0); // Just emit a 'ret' instruction
2188 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002189
Misha Brukman4633f1c2005-04-21 23:13:11 +00002190 case ISD::TRUNCSTORE:
2191 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002192 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002193 SDOperand Chain = N.getOperand(0);
2194 SDOperand Value = N.getOperand(1);
2195 SDOperand Address = N.getOperand(2);
2196 Select(Chain);
2197
2198 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00002199
2200 if (opcode == ISD::STORE) {
2201 switch(Value.getValueType()) {
2202 default: assert(0 && "unknown Type in store");
2203 case MVT::i64: Opc = Alpha::STQ; break;
2204 case MVT::f64: Opc = Alpha::STT; break;
2205 case MVT::f32: Opc = Alpha::STS; break;
2206 }
2207 } else { //ISD::TRUNCSTORE
2208 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2209 default: assert(0 && "unknown Type in store");
2210 case MVT::i1: //FIXME: DAG does not promote this load
2211 case MVT::i8: Opc = Alpha::STB; break;
2212 case MVT::i16: Opc = Alpha::STW; break;
2213 case MVT::i32: Opc = Alpha::STL; break;
2214 }
Andrew Lenharth65838902005-02-06 16:22:15 +00002215 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00002216
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002217 if (Address.getOpcode() == ISD::GlobalAddress)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002218 {
2219 AlphaLowering.restoreGP(BB);
2220 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002221 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002222 BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
2223 }
Andrew Lenharth05380342005-02-07 05:07:00 +00002224 else if(Address.getOpcode() == ISD::FrameIndex)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002225 {
Andrew Lenharth032f2352005-02-22 21:59:48 +00002226 BuildMI(BB, Opc, 3).addReg(Tmp1)
2227 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
2228 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002229 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002230 else
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002231 {
2232 long offset;
2233 SelectAddr(Address, Tmp2, offset);
2234 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2235 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002236 return;
2237 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002238
2239 case ISD::EXTLOAD:
2240 case ISD::SEXTLOAD:
2241 case ISD::ZEXTLOAD:
2242 case ISD::LOAD:
2243 case ISD::CopyFromReg:
2244 case ISD::CALL:
Andrew Lenharth032f2352005-02-22 21:59:48 +00002245 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002246 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002247 SelectExpr(N);
2248 return;
2249
Chris Lattner16cd04d2005-05-12 23:24:06 +00002250 case ISD::CALLSEQ_START:
2251 case ISD::CALLSEQ_END:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002252 Select(N.getOperand(0));
2253 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002254
Chris Lattner16cd04d2005-05-12 23:24:06 +00002255 Opc = N.getOpcode() == ISD::CALLSEQ_START ? Alpha::ADJUSTSTACKDOWN :
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002256 Alpha::ADJUSTSTACKUP;
2257 BuildMI(BB, Opc, 1).addImm(Tmp1);
2258 return;
Andrew Lenharth95762122005-03-31 21:24:06 +00002259
2260 case ISD::PCMARKER:
2261 Select(N.getOperand(0)); //Chain
2262 BuildMI(BB, Alpha::PCLABEL, 2).addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
2263 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002264 }
2265 assert(0 && "Should not be reached!");
2266}
2267
2268
2269/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
2270/// into a machine code representation using pattern matching and a machine
2271/// description file.
2272///
2273FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002274 return new ISel(TM);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002275}
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002276