blob: 9800c506ca9eabd297853b0a05b6c75228199093 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===---- IA64ISelDAGToDAG.cpp - IA64 pattern matching inst selector ------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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#define DEBUG_TYPE "ia64-codegen"
16#include "IA64.h"
17#include "IA64TargetMachine.h"
18#include "IA64ISelLowering.h"
19#include "llvm/CodeGen/MachineInstrBuilder.h"
20#include "llvm/CodeGen/MachineFunction.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include "llvm/CodeGen/SelectionDAG.h"
22#include "llvm/CodeGen/SelectionDAGISel.h"
23#include "llvm/Target/TargetOptions.h"
24#include "llvm/Constants.h"
25#include "llvm/GlobalValue.h"
26#include "llvm/Intrinsics.h"
Chris Lattner93c741a2008-02-03 05:43:57 +000027#include "llvm/Support/Compiler.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028#include "llvm/Support/Debug.h"
29#include "llvm/Support/MathExtras.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030using namespace llvm;
31
32namespace {
33 //===--------------------------------------------------------------------===//
34 /// IA64DAGToDAGISel - IA64 specific code to select IA64 machine
35 /// instructions for SelectionDAG operations.
36 ///
37 class IA64DAGToDAGISel : public SelectionDAGISel {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038 unsigned GlobalBaseReg;
39 public:
Dan Gohmane887fdf2008-07-07 18:00:37 +000040 explicit IA64DAGToDAGISel(IA64TargetMachine &TM)
Dan Gohman96eb47a2009-01-15 19:20:50 +000041 : SelectionDAGISel(TM) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000042
43 virtual bool runOnFunction(Function &Fn) {
44 // Make sure we re-emit a set of the global base reg if necessary
45 GlobalBaseReg = 0;
46 return SelectionDAGISel::runOnFunction(Fn);
47 }
48
49 /// getI64Imm - Return a target constant with the specified value, of type
50 /// i64.
Dan Gohman8181bd12008-07-27 21:46:04 +000051 inline SDValue getI64Imm(uint64_t Imm) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000052 return CurDAG->getTargetConstant(Imm, MVT::i64);
53 }
54
55 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
56 /// base register. Return the virtual register that holds this value.
Dan Gohman8181bd12008-07-27 21:46:04 +000057 // SDValue getGlobalBaseReg(); TODO: hmm
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058
59 // Select - Convert the specified operand from a target-independent to a
60 // target-specific node if it hasn't already been changed.
Dan Gohman8181bd12008-07-27 21:46:04 +000061 SDNode *Select(SDValue N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062
Dan Gohman8181bd12008-07-27 21:46:04 +000063 SDNode *SelectIntImmediateExpr(SDValue LHS, SDValue RHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000064 unsigned OCHi, unsigned OCLo,
65 bool IsArithmetic = false,
66 bool Negate = false);
67 SDNode *SelectBitfieldInsert(SDNode *N);
68
69 /// SelectCC - Select a comparison of the specified values with the
70 /// specified condition code, returning the CR# of the expression.
Dan Gohman8181bd12008-07-27 21:46:04 +000071 SDValue SelectCC(SDValue LHS, SDValue RHS, ISD::CondCode CC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000072
73 /// SelectAddr - Given the specified address, return the two operands for a
74 /// load/store instruction, and return true if it should be an indexed [r+r]
75 /// operation.
Dan Gohman8181bd12008-07-27 21:46:04 +000076 bool SelectAddr(SDValue Addr, SDValue &Op1, SDValue &Op2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077
Evan Cheng34fd4f32008-06-30 20:45:06 +000078 /// InstructionSelect - This callback is invoked by
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohman14a66442008-08-23 02:25:05 +000080 virtual void InstructionSelect();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081
82 virtual const char *getPassName() const {
83 return "IA64 (Itanium) DAG->DAG Instruction Selector";
84 }
85
86// Include the pieces autogenerated from the target description.
87#include "IA64GenDAGISel.inc"
88
89private:
Dan Gohman8181bd12008-07-27 21:46:04 +000090 SDNode *SelectDIV(SDValue Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091 };
92}
93
Evan Cheng34fd4f32008-06-30 20:45:06 +000094/// InstructionSelect - This callback is invoked by
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohman14a66442008-08-23 02:25:05 +000096void IA64DAGToDAGISel::InstructionSelect() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097 DEBUG(BB->dump());
98
99 // Select target instructions for the DAG.
David Greene932618b2008-10-27 21:56:29 +0000100 SelectRoot(*CurDAG);
Dan Gohman14a66442008-08-23 02:25:05 +0000101 CurDAG->RemoveDeadNodes();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102}
103
Dan Gohman8181bd12008-07-27 21:46:04 +0000104SDNode *IA64DAGToDAGISel::SelectDIV(SDValue Op) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000105 SDNode *N = Op.getNode();
Dan Gohman8181bd12008-07-27 21:46:04 +0000106 SDValue Chain = N->getOperand(0);
107 SDValue Tmp1 = N->getOperand(0);
108 SDValue Tmp2 = N->getOperand(1);
Dale Johannesen913ba762009-02-06 01:31:28 +0000109 DebugLoc dl = N->getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110
111 bool isFP=false;
112
Duncan Sands92c43912008-06-06 12:08:01 +0000113 if(Tmp1.getValueType().isFloatingPoint())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114 isFP=true;
115
116 bool isModulus=false; // is it a division or a modulus?
117 bool isSigned=false;
118
119 switch(N->getOpcode()) {
120 case ISD::FDIV:
121 case ISD::SDIV: isModulus=false; isSigned=true; break;
122 case ISD::UDIV: isModulus=false; isSigned=false; break;
123 case ISD::FREM:
124 case ISD::SREM: isModulus=true; isSigned=true; break;
125 case ISD::UREM: isModulus=true; isSigned=false; break;
126 }
127
128 // TODO: check for integer divides by powers of 2 (or other simple patterns?)
129
Dan Gohman8181bd12008-07-27 21:46:04 +0000130 SDValue TmpPR, TmpPR2;
131 SDValue TmpF1, TmpF2, TmpF3, TmpF4, TmpF5, TmpF6, TmpF7, TmpF8;
132 SDValue TmpF9, TmpF10,TmpF11,TmpF12,TmpF13,TmpF14,TmpF15;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133 SDNode *Result;
134
135 // we'll need copies of F0 and F1
Dan Gohman8181bd12008-07-27 21:46:04 +0000136 SDValue F0 = CurDAG->getRegister(IA64::F0, MVT::f64);
137 SDValue F1 = CurDAG->getRegister(IA64::F1, MVT::f64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138
139 // OK, emit some code:
140
141 if(!isFP) {
142 // first, load the inputs into FP regs.
143 TmpF1 =
Dale Johannesen913ba762009-02-06 01:31:28 +0000144 SDValue(CurDAG->getTargetNode(IA64::SETFSIG, dl, MVT::f64, Tmp1), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000145 Chain = TmpF1.getValue(1);
146 TmpF2 =
Dale Johannesen913ba762009-02-06 01:31:28 +0000147 SDValue(CurDAG->getTargetNode(IA64::SETFSIG, dl, MVT::f64, Tmp2), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000148 Chain = TmpF2.getValue(1);
149
150 // next, convert the inputs to FP
151 if(isSigned) {
152 TmpF3 =
Dale Johannesen913ba762009-02-06 01:31:28 +0000153 SDValue(CurDAG->getTargetNode(IA64::FCVTXF, dl, MVT::f64, TmpF1), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000154 Chain = TmpF3.getValue(1);
155 TmpF4 =
Dale Johannesen913ba762009-02-06 01:31:28 +0000156 SDValue(CurDAG->getTargetNode(IA64::FCVTXF, dl, MVT::f64, TmpF2), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000157 Chain = TmpF4.getValue(1);
158 } else { // is unsigned
159 TmpF3 =
Dale Johannesen913ba762009-02-06 01:31:28 +0000160 SDValue(CurDAG->getTargetNode(IA64::FCVTXUFS1, dl, MVT::f64, TmpF1),
161 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162 Chain = TmpF3.getValue(1);
163 TmpF4 =
Dale Johannesen913ba762009-02-06 01:31:28 +0000164 SDValue(CurDAG->getTargetNode(IA64::FCVTXUFS1, dl, MVT::f64, TmpF2),
165 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166 Chain = TmpF4.getValue(1);
167 }
168
169 } else { // this is an FP divide/remainder, so we 'leak' some temp
170 // regs and assign TmpF3=Tmp1, TmpF4=Tmp2
171 TmpF3=Tmp1;
172 TmpF4=Tmp2;
173 }
174
175 // we start by computing an approximate reciprocal (good to 9 bits?)
176 // note, this instruction writes _both_ TmpF5 (answer) and TmpPR (predicate)
177 if(isFP)
Dale Johannesen913ba762009-02-06 01:31:28 +0000178 TmpF5 = SDValue(CurDAG->getTargetNode(IA64::FRCPAS0, dl, MVT::f64,
179 MVT::i1, TmpF3, TmpF4), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000180 else
Dale Johannesen913ba762009-02-06 01:31:28 +0000181 TmpF5 = SDValue(CurDAG->getTargetNode(IA64::FRCPAS1, dl, MVT::f64,
182 MVT::i1, TmpF3, TmpF4), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000183
184 TmpPR = TmpF5.getValue(1);
185 Chain = TmpF5.getValue(2);
186
Dan Gohman8181bd12008-07-27 21:46:04 +0000187 SDValue minusB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000188 if(isModulus) { // for remainders, it'll be handy to have
189 // copies of -input_b
Dale Johannesen913ba762009-02-06 01:31:28 +0000190 minusB = SDValue(CurDAG->getTargetNode(IA64::SUB, dl, MVT::i64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191 CurDAG->getRegister(IA64::r0, MVT::i64), Tmp2), 0);
192 Chain = minusB.getValue(1);
193 }
194
Dan Gohman8181bd12008-07-27 21:46:04 +0000195 SDValue TmpE0, TmpY1, TmpE1, TmpY2;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196
Dan Gohman8181bd12008-07-27 21:46:04 +0000197 SDValue OpsE0[] = { TmpF4, TmpF5, F1, TmpPR };
Dale Johannesen913ba762009-02-06 01:31:28 +0000198 TmpE0 = SDValue(CurDAG->getTargetNode(IA64::CFNMAS1, dl, MVT::f64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000199 OpsE0, 4), 0);
200 Chain = TmpE0.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000201 SDValue OpsY1[] = { TmpF5, TmpE0, TmpF5, TmpPR };
Dale Johannesen913ba762009-02-06 01:31:28 +0000202 TmpY1 = SDValue(CurDAG->getTargetNode(IA64::CFMAS1, dl, MVT::f64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000203 OpsY1, 4), 0);
204 Chain = TmpY1.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000205 SDValue OpsE1[] = { TmpE0, TmpE0, F0, TmpPR };
Dale Johannesen913ba762009-02-06 01:31:28 +0000206 TmpE1 = SDValue(CurDAG->getTargetNode(IA64::CFMAS1, dl, MVT::f64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000207 OpsE1, 4), 0);
208 Chain = TmpE1.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000209 SDValue OpsY2[] = { TmpY1, TmpE1, TmpY1, TmpPR };
Dale Johannesen913ba762009-02-06 01:31:28 +0000210 TmpY2 = SDValue(CurDAG->getTargetNode(IA64::CFMAS1, dl, MVT::f64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211 OpsY2, 4), 0);
212 Chain = TmpY2.getValue(1);
213
214 if(isFP) { // if this is an FP divide, we finish up here and exit early
215 if(isModulus)
216 assert(0 && "Sorry, try another FORTRAN compiler.");
217
Dan Gohman8181bd12008-07-27 21:46:04 +0000218 SDValue TmpE2, TmpY3, TmpQ0, TmpR0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000219
Dan Gohman8181bd12008-07-27 21:46:04 +0000220 SDValue OpsE2[] = { TmpE1, TmpE1, F0, TmpPR };
Dale Johannesen913ba762009-02-06 01:31:28 +0000221 TmpE2 = SDValue(CurDAG->getTargetNode(IA64::CFMAS1, dl, MVT::f64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000222 OpsE2, 4), 0);
223 Chain = TmpE2.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000224 SDValue OpsY3[] = { TmpY2, TmpE2, TmpY2, TmpPR };
Dale Johannesen913ba762009-02-06 01:31:28 +0000225 TmpY3 = SDValue(CurDAG->getTargetNode(IA64::CFMAS1, dl, MVT::f64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226 OpsY3, 4), 0);
227 Chain = TmpY3.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000228 SDValue OpsQ0[] = { Tmp1, TmpY3, F0, TmpPR };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229 TmpQ0 =
Dale Johannesen913ba762009-02-06 01:31:28 +0000230 SDValue(CurDAG->getTargetNode(IA64::CFMADS1, dl, // double prec!
231 MVT::f64, OpsQ0, 4), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232 Chain = TmpQ0.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000233 SDValue OpsR0[] = { Tmp2, TmpQ0, Tmp1, TmpPR };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000234 TmpR0 =
Dale Johannesen913ba762009-02-06 01:31:28 +0000235 SDValue(CurDAG->getTargetNode(IA64::CFNMADS1, dl, // double prec!
236 MVT::f64, OpsR0, 4), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237 Chain = TmpR0.getValue(1);
238
239// we want Result to have the same target register as the frcpa, so
240// we two-address hack it. See the comment "for this to work..." on
241// page 48 of Intel application note #245415
Dan Gohman8181bd12008-07-27 21:46:04 +0000242 SDValue Ops[] = { TmpF5, TmpY3, TmpR0, TmpQ0, TmpPR };
Dale Johannesen913ba762009-02-06 01:31:28 +0000243 Result = CurDAG->getTargetNode(IA64::TCFMADS0, dl, // d.p. s0 rndg!
244 MVT::f64, Ops, 5);
Dan Gohman8181bd12008-07-27 21:46:04 +0000245 Chain = SDValue(Result, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 return Result; // XXX: early exit!
247 } else { // this is *not* an FP divide, so there's a bit left to do:
248
Dan Gohman8181bd12008-07-27 21:46:04 +0000249 SDValue TmpQ2, TmpR2, TmpQ3, TmpQ;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250
Dan Gohman8181bd12008-07-27 21:46:04 +0000251 SDValue OpsQ2[] = { TmpF3, TmpY2, F0, TmpPR };
Dale Johannesen913ba762009-02-06 01:31:28 +0000252 TmpQ2 = SDValue(CurDAG->getTargetNode(IA64::CFMAS1, dl, MVT::f64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253 OpsQ2, 4), 0);
254 Chain = TmpQ2.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000255 SDValue OpsR2[] = { TmpF4, TmpQ2, TmpF3, TmpPR };
Dale Johannesen913ba762009-02-06 01:31:28 +0000256 TmpR2 = SDValue(CurDAG->getTargetNode(IA64::CFNMAS1, dl, MVT::f64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000257 OpsR2, 4), 0);
258 Chain = TmpR2.getValue(1);
259
260// we want TmpQ3 to have the same target register as the frcpa? maybe we
261// should two-address hack it. See the comment "for this to work..." on page
262// 48 of Intel application note #245415
Dan Gohman8181bd12008-07-27 21:46:04 +0000263 SDValue OpsQ3[] = { TmpF5, TmpR2, TmpY2, TmpQ2, TmpPR };
Dale Johannesen913ba762009-02-06 01:31:28 +0000264 TmpQ3 = SDValue(CurDAG->getTargetNode(IA64::TCFMAS1, dl, MVT::f64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000265 OpsQ3, 5), 0);
266 Chain = TmpQ3.getValue(1);
267
268 // STORY: without these two-address instructions (TCFMAS1 and TCFMADS0)
269 // the FPSWA won't be able to help out in the case of large/tiny
270 // arguments. Other fun bugs may also appear, e.g. 0/x = x, not 0.
271
272 if(isSigned)
Dale Johannesen913ba762009-02-06 01:31:28 +0000273 TmpQ = SDValue(CurDAG->getTargetNode(IA64::FCVTFXTRUNCS1, dl,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274 MVT::f64, TmpQ3), 0);
275 else
Dale Johannesen913ba762009-02-06 01:31:28 +0000276 TmpQ = SDValue(CurDAG->getTargetNode(IA64::FCVTFXUTRUNCS1, dl,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000277 MVT::f64, TmpQ3), 0);
278
279 Chain = TmpQ.getValue(1);
280
281 if(isModulus) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000282 SDValue FPminusB =
Dale Johannesen913ba762009-02-06 01:31:28 +0000283 SDValue(CurDAG->getTargetNode(IA64::SETFSIG, dl, MVT::f64, minusB),
284 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000285 Chain = FPminusB.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000286 SDValue Remainder =
Dale Johannesen913ba762009-02-06 01:31:28 +0000287 SDValue(CurDAG->getTargetNode(IA64::XMAL, dl, MVT::f64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000288 TmpQ, FPminusB, TmpF1), 0);
289 Chain = Remainder.getValue(1);
Dale Johannesen913ba762009-02-06 01:31:28 +0000290 Result = CurDAG->getTargetNode(IA64::GETFSIG, dl, MVT::i64, Remainder);
Dan Gohman8181bd12008-07-27 21:46:04 +0000291 Chain = SDValue(Result, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000292 } else { // just an integer divide
Dale Johannesen913ba762009-02-06 01:31:28 +0000293 Result = CurDAG->getTargetNode(IA64::GETFSIG, dl, MVT::i64, TmpQ);
Dan Gohman8181bd12008-07-27 21:46:04 +0000294 Chain = SDValue(Result, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000295 }
296
297 return Result;
298 } // wasn't an FP divide
299}
300
301// Select - Convert the specified operand from a target-independent to a
302// target-specific node if it hasn't already been changed.
Dan Gohman8181bd12008-07-27 21:46:04 +0000303SDNode *IA64DAGToDAGISel::Select(SDValue Op) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000304 SDNode *N = Op.getNode();
Dan Gohmanbd68c792008-07-17 19:10:17 +0000305 if (N->isMachineOpcode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000306 return NULL; // Already selected.
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000307 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000308
309 switch (N->getOpcode()) {
310 default: break;
311
312 case IA64ISD::BRCALL: { // XXX: this is also a hack!
Dan Gohman8181bd12008-07-27 21:46:04 +0000313 SDValue Chain = N->getOperand(0);
314 SDValue InFlag; // Null incoming flag value.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000315
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000316 if(N->getNumOperands()==3) { // we have an incoming chain, callee and flag
317 InFlag = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000318 }
319
320 unsigned CallOpcode;
Dan Gohman8181bd12008-07-27 21:46:04 +0000321 SDValue CallOperand;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000322
323 // if we can call directly, do so
324 if (GlobalAddressSDNode *GASD =
325 dyn_cast<GlobalAddressSDNode>(N->getOperand(1))) {
326 CallOpcode = IA64::BRCALL_IPREL_GA;
327 CallOperand = CurDAG->getTargetGlobalAddress(GASD->getGlobal(), MVT::i64);
Bill Wendlingfef06052008-09-16 21:48:12 +0000328 } else if (isa<ExternalSymbolSDNode>(N->getOperand(1))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000329 // FIXME: we currently NEED this case for correctness, to avoid
330 // "non-pic code with imm reloc.n against dynamic symbol" errors
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
Dan Gohman8181bd12008-07-27 21:46:04 +0000337 SDValue FnDescriptor = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000338
339 // load the branch target's entry point [mem] and
340 // GP value [mem+8]
Dan Gohman8181bd12008-07-27 21:46:04 +0000341 SDValue targetEntryPoint=
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000342 SDValue(CurDAG->getTargetNode(IA64::LD8, dl, MVT::i64, MVT::Other,
Chris Lattnerd04567e2008-05-28 04:25:57 +0000343 FnDescriptor, CurDAG->getEntryNode()), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000344 Chain = targetEntryPoint.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000345 SDValue targetGPAddr=
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000346 SDValue(CurDAG->getTargetNode(IA64::ADDS, dl, MVT::i64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000347 FnDescriptor,
348 CurDAG->getConstant(8, MVT::i64)), 0);
349 Chain = targetGPAddr.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000350 SDValue targetGP =
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000351 SDValue(CurDAG->getTargetNode(IA64::LD8, dl, MVT::i64,MVT::Other,
Chris Lattnerd04567e2008-05-28 04:25:57 +0000352 targetGPAddr, CurDAG->getEntryNode()), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000353 Chain = targetGP.getValue(1);
354
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000355 Chain = CurDAG->getCopyToReg(Chain, dl, IA64::r1, targetGP, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000356 InFlag = Chain.getValue(1);
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000357 Chain = CurDAG->getCopyToReg(Chain, dl, IA64::B6,
Gabor Greif4e8901a2008-08-30 10:09:02 +0000358 targetEntryPoint, InFlag); // FLAG these?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000359 InFlag = Chain.getValue(1);
360
361 CallOperand = CurDAG->getRegister(IA64::B6, MVT::i64);
362 CallOpcode = IA64::BRCALL_INDIRECT;
363 }
364
365 // Finally, once everything is setup, emit the call itself
Gabor Greif1c80d112008-08-28 21:40:38 +0000366 if (InFlag.getNode())
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000367 Chain = SDValue(CurDAG->getTargetNode(CallOpcode, dl, MVT::Other,
368 MVT::Flag, CallOperand, InFlag), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000369 else // there might be no arguments
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000370 Chain = SDValue(CurDAG->getTargetNode(CallOpcode, dl, MVT::Other,
371 MVT::Flag, CallOperand, Chain), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000372 InFlag = Chain.getValue(1);
373
Dan Gohman8181bd12008-07-27 21:46:04 +0000374 std::vector<SDValue> CallResults;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000375
376 CallResults.push_back(Chain);
377 CallResults.push_back(InFlag);
378
379 for (unsigned i = 0, e = CallResults.size(); i != e; ++i)
380 ReplaceUses(Op.getValue(i), CallResults[i]);
381 return NULL;
382 }
383
384 case IA64ISD::GETFD: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000385 SDValue Input = N->getOperand(0);
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000386 return CurDAG->getTargetNode(IA64::GETFD, dl, MVT::i64, Input);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000387 }
388
389 case ISD::FDIV:
390 case ISD::SDIV:
391 case ISD::UDIV:
392 case ISD::SREM:
393 case ISD::UREM:
394 return SelectDIV(Op);
395
396 case ISD::TargetConstantFP: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000397 SDValue Chain = CurDAG->getEntryNode(); // this is a constant, so..
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000398
Dan Gohman8181bd12008-07-27 21:46:04 +0000399 SDValue V;
Dale Johannesen76844472007-08-31 17:03:33 +0000400 ConstantFPSDNode* N2 = cast<ConstantFPSDNode>(N);
401 if (N2->getValueAPF().isPosZero()) {
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000402 V = CurDAG->getCopyFromReg(Chain, dl, IA64::F0, MVT::f64);
Dale Johannesen76844472007-08-31 17:03:33 +0000403 } else if (N2->isExactlyValue(N2->getValueType(0) == MVT::f32 ?
404 APFloat(+1.0f) : APFloat(+1.0))) {
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000405 V = CurDAG->getCopyFromReg(Chain, dl, IA64::F1, MVT::f64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000406 } else
407 assert(0 && "Unexpected FP constant!");
408
Dan Gohman8181bd12008-07-27 21:46:04 +0000409 ReplaceUses(SDValue(N, 0), V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000410 return 0;
411 }
412
413 case ISD::FrameIndex: { // TODO: reduce creepyness
414 int FI = cast<FrameIndexSDNode>(N)->getIndex();
415 if (N->hasOneUse())
416 return CurDAG->SelectNodeTo(N, IA64::MOV, MVT::i64,
417 CurDAG->getTargetFrameIndex(FI, MVT::i64));
418 else
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000419 return CurDAG->getTargetNode(IA64::MOV, dl, MVT::i64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000420 CurDAG->getTargetFrameIndex(FI, MVT::i64));
421 }
422
423 case ISD::ConstantPool: { // TODO: nuke the constant pool
424 // (ia64 doesn't need one)
425 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
426 Constant *C = CP->getConstVal();
Dan Gohman8181bd12008-07-27 21:46:04 +0000427 SDValue CPI = CurDAG->getTargetConstantPool(C, MVT::i64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000428 CP->getAlignment());
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000429 return CurDAG->getTargetNode(IA64::ADDL_GA, dl, MVT::i64, // ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000430 CurDAG->getRegister(IA64::r1, MVT::i64), CPI);
431 }
432
433 case ISD::GlobalAddress: {
434 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Dan Gohman8181bd12008-07-27 21:46:04 +0000435 SDValue GA = CurDAG->getTargetGlobalAddress(GV, MVT::i64);
436 SDValue Tmp =
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000437 SDValue(CurDAG->getTargetNode(IA64::ADDL_GA, dl, MVT::i64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000438 CurDAG->getRegister(IA64::r1,
439 MVT::i64), GA), 0);
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000440 return CurDAG->getTargetNode(IA64::LD8, dl, MVT::i64, MVT::Other, Tmp,
Chris Lattner7e03fd62008-07-09 05:12:07 +0000441 CurDAG->getEntryNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000442 }
443
444/* XXX
Bill Wendlingfef06052008-09-16 21:48:12 +0000445 case ISD::ExternalSymbol: {
446 SDValue EA = CurDAG->getTargetExternalSymbol(
447 cast<ExternalSymbolSDNode>(N)->getSymbol(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000448 MVT::i64);
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000449 SDValue Tmp = CurDAG->getTargetNode(IA64::ADDL_EA, dl, MVT::i64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000450 CurDAG->getRegister(IA64::r1,
451 MVT::i64),
452 EA);
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000453 return CurDAG->getTargetNode(IA64::LD8, dl, MVT::i64, Tmp);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000454 }
455*/
456
457 case ISD::LOAD: { // FIXME: load -1, not 1, for bools?
458 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman8181bd12008-07-27 21:46:04 +0000459 SDValue Chain = LD->getChain();
460 SDValue Address = LD->getBasePtr();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000461
Duncan Sands92c43912008-06-06 12:08:01 +0000462 MVT TypeBeingLoaded = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000463 unsigned Opc;
Duncan Sands92c43912008-06-06 12:08:01 +0000464 switch (TypeBeingLoaded.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000465 default:
466#ifndef NDEBUG
467 N->dump(CurDAG);
468#endif
469 assert(0 && "Cannot load this type!");
470 case MVT::i1: { // this is a bool
471 Opc = IA64::LD1; // first we load a byte, then compare for != 0
472 if(N->getValueType(0) == MVT::i1) { // XXX: early exit!
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000473 return CurDAG->SelectNodeTo(N, IA64::CMPNE, MVT::i1, MVT::Other,
474 SDValue(CurDAG->getTargetNode(Opc, dl,
475 MVT::i64,
476 Address), 0),
477 CurDAG->getRegister(IA64::r0, MVT::i64),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000478 Chain);
479 }
480 /* otherwise, we want to load a bool into something bigger: LD1
481 will do that for us, so we just fall through */
482 }
483 case MVT::i8: Opc = IA64::LD1; break;
484 case MVT::i16: Opc = IA64::LD2; break;
485 case MVT::i32: Opc = IA64::LD4; break;
486 case MVT::i64: Opc = IA64::LD8; break;
487
488 case MVT::f32: Opc = IA64::LDF4; break;
489 case MVT::f64: Opc = IA64::LDF8; break;
490 }
491
492 // TODO: comment this
493 return CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), MVT::Other,
494 Address, Chain);
495 }
496
497 case ISD::STORE: {
498 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman8181bd12008-07-27 21:46:04 +0000499 SDValue Address = ST->getBasePtr();
500 SDValue Chain = ST->getChain();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000501
502 unsigned Opc;
503 if (ISD::isNON_TRUNCStore(N)) {
Duncan Sands92c43912008-06-06 12:08:01 +0000504 switch (N->getOperand(1).getValueType().getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000505 default: assert(0 && "unknown type in store");
506 case MVT::i1: { // this is a bool
507 Opc = IA64::ST1; // we store either 0 or 1 as a byte
508 // first load zero!
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000509 SDValue Initial = CurDAG->getCopyFromReg(Chain, dl, IA64::r0, MVT::i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000510 Chain = Initial.getValue(1);
511 // then load 1 into the same reg iff the predicate to store is 1
Dan Gohman8181bd12008-07-27 21:46:04 +0000512 SDValue Tmp = ST->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000513 Tmp =
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000514 SDValue(CurDAG->getTargetNode(IA64::TPCADDS, dl, MVT::i64, Initial,
Gabor Greif4e8901a2008-08-30 10:09:02 +0000515 CurDAG->getTargetConstant(1,
516 MVT::i64),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000517 Tmp), 0);
518 return CurDAG->SelectNodeTo(N, Opc, MVT::Other, Address, Tmp, Chain);
519 }
520 case MVT::i64: Opc = IA64::ST8; break;
521 case MVT::f64: Opc = IA64::STF8; break;
522 }
523 } else { // Truncating store
Duncan Sands92c43912008-06-06 12:08:01 +0000524 switch(ST->getMemoryVT().getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000525 default: assert(0 && "unknown type in truncstore");
526 case MVT::i8: Opc = IA64::ST1; break;
527 case MVT::i16: Opc = IA64::ST2; break;
528 case MVT::i32: Opc = IA64::ST4; break;
529 case MVT::f32: Opc = IA64::STF4; break;
530 }
531 }
532
Dan Gohman8181bd12008-07-27 21:46:04 +0000533 SDValue N1 = N->getOperand(1);
534 SDValue N2 = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000535 return CurDAG->SelectNodeTo(N, Opc, MVT::Other, N2, N1, Chain);
536 }
537
538 case ISD::BRCOND: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000539 SDValue Chain = N->getOperand(0);
540 SDValue CC = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000541 MachineBasicBlock *Dest =
542 cast<BasicBlockSDNode>(N->getOperand(2))->getBasicBlock();
543 //FIXME - we do NOT need long branches all the time
544 return CurDAG->SelectNodeTo(N, IA64::BRLCOND_NOTCALL, MVT::Other, CC,
545 CurDAG->getBasicBlock(Dest), Chain);
546 }
547
548 case ISD::CALLSEQ_START:
549 case ISD::CALLSEQ_END: {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000550 int64_t Amt = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000551 unsigned Opc = N->getOpcode() == ISD::CALLSEQ_START ?
552 IA64::ADJUSTCALLSTACKDOWN : IA64::ADJUSTCALLSTACKUP;
Dan Gohman8181bd12008-07-27 21:46:04 +0000553 SDValue N0 = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000554 return CurDAG->SelectNodeTo(N, Opc, MVT::Other, getI64Imm(Amt), N0);
555 }
556
557 case ISD::BR:
558 // FIXME: we don't need long branches all the time!
Dan Gohman8181bd12008-07-27 21:46:04 +0000559 SDValue N0 = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000560 return CurDAG->SelectNodeTo(N, IA64::BRL_NOTCALL, MVT::Other,
561 N->getOperand(1), N0);
562 }
563
564 return SelectCode(Op);
565}
566
567
568/// createIA64DAGToDAGInstructionSelector - This pass converts a legalized DAG
569/// into an IA64-specific DAG, ready for instruction scheduling.
570///
571FunctionPass
572*llvm::createIA64DAGToDAGInstructionSelector(IA64TargetMachine &TM) {
573 return new IA64DAGToDAGISel(TM);
574}
575