LLVM API Documentation

PPCISelDAGToDAG.cpp

Go to the documentation of this file.
00001 //===-- PPCISelDAGToDAG.cpp - PPC --pattern matching inst selector --------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines a pattern matching instruction selector for PowerPC,
00011 // converting from a legalized dag to a PPC dag.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #define DEBUG_TYPE "ppc-codegen"
00016 #include "PPC.h"
00017 #include "PPCPredicates.h"
00018 #include "PPCTargetMachine.h"
00019 #include "PPCISelLowering.h"
00020 #include "PPCHazardRecognizers.h"
00021 #include "llvm/CodeGen/MachineInstrBuilder.h"
00022 #include "llvm/CodeGen/MachineFunction.h"
00023 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
00024 #include "llvm/CodeGen/MachineRegisterInfo.h"
00025 #include "llvm/CodeGen/SelectionDAG.h"
00026 #include "llvm/CodeGen/SelectionDAGISel.h"
00027 #include "llvm/Target/TargetOptions.h"
00028 #include "llvm/Constants.h"
00029 #include "llvm/Function.h"
00030 #include "llvm/GlobalValue.h"
00031 #include "llvm/Intrinsics.h"
00032 #include "llvm/Support/Debug.h"
00033 #include "llvm/Support/MathExtras.h"
00034 #include "llvm/Support/Compiler.h"
00035 #include "llvm/Support/ErrorHandling.h"
00036 #include "llvm/Support/raw_ostream.h"
00037 using namespace llvm;
00038 
00039 namespace {
00040   //===--------------------------------------------------------------------===//
00041   /// PPCDAGToDAGISel - PPC specific code to select PPC machine
00042   /// instructions for SelectionDAG operations.
00043   ///
00044   class VISIBILITY_HIDDEN PPCDAGToDAGISel : public SelectionDAGISel {
00045     PPCTargetMachine &TM;
00046     PPCTargetLowering &PPCLowering;
00047     const PPCSubtarget &PPCSubTarget;
00048     unsigned GlobalBaseReg;
00049   public:
00050     explicit PPCDAGToDAGISel(PPCTargetMachine &tm)
00051       : SelectionDAGISel(tm), TM(tm),
00052         PPCLowering(*TM.getTargetLowering()),
00053         PPCSubTarget(*TM.getSubtargetImpl()) {}
00054     
00055     virtual bool runOnMachineFunction(MachineFunction &MF) {
00056       // Make sure we re-emit a set of the global base reg if necessary
00057       GlobalBaseReg = 0;
00058       SelectionDAGISel::runOnMachineFunction(MF);
00059       
00060       InsertVRSaveCode(MF);
00061       return true;
00062     }
00063    
00064     /// getI32Imm - Return a target constant with the specified value, of type
00065     /// i32.
00066     inline SDValue getI32Imm(unsigned Imm) {
00067       return CurDAG->getTargetConstant(Imm, MVT::i32);
00068     }
00069 
00070     /// getI64Imm - Return a target constant with the specified value, of type
00071     /// i64.
00072     inline SDValue getI64Imm(uint64_t Imm) {
00073       return CurDAG->getTargetConstant(Imm, MVT::i64);
00074     }
00075     
00076     /// getSmallIPtrImm - Return a target constant of pointer type.
00077     inline SDValue getSmallIPtrImm(unsigned Imm) {
00078       return CurDAG->getTargetConstant(Imm, PPCLowering.getPointerTy());
00079     }
00080     
00081     /// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s 
00082     /// with any number of 0s on either side.  The 1s are allowed to wrap from
00083     /// LSB to MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs.
00084     /// 0x0F0F0000 is not, since all 1s are not contiguous.
00085     static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME);
00086 
00087 
00088     /// isRotateAndMask - Returns true if Mask and Shift can be folded into a
00089     /// rotate and mask opcode and mask operation.
00090     static bool isRotateAndMask(SDNode *N, unsigned Mask, bool IsShiftMask,
00091                                 unsigned &SH, unsigned &MB, unsigned &ME);
00092     
00093     /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
00094     /// base register.  Return the virtual register that holds this value.
00095     SDNode *getGlobalBaseReg();
00096     
00097     // Select - Convert the specified operand from a target-independent to a
00098     // target-specific node if it hasn't already been changed.
00099     SDNode *Select(SDValue Op);
00100     
00101     SDNode *SelectBitfieldInsert(SDNode *N);
00102 
00103     /// SelectCC - Select a comparison of the specified values with the
00104     /// specified condition code, returning the CR# of the expression.
00105     SDValue SelectCC(SDValue LHS, SDValue RHS, ISD::CondCode CC, DebugLoc dl);
00106 
00107     /// SelectAddrImm - Returns true if the address N can be represented by
00108     /// a base register plus a signed 16-bit displacement [r+imm].
00109     bool SelectAddrImm(SDValue Op, SDValue N, SDValue &Disp,
00110                        SDValue &Base) {
00111       return PPCLowering.SelectAddressRegImm(N, Disp, Base, *CurDAG);
00112     }
00113     
00114     /// SelectAddrImmOffs - Return true if the operand is valid for a preinc
00115     /// immediate field.  Because preinc imms have already been validated, just
00116     /// accept it.
00117     bool SelectAddrImmOffs(SDValue Op, SDValue N, SDValue &Out) const {
00118       Out = N;
00119       return true;
00120     }
00121       
00122     /// SelectAddrIdx - Given the specified addressed, check to see if it can be
00123     /// represented as an indexed [r+r] operation.  Returns false if it can
00124     /// be represented by [r+imm], which are preferred.
00125     bool SelectAddrIdx(SDValue Op, SDValue N, SDValue &Base,
00126                        SDValue &Index) {
00127       return PPCLowering.SelectAddressRegReg(N, Base, Index, *CurDAG);
00128     }
00129     
00130     /// SelectAddrIdxOnly - Given the specified addressed, force it to be
00131     /// represented as an indexed [r+r] operation.
00132     bool SelectAddrIdxOnly(SDValue Op, SDValue N, SDValue &Base,
00133                            SDValue &Index) {
00134       return PPCLowering.SelectAddressRegRegOnly(N, Base, Index, *CurDAG);
00135     }
00136 
00137     /// SelectAddrImmShift - Returns true if the address N can be represented by
00138     /// a base register plus a signed 14-bit displacement [r+imm*4].  Suitable
00139     /// for use by STD and friends.
00140     bool SelectAddrImmShift(SDValue Op, SDValue N, SDValue &Disp,
00141                             SDValue &Base) {
00142       return PPCLowering.SelectAddressRegImmShift(N, Disp, Base, *CurDAG);
00143     }
00144       
00145     /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
00146     /// inline asm expressions.  It is always correct to compute the value into
00147     /// a register.  The case of adding a (possibly relocatable) constant to a
00148     /// register can be improved, but it is wrong to substitute Reg+Reg for
00149     /// Reg in an asm, because the load or store opcode would have to change.
00150    virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
00151                                               char ConstraintCode,
00152                                               std::vector<SDValue> &OutOps) {
00153       OutOps.push_back(Op);
00154       return false;
00155     }
00156     
00157     SDValue BuildSDIVSequence(SDNode *N);
00158     SDValue BuildUDIVSequence(SDNode *N);
00159     
00160     /// InstructionSelect - This callback is invoked by
00161     /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
00162     virtual void InstructionSelect();
00163     
00164     void InsertVRSaveCode(MachineFunction &MF);
00165 
00166     virtual const char *getPassName() const {
00167       return "PowerPC DAG->DAG Pattern Instruction Selection";
00168     } 
00169     
00170     /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
00171     /// this target when scheduling the DAG.
00172     virtual ScheduleHazardRecognizer *CreateTargetHazardRecognizer() {
00173       // Should use subtarget info to pick the right hazard recognizer.  For
00174       // now, always return a PPC970 recognizer.
00175       const TargetInstrInfo *II = TM.getInstrInfo();
00176       assert(II && "No InstrInfo?");
00177       return new PPCHazardRecognizer970(*II); 
00178     }
00179 
00180 // Include the pieces autogenerated from the target description.
00181 #include "PPCGenDAGISel.inc"
00182     
00183 private:
00184     SDNode *SelectSETCC(SDValue Op);
00185   };
00186 }
00187 
00188 /// InstructionSelect - This callback is invoked by
00189 /// SelectionDAGISel when it has created a SelectionDAG for us to codegen.
00190 void PPCDAGToDAGISel::InstructionSelect() {
00191   DEBUG(BB->dump());
00192 
00193   // Select target instructions for the DAG.
00194   SelectRoot(*CurDAG);
00195   CurDAG->RemoveDeadNodes();
00196 }
00197 
00198 /// InsertVRSaveCode - Once the entire function has been instruction selected,
00199 /// all virtual registers are created and all machine instructions are built,
00200 /// check to see if we need to save/restore VRSAVE.  If so, do it.
00201 void PPCDAGToDAGISel::InsertVRSaveCode(MachineFunction &Fn) {
00202   // Check to see if this function uses vector registers, which means we have to
00203   // save and restore the VRSAVE register and update it with the regs we use.  
00204   //
00205   // In this case, there will be virtual registers of vector type type created
00206   // by the scheduler.  Detect them now.
00207   bool HasVectorVReg = false;
00208   for (unsigned i = TargetRegisterInfo::FirstVirtualRegister, 
00209        e = RegInfo->getLastVirtReg()+1; i != e; ++i)
00210     if (RegInfo->getRegClass(i) == &PPC::VRRCRegClass) {
00211       HasVectorVReg = true;
00212       break;
00213     }
00214   if (!HasVectorVReg) return;  // nothing to do.
00215       
00216   // If we have a vector register, we want to emit code into the entry and exit
00217   // blocks to save and restore the VRSAVE register.  We do this here (instead
00218   // of marking all vector instructions as clobbering VRSAVE) for two reasons:
00219   //
00220   // 1. This (trivially) reduces the load on the register allocator, by not
00221   //    having to represent the live range of the VRSAVE register.
00222   // 2. This (more significantly) allows us to create a temporary virtual
00223   //    register to hold the saved VRSAVE value, allowing this temporary to be
00224   //    register allocated, instead of forcing it to be spilled to the stack.
00225 
00226   // Create two vregs - one to hold the VRSAVE register that is live-in to the
00227   // function and one for the value after having bits or'd into it.
00228   unsigned InVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
00229   unsigned UpdatedVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
00230   
00231   const TargetInstrInfo &TII = *TM.getInstrInfo();
00232   MachineBasicBlock &EntryBB = *Fn.begin();
00233   DebugLoc dl = DebugLoc::getUnknownLoc();
00234   // Emit the following code into the entry block:
00235   // InVRSAVE = MFVRSAVE
00236   // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE
00237   // MTVRSAVE UpdatedVRSAVE
00238   MachineBasicBlock::iterator IP = EntryBB.begin();  // Insert Point
00239   BuildMI(EntryBB, IP, dl, TII.get(PPC::MFVRSAVE), InVRSAVE);
00240   BuildMI(EntryBB, IP, dl, TII.get(PPC::UPDATE_VRSAVE),
00241           UpdatedVRSAVE).addReg(InVRSAVE);
00242   BuildMI(EntryBB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(UpdatedVRSAVE);
00243   
00244   // Find all return blocks, outputting a restore in each epilog.
00245   for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
00246     if (!BB->empty() && BB->back().getDesc().isReturn()) {
00247       IP = BB->end(); --IP;
00248       
00249       // Skip over all terminator instructions, which are part of the return
00250       // sequence.
00251       MachineBasicBlock::iterator I2 = IP;
00252       while (I2 != BB->begin() && (--I2)->getDesc().isTerminator())
00253         IP = I2;
00254       
00255       // Emit: MTVRSAVE InVRSave
00256       BuildMI(*BB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(InVRSAVE);
00257     }        
00258   }
00259 }
00260 
00261 
00262 /// getGlobalBaseReg - Output the instructions required to put the
00263 /// base address to use for accessing globals into a register.
00264 ///
00265 SDNode *PPCDAGToDAGISel::getGlobalBaseReg() {
00266   if (!GlobalBaseReg) {
00267     const TargetInstrInfo &TII = *TM.getInstrInfo();
00268     // Insert the set of GlobalBaseReg into the first MBB of the function
00269     MachineBasicBlock &FirstMBB = MF->front();
00270     MachineBasicBlock::iterator MBBI = FirstMBB.begin();
00271     DebugLoc dl = DebugLoc::getUnknownLoc();
00272 
00273     if (PPCLowering.getPointerTy() == MVT::i32) {
00274       GlobalBaseReg = RegInfo->createVirtualRegister(PPC::GPRCRegisterClass);
00275       BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR), PPC::LR);
00276       BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
00277     } else {
00278       GlobalBaseReg = RegInfo->createVirtualRegister(PPC::G8RCRegisterClass);
00279       BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR8), PPC::LR8);
00280       BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR8), GlobalBaseReg);
00281     }
00282   }
00283   return CurDAG->getRegister(GlobalBaseReg,
00284                              PPCLowering.getPointerTy()).getNode();
00285 }
00286 
00287 /// isIntS16Immediate - This method tests to see if the node is either a 32-bit
00288 /// or 64-bit immediate, and if the value can be accurately represented as a
00289 /// sign extension from a 16-bit value.  If so, this returns true and the
00290 /// immediate.
00291 static bool isIntS16Immediate(SDNode *N, short &Imm) {
00292   if (N->getOpcode() != ISD::Constant)
00293     return false;
00294 
00295   Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
00296   if (N->getValueType(0) == MVT::i32)
00297     return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
00298   else
00299     return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
00300 }
00301 
00302 static bool isIntS16Immediate(SDValue Op, short &Imm) {
00303   return isIntS16Immediate(Op.getNode(), Imm);
00304 }
00305 
00306 
00307 /// isInt32Immediate - This method tests to see if the node is a 32-bit constant
00308 /// operand. If so Imm will receive the 32-bit value.
00309 static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
00310   if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
00311     Imm = cast<ConstantSDNode>(N)->getZExtValue();
00312     return true;
00313   }
00314   return false;
00315 }
00316 
00317 /// isInt64Immediate - This method tests to see if the node is a 64-bit constant
00318 /// operand.  If so Imm will receive the 64-bit value.
00319 static bool isInt64Immediate(SDNode *N, uint64_t &Imm) {
00320   if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i64) {
00321     Imm = cast<ConstantSDNode>(N)->getZExtValue();
00322     return true;
00323   }
00324   return false;
00325 }
00326 
00327 // isInt32Immediate - This method tests to see if a constant operand.
00328 // If so Imm will receive the 32 bit value.
00329 static bool isInt32Immediate(SDValue N, unsigned &Imm) {
00330   return isInt32Immediate(N.getNode(), Imm);
00331 }
00332 
00333 
00334 // isOpcWithIntImmediate - This method tests to see if the node is a specific
00335 // opcode and that it has a immediate integer right operand.
00336 // If so Imm will receive the 32 bit value.
00337 static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
00338   return N->getOpcode() == Opc
00339          && isInt32Immediate(N->getOperand(1).getNode(), Imm);
00340 }
00341 
00342 bool PPCDAGToDAGISel::isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
00343   if (isShiftedMask_32(Val)) {
00344     // look for the first non-zero bit
00345     MB = CountLeadingZeros_32(Val);
00346     // look for the first zero bit after the run of ones
00347     ME = CountLeadingZeros_32((Val - 1) ^ Val);
00348     return true;
00349   } else {
00350     Val = ~Val; // invert mask
00351     if (isShiftedMask_32(Val)) {
00352       // effectively look for the first zero bit
00353       ME = CountLeadingZeros_32(Val) - 1;
00354       // effectively look for the first one bit after the run of zeros
00355       MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1;
00356       return true;
00357     }
00358   }
00359   // no run present
00360   return false;
00361 }
00362 
00363 bool PPCDAGToDAGISel::isRotateAndMask(SDNode *N, unsigned Mask, 
00364                                       bool IsShiftMask, unsigned &SH, 
00365                                       unsigned &MB, unsigned &ME) {
00366   // Don't even go down this path for i64, since different logic will be
00367   // necessary for rldicl/rldicr/rldimi.
00368   if (N->getValueType(0) != MVT::i32)
00369     return false;
00370 
00371   unsigned Shift  = 32;
00372   unsigned Indeterminant = ~0;  // bit mask marking indeterminant results
00373   unsigned Opcode = N->getOpcode();
00374   if (N->getNumOperands() != 2 ||
00375       !isInt32Immediate(N->getOperand(1).getNode(), Shift) || (Shift > 31))
00376     return false;
00377   
00378   if (Opcode == ISD::SHL) {
00379     // apply shift left to mask if it comes first
00380     if (IsShiftMask) Mask = Mask << Shift;
00381     // determine which bits are made indeterminant by shift
00382     Indeterminant = ~(0xFFFFFFFFu << Shift);
00383   } else if (Opcode == ISD::SRL) { 
00384     // apply shift right to mask if it comes first
00385     if (IsShiftMask) Mask = Mask >> Shift;
00386     // determine which bits are made indeterminant by shift
00387     Indeterminant = ~(0xFFFFFFFFu >> Shift);
00388     // adjust for the left rotate
00389     Shift = 32 - Shift;
00390   } else if (Opcode == ISD::ROTL) {
00391     Indeterminant = 0;
00392   } else {
00393     return false;
00394   }
00395   
00396   // if the mask doesn't intersect any Indeterminant bits
00397   if (Mask && !(Mask & Indeterminant)) {
00398     SH = Shift & 31;
00399     // make sure the mask is still a mask (wrap arounds may not be)
00400     return isRunOfOnes(Mask, MB, ME);
00401   }
00402   return false;
00403 }
00404 
00405 /// SelectBitfieldInsert - turn an or of two masked values into
00406 /// the rotate left word immediate then mask insert (rlwimi) instruction.
00407 SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
00408   SDValue Op0 = N->getOperand(0);
00409   SDValue Op1 = N->getOperand(1);
00410   DebugLoc dl = N->getDebugLoc();
00411   
00412   APInt LKZ, LKO, RKZ, RKO;
00413   CurDAG->ComputeMaskedBits(Op0, APInt::getAllOnesValue(32), LKZ, LKO);
00414   CurDAG->ComputeMaskedBits(Op1, APInt::getAllOnesValue(32), RKZ, RKO);
00415   
00416   unsigned TargetMask = LKZ.getZExtValue();
00417   unsigned InsertMask = RKZ.getZExtValue();
00418   
00419   if ((TargetMask | InsertMask) == 0xFFFFFFFF) {
00420     unsigned Op0Opc = Op0.getOpcode();
00421     unsigned Op1Opc = Op1.getOpcode();
00422     unsigned Value, SH = 0;
00423     TargetMask = ~TargetMask;
00424     InsertMask = ~InsertMask;
00425 
00426     // If the LHS has a foldable shift and the RHS does not, then swap it to the
00427     // RHS so that we can fold the shift into the insert.
00428     if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
00429       if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
00430           Op0.getOperand(0).getOpcode() == ISD::SRL) {
00431         if (Op1.getOperand(0).getOpcode() != ISD::SHL &&
00432             Op1.getOperand(0).getOpcode() != ISD::SRL) {
00433           std::swap(Op0, Op1);
00434           std::swap(Op0Opc, Op1Opc);
00435           std::swap(TargetMask, InsertMask);
00436         }
00437       }
00438     } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) {
00439       if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL &&
00440           Op1.getOperand(0).getOpcode() != ISD::SRL) {
00441         std::swap(Op0, Op1);
00442         std::swap(Op0Opc, Op1Opc);
00443         std::swap(TargetMask, InsertMask);
00444       }
00445     }
00446     
00447     unsigned MB, ME;
00448     if (InsertMask && isRunOfOnes(InsertMask, MB, ME)) {
00449       SDValue Tmp1, Tmp2, Tmp3;
00450       bool DisjointMask = (TargetMask ^ InsertMask) == 0xFFFFFFFF;
00451 
00452       if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) &&
00453           isInt32Immediate(Op1.getOperand(1), Value)) {
00454         Op1 = Op1.getOperand(0);
00455         SH  = (Op1Opc == ISD::SHL) ? Value : 32 - Value;
00456       }
00457       if (Op1Opc == ISD::AND) {
00458         unsigned SHOpc = Op1.getOperand(0).getOpcode();
00459         if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) &&
00460             isInt32Immediate(Op1.getOperand(0).getOperand(1), Value)) {
00461           Op1 = Op1.getOperand(0).getOperand(0);
00462           SH  = (SHOpc == ISD::SHL) ? Value : 32 - Value;
00463         } else {
00464           Op1 = Op1.getOperand(0);
00465         }
00466       }
00467       
00468       Tmp3 = (Op0Opc == ISD::AND && DisjointMask) ? Op0.getOperand(0) : Op0;
00469       SH &= 31;
00470       SDValue Ops[] = { Tmp3, Op1, getI32Imm(SH), getI32Imm(MB),
00471                           getI32Imm(ME) };
00472       return CurDAG->getTargetNode(PPC::RLWIMI, dl, MVT::i32, Ops, 5);
00473     }
00474   }
00475   return 0;
00476 }
00477 
00478 /// SelectCC - Select a comparison of the specified values with the specified
00479 /// condition code, returning the CR# of the expression.
00480 SDValue PPCDAGToDAGISel::SelectCC(SDValue LHS, SDValue RHS,
00481                                     ISD::CondCode CC, DebugLoc dl) {
00482   // Always select the LHS.
00483   unsigned Opc;
00484   
00485   if (LHS.getValueType() == MVT::i32) {
00486     unsigned Imm;
00487     if (CC == ISD::SETEQ || CC == ISD::SETNE) {
00488       if (isInt32Immediate(RHS, Imm)) {
00489         // SETEQ/SETNE comparison with 16-bit immediate, fold it.
00490         if (isUInt16(Imm))
00491           return SDValue(CurDAG->getTargetNode(PPC::CMPLWI, dl, MVT::i32, LHS,
00492                                                  getI32Imm(Imm & 0xFFFF)), 0);
00493         // If this is a 16-bit signed immediate, fold it.
00494         if (isInt16((int)Imm))
00495           return SDValue(CurDAG->getTargetNode(PPC::CMPWI, dl, MVT::i32, LHS,
00496                                                  getI32Imm(Imm & 0xFFFF)), 0);
00497         
00498         // For non-equality comparisons, the default code would materialize the
00499         // constant, then compare against it, like this:
00500         //   lis r2, 4660
00501         //   ori r2, r2, 22136 
00502         //   cmpw cr0, r3, r2
00503         // Since we are just comparing for equality, we can emit this instead:
00504         //   xoris r0,r3,0x1234
00505         //   cmplwi cr0,r0,0x5678
00506         //   beq cr0,L6
00507         SDValue Xor(CurDAG->getTargetNode(PPC::XORIS, dl, MVT::i32, LHS,
00508                                             getI32Imm(Imm >> 16)), 0);
00509         return SDValue(CurDAG->getTargetNode(PPC::CMPLWI, dl, MVT::i32, Xor,
00510                                                getI32Imm(Imm & 0xFFFF)), 0);
00511       }
00512       Opc = PPC::CMPLW;
00513     } else if (ISD::isUnsignedIntSetCC(CC)) {
00514       if (isInt32Immediate(RHS, Imm) && isUInt16(Imm))
00515         return SDValue(CurDAG->getTargetNode(PPC::CMPLWI, dl, MVT::i32, LHS,
00516                                                getI32Imm(Imm & 0xFFFF)), 0);
00517       Opc = PPC::CMPLW;
00518     } else {
00519       short SImm;
00520       if (isIntS16Immediate(RHS, SImm))
00521         return SDValue(CurDAG->getTargetNode(PPC::CMPWI, dl, MVT::i32, LHS,
00522                                                getI32Imm((int)SImm & 0xFFFF)),
00523                          0);
00524       Opc = PPC::CMPW;
00525     }
00526   } else if (LHS.getValueType() == MVT::i64) {
00527     uint64_t Imm;
00528     if (CC == ISD::SETEQ || CC == ISD::SETNE) {
00529       if (isInt64Immediate(RHS.getNode(), Imm)) {
00530         // SETEQ/SETNE comparison with 16-bit immediate, fold it.
00531         if (isUInt16(Imm))
00532           return SDValue(CurDAG->getTargetNode(PPC::CMPLDI, dl, MVT::i64, LHS,
00533                                                  getI32Imm(Imm & 0xFFFF)), 0);
00534         // If this is a 16-bit signed immediate, fold it.
00535         if (isInt16(Imm))
00536           return SDValue(CurDAG->getTargetNode(PPC::CMPDI, dl, MVT::i64, LHS,
00537                                                  getI32Imm(Imm & 0xFFFF)), 0);
00538         
00539         // For non-equality comparisons, the default code would materialize the
00540         // constant, then compare against it, like this:
00541         //   lis r2, 4660
00542         //   ori r2, r2, 22136 
00543         //   cmpd cr0, r3, r2
00544         // Since we are just comparing for equality, we can emit this instead:
00545         //   xoris r0,r3,0x1234
00546         //   cmpldi cr0,r0,0x5678
00547         //   beq cr0,L6
00548         if (isUInt32(Imm)) {
00549           SDValue Xor(CurDAG->getTargetNode(PPC::XORIS8, dl, MVT::i64, LHS,
00550                                               getI64Imm(Imm >> 16)), 0);
00551           return SDValue(CurDAG->getTargetNode(PPC::CMPLDI, dl, MVT::i64, Xor,
00552                                                  getI64Imm(Imm & 0xFFFF)), 0);
00553         }
00554       }
00555       Opc = PPC::CMPLD;
00556     } else if (ISD::isUnsignedIntSetCC(CC)) {
00557       if (isInt64Immediate(RHS.getNode(), Imm) && isUInt16(Imm))
00558         return SDValue(CurDAG->getTargetNode(PPC::CMPLDI, dl, MVT::i64, LHS,
00559                                                getI64Imm(Imm & 0xFFFF)), 0);
00560       Opc = PPC::CMPLD;
00561     } else {
00562       short SImm;
00563       if (isIntS16Immediate(RHS, SImm))
00564         return SDValue(CurDAG->getTargetNode(PPC::CMPDI, dl, MVT::i64, LHS,
00565                                                getI64Imm(SImm & 0xFFFF)),
00566                          0);
00567       Opc = PPC::CMPD;
00568     }
00569   } else if (LHS.getValueType() == MVT::f32) {
00570     Opc = PPC::FCMPUS;
00571   } else {
00572     assert(LHS.getValueType() == MVT::f64 && "Unknown vt!");
00573     Opc = PPC::FCMPUD;
00574   }
00575   return SDValue(CurDAG->getTargetNode(Opc, dl, MVT::i32, LHS, RHS), 0);
00576 }
00577 
00578 static PPC::Predicate getPredicateForSetCC(ISD::CondCode CC) {
00579   switch (CC) {
00580   case ISD::SETUEQ:
00581   case ISD::SETONE:
00582   case ISD::SETOLE:
00583   case ISD::SETOGE:
00584     llvm_unreachable("Should be lowered by legalize!");
00585   default: llvm_unreachable("Unknown condition!");
00586   case ISD::SETOEQ:
00587   case ISD::SETEQ:  return PPC::PRED_EQ;
00588   case ISD::SETUNE:
00589   case ISD::SETNE:  return PPC::PRED_NE;
00590   case ISD::SETOLT:
00591   case ISD::SETLT:  return PPC::PRED_LT;
00592   case ISD::SETULE:
00593   case ISD::SETLE:  return PPC::PRED_LE;
00594   case ISD::SETOGT:
00595   case ISD::SETGT:  return PPC::PRED_GT;
00596   case ISD::SETUGE:
00597   case ISD::SETGE:  return PPC::PRED_GE;
00598   case ISD::SETO:   return PPC::PRED_NU;
00599   case ISD::SETUO:  return PPC::PRED_UN;
00600     // These two are invalid for floating point.  Assume we have int.
00601   case ISD::SETULT: return PPC::PRED_LT;
00602   case ISD::SETUGT: return PPC::PRED_GT;
00603   }
00604 }
00605 
00606 /// getCRIdxForSetCC - Return the index of the condition register field
00607 /// associated with the SetCC condition, and whether or not the field is
00608 /// treated as inverted.  That is, lt = 0; ge = 0 inverted.
00609 ///
00610 /// If this returns with Other != -1, then the returned comparison is an or of
00611 /// two simpler comparisons.  In this case, Invert is guaranteed to be false.
00612 static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool &Invert, int &Other) {
00613   Invert = false;
00614   Other = -1;
00615   switch (CC) {
00616   default: llvm_unreachable("Unknown condition!");
00617   case ISD::SETOLT:
00618   case ISD::SETLT:  return 0;                  // Bit #0 = SETOLT
00619   case ISD::SETOGT:
00620   case ISD::SETGT:  return 1;                  // Bit #1 = SETOGT
00621   case ISD::SETOEQ:
00622   case ISD::SETEQ:  return 2;                  // Bit #2 = SETOEQ
00623   case ISD::SETUO:  return 3;                  // Bit #3 = SETUO
00624   case ISD::SETUGE:
00625   case ISD::SETGE:  Invert = true; return 0;   // !Bit #0 = SETUGE
00626   case ISD::SETULE:
00627   case ISD::SETLE:  Invert = true; return 1;   // !Bit #1 = SETULE
00628   case ISD::SETUNE:
00629   case ISD::SETNE:  Invert = true; return 2;   // !Bit #2 = SETUNE
00630   case ISD::SETO:   Invert = true; return 3;   // !Bit #3 = SETO
00631   case ISD::SETUEQ: 
00632   case ISD::SETOGE: 
00633   case ISD::SETOLE: 
00634   case ISD::SETONE:
00635     llvm_unreachable("Invalid branch code: should be expanded by legalize");
00636   // These are invalid for floating point.  Assume integer.
00637   case ISD::SETULT: return 0;
00638   case ISD::SETUGT: return 1;
00639   }
00640   return 0;
00641 }
00642 
00643 SDNode *PPCDAGToDAGISel::SelectSETCC(SDValue Op) {
00644   SDNode *N = Op.getNode();
00645   DebugLoc dl = N->getDebugLoc();
00646   unsigned Imm;
00647   ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
00648   if (isInt32Immediate(N->getOperand(1), Imm)) {
00649     // We can codegen setcc op, imm very efficiently compared to a brcond.
00650     // Check for those cases here.
00651     // setcc op, 0
00652     if (Imm == 0) {
00653       SDValue Op = N->getOperand(0);
00654       switch (CC) {
00655       default: break;
00656       case ISD::SETEQ: {
00657         Op = SDValue(CurDAG->getTargetNode(PPC::CNTLZW, dl, MVT::i32, Op), 0);
00658         SDValue Ops[] = { Op, getI32Imm(27), getI32Imm(5), getI32Imm(31) };
00659         return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
00660       }
00661       case ISD::SETNE: {
00662         SDValue AD =
00663           SDValue(CurDAG->getTargetNode(PPC::ADDIC, dl, MVT::i32, MVT::Flag,
00664                                           Op, getI32Imm(~0U)), 0);
00665         return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op, 
00666                                     AD.getValue(1));
00667       }
00668       case ISD::SETLT: {
00669         SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
00670         return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
00671       }
00672       case ISD::SETGT: {
00673         SDValue T =
00674           SDValue(CurDAG->getTargetNode(PPC::NEG, dl, MVT::i32, Op), 0);
00675         T = SDValue(CurDAG->getTargetNode(PPC::ANDC, dl, MVT::i32, T, Op), 0);
00676         SDValue Ops[] = { T, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
00677         return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
00678       }
00679       }
00680     } else if (Imm == ~0U) {        // setcc op, -1
00681       SDValue Op = N->getOperand(0);
00682       switch (CC) {
00683       default: break;
00684       case ISD::SETEQ:
00685         Op = SDValue(CurDAG->getTargetNode(PPC::ADDIC, dl, MVT::i32, MVT::Flag,
00686                                              Op, getI32Imm(1)), 0);
00687         return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32, 
00688                               SDValue(CurDAG->getTargetNode(PPC::LI, dl, 
00689                                                             MVT::i32,
00690                                                             getI32Imm(0)), 0),
00691                                       Op.getValue(1));
00692       case ISD::SETNE: {
00693         Op = SDValue(CurDAG->getTargetNode(PPC::NOR, dl, MVT::i32, Op, Op), 0);
00694         SDNode *AD = CurDAG->getTargetNode(PPC::ADDIC, dl, MVT::i32, MVT::Flag,
00695                                            Op, getI32Imm(~0U));
00696         return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDValue(AD, 0),
00697                                     Op, SDValue(AD, 1));
00698       }
00699       case ISD::SETLT: {
00700         SDValue AD = SDValue(CurDAG->getTargetNode(PPC::ADDI, dl, MVT::i32, Op,
00701                                                        getI32Imm(1)), 0);
00702         SDValue AN = SDValue(CurDAG->getTargetNode(PPC::AND, dl, MVT::i32, AD,
00703                                                        Op), 0);
00704         SDValue Ops[] = { AN, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
00705         return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
00706       }
00707       case ISD::SETGT: {
00708         SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
00709         Op = SDValue(CurDAG->getTargetNode(PPC::RLWINM, dl, MVT::i32, Ops, 4), 
00710                      0);
00711         return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op, 
00712                                     getI32Imm(1));
00713       }
00714       }
00715     }
00716   }
00717   
00718   bool Inv;
00719   int OtherCondIdx;
00720   unsigned Idx = getCRIdxForSetCC(CC, Inv, OtherCondIdx);
00721   SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC, dl);
00722   SDValue IntCR;
00723   
00724   // Force the ccreg into CR7.
00725   SDValue CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
00726   
00727   SDValue InFlag(0, 0);  // Null incoming flag value.
00728   CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, CR7Reg, CCReg, 
00729                                InFlag).getValue(1);
00730   
00731   if (PPCSubTarget.isGigaProcessor() && OtherCondIdx == -1)
00732     IntCR = SDValue(CurDAG->getTargetNode(PPC::MFOCRF, dl, MVT::i32, CR7Reg,
00733                                             CCReg), 0);
00734   else
00735     IntCR = SDValue(CurDAG->getTargetNode(PPC::MFCR, dl, MVT::i32, CCReg), 0);
00736   
00737   SDValue Ops[] = { IntCR, getI32Imm((32-(3-Idx)) & 31),
00738                       getI32Imm(31), getI32Imm(31) };
00739   if (OtherCondIdx == -1 && !Inv)
00740     return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
00741 
00742   // Get the specified bit.
00743   SDValue Tmp =
00744     SDValue(CurDAG->getTargetNode(PPC::RLWINM, dl, MVT::i32, Ops, 4), 0);
00745   if (Inv) {
00746     assert(OtherCondIdx == -1 && "Can't have split plus negation");
00747     return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1));
00748   }
00749 
00750   // Otherwise, we have to turn an operation like SETONE -> SETOLT | SETOGT.
00751   // We already got the bit for the first part of the comparison (e.g. SETULE).
00752 
00753   // Get the other bit of the comparison.
00754   Ops[1] = getI32Imm((32-(3-OtherCondIdx)) & 31);
00755   SDValue OtherCond = 
00756     SDValue(CurDAG->getTargetNode(PPC::RLWINM, dl, MVT::i32, Ops, 4), 0);
00757 
00758   return CurDAG->SelectNodeTo(N, PPC::OR, MVT::i32, Tmp, OtherCond);
00759 }
00760 
00761 
00762 // Select - Convert the specified operand from a target-independent to a
00763 // target-specific node if it hasn't already been changed.
00764 SDNode *PPCDAGToDAGISel::Select(SDValue Op) {
00765   SDNode *N = Op.getNode();
00766   DebugLoc dl = Op.getDebugLoc();
00767   if (N->isMachineOpcode())
00768     return NULL;   // Already selected.
00769 
00770   switch (N->getOpcode()) {
00771   default: break;
00772   
00773   case ISD::Constant: {
00774     if (N->getValueType(0) == MVT::i64) {
00775       // Get 64 bit value.
00776       int64_t Imm = cast<ConstantSDNode>(N)->getZExtValue();
00777       // Assume no remaining bits.
00778       unsigned Remainder = 0;
00779       // Assume no shift required.
00780       unsigned Shift = 0;
00781       
00782       // If it can't be represented as a 32 bit value.
00783       if (!isInt32(Imm)) {
00784         Shift = CountTrailingZeros_64(Imm);
00785         int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
00786         
00787         // If the shifted value fits 32 bits.
00788         if (isInt32(ImmSh)) {
00789           // Go with the shifted value.
00790           Imm = ImmSh;
00791         } else {
00792           // Still stuck with a 64 bit value.
00793           Remainder = Imm;
00794           Shift = 32;
00795           Imm >>= 32;
00796         }
00797       }
00798       
00799       // Intermediate operand.
00800       SDNode *Result;
00801 
00802       // Handle first 32 bits.
00803       unsigned Lo = Imm & 0xFFFF;
00804       unsigned Hi = (Imm >> 16) & 0xFFFF;
00805       
00806       // Simple value.
00807       if (isInt16(Imm)) {
00808        // Just the Lo bits.
00809         Result = CurDAG->getTargetNode(PPC::LI8, dl, MVT::i64, getI32Imm(Lo));
00810       } else if (Lo) {
00811         // Handle the Hi bits.
00812         unsigned OpC = Hi ? PPC::LIS8 : PPC::LI8;
00813         Result = CurDAG->getTargetNode(OpC, dl, MVT::i64, getI32Imm(Hi));
00814         // And Lo bits.
00815         Result = CurDAG->getTargetNode(PPC::ORI8, dl, MVT::i64,
00816                                        SDValue(Result, 0), getI32Imm(Lo));
00817       } else {
00818        // Just the Hi bits.
00819         Result = CurDAG->getTargetNode(PPC::LIS8, dl, MVT::i64, getI32Imm(Hi));
00820       }
00821       
00822       // If no shift, we're done.
00823       if (!Shift) return Result;
00824 
00825       // Shift for next step if the upper 32-bits were not zero.
00826       if (Imm) {
00827         Result = CurDAG->getTargetNode(PPC::RLDICR, dl, MVT::i64,
00828                                        SDValue(Result, 0),
00829                                        getI32Imm(Shift), getI32Imm(63 - Shift));
00830       }
00831 
00832       // Add in the last bits as required.
00833       if ((Hi = (Remainder >> 16) & 0xFFFF)) {
00834         Result = CurDAG->getTargetNode(PPC::ORIS8, dl, MVT::i64,
00835                                        SDValue(Result, 0), getI32Imm(Hi));
00836       } 
00837       if ((Lo = Remainder & 0xFFFF)) {
00838         Result = CurDAG->getTargetNode(PPC::ORI8, dl, MVT::i64,
00839                                        SDValue(Result, 0), getI32Imm(Lo));
00840       }
00841       
00842       return Result;
00843     }
00844     break;
00845   }
00846   
00847   case ISD::SETCC:
00848     return SelectSETCC(Op);
00849   case PPCISD::GlobalBaseReg:
00850     return getGlobalBaseReg();
00851     
00852   case ISD::FrameIndex: {
00853     int FI = cast<FrameIndexSDNode>(N)->getIndex();
00854     SDValue TFI = CurDAG->getTargetFrameIndex(FI, Op.getValueType());
00855     unsigned Opc = Op.getValueType() == MVT::i32 ? PPC::ADDI : PPC::ADDI8;
00856     if (N->hasOneUse())
00857       return CurDAG->SelectNodeTo(N, Opc, Op.getValueType(), TFI,
00858                                   getSmallIPtrImm(0));
00859     return CurDAG->getTargetNode(Opc, dl, Op.getValueType(), TFI,
00860                                  getSmallIPtrImm(0));
00861   }
00862 
00863   case PPCISD::MFCR: {
00864     SDValue InFlag = N->getOperand(1);
00865     // Use MFOCRF if supported.
00866     if (PPCSubTarget.isGigaProcessor())
00867       return CurDAG->getTargetNode(PPC::MFOCRF, dl, MVT::i32,
00868                                    N->getOperand(0), InFlag);
00869     else
00870       return CurDAG->getTargetNode(PPC::MFCR, dl, MVT::i32, InFlag);
00871   }
00872     
00873   case ISD::SDIV: {
00874     // FIXME: since this depends on the setting of the carry flag from the srawi
00875     //        we should really be making notes about that for the scheduler.
00876     // FIXME: It sure would be nice if we could cheaply recognize the 
00877     //        srl/add/sra pattern the dag combiner will generate for this as
00878     //        sra/addze rather than having to handle sdiv ourselves.  oh well.
00879     unsigned Imm;
00880     if (isInt32Immediate(N->getOperand(1), Imm)) {
00881       SDValue N0 = N->getOperand(0);
00882       if ((signed)Imm > 0 && isPowerOf2_32(Imm)) {
00883         SDNode *Op =
00884           CurDAG->getTargetNode(PPC::SRAWI, dl, MVT::i32, MVT::Flag,
00885                                 N0, getI32Imm(Log2_32(Imm)));
00886         return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32, 
00887                                     SDValue(Op, 0), SDValue(Op, 1));
00888       } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) {
00889         SDNode *Op =
00890           CurDAG->getTargetNode(PPC::SRAWI, dl, MVT::i32, MVT::Flag,
00891                                 N0, getI32Imm(Log2_32(-Imm)));
00892         SDValue PT =
00893           SDValue(CurDAG->getTargetNode(PPC::ADDZE, dl, MVT::i32,
00894                                           SDValue(Op, 0), SDValue(Op, 1)),
00895                     0);
00896         return CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT);
00897       }
00898     }
00899     
00900     // Other cases are autogenerated.
00901     break;
00902   }
00903     
00904   case ISD::LOAD: {
00905     // Handle preincrement loads.
00906     LoadSDNode *LD = cast<LoadSDNode>(Op);
00907     EVT LoadedVT = LD->getMemoryVT();
00908     
00909     // Normal loads are handled by code generated from the .td file.
00910     if (LD->getAddressingMode() != ISD::PRE_INC)
00911       break;
00912     
00913     SDValue Offset = LD->getOffset();
00914     if (isa<ConstantSDNode>(Offset) ||
00915         Offset.getOpcode() == ISD::TargetGlobalAddress) {
00916       
00917       unsigned Opcode;
00918       bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
00919       if (LD->getValueType(0) != MVT::i64) {
00920         // Handle PPC32 integer and normal FP loads.
00921         assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
00922         switch (LoadedVT.getSimpleVT().SimpleTy) {
00923           default: llvm_unreachable("Invalid PPC load type!");
00924           case MVT::f64: Opcode = PPC::LFDU; break;
00925           case MVT::f32: Opcode = PPC::LFSU; break;
00926           case MVT::i32: Opcode = PPC::LWZU; break;
00927           case MVT::i16: Opcode = isSExt ? PPC::LHAU : PPC::LHZU; break;
00928           case MVT::i1:
00929           case MVT::i8:  Opcode = PPC::LBZU; break;
00930         }
00931       } else {
00932         assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
00933         assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
00934         switch (LoadedVT.getSimpleVT().SimpleTy) {
00935           default: llvm_unreachable("Invalid PPC load type!");
00936           case MVT::i64: Opcode = PPC::LDU; break;
00937           case MVT::i32: Opcode = PPC::LWZU8; break;
00938           case MVT::i16: Opcode = isSExt ? PPC::LHAU8 : PPC::LHZU8; break;
00939           case MVT::i1:
00940           case MVT::i8:  Opcode = PPC::LBZU8; break;
00941         }
00942       }
00943       
00944       SDValue Chain = LD->getChain();
00945       SDValue Base = LD->getBasePtr();
00946       SDValue Ops[] = { Offset, Base, Chain };
00947       // FIXME: PPC64
00948       return CurDAG->getTargetNode(Opcode, dl, LD->getValueType(0),
00949                                    PPCLowering.getPointerTy(),
00950                                    MVT::Other, Ops, 3);
00951     } else {
00952       llvm_unreachable("R+R preindex loads not supported yet!");
00953     }
00954   }
00955     
00956   case ISD::AND: {
00957     unsigned Imm, Imm2, SH, MB, ME;
00958 
00959     // If this is an and of a value rotated between 0 and 31 bits and then and'd
00960     // with a mask, emit rlwinm
00961     if (isInt32Immediate(N->getOperand(1), Imm) &&
00962         isRotateAndMask(N->getOperand(0).getNode(), Imm, false, SH, MB, ME)) {
00963       SDValue Val = N->getOperand(0).getOperand(0);
00964       SDValue Ops[] = { Val, getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
00965       return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
00966     }
00967     // If this is just a masked value where the input is not handled above, and
00968     // is not a rotate-left (handled by a pattern in the .td file), emit rlwinm
00969     if (isInt32Immediate(N->getOperand(1), Imm) &&
00970         isRunOfOnes(Imm, MB, ME) && 
00971         N->getOperand(0).getOpcode() != ISD::ROTL) {
00972       SDValue Val = N->getOperand(0);
00973       SDValue Ops[] = { Val, getI32Imm(0), getI32Imm(MB), getI32Imm(ME) };
00974       return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
00975     }
00976     // AND X, 0 -> 0, not "rlwinm 32".
00977     if (isInt32Immediate(N->getOperand(1), Imm) && (Imm == 0)) {
00978       ReplaceUses(SDValue(N, 0), N->getOperand(1));
00979       return NULL;
00980     }
00981     // ISD::OR doesn't get all the bitfield insertion fun.
00982     // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) is a bitfield insert
00983     if (isInt32Immediate(N->getOperand(1), Imm) && 
00984         N->getOperand(0).getOpcode() == ISD::OR &&
00985         isInt32Immediate(N->getOperand(0).getOperand(1), Imm2)) {
00986       unsigned MB, ME;
00987       Imm = ~(Imm^Imm2);
00988       if (isRunOfOnes(Imm, MB, ME)) {
00989         SDValue Ops[] = { N->getOperand(0).getOperand(0),
00990                             N->getOperand(0).getOperand(1),
00991                             getI32Imm(0), getI32Imm(MB),getI32Imm(ME) };
00992         return CurDAG->getTargetNode(PPC::RLWIMI, dl, MVT::i32, Ops, 5);
00993       }
00994     }
00995     
00996     // Other cases are autogenerated.
00997     break;
00998   }
00999   case ISD::OR:
01000     if (N->getValueType(0) == MVT::i32)
01001       if (SDNode *I = SelectBitfieldInsert(N))
01002         return I;
01003       
01004     // Other cases are autogenerated.
01005     break;
01006   case ISD::SHL: {
01007     unsigned Imm, SH, MB, ME;
01008     if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
01009         isRotateAndMask(N, Imm, true, SH, MB, ME)) {
01010       SDValue Ops[] = { N->getOperand(0).getOperand(0),
01011                           getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
01012       return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
01013     }
01014     
01015     // Other cases are autogenerated.
01016     break;
01017   }
01018   case ISD::SRL: {
01019     unsigned Imm, SH, MB, ME;
01020     if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
01021         isRotateAndMask(N, Imm, true, SH, MB, ME)) { 
01022       SDValue Ops[] = { N->getOperand(0).getOperand(0),
01023                           getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
01024       return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
01025     }
01026     
01027     // Other cases are autogenerated.
01028     break;
01029   }
01030   case ISD::SELECT_CC: {
01031     ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
01032     
01033     // Handle the setcc cases here.  select_cc lhs, 0, 1, 0, cc
01034     if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
01035       if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
01036         if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
01037           if (N1C->isNullValue() && N3C->isNullValue() &&
01038               N2C->getZExtValue() == 1ULL && CC == ISD::SETNE &&
01039               // FIXME: Implement this optzn for PPC64.
01040               N->getValueType(0) == MVT::i32) {
01041             SDNode *Tmp =
01042               CurDAG->getTargetNode(PPC::ADDIC, dl, MVT::i32, MVT::Flag,
01043                                     N->getOperand(0), getI32Imm(~0U));
01044             return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32,
01045                                         SDValue(Tmp, 0), N->getOperand(0),
01046                                         SDValue(Tmp, 1));
01047           }
01048 
01049     SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC, dl);
01050     unsigned BROpc = getPredicateForSetCC(CC);
01051 
01052     unsigned SelectCCOp;
01053     if (N->getValueType(0) == MVT::i32)
01054       SelectCCOp = PPC::SELECT_CC_I4;
01055     else if (N->getValueType(0) == MVT::i64)
01056       SelectCCOp = PPC::SELECT_CC_I8;
01057     else if (N->getValueType(0) == MVT::f32)
01058       SelectCCOp = PPC::SELECT_CC_F4;
01059     else if (N->getValueType(0) == MVT::f64)
01060       SelectCCOp = PPC::SELECT_CC_F8;
01061     else
01062       SelectCCOp = PPC::SELECT_CC_VRRC;
01063 
01064     SDValue Ops[] = { CCReg, N->getOperand(2), N->getOperand(3),
01065                         getI32Imm(BROpc) };
01066     return CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), Ops, 4);
01067   }
01068   case PPCISD::COND_BRANCH: {
01069     // Op #0 is the Chain.
01070     // Op #1 is the PPC::PRED_* number.
01071     // Op #2 is the CR#
01072     // Op #3 is the Dest MBB
01073     // Op #4 is the Flag.
01074     // Prevent PPC::PRED_* from being selected into LI.
01075     SDValue Pred =
01076       getI32Imm(cast<ConstantSDNode>(N->getOperand(1))->getZExtValue());
01077     SDValue Ops[] = { Pred, N->getOperand(2), N->getOperand(3),
01078       N->getOperand(0), N->getOperand(4) };
01079     return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops, 5);
01080   }
01081   case ISD::BR_CC: {
01082     ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
01083     SDValue CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC, dl);
01084     SDValue Ops[] = { getI32Imm(getPredicateForSetCC(CC)), CondCode, 
01085                         N->getOperand(4), N->getOperand(0) };
01086     return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops, 4);
01087   }
01088   case ISD::BRIND: {
01089     // FIXME: Should custom lower this.
01090     SDValue Chain = N->getOperand(0);
01091     SDValue Target = N->getOperand(1);
01092     unsigned Opc = Target.getValueType() == MVT::i32 ? PPC::MTCTR : PPC::MTCTR8;
01093     Chain = SDValue(CurDAG->getTargetNode(Opc, dl, MVT::Other, Target,
01094                                             Chain), 0);
01095     return CurDAG->SelectNodeTo(N, PPC::BCTR, MVT::Other, Chain);
01096   }
01097   case ISD::DECLARE: {
01098     SDValue Chain = N->getOperand(0);
01099     SDValue N1 = N->getOperand(1);
01100     SDValue N2 = N->getOperand(2);
01101     FrameIndexSDNode *FINode = dyn_cast<FrameIndexSDNode>(N1);
01102     
01103     // FIXME: We need to handle this for VLAs.
01104     if (!FINode) {
01105       ReplaceUses(Op.getValue(0), Chain);
01106       return NULL;
01107     }
01108     
01109     if (N2.getOpcode() == ISD::ADD) {
01110       if (N2.getOperand(0).getOpcode() == ISD::ADD &&
01111           N2.getOperand(0).getOperand(0).getOpcode() == PPCISD::GlobalBaseReg &&
01112           N2.getOperand(0).getOperand(1).getOpcode() == PPCISD::Hi &&
01113           N2.getOperand(1).getOpcode() == PPCISD::Lo)
01114         N2 = N2.getOperand(0).getOperand(1).getOperand(0);
01115       else if (N2.getOperand(0).getOpcode() == ISD::ADD &&
01116           N2.getOperand(0).getOperand(0).getOpcode() == PPCISD::GlobalBaseReg &&
01117           N2.getOperand(0).getOperand(1).getOpcode() == PPCISD::Lo &&
01118                N2.getOperand(1).getOpcode() == PPCISD::Hi)
01119         N2 = N2.getOperand(0).getOperand(1).getOperand(0);
01120       else if (N2.getOperand(0).getOpcode() == PPCISD::Hi &&
01121                N2.getOperand(1).getOpcode() == PPCISD::Lo)
01122         N2 = N2.getOperand(0).getOperand(0);
01123     }
01124     
01125     // If we don't have a global address here, the debug info is mangled, just
01126     // drop it.
01127     if (!isa<GlobalAddressSDNode>(N2)) {
01128       ReplaceUses(Op.getValue(0), Chain);
01129       return NULL;
01130     }
01131     int FI = cast<FrameIndexSDNode>(N1)->getIndex();
01132     GlobalValue *GV = cast<GlobalAddressSDNode>(N2)->getGlobal();
01133     SDValue Tmp1 = CurDAG->getTargetFrameIndex(FI, TLI.getPointerTy());
01134     SDValue Tmp2 = CurDAG->getTargetGlobalAddress(GV, TLI.getPointerTy());
01135     return CurDAG->SelectNodeTo(N, TargetInstrInfo::DECLARE,
01136                                 MVT::Other, Tmp1, Tmp2, Chain);
01137   }
01138   }
01139   
01140   return SelectCode(Op);
01141 }
01142 
01143 
01144 
01145 /// createPPCISelDag - This pass converts a legalized DAG into a 
01146 /// PowerPC-specific DAG, ready for instruction scheduling.
01147 ///
01148 FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) {
01149   return new PPCDAGToDAGISel(TM);
01150 }
01151