blob: 557da82d1d66f1ad92d943b65a22faedbcfdf7a4 [file] [log] [blame]
Misha Brukman2a8350a2005-02-05 02:24:26 +00001//===- AlphaISelPattern.cpp - A pattern matching inst selector for Alpha --===//
Misha Brukman4633f1c2005-04-21 23:13:11 +00002//
Andrew Lenharth304d0f32005-01-22 23:41:55 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman4633f1c2005-04-21 23:13:11 +00007//
Andrew Lenharth304d0f32005-01-22 23:41:55 +00008//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for Alpha.
11//
12//===----------------------------------------------------------------------===//
13
14#include "Alpha.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000015#include "AlphaRegisterInfo.h"
Andrew Lenharth120ab482005-09-29 22:54:56 +000016#include "AlphaTargetMachine.h"
Andrew Lenharthaa38ce42005-09-02 18:46:02 +000017#include "AlphaISelLowering.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000018#include "llvm/Constants.h" // FIXME: REMOVE
19#include "llvm/Function.h"
Andrew Lenharthb69f3422005-06-22 17:19:45 +000020#include "llvm/Module.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000021#include "llvm/CodeGen/MachineInstrBuilder.h"
22#include "llvm/CodeGen/MachineConstantPool.h" // FIXME: REMOVE
23#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/SelectionDAG.h"
26#include "llvm/CodeGen/SelectionDAGISel.h"
27#include "llvm/CodeGen/SSARegMap.h"
28#include "llvm/Target/TargetData.h"
29#include "llvm/Target/TargetLowering.h"
30#include "llvm/Support/MathExtras.h"
31#include "llvm/ADT/Statistic.h"
Andrew Lenharth032f2352005-02-22 21:59:48 +000032#include "llvm/Support/Debug.h"
Andrew Lenharth95762122005-03-31 21:24:06 +000033#include "llvm/Support/CommandLine.h"
Andrew Lenharth304d0f32005-01-22 23:41:55 +000034#include <set>
Andrew Lenharth684f2292005-01-30 00:35:27 +000035#include <algorithm>
Andrew Lenharth304d0f32005-01-22 23:41:55 +000036using namespace llvm;
37
Andrew Lenharth95762122005-03-31 21:24:06 +000038namespace llvm {
Misha Brukman4633f1c2005-04-21 23:13:11 +000039 cl::opt<bool> EnableAlphaIDIV("enable-alpha-intfpdiv",
Andrew Lenharthd4653b12005-06-27 17:39:17 +000040 cl::desc("Use the FP div instruction for integer div when possible"),
Andrew Lenharth95762122005-03-31 21:24:06 +000041 cl::Hidden);
Misha Brukman4633f1c2005-04-21 23:13:11 +000042 cl::opt<bool> EnableAlphaCount("enable-alpha-count",
Andrew Lenharthd4653b12005-06-27 17:39:17 +000043 cl::desc("Print estimates on live ins and outs"),
44 cl::Hidden);
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +000045 cl::opt<bool> EnableAlphaLSMark("enable-alpha-lsmark",
Andrew Lenharthd4653b12005-06-27 17:39:17 +000046 cl::desc("Emit symbols to correlate Mem ops to LLVM Values"),
47 cl::Hidden);
Andrew Lenharth95762122005-03-31 21:24:06 +000048}
49
Andrew Lenharthe3c8c0a42005-05-31 19:49:34 +000050namespace {
Andrew Lenharth304d0f32005-01-22 23:41:55 +000051
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000052//===--------------------------------------------------------------------===//
53/// ISel - Alpha specific code to select Alpha machine instructions for
54/// SelectionDAG operations.
55//===--------------------------------------------------------------------===//
Andrew Lenharthb69f3422005-06-22 17:19:45 +000056class AlphaISel : public SelectionDAGISel {
Misha Brukman4633f1c2005-04-21 23:13:11 +000057
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000058 /// AlphaLowering - This object fully describes how to lower LLVM code to an
59 /// Alpha-specific SelectionDAG.
60 AlphaTargetLowering AlphaLowering;
Misha Brukman4633f1c2005-04-21 23:13:11 +000061
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000062 SelectionDAG *ISelDAG; // Hack to support us having a dag->dag transform
63 // for sdiv and udiv until it is put into the future
64 // dag combiner.
65
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000066 /// ExprMap - As shared expressions are codegen'd, we keep track of which
67 /// vreg the value is produced in, so we only emit one copy of each compiled
68 /// tree.
69 static const unsigned notIn = (unsigned)(-1);
70 std::map<SDOperand, unsigned> ExprMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +000071
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000072 //CCInvMap sometimes (SetNE) we have the inverse CC code for free
73 std::map<SDOperand, unsigned> CCInvMap;
Misha Brukman4633f1c2005-04-21 23:13:11 +000074
Andrew Lenhartha32b9e32005-04-08 17:28:49 +000075 int count_ins;
76 int count_outs;
77 bool has_sym;
Andrew Lenharth500b4db2005-04-22 13:35:18 +000078 int max_depth;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +000079
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000080public:
Jeff Cohen00b168892005-07-27 06:12:32 +000081 AlphaISel(TargetMachine &TM) : SelectionDAGISel(AlphaLowering),
Andrew Lenharthd4653b12005-06-27 17:39:17 +000082 AlphaLowering(TM)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000083 {}
Misha Brukman4633f1c2005-04-21 23:13:11 +000084
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000085 /// InstructionSelectBasicBlock - This callback is invoked by
86 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
87 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG) {
Andrew Lenharth032f2352005-02-22 21:59:48 +000088 DEBUG(BB->dump());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +000089 count_ins = 0;
90 count_outs = 0;
Andrew Lenharth500b4db2005-04-22 13:35:18 +000091 max_depth = 0;
Andrew Lenhartha32b9e32005-04-08 17:28:49 +000092 has_sym = false;
93
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000094 // Codegen the basic block.
Andrew Lenharth4b8ac152005-04-06 20:25:34 +000095 ISelDAG = &DAG;
Andrew Lenharth500b4db2005-04-22 13:35:18 +000096 max_depth = DAG.getRoot().getNodeDepth();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +000097 Select(DAG.getRoot());
Andrew Lenhartha32b9e32005-04-08 17:28:49 +000098
99 if(has_sym)
100 ++count_ins;
101 if(EnableAlphaCount)
Jeff Cohen00b168892005-07-27 06:12:32 +0000102 std::cerr << "COUNT: "
103 << BB->getParent()->getFunction ()->getName() << " "
104 << BB->getNumber() << " "
Andrew Lenharth500b4db2005-04-22 13:35:18 +0000105 << max_depth << " "
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000106 << count_ins << " "
107 << count_outs << "\n";
Misha Brukman4633f1c2005-04-21 23:13:11 +0000108
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000109 // Clear state used for selection.
110 ExprMap.clear();
111 CCInvMap.clear();
112 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000113
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000114 unsigned SelectExpr(SDOperand N);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000115 void Select(SDOperand N);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000116
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000117 void SelectAddr(SDOperand N, unsigned& Reg, long& offset);
118 void SelectBranchCC(SDOperand N);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000119 void MoveFP2Int(unsigned src, unsigned dst, bool isDouble);
120 void MoveInt2FP(unsigned src, unsigned dst, bool isDouble);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000121 //returns whether the sense of the comparison was inverted
122 bool SelectFPSetCC(SDOperand N, unsigned dst);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000123
124 // dag -> dag expanders for integer divide by constant
125 SDOperand BuildSDIVSequence(SDOperand N);
126 SDOperand BuildUDIVSequence(SDOperand N);
127
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000128};
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000129}
130
Andrew Lenharthd2284272005-08-15 14:31:37 +0000131static bool isSIntImmediate(SDOperand N, int64_t& Imm) {
132 // test for constant
133 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
134 // retrieve value
135 Imm = CN->getSignExtended();
136 // passes muster
137 return true;
138 }
139 // not a constant
140 return false;
141}
142
143// isSIntImmediateBounded - This method tests to see if a constant operand
144// bounded s.t. low <= Imm <= high
145// If so Imm will receive the 64 bit value.
146static bool isSIntImmediateBounded(SDOperand N, int64_t& Imm,
147 int64_t low, int64_t high) {
Andrew Lenharth035b8ab2005-08-17 00:47:24 +0000148 if (isSIntImmediate(N, Imm) && Imm <= high && Imm >= low)
Andrew Lenharthd2284272005-08-15 14:31:37 +0000149 return true;
150 return false;
151}
152static bool isUIntImmediate(SDOperand N, uint64_t& Imm) {
153 // test for constant
154 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
155 // retrieve value
156 Imm = (uint64_t)CN->getValue();
157 // passes muster
158 return true;
159 }
160 // not a constant
161 return false;
162}
163
164static bool isUIntImmediateBounded(SDOperand N, uint64_t& Imm,
165 uint64_t low, uint64_t high) {
Andrew Lenharth035b8ab2005-08-17 00:47:24 +0000166 if (isUIntImmediate(N, Imm) && Imm <= high && Imm >= low)
Andrew Lenharthd2284272005-08-15 14:31:37 +0000167 return true;
168 return false;
169}
170
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000171static void getValueInfo(const Value* v, int& type, int& fun, int& offset)
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000172{
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000173 fun = type = offset = 0;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000174 if (v == NULL) {
175 type = 0;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000176 } else if (const GlobalValue* GV = dyn_cast<GlobalValue>(v)) {
177 type = 1;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000178 const Module* M = GV->getParent();
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000179 for(Module::const_global_iterator ii = M->global_begin(); &*ii != GV; ++ii)
180 ++offset;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000181 } else if (const Argument* Arg = dyn_cast<Argument>(v)) {
182 type = 2;
183 const Function* F = Arg->getParent();
184 const Module* M = F->getParent();
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000185 for(Module::const_iterator ii = M->begin(); &*ii != F; ++ii)
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000186 ++fun;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000187 for(Function::const_arg_iterator ii = F->arg_begin(); &*ii != Arg; ++ii)
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000188 ++offset;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000189 } else if (const Instruction* I = dyn_cast<Instruction>(v)) {
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +0000190 assert(dyn_cast<PointerType>(I->getType()));
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000191 type = 3;
192 const BasicBlock* bb = I->getParent();
193 const Function* F = bb->getParent();
194 const Module* M = F->getParent();
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000195 for(Module::const_iterator ii = M->begin(); &*ii != F; ++ii)
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000196 ++fun;
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000197 for(Function::const_iterator ii = F->begin(); &*ii != bb; ++ii)
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000198 offset += ii->size();
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000199 for(BasicBlock::const_iterator ii = bb->begin(); &*ii != I; ++ii)
Andrew Lenharthfec0e402005-07-12 04:20:52 +0000200 ++offset;
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +0000201 } else if (const Constant* C = dyn_cast<Constant>(v)) {
202 //Don't know how to look these up yet
203 type = 0;
Andrew Lenhartha48f3ce2005-07-07 19:52:58 +0000204 } else {
205 assert(0 && "Error in value marking");
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000206 }
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000207 //type = 4: register spilling
208 //type = 5: global address loading or constant loading
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000209}
210
211static int getUID()
212{
213 static int id = 0;
214 return ++id;
215}
Andrew Lenharthcd7f8cf2005-06-06 19:03:55 +0000216
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000217//Factorize a number using the list of constants
218static bool factorize(int v[], int res[], int size, uint64_t c)
219{
220 bool cont = true;
221 while (c != 1 && cont)
222 {
223 cont = false;
224 for(int i = 0; i < size; ++i)
225 {
226 if (c % v[i] == 0)
227 {
228 c /= v[i];
229 ++res[i];
230 cont=true;
231 }
232 }
233 }
234 return c == 1;
235}
236
237
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000238//Shamelessly adapted from PPC32
Misha Brukman4633f1c2005-04-21 23:13:11 +0000239// Structure used to return the necessary information to codegen an SDIV as
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000240// a multiply.
241struct ms {
242 int64_t m; // magic number
243 int64_t s; // shift amount
244};
245
246struct mu {
247 uint64_t m; // magic number
248 int64_t a; // add indicator
249 int64_t s; // shift amount
250};
251
252/// magic - calculate the magic numbers required to codegen an integer sdiv as
Misha Brukman4633f1c2005-04-21 23:13:11 +0000253/// a sequence of multiply and shifts. Requires that the divisor not be 0, 1,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000254/// or -1.
255static struct ms magic(int64_t d) {
256 int64_t p;
257 uint64_t ad, anc, delta, q1, r1, q2, r2, t;
258 const uint64_t two63 = 9223372036854775808ULL; // 2^63
259 struct ms mag;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000260
Andrew Lenharth01c8f6e2005-08-01 17:47:28 +0000261 ad = llabs(d);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000262 t = two63 + ((uint64_t)d >> 63);
263 anc = t - 1 - t%ad; // absolute value of nc
Andrew Lenharth320174f2005-04-07 17:19:16 +0000264 p = 63; // initialize p
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000265 q1 = two63/anc; // initialize q1 = 2p/abs(nc)
266 r1 = two63 - q1*anc; // initialize r1 = rem(2p,abs(nc))
267 q2 = two63/ad; // initialize q2 = 2p/abs(d)
268 r2 = two63 - q2*ad; // initialize r2 = rem(2p,abs(d))
269 do {
270 p = p + 1;
271 q1 = 2*q1; // update q1 = 2p/abs(nc)
272 r1 = 2*r1; // update r1 = rem(2p/abs(nc))
273 if (r1 >= anc) { // must be unsigned comparison
274 q1 = q1 + 1;
275 r1 = r1 - anc;
276 }
277 q2 = 2*q2; // update q2 = 2p/abs(d)
278 r2 = 2*r2; // update r2 = rem(2p/abs(d))
279 if (r2 >= ad) { // must be unsigned comparison
280 q2 = q2 + 1;
281 r2 = r2 - ad;
282 }
283 delta = ad - r2;
284 } while (q1 < delta || (q1 == delta && r1 == 0));
285
286 mag.m = q2 + 1;
287 if (d < 0) mag.m = -mag.m; // resulting magic number
288 mag.s = p - 64; // resulting shift
289 return mag;
290}
291
292/// magicu - calculate the magic numbers required to codegen an integer udiv as
293/// a sequence of multiply, add and shifts. Requires that the divisor not be 0.
294static struct mu magicu(uint64_t d)
295{
296 int64_t p;
297 uint64_t nc, delta, q1, r1, q2, r2;
298 struct mu magu;
299 magu.a = 0; // initialize "add" indicator
300 nc = - 1 - (-d)%d;
Andrew Lenharth320174f2005-04-07 17:19:16 +0000301 p = 63; // initialize p
302 q1 = 0x8000000000000000ull/nc; // initialize q1 = 2p/nc
303 r1 = 0x8000000000000000ull - q1*nc; // initialize r1 = rem(2p,nc)
304 q2 = 0x7FFFFFFFFFFFFFFFull/d; // initialize q2 = (2p-1)/d
305 r2 = 0x7FFFFFFFFFFFFFFFull - q2*d; // initialize r2 = rem((2p-1),d)
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000306 do {
307 p = p + 1;
308 if (r1 >= nc - r1 ) {
309 q1 = 2*q1 + 1; // update q1
310 r1 = 2*r1 - nc; // update r1
311 }
312 else {
313 q1 = 2*q1; // update q1
314 r1 = 2*r1; // update r1
315 }
316 if (r2 + 1 >= d - r2) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000317 if (q2 >= 0x7FFFFFFFFFFFFFFFull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000318 q2 = 2*q2 + 1; // update q2
319 r2 = 2*r2 + 1 - d; // update r2
320 }
321 else {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000322 if (q2 >= 0x8000000000000000ull) magu.a = 1;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000323 q2 = 2*q2; // update q2
324 r2 = 2*r2 + 1; // update r2
325 }
326 delta = d - 1 - r2;
327 } while (p < 64 && (q1 < delta || (q1 == delta && r1 == 0)));
328 magu.m = q2 + 1; // resulting magic number
Andrew Lenharth320174f2005-04-07 17:19:16 +0000329 magu.s = p - 64; // resulting shift
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000330 return magu;
331}
332
333/// BuildSDIVSequence - Given an ISD::SDIV node expressing a divide by constant,
334/// return a DAG expression to select that will generate the same value by
335/// multiplying by a magic number. See:
336/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000337SDOperand AlphaISel::BuildSDIVSequence(SDOperand N) {
Andrew Lenharth320174f2005-04-07 17:19:16 +0000338 int64_t d = (int64_t)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000339 ms magics = magic(d);
340 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000341 SDOperand Q = ISelDAG->getNode(ISD::MULHS, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000342 ISelDAG->getConstant(magics.m, MVT::i64));
343 // If d > 0 and m < 0, add the numerator
344 if (d > 0 && magics.m < 0)
345 Q = ISelDAG->getNode(ISD::ADD, MVT::i64, Q, N.getOperand(0));
346 // If d < 0 and m > 0, subtract the numerator.
347 if (d < 0 && magics.m > 0)
348 Q = ISelDAG->getNode(ISD::SUB, MVT::i64, Q, N.getOperand(0));
349 // Shift right algebraic if shift value is nonzero
350 if (magics.s > 0)
Misha Brukman4633f1c2005-04-21 23:13:11 +0000351 Q = ISelDAG->getNode(ISD::SRA, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000352 ISelDAG->getConstant(magics.s, MVT::i64));
353 // Extract the sign bit and add it to the quotient
Misha Brukman4633f1c2005-04-21 23:13:11 +0000354 SDOperand T =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000355 ISelDAG->getNode(ISD::SRL, MVT::i64, Q, ISelDAG->getConstant(63, MVT::i64));
356 return ISelDAG->getNode(ISD::ADD, MVT::i64, Q, T);
357}
358
359/// BuildUDIVSequence - Given an ISD::UDIV node expressing a divide by constant,
360/// return a DAG expression to select that will generate the same value by
361/// multiplying by a magic number. See:
362/// <http://the.wall.riscom.net/books/proc/ppc/cwg/code2.html>
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000363SDOperand AlphaISel::BuildUDIVSequence(SDOperand N) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000364 unsigned d =
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000365 (unsigned)cast<ConstantSDNode>(N.getOperand(1))->getSignExtended();
366 mu magics = magicu(d);
367 // Multiply the numerator (operand 0) by the magic value
Misha Brukman4633f1c2005-04-21 23:13:11 +0000368 SDOperand Q = ISelDAG->getNode(ISD::MULHU, MVT::i64, N.getOperand(0),
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000369 ISelDAG->getConstant(magics.m, MVT::i64));
370 if (magics.a == 0) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000371 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, Q,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000372 ISelDAG->getConstant(magics.s, MVT::i64));
373 } else {
374 SDOperand NPQ = ISelDAG->getNode(ISD::SUB, MVT::i64, N.getOperand(0), Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000375 NPQ = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000376 ISelDAG->getConstant(1, MVT::i64));
377 NPQ = ISelDAG->getNode(ISD::ADD, MVT::i64, NPQ, Q);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000378 Q = ISelDAG->getNode(ISD::SRL, MVT::i64, NPQ,
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000379 ISelDAG->getConstant(magics.s-1, MVT::i64));
380 }
381 return Q;
382}
383
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000384//These describe LDAx
Andrew Lenharthc0513832005-03-29 19:24:04 +0000385static const int IMM_LOW = -32768;
386static const int IMM_HIGH = 32767;
Andrew Lenharthe87f6c32005-03-11 17:48:05 +0000387static const int IMM_MULT = 65536;
388
389static long getUpper16(long l)
390{
391 long y = l / IMM_MULT;
392 if (l % IMM_MULT > IMM_HIGH)
393 ++y;
394 return y;
395}
396
397static long getLower16(long l)
398{
399 long h = getUpper16(l);
400 return l - h * IMM_MULT;
401}
402
Andrew Lenharthfe895e32005-06-27 17:15:36 +0000403static unsigned GetRelVersion(unsigned opcode)
404{
405 switch (opcode) {
406 default: assert(0 && "unknown load or store"); return 0;
407 case Alpha::LDQ: return Alpha::LDQr;
408 case Alpha::LDS: return Alpha::LDSr;
409 case Alpha::LDT: return Alpha::LDTr;
410 case Alpha::LDL: return Alpha::LDLr;
411 case Alpha::LDBU: return Alpha::LDBUr;
412 case Alpha::LDWU: return Alpha::LDWUr;
Andrew Lenharthfce587e2005-06-29 00:39:17 +0000413 case Alpha::STB: return Alpha::STBr;
414 case Alpha::STW: return Alpha::STWr;
415 case Alpha::STL: return Alpha::STLr;
416 case Alpha::STQ: return Alpha::STQr;
417 case Alpha::STS: return Alpha::STSr;
418 case Alpha::STT: return Alpha::STTr;
419
Andrew Lenharthfe895e32005-06-27 17:15:36 +0000420 }
421}
Andrew Lenharth65838902005-02-06 16:22:15 +0000422
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000423void AlphaISel::MoveFP2Int(unsigned src, unsigned dst, bool isDouble)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000424{
425 unsigned Opc;
Andrew Lenharth120ab482005-09-29 22:54:56 +0000426 if (TLI.getTargetMachine().getSubtarget<AlphaSubtarget>().hasF2I()) {
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000427 Opc = isDouble ? Alpha::FTOIT : Alpha::FTOIS;
Andrew Lenharth98169be2005-07-28 18:14:47 +0000428 BuildMI(BB, Opc, 1, dst).addReg(src).addReg(Alpha::F31);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000429 } else {
430 //The hard way:
431 // Spill the integer to memory and reload it from there.
432 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
433 MachineFunction *F = BB->getParent();
434 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
435
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000436 if (EnableAlphaLSMark)
437 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
438 .addImm(getUID());
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000439 Opc = isDouble ? Alpha::STT : Alpha::STS;
440 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000441
442 if (EnableAlphaLSMark)
443 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
444 .addImm(getUID());
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000445 Opc = isDouble ? Alpha::LDQ : Alpha::LDL;
446 BuildMI(BB, Alpha::LDQ, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
447 }
448}
449
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000450void AlphaISel::MoveInt2FP(unsigned src, unsigned dst, bool isDouble)
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000451{
452 unsigned Opc;
Andrew Lenharth120ab482005-09-29 22:54:56 +0000453 if (TLI.getTargetMachine().getSubtarget<AlphaSubtarget>().hasF2I()) {
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000454 Opc = isDouble?Alpha::ITOFT:Alpha::ITOFS;
Andrew Lenharth98169be2005-07-28 18:14:47 +0000455 BuildMI(BB, Opc, 1, dst).addReg(src).addReg(Alpha::R31);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000456 } else {
457 //The hard way:
458 // Spill the integer to memory and reload it from there.
459 unsigned Size = MVT::getSizeInBits(MVT::f64)/8;
460 MachineFunction *F = BB->getParent();
461 int FrameIdx = F->getFrameInfo()->CreateStackObject(Size, 8);
462
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000463 if (EnableAlphaLSMark)
464 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
465 .addImm(getUID());
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000466 Opc = isDouble ? Alpha::STQ : Alpha::STL;
467 BuildMI(BB, Opc, 3).addReg(src).addFrameIndex(FrameIdx).addReg(Alpha::F31);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000468
469 if (EnableAlphaLSMark)
470 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(4).addImm(0).addImm(0)
471 .addImm(getUID());
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +0000472 Opc = isDouble ? Alpha::LDT : Alpha::LDS;
473 BuildMI(BB, Opc, 2, dst).addFrameIndex(FrameIdx).addReg(Alpha::F31);
474 }
475}
476
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000477bool AlphaISel::SelectFPSetCC(SDOperand N, unsigned dst)
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000478{
Chris Lattner88ac32c2005-08-09 20:21:10 +0000479 SDNode *SetCC = N.Val;
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000480 unsigned Opc, Tmp1, Tmp2, Tmp3;
Chris Lattner88ac32c2005-08-09 20:21:10 +0000481 ISD::CondCode CC = cast<CondCodeSDNode>(SetCC->getOperand(2))->get();
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000482 bool rev = false;
483 bool inv = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000484
Chris Lattner88ac32c2005-08-09 20:21:10 +0000485 switch (CC) {
486 default: SetCC->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000487 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
488 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
489 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
490 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
491 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
492 case ISD::SETNE: Opc = Alpha::CMPTEQ; inv = true; break;
493 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000494
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000495 ConstantFPSDNode *CN;
496 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(0)))
497 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
498 Tmp1 = Alpha::F31;
499 else
500 Tmp1 = SelectExpr(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000501
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000502 if ((CN = dyn_cast<ConstantFPSDNode>(SetCC->getOperand(1)))
503 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
504 Tmp2 = Alpha::F31;
505 else
Chris Lattner9c9183a2005-04-30 04:44:07 +0000506 Tmp2 = SelectExpr(N.getOperand(1));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000507
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000508 //Can only compare doubles, and dag won't promote for me
509 if (SetCC->getOperand(0).getValueType() == MVT::f32)
510 {
511 //assert(0 && "Setcc On float?\n");
512 std::cerr << "Setcc on float!\n";
513 Tmp3 = MakeReg(MVT::f64);
Andrew Lenharth98169be2005-07-28 18:14:47 +0000514 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000515 Tmp1 = Tmp3;
516 }
517 if (SetCC->getOperand(1).getValueType() == MVT::f32)
518 {
519 //assert (0 && "Setcc On float?\n");
520 std::cerr << "Setcc on float!\n";
521 Tmp3 = MakeReg(MVT::f64);
Andrew Lenharth98169be2005-07-28 18:14:47 +0000522 BuildMI(BB, Alpha::CVTST, 1, Tmp3).addReg(Alpha::F31).addReg(Tmp2);
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000523 Tmp2 = Tmp3;
524 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000525
Andrew Lenharth10c085b2005-04-02 22:32:39 +0000526 if (rev) std::swap(Tmp1, Tmp2);
527 //do the comparison
528 BuildMI(BB, Opc, 2, dst).addReg(Tmp1).addReg(Tmp2);
529 return inv;
530}
531
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000532//Check to see if the load is a constant offset from a base register
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000533void AlphaISel::SelectAddr(SDOperand N, unsigned& Reg, long& offset)
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000534{
535 unsigned opcode = N.getOpcode();
Andrew Lenharth694c2982005-06-26 23:01:11 +0000536 if (opcode == ISD::ADD && N.getOperand(1).getOpcode() == ISD::Constant &&
537 cast<ConstantSDNode>(N.getOperand(1))->getValue() <= 32767)
538 { //Normal imm add
539 Reg = SelectExpr(N.getOperand(0));
540 offset = cast<ConstantSDNode>(N.getOperand(1))->getValue();
541 return;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000542 }
543 Reg = SelectExpr(N);
544 offset = 0;
545 return;
546}
547
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000548void AlphaISel::SelectBranchCC(SDOperand N)
Andrew Lenharth445171a2005-02-08 00:40:03 +0000549{
550 assert(N.getOpcode() == ISD::BRCOND && "Not a BranchCC???");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000551 MachineBasicBlock *Dest =
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000552 cast<BasicBlockSDNode>(N.getOperand(2))->getBasicBlock();
553 unsigned Opc = Alpha::WTF;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000554
Andrew Lenharth445171a2005-02-08 00:40:03 +0000555 Select(N.getOperand(0)); //chain
556 SDOperand CC = N.getOperand(1);
Misha Brukman4633f1c2005-04-21 23:13:11 +0000557
Andrew Lenharth445171a2005-02-08 00:40:03 +0000558 if (CC.getOpcode() == ISD::SETCC)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000559 {
Chris Lattner88ac32c2005-08-09 20:21:10 +0000560 ISD::CondCode cCode= cast<CondCodeSDNode>(CC.getOperand(2))->get();
561 if (MVT::isInteger(CC.getOperand(0).getValueType())) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000562 //Dropping the CC is only useful if we are comparing to 0
Chris Lattner88ac32c2005-08-09 20:21:10 +0000563 bool RightZero = CC.getOperand(1).getOpcode() == ISD::Constant &&
564 cast<ConstantSDNode>(CC.getOperand(1))->getValue() == 0;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000565 bool isNE = false;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000566
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000567 //Fix up CC
Andrew Lenharth63b720a2005-04-03 20:35:21 +0000568 if(cCode == ISD::SETNE)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000569 isNE = true;
Andrew Lenharth445171a2005-02-08 00:40:03 +0000570
Andrew Lenharth694c2982005-06-26 23:01:11 +0000571 if (RightZero) {
Andrew Lenharth09552bf2005-06-08 18:02:21 +0000572 switch (cCode) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000573 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
574 case ISD::SETEQ: Opc = Alpha::BEQ; break;
575 case ISD::SETLT: Opc = Alpha::BLT; break;
576 case ISD::SETLE: Opc = Alpha::BLE; break;
577 case ISD::SETGT: Opc = Alpha::BGT; break;
578 case ISD::SETGE: Opc = Alpha::BGE; break;
579 case ISD::SETULT: assert(0 && "x (unsigned) < 0 is never true"); break;
580 case ISD::SETUGT: Opc = Alpha::BNE; break;
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000581 //Technically you could have this CC
582 case ISD::SETULE: Opc = Alpha::BEQ; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000583 case ISD::SETUGE: assert(0 && "x (unsgined >= 0 is always true"); break;
584 case ISD::SETNE: Opc = Alpha::BNE; break;
585 }
Chris Lattner88ac32c2005-08-09 20:21:10 +0000586 unsigned Tmp1 = SelectExpr(CC.getOperand(0)); //Cond
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000587 BuildMI(BB, Opc, 2).addReg(Tmp1).addMBB(Dest);
588 return;
589 } else {
590 unsigned Tmp1 = SelectExpr(CC);
591 if (isNE)
592 BuildMI(BB, Alpha::BEQ, 2).addReg(CCInvMap[CC]).addMBB(Dest);
593 else
594 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000595 return;
596 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000597 } else { //FP
Jeff Cohen00b168892005-07-27 06:12:32 +0000598 //Any comparison between 2 values should be codegened as an folded
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000599 //branch, as moving CC to the integer register is very expensive
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000600 //for a cmp b: c = a - b;
601 //a = b: c = 0
602 //a < b: c < 0
603 //a > b: c > 0
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000604
605 bool invTest = false;
606 unsigned Tmp3;
607
608 ConstantFPSDNode *CN;
Chris Lattner88ac32c2005-08-09 20:21:10 +0000609 if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(1)))
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000610 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
Chris Lattner88ac32c2005-08-09 20:21:10 +0000611 Tmp3 = SelectExpr(CC.getOperand(0));
612 else if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(0)))
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000613 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
614 {
Chris Lattner88ac32c2005-08-09 20:21:10 +0000615 Tmp3 = SelectExpr(CC.getOperand(1));
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000616 invTest = true;
617 }
618 else
619 {
Chris Lattner88ac32c2005-08-09 20:21:10 +0000620 unsigned Tmp1 = SelectExpr(CC.getOperand(0));
621 unsigned Tmp2 = SelectExpr(CC.getOperand(1));
622 bool isD = CC.getOperand(0).getValueType() == MVT::f64;
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000623 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
624 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
625 .addReg(Tmp1).addReg(Tmp2);
626 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000627
Chris Lattner88ac32c2005-08-09 20:21:10 +0000628 switch (cCode) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000629 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
Andrew Lenharth2b6c4f52005-02-25 22:55:15 +0000630 case ISD::SETEQ: Opc = invTest ? Alpha::FBNE : Alpha::FBEQ; break;
631 case ISD::SETLT: Opc = invTest ? Alpha::FBGT : Alpha::FBLT; break;
632 case ISD::SETLE: Opc = invTest ? Alpha::FBGE : Alpha::FBLE; break;
633 case ISD::SETGT: Opc = invTest ? Alpha::FBLT : Alpha::FBGT; break;
634 case ISD::SETGE: Opc = invTest ? Alpha::FBLE : Alpha::FBGE; break;
635 case ISD::SETNE: Opc = invTest ? Alpha::FBEQ : Alpha::FBNE; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000636 }
637 BuildMI(BB, Opc, 2).addReg(Tmp3).addMBB(Dest);
Andrew Lenharth445171a2005-02-08 00:40:03 +0000638 return;
639 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000640 abort(); //Should never be reached
641 } else {
642 //Giveup and do the stupid thing
643 unsigned Tmp1 = SelectExpr(CC);
644 BuildMI(BB, Alpha::BNE, 2).addReg(Tmp1).addMBB(Dest);
645 return;
646 }
Andrew Lenharth445171a2005-02-08 00:40:03 +0000647 abort(); //Should never be reached
648}
649
Andrew Lenharthb69f3422005-06-22 17:19:45 +0000650unsigned AlphaISel::SelectExpr(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000651 unsigned Result;
Andrew Lenharth2966e842005-04-07 18:15:28 +0000652 unsigned Tmp1, Tmp2 = 0, Tmp3;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000653 unsigned Opc = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000654 unsigned opcode = N.getOpcode();
Andrew Lenharthd2284272005-08-15 14:31:37 +0000655 int64_t SImm;
656 uint64_t UImm;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000657
658 SDNode *Node = N.Val;
Andrew Lenharth40831c52005-01-28 06:57:18 +0000659 MVT::ValueType DestType = N.getValueType();
Andrew Lenharthf4da9452005-06-29 12:49:51 +0000660 bool isFP = DestType == MVT::f64 || DestType == MVT::f32;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000661
662 unsigned &Reg = ExprMap[N];
663 if (Reg) return Reg;
664
Andrew Lenharth46a776e2005-09-06 17:00:23 +0000665 switch(N.getOpcode()) {
666 default:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000667 Reg = Result = (N.getValueType() != MVT::Other) ?
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000668 MakeReg(N.getValueType()) : notIn;
Andrew Lenharth46a776e2005-09-06 17:00:23 +0000669 break;
670 case ISD::AssertSext:
671 case ISD::AssertZext:
672 return Reg = SelectExpr(N.getOperand(0));
673 case ISD::CALL:
674 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000675 // If this is a call instruction, make sure to prepare ALL of the result
676 // values as well as the chain.
677 if (Node->getNumValues() == 1)
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000678 Reg = Result = notIn; // Void call, just a chain.
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000679 else {
680 Result = MakeReg(Node->getValueType(0));
681 ExprMap[N.getValue(0)] = Result;
682 for (unsigned i = 1, e = N.Val->getNumValues()-1; i != e; ++i)
683 ExprMap[N.getValue(i)] = MakeReg(Node->getValueType(i));
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000684 ExprMap[SDOperand(Node, Node->getNumValues()-1)] = notIn;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000685 }
Andrew Lenharth46a776e2005-09-06 17:00:23 +0000686 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000687 }
688
Andrew Lenharth40831c52005-01-28 06:57:18 +0000689 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000690 default:
691 Node->dump();
692 assert(0 && "Node not handled!\n");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000693
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000694 case ISD::CTPOP:
695 case ISD::CTTZ:
696 case ISD::CTLZ:
697 Opc = opcode == ISD::CTPOP ? Alpha::CTPOP :
698 (opcode == ISD::CTTZ ? Alpha::CTTZ : Alpha::CTLZ);
699 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthf3f951a2005-07-22 20:50:29 +0000700 BuildMI(BB, Opc, 1, Result).addReg(Alpha::R31).addReg(Tmp1);
Andrew Lenharth691ef2b2005-05-03 17:19:30 +0000701 return Result;
702
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000703 case ISD::MULHU:
704 Tmp1 = SelectExpr(N.getOperand(0));
705 Tmp2 = SelectExpr(N.getOperand(1));
706 BuildMI(BB, Alpha::UMULH, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharth706be912005-04-07 13:55:53 +0000707 return Result;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000708 case ISD::MULHS:
709 {
710 //MULHU - Ra<63>*Rb - Rb<63>*Ra
711 Tmp1 = SelectExpr(N.getOperand(0));
712 Tmp2 = SelectExpr(N.getOperand(1));
713 Tmp3 = MakeReg(MVT::i64);
714 BuildMI(BB, Alpha::UMULH, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
715 unsigned V1 = MakeReg(MVT::i64);
716 unsigned V2 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000717 BuildMI(BB, Alpha::CMOVGE, 3, V1).addReg(Tmp2).addReg(Alpha::R31)
718 .addReg(Tmp1);
719 BuildMI(BB, Alpha::CMOVGE, 3, V2).addReg(Tmp1).addReg(Alpha::R31)
720 .addReg(Tmp2);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +0000721 unsigned IRes = MakeReg(MVT::i64);
722 BuildMI(BB, Alpha::SUBQ, 2, IRes).addReg(Tmp3).addReg(V1);
723 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(IRes).addReg(V2);
724 return Result;
725 }
Andrew Lenharth7332f3e2005-04-02 19:11:07 +0000726 case ISD::UNDEF: {
727 BuildMI(BB, Alpha::IDEF, 0, Result);
728 return Result;
729 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000730
Andrew Lenharth032f2352005-02-22 21:59:48 +0000731 case ISD::DYNAMIC_STACKALLOC:
732 // Generate both result values.
Andrew Lenharth3a7118d2005-02-23 17:33:42 +0000733 if (Result != notIn)
734 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth032f2352005-02-22 21:59:48 +0000735 else
736 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
737
738 // FIXME: We are currently ignoring the requested alignment for handling
739 // greater than the stack alignment. This will need to be revisited at some
740 // point. Align = N.getOperand(2);
741
742 if (!isa<ConstantSDNode>(N.getOperand(2)) ||
743 cast<ConstantSDNode>(N.getOperand(2))->getValue() != 0) {
744 std::cerr << "Cannot allocate stack object with greater alignment than"
745 << " the stack alignment yet!";
746 abort();
747 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000748
Andrew Lenharth032f2352005-02-22 21:59:48 +0000749 Select(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +0000750 if (isSIntImmediateBounded(N.getOperand(1), SImm, 0, 32767))
751 BuildMI(BB, Alpha::LDA, 2, Alpha::R30).addImm(-SImm).addReg(Alpha::R30);
752 else {
Andrew Lenharth032f2352005-02-22 21:59:48 +0000753 Tmp1 = SelectExpr(N.getOperand(1));
754 // Subtract size from stack pointer, thereby allocating some space.
755 BuildMI(BB, Alpha::SUBQ, 2, Alpha::R30).addReg(Alpha::R30).addReg(Tmp1);
756 }
757
758 // Put a pointer to the space into the result register, by copying the stack
759 // pointer.
Andrew Lenharth7bc47022005-02-22 23:29:25 +0000760 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R30).addReg(Alpha::R30);
Andrew Lenharth032f2352005-02-22 21:59:48 +0000761 return Result;
762
Andrew Lenharth02c318e2005-06-27 21:02:56 +0000763 case ISD::ConstantPool:
Chris Lattner5839bf22005-08-26 17:15:30 +0000764 Tmp1 = BB->getParent()->getConstantPool()->
765 getConstantPoolIndex(cast<ConstantPoolSDNode>(N)->get());
Andrew Lenharth02c318e2005-06-27 21:02:56 +0000766 AlphaLowering.restoreGP(BB);
767 Tmp2 = MakeReg(MVT::i64);
768 BuildMI(BB, Alpha::LDAHr, 2, Tmp2).addConstantPoolIndex(Tmp1)
769 .addReg(Alpha::R29);
770 BuildMI(BB, Alpha::LDAr, 2, Result).addConstantPoolIndex(Tmp1)
771 .addReg(Tmp2);
772 return Result;
Andrew Lenharth2c594352005-01-29 15:42:07 +0000773
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000774 case ISD::FrameIndex:
Andrew Lenharth032f2352005-02-22 21:59:48 +0000775 BuildMI(BB, Alpha::LDA, 2, Result)
776 .addFrameIndex(cast<FrameIndexSDNode>(N)->getIndex())
777 .addReg(Alpha::F31);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000778 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000779
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000780 case ISD::EXTLOAD:
Andrew Lenharthf311e8b2005-02-07 05:18:02 +0000781 case ISD::ZEXTLOAD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000782 case ISD::SEXTLOAD:
Misha Brukman4633f1c2005-04-21 23:13:11 +0000783 case ISD::LOAD:
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000784 {
785 // Make sure we generate both values.
786 if (Result != notIn)
787 ExprMap[N.getValue(1)] = notIn; // Generate the token
788 else
789 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +0000790
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000791 SDOperand Chain = N.getOperand(0);
792 SDOperand Address = N.getOperand(1);
793 Select(Chain);
794
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000795 bool fpext = true;
796
Andrew Lenharth03824012005-02-07 05:55:55 +0000797 if (opcode == ISD::LOAD)
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000798 switch (Node->getValueType(0)) {
799 default: Node->dump(); assert(0 && "Bad load!");
800 case MVT::i64: Opc = Alpha::LDQ; break;
801 case MVT::f64: Opc = Alpha::LDT; break;
802 case MVT::f32: Opc = Alpha::LDS; break;
803 }
Andrew Lenharth03824012005-02-07 05:55:55 +0000804 else
Chris Lattnerbce81ae2005-07-10 01:56:13 +0000805 switch (cast<VTSDNode>(Node->getOperand(3))->getVT()) {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000806 default: Node->dump(); assert(0 && "Bad sign extend!");
Misha Brukman4633f1c2005-04-21 23:13:11 +0000807 case MVT::i32: Opc = Alpha::LDL;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000808 assert(opcode != ISD::ZEXTLOAD && "Not sext"); break;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000809 case MVT::i16: Opc = Alpha::LDWU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000810 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharthf311e8b2005-02-07 05:18:02 +0000811 case MVT::i1: //FIXME: Treat i1 as i8 since there are problems otherwise
Misha Brukman4633f1c2005-04-21 23:13:11 +0000812 case MVT::i8: Opc = Alpha::LDBU;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000813 assert(opcode != ISD::SEXTLOAD && "Not zext"); break;
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000814 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000815
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000816 int i, j, k;
817 if (EnableAlphaLSMark)
818 getValueInfo(dyn_cast<SrcValueSDNode>(N.getOperand(2))->getValue(),
819 i, j, k);
820
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000821 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Address);
822 if (GASD && !GASD->getGlobal()->isExternal()) {
823 Tmp1 = MakeReg(MVT::i64);
824 AlphaLowering.restoreGP(BB);
825 BuildMI(BB, Alpha::LDAHr, 2, Tmp1)
826 .addGlobalAddress(GASD->getGlobal()).addReg(Alpha::R29);
827 if (EnableAlphaLSMark)
828 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
829 .addImm(getUID());
830 BuildMI(BB, GetRelVersion(Opc), 2, Result)
831 .addGlobalAddress(GASD->getGlobal()).addReg(Tmp1);
Chris Lattnerbce81ae2005-07-10 01:56:13 +0000832 } else if (ConstantPoolSDNode *CP =
833 dyn_cast<ConstantPoolSDNode>(Address)) {
Chris Lattner5839bf22005-08-26 17:15:30 +0000834 unsigned CPIdx = BB->getParent()->getConstantPool()->
835 getConstantPoolIndex(CP->get());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000836 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000837 has_sym = true;
Andrew Lenharthfe895e32005-06-27 17:15:36 +0000838 Tmp1 = MakeReg(MVT::i64);
Chris Lattner5839bf22005-08-26 17:15:30 +0000839 BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CPIdx)
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000840 .addReg(Alpha::R29);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000841 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000842 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
843 .addImm(getUID());
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000844 BuildMI(BB, GetRelVersion(Opc), 2, Result)
Chris Lattner5839bf22005-08-26 17:15:30 +0000845 .addConstantPoolIndex(CPIdx).addReg(Tmp1);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000846 } else if(Address.getOpcode() == ISD::FrameIndex) {
847 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000848 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
849 .addImm(getUID());
Andrew Lenharth032f2352005-02-22 21:59:48 +0000850 BuildMI(BB, Opc, 2, Result)
851 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
852 .addReg(Alpha::F31);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000853 } else {
854 long offset;
855 SelectAddr(Address, Tmp1, offset);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000856 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000857 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
858 .addImm(getUID());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000859 BuildMI(BB, Opc, 2, Result).addImm(offset).addReg(Tmp1);
860 }
Andrew Lenharth9e8d1092005-02-06 15:40:40 +0000861 return Result;
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000862 }
Andrew Lenharth2f8fb772005-01-25 00:35:34 +0000863
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000864 case ISD::GlobalAddress:
865 AlphaLowering.restoreGP(BB);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000866 has_sym = true;
Jeff Cohen00b168892005-07-27 06:12:32 +0000867
Andrew Lenharth2f5bca52005-07-03 20:06:13 +0000868 Reg = Result = MakeReg(MVT::i64);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000869
870 if (EnableAlphaLSMark)
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000871 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
Andrew Lenharth06ef8842005-06-29 18:54:02 +0000872 .addImm(getUID());
Andrew Lenharthc7989ce2005-06-29 00:31:08 +0000873
874 BuildMI(BB, Alpha::LDQl, 2, Result)
Andrew Lenharthc95d9842005-06-27 21:11:40 +0000875 .addGlobalAddress(cast<GlobalAddressSDNode>(N)->getGlobal())
876 .addReg(Alpha::R29);
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000877 return Result;
878
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000879 case ISD::ExternalSymbol:
880 AlphaLowering.restoreGP(BB);
881 has_sym = true;
882
Andrew Lenharth2f5bca52005-07-03 20:06:13 +0000883 Reg = Result = MakeReg(MVT::i64);
884
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000885 if (EnableAlphaLSMark)
886 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
887 .addImm(getUID());
888
889 BuildMI(BB, Alpha::LDQl, 2, Result)
890 .addExternalSymbol(cast<ExternalSymbolSDNode>(N)->getSymbol())
891 .addReg(Alpha::R29);
892 return Result;
893
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +0000894 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000895 case ISD::CALL:
896 {
897 Select(N.getOperand(0));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000898
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000899 // The chain for this call is now lowered.
Andrew Lenharthf3f951a2005-07-22 20:50:29 +0000900 ExprMap[N.getValue(Node->getNumValues()-1)] = notIn;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000901
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000902 //grab the arguments
903 std::vector<unsigned> argvregs;
Andrew Lenharth7b2a5272005-01-30 20:42:36 +0000904 //assert(Node->getNumOperands() < 8 && "Only 6 args supported");
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000905 for(int i = 2, e = Node->getNumOperands(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000906 argvregs.push_back(SelectExpr(N.getOperand(i)));
Misha Brukman4633f1c2005-04-21 23:13:11 +0000907
Andrew Lenharth684f2292005-01-30 00:35:27 +0000908 //in reg args
909 for(int i = 0, e = std::min(6, (int)argvregs.size()); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000910 {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000911 unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000912 Alpha::R19, Alpha::R20, Alpha::R21};
Misha Brukman4633f1c2005-04-21 23:13:11 +0000913 unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000914 Alpha::F19, Alpha::F20, Alpha::F21};
915 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000916 default:
917 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000918 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000919 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000920 N.getOperand(i+2).getValueType() << "\n";
921 assert(0 && "Unknown value type for call");
922 case MVT::i1:
923 case MVT::i8:
924 case MVT::i16:
925 case MVT::i32:
926 case MVT::i64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000927 BuildMI(BB, Alpha::BIS, 2, args_int[i]).addReg(argvregs[i])
928 .addReg(argvregs[i]);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000929 break;
930 case MVT::f32:
931 case MVT::f64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000932 BuildMI(BB, Alpha::CPYS, 2, args_float[i]).addReg(argvregs[i])
933 .addReg(argvregs[i]);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000934 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000935 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000936 }
Andrew Lenharth684f2292005-01-30 00:35:27 +0000937 //in mem args
938 for (int i = 6, e = argvregs.size(); i < e; ++i)
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000939 {
940 switch(N.getOperand(i+2).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +0000941 default:
942 Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000943 N.getOperand(i).Val->dump();
Misha Brukman4633f1c2005-04-21 23:13:11 +0000944 std::cerr << "Type for " << i << " is: " <<
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000945 N.getOperand(i+2).getValueType() << "\n";
946 assert(0 && "Unknown value type for call");
947 case MVT::i1:
948 case MVT::i8:
949 case MVT::i16:
950 case MVT::i32:
951 case MVT::i64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000952 BuildMI(BB, Alpha::STQ, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
953 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000954 break;
955 case MVT::f32:
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000956 BuildMI(BB, Alpha::STS, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
957 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000958 break;
959 case MVT::f64:
Andrew Lenharthd4653b12005-06-27 17:39:17 +0000960 BuildMI(BB, Alpha::STT, 3).addReg(argvregs[i]).addImm((i - 6) * 8)
961 .addReg(Alpha::R30);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000962 break;
Andrew Lenharth684f2292005-01-30 00:35:27 +0000963 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000964 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000965 //build the right kind of call
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000966 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(N.getOperand(1));
967 if (GASD && !GASD->getGlobal()->isExternal()) {
968 //use PC relative branch call
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000969 AlphaLowering.restoreGP(BB);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +0000970 BuildMI(BB, Alpha::BSR, 1, Alpha::R26)
971 .addGlobalAddress(GASD->getGlobal(),true);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +0000972 } else {
973 //no need to restore GP as we are doing an indirect call
974 Tmp1 = SelectExpr(N.getOperand(1));
975 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp1).addReg(Tmp1);
976 BuildMI(BB, Alpha::JSR, 2, Alpha::R26).addReg(Alpha::R27).addImm(0);
977 }
Misha Brukman4633f1c2005-04-21 23:13:11 +0000978
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000979 //push the result into a virtual register
Misha Brukman4633f1c2005-04-21 23:13:11 +0000980
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000981 switch (Node->getValueType(0)) {
982 default: Node->dump(); assert(0 && "Unknown value type for call result!");
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +0000983 case MVT::Other: return notIn;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000984 case MVT::i64:
Misha Brukman7847fca2005-04-22 17:54:37 +0000985 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R0).addReg(Alpha::R0);
986 break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000987 case MVT::f32:
988 case MVT::f64:
Misha Brukman7847fca2005-04-22 17:54:37 +0000989 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F0).addReg(Alpha::F0);
990 break;
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000991 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +0000992 return Result+N.ResNo;
Misha Brukman4633f1c2005-04-21 23:13:11 +0000993 }
994
Andrew Lenharth304d0f32005-01-22 23:41:55 +0000995 case ISD::SIGN_EXTEND_INREG:
996 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +0000997 //do SDIV opt for all levels of ints if not dividing by a constant
998 if (EnableAlphaIDIV && N.getOperand(0).getOpcode() == ISD::SDIV
999 && N.getOperand(0).getOperand(1).getOpcode() != ISD::Constant)
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001000 {
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001001 unsigned Tmp4 = MakeReg(MVT::f64);
1002 unsigned Tmp5 = MakeReg(MVT::f64);
1003 unsigned Tmp6 = MakeReg(MVT::f64);
1004 unsigned Tmp7 = MakeReg(MVT::f64);
1005 unsigned Tmp8 = MakeReg(MVT::f64);
1006 unsigned Tmp9 = MakeReg(MVT::f64);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001007
1008 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1009 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1010 MoveInt2FP(Tmp1, Tmp4, true);
1011 MoveInt2FP(Tmp2, Tmp5, true);
Andrew Lenharth98169be2005-07-28 18:14:47 +00001012 BuildMI(BB, Alpha::CVTQT, 1, Tmp6).addReg(Alpha::F31).addReg(Tmp4);
1013 BuildMI(BB, Alpha::CVTQT, 1, Tmp7).addReg(Alpha::F31).addReg(Tmp5);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001014 BuildMI(BB, Alpha::DIVT, 2, Tmp8).addReg(Tmp6).addReg(Tmp7);
Andrew Lenharth98169be2005-07-28 18:14:47 +00001015 BuildMI(BB, Alpha::CVTTQ, 1, Tmp9).addReg(Alpha::F31).addReg(Tmp8);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001016 MoveFP2Int(Tmp9, Result, true);
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001017 return Result;
1018 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001019
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001020 //Alpha has instructions for a bunch of signed 32 bit stuff
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001021 if(cast<VTSDNode>(Node->getOperand(1))->getVT() == MVT::i32) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001022 switch (N.getOperand(0).getOpcode()) {
1023 case ISD::ADD:
1024 case ISD::SUB:
1025 case ISD::MUL:
1026 {
1027 bool isAdd = N.getOperand(0).getOpcode() == ISD::ADD;
1028 bool isMul = N.getOperand(0).getOpcode() == ISD::MUL;
1029 //FIXME: first check for Scaled Adds and Subs!
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001030 if(!isMul && N.getOperand(0).getOperand(0).getOpcode() == ISD::SHL &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001031 isSIntImmediateBounded(N.getOperand(0).getOperand(0).getOperand(1), SImm, 2, 3))
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001032 {
Andrew Lenharthd2284272005-08-15 14:31:37 +00001033 bool use4 = SImm == 2;
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001034 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0).getOperand(0));
1035 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
1036 BuildMI(BB, isAdd?(use4?Alpha::S4ADDL:Alpha::S8ADDL):(use4?Alpha::S4SUBL:Alpha::S8SUBL),
1037 2,Result).addReg(Tmp1).addReg(Tmp2);
1038 }
1039 else if(isAdd && N.getOperand(0).getOperand(1).getOpcode() == ISD::SHL &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001040 isSIntImmediateBounded(N.getOperand(0).getOperand(1).getOperand(1), SImm, 2, 3))
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001041 {
Andrew Lenharthd2284272005-08-15 14:31:37 +00001042 bool use4 = SImm == 2;
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001043 Tmp1 = SelectExpr(N.getOperand(0).getOperand(1).getOperand(0));
1044 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1045 BuildMI(BB, use4?Alpha::S4ADDL:Alpha::S8ADDL, 2,Result).addReg(Tmp1).addReg(Tmp2);
1046 }
Andrew Lenharthd2284272005-08-15 14:31:37 +00001047 else if(isSIntImmediateBounded(N.getOperand(0).getOperand(1), SImm, 0, 255))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001048 { //Normal imm add/sub
1049 Opc = isAdd ? Alpha::ADDLi : (isMul ? Alpha::MULLi : Alpha::SUBLi);
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001050 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001051 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001052 }
Andrew Lenharthd2284272005-08-15 14:31:37 +00001053 else if(!isMul && isSIntImmediate(N.getOperand(0).getOperand(1), SImm) &&
1054 (((SImm << 32) >> 32) >= -255) && (((SImm << 32) >> 32) <= 0))
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001055 { //handle canonicalization
1056 Opc = isAdd ? Alpha::SUBLi : Alpha::ADDLi;
1057 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001058 SImm = 0 - ((SImm << 32) >> 32);
1059 assert(SImm >= 0 && SImm <= 255);
1060 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001061 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001062 else
1063 { //Normal add/sub
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001064 Opc = isAdd ? Alpha::ADDL : (isMul ? Alpha::MULL : Alpha::SUBL);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001065 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001066 Tmp2 = SelectExpr(N.getOperand(0).getOperand(1));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001067 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1068 }
1069 return Result;
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001070 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001071 default: break; //Fall Though;
1072 }
1073 } //Every thing else fall though too, including unhandled opcodes above
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001074 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001075 //std::cerr << "SrcT: " << MVN->getExtraValueType() << "\n";
Chris Lattnerbce81ae2005-07-10 01:56:13 +00001076 switch(cast<VTSDNode>(Node->getOperand(1))->getVT()) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001077 default:
1078 Node->dump();
1079 assert(0 && "Sign Extend InReg not there yet");
1080 break;
1081 case MVT::i32:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001082 {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001083 BuildMI(BB, Alpha::ADDLi, 2, Result).addReg(Tmp1).addImm(0);
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001084 break;
1085 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001086 case MVT::i16:
Andrew Lenharthf3f951a2005-07-22 20:50:29 +00001087 BuildMI(BB, Alpha::SEXTW, 1, Result).addReg(Alpha::R31).addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001088 break;
1089 case MVT::i8:
Andrew Lenharthf3f951a2005-07-22 20:50:29 +00001090 BuildMI(BB, Alpha::SEXTB, 1, Result).addReg(Alpha::R31).addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001091 break;
Andrew Lenharthebce5042005-02-12 19:35:12 +00001092 case MVT::i1:
1093 Tmp2 = MakeReg(MVT::i64);
1094 BuildMI(BB, Alpha::ANDi, 2, Tmp2).addReg(Tmp1).addImm(1);
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001095 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp2);
Andrew Lenharthebce5042005-02-12 19:35:12 +00001096 break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001097 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001098 return Result;
1099 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001100
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001101 case ISD::SETCC:
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001102 {
Chris Lattner88ac32c2005-08-09 20:21:10 +00001103 ISD::CondCode CC = cast<CondCodeSDNode>(N.getOperand(2))->get();
1104 if (MVT::isInteger(N.getOperand(0).getValueType())) {
1105 bool isConst = false;
1106 int dir;
Misha Brukman7847fca2005-04-22 17:54:37 +00001107
Chris Lattner88ac32c2005-08-09 20:21:10 +00001108 //Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001109 if(isSIntImmediate(N.getOperand(1), SImm) && SImm <= 255 && SImm >= 0)
Chris Lattner88ac32c2005-08-09 20:21:10 +00001110 isConst = true;
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001111
Chris Lattner88ac32c2005-08-09 20:21:10 +00001112 switch (CC) {
1113 default: Node->dump(); assert(0 && "Unknown integer comparison!");
1114 case ISD::SETEQ:
1115 Opc = isConst ? Alpha::CMPEQi : Alpha::CMPEQ; dir=1; break;
1116 case ISD::SETLT:
1117 Opc = isConst ? Alpha::CMPLTi : Alpha::CMPLT; dir = 1; break;
1118 case ISD::SETLE:
1119 Opc = isConst ? Alpha::CMPLEi : Alpha::CMPLE; dir = 1; break;
1120 case ISD::SETGT: Opc = Alpha::CMPLT; dir = 2; break;
1121 case ISD::SETGE: Opc = Alpha::CMPLE; dir = 2; break;
1122 case ISD::SETULT:
1123 Opc = isConst ? Alpha::CMPULTi : Alpha::CMPULT; dir = 1; break;
1124 case ISD::SETUGT: Opc = Alpha::CMPULT; dir = 2; break;
1125 case ISD::SETULE:
1126 Opc = isConst ? Alpha::CMPULEi : Alpha::CMPULE; dir = 1; break;
1127 case ISD::SETUGE: Opc = Alpha::CMPULE; dir = 2; break;
1128 case ISD::SETNE: {//Handle this one special
1129 //std::cerr << "Alpha does not have a setne.\n";
1130 //abort();
1131 Tmp1 = SelectExpr(N.getOperand(0));
1132 Tmp2 = SelectExpr(N.getOperand(1));
1133 Tmp3 = MakeReg(MVT::i64);
1134 BuildMI(BB, Alpha::CMPEQ, 2, Tmp3).addReg(Tmp1).addReg(Tmp2);
1135 //Remeber we have the Inv for this CC
1136 CCInvMap[N] = Tmp3;
1137 //and invert
1138 BuildMI(BB, Alpha::CMPEQ, 2, Result).addReg(Alpha::R31).addReg(Tmp3);
1139 return Result;
1140 }
1141 }
1142 if (dir == 1) {
1143 Tmp1 = SelectExpr(N.getOperand(0));
1144 if (isConst) {
Andrew Lenharthd2284272005-08-15 14:31:37 +00001145 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Chris Lattner88ac32c2005-08-09 20:21:10 +00001146 } else {
Andrew Lenharthd2bb9602005-01-27 07:50:35 +00001147 Tmp2 = SelectExpr(N.getOperand(1));
Andrew Lenharth694c2982005-06-26 23:01:11 +00001148 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
Andrew Lenharthd4bdd542005-02-05 16:41:03 +00001149 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001150 } else { //if (dir == 2) {
1151 Tmp1 = SelectExpr(N.getOperand(1));
1152 Tmp2 = SelectExpr(N.getOperand(0));
1153 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 {
1156 //do the comparison
1157 Tmp1 = MakeReg(MVT::f64);
1158 bool inv = SelectFPSetCC(N, Tmp1);
1159
1160 //now arrange for Result (int) to have a 1 or 0
1161 Tmp2 = MakeReg(MVT::i64);
1162 BuildMI(BB, Alpha::ADDQi, 2, Tmp2).addReg(Alpha::R31).addImm(1);
1163 Opc = inv?Alpha::CMOVNEi_FP:Alpha::CMOVEQi_FP;
1164 BuildMI(BB, Opc, 3, Result).addReg(Tmp2).addImm(0).addReg(Tmp1);
Andrew Lenharth9818c052005-02-05 13:19:12 +00001165 }
Andrew Lenharth3d65d312005-01-27 03:49:45 +00001166 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001167 }
Misha Brukman4633f1c2005-04-21 23:13:11 +00001168
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001169 case ISD::CopyFromReg:
1170 {
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001171 ++count_ins;
1172
Andrew Lenharth40831c52005-01-28 06:57:18 +00001173 // Make sure we generate both values.
Andrew Lenharthcc1b16f2005-01-28 23:17:54 +00001174 if (Result != notIn)
Misha Brukman7847fca2005-04-22 17:54:37 +00001175 ExprMap[N.getValue(1)] = notIn; // Generate the token
Andrew Lenharth40831c52005-01-28 06:57:18 +00001176 else
Misha Brukman7847fca2005-04-22 17:54:37 +00001177 Result = ExprMap[N.getValue(0)] = MakeReg(N.getValue(0).getValueType());
Misha Brukman4633f1c2005-04-21 23:13:11 +00001178
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001179 SDOperand Chain = N.getOperand(0);
1180
1181 Select(Chain);
Chris Lattner707ebc52005-08-16 21:56:37 +00001182 unsigned r = cast<RegisterSDNode>(Node->getOperand(1))->getReg();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001183 //std::cerr << "CopyFromReg " << Result << " = " << r << "\n";
Andrew Lenharth619fb522005-07-04 20:07:21 +00001184 if (MVT::isFloatingPoint(N.getValue(0).getValueType()))
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001185 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(r).addReg(r);
1186 else
1187 BuildMI(BB, Alpha::BIS, 2, Result).addReg(r).addReg(r);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001188 return Result;
1189 }
1190
Misha Brukman4633f1c2005-04-21 23:13:11 +00001191 //Most of the plain arithmetic and logic share the same form, and the same
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001192 //constant immediate test
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001193 case ISD::XOR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001194 //Match Not
Andrew Lenharthd2284272005-08-15 14:31:37 +00001195 if (isSIntImmediate(N.getOperand(1), SImm) && SImm == -1) {
1196 Tmp1 = SelectExpr(N.getOperand(0));
1197 BuildMI(BB, Alpha::ORNOT, 2, Result).addReg(Alpha::R31).addReg(Tmp1);
1198 return Result;
1199 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001200 //Fall through
1201 case ISD::AND:
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001202 //handle zap
Andrew Lenharthd2284272005-08-15 14:31:37 +00001203 if (opcode == ISD::AND && isUIntImmediate(N.getOperand(1), UImm))
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001204 {
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001205 unsigned int build = 0;
1206 for(int i = 0; i < 8; ++i)
1207 {
Andrew Lenharthd2284272005-08-15 14:31:37 +00001208 if ((UImm & 0x00FF) == 0x00FF)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001209 build |= 1 << i;
Andrew Lenharthd2284272005-08-15 14:31:37 +00001210 else if ((UImm & 0x00FF) != 0)
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001211 { build = 0; break; }
Andrew Lenharthd2284272005-08-15 14:31:37 +00001212 UImm >>= 8;
Andrew Lenharth483f22d2005-04-13 03:47:03 +00001213 }
1214 if (build)
1215 {
1216 Tmp1 = SelectExpr(N.getOperand(0));
1217 BuildMI(BB, Alpha::ZAPNOTi, 2, Result).addReg(Tmp1).addImm(build);
1218 return Result;
1219 }
1220 }
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001221 case ISD::OR:
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001222 //Check operand(0) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001223 if (N.getOperand(0).getOpcode() == ISD::XOR &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001224 isSIntImmediate(N.getOperand(0).getOperand(1), SImm) && SImm == -1) {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001225 switch(opcode) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001226 case ISD::AND: Opc = Alpha::BIC; break;
1227 case ISD::OR: Opc = Alpha::ORNOT; break;
1228 case ISD::XOR: Opc = Alpha::EQV; break;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001229 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001230 Tmp1 = SelectExpr(N.getOperand(1));
1231 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
1232 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1233 return Result;
1234 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001235 //Check operand(1) == Not
Misha Brukman4633f1c2005-04-21 23:13:11 +00001236 if (N.getOperand(1).getOpcode() == ISD::XOR &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001237 isSIntImmediate(N.getOperand(1).getOperand(1), SImm) && SImm == -1) {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001238 switch(opcode) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001239 case ISD::AND: Opc = Alpha::BIC; break;
1240 case ISD::OR: Opc = Alpha::ORNOT; break;
1241 case ISD::XOR: Opc = Alpha::EQV; break;
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001242 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001243 Tmp1 = SelectExpr(N.getOperand(0));
1244 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
1245 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1246 return Result;
1247 }
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001248 //Fall through
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001249 case ISD::SHL:
1250 case ISD::SRL:
Andrew Lenharth2c594352005-01-29 15:42:07 +00001251 case ISD::SRA:
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001252 case ISD::MUL:
Andrew Lenharthd2284272005-08-15 14:31:37 +00001253 if(isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255)) {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001254 switch(opcode) {
1255 case ISD::AND: Opc = Alpha::ANDi; break;
1256 case ISD::OR: Opc = Alpha::BISi; break;
1257 case ISD::XOR: Opc = Alpha::XORi; break;
1258 case ISD::SHL: Opc = Alpha::SLi; break;
1259 case ISD::SRL: Opc = Alpha::SRLi; break;
1260 case ISD::SRA: Opc = Alpha::SRAi; break;
1261 case ISD::MUL: Opc = Alpha::MULQi; break;
1262 };
1263 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001264 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001265 } else {
1266 switch(opcode) {
1267 case ISD::AND: Opc = Alpha::AND; break;
1268 case ISD::OR: Opc = Alpha::BIS; break;
1269 case ISD::XOR: Opc = Alpha::XOR; break;
1270 case ISD::SHL: Opc = Alpha::SL; break;
1271 case ISD::SRL: Opc = Alpha::SRL; break;
1272 case ISD::SRA: Opc = Alpha::SRA; break;
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001273 case ISD::MUL: Opc = Alpha::MULQ; break;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001274 };
1275 Tmp1 = SelectExpr(N.getOperand(0));
1276 Tmp2 = SelectExpr(N.getOperand(1));
1277 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1278 }
Andrew Lenharth2d6f0222005-01-24 19:44:07 +00001279 return Result;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001280
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001281 case ISD::ADD:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001282 case ISD::SUB:
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001283 {
Andrew Lenharth40831c52005-01-28 06:57:18 +00001284 bool isAdd = opcode == ISD::ADD;
1285
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001286 //first check for Scaled Adds and Subs!
1287 //Valid for add and sub
Andrew Lenharthd2284272005-08-15 14:31:37 +00001288 if(N.getOperand(0).getOpcode() == ISD::SHL &&
1289 isSIntImmediate(N.getOperand(0).getOperand(1), SImm) &&
1290 (SImm == 2 || SImm == 3)) {
1291 bool use4 = SImm == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001292 Tmp2 = SelectExpr(N.getOperand(0).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001293 if (isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255))
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001294 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
Andrew Lenharthd2284272005-08-15 14:31:37 +00001295 2, Result).addReg(Tmp2).addImm(SImm);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001296 else {
1297 Tmp1 = SelectExpr(N.getOperand(1));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001298 BuildMI(BB, isAdd?(use4?Alpha::S4ADDQi:Alpha::S8ADDQi):(use4?Alpha::S4SUBQi:Alpha::S8SUBQi),
1299 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001300 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001301 }
1302 //Position prevents subs
Andrew Lenharth273a1f92005-04-07 14:18:13 +00001303 else if(N.getOperand(1).getOpcode() == ISD::SHL && isAdd &&
Andrew Lenharthd2284272005-08-15 14:31:37 +00001304 isSIntImmediate(N.getOperand(1).getOperand(1), SImm) &&
1305 (SImm == 2 || SImm == 3)) {
1306 bool use4 = SImm == 2;
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001307 Tmp2 = SelectExpr(N.getOperand(1).getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001308 if (isSIntImmediateBounded(N.getOperand(0), SImm, 0, 255))
1309 BuildMI(BB, use4?Alpha::S4ADDQi:Alpha::S8ADDQi, 2, Result).addReg(Tmp2).addImm(SImm);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001310 else {
1311 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001312 BuildMI(BB, use4?Alpha::S4ADDQ:Alpha::S8ADDQ, 2, Result).addReg(Tmp2).addReg(Tmp1);
Andrew Lenharthf77f3952005-04-06 20:59:59 +00001313 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001314 }
1315 //small addi
Andrew Lenharthd2284272005-08-15 14:31:37 +00001316 else if(isSIntImmediateBounded(N.getOperand(1), SImm, 0, 255))
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001317 { //Normal imm add/sub
1318 Opc = isAdd ? Alpha::ADDQi : Alpha::SUBQi;
1319 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001320 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(SImm);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001321 }
Andrew Lenharthd2284272005-08-15 14:31:37 +00001322 else if(isSIntImmediateBounded(N.getOperand(1), SImm, -255, 0))
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001323 { //inverted imm add/sub
1324 Opc = isAdd ? Alpha::SUBQi : Alpha::ADDQi;
1325 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharthd2284272005-08-15 14:31:37 +00001326 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addImm(-SImm);
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001327 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001328 //larger addi
Andrew Lenharthd2284272005-08-15 14:31:37 +00001329 else if(isSIntImmediateBounded(N.getOperand(1), SImm, -32767, 32767))
Andrew Lenharth74d00d82005-03-02 17:23:03 +00001330 { //LDA
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001331 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001332 if (!isAdd)
Andrew Lenharthd2284272005-08-15 14:31:37 +00001333 SImm = -SImm;
1334 BuildMI(BB, Alpha::LDA, 2, Result).addImm(SImm).addReg(Tmp1);
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001335 }
1336 //give up and do the operation
1337 else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001338 //Normal add/sub
1339 Opc = isAdd ? Alpha::ADDQ : Alpha::SUBQ;
1340 Tmp1 = SelectExpr(N.getOperand(0));
1341 Tmp2 = SelectExpr(N.getOperand(1));
1342 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1343 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001344 return Result;
1345 }
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001346 case ISD::FADD:
1347 case ISD::FSUB:
1348 case ISD::FMUL:
1349 case ISD::FDIV: {
1350 if (opcode == ISD::FADD)
1351 Opc = DestType == MVT::f64 ? Alpha::ADDT : Alpha::ADDS;
1352 else if (opcode == ISD::FSUB)
1353 Opc = DestType == MVT::f64 ? Alpha::SUBT : Alpha::SUBS;
1354 else if (opcode == ISD::FMUL)
1355 Opc = DestType == MVT::f64 ? Alpha::MULT : Alpha::MULS;
1356 else
1357 Opc = DestType == MVT::f64 ? Alpha::DIVT : Alpha::DIVS;
1358 Tmp1 = SelectExpr(N.getOperand(0));
1359 Tmp2 = SelectExpr(N.getOperand(1));
1360 BuildMI(BB, Opc, 2, Result).addReg(Tmp1).addReg(Tmp2);
1361 return Result;
1362 }
Andrew Lenharthdc0b71b2005-03-22 00:24:07 +00001363 case ISD::SDIV:
Chris Lattner3e2bafd2005-09-28 22:29:17 +00001364 {
Andrew Lenhartha565c272005-04-06 22:03:13 +00001365 //check if we can convert into a shift!
Andrew Lenharthd2284272005-08-15 14:31:37 +00001366 if (isSIntImmediate(N.getOperand(1), SImm) &&
1367 SImm != 0 && isPowerOf2_64(llabs(SImm))) {
1368 unsigned k = Log2_64(llabs(SImm));
Andrew Lenhartha565c272005-04-06 22:03:13 +00001369 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenhartha565c272005-04-06 22:03:13 +00001370 if (k == 1)
1371 Tmp2 = Tmp1;
1372 else
1373 {
1374 Tmp2 = MakeReg(MVT::i64);
1375 BuildMI(BB, Alpha::SRAi, 2, Tmp2).addReg(Tmp1).addImm(k - 1);
1376 }
1377 Tmp3 = MakeReg(MVT::i64);
1378 BuildMI(BB, Alpha::SRLi, 2, Tmp3).addReg(Tmp2).addImm(64-k);
1379 unsigned Tmp4 = MakeReg(MVT::i64);
1380 BuildMI(BB, Alpha::ADDQ, 2, Tmp4).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharthd2284272005-08-15 14:31:37 +00001381 if (SImm > 0)
Andrew Lenhartha565c272005-04-06 22:03:13 +00001382 BuildMI(BB, Alpha::SRAi, 2, Result).addReg(Tmp4).addImm(k);
1383 else
1384 {
1385 unsigned Tmp5 = MakeReg(MVT::i64);
1386 BuildMI(BB, Alpha::SRAi, 2, Tmp5).addReg(Tmp4).addImm(k);
1387 BuildMI(BB, Alpha::SUBQ, 2, Result).addReg(Alpha::R31).addReg(Tmp5);
1388 }
1389 return Result;
1390 }
1391 }
1392 //Else fall through
1393
1394 case ISD::UDIV:
1395 {
Andrew Lenharthd2284272005-08-15 14:31:37 +00001396 if (isSIntImmediate(N.getOperand(1), SImm) && (SImm >= 2 || SImm <= -2))
Andrew Lenhartha565c272005-04-06 22:03:13 +00001397 {
1398 // If this is a divide by constant, we can emit code using some magic
1399 // constants to implement it as a multiply instead.
1400 ExprMap.erase(N);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001401 if (opcode == ISD::SDIV)
Andrew Lenhartha565c272005-04-06 22:03:13 +00001402 return SelectExpr(BuildSDIVSequence(N));
1403 else
1404 return SelectExpr(BuildUDIVSequence(N));
1405 }
Andrew Lenharth4b8ac152005-04-06 20:25:34 +00001406 }
1407 //else fall though
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001408 case ISD::UREM:
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001409 case ISD::SREM: {
1410 const char* opstr = 0;
Andrew Lenharth40831c52005-01-28 06:57:18 +00001411 switch(opcode) {
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001412 case ISD::UREM: opstr = "__remqu"; break;
1413 case ISD::SREM: opstr = "__remq"; break;
1414 case ISD::UDIV: opstr = "__divqu"; break;
1415 case ISD::SDIV: opstr = "__divq"; break;
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001416 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001417 Tmp1 = SelectExpr(N.getOperand(0));
1418 Tmp2 = SelectExpr(N.getOperand(1));
Jeff Cohen00b168892005-07-27 06:12:32 +00001419 SDOperand Addr =
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001420 ISelDAG->getExternalSymbol(opstr, AlphaLowering.getPointerTy());
1421 Tmp3 = SelectExpr(Addr);
Andrew Lenharth33819132005-03-04 20:09:23 +00001422 //set up regs explicitly (helps Reg alloc)
1423 BuildMI(BB, Alpha::BIS, 2, Alpha::R24).addReg(Tmp1).addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001424 BuildMI(BB, Alpha::BIS, 2, Alpha::R25).addReg(Tmp2).addReg(Tmp2);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001425 BuildMI(BB, Alpha::BIS, 2, Alpha::R27).addReg(Tmp3).addReg(Tmp3);
1426 BuildMI(BB, Alpha::JSRs, 2, Alpha::R23).addReg(Alpha::R27).addImm(0);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001427 BuildMI(BB, Alpha::BIS, 2, Result).addReg(Alpha::R27).addReg(Alpha::R27);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001428 return Result;
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001429 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001430
Andrew Lenharthe76797c2005-02-01 20:40:27 +00001431 case ISD::FP_TO_UINT:
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001432 case ISD::FP_TO_SINT:
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001433 {
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001434 assert (DestType == MVT::i64 && "only quads can be loaded to");
1435 MVT::ValueType SrcType = N.getOperand(0).getValueType();
Andrew Lenharth03824012005-02-07 05:55:55 +00001436 assert (SrcType == MVT::f32 || SrcType == MVT::f64);
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001437 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001438 if (SrcType == MVT::f32)
Misha Brukman7847fca2005-04-22 17:54:37 +00001439 {
1440 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth98169be2005-07-28 18:14:47 +00001441 BuildMI(BB, Alpha::CVTST, 1, Tmp2).addReg(Alpha::F31).addReg(Tmp1);
Misha Brukman7847fca2005-04-22 17:54:37 +00001442 Tmp1 = Tmp2;
1443 }
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001444 Tmp2 = MakeReg(MVT::f64);
Andrew Lenharth98169be2005-07-28 18:14:47 +00001445 BuildMI(BB, Alpha::CVTTQ, 1, Tmp2).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharth0eaf6ce2005-04-02 21:06:51 +00001446 MoveFP2Int(Tmp2, Result, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001447
Andrew Lenharth7efadce2005-01-31 01:44:26 +00001448 return Result;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001449 }
Andrew Lenharth3e98fde2005-01-26 21:54:09 +00001450
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001451 case ISD::SELECT:
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001452 if (isFP) {
1453 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1454 unsigned TV = SelectExpr(N.getOperand(1)); //Use if TRUE
1455 unsigned FV = SelectExpr(N.getOperand(2)); //Use if FALSE
1456
1457 SDOperand CC = N.getOperand(0);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001458
Chris Lattner88ac32c2005-08-09 20:21:10 +00001459 if (CC.getOpcode() == ISD::SETCC &&
1460 !MVT::isInteger(CC.getOperand(0).getValueType())) {
1461 //FP Setcc -> Select yay!
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001462
Jeff Cohen00b168892005-07-27 06:12:32 +00001463
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001464 //for a cmp b: c = a - b;
1465 //a = b: c = 0
1466 //a < b: c < 0
1467 //a > b: c > 0
1468
1469 bool invTest = false;
1470 unsigned Tmp3;
1471
1472 ConstantFPSDNode *CN;
Chris Lattner88ac32c2005-08-09 20:21:10 +00001473 if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(1)))
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001474 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
Chris Lattner88ac32c2005-08-09 20:21:10 +00001475 Tmp3 = SelectExpr(CC.getOperand(0));
1476 else if ((CN = dyn_cast<ConstantFPSDNode>(CC.getOperand(0)))
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001477 && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)))
1478 {
Chris Lattner88ac32c2005-08-09 20:21:10 +00001479 Tmp3 = SelectExpr(CC.getOperand(1));
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001480 invTest = true;
1481 }
1482 else
1483 {
Chris Lattner88ac32c2005-08-09 20:21:10 +00001484 unsigned Tmp1 = SelectExpr(CC.getOperand(0));
1485 unsigned Tmp2 = SelectExpr(CC.getOperand(1));
1486 bool isD = CC.getOperand(0).getValueType() == MVT::f64;
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001487 Tmp3 = MakeReg(isD ? MVT::f64 : MVT::f32);
1488 BuildMI(BB, isD ? Alpha::SUBT : Alpha::SUBS, 2, Tmp3)
1489 .addReg(Tmp1).addReg(Tmp2);
1490 }
1491
Chris Lattner88ac32c2005-08-09 20:21:10 +00001492 switch (cast<CondCodeSDNode>(CC.getOperand(2))->get()) {
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001493 default: CC.Val->dump(); assert(0 && "Unknown FP comparison!");
1494 case ISD::SETEQ: Opc = invTest ? Alpha::FCMOVNE : Alpha::FCMOVEQ; break;
1495 case ISD::SETLT: Opc = invTest ? Alpha::FCMOVGT : Alpha::FCMOVLT; break;
1496 case ISD::SETLE: Opc = invTest ? Alpha::FCMOVGE : Alpha::FCMOVLE; break;
1497 case ISD::SETGT: Opc = invTest ? Alpha::FCMOVLT : Alpha::FCMOVGT; break;
1498 case ISD::SETGE: Opc = invTest ? Alpha::FCMOVLE : Alpha::FCMOVGE; break;
1499 case ISD::SETNE: Opc = invTest ? Alpha::FCMOVEQ : Alpha::FCMOVNE; break;
1500 }
1501 BuildMI(BB, Opc, 3, Result).addReg(FV).addReg(TV).addReg(Tmp3);
1502 return Result;
1503 }
1504 else
1505 {
1506 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
1507 BuildMI(BB, Alpha::FCMOVEQ_INT, 3, Result).addReg(TV).addReg(FV)
1508 .addReg(Tmp1);
1509// // Spill the cond to memory and reload it from there.
1510// unsigned Tmp4 = MakeReg(MVT::f64);
1511// MoveIntFP(Tmp1, Tmp4, true);
1512// //now ideally, we don't have to do anything to the flag...
1513// // Get the condition into the zero flag.
1514// BuildMI(BB, Alpha::FCMOVEQ, 3, Result).addReg(TV).addReg(FV).addReg(Tmp4);
1515 return Result;
Jeff Cohen00b168892005-07-27 06:12:32 +00001516 }
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001517 } else {
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001518 //FIXME: look at parent to decide if intCC can be folded, or if setCC(FP)
1519 //and can save stack use
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001520 //Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001521 //Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1522 //Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001523 // Get the condition into the zero flag.
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001524 //BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001525
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001526 SDOperand CC = N.getOperand(0);
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001527
Misha Brukman4633f1c2005-04-21 23:13:11 +00001528 if (CC.getOpcode() == ISD::SETCC &&
Chris Lattner88ac32c2005-08-09 20:21:10 +00001529 !MVT::isInteger(CC.getOperand(0).getValueType()))
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001530 { //FP Setcc -> Int Select
Misha Brukman7847fca2005-04-22 17:54:37 +00001531 Tmp1 = MakeReg(MVT::f64);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001532 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1533 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Misha Brukman7847fca2005-04-22 17:54:37 +00001534 bool inv = SelectFPSetCC(CC, Tmp1);
1535 BuildMI(BB, inv?Alpha::CMOVNE_FP:Alpha::CMOVEQ_FP, 2, Result)
1536 .addReg(Tmp2).addReg(Tmp3).addReg(Tmp1);
1537 return Result;
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001538 }
1539 if (CC.getOpcode() == ISD::SETCC) {
Misha Brukman7847fca2005-04-22 17:54:37 +00001540 //Int SetCC -> Select
1541 //Dropping the CC is only useful if we are comparing to 0
Andrew Lenharthd2284272005-08-15 14:31:37 +00001542 if(isSIntImmediateBounded(CC.getOperand(1), SImm, 0, 0)) {
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001543 //figure out a few things
Andrew Lenharthd2284272005-08-15 14:31:37 +00001544 bool useImm = isSIntImmediateBounded(N.getOperand(2), SImm, 0, 255);
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001545
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001546 //Fix up CC
Chris Lattner88ac32c2005-08-09 20:21:10 +00001547 ISD::CondCode cCode= cast<CondCodeSDNode>(CC.getOperand(2))->get();
Andrew Lenharth694c2982005-06-26 23:01:11 +00001548 if (useImm) //Invert sense to get Imm field right
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001549 cCode = ISD::getSetCCInverse(cCode, true);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001550
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001551 //Choose the CMOV
1552 switch (cCode) {
1553 default: CC.Val->dump(); assert(0 && "Unknown integer comparison!");
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001554 case ISD::SETEQ: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
1555 case ISD::SETLT: Opc = useImm?Alpha::CMOVLTi:Alpha::CMOVLT; break;
1556 case ISD::SETLE: Opc = useImm?Alpha::CMOVLEi:Alpha::CMOVLE; break;
1557 case ISD::SETGT: Opc = useImm?Alpha::CMOVGTi:Alpha::CMOVGT; break;
1558 case ISD::SETGE: Opc = useImm?Alpha::CMOVGEi:Alpha::CMOVGE; break;
1559 case ISD::SETULT: assert(0 && "unsigned < 0 is never true"); break;
1560 case ISD::SETUGT: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
1561 //Technically you could have this CC
1562 case ISD::SETULE: Opc = useImm?Alpha::CMOVEQi:Alpha::CMOVEQ; break;
1563 case ISD::SETUGE: assert(0 && "unsgined >= 0 is always true"); break;
1564 case ISD::SETNE: Opc = useImm?Alpha::CMOVNEi:Alpha::CMOVNE; break;
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001565 }
Chris Lattner88ac32c2005-08-09 20:21:10 +00001566 Tmp1 = SelectExpr(CC.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001567
Andrew Lenharth694c2982005-06-26 23:01:11 +00001568 if (useImm) {
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001569 Tmp3 = SelectExpr(N.getOperand(1)); //Use if FALSE
Andrew Lenharthd2284272005-08-15 14:31:37 +00001570 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addImm(SImm).addReg(Tmp1);
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001571 } else {
1572 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1573 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
1574 BuildMI(BB, Opc, 2, Result).addReg(Tmp3).addReg(Tmp2).addReg(Tmp1);
1575 }
1576 return Result;
1577 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001578 //Otherwise, fall though
Andrew Lenharth10c085b2005-04-02 22:32:39 +00001579 }
1580 Tmp1 = SelectExpr(N.getOperand(0)); //Cond
Andrew Lenharth63b720a2005-04-03 20:35:21 +00001581 Tmp2 = SelectExpr(N.getOperand(1)); //Use if TRUE
1582 Tmp3 = SelectExpr(N.getOperand(2)); //Use if FALSE
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001583 BuildMI(BB, Alpha::CMOVEQ, 2, Result).addReg(Tmp2).addReg(Tmp3)
1584 .addReg(Tmp1);
Misha Brukman4633f1c2005-04-21 23:13:11 +00001585
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001586 return Result;
1587 }
1588
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001589 case ISD::Constant:
1590 {
Andrew Lenharthc0513832005-03-29 19:24:04 +00001591 int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001592 int zero_extend_top = 0;
Andrew Lenharthf075cac2005-07-23 07:46:48 +00001593 if (val > 0 && (val & 0xFFFFFFFF00000000ULL) == 0 &&
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001594 ((int32_t)val < 0)) {
1595 //try a small load and zero extend
1596 val = (int32_t)val;
1597 zero_extend_top = 15;
1598 }
1599
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00001600 if (val <= IMM_HIGH && val >= IMM_LOW) {
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001601 if(!zero_extend_top)
1602 BuildMI(BB, Alpha::LDA, 2, Result).addImm(val).addReg(Alpha::R31);
1603 else {
1604 Tmp1 = MakeReg(MVT::i64);
1605 BuildMI(BB, Alpha::LDA, 2, Tmp1).addImm(val).addReg(Alpha::R31);
1606 BuildMI(BB, Alpha::ZAPNOT, 2, Result).addReg(Tmp1).addImm(zero_extend_top);
1607 }
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00001608 }
Misha Brukman7847fca2005-04-22 17:54:37 +00001609 else if (val <= (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT &&
1610 val >= (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) {
1611 Tmp1 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001612 BuildMI(BB, Alpha::LDAH, 2, Tmp1).addImm(getUpper16(val))
1613 .addReg(Alpha::R31);
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001614 if (!zero_extend_top)
1615 BuildMI(BB, Alpha::LDA, 2, Result).addImm(getLower16(val)).addReg(Tmp1);
1616 else {
1617 Tmp3 = MakeReg(MVT::i64);
1618 BuildMI(BB, Alpha::LDA, 2, Tmp3).addImm(getLower16(val)).addReg(Tmp1);
1619 BuildMI(BB, Alpha::ZAPNOT, 2, Result).addReg(Tmp3).addImm(zero_extend_top);
1620 }
Andrew Lenharthe87f6c32005-03-11 17:48:05 +00001621 }
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001622 else {
Andrew Lenharth6b137d82005-07-22 22:24:01 +00001623 //re-get the val since we are going to mem anyway
1624 val = (int64_t)cast<ConstantSDNode>(N)->getValue();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001625 MachineConstantPool *CP = BB->getParent()->getConstantPool();
Jeff Cohen00b168892005-07-27 06:12:32 +00001626 ConstantUInt *C =
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001627 ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001628 unsigned CPI = CP->getConstantPoolIndex(C);
1629 AlphaLowering.restoreGP(BB);
Andrew Lenharthfe895e32005-06-27 17:15:36 +00001630 has_sym = true;
1631 Tmp1 = MakeReg(MVT::i64);
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001632 BuildMI(BB, Alpha::LDAHr, 2, Tmp1).addConstantPoolIndex(CPI)
1633 .addReg(Alpha::R29);
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001634 if (EnableAlphaLSMark)
1635 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(5).addImm(0).addImm(0)
1636 .addImm(getUID());
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001637 BuildMI(BB, Alpha::LDQr, 2, Result).addConstantPoolIndex(CPI)
1638 .addReg(Tmp1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001639 }
1640 return Result;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001641 }
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001642 case ISD::FNEG:
1643 if(ISD::FABS == N.getOperand(0).getOpcode())
1644 {
1645 Tmp1 = SelectExpr(N.getOperand(0).getOperand(0));
1646 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
1647 } else {
1648 Tmp1 = SelectExpr(N.getOperand(0));
1649 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Tmp1).addReg(Tmp1);
1650 }
1651 return Result;
1652
1653 case ISD::FABS:
1654 Tmp1 = SelectExpr(N.getOperand(0));
1655 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31).addReg(Tmp1);
1656 return Result;
1657
1658 case ISD::FP_ROUND:
1659 assert (DestType == MVT::f32 &&
1660 N.getOperand(0).getValueType() == MVT::f64 &&
1661 "only f64 to f32 conversion supported here");
1662 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth98169be2005-07-28 18:14:47 +00001663 BuildMI(BB, Alpha::CVTTS, 1, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001664 return Result;
1665
1666 case ISD::FP_EXTEND:
1667 assert (DestType == MVT::f64 &&
1668 N.getOperand(0).getValueType() == MVT::f32 &&
1669 "only f32 to f64 conversion supported here");
1670 Tmp1 = SelectExpr(N.getOperand(0));
Andrew Lenharth98169be2005-07-28 18:14:47 +00001671 BuildMI(BB, Alpha::CVTST, 1, Result).addReg(Alpha::F31).addReg(Tmp1);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001672 return Result;
1673
1674 case ISD::ConstantFP:
1675 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) {
1676 if (CN->isExactlyValue(+0.0)) {
1677 BuildMI(BB, Alpha::CPYS, 2, Result).addReg(Alpha::F31)
1678 .addReg(Alpha::F31);
1679 } else if ( CN->isExactlyValue(-0.0)) {
1680 BuildMI(BB, Alpha::CPYSN, 2, Result).addReg(Alpha::F31)
1681 .addReg(Alpha::F31);
1682 } else {
1683 abort();
1684 }
1685 }
1686 return Result;
1687
1688 case ISD::SINT_TO_FP:
1689 {
1690 assert (N.getOperand(0).getValueType() == MVT::i64
1691 && "only quads can be loaded from");
1692 Tmp1 = SelectExpr(N.getOperand(0)); // Get the operand register
1693 Tmp2 = MakeReg(MVT::f64);
1694 MoveInt2FP(Tmp1, Tmp2, true);
1695 Opc = DestType == MVT::f64 ? Alpha::CVTQT : Alpha::CVTQS;
Andrew Lenharth98169be2005-07-28 18:14:47 +00001696 BuildMI(BB, Opc, 1, Result).addReg(Alpha::F31).addReg(Tmp2);
Andrew Lenharthf4da9452005-06-29 12:49:51 +00001697 return Result;
1698 }
Andrew Lenharthf71df332005-09-04 06:12:19 +00001699
1700 case ISD::AssertSext:
1701 case ISD::AssertZext:
1702 return SelectExpr(N.getOperand(0));
1703
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001704 }
1705
1706 return 0;
1707}
1708
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001709void AlphaISel::Select(SDOperand N) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001710 unsigned Tmp1, Tmp2, Opc;
Andrew Lenharth760270d2005-02-07 23:02:23 +00001711 unsigned opcode = N.getOpcode();
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001712
Nate Begeman85fdeb22005-03-24 04:39:54 +00001713 if (!ExprMap.insert(std::make_pair(N, notIn)).second)
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001714 return; // Already selected.
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001715
1716 SDNode *Node = N.Val;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001717
Andrew Lenharth760270d2005-02-07 23:02:23 +00001718 switch (opcode) {
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001719
1720 default:
1721 Node->dump(); std::cerr << "\n";
1722 assert(0 && "Node not handled yet!");
1723
1724 case ISD::BRCOND: {
Andrew Lenharth445171a2005-02-08 00:40:03 +00001725 SelectBranchCC(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001726 return;
1727 }
1728
1729 case ISD::BR: {
1730 MachineBasicBlock *Dest =
1731 cast<BasicBlockSDNode>(N.getOperand(1))->getBasicBlock();
1732
1733 Select(N.getOperand(0));
1734 BuildMI(BB, Alpha::BR, 1, Alpha::R31).addMBB(Dest);
1735 return;
1736 }
1737
1738 case ISD::ImplicitDef:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001739 ++count_ins;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001740 Select(N.getOperand(0));
Chris Lattner707ebc52005-08-16 21:56:37 +00001741 BuildMI(BB, Alpha::IDEF, 0,
1742 cast<RegisterSDNode>(N.getOperand(1))->getReg());
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001743 return;
Misha Brukman4633f1c2005-04-21 23:13:11 +00001744
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001745 case ISD::EntryToken: return; // Noop
1746
1747 case ISD::TokenFactor:
1748 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i)
1749 Select(Node->getOperand(i));
Misha Brukman4633f1c2005-04-21 23:13:11 +00001750
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001751 //N.Val->dump(); std::cerr << "\n";
1752 //assert(0 && "Node not handled yet!");
Misha Brukman4633f1c2005-04-21 23:13:11 +00001753
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001754 return;
1755
1756 case ISD::CopyToReg:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001757 ++count_outs;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001758 Select(N.getOperand(0));
Chris Lattner707ebc52005-08-16 21:56:37 +00001759 Tmp1 = SelectExpr(N.getOperand(2));
1760 Tmp2 = cast<RegisterSDNode>(N.getOperand(1))->getReg();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001761
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001762 if (Tmp1 != Tmp2) {
Chris Lattner707ebc52005-08-16 21:56:37 +00001763 if (N.getOperand(2).getValueType() == MVT::f64 ||
1764 N.getOperand(2).getValueType() == MVT::f32)
Andrew Lenharth29219162005-02-07 06:31:44 +00001765 BuildMI(BB, Alpha::CPYS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
1766 else
1767 BuildMI(BB, Alpha::BIS, 2, Tmp2).addReg(Tmp1).addReg(Tmp1);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001768 }
1769 return;
1770
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001771 case ISD::RET:
Andrew Lenhartha32b9e32005-04-08 17:28:49 +00001772 ++count_outs;
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001773 switch (N.getNumOperands()) {
1774 default:
1775 std::cerr << N.getNumOperands() << "\n";
1776 for (unsigned i = 0; i < N.getNumOperands(); ++i)
1777 std::cerr << N.getOperand(i).getValueType() << "\n";
1778 Node->dump();
1779 assert(0 && "Unknown return instruction!");
1780 case 2:
1781 Select(N.getOperand(0));
1782 Tmp1 = SelectExpr(N.getOperand(1));
1783 switch (N.getOperand(1).getValueType()) {
Misha Brukman4633f1c2005-04-21 23:13:11 +00001784 default: Node->dump();
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001785 assert(0 && "All other types should have been promoted!!");
1786 case MVT::f64:
1787 case MVT::f32:
1788 BuildMI(BB, Alpha::CPYS, 2, Alpha::F0).addReg(Tmp1).addReg(Tmp1);
1789 break;
1790 case MVT::i32:
1791 case MVT::i64:
1792 BuildMI(BB, Alpha::BIS, 2, Alpha::R0).addReg(Tmp1).addReg(Tmp1);
1793 break;
1794 }
1795 break;
1796 case 1:
1797 Select(N.getOperand(0));
1798 break;
1799 }
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001800 // Just emit a 'ret' instruction
Andrew Lenharth6968bff2005-06-27 23:24:11 +00001801 AlphaLowering.restoreRA(BB);
Andrew Lenharthf3f951a2005-07-22 20:50:29 +00001802 BuildMI(BB, Alpha::RET, 2, Alpha::R31).addReg(Alpha::R26).addImm(1);
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001803 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001804
Misha Brukman4633f1c2005-04-21 23:13:11 +00001805 case ISD::TRUNCSTORE:
1806 case ISD::STORE:
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001807 {
Andrew Lenharth9e8d1092005-02-06 15:40:40 +00001808 SDOperand Chain = N.getOperand(0);
1809 SDOperand Value = N.getOperand(1);
1810 SDOperand Address = N.getOperand(2);
1811 Select(Chain);
1812
1813 Tmp1 = SelectExpr(Value); //value
Andrew Lenharth760270d2005-02-07 23:02:23 +00001814
1815 if (opcode == ISD::STORE) {
1816 switch(Value.getValueType()) {
1817 default: assert(0 && "unknown Type in store");
1818 case MVT::i64: Opc = Alpha::STQ; break;
1819 case MVT::f64: Opc = Alpha::STT; break;
1820 case MVT::f32: Opc = Alpha::STS; break;
1821 }
1822 } else { //ISD::TRUNCSTORE
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001823 switch(cast<VTSDNode>(Node->getOperand(4))->getVT()) {
Andrew Lenharth760270d2005-02-07 23:02:23 +00001824 default: assert(0 && "unknown Type in store");
1825 case MVT::i1: //FIXME: DAG does not promote this load
1826 case MVT::i8: Opc = Alpha::STB; break;
1827 case MVT::i16: Opc = Alpha::STW; break;
1828 case MVT::i32: Opc = Alpha::STL; break;
1829 }
Andrew Lenharth65838902005-02-06 16:22:15 +00001830 }
Andrew Lenharth760270d2005-02-07 23:02:23 +00001831
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001832 int i, j, k;
Jeff Cohen00b168892005-07-27 06:12:32 +00001833 if (EnableAlphaLSMark)
1834 getValueInfo(cast<SrcValueSDNode>(N.getOperand(3))->getValue(),
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001835 i, j, k);
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001836
Andrew Lenharthcf8bf382005-07-01 19:12:13 +00001837 GlobalAddressSDNode *GASD = dyn_cast<GlobalAddressSDNode>(Address);
1838 if (GASD && !GASD->getGlobal()->isExternal()) {
1839 Tmp2 = MakeReg(MVT::i64);
1840 AlphaLowering.restoreGP(BB);
1841 BuildMI(BB, Alpha::LDAHr, 2, Tmp2)
1842 .addGlobalAddress(GASD->getGlobal()).addReg(Alpha::R29);
1843 if (EnableAlphaLSMark)
1844 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1845 .addImm(getUID());
1846 BuildMI(BB, GetRelVersion(Opc), 3).addReg(Tmp1)
1847 .addGlobalAddress(GASD->getGlobal()).addReg(Tmp2);
Andrew Lenharthfce587e2005-06-29 00:39:17 +00001848 } else if(Address.getOpcode() == ISD::FrameIndex) {
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001849 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001850 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1851 .addImm(getUID());
Andrew Lenharth032f2352005-02-22 21:59:48 +00001852 BuildMI(BB, Opc, 3).addReg(Tmp1)
1853 .addFrameIndex(cast<FrameIndexSDNode>(Address)->getIndex())
1854 .addReg(Alpha::F31);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001855 } else {
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001856 long offset;
1857 SelectAddr(Address, Tmp2, offset);
Andrew Lenharthc7989ce2005-06-29 00:31:08 +00001858 if (EnableAlphaLSMark)
Andrew Lenharth06ef8842005-06-29 18:54:02 +00001859 BuildMI(BB, Alpha::MEMLABEL, 4).addImm(i).addImm(j).addImm(k)
1860 .addImm(getUID());
Andrew Lenharth63f2ab22005-02-10 06:25:22 +00001861 BuildMI(BB, Opc, 3).addReg(Tmp1).addImm(offset).addReg(Tmp2);
1862 }
Andrew Lenharthb014d3e2005-02-02 17:32:39 +00001863 return;
1864 }
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001865
1866 case ISD::EXTLOAD:
1867 case ISD::SEXTLOAD:
1868 case ISD::ZEXTLOAD:
1869 case ISD::LOAD:
1870 case ISD::CopyFromReg:
Chris Lattnerb5d8e6e2005-05-13 20:29:26 +00001871 case ISD::TAILCALL:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001872 case ISD::CALL:
Andrew Lenharth032f2352005-02-22 21:59:48 +00001873 case ISD::DYNAMIC_STACKALLOC:
Andrew Lenharth6b9870a2005-01-28 14:06:46 +00001874 ExprMap.erase(N);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001875 SelectExpr(N);
1876 return;
1877
Chris Lattner16cd04d2005-05-12 23:24:06 +00001878 case ISD::CALLSEQ_START:
1879 case ISD::CALLSEQ_END:
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001880 Select(N.getOperand(0));
1881 Tmp1 = cast<ConstantSDNode>(N.getOperand(1))->getValue();
Misha Brukman4633f1c2005-04-21 23:13:11 +00001882
Chris Lattner16cd04d2005-05-12 23:24:06 +00001883 Opc = N.getOpcode() == ISD::CALLSEQ_START ? Alpha::ADJUSTSTACKDOWN :
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001884 Alpha::ADJUSTSTACKUP;
1885 BuildMI(BB, Opc, 1).addImm(Tmp1);
1886 return;
Andrew Lenharth95762122005-03-31 21:24:06 +00001887
1888 case ISD::PCMARKER:
1889 Select(N.getOperand(0)); //Chain
Andrew Lenharthd4653b12005-06-27 17:39:17 +00001890 BuildMI(BB, Alpha::PCLABEL, 2)
1891 .addImm( cast<ConstantSDNode>(N.getOperand(1))->getValue());
Andrew Lenharth95762122005-03-31 21:24:06 +00001892 return;
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001893 }
1894 assert(0 && "Should not be reached!");
1895}
1896
1897
1898/// createAlphaPatternInstructionSelector - This pass converts an LLVM function
1899/// into a machine code representation using pattern matching and a machine
1900/// description file.
1901///
1902FunctionPass *llvm::createAlphaPatternInstructionSelector(TargetMachine &TM) {
Andrew Lenharthb69f3422005-06-22 17:19:45 +00001903 return new AlphaISel(TM);
Andrew Lenharth304d0f32005-01-22 23:41:55 +00001904}
Andrew Lenharth4f7cba52005-04-13 05:19:55 +00001905