blob: 31b6fd6d0135c21c0553ed6358f959d0002caf1e [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);
Misha Brukman4633f1c2005-04-21 23:13:11 +000039 cl::opt<bool> EnableAlphaFTOI("enable-alpha-ftoi",
40 cl::desc("Enable use of ftoi* and itof* instructions (ev6 and higher)"),
Andrew Lenharth95762122005-03-31 21:24:06 +000041 cl::Hidden);
Misha Brukman4633f1c2005-04-21 23:13:11 +000042 cl::opt<bool> EnableAlphaCount("enable-alpha-count",
43 cl::desc("Print estimates on live ins and outs"),
Andrew Lenhartha32b9e32005-04-08 17:28:49 +000044 cl::Hidden);
Andrew Lenharth95762122005-03-31 21:24:06 +000045}
46
Andrew Lenharth304d0f32005-01-22 23:41:55 +000047//===----------------------------------------------------------------------===//
48// AlphaTargetLowering - Alpha Implementation of the TargetLowering interface
49namespace {
50 class AlphaTargetLowering : public TargetLowering {
51 int VarArgsFrameIndex; // FrameIndex for start of varargs area.
52 unsigned GP; //GOT vreg
53 public:
54 AlphaTargetLowering(TargetMachine &TM) : TargetLowering(TM) {
55 // Set up the TargetLowering object.
Andrew Lenharth3d65d312005-01-27 03:49:45 +000056 //I am having problems with shr n ubyte 1
Andrew Lenharth879ef222005-02-02 17:00:21 +000057 setShiftAmountType(MVT::i64);
58 setSetCCResultType(MVT::i64);
Andrew Lenharthd3355e22005-04-07 20:11:32 +000059 setSetCCResultContents(ZeroOrOneSetCCResult);
Misha Brukman4633f1c2005-04-21 23:13:11 +000060
Andrew Lenharth304d0f32005-01-22 23:41:55 +000061 addRegisterClass(MVT::i64, Alpha::GPRCRegisterClass);
62 addRegisterClass(MVT::f64, Alpha::FPRCRegisterClass);
Andrew Lenharth3d65d312005-01-27 03:49:45 +000063 addRegisterClass(MVT::f32, Alpha::FPRCRegisterClass);
Misha Brukman4633f1c2005-04-21 23:13:11 +000064
Chris Lattnerda4d4692005-04-09 03:22:37 +000065 setOperationAction(ISD::BRCONDTWOWAY, MVT::Other, Expand);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000066 setOperationAction(ISD::EXTLOAD , MVT::i1 , Promote);
67 setOperationAction(ISD::EXTLOAD , MVT::f32 , Promote);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000068
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000069 setOperationAction(ISD::ZEXTLOAD , MVT::i1 , Expand);
70 setOperationAction(ISD::ZEXTLOAD , MVT::i32 , Expand);
Andrew Lenharth2f8fb772005-01-25 00:35:34 +000071
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000072 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
73 setOperationAction(ISD::SEXTLOAD , MVT::i8 , Expand);
74 setOperationAction(ISD::SEXTLOAD , MVT::i16 , Expand);
Andrew Lenharth304d0f32005-01-22 23:41:55 +000075
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000076 setOperationAction(ISD::SREM , MVT::f32 , Expand);
77 setOperationAction(ISD::SREM , MVT::f64 , Expand);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +000078
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000079 //If this didn't legalize into a div....
80 // setOperationAction(ISD::SREM , MVT::i64, Expand);
81 // setOperationAction(ISD::UREM , MVT::i64, Expand);
82
83 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
84 setOperationAction(ISD::MEMSET , MVT::Other, Expand);
85 setOperationAction(ISD::MEMCPY , MVT::Other, Expand);
Andrew Lenharth9818c052005-02-05 13:19:12 +000086
Chris Lattner17234b72005-04-30 04:26:06 +000087 // We don't support sin/cos/sqrt
88 setOperationAction(ISD::FSIN , MVT::f64, Expand);
89 setOperationAction(ISD::FCOS , MVT::f64, Expand);
90 setOperationAction(ISD::FSQRT, MVT::f64, Expand);
91 setOperationAction(ISD::FSIN , MVT::f32, Expand);
92 setOperationAction(ISD::FCOS , MVT::f32, Expand);
93 setOperationAction(ISD::FSQRT, MVT::f32, Expand);
94
Andrew Lenharth33819132005-03-04 20:09:23 +000095 //Doesn't work yet
Chris Lattner17234b72005-04-30 04:26:06 +000096 setOperationAction(ISD::SETCC, MVT::f32, Promote);
Andrew Lenharth572af902005-02-14 05:41:43 +000097
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000098 computeRegisterProperties();
Misha Brukman4633f1c2005-04-21 23:13:11 +000099
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000100 addLegalFPImmediate(+0.0); //F31
101 addLegalFPImmediate(-0.0); //-F31
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000102 }
103
104 /// LowerArguments - This hook must be implemented to indicate how we should
105 /// lower the arguments for the specified function, into the specified DAG.
106 virtual std::vector<SDOperand>
107 LowerArguments(Function &F, SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000108
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000109 /// LowerCallTo - This hook lowers an abstract call to a function into an
110 /// actual call.
111 virtual std::pair<SDOperand, SDOperand>
Nate Begeman8e21e712005-03-26 01:29:23 +0000112 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
113 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000114
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000115 virtual std::pair<SDOperand, SDOperand>
116 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000117
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000118 virtual std::pair<SDOperand,SDOperand>
119 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
120 const Type *ArgTy, SelectionDAG &DAG);
121
122 virtual std::pair<SDOperand, SDOperand>
123 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
124 SelectionDAG &DAG);
125
126 void restoreGP(MachineBasicBlock* BB)
127 {
128 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
129 }
130 };
131}
132
133//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
134
135//For now, just use variable size stack frame format
136
137//In a standard call, the first six items are passed in registers $16
138//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
139//of argument-to-register correspondence.) The remaining items are
140//collected in a memory argument list that is a naturally aligned
141//array of quadwords. In a standard call, this list, if present, must
142//be passed at 0(SP).
Misha Brukman7847fca2005-04-22 17:54:37 +0000143//7 ... n 0(SP) ... (n-7)*8(SP)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000144
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000145// //#define FP $15
146// //#define RA $26
147// //#define PV $27
148// //#define GP $29
149// //#define SP $30
Misha Brukman4633f1c2005-04-21 23:13:11 +0000150
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000151std::vector<SDOperand>
Misha Brukman4633f1c2005-04-21 23:13:11 +0000152AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000153{
154 std::vector<SDOperand> ArgValues;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000155 std::vector<SDOperand> LS;
156 SDOperand Chain = DAG.getRoot();
157
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000158 // assert(0 && "TODO");
159 MachineFunction &MF = DAG.getMachineFunction();
Andrew Lenharth05380342005-02-07 05:07:00 +0000160 MachineFrameInfo*MFI = MF.getFrameInfo();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000161
162 GP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
163 MachineBasicBlock& BB = MF.front();
164
165 //Handle the return address
166 //BuildMI(&BB, Alpha::IDEF, 0, Alpha::R26);
167
Misha Brukman4633f1c2005-04-21 23:13:11 +0000168 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000169 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000170 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000171 Alpha::F19, Alpha::F20, Alpha::F21};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000172 int count = 0;
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000173
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000174 //Def incoming registers
175 {
176 Function::arg_iterator I = F.arg_begin();
177 Function::arg_iterator E = F.arg_end();
178 for (int i = 0; i < 6; ++i)
179 {
180 if (F.isVarArg()) {
Andrew Lenharth0b04b5d2005-04-12 17:47:57 +0000181 MF.addLiveIn(args_int[i]);
182 MF.addLiveIn(args_float[i]);
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000183// BuildMI(&BB, Alpha::IDEF, 0, args_int[i]);
184// BuildMI(&BB, Alpha::IDEF, 0, args_float[i]);
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000185 } else if (I != E)
186 {
187 if(MVT::isInteger(getValueType(I->getType())))
Andrew Lenharth0b04b5d2005-04-12 17:47:57 +0000188 MF.addLiveIn(args_int[i]);
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000189// BuildMI(&BB, Alpha::IDEF, 0, args_int[i]);
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000190 else
Andrew Lenharth0b04b5d2005-04-12 17:47:57 +0000191 MF.addLiveIn(args_float[i]);
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000192// BuildMI(&BB, Alpha::IDEF, 0, args_float[i]);
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000193 ++I;
194 }
195 }
196 }
197
Andrew Lenharth0b04b5d2005-04-12 17:47:57 +0000198 MF.addLiveIn(Alpha::R29);
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000199// BuildMI(&BB, Alpha::IDEF, 0, Alpha::R29);
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000200 BuildMI(&BB, Alpha::BIS, 2, GP).addReg(Alpha::R29).addReg(Alpha::R29);
201
Chris Lattnere4d5c442005-03-15 04:54:21 +0000202 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000203 {
204 SDOperand newroot, argt;
205 if (count < 6) {
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000206 unsigned Vreg;
207 MVT::ValueType VT = getValueType(I->getType());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000208 switch (getValueType(I->getType())) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000209 default:
210 std::cerr << "Unknown Type " << VT << "\n";
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000211 abort();
212 case MVT::f64:
213 case MVT::f32:
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000214 Vreg = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(VT));
215 BuildMI(&BB, Alpha::CPYS, 2, Vreg).addReg(args_float[count]).addReg(args_float[count]);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000216 argt = newroot = DAG.getCopyFromReg(Vreg,
217 getValueType(I->getType()),
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000218 Chain);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000219 break;
220 case MVT::i1:
221 case MVT::i8:
222 case MVT::i16:
223 case MVT::i32:
224 case MVT::i64:
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000225 Vreg = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
226 BuildMI(&BB, Alpha::BIS, 2, Vreg).addReg(args_int[count]).addReg(args_int[count]);
227 argt = newroot = DAG.getCopyFromReg(Vreg, MVT::i64, Chain);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000228 if (getValueType(I->getType()) != MVT::i64)
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000229 argt = DAG.getNode(ISD::TRUNCATE, getValueType(I->getType()), newroot);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000230 break;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000231 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000232 } else { //more args
233 // Create the frame index object for this incoming parameter...
234 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000235
236 // Create the SelectionDAG nodes corresponding to a load
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000237 //from this parameter
238 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000239 argt = newroot = DAG.getLoad(getValueType(I->getType()),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000240 DAG.getEntryNode(), FIN, DAG.getSrcValue(NULL));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000241 }
Andrew Lenharth032f2352005-02-22 21:59:48 +0000242 ++count;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000243 LS.push_back(newroot.getValue(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000244 ArgValues.push_back(argt);
245 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000246
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000247 // If the functions takes variable number of arguments, copy all regs to stack
Misha Brukman4633f1c2005-04-21 23:13:11 +0000248 if (F.isVarArg())
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000249 for (int i = 0; i < 6; ++i)
250 {
251 unsigned Vreg = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
252 BuildMI(&BB, Alpha::BIS, 2, Vreg).addReg(args_int[i]).addReg(args_int[i]);
253 SDOperand argt = DAG.getCopyFromReg(Vreg, MVT::i64, Chain);
254 int FI = MFI->CreateFixedObject(8, -8 * (6 - i));
255 SDOperand SDFI = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000256 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, argt, SDFI, DAG.getSrcValue(NULL)));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000257
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000258 Vreg = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::f64));
259 BuildMI(&BB, Alpha::CPYS, 2, Vreg).addReg(args_float[i]).addReg(args_float[i]);
260 argt = DAG.getCopyFromReg(Vreg, MVT::f64, Chain);
261 FI = MFI->CreateFixedObject(8, - 8 * (12 - i));
262 SDFI = DAG.getFrameIndex(FI, MVT::i64);
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000263 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, argt, SDFI, DAG.getSrcValue(NULL)));
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000264 }
265
266 // If the function takes variable number of arguments, make a frame index for
267 // the start of the first arg value... for expansion of llvm.va_start.
268 // if (F.isVarArg())
269 // VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
270
271 //Set up a token factor with all the stack traffic
272 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, LS));
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000273
274 // Finally, inform the code generator which regs we return values in.
275 switch (getValueType(F.getReturnType())) {
276 default: assert(0 && "Unknown type!");
277 case MVT::isVoid: break;
278 case MVT::i1:
279 case MVT::i8:
280 case MVT::i16:
281 case MVT::i32:
282 case MVT::i64:
283 MF.addLiveOut(Alpha::R0);
284 break;
285 case MVT::f32:
286 case MVT::f64:
287 MF.addLiveOut(Alpha::F0);
288 break;
289 }
290
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000291 //return the arguments
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000292 return ArgValues;
293}
294
295std::pair<SDOperand, SDOperand>
296AlphaTargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000297 const Type *RetTy, bool isVarArg,
298 SDOperand Callee, ArgListTy &Args,
299 SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000300 int NumBytes = 0;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000301 if (Args.size() > 6)
302 NumBytes = (Args.size() - 6) * 8;
303
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000304 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000305 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000306 std::vector<SDOperand> args_to_use;
307 for (unsigned i = 0, e = Args.size(); i != e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000308 {
309 switch (getValueType(Args[i].second)) {
310 default: assert(0 && "Unexpected ValueType for argument!");
311 case MVT::i1:
312 case MVT::i8:
313 case MVT::i16:
314 case MVT::i32:
315 // Promote the integer to 64 bits. If the input type is signed use a
316 // sign extend, otherwise use a zero extend.
317 if (Args[i].second->isSigned())
318 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
319 else
320 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
321 break;
322 case MVT::i64:
323 case MVT::f64:
324 case MVT::f32:
325 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000326 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000327 args_to_use.push_back(Args[i].first);
328 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000329
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000330 std::vector<MVT::ValueType> RetVals;
331 MVT::ValueType RetTyVT = getValueType(RetTy);
332 if (RetTyVT != MVT::isVoid)
333 RetVals.push_back(RetTyVT);
334 RetVals.push_back(MVT::Other);
335
Misha Brukman4633f1c2005-04-21 23:13:11 +0000336 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000337 Chain, Callee, args_to_use), 0);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000338 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
339 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
340 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000341 return std::make_pair(TheCall, Chain);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000342}
343
344std::pair<SDOperand, SDOperand>
345AlphaTargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
346 //vastart just returns the address of the VarArgsFrameIndex slot.
347 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64), Chain);
348}
349
350std::pair<SDOperand,SDOperand> AlphaTargetLowering::
351LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000352 const Type *ArgTy, SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000353 abort();
354}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000355
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000356
357std::pair<SDOperand, SDOperand> AlphaTargetLowering::
358LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
359 SelectionDAG &DAG) {
360 abort();
361}
362
363
364
365
366
367namespace {
368
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000369//===--------------------------------------------------------------------===//
370/// ISel - Alpha specific code to select Alpha machine instructions for
371/// SelectionDAG operations.
372//===--------------------------------------------------------------------===//
373class ISel : public SelectionDAGISel {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000374
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000375 /// AlphaLowering - This object fully describes how to lower LLVM code to an
376 /// Alpha-specific SelectionDAG.
377 AlphaTargetLowering AlphaLowering;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000378
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000379 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
380 // for sdiv and udiv until it is put into the future
381 // dag combiner.
382
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000383 /// ExprMap - As shared expressions are codegen'd, we keep track of which
384 /// vreg the value is produced in, so we only emit one copy of each compiled
385 /// tree.
386 static const unsigned notIn = (unsigned)(-1);
387 std::map<SDOperand, unsigned> ExprMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000388
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000389 //CCInvMap sometimes (SetNE) we have the inverse CC code for free
390 std::map<SDOperand, unsigned> CCInvMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000391
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000392 int count_ins;
393 int count_outs;
394 bool has_sym;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000395 int max_depth;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000396
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000397public:
Misha Brukman4633f1c2005-04-21 23:13:11 +0000398 ISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering), AlphaLowering(TM)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000399 {}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000400
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000401 /// InstructionSelectBasicBlock - This callback is invoked by
402 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
403 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
Andrew Lenharth032f2352005-02-22 21:59:48 +0000404 DEBUG(BB->dump());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000405 count_ins = 0;
406 count_outs = 0;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000407 max_depth = 0;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000408 has_sym = false;
409
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000410 // Codegen the basic block.
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000411 ISelDAG = &DAG;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000412 max_depth = DAG.getRoot().getNodeDepth();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000413 Select(DAG.getRoot());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000414
415 if(has_sym)
416 ++count_ins;
417 if(EnableAlphaCount)
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000418 std::cerr << "COUNT: " << BB->getParent()->getFunction ()->getName() << " "
419 << BB->getNumber() << " "
420 << max_depth << " "
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000421 << count_ins << " "
422 << count_outs << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000423
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000424 // Clear state used for selection.
425 ExprMap.clear();
426 CCInvMap.clear();
427 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000428
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000429 unsigned SelectExpr(SDOperand N);
430 unsigned SelectExprFP(SDOperand N, unsigned Result);
431 void Select(SDOperand N);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000432
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000433 void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
434 void SelectBranchCC(SDOperand N);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000435 void MoveFP2Int(unsigned src, unsigned dst, bool isDouble);
436 void MoveInt2FP(unsigned src, unsigned dst, bool isDouble);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000437 //returns whether the sense of the comparison was inverted
438 bool SelectFPSetCC(SDOperand N, unsigned dst);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000439
440 // dag -> dag expanders for integer divide by constant
441 SDOperand BuildSDIVSequence(SDOperand N);
442 SDOperand BuildUDIVSequence(SDOperand N);
443
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000444};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000445}
446
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000447//Factorize a number using the list of constants
448static bool factorize(int v[], int res[], int size, uint64_t c)
449{
450 bool cont = true;
451 while (c != 1 && cont)
452 {
453 cont = false;
454 for(int i = 0; i < size; ++i)
455 {
456 if (c % v[i] == 0)
457 {
458 c /= v[i];
459 ++res[i];
460 cont=true;
461 }
462 }
463 }
464 return c == 1;
465}
466
467
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000468//Shamelessly adapted from PPC32
Misha Brukman4633f1c2005-04-21 23:13:11 +0000469// Structure used to return the necessary information to codegen an SDIV as
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000470// a multiply.
471struct ms {
472 int64_t m; // magic number
473 int64_t s; // shift amount
474};
475
476struct mu {
477 uint64_t m; // magic number
478 int64_t a; // add indicator
479 int64_t s; // shift amount
480};
481
482/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukman4633f1c2005-04-21 23:13:11 +0000483/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000484/// or -1.
485static struct ms magic(int64_t d) {
486 int64_t p;
487 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
488 const uint64_t two63 = 9223372036854775808ULL; // 2^63
489 struct ms mag;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000490
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000491 ad = abs(d);
492 t = two63 + ((uint64_t)d >> 63);
493 anc = t - 1 - t%ad; // absolute value of nc
Andrew Lenharth320174f2005-04-07 17:19:16 +0000494 p = 63; // initialize p
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000495 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
496 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
497 q2 = two63/ad; // initialize q2 = 2p/abs(d)
498 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
499 do {
500 p = p + 1;
501 q1 = 2*q1; // update q1 = 2p/abs(nc)
502 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
503 if (r1 >= anc) { // must be unsigned comparison
504 q1 = q1 + 1;
505 r1 = r1 - anc;
506 }
507 q2 = 2*q2; // update q2 = 2p/abs(d)
508 r2 = 2*r2; // update r2 = rem(2p/abs(d))
509 if (r2 >= ad) { // must be unsigned comparison
510 q2 = q2 + 1;
511 r2 = r2 - ad;
512 }
513 delta = ad - r2;
514 } while (q1 < delta || (q1 == delta && r1 == 0));
515
516 mag.m = q2 + 1;
517 if (d < 0) mag.m = -mag.m; // resulting magic number
518 mag.s = p - 64; // resulting shift
519 return mag;
520}
521
522/// magicu - calculate the magic numbers required to codegen an integer udiv as
523/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
524static struct mu magicu(uint64_t d)
525{
526 int64_t p;
527 uint64_t nc, delta, q1, r1, q2, r2;
528 struct mu magu;
529 magu.a = 0; // initialize "add" indicator
530 nc = - 1 - (-d)%d;
Andrew Lenharth320174f2005-04-07 17:19:16 +0000531 p = 63; // initialize p
532 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
533 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
534 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
535 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000536 do {
537 p = p + 1;
538 if (r1 >= nc - r1 ) {
539 q1 = 2*q1 + 1; // update q1
540 r1 = 2*r1 - nc; // update r1
541 }
542 else {
543 q1 = 2*q1; // update q1
544 r1 = 2*r1; // update r1
545 }
546 if (r2 + 1 >= d - r2) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000547 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000548 q2 = 2*q2 + 1; // update q2
549 r2 = 2*r2 + 1 - d; // update r2
550 }
551 else {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000552 if (q2 >= 0x8000000000000000ull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000553 q2 = 2*q2; // update q2
554 r2 = 2*r2 + 1; // update r2
555 }
556 delta = d - 1 - r2;
557 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
558 magu.m = q2 + 1; // resulting magic number
Andrew Lenharth320174f2005-04-07 17:19:16 +0000559 magu.s = p - 64; // resulting shift
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000560 return magu;
561}
562
563/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
564/// return a DAG expression to select that will generate the same value by
565/// multiplying by a magic number. See:
566/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
567SDOperand ISel::BuildSDIVSequence(SDOperand N) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000568 int64_t d = (int64_t)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000569 ms magics = magic(d);
570 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000571 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000572 ISelDAG->getConstant(magics.m, MVT::i64));
573 // If d > 0 and m < 0, add the numerator
574 if (d > 0 && magics.m < 0)
575 Q = ISelDAG->getNode(ISD::ADD, MVT::i64, Q, N.getOperand(0));
576 // If d < 0 and m > 0, subtract the numerator.
577 if (d < 0 && magics.m > 0)
578 Q = ISelDAG->getNode(ISD::SUB, MVT::i64, Q, N.getOperand(0));
579 // Shift right algebraic if shift value is nonzero
580 if (magics.s > 0)
Misha Brukman4633f1c2005-04-21 23:13:11 +0000581 Q = ISelDAG->getNode(ISD::SRA, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000582 ISelDAG->getConstant(magics.s, MVT::i64));
583 // Extract the sign bit and add it to the quotient
Misha Brukman4633f1c2005-04-21 23:13:11 +0000584 SDOperand T =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000585 ISelDAG->getNode(ISD::SRL, MVT::i64, Q, ISelDAG->getConstant(63, MVT::i64));
586 return ISelDAG->getNode(ISD::ADD, MVT::i64, Q, T);
587}
588
589/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
590/// return a DAG expression to select that will generate the same value by
591/// multiplying by a magic number. See:
592/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
593SDOperand ISel::BuildUDIVSequence(SDOperand N) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000594 unsigned d =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000595 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
596 mu magics = magicu(d);
597 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000598 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000599 ISelDAG->getConstant(magics.m, MVT::i64));
600 if (magics.a == 0) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000601 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000602 ISelDAG->getConstant(magics.s, MVT::i64));
603 } else {
604 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i64, N.getOperand(0), Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000605 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000606 ISelDAG->getConstant(1, MVT::i64));
607 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i64, NPQ, Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000608 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000609 ISelDAG->getConstant(magics.s-1, MVT::i64));
610 }
611 return Q;
612}
613
Andrew Lenhartha565c272005-04-06 22:03:13 +0000614//From PPC32
615/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
616/// returns zero when the input is not exactly a power of two.
617static unsigned ExactLog2(uint64_t Val) {
618 if (Val == 0 || (Val & (Val-1))) return 0;
619 unsigned Count = 0;
620 while (Val != 1) {
621 Val >>= 1;
622 ++Count;
623 }
624 return Count;
625}
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000626
627
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000628//These describe LDAx
Andrew Lenharthc0513832005-03-29 19:24:04 +0000629static const int IMM_LOW = -32768;
630static const int IMM_HIGH = 32767;
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000631static const int IMM_MULT = 65536;
632
633static long getUpper16(long l)
634{
635 long y = l / IMM_MULT;
636 if (l % IMM_MULT > IMM_HIGH)
637 ++y;
638 return y;
639}
640
641static long getLower16(long l)
642{
643 long h = getUpper16(l);
644 return l - h * IMM_MULT;
645}
646
Andrew Lenharth65838902005-02-06 16:22:15 +0000647static unsigned GetSymVersion(unsigned opcode)
648{
649 switch (opcode) {
650 default: assert(0 && "unknown load or store"); return 0;
651 case Alpha::LDQ: return Alpha::LDQ_SYM;
652 case Alpha::LDS: return Alpha::LDS_SYM;
653 case Alpha::LDT: return Alpha::LDT_SYM;
654 case Alpha::LDL: return Alpha::LDL_SYM;
655 case Alpha::LDBU: return Alpha::LDBU_SYM;
656 case Alpha::LDWU: return Alpha::LDWU_SYM;
657 case Alpha::LDW: return Alpha::LDW_SYM;
658 case Alpha::LDB: return Alpha::LDB_SYM;
659 case Alpha::STQ: return Alpha::STQ_SYM;
660 case Alpha::STS: return Alpha::STS_SYM;
661 case Alpha::STT: return Alpha::STT_SYM;
662 case Alpha::STL: return Alpha::STL_SYM;
663 case Alpha::STW: return Alpha::STW_SYM;
664 case Alpha::STB: return Alpha::STB_SYM;
665 }
666}
667
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000668void ISel::MoveFP2Int(unsigned src, unsigned dst, bool isDouble)
669{
670 unsigned Opc;
671 if (EnableAlphaFTOI) {
672 Opc = isDouble ? Alpha::FTOIT : Alpha::FTOIS;
673 BuildMI(BB, Opc, 1, dst).addReg(src);
674 } else {
675 //The hard way:
676 // Spill the integer to memory and reload it from there.
677 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
678 MachineFunction *F = BB->getParent();
679 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
680
681 Opc = isDouble ? Alpha::STT : Alpha::STS;
682 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
683 Opc = isDouble ? Alpha::LDQ : Alpha::LDL;
684 BuildMI(BB, Alpha::LDQ, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
685 }
686}
687
688void ISel::MoveInt2FP(unsigned src, unsigned dst, bool isDouble)
689{
690 unsigned Opc;
691 if (EnableAlphaFTOI) {
692 Opc = isDouble?Alpha::ITOFT:Alpha::ITOFS;
693 BuildMI(BB, Opc, 1, dst).addReg(src);
694 } else {
695 //The hard way:
696 // Spill the integer to memory and reload it from there.
697 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
698 MachineFunction *F = BB->getParent();
699 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
700
701 Opc = isDouble ? Alpha::STQ : Alpha::STL;
702 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
703 Opc = isDouble ? Alpha::LDT : Alpha::LDS;
704 BuildMI(BB, Opc, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
705 }
706}
707
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000708bool ISel::SelectFPSetCC(SDOperand N, unsigned dst)
709{
710 SDNode *Node = N.Val;
711 unsigned Opc, Tmp1, Tmp2, Tmp3;
712 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node);
713
714 //assert(SetCC->getOperand(0).getValueType() != MVT::f32 && "SetCC f32 should have been promoted");
715 bool rev = false;
716 bool inv = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000717
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000718 switch (SetCC->getCondition()) {
719 default: Node->dump(); assert(0 && "Unknown FP comparison!");
720 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
721 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
722 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
723 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
724 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
725 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
726 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000727
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000728 //FIXME: check for constant 0.0
729 ConstantFPSDNode *CN;
730 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
731 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
732 Tmp1 = Alpha::F31;
733 else
734 Tmp1 = SelectExpr(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000735
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000736 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
737 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
738 Tmp2 = Alpha::F31;
739 else
740 Tmp2 = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000741
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000742 //Can only compare doubles, and dag won't promote for me
743 if (SetCC->getOperand(0).getValueType() == MVT::f32)
744 {
745 //assert(0 && "Setcc On float?\n");
746 std::cerr << "Setcc on float!\n";
747 Tmp3 = MakeReg(MVT::f64);
748 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
749 Tmp1 = Tmp3;
750 }
751 if (SetCC->getOperand(1).getValueType() == MVT::f32)
752 {
753 //assert (0 && "Setcc On float?\n");
754 std::cerr << "Setcc on float!\n";
755 Tmp3 = MakeReg(MVT::f64);
756 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp2);
757 Tmp2 = Tmp3;
758 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000759
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000760 if (rev) std::swap(Tmp1, Tmp2);
761 //do the comparison
762 BuildMI(BB, Opc, 2, dst).addReg(Tmp1).addReg(Tmp2);
763 return inv;
764}
765
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000766//Check to see if the load is a constant offset from a base register
767void ISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
768{
769 unsigned opcode = N.getOpcode();
770 if (opcode == ISD::ADD) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000771 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000772 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
773 { //Normal imm add
774 Reg = SelectExpr(N.getOperand(0));
775 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
776 return;
777 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000778 else if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000779 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 32767)
780 {
781 Reg = SelectExpr(N.getOperand(1));
782 offset = cast<ConstantSDNode>(N.getOperand(0))->getValue();
783 return;
784 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000785 }
786 Reg = SelectExpr(N);
787 offset = 0;
788 return;
789}
790
Andrew Lenharth445171a2005-02-08 00:40:03 +0000791void ISel::SelectBranchCC(SDOperand N)
792{
793 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000794 MachineBasicBlock *Dest =
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000795 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
796 unsigned Opc = Alpha::WTF;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000797
Andrew Lenharth445171a2005-02-08 00:40:03 +0000798 Select(N.getOperand(0)); //chain
799 SDOperand CC = N.getOperand(1);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000800
Andrew Lenharth445171a2005-02-08 00:40:03 +0000801 if (CC.getOpcode() == ISD::SETCC)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000802 {
803 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
804 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
805 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000806 bool LeftZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
807 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
808 bool RightZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
809 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000810 bool isNE = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000811
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000812 //Fix up CC
813 ISD::CondCode cCode= SetCC->getCondition();
814 if (LeftZero && !RightZero) //Swap Operands
815 cCode = ISD::getSetCCSwappedOperands(cCode);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000816
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000817 if(cCode == ISD::SETNE)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000818 isNE = true;
Andrew Lenharth445171a2005-02-08 00:40:03 +0000819
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000820 if (LeftZero || RightZero) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000821 switch (SetCC->getCondition()) {
822 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
823 case ISD::SETEQ: Opc = Alpha::BEQ; break;
824 case ISD::SETLT: Opc = Alpha::BLT; break;
825 case ISD::SETLE: Opc = Alpha::BLE; break;
826 case ISD::SETGT: Opc = Alpha::BGT; break;
827 case ISD::SETGE: Opc = Alpha::BGE; break;
828 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
829 case ISD::SETUGT: Opc = Alpha::BNE; break;
830 case ISD::SETULE: Opc = Alpha::BEQ; break; //Technically you could have this CC
831 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
832 case ISD::SETNE: Opc = Alpha::BNE; break;
833 }
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000834 unsigned Tmp1;
835 if(LeftZero && !RightZero) //swap Operands
836 Tmp1 = SelectExpr(SetCC->getOperand(1)); //Cond
837 else
838 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000839 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
840 return;
841 } else {
842 unsigned Tmp1 = SelectExpr(CC);
843 if (isNE)
844 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
845 else
846 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000847 return;
848 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000849 } else { //FP
850 //Any comparison between 2 values should be codegened as an folded branch, as moving
851 //CC to the integer register is very expensive
852 //for a cmp b: c = a - b;
853 //a = b: c = 0
854 //a < b: c < 0
855 //a > b: c > 0
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000856
857 bool invTest = false;
858 unsigned Tmp3;
859
860 ConstantFPSDNode *CN;
861 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
862 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
863 Tmp3 = SelectExpr(SetCC->getOperand(0));
864 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
865 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
866 {
867 Tmp3 = SelectExpr(SetCC->getOperand(1));
868 invTest = true;
869 }
870 else
871 {
872 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
873 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
874 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
875 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
876 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
877 .addReg(Tmp1).addReg(Tmp2);
878 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000879
880 switch (SetCC->getCondition()) {
881 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000882 case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
883 case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
884 case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
885 case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
886 case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
887 case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000888 }
889 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000890 return;
891 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000892 abort(); //Should never be reached
893 } else {
894 //Giveup and do the stupid thing
895 unsigned Tmp1 = SelectExpr(CC);
896 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
897 return;
898 }
Andrew Lenharth445171a2005-02-08 00:40:03 +0000899 abort(); //Should never be reached
900}
901
Andrew Lenharth40831c52005-01-28 06:57:18 +0000902unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
903{
904 unsigned Tmp1, Tmp2, Tmp3;
905 unsigned Opc = 0;
906 SDNode *Node = N.Val;
907 MVT::ValueType DestType = N.getValueType();
908 unsigned opcode = N.getOpcode();
909
910 switch (opcode) {
911 default:
912 Node->dump();
913 assert(0 && "Node not handled!\n");
Andrew Lenharth2c594352005-01-29 15:42:07 +0000914
Andrew Lenharth7332f3e2005-04-02 19:11:07 +0000915 case ISD::UNDEF: {
916 BuildMI(BB, Alpha::IDEF, 0, Result);
917 return Result;
918 }
919
Andrew Lenharth30b46d42005-04-02 19:04:58 +0000920 case ISD::FNEG:
921 if(ISD::FABS == N.getOperand(0).getOpcode())
922 {
Misha Brukman7847fca2005-04-22 17:54:37 +0000923 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
924 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +0000925 } else {
Misha Brukman7847fca2005-04-22 17:54:37 +0000926 Tmp1 = SelectExpr(N.getOperand(0));
927 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +0000928 }
929 return Result;
930
931 case ISD::FABS:
932 Tmp1 = SelectExpr(N.getOperand(0));
933 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
934 return Result;
935
Andrew Lenharth9818c052005-02-05 13:19:12 +0000936 case ISD::SELECT:
937 {
Andrew Lenharth45859692005-03-03 21:47:53 +0000938 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
939 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
940 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
941
942 SDOperand CC = N.getOperand(0);
943 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
944
Misha Brukman4633f1c2005-04-21 23:13:11 +0000945 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth45859692005-03-03 21:47:53 +0000946 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
947 { //FP Setcc -> Select yay!
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000948
949
Andrew Lenharth45859692005-03-03 21:47:53 +0000950 //for a cmp b: c = a - b;
951 //a = b: c = 0
952 //a < b: c < 0
953 //a > b: c > 0
Misha Brukman4633f1c2005-04-21 23:13:11 +0000954
Andrew Lenharth45859692005-03-03 21:47:53 +0000955 bool invTest = false;
956 unsigned Tmp3;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000957
Andrew Lenharth45859692005-03-03 21:47:53 +0000958 ConstantFPSDNode *CN;
959 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
960 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
961 Tmp3 = SelectExpr(SetCC->getOperand(0));
962 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
963 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
964 {
965 Tmp3 = SelectExpr(SetCC->getOperand(1));
966 invTest = true;
967 }
968 else
969 {
970 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
971 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
972 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
973 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
974 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
975 .addReg(Tmp1).addReg(Tmp2);
976 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000977
Andrew Lenharth45859692005-03-03 21:47:53 +0000978 switch (SetCC->getCondition()) {
979 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
980 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNE : Alpha::FCMOVEQ; break;
981 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGT : Alpha::FCMOVLT; break;
982 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGE : Alpha::FCMOVLE; break;
983 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLT : Alpha::FCMOVGT; break;
984 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLE : Alpha::FCMOVGE; break;
985 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQ : Alpha::FCMOVNE; break;
986 }
Andrew Lenharth33819132005-03-04 20:09:23 +0000987 BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
Andrew Lenharth45859692005-03-03 21:47:53 +0000988 return Result;
989 }
990 else
991 {
992 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000993 BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV).addReg(Tmp1);
994// // Spill the cond to memory and reload it from there.
995// unsigned Tmp4 = MakeReg(MVT::f64);
996// MoveIntFP(Tmp1, Tmp4, true);
997// //now ideally, we don't have to do anything to the flag...
998// // Get the condition into the zero flag.
999// BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
Andrew Lenharth45859692005-03-03 21:47:53 +00001000 return Result;
1001 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001002 }
1003
Andrew Lenharthc1faced2005-02-01 01:37:24 +00001004 case ISD::FP_ROUND:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001005 assert (DestType == MVT::f32 &&
1006 N.getOperand(0).getValueType() == MVT::f64 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001007 "only f64 to f32 conversion supported here");
Andrew Lenharthc1faced2005-02-01 01:37:24 +00001008 Tmp1 = SelectExpr(N.getOperand(0));
1009 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
1010 return Result;
1011
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001012 case ISD::FP_EXTEND:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001013 assert (DestType == MVT::f64 &&
1014 N.getOperand(0).getValueType() == MVT::f32 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001015 "only f32 to f64 conversion supported here");
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001016 Tmp1 = SelectExpr(N.getOperand(0));
1017 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
1018 return Result;
1019
Andrew Lenharth2c594352005-01-29 15:42:07 +00001020 case ISD::CopyFromReg:
1021 {
1022 // Make sure we generate both values.
1023 if (Result != notIn)
1024 ExprMap[N.getValue(1)] = notIn; // Generate the token
1025 else
1026 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001027
Andrew Lenharth2c594352005-01-29 15:42:07 +00001028 SDOperand Chain = N.getOperand(0);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001029
Andrew Lenharth2c594352005-01-29 15:42:07 +00001030 Select(Chain);
1031 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1032 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1033 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
1034 return Result;
1035 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001036
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001037 case ISD::LOAD:
1038 {
1039 // Make sure we generate both values.
1040 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001041 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001042 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001043 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001044
Andrew Lenharth29219162005-02-07 06:31:44 +00001045 DestType = N.getValue(0).getValueType();
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001046
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001047 SDOperand Chain = N.getOperand(0);
1048 SDOperand Address = N.getOperand(1);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001049 Select(Chain);
Andrew Lenharth65838902005-02-06 16:22:15 +00001050 Opc = DestType == MVT::f64 ? Alpha::LDT : Alpha::LDS;
1051
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001052 if (Address.getOpcode() == ISD::GlobalAddress) {
1053 AlphaLowering.restoreGP(BB);
1054 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001055 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001056 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1057 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001058 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001059 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001060 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001061 has_sym = true;
Andrew Lenharth97127a12005-02-05 17:41:39 +00001062 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001063 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001064 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001065 BuildMI(BB, Opc, 2, Result)
1066 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1067 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001068 } else {
1069 long offset;
1070 SelectAddr(Address, Tmp1, offset);
1071 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1072 }
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001073 return Result;
1074 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001075 case ISD::ConstantFP:
1076 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
1077 if (CN->isExactlyValue(+0.0)) {
1078 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001079 } else if ( CN->isExactlyValue(-0.0)) {
1080 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001081 } else {
1082 abort();
1083 }
1084 }
1085 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001086
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001087 case ISD::SDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001088 case ISD::MUL:
1089 case ISD::ADD:
1090 case ISD::SUB:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001091 switch( opcode ) {
1092 case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS; break;
1093 case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS; break;
1094 case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS; break;
1095 case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS; break;
1096 };
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001097
1098 ConstantFPSDNode *CN;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001099 if (opcode == ISD::SUB
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001100 && (CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
1101 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1102 {
1103 Tmp2 = SelectExpr(N.getOperand(1));
1104 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp2).addReg(Tmp2);
1105 } else {
1106 Tmp1 = SelectExpr(N.getOperand(0));
1107 Tmp2 = SelectExpr(N.getOperand(1));
1108 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1109 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001110 return Result;
1111
Andrew Lenharth2c594352005-01-29 15:42:07 +00001112 case ISD::EXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001113 {
1114 //include a conversion sequence for float loads to double
1115 if (Result != notIn)
1116 ExprMap[N.getValue(1)] = notIn; // Generate the token
1117 else
1118 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001119
Andrew Lenhartha549deb2005-02-07 05:33:15 +00001120 Tmp1 = MakeReg(MVT::f32);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001121
1122 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001123 "EXTLOAD not from f32");
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001124 assert(Node->getValueType(0) == MVT::f64 && "EXTLOAD not to f64");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001125
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001126 SDOperand Chain = N.getOperand(0);
1127 SDOperand Address = N.getOperand(1);
1128 Select(Chain);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001129
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001130 if (Address.getOpcode() == ISD::GlobalAddress) {
1131 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001132 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001133 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1134 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001135 else if (ConstantPoolSDNode *CP =
1136 dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001137 {
1138 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001139 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001140 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addConstantPoolIndex(CP->getIndex());
1141 }
1142 else if(Address.getOpcode() == ISD::FrameIndex) {
1143 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
Andrew Lenharth032f2352005-02-22 21:59:48 +00001144 BuildMI(BB, Alpha::LDS, 2, Tmp1)
1145 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1146 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001147 } else {
1148 long offset;
1149 SelectAddr(Address, Tmp2, offset);
1150 BuildMI(BB, Alpha::LDS, 1, Tmp1).addImm(offset).addReg(Tmp2);
1151 }
Andrew Lenharth29219162005-02-07 06:31:44 +00001152 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001153 return Result;
1154 }
Andrew Lenharth2c594352005-01-29 15:42:07 +00001155
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001156 case ISD::UINT_TO_FP:
1157 case ISD::SINT_TO_FP:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001158 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001159 assert (N.getOperand(0).getValueType() == MVT::i64
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001160 && "only quads can be loaded from");
Andrew Lenharth40831c52005-01-28 06:57:18 +00001161 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001162 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001163 MoveInt2FP(Tmp1, Tmp2, true);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001164 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
1165 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001166 return Result;
1167 }
1168 }
1169 assert(0 && "should not get here");
1170 return 0;
1171}
1172
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001173unsigned ISel::SelectExpr(SDOperand N) {
1174 unsigned Result;
Andrew Lenharth2966e842005-04-07 18:15:28 +00001175 unsigned Tmp1, Tmp2 = 0, Tmp3;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001176 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001177 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001178
1179 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001180 MVT::ValueType DestType = N.getValueType();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001181
1182 unsigned &Reg = ExprMap[N];
1183 if (Reg) return Reg;
1184
1185 if (N.getOpcode() != ISD::CALL)
1186 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001187 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001188 else {
1189 // If this is a call instruction, make sure to prepare ALL of the result
1190 // values as well as the chain.
1191 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001192 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001193 else {
1194 Result = MakeReg(Node->getValueType(0));
1195 ExprMap[N.getValue(0)] = Result;
1196 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1197 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001198 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001199 }
1200 }
1201
Andrew Lenharth22088bb2005-02-02 15:05:33 +00001202 if (DestType == MVT::f64 || DestType == MVT::f32 ||
Andrew Lenharth06342c32005-02-07 06:21:37 +00001203 (
Misha Brukman4633f1c2005-04-21 23:13:11 +00001204 (opcode == ISD::LOAD || opcode == ISD::CopyFromReg ||
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001205 opcode == ISD::EXTLOAD) &&
Misha Brukman4633f1c2005-04-21 23:13:11 +00001206 (N.getValue(0).getValueType() == MVT::f32 ||
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001207 N.getValue(0).getValueType() == MVT::f64)
Andrew Lenharth06342c32005-02-07 06:21:37 +00001208 )
1209 )
Andrew Lenharth40831c52005-01-28 06:57:18 +00001210 return SelectExprFP(N, Result);
1211
1212 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001213 default:
1214 Node->dump();
1215 assert(0 && "Node not handled!\n");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001216
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001217 case ISD::MULHU:
1218 Tmp1 = SelectExpr(N.getOperand(0));
1219 Tmp2 = SelectExpr(N.getOperand(1));
1220 BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth706be912005-04-07 13:55:53 +00001221 return Result;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001222 case ISD::MULHS:
1223 {
1224 //MULHU - Ra<63>*Rb - Rb<63>*Ra
1225 Tmp1 = SelectExpr(N.getOperand(0));
1226 Tmp2 = SelectExpr(N.getOperand(1));
1227 Tmp3 = MakeReg(MVT::i64);
1228 BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1229 unsigned V1 = MakeReg(MVT::i64);
1230 unsigned V2 = MakeReg(MVT::i64);
1231 BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31).addReg(Tmp1);
1232 BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31).addReg(Tmp2);
1233 unsigned IRes = MakeReg(MVT::i64);
1234 BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
1235 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
1236 return Result;
1237 }
Andrew Lenharth7332f3e2005-04-02 19:11:07 +00001238 case ISD::UNDEF: {
1239 BuildMI(BB, Alpha::IDEF, 0, Result);
1240 return Result;
1241 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001242
Andrew Lenharth032f2352005-02-22 21:59:48 +00001243 case ISD::DYNAMIC_STACKALLOC:
1244 // Generate both result values.
Andrew Lenharth3a7118d2005-02-23 17:33:42 +00001245 if (Result != notIn)
1246 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth032f2352005-02-22 21:59:48 +00001247 else
1248 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1249
1250 // FIXME: We are currently ignoring the requested alignment for handling
1251 // greater than the stack alignment. This will need to be revisited at some
1252 // point. Align = N.getOperand(2);
1253
1254 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1255 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1256 std::cerr << "Cannot allocate stack object with greater alignment than"
1257 << " the stack alignment yet!";
1258 abort();
1259 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001260
Andrew Lenharth032f2352005-02-22 21:59:48 +00001261 Select(N.getOperand(0));
1262 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1263 {
1264 if (CN->getValue() < 32000)
1265 {
1266 BuildMI(BB, Alpha::LDA, 2, Alpha::R30)
1267 .addImm(-CN->getValue()).addReg(Alpha::R30);
1268 } else {
1269 Tmp1 = SelectExpr(N.getOperand(1));
1270 // Subtract size from stack pointer, thereby allocating some space.
1271 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1272 }
1273 } else {
1274 Tmp1 = SelectExpr(N.getOperand(1));
1275 // Subtract size from stack pointer, thereby allocating some space.
1276 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1277 }
1278
1279 // Put a pointer to the space into the result register, by copying the stack
1280 // pointer.
Andrew Lenharth7bc47022005-02-22 23:29:25 +00001281 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
Andrew Lenharth032f2352005-02-22 21:59:48 +00001282 return Result;
1283
Andrew Lenharth33819132005-03-04 20:09:23 +00001284// case ISD::ConstantPool:
1285// Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1286// AlphaLowering.restoreGP(BB);
1287// BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(Tmp1);
1288// return Result;
Andrew Lenharth2c594352005-01-29 15:42:07 +00001289
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001290 case ISD::FrameIndex:
Andrew Lenharth032f2352005-02-22 21:59:48 +00001291 BuildMI(BB, Alpha::LDA, 2, Result)
1292 .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
1293 .addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001294 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001295
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001296 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001297 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001298 case ISD::SEXTLOAD:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001299 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001300 {
1301 // Make sure we generate both values.
1302 if (Result != notIn)
1303 ExprMap[N.getValue(1)] = notIn; // Generate the token
1304 else
1305 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001306
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001307 SDOperand Chain = N.getOperand(0);
1308 SDOperand Address = N.getOperand(1);
1309 Select(Chain);
1310
Misha Brukman4633f1c2005-04-21 23:13:11 +00001311 assert(Node->getValueType(0) == MVT::i64 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001312 "Unknown type to sign extend to.");
Andrew Lenharth03824012005-02-07 05:55:55 +00001313 if (opcode == ISD::LOAD)
1314 Opc = Alpha::LDQ;
1315 else
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001316 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
1317 default: Node->dump(); assert(0 && "Bad sign extend!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001318 case MVT::i32: Opc = Alpha::LDL;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001319 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001320 case MVT::i16: Opc = Alpha::LDWU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001321 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001322 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Misha Brukman4633f1c2005-04-21 23:13:11 +00001323 case MVT::i8: Opc = Alpha::LDBU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001324 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001325 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001326
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001327 if (Address.getOpcode() == ISD::GlobalAddress) {
1328 AlphaLowering.restoreGP(BB);
1329 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001330 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001331 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1332 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001333 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1334 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001335 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001336 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001337 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001338 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001339 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001340 BuildMI(BB, Opc, 2, Result)
1341 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1342 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001343 } else {
1344 long offset;
1345 SelectAddr(Address, Tmp1, offset);
1346 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1347 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001348 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001349 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001350
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001351 case ISD::GlobalAddress:
1352 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001353 has_sym = true;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001354 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
1355 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
1356 return Result;
1357
1358 case ISD::CALL:
1359 {
1360 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001361
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001362 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001363 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001364
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001365 //grab the arguments
1366 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001367 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001368 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001369 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001370
Andrew Lenharth684f2292005-01-30 00:35:27 +00001371 //in reg args
1372 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001373 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001374 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001375 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +00001376 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001377 Alpha::F19, Alpha::F20, Alpha::F21};
1378 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001379 default:
1380 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001381 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001382 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001383 N.getOperand(i+2).getValueType() << "\n";
1384 assert(0 && "Unknown value type for call");
1385 case MVT::i1:
1386 case MVT::i8:
1387 case MVT::i16:
1388 case MVT::i32:
1389 case MVT::i64:
1390 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1391 break;
1392 case MVT::f32:
1393 case MVT::f64:
1394 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1395 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001396 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001397 }
Andrew Lenharth684f2292005-01-30 00:35:27 +00001398 //in mem args
1399 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001400 {
1401 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001402 default:
1403 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001404 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001405 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001406 N.getOperand(i+2).getValueType() << "\n";
1407 assert(0 && "Unknown value type for call");
1408 case MVT::i1:
1409 case MVT::i8:
1410 case MVT::i16:
1411 case MVT::i32:
1412 case MVT::i64:
1413 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1414 break;
1415 case MVT::f32:
1416 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1417 break;
1418 case MVT::f64:
1419 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1420 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001421 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001422 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001423 //build the right kind of call
1424 if (GlobalAddressSDNode *GASD =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001425 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001426 {
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001427 if (GASD->getGlobal()->isExternal()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001428 //use safe calling convention
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001429 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001430 has_sym = true;
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001431 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal());
1432 } else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001433 //use PC relative branch call
Andrew Lenharth1e0d9bd2005-04-14 17:34:20 +00001434 AlphaLowering.restoreGP(BB);
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001435 BuildMI(BB, Alpha::BSR, 1, Alpha::R26).addGlobalAddress(GASD->getGlobal(),true);
1436 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001437 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001438 else if (ExternalSymbolSDNode *ESSDN =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001439 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001440 {
1441 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001442 has_sym = true;
Andrew Lenharthba05ad62005-03-30 18:22:52 +00001443 BuildMI(BB, Alpha::CALL, 1).addExternalSymbol(ESSDN->getSymbol(), true);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001444 } else {
1445 //no need to restore GP as we are doing an indirect call
1446 Tmp1 = SelectExpr(N.getOperand(1));
1447 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
1448 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
1449 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001450
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001451 //push the result into a virtual register
Misha Brukman4633f1c2005-04-21 23:13:11 +00001452
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001453 switch (Node->getValueType(0)) {
1454 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001455 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001456 case MVT::i1:
1457 case MVT::i8:
1458 case MVT::i16:
1459 case MVT::i32:
1460 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001461 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
1462 break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001463 case MVT::f32:
1464 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001465 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
1466 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001467 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001468 return Result+N.ResNo;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001469 }
1470
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001471 case ISD::SIGN_EXTEND_INREG:
1472 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001473 //do SDIV opt for all levels of ints if not dividing by a constant
1474 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
1475 && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001476 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001477 unsigned Tmp4 = MakeReg(MVT::f64);
1478 unsigned Tmp5 = MakeReg(MVT::f64);
1479 unsigned Tmp6 = MakeReg(MVT::f64);
1480 unsigned Tmp7 = MakeReg(MVT::f64);
1481 unsigned Tmp8 = MakeReg(MVT::f64);
1482 unsigned Tmp9 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001483
1484 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1485 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1486 MoveInt2FP(Tmp1, Tmp4, true);
1487 MoveInt2FP(Tmp2, Tmp5, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001488 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Tmp4);
1489 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Tmp5);
1490 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
1491 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Tmp8);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001492 MoveFP2Int(Tmp9, Result, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001493 return Result;
1494 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001495
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001496 //Alpha has instructions for a bunch of signed 32 bit stuff
1497 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001498 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001499 switch (N.getOperand(0).getOpcode()) {
1500 case ISD::ADD:
1501 case ISD::SUB:
1502 case ISD::MUL:
1503 {
1504 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
1505 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
1506 //FIXME: first check for Scaled Adds and Subs!
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001507 ConstantSDNode* CSD = NULL;
1508 if(!isMul && N.getOperand(0).getOperand(0).getOpcode() == ISD::SHL &&
1509 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(0).getOperand(1))) &&
1510 (CSD->getValue() == 2 || CSD->getValue() == 3))
1511 {
1512 bool use4 = CSD->getValue() == 2;
1513 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1514 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1515 BuildMI(BB, isAdd?(use4?Alpha::S4ADDL:Alpha::S8ADDL):(use4?Alpha::S4SUBL:Alpha::S8SUBL),
1516 2,Result).addReg(Tmp1).addReg(Tmp2);
1517 }
1518 else if(isAdd && N.getOperand(0).getOperand(1).getOpcode() == ISD::SHL &&
1519 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1).getOperand(1))) &&
1520 (CSD->getValue() == 2 || CSD->getValue() == 3))
1521 {
1522 bool use4 = CSD->getValue() == 2;
1523 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1524 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1525 BuildMI(BB, use4?Alpha::S4ADDL:Alpha::S8ADDL, 2,Result).addReg(Tmp1).addReg(Tmp2);
1526 }
1527 else if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001528 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
1529 { //Normal imm add/sub
1530 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001531 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001532 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
1533 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001534 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001535 else
1536 { //Normal add/sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001537 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULL : Alpha::SUBL);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001538 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001539 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001540 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1541 }
1542 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001543 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001544 default: break; //Fall Though;
1545 }
1546 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001547 Tmp1 = SelectExpr(N.getOperand(0));
1548 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001549 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001550 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001551 {
1552 default:
1553 Node->dump();
1554 assert(0 && "Sign Extend InReg not there yet");
1555 break;
1556 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001557 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001558 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001559 break;
1560 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001561 case MVT::i16:
1562 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
1563 break;
1564 case MVT::i8:
1565 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
1566 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +00001567 case MVT::i1:
1568 Tmp2 = MakeReg(MVT::i64);
1569 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001570 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +00001571 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001572 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001573 return Result;
1574 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001575
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001576 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001577 {
1578 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1579 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1580 bool isConst1 = false;
1581 bool isConst2 = false;
1582 int dir;
Misha Brukman7847fca2005-04-22 17:54:37 +00001583
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001584 //Tmp1 = SelectExpr(N.getOperand(0));
1585 if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001586 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
1587 isConst1 = true;
1588 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001589 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1590 isConst2 = true;
1591
1592 switch (SetCC->getCondition()) {
1593 default: Node->dump(); assert(0 && "Unknown integer comparison!");
1594 case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001595 case ISD::SETLT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001596 Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001597 case ISD::SETLE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001598 Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001599 case ISD::SETGT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001600 Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001601 case ISD::SETGE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001602 Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001603 case ISD::SETULT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001604 Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001605 case ISD::SETUGT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001606 Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001607 case ISD::SETULE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001608 Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001609 case ISD::SETUGE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001610 Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001611 case ISD::SETNE: {//Handle this one special
1612 //std::cerr << "Alpha does not have a setne.\n";
1613 //abort();
1614 Tmp1 = SelectExpr(N.getOperand(0));
1615 Tmp2 = SelectExpr(N.getOperand(1));
1616 Tmp3 = MakeReg(MVT::i64);
1617 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001618 //Remeber we have the Inv for this CC
1619 CCInvMap[N] = Tmp3;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001620 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001621 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001622 return Result;
1623 }
1624 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001625 if (dir == 1) {
1626 Tmp1 = SelectExpr(N.getOperand(0));
1627 if (isConst2) {
1628 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1629 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1630 } else {
1631 Tmp2 = SelectExpr(N.getOperand(1));
1632 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1633 }
1634 } else if (dir == 2) {
1635 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001636 if (isConst1) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001637 Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1638 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1639 } else {
1640 Tmp2 = SelectExpr(N.getOperand(0));
1641 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1642 }
1643 } else { //dir == 0
1644 if (isConst1) {
1645 Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1646 Tmp2 = SelectExpr(N.getOperand(1));
1647 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
1648 } else if (isConst2) {
1649 Tmp1 = SelectExpr(N.getOperand(0));
1650 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1651 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
1652 } else {
1653 Tmp1 = SelectExpr(N.getOperand(0));
1654 Tmp2 = SelectExpr(N.getOperand(1));
1655 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1656 }
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001657 }
1658 } else {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001659 //do the comparison
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001660 Tmp1 = MakeReg(MVT::f64);
1661 bool inv = SelectFPSetCC(N, Tmp1);
1662
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001663 //now arrange for Result (int) to have a 1 or 0
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001664 Tmp2 = MakeReg(MVT::i64);
1665 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001666 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001667 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001668 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001669 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001670 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001671 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001672
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001673 case ISD::CopyFromReg:
1674 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001675 ++count_ins;
1676
Andrew Lenharth40831c52005-01-28 06:57:18 +00001677 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001678 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001679 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001680 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001681 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001682
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001683 SDOperand Chain = N.getOperand(0);
1684
1685 Select(Chain);
1686 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1687 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1688 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1689 return Result;
1690 }
1691
Misha Brukman4633f1c2005-04-21 23:13:11 +00001692 //Most of the plain arithmetic and logic share the same form, and the same
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001693 //constant immediate test
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001694 case ISD::XOR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001695 //Match Not
1696 if (N.getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001697 cast<ConstantSDNode>(N.getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001698 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001699 Tmp1 = SelectExpr(N.getOperand(0));
1700 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1701 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001702 }
1703 //Fall through
1704 case ISD::AND:
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001705 //handle zap
1706 if (opcode == ISD::AND && N.getOperand(1).getOpcode() == ISD::Constant)
1707 {
1708 uint64_t k = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1709 unsigned int build = 0;
1710 for(int i = 0; i < 8; ++i)
1711 {
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001712 if ((k & 0x00FF) == 0x00FF)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001713 build |= 1 << i;
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001714 else if ((k & 0x00FF) != 0)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001715 { build = 0; break; }
1716 k >>= 8;
1717 }
1718 if (build)
1719 {
1720 Tmp1 = SelectExpr(N.getOperand(0));
1721 BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1722 return Result;
1723 }
1724 }
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001725 case ISD::OR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001726 //Check operand(0) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001727 if (N.getOperand(0).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001728 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001729 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001730 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001731 switch(opcode) {
1732 case ISD::AND: Opc = Alpha::BIC; break;
1733 case ISD::OR: Opc = Alpha::ORNOT; break;
1734 case ISD::XOR: Opc = Alpha::EQV; break;
1735 }
1736 Tmp1 = SelectExpr(N.getOperand(1));
1737 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1738 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1739 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001740 }
1741 //Check operand(1) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001742 if (N.getOperand(1).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001743 N.getOperand(1).getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001744 cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001745 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001746 switch(opcode) {
1747 case ISD::AND: Opc = Alpha::BIC; break;
1748 case ISD::OR: Opc = Alpha::ORNOT; break;
1749 case ISD::XOR: Opc = Alpha::EQV; break;
1750 }
1751 Tmp1 = SelectExpr(N.getOperand(0));
1752 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1753 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1754 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001755 }
1756 //Fall through
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001757 case ISD::SHL:
1758 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001759 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001760 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001761 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1762 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001763 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001764 {
1765 switch(opcode) {
1766 case ISD::AND: Opc = Alpha::ANDi; break;
1767 case ISD::OR: Opc = Alpha::BISi; break;
1768 case ISD::XOR: Opc = Alpha::XORi; break;
1769 case ISD::SHL: Opc = Alpha::SLi; break;
1770 case ISD::SRL: Opc = Alpha::SRLi; break;
1771 case ISD::SRA: Opc = Alpha::SRAi; break;
1772 case ISD::MUL: Opc = Alpha::MULQi; break;
1773 };
1774 Tmp1 = SelectExpr(N.getOperand(0));
1775 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1776 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1777 } else {
1778 switch(opcode) {
1779 case ISD::AND: Opc = Alpha::AND; break;
1780 case ISD::OR: Opc = Alpha::BIS; break;
1781 case ISD::XOR: Opc = Alpha::XOR; break;
1782 case ISD::SHL: Opc = Alpha::SL; break;
1783 case ISD::SRL: Opc = Alpha::SRL; break;
1784 case ISD::SRA: Opc = Alpha::SRA; break;
1785 case ISD::MUL: Opc = Alpha::MULQ; break;
1786 };
1787 Tmp1 = SelectExpr(N.getOperand(0));
1788 Tmp2 = SelectExpr(N.getOperand(1));
1789 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1790 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001791 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001792
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001793 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001794 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001795 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001796 bool isAdd = opcode == ISD::ADD;
1797
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001798 //first check for Scaled Adds and Subs!
1799 //Valid for add and sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001800 ConstantSDNode* CSD = NULL;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001801 if(N.getOperand(0).getOpcode() == ISD::SHL &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001802 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
1803 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001804 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001805 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001806 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001807 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) && CSD->getValue() <= 255)
1808 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1809 2, Result).addReg(Tmp2).addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001810 else {
1811 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001812 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1813 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001814 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001815 }
1816 //Position prevents subs
Andrew Lenharth273a1f92005-04-07 14:18:13 +00001817 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001818 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) &&
1819 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001820 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001821 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001822 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001823 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(0))) && CSD->getValue() <= 255)
1824 BuildMI(BB, use4?Alpha::S4ADDQi:Alpha::S8ADDQi, 2, Result).addReg(Tmp2)
1825 .addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001826 else {
1827 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001828 BuildMI(BB, use4?Alpha::S4ADDQ:Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001829 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001830 }
1831 //small addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001832 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
1833 CSD->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001834 { //Normal imm add/sub
1835 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1836 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001837 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CSD->getValue());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001838 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001839 //larger addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001840 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
1841 CSD->getSignExtended() <= 32767 &&
1842 CSD->getSignExtended() >= -32767)
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001843 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001844 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001845 Tmp2 = (long)CSD->getSignExtended();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001846 if (!isAdd)
1847 Tmp2 = -Tmp2;
1848 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001849 }
1850 //give up and do the operation
1851 else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001852 //Normal add/sub
1853 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1854 Tmp1 = SelectExpr(N.getOperand(0));
1855 Tmp2 = SelectExpr(N.getOperand(1));
1856 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1857 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001858 return Result;
1859 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001860
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001861 case ISD::SDIV:
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001862 {
Andrew Lenhartha565c272005-04-06 22:03:13 +00001863 ConstantSDNode* CSD;
1864 //check if we can convert into a shift!
1865 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
1866 (int64_t)CSD->getSignExtended() != 0 &&
1867 ExactLog2(abs((int64_t)CSD->getSignExtended())) != 0)
1868 {
1869 unsigned k = ExactLog2(abs(CSD->getSignExtended()));
1870 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenhartha565c272005-04-06 22:03:13 +00001871 if (k == 1)
1872 Tmp2 = Tmp1;
1873 else
1874 {
1875 Tmp2 = MakeReg(MVT::i64);
1876 BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
1877 }
1878 Tmp3 = MakeReg(MVT::i64);
1879 BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
1880 unsigned Tmp4 = MakeReg(MVT::i64);
1881 BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
1882 if ((int64_t)CSD->getSignExtended() > 0)
1883 BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
1884 else
1885 {
1886 unsigned Tmp5 = MakeReg(MVT::i64);
1887 BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
1888 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
1889 }
1890 return Result;
1891 }
1892 }
1893 //Else fall through
1894
1895 case ISD::UDIV:
1896 {
1897 ConstantSDNode* CSD;
1898 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
1899 ((int64_t)CSD->getSignExtended() >= 2 ||
1900 (int64_t)CSD->getSignExtended() <= -2))
1901 {
1902 // If this is a divide by constant, we can emit code using some magic
1903 // constants to implement it as a multiply instead.
1904 ExprMap.erase(N);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001905 if (opcode == ISD::SDIV)
Andrew Lenhartha565c272005-04-06 22:03:13 +00001906 return SelectExpr(BuildSDIVSequence(N));
1907 else
1908 return SelectExpr(BuildUDIVSequence(N));
1909 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001910 }
1911 //else fall though
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001912 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +00001913 case ISD::SREM:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001914 //FIXME: alpha really doesn't support any of these operations,
Andrew Lenharth40831c52005-01-28 06:57:18 +00001915 // the ops are expanded into special library calls with
1916 // special calling conventions
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001917 //Restore GP because it is a call after all...
Andrew Lenharth40831c52005-01-28 06:57:18 +00001918 switch(opcode) {
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001919 case ISD::UREM: Opc = Alpha::REMQU; break;
1920 case ISD::SREM: Opc = Alpha::REMQ; break;
1921 case ISD::UDIV: Opc = Alpha::DIVQU; break;
1922 case ISD::SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001923 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001924 Tmp1 = SelectExpr(N.getOperand(0));
1925 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth33819132005-03-04 20:09:23 +00001926 //set up regs explicitly (helps Reg alloc)
1927 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001928 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001929 AlphaLowering.restoreGP(BB);
Andrew Lenharth33819132005-03-04 20:09:23 +00001930 BuildMI(BB, Opc, 2).addReg(Alpha::R24).addReg(Alpha::R25);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001931 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001932 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001933
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001934 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001935 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001936 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001937 assert (DestType == MVT::i64 && "only quads can be loaded to");
1938 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00001939 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001940 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001941 if (SrcType == MVT::f32)
Misha Brukman7847fca2005-04-22 17:54:37 +00001942 {
1943 Tmp2 = MakeReg(MVT::f64);
1944 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
1945 Tmp1 = Tmp2;
1946 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001947 Tmp2 = MakeReg(MVT::f64);
1948 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001949 MoveFP2Int(Tmp2, Result, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001950
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001951 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001952 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001953
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001954 case ISD::SELECT:
1955 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001956 //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 +00001957 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001958 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1959 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001960 // Get the condition into the zero flag.
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001961 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001962
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001963 SDOperand CC = N.getOperand(0);
1964 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
1965
Misha Brukman4633f1c2005-04-21 23:13:11 +00001966 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001967 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
1968 { //FP Setcc -> Int Select
Misha Brukman7847fca2005-04-22 17:54:37 +00001969 Tmp1 = MakeReg(MVT::f64);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001970 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1971 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Misha Brukman7847fca2005-04-22 17:54:37 +00001972 bool inv = SelectFPSetCC(CC, Tmp1);
1973 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
1974 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
1975 return Result;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001976 }
1977 if (CC.getOpcode() == ISD::SETCC) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001978 //Int SetCC -> Select
1979 //Dropping the CC is only useful if we are comparing to 0
1980 if((SetCC->getOperand(1).getOpcode() == ISD::Constant &&
1981 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0) ||
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001982 (SetCC->getOperand(0).getOpcode() == ISD::Constant &&
1983 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0))
1984 {
1985 //figure out a few things
1986 bool LeftZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
1987 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
1988 bool RightZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
1989 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
1990 bool LeftConst = N.getOperand(1).getOpcode() == ISD::Constant &&
1991 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255;
1992 bool RightConst = N.getOperand(2).getOpcode() == ISD::Constant &&
1993 cast<ConstantSDNode>(N.getOperand(2))->getValue() <= 255;
1994 bool useImm = LeftConst || RightConst;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001995
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001996 //Fix up CC
1997 ISD::CondCode cCode= SetCC->getCondition();
1998 if (RightConst && !LeftConst) //Invert sense to get Imm field right
1999 cCode = ISD::getSetCCInverse(cCode, true);
2000 if (LeftZero && !RightZero) //Swap Operands
2001 cCode = ISD::getSetCCSwappedOperands(cCode);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002002
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002003 //Choose the CMOV
2004 switch (cCode) {
2005 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
2006 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
2007 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
2008 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
2009 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
2010 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
2011 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
2012 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2013 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break; //Technically you could have this CC
2014 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
2015 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2016 }
2017 if(LeftZero && !RightZero) //swap Operands
2018 Tmp1 = SelectExpr(SetCC->getOperand(1)); //Cond
2019 else
2020 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
2021
2022 if (LeftConst) {
2023 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2024 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
Misha Brukman7847fca2005-04-22 17:54:37 +00002025 .addImm(cast<ConstantSDNode>(N.getOperand(1))->getValue())
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002026 .addReg(Tmp1);
2027 } else if (RightConst) {
2028 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
2029 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
Misha Brukman7847fca2005-04-22 17:54:37 +00002030 .addImm(cast<ConstantSDNode>(N.getOperand(2))->getValue())
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002031 .addReg(Tmp1);
2032 } else {
2033 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2034 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2035 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
2036 }
2037 return Result;
2038 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002039 //Otherwise, fall though
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002040 }
2041 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002042 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2043 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthe76797c2005-02-01 20:40:27 +00002044 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002045
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002046 return Result;
2047 }
2048
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002049 case ISD::Constant:
2050 {
Andrew Lenharthc0513832005-03-29 19:24:04 +00002051 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002052 if (val <= IMM_HIGH && val >= IMM_LOW) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002053 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002054 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002055 else if (val <= (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT &&
2056 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
2057 Tmp1 = MakeReg(MVT::i64);
2058 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val)).addReg(Alpha::R31);
2059 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002060 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002061 else {
2062 MachineConstantPool *CP = BB->getParent()->getConstantPool();
2063 ConstantUInt *C = ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
2064 unsigned CPI = CP->getConstantPoolIndex(C);
2065 AlphaLowering.restoreGP(BB);
2066 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CPI);
2067 }
2068 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002069 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002070 }
2071
2072 return 0;
2073}
2074
2075void ISel::Select(SDOperand N) {
2076 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00002077 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002078
Nate Begeman85fdeb22005-03-24 04:39:54 +00002079 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002080 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002081
2082 SDNode *Node = N.Val;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002083
Andrew Lenharth760270d2005-02-07 23:02:23 +00002084 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002085
2086 default:
2087 Node->dump(); std::cerr << "\n";
2088 assert(0 && "Node not handled yet!");
2089
2090 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00002091 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002092 return;
2093 }
2094
2095 case ISD::BR: {
2096 MachineBasicBlock *Dest =
2097 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2098
2099 Select(N.getOperand(0));
2100 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
2101 return;
2102 }
2103
2104 case ISD::ImplicitDef:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002105 ++count_ins;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002106 Select(N.getOperand(0));
2107 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
2108 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002109
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002110 case ISD::EntryToken: return; // Noop
2111
2112 case ISD::TokenFactor:
2113 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2114 Select(Node->getOperand(i));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002115
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002116 //N.Val->dump(); std::cerr << "\n";
2117 //assert(0 && "Node not handled yet!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00002118
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002119 return;
2120
2121 case ISD::CopyToReg:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002122 ++count_outs;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002123 Select(N.getOperand(0));
2124 Tmp1 = SelectExpr(N.getOperand(1));
2125 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002126
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002127 if (Tmp1 != Tmp2) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002128 if (N.getOperand(1).getValueType() == MVT::f64 ||
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002129 N.getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00002130 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2131 else
2132 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002133 }
2134 return;
2135
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002136 case ISD::RET:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002137 ++count_outs;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002138 switch (N.getNumOperands()) {
2139 default:
2140 std::cerr << N.getNumOperands() << "\n";
2141 for (unsigned i = 0; i < N.getNumOperands(); ++i)
2142 std::cerr << N.getOperand(i).getValueType() << "\n";
2143 Node->dump();
2144 assert(0 && "Unknown return instruction!");
2145 case 2:
2146 Select(N.getOperand(0));
2147 Tmp1 = SelectExpr(N.getOperand(1));
2148 switch (N.getOperand(1).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002149 default: Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002150 assert(0 && "All other types should have been promoted!!");
2151 case MVT::f64:
2152 case MVT::f32:
2153 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
2154 break;
2155 case MVT::i32:
2156 case MVT::i64:
2157 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
2158 break;
2159 }
2160 break;
2161 case 1:
2162 Select(N.getOperand(0));
2163 break;
2164 }
2165 //Tmp2 = AlphaLowering.getRetAddr();
2166 //BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(Tmp2).addReg(Tmp2);
2167 BuildMI(BB, Alpha::RETURN, 0); // Just emit a 'ret' instruction
2168 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002169
Misha Brukman4633f1c2005-04-21 23:13:11 +00002170 case ISD::TRUNCSTORE:
2171 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002172 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002173 SDOperand Chain = N.getOperand(0);
2174 SDOperand Value = N.getOperand(1);
2175 SDOperand Address = N.getOperand(2);
2176 Select(Chain);
2177
2178 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00002179
2180 if (opcode == ISD::STORE) {
2181 switch(Value.getValueType()) {
2182 default: assert(0 && "unknown Type in store");
2183 case MVT::i64: Opc = Alpha::STQ; break;
2184 case MVT::f64: Opc = Alpha::STT; break;
2185 case MVT::f32: Opc = Alpha::STS; break;
2186 }
2187 } else { //ISD::TRUNCSTORE
2188 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2189 default: assert(0 && "unknown Type in store");
2190 case MVT::i1: //FIXME: DAG does not promote this load
2191 case MVT::i8: Opc = Alpha::STB; break;
2192 case MVT::i16: Opc = Alpha::STW; break;
2193 case MVT::i32: Opc = Alpha::STL; break;
2194 }
Andrew Lenharth65838902005-02-06 16:22:15 +00002195 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00002196
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002197 if (Address.getOpcode() == ISD::GlobalAddress)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002198 {
2199 AlphaLowering.restoreGP(BB);
2200 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002201 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002202 BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
2203 }
Andrew Lenharth05380342005-02-07 05:07:00 +00002204 else if(Address.getOpcode() == ISD::FrameIndex)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002205 {
Andrew Lenharth032f2352005-02-22 21:59:48 +00002206 BuildMI(BB, Opc, 3).addReg(Tmp1)
2207 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
2208 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002209 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002210 else
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002211 {
2212 long offset;
2213 SelectAddr(Address, Tmp2, offset);
2214 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2215 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002216 return;
2217 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002218
2219 case ISD::EXTLOAD:
2220 case ISD::SEXTLOAD:
2221 case ISD::ZEXTLOAD:
2222 case ISD::LOAD:
2223 case ISD::CopyFromReg:
2224 case ISD::CALL:
Andrew Lenharth032f2352005-02-22 21:59:48 +00002225 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002226 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002227 SelectExpr(N);
2228 return;
2229
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002230 case ISD::ADJCALLSTACKDOWN:
2231 case ISD::ADJCALLSTACKUP:
2232 Select(N.getOperand(0));
2233 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002234
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002235 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? Alpha::ADJUSTSTACKDOWN :
2236 Alpha::ADJUSTSTACKUP;
2237 BuildMI(BB, Opc, 1).addImm(Tmp1);
2238 return;
Andrew Lenharth95762122005-03-31 21:24:06 +00002239
2240 case ISD::PCMARKER:
2241 Select(N.getOperand(0)); //Chain
2242 BuildMI(BB, Alpha::PCLABEL, 2).addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
2243 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002244 }
2245 assert(0 && "Should not be reached!");
2246}
2247
2248
2249/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
2250/// into a machine code representation using pattern matching and a machine
2251/// description file.
2252///
2253FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002254 return new ISel(TM);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002255}
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002256