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();
195 /// InsertVRSaveCode - Once the entire function has been instruction selected,
196 /// all virtual registers are created and all machine instructions are built,
197 /// check to see if we need to save/restore VRSAVE. If so, do it.
198 void PPCDAGToDAGISel::InsertVRSaveCode(MachineFunction &Fn) {
199 // Check to see if this function uses vector registers, which means we have to
200 // save and restore the VRSAVE register and update it with the regs we use.
202 // In this case, there will be virtual registers of vector type created
203 // by the scheduler. Detect them now.
204 bool HasVectorVReg = false;
205 for (unsigned i = 0, e = RegInfo->getNumVirtRegs(); i != e; ++i) {
206 unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
207 if (RegInfo->getRegClass(Reg) == &PPC::VRRCRegClass) {
208 HasVectorVReg = true;
212 if (!HasVectorVReg) return; // nothing to do.
214 // If we have a vector register, we want to emit code into the entry and exit
215 // blocks to save and restore the VRSAVE register. We do this here (instead
216 // of marking all vector instructions as clobbering VRSAVE) for two reasons:
218 // 1. This (trivially) reduces the load on the register allocator, by not
219 // having to represent the live range of the VRSAVE register.
220 // 2. This (more significantly) allows us to create a temporary virtual
221 // register to hold the saved VRSAVE value, allowing this temporary to be
222 // register allocated, instead of forcing it to be spilled to the stack.
224 // Create two vregs - one to hold the VRSAVE register that is live-in to the
225 // function and one for the value after having bits or'd into it.
226 unsigned InVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
227 unsigned UpdatedVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
229 const TargetInstrInfo &TII = *TM.getInstrInfo();
230 MachineBasicBlock &EntryBB = *Fn.begin();
232 // Emit the following code into the entry block:
233 // InVRSAVE = MFVRSAVE
234 // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE
235 // MTVRSAVE UpdatedVRSAVE
236 MachineBasicBlock::iterator IP = EntryBB.begin(); // Insert Point
237 BuildMI(EntryBB, IP, dl, TII.get(PPC::MFVRSAVE), InVRSAVE);
238 BuildMI(EntryBB, IP, dl, TII.get(PPC::UPDATE_VRSAVE),
239 UpdatedVRSAVE).addReg(InVRSAVE);
240 BuildMI(EntryBB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(UpdatedVRSAVE);
242 // Find all return blocks, outputting a restore in each epilog.
243 for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
244 if (!BB->empty() && BB->back().isReturn()) {
245 IP = BB->end(); --IP;
247 // Skip over all terminator instructions, which are part of the return
249 MachineBasicBlock::iterator I2 = IP;
250 while (I2 != BB->begin() && (--I2)->isTerminator())
253 // Emit: MTVRSAVE InVRSave
254 BuildMI(*BB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(InVRSAVE);
260 /// getGlobalBaseReg - Output the instructions required to put the
261 /// base address to use for accessing globals into a register.
263 SDNode *PPCDAGToDAGISel::getGlobalBaseReg() {
264 if (!GlobalBaseReg) {
265 const TargetInstrInfo &TII = *TM.getInstrInfo();
266 // Insert the set of GlobalBaseReg into the first MBB of the function
267 MachineBasicBlock &FirstMBB = MF->front();
268 MachineBasicBlock::iterator MBBI = FirstMBB.begin();
271 if (PPCLowering.getPointerTy() == MVT::i32) {
272 GlobalBaseReg = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
273 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR));
274 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
276 GlobalBaseReg = RegInfo->createVirtualRegister(&PPC::G8RCRegClass);
277 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR8));
278 BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR8), GlobalBaseReg);
281 return CurDAG->getRegister(GlobalBaseReg,
282 PPCLowering.getPointerTy()).getNode();
285 /// isIntS16Immediate - This method tests to see if the node is either a 32-bit
286 /// or 64-bit immediate, and if the value can be accurately represented as a
287 /// sign extension from a 16-bit value. If so, this returns true and the
289 static bool isIntS16Immediate(SDNode *N, short &Imm) {
290 if (N->getOpcode() != ISD::Constant)
293 Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
294 if (N->getValueType(0) == MVT::i32)
295 return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
297 return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
300 static bool isIntS16Immediate(SDValue Op, short &Imm) {
301 return isIntS16Immediate(Op.getNode(), Imm);
305 /// isInt32Immediate - This method tests to see if the node is a 32-bit constant
306 /// operand. If so Imm will receive the 32-bit value.
307 static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
308 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
309 Imm = cast<ConstantSDNode>(N)->getZExtValue();
315 /// isInt64Immediate - This method tests to see if the node is a 64-bit constant
316 /// operand. If so Imm will receive the 64-bit value.
317 static bool isInt64Immediate(SDNode *N, uint64_t &Imm) {
318 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i64) {
319 Imm = cast<ConstantSDNode>(N)->getZExtValue();
325 // isInt32Immediate - This method tests to see if a constant operand.
326 // If so Imm will receive the 32 bit value.
327 static bool isInt32Immediate(SDValue N, unsigned &Imm) {
328 return isInt32Immediate(N.getNode(), Imm);
332 // isOpcWithIntImmediate - This method tests to see if the node is a specific
333 // opcode and that it has a immediate integer right operand.
334 // If so Imm will receive the 32 bit value.
335 static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
336 return N->getOpcode() == Opc
337 && isInt32Immediate(N->getOperand(1).getNode(), Imm);
340 bool PPCDAGToDAGISel::isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
344 if (isShiftedMask_32(Val)) {
345 // look for the first non-zero bit
346 MB = countLeadingZeros(Val);
347 // look for the first zero bit after the run of ones
348 ME = countLeadingZeros((Val - 1) ^ Val);
351 Val = ~Val; // invert mask
352 if (isShiftedMask_32(Val)) {
353 // effectively look for the first zero bit
354 ME = countLeadingZeros(Val) - 1;
355 // effectively look for the first one bit after the run of zeros
356 MB = countLeadingZeros((Val - 1) ^ Val) + 1;
364 bool PPCDAGToDAGISel::isRotateAndMask(SDNode *N, unsigned Mask,
365 bool isShiftMask, unsigned &SH,
366 unsigned &MB, unsigned &ME) {
367 // Don't even go down this path for i64, since different logic will be
368 // necessary for rldicl/rldicr/rldimi.
369 if (N->getValueType(0) != MVT::i32)
373 unsigned Indeterminant = ~0; // bit mask marking indeterminant results
374 unsigned Opcode = N->getOpcode();
375 if (N->getNumOperands() != 2 ||
376 !isInt32Immediate(N->getOperand(1).getNode(), Shift) || (Shift > 31))
379 if (Opcode == ISD::SHL) {
380 // apply shift left to mask if it comes first
381 if (isShiftMask) Mask = Mask << Shift;
382 // determine which bits are made indeterminant by shift
383 Indeterminant = ~(0xFFFFFFFFu << Shift);
384 } else if (Opcode == ISD::SRL) {
385 // apply shift right to mask if it comes first
386 if (isShiftMask) Mask = Mask >> Shift;
387 // determine which bits are made indeterminant by shift
388 Indeterminant = ~(0xFFFFFFFFu >> Shift);
389 // adjust for the left rotate
391 } else if (Opcode == ISD::ROTL) {
397 // if the mask doesn't intersect any Indeterminant bits
398 if (Mask && !(Mask & Indeterminant)) {
400 // make sure the mask is still a mask (wrap arounds may not be)
401 return isRunOfOnes(Mask, MB, ME);
406 /// SelectBitfieldInsert - turn an or of two masked values into
407 /// the rotate left word immediate then mask insert (rlwimi) instruction.
408 SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
409 SDValue Op0 = N->getOperand(0);
410 SDValue Op1 = N->getOperand(1);
413 APInt LKZ, LKO, RKZ, RKO;
414 CurDAG->ComputeMaskedBits(Op0, LKZ, LKO);
415 CurDAG->ComputeMaskedBits(Op1, RKZ, RKO);
417 unsigned TargetMask = LKZ.getZExtValue();
418 unsigned InsertMask = RKZ.getZExtValue();
420 if ((TargetMask | InsertMask) == 0xFFFFFFFF) {
421 unsigned Op0Opc = Op0.getOpcode();
422 unsigned Op1Opc = Op1.getOpcode();
423 unsigned Value, SH = 0;
424 TargetMask = ~TargetMask;
425 InsertMask = ~InsertMask;
427 // If the LHS has a foldable shift and the RHS does not, then swap it to the
428 // RHS so that we can fold the shift into the insert.
429 if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
430 if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
431 Op0.getOperand(0).getOpcode() == ISD::SRL) {
432 if (Op1.getOperand(0).getOpcode() != ISD::SHL &&
433 Op1.getOperand(0).getOpcode() != ISD::SRL) {
435 std::swap(Op0Opc, Op1Opc);
436 std::swap(TargetMask, InsertMask);
439 } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) {
440 if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL &&
441 Op1.getOperand(0).getOpcode() != ISD::SRL) {
443 std::swap(Op0Opc, Op1Opc);
444 std::swap(TargetMask, InsertMask);
449 if (isRunOfOnes(InsertMask, MB, ME)) {
452 if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) &&
453 isInt32Immediate(Op1.getOperand(1), Value)) {
454 Op1 = Op1.getOperand(0);
455 SH = (Op1Opc == ISD::SHL) ? Value : 32 - Value;
457 if (Op1Opc == ISD::AND) {
458 unsigned SHOpc = Op1.getOperand(0).getOpcode();
459 if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) &&
460 isInt32Immediate(Op1.getOperand(0).getOperand(1), Value)) {
461 // Note that Value must be in range here (less than 32) because
462 // otherwise there would not be any bits set in InsertMask.
463 Op1 = Op1.getOperand(0).getOperand(0);
464 SH = (SHOpc == ISD::SHL) ? Value : 32 - Value;
469 SDValue Ops[] = { Op0, Op1, getI32Imm(SH), getI32Imm(MB),
471 return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops);
477 /// SelectCC - Select a comparison of the specified values with the specified
478 /// condition code, returning the CR# of the expression.
479 SDValue PPCDAGToDAGISel::SelectCC(SDValue LHS, SDValue RHS,
480 ISD::CondCode CC, SDLoc dl) {
481 // Always select the LHS.
484 if (LHS.getValueType() == MVT::i32) {
486 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
487 if (isInt32Immediate(RHS, Imm)) {
488 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
490 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
491 getI32Imm(Imm & 0xFFFF)), 0);
492 // If this is a 16-bit signed immediate, fold it.
493 if (isInt<16>((int)Imm))
494 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
495 getI32Imm(Imm & 0xFFFF)), 0);
497 // For non-equality comparisons, the default code would materialize the
498 // constant, then compare against it, like this:
502 // Since we are just comparing for equality, we can emit this instead:
503 // xoris r0,r3,0x1234
504 // cmplwi cr0,r0,0x5678
506 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS, dl, MVT::i32, LHS,
507 getI32Imm(Imm >> 16)), 0);
508 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, Xor,
509 getI32Imm(Imm & 0xFFFF)), 0);
512 } else if (ISD::isUnsignedIntSetCC(CC)) {
513 if (isInt32Immediate(RHS, Imm) && isUInt<16>(Imm))
514 return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
515 getI32Imm(Imm & 0xFFFF)), 0);
519 if (isIntS16Immediate(RHS, SImm))
520 return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
521 getI32Imm((int)SImm & 0xFFFF)),
525 } else if (LHS.getValueType() == MVT::i64) {
527 if (CC == ISD::SETEQ || CC == ISD::SETNE) {
528 if (isInt64Immediate(RHS.getNode(), Imm)) {
529 // SETEQ/SETNE comparison with 16-bit immediate, fold it.
531 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
532 getI32Imm(Imm & 0xFFFF)), 0);
533 // If this is a 16-bit signed immediate, fold it.
535 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
536 getI32Imm(Imm & 0xFFFF)), 0);
538 // For non-equality comparisons, the default code would materialize the
539 // constant, then compare against it, like this:
543 // Since we are just comparing for equality, we can emit this instead:
544 // xoris r0,r3,0x1234
545 // cmpldi cr0,r0,0x5678
547 if (isUInt<32>(Imm)) {
548 SDValue Xor(CurDAG->getMachineNode(PPC::XORIS8, dl, MVT::i64, LHS,
549 getI64Imm(Imm >> 16)), 0);
550 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, Xor,
551 getI64Imm(Imm & 0xFFFF)), 0);
555 } else if (ISD::isUnsignedIntSetCC(CC)) {
556 if (isInt64Immediate(RHS.getNode(), Imm) && isUInt<16>(Imm))
557 return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
558 getI64Imm(Imm & 0xFFFF)), 0);
562 if (isIntS16Immediate(RHS, SImm))
563 return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
564 getI64Imm(SImm & 0xFFFF)),
568 } else if (LHS.getValueType() == MVT::f32) {
571 assert(LHS.getValueType() == MVT::f64 && "Unknown vt!");
574 return SDValue(CurDAG->getMachineNode(Opc, dl, MVT::i32, LHS, RHS), 0);
577 static PPC::Predicate getPredicateForSetCC(ISD::CondCode CC) {
583 llvm_unreachable("Should be lowered by legalize!");
584 default: llvm_unreachable("Unknown condition!");
586 case ISD::SETEQ: return PPC::PRED_EQ;
588 case ISD::SETNE: return PPC::PRED_NE;
590 case ISD::SETLT: return PPC::PRED_LT;
592 case ISD::SETLE: return PPC::PRED_LE;
594 case ISD::SETGT: return PPC::PRED_GT;
596 case ISD::SETGE: return PPC::PRED_GE;
597 case ISD::SETO: return PPC::PRED_NU;
598 case ISD::SETUO: return PPC::PRED_UN;
599 // These two are invalid for floating point. Assume we have int.
600 case ISD::SETULT: return PPC::PRED_LT;
601 case ISD::SETUGT: return PPC::PRED_GT;
605 /// getCRIdxForSetCC - Return the index of the condition register field
606 /// associated with the SetCC condition, and whether or not the field is
607 /// treated as inverted. That is, lt = 0; ge = 0 inverted.
608 static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool &Invert) {
611 default: llvm_unreachable("Unknown condition!");
613 case ISD::SETLT: return 0; // Bit #0 = SETOLT
615 case ISD::SETGT: return 1; // Bit #1 = SETOGT
617 case ISD::SETEQ: return 2; // Bit #2 = SETOEQ
618 case ISD::SETUO: return 3; // Bit #3 = SETUO
620 case ISD::SETGE: Invert = true; return 0; // !Bit #0 = SETUGE
622 case ISD::SETLE: Invert = true; return 1; // !Bit #1 = SETULE
624 case ISD::SETNE: Invert = true; return 2; // !Bit #2 = SETUNE
625 case ISD::SETO: Invert = true; return 3; // !Bit #3 = SETO
630 llvm_unreachable("Invalid branch code: should be expanded by legalize");
631 // These are invalid for floating point. Assume integer.
632 case ISD::SETULT: return 0;
633 case ISD::SETUGT: return 1;
637 // getVCmpInst: return the vector compare instruction for the specified
638 // vector type and condition code. Since this is for altivec specific code,
639 // only support the altivec types (v16i8, v8i16, v4i32, and v4f32).
640 static unsigned int getVCmpInst(MVT::SimpleValueType VecVT, ISD::CondCode CC) {
646 if (VecVT == MVT::v16i8)
647 return PPC::VCMPEQUB;
648 else if (VecVT == MVT::v8i16)
649 return PPC::VCMPEQUH;
650 else if (VecVT == MVT::v4i32)
651 return PPC::VCMPEQUW;
652 // v4f32 != v4f32 could be translate to unordered not equal
653 else if (VecVT == MVT::v4f32)
654 return PPC::VCMPEQFP;
660 if (VecVT == MVT::v16i8)
661 return PPC::VCMPGTSB;
662 else if (VecVT == MVT::v8i16)
663 return PPC::VCMPGTSH;
664 else if (VecVT == MVT::v4i32)
665 return PPC::VCMPGTSW;
666 else if (VecVT == MVT::v4f32)
667 return PPC::VCMPGTFP;
673 if (VecVT == MVT::v16i8)
674 return PPC::VCMPGTUB;
675 else if (VecVT == MVT::v8i16)
676 return PPC::VCMPGTUH;
677 else if (VecVT == MVT::v4i32)
678 return PPC::VCMPGTUW;
681 if (VecVT == MVT::v4f32)
682 return PPC::VCMPEQFP;
687 if (VecVT == MVT::v4f32)
688 return PPC::VCMPGTFP;
691 if (VecVT == MVT::v4f32)
692 return PPC::VCMPGEFP;
697 llvm_unreachable("Invalid integer vector compare condition");
700 // getVCmpEQInst: return the equal compare instruction for the specified vector
701 // type. Since this is for altivec specific code, only support the altivec
702 // types (v16i8, v8i16, v4i32, and v4f32).
703 static unsigned int getVCmpEQInst(MVT::SimpleValueType VecVT) {
706 return PPC::VCMPEQUB;
708 return PPC::VCMPEQUH;
710 return PPC::VCMPEQUW;
712 return PPC::VCMPEQFP;
714 llvm_unreachable("Invalid integer vector compare condition");
719 SDNode *PPCDAGToDAGISel::SelectSETCC(SDNode *N) {
722 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
723 EVT PtrVT = CurDAG->getTargetLoweringInfo().getPointerTy();
724 bool isPPC64 = (PtrVT == MVT::i64);
726 if (!PPCSubTarget.useCRBits() &&
727 isInt32Immediate(N->getOperand(1), Imm)) {
728 // We can codegen setcc op, imm very efficiently compared to a brcond.
729 // Check for those cases here.
732 SDValue Op = N->getOperand(0);
736 Op = SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, Op), 0);
737 SDValue Ops[] = { Op, getI32Imm(27), getI32Imm(5), getI32Imm(31) };
738 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
743 SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
744 Op, getI32Imm(~0U)), 0);
745 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op,
749 SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
750 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
754 SDValue(CurDAG->getMachineNode(PPC::NEG, dl, MVT::i32, Op), 0);
755 T = SDValue(CurDAG->getMachineNode(PPC::ANDC, dl, MVT::i32, T, Op), 0);
756 SDValue Ops[] = { T, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
757 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
760 } else if (Imm == ~0U) { // setcc op, -1
761 SDValue Op = N->getOperand(0);
766 Op = SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
767 Op, getI32Imm(1)), 0);
768 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
769 SDValue(CurDAG->getMachineNode(PPC::LI, dl,
775 Op = SDValue(CurDAG->getMachineNode(PPC::NOR, dl, MVT::i32, Op, Op), 0);
776 SDNode *AD = CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
778 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDValue(AD, 0),
782 SDValue AD = SDValue(CurDAG->getMachineNode(PPC::ADDI, dl, MVT::i32, Op,
784 SDValue AN = SDValue(CurDAG->getMachineNode(PPC::AND, dl, MVT::i32, AD,
786 SDValue Ops[] = { AN, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
787 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
790 SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
791 Op = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops),
793 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op,
800 SDValue LHS = N->getOperand(0);
801 SDValue RHS = N->getOperand(1);
803 // Altivec Vector compare instructions do not set any CR register by default and
804 // vector compare operations return the same type as the operands.
805 if (LHS.getValueType().isVector()) {
806 EVT VecVT = LHS.getValueType();
807 MVT::SimpleValueType VT = VecVT.getSimpleVT().SimpleTy;
808 unsigned int VCmpInst = getVCmpInst(VT, CC);
814 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, LHS, RHS);
818 SDValue VCmp(CurDAG->getMachineNode(VCmpInst, dl, VecVT, LHS, RHS), 0);
819 return CurDAG->SelectNodeTo(N, PPC::VNOR, VecVT, VCmp, VCmp);
824 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, RHS, LHS);
828 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, LHS, RHS);
832 // Small optimization: Altivec provides a 'Vector Compare Greater Than
833 // or Equal To' instruction (vcmpgefp), so in this case there is no
834 // need for extra logic for the equal compare.
835 if (VecVT.getSimpleVT().isFloatingPoint()) {
836 return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, LHS, RHS);
838 SDValue VCmpGT(CurDAG->getMachineNode(VCmpInst, dl, VecVT, LHS, RHS), 0);
839 unsigned int VCmpEQInst = getVCmpEQInst(VT);
840 SDValue VCmpEQ(CurDAG->getMachineNode(VCmpEQInst, dl, VecVT, LHS, RHS), 0);
841 return CurDAG->SelectNodeTo(N, PPC::VOR, VecVT, VCmpGT, VCmpEQ);
847 SDValue VCmpLE(CurDAG->getMachineNode(VCmpInst, dl, VecVT, RHS, LHS), 0);
848 unsigned int VCmpEQInst = getVCmpEQInst(VT);
849 SDValue VCmpEQ(CurDAG->getMachineNode(VCmpEQInst, dl, VecVT, LHS, RHS), 0);
850 return CurDAG->SelectNodeTo(N, PPC::VOR, VecVT, VCmpLE, VCmpEQ);
853 llvm_unreachable("Invalid vector compare type: should be expanded by legalize");
857 if (PPCSubTarget.useCRBits())
861 unsigned Idx = getCRIdxForSetCC(CC, Inv);
862 SDValue CCReg = SelectCC(LHS, RHS, CC, dl);
865 // Force the ccreg into CR7.
866 SDValue CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
868 SDValue InFlag(0, 0); // Null incoming flag value.
869 CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, CR7Reg, CCReg,
872 IntCR = SDValue(CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32, CR7Reg,
875 SDValue Ops[] = { IntCR, getI32Imm((32-(3-Idx)) & 31),
876 getI32Imm(31), getI32Imm(31) };
878 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
880 // Get the specified bit.
882 SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops), 0);
883 return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1));
887 // Select - Convert the specified operand from a target-independent to a
888 // target-specific node if it hasn't already been changed.
889 SDNode *PPCDAGToDAGISel::Select(SDNode *N) {
891 if (N->isMachineOpcode()) {
893 return NULL; // Already selected.
896 switch (N->getOpcode()) {
899 case ISD::Constant: {
900 if (N->getValueType(0) == MVT::i64) {
902 int64_t Imm = cast<ConstantSDNode>(N)->getZExtValue();
903 // Assume no remaining bits.
904 unsigned Remainder = 0;
905 // Assume no shift required.
908 // If it can't be represented as a 32 bit value.
909 if (!isInt<32>(Imm)) {
910 Shift = countTrailingZeros<uint64_t>(Imm);
911 int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
913 // If the shifted value fits 32 bits.
914 if (isInt<32>(ImmSh)) {
915 // Go with the shifted value.
918 // Still stuck with a 64 bit value.
925 // Intermediate operand.
928 // Handle first 32 bits.
929 unsigned Lo = Imm & 0xFFFF;
930 unsigned Hi = (Imm >> 16) & 0xFFFF;
933 if (isInt<16>(Imm)) {
935 Result = CurDAG->getMachineNode(PPC::LI8, dl, MVT::i64, getI32Imm(Lo));
937 // Handle the Hi bits.
938 unsigned OpC = Hi ? PPC::LIS8 : PPC::LI8;
939 Result = CurDAG->getMachineNode(OpC, dl, MVT::i64, getI32Imm(Hi));
941 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
942 SDValue(Result, 0), getI32Imm(Lo));
945 Result = CurDAG->getMachineNode(PPC::LIS8, dl, MVT::i64, getI32Imm(Hi));
948 // If no shift, we're done.
949 if (!Shift) return Result;
951 // Shift for next step if the upper 32-bits were not zero.
953 Result = CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64,
956 getI32Imm(63 - Shift));
959 // Add in the last bits as required.
960 if ((Hi = (Remainder >> 16) & 0xFFFF)) {
961 Result = CurDAG->getMachineNode(PPC::ORIS8, dl, MVT::i64,
962 SDValue(Result, 0), getI32Imm(Hi));
964 if ((Lo = Remainder & 0xFFFF)) {
965 Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
966 SDValue(Result, 0), getI32Imm(Lo));
975 SDNode *SN = SelectSETCC(N);
980 case PPCISD::GlobalBaseReg:
981 return getGlobalBaseReg();
983 case ISD::FrameIndex: {
984 int FI = cast<FrameIndexSDNode>(N)->getIndex();
985 SDValue TFI = CurDAG->getTargetFrameIndex(FI, N->getValueType(0));
986 unsigned Opc = N->getValueType(0) == MVT::i32 ? PPC::ADDI : PPC::ADDI8;
988 return CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), TFI,
990 return CurDAG->getMachineNode(Opc, dl, N->getValueType(0), TFI,
994 case PPCISD::MFOCRF: {
995 SDValue InFlag = N->getOperand(1);
996 return CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32,
997 N->getOperand(0), InFlag);
1001 // FIXME: since this depends on the setting of the carry flag from the srawi
1002 // we should really be making notes about that for the scheduler.
1003 // FIXME: It sure would be nice if we could cheaply recognize the
1004 // srl/add/sra pattern the dag combiner will generate for this as
1005 // sra/addze rather than having to handle sdiv ourselves. oh well.
1007 if (isInt32Immediate(N->getOperand(1), Imm)) {
1008 SDValue N0 = N->getOperand(0);
1009 if ((signed)Imm > 0 && isPowerOf2_32(Imm)) {
1011 CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue,
1012 N0, getI32Imm(Log2_32(Imm)));
1013 return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
1014 SDValue(Op, 0), SDValue(Op, 1));
1015 } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) {
1017 CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue,
1018 N0, getI32Imm(Log2_32(-Imm)));
1020 SDValue(CurDAG->getMachineNode(PPC::ADDZE, dl, MVT::i32,
1021 SDValue(Op, 0), SDValue(Op, 1)),
1023 return CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT);
1027 // Other cases are autogenerated.
1032 // Handle preincrement loads.
1033 LoadSDNode *LD = cast<LoadSDNode>(N);
1034 EVT LoadedVT = LD->getMemoryVT();
1036 // Normal loads are handled by code generated from the .td file.
1037 if (LD->getAddressingMode() != ISD::PRE_INC)
1040 SDValue Offset = LD->getOffset();
1041 if (Offset.getOpcode() == ISD::TargetConstant ||
1042 Offset.getOpcode() == ISD::TargetGlobalAddress) {
1045 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
1046 if (LD->getValueType(0) != MVT::i64) {
1047 // Handle PPC32 integer and normal FP loads.
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::f64: Opcode = PPC::LFDU; break;
1052 case MVT::f32: Opcode = PPC::LFSU; break;
1053 case MVT::i32: Opcode = PPC::LWZU; break;
1054 case MVT::i16: Opcode = isSExt ? PPC::LHAU : PPC::LHZU; break;
1056 case MVT::i8: Opcode = PPC::LBZU; break;
1059 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
1060 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
1061 switch (LoadedVT.getSimpleVT().SimpleTy) {
1062 default: llvm_unreachable("Invalid PPC load type!");
1063 case MVT::i64: Opcode = PPC::LDU; break;
1064 case MVT::i32: Opcode = PPC::LWZU8; break;
1065 case MVT::i16: Opcode = isSExt ? PPC::LHAU8 : PPC::LHZU8; break;
1067 case MVT::i8: Opcode = PPC::LBZU8; break;
1071 SDValue Chain = LD->getChain();
1072 SDValue Base = LD->getBasePtr();
1073 SDValue Ops[] = { Offset, Base, Chain };
1074 return CurDAG->getMachineNode(Opcode, dl, LD->getValueType(0),
1075 PPCLowering.getPointerTy(),
1079 bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
1080 if (LD->getValueType(0) != MVT::i64) {
1081 // Handle PPC32 integer and normal FP loads.
1082 assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
1083 switch (LoadedVT.getSimpleVT().SimpleTy) {
1084 default: llvm_unreachable("Invalid PPC load type!");
1085 case MVT::f64: Opcode = PPC::LFDUX; break;
1086 case MVT::f32: Opcode = PPC::LFSUX; break;
1087 case MVT::i32: Opcode = PPC::LWZUX; break;
1088 case MVT::i16: Opcode = isSExt ? PPC::LHAUX : PPC::LHZUX; break;
1090 case MVT::i8: Opcode = PPC::LBZUX; break;
1093 assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
1094 assert((!isSExt || LoadedVT == MVT::i16 || LoadedVT == MVT::i32) &&
1095 "Invalid sext update load");
1096 switch (LoadedVT.getSimpleVT().SimpleTy) {
1097 default: llvm_unreachable("Invalid PPC load type!");
1098 case MVT::i64: Opcode = PPC::LDUX; break;
1099 case MVT::i32: Opcode = isSExt ? PPC::LWAUX : PPC::LWZUX8; break;
1100 case MVT::i16: Opcode = isSExt ? PPC::LHAUX8 : PPC::LHZUX8; break;
1102 case MVT::i8: Opcode = PPC::LBZUX8; break;
1106 SDValue Chain = LD->getChain();
1107 SDValue Base = LD->getBasePtr();
1108 SDValue Ops[] = { Base, Offset, Chain };
1109 return CurDAG->getMachineNode(Opcode, dl, LD->getValueType(0),
1110 PPCLowering.getPointerTy(),
1116 unsigned Imm, Imm2, SH, MB, ME;
1119 // If this is an and of a value rotated between 0 and 31 bits and then and'd
1120 // with a mask, emit rlwinm
1121 if (isInt32Immediate(N->getOperand(1), Imm) &&
1122 isRotateAndMask(N->getOperand(0).getNode(), Imm, false, SH, MB, ME)) {
1123 SDValue Val = N->getOperand(0).getOperand(0);
1124 SDValue Ops[] = { Val, getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
1125 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
1127 // If this is just a masked value where the input is not handled above, and
1128 // is not a rotate-left (handled by a pattern in the .td file), emit rlwinm
1129 if (isInt32Immediate(N->getOperand(1), Imm) &&
1130 isRunOfOnes(Imm, MB, ME) &&
1131 N->getOperand(0).getOpcode() != ISD::ROTL) {
1132 SDValue Val = N->getOperand(0);
1133 SDValue Ops[] = { Val, getI32Imm(0), getI32Imm(MB), getI32Imm(ME) };
1134 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
1136 // If this is a 64-bit zero-extension mask, emit rldicl.
1137 if (isInt64Immediate(N->getOperand(1).getNode(), Imm64) &&
1139 SDValue Val = N->getOperand(0);
1140 MB = 64 - CountTrailingOnes_64(Imm64);
1143 // If the operand is a logical right shift, we can fold it into this
1144 // instruction: rldicl(rldicl(x, 64-n, n), 0, mb) -> rldicl(x, 64-n, mb)
1145 // for n <= mb. The right shift is really a left rotate followed by a
1146 // mask, and this mask is a more-restrictive sub-mask of the mask implied
1148 if (Val.getOpcode() == ISD::SRL &&
1149 isInt32Immediate(Val.getOperand(1).getNode(), Imm) && Imm <= MB) {
1150 assert(Imm < 64 && "Illegal shift amount");
1151 Val = Val.getOperand(0);
1155 SDValue Ops[] = { Val, getI32Imm(SH), getI32Imm(MB) };
1156 return CurDAG->SelectNodeTo(N, PPC::RLDICL, MVT::i64, Ops, 3);
1158 // AND X, 0 -> 0, not "rlwinm 32".
1159 if (isInt32Immediate(N->getOperand(1), Imm) && (Imm == 0)) {
1160 ReplaceUses(SDValue(N, 0), N->getOperand(1));
1163 // ISD::OR doesn't get all the bitfield insertion fun.
1164 // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) is a bitfield insert
1165 if (isInt32Immediate(N->getOperand(1), Imm) &&
1166 N->getOperand(0).getOpcode() == ISD::OR &&
1167 isInt32Immediate(N->getOperand(0).getOperand(1), Imm2)) {
1170 if (isRunOfOnes(Imm, MB, ME)) {
1171 SDValue Ops[] = { N->getOperand(0).getOperand(0),
1172 N->getOperand(0).getOperand(1),
1173 getI32Imm(0), getI32Imm(MB),getI32Imm(ME) };
1174 return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops);
1178 // Other cases are autogenerated.
1182 if (N->getValueType(0) == MVT::i32)
1183 if (SDNode *I = SelectBitfieldInsert(N))
1186 // Other cases are autogenerated.
1189 unsigned Imm, SH, MB, ME;
1190 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
1191 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
1192 SDValue Ops[] = { N->getOperand(0).getOperand(0),
1193 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
1194 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
1197 // Other cases are autogenerated.
1201 unsigned Imm, SH, MB, ME;
1202 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
1203 isRotateAndMask(N, Imm, true, SH, MB, ME)) {
1204 SDValue Ops[] = { N->getOperand(0).getOperand(0),
1205 getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
1206 return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
1209 // Other cases are autogenerated.
1212 // FIXME: Remove this once the ANDI glue bug is fixed:
1213 case PPCISD::ANDIo_1_EQ_BIT:
1214 case PPCISD::ANDIo_1_GT_BIT: {
1218 EVT InVT = N->getOperand(0).getValueType();
1219 assert((InVT == MVT::i64 || InVT == MVT::i32) &&
1220 "Invalid input type for ANDIo_1_EQ_BIT");
1222 unsigned Opcode = (InVT == MVT::i64) ? PPC::ANDIo8 : PPC::ANDIo;
1223 SDValue AndI(CurDAG->getMachineNode(Opcode, dl, InVT, MVT::Glue,
1225 CurDAG->getTargetConstant(1, InVT)), 0);
1226 SDValue CR0Reg = CurDAG->getRegister(PPC::CR0, MVT::i32);
1228 CurDAG->getTargetConstant(N->getOpcode() == PPCISD::ANDIo_1_EQ_BIT ?
1229 PPC::sub_eq : PPC::sub_gt, MVT::i32);
1231 return CurDAG->SelectNodeTo(N, TargetOpcode::EXTRACT_SUBREG, MVT::i1,
1233 SDValue(AndI.getNode(), 1) /* glue */);
1235 case ISD::SELECT_CC: {
1236 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
1237 EVT PtrVT = CurDAG->getTargetLoweringInfo().getPointerTy();
1238 bool isPPC64 = (PtrVT == MVT::i64);
1240 // If this is a select of i1 operands, we'll pattern match it.
1241 if (PPCSubTarget.useCRBits() &&
1242 N->getOperand(0).getValueType() == MVT::i1)
1245 // Handle the setcc cases here. select_cc lhs, 0, 1, 0, cc
1247 if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1248 if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1249 if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1250 if (N1C->isNullValue() && N3C->isNullValue() &&
1251 N2C->getZExtValue() == 1ULL && CC == ISD::SETNE &&
1252 // FIXME: Implement this optzn for PPC64.
1253 N->getValueType(0) == MVT::i32) {
1255 CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
1256 N->getOperand(0), getI32Imm(~0U));
1257 return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32,
1258 SDValue(Tmp, 0), N->getOperand(0),
1262 SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC, dl);
1264 if (N->getValueType(0) == MVT::i1) {
1265 // An i1 select is: (c & t) | (!c & f).
1267 unsigned Idx = getCRIdxForSetCC(CC, Inv);
1271 default: llvm_unreachable("Invalid CC index");
1272 case 0: SRI = PPC::sub_lt; break;
1273 case 1: SRI = PPC::sub_gt; break;
1274 case 2: SRI = PPC::sub_eq; break;
1275 case 3: SRI = PPC::sub_un; break;
1278 SDValue CCBit = CurDAG->getTargetExtractSubreg(SRI, dl, MVT::i1, CCReg);
1280 SDValue NotCCBit(CurDAG->getMachineNode(PPC::CRNOR, dl, MVT::i1,
1282 SDValue C = Inv ? NotCCBit : CCBit,
1283 NotC = Inv ? CCBit : NotCCBit;
1285 SDValue CAndT(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1,
1286 C, N->getOperand(2)), 0);
1287 SDValue NotCAndF(CurDAG->getMachineNode(PPC::CRAND, dl, MVT::i1,
1288 NotC, N->getOperand(3)), 0);
1290 return CurDAG->SelectNodeTo(N, PPC::CROR, MVT::i1, CAndT, NotCAndF);
1293 unsigned BROpc = getPredicateForSetCC(CC);
1295 unsigned SelectCCOp;
1296 if (N->getValueType(0) == MVT::i32)
1297 SelectCCOp = PPC::SELECT_CC_I4;
1298 else if (N->getValueType(0) == MVT::i64)
1299 SelectCCOp = PPC::SELECT_CC_I8;
1300 else if (N->getValueType(0) == MVT::f32)
1301 SelectCCOp = PPC::SELECT_CC_F4;
1302 else if (N->getValueType(0) == MVT::f64)
1303 SelectCCOp = PPC::SELECT_CC_F8;
1305 SelectCCOp = PPC::SELECT_CC_VRRC;
1307 SDValue Ops[] = { CCReg, N->getOperand(2), N->getOperand(3),
1309 return CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), Ops, 4);
1313 bool IsPPC64 = PPCSubTarget.isPPC64();
1314 SDValue Ops[] = { N->getOperand(1), N->getOperand(0) };
1315 return CurDAG->SelectNodeTo(N, N->getOpcode() == PPCISD::BDNZ ?
1316 (IsPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
1317 (IsPPC64 ? PPC::BDZ8 : PPC::BDZ),
1318 MVT::Other, Ops, 2);
1320 case PPCISD::COND_BRANCH: {
1321 // Op #0 is the Chain.
1322 // Op #1 is the PPC::PRED_* number.
1324 // Op #3 is the Dest MBB
1325 // Op #4 is the Flag.
1326 // Prevent PPC::PRED_* from being selected into LI.
1328 getI32Imm(cast<ConstantSDNode>(N->getOperand(1))->getZExtValue());
1329 SDValue Ops[] = { Pred, N->getOperand(2), N->getOperand(3),
1330 N->getOperand(0), N->getOperand(4) };
1331 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops, 5);
1334 ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
1335 unsigned PCC = getPredicateForSetCC(CC);
1337 if (N->getOperand(2).getValueType() == MVT::i1) {
1341 default: llvm_unreachable("Unexpected Boolean-operand predicate");
1342 case PPC::PRED_LT: Opc = PPC::CRANDC; Swap = true; break;
1343 case PPC::PRED_LE: Opc = PPC::CRORC; Swap = true; break;
1344 case PPC::PRED_EQ: Opc = PPC::CREQV; Swap = false; break;
1345 case PPC::PRED_GE: Opc = PPC::CRORC; Swap = false; break;
1346 case PPC::PRED_GT: Opc = PPC::CRANDC; Swap = false; break;
1347 case PPC::PRED_NE: Opc = PPC::CRXOR; Swap = false; break;
1350 SDValue BitComp(CurDAG->getMachineNode(Opc, dl, MVT::i1,
1351 N->getOperand(Swap ? 3 : 2),
1352 N->getOperand(Swap ? 2 : 3)), 0);
1353 return CurDAG->SelectNodeTo(N, PPC::BC, MVT::Other,
1354 BitComp, N->getOperand(4), N->getOperand(0));
1357 SDValue CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC, dl);
1358 SDValue Ops[] = { getI32Imm(PCC), CondCode,
1359 N->getOperand(4), N->getOperand(0) };
1360 return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops, 4);
1363 // FIXME: Should custom lower this.
1364 SDValue Chain = N->getOperand(0);
1365 SDValue Target = N->getOperand(1);
1366 unsigned Opc = Target.getValueType() == MVT::i32 ? PPC::MTCTR : PPC::MTCTR8;
1367 unsigned Reg = Target.getValueType() == MVT::i32 ? PPC::BCTR : PPC::BCTR8;
1368 Chain = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Glue, Target,
1370 return CurDAG->SelectNodeTo(N, Reg, MVT::Other, Chain);
1372 case PPCISD::TOC_ENTRY: {
1373 assert (PPCSubTarget.isPPC64() && "Only supported for 64-bit ABI");
1375 // For medium and large code model, we generate two instructions as
1376 // described below. Otherwise we allow SelectCodeCommon to handle this,
1377 // selecting one of LDtoc, LDtocJTI, and LDtocCPT.
1378 CodeModel::Model CModel = TM.getCodeModel();
1379 if (CModel != CodeModel::Medium && CModel != CodeModel::Large)
1382 // The first source operand is a TargetGlobalAddress or a
1383 // TargetJumpTable. If it is an externally defined symbol, a symbol
1384 // with common linkage, a function address, or a jump table address,
1385 // or if we are generating code for large code model, we generate:
1386 // LDtocL(<ga:@sym>, ADDIStocHA(%X2, <ga:@sym>))
1387 // Otherwise we generate:
1388 // ADDItocL(ADDIStocHA(%X2, <ga:@sym>), <ga:@sym>)
1389 SDValue GA = N->getOperand(0);
1390 SDValue TOCbase = N->getOperand(1);
1391 SDNode *Tmp = CurDAG->getMachineNode(PPC::ADDIStocHA, dl, MVT::i64,
1394 if (isa<JumpTableSDNode>(GA) || CModel == CodeModel::Large)
1395 return CurDAG->getMachineNode(PPC::LDtocL, dl, MVT::i64, GA,
1398 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(GA)) {
1399 const GlobalValue *GValue = G->getGlobal();
1400 const GlobalAlias *GAlias = dyn_cast<GlobalAlias>(GValue);
1401 const GlobalValue *RealGValue = GAlias ?
1402 GAlias->resolveAliasedGlobal(false) : GValue;
1403 const GlobalVariable *GVar = dyn_cast<GlobalVariable>(RealGValue);
1404 assert((GVar || isa<Function>(RealGValue)) &&
1405 "Unexpected global value subclass!");
1407 // An external variable is one without an initializer. For these,
1408 // for variables with common linkage, and for Functions, generate
1410 if (!GVar || !GVar->hasInitializer() || RealGValue->hasCommonLinkage() ||
1411 RealGValue->hasAvailableExternallyLinkage())
1412 return CurDAG->getMachineNode(PPC::LDtocL, dl, MVT::i64, GA,
1416 return CurDAG->getMachineNode(PPC::ADDItocL, dl, MVT::i64,
1417 SDValue(Tmp, 0), GA);
1419 case PPCISD::VADD_SPLAT: {
1420 // This expands into one of three sequences, depending on whether
1421 // the first operand is odd or even, positive or negative.
1422 assert(isa<ConstantSDNode>(N->getOperand(0)) &&
1423 isa<ConstantSDNode>(N->getOperand(1)) &&
1424 "Invalid operand on VADD_SPLAT!");
1426 int Elt = N->getConstantOperandVal(0);
1427 int EltSize = N->getConstantOperandVal(1);
1428 unsigned Opc1, Opc2, Opc3;
1432 Opc1 = PPC::VSPLTISB;
1433 Opc2 = PPC::VADDUBM;
1434 Opc3 = PPC::VSUBUBM;
1436 } else if (EltSize == 2) {
1437 Opc1 = PPC::VSPLTISH;
1438 Opc2 = PPC::VADDUHM;
1439 Opc3 = PPC::VSUBUHM;
1442 assert(EltSize == 4 && "Invalid element size on VADD_SPLAT!");
1443 Opc1 = PPC::VSPLTISW;
1444 Opc2 = PPC::VADDUWM;
1445 Opc3 = PPC::VSUBUWM;
1449 if ((Elt & 1) == 0) {
1450 // Elt is even, in the range [-32,-18] + [16,30].
1452 // Convert: VADD_SPLAT elt, size
1453 // Into: tmp = VSPLTIS[BHW] elt
1454 // VADDU[BHW]M tmp, tmp
1455 // Where: [BHW] = B for size = 1, H for size = 2, W for size = 4
1456 SDValue EltVal = getI32Imm(Elt >> 1);
1457 SDNode *Tmp = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1458 SDValue TmpVal = SDValue(Tmp, 0);
1459 return CurDAG->getMachineNode(Opc2, dl, VT, TmpVal, TmpVal);
1461 } else if (Elt > 0) {
1462 // Elt is odd and positive, in the range [17,31].
1464 // Convert: VADD_SPLAT elt, size
1465 // Into: tmp1 = VSPLTIS[BHW] elt-16
1466 // tmp2 = VSPLTIS[BHW] -16
1467 // VSUBU[BHW]M tmp1, tmp2
1468 SDValue EltVal = getI32Imm(Elt - 16);
1469 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1470 EltVal = getI32Imm(-16);
1471 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1472 return CurDAG->getMachineNode(Opc3, dl, VT, SDValue(Tmp1, 0),
1476 // Elt is odd and negative, in the range [-31,-17].
1478 // Convert: VADD_SPLAT elt, size
1479 // Into: tmp1 = VSPLTIS[BHW] elt+16
1480 // tmp2 = VSPLTIS[BHW] -16
1481 // VADDU[BHW]M tmp1, tmp2
1482 SDValue EltVal = getI32Imm(Elt + 16);
1483 SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1484 EltVal = getI32Imm(-16);
1485 SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
1486 return CurDAG->getMachineNode(Opc2, dl, VT, SDValue(Tmp1, 0),
1492 return SelectCode(N);
1495 /// PostprocessISelDAG - Perform some late peephole optimizations
1496 /// on the DAG representation.
1497 void PPCDAGToDAGISel::PostprocessISelDAG() {
1499 // Skip peepholes at -O0.
1500 if (TM.getOptLevel() == CodeGenOpt::None)
1507 void PPCDAGToDAGISel::PeepholdCROps() {
1511 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
1512 E = CurDAG->allnodes_end(); I != E; ++I) {
1513 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(I);
1514 if (!MachineNode || MachineNode->use_empty())
1516 SDNode *ResNode = MachineNode;
1518 bool Op1Set = false, Op1Unset = false,
1520 Op2Set = false, Op2Unset = false,
1523 unsigned Opcode = MachineNode->getMachineOpcode();
1534 SDValue Op = MachineNode->getOperand(1);
1535 if (Op.isMachineOpcode()) {
1536 if (Op.getMachineOpcode() == PPC::CRSET)
1538 else if (Op.getMachineOpcode() == PPC::CRUNSET)
1540 else if (Op.getMachineOpcode() == PPC::CRNOR &&
1541 Op.getOperand(0) == Op.getOperand(1))
1547 case PPC::SELECT_I4:
1548 case PPC::SELECT_I8:
1549 case PPC::SELECT_F4:
1550 case PPC::SELECT_F8:
1551 case PPC::SELECT_VRRC: {
1552 SDValue Op = MachineNode->getOperand(0);
1553 if (Op.isMachineOpcode()) {
1554 if (Op.getMachineOpcode() == PPC::CRSET)
1556 else if (Op.getMachineOpcode() == PPC::CRUNSET)
1558 else if (Op.getMachineOpcode() == PPC::CRNOR &&
1559 Op.getOperand(0) == Op.getOperand(1))
1569 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1571 ResNode = MachineNode->getOperand(0).getNode();
1574 ResNode = MachineNode->getOperand(1).getNode();
1577 ResNode = MachineNode->getOperand(0).getNode();
1578 else if (Op1Unset || Op2Unset)
1579 // x & 0 = 0 & y = 0
1580 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1583 // ~x & y = andc(y, x)
1584 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1585 MVT::i1, MachineNode->getOperand(1),
1586 MachineNode->getOperand(0).
1589 // x & ~y = andc(x, y)
1590 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1591 MVT::i1, MachineNode->getOperand(0),
1592 MachineNode->getOperand(1).
1596 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1597 // nand(x, x) -> nor(x, x)
1598 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1599 MVT::i1, MachineNode->getOperand(0),
1600 MachineNode->getOperand(0));
1602 // nand(1, y) -> nor(y, y)
1603 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1604 MVT::i1, MachineNode->getOperand(1),
1605 MachineNode->getOperand(1));
1607 // nand(x, 1) -> nor(x, x)
1608 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1609 MVT::i1, MachineNode->getOperand(0),
1610 MachineNode->getOperand(0));
1611 else if (Op1Unset || Op2Unset)
1612 // nand(x, 0) = nand(0, y) = 1
1613 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1616 // nand(~x, y) = ~(~x & y) = x | ~y = orc(x, y)
1617 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1618 MVT::i1, MachineNode->getOperand(0).
1620 MachineNode->getOperand(1));
1622 // nand(x, ~y) = ~x | y = orc(y, x)
1623 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1624 MVT::i1, MachineNode->getOperand(1).
1626 MachineNode->getOperand(0));
1629 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1631 ResNode = MachineNode->getOperand(0).getNode();
1632 else if (Op1Set || Op2Set)
1633 // x | 1 = 1 | y = 1
1634 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1638 ResNode = MachineNode->getOperand(1).getNode();
1641 ResNode = MachineNode->getOperand(0).getNode();
1643 // ~x | y = orc(y, x)
1644 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1645 MVT::i1, MachineNode->getOperand(1),
1646 MachineNode->getOperand(0).
1649 // x | ~y = orc(x, y)
1650 ResNode = CurDAG->getMachineNode(PPC::CRORC, SDLoc(MachineNode),
1651 MVT::i1, MachineNode->getOperand(0),
1652 MachineNode->getOperand(1).
1656 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1658 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1661 // xor(1, y) -> nor(y, y)
1662 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1663 MVT::i1, MachineNode->getOperand(1),
1664 MachineNode->getOperand(1));
1666 // xor(x, 1) -> nor(x, x)
1667 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1668 MVT::i1, MachineNode->getOperand(0),
1669 MachineNode->getOperand(0));
1672 ResNode = MachineNode->getOperand(1).getNode();
1675 ResNode = MachineNode->getOperand(0).getNode();
1677 // xor(~x, y) = eqv(x, y)
1678 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
1679 MVT::i1, MachineNode->getOperand(0).
1681 MachineNode->getOperand(1));
1683 // xor(x, ~y) = eqv(x, y)
1684 ResNode = CurDAG->getMachineNode(PPC::CREQV, SDLoc(MachineNode),
1685 MVT::i1, MachineNode->getOperand(0),
1686 MachineNode->getOperand(1).
1690 if (Op1Set || Op2Set)
1692 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1695 // nor(0, y) = ~y -> nor(y, y)
1696 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1697 MVT::i1, MachineNode->getOperand(1),
1698 MachineNode->getOperand(1));
1701 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1702 MVT::i1, MachineNode->getOperand(0),
1703 MachineNode->getOperand(0));
1705 // nor(~x, y) = andc(x, y)
1706 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1707 MVT::i1, MachineNode->getOperand(0).
1709 MachineNode->getOperand(1));
1711 // nor(x, ~y) = andc(y, x)
1712 ResNode = CurDAG->getMachineNode(PPC::CRANDC, SDLoc(MachineNode),
1713 MVT::i1, MachineNode->getOperand(1).
1715 MachineNode->getOperand(0));
1718 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1720 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1724 ResNode = MachineNode->getOperand(1).getNode();
1727 ResNode = MachineNode->getOperand(0).getNode();
1729 // eqv(0, y) = ~y -> nor(y, y)
1730 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1731 MVT::i1, MachineNode->getOperand(1),
1732 MachineNode->getOperand(1));
1735 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1736 MVT::i1, MachineNode->getOperand(0),
1737 MachineNode->getOperand(0));
1739 // eqv(~x, y) = xor(x, y)
1740 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
1741 MVT::i1, MachineNode->getOperand(0).
1743 MachineNode->getOperand(1));
1745 // eqv(x, ~y) = xor(x, y)
1746 ResNode = CurDAG->getMachineNode(PPC::CRXOR, SDLoc(MachineNode),
1747 MVT::i1, MachineNode->getOperand(0),
1748 MachineNode->getOperand(1).
1752 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1754 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1758 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1759 MVT::i1, MachineNode->getOperand(1),
1760 MachineNode->getOperand(1));
1761 else if (Op1Unset || Op2Set)
1762 // andc(0, y) = andc(x, 1) = 0
1763 ResNode = CurDAG->getMachineNode(PPC::CRUNSET, SDLoc(MachineNode),
1767 ResNode = MachineNode->getOperand(0).getNode();
1769 // andc(~x, y) = ~(x | y) = nor(x, y)
1770 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1771 MVT::i1, MachineNode->getOperand(0).
1773 MachineNode->getOperand(1));
1775 // andc(x, ~y) = x & y
1776 ResNode = CurDAG->getMachineNode(PPC::CRAND, SDLoc(MachineNode),
1777 MVT::i1, MachineNode->getOperand(0),
1778 MachineNode->getOperand(1).
1782 if (MachineNode->getOperand(0) == MachineNode->getOperand(1))
1784 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1786 else if (Op1Set || Op2Unset)
1787 // orc(1, y) = orc(x, 0) = 1
1788 ResNode = CurDAG->getMachineNode(PPC::CRSET, SDLoc(MachineNode),
1792 ResNode = MachineNode->getOperand(0).getNode();
1795 ResNode = CurDAG->getMachineNode(PPC::CRNOR, SDLoc(MachineNode),
1796 MVT::i1, MachineNode->getOperand(1),
1797 MachineNode->getOperand(1));
1799 // orc(~x, y) = ~(x & y) = nand(x, y)
1800 ResNode = CurDAG->getMachineNode(PPC::CRNAND, SDLoc(MachineNode),
1801 MVT::i1, MachineNode->getOperand(0).
1803 MachineNode->getOperand(1));
1805 // orc(x, ~y) = x | y
1806 ResNode = CurDAG->getMachineNode(PPC::CROR, SDLoc(MachineNode),
1807 MVT::i1, MachineNode->getOperand(0),
1808 MachineNode->getOperand(1).
1811 case PPC::SELECT_I4:
1812 case PPC::SELECT_I8:
1813 case PPC::SELECT_F4:
1814 case PPC::SELECT_F8:
1815 case PPC::SELECT_VRRC:
1817 ResNode = MachineNode->getOperand(1).getNode();
1819 ResNode = MachineNode->getOperand(2).getNode();
1821 ResNode = CurDAG->getMachineNode(MachineNode->getMachineOpcode(),
1823 MachineNode->getValueType(0),
1824 MachineNode->getOperand(0).
1826 MachineNode->getOperand(2),
1827 MachineNode->getOperand(1));
1832 ResNode = CurDAG->getMachineNode(Opcode == PPC::BC ? PPC::BCn :
1836 MachineNode->getOperand(0).
1838 MachineNode->getOperand(1),
1839 MachineNode->getOperand(2));
1840 // FIXME: Handle Op1Set, Op1Unset here too.
1844 if (ResNode != MachineNode) {
1845 DEBUG(dbgs() << "CR Peephole replacing:\nOld: ");
1846 DEBUG(MachineNode->dump(CurDAG));
1847 DEBUG(dbgs() << "\nNew: ");
1848 DEBUG(ResNode->dump(CurDAG));
1849 DEBUG(dbgs() << "\n");
1851 ReplaceUses(MachineNode, ResNode);
1856 CurDAG->RemoveDeadNodes();
1857 } while (IsModified);
1860 void PPCDAGToDAGISel::PeepholePPC64() {
1861 // These optimizations are currently supported only for 64-bit SVR4.
1862 if (PPCSubTarget.isDarwin() || !PPCSubTarget.isPPC64())
1865 SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode());
1868 while (Position != CurDAG->allnodes_begin()) {
1869 SDNode *N = --Position;
1870 // Skip dead nodes and any non-machine opcodes.
1871 if (N->use_empty() || !N->isMachineOpcode())
1875 unsigned StorageOpcode = N->getMachineOpcode();
1877 switch (StorageOpcode) {
1908 // If this is a load or store with a zero offset, we may be able to
1909 // fold an add-immediate into the memory operation.
1910 if (!isa<ConstantSDNode>(N->getOperand(FirstOp)) ||
1911 N->getConstantOperandVal(FirstOp) != 0)
1914 SDValue Base = N->getOperand(FirstOp + 1);
1915 if (!Base.isMachineOpcode())
1919 bool ReplaceFlags = true;
1921 // When the feeding operation is an add-immediate of some sort,
1922 // determine whether we need to add relocation information to the
1923 // target flags on the immediate operand when we fold it into the
1924 // load instruction.
1926 // For something like ADDItocL, the relocation information is
1927 // inferred from the opcode; when we process it in the AsmPrinter,
1928 // we add the necessary relocation there. A load, though, can receive
1929 // relocation from various flavors of ADDIxxx, so we need to carry
1930 // the relocation information in the target flags.
1931 switch (Base.getMachineOpcode()) {
1936 // In some cases (such as TLS) the relocation information
1937 // is already in place on the operand, so copying the operand
1939 ReplaceFlags = false;
1940 // For these cases, the immediate may not be divisible by 4, in
1941 // which case the fold is illegal for DS-form instructions. (The
1942 // other cases provide aligned addresses and are always safe.)
1943 if ((StorageOpcode == PPC::LWA ||
1944 StorageOpcode == PPC::LD ||
1945 StorageOpcode == PPC::STD) &&
1946 (!isa<ConstantSDNode>(Base.getOperand(1)) ||
1947 Base.getConstantOperandVal(1) % 4 != 0))
1950 case PPC::ADDIdtprelL:
1951 Flags = PPCII::MO_DTPREL_LO;
1953 case PPC::ADDItlsldL:
1954 Flags = PPCII::MO_TLSLD_LO;
1957 Flags = PPCII::MO_TOC_LO;
1961 // We found an opportunity. Reverse the operands from the add
1962 // immediate and substitute them into the load or store. If
1963 // needed, update the target flags for the immediate operand to
1964 // reflect the necessary relocation information.
1965 DEBUG(dbgs() << "Folding add-immediate into mem-op:\nBase: ");
1966 DEBUG(Base->dump(CurDAG));
1967 DEBUG(dbgs() << "\nN: ");
1968 DEBUG(N->dump(CurDAG));
1969 DEBUG(dbgs() << "\n");
1971 SDValue ImmOpnd = Base.getOperand(1);
1973 // If the relocation information isn't already present on the
1974 // immediate operand, add it now.
1976 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(ImmOpnd)) {
1978 const GlobalValue *GV = GA->getGlobal();
1979 // We can't perform this optimization for data whose alignment
1980 // is insufficient for the instruction encoding.
1981 if (GV->getAlignment() < 4 &&
1982 (StorageOpcode == PPC::LD || StorageOpcode == PPC::STD ||
1983 StorageOpcode == PPC::LWA)) {
1984 DEBUG(dbgs() << "Rejected this candidate for alignment.\n\n");
1987 ImmOpnd = CurDAG->getTargetGlobalAddress(GV, dl, MVT::i64, 0, Flags);
1988 } else if (ConstantPoolSDNode *CP =
1989 dyn_cast<ConstantPoolSDNode>(ImmOpnd)) {
1990 const Constant *C = CP->getConstVal();
1991 ImmOpnd = CurDAG->getTargetConstantPool(C, MVT::i64,
1997 if (FirstOp == 1) // Store
1998 (void)CurDAG->UpdateNodeOperands(N, N->getOperand(0), ImmOpnd,
1999 Base.getOperand(0), N->getOperand(3));
2001 (void)CurDAG->UpdateNodeOperands(N, ImmOpnd, Base.getOperand(0),
2004 // The add-immediate may now be dead, in which case remove it.
2005 if (Base.getNode()->use_empty())
2006 CurDAG->RemoveDeadNode(Base.getNode());
2011 /// createPPCISelDag - This pass converts a legalized DAG into a
2012 /// PowerPC-specific DAG, ready for instruction scheduling.
2014 FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) {
2015 return new PPCDAGToDAGISel(TM);
2018 static void initializePassOnce(PassRegistry &Registry) {
2019 const char *Name = "PowerPC DAG->DAG Pattern Instruction Selection";
2020 PassInfo *PI = new PassInfo(Name, "ppc-codegen", &SelectionDAGISel::ID, 0,
2022 Registry.registerPass(*PI, true);
2025 void llvm::initializePPCDAGToDAGISelPass(PassRegistry &Registry) {
2026 CALL_ONCE_INITIALIZATION(initializePassOnce);