blob: 0b228c1c539c2f15bd6ffe7b968b4c65149c8388 [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"
29using namespace llvm;
30
31namespace {
32 Statistic<> FusedFP ("ia64-codegen", "Number of fused fp operations");
33 Statistic<> FrameOff("ia64-codegen", "Number of frame idx offsets collapsed");
34
35 //===--------------------------------------------------------------------===//
36 /// IA64DAGToDAGISel - IA64 specific code to select IA64 machine
37 /// instructions for SelectionDAG operations.
38 ///
39 class IA64DAGToDAGISel : public SelectionDAGISel {
40 IA64TargetLowering IA64Lowering;
41 unsigned GlobalBaseReg;
42 public:
43 IA64DAGToDAGISel(TargetMachine &TM)
44 : SelectionDAGISel(IA64Lowering), IA64Lowering(TM) {}
45
46 virtual bool runOnFunction(Function &Fn) {
47 // Make sure we re-emit a set of the global base reg if necessary
48 GlobalBaseReg = 0;
49 return SelectionDAGISel::runOnFunction(Fn);
50 }
51
52 /// getI64Imm - Return a target constant with the specified value, of type
53 /// i64.
54 inline SDOperand getI64Imm(uint64_t Imm) {
55 return CurDAG->getTargetConstant(Imm, MVT::i64);
56 }
57
58 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
59 /// base register. Return the virtual register that holds this value.
60 // SDOperand getGlobalBaseReg(); TODO: hmm
61
62 // Select - Convert the specified operand from a target-independent to a
63 // target-specific node if it hasn't already been changed.
64 SDOperand Select(SDOperand Op);
65
66 SDNode *SelectIntImmediateExpr(SDOperand LHS, SDOperand RHS,
67 unsigned OCHi, unsigned OCLo,
68 bool IsArithmetic = false,
69 bool Negate = false);
70 SDNode *SelectBitfieldInsert(SDNode *N);
71
72 /// SelectCC - Select a comparison of the specified values with the
73 /// specified condition code, returning the CR# of the expression.
74 SDOperand SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC);
75
76 /// SelectAddr - Given the specified address, return the two operands for a
77 /// load/store instruction, and return true if it should be an indexed [r+r]
78 /// operation.
79 bool SelectAddr(SDOperand Addr, SDOperand &Op1, SDOperand &Op2);
80
81 SDOperand BuildSDIVSequence(SDNode *N);
82 SDOperand BuildUDIVSequence(SDNode *N);
83
84 /// InstructionSelectBasicBlock - This callback is invoked by
85 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
86 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
87
88 virtual const char *getPassName() const {
89 return "IA64 (Itanium) DAG->DAG Instruction Selector";
90 }
91
92// Include the pieces autogenerated from the target description.
93#include "IA64GenDAGISel.inc"
94
95private:
96 SDOperand SelectCALL(SDOperand Op);
97 };
98}
99
100/// InstructionSelectBasicBlock - This callback is invoked by
101/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
102void IA64DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
103 DEBUG(BB->dump());
104
105 // The selection process is inherently a bottom-up recursive process (users
106 // select their uses before themselves). Given infinite stack space, we
107 // could just start selecting on the root and traverse the whole graph. In
108 // practice however, this causes us to run out of stack space on large basic
109 // blocks. To avoid this problem, select the entry node, then all its uses,
110 // iteratively instead of recursively.
111 std::vector<SDOperand> Worklist;
112 Worklist.push_back(DAG.getEntryNode());
113
114 // Note that we can do this in the IA64 target (scanning forward across token
115 // chain edges) because no nodes ever get folded across these edges. On a
116 // target like X86 which supports load/modify/store operations, this would
117 // have to be more careful.
118 while (!Worklist.empty()) {
119 SDOperand Node = Worklist.back();
120 Worklist.pop_back();
121
122 // Chose from the least deep of the top two nodes.
123 if (!Worklist.empty() &&
124 Worklist.back().Val->getNodeDepth() < Node.Val->getNodeDepth())
125 std::swap(Worklist.back(), Node);
126
127 if ((Node.Val->getOpcode() >= ISD::BUILTIN_OP_END &&
128 Node.Val->getOpcode() < IA64ISD::FIRST_NUMBER) ||
129 CodeGenMap.count(Node)) continue;
130
131 for (SDNode::use_iterator UI = Node.Val->use_begin(),
132 E = Node.Val->use_end(); UI != E; ++UI) {
133 // Scan the values. If this use has a value that is a token chain, add it
134 // to the worklist.
135 SDNode *User = *UI;
136 for (unsigned i = 0, e = User->getNumValues(); i != e; ++i)
137 if (User->getValueType(i) == MVT::Other) {
138 Worklist.push_back(SDOperand(User, i));
139 break;
140 }
141 }
142
143 // Finally, legalize this node.
144 Select(Node);
145 }
146
147 // Select target instructions for the DAG.
148 DAG.setRoot(Select(DAG.getRoot()));
149 CodeGenMap.clear();
150 DAG.RemoveDeadNodes();
151
152 // Emit machine code to BB.
153 ScheduleAndEmitDAG(DAG);
154}
155
156
157SDOperand IA64DAGToDAGISel::SelectCALL(SDOperand Op) {
158 SDNode *N = Op.Val;
159 SDOperand Chain = Select(N->getOperand(0));
160
161 unsigned CallOpcode;
162 std::vector<SDOperand> CallOperands;
163
164 // save the current GP, SP and RP : FIXME: do we need to do all 3 always?
165 SDOperand GPBeforeCall = CurDAG->getCopyFromReg(Chain, IA64::r1, MVT::i64);
166 Chain = GPBeforeCall.getValue(1);
167 SDOperand SPBeforeCall = CurDAG->getCopyFromReg(Chain, IA64::r12, MVT::i64);
168 Chain = SPBeforeCall.getValue(1);
169 SDOperand RPBeforeCall = CurDAG->getCopyFromReg(Chain, IA64::rp, MVT::i64);
170 Chain = RPBeforeCall.getValue(1);
171
172 // if we can call directly, do so
173 if (GlobalAddressSDNode *GASD =
174 dyn_cast<GlobalAddressSDNode>(N->getOperand(1))) {
175 CallOpcode = IA64::BRCALL_IPREL;
176 CallOperands.push_back(CurDAG->getTargetGlobalAddress(GASD->getGlobal(),
177 MVT::i64));
178 } else if (ExternalSymbolSDNode *ESSDN = // FIXME: we currently NEED this
179 // case for correctness, to avoid
180 // "non-pic code with imm reloc.n
181 // against dynamic symbol" errors
182 dyn_cast<ExternalSymbolSDNode>(N->getOperand(1))) {
183 CallOpcode = IA64::BRCALL_IPREL;
184 CallOperands.push_back(N->getOperand(1));
185 } else {
186 // otherwise we need to load the function descriptor,
187 // load the branch target (function)'s entry point and GP,
Duraid Madina764fe712005-11-04 17:55:53 +0000188 // branch (call) then restore the GP
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000189
190 SDOperand FnDescriptor = Select(N->getOperand(1));
191
192 // load the branch target's entry point [mem] and
193 // GP value [mem+8]
Duraid Madina764fe712005-11-04 17:55:53 +0000194 SDOperand targetEntryPoint=CurDAG->getTargetNode(IA64::LD8, MVT::i64,
195 FnDescriptor);
196 Chain = targetEntryPoint.getValue(1);
197 SDOperand targetGPAddr=CurDAG->getTargetNode(IA64::ADDS, MVT::i64,
198 FnDescriptor, CurDAG->getConstant(8, MVT::i64));
199 Chain = targetGPAddr.getValue(1);
200 SDOperand targetGP=CurDAG->getTargetNode(IA64::LD8, MVT::i64,
201 targetGPAddr);
202 Chain = targetGP.getValue(1);
203
Duraid Madina00873eb2005-11-06 13:43:30 +0000204/* FIXME? (methcall still fails)
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000205 SDOperand targetEntryPoint=CurDAG->getLoad(MVT::i64, Chain, FnDescriptor,
206 CurDAG->getSrcValue(0));
207 SDOperand targetGPAddr=CurDAG->getNode(ISD::ADD, MVT::i64, FnDescriptor,
208 CurDAG->getConstant(8, MVT::i64));
209 SDOperand targetGP=CurDAG->getLoad(MVT::i64, Chain, targetGPAddr,
Duraid Madina764fe712005-11-04 17:55:53 +0000210 CurDAG->getSrcValue(0));
211 */
Duraid Madina00873eb2005-11-06 13:43:30 +0000212
213 /* this is just the long way of writing the two lines below?
Duraid Madina764fe712005-11-04 17:55:53 +0000214 // Copy the callee GP into r1
215 SDOperand r1 = CurDAG->getRegister(IA64::r1, MVT::i64);
216 Chain = CurDAG->getNode(ISD::CopyToReg, MVT::i64, Chain, r1,
217 targetGP);
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000218
Duraid Madina764fe712005-11-04 17:55:53 +0000219
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000220 // Copy the callee address into the b6 branch register
221 SDOperand B6 = CurDAG->getRegister(IA64::B6, MVT::i64);
Duraid Madina764fe712005-11-04 17:55:53 +0000222 Chain = CurDAG->getNode(ISD::CopyToReg, MVT::i64, Chain, B6,
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000223 targetEntryPoint);
Duraid Madina00873eb2005-11-06 13:43:30 +0000224 */
225
226 Chain = CurDAG->getCopyToReg(Chain, IA64::r1, targetGP);
227 Chain = CurDAG->getCopyToReg(Chain, IA64::B6, targetEntryPoint);
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000228
Duraid Madina00873eb2005-11-06 13:43:30 +0000229 CallOperands.push_back(CurDAG->getRegister(IA64::B6, MVT::i64));
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000230 CallOpcode = IA64::BRCALL_INDIRECT;
231 }
232
Duraid Madinadc9549b2005-11-01 05:46:16 +0000233 // see section 8.5.8 of "Itanium Software Conventions and
234 // Runtime Architecture Guide to see some examples of what's going
235 // on here. (in short: int args get mapped 1:1 'slot-wise' to out0->out7,
236 // while FP args get mapped to F8->F15 as needed)
237
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000238 // TODO: support in-memory arguments
Duraid Madinadc9549b2005-11-01 05:46:16 +0000239
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000240 unsigned used_FPArgs=0; // how many FP args have been used so far?
241
242 unsigned intArgs[] = {IA64::out0, IA64::out1, IA64::out2, IA64::out3,
243 IA64::out4, IA64::out5, IA64::out6, IA64::out7 };
244 unsigned FPArgs[] = {IA64::F8, IA64::F9, IA64::F10, IA64::F11,
245 IA64::F12, IA64::F13, IA64::F14, IA64::F15 };
246
247 SDOperand InFlag; // Null incoming flag value.
248
249 for (unsigned i = 2, e = N->getNumOperands(); i != e; ++i) {
250 unsigned DestReg = 0;
251 MVT::ValueType RegTy = N->getOperand(i).getValueType();
252 if (RegTy == MVT::i64) {
253 assert((i-2) < 8 && "Too many int args");
254 DestReg = intArgs[i-2];
255 } else {
256 assert(MVT::isFloatingPoint(N->getOperand(i).getValueType()) &&
257 "Unpromoted integer arg?");
258 assert(used_FPArgs < 8 && "Too many fp args");
259 DestReg = FPArgs[used_FPArgs++];
260 }
261
262 if (N->getOperand(i).getOpcode() != ISD::UNDEF) {
263 SDOperand Val = Select(N->getOperand(i));
Duraid Madina00873eb2005-11-06 13:43:30 +0000264 if(MVT::isInteger(N->getOperand(i).getValueType())) {
265 Chain = CurDAG->getCopyToReg(Chain, DestReg, Val, InFlag);
266 InFlag = Chain.getValue(1);
267 CallOperands.push_back(CurDAG->getRegister(DestReg, RegTy));
268 }
Duraid Madinadc9549b2005-11-01 05:46:16 +0000269 // some functions (e.g. printf) want floating point arguments
270 // *also* passed as in-memory representations in integer registers
271 // this is FORTRAN legacy junk which we don't _always_ need
272 // to do, but to be on the safe side, we do.
Duraid Madina00873eb2005-11-06 13:43:30 +0000273 else if(MVT::isFloatingPoint(N->getOperand(i).getValueType())) {
Duraid Madinadc9549b2005-11-01 05:46:16 +0000274 assert((i-2) < 8 && "FP args alone would fit, but no int regs left");
Duraid Madina00873eb2005-11-06 13:43:30 +0000275 // first copy into the appropriate FP reg
276 Chain = CurDAG->getCopyToReg(Chain, DestReg, Val);
277 // then copy into the appropriate integer reg
278 DestReg = intArgs[i-2];
Duraid Madinadc9549b2005-11-01 05:46:16 +0000279 // GETFD takes an FP reg and writes a GP reg
Duraid Madina00873eb2005-11-06 13:43:30 +0000280 Chain = CurDAG->getTargetNode(IA64::GETFD, MVT::i64, Val);
Duraid Madinadc9549b2005-11-01 05:46:16 +0000281 // FIXME: this next line is a bit unfortunate
282 Chain = CurDAG->getCopyToReg(Chain, DestReg, Chain, InFlag);
283 InFlag = Chain.getValue(1);
284 CallOperands.push_back(CurDAG->getRegister(DestReg, MVT::i64));
285 }
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000286 }
287 }
288
289 // Finally, once everything is in registers to pass to the call, emit the
290 // call itself.
291 if (InFlag.Val)
292 CallOperands.push_back(InFlag); // Strong dep on register copies.
293 else
294 CallOperands.push_back(Chain); // Weak dep on whatever occurs before
295 Chain = CurDAG->getTargetNode(CallOpcode, MVT::Other, MVT::Flag,
296 CallOperands);
297
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000298 std::vector<SDOperand> CallResults;
299
300 // If the call has results, copy the values out of the ret val registers.
301 switch (N->getValueType(0)) {
302 default: assert(0 && "Unexpected ret value!");
303 case MVT::Other: break;
304 case MVT::i64:
305 Chain = CurDAG->getCopyFromReg(Chain, IA64::r8, MVT::i64,
306 Chain.getValue(1)).getValue(1);
307 CallResults.push_back(Chain.getValue(0));
308 break;
309 case MVT::f64:
310 Chain = CurDAG->getCopyFromReg(Chain, IA64::F8, N->getValueType(0),
311 Chain.getValue(1)).getValue(1);
312 CallResults.push_back(Chain.getValue(0));
313 break;
314 }
Duraid Madina00873eb2005-11-06 13:43:30 +0000315
316 // restore GP, SP and RP - FIXME: this doesn't quite work (e.g.
317 // methcall / objinst both segfault on exit) and it *really*
318 // doesn't work unless you have -sched=none
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000319 Chain = CurDAG->getCopyToReg(Chain, IA64::r1, GPBeforeCall);
320 Chain = CurDAG->getCopyToReg(Chain, IA64::r12, SPBeforeCall);
321 Chain = CurDAG->getCopyToReg(Chain, IA64::rp, RPBeforeCall);
Duraid Madina00873eb2005-11-06 13:43:30 +0000322 CallResults.push_back(Chain); // llc segfaults w/o this,
323 // ary3(e.g.) SIGILLs with 3
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000324
325 for (unsigned i = 0, e = CallResults.size(); i != e; ++i)
326 CodeGenMap[Op.getValue(i)] = CallResults[i];
327
328 return CallResults[Op.ResNo];
329}
330
331// Select - Convert the specified operand from a target-independent to a
332// target-specific node if it hasn't already been changed.
333SDOperand IA64DAGToDAGISel::Select(SDOperand Op) {
334 SDNode *N = Op.Val;
335 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
336 N->getOpcode() < IA64ISD::FIRST_NUMBER)
337 return Op; // Already selected.
338
339 // If this has already been converted, use it.
340 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(Op);
341 if (CGMI != CodeGenMap.end()) return CGMI->second;
342
343 switch (N->getOpcode()) {
344 default: break;
345
346 case ISD::CALL:
347 case ISD::TAILCALL: return SelectCALL(Op);
348
349/* todo:
350 * case ISD::DYNAMIC_STACKALLOC:
351*/
Duraid Madina93856802005-11-02 02:35:04 +0000352 case ISD::ConstantFP: {
Duraid Madina056728f2005-11-02 07:32:59 +0000353 SDOperand Chain = CurDAG->getEntryNode(); // this is a constant, so..
354
Duraid Madina93856802005-11-02 02:35:04 +0000355 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0))
Duraid Madina056728f2005-11-02 07:32:59 +0000356 return CurDAG->getCopyFromReg(Chain, IA64::F0, MVT::f64);
Duraid Madina93856802005-11-02 02:35:04 +0000357 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0))
Duraid Madina056728f2005-11-02 07:32:59 +0000358 return CurDAG->getCopyFromReg(Chain, IA64::F1, MVT::f64);
Duraid Madina93856802005-11-02 02:35:04 +0000359 else
360 assert(0 && "Unexpected FP constant!");
361 }
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000362
363 case ISD::FrameIndex: { // TODO: reduce creepyness
364 int FI = cast<FrameIndexSDNode>(N)->getIndex();
365 if (N->hasOneUse()) {
366 CurDAG->SelectNodeTo(N, IA64::MOV, MVT::i64,
367 CurDAG->getTargetFrameIndex(FI, MVT::i64));
368 return SDOperand(N, 0);
369 }
370 return CurDAG->getTargetNode(IA64::MOV, MVT::i64,
371 CurDAG->getTargetFrameIndex(FI, MVT::i64));
372 }
373
Duraid Madina25d0a882005-10-29 16:08:30 +0000374 case ISD::ConstantPool: {
375 Constant *C = cast<ConstantPoolSDNode>(N)->get();
376 SDOperand CPI = CurDAG->getTargetConstantPool(C, MVT::i64);
377 return CurDAG->getTargetNode(IA64::ADDL_GA, MVT::i64, // ?
378 CurDAG->getRegister(IA64::r1, MVT::i64), CPI);
379 }
380
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000381 case ISD::GlobalAddress: {
382 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
383 SDOperand GA = CurDAG->getTargetGlobalAddress(GV, MVT::i64);
384 SDOperand Tmp = CurDAG->getTargetNode(IA64::ADDL_GA, MVT::i64,
385 CurDAG->getRegister(IA64::r1, MVT::i64), GA);
386 return CurDAG->getTargetNode(IA64::LD8, MVT::i64, Tmp);
387 }
388
389 case ISD::LOAD:
390 case ISD::EXTLOAD:
391 case ISD::ZEXTLOAD: {
392 SDOperand Chain = Select(N->getOperand(0));
393 SDOperand Address = Select(N->getOperand(1));
394
395 MVT::ValueType TypeBeingLoaded = (N->getOpcode() == ISD::LOAD) ?
396 N->getValueType(0) : cast<VTSDNode>(N->getOperand(3))->getVT();
397 unsigned Opc;
398 switch (TypeBeingLoaded) {
399 default: N->dump(); assert(0 && "Cannot load this type!");
Duraid Madina9f729062005-11-04 09:59:06 +0000400 case MVT::i1: { // this is a bool
401 Opc = IA64::LD1; // first we load a byte, then compare for != 0
402 CurDAG->SelectNodeTo(N, IA64::CMPNE, MVT::i1, MVT::Other,
403 CurDAG->getTargetNode(Opc, MVT::i64, Address),
404 CurDAG->getRegister(IA64::r0, MVT::i64), Chain);
405 return SDOperand(N, Op.ResNo); // XXX: early exit
406 }
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000407 case MVT::i8: Opc = IA64::LD1; break;
408 case MVT::i16: Opc = IA64::LD2; break;
409 case MVT::i32: Opc = IA64::LD4; break;
410 case MVT::i64: Opc = IA64::LD8; break;
411
412 case MVT::f32: Opc = IA64::LDF4; break;
413 case MVT::f64: Opc = IA64::LDF8; break;
414 }
415
416 CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), MVT::Other,
417 Address, Chain); // TODO: comment this
418
419 return SDOperand(N, Op.ResNo);
420 }
421
422 case ISD::TRUNCSTORE:
423 case ISD::STORE: {
424 SDOperand Address = Select(N->getOperand(2));
425
426 unsigned Opc;
427 if (N->getOpcode() == ISD::STORE) {
428 switch (N->getOperand(1).getValueType()) {
429 default: assert(0 && "unknown Type in store");
430 case MVT::i64: Opc = IA64::ST8; break;
431 case MVT::f64: Opc = IA64::STF8; break;
432 }
433 } else { //ISD::TRUNCSTORE
434 switch(cast<VTSDNode>(N->getOperand(4))->getVT()) {
435 default: assert(0 && "unknown Type in store");
436 case MVT::i8: Opc = IA64::ST1; break;
437 case MVT::i16: Opc = IA64::ST2; break;
438 case MVT::i32: Opc = IA64::ST4; break;
439 case MVT::f32: Opc = IA64::STF4; break;
440 }
441 }
442
443 CurDAG->SelectNodeTo(N, Opc, MVT::Other, Select(N->getOperand(2)),
444 Select(N->getOperand(1)), Select(N->getOperand(0)));
445 return SDOperand(N, 0);
446 }
447
448 case ISD::BRCOND: {
449 SDOperand Chain = Select(N->getOperand(0));
450 SDOperand CC = Select(N->getOperand(1));
451 MachineBasicBlock *Dest =
452 cast<BasicBlockSDNode>(N->getOperand(2))->getBasicBlock();
453 //FIXME - we do NOT need long branches all the time
454 CurDAG->SelectNodeTo(N, IA64::BRLCOND_NOTCALL, MVT::Other, CC, CurDAG->getBasicBlock(Dest), Chain);
455 return SDOperand(N, 0);
456 }
457
458 case ISD::CALLSEQ_START:
459 case ISD::CALLSEQ_END: {
460 int64_t Amt = cast<ConstantSDNode>(N->getOperand(1))->getValue();
461 unsigned Opc = N->getOpcode() == ISD::CALLSEQ_START ?
462 IA64::ADJUSTCALLSTACKDOWN : IA64::ADJUSTCALLSTACKUP;
463 CurDAG->SelectNodeTo(N, Opc, MVT::Other,
464 getI64Imm(Amt), Select(N->getOperand(0)));
465 return SDOperand(N, 0);
466 }
467
468 case ISD::RET: {
469 SDOperand Chain = Select(N->getOperand(0)); // Token chain.
470
471 switch (N->getNumOperands()) {
472 default:
473 assert(0 && "Unknown return instruction!");
474 case 2: {
475 SDOperand RetVal = Select(N->getOperand(1));
476 switch (RetVal.getValueType()) {
477 default: assert(0 && "I don't know how to return this type! (promote?)");
478 // FIXME: do I need to add support for bools here?
479 // (return '0' or '1' in r8, basically...)
480 //
481 // FIXME: need to round floats - 80 bits is bad, the tester
482 // told me so
483 case MVT::i64:
484 // we mark r8 as live on exit up above in LowerArguments()
485 // BuildMI(BB, IA64::MOV, 1, IA64::r8).addReg(Tmp1);
486 Chain = CurDAG->getCopyToReg(Chain, IA64::r8, RetVal);
487 break;
488 case MVT::f64:
489 // we mark F8 as live on exit up above in LowerArguments()
490 // BuildMI(BB, IA64::FMOV, 1, IA64::F8).addReg(Tmp1);
491 Chain = CurDAG->getCopyToReg(Chain, IA64::F8, RetVal);
492 break;
493 }
494 break;
495 }
496 case 1:
497 break;
498 }
499
500 // we need to copy VirtGPR (the vreg (to become a real reg)) that holds
501 // the output of this function's alloc instruction back into ar.pfs
502 // before we return. this copy must not float up above the last
503 // outgoing call in this function!!!
504 SDOperand AR_PFSVal = CurDAG->getCopyFromReg(Chain, IA64Lowering.VirtGPR,
505 MVT::i64);
506 Chain = AR_PFSVal.getValue(1);
507 Chain = CurDAG->getCopyToReg(Chain, IA64::AR_PFS, AR_PFSVal);
508
509 CurDAG->SelectNodeTo(N, IA64::RET, MVT::Other, Chain); // and then just emit a 'ret' instruction
510
511 // before returning, restore the ar.pfs register (set by the 'alloc' up top)
512 // BuildMI(BB, IA64::MOV, 1).addReg(IA64::AR_PFS).addReg(IA64Lowering.VirtGPR);
513 //
514 return SDOperand(N, 0);
515 }
516
517 case ISD::BR:
518 // FIXME: we don't need long branches all the time!
519 CurDAG->SelectNodeTo(N, IA64::BRL_NOTCALL, MVT::Other, N->getOperand(1),
520 Select(N->getOperand(0)));
521 return SDOperand(N, 0);
522
523 }
524
525 return SelectCode(Op);
526}
527
528
529/// createIA64DAGToDAGInstructionSelector - This pass converts a legalized DAG
530/// into an IA64-specific DAG, ready for instruction scheduling.
531///
532FunctionPass *llvm::createIA64DAGToDAGInstructionSelector(TargetMachine &TM) {
533 return new IA64DAGToDAGISel(TM);
534}
535