blob: 56db7f126dcea6ba6c3c702e2b9abc8669f8c5d4 [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"
Andrew Lenharth120ab482005-09-29 22:54:56 +000016#include "AlphaTargetMachine.h"
Andrew Lenharthaa38ce42005-09-02 18:46:02 +000017#include "AlphaISelLowering.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000018#include "llvm/Constants.h" // FIXME: REMOVE
19#include "llvm/Function.h"
Andrew Lenharthb69f3422005-06-22 17:19:45 +000020#include "llvm/Module.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000021#include "llvm/CodeGen/MachineInstrBuilder.h"
22#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
23#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/SelectionDAG.h"
26#include "llvm/CodeGen/SelectionDAGISel.h"
27#include "llvm/CodeGen/SSARegMap.h"
28#include "llvm/Target/TargetData.h"
29#include "llvm/Target/TargetLowering.h"
30#include "llvm/Support/MathExtras.h"
31#include "llvm/ADT/Statistic.h"
Andrew Lenharth032f2352005-02-22 21:59:48 +000032#include "llvm/Support/Debug.h"
Andrew Lenharth95762122005-03-31 21:24:06 +000033#include "llvm/Support/CommandLine.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000034#include <set>
Andrew Lenharth684f2292005-01-30 00:35:27 +000035#include <algorithm>
Andrew Lenharth304d0f32005-01-22 23:41:55 +000036using namespace llvm;
37
Andrew Lenharth95762122005-03-31 21:24:06 +000038namespace llvm {
Misha Brukman4633f1c2005-04-21 23:13:11 +000039 cl::opt<bool> EnableAlphaIDIV("enable-alpha-intfpdiv",
Andrew Lenharthd4653b12005-06-27 17:39:17 +000040 cl::desc("Use the FP div instruction for integer div when possible"),
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",
Andrew Lenharthd4653b12005-06-27 17:39:17 +000043 cl::desc("Print estimates on live ins and outs"),
44 cl::Hidden);
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +000045 cl::opt<bool> EnableAlphaLSMark("enable-alpha-lsmark",
Andrew Lenharthd4653b12005-06-27 17:39:17 +000046 cl::desc("Emit symbols to correlate Mem ops to LLVM Values"),
47 cl::Hidden);
Andrew Lenharth95762122005-03-31 21:24:06 +000048}
49
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +000050namespace {
Andrew Lenharth304d0f32005-01-22 23:41:55 +000051
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000052//===--------------------------------------------------------------------===//
53/// ISel - Alpha specific code to select Alpha machine instructions for
54/// SelectionDAG operations.
55//===--------------------------------------------------------------------===//
Andrew Lenharthb69f3422005-06-22 17:19:45 +000056class AlphaISel : public SelectionDAGISel {
Misha Brukman4633f1c2005-04-21 23:13:11 +000057
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000058 /// AlphaLowering - This object fully describes how to lower LLVM code to an
59 /// Alpha-specific SelectionDAG.
60 AlphaTargetLowering AlphaLowering;
Misha Brukman4633f1c2005-04-21 23:13:11 +000061
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000062 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
63 // for sdiv and udiv until it is put into the future
64 // dag combiner.
65
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000066 /// ExprMap - As shared expressions are codegen'd, we keep track of which
67 /// vreg the value is produced in, so we only emit one copy of each compiled
68 /// tree.
69 static const unsigned notIn = (unsigned)(-1);
70 std::map<SDOperand, unsigned> ExprMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +000071
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000072 //CCInvMap sometimes (SetNE) we have the inverse CC code for free
73 std::map<SDOperand, unsigned> CCInvMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +000074
Andrew Lenhartha32b9e32005-04-08 17:28:49 +000075 int count_ins;
76 int count_outs;
77 bool has_sym;
Andrew Lenharth500b4db2005-04-22 13:35:18 +000078 int max_depth;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +000079
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000080public:
Jeff Cohen00b168892005-07-27 06:12:32 +000081 AlphaISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering),
Andrew Lenharthd4653b12005-06-27 17:39:17 +000082 AlphaLowering(TM)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000083 {}
Misha Brukman4633f1c2005-04-21 23:13:11 +000084
Chris Lattnerf519fe02005-10-29 16:45:02 +000085 virtual const char *getPassName() const {
86 return "Alpha Pattern Instruction Selection";
87 }
88
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000089 /// InstructionSelectBasicBlock - This callback is invoked by
90 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
91 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
Andrew Lenharth032f2352005-02-22 21:59:48 +000092 DEBUG(BB->dump());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +000093 count_ins = 0;
94 count_outs = 0;
Andrew Lenharth500b4db2005-04-22 13:35:18 +000095 max_depth = 0;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +000096 has_sym = false;
97
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000098 // Codegen the basic block.
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000099 ISelDAG = &DAG;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000100 max_depth = DAG.getRoot().getNodeDepth();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000101 Select(DAG.getRoot());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000102
103 if(has_sym)
104 ++count_ins;
105 if(EnableAlphaCount)
Jeff Cohen00b168892005-07-27 06:12:32 +0000106 std::cerr << "COUNT: "
107 << BB->getParent()->getFunction ()->getName() << " "
108 << BB->getNumber() << " "
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000109 << max_depth << " "
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000110 << count_ins << " "
111 << count_outs << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000112
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000113 // Clear state used for selection.
114 ExprMap.clear();
115 CCInvMap.clear();
116 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000117
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000118 unsigned SelectExpr(SDOperand N);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000119 void Select(SDOperand N);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000120
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000121 void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
122 void SelectBranchCC(SDOperand N);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000123 void MoveFP2Int(unsigned src, unsigned dst, bool isDouble);
124 void MoveInt2FP(unsigned src, unsigned dst, bool isDouble);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000125 //returns whether the sense of the comparison was inverted
126 bool SelectFPSetCC(SDOperand N, unsigned dst);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000127
128 // dag -> dag expanders for integer divide by constant
129 SDOperand BuildSDIVSequence(SDOperand N);
130 SDOperand BuildUDIVSequence(SDOperand N);
131
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000132};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000133}
134
Andrew Lenharthd2284272005-08-15 14:31:37 +0000135static bool isSIntImmediate(SDOperand N, int64_t& Imm) {
136 // test for constant
137 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
138 // retrieve value
139 Imm = CN->getSignExtended();
140 // passes muster
141 return true;
142 }
143 // not a constant
144 return false;
145}
146
147// isSIntImmediateBounded - This method tests to see if a constant operand
148// bounded s.t. low <= Imm <= high
149// If so Imm will receive the 64 bit value.
150static bool isSIntImmediateBounded(SDOperand N, int64_t& Imm,
151 int64_t low, int64_t high) {
Andrew Lenharth035b8ab2005-08-17 00:47:24 +0000152 if (isSIntImmediate(N, Imm) && Imm <= high && Imm >= low)
Andrew Lenharthd2284272005-08-15 14:31:37 +0000153 return true;
154 return false;
155}
156static bool isUIntImmediate(SDOperand N, uint64_t& Imm) {
157 // test for constant
158 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
159 // retrieve value
160 Imm = (uint64_t)CN->getValue();
161 // passes muster
162 return true;
163 }
164 // not a constant
165 return false;
166}
167
168static bool isUIntImmediateBounded(SDOperand N, uint64_t& Imm,
169 uint64_t low, uint64_t high) {
Andrew Lenharth035b8ab2005-08-17 00:47:24 +0000170 if (isUIntImmediate(N, Imm) && Imm <= high && Imm >= low)
Andrew Lenharthd2284272005-08-15 14:31:37 +0000171 return true;
172 return false;
173}
174
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000175static void getValueInfo(const Value* v, int& type, int& fun, int& offset)
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000176{
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000177 fun = type = offset = 0;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000178 if (v == NULL) {
179 type = 0;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000180 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(v)) {
181 type = 1;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000182 const Module* M = GV->getParent();
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000183 for(Module::const_global_iterator ii = M->global_begin(); &*ii != GV; ++ii)
184 ++offset;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000185 } else if (const Argument* Arg = dyn_cast<Argument>(v)) {
186 type = 2;
187 const Function* F = Arg->getParent();
188 const Module* M = F->getParent();
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000189 for(Module::const_iterator ii = M->begin(); &*ii != F; ++ii)
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000190 ++fun;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000191 for(Function::const_arg_iterator ii = F->arg_begin(); &*ii != Arg; ++ii)
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000192 ++offset;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000193 } else if (const Instruction* I = dyn_cast<Instruction>(v)) {
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +0000194 assert(dyn_cast<PointerType>(I->getType()));
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000195 type = 3;
196 const BasicBlock* bb = I->getParent();
197 const Function* F = bb->getParent();
198 const Module* M = F->getParent();
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000199 for(Module::const_iterator ii = M->begin(); &*ii != F; ++ii)
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000200 ++fun;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000201 for(Function::const_iterator ii = F->begin(); &*ii != bb; ++ii)
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000202 offset += ii->size();
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000203 for(BasicBlock::const_iterator ii = bb->begin(); &*ii != I; ++ii)
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000204 ++offset;
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +0000205 } else if (const Constant* C = dyn_cast<Constant>(v)) {
206 //Don't know how to look these up yet
207 type = 0;
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +0000208 } else {
209 assert(0 && "Error in value marking");
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000210 }
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000211 //type = 4: register spilling
212 //type = 5: global address loading or constant loading
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000213}
214
215static int getUID()
216{
217 static int id = 0;
218 return ++id;
219}
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000220
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000221//Factorize a number using the list of constants
222static bool factorize(int v[], int res[], int size, uint64_t c)
223{
224 bool cont = true;
225 while (c != 1 && cont)
226 {
227 cont = false;
228 for(int i = 0; i < size; ++i)
229 {
230 if (c % v[i] == 0)
231 {
232 c /= v[i];
233 ++res[i];
234 cont=true;
235 }
236 }
237 }
238 return c == 1;
239}
240
241
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000242//These describe LDAx
Andrew Lenharthc0513832005-03-29 19:24:04 +0000243static const int IMM_LOW = -32768;
244static const int IMM_HIGH = 32767;
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000245static const int IMM_MULT = 65536;
246
247static long getUpper16(long l)
248{
249 long y = l / IMM_MULT;
250 if (l % IMM_MULT > IMM_HIGH)
251 ++y;
252 return y;
253}
254
255static long getLower16(long l)
256{
257 long h = getUpper16(l);
258 return l - h * IMM_MULT;
259}
260
Andrew Lenharthfe895e32005-06-27 17:15:36 +0000261static unsigned GetRelVersion(unsigned opcode)
262{
263 switch (opcode) {
264 default: assert(0 && "unknown load or store"); return 0;
265 case Alpha::LDQ: return Alpha::LDQr;
266 case Alpha::LDS: return Alpha::LDSr;
267 case Alpha::LDT: return Alpha::LDTr;
268 case Alpha::LDL: return Alpha::LDLr;
269 case Alpha::LDBU: return Alpha::LDBUr;
270 case Alpha::LDWU: return Alpha::LDWUr;
Andrew Lenharthfce587e2005-06-29 00:39:17 +0000271 case Alpha::STB: return Alpha::STBr;
272 case Alpha::STW: return Alpha::STWr;
273 case Alpha::STL: return Alpha::STLr;
274 case Alpha::STQ: return Alpha::STQr;
275 case Alpha::STS: return Alpha::STSr;
276 case Alpha::STT: return Alpha::STTr;
277
Andrew Lenharthfe895e32005-06-27 17:15:36 +0000278 }
279}
Andrew Lenharth65838902005-02-06 16:22:15 +0000280
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000281void AlphaISel::MoveFP2Int(unsigned src, unsigned dst, bool isDouble)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000282{
283 unsigned Opc;
Andrew Lenharth120ab482005-09-29 22:54:56 +0000284 if (TLI.getTargetMachine().getSubtarget<AlphaSubtarget>().hasF2I()) {
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000285 Opc = isDouble ? Alpha::FTOIT : Alpha::FTOIS;
Andrew Lenharth98169be2005-07-28 18:14:47 +0000286 BuildMI(BB, Opc, 1, dst).addReg(src).addReg(Alpha::F31);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000287 } else {
288 //The hard way:
289 // Spill the integer to memory and reload it from there.
290 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
291 MachineFunction *F = BB->getParent();
292 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
293
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000294 if (EnableAlphaLSMark)
295 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
296 .addImm(getUID());
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000297 Opc = isDouble ? Alpha::STT : Alpha::STS;
298 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000299
300 if (EnableAlphaLSMark)
301 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
302 .addImm(getUID());
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000303 Opc = isDouble ? Alpha::LDQ : Alpha::LDL;
304 BuildMI(BB, Alpha::LDQ, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
305 }
306}
307
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000308void AlphaISel::MoveInt2FP(unsigned src, unsigned dst, bool isDouble)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000309{
310 unsigned Opc;
Andrew Lenharth120ab482005-09-29 22:54:56 +0000311 if (TLI.getTargetMachine().getSubtarget<AlphaSubtarget>().hasF2I()) {
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000312 Opc = isDouble?Alpha::ITOFT:Alpha::ITOFS;
Andrew Lenharth98169be2005-07-28 18:14:47 +0000313 BuildMI(BB, Opc, 1, dst).addReg(src).addReg(Alpha::R31);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000314 } else {
315 //The hard way:
316 // Spill the integer to memory and reload it from there.
317 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
318 MachineFunction *F = BB->getParent();
319 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
320
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000321 if (EnableAlphaLSMark)
322 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
323 .addImm(getUID());
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000324 Opc = isDouble ? Alpha::STQ : Alpha::STL;
325 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000326
327 if (EnableAlphaLSMark)
328 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
329 .addImm(getUID());
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000330 Opc = isDouble ? Alpha::LDT : Alpha::LDS;
331 BuildMI(BB, Opc, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
332 }
333}
334
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000335bool AlphaISel::SelectFPSetCC(SDOperand N, unsigned dst)
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000336{
Chris Lattner88ac32c2005-08-09 20:21:10 +0000337 SDNode *SetCC = N.Val;
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000338 unsigned Opc, Tmp1, Tmp2, Tmp3;
Chris Lattner88ac32c2005-08-09 20:21:10 +0000339 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC->getOperand(2))->get();
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000340 bool rev = false;
341 bool inv = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000342
Chris Lattner88ac32c2005-08-09 20:21:10 +0000343 switch (CC) {
344 default: SetCC->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000345 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
346 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
347 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
348 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
349 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
350 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
351 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000352
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000353 ConstantFPSDNode *CN;
354 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
355 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
356 Tmp1 = Alpha::F31;
357 else
358 Tmp1 = SelectExpr(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000359
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000360 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
361 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
362 Tmp2 = Alpha::F31;
363 else
Chris Lattner9c9183a2005-04-30 04:44:07 +0000364 Tmp2 = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000365
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000366 //Can only compare doubles, and dag won't promote for me
367 if (SetCC->getOperand(0).getValueType() == MVT::f32)
368 {
369 //assert(0 && "Setcc On float?\n");
370 std::cerr << "Setcc on float!\n";
371 Tmp3 = MakeReg(MVT::f64);
Andrew Lenharth98169be2005-07-28 18:14:47 +0000372 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000373 Tmp1 = Tmp3;
374 }
375 if (SetCC->getOperand(1).getValueType() == MVT::f32)
376 {
377 //assert (0 && "Setcc On float?\n");
378 std::cerr << "Setcc on float!\n";
379 Tmp3 = MakeReg(MVT::f64);
Andrew Lenharth98169be2005-07-28 18:14:47 +0000380 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Alpha::F31).addReg(Tmp2);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000381 Tmp2 = Tmp3;
382 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000383
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000384 if (rev) std::swap(Tmp1, Tmp2);
385 //do the comparison
386 BuildMI(BB, Opc, 2, dst).addReg(Tmp1).addReg(Tmp2);
387 return inv;
388}
389
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000390//Check to see if the load is a constant offset from a base register
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000391void AlphaISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000392{
393 unsigned opcode = N.getOpcode();
Andrew Lenharth694c2982005-06-26 23:01:11 +0000394 if (opcode == ISD::ADD && N.getOperand(1).getOpcode() == ISD::Constant &&
395 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
396 { //Normal imm add
397 Reg = SelectExpr(N.getOperand(0));
398 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
399 return;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000400 }
401 Reg = SelectExpr(N);
402 offset = 0;
403 return;
404}
405
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000406void AlphaISel::SelectBranchCC(SDOperand N)
Andrew Lenharth445171a2005-02-08 00:40:03 +0000407{
408 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000409 MachineBasicBlock *Dest =
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000410 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
411 unsigned Opc = Alpha::WTF;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000412
Andrew Lenharth445171a2005-02-08 00:40:03 +0000413 Select(N.getOperand(0)); //chain
414 SDOperand CC = N.getOperand(1);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000415
Andrew Lenharth445171a2005-02-08 00:40:03 +0000416 if (CC.getOpcode() == ISD::SETCC)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000417 {
Chris Lattner88ac32c2005-08-09 20:21:10 +0000418 ISD::CondCode cCode= cast<CondCodeSDNode>(CC.getOperand(2))->get();
419 if (MVT::isInteger(CC.getOperand(0).getValueType())) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000420 //Dropping the CC is only useful if we are comparing to 0
Chris Lattner88ac32c2005-08-09 20:21:10 +0000421 bool RightZero = CC.getOperand(1).getOpcode() == ISD::Constant &&
422 cast<ConstantSDNode>(CC.getOperand(1))->getValue() == 0;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000423 bool isNE = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000424
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000425 //Fix up CC
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000426 if(cCode == ISD::SETNE)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000427 isNE = true;
Andrew Lenharth445171a2005-02-08 00:40:03 +0000428
Andrew Lenharth694c2982005-06-26 23:01:11 +0000429 if (RightZero) {
Andrew Lenharth09552bf2005-06-08 18:02:21 +0000430 switch (cCode) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000431 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
432 case ISD::SETEQ: Opc = Alpha::BEQ; break;
433 case ISD::SETLT: Opc = Alpha::BLT; break;
434 case ISD::SETLE: Opc = Alpha::BLE; break;
435 case ISD::SETGT: Opc = Alpha::BGT; break;
436 case ISD::SETGE: Opc = Alpha::BGE; break;
437 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
438 case ISD::SETUGT: Opc = Alpha::BNE; break;
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000439 //Technically you could have this CC
440 case ISD::SETULE: Opc = Alpha::BEQ; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000441 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
442 case ISD::SETNE: Opc = Alpha::BNE; break;
443 }
Chris Lattner88ac32c2005-08-09 20:21:10 +0000444 unsigned Tmp1 = SelectExpr(CC.getOperand(0)); //Cond
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000445 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
446 return;
447 } else {
448 unsigned Tmp1 = SelectExpr(CC);
449 if (isNE)
450 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
451 else
452 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000453 return;
454 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000455 } else { //FP
Jeff Cohen00b168892005-07-27 06:12:32 +0000456 //Any comparison between 2 values should be codegened as an folded
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000457 //branch, as moving CC to the integer register is very expensive
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000458 //for a cmp b: c = a - b;
459 //a = b: c = 0
460 //a < b: c < 0
461 //a > b: c > 0
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000462
463 bool invTest = false;
464 unsigned Tmp3;
465
466 ConstantFPSDNode *CN;
Chris Lattner88ac32c2005-08-09 20:21:10 +0000467 if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(1)))
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000468 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
Chris Lattner88ac32c2005-08-09 20:21:10 +0000469 Tmp3 = SelectExpr(CC.getOperand(0));
470 else if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(0)))
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000471 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
472 {
Chris Lattner88ac32c2005-08-09 20:21:10 +0000473 Tmp3 = SelectExpr(CC.getOperand(1));
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000474 invTest = true;
475 }
476 else
477 {
Chris Lattner88ac32c2005-08-09 20:21:10 +0000478 unsigned Tmp1 = SelectExpr(CC.getOperand(0));
479 unsigned Tmp2 = SelectExpr(CC.getOperand(1));
480 bool isD = CC.getOperand(0).getValueType() == MVT::f64;
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000481 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
482 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
483 .addReg(Tmp1).addReg(Tmp2);
484 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000485
Chris Lattner88ac32c2005-08-09 20:21:10 +0000486 switch (cCode) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000487 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000488 case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
489 case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
490 case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
491 case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
492 case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
493 case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000494 }
495 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000496 return;
497 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000498 abort(); //Should never be reached
499 } else {
500 //Giveup and do the stupid thing
501 unsigned Tmp1 = SelectExpr(CC);
502 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
503 return;
504 }
Andrew Lenharth445171a2005-02-08 00:40:03 +0000505 abort(); //Should never be reached
506}
507
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000508unsigned AlphaISel::SelectExpr(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000509 unsigned Result;
Andrew Lenharth2966e842005-04-07 18:15:28 +0000510 unsigned Tmp1, Tmp2 = 0, Tmp3;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000511 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000512 unsigned opcode = N.getOpcode();
Chris Lattnerd2fc54e2005-10-21 16:01:26 +0000513 int64_t SImm = 0;
Andrew Lenharthd2284272005-08-15 14:31:37 +0000514 uint64_t UImm;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000515
516 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000517 MVT::ValueType DestType = N.getValueType();
Andrew Lenharthf4da9452005-06-29 12:49:51 +0000518 bool isFP = DestType == MVT::f64 || DestType == MVT::f32;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000519
520 unsigned &Reg = ExprMap[N];
521 if (Reg) return Reg;
522
Andrew Lenharth46a776e2005-09-06 17:00:23 +0000523 switch(N.getOpcode()) {
524 default:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000525 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000526 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth46a776e2005-09-06 17:00:23 +0000527 break;
528 case ISD::AssertSext:
529 case ISD::AssertZext:
530 return Reg = SelectExpr(N.getOperand(0));
531 case ISD::CALL:
532 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000533 // If this is a call instruction, make sure to prepare ALL of the result
534 // values as well as the chain.
535 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000536 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000537 else {
538 Result = MakeReg(Node->getValueType(0));
539 ExprMap[N.getValue(0)] = Result;
540 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
541 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000542 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000543 }
Andrew Lenharth46a776e2005-09-06 17:00:23 +0000544 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000545 }
546
Andrew Lenharth40831c52005-01-28 06:57:18 +0000547 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000548 default:
549 Node->dump();
550 assert(0 && "Node not handled!\n");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000551
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000552 case ISD::CTPOP:
553 case ISD::CTTZ:
554 case ISD::CTLZ:
555 Opc = opcode == ISD::CTPOP ? Alpha::CTPOP :
556 (opcode == ISD::CTTZ ? Alpha::CTTZ : Alpha::CTLZ);
557 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth964b6aa2005-10-20 19:39:24 +0000558 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000559 return Result;
560
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000561 case ISD::MULHU:
562 Tmp1 = SelectExpr(N.getOperand(0));
563 Tmp2 = SelectExpr(N.getOperand(1));
564 BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth706be912005-04-07 13:55:53 +0000565 return Result;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000566 case ISD::MULHS:
567 {
568 //MULHU - Ra<63>*Rb - Rb<63>*Ra
569 Tmp1 = SelectExpr(N.getOperand(0));
570 Tmp2 = SelectExpr(N.getOperand(1));
571 Tmp3 = MakeReg(MVT::i64);
572 BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
573 unsigned V1 = MakeReg(MVT::i64);
574 unsigned V2 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000575 BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31)
576 .addReg(Tmp1);
577 BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31)
578 .addReg(Tmp2);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000579 unsigned IRes = MakeReg(MVT::i64);
580 BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
581 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
582 return Result;
583 }
Andrew Lenharth7332f3e2005-04-02 19:11:07 +0000584 case ISD::UNDEF: {
585 BuildMI(BB, Alpha::IDEF, 0, Result);
586 return Result;
587 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000588
Andrew Lenharth032f2352005-02-22 21:59:48 +0000589 case ISD::DYNAMIC_STACKALLOC:
590 // Generate both result values.
Andrew Lenharth3a7118d2005-02-23 17:33:42 +0000591 if (Result != notIn)
592 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth032f2352005-02-22 21:59:48 +0000593 else
594 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
595
596 // FIXME: We are currently ignoring the requested alignment for handling
597 // greater than the stack alignment. This will need to be revisited at some
598 // point. Align = N.getOperand(2);
599
600 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
601 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
602 std::cerr << "Cannot allocate stack object with greater alignment than"
603 << " the stack alignment yet!";
604 abort();
605 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000606
Andrew Lenharth032f2352005-02-22 21:59:48 +0000607 Select(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +0000608 if (isSIntImmediateBounded(N.getOperand(1), SImm, 0, 32767))
609 BuildMI(BB, Alpha::LDA, 2, Alpha::R30).addImm(-SImm).addReg(Alpha::R30);
610 else {
Andrew Lenharth032f2352005-02-22 21:59:48 +0000611 Tmp1 = SelectExpr(N.getOperand(1));
612 // Subtract size from stack pointer, thereby allocating some space.
613 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
614 }
615
616 // Put a pointer to the space into the result register, by copying the stack
617 // pointer.
Andrew Lenharth7bc47022005-02-22 23:29:25 +0000618 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
Andrew Lenharth032f2352005-02-22 21:59:48 +0000619 return Result;
620
Andrew Lenharth02c318e2005-06-27 21:02:56 +0000621 case ISD::ConstantPool:
Chris Lattner5839bf22005-08-26 17:15:30 +0000622 Tmp1 = BB->getParent()->getConstantPool()->
623 getConstantPoolIndex(cast<ConstantPoolSDNode>(N)->get());
Andrew Lenharth02c318e2005-06-27 21:02:56 +0000624 AlphaLowering.restoreGP(BB);
625 Tmp2 = MakeReg(MVT::i64);
626 BuildMI(BB, Alpha::LDAHr, 2, Tmp2).addConstantPoolIndex(Tmp1)
627 .addReg(Alpha::R29);
628 BuildMI(BB, Alpha::LDAr, 2, Result).addConstantPoolIndex(Tmp1)
629 .addReg(Tmp2);
630 return Result;
Andrew Lenharth2c594352005-01-29 15:42:07 +0000631
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000632 case ISD::FrameIndex:
Andrew Lenharth032f2352005-02-22 21:59:48 +0000633 BuildMI(BB, Alpha::LDA, 2, Result)
634 .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
635 .addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000636 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000637
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000638 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +0000639 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000640 case ISD::SEXTLOAD:
Misha Brukman4633f1c2005-04-21 23:13:11 +0000641 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000642 {
643 // Make sure we generate both values.
644 if (Result != notIn)
645 ExprMap[N.getValue(1)] = notIn; // Generate the token
646 else
647 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +0000648
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000649 SDOperand Chain = N.getOperand(0);
650 SDOperand Address = N.getOperand(1);
651 Select(Chain);
652
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000653 bool fpext = true;
654
Andrew Lenharth03824012005-02-07 05:55:55 +0000655 if (opcode == ISD::LOAD)
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000656 switch (Node->getValueType(0)) {
657 default: Node->dump(); assert(0 && "Bad load!");
658 case MVT::i64: Opc = Alpha::LDQ; break;
659 case MVT::f64: Opc = Alpha::LDT; break;
660 case MVT::f32: Opc = Alpha::LDS; break;
661 }
Andrew Lenharth03824012005-02-07 05:55:55 +0000662 else
Chris Lattnerbce81ae2005-07-10 01:56:13 +0000663 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000664 default: Node->dump(); assert(0 && "Bad sign extend!");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000665 case MVT::i32: Opc = Alpha::LDL;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000666 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000667 case MVT::i16: Opc = Alpha::LDWU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000668 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +0000669 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Misha Brukman4633f1c2005-04-21 23:13:11 +0000670 case MVT::i8: Opc = Alpha::LDBU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000671 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000672 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000673
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000674 int i, j, k;
675 if (EnableAlphaLSMark)
676 getValueInfo(dyn_cast<SrcValueSDNode>(N.getOperand(2))->getValue(),
677 i, j, k);
678
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000679 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Address);
680 if (GASD && !GASD->getGlobal()->isExternal()) {
681 Tmp1 = MakeReg(MVT::i64);
682 AlphaLowering.restoreGP(BB);
683 BuildMI(BB, Alpha::LDAHr, 2, Tmp1)
684 .addGlobalAddress(GASD->getGlobal()).addReg(Alpha::R29);
685 if (EnableAlphaLSMark)
686 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
687 .addImm(getUID());
688 BuildMI(BB, GetRelVersion(Opc), 2, Result)
689 .addGlobalAddress(GASD->getGlobal()).addReg(Tmp1);
Chris Lattnerbce81ae2005-07-10 01:56:13 +0000690 } else if (ConstantPoolSDNode *CP =
691 dyn_cast<ConstantPoolSDNode>(Address)) {
Chris Lattner5839bf22005-08-26 17:15:30 +0000692 unsigned CPIdx = BB->getParent()->getConstantPool()->
693 getConstantPoolIndex(CP->get());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000694 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000695 has_sym = true;
Andrew Lenharthfe895e32005-06-27 17:15:36 +0000696 Tmp1 = MakeReg(MVT::i64);
Chris Lattner5839bf22005-08-26 17:15:30 +0000697 BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CPIdx)
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000698 .addReg(Alpha::R29);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000699 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000700 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
701 .addImm(getUID());
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000702 BuildMI(BB, GetRelVersion(Opc), 2, Result)
Chris Lattner5839bf22005-08-26 17:15:30 +0000703 .addConstantPoolIndex(CPIdx).addReg(Tmp1);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000704 } else if(Address.getOpcode() == ISD::FrameIndex) {
705 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000706 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
707 .addImm(getUID());
Andrew Lenharth032f2352005-02-22 21:59:48 +0000708 BuildMI(BB, Opc, 2, Result)
709 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
710 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000711 } else {
712 long offset;
713 SelectAddr(Address, Tmp1, offset);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000714 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000715 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
716 .addImm(getUID());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000717 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
718 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000719 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000720 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000721
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000722 case ISD::GlobalAddress:
723 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000724 has_sym = true;
Jeff Cohen00b168892005-07-27 06:12:32 +0000725
Andrew Lenharth2f5bca52005-07-03 20:06:13 +0000726 Reg = Result = MakeReg(MVT::i64);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000727
728 if (EnableAlphaLSMark)
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000729 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000730 .addImm(getUID());
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000731
732 BuildMI(BB, Alpha::LDQl, 2, Result)
Andrew Lenharthc95d9842005-06-27 21:11:40 +0000733 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal())
734 .addReg(Alpha::R29);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000735 return Result;
736
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000737 case ISD::ExternalSymbol:
738 AlphaLowering.restoreGP(BB);
739 has_sym = true;
740
Andrew Lenharth2f5bca52005-07-03 20:06:13 +0000741 Reg = Result = MakeReg(MVT::i64);
742
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000743 if (EnableAlphaLSMark)
744 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
745 .addImm(getUID());
746
747 BuildMI(BB, Alpha::LDQl, 2, Result)
748 .addExternalSymbol(cast<ExternalSymbolSDNode>(N)->getSymbol())
749 .addReg(Alpha::R29);
750 return Result;
751
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +0000752 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000753 case ISD::CALL:
754 {
755 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000756
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000757 // The chain for this call is now lowered.
Andrew Lenharthf3f951a2005-07-22 20:50:29 +0000758 ExprMap[N.getValue(Node->getNumValues()-1)] = notIn;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000759
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000760 //grab the arguments
761 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000762 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000763 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000764 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000765
Andrew Lenharth684f2292005-01-30 00:35:27 +0000766 //in reg args
767 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000768 {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000769 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000770 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000771 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000772 Alpha::F19, Alpha::F20, Alpha::F21};
773 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000774 default:
775 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000776 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000777 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000778 N.getOperand(i+2).getValueType() << "\n";
779 assert(0 && "Unknown value type for call");
780 case MVT::i1:
781 case MVT::i8:
782 case MVT::i16:
783 case MVT::i32:
784 case MVT::i64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000785 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i])
786 .addReg(argvregs[i]);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000787 break;
788 case MVT::f32:
789 case MVT::f64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000790 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i])
791 .addReg(argvregs[i]);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000792 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000793 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000794 }
Andrew Lenharth684f2292005-01-30 00:35:27 +0000795 //in mem args
796 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000797 {
798 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000799 default:
800 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000801 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000802 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000803 N.getOperand(i+2).getValueType() << "\n";
804 assert(0 && "Unknown value type for call");
805 case MVT::i1:
806 case MVT::i8:
807 case MVT::i16:
808 case MVT::i32:
809 case MVT::i64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000810 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
811 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000812 break;
813 case MVT::f32:
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000814 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
815 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000816 break;
817 case MVT::f64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000818 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
819 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000820 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000821 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000822 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000823 //build the right kind of call
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000824 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(N.getOperand(1));
825 if (GASD && !GASD->getGlobal()->isExternal()) {
826 //use PC relative branch call
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000827 AlphaLowering.restoreGP(BB);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000828 BuildMI(BB, Alpha::BSR, 1, Alpha::R26)
829 .addGlobalAddress(GASD->getGlobal(),true);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000830 } else {
831 //no need to restore GP as we are doing an indirect call
832 Tmp1 = SelectExpr(N.getOperand(1));
833 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
834 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
835 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000836
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000837 //push the result into a virtual register
Misha Brukman4633f1c2005-04-21 23:13:11 +0000838
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000839 switch (Node->getValueType(0)) {
840 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000841 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000842 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +0000843 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
844 break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000845 case MVT::f32:
846 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +0000847 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
848 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000849 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000850 return Result+N.ResNo;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000851 }
852
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000853 case ISD::SIGN_EXTEND_INREG:
854 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000855 //do SDIV opt for all levels of ints if not dividing by a constant
856 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
857 && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +0000858 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +0000859 unsigned Tmp4 = MakeReg(MVT::f64);
860 unsigned Tmp5 = MakeReg(MVT::f64);
861 unsigned Tmp6 = MakeReg(MVT::f64);
862 unsigned Tmp7 = MakeReg(MVT::f64);
863 unsigned Tmp8 = MakeReg(MVT::f64);
864 unsigned Tmp9 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000865
866 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
867 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
868 MoveInt2FP(Tmp1, Tmp4, true);
869 MoveInt2FP(Tmp2, Tmp5, true);
Andrew Lenharth98169be2005-07-28 18:14:47 +0000870 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Alpha::F31).addReg(Tmp4);
871 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Alpha::F31).addReg(Tmp5);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +0000872 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
Andrew Lenharth98169be2005-07-28 18:14:47 +0000873 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Alpha::F31).addReg(Tmp8);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000874 MoveFP2Int(Tmp9, Result, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +0000875 return Result;
876 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000877
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000878 //Alpha has instructions for a bunch of signed 32 bit stuff
Chris Lattnerbce81ae2005-07-10 01:56:13 +0000879 if(cast<VTSDNode>(Node->getOperand(1))->getVT() == MVT::i32) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000880 switch (N.getOperand(0).getOpcode()) {
881 case ISD::ADD:
882 case ISD::SUB:
883 case ISD::MUL:
884 {
885 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
886 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
887 //FIXME: first check for Scaled Adds and Subs!
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000888 if(!isMul && N.getOperand(0).getOperand(0).getOpcode() == ISD::SHL &&
Andrew Lenharthd2284272005-08-15 14:31:37 +0000889 isSIntImmediateBounded(N.getOperand(0).getOperand(0).getOperand(1), SImm, 2, 3))
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000890 {
Andrew Lenharthd2284272005-08-15 14:31:37 +0000891 bool use4 = SImm == 2;
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000892 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
893 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
894 BuildMI(BB, isAdd?(use4?Alpha::S4ADDL:Alpha::S8ADDL):(use4?Alpha::S4SUBL:Alpha::S8SUBL),
895 2,Result).addReg(Tmp1).addReg(Tmp2);
896 }
897 else if(isAdd && N.getOperand(0).getOperand(1).getOpcode() == ISD::SHL &&
Andrew Lenharthd2284272005-08-15 14:31:37 +0000898 isSIntImmediateBounded(N.getOperand(0).getOperand(1).getOperand(1), SImm, 2, 3))
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000899 {
Andrew Lenharthd2284272005-08-15 14:31:37 +0000900 bool use4 = SImm == 2;
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000901 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
902 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
903 BuildMI(BB, use4?Alpha::S4ADDL:Alpha::S8ADDL, 2,Result).addReg(Tmp1).addReg(Tmp2);
904 }
Andrew Lenharthd2284272005-08-15 14:31:37 +0000905 else if(isSIntImmediateBounded(N.getOperand(0).getOperand(1), SImm, 0, 255))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000906 { //Normal imm add/sub
907 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000908 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +0000909 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000910 }
Andrew Lenharthd2284272005-08-15 14:31:37 +0000911 else if(!isMul && isSIntImmediate(N.getOperand(0).getOperand(1), SImm) &&
912 (((SImm << 32) >> 32) >= -255) && (((SImm << 32) >> 32) <= 0))
Andrew Lenharth6b137d82005-07-22 22:24:01 +0000913 { //handle canonicalization
914 Opc = isAdd ? Alpha::SUBLi : Alpha::ADDLi;
915 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +0000916 SImm = 0 - ((SImm << 32) >> 32);
917 assert(SImm >= 0 && SImm <= 255);
918 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharth6b137d82005-07-22 22:24:01 +0000919 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000920 else
921 { //Normal add/sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000922 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULL : Alpha::SUBL);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000923 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000924 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000925 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
926 }
927 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000928 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000929 default: break; //Fall Though;
930 }
931 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000932 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000933 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Chris Lattnerbce81ae2005-07-10 01:56:13 +0000934 switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000935 default:
936 Node->dump();
937 assert(0 && "Sign Extend InReg not there yet");
938 break;
939 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000940 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000941 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000942 break;
943 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000944 case MVT::i16:
Andrew Lenharth964b6aa2005-10-20 19:39:24 +0000945 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000946 break;
947 case MVT::i8:
Andrew Lenharth964b6aa2005-10-20 19:39:24 +0000948 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000949 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +0000950 case MVT::i1:
951 Tmp2 = MakeReg(MVT::i64);
952 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000953 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +0000954 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000955 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000956 return Result;
957 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000958
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000959 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000960 {
Chris Lattner88ac32c2005-08-09 20:21:10 +0000961 ISD::CondCode CC = cast<CondCodeSDNode>(N.getOperand(2))->get();
962 if (MVT::isInteger(N.getOperand(0).getValueType())) {
963 bool isConst = false;
964 int dir;
Misha Brukman7847fca2005-04-22 17:54:37 +0000965
Chris Lattner88ac32c2005-08-09 20:21:10 +0000966 //Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +0000967 if(isSIntImmediate(N.getOperand(1), SImm) && SImm <= 255 && SImm >= 0)
Chris Lattner88ac32c2005-08-09 20:21:10 +0000968 isConst = true;
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000969
Chris Lattner88ac32c2005-08-09 20:21:10 +0000970 switch (CC) {
971 default: Node->dump(); assert(0 && "Unknown integer comparison!");
972 case ISD::SETEQ:
973 Opc = isConst ? Alpha::CMPEQi : Alpha::CMPEQ; dir=1; break;
974 case ISD::SETLT:
975 Opc = isConst ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
976 case ISD::SETLE:
977 Opc = isConst ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
978 case ISD::SETGT: Opc = Alpha::CMPLT; dir = 2; break;
979 case ISD::SETGE: Opc = Alpha::CMPLE; dir = 2; break;
980 case ISD::SETULT:
981 Opc = isConst ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
982 case ISD::SETUGT: Opc = Alpha::CMPULT; dir = 2; break;
983 case ISD::SETULE:
984 Opc = isConst ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
985 case ISD::SETUGE: Opc = Alpha::CMPULE; dir = 2; break;
986 case ISD::SETNE: {//Handle this one special
987 //std::cerr << "Alpha does not have a setne.\n";
988 //abort();
989 Tmp1 = SelectExpr(N.getOperand(0));
990 Tmp2 = SelectExpr(N.getOperand(1));
991 Tmp3 = MakeReg(MVT::i64);
992 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
993 //Remeber we have the Inv for this CC
994 CCInvMap[N] = Tmp3;
995 //and invert
996 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
997 return Result;
998 }
999 }
1000 if (dir == 1) {
1001 Tmp1 = SelectExpr(N.getOperand(0));
1002 if (isConst) {
Andrew Lenharthd2284272005-08-15 14:31:37 +00001003 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Chris Lattner88ac32c2005-08-09 20:21:10 +00001004 } else {
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001005 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth694c2982005-06-26 23:01:11 +00001006 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001007 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001008 } else { //if (dir == 2) {
1009 Tmp1 = SelectExpr(N.getOperand(1));
1010 Tmp2 = SelectExpr(N.getOperand(0));
1011 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001012 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001013 } else {
1014 //do the comparison
1015 Tmp1 = MakeReg(MVT::f64);
1016 bool inv = SelectFPSetCC(N, Tmp1);
1017
1018 //now arrange for Result (int) to have a 1 or 0
1019 Tmp2 = MakeReg(MVT::i64);
1020 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
1021 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
1022 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
Andrew Lenharth9818c052005-02-05 13:19:12 +00001023 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001024 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001025 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001026
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001027 case ISD::CopyFromReg:
1028 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001029 ++count_ins;
1030
Andrew Lenharth40831c52005-01-28 06:57:18 +00001031 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001032 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001033 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001034 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001035 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001036
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001037 SDOperand Chain = N.getOperand(0);
1038
1039 Select(Chain);
Chris Lattner707ebc52005-08-16 21:56:37 +00001040 unsigned r = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001041 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
Andrew Lenharth619fb522005-07-04 20:07:21 +00001042 if (MVT::isFloatingPoint(N.getValue(0).getValueType()))
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001043 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
1044 else
1045 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001046 return Result;
1047 }
1048
Misha Brukman4633f1c2005-04-21 23:13:11 +00001049 //Most of the plain arithmetic and logic share the same form, and the same
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001050 //constant immediate test
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001051 case ISD::XOR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001052 //Match Not
Andrew Lenharthd2284272005-08-15 14:31:37 +00001053 if (isSIntImmediate(N.getOperand(1), SImm) && SImm == -1) {
1054 Tmp1 = SelectExpr(N.getOperand(0));
1055 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1056 return Result;
1057 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001058 //Fall through
1059 case ISD::AND:
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001060 //handle zap
Andrew Lenharthd2284272005-08-15 14:31:37 +00001061 if (opcode == ISD::AND && isUIntImmediate(N.getOperand(1), UImm))
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001062 {
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001063 unsigned int build = 0;
1064 for(int i = 0; i < 8; ++i)
1065 {
Andrew Lenharthd2284272005-08-15 14:31:37 +00001066 if ((UImm & 0x00FF) == 0x00FF)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001067 build |= 1 << i;
Andrew Lenharthd2284272005-08-15 14:31:37 +00001068 else if ((UImm & 0x00FF) != 0)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001069 { build = 0; break; }
Andrew Lenharthd2284272005-08-15 14:31:37 +00001070 UImm >>= 8;
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001071 }
1072 if (build)
1073 {
1074 Tmp1 = SelectExpr(N.getOperand(0));
1075 BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1076 return Result;
1077 }
1078 }
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001079 case ISD::OR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001080 //Check operand(0) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001081 if (N.getOperand(0).getOpcode() == ISD::XOR &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001082 isSIntImmediate(N.getOperand(0).getOperand(1), SImm) && SImm == -1) {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001083 switch(opcode) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001084 case ISD::AND: Opc = Alpha::BIC; break;
1085 case ISD::OR: Opc = Alpha::ORNOT; break;
1086 case ISD::XOR: Opc = Alpha::EQV; break;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001087 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001088 Tmp1 = SelectExpr(N.getOperand(1));
1089 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1090 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1091 return Result;
1092 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001093 //Check operand(1) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001094 if (N.getOperand(1).getOpcode() == ISD::XOR &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001095 isSIntImmediate(N.getOperand(1).getOperand(1), SImm) && SImm == -1) {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001096 switch(opcode) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001097 case ISD::AND: Opc = Alpha::BIC; break;
1098 case ISD::OR: Opc = Alpha::ORNOT; break;
1099 case ISD::XOR: Opc = Alpha::EQV; break;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001100 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001101 Tmp1 = SelectExpr(N.getOperand(0));
1102 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1103 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1104 return Result;
1105 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001106 //Fall through
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001107 case ISD::SHL:
1108 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001109 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001110 case ISD::MUL:
Andrew Lenharthd2284272005-08-15 14:31:37 +00001111 if(isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255)) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001112 switch(opcode) {
1113 case ISD::AND: Opc = Alpha::ANDi; break;
1114 case ISD::OR: Opc = Alpha::BISi; break;
1115 case ISD::XOR: Opc = Alpha::XORi; break;
1116 case ISD::SHL: Opc = Alpha::SLi; break;
1117 case ISD::SRL: Opc = Alpha::SRLi; break;
1118 case ISD::SRA: Opc = Alpha::SRAi; break;
1119 case ISD::MUL: Opc = Alpha::MULQi; break;
1120 };
1121 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001122 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001123 } else {
1124 switch(opcode) {
1125 case ISD::AND: Opc = Alpha::AND; break;
1126 case ISD::OR: Opc = Alpha::BIS; break;
1127 case ISD::XOR: Opc = Alpha::XOR; break;
1128 case ISD::SHL: Opc = Alpha::SL; break;
1129 case ISD::SRL: Opc = Alpha::SRL; break;
1130 case ISD::SRA: Opc = Alpha::SRA; break;
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001131 case ISD::MUL: Opc = Alpha::MULQ; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001132 };
1133 Tmp1 = SelectExpr(N.getOperand(0));
1134 Tmp2 = SelectExpr(N.getOperand(1));
1135 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1136 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001137 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001138
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001139 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001140 case ISD::SUB:
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001141 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001142 bool isAdd = opcode == ISD::ADD;
1143
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001144 //first check for Scaled Adds and Subs!
1145 //Valid for add and sub
Andrew Lenharthd2284272005-08-15 14:31:37 +00001146 if(N.getOperand(0).getOpcode() == ISD::SHL &&
1147 isSIntImmediate(N.getOperand(0).getOperand(1), SImm) &&
1148 (SImm == 2 || SImm == 3)) {
1149 bool use4 = SImm == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001150 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001151 if (isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255))
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001152 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
Andrew Lenharthd2284272005-08-15 14:31:37 +00001153 2, Result).addReg(Tmp2).addImm(SImm);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001154 else {
1155 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001156 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1157 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001158 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001159 }
1160 //Position prevents subs
Andrew Lenharth273a1f92005-04-07 14:18:13 +00001161 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001162 isSIntImmediate(N.getOperand(1).getOperand(1), SImm) &&
1163 (SImm == 2 || SImm == 3)) {
1164 bool use4 = SImm == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001165 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001166 if (isSIntImmediateBounded(N.getOperand(0), SImm, 0, 255))
1167 BuildMI(BB, use4?Alpha::S4ADDQi:Alpha::S8ADDQi, 2, Result).addReg(Tmp2).addImm(SImm);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001168 else {
1169 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001170 BuildMI(BB, use4?Alpha::S4ADDQ:Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001171 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001172 }
1173 //small addi
Andrew Lenharthd2284272005-08-15 14:31:37 +00001174 else if(isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001175 { //Normal imm add/sub
1176 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1177 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001178 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001179 }
Andrew Lenharthd2284272005-08-15 14:31:37 +00001180 else if(isSIntImmediateBounded(N.getOperand(1), SImm, -255, 0))
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001181 { //inverted imm add/sub
1182 Opc = isAdd ? Alpha::SUBQi : Alpha::ADDQi;
1183 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001184 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(-SImm);
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001185 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001186 //larger addi
Andrew Lenharthd2284272005-08-15 14:31:37 +00001187 else if(isSIntImmediateBounded(N.getOperand(1), SImm, -32767, 32767))
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001188 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001189 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001190 if (!isAdd)
Andrew Lenharthd2284272005-08-15 14:31:37 +00001191 SImm = -SImm;
1192 BuildMI(BB, Alpha::LDA, 2, Result).addImm(SImm).addReg(Tmp1);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001193 }
1194 //give up and do the operation
1195 else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001196 //Normal add/sub
1197 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1198 Tmp1 = SelectExpr(N.getOperand(0));
1199 Tmp2 = SelectExpr(N.getOperand(1));
1200 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1201 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001202 return Result;
1203 }
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001204 case ISD::FADD:
1205 case ISD::FSUB:
1206 case ISD::FMUL:
1207 case ISD::FDIV: {
1208 if (opcode == ISD::FADD)
1209 Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS;
1210 else if (opcode == ISD::FSUB)
1211 Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS;
1212 else if (opcode == ISD::FMUL)
1213 Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS;
1214 else
1215 Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS;
1216 Tmp1 = SelectExpr(N.getOperand(0));
1217 Tmp2 = SelectExpr(N.getOperand(1));
1218 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1219 return Result;
1220 }
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001221 case ISD::SDIV:
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001222 {
Andrew Lenhartha565c272005-04-06 22:03:13 +00001223 //check if we can convert into a shift!
Andrew Lenharthd2284272005-08-15 14:31:37 +00001224 if (isSIntImmediate(N.getOperand(1), SImm) &&
1225 SImm != 0 && isPowerOf2_64(llabs(SImm))) {
1226 unsigned k = Log2_64(llabs(SImm));
Andrew Lenhartha565c272005-04-06 22:03:13 +00001227 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenhartha565c272005-04-06 22:03:13 +00001228 if (k == 1)
1229 Tmp2 = Tmp1;
1230 else
1231 {
1232 Tmp2 = MakeReg(MVT::i64);
1233 BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
1234 }
1235 Tmp3 = MakeReg(MVT::i64);
1236 BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
1237 unsigned Tmp4 = MakeReg(MVT::i64);
1238 BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharthd2284272005-08-15 14:31:37 +00001239 if (SImm > 0)
Andrew Lenhartha565c272005-04-06 22:03:13 +00001240 BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
1241 else
1242 {
1243 unsigned Tmp5 = MakeReg(MVT::i64);
1244 BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
1245 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
1246 }
1247 return Result;
1248 }
1249 }
1250 //Else fall through
Andrew Lenhartha565c272005-04-06 22:03:13 +00001251 case ISD::UDIV:
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001252 //else fall though
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001253 case ISD::UREM:
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001254 case ISD::SREM: {
1255 const char* opstr = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001256 switch(opcode) {
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001257 case ISD::UREM: opstr = "__remqu"; break;
1258 case ISD::SREM: opstr = "__remq"; break;
1259 case ISD::UDIV: opstr = "__divqu"; break;
1260 case ISD::SDIV: opstr = "__divq"; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001261 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001262 Tmp1 = SelectExpr(N.getOperand(0));
1263 Tmp2 = SelectExpr(N.getOperand(1));
Jeff Cohen00b168892005-07-27 06:12:32 +00001264 SDOperand Addr =
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001265 ISelDAG->getExternalSymbol(opstr, AlphaLowering.getPointerTy());
1266 Tmp3 = SelectExpr(Addr);
Andrew Lenharth33819132005-03-04 20:09:23 +00001267 //set up regs explicitly (helps Reg alloc)
1268 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001269 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001270 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp3).addReg(Tmp3);
1271 BuildMI(BB, Alpha::JSRs, 2, Alpha::R23).addReg(Alpha::R27).addImm(0);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001272 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001273 return Result;
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001274 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001275
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001276 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001277 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001278 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001279 assert (DestType == MVT::i64 && "only quads can be loaded to");
1280 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00001281 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001282 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001283 if (SrcType == MVT::f32)
Misha Brukman7847fca2005-04-22 17:54:37 +00001284 {
1285 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth98169be2005-07-28 18:14:47 +00001286 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Alpha::F31).addReg(Tmp1);
Misha Brukman7847fca2005-04-22 17:54:37 +00001287 Tmp1 = Tmp2;
1288 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001289 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth98169be2005-07-28 18:14:47 +00001290 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001291 MoveFP2Int(Tmp2, Result, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001292
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001293 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001294 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001295
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001296 case ISD::SELECT:
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001297 if (isFP) {
1298 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1299 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
1300 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
1301
1302 SDOperand CC = N.getOperand(0);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001303
Chris Lattner88ac32c2005-08-09 20:21:10 +00001304 if (CC.getOpcode() == ISD::SETCC &&
1305 !MVT::isInteger(CC.getOperand(0).getValueType())) {
1306 //FP Setcc -> Select yay!
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001307
Jeff Cohen00b168892005-07-27 06:12:32 +00001308
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001309 //for a cmp b: c = a - b;
1310 //a = b: c = 0
1311 //a < b: c < 0
1312 //a > b: c > 0
1313
1314 bool invTest = false;
1315 unsigned Tmp3;
1316
1317 ConstantFPSDNode *CN;
Chris Lattner88ac32c2005-08-09 20:21:10 +00001318 if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(1)))
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001319 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
Chris Lattner88ac32c2005-08-09 20:21:10 +00001320 Tmp3 = SelectExpr(CC.getOperand(0));
1321 else if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(0)))
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001322 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1323 {
Chris Lattner88ac32c2005-08-09 20:21:10 +00001324 Tmp3 = SelectExpr(CC.getOperand(1));
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001325 invTest = true;
1326 }
1327 else
1328 {
Chris Lattner88ac32c2005-08-09 20:21:10 +00001329 unsigned Tmp1 = SelectExpr(CC.getOperand(0));
1330 unsigned Tmp2 = SelectExpr(CC.getOperand(1));
1331 bool isD = CC.getOperand(0).getValueType() == MVT::f64;
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001332 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1333 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1334 .addReg(Tmp1).addReg(Tmp2);
1335 }
1336
Chris Lattner88ac32c2005-08-09 20:21:10 +00001337 switch (cast<CondCodeSDNode>(CC.getOperand(2))->get()) {
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001338 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
1339 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNE : Alpha::FCMOVEQ; break;
1340 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGT : Alpha::FCMOVLT; break;
1341 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGE : Alpha::FCMOVLE; break;
1342 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLT : Alpha::FCMOVGT; break;
1343 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLE : Alpha::FCMOVGE; break;
1344 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQ : Alpha::FCMOVNE; break;
1345 }
1346 BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
1347 return Result;
1348 }
1349 else
1350 {
1351 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1352 BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV)
1353 .addReg(Tmp1);
1354// // Spill the cond to memory and reload it from there.
1355// unsigned Tmp4 = MakeReg(MVT::f64);
1356// MoveIntFP(Tmp1, Tmp4, true);
1357// //now ideally, we don't have to do anything to the flag...
1358// // Get the condition into the zero flag.
1359// BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
1360 return Result;
Jeff Cohen00b168892005-07-27 06:12:32 +00001361 }
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001362 } else {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001363 //FIXME: look at parent to decide if intCC can be folded, or if setCC(FP)
1364 //and can save stack use
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001365 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001366 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1367 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001368 // Get the condition into the zero flag.
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001369 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001370
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001371 SDOperand CC = N.getOperand(0);
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001372
Misha Brukman4633f1c2005-04-21 23:13:11 +00001373 if (CC.getOpcode() == ISD::SETCC &&
Chris Lattner88ac32c2005-08-09 20:21:10 +00001374 !MVT::isInteger(CC.getOperand(0).getValueType()))
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001375 { //FP Setcc -> Int Select
Misha Brukman7847fca2005-04-22 17:54:37 +00001376 Tmp1 = MakeReg(MVT::f64);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001377 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1378 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Misha Brukman7847fca2005-04-22 17:54:37 +00001379 bool inv = SelectFPSetCC(CC, Tmp1);
1380 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
1381 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
1382 return Result;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001383 }
1384 if (CC.getOpcode() == ISD::SETCC) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001385 //Int SetCC -> Select
1386 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharthd2284272005-08-15 14:31:37 +00001387 if(isSIntImmediateBounded(CC.getOperand(1), SImm, 0, 0)) {
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001388 //figure out a few things
Andrew Lenharthd2284272005-08-15 14:31:37 +00001389 bool useImm = isSIntImmediateBounded(N.getOperand(2), SImm, 0, 255);
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001390
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001391 //Fix up CC
Chris Lattner88ac32c2005-08-09 20:21:10 +00001392 ISD::CondCode cCode= cast<CondCodeSDNode>(CC.getOperand(2))->get();
Andrew Lenharth694c2982005-06-26 23:01:11 +00001393 if (useImm) //Invert sense to get Imm field right
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001394 cCode = ISD::getSetCCInverse(cCode, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001395
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001396 //Choose the CMOV
1397 switch (cCode) {
1398 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001399 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
1400 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
1401 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
1402 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
1403 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
1404 case ISD::SETULT: assert(0 && "unsigned < 0 is never true"); break;
1405 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
1406 //Technically you could have this CC
1407 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
1408 case ISD::SETUGE: assert(0 && "unsgined >= 0 is always true"); break;
1409 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001410 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001411 Tmp1 = SelectExpr(CC.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001412
Andrew Lenharth694c2982005-06-26 23:01:11 +00001413 if (useImm) {
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001414 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
Andrew Lenharthd2284272005-08-15 14:31:37 +00001415 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addImm(SImm).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001416 } else {
1417 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1418 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1419 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
1420 }
1421 return Result;
1422 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001423 //Otherwise, fall though
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001424 }
1425 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001426 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1427 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001428 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3)
1429 .addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001430
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001431 return Result;
1432 }
1433
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001434 case ISD::Constant:
1435 {
Andrew Lenharthc0513832005-03-29 19:24:04 +00001436 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001437 int zero_extend_top = 0;
Andrew Lenharthf075cac2005-07-23 07:46:48 +00001438 if (val > 0 && (val & 0xFFFFFFFF00000000ULL) == 0 &&
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001439 ((int32_t)val < 0)) {
1440 //try a small load and zero extend
1441 val = (int32_t)val;
1442 zero_extend_top = 15;
1443 }
1444
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00001445 if (val <= IMM_HIGH && val >= IMM_LOW) {
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001446 if(!zero_extend_top)
1447 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
1448 else {
1449 Tmp1 = MakeReg(MVT::i64);
1450 BuildMI(BB, Alpha::LDA, 2, Tmp1).addImm(val).addReg(Alpha::R31);
1451 BuildMI(BB, Alpha::ZAPNOT, 2, Result).addReg(Tmp1).addImm(zero_extend_top);
1452 }
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00001453 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001454 else if (val <= (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT &&
1455 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
1456 Tmp1 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001457 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val))
1458 .addReg(Alpha::R31);
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001459 if (!zero_extend_top)
1460 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
1461 else {
1462 Tmp3 = MakeReg(MVT::i64);
1463 BuildMI(BB, Alpha::LDA, 2, Tmp3).addImm(getLower16(val)).addReg(Tmp1);
1464 BuildMI(BB, Alpha::ZAPNOT, 2, Result).addReg(Tmp3).addImm(zero_extend_top);
1465 }
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00001466 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001467 else {
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001468 //re-get the val since we are going to mem anyway
1469 val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001470 MachineConstantPool *CP = BB->getParent()->getConstantPool();
Jeff Cohen00b168892005-07-27 06:12:32 +00001471 ConstantUInt *C =
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001472 ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001473 unsigned CPI = CP->getConstantPoolIndex(C);
1474 AlphaLowering.restoreGP(BB);
Andrew Lenharthfe895e32005-06-27 17:15:36 +00001475 has_sym = true;
1476 Tmp1 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001477 BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CPI)
1478 .addReg(Alpha::R29);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001479 if (EnableAlphaLSMark)
1480 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
1481 .addImm(getUID());
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001482 BuildMI(BB, Alpha::LDQr, 2, Result).addConstantPoolIndex(CPI)
1483 .addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001484 }
1485 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001486 }
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001487 case ISD::FNEG:
1488 if(ISD::FABS == N.getOperand(0).getOpcode())
1489 {
1490 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1491 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
1492 } else {
1493 Tmp1 = SelectExpr(N.getOperand(0));
1494 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp1).addReg(Tmp1);
1495 }
1496 return Result;
1497
1498 case ISD::FABS:
1499 Tmp1 = SelectExpr(N.getOperand(0));
1500 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
1501 return Result;
1502
1503 case ISD::FP_ROUND:
1504 assert (DestType == MVT::f32 &&
1505 N.getOperand(0).getValueType() == MVT::f64 &&
1506 "only f64 to f32 conversion supported here");
1507 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth98169be2005-07-28 18:14:47 +00001508 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001509 return Result;
1510
1511 case ISD::FP_EXTEND:
1512 assert (DestType == MVT::f64 &&
1513 N.getOperand(0).getValueType() == MVT::f32 &&
1514 "only f32 to f64 conversion supported here");
1515 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth98169be2005-07-28 18:14:47 +00001516 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001517 return Result;
1518
1519 case ISD::ConstantFP:
1520 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
1521 if (CN->isExactlyValue(+0.0)) {
1522 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31)
1523 .addReg(Alpha::F31);
1524 } else if ( CN->isExactlyValue(-0.0)) {
1525 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31)
1526 .addReg(Alpha::F31);
1527 } else {
1528 abort();
1529 }
1530 }
1531 return Result;
1532
1533 case ISD::SINT_TO_FP:
1534 {
1535 assert (N.getOperand(0).getValueType() == MVT::i64
1536 && "only quads can be loaded from");
1537 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1538 Tmp2 = MakeReg(MVT::f64);
1539 MoveInt2FP(Tmp1, Tmp2, true);
1540 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
Andrew Lenharth98169be2005-07-28 18:14:47 +00001541 BuildMI(BB, Opc, 1, Result).addReg(Alpha::F31).addReg(Tmp2);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001542 return Result;
1543 }
Andrew Lenharthf71df332005-09-04 06:12:19 +00001544
1545 case ISD::AssertSext:
1546 case ISD::AssertZext:
1547 return SelectExpr(N.getOperand(0));
1548
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001549 }
1550
1551 return 0;
1552}
1553
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001554void AlphaISel::Select(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001555 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00001556 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001557
Nate Begeman85fdeb22005-03-24 04:39:54 +00001558 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001559 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001560
1561 SDNode *Node = N.Val;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001562
Andrew Lenharth760270d2005-02-07 23:02:23 +00001563 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001564
1565 default:
1566 Node->dump(); std::cerr << "\n";
1567 assert(0 && "Node not handled yet!");
1568
1569 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00001570 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001571 return;
1572 }
1573
1574 case ISD::BR: {
1575 MachineBasicBlock *Dest =
1576 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1577
1578 Select(N.getOperand(0));
1579 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
1580 return;
1581 }
1582
1583 case ISD::ImplicitDef:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001584 ++count_ins;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001585 Select(N.getOperand(0));
Chris Lattner707ebc52005-08-16 21:56:37 +00001586 BuildMI(BB, Alpha::IDEF, 0,
1587 cast<RegisterSDNode>(N.getOperand(1))->getReg());
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001588 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001589
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001590 case ISD::EntryToken: return; // Noop
1591
1592 case ISD::TokenFactor:
1593 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1594 Select(Node->getOperand(i));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001595
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001596 //N.Val->dump(); std::cerr << "\n";
1597 //assert(0 && "Node not handled yet!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001598
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001599 return;
1600
1601 case ISD::CopyToReg:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001602 ++count_outs;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001603 Select(N.getOperand(0));
Chris Lattner707ebc52005-08-16 21:56:37 +00001604 Tmp1 = SelectExpr(N.getOperand(2));
1605 Tmp2 = cast<RegisterSDNode>(N.getOperand(1))->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001606
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001607 if (Tmp1 != Tmp2) {
Chris Lattner707ebc52005-08-16 21:56:37 +00001608 if (N.getOperand(2).getValueType() == MVT::f64 ||
1609 N.getOperand(2).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00001610 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1611 else
1612 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001613 }
1614 return;
1615
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001616 case ISD::RET:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001617 ++count_outs;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001618 switch (N.getNumOperands()) {
1619 default:
1620 std::cerr << N.getNumOperands() << "\n";
1621 for (unsigned i = 0; i < N.getNumOperands(); ++i)
1622 std::cerr << N.getOperand(i).getValueType() << "\n";
1623 Node->dump();
1624 assert(0 && "Unknown return instruction!");
1625 case 2:
1626 Select(N.getOperand(0));
1627 Tmp1 = SelectExpr(N.getOperand(1));
1628 switch (N.getOperand(1).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001629 default: Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001630 assert(0 && "All other types should have been promoted!!");
1631 case MVT::f64:
1632 case MVT::f32:
1633 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
1634 break;
1635 case MVT::i32:
1636 case MVT::i64:
1637 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
1638 break;
1639 }
1640 break;
1641 case 1:
1642 Select(N.getOperand(0));
1643 break;
1644 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001645 // Just emit a 'ret' instruction
Andrew Lenharth6968bff2005-06-27 23:24:11 +00001646 AlphaLowering.restoreRA(BB);
Andrew Lenharthf3f951a2005-07-22 20:50:29 +00001647 BuildMI(BB, Alpha::RET, 2, Alpha::R31).addReg(Alpha::R26).addImm(1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001648 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001649
Misha Brukman4633f1c2005-04-21 23:13:11 +00001650 case ISD::TRUNCSTORE:
1651 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001652 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001653 SDOperand Chain = N.getOperand(0);
1654 SDOperand Value = N.getOperand(1);
1655 SDOperand Address = N.getOperand(2);
1656 Select(Chain);
1657
1658 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00001659
1660 if (opcode == ISD::STORE) {
1661 switch(Value.getValueType()) {
1662 default: assert(0 && "unknown Type in store");
1663 case MVT::i64: Opc = Alpha::STQ; break;
1664 case MVT::f64: Opc = Alpha::STT; break;
1665 case MVT::f32: Opc = Alpha::STS; break;
1666 }
1667 } else { //ISD::TRUNCSTORE
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001668 switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) {
Andrew Lenharth760270d2005-02-07 23:02:23 +00001669 default: assert(0 && "unknown Type in store");
Andrew Lenharth760270d2005-02-07 23:02:23 +00001670 case MVT::i8: Opc = Alpha::STB; break;
1671 case MVT::i16: Opc = Alpha::STW; break;
1672 case MVT::i32: Opc = Alpha::STL; break;
1673 }
Andrew Lenharth65838902005-02-06 16:22:15 +00001674 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00001675
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001676 int i, j, k;
Jeff Cohen00b168892005-07-27 06:12:32 +00001677 if (EnableAlphaLSMark)
1678 getValueInfo(cast<SrcValueSDNode>(N.getOperand(3))->getValue(),
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001679 i, j, k);
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001680
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001681 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Address);
1682 if (GASD && !GASD->getGlobal()->isExternal()) {
1683 Tmp2 = MakeReg(MVT::i64);
1684 AlphaLowering.restoreGP(BB);
1685 BuildMI(BB, Alpha::LDAHr, 2, Tmp2)
1686 .addGlobalAddress(GASD->getGlobal()).addReg(Alpha::R29);
1687 if (EnableAlphaLSMark)
1688 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1689 .addImm(getUID());
1690 BuildMI(BB, GetRelVersion(Opc), 3).addReg(Tmp1)
1691 .addGlobalAddress(GASD->getGlobal()).addReg(Tmp2);
Andrew Lenharthfce587e2005-06-29 00:39:17 +00001692 } else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001693 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001694 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1695 .addImm(getUID());
Andrew Lenharth032f2352005-02-22 21:59:48 +00001696 BuildMI(BB, Opc, 3).addReg(Tmp1)
1697 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1698 .addReg(Alpha::F31);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001699 } else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001700 long offset;
1701 SelectAddr(Address, Tmp2, offset);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001702 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001703 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1704 .addImm(getUID());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001705 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1706 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001707 return;
1708 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001709
1710 case ISD::EXTLOAD:
1711 case ISD::SEXTLOAD:
1712 case ISD::ZEXTLOAD:
1713 case ISD::LOAD:
1714 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001715 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001716 case ISD::CALL:
Andrew Lenharth032f2352005-02-22 21:59:48 +00001717 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001718 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001719 SelectExpr(N);
1720 return;
1721
Chris Lattner16cd04d2005-05-12 23:24:06 +00001722 case ISD::CALLSEQ_START:
1723 case ISD::CALLSEQ_END:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001724 Select(N.getOperand(0));
1725 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001726
Chris Lattner16cd04d2005-05-12 23:24:06 +00001727 Opc = N.getOpcode() == ISD::CALLSEQ_START ? Alpha::ADJUSTSTACKDOWN :
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001728 Alpha::ADJUSTSTACKUP;
1729 BuildMI(BB, Opc, 1).addImm(Tmp1);
1730 return;
Andrew Lenharth95762122005-03-31 21:24:06 +00001731
1732 case ISD::PCMARKER:
1733 Select(N.getOperand(0)); //Chain
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001734 BuildMI(BB, Alpha::PCLABEL, 2)
1735 .addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
Andrew Lenharth95762122005-03-31 21:24:06 +00001736 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001737 }
1738 assert(0 && "Should not be reached!");
1739}
1740
1741
1742/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
1743/// into a machine code representation using pattern matching and a machine
1744/// description file.
1745///
1746FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001747 return new AlphaISel(TM);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001748}
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001749