blob: 929e8f5af6d7100e1ae600c54e895521a69e0254 [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:
Duraid Madinab6f023a2005-11-21 14:14:54 +000096 SDOperand SelectDIV(SDOperand Op);
Duraid Madinaf2db9b82005-10-28 17:46:35 +000097 };
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
Duraid Madinab6f023a2005-11-21 14:14:54 +0000156SDOperand IA64DAGToDAGISel::SelectDIV(SDOperand Op) {
157 SDNode *N = Op.Val;
158 SDOperand Chain = Select(N->getOperand(0));
159
160 SDOperand Tmp1 = Select(N->getOperand(0));
161 SDOperand Tmp2 = Select(N->getOperand(1));
162
163 bool isFP=false;
164
165 if(MVT::isFloatingPoint(Tmp1.getValueType()))
166 isFP=true;
167
168 bool isModulus=false; // is it a division or a modulus?
169 bool isSigned=false;
170
171 switch(N->getOpcode()) {
172 case ISD::FDIV:
173 case ISD::SDIV: isModulus=false; isSigned=true; break;
174 case ISD::UDIV: isModulus=false; isSigned=false; break;
175 case ISD::FREM:
176 case ISD::SREM: isModulus=true; isSigned=true; break;
177 case ISD::UREM: isModulus=true; isSigned=false; break;
178 }
179
180 // TODO: check for integer divides by powers of 2 (or other simple patterns?)
181
182 SDOperand TmpPR, TmpPR2;
183 SDOperand TmpF1, TmpF2, TmpF3, TmpF4, TmpF5, TmpF6, TmpF7, TmpF8;
184 SDOperand TmpF9, TmpF10,TmpF11,TmpF12,TmpF13,TmpF14,TmpF15;
185 SDOperand Result;
186
187 // OK, emit some code:
188
189 if(!isFP) {
190 // first, load the inputs into FP regs.
191 TmpF1 = CurDAG->getTargetNode(IA64::SETFSIG, MVT::f64, Tmp1);
192 Chain = TmpF1.getValue(1);
193 TmpF2 = CurDAG->getTargetNode(IA64::SETFSIG, MVT::f64, Tmp2);
194 Chain = TmpF2.getValue(1);
195
196 // next, convert the inputs to FP
197 if(isSigned) {
198 TmpF3 = CurDAG->getTargetNode(IA64::FCVTXF, MVT::f64, TmpF1);
199 Chain = TmpF3.getValue(1);
200 TmpF4 = CurDAG->getTargetNode(IA64::FCVTXF, MVT::f64, TmpF2);
201 Chain = TmpF4.getValue(1);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000202 } else { // is unsigned
203 if(isModulus) { /* unsigned integer divides do not need any fcvt.x*f* insns */
204 TmpF3 = CurDAG->getTargetNode(IA64::FCVTXUFS1, MVT::f64, TmpF1);
205 Chain = TmpF3.getValue(1);
206 TmpF4 = CurDAG->getTargetNode(IA64::FCVTXUFS1, MVT::f64, TmpF2);
207 Chain = TmpF4.getValue(1);
208 }
Duraid Madinab6f023a2005-11-21 14:14:54 +0000209 }
210
211 } else { // this is an FP divide/remainder, so we 'leak' some temp
212 // regs and assign TmpF3=Tmp1, TmpF4=Tmp2
213 TmpF3=Tmp1;
214 TmpF4=Tmp2;
215 }
216
217 // we start by computing an approximate reciprocal (good to 9 bits?)
218 // note, this instruction writes _both_ TmpF5 (answer) and TmpPR (predicate)
Duraid Madina0c81dc82006-01-16 06:33:38 +0000219 if(isFP)
220 TmpF5 = CurDAG->getTargetNode(IA64::FRCPAS0, MVT::f64, MVT::i1,
Duraid Madinab6f023a2005-11-21 14:14:54 +0000221 TmpF3, TmpF4);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000222 else
223 TmpF5 = CurDAG->getTargetNode(IA64::FRCPAS1, MVT::f64, MVT::i1,
224 TmpF3, TmpF4);
225
Duraid Madinab6f023a2005-11-21 14:14:54 +0000226 TmpPR = TmpF5.getValue(1);
227 Chain = TmpF5.getValue(2);
228
Duraid Madina0c81dc82006-01-16 06:33:38 +0000229 // we'll need copies of F0 and F1
Duraid Madinab6f023a2005-11-21 14:14:54 +0000230 SDOperand F0 = CurDAG->getRegister(IA64::F0, MVT::f64);
231 SDOperand F1 = CurDAG->getRegister(IA64::F1, MVT::f64);
232
Duraid Madina0c81dc82006-01-16 06:33:38 +0000233 SDOperand minusB;
234 if(isModulus) { // for remainders, it'll be handy to have
235 // copies of -input_b
236 minusB = CurDAG->getTargetNode(IA64::SUB, MVT::i64,
237 CurDAG->getRegister(IA64::r0, MVT::i64), Tmp2);
238 Chain = minusB.getValue(1);
239 }
240
241 SDOperand TmpE0, TmpY1, TmpE1, TmpY2;
242
243 TmpE0 = CurDAG->getTargetNode(IA64::CFNMAS1, MVT::f64,
Duraid Madinab6f023a2005-11-21 14:14:54 +0000244 TmpF4, TmpF5, F1, TmpPR);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000245 Chain = TmpE0.getValue(1);
246 TmpY1 = CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
247 TmpF5, TmpE0, TmpF5, TmpPR);
248 Chain = TmpY1.getValue(1);
249 TmpE1 = CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
250 TmpE0, TmpE0, F0, TmpPR);
251 Chain = TmpE1.getValue(1);
252 TmpY2 = CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
253 TmpY1, TmpE1, TmpY1, TmpPR);
254 Chain = TmpY2.getValue(1);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000255
Duraid Madina0c81dc82006-01-16 06:33:38 +0000256 if(isFP) { // if this is an FP divide, we finish up here and exit early
257 if(isModulus)
258 assert(0 && "Sorry, try another FORTRAN compiler.");
259
260 SDOperand TmpE2, TmpY3, TmpQ0, TmpR0;
261
262 TmpE2 = CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
263 TmpE1, TmpE1, F0, TmpPR);
264 Chain = TmpE2.getValue(1);
265 TmpY3 = CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
266 TmpY2, TmpE2, TmpY2, TmpPR);
267 Chain = TmpY3.getValue(1);
268 TmpQ0 = CurDAG->getTargetNode(IA64::CFMADS1, MVT::f64, // double prec!
269 Tmp1, TmpY3, F0, TmpPR);
270 Chain = TmpQ0.getValue(1);
271 TmpR0 = CurDAG->getTargetNode(IA64::CFNMADS1, MVT::f64, // double prec!
272 Tmp2, TmpQ0, Tmp1, TmpPR);
273 Chain = TmpR0.getValue(1);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000274
Duraid Madina0c81dc82006-01-16 06:33:38 +0000275// we want Result to have the same target register as the frcpa, so
276// we two-address hack it. See the comment "for this to work..." on
277// page 48 of Intel application note #245415
278 Result = CurDAG->getTargetNode(IA64::TCFMADS0, MVT::f64, // d.p. s0 rndg!
279 TmpY3, TmpR0, TmpQ0, TmpPR);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000280 Chain = Result.getValue(1);
Duraid Madina0c81dc82006-01-16 06:33:38 +0000281 return Result; // XXX: early exit!
282 } else { // this is *not* an FP divide, so there's a bit left to do:
283
284 SDOperand TmpQ2, TmpR2, TmpQ3, TmpQ;
285
286 TmpQ2 = CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
287 TmpF3, TmpY2, F0, TmpPR);
288 Chain = TmpQ2.getValue(1);
289 TmpR2 = CurDAG->getTargetNode(IA64::CFNMAS1, MVT::f64,
290 TmpF4, TmpQ2, TmpF3, TmpPR);
291 Chain = TmpR2.getValue(1);
Duraid Madinab6f023a2005-11-21 14:14:54 +0000292
Duraid Madina0c81dc82006-01-16 06:33:38 +0000293// we want TmpQ3 to have the same target register as the frcpa, so
294// we two-address hack it. See the comment "for this to work..." on
295// page 48 of Intel application note #245415
296 TmpQ3 = CurDAG->getTargetNode(IA64::TCFMAS1, MVT::f64,
297 TmpR2, TmpR2, TmpY2, TmpQ2, TmpPR);
298 Chain = TmpQ3.getValue(1);
299
300 if(isSigned)
301 TmpQ = CurDAG->getTargetNode(IA64::FCVTFXTRUNCS1, MVT::f64, TmpQ3);
302 else
303 TmpQ = CurDAG->getTargetNode(IA64::FCVTFXUTRUNCS1, MVT::f64, TmpQ3);
304
305 Chain = TmpQ.getValue(1);
306
307 if(isModulus) {
308 SDOperand FPminusB = CurDAG->getTargetNode(IA64::SETFSIG, MVT::f64,
309 minusB);
310 Chain = FPminusB.getValue(1);
311 SDOperand Remainder = CurDAG->getTargetNode(IA64::XMAL, MVT::f64,
312 TmpQ, FPminusB, TmpF1);
313 Chain = Remainder.getValue(1);
314 Result = CurDAG->getTargetNode(IA64::GETFSIG, MVT::i64, Remainder);
315 Chain = Result.getValue(1);
316 } else { // just an integer divide
317 Result = CurDAG->getTargetNode(IA64::GETFSIG, MVT::i64, TmpQ);
318 Chain = Result.getValue(1);
319 }
320
321 return Result;
322 } // wasn't an FP divide
Duraid Madinab6f023a2005-11-21 14:14:54 +0000323}
324
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000325// Select - Convert the specified operand from a target-independent to a
326// target-specific node if it hasn't already been changed.
327SDOperand IA64DAGToDAGISel::Select(SDOperand Op) {
328 SDNode *N = Op.Val;
329 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
330 N->getOpcode() < IA64ISD::FIRST_NUMBER)
331 return Op; // Already selected.
332
333 // If this has already been converted, use it.
334 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(Op);
335 if (CGMI != CodeGenMap.end()) return CGMI->second;
336
337 switch (N->getOpcode()) {
338 default: break;
339
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000340 case IA64ISD::BRCALL: { // XXX: this is also a hack!
341 SDOperand Chain = Select(N->getOperand(0));
342 SDOperand InFlag; // Null incoming flag value.
343
344 if(N->getNumOperands()==3) // we have an incoming chain, callee and flag
345 InFlag = Select(N->getOperand(2));
346
347 unsigned CallOpcode;
348 SDOperand CallOperand;
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000349
350 // if we can call directly, do so
351 if (GlobalAddressSDNode *GASD =
352 dyn_cast<GlobalAddressSDNode>(N->getOperand(1))) {
353 CallOpcode = IA64::BRCALL_IPREL_GA;
354 CallOperand = CurDAG->getTargetGlobalAddress(GASD->getGlobal(), MVT::i64);
355 } else if (ExternalSymbolSDNode *ESSDN = // FIXME: we currently NEED this
356 // case for correctness, to avoid
357 // "non-pic code with imm reloc.n
358 // against dynamic symbol" errors
359 dyn_cast<ExternalSymbolSDNode>(N->getOperand(1))) {
360 CallOpcode = IA64::BRCALL_IPREL_ES;
361 CallOperand = N->getOperand(1);
362 } else {
363 // otherwise we need to load the function descriptor,
364 // load the branch target (function)'s entry point and GP,
365 // branch (call) then restore the GP
366 SDOperand FnDescriptor = Select(N->getOperand(1));
367
368 // load the branch target's entry point [mem] and
369 // GP value [mem+8]
370 SDOperand targetEntryPoint=CurDAG->getTargetNode(IA64::LD8, MVT::i64,
371 FnDescriptor);
372 Chain = targetEntryPoint.getValue(1);
373 SDOperand targetGPAddr=CurDAG->getTargetNode(IA64::ADDS, MVT::i64,
374 FnDescriptor, CurDAG->getConstant(8, MVT::i64));
375 Chain = targetGPAddr.getValue(1);
376 SDOperand targetGP=CurDAG->getTargetNode(IA64::LD8, MVT::i64,
377 targetGPAddr);
378 Chain = targetGP.getValue(1);
379
380 Chain = CurDAG->getCopyToReg(Chain, IA64::r1, targetGP, InFlag);
381 InFlag = Chain.getValue(1);
382 Chain = CurDAG->getCopyToReg(Chain, IA64::B6, targetEntryPoint, InFlag); // FLAG these?
383 InFlag = Chain.getValue(1);
384
385 CallOperand = CurDAG->getRegister(IA64::B6, MVT::i64);
386 CallOpcode = IA64::BRCALL_INDIRECT;
387 }
388
389 // Finally, once everything is setup, emit the call itself
390 if(InFlag.Val)
Duraid Madinab13d74a2005-12-25 14:09:08 +0000391 Chain = CurDAG->getTargetNode(CallOpcode, MVT::Other, MVT::Flag, CallOperand, InFlag);
Duraid Madina64aa0ea2005-12-22 13:29:14 +0000392 else // there might be no arguments
393 Chain = CurDAG->getTargetNode(CallOpcode, MVT::Other, MVT::Flag, CallOperand, Chain);
394 InFlag = Chain.getValue(1);
395
396 std::vector<SDOperand> CallResults;
397
398 CallResults.push_back(Chain);
399 CallResults.push_back(InFlag);
400
401 for (unsigned i = 0, e = CallResults.size(); i != e; ++i)
402 CodeGenMap[Op.getValue(i)] = CallResults[i];
403 return CallResults[Op.ResNo];
404 }
Duraid Madinaa36153a2005-12-22 03:58:17 +0000405
Duraid Madina8617f3c2005-12-22 07:14:45 +0000406 case IA64ISD::GETFD: {
407 SDOperand Input = Select(N->getOperand(0));
Duraid Madinabf094582006-01-11 03:50:40 +0000408 SDOperand Result = CurDAG->getTargetNode(IA64::GETFD, MVT::i64, Input);
409 CodeGenMap[Op] = Result;
410 return Result;
Duraid Madina8617f3c2005-12-22 07:14:45 +0000411 }
412
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000413 case ISD::CALL:
Duraid Madinaa36153a2005-12-22 03:58:17 +0000414 case ISD::TAILCALL: { {
415 // FIXME: This is a workaround for a bug in tblgen.
416 // Pattern #47: (call:Flag (tglobaladdr:i32):$dst, ICC:Flag)
417 // Emits: (CALL:void (tglobaladdr:i32):$dst)
418 // Pattern complexity = 2 cost = 1
419 SDOperand N1 = N->getOperand(1);
420 if (N1.getOpcode() != ISD::TargetGlobalAddress &&
421 N1.getOpcode() != ISD::ExternalSymbol) goto P47Fail;
422 SDOperand InFlag = SDOperand(0, 0);
423 SDOperand Chain = N->getOperand(0);
424 SDOperand Tmp0 = N1;
425 Chain = Select(Chain);
426 SDOperand Result;
427 if (N->getNumOperands() == 3) {
428 InFlag = Select(N->getOperand(2));
429 Result = CurDAG->getTargetNode(IA64::BRCALL, MVT::Other, MVT::Flag, Tmp0,
430 Chain, InFlag);
431 } else {
432 Result = CurDAG->getTargetNode(IA64::BRCALL, MVT::Other, MVT::Flag, Tmp0,
433 Chain);
434 }
435 Chain = CodeGenMap[SDOperand(N, 0)] = Result.getValue(0);
436 CodeGenMap[SDOperand(N, 1)] = Result.getValue(1);
437 return Result.getValue(Op.ResNo);
438 }
439 P47Fail:;
440
441 }
Duraid Madinab6f023a2005-11-21 14:14:54 +0000442
443 case ISD::FDIV:
444 case ISD::SDIV:
445 case ISD::UDIV:
446 case ISD::SREM:
447 case ISD::UREM: return SelectDIV(Op);
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000448
Duraid Madina93856802005-11-02 02:35:04 +0000449 case ISD::ConstantFP: {
Duraid Madina056728f2005-11-02 07:32:59 +0000450 SDOperand Chain = CurDAG->getEntryNode(); // this is a constant, so..
451
Duraid Madina93856802005-11-02 02:35:04 +0000452 if (cast<ConstantFPSDNode>(N)->isExactlyValue(+0.0))
Duraid Madina056728f2005-11-02 07:32:59 +0000453 return CurDAG->getCopyFromReg(Chain, IA64::F0, MVT::f64);
Duraid Madina93856802005-11-02 02:35:04 +0000454 else if (cast<ConstantFPSDNode>(N)->isExactlyValue(+1.0))
Duraid Madina056728f2005-11-02 07:32:59 +0000455 return CurDAG->getCopyFromReg(Chain, IA64::F1, MVT::f64);
Duraid Madina93856802005-11-02 02:35:04 +0000456 else
457 assert(0 && "Unexpected FP constant!");
458 }
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000459
460 case ISD::FrameIndex: { // TODO: reduce creepyness
461 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Chris Lattnerb19b8992005-11-30 23:02:08 +0000462 if (N->hasOneUse())
463 return CurDAG->SelectNodeTo(N, IA64::MOV, MVT::i64,
464 CurDAG->getTargetFrameIndex(FI, MVT::i64));
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000465 return CurDAG->getTargetNode(IA64::MOV, MVT::i64,
466 CurDAG->getTargetFrameIndex(FI, MVT::i64));
467 }
468
Duraid Madina2e0348e2006-01-15 09:45:23 +0000469 case ISD::ConstantPool: { // TODO: nuke the constant pool
470 // (ia64 doesn't need one)
Duraid Madina25d0a882005-10-29 16:08:30 +0000471 Constant *C = cast<ConstantPoolSDNode>(N)->get();
472 SDOperand CPI = CurDAG->getTargetConstantPool(C, MVT::i64);
473 return CurDAG->getTargetNode(IA64::ADDL_GA, MVT::i64, // ?
474 CurDAG->getRegister(IA64::r1, MVT::i64), CPI);
475 }
476
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000477 case ISD::GlobalAddress: {
478 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
479 SDOperand GA = CurDAG->getTargetGlobalAddress(GV, MVT::i64);
480 SDOperand Tmp = CurDAG->getTargetNode(IA64::ADDL_GA, MVT::i64,
481 CurDAG->getRegister(IA64::r1, MVT::i64), GA);
482 return CurDAG->getTargetNode(IA64::LD8, MVT::i64, Tmp);
483 }
Duraid Madinaa36153a2005-12-22 03:58:17 +0000484
485/* XXX case ISD::ExternalSymbol: {
486 SDOperand EA = CurDAG->getTargetExternalSymbol(cast<ExternalSymbolSDNode>(N)->getSymbol(),
487 MVT::i64);
488 SDOperand Tmp = CurDAG->getTargetNode(IA64::ADDL_EA, MVT::i64,
489 CurDAG->getRegister(IA64::r1, MVT::i64), EA);
490 return CurDAG->getTargetNode(IA64::LD8, MVT::i64, Tmp);
491 }
492*/
493
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000494 case ISD::LOAD:
495 case ISD::EXTLOAD:
496 case ISD::ZEXTLOAD: {
497 SDOperand Chain = Select(N->getOperand(0));
498 SDOperand Address = Select(N->getOperand(1));
499
500 MVT::ValueType TypeBeingLoaded = (N->getOpcode() == ISD::LOAD) ?
501 N->getValueType(0) : cast<VTSDNode>(N->getOperand(3))->getVT();
502 unsigned Opc;
503 switch (TypeBeingLoaded) {
504 default: N->dump(); assert(0 && "Cannot load this type!");
Duraid Madina9f729062005-11-04 09:59:06 +0000505 case MVT::i1: { // this is a bool
506 Opc = IA64::LD1; // first we load a byte, then compare for != 0
Duraid Madinaa36153a2005-12-22 03:58:17 +0000507 if(N->getValueType(0) == MVT::i1) // XXX: early exit!
508 return CurDAG->SelectNodeTo(N, IA64::CMPNE, MVT::i1, MVT::Other,
Chris Lattnerb19b8992005-11-30 23:02:08 +0000509 CurDAG->getTargetNode(Opc, MVT::i64, Address),
510 CurDAG->getRegister(IA64::r0, MVT::i64),
511 Chain).getValue(Op.ResNo);
Duraid Madinaa36153a2005-12-22 03:58:17 +0000512 /* otherwise, we want to load a bool into something bigger: LD1
513 will do that for us, so we just fall through */
Chris Lattnerb19b8992005-11-30 23:02:08 +0000514 }
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000515 case MVT::i8: Opc = IA64::LD1; break;
516 case MVT::i16: Opc = IA64::LD2; break;
517 case MVT::i32: Opc = IA64::LD4; break;
518 case MVT::i64: Opc = IA64::LD8; break;
519
520 case MVT::f32: Opc = IA64::LDF4; break;
521 case MVT::f64: Opc = IA64::LDF8; break;
522 }
523
Chris Lattnerb19b8992005-11-30 23:02:08 +0000524 // TODO: comment this
525 return CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), MVT::Other,
526 Address, Chain).getValue(Op.ResNo);
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000527 }
528
529 case ISD::TRUNCSTORE:
530 case ISD::STORE: {
531 SDOperand Address = Select(N->getOperand(2));
Duraid Madinad525df32005-11-07 03:11:02 +0000532 SDOperand Chain = Select(N->getOperand(0));
533
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000534 unsigned Opc;
535 if (N->getOpcode() == ISD::STORE) {
536 switch (N->getOperand(1).getValueType()) {
Duraid Madinad525df32005-11-07 03:11:02 +0000537 default: assert(0 && "unknown type in store");
538 case MVT::i1: { // this is a bool
539 Opc = IA64::ST1; // we store either 0 or 1 as a byte
Duraid Madina544cbbd2006-01-13 10:28:25 +0000540 // first load zero!
541 SDOperand Initial = CurDAG->getCopyFromReg(Chain, IA64::r0, MVT::i64);
542 Chain = Initial.getValue(1);
543 // then load 1 iff the predicate to store is 1
Chris Lattnerb19b8992005-11-30 23:02:08 +0000544 SDOperand Tmp =
Duraid Madina544cbbd2006-01-13 10:28:25 +0000545 CurDAG->getTargetNode(IA64::PADDS, MVT::i64, Initial,
Chris Lattnerb19b8992005-11-30 23:02:08 +0000546 CurDAG->getConstant(1, MVT::i64),
547 Select(N->getOperand(1)));
548 return CurDAG->SelectNodeTo(N, Opc, MVT::Other, Address, Tmp, Chain);
549 }
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000550 case MVT::i64: Opc = IA64::ST8; break;
551 case MVT::f64: Opc = IA64::STF8; break;
Duraid Madinad525df32005-11-07 03:11:02 +0000552 }
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000553 } else { //ISD::TRUNCSTORE
554 switch(cast<VTSDNode>(N->getOperand(4))->getVT()) {
Duraid Madinad525df32005-11-07 03:11:02 +0000555 default: assert(0 && "unknown type in truncstore");
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000556 case MVT::i8: Opc = IA64::ST1; break;
557 case MVT::i16: Opc = IA64::ST2; break;
558 case MVT::i32: Opc = IA64::ST4; break;
559 case MVT::f32: Opc = IA64::STF4; break;
560 }
561 }
562
Chris Lattnerb19b8992005-11-30 23:02:08 +0000563 return CurDAG->SelectNodeTo(N, Opc, MVT::Other, Select(N->getOperand(2)),
564 Select(N->getOperand(1)), Chain);
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000565 }
566
567 case ISD::BRCOND: {
568 SDOperand Chain = Select(N->getOperand(0));
569 SDOperand CC = Select(N->getOperand(1));
570 MachineBasicBlock *Dest =
571 cast<BasicBlockSDNode>(N->getOperand(2))->getBasicBlock();
572 //FIXME - we do NOT need long branches all the time
Chris Lattnerb19b8992005-11-30 23:02:08 +0000573 return CurDAG->SelectNodeTo(N, IA64::BRLCOND_NOTCALL, MVT::Other, CC,
574 CurDAG->getBasicBlock(Dest), Chain);
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000575 }
576
577 case ISD::CALLSEQ_START:
578 case ISD::CALLSEQ_END: {
579 int64_t Amt = cast<ConstantSDNode>(N->getOperand(1))->getValue();
580 unsigned Opc = N->getOpcode() == ISD::CALLSEQ_START ?
581 IA64::ADJUSTCALLSTACKDOWN : IA64::ADJUSTCALLSTACKUP;
Chris Lattnerb19b8992005-11-30 23:02:08 +0000582 return CurDAG->SelectNodeTo(N, Opc, MVT::Other,
583 getI64Imm(Amt), Select(N->getOperand(0)));
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000584 }
585
586 case ISD::RET: {
587 SDOperand Chain = Select(N->getOperand(0)); // Token chain.
Duraid Madinaa36153a2005-12-22 03:58:17 +0000588 SDOperand InFlag;
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000589
590 switch (N->getNumOperands()) {
591 default:
592 assert(0 && "Unknown return instruction!");
593 case 2: {
594 SDOperand RetVal = Select(N->getOperand(1));
595 switch (RetVal.getValueType()) {
596 default: assert(0 && "I don't know how to return this type! (promote?)");
597 // FIXME: do I need to add support for bools here?
598 // (return '0' or '1' in r8, basically...)
599 //
600 // FIXME: need to round floats - 80 bits is bad, the tester
601 // told me so
602 case MVT::i64:
603 // we mark r8 as live on exit up above in LowerArguments()
604 // BuildMI(BB, IA64::MOV, 1, IA64::r8).addReg(Tmp1);
605 Chain = CurDAG->getCopyToReg(Chain, IA64::r8, RetVal);
Duraid Madinaa36153a2005-12-22 03:58:17 +0000606 InFlag = Chain.getValue(1);
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000607 break;
608 case MVT::f64:
609 // we mark F8 as live on exit up above in LowerArguments()
610 // BuildMI(BB, IA64::FMOV, 1, IA64::F8).addReg(Tmp1);
611 Chain = CurDAG->getCopyToReg(Chain, IA64::F8, RetVal);
Duraid Madinaa36153a2005-12-22 03:58:17 +0000612 InFlag = Chain.getValue(1);
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000613 break;
614 }
615 break;
616 }
617 case 1:
618 break;
619 }
620
621 // we need to copy VirtGPR (the vreg (to become a real reg)) that holds
622 // the output of this function's alloc instruction back into ar.pfs
623 // before we return. this copy must not float up above the last
624 // outgoing call in this function!!!
625 SDOperand AR_PFSVal = CurDAG->getCopyFromReg(Chain, IA64Lowering.VirtGPR,
626 MVT::i64);
627 Chain = AR_PFSVal.getValue(1);
628 Chain = CurDAG->getCopyToReg(Chain, IA64::AR_PFS, AR_PFSVal);
629
Chris Lattnerb19b8992005-11-30 23:02:08 +0000630 // and then just emit a 'ret' instruction
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000631 // before returning, restore the ar.pfs register (set by the 'alloc' up top)
632 // BuildMI(BB, IA64::MOV, 1).addReg(IA64::AR_PFS).addReg(IA64Lowering.VirtGPR);
633 //
Chris Lattnerb19b8992005-11-30 23:02:08 +0000634 return CurDAG->SelectNodeTo(N, IA64::RET, MVT::Other, Chain);
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000635 }
636
637 case ISD::BR:
638 // FIXME: we don't need long branches all the time!
Chris Lattnerb19b8992005-11-30 23:02:08 +0000639 return CurDAG->SelectNodeTo(N, IA64::BRL_NOTCALL, MVT::Other,
640 N->getOperand(1), Select(N->getOperand(0)));
Duraid Madinaf2db9b82005-10-28 17:46:35 +0000641 }
642
643 return SelectCode(Op);
644}
645
646
647/// createIA64DAGToDAGInstructionSelector - This pass converts a legalized DAG
648/// into an IA64-specific DAG, ready for instruction scheduling.
649///
650FunctionPass *llvm::createIA64DAGToDAGInstructionSelector(TargetMachine &TM) {
651 return new IA64DAGToDAGISel(TM);
652}
653