blob: 92f16e8f7f35c8de4dfc863bcaf58890f0b1e053 [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 Lenhartheececba2005-12-25 17:36:48 +0000827 if (N.getOperand(1).getOpcode() == AlphaISD::GPRelLo) {
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000828 //use PC relative branch call
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000829 AlphaLowering.restoreGP(BB);
Andrew Lenhartheececba2005-12-25 17:36:48 +0000830 BuildMI(BB, Alpha::BSR, 1)
831 .addGlobalAddress(cast<GlobalAddressSDNode>(N.getOperand(1)
832 .getOperand(0))
833 ->getGlobal(),true);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000834 } else {
835 //no need to restore GP as we are doing an indirect call
836 Tmp1 = SelectExpr(N.getOperand(1));
837 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
Andrew Lenhartheececba2005-12-25 17:36:48 +0000838 BuildMI(BB, Alpha::JSR, 0);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000839 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000840
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000841 //push the result into a virtual register
Misha Brukman4633f1c2005-04-21 23:13:11 +0000842
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000843 switch (Node->getValueType(0)) {
844 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000845 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000846 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +0000847 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
848 break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000849 case MVT::f32:
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +0000850 BuildMI(BB, Alpha::CPYSS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
851 break;
852 case MVT::f64:
853 BuildMI(BB, Alpha::CPYST, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
Misha Brukman7847fca2005-04-22 17:54:37 +0000854 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000855 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000856 return Result+N.ResNo;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000857 }
858
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000859 case ISD::SIGN_EXTEND_INREG:
860 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000861 //do SDIV opt for all levels of ints if not dividing by a constant
862 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
863 && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +0000864 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +0000865 unsigned Tmp4 = MakeReg(MVT::f64);
866 unsigned Tmp5 = MakeReg(MVT::f64);
867 unsigned Tmp6 = MakeReg(MVT::f64);
868 unsigned Tmp7 = MakeReg(MVT::f64);
869 unsigned Tmp8 = MakeReg(MVT::f64);
870 unsigned Tmp9 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000871
872 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
873 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
874 MoveInt2FP(Tmp1, Tmp4, true);
875 MoveInt2FP(Tmp2, Tmp5, true);
Andrew Lenharth98169be2005-07-28 18:14:47 +0000876 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Alpha::F31).addReg(Tmp4);
877 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Alpha::F31).addReg(Tmp5);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +0000878 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
Andrew Lenharth98169be2005-07-28 18:14:47 +0000879 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Alpha::F31).addReg(Tmp8);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000880 MoveFP2Int(Tmp9, Result, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +0000881 return Result;
882 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000883
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000884 //Alpha has instructions for a bunch of signed 32 bit stuff
Chris Lattnerbce81ae2005-07-10 01:56:13 +0000885 if(cast<VTSDNode>(Node->getOperand(1))->getVT() == MVT::i32) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000886 switch (N.getOperand(0).getOpcode()) {
887 case ISD::ADD:
888 case ISD::SUB:
889 case ISD::MUL:
890 {
891 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
892 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
893 //FIXME: first check for Scaled Adds and Subs!
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000894 if(!isMul && N.getOperand(0).getOperand(0).getOpcode() == ISD::SHL &&
Andrew Lenharthd2284272005-08-15 14:31:37 +0000895 isSIntImmediateBounded(N.getOperand(0).getOperand(0).getOperand(1), SImm, 2, 3))
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000896 {
Andrew Lenharthd2284272005-08-15 14:31:37 +0000897 bool use4 = SImm == 2;
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000898 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
899 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
900 BuildMI(BB, isAdd?(use4?Alpha::S4ADDL:Alpha::S8ADDL):(use4?Alpha::S4SUBL:Alpha::S8SUBL),
901 2,Result).addReg(Tmp1).addReg(Tmp2);
902 }
903 else if(isAdd && N.getOperand(0).getOperand(1).getOpcode() == ISD::SHL &&
Andrew Lenharthd2284272005-08-15 14:31:37 +0000904 isSIntImmediateBounded(N.getOperand(0).getOperand(1).getOperand(1), SImm, 2, 3))
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000905 {
Andrew Lenharthd2284272005-08-15 14:31:37 +0000906 bool use4 = SImm == 2;
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000907 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
908 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
909 BuildMI(BB, use4?Alpha::S4ADDL:Alpha::S8ADDL, 2,Result).addReg(Tmp1).addReg(Tmp2);
910 }
Andrew Lenharthd2284272005-08-15 14:31:37 +0000911 else if(isSIntImmediateBounded(N.getOperand(0).getOperand(1), SImm, 0, 255))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000912 { //Normal imm add/sub
913 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000914 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +0000915 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000916 }
Andrew Lenharthd2284272005-08-15 14:31:37 +0000917 else if(!isMul && isSIntImmediate(N.getOperand(0).getOperand(1), SImm) &&
918 (((SImm << 32) >> 32) >= -255) && (((SImm << 32) >> 32) <= 0))
Andrew Lenharth6b137d82005-07-22 22:24:01 +0000919 { //handle canonicalization
920 Opc = isAdd ? Alpha::SUBLi : Alpha::ADDLi;
921 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +0000922 SImm = 0 - ((SImm << 32) >> 32);
923 assert(SImm >= 0 && SImm <= 255);
924 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharth6b137d82005-07-22 22:24:01 +0000925 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000926 else
927 { //Normal add/sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000928 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULL : Alpha::SUBL);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000929 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +0000930 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000931 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
932 }
933 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000934 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000935 default: break; //Fall Though;
936 }
937 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000938 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000939 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Chris Lattnerbce81ae2005-07-10 01:56:13 +0000940 switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000941 default:
942 Node->dump();
943 assert(0 && "Sign Extend InReg not there yet");
944 break;
945 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000946 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000947 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000948 break;
949 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000950 case MVT::i16:
Andrew Lenharth964b6aa2005-10-20 19:39:24 +0000951 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000952 break;
953 case MVT::i8:
Andrew Lenharth964b6aa2005-10-20 19:39:24 +0000954 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000955 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +0000956 case MVT::i1:
957 Tmp2 = MakeReg(MVT::i64);
958 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000959 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +0000960 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000961 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000962 return Result;
963 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000964
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000965 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000966 {
Chris Lattner88ac32c2005-08-09 20:21:10 +0000967 ISD::CondCode CC = cast<CondCodeSDNode>(N.getOperand(2))->get();
968 if (MVT::isInteger(N.getOperand(0).getValueType())) {
969 bool isConst = false;
970 int dir;
Misha Brukman7847fca2005-04-22 17:54:37 +0000971
Chris Lattner88ac32c2005-08-09 20:21:10 +0000972 //Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +0000973 if(isSIntImmediate(N.getOperand(1), SImm) && SImm <= 255 && SImm >= 0)
Chris Lattner88ac32c2005-08-09 20:21:10 +0000974 isConst = true;
Andrew Lenharth3d65d312005-01-27 03:49:45 +0000975
Chris Lattner88ac32c2005-08-09 20:21:10 +0000976 switch (CC) {
977 default: Node->dump(); assert(0 && "Unknown integer comparison!");
978 case ISD::SETEQ:
979 Opc = isConst ? Alpha::CMPEQi : Alpha::CMPEQ; dir=1; break;
980 case ISD::SETLT:
981 Opc = isConst ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
982 case ISD::SETLE:
983 Opc = isConst ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
984 case ISD::SETGT: Opc = Alpha::CMPLT; dir = 2; break;
985 case ISD::SETGE: Opc = Alpha::CMPLE; dir = 2; break;
986 case ISD::SETULT:
987 Opc = isConst ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
988 case ISD::SETUGT: Opc = Alpha::CMPULT; dir = 2; break;
989 case ISD::SETULE:
990 Opc = isConst ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
991 case ISD::SETUGE: Opc = Alpha::CMPULE; dir = 2; break;
992 case ISD::SETNE: {//Handle this one special
993 //std::cerr << "Alpha does not have a setne.\n";
994 //abort();
995 Tmp1 = SelectExpr(N.getOperand(0));
996 Tmp2 = SelectExpr(N.getOperand(1));
997 Tmp3 = MakeReg(MVT::i64);
998 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
999 //Remeber we have the Inv for this CC
1000 CCInvMap[N] = Tmp3;
1001 //and invert
1002 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
1003 return Result;
1004 }
1005 }
1006 if (dir == 1) {
1007 Tmp1 = SelectExpr(N.getOperand(0));
1008 if (isConst) {
Andrew Lenharthd2284272005-08-15 14:31:37 +00001009 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Chris Lattner88ac32c2005-08-09 20:21:10 +00001010 } else {
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001011 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth694c2982005-06-26 23:01:11 +00001012 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001013 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001014 } else { //if (dir == 2) {
1015 Tmp1 = SelectExpr(N.getOperand(1));
1016 Tmp2 = SelectExpr(N.getOperand(0));
1017 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001018 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001019 } else {
1020 //do the comparison
1021 Tmp1 = MakeReg(MVT::f64);
1022 bool inv = SelectFPSetCC(N, Tmp1);
1023
1024 //now arrange for Result (int) to have a 1 or 0
1025 Tmp2 = MakeReg(MVT::i64);
1026 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
1027 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
1028 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
Andrew Lenharth9818c052005-02-05 13:19:12 +00001029 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001030 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001031 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001032
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001033 case ISD::CopyFromReg:
1034 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001035 ++count_ins;
1036
Andrew Lenharth40831c52005-01-28 06:57:18 +00001037 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001038 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001039 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001040 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001041 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001042
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001043 SDOperand Chain = N.getOperand(0);
1044
1045 Select(Chain);
Chris Lattner707ebc52005-08-16 21:56:37 +00001046 unsigned r = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001047 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001048 switch(N.getValue(0).getValueType()) {
1049 case MVT::f32:
1050 BuildMI(BB, Alpha::CPYSS, 2, Result).addReg(r).addReg(r);
1051 break;
1052 case MVT::f64:
1053 BuildMI(BB, Alpha::CPYST, 2, Result).addReg(r).addReg(r);
1054 break;
1055 default:
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001056 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001057 break;
1058 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001059 return Result;
1060 }
1061
Misha Brukman4633f1c2005-04-21 23:13:11 +00001062 //Most of the plain arithmetic and logic share the same form, and the same
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001063 //constant immediate test
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001064 case ISD::XOR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001065 //Match Not
Andrew Lenharthd2284272005-08-15 14:31:37 +00001066 if (isSIntImmediate(N.getOperand(1), SImm) && SImm == -1) {
1067 Tmp1 = SelectExpr(N.getOperand(0));
1068 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1069 return Result;
1070 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001071 //Fall through
1072 case ISD::AND:
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001073 //handle zap
Andrew Lenharthd2284272005-08-15 14:31:37 +00001074 if (opcode == ISD::AND && isUIntImmediate(N.getOperand(1), UImm))
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001075 {
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001076 unsigned int build = 0;
1077 for(int i = 0; i < 8; ++i)
1078 {
Andrew Lenharthd2284272005-08-15 14:31:37 +00001079 if ((UImm & 0x00FF) == 0x00FF)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001080 build |= 1 << i;
Andrew Lenharthd2284272005-08-15 14:31:37 +00001081 else if ((UImm & 0x00FF) != 0)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001082 { build = 0; break; }
Andrew Lenharthd2284272005-08-15 14:31:37 +00001083 UImm >>= 8;
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001084 }
1085 if (build)
1086 {
1087 Tmp1 = SelectExpr(N.getOperand(0));
1088 BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1089 return Result;
1090 }
1091 }
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001092 case ISD::OR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001093 //Check operand(0) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001094 if (N.getOperand(0).getOpcode() == ISD::XOR &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001095 isSIntImmediate(N.getOperand(0).getOperand(1), SImm) && SImm == -1) {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001096 switch(opcode) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001097 case ISD::AND: Opc = Alpha::BIC; break;
1098 case ISD::OR: Opc = Alpha::ORNOT; break;
1099 case ISD::XOR: Opc = Alpha::EQV; break;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001100 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001101 Tmp1 = SelectExpr(N.getOperand(1));
1102 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1103 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1104 return Result;
1105 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001106 //Check operand(1) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001107 if (N.getOperand(1).getOpcode() == ISD::XOR &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001108 isSIntImmediate(N.getOperand(1).getOperand(1), SImm) && SImm == -1) {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001109 switch(opcode) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001110 case ISD::AND: Opc = Alpha::BIC; break;
1111 case ISD::OR: Opc = Alpha::ORNOT; break;
1112 case ISD::XOR: Opc = Alpha::EQV; break;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001113 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001114 Tmp1 = SelectExpr(N.getOperand(0));
1115 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1116 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1117 return Result;
1118 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001119 //Fall through
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001120 case ISD::SHL:
1121 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001122 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001123 case ISD::MUL:
Andrew Lenharthd2284272005-08-15 14:31:37 +00001124 if(isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255)) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001125 switch(opcode) {
1126 case ISD::AND: Opc = Alpha::ANDi; break;
1127 case ISD::OR: Opc = Alpha::BISi; break;
1128 case ISD::XOR: Opc = Alpha::XORi; break;
1129 case ISD::SHL: Opc = Alpha::SLi; break;
1130 case ISD::SRL: Opc = Alpha::SRLi; break;
1131 case ISD::SRA: Opc = Alpha::SRAi; break;
1132 case ISD::MUL: Opc = Alpha::MULQi; break;
1133 };
1134 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001135 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001136 } else {
1137 switch(opcode) {
1138 case ISD::AND: Opc = Alpha::AND; break;
1139 case ISD::OR: Opc = Alpha::BIS; break;
1140 case ISD::XOR: Opc = Alpha::XOR; break;
1141 case ISD::SHL: Opc = Alpha::SL; break;
1142 case ISD::SRL: Opc = Alpha::SRL; break;
1143 case ISD::SRA: Opc = Alpha::SRA; break;
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001144 case ISD::MUL: Opc = Alpha::MULQ; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001145 };
1146 Tmp1 = SelectExpr(N.getOperand(0));
1147 Tmp2 = SelectExpr(N.getOperand(1));
1148 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1149 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001150 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001151
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001152 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001153 case ISD::SUB:
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001154 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001155 bool isAdd = opcode == ISD::ADD;
1156
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001157 //first check for Scaled Adds and Subs!
1158 //Valid for add and sub
Andrew Lenharthd2284272005-08-15 14:31:37 +00001159 if(N.getOperand(0).getOpcode() == ISD::SHL &&
1160 isSIntImmediate(N.getOperand(0).getOperand(1), SImm) &&
1161 (SImm == 2 || SImm == 3)) {
1162 bool use4 = SImm == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001163 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001164 if (isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255))
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001165 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
Andrew Lenharthd2284272005-08-15 14:31:37 +00001166 2, Result).addReg(Tmp2).addImm(SImm);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001167 else {
1168 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001169 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1170 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001171 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001172 }
1173 //Position prevents subs
Andrew Lenharth273a1f92005-04-07 14:18:13 +00001174 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001175 isSIntImmediate(N.getOperand(1).getOperand(1), SImm) &&
1176 (SImm == 2 || SImm == 3)) {
1177 bool use4 = SImm == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001178 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001179 if (isSIntImmediateBounded(N.getOperand(0), SImm, 0, 255))
1180 BuildMI(BB, use4?Alpha::S4ADDQi:Alpha::S8ADDQi, 2, Result).addReg(Tmp2).addImm(SImm);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001181 else {
1182 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001183 BuildMI(BB, use4?Alpha::S4ADDQ:Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001184 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001185 }
1186 //small addi
Andrew Lenharthd2284272005-08-15 14:31:37 +00001187 else if(isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001188 { //Normal imm add/sub
1189 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1190 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001191 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001192 }
Andrew Lenharthd2284272005-08-15 14:31:37 +00001193 else if(isSIntImmediateBounded(N.getOperand(1), SImm, -255, 0))
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001194 { //inverted imm add/sub
1195 Opc = isAdd ? Alpha::SUBQi : Alpha::ADDQi;
1196 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001197 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(-SImm);
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001198 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001199 //larger addi
Andrew Lenharthd2284272005-08-15 14:31:37 +00001200 else if(isSIntImmediateBounded(N.getOperand(1), SImm, -32767, 32767))
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001201 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001202 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001203 if (!isAdd)
Andrew Lenharthd2284272005-08-15 14:31:37 +00001204 SImm = -SImm;
1205 BuildMI(BB, Alpha::LDA, 2, Result).addImm(SImm).addReg(Tmp1);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001206 }
1207 //give up and do the operation
1208 else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001209 //Normal add/sub
1210 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1211 Tmp1 = SelectExpr(N.getOperand(0));
1212 Tmp2 = SelectExpr(N.getOperand(1));
1213 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1214 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001215 return Result;
1216 }
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001217 case ISD::FADD:
1218 case ISD::FSUB:
1219 case ISD::FMUL:
1220 case ISD::FDIV: {
1221 if (opcode == ISD::FADD)
1222 Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS;
1223 else if (opcode == ISD::FSUB)
1224 Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS;
1225 else if (opcode == ISD::FMUL)
1226 Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS;
1227 else
1228 Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS;
1229 Tmp1 = SelectExpr(N.getOperand(0));
1230 Tmp2 = SelectExpr(N.getOperand(1));
1231 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1232 return Result;
1233 }
Andrew Lenharth53d89702005-12-25 01:34:27 +00001234 case AlphaISD::DivCall:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001235 Tmp1 = SelectExpr(N.getOperand(0));
1236 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth53d89702005-12-25 01:34:27 +00001237 Tmp3 = SelectExpr(N.getOperand(2));
1238 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp2).addReg(Tmp2);
1239 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp3).addReg(Tmp3);
1240 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
Andrew Lenhartheececba2005-12-25 17:36:48 +00001241 BuildMI(BB, Alpha::JSRs, 0);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001242 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001243 return Result;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001244
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001245 case ISD::SELECT:
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001246 if (isFP) {
1247 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1248 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
1249 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
1250
1251 SDOperand CC = N.getOperand(0);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001252
Chris Lattner88ac32c2005-08-09 20:21:10 +00001253 if (CC.getOpcode() == ISD::SETCC &&
1254 !MVT::isInteger(CC.getOperand(0).getValueType())) {
1255 //FP Setcc -> Select yay!
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001256
Jeff Cohen00b168892005-07-27 06:12:32 +00001257
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001258 //for a cmp b: c = a - b;
1259 //a = b: c = 0
1260 //a < b: c < 0
1261 //a > b: c > 0
1262
1263 bool invTest = false;
1264 unsigned Tmp3;
Andrew Lenharthb2156f92005-11-30 17:11:20 +00001265 bool isD = CC.getOperand(0).getValueType() == MVT::f64;
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001266 ConstantFPSDNode *CN;
Chris Lattner88ac32c2005-08-09 20:21:10 +00001267 if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(1)))
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001268 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
Chris Lattner88ac32c2005-08-09 20:21:10 +00001269 Tmp3 = SelectExpr(CC.getOperand(0));
1270 else if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(0)))
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001271 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1272 {
Chris Lattner88ac32c2005-08-09 20:21:10 +00001273 Tmp3 = SelectExpr(CC.getOperand(1));
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001274 invTest = true;
1275 }
1276 else
1277 {
Chris Lattner88ac32c2005-08-09 20:21:10 +00001278 unsigned Tmp1 = SelectExpr(CC.getOperand(0));
1279 unsigned Tmp2 = SelectExpr(CC.getOperand(1));
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001280 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1281 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1282 .addReg(Tmp1).addReg(Tmp2);
1283 }
1284
Andrew Lenharthb2156f92005-11-30 17:11:20 +00001285 if(isD)
1286 switch (cast<CondCodeSDNode>(CC.getOperand(2))->get()) {
1287 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
1288 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNET : Alpha::FCMOVEQT; break;
1289 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGTT : Alpha::FCMOVLTT; break;
1290 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGET : Alpha::FCMOVLET; break;
1291 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLTT : Alpha::FCMOVGTT; break;
1292 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLET : Alpha::FCMOVGET; break;
1293 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQT : Alpha::FCMOVNET; break;
1294 }
1295 else
1296 switch (cast<CondCodeSDNode>(CC.getOperand(2))->get()) {
1297 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
1298 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNES : Alpha::FCMOVEQS; break;
1299 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGTS : Alpha::FCMOVLTS; break;
1300 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGES : Alpha::FCMOVLES; break;
1301 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLTS : Alpha::FCMOVGTS; break;
1302 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLES : Alpha::FCMOVGES; break;
1303 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQS : Alpha::FCMOVNES; break;
1304 }
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001305 BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
1306 return Result;
1307 }
1308 else
1309 {
1310 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1311 BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV)
1312 .addReg(Tmp1);
1313// // Spill the cond to memory and reload it from there.
1314// unsigned Tmp4 = MakeReg(MVT::f64);
1315// MoveIntFP(Tmp1, Tmp4, true);
1316// //now ideally, we don't have to do anything to the flag...
1317// // Get the condition into the zero flag.
1318// BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
1319 return Result;
Jeff Cohen00b168892005-07-27 06:12:32 +00001320 }
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001321 } else {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001322 //FIXME: look at parent to decide if intCC can be folded, or if setCC(FP)
1323 //and can save stack use
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001324 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001325 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1326 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001327 // Get the condition into the zero flag.
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001328 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001329
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001330 SDOperand CC = N.getOperand(0);
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001331
Misha Brukman4633f1c2005-04-21 23:13:11 +00001332 if (CC.getOpcode() == ISD::SETCC &&
Chris Lattner88ac32c2005-08-09 20:21:10 +00001333 !MVT::isInteger(CC.getOperand(0).getValueType()))
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001334 { //FP Setcc -> Int Select
Misha Brukman7847fca2005-04-22 17:54:37 +00001335 Tmp1 = MakeReg(MVT::f64);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001336 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1337 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Misha Brukman7847fca2005-04-22 17:54:37 +00001338 bool inv = SelectFPSetCC(CC, Tmp1);
1339 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
1340 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
1341 return Result;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001342 }
1343 if (CC.getOpcode() == ISD::SETCC) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001344 //Int SetCC -> Select
1345 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharthd2284272005-08-15 14:31:37 +00001346 if(isSIntImmediateBounded(CC.getOperand(1), SImm, 0, 0)) {
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001347 //figure out a few things
Andrew Lenharthd2284272005-08-15 14:31:37 +00001348 bool useImm = isSIntImmediateBounded(N.getOperand(2), SImm, 0, 255);
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001349
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001350 //Fix up CC
Chris Lattner88ac32c2005-08-09 20:21:10 +00001351 ISD::CondCode cCode= cast<CondCodeSDNode>(CC.getOperand(2))->get();
Andrew Lenharth694c2982005-06-26 23:01:11 +00001352 if (useImm) //Invert sense to get Imm field right
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001353 cCode = ISD::getSetCCInverse(cCode, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001354
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001355 //Choose the CMOV
1356 switch (cCode) {
1357 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001358 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
1359 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
1360 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
1361 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
1362 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
1363 case ISD::SETULT: assert(0 && "unsigned < 0 is never true"); break;
1364 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
1365 //Technically you could have this CC
1366 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
1367 case ISD::SETUGE: assert(0 && "unsgined >= 0 is always true"); break;
1368 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001369 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001370 Tmp1 = SelectExpr(CC.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001371
Andrew Lenharth694c2982005-06-26 23:01:11 +00001372 if (useImm) {
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001373 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
Andrew Lenharthd2284272005-08-15 14:31:37 +00001374 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addImm(SImm).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001375 } else {
1376 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1377 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1378 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
1379 }
1380 return Result;
1381 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001382 //Otherwise, fall though
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001383 }
1384 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001385 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1386 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001387 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3)
1388 .addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001389
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001390 return Result;
1391 }
1392
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001393 case ISD::Constant:
1394 {
Andrew Lenharthc0513832005-03-29 19:24:04 +00001395 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001396 int zero_extend_top = 0;
Andrew Lenharthf075cac2005-07-23 07:46:48 +00001397 if (val > 0 && (val & 0xFFFFFFFF00000000ULL) == 0 &&
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001398 ((int32_t)val < 0)) {
1399 //try a small load and zero extend
1400 val = (int32_t)val;
1401 zero_extend_top = 15;
1402 }
1403
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00001404 if (val <= IMM_HIGH && val >= IMM_LOW) {
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001405 if(!zero_extend_top)
1406 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
1407 else {
1408 Tmp1 = MakeReg(MVT::i64);
1409 BuildMI(BB, Alpha::LDA, 2, Tmp1).addImm(val).addReg(Alpha::R31);
1410 BuildMI(BB, Alpha::ZAPNOT, 2, Result).addReg(Tmp1).addImm(zero_extend_top);
1411 }
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00001412 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001413 else if (val <= (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT &&
1414 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
1415 Tmp1 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001416 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val))
1417 .addReg(Alpha::R31);
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001418 if (!zero_extend_top)
1419 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
1420 else {
1421 Tmp3 = MakeReg(MVT::i64);
1422 BuildMI(BB, Alpha::LDA, 2, Tmp3).addImm(getLower16(val)).addReg(Tmp1);
1423 BuildMI(BB, Alpha::ZAPNOT, 2, Result).addReg(Tmp3).addImm(zero_extend_top);
1424 }
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00001425 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001426 else {
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001427 //re-get the val since we are going to mem anyway
1428 val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001429 MachineConstantPool *CP = BB->getParent()->getConstantPool();
Jeff Cohen00b168892005-07-27 06:12:32 +00001430 ConstantUInt *C =
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001431 ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001432 unsigned CPI = CP->getConstantPoolIndex(C);
1433 AlphaLowering.restoreGP(BB);
Andrew Lenharthfe895e32005-06-27 17:15:36 +00001434 Tmp1 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001435 BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CPI)
1436 .addReg(Alpha::R29);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001437 if (EnableAlphaLSMark)
1438 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
1439 .addImm(getUID());
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001440 BuildMI(BB, Alpha::LDQr, 2, Result).addConstantPoolIndex(CPI)
1441 .addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001442 }
1443 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001444 }
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001445 case ISD::FNEG:
1446 if(ISD::FABS == N.getOperand(0).getOpcode())
1447 {
1448 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001449 BuildMI(BB, DestType == MVT::f64 ? Alpha::CPYSNT : Alpha::CPYSNS,
1450 2, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001451 } else {
1452 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001453 BuildMI(BB, DestType == MVT::f64 ? Alpha::CPYSNT : Alpha::CPYSNS
1454 , 2, Result).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001455 }
1456 return Result;
1457
1458 case ISD::FABS:
1459 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001460 BuildMI(BB, DestType == MVT::f64 ? Alpha::CPYST : Alpha::CPYSS, 2, Result)
1461 .addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001462 return Result;
1463
1464 case ISD::FP_ROUND:
1465 assert (DestType == MVT::f32 &&
1466 N.getOperand(0).getValueType() == MVT::f64 &&
1467 "only f64 to f32 conversion supported here");
1468 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthb921f1b2005-11-11 23:08:46 +00001469 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Tmp1);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001470 return Result;
1471
1472 case ISD::FP_EXTEND:
1473 assert (DestType == MVT::f64 &&
1474 N.getOperand(0).getValueType() == MVT::f32 &&
1475 "only f32 to f64 conversion supported here");
1476 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthc2c64fd2005-11-11 19:52:25 +00001477 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Tmp1);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001478 return Result;
1479
1480 case ISD::ConstantFP:
1481 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
1482 if (CN->isExactlyValue(+0.0)) {
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001483 BuildMI(BB, DestType == MVT::f64 ? Alpha::CPYST : Alpha::CPYSS
1484 , 2, Result).addReg(Alpha::F31)
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001485 .addReg(Alpha::F31);
1486 } else if ( CN->isExactlyValue(-0.0)) {
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001487 BuildMI(BB, DestType == MVT::f64 ? Alpha::CPYSNT : Alpha::CPYSNS,
1488 2, Result).addReg(Alpha::F31)
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001489 .addReg(Alpha::F31);
1490 } else {
1491 abort();
1492 }
1493 }
1494 return Result;
1495
Andrew Lenharth7f0db912005-11-30 07:19:56 +00001496 case AlphaISD::CVTQT_:
Andrew Lenharth6251b362005-12-01 17:48:51 +00001497 Tmp1 = SelectExpr(N.getOperand(0));
1498 BuildMI(BB, Alpha::CVTQT, 1, Result).addReg(Tmp1);
Andrew Lenharth7f0db912005-11-30 07:19:56 +00001499 return Result;
1500
1501 case AlphaISD::CVTQS_:
Andrew Lenharth6251b362005-12-01 17:48:51 +00001502 Tmp1 = SelectExpr(N.getOperand(0));
1503 BuildMI(BB, Alpha::CVTQS, 1, Result).addReg(Tmp1);
Andrew Lenharth7f0db912005-11-30 07:19:56 +00001504 return Result;
1505
Andrew Lenharthcd804962005-11-30 16:10:29 +00001506 case AlphaISD::CVTTQ_:
Andrew Lenharth6251b362005-12-01 17:48:51 +00001507 Tmp1 = SelectExpr(N.getOperand(0));
1508 BuildMI(BB, Alpha::CVTTQ, 1, Result).addReg(Tmp1);
Andrew Lenharthcd804962005-11-30 16:10:29 +00001509 return Result;
1510
Andrew Lenharth7f0db912005-11-30 07:19:56 +00001511 case AlphaISD::ITOFT_:
Andrew Lenharth6251b362005-12-01 17:48:51 +00001512 Tmp1 = SelectExpr(N.getOperand(0));
1513 BuildMI(BB, Alpha::ITOFT, 1, Result).addReg(Tmp1);
Andrew Lenharth7f0db912005-11-30 07:19:56 +00001514 return Result;
Andrew Lenharthf71df332005-09-04 06:12:19 +00001515
Andrew Lenharthcd804962005-11-30 16:10:29 +00001516 case AlphaISD::FTOIT_:
Andrew Lenharth6251b362005-12-01 17:48:51 +00001517 Tmp1 = SelectExpr(N.getOperand(0));
1518 BuildMI(BB, Alpha::FTOIT, 1, Result).addReg(Tmp1);
Andrew Lenharthcd804962005-11-30 16:10:29 +00001519 return Result;
1520
Andrew Lenharthf71df332005-09-04 06:12:19 +00001521 case ISD::AssertSext:
1522 case ISD::AssertZext:
1523 return SelectExpr(N.getOperand(0));
1524
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001525 }
1526
1527 return 0;
1528}
1529
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001530void AlphaISel::Select(SDOperand N) {
Andrew Lenharth4052f022005-11-22 20:59:00 +00001531 unsigned Tmp1, Tmp2, Opc = Alpha::WTF;
Andrew Lenharth760270d2005-02-07 23:02:23 +00001532 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001533
Nate Begeman85fdeb22005-03-24 04:39:54 +00001534 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001535 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001536
1537 SDNode *Node = N.Val;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001538
Andrew Lenharth760270d2005-02-07 23:02:23 +00001539 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001540
1541 default:
1542 Node->dump(); std::cerr << "\n";
1543 assert(0 && "Node not handled yet!");
1544
1545 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00001546 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001547 return;
1548 }
1549
1550 case ISD::BR: {
1551 MachineBasicBlock *Dest =
1552 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1553
1554 Select(N.getOperand(0));
Andrew Lenhartheececba2005-12-25 17:36:48 +00001555 BuildMI(BB, Alpha::BR, 1).addMBB(Dest);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001556 return;
1557 }
1558
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001559 case ISD::EntryToken: return; // Noop
1560
1561 case ISD::TokenFactor:
1562 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1563 Select(Node->getOperand(i));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001564
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001565 //N.Val->dump(); std::cerr << "\n";
1566 //assert(0 && "Node not handled yet!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001567
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001568 return;
1569
1570 case ISD::CopyToReg:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001571 ++count_outs;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001572 Select(N.getOperand(0));
Chris Lattner707ebc52005-08-16 21:56:37 +00001573 Tmp1 = SelectExpr(N.getOperand(2));
1574 Tmp2 = cast<RegisterSDNode>(N.getOperand(1))->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001575
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001576 if (Tmp1 != Tmp2) {
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001577 switch(N.getOperand(2).getValueType()) {
1578 case MVT::f64:
1579 BuildMI(BB, Alpha::CPYST, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1580 break;
1581 case MVT::f32:
1582 BuildMI(BB, Alpha::CPYSS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1583 break;
1584 default:
Andrew Lenharth29219162005-02-07 06:31:44 +00001585 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001586 break;
1587 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001588 }
1589 return;
1590
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001591 case ISD::RET:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001592 ++count_outs;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001593 switch (N.getNumOperands()) {
1594 default:
1595 std::cerr << N.getNumOperands() << "\n";
1596 for (unsigned i = 0; i < N.getNumOperands(); ++i)
1597 std::cerr << N.getOperand(i).getValueType() << "\n";
1598 Node->dump();
1599 assert(0 && "Unknown return instruction!");
1600 case 2:
1601 Select(N.getOperand(0));
1602 Tmp1 = SelectExpr(N.getOperand(1));
1603 switch (N.getOperand(1).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001604 default: Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001605 assert(0 && "All other types should have been promoted!!");
1606 case MVT::f64:
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001607 BuildMI(BB, Alpha::CPYST, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
1608 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001609 case MVT::f32:
Andrew Lenharth5cefc5e2005-11-09 19:17:08 +00001610 BuildMI(BB, Alpha::CPYSS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001611 break;
1612 case MVT::i32:
1613 case MVT::i64:
1614 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
1615 break;
1616 }
1617 break;
1618 case 1:
1619 Select(N.getOperand(0));
1620 break;
1621 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001622 // Just emit a 'ret' instruction
Andrew Lenharth6968bff2005-06-27 23:24:11 +00001623 AlphaLowering.restoreRA(BB);
Andrew Lenharthf3f951a2005-07-22 20:50:29 +00001624 BuildMI(BB, Alpha::RET, 2, Alpha::R31).addReg(Alpha::R26).addImm(1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001625 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001626
Misha Brukman4633f1c2005-04-21 23:13:11 +00001627 case ISD::TRUNCSTORE:
1628 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001629 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001630 SDOperand Chain = N.getOperand(0);
1631 SDOperand Value = N.getOperand(1);
1632 SDOperand Address = N.getOperand(2);
1633 Select(Chain);
1634
1635 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00001636
1637 if (opcode == ISD::STORE) {
1638 switch(Value.getValueType()) {
Andrew Lenharth4052f022005-11-22 20:59:00 +00001639 default: assert(0 && "unknown Type in store");
Andrew Lenharth760270d2005-02-07 23:02:23 +00001640 case MVT::i64: Opc = Alpha::STQ; break;
1641 case MVT::f64: Opc = Alpha::STT; break;
1642 case MVT::f32: Opc = Alpha::STS; break;
1643 }
1644 } else { //ISD::TRUNCSTORE
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001645 switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) {
Andrew Lenharth4052f022005-11-22 20:59:00 +00001646 default: assert(0 && "unknown Type in store");
Andrew Lenharth760270d2005-02-07 23:02:23 +00001647 case MVT::i8: Opc = Alpha::STB; break;
1648 case MVT::i16: Opc = Alpha::STW; break;
1649 case MVT::i32: Opc = Alpha::STL; break;
1650 }
Andrew Lenharth65838902005-02-06 16:22:15 +00001651 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00001652
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001653 int i, j, k;
Jeff Cohen00b168892005-07-27 06:12:32 +00001654 if (EnableAlphaLSMark)
1655 getValueInfo(cast<SrcValueSDNode>(N.getOperand(3))->getValue(),
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001656 i, j, k);
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001657
Andrew Lenharth4e629512005-12-24 05:36:33 +00001658 if (Address.getOpcode() == AlphaISD::GPRelLo) {
1659 unsigned Hi = SelectExpr(Address.getOperand(1));
1660 Address = Address.getOperand(0);
1661 if (GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Address)) {
1662 if (EnableAlphaLSMark)
1663 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1664 .addImm(getUID());
1665 BuildMI(BB, GetRelVersion(Opc), 3).addReg(Tmp1)
1666 .addGlobalAddress(GASD->getGlobal()).addReg(Hi);
1667 } else assert(0 && "Unknown Lo part");
Andrew Lenharthfce587e2005-06-29 00:39:17 +00001668 } else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001669 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001670 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1671 .addImm(getUID());
Andrew Lenharth032f2352005-02-22 21:59:48 +00001672 BuildMI(BB, Opc, 3).addReg(Tmp1)
1673 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1674 .addReg(Alpha::F31);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001675 } else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001676 long offset;
1677 SelectAddr(Address, Tmp2, offset);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001678 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001679 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1680 .addImm(getUID());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001681 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1682 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001683 return;
1684 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001685
1686 case ISD::EXTLOAD:
1687 case ISD::SEXTLOAD:
1688 case ISD::ZEXTLOAD:
1689 case ISD::LOAD:
1690 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001691 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001692 case ISD::CALL:
Andrew Lenharth82a698c2005-11-12 19:04:09 +00001693 case ISD::READCYCLECOUNTER:
Andrew Lenharth032f2352005-02-22 21:59:48 +00001694 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001695 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001696 SelectExpr(N);
1697 return;
1698
Chris Lattner16cd04d2005-05-12 23:24:06 +00001699 case ISD::CALLSEQ_START:
1700 case ISD::CALLSEQ_END:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001701 Select(N.getOperand(0));
1702 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001703
Chris Lattner16cd04d2005-05-12 23:24:06 +00001704 Opc = N.getOpcode() == ISD::CALLSEQ_START ? Alpha::ADJUSTSTACKDOWN :
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001705 Alpha::ADJUSTSTACKUP;
1706 BuildMI(BB, Opc, 1).addImm(Tmp1);
1707 return;
Andrew Lenharth95762122005-03-31 21:24:06 +00001708
1709 case ISD::PCMARKER:
1710 Select(N.getOperand(0)); //Chain
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001711 BuildMI(BB, Alpha::PCLABEL, 2)
1712 .addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
Andrew Lenharth95762122005-03-31 21:24:06 +00001713 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001714 }
1715 assert(0 && "Should not be reached!");
1716}
1717
1718
1719/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
1720/// into a machine code representation using pattern matching and a machine
1721/// description file.
1722///
1723FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001724 return new AlphaISel(TM);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001725}
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001726