blob: 4c49fb9d0a28e094e1f9d4635ee02edf5850760e [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"
28#include "llvm/Support/Debug.h"
29#include "llvm/Support/MathExtras.h"
Andrew Lenharth756fbeb2005-10-22 22:06:58 +000030#include <algorithm>
Chris Lattner2c2c6c62006-01-22 23:41:00 +000031#include <iostream>
Evan Chengba2f0a92006-02-05 06:46:41 +000032#include <set>
Andrew Lenharthd97591a2005-10-20 00:29:02 +000033using namespace llvm;
34
35namespace {
36
37 //===--------------------------------------------------------------------===//
38 /// AlphaDAGToDAGISel - Alpha specific code to select Alpha machine
39 /// instructions for SelectionDAG operations.
Andrew Lenharthd97591a2005-10-20 00:29:02 +000040 class AlphaDAGToDAGISel : public SelectionDAGISel {
41 AlphaTargetLowering AlphaLowering;
42
Andrew Lenharthdcbaf8a2005-12-30 02:30:02 +000043 static const int64_t IMM_LOW = -32768;
44 static const int64_t IMM_HIGH = 32767;
45 static const int64_t IMM_MULT = 65536;
Andrew Lenharthfeab2f82006-01-01 22:16:14 +000046 static const int64_t IMM_FULLHIGH = IMM_HIGH + IMM_HIGH * IMM_MULT;
47 static const int64_t IMM_FULLLOW = IMM_LOW + IMM_LOW * IMM_MULT;
48
49 static int64_t get_ldah16(int64_t x) {
50 int64_t y = x / IMM_MULT;
51 if (x % IMM_MULT > IMM_HIGH)
52 ++y;
53 return y;
54 }
55
56 static int64_t get_lda16(int64_t x) {
57 return x - get_ldah16(x) * IMM_MULT;
58 }
59
60 static uint64_t get_zapImm(uint64_t x) {
61 unsigned int build = 0;
62 for(int i = 0; i < 8; ++i)
63 {
64 if ((x & 0x00FF) == 0x00FF)
65 build |= 1 << i;
66 else if ((x & 0x00FF) != 0)
67 { build = 0; break; }
68 x >>= 8;
69 }
Andrew Lenharth5d423602006-01-02 21:15:53 +000070 return build;
Andrew Lenharthfeab2f82006-01-01 22:16:14 +000071 }
72
73 static bool isFPZ(SDOperand N) {
74 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N);
75 return (CN && (CN->isExactlyValue(+0.0) || CN->isExactlyValue(-0.0)));
76 }
77 static bool isFPZn(SDOperand N) {
78 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N);
79 return (CN && CN->isExactlyValue(-0.0));
80 }
81 static bool isFPZp(SDOperand N) {
82 ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N);
83 return (CN && CN->isExactlyValue(+0.0));
84 }
85
Andrew Lenharthd97591a2005-10-20 00:29:02 +000086 public:
87 AlphaDAGToDAGISel(TargetMachine &TM)
Andrew Lenharthdcbaf8a2005-12-30 02:30:02 +000088 : SelectionDAGISel(AlphaLowering), AlphaLowering(TM)
89 {}
Andrew Lenharthd97591a2005-10-20 00:29:02 +000090
91 /// getI64Imm - Return a target constant with the specified value, of type
92 /// i64.
Andrew Lenharth756fbeb2005-10-22 22:06:58 +000093 inline SDOperand getI64Imm(int64_t Imm) {
Andrew Lenharthd97591a2005-10-20 00:29:02 +000094 return CurDAG->getTargetConstant(Imm, MVT::i64);
95 }
96
Andrew Lenharthd97591a2005-10-20 00:29:02 +000097 // Select - Convert the specified operand from a target-independent to a
98 // target-specific node if it hasn't already been changed.
Evan Cheng34167212006-02-09 00:37:58 +000099 void Select(SDOperand &Result, SDOperand Op);
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000100
101 /// InstructionSelectBasicBlock - This callback is invoked by
102 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
103 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
104
105 virtual const char *getPassName() const {
106 return "Alpha DAG->DAG Pattern Instruction Selection";
107 }
108
109// Include the pieces autogenerated from the target description.
110#include "AlphaGenDAGISel.inc"
111
112private:
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000113 SDOperand getGlobalBaseReg();
Andrew Lenharth93526222005-12-01 01:53:10 +0000114 SDOperand getRASaveReg();
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000115 SDOperand SelectCALL(SDOperand Op);
116
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000117 };
118}
119
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000120/// getGlobalBaseReg - Output the instructions required to put the
121/// GOT address into a register.
122///
123SDOperand AlphaDAGToDAGISel::getGlobalBaseReg() {
Andrew Lenharth93526222005-12-01 01:53:10 +0000124 return CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
125 AlphaLowering.getVRegGP(),
126 MVT::i64);
127}
128
129/// getRASaveReg - Grab the return address
130///
131SDOperand AlphaDAGToDAGISel::getRASaveReg() {
132 return CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
133 AlphaLowering.getVRegRA(),
134 MVT::i64);
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000135}
136
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000137/// InstructionSelectBasicBlock - This callback is invoked by
138/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
139void AlphaDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
140 DEBUG(BB->dump());
141
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000142 // Select target instructions for the DAG.
Evan Chengba2f0a92006-02-05 06:46:41 +0000143 DAG.setRoot(SelectRoot(DAG.getRoot()));
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000144 CodeGenMap.clear();
145 DAG.RemoveDeadNodes();
146
147 // Emit machine code to BB.
148 ScheduleAndEmitDAG(DAG);
149}
150
151// Select - Convert the specified operand from a target-independent to a
152// target-specific node if it hasn't already been changed.
Evan Cheng34167212006-02-09 00:37:58 +0000153void AlphaDAGToDAGISel::Select(SDOperand &Result, SDOperand Op) {
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000154 SDNode *N = Op.Val;
155 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
Evan Cheng34167212006-02-09 00:37:58 +0000156 N->getOpcode() < AlphaISD::FIRST_NUMBER) {
157 Result = Op;
158 return; // Already selected.
159 }
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000160
161 // If this has already been converted, use it.
162 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(Op);
Evan Cheng34167212006-02-09 00:37:58 +0000163 if (CGMI != CodeGenMap.end()) {
164 Result = CGMI->second;
165 return;
166 }
167
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000168 switch (N->getOpcode()) {
169 default: break;
Evan Cheng34167212006-02-09 00:37:58 +0000170 case AlphaISD::CALL:
171 Result = SelectCALL(Op);
172 return;
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000173
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000174 case ISD::FrameIndex: {
Andrew Lenharth50b37842005-11-22 04:20:06 +0000175 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Evan Cheng34167212006-02-09 00:37:58 +0000176 Result = CurDAG->SelectNodeTo(N, Alpha::LDA, MVT::i64,
177 CurDAG->getTargetFrameIndex(FI, MVT::i32),
178 getI64Imm(0));
179 return;
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000180 }
Andrew Lenharth4e629512005-12-24 05:36:33 +0000181 case AlphaISD::GlobalBaseReg:
Evan Cheng34167212006-02-09 00:37:58 +0000182 Result = getGlobalBaseReg();
183 return;
Andrew Lenharth4e629512005-12-24 05:36:33 +0000184
Andrew Lenharth53d89702005-12-25 01:34:27 +0000185 case AlphaISD::DivCall: {
186 SDOperand Chain = CurDAG->getEntryNode();
Evan Cheng34167212006-02-09 00:37:58 +0000187 SDOperand N0, N1, N2;
188 Select(N0, Op.getOperand(0));
189 Select(N1, Op.getOperand(1));
190 Select(N2, Op.getOperand(2));
191 Chain = CurDAG->getCopyToReg(Chain, Alpha::R24, N1,
Andrew Lenharth53d89702005-12-25 01:34:27 +0000192 SDOperand(0,0));
Evan Cheng34167212006-02-09 00:37:58 +0000193 Chain = CurDAG->getCopyToReg(Chain, Alpha::R25, N2,
Andrew Lenharth53d89702005-12-25 01:34:27 +0000194 Chain.getValue(1));
Evan Cheng34167212006-02-09 00:37:58 +0000195 Chain = CurDAG->getCopyToReg(Chain, Alpha::R27, N0,
Andrew Lenharth53d89702005-12-25 01:34:27 +0000196 Chain.getValue(1));
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000197 SDNode *CNode =
198 CurDAG->getTargetNode(Alpha::JSRs, MVT::Other, MVT::Flag,
199 Chain, Chain.getValue(1));
Andrew Lenharth53d89702005-12-25 01:34:27 +0000200 Chain = CurDAG->getCopyFromReg(Chain, Alpha::R27, MVT::i64,
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000201 SDOperand(CNode, 1));
Evan Cheng34167212006-02-09 00:37:58 +0000202 Result = CurDAG->SelectNodeTo(N, Alpha::BIS, MVT::i64, Chain, Chain);
203 return;
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000204 }
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000205
Andrew Lenharth739027e2006-01-16 21:22:38 +0000206 case ISD::READCYCLECOUNTER: {
Evan Cheng34167212006-02-09 00:37:58 +0000207 SDOperand Chain;
208 Select(Chain, N->getOperand(0)); //Select chain
209 Result = CurDAG->SelectNodeTo(N, Alpha::RPCC, MVT::i64, Chain);
210 return;
Andrew Lenharth739027e2006-01-16 21:22:38 +0000211 }
212
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000213 case ISD::RET: {
Evan Cheng34167212006-02-09 00:37:58 +0000214 SDOperand Chain;
215 Select(Chain, N->getOperand(0)); // Token chain.
Andrew Lenharth93526222005-12-01 01:53:10 +0000216 SDOperand InFlag;
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000217
218 if (N->getNumOperands() == 2) {
Evan Cheng34167212006-02-09 00:37:58 +0000219 SDOperand Val;
220 Select(Val, N->getOperand(1));
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000221 if (N->getOperand(1).getValueType() == MVT::i64) {
Andrew Lenharth93526222005-12-01 01:53:10 +0000222 Chain = CurDAG->getCopyToReg(Chain, Alpha::R0, Val, InFlag);
223 InFlag = Chain.getValue(1);
Andrew Lenharthe41419f2005-12-11 03:54:31 +0000224 } else if (N->getOperand(1).getValueType() == MVT::f64 ||
225 N->getOperand(1).getValueType() == MVT::f32) {
226 Chain = CurDAG->getCopyToReg(Chain, Alpha::F0, Val, InFlag);
227 InFlag = Chain.getValue(1);
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000228 }
229 }
Andrew Lenharth93526222005-12-01 01:53:10 +0000230 Chain = CurDAG->getCopyToReg(Chain, Alpha::R26, getRASaveReg(), InFlag);
231 InFlag = Chain.getValue(1);
232
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000233 // Finally, select this to a ret instruction.
Evan Cheng34167212006-02-09 00:37:58 +0000234 Result = CurDAG->SelectNodeTo(N, Alpha::RETDAG, MVT::Other, Chain, InFlag);
235 return;
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000236 }
Andrew Lenharth50b37842005-11-22 04:20:06 +0000237 case ISD::Constant: {
Andrew Lenharthdcbaf8a2005-12-30 02:30:02 +0000238 uint64_t uval = cast<ConstantSDNode>(N)->getValue();
Andrew Lenharth919e6662006-01-06 19:41:51 +0000239
Evan Cheng34167212006-02-09 00:37:58 +0000240 if (uval == 0) {
241 Result = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), Alpha::R31,
242 MVT::i64);
243 return;
244 }
Andrew Lenharth919e6662006-01-06 19:41:51 +0000245
Andrew Lenharthdcbaf8a2005-12-30 02:30:02 +0000246 int64_t val = (int64_t)uval;
247 int32_t val32 = (int32_t)val;
248 if (val <= IMM_HIGH + IMM_HIGH * IMM_MULT &&
249 val >= IMM_LOW + IMM_LOW * IMM_MULT)
250 break; //(LDAH (LDA))
251 if ((uval >> 32) == 0 && //empty upper bits
Andrew Lenharthfeab2f82006-01-01 22:16:14 +0000252 val32 <= IMM_HIGH + IMM_HIGH * IMM_MULT)
253 // val32 >= IMM_LOW + IMM_LOW * IMM_MULT) //always true
Andrew Lenharthdcbaf8a2005-12-30 02:30:02 +0000254 break; //(zext (LDAH (LDA)))
255 //Else use the constant pool
256 MachineConstantPool *CP = BB->getParent()->getConstantPool();
257 ConstantUInt *C =
258 ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , uval);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000259 SDOperand CPI = CurDAG->getTargetConstantPool(C, MVT::i64);
260 SDNode *Tmp = CurDAG->getTargetNode(Alpha::LDAHr, MVT::i64, CPI,
261 getGlobalBaseReg());
Evan Cheng34167212006-02-09 00:37:58 +0000262 Result = CurDAG->SelectNodeTo(N, Alpha::LDQr, MVT::i64, MVT::Other,
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000263 CPI, SDOperand(Tmp, 0), CurDAG->getEntryNode());
Evan Cheng34167212006-02-09 00:37:58 +0000264 return;
Andrew Lenharth50b37842005-11-22 04:20:06 +0000265 }
Chris Lattner08a90222006-01-29 06:25:22 +0000266 case ISD::TargetConstantFP: {
267 ConstantFPSDNode *CN = cast<ConstantFPSDNode>(N);
268 bool isDouble = N->getValueType(0) == MVT::f64;
269 MVT::ValueType T = isDouble ? MVT::f64 : MVT::f32;
270 if (CN->isExactlyValue(+0.0)) {
Evan Cheng34167212006-02-09 00:37:58 +0000271 Result = CurDAG->SelectNodeTo(N, isDouble ? Alpha::CPYST : Alpha::CPYSS,
272 T, CurDAG->getRegister(Alpha::F31, T),
273 CurDAG->getRegister(Alpha::F31, T));
274 return;
Chris Lattner08a90222006-01-29 06:25:22 +0000275 } else if ( CN->isExactlyValue(-0.0)) {
Evan Cheng34167212006-02-09 00:37:58 +0000276 Result = CurDAG->SelectNodeTo(N, isDouble ? Alpha::CPYSNT : Alpha::CPYSNS,
277 T, CurDAG->getRegister(Alpha::F31, T),
278 CurDAG->getRegister(Alpha::F31, T));
279 return;
Chris Lattner08a90222006-01-29 06:25:22 +0000280 } else {
281 abort();
Andrew Lenharth50b37842005-11-22 04:20:06 +0000282 }
Chris Lattner08a90222006-01-29 06:25:22 +0000283 break;
284 }
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000285
286 case ISD::SETCC:
287 if (MVT::isFloatingPoint(N->getOperand(0).Val->getValueType(0))) {
288 unsigned Opc = Alpha::WTF;
289 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
290 bool rev = false;
Andrew Lenharthb2156f92005-11-30 17:11:20 +0000291 bool isNE = false;
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000292 switch(CC) {
293 default: N->dump(); assert(0 && "Unknown FP comparison!");
294 case ISD::SETEQ: Opc = Alpha::CMPTEQ; break;
295 case ISD::SETLT: Opc = Alpha::CMPTLT; break;
296 case ISD::SETLE: Opc = Alpha::CMPTLE; break;
297 case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break;
298 case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break;
Andrew Lenharthb2156f92005-11-30 17:11:20 +0000299 case ISD::SETNE: Opc = Alpha::CMPTEQ; isNE = true; break;
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000300 };
Evan Cheng34167212006-02-09 00:37:58 +0000301 SDOperand tmp1, tmp2;
302 Select(tmp1, N->getOperand(0));
303 Select(tmp2, N->getOperand(1));
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000304 SDNode *cmp = CurDAG->getTargetNode(Opc, MVT::f64,
305 rev?tmp2:tmp1,
306 rev?tmp1:tmp2);
Andrew Lenharthb2156f92005-11-30 17:11:20 +0000307 if (isNE)
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000308 cmp = CurDAG->getTargetNode(Alpha::CMPTEQ, MVT::f64, SDOperand(cmp, 0),
Andrew Lenharthb2156f92005-11-30 17:11:20 +0000309 CurDAG->getRegister(Alpha::F31, MVT::f64));
310
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000311 SDOperand LD;
312 if (AlphaLowering.hasITOF()) {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000313 LD = CurDAG->getNode(AlphaISD::FTOIT_, MVT::i64, SDOperand(cmp, 0));
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000314 } else {
315 int FrameIdx =
316 CurDAG->getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
317 SDOperand FI = CurDAG->getFrameIndex(FrameIdx, MVT::i64);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000318 SDOperand ST =
319 SDOperand(CurDAG->getTargetNode(Alpha::STT, MVT::Other,
320 SDOperand(cmp, 0), FI,
321 CurDAG->getRegister(Alpha::R31, MVT::i64)), 0);
322 LD = SDOperand(CurDAG->getTargetNode(Alpha::LDQ, MVT::i64, FI,
323 CurDAG->getRegister(Alpha::R31, MVT::i64),
324 ST), 0);
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000325 }
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000326 Result = SDOperand(CurDAG->getTargetNode(Alpha::CMPULT, MVT::i64,
327 CurDAG->getRegister(Alpha::R31, MVT::i64),
328 LD), 0);
Evan Cheng34167212006-02-09 00:37:58 +0000329 return;
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000330 }
331 break;
Andrew Lenharthcd804962005-11-30 16:10:29 +0000332
Andrew Lenharth361f45a2005-12-12 17:43:52 +0000333 case ISD::SELECT:
334 if (MVT::isFloatingPoint(N->getValueType(0)) &&
335 (N->getOperand(0).getOpcode() != ISD::SETCC ||
336 !MVT::isFloatingPoint(N->getOperand(0).getOperand(1).getValueType()))) {
337 //This should be the condition not covered by the Patterns
338 //FIXME: Don't have SelectCode die, but rather return something testable
339 // so that things like this can be caught in fall though code
340 //move int to fp
341 bool isDouble = N->getValueType(0) == MVT::f64;
Evan Cheng34167212006-02-09 00:37:58 +0000342 SDOperand LD, cond, TV, FV;
343 Select(cond, N->getOperand(0));
344 Select(TV, N->getOperand(1));
345 Select(FV, N->getOperand(2));
Andrew Lenharth361f45a2005-12-12 17:43:52 +0000346
347 if (AlphaLowering.hasITOF()) {
348 LD = CurDAG->getNode(AlphaISD::ITOFT_, MVT::f64, cond);
349 } else {
350 int FrameIdx =
351 CurDAG->getMachineFunction().getFrameInfo()->CreateStackObject(8, 8);
352 SDOperand FI = CurDAG->getFrameIndex(FrameIdx, MVT::i64);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000353 SDOperand ST =
354 SDOperand(CurDAG->getTargetNode(Alpha::STQ, MVT::Other,
355 cond, FI, CurDAG->getRegister(Alpha::R31, MVT::i64)), 0);
356 LD = SDOperand(CurDAG->getTargetNode(Alpha::LDT, MVT::f64, FI,
357 CurDAG->getRegister(Alpha::R31, MVT::i64),
358 ST), 0);
Andrew Lenharth361f45a2005-12-12 17:43:52 +0000359 }
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000360 Result = SDOperand(CurDAG->getTargetNode(isDouble?Alpha::FCMOVNET:Alpha::FCMOVNES,
361 MVT::f64, FV, TV, LD), 0);
Evan Cheng34167212006-02-09 00:37:58 +0000362 return;
Andrew Lenharth361f45a2005-12-12 17:43:52 +0000363 }
364 break;
365
Andrew Lenharth40ec5032006-02-13 18:52:29 +0000366 case ISD::AND: {
367 ConstantSDNode* SC;
368 ConstantSDNode* MC;
369 if (N->getOperand(0).getOpcode() == ISD::SRL &&
370 (MC = dyn_cast<ConstantSDNode>(N->getOperand(1))) &&
371 (SC = dyn_cast<ConstantSDNode>(N->getOperand(0).getOperand(1))))
372 {
373 uint64_t sval = SC->getValue();
374 uint64_t mval = MC->getValue();
375 if (get_zapImm(mval)) //the result is a zap, let the autogened stuff deal
376 break;
377 // given mask X, and shift S, we want to see if there is any zap in the mask
378 // if we play around with the botton S bits
379 uint64_t dontcare = (~0ULL) >> (64 - sval);
380 uint64_t mask = mval << sval;
381
382 if (get_zapImm(mask | dontcare))
383 mask = mask | dontcare;
384
385 if (get_zapImm(mask)) {
386 SDOperand Src;
387 Select(Src, N->getOperand(0).getOperand(0));
388 SDOperand Z =
389 SDOperand(CurDAG->getTargetNode(Alpha::ZAPNOTi, MVT::i64, Src,
390 getI64Imm(get_zapImm(mask))), 0);
391 Result = SDOperand(CurDAG->getTargetNode(Alpha::SRL, MVT::i64, Z,
392 getI64Imm(sval)), 0);
393 return;
394 }
395 }
396 break;
397 }
398
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000399 }
Andrew Lenharthcd804962005-11-30 16:10:29 +0000400
Evan Cheng34167212006-02-09 00:37:58 +0000401 SelectCode(Result, Op);
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000402}
403
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000404SDOperand AlphaDAGToDAGISel::SelectCALL(SDOperand Op) {
Andrew Lenharth50b37842005-11-22 04:20:06 +0000405 //TODO: add flag stuff to prevent nondeturministic breakage!
406
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000407 SDNode *N = Op.Val;
Evan Cheng34167212006-02-09 00:37:58 +0000408 SDOperand Chain;
Andrew Lenhartheececba2005-12-25 17:36:48 +0000409 SDOperand Addr = N->getOperand(1);
Andrew Lenharth93526222005-12-01 01:53:10 +0000410 SDOperand InFlag; // Null incoming flag value.
Evan Cheng34167212006-02-09 00:37:58 +0000411 Select(Chain, N->getOperand(0));
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000412
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000413 std::vector<SDOperand> CallOperands;
414 std::vector<MVT::ValueType> TypeOperands;
415
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000416 //grab the arguments
417 for(int i = 2, e = N->getNumOperands(); i < e; ++i) {
Evan Cheng34167212006-02-09 00:37:58 +0000418 SDOperand Tmp;
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000419 TypeOperands.push_back(N->getOperand(i).getValueType());
Evan Cheng34167212006-02-09 00:37:58 +0000420 Select(Tmp, N->getOperand(i));
421 CallOperands.push_back(Tmp);
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000422 }
Andrew Lenharth8b7f14e2005-10-23 03:43:48 +0000423 int count = N->getNumOperands() - 2;
424
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000425 static const unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18,
426 Alpha::R19, Alpha::R20, Alpha::R21};
427 static const unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18,
428 Alpha::F19, Alpha::F20, Alpha::F21};
429
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000430 for (int i = 6; i < count; ++i) {
431 unsigned Opc = Alpha::WTF;
432 if (MVT::isInteger(TypeOperands[i])) {
433 Opc = Alpha::STQ;
434 } else if (TypeOperands[i] == MVT::f32) {
435 Opc = Alpha::STS;
436 } else if (TypeOperands[i] == MVT::f64) {
437 Opc = Alpha::STT;
438 } else
439 assert(0 && "Unknown operand");
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000440 Chain = SDOperand(CurDAG->getTargetNode(Opc, MVT::Other, CallOperands[i],
441 getI64Imm((i - 6) * 8),
442 CurDAG->getCopyFromReg(Chain, Alpha::R30, MVT::i64),
443 Chain), 0);
Andrew Lenharth7f0db912005-11-30 07:19:56 +0000444 }
Andrew Lenharth93526222005-12-01 01:53:10 +0000445 for (int i = 0; i < std::min(6, count); ++i) {
446 if (MVT::isInteger(TypeOperands[i])) {
447 Chain = CurDAG->getCopyToReg(Chain, args_int[i], CallOperands[i], InFlag);
448 InFlag = Chain.getValue(1);
449 } else if (TypeOperands[i] == MVT::f32 || TypeOperands[i] == MVT::f64) {
450 Chain = CurDAG->getCopyToReg(Chain, args_float[i], CallOperands[i], InFlag);
451 InFlag = Chain.getValue(1);
452 } else
453 assert(0 && "Unknown operand");
454 }
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000455
Andrew Lenharth93526222005-12-01 01:53:10 +0000456
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000457 // Finally, once everything is in registers to pass to the call, emit the
458 // call itself.
Andrew Lenhartheececba2005-12-25 17:36:48 +0000459 if (Addr.getOpcode() == AlphaISD::GPRelLo) {
460 SDOperand GOT = getGlobalBaseReg();
461 Chain = CurDAG->getCopyToReg(Chain, Alpha::R29, GOT, InFlag);
462 InFlag = Chain.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000463 Chain = SDOperand(CurDAG->getTargetNode(Alpha::BSR, MVT::Other, MVT::Flag,
464 Addr.getOperand(0), Chain, InFlag), 0);
Andrew Lenhartheececba2005-12-25 17:36:48 +0000465 } else {
Evan Cheng34167212006-02-09 00:37:58 +0000466 Select(Addr, Addr);
467 Chain = CurDAG->getCopyToReg(Chain, Alpha::R27, Addr, InFlag);
Andrew Lenhartheececba2005-12-25 17:36:48 +0000468 InFlag = Chain.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000469 Chain = SDOperand(CurDAG->getTargetNode(Alpha::JSR, MVT::Other, MVT::Flag,
470 Chain, InFlag), 0);
Andrew Lenhartheececba2005-12-25 17:36:48 +0000471 }
Andrew Lenharth93526222005-12-01 01:53:10 +0000472 InFlag = Chain.getValue(1);
473
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000474 std::vector<SDOperand> CallResults;
475
476 switch (N->getValueType(0)) {
477 default: assert(0 && "Unexpected ret value!");
478 case MVT::Other: break;
479 case MVT::i64:
Andrew Lenharth93526222005-12-01 01:53:10 +0000480 Chain = CurDAG->getCopyFromReg(Chain, Alpha::R0, MVT::i64, InFlag).getValue(1);
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000481 CallResults.push_back(Chain.getValue(0));
482 break;
Andrew Lenharth50b37842005-11-22 04:20:06 +0000483 case MVT::f32:
Andrew Lenharth93526222005-12-01 01:53:10 +0000484 Chain = CurDAG->getCopyFromReg(Chain, Alpha::F0, MVT::f32, InFlag).getValue(1);
Andrew Lenharth50b37842005-11-22 04:20:06 +0000485 CallResults.push_back(Chain.getValue(0));
486 break;
487 case MVT::f64:
Andrew Lenharth93526222005-12-01 01:53:10 +0000488 Chain = CurDAG->getCopyFromReg(Chain, Alpha::F0, MVT::f64, InFlag).getValue(1);
Andrew Lenharth50b37842005-11-22 04:20:06 +0000489 CallResults.push_back(Chain.getValue(0));
490 break;
Andrew Lenharth756fbeb2005-10-22 22:06:58 +0000491 }
492
493 CallResults.push_back(Chain);
494 for (unsigned i = 0, e = CallResults.size(); i != e; ++i)
495 CodeGenMap[Op.getValue(i)] = CallResults[i];
496 return CallResults[Op.ResNo];
497}
498
499
Andrew Lenharthd97591a2005-10-20 00:29:02 +0000500/// createAlphaISelDag - This pass converts a legalized DAG into a
501/// Alpha-specific DAG, ready for instruction scheduling.
502///
503FunctionPass *llvm::createAlphaISelDag(TargetMachine &TM) {
504 return new AlphaDAGToDAGISel(TM);
505}