blob: 80e18e7325ba889518a2575719d623a61672a9da [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{
Andrew Lenharth4052f022005-11-22 20:59:00 +0000283 unsigned Opc = Alpha::WTF;
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{
Andrew Lenharth4052f022005-11-22 20:59:00 +0000310 unsigned Opc = Alpha::WTF;
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 Lenharth4052f022005-11-22 20:59:00 +0000338 unsigned Tmp1, Tmp2, Tmp3, Opc = Alpha::WTF;
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)
Andrew Lenharth72d32c22005-11-30 17:14:11 +0000368 assert(0 && "Setcc On float?\n");
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000369 if (SetCC->getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth72d32c22005-11-30 17:14:11 +0000370 assert (0 && "Setcc On float?\n");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000371
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000372 if (rev) std::swap(Tmp1, Tmp2);
373 //do the comparison
374 BuildMI(BB, Opc, 2, dst).addReg(Tmp1).addReg(Tmp2);
375 return inv;
376}
377
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000378//Check to see if the load is a constant offset from a base register
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000379void AlphaISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000380{
381 unsigned opcode = N.getOpcode();
Andrew Lenharth694c2982005-06-26 23:01:11 +0000382 if (opcode == ISD::ADD && N.getOperand(1).getOpcode() == ISD::Constant &&
383 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
384 { //Normal imm add
385 Reg = SelectExpr(N.getOperand(0));
386 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
387 return;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000388 }
389 Reg = SelectExpr(N);
390 offset = 0;
391 return;
392}
393
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000394void AlphaISel::SelectBranchCC(SDOperand N)
Andrew Lenharth445171a2005-02-08 00:40:03 +0000395{
396 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000397 MachineBasicBlock *Dest =
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000398 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
399 unsigned Opc = Alpha::WTF;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000400
Andrew Lenharth445171a2005-02-08 00:40:03 +0000401 Select(N.getOperand(0)); //chain
402 SDOperand CC = N.getOperand(1);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000403
Andrew Lenharth445171a2005-02-08 00:40:03 +0000404 if (CC.getOpcode() == ISD::SETCC)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000405 {
Chris Lattner88ac32c2005-08-09 20:21:10 +0000406 ISD::CondCode cCode= cast<CondCodeSDNode>(CC.getOperand(2))->get();
407 if (MVT::isInteger(CC.getOperand(0).getValueType())) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000408 //Dropping the CC is only useful if we are comparing to 0
Chris Lattner88ac32c2005-08-09 20:21:10 +0000409 bool RightZero = CC.getOperand(1).getOpcode() == ISD::Constant &&
410 cast<ConstantSDNode>(CC.getOperand(1))->getValue() == 0;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000411 bool isNE = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000412
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000413 //Fix up CC
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000414 if(cCode == ISD::SETNE)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000415 isNE = true;
Andrew Lenharth445171a2005-02-08 00:40:03 +0000416
Andrew Lenharth694c2982005-06-26 23:01:11 +0000417 if (RightZero) {
Andrew Lenharth09552bf2005-06-08 18:02:21 +0000418 switch (cCode) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000419 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
420 case ISD::SETEQ: Opc = Alpha::BEQ; break;
421 case ISD::SETLT: Opc = Alpha::BLT; break;
422 case ISD::SETLE: Opc = Alpha::BLE; break;
423 case ISD::SETGT: Opc = Alpha::BGT; break;
424 case ISD::SETGE: Opc = Alpha::BGE; break;
425 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
426 case ISD::SETUGT: Opc = Alpha::BNE; break;
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000427 //Technically you could have this CC
428 case ISD::SETULE: Opc = Alpha::BEQ; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000429 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
430 case ISD::SETNE: Opc = Alpha::BNE; break;
431 }
Chris Lattner88ac32c2005-08-09 20:21:10 +0000432 unsigned Tmp1 = SelectExpr(CC.getOperand(0)); //Cond
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000433 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
434 return;
435 } else {
436 unsigned Tmp1 = SelectExpr(CC);
437 if (isNE)
438 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
439 else
440 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000441 return;
442 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000443 } else { //FP
Jeff Cohen00b168892005-07-27 06:12:32 +0000444 //Any comparison between 2 values should be codegened as an folded
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000445 //branch, as moving CC to the integer register is very expensive
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000446 //for a cmp b: c = a - b;
447 //a = b: c = 0
448 //a < b: c < 0
449 //a > b: c > 0
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000450
451 bool invTest = false;
452 unsigned Tmp3;
453
454 ConstantFPSDNode *CN;
Chris Lattner88ac32c2005-08-09 20:21:10 +0000455 if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(1)))
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000456 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
Chris Lattner88ac32c2005-08-09 20:21:10 +0000457 Tmp3 = SelectExpr(CC.getOperand(0));
458 else if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(0)))
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000459 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
460 {
Chris Lattner88ac32c2005-08-09 20:21:10 +0000461 Tmp3 = SelectExpr(CC.getOperand(1));
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000462 invTest = true;
463 }
464 else
465 {
Chris Lattner88ac32c2005-08-09 20:21:10 +0000466 unsigned Tmp1 = SelectExpr(CC.getOperand(0));
467 unsigned Tmp2 = SelectExpr(CC.getOperand(1));
468 bool isD = CC.getOperand(0).getValueType() == MVT::f64;
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000469 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
470 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
471 .addReg(Tmp1).addReg(Tmp2);
472 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000473
Chris Lattner88ac32c2005-08-09 20:21:10 +0000474 switch (cCode) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000475 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000476 case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
477 case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
478 case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
479 case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
480 case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
481 case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000482 }
483 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000484 return;
485 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000486 abort(); //Should never be reached
487 } else {
488 //Giveup and do the stupid thing
489 unsigned Tmp1 = SelectExpr(CC);
490 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
491 return;
492 }
Andrew Lenharth445171a2005-02-08 00:40:03 +0000493 abort(); //Should never be reached
494}
495
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000496unsigned AlphaISel::SelectExpr(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000497 unsigned Result;
Andrew Lenharth2966e842005-04-07 18:15:28 +0000498 unsigned Tmp1, Tmp2 = 0, Tmp3;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000499 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000500 unsigned opcode = N.getOpcode();
Chris Lattnerd2fc54e2005-10-21 16:01:26 +0000501 int64_t SImm = 0;
Andrew Lenharthd2284272005-08-15 14:31:37 +0000502 uint64_t UImm;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000503
504 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000505 MVT::ValueType DestType = N.getValueType();
Andrew Lenharthf4da9452005-06-29 12:49:51 +0000506 bool isFP = DestType == MVT::f64 || DestType == MVT::f32;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000507
508 unsigned &Reg = ExprMap[N];
509 if (Reg) return Reg;
510
Andrew Lenharth46a776e2005-09-06 17:00:23 +0000511 switch(N.getOpcode()) {
512 default:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000513 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000514 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth46a776e2005-09-06 17:00:23 +0000515 break;
516 case ISD::AssertSext:
517 case ISD::AssertZext:
518 return Reg = SelectExpr(N.getOperand(0));
519 case ISD::CALL:
520 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000521 // If this is a call instruction, make sure to prepare ALL of the result
522 // values as well as the chain.
523 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000524 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000525 else {
526 Result = MakeReg(Node->getValueType(0));
527 ExprMap[N.getValue(0)] = Result;
528 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
529 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000530 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000531 }
Andrew Lenharth46a776e2005-09-06 17:00:23 +0000532 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000533 }
534
Andrew Lenharth40831c52005-01-28 06:57:18 +0000535 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000536 default:
537 Node->dump();
538 assert(0 && "Node not handled!\n");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000539
Andrew Lenharth51b8d542005-11-11 16:47:30 +0000540 case ISD::READCYCLECOUNTER:
541 Select(N.getOperand(0)); //Select chain
Andrew Lenharth2729e612005-11-11 23:02:55 +0000542 if (Result != notIn)
543 ExprMap[N.getValue(1)] = notIn; // Generate the token
544 else
545 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
546
Andrew Lenharth51b8d542005-11-11 16:47:30 +0000547 BuildMI(BB, Alpha::RPCC, 1, Result).addReg(Alpha::R31);
548 return Result;
549
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000550 case ISD::CTPOP:
551 case ISD::CTTZ:
552 case ISD::CTLZ:
553 Opc = opcode == ISD::CTPOP ? Alpha::CTPOP :
554 (opcode == ISD::CTTZ ? Alpha::CTTZ : Alpha::CTLZ);
555 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth964b6aa2005-10-20 19:39:24 +0000556 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000557 return Result;
558
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000559 case ISD::MULHU:
560 Tmp1 = SelectExpr(N.getOperand(0));
561 Tmp2 = SelectExpr(N.getOperand(1));
562 BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth706be912005-04-07 13:55:53 +0000563 return Result;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000564 case ISD::MULHS:
565 {
566 //MULHU - Ra<63>*Rb - Rb<63>*Ra
567 Tmp1 = SelectExpr(N.getOperand(0));
568 Tmp2 = SelectExpr(N.getOperand(1));
569 Tmp3 = MakeReg(MVT::i64);
570 BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
571 unsigned V1 = MakeReg(MVT::i64);
572 unsigned V2 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000573 BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31)
574 .addReg(Tmp1);
575 BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31)
576 .addReg(Tmp2);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000577 unsigned IRes = MakeReg(MVT::i64);
578 BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
579 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
580 return Result;
581 }
Andrew Lenharth7332f3e2005-04-02 19:11:07 +0000582 case ISD::UNDEF: {
Andrew Lenharth50b37842005-11-22 04:20:06 +0000583 Opc = isFP ? (DestType == MVT::f32 ? Alpha::IDEF_F32 : Alpha::IDEF_F64)
584 : Alpha::IDEF_I;
585 BuildMI(BB, Opc, 0, Result);
Andrew Lenharth7332f3e2005-04-02 19:11:07 +0000586 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 Lenharth4e629512005-12-24 05:36:33 +0000679 if (Address.getOpcode() == AlphaISD::GPRelLo) {
680 unsigned Hi = SelectExpr(Address.getOperand(1));
681 Address = Address.getOperand(0);
682 if (GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Address)) {
683 if (EnableAlphaLSMark)
684 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
685 .addImm(getUID());
686 BuildMI(BB, GetRelVersion(Opc), 2, Result)
687 .addGlobalAddress(GASD->getGlobal()).addReg(Hi);
688 } else if (ConstantPoolSDNode *CP =
689 dyn_cast<ConstantPoolSDNode>(Address)) {
690 unsigned CPIdx = BB->getParent()->getConstantPool()->
691 getConstantPoolIndex(CP->get());
692 has_sym = true;
693 if (EnableAlphaLSMark)
694 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
695 .addImm(getUID());
696 BuildMI(BB, GetRelVersion(Opc), 2, Result)
697 .addConstantPoolIndex(CPIdx).addReg(Tmp1);
698 } else assert(0 && "Unknown Lo part");
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000699 } else if(Address.getOpcode() == ISD::FrameIndex) {
700 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000701 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
702 .addImm(getUID());
Andrew Lenharth032f2352005-02-22 21:59:48 +0000703 BuildMI(BB, Opc, 2, Result)
704 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
705 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000706 } else {
707 long offset;
708 SelectAddr(Address, Tmp1, offset);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000709 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000710 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
711 .addImm(getUID());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000712 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
713 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000714 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000715 }
Andrew Lenharth4e629512005-12-24 05:36:33 +0000716 case AlphaISD::GlobalBaseReg:
717 AlphaLowering.restoreGP(BB);
718 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R29).addReg(Alpha::R29);
719 return Result;
720 case AlphaISD::GPRelHi:
721 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(0)))
722 BuildMI(BB, Alpha::LDAHr, 2, Result)
723 .addConstantPoolIndex(BB->getParent()->getConstantPool()->
724 getConstantPoolIndex(CP->get()))
725 .addReg(SelectExpr(N.getOperand(1)));
726 else if (GlobalAddressSDNode *GASD =
727 dyn_cast<GlobalAddressSDNode>(N.getOperand(0)))
728 BuildMI(BB, Alpha::LDAHr, 2, Result)
729 .addGlobalAddress(GASD->getGlobal())
730 .addReg(SelectExpr(N.getOperand(1)));
731 else assert(0 && "unknown Hi part");
732 return Result;
733 case AlphaISD::GPRelLo:
734 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(0)))
735 BuildMI(BB, Alpha::LDAr, 2, Result)
736 .addConstantPoolIndex(BB->getParent()->getConstantPool()->
737 getConstantPoolIndex(CP->get()))
738 .addReg(SelectExpr(N.getOperand(1)));
739 else if (GlobalAddressSDNode *GASD =
740 dyn_cast<GlobalAddressSDNode>(N.getOperand(0)))
741 BuildMI(BB, Alpha::LDAr, 2, Result)
742 .addGlobalAddress(GASD->getGlobal())
743 .addReg(SelectExpr(N.getOperand(1)));
744 else assert(0 && "unknown Lo part");
745 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000746
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000747 case ISD::GlobalAddress:
748 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000749 has_sym = true;
Jeff Cohen00b168892005-07-27 06:12:32 +0000750
Andrew Lenharth2f5bca52005-07-03 20:06:13 +0000751 Reg = Result = MakeReg(MVT::i64);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000752
753 if (EnableAlphaLSMark)
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000754 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000755 .addImm(getUID());
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000756
757 BuildMI(BB, Alpha::LDQl, 2, Result)
Andrew Lenharthc95d9842005-06-27 21:11:40 +0000758 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal())
759 .addReg(Alpha::R29);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000760 return Result;
761
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000762 case ISD::ExternalSymbol:
763 AlphaLowering.restoreGP(BB);
764 has_sym = true;
765
Andrew Lenharth2f5bca52005-07-03 20:06:13 +0000766 Reg = Result = MakeReg(MVT::i64);
767
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000768 if (EnableAlphaLSMark)
769 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
770 .addImm(getUID());
771
772 BuildMI(BB, Alpha::LDQl, 2, Result)
773 .addExternalSymbol(cast<ExternalSymbolSDNode>(N)->getSymbol())
774 .addReg(Alpha::R29);
775 return Result;
776
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +0000777 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000778 case ISD::CALL:
779 {
780 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000781
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000782 // The chain for this call is now lowered.
Andrew Lenharthf3f951a2005-07-22 20:50:29 +0000783 ExprMap[N.getValue(Node->getNumValues()-1)] = notIn;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000784
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000785 //grab the arguments
786 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000787 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000788 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000789 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000790
Andrew Lenharth684f2292005-01-30 00:35:27 +0000791 //in reg args
792 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000793 {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000794 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000795 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000796 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000797 Alpha::F19, Alpha::F20, Alpha::F21};
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::BIS, 2, args_int[i]).addReg(argvregs[i])
811 .addReg(argvregs[i]);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000812 break;
813 case MVT::f32:
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +0000814 BuildMI(BB, Alpha::CPYSS, 2, args_float[i]).addReg(argvregs[i])
815 .addReg(argvregs[i]);
816 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000817 case MVT::f64:
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +0000818 BuildMI(BB, Alpha::CPYST, 2, args_float[i]).addReg(argvregs[i])
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000819 .addReg(argvregs[i]);
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 Lenharth684f2292005-01-30 00:35:27 +0000823 //in mem args
824 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000825 {
826 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000827 default:
828 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000829 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000830 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000831 N.getOperand(i+2).getValueType() << "\n";
832 assert(0 && "Unknown value type for call");
833 case MVT::i1:
834 case MVT::i8:
835 case MVT::i16:
836 case MVT::i32:
837 case MVT::i64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000838 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
839 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000840 break;
841 case MVT::f32:
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000842 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
843 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000844 break;
845 case MVT::f64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000846 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
847 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000848 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000849 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000850 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000851 //build the right kind of call
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000852 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(N.getOperand(1));
853 if (GASD && !GASD->getGlobal()->isExternal()) {
854 //use PC relative branch call
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000855 AlphaLowering.restoreGP(BB);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000856 BuildMI(BB, Alpha::BSR, 1, Alpha::R26)
857 .addGlobalAddress(GASD->getGlobal(),true);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000858 } else {
859 //no need to restore GP as we are doing an indirect call
860 Tmp1 = SelectExpr(N.getOperand(1));
861 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
862 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
863 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000864
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000865 //push the result into a virtual register
Misha Brukman4633f1c2005-04-21 23:13:11 +0000866
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000867 switch (Node->getValueType(0)) {
868 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000869 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000870 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +0000871 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
872 break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000873 case MVT::f32:
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +0000874 BuildMI(BB, Alpha::CPYSS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
875 break;
876 case MVT::f64:
877 BuildMI(BB, Alpha::CPYST, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
Misha Brukman7847fca2005-04-22 17:54:37 +0000878 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000879 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000880 return Result+N.ResNo;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000881 }
882
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000883 case ISD::SIGN_EXTEND_INREG:
884 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000885 //do SDIV opt for all levels of ints if not dividing by a constant
886 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
887 && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +0000888 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +0000889 unsigned Tmp4 = MakeReg(MVT::f64);
890 unsigned Tmp5 = MakeReg(MVT::f64);
891 unsigned Tmp6 = MakeReg(MVT::f64);
892 unsigned Tmp7 = MakeReg(MVT::f64);
893 unsigned Tmp8 = MakeReg(MVT::f64);
894 unsigned Tmp9 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000895
896 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
897 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
898 MoveInt2FP(Tmp1, Tmp4, true);
899 MoveInt2FP(Tmp2, Tmp5, true);
Andrew Lenharth98169be2005-07-28 18:14:47 +0000900 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Alpha::F31).addReg(Tmp4);
901 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Alpha::F31).addReg(Tmp5);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +0000902 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
Andrew Lenharth98169be2005-07-28 18:14:47 +0000903 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Alpha::F31).addReg(Tmp8);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000904 MoveFP2Int(Tmp9, Result, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +0000905 return Result;
906 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000907
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000908 //Alpha has instructions for a bunch of signed 32 bit stuff
Chris Lattnerbce81ae2005-07-10 01:56:13 +0000909 if(cast<VTSDNode>(Node->getOperand(1))->getVT() == MVT::i32) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000910 switch (N.getOperand(0).getOpcode()) {
911 case ISD::ADD:
912 case ISD::SUB:
913 case ISD::MUL:
914 {
915 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
916 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
917 //FIXME: first check for Scaled Adds and Subs!
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000918 if(!isMul && N.getOperand(0).getOperand(0).getOpcode() == ISD::SHL &&
Andrew Lenharthd2284272005-08-15 14:31:37 +0000919 isSIntImmediateBounded(N.getOperand(0).getOperand(0).getOperand(1), SImm, 2, 3))
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000920 {
Andrew Lenharthd2284272005-08-15 14:31:37 +0000921 bool use4 = SImm == 2;
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000922 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
923 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
924 BuildMI(BB, isAdd?(use4?Alpha::S4ADDL:Alpha::S8ADDL):(use4?Alpha::S4SUBL:Alpha::S8SUBL),
925 2,Result).addReg(Tmp1).addReg(Tmp2);
926 }
927 else if(isAdd && N.getOperand(0).getOperand(1).getOpcode() == ISD::SHL &&
Andrew Lenharthd2284272005-08-15 14:31:37 +0000928 isSIntImmediateBounded(N.getOperand(0).getOperand(1).getOperand(1), SImm, 2, 3))
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000929 {
Andrew Lenharthd2284272005-08-15 14:31:37 +0000930 bool use4 = SImm == 2;
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000931 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
932 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
933 BuildMI(BB, use4?Alpha::S4ADDL:Alpha::S8ADDL, 2,Result).addReg(Tmp1).addReg(Tmp2);
934 }
Andrew Lenharthd2284272005-08-15 14:31:37 +0000935 else if(isSIntImmediateBounded(N.getOperand(0).getOperand(1), SImm, 0, 255))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000936 { //Normal imm add/sub
937 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000938 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +0000939 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000940 }
Andrew Lenharthd2284272005-08-15 14:31:37 +0000941 else if(!isMul && isSIntImmediate(N.getOperand(0).getOperand(1), SImm) &&
942 (((SImm << 32) >> 32) >= -255) && (((SImm << 32) >> 32) <= 0))
Andrew Lenharth6b137d82005-07-22 22:24:01 +0000943 { //handle canonicalization
944 Opc = isAdd ? Alpha::SUBLi : Alpha::ADDLi;
945 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +0000946 SImm = 0 - ((SImm << 32) >> 32);
947 assert(SImm >= 0 && SImm <= 255);
948 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharth6b137d82005-07-22 22:24:01 +0000949 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000950 else
951 { //Normal add/sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000952 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULL : Alpha::SUBL);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000953 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000954 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000955 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
956 }
957 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000958 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000959 default: break; //Fall Though;
960 }
961 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000962 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000963 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Chris Lattnerbce81ae2005-07-10 01:56:13 +0000964 switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000965 default:
966 Node->dump();
967 assert(0 && "Sign Extend InReg not there yet");
968 break;
969 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000970 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000971 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000972 break;
973 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000974 case MVT::i16:
Andrew Lenharth964b6aa2005-10-20 19:39:24 +0000975 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000976 break;
977 case MVT::i8:
Andrew Lenharth964b6aa2005-10-20 19:39:24 +0000978 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000979 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +0000980 case MVT::i1:
981 Tmp2 = MakeReg(MVT::i64);
982 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000983 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +0000984 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000985 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000986 return Result;
987 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000988
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000989 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000990 {
Chris Lattner88ac32c2005-08-09 20:21:10 +0000991 ISD::CondCode CC = cast<CondCodeSDNode>(N.getOperand(2))->get();
992 if (MVT::isInteger(N.getOperand(0).getValueType())) {
993 bool isConst = false;
994 int dir;
Misha Brukman7847fca2005-04-22 17:54:37 +0000995
Chris Lattner88ac32c2005-08-09 20:21:10 +0000996 //Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +0000997 if(isSIntImmediate(N.getOperand(1), SImm) && SImm <= 255 && SImm >= 0)
Chris Lattner88ac32c2005-08-09 20:21:10 +0000998 isConst = true;
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000999
Chris Lattner88ac32c2005-08-09 20:21:10 +00001000 switch (CC) {
1001 default: Node->dump(); assert(0 && "Unknown integer comparison!");
1002 case ISD::SETEQ:
1003 Opc = isConst ? Alpha::CMPEQi : Alpha::CMPEQ; dir=1; break;
1004 case ISD::SETLT:
1005 Opc = isConst ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
1006 case ISD::SETLE:
1007 Opc = isConst ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
1008 case ISD::SETGT: Opc = Alpha::CMPLT; dir = 2; break;
1009 case ISD::SETGE: Opc = Alpha::CMPLE; dir = 2; break;
1010 case ISD::SETULT:
1011 Opc = isConst ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
1012 case ISD::SETUGT: Opc = Alpha::CMPULT; dir = 2; break;
1013 case ISD::SETULE:
1014 Opc = isConst ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
1015 case ISD::SETUGE: Opc = Alpha::CMPULE; dir = 2; break;
1016 case ISD::SETNE: {//Handle this one special
1017 //std::cerr << "Alpha does not have a setne.\n";
1018 //abort();
1019 Tmp1 = SelectExpr(N.getOperand(0));
1020 Tmp2 = SelectExpr(N.getOperand(1));
1021 Tmp3 = MakeReg(MVT::i64);
1022 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1023 //Remeber we have the Inv for this CC
1024 CCInvMap[N] = Tmp3;
1025 //and invert
1026 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
1027 return Result;
1028 }
1029 }
1030 if (dir == 1) {
1031 Tmp1 = SelectExpr(N.getOperand(0));
1032 if (isConst) {
Andrew Lenharthd2284272005-08-15 14:31:37 +00001033 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Chris Lattner88ac32c2005-08-09 20:21:10 +00001034 } else {
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001035 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth694c2982005-06-26 23:01:11 +00001036 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001037 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001038 } else { //if (dir == 2) {
1039 Tmp1 = SelectExpr(N.getOperand(1));
1040 Tmp2 = SelectExpr(N.getOperand(0));
1041 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001042 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001043 } else {
1044 //do the comparison
1045 Tmp1 = MakeReg(MVT::f64);
1046 bool inv = SelectFPSetCC(N, Tmp1);
1047
1048 //now arrange for Result (int) to have a 1 or 0
1049 Tmp2 = MakeReg(MVT::i64);
1050 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
1051 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
1052 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
Andrew Lenharth9818c052005-02-05 13:19:12 +00001053 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001054 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001055 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001056
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001057 case ISD::CopyFromReg:
1058 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001059 ++count_ins;
1060
Andrew Lenharth40831c52005-01-28 06:57:18 +00001061 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001062 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001063 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001064 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001065 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001066
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001067 SDOperand Chain = N.getOperand(0);
1068
1069 Select(Chain);
Chris Lattner707ebc52005-08-16 21:56:37 +00001070 unsigned r = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001071 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001072 switch(N.getValue(0).getValueType()) {
1073 case MVT::f32:
1074 BuildMI(BB, Alpha::CPYSS, 2, Result).addReg(r).addReg(r);
1075 break;
1076 case MVT::f64:
1077 BuildMI(BB, Alpha::CPYST, 2, Result).addReg(r).addReg(r);
1078 break;
1079 default:
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001080 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001081 break;
1082 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001083 return Result;
1084 }
1085
Misha Brukman4633f1c2005-04-21 23:13:11 +00001086 //Most of the plain arithmetic and logic share the same form, and the same
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001087 //constant immediate test
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001088 case ISD::XOR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001089 //Match Not
Andrew Lenharthd2284272005-08-15 14:31:37 +00001090 if (isSIntImmediate(N.getOperand(1), SImm) && SImm == -1) {
1091 Tmp1 = SelectExpr(N.getOperand(0));
1092 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1093 return Result;
1094 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001095 //Fall through
1096 case ISD::AND:
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001097 //handle zap
Andrew Lenharthd2284272005-08-15 14:31:37 +00001098 if (opcode == ISD::AND && isUIntImmediate(N.getOperand(1), UImm))
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001099 {
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001100 unsigned int build = 0;
1101 for(int i = 0; i < 8; ++i)
1102 {
Andrew Lenharthd2284272005-08-15 14:31:37 +00001103 if ((UImm & 0x00FF) == 0x00FF)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001104 build |= 1 << i;
Andrew Lenharthd2284272005-08-15 14:31:37 +00001105 else if ((UImm & 0x00FF) != 0)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001106 { build = 0; break; }
Andrew Lenharthd2284272005-08-15 14:31:37 +00001107 UImm >>= 8;
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001108 }
1109 if (build)
1110 {
1111 Tmp1 = SelectExpr(N.getOperand(0));
1112 BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1113 return Result;
1114 }
1115 }
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001116 case ISD::OR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001117 //Check operand(0) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001118 if (N.getOperand(0).getOpcode() == ISD::XOR &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001119 isSIntImmediate(N.getOperand(0).getOperand(1), SImm) && SImm == -1) {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001120 switch(opcode) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001121 case ISD::AND: Opc = Alpha::BIC; break;
1122 case ISD::OR: Opc = Alpha::ORNOT; break;
1123 case ISD::XOR: Opc = Alpha::EQV; break;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001124 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001125 Tmp1 = SelectExpr(N.getOperand(1));
1126 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1127 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1128 return Result;
1129 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001130 //Check operand(1) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001131 if (N.getOperand(1).getOpcode() == ISD::XOR &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001132 isSIntImmediate(N.getOperand(1).getOperand(1), SImm) && SImm == -1) {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001133 switch(opcode) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001134 case ISD::AND: Opc = Alpha::BIC; break;
1135 case ISD::OR: Opc = Alpha::ORNOT; break;
1136 case ISD::XOR: Opc = Alpha::EQV; break;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001137 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001138 Tmp1 = SelectExpr(N.getOperand(0));
1139 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1140 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1141 return Result;
1142 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001143 //Fall through
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001144 case ISD::SHL:
1145 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001146 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001147 case ISD::MUL:
Andrew Lenharthd2284272005-08-15 14:31:37 +00001148 if(isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255)) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001149 switch(opcode) {
1150 case ISD::AND: Opc = Alpha::ANDi; break;
1151 case ISD::OR: Opc = Alpha::BISi; break;
1152 case ISD::XOR: Opc = Alpha::XORi; break;
1153 case ISD::SHL: Opc = Alpha::SLi; break;
1154 case ISD::SRL: Opc = Alpha::SRLi; break;
1155 case ISD::SRA: Opc = Alpha::SRAi; break;
1156 case ISD::MUL: Opc = Alpha::MULQi; break;
1157 };
1158 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001159 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001160 } else {
1161 switch(opcode) {
1162 case ISD::AND: Opc = Alpha::AND; break;
1163 case ISD::OR: Opc = Alpha::BIS; break;
1164 case ISD::XOR: Opc = Alpha::XOR; break;
1165 case ISD::SHL: Opc = Alpha::SL; break;
1166 case ISD::SRL: Opc = Alpha::SRL; break;
1167 case ISD::SRA: Opc = Alpha::SRA; break;
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001168 case ISD::MUL: Opc = Alpha::MULQ; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001169 };
1170 Tmp1 = SelectExpr(N.getOperand(0));
1171 Tmp2 = SelectExpr(N.getOperand(1));
1172 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1173 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001174 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001175
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001176 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001177 case ISD::SUB:
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001178 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001179 bool isAdd = opcode == ISD::ADD;
1180
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001181 //first check for Scaled Adds and Subs!
1182 //Valid for add and sub
Andrew Lenharthd2284272005-08-15 14:31:37 +00001183 if(N.getOperand(0).getOpcode() == ISD::SHL &&
1184 isSIntImmediate(N.getOperand(0).getOperand(1), SImm) &&
1185 (SImm == 2 || SImm == 3)) {
1186 bool use4 = SImm == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001187 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001188 if (isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255))
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001189 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
Andrew Lenharthd2284272005-08-15 14:31:37 +00001190 2, Result).addReg(Tmp2).addImm(SImm);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001191 else {
1192 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001193 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1194 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001195 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001196 }
1197 //Position prevents subs
Andrew Lenharth273a1f92005-04-07 14:18:13 +00001198 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001199 isSIntImmediate(N.getOperand(1).getOperand(1), SImm) &&
1200 (SImm == 2 || SImm == 3)) {
1201 bool use4 = SImm == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001202 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001203 if (isSIntImmediateBounded(N.getOperand(0), SImm, 0, 255))
1204 BuildMI(BB, use4?Alpha::S4ADDQi:Alpha::S8ADDQi, 2, Result).addReg(Tmp2).addImm(SImm);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001205 else {
1206 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001207 BuildMI(BB, use4?Alpha::S4ADDQ:Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001208 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001209 }
1210 //small addi
Andrew Lenharthd2284272005-08-15 14:31:37 +00001211 else if(isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001212 { //Normal imm add/sub
1213 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1214 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001215 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001216 }
Andrew Lenharthd2284272005-08-15 14:31:37 +00001217 else if(isSIntImmediateBounded(N.getOperand(1), SImm, -255, 0))
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001218 { //inverted imm add/sub
1219 Opc = isAdd ? Alpha::SUBQi : Alpha::ADDQi;
1220 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001221 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(-SImm);
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001222 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001223 //larger addi
Andrew Lenharthd2284272005-08-15 14:31:37 +00001224 else if(isSIntImmediateBounded(N.getOperand(1), SImm, -32767, 32767))
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001225 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001226 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001227 if (!isAdd)
Andrew Lenharthd2284272005-08-15 14:31:37 +00001228 SImm = -SImm;
1229 BuildMI(BB, Alpha::LDA, 2, Result).addImm(SImm).addReg(Tmp1);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001230 }
1231 //give up and do the operation
1232 else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001233 //Normal add/sub
1234 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1235 Tmp1 = SelectExpr(N.getOperand(0));
1236 Tmp2 = SelectExpr(N.getOperand(1));
1237 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1238 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001239 return Result;
1240 }
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001241 case ISD::FADD:
1242 case ISD::FSUB:
1243 case ISD::FMUL:
1244 case ISD::FDIV: {
1245 if (opcode == ISD::FADD)
1246 Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS;
1247 else if (opcode == ISD::FSUB)
1248 Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS;
1249 else if (opcode == ISD::FMUL)
1250 Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS;
1251 else
1252 Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS;
1253 Tmp1 = SelectExpr(N.getOperand(0));
1254 Tmp2 = SelectExpr(N.getOperand(1));
1255 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1256 return Result;
1257 }
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001258 case ISD::SDIV:
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001259 {
Andrew Lenhartha565c272005-04-06 22:03:13 +00001260 //check if we can convert into a shift!
Andrew Lenharthd2284272005-08-15 14:31:37 +00001261 if (isSIntImmediate(N.getOperand(1), SImm) &&
1262 SImm != 0 && isPowerOf2_64(llabs(SImm))) {
1263 unsigned k = Log2_64(llabs(SImm));
Andrew Lenhartha565c272005-04-06 22:03:13 +00001264 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenhartha565c272005-04-06 22:03:13 +00001265 if (k == 1)
1266 Tmp2 = Tmp1;
1267 else
1268 {
1269 Tmp2 = MakeReg(MVT::i64);
1270 BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
1271 }
1272 Tmp3 = MakeReg(MVT::i64);
1273 BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
1274 unsigned Tmp4 = MakeReg(MVT::i64);
1275 BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharthd2284272005-08-15 14:31:37 +00001276 if (SImm > 0)
Andrew Lenhartha565c272005-04-06 22:03:13 +00001277 BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
1278 else
1279 {
1280 unsigned Tmp5 = MakeReg(MVT::i64);
1281 BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
1282 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
1283 }
1284 return Result;
1285 }
1286 }
1287 //Else fall through
Andrew Lenhartha565c272005-04-06 22:03:13 +00001288 case ISD::UDIV:
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001289 //else fall though
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001290 case ISD::UREM:
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001291 case ISD::SREM: {
1292 const char* opstr = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001293 switch(opcode) {
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001294 case ISD::UREM: opstr = "__remqu"; break;
1295 case ISD::SREM: opstr = "__remq"; break;
1296 case ISD::UDIV: opstr = "__divqu"; break;
1297 case ISD::SDIV: opstr = "__divq"; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001298 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001299 Tmp1 = SelectExpr(N.getOperand(0));
1300 Tmp2 = SelectExpr(N.getOperand(1));
Jeff Cohen00b168892005-07-27 06:12:32 +00001301 SDOperand Addr =
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001302 ISelDAG->getExternalSymbol(opstr, AlphaLowering.getPointerTy());
1303 Tmp3 = SelectExpr(Addr);
Andrew Lenharth33819132005-03-04 20:09:23 +00001304 //set up regs explicitly (helps Reg alloc)
1305 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001306 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001307 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp3).addReg(Tmp3);
1308 BuildMI(BB, Alpha::JSRs, 2, Alpha::R23).addReg(Alpha::R27).addImm(0);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001309 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001310 return Result;
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001311 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001312
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001313 case ISD::SELECT:
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001314 if (isFP) {
1315 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1316 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
1317 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
1318
1319 SDOperand CC = N.getOperand(0);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001320
Chris Lattner88ac32c2005-08-09 20:21:10 +00001321 if (CC.getOpcode() == ISD::SETCC &&
1322 !MVT::isInteger(CC.getOperand(0).getValueType())) {
1323 //FP Setcc -> Select yay!
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001324
Jeff Cohen00b168892005-07-27 06:12:32 +00001325
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001326 //for a cmp b: c = a - b;
1327 //a = b: c = 0
1328 //a < b: c < 0
1329 //a > b: c > 0
1330
1331 bool invTest = false;
1332 unsigned Tmp3;
Andrew Lenharthb2156f92005-11-30 17:11:20 +00001333 bool isD = CC.getOperand(0).getValueType() == MVT::f64;
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001334 ConstantFPSDNode *CN;
Chris Lattner88ac32c2005-08-09 20:21:10 +00001335 if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(1)))
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001336 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
Chris Lattner88ac32c2005-08-09 20:21:10 +00001337 Tmp3 = SelectExpr(CC.getOperand(0));
1338 else if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(0)))
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001339 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1340 {
Chris Lattner88ac32c2005-08-09 20:21:10 +00001341 Tmp3 = SelectExpr(CC.getOperand(1));
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001342 invTest = true;
1343 }
1344 else
1345 {
Chris Lattner88ac32c2005-08-09 20:21:10 +00001346 unsigned Tmp1 = SelectExpr(CC.getOperand(0));
1347 unsigned Tmp2 = SelectExpr(CC.getOperand(1));
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001348 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1349 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1350 .addReg(Tmp1).addReg(Tmp2);
1351 }
1352
Andrew Lenharthb2156f92005-11-30 17:11:20 +00001353 if(isD)
1354 switch (cast<CondCodeSDNode>(CC.getOperand(2))->get()) {
1355 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
1356 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNET : Alpha::FCMOVEQT; break;
1357 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGTT : Alpha::FCMOVLTT; break;
1358 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGET : Alpha::FCMOVLET; break;
1359 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLTT : Alpha::FCMOVGTT; break;
1360 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLET : Alpha::FCMOVGET; break;
1361 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQT : Alpha::FCMOVNET; break;
1362 }
1363 else
1364 switch (cast<CondCodeSDNode>(CC.getOperand(2))->get()) {
1365 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
1366 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNES : Alpha::FCMOVEQS; break;
1367 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGTS : Alpha::FCMOVLTS; break;
1368 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGES : Alpha::FCMOVLES; break;
1369 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLTS : Alpha::FCMOVGTS; break;
1370 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLES : Alpha::FCMOVGES; break;
1371 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQS : Alpha::FCMOVNES; break;
1372 }
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001373 BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
1374 return Result;
1375 }
1376 else
1377 {
1378 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1379 BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV)
1380 .addReg(Tmp1);
1381// // Spill the cond to memory and reload it from there.
1382// unsigned Tmp4 = MakeReg(MVT::f64);
1383// MoveIntFP(Tmp1, Tmp4, true);
1384// //now ideally, we don't have to do anything to the flag...
1385// // Get the condition into the zero flag.
1386// BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
1387 return Result;
Jeff Cohen00b168892005-07-27 06:12:32 +00001388 }
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001389 } else {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001390 //FIXME: look at parent to decide if intCC can be folded, or if setCC(FP)
1391 //and can save stack use
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001392 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001393 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1394 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001395 // Get the condition into the zero flag.
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001396 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001397
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001398 SDOperand CC = N.getOperand(0);
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001399
Misha Brukman4633f1c2005-04-21 23:13:11 +00001400 if (CC.getOpcode() == ISD::SETCC &&
Chris Lattner88ac32c2005-08-09 20:21:10 +00001401 !MVT::isInteger(CC.getOperand(0).getValueType()))
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001402 { //FP Setcc -> Int Select
Misha Brukman7847fca2005-04-22 17:54:37 +00001403 Tmp1 = MakeReg(MVT::f64);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001404 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1405 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Misha Brukman7847fca2005-04-22 17:54:37 +00001406 bool inv = SelectFPSetCC(CC, Tmp1);
1407 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
1408 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
1409 return Result;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001410 }
1411 if (CC.getOpcode() == ISD::SETCC) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001412 //Int SetCC -> Select
1413 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharthd2284272005-08-15 14:31:37 +00001414 if(isSIntImmediateBounded(CC.getOperand(1), SImm, 0, 0)) {
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001415 //figure out a few things
Andrew Lenharthd2284272005-08-15 14:31:37 +00001416 bool useImm = isSIntImmediateBounded(N.getOperand(2), SImm, 0, 255);
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001417
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001418 //Fix up CC
Chris Lattner88ac32c2005-08-09 20:21:10 +00001419 ISD::CondCode cCode= cast<CondCodeSDNode>(CC.getOperand(2))->get();
Andrew Lenharth694c2982005-06-26 23:01:11 +00001420 if (useImm) //Invert sense to get Imm field right
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001421 cCode = ISD::getSetCCInverse(cCode, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001422
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001423 //Choose the CMOV
1424 switch (cCode) {
1425 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001426 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
1427 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
1428 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
1429 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
1430 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
1431 case ISD::SETULT: assert(0 && "unsigned < 0 is never true"); break;
1432 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
1433 //Technically you could have this CC
1434 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
1435 case ISD::SETUGE: assert(0 && "unsgined >= 0 is always true"); break;
1436 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001437 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001438 Tmp1 = SelectExpr(CC.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001439
Andrew Lenharth694c2982005-06-26 23:01:11 +00001440 if (useImm) {
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001441 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
Andrew Lenharthd2284272005-08-15 14:31:37 +00001442 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addImm(SImm).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001443 } else {
1444 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1445 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1446 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
1447 }
1448 return Result;
1449 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001450 //Otherwise, fall though
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001451 }
1452 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001453 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1454 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001455 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3)
1456 .addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001457
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001458 return Result;
1459 }
1460
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001461 case ISD::Constant:
1462 {
Andrew Lenharthc0513832005-03-29 19:24:04 +00001463 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001464 int zero_extend_top = 0;
Andrew Lenharthf075cac2005-07-23 07:46:48 +00001465 if (val > 0 && (val & 0xFFFFFFFF00000000ULL) == 0 &&
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001466 ((int32_t)val < 0)) {
1467 //try a small load and zero extend
1468 val = (int32_t)val;
1469 zero_extend_top = 15;
1470 }
1471
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00001472 if (val <= IMM_HIGH && val >= IMM_LOW) {
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001473 if(!zero_extend_top)
1474 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
1475 else {
1476 Tmp1 = MakeReg(MVT::i64);
1477 BuildMI(BB, Alpha::LDA, 2, Tmp1).addImm(val).addReg(Alpha::R31);
1478 BuildMI(BB, Alpha::ZAPNOT, 2, Result).addReg(Tmp1).addImm(zero_extend_top);
1479 }
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00001480 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001481 else if (val <= (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT &&
1482 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
1483 Tmp1 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001484 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val))
1485 .addReg(Alpha::R31);
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001486 if (!zero_extend_top)
1487 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
1488 else {
1489 Tmp3 = MakeReg(MVT::i64);
1490 BuildMI(BB, Alpha::LDA, 2, Tmp3).addImm(getLower16(val)).addReg(Tmp1);
1491 BuildMI(BB, Alpha::ZAPNOT, 2, Result).addReg(Tmp3).addImm(zero_extend_top);
1492 }
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00001493 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001494 else {
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001495 //re-get the val since we are going to mem anyway
1496 val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001497 MachineConstantPool *CP = BB->getParent()->getConstantPool();
Jeff Cohen00b168892005-07-27 06:12:32 +00001498 ConstantUInt *C =
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001499 ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001500 unsigned CPI = CP->getConstantPoolIndex(C);
1501 AlphaLowering.restoreGP(BB);
Andrew Lenharthfe895e32005-06-27 17:15:36 +00001502 has_sym = true;
1503 Tmp1 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001504 BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CPI)
1505 .addReg(Alpha::R29);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001506 if (EnableAlphaLSMark)
1507 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
1508 .addImm(getUID());
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001509 BuildMI(BB, Alpha::LDQr, 2, Result).addConstantPoolIndex(CPI)
1510 .addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001511 }
1512 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001513 }
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001514 case ISD::FNEG:
1515 if(ISD::FABS == N.getOperand(0).getOpcode())
1516 {
1517 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001518 BuildMI(BB, DestType == MVT::f64 ? Alpha::CPYSNT : Alpha::CPYSNS,
1519 2, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001520 } else {
1521 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001522 BuildMI(BB, DestType == MVT::f64 ? Alpha::CPYSNT : Alpha::CPYSNS
1523 , 2, Result).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001524 }
1525 return Result;
1526
1527 case ISD::FABS:
1528 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001529 BuildMI(BB, DestType == MVT::f64 ? Alpha::CPYST : Alpha::CPYSS, 2, Result)
1530 .addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001531 return Result;
1532
1533 case ISD::FP_ROUND:
1534 assert (DestType == MVT::f32 &&
1535 N.getOperand(0).getValueType() == MVT::f64 &&
1536 "only f64 to f32 conversion supported here");
1537 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthb921f1b2005-11-11 23:08:46 +00001538 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001539 return Result;
1540
1541 case ISD::FP_EXTEND:
1542 assert (DestType == MVT::f64 &&
1543 N.getOperand(0).getValueType() == MVT::f32 &&
1544 "only f32 to f64 conversion supported here");
1545 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthc2c64fd2005-11-11 19:52:25 +00001546 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001547 return Result;
1548
1549 case ISD::ConstantFP:
1550 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
1551 if (CN->isExactlyValue(+0.0)) {
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001552 BuildMI(BB, DestType == MVT::f64 ? Alpha::CPYST : Alpha::CPYSS
1553 , 2, Result).addReg(Alpha::F31)
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001554 .addReg(Alpha::F31);
1555 } else if ( CN->isExactlyValue(-0.0)) {
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001556 BuildMI(BB, DestType == MVT::f64 ? Alpha::CPYSNT : Alpha::CPYSNS,
1557 2, Result).addReg(Alpha::F31)
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001558 .addReg(Alpha::F31);
1559 } else {
1560 abort();
1561 }
1562 }
1563 return Result;
1564
Andrew Lenharth7f0db912005-11-30 07:19:56 +00001565 case AlphaISD::CVTQT_:
Andrew Lenharth6251b362005-12-01 17:48:51 +00001566 Tmp1 = SelectExpr(N.getOperand(0));
1567 BuildMI(BB, Alpha::CVTQT, 1, Result).addReg(Tmp1);
Andrew Lenharth7f0db912005-11-30 07:19:56 +00001568 return Result;
1569
1570 case AlphaISD::CVTQS_:
Andrew Lenharth6251b362005-12-01 17:48:51 +00001571 Tmp1 = SelectExpr(N.getOperand(0));
1572 BuildMI(BB, Alpha::CVTQS, 1, Result).addReg(Tmp1);
Andrew Lenharth7f0db912005-11-30 07:19:56 +00001573 return Result;
1574
Andrew Lenharthcd804962005-11-30 16:10:29 +00001575 case AlphaISD::CVTTQ_:
Andrew Lenharth6251b362005-12-01 17:48:51 +00001576 Tmp1 = SelectExpr(N.getOperand(0));
1577 BuildMI(BB, Alpha::CVTTQ, 1, Result).addReg(Tmp1);
Andrew Lenharthcd804962005-11-30 16:10:29 +00001578 return Result;
1579
Andrew Lenharth7f0db912005-11-30 07:19:56 +00001580 case AlphaISD::ITOFT_:
Andrew Lenharth6251b362005-12-01 17:48:51 +00001581 Tmp1 = SelectExpr(N.getOperand(0));
1582 BuildMI(BB, Alpha::ITOFT, 1, Result).addReg(Tmp1);
Andrew Lenharth7f0db912005-11-30 07:19:56 +00001583 return Result;
Andrew Lenharthf71df332005-09-04 06:12:19 +00001584
Andrew Lenharthcd804962005-11-30 16:10:29 +00001585 case AlphaISD::FTOIT_:
Andrew Lenharth6251b362005-12-01 17:48:51 +00001586 Tmp1 = SelectExpr(N.getOperand(0));
1587 BuildMI(BB, Alpha::FTOIT, 1, Result).addReg(Tmp1);
Andrew Lenharthcd804962005-11-30 16:10:29 +00001588 return Result;
1589
Andrew Lenharthf71df332005-09-04 06:12:19 +00001590 case ISD::AssertSext:
1591 case ISD::AssertZext:
1592 return SelectExpr(N.getOperand(0));
1593
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001594 }
1595
1596 return 0;
1597}
1598
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001599void AlphaISel::Select(SDOperand N) {
Andrew Lenharth4052f022005-11-22 20:59:00 +00001600 unsigned Tmp1, Tmp2, Opc = Alpha::WTF;
Andrew Lenharth760270d2005-02-07 23:02:23 +00001601 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001602
Nate Begeman85fdeb22005-03-24 04:39:54 +00001603 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001604 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001605
1606 SDNode *Node = N.Val;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001607
Andrew Lenharth760270d2005-02-07 23:02:23 +00001608 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001609
1610 default:
1611 Node->dump(); std::cerr << "\n";
1612 assert(0 && "Node not handled yet!");
1613
1614 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00001615 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001616 return;
1617 }
1618
1619 case ISD::BR: {
1620 MachineBasicBlock *Dest =
1621 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1622
1623 Select(N.getOperand(0));
1624 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
1625 return;
1626 }
1627
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001628 case ISD::EntryToken: return; // Noop
1629
1630 case ISD::TokenFactor:
1631 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1632 Select(Node->getOperand(i));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001633
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001634 //N.Val->dump(); std::cerr << "\n";
1635 //assert(0 && "Node not handled yet!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001636
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001637 return;
1638
1639 case ISD::CopyToReg:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001640 ++count_outs;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001641 Select(N.getOperand(0));
Chris Lattner707ebc52005-08-16 21:56:37 +00001642 Tmp1 = SelectExpr(N.getOperand(2));
1643 Tmp2 = cast<RegisterSDNode>(N.getOperand(1))->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001644
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001645 if (Tmp1 != Tmp2) {
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001646 switch(N.getOperand(2).getValueType()) {
1647 case MVT::f64:
1648 BuildMI(BB, Alpha::CPYST, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1649 break;
1650 case MVT::f32:
1651 BuildMI(BB, Alpha::CPYSS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1652 break;
1653 default:
Andrew Lenharth29219162005-02-07 06:31:44 +00001654 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001655 break;
1656 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001657 }
1658 return;
1659
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001660 case ISD::RET:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001661 ++count_outs;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001662 switch (N.getNumOperands()) {
1663 default:
1664 std::cerr << N.getNumOperands() << "\n";
1665 for (unsigned i = 0; i < N.getNumOperands(); ++i)
1666 std::cerr << N.getOperand(i).getValueType() << "\n";
1667 Node->dump();
1668 assert(0 && "Unknown return instruction!");
1669 case 2:
1670 Select(N.getOperand(0));
1671 Tmp1 = SelectExpr(N.getOperand(1));
1672 switch (N.getOperand(1).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001673 default: Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001674 assert(0 && "All other types should have been promoted!!");
1675 case MVT::f64:
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001676 BuildMI(BB, Alpha::CPYST, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
1677 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001678 case MVT::f32:
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001679 BuildMI(BB, Alpha::CPYSS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001680 break;
1681 case MVT::i32:
1682 case MVT::i64:
1683 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
1684 break;
1685 }
1686 break;
1687 case 1:
1688 Select(N.getOperand(0));
1689 break;
1690 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001691 // Just emit a 'ret' instruction
Andrew Lenharth6968bff2005-06-27 23:24:11 +00001692 AlphaLowering.restoreRA(BB);
Andrew Lenharthf3f951a2005-07-22 20:50:29 +00001693 BuildMI(BB, Alpha::RET, 2, Alpha::R31).addReg(Alpha::R26).addImm(1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001694 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001695
Misha Brukman4633f1c2005-04-21 23:13:11 +00001696 case ISD::TRUNCSTORE:
1697 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001698 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001699 SDOperand Chain = N.getOperand(0);
1700 SDOperand Value = N.getOperand(1);
1701 SDOperand Address = N.getOperand(2);
1702 Select(Chain);
1703
1704 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00001705
1706 if (opcode == ISD::STORE) {
1707 switch(Value.getValueType()) {
Andrew Lenharth4052f022005-11-22 20:59:00 +00001708 default: assert(0 && "unknown Type in store");
Andrew Lenharth760270d2005-02-07 23:02:23 +00001709 case MVT::i64: Opc = Alpha::STQ; break;
1710 case MVT::f64: Opc = Alpha::STT; break;
1711 case MVT::f32: Opc = Alpha::STS; break;
1712 }
1713 } else { //ISD::TRUNCSTORE
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001714 switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) {
Andrew Lenharth4052f022005-11-22 20:59:00 +00001715 default: assert(0 && "unknown Type in store");
Andrew Lenharth760270d2005-02-07 23:02:23 +00001716 case MVT::i8: Opc = Alpha::STB; break;
1717 case MVT::i16: Opc = Alpha::STW; break;
1718 case MVT::i32: Opc = Alpha::STL; break;
1719 }
Andrew Lenharth65838902005-02-06 16:22:15 +00001720 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00001721
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001722 int i, j, k;
Jeff Cohen00b168892005-07-27 06:12:32 +00001723 if (EnableAlphaLSMark)
1724 getValueInfo(cast<SrcValueSDNode>(N.getOperand(3))->getValue(),
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001725 i, j, k);
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001726
Andrew Lenharth4e629512005-12-24 05:36:33 +00001727 if (Address.getOpcode() == AlphaISD::GPRelLo) {
1728 unsigned Hi = SelectExpr(Address.getOperand(1));
1729 Address = Address.getOperand(0);
1730 if (GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Address)) {
1731 if (EnableAlphaLSMark)
1732 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1733 .addImm(getUID());
1734 BuildMI(BB, GetRelVersion(Opc), 3).addReg(Tmp1)
1735 .addGlobalAddress(GASD->getGlobal()).addReg(Hi);
1736 } else assert(0 && "Unknown Lo part");
Andrew Lenharthfce587e2005-06-29 00:39:17 +00001737 } else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001738 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001739 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1740 .addImm(getUID());
Andrew Lenharth032f2352005-02-22 21:59:48 +00001741 BuildMI(BB, Opc, 3).addReg(Tmp1)
1742 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1743 .addReg(Alpha::F31);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001744 } else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001745 long offset;
1746 SelectAddr(Address, Tmp2, offset);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001747 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001748 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1749 .addImm(getUID());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001750 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1751 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001752 return;
1753 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001754
1755 case ISD::EXTLOAD:
1756 case ISD::SEXTLOAD:
1757 case ISD::ZEXTLOAD:
1758 case ISD::LOAD:
1759 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001760 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001761 case ISD::CALL:
Andrew Lenharth82a698c2005-11-12 19:04:09 +00001762 case ISD::READCYCLECOUNTER:
Andrew Lenharth032f2352005-02-22 21:59:48 +00001763 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001764 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001765 SelectExpr(N);
1766 return;
1767
Chris Lattner16cd04d2005-05-12 23:24:06 +00001768 case ISD::CALLSEQ_START:
1769 case ISD::CALLSEQ_END:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001770 Select(N.getOperand(0));
1771 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001772
Chris Lattner16cd04d2005-05-12 23:24:06 +00001773 Opc = N.getOpcode() == ISD::CALLSEQ_START ? Alpha::ADJUSTSTACKDOWN :
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001774 Alpha::ADJUSTSTACKUP;
1775 BuildMI(BB, Opc, 1).addImm(Tmp1);
1776 return;
Andrew Lenharth95762122005-03-31 21:24:06 +00001777
1778 case ISD::PCMARKER:
1779 Select(N.getOperand(0)); //Chain
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001780 BuildMI(BB, Alpha::PCLABEL, 2)
1781 .addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
Andrew Lenharth95762122005-03-31 21:24:06 +00001782 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001783 }
1784 assert(0 && "Should not be reached!");
1785}
1786
1787
1788/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
1789/// into a machine code representation using pattern matching and a machine
1790/// description file.
1791///
1792FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001793 return new AlphaISel(TM);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001794}
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001795