blob: 046e30b1684d6702f091c1a3d218ed74852de7ad [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>
Duraid Madinaf2db9b82005-10-28 17:46:35 +000030using namespace llvm;
31
32namespace {
33 Statistic<> FusedFP ("ia64-codegen", "Number of fused fp operations");
34 Statistic<> FrameOff("ia64-codegen", "Number of frame idx offsets collapsed");
35
36 //===--------------------------------------------------------------------===//
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:
44 IA64DAGToDAGISel(TargetMachine &TM)
45 : SelectionDAGISel(IA64Lowering), IA64Lowering(TM) {}
46
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.
65 SDOperand Select(SDOperand Op);
66
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
82 SDOperand BuildSDIVSequence(SDNode *N);
83 SDOperand BuildUDIVSequence(SDNode *N);
84
85 /// 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());
105
106 // The selection process is inherently a bottom-up recursive process (users
107 // select their uses before themselves). Given infinite stack space, we
108 // could just start selecting on the root and traverse the whole graph. In
109 // practice however, this causes us to run out of stack space on large basic
110 // blocks. To avoid this problem, select the entry node, then all its uses,
111 // iteratively instead of recursively.
112 std::vector<SDOperand> Worklist;
113 Worklist.push_back(DAG.getEntryNode());
114
115 // Note that we can do this in the IA64 target (scanning forward across token
116 // chain edges) because no nodes ever get folded across these edges. On a
117 // target like X86 which supports load/modify/store operations, this would
118 // have to be more careful.
119 while (!Worklist.empty()) {
120 SDOperand Node = Worklist.back();
121 Worklist.pop_back();
122
123 // Chose from the least deep of the top two nodes.
124 if (!Worklist.empty() &&
125 Worklist.back().Val->getNodeDepth() < Node.Val->getNodeDepth())
126 std::swap(Worklist.back(), Node);
127
128 if ((Node.Val->getOpcode() >= ISD::BUILTIN_OP_END &&
129 Node.Val->getOpcode() < IA64ISD::FIRST_NUMBER) ||
130 CodeGenMap.count(Node)) continue;
131
132 for (SDNode::use_iterator UI = Node.Val->use_begin(),
133 E = Node.Val->use_end(); UI != E; ++UI) {
134 // Scan the values. If this use has a value that is a token chain, add it
135 // to the worklist.
136 SDNode *User = *UI;
137 for (unsigned i = 0, e = User->getNumValues(); i != e; ++i)
138 if (User->getValueType(i) == MVT::Other) {
139 Worklist.push_back(SDOperand(User, i));
140 break;
141 }
142 }
143
144 // Finally, legalize this node.
145 Select(Node);
146 }
147
148 // Select target instructions for the DAG.
149 DAG.setRoot(Select(DAG.getRoot()));
150 CodeGenMap.clear();
151 DAG.RemoveDeadNodes();
152
153 // Emit machine code to BB.
154 ScheduleAndEmitDAG(DAG);
155}
156
Duraid Madinab6f023a2005-11-21 14:14:54 +0000157SDOperand IA64DAGToDAGISel::SelectDIV(SDOperand Op) {
158 SDNode *N = Op.Val;
159 SDOperand Chain = Select(N->getOperand(0));
160
161 SDOperand Tmp1 = Select(N->getOperand(0));
162 SDOperand Tmp2 = Select(N->getOperand(1));
163
164 bool isFP=false;
165
166 if(MVT::isFloatingPoint(Tmp1.getValueType()))
167 isFP=true;
168
169 bool isModulus=false; // is it a division or a modulus?
170 bool isSigned=false;
171
172 switch(N->getOpcode()) {
173 case ISD::FDIV:
174 case ISD::SDIV: isModulus=false; isSigned=true; break;
175 case ISD::UDIV: isModulus=false; isSigned=false; break;
176 case ISD::FREM:
177 case ISD::SREM: isModulus=true; isSigned=true; break;
178 case ISD::UREM: isModulus=true; isSigned=false; break;
179 }
180
181 // TODO: check for integer divides by powers of 2 (or other simple patterns?)
182
183 SDOperand TmpPR, TmpPR2;
184 SDOperand TmpF1, TmpF2, TmpF3, TmpF4, TmpF5, TmpF6, TmpF7, TmpF8;
185 SDOperand TmpF9, TmpF10,TmpF11,TmpF12,TmpF13,TmpF14,TmpF15;
186 SDOperand Result;
Duraid Madina76bb6ae2006-01-16 14:33:04 +0000187
188 // we'll need copies of F0 and F1
189 SDOperand F0 = CurDAG->getRegister(IA64::F0, MVT::f64);
190 SDOperand F1 = CurDAG->getRegister(IA64::F1, MVT::f64);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000191
192 // OK, emit some code:
193
194 if(!isFP) {
195 // first, load the inputs into FP regs.
196 TmpF1 = CurDAG->getTargetNode(IA64::SETFSIG, MVT::f64, Tmp1);
197 Chain = TmpF1.getValue(1);
198 TmpF2 = CurDAG->getTargetNode(IA64::SETFSIG, MVT::f64, Tmp2);
199 Chain = TmpF2.getValue(1);
200
201 // next, convert the inputs to FP
202 if(isSigned) {
203 TmpF3 = CurDAG->getTargetNode(IA64::FCVTXF, MVT::f64, TmpF1);
204 Chain = TmpF3.getValue(1);
205 TmpF4 = CurDAG->getTargetNode(IA64::FCVTXF, MVT::f64, TmpF2);
206 Chain = TmpF4.getValue(1);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000207 } else { // is unsigned
Duraid Madina76bb6ae2006-01-16 14:33:04 +0000208 TmpF3 = CurDAG->getTargetNode(IA64::FCVTXUFS1, MVT::f64, TmpF1);
209 Chain = TmpF3.getValue(1);
210 TmpF4 = CurDAG->getTargetNode(IA64::FCVTXUFS1, MVT::f64, TmpF2);
211 Chain = TmpF4.getValue(1);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000212 }
213
214 } else { // this is an FP divide/remainder, so we 'leak' some temp
215 // regs and assign TmpF3=Tmp1, TmpF4=Tmp2
216 TmpF3=Tmp1;
217 TmpF4=Tmp2;
218 }
219
220 // we start by computing an approximate reciprocal (good to 9 bits?)
221 // note, this instruction writes _both_ TmpF5 (answer) and TmpPR (predicate)
Duraid Madina0c81dc82006-01-16 06:33:38 +0000222 if(isFP)
223 TmpF5 = CurDAG->getTargetNode(IA64::FRCPAS0, MVT::f64, MVT::i1,
Duraid Madinab6f023a2005-11-21 14:14:54 +0000224 TmpF3, TmpF4);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000225 else
226 TmpF5 = CurDAG->getTargetNode(IA64::FRCPAS1, MVT::f64, MVT::i1,
227 TmpF3, TmpF4);
228
Duraid Madinab6f023a2005-11-21 14:14:54 +0000229 TmpPR = TmpF5.getValue(1);
230 Chain = TmpF5.getValue(2);
231
Duraid Madina0c81dc82006-01-16 06:33:38 +0000232 SDOperand minusB;
233 if(isModulus) { // for remainders, it'll be handy to have
234 // copies of -input_b
235 minusB = CurDAG->getTargetNode(IA64::SUB, MVT::i64,
236 CurDAG->getRegister(IA64::r0, MVT::i64), Tmp2);
237 Chain = minusB.getValue(1);
238 }
239
240 SDOperand TmpE0, TmpY1, TmpE1, TmpY2;
241
242 TmpE0 = CurDAG->getTargetNode(IA64::CFNMAS1, MVT::f64,
Duraid Madinab6f023a2005-11-21 14:14:54 +0000243 TmpF4, TmpF5, F1, TmpPR);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000244 Chain = TmpE0.getValue(1);
245 TmpY1 = CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
246 TmpF5, TmpE0, TmpF5, TmpPR);
247 Chain = TmpY1.getValue(1);
248 TmpE1 = CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
249 TmpE0, TmpE0, F0, TmpPR);
250 Chain = TmpE1.getValue(1);
251 TmpY2 = CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
252 TmpY1, TmpE1, TmpY1, TmpPR);
253 Chain = TmpY2.getValue(1);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000254
Duraid Madina0c81dc82006-01-16 06:33:38 +0000255 if(isFP) { // if this is an FP divide, we finish up here and exit early
256 if(isModulus)
257 assert(0 && "Sorry, try another FORTRAN compiler.");
258
259 SDOperand TmpE2, TmpY3, TmpQ0, TmpR0;
260
261 TmpE2 = CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
262 TmpE1, TmpE1, F0, TmpPR);
263 Chain = TmpE2.getValue(1);
264 TmpY3 = CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
265 TmpY2, TmpE2, TmpY2, TmpPR);
266 Chain = TmpY3.getValue(1);
267 TmpQ0 = CurDAG->getTargetNode(IA64::CFMADS1, MVT::f64, // double prec!
268 Tmp1, TmpY3, F0, TmpPR);
269 Chain = TmpQ0.getValue(1);
270 TmpR0 = CurDAG->getTargetNode(IA64::CFNMADS1, MVT::f64, // double prec!
271 Tmp2, TmpQ0, Tmp1, TmpPR);
272 Chain = TmpR0.getValue(1);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000273
Duraid Madina0c81dc82006-01-16 06:33:38 +0000274// we want Result to have the same target register as the frcpa, so
275// we two-address hack it. See the comment "for this to work..." on
276// page 48 of Intel application note #245415
277 Result = CurDAG->getTargetNode(IA64::TCFMADS0, MVT::f64, // d.p. s0 rndg!
Duraid Madina76bb6ae2006-01-16 14:33:04 +0000278 TmpF5, TmpY3, TmpR0, TmpQ0, TmpPR);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000279 Chain = Result.getValue(1);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000280 return Result; // XXX: early exit!
281 } else { // this is *not* an FP divide, so there's a bit left to do:
282
283 SDOperand TmpQ2, TmpR2, TmpQ3, TmpQ;
284
285 TmpQ2 = CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
286 TmpF3, TmpY2, F0, TmpPR);
287 Chain = TmpQ2.getValue(1);
288 TmpR2 = CurDAG->getTargetNode(IA64::CFNMAS1, MVT::f64,
289 TmpF4, TmpQ2, TmpF3, TmpPR);
290 Chain = TmpR2.getValue(1);
Duraid Madinaae6dcdd2006-01-17 01:19:49 +0000291
Duraid Madina76bb6ae2006-01-16 14:33:04 +0000292// we want TmpQ3 to have the same target register as the frcpa? maybe we
293// should two-address hack it. See the comment "for this to work..." on page
294// 48 of Intel application note #245415
Duraid Madinaae6dcdd2006-01-17 01:19:49 +0000295 TmpQ3 = CurDAG->getTargetNode(IA64::TCFMAS1, MVT::f64,
296 TmpF5, TmpR2, TmpY2, TmpQ2, TmpPR);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000297 Chain = TmpQ3.getValue(1);
Duraid Madina76bb6ae2006-01-16 14:33:04 +0000298
Duraid Madinaae6dcdd2006-01-17 01:19:49 +0000299 // STORY: without these two-address instructions (TCFMAS1 and TCFMADS0)
300 // the FPSWA won't be able to help out in the case of large/tiny
301 // arguments. Other fun bugs may also appear, e.g. 0/x = x, not 0.
302
Duraid Madina0c81dc82006-01-16 06:33:38 +0000303 if(isSigned)
304 TmpQ = CurDAG->getTargetNode(IA64::FCVTFXTRUNCS1, MVT::f64, TmpQ3);
305 else
306 TmpQ = CurDAG->getTargetNode(IA64::FCVTFXUTRUNCS1, MVT::f64, TmpQ3);
307
308 Chain = TmpQ.getValue(1);
309
310 if(isModulus) {
311 SDOperand FPminusB = CurDAG->getTargetNode(IA64::SETFSIG, MVT::f64,
312 minusB);
313 Chain = FPminusB.getValue(1);
314 SDOperand Remainder = CurDAG->getTargetNode(IA64::XMAL, MVT::f64,
315 TmpQ, FPminusB, TmpF1);
316 Chain = Remainder.getValue(1);
317 Result = CurDAG->getTargetNode(IA64::GETFSIG, MVT::i64, Remainder);
318 Chain = Result.getValue(1);
319 } else { // just an integer divide
320 Result = CurDAG->getTargetNode(IA64::GETFSIG, MVT::i64, TmpQ);
321 Chain = Result.getValue(1);
322 }
323
324 return Result;
325 } // wasn't an FP divide
Duraid Madinab6f023a2005-11-21 14:14:54 +0000326}
327
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000328// Select - Convert the specified operand from a target-independent to a
329// target-specific node if it hasn't already been changed.
330SDOperand IA64DAGToDAGISel::Select(SDOperand Op) {
331 SDNode *N = Op.Val;
332 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
333 N->getOpcode() < IA64ISD::FIRST_NUMBER)
334 return Op; // Already selected.
335
336 // If this has already been converted, use it.
337 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(Op);
338 if (CGMI != CodeGenMap.end()) return CGMI->second;
339
340 switch (N->getOpcode()) {
341 default: break;
342
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000343 case IA64ISD::BRCALL: { // XXX: this is also a hack!
344 SDOperand Chain = Select(N->getOperand(0));
345 SDOperand InFlag; // Null incoming flag value.
346
347 if(N->getNumOperands()==3) // we have an incoming chain, callee and flag
348 InFlag = Select(N->getOperand(2));
349
350 unsigned CallOpcode;
351 SDOperand CallOperand;
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000352
353 // if we can call directly, do so
354 if (GlobalAddressSDNode *GASD =
355 dyn_cast<GlobalAddressSDNode>(N->getOperand(1))) {
356 CallOpcode = IA64::BRCALL_IPREL_GA;
357 CallOperand = CurDAG->getTargetGlobalAddress(GASD->getGlobal(), MVT::i64);
358 } else if (ExternalSymbolSDNode *ESSDN = // FIXME: we currently NEED this
359 // case for correctness, to avoid
360 // "non-pic code with imm reloc.n
361 // against dynamic symbol" errors
362 dyn_cast<ExternalSymbolSDNode>(N->getOperand(1))) {
363 CallOpcode = IA64::BRCALL_IPREL_ES;
364 CallOperand = N->getOperand(1);
365 } else {
366 // otherwise we need to load the function descriptor,
367 // load the branch target (function)'s entry point and GP,
368 // branch (call) then restore the GP
369 SDOperand FnDescriptor = Select(N->getOperand(1));
370
371 // load the branch target's entry point [mem] and
372 // GP value [mem+8]
373 SDOperand targetEntryPoint=CurDAG->getTargetNode(IA64::LD8, MVT::i64,
374 FnDescriptor);
375 Chain = targetEntryPoint.getValue(1);
376 SDOperand targetGPAddr=CurDAG->getTargetNode(IA64::ADDS, MVT::i64,
377 FnDescriptor, CurDAG->getConstant(8, MVT::i64));
378 Chain = targetGPAddr.getValue(1);
379 SDOperand targetGP=CurDAG->getTargetNode(IA64::LD8, MVT::i64,
380 targetGPAddr);
381 Chain = targetGP.getValue(1);
382
383 Chain = CurDAG->getCopyToReg(Chain, IA64::r1, targetGP, InFlag);
384 InFlag = Chain.getValue(1);
385 Chain = CurDAG->getCopyToReg(Chain, IA64::B6, targetEntryPoint, InFlag); // FLAG these?
386 InFlag = Chain.getValue(1);
387
388 CallOperand = CurDAG->getRegister(IA64::B6, MVT::i64);
389 CallOpcode = IA64::BRCALL_INDIRECT;
390 }
391
392 // Finally, once everything is setup, emit the call itself
393 if(InFlag.Val)
Duraid Madinab13d74a2005-12-25 14:09:08 +0000394 Chain = CurDAG->getTargetNode(CallOpcode, MVT::Other, MVT::Flag, CallOperand, InFlag);
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000395 else // there might be no arguments
396 Chain = CurDAG->getTargetNode(CallOpcode, MVT::Other, MVT::Flag, CallOperand, Chain);
397 InFlag = Chain.getValue(1);
398
399 std::vector<SDOperand> CallResults;
400
401 CallResults.push_back(Chain);
402 CallResults.push_back(InFlag);
403
404 for (unsigned i = 0, e = CallResults.size(); i != e; ++i)
405 CodeGenMap[Op.getValue(i)] = CallResults[i];
406 return CallResults[Op.ResNo];
407 }
Duraid Madinaa36153a2005-12-22 03:58:17 +0000408
Duraid Madina8617f3c2005-12-22 07:14:45 +0000409 case IA64ISD::GETFD: {
410 SDOperand Input = Select(N->getOperand(0));
Duraid Madinabf094582006-01-11 03:50:40 +0000411 SDOperand Result = CurDAG->getTargetNode(IA64::GETFD, MVT::i64, Input);
412 CodeGenMap[Op] = Result;
413 return Result;
Duraid Madina8617f3c2005-12-22 07:14:45 +0000414 }
415
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000416 case ISD::CALL:
Duraid Madinaa36153a2005-12-22 03:58:17 +0000417 case ISD::TAILCALL: { {
418 // FIXME: This is a workaround for a bug in tblgen.
419 // Pattern #47: (call:Flag (tglobaladdr:i32):$dst, ICC:Flag)
420 // Emits: (CALL:void (tglobaladdr:i32):$dst)
421 // Pattern complexity = 2 cost = 1
422 SDOperand N1 = N->getOperand(1);
423 if (N1.getOpcode() != ISD::TargetGlobalAddress &&
424 N1.getOpcode() != ISD::ExternalSymbol) goto P47Fail;
425 SDOperand InFlag = SDOperand(0, 0);
426 SDOperand Chain = N->getOperand(0);
427 SDOperand Tmp0 = N1;
428 Chain = Select(Chain);
429 SDOperand Result;
430 if (N->getNumOperands() == 3) {
431 InFlag = Select(N->getOperand(2));
432 Result = CurDAG->getTargetNode(IA64::BRCALL, MVT::Other, MVT::Flag, Tmp0,
433 Chain, InFlag);
434 } else {
435 Result = CurDAG->getTargetNode(IA64::BRCALL, MVT::Other, MVT::Flag, Tmp0,
436 Chain);
437 }
438 Chain = CodeGenMap[SDOperand(N, 0)] = Result.getValue(0);
439 CodeGenMap[SDOperand(N, 1)] = Result.getValue(1);
440 return Result.getValue(Op.ResNo);
441 }
442 P47Fail:;
443
444 }
Duraid Madinab6f023a2005-11-21 14:14:54 +0000445
446 case ISD::FDIV:
447 case ISD::SDIV:
448 case ISD::UDIV:
449 case ISD::SREM:
450 case ISD::UREM: return SelectDIV(Op);
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000451
Duraid Madina93856802005-11-02 02:35:04 +0000452 case ISD::ConstantFP: {
Duraid Madina056728f2005-11-02 07:32:59 +0000453 SDOperand Chain = CurDAG->getEntryNode(); // this is a constant, so..
454
Duraid Madina93856802005-11-02 02:35:04 +0000455 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0))
Duraid Madina056728f2005-11-02 07:32:59 +0000456 return CurDAG->getCopyFromReg(Chain, IA64::F0, MVT::f64);
Duraid Madina93856802005-11-02 02:35:04 +0000457 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0))
Duraid Madina056728f2005-11-02 07:32:59 +0000458 return CurDAG->getCopyFromReg(Chain, IA64::F1, MVT::f64);
Duraid Madina93856802005-11-02 02:35:04 +0000459 else
460 assert(0 && "Unexpected FP constant!");
461 }
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000462
463 case ISD::FrameIndex: { // TODO: reduce creepyness
464 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Chris Lattnerb19b8992005-11-30 23:02:08 +0000465 if (N->hasOneUse())
466 return CurDAG->SelectNodeTo(N, IA64::MOV, MVT::i64,
467 CurDAG->getTargetFrameIndex(FI, MVT::i64));
Duraid Madina19e5e142006-01-21 14:27:19 +0000468 else
469 return CodeGenMap[Op] = CurDAG->getTargetNode(IA64::MOV, MVT::i64,
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000470 CurDAG->getTargetFrameIndex(FI, MVT::i64));
471 }
472
Duraid Madina2e0348e2006-01-15 09:45:23 +0000473 case ISD::ConstantPool: { // TODO: nuke the constant pool
474 // (ia64 doesn't need one)
Duraid Madina25d0a882005-10-29 16:08:30 +0000475 Constant *C = cast<ConstantPoolSDNode>(N)->get();
476 SDOperand CPI = CurDAG->getTargetConstantPool(C, MVT::i64);
477 return CurDAG->getTargetNode(IA64::ADDL_GA, MVT::i64, // ?
478 CurDAG->getRegister(IA64::r1, MVT::i64), CPI);
479 }
480
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000481 case ISD::GlobalAddress: {
482 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
483 SDOperand GA = CurDAG->getTargetGlobalAddress(GV, MVT::i64);
484 SDOperand Tmp = CurDAG->getTargetNode(IA64::ADDL_GA, MVT::i64,
485 CurDAG->getRegister(IA64::r1, MVT::i64), GA);
486 return CurDAG->getTargetNode(IA64::LD8, MVT::i64, Tmp);
487 }
Duraid Madinaa36153a2005-12-22 03:58:17 +0000488
489/* XXX case ISD::ExternalSymbol: {
490 SDOperand EA = CurDAG->getTargetExternalSymbol(cast<ExternalSymbolSDNode>(N)->getSymbol(),
491 MVT::i64);
492 SDOperand Tmp = CurDAG->getTargetNode(IA64::ADDL_EA, MVT::i64,
493 CurDAG->getRegister(IA64::r1, MVT::i64), EA);
494 return CurDAG->getTargetNode(IA64::LD8, MVT::i64, Tmp);
495 }
496*/
497
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000498 case ISD::LOAD:
Duraid Madinaecc1a1b2006-01-20 16:10:05 +0000499 case ISD::EXTLOAD: // FIXME: load -1, not 1, for bools?
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000500 case ISD::ZEXTLOAD: {
501 SDOperand Chain = Select(N->getOperand(0));
502 SDOperand Address = Select(N->getOperand(1));
503
504 MVT::ValueType TypeBeingLoaded = (N->getOpcode() == ISD::LOAD) ?
505 N->getValueType(0) : cast<VTSDNode>(N->getOperand(3))->getVT();
506 unsigned Opc;
507 switch (TypeBeingLoaded) {
508 default: N->dump(); assert(0 && "Cannot load this type!");
Duraid Madina9f729062005-11-04 09:59:06 +0000509 case MVT::i1: { // this is a bool
510 Opc = IA64::LD1; // first we load a byte, then compare for != 0
Duraid Madinaa36153a2005-12-22 03:58:17 +0000511 if(N->getValueType(0) == MVT::i1) // XXX: early exit!
512 return CurDAG->SelectNodeTo(N, IA64::CMPNE, MVT::i1, MVT::Other,
Chris Lattnerb19b8992005-11-30 23:02:08 +0000513 CurDAG->getTargetNode(Opc, MVT::i64, Address),
514 CurDAG->getRegister(IA64::r0, MVT::i64),
515 Chain).getValue(Op.ResNo);
Duraid Madinaa36153a2005-12-22 03:58:17 +0000516 /* otherwise, we want to load a bool into something bigger: LD1
517 will do that for us, so we just fall through */
Chris Lattnerb19b8992005-11-30 23:02:08 +0000518 }
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000519 case MVT::i8: Opc = IA64::LD1; break;
520 case MVT::i16: Opc = IA64::LD2; break;
521 case MVT::i32: Opc = IA64::LD4; break;
522 case MVT::i64: Opc = IA64::LD8; break;
523
524 case MVT::f32: Opc = IA64::LDF4; break;
525 case MVT::f64: Opc = IA64::LDF8; break;
526 }
527
Chris Lattnerb19b8992005-11-30 23:02:08 +0000528 // TODO: comment this
529 return CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), MVT::Other,
530 Address, Chain).getValue(Op.ResNo);
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000531 }
532
533 case ISD::TRUNCSTORE:
534 case ISD::STORE: {
535 SDOperand Address = Select(N->getOperand(2));
Duraid Madinad525df32005-11-07 03:11:02 +0000536 SDOperand Chain = Select(N->getOperand(0));
537
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000538 unsigned Opc;
539 if (N->getOpcode() == ISD::STORE) {
540 switch (N->getOperand(1).getValueType()) {
Duraid Madinad525df32005-11-07 03:11:02 +0000541 default: assert(0 && "unknown type in store");
542 case MVT::i1: { // this is a bool
543 Opc = IA64::ST1; // we store either 0 or 1 as a byte
Duraid Madina544cbbd2006-01-13 10:28:25 +0000544 // first load zero!
545 SDOperand Initial = CurDAG->getCopyFromReg(Chain, IA64::r0, MVT::i64);
546 Chain = Initial.getValue(1);
Duraid Madinaa7fb5be2006-01-20 03:40:25 +0000547 // then load 1 into the same reg iff the predicate to store is 1
Chris Lattnerb19b8992005-11-30 23:02:08 +0000548 SDOperand Tmp =
Duraid Madinaa7fb5be2006-01-20 03:40:25 +0000549 CurDAG->getTargetNode(IA64::TPCADDS, MVT::i64, Initial,
Chris Lattnerb19b8992005-11-30 23:02:08 +0000550 CurDAG->getConstant(1, MVT::i64),
551 Select(N->getOperand(1)));
552 return CurDAG->SelectNodeTo(N, Opc, MVT::Other, Address, Tmp, Chain);
553 }
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000554 case MVT::i64: Opc = IA64::ST8; break;
555 case MVT::f64: Opc = IA64::STF8; break;
Duraid Madinad525df32005-11-07 03:11:02 +0000556 }
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000557 } else { //ISD::TRUNCSTORE
558 switch(cast<VTSDNode>(N->getOperand(4))->getVT()) {
Duraid Madinad525df32005-11-07 03:11:02 +0000559 default: assert(0 && "unknown type in truncstore");
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000560 case MVT::i8: Opc = IA64::ST1; break;
561 case MVT::i16: Opc = IA64::ST2; break;
562 case MVT::i32: Opc = IA64::ST4; break;
563 case MVT::f32: Opc = IA64::STF4; break;
564 }
565 }
566
Chris Lattnerb19b8992005-11-30 23:02:08 +0000567 return CurDAG->SelectNodeTo(N, Opc, MVT::Other, Select(N->getOperand(2)),
568 Select(N->getOperand(1)), Chain);
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000569 }
570
571 case ISD::BRCOND: {
572 SDOperand Chain = Select(N->getOperand(0));
573 SDOperand CC = Select(N->getOperand(1));
574 MachineBasicBlock *Dest =
575 cast<BasicBlockSDNode>(N->getOperand(2))->getBasicBlock();
576 //FIXME - we do NOT need long branches all the time
Chris Lattnerb19b8992005-11-30 23:02:08 +0000577 return CurDAG->SelectNodeTo(N, IA64::BRLCOND_NOTCALL, MVT::Other, CC,
578 CurDAG->getBasicBlock(Dest), Chain);
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000579 }
580
581 case ISD::CALLSEQ_START:
582 case ISD::CALLSEQ_END: {
583 int64_t Amt = cast<ConstantSDNode>(N->getOperand(1))->getValue();
584 unsigned Opc = N->getOpcode() == ISD::CALLSEQ_START ?
585 IA64::ADJUSTCALLSTACKDOWN : IA64::ADJUSTCALLSTACKUP;
Chris Lattnerb19b8992005-11-30 23:02:08 +0000586 return CurDAG->SelectNodeTo(N, Opc, MVT::Other,
587 getI64Imm(Amt), Select(N->getOperand(0)));
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000588 }
589
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000590 case ISD::BR:
591 // FIXME: we don't need long branches all the time!
Chris Lattnerb19b8992005-11-30 23:02:08 +0000592 return CurDAG->SelectNodeTo(N, IA64::BRL_NOTCALL, MVT::Other,
593 N->getOperand(1), Select(N->getOperand(0)));
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000594 }
595
596 return SelectCode(Op);
597}
598
599
600/// createIA64DAGToDAGInstructionSelector - This pass converts a legalized DAG
601/// into an IA64-specific DAG, ready for instruction scheduling.
602///
603FunctionPass *llvm::createIA64DAGToDAGInstructionSelector(TargetMachine &TM) {
604 return new IA64DAGToDAGISel(TM);
605}
606