blob: 18b9ecf94eda11b31efd6ca1431d7ffd584aa21b [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"
30#include <queue>
31#include <set>
32using namespace llvm;
33
34namespace {
35 //===--------------------------------------------------------------------===//
36 /// IA64DAGToDAGISel - IA64 specific code to select IA64 machine
37 /// instructions for SelectionDAG operations.
38 ///
39 class IA64DAGToDAGISel : public SelectionDAGISel {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040 unsigned GlobalBaseReg;
41 public:
Dan Gohmane887fdf2008-07-07 18:00:37 +000042 explicit IA64DAGToDAGISel(IA64TargetMachine &TM)
Dan Gohmanf2b29572008-10-03 16:55:19 +000043 : SelectionDAGISel(*TM.getTargetLowering()) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044
45 virtual bool runOnFunction(Function &Fn) {
46 // Make sure we re-emit a set of the global base reg if necessary
47 GlobalBaseReg = 0;
48 return SelectionDAGISel::runOnFunction(Fn);
49 }
50
51 /// getI64Imm - Return a target constant with the specified value, of type
52 /// i64.
Dan Gohman8181bd12008-07-27 21:46:04 +000053 inline SDValue getI64Imm(uint64_t Imm) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054 return CurDAG->getTargetConstant(Imm, MVT::i64);
55 }
56
57 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
58 /// base register. Return the virtual register that holds this value.
Dan Gohman8181bd12008-07-27 21:46:04 +000059 // SDValue getGlobalBaseReg(); TODO: hmm
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060
61 // Select - Convert the specified operand from a target-independent to a
62 // target-specific node if it hasn't already been changed.
Dan Gohman8181bd12008-07-27 21:46:04 +000063 SDNode *Select(SDValue N);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000064
Dan Gohman8181bd12008-07-27 21:46:04 +000065 SDNode *SelectIntImmediateExpr(SDValue LHS, SDValue RHS,
Dan Gohmanf17a25c2007-07-18 16:29:46 +000066 unsigned OCHi, unsigned OCLo,
67 bool IsArithmetic = false,
68 bool Negate = false);
69 SDNode *SelectBitfieldInsert(SDNode *N);
70
71 /// SelectCC - Select a comparison of the specified values with the
72 /// specified condition code, returning the CR# of the expression.
Dan Gohman8181bd12008-07-27 21:46:04 +000073 SDValue SelectCC(SDValue LHS, SDValue RHS, ISD::CondCode CC);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074
75 /// SelectAddr - Given the specified address, return the two operands for a
76 /// load/store instruction, and return true if it should be an indexed [r+r]
77 /// operation.
Dan Gohman8181bd12008-07-27 21:46:04 +000078 bool SelectAddr(SDValue Addr, SDValue &Op1, SDValue &Op2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079
Evan Cheng34fd4f32008-06-30 20:45:06 +000080 /// InstructionSelect - This callback is invoked by
Dan Gohmanf17a25c2007-07-18 16:29:46 +000081 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohman14a66442008-08-23 02:25:05 +000082 virtual void InstructionSelect();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000083
84 virtual const char *getPassName() const {
85 return "IA64 (Itanium) DAG->DAG Instruction Selector";
86 }
87
88// Include the pieces autogenerated from the target description.
89#include "IA64GenDAGISel.inc"
90
91private:
Dan Gohman8181bd12008-07-27 21:46:04 +000092 SDNode *SelectDIV(SDValue Op);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093 };
94}
95
Evan Cheng34fd4f32008-06-30 20:45:06 +000096/// InstructionSelect - This callback is invoked by
Dan Gohmanf17a25c2007-07-18 16:29:46 +000097/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
Dan Gohman14a66442008-08-23 02:25:05 +000098void IA64DAGToDAGISel::InstructionSelect() {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000099 DEBUG(BB->dump());
100
101 // Select target instructions for the DAG.
Dan Gohmanbd3f8822008-08-21 16:36:34 +0000102 SelectRoot();
Dan Gohman14a66442008-08-23 02:25:05 +0000103 CurDAG->RemoveDeadNodes();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104}
105
Dan Gohman8181bd12008-07-27 21:46:04 +0000106SDNode *IA64DAGToDAGISel::SelectDIV(SDValue Op) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000107 SDNode *N = Op.getNode();
Dan Gohman8181bd12008-07-27 21:46:04 +0000108 SDValue Chain = N->getOperand(0);
109 SDValue Tmp1 = N->getOperand(0);
110 SDValue Tmp2 = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000111 AddToISelQueue(Chain);
112
113 AddToISelQueue(Tmp1);
114 AddToISelQueue(Tmp2);
115
116 bool isFP=false;
117
Duncan Sands92c43912008-06-06 12:08:01 +0000118 if(Tmp1.getValueType().isFloatingPoint())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119 isFP=true;
120
121 bool isModulus=false; // is it a division or a modulus?
122 bool isSigned=false;
123
124 switch(N->getOpcode()) {
125 case ISD::FDIV:
126 case ISD::SDIV: isModulus=false; isSigned=true; break;
127 case ISD::UDIV: isModulus=false; isSigned=false; break;
128 case ISD::FREM:
129 case ISD::SREM: isModulus=true; isSigned=true; break;
130 case ISD::UREM: isModulus=true; isSigned=false; break;
131 }
132
133 // TODO: check for integer divides by powers of 2 (or other simple patterns?)
134
Dan Gohman8181bd12008-07-27 21:46:04 +0000135 SDValue TmpPR, TmpPR2;
136 SDValue TmpF1, TmpF2, TmpF3, TmpF4, TmpF5, TmpF6, TmpF7, TmpF8;
137 SDValue TmpF9, TmpF10,TmpF11,TmpF12,TmpF13,TmpF14,TmpF15;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138 SDNode *Result;
139
140 // we'll need copies of F0 and F1
Dan Gohman8181bd12008-07-27 21:46:04 +0000141 SDValue F0 = CurDAG->getRegister(IA64::F0, MVT::f64);
142 SDValue F1 = CurDAG->getRegister(IA64::F1, MVT::f64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000143
144 // OK, emit some code:
145
146 if(!isFP) {
147 // first, load the inputs into FP regs.
148 TmpF1 =
Dan Gohman8181bd12008-07-27 21:46:04 +0000149 SDValue(CurDAG->getTargetNode(IA64::SETFSIG, MVT::f64, Tmp1), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000150 Chain = TmpF1.getValue(1);
151 TmpF2 =
Dan Gohman8181bd12008-07-27 21:46:04 +0000152 SDValue(CurDAG->getTargetNode(IA64::SETFSIG, MVT::f64, Tmp2), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000153 Chain = TmpF2.getValue(1);
154
155 // next, convert the inputs to FP
156 if(isSigned) {
157 TmpF3 =
Dan Gohman8181bd12008-07-27 21:46:04 +0000158 SDValue(CurDAG->getTargetNode(IA64::FCVTXF, MVT::f64, TmpF1), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000159 Chain = TmpF3.getValue(1);
160 TmpF4 =
Dan Gohman8181bd12008-07-27 21:46:04 +0000161 SDValue(CurDAG->getTargetNode(IA64::FCVTXF, MVT::f64, TmpF2), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162 Chain = TmpF4.getValue(1);
163 } else { // is unsigned
164 TmpF3 =
Dan Gohman8181bd12008-07-27 21:46:04 +0000165 SDValue(CurDAG->getTargetNode(IA64::FCVTXUFS1, MVT::f64, TmpF1), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000166 Chain = TmpF3.getValue(1);
167 TmpF4 =
Dan Gohman8181bd12008-07-27 21:46:04 +0000168 SDValue(CurDAG->getTargetNode(IA64::FCVTXUFS1, MVT::f64, TmpF2), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169 Chain = TmpF4.getValue(1);
170 }
171
172 } else { // this is an FP divide/remainder, so we 'leak' some temp
173 // regs and assign TmpF3=Tmp1, TmpF4=Tmp2
174 TmpF3=Tmp1;
175 TmpF4=Tmp2;
176 }
177
178 // we start by computing an approximate reciprocal (good to 9 bits?)
179 // note, this instruction writes _both_ TmpF5 (answer) and TmpPR (predicate)
180 if(isFP)
Dan Gohman8181bd12008-07-27 21:46:04 +0000181 TmpF5 = SDValue(CurDAG->getTargetNode(IA64::FRCPAS0, MVT::f64, MVT::i1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000182 TmpF3, TmpF4), 0);
183 else
Dan Gohman8181bd12008-07-27 21:46:04 +0000184 TmpF5 = SDValue(CurDAG->getTargetNode(IA64::FRCPAS1, MVT::f64, MVT::i1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000185 TmpF3, TmpF4), 0);
186
187 TmpPR = TmpF5.getValue(1);
188 Chain = TmpF5.getValue(2);
189
Dan Gohman8181bd12008-07-27 21:46:04 +0000190 SDValue minusB;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191 if(isModulus) { // for remainders, it'll be handy to have
192 // copies of -input_b
Dan Gohman8181bd12008-07-27 21:46:04 +0000193 minusB = SDValue(CurDAG->getTargetNode(IA64::SUB, MVT::i64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000194 CurDAG->getRegister(IA64::r0, MVT::i64), Tmp2), 0);
195 Chain = minusB.getValue(1);
196 }
197
Dan Gohman8181bd12008-07-27 21:46:04 +0000198 SDValue TmpE0, TmpY1, TmpE1, TmpY2;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000199
Dan Gohman8181bd12008-07-27 21:46:04 +0000200 SDValue OpsE0[] = { TmpF4, TmpF5, F1, TmpPR };
201 TmpE0 = SDValue(CurDAG->getTargetNode(IA64::CFNMAS1, MVT::f64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000202 OpsE0, 4), 0);
203 Chain = TmpE0.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000204 SDValue OpsY1[] = { TmpF5, TmpE0, TmpF5, TmpPR };
205 TmpY1 = SDValue(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000206 OpsY1, 4), 0);
207 Chain = TmpY1.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000208 SDValue OpsE1[] = { TmpE0, TmpE0, F0, TmpPR };
209 TmpE1 = SDValue(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000210 OpsE1, 4), 0);
211 Chain = TmpE1.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000212 SDValue OpsY2[] = { TmpY1, TmpE1, TmpY1, TmpPR };
213 TmpY2 = SDValue(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000214 OpsY2, 4), 0);
215 Chain = TmpY2.getValue(1);
216
217 if(isFP) { // if this is an FP divide, we finish up here and exit early
218 if(isModulus)
219 assert(0 && "Sorry, try another FORTRAN compiler.");
220
Dan Gohman8181bd12008-07-27 21:46:04 +0000221 SDValue TmpE2, TmpY3, TmpQ0, TmpR0;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000222
Dan Gohman8181bd12008-07-27 21:46:04 +0000223 SDValue OpsE2[] = { TmpE1, TmpE1, F0, TmpPR };
224 TmpE2 = SDValue(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000225 OpsE2, 4), 0);
226 Chain = TmpE2.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000227 SDValue OpsY3[] = { TmpY2, TmpE2, TmpY2, TmpPR };
228 TmpY3 = SDValue(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000229 OpsY3, 4), 0);
230 Chain = TmpY3.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000231 SDValue OpsQ0[] = { Tmp1, TmpY3, F0, TmpPR };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000232 TmpQ0 =
Dan Gohman8181bd12008-07-27 21:46:04 +0000233 SDValue(CurDAG->getTargetNode(IA64::CFMADS1, MVT::f64, // double prec!
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000234 OpsQ0, 4), 0);
235 Chain = TmpQ0.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000236 SDValue OpsR0[] = { Tmp2, TmpQ0, Tmp1, TmpPR };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000237 TmpR0 =
Dan Gohman8181bd12008-07-27 21:46:04 +0000238 SDValue(CurDAG->getTargetNode(IA64::CFNMADS1, MVT::f64, // double prec!
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000239 OpsR0, 4), 0);
240 Chain = TmpR0.getValue(1);
241
242// we want Result to have the same target register as the frcpa, so
243// we two-address hack it. See the comment "for this to work..." on
244// page 48 of Intel application note #245415
Dan Gohman8181bd12008-07-27 21:46:04 +0000245 SDValue Ops[] = { TmpF5, TmpY3, TmpR0, TmpQ0, TmpPR };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000246 Result = CurDAG->getTargetNode(IA64::TCFMADS0, MVT::f64, // d.p. s0 rndg!
247 Ops, 5);
Dan Gohman8181bd12008-07-27 21:46:04 +0000248 Chain = SDValue(Result, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000249 return Result; // XXX: early exit!
250 } else { // this is *not* an FP divide, so there's a bit left to do:
251
Dan Gohman8181bd12008-07-27 21:46:04 +0000252 SDValue TmpQ2, TmpR2, TmpQ3, TmpQ;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000253
Dan Gohman8181bd12008-07-27 21:46:04 +0000254 SDValue OpsQ2[] = { TmpF3, TmpY2, F0, TmpPR };
255 TmpQ2 = SDValue(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000256 OpsQ2, 4), 0);
257 Chain = TmpQ2.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000258 SDValue OpsR2[] = { TmpF4, TmpQ2, TmpF3, TmpPR };
259 TmpR2 = SDValue(CurDAG->getTargetNode(IA64::CFNMAS1, MVT::f64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000260 OpsR2, 4), 0);
261 Chain = TmpR2.getValue(1);
262
263// we want TmpQ3 to have the same target register as the frcpa? maybe we
264// should two-address hack it. See the comment "for this to work..." on page
265// 48 of Intel application note #245415
Dan Gohman8181bd12008-07-27 21:46:04 +0000266 SDValue OpsQ3[] = { TmpF5, TmpR2, TmpY2, TmpQ2, TmpPR };
267 TmpQ3 = SDValue(CurDAG->getTargetNode(IA64::TCFMAS1, MVT::f64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000268 OpsQ3, 5), 0);
269 Chain = TmpQ3.getValue(1);
270
271 // STORY: without these two-address instructions (TCFMAS1 and TCFMADS0)
272 // the FPSWA won't be able to help out in the case of large/tiny
273 // arguments. Other fun bugs may also appear, e.g. 0/x = x, not 0.
274
275 if(isSigned)
Dan Gohman8181bd12008-07-27 21:46:04 +0000276 TmpQ = SDValue(CurDAG->getTargetNode(IA64::FCVTFXTRUNCS1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000277 MVT::f64, TmpQ3), 0);
278 else
Dan Gohman8181bd12008-07-27 21:46:04 +0000279 TmpQ = SDValue(CurDAG->getTargetNode(IA64::FCVTFXUTRUNCS1,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000280 MVT::f64, TmpQ3), 0);
281
282 Chain = TmpQ.getValue(1);
283
284 if(isModulus) {
Dan Gohman8181bd12008-07-27 21:46:04 +0000285 SDValue FPminusB =
286 SDValue(CurDAG->getTargetNode(IA64::SETFSIG, MVT::f64, minusB), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287 Chain = FPminusB.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000288 SDValue Remainder =
289 SDValue(CurDAG->getTargetNode(IA64::XMAL, MVT::f64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000290 TmpQ, FPminusB, TmpF1), 0);
291 Chain = Remainder.getValue(1);
292 Result = CurDAG->getTargetNode(IA64::GETFSIG, MVT::i64, Remainder);
Dan Gohman8181bd12008-07-27 21:46:04 +0000293 Chain = SDValue(Result, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000294 } else { // just an integer divide
295 Result = CurDAG->getTargetNode(IA64::GETFSIG, MVT::i64, TmpQ);
Dan Gohman8181bd12008-07-27 21:46:04 +0000296 Chain = SDValue(Result, 1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000297 }
298
299 return Result;
300 } // wasn't an FP divide
301}
302
303// Select - Convert the specified operand from a target-independent to a
304// target-specific node if it hasn't already been changed.
Dan Gohman8181bd12008-07-27 21:46:04 +0000305SDNode *IA64DAGToDAGISel::Select(SDValue Op) {
Gabor Greif1c80d112008-08-28 21:40:38 +0000306 SDNode *N = Op.getNode();
Dan Gohmanbd68c792008-07-17 19:10:17 +0000307 if (N->isMachineOpcode())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000308 return NULL; // Already selected.
309
310 switch (N->getOpcode()) {
311 default: break;
312
313 case IA64ISD::BRCALL: { // XXX: this is also a hack!
Dan Gohman8181bd12008-07-27 21:46:04 +0000314 SDValue Chain = N->getOperand(0);
315 SDValue InFlag; // Null incoming flag value.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000316
317 AddToISelQueue(Chain);
318 if(N->getNumOperands()==3) { // we have an incoming chain, callee and flag
319 InFlag = N->getOperand(2);
320 AddToISelQueue(InFlag);
321 }
322
323 unsigned CallOpcode;
Dan Gohman8181bd12008-07-27 21:46:04 +0000324 SDValue CallOperand;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000325
326 // if we can call directly, do so
327 if (GlobalAddressSDNode *GASD =
328 dyn_cast<GlobalAddressSDNode>(N->getOperand(1))) {
329 CallOpcode = IA64::BRCALL_IPREL_GA;
330 CallOperand = CurDAG->getTargetGlobalAddress(GASD->getGlobal(), MVT::i64);
Bill Wendlingfef06052008-09-16 21:48:12 +0000331 } else if (isa<ExternalSymbolSDNode>(N->getOperand(1))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000332 // FIXME: we currently NEED this case for correctness, to avoid
333 // "non-pic code with imm reloc.n against dynamic symbol" errors
334 CallOpcode = IA64::BRCALL_IPREL_ES;
335 CallOperand = N->getOperand(1);
336 } else {
337 // otherwise we need to load the function descriptor,
338 // load the branch target (function)'s entry point and GP,
339 // branch (call) then restore the GP
Dan Gohman8181bd12008-07-27 21:46:04 +0000340 SDValue FnDescriptor = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000341 AddToISelQueue(FnDescriptor);
342
343 // load the branch target's entry point [mem] and
344 // GP value [mem+8]
Dan Gohman8181bd12008-07-27 21:46:04 +0000345 SDValue targetEntryPoint=
346 SDValue(CurDAG->getTargetNode(IA64::LD8, MVT::i64, MVT::Other,
Chris Lattnerd04567e2008-05-28 04:25:57 +0000347 FnDescriptor, CurDAG->getEntryNode()), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000348 Chain = targetEntryPoint.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000349 SDValue targetGPAddr=
350 SDValue(CurDAG->getTargetNode(IA64::ADDS, MVT::i64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000351 FnDescriptor,
352 CurDAG->getConstant(8, MVT::i64)), 0);
353 Chain = targetGPAddr.getValue(1);
Dan Gohman8181bd12008-07-27 21:46:04 +0000354 SDValue targetGP =
355 SDValue(CurDAG->getTargetNode(IA64::LD8, MVT::i64,MVT::Other,
Chris Lattnerd04567e2008-05-28 04:25:57 +0000356 targetGPAddr, CurDAG->getEntryNode()), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000357 Chain = targetGP.getValue(1);
358
359 Chain = CurDAG->getCopyToReg(Chain, IA64::r1, targetGP, InFlag);
360 InFlag = Chain.getValue(1);
Gabor Greif4e8901a2008-08-30 10:09:02 +0000361 Chain = CurDAG->getCopyToReg(Chain, IA64::B6,
362 targetEntryPoint, InFlag); // FLAG these?
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000363 InFlag = Chain.getValue(1);
364
365 CallOperand = CurDAG->getRegister(IA64::B6, MVT::i64);
366 CallOpcode = IA64::BRCALL_INDIRECT;
367 }
368
369 // Finally, once everything is setup, emit the call itself
Gabor Greif1c80d112008-08-28 21:40:38 +0000370 if (InFlag.getNode())
Dan Gohman8181bd12008-07-27 21:46:04 +0000371 Chain = SDValue(CurDAG->getTargetNode(CallOpcode, MVT::Other, MVT::Flag,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000372 CallOperand, InFlag), 0);
373 else // there might be no arguments
Dan Gohman8181bd12008-07-27 21:46:04 +0000374 Chain = SDValue(CurDAG->getTargetNode(CallOpcode, MVT::Other, MVT::Flag,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000375 CallOperand, Chain), 0);
376 InFlag = Chain.getValue(1);
377
Dan Gohman8181bd12008-07-27 21:46:04 +0000378 std::vector<SDValue> CallResults;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000379
380 CallResults.push_back(Chain);
381 CallResults.push_back(InFlag);
382
383 for (unsigned i = 0, e = CallResults.size(); i != e; ++i)
384 ReplaceUses(Op.getValue(i), CallResults[i]);
385 return NULL;
386 }
387
388 case IA64ISD::GETFD: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000389 SDValue Input = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000390 AddToISelQueue(Input);
391 return CurDAG->getTargetNode(IA64::GETFD, MVT::i64, Input);
392 }
393
394 case ISD::FDIV:
395 case ISD::SDIV:
396 case ISD::UDIV:
397 case ISD::SREM:
398 case ISD::UREM:
399 return SelectDIV(Op);
400
401 case ISD::TargetConstantFP: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000402 SDValue Chain = CurDAG->getEntryNode(); // this is a constant, so..
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000403
Dan Gohman8181bd12008-07-27 21:46:04 +0000404 SDValue V;
Dale Johannesen76844472007-08-31 17:03:33 +0000405 ConstantFPSDNode* N2 = cast<ConstantFPSDNode>(N);
406 if (N2->getValueAPF().isPosZero()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000407 V = CurDAG->getCopyFromReg(Chain, IA64::F0, MVT::f64);
Dale Johannesen76844472007-08-31 17:03:33 +0000408 } else if (N2->isExactlyValue(N2->getValueType(0) == MVT::f32 ?
409 APFloat(+1.0f) : APFloat(+1.0))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000410 V = CurDAG->getCopyFromReg(Chain, IA64::F1, MVT::f64);
411 } else
412 assert(0 && "Unexpected FP constant!");
413
Dan Gohman8181bd12008-07-27 21:46:04 +0000414 ReplaceUses(SDValue(N, 0), V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000415 return 0;
416 }
417
418 case ISD::FrameIndex: { // TODO: reduce creepyness
419 int FI = cast<FrameIndexSDNode>(N)->getIndex();
420 if (N->hasOneUse())
421 return CurDAG->SelectNodeTo(N, IA64::MOV, MVT::i64,
422 CurDAG->getTargetFrameIndex(FI, MVT::i64));
423 else
424 return CurDAG->getTargetNode(IA64::MOV, MVT::i64,
425 CurDAG->getTargetFrameIndex(FI, MVT::i64));
426 }
427
428 case ISD::ConstantPool: { // TODO: nuke the constant pool
429 // (ia64 doesn't need one)
430 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
431 Constant *C = CP->getConstVal();
Dan Gohman8181bd12008-07-27 21:46:04 +0000432 SDValue CPI = CurDAG->getTargetConstantPool(C, MVT::i64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000433 CP->getAlignment());
434 return CurDAG->getTargetNode(IA64::ADDL_GA, MVT::i64, // ?
435 CurDAG->getRegister(IA64::r1, MVT::i64), CPI);
436 }
437
438 case ISD::GlobalAddress: {
439 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
Dan Gohman8181bd12008-07-27 21:46:04 +0000440 SDValue GA = CurDAG->getTargetGlobalAddress(GV, MVT::i64);
441 SDValue Tmp =
442 SDValue(CurDAG->getTargetNode(IA64::ADDL_GA, MVT::i64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000443 CurDAG->getRegister(IA64::r1,
444 MVT::i64), GA), 0);
Chris Lattner7e03fd62008-07-09 05:12:07 +0000445 return CurDAG->getTargetNode(IA64::LD8, MVT::i64, MVT::Other, Tmp,
446 CurDAG->getEntryNode());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000447 }
448
449/* XXX
Bill Wendlingfef06052008-09-16 21:48:12 +0000450 case ISD::ExternalSymbol: {
451 SDValue EA = CurDAG->getTargetExternalSymbol(
452 cast<ExternalSymbolSDNode>(N)->getSymbol(),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000453 MVT::i64);
Dan Gohman8181bd12008-07-27 21:46:04 +0000454 SDValue Tmp = CurDAG->getTargetNode(IA64::ADDL_EA, MVT::i64,
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000455 CurDAG->getRegister(IA64::r1,
456 MVT::i64),
457 EA);
458 return CurDAG->getTargetNode(IA64::LD8, MVT::i64, Tmp);
459 }
460*/
461
462 case ISD::LOAD: { // FIXME: load -1, not 1, for bools?
463 LoadSDNode *LD = cast<LoadSDNode>(N);
Dan Gohman8181bd12008-07-27 21:46:04 +0000464 SDValue Chain = LD->getChain();
465 SDValue Address = LD->getBasePtr();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000466 AddToISelQueue(Chain);
467 AddToISelQueue(Address);
468
Duncan Sands92c43912008-06-06 12:08:01 +0000469 MVT TypeBeingLoaded = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000470 unsigned Opc;
Duncan Sands92c43912008-06-06 12:08:01 +0000471 switch (TypeBeingLoaded.getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000472 default:
473#ifndef NDEBUG
474 N->dump(CurDAG);
475#endif
476 assert(0 && "Cannot load this type!");
477 case MVT::i1: { // this is a bool
478 Opc = IA64::LD1; // first we load a byte, then compare for != 0
479 if(N->getValueType(0) == MVT::i1) { // XXX: early exit!
480 return CurDAG->SelectNodeTo(N, IA64::CMPNE, MVT::i1, MVT::Other,
Dan Gohman8181bd12008-07-27 21:46:04 +0000481 SDValue(CurDAG->getTargetNode(Opc, MVT::i64, Address), 0),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000482 CurDAG->getRegister(IA64::r0, MVT::i64),
483 Chain);
484 }
485 /* otherwise, we want to load a bool into something bigger: LD1
486 will do that for us, so we just fall through */
487 }
488 case MVT::i8: Opc = IA64::LD1; break;
489 case MVT::i16: Opc = IA64::LD2; break;
490 case MVT::i32: Opc = IA64::LD4; break;
491 case MVT::i64: Opc = IA64::LD8; break;
492
493 case MVT::f32: Opc = IA64::LDF4; break;
494 case MVT::f64: Opc = IA64::LDF8; break;
495 }
496
497 // TODO: comment this
498 return CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), MVT::Other,
499 Address, Chain);
500 }
501
502 case ISD::STORE: {
503 StoreSDNode *ST = cast<StoreSDNode>(N);
Dan Gohman8181bd12008-07-27 21:46:04 +0000504 SDValue Address = ST->getBasePtr();
505 SDValue Chain = ST->getChain();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000506 AddToISelQueue(Address);
507 AddToISelQueue(Chain);
508
509 unsigned Opc;
510 if (ISD::isNON_TRUNCStore(N)) {
Duncan Sands92c43912008-06-06 12:08:01 +0000511 switch (N->getOperand(1).getValueType().getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000512 default: assert(0 && "unknown type in store");
513 case MVT::i1: { // this is a bool
514 Opc = IA64::ST1; // we store either 0 or 1 as a byte
515 // first load zero!
Dan Gohman8181bd12008-07-27 21:46:04 +0000516 SDValue Initial = CurDAG->getCopyFromReg(Chain, IA64::r0, MVT::i64);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000517 Chain = Initial.getValue(1);
518 // then load 1 into the same reg iff the predicate to store is 1
Dan Gohman8181bd12008-07-27 21:46:04 +0000519 SDValue Tmp = ST->getValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000520 AddToISelQueue(Tmp);
521 Tmp =
Dan Gohman8181bd12008-07-27 21:46:04 +0000522 SDValue(CurDAG->getTargetNode(IA64::TPCADDS, MVT::i64, Initial,
Gabor Greif4e8901a2008-08-30 10:09:02 +0000523 CurDAG->getTargetConstant(1,
524 MVT::i64),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000525 Tmp), 0);
526 return CurDAG->SelectNodeTo(N, Opc, MVT::Other, Address, Tmp, Chain);
527 }
528 case MVT::i64: Opc = IA64::ST8; break;
529 case MVT::f64: Opc = IA64::STF8; break;
530 }
531 } else { // Truncating store
Duncan Sands92c43912008-06-06 12:08:01 +0000532 switch(ST->getMemoryVT().getSimpleVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000533 default: assert(0 && "unknown type in truncstore");
534 case MVT::i8: Opc = IA64::ST1; break;
535 case MVT::i16: Opc = IA64::ST2; break;
536 case MVT::i32: Opc = IA64::ST4; break;
537 case MVT::f32: Opc = IA64::STF4; break;
538 }
539 }
540
Dan Gohman8181bd12008-07-27 21:46:04 +0000541 SDValue N1 = N->getOperand(1);
542 SDValue N2 = N->getOperand(2);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000543 AddToISelQueue(N1);
544 AddToISelQueue(N2);
545 return CurDAG->SelectNodeTo(N, Opc, MVT::Other, N2, N1, Chain);
546 }
547
548 case ISD::BRCOND: {
Dan Gohman8181bd12008-07-27 21:46:04 +0000549 SDValue Chain = N->getOperand(0);
550 SDValue CC = N->getOperand(1);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000551 AddToISelQueue(Chain);
552 AddToISelQueue(CC);
553 MachineBasicBlock *Dest =
554 cast<BasicBlockSDNode>(N->getOperand(2))->getBasicBlock();
555 //FIXME - we do NOT need long branches all the time
556 return CurDAG->SelectNodeTo(N, IA64::BRLCOND_NOTCALL, MVT::Other, CC,
557 CurDAG->getBasicBlock(Dest), Chain);
558 }
559
560 case ISD::CALLSEQ_START:
561 case ISD::CALLSEQ_END: {
Dan Gohmanfaeb4a32008-09-12 16:56:44 +0000562 int64_t Amt = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000563 unsigned Opc = N->getOpcode() == ISD::CALLSEQ_START ?
564 IA64::ADJUSTCALLSTACKDOWN : IA64::ADJUSTCALLSTACKUP;
Dan Gohman8181bd12008-07-27 21:46:04 +0000565 SDValue N0 = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000566 AddToISelQueue(N0);
567 return CurDAG->SelectNodeTo(N, Opc, MVT::Other, getI64Imm(Amt), N0);
568 }
569
570 case ISD::BR:
571 // FIXME: we don't need long branches all the time!
Dan Gohman8181bd12008-07-27 21:46:04 +0000572 SDValue N0 = N->getOperand(0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000573 AddToISelQueue(N0);
574 return CurDAG->SelectNodeTo(N, IA64::BRL_NOTCALL, MVT::Other,
575 N->getOperand(1), N0);
576 }
577
578 return SelectCode(Op);
579}
580
581
582/// createIA64DAGToDAGInstructionSelector - This pass converts a legalized DAG
583/// into an IA64-specific DAG, ready for instruction scheduling.
584///
585FunctionPass
586*llvm::createIA64DAGToDAGInstructionSelector(IA64TargetMachine &TM) {
587 return new IA64DAGToDAGISel(TM);
588}
589