blob: 3fb7b2a3b8a5351dc90a8da71a20c3d20ad56f10 [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 Chengba2f0a92006-02-05 06:46:41 +000031#include <set>
Duraid Madinaf2db9b82005-10-28 17:46:35 +000032using namespace llvm;
33
34namespace {
35 Statistic<> FusedFP ("ia64-codegen", "Number of fused fp operations");
36 Statistic<> FrameOff("ia64-codegen", "Number of frame idx offsets collapsed");
37
38 //===--------------------------------------------------------------------===//
39 /// IA64DAGToDAGISel - IA64 specific code to select IA64 machine
40 /// instructions for SelectionDAG operations.
41 ///
42 class IA64DAGToDAGISel : public SelectionDAGISel {
43 IA64TargetLowering IA64Lowering;
44 unsigned GlobalBaseReg;
45 public:
Evan Chengc4c62572006-03-13 23:20:37 +000046 IA64DAGToDAGISel(IA64TargetMachine &TM)
47 : SelectionDAGISel(IA64Lowering), IA64Lowering(*TM.getTargetLowering()) {}
Duraid Madinaf2db9b82005-10-28 17:46:35 +000048
49 virtual bool runOnFunction(Function &Fn) {
50 // Make sure we re-emit a set of the global base reg if necessary
51 GlobalBaseReg = 0;
52 return SelectionDAGISel::runOnFunction(Fn);
53 }
54
55 /// getI64Imm - Return a target constant with the specified value, of type
56 /// i64.
57 inline SDOperand getI64Imm(uint64_t Imm) {
58 return CurDAG->getTargetConstant(Imm, MVT::i64);
59 }
60
61 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
62 /// base register. Return the virtual register that holds this value.
63 // SDOperand getGlobalBaseReg(); TODO: hmm
64
65 // Select - Convert the specified operand from a target-independent to a
66 // target-specific node if it hasn't already been changed.
Evan Cheng34167212006-02-09 00:37:58 +000067 void Select(SDOperand &Result, SDOperand N);
Duraid Madinaf2db9b82005-10-28 17:46:35 +000068
69 SDNode *SelectIntImmediateExpr(SDOperand LHS, SDOperand RHS,
70 unsigned OCHi, unsigned OCLo,
71 bool IsArithmetic = false,
72 bool Negate = false);
73 SDNode *SelectBitfieldInsert(SDNode *N);
74
75 /// SelectCC - Select a comparison of the specified values with the
76 /// specified condition code, returning the CR# of the expression.
77 SDOperand SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC);
78
79 /// SelectAddr - Given the specified address, return the two operands for a
80 /// load/store instruction, and return true if it should be an indexed [r+r]
81 /// operation.
82 bool SelectAddr(SDOperand Addr, SDOperand &Op1, SDOperand &Op2);
83
Duraid Madinaf2db9b82005-10-28 17:46:35 +000084 /// InstructionSelectBasicBlock - This callback is invoked by
85 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
86 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
87
88 virtual const char *getPassName() const {
89 return "IA64 (Itanium) DAG->DAG Instruction Selector";
90 }
91
92// Include the pieces autogenerated from the target description.
93#include "IA64GenDAGISel.inc"
94
95private:
Duraid Madinab6f023a2005-11-21 14:14:54 +000096 SDOperand SelectDIV(SDOperand Op);
Duraid Madinaf2db9b82005-10-28 17:46:35 +000097 };
98}
99
100/// InstructionSelectBasicBlock - This callback is invoked by
101/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
102void IA64DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
103 DEBUG(BB->dump());
104
105 // The selection process is inherently a bottom-up recursive process (users
106 // select their uses before themselves). Given infinite stack space, we
107 // could just start selecting on the root and traverse the whole graph. In
108 // practice however, this causes us to run out of stack space on large basic
109 // blocks. To avoid this problem, select the entry node, then all its uses,
110 // iteratively instead of recursively.
111 std::vector<SDOperand> Worklist;
112 Worklist.push_back(DAG.getEntryNode());
113
114 // Note that we can do this in the IA64 target (scanning forward across token
115 // chain edges) because no nodes ever get folded across these edges. On a
116 // target like X86 which supports load/modify/store operations, this would
117 // have to be more careful.
118 while (!Worklist.empty()) {
119 SDOperand Node = Worklist.back();
120 Worklist.pop_back();
Evan Cheng33e9ad92006-07-27 06:40:15 +0000121
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000122 if ((Node.Val->getOpcode() >= ISD::BUILTIN_OP_END &&
123 Node.Val->getOpcode() < IA64ISD::FIRST_NUMBER) ||
124 CodeGenMap.count(Node)) continue;
125
126 for (SDNode::use_iterator UI = Node.Val->use_begin(),
127 E = Node.Val->use_end(); UI != E; ++UI) {
128 // Scan the values. If this use has a value that is a token chain, add it
129 // to the worklist.
130 SDNode *User = *UI;
131 for (unsigned i = 0, e = User->getNumValues(); i != e; ++i)
132 if (User->getValueType(i) == MVT::Other) {
133 Worklist.push_back(SDOperand(User, i));
134 break;
135 }
136 }
137
138 // Finally, legalize this node.
Evan Cheng34167212006-02-09 00:37:58 +0000139 SDOperand Dummy;
140 Select(Dummy, Node);
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000141 }
142
143 // Select target instructions for the DAG.
Evan Chengba2f0a92006-02-05 06:46:41 +0000144 DAG.setRoot(SelectRoot(DAG.getRoot()));
Evan Cheng6a3d5a62006-05-25 00:24:28 +0000145 assert(InFlightSet.empty() && "ISel InFlightSet has not been emptied!");
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000146 CodeGenMap.clear();
Evan Chengafe358e2006-05-24 20:46:25 +0000147 HandleMap.clear();
148 ReplaceMap.clear();
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000149 DAG.RemoveDeadNodes();
150
151 // Emit machine code to BB.
152 ScheduleAndEmitDAG(DAG);
153}
154
Duraid Madinab6f023a2005-11-21 14:14:54 +0000155SDOperand IA64DAGToDAGISel::SelectDIV(SDOperand Op) {
156 SDNode *N = Op.Val;
Evan Cheng34167212006-02-09 00:37:58 +0000157 SDOperand Chain, Tmp1, Tmp2;
158 Select(Chain, N->getOperand(0));
Duraid Madinab6f023a2005-11-21 14:14:54 +0000159
Evan Cheng34167212006-02-09 00:37:58 +0000160 Select(Tmp1, N->getOperand(0));
161 Select(Tmp2, N->getOperand(1));
Duraid Madinab6f023a2005-11-21 14:14:54 +0000162
163 bool isFP=false;
164
165 if(MVT::isFloatingPoint(Tmp1.getValueType()))
166 isFP=true;
167
168 bool isModulus=false; // is it a division or a modulus?
169 bool isSigned=false;
170
171 switch(N->getOpcode()) {
172 case ISD::FDIV:
173 case ISD::SDIV: isModulus=false; isSigned=true; break;
174 case ISD::UDIV: isModulus=false; isSigned=false; break;
175 case ISD::FREM:
176 case ISD::SREM: isModulus=true; isSigned=true; break;
177 case ISD::UREM: isModulus=true; isSigned=false; break;
178 }
179
180 // TODO: check for integer divides by powers of 2 (or other simple patterns?)
181
182 SDOperand TmpPR, TmpPR2;
183 SDOperand TmpF1, TmpF2, TmpF3, TmpF4, TmpF5, TmpF6, TmpF7, TmpF8;
184 SDOperand TmpF9, TmpF10,TmpF11,TmpF12,TmpF13,TmpF14,TmpF15;
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000185 SDNode *Result;
Duraid Madina76bb6ae2006-01-16 14:33:04 +0000186
187 // we'll need copies of F0 and F1
188 SDOperand F0 = CurDAG->getRegister(IA64::F0, MVT::f64);
189 SDOperand F1 = CurDAG->getRegister(IA64::F1, MVT::f64);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000190
191 // OK, emit some code:
192
193 if(!isFP) {
194 // first, load the inputs into FP regs.
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000195 TmpF1 =
196 SDOperand(CurDAG->getTargetNode(IA64::SETFSIG, MVT::f64, Tmp1), 0);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000197 Chain = TmpF1.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000198 TmpF2 =
199 SDOperand(CurDAG->getTargetNode(IA64::SETFSIG, MVT::f64, Tmp2), 0);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000200 Chain = TmpF2.getValue(1);
201
202 // next, convert the inputs to FP
203 if(isSigned) {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000204 TmpF3 =
205 SDOperand(CurDAG->getTargetNode(IA64::FCVTXF, MVT::f64, TmpF1), 0);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000206 Chain = TmpF3.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000207 TmpF4 =
208 SDOperand(CurDAG->getTargetNode(IA64::FCVTXF, MVT::f64, TmpF2), 0);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000209 Chain = TmpF4.getValue(1);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000210 } else { // is unsigned
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000211 TmpF3 =
212 SDOperand(CurDAG->getTargetNode(IA64::FCVTXUFS1, MVT::f64, TmpF1), 0);
Duraid Madina76bb6ae2006-01-16 14:33:04 +0000213 Chain = TmpF3.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000214 TmpF4 =
215 SDOperand(CurDAG->getTargetNode(IA64::FCVTXUFS1, MVT::f64, TmpF2), 0);
Duraid Madina76bb6ae2006-01-16 14:33:04 +0000216 Chain = TmpF4.getValue(1);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000217 }
218
219 } else { // this is an FP divide/remainder, so we 'leak' some temp
220 // regs and assign TmpF3=Tmp1, TmpF4=Tmp2
221 TmpF3=Tmp1;
222 TmpF4=Tmp2;
223 }
224
225 // we start by computing an approximate reciprocal (good to 9 bits?)
226 // note, this instruction writes _both_ TmpF5 (answer) and TmpPR (predicate)
Duraid Madina0c81dc82006-01-16 06:33:38 +0000227 if(isFP)
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000228 TmpF5 = SDOperand(CurDAG->getTargetNode(IA64::FRCPAS0, MVT::f64, MVT::i1,
229 TmpF3, TmpF4), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000230 else
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000231 TmpF5 = SDOperand(CurDAG->getTargetNode(IA64::FRCPAS1, MVT::f64, MVT::i1,
232 TmpF3, TmpF4), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000233
Duraid Madinab6f023a2005-11-21 14:14:54 +0000234 TmpPR = TmpF5.getValue(1);
235 Chain = TmpF5.getValue(2);
236
Duraid Madina0c81dc82006-01-16 06:33:38 +0000237 SDOperand minusB;
238 if(isModulus) { // for remainders, it'll be handy to have
239 // copies of -input_b
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000240 minusB = SDOperand(CurDAG->getTargetNode(IA64::SUB, MVT::i64,
241 CurDAG->getRegister(IA64::r0, MVT::i64), Tmp2), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000242 Chain = minusB.getValue(1);
243 }
244
245 SDOperand TmpE0, TmpY1, TmpE1, TmpY2;
246
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000247 TmpE0 = SDOperand(CurDAG->getTargetNode(IA64::CFNMAS1, MVT::f64,
248 TmpF4, TmpF5, F1, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000249 Chain = TmpE0.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000250 TmpY1 = SDOperand(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
251 TmpF5, TmpE0, TmpF5, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000252 Chain = TmpY1.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000253 TmpE1 = SDOperand(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
254 TmpE0, TmpE0, F0, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000255 Chain = TmpE1.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000256 TmpY2 = SDOperand(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
257 TmpY1, TmpE1, TmpY1, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000258 Chain = TmpY2.getValue(1);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000259
Duraid Madina0c81dc82006-01-16 06:33:38 +0000260 if(isFP) { // if this is an FP divide, we finish up here and exit early
261 if(isModulus)
262 assert(0 && "Sorry, try another FORTRAN compiler.");
263
264 SDOperand TmpE2, TmpY3, TmpQ0, TmpR0;
265
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000266 TmpE2 = SDOperand(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
267 TmpE1, TmpE1, F0, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000268 Chain = TmpE2.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000269 TmpY3 = SDOperand(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
270 TmpY2, TmpE2, TmpY2, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000271 Chain = TmpY3.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000272 TmpQ0 =
273 SDOperand(CurDAG->getTargetNode(IA64::CFMADS1, MVT::f64, // double prec!
274 Tmp1, TmpY3, F0, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000275 Chain = TmpQ0.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000276 TmpR0 =
277 SDOperand(CurDAG->getTargetNode(IA64::CFNMADS1, MVT::f64, // double prec!
278 Tmp2, TmpQ0, Tmp1, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000279 Chain = TmpR0.getValue(1);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000280
Duraid Madina0c81dc82006-01-16 06:33:38 +0000281// we want Result to have the same target register as the frcpa, so
282// we two-address hack it. See the comment "for this to work..." on
283// page 48 of Intel application note #245415
284 Result = CurDAG->getTargetNode(IA64::TCFMADS0, MVT::f64, // d.p. s0 rndg!
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000285 TmpF5, TmpY3, TmpR0, TmpQ0, TmpPR);
286 Chain = SDOperand(Result, 1);
287 return SDOperand(Result, 0); // XXX: early exit!
Duraid Madina0c81dc82006-01-16 06:33:38 +0000288 } else { // this is *not* an FP divide, so there's a bit left to do:
289
290 SDOperand TmpQ2, TmpR2, TmpQ3, TmpQ;
291
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000292 TmpQ2 = SDOperand(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
293 TmpF3, TmpY2, F0, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000294 Chain = TmpQ2.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000295 TmpR2 = SDOperand(CurDAG->getTargetNode(IA64::CFNMAS1, MVT::f64,
296 TmpF4, TmpQ2, TmpF3, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000297 Chain = TmpR2.getValue(1);
Duraid Madinaae6dcdd2006-01-17 01:19:49 +0000298
Duraid Madina76bb6ae2006-01-16 14:33:04 +0000299// we want TmpQ3 to have the same target register as the frcpa? maybe we
300// should two-address hack it. See the comment "for this to work..." on page
301// 48 of Intel application note #245415
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000302 TmpQ3 = SDOperand(CurDAG->getTargetNode(IA64::TCFMAS1, MVT::f64,
303 TmpF5, TmpR2, TmpY2, TmpQ2, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000304 Chain = TmpQ3.getValue(1);
Duraid Madina76bb6ae2006-01-16 14:33:04 +0000305
Duraid Madinaae6dcdd2006-01-17 01:19:49 +0000306 // STORY: without these two-address instructions (TCFMAS1 and TCFMADS0)
307 // the FPSWA won't be able to help out in the case of large/tiny
308 // arguments. Other fun bugs may also appear, e.g. 0/x = x, not 0.
309
Duraid Madina0c81dc82006-01-16 06:33:38 +0000310 if(isSigned)
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000311 TmpQ = SDOperand(CurDAG->getTargetNode(IA64::FCVTFXTRUNCS1,
312 MVT::f64, TmpQ3), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000313 else
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000314 TmpQ = SDOperand(CurDAG->getTargetNode(IA64::FCVTFXUTRUNCS1,
315 MVT::f64, TmpQ3), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000316
317 Chain = TmpQ.getValue(1);
318
319 if(isModulus) {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000320 SDOperand FPminusB =
321 SDOperand(CurDAG->getTargetNode(IA64::SETFSIG, MVT::f64, minusB), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000322 Chain = FPminusB.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000323 SDOperand Remainder =
324 SDOperand(CurDAG->getTargetNode(IA64::XMAL, MVT::f64,
325 TmpQ, FPminusB, TmpF1), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000326 Chain = Remainder.getValue(1);
327 Result = CurDAG->getTargetNode(IA64::GETFSIG, MVT::i64, Remainder);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000328 Chain = SDOperand(Result, 1);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000329 } else { // just an integer divide
330 Result = CurDAG->getTargetNode(IA64::GETFSIG, MVT::i64, TmpQ);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000331 Chain = SDOperand(Result, 1);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000332 }
333
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000334 return SDOperand(Result, 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000335 } // wasn't an FP divide
Duraid Madinab6f023a2005-11-21 14:14:54 +0000336}
337
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000338// Select - Convert the specified operand from a target-independent to a
339// target-specific node if it hasn't already been changed.
Evan Cheng34167212006-02-09 00:37:58 +0000340void IA64DAGToDAGISel::Select(SDOperand &Result, SDOperand Op) {
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000341 SDNode *N = Op.Val;
342 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
Evan Cheng34167212006-02-09 00:37:58 +0000343 N->getOpcode() < IA64ISD::FIRST_NUMBER) {
344 Result = Op;
345 return; // Already selected.
346 }
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000347
348 // If this has already been converted, use it.
349 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(Op);
Evan Cheng34167212006-02-09 00:37:58 +0000350 if (CGMI != CodeGenMap.end()) {
351 Result = CGMI->second;
352 return;
353 }
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000354
355 switch (N->getOpcode()) {
356 default: break;
357
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000358 case IA64ISD::BRCALL: { // XXX: this is also a hack!
Evan Cheng34167212006-02-09 00:37:58 +0000359 SDOperand Chain;
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000360 SDOperand InFlag; // Null incoming flag value.
361
Evan Cheng34167212006-02-09 00:37:58 +0000362 Select(Chain, N->getOperand(0));
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000363 if(N->getNumOperands()==3) // we have an incoming chain, callee and flag
Evan Cheng34167212006-02-09 00:37:58 +0000364 Select(InFlag, N->getOperand(2));
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000365
366 unsigned CallOpcode;
367 SDOperand CallOperand;
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000368
369 // if we can call directly, do so
370 if (GlobalAddressSDNode *GASD =
371 dyn_cast<GlobalAddressSDNode>(N->getOperand(1))) {
372 CallOpcode = IA64::BRCALL_IPREL_GA;
373 CallOperand = CurDAG->getTargetGlobalAddress(GASD->getGlobal(), MVT::i64);
374 } else if (ExternalSymbolSDNode *ESSDN = // FIXME: we currently NEED this
375 // case for correctness, to avoid
376 // "non-pic code with imm reloc.n
377 // against dynamic symbol" errors
378 dyn_cast<ExternalSymbolSDNode>(N->getOperand(1))) {
379 CallOpcode = IA64::BRCALL_IPREL_ES;
380 CallOperand = N->getOperand(1);
381 } else {
382 // otherwise we need to load the function descriptor,
383 // load the branch target (function)'s entry point and GP,
384 // branch (call) then restore the GP
Evan Cheng34167212006-02-09 00:37:58 +0000385 SDOperand FnDescriptor;
386 Select(FnDescriptor, N->getOperand(1));
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000387
388 // load the branch target's entry point [mem] and
389 // GP value [mem+8]
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000390 SDOperand targetEntryPoint=
391 SDOperand(CurDAG->getTargetNode(IA64::LD8, MVT::i64, FnDescriptor), 0);
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000392 Chain = targetEntryPoint.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000393 SDOperand targetGPAddr=
394 SDOperand(CurDAG->getTargetNode(IA64::ADDS, MVT::i64,
395 FnDescriptor, CurDAG->getConstant(8, MVT::i64)), 0);
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000396 Chain = targetGPAddr.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000397 SDOperand targetGP=
398 SDOperand(CurDAG->getTargetNode(IA64::LD8, MVT::i64, targetGPAddr), 0);
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000399 Chain = targetGP.getValue(1);
400
401 Chain = CurDAG->getCopyToReg(Chain, IA64::r1, targetGP, InFlag);
402 InFlag = Chain.getValue(1);
403 Chain = CurDAG->getCopyToReg(Chain, IA64::B6, targetEntryPoint, InFlag); // FLAG these?
404 InFlag = Chain.getValue(1);
405
406 CallOperand = CurDAG->getRegister(IA64::B6, MVT::i64);
407 CallOpcode = IA64::BRCALL_INDIRECT;
408 }
409
410 // Finally, once everything is setup, emit the call itself
411 if(InFlag.Val)
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000412 Chain = SDOperand(CurDAG->getTargetNode(CallOpcode, MVT::Other, MVT::Flag,
413 CallOperand, InFlag), 0);
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000414 else // there might be no arguments
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000415 Chain = SDOperand(CurDAG->getTargetNode(CallOpcode, MVT::Other, MVT::Flag,
416 CallOperand, Chain), 0);
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000417 InFlag = Chain.getValue(1);
418
419 std::vector<SDOperand> CallResults;
420
421 CallResults.push_back(Chain);
422 CallResults.push_back(InFlag);
423
424 for (unsigned i = 0, e = CallResults.size(); i != e; ++i)
425 CodeGenMap[Op.getValue(i)] = CallResults[i];
Evan Cheng34167212006-02-09 00:37:58 +0000426 Result = CallResults[Op.ResNo];
427 return;
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000428 }
Duraid Madinaa36153a2005-12-22 03:58:17 +0000429
Duraid Madina8617f3c2005-12-22 07:14:45 +0000430 case IA64ISD::GETFD: {
Evan Cheng34167212006-02-09 00:37:58 +0000431 SDOperand Input;
432 Select(Input, N->getOperand(0));
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000433 Result = SDOperand(CurDAG->getTargetNode(IA64::GETFD, MVT::i64, Input), 0);
Duraid Madinabf094582006-01-11 03:50:40 +0000434 CodeGenMap[Op] = Result;
Evan Cheng34167212006-02-09 00:37:58 +0000435 return;
Duraid Madina8617f3c2005-12-22 07:14:45 +0000436 }
437
Duraid Madinab6f023a2005-11-21 14:14:54 +0000438 case ISD::FDIV:
439 case ISD::SDIV:
440 case ISD::UDIV:
441 case ISD::SREM:
Evan Cheng34167212006-02-09 00:37:58 +0000442 case ISD::UREM:
443 Result = SelectDIV(Op);
444 return;
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000445
Chris Lattnera54aa942006-01-29 06:26:08 +0000446 case ISD::TargetConstantFP: {
Duraid Madina056728f2005-11-02 07:32:59 +0000447 SDOperand Chain = CurDAG->getEntryNode(); // this is a constant, so..
448
Evan Cheng34167212006-02-09 00:37:58 +0000449 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0)) {
450 Result = CurDAG->getCopyFromReg(Chain, IA64::F0, MVT::f64);
451 } else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0)) {
452 Result = CurDAG->getCopyFromReg(Chain, IA64::F1, MVT::f64);
453 } else
Duraid Madina93856802005-11-02 02:35:04 +0000454 assert(0 && "Unexpected FP constant!");
Evan Cheng34167212006-02-09 00:37:58 +0000455 return;
Duraid Madina93856802005-11-02 02:35:04 +0000456 }
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000457
458 case ISD::FrameIndex: { // TODO: reduce creepyness
459 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Chris Lattnerb19b8992005-11-30 23:02:08 +0000460 if (N->hasOneUse())
Evan Cheng34167212006-02-09 00:37:58 +0000461 Result = CurDAG->SelectNodeTo(N, IA64::MOV, MVT::i64,
Chris Lattnerb19b8992005-11-30 23:02:08 +0000462 CurDAG->getTargetFrameIndex(FI, MVT::i64));
Duraid Madina19e5e142006-01-21 14:27:19 +0000463 else
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000464 Result = CodeGenMap[Op] = SDOperand(CurDAG->getTargetNode(IA64::MOV, MVT::i64,
465 CurDAG->getTargetFrameIndex(FI, MVT::i64)), 0);
Evan Cheng34167212006-02-09 00:37:58 +0000466 return;
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000467 }
468
Duraid Madina2e0348e2006-01-15 09:45:23 +0000469 case ISD::ConstantPool: { // TODO: nuke the constant pool
470 // (ia64 doesn't need one)
Evan Chengb8973bd2006-01-31 22:23:14 +0000471 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
472 Constant *C = CP->get();
473 SDOperand CPI = CurDAG->getTargetConstantPool(C, MVT::i64,
474 CP->getAlignment());
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000475 Result = SDOperand(CurDAG->getTargetNode(IA64::ADDL_GA, MVT::i64, // ?
476 CurDAG->getRegister(IA64::r1, MVT::i64), CPI), 0);
Evan Cheng34167212006-02-09 00:37:58 +0000477 return;
Duraid Madina25d0a882005-10-29 16:08:30 +0000478 }
479
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000480 case ISD::GlobalAddress: {
481 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
482 SDOperand GA = CurDAG->getTargetGlobalAddress(GV, MVT::i64);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000483 SDOperand Tmp = SDOperand(CurDAG->getTargetNode(IA64::ADDL_GA, MVT::i64,
484 CurDAG->getRegister(IA64::r1, MVT::i64), GA), 0);
485 Result = SDOperand(CurDAG->getTargetNode(IA64::LD8, MVT::i64, Tmp), 0);
Evan Cheng34167212006-02-09 00:37:58 +0000486 return;
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000487 }
Duraid Madinaa36153a2005-12-22 03:58:17 +0000488
489/* XXX case ISD::ExternalSymbol: {
490 SDOperand EA = CurDAG->getTargetExternalSymbol(cast<ExternalSymbolSDNode>(N)->getSymbol(),
491 MVT::i64);
492 SDOperand Tmp = CurDAG->getTargetNode(IA64::ADDL_EA, MVT::i64,
493 CurDAG->getRegister(IA64::r1, MVT::i64), EA);
494 return CurDAG->getTargetNode(IA64::LD8, MVT::i64, Tmp);
495 }
496*/
497
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000498 case ISD::LOAD:
Duraid Madinaecc1a1b2006-01-20 16:10:05 +0000499 case ISD::EXTLOAD: // FIXME: load -1, not 1, for bools?
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000500 case ISD::ZEXTLOAD: {
Evan Cheng34167212006-02-09 00:37:58 +0000501 SDOperand Chain, Address;
502 Select(Chain, N->getOperand(0));
503 Select(Address, N->getOperand(1));
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000504
505 MVT::ValueType TypeBeingLoaded = (N->getOpcode() == ISD::LOAD) ?
506 N->getValueType(0) : cast<VTSDNode>(N->getOperand(3))->getVT();
507 unsigned Opc;
508 switch (TypeBeingLoaded) {
Jim Laskey16d42c62006-07-11 18:25:13 +0000509 default:
510#ifndef NDEBUG
511 N->dump();
512#endif
513 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///
Evan Chengc4c62572006-03-13 23:20:37 +0000624FunctionPass
625*llvm::createIA64DAGToDAGInstructionSelector(IA64TargetMachine &TM) {
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000626 return new IA64DAGToDAGISel(TM);
627}
628