1 //===---- IA64ISelDAGToDAG.cpp - IA64 pattern matching inst selector ------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a pattern matching instruction selector for IA64,
11 // converting a legalized dag to an IA64 dag.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "ia64-codegen"
17 #include "IA64TargetMachine.h"
18 #include "IA64ISelLowering.h"
19 #include "llvm/CodeGen/MachineInstrBuilder.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #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"
27 #include "llvm/Support/Compiler.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/MathExtras.h"
35 //===--------------------------------------------------------------------===//
36 /// IA64DAGToDAGISel - IA64 specific code to select IA64 machine
37 /// instructions for SelectionDAG operations.
39 class IA64DAGToDAGISel : public SelectionDAGISel {
40 IA64TargetLowering IA64Lowering;
41 unsigned GlobalBaseReg;
43 explicit IA64DAGToDAGISel(IA64TargetMachine &TM)
44 : SelectionDAGISel(IA64Lowering), IA64Lowering(*TM.getTargetLowering()) {}
46 virtual bool runOnFunction(Function &Fn) {
47 // Make sure we re-emit a set of the global base reg if necessary
49 return SelectionDAGISel::runOnFunction(Fn);
52 /// getI64Imm - Return a target constant with the specified value, of type
54 inline SDValue getI64Imm(uint64_t Imm) {
55 return CurDAG->getTargetConstant(Imm, MVT::i64);
58 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
59 /// base register. Return the virtual register that holds this value.
60 // SDValue getGlobalBaseReg(); TODO: hmm
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(SDValue N);
66 SDNode *SelectIntImmediateExpr(SDValue LHS, SDValue RHS,
67 unsigned OCHi, unsigned OCLo,
68 bool IsArithmetic = false,
70 SDNode *SelectBitfieldInsert(SDNode *N);
72 /// SelectCC - Select a comparison of the specified values with the
73 /// specified condition code, returning the CR# of the expression.
74 SDValue SelectCC(SDValue LHS, SDValue RHS, ISD::CondCode CC);
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]
79 bool SelectAddr(SDValue Addr, SDValue &Op1, SDValue &Op2);
81 /// InstructionSelect - This callback is invoked by
82 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
83 virtual void InstructionSelect();
85 virtual const char *getPassName() const {
86 return "IA64 (Itanium) DAG->DAG Instruction Selector";
89 // Include the pieces autogenerated from the target description.
90 #include "IA64GenDAGISel.inc"
93 SDNode *SelectDIV(SDValue Op);
97 /// InstructionSelect - This callback is invoked by
98 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
99 void IA64DAGToDAGISel::InstructionSelect() {
102 // Select target instructions for the DAG.
104 CurDAG->RemoveDeadNodes();
107 SDNode *IA64DAGToDAGISel::SelectDIV(SDValue Op) {
108 SDNode *N = Op.getNode();
109 SDValue Chain = N->getOperand(0);
110 SDValue Tmp1 = N->getOperand(0);
111 SDValue Tmp2 = N->getOperand(1);
112 AddToISelQueue(Chain);
114 AddToISelQueue(Tmp1);
115 AddToISelQueue(Tmp2);
119 if(Tmp1.getValueType().isFloatingPoint())
122 bool isModulus=false; // is it a division or a modulus?
125 switch(N->getOpcode()) {
127 case ISD::SDIV: isModulus=false; isSigned=true; break;
128 case ISD::UDIV: isModulus=false; isSigned=false; break;
130 case ISD::SREM: isModulus=true; isSigned=true; break;
131 case ISD::UREM: isModulus=true; isSigned=false; break;
134 // TODO: check for integer divides by powers of 2 (or other simple patterns?)
136 SDValue TmpPR, TmpPR2;
137 SDValue TmpF1, TmpF2, TmpF3, TmpF4, TmpF5, TmpF6, TmpF7, TmpF8;
138 SDValue TmpF9, TmpF10,TmpF11,TmpF12,TmpF13,TmpF14,TmpF15;
141 // we'll need copies of F0 and F1
142 SDValue F0 = CurDAG->getRegister(IA64::F0, MVT::f64);
143 SDValue F1 = CurDAG->getRegister(IA64::F1, MVT::f64);
145 // OK, emit some code:
148 // first, load the inputs into FP regs.
150 SDValue(CurDAG->getTargetNode(IA64::SETFSIG, MVT::f64, Tmp1), 0);
151 Chain = TmpF1.getValue(1);
153 SDValue(CurDAG->getTargetNode(IA64::SETFSIG, MVT::f64, Tmp2), 0);
154 Chain = TmpF2.getValue(1);
156 // next, convert the inputs to FP
159 SDValue(CurDAG->getTargetNode(IA64::FCVTXF, MVT::f64, TmpF1), 0);
160 Chain = TmpF3.getValue(1);
162 SDValue(CurDAG->getTargetNode(IA64::FCVTXF, MVT::f64, TmpF2), 0);
163 Chain = TmpF4.getValue(1);
164 } else { // is unsigned
166 SDValue(CurDAG->getTargetNode(IA64::FCVTXUFS1, MVT::f64, TmpF1), 0);
167 Chain = TmpF3.getValue(1);
169 SDValue(CurDAG->getTargetNode(IA64::FCVTXUFS1, MVT::f64, TmpF2), 0);
170 Chain = TmpF4.getValue(1);
173 } else { // this is an FP divide/remainder, so we 'leak' some temp
174 // regs and assign TmpF3=Tmp1, TmpF4=Tmp2
179 // we start by computing an approximate reciprocal (good to 9 bits?)
180 // note, this instruction writes _both_ TmpF5 (answer) and TmpPR (predicate)
182 TmpF5 = SDValue(CurDAG->getTargetNode(IA64::FRCPAS0, MVT::f64, MVT::i1,
185 TmpF5 = SDValue(CurDAG->getTargetNode(IA64::FRCPAS1, MVT::f64, MVT::i1,
188 TmpPR = TmpF5.getValue(1);
189 Chain = TmpF5.getValue(2);
192 if(isModulus) { // for remainders, it'll be handy to have
193 // copies of -input_b
194 minusB = SDValue(CurDAG->getTargetNode(IA64::SUB, MVT::i64,
195 CurDAG->getRegister(IA64::r0, MVT::i64), Tmp2), 0);
196 Chain = minusB.getValue(1);
199 SDValue TmpE0, TmpY1, TmpE1, TmpY2;
201 SDValue OpsE0[] = { TmpF4, TmpF5, F1, TmpPR };
202 TmpE0 = SDValue(CurDAG->getTargetNode(IA64::CFNMAS1, MVT::f64,
204 Chain = TmpE0.getValue(1);
205 SDValue OpsY1[] = { TmpF5, TmpE0, TmpF5, TmpPR };
206 TmpY1 = SDValue(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
208 Chain = TmpY1.getValue(1);
209 SDValue OpsE1[] = { TmpE0, TmpE0, F0, TmpPR };
210 TmpE1 = SDValue(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
212 Chain = TmpE1.getValue(1);
213 SDValue OpsY2[] = { TmpY1, TmpE1, TmpY1, TmpPR };
214 TmpY2 = SDValue(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
216 Chain = TmpY2.getValue(1);
218 if(isFP) { // if this is an FP divide, we finish up here and exit early
220 assert(0 && "Sorry, try another FORTRAN compiler.");
222 SDValue TmpE2, TmpY3, TmpQ0, TmpR0;
224 SDValue OpsE2[] = { TmpE1, TmpE1, F0, TmpPR };
225 TmpE2 = SDValue(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
227 Chain = TmpE2.getValue(1);
228 SDValue OpsY3[] = { TmpY2, TmpE2, TmpY2, TmpPR };
229 TmpY3 = SDValue(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
231 Chain = TmpY3.getValue(1);
232 SDValue OpsQ0[] = { Tmp1, TmpY3, F0, TmpPR };
234 SDValue(CurDAG->getTargetNode(IA64::CFMADS1, MVT::f64, // double prec!
236 Chain = TmpQ0.getValue(1);
237 SDValue OpsR0[] = { Tmp2, TmpQ0, Tmp1, TmpPR };
239 SDValue(CurDAG->getTargetNode(IA64::CFNMADS1, MVT::f64, // double prec!
241 Chain = TmpR0.getValue(1);
243 // we want Result to have the same target register as the frcpa, so
244 // we two-address hack it. See the comment "for this to work..." on
245 // page 48 of Intel application note #245415
246 SDValue Ops[] = { TmpF5, TmpY3, TmpR0, TmpQ0, TmpPR };
247 Result = CurDAG->getTargetNode(IA64::TCFMADS0, MVT::f64, // d.p. s0 rndg!
249 Chain = SDValue(Result, 1);
250 return Result; // XXX: early exit!
251 } else { // this is *not* an FP divide, so there's a bit left to do:
253 SDValue TmpQ2, TmpR2, TmpQ3, TmpQ;
255 SDValue OpsQ2[] = { TmpF3, TmpY2, F0, TmpPR };
256 TmpQ2 = SDValue(CurDAG->getTargetNode(IA64::CFMAS1, MVT::f64,
258 Chain = TmpQ2.getValue(1);
259 SDValue OpsR2[] = { TmpF4, TmpQ2, TmpF3, TmpPR };
260 TmpR2 = SDValue(CurDAG->getTargetNode(IA64::CFNMAS1, MVT::f64,
262 Chain = TmpR2.getValue(1);
264 // we want TmpQ3 to have the same target register as the frcpa? maybe we
265 // should two-address hack it. See the comment "for this to work..." on page
266 // 48 of Intel application note #245415
267 SDValue OpsQ3[] = { TmpF5, TmpR2, TmpY2, TmpQ2, TmpPR };
268 TmpQ3 = SDValue(CurDAG->getTargetNode(IA64::TCFMAS1, MVT::f64,
270 Chain = TmpQ3.getValue(1);
272 // STORY: without these two-address instructions (TCFMAS1 and TCFMADS0)
273 // the FPSWA won't be able to help out in the case of large/tiny
274 // arguments. Other fun bugs may also appear, e.g. 0/x = x, not 0.
277 TmpQ = SDValue(CurDAG->getTargetNode(IA64::FCVTFXTRUNCS1,
278 MVT::f64, TmpQ3), 0);
280 TmpQ = SDValue(CurDAG->getTargetNode(IA64::FCVTFXUTRUNCS1,
281 MVT::f64, TmpQ3), 0);
283 Chain = TmpQ.getValue(1);
287 SDValue(CurDAG->getTargetNode(IA64::SETFSIG, MVT::f64, minusB), 0);
288 Chain = FPminusB.getValue(1);
290 SDValue(CurDAG->getTargetNode(IA64::XMAL, MVT::f64,
291 TmpQ, FPminusB, TmpF1), 0);
292 Chain = Remainder.getValue(1);
293 Result = CurDAG->getTargetNode(IA64::GETFSIG, MVT::i64, Remainder);
294 Chain = SDValue(Result, 1);
295 } else { // just an integer divide
296 Result = CurDAG->getTargetNode(IA64::GETFSIG, MVT::i64, TmpQ);
297 Chain = SDValue(Result, 1);
301 } // wasn't an FP divide
304 // Select - Convert the specified operand from a target-independent to a
305 // target-specific node if it hasn't already been changed.
306 SDNode *IA64DAGToDAGISel::Select(SDValue Op) {
307 SDNode *N = Op.getNode();
308 if (N->isMachineOpcode())
309 return NULL; // Already selected.
311 switch (N->getOpcode()) {
314 case IA64ISD::BRCALL: { // XXX: this is also a hack!
315 SDValue Chain = N->getOperand(0);
316 SDValue InFlag; // Null incoming flag value.
318 AddToISelQueue(Chain);
319 if(N->getNumOperands()==3) { // we have an incoming chain, callee and flag
320 InFlag = N->getOperand(2);
321 AddToISelQueue(InFlag);
327 // if we can call directly, do so
328 if (GlobalAddressSDNode *GASD =
329 dyn_cast<GlobalAddressSDNode>(N->getOperand(1))) {
330 CallOpcode = IA64::BRCALL_IPREL_GA;
331 CallOperand = CurDAG->getTargetGlobalAddress(GASD->getGlobal(), MVT::i64);
332 } else if (isa<ExternalSymbolSDNode>(N->getOperand(1))) {
333 // FIXME: we currently NEED this case for correctness, to avoid
334 // "non-pic code with imm reloc.n against dynamic symbol" errors
335 CallOpcode = IA64::BRCALL_IPREL_ES;
336 CallOperand = N->getOperand(1);
338 // otherwise we need to load the function descriptor,
339 // load the branch target (function)'s entry point and GP,
340 // branch (call) then restore the GP
341 SDValue FnDescriptor = N->getOperand(1);
342 AddToISelQueue(FnDescriptor);
344 // load the branch target's entry point [mem] and
346 SDValue targetEntryPoint=
347 SDValue(CurDAG->getTargetNode(IA64::LD8, MVT::i64, MVT::Other,
348 FnDescriptor, CurDAG->getEntryNode()), 0);
349 Chain = targetEntryPoint.getValue(1);
350 SDValue targetGPAddr=
351 SDValue(CurDAG->getTargetNode(IA64::ADDS, MVT::i64,
353 CurDAG->getConstant(8, MVT::i64)), 0);
354 Chain = targetGPAddr.getValue(1);
356 SDValue(CurDAG->getTargetNode(IA64::LD8, MVT::i64,MVT::Other,
357 targetGPAddr, CurDAG->getEntryNode()), 0);
358 Chain = targetGP.getValue(1);
360 Chain = CurDAG->getCopyToReg(Chain, IA64::r1, targetGP, InFlag);
361 InFlag = Chain.getValue(1);
362 Chain = CurDAG->getCopyToReg(Chain, IA64::B6, targetEntryPoint, InFlag); // FLAG these?
363 InFlag = Chain.getValue(1);
365 CallOperand = CurDAG->getRegister(IA64::B6, MVT::i64);
366 CallOpcode = IA64::BRCALL_INDIRECT;
369 // Finally, once everything is setup, emit the call itself
370 if (InFlag.getNode())
371 Chain = SDValue(CurDAG->getTargetNode(CallOpcode, MVT::Other, MVT::Flag,
372 CallOperand, InFlag), 0);
373 else // there might be no arguments
374 Chain = SDValue(CurDAG->getTargetNode(CallOpcode, MVT::Other, MVT::Flag,
375 CallOperand, Chain), 0);
376 InFlag = Chain.getValue(1);
378 std::vector<SDValue> CallResults;
380 CallResults.push_back(Chain);
381 CallResults.push_back(InFlag);
383 for (unsigned i = 0, e = CallResults.size(); i != e; ++i)
384 ReplaceUses(Op.getValue(i), CallResults[i]);
388 case IA64ISD::GETFD: {
389 SDValue Input = N->getOperand(0);
390 AddToISelQueue(Input);
391 return CurDAG->getTargetNode(IA64::GETFD, MVT::i64, Input);
399 return SelectDIV(Op);
401 case ISD::TargetConstantFP: {
402 SDValue Chain = CurDAG->getEntryNode(); // this is a constant, so..
405 ConstantFPSDNode* N2 = cast<ConstantFPSDNode>(N);
406 if (N2->getValueAPF().isPosZero()) {
407 V = CurDAG->getCopyFromReg(Chain, IA64::F0, MVT::f64);
408 } else if (N2->isExactlyValue(N2->getValueType(0) == MVT::f32 ?
409 APFloat(+1.0f) : APFloat(+1.0))) {
410 V = CurDAG->getCopyFromReg(Chain, IA64::F1, MVT::f64);
412 assert(0 && "Unexpected FP constant!");
414 ReplaceUses(SDValue(N, 0), V);
418 case ISD::FrameIndex: { // TODO: reduce creepyness
419 int FI = cast<FrameIndexSDNode>(N)->getIndex();
421 return CurDAG->SelectNodeTo(N, IA64::MOV, MVT::i64,
422 CurDAG->getTargetFrameIndex(FI, MVT::i64));
424 return CurDAG->getTargetNode(IA64::MOV, MVT::i64,
425 CurDAG->getTargetFrameIndex(FI, MVT::i64));
428 case ISD::ConstantPool: { // TODO: nuke the constant pool
429 // (ia64 doesn't need one)
430 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(N);
431 Constant *C = CP->getConstVal();
432 SDValue CPI = CurDAG->getTargetConstantPool(C, MVT::i64,
434 return CurDAG->getTargetNode(IA64::ADDL_GA, MVT::i64, // ?
435 CurDAG->getRegister(IA64::r1, MVT::i64), CPI);
438 case ISD::GlobalAddress: {
439 GlobalValue *GV = cast<GlobalAddressSDNode>(N)->getGlobal();
440 SDValue GA = CurDAG->getTargetGlobalAddress(GV, MVT::i64);
442 SDValue(CurDAG->getTargetNode(IA64::ADDL_GA, MVT::i64,
443 CurDAG->getRegister(IA64::r1,
445 return CurDAG->getTargetNode(IA64::LD8, MVT::i64, MVT::Other, Tmp,
446 CurDAG->getEntryNode());
450 case ISD::ExternalSymbol: {
451 SDValue EA = CurDAG->getTargetExternalSymbol(
452 cast<ExternalSymbolSDNode>(N)->getSymbol(),
454 SDValue Tmp = CurDAG->getTargetNode(IA64::ADDL_EA, MVT::i64,
455 CurDAG->getRegister(IA64::r1,
458 return CurDAG->getTargetNode(IA64::LD8, MVT::i64, Tmp);
462 case ISD::LOAD: { // FIXME: load -1, not 1, for bools?
463 LoadSDNode *LD = cast<LoadSDNode>(N);
464 SDValue Chain = LD->getChain();
465 SDValue Address = LD->getBasePtr();
466 AddToISelQueue(Chain);
467 AddToISelQueue(Address);
469 MVT TypeBeingLoaded = LD->getMemoryVT();
471 switch (TypeBeingLoaded.getSimpleVT()) {
476 assert(0 && "Cannot load this type!");
477 case MVT::i1: { // this is a bool
478 Opc = IA64::LD1; // first we load a byte, then compare for != 0
479 if(N->getValueType(0) == MVT::i1) { // XXX: early exit!
480 return CurDAG->SelectNodeTo(N, IA64::CMPNE, MVT::i1, MVT::Other,
481 SDValue(CurDAG->getTargetNode(Opc, MVT::i64, Address), 0),
482 CurDAG->getRegister(IA64::r0, MVT::i64),
485 /* otherwise, we want to load a bool into something bigger: LD1
486 will do that for us, so we just fall through */
488 case MVT::i8: Opc = IA64::LD1; break;
489 case MVT::i16: Opc = IA64::LD2; break;
490 case MVT::i32: Opc = IA64::LD4; break;
491 case MVT::i64: Opc = IA64::LD8; break;
493 case MVT::f32: Opc = IA64::LDF4; break;
494 case MVT::f64: Opc = IA64::LDF8; break;
497 // TODO: comment this
498 return CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), MVT::Other,
503 StoreSDNode *ST = cast<StoreSDNode>(N);
504 SDValue Address = ST->getBasePtr();
505 SDValue Chain = ST->getChain();
506 AddToISelQueue(Address);
507 AddToISelQueue(Chain);
510 if (ISD::isNON_TRUNCStore(N)) {
511 switch (N->getOperand(1).getValueType().getSimpleVT()) {
512 default: assert(0 && "unknown type in store");
513 case MVT::i1: { // this is a bool
514 Opc = IA64::ST1; // we store either 0 or 1 as a byte
516 SDValue Initial = CurDAG->getCopyFromReg(Chain, IA64::r0, MVT::i64);
517 Chain = Initial.getValue(1);
518 // then load 1 into the same reg iff the predicate to store is 1
519 SDValue Tmp = ST->getValue();
522 SDValue(CurDAG->getTargetNode(IA64::TPCADDS, MVT::i64, Initial,
523 CurDAG->getTargetConstant(1, MVT::i64),
525 return CurDAG->SelectNodeTo(N, Opc, MVT::Other, Address, Tmp, Chain);
527 case MVT::i64: Opc = IA64::ST8; break;
528 case MVT::f64: Opc = IA64::STF8; break;
530 } else { // Truncating store
531 switch(ST->getMemoryVT().getSimpleVT()) {
532 default: assert(0 && "unknown type in truncstore");
533 case MVT::i8: Opc = IA64::ST1; break;
534 case MVT::i16: Opc = IA64::ST2; break;
535 case MVT::i32: Opc = IA64::ST4; break;
536 case MVT::f32: Opc = IA64::STF4; break;
540 SDValue N1 = N->getOperand(1);
541 SDValue N2 = N->getOperand(2);
544 return CurDAG->SelectNodeTo(N, Opc, MVT::Other, N2, N1, Chain);
548 SDValue Chain = N->getOperand(0);
549 SDValue CC = N->getOperand(1);
550 AddToISelQueue(Chain);
552 MachineBasicBlock *Dest =
553 cast<BasicBlockSDNode>(N->getOperand(2))->getBasicBlock();
554 //FIXME - we do NOT need long branches all the time
555 return CurDAG->SelectNodeTo(N, IA64::BRLCOND_NOTCALL, MVT::Other, CC,
556 CurDAG->getBasicBlock(Dest), Chain);
559 case ISD::CALLSEQ_START:
560 case ISD::CALLSEQ_END: {
561 int64_t Amt = cast<ConstantSDNode>(N->getOperand(1))->getValue();
562 unsigned Opc = N->getOpcode() == ISD::CALLSEQ_START ?
563 IA64::ADJUSTCALLSTACKDOWN : IA64::ADJUSTCALLSTACKUP;
564 SDValue N0 = N->getOperand(0);
566 return CurDAG->SelectNodeTo(N, Opc, MVT::Other, getI64Imm(Amt), N0);
570 // FIXME: we don't need long branches all the time!
571 SDValue N0 = N->getOperand(0);
573 return CurDAG->SelectNodeTo(N, IA64::BRL_NOTCALL, MVT::Other,
574 N->getOperand(1), N0);
577 return SelectCode(Op);
581 /// createIA64DAGToDAGInstructionSelector - This pass converts a legalized DAG
582 /// into an IA64-specific DAG, ready for instruction scheduling.
585 *llvm::createIA64DAGToDAGInstructionSelector(IA64TargetMachine &TM) {
586 return new IA64DAGToDAGISel(TM);