blob: 450de9f49172ed2eec72b918c373216f57de3b3f [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
Andrew Lenharth33819132005-03-04 20:09:23 +000087 //Doesn't work yet
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000088 setOperationAction(ISD::SETCC , MVT::f32, Promote);
Andrew Lenharth572af902005-02-14 05:41:43 +000089
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000090 computeRegisterProperties();
Misha Brukman4633f1c2005-04-21 23:13:11 +000091
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000092 addLegalFPImmediate(+0.0); //F31
93 addLegalFPImmediate(-0.0); //-F31
Andrew Lenharth304d0f32005-01-22 23:41:55 +000094 }
95
96 /// LowerArguments - This hook must be implemented to indicate how we should
97 /// lower the arguments for the specified function, into the specified DAG.
98 virtual std::vector<SDOperand>
99 LowerArguments(Function &F, SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000100
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000101 /// LowerCallTo - This hook lowers an abstract call to a function into an
102 /// actual call.
103 virtual std::pair<SDOperand, SDOperand>
Nate Begeman8e21e712005-03-26 01:29:23 +0000104 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
105 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000106
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000107 virtual std::pair<SDOperand, SDOperand>
108 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000109
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000110 virtual std::pair<SDOperand,SDOperand>
111 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
112 const Type *ArgTy, SelectionDAG &DAG);
113
114 virtual std::pair<SDOperand, SDOperand>
115 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
116 SelectionDAG &DAG);
117
118 void restoreGP(MachineBasicBlock* BB)
119 {
120 BuildMI(BB, Alpha::BIS, 2, Alpha::R29).addReg(GP).addReg(GP);
121 }
122 };
123}
124
125//http://www.cs.arizona.edu/computer.help/policy/DIGITAL_unix/AA-PY8AC-TET1_html/callCH3.html#BLOCK21
126
127//For now, just use variable size stack frame format
128
129//In a standard call, the first six items are passed in registers $16
130//- $21 and/or registers $f16 - $f21. (See Section 4.1.2 for details
131//of argument-to-register correspondence.) The remaining items are
132//collected in a memory argument list that is a naturally aligned
133//array of quadwords. In a standard call, this list, if present, must
134//be passed at 0(SP).
Misha Brukman7847fca2005-04-22 17:54:37 +0000135//7 ... n 0(SP) ... (n-7)*8(SP)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000136
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000137// //#define FP $15
138// //#define RA $26
139// //#define PV $27
140// //#define GP $29
141// //#define SP $30
Misha Brukman4633f1c2005-04-21 23:13:11 +0000142
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000143std::vector<SDOperand>
Misha Brukman4633f1c2005-04-21 23:13:11 +0000144AlphaTargetLowering::LowerArguments(Function &F, SelectionDAG &DAG)
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000145{
146 std::vector<SDOperand> ArgValues;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000147 std::vector<SDOperand> LS;
148 SDOperand Chain = DAG.getRoot();
149
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000150 // assert(0 && "TODO");
151 MachineFunction &MF = DAG.getMachineFunction();
Andrew Lenharth05380342005-02-07 05:07:00 +0000152 MachineFrameInfo*MFI = MF.getFrameInfo();
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000153
154 GP = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
155 MachineBasicBlock& BB = MF.front();
156
157 //Handle the return address
158 //BuildMI(&BB, Alpha::IDEF, 0, Alpha::R26);
159
Misha Brukman4633f1c2005-04-21 23:13:11 +0000160 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000161 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000162 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Misha Brukman7847fca2005-04-22 17:54:37 +0000163 Alpha::F19, Alpha::F20, Alpha::F21};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000164 int count = 0;
Andrew Lenharth2c9e38c2005-02-06 21:07:31 +0000165
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000166 //Def incoming registers
167 {
168 Function::arg_iterator I = F.arg_begin();
169 Function::arg_iterator E = F.arg_end();
170 for (int i = 0; i < 6; ++i)
171 {
172 if (F.isVarArg()) {
Andrew Lenharth0b04b5d2005-04-12 17:47:57 +0000173 MF.addLiveIn(args_int[i]);
174 MF.addLiveIn(args_float[i]);
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000175// BuildMI(&BB, Alpha::IDEF, 0, args_int[i]);
176// BuildMI(&BB, Alpha::IDEF, 0, args_float[i]);
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000177 } else if (I != E)
178 {
179 if(MVT::isInteger(getValueType(I->getType())))
Andrew Lenharth0b04b5d2005-04-12 17:47:57 +0000180 MF.addLiveIn(args_int[i]);
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000181// BuildMI(&BB, Alpha::IDEF, 0, args_int[i]);
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000182 else
Andrew Lenharth0b04b5d2005-04-12 17:47:57 +0000183 MF.addLiveIn(args_float[i]);
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000184// BuildMI(&BB, Alpha::IDEF, 0, args_float[i]);
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000185 ++I;
186 }
187 }
188 }
189
Andrew Lenharth0b04b5d2005-04-12 17:47:57 +0000190 MF.addLiveIn(Alpha::R29);
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000191// BuildMI(&BB, Alpha::IDEF, 0, Alpha::R29);
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000192 BuildMI(&BB, Alpha::BIS, 2, GP).addReg(Alpha::R29).addReg(Alpha::R29);
193
Chris Lattnere4d5c442005-03-15 04:54:21 +0000194 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000195 {
196 SDOperand newroot, argt;
197 if (count < 6) {
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000198 unsigned Vreg;
199 MVT::ValueType VT = getValueType(I->getType());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000200 switch (getValueType(I->getType())) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000201 default:
202 std::cerr << "Unknown Type " << VT << "\n";
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000203 abort();
204 case MVT::f64:
205 case MVT::f32:
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000206 Vreg = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(VT));
207 BuildMI(&BB, Alpha::CPYS, 2, Vreg).addReg(args_float[count]).addReg(args_float[count]);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000208 argt = newroot = DAG.getCopyFromReg(Vreg,
209 getValueType(I->getType()),
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000210 Chain);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000211 break;
212 case MVT::i1:
213 case MVT::i8:
214 case MVT::i16:
215 case MVT::i32:
216 case MVT::i64:
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000217 Vreg = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
218 BuildMI(&BB, Alpha::BIS, 2, Vreg).addReg(args_int[count]).addReg(args_int[count]);
219 argt = newroot = DAG.getCopyFromReg(Vreg, MVT::i64, Chain);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000220 if (getValueType(I->getType()) != MVT::i64)
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000221 argt = DAG.getNode(ISD::TRUNCATE, getValueType(I->getType()), newroot);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000222 break;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000223 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000224 } else { //more args
225 // Create the frame index object for this incoming parameter...
226 int FI = MFI->CreateFixedObject(8, 8 * (count - 6));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000227
228 // Create the SelectionDAG nodes corresponding to a load
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000229 //from this parameter
230 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i64);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000231 argt = newroot = DAG.getLoad(getValueType(I->getType()),
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000232 DAG.getEntryNode(), FIN);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000233 }
Andrew Lenharth032f2352005-02-22 21:59:48 +0000234 ++count;
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000235 LS.push_back(newroot.getValue(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000236 ArgValues.push_back(argt);
237 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000238
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000239 // If the functions takes variable number of arguments, copy all regs to stack
Misha Brukman4633f1c2005-04-21 23:13:11 +0000240 if (F.isVarArg())
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000241 for (int i = 0; i < 6; ++i)
242 {
243 unsigned Vreg = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::i64));
244 BuildMI(&BB, Alpha::BIS, 2, Vreg).addReg(args_int[i]).addReg(args_int[i]);
245 SDOperand argt = DAG.getCopyFromReg(Vreg, MVT::i64, Chain);
246 int FI = MFI->CreateFixedObject(8, -8 * (6 - i));
247 SDOperand SDFI = DAG.getFrameIndex(FI, MVT::i64);
248 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, argt, SDFI));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000249
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000250 Vreg = MF.getSSARegMap()->createVirtualRegister(getRegClassFor(MVT::f64));
251 BuildMI(&BB, Alpha::CPYS, 2, Vreg).addReg(args_float[i]).addReg(args_float[i]);
252 argt = DAG.getCopyFromReg(Vreg, MVT::f64, Chain);
253 FI = MFI->CreateFixedObject(8, - 8 * (12 - i));
254 SDFI = DAG.getFrameIndex(FI, MVT::i64);
255 LS.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, argt, SDFI));
256 }
257
258 // If the function takes variable number of arguments, make a frame index for
259 // the start of the first arg value... for expansion of llvm.va_start.
260 // if (F.isVarArg())
261 // VarArgsFrameIndex = MFI->CreateFixedObject(4, ArgOffset);
262
263 //Set up a token factor with all the stack traffic
264 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, LS));
Andrew Lenharthe1c5a002005-04-12 17:35:16 +0000265
266 // Finally, inform the code generator which regs we return values in.
267 switch (getValueType(F.getReturnType())) {
268 default: assert(0 && "Unknown type!");
269 case MVT::isVoid: break;
270 case MVT::i1:
271 case MVT::i8:
272 case MVT::i16:
273 case MVT::i32:
274 case MVT::i64:
275 MF.addLiveOut(Alpha::R0);
276 break;
277 case MVT::f32:
278 case MVT::f64:
279 MF.addLiveOut(Alpha::F0);
280 break;
281 }
282
Andrew Lenharth2513ddc2005-04-05 20:51:46 +0000283 //return the arguments
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000284 return ArgValues;
285}
286
287std::pair<SDOperand, SDOperand>
288AlphaTargetLowering::LowerCallTo(SDOperand Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000289 const Type *RetTy, bool isVarArg,
290 SDOperand Callee, ArgListTy &Args,
291 SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000292 int NumBytes = 0;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000293 if (Args.size() > 6)
294 NumBytes = (Args.size() - 6) * 8;
295
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000296 Chain = DAG.getNode(ISD::ADJCALLSTACKDOWN, MVT::Other, Chain,
Misha Brukman7847fca2005-04-22 17:54:37 +0000297 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000298 std::vector<SDOperand> args_to_use;
299 for (unsigned i = 0, e = Args.size(); i != e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000300 {
301 switch (getValueType(Args[i].second)) {
302 default: assert(0 && "Unexpected ValueType for argument!");
303 case MVT::i1:
304 case MVT::i8:
305 case MVT::i16:
306 case MVT::i32:
307 // Promote the integer to 64 bits. If the input type is signed use a
308 // sign extend, otherwise use a zero extend.
309 if (Args[i].second->isSigned())
310 Args[i].first = DAG.getNode(ISD::SIGN_EXTEND, MVT::i64, Args[i].first);
311 else
312 Args[i].first = DAG.getNode(ISD::ZERO_EXTEND, MVT::i64, Args[i].first);
313 break;
314 case MVT::i64:
315 case MVT::f64:
316 case MVT::f32:
317 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000318 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000319 args_to_use.push_back(Args[i].first);
320 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000321
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000322 std::vector<MVT::ValueType> RetVals;
323 MVT::ValueType RetTyVT = getValueType(RetTy);
324 if (RetTyVT != MVT::isVoid)
325 RetVals.push_back(RetTyVT);
326 RetVals.push_back(MVT::Other);
327
Misha Brukman4633f1c2005-04-21 23:13:11 +0000328 SDOperand TheCall = SDOperand(DAG.getCall(RetVals,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000329 Chain, Callee, args_to_use), 0);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000330 Chain = TheCall.getValue(RetTyVT != MVT::isVoid);
331 Chain = DAG.getNode(ISD::ADJCALLSTACKUP, MVT::Other, Chain,
332 DAG.getConstant(NumBytes, getPointerTy()));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000333 return std::make_pair(TheCall, Chain);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000334}
335
336std::pair<SDOperand, SDOperand>
337AlphaTargetLowering::LowerVAStart(SDOperand Chain, SelectionDAG &DAG) {
338 //vastart just returns the address of the VarArgsFrameIndex slot.
339 return std::make_pair(DAG.getFrameIndex(VarArgsFrameIndex, MVT::i64), Chain);
340}
341
342std::pair<SDOperand,SDOperand> AlphaTargetLowering::
343LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000344 const Type *ArgTy, SelectionDAG &DAG) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000345 abort();
346}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000347
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000348
349std::pair<SDOperand, SDOperand> AlphaTargetLowering::
350LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
351 SelectionDAG &DAG) {
352 abort();
353}
354
355
356
357
358
359namespace {
360
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000361//===--------------------------------------------------------------------===//
362/// ISel - Alpha specific code to select Alpha machine instructions for
363/// SelectionDAG operations.
364//===--------------------------------------------------------------------===//
365class ISel : public SelectionDAGISel {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000366
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000367 /// AlphaLowering - This object fully describes how to lower LLVM code to an
368 /// Alpha-specific SelectionDAG.
369 AlphaTargetLowering AlphaLowering;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000370
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000371 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
372 // for sdiv and udiv until it is put into the future
373 // dag combiner.
374
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000375 /// ExprMap - As shared expressions are codegen'd, we keep track of which
376 /// vreg the value is produced in, so we only emit one copy of each compiled
377 /// tree.
378 static const unsigned notIn = (unsigned)(-1);
379 std::map<SDOperand, unsigned> ExprMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000380
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000381 //CCInvMap sometimes (SetNE) we have the inverse CC code for free
382 std::map<SDOperand, unsigned> CCInvMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000383
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000384 int count_ins;
385 int count_outs;
386 bool has_sym;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000387 int max_depth;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000388
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000389public:
Misha Brukman4633f1c2005-04-21 23:13:11 +0000390 ISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering), AlphaLowering(TM)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000391 {}
Misha Brukman4633f1c2005-04-21 23:13:11 +0000392
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000393 /// InstructionSelectBasicBlock - This callback is invoked by
394 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
395 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
Andrew Lenharth032f2352005-02-22 21:59:48 +0000396 DEBUG(BB->dump());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000397 count_ins = 0;
398 count_outs = 0;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000399 max_depth = 0;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000400 has_sym = false;
401
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000402 // Codegen the basic block.
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000403 ISelDAG = &DAG;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000404 max_depth = DAG.getRoot().getNodeDepth();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000405 Select(DAG.getRoot());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000406
407 if(has_sym)
408 ++count_ins;
409 if(EnableAlphaCount)
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000410 std::cerr << "COUNT: " << BB->getParent()->getFunction ()->getName() << " "
411 << BB->getNumber() << " "
412 << max_depth << " "
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000413 << count_ins << " "
414 << count_outs << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000415
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000416 // Clear state used for selection.
417 ExprMap.clear();
418 CCInvMap.clear();
419 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000420
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000421 unsigned SelectExpr(SDOperand N);
422 unsigned SelectExprFP(SDOperand N, unsigned Result);
423 void Select(SDOperand N);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000424
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000425 void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
426 void SelectBranchCC(SDOperand N);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000427 void MoveFP2Int(unsigned src, unsigned dst, bool isDouble);
428 void MoveInt2FP(unsigned src, unsigned dst, bool isDouble);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000429 //returns whether the sense of the comparison was inverted
430 bool SelectFPSetCC(SDOperand N, unsigned dst);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000431
432 // dag -> dag expanders for integer divide by constant
433 SDOperand BuildSDIVSequence(SDOperand N);
434 SDOperand BuildUDIVSequence(SDOperand N);
435
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000436};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000437}
438
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000439//Factorize a number using the list of constants
440static bool factorize(int v[], int res[], int size, uint64_t c)
441{
442 bool cont = true;
443 while (c != 1 && cont)
444 {
445 cont = false;
446 for(int i = 0; i < size; ++i)
447 {
448 if (c % v[i] == 0)
449 {
450 c /= v[i];
451 ++res[i];
452 cont=true;
453 }
454 }
455 }
456 return c == 1;
457}
458
459
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000460//Shamelessly adapted from PPC32
Misha Brukman4633f1c2005-04-21 23:13:11 +0000461// Structure used to return the necessary information to codegen an SDIV as
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000462// a multiply.
463struct ms {
464 int64_t m; // magic number
465 int64_t s; // shift amount
466};
467
468struct mu {
469 uint64_t m; // magic number
470 int64_t a; // add indicator
471 int64_t s; // shift amount
472};
473
474/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukman4633f1c2005-04-21 23:13:11 +0000475/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000476/// or -1.
477static struct ms magic(int64_t d) {
478 int64_t p;
479 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
480 const uint64_t two63 = 9223372036854775808ULL; // 2^63
481 struct ms mag;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000482
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000483 ad = abs(d);
484 t = two63 + ((uint64_t)d >> 63);
485 anc = t - 1 - t%ad; // absolute value of nc
Andrew Lenharth320174f2005-04-07 17:19:16 +0000486 p = 63; // initialize p
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000487 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
488 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
489 q2 = two63/ad; // initialize q2 = 2p/abs(d)
490 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
491 do {
492 p = p + 1;
493 q1 = 2*q1; // update q1 = 2p/abs(nc)
494 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
495 if (r1 >= anc) { // must be unsigned comparison
496 q1 = q1 + 1;
497 r1 = r1 - anc;
498 }
499 q2 = 2*q2; // update q2 = 2p/abs(d)
500 r2 = 2*r2; // update r2 = rem(2p/abs(d))
501 if (r2 >= ad) { // must be unsigned comparison
502 q2 = q2 + 1;
503 r2 = r2 - ad;
504 }
505 delta = ad - r2;
506 } while (q1 < delta || (q1 == delta && r1 == 0));
507
508 mag.m = q2 + 1;
509 if (d < 0) mag.m = -mag.m; // resulting magic number
510 mag.s = p - 64; // resulting shift
511 return mag;
512}
513
514/// magicu - calculate the magic numbers required to codegen an integer udiv as
515/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
516static struct mu magicu(uint64_t d)
517{
518 int64_t p;
519 uint64_t nc, delta, q1, r1, q2, r2;
520 struct mu magu;
521 magu.a = 0; // initialize "add" indicator
522 nc = - 1 - (-d)%d;
Andrew Lenharth320174f2005-04-07 17:19:16 +0000523 p = 63; // initialize p
524 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
525 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
526 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
527 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000528 do {
529 p = p + 1;
530 if (r1 >= nc - r1 ) {
531 q1 = 2*q1 + 1; // update q1
532 r1 = 2*r1 - nc; // update r1
533 }
534 else {
535 q1 = 2*q1; // update q1
536 r1 = 2*r1; // update r1
537 }
538 if (r2 + 1 >= d - r2) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000539 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000540 q2 = 2*q2 + 1; // update q2
541 r2 = 2*r2 + 1 - d; // update r2
542 }
543 else {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000544 if (q2 >= 0x8000000000000000ull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000545 q2 = 2*q2; // update q2
546 r2 = 2*r2 + 1; // update r2
547 }
548 delta = d - 1 - r2;
549 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
550 magu.m = q2 + 1; // resulting magic number
Andrew Lenharth320174f2005-04-07 17:19:16 +0000551 magu.s = p - 64; // resulting shift
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000552 return magu;
553}
554
555/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
556/// return a DAG expression to select that will generate the same value by
557/// multiplying by a magic number. See:
558/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
559SDOperand ISel::BuildSDIVSequence(SDOperand N) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000560 int64_t d = (int64_t)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000561 ms magics = magic(d);
562 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000563 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000564 ISelDAG->getConstant(magics.m, MVT::i64));
565 // If d > 0 and m < 0, add the numerator
566 if (d > 0 && magics.m < 0)
567 Q = ISelDAG->getNode(ISD::ADD, MVT::i64, Q, N.getOperand(0));
568 // If d < 0 and m > 0, subtract the numerator.
569 if (d < 0 && magics.m > 0)
570 Q = ISelDAG->getNode(ISD::SUB, MVT::i64, Q, N.getOperand(0));
571 // Shift right algebraic if shift value is nonzero
572 if (magics.s > 0)
Misha Brukman4633f1c2005-04-21 23:13:11 +0000573 Q = ISelDAG->getNode(ISD::SRA, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000574 ISelDAG->getConstant(magics.s, MVT::i64));
575 // Extract the sign bit and add it to the quotient
Misha Brukman4633f1c2005-04-21 23:13:11 +0000576 SDOperand T =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000577 ISelDAG->getNode(ISD::SRL, MVT::i64, Q, ISelDAG->getConstant(63, MVT::i64));
578 return ISelDAG->getNode(ISD::ADD, MVT::i64, Q, T);
579}
580
581/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
582/// return a DAG expression to select that will generate the same value by
583/// multiplying by a magic number. See:
584/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
585SDOperand ISel::BuildUDIVSequence(SDOperand N) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000586 unsigned d =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000587 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
588 mu magics = magicu(d);
589 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000590 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000591 ISelDAG->getConstant(magics.m, MVT::i64));
592 if (magics.a == 0) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000593 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000594 ISelDAG->getConstant(magics.s, MVT::i64));
595 } else {
596 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i64, N.getOperand(0), Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000597 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000598 ISelDAG->getConstant(1, MVT::i64));
599 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i64, NPQ, Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000600 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000601 ISelDAG->getConstant(magics.s-1, MVT::i64));
602 }
603 return Q;
604}
605
Andrew Lenhartha565c272005-04-06 22:03:13 +0000606//From PPC32
607/// ExactLog2 - This function solves for (Val == 1 << (N-1)) and returns N. It
608/// returns zero when the input is not exactly a power of two.
609static unsigned ExactLog2(uint64_t Val) {
610 if (Val == 0 || (Val & (Val-1))) return 0;
611 unsigned Count = 0;
612 while (Val != 1) {
613 Val >>= 1;
614 ++Count;
615 }
616 return Count;
617}
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000618
619
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000620//These describe LDAx
Andrew Lenharthc0513832005-03-29 19:24:04 +0000621static const int IMM_LOW = -32768;
622static const int IMM_HIGH = 32767;
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000623static const int IMM_MULT = 65536;
624
625static long getUpper16(long l)
626{
627 long y = l / IMM_MULT;
628 if (l % IMM_MULT > IMM_HIGH)
629 ++y;
630 return y;
631}
632
633static long getLower16(long l)
634{
635 long h = getUpper16(l);
636 return l - h * IMM_MULT;
637}
638
Andrew Lenharth65838902005-02-06 16:22:15 +0000639static unsigned GetSymVersion(unsigned opcode)
640{
641 switch (opcode) {
642 default: assert(0 && "unknown load or store"); return 0;
643 case Alpha::LDQ: return Alpha::LDQ_SYM;
644 case Alpha::LDS: return Alpha::LDS_SYM;
645 case Alpha::LDT: return Alpha::LDT_SYM;
646 case Alpha::LDL: return Alpha::LDL_SYM;
647 case Alpha::LDBU: return Alpha::LDBU_SYM;
648 case Alpha::LDWU: return Alpha::LDWU_SYM;
649 case Alpha::LDW: return Alpha::LDW_SYM;
650 case Alpha::LDB: return Alpha::LDB_SYM;
651 case Alpha::STQ: return Alpha::STQ_SYM;
652 case Alpha::STS: return Alpha::STS_SYM;
653 case Alpha::STT: return Alpha::STT_SYM;
654 case Alpha::STL: return Alpha::STL_SYM;
655 case Alpha::STW: return Alpha::STW_SYM;
656 case Alpha::STB: return Alpha::STB_SYM;
657 }
658}
659
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000660void ISel::MoveFP2Int(unsigned src, unsigned dst, bool isDouble)
661{
662 unsigned Opc;
663 if (EnableAlphaFTOI) {
664 Opc = isDouble ? Alpha::FTOIT : Alpha::FTOIS;
665 BuildMI(BB, Opc, 1, dst).addReg(src);
666 } else {
667 //The hard way:
668 // Spill the integer to memory and reload it from there.
669 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
670 MachineFunction *F = BB->getParent();
671 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
672
673 Opc = isDouble ? Alpha::STT : Alpha::STS;
674 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
675 Opc = isDouble ? Alpha::LDQ : Alpha::LDL;
676 BuildMI(BB, Alpha::LDQ, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
677 }
678}
679
680void ISel::MoveInt2FP(unsigned src, unsigned dst, bool isDouble)
681{
682 unsigned Opc;
683 if (EnableAlphaFTOI) {
684 Opc = isDouble?Alpha::ITOFT:Alpha::ITOFS;
685 BuildMI(BB, Opc, 1, dst).addReg(src);
686 } else {
687 //The hard way:
688 // Spill the integer to memory and reload it from there.
689 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
690 MachineFunction *F = BB->getParent();
691 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
692
693 Opc = isDouble ? Alpha::STQ : Alpha::STL;
694 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
695 Opc = isDouble ? Alpha::LDT : Alpha::LDS;
696 BuildMI(BB, Opc, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
697 }
698}
699
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000700bool ISel::SelectFPSetCC(SDOperand N, unsigned dst)
701{
702 SDNode *Node = N.Val;
703 unsigned Opc, Tmp1, Tmp2, Tmp3;
704 SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node);
705
706 //assert(SetCC->getOperand(0).getValueType() != MVT::f32 && "SetCC f32 should have been promoted");
707 bool rev = false;
708 bool inv = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000709
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000710 switch (SetCC->getCondition()) {
711 default: Node->dump(); assert(0 && "Unknown FP comparison!");
712 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
713 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
714 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
715 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
716 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
717 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
718 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000719
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000720 //FIXME: check for constant 0.0
721 ConstantFPSDNode *CN;
722 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
723 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
724 Tmp1 = Alpha::F31;
725 else
726 Tmp1 = SelectExpr(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000727
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000728 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
729 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
730 Tmp2 = Alpha::F31;
731 else
732 Tmp2 = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000733
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000734 //Can only compare doubles, and dag won't promote for me
735 if (SetCC->getOperand(0).getValueType() == MVT::f32)
736 {
737 //assert(0 && "Setcc On float?\n");
738 std::cerr << "Setcc on float!\n";
739 Tmp3 = MakeReg(MVT::f64);
740 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
741 Tmp1 = Tmp3;
742 }
743 if (SetCC->getOperand(1).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(Tmp2);
749 Tmp2 = Tmp3;
750 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000751
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000752 if (rev) std::swap(Tmp1, Tmp2);
753 //do the comparison
754 BuildMI(BB, Opc, 2, dst).addReg(Tmp1).addReg(Tmp2);
755 return inv;
756}
757
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000758//Check to see if the load is a constant offset from a base register
759void ISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
760{
761 unsigned opcode = N.getOpcode();
762 if (opcode == ISD::ADD) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000763 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000764 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
765 { //Normal imm add
766 Reg = SelectExpr(N.getOperand(0));
767 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
768 return;
769 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000770 else if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000771 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 32767)
772 {
773 Reg = SelectExpr(N.getOperand(1));
774 offset = cast<ConstantSDNode>(N.getOperand(0))->getValue();
775 return;
776 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000777 }
778 Reg = SelectExpr(N);
779 offset = 0;
780 return;
781}
782
Andrew Lenharth445171a2005-02-08 00:40:03 +0000783void ISel::SelectBranchCC(SDOperand N)
784{
785 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000786 MachineBasicBlock *Dest =
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000787 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
788 unsigned Opc = Alpha::WTF;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000789
Andrew Lenharth445171a2005-02-08 00:40:03 +0000790 Select(N.getOperand(0)); //chain
791 SDOperand CC = N.getOperand(1);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000792
Andrew Lenharth445171a2005-02-08 00:40:03 +0000793 if (CC.getOpcode() == ISD::SETCC)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000794 {
795 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
796 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
797 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000798 bool LeftZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
799 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
800 bool RightZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
801 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000802 bool isNE = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000803
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000804 //Fix up CC
805 ISD::CondCode cCode= SetCC->getCondition();
806 if (LeftZero && !RightZero) //Swap Operands
807 cCode = ISD::getSetCCSwappedOperands(cCode);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000808
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000809 if(cCode == ISD::SETNE)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000810 isNE = true;
Andrew Lenharth445171a2005-02-08 00:40:03 +0000811
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000812 if (LeftZero || RightZero) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000813 switch (SetCC->getCondition()) {
814 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
815 case ISD::SETEQ: Opc = Alpha::BEQ; break;
816 case ISD::SETLT: Opc = Alpha::BLT; break;
817 case ISD::SETLE: Opc = Alpha::BLE; break;
818 case ISD::SETGT: Opc = Alpha::BGT; break;
819 case ISD::SETGE: Opc = Alpha::BGE; break;
820 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
821 case ISD::SETUGT: Opc = Alpha::BNE; break;
822 case ISD::SETULE: Opc = Alpha::BEQ; break; //Technically you could have this CC
823 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
824 case ISD::SETNE: Opc = Alpha::BNE; break;
825 }
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000826 unsigned Tmp1;
827 if(LeftZero && !RightZero) //swap Operands
828 Tmp1 = SelectExpr(SetCC->getOperand(1)); //Cond
829 else
830 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000831 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
832 return;
833 } else {
834 unsigned Tmp1 = SelectExpr(CC);
835 if (isNE)
836 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
837 else
838 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000839 return;
840 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000841 } else { //FP
842 //Any comparison between 2 values should be codegened as an folded branch, as moving
843 //CC to the integer register is very expensive
844 //for a cmp b: c = a - b;
845 //a = b: c = 0
846 //a < b: c < 0
847 //a > b: c > 0
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000848
849 bool invTest = false;
850 unsigned Tmp3;
851
852 ConstantFPSDNode *CN;
853 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
854 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
855 Tmp3 = SelectExpr(SetCC->getOperand(0));
856 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
857 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
858 {
859 Tmp3 = SelectExpr(SetCC->getOperand(1));
860 invTest = true;
861 }
862 else
863 {
864 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
865 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
866 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
867 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
868 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
869 .addReg(Tmp1).addReg(Tmp2);
870 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000871
872 switch (SetCC->getCondition()) {
873 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000874 case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
875 case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
876 case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
877 case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
878 case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
879 case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000880 }
881 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000882 return;
883 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000884 abort(); //Should never be reached
885 } else {
886 //Giveup and do the stupid thing
887 unsigned Tmp1 = SelectExpr(CC);
888 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
889 return;
890 }
Andrew Lenharth445171a2005-02-08 00:40:03 +0000891 abort(); //Should never be reached
892}
893
Andrew Lenharth40831c52005-01-28 06:57:18 +0000894unsigned ISel::SelectExprFP(SDOperand N, unsigned Result)
895{
896 unsigned Tmp1, Tmp2, Tmp3;
897 unsigned Opc = 0;
898 SDNode *Node = N.Val;
899 MVT::ValueType DestType = N.getValueType();
900 unsigned opcode = N.getOpcode();
901
902 switch (opcode) {
903 default:
904 Node->dump();
905 assert(0 && "Node not handled!\n");
Andrew Lenharth2c594352005-01-29 15:42:07 +0000906
Andrew Lenharth7332f3e2005-04-02 19:11:07 +0000907 case ISD::UNDEF: {
908 BuildMI(BB, Alpha::IDEF, 0, Result);
909 return Result;
910 }
911
Andrew Lenharth30b46d42005-04-02 19:04:58 +0000912 case ISD::FNEG:
913 if(ISD::FABS == N.getOperand(0).getOpcode())
914 {
Misha Brukman7847fca2005-04-22 17:54:37 +0000915 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
916 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +0000917 } else {
Misha Brukman7847fca2005-04-22 17:54:37 +0000918 Tmp1 = SelectExpr(N.getOperand(0));
919 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth30b46d42005-04-02 19:04:58 +0000920 }
921 return Result;
922
923 case ISD::FABS:
924 Tmp1 = SelectExpr(N.getOperand(0));
925 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
926 return Result;
927
Andrew Lenharth9818c052005-02-05 13:19:12 +0000928 case ISD::SELECT:
929 {
Andrew Lenharth45859692005-03-03 21:47:53 +0000930 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
931 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
932 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
933
934 SDOperand CC = N.getOperand(0);
935 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
936
Misha Brukman4633f1c2005-04-21 23:13:11 +0000937 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth45859692005-03-03 21:47:53 +0000938 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
939 { //FP Setcc -> Select yay!
Andrew Lenharthd4bdd542005-02-05 16:41:03 +0000940
941
Andrew Lenharth45859692005-03-03 21:47:53 +0000942 //for a cmp b: c = a - b;
943 //a = b: c = 0
944 //a < b: c < 0
945 //a > b: c > 0
Misha Brukman4633f1c2005-04-21 23:13:11 +0000946
Andrew Lenharth45859692005-03-03 21:47:53 +0000947 bool invTest = false;
948 unsigned Tmp3;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000949
Andrew Lenharth45859692005-03-03 21:47:53 +0000950 ConstantFPSDNode *CN;
951 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
952 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
953 Tmp3 = SelectExpr(SetCC->getOperand(0));
954 else if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
955 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
956 {
957 Tmp3 = SelectExpr(SetCC->getOperand(1));
958 invTest = true;
959 }
960 else
961 {
962 unsigned Tmp1 = SelectExpr(SetCC->getOperand(0));
963 unsigned Tmp2 = SelectExpr(SetCC->getOperand(1));
964 bool isD = SetCC->getOperand(0).getValueType() == MVT::f64;
965 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
966 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
967 .addReg(Tmp1).addReg(Tmp2);
968 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000969
Andrew Lenharth45859692005-03-03 21:47:53 +0000970 switch (SetCC->getCondition()) {
971 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
972 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNE : Alpha::FCMOVEQ; break;
973 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGT : Alpha::FCMOVLT; break;
974 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGE : Alpha::FCMOVLE; break;
975 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLT : Alpha::FCMOVGT; break;
976 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLE : Alpha::FCMOVGE; break;
977 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQ : Alpha::FCMOVNE; break;
978 }
Andrew Lenharth33819132005-03-04 20:09:23 +0000979 BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
Andrew Lenharth45859692005-03-03 21:47:53 +0000980 return Result;
981 }
982 else
983 {
984 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000985 BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV).addReg(Tmp1);
986// // Spill the cond to memory and reload it from there.
987// unsigned Tmp4 = MakeReg(MVT::f64);
988// MoveIntFP(Tmp1, Tmp4, true);
989// //now ideally, we don't have to do anything to the flag...
990// // Get the condition into the zero flag.
991// BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
Andrew Lenharth45859692005-03-03 21:47:53 +0000992 return Result;
993 }
Andrew Lenharth9818c052005-02-05 13:19:12 +0000994 }
995
Andrew Lenharthc1faced2005-02-01 01:37:24 +0000996 case ISD::FP_ROUND:
Misha Brukman4633f1c2005-04-21 23:13:11 +0000997 assert (DestType == MVT::f32 &&
998 N.getOperand(0).getValueType() == MVT::f64 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000999 "only f64 to f32 conversion supported here");
Andrew Lenharthc1faced2005-02-01 01:37:24 +00001000 Tmp1 = SelectExpr(N.getOperand(0));
1001 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
1002 return Result;
1003
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001004 case ISD::FP_EXTEND:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001005 assert (DestType == MVT::f64 &&
1006 N.getOperand(0).getValueType() == MVT::f32 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001007 "only f32 to f64 conversion supported here");
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001008 Tmp1 = SelectExpr(N.getOperand(0));
1009 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
1010 return Result;
1011
Andrew Lenharth2c594352005-01-29 15:42:07 +00001012 case ISD::CopyFromReg:
1013 {
1014 // Make sure we generate both values.
1015 if (Result != notIn)
1016 ExprMap[N.getValue(1)] = notIn; // Generate the token
1017 else
1018 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001019
Andrew Lenharth2c594352005-01-29 15:42:07 +00001020 SDOperand Chain = N.getOperand(0);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001021
Andrew Lenharth2c594352005-01-29 15:42:07 +00001022 Select(Chain);
1023 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1024 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1025 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
1026 return Result;
1027 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001028
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001029 case ISD::LOAD:
1030 {
1031 // Make sure we generate both values.
1032 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001033 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001034 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001035 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001036
Andrew Lenharth29219162005-02-07 06:31:44 +00001037 DestType = N.getValue(0).getValueType();
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001038
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001039 SDOperand Chain = N.getOperand(0);
1040 SDOperand Address = N.getOperand(1);
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001041 Select(Chain);
Andrew Lenharth65838902005-02-06 16:22:15 +00001042 Opc = DestType == MVT::f64 ? Alpha::LDT : Alpha::LDS;
1043
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001044 if (Address.getOpcode() == ISD::GlobalAddress) {
1045 AlphaLowering.restoreGP(BB);
1046 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001047 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001048 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1049 }
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001050 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001051 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001052 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001053 has_sym = true;
Andrew Lenharth97127a12005-02-05 17:41:39 +00001054 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001055 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001056 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001057 BuildMI(BB, Opc, 2, Result)
1058 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1059 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001060 } else {
1061 long offset;
1062 SelectAddr(Address, Tmp1, offset);
1063 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1064 }
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001065 return Result;
1066 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001067 case ISD::ConstantFP:
1068 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
1069 if (CN->isExactlyValue(+0.0)) {
1070 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001071 } else if ( CN->isExactlyValue(-0.0)) {
1072 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Alpha::F31);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001073 } else {
1074 abort();
1075 }
1076 }
1077 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001078
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001079 case ISD::SDIV:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001080 case ISD::MUL:
1081 case ISD::ADD:
1082 case ISD::SUB:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001083 switch( opcode ) {
1084 case ISD::MUL: Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS; break;
1085 case ISD::ADD: Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS; break;
1086 case ISD::SUB: Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS; break;
1087 case ISD::SDIV: Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS; break;
1088 };
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001089
1090 ConstantFPSDNode *CN;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001091 if (opcode == ISD::SUB
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001092 && (CN = dyn_cast<ConstantFPSDNode>(N.getOperand(0)))
1093 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1094 {
1095 Tmp2 = SelectExpr(N.getOperand(1));
1096 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp2).addReg(Tmp2);
1097 } else {
1098 Tmp1 = SelectExpr(N.getOperand(0));
1099 Tmp2 = SelectExpr(N.getOperand(1));
1100 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1101 }
Andrew Lenharth40831c52005-01-28 06:57:18 +00001102 return Result;
1103
Andrew Lenharth2c594352005-01-29 15:42:07 +00001104 case ISD::EXTLOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001105 {
1106 //include a conversion sequence for float loads to double
1107 if (Result != notIn)
1108 ExprMap[N.getValue(1)] = notIn; // Generate the token
1109 else
1110 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001111
Andrew Lenhartha549deb2005-02-07 05:33:15 +00001112 Tmp1 = MakeReg(MVT::f32);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001113
1114 assert(cast<MVTSDNode>(Node)->getExtraValueType() == MVT::f32 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001115 "EXTLOAD not from f32");
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001116 assert(Node->getValueType(0) == MVT::f64 && "EXTLOAD not to f64");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001117
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001118 SDOperand Chain = N.getOperand(0);
1119 SDOperand Address = N.getOperand(1);
1120 Select(Chain);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001121
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001122 if (Address.getOpcode() == ISD::GlobalAddress) {
1123 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001124 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001125 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1126 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001127 else if (ConstantPoolSDNode *CP =
1128 dyn_cast<ConstantPoolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001129 {
1130 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001131 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001132 BuildMI(BB, Alpha::LDS_SYM, 1, Tmp1).addConstantPoolIndex(CP->getIndex());
1133 }
1134 else if(Address.getOpcode() == ISD::FrameIndex) {
1135 Tmp2 = cast<FrameIndexSDNode>(Address)->getIndex();
Andrew Lenharth032f2352005-02-22 21:59:48 +00001136 BuildMI(BB, Alpha::LDS, 2, Tmp1)
1137 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1138 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001139 } else {
1140 long offset;
1141 SelectAddr(Address, Tmp2, offset);
1142 BuildMI(BB, Alpha::LDS, 1, Tmp1).addImm(offset).addReg(Tmp2);
1143 }
Andrew Lenharth29219162005-02-07 06:31:44 +00001144 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
Andrew Lenharth12dd2622005-02-03 21:01:15 +00001145 return Result;
1146 }
Andrew Lenharth2c594352005-01-29 15:42:07 +00001147
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001148 case ISD::UINT_TO_FP:
1149 case ISD::SINT_TO_FP:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001150 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001151 assert (N.getOperand(0).getValueType() == MVT::i64
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001152 && "only quads can be loaded from");
Andrew Lenharth40831c52005-01-28 06:57:18 +00001153 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001154 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001155 MoveInt2FP(Tmp1, Tmp2, true);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001156 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
1157 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Andrew Lenharth40831c52005-01-28 06:57:18 +00001158 return Result;
1159 }
1160 }
1161 assert(0 && "should not get here");
1162 return 0;
1163}
1164
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001165unsigned ISel::SelectExpr(SDOperand N) {
1166 unsigned Result;
Andrew Lenharth2966e842005-04-07 18:15:28 +00001167 unsigned Tmp1, Tmp2 = 0, Tmp3;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001168 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001169 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001170
1171 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001172 MVT::ValueType DestType = N.getValueType();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001173
1174 unsigned &Reg = ExprMap[N];
1175 if (Reg) return Reg;
1176
1177 if (N.getOpcode() != ISD::CALL)
1178 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001179 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001180 else {
1181 // If this is a call instruction, make sure to prepare ALL of the result
1182 // values as well as the chain.
1183 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001184 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001185 else {
1186 Result = MakeReg(Node->getValueType(0));
1187 ExprMap[N.getValue(0)] = Result;
1188 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
1189 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001190 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001191 }
1192 }
1193
Andrew Lenharth22088bb2005-02-02 15:05:33 +00001194 if (DestType == MVT::f64 || DestType == MVT::f32 ||
Andrew Lenharth06342c32005-02-07 06:21:37 +00001195 (
Misha Brukman4633f1c2005-04-21 23:13:11 +00001196 (opcode == ISD::LOAD || opcode == ISD::CopyFromReg ||
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001197 opcode == ISD::EXTLOAD) &&
Misha Brukman4633f1c2005-04-21 23:13:11 +00001198 (N.getValue(0).getValueType() == MVT::f32 ||
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001199 N.getValue(0).getValueType() == MVT::f64)
Andrew Lenharth06342c32005-02-07 06:21:37 +00001200 )
1201 )
Andrew Lenharth40831c52005-01-28 06:57:18 +00001202 return SelectExprFP(N, Result);
1203
1204 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001205 default:
1206 Node->dump();
1207 assert(0 && "Node not handled!\n");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001208
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001209 case ISD::MULHU:
1210 Tmp1 = SelectExpr(N.getOperand(0));
1211 Tmp2 = SelectExpr(N.getOperand(1));
1212 BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth706be912005-04-07 13:55:53 +00001213 return Result;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001214 case ISD::MULHS:
1215 {
1216 //MULHU - Ra<63>*Rb - Rb<63>*Ra
1217 Tmp1 = SelectExpr(N.getOperand(0));
1218 Tmp2 = SelectExpr(N.getOperand(1));
1219 Tmp3 = MakeReg(MVT::i64);
1220 BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1221 unsigned V1 = MakeReg(MVT::i64);
1222 unsigned V2 = MakeReg(MVT::i64);
1223 BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31).addReg(Tmp1);
1224 BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31).addReg(Tmp2);
1225 unsigned IRes = MakeReg(MVT::i64);
1226 BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
1227 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
1228 return Result;
1229 }
Andrew Lenharth7332f3e2005-04-02 19:11:07 +00001230 case ISD::UNDEF: {
1231 BuildMI(BB, Alpha::IDEF, 0, Result);
1232 return Result;
1233 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001234
Andrew Lenharth032f2352005-02-22 21:59:48 +00001235 case ISD::DYNAMIC_STACKALLOC:
1236 // Generate both result values.
Andrew Lenharth3a7118d2005-02-23 17:33:42 +00001237 if (Result != notIn)
1238 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth032f2352005-02-22 21:59:48 +00001239 else
1240 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
1241
1242 // FIXME: We are currently ignoring the requested alignment for handling
1243 // greater than the stack alignment. This will need to be revisited at some
1244 // point. Align = N.getOperand(2);
1245
1246 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
1247 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
1248 std::cerr << "Cannot allocate stack object with greater alignment than"
1249 << " the stack alignment yet!";
1250 abort();
1251 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001252
Andrew Lenharth032f2352005-02-22 21:59:48 +00001253 Select(N.getOperand(0));
1254 if (ConstantSDNode* CN = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1255 {
1256 if (CN->getValue() < 32000)
1257 {
1258 BuildMI(BB, Alpha::LDA, 2, Alpha::R30)
1259 .addImm(-CN->getValue()).addReg(Alpha::R30);
1260 } else {
1261 Tmp1 = SelectExpr(N.getOperand(1));
1262 // Subtract size from stack pointer, thereby allocating some space.
1263 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1264 }
1265 } else {
1266 Tmp1 = SelectExpr(N.getOperand(1));
1267 // Subtract size from stack pointer, thereby allocating some space.
1268 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
1269 }
1270
1271 // Put a pointer to the space into the result register, by copying the stack
1272 // pointer.
Andrew Lenharth7bc47022005-02-22 23:29:25 +00001273 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
Andrew Lenharth032f2352005-02-22 21:59:48 +00001274 return Result;
1275
Andrew Lenharth33819132005-03-04 20:09:23 +00001276// case ISD::ConstantPool:
1277// Tmp1 = cast<ConstantPoolSDNode>(N)->getIndex();
1278// AlphaLowering.restoreGP(BB);
1279// BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(Tmp1);
1280// return Result;
Andrew Lenharth2c594352005-01-29 15:42:07 +00001281
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001282 case ISD::FrameIndex:
Andrew Lenharth032f2352005-02-22 21:59:48 +00001283 BuildMI(BB, Alpha::LDA, 2, Result)
1284 .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
1285 .addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001286 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001287
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001288 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001289 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001290 case ISD::SEXTLOAD:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001291 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001292 {
1293 // Make sure we generate both values.
1294 if (Result != notIn)
1295 ExprMap[N.getValue(1)] = notIn; // Generate the token
1296 else
1297 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001298
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001299 SDOperand Chain = N.getOperand(0);
1300 SDOperand Address = N.getOperand(1);
1301 Select(Chain);
1302
Misha Brukman4633f1c2005-04-21 23:13:11 +00001303 assert(Node->getValueType(0) == MVT::i64 &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001304 "Unknown type to sign extend to.");
Andrew Lenharth03824012005-02-07 05:55:55 +00001305 if (opcode == ISD::LOAD)
1306 Opc = Alpha::LDQ;
1307 else
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001308 switch (cast<MVTSDNode>(Node)->getExtraValueType()) {
1309 default: Node->dump(); assert(0 && "Bad sign extend!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001310 case MVT::i32: Opc = Alpha::LDL;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001311 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001312 case MVT::i16: Opc = Alpha::LDWU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001313 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +00001314 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Misha Brukman4633f1c2005-04-21 23:13:11 +00001315 case MVT::i8: Opc = Alpha::LDBU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001316 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001317 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001318
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001319 if (Address.getOpcode() == ISD::GlobalAddress) {
1320 AlphaLowering.restoreGP(BB);
1321 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001322 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001323 BuildMI(BB, Opc, 1, Result).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
1324 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001325 else if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(Address)) {
1326 AlphaLowering.restoreGP(BB);
Andrew Lenharth65838902005-02-06 16:22:15 +00001327 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001328 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001329 BuildMI(BB, Opc, 1, Result).addConstantPoolIndex(CP->getIndex());
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001330 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001331 else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharth032f2352005-02-22 21:59:48 +00001332 BuildMI(BB, Opc, 2, Result)
1333 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1334 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001335 } else {
1336 long offset;
1337 SelectAddr(Address, Tmp1, offset);
1338 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
1339 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001340 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001341 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001342
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001343 case ISD::GlobalAddress:
1344 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001345 has_sym = true;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001346 BuildMI(BB, Alpha::LOAD_ADDR, 1, Result)
1347 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal());
1348 return Result;
1349
1350 case ISD::CALL:
1351 {
1352 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001353
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001354 // The chain for this call is now lowered.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001355 ExprMap.insert(std::make_pair(N.getValue(Node->getNumValues()-1), notIn));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001356
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001357 //grab the arguments
1358 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001359 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001360 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001361 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001362
Andrew Lenharth684f2292005-01-30 00:35:27 +00001363 //in reg args
1364 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001365 {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001366 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001367 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +00001368 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001369 Alpha::F19, Alpha::F20, Alpha::F21};
1370 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001371 default:
1372 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001373 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001374 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001375 N.getOperand(i+2).getValueType() << "\n";
1376 assert(0 && "Unknown value type for call");
1377 case MVT::i1:
1378 case MVT::i8:
1379 case MVT::i16:
1380 case MVT::i32:
1381 case MVT::i64:
1382 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1383 break;
1384 case MVT::f32:
1385 case MVT::f64:
1386 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i]).addReg(argvregs[i]);
1387 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001388 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001389 }
Andrew Lenharth684f2292005-01-30 00:35:27 +00001390 //in mem args
1391 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001392 {
1393 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001394 default:
1395 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001396 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001397 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001398 N.getOperand(i+2).getValueType() << "\n";
1399 assert(0 && "Unknown value type for call");
1400 case MVT::i1:
1401 case MVT::i8:
1402 case MVT::i16:
1403 case MVT::i32:
1404 case MVT::i64:
1405 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1406 break;
1407 case MVT::f32:
1408 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1409 break;
1410 case MVT::f64:
1411 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8).addReg(Alpha::R30);
1412 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +00001413 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001414 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001415 //build the right kind of call
1416 if (GlobalAddressSDNode *GASD =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001417 dyn_cast<GlobalAddressSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001418 {
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001419 if (GASD->getGlobal()->isExternal()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001420 //use safe calling convention
Andrew Lenharth7b2a5272005-01-30 20:42:36 +00001421 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001422 has_sym = true;
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001423 BuildMI(BB, Alpha::CALL, 1).addGlobalAddress(GASD->getGlobal());
1424 } else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001425 //use PC relative branch call
Andrew Lenharth1e0d9bd2005-04-14 17:34:20 +00001426 AlphaLowering.restoreGP(BB);
Andrew Lenharthc24b5372005-04-13 17:17:28 +00001427 BuildMI(BB, Alpha::BSR, 1, Alpha::R26).addGlobalAddress(GASD->getGlobal(),true);
1428 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001429 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001430 else if (ExternalSymbolSDNode *ESSDN =
Misha Brukman4633f1c2005-04-21 23:13:11 +00001431 dyn_cast<ExternalSymbolSDNode>(N.getOperand(1)))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001432 {
1433 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001434 has_sym = true;
Andrew Lenharthba05ad62005-03-30 18:22:52 +00001435 BuildMI(BB, Alpha::CALL, 1).addExternalSymbol(ESSDN->getSymbol(), true);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001436 } else {
1437 //no need to restore GP as we are doing an indirect call
1438 Tmp1 = SelectExpr(N.getOperand(1));
1439 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
1440 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
1441 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001442
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001443 //push the result into a virtual register
Misha Brukman4633f1c2005-04-21 23:13:11 +00001444
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001445 switch (Node->getValueType(0)) {
1446 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001447 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001448 case MVT::i1:
1449 case MVT::i8:
1450 case MVT::i16:
1451 case MVT::i32:
1452 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001453 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
1454 break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001455 case MVT::f32:
1456 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +00001457 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
1458 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001459 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001460 return Result+N.ResNo;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001461 }
1462
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001463 case ISD::SIGN_EXTEND_INREG:
1464 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001465 //do SDIV opt for all levels of ints if not dividing by a constant
1466 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
1467 && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001468 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001469 unsigned Tmp4 = MakeReg(MVT::f64);
1470 unsigned Tmp5 = MakeReg(MVT::f64);
1471 unsigned Tmp6 = MakeReg(MVT::f64);
1472 unsigned Tmp7 = MakeReg(MVT::f64);
1473 unsigned Tmp8 = MakeReg(MVT::f64);
1474 unsigned Tmp9 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001475
1476 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1477 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1478 MoveInt2FP(Tmp1, Tmp4, true);
1479 MoveInt2FP(Tmp2, Tmp5, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001480 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Tmp4);
1481 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Tmp5);
1482 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
1483 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Tmp8);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001484 MoveFP2Int(Tmp9, Result, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001485 return Result;
1486 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001487
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001488 //Alpha has instructions for a bunch of signed 32 bit stuff
1489 if( dyn_cast<MVTSDNode>(Node)->getExtraValueType() == MVT::i32)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001490 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001491 switch (N.getOperand(0).getOpcode()) {
1492 case ISD::ADD:
1493 case ISD::SUB:
1494 case ISD::MUL:
1495 {
1496 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
1497 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
1498 //FIXME: first check for Scaled Adds and Subs!
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001499 ConstantSDNode* CSD = NULL;
1500 if(!isMul && N.getOperand(0).getOperand(0).getOpcode() == ISD::SHL &&
1501 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(0).getOperand(1))) &&
1502 (CSD->getValue() == 2 || CSD->getValue() == 3))
1503 {
1504 bool use4 = CSD->getValue() == 2;
1505 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1506 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1507 BuildMI(BB, isAdd?(use4?Alpha::S4ADDL:Alpha::S8ADDL):(use4?Alpha::S4SUBL:Alpha::S8SUBL),
1508 2,Result).addReg(Tmp1).addReg(Tmp2);
1509 }
1510 else if(isAdd && N.getOperand(0).getOperand(1).getOpcode() == ISD::SHL &&
1511 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1).getOperand(1))) &&
1512 (CSD->getValue() == 2 || CSD->getValue() == 3))
1513 {
1514 bool use4 = CSD->getValue() == 2;
1515 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1516 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1517 BuildMI(BB, use4?Alpha::S4ADDL:Alpha::S8ADDL, 2,Result).addReg(Tmp1).addReg(Tmp2);
1518 }
1519 else if(N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001520 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue() <= 255)
1521 { //Normal imm add/sub
1522 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001523 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001524 Tmp2 = cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getValue();
1525 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001526 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001527 else
1528 { //Normal add/sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001529 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULL : Alpha::SUBL);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001530 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001531 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001532 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1533 }
1534 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001535 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001536 default: break; //Fall Though;
1537 }
1538 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001539 Tmp1 = SelectExpr(N.getOperand(0));
1540 MVTSDNode* MVN = dyn_cast<MVTSDNode>(Node);
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001541 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001542 switch(MVN->getExtraValueType())
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001543 {
1544 default:
1545 Node->dump();
1546 assert(0 && "Sign Extend InReg not there yet");
1547 break;
1548 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001549 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001550 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001551 break;
1552 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001553 case MVT::i16:
1554 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
1555 break;
1556 case MVT::i8:
1557 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
1558 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +00001559 case MVT::i1:
1560 Tmp2 = MakeReg(MVT::i64);
1561 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001562 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +00001563 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001564 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001565 return Result;
1566 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001567
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001568 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001569 {
1570 if (SetCCSDNode *SetCC = dyn_cast<SetCCSDNode>(Node)) {
1571 if (MVT::isInteger(SetCC->getOperand(0).getValueType())) {
1572 bool isConst1 = false;
1573 bool isConst2 = false;
1574 int dir;
Misha Brukman7847fca2005-04-22 17:54:37 +00001575
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001576 //Tmp1 = SelectExpr(N.getOperand(0));
1577 if(N.getOperand(0).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001578 cast<ConstantSDNode>(N.getOperand(0))->getValue() <= 255)
1579 isConst1 = true;
1580 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001581 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
1582 isConst2 = true;
1583
1584 switch (SetCC->getCondition()) {
1585 default: Node->dump(); assert(0 && "Unknown integer comparison!");
1586 case ISD::SETEQ: Opc = Alpha::CMPEQ; dir=0; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001587 case ISD::SETLT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001588 Opc = isConst2 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001589 case ISD::SETLE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001590 Opc = isConst2 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001591 case ISD::SETGT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001592 Opc = isConst1 ? Alpha::CMPLTi : Alpha::CMPLT; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001593 case ISD::SETGE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001594 Opc = isConst1 ? Alpha::CMPLEi : Alpha::CMPLE; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001595 case ISD::SETULT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001596 Opc = isConst2 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001597 case ISD::SETUGT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001598 Opc = isConst1 ? Alpha::CMPULTi : Alpha::CMPULT; dir = 2; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001599 case ISD::SETULE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001600 Opc = isConst2 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001601 case ISD::SETUGE:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001602 Opc = isConst1 ? Alpha::CMPULEi : Alpha::CMPULE; dir = 2; break;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001603 case ISD::SETNE: {//Handle this one special
1604 //std::cerr << "Alpha does not have a setne.\n";
1605 //abort();
1606 Tmp1 = SelectExpr(N.getOperand(0));
1607 Tmp2 = SelectExpr(N.getOperand(1));
1608 Tmp3 = MakeReg(MVT::i64);
1609 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth445171a2005-02-08 00:40:03 +00001610 //Remeber we have the Inv for this CC
1611 CCInvMap[N] = Tmp3;
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001612 //and invert
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001613 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001614 return Result;
1615 }
1616 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001617 if (dir == 1) {
1618 Tmp1 = SelectExpr(N.getOperand(0));
1619 if (isConst2) {
1620 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1621 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1622 } else {
1623 Tmp2 = SelectExpr(N.getOperand(1));
1624 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1625 }
1626 } else if (dir == 2) {
1627 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001628 if (isConst1) {
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001629 Tmp2 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1630 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1631 } else {
1632 Tmp2 = SelectExpr(N.getOperand(0));
1633 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1634 }
1635 } else { //dir == 0
1636 if (isConst1) {
1637 Tmp1 = cast<ConstantSDNode>(N.getOperand(0))->getValue();
1638 Tmp2 = SelectExpr(N.getOperand(1));
1639 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp2).addImm(Tmp1);
1640 } else if (isConst2) {
1641 Tmp1 = SelectExpr(N.getOperand(0));
1642 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1643 BuildMI(BB, Alpha::CMPEQi, 2, Result).addReg(Tmp1).addImm(Tmp2);
1644 } else {
1645 Tmp1 = SelectExpr(N.getOperand(0));
1646 Tmp2 = SelectExpr(N.getOperand(1));
1647 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Tmp1).addReg(Tmp2);
1648 }
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001649 }
1650 } else {
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001651 //do the comparison
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001652 Tmp1 = MakeReg(MVT::f64);
1653 bool inv = SelectFPSetCC(N, Tmp1);
1654
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001655 //now arrange for Result (int) to have a 1 or 0
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001656 Tmp2 = MakeReg(MVT::i64);
1657 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001658 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001659 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001660 }
Andrew Lenharth9818c052005-02-05 13:19:12 +00001661 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001662 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001663 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001664
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001665 case ISD::CopyFromReg:
1666 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001667 ++count_ins;
1668
Andrew Lenharth40831c52005-01-28 06:57:18 +00001669 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001670 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001671 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001672 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001673 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001674
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001675 SDOperand Chain = N.getOperand(0);
1676
1677 Select(Chain);
1678 unsigned r = dyn_cast<RegSDNode>(Node)->getReg();
1679 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
1680 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
1681 return Result;
1682 }
1683
Misha Brukman4633f1c2005-04-21 23:13:11 +00001684 //Most of the plain arithmetic and logic share the same form, and the same
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001685 //constant immediate test
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001686 case ISD::XOR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001687 //Match Not
1688 if (N.getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001689 cast<ConstantSDNode>(N.getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001690 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001691 Tmp1 = SelectExpr(N.getOperand(0));
1692 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1693 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001694 }
1695 //Fall through
1696 case ISD::AND:
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001697 //handle zap
1698 if (opcode == ISD::AND && N.getOperand(1).getOpcode() == ISD::Constant)
1699 {
1700 uint64_t k = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1701 unsigned int build = 0;
1702 for(int i = 0; i < 8; ++i)
1703 {
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001704 if ((k & 0x00FF) == 0x00FF)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001705 build |= 1 << i;
Andrew Lenharth3ae18292005-04-14 16:24:00 +00001706 else if ((k & 0x00FF) != 0)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001707 { build = 0; break; }
1708 k >>= 8;
1709 }
1710 if (build)
1711 {
1712 Tmp1 = SelectExpr(N.getOperand(0));
1713 BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1714 return Result;
1715 }
1716 }
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001717 case ISD::OR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001718 //Check operand(0) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001719 if (N.getOperand(0).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001720 N.getOperand(0).getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001721 cast<ConstantSDNode>(N.getOperand(0).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001722 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001723 switch(opcode) {
1724 case ISD::AND: Opc = Alpha::BIC; break;
1725 case ISD::OR: Opc = Alpha::ORNOT; break;
1726 case ISD::XOR: Opc = Alpha::EQV; break;
1727 }
1728 Tmp1 = SelectExpr(N.getOperand(1));
1729 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1730 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1731 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001732 }
1733 //Check operand(1) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001734 if (N.getOperand(1).getOpcode() == ISD::XOR &&
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001735 N.getOperand(1).getOperand(1).getOpcode() == ISD::Constant &&
Misha Brukman7847fca2005-04-22 17:54:37 +00001736 cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getSignExtended() == -1)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001737 {
Misha Brukman7847fca2005-04-22 17:54:37 +00001738 switch(opcode) {
1739 case ISD::AND: Opc = Alpha::BIC; break;
1740 case ISD::OR: Opc = Alpha::ORNOT; break;
1741 case ISD::XOR: Opc = Alpha::EQV; break;
1742 }
1743 Tmp1 = SelectExpr(N.getOperand(0));
1744 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1745 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1746 return Result;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001747 }
1748 //Fall through
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001749 case ISD::SHL:
1750 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001751 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001752 case ISD::MUL:
Andrew Lenharth40831c52005-01-28 06:57:18 +00001753 assert (DestType == MVT::i64 && "Only do arithmetic on i64s!");
1754 if(N.getOperand(1).getOpcode() == ISD::Constant &&
Andrew Lenharth40831c52005-01-28 06:57:18 +00001755 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001756 {
1757 switch(opcode) {
1758 case ISD::AND: Opc = Alpha::ANDi; break;
1759 case ISD::OR: Opc = Alpha::BISi; break;
1760 case ISD::XOR: Opc = Alpha::XORi; break;
1761 case ISD::SHL: Opc = Alpha::SLi; break;
1762 case ISD::SRL: Opc = Alpha::SRLi; break;
1763 case ISD::SRA: Opc = Alpha::SRAi; break;
1764 case ISD::MUL: Opc = Alpha::MULQi; break;
1765 };
1766 Tmp1 = SelectExpr(N.getOperand(0));
1767 Tmp2 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
1768 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(Tmp2);
1769 } else {
1770 switch(opcode) {
1771 case ISD::AND: Opc = Alpha::AND; break;
1772 case ISD::OR: Opc = Alpha::BIS; break;
1773 case ISD::XOR: Opc = Alpha::XOR; break;
1774 case ISD::SHL: Opc = Alpha::SL; break;
1775 case ISD::SRL: Opc = Alpha::SRL; break;
1776 case ISD::SRA: Opc = Alpha::SRA; break;
1777 case ISD::MUL: Opc = Alpha::MULQ; break;
1778 };
1779 Tmp1 = SelectExpr(N.getOperand(0));
1780 Tmp2 = SelectExpr(N.getOperand(1));
1781 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1782 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001783 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001784
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001785 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001786 case ISD::SUB:
Andrew Lenharth2f8fb772005-01-25 00:35:34 +00001787 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001788 bool isAdd = opcode == ISD::ADD;
1789
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001790 //first check for Scaled Adds and Subs!
1791 //Valid for add and sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001792 ConstantSDNode* CSD = NULL;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001793 if(N.getOperand(0).getOpcode() == ISD::SHL &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001794 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) &&
1795 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001796 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001797 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001798 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001799 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) && CSD->getValue() <= 255)
1800 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1801 2, Result).addReg(Tmp2).addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001802 else {
1803 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001804 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1805 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001806 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001807 }
1808 //Position prevents subs
Andrew Lenharth273a1f92005-04-07 14:18:13 +00001809 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001810 (CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) &&
1811 (CSD->getValue() == 2 || CSD->getValue() == 3))
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001812 {
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001813 bool use4 = CSD->getValue() == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001814 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001815 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(0))) && CSD->getValue() <= 255)
1816 BuildMI(BB, use4?Alpha::S4ADDQi:Alpha::S8ADDQi, 2, Result).addReg(Tmp2)
1817 .addImm(CSD->getValue());
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001818 else {
1819 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001820 BuildMI(BB, use4?Alpha::S4ADDQ:Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001821 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001822 }
1823 //small addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001824 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
1825 CSD->getValue() <= 255)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001826 { //Normal imm add/sub
1827 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1828 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001829 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(CSD->getValue());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001830 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001831 //larger addi
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001832 else if((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1))) &&
1833 CSD->getSignExtended() <= 32767 &&
1834 CSD->getSignExtended() >= -32767)
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001835 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001836 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001837 Tmp2 = (long)CSD->getSignExtended();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001838 if (!isAdd)
1839 Tmp2 = -Tmp2;
1840 BuildMI(BB, Alpha::LDA, 2, Result).addImm(Tmp2).addReg(Tmp1);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001841 }
1842 //give up and do the operation
1843 else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001844 //Normal add/sub
1845 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1846 Tmp1 = SelectExpr(N.getOperand(0));
1847 Tmp2 = SelectExpr(N.getOperand(1));
1848 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1849 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001850 return Result;
1851 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001852
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001853 case ISD::SDIV:
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001854 {
Andrew Lenhartha565c272005-04-06 22:03:13 +00001855 ConstantSDNode* CSD;
1856 //check if we can convert into a shift!
1857 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
1858 (int64_t)CSD->getSignExtended() != 0 &&
1859 ExactLog2(abs((int64_t)CSD->getSignExtended())) != 0)
1860 {
1861 unsigned k = ExactLog2(abs(CSD->getSignExtended()));
1862 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenhartha565c272005-04-06 22:03:13 +00001863 if (k == 1)
1864 Tmp2 = Tmp1;
1865 else
1866 {
1867 Tmp2 = MakeReg(MVT::i64);
1868 BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
1869 }
1870 Tmp3 = MakeReg(MVT::i64);
1871 BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
1872 unsigned Tmp4 = MakeReg(MVT::i64);
1873 BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
1874 if ((int64_t)CSD->getSignExtended() > 0)
1875 BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
1876 else
1877 {
1878 unsigned Tmp5 = MakeReg(MVT::i64);
1879 BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
1880 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
1881 }
1882 return Result;
1883 }
1884 }
1885 //Else fall through
1886
1887 case ISD::UDIV:
1888 {
1889 ConstantSDNode* CSD;
1890 if ((CSD = dyn_cast<ConstantSDNode>(N.getOperand(1).Val)) &&
1891 ((int64_t)CSD->getSignExtended() >= 2 ||
1892 (int64_t)CSD->getSignExtended() <= -2))
1893 {
1894 // If this is a divide by constant, we can emit code using some magic
1895 // constants to implement it as a multiply instead.
1896 ExprMap.erase(N);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001897 if (opcode == ISD::SDIV)
Andrew Lenhartha565c272005-04-06 22:03:13 +00001898 return SelectExpr(BuildSDIVSequence(N));
1899 else
1900 return SelectExpr(BuildUDIVSequence(N));
1901 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001902 }
1903 //else fall though
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001904 case ISD::UREM:
Andrew Lenharth02981182005-01-26 01:24:38 +00001905 case ISD::SREM:
Misha Brukman4633f1c2005-04-21 23:13:11 +00001906 //FIXME: alpha really doesn't support any of these operations,
Andrew Lenharth40831c52005-01-28 06:57:18 +00001907 // the ops are expanded into special library calls with
1908 // special calling conventions
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001909 //Restore GP because it is a call after all...
Andrew Lenharth40831c52005-01-28 06:57:18 +00001910 switch(opcode) {
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001911 case ISD::UREM: Opc = Alpha::REMQU; break;
1912 case ISD::SREM: Opc = Alpha::REMQ; break;
1913 case ISD::UDIV: Opc = Alpha::DIVQU; break;
1914 case ISD::SDIV: Opc = Alpha::DIVQ; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001915 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001916 Tmp1 = SelectExpr(N.getOperand(0));
1917 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth33819132005-03-04 20:09:23 +00001918 //set up regs explicitly (helps Reg alloc)
1919 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001920 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +00001921 AlphaLowering.restoreGP(BB);
Andrew Lenharth33819132005-03-04 20:09:23 +00001922 BuildMI(BB, Opc, 2).addReg(Alpha::R24).addReg(Alpha::R25);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001923 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001924 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001925
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001926 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001927 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001928 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001929 assert (DestType == MVT::i64 && "only quads can be loaded to");
1930 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00001931 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001932 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001933 if (SrcType == MVT::f32)
Misha Brukman7847fca2005-04-22 17:54:37 +00001934 {
1935 Tmp2 = MakeReg(MVT::f64);
1936 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
1937 Tmp1 = Tmp2;
1938 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001939 Tmp2 = MakeReg(MVT::f64);
1940 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001941 MoveFP2Int(Tmp2, Result, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001942
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001943 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001944 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001945
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001946 case ISD::SELECT:
1947 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001948 //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 +00001949 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001950 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1951 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001952 // Get the condition into the zero flag.
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001953 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001954
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001955 SDOperand CC = N.getOperand(0);
1956 SetCCSDNode* SetCC = dyn_cast<SetCCSDNode>(CC.Val);
1957
Misha Brukman4633f1c2005-04-21 23:13:11 +00001958 if (CC.getOpcode() == ISD::SETCC &&
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001959 !MVT::isInteger(SetCC->getOperand(0).getValueType()))
1960 { //FP Setcc -> Int Select
Misha Brukman7847fca2005-04-22 17:54:37 +00001961 Tmp1 = MakeReg(MVT::f64);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001962 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1963 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Misha Brukman7847fca2005-04-22 17:54:37 +00001964 bool inv = SelectFPSetCC(CC, Tmp1);
1965 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
1966 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
1967 return Result;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001968 }
1969 if (CC.getOpcode() == ISD::SETCC) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001970 //Int SetCC -> Select
1971 //Dropping the CC is only useful if we are comparing to 0
1972 if((SetCC->getOperand(1).getOpcode() == ISD::Constant &&
1973 cast<ConstantSDNode>(SetCC->getOperand(1))->getValue() == 0) ||
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001974 (SetCC->getOperand(0).getOpcode() == ISD::Constant &&
1975 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0))
1976 {
1977 //figure out a few things
1978 bool LeftZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
1979 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
1980 bool RightZero = SetCC->getOperand(0).getOpcode() == ISD::Constant &&
1981 cast<ConstantSDNode>(SetCC->getOperand(0))->getValue() == 0;
1982 bool LeftConst = N.getOperand(1).getOpcode() == ISD::Constant &&
1983 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 255;
1984 bool RightConst = N.getOperand(2).getOpcode() == ISD::Constant &&
1985 cast<ConstantSDNode>(N.getOperand(2))->getValue() <= 255;
1986 bool useImm = LeftConst || RightConst;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001987
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001988 //Fix up CC
1989 ISD::CondCode cCode= SetCC->getCondition();
1990 if (RightConst && !LeftConst) //Invert sense to get Imm field right
1991 cCode = ISD::getSetCCInverse(cCode, true);
1992 if (LeftZero && !RightZero) //Swap Operands
1993 cCode = ISD::getSetCCSwappedOperands(cCode);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001994
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001995 //Choose the CMOV
1996 switch (cCode) {
1997 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
1998 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
1999 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
2000 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
2001 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
2002 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
2003 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
2004 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2005 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break; //Technically you could have this CC
2006 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
2007 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
2008 }
2009 if(LeftZero && !RightZero) //swap Operands
2010 Tmp1 = SelectExpr(SetCC->getOperand(1)); //Cond
2011 else
2012 Tmp1 = SelectExpr(SetCC->getOperand(0)); //Cond
2013
2014 if (LeftConst) {
2015 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2016 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
Misha Brukman7847fca2005-04-22 17:54:37 +00002017 .addImm(cast<ConstantSDNode>(N.getOperand(1))->getValue())
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002018 .addReg(Tmp1);
2019 } else if (RightConst) {
2020 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
2021 BuildMI(BB, Opc, 2, Result).addReg(Tmp3)
Misha Brukman7847fca2005-04-22 17:54:37 +00002022 .addImm(cast<ConstantSDNode>(N.getOperand(2))->getValue())
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002023 .addReg(Tmp1);
2024 } else {
2025 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2026 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
2027 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
2028 }
2029 return Result;
2030 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002031 //Otherwise, fall though
Andrew Lenharth10c085b2005-04-02 22:32:39 +00002032 }
2033 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00002034 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
2035 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthe76797c2005-02-01 20:40:27 +00002036 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00002037
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002038 return Result;
2039 }
2040
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002041 case ISD::Constant:
2042 {
Andrew Lenharthc0513832005-03-29 19:24:04 +00002043 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002044 if (val <= IMM_HIGH && val >= IMM_LOW) {
Misha Brukman7847fca2005-04-22 17:54:37 +00002045 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002046 }
Misha Brukman7847fca2005-04-22 17:54:37 +00002047 else if (val <= (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT &&
2048 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
2049 Tmp1 = MakeReg(MVT::i64);
2050 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val)).addReg(Alpha::R31);
2051 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00002052 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002053 else {
2054 MachineConstantPool *CP = BB->getParent()->getConstantPool();
2055 ConstantUInt *C = ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
2056 unsigned CPI = CP->getConstantPoolIndex(C);
2057 AlphaLowering.restoreGP(BB);
2058 BuildMI(BB, Alpha::LDQ_SYM, 1, Result).addConstantPoolIndex(CPI);
2059 }
2060 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002061 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002062 }
2063
2064 return 0;
2065}
2066
2067void ISel::Select(SDOperand N) {
2068 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00002069 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002070
Nate Begeman85fdeb22005-03-24 04:39:54 +00002071 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002072 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002073
2074 SDNode *Node = N.Val;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002075
Andrew Lenharth760270d2005-02-07 23:02:23 +00002076 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002077
2078 default:
2079 Node->dump(); std::cerr << "\n";
2080 assert(0 && "Node not handled yet!");
2081
2082 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00002083 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002084 return;
2085 }
2086
2087 case ISD::BR: {
2088 MachineBasicBlock *Dest =
2089 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
2090
2091 Select(N.getOperand(0));
2092 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
2093 return;
2094 }
2095
2096 case ISD::ImplicitDef:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002097 ++count_ins;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002098 Select(N.getOperand(0));
2099 BuildMI(BB, Alpha::IDEF, 0, cast<RegSDNode>(N)->getReg());
2100 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +00002101
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002102 case ISD::EntryToken: return; // Noop
2103
2104 case ISD::TokenFactor:
2105 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
2106 Select(Node->getOperand(i));
Misha Brukman4633f1c2005-04-21 23:13:11 +00002107
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002108 //N.Val->dump(); std::cerr << "\n";
2109 //assert(0 && "Node not handled yet!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00002110
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002111 return;
2112
2113 case ISD::CopyToReg:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002114 ++count_outs;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002115 Select(N.getOperand(0));
2116 Tmp1 = SelectExpr(N.getOperand(1));
2117 Tmp2 = cast<RegSDNode>(N)->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002118
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002119 if (Tmp1 != Tmp2) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002120 if (N.getOperand(1).getValueType() == MVT::f64 ||
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002121 N.getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00002122 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
2123 else
2124 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002125 }
2126 return;
2127
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002128 case ISD::RET:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002129 ++count_outs;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002130 switch (N.getNumOperands()) {
2131 default:
2132 std::cerr << N.getNumOperands() << "\n";
2133 for (unsigned i = 0; i < N.getNumOperands(); ++i)
2134 std::cerr << N.getOperand(i).getValueType() << "\n";
2135 Node->dump();
2136 assert(0 && "Unknown return instruction!");
2137 case 2:
2138 Select(N.getOperand(0));
2139 Tmp1 = SelectExpr(N.getOperand(1));
2140 switch (N.getOperand(1).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002141 default: Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002142 assert(0 && "All other types should have been promoted!!");
2143 case MVT::f64:
2144 case MVT::f32:
2145 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
2146 break;
2147 case MVT::i32:
2148 case MVT::i64:
2149 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
2150 break;
2151 }
2152 break;
2153 case 1:
2154 Select(N.getOperand(0));
2155 break;
2156 }
2157 //Tmp2 = AlphaLowering.getRetAddr();
2158 //BuildMI(BB, Alpha::BIS, 2, Alpha::R26).addReg(Tmp2).addReg(Tmp2);
2159 BuildMI(BB, Alpha::RETURN, 0); // Just emit a 'ret' instruction
2160 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002161
Misha Brukman4633f1c2005-04-21 23:13:11 +00002162 case ISD::TRUNCSTORE:
2163 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002164 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002165 SDOperand Chain = N.getOperand(0);
2166 SDOperand Value = N.getOperand(1);
2167 SDOperand Address = N.getOperand(2);
2168 Select(Chain);
2169
2170 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00002171
2172 if (opcode == ISD::STORE) {
2173 switch(Value.getValueType()) {
2174 default: assert(0 && "unknown Type in store");
2175 case MVT::i64: Opc = Alpha::STQ; break;
2176 case MVT::f64: Opc = Alpha::STT; break;
2177 case MVT::f32: Opc = Alpha::STS; break;
2178 }
2179 } else { //ISD::TRUNCSTORE
2180 switch(cast<MVTSDNode>(Node)->getExtraValueType()) {
2181 default: assert(0 && "unknown Type in store");
2182 case MVT::i1: //FIXME: DAG does not promote this load
2183 case MVT::i8: Opc = Alpha::STB; break;
2184 case MVT::i16: Opc = Alpha::STW; break;
2185 case MVT::i32: Opc = Alpha::STL; break;
2186 }
Andrew Lenharth65838902005-02-06 16:22:15 +00002187 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00002188
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00002189 if (Address.getOpcode() == ISD::GlobalAddress)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002190 {
2191 AlphaLowering.restoreGP(BB);
2192 Opc = GetSymVersion(Opc);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00002193 has_sym = true;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002194 BuildMI(BB, Opc, 2).addReg(Tmp1).addGlobalAddress(cast<GlobalAddressSDNode>(Address)->getGlobal());
2195 }
Andrew Lenharth05380342005-02-07 05:07:00 +00002196 else if(Address.getOpcode() == ISD::FrameIndex)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002197 {
Andrew Lenharth032f2352005-02-22 21:59:48 +00002198 BuildMI(BB, Opc, 3).addReg(Tmp1)
2199 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
2200 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002201 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002202 else
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00002203 {
2204 long offset;
2205 SelectAddr(Address, Tmp2, offset);
2206 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
2207 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00002208 return;
2209 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002210
2211 case ISD::EXTLOAD:
2212 case ISD::SEXTLOAD:
2213 case ISD::ZEXTLOAD:
2214 case ISD::LOAD:
2215 case ISD::CopyFromReg:
2216 case ISD::CALL:
Andrew Lenharth032f2352005-02-22 21:59:48 +00002217 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00002218 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002219 SelectExpr(N);
2220 return;
2221
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002222 case ISD::ADJCALLSTACKDOWN:
2223 case ISD::ADJCALLSTACKUP:
2224 Select(N.getOperand(0));
2225 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00002226
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002227 Opc = N.getOpcode() == ISD::ADJCALLSTACKDOWN ? Alpha::ADJUSTSTACKDOWN :
2228 Alpha::ADJUSTSTACKUP;
2229 BuildMI(BB, Opc, 1).addImm(Tmp1);
2230 return;
Andrew Lenharth95762122005-03-31 21:24:06 +00002231
2232 case ISD::PCMARKER:
2233 Select(N.getOperand(0)); //Chain
2234 BuildMI(BB, Alpha::PCLABEL, 2).addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
2235 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002236 }
2237 assert(0 && "Should not be reached!");
2238}
2239
2240
2241/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
2242/// into a machine code representation using pattern matching and a machine
2243/// description file.
2244///
2245FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00002246 return new ISel(TM);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00002247}
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00002248