Replace FIXME with explanatory comment.
[oota-llvm.git] / lib / Target / PowerPC / PPCISelDAGToDAG.cpp
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 "PPCTargetMachine.h"
18 #include "MCTargetDesc/PPCPredicates.h"
19 #include "llvm/CodeGen/MachineInstrBuilder.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineRegisterInfo.h"
22 #include "llvm/CodeGen/SelectionDAG.h"
23 #include "llvm/CodeGen/SelectionDAGISel.h"
24 #include "llvm/Target/TargetOptions.h"
25 #include "llvm/Constants.h"
26 #include "llvm/Function.h"
27 #include "llvm/GlobalValue.h"
28 #include "llvm/Intrinsics.h"
29 #include "llvm/Support/Debug.h"
30 #include "llvm/Support/MathExtras.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Support/raw_ostream.h"
33 using namespace llvm;
34
35 namespace {
36   //===--------------------------------------------------------------------===//
37   /// PPCDAGToDAGISel - PPC specific code to select PPC machine
38   /// instructions for SelectionDAG operations.
39   ///
40   class PPCDAGToDAGISel : public SelectionDAGISel {
41     const PPCTargetMachine &TM;
42     const PPCTargetLowering &PPCLowering;
43     const PPCSubtarget &PPCSubTarget;
44     unsigned GlobalBaseReg;
45   public:
46     explicit PPCDAGToDAGISel(PPCTargetMachine &tm)
47       : SelectionDAGISel(tm), TM(tm),
48         PPCLowering(*TM.getTargetLowering()),
49         PPCSubTarget(*TM.getSubtargetImpl()) {}
50
51     virtual bool runOnMachineFunction(MachineFunction &MF) {
52       // Make sure we re-emit a set of the global base reg if necessary
53       GlobalBaseReg = 0;
54       SelectionDAGISel::runOnMachineFunction(MF);
55
56       InsertVRSaveCode(MF);
57       return true;
58     }
59
60     /// getI32Imm - Return a target constant with the specified value, of type
61     /// i32.
62     inline SDValue getI32Imm(unsigned Imm) {
63       return CurDAG->getTargetConstant(Imm, MVT::i32);
64     }
65
66     /// getI64Imm - Return a target constant with the specified value, of type
67     /// i64.
68     inline SDValue getI64Imm(uint64_t Imm) {
69       return CurDAG->getTargetConstant(Imm, MVT::i64);
70     }
71
72     /// getSmallIPtrImm - Return a target constant of pointer type.
73     inline SDValue getSmallIPtrImm(unsigned Imm) {
74       return CurDAG->getTargetConstant(Imm, PPCLowering.getPointerTy());
75     }
76
77     /// isRunOfOnes - Returns true iff Val consists of one contiguous run of 1s
78     /// with any number of 0s on either side.  The 1s are allowed to wrap from
79     /// LSB to MSB, so 0x000FFF0, 0x0000FFFF, and 0xFF0000FF are all runs.
80     /// 0x0F0F0000 is not, since all 1s are not contiguous.
81     static bool isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME);
82
83
84     /// isRotateAndMask - Returns true if Mask and Shift can be folded into a
85     /// rotate and mask opcode and mask operation.
86     static bool isRotateAndMask(SDNode *N, unsigned Mask, bool isShiftMask,
87                                 unsigned &SH, unsigned &MB, unsigned &ME);
88
89     /// getGlobalBaseReg - insert code into the entry mbb to materialize the PIC
90     /// base register.  Return the virtual register that holds this value.
91     SDNode *getGlobalBaseReg();
92
93     // Select - Convert the specified operand from a target-independent to a
94     // target-specific node if it hasn't already been changed.
95     SDNode *Select(SDNode *N);
96
97     SDNode *SelectBitfieldInsert(SDNode *N);
98
99     /// SelectCC - Select a comparison of the specified values with the
100     /// specified condition code, returning the CR# of the expression.
101     SDValue SelectCC(SDValue LHS, SDValue RHS, ISD::CondCode CC, DebugLoc dl);
102
103     /// SelectAddrImm - Returns true if the address N can be represented by
104     /// a base register plus a signed 16-bit displacement [r+imm].
105     bool SelectAddrImm(SDValue N, SDValue &Disp,
106                        SDValue &Base) {
107       return PPCLowering.SelectAddressRegImm(N, Disp, Base, *CurDAG);
108     }
109
110     /// SelectAddrImmOffs - Return true if the operand is valid for a preinc
111     /// immediate field.  Because preinc imms have already been validated, just
112     /// accept it.
113     bool SelectAddrImmOffs(SDValue N, SDValue &Out) const {
114       Out = N;
115       return true;
116     }
117
118     /// SelectAddrIdx - Given the specified addressed, check to see if it can be
119     /// represented as an indexed [r+r] operation.  Returns false if it can
120     /// be represented by [r+imm], which are preferred.
121     bool SelectAddrIdx(SDValue N, SDValue &Base, SDValue &Index) {
122       return PPCLowering.SelectAddressRegReg(N, Base, Index, *CurDAG);
123     }
124
125     /// SelectAddrIdxOnly - Given the specified addressed, force it to be
126     /// represented as an indexed [r+r] operation.
127     bool SelectAddrIdxOnly(SDValue N, SDValue &Base, SDValue &Index) {
128       return PPCLowering.SelectAddressRegRegOnly(N, Base, Index, *CurDAG);
129     }
130
131     /// SelectAddrImmShift - Returns true if the address N can be represented by
132     /// a base register plus a signed 14-bit displacement [r+imm*4].  Suitable
133     /// for use by STD and friends.
134     bool SelectAddrImmShift(SDValue N, SDValue &Disp, SDValue &Base) {
135       return PPCLowering.SelectAddressRegImmShift(N, Disp, Base, *CurDAG);
136     }
137
138     /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
139     /// inline asm expressions.  It is always correct to compute the value into
140     /// a register.  The case of adding a (possibly relocatable) constant to a
141     /// register can be improved, but it is wrong to substitute Reg+Reg for
142     /// Reg in an asm, because the load or store opcode would have to change.
143    virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op,
144                                               char ConstraintCode,
145                                               std::vector<SDValue> &OutOps) {
146       OutOps.push_back(Op);
147       return false;
148     }
149
150     void InsertVRSaveCode(MachineFunction &MF);
151
152     virtual const char *getPassName() const {
153       return "PowerPC DAG->DAG Pattern Instruction Selection";
154     }
155
156 // Include the pieces autogenerated from the target description.
157 #include "PPCGenDAGISel.inc"
158
159 private:
160     SDNode *SelectSETCC(SDNode *N);
161   };
162 }
163
164 /// InsertVRSaveCode - Once the entire function has been instruction selected,
165 /// all virtual registers are created and all machine instructions are built,
166 /// check to see if we need to save/restore VRSAVE.  If so, do it.
167 void PPCDAGToDAGISel::InsertVRSaveCode(MachineFunction &Fn) {
168   // Check to see if this function uses vector registers, which means we have to
169   // save and restore the VRSAVE register and update it with the regs we use.
170   //
171   // In this case, there will be virtual registers of vector type created
172   // by the scheduler.  Detect them now.
173   bool HasVectorVReg = false;
174   for (unsigned i = 0, e = RegInfo->getNumVirtRegs(); i != e; ++i) {
175     unsigned Reg = TargetRegisterInfo::index2VirtReg(i);
176     if (RegInfo->getRegClass(Reg) == &PPC::VRRCRegClass) {
177       HasVectorVReg = true;
178       break;
179     }
180   }
181   if (!HasVectorVReg) return;  // nothing to do.
182
183   // If we have a vector register, we want to emit code into the entry and exit
184   // blocks to save and restore the VRSAVE register.  We do this here (instead
185   // of marking all vector instructions as clobbering VRSAVE) for two reasons:
186   //
187   // 1. This (trivially) reduces the load on the register allocator, by not
188   //    having to represent the live range of the VRSAVE register.
189   // 2. This (more significantly) allows us to create a temporary virtual
190   //    register to hold the saved VRSAVE value, allowing this temporary to be
191   //    register allocated, instead of forcing it to be spilled to the stack.
192
193   // Create two vregs - one to hold the VRSAVE register that is live-in to the
194   // function and one for the value after having bits or'd into it.
195   unsigned InVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
196   unsigned UpdatedVRSAVE = RegInfo->createVirtualRegister(&PPC::GPRCRegClass);
197
198   const TargetInstrInfo &TII = *TM.getInstrInfo();
199   MachineBasicBlock &EntryBB = *Fn.begin();
200   DebugLoc dl;
201   // Emit the following code into the entry block:
202   // InVRSAVE = MFVRSAVE
203   // UpdatedVRSAVE = UPDATE_VRSAVE InVRSAVE
204   // MTVRSAVE UpdatedVRSAVE
205   MachineBasicBlock::iterator IP = EntryBB.begin();  // Insert Point
206   BuildMI(EntryBB, IP, dl, TII.get(PPC::MFVRSAVE), InVRSAVE);
207   BuildMI(EntryBB, IP, dl, TII.get(PPC::UPDATE_VRSAVE),
208           UpdatedVRSAVE).addReg(InVRSAVE);
209   BuildMI(EntryBB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(UpdatedVRSAVE);
210
211   // Find all return blocks, outputting a restore in each epilog.
212   for (MachineFunction::iterator BB = Fn.begin(), E = Fn.end(); BB != E; ++BB) {
213     if (!BB->empty() && BB->back().isReturn()) {
214       IP = BB->end(); --IP;
215
216       // Skip over all terminator instructions, which are part of the return
217       // sequence.
218       MachineBasicBlock::iterator I2 = IP;
219       while (I2 != BB->begin() && (--I2)->isTerminator())
220         IP = I2;
221
222       // Emit: MTVRSAVE InVRSave
223       BuildMI(*BB, IP, dl, TII.get(PPC::MTVRSAVE)).addReg(InVRSAVE);
224     }
225   }
226 }
227
228
229 /// getGlobalBaseReg - Output the instructions required to put the
230 /// base address to use for accessing globals into a register.
231 ///
232 SDNode *PPCDAGToDAGISel::getGlobalBaseReg() {
233   if (!GlobalBaseReg) {
234     const TargetInstrInfo &TII = *TM.getInstrInfo();
235     // Insert the set of GlobalBaseReg into the first MBB of the function
236     MachineBasicBlock &FirstMBB = MF->front();
237     MachineBasicBlock::iterator MBBI = FirstMBB.begin();
238     DebugLoc dl;
239
240     if (PPCLowering.getPointerTy() == MVT::i32) {
241       GlobalBaseReg = RegInfo->createVirtualRegister(PPC::GPRCRegisterClass);
242       BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR));
243       BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR), GlobalBaseReg);
244     } else {
245       GlobalBaseReg = RegInfo->createVirtualRegister(PPC::G8RCRegisterClass);
246       BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MovePCtoLR8));
247       BuildMI(FirstMBB, MBBI, dl, TII.get(PPC::MFLR8), GlobalBaseReg);
248     }
249   }
250   return CurDAG->getRegister(GlobalBaseReg,
251                              PPCLowering.getPointerTy()).getNode();
252 }
253
254 /// isIntS16Immediate - This method tests to see if the node is either a 32-bit
255 /// or 64-bit immediate, and if the value can be accurately represented as a
256 /// sign extension from a 16-bit value.  If so, this returns true and the
257 /// immediate.
258 static bool isIntS16Immediate(SDNode *N, short &Imm) {
259   if (N->getOpcode() != ISD::Constant)
260     return false;
261
262   Imm = (short)cast<ConstantSDNode>(N)->getZExtValue();
263   if (N->getValueType(0) == MVT::i32)
264     return Imm == (int32_t)cast<ConstantSDNode>(N)->getZExtValue();
265   else
266     return Imm == (int64_t)cast<ConstantSDNode>(N)->getZExtValue();
267 }
268
269 static bool isIntS16Immediate(SDValue Op, short &Imm) {
270   return isIntS16Immediate(Op.getNode(), Imm);
271 }
272
273
274 /// isInt32Immediate - This method tests to see if the node is a 32-bit constant
275 /// operand. If so Imm will receive the 32-bit value.
276 static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
277   if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
278     Imm = cast<ConstantSDNode>(N)->getZExtValue();
279     return true;
280   }
281   return false;
282 }
283
284 /// isInt64Immediate - This method tests to see if the node is a 64-bit constant
285 /// operand.  If so Imm will receive the 64-bit value.
286 static bool isInt64Immediate(SDNode *N, uint64_t &Imm) {
287   if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i64) {
288     Imm = cast<ConstantSDNode>(N)->getZExtValue();
289     return true;
290   }
291   return false;
292 }
293
294 // isInt32Immediate - This method tests to see if a constant operand.
295 // If so Imm will receive the 32 bit value.
296 static bool isInt32Immediate(SDValue N, unsigned &Imm) {
297   return isInt32Immediate(N.getNode(), Imm);
298 }
299
300
301 // isOpcWithIntImmediate - This method tests to see if the node is a specific
302 // opcode and that it has a immediate integer right operand.
303 // If so Imm will receive the 32 bit value.
304 static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
305   return N->getOpcode() == Opc
306          && isInt32Immediate(N->getOperand(1).getNode(), Imm);
307 }
308
309 bool PPCDAGToDAGISel::isRunOfOnes(unsigned Val, unsigned &MB, unsigned &ME) {
310   if (isShiftedMask_32(Val)) {
311     // look for the first non-zero bit
312     MB = CountLeadingZeros_32(Val);
313     // look for the first zero bit after the run of ones
314     ME = CountLeadingZeros_32((Val - 1) ^ Val);
315     return true;
316   } else {
317     Val = ~Val; // invert mask
318     if (isShiftedMask_32(Val)) {
319       // effectively look for the first zero bit
320       ME = CountLeadingZeros_32(Val) - 1;
321       // effectively look for the first one bit after the run of zeros
322       MB = CountLeadingZeros_32((Val - 1) ^ Val) + 1;
323       return true;
324     }
325   }
326   // no run present
327   return false;
328 }
329
330 bool PPCDAGToDAGISel::isRotateAndMask(SDNode *N, unsigned Mask,
331                                       bool isShiftMask, unsigned &SH,
332                                       unsigned &MB, unsigned &ME) {
333   // Don't even go down this path for i64, since different logic will be
334   // necessary for rldicl/rldicr/rldimi.
335   if (N->getValueType(0) != MVT::i32)
336     return false;
337
338   unsigned Shift  = 32;
339   unsigned Indeterminant = ~0;  // bit mask marking indeterminant results
340   unsigned Opcode = N->getOpcode();
341   if (N->getNumOperands() != 2 ||
342       !isInt32Immediate(N->getOperand(1).getNode(), Shift) || (Shift > 31))
343     return false;
344
345   if (Opcode == ISD::SHL) {
346     // apply shift left to mask if it comes first
347     if (isShiftMask) Mask = Mask << Shift;
348     // determine which bits are made indeterminant by shift
349     Indeterminant = ~(0xFFFFFFFFu << Shift);
350   } else if (Opcode == ISD::SRL) {
351     // apply shift right to mask if it comes first
352     if (isShiftMask) Mask = Mask >> Shift;
353     // determine which bits are made indeterminant by shift
354     Indeterminant = ~(0xFFFFFFFFu >> Shift);
355     // adjust for the left rotate
356     Shift = 32 - Shift;
357   } else if (Opcode == ISD::ROTL) {
358     Indeterminant = 0;
359   } else {
360     return false;
361   }
362
363   // if the mask doesn't intersect any Indeterminant bits
364   if (Mask && !(Mask & Indeterminant)) {
365     SH = Shift & 31;
366     // make sure the mask is still a mask (wrap arounds may not be)
367     return isRunOfOnes(Mask, MB, ME);
368   }
369   return false;
370 }
371
372 /// SelectBitfieldInsert - turn an or of two masked values into
373 /// the rotate left word immediate then mask insert (rlwimi) instruction.
374 SDNode *PPCDAGToDAGISel::SelectBitfieldInsert(SDNode *N) {
375   SDValue Op0 = N->getOperand(0);
376   SDValue Op1 = N->getOperand(1);
377   DebugLoc dl = N->getDebugLoc();
378
379   APInt LKZ, LKO, RKZ, RKO;
380   CurDAG->ComputeMaskedBits(Op0, APInt::getAllOnesValue(32), LKZ, LKO);
381   CurDAG->ComputeMaskedBits(Op1, APInt::getAllOnesValue(32), RKZ, RKO);
382
383   unsigned TargetMask = LKZ.getZExtValue();
384   unsigned InsertMask = RKZ.getZExtValue();
385
386   if ((TargetMask | InsertMask) == 0xFFFFFFFF) {
387     unsigned Op0Opc = Op0.getOpcode();
388     unsigned Op1Opc = Op1.getOpcode();
389     unsigned Value, SH = 0;
390     TargetMask = ~TargetMask;
391     InsertMask = ~InsertMask;
392
393     // If the LHS has a foldable shift and the RHS does not, then swap it to the
394     // RHS so that we can fold the shift into the insert.
395     if (Op0Opc == ISD::AND && Op1Opc == ISD::AND) {
396       if (Op0.getOperand(0).getOpcode() == ISD::SHL ||
397           Op0.getOperand(0).getOpcode() == ISD::SRL) {
398         if (Op1.getOperand(0).getOpcode() != ISD::SHL &&
399             Op1.getOperand(0).getOpcode() != ISD::SRL) {
400           std::swap(Op0, Op1);
401           std::swap(Op0Opc, Op1Opc);
402           std::swap(TargetMask, InsertMask);
403         }
404       }
405     } else if (Op0Opc == ISD::SHL || Op0Opc == ISD::SRL) {
406       if (Op1Opc == ISD::AND && Op1.getOperand(0).getOpcode() != ISD::SHL &&
407           Op1.getOperand(0).getOpcode() != ISD::SRL) {
408         std::swap(Op0, Op1);
409         std::swap(Op0Opc, Op1Opc);
410         std::swap(TargetMask, InsertMask);
411       }
412     }
413
414     unsigned MB, ME;
415     if (InsertMask && isRunOfOnes(InsertMask, MB, ME)) {
416       SDValue Tmp1, Tmp2;
417
418       if ((Op1Opc == ISD::SHL || Op1Opc == ISD::SRL) &&
419           isInt32Immediate(Op1.getOperand(1), Value)) {
420         Op1 = Op1.getOperand(0);
421         SH  = (Op1Opc == ISD::SHL) ? Value : 32 - Value;
422       }
423       if (Op1Opc == ISD::AND) {
424         unsigned SHOpc = Op1.getOperand(0).getOpcode();
425         if ((SHOpc == ISD::SHL || SHOpc == ISD::SRL) &&
426             isInt32Immediate(Op1.getOperand(0).getOperand(1), Value)) {
427           Op1 = Op1.getOperand(0).getOperand(0);
428           SH  = (SHOpc == ISD::SHL) ? Value : 32 - Value;
429         } else {
430           Op1 = Op1.getOperand(0);
431         }
432       }
433
434       SH &= 31;
435       SDValue Ops[] = { Op0, Op1, getI32Imm(SH), getI32Imm(MB),
436                           getI32Imm(ME) };
437       return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops, 5);
438     }
439   }
440   return 0;
441 }
442
443 /// SelectCC - Select a comparison of the specified values with the specified
444 /// condition code, returning the CR# of the expression.
445 SDValue PPCDAGToDAGISel::SelectCC(SDValue LHS, SDValue RHS,
446                                     ISD::CondCode CC, DebugLoc dl) {
447   // Always select the LHS.
448   unsigned Opc;
449
450   if (LHS.getValueType() == MVT::i32) {
451     unsigned Imm;
452     if (CC == ISD::SETEQ || CC == ISD::SETNE) {
453       if (isInt32Immediate(RHS, Imm)) {
454         // SETEQ/SETNE comparison with 16-bit immediate, fold it.
455         if (isUInt<16>(Imm))
456           return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
457                                                 getI32Imm(Imm & 0xFFFF)), 0);
458         // If this is a 16-bit signed immediate, fold it.
459         if (isInt<16>((int)Imm))
460           return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
461                                                 getI32Imm(Imm & 0xFFFF)), 0);
462
463         // For non-equality comparisons, the default code would materialize the
464         // constant, then compare against it, like this:
465         //   lis r2, 4660
466         //   ori r2, r2, 22136
467         //   cmpw cr0, r3, r2
468         // Since we are just comparing for equality, we can emit this instead:
469         //   xoris r0,r3,0x1234
470         //   cmplwi cr0,r0,0x5678
471         //   beq cr0,L6
472         SDValue Xor(CurDAG->getMachineNode(PPC::XORIS, dl, MVT::i32, LHS,
473                                            getI32Imm(Imm >> 16)), 0);
474         return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, Xor,
475                                               getI32Imm(Imm & 0xFFFF)), 0);
476       }
477       Opc = PPC::CMPLW;
478     } else if (ISD::isUnsignedIntSetCC(CC)) {
479       if (isInt32Immediate(RHS, Imm) && isUInt<16>(Imm))
480         return SDValue(CurDAG->getMachineNode(PPC::CMPLWI, dl, MVT::i32, LHS,
481                                               getI32Imm(Imm & 0xFFFF)), 0);
482       Opc = PPC::CMPLW;
483     } else {
484       short SImm;
485       if (isIntS16Immediate(RHS, SImm))
486         return SDValue(CurDAG->getMachineNode(PPC::CMPWI, dl, MVT::i32, LHS,
487                                               getI32Imm((int)SImm & 0xFFFF)),
488                          0);
489       Opc = PPC::CMPW;
490     }
491   } else if (LHS.getValueType() == MVT::i64) {
492     uint64_t Imm;
493     if (CC == ISD::SETEQ || CC == ISD::SETNE) {
494       if (isInt64Immediate(RHS.getNode(), Imm)) {
495         // SETEQ/SETNE comparison with 16-bit immediate, fold it.
496         if (isUInt<16>(Imm))
497           return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
498                                                 getI32Imm(Imm & 0xFFFF)), 0);
499         // If this is a 16-bit signed immediate, fold it.
500         if (isInt<16>(Imm))
501           return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
502                                                 getI32Imm(Imm & 0xFFFF)), 0);
503
504         // For non-equality comparisons, the default code would materialize the
505         // constant, then compare against it, like this:
506         //   lis r2, 4660
507         //   ori r2, r2, 22136
508         //   cmpd cr0, r3, r2
509         // Since we are just comparing for equality, we can emit this instead:
510         //   xoris r0,r3,0x1234
511         //   cmpldi cr0,r0,0x5678
512         //   beq cr0,L6
513         if (isUInt<32>(Imm)) {
514           SDValue Xor(CurDAG->getMachineNode(PPC::XORIS8, dl, MVT::i64, LHS,
515                                              getI64Imm(Imm >> 16)), 0);
516           return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, Xor,
517                                                 getI64Imm(Imm & 0xFFFF)), 0);
518         }
519       }
520       Opc = PPC::CMPLD;
521     } else if (ISD::isUnsignedIntSetCC(CC)) {
522       if (isInt64Immediate(RHS.getNode(), Imm) && isUInt<16>(Imm))
523         return SDValue(CurDAG->getMachineNode(PPC::CMPLDI, dl, MVT::i64, LHS,
524                                               getI64Imm(Imm & 0xFFFF)), 0);
525       Opc = PPC::CMPLD;
526     } else {
527       short SImm;
528       if (isIntS16Immediate(RHS, SImm))
529         return SDValue(CurDAG->getMachineNode(PPC::CMPDI, dl, MVT::i64, LHS,
530                                               getI64Imm(SImm & 0xFFFF)),
531                          0);
532       Opc = PPC::CMPD;
533     }
534   } else if (LHS.getValueType() == MVT::f32) {
535     Opc = PPC::FCMPUS;
536   } else {
537     assert(LHS.getValueType() == MVT::f64 && "Unknown vt!");
538     Opc = PPC::FCMPUD;
539   }
540   return SDValue(CurDAG->getMachineNode(Opc, dl, MVT::i32, LHS, RHS), 0);
541 }
542
543 static PPC::Predicate getPredicateForSetCC(ISD::CondCode CC) {
544   switch (CC) {
545   case ISD::SETUEQ:
546   case ISD::SETONE:
547   case ISD::SETOLE:
548   case ISD::SETOGE:
549     llvm_unreachable("Should be lowered by legalize!");
550   default: llvm_unreachable("Unknown condition!");
551   case ISD::SETOEQ:
552   case ISD::SETEQ:  return PPC::PRED_EQ;
553   case ISD::SETUNE:
554   case ISD::SETNE:  return PPC::PRED_NE;
555   case ISD::SETOLT:
556   case ISD::SETLT:  return PPC::PRED_LT;
557   case ISD::SETULE:
558   case ISD::SETLE:  return PPC::PRED_LE;
559   case ISD::SETOGT:
560   case ISD::SETGT:  return PPC::PRED_GT;
561   case ISD::SETUGE:
562   case ISD::SETGE:  return PPC::PRED_GE;
563   case ISD::SETO:   return PPC::PRED_NU;
564   case ISD::SETUO:  return PPC::PRED_UN;
565     // These two are invalid for floating point.  Assume we have int.
566   case ISD::SETULT: return PPC::PRED_LT;
567   case ISD::SETUGT: return PPC::PRED_GT;
568   }
569 }
570
571 /// getCRIdxForSetCC - Return the index of the condition register field
572 /// associated with the SetCC condition, and whether or not the field is
573 /// treated as inverted.  That is, lt = 0; ge = 0 inverted.
574 ///
575 /// If this returns with Other != -1, then the returned comparison is an or of
576 /// two simpler comparisons.  In this case, Invert is guaranteed to be false.
577 static unsigned getCRIdxForSetCC(ISD::CondCode CC, bool &Invert, int &Other) {
578   Invert = false;
579   Other = -1;
580   switch (CC) {
581   default: llvm_unreachable("Unknown condition!");
582   case ISD::SETOLT:
583   case ISD::SETLT:  return 0;                  // Bit #0 = SETOLT
584   case ISD::SETOGT:
585   case ISD::SETGT:  return 1;                  // Bit #1 = SETOGT
586   case ISD::SETOEQ:
587   case ISD::SETEQ:  return 2;                  // Bit #2 = SETOEQ
588   case ISD::SETUO:  return 3;                  // Bit #3 = SETUO
589   case ISD::SETUGE:
590   case ISD::SETGE:  Invert = true; return 0;   // !Bit #0 = SETUGE
591   case ISD::SETULE:
592   case ISD::SETLE:  Invert = true; return 1;   // !Bit #1 = SETULE
593   case ISD::SETUNE:
594   case ISD::SETNE:  Invert = true; return 2;   // !Bit #2 = SETUNE
595   case ISD::SETO:   Invert = true; return 3;   // !Bit #3 = SETO
596   case ISD::SETUEQ:
597   case ISD::SETOGE:
598   case ISD::SETOLE:
599   case ISD::SETONE:
600     llvm_unreachable("Invalid branch code: should be expanded by legalize");
601   // These are invalid for floating point.  Assume integer.
602   case ISD::SETULT: return 0;
603   case ISD::SETUGT: return 1;
604   }
605   return 0;
606 }
607
608 SDNode *PPCDAGToDAGISel::SelectSETCC(SDNode *N) {
609   DebugLoc dl = N->getDebugLoc();
610   unsigned Imm;
611   ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get();
612   EVT PtrVT = CurDAG->getTargetLoweringInfo().getPointerTy();
613   bool isPPC64 = (PtrVT == MVT::i64);
614
615   if (isInt32Immediate(N->getOperand(1), Imm)) {
616     // We can codegen setcc op, imm very efficiently compared to a brcond.
617     // Check for those cases here.
618     // setcc op, 0
619     if (Imm == 0) {
620       SDValue Op = N->getOperand(0);
621       switch (CC) {
622       default: break;
623       case ISD::SETEQ: {
624         Op = SDValue(CurDAG->getMachineNode(PPC::CNTLZW, dl, MVT::i32, Op), 0);
625         SDValue Ops[] = { Op, getI32Imm(27), getI32Imm(5), getI32Imm(31) };
626         return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
627       }
628       case ISD::SETNE: {
629         if (isPPC64) break;
630         SDValue AD =
631           SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
632                                          Op, getI32Imm(~0U)), 0);
633         return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, AD, Op,
634                                     AD.getValue(1));
635       }
636       case ISD::SETLT: {
637         SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
638         return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
639       }
640       case ISD::SETGT: {
641         SDValue T =
642           SDValue(CurDAG->getMachineNode(PPC::NEG, dl, MVT::i32, Op), 0);
643         T = SDValue(CurDAG->getMachineNode(PPC::ANDC, dl, MVT::i32, T, Op), 0);
644         SDValue Ops[] = { T, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
645         return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
646       }
647       }
648     } else if (Imm == ~0U) {        // setcc op, -1
649       SDValue Op = N->getOperand(0);
650       switch (CC) {
651       default: break;
652       case ISD::SETEQ:
653         if (isPPC64) break;
654         Op = SDValue(CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
655                                             Op, getI32Imm(1)), 0);
656         return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
657                               SDValue(CurDAG->getMachineNode(PPC::LI, dl,
658                                                              MVT::i32,
659                                                              getI32Imm(0)), 0),
660                                       Op.getValue(1));
661       case ISD::SETNE: {
662         if (isPPC64) break;
663         Op = SDValue(CurDAG->getMachineNode(PPC::NOR, dl, MVT::i32, Op, Op), 0);
664         SDNode *AD = CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
665                                             Op, getI32Imm(~0U));
666         return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32, SDValue(AD, 0),
667                                     Op, SDValue(AD, 1));
668       }
669       case ISD::SETLT: {
670         SDValue AD = SDValue(CurDAG->getMachineNode(PPC::ADDI, dl, MVT::i32, Op,
671                                                     getI32Imm(1)), 0);
672         SDValue AN = SDValue(CurDAG->getMachineNode(PPC::AND, dl, MVT::i32, AD,
673                                                     Op), 0);
674         SDValue Ops[] = { AN, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
675         return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
676       }
677       case ISD::SETGT: {
678         SDValue Ops[] = { Op, getI32Imm(1), getI32Imm(31), getI32Imm(31) };
679         Op = SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops, 4),
680                      0);
681         return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Op,
682                                     getI32Imm(1));
683       }
684       }
685     }
686   }
687
688   bool Inv;
689   int OtherCondIdx;
690   unsigned Idx = getCRIdxForSetCC(CC, Inv, OtherCondIdx);
691   SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC, dl);
692   SDValue IntCR;
693
694   // Force the ccreg into CR7.
695   SDValue CR7Reg = CurDAG->getRegister(PPC::CR7, MVT::i32);
696
697   SDValue InFlag(0, 0);  // Null incoming flag value.
698   CCReg = CurDAG->getCopyToReg(CurDAG->getEntryNode(), dl, CR7Reg, CCReg,
699                                InFlag).getValue(1);
700
701   if (PPCSubTarget.isGigaProcessor() && OtherCondIdx == -1)
702     IntCR = SDValue(CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32, CR7Reg,
703                                            CCReg), 0);
704  else
705     IntCR = SDValue(CurDAG->getMachineNode(PPC::MFCRpseud, dl, MVT::i32,
706                                            CR7Reg, CCReg), 0);
707
708   SDValue Ops[] = { IntCR, getI32Imm((32-(3-Idx)) & 31),
709                       getI32Imm(31), getI32Imm(31) };
710   if (OtherCondIdx == -1 && !Inv)
711     return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
712
713   // Get the specified bit.
714   SDValue Tmp =
715     SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops, 4), 0);
716   if (Inv) {
717     assert(OtherCondIdx == -1 && "Can't have split plus negation");
718     return CurDAG->SelectNodeTo(N, PPC::XORI, MVT::i32, Tmp, getI32Imm(1));
719   }
720
721   // Otherwise, we have to turn an operation like SETONE -> SETOLT | SETOGT.
722   // We already got the bit for the first part of the comparison (e.g. SETULE).
723
724   // Get the other bit of the comparison.
725   Ops[1] = getI32Imm((32-(3-OtherCondIdx)) & 31);
726   SDValue OtherCond =
727     SDValue(CurDAG->getMachineNode(PPC::RLWINM, dl, MVT::i32, Ops, 4), 0);
728
729   return CurDAG->SelectNodeTo(N, PPC::OR, MVT::i32, Tmp, OtherCond);
730 }
731
732
733 // Select - Convert the specified operand from a target-independent to a
734 // target-specific node if it hasn't already been changed.
735 SDNode *PPCDAGToDAGISel::Select(SDNode *N) {
736   DebugLoc dl = N->getDebugLoc();
737   if (N->isMachineOpcode())
738     return NULL;   // Already selected.
739
740   switch (N->getOpcode()) {
741   default: break;
742
743   case ISD::Constant: {
744     if (N->getValueType(0) == MVT::i64) {
745       // Get 64 bit value.
746       int64_t Imm = cast<ConstantSDNode>(N)->getZExtValue();
747       // Assume no remaining bits.
748       unsigned Remainder = 0;
749       // Assume no shift required.
750       unsigned Shift = 0;
751
752       // If it can't be represented as a 32 bit value.
753       if (!isInt<32>(Imm)) {
754         Shift = CountTrailingZeros_64(Imm);
755         int64_t ImmSh = static_cast<uint64_t>(Imm) >> Shift;
756
757         // If the shifted value fits 32 bits.
758         if (isInt<32>(ImmSh)) {
759           // Go with the shifted value.
760           Imm = ImmSh;
761         } else {
762           // Still stuck with a 64 bit value.
763           Remainder = Imm;
764           Shift = 32;
765           Imm >>= 32;
766         }
767       }
768
769       // Intermediate operand.
770       SDNode *Result;
771
772       // Handle first 32 bits.
773       unsigned Lo = Imm & 0xFFFF;
774       unsigned Hi = (Imm >> 16) & 0xFFFF;
775
776       // Simple value.
777       if (isInt<16>(Imm)) {
778        // Just the Lo bits.
779         Result = CurDAG->getMachineNode(PPC::LI8, dl, MVT::i64, getI32Imm(Lo));
780       } else if (Lo) {
781         // Handle the Hi bits.
782         unsigned OpC = Hi ? PPC::LIS8 : PPC::LI8;
783         Result = CurDAG->getMachineNode(OpC, dl, MVT::i64, getI32Imm(Hi));
784         // And Lo bits.
785         Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
786                                         SDValue(Result, 0), getI32Imm(Lo));
787       } else {
788        // Just the Hi bits.
789         Result = CurDAG->getMachineNode(PPC::LIS8, dl, MVT::i64, getI32Imm(Hi));
790       }
791
792       // If no shift, we're done.
793       if (!Shift) return Result;
794
795       // Shift for next step if the upper 32-bits were not zero.
796       if (Imm) {
797         Result = CurDAG->getMachineNode(PPC::RLDICR, dl, MVT::i64,
798                                         SDValue(Result, 0),
799                                         getI32Imm(Shift),
800                                         getI32Imm(63 - Shift));
801       }
802
803       // Add in the last bits as required.
804       if ((Hi = (Remainder >> 16) & 0xFFFF)) {
805         Result = CurDAG->getMachineNode(PPC::ORIS8, dl, MVT::i64,
806                                         SDValue(Result, 0), getI32Imm(Hi));
807       }
808       if ((Lo = Remainder & 0xFFFF)) {
809         Result = CurDAG->getMachineNode(PPC::ORI8, dl, MVT::i64,
810                                         SDValue(Result, 0), getI32Imm(Lo));
811       }
812
813       return Result;
814     }
815     break;
816   }
817
818   case ISD::SETCC:
819     return SelectSETCC(N);
820   case PPCISD::GlobalBaseReg:
821     return getGlobalBaseReg();
822
823   case ISD::FrameIndex: {
824     int FI = cast<FrameIndexSDNode>(N)->getIndex();
825     SDValue TFI = CurDAG->getTargetFrameIndex(FI, N->getValueType(0));
826     unsigned Opc = N->getValueType(0) == MVT::i32 ? PPC::ADDI : PPC::ADDI8;
827     if (N->hasOneUse())
828       return CurDAG->SelectNodeTo(N, Opc, N->getValueType(0), TFI,
829                                   getSmallIPtrImm(0));
830     return CurDAG->getMachineNode(Opc, dl, N->getValueType(0), TFI,
831                                   getSmallIPtrImm(0));
832   }
833
834   case PPCISD::MFCR: {
835     SDValue InFlag = N->getOperand(1);
836     // Use MFOCRF if supported.
837     if (PPCSubTarget.isGigaProcessor())
838       return CurDAG->getMachineNode(PPC::MFOCRF, dl, MVT::i32,
839                                     N->getOperand(0), InFlag);
840     else
841       return CurDAG->getMachineNode(PPC::MFCRpseud, dl, MVT::i32,
842                                     N->getOperand(0), InFlag);
843   }
844
845   case ISD::SDIV: {
846     // FIXME: since this depends on the setting of the carry flag from the srawi
847     //        we should really be making notes about that for the scheduler.
848     // FIXME: It sure would be nice if we could cheaply recognize the
849     //        srl/add/sra pattern the dag combiner will generate for this as
850     //        sra/addze rather than having to handle sdiv ourselves.  oh well.
851     unsigned Imm;
852     if (isInt32Immediate(N->getOperand(1), Imm)) {
853       SDValue N0 = N->getOperand(0);
854       if ((signed)Imm > 0 && isPowerOf2_32(Imm)) {
855         SDNode *Op =
856           CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue,
857                                  N0, getI32Imm(Log2_32(Imm)));
858         return CurDAG->SelectNodeTo(N, PPC::ADDZE, MVT::i32,
859                                     SDValue(Op, 0), SDValue(Op, 1));
860       } else if ((signed)Imm < 0 && isPowerOf2_32(-Imm)) {
861         SDNode *Op =
862           CurDAG->getMachineNode(PPC::SRAWI, dl, MVT::i32, MVT::Glue,
863                                  N0, getI32Imm(Log2_32(-Imm)));
864         SDValue PT =
865           SDValue(CurDAG->getMachineNode(PPC::ADDZE, dl, MVT::i32,
866                                          SDValue(Op, 0), SDValue(Op, 1)),
867                     0);
868         return CurDAG->SelectNodeTo(N, PPC::NEG, MVT::i32, PT);
869       }
870     }
871
872     // Other cases are autogenerated.
873     break;
874   }
875
876   case ISD::LOAD: {
877     // Handle preincrement loads.
878     LoadSDNode *LD = cast<LoadSDNode>(N);
879     EVT LoadedVT = LD->getMemoryVT();
880
881     // Normal loads are handled by code generated from the .td file.
882     if (LD->getAddressingMode() != ISD::PRE_INC)
883       break;
884
885     SDValue Offset = LD->getOffset();
886     if (isa<ConstantSDNode>(Offset) ||
887         Offset.getOpcode() == ISD::TargetGlobalAddress) {
888
889       unsigned Opcode;
890       bool isSExt = LD->getExtensionType() == ISD::SEXTLOAD;
891       if (LD->getValueType(0) != MVT::i64) {
892         // Handle PPC32 integer and normal FP loads.
893         assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
894         switch (LoadedVT.getSimpleVT().SimpleTy) {
895           default: llvm_unreachable("Invalid PPC load type!");
896           case MVT::f64: Opcode = PPC::LFDU; break;
897           case MVT::f32: Opcode = PPC::LFSU; break;
898           case MVT::i32: Opcode = PPC::LWZU; break;
899           case MVT::i16: Opcode = isSExt ? PPC::LHAU : PPC::LHZU; break;
900           case MVT::i1:
901           case MVT::i8:  Opcode = PPC::LBZU; break;
902         }
903       } else {
904         assert(LD->getValueType(0) == MVT::i64 && "Unknown load result type!");
905         assert((!isSExt || LoadedVT == MVT::i16) && "Invalid sext update load");
906         switch (LoadedVT.getSimpleVT().SimpleTy) {
907           default: llvm_unreachable("Invalid PPC load type!");
908           case MVT::i64: Opcode = PPC::LDU; break;
909           case MVT::i32: Opcode = PPC::LWZU8; break;
910           case MVT::i16: Opcode = isSExt ? PPC::LHAU8 : PPC::LHZU8; break;
911           case MVT::i1:
912           case MVT::i8:  Opcode = PPC::LBZU8; break;
913         }
914       }
915
916       SDValue Chain = LD->getChain();
917       SDValue Base = LD->getBasePtr();
918       SDValue Ops[] = { Offset, Base, Chain };
919       // FIXME: PPC64
920       return CurDAG->getMachineNode(Opcode, dl, LD->getValueType(0),
921                                     PPCLowering.getPointerTy(),
922                                     MVT::Other, Ops, 3);
923     } else {
924       llvm_unreachable("R+R preindex loads not supported yet!");
925     }
926   }
927
928   case ISD::AND: {
929     unsigned Imm, Imm2, SH, MB, ME;
930
931     // If this is an and of a value rotated between 0 and 31 bits and then and'd
932     // with a mask, emit rlwinm
933     if (isInt32Immediate(N->getOperand(1), Imm) &&
934         isRotateAndMask(N->getOperand(0).getNode(), Imm, false, SH, MB, ME)) {
935       SDValue Val = N->getOperand(0).getOperand(0);
936       SDValue Ops[] = { Val, getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
937       return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
938     }
939     // If this is just a masked value where the input is not handled above, and
940     // is not a rotate-left (handled by a pattern in the .td file), emit rlwinm
941     if (isInt32Immediate(N->getOperand(1), Imm) &&
942         isRunOfOnes(Imm, MB, ME) &&
943         N->getOperand(0).getOpcode() != ISD::ROTL) {
944       SDValue Val = N->getOperand(0);
945       SDValue Ops[] = { Val, getI32Imm(0), getI32Imm(MB), getI32Imm(ME) };
946       return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
947     }
948     // AND X, 0 -> 0, not "rlwinm 32".
949     if (isInt32Immediate(N->getOperand(1), Imm) && (Imm == 0)) {
950       ReplaceUses(SDValue(N, 0), N->getOperand(1));
951       return NULL;
952     }
953     // ISD::OR doesn't get all the bitfield insertion fun.
954     // (and (or x, c1), c2) where isRunOfOnes(~(c1^c2)) is a bitfield insert
955     if (isInt32Immediate(N->getOperand(1), Imm) &&
956         N->getOperand(0).getOpcode() == ISD::OR &&
957         isInt32Immediate(N->getOperand(0).getOperand(1), Imm2)) {
958       unsigned MB, ME;
959       Imm = ~(Imm^Imm2);
960       if (isRunOfOnes(Imm, MB, ME)) {
961         SDValue Ops[] = { N->getOperand(0).getOperand(0),
962                             N->getOperand(0).getOperand(1),
963                             getI32Imm(0), getI32Imm(MB),getI32Imm(ME) };
964         return CurDAG->getMachineNode(PPC::RLWIMI, dl, MVT::i32, Ops, 5);
965       }
966     }
967
968     // Other cases are autogenerated.
969     break;
970   }
971   case ISD::OR:
972     if (N->getValueType(0) == MVT::i32)
973       if (SDNode *I = SelectBitfieldInsert(N))
974         return I;
975
976     // Other cases are autogenerated.
977     break;
978   case ISD::SHL: {
979     unsigned Imm, SH, MB, ME;
980     if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
981         isRotateAndMask(N, Imm, true, SH, MB, ME)) {
982       SDValue Ops[] = { N->getOperand(0).getOperand(0),
983                           getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
984       return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
985     }
986
987     // Other cases are autogenerated.
988     break;
989   }
990   case ISD::SRL: {
991     unsigned Imm, SH, MB, ME;
992     if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, Imm) &&
993         isRotateAndMask(N, Imm, true, SH, MB, ME)) {
994       SDValue Ops[] = { N->getOperand(0).getOperand(0),
995                           getI32Imm(SH), getI32Imm(MB), getI32Imm(ME) };
996       return CurDAG->SelectNodeTo(N, PPC::RLWINM, MVT::i32, Ops, 4);
997     }
998
999     // Other cases are autogenerated.
1000     break;
1001   }
1002   case ISD::SELECT_CC: {
1003     ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(4))->get();
1004     EVT PtrVT = CurDAG->getTargetLoweringInfo().getPointerTy();
1005     bool isPPC64 = (PtrVT == MVT::i64);
1006
1007     // Handle the setcc cases here.  select_cc lhs, 0, 1, 0, cc
1008     if (!isPPC64)
1009       if (ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1)))
1010         if (ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N->getOperand(2)))
1011           if (ConstantSDNode *N3C = dyn_cast<ConstantSDNode>(N->getOperand(3)))
1012             if (N1C->isNullValue() && N3C->isNullValue() &&
1013                 N2C->getZExtValue() == 1ULL && CC == ISD::SETNE &&
1014                 // FIXME: Implement this optzn for PPC64.
1015                 N->getValueType(0) == MVT::i32) {
1016               SDNode *Tmp =
1017                 CurDAG->getMachineNode(PPC::ADDIC, dl, MVT::i32, MVT::Glue,
1018                                        N->getOperand(0), getI32Imm(~0U));
1019               return CurDAG->SelectNodeTo(N, PPC::SUBFE, MVT::i32,
1020                                           SDValue(Tmp, 0), N->getOperand(0),
1021                                           SDValue(Tmp, 1));
1022             }
1023
1024     SDValue CCReg = SelectCC(N->getOperand(0), N->getOperand(1), CC, dl);
1025     unsigned BROpc = getPredicateForSetCC(CC);
1026
1027     unsigned SelectCCOp;
1028     if (N->getValueType(0) == MVT::i32)
1029       SelectCCOp = PPC::SELECT_CC_I4;
1030     else if (N->getValueType(0) == MVT::i64)
1031       SelectCCOp = PPC::SELECT_CC_I8;
1032     else if (N->getValueType(0) == MVT::f32)
1033       SelectCCOp = PPC::SELECT_CC_F4;
1034     else if (N->getValueType(0) == MVT::f64)
1035       SelectCCOp = PPC::SELECT_CC_F8;
1036     else
1037       SelectCCOp = PPC::SELECT_CC_VRRC;
1038
1039     SDValue Ops[] = { CCReg, N->getOperand(2), N->getOperand(3),
1040                         getI32Imm(BROpc) };
1041     return CurDAG->SelectNodeTo(N, SelectCCOp, N->getValueType(0), Ops, 4);
1042   }
1043   case PPCISD::COND_BRANCH: {
1044     // Op #0 is the Chain.
1045     // Op #1 is the PPC::PRED_* number.
1046     // Op #2 is the CR#
1047     // Op #3 is the Dest MBB
1048     // Op #4 is the Flag.
1049     // Prevent PPC::PRED_* from being selected into LI.
1050     SDValue Pred =
1051       getI32Imm(cast<ConstantSDNode>(N->getOperand(1))->getZExtValue());
1052     SDValue Ops[] = { Pred, N->getOperand(2), N->getOperand(3),
1053       N->getOperand(0), N->getOperand(4) };
1054     return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops, 5);
1055   }
1056   case ISD::BR_CC: {
1057     ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(1))->get();
1058     SDValue CondCode = SelectCC(N->getOperand(2), N->getOperand(3), CC, dl);
1059     SDValue Ops[] = { getI32Imm(getPredicateForSetCC(CC)), CondCode,
1060                         N->getOperand(4), N->getOperand(0) };
1061     return CurDAG->SelectNodeTo(N, PPC::BCC, MVT::Other, Ops, 4);
1062   }
1063   case ISD::BRIND: {
1064     // FIXME: Should custom lower this.
1065     SDValue Chain = N->getOperand(0);
1066     SDValue Target = N->getOperand(1);
1067     unsigned Opc = Target.getValueType() == MVT::i32 ? PPC::MTCTR : PPC::MTCTR8;
1068     unsigned Reg = Target.getValueType() == MVT::i32 ? PPC::BCTR : PPC::BCTR8;
1069     Chain = SDValue(CurDAG->getMachineNode(Opc, dl, MVT::Glue, Target,
1070                                            Chain), 0);
1071     return CurDAG->SelectNodeTo(N, Reg, MVT::Other, Chain);
1072   }
1073   }
1074
1075   return SelectCode(N);
1076 }
1077
1078
1079
1080 /// createPPCISelDag - This pass converts a legalized DAG into a
1081 /// PowerPC-specific DAG, ready for instruction scheduling.
1082 ///
1083 FunctionPass *llvm::createPPCISelDag(PPCTargetMachine &TM) {
1084   return new PPCDAGToDAGISel(TM);
1085 }
1086