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/CommandLine.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/MathExtras.h"
34 #include "llvm/Support/raw_ostream.h"
35 #include "llvm/Target/TargetOptions.h"
38 // FIXME: Remove this once the bug has been fixed!
39 cl::opt<bool> ANDIGlueBug("expose-ppc-andi-glue-bug",
40 cl::desc("expose the ANDI glue bug on PPC"), cl::Hidden);
43 void initializePPCDAGToDAGISelPass(PassRegistry&);
47 //===--------------------------------------------------------------------===//
48 /// PPCDAGToDAGISel - PPC specific code to select PPC machine
49 /// instructions for SelectionDAG operations.
51 class PPCDAGToDAGISel : public SelectionDAGISel {
52 const PPCTargetMachine &TM;
53 const PPCTargetLowering &PPCLowering;
54 const PPCSubtarget &PPCSubTarget;
55 unsigned GlobalBaseReg;
57 explicit PPCDAGToDAGISel(PPCTargetMachine &tm)
58 : SelectionDAGISel(tm), TM(tm),
59 PPCLowering(*TM.getTargetLowering()),
60 PPCSubTarget(*TM.getSubtargetImpl()) {
61 initializePPCDAGToDAGISelPass(*PassRegistry::getPassRegistry());
64 virtual bool runOnMachineFunction(MachineFunction &MF) {
65 // Make sure we re-emit a set of the global base reg if necessary
67 SelectionDAGISel::runOnMachineFunction(MF);
69 if (!PPCSubTarget.isSVR4ABI())
75 virtual void PostprocessISelDAG();
77 /// getI32Imm - Return a target constant with the specified value, of type
79 inline SDValue getI32Imm(unsigned Imm) {
80 return CurDAG->getTargetConstant(Imm, MVT::i32);
83 /// getI64Imm - Return a target constant with the specified value, of type
85 inline SDValue getI64Imm(uint64_t Imm) {
86 return CurDAG->getTargetConstant(Imm, MVT::i64);
89 /// getSmallIPtrImm - Return a target constant of pointer type.
90 inline SDValue getSmallIPtrImm(unsigned Imm) {
91 return CurDAG->getTargetConstant(Imm, PPCLowering.getPointerTy());
94 /// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s
95 /// with any number of 0s on either side. The 1s are allowed to wrap from
96 /// LSB to MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs.
97 /// 0x0F0F0000 is not, since all 1s are not contiguous.
98 static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME);
101 /// isRotateAndMask - Returns true if Mask and Shift can be folded into a
102 /// rotate and mask opcode and mask operation.
103 static bool isRotateAndMask(SDNode *N, unsigned Mask, bool isShiftMask,
104 unsigned &SH, unsigned &MB, unsigned &ME);
106 /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
107 /// base register. Return the virtual register that holds this value.
108 SDNode *getGlobalBaseReg();
110 // Select - Convert the specified operand from a target-independent to a
111 // target-specific node if it hasn't already been changed.
112 SDNode *Select(SDNode *N);
114 SDNode *SelectBitfieldInsert(SDNode *N);
116 /// SelectCC - Select a comparison of the specified values with the
117 /// specified condition code, returning the CR# of the expression.
118 SDValue SelectCC(SDValue LHS, SDValue RHS, ISD::CondCode CC, SDLoc dl);
120 /// SelectAddrImm - Returns true if the address N can be represented by
121 /// a base register plus a signed 16-bit displacement [r+imm].
122 bool SelectAddrImm(SDValue N, SDValue &Disp,
124 return PPCLowering.SelectAddressRegImm(N, Disp, Base, *CurDAG, false);
127 /// SelectAddrImmOffs - Return true if the operand is valid for a preinc
128 /// immediate field. Note that the operand at this point is already the
129 /// result of a prior SelectAddressRegImm call.
130 bool SelectAddrImmOffs(SDValue N, SDValue &Out) const {
131 if (N.getOpcode() == ISD::TargetConstant ||
132 N.getOpcode() == ISD::TargetGlobalAddress) {
140 /// SelectAddrIdx - Given the specified addressed, check to see if it can be
141 /// represented as an indexed [r+r] operation. Returns false if it can
142 /// be represented by [r+imm], which are preferred.
143 bool SelectAddrIdx(SDValue N, SDValue &Base, SDValue &Index) {
144 return PPCLowering.SelectAddressRegReg(N, Base, Index, *CurDAG);
147 /// SelectAddrIdxOnly - Given the specified addressed, force it to be
148 /// represented as an indexed [r+r] operation.
149 bool SelectAddrIdxOnly(SDValue N, SDValue &Base, SDValue &Index) {
150 return PPCLowering.SelectAddressRegRegOnly(N, Base, Index, *CurDAG);
153 /// SelectAddrImmX4 - Returns true if the address N can be represented by
154 /// a base register plus a signed 16-bit displacement that is a multiple of 4.
155 /// Suitable for use by STD and friends.
156 bool SelectAddrImmX4(SDValue N, SDValue &Disp, SDValue &Base) {
157 return PPCLowering.SelectAddressRegImm(N, Disp, Base, *CurDAG, true);
160 // Select an address into a single register.
161 bool SelectAddr(SDValue N, SDValue &Base) {
166 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
167 /// inline asm expressions. It is always correct to compute the value into
168 /// a register. The case of adding a (possibly relocatable) constant to a
169 /// register can be improved, but it is wrong to substitute Reg+Reg for
170 /// Reg in an asm, because the load or store opcode would have to change.
171 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
173 std::vector<SDValue> &OutOps) {
174 OutOps.push_back(Op);
178 void InsertVRSaveCode(MachineFunction &MF);
180 virtual const char *getPassName() const {
181 return "PowerPC DAG->DAG Pattern Instruction Selection";
184 // Include the pieces autogenerated from the target description.
185 #include "PPCGenDAGISel.inc"
188 SDNode *SelectSETCC(SDNode *N);
190 void PeepholePPC64();
191 void PeepholdCROps();
193 bool AllUsersSelectZero(SDNode *N);
194 void SwapAllSelectUsers(SDNode *N);
198 /// InsertVRSaveCode - Once the entire function has been instruction selected,
199 /// all virtual registers are created and all machine instructions are built,
200 /// check to see if we need to save/restore VRSAVE. If so, do it.
201 void PPCDAGToDAGISel::InsertVRSaveCode(MachineFunction &Fn) {
202 // Check to see if this function uses vector registers, which means we have to
203 // save and restore the VRSAVE register and update it with the regs we use.
205 // In this case, there will be virtual registers of vector type created
206 // by the scheduler. Detect them now.
207 bool HasVectorVReg = false;
208 for (unsigned i = 0, e = RegInfo->getNumVirtRegs(); i != e; ++i) {
209 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
210 if (RegInfo->getRegClass(Reg) == &PPC::VRRCRegClass) {
211 HasVectorVReg = true;
215 if (!HasVectorVReg) return; // nothing to do.
217 // If we have a vector register, we want to emit code into the entry and exit
218 // blocks to save and restore the VRSAVE register. We do this here (instead
219 // of marking all vector instructions as clobbering VRSAVE) for two reasons:
221 // 1. This (trivially) reduces the load on the register allocator, by not
222 // having to represent the live range of the VRSAVE register.
223 // 2. This (more significantly) allows us to create a temporary virtual
224 // register to hold the saved VRSAVE value, allowing this temporary to be
225 // register allocated, instead of forcing it to be spilled to the stack.
227 // Create two vregs - one to hold the VRSAVE register that is live-in to the
228 // function and one for the value after having bits or'd into it.
229 unsigned InVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
230 unsigned UpdatedVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
232 const TargetInstrInfo &TII = *TM.getInstrInfo();
233 MachineBasicBlock &EntryBB = *Fn.begin();
235 // Emit the following code into the entry block:
236 // InVRSAVE = MFVRSAVE
237 // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE
238 // MTVRSAVE UpdatedVRSAVE
239 MachineBasicBlock::iterator IP = EntryBB.begin(); // Insert Point
240 BuildMI(EntryBB, IP, dl, TII.get(PPC::MFVRSAVE), InVRSAVE);
241 BuildMI(EntryBB, IP, dl, TII.get(PPC::UPDATE_VRSAVE),
242 UpdatedVRSAVE).addReg(InVRSAVE);
243 BuildMI(EntryBB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(UpdatedVRSAVE);
245 // Find all return blocks, outputting a restore in each epilog.
246 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
247 if (!BB->empty() && BB->back().isReturn()) {
248 IP = BB->end(); --IP;
250 // Skip over all terminator instructions, which are part of the return
252 MachineBasicBlock::iterator I2 = IP;
253 while (I2 != BB->begin() && (--I2)->isTerminator())
256 // Emit: MTVRSAVE InVRSave
257 BuildMI(*BB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(InVRSAVE);
263 /// getGlobalBaseReg - Output the instructions required to put the
264 /// base address to use for accessing globals into a register.
266 SDNode *PPCDAGToDAGISel::getGlobalBaseReg() {
267 if (!GlobalBaseReg) {
268 const TargetInstrInfo &TII = *TM.getInstrInfo();
269 // Insert the set of GlobalBaseReg into the first MBB of the function
270 MachineBasicBlock &FirstMBB = MF->front();
271 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
274 if (PPCLowering.getPointerTy() == MVT::i32) {
275 GlobalBaseReg = RegInfo->createVirtualRegister(&PPC::GPRC_NOR0RegClass);
276 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR));
277 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
279 GlobalBaseReg = RegInfo->createVirtualRegister(&PPC::G8RC_NOX0RegClass);
280 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR8));
281 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR8), GlobalBaseReg);
284 return CurDAG->getRegister(GlobalBaseReg,
285 PPCLowering.getPointerTy()).getNode();
288 /// isIntS16Immediate - This method tests to see if the node is either a 32-bit
289 /// or 64-bit immediate, and if the value can be accurately represented as a
290 /// sign extension from a 16-bit value. If so, this returns true and the
292 static bool isIntS16Immediate(SDNode *N, short &Imm) {
293 if (N->getOpcode() != ISD::Constant)
296 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
297 if (N->getValueType(0) == MVT::i32)
298 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
300 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
303 static bool isIntS16Immediate(SDValue Op, short &Imm) {
304 return isIntS16Immediate(Op.getNode(), Imm);
308 /// isInt32Immediate - This method tests to see if the node is a 32-bit constant
309 /// operand. If so Imm will receive the 32-bit value.
310 static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
311 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
312 Imm = cast<ConstantSDNode>(N)->getZExtValue();
318 /// isInt64Immediate - This method tests to see if the node is a 64-bit constant
319 /// operand. If so Imm will receive the 64-bit value.
320 static bool isInt64Immediate(SDNode *N, uint64_t &Imm) {
321 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i64) {
322 Imm = cast<ConstantSDNode>(N)->getZExtValue();
328 // isInt32Immediate - This method tests to see if a constant operand.
329 // If so Imm will receive the 32 bit value.
330 static bool isInt32Immediate(SDValue N, unsigned &Imm) {
331 return isInt32Immediate(N.getNode(), Imm);
335 // isOpcWithIntImmediate - This method tests to see if the node is a specific
336 // opcode and that it has a immediate integer right operand.
337 // If so Imm will receive the 32 bit value.
338 static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
339 return N->getOpcode() == Opc
340 && isInt32Immediate(N->getOperand(1).getNode(), Imm);
343 bool PPCDAGToDAGISel::isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
347 if (isShiftedMask_32(Val)) {
348 // look for the first non-zero bit
349 MB = countLeadingZeros(Val);
350 // look for the first zero bit after the run of ones
351 ME = countLeadingZeros((Val - 1) ^ Val);
354 Val = ~Val; // invert mask
355 if (isShiftedMask_32(Val)) {
356 // effectively look for the first zero bit
357 ME = countLeadingZeros(Val) - 1;
358 // effectively look for the first one bit after the run of zeros
359 MB = countLeadingZeros((Val - 1) ^ Val) + 1;
367 bool PPCDAGToDAGISel::isRotateAndMask(SDNode *N, unsigned Mask,
368 bool isShiftMask, unsigned &SH,
369 unsigned &MB, unsigned &ME) {
370 // Don't even go down this path for i64, since different logic will be
371 // necessary for rldicl/rldicr/rldimi.
372 if (N->getValueType(0) != MVT::i32)
376 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
377 unsigned Opcode = N->getOpcode();
378 if (N->getNumOperands() != 2 ||
379 !isInt32Immediate(N->getOperand(1).getNode(), Shift) || (Shift > 31))
382 if (Opcode == ISD::SHL) {
383 // apply shift left to mask if it comes first
384 if (isShiftMask) Mask = Mask << Shift;
385 // determine which bits are made indeterminant by shift
386 Indeterminant = ~(0xFFFFFFFFu << Shift);
387 } else if (Opcode == ISD::SRL) {
388 // apply shift right to mask if it comes first
389 if (isShiftMask) Mask = Mask >> Shift;
390 // determine which bits are made indeterminant by shift
391 Indeterminant = ~(0xFFFFFFFFu >> Shift);
392 // adjust for the left rotate
394 } else if (Opcode == ISD::ROTL) {
400 // if the mask doesn't intersect any Indeterminant bits
401 if (Mask && !(Mask & Indeterminant)) {
403 // make sure the mask is still a mask (wrap arounds may not be)
404 return isRunOfOnes(Mask, MB, ME);
409 /// SelectBitfieldInsert - turn an or of two masked values into
410 /// the rotate left word immediate then mask insert (rlwimi) instruction.
411 SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
412 SDValue Op0 = N->getOperand(0);
413 SDValue Op1 = N->getOperand(1);
416 APInt LKZ, LKO, RKZ, RKO;
417 CurDAG->ComputeMaskedBits(Op0, LKZ, LKO);
418 CurDAG->ComputeMaskedBits(Op1, RKZ, RKO);
420 unsigned TargetMask = LKZ.getZExtValue();
421 unsigned InsertMask = RKZ.getZExtValue();
423 if ((TargetMask | InsertMask) == 0xFFFFFFFF) {
424 unsigned Op0Opc = Op0.getOpcode();
425 unsigned Op1Opc = Op1.getOpcode();
426 unsigned Value, SH = 0;
427 TargetMask = ~TargetMask;
428 InsertMask = ~InsertMask;
430 // If the LHS has a foldable shift and the RHS does not, then swap it to the
431 // RHS so that we can fold the shift into the insert.
432 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
433 if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
434 Op0.getOperand(0).getOpcode() == ISD::SRL) {
435 if (Op1.getOperand(0).getOpcode() != ISD::SHL &&
436 Op1.getOperand(0).getOpcode() != ISD::SRL) {
438 std::swap(Op0Opc, Op1Opc);
439 std::swap(TargetMask, InsertMask);
442 } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) {
443 if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL &&
444 Op1.getOperand(0).getOpcode() != ISD::SRL) {
446 std::swap(Op0Opc, Op1Opc);
447 std::swap(TargetMask, InsertMask);
452 if (isRunOfOnes(InsertMask, MB, ME)) {
455 if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) &&
456 isInt32Immediate(Op1.getOperand(1), Value)) {
457 Op1 = Op1.getOperand(0);
458 SH = (Op1Opc == ISD::SHL) ? Value : 32 - Value;
460 if (Op1Opc == ISD::AND) {
461 unsigned SHOpc = Op1.getOperand(0).getOpcode();
462 if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) &&
463 isInt32Immediate(Op1.getOperand(0).getOperand(1), Value)) {
464 // Note that Value must be in range here (less than 32) because
465 // otherwise there would not be any bits set in InsertMask.
466 Op1 = Op1.getOperand(0).getOperand(0);
467 SH = (SHOpc == ISD::SHL) ? Value : 32 - Value;
472 SDValue Ops[] = { Op0, Op1, getI32Imm(SH), getI32Imm(MB),
474 return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops);
480 /// SelectCC - Select a comparison of the specified values with the specified
481 /// condition code, returning the CR# of the expression.
482 SDValue PPCDAGToDAGISel::SelectCC(SDValue LHS, SDValue RHS,
483 ISD::CondCode CC, SDLoc dl) {
484 // Always select the LHS.
487 if (LHS.getValueType() == MVT::i32) {
489 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
490 if (isInt32Immediate(RHS, Imm)) {
491 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
493 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
494 getI32Imm(Imm & 0xFFFF)), 0);
495 // If this is a 16-bit signed immediate, fold it.
496 if (isInt<16>((int)Imm))
497 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
498 getI32Imm(Imm & 0xFFFF)), 0);
500 // For non-equality comparisons, the default code would materialize the
501 // constant, then compare against it, like this:
505 // Since we are just comparing for equality, we can emit this instead:
506 // xoris r0,r3,0x1234
507 // cmplwi cr0,r0,0x5678
509 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS, dl, MVT::i32, LHS,
510 getI32Imm(Imm >> 16)), 0);
511 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, Xor,
512 getI32Imm(Imm & 0xFFFF)), 0);
515 } else if (ISD::isUnsignedIntSetCC(CC)) {
516 if (isInt32Immediate(RHS, Imm) && isUInt<16>(Imm))
517 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
518 getI32Imm(Imm & 0xFFFF)), 0);
522 if (isIntS16Immediate(RHS, SImm))
523 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
524 getI32Imm((int)SImm & 0xFFFF)),
528 } else if (LHS.getValueType() == MVT::i64) {
530 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
531 if (isInt64Immediate(RHS.getNode(), Imm)) {
532 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
534 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
535 getI32Imm(Imm & 0xFFFF)), 0);
536 // If this is a 16-bit signed immediate, fold it.
538 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
539 getI32Imm(Imm & 0xFFFF)), 0);
541 // For non-equality comparisons, the default code would materialize the
542 // constant, then compare against it, like this:
546 // Since we are just comparing for equality, we can emit this instead:
547 // xoris r0,r3,0x1234
548 // cmpldi cr0,r0,0x5678
550 if (isUInt<32>(Imm)) {
551 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS8, dl, MVT::i64, LHS,
552 getI64Imm(Imm >> 16)), 0);
553 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, Xor,
554 getI64Imm(Imm & 0xFFFF)), 0);
558 } else if (ISD::isUnsignedIntSetCC(CC)) {
559 if (isInt64Immediate(RHS.getNode(), Imm) && isUInt<16>(Imm))
560 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
561 getI64Imm(Imm & 0xFFFF)), 0);
565 if (isIntS16Immediate(RHS, SImm))
566 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
567 getI64Imm(SImm & 0xFFFF)),
571 } else if (LHS.getValueType() == MVT::f32) {
574 assert(LHS.getValueType() == MVT::f64 && "Unknown vt!");
575 Opc = PPCSubTarget.hasVSX() ? PPC::XSCMPUDP : PPC::FCMPUD;
577 return SDValue(CurDAG->getMachineNode(Opc, dl, MVT::i32, LHS, RHS), 0);
580 static PPC::Predicate getPredicateForSetCC(ISD::CondCode CC) {
586 llvm_unreachable("Should be lowered by legalize!");
587 default: llvm_unreachable("Unknown condition!");
589 case ISD::SETEQ: return PPC::PRED_EQ;
591 case ISD::SETNE: return PPC::PRED_NE;
593 case ISD::SETLT: return PPC::PRED_LT;
595 case ISD::SETLE: return PPC::PRED_LE;
597 case ISD::SETGT: return PPC::PRED_GT;
599 case ISD::SETGE: return PPC::PRED_GE;
600 case ISD::SETO: return PPC::PRED_NU;
601 case ISD::SETUO: return PPC::PRED_UN;
602 // These two are invalid for floating point. Assume we have int.
603 case ISD::SETULT: return PPC::PRED_LT;
604 case ISD::SETUGT: return PPC::PRED_GT;
608 /// getCRIdxForSetCC - Return the index of the condition register field
609 /// associated with the SetCC condition, and whether or not the field is
610 /// treated as inverted. That is, lt = 0; ge = 0 inverted.
611 static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool &Invert) {
614 default: llvm_unreachable("Unknown condition!");
616 case ISD::SETLT: return 0; // Bit #0 = SETOLT
618 case ISD::SETGT: return 1; // Bit #1 = SETOGT
620 case ISD::SETEQ: return 2; // Bit #2 = SETOEQ
621 case ISD::SETUO: return 3; // Bit #3 = SETUO
623 case ISD::SETGE: Invert = true; return 0; // !Bit #0 = SETUGE
625 case ISD::SETLE: Invert = true; return 1; // !Bit #1 = SETULE
627 case ISD::SETNE: Invert = true; return 2; // !Bit #2 = SETUNE
628 case ISD::SETO: Invert = true; return 3; // !Bit #3 = SETO
633 llvm_unreachable("Invalid branch code: should be expanded by legalize");
634 // These are invalid for floating point. Assume integer.
635 case ISD::SETULT: return 0;
636 case ISD::SETUGT: return 1;
640 // getVCmpInst: return the vector compare instruction for the specified
641 // vector type and condition code. Since this is for altivec specific code,
642 // only support the altivec types (v16i8, v8i16, v4i32, and v4f32).
643 static unsigned int getVCmpInst(MVT::SimpleValueType VecVT, ISD::CondCode CC,
650 if (VecVT == MVT::v16i8)
651 return PPC::VCMPEQUB;
652 else if (VecVT == MVT::v8i16)
653 return PPC::VCMPEQUH;
654 else if (VecVT == MVT::v4i32)
655 return PPC::VCMPEQUW;
656 // v4f32 != v4f32 could be translate to unordered not equal
657 else if (VecVT == MVT::v4f32)
658 return HasVSX ? PPC::XVCMPEQSP : PPC::VCMPEQFP;
659 else if (VecVT == MVT::v2f64)
660 return PPC::XVCMPEQDP;
666 if (VecVT == MVT::v16i8)
667 return PPC::VCMPGTSB;
668 else if (VecVT == MVT::v8i16)
669 return PPC::VCMPGTSH;
670 else if (VecVT == MVT::v4i32)
671 return PPC::VCMPGTSW;
672 else if (VecVT == MVT::v4f32)
673 return HasVSX ? PPC::XVCMPGTSP : PPC::VCMPGTFP;
674 else if (VecVT == MVT::v2f64)
675 return PPC::XVCMPGTDP;
681 if (VecVT == MVT::v16i8)
682 return PPC::VCMPGTUB;
683 else if (VecVT == MVT::v8i16)
684 return PPC::VCMPGTUH;
685 else if (VecVT == MVT::v4i32)
686 return PPC::VCMPGTUW;
689 if (VecVT == MVT::v4f32)
690 return HasVSX ? PPC::XVCMPEQSP : PPC::VCMPEQFP;
691 else if (VecVT == MVT::v2f64)
692 return PPC::XVCMPEQDP;
697 if (VecVT == MVT::v4f32)
698 return HasVSX ? PPC::XVCMPGTSP : PPC::VCMPGTFP;
699 else if (VecVT == MVT::v2f64)
700 return PPC::XVCMPGTDP;
703 if (VecVT == MVT::v4f32)
704 return HasVSX ? PPC::XVCMPGESP : PPC::VCMPGEFP;
705 else if (VecVT == MVT::v2f64)
706 return PPC::XVCMPGEDP;
711 llvm_unreachable("Invalid integer vector compare condition");
714 // getVCmpEQInst: return the equal compare instruction for the specified vector
715 // type. Since this is for altivec specific code, only support the altivec
716 // types (v16i8, v8i16, v4i32, and v4f32).
717 static unsigned int getVCmpEQInst(MVT::SimpleValueType VecVT, bool HasVSX) {
720 return PPC::VCMPEQUB;
722 return PPC::VCMPEQUH;
724 return PPC::VCMPEQUW;
726 return HasVSX ? PPC::XVCMPEQSP : PPC::VCMPEQFP;
728 return PPC::XVCMPEQDP;
730 llvm_unreachable("Invalid integer vector compare condition");
734 SDNode *PPCDAGToDAGISel::SelectSETCC(SDNode *N) {
737 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
738 EVT PtrVT = CurDAG->getTargetLoweringInfo().getPointerTy();
739 bool isPPC64 = (PtrVT == MVT::i64);
741 if (!PPCSubTarget.useCRBits() &&
742 isInt32Immediate(N->getOperand(1), Imm)) {
743 // We can codegen setcc op, imm very efficiently compared to a brcond.
744 // Check for those cases here.
747 SDValue Op = N->getOperand(0);
751 Op = SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, Op), 0);
752 SDValue Ops[] = { Op, getI32Imm(27), getI32Imm(5), getI32Imm(31) };
753 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
758 SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
759 Op, getI32Imm(~0U)), 0);
760 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op,
764 SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
765 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
769 SDValue(CurDAG->getMachineNode(PPC::NEG, dl, MVT::i32, Op), 0);
770 T = SDValue(CurDAG->getMachineNode(PPC::ANDC, dl, MVT::i32, T, Op), 0);
771 SDValue Ops[] = { T, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
772 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
775 } else if (Imm == ~0U) { // setcc op, -1
776 SDValue Op = N->getOperand(0);
781 Op = SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
782 Op, getI32Imm(1)), 0);
783 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
784 SDValue(CurDAG->getMachineNode(PPC::LI, dl,
790 Op = SDValue(CurDAG->getMachineNode(PPC::NOR, dl, MVT::i32, Op, Op), 0);
791 SDNode *AD = CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
793 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDValue(AD, 0),
797 SDValue AD = SDValue(CurDAG->getMachineNode(PPC::ADDI, dl, MVT::i32, Op,
799 SDValue AN = SDValue(CurDAG->getMachineNode(PPC::AND, dl, MVT::i32, AD,
801 SDValue Ops[] = { AN, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
802 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
805 SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
806 Op = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops),
808 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op,
815 SDValue LHS = N->getOperand(0);
816 SDValue RHS = N->getOperand(1);
818 // Altivec Vector compare instructions do not set any CR register by default and
819 // vector compare operations return the same type as the operands.
820 if (LHS.getValueType().isVector()) {
821 EVT VecVT = LHS.getValueType();
822 MVT::SimpleValueType VT = VecVT.getSimpleVT().SimpleTy;
823 unsigned int VCmpInst = getVCmpInst(VT, CC, PPCSubTarget.hasVSX());
829 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, LHS, RHS);
833 SDValue VCmp(CurDAG->getMachineNode(VCmpInst, dl, VecVT, LHS, RHS), 0);
834 return CurDAG->SelectNodeTo(N, PPCSubTarget.hasVSX() ? PPC::XXLNOR :
841 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, RHS, LHS);
845 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, LHS, RHS);
849 // Small optimization: Altivec provides a 'Vector Compare Greater Than
850 // or Equal To' instruction (vcmpgefp), so in this case there is no
851 // need for extra logic for the equal compare.
852 if (VecVT.getSimpleVT().isFloatingPoint()) {
853 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, LHS, RHS);
855 SDValue VCmpGT(CurDAG->getMachineNode(VCmpInst, dl, VecVT, LHS, RHS), 0);
856 unsigned int VCmpEQInst = getVCmpEQInst(VT, PPCSubTarget.hasVSX());
857 SDValue VCmpEQ(CurDAG->getMachineNode(VCmpEQInst, dl, VecVT, LHS, RHS), 0);
858 return CurDAG->SelectNodeTo(N, PPCSubTarget.hasVSX() ? PPC::XXLOR :
860 VecVT, VCmpGT, VCmpEQ);
866 SDValue VCmpLE(CurDAG->getMachineNode(VCmpInst, dl, VecVT, RHS, LHS), 0);
867 unsigned int VCmpEQInst = getVCmpEQInst(VT, PPCSubTarget.hasVSX());
868 SDValue VCmpEQ(CurDAG->getMachineNode(VCmpEQInst, dl, VecVT, LHS, RHS), 0);
869 return CurDAG->SelectNodeTo(N, PPCSubTarget.hasVSX() ? PPC::XXLOR :
871 VecVT, VCmpLE, VCmpEQ);
874 llvm_unreachable("Invalid vector compare type: should be expanded by legalize");
878 if (PPCSubTarget.useCRBits())
882 unsigned Idx = getCRIdxForSetCC(CC, Inv);
883 SDValue CCReg = SelectCC(LHS, RHS, CC, dl);
886 // Force the ccreg into CR7.
887 SDValue CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
889 SDValue InFlag(0, 0); // Null incoming flag value.
890 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, CR7Reg, CCReg,
893 IntCR = SDValue(CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32, CR7Reg,
896 SDValue Ops[] = { IntCR, getI32Imm((32-(3-Idx)) & 31),
897 getI32Imm(31), getI32Imm(31) };
899 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
901 // Get the specified bit.
903 SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0);
904 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1));
908 // Select - Convert the specified operand from a target-independent to a
909 // target-specific node if it hasn't already been changed.
910 SDNode *PPCDAGToDAGISel::Select(SDNode *N) {
912 if (N->isMachineOpcode()) {
914 return NULL; // Already selected.
917 switch (N->getOpcode()) {
920 case ISD::Constant: {
921 if (N->getValueType(0) == MVT::i64) {
923 int64_t Imm = cast<ConstantSDNode>(N)->getZExtValue();
924 // Assume no remaining bits.
925 unsigned Remainder = 0;
926 // Assume no shift required.
929 // If it can't be represented as a 32 bit value.
930 if (!isInt<32>(Imm)) {
931 Shift = countTrailingZeros<uint64_t>(Imm);
932 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
934 // If the shifted value fits 32 bits.
935 if (isInt<32>(ImmSh)) {
936 // Go with the shifted value.
939 // Still stuck with a 64 bit value.
946 // Intermediate operand.
949 // Handle first 32 bits.
950 unsigned Lo = Imm & 0xFFFF;
951 unsigned Hi = (Imm >> 16) & 0xFFFF;
954 if (isInt<16>(Imm)) {
956 Result = CurDAG->getMachineNode(PPC::LI8, dl, MVT::i64, getI32Imm(Lo));
958 // Handle the Hi bits.
959 unsigned OpC = Hi ? PPC::LIS8 : PPC::LI8;
960 Result = CurDAG->getMachineNode(OpC, dl, MVT::i64, getI32Imm(Hi));
962 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
963 SDValue(Result, 0), getI32Imm(Lo));
966 Result = CurDAG->getMachineNode(PPC::LIS8, dl, MVT::i64, getI32Imm(Hi));
969 // If no shift, we're done.
970 if (!Shift) return Result;
972 // Shift for next step if the upper 32-bits were not zero.
974 Result = CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64,
977 getI32Imm(63 - Shift));
980 // Add in the last bits as required.
981 if ((Hi = (Remainder >> 16) & 0xFFFF)) {
982 Result = CurDAG->getMachineNode(PPC::ORIS8, dl, MVT::i64,
983 SDValue(Result, 0), getI32Imm(Hi));
985 if ((Lo = Remainder & 0xFFFF)) {
986 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
987 SDValue(Result, 0), getI32Imm(Lo));
996 SDNode *SN = SelectSETCC(N);
1001 case PPCISD::GlobalBaseReg:
1002 return getGlobalBaseReg();
1004 case ISD::FrameIndex: {
1005 int FI = cast<FrameIndexSDNode>(N)->getIndex();
1006 SDValue TFI = CurDAG->getTargetFrameIndex(FI, N->getValueType(0));
1007 unsigned Opc = N->getValueType(0) == MVT::i32 ? PPC::ADDI : PPC::ADDI8;
1009 return CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), TFI,
1010 getSmallIPtrImm(0));
1011 return CurDAG->getMachineNode(Opc, dl, N->getValueType(0), TFI,
1012 getSmallIPtrImm(0));
1015 case PPCISD::MFOCRF: {
1016 SDValue InFlag = N->getOperand(1);
1017 return CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32,
1018 N->getOperand(0), InFlag);
1022 // FIXME: since this depends on the setting of the carry flag from the srawi
1023 // we should really be making notes about that for the scheduler.
1024 // FIXME: It sure would be nice if we could cheaply recognize the
1025 // srl/add/sra pattern the dag combiner will generate for this as
1026 // sra/addze rather than having to handle sdiv ourselves. oh well.
1028 if (isInt32Immediate(N->getOperand(1), Imm)) {
1029 SDValue N0 = N->getOperand(0);
1030 if ((signed)Imm > 0 && isPowerOf2_32(Imm)) {
1032 CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue,
1033 N0, getI32Imm(Log2_32(Imm)));
1034 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
1035 SDValue(Op, 0), SDValue(Op, 1));
1036 } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) {
1038 CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue,
1039 N0, getI32Imm(Log2_32(-Imm)));
1041 SDValue(CurDAG->getMachineNode(PPC::ADDZE, dl, MVT::i32,
1042 SDValue(Op, 0), SDValue(Op, 1)),
1044 return CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT);
1048 // Other cases are autogenerated.
1053 // Handle preincrement loads.
1054 LoadSDNode *LD = cast<LoadSDNode>(N);
1055 EVT LoadedVT = LD->getMemoryVT();
1057 // Normal loads are handled by code generated from the .td file.
1058 if (LD->getAddressingMode() != ISD::PRE_INC)
1061 SDValue Offset = LD->getOffset();
1062 if (Offset.getOpcode() == ISD::TargetConstant ||
1063 Offset.getOpcode() == ISD::TargetGlobalAddress) {
1066 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
1067 if (LD->getValueType(0) != MVT::i64) {
1068 // Handle PPC32 integer and normal FP loads.
1069 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
1070 switch (LoadedVT.getSimpleVT().SimpleTy) {
1071 default: llvm_unreachable("Invalid PPC load type!");
1072 case MVT::f64: Opcode = PPC::LFDU; break;
1073 case MVT::f32: Opcode = PPC::LFSU; break;
1074 case MVT::i32: Opcode = PPC::LWZU; break;
1075 case MVT::i16: Opcode = isSExt ? PPC::LHAU : PPC::LHZU; break;
1077 case MVT::i8: Opcode = PPC::LBZU; break;
1080 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
1081 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
1082 switch (LoadedVT.getSimpleVT().SimpleTy) {
1083 default: llvm_unreachable("Invalid PPC load type!");
1084 case MVT::i64: Opcode = PPC::LDU; break;
1085 case MVT::i32: Opcode = PPC::LWZU8; break;
1086 case MVT::i16: Opcode = isSExt ? PPC::LHAU8 : PPC::LHZU8; break;
1088 case MVT::i8: Opcode = PPC::LBZU8; break;
1092 SDValue Chain = LD->getChain();
1093 SDValue Base = LD->getBasePtr();
1094 SDValue Ops[] = { Offset, Base, Chain };
1095 return CurDAG->getMachineNode(Opcode, dl, LD->getValueType(0),
1096 PPCLowering.getPointerTy(),
1100 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
1101 if (LD->getValueType(0) != MVT::i64) {
1102 // Handle PPC32 integer and normal FP loads.
1103 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
1104 switch (LoadedVT.getSimpleVT().SimpleTy) {
1105 default: llvm_unreachable("Invalid PPC load type!");
1106 case MVT::f64: Opcode = PPC::LFDUX; break;
1107 case MVT::f32: Opcode = PPC::LFSUX; break;
1108 case MVT::i32: Opcode = PPC::LWZUX; break;
1109 case MVT::i16: Opcode = isSExt ? PPC::LHAUX : PPC::LHZUX; break;
1111 case MVT::i8: Opcode = PPC::LBZUX; break;
1114 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
1115 assert((!isSExt || LoadedVT == MVT::i16 || LoadedVT == MVT::i32) &&
1116 "Invalid sext update load");
1117 switch (LoadedVT.getSimpleVT().SimpleTy) {
1118 default: llvm_unreachable("Invalid PPC load type!");
1119 case MVT::i64: Opcode = PPC::LDUX; break;
1120 case MVT::i32: Opcode = isSExt ? PPC::LWAUX : PPC::LWZUX8; break;
1121 case MVT::i16: Opcode = isSExt ? PPC::LHAUX8 : PPC::LHZUX8; break;
1123 case MVT::i8: Opcode = PPC::LBZUX8; break;
1127 SDValue Chain = LD->getChain();
1128 SDValue Base = LD->getBasePtr();
1129 SDValue Ops[] = { Base, Offset, Chain };
1130 return CurDAG->getMachineNode(Opcode, dl, LD->getValueType(0),
1131 PPCLowering.getPointerTy(),
1137 unsigned Imm, Imm2, SH, MB, ME;
1140 // If this is an and of a value rotated between 0 and 31 bits and then and'd
1141 // with a mask, emit rlwinm
1142 if (isInt32Immediate(N->getOperand(1), Imm) &&
1143 isRotateAndMask(N->getOperand(0).getNode(), Imm, false, SH, MB, ME)) {
1144 SDValue Val = N->getOperand(0).getOperand(0);
1145 SDValue Ops[] = { Val, getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
1146 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
1148 // If this is just a masked value where the input is not handled above, and
1149 // is not a rotate-left (handled by a pattern in the .td file), emit rlwinm
1150 if (isInt32Immediate(N->getOperand(1), Imm) &&
1151 isRunOfOnes(Imm, MB, ME) &&
1152 N->getOperand(0).getOpcode() != ISD::ROTL) {
1153 SDValue Val = N->getOperand(0);
1154 SDValue Ops[] = { Val, getI32Imm(0), getI32Imm(MB), getI32Imm(ME) };
1155 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
1157 // If this is a 64-bit zero-extension mask, emit rldicl.
1158 if (isInt64Immediate(N->getOperand(1).getNode(), Imm64) &&
1160 SDValue Val = N->getOperand(0);
1161 MB = 64 - CountTrailingOnes_64(Imm64);
1164 // If the operand is a logical right shift, we can fold it into this
1165 // instruction: rldicl(rldicl(x, 64-n, n), 0, mb) -> rldicl(x, 64-n, mb)
1166 // for n <= mb. The right shift is really a left rotate followed by a
1167 // mask, and this mask is a more-restrictive sub-mask of the mask implied
1169 if (Val.getOpcode() == ISD::SRL &&
1170 isInt32Immediate(Val.getOperand(1).getNode(), Imm) && Imm <= MB) {
1171 assert(Imm < 64 && "Illegal shift amount");
1172 Val = Val.getOperand(0);
1176 SDValue Ops[] = { Val, getI32Imm(SH), getI32Imm(MB) };
1177 return CurDAG->SelectNodeTo(N, PPC::RLDICL, MVT::i64, Ops, 3);
1179 // AND X, 0 -> 0, not "rlwinm 32".
1180 if (isInt32Immediate(N->getOperand(1), Imm) && (Imm == 0)) {
1181 ReplaceUses(SDValue(N, 0), N->getOperand(1));
1184 // ISD::OR doesn't get all the bitfield insertion fun.
1185 // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) is a bitfield insert
1186 if (isInt32Immediate(N->getOperand(1), Imm) &&
1187 N->getOperand(0).getOpcode() == ISD::OR &&
1188 isInt32Immediate(N->getOperand(0).getOperand(1), Imm2)) {
1191 if (isRunOfOnes(Imm, MB, ME)) {
1192 SDValue Ops[] = { N->getOperand(0).getOperand(0),
1193 N->getOperand(0).getOperand(1),
1194 getI32Imm(0), getI32Imm(MB),getI32Imm(ME) };
1195 return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops);
1199 // Other cases are autogenerated.
1203 if (N->getValueType(0) == MVT::i32)
1204 if (SDNode *I = SelectBitfieldInsert(N))
1207 // Other cases are autogenerated.
1210 unsigned Imm, SH, MB, ME;
1211 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
1212 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
1213 SDValue Ops[] = { N->getOperand(0).getOperand(0),
1214 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
1215 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
1218 // Other cases are autogenerated.
1222 unsigned Imm, SH, MB, ME;
1223 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
1224 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
1225 SDValue Ops[] = { N->getOperand(0).getOperand(0),
1226 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
1227 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
1230 // Other cases are autogenerated.
1233 // FIXME: Remove this once the ANDI glue bug is fixed:
1234 case PPCISD::ANDIo_1_EQ_BIT:
1235 case PPCISD::ANDIo_1_GT_BIT: {
1239 EVT InVT = N->getOperand(0).getValueType();
1240 assert((InVT == MVT::i64 || InVT == MVT::i32) &&
1241 "Invalid input type for ANDIo_1_EQ_BIT");
1243 unsigned Opcode = (InVT == MVT::i64) ? PPC::ANDIo8 : PPC::ANDIo;
1244 SDValue AndI(CurDAG->getMachineNode(Opcode, dl, InVT, MVT::Glue,
1246 CurDAG->getTargetConstant(1, InVT)), 0);
1247 SDValue CR0Reg = CurDAG->getRegister(PPC::CR0, MVT::i32);
1249 CurDAG->getTargetConstant(N->getOpcode() == PPCISD::ANDIo_1_EQ_BIT ?
1250 PPC::sub_eq : PPC::sub_gt, MVT::i32);
1252 return CurDAG->SelectNodeTo(N, TargetOpcode::EXTRACT_SUBREG, MVT::i1,
1254 SDValue(AndI.getNode(), 1) /* glue */);
1256 case ISD::SELECT_CC: {
1257 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
1258 EVT PtrVT = CurDAG->getTargetLoweringInfo().getPointerTy();
1259 bool isPPC64 = (PtrVT == MVT::i64);
1261 // If this is a select of i1 operands, we'll pattern match it.
1262 if (PPCSubTarget.useCRBits() &&
1263 N->getOperand(0).getValueType() == MVT::i1)
1266 // Handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
1268 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1269 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1270 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1271 if (N1C->isNullValue() && N3C->isNullValue() &&
1272 N2C->getZExtValue() == 1ULL && CC == ISD::SETNE &&
1273 // FIXME: Implement this optzn for PPC64.
1274 N->getValueType(0) == MVT::i32) {
1276 CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
1277 N->getOperand(0), getI32Imm(~0U));
1278 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32,
1279 SDValue(Tmp, 0), N->getOperand(0),
1283 SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC, dl);
1285 if (N->getValueType(0) == MVT::i1) {
1286 // An i1 select is: (c & t) | (!c & f).
1288 unsigned Idx = getCRIdxForSetCC(CC, Inv);
1292 default: llvm_unreachable("Invalid CC index");
1293 case 0: SRI = PPC::sub_lt; break;
1294 case 1: SRI = PPC::sub_gt; break;
1295 case 2: SRI = PPC::sub_eq; break;
1296 case 3: SRI = PPC::sub_un; break;
1299 SDValue CCBit = CurDAG->getTargetExtractSubreg(SRI, dl, MVT::i1, CCReg);
1301 SDValue NotCCBit(CurDAG->getMachineNode(PPC::CRNOR, dl, MVT::i1,
1303 SDValue C = Inv ? NotCCBit : CCBit,
1304 NotC = Inv ? CCBit : NotCCBit;
1306 SDValue CAndT(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1,
1307 C, N->getOperand(2)), 0);
1308 SDValue NotCAndF(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1,
1309 NotC, N->getOperand(3)), 0);
1311 return CurDAG->SelectNodeTo(N, PPC::CROR, MVT::i1, CAndT, NotCAndF);
1314 unsigned BROpc = getPredicateForSetCC(CC);
1316 unsigned SelectCCOp;
1317 if (N->getValueType(0) == MVT::i32)
1318 SelectCCOp = PPC::SELECT_CC_I4;
1319 else if (N->getValueType(0) == MVT::i64)
1320 SelectCCOp = PPC::SELECT_CC_I8;
1321 else if (N->getValueType(0) == MVT::f32)
1322 SelectCCOp = PPC::SELECT_CC_F4;
1323 else if (N->getValueType(0) == MVT::f64)
1324 SelectCCOp = PPC::SELECT_CC_F8;
1326 SelectCCOp = PPC::SELECT_CC_VRRC;
1328 SDValue Ops[] = { CCReg, N->getOperand(2), N->getOperand(3),
1330 return CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), Ops, 4);
1333 if (PPCSubTarget.hasVSX()) {
1334 SDValue Ops[] = { N->getOperand(2), N->getOperand(1), N->getOperand(0) };
1335 return CurDAG->SelectNodeTo(N, PPC::XXSEL, N->getValueType(0), Ops, 3);
1339 case ISD::VECTOR_SHUFFLE:
1340 if (PPCSubTarget.hasVSX() && (N->getValueType(0) == MVT::v2f64 ||
1341 N->getValueType(0) == MVT::v2i64)) {
1342 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
1344 SDValue Op1 = N->getOperand(SVN->getMaskElt(0) < 2 ? 0 : 1),
1345 Op2 = N->getOperand(SVN->getMaskElt(1) < 2 ? 0 : 1);
1348 for (int i = 0; i < 2; ++i)
1349 if (SVN->getMaskElt(i) <= 0 || SVN->getMaskElt(i) == 2)
1354 SDValue DMV = CurDAG->getTargetConstant(DM[0] | (DM[1] << 1), MVT::i32);
1356 if (Op1 == Op2 && DM[0] == 0 && DM[1] == 0 &&
1357 Op1.getOpcode() == ISD::SCALAR_TO_VECTOR &&
1358 isa<LoadSDNode>(Op1.getOperand(0))) {
1359 LoadSDNode *LD = cast<LoadSDNode>(Op1.getOperand(0));
1360 SDValue Base, Offset;
1362 if (LD->isUnindexed() &&
1363 SelectAddrIdxOnly(LD->getBasePtr(), Base, Offset)) {
1364 SDValue Chain = LD->getChain();
1365 SDValue Ops[] = { Base, Offset, Chain };
1366 return CurDAG->SelectNodeTo(N, PPC::LXVDSX,
1367 N->getValueType(0), Ops, 3);
1371 SDValue Ops[] = { Op1, Op2, DMV };
1372 return CurDAG->SelectNodeTo(N, PPC::XXPERMDI, N->getValueType(0), Ops, 3);
1378 bool IsPPC64 = PPCSubTarget.isPPC64();
1379 SDValue Ops[] = { N->getOperand(1), N->getOperand(0) };
1380 return CurDAG->SelectNodeTo(N, N->getOpcode() == PPCISD::BDNZ ?
1381 (IsPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
1382 (IsPPC64 ? PPC::BDZ8 : PPC::BDZ),
1383 MVT::Other, Ops, 2);
1385 case PPCISD::COND_BRANCH: {
1386 // Op #0 is the Chain.
1387 // Op #1 is the PPC::PRED_* number.
1389 // Op #3 is the Dest MBB
1390 // Op #4 is the Flag.
1391 // Prevent PPC::PRED_* from being selected into LI.
1393 getI32Imm(cast<ConstantSDNode>(N->getOperand(1))->getZExtValue());
1394 SDValue Ops[] = { Pred, N->getOperand(2), N->getOperand(3),
1395 N->getOperand(0), N->getOperand(4) };
1396 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops, 5);
1399 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
1400 unsigned PCC = getPredicateForSetCC(CC);
1402 if (N->getOperand(2).getValueType() == MVT::i1) {
1406 default: llvm_unreachable("Unexpected Boolean-operand predicate");
1407 case PPC::PRED_LT: Opc = PPC::CRANDC; Swap = true; break;
1408 case PPC::PRED_LE: Opc = PPC::CRORC; Swap = true; break;
1409 case PPC::PRED_EQ: Opc = PPC::CREQV; Swap = false; break;
1410 case PPC::PRED_GE: Opc = PPC::CRORC; Swap = false; break;
1411 case PPC::PRED_GT: Opc = PPC::CRANDC; Swap = false; break;
1412 case PPC::PRED_NE: Opc = PPC::CRXOR; Swap = false; break;
1415 SDValue BitComp(CurDAG->getMachineNode(Opc, dl, MVT::i1,
1416 N->getOperand(Swap ? 3 : 2),
1417 N->getOperand(Swap ? 2 : 3)), 0);
1418 return CurDAG->SelectNodeTo(N, PPC::BC, MVT::Other,
1419 BitComp, N->getOperand(4), N->getOperand(0));
1422 SDValue CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC, dl);
1423 SDValue Ops[] = { getI32Imm(PCC), CondCode,
1424 N->getOperand(4), N->getOperand(0) };
1425 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops, 4);
1428 // FIXME: Should custom lower this.
1429 SDValue Chain = N->getOperand(0);
1430 SDValue Target = N->getOperand(1);
1431 unsigned Opc = Target.getValueType() == MVT::i32 ? PPC::MTCTR : PPC::MTCTR8;
1432 unsigned Reg = Target.getValueType() == MVT::i32 ? PPC::BCTR : PPC::BCTR8;
1433 Chain = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Glue, Target,
1435 return CurDAG->SelectNodeTo(N, Reg, MVT::Other, Chain);
1437 case PPCISD::TOC_ENTRY: {
1438 assert (PPCSubTarget.isPPC64() && "Only supported for 64-bit ABI");
1440 // For medium and large code model, we generate two instructions as
1441 // described below. Otherwise we allow SelectCodeCommon to handle this,
1442 // selecting one of LDtoc, LDtocJTI, and LDtocCPT.
1443 CodeModel::Model CModel = TM.getCodeModel();
1444 if (CModel != CodeModel::Medium && CModel != CodeModel::Large)
1447 // The first source operand is a TargetGlobalAddress or a
1448 // TargetJumpTable. If it is an externally defined symbol, a symbol
1449 // with common linkage, a function address, or a jump table address,
1450 // or if we are generating code for large code model, we generate:
1451 // LDtocL(<ga:@sym>, ADDIStocHA(%X2, <ga:@sym>))
1452 // Otherwise we generate:
1453 // ADDItocL(ADDIStocHA(%X2, <ga:@sym>), <ga:@sym>)
1454 SDValue GA = N->getOperand(0);
1455 SDValue TOCbase = N->getOperand(1);
1456 SDNode *Tmp = CurDAG->getMachineNode(PPC::ADDIStocHA, dl, MVT::i64,
1459 if (isa<JumpTableSDNode>(GA) || CModel == CodeModel::Large)
1460 return CurDAG->getMachineNode(PPC::LDtocL, dl, MVT::i64, GA,
1463 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(GA)) {
1464 const GlobalValue *GValue = G->getGlobal();
1465 const GlobalAlias *GAlias = dyn_cast<GlobalAlias>(GValue);
1466 const GlobalValue *RealGValue =
1467 GAlias ? GAlias->getAliasedGlobal() : GValue;
1468 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(RealGValue);
1469 assert((GVar || isa<Function>(RealGValue)) &&
1470 "Unexpected global value subclass!");
1472 // An external variable is one without an initializer. For these,
1473 // for variables with common linkage, and for Functions, generate
1475 if (!GVar || !GVar->hasInitializer() || RealGValue->hasCommonLinkage() ||
1476 RealGValue->hasAvailableExternallyLinkage())
1477 return CurDAG->getMachineNode(PPC::LDtocL, dl, MVT::i64, GA,
1481 return CurDAG->getMachineNode(PPC::ADDItocL, dl, MVT::i64,
1482 SDValue(Tmp, 0), GA);
1484 case PPCISD::VADD_SPLAT: {
1485 // This expands into one of three sequences, depending on whether
1486 // the first operand is odd or even, positive or negative.
1487 assert(isa<ConstantSDNode>(N->getOperand(0)) &&
1488 isa<ConstantSDNode>(N->getOperand(1)) &&
1489 "Invalid operand on VADD_SPLAT!");
1491 int Elt = N->getConstantOperandVal(0);
1492 int EltSize = N->getConstantOperandVal(1);
1493 unsigned Opc1, Opc2, Opc3;
1497 Opc1 = PPC::VSPLTISB;
1498 Opc2 = PPC::VADDUBM;
1499 Opc3 = PPC::VSUBUBM;
1501 } else if (EltSize == 2) {
1502 Opc1 = PPC::VSPLTISH;
1503 Opc2 = PPC::VADDUHM;
1504 Opc3 = PPC::VSUBUHM;
1507 assert(EltSize == 4 && "Invalid element size on VADD_SPLAT!");
1508 Opc1 = PPC::VSPLTISW;
1509 Opc2 = PPC::VADDUWM;
1510 Opc3 = PPC::VSUBUWM;
1514 if ((Elt & 1) == 0) {
1515 // Elt is even, in the range [-32,-18] + [16,30].
1517 // Convert: VADD_SPLAT elt, size
1518 // Into: tmp = VSPLTIS[BHW] elt
1519 // VADDU[BHW]M tmp, tmp
1520 // Where: [BHW] = B for size = 1, H for size = 2, W for size = 4
1521 SDValue EltVal = getI32Imm(Elt >> 1);
1522 SDNode *Tmp = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1523 SDValue TmpVal = SDValue(Tmp, 0);
1524 return CurDAG->getMachineNode(Opc2, dl, VT, TmpVal, TmpVal);
1526 } else if (Elt > 0) {
1527 // Elt is odd and positive, in the range [17,31].
1529 // Convert: VADD_SPLAT elt, size
1530 // Into: tmp1 = VSPLTIS[BHW] elt-16
1531 // tmp2 = VSPLTIS[BHW] -16
1532 // VSUBU[BHW]M tmp1, tmp2
1533 SDValue EltVal = getI32Imm(Elt - 16);
1534 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1535 EltVal = getI32Imm(-16);
1536 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1537 return CurDAG->getMachineNode(Opc3, dl, VT, SDValue(Tmp1, 0),
1541 // Elt is odd and negative, in the range [-31,-17].
1543 // Convert: VADD_SPLAT elt, size
1544 // Into: tmp1 = VSPLTIS[BHW] elt+16
1545 // tmp2 = VSPLTIS[BHW] -16
1546 // VADDU[BHW]M tmp1, tmp2
1547 SDValue EltVal = getI32Imm(Elt + 16);
1548 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1549 EltVal = getI32Imm(-16);
1550 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1551 return CurDAG->getMachineNode(Opc2, dl, VT, SDValue(Tmp1, 0),
1557 return SelectCode(N);
1560 /// PostprocessISelDAG - Perform some late peephole optimizations
1561 /// on the DAG representation.
1562 void PPCDAGToDAGISel::PostprocessISelDAG() {
1564 // Skip peepholes at -O0.
1565 if (TM.getOptLevel() == CodeGenOpt::None)
1572 // Check if all users of this node will become isel where the second operand
1573 // is the constant zero. If this is so, and if we can negate the condition,
1574 // then we can flip the true and false operands. This will allow the zero to
1575 // be folded with the isel so that we don't need to materialize a register
1577 bool PPCDAGToDAGISel::AllUsersSelectZero(SDNode *N) {
1578 // If we're not using isel, then this does not matter.
1579 if (!PPCSubTarget.hasISEL())
1582 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
1585 if (!User->isMachineOpcode())
1587 if (User->getMachineOpcode() != PPC::SELECT_I4 &&
1588 User->getMachineOpcode() != PPC::SELECT_I8)
1591 SDNode *Op2 = User->getOperand(2).getNode();
1592 if (!Op2->isMachineOpcode())
1595 if (Op2->getMachineOpcode() != PPC::LI &&
1596 Op2->getMachineOpcode() != PPC::LI8)
1599 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op2->getOperand(0));
1603 if (!C->isNullValue())
1610 void PPCDAGToDAGISel::SwapAllSelectUsers(SDNode *N) {
1611 SmallVector<SDNode *, 4> ToReplace;
1612 for (SDNode::use_iterator UI = N->use_begin(), UE = N->use_end();
1615 assert((User->getMachineOpcode() == PPC::SELECT_I4 ||
1616 User->getMachineOpcode() == PPC::SELECT_I8) &&
1617 "Must have all select users");
1618 ToReplace.push_back(User);
1621 for (SmallVector<SDNode *, 4>::iterator UI = ToReplace.begin(),
1622 UE = ToReplace.end(); UI != UE; ++UI) {
1625 CurDAG->getMachineNode(User->getMachineOpcode(), SDLoc(User),
1626 User->getValueType(0), User->getOperand(0),
1627 User->getOperand(2),
1628 User->getOperand(1));
1630 DEBUG(dbgs() << "CR Peephole replacing:\nOld: ");
1631 DEBUG(User->dump(CurDAG));
1632 DEBUG(dbgs() << "\nNew: ");
1633 DEBUG(ResNode->dump(CurDAG));
1634 DEBUG(dbgs() << "\n");
1636 ReplaceUses(User, ResNode);
1640 void PPCDAGToDAGISel::PeepholdCROps() {
1644 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
1645 E = CurDAG->allnodes_end(); I != E; ++I) {
1646 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(I);
1647 if (!MachineNode || MachineNode->use_empty())
1649 SDNode *ResNode = MachineNode;
1651 bool Op1Set = false, Op1Unset = false,
1653 Op2Set = false, Op2Unset = false,
1656 unsigned Opcode = MachineNode->getMachineOpcode();
1667 SDValue Op = MachineNode->getOperand(1);
1668 if (Op.isMachineOpcode()) {
1669 if (Op.getMachineOpcode() == PPC::CRSET)
1671 else if (Op.getMachineOpcode() == PPC::CRUNSET)
1673 else if (Op.getMachineOpcode() == PPC::CRNOR &&
1674 Op.getOperand(0) == Op.getOperand(1))
1680 case PPC::SELECT_I4:
1681 case PPC::SELECT_I8:
1682 case PPC::SELECT_F4:
1683 case PPC::SELECT_F8:
1684 case PPC::SELECT_VRRC: {
1685 SDValue Op = MachineNode->getOperand(0);
1686 if (Op.isMachineOpcode()) {
1687 if (Op.getMachineOpcode() == PPC::CRSET)
1689 else if (Op.getMachineOpcode() == PPC::CRUNSET)
1691 else if (Op.getMachineOpcode() == PPC::CRNOR &&
1692 Op.getOperand(0) == Op.getOperand(1))
1699 bool SelectSwap = false;
1703 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1705 ResNode = MachineNode->getOperand(0).getNode();
1708 ResNode = MachineNode->getOperand(1).getNode();
1711 ResNode = MachineNode->getOperand(0).getNode();
1712 else if (Op1Unset || Op2Unset)
1713 // x & 0 = 0 & y = 0
1714 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1717 // ~x & y = andc(y, x)
1718 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1719 MVT::i1, MachineNode->getOperand(1),
1720 MachineNode->getOperand(0).
1723 // x & ~y = andc(x, y)
1724 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1725 MVT::i1, MachineNode->getOperand(0),
1726 MachineNode->getOperand(1).
1728 else if (AllUsersSelectZero(MachineNode))
1729 ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode),
1730 MVT::i1, MachineNode->getOperand(0),
1731 MachineNode->getOperand(1)),
1735 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1736 // nand(x, x) -> nor(x, x)
1737 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1738 MVT::i1, MachineNode->getOperand(0),
1739 MachineNode->getOperand(0));
1741 // nand(1, y) -> nor(y, y)
1742 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1743 MVT::i1, MachineNode->getOperand(1),
1744 MachineNode->getOperand(1));
1746 // nand(x, 1) -> nor(x, x)
1747 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1748 MVT::i1, MachineNode->getOperand(0),
1749 MachineNode->getOperand(0));
1750 else if (Op1Unset || Op2Unset)
1751 // nand(x, 0) = nand(0, y) = 1
1752 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1755 // nand(~x, y) = ~(~x & y) = x | ~y = orc(x, y)
1756 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1757 MVT::i1, MachineNode->getOperand(0).
1759 MachineNode->getOperand(1));
1761 // nand(x, ~y) = ~x | y = orc(y, x)
1762 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1763 MVT::i1, MachineNode->getOperand(1).
1765 MachineNode->getOperand(0));
1766 else if (AllUsersSelectZero(MachineNode))
1767 ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode),
1768 MVT::i1, MachineNode->getOperand(0),
1769 MachineNode->getOperand(1)),
1773 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1775 ResNode = MachineNode->getOperand(0).getNode();
1776 else if (Op1Set || Op2Set)
1777 // x | 1 = 1 | y = 1
1778 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1782 ResNode = MachineNode->getOperand(1).getNode();
1785 ResNode = MachineNode->getOperand(0).getNode();
1787 // ~x | y = orc(y, x)
1788 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1789 MVT::i1, MachineNode->getOperand(1),
1790 MachineNode->getOperand(0).
1793 // x | ~y = orc(x, y)
1794 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1795 MVT::i1, MachineNode->getOperand(0),
1796 MachineNode->getOperand(1).
1798 else if (AllUsersSelectZero(MachineNode))
1799 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1800 MVT::i1, MachineNode->getOperand(0),
1801 MachineNode->getOperand(1)),
1805 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1807 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1810 // xor(1, y) -> nor(y, y)
1811 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1812 MVT::i1, MachineNode->getOperand(1),
1813 MachineNode->getOperand(1));
1815 // xor(x, 1) -> nor(x, x)
1816 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1817 MVT::i1, MachineNode->getOperand(0),
1818 MachineNode->getOperand(0));
1821 ResNode = MachineNode->getOperand(1).getNode();
1824 ResNode = MachineNode->getOperand(0).getNode();
1826 // xor(~x, y) = eqv(x, y)
1827 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
1828 MVT::i1, MachineNode->getOperand(0).
1830 MachineNode->getOperand(1));
1832 // xor(x, ~y) = eqv(x, y)
1833 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
1834 MVT::i1, MachineNode->getOperand(0),
1835 MachineNode->getOperand(1).
1837 else if (AllUsersSelectZero(MachineNode))
1838 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
1839 MVT::i1, MachineNode->getOperand(0),
1840 MachineNode->getOperand(1)),
1844 if (Op1Set || Op2Set)
1846 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1849 // nor(0, y) = ~y -> nor(y, y)
1850 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1851 MVT::i1, MachineNode->getOperand(1),
1852 MachineNode->getOperand(1));
1855 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1856 MVT::i1, MachineNode->getOperand(0),
1857 MachineNode->getOperand(0));
1859 // nor(~x, y) = andc(x, y)
1860 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1861 MVT::i1, MachineNode->getOperand(0).
1863 MachineNode->getOperand(1));
1865 // nor(x, ~y) = andc(y, x)
1866 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1867 MVT::i1, MachineNode->getOperand(1).
1869 MachineNode->getOperand(0));
1870 else if (AllUsersSelectZero(MachineNode))
1871 ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode),
1872 MVT::i1, MachineNode->getOperand(0),
1873 MachineNode->getOperand(1)),
1877 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1879 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1883 ResNode = MachineNode->getOperand(1).getNode();
1886 ResNode = MachineNode->getOperand(0).getNode();
1888 // eqv(0, y) = ~y -> nor(y, y)
1889 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1890 MVT::i1, MachineNode->getOperand(1),
1891 MachineNode->getOperand(1));
1894 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1895 MVT::i1, MachineNode->getOperand(0),
1896 MachineNode->getOperand(0));
1898 // eqv(~x, y) = xor(x, y)
1899 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
1900 MVT::i1, MachineNode->getOperand(0).
1902 MachineNode->getOperand(1));
1904 // eqv(x, ~y) = xor(x, y)
1905 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
1906 MVT::i1, MachineNode->getOperand(0),
1907 MachineNode->getOperand(1).
1909 else if (AllUsersSelectZero(MachineNode))
1910 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
1911 MVT::i1, MachineNode->getOperand(0),
1912 MachineNode->getOperand(1)),
1916 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1918 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1922 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1923 MVT::i1, MachineNode->getOperand(1),
1924 MachineNode->getOperand(1));
1925 else if (Op1Unset || Op2Set)
1926 // andc(0, y) = andc(x, 1) = 0
1927 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1931 ResNode = MachineNode->getOperand(0).getNode();
1933 // andc(~x, y) = ~(x | y) = nor(x, y)
1934 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1935 MVT::i1, MachineNode->getOperand(0).
1937 MachineNode->getOperand(1));
1939 // andc(x, ~y) = x & y
1940 ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode),
1941 MVT::i1, MachineNode->getOperand(0),
1942 MachineNode->getOperand(1).
1944 else if (AllUsersSelectZero(MachineNode))
1945 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1946 MVT::i1, MachineNode->getOperand(1),
1947 MachineNode->getOperand(0)),
1951 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1953 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1955 else if (Op1Set || Op2Unset)
1956 // orc(1, y) = orc(x, 0) = 1
1957 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1961 ResNode = MachineNode->getOperand(0).getNode();
1964 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1965 MVT::i1, MachineNode->getOperand(1),
1966 MachineNode->getOperand(1));
1968 // orc(~x, y) = ~(x & y) = nand(x, y)
1969 ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode),
1970 MVT::i1, MachineNode->getOperand(0).
1972 MachineNode->getOperand(1));
1974 // orc(x, ~y) = x | y
1975 ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode),
1976 MVT::i1, MachineNode->getOperand(0),
1977 MachineNode->getOperand(1).
1979 else if (AllUsersSelectZero(MachineNode))
1980 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1981 MVT::i1, MachineNode->getOperand(1),
1982 MachineNode->getOperand(0)),
1985 case PPC::SELECT_I4:
1986 case PPC::SELECT_I8:
1987 case PPC::SELECT_F4:
1988 case PPC::SELECT_F8:
1989 case PPC::SELECT_VRRC:
1991 ResNode = MachineNode->getOperand(1).getNode();
1993 ResNode = MachineNode->getOperand(2).getNode();
1995 ResNode = CurDAG->getMachineNode(MachineNode->getMachineOpcode(),
1997 MachineNode->getValueType(0),
1998 MachineNode->getOperand(0).
2000 MachineNode->getOperand(2),
2001 MachineNode->getOperand(1));
2006 ResNode = CurDAG->getMachineNode(Opcode == PPC::BC ? PPC::BCn :
2010 MachineNode->getOperand(0).
2012 MachineNode->getOperand(1),
2013 MachineNode->getOperand(2));
2014 // FIXME: Handle Op1Set, Op1Unset here too.
2018 // If we're inverting this node because it is used only by selects that
2019 // we'd like to swap, then swap the selects before the node replacement.
2021 SwapAllSelectUsers(MachineNode);
2023 if (ResNode != MachineNode) {
2024 DEBUG(dbgs() << "CR Peephole replacing:\nOld: ");
2025 DEBUG(MachineNode->dump(CurDAG));
2026 DEBUG(dbgs() << "\nNew: ");
2027 DEBUG(ResNode->dump(CurDAG));
2028 DEBUG(dbgs() << "\n");
2030 ReplaceUses(MachineNode, ResNode);
2035 CurDAG->RemoveDeadNodes();
2036 } while (IsModified);
2039 void PPCDAGToDAGISel::PeepholePPC64() {
2040 // These optimizations are currently supported only for 64-bit SVR4.
2041 if (PPCSubTarget.isDarwin() || !PPCSubTarget.isPPC64())
2044 SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode());
2047 while (Position != CurDAG->allnodes_begin()) {
2048 SDNode *N = --Position;
2049 // Skip dead nodes and any non-machine opcodes.
2050 if (N->use_empty() || !N->isMachineOpcode())
2054 unsigned StorageOpcode = N->getMachineOpcode();
2056 switch (StorageOpcode) {
2087 // If this is a load or store with a zero offset, we may be able to
2088 // fold an add-immediate into the memory operation.
2089 if (!isa<ConstantSDNode>(N->getOperand(FirstOp)) ||
2090 N->getConstantOperandVal(FirstOp) != 0)
2093 SDValue Base = N->getOperand(FirstOp + 1);
2094 if (!Base.isMachineOpcode())
2098 bool ReplaceFlags = true;
2100 // When the feeding operation is an add-immediate of some sort,
2101 // determine whether we need to add relocation information to the
2102 // target flags on the immediate operand when we fold it into the
2103 // load instruction.
2105 // For something like ADDItocL, the relocation information is
2106 // inferred from the opcode; when we process it in the AsmPrinter,
2107 // we add the necessary relocation there. A load, though, can receive
2108 // relocation from various flavors of ADDIxxx, so we need to carry
2109 // the relocation information in the target flags.
2110 switch (Base.getMachineOpcode()) {
2115 // In some cases (such as TLS) the relocation information
2116 // is already in place on the operand, so copying the operand
2118 ReplaceFlags = false;
2119 // For these cases, the immediate may not be divisible by 4, in
2120 // which case the fold is illegal for DS-form instructions. (The
2121 // other cases provide aligned addresses and are always safe.)
2122 if ((StorageOpcode == PPC::LWA ||
2123 StorageOpcode == PPC::LD ||
2124 StorageOpcode == PPC::STD) &&
2125 (!isa<ConstantSDNode>(Base.getOperand(1)) ||
2126 Base.getConstantOperandVal(1) % 4 != 0))
2129 case PPC::ADDIdtprelL:
2130 Flags = PPCII::MO_DTPREL_LO;
2132 case PPC::ADDItlsldL:
2133 Flags = PPCII::MO_TLSLD_LO;
2136 Flags = PPCII::MO_TOC_LO;
2140 // We found an opportunity. Reverse the operands from the add
2141 // immediate and substitute them into the load or store. If
2142 // needed, update the target flags for the immediate operand to
2143 // reflect the necessary relocation information.
2144 DEBUG(dbgs() << "Folding add-immediate into mem-op:\nBase: ");
2145 DEBUG(Base->dump(CurDAG));
2146 DEBUG(dbgs() << "\nN: ");
2147 DEBUG(N->dump(CurDAG));
2148 DEBUG(dbgs() << "\n");
2150 SDValue ImmOpnd = Base.getOperand(1);
2152 // If the relocation information isn't already present on the
2153 // immediate operand, add it now.
2155 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(ImmOpnd)) {
2157 const GlobalValue *GV = GA->getGlobal();
2158 // We can't perform this optimization for data whose alignment
2159 // is insufficient for the instruction encoding.
2160 if (GV->getAlignment() < 4 &&
2161 (StorageOpcode == PPC::LD || StorageOpcode == PPC::STD ||
2162 StorageOpcode == PPC::LWA)) {
2163 DEBUG(dbgs() << "Rejected this candidate for alignment.\n\n");
2166 ImmOpnd = CurDAG->getTargetGlobalAddress(GV, dl, MVT::i64, 0, Flags);
2167 } else if (ConstantPoolSDNode *CP =
2168 dyn_cast<ConstantPoolSDNode>(ImmOpnd)) {
2169 const Constant *C = CP->getConstVal();
2170 ImmOpnd = CurDAG->getTargetConstantPool(C, MVT::i64,
2176 if (FirstOp == 1) // Store
2177 (void)CurDAG->UpdateNodeOperands(N, N->getOperand(0), ImmOpnd,
2178 Base.getOperand(0), N->getOperand(3));
2180 (void)CurDAG->UpdateNodeOperands(N, ImmOpnd, Base.getOperand(0),
2183 // The add-immediate may now be dead, in which case remove it.
2184 if (Base.getNode()->use_empty())
2185 CurDAG->RemoveDeadNode(Base.getNode());
2190 /// createPPCISelDag - This pass converts a legalized DAG into a
2191 /// PowerPC-specific DAG, ready for instruction scheduling.
2193 FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) {
2194 return new PPCDAGToDAGISel(TM);
2197 static void initializePassOnce(PassRegistry &Registry) {
2198 const char *Name = "PowerPC DAG->DAG Pattern Instruction Selection";
2199 PassInfo *PI = new PassInfo(Name, "ppc-codegen", &SelectionDAGISel::ID, 0,
2201 Registry.registerPass(*PI, true);
2204 void llvm::initializePPCDAGToDAGISelPass(PassRegistry &Registry) {
2205 CALL_ONCE_INITIALIZATION(initializePassOnce);