Home | History | Annotate | Download | only in PowerPC
      1 //===-- PPCISelDAGToDAG.cpp - PPC --pattern matching inst selector --------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file defines a pattern matching instruction selector for PowerPC,
     11 // converting from a legalized dag to a PPC dag.
     12 //
     13 //===----------------------------------------------------------------------===//
     14 
     15 #define DEBUG_TYPE "ppc-codegen"
     16 #include "PPC.h"
     17 #include "MCTargetDesc/PPCPredicates.h"
     18 #include "PPCTargetMachine.h"
     19 #include "llvm/CodeGen/MachineFunction.h"
     20 #include "llvm/CodeGen/MachineInstrBuilder.h"
     21 #include "llvm/CodeGen/MachineRegisterInfo.h"
     22 #include "llvm/CodeGen/SelectionDAG.h"
     23 #include "llvm/CodeGen/SelectionDAGISel.h"
     24 #include "llvm/IR/Constants.h"
     25 #include "llvm/IR/Function.h"
     26 #include "llvm/IR/GlobalAlias.h"
     27 #include "llvm/IR/GlobalValue.h"
     28 #include "llvm/IR/GlobalVariable.h"
     29 #include "llvm/IR/Intrinsics.h"
     30 #include "llvm/Support/Debug.h"
     31 #include "llvm/Support/ErrorHandling.h"
     32 #include "llvm/Support/MathExtras.h"
     33 #include "llvm/Support/raw_ostream.h"
     34 #include "llvm/Target/TargetOptions.h"
     35 using namespace llvm;
     36 
     37 namespace llvm {
     38   void initializePPCDAGToDAGISelPass(PassRegistry&);
     39 }
     40 
     41 namespace {
     42   //===--------------------------------------------------------------------===//
     43   /// PPCDAGToDAGISel - PPC specific code to select PPC machine
     44   /// instructions for SelectionDAG operations.
     45   ///
     46   class PPCDAGToDAGISel : public SelectionDAGISel {
     47     const PPCTargetMachine &TM;
     48     const PPCTargetLowering &PPCLowering;
     49     const PPCSubtarget &PPCSubTarget;
     50     unsigned GlobalBaseReg;
     51   public:
     52     explicit PPCDAGToDAGISel(PPCTargetMachine &tm)
     53       : SelectionDAGISel(tm), TM(tm),
     54         PPCLowering(*TM.getTargetLowering()),
     55         PPCSubTarget(*TM.getSubtargetImpl()) {
     56       initializePPCDAGToDAGISelPass(*PassRegistry::getPassRegistry());
     57     }
     58 
     59     virtual bool runOnMachineFunction(MachineFunction &MF) {
     60       // Make sure we re-emit a set of the global base reg if necessary
     61       GlobalBaseReg = 0;
     62       SelectionDAGISel::runOnMachineFunction(MF);
     63 
     64       if (!PPCSubTarget.isSVR4ABI())
     65         InsertVRSaveCode(MF);
     66 
     67       return true;
     68     }
     69 
     70     virtual void PostprocessISelDAG();
     71 
     72     /// getI32Imm - Return a target constant with the specified value, of type
     73     /// i32.
     74     inline SDValue getI32Imm(unsigned Imm) {
     75       return CurDAG->getTargetConstant(Imm, MVT::i32);
     76     }
     77 
     78     /// getI64Imm - Return a target constant with the specified value, of type
     79     /// i64.
     80     inline SDValue getI64Imm(uint64_t Imm) {
     81       return CurDAG->getTargetConstant(Imm, MVT::i64);
     82     }
     83 
     84     /// getSmallIPtrImm - Return a target constant of pointer type.
     85     inline SDValue getSmallIPtrImm(unsigned Imm) {
     86       return CurDAG->getTargetConstant(Imm, PPCLowering.getPointerTy());
     87     }
     88 
     89     /// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s
     90     /// with any number of 0s on either side.  The 1s are allowed to wrap from
     91     /// LSB to MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs.
     92     /// 0x0F0F0000 is not, since all 1s are not contiguous.
     93     static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME);
     94 
     95 
     96     /// isRotateAndMask - Returns true if Mask and Shift can be folded into a
     97     /// rotate and mask opcode and mask operation.
     98     static bool isRotateAndMask(SDNode *N, unsigned Mask, bool isShiftMask,
     99                                 unsigned &SH, unsigned &MB, unsigned &ME);
    100 
    101     /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
    102     /// base register.  Return the virtual register that holds this value.
    103     SDNode *getGlobalBaseReg();
    104 
    105     // Select - Convert the specified operand from a target-independent to a
    106     // target-specific node if it hasn't already been changed.
    107     SDNode *Select(SDNode *N);
    108 
    109     SDNode *SelectBitfieldInsert(SDNode *N);
    110 
    111     /// SelectCC - Select a comparison of the specified values with the
    112     /// specified condition code, returning the CR# of the expression.
    113     SDValue SelectCC(SDValue LHS, SDValue RHS, ISD::CondCode CC, DebugLoc dl);
    114 
    115     /// SelectAddrImm - Returns true if the address N can be represented by
    116     /// a base register plus a signed 16-bit displacement [r+imm].
    117     bool SelectAddrImm(SDValue N, SDValue &Disp,
    118                        SDValue &Base) {
    119       return PPCLowering.SelectAddressRegImm(N, Disp, Base, *CurDAG);
    120     }
    121 
    122     /// SelectAddrImmOffs - Return true if the operand is valid for a preinc
    123     /// immediate field.  Because preinc imms have already been validated, just
    124     /// accept it.
    125     bool SelectAddrImmOffs(SDValue N, SDValue &Out) const {
    126       if (isa<ConstantSDNode>(N) || N.getOpcode() == PPCISD::Lo ||
    127           N.getOpcode() == ISD::TargetGlobalAddress) {
    128         Out = N;
    129         return true;
    130       }
    131 
    132       return false;
    133     }
    134 
    135     /// SelectAddrIdxOffs - Return true if the operand is valid for a preinc
    136     /// index field.  Because preinc imms have already been validated, just
    137     /// accept it.
    138     bool SelectAddrIdxOffs(SDValue N, SDValue &Out) const {
    139       if (isa<ConstantSDNode>(N) || N.getOpcode() == PPCISD::Lo ||
    140           N.getOpcode() == ISD::TargetGlobalAddress)
    141         return false;
    142 
    143       Out = N;
    144       return true;
    145     }
    146 
    147     /// SelectAddrIdx - Given the specified addressed, check to see if it can be
    148     /// represented as an indexed [r+r] operation.  Returns false if it can
    149     /// be represented by [r+imm], which are preferred.
    150     bool SelectAddrIdx(SDValue N, SDValue &Base, SDValue &Index) {
    151       return PPCLowering.SelectAddressRegReg(N, Base, Index, *CurDAG);
    152     }
    153 
    154     /// SelectAddrIdxOnly - Given the specified addressed, force it to be
    155     /// represented as an indexed [r+r] operation.
    156     bool SelectAddrIdxOnly(SDValue N, SDValue &Base, SDValue &Index) {
    157       return PPCLowering.SelectAddressRegRegOnly(N, Base, Index, *CurDAG);
    158     }
    159 
    160     /// SelectAddrImmShift - Returns true if the address N can be represented by
    161     /// a base register plus a signed 14-bit displacement [r+imm*4].  Suitable
    162     /// for use by STD and friends.
    163     bool SelectAddrImmShift(SDValue N, SDValue &Disp, SDValue &Base) {
    164       return PPCLowering.SelectAddressRegImmShift(N, Disp, Base, *CurDAG);
    165     }
    166 
    167     /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
    168     /// inline asm expressions.  It is always correct to compute the value into
    169     /// a register.  The case of adding a (possibly relocatable) constant to a
    170     /// register can be improved, but it is wrong to substitute Reg+Reg for
    171     /// Reg in an asm, because the load or store opcode would have to change.
    172    virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
    173                                               char ConstraintCode,
    174                                               std::vector<SDValue> &OutOps) {
    175       OutOps.push_back(Op);
    176       return false;
    177     }
    178 
    179     void InsertVRSaveCode(MachineFunction &MF);
    180 
    181     virtual const char *getPassName() const {
    182       return "PowerPC DAG->DAG Pattern Instruction Selection";
    183     }
    184 
    185 // Include the pieces autogenerated from the target description.
    186 #include "PPCGenDAGISel.inc"
    187 
    188 private:
    189     SDNode *SelectSETCC(SDNode *N);
    190   };
    191 }
    192 
    193 /// InsertVRSaveCode - Once the entire function has been instruction selected,
    194 /// all virtual registers are created and all machine instructions are built,
    195 /// check to see if we need to save/restore VRSAVE.  If so, do it.
    196 void PPCDAGToDAGISel::InsertVRSaveCode(MachineFunction &Fn) {
    197   // Check to see if this function uses vector registers, which means we have to
    198   // save and restore the VRSAVE register and update it with the regs we use.
    199   //
    200   // In this case, there will be virtual registers of vector type created
    201   // by the scheduler.  Detect them now.
    202   bool HasVectorVReg = false;
    203   for (unsigned i = 0, e = RegInfo->getNumVirtRegs(); i != e; ++i) {
    204     unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
    205     if (RegInfo->getRegClass(Reg) == &PPC::VRRCRegClass) {
    206       HasVectorVReg = true;
    207       break;
    208     }
    209   }
    210   if (!HasVectorVReg) return;  // nothing to do.
    211 
    212   // If we have a vector register, we want to emit code into the entry and exit
    213   // blocks to save and restore the VRSAVE register.  We do this here (instead
    214   // of marking all vector instructions as clobbering VRSAVE) for two reasons:
    215   //
    216   // 1. This (trivially) reduces the load on the register allocator, by not
    217   //    having to represent the live range of the VRSAVE register.
    218   // 2. This (more significantly) allows us to create a temporary virtual
    219   //    register to hold the saved VRSAVE value, allowing this temporary to be
    220   //    register allocated, instead of forcing it to be spilled to the stack.
    221 
    222   // Create two vregs - one to hold the VRSAVE register that is live-in to the
    223   // function and one for the value after having bits or'd into it.
    224   unsigned InVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
    225   unsigned UpdatedVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
    226 
    227   const TargetInstrInfo &TII = *TM.getInstrInfo();
    228   MachineBasicBlock &EntryBB = *Fn.begin();
    229   DebugLoc dl;
    230   // Emit the following code into the entry block:
    231   // InVRSAVE = MFVRSAVE
    232   // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE
    233   // MTVRSAVE UpdatedVRSAVE
    234   MachineBasicBlock::iterator IP = EntryBB.begin();  // Insert Point
    235   BuildMI(EntryBB, IP, dl, TII.get(PPC::MFVRSAVE), InVRSAVE);
    236   BuildMI(EntryBB, IP, dl, TII.get(PPC::UPDATE_VRSAVE),
    237           UpdatedVRSAVE).addReg(InVRSAVE);
    238   BuildMI(EntryBB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(UpdatedVRSAVE);
    239 
    240   // Find all return blocks, outputting a restore in each epilog.
    241   for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
    242     if (!BB->empty() && BB->back().isReturn()) {
    243       IP = BB->end(); --IP;
    244 
    245       // Skip over all terminator instructions, which are part of the return
    246       // sequence.
    247       MachineBasicBlock::iterator I2 = IP;
    248       while (I2 != BB->begin() && (--I2)->isTerminator())
    249         IP = I2;
    250 
    251       // Emit: MTVRSAVE InVRSave
    252       BuildMI(*BB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(InVRSAVE);
    253     }
    254   }
    255 }
    256 
    257 
    258 /// getGlobalBaseReg - Output the instructions required to put the
    259 /// base address to use for accessing globals into a register.
    260 ///
    261 SDNode *PPCDAGToDAGISel::getGlobalBaseReg() {
    262   if (!GlobalBaseReg) {
    263     const TargetInstrInfo &TII = *TM.getInstrInfo();
    264     // Insert the set of GlobalBaseReg into the first MBB of the function
    265     MachineBasicBlock &FirstMBB = MF->front();
    266     MachineBasicBlock::iterator MBBI = FirstMBB.begin();
    267     DebugLoc dl;
    268 
    269     if (PPCLowering.getPointerTy() == MVT::i32) {
    270       GlobalBaseReg = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
    271       BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR));
    272       BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
    273     } else {
    274       GlobalBaseReg = RegInfo->createVirtualRegister(&PPC::G8RCRegClass);
    275       BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR8));
    276       BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR8), GlobalBaseReg);
    277     }
    278   }
    279   return CurDAG->getRegister(GlobalBaseReg,
    280                              PPCLowering.getPointerTy()).getNode();
    281 }
    282 
    283 /// isIntS16Immediate - This method tests to see if the node is either a 32-bit
    284 /// or 64-bit immediate, and if the value can be accurately represented as a
    285 /// sign extension from a 16-bit value.  If so, this returns true and the
    286 /// immediate.
    287 static bool isIntS16Immediate(SDNode *N, short &Imm) {
    288   if (N->getOpcode() != ISD::Constant)
    289     return false;
    290 
    291   Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
    292   if (N->getValueType(0) == MVT::i32)
    293     return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
    294   else
    295     return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
    296 }
    297 
    298 static bool isIntS16Immediate(SDValue Op, short &Imm) {
    299   return isIntS16Immediate(Op.getNode(), Imm);
    300 }
    301 
    302 
    303 /// isInt32Immediate - This method tests to see if the node is a 32-bit constant
    304 /// operand. If so Imm will receive the 32-bit value.
    305 static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
    306   if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
    307     Imm = cast<ConstantSDNode>(N)->getZExtValue();
    308     return true;
    309   }
    310   return false;
    311 }
    312 
    313 /// isInt64Immediate - This method tests to see if the node is a 64-bit constant
    314 /// operand.  If so Imm will receive the 64-bit value.
    315 static bool isInt64Immediate(SDNode *N, uint64_t &Imm) {
    316   if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i64) {
    317     Imm = cast<ConstantSDNode>(N)->getZExtValue();
    318     return true;
    319   }
    320   return false;
    321 }
    322 
    323 // isInt32Immediate - This method tests to see if a constant operand.
    324 // If so Imm will receive the 32 bit value.
    325 static bool isInt32Immediate(SDValue N, unsigned &Imm) {
    326   return isInt32Immediate(N.getNode(), Imm);
    327 }
    328 
    329 
    330 // isOpcWithIntImmediate - This method tests to see if the node is a specific
    331 // opcode and that it has a immediate integer right operand.
    332 // If so Imm will receive the 32 bit value.
    333 static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
    334   return N->getOpcode() == Opc
    335          && isInt32Immediate(N->getOperand(1).getNode(), Imm);
    336 }
    337 
    338 bool PPCDAGToDAGISel::isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
    339   if (isShiftedMask_32(Val)) {
    340     // look for the first non-zero bit
    341     MB = CountLeadingZeros_32(Val);
    342     // look for the first zero bit after the run of ones
    343     ME = CountLeadingZeros_32((Val - 1) ^ Val);
    344     return true;
    345   } else {
    346     Val = ~Val; // invert mask
    347     if (isShiftedMask_32(Val)) {
    348       // effectively look for the first zero bit
    349       ME = CountLeadingZeros_32(Val) - 1;
    350       // effectively look for the first one bit after the run of zeros
    351       MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1;
    352       return true;
    353     }
    354   }
    355   // no run present
    356   return false;
    357 }
    358 
    359 bool PPCDAGToDAGISel::isRotateAndMask(SDNode *N, unsigned Mask,
    360                                       bool isShiftMask, unsigned &SH,
    361                                       unsigned &MB, unsigned &ME) {
    362   // Don't even go down this path for i64, since different logic will be
    363   // necessary for rldicl/rldicr/rldimi.
    364   if (N->getValueType(0) != MVT::i32)
    365     return false;
    366 
    367   unsigned Shift  = 32;
    368   unsigned Indeterminant = ~0;  // bit mask marking indeterminant results
    369   unsigned Opcode = N->getOpcode();
    370   if (N->getNumOperands() != 2 ||
    371       !isInt32Immediate(N->getOperand(1).getNode(), Shift) || (Shift > 31))
    372     return false;
    373 
    374   if (Opcode == ISD::SHL) {
    375     // apply shift left to mask if it comes first
    376     if (isShiftMask) Mask = Mask << Shift;
    377     // determine which bits are made indeterminant by shift
    378     Indeterminant = ~(0xFFFFFFFFu << Shift);
    379   } else if (Opcode == ISD::SRL) {
    380     // apply shift right 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     // adjust for the left rotate
    385     Shift = 32 - Shift;
    386   } else if (Opcode == ISD::ROTL) {
    387     Indeterminant = 0;
    388   } else {
    389     return false;
    390   }
    391 
    392   // if the mask doesn't intersect any Indeterminant bits
    393   if (Mask && !(Mask & Indeterminant)) {
    394     SH = Shift & 31;
    395     // make sure the mask is still a mask (wrap arounds may not be)
    396     return isRunOfOnes(Mask, MB, ME);
    397   }
    398   return false;
    399 }
    400 
    401 /// SelectBitfieldInsert - turn an or of two masked values into
    402 /// the rotate left word immediate then mask insert (rlwimi) instruction.
    403 SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
    404   SDValue Op0 = N->getOperand(0);
    405   SDValue Op1 = N->getOperand(1);
    406   DebugLoc dl = N->getDebugLoc();
    407 
    408   APInt LKZ, LKO, RKZ, RKO;
    409   CurDAG->ComputeMaskedBits(Op0, LKZ, LKO);
    410   CurDAG->ComputeMaskedBits(Op1, RKZ, RKO);
    411 
    412   unsigned TargetMask = LKZ.getZExtValue();
    413   unsigned InsertMask = RKZ.getZExtValue();
    414 
    415   if ((TargetMask | InsertMask) == 0xFFFFFFFF) {
    416     unsigned Op0Opc = Op0.getOpcode();
    417     unsigned Op1Opc = Op1.getOpcode();
    418     unsigned Value, SH = 0;
    419     TargetMask = ~TargetMask;
    420     InsertMask = ~InsertMask;
    421 
    422     // If the LHS has a foldable shift and the RHS does not, then swap it to the
    423     // RHS so that we can fold the shift into the insert.
    424     if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
    425       if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
    426           Op0.getOperand(0).getOpcode() == ISD::SRL) {
    427         if (Op1.getOperand(0).getOpcode() != ISD::SHL &&
    428             Op1.getOperand(0).getOpcode() != ISD::SRL) {
    429           std::swap(Op0, Op1);
    430           std::swap(Op0Opc, Op1Opc);
    431           std::swap(TargetMask, InsertMask);
    432         }
    433       }
    434     } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) {
    435       if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL &&
    436           Op1.getOperand(0).getOpcode() != ISD::SRL) {
    437         std::swap(Op0, Op1);
    438         std::swap(Op0Opc, Op1Opc);
    439         std::swap(TargetMask, InsertMask);
    440       }
    441     }
    442 
    443     unsigned MB, ME;
    444     if (InsertMask && isRunOfOnes(InsertMask, MB, ME)) {
    445       SDValue Tmp1, Tmp2;
    446 
    447       if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) &&
    448           isInt32Immediate(Op1.getOperand(1), Value)) {
    449         Op1 = Op1.getOperand(0);
    450         SH  = (Op1Opc == ISD::SHL) ? Value : 32 - Value;
    451       }
    452       if (Op1Opc == ISD::AND) {
    453         unsigned SHOpc = Op1.getOperand(0).getOpcode();
    454         if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) &&
    455             isInt32Immediate(Op1.getOperand(0).getOperand(1), Value)) {
    456           Op1 = Op1.getOperand(0).getOperand(0);
    457           SH  = (SHOpc == ISD::SHL) ? Value : 32 - Value;
    458         } else {
    459           Op1 = Op1.getOperand(0);
    460         }
    461       }
    462 
    463       SH &= 31;
    464       SDValue Ops[] = { Op0, Op1, getI32Imm(SH), getI32Imm(MB),
    465                           getI32Imm(ME) };
    466       return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops, 5);
    467     }
    468   }
    469   return 0;
    470 }
    471 
    472 /// SelectCC - Select a comparison of the specified values with the specified
    473 /// condition code, returning the CR# of the expression.
    474 SDValue PPCDAGToDAGISel::SelectCC(SDValue LHS, SDValue RHS,
    475                                     ISD::CondCode CC, DebugLoc dl) {
    476   // Always select the LHS.
    477   unsigned Opc;
    478 
    479   if (LHS.getValueType() == MVT::i32) {
    480     unsigned Imm;
    481     if (CC == ISD::SETEQ || CC == ISD::SETNE) {
    482       if (isInt32Immediate(RHS, Imm)) {
    483         // SETEQ/SETNE comparison with 16-bit immediate, fold it.
    484         if (isUInt<16>(Imm))
    485           return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
    486                                                 getI32Imm(Imm & 0xFFFF)), 0);
    487         // If this is a 16-bit signed immediate, fold it.
    488         if (isInt<16>((int)Imm))
    489           return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
    490                                                 getI32Imm(Imm & 0xFFFF)), 0);
    491 
    492         // For non-equality comparisons, the default code would materialize the
    493         // constant, then compare against it, like this:
    494         //   lis r2, 4660
    495         //   ori r2, r2, 22136
    496         //   cmpw cr0, r3, r2
    497         // Since we are just comparing for equality, we can emit this instead:
    498         //   xoris r0,r3,0x1234
    499         //   cmplwi cr0,r0,0x5678
    500         //   beq cr0,L6
    501         SDValue Xor(CurDAG->getMachineNode(PPC::XORIS, dl, MVT::i32, LHS,
    502                                            getI32Imm(Imm >> 16)), 0);
    503         return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, Xor,
    504                                               getI32Imm(Imm & 0xFFFF)), 0);
    505       }
    506       Opc = PPC::CMPLW;
    507     } else if (ISD::isUnsignedIntSetCC(CC)) {
    508       if (isInt32Immediate(RHS, Imm) && isUInt<16>(Imm))
    509         return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
    510                                               getI32Imm(Imm & 0xFFFF)), 0);
    511       Opc = PPC::CMPLW;
    512     } else {
    513       short SImm;
    514       if (isIntS16Immediate(RHS, SImm))
    515         return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
    516                                               getI32Imm((int)SImm & 0xFFFF)),
    517                          0);
    518       Opc = PPC::CMPW;
    519     }
    520   } else if (LHS.getValueType() == MVT::i64) {
    521     uint64_t Imm;
    522     if (CC == ISD::SETEQ || CC == ISD::SETNE) {
    523       if (isInt64Immediate(RHS.getNode(), Imm)) {
    524         // SETEQ/SETNE comparison with 16-bit immediate, fold it.
    525         if (isUInt<16>(Imm))
    526           return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
    527                                                 getI32Imm(Imm & 0xFFFF)), 0);
    528         // If this is a 16-bit signed immediate, fold it.
    529         if (isInt<16>(Imm))
    530           return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
    531                                                 getI32Imm(Imm & 0xFFFF)), 0);
    532 
    533         // For non-equality comparisons, the default code would materialize the
    534         // constant, then compare against it, like this:
    535         //   lis r2, 4660
    536         //   ori r2, r2, 22136
    537         //   cmpd cr0, r3, r2
    538         // Since we are just comparing for equality, we can emit this instead:
    539         //   xoris r0,r3,0x1234
    540         //   cmpldi cr0,r0,0x5678
    541         //   beq cr0,L6
    542         if (isUInt<32>(Imm)) {
    543           SDValue Xor(CurDAG->getMachineNode(PPC::XORIS8, dl, MVT::i64, LHS,
    544                                              getI64Imm(Imm >> 16)), 0);
    545           return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, Xor,
    546                                                 getI64Imm(Imm & 0xFFFF)), 0);
    547         }
    548       }
    549       Opc = PPC::CMPLD;
    550     } else if (ISD::isUnsignedIntSetCC(CC)) {
    551       if (isInt64Immediate(RHS.getNode(), Imm) && isUInt<16>(Imm))
    552         return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
    553                                               getI64Imm(Imm & 0xFFFF)), 0);
    554       Opc = PPC::CMPLD;
    555     } else {
    556       short SImm;
    557       if (isIntS16Immediate(RHS, SImm))
    558         return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
    559                                               getI64Imm(SImm & 0xFFFF)),
    560                          0);
    561       Opc = PPC::CMPD;
    562     }
    563   } else if (LHS.getValueType() == MVT::f32) {
    564     Opc = PPC::FCMPUS;
    565   } else {
    566     assert(LHS.getValueType() == MVT::f64 && "Unknown vt!");
    567     Opc = PPC::FCMPUD;
    568   }
    569   return SDValue(CurDAG->getMachineNode(Opc, dl, MVT::i32, LHS, RHS), 0);
    570 }
    571 
    572 static PPC::Predicate getPredicateForSetCC(ISD::CondCode CC) {
    573   switch (CC) {
    574   case ISD::SETUEQ:
    575   case ISD::SETONE:
    576   case ISD::SETOLE:
    577   case ISD::SETOGE:
    578     llvm_unreachable("Should be lowered by legalize!");
    579   default: llvm_unreachable("Unknown condition!");
    580   case ISD::SETOEQ:
    581   case ISD::SETEQ:  return PPC::PRED_EQ;
    582   case ISD::SETUNE:
    583   case ISD::SETNE:  return PPC::PRED_NE;
    584   case ISD::SETOLT:
    585   case ISD::SETLT:  return PPC::PRED_LT;
    586   case ISD::SETULE:
    587   case ISD::SETLE:  return PPC::PRED_LE;
    588   case ISD::SETOGT:
    589   case ISD::SETGT:  return PPC::PRED_GT;
    590   case ISD::SETUGE:
    591   case ISD::SETGE:  return PPC::PRED_GE;
    592   case ISD::SETO:   return PPC::PRED_NU;
    593   case ISD::SETUO:  return PPC::PRED_UN;
    594     // These two are invalid for floating point.  Assume we have int.
    595   case ISD::SETULT: return PPC::PRED_LT;
    596   case ISD::SETUGT: return PPC::PRED_GT;
    597   }
    598 }
    599 
    600 /// getCRIdxForSetCC - Return the index of the condition register field
    601 /// associated with the SetCC condition, and whether or not the field is
    602 /// treated as inverted.  That is, lt = 0; ge = 0 inverted.
    603 ///
    604 /// If this returns with Other != -1, then the returned comparison is an or of
    605 /// two simpler comparisons.  In this case, Invert is guaranteed to be false.
    606 static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool &Invert, int &Other) {
    607   Invert = false;
    608   Other = -1;
    609   switch (CC) {
    610   default: llvm_unreachable("Unknown condition!");
    611   case ISD::SETOLT:
    612   case ISD::SETLT:  return 0;                  // Bit #0 = SETOLT
    613   case ISD::SETOGT:
    614   case ISD::SETGT:  return 1;                  // Bit #1 = SETOGT
    615   case ISD::SETOEQ:
    616   case ISD::SETEQ:  return 2;                  // Bit #2 = SETOEQ
    617   case ISD::SETUO:  return 3;                  // Bit #3 = SETUO
    618   case ISD::SETUGE:
    619   case ISD::SETGE:  Invert = true; return 0;   // !Bit #0 = SETUGE
    620   case ISD::SETULE:
    621   case ISD::SETLE:  Invert = true; return 1;   // !Bit #1 = SETULE
    622   case ISD::SETUNE:
    623   case ISD::SETNE:  Invert = true; return 2;   // !Bit #2 = SETUNE
    624   case ISD::SETO:   Invert = true; return 3;   // !Bit #3 = SETO
    625   case ISD::SETUEQ:
    626   case ISD::SETOGE:
    627   case ISD::SETOLE:
    628   case ISD::SETONE:
    629     llvm_unreachable("Invalid branch code: should be expanded by legalize");
    630   // These are invalid for floating point.  Assume integer.
    631   case ISD::SETULT: return 0;
    632   case ISD::SETUGT: return 1;
    633   }
    634 }
    635 
    636 // getVCmpInst: return the vector compare instruction for the specified
    637 // vector type and condition code. Since this is for altivec specific code,
    638 // only support the altivec types (v16i8, v8i16, v4i32, and v4f32).
    639 static unsigned int getVCmpInst(MVT::SimpleValueType VecVT, ISD::CondCode CC) {
    640   switch (CC) {
    641     case ISD::SETEQ:
    642     case ISD::SETUEQ:
    643     case ISD::SETNE:
    644     case ISD::SETUNE:
    645       if (VecVT == MVT::v16i8)
    646         return PPC::VCMPEQUB;
    647       else if (VecVT == MVT::v8i16)
    648         return PPC::VCMPEQUH;
    649       else if (VecVT == MVT::v4i32)
    650         return PPC::VCMPEQUW;
    651       // v4f32 != v4f32 could be translate to unordered not equal
    652       else if (VecVT == MVT::v4f32)
    653         return PPC::VCMPEQFP;
    654       break;
    655     case ISD::SETLT:
    656     case ISD::SETGT:
    657     case ISD::SETLE:
    658     case ISD::SETGE:
    659       if (VecVT == MVT::v16i8)
    660         return PPC::VCMPGTSB;
    661       else if (VecVT == MVT::v8i16)
    662         return PPC::VCMPGTSH;
    663       else if (VecVT == MVT::v4i32)
    664         return PPC::VCMPGTSW;
    665       else if (VecVT == MVT::v4f32)
    666         return PPC::VCMPGTFP;
    667       break;
    668     case ISD::SETULT:
    669     case ISD::SETUGT:
    670     case ISD::SETUGE:
    671     case ISD::SETULE:
    672       if (VecVT == MVT::v16i8)
    673         return PPC::VCMPGTUB;
    674       else if (VecVT == MVT::v8i16)
    675         return PPC::VCMPGTUH;
    676       else if (VecVT == MVT::v4i32)
    677         return PPC::VCMPGTUW;
    678       break;
    679     case ISD::SETOEQ:
    680       if (VecVT == MVT::v4f32)
    681         return PPC::VCMPEQFP;
    682       break;
    683     case ISD::SETOLT:
    684     case ISD::SETOGT:
    685     case ISD::SETOLE:
    686       if (VecVT == MVT::v4f32)
    687         return PPC::VCMPGTFP;
    688       break;
    689     case ISD::SETOGE:
    690       if (VecVT == MVT::v4f32)
    691         return PPC::VCMPGEFP;
    692       break;
    693     default:
    694       break;
    695   }
    696   llvm_unreachable("Invalid integer vector compare condition");
    697 }
    698 
    699 // getVCmpEQInst: return the equal compare instruction for the specified vector
    700 // type. Since this is for altivec specific code, only support the altivec
    701 // types (v16i8, v8i16, v4i32, and v4f32).
    702 static unsigned int getVCmpEQInst(MVT::SimpleValueType VecVT) {
    703   switch (VecVT) {
    704     case MVT::v16i8:
    705       return PPC::VCMPEQUB;
    706     case MVT::v8i16:
    707       return PPC::VCMPEQUH;
    708     case MVT::v4i32:
    709       return PPC::VCMPEQUW;
    710     case MVT::v4f32:
    711       return PPC::VCMPEQFP;
    712     default:
    713       llvm_unreachable("Invalid integer vector compare condition");
    714   }
    715 }
    716 
    717 
    718 SDNode *PPCDAGToDAGISel::SelectSETCC(SDNode *N) {
    719   DebugLoc dl = N->getDebugLoc();
    720   unsigned Imm;
    721   ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
    722   EVT PtrVT = CurDAG->getTargetLoweringInfo().getPointerTy();
    723   bool isPPC64 = (PtrVT == MVT::i64);
    724 
    725   if (isInt32Immediate(N->getOperand(1), Imm)) {
    726     // We can codegen setcc op, imm very efficiently compared to a brcond.
    727     // Check for those cases here.
    728     // setcc op, 0
    729     if (Imm == 0) {
    730       SDValue Op = N->getOperand(0);
    731       switch (CC) {
    732       default: break;
    733       case ISD::SETEQ: {
    734         Op = SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, Op), 0);
    735         SDValue Ops[] = { Op, getI32Imm(27), getI32Imm(5), getI32Imm(31) };
    736         return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
    737       }
    738       case ISD::SETNE: {
    739         if (isPPC64) break;
    740         SDValue AD =
    741           SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
    742                                          Op, getI32Imm(~0U)), 0);
    743         return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op,
    744                                     AD.getValue(1));
    745       }
    746       case ISD::SETLT: {
    747         SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
    748         return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
    749       }
    750       case ISD::SETGT: {
    751         SDValue T =
    752           SDValue(CurDAG->getMachineNode(PPC::NEG, dl, MVT::i32, Op), 0);
    753         T = SDValue(CurDAG->getMachineNode(PPC::ANDC, dl, MVT::i32, T, Op), 0);
    754         SDValue Ops[] = { T, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
    755         return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
    756       }
    757       }
    758     } else if (Imm == ~0U) {        // setcc op, -1
    759       SDValue Op = N->getOperand(0);
    760       switch (CC) {
    761       default: break;
    762       case ISD::SETEQ:
    763         if (isPPC64) break;
    764         Op = SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
    765                                             Op, getI32Imm(1)), 0);
    766         return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
    767                               SDValue(CurDAG->getMachineNode(PPC::LI, dl,
    768                                                              MVT::i32,
    769                                                              getI32Imm(0)), 0),
    770                                       Op.getValue(1));
    771       case ISD::SETNE: {
    772         if (isPPC64) break;
    773         Op = SDValue(CurDAG->getMachineNode(PPC::NOR, dl, MVT::i32, Op, Op), 0);
    774         SDNode *AD = CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
    775                                             Op, getI32Imm(~0U));
    776         return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDValue(AD, 0),
    777                                     Op, SDValue(AD, 1));
    778       }
    779       case ISD::SETLT: {
    780         SDValue AD = SDValue(CurDAG->getMachineNode(PPC::ADDI, dl, MVT::i32, Op,
    781                                                     getI32Imm(1)), 0);
    782         SDValue AN = SDValue(CurDAG->getMachineNode(PPC::AND, dl, MVT::i32, AD,
    783                                                     Op), 0);
    784         SDValue Ops[] = { AN, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
    785         return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
    786       }
    787       case ISD::SETGT: {
    788         SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
    789         Op = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops, 4),
    790                      0);
    791         return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op,
    792                                     getI32Imm(1));
    793       }
    794       }
    795     }
    796   }
    797 
    798   SDValue LHS = N->getOperand(0);
    799   SDValue RHS = N->getOperand(1);
    800 
    801   // Altivec Vector compare instructions do not set any CR register by default and
    802   // vector compare operations return the same type as the operands.
    803   if (LHS.getValueType().isVector()) {
    804     EVT VecVT = LHS.getValueType();
    805     MVT::SimpleValueType VT = VecVT.getSimpleVT().SimpleTy;
    806     unsigned int VCmpInst = getVCmpInst(VT, CC);
    807 
    808     switch (CC) {
    809       case ISD::SETEQ:
    810       case ISD::SETOEQ:
    811       case ISD::SETUEQ:
    812         return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, LHS, RHS);
    813       case ISD::SETNE:
    814       case ISD::SETONE:
    815       case ISD::SETUNE: {
    816         SDValue VCmp(CurDAG->getMachineNode(VCmpInst, dl, VecVT, LHS, RHS), 0);
    817         return CurDAG->SelectNodeTo(N, PPC::VNOR, VecVT, VCmp, VCmp);
    818       }
    819       case ISD::SETLT:
    820       case ISD::SETOLT:
    821       case ISD::SETULT:
    822         return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, RHS, LHS);
    823       case ISD::SETGT:
    824       case ISD::SETOGT:
    825       case ISD::SETUGT:
    826         return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, LHS, RHS);
    827       case ISD::SETGE:
    828       case ISD::SETOGE:
    829       case ISD::SETUGE: {
    830         // Small optimization: Altivec provides a 'Vector Compare Greater Than
    831         // or Equal To' instruction (vcmpgefp), so in this case there is no
    832         // need for extra logic for the equal compare.
    833         if (VecVT.getSimpleVT().isFloatingPoint()) {
    834           return CurDAG->SelectNodeTo(N, VCmpInst, VecVT, LHS, RHS);
    835         } else {
    836           SDValue VCmpGT(CurDAG->getMachineNode(VCmpInst, dl, VecVT, LHS, RHS), 0);
    837           unsigned int VCmpEQInst = getVCmpEQInst(VT);
    838           SDValue VCmpEQ(CurDAG->getMachineNode(VCmpEQInst, dl, VecVT, LHS, RHS), 0);
    839           return CurDAG->SelectNodeTo(N, PPC::VOR, VecVT, VCmpGT, VCmpEQ);
    840         }
    841       }
    842       case ISD::SETLE:
    843       case ISD::SETOLE:
    844       case ISD::SETULE: {
    845         SDValue VCmpLE(CurDAG->getMachineNode(VCmpInst, dl, VecVT, RHS, LHS), 0);
    846         unsigned int VCmpEQInst = getVCmpEQInst(VT);
    847         SDValue VCmpEQ(CurDAG->getMachineNode(VCmpEQInst, dl, VecVT, LHS, RHS), 0);
    848         return CurDAG->SelectNodeTo(N, PPC::VOR, VecVT, VCmpLE, VCmpEQ);
    849       }
    850       default:
    851         llvm_unreachable("Invalid vector compare type: should be expanded by legalize");
    852     }
    853   }
    854 
    855   bool Inv;
    856   int OtherCondIdx;
    857   unsigned Idx = getCRIdxForSetCC(CC, Inv, OtherCondIdx);
    858   SDValue CCReg = SelectCC(LHS, RHS, CC, dl);
    859   SDValue IntCR;
    860 
    861   // Force the ccreg into CR7.
    862   SDValue CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
    863 
    864   SDValue InFlag(0, 0);  // Null incoming flag value.
    865   CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, CR7Reg, CCReg,
    866                                InFlag).getValue(1);
    867 
    868   if (PPCSubTarget.hasMFOCRF() && OtherCondIdx == -1)
    869     IntCR = SDValue(CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32, CR7Reg,
    870                                            CCReg), 0);
    871   else
    872     IntCR = SDValue(CurDAG->getMachineNode(PPC::MFCRpseud, dl, MVT::i32,
    873                                            CR7Reg, CCReg), 0);
    874 
    875   SDValue Ops[] = { IntCR, getI32Imm((32-(3-Idx)) & 31),
    876                       getI32Imm(31), getI32Imm(31) };
    877   if (OtherCondIdx == -1 && !Inv)
    878     return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
    879 
    880   // Get the specified bit.
    881   SDValue Tmp =
    882     SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops, 4), 0);
    883   if (Inv) {
    884     assert(OtherCondIdx == -1 && "Can't have split plus negation");
    885     return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1));
    886   }
    887 
    888   // Otherwise, we have to turn an operation like SETONE -> SETOLT | SETOGT.
    889   // We already got the bit for the first part of the comparison (e.g. SETULE).
    890 
    891   // Get the other bit of the comparison.
    892   Ops[1] = getI32Imm((32-(3-OtherCondIdx)) & 31);
    893   SDValue OtherCond =
    894     SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops, 4), 0);
    895 
    896   return CurDAG->SelectNodeTo(N, PPC::OR, MVT::i32, Tmp, OtherCond);
    897 }
    898 
    899 
    900 // Select - Convert the specified operand from a target-independent to a
    901 // target-specific node if it hasn't already been changed.
    902 SDNode *PPCDAGToDAGISel::Select(SDNode *N) {
    903   DebugLoc dl = N->getDebugLoc();
    904   if (N->isMachineOpcode())
    905     return NULL;   // Already selected.
    906 
    907   switch (N->getOpcode()) {
    908   default: break;
    909 
    910   case ISD::Constant: {
    911     if (N->getValueType(0) == MVT::i64) {
    912       // Get 64 bit value.
    913       int64_t Imm = cast<ConstantSDNode>(N)->getZExtValue();
    914       // Assume no remaining bits.
    915       unsigned Remainder = 0;
    916       // Assume no shift required.
    917       unsigned Shift = 0;
    918 
    919       // If it can't be represented as a 32 bit value.
    920       if (!isInt<32>(Imm)) {
    921         Shift = CountTrailingZeros_64(Imm);
    922         int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
    923 
    924         // If the shifted value fits 32 bits.
    925         if (isInt<32>(ImmSh)) {
    926           // Go with the shifted value.
    927           Imm = ImmSh;
    928         } else {
    929           // Still stuck with a 64 bit value.
    930           Remainder = Imm;
    931           Shift = 32;
    932           Imm >>= 32;
    933         }
    934       }
    935 
    936       // Intermediate operand.
    937       SDNode *Result;
    938 
    939       // Handle first 32 bits.
    940       unsigned Lo = Imm & 0xFFFF;
    941       unsigned Hi = (Imm >> 16) & 0xFFFF;
    942 
    943       // Simple value.
    944       if (isInt<16>(Imm)) {
    945        // Just the Lo bits.
    946         Result = CurDAG->getMachineNode(PPC::LI8, dl, MVT::i64, getI32Imm(Lo));
    947       } else if (Lo) {
    948         // Handle the Hi bits.
    949         unsigned OpC = Hi ? PPC::LIS8 : PPC::LI8;
    950         Result = CurDAG->getMachineNode(OpC, dl, MVT::i64, getI32Imm(Hi));
    951         // And Lo bits.
    952         Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
    953                                         SDValue(Result, 0), getI32Imm(Lo));
    954       } else {
    955        // Just the Hi bits.
    956         Result = CurDAG->getMachineNode(PPC::LIS8, dl, MVT::i64, getI32Imm(Hi));
    957       }
    958 
    959       // If no shift, we're done.
    960       if (!Shift) return Result;
    961 
    962       // Shift for next step if the upper 32-bits were not zero.
    963       if (Imm) {
    964         Result = CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64,
    965                                         SDValue(Result, 0),
    966                                         getI32Imm(Shift),
    967                                         getI32Imm(63 - Shift));
    968       }
    969 
    970       // Add in the last bits as required.
    971       if ((Hi = (Remainder >> 16) & 0xFFFF)) {
    972         Result = CurDAG->getMachineNode(PPC::ORIS8, dl, MVT::i64,
    973                                         SDValue(Result, 0), getI32Imm(Hi));
    974       }
    975       if ((Lo = Remainder & 0xFFFF)) {
    976         Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
    977                                         SDValue(Result, 0), getI32Imm(Lo));
    978       }
    979 
    980       return Result;
    981     }
    982     break;
    983   }
    984 
    985   case ISD::SETCC:
    986     return SelectSETCC(N);
    987   case PPCISD::GlobalBaseReg:
    988     return getGlobalBaseReg();
    989 
    990   case ISD::FrameIndex: {
    991     int FI = cast<FrameIndexSDNode>(N)->getIndex();
    992     SDValue TFI = CurDAG->getTargetFrameIndex(FI, N->getValueType(0));
    993     unsigned Opc = N->getValueType(0) == MVT::i32 ? PPC::ADDI : PPC::ADDI8;
    994     if (N->hasOneUse())
    995       return CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), TFI,
    996                                   getSmallIPtrImm(0));
    997     return CurDAG->getMachineNode(Opc, dl, N->getValueType(0), TFI,
    998                                   getSmallIPtrImm(0));
    999   }
   1000 
   1001   case PPCISD::MFCR: {
   1002     SDValue InFlag = N->getOperand(1);
   1003     // Use MFOCRF if supported.
   1004     if (PPCSubTarget.hasMFOCRF())
   1005       return CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32,
   1006                                     N->getOperand(0), InFlag);
   1007     else
   1008       return CurDAG->getMachineNode(PPC::MFCRpseud, dl, MVT::i32,
   1009                                     N->getOperand(0), InFlag);
   1010   }
   1011 
   1012   case ISD::SDIV: {
   1013     // FIXME: since this depends on the setting of the carry flag from the srawi
   1014     //        we should really be making notes about that for the scheduler.
   1015     // FIXME: It sure would be nice if we could cheaply recognize the
   1016     //        srl/add/sra pattern the dag combiner will generate for this as
   1017     //        sra/addze rather than having to handle sdiv ourselves.  oh well.
   1018     unsigned Imm;
   1019     if (isInt32Immediate(N->getOperand(1), Imm)) {
   1020       SDValue N0 = N->getOperand(0);
   1021       if ((signed)Imm > 0 && isPowerOf2_32(Imm)) {
   1022         SDNode *Op =
   1023           CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue,
   1024                                  N0, getI32Imm(Log2_32(Imm)));
   1025         return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
   1026                                     SDValue(Op, 0), SDValue(Op, 1));
   1027       } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) {
   1028         SDNode *Op =
   1029           CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue,
   1030                                  N0, getI32Imm(Log2_32(-Imm)));
   1031         SDValue PT =
   1032           SDValue(CurDAG->getMachineNode(PPC::ADDZE, dl, MVT::i32,
   1033                                          SDValue(Op, 0), SDValue(Op, 1)),
   1034                     0);
   1035         return CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT);
   1036       }
   1037     }
   1038 
   1039     // Other cases are autogenerated.
   1040     break;
   1041   }
   1042 
   1043   case ISD::LOAD: {
   1044     // Handle preincrement loads.
   1045     LoadSDNode *LD = cast<LoadSDNode>(N);
   1046     EVT LoadedVT = LD->getMemoryVT();
   1047 
   1048     // Normal loads are handled by code generated from the .td file.
   1049     if (LD->getAddressingMode() != ISD::PRE_INC)
   1050       break;
   1051 
   1052     SDValue Offset = LD->getOffset();
   1053     if (isa<ConstantSDNode>(Offset) ||
   1054         Offset.getOpcode() == ISD::TargetGlobalAddress) {
   1055 
   1056       unsigned Opcode;
   1057       bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
   1058       if (LD->getValueType(0) != MVT::i64) {
   1059         // Handle PPC32 integer and normal FP loads.
   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::f64: Opcode = PPC::LFDU; break;
   1064           case MVT::f32: Opcode = PPC::LFSU; break;
   1065           case MVT::i32: Opcode = PPC::LWZU; break;
   1066           case MVT::i16: Opcode = isSExt ? PPC::LHAU : PPC::LHZU; break;
   1067           case MVT::i1:
   1068           case MVT::i8:  Opcode = PPC::LBZU; break;
   1069         }
   1070       } else {
   1071         assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
   1072         assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
   1073         switch (LoadedVT.getSimpleVT().SimpleTy) {
   1074           default: llvm_unreachable("Invalid PPC load type!");
   1075           case MVT::i64: Opcode = PPC::LDU; break;
   1076           case MVT::i32: Opcode = PPC::LWZU8; break;
   1077           case MVT::i16: Opcode = isSExt ? PPC::LHAU8 : PPC::LHZU8; break;
   1078           case MVT::i1:
   1079           case MVT::i8:  Opcode = PPC::LBZU8; break;
   1080         }
   1081       }
   1082 
   1083       SDValue Chain = LD->getChain();
   1084       SDValue Base = LD->getBasePtr();
   1085       SDValue Ops[] = { Offset, Base, Chain };
   1086       return CurDAG->getMachineNode(Opcode, dl, LD->getValueType(0),
   1087                                     PPCLowering.getPointerTy(),
   1088                                     MVT::Other, Ops, 3);
   1089     } else {
   1090       unsigned Opcode;
   1091       bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
   1092       if (LD->getValueType(0) != MVT::i64) {
   1093         // Handle PPC32 integer and normal FP loads.
   1094         assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
   1095         switch (LoadedVT.getSimpleVT().SimpleTy) {
   1096           default: llvm_unreachable("Invalid PPC load type!");
   1097           case MVT::f64: Opcode = PPC::LFDUX; break;
   1098           case MVT::f32: Opcode = PPC::LFSUX; break;
   1099           case MVT::i32: Opcode = PPC::LWZUX; break;
   1100           case MVT::i16: Opcode = isSExt ? PPC::LHAUX : PPC::LHZUX; break;
   1101           case MVT::i1:
   1102           case MVT::i8:  Opcode = PPC::LBZUX; break;
   1103         }
   1104       } else {
   1105         assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
   1106         assert((!isSExt || LoadedVT == MVT::i16 || LoadedVT == MVT::i32) &&
   1107                "Invalid sext update load");
   1108         switch (LoadedVT.getSimpleVT().SimpleTy) {
   1109           default: llvm_unreachable("Invalid PPC load type!");
   1110           case MVT::i64: Opcode = PPC::LDUX; break;
   1111           case MVT::i32: Opcode = isSExt ? PPC::LWAUX  : PPC::LWZUX8; break;
   1112           case MVT::i16: Opcode = isSExt ? PPC::LHAUX8 : PPC::LHZUX8; break;
   1113           case MVT::i1:
   1114           case MVT::i8:  Opcode = PPC::LBZUX8; break;
   1115         }
   1116       }
   1117 
   1118       SDValue Chain = LD->getChain();
   1119       SDValue Base = LD->getBasePtr();
   1120       SDValue Ops[] = { Offset, Base, Chain };
   1121       return CurDAG->getMachineNode(Opcode, dl, LD->getValueType(0),
   1122                                     PPCLowering.getPointerTy(),
   1123                                     MVT::Other, Ops, 3);
   1124     }
   1125   }
   1126 
   1127   case ISD::AND: {
   1128     unsigned Imm, Imm2, SH, MB, ME;
   1129     uint64_t Imm64;
   1130 
   1131     // If this is an and of a value rotated between 0 and 31 bits and then and'd
   1132     // with a mask, emit rlwinm
   1133     if (isInt32Immediate(N->getOperand(1), Imm) &&
   1134         isRotateAndMask(N->getOperand(0).getNode(), Imm, false, SH, MB, ME)) {
   1135       SDValue Val = N->getOperand(0).getOperand(0);
   1136       SDValue Ops[] = { Val, getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
   1137       return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
   1138     }
   1139     // If this is just a masked value where the input is not handled above, and
   1140     // is not a rotate-left (handled by a pattern in the .td file), emit rlwinm
   1141     if (isInt32Immediate(N->getOperand(1), Imm) &&
   1142         isRunOfOnes(Imm, MB, ME) &&
   1143         N->getOperand(0).getOpcode() != ISD::ROTL) {
   1144       SDValue Val = N->getOperand(0);
   1145       SDValue Ops[] = { Val, getI32Imm(0), getI32Imm(MB), getI32Imm(ME) };
   1146       return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
   1147     }
   1148     // If this is a 64-bit zero-extension mask, emit rldicl.
   1149     if (isInt64Immediate(N->getOperand(1).getNode(), Imm64) &&
   1150         isMask_64(Imm64)) {
   1151       SDValue Val = N->getOperand(0);
   1152       MB = 64 - CountTrailingOnes_64(Imm64);
   1153       SDValue Ops[] = { Val, getI32Imm(0), getI32Imm(MB) };
   1154       return CurDAG->SelectNodeTo(N, PPC::RLDICL, MVT::i64, Ops, 3);
   1155     }
   1156     // AND X, 0 -> 0, not "rlwinm 32".
   1157     if (isInt32Immediate(N->getOperand(1), Imm) && (Imm == 0)) {
   1158       ReplaceUses(SDValue(N, 0), N->getOperand(1));
   1159       return NULL;
   1160     }
   1161     // ISD::OR doesn't get all the bitfield insertion fun.
   1162     // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) is a bitfield insert
   1163     if (isInt32Immediate(N->getOperand(1), Imm) &&
   1164         N->getOperand(0).getOpcode() == ISD::OR &&
   1165         isInt32Immediate(N->getOperand(0).getOperand(1), Imm2)) {
   1166       unsigned MB, ME;
   1167       Imm = ~(Imm^Imm2);
   1168       if (isRunOfOnes(Imm, MB, ME)) {
   1169         SDValue Ops[] = { N->getOperand(0).getOperand(0),
   1170                             N->getOperand(0).getOperand(1),
   1171                             getI32Imm(0), getI32Imm(MB),getI32Imm(ME) };
   1172         return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops, 5);
   1173       }
   1174     }
   1175 
   1176     // Other cases are autogenerated.
   1177     break;
   1178   }
   1179   case ISD::OR:
   1180     if (N->getValueType(0) == MVT::i32)
   1181       if (SDNode *I = SelectBitfieldInsert(N))
   1182         return I;
   1183 
   1184     // Other cases are autogenerated.
   1185     break;
   1186   case ISD::SHL: {
   1187     unsigned Imm, SH, MB, ME;
   1188     if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
   1189         isRotateAndMask(N, Imm, true, SH, MB, ME)) {
   1190       SDValue Ops[] = { N->getOperand(0).getOperand(0),
   1191                           getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
   1192       return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
   1193     }
   1194 
   1195     // Other cases are autogenerated.
   1196     break;
   1197   }
   1198   case ISD::SRL: {
   1199     unsigned Imm, SH, MB, ME;
   1200     if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
   1201         isRotateAndMask(N, Imm, true, SH, MB, ME)) {
   1202       SDValue Ops[] = { N->getOperand(0).getOperand(0),
   1203                           getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
   1204       return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
   1205     }
   1206 
   1207     // Other cases are autogenerated.
   1208     break;
   1209   }
   1210   case ISD::SELECT_CC: {
   1211     ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
   1212     EVT PtrVT = CurDAG->getTargetLoweringInfo().getPointerTy();
   1213     bool isPPC64 = (PtrVT == MVT::i64);
   1214 
   1215     // Handle the setcc cases here.  select_cc lhs, 0, 1, 0, cc
   1216     if (!isPPC64)
   1217       if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
   1218         if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
   1219           if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
   1220             if (N1C->isNullValue() && N3C->isNullValue() &&
   1221                 N2C->getZExtValue() == 1ULL && CC == ISD::SETNE &&
   1222                 // FIXME: Implement this optzn for PPC64.
   1223                 N->getValueType(0) == MVT::i32) {
   1224               SDNode *Tmp =
   1225                 CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
   1226                                        N->getOperand(0), getI32Imm(~0U));
   1227               return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32,
   1228                                           SDValue(Tmp, 0), N->getOperand(0),
   1229                                           SDValue(Tmp, 1));
   1230             }
   1231 
   1232     SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC, dl);
   1233     unsigned BROpc = getPredicateForSetCC(CC);
   1234 
   1235     unsigned SelectCCOp;
   1236     if (N->getValueType(0) == MVT::i32)
   1237       SelectCCOp = PPC::SELECT_CC_I4;
   1238     else if (N->getValueType(0) == MVT::i64)
   1239       SelectCCOp = PPC::SELECT_CC_I8;
   1240     else if (N->getValueType(0) == MVT::f32)
   1241       SelectCCOp = PPC::SELECT_CC_F4;
   1242     else if (N->getValueType(0) == MVT::f64)
   1243       SelectCCOp = PPC::SELECT_CC_F8;
   1244     else
   1245       SelectCCOp = PPC::SELECT_CC_VRRC;
   1246 
   1247     SDValue Ops[] = { CCReg, N->getOperand(2), N->getOperand(3),
   1248                         getI32Imm(BROpc) };
   1249     return CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), Ops, 4);
   1250   }
   1251   case PPCISD::COND_BRANCH: {
   1252     // Op #0 is the Chain.
   1253     // Op #1 is the PPC::PRED_* number.
   1254     // Op #2 is the CR#
   1255     // Op #3 is the Dest MBB
   1256     // Op #4 is the Flag.
   1257     // Prevent PPC::PRED_* from being selected into LI.
   1258     SDValue Pred =
   1259       getI32Imm(cast<ConstantSDNode>(N->getOperand(1))->getZExtValue());
   1260     SDValue Ops[] = { Pred, N->getOperand(2), N->getOperand(3),
   1261       N->getOperand(0), N->getOperand(4) };
   1262     return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops, 5);
   1263   }
   1264   case ISD::BR_CC: {
   1265     ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
   1266     SDValue CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC, dl);
   1267     SDValue Ops[] = { getI32Imm(getPredicateForSetCC(CC)), CondCode,
   1268                         N->getOperand(4), N->getOperand(0) };
   1269     return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops, 4);
   1270   }
   1271   case ISD::BRIND: {
   1272     // FIXME: Should custom lower this.
   1273     SDValue Chain = N->getOperand(0);
   1274     SDValue Target = N->getOperand(1);
   1275     unsigned Opc = Target.getValueType() == MVT::i32 ? PPC::MTCTR : PPC::MTCTR8;
   1276     unsigned Reg = Target.getValueType() == MVT::i32 ? PPC::BCTR : PPC::BCTR8;
   1277     Chain = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Glue, Target,
   1278                                            Chain), 0);
   1279     return CurDAG->SelectNodeTo(N, Reg, MVT::Other, Chain);
   1280   }
   1281   case PPCISD::TOC_ENTRY: {
   1282     assert (PPCSubTarget.isPPC64() && "Only supported for 64-bit ABI");
   1283 
   1284     // For medium and large code model, we generate two instructions as
   1285     // described below.  Otherwise we allow SelectCodeCommon to handle this,
   1286     // selecting one of LDtoc, LDtocJTI, and LDtocCPT.
   1287     CodeModel::Model CModel = TM.getCodeModel();
   1288     if (CModel != CodeModel::Medium && CModel != CodeModel::Large)
   1289       break;
   1290 
   1291     // The first source operand is a TargetGlobalAddress or a
   1292     // TargetJumpTable.  If it is an externally defined symbol, a symbol
   1293     // with common linkage, a function address, or a jump table address,
   1294     // or if we are generating code for large code model, we generate:
   1295     //   LDtocL(<ga:@sym>, ADDIStocHA(%X2, <ga:@sym>))
   1296     // Otherwise we generate:
   1297     //   ADDItocL(ADDIStocHA(%X2, <ga:@sym>), <ga:@sym>)
   1298     SDValue GA = N->getOperand(0);
   1299     SDValue TOCbase = N->getOperand(1);
   1300     SDNode *Tmp = CurDAG->getMachineNode(PPC::ADDIStocHA, dl, MVT::i64,
   1301                                         TOCbase, GA);
   1302 
   1303     if (isa<JumpTableSDNode>(GA) || CModel == CodeModel::Large)
   1304       return CurDAG->getMachineNode(PPC::LDtocL, dl, MVT::i64, GA,
   1305                                     SDValue(Tmp, 0));
   1306 
   1307     if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(GA)) {
   1308       const GlobalValue *GValue = G->getGlobal();
   1309       const GlobalAlias *GAlias = dyn_cast<GlobalAlias>(GValue);
   1310       const GlobalValue *RealGValue = GAlias ?
   1311         GAlias->resolveAliasedGlobal(false) : GValue;
   1312       const GlobalVariable *GVar = dyn_cast<GlobalVariable>(RealGValue);
   1313       assert((GVar || isa<Function>(RealGValue)) &&
   1314              "Unexpected global value subclass!");
   1315 
   1316       // An external variable is one without an initializer.  For these,
   1317       // for variables with common linkage, and for Functions, generate
   1318       // the LDtocL form.
   1319       if (!GVar || !GVar->hasInitializer() || RealGValue->hasCommonLinkage() ||
   1320           RealGValue->hasAvailableExternallyLinkage())
   1321         return CurDAG->getMachineNode(PPC::LDtocL, dl, MVT::i64, GA,
   1322                                       SDValue(Tmp, 0));
   1323     }
   1324 
   1325     return CurDAG->getMachineNode(PPC::ADDItocL, dl, MVT::i64,
   1326                                   SDValue(Tmp, 0), GA);
   1327   }
   1328   case PPCISD::VADD_SPLAT: {
   1329     // This expands into one of three sequences, depending on whether
   1330     // the first operand is odd or even, positive or negative.
   1331     assert(isa<ConstantSDNode>(N->getOperand(0)) &&
   1332            isa<ConstantSDNode>(N->getOperand(1)) &&
   1333            "Invalid operand on VADD_SPLAT!");
   1334 
   1335     int Elt     = N->getConstantOperandVal(0);
   1336     int EltSize = N->getConstantOperandVal(1);
   1337     unsigned Opc1, Opc2, Opc3;
   1338     EVT VT;
   1339 
   1340     if (EltSize == 1) {
   1341       Opc1 = PPC::VSPLTISB;
   1342       Opc2 = PPC::VADDUBM;
   1343       Opc3 = PPC::VSUBUBM;
   1344       VT = MVT::v16i8;
   1345     } else if (EltSize == 2) {
   1346       Opc1 = PPC::VSPLTISH;
   1347       Opc2 = PPC::VADDUHM;
   1348       Opc3 = PPC::VSUBUHM;
   1349       VT = MVT::v8i16;
   1350     } else {
   1351       assert(EltSize == 4 && "Invalid element size on VADD_SPLAT!");
   1352       Opc1 = PPC::VSPLTISW;
   1353       Opc2 = PPC::VADDUWM;
   1354       Opc3 = PPC::VSUBUWM;
   1355       VT = MVT::v4i32;
   1356     }
   1357 
   1358     if ((Elt & 1) == 0) {
   1359       // Elt is even, in the range [-32,-18] + [16,30].
   1360       //
   1361       // Convert: VADD_SPLAT elt, size
   1362       // Into:    tmp = VSPLTIS[BHW] elt
   1363       //          VADDU[BHW]M tmp, tmp
   1364       // Where:   [BHW] = B for size = 1, H for size = 2, W for size = 4
   1365       SDValue EltVal = getI32Imm(Elt >> 1);
   1366       SDNode *Tmp = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
   1367       SDValue TmpVal = SDValue(Tmp, 0);
   1368       return CurDAG->getMachineNode(Opc2, dl, VT, TmpVal, TmpVal);
   1369 
   1370     } else if (Elt > 0) {
   1371       // Elt is odd and positive, in the range [17,31].
   1372       //
   1373       // Convert: VADD_SPLAT elt, size
   1374       // Into:    tmp1 = VSPLTIS[BHW] elt-16
   1375       //          tmp2 = VSPLTIS[BHW] -16
   1376       //          VSUBU[BHW]M tmp1, tmp2
   1377       SDValue EltVal = getI32Imm(Elt - 16);
   1378       SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
   1379       EltVal = getI32Imm(-16);
   1380       SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
   1381       return CurDAG->getMachineNode(Opc3, dl, VT, SDValue(Tmp1, 0),
   1382                                     SDValue(Tmp2, 0));
   1383 
   1384     } else {
   1385       // Elt is odd and negative, in the range [-31,-17].
   1386       //
   1387       // Convert: VADD_SPLAT elt, size
   1388       // Into:    tmp1 = VSPLTIS[BHW] elt+16
   1389       //          tmp2 = VSPLTIS[BHW] -16
   1390       //          VADDU[BHW]M tmp1, tmp2
   1391       SDValue EltVal = getI32Imm(Elt + 16);
   1392       SDNode *Tmp1 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
   1393       EltVal = getI32Imm(-16);
   1394       SDNode *Tmp2 = CurDAG->getMachineNode(Opc1, dl, VT, EltVal);
   1395       return CurDAG->getMachineNode(Opc2, dl, VT, SDValue(Tmp1, 0),
   1396                                     SDValue(Tmp2, 0));
   1397     }
   1398   }
   1399   }
   1400 
   1401   return SelectCode(N);
   1402 }
   1403 
   1404 /// PostProcessISelDAG - Perform some late peephole optimizations
   1405 /// on the DAG representation.
   1406 void PPCDAGToDAGISel::PostprocessISelDAG() {
   1407 
   1408   // Skip peepholes at -O0.
   1409   if (TM.getOptLevel() == CodeGenOpt::None)
   1410     return;
   1411 
   1412   // These optimizations are currently supported only for 64-bit SVR4.
   1413   if (PPCSubTarget.isDarwin() || !PPCSubTarget.isPPC64())
   1414     return;
   1415 
   1416   SelectionDAG::allnodes_iterator Position(CurDAG->getRoot().getNode());
   1417   ++Position;
   1418 
   1419   while (Position != CurDAG->allnodes_begin()) {
   1420     SDNode *N = --Position;
   1421     // Skip dead nodes and any non-machine opcodes.
   1422     if (N->use_empty() || !N->isMachineOpcode())
   1423       continue;
   1424 
   1425     unsigned FirstOp;
   1426     unsigned StorageOpcode = N->getMachineOpcode();
   1427 
   1428     switch (StorageOpcode) {
   1429     default: continue;
   1430 
   1431     case PPC::LBZ:
   1432     case PPC::LBZ8:
   1433     case PPC::LD:
   1434     case PPC::LFD:
   1435     case PPC::LFS:
   1436     case PPC::LHA:
   1437     case PPC::LHA8:
   1438     case PPC::LHZ:
   1439     case PPC::LHZ8:
   1440     case PPC::LWA:
   1441     case PPC::LWZ:
   1442     case PPC::LWZ8:
   1443       FirstOp = 0;
   1444       break;
   1445 
   1446     case PPC::STB:
   1447     case PPC::STB8:
   1448     case PPC::STD:
   1449     case PPC::STFD:
   1450     case PPC::STFS:
   1451     case PPC::STH:
   1452     case PPC::STH8:
   1453     case PPC::STW:
   1454     case PPC::STW8:
   1455       FirstOp = 1;
   1456       break;
   1457     }
   1458 
   1459     // If this is a load or store with a zero offset, we may be able to
   1460     // fold an add-immediate into the memory operation.
   1461     if (!isa<ConstantSDNode>(N->getOperand(FirstOp)) ||
   1462         N->getConstantOperandVal(FirstOp) != 0)
   1463       continue;
   1464 
   1465     SDValue Base = N->getOperand(FirstOp + 1);
   1466     if (!Base.isMachineOpcode())
   1467       continue;
   1468 
   1469     unsigned Flags = 0;
   1470     bool ReplaceFlags = true;
   1471 
   1472     // When the feeding operation is an add-immediate of some sort,
   1473     // determine whether we need to add relocation information to the
   1474     // target flags on the immediate operand when we fold it into the
   1475     // load instruction.
   1476     //
   1477     // For something like ADDItocL, the relocation information is
   1478     // inferred from the opcode; when we process it in the AsmPrinter,
   1479     // we add the necessary relocation there.  A load, though, can receive
   1480     // relocation from various flavors of ADDIxxx, so we need to carry
   1481     // the relocation information in the target flags.
   1482     switch (Base.getMachineOpcode()) {
   1483     default: continue;
   1484 
   1485     case PPC::ADDI8:
   1486     case PPC::ADDI8L:
   1487     case PPC::ADDIL:
   1488       // In some cases (such as TLS) the relocation information
   1489       // is already in place on the operand, so copying the operand
   1490       // is sufficient.
   1491       ReplaceFlags = false;
   1492       // For these cases, the immediate may not be divisible by 4, in
   1493       // which case the fold is illegal for DS-form instructions.  (The
   1494       // other cases provide aligned addresses and are always safe.)
   1495       if ((StorageOpcode == PPC::LWA ||
   1496            StorageOpcode == PPC::LD  ||
   1497            StorageOpcode == PPC::STD) &&
   1498           (!isa<ConstantSDNode>(Base.getOperand(1)) ||
   1499            Base.getConstantOperandVal(1) % 4 != 0))
   1500         continue;
   1501       break;
   1502     case PPC::ADDIdtprelL:
   1503       Flags = PPCII::MO_DTPREL16_LO;
   1504       break;
   1505     case PPC::ADDItlsldL:
   1506       Flags = PPCII::MO_TLSLD16_LO;
   1507       break;
   1508     case PPC::ADDItocL:
   1509       Flags = PPCII::MO_TOC16_LO;
   1510       break;
   1511     }
   1512 
   1513     // We found an opportunity.  Reverse the operands from the add
   1514     // immediate and substitute them into the load or store.  If
   1515     // needed, update the target flags for the immediate operand to
   1516     // reflect the necessary relocation information.
   1517     DEBUG(dbgs() << "Folding add-immediate into mem-op:\nBase:    ");
   1518     DEBUG(Base->dump(CurDAG));
   1519     DEBUG(dbgs() << "\nN: ");
   1520     DEBUG(N->dump(CurDAG));
   1521     DEBUG(dbgs() << "\n");
   1522 
   1523     SDValue ImmOpnd = Base.getOperand(1);
   1524 
   1525     // If the relocation information isn't already present on the
   1526     // immediate operand, add it now.
   1527     if (ReplaceFlags) {
   1528       if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(ImmOpnd)) {
   1529         DebugLoc dl = GA->getDebugLoc();
   1530         const GlobalValue *GV = GA->getGlobal();
   1531         ImmOpnd = CurDAG->getTargetGlobalAddress(GV, dl, MVT::i64, 0, Flags);
   1532       } else if (ConstantPoolSDNode *CP =
   1533                  dyn_cast<ConstantPoolSDNode>(ImmOpnd)) {
   1534         const Constant *C = CP->getConstVal();
   1535         ImmOpnd = CurDAG->getTargetConstantPool(C, MVT::i64,
   1536                                                 CP->getAlignment(),
   1537                                                 0, Flags);
   1538       }
   1539     }
   1540 
   1541     if (FirstOp == 1) // Store
   1542       (void)CurDAG->UpdateNodeOperands(N, N->getOperand(0), ImmOpnd,
   1543                                        Base.getOperand(0), N->getOperand(3));
   1544     else // Load
   1545       (void)CurDAG->UpdateNodeOperands(N, ImmOpnd, Base.getOperand(0),
   1546                                        N->getOperand(2));
   1547 
   1548     // The add-immediate may now be dead, in which case remove it.
   1549     if (Base.getNode()->use_empty())
   1550       CurDAG->RemoveDeadNode(Base.getNode());
   1551   }
   1552 }
   1553 
   1554 
   1555 /// createPPCISelDag - This pass converts a legalized DAG into a
   1556 /// PowerPC-specific DAG, ready for instruction scheduling.
   1557 ///
   1558 FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) {
   1559   return new PPCDAGToDAGISel(TM);
   1560 }
   1561 
   1562 static void initializePassOnce(PassRegistry &Registry) {
   1563   const char *Name = "PowerPC DAG->DAG Pattern Instruction Selection";
   1564   PassInfo *PI = new PassInfo(Name, "ppc-codegen", &SelectionDAGISel::ID, 0,
   1565                               false, false);
   1566   Registry.registerPass(*PI, true);
   1567 }
   1568 
   1569 void llvm::initializePPCDAGToDAGISelPass(PassRegistry &Registry) {
   1570   CALL_ONCE_INITIALIZATION(initializePassOnce);
   1571 }
   1572 
   1573