1 //===-- PPCISelDAGToDAG.cpp - PPC --pattern matching inst selector --------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a pattern matching instruction selector for PowerPC,
11 // converting from a legalized dag to a PPC dag.
13 //===----------------------------------------------------------------------===//
16 #include "PPCTargetMachine.h"
17 #include "PPCISelLowering.h"
18 #include "PPCHazardRecognizers.h"
19 #include "llvm/CodeGen/MachineInstrBuilder.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/SSARegMap.h"
22 #include "llvm/CodeGen/SelectionDAG.h"
23 #include "llvm/CodeGen/SelectionDAGISel.h"
24 #include "llvm/Target/TargetOptions.h"
25 #include "llvm/ADT/Statistic.h"
26 #include "llvm/Constants.h"
27 #include "llvm/GlobalValue.h"
28 #include "llvm/Intrinsics.h"
29 #include "llvm/Support/Debug.h"
30 #include "llvm/Support/MathExtras.h"
31 #include "llvm/Support/Visibility.h"
37 Statistic<> FrameOff("ppc-codegen", "Number of frame idx offsets collapsed");
39 //===--------------------------------------------------------------------===//
40 /// PPCDAGToDAGISel - PPC specific code to select PPC machine
41 /// instructions for SelectionDAG operations.
43 class VISIBILITY_HIDDEN PPCDAGToDAGISel : public SelectionDAGISel {
45 PPCTargetLowering PPCLowering;
46 unsigned GlobalBaseReg;
48 PPCDAGToDAGISel(PPCTargetMachine &tm)
49 : SelectionDAGISel(PPCLowering), TM(tm),
50 PPCLowering(*TM.getTargetLowering()) {}
52 virtual bool runOnFunction(Function &Fn) {
53 // Make sure we re-emit a set of the global base reg if necessary
55 SelectionDAGISel::runOnFunction(Fn);
61 /// getI32Imm - Return a target constant with the specified value, of type
63 inline SDOperand getI32Imm(unsigned Imm) {
64 return CurDAG->getTargetConstant(Imm, MVT::i32);
67 /// getI64Imm - Return a target constant with the specified value, of type
69 inline SDOperand getI64Imm(uint64_t Imm) {
70 return CurDAG->getTargetConstant(Imm, MVT::i64);
73 /// getSmallIPtrImm - Return a target constant of pointer type.
74 inline SDOperand getSmallIPtrImm(unsigned Imm) {
75 return CurDAG->getTargetConstant(Imm, PPCLowering.getPointerTy());
79 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
80 /// base register. Return the virtual register that holds this value.
81 SDOperand getGlobalBaseReg();
83 // Select - Convert the specified operand from a target-independent to a
84 // target-specific node if it hasn't already been changed.
85 void Select(SDOperand &Result, SDOperand Op);
87 SDNode *SelectBitfieldInsert(SDNode *N);
89 /// SelectCC - Select a comparison of the specified values with the
90 /// specified condition code, returning the CR# of the expression.
91 SDOperand SelectCC(SDOperand LHS, SDOperand RHS, ISD::CondCode CC);
93 /// SelectAddrImm - Returns true if the address N can be represented by
94 /// a base register plus a signed 16-bit displacement [r+imm].
95 bool SelectAddrImm(SDOperand N, SDOperand &Disp, SDOperand &Base);
97 /// SelectAddrIdx - Given the specified addressed, check to see if it can be
98 /// represented as an indexed [r+r] operation. Returns false if it can
99 /// be represented by [r+imm], which are preferred.
100 bool SelectAddrIdx(SDOperand N, SDOperand &Base, SDOperand &Index);
102 /// SelectAddrIdxOnly - Given the specified addressed, force it to be
103 /// represented as an indexed [r+r] operation.
104 bool SelectAddrIdxOnly(SDOperand N, SDOperand &Base, SDOperand &Index);
106 /// SelectAddrImmShift - Returns true if the address N can be represented by
107 /// a base register plus a signed 14-bit displacement [r+imm*4]. Suitable
108 /// for use by STD and friends.
109 bool SelectAddrImmShift(SDOperand N, SDOperand &Disp, SDOperand &Base);
111 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
112 /// inline asm expressions.
113 virtual bool SelectInlineAsmMemoryOperand(const SDOperand &Op,
115 std::vector<SDOperand> &OutOps,
118 switch (ConstraintCode) {
119 default: return true;
121 if (!SelectAddrIdx(Op, Op0, Op1))
122 SelectAddrImm(Op, Op0, Op1);
124 case 'o': // offsetable
125 if (!SelectAddrImm(Op, Op0, Op1)) {
126 Select(Op0, Op); // r+0.
127 Op1 = getSmallIPtrImm(0);
130 case 'v': // not offsetable
131 SelectAddrIdxOnly(Op, Op0, Op1);
135 OutOps.push_back(Op0);
136 OutOps.push_back(Op1);
140 SDOperand BuildSDIVSequence(SDNode *N);
141 SDOperand BuildUDIVSequence(SDNode *N);
143 /// InstructionSelectBasicBlock - This callback is invoked by
144 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
145 virtual void InstructionSelectBasicBlock(SelectionDAG &DAG);
147 void InsertVRSaveCode(Function &Fn);
149 virtual const char *getPassName() const {
150 return "PowerPC DAG->DAG Pattern Instruction Selection";
153 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
154 /// this target when scheduling the DAG.
155 virtual HazardRecognizer *CreateTargetHazardRecognizer() {
156 // Should use subtarget info to pick the right hazard recognizer. For
157 // now, always return a PPC970 recognizer.
158 const TargetInstrInfo *II = PPCLowering.getTargetMachine().getInstrInfo();
159 assert(II && "No InstrInfo?");
160 return new PPCHazardRecognizer970(*II);
163 // Include the pieces autogenerated from the target description.
164 #include "PPCGenDAGISel.inc"
167 SDOperand SelectSETCC(SDOperand Op);
168 void MySelect_PPCbctrl(SDOperand &Result, SDOperand N);
169 void MySelect_PPCcall(SDOperand &Result, SDOperand N);
173 /// InstructionSelectBasicBlock - This callback is invoked by
174 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
175 void PPCDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) {
178 // The selection process is inherently a bottom-up recursive process (users
179 // select their uses before themselves). Given infinite stack space, we
180 // could just start selecting on the root and traverse the whole graph. In
181 // practice however, this causes us to run out of stack space on large basic
182 // blocks. To avoid this problem, select the entry node, then all its uses,
183 // iteratively instead of recursively.
184 std::vector<SDOperand> Worklist;
185 Worklist.push_back(DAG.getEntryNode());
187 // Note that we can do this in the PPC target (scanning forward across token
188 // chain edges) because no nodes ever get folded across these edges. On a
189 // target like X86 which supports load/modify/store operations, this would
190 // have to be more careful.
191 while (!Worklist.empty()) {
192 SDOperand Node = Worklist.back();
195 if ((Node.Val->getOpcode() >= ISD::BUILTIN_OP_END &&
196 Node.Val->getOpcode() < PPCISD::FIRST_NUMBER) ||
197 CodeGenMap.count(Node)) continue;
199 for (SDNode::use_iterator UI = Node.Val->use_begin(),
200 E = Node.Val->use_end(); UI != E; ++UI) {
201 // Scan the values. If this use has a value that is a token chain, add it
204 for (unsigned i = 0, e = User->getNumValues(); i != e; ++i)
205 if (User->getValueType(i) == MVT::Other) {
206 Worklist.push_back(SDOperand(User, i));
211 // Finally, legalize this node.
216 // Select target instructions for the DAG.
217 DAG.setRoot(SelectRoot(DAG.getRoot()));
218 assert(InFlightSet.empty() && "ISel InFlightSet has not been emptied!");
222 DAG.RemoveDeadNodes();
224 // Emit machine code to BB.
225 ScheduleAndEmitDAG(DAG);
228 /// InsertVRSaveCode - Once the entire function has been instruction selected,
229 /// all virtual registers are created and all machine instructions are built,
230 /// check to see if we need to save/restore VRSAVE. If so, do it.
231 void PPCDAGToDAGISel::InsertVRSaveCode(Function &F) {
232 // Check to see if this function uses vector registers, which means we have to
233 // save and restore the VRSAVE register and update it with the regs we use.
235 // In this case, there will be virtual registers of vector type type created
236 // by the scheduler. Detect them now.
237 MachineFunction &Fn = MachineFunction::get(&F);
238 SSARegMap *RegMap = Fn.getSSARegMap();
239 bool HasVectorVReg = false;
240 for (unsigned i = MRegisterInfo::FirstVirtualRegister,
241 e = RegMap->getLastVirtReg()+1; i != e; ++i)
242 if (RegMap->getRegClass(i) == &PPC::VRRCRegClass) {
243 HasVectorVReg = true;
246 if (!HasVectorVReg) return; // nothing to do.
248 // If we have a vector register, we want to emit code into the entry and exit
249 // blocks to save and restore the VRSAVE register. We do this here (instead
250 // of marking all vector instructions as clobbering VRSAVE) for two reasons:
252 // 1. This (trivially) reduces the load on the register allocator, by not
253 // having to represent the live range of the VRSAVE register.
254 // 2. This (more significantly) allows us to create a temporary virtual
255 // register to hold the saved VRSAVE value, allowing this temporary to be
256 // register allocated, instead of forcing it to be spilled to the stack.
258 // Create two vregs - one to hold the VRSAVE register that is live-in to the
259 // function and one for the value after having bits or'd into it.
260 unsigned InVRSAVE = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
261 unsigned UpdatedVRSAVE = RegMap->createVirtualRegister(&PPC::GPRCRegClass);
263 MachineBasicBlock &EntryBB = *Fn.begin();
264 // Emit the following code into the entry block:
265 // InVRSAVE = MFVRSAVE
266 // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE
267 // MTVRSAVE UpdatedVRSAVE
268 MachineBasicBlock::iterator IP = EntryBB.begin(); // Insert Point
269 BuildMI(EntryBB, IP, PPC::MFVRSAVE, 0, InVRSAVE);
270 BuildMI(EntryBB, IP, PPC::UPDATE_VRSAVE, 1, UpdatedVRSAVE).addReg(InVRSAVE);
271 BuildMI(EntryBB, IP, PPC::MTVRSAVE, 1).addReg(UpdatedVRSAVE);
273 // Find all return blocks, outputting a restore in each epilog.
274 const TargetInstrInfo &TII = *TM.getInstrInfo();
275 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
276 if (!BB->empty() && TII.isReturn(BB->back().getOpcode())) {
277 IP = BB->end(); --IP;
279 // Skip over all terminator instructions, which are part of the return
281 MachineBasicBlock::iterator I2 = IP;
282 while (I2 != BB->begin() && TII.isTerminatorInstr((--I2)->getOpcode()))
285 // Emit: MTVRSAVE InVRSave
286 BuildMI(*BB, IP, PPC::MTVRSAVE, 1).addReg(InVRSAVE);
292 /// getGlobalBaseReg - Output the instructions required to put the
293 /// base address to use for accessing globals into a register.
295 SDOperand PPCDAGToDAGISel::getGlobalBaseReg() {
296 if (!GlobalBaseReg) {
297 // Insert the set of GlobalBaseReg into the first MBB of the function
298 MachineBasicBlock &FirstMBB = BB->getParent()->front();
299 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
300 SSARegMap *RegMap = BB->getParent()->getSSARegMap();
302 if (PPCLowering.getPointerTy() == MVT::i32)
303 GlobalBaseReg = RegMap->createVirtualRegister(PPC::GPRCRegisterClass);
305 GlobalBaseReg = RegMap->createVirtualRegister(PPC::G8RCRegisterClass);
307 BuildMI(FirstMBB, MBBI, PPC::MovePCtoLR, 0, PPC::LR);
308 BuildMI(FirstMBB, MBBI, PPC::MFLR, 1, GlobalBaseReg);
310 return CurDAG->getRegister(GlobalBaseReg, PPCLowering.getPointerTy());
313 /// isIntS16Immediate - This method tests to see if the node is either a 32-bit
314 /// or 64-bit immediate, and if the value can be accurately represented as a
315 /// sign extension from a 16-bit value. If so, this returns true and the
317 static bool isIntS16Immediate(SDNode *N, short &Imm) {
318 if (N->getOpcode() != ISD::Constant)
321 Imm = (short)cast<ConstantSDNode>(N)->getValue();
322 if (N->getValueType(0) == MVT::i32)
323 return Imm == (int32_t)cast<ConstantSDNode>(N)->getValue();
325 return Imm == (int64_t)cast<ConstantSDNode>(N)->getValue();
328 static bool isIntS16Immediate(SDOperand Op, short &Imm) {
329 return isIntS16Immediate(Op.Val, Imm);
333 /// isInt32Immediate - This method tests to see if the node is a 32-bit constant
334 /// operand. If so Imm will receive the 32-bit value.
335 static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
336 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
337 Imm = cast<ConstantSDNode>(N)->getValue();
343 /// isInt64Immediate - This method tests to see if the node is a 64-bit constant
344 /// operand. If so Imm will receive the 64-bit value.
345 static bool isInt64Immediate(SDNode *N, uint64_t &Imm) {
346 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
347 Imm = cast<ConstantSDNode>(N)->getValue();
353 // isInt32Immediate - This method tests to see if a constant operand.
354 // If so Imm will receive the 32 bit value.
355 static bool isInt32Immediate(SDOperand N, unsigned &Imm) {
356 return isInt32Immediate(N.Val, Imm);
360 // isOpcWithIntImmediate - This method tests to see if the node is a specific
361 // opcode and that it has a immediate integer right operand.
362 // If so Imm will receive the 32 bit value.
363 static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
364 return N->getOpcode() == Opc && isInt32Immediate(N->getOperand(1).Val, Imm);
368 // isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s with
369 // any number of 0s on either side. The 1s are allowed to wrap from LSB to
370 // MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs. 0x0F0F0000 is
371 // not, since all 1s are not contiguous.
372 static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
373 if (isShiftedMask_32(Val)) {
374 // look for the first non-zero bit
375 MB = CountLeadingZeros_32(Val);
376 // look for the first zero bit after the run of ones
377 ME = CountLeadingZeros_32((Val - 1) ^ Val);
380 Val = ~Val; // invert mask
381 if (isShiftedMask_32(Val)) {
382 // effectively look for the first zero bit
383 ME = CountLeadingZeros_32(Val) - 1;
384 // effectively look for the first one bit after the run of zeros
385 MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1;
393 // isRotateAndMask - Returns true if Mask and Shift can be folded into a rotate
394 // and mask opcode and mask operation.
395 static bool isRotateAndMask(SDNode *N, unsigned Mask, bool IsShiftMask,
396 unsigned &SH, unsigned &MB, unsigned &ME) {
397 // Don't even go down this path for i64, since different logic will be
398 // necessary for rldicl/rldicr/rldimi.
399 if (N->getValueType(0) != MVT::i32)
403 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
404 unsigned Opcode = N->getOpcode();
405 if (N->getNumOperands() != 2 ||
406 !isInt32Immediate(N->getOperand(1).Val, Shift) || (Shift > 31))
409 if (Opcode == ISD::SHL) {
410 // apply shift left to mask if it comes first
411 if (IsShiftMask) Mask = Mask << Shift;
412 // determine which bits are made indeterminant by shift
413 Indeterminant = ~(0xFFFFFFFFu << Shift);
414 } else if (Opcode == ISD::SRL) {
415 // apply shift right to mask if it comes first
416 if (IsShiftMask) Mask = Mask >> Shift;
417 // determine which bits are made indeterminant by shift
418 Indeterminant = ~(0xFFFFFFFFu >> Shift);
419 // adjust for the left rotate
425 // if the mask doesn't intersect any Indeterminant bits
426 if (Mask && !(Mask & Indeterminant)) {
428 // make sure the mask is still a mask (wrap arounds may not be)
429 return isRunOfOnes(Mask, MB, ME);
434 /// SelectBitfieldInsert - turn an or of two masked values into
435 /// the rotate left word immediate then mask insert (rlwimi) instruction.
436 SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
437 SDOperand Op0 = N->getOperand(0);
438 SDOperand Op1 = N->getOperand(1);
440 uint64_t LKZ, LKO, RKZ, RKO;
441 TLI.ComputeMaskedBits(Op0, 0xFFFFFFFFULL, LKZ, LKO);
442 TLI.ComputeMaskedBits(Op1, 0xFFFFFFFFULL, RKZ, RKO);
444 unsigned TargetMask = LKZ;
445 unsigned InsertMask = RKZ;
447 if ((TargetMask | InsertMask) == 0xFFFFFFFF) {
448 unsigned Op0Opc = Op0.getOpcode();
449 unsigned Op1Opc = Op1.getOpcode();
450 unsigned Value, SH = 0;
451 TargetMask = ~TargetMask;
452 InsertMask = ~InsertMask;
454 // If the LHS has a foldable shift and the RHS does not, then swap it to the
455 // RHS so that we can fold the shift into the insert.
456 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
457 if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
458 Op0.getOperand(0).getOpcode() == ISD::SRL) {
459 if (Op1.getOperand(0).getOpcode() != ISD::SHL &&
460 Op1.getOperand(0).getOpcode() != ISD::SRL) {
462 std::swap(Op0Opc, Op1Opc);
463 std::swap(TargetMask, InsertMask);
466 } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) {
467 if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL &&
468 Op1.getOperand(0).getOpcode() != ISD::SRL) {
470 std::swap(Op0Opc, Op1Opc);
471 std::swap(TargetMask, InsertMask);
476 if (InsertMask && isRunOfOnes(InsertMask, MB, ME)) {
477 SDOperand Tmp1, Tmp2, Tmp3;
478 bool DisjointMask = (TargetMask ^ InsertMask) == 0xFFFFFFFF;
480 if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) &&
481 isInt32Immediate(Op1.getOperand(1), Value)) {
482 Op1 = Op1.getOperand(0);
483 SH = (Op1Opc == ISD::SHL) ? Value : 32 - Value;
485 if (Op1Opc == ISD::AND) {
486 unsigned SHOpc = Op1.getOperand(0).getOpcode();
487 if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) &&
488 isInt32Immediate(Op1.getOperand(0).getOperand(1), Value)) {
489 Op1 = Op1.getOperand(0).getOperand(0);
490 SH = (SHOpc == ISD::SHL) ? Value : 32 - Value;
492 Op1 = Op1.getOperand(0);
496 Tmp3 = (Op0Opc == ISD::AND && DisjointMask) ? Op0.getOperand(0) : Op0;
500 return CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32, Tmp1, Tmp2,
501 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME));
507 /// SelectAddrImm - Returns true if the address N can be represented by
508 /// a base register plus a signed 16-bit displacement [r+imm].
509 bool PPCDAGToDAGISel::SelectAddrImm(SDOperand N, SDOperand &Disp,
511 // If this can be more profitably realized as r+r, fail.
512 if (SelectAddrIdx(N, Disp, Base))
515 if (N.getOpcode() == ISD::ADD) {
517 if (isIntS16Immediate(N.getOperand(1), imm)) {
518 Disp = getI32Imm((int)imm & 0xFFFF);
519 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
520 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
522 Base = N.getOperand(0);
524 return true; // [r+i]
525 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
526 // Match LOAD (ADD (X, Lo(G))).
527 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getValue()
528 && "Cannot handle constant offsets yet!");
529 Disp = N.getOperand(1).getOperand(0); // The global address.
530 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
531 Disp.getOpcode() == ISD::TargetConstantPool ||
532 Disp.getOpcode() == ISD::TargetJumpTable);
533 Base = N.getOperand(0);
534 return true; // [&g+r]
536 } else if (N.getOpcode() == ISD::OR) {
538 if (isIntS16Immediate(N.getOperand(1), imm)) {
539 // If this is an or of disjoint bitfields, we can codegen this as an add
540 // (for better address arithmetic) if the LHS and RHS of the OR are
541 // provably disjoint.
542 uint64_t LHSKnownZero, LHSKnownOne;
543 PPCLowering.ComputeMaskedBits(N.getOperand(0), ~0U,
544 LHSKnownZero, LHSKnownOne);
545 if ((LHSKnownZero|~(unsigned)imm) == ~0U) {
546 // If all of the bits are known zero on the LHS or RHS, the add won't
548 Base = N.getOperand(0);
549 Disp = getI32Imm((int)imm & 0xFFFF);
553 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
554 // Loading from a constant address.
556 // If this address fits entirely in a 16-bit sext immediate field, codegen
559 if (isIntS16Immediate(CN, Imm)) {
560 Disp = CurDAG->getTargetConstant(Imm, CN->getValueType(0));
561 Base = CurDAG->getRegister(PPC::R0, CN->getValueType(0));
565 // FIXME: Handle small sext constant offsets in PPC64 mode also!
566 if (CN->getValueType(0) == MVT::i32) {
567 int Addr = (int)CN->getValue();
569 // Otherwise, break this down into an LIS + disp.
570 Disp = getI32Imm((short)Addr);
571 Base = CurDAG->getConstant(Addr - (signed short)Addr, MVT::i32);
576 Disp = getSmallIPtrImm(0);
577 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
578 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
581 return true; // [r+0]
584 /// SelectAddrIdx - Given the specified addressed, check to see if it can be
585 /// represented as an indexed [r+r] operation. Returns false if it can
586 /// be represented by [r+imm], which are preferred.
587 bool PPCDAGToDAGISel::SelectAddrIdx(SDOperand N, SDOperand &Base,
590 if (N.getOpcode() == ISD::ADD) {
591 if (isIntS16Immediate(N.getOperand(1), imm))
593 if (N.getOperand(1).getOpcode() == PPCISD::Lo)
596 Base = N.getOperand(0);
597 Index = N.getOperand(1);
599 } else if (N.getOpcode() == ISD::OR) {
600 if (isIntS16Immediate(N.getOperand(1), imm))
601 return false; // r+i can fold it if we can.
603 // If this is an or of disjoint bitfields, we can codegen this as an add
604 // (for better address arithmetic) if the LHS and RHS of the OR are provably
606 uint64_t LHSKnownZero, LHSKnownOne;
607 uint64_t RHSKnownZero, RHSKnownOne;
608 PPCLowering.ComputeMaskedBits(N.getOperand(0), ~0U,
609 LHSKnownZero, LHSKnownOne);
612 PPCLowering.ComputeMaskedBits(N.getOperand(1), ~0U,
613 RHSKnownZero, RHSKnownOne);
614 // If all of the bits are known zero on the LHS or RHS, the add won't
616 if ((LHSKnownZero | RHSKnownZero) == ~0U) {
617 Base = N.getOperand(0);
618 Index = N.getOperand(1);
627 /// SelectAddrIdxOnly - Given the specified addressed, force it to be
628 /// represented as an indexed [r+r] operation.
629 bool PPCDAGToDAGISel::SelectAddrIdxOnly(SDOperand N, SDOperand &Base,
631 // Check to see if we can easily represent this as an [r+r] address. This
632 // will fail if it thinks that the address is more profitably represented as
633 // reg+imm, e.g. where imm = 0.
634 if (SelectAddrIdx(N, Base, Index))
637 // If the operand is an addition, always emit this as [r+r], since this is
638 // better (for code size, and execution, as the memop does the add for free)
639 // than emitting an explicit add.
640 if (N.getOpcode() == ISD::ADD) {
641 Base = N.getOperand(0);
642 Index = N.getOperand(1);
646 // Otherwise, do it the hard way, using R0 as the base register.
647 Base = CurDAG->getRegister(PPC::R0, N.getValueType());
652 /// SelectAddrImmShift - Returns true if the address N can be represented by
653 /// a base register plus a signed 14-bit displacement [r+imm*4]. Suitable
654 /// for use by STD and friends.
655 bool PPCDAGToDAGISel::SelectAddrImmShift(SDOperand N, SDOperand &Disp,
657 // If this can be more profitably realized as r+r, fail.
658 if (SelectAddrIdx(N, Disp, Base))
661 if (N.getOpcode() == ISD::ADD) {
663 if (isIntS16Immediate(N.getOperand(1), imm) && (imm & 3) == 0) {
664 Disp = getI32Imm(((int)imm & 0xFFFF) >> 2);
665 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) {
666 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
668 Base = N.getOperand(0);
670 return true; // [r+i]
671 } else if (N.getOperand(1).getOpcode() == PPCISD::Lo) {
672 // Match LOAD (ADD (X, Lo(G))).
673 assert(!cast<ConstantSDNode>(N.getOperand(1).getOperand(1))->getValue()
674 && "Cannot handle constant offsets yet!");
675 Disp = N.getOperand(1).getOperand(0); // The global address.
676 assert(Disp.getOpcode() == ISD::TargetGlobalAddress ||
677 Disp.getOpcode() == ISD::TargetConstantPool ||
678 Disp.getOpcode() == ISD::TargetJumpTable);
679 Base = N.getOperand(0);
680 return true; // [&g+r]
682 } else if (N.getOpcode() == ISD::OR) {
684 if (isIntS16Immediate(N.getOperand(1), imm) && (imm & 3) == 0) {
685 // If this is an or of disjoint bitfields, we can codegen this as an add
686 // (for better address arithmetic) if the LHS and RHS of the OR are
687 // provably disjoint.
688 uint64_t LHSKnownZero, LHSKnownOne;
689 PPCLowering.ComputeMaskedBits(N.getOperand(0), ~0U,
690 LHSKnownZero, LHSKnownOne);
691 if ((LHSKnownZero|~(unsigned)imm) == ~0U) {
692 // If all of the bits are known zero on the LHS or RHS, the add won't
694 Base = N.getOperand(0);
695 Disp = getI32Imm(((int)imm & 0xFFFF) >> 2);
699 } else if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N)) {
700 // Loading from a constant address.
702 // If this address fits entirely in a 14-bit sext immediate field, codegen
705 if (isIntS16Immediate(CN, Imm)) {
706 Disp = getSmallIPtrImm((unsigned short)Imm >> 2);
707 Base = CurDAG->getRegister(PPC::R0, CN->getValueType(0));
711 // FIXME: Handle small sext constant offsets in PPC64 mode also!
712 if (CN->getValueType(0) == MVT::i32) {
713 int Addr = (int)CN->getValue();
715 // Otherwise, break this down into an LIS + disp.
716 Disp = getI32Imm((short)Addr >> 2);
717 Base = CurDAG->getConstant(Addr - (signed short)Addr, MVT::i32);
722 Disp = getSmallIPtrImm(0);
723 if (FrameIndexSDNode *FI = dyn_cast<FrameIndexSDNode>(N))
724 Base = CurDAG->getTargetFrameIndex(FI->getIndex(), N.getValueType());
727 return true; // [r+0]
731 /// SelectCC - Select a comparison of the specified values with the specified
732 /// condition code, returning the CR# of the expression.
733 SDOperand PPCDAGToDAGISel::SelectCC(SDOperand LHS, SDOperand RHS,
735 // Always select the LHS.
739 if (LHS.getValueType() == MVT::i32) {
741 if (ISD::isUnsignedIntSetCC(CC)) {
742 if (isInt32Immediate(RHS, Imm) && isUInt16(Imm))
743 return SDOperand(CurDAG->getTargetNode(PPC::CMPLWI, MVT::i32, LHS,
744 getI32Imm(Imm & 0xFFFF)), 0);
748 if (isIntS16Immediate(RHS, SImm))
749 return SDOperand(CurDAG->getTargetNode(PPC::CMPWI, MVT::i32, LHS,
750 getI32Imm((int)SImm & 0xFFFF)),
754 } else if (LHS.getValueType() == MVT::i64) {
756 if (ISD::isUnsignedIntSetCC(CC)) {
757 if (isInt64Immediate(RHS.Val, Imm) && isUInt16(Imm))
758 return SDOperand(CurDAG->getTargetNode(PPC::CMPLDI, MVT::i64, LHS,
759 getI64Imm(Imm & 0xFFFF)), 0);
763 if (isIntS16Immediate(RHS, SImm))
764 return SDOperand(CurDAG->getTargetNode(PPC::CMPDI, MVT::i64, LHS,
765 getI64Imm((int)SImm & 0xFFFF)),
769 } else if (LHS.getValueType() == MVT::f32) {
772 assert(LHS.getValueType() == MVT::f64 && "Unknown vt!");
776 return SDOperand(CurDAG->getTargetNode(Opc, MVT::i32, LHS, RHS), 0);
779 /// getBCCForSetCC - Returns the PowerPC condition branch mnemonic corresponding
781 static unsigned getBCCForSetCC(ISD::CondCode CC) {
783 default: assert(0 && "Unknown condition!"); abort();
784 case ISD::SETOEQ: // FIXME: This is incorrect see PR642.
786 case ISD::SETEQ: return PPC::BEQ;
787 case ISD::SETONE: // FIXME: This is incorrect see PR642.
789 case ISD::SETNE: return PPC::BNE;
790 case ISD::SETOLT: // FIXME: This is incorrect see PR642.
792 case ISD::SETLT: return PPC::BLT;
793 case ISD::SETOLE: // FIXME: This is incorrect see PR642.
795 case ISD::SETLE: return PPC::BLE;
796 case ISD::SETOGT: // FIXME: This is incorrect see PR642.
798 case ISD::SETGT: return PPC::BGT;
799 case ISD::SETOGE: // FIXME: This is incorrect see PR642.
801 case ISD::SETGE: return PPC::BGE;
803 case ISD::SETO: return PPC::BUN;
804 case ISD::SETUO: return PPC::BNU;
809 /// getCRIdxForSetCC - Return the index of the condition register field
810 /// associated with the SetCC condition, and whether or not the field is
811 /// treated as inverted. That is, lt = 0; ge = 0 inverted.
812 static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool& Inv) {
814 default: assert(0 && "Unknown condition!"); abort();
815 case ISD::SETOLT: // FIXME: This is incorrect see PR642.
817 case ISD::SETLT: Inv = false; return 0;
818 case ISD::SETOGE: // FIXME: This is incorrect see PR642.
820 case ISD::SETGE: Inv = true; return 0;
821 case ISD::SETOGT: // FIXME: This is incorrect see PR642.
823 case ISD::SETGT: Inv = false; return 1;
824 case ISD::SETOLE: // FIXME: This is incorrect see PR642.
826 case ISD::SETLE: Inv = true; return 1;
827 case ISD::SETOEQ: // FIXME: This is incorrect see PR642.
829 case ISD::SETEQ: Inv = false; return 2;
830 case ISD::SETONE: // FIXME: This is incorrect see PR642.
832 case ISD::SETNE: Inv = true; return 2;
833 case ISD::SETO: Inv = true; return 3;
834 case ISD::SETUO: Inv = false; return 3;
839 SDOperand PPCDAGToDAGISel::SelectSETCC(SDOperand Op) {
842 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
843 if (isInt32Immediate(N->getOperand(1), Imm)) {
844 // We can codegen setcc op, imm very efficiently compared to a brcond.
845 // Check for those cases here.
849 Select(Op, N->getOperand(0));
853 Op = SDOperand(CurDAG->getTargetNode(PPC::CNTLZW, MVT::i32, Op), 0);
854 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(27),
855 getI32Imm(5), getI32Imm(31));
858 SDOperand(CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
859 Op, getI32Imm(~0U)), 0);
860 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op,
864 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Op, getI32Imm(1),
865 getI32Imm(31), getI32Imm(31));
868 SDOperand(CurDAG->getTargetNode(PPC::NEG, MVT::i32, Op), 0);
869 T = SDOperand(CurDAG->getTargetNode(PPC::ANDC, MVT::i32, T, Op), 0);
870 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, T, getI32Imm(1),
871 getI32Imm(31), getI32Imm(31));
874 } else if (Imm == ~0U) { // setcc op, -1
876 Select(Op, N->getOperand(0));
880 Op = SDOperand(CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
881 Op, getI32Imm(1)), 0);
882 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
883 SDOperand(CurDAG->getTargetNode(PPC::LI, MVT::i32,
887 Op = SDOperand(CurDAG->getTargetNode(PPC::NOR, MVT::i32, Op, Op), 0);
888 SDNode *AD = CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
890 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDOperand(AD, 0),
891 Op, SDOperand(AD, 1));
894 SDOperand AD = SDOperand(CurDAG->getTargetNode(PPC::ADDI, MVT::i32, Op,
896 SDOperand AN = SDOperand(CurDAG->getTargetNode(PPC::AND, MVT::i32, AD,
898 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, AN, getI32Imm(1),
899 getI32Imm(31), getI32Imm(31));
902 Op = SDOperand(CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, Op,
903 getI32Imm(1), getI32Imm(31),
905 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op, getI32Imm(1));
911 unsigned Idx = getCRIdxForSetCC(CC, Inv);
912 SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC);
915 // Force the ccreg into CR7.
916 SDOperand CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
918 SDOperand InFlag(0, 0); // Null incoming flag value.
919 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), CR7Reg, CCReg,
922 if (TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor())
923 IntCR = SDOperand(CurDAG->getTargetNode(PPC::MFOCRF, MVT::i32, CR7Reg,
926 IntCR = SDOperand(CurDAG->getTargetNode(PPC::MFCR, MVT::i32, CCReg), 0);
929 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, IntCR,
930 getI32Imm((32-(3-Idx)) & 31),
931 getI32Imm(31), getI32Imm(31));
934 SDOperand(CurDAG->getTargetNode(PPC::RLWINM, MVT::i32, IntCR,
935 getI32Imm((32-(3-Idx)) & 31),
936 getI32Imm(31),getI32Imm(31)), 0);
937 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1));
942 // Select - Convert the specified operand from a target-independent to a
943 // target-specific node if it hasn't already been changed.
944 void PPCDAGToDAGISel::Select(SDOperand &Result, SDOperand Op) {
946 if (N->getOpcode() >= ISD::BUILTIN_OP_END &&
947 N->getOpcode() < PPCISD::FIRST_NUMBER) {
949 return; // Already selected.
952 // If this has already been converted, use it.
953 std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(Op);
954 if (CGMI != CodeGenMap.end()) {
955 Result = CGMI->second;
959 switch (N->getOpcode()) {
962 Result = SelectSETCC(Op);
964 case PPCISD::GlobalBaseReg:
965 Result = getGlobalBaseReg();
968 case ISD::FrameIndex: {
969 int FI = cast<FrameIndexSDNode>(N)->getIndex();
970 SDOperand TFI = CurDAG->getTargetFrameIndex(FI, Op.getValueType());
971 unsigned Opc = Op.getValueType() == MVT::i32 ? PPC::ADDI : PPC::ADDI8;
972 if (N->hasOneUse()) {
973 Result = CurDAG->SelectNodeTo(N, Opc, Op.getValueType(), TFI,
977 Result = CodeGenMap[Op] =
978 SDOperand(CurDAG->getTargetNode(Opc, Op.getValueType(), TFI,
979 getSmallIPtrImm(0)), 0);
985 Select(InFlag, N->getOperand(1));
986 // Use MFOCRF if supported.
987 if (TLI.getTargetMachine().getSubtarget<PPCSubtarget>().isGigaProcessor())
988 Result = SDOperand(CurDAG->getTargetNode(PPC::MFOCRF, MVT::i32,
989 N->getOperand(0), InFlag), 0);
991 Result = SDOperand(CurDAG->getTargetNode(PPC::MFCR, MVT::i32, InFlag), 0);
992 CodeGenMap[Op] = Result;
997 // FIXME: since this depends on the setting of the carry flag from the srawi
998 // we should really be making notes about that for the scheduler.
999 // FIXME: It sure would be nice if we could cheaply recognize the
1000 // srl/add/sra pattern the dag combiner will generate for this as
1001 // sra/addze rather than having to handle sdiv ourselves. oh well.
1003 if (isInt32Immediate(N->getOperand(1), Imm)) {
1005 Select(N0, N->getOperand(0));
1006 if ((signed)Imm > 0 && isPowerOf2_32(Imm)) {
1008 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
1009 N0, getI32Imm(Log2_32(Imm)));
1010 Result = CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
1011 SDOperand(Op, 0), SDOperand(Op, 1));
1012 } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) {
1014 CurDAG->getTargetNode(PPC::SRAWI, MVT::i32, MVT::Flag,
1015 N0, getI32Imm(Log2_32(-Imm)));
1017 SDOperand(CurDAG->getTargetNode(PPC::ADDZE, MVT::i32,
1018 SDOperand(Op, 0), SDOperand(Op, 1)),
1020 Result = CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT);
1025 // Other cases are autogenerated.
1030 // If this is an and of a value rotated between 0 and 31 bits and then and'd
1031 // with a mask, emit rlwinm
1032 if (isInt32Immediate(N->getOperand(1), Imm) &&
1033 (isShiftedMask_32(Imm) || isShiftedMask_32(~Imm))) {
1035 unsigned SH, MB, ME;
1036 if (isRotateAndMask(N->getOperand(0).Val, Imm, false, SH, MB, ME)) {
1037 Select(Val, N->getOperand(0).getOperand(0));
1038 } else if (Imm == 0) {
1039 // AND X, 0 -> 0, not "rlwinm 32".
1040 Select(Result, N->getOperand(1));
1043 Select(Val, N->getOperand(0));
1044 isRunOfOnes(Imm, MB, ME);
1047 Result = CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Val,
1048 getI32Imm(SH), getI32Imm(MB),
1052 // ISD::OR doesn't get all the bitfield insertion fun.
1053 // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) is a bitfield insert
1054 if (isInt32Immediate(N->getOperand(1), Imm) &&
1055 N->getOperand(0).getOpcode() == ISD::OR &&
1056 isInt32Immediate(N->getOperand(0).getOperand(1), Imm2)) {
1059 if (isRunOfOnes(Imm, MB, ME)) {
1060 SDOperand Tmp1, Tmp2;
1061 Select(Tmp1, N->getOperand(0).getOperand(0));
1062 Select(Tmp2, N->getOperand(0).getOperand(1));
1063 Result = SDOperand(CurDAG->getTargetNode(PPC::RLWIMI, MVT::i32,
1065 getI32Imm(0), getI32Imm(MB),
1071 // Other cases are autogenerated.
1075 if (N->getValueType(0) == MVT::i32)
1076 if (SDNode *I = SelectBitfieldInsert(N)) {
1077 Result = CodeGenMap[Op] = SDOperand(I, 0);
1081 // Other cases are autogenerated.
1084 unsigned Imm, SH, MB, ME;
1085 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
1086 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
1088 Select(Val, N->getOperand(0).getOperand(0));
1089 Result = CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
1090 Val, getI32Imm(SH), getI32Imm(MB),
1095 // Other cases are autogenerated.
1099 unsigned Imm, SH, MB, ME;
1100 if (isOpcWithIntImmediate(N->getOperand(0).Val, ISD::AND, Imm) &&
1101 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
1103 Select(Val, N->getOperand(0).getOperand(0));
1104 Result = CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32,
1105 Val, getI32Imm(SH), getI32Imm(MB),
1110 // Other cases are autogenerated.
1113 case ISD::SELECT_CC: {
1114 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
1116 // Handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
1117 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1118 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1119 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1120 if (N1C->isNullValue() && N3C->isNullValue() &&
1121 N2C->getValue() == 1ULL && CC == ISD::SETNE &&
1122 // FIXME: Implement this optzn for PPC64.
1123 N->getValueType(0) == MVT::i32) {
1125 Select(LHS, N->getOperand(0));
1127 CurDAG->getTargetNode(PPC::ADDIC, MVT::i32, MVT::Flag,
1128 LHS, getI32Imm(~0U));
1129 Result = CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32,
1130 SDOperand(Tmp, 0), LHS,
1135 SDOperand CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC);
1136 unsigned BROpc = getBCCForSetCC(CC);
1138 bool isFP = MVT::isFloatingPoint(N->getValueType(0));
1139 unsigned SelectCCOp;
1140 if (N->getValueType(0) == MVT::i32)
1141 SelectCCOp = PPC::SELECT_CC_I4;
1142 else if (N->getValueType(0) == MVT::i64)
1143 SelectCCOp = PPC::SELECT_CC_I8;
1144 else if (N->getValueType(0) == MVT::f32)
1145 SelectCCOp = PPC::SELECT_CC_F4;
1146 else if (N->getValueType(0) == MVT::f64)
1147 SelectCCOp = PPC::SELECT_CC_F8;
1149 SelectCCOp = PPC::SELECT_CC_VRRC;
1152 Select(N2, N->getOperand(2));
1153 Select(N3, N->getOperand(3));
1154 Result = CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), CCReg,
1155 N2, N3, getI32Imm(BROpc));
1160 Select(Chain, N->getOperand(0));
1161 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
1162 SDOperand CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC);
1163 Result = CurDAG->SelectNodeTo(N, PPC::COND_BRANCH, MVT::Other,
1164 CondCode, getI32Imm(getBCCForSetCC(CC)),
1165 N->getOperand(4), Chain);
1169 // FIXME: Should custom lower this.
1170 SDOperand Chain, Target;
1171 Select(Chain, N->getOperand(0));
1172 Select(Target,N->getOperand(1));
1173 unsigned Opc = Target.getValueType() == MVT::i32 ? PPC::MTCTR : PPC::MTCTR8;
1174 Chain = SDOperand(CurDAG->getTargetNode(Opc, MVT::Other, Target,
1176 Result = CurDAG->SelectNodeTo(N, PPC::BCTR, MVT::Other, Chain);
1179 // FIXME: These are manually selected because tblgen isn't handling varargs
1181 case PPCISD::BCTRL: MySelect_PPCbctrl(Result, Op); return;
1182 case PPCISD::CALL: MySelect_PPCcall(Result, Op); return;
1185 SelectCode(Result, Op);
1189 // FIXME: This is manually selected because tblgen isn't handling varargs nodes
1191 void PPCDAGToDAGISel::MySelect_PPCbctrl(SDOperand &Result, SDOperand N) {
1192 SDOperand Chain(0, 0);
1193 SDOperand InFlag(0, 0);
1197 N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag;
1199 std::vector<SDOperand> Ops;
1200 // Push varargs arguments, including optional flag.
1201 for (unsigned i = 1, e = N.getNumOperands()-hasFlag; i != e; ++i) {
1202 Select(Chain, N.getOperand(i));
1203 Ops.push_back(Chain);
1206 Select(Chain, N.getOperand(0));
1207 Ops.push_back(Chain);
1210 Select(Chain, N.getOperand(N.getNumOperands()-1));
1211 Ops.push_back(Chain);
1214 ResNode = CurDAG->getTargetNode(PPC::BCTRL, MVT::Other, MVT::Flag, Ops);
1215 Chain = SDOperand(ResNode, 0);
1216 InFlag = SDOperand(ResNode, 1);
1217 SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 0, Chain.Val,
1219 SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 1, InFlag.Val,
1221 Result = SDOperand(ResNode, N.ResNo);
1225 // FIXME: This is manually selected because tblgen isn't handling varargs nodes
1227 void PPCDAGToDAGISel::MySelect_PPCcall(SDOperand &Result, SDOperand N) {
1228 SDOperand Chain(0, 0);
1229 SDOperand InFlag(0, 0);
1231 SDOperand Tmp0(0, 0);
1233 Chain = N.getOperand(0);
1234 N1 = N.getOperand(1);
1236 // Pattern: (PPCcall:void (imm:i32):$func)
1237 // Emits: (BLA:void (imm:i32):$func)
1238 // Pattern complexity = 4 cost = 1
1239 if (N1.getOpcode() == ISD::Constant) {
1240 unsigned Tmp0C = (unsigned)cast<ConstantSDNode>(N1)->getValue();
1242 std::vector<SDOperand> Ops;
1243 Ops.push_back(CurDAG->getTargetConstant(Tmp0C, MVT::i32));
1246 N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag;
1248 // Push varargs arguments, not including optional flag.
1249 for (unsigned i = 2, e = N.getNumOperands()-hasFlag; i != e; ++i) {
1250 Select(Chain, N.getOperand(i));
1251 Ops.push_back(Chain);
1253 Select(Chain, N.getOperand(0));
1254 Ops.push_back(Chain);
1256 Select(Chain, N.getOperand(N.getNumOperands()-1));
1257 Ops.push_back(Chain);
1259 ResNode = CurDAG->getTargetNode(PPC::BLA, MVT::Other, MVT::Flag, Ops);
1261 Chain = SDOperand(ResNode, 0);
1262 InFlag = SDOperand(ResNode, 1);
1263 SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 0, Chain.Val,
1265 SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 1, InFlag.Val,
1267 Result = SDOperand(ResNode, N.ResNo);
1271 // Pattern: (PPCcall:void (tglobaladdr:i32):$dst)
1272 // Emits: (BL:void (tglobaladdr:i32):$dst)
1273 // Pattern complexity = 4 cost = 1
1274 if (N1.getOpcode() == ISD::TargetGlobalAddress) {
1275 std::vector<SDOperand> Ops;
1279 N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag;
1281 // Push varargs arguments, not including optional flag.
1282 for (unsigned i = 2, e = N.getNumOperands()-hasFlag; i != e; ++i) {
1283 Select(Chain, N.getOperand(i));
1284 Ops.push_back(Chain);
1286 Select(Chain, N.getOperand(0));
1287 Ops.push_back(Chain);
1289 Select(Chain, N.getOperand(N.getNumOperands()-1));
1290 Ops.push_back(Chain);
1293 ResNode = CurDAG->getTargetNode(PPC::BL, MVT::Other, MVT::Flag, Ops);
1295 Chain = SDOperand(ResNode, 0);
1296 InFlag = SDOperand(ResNode, 1);
1297 SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 0, Chain.Val,
1299 SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 1, InFlag.Val,
1301 Result = SDOperand(ResNode, N.ResNo);
1305 // Pattern: (PPCcall:void (texternalsym:i32):$dst)
1306 // Emits: (BL:void (texternalsym:i32):$dst)
1307 // Pattern complexity = 4 cost = 1
1308 if (N1.getOpcode() == ISD::TargetExternalSymbol) {
1309 std::vector<SDOperand> Ops;
1313 N.getOperand(N.getNumOperands()-1).getValueType() == MVT::Flag;
1315 // Push varargs arguments, not including optional flag.
1316 for (unsigned i = 2, e = N.getNumOperands()-hasFlag; i != e; ++i) {
1317 Select(Chain, N.getOperand(i));
1318 Ops.push_back(Chain);
1320 Select(Chain, N.getOperand(0));
1321 Ops.push_back(Chain);
1323 Select(Chain, N.getOperand(N.getNumOperands()-1));
1324 Ops.push_back(Chain);
1327 ResNode = CurDAG->getTargetNode(PPC::BL, MVT::Other, MVT::Flag, Ops);
1329 Chain = SDOperand(ResNode, 0);
1330 InFlag = SDOperand(ResNode, 1);
1331 SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 0, Chain.Val,
1333 SelectionDAG::InsertISelMapEntry(CodeGenMap, N.Val, 1, InFlag.Val,
1335 Result = SDOperand(ResNode, N.ResNo);
1338 std::cerr << "Cannot yet select: ";
1339 N.Val->dump(CurDAG);
1345 /// createPPCISelDag - This pass converts a legalized DAG into a
1346 /// PowerPC-specific DAG, ready for instruction scheduling.
1348 FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) {
1349 return new PPCDAGToDAGISel(TM);