blob: b82359ddd5160d9c030b5f0aeb49818da4cebef1 [file] [log] [blame]
Duraid Madinaf2db9b82005-10-28 17:46:35 +00001//===---- IA64ISelDAGToDAG.cpp - IA64 pattern matching inst selector ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Duraid Madina 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 IA64,
11// converting a legalized dag to an IA64 dag.
12//
13//===----------------------------------------------------------------------===//
14
15#include "IA64.h"
16#include "IA64TargetMachine.h"
17#include "IA64ISelLowering.h"
18#include "llvm/CodeGen/MachineInstrBuilder.h"
19#include "llvm/CodeGen/MachineFunction.h"
20#include "llvm/CodeGen/SSARegMap.h"
21#include "llvm/CodeGen/SelectionDAG.h"
22#include "llvm/CodeGen/SelectionDAGISel.h"
23#include "llvm/Target/TargetOptions.h"
24#include "llvm/ADT/Statistic.h"
25#include "llvm/Constants.h"
26#include "llvm/GlobalValue.h"
Chris Lattner420736d2006-03-25 06:47:10 +000027#include "llvm/Intrinsics.h"
Duraid Madinaf2db9b82005-10-28 17:46:35 +000028#include "llvm/Support/Debug.h"
29#include "llvm/Support/MathExtras.h"
Chris Lattner2c2c6c62006-01-22 23:41:00 +000030#include <iostream>
Evan Cheng2ef88a02006-08-07 22:28:20 +000031#include <queue>
Evan Chengba2f0a92006-02-05 06:46:41 +000032#include <set>
Duraid Madinaf2db9b82005-10-28 17:46:35 +000033using namespace llvm;
34
35namespace {
36 Statistic<> FusedFP ("ia64-codegen", "Number of fused fp operations");
37 Statistic<> FrameOff("ia64-codegen", "Number of frame idx offsets collapsed");
38
39 //===--------------------------------------------------------------------===//
40 /// IA64DAGToDAGISel - IA64 specific code to select IA64 machine
41 /// instructions for SelectionDAG operations.
42 ///
43 class IA64DAGToDAGISel : public SelectionDAGISel {
44 IA64TargetLowering IA64Lowering;
45 unsigned GlobalBaseReg;
46 public:
Evan Chengc4c62572006-03-13 23:20:37 +000047 IA64DAGToDAGISel(IA64TargetMachine &TM)
48 : SelectionDAGISel(IA64Lowering), IA64Lowering(*TM.getTargetLowering()) {}
Duraid Madinaf2db9b82005-10-28 17:46:35 +000049
50 virtual bool runOnFunction(Function &Fn) {
51 // Make sure we re-emit a set of the global base reg if necessary
52 GlobalBaseReg = 0;
53 return SelectionDAGISel::runOnFunction(Fn);
54 }
55
56 /// getI64Imm - Return a target constant with the specified value, of type
57 /// i64.
58 inline SDOperand getI64Imm(uint64_t Imm) {
59 return CurDAG->getTargetConstant(Imm, MVT::i64);
60 }
61
62 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
63 /// base register. Return the virtual register that holds this value.
64 // SDOperand getGlobalBaseReg(); TODO: hmm
65
66 // Select - Convert the specified operand from a target-independent to a
67 // target-specific node if it hasn't already been changed.
Evan Cheng64a752f2006-08-11 09:08:15 +000068 SDNode *Select(SDOperand &Result, SDOperand N);
Duraid Madinaf2db9b82005-10-28 17:46:35 +000069
70 SDNode *SelectIntImmediateExpr(SDOperand LHS, SDOperand RHS,
71 unsigned OCHi, unsigned OCLo,
72 bool IsArithmetic = false,
73 bool Negate = false);
74 SDNode *SelectBitfieldInsert(SDNode *N);
75
76 /// SelectCC - Select a comparison of the specified values with the
77 /// specified condition code, returning the CR# of the expression.
78 SDOperand SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC);
79
80 /// SelectAddr - Given the specified address, return the two operands for a
81 /// load/store instruction, and return true if it should be an indexed [r+r]
82 /// operation.
83 bool SelectAddr(SDOperand Addr, SDOperand &Op1, SDOperand &Op2);
84
Duraid Madinaf2db9b82005-10-28 17:46:35 +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);
88
89 virtual const char *getPassName() const {
90 return "IA64 (Itanium) DAG->DAG Instruction Selector";
91 }
92
93// Include the pieces autogenerated from the target description.
94#include "IA64GenDAGISel.inc"
95
96private:
Duraid Madinab6f023a2005-11-21 14:14:54 +000097 SDOperand SelectDIV(SDOperand Op);
Duraid Madinaf2db9b82005-10-28 17:46:35 +000098 };
99}
100
101/// InstructionSelectBasicBlock - This callback is invoked by
102/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
103void IA64DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
104 DEBUG(BB->dump());
Evan Cheng33e9ad92006-07-27 06:40:15 +0000105
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000106 // Select target instructions for the DAG.
Evan Chengba2f0a92006-02-05 06:46:41 +0000107 DAG.setRoot(SelectRoot(DAG.getRoot()));
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000108 DAG.RemoveDeadNodes();
109
110 // Emit machine code to BB.
111 ScheduleAndEmitDAG(DAG);
112}
113
Duraid Madinab6f023a2005-11-21 14:14:54 +0000114SDOperand IA64DAGToDAGISel::SelectDIV(SDOperand Op) {
115 SDNode *N = Op.Val;
Evan Cheng34167212006-02-09 00:37:58 +0000116 SDOperand Chain, Tmp1, Tmp2;
Evan Cheng2ef88a02006-08-07 22:28:20 +0000117 AddToQueue(Chain, N->getOperand(0));
Duraid Madinab6f023a2005-11-21 14:14:54 +0000118
Evan Cheng2ef88a02006-08-07 22:28:20 +0000119 AddToQueue(Tmp1, N->getOperand(0));
120 AddToQueue(Tmp2, N->getOperand(1));
Duraid Madinab6f023a2005-11-21 14:14:54 +0000121
122 bool isFP=false;
123
124 if(MVT::isFloatingPoint(Tmp1.getValueType()))
125 isFP=true;
126
127 bool isModulus=false; // is it a division or a modulus?
128 bool isSigned=false;
129
130 switch(N->getOpcode()) {
131 case ISD::FDIV:
132 case ISD::SDIV: isModulus=false; isSigned=true; break;
133 case ISD::UDIV: isModulus=false; isSigned=false; break;
134 case ISD::FREM:
135 case ISD::SREM: isModulus=true; isSigned=true; break;
136 case ISD::UREM: isModulus=true; isSigned=false; break;
137 }
138
139 // TODO: check for integer divides by powers of 2 (or other simple patterns?)
140
141 SDOperand TmpPR, TmpPR2;
142 SDOperand TmpF1, TmpF2, TmpF3, TmpF4, TmpF5, TmpF6, TmpF7, TmpF8;
143 SDOperand TmpF9, TmpF10,TmpF11,TmpF12,TmpF13,TmpF14,TmpF15;
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000144 SDNode *Result;
Duraid Madina76bb6ae2006-01-16 14:33:04 +0000145
146 // we'll need copies of F0 and F1
147 SDOperand F0 = CurDAG->getRegister(IA64::F0, MVT::f64);
148 SDOperand F1 = CurDAG->getRegister(IA64::F1, MVT::f64);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000149
150 // OK, emit some code:
151
152 if(!isFP) {
153 // first, load the inputs into FP regs.
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000154 TmpF1 =
155 SDOperand(CurDAG->getTargetNode(IA64::SETFSIG, MVT::f64, Tmp1), 0);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000156 Chain = TmpF1.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000157 TmpF2 =
158 SDOperand(CurDAG->getTargetNode(IA64::SETFSIG, MVT::f64, Tmp2), 0);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000159 Chain = TmpF2.getValue(1);
160
161 // next, convert the inputs to FP
162 if(isSigned) {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000163 TmpF3 =
164 SDOperand(CurDAG->getTargetNode(IA64::FCVTXF, MVT::f64, TmpF1), 0);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000165 Chain = TmpF3.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000166 TmpF4 =
167 SDOperand(CurDAG->getTargetNode(IA64::FCVTXF, MVT::f64, TmpF2), 0);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000168 Chain = TmpF4.getValue(1);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000169 } else { // is unsigned
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000170 TmpF3 =
171 SDOperand(CurDAG->getTargetNode(IA64::FCVTXUFS1, MVT::f64, TmpF1), 0);
Duraid Madina76bb6ae2006-01-16 14:33:04 +0000172 Chain = TmpF3.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000173 TmpF4 =
174 SDOperand(CurDAG->getTargetNode(IA64::FCVTXUFS1, MVT::f64, TmpF2), 0);
Duraid Madina76bb6ae2006-01-16 14:33:04 +0000175 Chain = TmpF4.getValue(1);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000176 }
177
178 } else { // this is an FP divide/remainder, so we 'leak' some temp
179 // regs and assign TmpF3=Tmp1, TmpF4=Tmp2
180 TmpF3=Tmp1;
181 TmpF4=Tmp2;
182 }
183
184 // we start by computing an approximate reciprocal (good to 9 bits?)
185 // note, this instruction writes _both_ TmpF5 (answer) and TmpPR (predicate)
Duraid Madina0c81dc82006-01-16 06:33:38 +0000186 if(isFP)
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000187 TmpF5 = SDOperand(CurDAG->getTargetNode(IA64::FRCPAS0, MVT::f64, MVT::i1,
188 TmpF3, TmpF4), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000189 else
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000190 TmpF5 = SDOperand(CurDAG->getTargetNode(IA64::FRCPAS1, MVT::f64, MVT::i1,
191 TmpF3, TmpF4), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000192
Duraid Madinab6f023a2005-11-21 14:14:54 +0000193 TmpPR = TmpF5.getValue(1);
194 Chain = TmpF5.getValue(2);
195
Duraid Madina0c81dc82006-01-16 06:33:38 +0000196 SDOperand minusB;
197 if(isModulus) { // for remainders, it'll be handy to have
198 // copies of -input_b
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000199 minusB = SDOperand(CurDAG->getTargetNode(IA64::SUB, MVT::i64,
200 CurDAG->getRegister(IA64::r0, MVT::i64), Tmp2), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000201 Chain = minusB.getValue(1);
202 }
203
204 SDOperand TmpE0, TmpY1, TmpE1, TmpY2;
205
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000206 TmpE0 = SDOperand(CurDAG->getTargetNode(IA64::CFNMAS1, MVT::f64,
207 TmpF4, TmpF5, F1, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000208 Chain = TmpE0.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000209 TmpY1 = SDOperand(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
210 TmpF5, TmpE0, TmpF5, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000211 Chain = TmpY1.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000212 TmpE1 = SDOperand(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
213 TmpE0, TmpE0, F0, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000214 Chain = TmpE1.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000215 TmpY2 = SDOperand(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
216 TmpY1, TmpE1, TmpY1, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000217 Chain = TmpY2.getValue(1);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000218
Duraid Madina0c81dc82006-01-16 06:33:38 +0000219 if(isFP) { // if this is an FP divide, we finish up here and exit early
220 if(isModulus)
221 assert(0 && "Sorry, try another FORTRAN compiler.");
222
223 SDOperand TmpE2, TmpY3, TmpQ0, TmpR0;
224
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000225 TmpE2 = SDOperand(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
226 TmpE1, TmpE1, F0, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000227 Chain = TmpE2.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000228 TmpY3 = SDOperand(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
229 TmpY2, TmpE2, TmpY2, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000230 Chain = TmpY3.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000231 TmpQ0 =
232 SDOperand(CurDAG->getTargetNode(IA64::CFMADS1, MVT::f64, // double prec!
233 Tmp1, TmpY3, F0, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000234 Chain = TmpQ0.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000235 TmpR0 =
236 SDOperand(CurDAG->getTargetNode(IA64::CFNMADS1, MVT::f64, // double prec!
237 Tmp2, TmpQ0, Tmp1, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000238 Chain = TmpR0.getValue(1);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000239
Duraid Madina0c81dc82006-01-16 06:33:38 +0000240// we want Result to have the same target register as the frcpa, so
241// we two-address hack it. See the comment "for this to work..." on
242// page 48 of Intel application note #245415
243 Result = CurDAG->getTargetNode(IA64::TCFMADS0, MVT::f64, // d.p. s0 rndg!
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000244 TmpF5, TmpY3, TmpR0, TmpQ0, TmpPR);
245 Chain = SDOperand(Result, 1);
246 return SDOperand(Result, 0); // XXX: early exit!
Duraid Madina0c81dc82006-01-16 06:33:38 +0000247 } else { // this is *not* an FP divide, so there's a bit left to do:
248
249 SDOperand TmpQ2, TmpR2, TmpQ3, TmpQ;
250
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000251 TmpQ2 = SDOperand(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
252 TmpF3, TmpY2, F0, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000253 Chain = TmpQ2.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000254 TmpR2 = SDOperand(CurDAG->getTargetNode(IA64::CFNMAS1, MVT::f64,
255 TmpF4, TmpQ2, TmpF3, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000256 Chain = TmpR2.getValue(1);
Duraid Madinaae6dcdd2006-01-17 01:19:49 +0000257
Duraid Madina76bb6ae2006-01-16 14:33:04 +0000258// we want TmpQ3 to have the same target register as the frcpa? maybe we
259// should two-address hack it. See the comment "for this to work..." on page
260// 48 of Intel application note #245415
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000261 TmpQ3 = SDOperand(CurDAG->getTargetNode(IA64::TCFMAS1, MVT::f64,
262 TmpF5, TmpR2, TmpY2, TmpQ2, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000263 Chain = TmpQ3.getValue(1);
Duraid Madina76bb6ae2006-01-16 14:33:04 +0000264
Duraid Madinaae6dcdd2006-01-17 01:19:49 +0000265 // STORY: without these two-address instructions (TCFMAS1 and TCFMADS0)
266 // the FPSWA won't be able to help out in the case of large/tiny
267 // arguments. Other fun bugs may also appear, e.g. 0/x = x, not 0.
268
Duraid Madina0c81dc82006-01-16 06:33:38 +0000269 if(isSigned)
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000270 TmpQ = SDOperand(CurDAG->getTargetNode(IA64::FCVTFXTRUNCS1,
271 MVT::f64, TmpQ3), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000272 else
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000273 TmpQ = SDOperand(CurDAG->getTargetNode(IA64::FCVTFXUTRUNCS1,
274 MVT::f64, TmpQ3), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000275
276 Chain = TmpQ.getValue(1);
277
278 if(isModulus) {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000279 SDOperand FPminusB =
280 SDOperand(CurDAG->getTargetNode(IA64::SETFSIG, MVT::f64, minusB), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000281 Chain = FPminusB.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000282 SDOperand Remainder =
283 SDOperand(CurDAG->getTargetNode(IA64::XMAL, MVT::f64,
284 TmpQ, FPminusB, TmpF1), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000285 Chain = Remainder.getValue(1);
286 Result = CurDAG->getTargetNode(IA64::GETFSIG, MVT::i64, Remainder);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000287 Chain = SDOperand(Result, 1);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000288 } else { // just an integer divide
289 Result = CurDAG->getTargetNode(IA64::GETFSIG, MVT::i64, TmpQ);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000290 Chain = SDOperand(Result, 1);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000291 }
292
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000293 return SDOperand(Result, 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000294 } // wasn't an FP divide
Duraid Madinab6f023a2005-11-21 14:14:54 +0000295}
296
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000297// Select - Convert the specified operand from a target-independent to a
298// target-specific node if it hasn't already been changed.
Evan Cheng64a752f2006-08-11 09:08:15 +0000299SDNode *IA64DAGToDAGISel::Select(SDOperand &Result, SDOperand Op) {
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000300 SDNode *N = Op.Val;
301 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
Evan Cheng34167212006-02-09 00:37:58 +0000302 N->getOpcode() < IA64ISD::FIRST_NUMBER) {
303 Result = Op;
Evan Cheng64a752f2006-08-11 09:08:15 +0000304 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +0000305 }
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000306
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000307 switch (N->getOpcode()) {
308 default: break;
309
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000310 case IA64ISD::BRCALL: { // XXX: this is also a hack!
Evan Cheng34167212006-02-09 00:37:58 +0000311 SDOperand Chain;
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000312 SDOperand InFlag; // Null incoming flag value.
313
Evan Cheng2ef88a02006-08-07 22:28:20 +0000314 AddToQueue(Chain, N->getOperand(0));
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000315 if(N->getNumOperands()==3) // we have an incoming chain, callee and flag
Evan Cheng2ef88a02006-08-07 22:28:20 +0000316 AddToQueue(InFlag, N->getOperand(2));
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000317
318 unsigned CallOpcode;
319 SDOperand CallOperand;
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000320
321 // if we can call directly, do so
322 if (GlobalAddressSDNode *GASD =
323 dyn_cast<GlobalAddressSDNode>(N->getOperand(1))) {
324 CallOpcode = IA64::BRCALL_IPREL_GA;
325 CallOperand = CurDAG->getTargetGlobalAddress(GASD->getGlobal(), MVT::i64);
326 } else if (ExternalSymbolSDNode *ESSDN = // FIXME: we currently NEED this
327 // case for correctness, to avoid
328 // "non-pic code with imm reloc.n
329 // against dynamic symbol" errors
330 dyn_cast<ExternalSymbolSDNode>(N->getOperand(1))) {
331 CallOpcode = IA64::BRCALL_IPREL_ES;
332 CallOperand = N->getOperand(1);
333 } else {
334 // otherwise we need to load the function descriptor,
335 // load the branch target (function)'s entry point and GP,
336 // branch (call) then restore the GP
Evan Cheng34167212006-02-09 00:37:58 +0000337 SDOperand FnDescriptor;
Evan Cheng2ef88a02006-08-07 22:28:20 +0000338 AddToQueue(FnDescriptor, N->getOperand(1));
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000339
340 // load the branch target's entry point [mem] and
341 // GP value [mem+8]
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000342 SDOperand targetEntryPoint=
343 SDOperand(CurDAG->getTargetNode(IA64::LD8, MVT::i64, FnDescriptor), 0);
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000344 Chain = targetEntryPoint.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000345 SDOperand targetGPAddr=
346 SDOperand(CurDAG->getTargetNode(IA64::ADDS, MVT::i64,
347 FnDescriptor, CurDAG->getConstant(8, MVT::i64)), 0);
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000348 Chain = targetGPAddr.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000349 SDOperand targetGP=
350 SDOperand(CurDAG->getTargetNode(IA64::LD8, MVT::i64, targetGPAddr), 0);
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000351 Chain = targetGP.getValue(1);
352
353 Chain = CurDAG->getCopyToReg(Chain, IA64::r1, targetGP, InFlag);
354 InFlag = Chain.getValue(1);
355 Chain = CurDAG->getCopyToReg(Chain, IA64::B6, targetEntryPoint, InFlag); // FLAG these?
356 InFlag = Chain.getValue(1);
357
358 CallOperand = CurDAG->getRegister(IA64::B6, MVT::i64);
359 CallOpcode = IA64::BRCALL_INDIRECT;
360 }
361
362 // Finally, once everything is setup, emit the call itself
363 if(InFlag.Val)
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000364 Chain = SDOperand(CurDAG->getTargetNode(CallOpcode, MVT::Other, MVT::Flag,
365 CallOperand, InFlag), 0);
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000366 else // there might be no arguments
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000367 Chain = SDOperand(CurDAG->getTargetNode(CallOpcode, MVT::Other, MVT::Flag,
368 CallOperand, Chain), 0);
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000369 InFlag = Chain.getValue(1);
370
371 std::vector<SDOperand> CallResults;
372
373 CallResults.push_back(Chain);
374 CallResults.push_back(InFlag);
375
376 for (unsigned i = 0, e = CallResults.size(); i != e; ++i)
Evan Cheng2ef88a02006-08-07 22:28:20 +0000377 ReplaceUses(Op.getValue(i), CallResults[i]);
Evan Cheng34167212006-02-09 00:37:58 +0000378 Result = CallResults[Op.ResNo];
Evan Cheng64a752f2006-08-11 09:08:15 +0000379 return NULL;
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000380 }
Duraid Madinaa36153a2005-12-22 03:58:17 +0000381
Duraid Madina8617f3c2005-12-22 07:14:45 +0000382 case IA64ISD::GETFD: {
Evan Cheng34167212006-02-09 00:37:58 +0000383 SDOperand Input;
Evan Cheng2ef88a02006-08-07 22:28:20 +0000384 AddToQueue(Input, N->getOperand(0));
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000385 Result = SDOperand(CurDAG->getTargetNode(IA64::GETFD, MVT::i64, Input), 0);
Evan Cheng64a752f2006-08-11 09:08:15 +0000386 return Result.Val;
Duraid Madina8617f3c2005-12-22 07:14:45 +0000387 }
388
Duraid Madinab6f023a2005-11-21 14:14:54 +0000389 case ISD::FDIV:
390 case ISD::SDIV:
391 case ISD::UDIV:
392 case ISD::SREM:
Evan Cheng34167212006-02-09 00:37:58 +0000393 case ISD::UREM:
394 Result = SelectDIV(Op);
Evan Cheng64a752f2006-08-11 09:08:15 +0000395 return Result.Val;
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000396
Chris Lattnera54aa942006-01-29 06:26:08 +0000397 case ISD::TargetConstantFP: {
Duraid Madina056728f2005-11-02 07:32:59 +0000398 SDOperand Chain = CurDAG->getEntryNode(); // this is a constant, so..
399
Evan Cheng34167212006-02-09 00:37:58 +0000400 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0)) {
Evan Cheng23329f52006-08-16 07:30:09 +0000401 return CurDAG->getCopyFromReg(Chain, IA64::F0, MVT::f64).Val;
Evan Cheng34167212006-02-09 00:37:58 +0000402 } else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0)) {
Evan Cheng23329f52006-08-16 07:30:09 +0000403 return CurDAG->getCopyFromReg(Chain, IA64::F1, MVT::f64).Val;
Evan Cheng34167212006-02-09 00:37:58 +0000404 } else
Duraid Madina93856802005-11-02 02:35:04 +0000405 assert(0 && "Unexpected FP constant!");
406 }
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000407
408 case ISD::FrameIndex: { // TODO: reduce creepyness
409 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Evan Cheng23329f52006-08-16 07:30:09 +0000410 if (N->hasOneUse())
411 return CurDAG->SelectNodeTo(N, IA64::MOV, MVT::i64,
412 CurDAG->getTargetFrameIndex(FI, MVT::i64)).Val;
413 else
414 return SDOperand(CurDAG->getTargetNode(IA64::MOV, MVT::i64,
415 CurDAG->getTargetFrameIndex(FI, MVT::i64)), 0).Val;
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000416 }
417
Duraid Madina2e0348e2006-01-15 09:45:23 +0000418 case ISD::ConstantPool: { // TODO: nuke the constant pool
419 // (ia64 doesn't need one)
Evan Chengb8973bd2006-01-31 22:23:14 +0000420 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
421 Constant *C = CP->get();
422 SDOperand CPI = CurDAG->getTargetConstantPool(C, MVT::i64,
423 CP->getAlignment());
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000424 Result = SDOperand(CurDAG->getTargetNode(IA64::ADDL_GA, MVT::i64, // ?
425 CurDAG->getRegister(IA64::r1, MVT::i64), CPI), 0);
Evan Cheng64a752f2006-08-11 09:08:15 +0000426 return Result.Val;
Duraid Madina25d0a882005-10-29 16:08:30 +0000427 }
428
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000429 case ISD::GlobalAddress: {
430 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
431 SDOperand GA = CurDAG->getTargetGlobalAddress(GV, MVT::i64);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000432 SDOperand Tmp = SDOperand(CurDAG->getTargetNode(IA64::ADDL_GA, MVT::i64,
433 CurDAG->getRegister(IA64::r1, MVT::i64), GA), 0);
434 Result = SDOperand(CurDAG->getTargetNode(IA64::LD8, MVT::i64, Tmp), 0);
Evan Cheng64a752f2006-08-11 09:08:15 +0000435 return Result.Val;
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000436 }
Duraid Madinaa36153a2005-12-22 03:58:17 +0000437
438/* XXX case ISD::ExternalSymbol: {
439 SDOperand EA = CurDAG->getTargetExternalSymbol(cast<ExternalSymbolSDNode>(N)->getSymbol(),
440 MVT::i64);
441 SDOperand Tmp = CurDAG->getTargetNode(IA64::ADDL_EA, MVT::i64,
442 CurDAG->getRegister(IA64::r1, MVT::i64), EA);
443 return CurDAG->getTargetNode(IA64::LD8, MVT::i64, Tmp);
444 }
445*/
446
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000447 case ISD::LOAD:
Duraid Madinaecc1a1b2006-01-20 16:10:05 +0000448 case ISD::EXTLOAD: // FIXME: load -1, not 1, for bools?
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000449 case ISD::ZEXTLOAD: {
Evan Cheng34167212006-02-09 00:37:58 +0000450 SDOperand Chain, Address;
Evan Cheng2ef88a02006-08-07 22:28:20 +0000451 AddToQueue(Chain, N->getOperand(0));
452 AddToQueue(Address, N->getOperand(1));
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000453
454 MVT::ValueType TypeBeingLoaded = (N->getOpcode() == ISD::LOAD) ?
455 N->getValueType(0) : cast<VTSDNode>(N->getOperand(3))->getVT();
456 unsigned Opc;
457 switch (TypeBeingLoaded) {
Jim Laskey16d42c62006-07-11 18:25:13 +0000458 default:
459#ifndef NDEBUG
460 N->dump();
461#endif
462 assert(0 && "Cannot load this type!");
Duraid Madina9f729062005-11-04 09:59:06 +0000463 case MVT::i1: { // this is a bool
464 Opc = IA64::LD1; // first we load a byte, then compare for != 0
Evan Cheng34167212006-02-09 00:37:58 +0000465 if(N->getValueType(0) == MVT::i1) { // XXX: early exit!
Evan Cheng23329f52006-08-16 07:30:09 +0000466 return CurDAG->SelectNodeTo(N, IA64::CMPNE, MVT::i1, MVT::Other,
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000467 SDOperand(CurDAG->getTargetNode(Opc, MVT::i64, Address), 0),
Evan Cheng23329f52006-08-16 07:30:09 +0000468 CurDAG->getRegister(IA64::r0, MVT::i64),
469 Chain).getValue(Op.ResNo).Val;
Evan Cheng34167212006-02-09 00:37:58 +0000470 }
Duraid Madinaa36153a2005-12-22 03:58:17 +0000471 /* otherwise, we want to load a bool into something bigger: LD1
472 will do that for us, so we just fall through */
Chris Lattnerb19b8992005-11-30 23:02:08 +0000473 }
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000474 case MVT::i8: Opc = IA64::LD1; break;
475 case MVT::i16: Opc = IA64::LD2; break;
476 case MVT::i32: Opc = IA64::LD4; break;
477 case MVT::i64: Opc = IA64::LD8; break;
478
479 case MVT::f32: Opc = IA64::LDF4; break;
480 case MVT::f64: Opc = IA64::LDF8; break;
481 }
482
Chris Lattnerb19b8992005-11-30 23:02:08 +0000483 // TODO: comment this
Evan Cheng23329f52006-08-16 07:30:09 +0000484 return CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), MVT::Other,
485 Address, Chain).getValue(Op.ResNo).Val;
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000486 }
487
488 case ISD::TRUNCSTORE:
489 case ISD::STORE: {
Evan Cheng34167212006-02-09 00:37:58 +0000490 SDOperand Address, Chain;
Evan Cheng2ef88a02006-08-07 22:28:20 +0000491 AddToQueue(Address, N->getOperand(2));
492 AddToQueue(Chain, N->getOperand(0));
Duraid Madinad525df32005-11-07 03:11:02 +0000493
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000494 unsigned Opc;
495 if (N->getOpcode() == ISD::STORE) {
496 switch (N->getOperand(1).getValueType()) {
Duraid Madinad525df32005-11-07 03:11:02 +0000497 default: assert(0 && "unknown type in store");
498 case MVT::i1: { // this is a bool
499 Opc = IA64::ST1; // we store either 0 or 1 as a byte
Duraid Madina544cbbd2006-01-13 10:28:25 +0000500 // first load zero!
501 SDOperand Initial = CurDAG->getCopyFromReg(Chain, IA64::r0, MVT::i64);
502 Chain = Initial.getValue(1);
Duraid Madinaa7fb5be2006-01-20 03:40:25 +0000503 // then load 1 into the same reg iff the predicate to store is 1
Evan Cheng34167212006-02-09 00:37:58 +0000504 SDOperand Tmp;
Evan Cheng2ef88a02006-08-07 22:28:20 +0000505 AddToQueue(Tmp, N->getOperand(1));
Duraid Madinab20f9792006-02-11 07:33:17 +0000506 Tmp = SDOperand(CurDAG->getTargetNode(IA64::TPCADDS, MVT::i64, Initial,
507 CurDAG->getConstant(1, MVT::i64),
508 Tmp), 0);
Evan Cheng23329f52006-08-16 07:30:09 +0000509 return CurDAG->SelectNodeTo(N, Opc, MVT::Other, Address, Tmp, Chain).Val;
Chris Lattnerb19b8992005-11-30 23:02:08 +0000510 }
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000511 case MVT::i64: Opc = IA64::ST8; break;
512 case MVT::f64: Opc = IA64::STF8; break;
Duraid Madinad525df32005-11-07 03:11:02 +0000513 }
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000514 } else { //ISD::TRUNCSTORE
515 switch(cast<VTSDNode>(N->getOperand(4))->getVT()) {
Duraid Madinad525df32005-11-07 03:11:02 +0000516 default: assert(0 && "unknown type in truncstore");
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000517 case MVT::i8: Opc = IA64::ST1; break;
518 case MVT::i16: Opc = IA64::ST2; break;
519 case MVT::i32: Opc = IA64::ST4; break;
520 case MVT::f32: Opc = IA64::STF4; break;
521 }
522 }
523
Evan Cheng34167212006-02-09 00:37:58 +0000524 SDOperand N1, N2;
Evan Cheng2ef88a02006-08-07 22:28:20 +0000525 AddToQueue(N1, N->getOperand(1));
526 AddToQueue(N2, N->getOperand(2));
Evan Cheng23329f52006-08-16 07:30:09 +0000527 return CurDAG->SelectNodeTo(N, Opc, MVT::Other, N2, N1, Chain).Val;
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000528 }
529
530 case ISD::BRCOND: {
Evan Cheng34167212006-02-09 00:37:58 +0000531 SDOperand Chain, CC;
Evan Cheng2ef88a02006-08-07 22:28:20 +0000532 AddToQueue(Chain, N->getOperand(0));
533 AddToQueue(CC, N->getOperand(1));
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000534 MachineBasicBlock *Dest =
535 cast<BasicBlockSDNode>(N->getOperand(2))->getBasicBlock();
536 //FIXME - we do NOT need long branches all the time
Evan Cheng23329f52006-08-16 07:30:09 +0000537 return CurDAG->SelectNodeTo(N, IA64::BRLCOND_NOTCALL, MVT::Other, CC,
538 CurDAG->getBasicBlock(Dest), Chain).Val;
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000539 }
540
541 case ISD::CALLSEQ_START:
542 case ISD::CALLSEQ_END: {
543 int64_t Amt = cast<ConstantSDNode>(N->getOperand(1))->getValue();
544 unsigned Opc = N->getOpcode() == ISD::CALLSEQ_START ?
545 IA64::ADJUSTCALLSTACKDOWN : IA64::ADJUSTCALLSTACKUP;
Evan Cheng34167212006-02-09 00:37:58 +0000546 SDOperand N0;
Evan Cheng2ef88a02006-08-07 22:28:20 +0000547 AddToQueue(N0, N->getOperand(0));
Evan Cheng23329f52006-08-16 07:30:09 +0000548 return CurDAG->SelectNodeTo(N, Opc, MVT::Other, getI64Imm(Amt), N0).Val;
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000549 }
550
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000551 case ISD::BR:
552 // FIXME: we don't need long branches all the time!
Evan Cheng34167212006-02-09 00:37:58 +0000553 SDOperand N0;
Evan Cheng2ef88a02006-08-07 22:28:20 +0000554 AddToQueue(N0, N->getOperand(0));
Evan Cheng23329f52006-08-16 07:30:09 +0000555 return CurDAG->SelectNodeTo(N, IA64::BRL_NOTCALL, MVT::Other,
556 N->getOperand(1), N0).Val;
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000557 }
558
Evan Cheng64a752f2006-08-11 09:08:15 +0000559 return SelectCode(Result, Op);
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000560}
561
562
563/// createIA64DAGToDAGInstructionSelector - This pass converts a legalized DAG
564/// into an IA64-specific DAG, ready for instruction scheduling.
565///
Evan Chengc4c62572006-03-13 23:20:37 +0000566FunctionPass
567*llvm::createIA64DAGToDAGInstructionSelector(IA64TargetMachine &TM) {
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000568 return new IA64DAGToDAGISel(TM);
569}
570