blob: 833a82578641630d8d75fbfa287634dadd29dd36 [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);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109
110 bool isFP=false;
111
Duncan Sands92c43912008-06-06 12:08:01 +0000112 if(Tmp1.getValueType().isFloatingPoint())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113 isFP=true;
114
115 bool isModulus=false; // is it a division or a modulus?
116 bool isSigned=false;
117
118 switch(N->getOpcode()) {
119 case ISD::FDIV:
120 case ISD::SDIV: isModulus=false; isSigned=true; break;
121 case ISD::UDIV: isModulus=false; isSigned=false; break;
122 case ISD::FREM:
123 case ISD::SREM: isModulus=true; isSigned=true; break;
124 case ISD::UREM: isModulus=true; isSigned=false; break;
125 }
126
127 // TODO: check for integer divides by powers of 2 (or other simple patterns?)
128
Dan Gohman8181bd12008-07-27 21:46:04 +0000129 SDValue TmpPR, TmpPR2;
130 SDValue TmpF1, TmpF2, TmpF3, TmpF4, TmpF5, TmpF6, TmpF7, TmpF8;
131 SDValue TmpF9, TmpF10,TmpF11,TmpF12,TmpF13,TmpF14,TmpF15;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000132 SDNode *Result;
133
134 // we'll need copies of F0 and F1
Dan Gohman8181bd12008-07-27 21:46:04 +0000135 SDValue F0 = CurDAG->getRegister(IA64::F0, MVT::f64);
136 SDValue F1 = CurDAG->getRegister(IA64::F1, MVT::f64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000137
138 // OK, emit some code:
139
140 if(!isFP) {
141 // first, load the inputs into FP regs.
142 TmpF1 =
Dan Gohman8181bd12008-07-27 21:46:04 +0000143 SDValue(CurDAG->getTargetNode(IA64::SETFSIG, MVT::f64, Tmp1), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000144 Chain = TmpF1.getValue(1);
145 TmpF2 =
Dan Gohman8181bd12008-07-27 21:46:04 +0000146 SDValue(CurDAG->getTargetNode(IA64::SETFSIG, MVT::f64, Tmp2), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000147 Chain = TmpF2.getValue(1);
148
149 // next, convert the inputs to FP
150 if(isSigned) {
151 TmpF3 =
Dan Gohman8181bd12008-07-27 21:46:04 +0000152 SDValue(CurDAG->getTargetNode(IA64::FCVTXF, MVT::f64, TmpF1), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153 Chain = TmpF3.getValue(1);
154 TmpF4 =
Dan Gohman8181bd12008-07-27 21:46:04 +0000155 SDValue(CurDAG->getTargetNode(IA64::FCVTXF, MVT::f64, TmpF2), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000156 Chain = TmpF4.getValue(1);
157 } else { // is unsigned
158 TmpF3 =
Dan Gohman8181bd12008-07-27 21:46:04 +0000159 SDValue(CurDAG->getTargetNode(IA64::FCVTXUFS1, MVT::f64, TmpF1), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160 Chain = TmpF3.getValue(1);
161 TmpF4 =
Dan Gohman8181bd12008-07-27 21:46:04 +0000162 SDValue(CurDAG->getTargetNode(IA64::FCVTXUFS1, MVT::f64, TmpF2), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000163 Chain = TmpF4.getValue(1);
164 }
165
166 } else { // this is an FP divide/remainder, so we 'leak' some temp
167 // regs and assign TmpF3=Tmp1, TmpF4=Tmp2
168 TmpF3=Tmp1;
169 TmpF4=Tmp2;
170 }
171
172 // we start by computing an approximate reciprocal (good to 9 bits?)
173 // note, this instruction writes _both_ TmpF5 (answer) and TmpPR (predicate)
174 if(isFP)
Dan Gohman8181bd12008-07-27 21:46:04 +0000175 TmpF5 = SDValue(CurDAG->getTargetNode(IA64::FRCPAS0, MVT::f64, MVT::i1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 TmpF3, TmpF4), 0);
177 else
Dan Gohman8181bd12008-07-27 21:46:04 +0000178 TmpF5 = SDValue(CurDAG->getTargetNode(IA64::FRCPAS1, MVT::f64, MVT::i1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000179 TmpF3, TmpF4), 0);
180
181 TmpPR = TmpF5.getValue(1);
182 Chain = TmpF5.getValue(2);
183
Dan Gohman8181bd12008-07-27 21:46:04 +0000184 SDValue minusB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000185 if(isModulus) { // for remainders, it'll be handy to have
186 // copies of -input_b
Dan Gohman8181bd12008-07-27 21:46:04 +0000187 minusB = SDValue(CurDAG->getTargetNode(IA64::SUB, MVT::i64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000188 CurDAG->getRegister(IA64::r0, MVT::i64), Tmp2), 0);
189 Chain = minusB.getValue(1);
190 }
191
Dan Gohman8181bd12008-07-27 21:46:04 +0000192 SDValue TmpE0, TmpY1, TmpE1, TmpY2;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000193
Dan Gohman8181bd12008-07-27 21:46:04 +0000194 SDValue OpsE0[] = { TmpF4, TmpF5, F1, TmpPR };
195 TmpE0 = SDValue(CurDAG->getTargetNode(IA64::CFNMAS1, MVT::f64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 OpsE0, 4), 0);
197 Chain = TmpE0.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000198 SDValue OpsY1[] = { TmpF5, TmpE0, TmpF5, TmpPR };
199 TmpY1 = SDValue(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000200 OpsY1, 4), 0);
201 Chain = TmpY1.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000202 SDValue OpsE1[] = { TmpE0, TmpE0, F0, TmpPR };
203 TmpE1 = SDValue(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000204 OpsE1, 4), 0);
205 Chain = TmpE1.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000206 SDValue OpsY2[] = { TmpY1, TmpE1, TmpY1, TmpPR };
207 TmpY2 = SDValue(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000208 OpsY2, 4), 0);
209 Chain = TmpY2.getValue(1);
210
211 if(isFP) { // if this is an FP divide, we finish up here and exit early
212 if(isModulus)
213 assert(0 && "Sorry, try another FORTRAN compiler.");
214
Dan Gohman8181bd12008-07-27 21:46:04 +0000215 SDValue TmpE2, TmpY3, TmpQ0, TmpR0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000216
Dan Gohman8181bd12008-07-27 21:46:04 +0000217 SDValue OpsE2[] = { TmpE1, TmpE1, F0, TmpPR };
218 TmpE2 = SDValue(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000219 OpsE2, 4), 0);
220 Chain = TmpE2.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000221 SDValue OpsY3[] = { TmpY2, TmpE2, TmpY2, TmpPR };
222 TmpY3 = SDValue(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000223 OpsY3, 4), 0);
224 Chain = TmpY3.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000225 SDValue OpsQ0[] = { Tmp1, TmpY3, F0, TmpPR };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000226 TmpQ0 =
Dan Gohman8181bd12008-07-27 21:46:04 +0000227 SDValue(CurDAG->getTargetNode(IA64::CFMADS1, MVT::f64, // double prec!
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000228 OpsQ0, 4), 0);
229 Chain = TmpQ0.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000230 SDValue OpsR0[] = { Tmp2, TmpQ0, Tmp1, TmpPR };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000231 TmpR0 =
Dan Gohman8181bd12008-07-27 21:46:04 +0000232 SDValue(CurDAG->getTargetNode(IA64::CFNMADS1, MVT::f64, // double prec!
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000233 OpsR0, 4), 0);
234 Chain = TmpR0.getValue(1);
235
236// we want Result to have the same target register as the frcpa, so
237// we two-address hack it. See the comment "for this to work..." on
238// page 48 of Intel application note #245415
Dan Gohman8181bd12008-07-27 21:46:04 +0000239 SDValue Ops[] = { TmpF5, TmpY3, TmpR0, TmpQ0, TmpPR };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000240 Result = CurDAG->getTargetNode(IA64::TCFMADS0, MVT::f64, // d.p. s0 rndg!
241 Ops, 5);
Dan Gohman8181bd12008-07-27 21:46:04 +0000242 Chain = SDValue(Result, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000243 return Result; // XXX: early exit!
244 } else { // this is *not* an FP divide, so there's a bit left to do:
245
Dan Gohman8181bd12008-07-27 21:46:04 +0000246 SDValue TmpQ2, TmpR2, TmpQ3, TmpQ;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247
Dan Gohman8181bd12008-07-27 21:46:04 +0000248 SDValue OpsQ2[] = { TmpF3, TmpY2, F0, TmpPR };
249 TmpQ2 = SDValue(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000250 OpsQ2, 4), 0);
251 Chain = TmpQ2.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000252 SDValue OpsR2[] = { TmpF4, TmpQ2, TmpF3, TmpPR };
253 TmpR2 = SDValue(CurDAG->getTargetNode(IA64::CFNMAS1, MVT::f64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000254 OpsR2, 4), 0);
255 Chain = TmpR2.getValue(1);
256
257// we want TmpQ3 to have the same target register as the frcpa? maybe we
258// should two-address hack it. See the comment "for this to work..." on page
259// 48 of Intel application note #245415
Dan Gohman8181bd12008-07-27 21:46:04 +0000260 SDValue OpsQ3[] = { TmpF5, TmpR2, TmpY2, TmpQ2, TmpPR };
261 TmpQ3 = SDValue(CurDAG->getTargetNode(IA64::TCFMAS1, MVT::f64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000262 OpsQ3, 5), 0);
263 Chain = TmpQ3.getValue(1);
264
265 // STORY: without these two-address instructions (TCFMAS1 and TCFMADS0)
266 // the FPSWA won't be able to help out in the case of large/tiny
267 // arguments. Other fun bugs may also appear, e.g. 0/x = x, not 0.
268
269 if(isSigned)
Dan Gohman8181bd12008-07-27 21:46:04 +0000270 TmpQ = SDValue(CurDAG->getTargetNode(IA64::FCVTFXTRUNCS1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000271 MVT::f64, TmpQ3), 0);
272 else
Dan Gohman8181bd12008-07-27 21:46:04 +0000273 TmpQ = SDValue(CurDAG->getTargetNode(IA64::FCVTFXUTRUNCS1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000274 MVT::f64, TmpQ3), 0);
275
276 Chain = TmpQ.getValue(1);
277
278 if(isModulus) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000279 SDValue FPminusB =
280 SDValue(CurDAG->getTargetNode(IA64::SETFSIG, MVT::f64, minusB), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000281 Chain = FPminusB.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000282 SDValue Remainder =
283 SDValue(CurDAG->getTargetNode(IA64::XMAL, MVT::f64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000284 TmpQ, FPminusB, TmpF1), 0);
285 Chain = Remainder.getValue(1);
286 Result = CurDAG->getTargetNode(IA64::GETFSIG, MVT::i64, Remainder);
Dan Gohman8181bd12008-07-27 21:46:04 +0000287 Chain = SDValue(Result, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000288 } else { // just an integer divide
289 Result = CurDAG->getTargetNode(IA64::GETFSIG, MVT::i64, TmpQ);
Dan Gohman8181bd12008-07-27 21:46:04 +0000290 Chain = SDValue(Result, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000291 }
292
293 return Result;
294 } // wasn't an FP divide
295}
296
297// Select - Convert the specified operand from a target-independent to a
298// target-specific node if it hasn't already been changed.
Dan Gohman8181bd12008-07-27 21:46:04 +0000299SDNode *IA64DAGToDAGISel::Select(SDValue Op) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000300 SDNode *N = Op.getNode();
Dan Gohmanbd68c792008-07-17 19:10:17 +0000301 if (N->isMachineOpcode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000302 return NULL; // Already selected.
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000303 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000304
305 switch (N->getOpcode()) {
306 default: break;
307
308 case IA64ISD::BRCALL: { // XXX: this is also a hack!
Dan Gohman8181bd12008-07-27 21:46:04 +0000309 SDValue Chain = N->getOperand(0);
310 SDValue InFlag; // Null incoming flag value.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000311
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000312 if(N->getNumOperands()==3) { // we have an incoming chain, callee and flag
313 InFlag = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000314 }
315
316 unsigned CallOpcode;
Dan Gohman8181bd12008-07-27 21:46:04 +0000317 SDValue CallOperand;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000318
319 // if we can call directly, do so
320 if (GlobalAddressSDNode *GASD =
321 dyn_cast<GlobalAddressSDNode>(N->getOperand(1))) {
322 CallOpcode = IA64::BRCALL_IPREL_GA;
323 CallOperand = CurDAG->getTargetGlobalAddress(GASD->getGlobal(), MVT::i64);
Bill Wendlingfef06052008-09-16 21:48:12 +0000324 } else if (isa<ExternalSymbolSDNode>(N->getOperand(1))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000325 // FIXME: we currently NEED this case for correctness, to avoid
326 // "non-pic code with imm reloc.n against dynamic symbol" errors
327 CallOpcode = IA64::BRCALL_IPREL_ES;
328 CallOperand = N->getOperand(1);
329 } else {
330 // otherwise we need to load the function descriptor,
331 // load the branch target (function)'s entry point and GP,
332 // branch (call) then restore the GP
Dan Gohman8181bd12008-07-27 21:46:04 +0000333 SDValue FnDescriptor = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000334
335 // load the branch target's entry point [mem] and
336 // GP value [mem+8]
Dan Gohman8181bd12008-07-27 21:46:04 +0000337 SDValue targetEntryPoint=
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000338 SDValue(CurDAG->getTargetNode(IA64::LD8, dl, MVT::i64, MVT::Other,
Chris Lattnerd04567e2008-05-28 04:25:57 +0000339 FnDescriptor, CurDAG->getEntryNode()), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000340 Chain = targetEntryPoint.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000341 SDValue targetGPAddr=
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000342 SDValue(CurDAG->getTargetNode(IA64::ADDS, dl, MVT::i64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000343 FnDescriptor,
344 CurDAG->getConstant(8, MVT::i64)), 0);
345 Chain = targetGPAddr.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000346 SDValue targetGP =
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000347 SDValue(CurDAG->getTargetNode(IA64::LD8, dl, MVT::i64,MVT::Other,
Chris Lattnerd04567e2008-05-28 04:25:57 +0000348 targetGPAddr, CurDAG->getEntryNode()), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000349 Chain = targetGP.getValue(1);
350
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000351 Chain = CurDAG->getCopyToReg(Chain, dl, IA64::r1, targetGP, InFlag);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000352 InFlag = Chain.getValue(1);
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000353 Chain = CurDAG->getCopyToReg(Chain, dl, IA64::B6,
Gabor Greif4e8901a2008-08-30 10:09:02 +0000354 targetEntryPoint, InFlag); // FLAG these?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000355 InFlag = Chain.getValue(1);
356
357 CallOperand = CurDAG->getRegister(IA64::B6, MVT::i64);
358 CallOpcode = IA64::BRCALL_INDIRECT;
359 }
360
361 // Finally, once everything is setup, emit the call itself
Gabor Greif1c80d112008-08-28 21:40:38 +0000362 if (InFlag.getNode())
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000363 Chain = SDValue(CurDAG->getTargetNode(CallOpcode, dl, MVT::Other,
364 MVT::Flag, CallOperand, InFlag), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000365 else // there might be no arguments
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000366 Chain = SDValue(CurDAG->getTargetNode(CallOpcode, dl, MVT::Other,
367 MVT::Flag, CallOperand, Chain), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000368 InFlag = Chain.getValue(1);
369
Dan Gohman8181bd12008-07-27 21:46:04 +0000370 std::vector<SDValue> CallResults;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000371
372 CallResults.push_back(Chain);
373 CallResults.push_back(InFlag);
374
375 for (unsigned i = 0, e = CallResults.size(); i != e; ++i)
376 ReplaceUses(Op.getValue(i), CallResults[i]);
377 return NULL;
378 }
379
380 case IA64ISD::GETFD: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000381 SDValue Input = N->getOperand(0);
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000382 return CurDAG->getTargetNode(IA64::GETFD, dl, MVT::i64, Input);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000383 }
384
385 case ISD::FDIV:
386 case ISD::SDIV:
387 case ISD::UDIV:
388 case ISD::SREM:
389 case ISD::UREM:
390 return SelectDIV(Op);
391
392 case ISD::TargetConstantFP: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000393 SDValue Chain = CurDAG->getEntryNode(); // this is a constant, so..
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000394
Dan Gohman8181bd12008-07-27 21:46:04 +0000395 SDValue V;
Dale Johannesen76844472007-08-31 17:03:33 +0000396 ConstantFPSDNode* N2 = cast<ConstantFPSDNode>(N);
397 if (N2->getValueAPF().isPosZero()) {
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000398 V = CurDAG->getCopyFromReg(Chain, dl, IA64::F0, MVT::f64);
Dale Johannesen76844472007-08-31 17:03:33 +0000399 } else if (N2->isExactlyValue(N2->getValueType(0) == MVT::f32 ?
400 APFloat(+1.0f) : APFloat(+1.0))) {
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000401 V = CurDAG->getCopyFromReg(Chain, dl, IA64::F1, MVT::f64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000402 } else
403 assert(0 && "Unexpected FP constant!");
404
Dan Gohman8181bd12008-07-27 21:46:04 +0000405 ReplaceUses(SDValue(N, 0), V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000406 return 0;
407 }
408
409 case ISD::FrameIndex: { // TODO: reduce creepyness
410 int FI = cast<FrameIndexSDNode>(N)->getIndex();
411 if (N->hasOneUse())
412 return CurDAG->SelectNodeTo(N, IA64::MOV, MVT::i64,
413 CurDAG->getTargetFrameIndex(FI, MVT::i64));
414 else
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000415 return CurDAG->getTargetNode(IA64::MOV, dl, MVT::i64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000416 CurDAG->getTargetFrameIndex(FI, MVT::i64));
417 }
418
419 case ISD::ConstantPool: { // TODO: nuke the constant pool
420 // (ia64 doesn't need one)
421 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
422 Constant *C = CP->getConstVal();
Dan Gohman8181bd12008-07-27 21:46:04 +0000423 SDValue CPI = CurDAG->getTargetConstantPool(C, MVT::i64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000424 CP->getAlignment());
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000425 return CurDAG->getTargetNode(IA64::ADDL_GA, dl, MVT::i64, // ?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000426 CurDAG->getRegister(IA64::r1, MVT::i64), CPI);
427 }
428
429 case ISD::GlobalAddress: {
430 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Dan Gohman8181bd12008-07-27 21:46:04 +0000431 SDValue GA = CurDAG->getTargetGlobalAddress(GV, MVT::i64);
432 SDValue Tmp =
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000433 SDValue(CurDAG->getTargetNode(IA64::ADDL_GA, dl, MVT::i64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000434 CurDAG->getRegister(IA64::r1,
435 MVT::i64), GA), 0);
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000436 return CurDAG->getTargetNode(IA64::LD8, dl, MVT::i64, MVT::Other, Tmp,
Chris Lattner7e03fd62008-07-09 05:12:07 +0000437 CurDAG->getEntryNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000438 }
439
440/* XXX
Bill Wendlingfef06052008-09-16 21:48:12 +0000441 case ISD::ExternalSymbol: {
442 SDValue EA = CurDAG->getTargetExternalSymbol(
443 cast<ExternalSymbolSDNode>(N)->getSymbol(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000444 MVT::i64);
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000445 SDValue Tmp = CurDAG->getTargetNode(IA64::ADDL_EA, dl, MVT::i64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000446 CurDAG->getRegister(IA64::r1,
447 MVT::i64),
448 EA);
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000449 return CurDAG->getTargetNode(IA64::LD8, dl, MVT::i64, Tmp);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000450 }
451*/
452
453 case ISD::LOAD: { // FIXME: load -1, not 1, for bools?
454 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman8181bd12008-07-27 21:46:04 +0000455 SDValue Chain = LD->getChain();
456 SDValue Address = LD->getBasePtr();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000457
Duncan Sands92c43912008-06-06 12:08:01 +0000458 MVT TypeBeingLoaded = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000459 unsigned Opc;
Duncan Sands92c43912008-06-06 12:08:01 +0000460 switch (TypeBeingLoaded.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000461 default:
462#ifndef NDEBUG
463 N->dump(CurDAG);
464#endif
465 assert(0 && "Cannot load this type!");
466 case MVT::i1: { // this is a bool
467 Opc = IA64::LD1; // first we load a byte, then compare for != 0
468 if(N->getValueType(0) == MVT::i1) { // XXX: early exit!
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000469 return CurDAG->SelectNodeTo(N, IA64::CMPNE, MVT::i1, MVT::Other,
470 SDValue(CurDAG->getTargetNode(Opc, dl,
471 MVT::i64,
472 Address), 0),
473 CurDAG->getRegister(IA64::r0, MVT::i64),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000474 Chain);
475 }
476 /* otherwise, we want to load a bool into something bigger: LD1
477 will do that for us, so we just fall through */
478 }
479 case MVT::i8: Opc = IA64::LD1; break;
480 case MVT::i16: Opc = IA64::LD2; break;
481 case MVT::i32: Opc = IA64::LD4; break;
482 case MVT::i64: Opc = IA64::LD8; break;
483
484 case MVT::f32: Opc = IA64::LDF4; break;
485 case MVT::f64: Opc = IA64::LDF8; break;
486 }
487
488 // TODO: comment this
489 return CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), MVT::Other,
490 Address, Chain);
491 }
492
493 case ISD::STORE: {
494 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman8181bd12008-07-27 21:46:04 +0000495 SDValue Address = ST->getBasePtr();
496 SDValue Chain = ST->getChain();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000497
498 unsigned Opc;
499 if (ISD::isNON_TRUNCStore(N)) {
Duncan Sands92c43912008-06-06 12:08:01 +0000500 switch (N->getOperand(1).getValueType().getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000501 default: assert(0 && "unknown type in store");
502 case MVT::i1: { // this is a bool
503 Opc = IA64::ST1; // we store either 0 or 1 as a byte
504 // first load zero!
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000505 SDValue Initial = CurDAG->getCopyFromReg(Chain, dl, IA64::r0, MVT::i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000506 Chain = Initial.getValue(1);
507 // then load 1 into the same reg iff the predicate to store is 1
Dan Gohman8181bd12008-07-27 21:46:04 +0000508 SDValue Tmp = ST->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000509 Tmp =
Dale Johannesenb03cc3f2009-02-04 23:02:30 +0000510 SDValue(CurDAG->getTargetNode(IA64::TPCADDS, dl, MVT::i64, Initial,
Gabor Greif4e8901a2008-08-30 10:09:02 +0000511 CurDAG->getTargetConstant(1,
512 MVT::i64),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000513 Tmp), 0);
514 return CurDAG->SelectNodeTo(N, Opc, MVT::Other, Address, Tmp, Chain);
515 }
516 case MVT::i64: Opc = IA64::ST8; break;
517 case MVT::f64: Opc = IA64::STF8; break;
518 }
519 } else { // Truncating store
Duncan Sands92c43912008-06-06 12:08:01 +0000520 switch(ST->getMemoryVT().getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000521 default: assert(0 && "unknown type in truncstore");
522 case MVT::i8: Opc = IA64::ST1; break;
523 case MVT::i16: Opc = IA64::ST2; break;
524 case MVT::i32: Opc = IA64::ST4; break;
525 case MVT::f32: Opc = IA64::STF4; break;
526 }
527 }
528
Dan Gohman8181bd12008-07-27 21:46:04 +0000529 SDValue N1 = N->getOperand(1);
530 SDValue N2 = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000531 return CurDAG->SelectNodeTo(N, Opc, MVT::Other, N2, N1, Chain);
532 }
533
534 case ISD::BRCOND: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000535 SDValue Chain = N->getOperand(0);
536 SDValue CC = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000537 MachineBasicBlock *Dest =
538 cast<BasicBlockSDNode>(N->getOperand(2))->getBasicBlock();
539 //FIXME - we do NOT need long branches all the time
540 return CurDAG->SelectNodeTo(N, IA64::BRLCOND_NOTCALL, MVT::Other, CC,
541 CurDAG->getBasicBlock(Dest), Chain);
542 }
543
544 case ISD::CALLSEQ_START:
545 case ISD::CALLSEQ_END: {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000546 int64_t Amt = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000547 unsigned Opc = N->getOpcode() == ISD::CALLSEQ_START ?
548 IA64::ADJUSTCALLSTACKDOWN : IA64::ADJUSTCALLSTACKUP;
Dan Gohman8181bd12008-07-27 21:46:04 +0000549 SDValue N0 = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000550 return CurDAG->SelectNodeTo(N, Opc, MVT::Other, getI64Imm(Amt), N0);
551 }
552
553 case ISD::BR:
554 // FIXME: we don't need long branches all the time!
Dan Gohman8181bd12008-07-27 21:46:04 +0000555 SDValue N0 = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000556 return CurDAG->SelectNodeTo(N, IA64::BRL_NOTCALL, MVT::Other,
557 N->getOperand(1), N0);
558 }
559
560 return SelectCode(Op);
561}
562
563
564/// createIA64DAGToDAGInstructionSelector - This pass converts a legalized DAG
565/// into an IA64-specific DAG, ready for instruction scheduling.
566///
567FunctionPass
568*llvm::createIA64DAGToDAGInstructionSelector(IA64TargetMachine &TM) {
569 return new IA64DAGToDAGISel(TM);
570}
571