blob: f61c9e1e535dc04c32ab4eda05fd81e03e3cc49d [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 Cheng2ef88a02006-08-07 22:28:20 +000031#include <queue>
Evan Chengba2f0a92006-02-05 06:46:41 +000032#include <set>
Duraid Madinaf2db9b82005-10-28 17:46:35 +000033using namespace llvm;
34
35namespace {
36 Statistic<> FusedFP ("ia64-codegen", "Number of fused fp operations");
37 Statistic<> FrameOff("ia64-codegen", "Number of frame idx offsets collapsed");
38
39 //===--------------------------------------------------------------------===//
40 /// IA64DAGToDAGISel - IA64 specific code to select IA64 machine
41 /// instructions for SelectionDAG operations.
42 ///
43 class IA64DAGToDAGISel : public SelectionDAGISel {
44 IA64TargetLowering IA64Lowering;
45 unsigned GlobalBaseReg;
46 public:
Evan Chengc4c62572006-03-13 23:20:37 +000047 IA64DAGToDAGISel(IA64TargetMachine &TM)
48 : SelectionDAGISel(IA64Lowering), IA64Lowering(*TM.getTargetLowering()) {}
Duraid Madinaf2db9b82005-10-28 17:46:35 +000049
50 virtual bool runOnFunction(Function &Fn) {
51 // Make sure we re-emit a set of the global base reg if necessary
52 GlobalBaseReg = 0;
53 return SelectionDAGISel::runOnFunction(Fn);
54 }
55
56 /// getI64Imm - Return a target constant with the specified value, of type
57 /// i64.
58 inline SDOperand getI64Imm(uint64_t Imm) {
59 return CurDAG->getTargetConstant(Imm, MVT::i64);
60 }
61
62 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
63 /// base register. Return the virtual register that holds this value.
64 // SDOperand getGlobalBaseReg(); TODO: hmm
65
66 // Select - Convert the specified operand from a target-independent to a
67 // target-specific node if it hasn't already been changed.
Evan Cheng64a752f2006-08-11 09:08:15 +000068 SDNode *Select(SDOperand &Result, SDOperand N);
Duraid Madinaf2db9b82005-10-28 17:46:35 +000069
70 SDNode *SelectIntImmediateExpr(SDOperand LHS, SDOperand RHS,
71 unsigned OCHi, unsigned OCLo,
72 bool IsArithmetic = false,
73 bool Negate = false);
74 SDNode *SelectBitfieldInsert(SDNode *N);
75
76 /// SelectCC - Select a comparison of the specified values with the
77 /// specified condition code, returning the CR# of the expression.
78 SDOperand SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC);
79
80 /// SelectAddr - Given the specified address, return the two operands for a
81 /// load/store instruction, and return true if it should be an indexed [r+r]
82 /// operation.
83 bool SelectAddr(SDOperand Addr, SDOperand &Op1, SDOperand &Op2);
84
Duraid Madinaf2db9b82005-10-28 17:46:35 +000085 /// InstructionSelectBasicBlock - This callback is invoked by
86 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
87 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
88
89 virtual const char *getPassName() const {
90 return "IA64 (Itanium) DAG->DAG Instruction Selector";
91 }
92
93// Include the pieces autogenerated from the target description.
94#include "IA64GenDAGISel.inc"
95
96private:
Duraid Madinab6f023a2005-11-21 14:14:54 +000097 SDOperand SelectDIV(SDOperand Op);
Duraid Madinaf2db9b82005-10-28 17:46:35 +000098 };
99}
100
101/// InstructionSelectBasicBlock - This callback is invoked by
102/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
103void IA64DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
104 DEBUG(BB->dump());
Evan Cheng33e9ad92006-07-27 06:40:15 +0000105
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000106 // Select target instructions for the DAG.
Evan Chengba2f0a92006-02-05 06:46:41 +0000107 DAG.setRoot(SelectRoot(DAG.getRoot()));
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000108 DAG.RemoveDeadNodes();
109
110 // Emit machine code to BB.
111 ScheduleAndEmitDAG(DAG);
112}
113
Duraid Madinab6f023a2005-11-21 14:14:54 +0000114SDOperand IA64DAGToDAGISel::SelectDIV(SDOperand Op) {
115 SDNode *N = Op.Val;
Evan Cheng6da2f322006-08-26 01:07:58 +0000116 SDOperand Chain = N->getOperand(0);
117 SDOperand Tmp1 = N->getOperand(0);
118 SDOperand Tmp2 = N->getOperand(1);
119 AddToISelQueue(Chain);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000120
Evan Cheng6da2f322006-08-26 01:07:58 +0000121 AddToISelQueue(Tmp1);
122 AddToISelQueue(Tmp2);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000123
124 bool isFP=false;
125
126 if(MVT::isFloatingPoint(Tmp1.getValueType()))
127 isFP=true;
128
129 bool isModulus=false; // is it a division or a modulus?
130 bool isSigned=false;
131
132 switch(N->getOpcode()) {
133 case ISD::FDIV:
134 case ISD::SDIV: isModulus=false; isSigned=true; break;
135 case ISD::UDIV: isModulus=false; isSigned=false; break;
136 case ISD::FREM:
137 case ISD::SREM: isModulus=true; isSigned=true; break;
138 case ISD::UREM: isModulus=true; isSigned=false; break;
139 }
140
141 // TODO: check for integer divides by powers of 2 (or other simple patterns?)
142
143 SDOperand TmpPR, TmpPR2;
144 SDOperand TmpF1, TmpF2, TmpF3, TmpF4, TmpF5, TmpF6, TmpF7, TmpF8;
145 SDOperand TmpF9, TmpF10,TmpF11,TmpF12,TmpF13,TmpF14,TmpF15;
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000146 SDNode *Result;
Duraid Madina76bb6ae2006-01-16 14:33:04 +0000147
148 // we'll need copies of F0 and F1
149 SDOperand F0 = CurDAG->getRegister(IA64::F0, MVT::f64);
150 SDOperand F1 = CurDAG->getRegister(IA64::F1, MVT::f64);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000151
152 // OK, emit some code:
153
154 if(!isFP) {
155 // first, load the inputs into FP regs.
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000156 TmpF1 =
157 SDOperand(CurDAG->getTargetNode(IA64::SETFSIG, MVT::f64, Tmp1), 0);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000158 Chain = TmpF1.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000159 TmpF2 =
160 SDOperand(CurDAG->getTargetNode(IA64::SETFSIG, MVT::f64, Tmp2), 0);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000161 Chain = TmpF2.getValue(1);
162
163 // next, convert the inputs to FP
164 if(isSigned) {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000165 TmpF3 =
166 SDOperand(CurDAG->getTargetNode(IA64::FCVTXF, MVT::f64, TmpF1), 0);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000167 Chain = TmpF3.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000168 TmpF4 =
169 SDOperand(CurDAG->getTargetNode(IA64::FCVTXF, MVT::f64, TmpF2), 0);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000170 Chain = TmpF4.getValue(1);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000171 } else { // is unsigned
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000172 TmpF3 =
173 SDOperand(CurDAG->getTargetNode(IA64::FCVTXUFS1, MVT::f64, TmpF1), 0);
Duraid Madina76bb6ae2006-01-16 14:33:04 +0000174 Chain = TmpF3.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000175 TmpF4 =
176 SDOperand(CurDAG->getTargetNode(IA64::FCVTXUFS1, MVT::f64, TmpF2), 0);
Duraid Madina76bb6ae2006-01-16 14:33:04 +0000177 Chain = TmpF4.getValue(1);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000178 }
179
180 } else { // this is an FP divide/remainder, so we 'leak' some temp
181 // regs and assign TmpF3=Tmp1, TmpF4=Tmp2
182 TmpF3=Tmp1;
183 TmpF4=Tmp2;
184 }
185
186 // we start by computing an approximate reciprocal (good to 9 bits?)
187 // note, this instruction writes _both_ TmpF5 (answer) and TmpPR (predicate)
Duraid Madina0c81dc82006-01-16 06:33:38 +0000188 if(isFP)
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000189 TmpF5 = SDOperand(CurDAG->getTargetNode(IA64::FRCPAS0, MVT::f64, MVT::i1,
190 TmpF3, TmpF4), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000191 else
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000192 TmpF5 = SDOperand(CurDAG->getTargetNode(IA64::FRCPAS1, MVT::f64, MVT::i1,
193 TmpF3, TmpF4), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000194
Duraid Madinab6f023a2005-11-21 14:14:54 +0000195 TmpPR = TmpF5.getValue(1);
196 Chain = TmpF5.getValue(2);
197
Duraid Madina0c81dc82006-01-16 06:33:38 +0000198 SDOperand minusB;
199 if(isModulus) { // for remainders, it'll be handy to have
200 // copies of -input_b
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000201 minusB = SDOperand(CurDAG->getTargetNode(IA64::SUB, MVT::i64,
202 CurDAG->getRegister(IA64::r0, MVT::i64), Tmp2), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000203 Chain = minusB.getValue(1);
204 }
205
206 SDOperand TmpE0, TmpY1, TmpE1, TmpY2;
207
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000208 TmpE0 = SDOperand(CurDAG->getTargetNode(IA64::CFNMAS1, MVT::f64,
209 TmpF4, TmpF5, F1, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000210 Chain = TmpE0.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000211 TmpY1 = SDOperand(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
212 TmpF5, TmpE0, TmpF5, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000213 Chain = TmpY1.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000214 TmpE1 = SDOperand(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
215 TmpE0, TmpE0, F0, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000216 Chain = TmpE1.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000217 TmpY2 = SDOperand(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
218 TmpY1, TmpE1, TmpY1, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000219 Chain = TmpY2.getValue(1);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000220
Duraid Madina0c81dc82006-01-16 06:33:38 +0000221 if(isFP) { // if this is an FP divide, we finish up here and exit early
222 if(isModulus)
223 assert(0 && "Sorry, try another FORTRAN compiler.");
224
225 SDOperand TmpE2, TmpY3, TmpQ0, TmpR0;
226
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000227 TmpE2 = SDOperand(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
228 TmpE1, TmpE1, F0, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000229 Chain = TmpE2.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000230 TmpY3 = SDOperand(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
231 TmpY2, TmpE2, TmpY2, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000232 Chain = TmpY3.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000233 TmpQ0 =
234 SDOperand(CurDAG->getTargetNode(IA64::CFMADS1, MVT::f64, // double prec!
235 Tmp1, TmpY3, F0, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000236 Chain = TmpQ0.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000237 TmpR0 =
238 SDOperand(CurDAG->getTargetNode(IA64::CFNMADS1, MVT::f64, // double prec!
239 Tmp2, TmpQ0, Tmp1, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000240 Chain = TmpR0.getValue(1);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000241
Duraid Madina0c81dc82006-01-16 06:33:38 +0000242// 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
245 Result = CurDAG->getTargetNode(IA64::TCFMADS0, MVT::f64, // d.p. s0 rndg!
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000246 TmpF5, TmpY3, TmpR0, TmpQ0, TmpPR);
247 Chain = SDOperand(Result, 1);
248 return SDOperand(Result, 0); // XXX: early exit!
Duraid Madina0c81dc82006-01-16 06:33:38 +0000249 } else { // this is *not* an FP divide, so there's a bit left to do:
250
251 SDOperand TmpQ2, TmpR2, TmpQ3, TmpQ;
252
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000253 TmpQ2 = SDOperand(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
254 TmpF3, TmpY2, F0, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000255 Chain = TmpQ2.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000256 TmpR2 = SDOperand(CurDAG->getTargetNode(IA64::CFNMAS1, MVT::f64,
257 TmpF4, TmpQ2, TmpF3, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000258 Chain = TmpR2.getValue(1);
Duraid Madinaae6dcdd2006-01-17 01:19:49 +0000259
Duraid Madina76bb6ae2006-01-16 14:33:04 +0000260// 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
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000263 TmpQ3 = SDOperand(CurDAG->getTargetNode(IA64::TCFMAS1, MVT::f64,
264 TmpF5, TmpR2, TmpY2, TmpQ2, TmpPR), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000265 Chain = TmpQ3.getValue(1);
Duraid Madina76bb6ae2006-01-16 14:33:04 +0000266
Duraid Madinaae6dcdd2006-01-17 01:19:49 +0000267 // STORY: without these two-address instructions (TCFMAS1 and TCFMADS0)
268 // the FPSWA won't be able to help out in the case of large/tiny
269 // arguments. Other fun bugs may also appear, e.g. 0/x = x, not 0.
270
Duraid Madina0c81dc82006-01-16 06:33:38 +0000271 if(isSigned)
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000272 TmpQ = SDOperand(CurDAG->getTargetNode(IA64::FCVTFXTRUNCS1,
273 MVT::f64, TmpQ3), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000274 else
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000275 TmpQ = SDOperand(CurDAG->getTargetNode(IA64::FCVTFXUTRUNCS1,
276 MVT::f64, TmpQ3), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000277
278 Chain = TmpQ.getValue(1);
279
280 if(isModulus) {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000281 SDOperand FPminusB =
282 SDOperand(CurDAG->getTargetNode(IA64::SETFSIG, MVT::f64, minusB), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000283 Chain = FPminusB.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000284 SDOperand Remainder =
285 SDOperand(CurDAG->getTargetNode(IA64::XMAL, MVT::f64,
286 TmpQ, FPminusB, TmpF1), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000287 Chain = Remainder.getValue(1);
288 Result = CurDAG->getTargetNode(IA64::GETFSIG, MVT::i64, Remainder);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000289 Chain = SDOperand(Result, 1);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000290 } else { // just an integer divide
291 Result = CurDAG->getTargetNode(IA64::GETFSIG, MVT::i64, TmpQ);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000292 Chain = SDOperand(Result, 1);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000293 }
294
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000295 return SDOperand(Result, 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000296 } // wasn't an FP divide
Duraid Madinab6f023a2005-11-21 14:14:54 +0000297}
298
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000299// Select - Convert the specified operand from a target-independent to a
300// target-specific node if it hasn't already been changed.
Evan Cheng64a752f2006-08-11 09:08:15 +0000301SDNode *IA64DAGToDAGISel::Select(SDOperand &Result, SDOperand Op) {
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000302 SDNode *N = Op.Val;
303 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
Evan Cheng34167212006-02-09 00:37:58 +0000304 N->getOpcode() < IA64ISD::FIRST_NUMBER) {
305 Result = Op;
Evan Cheng64a752f2006-08-11 09:08:15 +0000306 return NULL; // Already selected.
Evan Cheng34167212006-02-09 00:37:58 +0000307 }
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000308
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000309 switch (N->getOpcode()) {
310 default: break;
311
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000312 case IA64ISD::BRCALL: { // XXX: this is also a hack!
Evan Cheng6da2f322006-08-26 01:07:58 +0000313 SDOperand Chain = N->getOperand(0);
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000314 SDOperand InFlag; // Null incoming flag value.
315
Evan Cheng6da2f322006-08-26 01:07:58 +0000316 AddToISelQueue(Chain);
317 if(N->getNumOperands()==3) { // we have an incoming chain, callee and flag
318 InFlag = N->getOperand(2);
319 AddToISelQueue(InFlag);
320 }
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000321
322 unsigned CallOpcode;
323 SDOperand CallOperand;
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000324
325 // if we can call directly, do so
326 if (GlobalAddressSDNode *GASD =
327 dyn_cast<GlobalAddressSDNode>(N->getOperand(1))) {
328 CallOpcode = IA64::BRCALL_IPREL_GA;
329 CallOperand = CurDAG->getTargetGlobalAddress(GASD->getGlobal(), MVT::i64);
330 } else if (ExternalSymbolSDNode *ESSDN = // FIXME: we currently NEED this
331 // case for correctness, to avoid
332 // "non-pic code with imm reloc.n
333 // against dynamic symbol" errors
334 dyn_cast<ExternalSymbolSDNode>(N->getOperand(1))) {
335 CallOpcode = IA64::BRCALL_IPREL_ES;
336 CallOperand = N->getOperand(1);
337 } else {
338 // otherwise we need to load the function descriptor,
339 // load the branch target (function)'s entry point and GP,
340 // branch (call) then restore the GP
Evan Cheng6da2f322006-08-26 01:07:58 +0000341 SDOperand FnDescriptor = N->getOperand(1);
342 AddToISelQueue(FnDescriptor);
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000343
344 // load the branch target's entry point [mem] and
345 // GP value [mem+8]
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000346 SDOperand targetEntryPoint=
347 SDOperand(CurDAG->getTargetNode(IA64::LD8, MVT::i64, FnDescriptor), 0);
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000348 Chain = targetEntryPoint.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000349 SDOperand targetGPAddr=
350 SDOperand(CurDAG->getTargetNode(IA64::ADDS, MVT::i64,
351 FnDescriptor, CurDAG->getConstant(8, MVT::i64)), 0);
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000352 Chain = targetGPAddr.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000353 SDOperand targetGP=
354 SDOperand(CurDAG->getTargetNode(IA64::LD8, MVT::i64, targetGPAddr), 0);
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000355 Chain = targetGP.getValue(1);
356
357 Chain = CurDAG->getCopyToReg(Chain, IA64::r1, targetGP, InFlag);
358 InFlag = Chain.getValue(1);
359 Chain = CurDAG->getCopyToReg(Chain, IA64::B6, targetEntryPoint, InFlag); // FLAG these?
360 InFlag = Chain.getValue(1);
361
362 CallOperand = CurDAG->getRegister(IA64::B6, MVT::i64);
363 CallOpcode = IA64::BRCALL_INDIRECT;
364 }
365
366 // Finally, once everything is setup, emit the call itself
367 if(InFlag.Val)
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000368 Chain = SDOperand(CurDAG->getTargetNode(CallOpcode, MVT::Other, MVT::Flag,
369 CallOperand, InFlag), 0);
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000370 else // there might be no arguments
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000371 Chain = SDOperand(CurDAG->getTargetNode(CallOpcode, MVT::Other, MVT::Flag,
372 CallOperand, Chain), 0);
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000373 InFlag = Chain.getValue(1);
374
375 std::vector<SDOperand> CallResults;
376
377 CallResults.push_back(Chain);
378 CallResults.push_back(InFlag);
379
380 for (unsigned i = 0, e = CallResults.size(); i != e; ++i)
Evan Cheng2ef88a02006-08-07 22:28:20 +0000381 ReplaceUses(Op.getValue(i), CallResults[i]);
Evan Cheng34167212006-02-09 00:37:58 +0000382 Result = CallResults[Op.ResNo];
Evan Cheng64a752f2006-08-11 09:08:15 +0000383 return NULL;
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000384 }
Duraid Madinaa36153a2005-12-22 03:58:17 +0000385
Duraid Madina8617f3c2005-12-22 07:14:45 +0000386 case IA64ISD::GETFD: {
Evan Cheng6da2f322006-08-26 01:07:58 +0000387 SDOperand Input = N->getOperand(0);
388 AddToISelQueue(Input);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000389 Result = SDOperand(CurDAG->getTargetNode(IA64::GETFD, MVT::i64, Input), 0);
Evan Cheng64a752f2006-08-11 09:08:15 +0000390 return Result.Val;
Duraid Madina8617f3c2005-12-22 07:14:45 +0000391 }
392
Duraid Madinab6f023a2005-11-21 14:14:54 +0000393 case ISD::FDIV:
394 case ISD::SDIV:
395 case ISD::UDIV:
396 case ISD::SREM:
Evan Cheng34167212006-02-09 00:37:58 +0000397 case ISD::UREM:
398 Result = SelectDIV(Op);
Evan Cheng64a752f2006-08-11 09:08:15 +0000399 return Result.Val;
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000400
Chris Lattnera54aa942006-01-29 06:26:08 +0000401 case ISD::TargetConstantFP: {
Duraid Madina056728f2005-11-02 07:32:59 +0000402 SDOperand Chain = CurDAG->getEntryNode(); // this is a constant, so..
403
Evan Cheng34167212006-02-09 00:37:58 +0000404 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0)) {
Evan Cheng23329f52006-08-16 07:30:09 +0000405 return CurDAG->getCopyFromReg(Chain, IA64::F0, MVT::f64).Val;
Evan Cheng34167212006-02-09 00:37:58 +0000406 } else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0)) {
Evan Cheng23329f52006-08-16 07:30:09 +0000407 return CurDAG->getCopyFromReg(Chain, IA64::F1, MVT::f64).Val;
Evan Cheng34167212006-02-09 00:37:58 +0000408 } else
Duraid Madina93856802005-11-02 02:35:04 +0000409 assert(0 && "Unexpected FP constant!");
410 }
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000411
412 case ISD::FrameIndex: { // TODO: reduce creepyness
413 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Evan Cheng23329f52006-08-16 07:30:09 +0000414 if (N->hasOneUse())
415 return CurDAG->SelectNodeTo(N, IA64::MOV, MVT::i64,
416 CurDAG->getTargetFrameIndex(FI, MVT::i64)).Val;
417 else
418 return SDOperand(CurDAG->getTargetNode(IA64::MOV, MVT::i64,
419 CurDAG->getTargetFrameIndex(FI, MVT::i64)), 0).Val;
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000420 }
421
Duraid Madina2e0348e2006-01-15 09:45:23 +0000422 case ISD::ConstantPool: { // TODO: nuke the constant pool
423 // (ia64 doesn't need one)
Evan Chengb8973bd2006-01-31 22:23:14 +0000424 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
425 Constant *C = CP->get();
426 SDOperand CPI = CurDAG->getTargetConstantPool(C, MVT::i64,
427 CP->getAlignment());
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000428 Result = SDOperand(CurDAG->getTargetNode(IA64::ADDL_GA, MVT::i64, // ?
429 CurDAG->getRegister(IA64::r1, MVT::i64), CPI), 0);
Evan Cheng64a752f2006-08-11 09:08:15 +0000430 return Result.Val;
Duraid Madina25d0a882005-10-29 16:08:30 +0000431 }
432
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000433 case ISD::GlobalAddress: {
434 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
435 SDOperand GA = CurDAG->getTargetGlobalAddress(GV, MVT::i64);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000436 SDOperand Tmp = SDOperand(CurDAG->getTargetNode(IA64::ADDL_GA, MVT::i64,
437 CurDAG->getRegister(IA64::r1, MVT::i64), GA), 0);
438 Result = SDOperand(CurDAG->getTargetNode(IA64::LD8, MVT::i64, Tmp), 0);
Evan Cheng64a752f2006-08-11 09:08:15 +0000439 return Result.Val;
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000440 }
Duraid Madinaa36153a2005-12-22 03:58:17 +0000441
442/* XXX case ISD::ExternalSymbol: {
443 SDOperand EA = CurDAG->getTargetExternalSymbol(cast<ExternalSymbolSDNode>(N)->getSymbol(),
444 MVT::i64);
445 SDOperand Tmp = CurDAG->getTargetNode(IA64::ADDL_EA, MVT::i64,
446 CurDAG->getRegister(IA64::r1, MVT::i64), EA);
447 return CurDAG->getTargetNode(IA64::LD8, MVT::i64, Tmp);
448 }
449*/
450
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000451 case ISD::LOAD:
Duraid Madinaecc1a1b2006-01-20 16:10:05 +0000452 case ISD::EXTLOAD: // FIXME: load -1, not 1, for bools?
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000453 case ISD::ZEXTLOAD: {
Evan Cheng6da2f322006-08-26 01:07:58 +0000454 SDOperand Chain = N->getOperand(0);
455 SDOperand Address = N->getOperand(1);
456 AddToISelQueue(Chain);
457 AddToISelQueue(Address);
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000458
459 MVT::ValueType TypeBeingLoaded = (N->getOpcode() == ISD::LOAD) ?
460 N->getValueType(0) : cast<VTSDNode>(N->getOperand(3))->getVT();
461 unsigned Opc;
462 switch (TypeBeingLoaded) {
Jim Laskey16d42c62006-07-11 18:25:13 +0000463 default:
464#ifndef NDEBUG
465 N->dump();
466#endif
467 assert(0 && "Cannot load this type!");
Duraid Madina9f729062005-11-04 09:59:06 +0000468 case MVT::i1: { // this is a bool
469 Opc = IA64::LD1; // first we load a byte, then compare for != 0
Evan Cheng34167212006-02-09 00:37:58 +0000470 if(N->getValueType(0) == MVT::i1) { // XXX: early exit!
Evan Cheng23329f52006-08-16 07:30:09 +0000471 return CurDAG->SelectNodeTo(N, IA64::CMPNE, MVT::i1, MVT::Other,
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000472 SDOperand(CurDAG->getTargetNode(Opc, MVT::i64, Address), 0),
Evan Cheng23329f52006-08-16 07:30:09 +0000473 CurDAG->getRegister(IA64::r0, MVT::i64),
474 Chain).getValue(Op.ResNo).Val;
Evan Cheng34167212006-02-09 00:37:58 +0000475 }
Duraid Madinaa36153a2005-12-22 03:58:17 +0000476 /* otherwise, we want to load a bool into something bigger: LD1
477 will do that for us, so we just fall through */
Chris Lattnerb19b8992005-11-30 23:02:08 +0000478 }
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000479 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
Chris Lattnerb19b8992005-11-30 23:02:08 +0000488 // TODO: comment this
Evan Cheng23329f52006-08-16 07:30:09 +0000489 return CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), MVT::Other,
490 Address, Chain).getValue(Op.ResNo).Val;
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000491 }
492
493 case ISD::TRUNCSTORE:
494 case ISD::STORE: {
Evan Cheng6da2f322006-08-26 01:07:58 +0000495 SDOperand Address = N->getOperand(2);
496 SDOperand Chain = N->getOperand(0);
497 AddToISelQueue(Address);
498 AddToISelQueue(Chain);
Duraid Madinad525df32005-11-07 03:11:02 +0000499
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000500 unsigned Opc;
501 if (N->getOpcode() == ISD::STORE) {
502 switch (N->getOperand(1).getValueType()) {
Duraid Madinad525df32005-11-07 03:11:02 +0000503 default: assert(0 && "unknown type in store");
504 case MVT::i1: { // this is a bool
505 Opc = IA64::ST1; // we store either 0 or 1 as a byte
Duraid Madina544cbbd2006-01-13 10:28:25 +0000506 // first load zero!
507 SDOperand Initial = CurDAG->getCopyFromReg(Chain, IA64::r0, MVT::i64);
508 Chain = Initial.getValue(1);
Duraid Madinaa7fb5be2006-01-20 03:40:25 +0000509 // then load 1 into the same reg iff the predicate to store is 1
Evan Cheng6da2f322006-08-26 01:07:58 +0000510 SDOperand Tmp = N->getOperand(1);
511 AddToISelQueue(Tmp);
Duraid Madinab20f9792006-02-11 07:33:17 +0000512 Tmp = SDOperand(CurDAG->getTargetNode(IA64::TPCADDS, MVT::i64, Initial,
513 CurDAG->getConstant(1, MVT::i64),
514 Tmp), 0);
Evan Cheng23329f52006-08-16 07:30:09 +0000515 return CurDAG->SelectNodeTo(N, Opc, MVT::Other, Address, Tmp, Chain).Val;
Chris Lattnerb19b8992005-11-30 23:02:08 +0000516 }
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000517 case MVT::i64: Opc = IA64::ST8; break;
518 case MVT::f64: Opc = IA64::STF8; break;
Duraid Madinad525df32005-11-07 03:11:02 +0000519 }
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000520 } else { //ISD::TRUNCSTORE
521 switch(cast<VTSDNode>(N->getOperand(4))->getVT()) {
Duraid Madinad525df32005-11-07 03:11:02 +0000522 default: assert(0 && "unknown type in truncstore");
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000523 case MVT::i8: Opc = IA64::ST1; break;
524 case MVT::i16: Opc = IA64::ST2; break;
525 case MVT::i32: Opc = IA64::ST4; break;
526 case MVT::f32: Opc = IA64::STF4; break;
527 }
528 }
529
Evan Cheng6da2f322006-08-26 01:07:58 +0000530 SDOperand N1 = N->getOperand(1);
531 SDOperand N2 = N->getOperand(2);
532 AddToISelQueue(N1);
533 AddToISelQueue(N2);
Evan Cheng23329f52006-08-16 07:30:09 +0000534 return CurDAG->SelectNodeTo(N, Opc, MVT::Other, N2, N1, Chain).Val;
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000535 }
536
537 case ISD::BRCOND: {
Evan Cheng6da2f322006-08-26 01:07:58 +0000538 SDOperand Chain = N->getOperand(0);
539 SDOperand CC = N->getOperand(1);
540 AddToISelQueue(Chain);
541 AddToISelQueue(CC);
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000542 MachineBasicBlock *Dest =
543 cast<BasicBlockSDNode>(N->getOperand(2))->getBasicBlock();
544 //FIXME - we do NOT need long branches all the time
Evan Cheng23329f52006-08-16 07:30:09 +0000545 return CurDAG->SelectNodeTo(N, IA64::BRLCOND_NOTCALL, MVT::Other, CC,
546 CurDAG->getBasicBlock(Dest), Chain).Val;
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000547 }
548
549 case ISD::CALLSEQ_START:
550 case ISD::CALLSEQ_END: {
551 int64_t Amt = cast<ConstantSDNode>(N->getOperand(1))->getValue();
552 unsigned Opc = N->getOpcode() == ISD::CALLSEQ_START ?
553 IA64::ADJUSTCALLSTACKDOWN : IA64::ADJUSTCALLSTACKUP;
Evan Cheng6da2f322006-08-26 01:07:58 +0000554 SDOperand N0 = N->getOperand(0);
555 AddToISelQueue(N0);
Evan Cheng23329f52006-08-16 07:30:09 +0000556 return CurDAG->SelectNodeTo(N, Opc, MVT::Other, getI64Imm(Amt), N0).Val;
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000557 }
558
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000559 case ISD::BR:
560 // FIXME: we don't need long branches all the time!
Evan Cheng6da2f322006-08-26 01:07:58 +0000561 SDOperand N0 = N->getOperand(0);
562 AddToISelQueue(N0);
Evan Cheng23329f52006-08-16 07:30:09 +0000563 return CurDAG->SelectNodeTo(N, IA64::BRL_NOTCALL, MVT::Other,
564 N->getOperand(1), N0).Val;
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000565 }
566
Evan Cheng64a752f2006-08-11 09:08:15 +0000567 return SelectCode(Result, Op);
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000568}
569
570
571/// createIA64DAGToDAGInstructionSelector - This pass converts a legalized DAG
572/// into an IA64-specific DAG, ready for instruction scheduling.
573///
Evan Chengc4c62572006-03-13 23:20:37 +0000574FunctionPass
575*llvm::createIA64DAGToDAGInstructionSelector(IA64TargetMachine &TM) {
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000576 return new IA64DAGToDAGISel(TM);
577}
578