blob: c90493454ade91cebdd13172df8b141cf11d912f [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"
27#include "llvm/Support/Debug.h"
28#include "llvm/Support/MathExtras.h"
Chris Lattner2c2c6c62006-01-22 23:41:00 +000029#include <iostream>
Evan Chengba2f0a92006-02-05 06:46:41 +000030#include <set>
Duraid Madinaf2db9b82005-10-28 17:46:35 +000031using namespace llvm;
32
33namespace {
34 Statistic<> FusedFP ("ia64-codegen", "Number of fused fp operations");
35 Statistic<> FrameOff("ia64-codegen", "Number of frame idx offsets collapsed");
36
37 //===--------------------------------------------------------------------===//
38 /// IA64DAGToDAGISel - IA64 specific code to select IA64 machine
39 /// instructions for SelectionDAG operations.
40 ///
41 class IA64DAGToDAGISel : public SelectionDAGISel {
42 IA64TargetLowering IA64Lowering;
43 unsigned GlobalBaseReg;
44 public:
45 IA64DAGToDAGISel(TargetMachine &TM)
46 : SelectionDAGISel(IA64Lowering), IA64Lowering(TM) {}
47
48 virtual bool runOnFunction(Function &Fn) {
49 // Make sure we re-emit a set of the global base reg if necessary
50 GlobalBaseReg = 0;
51 return SelectionDAGISel::runOnFunction(Fn);
52 }
53
54 /// getI64Imm - Return a target constant with the specified value, of type
55 /// i64.
56 inline SDOperand getI64Imm(uint64_t Imm) {
57 return CurDAG->getTargetConstant(Imm, MVT::i64);
58 }
59
60 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
61 /// base register. Return the virtual register that holds this value.
62 // SDOperand getGlobalBaseReg(); TODO: hmm
63
64 // Select - Convert the specified operand from a target-independent to a
65 // target-specific node if it hasn't already been changed.
Evan Cheng34167212006-02-09 00:37:58 +000066 void Select(SDOperand &Result, SDOperand N);
Duraid Madinaf2db9b82005-10-28 17:46:35 +000067
68 SDNode *SelectIntImmediateExpr(SDOperand LHS, SDOperand RHS,
69 unsigned OCHi, unsigned OCLo,
70 bool IsArithmetic = false,
71 bool Negate = false);
72 SDNode *SelectBitfieldInsert(SDNode *N);
73
74 /// SelectCC - Select a comparison of the specified values with the
75 /// specified condition code, returning the CR# of the expression.
76 SDOperand SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC);
77
78 /// SelectAddr - Given the specified address, return the two operands for a
79 /// load/store instruction, and return true if it should be an indexed [r+r]
80 /// operation.
81 bool SelectAddr(SDOperand Addr, SDOperand &Op1, SDOperand &Op2);
82
83 SDOperand BuildSDIVSequence(SDNode *N);
84 SDOperand BuildUDIVSequence(SDNode *N);
85
86 /// InstructionSelectBasicBlock - This callback is invoked by
87 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
88 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
89
90 virtual const char *getPassName() const {
91 return "IA64 (Itanium) DAG->DAG Instruction Selector";
92 }
93
94// Include the pieces autogenerated from the target description.
95#include "IA64GenDAGISel.inc"
96
97private:
Duraid Madinab6f023a2005-11-21 14:14:54 +000098 SDOperand SelectDIV(SDOperand Op);
Duraid Madinaf2db9b82005-10-28 17:46:35 +000099 };
100}
101
102/// InstructionSelectBasicBlock - This callback is invoked by
103/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
104void IA64DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
105 DEBUG(BB->dump());
106
107 // The selection process is inherently a bottom-up recursive process (users
108 // select their uses before themselves). Given infinite stack space, we
109 // could just start selecting on the root and traverse the whole graph. In
110 // practice however, this causes us to run out of stack space on large basic
111 // blocks. To avoid this problem, select the entry node, then all its uses,
112 // iteratively instead of recursively.
113 std::vector<SDOperand> Worklist;
114 Worklist.push_back(DAG.getEntryNode());
115
116 // Note that we can do this in the IA64 target (scanning forward across token
117 // chain edges) because no nodes ever get folded across these edges. On a
118 // target like X86 which supports load/modify/store operations, this would
119 // have to be more careful.
120 while (!Worklist.empty()) {
121 SDOperand Node = Worklist.back();
122 Worklist.pop_back();
123
124 // Chose from the least deep of the top two nodes.
125 if (!Worklist.empty() &&
126 Worklist.back().Val->getNodeDepth() < Node.Val->getNodeDepth())
127 std::swap(Worklist.back(), Node);
128
129 if ((Node.Val->getOpcode() >= ISD::BUILTIN_OP_END &&
130 Node.Val->getOpcode() < IA64ISD::FIRST_NUMBER) ||
131 CodeGenMap.count(Node)) continue;
132
133 for (SDNode::use_iterator UI = Node.Val->use_begin(),
134 E = Node.Val->use_end(); UI != E; ++UI) {
135 // Scan the values. If this use has a value that is a token chain, add it
136 // to the worklist.
137 SDNode *User = *UI;
138 for (unsigned i = 0, e = User->getNumValues(); i != e; ++i)
139 if (User->getValueType(i) == MVT::Other) {
140 Worklist.push_back(SDOperand(User, i));
141 break;
142 }
143 }
144
145 // Finally, legalize this node.
Evan Cheng34167212006-02-09 00:37:58 +0000146 SDOperand Dummy;
147 Select(Dummy, Node);
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000148 }
149
150 // Select target instructions for the DAG.
Evan Chengba2f0a92006-02-05 06:46:41 +0000151 DAG.setRoot(SelectRoot(DAG.getRoot()));
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000152 CodeGenMap.clear();
153 DAG.RemoveDeadNodes();
154
155 // Emit machine code to BB.
156 ScheduleAndEmitDAG(DAG);
157}
158
Duraid Madinab6f023a2005-11-21 14:14:54 +0000159SDOperand IA64DAGToDAGISel::SelectDIV(SDOperand Op) {
160 SDNode *N = Op.Val;
Evan Cheng34167212006-02-09 00:37:58 +0000161 SDOperand Chain, Tmp1, Tmp2;
162 Select(Chain, N->getOperand(0));
Duraid Madinab6f023a2005-11-21 14:14:54 +0000163
Evan Cheng34167212006-02-09 00:37:58 +0000164 Select(Tmp1, N->getOperand(0));
165 Select(Tmp2, N->getOperand(1));
Duraid Madinab6f023a2005-11-21 14:14:54 +0000166
167 bool isFP=false;
168
169 if(MVT::isFloatingPoint(Tmp1.getValueType()))
170 isFP=true;
171
172 bool isModulus=false; // is it a division or a modulus?
173 bool isSigned=false;
174
175 switch(N->getOpcode()) {
176 case ISD::FDIV:
177 case ISD::SDIV: isModulus=false; isSigned=true; break;
178 case ISD::UDIV: isModulus=false; isSigned=false; break;
179 case ISD::FREM:
180 case ISD::SREM: isModulus=true; isSigned=true; break;
181 case ISD::UREM: isModulus=true; isSigned=false; break;
182 }
183
184 // TODO: check for integer divides by powers of 2 (or other simple patterns?)
185
186 SDOperand TmpPR, TmpPR2;
187 SDOperand TmpF1, TmpF2, TmpF3, TmpF4, TmpF5, TmpF6, TmpF7, TmpF8;
188 SDOperand TmpF9, TmpF10,TmpF11,TmpF12,TmpF13,TmpF14,TmpF15;
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000189 SDNode *Result;
Duraid Madina76bb6ae2006-01-16 14:33:04 +0000190
191 // we'll need copies of F0 and F1
192 SDOperand F0 = CurDAG->getRegister(IA64::F0, MVT::f64);
193 SDOperand F1 = CurDAG->getRegister(IA64::F1, MVT::f64);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000194
195 // OK, emit some code:
196
197 if(!isFP) {
198 // first, load the inputs into FP regs.
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000199 TmpF1 =
200 SDOperand(CurDAG->getTargetNode(IA64::SETFSIG, MVT::f64, Tmp1), 0);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000201 Chain = TmpF1.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000202 TmpF2 =
203 SDOperand(CurDAG->getTargetNode(IA64::SETFSIG, MVT::f64, Tmp2), 0);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000204 Chain = TmpF2.getValue(1);
205
206 // next, convert the inputs to FP
207 if(isSigned) {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000208 TmpF3 =
209 SDOperand(CurDAG->getTargetNode(IA64::FCVTXF, MVT::f64, TmpF1), 0);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000210 Chain = TmpF3.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000211 TmpF4 =
212 SDOperand(CurDAG->getTargetNode(IA64::FCVTXF, MVT::f64, TmpF2), 0);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000213 Chain = TmpF4.getValue(1);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000214 } else { // is unsigned
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000215 TmpF3 =
216 SDOperand(CurDAG->getTargetNode(IA64::FCVTXUFS1, MVT::f64, TmpF1), 0);
Duraid Madina76bb6ae2006-01-16 14:33:04 +0000217 Chain = TmpF3.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000218 TmpF4 =
219 SDOperand(CurDAG->getTargetNode(IA64::FCVTXUFS1, MVT::f64, TmpF2), 0);
Duraid Madina76bb6ae2006-01-16 14:33:04 +0000220 Chain = TmpF4.getValue(1);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000221 }
222
223 } else { // this is an FP divide/remainder, so we 'leak' some temp
224 // regs and assign TmpF3=Tmp1, TmpF4=Tmp2
225 TmpF3=Tmp1;
226 TmpF4=Tmp2;
227 }
228
229 // we start by computing an approximate reciprocal (good to 9 bits?)
230 // note, this instruction writes _both_ TmpF5 (answer) and TmpPR (predicate)
Duraid Madina0c81dc82006-01-16 06:33:38 +0000231 if(isFP)
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000232 TmpF5 = SDOperand(CurDAG->getTargetNode(IA64::FRCPAS0, MVT::f64, MVT::i1,
233 TmpF3, TmpF4), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000234 else
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000235 TmpF5 = SDOperand(CurDAG->getTargetNode(IA64::FRCPAS1, MVT::f64, MVT::i1,
236 TmpF3, TmpF4), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000237
Duraid Madinab6f023a2005-11-21 14:14:54 +0000238 TmpPR = TmpF5.getValue(1);
239 Chain = TmpF5.getValue(2);
240
Duraid Madina0c81dc82006-01-16 06:33:38 +0000241 SDOperand minusB;
242 if(isModulus) { // for remainders, it'll be handy to have
243 // copies of -input_b
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000244 minusB = SDOperand(CurDAG->getTargetNode(IA64::SUB, MVT::i64,
245 CurDAG->getRegister(IA64::r0, MVT::i64), Tmp2), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000246 Chain = minusB.getValue(1);
247 }
248
249 SDOperand TmpE0, TmpY1, TmpE1, TmpY2;
250
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000251 TmpE0 = SDOperand(CurDAG->getTargetNode(IA64::CFNMAS1, MVT::f64,
252 TmpF4, TmpF5, F1, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000253 Chain = TmpE0.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000254 TmpY1 = SDOperand(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
255 TmpF5, TmpE0, TmpF5, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000256 Chain = TmpY1.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000257 TmpE1 = SDOperand(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
258 TmpE0, TmpE0, F0, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000259 Chain = TmpE1.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000260 TmpY2 = SDOperand(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
261 TmpY1, TmpE1, TmpY1, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000262 Chain = TmpY2.getValue(1);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000263
Duraid Madina0c81dc82006-01-16 06:33:38 +0000264 if(isFP) { // if this is an FP divide, we finish up here and exit early
265 if(isModulus)
266 assert(0 && "Sorry, try another FORTRAN compiler.");
267
268 SDOperand TmpE2, TmpY3, TmpQ0, TmpR0;
269
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000270 TmpE2 = SDOperand(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
271 TmpE1, TmpE1, F0, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000272 Chain = TmpE2.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000273 TmpY3 = SDOperand(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
274 TmpY2, TmpE2, TmpY2, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000275 Chain = TmpY3.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000276 TmpQ0 =
277 SDOperand(CurDAG->getTargetNode(IA64::CFMADS1, MVT::f64, // double prec!
278 Tmp1, TmpY3, F0, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000279 Chain = TmpQ0.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000280 TmpR0 =
281 SDOperand(CurDAG->getTargetNode(IA64::CFNMADS1, MVT::f64, // double prec!
282 Tmp2, TmpQ0, Tmp1, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000283 Chain = TmpR0.getValue(1);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000284
Duraid Madina0c81dc82006-01-16 06:33:38 +0000285// we want Result to have the same target register as the frcpa, so
286// we two-address hack it. See the comment "for this to work..." on
287// page 48 of Intel application note #245415
288 Result = CurDAG->getTargetNode(IA64::TCFMADS0, MVT::f64, // d.p. s0 rndg!
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000289 TmpF5, TmpY3, TmpR0, TmpQ0, TmpPR);
290 Chain = SDOperand(Result, 1);
291 return SDOperand(Result, 0); // XXX: early exit!
Duraid Madina0c81dc82006-01-16 06:33:38 +0000292 } else { // this is *not* an FP divide, so there's a bit left to do:
293
294 SDOperand TmpQ2, TmpR2, TmpQ3, TmpQ;
295
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000296 TmpQ2 = SDOperand(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
297 TmpF3, TmpY2, F0, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000298 Chain = TmpQ2.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000299 TmpR2 = SDOperand(CurDAG->getTargetNode(IA64::CFNMAS1, MVT::f64,
300 TmpF4, TmpQ2, TmpF3, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000301 Chain = TmpR2.getValue(1);
Duraid Madinaae6dcdd2006-01-17 01:19:49 +0000302
Duraid Madina76bb6ae2006-01-16 14:33:04 +0000303// we want TmpQ3 to have the same target register as the frcpa? maybe we
304// should two-address hack it. See the comment "for this to work..." on page
305// 48 of Intel application note #245415
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000306 TmpQ3 = SDOperand(CurDAG->getTargetNode(IA64::TCFMAS1, MVT::f64,
307 TmpF5, TmpR2, TmpY2, TmpQ2, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000308 Chain = TmpQ3.getValue(1);
Duraid Madina76bb6ae2006-01-16 14:33:04 +0000309
Duraid Madinaae6dcdd2006-01-17 01:19:49 +0000310 // STORY: without these two-address instructions (TCFMAS1 and TCFMADS0)
311 // the FPSWA won't be able to help out in the case of large/tiny
312 // arguments. Other fun bugs may also appear, e.g. 0/x = x, not 0.
313
Duraid Madina0c81dc82006-01-16 06:33:38 +0000314 if(isSigned)
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000315 TmpQ = SDOperand(CurDAG->getTargetNode(IA64::FCVTFXTRUNCS1,
316 MVT::f64, TmpQ3), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000317 else
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000318 TmpQ = SDOperand(CurDAG->getTargetNode(IA64::FCVTFXUTRUNCS1,
319 MVT::f64, TmpQ3), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000320
321 Chain = TmpQ.getValue(1);
322
323 if(isModulus) {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000324 SDOperand FPminusB =
325 SDOperand(CurDAG->getTargetNode(IA64::SETFSIG, MVT::f64, minusB), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000326 Chain = FPminusB.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000327 SDOperand Remainder =
328 SDOperand(CurDAG->getTargetNode(IA64::XMAL, MVT::f64,
329 TmpQ, FPminusB, TmpF1), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000330 Chain = Remainder.getValue(1);
331 Result = CurDAG->getTargetNode(IA64::GETFSIG, MVT::i64, Remainder);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000332 Chain = SDOperand(Result, 1);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000333 } else { // just an integer divide
334 Result = CurDAG->getTargetNode(IA64::GETFSIG, MVT::i64, TmpQ);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000335 Chain = SDOperand(Result, 1);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000336 }
337
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000338 return SDOperand(Result, 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000339 } // wasn't an FP divide
Duraid Madinab6f023a2005-11-21 14:14:54 +0000340}
341
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000342// Select - Convert the specified operand from a target-independent to a
343// target-specific node if it hasn't already been changed.
Evan Cheng34167212006-02-09 00:37:58 +0000344void IA64DAGToDAGISel::Select(SDOperand &Result, SDOperand Op) {
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000345 SDNode *N = Op.Val;
346 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
Evan Cheng34167212006-02-09 00:37:58 +0000347 N->getOpcode() < IA64ISD::FIRST_NUMBER) {
348 Result = Op;
349 return; // Already selected.
350 }
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000351
352 // If this has already been converted, use it.
353 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(Op);
Evan Cheng34167212006-02-09 00:37:58 +0000354 if (CGMI != CodeGenMap.end()) {
355 Result = CGMI->second;
356 return;
357 }
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000358
359 switch (N->getOpcode()) {
360 default: break;
361
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000362 case IA64ISD::BRCALL: { // XXX: this is also a hack!
Evan Cheng34167212006-02-09 00:37:58 +0000363 SDOperand Chain;
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000364 SDOperand InFlag; // Null incoming flag value.
365
Evan Cheng34167212006-02-09 00:37:58 +0000366 Select(Chain, N->getOperand(0));
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000367 if(N->getNumOperands()==3) // we have an incoming chain, callee and flag
Evan Cheng34167212006-02-09 00:37:58 +0000368 Select(InFlag, N->getOperand(2));
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000369
370 unsigned CallOpcode;
371 SDOperand CallOperand;
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000372
373 // if we can call directly, do so
374 if (GlobalAddressSDNode *GASD =
375 dyn_cast<GlobalAddressSDNode>(N->getOperand(1))) {
376 CallOpcode = IA64::BRCALL_IPREL_GA;
377 CallOperand = CurDAG->getTargetGlobalAddress(GASD->getGlobal(), MVT::i64);
378 } else if (ExternalSymbolSDNode *ESSDN = // FIXME: we currently NEED this
379 // case for correctness, to avoid
380 // "non-pic code with imm reloc.n
381 // against dynamic symbol" errors
382 dyn_cast<ExternalSymbolSDNode>(N->getOperand(1))) {
383 CallOpcode = IA64::BRCALL_IPREL_ES;
384 CallOperand = N->getOperand(1);
385 } else {
386 // otherwise we need to load the function descriptor,
387 // load the branch target (function)'s entry point and GP,
388 // branch (call) then restore the GP
Evan Cheng34167212006-02-09 00:37:58 +0000389 SDOperand FnDescriptor;
390 Select(FnDescriptor, N->getOperand(1));
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000391
392 // load the branch target's entry point [mem] and
393 // GP value [mem+8]
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000394 SDOperand targetEntryPoint=
395 SDOperand(CurDAG->getTargetNode(IA64::LD8, MVT::i64, FnDescriptor), 0);
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000396 Chain = targetEntryPoint.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000397 SDOperand targetGPAddr=
398 SDOperand(CurDAG->getTargetNode(IA64::ADDS, MVT::i64,
399 FnDescriptor, CurDAG->getConstant(8, MVT::i64)), 0);
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000400 Chain = targetGPAddr.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000401 SDOperand targetGP=
402 SDOperand(CurDAG->getTargetNode(IA64::LD8, MVT::i64, targetGPAddr), 0);
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000403 Chain = targetGP.getValue(1);
404
405 Chain = CurDAG->getCopyToReg(Chain, IA64::r1, targetGP, InFlag);
406 InFlag = Chain.getValue(1);
407 Chain = CurDAG->getCopyToReg(Chain, IA64::B6, targetEntryPoint, InFlag); // FLAG these?
408 InFlag = Chain.getValue(1);
409
410 CallOperand = CurDAG->getRegister(IA64::B6, MVT::i64);
411 CallOpcode = IA64::BRCALL_INDIRECT;
412 }
413
414 // Finally, once everything is setup, emit the call itself
415 if(InFlag.Val)
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000416 Chain = SDOperand(CurDAG->getTargetNode(CallOpcode, MVT::Other, MVT::Flag,
417 CallOperand, InFlag), 0);
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000418 else // there might be no arguments
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000419 Chain = SDOperand(CurDAG->getTargetNode(CallOpcode, MVT::Other, MVT::Flag,
420 CallOperand, Chain), 0);
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000421 InFlag = Chain.getValue(1);
422
423 std::vector<SDOperand> CallResults;
424
425 CallResults.push_back(Chain);
426 CallResults.push_back(InFlag);
427
428 for (unsigned i = 0, e = CallResults.size(); i != e; ++i)
429 CodeGenMap[Op.getValue(i)] = CallResults[i];
Evan Cheng34167212006-02-09 00:37:58 +0000430 Result = CallResults[Op.ResNo];
431 return;
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000432 }
Duraid Madinaa36153a2005-12-22 03:58:17 +0000433
Duraid Madina8617f3c2005-12-22 07:14:45 +0000434 case IA64ISD::GETFD: {
Evan Cheng34167212006-02-09 00:37:58 +0000435 SDOperand Input;
436 Select(Input, N->getOperand(0));
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000437 Result = SDOperand(CurDAG->getTargetNode(IA64::GETFD, MVT::i64, Input), 0);
Duraid Madinabf094582006-01-11 03:50:40 +0000438 CodeGenMap[Op] = Result;
Evan Cheng34167212006-02-09 00:37:58 +0000439 return;
Duraid Madina8617f3c2005-12-22 07:14:45 +0000440 }
441
Duraid Madinab6f023a2005-11-21 14:14:54 +0000442 case ISD::FDIV:
443 case ISD::SDIV:
444 case ISD::UDIV:
445 case ISD::SREM:
Evan Cheng34167212006-02-09 00:37:58 +0000446 case ISD::UREM:
447 Result = SelectDIV(Op);
448 return;
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000449
Chris Lattnera54aa942006-01-29 06:26:08 +0000450 case ISD::TargetConstantFP: {
Duraid Madina056728f2005-11-02 07:32:59 +0000451 SDOperand Chain = CurDAG->getEntryNode(); // this is a constant, so..
452
Evan Cheng34167212006-02-09 00:37:58 +0000453 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0)) {
454 Result = CurDAG->getCopyFromReg(Chain, IA64::F0, MVT::f64);
455 } else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0)) {
456 Result = CurDAG->getCopyFromReg(Chain, IA64::F1, MVT::f64);
457 } else
Duraid Madina93856802005-11-02 02:35:04 +0000458 assert(0 && "Unexpected FP constant!");
Evan Cheng34167212006-02-09 00:37:58 +0000459 return;
Duraid Madina93856802005-11-02 02:35:04 +0000460 }
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000461
462 case ISD::FrameIndex: { // TODO: reduce creepyness
463 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Chris Lattnerb19b8992005-11-30 23:02:08 +0000464 if (N->hasOneUse())
Evan Cheng34167212006-02-09 00:37:58 +0000465 Result = CurDAG->SelectNodeTo(N, IA64::MOV, MVT::i64,
Chris Lattnerb19b8992005-11-30 23:02:08 +0000466 CurDAG->getTargetFrameIndex(FI, MVT::i64));
Duraid Madina19e5e142006-01-21 14:27:19 +0000467 else
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000468 Result = CodeGenMap[Op] = SDOperand(CurDAG->getTargetNode(IA64::MOV, MVT::i64,
469 CurDAG->getTargetFrameIndex(FI, MVT::i64)), 0);
Evan Cheng34167212006-02-09 00:37:58 +0000470 return;
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000471 }
472
Duraid Madina2e0348e2006-01-15 09:45:23 +0000473 case ISD::ConstantPool: { // TODO: nuke the constant pool
474 // (ia64 doesn't need one)
Evan Chengb8973bd2006-01-31 22:23:14 +0000475 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
476 Constant *C = CP->get();
477 SDOperand CPI = CurDAG->getTargetConstantPool(C, MVT::i64,
478 CP->getAlignment());
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000479 Result = SDOperand(CurDAG->getTargetNode(IA64::ADDL_GA, MVT::i64, // ?
480 CurDAG->getRegister(IA64::r1, MVT::i64), CPI), 0);
Evan Cheng34167212006-02-09 00:37:58 +0000481 return;
Duraid Madina25d0a882005-10-29 16:08:30 +0000482 }
483
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000484 case ISD::GlobalAddress: {
485 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
486 SDOperand GA = CurDAG->getTargetGlobalAddress(GV, MVT::i64);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000487 SDOperand Tmp = SDOperand(CurDAG->getTargetNode(IA64::ADDL_GA, MVT::i64,
488 CurDAG->getRegister(IA64::r1, MVT::i64), GA), 0);
489 Result = SDOperand(CurDAG->getTargetNode(IA64::LD8, MVT::i64, Tmp), 0);
Evan Cheng34167212006-02-09 00:37:58 +0000490 return;
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000491 }
Duraid Madinaa36153a2005-12-22 03:58:17 +0000492
493/* XXX case ISD::ExternalSymbol: {
494 SDOperand EA = CurDAG->getTargetExternalSymbol(cast<ExternalSymbolSDNode>(N)->getSymbol(),
495 MVT::i64);
496 SDOperand Tmp = CurDAG->getTargetNode(IA64::ADDL_EA, MVT::i64,
497 CurDAG->getRegister(IA64::r1, MVT::i64), EA);
498 return CurDAG->getTargetNode(IA64::LD8, MVT::i64, Tmp);
499 }
500*/
501
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000502 case ISD::LOAD:
Duraid Madinaecc1a1b2006-01-20 16:10:05 +0000503 case ISD::EXTLOAD: // FIXME: load -1, not 1, for bools?
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000504 case ISD::ZEXTLOAD: {
Evan Cheng34167212006-02-09 00:37:58 +0000505 SDOperand Chain, Address;
506 Select(Chain, N->getOperand(0));
507 Select(Address, N->getOperand(1));
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000508
509 MVT::ValueType TypeBeingLoaded = (N->getOpcode() == ISD::LOAD) ?
510 N->getValueType(0) : cast<VTSDNode>(N->getOperand(3))->getVT();
511 unsigned Opc;
512 switch (TypeBeingLoaded) {
513 default: N->dump(); assert(0 && "Cannot load this type!");
Duraid Madina9f729062005-11-04 09:59:06 +0000514 case MVT::i1: { // this is a bool
515 Opc = IA64::LD1; // first we load a byte, then compare for != 0
Evan Cheng34167212006-02-09 00:37:58 +0000516 if(N->getValueType(0) == MVT::i1) { // XXX: early exit!
517 Result = CurDAG->SelectNodeTo(N, IA64::CMPNE, MVT::i1, MVT::Other,
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000518 SDOperand(CurDAG->getTargetNode(Opc, MVT::i64, Address), 0),
Chris Lattnerb19b8992005-11-30 23:02:08 +0000519 CurDAG->getRegister(IA64::r0, MVT::i64),
520 Chain).getValue(Op.ResNo);
Evan Cheng34167212006-02-09 00:37:58 +0000521 return;
522 }
Duraid Madinaa36153a2005-12-22 03:58:17 +0000523 /* otherwise, we want to load a bool into something bigger: LD1
524 will do that for us, so we just fall through */
Chris Lattnerb19b8992005-11-30 23:02:08 +0000525 }
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000526 case MVT::i8: Opc = IA64::LD1; break;
527 case MVT::i16: Opc = IA64::LD2; break;
528 case MVT::i32: Opc = IA64::LD4; break;
529 case MVT::i64: Opc = IA64::LD8; break;
530
531 case MVT::f32: Opc = IA64::LDF4; break;
532 case MVT::f64: Opc = IA64::LDF8; break;
533 }
534
Chris Lattnerb19b8992005-11-30 23:02:08 +0000535 // TODO: comment this
Evan Cheng34167212006-02-09 00:37:58 +0000536 Result = CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), MVT::Other,
Chris Lattnerb19b8992005-11-30 23:02:08 +0000537 Address, Chain).getValue(Op.ResNo);
Evan Cheng34167212006-02-09 00:37:58 +0000538 return;
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000539 }
540
541 case ISD::TRUNCSTORE:
542 case ISD::STORE: {
Evan Cheng34167212006-02-09 00:37:58 +0000543 SDOperand Address, Chain;
544 Select(Address, N->getOperand(2));
545 Select(Chain, N->getOperand(0));
Duraid Madinad525df32005-11-07 03:11:02 +0000546
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000547 unsigned Opc;
548 if (N->getOpcode() == ISD::STORE) {
549 switch (N->getOperand(1).getValueType()) {
Duraid Madinad525df32005-11-07 03:11:02 +0000550 default: assert(0 && "unknown type in store");
551 case MVT::i1: { // this is a bool
552 Opc = IA64::ST1; // we store either 0 or 1 as a byte
Duraid Madina544cbbd2006-01-13 10:28:25 +0000553 // first load zero!
554 SDOperand Initial = CurDAG->getCopyFromReg(Chain, IA64::r0, MVT::i64);
555 Chain = Initial.getValue(1);
Duraid Madinaa7fb5be2006-01-20 03:40:25 +0000556 // then load 1 into the same reg iff the predicate to store is 1
Evan Cheng34167212006-02-09 00:37:58 +0000557 SDOperand Tmp;
558 Select(Tmp, N->getOperand(1));
Duraid Madinab20f9792006-02-11 07:33:17 +0000559 Tmp = SDOperand(CurDAG->getTargetNode(IA64::TPCADDS, MVT::i64, Initial,
560 CurDAG->getConstant(1, MVT::i64),
561 Tmp), 0);
Evan Cheng34167212006-02-09 00:37:58 +0000562 Result = CurDAG->SelectNodeTo(N, Opc, MVT::Other, Address, Tmp, Chain);
563 return;
Chris Lattnerb19b8992005-11-30 23:02:08 +0000564 }
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000565 case MVT::i64: Opc = IA64::ST8; break;
566 case MVT::f64: Opc = IA64::STF8; break;
Duraid Madinad525df32005-11-07 03:11:02 +0000567 }
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000568 } else { //ISD::TRUNCSTORE
569 switch(cast<VTSDNode>(N->getOperand(4))->getVT()) {
Duraid Madinad525df32005-11-07 03:11:02 +0000570 default: assert(0 && "unknown type in truncstore");
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000571 case MVT::i8: Opc = IA64::ST1; break;
572 case MVT::i16: Opc = IA64::ST2; break;
573 case MVT::i32: Opc = IA64::ST4; break;
574 case MVT::f32: Opc = IA64::STF4; break;
575 }
576 }
577
Evan Cheng34167212006-02-09 00:37:58 +0000578 SDOperand N1, N2;
579 Select(N1, N->getOperand(1));
580 Select(N2, N->getOperand(2));
581 Result = CurDAG->SelectNodeTo(N, Opc, MVT::Other, N2, N1, Chain);
582 return;
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000583 }
584
585 case ISD::BRCOND: {
Evan Cheng34167212006-02-09 00:37:58 +0000586 SDOperand Chain, CC;
587 Select(Chain, N->getOperand(0));
588 Select(CC, N->getOperand(1));
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000589 MachineBasicBlock *Dest =
590 cast<BasicBlockSDNode>(N->getOperand(2))->getBasicBlock();
591 //FIXME - we do NOT need long branches all the time
Evan Cheng34167212006-02-09 00:37:58 +0000592 Result = CurDAG->SelectNodeTo(N, IA64::BRLCOND_NOTCALL, MVT::Other, CC,
Chris Lattnerb19b8992005-11-30 23:02:08 +0000593 CurDAG->getBasicBlock(Dest), Chain);
Evan Cheng34167212006-02-09 00:37:58 +0000594 return;
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000595 }
596
597 case ISD::CALLSEQ_START:
598 case ISD::CALLSEQ_END: {
599 int64_t Amt = cast<ConstantSDNode>(N->getOperand(1))->getValue();
600 unsigned Opc = N->getOpcode() == ISD::CALLSEQ_START ?
601 IA64::ADJUSTCALLSTACKDOWN : IA64::ADJUSTCALLSTACKUP;
Evan Cheng34167212006-02-09 00:37:58 +0000602 SDOperand N0;
603 Select(N0, N->getOperand(0));
604 Result = CurDAG->SelectNodeTo(N, Opc, MVT::Other, getI64Imm(Amt), N0);
605 return;
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000606 }
607
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000608 case ISD::BR:
609 // FIXME: we don't need long branches all the time!
Evan Cheng34167212006-02-09 00:37:58 +0000610 SDOperand N0;
611 Select(N0, N->getOperand(0));
612 Result = CurDAG->SelectNodeTo(N, IA64::BRL_NOTCALL, MVT::Other,
613 N->getOperand(1), N0);
614 return;
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000615 }
616
Evan Cheng34167212006-02-09 00:37:58 +0000617 SelectCode(Result, Op);
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000618}
619
620
621/// createIA64DAGToDAGInstructionSelector - This pass converts a legalized DAG
622/// into an IA64-specific DAG, ready for instruction scheduling.
623///
624FunctionPass *llvm::createIA64DAGToDAGInstructionSelector(TargetMachine &TM) {
625 return new IA64DAGToDAGISel(TM);
626}
627