1 //===-- PPCISelDAGToDAG.cpp - PPC --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 PowerPC,
11 // converting from a legalized dag to a PPC dag.
13 //===----------------------------------------------------------------------===//
15 #define DEBUG_TYPE "ppc-codegen"
17 #include "MCTargetDesc/PPCPredicates.h"
18 #include "PPCTargetMachine.h"
19 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/CodeGen/MachineInstrBuilder.h"
21 #include "llvm/CodeGen/MachineRegisterInfo.h"
22 #include "llvm/CodeGen/SelectionDAG.h"
23 #include "llvm/CodeGen/SelectionDAGISel.h"
24 #include "llvm/IR/Constants.h"
25 #include "llvm/IR/Function.h"
26 #include "llvm/IR/GlobalAlias.h"
27 #include "llvm/IR/GlobalValue.h"
28 #include "llvm/IR/GlobalVariable.h"
29 #include "llvm/IR/Intrinsics.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Support/MathExtras.h"
33 #include "llvm/Support/raw_ostream.h"
34 #include "llvm/Target/TargetOptions.h"
38 void initializePPCDAGToDAGISelPass(PassRegistry&);
42 //===--------------------------------------------------------------------===//
43 /// PPCDAGToDAGISel - PPC specific code to select PPC machine
44 /// instructions for SelectionDAG operations.
46 class PPCDAGToDAGISel : public SelectionDAGISel {
47 const PPCTargetMachine &TM;
48 const PPCTargetLowering &PPCLowering;
49 const PPCSubtarget &PPCSubTarget;
50 unsigned GlobalBaseReg;
52 explicit PPCDAGToDAGISel(PPCTargetMachine &tm)
53 : SelectionDAGISel(tm), TM(tm),
54 PPCLowering(*TM.getTargetLowering()),
55 PPCSubTarget(*TM.getSubtargetImpl()) {
56 initializePPCDAGToDAGISelPass(*PassRegistry::getPassRegistry());
59 virtual bool runOnMachineFunction(MachineFunction &MF) {
60 // Make sure we re-emit a set of the global base reg if necessary
62 SelectionDAGISel::runOnMachineFunction(MF);
64 if (!PPCSubTarget.isSVR4ABI())
70 virtual void PostprocessISelDAG();
72 /// getI32Imm - Return a target constant with the specified value, of type
74 inline SDValue getI32Imm(unsigned Imm) {
75 return CurDAG->getTargetConstant(Imm, MVT::i32);
78 /// getI64Imm - Return a target constant with the specified value, of type
80 inline SDValue getI64Imm(uint64_t Imm) {
81 return CurDAG->getTargetConstant(Imm, MVT::i64);
84 /// getSmallIPtrImm - Return a target constant of pointer type.
85 inline SDValue getSmallIPtrImm(unsigned Imm) {
86 return CurDAG->getTargetConstant(Imm, PPCLowering.getPointerTy());
89 /// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s
90 /// with any number of 0s on either side. The 1s are allowed to wrap from
91 /// LSB to MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs.
92 /// 0x0F0F0000 is not, since all 1s are not contiguous.
93 static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME);
96 /// isRotateAndMask - Returns true if Mask and Shift can be folded into a
97 /// rotate and mask opcode and mask operation.
98 static bool isRotateAndMask(SDNode *N, unsigned Mask, bool isShiftMask,
99 unsigned &SH, unsigned &MB, unsigned &ME);
101 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
102 /// base register. Return the virtual register that holds this value.
103 SDNode *getGlobalBaseReg();
105 // Select - Convert the specified operand from a target-independent to a
106 // target-specific node if it hasn't already been changed.
107 SDNode *Select(SDNode *N);
109 SDNode *SelectBitfieldInsert(SDNode *N);
111 /// SelectCC - Select a comparison of the specified values with the
112 /// specified condition code, returning the CR# of the expression.
113 SDValue SelectCC(SDValue LHS, SDValue RHS, ISD::CondCode CC, SDLoc dl);
115 /// SelectAddrImm - Returns true if the address N can be represented by
116 /// a base register plus a signed 16-bit displacement [r+imm].
117 bool SelectAddrImm(SDValue N, SDValue &Disp,
119 return PPCLowering.SelectAddressRegImm(N, Disp, Base, *CurDAG, false);
122 /// SelectAddrImmOffs - Return true if the operand is valid for a preinc
123 /// immediate field. Note that the operand at this point is already the
124 /// result of a prior SelectAddressRegImm call.
125 bool SelectAddrImmOffs(SDValue N, SDValue &Out) const {
126 if (N.getOpcode() == ISD::TargetConstant ||
127 N.getOpcode() == ISD::TargetGlobalAddress) {
135 /// SelectAddrIdx - Given the specified addressed, check to see if it can be
136 /// represented as an indexed [r+r] operation. Returns false if it can
137 /// be represented by [r+imm], which are preferred.
138 bool SelectAddrIdx(SDValue N, SDValue &Base, SDValue &Index) {
139 return PPCLowering.SelectAddressRegReg(N, Base, Index, *CurDAG);
142 /// SelectAddrIdxOnly - Given the specified addressed, force it to be
143 /// represented as an indexed [r+r] operation.
144 bool SelectAddrIdxOnly(SDValue N, SDValue &Base, SDValue &Index) {
145 return PPCLowering.SelectAddressRegRegOnly(N, Base, Index, *CurDAG);
148 /// SelectAddrImmX4 - Returns true if the address N can be represented by
149 /// a base register plus a signed 16-bit displacement that is a multiple of 4.
150 /// Suitable for use by STD and friends.
151 bool SelectAddrImmX4(SDValue N, SDValue &Disp, SDValue &Base) {
152 return PPCLowering.SelectAddressRegImm(N, Disp, Base, *CurDAG, true);
155 // Select an address into a single register.
156 bool SelectAddr(SDValue N, SDValue &Base) {
161 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
162 /// inline asm expressions. It is always correct to compute the value into
163 /// a register. The case of adding a (possibly relocatable) constant to a
164 /// register can be improved, but it is wrong to substitute Reg+Reg for
165 /// Reg in an asm, because the load or store opcode would have to change.
166 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
168 std::vector<SDValue> &OutOps) {
169 OutOps.push_back(Op);
173 void InsertVRSaveCode(MachineFunction &MF);
175 virtual const char *getPassName() const {
176 return "PowerPC DAG->DAG Pattern Instruction Selection";
179 // Include the pieces autogenerated from the target description.
180 #include "PPCGenDAGISel.inc"
183 SDNode *SelectSETCC(SDNode *N);
187 /// InsertVRSaveCode - Once the entire function has been instruction selected,
188 /// all virtual registers are created and all machine instructions are built,
189 /// check to see if we need to save/restore VRSAVE. If so, do it.
190 void PPCDAGToDAGISel::InsertVRSaveCode(MachineFunction &Fn) {
191 // Check to see if this function uses vector registers, which means we have to
192 // save and restore the VRSAVE register and update it with the regs we use.
194 // In this case, there will be virtual registers of vector type created
195 // by the scheduler. Detect them now.
196 bool HasVectorVReg = false;
197 for (unsigned i = 0, e = RegInfo->getNumVirtRegs(); i != e; ++i) {
198 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
199 if (RegInfo->getRegClass(Reg) == &PPC::VRRCRegClass) {
200 HasVectorVReg = true;
204 if (!HasVectorVReg) return; // nothing to do.
206 // If we have a vector register, we want to emit code into the entry and exit
207 // blocks to save and restore the VRSAVE register. We do this here (instead
208 // of marking all vector instructions as clobbering VRSAVE) for two reasons:
210 // 1. This (trivially) reduces the load on the register allocator, by not
211 // having to represent the live range of the VRSAVE register.
212 // 2. This (more significantly) allows us to create a temporary virtual
213 // register to hold the saved VRSAVE value, allowing this temporary to be
214 // register allocated, instead of forcing it to be spilled to the stack.
216 // Create two vregs - one to hold the VRSAVE register that is live-in to the
217 // function and one for the value after having bits or'd into it.
218 unsigned InVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
219 unsigned UpdatedVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
221 const TargetInstrInfo &TII = *TM.getInstrInfo();
222 MachineBasicBlock &EntryBB = *Fn.begin();
224 // Emit the following code into the entry block:
225 // InVRSAVE = MFVRSAVE
226 // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE
227 // MTVRSAVE UpdatedVRSAVE
228 MachineBasicBlock::iterator IP = EntryBB.begin(); // Insert Point
229 BuildMI(EntryBB, IP, dl, TII.get(PPC::MFVRSAVE), InVRSAVE);
230 BuildMI(EntryBB, IP, dl, TII.get(PPC::UPDATE_VRSAVE),
231 UpdatedVRSAVE).addReg(InVRSAVE);
232 BuildMI(EntryBB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(UpdatedVRSAVE);
234 // Find all return blocks, outputting a restore in each epilog.
235 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
236 if (!BB->empty() && BB->back().isReturn()) {
237 IP = BB->end(); --IP;
239 // Skip over all terminator instructions, which are part of the return
241 MachineBasicBlock::iterator I2 = IP;
242 while (I2 != BB->begin() && (--I2)->isTerminator())
245 // Emit: MTVRSAVE InVRSave
246 BuildMI(*BB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(InVRSAVE);
252 /// getGlobalBaseReg - Output the instructions required to put the
253 /// base address to use for accessing globals into a register.
255 SDNode *PPCDAGToDAGISel::getGlobalBaseReg() {
256 if (!GlobalBaseReg) {
257 const TargetInstrInfo &TII = *TM.getInstrInfo();
258 // Insert the set of GlobalBaseReg into the first MBB of the function
259 MachineBasicBlock &FirstMBB = MF->front();
260 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
263 if (PPCLowering.getPointerTy() == MVT::i32) {
264 GlobalBaseReg = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
265 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR));
266 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
268 GlobalBaseReg = RegInfo->createVirtualRegister(&PPC::G8RCRegClass);
269 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR8));
270 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR8), GlobalBaseReg);
273 return CurDAG->getRegister(GlobalBaseReg,
274 PPCLowering.getPointerTy()).getNode();
277 /// isIntS16Immediate - This method tests to see if the node is either a 32-bit
278 /// or 64-bit immediate, and if the value can be accurately represented as a
279 /// sign extension from a 16-bit value. If so, this returns true and the
281 static bool isIntS16Immediate(SDNode *N, short &Imm) {
282 if (N->getOpcode() != ISD::Constant)
285 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
286 if (N->getValueType(0) == MVT::i32)
287 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
289 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
292 static bool isIntS16Immediate(SDValue Op, short &Imm) {
293 return isIntS16Immediate(Op.getNode(), Imm);
297 /// isInt32Immediate - This method tests to see if the node is a 32-bit constant
298 /// operand. If so Imm will receive the 32-bit value.
299 static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
300 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
301 Imm = cast<ConstantSDNode>(N)->getZExtValue();
307 /// isInt64Immediate - This method tests to see if the node is a 64-bit constant
308 /// operand. If so Imm will receive the 64-bit value.
309 static bool isInt64Immediate(SDNode *N, uint64_t &Imm) {
310 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i64) {
311 Imm = cast<ConstantSDNode>(N)->getZExtValue();
317 // isInt32Immediate - This method tests to see if a constant operand.
318 // If so Imm will receive the 32 bit value.
319 static bool isInt32Immediate(SDValue N, unsigned &Imm) {
320 return isInt32Immediate(N.getNode(), Imm);
324 // isOpcWithIntImmediate - This method tests to see if the node is a specific
325 // opcode and that it has a immediate integer right operand.
326 // If so Imm will receive the 32 bit value.
327 static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
328 return N->getOpcode() == Opc
329 && isInt32Immediate(N->getOperand(1).getNode(), Imm);
332 bool PPCDAGToDAGISel::isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
333 if (isShiftedMask_32(Val)) {
334 // look for the first non-zero bit
335 MB = countLeadingZeros(Val);
336 // look for the first zero bit after the run of ones
337 ME = countLeadingZeros((Val - 1) ^ Val);
340 Val = ~Val; // invert mask
341 if (isShiftedMask_32(Val)) {
342 // effectively look for the first zero bit
343 ME = countLeadingZeros(Val) - 1;
344 // effectively look for the first one bit after the run of zeros
345 MB = countLeadingZeros((Val - 1) ^ Val) + 1;
353 bool PPCDAGToDAGISel::isRotateAndMask(SDNode *N, unsigned Mask,
354 bool isShiftMask, unsigned &SH,
355 unsigned &MB, unsigned &ME) {
356 // Don't even go down this path for i64, since different logic will be
357 // necessary for rldicl/rldicr/rldimi.
358 if (N->getValueType(0) != MVT::i32)
362 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
363 unsigned Opcode = N->getOpcode();
364 if (N->getNumOperands() != 2 ||
365 !isInt32Immediate(N->getOperand(1).getNode(), Shift) || (Shift > 31))
368 if (Opcode == ISD::SHL) {
369 // apply shift left to mask if it comes first
370 if (isShiftMask) Mask = Mask << Shift;
371 // determine which bits are made indeterminant by shift
372 Indeterminant = ~(0xFFFFFFFFu << Shift);
373 } else if (Opcode == ISD::SRL) {
374 // apply shift right to mask if it comes first
375 if (isShiftMask) Mask = Mask >> Shift;
376 // determine which bits are made indeterminant by shift
377 Indeterminant = ~(0xFFFFFFFFu >> Shift);
378 // adjust for the left rotate
380 } else if (Opcode == ISD::ROTL) {
386 // if the mask doesn't intersect any Indeterminant bits
387 if (Mask && !(Mask & Indeterminant)) {
389 // make sure the mask is still a mask (wrap arounds may not be)
390 return isRunOfOnes(Mask, MB, ME);
395 /// SelectBitfieldInsert - turn an or of two masked values into
396 /// the rotate left word immediate then mask insert (rlwimi) instruction.
397 SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
398 SDValue Op0 = N->getOperand(0);
399 SDValue Op1 = N->getOperand(1);
402 APInt LKZ, LKO, RKZ, RKO;
403 CurDAG->ComputeMaskedBits(Op0, LKZ, LKO);
404 CurDAG->ComputeMaskedBits(Op1, RKZ, RKO);
406 unsigned TargetMask = LKZ.getZExtValue();
407 unsigned InsertMask = RKZ.getZExtValue();
409 if ((TargetMask | InsertMask) == 0xFFFFFFFF) {
410 unsigned Op0Opc = Op0.getOpcode();
411 unsigned Op1Opc = Op1.getOpcode();
412 unsigned Value, SH = 0;
413 TargetMask = ~TargetMask;
414 InsertMask = ~InsertMask;
416 // If the LHS has a foldable shift and the RHS does not, then swap it to the
417 // RHS so that we can fold the shift into the insert.
418 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
419 if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
420 Op0.getOperand(0).getOpcode() == ISD::SRL) {
421 if (Op1.getOperand(0).getOpcode() != ISD::SHL &&
422 Op1.getOperand(0).getOpcode() != ISD::SRL) {
424 std::swap(Op0Opc, Op1Opc);
425 std::swap(TargetMask, InsertMask);
428 } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) {
429 if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL &&
430 Op1.getOperand(0).getOpcode() != ISD::SRL) {
432 std::swap(Op0Opc, Op1Opc);
433 std::swap(TargetMask, InsertMask);
438 if (InsertMask && isRunOfOnes(InsertMask, MB, ME)) {
441 if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) &&
442 isInt32Immediate(Op1.getOperand(1), Value)) {
443 Op1 = Op1.getOperand(0);
444 SH = (Op1Opc == ISD::SHL) ? Value : 32 - Value;
446 if (Op1Opc == ISD::AND) {
447 unsigned SHOpc = Op1.getOperand(0).getOpcode();
448 if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) &&
449 isInt32Immediate(Op1.getOperand(0).getOperand(1), Value)) {
450 // Note that Value must be in range here (less than 32) because
451 // otherwise there would not be any bits set in InsertMask.
452 Op1 = Op1.getOperand(0).getOperand(0);
453 SH = (SHOpc == ISD::SHL) ? Value : 32 - Value;
458 SDValue Ops[] = { Op0, Op1, getI32Imm(SH), getI32Imm(MB),
460 return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops);
466 /// SelectCC - Select a comparison of the specified values with the specified
467 /// condition code, returning the CR# of the expression.
468 SDValue PPCDAGToDAGISel::SelectCC(SDValue LHS, SDValue RHS,
469 ISD::CondCode CC, SDLoc dl) {
470 // Always select the LHS.
473 if (LHS.getValueType() == MVT::i32) {
475 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
476 if (isInt32Immediate(RHS, Imm)) {
477 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
479 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
480 getI32Imm(Imm & 0xFFFF)), 0);
481 // If this is a 16-bit signed immediate, fold it.
482 if (isInt<16>((int)Imm))
483 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
484 getI32Imm(Imm & 0xFFFF)), 0);
486 // For non-equality comparisons, the default code would materialize the
487 // constant, then compare against it, like this:
491 // Since we are just comparing for equality, we can emit this instead:
492 // xoris r0,r3,0x1234
493 // cmplwi cr0,r0,0x5678
495 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS, dl, MVT::i32, LHS,
496 getI32Imm(Imm >> 16)), 0);
497 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, Xor,
498 getI32Imm(Imm & 0xFFFF)), 0);
501 } else if (ISD::isUnsignedIntSetCC(CC)) {
502 if (isInt32Immediate(RHS, Imm) && isUInt<16>(Imm))
503 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
504 getI32Imm(Imm & 0xFFFF)), 0);
508 if (isIntS16Immediate(RHS, SImm))
509 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
510 getI32Imm((int)SImm & 0xFFFF)),
514 } else if (LHS.getValueType() == MVT::i64) {
516 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
517 if (isInt64Immediate(RHS.getNode(), Imm)) {
518 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
520 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
521 getI32Imm(Imm & 0xFFFF)), 0);
522 // If this is a 16-bit signed immediate, fold it.
524 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
525 getI32Imm(Imm & 0xFFFF)), 0);
527 // For non-equality comparisons, the default code would materialize the
528 // constant, then compare against it, like this:
532 // Since we are just comparing for equality, we can emit this instead:
533 // xoris r0,r3,0x1234
534 // cmpldi cr0,r0,0x5678
536 if (isUInt<32>(Imm)) {
537 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS8, dl, MVT::i64, LHS,
538 getI64Imm(Imm >> 16)), 0);
539 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, Xor,
540 getI64Imm(Imm & 0xFFFF)), 0);
544 } else if (ISD::isUnsignedIntSetCC(CC)) {
545 if (isInt64Immediate(RHS.getNode(), Imm) && isUInt<16>(Imm))
546 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
547 getI64Imm(Imm & 0xFFFF)), 0);
551 if (isIntS16Immediate(RHS, SImm))
552 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
553 getI64Imm(SImm & 0xFFFF)),
557 } else if (LHS.getValueType() == MVT::f32) {
560 assert(LHS.getValueType() == MVT::f64 && "Unknown vt!");
563 return SDValue(CurDAG->getMachineNode(Opc, dl, MVT::i32, LHS, RHS), 0);
566 static PPC::Predicate getPredicateForSetCC(ISD::CondCode CC) {
572 llvm_unreachable("Should be lowered by legalize!");
573 default: llvm_unreachable("Unknown condition!");
575 case ISD::SETEQ: return PPC::PRED_EQ;
577 case ISD::SETNE: return PPC::PRED_NE;
579 case ISD::SETLT: return PPC::PRED_LT;
581 case ISD::SETLE: return PPC::PRED_LE;
583 case ISD::SETGT: return PPC::PRED_GT;
585 case ISD::SETGE: return PPC::PRED_GE;
586 case ISD::SETO: return PPC::PRED_NU;
587 case ISD::SETUO: return PPC::PRED_UN;
588 // These two are invalid for floating point. Assume we have int.
589 case ISD::SETULT: return PPC::PRED_LT;
590 case ISD::SETUGT: return PPC::PRED_GT;
594 /// getCRIdxForSetCC - Return the index of the condition register field
595 /// associated with the SetCC condition, and whether or not the field is
596 /// treated as inverted. That is, lt = 0; ge = 0 inverted.
597 static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool &Invert) {
600 default: llvm_unreachable("Unknown condition!");
602 case ISD::SETLT: return 0; // Bit #0 = SETOLT
604 case ISD::SETGT: return 1; // Bit #1 = SETOGT
606 case ISD::SETEQ: return 2; // Bit #2 = SETOEQ
607 case ISD::SETUO: return 3; // Bit #3 = SETUO
609 case ISD::SETGE: Invert = true; return 0; // !Bit #0 = SETUGE
611 case ISD::SETLE: Invert = true; return 1; // !Bit #1 = SETULE
613 case ISD::SETNE: Invert = true; return 2; // !Bit #2 = SETUNE
614 case ISD::SETO: Invert = true; return 3; // !Bit #3 = SETO
619 llvm_unreachable("Invalid branch code: should be expanded by legalize");
620 // These are invalid for floating point. Assume integer.
621 case ISD::SETULT: return 0;
622 case ISD::SETUGT: return 1;
626 // getVCmpInst: return the vector compare instruction for the specified
627 // vector type and condition code. Since this is for altivec specific code,
628 // only support the altivec types (v16i8, v8i16, v4i32, and v4f32).
629 static unsigned int getVCmpInst(MVT::SimpleValueType VecVT, ISD::CondCode CC) {
635 if (VecVT == MVT::v16i8)
636 return PPC::VCMPEQUB;
637 else if (VecVT == MVT::v8i16)
638 return PPC::VCMPEQUH;
639 else if (VecVT == MVT::v4i32)
640 return PPC::VCMPEQUW;
641 // v4f32 != v4f32 could be translate to unordered not equal
642 else if (VecVT == MVT::v4f32)
643 return PPC::VCMPEQFP;
649 if (VecVT == MVT::v16i8)
650 return PPC::VCMPGTSB;
651 else if (VecVT == MVT::v8i16)
652 return PPC::VCMPGTSH;
653 else if (VecVT == MVT::v4i32)
654 return PPC::VCMPGTSW;
655 else if (VecVT == MVT::v4f32)
656 return PPC::VCMPGTFP;
662 if (VecVT == MVT::v16i8)
663 return PPC::VCMPGTUB;
664 else if (VecVT == MVT::v8i16)
665 return PPC::VCMPGTUH;
666 else if (VecVT == MVT::v4i32)
667 return PPC::VCMPGTUW;
670 if (VecVT == MVT::v4f32)
671 return PPC::VCMPEQFP;
676 if (VecVT == MVT::v4f32)
677 return PPC::VCMPGTFP;
680 if (VecVT == MVT::v4f32)
681 return PPC::VCMPGEFP;
686 llvm_unreachable("Invalid integer vector compare condition");
689 // getVCmpEQInst: return the equal compare instruction for the specified vector
690 // type. Since this is for altivec specific code, only support the altivec
691 // types (v16i8, v8i16, v4i32, and v4f32).
692 static unsigned int getVCmpEQInst(MVT::SimpleValueType VecVT) {
695 return PPC::VCMPEQUB;
697 return PPC::VCMPEQUH;
699 return PPC::VCMPEQUW;
701 return PPC::VCMPEQFP;
703 llvm_unreachable("Invalid integer vector compare condition");
708 SDNode *PPCDAGToDAGISel::SelectSETCC(SDNode *N) {
711 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
712 EVT PtrVT = CurDAG->getTargetLoweringInfo().getPointerTy();
713 bool isPPC64 = (PtrVT == MVT::i64);
715 if (isInt32Immediate(N->getOperand(1), Imm)) {
716 // We can codegen setcc op, imm very efficiently compared to a brcond.
717 // Check for those cases here.
720 SDValue Op = N->getOperand(0);
724 Op = SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, Op), 0);
725 SDValue Ops[] = { Op, getI32Imm(27), getI32Imm(5), getI32Imm(31) };
726 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
731 SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
732 Op, getI32Imm(~0U)), 0);
733 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op,
737 SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
738 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
742 SDValue(CurDAG->getMachineNode(PPC::NEG, dl, MVT::i32, Op), 0);
743 T = SDValue(CurDAG->getMachineNode(PPC::ANDC, dl, MVT::i32, T, Op), 0);
744 SDValue Ops[] = { T, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
745 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
748 } else if (Imm == ~0U) { // setcc op, -1
749 SDValue Op = N->getOperand(0);
754 Op = SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
755 Op, getI32Imm(1)), 0);
756 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
757 SDValue(CurDAG->getMachineNode(PPC::LI, dl,
763 Op = SDValue(CurDAG->getMachineNode(PPC::NOR, dl, MVT::i32, Op, Op), 0);
764 SDNode *AD = CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
766 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDValue(AD, 0),
770 SDValue AD = SDValue(CurDAG->getMachineNode(PPC::ADDI, dl, MVT::i32, Op,
772 SDValue AN = SDValue(CurDAG->getMachineNode(PPC::AND, dl, MVT::i32, AD,
774 SDValue Ops[] = { AN, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
775 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
778 SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
779 Op = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops),
781 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op,
788 SDValue LHS = N->getOperand(0);
789 SDValue RHS = N->getOperand(1);
791 // Altivec Vector compare instructions do not set any CR register by default and
792 // vector compare operations return the same type as the operands.
793 if (LHS.getValueType().isVector()) {
794 EVT VecVT = LHS.getValueType();
795 MVT::SimpleValueType VT = VecVT.getSimpleVT().SimpleTy;
796 unsigned int VCmpInst = getVCmpInst(VT, CC);
802 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, LHS, RHS);
806 SDValue VCmp(CurDAG->getMachineNode(VCmpInst, dl, VecVT, LHS, RHS), 0);
807 return CurDAG->SelectNodeTo(N, PPC::VNOR, VecVT, VCmp, VCmp);
812 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, RHS, LHS);
816 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, LHS, RHS);
820 // Small optimization: Altivec provides a 'Vector Compare Greater Than
821 // or Equal To' instruction (vcmpgefp), so in this case there is no
822 // need for extra logic for the equal compare.
823 if (VecVT.getSimpleVT().isFloatingPoint()) {
824 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, LHS, RHS);
826 SDValue VCmpGT(CurDAG->getMachineNode(VCmpInst, dl, VecVT, LHS, RHS), 0);
827 unsigned int VCmpEQInst = getVCmpEQInst(VT);
828 SDValue VCmpEQ(CurDAG->getMachineNode(VCmpEQInst, dl, VecVT, LHS, RHS), 0);
829 return CurDAG->SelectNodeTo(N, PPC::VOR, VecVT, VCmpGT, VCmpEQ);
835 SDValue VCmpLE(CurDAG->getMachineNode(VCmpInst, dl, VecVT, RHS, LHS), 0);
836 unsigned int VCmpEQInst = getVCmpEQInst(VT);
837 SDValue VCmpEQ(CurDAG->getMachineNode(VCmpEQInst, dl, VecVT, LHS, RHS), 0);
838 return CurDAG->SelectNodeTo(N, PPC::VOR, VecVT, VCmpLE, VCmpEQ);
841 llvm_unreachable("Invalid vector compare type: should be expanded by legalize");
846 unsigned Idx = getCRIdxForSetCC(CC, Inv);
847 SDValue CCReg = SelectCC(LHS, RHS, CC, dl);
850 // Force the ccreg into CR7.
851 SDValue CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
853 SDValue InFlag(0, 0); // Null incoming flag value.
854 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, CR7Reg, CCReg,
857 if (PPCSubTarget.hasMFOCRF())
858 IntCR = SDValue(CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32, CR7Reg,
861 IntCR = SDValue(CurDAG->getMachineNode(PPC::MFCRpseud, dl, MVT::i32,
864 SDValue Ops[] = { IntCR, getI32Imm((32-(3-Idx)) & 31),
865 getI32Imm(31), getI32Imm(31) };
867 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
869 // Get the specified bit.
871 SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0);
872 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1));
876 // Select - Convert the specified operand from a target-independent to a
877 // target-specific node if it hasn't already been changed.
878 SDNode *PPCDAGToDAGISel::Select(SDNode *N) {
880 if (N->isMachineOpcode())
881 return NULL; // Already selected.
883 switch (N->getOpcode()) {
886 case ISD::Constant: {
887 if (N->getValueType(0) == MVT::i64) {
889 int64_t Imm = cast<ConstantSDNode>(N)->getZExtValue();
890 // Assume no remaining bits.
891 unsigned Remainder = 0;
892 // Assume no shift required.
895 // If it can't be represented as a 32 bit value.
896 if (!isInt<32>(Imm)) {
897 Shift = countTrailingZeros<uint64_t>(Imm);
898 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
900 // If the shifted value fits 32 bits.
901 if (isInt<32>(ImmSh)) {
902 // Go with the shifted value.
905 // Still stuck with a 64 bit value.
912 // Intermediate operand.
915 // Handle first 32 bits.
916 unsigned Lo = Imm & 0xFFFF;
917 unsigned Hi = (Imm >> 16) & 0xFFFF;
920 if (isInt<16>(Imm)) {
922 Result = CurDAG->getMachineNode(PPC::LI8, dl, MVT::i64, getI32Imm(Lo));
924 // Handle the Hi bits.
925 unsigned OpC = Hi ? PPC::LIS8 : PPC::LI8;
926 Result = CurDAG->getMachineNode(OpC, dl, MVT::i64, getI32Imm(Hi));
928 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
929 SDValue(Result, 0), getI32Imm(Lo));
932 Result = CurDAG->getMachineNode(PPC::LIS8, dl, MVT::i64, getI32Imm(Hi));
935 // If no shift, we're done.
936 if (!Shift) return Result;
938 // Shift for next step if the upper 32-bits were not zero.
940 Result = CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64,
943 getI32Imm(63 - Shift));
946 // Add in the last bits as required.
947 if ((Hi = (Remainder >> 16) & 0xFFFF)) {
948 Result = CurDAG->getMachineNode(PPC::ORIS8, dl, MVT::i64,
949 SDValue(Result, 0), getI32Imm(Hi));
951 if ((Lo = Remainder & 0xFFFF)) {
952 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
953 SDValue(Result, 0), getI32Imm(Lo));
962 return SelectSETCC(N);
963 case PPCISD::GlobalBaseReg:
964 return getGlobalBaseReg();
966 case ISD::FrameIndex: {
967 int FI = cast<FrameIndexSDNode>(N)->getIndex();
968 SDValue TFI = CurDAG->getTargetFrameIndex(FI, N->getValueType(0));
969 unsigned Opc = N->getValueType(0) == MVT::i32 ? PPC::ADDI : PPC::ADDI8;
971 return CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), TFI,
973 return CurDAG->getMachineNode(Opc, dl, N->getValueType(0), TFI,
978 SDValue InFlag = N->getOperand(1);
979 // Use MFOCRF if supported.
980 if (PPCSubTarget.hasMFOCRF())
981 return CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32,
982 N->getOperand(0), InFlag);
984 return CurDAG->getMachineNode(PPC::MFCRpseud, dl, MVT::i32,
985 N->getOperand(0), InFlag);
989 // FIXME: since this depends on the setting of the carry flag from the srawi
990 // we should really be making notes about that for the scheduler.
991 // FIXME: It sure would be nice if we could cheaply recognize the
992 // srl/add/sra pattern the dag combiner will generate for this as
993 // sra/addze rather than having to handle sdiv ourselves. oh well.
995 if (isInt32Immediate(N->getOperand(1), Imm)) {
996 SDValue N0 = N->getOperand(0);
997 if ((signed)Imm > 0 && isPowerOf2_32(Imm)) {
999 CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue,
1000 N0, getI32Imm(Log2_32(Imm)));
1001 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
1002 SDValue(Op, 0), SDValue(Op, 1));
1003 } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) {
1005 CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue,
1006 N0, getI32Imm(Log2_32(-Imm)));
1008 SDValue(CurDAG->getMachineNode(PPC::ADDZE, dl, MVT::i32,
1009 SDValue(Op, 0), SDValue(Op, 1)),
1011 return CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT);
1015 // Other cases are autogenerated.
1020 // Handle preincrement loads.
1021 LoadSDNode *LD = cast<LoadSDNode>(N);
1022 EVT LoadedVT = LD->getMemoryVT();
1024 // Normal loads are handled by code generated from the .td file.
1025 if (LD->getAddressingMode() != ISD::PRE_INC)
1028 SDValue Offset = LD->getOffset();
1029 if (Offset.getOpcode() == ISD::TargetConstant ||
1030 Offset.getOpcode() == ISD::TargetGlobalAddress) {
1033 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
1034 if (LD->getValueType(0) != MVT::i64) {
1035 // Handle PPC32 integer and normal FP loads.
1036 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
1037 switch (LoadedVT.getSimpleVT().SimpleTy) {
1038 default: llvm_unreachable("Invalid PPC load type!");
1039 case MVT::f64: Opcode = PPC::LFDU; break;
1040 case MVT::f32: Opcode = PPC::LFSU; break;
1041 case MVT::i32: Opcode = PPC::LWZU; break;
1042 case MVT::i16: Opcode = isSExt ? PPC::LHAU : PPC::LHZU; break;
1044 case MVT::i8: Opcode = PPC::LBZU; break;
1047 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
1048 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
1049 switch (LoadedVT.getSimpleVT().SimpleTy) {
1050 default: llvm_unreachable("Invalid PPC load type!");
1051 case MVT::i64: Opcode = PPC::LDU; break;
1052 case MVT::i32: Opcode = PPC::LWZU8; break;
1053 case MVT::i16: Opcode = isSExt ? PPC::LHAU8 : PPC::LHZU8; break;
1055 case MVT::i8: Opcode = PPC::LBZU8; break;
1059 SDValue Chain = LD->getChain();
1060 SDValue Base = LD->getBasePtr();
1061 SDValue Ops[] = { Offset, Base, Chain };
1062 return CurDAG->getMachineNode(Opcode, dl, LD->getValueType(0),
1063 PPCLowering.getPointerTy(),
1067 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
1068 if (LD->getValueType(0) != MVT::i64) {
1069 // Handle PPC32 integer and normal FP loads.
1070 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
1071 switch (LoadedVT.getSimpleVT().SimpleTy) {
1072 default: llvm_unreachable("Invalid PPC load type!");
1073 case MVT::f64: Opcode = PPC::LFDUX; break;
1074 case MVT::f32: Opcode = PPC::LFSUX; break;
1075 case MVT::i32: Opcode = PPC::LWZUX; break;
1076 case MVT::i16: Opcode = isSExt ? PPC::LHAUX : PPC::LHZUX; break;
1078 case MVT::i8: Opcode = PPC::LBZUX; break;
1081 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
1082 assert((!isSExt || LoadedVT == MVT::i16 || LoadedVT == MVT::i32) &&
1083 "Invalid sext update load");
1084 switch (LoadedVT.getSimpleVT().SimpleTy) {
1085 default: llvm_unreachable("Invalid PPC load type!");
1086 case MVT::i64: Opcode = PPC::LDUX; break;
1087 case MVT::i32: Opcode = isSExt ? PPC::LWAUX : PPC::LWZUX8; break;
1088 case MVT::i16: Opcode = isSExt ? PPC::LHAUX8 : PPC::LHZUX8; break;
1090 case MVT::i8: Opcode = PPC::LBZUX8; break;
1094 SDValue Chain = LD->getChain();
1095 SDValue Base = LD->getBasePtr();
1096 SDValue Ops[] = { Base, Offset, Chain };
1097 return CurDAG->getMachineNode(Opcode, dl, LD->getValueType(0),
1098 PPCLowering.getPointerTy(),
1104 unsigned Imm, Imm2, SH, MB, ME;
1107 // If this is an and of a value rotated between 0 and 31 bits and then and'd
1108 // with a mask, emit rlwinm
1109 if (isInt32Immediate(N->getOperand(1), Imm) &&
1110 isRotateAndMask(N->getOperand(0).getNode(), Imm, false, SH, MB, ME)) {
1111 SDValue Val = N->getOperand(0).getOperand(0);
1112 SDValue Ops[] = { Val, getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
1113 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
1115 // If this is just a masked value where the input is not handled above, and
1116 // is not a rotate-left (handled by a pattern in the .td file), emit rlwinm
1117 if (isInt32Immediate(N->getOperand(1), Imm) &&
1118 isRunOfOnes(Imm, MB, ME) &&
1119 N->getOperand(0).getOpcode() != ISD::ROTL) {
1120 SDValue Val = N->getOperand(0);
1121 SDValue Ops[] = { Val, getI32Imm(0), getI32Imm(MB), getI32Imm(ME) };
1122 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
1124 // If this is a 64-bit zero-extension mask, emit rldicl.
1125 if (isInt64Immediate(N->getOperand(1).getNode(), Imm64) &&
1127 SDValue Val = N->getOperand(0);
1128 MB = 64 - CountTrailingOnes_64(Imm64);
1129 SDValue Ops[] = { Val, getI32Imm(0), getI32Imm(MB) };
1130 return CurDAG->SelectNodeTo(N, PPC::RLDICL, MVT::i64, Ops, 3);
1132 // AND X, 0 -> 0, not "rlwinm 32".
1133 if (isInt32Immediate(N->getOperand(1), Imm) && (Imm == 0)) {
1134 ReplaceUses(SDValue(N, 0), N->getOperand(1));
1137 // ISD::OR doesn't get all the bitfield insertion fun.
1138 // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) is a bitfield insert
1139 if (isInt32Immediate(N->getOperand(1), Imm) &&
1140 N->getOperand(0).getOpcode() == ISD::OR &&
1141 isInt32Immediate(N->getOperand(0).getOperand(1), Imm2)) {
1144 if (isRunOfOnes(Imm, MB, ME)) {
1145 SDValue Ops[] = { N->getOperand(0).getOperand(0),
1146 N->getOperand(0).getOperand(1),
1147 getI32Imm(0), getI32Imm(MB),getI32Imm(ME) };
1148 return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops);
1152 // Other cases are autogenerated.
1156 if (N->getValueType(0) == MVT::i32)
1157 if (SDNode *I = SelectBitfieldInsert(N))
1160 // Other cases are autogenerated.
1163 unsigned Imm, SH, MB, ME;
1164 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
1165 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
1166 SDValue Ops[] = { N->getOperand(0).getOperand(0),
1167 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
1168 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
1171 // Other cases are autogenerated.
1175 unsigned Imm, SH, MB, ME;
1176 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
1177 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
1178 SDValue Ops[] = { N->getOperand(0).getOperand(0),
1179 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
1180 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
1183 // Other cases are autogenerated.
1186 case ISD::SELECT_CC: {
1187 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
1188 EVT PtrVT = CurDAG->getTargetLoweringInfo().getPointerTy();
1189 bool isPPC64 = (PtrVT == MVT::i64);
1191 // Handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
1193 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1194 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1195 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1196 if (N1C->isNullValue() && N3C->isNullValue() &&
1197 N2C->getZExtValue() == 1ULL && CC == ISD::SETNE &&
1198 // FIXME: Implement this optzn for PPC64.
1199 N->getValueType(0) == MVT::i32) {
1201 CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
1202 N->getOperand(0), getI32Imm(~0U));
1203 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32,
1204 SDValue(Tmp, 0), N->getOperand(0),
1208 SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC, dl);
1209 unsigned BROpc = getPredicateForSetCC(CC);
1211 unsigned SelectCCOp;
1212 if (N->getValueType(0) == MVT::i32)
1213 SelectCCOp = PPC::SELECT_CC_I4;
1214 else if (N->getValueType(0) == MVT::i64)
1215 SelectCCOp = PPC::SELECT_CC_I8;
1216 else if (N->getValueType(0) == MVT::f32)
1217 SelectCCOp = PPC::SELECT_CC_F4;
1218 else if (N->getValueType(0) == MVT::f64)
1219 SelectCCOp = PPC::SELECT_CC_F8;
1221 SelectCCOp = PPC::SELECT_CC_VRRC;
1223 SDValue Ops[] = { CCReg, N->getOperand(2), N->getOperand(3),
1225 return CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), Ops, 4);
1229 bool IsPPC64 = PPCSubTarget.isPPC64();
1230 SDValue Ops[] = { N->getOperand(1), N->getOperand(0) };
1231 return CurDAG->SelectNodeTo(N, N->getOpcode() == PPCISD::BDNZ ?
1232 (IsPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
1233 (IsPPC64 ? PPC::BDZ8 : PPC::BDZ),
1234 MVT::Other, Ops, 2);
1236 case PPCISD::COND_BRANCH: {
1237 // Op #0 is the Chain.
1238 // Op #1 is the PPC::PRED_* number.
1240 // Op #3 is the Dest MBB
1241 // Op #4 is the Flag.
1242 // Prevent PPC::PRED_* from being selected into LI.
1244 getI32Imm(cast<ConstantSDNode>(N->getOperand(1))->getZExtValue());
1245 SDValue Ops[] = { Pred, N->getOperand(2), N->getOperand(3),
1246 N->getOperand(0), N->getOperand(4) };
1247 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops, 5);
1250 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
1251 SDValue CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC, dl);
1252 SDValue Ops[] = { getI32Imm(getPredicateForSetCC(CC)), CondCode,
1253 N->getOperand(4), N->getOperand(0) };
1254 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops, 4);
1257 // FIXME: Should custom lower this.
1258 SDValue Chain = N->getOperand(0);
1259 SDValue Target = N->getOperand(1);
1260 unsigned Opc = Target.getValueType() == MVT::i32 ? PPC::MTCTR : PPC::MTCTR8;
1261 unsigned Reg = Target.getValueType() == MVT::i32 ? PPC::BCTR : PPC::BCTR8;
1262 Chain = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Glue, Target,
1264 return CurDAG->SelectNodeTo(N, Reg, MVT::Other, Chain);
1266 case PPCISD::TOC_ENTRY: {
1267 assert (PPCSubTarget.isPPC64() && "Only supported for 64-bit ABI");
1269 // For medium and large code model, we generate two instructions as
1270 // described below. Otherwise we allow SelectCodeCommon to handle this,
1271 // selecting one of LDtoc, LDtocJTI, and LDtocCPT.
1272 CodeModel::Model CModel = TM.getCodeModel();
1273 if (CModel != CodeModel::Medium && CModel != CodeModel::Large)
1276 // The first source operand is a TargetGlobalAddress or a
1277 // TargetJumpTable. If it is an externally defined symbol, a symbol
1278 // with common linkage, a function address, or a jump table address,
1279 // or if we are generating code for large code model, we generate:
1280 // LDtocL(<ga:@sym>, ADDIStocHA(%X2, <ga:@sym>))
1281 // Otherwise we generate:
1282 // ADDItocL(ADDIStocHA(%X2, <ga:@sym>), <ga:@sym>)
1283 SDValue GA = N->getOperand(0);
1284 SDValue TOCbase = N->getOperand(1);
1285 SDNode *Tmp = CurDAG->getMachineNode(PPC::ADDIStocHA, dl, MVT::i64,
1288 if (isa<JumpTableSDNode>(GA) || CModel == CodeModel::Large)
1289 return CurDAG->getMachineNode(PPC::LDtocL, dl, MVT::i64, GA,
1292 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(GA)) {
1293 const GlobalValue *GValue = G->getGlobal();
1294 const GlobalAlias *GAlias = dyn_cast<GlobalAlias>(GValue);
1295 const GlobalValue *RealGValue = GAlias ?
1296 GAlias->resolveAliasedGlobal(false) : GValue;
1297 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(RealGValue);
1298 assert((GVar || isa<Function>(RealGValue)) &&
1299 "Unexpected global value subclass!");
1301 // An external variable is one without an initializer. For these,
1302 // for variables with common linkage, and for Functions, generate
1304 if (!GVar || !GVar->hasInitializer() || RealGValue->hasCommonLinkage() ||
1305 RealGValue->hasAvailableExternallyLinkage())
1306 return CurDAG->getMachineNode(PPC::LDtocL, dl, MVT::i64, GA,
1310 return CurDAG->getMachineNode(PPC::ADDItocL, dl, MVT::i64,
1311 SDValue(Tmp, 0), GA);
1313 case PPCISD::VADD_SPLAT: {
1314 // This expands into one of three sequences, depending on whether
1315 // the first operand is odd or even, positive or negative.
1316 assert(isa<ConstantSDNode>(N->getOperand(0)) &&
1317 isa<ConstantSDNode>(N->getOperand(1)) &&
1318 "Invalid operand on VADD_SPLAT!");
1320 int Elt = N->getConstantOperandVal(0);
1321 int EltSize = N->getConstantOperandVal(1);
1322 unsigned Opc1, Opc2, Opc3;
1326 Opc1 = PPC::VSPLTISB;
1327 Opc2 = PPC::VADDUBM;
1328 Opc3 = PPC::VSUBUBM;
1330 } else if (EltSize == 2) {
1331 Opc1 = PPC::VSPLTISH;
1332 Opc2 = PPC::VADDUHM;
1333 Opc3 = PPC::VSUBUHM;
1336 assert(EltSize == 4 && "Invalid element size on VADD_SPLAT!");
1337 Opc1 = PPC::VSPLTISW;
1338 Opc2 = PPC::VADDUWM;
1339 Opc3 = PPC::VSUBUWM;
1343 if ((Elt & 1) == 0) {
1344 // Elt is even, in the range [-32,-18] + [16,30].
1346 // Convert: VADD_SPLAT elt, size
1347 // Into: tmp = VSPLTIS[BHW] elt
1348 // VADDU[BHW]M tmp, tmp
1349 // Where: [BHW] = B for size = 1, H for size = 2, W for size = 4
1350 SDValue EltVal = getI32Imm(Elt >> 1);
1351 SDNode *Tmp = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1352 SDValue TmpVal = SDValue(Tmp, 0);
1353 return CurDAG->getMachineNode(Opc2, dl, VT, TmpVal, TmpVal);
1355 } else if (Elt > 0) {
1356 // Elt is odd and positive, in the range [17,31].
1358 // Convert: VADD_SPLAT elt, size
1359 // Into: tmp1 = VSPLTIS[BHW] elt-16
1360 // tmp2 = VSPLTIS[BHW] -16
1361 // VSUBU[BHW]M tmp1, tmp2
1362 SDValue EltVal = getI32Imm(Elt - 16);
1363 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1364 EltVal = getI32Imm(-16);
1365 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1366 return CurDAG->getMachineNode(Opc3, dl, VT, SDValue(Tmp1, 0),
1370 // Elt is odd and negative, in the range [-31,-17].
1372 // Convert: VADD_SPLAT elt, size
1373 // Into: tmp1 = VSPLTIS[BHW] elt+16
1374 // tmp2 = VSPLTIS[BHW] -16
1375 // VADDU[BHW]M tmp1, tmp2
1376 SDValue EltVal = getI32Imm(Elt + 16);
1377 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1378 EltVal = getI32Imm(-16);
1379 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1380 return CurDAG->getMachineNode(Opc2, dl, VT, SDValue(Tmp1, 0),
1386 return SelectCode(N);
1389 /// PostProcessISelDAG - Perform some late peephole optimizations
1390 /// on the DAG representation.
1391 void PPCDAGToDAGISel::PostprocessISelDAG() {
1393 // Skip peepholes at -O0.
1394 if (TM.getOptLevel() == CodeGenOpt::None)
1397 // These optimizations are currently supported only for 64-bit SVR4.
1398 if (PPCSubTarget.isDarwin() || !PPCSubTarget.isPPC64())
1401 SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode());
1404 while (Position != CurDAG->allnodes_begin()) {
1405 SDNode *N = --Position;
1406 // Skip dead nodes and any non-machine opcodes.
1407 if (N->use_empty() || !N->isMachineOpcode())
1411 unsigned StorageOpcode = N->getMachineOpcode();
1413 switch (StorageOpcode) {
1444 // If this is a load or store with a zero offset, we may be able to
1445 // fold an add-immediate into the memory operation.
1446 if (!isa<ConstantSDNode>(N->getOperand(FirstOp)) ||
1447 N->getConstantOperandVal(FirstOp) != 0)
1450 SDValue Base = N->getOperand(FirstOp + 1);
1451 if (!Base.isMachineOpcode())
1455 bool ReplaceFlags = true;
1457 // When the feeding operation is an add-immediate of some sort,
1458 // determine whether we need to add relocation information to the
1459 // target flags on the immediate operand when we fold it into the
1460 // load instruction.
1462 // For something like ADDItocL, the relocation information is
1463 // inferred from the opcode; when we process it in the AsmPrinter,
1464 // we add the necessary relocation there. A load, though, can receive
1465 // relocation from various flavors of ADDIxxx, so we need to carry
1466 // the relocation information in the target flags.
1467 switch (Base.getMachineOpcode()) {
1472 // In some cases (such as TLS) the relocation information
1473 // is already in place on the operand, so copying the operand
1475 ReplaceFlags = false;
1476 // For these cases, the immediate may not be divisible by 4, in
1477 // which case the fold is illegal for DS-form instructions. (The
1478 // other cases provide aligned addresses and are always safe.)
1479 if ((StorageOpcode == PPC::LWA ||
1480 StorageOpcode == PPC::LD ||
1481 StorageOpcode == PPC::STD) &&
1482 (!isa<ConstantSDNode>(Base.getOperand(1)) ||
1483 Base.getConstantOperandVal(1) % 4 != 0))
1486 case PPC::ADDIdtprelL:
1487 Flags = PPCII::MO_DTPREL_LO;
1489 case PPC::ADDItlsldL:
1490 Flags = PPCII::MO_TLSLD_LO;
1493 Flags = PPCII::MO_TOC_LO;
1497 // We found an opportunity. Reverse the operands from the add
1498 // immediate and substitute them into the load or store. If
1499 // needed, update the target flags for the immediate operand to
1500 // reflect the necessary relocation information.
1501 DEBUG(dbgs() << "Folding add-immediate into mem-op:\nBase: ");
1502 DEBUG(Base->dump(CurDAG));
1503 DEBUG(dbgs() << "\nN: ");
1504 DEBUG(N->dump(CurDAG));
1505 DEBUG(dbgs() << "\n");
1507 SDValue ImmOpnd = Base.getOperand(1);
1509 // If the relocation information isn't already present on the
1510 // immediate operand, add it now.
1512 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(ImmOpnd)) {
1514 const GlobalValue *GV = GA->getGlobal();
1515 // We can't perform this optimization for data whose alignment
1516 // is insufficient for the instruction encoding.
1517 if (GV->getAlignment() < 4 &&
1518 (StorageOpcode == PPC::LD || StorageOpcode == PPC::STD ||
1519 StorageOpcode == PPC::LWA)) {
1520 DEBUG(dbgs() << "Rejected this candidate for alignment.\n\n");
1523 ImmOpnd = CurDAG->getTargetGlobalAddress(GV, dl, MVT::i64, 0, Flags);
1524 } else if (ConstantPoolSDNode *CP =
1525 dyn_cast<ConstantPoolSDNode>(ImmOpnd)) {
1526 const Constant *C = CP->getConstVal();
1527 ImmOpnd = CurDAG->getTargetConstantPool(C, MVT::i64,
1533 if (FirstOp == 1) // Store
1534 (void)CurDAG->UpdateNodeOperands(N, N->getOperand(0), ImmOpnd,
1535 Base.getOperand(0), N->getOperand(3));
1537 (void)CurDAG->UpdateNodeOperands(N, ImmOpnd, Base.getOperand(0),
1540 // The add-immediate may now be dead, in which case remove it.
1541 if (Base.getNode()->use_empty())
1542 CurDAG->RemoveDeadNode(Base.getNode());
1547 /// createPPCISelDag - This pass converts a legalized DAG into a
1548 /// PowerPC-specific DAG, ready for instruction scheduling.
1550 FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) {
1551 return new PPCDAGToDAGISel(TM);
1554 static void initializePassOnce(PassRegistry &Registry) {
1555 const char *Name = "PowerPC DAG->DAG Pattern Instruction Selection";
1556 PassInfo *PI = new PassInfo(Name, "ppc-codegen", &SelectionDAGISel::ID, 0,
1558 Registry.registerPass(*PI, true);
1561 void llvm::initializePPCDAGToDAGISelPass(PassRegistry &Registry) {
1562 CALL_ONCE_INITIALIZATION(initializePassOnce);