blob: afacf726cb3041efac3d041d5020e0cd40cbf03e [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;
Andrew Lenharth500b4db2005-04-22 13:35:18 +000077 int max_depth;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +000078
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000079public:
Jeff Cohen00b168892005-07-27 06:12:32 +000080 AlphaISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering),
Andrew Lenharthd4653b12005-06-27 17:39:17 +000081 AlphaLowering(TM)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000082 {}
Misha Brukman4633f1c2005-04-21 23:13:11 +000083
Chris Lattnerf519fe02005-10-29 16:45:02 +000084 virtual const char *getPassName() const {
85 return "Alpha Pattern Instruction Selection";
86 }
87
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000088 /// InstructionSelectBasicBlock - This callback is invoked by
89 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
90 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
Andrew Lenharth032f2352005-02-22 21:59:48 +000091 DEBUG(BB->dump());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +000092 count_ins = 0;
93 count_outs = 0;
Andrew Lenharth500b4db2005-04-22 13:35:18 +000094 max_depth = 0;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +000095
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000096 // Codegen the basic block.
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000097 ISelDAG = &DAG;
Andrew Lenharth500b4db2005-04-22 13:35:18 +000098 max_depth = DAG.getRoot().getNodeDepth();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000099 Select(DAG.getRoot());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000100
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000101 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{
Andrew Lenharth4052f022005-11-22 20:59:00 +0000279 unsigned Opc = Alpha::WTF;
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{
Andrew Lenharth4052f022005-11-22 20:59:00 +0000306 unsigned Opc = Alpha::WTF;
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 Lenharth4052f022005-11-22 20:59:00 +0000334 unsigned Tmp1, Tmp2, Tmp3, Opc = Alpha::WTF;
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)
Andrew Lenharth72d32c22005-11-30 17:14:11 +0000364 assert(0 && "Setcc On float?\n");
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000365 if (SetCC->getOperand(1).getValueType() == MVT::f32)
Andrew Lenharth72d32c22005-11-30 17:14:11 +0000366 assert (0 && "Setcc On float?\n");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000367
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000368 if (rev) std::swap(Tmp1, Tmp2);
369 //do the comparison
370 BuildMI(BB, Opc, 2, dst).addReg(Tmp1).addReg(Tmp2);
371 return inv;
372}
373
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000374//Check to see if the load is a constant offset from a base register
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000375void AlphaISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000376{
377 unsigned opcode = N.getOpcode();
Andrew Lenharth694c2982005-06-26 23:01:11 +0000378 if (opcode == ISD::ADD && N.getOperand(1).getOpcode() == ISD::Constant &&
379 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
380 { //Normal imm add
381 Reg = SelectExpr(N.getOperand(0));
382 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
383 return;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000384 }
385 Reg = SelectExpr(N);
386 offset = 0;
387 return;
388}
389
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000390void AlphaISel::SelectBranchCC(SDOperand N)
Andrew Lenharth445171a2005-02-08 00:40:03 +0000391{
392 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000393 MachineBasicBlock *Dest =
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000394 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
395 unsigned Opc = Alpha::WTF;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000396
Andrew Lenharth445171a2005-02-08 00:40:03 +0000397 Select(N.getOperand(0)); //chain
398 SDOperand CC = N.getOperand(1);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000399
Andrew Lenharth445171a2005-02-08 00:40:03 +0000400 if (CC.getOpcode() == ISD::SETCC)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000401 {
Chris Lattner88ac32c2005-08-09 20:21:10 +0000402 ISD::CondCode cCode= cast<CondCodeSDNode>(CC.getOperand(2))->get();
403 if (MVT::isInteger(CC.getOperand(0).getValueType())) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000404 //Dropping the CC is only useful if we are comparing to 0
Chris Lattner88ac32c2005-08-09 20:21:10 +0000405 bool RightZero = CC.getOperand(1).getOpcode() == ISD::Constant &&
406 cast<ConstantSDNode>(CC.getOperand(1))->getValue() == 0;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000407 bool isNE = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000408
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000409 //Fix up CC
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000410 if(cCode == ISD::SETNE)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000411 isNE = true;
Andrew Lenharth445171a2005-02-08 00:40:03 +0000412
Andrew Lenharth694c2982005-06-26 23:01:11 +0000413 if (RightZero) {
Andrew Lenharth09552bf2005-06-08 18:02:21 +0000414 switch (cCode) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000415 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
416 case ISD::SETEQ: Opc = Alpha::BEQ; break;
417 case ISD::SETLT: Opc = Alpha::BLT; break;
418 case ISD::SETLE: Opc = Alpha::BLE; break;
419 case ISD::SETGT: Opc = Alpha::BGT; break;
420 case ISD::SETGE: Opc = Alpha::BGE; break;
421 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
422 case ISD::SETUGT: Opc = Alpha::BNE; break;
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000423 //Technically you could have this CC
424 case ISD::SETULE: Opc = Alpha::BEQ; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000425 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
426 case ISD::SETNE: Opc = Alpha::BNE; break;
427 }
Chris Lattner88ac32c2005-08-09 20:21:10 +0000428 unsigned Tmp1 = SelectExpr(CC.getOperand(0)); //Cond
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000429 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
430 return;
431 } else {
432 unsigned Tmp1 = SelectExpr(CC);
433 if (isNE)
434 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
435 else
436 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000437 return;
438 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000439 } else { //FP
Jeff Cohen00b168892005-07-27 06:12:32 +0000440 //Any comparison between 2 values should be codegened as an folded
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000441 //branch, as moving CC to the integer register is very expensive
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000442 //for a cmp b: c = a - b;
443 //a = b: c = 0
444 //a < b: c < 0
445 //a > b: c > 0
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000446
447 bool invTest = false;
448 unsigned Tmp3;
449
450 ConstantFPSDNode *CN;
Chris Lattner88ac32c2005-08-09 20:21:10 +0000451 if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(1)))
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000452 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
Chris Lattner88ac32c2005-08-09 20:21:10 +0000453 Tmp3 = SelectExpr(CC.getOperand(0));
454 else if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(0)))
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000455 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
456 {
Chris Lattner88ac32c2005-08-09 20:21:10 +0000457 Tmp3 = SelectExpr(CC.getOperand(1));
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000458 invTest = true;
459 }
460 else
461 {
Chris Lattner88ac32c2005-08-09 20:21:10 +0000462 unsigned Tmp1 = SelectExpr(CC.getOperand(0));
463 unsigned Tmp2 = SelectExpr(CC.getOperand(1));
464 bool isD = CC.getOperand(0).getValueType() == MVT::f64;
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000465 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
466 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
467 .addReg(Tmp1).addReg(Tmp2);
468 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000469
Chris Lattner88ac32c2005-08-09 20:21:10 +0000470 switch (cCode) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000471 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000472 case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
473 case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
474 case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
475 case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
476 case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
477 case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000478 }
479 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000480 return;
481 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000482 abort(); //Should never be reached
483 } else {
484 //Giveup and do the stupid thing
485 unsigned Tmp1 = SelectExpr(CC);
486 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
487 return;
488 }
Andrew Lenharth445171a2005-02-08 00:40:03 +0000489 abort(); //Should never be reached
490}
491
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000492unsigned AlphaISel::SelectExpr(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000493 unsigned Result;
Andrew Lenharth2966e842005-04-07 18:15:28 +0000494 unsigned Tmp1, Tmp2 = 0, Tmp3;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000495 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000496 unsigned opcode = N.getOpcode();
Chris Lattnerd2fc54e2005-10-21 16:01:26 +0000497 int64_t SImm = 0;
Andrew Lenharthd2284272005-08-15 14:31:37 +0000498 uint64_t UImm;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000499
500 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000501 MVT::ValueType DestType = N.getValueType();
Andrew Lenharthf4da9452005-06-29 12:49:51 +0000502 bool isFP = DestType == MVT::f64 || DestType == MVT::f32;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000503
504 unsigned &Reg = ExprMap[N];
505 if (Reg) return Reg;
506
Andrew Lenharth46a776e2005-09-06 17:00:23 +0000507 switch(N.getOpcode()) {
508 default:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000509 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000510 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth46a776e2005-09-06 17:00:23 +0000511 break;
512 case ISD::AssertSext:
513 case ISD::AssertZext:
514 return Reg = SelectExpr(N.getOperand(0));
515 case ISD::CALL:
516 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000517 // If this is a call instruction, make sure to prepare ALL of the result
518 // values as well as the chain.
519 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000520 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000521 else {
522 Result = MakeReg(Node->getValueType(0));
523 ExprMap[N.getValue(0)] = Result;
524 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
525 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000526 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000527 }
Andrew Lenharth46a776e2005-09-06 17:00:23 +0000528 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000529 }
530
Andrew Lenharth40831c52005-01-28 06:57:18 +0000531 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000532 default:
533 Node->dump();
534 assert(0 && "Node not handled!\n");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000535
Andrew Lenharth51b8d542005-11-11 16:47:30 +0000536 case ISD::READCYCLECOUNTER:
537 Select(N.getOperand(0)); //Select chain
Andrew Lenharth2729e612005-11-11 23:02:55 +0000538 if (Result != notIn)
539 ExprMap[N.getValue(1)] = notIn; // Generate the token
540 else
541 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
542
Andrew Lenharth51b8d542005-11-11 16:47:30 +0000543 BuildMI(BB, Alpha::RPCC, 1, Result).addReg(Alpha::R31);
544 return Result;
545
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000546 case ISD::CTPOP:
547 case ISD::CTTZ:
548 case ISD::CTLZ:
549 Opc = opcode == ISD::CTPOP ? Alpha::CTPOP :
550 (opcode == ISD::CTTZ ? Alpha::CTTZ : Alpha::CTLZ);
551 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth964b6aa2005-10-20 19:39:24 +0000552 BuildMI(BB, Opc, 1, Result).addReg(Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000553 return Result;
554
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000555 case ISD::MULHU:
556 Tmp1 = SelectExpr(N.getOperand(0));
557 Tmp2 = SelectExpr(N.getOperand(1));
558 BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth706be912005-04-07 13:55:53 +0000559 return Result;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000560 case ISD::MULHS:
561 {
562 //MULHU - Ra<63>*Rb - Rb<63>*Ra
563 Tmp1 = SelectExpr(N.getOperand(0));
564 Tmp2 = SelectExpr(N.getOperand(1));
565 Tmp3 = MakeReg(MVT::i64);
566 BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
567 unsigned V1 = MakeReg(MVT::i64);
568 unsigned V2 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000569 BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31)
570 .addReg(Tmp1);
571 BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31)
572 .addReg(Tmp2);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000573 unsigned IRes = MakeReg(MVT::i64);
574 BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
575 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
576 return Result;
577 }
Andrew Lenharth7332f3e2005-04-02 19:11:07 +0000578 case ISD::UNDEF: {
Andrew Lenharth50b37842005-11-22 04:20:06 +0000579 Opc = isFP ? (DestType == MVT::f32 ? Alpha::IDEF_F32 : Alpha::IDEF_F64)
580 : Alpha::IDEF_I;
581 BuildMI(BB, Opc, 0, Result);
Andrew Lenharth7332f3e2005-04-02 19:11:07 +0000582 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 Lenharth4e629512005-12-24 05:36:33 +0000675 if (Address.getOpcode() == AlphaISD::GPRelLo) {
676 unsigned Hi = SelectExpr(Address.getOperand(1));
677 Address = Address.getOperand(0);
678 if (GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Address)) {
679 if (EnableAlphaLSMark)
680 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
681 .addImm(getUID());
682 BuildMI(BB, GetRelVersion(Opc), 2, Result)
683 .addGlobalAddress(GASD->getGlobal()).addReg(Hi);
684 } else if (ConstantPoolSDNode *CP =
685 dyn_cast<ConstantPoolSDNode>(Address)) {
686 unsigned CPIdx = BB->getParent()->getConstantPool()->
687 getConstantPoolIndex(CP->get());
Andrew Lenharth4e629512005-12-24 05:36:33 +0000688 if (EnableAlphaLSMark)
689 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
690 .addImm(getUID());
691 BuildMI(BB, GetRelVersion(Opc), 2, Result)
Andrew Lenharth53d89702005-12-25 01:34:27 +0000692 .addConstantPoolIndex(CPIdx).addReg(Hi);
Andrew Lenharth4e629512005-12-24 05:36:33 +0000693 } else assert(0 && "Unknown Lo part");
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000694 } else if(Address.getOpcode() == ISD::FrameIndex) {
695 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 Lenharth032f2352005-02-22 21:59:48 +0000698 BuildMI(BB, Opc, 2, Result)
699 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
700 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000701 } else {
702 long offset;
703 SelectAddr(Address, Tmp1, offset);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000704 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000705 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
706 .addImm(getUID());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000707 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
708 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000709 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000710 }
Andrew Lenharth4e629512005-12-24 05:36:33 +0000711 case AlphaISD::GlobalBaseReg:
712 AlphaLowering.restoreGP(BB);
713 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R29).addReg(Alpha::R29);
714 return Result;
Andrew Lenharth53d89702005-12-25 01:34:27 +0000715 case AlphaISD::GPRelHi: {
716 unsigned hi = SelectExpr(N.getOperand(1));
Andrew Lenharth4e629512005-12-24 05:36:33 +0000717 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(0)))
718 BuildMI(BB, Alpha::LDAHr, 2, Result)
719 .addConstantPoolIndex(BB->getParent()->getConstantPool()->
Andrew Lenharth53d89702005-12-25 01:34:27 +0000720 getConstantPoolIndex(CP->get())).addReg(hi);
Andrew Lenharth4e629512005-12-24 05:36:33 +0000721 else if (GlobalAddressSDNode *GASD =
722 dyn_cast<GlobalAddressSDNode>(N.getOperand(0)))
723 BuildMI(BB, Alpha::LDAHr, 2, Result)
Andrew Lenharth53d89702005-12-25 01:34:27 +0000724 .addGlobalAddress(GASD->getGlobal()).addReg(hi);
Andrew Lenharth4e629512005-12-24 05:36:33 +0000725 else assert(0 && "unknown Hi part");
726 return Result;
Andrew Lenharth53d89702005-12-25 01:34:27 +0000727 }
728 case AlphaISD::GPRelLo: {
729 unsigned hi = SelectExpr(N.getOperand(1));
Andrew Lenharth4e629512005-12-24 05:36:33 +0000730 if (ConstantPoolSDNode *CP = dyn_cast<ConstantPoolSDNode>(N.getOperand(0)))
731 BuildMI(BB, Alpha::LDAr, 2, Result)
732 .addConstantPoolIndex(BB->getParent()->getConstantPool()->
Andrew Lenharth53d89702005-12-25 01:34:27 +0000733 getConstantPoolIndex(CP->get())).addReg(hi);
Andrew Lenharth4e629512005-12-24 05:36:33 +0000734 else if (GlobalAddressSDNode *GASD =
735 dyn_cast<GlobalAddressSDNode>(N.getOperand(0)))
736 BuildMI(BB, Alpha::LDAr, 2, Result)
Andrew Lenharth53d89702005-12-25 01:34:27 +0000737 .addGlobalAddress(GASD->getGlobal()).addReg(hi);
Andrew Lenharth4e629512005-12-24 05:36:33 +0000738 else assert(0 && "unknown Lo part");
739 return Result;
Andrew Lenharth53d89702005-12-25 01:34:27 +0000740 }
Andrew Lenharthc687b482005-12-24 08:29:32 +0000741 case AlphaISD::RelLit: {
Andrew Lenharth53d89702005-12-25 01:34:27 +0000742 unsigned hi = SelectExpr(N.getOperand(1));
743 if (GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(N.getOperand(0)))
744 BuildMI(BB, Alpha::LDQl, 2, Result)
745 .addGlobalAddress(GASD->getGlobal()).addReg(hi);
746 else if (ExternalSymbolSDNode *ESSD = dyn_cast<ExternalSymbolSDNode>(N.getOperand(0)))
747 BuildMI(BB, Alpha::LDQl, 2, Result)
748 .addExternalSymbol(ESSD->getSymbol()).addReg(hi);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000749 return Result;
Andrew Lenharthc687b482005-12-24 08:29:32 +0000750 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000751
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +0000752 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000753 case ISD::CALL:
754 {
755 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000756
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000757 // The chain for this call is now lowered.
Andrew Lenharthf3f951a2005-07-22 20:50:29 +0000758 ExprMap[N.getValue(Node->getNumValues()-1)] = notIn;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000759
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000760 //grab the arguments
761 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000762 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000763 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000764 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000765
Andrew Lenharth684f2292005-01-30 00:35:27 +0000766 //in reg args
767 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000768 {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000769 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000770 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000771 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000772 Alpha::F19, Alpha::F20, Alpha::F21};
773 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000774 default:
775 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000776 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000777 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000778 N.getOperand(i+2).getValueType() << "\n";
779 assert(0 && "Unknown value type for call");
780 case MVT::i1:
781 case MVT::i8:
782 case MVT::i16:
783 case MVT::i32:
784 case MVT::i64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000785 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i])
786 .addReg(argvregs[i]);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000787 break;
788 case MVT::f32:
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +0000789 BuildMI(BB, Alpha::CPYSS, 2, args_float[i]).addReg(argvregs[i])
790 .addReg(argvregs[i]);
791 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000792 case MVT::f64:
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +0000793 BuildMI(BB, Alpha::CPYST, 2, args_float[i]).addReg(argvregs[i])
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000794 .addReg(argvregs[i]);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000795 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000796 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000797 }
Andrew Lenharth684f2292005-01-30 00:35:27 +0000798 //in mem args
799 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000800 {
801 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000802 default:
803 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000804 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000805 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000806 N.getOperand(i+2).getValueType() << "\n";
807 assert(0 && "Unknown value type for call");
808 case MVT::i1:
809 case MVT::i8:
810 case MVT::i16:
811 case MVT::i32:
812 case MVT::i64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000813 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
814 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000815 break;
816 case MVT::f32:
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000817 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
818 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000819 break;
820 case MVT::f64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000821 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
822 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000823 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000824 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000825 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000826 //build the right kind of call
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000827 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(N.getOperand(1));
828 if (GASD && !GASD->getGlobal()->isExternal()) {
829 //use PC relative branch call
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000830 AlphaLowering.restoreGP(BB);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000831 BuildMI(BB, Alpha::BSR, 1, Alpha::R26)
832 .addGlobalAddress(GASD->getGlobal(),true);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000833 } else {
834 //no need to restore GP as we are doing an indirect call
835 Tmp1 = SelectExpr(N.getOperand(1));
836 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
837 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
838 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000839
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000840 //push the result into a virtual register
Misha Brukman4633f1c2005-04-21 23:13:11 +0000841
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000842 switch (Node->getValueType(0)) {
843 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000844 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000845 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +0000846 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
847 break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000848 case MVT::f32:
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +0000849 BuildMI(BB, Alpha::CPYSS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
850 break;
851 case MVT::f64:
852 BuildMI(BB, Alpha::CPYST, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
Misha Brukman7847fca2005-04-22 17:54:37 +0000853 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000854 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000855 return Result+N.ResNo;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000856 }
857
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000858 case ISD::SIGN_EXTEND_INREG:
859 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000860 //do SDIV opt for all levels of ints if not dividing by a constant
861 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
862 && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +0000863 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +0000864 unsigned Tmp4 = MakeReg(MVT::f64);
865 unsigned Tmp5 = MakeReg(MVT::f64);
866 unsigned Tmp6 = MakeReg(MVT::f64);
867 unsigned Tmp7 = MakeReg(MVT::f64);
868 unsigned Tmp8 = MakeReg(MVT::f64);
869 unsigned Tmp9 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000870
871 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
872 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
873 MoveInt2FP(Tmp1, Tmp4, true);
874 MoveInt2FP(Tmp2, Tmp5, true);
Andrew Lenharth98169be2005-07-28 18:14:47 +0000875 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Alpha::F31).addReg(Tmp4);
876 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Alpha::F31).addReg(Tmp5);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +0000877 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
Andrew Lenharth98169be2005-07-28 18:14:47 +0000878 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Alpha::F31).addReg(Tmp8);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000879 MoveFP2Int(Tmp9, Result, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +0000880 return Result;
881 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000882
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000883 //Alpha has instructions for a bunch of signed 32 bit stuff
Chris Lattnerbce81ae2005-07-10 01:56:13 +0000884 if(cast<VTSDNode>(Node->getOperand(1))->getVT() == MVT::i32) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000885 switch (N.getOperand(0).getOpcode()) {
886 case ISD::ADD:
887 case ISD::SUB:
888 case ISD::MUL:
889 {
890 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
891 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
892 //FIXME: first check for Scaled Adds and Subs!
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000893 if(!isMul && N.getOperand(0).getOperand(0).getOpcode() == ISD::SHL &&
Andrew Lenharthd2284272005-08-15 14:31:37 +0000894 isSIntImmediateBounded(N.getOperand(0).getOperand(0).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(0).getOperand(0));
898 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
899 BuildMI(BB, isAdd?(use4?Alpha::S4ADDL:Alpha::S8ADDL):(use4?Alpha::S4SUBL:Alpha::S8SUBL),
900 2,Result).addReg(Tmp1).addReg(Tmp2);
901 }
902 else if(isAdd && N.getOperand(0).getOperand(1).getOpcode() == ISD::SHL &&
Andrew Lenharthd2284272005-08-15 14:31:37 +0000903 isSIntImmediateBounded(N.getOperand(0).getOperand(1).getOperand(1), SImm, 2, 3))
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000904 {
Andrew Lenharthd2284272005-08-15 14:31:37 +0000905 bool use4 = SImm == 2;
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000906 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
907 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
908 BuildMI(BB, use4?Alpha::S4ADDL:Alpha::S8ADDL, 2,Result).addReg(Tmp1).addReg(Tmp2);
909 }
Andrew Lenharthd2284272005-08-15 14:31:37 +0000910 else if(isSIntImmediateBounded(N.getOperand(0).getOperand(1), SImm, 0, 255))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000911 { //Normal imm add/sub
912 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000913 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +0000914 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000915 }
Andrew Lenharthd2284272005-08-15 14:31:37 +0000916 else if(!isMul && isSIntImmediate(N.getOperand(0).getOperand(1), SImm) &&
917 (((SImm << 32) >> 32) >= -255) && (((SImm << 32) >> 32) <= 0))
Andrew Lenharth6b137d82005-07-22 22:24:01 +0000918 { //handle canonicalization
919 Opc = isAdd ? Alpha::SUBLi : Alpha::ADDLi;
920 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +0000921 SImm = 0 - ((SImm << 32) >> 32);
922 assert(SImm >= 0 && SImm <= 255);
923 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharth6b137d82005-07-22 22:24:01 +0000924 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000925 else
926 { //Normal add/sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000927 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULL : Alpha::SUBL);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000928 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000929 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000930 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
931 }
932 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000933 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000934 default: break; //Fall Though;
935 }
936 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000937 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000938 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Chris Lattnerbce81ae2005-07-10 01:56:13 +0000939 switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000940 default:
941 Node->dump();
942 assert(0 && "Sign Extend InReg not there yet");
943 break;
944 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000945 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000946 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000947 break;
948 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000949 case MVT::i16:
Andrew Lenharth964b6aa2005-10-20 19:39:24 +0000950 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000951 break;
952 case MVT::i8:
Andrew Lenharth964b6aa2005-10-20 19:39:24 +0000953 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000954 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +0000955 case MVT::i1:
956 Tmp2 = MakeReg(MVT::i64);
957 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000958 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +0000959 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000960 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000961 return Result;
962 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000963
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000964 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000965 {
Chris Lattner88ac32c2005-08-09 20:21:10 +0000966 ISD::CondCode CC = cast<CondCodeSDNode>(N.getOperand(2))->get();
967 if (MVT::isInteger(N.getOperand(0).getValueType())) {
968 bool isConst = false;
969 int dir;
Misha Brukman7847fca2005-04-22 17:54:37 +0000970
Chris Lattner88ac32c2005-08-09 20:21:10 +0000971 //Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +0000972 if(isSIntImmediate(N.getOperand(1), SImm) && SImm <= 255 && SImm >= 0)
Chris Lattner88ac32c2005-08-09 20:21:10 +0000973 isConst = true;
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000974
Chris Lattner88ac32c2005-08-09 20:21:10 +0000975 switch (CC) {
976 default: Node->dump(); assert(0 && "Unknown integer comparison!");
977 case ISD::SETEQ:
978 Opc = isConst ? Alpha::CMPEQi : Alpha::CMPEQ; dir=1; break;
979 case ISD::SETLT:
980 Opc = isConst ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
981 case ISD::SETLE:
982 Opc = isConst ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
983 case ISD::SETGT: Opc = Alpha::CMPLT; dir = 2; break;
984 case ISD::SETGE: Opc = Alpha::CMPLE; dir = 2; break;
985 case ISD::SETULT:
986 Opc = isConst ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
987 case ISD::SETUGT: Opc = Alpha::CMPULT; dir = 2; break;
988 case ISD::SETULE:
989 Opc = isConst ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
990 case ISD::SETUGE: Opc = Alpha::CMPULE; dir = 2; break;
991 case ISD::SETNE: {//Handle this one special
992 //std::cerr << "Alpha does not have a setne.\n";
993 //abort();
994 Tmp1 = SelectExpr(N.getOperand(0));
995 Tmp2 = SelectExpr(N.getOperand(1));
996 Tmp3 = MakeReg(MVT::i64);
997 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
998 //Remeber we have the Inv for this CC
999 CCInvMap[N] = Tmp3;
1000 //and invert
1001 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
1002 return Result;
1003 }
1004 }
1005 if (dir == 1) {
1006 Tmp1 = SelectExpr(N.getOperand(0));
1007 if (isConst) {
Andrew Lenharthd2284272005-08-15 14:31:37 +00001008 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Chris Lattner88ac32c2005-08-09 20:21:10 +00001009 } else {
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001010 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth694c2982005-06-26 23:01:11 +00001011 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001012 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001013 } else { //if (dir == 2) {
1014 Tmp1 = SelectExpr(N.getOperand(1));
1015 Tmp2 = SelectExpr(N.getOperand(0));
1016 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001017 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001018 } else {
1019 //do the comparison
1020 Tmp1 = MakeReg(MVT::f64);
1021 bool inv = SelectFPSetCC(N, Tmp1);
1022
1023 //now arrange for Result (int) to have a 1 or 0
1024 Tmp2 = MakeReg(MVT::i64);
1025 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
1026 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
1027 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
Andrew Lenharth9818c052005-02-05 13:19:12 +00001028 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001029 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001030 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001031
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001032 case ISD::CopyFromReg:
1033 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001034 ++count_ins;
1035
Andrew Lenharth40831c52005-01-28 06:57:18 +00001036 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001037 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001038 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001039 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001040 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001041
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001042 SDOperand Chain = N.getOperand(0);
1043
1044 Select(Chain);
Chris Lattner707ebc52005-08-16 21:56:37 +00001045 unsigned r = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001046 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001047 switch(N.getValue(0).getValueType()) {
1048 case MVT::f32:
1049 BuildMI(BB, Alpha::CPYSS, 2, Result).addReg(r).addReg(r);
1050 break;
1051 case MVT::f64:
1052 BuildMI(BB, Alpha::CPYST, 2, Result).addReg(r).addReg(r);
1053 break;
1054 default:
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001055 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001056 break;
1057 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001058 return Result;
1059 }
1060
Misha Brukman4633f1c2005-04-21 23:13:11 +00001061 //Most of the plain arithmetic and logic share the same form, and the same
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001062 //constant immediate test
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001063 case ISD::XOR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001064 //Match Not
Andrew Lenharthd2284272005-08-15 14:31:37 +00001065 if (isSIntImmediate(N.getOperand(1), SImm) && SImm == -1) {
1066 Tmp1 = SelectExpr(N.getOperand(0));
1067 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1068 return Result;
1069 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001070 //Fall through
1071 case ISD::AND:
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001072 //handle zap
Andrew Lenharthd2284272005-08-15 14:31:37 +00001073 if (opcode == ISD::AND && isUIntImmediate(N.getOperand(1), UImm))
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001074 {
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001075 unsigned int build = 0;
1076 for(int i = 0; i < 8; ++i)
1077 {
Andrew Lenharthd2284272005-08-15 14:31:37 +00001078 if ((UImm & 0x00FF) == 0x00FF)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001079 build |= 1 << i;
Andrew Lenharthd2284272005-08-15 14:31:37 +00001080 else if ((UImm & 0x00FF) != 0)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001081 { build = 0; break; }
Andrew Lenharthd2284272005-08-15 14:31:37 +00001082 UImm >>= 8;
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001083 }
1084 if (build)
1085 {
1086 Tmp1 = SelectExpr(N.getOperand(0));
1087 BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1088 return Result;
1089 }
1090 }
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001091 case ISD::OR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001092 //Check operand(0) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001093 if (N.getOperand(0).getOpcode() == ISD::XOR &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001094 isSIntImmediate(N.getOperand(0).getOperand(1), SImm) && SImm == -1) {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001095 switch(opcode) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001096 case ISD::AND: Opc = Alpha::BIC; break;
1097 case ISD::OR: Opc = Alpha::ORNOT; break;
1098 case ISD::XOR: Opc = Alpha::EQV; break;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001099 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001100 Tmp1 = SelectExpr(N.getOperand(1));
1101 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1102 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1103 return Result;
1104 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001105 //Check operand(1) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001106 if (N.getOperand(1).getOpcode() == ISD::XOR &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001107 isSIntImmediate(N.getOperand(1).getOperand(1), SImm) && SImm == -1) {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001108 switch(opcode) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001109 case ISD::AND: Opc = Alpha::BIC; break;
1110 case ISD::OR: Opc = Alpha::ORNOT; break;
1111 case ISD::XOR: Opc = Alpha::EQV; break;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001112 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001113 Tmp1 = SelectExpr(N.getOperand(0));
1114 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1115 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1116 return Result;
1117 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001118 //Fall through
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001119 case ISD::SHL:
1120 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001121 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001122 case ISD::MUL:
Andrew Lenharthd2284272005-08-15 14:31:37 +00001123 if(isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255)) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001124 switch(opcode) {
1125 case ISD::AND: Opc = Alpha::ANDi; break;
1126 case ISD::OR: Opc = Alpha::BISi; break;
1127 case ISD::XOR: Opc = Alpha::XORi; break;
1128 case ISD::SHL: Opc = Alpha::SLi; break;
1129 case ISD::SRL: Opc = Alpha::SRLi; break;
1130 case ISD::SRA: Opc = Alpha::SRAi; break;
1131 case ISD::MUL: Opc = Alpha::MULQi; break;
1132 };
1133 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001134 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001135 } else {
1136 switch(opcode) {
1137 case ISD::AND: Opc = Alpha::AND; break;
1138 case ISD::OR: Opc = Alpha::BIS; break;
1139 case ISD::XOR: Opc = Alpha::XOR; break;
1140 case ISD::SHL: Opc = Alpha::SL; break;
1141 case ISD::SRL: Opc = Alpha::SRL; break;
1142 case ISD::SRA: Opc = Alpha::SRA; break;
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001143 case ISD::MUL: Opc = Alpha::MULQ; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001144 };
1145 Tmp1 = SelectExpr(N.getOperand(0));
1146 Tmp2 = SelectExpr(N.getOperand(1));
1147 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1148 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001149 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001150
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001151 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001152 case ISD::SUB:
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001153 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001154 bool isAdd = opcode == ISD::ADD;
1155
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001156 //first check for Scaled Adds and Subs!
1157 //Valid for add and sub
Andrew Lenharthd2284272005-08-15 14:31:37 +00001158 if(N.getOperand(0).getOpcode() == ISD::SHL &&
1159 isSIntImmediate(N.getOperand(0).getOperand(1), SImm) &&
1160 (SImm == 2 || SImm == 3)) {
1161 bool use4 = SImm == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001162 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001163 if (isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255))
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001164 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
Andrew Lenharthd2284272005-08-15 14:31:37 +00001165 2, Result).addReg(Tmp2).addImm(SImm);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001166 else {
1167 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001168 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1169 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001170 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001171 }
1172 //Position prevents subs
Andrew Lenharth273a1f92005-04-07 14:18:13 +00001173 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001174 isSIntImmediate(N.getOperand(1).getOperand(1), SImm) &&
1175 (SImm == 2 || SImm == 3)) {
1176 bool use4 = SImm == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001177 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001178 if (isSIntImmediateBounded(N.getOperand(0), SImm, 0, 255))
1179 BuildMI(BB, use4?Alpha::S4ADDQi:Alpha::S8ADDQi, 2, Result).addReg(Tmp2).addImm(SImm);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001180 else {
1181 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001182 BuildMI(BB, use4?Alpha::S4ADDQ:Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001183 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001184 }
1185 //small addi
Andrew Lenharthd2284272005-08-15 14:31:37 +00001186 else if(isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001187 { //Normal imm add/sub
1188 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1189 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001190 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001191 }
Andrew Lenharthd2284272005-08-15 14:31:37 +00001192 else if(isSIntImmediateBounded(N.getOperand(1), SImm, -255, 0))
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001193 { //inverted imm add/sub
1194 Opc = isAdd ? Alpha::SUBQi : Alpha::ADDQi;
1195 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001196 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(-SImm);
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001197 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001198 //larger addi
Andrew Lenharthd2284272005-08-15 14:31:37 +00001199 else if(isSIntImmediateBounded(N.getOperand(1), SImm, -32767, 32767))
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001200 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001201 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001202 if (!isAdd)
Andrew Lenharthd2284272005-08-15 14:31:37 +00001203 SImm = -SImm;
1204 BuildMI(BB, Alpha::LDA, 2, Result).addImm(SImm).addReg(Tmp1);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001205 }
1206 //give up and do the operation
1207 else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001208 //Normal add/sub
1209 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1210 Tmp1 = SelectExpr(N.getOperand(0));
1211 Tmp2 = SelectExpr(N.getOperand(1));
1212 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1213 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001214 return Result;
1215 }
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001216 case ISD::FADD:
1217 case ISD::FSUB:
1218 case ISD::FMUL:
1219 case ISD::FDIV: {
1220 if (opcode == ISD::FADD)
1221 Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS;
1222 else if (opcode == ISD::FSUB)
1223 Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS;
1224 else if (opcode == ISD::FMUL)
1225 Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS;
1226 else
1227 Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS;
1228 Tmp1 = SelectExpr(N.getOperand(0));
1229 Tmp2 = SelectExpr(N.getOperand(1));
1230 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1231 return Result;
1232 }
Andrew Lenharth53d89702005-12-25 01:34:27 +00001233 case AlphaISD::DivCall:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001234 Tmp1 = SelectExpr(N.getOperand(0));
1235 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth53d89702005-12-25 01:34:27 +00001236 Tmp3 = SelectExpr(N.getOperand(2));
1237 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp2).addReg(Tmp2);
1238 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp3).addReg(Tmp3);
1239 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001240 BuildMI(BB, Alpha::JSRs, 2, Alpha::R23).addReg(Alpha::R27).addImm(0);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001241 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001242 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001243
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001244 case ISD::SELECT:
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001245 if (isFP) {
1246 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1247 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
1248 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
1249
1250 SDOperand CC = N.getOperand(0);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001251
Chris Lattner88ac32c2005-08-09 20:21:10 +00001252 if (CC.getOpcode() == ISD::SETCC &&
1253 !MVT::isInteger(CC.getOperand(0).getValueType())) {
1254 //FP Setcc -> Select yay!
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001255
Jeff Cohen00b168892005-07-27 06:12:32 +00001256
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001257 //for a cmp b: c = a - b;
1258 //a = b: c = 0
1259 //a < b: c < 0
1260 //a > b: c > 0
1261
1262 bool invTest = false;
1263 unsigned Tmp3;
Andrew Lenharthb2156f92005-11-30 17:11:20 +00001264 bool isD = CC.getOperand(0).getValueType() == MVT::f64;
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001265 ConstantFPSDNode *CN;
Chris Lattner88ac32c2005-08-09 20:21:10 +00001266 if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(1)))
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001267 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
Chris Lattner88ac32c2005-08-09 20:21:10 +00001268 Tmp3 = SelectExpr(CC.getOperand(0));
1269 else if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(0)))
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001270 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1271 {
Chris Lattner88ac32c2005-08-09 20:21:10 +00001272 Tmp3 = SelectExpr(CC.getOperand(1));
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001273 invTest = true;
1274 }
1275 else
1276 {
Chris Lattner88ac32c2005-08-09 20:21:10 +00001277 unsigned Tmp1 = SelectExpr(CC.getOperand(0));
1278 unsigned Tmp2 = SelectExpr(CC.getOperand(1));
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001279 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1280 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1281 .addReg(Tmp1).addReg(Tmp2);
1282 }
1283
Andrew Lenharthb2156f92005-11-30 17:11:20 +00001284 if(isD)
1285 switch (cast<CondCodeSDNode>(CC.getOperand(2))->get()) {
1286 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
1287 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNET : Alpha::FCMOVEQT; break;
1288 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGTT : Alpha::FCMOVLTT; break;
1289 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGET : Alpha::FCMOVLET; break;
1290 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLTT : Alpha::FCMOVGTT; break;
1291 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLET : Alpha::FCMOVGET; break;
1292 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQT : Alpha::FCMOVNET; break;
1293 }
1294 else
1295 switch (cast<CondCodeSDNode>(CC.getOperand(2))->get()) {
1296 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
1297 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNES : Alpha::FCMOVEQS; break;
1298 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGTS : Alpha::FCMOVLTS; break;
1299 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGES : Alpha::FCMOVLES; break;
1300 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLTS : Alpha::FCMOVGTS; break;
1301 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLES : Alpha::FCMOVGES; break;
1302 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQS : Alpha::FCMOVNES; break;
1303 }
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001304 BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
1305 return Result;
1306 }
1307 else
1308 {
1309 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1310 BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV)
1311 .addReg(Tmp1);
1312// // Spill the cond to memory and reload it from there.
1313// unsigned Tmp4 = MakeReg(MVT::f64);
1314// MoveIntFP(Tmp1, Tmp4, true);
1315// //now ideally, we don't have to do anything to the flag...
1316// // Get the condition into the zero flag.
1317// BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
1318 return Result;
Jeff Cohen00b168892005-07-27 06:12:32 +00001319 }
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001320 } else {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001321 //FIXME: look at parent to decide if intCC can be folded, or if setCC(FP)
1322 //and can save stack use
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001323 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001324 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1325 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001326 // Get the condition into the zero flag.
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001327 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001328
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001329 SDOperand CC = N.getOperand(0);
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001330
Misha Brukman4633f1c2005-04-21 23:13:11 +00001331 if (CC.getOpcode() == ISD::SETCC &&
Chris Lattner88ac32c2005-08-09 20:21:10 +00001332 !MVT::isInteger(CC.getOperand(0).getValueType()))
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001333 { //FP Setcc -> Int Select
Misha Brukman7847fca2005-04-22 17:54:37 +00001334 Tmp1 = MakeReg(MVT::f64);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001335 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1336 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Misha Brukman7847fca2005-04-22 17:54:37 +00001337 bool inv = SelectFPSetCC(CC, Tmp1);
1338 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
1339 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
1340 return Result;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001341 }
1342 if (CC.getOpcode() == ISD::SETCC) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001343 //Int SetCC -> Select
1344 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharthd2284272005-08-15 14:31:37 +00001345 if(isSIntImmediateBounded(CC.getOperand(1), SImm, 0, 0)) {
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001346 //figure out a few things
Andrew Lenharthd2284272005-08-15 14:31:37 +00001347 bool useImm = isSIntImmediateBounded(N.getOperand(2), SImm, 0, 255);
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001348
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001349 //Fix up CC
Chris Lattner88ac32c2005-08-09 20:21:10 +00001350 ISD::CondCode cCode= cast<CondCodeSDNode>(CC.getOperand(2))->get();
Andrew Lenharth694c2982005-06-26 23:01:11 +00001351 if (useImm) //Invert sense to get Imm field right
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001352 cCode = ISD::getSetCCInverse(cCode, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001353
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001354 //Choose the CMOV
1355 switch (cCode) {
1356 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001357 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
1358 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
1359 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
1360 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
1361 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
1362 case ISD::SETULT: assert(0 && "unsigned < 0 is never true"); break;
1363 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
1364 //Technically you could have this CC
1365 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
1366 case ISD::SETUGE: assert(0 && "unsgined >= 0 is always true"); break;
1367 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001368 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001369 Tmp1 = SelectExpr(CC.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001370
Andrew Lenharth694c2982005-06-26 23:01:11 +00001371 if (useImm) {
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001372 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
Andrew Lenharthd2284272005-08-15 14:31:37 +00001373 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addImm(SImm).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001374 } else {
1375 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1376 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1377 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
1378 }
1379 return Result;
1380 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001381 //Otherwise, fall though
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001382 }
1383 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001384 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1385 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001386 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3)
1387 .addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001388
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001389 return Result;
1390 }
1391
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001392 case ISD::Constant:
1393 {
Andrew Lenharthc0513832005-03-29 19:24:04 +00001394 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001395 int zero_extend_top = 0;
Andrew Lenharthf075cac2005-07-23 07:46:48 +00001396 if (val > 0 && (val & 0xFFFFFFFF00000000ULL) == 0 &&
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001397 ((int32_t)val < 0)) {
1398 //try a small load and zero extend
1399 val = (int32_t)val;
1400 zero_extend_top = 15;
1401 }
1402
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00001403 if (val <= IMM_HIGH && val >= IMM_LOW) {
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001404 if(!zero_extend_top)
1405 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
1406 else {
1407 Tmp1 = MakeReg(MVT::i64);
1408 BuildMI(BB, Alpha::LDA, 2, Tmp1).addImm(val).addReg(Alpha::R31);
1409 BuildMI(BB, Alpha::ZAPNOT, 2, Result).addReg(Tmp1).addImm(zero_extend_top);
1410 }
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00001411 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001412 else if (val <= (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT &&
1413 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
1414 Tmp1 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001415 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val))
1416 .addReg(Alpha::R31);
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001417 if (!zero_extend_top)
1418 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
1419 else {
1420 Tmp3 = MakeReg(MVT::i64);
1421 BuildMI(BB, Alpha::LDA, 2, Tmp3).addImm(getLower16(val)).addReg(Tmp1);
1422 BuildMI(BB, Alpha::ZAPNOT, 2, Result).addReg(Tmp3).addImm(zero_extend_top);
1423 }
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00001424 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001425 else {
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001426 //re-get the val since we are going to mem anyway
1427 val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001428 MachineConstantPool *CP = BB->getParent()->getConstantPool();
Jeff Cohen00b168892005-07-27 06:12:32 +00001429 ConstantUInt *C =
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001430 ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001431 unsigned CPI = CP->getConstantPoolIndex(C);
1432 AlphaLowering.restoreGP(BB);
Andrew Lenharthfe895e32005-06-27 17:15:36 +00001433 Tmp1 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001434 BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CPI)
1435 .addReg(Alpha::R29);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001436 if (EnableAlphaLSMark)
1437 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
1438 .addImm(getUID());
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001439 BuildMI(BB, Alpha::LDQr, 2, Result).addConstantPoolIndex(CPI)
1440 .addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001441 }
1442 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001443 }
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001444 case ISD::FNEG:
1445 if(ISD::FABS == N.getOperand(0).getOpcode())
1446 {
1447 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001448 BuildMI(BB, DestType == MVT::f64 ? Alpha::CPYSNT : Alpha::CPYSNS,
1449 2, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001450 } else {
1451 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001452 BuildMI(BB, DestType == MVT::f64 ? Alpha::CPYSNT : Alpha::CPYSNS
1453 , 2, Result).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001454 }
1455 return Result;
1456
1457 case ISD::FABS:
1458 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001459 BuildMI(BB, DestType == MVT::f64 ? Alpha::CPYST : Alpha::CPYSS, 2, Result)
1460 .addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001461 return Result;
1462
1463 case ISD::FP_ROUND:
1464 assert (DestType == MVT::f32 &&
1465 N.getOperand(0).getValueType() == MVT::f64 &&
1466 "only f64 to f32 conversion supported here");
1467 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthb921f1b2005-11-11 23:08:46 +00001468 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001469 return Result;
1470
1471 case ISD::FP_EXTEND:
1472 assert (DestType == MVT::f64 &&
1473 N.getOperand(0).getValueType() == MVT::f32 &&
1474 "only f32 to f64 conversion supported here");
1475 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthc2c64fd2005-11-11 19:52:25 +00001476 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001477 return Result;
1478
1479 case ISD::ConstantFP:
1480 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
1481 if (CN->isExactlyValue(+0.0)) {
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001482 BuildMI(BB, DestType == MVT::f64 ? Alpha::CPYST : Alpha::CPYSS
1483 , 2, Result).addReg(Alpha::F31)
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001484 .addReg(Alpha::F31);
1485 } else if ( CN->isExactlyValue(-0.0)) {
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001486 BuildMI(BB, DestType == MVT::f64 ? Alpha::CPYSNT : Alpha::CPYSNS,
1487 2, Result).addReg(Alpha::F31)
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001488 .addReg(Alpha::F31);
1489 } else {
1490 abort();
1491 }
1492 }
1493 return Result;
1494
Andrew Lenharth7f0db912005-11-30 07:19:56 +00001495 case AlphaISD::CVTQT_:
Andrew Lenharth6251b362005-12-01 17:48:51 +00001496 Tmp1 = SelectExpr(N.getOperand(0));
1497 BuildMI(BB, Alpha::CVTQT, 1, Result).addReg(Tmp1);
Andrew Lenharth7f0db912005-11-30 07:19:56 +00001498 return Result;
1499
1500 case AlphaISD::CVTQS_:
Andrew Lenharth6251b362005-12-01 17:48:51 +00001501 Tmp1 = SelectExpr(N.getOperand(0));
1502 BuildMI(BB, Alpha::CVTQS, 1, Result).addReg(Tmp1);
Andrew Lenharth7f0db912005-11-30 07:19:56 +00001503 return Result;
1504
Andrew Lenharthcd804962005-11-30 16:10:29 +00001505 case AlphaISD::CVTTQ_:
Andrew Lenharth6251b362005-12-01 17:48:51 +00001506 Tmp1 = SelectExpr(N.getOperand(0));
1507 BuildMI(BB, Alpha::CVTTQ, 1, Result).addReg(Tmp1);
Andrew Lenharthcd804962005-11-30 16:10:29 +00001508 return Result;
1509
Andrew Lenharth7f0db912005-11-30 07:19:56 +00001510 case AlphaISD::ITOFT_:
Andrew Lenharth6251b362005-12-01 17:48:51 +00001511 Tmp1 = SelectExpr(N.getOperand(0));
1512 BuildMI(BB, Alpha::ITOFT, 1, Result).addReg(Tmp1);
Andrew Lenharth7f0db912005-11-30 07:19:56 +00001513 return Result;
Andrew Lenharthf71df332005-09-04 06:12:19 +00001514
Andrew Lenharthcd804962005-11-30 16:10:29 +00001515 case AlphaISD::FTOIT_:
Andrew Lenharth6251b362005-12-01 17:48:51 +00001516 Tmp1 = SelectExpr(N.getOperand(0));
1517 BuildMI(BB, Alpha::FTOIT, 1, Result).addReg(Tmp1);
Andrew Lenharthcd804962005-11-30 16:10:29 +00001518 return Result;
1519
Andrew Lenharthf71df332005-09-04 06:12:19 +00001520 case ISD::AssertSext:
1521 case ISD::AssertZext:
1522 return SelectExpr(N.getOperand(0));
1523
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001524 }
1525
1526 return 0;
1527}
1528
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001529void AlphaISel::Select(SDOperand N) {
Andrew Lenharth4052f022005-11-22 20:59:00 +00001530 unsigned Tmp1, Tmp2, Opc = Alpha::WTF;
Andrew Lenharth760270d2005-02-07 23:02:23 +00001531 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001532
Nate Begeman85fdeb22005-03-24 04:39:54 +00001533 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001534 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001535
1536 SDNode *Node = N.Val;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001537
Andrew Lenharth760270d2005-02-07 23:02:23 +00001538 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001539
1540 default:
1541 Node->dump(); std::cerr << "\n";
1542 assert(0 && "Node not handled yet!");
1543
1544 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00001545 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001546 return;
1547 }
1548
1549 case ISD::BR: {
1550 MachineBasicBlock *Dest =
1551 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1552
1553 Select(N.getOperand(0));
1554 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
1555 return;
1556 }
1557
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001558 case ISD::EntryToken: return; // Noop
1559
1560 case ISD::TokenFactor:
1561 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1562 Select(Node->getOperand(i));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001563
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001564 //N.Val->dump(); std::cerr << "\n";
1565 //assert(0 && "Node not handled yet!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001566
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001567 return;
1568
1569 case ISD::CopyToReg:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001570 ++count_outs;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001571 Select(N.getOperand(0));
Chris Lattner707ebc52005-08-16 21:56:37 +00001572 Tmp1 = SelectExpr(N.getOperand(2));
1573 Tmp2 = cast<RegisterSDNode>(N.getOperand(1))->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001574
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001575 if (Tmp1 != Tmp2) {
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001576 switch(N.getOperand(2).getValueType()) {
1577 case MVT::f64:
1578 BuildMI(BB, Alpha::CPYST, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1579 break;
1580 case MVT::f32:
1581 BuildMI(BB, Alpha::CPYSS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1582 break;
1583 default:
Andrew Lenharth29219162005-02-07 06:31:44 +00001584 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001585 break;
1586 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001587 }
1588 return;
1589
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001590 case ISD::RET:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001591 ++count_outs;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001592 switch (N.getNumOperands()) {
1593 default:
1594 std::cerr << N.getNumOperands() << "\n";
1595 for (unsigned i = 0; i < N.getNumOperands(); ++i)
1596 std::cerr << N.getOperand(i).getValueType() << "\n";
1597 Node->dump();
1598 assert(0 && "Unknown return instruction!");
1599 case 2:
1600 Select(N.getOperand(0));
1601 Tmp1 = SelectExpr(N.getOperand(1));
1602 switch (N.getOperand(1).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001603 default: Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001604 assert(0 && "All other types should have been promoted!!");
1605 case MVT::f64:
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001606 BuildMI(BB, Alpha::CPYST, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
1607 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001608 case MVT::f32:
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001609 BuildMI(BB, Alpha::CPYSS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001610 break;
1611 case MVT::i32:
1612 case MVT::i64:
1613 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
1614 break;
1615 }
1616 break;
1617 case 1:
1618 Select(N.getOperand(0));
1619 break;
1620 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001621 // Just emit a 'ret' instruction
Andrew Lenharth6968bff2005-06-27 23:24:11 +00001622 AlphaLowering.restoreRA(BB);
Andrew Lenharthf3f951a2005-07-22 20:50:29 +00001623 BuildMI(BB, Alpha::RET, 2, Alpha::R31).addReg(Alpha::R26).addImm(1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001624 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001625
Misha Brukman4633f1c2005-04-21 23:13:11 +00001626 case ISD::TRUNCSTORE:
1627 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001628 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001629 SDOperand Chain = N.getOperand(0);
1630 SDOperand Value = N.getOperand(1);
1631 SDOperand Address = N.getOperand(2);
1632 Select(Chain);
1633
1634 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00001635
1636 if (opcode == ISD::STORE) {
1637 switch(Value.getValueType()) {
Andrew Lenharth4052f022005-11-22 20:59:00 +00001638 default: assert(0 && "unknown Type in store");
Andrew Lenharth760270d2005-02-07 23:02:23 +00001639 case MVT::i64: Opc = Alpha::STQ; break;
1640 case MVT::f64: Opc = Alpha::STT; break;
1641 case MVT::f32: Opc = Alpha::STS; break;
1642 }
1643 } else { //ISD::TRUNCSTORE
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001644 switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) {
Andrew Lenharth4052f022005-11-22 20:59:00 +00001645 default: assert(0 && "unknown Type in store");
Andrew Lenharth760270d2005-02-07 23:02:23 +00001646 case MVT::i8: Opc = Alpha::STB; break;
1647 case MVT::i16: Opc = Alpha::STW; break;
1648 case MVT::i32: Opc = Alpha::STL; break;
1649 }
Andrew Lenharth65838902005-02-06 16:22:15 +00001650 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00001651
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001652 int i, j, k;
Jeff Cohen00b168892005-07-27 06:12:32 +00001653 if (EnableAlphaLSMark)
1654 getValueInfo(cast<SrcValueSDNode>(N.getOperand(3))->getValue(),
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001655 i, j, k);
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001656
Andrew Lenharth4e629512005-12-24 05:36:33 +00001657 if (Address.getOpcode() == AlphaISD::GPRelLo) {
1658 unsigned Hi = SelectExpr(Address.getOperand(1));
1659 Address = Address.getOperand(0);
1660 if (GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Address)) {
1661 if (EnableAlphaLSMark)
1662 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1663 .addImm(getUID());
1664 BuildMI(BB, GetRelVersion(Opc), 3).addReg(Tmp1)
1665 .addGlobalAddress(GASD->getGlobal()).addReg(Hi);
1666 } else assert(0 && "Unknown Lo part");
Andrew Lenharthfce587e2005-06-29 00:39:17 +00001667 } else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001668 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001669 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1670 .addImm(getUID());
Andrew Lenharth032f2352005-02-22 21:59:48 +00001671 BuildMI(BB, Opc, 3).addReg(Tmp1)
1672 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1673 .addReg(Alpha::F31);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001674 } else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001675 long offset;
1676 SelectAddr(Address, Tmp2, offset);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001677 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001678 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1679 .addImm(getUID());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001680 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1681 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001682 return;
1683 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001684
1685 case ISD::EXTLOAD:
1686 case ISD::SEXTLOAD:
1687 case ISD::ZEXTLOAD:
1688 case ISD::LOAD:
1689 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001690 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001691 case ISD::CALL:
Andrew Lenharth82a698c2005-11-12 19:04:09 +00001692 case ISD::READCYCLECOUNTER:
Andrew Lenharth032f2352005-02-22 21:59:48 +00001693 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001694 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001695 SelectExpr(N);
1696 return;
1697
Chris Lattner16cd04d2005-05-12 23:24:06 +00001698 case ISD::CALLSEQ_START:
1699 case ISD::CALLSEQ_END:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001700 Select(N.getOperand(0));
1701 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001702
Chris Lattner16cd04d2005-05-12 23:24:06 +00001703 Opc = N.getOpcode() == ISD::CALLSEQ_START ? Alpha::ADJUSTSTACKDOWN :
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001704 Alpha::ADJUSTSTACKUP;
1705 BuildMI(BB, Opc, 1).addImm(Tmp1);
1706 return;
Andrew Lenharth95762122005-03-31 21:24:06 +00001707
1708 case ISD::PCMARKER:
1709 Select(N.getOperand(0)); //Chain
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001710 BuildMI(BB, Alpha::PCLABEL, 2)
1711 .addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
Andrew Lenharth95762122005-03-31 21:24:06 +00001712 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001713 }
1714 assert(0 && "Should not be reached!");
1715}
1716
1717
1718/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
1719/// into a machine code representation using pattern matching and a machine
1720/// description file.
1721///
1722FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001723 return new AlphaISel(TM);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001724}
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001725