blob: 9a1add9753ba729d4578a0b8d982a2d76a2cf1f8 [file] [log] [blame]
Andrew Lenharthd97591a2005-10-20 00:29:02 +00001//===-- AlphaISelDAGToDAG.cpp - Alpha pattern matching inst selector ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Andrew Lenharth and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
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"
Andrew Lenharth7f0db912005-11-30 07:19:56 +000019#include "llvm/CodeGen/MachineFrameInfo.h"
Andrew Lenharthd97591a2005-10-20 00:29:02 +000020#include "llvm/CodeGen/MachineFunction.h"
21#include "llvm/CodeGen/SSARegMap.h"
22#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SelectionDAGISel.h"
24#include "llvm/Target/TargetOptions.h"
25#include "llvm/ADT/Statistic.h"
26#include "llvm/Constants.h"
27#include "llvm/GlobalValue.h"
Chris Lattner420736d2006-03-25 06:47:10 +000028#include "llvm/Intrinsics.h"
Andrew Lenharthd97591a2005-10-20 00:29:02 +000029#include "llvm/Support/Debug.h"
30#include "llvm/Support/MathExtras.h"
Andrew Lenharth756fbeb2005-10-22 22:06:58 +000031#include <algorithm>
Chris Lattner2c2c6c62006-01-22 23:41:00 +000032#include <iostream>
Evan Chengba2f0a92006-02-05 06:46:41 +000033#include <set>
Andrew Lenharthd97591a2005-10-20 00:29:02 +000034using namespace llvm;
35
36namespace {
37
38 //===--------------------------------------------------------------------===//
39 /// AlphaDAGToDAGISel - Alpha specific code to select Alpha machine
40 /// instructions for SelectionDAG operations.
Andrew Lenharthd97591a2005-10-20 00:29:02 +000041 class AlphaDAGToDAGISel : public SelectionDAGISel {
42 AlphaTargetLowering AlphaLowering;
43
Andrew Lenharthdcbaf8a2005-12-30 02:30:02 +000044 static const int64_t IMM_LOW = -32768;
45 static const int64_t IMM_HIGH = 32767;
46 static const int64_t IMM_MULT = 65536;
Andrew Lenharthfeab2f82006-01-01 22:16:14 +000047 static const int64_t IMM_FULLHIGH = IMM_HIGH + IMM_HIGH * IMM_MULT;
48 static const int64_t IMM_FULLLOW = IMM_LOW + IMM_LOW * IMM_MULT;
49
50 static int64_t get_ldah16(int64_t x) {
51 int64_t y = x / IMM_MULT;
52 if (x % IMM_MULT > IMM_HIGH)
53 ++y;
54 return y;
55 }
56
57 static int64_t get_lda16(int64_t x) {
58 return x - get_ldah16(x) * IMM_MULT;
59 }
60
61 static uint64_t get_zapImm(uint64_t x) {
62 unsigned int build = 0;
63 for(int i = 0; i < 8; ++i)
64 {
65 if ((x & 0x00FF) == 0x00FF)
66 build |= 1 << i;
67 else if ((x & 0x00FF) != 0)
68 { build = 0; break; }
69 x >>= 8;
70 }
Andrew Lenharth5d423602006-01-02 21:15:53 +000071 return build;
Andrew Lenharthfeab2f82006-01-01 22:16:14 +000072 }
73
74 static bool isFPZ(SDOperand N) {
75 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N);
76 return (CN && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)));
77 }
78 static bool isFPZn(SDOperand N) {
79 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N);
80 return (CN && CN->isExactlyValue(-0.0));
81 }
82 static bool isFPZp(SDOperand N) {
83 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N);
84 return (CN && CN->isExactlyValue(+0.0));
85 }
86
Andrew Lenharthd97591a2005-10-20 00:29:02 +000087 public:
88 AlphaDAGToDAGISel(TargetMachine &TM)
Andrew Lenharthdcbaf8a2005-12-30 02:30:02 +000089 : SelectionDAGISel(AlphaLowering), AlphaLowering(TM)
90 {}
Andrew Lenharthd97591a2005-10-20 00:29:02 +000091
92 /// getI64Imm - Return a target constant with the specified value, of type
93 /// i64.
Andrew Lenharth756fbeb2005-10-22 22:06:58 +000094 inline SDOperand getI64Imm(int64_t Imm) {
Andrew Lenharthd97591a2005-10-20 00:29:02 +000095 return CurDAG->getTargetConstant(Imm, MVT::i64);
96 }
97
Andrew Lenharthd97591a2005-10-20 00:29:02 +000098 // Select - Convert the specified operand from a target-independent to a
99 // target-specific node if it hasn't already been changed.
Evan Cheng34167212006-02-09 00:37:58 +0000100 void Select(SDOperand &Result, SDOperand Op);
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000101
102 /// InstructionSelectBasicBlock - This callback is invoked by
103 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
104 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
105
106 virtual const char *getPassName() const {
107 return "Alpha DAG->DAG Pattern Instruction Selection";
108 }
109
110// Include the pieces autogenerated from the target description.
111#include "AlphaGenDAGISel.inc"
112
113private:
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000114 SDOperand getGlobalBaseReg();
Andrew Lenharth93526222005-12-01 01:53:10 +0000115 SDOperand getRASaveReg();
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000116 SDOperand SelectCALL(SDOperand Op);
117
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000118 };
119}
120
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000121/// getGlobalBaseReg - Output the instructions required to put the
122/// GOT address into a register.
123///
124SDOperand AlphaDAGToDAGISel::getGlobalBaseReg() {
Andrew Lenharth93526222005-12-01 01:53:10 +0000125 return CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
126 AlphaLowering.getVRegGP(),
127 MVT::i64);
128}
129
130/// getRASaveReg - Grab the return address
131///
132SDOperand AlphaDAGToDAGISel::getRASaveReg() {
133 return CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
134 AlphaLowering.getVRegRA(),
135 MVT::i64);
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000136}
137
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000138/// InstructionSelectBasicBlock - This callback is invoked by
139/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
140void AlphaDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
141 DEBUG(BB->dump());
142
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000143 // Select target instructions for the DAG.
Evan Chengba2f0a92006-02-05 06:46:41 +0000144 DAG.setRoot(SelectRoot(DAG.getRoot()));
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000145 CodeGenMap.clear();
146 DAG.RemoveDeadNodes();
147
148 // Emit machine code to BB.
149 ScheduleAndEmitDAG(DAG);
150}
151
152// Select - Convert the specified operand from a target-independent to a
153// target-specific node if it hasn't already been changed.
Evan Cheng34167212006-02-09 00:37:58 +0000154void AlphaDAGToDAGISel::Select(SDOperand &Result, SDOperand Op) {
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000155 SDNode *N = Op.Val;
156 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
Evan Cheng34167212006-02-09 00:37:58 +0000157 N->getOpcode() < AlphaISD::FIRST_NUMBER) {
158 Result = Op;
159 return; // Already selected.
160 }
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000161
162 // If this has already been converted, use it.
163 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(Op);
Evan Cheng34167212006-02-09 00:37:58 +0000164 if (CGMI != CodeGenMap.end()) {
165 Result = CGMI->second;
166 return;
167 }
168
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000169 switch (N->getOpcode()) {
170 default: break;
Evan Cheng34167212006-02-09 00:37:58 +0000171 case AlphaISD::CALL:
172 Result = SelectCALL(Op);
173 return;
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000174
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000175 case ISD::FrameIndex: {
Andrew Lenharth50b37842005-11-22 04:20:06 +0000176 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Evan Cheng34167212006-02-09 00:37:58 +0000177 Result = CurDAG->SelectNodeTo(N, Alpha::LDA, MVT::i64,
178 CurDAG->getTargetFrameIndex(FI, MVT::i32),
179 getI64Imm(0));
180 return;
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000181 }
Andrew Lenharth4e629512005-12-24 05:36:33 +0000182 case AlphaISD::GlobalBaseReg:
Evan Cheng34167212006-02-09 00:37:58 +0000183 Result = getGlobalBaseReg();
184 return;
Andrew Lenharth4e629512005-12-24 05:36:33 +0000185
Andrew Lenharth53d89702005-12-25 01:34:27 +0000186 case AlphaISD::DivCall: {
187 SDOperand Chain = CurDAG->getEntryNode();
Evan Cheng34167212006-02-09 00:37:58 +0000188 SDOperand N0, N1, N2;
189 Select(N0, Op.getOperand(0));
190 Select(N1, Op.getOperand(1));
191 Select(N2, Op.getOperand(2));
192 Chain = CurDAG->getCopyToReg(Chain, Alpha::R24, N1,
Andrew Lenharth53d89702005-12-25 01:34:27 +0000193 SDOperand(0,0));
Evan Cheng34167212006-02-09 00:37:58 +0000194 Chain = CurDAG->getCopyToReg(Chain, Alpha::R25, N2,
Andrew Lenharth53d89702005-12-25 01:34:27 +0000195 Chain.getValue(1));
Evan Cheng34167212006-02-09 00:37:58 +0000196 Chain = CurDAG->getCopyToReg(Chain, Alpha::R27, N0,
Andrew Lenharth53d89702005-12-25 01:34:27 +0000197 Chain.getValue(1));
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000198 SDNode *CNode =
199 CurDAG->getTargetNode(Alpha::JSRs, MVT::Other, MVT::Flag,
200 Chain, Chain.getValue(1));
Andrew Lenharth53d89702005-12-25 01:34:27 +0000201 Chain = CurDAG->getCopyFromReg(Chain, Alpha::R27, MVT::i64,
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000202 SDOperand(CNode, 1));
Evan Cheng34167212006-02-09 00:37:58 +0000203 Result = CurDAG->SelectNodeTo(N, Alpha::BIS, MVT::i64, Chain, Chain);
204 return;
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000205 }
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000206
Andrew Lenharth739027e2006-01-16 21:22:38 +0000207 case ISD::READCYCLECOUNTER: {
Evan Cheng34167212006-02-09 00:37:58 +0000208 SDOperand Chain;
209 Select(Chain, N->getOperand(0)); //Select chain
210 Result = CurDAG->SelectNodeTo(N, Alpha::RPCC, MVT::i64, Chain);
211 return;
Andrew Lenharth739027e2006-01-16 21:22:38 +0000212 }
213
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000214 case ISD::RET: {
Evan Cheng34167212006-02-09 00:37:58 +0000215 SDOperand Chain;
216 Select(Chain, N->getOperand(0)); // Token chain.
Andrew Lenharth93526222005-12-01 01:53:10 +0000217 SDOperand InFlag;
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000218
219 if (N->getNumOperands() == 2) {
Evan Cheng34167212006-02-09 00:37:58 +0000220 SDOperand Val;
221 Select(Val, N->getOperand(1));
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000222 if (N->getOperand(1).getValueType() == MVT::i64) {
Andrew Lenharth93526222005-12-01 01:53:10 +0000223 Chain = CurDAG->getCopyToReg(Chain, Alpha::R0, Val, InFlag);
224 InFlag = Chain.getValue(1);
Andrew Lenharthe41419f2005-12-11 03:54:31 +0000225 } else if (N->getOperand(1).getValueType() == MVT::f64 ||
226 N->getOperand(1).getValueType() == MVT::f32) {
227 Chain = CurDAG->getCopyToReg(Chain, Alpha::F0, Val, InFlag);
228 InFlag = Chain.getValue(1);
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000229 }
230 }
Andrew Lenharth93526222005-12-01 01:53:10 +0000231 Chain = CurDAG->getCopyToReg(Chain, Alpha::R26, getRASaveReg(), InFlag);
232 InFlag = Chain.getValue(1);
233
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000234 // Finally, select this to a ret instruction.
Evan Cheng34167212006-02-09 00:37:58 +0000235 Result = CurDAG->SelectNodeTo(N, Alpha::RETDAG, MVT::Other, Chain, InFlag);
236 return;
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000237 }
Andrew Lenharth50b37842005-11-22 04:20:06 +0000238 case ISD::Constant: {
Andrew Lenharthdcbaf8a2005-12-30 02:30:02 +0000239 uint64_t uval = cast<ConstantSDNode>(N)->getValue();
Andrew Lenharth919e6662006-01-06 19:41:51 +0000240
Evan Cheng34167212006-02-09 00:37:58 +0000241 if (uval == 0) {
242 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), Alpha::R31,
243 MVT::i64);
244 return;
245 }
Andrew Lenharth919e6662006-01-06 19:41:51 +0000246
Andrew Lenharthdcbaf8a2005-12-30 02:30:02 +0000247 int64_t val = (int64_t)uval;
248 int32_t val32 = (int32_t)val;
249 if (val <= IMM_HIGH + IMM_HIGH * IMM_MULT &&
250 val >= IMM_LOW + IMM_LOW * IMM_MULT)
251 break; //(LDAH (LDA))
252 if ((uval >> 32) == 0 && //empty upper bits
Andrew Lenharthfeab2f82006-01-01 22:16:14 +0000253 val32 <= IMM_HIGH + IMM_HIGH * IMM_MULT)
254 // val32 >= IMM_LOW + IMM_LOW * IMM_MULT) //always true
Andrew Lenharthdcbaf8a2005-12-30 02:30:02 +0000255 break; //(zext (LDAH (LDA)))
256 //Else use the constant pool
257 MachineConstantPool *CP = BB->getParent()->getConstantPool();
258 ConstantUInt *C =
259 ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , uval);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000260 SDOperand CPI = CurDAG->getTargetConstantPool(C, MVT::i64);
261 SDNode *Tmp = CurDAG->getTargetNode(Alpha::LDAHr, MVT::i64, CPI,
262 getGlobalBaseReg());
Evan Cheng34167212006-02-09 00:37:58 +0000263 Result = CurDAG->SelectNodeTo(N, Alpha::LDQr, MVT::i64, MVT::Other,
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000264 CPI, SDOperand(Tmp, 0), CurDAG->getEntryNode());
Evan Cheng34167212006-02-09 00:37:58 +0000265 return;
Andrew Lenharth50b37842005-11-22 04:20:06 +0000266 }
Chris Lattner08a90222006-01-29 06:25:22 +0000267 case ISD::TargetConstantFP: {
268 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
269 bool isDouble = N->getValueType(0) == MVT::f64;
270 MVT::ValueType T = isDouble ? MVT::f64 : MVT::f32;
271 if (CN->isExactlyValue(+0.0)) {
Evan Cheng34167212006-02-09 00:37:58 +0000272 Result = CurDAG->SelectNodeTo(N, isDouble ? Alpha::CPYST : Alpha::CPYSS,
273 T, CurDAG->getRegister(Alpha::F31, T),
274 CurDAG->getRegister(Alpha::F31, T));
275 return;
Chris Lattner08a90222006-01-29 06:25:22 +0000276 } else if ( CN->isExactlyValue(-0.0)) {
Evan Cheng34167212006-02-09 00:37:58 +0000277 Result = CurDAG->SelectNodeTo(N, isDouble ? Alpha::CPYSNT : Alpha::CPYSNS,
278 T, CurDAG->getRegister(Alpha::F31, T),
279 CurDAG->getRegister(Alpha::F31, T));
280 return;
Chris Lattner08a90222006-01-29 06:25:22 +0000281 } else {
282 abort();
Andrew Lenharth50b37842005-11-22 04:20:06 +0000283 }
Chris Lattner08a90222006-01-29 06:25:22 +0000284 break;
285 }
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000286
287 case ISD::SETCC:
288 if (MVT::isFloatingPoint(N->getOperand(0).Val->getValueType(0))) {
289 unsigned Opc = Alpha::WTF;
290 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
291 bool rev = false;
Andrew Lenharthb2156f92005-11-30 17:11:20 +0000292 bool isNE = false;
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000293 switch(CC) {
294 default: N->dump(); assert(0 && "Unknown FP comparison!");
295 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
296 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
297 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
298 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
299 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
Andrew Lenharthb2156f92005-11-30 17:11:20 +0000300 case ISD::SETNE: Opc = Alpha::CMPTEQ; isNE = true; break;
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000301 };
Evan Cheng34167212006-02-09 00:37:58 +0000302 SDOperand tmp1, tmp2;
303 Select(tmp1, N->getOperand(0));
304 Select(tmp2, N->getOperand(1));
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000305 SDNode *cmp = CurDAG->getTargetNode(Opc, MVT::f64,
306 rev?tmp2:tmp1,
307 rev?tmp1:tmp2);
Andrew Lenharthb2156f92005-11-30 17:11:20 +0000308 if (isNE)
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000309 cmp = CurDAG->getTargetNode(Alpha::CMPTEQ, MVT::f64, SDOperand(cmp, 0),
Andrew Lenharthb2156f92005-11-30 17:11:20 +0000310 CurDAG->getRegister(Alpha::F31, MVT::f64));
311
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000312 SDOperand LD;
313 if (AlphaLowering.hasITOF()) {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000314 LD = CurDAG->getNode(AlphaISD::FTOIT_, MVT::i64, SDOperand(cmp, 0));
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000315 } else {
316 int FrameIdx =
317 CurDAG->getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
318 SDOperand FI = CurDAG->getFrameIndex(FrameIdx, MVT::i64);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000319 SDOperand ST =
320 SDOperand(CurDAG->getTargetNode(Alpha::STT, MVT::Other,
321 SDOperand(cmp, 0), FI,
322 CurDAG->getRegister(Alpha::R31, MVT::i64)), 0);
323 LD = SDOperand(CurDAG->getTargetNode(Alpha::LDQ, MVT::i64, FI,
324 CurDAG->getRegister(Alpha::R31, MVT::i64),
325 ST), 0);
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000326 }
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000327 Result = SDOperand(CurDAG->getTargetNode(Alpha::CMPULT, MVT::i64,
328 CurDAG->getRegister(Alpha::R31, MVT::i64),
329 LD), 0);
Evan Cheng34167212006-02-09 00:37:58 +0000330 return;
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000331 }
332 break;
Andrew Lenharthcd804962005-11-30 16:10:29 +0000333
Andrew Lenharth361f45a2005-12-12 17:43:52 +0000334 case ISD::SELECT:
335 if (MVT::isFloatingPoint(N->getValueType(0)) &&
336 (N->getOperand(0).getOpcode() != ISD::SETCC ||
337 !MVT::isFloatingPoint(N->getOperand(0).getOperand(1).getValueType()))) {
338 //This should be the condition not covered by the Patterns
339 //FIXME: Don't have SelectCode die, but rather return something testable
340 // so that things like this can be caught in fall though code
341 //move int to fp
342 bool isDouble = N->getValueType(0) == MVT::f64;
Evan Cheng34167212006-02-09 00:37:58 +0000343 SDOperand LD, cond, TV, FV;
344 Select(cond, N->getOperand(0));
345 Select(TV, N->getOperand(1));
346 Select(FV, N->getOperand(2));
Andrew Lenharth361f45a2005-12-12 17:43:52 +0000347
348 if (AlphaLowering.hasITOF()) {
349 LD = CurDAG->getNode(AlphaISD::ITOFT_, MVT::f64, cond);
350 } else {
351 int FrameIdx =
352 CurDAG->getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
353 SDOperand FI = CurDAG->getFrameIndex(FrameIdx, MVT::i64);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000354 SDOperand ST =
355 SDOperand(CurDAG->getTargetNode(Alpha::STQ, MVT::Other,
356 cond, FI, CurDAG->getRegister(Alpha::R31, MVT::i64)), 0);
357 LD = SDOperand(CurDAG->getTargetNode(Alpha::LDT, MVT::f64, FI,
358 CurDAG->getRegister(Alpha::R31, MVT::i64),
359 ST), 0);
Andrew Lenharth361f45a2005-12-12 17:43:52 +0000360 }
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000361 Result = SDOperand(CurDAG->getTargetNode(isDouble?Alpha::FCMOVNET:Alpha::FCMOVNES,
362 MVT::f64, FV, TV, LD), 0);
Evan Cheng34167212006-02-09 00:37:58 +0000363 return;
Andrew Lenharth361f45a2005-12-12 17:43:52 +0000364 }
365 break;
366
Andrew Lenharth40ec5032006-02-13 18:52:29 +0000367 case ISD::AND: {
368 ConstantSDNode* SC;
369 ConstantSDNode* MC;
370 if (N->getOperand(0).getOpcode() == ISD::SRL &&
371 (MC = dyn_cast<ConstantSDNode>(N->getOperand(1))) &&
372 (SC = dyn_cast<ConstantSDNode>(N->getOperand(0).getOperand(1))))
373 {
374 uint64_t sval = SC->getValue();
375 uint64_t mval = MC->getValue();
376 if (get_zapImm(mval)) //the result is a zap, let the autogened stuff deal
377 break;
378 // given mask X, and shift S, we want to see if there is any zap in the mask
379 // if we play around with the botton S bits
380 uint64_t dontcare = (~0ULL) >> (64 - sval);
381 uint64_t mask = mval << sval;
382
383 if (get_zapImm(mask | dontcare))
384 mask = mask | dontcare;
385
386 if (get_zapImm(mask)) {
387 SDOperand Src;
388 Select(Src, N->getOperand(0).getOperand(0));
389 SDOperand Z =
390 SDOperand(CurDAG->getTargetNode(Alpha::ZAPNOTi, MVT::i64, Src,
391 getI64Imm(get_zapImm(mask))), 0);
392 Result = SDOperand(CurDAG->getTargetNode(Alpha::SRL, MVT::i64, Z,
393 getI64Imm(sval)), 0);
394 return;
395 }
396 }
397 break;
398 }
399
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000400 }
Andrew Lenharthcd804962005-11-30 16:10:29 +0000401
Evan Cheng34167212006-02-09 00:37:58 +0000402 SelectCode(Result, Op);
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000403}
404
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000405SDOperand AlphaDAGToDAGISel::SelectCALL(SDOperand Op) {
Andrew Lenharth50b37842005-11-22 04:20:06 +0000406 //TODO: add flag stuff to prevent nondeturministic breakage!
407
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000408 SDNode *N = Op.Val;
Evan Cheng34167212006-02-09 00:37:58 +0000409 SDOperand Chain;
Andrew Lenhartheececba2005-12-25 17:36:48 +0000410 SDOperand Addr = N->getOperand(1);
Andrew Lenharth93526222005-12-01 01:53:10 +0000411 SDOperand InFlag; // Null incoming flag value.
Evan Cheng34167212006-02-09 00:37:58 +0000412 Select(Chain, N->getOperand(0));
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000413
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000414 std::vector<SDOperand> CallOperands;
415 std::vector<MVT::ValueType> TypeOperands;
416
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000417 //grab the arguments
418 for(int i = 2, e = N->getNumOperands(); i < e; ++i) {
Evan Cheng34167212006-02-09 00:37:58 +0000419 SDOperand Tmp;
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000420 TypeOperands.push_back(N->getOperand(i).getValueType());
Evan Cheng34167212006-02-09 00:37:58 +0000421 Select(Tmp, N->getOperand(i));
422 CallOperands.push_back(Tmp);
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000423 }
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000424 int count = N->getNumOperands() - 2;
425
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000426 static const unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
427 Alpha::R19, Alpha::R20, Alpha::R21};
428 static const unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
429 Alpha::F19, Alpha::F20, Alpha::F21};
430
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000431 for (int i = 6; i < count; ++i) {
432 unsigned Opc = Alpha::WTF;
433 if (MVT::isInteger(TypeOperands[i])) {
434 Opc = Alpha::STQ;
435 } else if (TypeOperands[i] == MVT::f32) {
436 Opc = Alpha::STS;
437 } else if (TypeOperands[i] == MVT::f64) {
438 Opc = Alpha::STT;
439 } else
440 assert(0 && "Unknown operand");
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000441 Chain = SDOperand(CurDAG->getTargetNode(Opc, MVT::Other, CallOperands[i],
442 getI64Imm((i - 6) * 8),
443 CurDAG->getCopyFromReg(Chain, Alpha::R30, MVT::i64),
444 Chain), 0);
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000445 }
Andrew Lenharth93526222005-12-01 01:53:10 +0000446 for (int i = 0; i < std::min(6, count); ++i) {
447 if (MVT::isInteger(TypeOperands[i])) {
448 Chain = CurDAG->getCopyToReg(Chain, args_int[i], CallOperands[i], InFlag);
449 InFlag = Chain.getValue(1);
450 } else if (TypeOperands[i] == MVT::f32 || TypeOperands[i] == MVT::f64) {
451 Chain = CurDAG->getCopyToReg(Chain, args_float[i], CallOperands[i], InFlag);
452 InFlag = Chain.getValue(1);
453 } else
454 assert(0 && "Unknown operand");
455 }
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000456
Andrew Lenharth93526222005-12-01 01:53:10 +0000457
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000458 // Finally, once everything is in registers to pass to the call, emit the
459 // call itself.
Andrew Lenhartheececba2005-12-25 17:36:48 +0000460 if (Addr.getOpcode() == AlphaISD::GPRelLo) {
461 SDOperand GOT = getGlobalBaseReg();
462 Chain = CurDAG->getCopyToReg(Chain, Alpha::R29, GOT, InFlag);
463 InFlag = Chain.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000464 Chain = SDOperand(CurDAG->getTargetNode(Alpha::BSR, MVT::Other, MVT::Flag,
465 Addr.getOperand(0), Chain, InFlag), 0);
Andrew Lenhartheececba2005-12-25 17:36:48 +0000466 } else {
Evan Cheng34167212006-02-09 00:37:58 +0000467 Select(Addr, Addr);
468 Chain = CurDAG->getCopyToReg(Chain, Alpha::R27, Addr, InFlag);
Andrew Lenhartheececba2005-12-25 17:36:48 +0000469 InFlag = Chain.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000470 Chain = SDOperand(CurDAG->getTargetNode(Alpha::JSR, MVT::Other, MVT::Flag,
471 Chain, InFlag), 0);
Andrew Lenhartheececba2005-12-25 17:36:48 +0000472 }
Andrew Lenharth93526222005-12-01 01:53:10 +0000473 InFlag = Chain.getValue(1);
474
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000475 std::vector<SDOperand> CallResults;
476
477 switch (N->getValueType(0)) {
478 default: assert(0 && "Unexpected ret value!");
479 case MVT::Other: break;
480 case MVT::i64:
Andrew Lenharth93526222005-12-01 01:53:10 +0000481 Chain = CurDAG->getCopyFromReg(Chain, Alpha::R0, MVT::i64, InFlag).getValue(1);
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000482 CallResults.push_back(Chain.getValue(0));
483 break;
Andrew Lenharth50b37842005-11-22 04:20:06 +0000484 case MVT::f32:
Andrew Lenharth93526222005-12-01 01:53:10 +0000485 Chain = CurDAG->getCopyFromReg(Chain, Alpha::F0, MVT::f32, InFlag).getValue(1);
Andrew Lenharth50b37842005-11-22 04:20:06 +0000486 CallResults.push_back(Chain.getValue(0));
487 break;
488 case MVT::f64:
Andrew Lenharth93526222005-12-01 01:53:10 +0000489 Chain = CurDAG->getCopyFromReg(Chain, Alpha::F0, MVT::f64, InFlag).getValue(1);
Andrew Lenharth50b37842005-11-22 04:20:06 +0000490 CallResults.push_back(Chain.getValue(0));
491 break;
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000492 }
493
494 CallResults.push_back(Chain);
495 for (unsigned i = 0, e = CallResults.size(); i != e; ++i)
496 CodeGenMap[Op.getValue(i)] = CallResults[i];
497 return CallResults[Op.ResNo];
498}
499
500
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000501/// createAlphaISelDag - This pass converts a legalized DAG into a
502/// Alpha-specific DAG, ready for instruction scheduling.
503///
504FunctionPass *llvm::createAlphaISelDag(TargetMachine &TM) {
505 return new AlphaDAGToDAGISel(TM);
506}