blob: 1a9eae0e16abf8562ef15d6a4607633e6044234a [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//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Duraid Madinaf2db9b82005-10-28 17:46:35 +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
Chris Lattner95b2c7d2006-12-19 22:59:26 +000015#define DEBUG_TYPE "ia64-codegen"
Duraid Madinaf2db9b82005-10-28 17:46:35 +000016#include "IA64.h"
17#include "IA64TargetMachine.h"
18#include "IA64ISelLowering.h"
19#include "llvm/CodeGen/MachineInstrBuilder.h"
20#include "llvm/CodeGen/MachineFunction.h"
Evan Chenga844bde2008-02-02 04:07:54 +000021#include "llvm/CodeGen/MachineModuleInfo.h"
Duraid Madinaf2db9b82005-10-28 17:46:35 +000022#include "llvm/CodeGen/SelectionDAG.h"
23#include "llvm/CodeGen/SelectionDAGISel.h"
24#include "llvm/Target/TargetOptions.h"
Duraid Madinaf2db9b82005-10-28 17:46:35 +000025#include "llvm/Constants.h"
26#include "llvm/GlobalValue.h"
Chris Lattner420736d2006-03-25 06:47:10 +000027#include "llvm/Intrinsics.h"
Chris Lattner3d62d782008-02-03 05:43:57 +000028#include "llvm/Support/Compiler.h"
Duraid Madinaf2db9b82005-10-28 17:46:35 +000029#include "llvm/Support/Debug.h"
30#include "llvm/Support/MathExtras.h"
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 {
Duraid Madinaf2db9b82005-10-28 17:46:35 +000036 //===--------------------------------------------------------------------===//
37 /// IA64DAGToDAGISel - IA64 specific code to select IA64 machine
38 /// instructions for SelectionDAG operations.
39 ///
40 class IA64DAGToDAGISel : public SelectionDAGISel {
41 IA64TargetLowering IA64Lowering;
42 unsigned GlobalBaseReg;
43 public:
Evan Chengc4c62572006-03-13 23:20:37 +000044 IA64DAGToDAGISel(IA64TargetMachine &TM)
45 : SelectionDAGISel(IA64Lowering), IA64Lowering(*TM.getTargetLowering()) {}
Duraid Madinaf2db9b82005-10-28 17:46:35 +000046
47 virtual bool runOnFunction(Function &Fn) {
48 // Make sure we re-emit a set of the global base reg if necessary
49 GlobalBaseReg = 0;
50 return SelectionDAGISel::runOnFunction(Fn);
51 }
52
53 /// getI64Imm - Return a target constant with the specified value, of type
54 /// i64.
55 inline SDOperand getI64Imm(uint64_t Imm) {
56 return CurDAG->getTargetConstant(Imm, MVT::i64);
57 }
58
59 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
60 /// base register. Return the virtual register that holds this value.
61 // SDOperand getGlobalBaseReg(); TODO: hmm
62
63 // Select - Convert the specified operand from a target-independent to a
64 // target-specific node if it hasn't already been changed.
Evan Cheng9ade2182006-08-26 05:34:46 +000065 SDNode *Select(SDOperand N);
Duraid Madinaf2db9b82005-10-28 17:46:35 +000066
67 SDNode *SelectIntImmediateExpr(SDOperand LHS, SDOperand RHS,
68 unsigned OCHi, unsigned OCLo,
69 bool IsArithmetic = false,
70 bool Negate = false);
71 SDNode *SelectBitfieldInsert(SDNode *N);
72
73 /// SelectCC - Select a comparison of the specified values with the
74 /// specified condition code, returning the CR# of the expression.
75 SDOperand SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC);
76
77 /// SelectAddr - Given the specified address, return the two operands for a
78 /// load/store instruction, and return true if it should be an indexed [r+r]
79 /// operation.
80 bool SelectAddr(SDOperand Addr, SDOperand &Op1, SDOperand &Op2);
81
Duraid Madinaf2db9b82005-10-28 17:46:35 +000082 /// InstructionSelectBasicBlock - This callback is invoked by
83 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
84 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
85
86 virtual const char *getPassName() const {
87 return "IA64 (Itanium) DAG->DAG Instruction Selector";
88 }
89
90// Include the pieces autogenerated from the target description.
91#include "IA64GenDAGISel.inc"
92
93private:
Evan Cheng9ade2182006-08-26 05:34:46 +000094 SDNode *SelectDIV(SDOperand Op);
Duraid Madinaf2db9b82005-10-28 17:46:35 +000095 };
96}
97
98/// InstructionSelectBasicBlock - This callback is invoked by
99/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
100void IA64DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
101 DEBUG(BB->dump());
Evan Cheng33e9ad92006-07-27 06:40:15 +0000102
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000103 // Select target instructions for the DAG.
Evan Chengba2f0a92006-02-05 06:46:41 +0000104 DAG.setRoot(SelectRoot(DAG.getRoot()));
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000105 DAG.RemoveDeadNodes();
106
107 // Emit machine code to BB.
108 ScheduleAndEmitDAG(DAG);
109}
110
Evan Cheng9ade2182006-08-26 05:34:46 +0000111SDNode *IA64DAGToDAGISel::SelectDIV(SDOperand Op) {
Duraid Madinab6f023a2005-11-21 14:14:54 +0000112 SDNode *N = Op.Val;
Evan Cheng6da2f322006-08-26 01:07:58 +0000113 SDOperand Chain = N->getOperand(0);
114 SDOperand Tmp1 = N->getOperand(0);
115 SDOperand Tmp2 = N->getOperand(1);
116 AddToISelQueue(Chain);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000117
Evan Cheng6da2f322006-08-26 01:07:58 +0000118 AddToISelQueue(Tmp1);
119 AddToISelQueue(Tmp2);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000120
121 bool isFP=false;
122
123 if(MVT::isFloatingPoint(Tmp1.getValueType()))
124 isFP=true;
125
126 bool isModulus=false; // is it a division or a modulus?
127 bool isSigned=false;
128
129 switch(N->getOpcode()) {
130 case ISD::FDIV:
131 case ISD::SDIV: isModulus=false; isSigned=true; break;
132 case ISD::UDIV: isModulus=false; isSigned=false; break;
133 case ISD::FREM:
134 case ISD::SREM: isModulus=true; isSigned=true; break;
135 case ISD::UREM: isModulus=true; isSigned=false; break;
136 }
137
138 // TODO: check for integer divides by powers of 2 (or other simple patterns?)
139
140 SDOperand TmpPR, TmpPR2;
141 SDOperand TmpF1, TmpF2, TmpF3, TmpF4, TmpF5, TmpF6, TmpF7, TmpF8;
142 SDOperand TmpF9, TmpF10,TmpF11,TmpF12,TmpF13,TmpF14,TmpF15;
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000143 SDNode *Result;
Duraid Madina76bb6ae2006-01-16 14:33:04 +0000144
145 // we'll need copies of F0 and F1
146 SDOperand F0 = CurDAG->getRegister(IA64::F0, MVT::f64);
147 SDOperand F1 = CurDAG->getRegister(IA64::F1, MVT::f64);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000148
149 // OK, emit some code:
150
151 if(!isFP) {
152 // first, load the inputs into FP regs.
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000153 TmpF1 =
154 SDOperand(CurDAG->getTargetNode(IA64::SETFSIG, MVT::f64, Tmp1), 0);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000155 Chain = TmpF1.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000156 TmpF2 =
157 SDOperand(CurDAG->getTargetNode(IA64::SETFSIG, MVT::f64, Tmp2), 0);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000158 Chain = TmpF2.getValue(1);
159
160 // next, convert the inputs to FP
161 if(isSigned) {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000162 TmpF3 =
163 SDOperand(CurDAG->getTargetNode(IA64::FCVTXF, MVT::f64, TmpF1), 0);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000164 Chain = TmpF3.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000165 TmpF4 =
166 SDOperand(CurDAG->getTargetNode(IA64::FCVTXF, MVT::f64, TmpF2), 0);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000167 Chain = TmpF4.getValue(1);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000168 } else { // is unsigned
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000169 TmpF3 =
170 SDOperand(CurDAG->getTargetNode(IA64::FCVTXUFS1, MVT::f64, TmpF1), 0);
Duraid Madina76bb6ae2006-01-16 14:33:04 +0000171 Chain = TmpF3.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000172 TmpF4 =
173 SDOperand(CurDAG->getTargetNode(IA64::FCVTXUFS1, MVT::f64, TmpF2), 0);
Duraid Madina76bb6ae2006-01-16 14:33:04 +0000174 Chain = TmpF4.getValue(1);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000175 }
176
177 } else { // this is an FP divide/remainder, so we 'leak' some temp
178 // regs and assign TmpF3=Tmp1, TmpF4=Tmp2
179 TmpF3=Tmp1;
180 TmpF4=Tmp2;
181 }
182
183 // we start by computing an approximate reciprocal (good to 9 bits?)
184 // note, this instruction writes _both_ TmpF5 (answer) and TmpPR (predicate)
Duraid Madina0c81dc82006-01-16 06:33:38 +0000185 if(isFP)
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000186 TmpF5 = SDOperand(CurDAG->getTargetNode(IA64::FRCPAS0, MVT::f64, MVT::i1,
187 TmpF3, TmpF4), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000188 else
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000189 TmpF5 = SDOperand(CurDAG->getTargetNode(IA64::FRCPAS1, MVT::f64, MVT::i1,
190 TmpF3, TmpF4), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000191
Duraid Madinab6f023a2005-11-21 14:14:54 +0000192 TmpPR = TmpF5.getValue(1);
193 Chain = TmpF5.getValue(2);
194
Duraid Madina0c81dc82006-01-16 06:33:38 +0000195 SDOperand minusB;
196 if(isModulus) { // for remainders, it'll be handy to have
197 // copies of -input_b
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000198 minusB = SDOperand(CurDAG->getTargetNode(IA64::SUB, MVT::i64,
199 CurDAG->getRegister(IA64::r0, MVT::i64), Tmp2), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000200 Chain = minusB.getValue(1);
201 }
202
203 SDOperand TmpE0, TmpY1, TmpE1, TmpY2;
Evan Cheng0b828e02006-08-27 08:14:06 +0000204
205 SDOperand OpsE0[] = { TmpF4, TmpF5, F1, TmpPR };
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000206 TmpE0 = SDOperand(CurDAG->getTargetNode(IA64::CFNMAS1, MVT::f64,
Evan Cheng0b828e02006-08-27 08:14:06 +0000207 OpsE0, 4), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000208 Chain = TmpE0.getValue(1);
Evan Cheng0b828e02006-08-27 08:14:06 +0000209 SDOperand OpsY1[] = { TmpF5, TmpE0, TmpF5, TmpPR };
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000210 TmpY1 = SDOperand(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
Evan Cheng0b828e02006-08-27 08:14:06 +0000211 OpsY1, 4), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000212 Chain = TmpY1.getValue(1);
Evan Cheng0b828e02006-08-27 08:14:06 +0000213 SDOperand OpsE1[] = { TmpE0, TmpE0, F0, TmpPR };
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000214 TmpE1 = SDOperand(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
Evan Cheng0b828e02006-08-27 08:14:06 +0000215 OpsE1, 4), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000216 Chain = TmpE1.getValue(1);
Evan Cheng0b828e02006-08-27 08:14:06 +0000217 SDOperand OpsY2[] = { TmpY1, TmpE1, TmpY1, TmpPR };
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000218 TmpY2 = SDOperand(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
Evan Cheng0b828e02006-08-27 08:14:06 +0000219 OpsY2, 4), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000220 Chain = TmpY2.getValue(1);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000221
Duraid Madina0c81dc82006-01-16 06:33:38 +0000222 if(isFP) { // if this is an FP divide, we finish up here and exit early
223 if(isModulus)
224 assert(0 && "Sorry, try another FORTRAN compiler.");
225
226 SDOperand TmpE2, TmpY3, TmpQ0, TmpR0;
Evan Cheng0b828e02006-08-27 08:14:06 +0000227
228 SDOperand OpsE2[] = { TmpE1, TmpE1, F0, TmpPR };
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000229 TmpE2 = SDOperand(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
Evan Cheng0b828e02006-08-27 08:14:06 +0000230 OpsE2, 4), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000231 Chain = TmpE2.getValue(1);
Evan Cheng0b828e02006-08-27 08:14:06 +0000232 SDOperand OpsY3[] = { TmpY2, TmpE2, TmpY2, TmpPR };
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000233 TmpY3 = SDOperand(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
Evan Cheng0b828e02006-08-27 08:14:06 +0000234 OpsY3, 4), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000235 Chain = TmpY3.getValue(1);
Evan Cheng0b828e02006-08-27 08:14:06 +0000236 SDOperand OpsQ0[] = { Tmp1, TmpY3, F0, TmpPR };
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000237 TmpQ0 =
238 SDOperand(CurDAG->getTargetNode(IA64::CFMADS1, MVT::f64, // double prec!
Evan Cheng0b828e02006-08-27 08:14:06 +0000239 OpsQ0, 4), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000240 Chain = TmpQ0.getValue(1);
Evan Cheng0b828e02006-08-27 08:14:06 +0000241 SDOperand OpsR0[] = { Tmp2, TmpQ0, Tmp1, TmpPR };
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000242 TmpR0 =
243 SDOperand(CurDAG->getTargetNode(IA64::CFNMADS1, MVT::f64, // double prec!
Evan Cheng0b828e02006-08-27 08:14:06 +0000244 OpsR0, 4), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000245 Chain = TmpR0.getValue(1);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000246
Duraid Madina0c81dc82006-01-16 06:33:38 +0000247// we want Result to have the same target register as the frcpa, so
248// we two-address hack it. See the comment "for this to work..." on
249// page 48 of Intel application note #245415
Evan Cheng0b828e02006-08-27 08:14:06 +0000250 SDOperand Ops[] = { TmpF5, TmpY3, TmpR0, TmpQ0, TmpPR };
Duraid Madina0c81dc82006-01-16 06:33:38 +0000251 Result = CurDAG->getTargetNode(IA64::TCFMADS0, MVT::f64, // d.p. s0 rndg!
Evan Cheng0b828e02006-08-27 08:14:06 +0000252 Ops, 5);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000253 Chain = SDOperand(Result, 1);
Evan Cheng9ade2182006-08-26 05:34:46 +0000254 return Result; // XXX: early exit!
Duraid Madina0c81dc82006-01-16 06:33:38 +0000255 } else { // this is *not* an FP divide, so there's a bit left to do:
256
257 SDOperand TmpQ2, TmpR2, TmpQ3, TmpQ;
Evan Cheng0b828e02006-08-27 08:14:06 +0000258
259 SDOperand OpsQ2[] = { TmpF3, TmpY2, F0, TmpPR };
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000260 TmpQ2 = SDOperand(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
Evan Cheng0b828e02006-08-27 08:14:06 +0000261 OpsQ2, 4), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000262 Chain = TmpQ2.getValue(1);
Evan Cheng0b828e02006-08-27 08:14:06 +0000263 SDOperand OpsR2[] = { TmpF4, TmpQ2, TmpF3, TmpPR };
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000264 TmpR2 = SDOperand(CurDAG->getTargetNode(IA64::CFNMAS1, MVT::f64,
Evan Cheng0b828e02006-08-27 08:14:06 +0000265 OpsR2, 4), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000266 Chain = TmpR2.getValue(1);
Duraid Madinaae6dcdd2006-01-17 01:19:49 +0000267
Duraid Madina76bb6ae2006-01-16 14:33:04 +0000268// we want TmpQ3 to have the same target register as the frcpa? maybe we
269// should two-address hack it. See the comment "for this to work..." on page
270// 48 of Intel application note #245415
Evan Cheng0b828e02006-08-27 08:14:06 +0000271 SDOperand OpsQ3[] = { TmpF5, TmpR2, TmpY2, TmpQ2, TmpPR };
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000272 TmpQ3 = SDOperand(CurDAG->getTargetNode(IA64::TCFMAS1, MVT::f64,
Evan Cheng0b828e02006-08-27 08:14:06 +0000273 OpsQ3, 5), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000274 Chain = TmpQ3.getValue(1);
Duraid Madina76bb6ae2006-01-16 14:33:04 +0000275
Duraid Madinaae6dcdd2006-01-17 01:19:49 +0000276 // STORY: without these two-address instructions (TCFMAS1 and TCFMADS0)
277 // the FPSWA won't be able to help out in the case of large/tiny
278 // arguments. Other fun bugs may also appear, e.g. 0/x = x, not 0.
279
Duraid Madina0c81dc82006-01-16 06:33:38 +0000280 if(isSigned)
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000281 TmpQ = SDOperand(CurDAG->getTargetNode(IA64::FCVTFXTRUNCS1,
282 MVT::f64, TmpQ3), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000283 else
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000284 TmpQ = SDOperand(CurDAG->getTargetNode(IA64::FCVTFXUTRUNCS1,
285 MVT::f64, TmpQ3), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000286
287 Chain = TmpQ.getValue(1);
288
289 if(isModulus) {
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000290 SDOperand FPminusB =
291 SDOperand(CurDAG->getTargetNode(IA64::SETFSIG, MVT::f64, minusB), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000292 Chain = FPminusB.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000293 SDOperand Remainder =
294 SDOperand(CurDAG->getTargetNode(IA64::XMAL, MVT::f64,
295 TmpQ, FPminusB, TmpF1), 0);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000296 Chain = Remainder.getValue(1);
297 Result = CurDAG->getTargetNode(IA64::GETFSIG, MVT::i64, Remainder);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000298 Chain = SDOperand(Result, 1);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000299 } else { // just an integer divide
300 Result = CurDAG->getTargetNode(IA64::GETFSIG, MVT::i64, TmpQ);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000301 Chain = SDOperand(Result, 1);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000302 }
303
Evan Cheng9ade2182006-08-26 05:34:46 +0000304 return Result;
Duraid Madina0c81dc82006-01-16 06:33:38 +0000305 } // wasn't an FP divide
Duraid Madinab6f023a2005-11-21 14:14:54 +0000306}
307
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000308// Select - Convert the specified operand from a target-independent to a
309// target-specific node if it hasn't already been changed.
Evan Cheng9ade2182006-08-26 05:34:46 +0000310SDNode *IA64DAGToDAGISel::Select(SDOperand Op) {
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000311 SDNode *N = Op.Val;
312 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
Evan Cheng9ade2182006-08-26 05:34:46 +0000313 N->getOpcode() < IA64ISD::FIRST_NUMBER)
Evan Cheng64a752f2006-08-11 09:08:15 +0000314 return NULL; // Already selected.
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000315
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000316 switch (N->getOpcode()) {
317 default: break;
318
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000319 case IA64ISD::BRCALL: { // XXX: this is also a hack!
Evan Cheng6da2f322006-08-26 01:07:58 +0000320 SDOperand Chain = N->getOperand(0);
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000321 SDOperand InFlag; // Null incoming flag value.
322
Evan Cheng6da2f322006-08-26 01:07:58 +0000323 AddToISelQueue(Chain);
324 if(N->getNumOperands()==3) { // we have an incoming chain, callee and flag
325 InFlag = N->getOperand(2);
326 AddToISelQueue(InFlag);
327 }
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000328
329 unsigned CallOpcode;
330 SDOperand CallOperand;
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000331
332 // if we can call directly, do so
333 if (GlobalAddressSDNode *GASD =
334 dyn_cast<GlobalAddressSDNode>(N->getOperand(1))) {
335 CallOpcode = IA64::BRCALL_IPREL_GA;
336 CallOperand = CurDAG->getTargetGlobalAddress(GASD->getGlobal(), MVT::i64);
Reid Spencer3ed469c2006-11-02 20:25:50 +0000337 } else if (isa<ExternalSymbolSDNode>(N->getOperand(1))) {
338 // FIXME: we currently NEED this case for correctness, to avoid
339 // "non-pic code with imm reloc.n against dynamic symbol" errors
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000340 CallOpcode = IA64::BRCALL_IPREL_ES;
341 CallOperand = N->getOperand(1);
342 } else {
343 // otherwise we need to load the function descriptor,
344 // load the branch target (function)'s entry point and GP,
345 // branch (call) then restore the GP
Evan Cheng6da2f322006-08-26 01:07:58 +0000346 SDOperand FnDescriptor = N->getOperand(1);
347 AddToISelQueue(FnDescriptor);
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000348
349 // load the branch target's entry point [mem] and
350 // GP value [mem+8]
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000351 SDOperand targetEntryPoint=
352 SDOperand(CurDAG->getTargetNode(IA64::LD8, MVT::i64, FnDescriptor), 0);
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000353 Chain = targetEntryPoint.getValue(1);
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000354 SDOperand targetGPAddr=
355 SDOperand(CurDAG->getTargetNode(IA64::ADDS, MVT::i64,
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000356 FnDescriptor,
357 CurDAG->getConstant(8, MVT::i64)), 0);
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000358 Chain = targetGPAddr.getValue(1);
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000359 SDOperand targetGP =
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000360 SDOperand(CurDAG->getTargetNode(IA64::LD8, MVT::i64, targetGPAddr), 0);
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000361 Chain = targetGP.getValue(1);
362
363 Chain = CurDAG->getCopyToReg(Chain, IA64::r1, targetGP, InFlag);
364 InFlag = Chain.getValue(1);
365 Chain = CurDAG->getCopyToReg(Chain, IA64::B6, targetEntryPoint, InFlag); // FLAG these?
366 InFlag = Chain.getValue(1);
367
368 CallOperand = CurDAG->getRegister(IA64::B6, MVT::i64);
369 CallOpcode = IA64::BRCALL_INDIRECT;
370 }
371
372 // Finally, once everything is setup, emit the call itself
373 if(InFlag.Val)
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000374 Chain = SDOperand(CurDAG->getTargetNode(CallOpcode, MVT::Other, MVT::Flag,
375 CallOperand, InFlag), 0);
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000376 else // there might be no arguments
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000377 Chain = SDOperand(CurDAG->getTargetNode(CallOpcode, MVT::Other, MVT::Flag,
378 CallOperand, Chain), 0);
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000379 InFlag = Chain.getValue(1);
380
381 std::vector<SDOperand> CallResults;
382
383 CallResults.push_back(Chain);
384 CallResults.push_back(InFlag);
385
386 for (unsigned i = 0, e = CallResults.size(); i != e; ++i)
Evan Cheng2ef88a02006-08-07 22:28:20 +0000387 ReplaceUses(Op.getValue(i), CallResults[i]);
Evan Cheng64a752f2006-08-11 09:08:15 +0000388 return NULL;
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000389 }
Duraid Madinaa36153a2005-12-22 03:58:17 +0000390
Duraid Madina8617f3c2005-12-22 07:14:45 +0000391 case IA64ISD::GETFD: {
Evan Cheng6da2f322006-08-26 01:07:58 +0000392 SDOperand Input = N->getOperand(0);
393 AddToISelQueue(Input);
Evan Cheng9ade2182006-08-26 05:34:46 +0000394 return CurDAG->getTargetNode(IA64::GETFD, MVT::i64, Input);
Duraid Madina8617f3c2005-12-22 07:14:45 +0000395 }
396
Duraid Madinab6f023a2005-11-21 14:14:54 +0000397 case ISD::FDIV:
398 case ISD::SDIV:
399 case ISD::UDIV:
400 case ISD::SREM:
Evan Cheng34167212006-02-09 00:37:58 +0000401 case ISD::UREM:
Evan Cheng9ade2182006-08-26 05:34:46 +0000402 return SelectDIV(Op);
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000403
Chris Lattnera54aa942006-01-29 06:26:08 +0000404 case ISD::TargetConstantFP: {
Duraid Madina056728f2005-11-02 07:32:59 +0000405 SDOperand Chain = CurDAG->getEntryNode(); // this is a constant, so..
406
Chris Lattnerbacf9f42006-10-24 17:09:43 +0000407 SDOperand V;
Dale Johannesenee847682007-08-31 17:03:33 +0000408 ConstantFPSDNode* N2 = cast<ConstantFPSDNode>(N);
409 if (N2->getValueAPF().isPosZero()) {
Chris Lattnerbacf9f42006-10-24 17:09:43 +0000410 V = CurDAG->getCopyFromReg(Chain, IA64::F0, MVT::f64);
Dale Johannesenee847682007-08-31 17:03:33 +0000411 } else if (N2->isExactlyValue(N2->getValueType(0) == MVT::f32 ?
412 APFloat(+1.0f) : APFloat(+1.0))) {
Chris Lattnerbacf9f42006-10-24 17:09:43 +0000413 V = CurDAG->getCopyFromReg(Chain, IA64::F1, MVT::f64);
Evan Cheng34167212006-02-09 00:37:58 +0000414 } else
Duraid Madina93856802005-11-02 02:35:04 +0000415 assert(0 && "Unexpected FP constant!");
Chris Lattnerbacf9f42006-10-24 17:09:43 +0000416
417 ReplaceUses(SDOperand(N, 0), V);
418 return 0;
Duraid Madina93856802005-11-02 02:35:04 +0000419 }
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000420
421 case ISD::FrameIndex: { // TODO: reduce creepyness
422 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Evan Cheng23329f52006-08-16 07:30:09 +0000423 if (N->hasOneUse())
424 return CurDAG->SelectNodeTo(N, IA64::MOV, MVT::i64,
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000425 CurDAG->getTargetFrameIndex(FI, MVT::i64));
Evan Cheng23329f52006-08-16 07:30:09 +0000426 else
Evan Cheng95514ba2006-08-26 08:00:10 +0000427 return CurDAG->getTargetNode(IA64::MOV, MVT::i64,
428 CurDAG->getTargetFrameIndex(FI, MVT::i64));
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000429 }
430
Duraid Madina2e0348e2006-01-15 09:45:23 +0000431 case ISD::ConstantPool: { // TODO: nuke the constant pool
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000432 // (ia64 doesn't need one)
Evan Chengb8973bd2006-01-31 22:23:14 +0000433 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
Evan Chengc356a572006-09-12 21:04:05 +0000434 Constant *C = CP->getConstVal();
Evan Chengb8973bd2006-01-31 22:23:14 +0000435 SDOperand CPI = CurDAG->getTargetConstantPool(C, MVT::i64,
436 CP->getAlignment());
Evan Cheng9ade2182006-08-26 05:34:46 +0000437 return CurDAG->getTargetNode(IA64::ADDL_GA, MVT::i64, // ?
438 CurDAG->getRegister(IA64::r1, MVT::i64), CPI);
Duraid Madina25d0a882005-10-29 16:08:30 +0000439 }
440
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000441 case ISD::GlobalAddress: {
442 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
443 SDOperand GA = CurDAG->getTargetGlobalAddress(GV, MVT::i64);
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000444 SDOperand Tmp =
445 SDOperand(CurDAG->getTargetNode(IA64::ADDL_GA, MVT::i64,
446 CurDAG->getRegister(IA64::r1,
447 MVT::i64), GA), 0);
Evan Cheng9ade2182006-08-26 05:34:46 +0000448 return CurDAG->getTargetNode(IA64::LD8, MVT::i64, Tmp);
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000449 }
Duraid Madinaa36153a2005-12-22 03:58:17 +0000450
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000451/* XXX
452 case ISD::ExternalSymbol: {
453 SDOperand EA = CurDAG->getTargetExternalSymbol(
454 cast<ExternalSymbolSDNode>(N)->getSymbol(),
455 MVT::i64);
456 SDOperand Tmp = CurDAG->getTargetNode(IA64::ADDL_EA, MVT::i64,
457 CurDAG->getRegister(IA64::r1,
458 MVT::i64),
459 EA);
460 return CurDAG->getTargetNode(IA64::LD8, MVT::i64, Tmp);
461 }
Duraid Madinaa36153a2005-12-22 03:58:17 +0000462*/
463
Evan Cheng466685d2006-10-09 20:57:25 +0000464 case ISD::LOAD: { // FIXME: load -1, not 1, for bools?
465 LoadSDNode *LD = cast<LoadSDNode>(N);
466 SDOperand Chain = LD->getChain();
467 SDOperand Address = LD->getBasePtr();
Evan Cheng6da2f322006-08-26 01:07:58 +0000468 AddToISelQueue(Chain);
469 AddToISelQueue(Address);
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000470
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000471 MVT::ValueType TypeBeingLoaded = LD->getMemoryVT();
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000472 unsigned Opc;
473 switch (TypeBeingLoaded) {
Jim Laskey16d42c62006-07-11 18:25:13 +0000474 default:
475#ifndef NDEBUG
Dan Gohmanb5bec2b2007-06-19 14:13:56 +0000476 N->dump(CurDAG);
Jim Laskey16d42c62006-07-11 18:25:13 +0000477#endif
478 assert(0 && "Cannot load this type!");
Duraid Madina9f729062005-11-04 09:59:06 +0000479 case MVT::i1: { // this is a bool
480 Opc = IA64::LD1; // first we load a byte, then compare for != 0
Evan Cheng34167212006-02-09 00:37:58 +0000481 if(N->getValueType(0) == MVT::i1) { // XXX: early exit!
Evan Cheng23329f52006-08-16 07:30:09 +0000482 return CurDAG->SelectNodeTo(N, IA64::CMPNE, MVT::i1, MVT::Other,
Evan Cheng7e9b26f2006-02-09 07:17:49 +0000483 SDOperand(CurDAG->getTargetNode(Opc, MVT::i64, Address), 0),
Evan Cheng23329f52006-08-16 07:30:09 +0000484 CurDAG->getRegister(IA64::r0, MVT::i64),
Evan Cheng95514ba2006-08-26 08:00:10 +0000485 Chain);
Evan Cheng34167212006-02-09 00:37:58 +0000486 }
Duraid Madinaa36153a2005-12-22 03:58:17 +0000487 /* otherwise, we want to load a bool into something bigger: LD1
488 will do that for us, so we just fall through */
Chris Lattnerb19b8992005-11-30 23:02:08 +0000489 }
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000490 case MVT::i8: Opc = IA64::LD1; break;
491 case MVT::i16: Opc = IA64::LD2; break;
492 case MVT::i32: Opc = IA64::LD4; break;
493 case MVT::i64: Opc = IA64::LD8; break;
494
495 case MVT::f32: Opc = IA64::LDF4; break;
496 case MVT::f64: Opc = IA64::LDF8; break;
497 }
498
Chris Lattnerb19b8992005-11-30 23:02:08 +0000499 // TODO: comment this
Evan Cheng23329f52006-08-16 07:30:09 +0000500 return CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), MVT::Other,
Evan Cheng95514ba2006-08-26 08:00:10 +0000501 Address, Chain);
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000502 }
503
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000504 case ISD::STORE: {
Evan Cheng8b2794a2006-10-13 21:14:26 +0000505 StoreSDNode *ST = cast<StoreSDNode>(N);
506 SDOperand Address = ST->getBasePtr();
507 SDOperand Chain = ST->getChain();
Evan Cheng6da2f322006-08-26 01:07:58 +0000508 AddToISelQueue(Address);
509 AddToISelQueue(Chain);
Duraid Madinad525df32005-11-07 03:11:02 +0000510
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000511 unsigned Opc;
Evan Cheng8b2794a2006-10-13 21:14:26 +0000512 if (ISD::isNON_TRUNCStore(N)) {
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000513 switch (N->getOperand(1).getValueType()) {
Duraid Madinad525df32005-11-07 03:11:02 +0000514 default: assert(0 && "unknown type in store");
515 case MVT::i1: { // this is a bool
516 Opc = IA64::ST1; // we store either 0 or 1 as a byte
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000517 // first load zero!
518 SDOperand Initial = CurDAG->getCopyFromReg(Chain, IA64::r0, MVT::i64);
519 Chain = Initial.getValue(1);
520 // then load 1 into the same reg iff the predicate to store is 1
Evan Cheng8b2794a2006-10-13 21:14:26 +0000521 SDOperand Tmp = ST->getValue();
Evan Cheng6da2f322006-08-26 01:07:58 +0000522 AddToISelQueue(Tmp);
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000523 Tmp =
524 SDOperand(CurDAG->getTargetNode(IA64::TPCADDS, MVT::i64, Initial,
525 CurDAG->getTargetConstant(1, MVT::i64),
526 Tmp), 0);
Evan Cheng95514ba2006-08-26 08:00:10 +0000527 return CurDAG->SelectNodeTo(N, Opc, MVT::Other, Address, Tmp, Chain);
Chris Lattnerb19b8992005-11-30 23:02:08 +0000528 }
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000529 case MVT::i64: Opc = IA64::ST8; break;
530 case MVT::f64: Opc = IA64::STF8; break;
Duraid Madinad525df32005-11-07 03:11:02 +0000531 }
Evan Cheng8b2794a2006-10-13 21:14:26 +0000532 } else { // Truncating store
Dan Gohmanb625f2f2008-01-30 00:15:11 +0000533 switch(ST->getMemoryVT()) {
Duraid Madinad525df32005-11-07 03:11:02 +0000534 default: assert(0 && "unknown type in truncstore");
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000535 case MVT::i8: Opc = IA64::ST1; break;
536 case MVT::i16: Opc = IA64::ST2; break;
537 case MVT::i32: Opc = IA64::ST4; break;
538 case MVT::f32: Opc = IA64::STF4; break;
539 }
540 }
541
Evan Cheng6da2f322006-08-26 01:07:58 +0000542 SDOperand N1 = N->getOperand(1);
543 SDOperand N2 = N->getOperand(2);
544 AddToISelQueue(N1);
545 AddToISelQueue(N2);
Evan Cheng95514ba2006-08-26 08:00:10 +0000546 return CurDAG->SelectNodeTo(N, Opc, MVT::Other, N2, N1, Chain);
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000547 }
548
549 case ISD::BRCOND: {
Evan Cheng6da2f322006-08-26 01:07:58 +0000550 SDOperand Chain = N->getOperand(0);
551 SDOperand CC = N->getOperand(1);
552 AddToISelQueue(Chain);
553 AddToISelQueue(CC);
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000554 MachineBasicBlock *Dest =
555 cast<BasicBlockSDNode>(N->getOperand(2))->getBasicBlock();
556 //FIXME - we do NOT need long branches all the time
Evan Cheng23329f52006-08-16 07:30:09 +0000557 return CurDAG->SelectNodeTo(N, IA64::BRLCOND_NOTCALL, MVT::Other, CC,
Evan Cheng95514ba2006-08-26 08:00:10 +0000558 CurDAG->getBasicBlock(Dest), Chain);
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000559 }
560
561 case ISD::CALLSEQ_START:
562 case ISD::CALLSEQ_END: {
563 int64_t Amt = cast<ConstantSDNode>(N->getOperand(1))->getValue();
564 unsigned Opc = N->getOpcode() == ISD::CALLSEQ_START ?
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000565 IA64::ADJUSTCALLSTACKDOWN : IA64::ADJUSTCALLSTACKUP;
Evan Cheng6da2f322006-08-26 01:07:58 +0000566 SDOperand N0 = N->getOperand(0);
567 AddToISelQueue(N0);
Evan Cheng95514ba2006-08-26 08:00:10 +0000568 return CurDAG->SelectNodeTo(N, Opc, MVT::Other, getI64Imm(Amt), N0);
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000569 }
570
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000571 case ISD::BR:
Anton Korobeynikovbed29462007-04-16 18:10:23 +0000572 // FIXME: we don't need long branches all the time!
Evan Cheng6da2f322006-08-26 01:07:58 +0000573 SDOperand N0 = N->getOperand(0);
574 AddToISelQueue(N0);
Evan Cheng23329f52006-08-16 07:30:09 +0000575 return CurDAG->SelectNodeTo(N, IA64::BRL_NOTCALL, MVT::Other,
Evan Cheng95514ba2006-08-26 08:00:10 +0000576 N->getOperand(1), N0);
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000577 }
578
Evan Cheng9ade2182006-08-26 05:34:46 +0000579 return SelectCode(Op);
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000580}
581
582
583/// createIA64DAGToDAGInstructionSelector - This pass converts a legalized DAG
584/// into an IA64-specific DAG, ready for instruction scheduling.
585///
Evan Chengc4c62572006-03-13 23:20:37 +0000586FunctionPass
587*llvm::createIA64DAGToDAGInstructionSelector(IA64TargetMachine &TM) {
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000588 return new IA64DAGToDAGISel(TM);
589}
590