blob: 2f04646bfdc8a8b5237db7c0341bc0638100d1fe [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
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000085 /// InstructionSelectBasicBlock - This callback is invoked by
86 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
87 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
Andrew Lenharth032f2352005-02-22 21:59:48 +000088 DEBUG(BB->dump());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +000089 count_ins = 0;
90 count_outs = 0;
Andrew Lenharth500b4db2005-04-22 13:35:18 +000091 max_depth = 0;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +000092 has_sym = false;
93
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000094 // Codegen the basic block.
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000095 ISelDAG = &DAG;
Andrew Lenharth500b4db2005-04-22 13:35:18 +000096 max_depth = DAG.getRoot().getNodeDepth();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000097 Select(DAG.getRoot());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +000098
99 if(has_sym)
100 ++count_ins;
101 if(EnableAlphaCount)
Jeff Cohen00b168892005-07-27 06:12:32 +0000102 std::cerr << "COUNT: "
103 << BB->getParent()->getFunction ()->getName() << " "
104 << BB->getNumber() << " "
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000105 << max_depth << " "
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000106 << count_ins << " "
107 << count_outs << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000108
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000109 // Clear state used for selection.
110 ExprMap.clear();
111 CCInvMap.clear();
112 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000113
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000114 unsigned SelectExpr(SDOperand N);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000115 void Select(SDOperand N);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000116
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000117 void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
118 void SelectBranchCC(SDOperand N);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000119 void MoveFP2Int(unsigned src, unsigned dst, bool isDouble);
120 void MoveInt2FP(unsigned src, unsigned dst, bool isDouble);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000121 //returns whether the sense of the comparison was inverted
122 bool SelectFPSetCC(SDOperand N, unsigned dst);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000123
124 // dag -> dag expanders for integer divide by constant
125 SDOperand BuildSDIVSequence(SDOperand N);
126 SDOperand BuildUDIVSequence(SDOperand N);
127
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000128};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000129}
130
Andrew Lenharthd2284272005-08-15 14:31:37 +0000131static bool isSIntImmediate(SDOperand N, int64_t& Imm) {
132 // test for constant
133 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
134 // retrieve value
135 Imm = CN->getSignExtended();
136 // passes muster
137 return true;
138 }
139 // not a constant
140 return false;
141}
142
143// isSIntImmediateBounded - This method tests to see if a constant operand
144// bounded s.t. low <= Imm <= high
145// If so Imm will receive the 64 bit value.
146static bool isSIntImmediateBounded(SDOperand N, int64_t& Imm,
147 int64_t low, int64_t high) {
Andrew Lenharth035b8ab2005-08-17 00:47:24 +0000148 if (isSIntImmediate(N, Imm) && Imm <= high && Imm >= low)
Andrew Lenharthd2284272005-08-15 14:31:37 +0000149 return true;
150 return false;
151}
152static bool isUIntImmediate(SDOperand N, uint64_t& Imm) {
153 // test for constant
154 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
155 // retrieve value
156 Imm = (uint64_t)CN->getValue();
157 // passes muster
158 return true;
159 }
160 // not a constant
161 return false;
162}
163
164static bool isUIntImmediateBounded(SDOperand N, uint64_t& Imm,
165 uint64_t low, uint64_t high) {
Andrew Lenharth035b8ab2005-08-17 00:47:24 +0000166 if (isUIntImmediate(N, Imm) && Imm <= high && Imm >= low)
Andrew Lenharthd2284272005-08-15 14:31:37 +0000167 return true;
168 return false;
169}
170
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000171static void getValueInfo(const Value* v, int& type, int& fun, int& offset)
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000172{
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000173 fun = type = offset = 0;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000174 if (v == NULL) {
175 type = 0;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000176 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(v)) {
177 type = 1;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000178 const Module* M = GV->getParent();
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000179 for(Module::const_global_iterator ii = M->global_begin(); &*ii != GV; ++ii)
180 ++offset;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000181 } else if (const Argument* Arg = dyn_cast<Argument>(v)) {
182 type = 2;
183 const Function* F = Arg->getParent();
184 const Module* M = F->getParent();
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000185 for(Module::const_iterator ii = M->begin(); &*ii != F; ++ii)
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000186 ++fun;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000187 for(Function::const_arg_iterator ii = F->arg_begin(); &*ii != Arg; ++ii)
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000188 ++offset;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000189 } else if (const Instruction* I = dyn_cast<Instruction>(v)) {
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +0000190 assert(dyn_cast<PointerType>(I->getType()));
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000191 type = 3;
192 const BasicBlock* bb = I->getParent();
193 const Function* F = bb->getParent();
194 const Module* M = F->getParent();
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000195 for(Module::const_iterator ii = M->begin(); &*ii != F; ++ii)
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000196 ++fun;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000197 for(Function::const_iterator ii = F->begin(); &*ii != bb; ++ii)
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000198 offset += ii->size();
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000199 for(BasicBlock::const_iterator ii = bb->begin(); &*ii != I; ++ii)
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000200 ++offset;
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +0000201 } else if (const Constant* C = dyn_cast<Constant>(v)) {
202 //Don't know how to look these up yet
203 type = 0;
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +0000204 } else {
205 assert(0 && "Error in value marking");
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000206 }
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000207 //type = 4: register spilling
208 //type = 5: global address loading or constant loading
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000209}
210
211static int getUID()
212{
213 static int id = 0;
214 return ++id;
215}
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000216
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000217//Factorize a number using the list of constants
218static bool factorize(int v[], int res[], int size, uint64_t c)
219{
220 bool cont = true;
221 while (c != 1 && cont)
222 {
223 cont = false;
224 for(int i = 0; i < size; ++i)
225 {
226 if (c % v[i] == 0)
227 {
228 c /= v[i];
229 ++res[i];
230 cont=true;
231 }
232 }
233 }
234 return c == 1;
235}
236
237
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000238//These describe LDAx
Andrew Lenharthc0513832005-03-29 19:24:04 +0000239static const int IMM_LOW = -32768;
240static const int IMM_HIGH = 32767;
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000241static const int IMM_MULT = 65536;
242
243static long getUpper16(long l)
244{
245 long y = l / IMM_MULT;
246 if (l % IMM_MULT > IMM_HIGH)
247 ++y;
248 return y;
249}
250
251static long getLower16(long l)
252{
253 long h = getUpper16(l);
254 return l - h * IMM_MULT;
255}
256
Andrew Lenharthfe895e32005-06-27 17:15:36 +0000257static unsigned GetRelVersion(unsigned opcode)
258{
259 switch (opcode) {
260 default: assert(0 && "unknown load or store"); return 0;
261 case Alpha::LDQ: return Alpha::LDQr;
262 case Alpha::LDS: return Alpha::LDSr;
263 case Alpha::LDT: return Alpha::LDTr;
264 case Alpha::LDL: return Alpha::LDLr;
265 case Alpha::LDBU: return Alpha::LDBUr;
266 case Alpha::LDWU: return Alpha::LDWUr;
Andrew Lenharthfce587e2005-06-29 00:39:17 +0000267 case Alpha::STB: return Alpha::STBr;
268 case Alpha::STW: return Alpha::STWr;
269 case Alpha::STL: return Alpha::STLr;
270 case Alpha::STQ: return Alpha::STQr;
271 case Alpha::STS: return Alpha::STSr;
272 case Alpha::STT: return Alpha::STTr;
273
Andrew Lenharthfe895e32005-06-27 17:15:36 +0000274 }
275}
Andrew Lenharth65838902005-02-06 16:22:15 +0000276
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000277void AlphaISel::MoveFP2Int(unsigned src, unsigned dst, bool isDouble)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000278{
279 unsigned Opc;
Andrew Lenharth120ab482005-09-29 22:54:56 +0000280 if (TLI.getTargetMachine().getSubtarget<AlphaSubtarget>().hasF2I()) {
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000281 Opc = isDouble ? Alpha::FTOIT : Alpha::FTOIS;
Andrew Lenharth98169be2005-07-28 18:14:47 +0000282 BuildMI(BB, Opc, 1, dst).addReg(src).addReg(Alpha::F31);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000283 } else {
284 //The hard way:
285 // Spill the integer to memory and reload it from there.
286 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
287 MachineFunction *F = BB->getParent();
288 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
289
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000290 if (EnableAlphaLSMark)
291 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
292 .addImm(getUID());
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000293 Opc = isDouble ? Alpha::STT : Alpha::STS;
294 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000295
296 if (EnableAlphaLSMark)
297 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
298 .addImm(getUID());
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000299 Opc = isDouble ? Alpha::LDQ : Alpha::LDL;
300 BuildMI(BB, Alpha::LDQ, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
301 }
302}
303
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000304void AlphaISel::MoveInt2FP(unsigned src, unsigned dst, bool isDouble)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000305{
306 unsigned Opc;
Andrew Lenharth120ab482005-09-29 22:54:56 +0000307 if (TLI.getTargetMachine().getSubtarget<AlphaSubtarget>().hasF2I()) {
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000308 Opc = isDouble?Alpha::ITOFT:Alpha::ITOFS;
Andrew Lenharth98169be2005-07-28 18:14:47 +0000309 BuildMI(BB, Opc, 1, dst).addReg(src).addReg(Alpha::R31);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000310 } else {
311 //The hard way:
312 // Spill the integer to memory and reload it from there.
313 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
314 MachineFunction *F = BB->getParent();
315 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
316
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000317 if (EnableAlphaLSMark)
318 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
319 .addImm(getUID());
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000320 Opc = isDouble ? Alpha::STQ : Alpha::STL;
321 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000322
323 if (EnableAlphaLSMark)
324 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
325 .addImm(getUID());
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000326 Opc = isDouble ? Alpha::LDT : Alpha::LDS;
327 BuildMI(BB, Opc, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
328 }
329}
330
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000331bool AlphaISel::SelectFPSetCC(SDOperand N, unsigned dst)
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000332{
Chris Lattner88ac32c2005-08-09 20:21:10 +0000333 SDNode *SetCC = N.Val;
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000334 unsigned Opc, Tmp1, Tmp2, Tmp3;
Chris Lattner88ac32c2005-08-09 20:21:10 +0000335 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC->getOperand(2))->get();
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000336 bool rev = false;
337 bool inv = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000338
Chris Lattner88ac32c2005-08-09 20:21:10 +0000339 switch (CC) {
340 default: SetCC->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000341 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
342 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
343 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
344 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
345 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
346 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
347 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000348
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000349 ConstantFPSDNode *CN;
350 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
351 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
352 Tmp1 = Alpha::F31;
353 else
354 Tmp1 = SelectExpr(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000355
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000356 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
357 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
358 Tmp2 = Alpha::F31;
359 else
Chris Lattner9c9183a2005-04-30 04:44:07 +0000360 Tmp2 = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000361
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000362 //Can only compare doubles, and dag won't promote for me
363 if (SetCC->getOperand(0).getValueType() == MVT::f32)
364 {
365 //assert(0 && "Setcc On float?\n");
366 std::cerr << "Setcc on float!\n";
367 Tmp3 = MakeReg(MVT::f64);
Andrew Lenharth98169be2005-07-28 18:14:47 +0000368 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000369 Tmp1 = Tmp3;
370 }
371 if (SetCC->getOperand(1).getValueType() == MVT::f32)
372 {
373 //assert (0 && "Setcc On float?\n");
374 std::cerr << "Setcc on float!\n";
375 Tmp3 = MakeReg(MVT::f64);
Andrew Lenharth98169be2005-07-28 18:14:47 +0000376 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Alpha::F31).addReg(Tmp2);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000377 Tmp2 = Tmp3;
378 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000379
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000380 if (rev) std::swap(Tmp1, Tmp2);
381 //do the comparison
382 BuildMI(BB, Opc, 2, dst).addReg(Tmp1).addReg(Tmp2);
383 return inv;
384}
385
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000386//Check to see if the load is a constant offset from a base register
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000387void AlphaISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000388{
389 unsigned opcode = N.getOpcode();
Andrew Lenharth694c2982005-06-26 23:01:11 +0000390 if (opcode == ISD::ADD && N.getOperand(1).getOpcode() == ISD::Constant &&
391 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
392 { //Normal imm add
393 Reg = SelectExpr(N.getOperand(0));
394 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
395 return;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000396 }
397 Reg = SelectExpr(N);
398 offset = 0;
399 return;
400}
401
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000402void AlphaISel::SelectBranchCC(SDOperand N)
Andrew Lenharth445171a2005-02-08 00:40:03 +0000403{
404 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000405 MachineBasicBlock *Dest =
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000406 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
407 unsigned Opc = Alpha::WTF;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000408
Andrew Lenharth445171a2005-02-08 00:40:03 +0000409 Select(N.getOperand(0)); //chain
410 SDOperand CC = N.getOperand(1);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000411
Andrew Lenharth445171a2005-02-08 00:40:03 +0000412 if (CC.getOpcode() == ISD::SETCC)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000413 {
Chris Lattner88ac32c2005-08-09 20:21:10 +0000414 ISD::CondCode cCode= cast<CondCodeSDNode>(CC.getOperand(2))->get();
415 if (MVT::isInteger(CC.getOperand(0).getValueType())) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000416 //Dropping the CC is only useful if we are comparing to 0
Chris Lattner88ac32c2005-08-09 20:21:10 +0000417 bool RightZero = CC.getOperand(1).getOpcode() == ISD::Constant &&
418 cast<ConstantSDNode>(CC.getOperand(1))->getValue() == 0;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000419 bool isNE = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000420
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000421 //Fix up CC
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000422 if(cCode == ISD::SETNE)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000423 isNE = true;
Andrew Lenharth445171a2005-02-08 00:40:03 +0000424
Andrew Lenharth694c2982005-06-26 23:01:11 +0000425 if (RightZero) {
Andrew Lenharth09552bf2005-06-08 18:02:21 +0000426 switch (cCode) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000427 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
428 case ISD::SETEQ: Opc = Alpha::BEQ; break;
429 case ISD::SETLT: Opc = Alpha::BLT; break;
430 case ISD::SETLE: Opc = Alpha::BLE; break;
431 case ISD::SETGT: Opc = Alpha::BGT; break;
432 case ISD::SETGE: Opc = Alpha::BGE; break;
433 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
434 case ISD::SETUGT: Opc = Alpha::BNE; break;
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000435 //Technically you could have this CC
436 case ISD::SETULE: Opc = Alpha::BEQ; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000437 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
438 case ISD::SETNE: Opc = Alpha::BNE; break;
439 }
Chris Lattner88ac32c2005-08-09 20:21:10 +0000440 unsigned Tmp1 = SelectExpr(CC.getOperand(0)); //Cond
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000441 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
442 return;
443 } else {
444 unsigned Tmp1 = SelectExpr(CC);
445 if (isNE)
446 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
447 else
448 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000449 return;
450 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000451 } else { //FP
Jeff Cohen00b168892005-07-27 06:12:32 +0000452 //Any comparison between 2 values should be codegened as an folded
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000453 //branch, as moving CC to the integer register is very expensive
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000454 //for a cmp b: c = a - b;
455 //a = b: c = 0
456 //a < b: c < 0
457 //a > b: c > 0
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000458
459 bool invTest = false;
460 unsigned Tmp3;
461
462 ConstantFPSDNode *CN;
Chris Lattner88ac32c2005-08-09 20:21:10 +0000463 if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(1)))
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000464 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
Chris Lattner88ac32c2005-08-09 20:21:10 +0000465 Tmp3 = SelectExpr(CC.getOperand(0));
466 else if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(0)))
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000467 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
468 {
Chris Lattner88ac32c2005-08-09 20:21:10 +0000469 Tmp3 = SelectExpr(CC.getOperand(1));
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000470 invTest = true;
471 }
472 else
473 {
Chris Lattner88ac32c2005-08-09 20:21:10 +0000474 unsigned Tmp1 = SelectExpr(CC.getOperand(0));
475 unsigned Tmp2 = SelectExpr(CC.getOperand(1));
476 bool isD = CC.getOperand(0).getValueType() == MVT::f64;
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000477 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
478 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
479 .addReg(Tmp1).addReg(Tmp2);
480 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000481
Chris Lattner88ac32c2005-08-09 20:21:10 +0000482 switch (cCode) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000483 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000484 case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
485 case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
486 case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
487 case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
488 case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
489 case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000490 }
491 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000492 return;
493 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000494 abort(); //Should never be reached
495 } else {
496 //Giveup and do the stupid thing
497 unsigned Tmp1 = SelectExpr(CC);
498 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
499 return;
500 }
Andrew Lenharth445171a2005-02-08 00:40:03 +0000501 abort(); //Should never be reached
502}
503
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000504unsigned AlphaISel::SelectExpr(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000505 unsigned Result;
Andrew Lenharth2966e842005-04-07 18:15:28 +0000506 unsigned Tmp1, Tmp2 = 0, Tmp3;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000507 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000508 unsigned opcode = N.getOpcode();
Andrew Lenharthd2284272005-08-15 14:31:37 +0000509 int64_t SImm;
510 uint64_t UImm;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000511
512 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000513 MVT::ValueType DestType = N.getValueType();
Andrew Lenharthf4da9452005-06-29 12:49:51 +0000514 bool isFP = DestType == MVT::f64 || DestType == MVT::f32;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000515
516 unsigned &Reg = ExprMap[N];
517 if (Reg) return Reg;
518
Andrew Lenharth46a776e2005-09-06 17:00:23 +0000519 switch(N.getOpcode()) {
520 default:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000521 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000522 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth46a776e2005-09-06 17:00:23 +0000523 break;
524 case ISD::AssertSext:
525 case ISD::AssertZext:
526 return Reg = SelectExpr(N.getOperand(0));
527 case ISD::CALL:
528 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000529 // If this is a call instruction, make sure to prepare ALL of the result
530 // values as well as the chain.
531 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000532 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000533 else {
534 Result = MakeReg(Node->getValueType(0));
535 ExprMap[N.getValue(0)] = Result;
536 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
537 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000538 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000539 }
Andrew Lenharth46a776e2005-09-06 17:00:23 +0000540 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000541 }
542
Andrew Lenharth40831c52005-01-28 06:57:18 +0000543 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000544 default:
545 Node->dump();
546 assert(0 && "Node not handled!\n");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000547
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000548 case ISD::CTPOP:
549 case ISD::CTTZ:
550 case ISD::CTLZ:
551 Opc = opcode == ISD::CTPOP ? Alpha::CTPOP :
552 (opcode == ISD::CTTZ ? Alpha::CTTZ : Alpha::CTLZ);
553 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth964b6aa2005-10-20 19:39:24 +0000554 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000555 return Result;
556
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000557 case ISD::MULHU:
558 Tmp1 = SelectExpr(N.getOperand(0));
559 Tmp2 = SelectExpr(N.getOperand(1));
560 BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth706be912005-04-07 13:55:53 +0000561 return Result;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000562 case ISD::MULHS:
563 {
564 //MULHU - Ra<63>*Rb - Rb<63>*Ra
565 Tmp1 = SelectExpr(N.getOperand(0));
566 Tmp2 = SelectExpr(N.getOperand(1));
567 Tmp3 = MakeReg(MVT::i64);
568 BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
569 unsigned V1 = MakeReg(MVT::i64);
570 unsigned V2 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000571 BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31)
572 .addReg(Tmp1);
573 BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31)
574 .addReg(Tmp2);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000575 unsigned IRes = MakeReg(MVT::i64);
576 BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
577 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
578 return Result;
579 }
Andrew Lenharth7332f3e2005-04-02 19:11:07 +0000580 case ISD::UNDEF: {
581 BuildMI(BB, Alpha::IDEF, 0, Result);
582 return Result;
583 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000584
Andrew Lenharth032f2352005-02-22 21:59:48 +0000585 case ISD::DYNAMIC_STACKALLOC:
586 // Generate both result values.
Andrew Lenharth3a7118d2005-02-23 17:33:42 +0000587 if (Result != notIn)
588 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth032f2352005-02-22 21:59:48 +0000589 else
590 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
591
592 // FIXME: We are currently ignoring the requested alignment for handling
593 // greater than the stack alignment. This will need to be revisited at some
594 // point. Align = N.getOperand(2);
595
596 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
597 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
598 std::cerr << "Cannot allocate stack object with greater alignment than"
599 << " the stack alignment yet!";
600 abort();
601 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000602
Andrew Lenharth032f2352005-02-22 21:59:48 +0000603 Select(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +0000604 if (isSIntImmediateBounded(N.getOperand(1), SImm, 0, 32767))
605 BuildMI(BB, Alpha::LDA, 2, Alpha::R30).addImm(-SImm).addReg(Alpha::R30);
606 else {
Andrew Lenharth032f2352005-02-22 21:59:48 +0000607 Tmp1 = SelectExpr(N.getOperand(1));
608 // Subtract size from stack pointer, thereby allocating some space.
609 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
610 }
611
612 // Put a pointer to the space into the result register, by copying the stack
613 // pointer.
Andrew Lenharth7bc47022005-02-22 23:29:25 +0000614 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
Andrew Lenharth032f2352005-02-22 21:59:48 +0000615 return Result;
616
Andrew Lenharth02c318e2005-06-27 21:02:56 +0000617 case ISD::ConstantPool:
Chris Lattner5839bf22005-08-26 17:15:30 +0000618 Tmp1 = BB->getParent()->getConstantPool()->
619 getConstantPoolIndex(cast<ConstantPoolSDNode>(N)->get());
Andrew Lenharth02c318e2005-06-27 21:02:56 +0000620 AlphaLowering.restoreGP(BB);
621 Tmp2 = MakeReg(MVT::i64);
622 BuildMI(BB, Alpha::LDAHr, 2, Tmp2).addConstantPoolIndex(Tmp1)
623 .addReg(Alpha::R29);
624 BuildMI(BB, Alpha::LDAr, 2, Result).addConstantPoolIndex(Tmp1)
625 .addReg(Tmp2);
626 return Result;
Andrew Lenharth2c594352005-01-29 15:42:07 +0000627
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000628 case ISD::FrameIndex:
Andrew Lenharth032f2352005-02-22 21:59:48 +0000629 BuildMI(BB, Alpha::LDA, 2, Result)
630 .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
631 .addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000632 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000633
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000634 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +0000635 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000636 case ISD::SEXTLOAD:
Misha Brukman4633f1c2005-04-21 23:13:11 +0000637 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000638 {
639 // Make sure we generate both values.
640 if (Result != notIn)
641 ExprMap[N.getValue(1)] = notIn; // Generate the token
642 else
643 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +0000644
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000645 SDOperand Chain = N.getOperand(0);
646 SDOperand Address = N.getOperand(1);
647 Select(Chain);
648
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000649 bool fpext = true;
650
Andrew Lenharth03824012005-02-07 05:55:55 +0000651 if (opcode == ISD::LOAD)
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000652 switch (Node->getValueType(0)) {
653 default: Node->dump(); assert(0 && "Bad load!");
654 case MVT::i64: Opc = Alpha::LDQ; break;
655 case MVT::f64: Opc = Alpha::LDT; break;
656 case MVT::f32: Opc = Alpha::LDS; break;
657 }
Andrew Lenharth03824012005-02-07 05:55:55 +0000658 else
Chris Lattnerbce81ae2005-07-10 01:56:13 +0000659 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000660 default: Node->dump(); assert(0 && "Bad sign extend!");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000661 case MVT::i32: Opc = Alpha::LDL;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000662 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000663 case MVT::i16: Opc = Alpha::LDWU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000664 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +0000665 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Misha Brukman4633f1c2005-04-21 23:13:11 +0000666 case MVT::i8: Opc = Alpha::LDBU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000667 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000668 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000669
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000670 int i, j, k;
671 if (EnableAlphaLSMark)
672 getValueInfo(dyn_cast<SrcValueSDNode>(N.getOperand(2))->getValue(),
673 i, j, k);
674
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000675 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Address);
676 if (GASD && !GASD->getGlobal()->isExternal()) {
677 Tmp1 = MakeReg(MVT::i64);
678 AlphaLowering.restoreGP(BB);
679 BuildMI(BB, Alpha::LDAHr, 2, Tmp1)
680 .addGlobalAddress(GASD->getGlobal()).addReg(Alpha::R29);
681 if (EnableAlphaLSMark)
682 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
683 .addImm(getUID());
684 BuildMI(BB, GetRelVersion(Opc), 2, Result)
685 .addGlobalAddress(GASD->getGlobal()).addReg(Tmp1);
Chris Lattnerbce81ae2005-07-10 01:56:13 +0000686 } else if (ConstantPoolSDNode *CP =
687 dyn_cast<ConstantPoolSDNode>(Address)) {
Chris Lattner5839bf22005-08-26 17:15:30 +0000688 unsigned CPIdx = BB->getParent()->getConstantPool()->
689 getConstantPoolIndex(CP->get());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000690 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000691 has_sym = true;
Andrew Lenharthfe895e32005-06-27 17:15:36 +0000692 Tmp1 = MakeReg(MVT::i64);
Chris Lattner5839bf22005-08-26 17:15:30 +0000693 BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CPIdx)
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000694 .addReg(Alpha::R29);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000695 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000696 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
697 .addImm(getUID());
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000698 BuildMI(BB, GetRelVersion(Opc), 2, Result)
Chris Lattner5839bf22005-08-26 17:15:30 +0000699 .addConstantPoolIndex(CPIdx).addReg(Tmp1);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000700 } else if(Address.getOpcode() == ISD::FrameIndex) {
701 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000702 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
703 .addImm(getUID());
Andrew Lenharth032f2352005-02-22 21:59:48 +0000704 BuildMI(BB, Opc, 2, Result)
705 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
706 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000707 } else {
708 long offset;
709 SelectAddr(Address, Tmp1, offset);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000710 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000711 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
712 .addImm(getUID());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000713 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
714 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000715 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000716 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000717
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000718 case ISD::GlobalAddress:
719 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000720 has_sym = true;
Jeff Cohen00b168892005-07-27 06:12:32 +0000721
Andrew Lenharth2f5bca52005-07-03 20:06:13 +0000722 Reg = Result = MakeReg(MVT::i64);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000723
724 if (EnableAlphaLSMark)
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000725 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000726 .addImm(getUID());
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000727
728 BuildMI(BB, Alpha::LDQl, 2, Result)
Andrew Lenharthc95d9842005-06-27 21:11:40 +0000729 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal())
730 .addReg(Alpha::R29);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000731 return Result;
732
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000733 case ISD::ExternalSymbol:
734 AlphaLowering.restoreGP(BB);
735 has_sym = true;
736
Andrew Lenharth2f5bca52005-07-03 20:06:13 +0000737 Reg = Result = MakeReg(MVT::i64);
738
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000739 if (EnableAlphaLSMark)
740 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
741 .addImm(getUID());
742
743 BuildMI(BB, Alpha::LDQl, 2, Result)
744 .addExternalSymbol(cast<ExternalSymbolSDNode>(N)->getSymbol())
745 .addReg(Alpha::R29);
746 return Result;
747
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +0000748 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000749 case ISD::CALL:
750 {
751 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000752
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000753 // The chain for this call is now lowered.
Andrew Lenharthf3f951a2005-07-22 20:50:29 +0000754 ExprMap[N.getValue(Node->getNumValues()-1)] = notIn;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000755
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000756 //grab the arguments
757 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000758 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000759 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000760 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000761
Andrew Lenharth684f2292005-01-30 00:35:27 +0000762 //in reg args
763 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000764 {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000765 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000766 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000767 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000768 Alpha::F19, Alpha::F20, Alpha::F21};
769 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000770 default:
771 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000772 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000773 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000774 N.getOperand(i+2).getValueType() << "\n";
775 assert(0 && "Unknown value type for call");
776 case MVT::i1:
777 case MVT::i8:
778 case MVT::i16:
779 case MVT::i32:
780 case MVT::i64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000781 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i])
782 .addReg(argvregs[i]);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000783 break;
784 case MVT::f32:
785 case MVT::f64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000786 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i])
787 .addReg(argvregs[i]);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000788 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000789 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000790 }
Andrew Lenharth684f2292005-01-30 00:35:27 +0000791 //in mem args
792 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000793 {
794 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000795 default:
796 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000797 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000798 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000799 N.getOperand(i+2).getValueType() << "\n";
800 assert(0 && "Unknown value type for call");
801 case MVT::i1:
802 case MVT::i8:
803 case MVT::i16:
804 case MVT::i32:
805 case MVT::i64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000806 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
807 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000808 break;
809 case MVT::f32:
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000810 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
811 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000812 break;
813 case MVT::f64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000814 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
815 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000816 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000817 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000818 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000819 //build the right kind of call
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000820 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(N.getOperand(1));
821 if (GASD && !GASD->getGlobal()->isExternal()) {
822 //use PC relative branch call
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000823 AlphaLowering.restoreGP(BB);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000824 BuildMI(BB, Alpha::BSR, 1, Alpha::R26)
825 .addGlobalAddress(GASD->getGlobal(),true);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000826 } else {
827 //no need to restore GP as we are doing an indirect call
828 Tmp1 = SelectExpr(N.getOperand(1));
829 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
830 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
831 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000832
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000833 //push the result into a virtual register
Misha Brukman4633f1c2005-04-21 23:13:11 +0000834
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000835 switch (Node->getValueType(0)) {
836 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000837 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000838 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +0000839 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
840 break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000841 case MVT::f32:
842 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +0000843 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
844 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000845 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000846 return Result+N.ResNo;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000847 }
848
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000849 case ISD::SIGN_EXTEND_INREG:
850 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000851 //do SDIV opt for all levels of ints if not dividing by a constant
852 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
853 && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +0000854 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +0000855 unsigned Tmp4 = MakeReg(MVT::f64);
856 unsigned Tmp5 = MakeReg(MVT::f64);
857 unsigned Tmp6 = MakeReg(MVT::f64);
858 unsigned Tmp7 = MakeReg(MVT::f64);
859 unsigned Tmp8 = MakeReg(MVT::f64);
860 unsigned Tmp9 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000861
862 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
863 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
864 MoveInt2FP(Tmp1, Tmp4, true);
865 MoveInt2FP(Tmp2, Tmp5, true);
Andrew Lenharth98169be2005-07-28 18:14:47 +0000866 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Alpha::F31).addReg(Tmp4);
867 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Alpha::F31).addReg(Tmp5);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +0000868 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
Andrew Lenharth98169be2005-07-28 18:14:47 +0000869 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Alpha::F31).addReg(Tmp8);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000870 MoveFP2Int(Tmp9, Result, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +0000871 return Result;
872 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000873
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000874 //Alpha has instructions for a bunch of signed 32 bit stuff
Chris Lattnerbce81ae2005-07-10 01:56:13 +0000875 if(cast<VTSDNode>(Node->getOperand(1))->getVT() == MVT::i32) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000876 switch (N.getOperand(0).getOpcode()) {
877 case ISD::ADD:
878 case ISD::SUB:
879 case ISD::MUL:
880 {
881 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
882 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
883 //FIXME: first check for Scaled Adds and Subs!
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000884 if(!isMul && N.getOperand(0).getOperand(0).getOpcode() == ISD::SHL &&
Andrew Lenharthd2284272005-08-15 14:31:37 +0000885 isSIntImmediateBounded(N.getOperand(0).getOperand(0).getOperand(1), SImm, 2, 3))
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000886 {
Andrew Lenharthd2284272005-08-15 14:31:37 +0000887 bool use4 = SImm == 2;
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000888 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
889 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
890 BuildMI(BB, isAdd?(use4?Alpha::S4ADDL:Alpha::S8ADDL):(use4?Alpha::S4SUBL:Alpha::S8SUBL),
891 2,Result).addReg(Tmp1).addReg(Tmp2);
892 }
893 else if(isAdd && N.getOperand(0).getOperand(1).getOpcode() == ISD::SHL &&
Andrew Lenharthd2284272005-08-15 14:31:37 +0000894 isSIntImmediateBounded(N.getOperand(0).getOperand(1).getOperand(1), SImm, 2, 3))
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000895 {
Andrew Lenharthd2284272005-08-15 14:31:37 +0000896 bool use4 = SImm == 2;
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000897 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
898 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
899 BuildMI(BB, use4?Alpha::S4ADDL:Alpha::S8ADDL, 2,Result).addReg(Tmp1).addReg(Tmp2);
900 }
Andrew Lenharthd2284272005-08-15 14:31:37 +0000901 else if(isSIntImmediateBounded(N.getOperand(0).getOperand(1), SImm, 0, 255))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000902 { //Normal imm add/sub
903 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000904 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +0000905 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000906 }
Andrew Lenharthd2284272005-08-15 14:31:37 +0000907 else if(!isMul && isSIntImmediate(N.getOperand(0).getOperand(1), SImm) &&
908 (((SImm << 32) >> 32) >= -255) && (((SImm << 32) >> 32) <= 0))
Andrew Lenharth6b137d82005-07-22 22:24:01 +0000909 { //handle canonicalization
910 Opc = isAdd ? Alpha::SUBLi : Alpha::ADDLi;
911 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +0000912 SImm = 0 - ((SImm << 32) >> 32);
913 assert(SImm >= 0 && SImm <= 255);
914 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharth6b137d82005-07-22 22:24:01 +0000915 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000916 else
917 { //Normal add/sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000918 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULL : Alpha::SUBL);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000919 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000920 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000921 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
922 }
923 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000924 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000925 default: break; //Fall Though;
926 }
927 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000928 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000929 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Chris Lattnerbce81ae2005-07-10 01:56:13 +0000930 switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000931 default:
932 Node->dump();
933 assert(0 && "Sign Extend InReg not there yet");
934 break;
935 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000936 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000937 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000938 break;
939 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000940 case MVT::i16:
Andrew Lenharth964b6aa2005-10-20 19:39:24 +0000941 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000942 break;
943 case MVT::i8:
Andrew Lenharth964b6aa2005-10-20 19:39:24 +0000944 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000945 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +0000946 case MVT::i1:
947 Tmp2 = MakeReg(MVT::i64);
948 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000949 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +0000950 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000951 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000952 return Result;
953 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000954
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000955 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000956 {
Chris Lattner88ac32c2005-08-09 20:21:10 +0000957 ISD::CondCode CC = cast<CondCodeSDNode>(N.getOperand(2))->get();
958 if (MVT::isInteger(N.getOperand(0).getValueType())) {
959 bool isConst = false;
960 int dir;
Misha Brukman7847fca2005-04-22 17:54:37 +0000961
Chris Lattner88ac32c2005-08-09 20:21:10 +0000962 //Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +0000963 if(isSIntImmediate(N.getOperand(1), SImm) && SImm <= 255 && SImm >= 0)
Chris Lattner88ac32c2005-08-09 20:21:10 +0000964 isConst = true;
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000965
Chris Lattner88ac32c2005-08-09 20:21:10 +0000966 switch (CC) {
967 default: Node->dump(); assert(0 && "Unknown integer comparison!");
968 case ISD::SETEQ:
969 Opc = isConst ? Alpha::CMPEQi : Alpha::CMPEQ; dir=1; break;
970 case ISD::SETLT:
971 Opc = isConst ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
972 case ISD::SETLE:
973 Opc = isConst ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
974 case ISD::SETGT: Opc = Alpha::CMPLT; dir = 2; break;
975 case ISD::SETGE: Opc = Alpha::CMPLE; dir = 2; break;
976 case ISD::SETULT:
977 Opc = isConst ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
978 case ISD::SETUGT: Opc = Alpha::CMPULT; dir = 2; break;
979 case ISD::SETULE:
980 Opc = isConst ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
981 case ISD::SETUGE: Opc = Alpha::CMPULE; dir = 2; break;
982 case ISD::SETNE: {//Handle this one special
983 //std::cerr << "Alpha does not have a setne.\n";
984 //abort();
985 Tmp1 = SelectExpr(N.getOperand(0));
986 Tmp2 = SelectExpr(N.getOperand(1));
987 Tmp3 = MakeReg(MVT::i64);
988 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
989 //Remeber we have the Inv for this CC
990 CCInvMap[N] = Tmp3;
991 //and invert
992 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
993 return Result;
994 }
995 }
996 if (dir == 1) {
997 Tmp1 = SelectExpr(N.getOperand(0));
998 if (isConst) {
Andrew Lenharthd2284272005-08-15 14:31:37 +0000999 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Chris Lattner88ac32c2005-08-09 20:21:10 +00001000 } else {
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001001 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth694c2982005-06-26 23:01:11 +00001002 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001003 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001004 } else { //if (dir == 2) {
1005 Tmp1 = SelectExpr(N.getOperand(1));
1006 Tmp2 = SelectExpr(N.getOperand(0));
1007 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001008 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001009 } else {
1010 //do the comparison
1011 Tmp1 = MakeReg(MVT::f64);
1012 bool inv = SelectFPSetCC(N, Tmp1);
1013
1014 //now arrange for Result (int) to have a 1 or 0
1015 Tmp2 = MakeReg(MVT::i64);
1016 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
1017 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
1018 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
Andrew Lenharth9818c052005-02-05 13:19:12 +00001019 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001020 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001021 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001022
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001023 case ISD::CopyFromReg:
1024 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001025 ++count_ins;
1026
Andrew Lenharth40831c52005-01-28 06:57:18 +00001027 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001028 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001029 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001030 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001031 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001032
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001033 SDOperand Chain = N.getOperand(0);
1034
1035 Select(Chain);
Chris Lattner707ebc52005-08-16 21:56:37 +00001036 unsigned r = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001037 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
Andrew Lenharth619fb522005-07-04 20:07:21 +00001038 if (MVT::isFloatingPoint(N.getValue(0).getValueType()))
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001039 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
1040 else
1041 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001042 return Result;
1043 }
1044
Misha Brukman4633f1c2005-04-21 23:13:11 +00001045 //Most of the plain arithmetic and logic share the same form, and the same
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001046 //constant immediate test
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001047 case ISD::XOR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001048 //Match Not
Andrew Lenharthd2284272005-08-15 14:31:37 +00001049 if (isSIntImmediate(N.getOperand(1), SImm) && SImm == -1) {
1050 Tmp1 = SelectExpr(N.getOperand(0));
1051 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1052 return Result;
1053 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001054 //Fall through
1055 case ISD::AND:
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001056 //handle zap
Andrew Lenharthd2284272005-08-15 14:31:37 +00001057 if (opcode == ISD::AND && isUIntImmediate(N.getOperand(1), UImm))
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001058 {
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001059 unsigned int build = 0;
1060 for(int i = 0; i < 8; ++i)
1061 {
Andrew Lenharthd2284272005-08-15 14:31:37 +00001062 if ((UImm & 0x00FF) == 0x00FF)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001063 build |= 1 << i;
Andrew Lenharthd2284272005-08-15 14:31:37 +00001064 else if ((UImm & 0x00FF) != 0)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001065 { build = 0; break; }
Andrew Lenharthd2284272005-08-15 14:31:37 +00001066 UImm >>= 8;
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001067 }
1068 if (build)
1069 {
1070 Tmp1 = SelectExpr(N.getOperand(0));
1071 BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1072 return Result;
1073 }
1074 }
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001075 case ISD::OR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001076 //Check operand(0) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001077 if (N.getOperand(0).getOpcode() == ISD::XOR &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001078 isSIntImmediate(N.getOperand(0).getOperand(1), SImm) && SImm == -1) {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001079 switch(opcode) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001080 case ISD::AND: Opc = Alpha::BIC; break;
1081 case ISD::OR: Opc = Alpha::ORNOT; break;
1082 case ISD::XOR: Opc = Alpha::EQV; break;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001083 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001084 Tmp1 = SelectExpr(N.getOperand(1));
1085 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1086 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1087 return Result;
1088 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001089 //Check operand(1) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001090 if (N.getOperand(1).getOpcode() == ISD::XOR &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001091 isSIntImmediate(N.getOperand(1).getOperand(1), SImm) && SImm == -1) {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001092 switch(opcode) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001093 case ISD::AND: Opc = Alpha::BIC; break;
1094 case ISD::OR: Opc = Alpha::ORNOT; break;
1095 case ISD::XOR: Opc = Alpha::EQV; break;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001096 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001097 Tmp1 = SelectExpr(N.getOperand(0));
1098 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1099 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1100 return Result;
1101 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001102 //Fall through
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001103 case ISD::SHL:
1104 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001105 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001106 case ISD::MUL:
Andrew Lenharthd2284272005-08-15 14:31:37 +00001107 if(isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255)) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001108 switch(opcode) {
1109 case ISD::AND: Opc = Alpha::ANDi; break;
1110 case ISD::OR: Opc = Alpha::BISi; break;
1111 case ISD::XOR: Opc = Alpha::XORi; break;
1112 case ISD::SHL: Opc = Alpha::SLi; break;
1113 case ISD::SRL: Opc = Alpha::SRLi; break;
1114 case ISD::SRA: Opc = Alpha::SRAi; break;
1115 case ISD::MUL: Opc = Alpha::MULQi; break;
1116 };
1117 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001118 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001119 } else {
1120 switch(opcode) {
1121 case ISD::AND: Opc = Alpha::AND; break;
1122 case ISD::OR: Opc = Alpha::BIS; break;
1123 case ISD::XOR: Opc = Alpha::XOR; break;
1124 case ISD::SHL: Opc = Alpha::SL; break;
1125 case ISD::SRL: Opc = Alpha::SRL; break;
1126 case ISD::SRA: Opc = Alpha::SRA; break;
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001127 case ISD::MUL: Opc = Alpha::MULQ; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001128 };
1129 Tmp1 = SelectExpr(N.getOperand(0));
1130 Tmp2 = SelectExpr(N.getOperand(1));
1131 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1132 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001133 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001134
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001135 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001136 case ISD::SUB:
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001137 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001138 bool isAdd = opcode == ISD::ADD;
1139
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001140 //first check for Scaled Adds and Subs!
1141 //Valid for add and sub
Andrew Lenharthd2284272005-08-15 14:31:37 +00001142 if(N.getOperand(0).getOpcode() == ISD::SHL &&
1143 isSIntImmediate(N.getOperand(0).getOperand(1), SImm) &&
1144 (SImm == 2 || SImm == 3)) {
1145 bool use4 = SImm == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001146 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001147 if (isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255))
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001148 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
Andrew Lenharthd2284272005-08-15 14:31:37 +00001149 2, Result).addReg(Tmp2).addImm(SImm);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001150 else {
1151 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001152 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1153 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001154 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001155 }
1156 //Position prevents subs
Andrew Lenharth273a1f92005-04-07 14:18:13 +00001157 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001158 isSIntImmediate(N.getOperand(1).getOperand(1), SImm) &&
1159 (SImm == 2 || SImm == 3)) {
1160 bool use4 = SImm == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001161 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001162 if (isSIntImmediateBounded(N.getOperand(0), SImm, 0, 255))
1163 BuildMI(BB, use4?Alpha::S4ADDQi:Alpha::S8ADDQi, 2, Result).addReg(Tmp2).addImm(SImm);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001164 else {
1165 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001166 BuildMI(BB, use4?Alpha::S4ADDQ:Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001167 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001168 }
1169 //small addi
Andrew Lenharthd2284272005-08-15 14:31:37 +00001170 else if(isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001171 { //Normal imm add/sub
1172 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1173 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001174 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001175 }
Andrew Lenharthd2284272005-08-15 14:31:37 +00001176 else if(isSIntImmediateBounded(N.getOperand(1), SImm, -255, 0))
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001177 { //inverted imm add/sub
1178 Opc = isAdd ? Alpha::SUBQi : Alpha::ADDQi;
1179 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001180 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(-SImm);
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001181 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001182 //larger addi
Andrew Lenharthd2284272005-08-15 14:31:37 +00001183 else if(isSIntImmediateBounded(N.getOperand(1), SImm, -32767, 32767))
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001184 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001185 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001186 if (!isAdd)
Andrew Lenharthd2284272005-08-15 14:31:37 +00001187 SImm = -SImm;
1188 BuildMI(BB, Alpha::LDA, 2, Result).addImm(SImm).addReg(Tmp1);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001189 }
1190 //give up and do the operation
1191 else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001192 //Normal add/sub
1193 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1194 Tmp1 = SelectExpr(N.getOperand(0));
1195 Tmp2 = SelectExpr(N.getOperand(1));
1196 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1197 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001198 return Result;
1199 }
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001200 case ISD::FADD:
1201 case ISD::FSUB:
1202 case ISD::FMUL:
1203 case ISD::FDIV: {
1204 if (opcode == ISD::FADD)
1205 Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS;
1206 else if (opcode == ISD::FSUB)
1207 Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS;
1208 else if (opcode == ISD::FMUL)
1209 Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS;
1210 else
1211 Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS;
1212 Tmp1 = SelectExpr(N.getOperand(0));
1213 Tmp2 = SelectExpr(N.getOperand(1));
1214 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1215 return Result;
1216 }
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001217 case ISD::SDIV:
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001218 {
Andrew Lenhartha565c272005-04-06 22:03:13 +00001219 //check if we can convert into a shift!
Andrew Lenharthd2284272005-08-15 14:31:37 +00001220 if (isSIntImmediate(N.getOperand(1), SImm) &&
1221 SImm != 0 && isPowerOf2_64(llabs(SImm))) {
1222 unsigned k = Log2_64(llabs(SImm));
Andrew Lenhartha565c272005-04-06 22:03:13 +00001223 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenhartha565c272005-04-06 22:03:13 +00001224 if (k == 1)
1225 Tmp2 = Tmp1;
1226 else
1227 {
1228 Tmp2 = MakeReg(MVT::i64);
1229 BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
1230 }
1231 Tmp3 = MakeReg(MVT::i64);
1232 BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
1233 unsigned Tmp4 = MakeReg(MVT::i64);
1234 BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharthd2284272005-08-15 14:31:37 +00001235 if (SImm > 0)
Andrew Lenhartha565c272005-04-06 22:03:13 +00001236 BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
1237 else
1238 {
1239 unsigned Tmp5 = MakeReg(MVT::i64);
1240 BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
1241 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
1242 }
1243 return Result;
1244 }
1245 }
1246 //Else fall through
Andrew Lenhartha565c272005-04-06 22:03:13 +00001247 case ISD::UDIV:
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001248 //else fall though
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001249 case ISD::UREM:
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001250 case ISD::SREM: {
1251 const char* opstr = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001252 switch(opcode) {
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001253 case ISD::UREM: opstr = "__remqu"; break;
1254 case ISD::SREM: opstr = "__remq"; break;
1255 case ISD::UDIV: opstr = "__divqu"; break;
1256 case ISD::SDIV: opstr = "__divq"; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001257 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001258 Tmp1 = SelectExpr(N.getOperand(0));
1259 Tmp2 = SelectExpr(N.getOperand(1));
Jeff Cohen00b168892005-07-27 06:12:32 +00001260 SDOperand Addr =
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001261 ISelDAG->getExternalSymbol(opstr, AlphaLowering.getPointerTy());
1262 Tmp3 = SelectExpr(Addr);
Andrew Lenharth33819132005-03-04 20:09:23 +00001263 //set up regs explicitly (helps Reg alloc)
1264 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001265 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001266 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp3).addReg(Tmp3);
1267 BuildMI(BB, Alpha::JSRs, 2, Alpha::R23).addReg(Alpha::R27).addImm(0);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001268 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001269 return Result;
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001270 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001271
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001272 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001273 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001274 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001275 assert (DestType == MVT::i64 && "only quads can be loaded to");
1276 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00001277 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001278 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001279 if (SrcType == MVT::f32)
Misha Brukman7847fca2005-04-22 17:54:37 +00001280 {
1281 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth98169be2005-07-28 18:14:47 +00001282 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Alpha::F31).addReg(Tmp1);
Misha Brukman7847fca2005-04-22 17:54:37 +00001283 Tmp1 = Tmp2;
1284 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001285 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth98169be2005-07-28 18:14:47 +00001286 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001287 MoveFP2Int(Tmp2, Result, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001288
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001289 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001290 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001291
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001292 case ISD::SELECT:
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001293 if (isFP) {
1294 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1295 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
1296 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
1297
1298 SDOperand CC = N.getOperand(0);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001299
Chris Lattner88ac32c2005-08-09 20:21:10 +00001300 if (CC.getOpcode() == ISD::SETCC &&
1301 !MVT::isInteger(CC.getOperand(0).getValueType())) {
1302 //FP Setcc -> Select yay!
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001303
Jeff Cohen00b168892005-07-27 06:12:32 +00001304
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001305 //for a cmp b: c = a - b;
1306 //a = b: c = 0
1307 //a < b: c < 0
1308 //a > b: c > 0
1309
1310 bool invTest = false;
1311 unsigned Tmp3;
1312
1313 ConstantFPSDNode *CN;
Chris Lattner88ac32c2005-08-09 20:21:10 +00001314 if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(1)))
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001315 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
Chris Lattner88ac32c2005-08-09 20:21:10 +00001316 Tmp3 = SelectExpr(CC.getOperand(0));
1317 else if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(0)))
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001318 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1319 {
Chris Lattner88ac32c2005-08-09 20:21:10 +00001320 Tmp3 = SelectExpr(CC.getOperand(1));
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001321 invTest = true;
1322 }
1323 else
1324 {
Chris Lattner88ac32c2005-08-09 20:21:10 +00001325 unsigned Tmp1 = SelectExpr(CC.getOperand(0));
1326 unsigned Tmp2 = SelectExpr(CC.getOperand(1));
1327 bool isD = CC.getOperand(0).getValueType() == MVT::f64;
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001328 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1329 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1330 .addReg(Tmp1).addReg(Tmp2);
1331 }
1332
Chris Lattner88ac32c2005-08-09 20:21:10 +00001333 switch (cast<CondCodeSDNode>(CC.getOperand(2))->get()) {
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001334 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
1335 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNE : Alpha::FCMOVEQ; break;
1336 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGT : Alpha::FCMOVLT; break;
1337 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGE : Alpha::FCMOVLE; break;
1338 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLT : Alpha::FCMOVGT; break;
1339 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLE : Alpha::FCMOVGE; break;
1340 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQ : Alpha::FCMOVNE; break;
1341 }
1342 BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
1343 return Result;
1344 }
1345 else
1346 {
1347 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1348 BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV)
1349 .addReg(Tmp1);
1350// // Spill the cond to memory and reload it from there.
1351// unsigned Tmp4 = MakeReg(MVT::f64);
1352// MoveIntFP(Tmp1, Tmp4, true);
1353// //now ideally, we don't have to do anything to the flag...
1354// // Get the condition into the zero flag.
1355// BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
1356 return Result;
Jeff Cohen00b168892005-07-27 06:12:32 +00001357 }
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001358 } else {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001359 //FIXME: look at parent to decide if intCC can be folded, or if setCC(FP)
1360 //and can save stack use
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001361 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001362 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1363 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001364 // Get the condition into the zero flag.
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001365 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001366
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001367 SDOperand CC = N.getOperand(0);
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001368
Misha Brukman4633f1c2005-04-21 23:13:11 +00001369 if (CC.getOpcode() == ISD::SETCC &&
Chris Lattner88ac32c2005-08-09 20:21:10 +00001370 !MVT::isInteger(CC.getOperand(0).getValueType()))
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001371 { //FP Setcc -> Int Select
Misha Brukman7847fca2005-04-22 17:54:37 +00001372 Tmp1 = MakeReg(MVT::f64);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001373 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1374 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Misha Brukman7847fca2005-04-22 17:54:37 +00001375 bool inv = SelectFPSetCC(CC, Tmp1);
1376 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
1377 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
1378 return Result;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001379 }
1380 if (CC.getOpcode() == ISD::SETCC) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001381 //Int SetCC -> Select
1382 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharthd2284272005-08-15 14:31:37 +00001383 if(isSIntImmediateBounded(CC.getOperand(1), SImm, 0, 0)) {
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001384 //figure out a few things
Andrew Lenharthd2284272005-08-15 14:31:37 +00001385 bool useImm = isSIntImmediateBounded(N.getOperand(2), SImm, 0, 255);
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001386
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001387 //Fix up CC
Chris Lattner88ac32c2005-08-09 20:21:10 +00001388 ISD::CondCode cCode= cast<CondCodeSDNode>(CC.getOperand(2))->get();
Andrew Lenharth694c2982005-06-26 23:01:11 +00001389 if (useImm) //Invert sense to get Imm field right
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001390 cCode = ISD::getSetCCInverse(cCode, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001391
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001392 //Choose the CMOV
1393 switch (cCode) {
1394 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001395 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
1396 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
1397 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
1398 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
1399 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
1400 case ISD::SETULT: assert(0 && "unsigned < 0 is never true"); break;
1401 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
1402 //Technically you could have this CC
1403 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
1404 case ISD::SETUGE: assert(0 && "unsgined >= 0 is always true"); break;
1405 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001406 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001407 Tmp1 = SelectExpr(CC.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001408
Andrew Lenharth694c2982005-06-26 23:01:11 +00001409 if (useImm) {
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001410 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
Andrew Lenharthd2284272005-08-15 14:31:37 +00001411 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addImm(SImm).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001412 } else {
1413 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1414 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1415 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
1416 }
1417 return Result;
1418 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001419 //Otherwise, fall though
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001420 }
1421 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001422 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1423 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001424 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3)
1425 .addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001426
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001427 return Result;
1428 }
1429
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001430 case ISD::Constant:
1431 {
Andrew Lenharthc0513832005-03-29 19:24:04 +00001432 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001433 int zero_extend_top = 0;
Andrew Lenharthf075cac2005-07-23 07:46:48 +00001434 if (val > 0 && (val & 0xFFFFFFFF00000000ULL) == 0 &&
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001435 ((int32_t)val < 0)) {
1436 //try a small load and zero extend
1437 val = (int32_t)val;
1438 zero_extend_top = 15;
1439 }
1440
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00001441 if (val <= IMM_HIGH && val >= IMM_LOW) {
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001442 if(!zero_extend_top)
1443 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
1444 else {
1445 Tmp1 = MakeReg(MVT::i64);
1446 BuildMI(BB, Alpha::LDA, 2, Tmp1).addImm(val).addReg(Alpha::R31);
1447 BuildMI(BB, Alpha::ZAPNOT, 2, Result).addReg(Tmp1).addImm(zero_extend_top);
1448 }
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00001449 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001450 else if (val <= (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT &&
1451 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
1452 Tmp1 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001453 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val))
1454 .addReg(Alpha::R31);
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001455 if (!zero_extend_top)
1456 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
1457 else {
1458 Tmp3 = MakeReg(MVT::i64);
1459 BuildMI(BB, Alpha::LDA, 2, Tmp3).addImm(getLower16(val)).addReg(Tmp1);
1460 BuildMI(BB, Alpha::ZAPNOT, 2, Result).addReg(Tmp3).addImm(zero_extend_top);
1461 }
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00001462 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001463 else {
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001464 //re-get the val since we are going to mem anyway
1465 val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001466 MachineConstantPool *CP = BB->getParent()->getConstantPool();
Jeff Cohen00b168892005-07-27 06:12:32 +00001467 ConstantUInt *C =
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001468 ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001469 unsigned CPI = CP->getConstantPoolIndex(C);
1470 AlphaLowering.restoreGP(BB);
Andrew Lenharthfe895e32005-06-27 17:15:36 +00001471 has_sym = true;
1472 Tmp1 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001473 BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CPI)
1474 .addReg(Alpha::R29);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001475 if (EnableAlphaLSMark)
1476 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
1477 .addImm(getUID());
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001478 BuildMI(BB, Alpha::LDQr, 2, Result).addConstantPoolIndex(CPI)
1479 .addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001480 }
1481 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001482 }
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001483 case ISD::FNEG:
1484 if(ISD::FABS == N.getOperand(0).getOpcode())
1485 {
1486 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1487 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
1488 } else {
1489 Tmp1 = SelectExpr(N.getOperand(0));
1490 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp1).addReg(Tmp1);
1491 }
1492 return Result;
1493
1494 case ISD::FABS:
1495 Tmp1 = SelectExpr(N.getOperand(0));
1496 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
1497 return Result;
1498
1499 case ISD::FP_ROUND:
1500 assert (DestType == MVT::f32 &&
1501 N.getOperand(0).getValueType() == MVT::f64 &&
1502 "only f64 to f32 conversion supported here");
1503 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth98169be2005-07-28 18:14:47 +00001504 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001505 return Result;
1506
1507 case ISD::FP_EXTEND:
1508 assert (DestType == MVT::f64 &&
1509 N.getOperand(0).getValueType() == MVT::f32 &&
1510 "only f32 to f64 conversion supported here");
1511 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth98169be2005-07-28 18:14:47 +00001512 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001513 return Result;
1514
1515 case ISD::ConstantFP:
1516 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
1517 if (CN->isExactlyValue(+0.0)) {
1518 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31)
1519 .addReg(Alpha::F31);
1520 } else if ( CN->isExactlyValue(-0.0)) {
1521 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31)
1522 .addReg(Alpha::F31);
1523 } else {
1524 abort();
1525 }
1526 }
1527 return Result;
1528
1529 case ISD::SINT_TO_FP:
1530 {
1531 assert (N.getOperand(0).getValueType() == MVT::i64
1532 && "only quads can be loaded from");
1533 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1534 Tmp2 = MakeReg(MVT::f64);
1535 MoveInt2FP(Tmp1, Tmp2, true);
1536 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
Andrew Lenharth98169be2005-07-28 18:14:47 +00001537 BuildMI(BB, Opc, 1, Result).addReg(Alpha::F31).addReg(Tmp2);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001538 return Result;
1539 }
Andrew Lenharthf71df332005-09-04 06:12:19 +00001540
1541 case ISD::AssertSext:
1542 case ISD::AssertZext:
1543 return SelectExpr(N.getOperand(0));
1544
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001545 }
1546
1547 return 0;
1548}
1549
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001550void AlphaISel::Select(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001551 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00001552 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001553
Nate Begeman85fdeb22005-03-24 04:39:54 +00001554 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001555 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001556
1557 SDNode *Node = N.Val;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001558
Andrew Lenharth760270d2005-02-07 23:02:23 +00001559 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001560
1561 default:
1562 Node->dump(); std::cerr << "\n";
1563 assert(0 && "Node not handled yet!");
1564
1565 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00001566 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001567 return;
1568 }
1569
1570 case ISD::BR: {
1571 MachineBasicBlock *Dest =
1572 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1573
1574 Select(N.getOperand(0));
1575 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
1576 return;
1577 }
1578
1579 case ISD::ImplicitDef:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001580 ++count_ins;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001581 Select(N.getOperand(0));
Chris Lattner707ebc52005-08-16 21:56:37 +00001582 BuildMI(BB, Alpha::IDEF, 0,
1583 cast<RegisterSDNode>(N.getOperand(1))->getReg());
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001584 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001585
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001586 case ISD::EntryToken: return; // Noop
1587
1588 case ISD::TokenFactor:
1589 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1590 Select(Node->getOperand(i));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001591
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001592 //N.Val->dump(); std::cerr << "\n";
1593 //assert(0 && "Node not handled yet!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001594
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001595 return;
1596
1597 case ISD::CopyToReg:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001598 ++count_outs;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001599 Select(N.getOperand(0));
Chris Lattner707ebc52005-08-16 21:56:37 +00001600 Tmp1 = SelectExpr(N.getOperand(2));
1601 Tmp2 = cast<RegisterSDNode>(N.getOperand(1))->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001602
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001603 if (Tmp1 != Tmp2) {
Chris Lattner707ebc52005-08-16 21:56:37 +00001604 if (N.getOperand(2).getValueType() == MVT::f64 ||
1605 N.getOperand(2).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00001606 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1607 else
1608 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001609 }
1610 return;
1611
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001612 case ISD::RET:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001613 ++count_outs;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001614 switch (N.getNumOperands()) {
1615 default:
1616 std::cerr << N.getNumOperands() << "\n";
1617 for (unsigned i = 0; i < N.getNumOperands(); ++i)
1618 std::cerr << N.getOperand(i).getValueType() << "\n";
1619 Node->dump();
1620 assert(0 && "Unknown return instruction!");
1621 case 2:
1622 Select(N.getOperand(0));
1623 Tmp1 = SelectExpr(N.getOperand(1));
1624 switch (N.getOperand(1).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001625 default: Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001626 assert(0 && "All other types should have been promoted!!");
1627 case MVT::f64:
1628 case MVT::f32:
1629 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
1630 break;
1631 case MVT::i32:
1632 case MVT::i64:
1633 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
1634 break;
1635 }
1636 break;
1637 case 1:
1638 Select(N.getOperand(0));
1639 break;
1640 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001641 // Just emit a 'ret' instruction
Andrew Lenharth6968bff2005-06-27 23:24:11 +00001642 AlphaLowering.restoreRA(BB);
Andrew Lenharthf3f951a2005-07-22 20:50:29 +00001643 BuildMI(BB, Alpha::RET, 2, Alpha::R31).addReg(Alpha::R26).addImm(1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001644 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001645
Misha Brukman4633f1c2005-04-21 23:13:11 +00001646 case ISD::TRUNCSTORE:
1647 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001648 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001649 SDOperand Chain = N.getOperand(0);
1650 SDOperand Value = N.getOperand(1);
1651 SDOperand Address = N.getOperand(2);
1652 Select(Chain);
1653
1654 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00001655
1656 if (opcode == ISD::STORE) {
1657 switch(Value.getValueType()) {
1658 default: assert(0 && "unknown Type in store");
1659 case MVT::i64: Opc = Alpha::STQ; break;
1660 case MVT::f64: Opc = Alpha::STT; break;
1661 case MVT::f32: Opc = Alpha::STS; break;
1662 }
1663 } else { //ISD::TRUNCSTORE
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001664 switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) {
Andrew Lenharth760270d2005-02-07 23:02:23 +00001665 default: assert(0 && "unknown Type in store");
Andrew Lenharth760270d2005-02-07 23:02:23 +00001666 case MVT::i8: Opc = Alpha::STB; break;
1667 case MVT::i16: Opc = Alpha::STW; break;
1668 case MVT::i32: Opc = Alpha::STL; break;
1669 }
Andrew Lenharth65838902005-02-06 16:22:15 +00001670 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00001671
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001672 int i, j, k;
Jeff Cohen00b168892005-07-27 06:12:32 +00001673 if (EnableAlphaLSMark)
1674 getValueInfo(cast<SrcValueSDNode>(N.getOperand(3))->getValue(),
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001675 i, j, k);
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001676
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001677 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Address);
1678 if (GASD && !GASD->getGlobal()->isExternal()) {
1679 Tmp2 = MakeReg(MVT::i64);
1680 AlphaLowering.restoreGP(BB);
1681 BuildMI(BB, Alpha::LDAHr, 2, Tmp2)
1682 .addGlobalAddress(GASD->getGlobal()).addReg(Alpha::R29);
1683 if (EnableAlphaLSMark)
1684 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1685 .addImm(getUID());
1686 BuildMI(BB, GetRelVersion(Opc), 3).addReg(Tmp1)
1687 .addGlobalAddress(GASD->getGlobal()).addReg(Tmp2);
Andrew Lenharthfce587e2005-06-29 00:39:17 +00001688 } else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001689 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001690 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1691 .addImm(getUID());
Andrew Lenharth032f2352005-02-22 21:59:48 +00001692 BuildMI(BB, Opc, 3).addReg(Tmp1)
1693 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1694 .addReg(Alpha::F31);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001695 } else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001696 long offset;
1697 SelectAddr(Address, Tmp2, offset);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001698 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001699 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1700 .addImm(getUID());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001701 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1702 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001703 return;
1704 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001705
1706 case ISD::EXTLOAD:
1707 case ISD::SEXTLOAD:
1708 case ISD::ZEXTLOAD:
1709 case ISD::LOAD:
1710 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001711 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001712 case ISD::CALL:
Andrew Lenharth032f2352005-02-22 21:59:48 +00001713 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001714 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001715 SelectExpr(N);
1716 return;
1717
Chris Lattner16cd04d2005-05-12 23:24:06 +00001718 case ISD::CALLSEQ_START:
1719 case ISD::CALLSEQ_END:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001720 Select(N.getOperand(0));
1721 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001722
Chris Lattner16cd04d2005-05-12 23:24:06 +00001723 Opc = N.getOpcode() == ISD::CALLSEQ_START ? Alpha::ADJUSTSTACKDOWN :
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001724 Alpha::ADJUSTSTACKUP;
1725 BuildMI(BB, Opc, 1).addImm(Tmp1);
1726 return;
Andrew Lenharth95762122005-03-31 21:24:06 +00001727
1728 case ISD::PCMARKER:
1729 Select(N.getOperand(0)); //Chain
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001730 BuildMI(BB, Alpha::PCLABEL, 2)
1731 .addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
Andrew Lenharth95762122005-03-31 21:24:06 +00001732 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001733 }
1734 assert(0 && "Should not be reached!");
1735}
1736
1737
1738/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
1739/// into a machine code representation using pattern matching and a machine
1740/// description file.
1741///
1742FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001743 return new AlphaISel(TM);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001744}
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001745