blob: 295b38246004285dbf1f72ce969dd5f60a5bc516 [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"
27#include "llvm/Support/Debug.h"
28#include "llvm/Support/MathExtras.h"
Chris Lattner2c2c6c62006-01-22 23:41:00 +000029#include <iostream>
Evan Chengba2f0a92006-02-05 06:46:41 +000030#include <set>
Duraid Madinaf2db9b82005-10-28 17:46:35 +000031using namespace llvm;
32
33namespace {
34 Statistic<> FusedFP ("ia64-codegen", "Number of fused fp operations");
35 Statistic<> FrameOff("ia64-codegen", "Number of frame idx offsets collapsed");
36
37 //===--------------------------------------------------------------------===//
38 /// IA64DAGToDAGISel - IA64 specific code to select IA64 machine
39 /// instructions for SelectionDAG operations.
40 ///
41 class IA64DAGToDAGISel : public SelectionDAGISel {
42 IA64TargetLowering IA64Lowering;
43 unsigned GlobalBaseReg;
44 public:
45 IA64DAGToDAGISel(TargetMachine &TM)
46 : SelectionDAGISel(IA64Lowering), IA64Lowering(TM) {}
47
48 virtual bool runOnFunction(Function &Fn) {
49 // Make sure we re-emit a set of the global base reg if necessary
50 GlobalBaseReg = 0;
51 return SelectionDAGISel::runOnFunction(Fn);
52 }
53
54 /// getI64Imm - Return a target constant with the specified value, of type
55 /// i64.
56 inline SDOperand getI64Imm(uint64_t Imm) {
57 return CurDAG->getTargetConstant(Imm, MVT::i64);
58 }
59
60 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
61 /// base register. Return the virtual register that holds this value.
62 // SDOperand getGlobalBaseReg(); TODO: hmm
63
64 // Select - Convert the specified operand from a target-independent to a
65 // target-specific node if it hasn't already been changed.
Evan Cheng34167212006-02-09 00:37:58 +000066 void Select(SDOperand &Result, SDOperand N);
Duraid Madinaf2db9b82005-10-28 17:46:35 +000067
68 SDNode *SelectIntImmediateExpr(SDOperand LHS, SDOperand RHS,
69 unsigned OCHi, unsigned OCLo,
70 bool IsArithmetic = false,
71 bool Negate = false);
72 SDNode *SelectBitfieldInsert(SDNode *N);
73
74 /// SelectCC - Select a comparison of the specified values with the
75 /// specified condition code, returning the CR# of the expression.
76 SDOperand SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC);
77
78 /// SelectAddr - Given the specified address, return the two operands for a
79 /// load/store instruction, and return true if it should be an indexed [r+r]
80 /// operation.
81 bool SelectAddr(SDOperand Addr, SDOperand &Op1, SDOperand &Op2);
82
83 SDOperand BuildSDIVSequence(SDNode *N);
84 SDOperand BuildUDIVSequence(SDNode *N);
85
86 /// InstructionSelectBasicBlock - This callback is invoked by
87 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
88 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
89
90 virtual const char *getPassName() const {
91 return "IA64 (Itanium) DAG->DAG Instruction Selector";
92 }
93
94// Include the pieces autogenerated from the target description.
95#include "IA64GenDAGISel.inc"
96
97private:
Duraid Madinab6f023a2005-11-21 14:14:54 +000098 SDOperand SelectDIV(SDOperand Op);
Duraid Madinaf2db9b82005-10-28 17:46:35 +000099 };
100}
101
102/// InstructionSelectBasicBlock - This callback is invoked by
103/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
104void IA64DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
105 DEBUG(BB->dump());
106
107 // The selection process is inherently a bottom-up recursive process (users
108 // select their uses before themselves). Given infinite stack space, we
109 // could just start selecting on the root and traverse the whole graph. In
110 // practice however, this causes us to run out of stack space on large basic
111 // blocks. To avoid this problem, select the entry node, then all its uses,
112 // iteratively instead of recursively.
113 std::vector<SDOperand> Worklist;
114 Worklist.push_back(DAG.getEntryNode());
115
116 // Note that we can do this in the IA64 target (scanning forward across token
117 // chain edges) because no nodes ever get folded across these edges. On a
118 // target like X86 which supports load/modify/store operations, this would
119 // have to be more careful.
120 while (!Worklist.empty()) {
121 SDOperand Node = Worklist.back();
122 Worklist.pop_back();
123
124 // Chose from the least deep of the top two nodes.
125 if (!Worklist.empty() &&
126 Worklist.back().Val->getNodeDepth() < Node.Val->getNodeDepth())
127 std::swap(Worklist.back(), Node);
128
129 if ((Node.Val->getOpcode() >= ISD::BUILTIN_OP_END &&
130 Node.Val->getOpcode() < IA64ISD::FIRST_NUMBER) ||
131 CodeGenMap.count(Node)) continue;
132
133 for (SDNode::use_iterator UI = Node.Val->use_begin(),
134 E = Node.Val->use_end(); UI != E; ++UI) {
135 // Scan the values. If this use has a value that is a token chain, add it
136 // to the worklist.
137 SDNode *User = *UI;
138 for (unsigned i = 0, e = User->getNumValues(); i != e; ++i)
139 if (User->getValueType(i) == MVT::Other) {
140 Worklist.push_back(SDOperand(User, i));
141 break;
142 }
143 }
144
145 // Finally, legalize this node.
Evan Cheng34167212006-02-09 00:37:58 +0000146 SDOperand Dummy;
147 Select(Dummy, Node);
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000148 }
149
150 // Select target instructions for the DAG.
Evan Chengba2f0a92006-02-05 06:46:41 +0000151 DAG.setRoot(SelectRoot(DAG.getRoot()));
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000152 CodeGenMap.clear();
153 DAG.RemoveDeadNodes();
154
155 // Emit machine code to BB.
156 ScheduleAndEmitDAG(DAG);
157}
158
Duraid Madinab6f023a2005-11-21 14:14:54 +0000159SDOperand IA64DAGToDAGISel::SelectDIV(SDOperand Op) {
160 SDNode *N = Op.Val;
Evan Cheng34167212006-02-09 00:37:58 +0000161 SDOperand Chain, Tmp1, Tmp2;
162 Select(Chain, N->getOperand(0));
Duraid Madinab6f023a2005-11-21 14:14:54 +0000163
Evan Cheng34167212006-02-09 00:37:58 +0000164 Select(Tmp1, N->getOperand(0));
165 Select(Tmp2, N->getOperand(1));
Duraid Madinab6f023a2005-11-21 14:14:54 +0000166
167 bool isFP=false;
168
169 if(MVT::isFloatingPoint(Tmp1.getValueType()))
170 isFP=true;
171
172 bool isModulus=false; // is it a division or a modulus?
173 bool isSigned=false;
174
175 switch(N->getOpcode()) {
176 case ISD::FDIV:
177 case ISD::SDIV: isModulus=false; isSigned=true; break;
178 case ISD::UDIV: isModulus=false; isSigned=false; break;
179 case ISD::FREM:
180 case ISD::SREM: isModulus=true; isSigned=true; break;
181 case ISD::UREM: isModulus=true; isSigned=false; break;
182 }
183
184 // TODO: check for integer divides by powers of 2 (or other simple patterns?)
185
186 SDOperand TmpPR, TmpPR2;
187 SDOperand TmpF1, TmpF2, TmpF3, TmpF4, TmpF5, TmpF6, TmpF7, TmpF8;
188 SDOperand TmpF9, TmpF10,TmpF11,TmpF12,TmpF13,TmpF14,TmpF15;
189 SDOperand Result;
Duraid Madina76bb6ae2006-01-16 14:33:04 +0000190
191 // we'll need copies of F0 and F1
192 SDOperand F0 = CurDAG->getRegister(IA64::F0, MVT::f64);
193 SDOperand F1 = CurDAG->getRegister(IA64::F1, MVT::f64);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000194
195 // OK, emit some code:
196
197 if(!isFP) {
198 // first, load the inputs into FP regs.
199 TmpF1 = CurDAG->getTargetNode(IA64::SETFSIG, MVT::f64, Tmp1);
200 Chain = TmpF1.getValue(1);
201 TmpF2 = CurDAG->getTargetNode(IA64::SETFSIG, MVT::f64, Tmp2);
202 Chain = TmpF2.getValue(1);
203
204 // next, convert the inputs to FP
205 if(isSigned) {
206 TmpF3 = CurDAG->getTargetNode(IA64::FCVTXF, MVT::f64, TmpF1);
207 Chain = TmpF3.getValue(1);
208 TmpF4 = CurDAG->getTargetNode(IA64::FCVTXF, MVT::f64, TmpF2);
209 Chain = TmpF4.getValue(1);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000210 } else { // is unsigned
Duraid Madina76bb6ae2006-01-16 14:33:04 +0000211 TmpF3 = CurDAG->getTargetNode(IA64::FCVTXUFS1, MVT::f64, TmpF1);
212 Chain = TmpF3.getValue(1);
213 TmpF4 = CurDAG->getTargetNode(IA64::FCVTXUFS1, MVT::f64, TmpF2);
214 Chain = TmpF4.getValue(1);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000215 }
216
217 } else { // this is an FP divide/remainder, so we 'leak' some temp
218 // regs and assign TmpF3=Tmp1, TmpF4=Tmp2
219 TmpF3=Tmp1;
220 TmpF4=Tmp2;
221 }
222
223 // we start by computing an approximate reciprocal (good to 9 bits?)
224 // note, this instruction writes _both_ TmpF5 (answer) and TmpPR (predicate)
Duraid Madina0c81dc82006-01-16 06:33:38 +0000225 if(isFP)
226 TmpF5 = CurDAG->getTargetNode(IA64::FRCPAS0, MVT::f64, MVT::i1,
Duraid Madinab6f023a2005-11-21 14:14:54 +0000227 TmpF3, TmpF4);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000228 else
229 TmpF5 = CurDAG->getTargetNode(IA64::FRCPAS1, MVT::f64, MVT::i1,
230 TmpF3, TmpF4);
231
Duraid Madinab6f023a2005-11-21 14:14:54 +0000232 TmpPR = TmpF5.getValue(1);
233 Chain = TmpF5.getValue(2);
234
Duraid Madina0c81dc82006-01-16 06:33:38 +0000235 SDOperand minusB;
236 if(isModulus) { // for remainders, it'll be handy to have
237 // copies of -input_b
238 minusB = CurDAG->getTargetNode(IA64::SUB, MVT::i64,
239 CurDAG->getRegister(IA64::r0, MVT::i64), Tmp2);
240 Chain = minusB.getValue(1);
241 }
242
243 SDOperand TmpE0, TmpY1, TmpE1, TmpY2;
244
245 TmpE0 = CurDAG->getTargetNode(IA64::CFNMAS1, MVT::f64,
Duraid Madinab6f023a2005-11-21 14:14:54 +0000246 TmpF4, TmpF5, F1, TmpPR);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000247 Chain = TmpE0.getValue(1);
248 TmpY1 = CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
249 TmpF5, TmpE0, TmpF5, TmpPR);
250 Chain = TmpY1.getValue(1);
251 TmpE1 = CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
252 TmpE0, TmpE0, F0, TmpPR);
253 Chain = TmpE1.getValue(1);
254 TmpY2 = CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
255 TmpY1, TmpE1, TmpY1, TmpPR);
256 Chain = TmpY2.getValue(1);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000257
Duraid Madina0c81dc82006-01-16 06:33:38 +0000258 if(isFP) { // if this is an FP divide, we finish up here and exit early
259 if(isModulus)
260 assert(0 && "Sorry, try another FORTRAN compiler.");
261
262 SDOperand TmpE2, TmpY3, TmpQ0, TmpR0;
263
264 TmpE2 = CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
265 TmpE1, TmpE1, F0, TmpPR);
266 Chain = TmpE2.getValue(1);
267 TmpY3 = CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
268 TmpY2, TmpE2, TmpY2, TmpPR);
269 Chain = TmpY3.getValue(1);
270 TmpQ0 = CurDAG->getTargetNode(IA64::CFMADS1, MVT::f64, // double prec!
271 Tmp1, TmpY3, F0, TmpPR);
272 Chain = TmpQ0.getValue(1);
273 TmpR0 = CurDAG->getTargetNode(IA64::CFNMADS1, MVT::f64, // double prec!
274 Tmp2, TmpQ0, Tmp1, TmpPR);
275 Chain = TmpR0.getValue(1);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000276
Duraid Madina0c81dc82006-01-16 06:33:38 +0000277// we want Result to have the same target register as the frcpa, so
278// we two-address hack it. See the comment "for this to work..." on
279// page 48 of Intel application note #245415
280 Result = CurDAG->getTargetNode(IA64::TCFMADS0, MVT::f64, // d.p. s0 rndg!
Duraid Madina76bb6ae2006-01-16 14:33:04 +0000281 TmpF5, TmpY3, TmpR0, TmpQ0, TmpPR);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000282 Chain = Result.getValue(1);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000283 return Result; // XXX: early exit!
284 } else { // this is *not* an FP divide, so there's a bit left to do:
285
286 SDOperand TmpQ2, TmpR2, TmpQ3, TmpQ;
287
288 TmpQ2 = CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
289 TmpF3, TmpY2, F0, TmpPR);
290 Chain = TmpQ2.getValue(1);
291 TmpR2 = CurDAG->getTargetNode(IA64::CFNMAS1, MVT::f64,
292 TmpF4, TmpQ2, TmpF3, TmpPR);
293 Chain = TmpR2.getValue(1);
Duraid Madinaae6dcdd2006-01-17 01:19:49 +0000294
Duraid Madina76bb6ae2006-01-16 14:33:04 +0000295// we want TmpQ3 to have the same target register as the frcpa? maybe we
296// should two-address hack it. See the comment "for this to work..." on page
297// 48 of Intel application note #245415
Duraid Madinaae6dcdd2006-01-17 01:19:49 +0000298 TmpQ3 = CurDAG->getTargetNode(IA64::TCFMAS1, MVT::f64,
299 TmpF5, TmpR2, TmpY2, TmpQ2, TmpPR);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000300 Chain = TmpQ3.getValue(1);
Duraid Madina76bb6ae2006-01-16 14:33:04 +0000301
Duraid Madinaae6dcdd2006-01-17 01:19:49 +0000302 // STORY: without these two-address instructions (TCFMAS1 and TCFMADS0)
303 // the FPSWA won't be able to help out in the case of large/tiny
304 // arguments. Other fun bugs may also appear, e.g. 0/x = x, not 0.
305
Duraid Madina0c81dc82006-01-16 06:33:38 +0000306 if(isSigned)
307 TmpQ = CurDAG->getTargetNode(IA64::FCVTFXTRUNCS1, MVT::f64, TmpQ3);
308 else
309 TmpQ = CurDAG->getTargetNode(IA64::FCVTFXUTRUNCS1, MVT::f64, TmpQ3);
310
311 Chain = TmpQ.getValue(1);
312
313 if(isModulus) {
314 SDOperand FPminusB = CurDAG->getTargetNode(IA64::SETFSIG, MVT::f64,
315 minusB);
316 Chain = FPminusB.getValue(1);
317 SDOperand Remainder = CurDAG->getTargetNode(IA64::XMAL, MVT::f64,
318 TmpQ, FPminusB, TmpF1);
319 Chain = Remainder.getValue(1);
320 Result = CurDAG->getTargetNode(IA64::GETFSIG, MVT::i64, Remainder);
321 Chain = Result.getValue(1);
322 } else { // just an integer divide
323 Result = CurDAG->getTargetNode(IA64::GETFSIG, MVT::i64, TmpQ);
324 Chain = Result.getValue(1);
325 }
326
327 return Result;
328 } // wasn't an FP divide
Duraid Madinab6f023a2005-11-21 14:14:54 +0000329}
330
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000331// Select - Convert the specified operand from a target-independent to a
332// target-specific node if it hasn't already been changed.
Evan Cheng34167212006-02-09 00:37:58 +0000333void IA64DAGToDAGISel::Select(SDOperand &Result, SDOperand Op) {
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000334 SDNode *N = Op.Val;
335 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
Evan Cheng34167212006-02-09 00:37:58 +0000336 N->getOpcode() < IA64ISD::FIRST_NUMBER) {
337 Result = Op;
338 return; // Already selected.
339 }
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000340
341 // If this has already been converted, use it.
342 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(Op);
Evan Cheng34167212006-02-09 00:37:58 +0000343 if (CGMI != CodeGenMap.end()) {
344 Result = CGMI->second;
345 return;
346 }
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000347
348 switch (N->getOpcode()) {
349 default: break;
350
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000351 case IA64ISD::BRCALL: { // XXX: this is also a hack!
Evan Cheng34167212006-02-09 00:37:58 +0000352 SDOperand Chain;
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000353 SDOperand InFlag; // Null incoming flag value.
354
Evan Cheng34167212006-02-09 00:37:58 +0000355 Select(Chain, N->getOperand(0));
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000356 if(N->getNumOperands()==3) // we have an incoming chain, callee and flag
Evan Cheng34167212006-02-09 00:37:58 +0000357 Select(InFlag, N->getOperand(2));
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000358
359 unsigned CallOpcode;
360 SDOperand CallOperand;
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000361
362 // if we can call directly, do so
363 if (GlobalAddressSDNode *GASD =
364 dyn_cast<GlobalAddressSDNode>(N->getOperand(1))) {
365 CallOpcode = IA64::BRCALL_IPREL_GA;
366 CallOperand = CurDAG->getTargetGlobalAddress(GASD->getGlobal(), MVT::i64);
367 } else if (ExternalSymbolSDNode *ESSDN = // FIXME: we currently NEED this
368 // case for correctness, to avoid
369 // "non-pic code with imm reloc.n
370 // against dynamic symbol" errors
371 dyn_cast<ExternalSymbolSDNode>(N->getOperand(1))) {
372 CallOpcode = IA64::BRCALL_IPREL_ES;
373 CallOperand = N->getOperand(1);
374 } else {
375 // otherwise we need to load the function descriptor,
376 // load the branch target (function)'s entry point and GP,
377 // branch (call) then restore the GP
Evan Cheng34167212006-02-09 00:37:58 +0000378 SDOperand FnDescriptor;
379 Select(FnDescriptor, N->getOperand(1));
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000380
381 // load the branch target's entry point [mem] and
382 // GP value [mem+8]
383 SDOperand targetEntryPoint=CurDAG->getTargetNode(IA64::LD8, MVT::i64,
384 FnDescriptor);
385 Chain = targetEntryPoint.getValue(1);
386 SDOperand targetGPAddr=CurDAG->getTargetNode(IA64::ADDS, MVT::i64,
387 FnDescriptor, CurDAG->getConstant(8, MVT::i64));
388 Chain = targetGPAddr.getValue(1);
389 SDOperand targetGP=CurDAG->getTargetNode(IA64::LD8, MVT::i64,
390 targetGPAddr);
391 Chain = targetGP.getValue(1);
392
393 Chain = CurDAG->getCopyToReg(Chain, IA64::r1, targetGP, InFlag);
394 InFlag = Chain.getValue(1);
395 Chain = CurDAG->getCopyToReg(Chain, IA64::B6, targetEntryPoint, InFlag); // FLAG these?
396 InFlag = Chain.getValue(1);
397
398 CallOperand = CurDAG->getRegister(IA64::B6, MVT::i64);
399 CallOpcode = IA64::BRCALL_INDIRECT;
400 }
401
402 // Finally, once everything is setup, emit the call itself
403 if(InFlag.Val)
Duraid Madinab13d74a2005-12-25 14:09:08 +0000404 Chain = CurDAG->getTargetNode(CallOpcode, MVT::Other, MVT::Flag, CallOperand, InFlag);
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000405 else // there might be no arguments
406 Chain = CurDAG->getTargetNode(CallOpcode, MVT::Other, MVT::Flag, CallOperand, Chain);
407 InFlag = Chain.getValue(1);
408
409 std::vector<SDOperand> CallResults;
410
411 CallResults.push_back(Chain);
412 CallResults.push_back(InFlag);
413
414 for (unsigned i = 0, e = CallResults.size(); i != e; ++i)
415 CodeGenMap[Op.getValue(i)] = CallResults[i];
Evan Cheng34167212006-02-09 00:37:58 +0000416 Result = CallResults[Op.ResNo];
417 return;
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000418 }
Duraid Madinaa36153a2005-12-22 03:58:17 +0000419
Duraid Madina8617f3c2005-12-22 07:14:45 +0000420 case IA64ISD::GETFD: {
Evan Cheng34167212006-02-09 00:37:58 +0000421 SDOperand Input;
422 Select(Input, N->getOperand(0));
423 Result = CurDAG->getTargetNode(IA64::GETFD, MVT::i64, Input);
Duraid Madinabf094582006-01-11 03:50:40 +0000424 CodeGenMap[Op] = Result;
Evan Cheng34167212006-02-09 00:37:58 +0000425 return;
Duraid Madina8617f3c2005-12-22 07:14:45 +0000426 }
427
Duraid Madinab6f023a2005-11-21 14:14:54 +0000428 case ISD::FDIV:
429 case ISD::SDIV:
430 case ISD::UDIV:
431 case ISD::SREM:
Evan Cheng34167212006-02-09 00:37:58 +0000432 case ISD::UREM:
433 Result = SelectDIV(Op);
434 return;
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000435
Chris Lattnera54aa942006-01-29 06:26:08 +0000436 case ISD::TargetConstantFP: {
Duraid Madina056728f2005-11-02 07:32:59 +0000437 SDOperand Chain = CurDAG->getEntryNode(); // this is a constant, so..
438
Evan Cheng34167212006-02-09 00:37:58 +0000439 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0)) {
440 Result = CurDAG->getCopyFromReg(Chain, IA64::F0, MVT::f64);
441 } else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0)) {
442 Result = CurDAG->getCopyFromReg(Chain, IA64::F1, MVT::f64);
443 } else
Duraid Madina93856802005-11-02 02:35:04 +0000444 assert(0 && "Unexpected FP constant!");
Evan Cheng34167212006-02-09 00:37:58 +0000445 return;
Duraid Madina93856802005-11-02 02:35:04 +0000446 }
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000447
448 case ISD::FrameIndex: { // TODO: reduce creepyness
449 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Chris Lattnerb19b8992005-11-30 23:02:08 +0000450 if (N->hasOneUse())
Evan Cheng34167212006-02-09 00:37:58 +0000451 Result = CurDAG->SelectNodeTo(N, IA64::MOV, MVT::i64,
Chris Lattnerb19b8992005-11-30 23:02:08 +0000452 CurDAG->getTargetFrameIndex(FI, MVT::i64));
Duraid Madina19e5e142006-01-21 14:27:19 +0000453 else
Evan Cheng34167212006-02-09 00:37:58 +0000454 Result = CodeGenMap[Op] = CurDAG->getTargetNode(IA64::MOV, MVT::i64,
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000455 CurDAG->getTargetFrameIndex(FI, MVT::i64));
Evan Cheng34167212006-02-09 00:37:58 +0000456 return;
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000457 }
458
Duraid Madina2e0348e2006-01-15 09:45:23 +0000459 case ISD::ConstantPool: { // TODO: nuke the constant pool
460 // (ia64 doesn't need one)
Evan Chengb8973bd2006-01-31 22:23:14 +0000461 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
462 Constant *C = CP->get();
463 SDOperand CPI = CurDAG->getTargetConstantPool(C, MVT::i64,
464 CP->getAlignment());
Evan Cheng34167212006-02-09 00:37:58 +0000465 Result = CurDAG->getTargetNode(IA64::ADDL_GA, MVT::i64, // ?
Duraid Madina25d0a882005-10-29 16:08:30 +0000466 CurDAG->getRegister(IA64::r1, MVT::i64), CPI);
Evan Cheng34167212006-02-09 00:37:58 +0000467 return;
Duraid Madina25d0a882005-10-29 16:08:30 +0000468 }
469
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000470 case ISD::GlobalAddress: {
471 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
472 SDOperand GA = CurDAG->getTargetGlobalAddress(GV, MVT::i64);
473 SDOperand Tmp = CurDAG->getTargetNode(IA64::ADDL_GA, MVT::i64,
474 CurDAG->getRegister(IA64::r1, MVT::i64), GA);
Evan Cheng34167212006-02-09 00:37:58 +0000475 Result = CurDAG->getTargetNode(IA64::LD8, MVT::i64, Tmp);
476 return;
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000477 }
Duraid Madinaa36153a2005-12-22 03:58:17 +0000478
479/* XXX case ISD::ExternalSymbol: {
480 SDOperand EA = CurDAG->getTargetExternalSymbol(cast<ExternalSymbolSDNode>(N)->getSymbol(),
481 MVT::i64);
482 SDOperand Tmp = CurDAG->getTargetNode(IA64::ADDL_EA, MVT::i64,
483 CurDAG->getRegister(IA64::r1, MVT::i64), EA);
484 return CurDAG->getTargetNode(IA64::LD8, MVT::i64, Tmp);
485 }
486*/
487
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000488 case ISD::LOAD:
Duraid Madinaecc1a1b2006-01-20 16:10:05 +0000489 case ISD::EXTLOAD: // FIXME: load -1, not 1, for bools?
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000490 case ISD::ZEXTLOAD: {
Evan Cheng34167212006-02-09 00:37:58 +0000491 SDOperand Chain, Address;
492 Select(Chain, N->getOperand(0));
493 Select(Address, N->getOperand(1));
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000494
495 MVT::ValueType TypeBeingLoaded = (N->getOpcode() == ISD::LOAD) ?
496 N->getValueType(0) : cast<VTSDNode>(N->getOperand(3))->getVT();
497 unsigned Opc;
498 switch (TypeBeingLoaded) {
499 default: N->dump(); assert(0 && "Cannot load this type!");
Duraid Madina9f729062005-11-04 09:59:06 +0000500 case MVT::i1: { // this is a bool
501 Opc = IA64::LD1; // first we load a byte, then compare for != 0
Evan Cheng34167212006-02-09 00:37:58 +0000502 if(N->getValueType(0) == MVT::i1) { // XXX: early exit!
503 Result = CurDAG->SelectNodeTo(N, IA64::CMPNE, MVT::i1, MVT::Other,
Chris Lattnerb19b8992005-11-30 23:02:08 +0000504 CurDAG->getTargetNode(Opc, MVT::i64, Address),
505 CurDAG->getRegister(IA64::r0, MVT::i64),
506 Chain).getValue(Op.ResNo);
Evan Cheng34167212006-02-09 00:37:58 +0000507 return;
508 }
Duraid Madinaa36153a2005-12-22 03:58:17 +0000509 /* otherwise, we want to load a bool into something bigger: LD1
510 will do that for us, so we just fall through */
Chris Lattnerb19b8992005-11-30 23:02:08 +0000511 }
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000512 case MVT::i8: Opc = IA64::LD1; break;
513 case MVT::i16: Opc = IA64::LD2; break;
514 case MVT::i32: Opc = IA64::LD4; break;
515 case MVT::i64: Opc = IA64::LD8; break;
516
517 case MVT::f32: Opc = IA64::LDF4; break;
518 case MVT::f64: Opc = IA64::LDF8; break;
519 }
520
Chris Lattnerb19b8992005-11-30 23:02:08 +0000521 // TODO: comment this
Evan Cheng34167212006-02-09 00:37:58 +0000522 Result = CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), MVT::Other,
Chris Lattnerb19b8992005-11-30 23:02:08 +0000523 Address, Chain).getValue(Op.ResNo);
Evan Cheng34167212006-02-09 00:37:58 +0000524 return;
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000525 }
526
527 case ISD::TRUNCSTORE:
528 case ISD::STORE: {
Evan Cheng34167212006-02-09 00:37:58 +0000529 SDOperand Address, Chain;
530 Select(Address, N->getOperand(2));
531 Select(Chain, N->getOperand(0));
Duraid Madinad525df32005-11-07 03:11:02 +0000532
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000533 unsigned Opc;
534 if (N->getOpcode() == ISD::STORE) {
535 switch (N->getOperand(1).getValueType()) {
Duraid Madinad525df32005-11-07 03:11:02 +0000536 default: assert(0 && "unknown type in store");
537 case MVT::i1: { // this is a bool
538 Opc = IA64::ST1; // we store either 0 or 1 as a byte
Duraid Madina544cbbd2006-01-13 10:28:25 +0000539 // first load zero!
540 SDOperand Initial = CurDAG->getCopyFromReg(Chain, IA64::r0, MVT::i64);
541 Chain = Initial.getValue(1);
Duraid Madinaa7fb5be2006-01-20 03:40:25 +0000542 // then load 1 into the same reg iff the predicate to store is 1
Evan Cheng34167212006-02-09 00:37:58 +0000543 SDOperand Tmp;
544 Select(Tmp, N->getOperand(1));
545 CurDAG->getTargetNode(IA64::TPCADDS, MVT::i64, Initial,
546 CurDAG->getConstant(1, MVT::i64),
547 Tmp);
548 Result = CurDAG->SelectNodeTo(N, Opc, MVT::Other, Address, Tmp, Chain);
549 return;
Chris Lattnerb19b8992005-11-30 23:02:08 +0000550 }
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000551 case MVT::i64: Opc = IA64::ST8; break;
552 case MVT::f64: Opc = IA64::STF8; break;
Duraid Madinad525df32005-11-07 03:11:02 +0000553 }
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000554 } else { //ISD::TRUNCSTORE
555 switch(cast<VTSDNode>(N->getOperand(4))->getVT()) {
Duraid Madinad525df32005-11-07 03:11:02 +0000556 default: assert(0 && "unknown type in truncstore");
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000557 case MVT::i8: Opc = IA64::ST1; break;
558 case MVT::i16: Opc = IA64::ST2; break;
559 case MVT::i32: Opc = IA64::ST4; break;
560 case MVT::f32: Opc = IA64::STF4; break;
561 }
562 }
563
Evan Cheng34167212006-02-09 00:37:58 +0000564 SDOperand N1, N2;
565 Select(N1, N->getOperand(1));
566 Select(N2, N->getOperand(2));
567 Result = CurDAG->SelectNodeTo(N, Opc, MVT::Other, N2, N1, Chain);
568 return;
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000569 }
570
571 case ISD::BRCOND: {
Evan Cheng34167212006-02-09 00:37:58 +0000572 SDOperand Chain, CC;
573 Select(Chain, N->getOperand(0));
574 Select(CC, N->getOperand(1));
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000575 MachineBasicBlock *Dest =
576 cast<BasicBlockSDNode>(N->getOperand(2))->getBasicBlock();
577 //FIXME - we do NOT need long branches all the time
Evan Cheng34167212006-02-09 00:37:58 +0000578 Result = CurDAG->SelectNodeTo(N, IA64::BRLCOND_NOTCALL, MVT::Other, CC,
Chris Lattnerb19b8992005-11-30 23:02:08 +0000579 CurDAG->getBasicBlock(Dest), Chain);
Evan Cheng34167212006-02-09 00:37:58 +0000580 return;
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000581 }
582
583 case ISD::CALLSEQ_START:
584 case ISD::CALLSEQ_END: {
585 int64_t Amt = cast<ConstantSDNode>(N->getOperand(1))->getValue();
586 unsigned Opc = N->getOpcode() == ISD::CALLSEQ_START ?
587 IA64::ADJUSTCALLSTACKDOWN : IA64::ADJUSTCALLSTACKUP;
Evan Cheng34167212006-02-09 00:37:58 +0000588 SDOperand N0;
589 Select(N0, N->getOperand(0));
590 Result = CurDAG->SelectNodeTo(N, Opc, MVT::Other, getI64Imm(Amt), N0);
591 return;
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000592 }
593
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000594 case ISD::BR:
595 // FIXME: we don't need long branches all the time!
Evan Cheng34167212006-02-09 00:37:58 +0000596 SDOperand N0;
597 Select(N0, N->getOperand(0));
598 Result = CurDAG->SelectNodeTo(N, IA64::BRL_NOTCALL, MVT::Other,
599 N->getOperand(1), N0);
600 return;
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000601 }
602
Evan Cheng34167212006-02-09 00:37:58 +0000603 SelectCode(Result, Op);
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000604}
605
606
607/// createIA64DAGToDAGInstructionSelector - This pass converts a legalized DAG
608/// into an IA64-specific DAG, ready for instruction scheduling.
609///
610FunctionPass *llvm::createIA64DAGToDAGInstructionSelector(TargetMachine &TM) {
611 return new IA64DAGToDAGISel(TM);
612}
613