blob: c30c459c9041074e461c646a67a14c9d6c517ac9 [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 Lenharthaa38ce42005-09-02 18:46:02 +000016#include "AlphaISelLowering.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000017#include "llvm/Constants.h" // FIXME: REMOVE
18#include "llvm/Function.h"
Andrew Lenharthb69f3422005-06-22 17:19:45 +000019#include "llvm/Module.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000020#include "llvm/CodeGen/MachineInstrBuilder.h"
21#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
22#include "llvm/CodeGen/MachineFunction.h"
23#include "llvm/CodeGen/MachineFrameInfo.h"
24#include "llvm/CodeGen/SelectionDAG.h"
25#include "llvm/CodeGen/SelectionDAGISel.h"
26#include "llvm/CodeGen/SSARegMap.h"
27#include "llvm/Target/TargetData.h"
28#include "llvm/Target/TargetLowering.h"
29#include "llvm/Support/MathExtras.h"
30#include "llvm/ADT/Statistic.h"
Andrew Lenharth032f2352005-02-22 21:59:48 +000031#include "llvm/Support/Debug.h"
Andrew Lenharth95762122005-03-31 21:24:06 +000032#include "llvm/Support/CommandLine.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000033#include <set>
Andrew Lenharth684f2292005-01-30 00:35:27 +000034#include <algorithm>
Andrew Lenharth304d0f32005-01-22 23:41:55 +000035using namespace llvm;
36
Andrew Lenharth95762122005-03-31 21:24:06 +000037namespace llvm {
Misha Brukman4633f1c2005-04-21 23:13:11 +000038 cl::opt<bool> EnableAlphaIDIV("enable-alpha-intfpdiv",
Andrew Lenharthd4653b12005-06-27 17:39:17 +000039 cl::desc("Use the FP div instruction for integer div when possible"),
Andrew Lenharth95762122005-03-31 21:24:06 +000040 cl::Hidden);
Andrew Lenharth59009192005-05-04 19:12:09 +000041 cl::opt<bool> EnableAlphaFTOI("enable-alpha-FTOI",
Andrew Lenharthd4653b12005-06-27 17:39:17 +000042 cl::desc("Enable use of ftoi* and itof* instructions (ev6 and higher)"),
Andrew Lenharth95762122005-03-31 21:24:06 +000043 cl::Hidden);
Andrew Lenharth59009192005-05-04 19:12:09 +000044 cl::opt<bool> EnableAlphaCT("enable-alpha-CT",
Andrew Lenharthd4653b12005-06-27 17:39:17 +000045 cl::desc("Enable use of the ctpop, ctlz, and cttz instructions"),
Andrew Lenharth59009192005-05-04 19:12:09 +000046 cl::Hidden);
Misha Brukman4633f1c2005-04-21 23:13:11 +000047 cl::opt<bool> EnableAlphaCount("enable-alpha-count",
Andrew Lenharthd4653b12005-06-27 17:39:17 +000048 cl::desc("Print estimates on live ins and outs"),
49 cl::Hidden);
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +000050 cl::opt<bool> EnableAlphaLSMark("enable-alpha-lsmark",
Andrew Lenharthd4653b12005-06-27 17:39:17 +000051 cl::desc("Emit symbols to correlate Mem ops to LLVM Values"),
52 cl::Hidden);
Andrew Lenharth95762122005-03-31 21:24:06 +000053}
54
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +000055namespace {
Andrew Lenharth304d0f32005-01-22 23:41:55 +000056
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000057//===--------------------------------------------------------------------===//
58/// ISel - Alpha specific code to select Alpha machine instructions for
59/// SelectionDAG operations.
60//===--------------------------------------------------------------------===//
Andrew Lenharthb69f3422005-06-22 17:19:45 +000061class AlphaISel : public SelectionDAGISel {
Misha Brukman4633f1c2005-04-21 23:13:11 +000062
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000063 /// AlphaLowering - This object fully describes how to lower LLVM code to an
64 /// Alpha-specific SelectionDAG.
65 AlphaTargetLowering AlphaLowering;
Misha Brukman4633f1c2005-04-21 23:13:11 +000066
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000067 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
68 // for sdiv and udiv until it is put into the future
69 // dag combiner.
70
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000071 /// ExprMap - As shared expressions are codegen'd, we keep track of which
72 /// vreg the value is produced in, so we only emit one copy of each compiled
73 /// tree.
74 static const unsigned notIn = (unsigned)(-1);
75 std::map<SDOperand, unsigned> ExprMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +000076
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000077 //CCInvMap sometimes (SetNE) we have the inverse CC code for free
78 std::map<SDOperand, unsigned> CCInvMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +000079
Andrew Lenhartha32b9e32005-04-08 17:28:49 +000080 int count_ins;
81 int count_outs;
82 bool has_sym;
Andrew Lenharth500b4db2005-04-22 13:35:18 +000083 int max_depth;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +000084
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000085public:
Jeff Cohen00b168892005-07-27 06:12:32 +000086 AlphaISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering),
Andrew Lenharthd4653b12005-06-27 17:39:17 +000087 AlphaLowering(TM)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000088 {}
Misha Brukman4633f1c2005-04-21 23:13:11 +000089
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000090 /// InstructionSelectBasicBlock - This callback is invoked by
91 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
92 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
Andrew Lenharth032f2352005-02-22 21:59:48 +000093 DEBUG(BB->dump());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +000094 count_ins = 0;
95 count_outs = 0;
Andrew Lenharth500b4db2005-04-22 13:35:18 +000096 max_depth = 0;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +000097 has_sym = false;
98
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000099 // Codegen the basic block.
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000100 ISelDAG = &DAG;
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000101 max_depth = DAG.getRoot().getNodeDepth();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000102 Select(DAG.getRoot());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000103
104 if(has_sym)
105 ++count_ins;
106 if(EnableAlphaCount)
Jeff Cohen00b168892005-07-27 06:12:32 +0000107 std::cerr << "COUNT: "
108 << BB->getParent()->getFunction ()->getName() << " "
109 << BB->getNumber() << " "
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000110 << max_depth << " "
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000111 << count_ins << " "
112 << count_outs << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000113
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000114 // Clear state used for selection.
115 ExprMap.clear();
116 CCInvMap.clear();
117 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000118
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000119 unsigned SelectExpr(SDOperand N);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000120 void Select(SDOperand N);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000121
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000122 void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
123 void SelectBranchCC(SDOperand N);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000124 void MoveFP2Int(unsigned src, unsigned dst, bool isDouble);
125 void MoveInt2FP(unsigned src, unsigned dst, bool isDouble);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000126 //returns whether the sense of the comparison was inverted
127 bool SelectFPSetCC(SDOperand N, unsigned dst);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000128
129 // dag -> dag expanders for integer divide by constant
130 SDOperand BuildSDIVSequence(SDOperand N);
131 SDOperand BuildUDIVSequence(SDOperand N);
132
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000133};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000134}
135
Andrew Lenharthd2284272005-08-15 14:31:37 +0000136static bool isSIntImmediate(SDOperand N, int64_t& Imm) {
137 // test for constant
138 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
139 // retrieve value
140 Imm = CN->getSignExtended();
141 // passes muster
142 return true;
143 }
144 // not a constant
145 return false;
146}
147
148// isSIntImmediateBounded - This method tests to see if a constant operand
149// bounded s.t. low <= Imm <= high
150// If so Imm will receive the 64 bit value.
151static bool isSIntImmediateBounded(SDOperand N, int64_t& Imm,
152 int64_t low, int64_t high) {
Andrew Lenharth035b8ab2005-08-17 00:47:24 +0000153 if (isSIntImmediate(N, Imm) && Imm <= high && Imm >= low)
Andrew Lenharthd2284272005-08-15 14:31:37 +0000154 return true;
155 return false;
156}
157static bool isUIntImmediate(SDOperand N, uint64_t& Imm) {
158 // test for constant
159 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
160 // retrieve value
161 Imm = (uint64_t)CN->getValue();
162 // passes muster
163 return true;
164 }
165 // not a constant
166 return false;
167}
168
169static bool isUIntImmediateBounded(SDOperand N, uint64_t& Imm,
170 uint64_t low, uint64_t high) {
Andrew Lenharth035b8ab2005-08-17 00:47:24 +0000171 if (isUIntImmediate(N, Imm) && Imm <= high && Imm >= low)
Andrew Lenharthd2284272005-08-15 14:31:37 +0000172 return true;
173 return false;
174}
175
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000176static void getValueInfo(const Value* v, int& type, int& fun, int& offset)
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000177{
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000178 fun = type = offset = 0;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000179 if (v == NULL) {
180 type = 0;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000181 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(v)) {
182 type = 1;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000183 const Module* M = GV->getParent();
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000184 for(Module::const_global_iterator ii = M->global_begin(); &*ii != GV; ++ii)
185 ++offset;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000186 } else if (const Argument* Arg = dyn_cast<Argument>(v)) {
187 type = 2;
188 const Function* F = Arg->getParent();
189 const Module* M = F->getParent();
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000190 for(Module::const_iterator ii = M->begin(); &*ii != F; ++ii)
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000191 ++fun;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000192 for(Function::const_arg_iterator ii = F->arg_begin(); &*ii != Arg; ++ii)
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000193 ++offset;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000194 } else if (const Instruction* I = dyn_cast<Instruction>(v)) {
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +0000195 assert(dyn_cast<PointerType>(I->getType()));
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000196 type = 3;
197 const BasicBlock* bb = I->getParent();
198 const Function* F = bb->getParent();
199 const Module* M = F->getParent();
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000200 for(Module::const_iterator ii = M->begin(); &*ii != F; ++ii)
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000201 ++fun;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000202 for(Function::const_iterator ii = F->begin(); &*ii != bb; ++ii)
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000203 offset += ii->size();
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000204 for(BasicBlock::const_iterator ii = bb->begin(); &*ii != I; ++ii)
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000205 ++offset;
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +0000206 } else if (const Constant* C = dyn_cast<Constant>(v)) {
207 //Don't know how to look these up yet
208 type = 0;
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +0000209 } else {
210 assert(0 && "Error in value marking");
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000211 }
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000212 //type = 4: register spilling
213 //type = 5: global address loading or constant loading
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000214}
215
216static int getUID()
217{
218 static int id = 0;
219 return ++id;
220}
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000221
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000222//Factorize a number using the list of constants
223static bool factorize(int v[], int res[], int size, uint64_t c)
224{
225 bool cont = true;
226 while (c != 1 && cont)
227 {
228 cont = false;
229 for(int i = 0; i < size; ++i)
230 {
231 if (c % v[i] == 0)
232 {
233 c /= v[i];
234 ++res[i];
235 cont=true;
236 }
237 }
238 }
239 return c == 1;
240}
241
242
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000243//Shamelessly adapted from PPC32
Misha Brukman4633f1c2005-04-21 23:13:11 +0000244// Structure used to return the necessary information to codegen an SDIV as
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000245// a multiply.
246struct ms {
247 int64_t m; // magic number
248 int64_t s; // shift amount
249};
250
251struct mu {
252 uint64_t m; // magic number
253 int64_t a; // add indicator
254 int64_t s; // shift amount
255};
256
257/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukman4633f1c2005-04-21 23:13:11 +0000258/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000259/// or -1.
260static struct ms magic(int64_t d) {
261 int64_t p;
262 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
263 const uint64_t two63 = 9223372036854775808ULL; // 2^63
264 struct ms mag;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000265
Andrew Lenharth01c8f6e2005-08-01 17:47:28 +0000266 ad = llabs(d);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000267 t = two63 + ((uint64_t)d >> 63);
268 anc = t - 1 - t%ad; // absolute value of nc
Andrew Lenharth320174f2005-04-07 17:19:16 +0000269 p = 63; // initialize p
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000270 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
271 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
272 q2 = two63/ad; // initialize q2 = 2p/abs(d)
273 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
274 do {
275 p = p + 1;
276 q1 = 2*q1; // update q1 = 2p/abs(nc)
277 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
278 if (r1 >= anc) { // must be unsigned comparison
279 q1 = q1 + 1;
280 r1 = r1 - anc;
281 }
282 q2 = 2*q2; // update q2 = 2p/abs(d)
283 r2 = 2*r2; // update r2 = rem(2p/abs(d))
284 if (r2 >= ad) { // must be unsigned comparison
285 q2 = q2 + 1;
286 r2 = r2 - ad;
287 }
288 delta = ad - r2;
289 } while (q1 < delta || (q1 == delta && r1 == 0));
290
291 mag.m = q2 + 1;
292 if (d < 0) mag.m = -mag.m; // resulting magic number
293 mag.s = p - 64; // resulting shift
294 return mag;
295}
296
297/// magicu - calculate the magic numbers required to codegen an integer udiv as
298/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
299static struct mu magicu(uint64_t d)
300{
301 int64_t p;
302 uint64_t nc, delta, q1, r1, q2, r2;
303 struct mu magu;
304 magu.a = 0; // initialize "add" indicator
305 nc = - 1 - (-d)%d;
Andrew Lenharth320174f2005-04-07 17:19:16 +0000306 p = 63; // initialize p
307 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
308 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
309 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
310 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000311 do {
312 p = p + 1;
313 if (r1 >= nc - r1 ) {
314 q1 = 2*q1 + 1; // update q1
315 r1 = 2*r1 - nc; // update r1
316 }
317 else {
318 q1 = 2*q1; // update q1
319 r1 = 2*r1; // update r1
320 }
321 if (r2 + 1 >= d - r2) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000322 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000323 q2 = 2*q2 + 1; // update q2
324 r2 = 2*r2 + 1 - d; // update r2
325 }
326 else {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000327 if (q2 >= 0x8000000000000000ull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000328 q2 = 2*q2; // update q2
329 r2 = 2*r2 + 1; // update r2
330 }
331 delta = d - 1 - r2;
332 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
333 magu.m = q2 + 1; // resulting magic number
Andrew Lenharth320174f2005-04-07 17:19:16 +0000334 magu.s = p - 64; // resulting shift
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000335 return magu;
336}
337
338/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
339/// return a DAG expression to select that will generate the same value by
340/// multiplying by a magic number. See:
341/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000342SDOperand AlphaISel::BuildSDIVSequence(SDOperand N) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000343 int64_t d = (int64_t)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000344 ms magics = magic(d);
345 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000346 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000347 ISelDAG->getConstant(magics.m, MVT::i64));
348 // If d > 0 and m < 0, add the numerator
349 if (d > 0 && magics.m < 0)
350 Q = ISelDAG->getNode(ISD::ADD, MVT::i64, Q, N.getOperand(0));
351 // If d < 0 and m > 0, subtract the numerator.
352 if (d < 0 && magics.m > 0)
353 Q = ISelDAG->getNode(ISD::SUB, MVT::i64, Q, N.getOperand(0));
354 // Shift right algebraic if shift value is nonzero
355 if (magics.s > 0)
Misha Brukman4633f1c2005-04-21 23:13:11 +0000356 Q = ISelDAG->getNode(ISD::SRA, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000357 ISelDAG->getConstant(magics.s, MVT::i64));
358 // Extract the sign bit and add it to the quotient
Misha Brukman4633f1c2005-04-21 23:13:11 +0000359 SDOperand T =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000360 ISelDAG->getNode(ISD::SRL, MVT::i64, Q, ISelDAG->getConstant(63, MVT::i64));
361 return ISelDAG->getNode(ISD::ADD, MVT::i64, Q, T);
362}
363
364/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
365/// return a DAG expression to select that will generate the same value by
366/// multiplying by a magic number. See:
367/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000368SDOperand AlphaISel::BuildUDIVSequence(SDOperand N) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000369 unsigned d =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000370 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
371 mu magics = magicu(d);
372 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000373 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000374 ISelDAG->getConstant(magics.m, MVT::i64));
375 if (magics.a == 0) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000376 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000377 ISelDAG->getConstant(magics.s, MVT::i64));
378 } else {
379 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i64, N.getOperand(0), Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000380 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000381 ISelDAG->getConstant(1, MVT::i64));
382 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i64, NPQ, Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000383 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000384 ISelDAG->getConstant(magics.s-1, MVT::i64));
385 }
386 return Q;
387}
388
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000389//These describe LDAx
Andrew Lenharthc0513832005-03-29 19:24:04 +0000390static const int IMM_LOW = -32768;
391static const int IMM_HIGH = 32767;
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000392static const int IMM_MULT = 65536;
393
394static long getUpper16(long l)
395{
396 long y = l / IMM_MULT;
397 if (l % IMM_MULT > IMM_HIGH)
398 ++y;
399 return y;
400}
401
402static long getLower16(long l)
403{
404 long h = getUpper16(l);
405 return l - h * IMM_MULT;
406}
407
Andrew Lenharthfe895e32005-06-27 17:15:36 +0000408static unsigned GetRelVersion(unsigned opcode)
409{
410 switch (opcode) {
411 default: assert(0 && "unknown load or store"); return 0;
412 case Alpha::LDQ: return Alpha::LDQr;
413 case Alpha::LDS: return Alpha::LDSr;
414 case Alpha::LDT: return Alpha::LDTr;
415 case Alpha::LDL: return Alpha::LDLr;
416 case Alpha::LDBU: return Alpha::LDBUr;
417 case Alpha::LDWU: return Alpha::LDWUr;
Andrew Lenharthfce587e2005-06-29 00:39:17 +0000418 case Alpha::STB: return Alpha::STBr;
419 case Alpha::STW: return Alpha::STWr;
420 case Alpha::STL: return Alpha::STLr;
421 case Alpha::STQ: return Alpha::STQr;
422 case Alpha::STS: return Alpha::STSr;
423 case Alpha::STT: return Alpha::STTr;
424
Andrew Lenharthfe895e32005-06-27 17:15:36 +0000425 }
426}
Andrew Lenharth65838902005-02-06 16:22:15 +0000427
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000428void AlphaISel::MoveFP2Int(unsigned src, unsigned dst, bool isDouble)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000429{
430 unsigned Opc;
431 if (EnableAlphaFTOI) {
432 Opc = isDouble ? Alpha::FTOIT : Alpha::FTOIS;
Andrew Lenharth98169be2005-07-28 18:14:47 +0000433 BuildMI(BB, Opc, 1, dst).addReg(src).addReg(Alpha::F31);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000434 } else {
435 //The hard way:
436 // Spill the integer to memory and reload it from there.
437 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
438 MachineFunction *F = BB->getParent();
439 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
440
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000441 if (EnableAlphaLSMark)
442 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
443 .addImm(getUID());
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000444 Opc = isDouble ? Alpha::STT : Alpha::STS;
445 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000446
447 if (EnableAlphaLSMark)
448 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
449 .addImm(getUID());
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000450 Opc = isDouble ? Alpha::LDQ : Alpha::LDL;
451 BuildMI(BB, Alpha::LDQ, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
452 }
453}
454
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000455void AlphaISel::MoveInt2FP(unsigned src, unsigned dst, bool isDouble)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000456{
457 unsigned Opc;
458 if (EnableAlphaFTOI) {
459 Opc = isDouble?Alpha::ITOFT:Alpha::ITOFS;
Andrew Lenharth98169be2005-07-28 18:14:47 +0000460 BuildMI(BB, Opc, 1, dst).addReg(src).addReg(Alpha::R31);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000461 } else {
462 //The hard way:
463 // Spill the integer to memory and reload it from there.
464 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
465 MachineFunction *F = BB->getParent();
466 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
467
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000468 if (EnableAlphaLSMark)
469 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
470 .addImm(getUID());
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000471 Opc = isDouble ? Alpha::STQ : Alpha::STL;
472 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000473
474 if (EnableAlphaLSMark)
475 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
476 .addImm(getUID());
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000477 Opc = isDouble ? Alpha::LDT : Alpha::LDS;
478 BuildMI(BB, Opc, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
479 }
480}
481
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000482bool AlphaISel::SelectFPSetCC(SDOperand N, unsigned dst)
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000483{
Chris Lattner88ac32c2005-08-09 20:21:10 +0000484 SDNode *SetCC = N.Val;
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000485 unsigned Opc, Tmp1, Tmp2, Tmp3;
Chris Lattner88ac32c2005-08-09 20:21:10 +0000486 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC->getOperand(2))->get();
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000487 bool rev = false;
488 bool inv = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000489
Chris Lattner88ac32c2005-08-09 20:21:10 +0000490 switch (CC) {
491 default: SetCC->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000492 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
493 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
494 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
495 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
496 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
497 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
498 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000499
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000500 ConstantFPSDNode *CN;
501 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
502 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
503 Tmp1 = Alpha::F31;
504 else
505 Tmp1 = SelectExpr(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000506
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000507 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
508 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
509 Tmp2 = Alpha::F31;
510 else
Chris Lattner9c9183a2005-04-30 04:44:07 +0000511 Tmp2 = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000512
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000513 //Can only compare doubles, and dag won't promote for me
514 if (SetCC->getOperand(0).getValueType() == MVT::f32)
515 {
516 //assert(0 && "Setcc On float?\n");
517 std::cerr << "Setcc on float!\n";
518 Tmp3 = MakeReg(MVT::f64);
Andrew Lenharth98169be2005-07-28 18:14:47 +0000519 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000520 Tmp1 = Tmp3;
521 }
522 if (SetCC->getOperand(1).getValueType() == MVT::f32)
523 {
524 //assert (0 && "Setcc On float?\n");
525 std::cerr << "Setcc on float!\n";
526 Tmp3 = MakeReg(MVT::f64);
Andrew Lenharth98169be2005-07-28 18:14:47 +0000527 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Alpha::F31).addReg(Tmp2);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000528 Tmp2 = Tmp3;
529 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000530
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000531 if (rev) std::swap(Tmp1, Tmp2);
532 //do the comparison
533 BuildMI(BB, Opc, 2, dst).addReg(Tmp1).addReg(Tmp2);
534 return inv;
535}
536
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000537//Check to see if the load is a constant offset from a base register
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000538void AlphaISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000539{
540 unsigned opcode = N.getOpcode();
Andrew Lenharth694c2982005-06-26 23:01:11 +0000541 if (opcode == ISD::ADD && N.getOperand(1).getOpcode() == ISD::Constant &&
542 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
543 { //Normal imm add
544 Reg = SelectExpr(N.getOperand(0));
545 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
546 return;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000547 }
548 Reg = SelectExpr(N);
549 offset = 0;
550 return;
551}
552
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000553void AlphaISel::SelectBranchCC(SDOperand N)
Andrew Lenharth445171a2005-02-08 00:40:03 +0000554{
555 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000556 MachineBasicBlock *Dest =
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000557 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
558 unsigned Opc = Alpha::WTF;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000559
Andrew Lenharth445171a2005-02-08 00:40:03 +0000560 Select(N.getOperand(0)); //chain
561 SDOperand CC = N.getOperand(1);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000562
Andrew Lenharth445171a2005-02-08 00:40:03 +0000563 if (CC.getOpcode() == ISD::SETCC)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000564 {
Chris Lattner88ac32c2005-08-09 20:21:10 +0000565 ISD::CondCode cCode= cast<CondCodeSDNode>(CC.getOperand(2))->get();
566 if (MVT::isInteger(CC.getOperand(0).getValueType())) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000567 //Dropping the CC is only useful if we are comparing to 0
Chris Lattner88ac32c2005-08-09 20:21:10 +0000568 bool RightZero = CC.getOperand(1).getOpcode() == ISD::Constant &&
569 cast<ConstantSDNode>(CC.getOperand(1))->getValue() == 0;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000570 bool isNE = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000571
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000572 //Fix up CC
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000573 if(cCode == ISD::SETNE)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000574 isNE = true;
Andrew Lenharth445171a2005-02-08 00:40:03 +0000575
Andrew Lenharth694c2982005-06-26 23:01:11 +0000576 if (RightZero) {
Andrew Lenharth09552bf2005-06-08 18:02:21 +0000577 switch (cCode) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000578 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
579 case ISD::SETEQ: Opc = Alpha::BEQ; break;
580 case ISD::SETLT: Opc = Alpha::BLT; break;
581 case ISD::SETLE: Opc = Alpha::BLE; break;
582 case ISD::SETGT: Opc = Alpha::BGT; break;
583 case ISD::SETGE: Opc = Alpha::BGE; break;
584 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
585 case ISD::SETUGT: Opc = Alpha::BNE; break;
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000586 //Technically you could have this CC
587 case ISD::SETULE: Opc = Alpha::BEQ; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000588 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
589 case ISD::SETNE: Opc = Alpha::BNE; break;
590 }
Chris Lattner88ac32c2005-08-09 20:21:10 +0000591 unsigned Tmp1 = SelectExpr(CC.getOperand(0)); //Cond
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000592 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
593 return;
594 } else {
595 unsigned Tmp1 = SelectExpr(CC);
596 if (isNE)
597 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
598 else
599 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000600 return;
601 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000602 } else { //FP
Jeff Cohen00b168892005-07-27 06:12:32 +0000603 //Any comparison between 2 values should be codegened as an folded
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000604 //branch, as moving CC to the integer register is very expensive
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000605 //for a cmp b: c = a - b;
606 //a = b: c = 0
607 //a < b: c < 0
608 //a > b: c > 0
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000609
610 bool invTest = false;
611 unsigned Tmp3;
612
613 ConstantFPSDNode *CN;
Chris Lattner88ac32c2005-08-09 20:21:10 +0000614 if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(1)))
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000615 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
Chris Lattner88ac32c2005-08-09 20:21:10 +0000616 Tmp3 = SelectExpr(CC.getOperand(0));
617 else if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(0)))
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000618 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
619 {
Chris Lattner88ac32c2005-08-09 20:21:10 +0000620 Tmp3 = SelectExpr(CC.getOperand(1));
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000621 invTest = true;
622 }
623 else
624 {
Chris Lattner88ac32c2005-08-09 20:21:10 +0000625 unsigned Tmp1 = SelectExpr(CC.getOperand(0));
626 unsigned Tmp2 = SelectExpr(CC.getOperand(1));
627 bool isD = CC.getOperand(0).getValueType() == MVT::f64;
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000628 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
629 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
630 .addReg(Tmp1).addReg(Tmp2);
631 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000632
Chris Lattner88ac32c2005-08-09 20:21:10 +0000633 switch (cCode) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000634 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000635 case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
636 case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
637 case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
638 case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
639 case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
640 case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000641 }
642 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000643 return;
644 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000645 abort(); //Should never be reached
646 } else {
647 //Giveup and do the stupid thing
648 unsigned Tmp1 = SelectExpr(CC);
649 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
650 return;
651 }
Andrew Lenharth445171a2005-02-08 00:40:03 +0000652 abort(); //Should never be reached
653}
654
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000655unsigned AlphaISel::SelectExpr(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000656 unsigned Result;
Andrew Lenharth2966e842005-04-07 18:15:28 +0000657 unsigned Tmp1, Tmp2 = 0, Tmp3;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000658 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000659 unsigned opcode = N.getOpcode();
Andrew Lenharthd2284272005-08-15 14:31:37 +0000660 int64_t SImm;
661 uint64_t UImm;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000662
663 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000664 MVT::ValueType DestType = N.getValueType();
Andrew Lenharthf4da9452005-06-29 12:49:51 +0000665 bool isFP = DestType == MVT::f64 || DestType == MVT::f32;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000666
667 unsigned &Reg = ExprMap[N];
668 if (Reg) return Reg;
669
Andrew Lenharth46a776e2005-09-06 17:00:23 +0000670 switch(N.getOpcode()) {
671 default:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000672 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000673 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth46a776e2005-09-06 17:00:23 +0000674 break;
675 case ISD::AssertSext:
676 case ISD::AssertZext:
677 return Reg = SelectExpr(N.getOperand(0));
678 case ISD::CALL:
679 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000680 // If this is a call instruction, make sure to prepare ALL of the result
681 // values as well as the chain.
682 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000683 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000684 else {
685 Result = MakeReg(Node->getValueType(0));
686 ExprMap[N.getValue(0)] = Result;
687 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
688 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000689 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000690 }
Andrew Lenharth46a776e2005-09-06 17:00:23 +0000691 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000692 }
693
Andrew Lenharth40831c52005-01-28 06:57:18 +0000694 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000695 default:
696 Node->dump();
697 assert(0 && "Node not handled!\n");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000698
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000699 case ISD::CTPOP:
700 case ISD::CTTZ:
701 case ISD::CTLZ:
702 Opc = opcode == ISD::CTPOP ? Alpha::CTPOP :
703 (opcode == ISD::CTTZ ? Alpha::CTTZ : Alpha::CTLZ);
704 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthf3f951a2005-07-22 20:50:29 +0000705 BuildMI(BB, Opc, 1, Result).addReg(Alpha::R31).addReg(Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000706 return Result;
707
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000708 case ISD::MULHU:
709 Tmp1 = SelectExpr(N.getOperand(0));
710 Tmp2 = SelectExpr(N.getOperand(1));
711 BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth706be912005-04-07 13:55:53 +0000712 return Result;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000713 case ISD::MULHS:
714 {
715 //MULHU - Ra<63>*Rb - Rb<63>*Ra
716 Tmp1 = SelectExpr(N.getOperand(0));
717 Tmp2 = SelectExpr(N.getOperand(1));
718 Tmp3 = MakeReg(MVT::i64);
719 BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
720 unsigned V1 = MakeReg(MVT::i64);
721 unsigned V2 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000722 BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31)
723 .addReg(Tmp1);
724 BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31)
725 .addReg(Tmp2);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000726 unsigned IRes = MakeReg(MVT::i64);
727 BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
728 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
729 return Result;
730 }
Andrew Lenharth7332f3e2005-04-02 19:11:07 +0000731 case ISD::UNDEF: {
732 BuildMI(BB, Alpha::IDEF, 0, Result);
733 return Result;
734 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000735
Andrew Lenharth032f2352005-02-22 21:59:48 +0000736 case ISD::DYNAMIC_STACKALLOC:
737 // Generate both result values.
Andrew Lenharth3a7118d2005-02-23 17:33:42 +0000738 if (Result != notIn)
739 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth032f2352005-02-22 21:59:48 +0000740 else
741 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
742
743 // FIXME: We are currently ignoring the requested alignment for handling
744 // greater than the stack alignment. This will need to be revisited at some
745 // point. Align = N.getOperand(2);
746
747 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
748 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
749 std::cerr << "Cannot allocate stack object with greater alignment than"
750 << " the stack alignment yet!";
751 abort();
752 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000753
Andrew Lenharth032f2352005-02-22 21:59:48 +0000754 Select(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +0000755 if (isSIntImmediateBounded(N.getOperand(1), SImm, 0, 32767))
756 BuildMI(BB, Alpha::LDA, 2, Alpha::R30).addImm(-SImm).addReg(Alpha::R30);
757 else {
Andrew Lenharth032f2352005-02-22 21:59:48 +0000758 Tmp1 = SelectExpr(N.getOperand(1));
759 // Subtract size from stack pointer, thereby allocating some space.
760 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
761 }
762
763 // Put a pointer to the space into the result register, by copying the stack
764 // pointer.
Andrew Lenharth7bc47022005-02-22 23:29:25 +0000765 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
Andrew Lenharth032f2352005-02-22 21:59:48 +0000766 return Result;
767
Andrew Lenharth02c318e2005-06-27 21:02:56 +0000768 case ISD::ConstantPool:
Chris Lattner5839bf22005-08-26 17:15:30 +0000769 Tmp1 = BB->getParent()->getConstantPool()->
770 getConstantPoolIndex(cast<ConstantPoolSDNode>(N)->get());
Andrew Lenharth02c318e2005-06-27 21:02:56 +0000771 AlphaLowering.restoreGP(BB);
772 Tmp2 = MakeReg(MVT::i64);
773 BuildMI(BB, Alpha::LDAHr, 2, Tmp2).addConstantPoolIndex(Tmp1)
774 .addReg(Alpha::R29);
775 BuildMI(BB, Alpha::LDAr, 2, Result).addConstantPoolIndex(Tmp1)
776 .addReg(Tmp2);
777 return Result;
Andrew Lenharth2c594352005-01-29 15:42:07 +0000778
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000779 case ISD::FrameIndex:
Andrew Lenharth032f2352005-02-22 21:59:48 +0000780 BuildMI(BB, Alpha::LDA, 2, Result)
781 .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
782 .addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000783 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000784
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000785 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +0000786 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000787 case ISD::SEXTLOAD:
Misha Brukman4633f1c2005-04-21 23:13:11 +0000788 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000789 {
790 // Make sure we generate both values.
791 if (Result != notIn)
792 ExprMap[N.getValue(1)] = notIn; // Generate the token
793 else
794 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +0000795
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000796 SDOperand Chain = N.getOperand(0);
797 SDOperand Address = N.getOperand(1);
798 Select(Chain);
799
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000800 bool fpext = true;
801
Andrew Lenharth03824012005-02-07 05:55:55 +0000802 if (opcode == ISD::LOAD)
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000803 switch (Node->getValueType(0)) {
804 default: Node->dump(); assert(0 && "Bad load!");
805 case MVT::i64: Opc = Alpha::LDQ; break;
806 case MVT::f64: Opc = Alpha::LDT; break;
807 case MVT::f32: Opc = Alpha::LDS; break;
808 }
Andrew Lenharth03824012005-02-07 05:55:55 +0000809 else
Chris Lattnerbce81ae2005-07-10 01:56:13 +0000810 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000811 default: Node->dump(); assert(0 && "Bad sign extend!");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000812 case MVT::i32: Opc = Alpha::LDL;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000813 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000814 case MVT::i16: Opc = Alpha::LDWU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000815 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +0000816 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Misha Brukman4633f1c2005-04-21 23:13:11 +0000817 case MVT::i8: Opc = Alpha::LDBU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000818 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000819 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000820
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000821 int i, j, k;
822 if (EnableAlphaLSMark)
823 getValueInfo(dyn_cast<SrcValueSDNode>(N.getOperand(2))->getValue(),
824 i, j, k);
825
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000826 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Address);
827 if (GASD && !GASD->getGlobal()->isExternal()) {
828 Tmp1 = MakeReg(MVT::i64);
829 AlphaLowering.restoreGP(BB);
830 BuildMI(BB, Alpha::LDAHr, 2, Tmp1)
831 .addGlobalAddress(GASD->getGlobal()).addReg(Alpha::R29);
832 if (EnableAlphaLSMark)
833 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
834 .addImm(getUID());
835 BuildMI(BB, GetRelVersion(Opc), 2, Result)
836 .addGlobalAddress(GASD->getGlobal()).addReg(Tmp1);
Chris Lattnerbce81ae2005-07-10 01:56:13 +0000837 } else if (ConstantPoolSDNode *CP =
838 dyn_cast<ConstantPoolSDNode>(Address)) {
Chris Lattner5839bf22005-08-26 17:15:30 +0000839 unsigned CPIdx = BB->getParent()->getConstantPool()->
840 getConstantPoolIndex(CP->get());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000841 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000842 has_sym = true;
Andrew Lenharthfe895e32005-06-27 17:15:36 +0000843 Tmp1 = MakeReg(MVT::i64);
Chris Lattner5839bf22005-08-26 17:15:30 +0000844 BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CPIdx)
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000845 .addReg(Alpha::R29);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000846 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000847 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
848 .addImm(getUID());
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000849 BuildMI(BB, GetRelVersion(Opc), 2, Result)
Chris Lattner5839bf22005-08-26 17:15:30 +0000850 .addConstantPoolIndex(CPIdx).addReg(Tmp1);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000851 } else if(Address.getOpcode() == ISD::FrameIndex) {
852 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000853 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
854 .addImm(getUID());
Andrew Lenharth032f2352005-02-22 21:59:48 +0000855 BuildMI(BB, Opc, 2, Result)
856 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
857 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000858 } else {
859 long offset;
860 SelectAddr(Address, Tmp1, offset);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000861 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000862 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
863 .addImm(getUID());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000864 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
865 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000866 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000867 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000868
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000869 case ISD::GlobalAddress:
870 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000871 has_sym = true;
Jeff Cohen00b168892005-07-27 06:12:32 +0000872
Andrew Lenharth2f5bca52005-07-03 20:06:13 +0000873 Reg = Result = MakeReg(MVT::i64);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000874
875 if (EnableAlphaLSMark)
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000876 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000877 .addImm(getUID());
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000878
879 BuildMI(BB, Alpha::LDQl, 2, Result)
Andrew Lenharthc95d9842005-06-27 21:11:40 +0000880 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal())
881 .addReg(Alpha::R29);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000882 return Result;
883
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000884 case ISD::ExternalSymbol:
885 AlphaLowering.restoreGP(BB);
886 has_sym = true;
887
Andrew Lenharth2f5bca52005-07-03 20:06:13 +0000888 Reg = Result = MakeReg(MVT::i64);
889
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000890 if (EnableAlphaLSMark)
891 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
892 .addImm(getUID());
893
894 BuildMI(BB, Alpha::LDQl, 2, Result)
895 .addExternalSymbol(cast<ExternalSymbolSDNode>(N)->getSymbol())
896 .addReg(Alpha::R29);
897 return Result;
898
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +0000899 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000900 case ISD::CALL:
901 {
902 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000903
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000904 // The chain for this call is now lowered.
Andrew Lenharthf3f951a2005-07-22 20:50:29 +0000905 ExprMap[N.getValue(Node->getNumValues()-1)] = notIn;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000906
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000907 //grab the arguments
908 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000909 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000910 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000911 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000912
Andrew Lenharth684f2292005-01-30 00:35:27 +0000913 //in reg args
914 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000915 {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000916 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000917 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000918 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000919 Alpha::F19, Alpha::F20, Alpha::F21};
920 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000921 default:
922 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000923 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000924 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000925 N.getOperand(i+2).getValueType() << "\n";
926 assert(0 && "Unknown value type for call");
927 case MVT::i1:
928 case MVT::i8:
929 case MVT::i16:
930 case MVT::i32:
931 case MVT::i64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000932 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i])
933 .addReg(argvregs[i]);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000934 break;
935 case MVT::f32:
936 case MVT::f64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000937 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i])
938 .addReg(argvregs[i]);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000939 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000940 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000941 }
Andrew Lenharth684f2292005-01-30 00:35:27 +0000942 //in mem args
943 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000944 {
945 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000946 default:
947 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000948 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000949 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000950 N.getOperand(i+2).getValueType() << "\n";
951 assert(0 && "Unknown value type for call");
952 case MVT::i1:
953 case MVT::i8:
954 case MVT::i16:
955 case MVT::i32:
956 case MVT::i64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000957 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
958 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000959 break;
960 case MVT::f32:
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000961 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
962 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000963 break;
964 case MVT::f64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000965 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
966 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000967 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000968 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000969 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000970 //build the right kind of call
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000971 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(N.getOperand(1));
972 if (GASD && !GASD->getGlobal()->isExternal()) {
973 //use PC relative branch call
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000974 AlphaLowering.restoreGP(BB);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000975 BuildMI(BB, Alpha::BSR, 1, Alpha::R26)
976 .addGlobalAddress(GASD->getGlobal(),true);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000977 } else {
978 //no need to restore GP as we are doing an indirect call
979 Tmp1 = SelectExpr(N.getOperand(1));
980 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
981 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
982 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000983
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000984 //push the result into a virtual register
Misha Brukman4633f1c2005-04-21 23:13:11 +0000985
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000986 switch (Node->getValueType(0)) {
987 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000988 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000989 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +0000990 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
991 break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000992 case MVT::f32:
993 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +0000994 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
995 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000996 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000997 return Result+N.ResNo;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000998 }
999
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001000 case ISD::SIGN_EXTEND_INREG:
1001 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001002 //do SDIV opt for all levels of ints if not dividing by a constant
1003 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
1004 && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001005 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001006 unsigned Tmp4 = MakeReg(MVT::f64);
1007 unsigned Tmp5 = MakeReg(MVT::f64);
1008 unsigned Tmp6 = MakeReg(MVT::f64);
1009 unsigned Tmp7 = MakeReg(MVT::f64);
1010 unsigned Tmp8 = MakeReg(MVT::f64);
1011 unsigned Tmp9 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001012
1013 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1014 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1015 MoveInt2FP(Tmp1, Tmp4, true);
1016 MoveInt2FP(Tmp2, Tmp5, true);
Andrew Lenharth98169be2005-07-28 18:14:47 +00001017 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Alpha::F31).addReg(Tmp4);
1018 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Alpha::F31).addReg(Tmp5);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001019 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
Andrew Lenharth98169be2005-07-28 18:14:47 +00001020 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Alpha::F31).addReg(Tmp8);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001021 MoveFP2Int(Tmp9, Result, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001022 return Result;
1023 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001024
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001025 //Alpha has instructions for a bunch of signed 32 bit stuff
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001026 if(cast<VTSDNode>(Node->getOperand(1))->getVT() == MVT::i32) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001027 switch (N.getOperand(0).getOpcode()) {
1028 case ISD::ADD:
1029 case ISD::SUB:
1030 case ISD::MUL:
1031 {
1032 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
1033 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
1034 //FIXME: first check for Scaled Adds and Subs!
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001035 if(!isMul && N.getOperand(0).getOperand(0).getOpcode() == ISD::SHL &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001036 isSIntImmediateBounded(N.getOperand(0).getOperand(0).getOperand(1), SImm, 2, 3))
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001037 {
Andrew Lenharthd2284272005-08-15 14:31:37 +00001038 bool use4 = SImm == 2;
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001039 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1040 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1041 BuildMI(BB, isAdd?(use4?Alpha::S4ADDL:Alpha::S8ADDL):(use4?Alpha::S4SUBL:Alpha::S8SUBL),
1042 2,Result).addReg(Tmp1).addReg(Tmp2);
1043 }
1044 else if(isAdd && N.getOperand(0).getOperand(1).getOpcode() == ISD::SHL &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001045 isSIntImmediateBounded(N.getOperand(0).getOperand(1).getOperand(1), SImm, 2, 3))
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001046 {
Andrew Lenharthd2284272005-08-15 14:31:37 +00001047 bool use4 = SImm == 2;
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001048 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1049 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1050 BuildMI(BB, use4?Alpha::S4ADDL:Alpha::S8ADDL, 2,Result).addReg(Tmp1).addReg(Tmp2);
1051 }
Andrew Lenharthd2284272005-08-15 14:31:37 +00001052 else if(isSIntImmediateBounded(N.getOperand(0).getOperand(1), SImm, 0, 255))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001053 { //Normal imm add/sub
1054 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001055 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001056 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001057 }
Andrew Lenharthd2284272005-08-15 14:31:37 +00001058 else if(!isMul && isSIntImmediate(N.getOperand(0).getOperand(1), SImm) &&
1059 (((SImm << 32) >> 32) >= -255) && (((SImm << 32) >> 32) <= 0))
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001060 { //handle canonicalization
1061 Opc = isAdd ? Alpha::SUBLi : Alpha::ADDLi;
1062 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001063 SImm = 0 - ((SImm << 32) >> 32);
1064 assert(SImm >= 0 && SImm <= 255);
1065 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001066 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001067 else
1068 { //Normal add/sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001069 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULL : Alpha::SUBL);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001070 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001071 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001072 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1073 }
1074 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001075 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001076 default: break; //Fall Though;
1077 }
1078 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001079 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001080 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001081 switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001082 default:
1083 Node->dump();
1084 assert(0 && "Sign Extend InReg not there yet");
1085 break;
1086 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001087 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001088 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001089 break;
1090 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001091 case MVT::i16:
Andrew Lenharthf3f951a2005-07-22 20:50:29 +00001092 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Alpha::R31).addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001093 break;
1094 case MVT::i8:
Andrew Lenharthf3f951a2005-07-22 20:50:29 +00001095 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Alpha::R31).addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001096 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +00001097 case MVT::i1:
1098 Tmp2 = MakeReg(MVT::i64);
1099 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001100 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +00001101 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001102 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001103 return Result;
1104 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001105
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001106 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001107 {
Chris Lattner88ac32c2005-08-09 20:21:10 +00001108 ISD::CondCode CC = cast<CondCodeSDNode>(N.getOperand(2))->get();
1109 if (MVT::isInteger(N.getOperand(0).getValueType())) {
1110 bool isConst = false;
1111 int dir;
Misha Brukman7847fca2005-04-22 17:54:37 +00001112
Chris Lattner88ac32c2005-08-09 20:21:10 +00001113 //Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001114 if(isSIntImmediate(N.getOperand(1), SImm) && SImm <= 255 && SImm >= 0)
Chris Lattner88ac32c2005-08-09 20:21:10 +00001115 isConst = true;
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001116
Chris Lattner88ac32c2005-08-09 20:21:10 +00001117 switch (CC) {
1118 default: Node->dump(); assert(0 && "Unknown integer comparison!");
1119 case ISD::SETEQ:
1120 Opc = isConst ? Alpha::CMPEQi : Alpha::CMPEQ; dir=1; break;
1121 case ISD::SETLT:
1122 Opc = isConst ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
1123 case ISD::SETLE:
1124 Opc = isConst ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
1125 case ISD::SETGT: Opc = Alpha::CMPLT; dir = 2; break;
1126 case ISD::SETGE: Opc = Alpha::CMPLE; dir = 2; break;
1127 case ISD::SETULT:
1128 Opc = isConst ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
1129 case ISD::SETUGT: Opc = Alpha::CMPULT; dir = 2; break;
1130 case ISD::SETULE:
1131 Opc = isConst ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
1132 case ISD::SETUGE: Opc = Alpha::CMPULE; dir = 2; break;
1133 case ISD::SETNE: {//Handle this one special
1134 //std::cerr << "Alpha does not have a setne.\n";
1135 //abort();
1136 Tmp1 = SelectExpr(N.getOperand(0));
1137 Tmp2 = SelectExpr(N.getOperand(1));
1138 Tmp3 = MakeReg(MVT::i64);
1139 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1140 //Remeber we have the Inv for this CC
1141 CCInvMap[N] = Tmp3;
1142 //and invert
1143 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
1144 return Result;
1145 }
1146 }
1147 if (dir == 1) {
1148 Tmp1 = SelectExpr(N.getOperand(0));
1149 if (isConst) {
Andrew Lenharthd2284272005-08-15 14:31:37 +00001150 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Chris Lattner88ac32c2005-08-09 20:21:10 +00001151 } else {
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001152 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth694c2982005-06-26 23:01:11 +00001153 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001154 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001155 } else { //if (dir == 2) {
1156 Tmp1 = SelectExpr(N.getOperand(1));
1157 Tmp2 = SelectExpr(N.getOperand(0));
1158 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001159 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001160 } else {
1161 //do the comparison
1162 Tmp1 = MakeReg(MVT::f64);
1163 bool inv = SelectFPSetCC(N, Tmp1);
1164
1165 //now arrange for Result (int) to have a 1 or 0
1166 Tmp2 = MakeReg(MVT::i64);
1167 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
1168 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
1169 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
Andrew Lenharth9818c052005-02-05 13:19:12 +00001170 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001171 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001172 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001173
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001174 case ISD::CopyFromReg:
1175 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001176 ++count_ins;
1177
Andrew Lenharth40831c52005-01-28 06:57:18 +00001178 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001179 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001180 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001181 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001182 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001183
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001184 SDOperand Chain = N.getOperand(0);
1185
1186 Select(Chain);
Chris Lattner707ebc52005-08-16 21:56:37 +00001187 unsigned r = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001188 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
Andrew Lenharth619fb522005-07-04 20:07:21 +00001189 if (MVT::isFloatingPoint(N.getValue(0).getValueType()))
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001190 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
1191 else
1192 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001193 return Result;
1194 }
1195
Misha Brukman4633f1c2005-04-21 23:13:11 +00001196 //Most of the plain arithmetic and logic share the same form, and the same
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001197 //constant immediate test
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001198 case ISD::XOR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001199 //Match Not
Andrew Lenharthd2284272005-08-15 14:31:37 +00001200 if (isSIntImmediate(N.getOperand(1), SImm) && SImm == -1) {
1201 Tmp1 = SelectExpr(N.getOperand(0));
1202 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1203 return Result;
1204 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001205 //Fall through
1206 case ISD::AND:
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001207 //handle zap
Andrew Lenharthd2284272005-08-15 14:31:37 +00001208 if (opcode == ISD::AND && isUIntImmediate(N.getOperand(1), UImm))
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001209 {
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001210 unsigned int build = 0;
1211 for(int i = 0; i < 8; ++i)
1212 {
Andrew Lenharthd2284272005-08-15 14:31:37 +00001213 if ((UImm & 0x00FF) == 0x00FF)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001214 build |= 1 << i;
Andrew Lenharthd2284272005-08-15 14:31:37 +00001215 else if ((UImm & 0x00FF) != 0)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001216 { build = 0; break; }
Andrew Lenharthd2284272005-08-15 14:31:37 +00001217 UImm >>= 8;
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001218 }
1219 if (build)
1220 {
1221 Tmp1 = SelectExpr(N.getOperand(0));
1222 BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1223 return Result;
1224 }
1225 }
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001226 case ISD::OR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001227 //Check operand(0) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001228 if (N.getOperand(0).getOpcode() == ISD::XOR &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001229 isSIntImmediate(N.getOperand(0).getOperand(1), SImm) && SImm == -1) {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001230 switch(opcode) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001231 case ISD::AND: Opc = Alpha::BIC; break;
1232 case ISD::OR: Opc = Alpha::ORNOT; break;
1233 case ISD::XOR: Opc = Alpha::EQV; break;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001234 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001235 Tmp1 = SelectExpr(N.getOperand(1));
1236 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1237 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1238 return Result;
1239 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001240 //Check operand(1) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001241 if (N.getOperand(1).getOpcode() == ISD::XOR &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001242 isSIntImmediate(N.getOperand(1).getOperand(1), SImm) && SImm == -1) {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001243 switch(opcode) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001244 case ISD::AND: Opc = Alpha::BIC; break;
1245 case ISD::OR: Opc = Alpha::ORNOT; break;
1246 case ISD::XOR: Opc = Alpha::EQV; break;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001247 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001248 Tmp1 = SelectExpr(N.getOperand(0));
1249 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1250 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1251 return Result;
1252 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001253 //Fall through
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001254 case ISD::SHL:
1255 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001256 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001257 case ISD::MUL:
Andrew Lenharthd2284272005-08-15 14:31:37 +00001258 if(isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255)) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001259 switch(opcode) {
1260 case ISD::AND: Opc = Alpha::ANDi; break;
1261 case ISD::OR: Opc = Alpha::BISi; break;
1262 case ISD::XOR: Opc = Alpha::XORi; break;
1263 case ISD::SHL: Opc = Alpha::SLi; break;
1264 case ISD::SRL: Opc = Alpha::SRLi; break;
1265 case ISD::SRA: Opc = Alpha::SRAi; break;
1266 case ISD::MUL: Opc = Alpha::MULQi; break;
1267 };
1268 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001269 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001270 } else {
1271 switch(opcode) {
1272 case ISD::AND: Opc = Alpha::AND; break;
1273 case ISD::OR: Opc = Alpha::BIS; break;
1274 case ISD::XOR: Opc = Alpha::XOR; break;
1275 case ISD::SHL: Opc = Alpha::SL; break;
1276 case ISD::SRL: Opc = Alpha::SRL; break;
1277 case ISD::SRA: Opc = Alpha::SRA; break;
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001278 case ISD::MUL: Opc = Alpha::MULQ; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001279 };
1280 Tmp1 = SelectExpr(N.getOperand(0));
1281 Tmp2 = SelectExpr(N.getOperand(1));
1282 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1283 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001284 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001285
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001286 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001287 case ISD::SUB:
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001288 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001289 bool isAdd = opcode == ISD::ADD;
1290
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001291 //first check for Scaled Adds and Subs!
1292 //Valid for add and sub
Andrew Lenharthd2284272005-08-15 14:31:37 +00001293 if(N.getOperand(0).getOpcode() == ISD::SHL &&
1294 isSIntImmediate(N.getOperand(0).getOperand(1), SImm) &&
1295 (SImm == 2 || SImm == 3)) {
1296 bool use4 = SImm == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001297 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001298 if (isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255))
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001299 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
Andrew Lenharthd2284272005-08-15 14:31:37 +00001300 2, Result).addReg(Tmp2).addImm(SImm);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001301 else {
1302 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001303 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1304 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001305 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001306 }
1307 //Position prevents subs
Andrew Lenharth273a1f92005-04-07 14:18:13 +00001308 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001309 isSIntImmediate(N.getOperand(1).getOperand(1), SImm) &&
1310 (SImm == 2 || SImm == 3)) {
1311 bool use4 = SImm == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001312 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001313 if (isSIntImmediateBounded(N.getOperand(0), SImm, 0, 255))
1314 BuildMI(BB, use4?Alpha::S4ADDQi:Alpha::S8ADDQi, 2, Result).addReg(Tmp2).addImm(SImm);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001315 else {
1316 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001317 BuildMI(BB, use4?Alpha::S4ADDQ:Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001318 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001319 }
1320 //small addi
Andrew Lenharthd2284272005-08-15 14:31:37 +00001321 else if(isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001322 { //Normal imm add/sub
1323 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1324 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001325 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001326 }
Andrew Lenharthd2284272005-08-15 14:31:37 +00001327 else if(isSIntImmediateBounded(N.getOperand(1), SImm, -255, 0))
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001328 { //inverted imm add/sub
1329 Opc = isAdd ? Alpha::SUBQi : Alpha::ADDQi;
1330 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001331 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(-SImm);
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001332 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001333 //larger addi
Andrew Lenharthd2284272005-08-15 14:31:37 +00001334 else if(isSIntImmediateBounded(N.getOperand(1), SImm, -32767, 32767))
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001335 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001336 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001337 if (!isAdd)
Andrew Lenharthd2284272005-08-15 14:31:37 +00001338 SImm = -SImm;
1339 BuildMI(BB, Alpha::LDA, 2, Result).addImm(SImm).addReg(Tmp1);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001340 }
1341 //give up and do the operation
1342 else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001343 //Normal add/sub
1344 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1345 Tmp1 = SelectExpr(N.getOperand(0));
1346 Tmp2 = SelectExpr(N.getOperand(1));
1347 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1348 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001349 return Result;
1350 }
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001351 case ISD::FADD:
1352 case ISD::FSUB:
1353 case ISD::FMUL:
1354 case ISD::FDIV: {
1355 if (opcode == ISD::FADD)
1356 Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS;
1357 else if (opcode == ISD::FSUB)
1358 Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS;
1359 else if (opcode == ISD::FMUL)
1360 Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS;
1361 else
1362 Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS;
1363 Tmp1 = SelectExpr(N.getOperand(0));
1364 Tmp2 = SelectExpr(N.getOperand(1));
1365 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1366 return Result;
1367 }
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001368 case ISD::SDIV:
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001369 {
Andrew Lenhartha565c272005-04-06 22:03:13 +00001370 //check if we can convert into a shift!
Andrew Lenharthd2284272005-08-15 14:31:37 +00001371 if (isSIntImmediate(N.getOperand(1), SImm) &&
1372 SImm != 0 && isPowerOf2_64(llabs(SImm))) {
1373 unsigned k = Log2_64(llabs(SImm));
Andrew Lenhartha565c272005-04-06 22:03:13 +00001374 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenhartha565c272005-04-06 22:03:13 +00001375 if (k == 1)
1376 Tmp2 = Tmp1;
1377 else
1378 {
1379 Tmp2 = MakeReg(MVT::i64);
1380 BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
1381 }
1382 Tmp3 = MakeReg(MVT::i64);
1383 BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
1384 unsigned Tmp4 = MakeReg(MVT::i64);
1385 BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharthd2284272005-08-15 14:31:37 +00001386 if (SImm > 0)
Andrew Lenhartha565c272005-04-06 22:03:13 +00001387 BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
1388 else
1389 {
1390 unsigned Tmp5 = MakeReg(MVT::i64);
1391 BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
1392 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
1393 }
1394 return Result;
1395 }
1396 }
1397 //Else fall through
1398
1399 case ISD::UDIV:
1400 {
Andrew Lenharthd2284272005-08-15 14:31:37 +00001401 if (isSIntImmediate(N.getOperand(1), SImm) && (SImm >= 2 || SImm <= -2))
Andrew Lenhartha565c272005-04-06 22:03:13 +00001402 {
1403 // If this is a divide by constant, we can emit code using some magic
1404 // constants to implement it as a multiply instead.
1405 ExprMap.erase(N);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001406 if (opcode == ISD::SDIV)
Andrew Lenhartha565c272005-04-06 22:03:13 +00001407 return SelectExpr(BuildSDIVSequence(N));
1408 else
1409 return SelectExpr(BuildUDIVSequence(N));
1410 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001411 }
1412 //else fall though
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001413 case ISD::UREM:
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001414 case ISD::SREM: {
1415 const char* opstr = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001416 switch(opcode) {
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001417 case ISD::UREM: opstr = "__remqu"; break;
1418 case ISD::SREM: opstr = "__remq"; break;
1419 case ISD::UDIV: opstr = "__divqu"; break;
1420 case ISD::SDIV: opstr = "__divq"; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001421 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001422 Tmp1 = SelectExpr(N.getOperand(0));
1423 Tmp2 = SelectExpr(N.getOperand(1));
Jeff Cohen00b168892005-07-27 06:12:32 +00001424 SDOperand Addr =
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001425 ISelDAG->getExternalSymbol(opstr, AlphaLowering.getPointerTy());
1426 Tmp3 = SelectExpr(Addr);
Andrew Lenharth33819132005-03-04 20:09:23 +00001427 //set up regs explicitly (helps Reg alloc)
1428 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001429 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001430 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp3).addReg(Tmp3);
1431 BuildMI(BB, Alpha::JSRs, 2, Alpha::R23).addReg(Alpha::R27).addImm(0);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001432 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001433 return Result;
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001434 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001435
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001436 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001437 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001438 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001439 assert (DestType == MVT::i64 && "only quads can be loaded to");
1440 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00001441 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001442 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001443 if (SrcType == MVT::f32)
Misha Brukman7847fca2005-04-22 17:54:37 +00001444 {
1445 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth98169be2005-07-28 18:14:47 +00001446 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Alpha::F31).addReg(Tmp1);
Misha Brukman7847fca2005-04-22 17:54:37 +00001447 Tmp1 = Tmp2;
1448 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001449 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth98169be2005-07-28 18:14:47 +00001450 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001451 MoveFP2Int(Tmp2, Result, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001452
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001453 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001454 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001455
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001456 case ISD::SELECT:
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001457 if (isFP) {
1458 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1459 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
1460 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
1461
1462 SDOperand CC = N.getOperand(0);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001463
Chris Lattner88ac32c2005-08-09 20:21:10 +00001464 if (CC.getOpcode() == ISD::SETCC &&
1465 !MVT::isInteger(CC.getOperand(0).getValueType())) {
1466 //FP Setcc -> Select yay!
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001467
Jeff Cohen00b168892005-07-27 06:12:32 +00001468
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001469 //for a cmp b: c = a - b;
1470 //a = b: c = 0
1471 //a < b: c < 0
1472 //a > b: c > 0
1473
1474 bool invTest = false;
1475 unsigned Tmp3;
1476
1477 ConstantFPSDNode *CN;
Chris Lattner88ac32c2005-08-09 20:21:10 +00001478 if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(1)))
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001479 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
Chris Lattner88ac32c2005-08-09 20:21:10 +00001480 Tmp3 = SelectExpr(CC.getOperand(0));
1481 else if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(0)))
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001482 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1483 {
Chris Lattner88ac32c2005-08-09 20:21:10 +00001484 Tmp3 = SelectExpr(CC.getOperand(1));
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001485 invTest = true;
1486 }
1487 else
1488 {
Chris Lattner88ac32c2005-08-09 20:21:10 +00001489 unsigned Tmp1 = SelectExpr(CC.getOperand(0));
1490 unsigned Tmp2 = SelectExpr(CC.getOperand(1));
1491 bool isD = CC.getOperand(0).getValueType() == MVT::f64;
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001492 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1493 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1494 .addReg(Tmp1).addReg(Tmp2);
1495 }
1496
Chris Lattner88ac32c2005-08-09 20:21:10 +00001497 switch (cast<CondCodeSDNode>(CC.getOperand(2))->get()) {
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001498 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
1499 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNE : Alpha::FCMOVEQ; break;
1500 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGT : Alpha::FCMOVLT; break;
1501 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGE : Alpha::FCMOVLE; break;
1502 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLT : Alpha::FCMOVGT; break;
1503 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLE : Alpha::FCMOVGE; break;
1504 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQ : Alpha::FCMOVNE; break;
1505 }
1506 BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
1507 return Result;
1508 }
1509 else
1510 {
1511 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1512 BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV)
1513 .addReg(Tmp1);
1514// // Spill the cond to memory and reload it from there.
1515// unsigned Tmp4 = MakeReg(MVT::f64);
1516// MoveIntFP(Tmp1, Tmp4, true);
1517// //now ideally, we don't have to do anything to the flag...
1518// // Get the condition into the zero flag.
1519// BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
1520 return Result;
Jeff Cohen00b168892005-07-27 06:12:32 +00001521 }
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001522 } else {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001523 //FIXME: look at parent to decide if intCC can be folded, or if setCC(FP)
1524 //and can save stack use
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001525 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001526 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1527 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001528 // Get the condition into the zero flag.
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001529 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001530
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001531 SDOperand CC = N.getOperand(0);
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001532
Misha Brukman4633f1c2005-04-21 23:13:11 +00001533 if (CC.getOpcode() == ISD::SETCC &&
Chris Lattner88ac32c2005-08-09 20:21:10 +00001534 !MVT::isInteger(CC.getOperand(0).getValueType()))
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001535 { //FP Setcc -> Int Select
Misha Brukman7847fca2005-04-22 17:54:37 +00001536 Tmp1 = MakeReg(MVT::f64);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001537 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1538 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Misha Brukman7847fca2005-04-22 17:54:37 +00001539 bool inv = SelectFPSetCC(CC, Tmp1);
1540 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
1541 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
1542 return Result;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001543 }
1544 if (CC.getOpcode() == ISD::SETCC) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001545 //Int SetCC -> Select
1546 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharthd2284272005-08-15 14:31:37 +00001547 if(isSIntImmediateBounded(CC.getOperand(1), SImm, 0, 0)) {
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001548 //figure out a few things
Andrew Lenharthd2284272005-08-15 14:31:37 +00001549 bool useImm = isSIntImmediateBounded(N.getOperand(2), SImm, 0, 255);
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001550
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001551 //Fix up CC
Chris Lattner88ac32c2005-08-09 20:21:10 +00001552 ISD::CondCode cCode= cast<CondCodeSDNode>(CC.getOperand(2))->get();
Andrew Lenharth694c2982005-06-26 23:01:11 +00001553 if (useImm) //Invert sense to get Imm field right
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001554 cCode = ISD::getSetCCInverse(cCode, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001555
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001556 //Choose the CMOV
1557 switch (cCode) {
1558 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001559 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
1560 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
1561 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
1562 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
1563 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
1564 case ISD::SETULT: assert(0 && "unsigned < 0 is never true"); break;
1565 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
1566 //Technically you could have this CC
1567 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
1568 case ISD::SETUGE: assert(0 && "unsgined >= 0 is always true"); break;
1569 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001570 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001571 Tmp1 = SelectExpr(CC.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001572
Andrew Lenharth694c2982005-06-26 23:01:11 +00001573 if (useImm) {
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001574 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
Andrew Lenharthd2284272005-08-15 14:31:37 +00001575 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addImm(SImm).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001576 } else {
1577 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1578 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1579 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
1580 }
1581 return Result;
1582 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001583 //Otherwise, fall though
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001584 }
1585 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001586 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1587 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001588 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3)
1589 .addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001590
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001591 return Result;
1592 }
1593
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001594 case ISD::Constant:
1595 {
Andrew Lenharthc0513832005-03-29 19:24:04 +00001596 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001597 int zero_extend_top = 0;
Andrew Lenharthf075cac2005-07-23 07:46:48 +00001598 if (val > 0 && (val & 0xFFFFFFFF00000000ULL) == 0 &&
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001599 ((int32_t)val < 0)) {
1600 //try a small load and zero extend
1601 val = (int32_t)val;
1602 zero_extend_top = 15;
1603 }
1604
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00001605 if (val <= IMM_HIGH && val >= IMM_LOW) {
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001606 if(!zero_extend_top)
1607 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
1608 else {
1609 Tmp1 = MakeReg(MVT::i64);
1610 BuildMI(BB, Alpha::LDA, 2, Tmp1).addImm(val).addReg(Alpha::R31);
1611 BuildMI(BB, Alpha::ZAPNOT, 2, Result).addReg(Tmp1).addImm(zero_extend_top);
1612 }
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00001613 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001614 else if (val <= (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT &&
1615 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
1616 Tmp1 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001617 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val))
1618 .addReg(Alpha::R31);
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001619 if (!zero_extend_top)
1620 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
1621 else {
1622 Tmp3 = MakeReg(MVT::i64);
1623 BuildMI(BB, Alpha::LDA, 2, Tmp3).addImm(getLower16(val)).addReg(Tmp1);
1624 BuildMI(BB, Alpha::ZAPNOT, 2, Result).addReg(Tmp3).addImm(zero_extend_top);
1625 }
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00001626 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001627 else {
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001628 //re-get the val since we are going to mem anyway
1629 val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001630 MachineConstantPool *CP = BB->getParent()->getConstantPool();
Jeff Cohen00b168892005-07-27 06:12:32 +00001631 ConstantUInt *C =
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001632 ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001633 unsigned CPI = CP->getConstantPoolIndex(C);
1634 AlphaLowering.restoreGP(BB);
Andrew Lenharthfe895e32005-06-27 17:15:36 +00001635 has_sym = true;
1636 Tmp1 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001637 BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CPI)
1638 .addReg(Alpha::R29);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001639 if (EnableAlphaLSMark)
1640 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
1641 .addImm(getUID());
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001642 BuildMI(BB, Alpha::LDQr, 2, Result).addConstantPoolIndex(CPI)
1643 .addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001644 }
1645 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001646 }
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001647 case ISD::FNEG:
1648 if(ISD::FABS == N.getOperand(0).getOpcode())
1649 {
1650 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1651 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
1652 } else {
1653 Tmp1 = SelectExpr(N.getOperand(0));
1654 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp1).addReg(Tmp1);
1655 }
1656 return Result;
1657
1658 case ISD::FABS:
1659 Tmp1 = SelectExpr(N.getOperand(0));
1660 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
1661 return Result;
1662
1663 case ISD::FP_ROUND:
1664 assert (DestType == MVT::f32 &&
1665 N.getOperand(0).getValueType() == MVT::f64 &&
1666 "only f64 to f32 conversion supported here");
1667 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth98169be2005-07-28 18:14:47 +00001668 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001669 return Result;
1670
1671 case ISD::FP_EXTEND:
1672 assert (DestType == MVT::f64 &&
1673 N.getOperand(0).getValueType() == MVT::f32 &&
1674 "only f32 to f64 conversion supported here");
1675 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth98169be2005-07-28 18:14:47 +00001676 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001677 return Result;
1678
1679 case ISD::ConstantFP:
1680 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
1681 if (CN->isExactlyValue(+0.0)) {
1682 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31)
1683 .addReg(Alpha::F31);
1684 } else if ( CN->isExactlyValue(-0.0)) {
1685 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31)
1686 .addReg(Alpha::F31);
1687 } else {
1688 abort();
1689 }
1690 }
1691 return Result;
1692
1693 case ISD::SINT_TO_FP:
1694 {
1695 assert (N.getOperand(0).getValueType() == MVT::i64
1696 && "only quads can be loaded from");
1697 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1698 Tmp2 = MakeReg(MVT::f64);
1699 MoveInt2FP(Tmp1, Tmp2, true);
1700 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
Andrew Lenharth98169be2005-07-28 18:14:47 +00001701 BuildMI(BB, Opc, 1, Result).addReg(Alpha::F31).addReg(Tmp2);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001702 return Result;
1703 }
Andrew Lenharthf71df332005-09-04 06:12:19 +00001704
1705 case ISD::AssertSext:
1706 case ISD::AssertZext:
1707 return SelectExpr(N.getOperand(0));
1708
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001709 }
1710
1711 return 0;
1712}
1713
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001714void AlphaISel::Select(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001715 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00001716 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001717
Nate Begeman85fdeb22005-03-24 04:39:54 +00001718 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001719 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001720
1721 SDNode *Node = N.Val;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001722
Andrew Lenharth760270d2005-02-07 23:02:23 +00001723 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001724
1725 default:
1726 Node->dump(); std::cerr << "\n";
1727 assert(0 && "Node not handled yet!");
1728
1729 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00001730 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001731 return;
1732 }
1733
1734 case ISD::BR: {
1735 MachineBasicBlock *Dest =
1736 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1737
1738 Select(N.getOperand(0));
1739 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
1740 return;
1741 }
1742
1743 case ISD::ImplicitDef:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001744 ++count_ins;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001745 Select(N.getOperand(0));
Chris Lattner707ebc52005-08-16 21:56:37 +00001746 BuildMI(BB, Alpha::IDEF, 0,
1747 cast<RegisterSDNode>(N.getOperand(1))->getReg());
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001748 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001749
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001750 case ISD::EntryToken: return; // Noop
1751
1752 case ISD::TokenFactor:
1753 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1754 Select(Node->getOperand(i));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001755
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001756 //N.Val->dump(); std::cerr << "\n";
1757 //assert(0 && "Node not handled yet!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001758
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001759 return;
1760
1761 case ISD::CopyToReg:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001762 ++count_outs;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001763 Select(N.getOperand(0));
Chris Lattner707ebc52005-08-16 21:56:37 +00001764 Tmp1 = SelectExpr(N.getOperand(2));
1765 Tmp2 = cast<RegisterSDNode>(N.getOperand(1))->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001766
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001767 if (Tmp1 != Tmp2) {
Chris Lattner707ebc52005-08-16 21:56:37 +00001768 if (N.getOperand(2).getValueType() == MVT::f64 ||
1769 N.getOperand(2).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00001770 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1771 else
1772 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001773 }
1774 return;
1775
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001776 case ISD::RET:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001777 ++count_outs;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001778 switch (N.getNumOperands()) {
1779 default:
1780 std::cerr << N.getNumOperands() << "\n";
1781 for (unsigned i = 0; i < N.getNumOperands(); ++i)
1782 std::cerr << N.getOperand(i).getValueType() << "\n";
1783 Node->dump();
1784 assert(0 && "Unknown return instruction!");
1785 case 2:
1786 Select(N.getOperand(0));
1787 Tmp1 = SelectExpr(N.getOperand(1));
1788 switch (N.getOperand(1).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001789 default: Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001790 assert(0 && "All other types should have been promoted!!");
1791 case MVT::f64:
1792 case MVT::f32:
1793 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
1794 break;
1795 case MVT::i32:
1796 case MVT::i64:
1797 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
1798 break;
1799 }
1800 break;
1801 case 1:
1802 Select(N.getOperand(0));
1803 break;
1804 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001805 // Just emit a 'ret' instruction
Andrew Lenharth6968bff2005-06-27 23:24:11 +00001806 AlphaLowering.restoreRA(BB);
Andrew Lenharthf3f951a2005-07-22 20:50:29 +00001807 BuildMI(BB, Alpha::RET, 2, Alpha::R31).addReg(Alpha::R26).addImm(1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001808 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001809
Misha Brukman4633f1c2005-04-21 23:13:11 +00001810 case ISD::TRUNCSTORE:
1811 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001812 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001813 SDOperand Chain = N.getOperand(0);
1814 SDOperand Value = N.getOperand(1);
1815 SDOperand Address = N.getOperand(2);
1816 Select(Chain);
1817
1818 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00001819
1820 if (opcode == ISD::STORE) {
1821 switch(Value.getValueType()) {
1822 default: assert(0 && "unknown Type in store");
1823 case MVT::i64: Opc = Alpha::STQ; break;
1824 case MVT::f64: Opc = Alpha::STT; break;
1825 case MVT::f32: Opc = Alpha::STS; break;
1826 }
1827 } else { //ISD::TRUNCSTORE
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001828 switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) {
Andrew Lenharth760270d2005-02-07 23:02:23 +00001829 default: assert(0 && "unknown Type in store");
1830 case MVT::i1: //FIXME: DAG does not promote this load
1831 case MVT::i8: Opc = Alpha::STB; break;
1832 case MVT::i16: Opc = Alpha::STW; break;
1833 case MVT::i32: Opc = Alpha::STL; break;
1834 }
Andrew Lenharth65838902005-02-06 16:22:15 +00001835 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00001836
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001837 int i, j, k;
Jeff Cohen00b168892005-07-27 06:12:32 +00001838 if (EnableAlphaLSMark)
1839 getValueInfo(cast<SrcValueSDNode>(N.getOperand(3))->getValue(),
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001840 i, j, k);
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001841
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001842 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Address);
1843 if (GASD && !GASD->getGlobal()->isExternal()) {
1844 Tmp2 = MakeReg(MVT::i64);
1845 AlphaLowering.restoreGP(BB);
1846 BuildMI(BB, Alpha::LDAHr, 2, Tmp2)
1847 .addGlobalAddress(GASD->getGlobal()).addReg(Alpha::R29);
1848 if (EnableAlphaLSMark)
1849 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1850 .addImm(getUID());
1851 BuildMI(BB, GetRelVersion(Opc), 3).addReg(Tmp1)
1852 .addGlobalAddress(GASD->getGlobal()).addReg(Tmp2);
Andrew Lenharthfce587e2005-06-29 00:39:17 +00001853 } else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001854 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001855 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1856 .addImm(getUID());
Andrew Lenharth032f2352005-02-22 21:59:48 +00001857 BuildMI(BB, Opc, 3).addReg(Tmp1)
1858 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1859 .addReg(Alpha::F31);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001860 } else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001861 long offset;
1862 SelectAddr(Address, Tmp2, offset);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001863 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001864 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1865 .addImm(getUID());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001866 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1867 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001868 return;
1869 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001870
1871 case ISD::EXTLOAD:
1872 case ISD::SEXTLOAD:
1873 case ISD::ZEXTLOAD:
1874 case ISD::LOAD:
1875 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001876 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001877 case ISD::CALL:
Andrew Lenharth032f2352005-02-22 21:59:48 +00001878 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001879 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001880 SelectExpr(N);
1881 return;
1882
Chris Lattner16cd04d2005-05-12 23:24:06 +00001883 case ISD::CALLSEQ_START:
1884 case ISD::CALLSEQ_END:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001885 Select(N.getOperand(0));
1886 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001887
Chris Lattner16cd04d2005-05-12 23:24:06 +00001888 Opc = N.getOpcode() == ISD::CALLSEQ_START ? Alpha::ADJUSTSTACKDOWN :
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001889 Alpha::ADJUSTSTACKUP;
1890 BuildMI(BB, Opc, 1).addImm(Tmp1);
1891 return;
Andrew Lenharth95762122005-03-31 21:24:06 +00001892
1893 case ISD::PCMARKER:
1894 Select(N.getOperand(0)); //Chain
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001895 BuildMI(BB, Alpha::PCLABEL, 2)
1896 .addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
Andrew Lenharth95762122005-03-31 21:24:06 +00001897 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001898 }
1899 assert(0 && "Should not be reached!");
1900}
1901
1902
1903/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
1904/// into a machine code representation using pattern matching and a machine
1905/// description file.
1906///
1907FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001908 return new AlphaISel(TM);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001909}
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001910