blob: 6c2e3a6f53d4ac63a4b4371cb9d49114bc6c7da2 [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)
368 {
369 //assert(0 && "Setcc On float?\n");
370 std::cerr << "Setcc on float!\n";
371 Tmp3 = MakeReg(MVT::f64);
Andrew Lenharth6467dfa2005-11-12 19:06:28 +0000372 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp1);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000373 Tmp1 = Tmp3;
374 }
375 if (SetCC->getOperand(1).getValueType() == MVT::f32)
376 {
377 //assert (0 && "Setcc On float?\n");
378 std::cerr << "Setcc on float!\n";
379 Tmp3 = MakeReg(MVT::f64);
Andrew Lenharth6467dfa2005-11-12 19:06:28 +0000380 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Tmp2);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000381 Tmp2 = Tmp3;
382 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000383
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000384 if (rev) std::swap(Tmp1, Tmp2);
385 //do the comparison
386 BuildMI(BB, Opc, 2, dst).addReg(Tmp1).addReg(Tmp2);
387 return inv;
388}
389
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000390//Check to see if the load is a constant offset from a base register
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000391void AlphaISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000392{
393 unsigned opcode = N.getOpcode();
Andrew Lenharth694c2982005-06-26 23:01:11 +0000394 if (opcode == ISD::ADD && N.getOperand(1).getOpcode() == ISD::Constant &&
395 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
396 { //Normal imm add
397 Reg = SelectExpr(N.getOperand(0));
398 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
399 return;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000400 }
401 Reg = SelectExpr(N);
402 offset = 0;
403 return;
404}
405
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000406void AlphaISel::SelectBranchCC(SDOperand N)
Andrew Lenharth445171a2005-02-08 00:40:03 +0000407{
408 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000409 MachineBasicBlock *Dest =
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000410 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
411 unsigned Opc = Alpha::WTF;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000412
Andrew Lenharth445171a2005-02-08 00:40:03 +0000413 Select(N.getOperand(0)); //chain
414 SDOperand CC = N.getOperand(1);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000415
Andrew Lenharth445171a2005-02-08 00:40:03 +0000416 if (CC.getOpcode() == ISD::SETCC)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000417 {
Chris Lattner88ac32c2005-08-09 20:21:10 +0000418 ISD::CondCode cCode= cast<CondCodeSDNode>(CC.getOperand(2))->get();
419 if (MVT::isInteger(CC.getOperand(0).getValueType())) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000420 //Dropping the CC is only useful if we are comparing to 0
Chris Lattner88ac32c2005-08-09 20:21:10 +0000421 bool RightZero = CC.getOperand(1).getOpcode() == ISD::Constant &&
422 cast<ConstantSDNode>(CC.getOperand(1))->getValue() == 0;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000423 bool isNE = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000424
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000425 //Fix up CC
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000426 if(cCode == ISD::SETNE)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000427 isNE = true;
Andrew Lenharth445171a2005-02-08 00:40:03 +0000428
Andrew Lenharth694c2982005-06-26 23:01:11 +0000429 if (RightZero) {
Andrew Lenharth09552bf2005-06-08 18:02:21 +0000430 switch (cCode) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000431 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
432 case ISD::SETEQ: Opc = Alpha::BEQ; break;
433 case ISD::SETLT: Opc = Alpha::BLT; break;
434 case ISD::SETLE: Opc = Alpha::BLE; break;
435 case ISD::SETGT: Opc = Alpha::BGT; break;
436 case ISD::SETGE: Opc = Alpha::BGE; break;
437 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
438 case ISD::SETUGT: Opc = Alpha::BNE; break;
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000439 //Technically you could have this CC
440 case ISD::SETULE: Opc = Alpha::BEQ; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000441 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
442 case ISD::SETNE: Opc = Alpha::BNE; break;
443 }
Chris Lattner88ac32c2005-08-09 20:21:10 +0000444 unsigned Tmp1 = SelectExpr(CC.getOperand(0)); //Cond
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000445 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
446 return;
447 } else {
448 unsigned Tmp1 = SelectExpr(CC);
449 if (isNE)
450 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
451 else
452 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000453 return;
454 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000455 } else { //FP
Jeff Cohen00b168892005-07-27 06:12:32 +0000456 //Any comparison between 2 values should be codegened as an folded
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000457 //branch, as moving CC to the integer register is very expensive
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000458 //for a cmp b: c = a - b;
459 //a = b: c = 0
460 //a < b: c < 0
461 //a > b: c > 0
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000462
463 bool invTest = false;
464 unsigned Tmp3;
465
466 ConstantFPSDNode *CN;
Chris Lattner88ac32c2005-08-09 20:21:10 +0000467 if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(1)))
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000468 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
Chris Lattner88ac32c2005-08-09 20:21:10 +0000469 Tmp3 = SelectExpr(CC.getOperand(0));
470 else if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(0)))
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000471 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
472 {
Chris Lattner88ac32c2005-08-09 20:21:10 +0000473 Tmp3 = SelectExpr(CC.getOperand(1));
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000474 invTest = true;
475 }
476 else
477 {
Chris Lattner88ac32c2005-08-09 20:21:10 +0000478 unsigned Tmp1 = SelectExpr(CC.getOperand(0));
479 unsigned Tmp2 = SelectExpr(CC.getOperand(1));
480 bool isD = CC.getOperand(0).getValueType() == MVT::f64;
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000481 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
482 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
483 .addReg(Tmp1).addReg(Tmp2);
484 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000485
Chris Lattner88ac32c2005-08-09 20:21:10 +0000486 switch (cCode) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000487 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000488 case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
489 case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
490 case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
491 case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
492 case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
493 case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000494 }
495 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000496 return;
497 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000498 abort(); //Should never be reached
499 } else {
500 //Giveup and do the stupid thing
501 unsigned Tmp1 = SelectExpr(CC);
502 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
503 return;
504 }
Andrew Lenharth445171a2005-02-08 00:40:03 +0000505 abort(); //Should never be reached
506}
507
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000508unsigned AlphaISel::SelectExpr(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000509 unsigned Result;
Andrew Lenharth2966e842005-04-07 18:15:28 +0000510 unsigned Tmp1, Tmp2 = 0, Tmp3;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000511 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000512 unsigned opcode = N.getOpcode();
Chris Lattnerd2fc54e2005-10-21 16:01:26 +0000513 int64_t SImm = 0;
Andrew Lenharthd2284272005-08-15 14:31:37 +0000514 uint64_t UImm;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000515
516 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000517 MVT::ValueType DestType = N.getValueType();
Andrew Lenharthf4da9452005-06-29 12:49:51 +0000518 bool isFP = DestType == MVT::f64 || DestType == MVT::f32;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000519
520 unsigned &Reg = ExprMap[N];
521 if (Reg) return Reg;
522
Andrew Lenharth46a776e2005-09-06 17:00:23 +0000523 switch(N.getOpcode()) {
524 default:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000525 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000526 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth46a776e2005-09-06 17:00:23 +0000527 break;
528 case ISD::AssertSext:
529 case ISD::AssertZext:
530 return Reg = SelectExpr(N.getOperand(0));
531 case ISD::CALL:
532 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000533 // If this is a call instruction, make sure to prepare ALL of the result
534 // values as well as the chain.
535 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000536 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000537 else {
538 Result = MakeReg(Node->getValueType(0));
539 ExprMap[N.getValue(0)] = Result;
540 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
541 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000542 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000543 }
Andrew Lenharth46a776e2005-09-06 17:00:23 +0000544 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000545 }
546
Andrew Lenharth40831c52005-01-28 06:57:18 +0000547 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000548 default:
549 Node->dump();
550 assert(0 && "Node not handled!\n");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000551
Andrew Lenharth51b8d542005-11-11 16:47:30 +0000552 case ISD::READCYCLECOUNTER:
553 Select(N.getOperand(0)); //Select chain
Andrew Lenharth2729e612005-11-11 23:02:55 +0000554 if (Result != notIn)
555 ExprMap[N.getValue(1)] = notIn; // Generate the token
556 else
557 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
558
Andrew Lenharth51b8d542005-11-11 16:47:30 +0000559 BuildMI(BB, Alpha::RPCC, 1, Result).addReg(Alpha::R31);
560 return Result;
561
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000562 case ISD::CTPOP:
563 case ISD::CTTZ:
564 case ISD::CTLZ:
565 Opc = opcode == ISD::CTPOP ? Alpha::CTPOP :
566 (opcode == ISD::CTTZ ? Alpha::CTTZ : Alpha::CTLZ);
567 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth964b6aa2005-10-20 19:39:24 +0000568 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000569 return Result;
570
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000571 case ISD::MULHU:
572 Tmp1 = SelectExpr(N.getOperand(0));
573 Tmp2 = SelectExpr(N.getOperand(1));
574 BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth706be912005-04-07 13:55:53 +0000575 return Result;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000576 case ISD::MULHS:
577 {
578 //MULHU - Ra<63>*Rb - Rb<63>*Ra
579 Tmp1 = SelectExpr(N.getOperand(0));
580 Tmp2 = SelectExpr(N.getOperand(1));
581 Tmp3 = MakeReg(MVT::i64);
582 BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
583 unsigned V1 = MakeReg(MVT::i64);
584 unsigned V2 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000585 BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31)
586 .addReg(Tmp1);
587 BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31)
588 .addReg(Tmp2);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000589 unsigned IRes = MakeReg(MVT::i64);
590 BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
591 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
592 return Result;
593 }
Andrew Lenharth7332f3e2005-04-02 19:11:07 +0000594 case ISD::UNDEF: {
Andrew Lenharth50b37842005-11-22 04:20:06 +0000595 Opc = isFP ? (DestType == MVT::f32 ? Alpha::IDEF_F32 : Alpha::IDEF_F64)
596 : Alpha::IDEF_I;
597 BuildMI(BB, Opc, 0, Result);
Andrew Lenharth7332f3e2005-04-02 19:11:07 +0000598 return Result;
599 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000600
Andrew Lenharth032f2352005-02-22 21:59:48 +0000601 case ISD::DYNAMIC_STACKALLOC:
602 // Generate both result values.
Andrew Lenharth3a7118d2005-02-23 17:33:42 +0000603 if (Result != notIn)
604 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth032f2352005-02-22 21:59:48 +0000605 else
606 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
607
608 // FIXME: We are currently ignoring the requested alignment for handling
609 // greater than the stack alignment. This will need to be revisited at some
610 // point. Align = N.getOperand(2);
611
612 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
613 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
614 std::cerr << "Cannot allocate stack object with greater alignment than"
615 << " the stack alignment yet!";
616 abort();
617 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000618
Andrew Lenharth032f2352005-02-22 21:59:48 +0000619 Select(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +0000620 if (isSIntImmediateBounded(N.getOperand(1), SImm, 0, 32767))
621 BuildMI(BB, Alpha::LDA, 2, Alpha::R30).addImm(-SImm).addReg(Alpha::R30);
622 else {
Andrew Lenharth032f2352005-02-22 21:59:48 +0000623 Tmp1 = SelectExpr(N.getOperand(1));
624 // Subtract size from stack pointer, thereby allocating some space.
625 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
626 }
627
628 // Put a pointer to the space into the result register, by copying the stack
629 // pointer.
Andrew Lenharth7bc47022005-02-22 23:29:25 +0000630 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
Andrew Lenharth032f2352005-02-22 21:59:48 +0000631 return Result;
632
Andrew Lenharth02c318e2005-06-27 21:02:56 +0000633 case ISD::ConstantPool:
Chris Lattner5839bf22005-08-26 17:15:30 +0000634 Tmp1 = BB->getParent()->getConstantPool()->
635 getConstantPoolIndex(cast<ConstantPoolSDNode>(N)->get());
Andrew Lenharth02c318e2005-06-27 21:02:56 +0000636 AlphaLowering.restoreGP(BB);
637 Tmp2 = MakeReg(MVT::i64);
638 BuildMI(BB, Alpha::LDAHr, 2, Tmp2).addConstantPoolIndex(Tmp1)
639 .addReg(Alpha::R29);
640 BuildMI(BB, Alpha::LDAr, 2, Result).addConstantPoolIndex(Tmp1)
641 .addReg(Tmp2);
642 return Result;
Andrew Lenharth2c594352005-01-29 15:42:07 +0000643
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000644 case ISD::FrameIndex:
Andrew Lenharth032f2352005-02-22 21:59:48 +0000645 BuildMI(BB, Alpha::LDA, 2, Result)
646 .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
647 .addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000648 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000649
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000650 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +0000651 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000652 case ISD::SEXTLOAD:
Misha Brukman4633f1c2005-04-21 23:13:11 +0000653 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000654 {
655 // Make sure we generate both values.
656 if (Result != notIn)
657 ExprMap[N.getValue(1)] = notIn; // Generate the token
658 else
659 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +0000660
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000661 SDOperand Chain = N.getOperand(0);
662 SDOperand Address = N.getOperand(1);
663 Select(Chain);
664
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000665 bool fpext = true;
666
Andrew Lenharth03824012005-02-07 05:55:55 +0000667 if (opcode == ISD::LOAD)
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000668 switch (Node->getValueType(0)) {
669 default: Node->dump(); assert(0 && "Bad load!");
670 case MVT::i64: Opc = Alpha::LDQ; break;
671 case MVT::f64: Opc = Alpha::LDT; break;
672 case MVT::f32: Opc = Alpha::LDS; break;
673 }
Andrew Lenharth03824012005-02-07 05:55:55 +0000674 else
Chris Lattnerbce81ae2005-07-10 01:56:13 +0000675 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000676 default: Node->dump(); assert(0 && "Bad sign extend!");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000677 case MVT::i32: Opc = Alpha::LDL;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000678 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000679 case MVT::i16: Opc = Alpha::LDWU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000680 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +0000681 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Misha Brukman4633f1c2005-04-21 23:13:11 +0000682 case MVT::i8: Opc = Alpha::LDBU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000683 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000684 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000685
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000686 int i, j, k;
687 if (EnableAlphaLSMark)
688 getValueInfo(dyn_cast<SrcValueSDNode>(N.getOperand(2))->getValue(),
689 i, j, k);
690
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000691 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Address);
692 if (GASD && !GASD->getGlobal()->isExternal()) {
693 Tmp1 = MakeReg(MVT::i64);
694 AlphaLowering.restoreGP(BB);
695 BuildMI(BB, Alpha::LDAHr, 2, Tmp1)
696 .addGlobalAddress(GASD->getGlobal()).addReg(Alpha::R29);
697 if (EnableAlphaLSMark)
698 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
699 .addImm(getUID());
700 BuildMI(BB, GetRelVersion(Opc), 2, Result)
701 .addGlobalAddress(GASD->getGlobal()).addReg(Tmp1);
Chris Lattnerbce81ae2005-07-10 01:56:13 +0000702 } else if (ConstantPoolSDNode *CP =
703 dyn_cast<ConstantPoolSDNode>(Address)) {
Chris Lattner5839bf22005-08-26 17:15:30 +0000704 unsigned CPIdx = BB->getParent()->getConstantPool()->
705 getConstantPoolIndex(CP->get());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000706 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000707 has_sym = true;
Andrew Lenharthfe895e32005-06-27 17:15:36 +0000708 Tmp1 = MakeReg(MVT::i64);
Chris Lattner5839bf22005-08-26 17:15:30 +0000709 BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CPIdx)
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000710 .addReg(Alpha::R29);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000711 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000712 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
713 .addImm(getUID());
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000714 BuildMI(BB, GetRelVersion(Opc), 2, Result)
Chris Lattner5839bf22005-08-26 17:15:30 +0000715 .addConstantPoolIndex(CPIdx).addReg(Tmp1);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000716 } else if(Address.getOpcode() == ISD::FrameIndex) {
717 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000718 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
719 .addImm(getUID());
Andrew Lenharth032f2352005-02-22 21:59:48 +0000720 BuildMI(BB, Opc, 2, Result)
721 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
722 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000723 } else {
724 long offset;
725 SelectAddr(Address, Tmp1, offset);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000726 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000727 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
728 .addImm(getUID());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000729 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
730 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000731 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000732 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000733
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000734 case ISD::GlobalAddress:
735 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000736 has_sym = true;
Jeff Cohen00b168892005-07-27 06:12:32 +0000737
Andrew Lenharth2f5bca52005-07-03 20:06:13 +0000738 Reg = Result = MakeReg(MVT::i64);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000739
740 if (EnableAlphaLSMark)
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000741 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000742 .addImm(getUID());
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000743
744 BuildMI(BB, Alpha::LDQl, 2, Result)
Andrew Lenharthc95d9842005-06-27 21:11:40 +0000745 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal())
746 .addReg(Alpha::R29);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000747 return Result;
748
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000749 case ISD::ExternalSymbol:
750 AlphaLowering.restoreGP(BB);
751 has_sym = true;
752
Andrew Lenharth2f5bca52005-07-03 20:06:13 +0000753 Reg = Result = MakeReg(MVT::i64);
754
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000755 if (EnableAlphaLSMark)
756 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
757 .addImm(getUID());
758
759 BuildMI(BB, Alpha::LDQl, 2, Result)
760 .addExternalSymbol(cast<ExternalSymbolSDNode>(N)->getSymbol())
761 .addReg(Alpha::R29);
762 return Result;
763
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +0000764 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000765 case ISD::CALL:
766 {
767 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000768
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000769 // The chain for this call is now lowered.
Andrew Lenharthf3f951a2005-07-22 20:50:29 +0000770 ExprMap[N.getValue(Node->getNumValues()-1)] = notIn;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000771
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000772 //grab the arguments
773 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000774 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000775 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000776 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000777
Andrew Lenharth684f2292005-01-30 00:35:27 +0000778 //in reg args
779 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000780 {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000781 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000782 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000783 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000784 Alpha::F19, Alpha::F20, Alpha::F21};
785 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000786 default:
787 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000788 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000789 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000790 N.getOperand(i+2).getValueType() << "\n";
791 assert(0 && "Unknown value type for call");
792 case MVT::i1:
793 case MVT::i8:
794 case MVT::i16:
795 case MVT::i32:
796 case MVT::i64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000797 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i])
798 .addReg(argvregs[i]);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000799 break;
800 case MVT::f32:
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +0000801 BuildMI(BB, Alpha::CPYSS, 2, args_float[i]).addReg(argvregs[i])
802 .addReg(argvregs[i]);
803 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000804 case MVT::f64:
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +0000805 BuildMI(BB, Alpha::CPYST, 2, args_float[i]).addReg(argvregs[i])
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000806 .addReg(argvregs[i]);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000807 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000808 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000809 }
Andrew Lenharth684f2292005-01-30 00:35:27 +0000810 //in mem args
811 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000812 {
813 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000814 default:
815 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000816 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000817 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000818 N.getOperand(i+2).getValueType() << "\n";
819 assert(0 && "Unknown value type for call");
820 case MVT::i1:
821 case MVT::i8:
822 case MVT::i16:
823 case MVT::i32:
824 case MVT::i64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000825 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
826 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000827 break;
828 case MVT::f32:
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000829 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
830 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000831 break;
832 case MVT::f64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000833 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
834 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000835 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000836 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000837 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000838 //build the right kind of call
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000839 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(N.getOperand(1));
840 if (GASD && !GASD->getGlobal()->isExternal()) {
841 //use PC relative branch call
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000842 AlphaLowering.restoreGP(BB);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000843 BuildMI(BB, Alpha::BSR, 1, Alpha::R26)
844 .addGlobalAddress(GASD->getGlobal(),true);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000845 } else {
846 //no need to restore GP as we are doing an indirect call
847 Tmp1 = SelectExpr(N.getOperand(1));
848 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
849 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
850 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000851
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000852 //push the result into a virtual register
Misha Brukman4633f1c2005-04-21 23:13:11 +0000853
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000854 switch (Node->getValueType(0)) {
855 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000856 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000857 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +0000858 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
859 break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000860 case MVT::f32:
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +0000861 BuildMI(BB, Alpha::CPYSS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
862 break;
863 case MVT::f64:
864 BuildMI(BB, Alpha::CPYST, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
Misha Brukman7847fca2005-04-22 17:54:37 +0000865 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000866 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000867 return Result+N.ResNo;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000868 }
869
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000870 case ISD::SIGN_EXTEND_INREG:
871 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000872 //do SDIV opt for all levels of ints if not dividing by a constant
873 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
874 && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +0000875 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +0000876 unsigned Tmp4 = MakeReg(MVT::f64);
877 unsigned Tmp5 = MakeReg(MVT::f64);
878 unsigned Tmp6 = MakeReg(MVT::f64);
879 unsigned Tmp7 = MakeReg(MVT::f64);
880 unsigned Tmp8 = MakeReg(MVT::f64);
881 unsigned Tmp9 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000882
883 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
884 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
885 MoveInt2FP(Tmp1, Tmp4, true);
886 MoveInt2FP(Tmp2, Tmp5, true);
Andrew Lenharth98169be2005-07-28 18:14:47 +0000887 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Alpha::F31).addReg(Tmp4);
888 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Alpha::F31).addReg(Tmp5);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +0000889 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
Andrew Lenharth98169be2005-07-28 18:14:47 +0000890 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Alpha::F31).addReg(Tmp8);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000891 MoveFP2Int(Tmp9, Result, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +0000892 return Result;
893 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000894
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000895 //Alpha has instructions for a bunch of signed 32 bit stuff
Chris Lattnerbce81ae2005-07-10 01:56:13 +0000896 if(cast<VTSDNode>(Node->getOperand(1))->getVT() == MVT::i32) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000897 switch (N.getOperand(0).getOpcode()) {
898 case ISD::ADD:
899 case ISD::SUB:
900 case ISD::MUL:
901 {
902 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
903 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
904 //FIXME: first check for Scaled Adds and Subs!
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000905 if(!isMul && N.getOperand(0).getOperand(0).getOpcode() == ISD::SHL &&
Andrew Lenharthd2284272005-08-15 14:31:37 +0000906 isSIntImmediateBounded(N.getOperand(0).getOperand(0).getOperand(1), SImm, 2, 3))
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000907 {
Andrew Lenharthd2284272005-08-15 14:31:37 +0000908 bool use4 = SImm == 2;
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000909 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
910 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
911 BuildMI(BB, isAdd?(use4?Alpha::S4ADDL:Alpha::S8ADDL):(use4?Alpha::S4SUBL:Alpha::S8SUBL),
912 2,Result).addReg(Tmp1).addReg(Tmp2);
913 }
914 else if(isAdd && N.getOperand(0).getOperand(1).getOpcode() == ISD::SHL &&
Andrew Lenharthd2284272005-08-15 14:31:37 +0000915 isSIntImmediateBounded(N.getOperand(0).getOperand(1).getOperand(1), SImm, 2, 3))
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000916 {
Andrew Lenharthd2284272005-08-15 14:31:37 +0000917 bool use4 = SImm == 2;
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000918 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
919 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
920 BuildMI(BB, use4?Alpha::S4ADDL:Alpha::S8ADDL, 2,Result).addReg(Tmp1).addReg(Tmp2);
921 }
Andrew Lenharthd2284272005-08-15 14:31:37 +0000922 else if(isSIntImmediateBounded(N.getOperand(0).getOperand(1), SImm, 0, 255))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000923 { //Normal imm add/sub
924 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000925 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +0000926 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000927 }
Andrew Lenharthd2284272005-08-15 14:31:37 +0000928 else if(!isMul && isSIntImmediate(N.getOperand(0).getOperand(1), SImm) &&
929 (((SImm << 32) >> 32) >= -255) && (((SImm << 32) >> 32) <= 0))
Andrew Lenharth6b137d82005-07-22 22:24:01 +0000930 { //handle canonicalization
931 Opc = isAdd ? Alpha::SUBLi : Alpha::ADDLi;
932 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +0000933 SImm = 0 - ((SImm << 32) >> 32);
934 assert(SImm >= 0 && SImm <= 255);
935 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharth6b137d82005-07-22 22:24:01 +0000936 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000937 else
938 { //Normal add/sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000939 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULL : Alpha::SUBL);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000940 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000941 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000942 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
943 }
944 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000945 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000946 default: break; //Fall Though;
947 }
948 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000949 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000950 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Chris Lattnerbce81ae2005-07-10 01:56:13 +0000951 switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000952 default:
953 Node->dump();
954 assert(0 && "Sign Extend InReg not there yet");
955 break;
956 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000957 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000958 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000959 break;
960 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000961 case MVT::i16:
Andrew Lenharth964b6aa2005-10-20 19:39:24 +0000962 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000963 break;
964 case MVT::i8:
Andrew Lenharth964b6aa2005-10-20 19:39:24 +0000965 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000966 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +0000967 case MVT::i1:
968 Tmp2 = MakeReg(MVT::i64);
969 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000970 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +0000971 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000972 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000973 return Result;
974 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000975
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000976 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000977 {
Chris Lattner88ac32c2005-08-09 20:21:10 +0000978 ISD::CondCode CC = cast<CondCodeSDNode>(N.getOperand(2))->get();
979 if (MVT::isInteger(N.getOperand(0).getValueType())) {
980 bool isConst = false;
981 int dir;
Misha Brukman7847fca2005-04-22 17:54:37 +0000982
Chris Lattner88ac32c2005-08-09 20:21:10 +0000983 //Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +0000984 if(isSIntImmediate(N.getOperand(1), SImm) && SImm <= 255 && SImm >= 0)
Chris Lattner88ac32c2005-08-09 20:21:10 +0000985 isConst = true;
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000986
Chris Lattner88ac32c2005-08-09 20:21:10 +0000987 switch (CC) {
988 default: Node->dump(); assert(0 && "Unknown integer comparison!");
989 case ISD::SETEQ:
990 Opc = isConst ? Alpha::CMPEQi : Alpha::CMPEQ; dir=1; break;
991 case ISD::SETLT:
992 Opc = isConst ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
993 case ISD::SETLE:
994 Opc = isConst ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
995 case ISD::SETGT: Opc = Alpha::CMPLT; dir = 2; break;
996 case ISD::SETGE: Opc = Alpha::CMPLE; dir = 2; break;
997 case ISD::SETULT:
998 Opc = isConst ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
999 case ISD::SETUGT: Opc = Alpha::CMPULT; dir = 2; break;
1000 case ISD::SETULE:
1001 Opc = isConst ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
1002 case ISD::SETUGE: Opc = Alpha::CMPULE; dir = 2; break;
1003 case ISD::SETNE: {//Handle this one special
1004 //std::cerr << "Alpha does not have a setne.\n";
1005 //abort();
1006 Tmp1 = SelectExpr(N.getOperand(0));
1007 Tmp2 = SelectExpr(N.getOperand(1));
1008 Tmp3 = MakeReg(MVT::i64);
1009 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1010 //Remeber we have the Inv for this CC
1011 CCInvMap[N] = Tmp3;
1012 //and invert
1013 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
1014 return Result;
1015 }
1016 }
1017 if (dir == 1) {
1018 Tmp1 = SelectExpr(N.getOperand(0));
1019 if (isConst) {
Andrew Lenharthd2284272005-08-15 14:31:37 +00001020 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Chris Lattner88ac32c2005-08-09 20:21:10 +00001021 } else {
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001022 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth694c2982005-06-26 23:01:11 +00001023 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001024 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001025 } else { //if (dir == 2) {
1026 Tmp1 = SelectExpr(N.getOperand(1));
1027 Tmp2 = SelectExpr(N.getOperand(0));
1028 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001029 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001030 } else {
1031 //do the comparison
1032 Tmp1 = MakeReg(MVT::f64);
1033 bool inv = SelectFPSetCC(N, Tmp1);
1034
1035 //now arrange for Result (int) to have a 1 or 0
1036 Tmp2 = MakeReg(MVT::i64);
1037 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
1038 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
1039 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
Andrew Lenharth9818c052005-02-05 13:19:12 +00001040 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001041 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001042 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001043
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001044 case ISD::CopyFromReg:
1045 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001046 ++count_ins;
1047
Andrew Lenharth40831c52005-01-28 06:57:18 +00001048 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001049 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001050 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001051 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001052 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001053
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001054 SDOperand Chain = N.getOperand(0);
1055
1056 Select(Chain);
Chris Lattner707ebc52005-08-16 21:56:37 +00001057 unsigned r = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001058 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001059 switch(N.getValue(0).getValueType()) {
1060 case MVT::f32:
1061 BuildMI(BB, Alpha::CPYSS, 2, Result).addReg(r).addReg(r);
1062 break;
1063 case MVT::f64:
1064 BuildMI(BB, Alpha::CPYST, 2, Result).addReg(r).addReg(r);
1065 break;
1066 default:
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001067 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001068 break;
1069 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001070 return Result;
1071 }
1072
Misha Brukman4633f1c2005-04-21 23:13:11 +00001073 //Most of the plain arithmetic and logic share the same form, and the same
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001074 //constant immediate test
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001075 case ISD::XOR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001076 //Match Not
Andrew Lenharthd2284272005-08-15 14:31:37 +00001077 if (isSIntImmediate(N.getOperand(1), SImm) && SImm == -1) {
1078 Tmp1 = SelectExpr(N.getOperand(0));
1079 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1080 return Result;
1081 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001082 //Fall through
1083 case ISD::AND:
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001084 //handle zap
Andrew Lenharthd2284272005-08-15 14:31:37 +00001085 if (opcode == ISD::AND && isUIntImmediate(N.getOperand(1), UImm))
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001086 {
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001087 unsigned int build = 0;
1088 for(int i = 0; i < 8; ++i)
1089 {
Andrew Lenharthd2284272005-08-15 14:31:37 +00001090 if ((UImm & 0x00FF) == 0x00FF)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001091 build |= 1 << i;
Andrew Lenharthd2284272005-08-15 14:31:37 +00001092 else if ((UImm & 0x00FF) != 0)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001093 { build = 0; break; }
Andrew Lenharthd2284272005-08-15 14:31:37 +00001094 UImm >>= 8;
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001095 }
1096 if (build)
1097 {
1098 Tmp1 = SelectExpr(N.getOperand(0));
1099 BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1100 return Result;
1101 }
1102 }
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001103 case ISD::OR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001104 //Check operand(0) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001105 if (N.getOperand(0).getOpcode() == ISD::XOR &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001106 isSIntImmediate(N.getOperand(0).getOperand(1), SImm) && SImm == -1) {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001107 switch(opcode) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001108 case ISD::AND: Opc = Alpha::BIC; break;
1109 case ISD::OR: Opc = Alpha::ORNOT; break;
1110 case ISD::XOR: Opc = Alpha::EQV; break;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001111 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001112 Tmp1 = SelectExpr(N.getOperand(1));
1113 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1114 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1115 return Result;
1116 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001117 //Check operand(1) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001118 if (N.getOperand(1).getOpcode() == ISD::XOR &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001119 isSIntImmediate(N.getOperand(1).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(0));
1126 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1127 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1128 return Result;
1129 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001130 //Fall through
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001131 case ISD::SHL:
1132 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001133 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001134 case ISD::MUL:
Andrew Lenharthd2284272005-08-15 14:31:37 +00001135 if(isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255)) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001136 switch(opcode) {
1137 case ISD::AND: Opc = Alpha::ANDi; break;
1138 case ISD::OR: Opc = Alpha::BISi; break;
1139 case ISD::XOR: Opc = Alpha::XORi; break;
1140 case ISD::SHL: Opc = Alpha::SLi; break;
1141 case ISD::SRL: Opc = Alpha::SRLi; break;
1142 case ISD::SRA: Opc = Alpha::SRAi; break;
1143 case ISD::MUL: Opc = Alpha::MULQi; break;
1144 };
1145 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001146 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001147 } else {
1148 switch(opcode) {
1149 case ISD::AND: Opc = Alpha::AND; break;
1150 case ISD::OR: Opc = Alpha::BIS; break;
1151 case ISD::XOR: Opc = Alpha::XOR; break;
1152 case ISD::SHL: Opc = Alpha::SL; break;
1153 case ISD::SRL: Opc = Alpha::SRL; break;
1154 case ISD::SRA: Opc = Alpha::SRA; break;
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001155 case ISD::MUL: Opc = Alpha::MULQ; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001156 };
1157 Tmp1 = SelectExpr(N.getOperand(0));
1158 Tmp2 = SelectExpr(N.getOperand(1));
1159 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1160 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001161 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001162
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001163 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001164 case ISD::SUB:
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001165 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001166 bool isAdd = opcode == ISD::ADD;
1167
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001168 //first check for Scaled Adds and Subs!
1169 //Valid for add and sub
Andrew Lenharthd2284272005-08-15 14:31:37 +00001170 if(N.getOperand(0).getOpcode() == ISD::SHL &&
1171 isSIntImmediate(N.getOperand(0).getOperand(1), SImm) &&
1172 (SImm == 2 || SImm == 3)) {
1173 bool use4 = SImm == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001174 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001175 if (isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255))
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001176 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
Andrew Lenharthd2284272005-08-15 14:31:37 +00001177 2, Result).addReg(Tmp2).addImm(SImm);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001178 else {
1179 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001180 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1181 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001182 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001183 }
1184 //Position prevents subs
Andrew Lenharth273a1f92005-04-07 14:18:13 +00001185 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001186 isSIntImmediate(N.getOperand(1).getOperand(1), SImm) &&
1187 (SImm == 2 || SImm == 3)) {
1188 bool use4 = SImm == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001189 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001190 if (isSIntImmediateBounded(N.getOperand(0), SImm, 0, 255))
1191 BuildMI(BB, use4?Alpha::S4ADDQi:Alpha::S8ADDQi, 2, Result).addReg(Tmp2).addImm(SImm);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001192 else {
1193 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001194 BuildMI(BB, use4?Alpha::S4ADDQ:Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001195 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001196 }
1197 //small addi
Andrew Lenharthd2284272005-08-15 14:31:37 +00001198 else if(isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001199 { //Normal imm add/sub
1200 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1201 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001202 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001203 }
Andrew Lenharthd2284272005-08-15 14:31:37 +00001204 else if(isSIntImmediateBounded(N.getOperand(1), SImm, -255, 0))
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001205 { //inverted imm add/sub
1206 Opc = isAdd ? Alpha::SUBQi : Alpha::ADDQi;
1207 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001208 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(-SImm);
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001209 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001210 //larger addi
Andrew Lenharthd2284272005-08-15 14:31:37 +00001211 else if(isSIntImmediateBounded(N.getOperand(1), SImm, -32767, 32767))
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001212 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001213 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001214 if (!isAdd)
Andrew Lenharthd2284272005-08-15 14:31:37 +00001215 SImm = -SImm;
1216 BuildMI(BB, Alpha::LDA, 2, Result).addImm(SImm).addReg(Tmp1);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001217 }
1218 //give up and do the operation
1219 else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001220 //Normal add/sub
1221 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1222 Tmp1 = SelectExpr(N.getOperand(0));
1223 Tmp2 = SelectExpr(N.getOperand(1));
1224 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1225 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001226 return Result;
1227 }
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001228 case ISD::FADD:
1229 case ISD::FSUB:
1230 case ISD::FMUL:
1231 case ISD::FDIV: {
1232 if (opcode == ISD::FADD)
1233 Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS;
1234 else if (opcode == ISD::FSUB)
1235 Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS;
1236 else if (opcode == ISD::FMUL)
1237 Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS;
1238 else
1239 Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS;
1240 Tmp1 = SelectExpr(N.getOperand(0));
1241 Tmp2 = SelectExpr(N.getOperand(1));
1242 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1243 return Result;
1244 }
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001245 case ISD::SDIV:
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001246 {
Andrew Lenhartha565c272005-04-06 22:03:13 +00001247 //check if we can convert into a shift!
Andrew Lenharthd2284272005-08-15 14:31:37 +00001248 if (isSIntImmediate(N.getOperand(1), SImm) &&
1249 SImm != 0 && isPowerOf2_64(llabs(SImm))) {
1250 unsigned k = Log2_64(llabs(SImm));
Andrew Lenhartha565c272005-04-06 22:03:13 +00001251 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenhartha565c272005-04-06 22:03:13 +00001252 if (k == 1)
1253 Tmp2 = Tmp1;
1254 else
1255 {
1256 Tmp2 = MakeReg(MVT::i64);
1257 BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
1258 }
1259 Tmp3 = MakeReg(MVT::i64);
1260 BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
1261 unsigned Tmp4 = MakeReg(MVT::i64);
1262 BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharthd2284272005-08-15 14:31:37 +00001263 if (SImm > 0)
Andrew Lenhartha565c272005-04-06 22:03:13 +00001264 BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
1265 else
1266 {
1267 unsigned Tmp5 = MakeReg(MVT::i64);
1268 BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
1269 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
1270 }
1271 return Result;
1272 }
1273 }
1274 //Else fall through
Andrew Lenhartha565c272005-04-06 22:03:13 +00001275 case ISD::UDIV:
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001276 //else fall though
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001277 case ISD::UREM:
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001278 case ISD::SREM: {
1279 const char* opstr = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001280 switch(opcode) {
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001281 case ISD::UREM: opstr = "__remqu"; break;
1282 case ISD::SREM: opstr = "__remq"; break;
1283 case ISD::UDIV: opstr = "__divqu"; break;
1284 case ISD::SDIV: opstr = "__divq"; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001285 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001286 Tmp1 = SelectExpr(N.getOperand(0));
1287 Tmp2 = SelectExpr(N.getOperand(1));
Jeff Cohen00b168892005-07-27 06:12:32 +00001288 SDOperand Addr =
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001289 ISelDAG->getExternalSymbol(opstr, AlphaLowering.getPointerTy());
1290 Tmp3 = SelectExpr(Addr);
Andrew Lenharth33819132005-03-04 20:09:23 +00001291 //set up regs explicitly (helps Reg alloc)
1292 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001293 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001294 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp3).addReg(Tmp3);
1295 BuildMI(BB, Alpha::JSRs, 2, Alpha::R23).addReg(Alpha::R27).addImm(0);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001296 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001297 return Result;
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001298 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001299
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001300 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001301 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001302 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001303 assert (DestType == MVT::i64 && "only quads can be loaded to");
1304 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00001305 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001306 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001307 if (SrcType == MVT::f32)
Misha Brukman7847fca2005-04-22 17:54:37 +00001308 {
1309 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth7b441dc2005-11-10 16:59:55 +00001310 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Tmp1);
Misha Brukman7847fca2005-04-22 17:54:37 +00001311 Tmp1 = Tmp2;
1312 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001313 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth7b441dc2005-11-10 16:59:55 +00001314 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Tmp1);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001315 MoveFP2Int(Tmp2, Result, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001316
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001317 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001318 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001319
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001320 case ISD::SELECT:
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001321 if (isFP) {
1322 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1323 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
1324 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
1325
1326 SDOperand CC = N.getOperand(0);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001327
Chris Lattner88ac32c2005-08-09 20:21:10 +00001328 if (CC.getOpcode() == ISD::SETCC &&
1329 !MVT::isInteger(CC.getOperand(0).getValueType())) {
1330 //FP Setcc -> Select yay!
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001331
Jeff Cohen00b168892005-07-27 06:12:32 +00001332
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001333 //for a cmp b: c = a - b;
1334 //a = b: c = 0
1335 //a < b: c < 0
1336 //a > b: c > 0
1337
1338 bool invTest = false;
1339 unsigned Tmp3;
1340
1341 ConstantFPSDNode *CN;
Chris Lattner88ac32c2005-08-09 20:21:10 +00001342 if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(1)))
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001343 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
Chris Lattner88ac32c2005-08-09 20:21:10 +00001344 Tmp3 = SelectExpr(CC.getOperand(0));
1345 else if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(0)))
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001346 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1347 {
Chris Lattner88ac32c2005-08-09 20:21:10 +00001348 Tmp3 = SelectExpr(CC.getOperand(1));
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001349 invTest = true;
1350 }
1351 else
1352 {
Chris Lattner88ac32c2005-08-09 20:21:10 +00001353 unsigned Tmp1 = SelectExpr(CC.getOperand(0));
1354 unsigned Tmp2 = SelectExpr(CC.getOperand(1));
1355 bool isD = CC.getOperand(0).getValueType() == MVT::f64;
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001356 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1357 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1358 .addReg(Tmp1).addReg(Tmp2);
1359 }
1360
Chris Lattner88ac32c2005-08-09 20:21:10 +00001361 switch (cast<CondCodeSDNode>(CC.getOperand(2))->get()) {
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001362 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
1363 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNE : Alpha::FCMOVEQ; break;
1364 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGT : Alpha::FCMOVLT; break;
1365 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGE : Alpha::FCMOVLE; break;
1366 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLT : Alpha::FCMOVGT; break;
1367 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLE : Alpha::FCMOVGE; break;
1368 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQ : Alpha::FCMOVNE; break;
1369 }
1370 BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
1371 return Result;
1372 }
1373 else
1374 {
1375 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1376 BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV)
1377 .addReg(Tmp1);
1378// // Spill the cond to memory and reload it from there.
1379// unsigned Tmp4 = MakeReg(MVT::f64);
1380// MoveIntFP(Tmp1, Tmp4, true);
1381// //now ideally, we don't have to do anything to the flag...
1382// // Get the condition into the zero flag.
1383// BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
1384 return Result;
Jeff Cohen00b168892005-07-27 06:12:32 +00001385 }
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001386 } else {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001387 //FIXME: look at parent to decide if intCC can be folded, or if setCC(FP)
1388 //and can save stack use
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001389 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001390 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1391 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001392 // Get the condition into the zero flag.
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001393 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001394
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001395 SDOperand CC = N.getOperand(0);
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001396
Misha Brukman4633f1c2005-04-21 23:13:11 +00001397 if (CC.getOpcode() == ISD::SETCC &&
Chris Lattner88ac32c2005-08-09 20:21:10 +00001398 !MVT::isInteger(CC.getOperand(0).getValueType()))
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001399 { //FP Setcc -> Int Select
Misha Brukman7847fca2005-04-22 17:54:37 +00001400 Tmp1 = MakeReg(MVT::f64);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001401 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1402 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Misha Brukman7847fca2005-04-22 17:54:37 +00001403 bool inv = SelectFPSetCC(CC, Tmp1);
1404 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
1405 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
1406 return Result;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001407 }
1408 if (CC.getOpcode() == ISD::SETCC) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001409 //Int SetCC -> Select
1410 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharthd2284272005-08-15 14:31:37 +00001411 if(isSIntImmediateBounded(CC.getOperand(1), SImm, 0, 0)) {
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001412 //figure out a few things
Andrew Lenharthd2284272005-08-15 14:31:37 +00001413 bool useImm = isSIntImmediateBounded(N.getOperand(2), SImm, 0, 255);
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001414
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001415 //Fix up CC
Chris Lattner88ac32c2005-08-09 20:21:10 +00001416 ISD::CondCode cCode= cast<CondCodeSDNode>(CC.getOperand(2))->get();
Andrew Lenharth694c2982005-06-26 23:01:11 +00001417 if (useImm) //Invert sense to get Imm field right
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001418 cCode = ISD::getSetCCInverse(cCode, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001419
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001420 //Choose the CMOV
1421 switch (cCode) {
1422 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001423 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
1424 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
1425 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
1426 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
1427 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
1428 case ISD::SETULT: assert(0 && "unsigned < 0 is never true"); break;
1429 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
1430 //Technically you could have this CC
1431 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
1432 case ISD::SETUGE: assert(0 && "unsgined >= 0 is always true"); break;
1433 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001434 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001435 Tmp1 = SelectExpr(CC.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001436
Andrew Lenharth694c2982005-06-26 23:01:11 +00001437 if (useImm) {
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001438 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
Andrew Lenharthd2284272005-08-15 14:31:37 +00001439 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addImm(SImm).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001440 } else {
1441 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1442 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1443 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
1444 }
1445 return Result;
1446 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001447 //Otherwise, fall though
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001448 }
1449 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001450 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1451 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001452 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3)
1453 .addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001454
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001455 return Result;
1456 }
1457
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001458 case ISD::Constant:
1459 {
Andrew Lenharthc0513832005-03-29 19:24:04 +00001460 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001461 int zero_extend_top = 0;
Andrew Lenharthf075cac2005-07-23 07:46:48 +00001462 if (val > 0 && (val & 0xFFFFFFFF00000000ULL) == 0 &&
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001463 ((int32_t)val < 0)) {
1464 //try a small load and zero extend
1465 val = (int32_t)val;
1466 zero_extend_top = 15;
1467 }
1468
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00001469 if (val <= IMM_HIGH && val >= IMM_LOW) {
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001470 if(!zero_extend_top)
1471 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
1472 else {
1473 Tmp1 = MakeReg(MVT::i64);
1474 BuildMI(BB, Alpha::LDA, 2, Tmp1).addImm(val).addReg(Alpha::R31);
1475 BuildMI(BB, Alpha::ZAPNOT, 2, Result).addReg(Tmp1).addImm(zero_extend_top);
1476 }
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00001477 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001478 else if (val <= (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT &&
1479 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
1480 Tmp1 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001481 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val))
1482 .addReg(Alpha::R31);
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001483 if (!zero_extend_top)
1484 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
1485 else {
1486 Tmp3 = MakeReg(MVT::i64);
1487 BuildMI(BB, Alpha::LDA, 2, Tmp3).addImm(getLower16(val)).addReg(Tmp1);
1488 BuildMI(BB, Alpha::ZAPNOT, 2, Result).addReg(Tmp3).addImm(zero_extend_top);
1489 }
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00001490 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001491 else {
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001492 //re-get the val since we are going to mem anyway
1493 val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001494 MachineConstantPool *CP = BB->getParent()->getConstantPool();
Jeff Cohen00b168892005-07-27 06:12:32 +00001495 ConstantUInt *C =
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001496 ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001497 unsigned CPI = CP->getConstantPoolIndex(C);
1498 AlphaLowering.restoreGP(BB);
Andrew Lenharthfe895e32005-06-27 17:15:36 +00001499 has_sym = true;
1500 Tmp1 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001501 BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CPI)
1502 .addReg(Alpha::R29);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001503 if (EnableAlphaLSMark)
1504 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
1505 .addImm(getUID());
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001506 BuildMI(BB, Alpha::LDQr, 2, Result).addConstantPoolIndex(CPI)
1507 .addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001508 }
1509 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001510 }
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001511 case ISD::FNEG:
1512 if(ISD::FABS == N.getOperand(0).getOpcode())
1513 {
1514 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001515 BuildMI(BB, DestType == MVT::f64 ? Alpha::CPYSNT : Alpha::CPYSNS,
1516 2, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001517 } else {
1518 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001519 BuildMI(BB, DestType == MVT::f64 ? Alpha::CPYSNT : Alpha::CPYSNS
1520 , 2, Result).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001521 }
1522 return Result;
1523
1524 case ISD::FABS:
1525 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001526 BuildMI(BB, DestType == MVT::f64 ? Alpha::CPYST : Alpha::CPYSS, 2, Result)
1527 .addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001528 return Result;
1529
1530 case ISD::FP_ROUND:
1531 assert (DestType == MVT::f32 &&
1532 N.getOperand(0).getValueType() == MVT::f64 &&
1533 "only f64 to f32 conversion supported here");
1534 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthb921f1b2005-11-11 23:08:46 +00001535 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001536 return Result;
1537
1538 case ISD::FP_EXTEND:
1539 assert (DestType == MVT::f64 &&
1540 N.getOperand(0).getValueType() == MVT::f32 &&
1541 "only f32 to f64 conversion supported here");
1542 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthc2c64fd2005-11-11 19:52:25 +00001543 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001544 return Result;
1545
1546 case ISD::ConstantFP:
1547 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
1548 if (CN->isExactlyValue(+0.0)) {
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001549 BuildMI(BB, DestType == MVT::f64 ? Alpha::CPYST : Alpha::CPYSS
1550 , 2, Result).addReg(Alpha::F31)
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001551 .addReg(Alpha::F31);
1552 } else if ( CN->isExactlyValue(-0.0)) {
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001553 BuildMI(BB, DestType == MVT::f64 ? Alpha::CPYSNT : Alpha::CPYSNS,
1554 2, Result).addReg(Alpha::F31)
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001555 .addReg(Alpha::F31);
1556 } else {
1557 abort();
1558 }
1559 }
1560 return Result;
1561
1562 case ISD::SINT_TO_FP:
1563 {
1564 assert (N.getOperand(0).getValueType() == MVT::i64
1565 && "only quads can be loaded from");
1566 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1567 Tmp2 = MakeReg(MVT::f64);
1568 MoveInt2FP(Tmp1, Tmp2, true);
1569 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001570 BuildMI(BB, Opc, 1, Result).addReg(Tmp2);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001571 return Result;
1572 }
Andrew Lenharthf71df332005-09-04 06:12:19 +00001573
1574 case ISD::AssertSext:
1575 case ISD::AssertZext:
1576 return SelectExpr(N.getOperand(0));
1577
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001578 }
1579
1580 return 0;
1581}
1582
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001583void AlphaISel::Select(SDOperand N) {
Andrew Lenharth4052f022005-11-22 20:59:00 +00001584 unsigned Tmp1, Tmp2, Opc = Alpha::WTF;
Andrew Lenharth760270d2005-02-07 23:02:23 +00001585 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001586
Nate Begeman85fdeb22005-03-24 04:39:54 +00001587 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001588 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001589
1590 SDNode *Node = N.Val;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001591
Andrew Lenharth760270d2005-02-07 23:02:23 +00001592 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001593
1594 default:
1595 Node->dump(); std::cerr << "\n";
1596 assert(0 && "Node not handled yet!");
1597
1598 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00001599 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001600 return;
1601 }
1602
1603 case ISD::BR: {
1604 MachineBasicBlock *Dest =
1605 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1606
1607 Select(N.getOperand(0));
1608 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
1609 return;
1610 }
1611
1612 case ISD::ImplicitDef:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001613 ++count_ins;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001614 Select(N.getOperand(0));
Andrew Lenharth50b37842005-11-22 04:20:06 +00001615 switch(N.getValueType()) {
1616 case MVT::f32: Opc = Alpha::IDEF_F32; break;
1617 case MVT::f64: Opc = Alpha::IDEF_F64; break;
1618 case MVT::i64: Opc = Alpha::IDEF_I; break;
Andrew Lenharth4052f022005-11-22 20:59:00 +00001619 default: assert(0 && "should have been legalized");
Andrew Lenharth50b37842005-11-22 04:20:06 +00001620 };
1621 BuildMI(BB, Opc, 0,
Chris Lattner707ebc52005-08-16 21:56:37 +00001622 cast<RegisterSDNode>(N.getOperand(1))->getReg());
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001623 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001624
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001625 case ISD::EntryToken: return; // Noop
1626
1627 case ISD::TokenFactor:
1628 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1629 Select(Node->getOperand(i));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001630
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001631 //N.Val->dump(); std::cerr << "\n";
1632 //assert(0 && "Node not handled yet!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001633
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001634 return;
1635
1636 case ISD::CopyToReg:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001637 ++count_outs;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001638 Select(N.getOperand(0));
Chris Lattner707ebc52005-08-16 21:56:37 +00001639 Tmp1 = SelectExpr(N.getOperand(2));
1640 Tmp2 = cast<RegisterSDNode>(N.getOperand(1))->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001641
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001642 if (Tmp1 != Tmp2) {
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001643 switch(N.getOperand(2).getValueType()) {
1644 case MVT::f64:
1645 BuildMI(BB, Alpha::CPYST, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1646 break;
1647 case MVT::f32:
1648 BuildMI(BB, Alpha::CPYSS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1649 break;
1650 default:
Andrew Lenharth29219162005-02-07 06:31:44 +00001651 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001652 break;
1653 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001654 }
1655 return;
1656
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001657 case ISD::RET:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001658 ++count_outs;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001659 switch (N.getNumOperands()) {
1660 default:
1661 std::cerr << N.getNumOperands() << "\n";
1662 for (unsigned i = 0; i < N.getNumOperands(); ++i)
1663 std::cerr << N.getOperand(i).getValueType() << "\n";
1664 Node->dump();
1665 assert(0 && "Unknown return instruction!");
1666 case 2:
1667 Select(N.getOperand(0));
1668 Tmp1 = SelectExpr(N.getOperand(1));
1669 switch (N.getOperand(1).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001670 default: Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001671 assert(0 && "All other types should have been promoted!!");
1672 case MVT::f64:
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001673 BuildMI(BB, Alpha::CPYST, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
1674 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001675 case MVT::f32:
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001676 BuildMI(BB, Alpha::CPYSS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001677 break;
1678 case MVT::i32:
1679 case MVT::i64:
1680 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
1681 break;
1682 }
1683 break;
1684 case 1:
1685 Select(N.getOperand(0));
1686 break;
1687 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001688 // Just emit a 'ret' instruction
Andrew Lenharth6968bff2005-06-27 23:24:11 +00001689 AlphaLowering.restoreRA(BB);
Andrew Lenharthf3f951a2005-07-22 20:50:29 +00001690 BuildMI(BB, Alpha::RET, 2, Alpha::R31).addReg(Alpha::R26).addImm(1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001691 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001692
Misha Brukman4633f1c2005-04-21 23:13:11 +00001693 case ISD::TRUNCSTORE:
1694 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001695 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001696 SDOperand Chain = N.getOperand(0);
1697 SDOperand Value = N.getOperand(1);
1698 SDOperand Address = N.getOperand(2);
1699 Select(Chain);
1700
1701 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00001702
1703 if (opcode == ISD::STORE) {
1704 switch(Value.getValueType()) {
Andrew Lenharth4052f022005-11-22 20:59:00 +00001705 default: assert(0 && "unknown Type in store");
Andrew Lenharth760270d2005-02-07 23:02:23 +00001706 case MVT::i64: Opc = Alpha::STQ; break;
1707 case MVT::f64: Opc = Alpha::STT; break;
1708 case MVT::f32: Opc = Alpha::STS; break;
1709 }
1710 } else { //ISD::TRUNCSTORE
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001711 switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) {
Andrew Lenharth4052f022005-11-22 20:59:00 +00001712 default: assert(0 && "unknown Type in store");
Andrew Lenharth760270d2005-02-07 23:02:23 +00001713 case MVT::i8: Opc = Alpha::STB; break;
1714 case MVT::i16: Opc = Alpha::STW; break;
1715 case MVT::i32: Opc = Alpha::STL; break;
1716 }
Andrew Lenharth65838902005-02-06 16:22:15 +00001717 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00001718
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001719 int i, j, k;
Jeff Cohen00b168892005-07-27 06:12:32 +00001720 if (EnableAlphaLSMark)
1721 getValueInfo(cast<SrcValueSDNode>(N.getOperand(3))->getValue(),
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001722 i, j, k);
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001723
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001724 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Address);
1725 if (GASD && !GASD->getGlobal()->isExternal()) {
1726 Tmp2 = MakeReg(MVT::i64);
1727 AlphaLowering.restoreGP(BB);
1728 BuildMI(BB, Alpha::LDAHr, 2, Tmp2)
1729 .addGlobalAddress(GASD->getGlobal()).addReg(Alpha::R29);
1730 if (EnableAlphaLSMark)
1731 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1732 .addImm(getUID());
1733 BuildMI(BB, GetRelVersion(Opc), 3).addReg(Tmp1)
1734 .addGlobalAddress(GASD->getGlobal()).addReg(Tmp2);
Andrew Lenharthfce587e2005-06-29 00:39:17 +00001735 } else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001736 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001737 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1738 .addImm(getUID());
Andrew Lenharth032f2352005-02-22 21:59:48 +00001739 BuildMI(BB, Opc, 3).addReg(Tmp1)
1740 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1741 .addReg(Alpha::F31);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001742 } else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001743 long offset;
1744 SelectAddr(Address, Tmp2, offset);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001745 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001746 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1747 .addImm(getUID());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001748 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1749 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001750 return;
1751 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001752
1753 case ISD::EXTLOAD:
1754 case ISD::SEXTLOAD:
1755 case ISD::ZEXTLOAD:
1756 case ISD::LOAD:
1757 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001758 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001759 case ISD::CALL:
Andrew Lenharth82a698c2005-11-12 19:04:09 +00001760 case ISD::READCYCLECOUNTER:
Andrew Lenharth032f2352005-02-22 21:59:48 +00001761 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001762 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001763 SelectExpr(N);
1764 return;
1765
Chris Lattner16cd04d2005-05-12 23:24:06 +00001766 case ISD::CALLSEQ_START:
1767 case ISD::CALLSEQ_END:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001768 Select(N.getOperand(0));
1769 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001770
Chris Lattner16cd04d2005-05-12 23:24:06 +00001771 Opc = N.getOpcode() == ISD::CALLSEQ_START ? Alpha::ADJUSTSTACKDOWN :
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001772 Alpha::ADJUSTSTACKUP;
1773 BuildMI(BB, Opc, 1).addImm(Tmp1);
1774 return;
Andrew Lenharth95762122005-03-31 21:24:06 +00001775
1776 case ISD::PCMARKER:
1777 Select(N.getOperand(0)); //Chain
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001778 BuildMI(BB, Alpha::PCLABEL, 2)
1779 .addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
Andrew Lenharth95762122005-03-31 21:24:06 +00001780 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001781 }
1782 assert(0 && "Should not be reached!");
1783}
1784
1785
1786/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
1787/// into a machine code representation using pattern matching and a machine
1788/// description file.
1789///
1790FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001791 return new AlphaISel(TM);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001792}
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001793