blob: e0ae5564ed81841ab1eca7e647732190a61d9fc5 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- AlphaISelDAGToDAG.cpp - Alpha pattern matching inst selector ------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for Alpha,
11// converting from a legalized dag to a Alpha dag.
12//
13//===----------------------------------------------------------------------===//
14
15#include "Alpha.h"
16#include "AlphaTargetMachine.h"
17#include "AlphaISelLowering.h"
18#include "llvm/CodeGen/MachineInstrBuilder.h"
19#include "llvm/CodeGen/MachineFrameInfo.h"
20#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner1b989192007-12-31 04:13:23 +000021#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SelectionDAGISel.h"
24#include "llvm/Target/TargetOptions.h"
25#include "llvm/Constants.h"
26#include "llvm/DerivedTypes.h"
27#include "llvm/GlobalValue.h"
28#include "llvm/Intrinsics.h"
Chris Lattner93c741a2008-02-03 05:43:57 +000029#include "llvm/Support/Compiler.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030#include "llvm/Support/Debug.h"
31#include "llvm/Support/MathExtras.h"
32#include <algorithm>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033using namespace llvm;
34
35namespace {
36
37 //===--------------------------------------------------------------------===//
38 /// AlphaDAGToDAGISel - Alpha specific code to select Alpha machine
39 /// instructions for SelectionDAG operations.
40 class AlphaDAGToDAGISel : public SelectionDAGISel {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041 static const int64_t IMM_LOW = -32768;
42 static const int64_t IMM_HIGH = 32767;
43 static const int64_t IMM_MULT = 65536;
44 static const int64_t IMM_FULLHIGH = IMM_HIGH + IMM_HIGH * IMM_MULT;
45 static const int64_t IMM_FULLLOW = IMM_LOW + IMM_LOW * IMM_MULT;
46
47 static int64_t get_ldah16(int64_t x) {
48 int64_t y = x / IMM_MULT;
49 if (x % IMM_MULT > IMM_HIGH)
50 ++y;
51 return y;
52 }
53
54 static int64_t get_lda16(int64_t x) {
55 return x - get_ldah16(x) * IMM_MULT;
56 }
57
58 /// get_zapImm - Return a zap mask if X is a valid immediate for a zapnot
59 /// instruction (if not, return 0). Note that this code accepts partial
60 /// zap masks. For example (and LHS, 1) is a valid zap, as long we know
61 /// that the bits 1-7 of LHS are already zero. If LHS is non-null, we are
62 /// in checking mode. If LHS is null, we assume that the mask has already
63 /// been validated before.
Dan Gohman8181bd12008-07-27 21:46:04 +000064 uint64_t get_zapImm(SDValue LHS, uint64_t Constant) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065 uint64_t BitsToCheck = 0;
66 unsigned Result = 0;
67 for (unsigned i = 0; i != 8; ++i) {
68 if (((Constant >> 8*i) & 0xFF) == 0) {
69 // nothing to do.
70 } else {
71 Result |= 1 << i;
72 if (((Constant >> 8*i) & 0xFF) == 0xFF) {
73 // If the entire byte is set, zapnot the byte.
Gabor Greif1c80d112008-08-28 21:40:38 +000074 } else if (LHS.getNode() == 0) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075 // Otherwise, if the mask was previously validated, we know its okay
76 // to zapnot this entire byte even though all the bits aren't set.
77 } else {
78 // Otherwise we don't know that the it's okay to zapnot this entire
79 // byte. Only do this iff we can prove that the missing bits are
80 // already null, so the bytezap doesn't need to really null them.
81 BitsToCheck |= ~Constant & (0xFF << 8*i);
82 }
83 }
84 }
85
86 // If there are missing bits in a byte (for example, X & 0xEF00), check to
87 // see if the missing bits (0x1000) are already known zero if not, the zap
88 // isn't okay to do, as it won't clear all the required bits.
89 if (BitsToCheck &&
Dan Gohman07961cd2008-02-25 21:11:39 +000090 !CurDAG->MaskedValueIsZero(LHS,
91 APInt(LHS.getValueSizeInBits(),
92 BitsToCheck)))
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093 return 0;
94
95 return Result;
96 }
97
98 static uint64_t get_zapImm(uint64_t x) {
99 unsigned build = 0;
100 for(int i = 0; i != 8; ++i) {
101 if ((x & 0x00FF) == 0x00FF)
102 build |= 1 << i;
103 else if ((x & 0x00FF) != 0)
104 return 0;
105 x >>= 8;
106 }
107 return build;
108 }
109
110
111 static uint64_t getNearPower2(uint64_t x) {
112 if (!x) return 0;
113 unsigned at = CountLeadingZeros_64(x);
114 uint64_t complow = 1 << (63 - at);
115 uint64_t comphigh = 1 << (64 - at);
116 //cerr << x << ":" << complow << ":" << comphigh << "\n";
117 if (abs(complow - x) <= abs(comphigh - x))
118 return complow;
119 else
120 return comphigh;
121 }
122
123 static bool chkRemNearPower2(uint64_t x, uint64_t r, bool swap) {
124 uint64_t y = getNearPower2(x);
125 if (swap)
126 return (y - x) == r;
127 else
128 return (x - y) == r;
129 }
130
Dan Gohman8181bd12008-07-27 21:46:04 +0000131 static bool isFPZ(SDValue N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000132 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N);
Dale Johannesendf8a8312007-08-31 04:03:46 +0000133 return (CN && (CN->getValueAPF().isZero()));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000134 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000135 static bool isFPZn(SDValue N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000136 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N);
Dale Johannesendf8a8312007-08-31 04:03:46 +0000137 return (CN && CN->getValueAPF().isNegZero());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138 }
Dan Gohman8181bd12008-07-27 21:46:04 +0000139 static bool isFPZp(SDValue N) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N);
Dale Johannesendf8a8312007-08-31 04:03:46 +0000141 return (CN && CN->getValueAPF().isPosZero());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142 }
143
144 public:
Dan Gohmanb41dfba2008-05-14 01:58:56 +0000145 explicit AlphaDAGToDAGISel(AlphaTargetMachine &TM)
Dan Gohmanf2b29572008-10-03 16:55:19 +0000146 : SelectionDAGISel(*TM.getTargetLowering())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147 {}
148
149 /// getI64Imm - Return a target constant with the specified value, of type
150 /// i64.
Dan Gohman8181bd12008-07-27 21:46:04 +0000151 inline SDValue getI64Imm(int64_t Imm) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000152 return CurDAG->getTargetConstant(Imm, MVT::i64);
153 }
154
155 // Select - Convert the specified operand from a target-independent to a
156 // target-specific node if it hasn't already been changed.
Dan Gohman8181bd12008-07-27 21:46:04 +0000157 SDNode *Select(SDValue Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158
Evan Cheng34fd4f32008-06-30 20:45:06 +0000159 /// InstructionSelect - This callback is invoked by
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohman14a66442008-08-23 02:25:05 +0000161 virtual void InstructionSelect();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162
163 virtual const char *getPassName() const {
164 return "Alpha DAG->DAG Pattern Instruction Selection";
165 }
166
167 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
168 /// inline asm expressions.
Dan Gohman8181bd12008-07-27 21:46:04 +0000169 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 char ConstraintCode,
Dan Gohman14a66442008-08-23 02:25:05 +0000171 std::vector<SDValue> &OutOps) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000172 SDValue Op0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 switch (ConstraintCode) {
174 default: return true;
175 case 'm': // memory
176 Op0 = Op;
177 AddToISelQueue(Op0);
178 break;
179 }
180
181 OutOps.push_back(Op0);
182 return false;
183 }
184
185// Include the pieces autogenerated from the target description.
186#include "AlphaGenDAGISel.inc"
187
188private:
Dan Gohman8181bd12008-07-27 21:46:04 +0000189 SDValue getGlobalBaseReg();
190 SDValue getGlobalRetAddr();
191 void SelectCALL(SDValue Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000192
193 };
194}
195
196/// getGlobalBaseReg - Output the instructions required to put the
197/// GOT address into a register.
198///
Dan Gohman8181bd12008-07-27 21:46:04 +0000199SDValue AlphaDAGToDAGISel::getGlobalBaseReg() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200 unsigned GP = 0;
Chris Lattner1b989192007-12-31 04:13:23 +0000201 for(MachineRegisterInfo::livein_iterator ii = RegInfo->livein_begin(),
202 ee = RegInfo->livein_end(); ii != ee; ++ii)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000203 if (ii->first == Alpha::R29) {
204 GP = ii->second;
205 break;
206 }
207 assert(GP && "GOT PTR not in liveins");
208 return CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
209 GP, MVT::i64);
210}
211
212/// getRASaveReg - Grab the return address
213///
Dan Gohman8181bd12008-07-27 21:46:04 +0000214SDValue AlphaDAGToDAGISel::getGlobalRetAddr() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000215 unsigned RA = 0;
Chris Lattner1b989192007-12-31 04:13:23 +0000216 for(MachineRegisterInfo::livein_iterator ii = RegInfo->livein_begin(),
217 ee = RegInfo->livein_end(); ii != ee; ++ii)
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000218 if (ii->first == Alpha::R26) {
219 RA = ii->second;
220 break;
221 }
222 assert(RA && "RA PTR not in liveins");
223 return CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
224 RA, MVT::i64);
225}
226
Evan Cheng34fd4f32008-06-30 20:45:06 +0000227/// InstructionSelect - This callback is invoked by
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000228/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohman14a66442008-08-23 02:25:05 +0000229void AlphaDAGToDAGISel::InstructionSelect() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000230 DEBUG(BB->dump());
231
232 // Select target instructions for the DAG.
David Greene932618b2008-10-27 21:56:29 +0000233 SelectRoot(*CurDAG);
Dan Gohman14a66442008-08-23 02:25:05 +0000234 CurDAG->RemoveDeadNodes();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000235}
236
237// Select - Convert the specified operand from a target-independent to a
238// target-specific node if it hasn't already been changed.
Dan Gohman8181bd12008-07-27 21:46:04 +0000239SDNode *AlphaDAGToDAGISel::Select(SDValue Op) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000240 SDNode *N = Op.getNode();
Dan Gohmanbd68c792008-07-17 19:10:17 +0000241 if (N->isMachineOpcode()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000242 return NULL; // Already selected.
243 }
244
245 switch (N->getOpcode()) {
246 default: break;
247 case AlphaISD::CALL:
248 SelectCALL(Op);
249 return NULL;
250
251 case ISD::FrameIndex: {
252 int FI = cast<FrameIndexSDNode>(N)->getIndex();
253 return CurDAG->SelectNodeTo(N, Alpha::LDA, MVT::i64,
254 CurDAG->getTargetFrameIndex(FI, MVT::i32),
255 getI64Imm(0));
256 }
257 case ISD::GLOBAL_OFFSET_TABLE: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000258 SDValue Result = getGlobalBaseReg();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000259 ReplaceUses(Op, Result);
260 return NULL;
261 }
262 case AlphaISD::GlobalRetAddr: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000263 SDValue Result = getGlobalRetAddr();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000264 ReplaceUses(Op, Result);
265 return NULL;
266 }
267
268 case AlphaISD::DivCall: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000269 SDValue Chain = CurDAG->getEntryNode();
270 SDValue N0 = Op.getOperand(0);
271 SDValue N1 = Op.getOperand(1);
272 SDValue N2 = Op.getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000273 AddToISelQueue(N0);
274 AddToISelQueue(N1);
275 AddToISelQueue(N2);
276 Chain = CurDAG->getCopyToReg(Chain, Alpha::R24, N1,
Dan Gohman8181bd12008-07-27 21:46:04 +0000277 SDValue(0,0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000278 Chain = CurDAG->getCopyToReg(Chain, Alpha::R25, N2,
279 Chain.getValue(1));
280 Chain = CurDAG->getCopyToReg(Chain, Alpha::R27, N0,
281 Chain.getValue(1));
282 SDNode *CNode =
283 CurDAG->getTargetNode(Alpha::JSRs, MVT::Other, MVT::Flag,
284 Chain, Chain.getValue(1));
285 Chain = CurDAG->getCopyFromReg(Chain, Alpha::R27, MVT::i64,
Dan Gohman8181bd12008-07-27 21:46:04 +0000286 SDValue(CNode, 1));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287 return CurDAG->SelectNodeTo(N, Alpha::BISr, MVT::i64, Chain, Chain);
288 }
289
290 case ISD::READCYCLECOUNTER: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000291 SDValue Chain = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000292 AddToISelQueue(Chain); //Select chain
293 return CurDAG->getTargetNode(Alpha::RPCC, MVT::i64, MVT::Other,
294 Chain);
295 }
296
297 case ISD::Constant: {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000298 uint64_t uval = cast<ConstantSDNode>(N)->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000299
300 if (uval == 0) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000301 SDValue Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000302 Alpha::R31, MVT::i64);
303 ReplaceUses(Op, Result);
304 return NULL;
305 }
306
307 int64_t val = (int64_t)uval;
308 int32_t val32 = (int32_t)val;
309 if (val <= IMM_HIGH + IMM_HIGH * IMM_MULT &&
310 val >= IMM_LOW + IMM_LOW * IMM_MULT)
311 break; //(LDAH (LDA))
312 if ((uval >> 32) == 0 && //empty upper bits
313 val32 <= IMM_HIGH + IMM_HIGH * IMM_MULT)
314 // val32 >= IMM_LOW + IMM_LOW * IMM_MULT) //always true
315 break; //(zext (LDAH (LDA)))
316 //Else use the constant pool
317 ConstantInt *C = ConstantInt::get(Type::Int64Ty, uval);
Dan Gohman8181bd12008-07-27 21:46:04 +0000318 SDValue CPI = CurDAG->getTargetConstantPool(C, MVT::i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000319 SDNode *Tmp = CurDAG->getTargetNode(Alpha::LDAHr, MVT::i64, CPI,
320 getGlobalBaseReg());
321 return CurDAG->SelectNodeTo(N, Alpha::LDQr, MVT::i64, MVT::Other,
Dan Gohman8181bd12008-07-27 21:46:04 +0000322 CPI, SDValue(Tmp, 0), CurDAG->getEntryNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000323 }
Andrew Lenharthc69be952008-10-07 02:10:26 +0000324 case ISD::TargetConstantFP:
325 case ISD::ConstantFP: {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000326 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
327 bool isDouble = N->getValueType(0) == MVT::f64;
Duncan Sands92c43912008-06-06 12:08:01 +0000328 MVT T = isDouble ? MVT::f64 : MVT::f32;
Dale Johannesendf8a8312007-08-31 04:03:46 +0000329 if (CN->getValueAPF().isPosZero()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000330 return CurDAG->SelectNodeTo(N, isDouble ? Alpha::CPYST : Alpha::CPYSS,
331 T, CurDAG->getRegister(Alpha::F31, T),
332 CurDAG->getRegister(Alpha::F31, T));
Dale Johannesendf8a8312007-08-31 04:03:46 +0000333 } else if (CN->getValueAPF().isNegZero()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000334 return CurDAG->SelectNodeTo(N, isDouble ? Alpha::CPYSNT : Alpha::CPYSNS,
335 T, CurDAG->getRegister(Alpha::F31, T),
336 CurDAG->getRegister(Alpha::F31, T));
337 } else {
338 abort();
339 }
340 break;
341 }
342
343 case ISD::SETCC:
Gabor Greif1c80d112008-08-28 21:40:38 +0000344 if (N->getOperand(0).getNode()->getValueType(0).isFloatingPoint()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000345 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
346
347 unsigned Opc = Alpha::WTF;
348 bool rev = false;
349 bool inv = false;
350 switch(CC) {
351 default: DEBUG(N->dump(CurDAG)); assert(0 && "Unknown FP comparison!");
352 case ISD::SETEQ: case ISD::SETOEQ: case ISD::SETUEQ:
353 Opc = Alpha::CMPTEQ; break;
354 case ISD::SETLT: case ISD::SETOLT: case ISD::SETULT:
355 Opc = Alpha::CMPTLT; break;
356 case ISD::SETLE: case ISD::SETOLE: case ISD::SETULE:
357 Opc = Alpha::CMPTLE; break;
358 case ISD::SETGT: case ISD::SETOGT: case ISD::SETUGT:
359 Opc = Alpha::CMPTLT; rev = true; break;
360 case ISD::SETGE: case ISD::SETOGE: case ISD::SETUGE:
361 Opc = Alpha::CMPTLE; rev = true; break;
362 case ISD::SETNE: case ISD::SETONE: case ISD::SETUNE:
363 Opc = Alpha::CMPTEQ; inv = true; break;
364 case ISD::SETO:
365 Opc = Alpha::CMPTUN; inv = true; break;
366 case ISD::SETUO:
367 Opc = Alpha::CMPTUN; break;
368 };
Dan Gohman8181bd12008-07-27 21:46:04 +0000369 SDValue tmp1 = N->getOperand(rev?1:0);
370 SDValue tmp2 = N->getOperand(rev?0:1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000371 AddToISelQueue(tmp1);
372 AddToISelQueue(tmp2);
373 SDNode *cmp = CurDAG->getTargetNode(Opc, MVT::f64, tmp1, tmp2);
374 if (inv)
Dan Gohman8181bd12008-07-27 21:46:04 +0000375 cmp = CurDAG->getTargetNode(Alpha::CMPTEQ, MVT::f64, SDValue(cmp, 0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000376 CurDAG->getRegister(Alpha::F31, MVT::f64));
377 switch(CC) {
378 case ISD::SETUEQ: case ISD::SETULT: case ISD::SETULE:
379 case ISD::SETUNE: case ISD::SETUGT: case ISD::SETUGE:
380 {
381 SDNode* cmp2 = CurDAG->getTargetNode(Alpha::CMPTUN, MVT::f64,
382 tmp1, tmp2);
383 cmp = CurDAG->getTargetNode(Alpha::ADDT, MVT::f64,
Dan Gohman8181bd12008-07-27 21:46:04 +0000384 SDValue(cmp2, 0), SDValue(cmp, 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000385 break;
386 }
387 default: break;
388 }
389
Dan Gohman8181bd12008-07-27 21:46:04 +0000390 SDNode* LD = CurDAG->getTargetNode(Alpha::FTOIT, MVT::i64, SDValue(cmp, 0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000391 return CurDAG->getTargetNode(Alpha::CMPULT, MVT::i64,
392 CurDAG->getRegister(Alpha::R31, MVT::i64),
Dan Gohman8181bd12008-07-27 21:46:04 +0000393 SDValue(LD,0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000394 }
395 break;
396
397 case ISD::SELECT:
Duncan Sands92c43912008-06-06 12:08:01 +0000398 if (N->getValueType(0).isFloatingPoint() &&
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000399 (N->getOperand(0).getOpcode() != ISD::SETCC ||
Duncan Sands92c43912008-06-06 12:08:01 +0000400 !N->getOperand(0).getOperand(1).getValueType().isFloatingPoint())) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000401 //This should be the condition not covered by the Patterns
402 //FIXME: Don't have SelectCode die, but rather return something testable
403 // so that things like this can be caught in fall though code
404 //move int to fp
405 bool isDouble = N->getValueType(0) == MVT::f64;
Dan Gohman8181bd12008-07-27 21:46:04 +0000406 SDValue cond = N->getOperand(0);
407 SDValue TV = N->getOperand(1);
408 SDValue FV = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000409 AddToISelQueue(cond);
410 AddToISelQueue(TV);
411 AddToISelQueue(FV);
412
413 SDNode* LD = CurDAG->getTargetNode(Alpha::ITOFT, MVT::f64, cond);
414 return CurDAG->getTargetNode(isDouble?Alpha::FCMOVNET:Alpha::FCMOVNES,
Dan Gohman8181bd12008-07-27 21:46:04 +0000415 MVT::f64, FV, TV, SDValue(LD,0));
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000416 }
417 break;
418
419 case ISD::AND: {
420 ConstantSDNode* SC = NULL;
421 ConstantSDNode* MC = NULL;
422 if (N->getOperand(0).getOpcode() == ISD::SRL &&
423 (MC = dyn_cast<ConstantSDNode>(N->getOperand(1))) &&
424 (SC = dyn_cast<ConstantSDNode>(N->getOperand(0).getOperand(1)))) {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000425 uint64_t sval = SC->getZExtValue();
426 uint64_t mval = MC->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000427 // If the result is a zap, let the autogened stuff handle it.
428 if (get_zapImm(N->getOperand(0), mval))
429 break;
430 // given mask X, and shift S, we want to see if there is any zap in the
431 // mask if we play around with the botton S bits
432 uint64_t dontcare = (~0ULL) >> (64 - sval);
433 uint64_t mask = mval << sval;
434
435 if (get_zapImm(mask | dontcare))
436 mask = mask | dontcare;
437
438 if (get_zapImm(mask)) {
439 AddToISelQueue(N->getOperand(0).getOperand(0));
Dan Gohman8181bd12008-07-27 21:46:04 +0000440 SDValue Z =
441 SDValue(CurDAG->getTargetNode(Alpha::ZAPNOTi, MVT::i64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000442 N->getOperand(0).getOperand(0),
443 getI64Imm(get_zapImm(mask))), 0);
444 return CurDAG->getTargetNode(Alpha::SRLr, MVT::i64, Z,
445 getI64Imm(sval));
446 }
447 }
448 break;
449 }
450
451 }
452
453 return SelectCode(Op);
454}
455
Dan Gohman8181bd12008-07-27 21:46:04 +0000456void AlphaDAGToDAGISel::SelectCALL(SDValue Op) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000457 //TODO: add flag stuff to prevent nondeturministic breakage!
458
Gabor Greif1c80d112008-08-28 21:40:38 +0000459 SDNode *N = Op.getNode();
Dan Gohman8181bd12008-07-27 21:46:04 +0000460 SDValue Chain = N->getOperand(0);
461 SDValue Addr = N->getOperand(1);
462 SDValue InFlag(0,0); // Null incoming flag value.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000463 AddToISelQueue(Chain);
464
Dan Gohman8181bd12008-07-27 21:46:04 +0000465 std::vector<SDValue> CallOperands;
Duncan Sands92c43912008-06-06 12:08:01 +0000466 std::vector<MVT> TypeOperands;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000467
468 //grab the arguments
469 for(int i = 2, e = N->getNumOperands(); i < e; ++i) {
470 TypeOperands.push_back(N->getOperand(i).getValueType());
471 AddToISelQueue(N->getOperand(i));
472 CallOperands.push_back(N->getOperand(i));
473 }
474 int count = N->getNumOperands() - 2;
475
476 static const unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
477 Alpha::R19, Alpha::R20, Alpha::R21};
478 static const unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
479 Alpha::F19, Alpha::F20, Alpha::F21};
480
481 for (int i = 6; i < count; ++i) {
482 unsigned Opc = Alpha::WTF;
Duncan Sands92c43912008-06-06 12:08:01 +0000483 if (TypeOperands[i].isInteger()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000484 Opc = Alpha::STQ;
485 } else if (TypeOperands[i] == MVT::f32) {
486 Opc = Alpha::STS;
487 } else if (TypeOperands[i] == MVT::f64) {
488 Opc = Alpha::STT;
489 } else
490 assert(0 && "Unknown operand");
491
Dan Gohman8181bd12008-07-27 21:46:04 +0000492 SDValue Ops[] = { CallOperands[i], getI64Imm((i - 6) * 8),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000493 CurDAG->getCopyFromReg(Chain, Alpha::R30, MVT::i64),
494 Chain };
Dan Gohman8181bd12008-07-27 21:46:04 +0000495 Chain = SDValue(CurDAG->getTargetNode(Opc, MVT::Other, Ops, 4), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000496 }
497 for (int i = 0; i < std::min(6, count); ++i) {
Duncan Sands92c43912008-06-06 12:08:01 +0000498 if (TypeOperands[i].isInteger()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000499 Chain = CurDAG->getCopyToReg(Chain, args_int[i], CallOperands[i], InFlag);
500 InFlag = Chain.getValue(1);
501 } else if (TypeOperands[i] == MVT::f32 || TypeOperands[i] == MVT::f64) {
502 Chain = CurDAG->getCopyToReg(Chain, args_float[i], CallOperands[i], InFlag);
503 InFlag = Chain.getValue(1);
504 } else
505 assert(0 && "Unknown operand");
506 }
507
508 // Finally, once everything is in registers to pass to the call, emit the
509 // call itself.
510 if (Addr.getOpcode() == AlphaISD::GPRelLo) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000511 SDValue GOT = getGlobalBaseReg();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000512 Chain = CurDAG->getCopyToReg(Chain, Alpha::R29, GOT, InFlag);
513 InFlag = Chain.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000514 Chain = SDValue(CurDAG->getTargetNode(Alpha::BSR, MVT::Other, MVT::Flag,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000515 Addr.getOperand(0), Chain, InFlag), 0);
516 } else {
517 AddToISelQueue(Addr);
518 Chain = CurDAG->getCopyToReg(Chain, Alpha::R27, Addr, InFlag);
519 InFlag = Chain.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000520 Chain = SDValue(CurDAG->getTargetNode(Alpha::JSR, MVT::Other, MVT::Flag,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000521 Chain, InFlag), 0);
522 }
523 InFlag = Chain.getValue(1);
524
Dan Gohman8181bd12008-07-27 21:46:04 +0000525 std::vector<SDValue> CallResults;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000526
Duncan Sands92c43912008-06-06 12:08:01 +0000527 switch (N->getValueType(0).getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000528 default: assert(0 && "Unexpected ret value!");
529 case MVT::Other: break;
530 case MVT::i64:
531 Chain = CurDAG->getCopyFromReg(Chain, Alpha::R0, MVT::i64, InFlag).getValue(1);
532 CallResults.push_back(Chain.getValue(0));
533 break;
534 case MVT::f32:
535 Chain = CurDAG->getCopyFromReg(Chain, Alpha::F0, MVT::f32, InFlag).getValue(1);
536 CallResults.push_back(Chain.getValue(0));
537 break;
538 case MVT::f64:
539 Chain = CurDAG->getCopyFromReg(Chain, Alpha::F0, MVT::f64, InFlag).getValue(1);
540 CallResults.push_back(Chain.getValue(0));
541 break;
542 }
543
544 CallResults.push_back(Chain);
545 for (unsigned i = 0, e = CallResults.size(); i != e; ++i)
546 ReplaceUses(Op.getValue(i), CallResults[i]);
547}
548
549
550/// createAlphaISelDag - This pass converts a legalized DAG into a
551/// Alpha-specific DAG, ready for instruction scheduling.
552///
Dan Gohmanb41dfba2008-05-14 01:58:56 +0000553FunctionPass *llvm::createAlphaISelDag(AlphaTargetMachine &TM) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000554 return new AlphaDAGToDAGISel(TM);
555}