blob: da968b9c79639314ddab46361d6c6a515773a868 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===---- IA64ISelDAGToDAG.cpp - IA64 pattern matching inst selector ------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a pattern matching instruction selector for IA64,
11// converting a legalized dag to an IA64 dag.
12//
13//===----------------------------------------------------------------------===//
14
15#define DEBUG_TYPE "ia64-codegen"
16#include "IA64.h"
17#include "IA64TargetMachine.h"
18#include "IA64ISelLowering.h"
19#include "llvm/CodeGen/MachineInstrBuilder.h"
20#include "llvm/CodeGen/MachineFunction.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include "llvm/CodeGen/SelectionDAG.h"
22#include "llvm/CodeGen/SelectionDAGISel.h"
23#include "llvm/Target/TargetOptions.h"
24#include "llvm/Constants.h"
25#include "llvm/GlobalValue.h"
26#include "llvm/Intrinsics.h"
Chris Lattner93c741a2008-02-03 05:43:57 +000027#include "llvm/Support/Compiler.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000028#include "llvm/Support/Debug.h"
29#include "llvm/Support/MathExtras.h"
30#include <queue>
31#include <set>
32using namespace llvm;
33
34namespace {
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(IA64TargetMachine &TM)
44 : SelectionDAGISel(IA64Lowering), IA64Lowering(*TM.getTargetLowering()) {}
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 SDNode *Select(SDOperand N);
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 /// InstructionSelectBasicBlock - This callback is invoked by
82 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
83 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
84
85 virtual const char *getPassName() const {
86 return "IA64 (Itanium) DAG->DAG Instruction Selector";
87 }
88
89// Include the pieces autogenerated from the target description.
90#include "IA64GenDAGISel.inc"
91
92private:
93 SDNode *SelectDIV(SDOperand Op);
94 };
95}
96
97/// InstructionSelectBasicBlock - This callback is invoked by
98/// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
99void IA64DAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
100 DEBUG(BB->dump());
101
102 // Select target instructions for the DAG.
103 DAG.setRoot(SelectRoot(DAG.getRoot()));
104 DAG.RemoveDeadNodes();
105
106 // Emit machine code to BB.
107 ScheduleAndEmitDAG(DAG);
108}
109
110SDNode *IA64DAGToDAGISel::SelectDIV(SDOperand Op) {
111 SDNode *N = Op.Val;
112 SDOperand Chain = N->getOperand(0);
113 SDOperand Tmp1 = N->getOperand(0);
114 SDOperand Tmp2 = N->getOperand(1);
115 AddToISelQueue(Chain);
116
117 AddToISelQueue(Tmp1);
118 AddToISelQueue(Tmp2);
119
120 bool isFP=false;
121
122 if(MVT::isFloatingPoint(Tmp1.getValueType()))
123 isFP=true;
124
125 bool isModulus=false; // is it a division or a modulus?
126 bool isSigned=false;
127
128 switch(N->getOpcode()) {
129 case ISD::FDIV:
130 case ISD::SDIV: isModulus=false; isSigned=true; break;
131 case ISD::UDIV: isModulus=false; isSigned=false; break;
132 case ISD::FREM:
133 case ISD::SREM: isModulus=true; isSigned=true; break;
134 case ISD::UREM: isModulus=true; isSigned=false; break;
135 }
136
137 // TODO: check for integer divides by powers of 2 (or other simple patterns?)
138
139 SDOperand TmpPR, TmpPR2;
140 SDOperand TmpF1, TmpF2, TmpF3, TmpF4, TmpF5, TmpF6, TmpF7, TmpF8;
141 SDOperand TmpF9, TmpF10,TmpF11,TmpF12,TmpF13,TmpF14,TmpF15;
142 SDNode *Result;
143
144 // we'll need copies of F0 and F1
145 SDOperand F0 = CurDAG->getRegister(IA64::F0, MVT::f64);
146 SDOperand F1 = CurDAG->getRegister(IA64::F1, MVT::f64);
147
148 // OK, emit some code:
149
150 if(!isFP) {
151 // first, load the inputs into FP regs.
152 TmpF1 =
153 SDOperand(CurDAG->getTargetNode(IA64::SETFSIG, MVT::f64, Tmp1), 0);
154 Chain = TmpF1.getValue(1);
155 TmpF2 =
156 SDOperand(CurDAG->getTargetNode(IA64::SETFSIG, MVT::f64, Tmp2), 0);
157 Chain = TmpF2.getValue(1);
158
159 // next, convert the inputs to FP
160 if(isSigned) {
161 TmpF3 =
162 SDOperand(CurDAG->getTargetNode(IA64::FCVTXF, MVT::f64, TmpF1), 0);
163 Chain = TmpF3.getValue(1);
164 TmpF4 =
165 SDOperand(CurDAG->getTargetNode(IA64::FCVTXF, MVT::f64, TmpF2), 0);
166 Chain = TmpF4.getValue(1);
167 } else { // is unsigned
168 TmpF3 =
169 SDOperand(CurDAG->getTargetNode(IA64::FCVTXUFS1, MVT::f64, TmpF1), 0);
170 Chain = TmpF3.getValue(1);
171 TmpF4 =
172 SDOperand(CurDAG->getTargetNode(IA64::FCVTXUFS1, MVT::f64, TmpF2), 0);
173 Chain = TmpF4.getValue(1);
174 }
175
176 } else { // this is an FP divide/remainder, so we 'leak' some temp
177 // regs and assign TmpF3=Tmp1, TmpF4=Tmp2
178 TmpF3=Tmp1;
179 TmpF4=Tmp2;
180 }
181
182 // we start by computing an approximate reciprocal (good to 9 bits?)
183 // note, this instruction writes _both_ TmpF5 (answer) and TmpPR (predicate)
184 if(isFP)
185 TmpF5 = SDOperand(CurDAG->getTargetNode(IA64::FRCPAS0, MVT::f64, MVT::i1,
186 TmpF3, TmpF4), 0);
187 else
188 TmpF5 = SDOperand(CurDAG->getTargetNode(IA64::FRCPAS1, MVT::f64, MVT::i1,
189 TmpF3, TmpF4), 0);
190
191 TmpPR = TmpF5.getValue(1);
192 Chain = TmpF5.getValue(2);
193
194 SDOperand minusB;
195 if(isModulus) { // for remainders, it'll be handy to have
196 // copies of -input_b
197 minusB = SDOperand(CurDAG->getTargetNode(IA64::SUB, MVT::i64,
198 CurDAG->getRegister(IA64::r0, MVT::i64), Tmp2), 0);
199 Chain = minusB.getValue(1);
200 }
201
202 SDOperand TmpE0, TmpY1, TmpE1, TmpY2;
203
204 SDOperand OpsE0[] = { TmpF4, TmpF5, F1, TmpPR };
205 TmpE0 = SDOperand(CurDAG->getTargetNode(IA64::CFNMAS1, MVT::f64,
206 OpsE0, 4), 0);
207 Chain = TmpE0.getValue(1);
208 SDOperand OpsY1[] = { TmpF5, TmpE0, TmpF5, TmpPR };
209 TmpY1 = SDOperand(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
210 OpsY1, 4), 0);
211 Chain = TmpY1.getValue(1);
212 SDOperand OpsE1[] = { TmpE0, TmpE0, F0, TmpPR };
213 TmpE1 = SDOperand(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
214 OpsE1, 4), 0);
215 Chain = TmpE1.getValue(1);
216 SDOperand OpsY2[] = { TmpY1, TmpE1, TmpY1, TmpPR };
217 TmpY2 = SDOperand(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
218 OpsY2, 4), 0);
219 Chain = TmpY2.getValue(1);
220
221 if(isFP) { // if this is an FP divide, we finish up here and exit early
222 if(isModulus)
223 assert(0 && "Sorry, try another FORTRAN compiler.");
224
225 SDOperand TmpE2, TmpY3, TmpQ0, TmpR0;
226
227 SDOperand OpsE2[] = { TmpE1, TmpE1, F0, TmpPR };
228 TmpE2 = SDOperand(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
229 OpsE2, 4), 0);
230 Chain = TmpE2.getValue(1);
231 SDOperand OpsY3[] = { TmpY2, TmpE2, TmpY2, TmpPR };
232 TmpY3 = SDOperand(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
233 OpsY3, 4), 0);
234 Chain = TmpY3.getValue(1);
235 SDOperand OpsQ0[] = { Tmp1, TmpY3, F0, TmpPR };
236 TmpQ0 =
237 SDOperand(CurDAG->getTargetNode(IA64::CFMADS1, MVT::f64, // double prec!
238 OpsQ0, 4), 0);
239 Chain = TmpQ0.getValue(1);
240 SDOperand OpsR0[] = { Tmp2, TmpQ0, Tmp1, TmpPR };
241 TmpR0 =
242 SDOperand(CurDAG->getTargetNode(IA64::CFNMADS1, MVT::f64, // double prec!
243 OpsR0, 4), 0);
244 Chain = TmpR0.getValue(1);
245
246// we want Result to have the same target register as the frcpa, so
247// we two-address hack it. See the comment "for this to work..." on
248// page 48 of Intel application note #245415
249 SDOperand Ops[] = { TmpF5, TmpY3, TmpR0, TmpQ0, TmpPR };
250 Result = CurDAG->getTargetNode(IA64::TCFMADS0, MVT::f64, // d.p. s0 rndg!
251 Ops, 5);
252 Chain = SDOperand(Result, 1);
253 return Result; // XXX: early exit!
254 } else { // this is *not* an FP divide, so there's a bit left to do:
255
256 SDOperand TmpQ2, TmpR2, TmpQ3, TmpQ;
257
258 SDOperand OpsQ2[] = { TmpF3, TmpY2, F0, TmpPR };
259 TmpQ2 = SDOperand(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
260 OpsQ2, 4), 0);
261 Chain = TmpQ2.getValue(1);
262 SDOperand OpsR2[] = { TmpF4, TmpQ2, TmpF3, TmpPR };
263 TmpR2 = SDOperand(CurDAG->getTargetNode(IA64::CFNMAS1, MVT::f64,
264 OpsR2, 4), 0);
265 Chain = TmpR2.getValue(1);
266
267// we want TmpQ3 to have the same target register as the frcpa? maybe we
268// should two-address hack it. See the comment "for this to work..." on page
269// 48 of Intel application note #245415
270 SDOperand OpsQ3[] = { TmpF5, TmpR2, TmpY2, TmpQ2, TmpPR };
271 TmpQ3 = SDOperand(CurDAG->getTargetNode(IA64::TCFMAS1, MVT::f64,
272 OpsQ3, 5), 0);
273 Chain = TmpQ3.getValue(1);
274
275 // STORY: without these two-address instructions (TCFMAS1 and TCFMADS0)
276 // the FPSWA won't be able to help out in the case of large/tiny
277 // arguments. Other fun bugs may also appear, e.g. 0/x = x, not 0.
278
279 if(isSigned)
280 TmpQ = SDOperand(CurDAG->getTargetNode(IA64::FCVTFXTRUNCS1,
281 MVT::f64, TmpQ3), 0);
282 else
283 TmpQ = SDOperand(CurDAG->getTargetNode(IA64::FCVTFXUTRUNCS1,
284 MVT::f64, TmpQ3), 0);
285
286 Chain = TmpQ.getValue(1);
287
288 if(isModulus) {
289 SDOperand FPminusB =
290 SDOperand(CurDAG->getTargetNode(IA64::SETFSIG, MVT::f64, minusB), 0);
291 Chain = FPminusB.getValue(1);
292 SDOperand Remainder =
293 SDOperand(CurDAG->getTargetNode(IA64::XMAL, MVT::f64,
294 TmpQ, FPminusB, TmpF1), 0);
295 Chain = Remainder.getValue(1);
296 Result = CurDAG->getTargetNode(IA64::GETFSIG, MVT::i64, Remainder);
297 Chain = SDOperand(Result, 1);
298 } else { // just an integer divide
299 Result = CurDAG->getTargetNode(IA64::GETFSIG, MVT::i64, TmpQ);
300 Chain = SDOperand(Result, 1);
301 }
302
303 return Result;
304 } // wasn't an FP divide
305}
306
307// Select - Convert the specified operand from a target-independent to a
308// target-specific node if it hasn't already been changed.
309SDNode *IA64DAGToDAGISel::Select(SDOperand Op) {
310 SDNode *N = Op.Val;
311 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
312 N->getOpcode() < IA64ISD::FIRST_NUMBER)
313 return NULL; // Already selected.
314
315 switch (N->getOpcode()) {
316 default: break;
317
318 case IA64ISD::BRCALL: { // XXX: this is also a hack!
319 SDOperand Chain = N->getOperand(0);
320 SDOperand InFlag; // Null incoming flag value.
321
322 AddToISelQueue(Chain);
323 if(N->getNumOperands()==3) { // we have an incoming chain, callee and flag
324 InFlag = N->getOperand(2);
325 AddToISelQueue(InFlag);
326 }
327
328 unsigned CallOpcode;
329 SDOperand CallOperand;
330
331 // if we can call directly, do so
332 if (GlobalAddressSDNode *GASD =
333 dyn_cast<GlobalAddressSDNode>(N->getOperand(1))) {
334 CallOpcode = IA64::BRCALL_IPREL_GA;
335 CallOperand = CurDAG->getTargetGlobalAddress(GASD->getGlobal(), MVT::i64);
336 } else if (isa<ExternalSymbolSDNode>(N->getOperand(1))) {
337 // FIXME: we currently NEED this case for correctness, to avoid
338 // "non-pic code with imm reloc.n against dynamic symbol" errors
339 CallOpcode = IA64::BRCALL_IPREL_ES;
340 CallOperand = N->getOperand(1);
341 } else {
342 // otherwise we need to load the function descriptor,
343 // load the branch target (function)'s entry point and GP,
344 // branch (call) then restore the GP
345 SDOperand FnDescriptor = N->getOperand(1);
346 AddToISelQueue(FnDescriptor);
347
348 // load the branch target's entry point [mem] and
349 // GP value [mem+8]
350 SDOperand targetEntryPoint=
Chris Lattner20e32202008-05-28 04:06:52 +0000351 SDOperand(CurDAG->getTargetNode(IA64::LD8, MVT::i64, MVT::Other,
Chris Lattnerd04567e2008-05-28 04:25:57 +0000352 FnDescriptor, CurDAG->getEntryNode()), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000353 Chain = targetEntryPoint.getValue(1);
354 SDOperand targetGPAddr=
355 SDOperand(CurDAG->getTargetNode(IA64::ADDS, MVT::i64,
356 FnDescriptor,
357 CurDAG->getConstant(8, MVT::i64)), 0);
358 Chain = targetGPAddr.getValue(1);
359 SDOperand targetGP =
Chris Lattner20e32202008-05-28 04:06:52 +0000360 SDOperand(CurDAG->getTargetNode(IA64::LD8, MVT::i64,MVT::Other,
Chris Lattnerd04567e2008-05-28 04:25:57 +0000361 targetGPAddr, CurDAG->getEntryNode()), 0);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000362 Chain = targetGP.getValue(1);
363
364 Chain = CurDAG->getCopyToReg(Chain, IA64::r1, targetGP, InFlag);
365 InFlag = Chain.getValue(1);
366 Chain = CurDAG->getCopyToReg(Chain, IA64::B6, targetEntryPoint, InFlag); // FLAG these?
367 InFlag = Chain.getValue(1);
368
369 CallOperand = CurDAG->getRegister(IA64::B6, MVT::i64);
370 CallOpcode = IA64::BRCALL_INDIRECT;
371 }
372
373 // Finally, once everything is setup, emit the call itself
374 if(InFlag.Val)
375 Chain = SDOperand(CurDAG->getTargetNode(CallOpcode, MVT::Other, MVT::Flag,
376 CallOperand, InFlag), 0);
377 else // there might be no arguments
378 Chain = SDOperand(CurDAG->getTargetNode(CallOpcode, MVT::Other, MVT::Flag,
379 CallOperand, Chain), 0);
380 InFlag = Chain.getValue(1);
381
382 std::vector<SDOperand> CallResults;
383
384 CallResults.push_back(Chain);
385 CallResults.push_back(InFlag);
386
387 for (unsigned i = 0, e = CallResults.size(); i != e; ++i)
388 ReplaceUses(Op.getValue(i), CallResults[i]);
389 return NULL;
390 }
391
392 case IA64ISD::GETFD: {
393 SDOperand Input = N->getOperand(0);
394 AddToISelQueue(Input);
395 return CurDAG->getTargetNode(IA64::GETFD, MVT::i64, Input);
396 }
397
398 case ISD::FDIV:
399 case ISD::SDIV:
400 case ISD::UDIV:
401 case ISD::SREM:
402 case ISD::UREM:
403 return SelectDIV(Op);
404
405 case ISD::TargetConstantFP: {
406 SDOperand Chain = CurDAG->getEntryNode(); // this is a constant, so..
407
408 SDOperand V;
Dale Johannesen76844472007-08-31 17:03:33 +0000409 ConstantFPSDNode* N2 = cast<ConstantFPSDNode>(N);
410 if (N2->getValueAPF().isPosZero()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000411 V = CurDAG->getCopyFromReg(Chain, IA64::F0, MVT::f64);
Dale Johannesen76844472007-08-31 17:03:33 +0000412 } else if (N2->isExactlyValue(N2->getValueType(0) == MVT::f32 ?
413 APFloat(+1.0f) : APFloat(+1.0))) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000414 V = CurDAG->getCopyFromReg(Chain, IA64::F1, MVT::f64);
415 } else
416 assert(0 && "Unexpected FP constant!");
417
418 ReplaceUses(SDOperand(N, 0), V);
419 return 0;
420 }
421
422 case ISD::FrameIndex: { // TODO: reduce creepyness
423 int FI = cast<FrameIndexSDNode>(N)->getIndex();
424 if (N->hasOneUse())
425 return CurDAG->SelectNodeTo(N, IA64::MOV, MVT::i64,
426 CurDAG->getTargetFrameIndex(FI, MVT::i64));
427 else
428 return CurDAG->getTargetNode(IA64::MOV, MVT::i64,
429 CurDAG->getTargetFrameIndex(FI, MVT::i64));
430 }
431
432 case ISD::ConstantPool: { // TODO: nuke the constant pool
433 // (ia64 doesn't need one)
434 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
435 Constant *C = CP->getConstVal();
436 SDOperand CPI = CurDAG->getTargetConstantPool(C, MVT::i64,
437 CP->getAlignment());
438 return CurDAG->getTargetNode(IA64::ADDL_GA, MVT::i64, // ?
439 CurDAG->getRegister(IA64::r1, MVT::i64), CPI);
440 }
441
442 case ISD::GlobalAddress: {
443 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
444 SDOperand GA = CurDAG->getTargetGlobalAddress(GV, MVT::i64);
445 SDOperand Tmp =
446 SDOperand(CurDAG->getTargetNode(IA64::ADDL_GA, MVT::i64,
447 CurDAG->getRegister(IA64::r1,
448 MVT::i64), GA), 0);
Chris Lattnerd04567e2008-05-28 04:25:57 +0000449 return CurDAG->getTargetNode(IA64::LD8, MVT::i64, MVT::Other, Tmp);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000450 }
451
452/* XXX
453 case ISD::ExternalSymbol: {
454 SDOperand EA = CurDAG->getTargetExternalSymbol(
455 cast<ExternalSymbolSDNode>(N)->getSymbol(),
456 MVT::i64);
457 SDOperand Tmp = CurDAG->getTargetNode(IA64::ADDL_EA, MVT::i64,
458 CurDAG->getRegister(IA64::r1,
459 MVT::i64),
460 EA);
461 return CurDAG->getTargetNode(IA64::LD8, MVT::i64, Tmp);
462 }
463*/
464
465 case ISD::LOAD: { // FIXME: load -1, not 1, for bools?
466 LoadSDNode *LD = cast<LoadSDNode>(N);
467 SDOperand Chain = LD->getChain();
468 SDOperand Address = LD->getBasePtr();
469 AddToISelQueue(Chain);
470 AddToISelQueue(Address);
471
Dan Gohman9a4c92c2008-01-30 00:15:11 +0000472 MVT::ValueType TypeBeingLoaded = LD->getMemoryVT();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000473 unsigned Opc;
474 switch (TypeBeingLoaded) {
475 default:
476#ifndef NDEBUG
477 N->dump(CurDAG);
478#endif
479 assert(0 && "Cannot load this type!");
480 case MVT::i1: { // this is a bool
481 Opc = IA64::LD1; // first we load a byte, then compare for != 0
482 if(N->getValueType(0) == MVT::i1) { // XXX: early exit!
483 return CurDAG->SelectNodeTo(N, IA64::CMPNE, MVT::i1, MVT::Other,
484 SDOperand(CurDAG->getTargetNode(Opc, MVT::i64, Address), 0),
485 CurDAG->getRegister(IA64::r0, MVT::i64),
486 Chain);
487 }
488 /* otherwise, we want to load a bool into something bigger: LD1
489 will do that for us, so we just fall through */
490 }
491 case MVT::i8: Opc = IA64::LD1; break;
492 case MVT::i16: Opc = IA64::LD2; break;
493 case MVT::i32: Opc = IA64::LD4; break;
494 case MVT::i64: Opc = IA64::LD8; break;
495
496 case MVT::f32: Opc = IA64::LDF4; break;
497 case MVT::f64: Opc = IA64::LDF8; break;
498 }
499
500 // TODO: comment this
501 return CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), MVT::Other,
502 Address, Chain);
503 }
504
505 case ISD::STORE: {
506 StoreSDNode *ST = cast<StoreSDNode>(N);
507 SDOperand Address = ST->getBasePtr();
508 SDOperand Chain = ST->getChain();
509 AddToISelQueue(Address);
510 AddToISelQueue(Chain);
511
512 unsigned Opc;
513 if (ISD::isNON_TRUNCStore(N)) {
514 switch (N->getOperand(1).getValueType()) {
515 default: assert(0 && "unknown type in store");
516 case MVT::i1: { // this is a bool
517 Opc = IA64::ST1; // we store either 0 or 1 as a byte
518 // first load zero!
519 SDOperand Initial = CurDAG->getCopyFromReg(Chain, IA64::r0, MVT::i64);
520 Chain = Initial.getValue(1);
521 // then load 1 into the same reg iff the predicate to store is 1
522 SDOperand Tmp = ST->getValue();
523 AddToISelQueue(Tmp);
524 Tmp =
525 SDOperand(CurDAG->getTargetNode(IA64::TPCADDS, MVT::i64, Initial,
526 CurDAG->getTargetConstant(1, MVT::i64),
527 Tmp), 0);
528 return CurDAG->SelectNodeTo(N, Opc, MVT::Other, Address, Tmp, Chain);
529 }
530 case MVT::i64: Opc = IA64::ST8; break;
531 case MVT::f64: Opc = IA64::STF8; break;
532 }
533 } else { // Truncating store
Dan Gohman9a4c92c2008-01-30 00:15:11 +0000534 switch(ST->getMemoryVT()) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000535 default: assert(0 && "unknown type in truncstore");
536 case MVT::i8: Opc = IA64::ST1; break;
537 case MVT::i16: Opc = IA64::ST2; break;
538 case MVT::i32: Opc = IA64::ST4; break;
539 case MVT::f32: Opc = IA64::STF4; break;
540 }
541 }
542
543 SDOperand N1 = N->getOperand(1);
544 SDOperand N2 = N->getOperand(2);
545 AddToISelQueue(N1);
546 AddToISelQueue(N2);
547 return CurDAG->SelectNodeTo(N, Opc, MVT::Other, N2, N1, Chain);
548 }
549
550 case ISD::BRCOND: {
551 SDOperand Chain = N->getOperand(0);
552 SDOperand CC = N->getOperand(1);
553 AddToISelQueue(Chain);
554 AddToISelQueue(CC);
555 MachineBasicBlock *Dest =
556 cast<BasicBlockSDNode>(N->getOperand(2))->getBasicBlock();
557 //FIXME - we do NOT need long branches all the time
558 return CurDAG->SelectNodeTo(N, IA64::BRLCOND_NOTCALL, MVT::Other, CC,
559 CurDAG->getBasicBlock(Dest), Chain);
560 }
561
562 case ISD::CALLSEQ_START:
563 case ISD::CALLSEQ_END: {
564 int64_t Amt = cast<ConstantSDNode>(N->getOperand(1))->getValue();
565 unsigned Opc = N->getOpcode() == ISD::CALLSEQ_START ?
566 IA64::ADJUSTCALLSTACKDOWN : IA64::ADJUSTCALLSTACKUP;
567 SDOperand N0 = N->getOperand(0);
568 AddToISelQueue(N0);
569 return CurDAG->SelectNodeTo(N, Opc, MVT::Other, getI64Imm(Amt), N0);
570 }
571
572 case ISD::BR:
573 // FIXME: we don't need long branches all the time!
574 SDOperand N0 = N->getOperand(0);
575 AddToISelQueue(N0);
576 return CurDAG->SelectNodeTo(N, IA64::BRL_NOTCALL, MVT::Other,
577 N->getOperand(1), N0);
578 }
579
580 return SelectCode(Op);
581}
582
583
584/// createIA64DAGToDAGInstructionSelector - This pass converts a legalized DAG
585/// into an IA64-specific DAG, ready for instruction scheduling.
586///
587FunctionPass
588*llvm::createIA64DAGToDAGInstructionSelector(IA64TargetMachine &TM) {
589 return new IA64DAGToDAGISel(TM);
590}
591