Re-sort all of the includes with ./utils/sort_includes.py so that
[oota-llvm.git] / lib / Target / PowerPC / PPCInstrInfo.cpp
1 //===-- PPCInstrInfo.cpp - PowerPC Instruction Information ----------------===//
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 contains the PowerPC implementation of the TargetInstrInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "PPCInstrInfo.h"
15 #include "MCTargetDesc/PPCPredicates.h"
16 #include "PPC.h"
17 #include "PPCHazardRecognizers.h"
18 #include "PPCInstrBuilder.h"
19 #include "PPCMachineFunctionInfo.h"
20 #include "PPCTargetMachine.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/CodeGen/MachineFrameInfo.h"
24 #include "llvm/CodeGen/MachineFunctionPass.h"
25 #include "llvm/CodeGen/MachineInstrBuilder.h"
26 #include "llvm/CodeGen/MachineMemOperand.h"
27 #include "llvm/CodeGen/MachineRegisterInfo.h"
28 #include "llvm/CodeGen/PseudoSourceValue.h"
29 #include "llvm/MC/MCAsmInfo.h"
30 #include "llvm/Support/CommandLine.h"
31 #include "llvm/Support/ErrorHandling.h"
32 #include "llvm/Support/TargetRegistry.h"
33 #include "llvm/Support/raw_ostream.h"
34
35 #define GET_INSTRMAP_INFO
36 #define GET_INSTRINFO_CTOR_DTOR
37 #include "PPCGenInstrInfo.inc"
38
39 using namespace llvm;
40
41 static cl::
42 opt<bool> DisableCTRLoopAnal("disable-ppc-ctrloop-analysis", cl::Hidden,
43             cl::desc("Disable analysis for CTR loops"));
44
45 static cl::opt<bool> DisableCmpOpt("disable-ppc-cmp-opt",
46 cl::desc("Disable compare instruction optimization"), cl::Hidden);
47
48 // Pin the vtable to this file.
49 void PPCInstrInfo::anchor() {}
50
51 PPCInstrInfo::PPCInstrInfo(PPCTargetMachine &tm)
52   : PPCGenInstrInfo(PPC::ADJCALLSTACKDOWN, PPC::ADJCALLSTACKUP),
53     TM(tm), RI(*TM.getSubtargetImpl()) {}
54
55 /// CreateTargetHazardRecognizer - Return the hazard recognizer to use for
56 /// this target when scheduling the DAG.
57 ScheduleHazardRecognizer *PPCInstrInfo::CreateTargetHazardRecognizer(
58   const TargetMachine *TM,
59   const ScheduleDAG *DAG) const {
60   unsigned Directive = TM->getSubtarget<PPCSubtarget>().getDarwinDirective();
61   if (Directive == PPC::DIR_440 || Directive == PPC::DIR_A2 ||
62       Directive == PPC::DIR_E500mc || Directive == PPC::DIR_E5500) {
63     const InstrItineraryData *II = TM->getInstrItineraryData();
64     return new ScoreboardHazardRecognizer(II, DAG);
65   }
66
67   return TargetInstrInfo::CreateTargetHazardRecognizer(TM, DAG);
68 }
69
70 /// CreateTargetPostRAHazardRecognizer - Return the postRA hazard recognizer
71 /// to use for this target when scheduling the DAG.
72 ScheduleHazardRecognizer *PPCInstrInfo::CreateTargetPostRAHazardRecognizer(
73   const InstrItineraryData *II,
74   const ScheduleDAG *DAG) const {
75   unsigned Directive = TM.getSubtarget<PPCSubtarget>().getDarwinDirective();
76
77   if (Directive == PPC::DIR_PWR7)
78     return new PPCDispatchGroupSBHazardRecognizer(II, DAG);
79
80   // Most subtargets use a PPC970 recognizer.
81   if (Directive != PPC::DIR_440 && Directive != PPC::DIR_A2 &&
82       Directive != PPC::DIR_E500mc && Directive != PPC::DIR_E5500) {
83     assert(TM.getInstrInfo() && "No InstrInfo?");
84
85     return new PPCHazardRecognizer970(TM);
86   }
87
88   return new ScoreboardHazardRecognizer(II, DAG);
89 }
90
91
92 int PPCInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
93                                     const MachineInstr *DefMI, unsigned DefIdx,
94                                     const MachineInstr *UseMI,
95                                     unsigned UseIdx) const {
96   int Latency = PPCGenInstrInfo::getOperandLatency(ItinData, DefMI, DefIdx,
97                                                    UseMI, UseIdx);
98
99   const MachineOperand &DefMO = DefMI->getOperand(DefIdx);
100   unsigned Reg = DefMO.getReg();
101
102   const TargetRegisterInfo *TRI = &getRegisterInfo();
103   bool IsRegCR;
104   if (TRI->isVirtualRegister(Reg)) {
105     const MachineRegisterInfo *MRI =
106       &DefMI->getParent()->getParent()->getRegInfo();
107     IsRegCR = MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRRCRegClass) ||
108               MRI->getRegClass(Reg)->hasSuperClassEq(&PPC::CRBITRCRegClass);
109   } else {
110     IsRegCR = PPC::CRRCRegClass.contains(Reg) ||
111               PPC::CRBITRCRegClass.contains(Reg);
112   }
113
114   if (UseMI->isBranch() && IsRegCR) {
115     if (Latency < 0)
116       Latency = getInstrLatency(ItinData, DefMI);
117
118     // On some cores, there is an additional delay between writing to a condition
119     // register, and using it from a branch.
120     unsigned Directive = TM.getSubtarget<PPCSubtarget>().getDarwinDirective();
121     switch (Directive) {
122     default: break;
123     case PPC::DIR_7400:
124     case PPC::DIR_750:
125     case PPC::DIR_970:
126     case PPC::DIR_E5500:
127     case PPC::DIR_PWR4:
128     case PPC::DIR_PWR5:
129     case PPC::DIR_PWR5X:
130     case PPC::DIR_PWR6:
131     case PPC::DIR_PWR6X:
132     case PPC::DIR_PWR7:
133       Latency += 2;
134       break;
135     }
136   }
137
138   return Latency;
139 }
140
141 // Detect 32 -> 64-bit extensions where we may reuse the low sub-register.
142 bool PPCInstrInfo::isCoalescableExtInstr(const MachineInstr &MI,
143                                          unsigned &SrcReg, unsigned &DstReg,
144                                          unsigned &SubIdx) const {
145   switch (MI.getOpcode()) {
146   default: return false;
147   case PPC::EXTSW:
148   case PPC::EXTSW_32_64:
149     SrcReg = MI.getOperand(1).getReg();
150     DstReg = MI.getOperand(0).getReg();
151     SubIdx = PPC::sub_32;
152     return true;
153   }
154 }
155
156 unsigned PPCInstrInfo::isLoadFromStackSlot(const MachineInstr *MI,
157                                            int &FrameIndex) const {
158   // Note: This list must be kept consistent with LoadRegFromStackSlot.
159   switch (MI->getOpcode()) {
160   default: break;
161   case PPC::LD:
162   case PPC::LWZ:
163   case PPC::LFS:
164   case PPC::LFD:
165   case PPC::RESTORE_CR:
166   case PPC::LVX:
167   case PPC::RESTORE_VRSAVE:
168     // Check for the operands added by addFrameReference (the immediate is the
169     // offset which defaults to 0).
170     if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
171         MI->getOperand(2).isFI()) {
172       FrameIndex = MI->getOperand(2).getIndex();
173       return MI->getOperand(0).getReg();
174     }
175     break;
176   }
177   return 0;
178 }
179
180 unsigned PPCInstrInfo::isStoreToStackSlot(const MachineInstr *MI,
181                                           int &FrameIndex) const {
182   // Note: This list must be kept consistent with StoreRegToStackSlot.
183   switch (MI->getOpcode()) {
184   default: break;
185   case PPC::STD:
186   case PPC::STW:
187   case PPC::STFS:
188   case PPC::STFD:
189   case PPC::SPILL_CR:
190   case PPC::STVX:
191   case PPC::SPILL_VRSAVE:
192     // Check for the operands added by addFrameReference (the immediate is the
193     // offset which defaults to 0).
194     if (MI->getOperand(1).isImm() && !MI->getOperand(1).getImm() &&
195         MI->getOperand(2).isFI()) {
196       FrameIndex = MI->getOperand(2).getIndex();
197       return MI->getOperand(0).getReg();
198     }
199     break;
200   }
201   return 0;
202 }
203
204 // commuteInstruction - We can commute rlwimi instructions, but only if the
205 // rotate amt is zero.  We also have to munge the immediates a bit.
206 MachineInstr *
207 PPCInstrInfo::commuteInstruction(MachineInstr *MI, bool NewMI) const {
208   MachineFunction &MF = *MI->getParent()->getParent();
209
210   // Normal instructions can be commuted the obvious way.
211   if (MI->getOpcode() != PPC::RLWIMI &&
212       MI->getOpcode() != PPC::RLWIMIo)
213     return TargetInstrInfo::commuteInstruction(MI, NewMI);
214
215   // Cannot commute if it has a non-zero rotate count.
216   if (MI->getOperand(3).getImm() != 0)
217     return 0;
218
219   // If we have a zero rotate count, we have:
220   //   M = mask(MB,ME)
221   //   Op0 = (Op1 & ~M) | (Op2 & M)
222   // Change this to:
223   //   M = mask((ME+1)&31, (MB-1)&31)
224   //   Op0 = (Op2 & ~M) | (Op1 & M)
225
226   // Swap op1/op2
227   unsigned Reg0 = MI->getOperand(0).getReg();
228   unsigned Reg1 = MI->getOperand(1).getReg();
229   unsigned Reg2 = MI->getOperand(2).getReg();
230   unsigned SubReg1 = MI->getOperand(1).getSubReg();
231   unsigned SubReg2 = MI->getOperand(2).getSubReg();
232   bool Reg1IsKill = MI->getOperand(1).isKill();
233   bool Reg2IsKill = MI->getOperand(2).isKill();
234   bool ChangeReg0 = false;
235   // If machine instrs are no longer in two-address forms, update
236   // destination register as well.
237   if (Reg0 == Reg1) {
238     // Must be two address instruction!
239     assert(MI->getDesc().getOperandConstraint(0, MCOI::TIED_TO) &&
240            "Expecting a two-address instruction!");
241     assert(MI->getOperand(0).getSubReg() == SubReg1 && "Tied subreg mismatch");
242     Reg2IsKill = false;
243     ChangeReg0 = true;
244   }
245
246   // Masks.
247   unsigned MB = MI->getOperand(4).getImm();
248   unsigned ME = MI->getOperand(5).getImm();
249
250   if (NewMI) {
251     // Create a new instruction.
252     unsigned Reg0 = ChangeReg0 ? Reg2 : MI->getOperand(0).getReg();
253     bool Reg0IsDead = MI->getOperand(0).isDead();
254     return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
255       .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead))
256       .addReg(Reg2, getKillRegState(Reg2IsKill))
257       .addReg(Reg1, getKillRegState(Reg1IsKill))
258       .addImm((ME+1) & 31)
259       .addImm((MB-1) & 31);
260   }
261
262   if (ChangeReg0) {
263     MI->getOperand(0).setReg(Reg2);
264     MI->getOperand(0).setSubReg(SubReg2);
265   }
266   MI->getOperand(2).setReg(Reg1);
267   MI->getOperand(1).setReg(Reg2);
268   MI->getOperand(2).setSubReg(SubReg1);
269   MI->getOperand(1).setSubReg(SubReg2);
270   MI->getOperand(2).setIsKill(Reg1IsKill);
271   MI->getOperand(1).setIsKill(Reg2IsKill);
272
273   // Swap the mask around.
274   MI->getOperand(4).setImm((ME+1) & 31);
275   MI->getOperand(5).setImm((MB-1) & 31);
276   return MI;
277 }
278
279 void PPCInstrInfo::insertNoop(MachineBasicBlock &MBB,
280                               MachineBasicBlock::iterator MI) const {
281   // This function is used for scheduling, and the nop wanted here is the type
282   // that terminates dispatch groups on the POWER cores.
283   unsigned Directive = TM.getSubtarget<PPCSubtarget>().getDarwinDirective();
284   unsigned Opcode;
285   switch (Directive) {
286   default:            Opcode = PPC::NOP; break;
287   case PPC::DIR_PWR6: Opcode = PPC::NOP_GT_PWR6; break;
288   case PPC::DIR_PWR7: Opcode = PPC::NOP_GT_PWR7; break;
289   }
290
291   DebugLoc DL;
292   BuildMI(MBB, MI, DL, get(Opcode));
293 }
294
295 // Branch analysis.
296 // Note: If the condition register is set to CTR or CTR8 then this is a
297 // BDNZ (imm == 1) or BDZ (imm == 0) branch.
298 bool PPCInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB,
299                                  MachineBasicBlock *&FBB,
300                                  SmallVectorImpl<MachineOperand> &Cond,
301                                  bool AllowModify) const {
302   bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
303
304   // If the block has no terminators, it just falls into the block after it.
305   MachineBasicBlock::iterator I = MBB.end();
306   if (I == MBB.begin())
307     return false;
308   --I;
309   while (I->isDebugValue()) {
310     if (I == MBB.begin())
311       return false;
312     --I;
313   }
314   if (!isUnpredicatedTerminator(I))
315     return false;
316
317   // Get the last instruction in the block.
318   MachineInstr *LastInst = I;
319
320   // If there is only one terminator instruction, process it.
321   if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) {
322     if (LastInst->getOpcode() == PPC::B) {
323       if (!LastInst->getOperand(0).isMBB())
324         return true;
325       TBB = LastInst->getOperand(0).getMBB();
326       return false;
327     } else if (LastInst->getOpcode() == PPC::BCC) {
328       if (!LastInst->getOperand(2).isMBB())
329         return true;
330       // Block ends with fall-through condbranch.
331       TBB = LastInst->getOperand(2).getMBB();
332       Cond.push_back(LastInst->getOperand(0));
333       Cond.push_back(LastInst->getOperand(1));
334       return false;
335     } else if (LastInst->getOpcode() == PPC::BDNZ8 ||
336                LastInst->getOpcode() == PPC::BDNZ) {
337       if (!LastInst->getOperand(0).isMBB())
338         return true;
339       if (DisableCTRLoopAnal)
340         return true;
341       TBB = LastInst->getOperand(0).getMBB();
342       Cond.push_back(MachineOperand::CreateImm(1));
343       Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
344                                                true));
345       return false;
346     } else if (LastInst->getOpcode() == PPC::BDZ8 ||
347                LastInst->getOpcode() == PPC::BDZ) {
348       if (!LastInst->getOperand(0).isMBB())
349         return true;
350       if (DisableCTRLoopAnal)
351         return true;
352       TBB = LastInst->getOperand(0).getMBB();
353       Cond.push_back(MachineOperand::CreateImm(0));
354       Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
355                                                true));
356       return false;
357     }
358
359     // Otherwise, don't know what this is.
360     return true;
361   }
362
363   // Get the instruction before it if it's a terminator.
364   MachineInstr *SecondLastInst = I;
365
366   // If there are three terminators, we don't know what sort of block this is.
367   if (SecondLastInst && I != MBB.begin() &&
368       isUnpredicatedTerminator(--I))
369     return true;
370
371   // If the block ends with PPC::B and PPC:BCC, handle it.
372   if (SecondLastInst->getOpcode() == PPC::BCC &&
373       LastInst->getOpcode() == PPC::B) {
374     if (!SecondLastInst->getOperand(2).isMBB() ||
375         !LastInst->getOperand(0).isMBB())
376       return true;
377     TBB =  SecondLastInst->getOperand(2).getMBB();
378     Cond.push_back(SecondLastInst->getOperand(0));
379     Cond.push_back(SecondLastInst->getOperand(1));
380     FBB = LastInst->getOperand(0).getMBB();
381     return false;
382   } else if ((SecondLastInst->getOpcode() == PPC::BDNZ8 ||
383               SecondLastInst->getOpcode() == PPC::BDNZ) &&
384       LastInst->getOpcode() == PPC::B) {
385     if (!SecondLastInst->getOperand(0).isMBB() ||
386         !LastInst->getOperand(0).isMBB())
387       return true;
388     if (DisableCTRLoopAnal)
389       return true;
390     TBB = SecondLastInst->getOperand(0).getMBB();
391     Cond.push_back(MachineOperand::CreateImm(1));
392     Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
393                                              true));
394     FBB = LastInst->getOperand(0).getMBB();
395     return false;
396   } else if ((SecondLastInst->getOpcode() == PPC::BDZ8 ||
397               SecondLastInst->getOpcode() == PPC::BDZ) &&
398       LastInst->getOpcode() == PPC::B) {
399     if (!SecondLastInst->getOperand(0).isMBB() ||
400         !LastInst->getOperand(0).isMBB())
401       return true;
402     if (DisableCTRLoopAnal)
403       return true;
404     TBB = SecondLastInst->getOperand(0).getMBB();
405     Cond.push_back(MachineOperand::CreateImm(0));
406     Cond.push_back(MachineOperand::CreateReg(isPPC64 ? PPC::CTR8 : PPC::CTR,
407                                              true));
408     FBB = LastInst->getOperand(0).getMBB();
409     return false;
410   }
411
412   // If the block ends with two PPC:Bs, handle it.  The second one is not
413   // executed, so remove it.
414   if (SecondLastInst->getOpcode() == PPC::B &&
415       LastInst->getOpcode() == PPC::B) {
416     if (!SecondLastInst->getOperand(0).isMBB())
417       return true;
418     TBB = SecondLastInst->getOperand(0).getMBB();
419     I = LastInst;
420     if (AllowModify)
421       I->eraseFromParent();
422     return false;
423   }
424
425   // Otherwise, can't handle this.
426   return true;
427 }
428
429 unsigned PPCInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const {
430   MachineBasicBlock::iterator I = MBB.end();
431   if (I == MBB.begin()) return 0;
432   --I;
433   while (I->isDebugValue()) {
434     if (I == MBB.begin())
435       return 0;
436     --I;
437   }
438   if (I->getOpcode() != PPC::B && I->getOpcode() != PPC::BCC &&
439       I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
440       I->getOpcode() != PPC::BDZ8  && I->getOpcode() != PPC::BDZ)
441     return 0;
442
443   // Remove the branch.
444   I->eraseFromParent();
445
446   I = MBB.end();
447
448   if (I == MBB.begin()) return 1;
449   --I;
450   if (I->getOpcode() != PPC::BCC &&
451       I->getOpcode() != PPC::BDNZ8 && I->getOpcode() != PPC::BDNZ &&
452       I->getOpcode() != PPC::BDZ8  && I->getOpcode() != PPC::BDZ)
453     return 1;
454
455   // Remove the branch.
456   I->eraseFromParent();
457   return 2;
458 }
459
460 unsigned
461 PPCInstrInfo::InsertBranch(MachineBasicBlock &MBB, MachineBasicBlock *TBB,
462                            MachineBasicBlock *FBB,
463                            const SmallVectorImpl<MachineOperand> &Cond,
464                            DebugLoc DL) const {
465   // Shouldn't be a fall through.
466   assert(TBB && "InsertBranch must not be told to insert a fallthrough");
467   assert((Cond.size() == 2 || Cond.size() == 0) &&
468          "PPC branch conditions have two components!");
469
470   bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
471
472   // One-way branch.
473   if (FBB == 0) {
474     if (Cond.empty())   // Unconditional branch
475       BuildMI(&MBB, DL, get(PPC::B)).addMBB(TBB);
476     else if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
477       BuildMI(&MBB, DL, get(Cond[0].getImm() ?
478                               (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
479                               (isPPC64 ? PPC::BDZ8  : PPC::BDZ))).addMBB(TBB);
480     else                // Conditional branch
481       BuildMI(&MBB, DL, get(PPC::BCC))
482         .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
483     return 1;
484   }
485
486   // Two-way Conditional Branch.
487   if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
488     BuildMI(&MBB, DL, get(Cond[0].getImm() ?
489                             (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
490                             (isPPC64 ? PPC::BDZ8  : PPC::BDZ))).addMBB(TBB);
491   else
492     BuildMI(&MBB, DL, get(PPC::BCC))
493       .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB);
494   BuildMI(&MBB, DL, get(PPC::B)).addMBB(FBB);
495   return 2;
496 }
497
498 // Select analysis.
499 bool PPCInstrInfo::canInsertSelect(const MachineBasicBlock &MBB,
500                 const SmallVectorImpl<MachineOperand> &Cond,
501                 unsigned TrueReg, unsigned FalseReg,
502                 int &CondCycles, int &TrueCycles, int &FalseCycles) const {
503   if (!TM.getSubtargetImpl()->hasISEL())
504     return false;
505
506   if (Cond.size() != 2)
507     return false;
508
509   // If this is really a bdnz-like condition, then it cannot be turned into a
510   // select.
511   if (Cond[1].getReg() == PPC::CTR || Cond[1].getReg() == PPC::CTR8)
512     return false;
513
514   // Check register classes.
515   const MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
516   const TargetRegisterClass *RC =
517     RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
518   if (!RC)
519     return false;
520
521   // isel is for regular integer GPRs only.
522   if (!PPC::GPRCRegClass.hasSubClassEq(RC) &&
523       !PPC::GPRC_NOR0RegClass.hasSubClassEq(RC) &&
524       !PPC::G8RCRegClass.hasSubClassEq(RC) &&
525       !PPC::G8RC_NOX0RegClass.hasSubClassEq(RC))
526     return false;
527
528   // FIXME: These numbers are for the A2, how well they work for other cores is
529   // an open question. On the A2, the isel instruction has a 2-cycle latency
530   // but single-cycle throughput. These numbers are used in combination with
531   // the MispredictPenalty setting from the active SchedMachineModel.
532   CondCycles = 1;
533   TrueCycles = 1;
534   FalseCycles = 1;
535
536   return true;
537 }
538
539 void PPCInstrInfo::insertSelect(MachineBasicBlock &MBB,
540                                 MachineBasicBlock::iterator MI, DebugLoc dl,
541                                 unsigned DestReg,
542                                 const SmallVectorImpl<MachineOperand> &Cond,
543                                 unsigned TrueReg, unsigned FalseReg) const {
544   assert(Cond.size() == 2 &&
545          "PPC branch conditions have two components!");
546
547   assert(TM.getSubtargetImpl()->hasISEL() &&
548          "Cannot insert select on target without ISEL support");
549
550   // Get the register classes.
551   MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
552   const TargetRegisterClass *RC =
553     RI.getCommonSubClass(MRI.getRegClass(TrueReg), MRI.getRegClass(FalseReg));
554   assert(RC && "TrueReg and FalseReg must have overlapping register classes");
555
556   bool Is64Bit = PPC::G8RCRegClass.hasSubClassEq(RC) ||
557                  PPC::G8RC_NOX0RegClass.hasSubClassEq(RC);
558   assert((Is64Bit ||
559           PPC::GPRCRegClass.hasSubClassEq(RC) ||
560           PPC::GPRC_NOR0RegClass.hasSubClassEq(RC)) &&
561          "isel is for regular integer GPRs only");
562
563   unsigned OpCode = Is64Bit ? PPC::ISEL8 : PPC::ISEL;
564   unsigned SelectPred = Cond[0].getImm();
565
566   unsigned SubIdx;
567   bool SwapOps;
568   switch (SelectPred) {
569   default: llvm_unreachable("invalid predicate for isel");
570   case PPC::PRED_EQ: SubIdx = PPC::sub_eq; SwapOps = false; break;
571   case PPC::PRED_NE: SubIdx = PPC::sub_eq; SwapOps = true; break;
572   case PPC::PRED_LT: SubIdx = PPC::sub_lt; SwapOps = false; break;
573   case PPC::PRED_GE: SubIdx = PPC::sub_lt; SwapOps = true; break;
574   case PPC::PRED_GT: SubIdx = PPC::sub_gt; SwapOps = false; break;
575   case PPC::PRED_LE: SubIdx = PPC::sub_gt; SwapOps = true; break;
576   case PPC::PRED_UN: SubIdx = PPC::sub_un; SwapOps = false; break;
577   case PPC::PRED_NU: SubIdx = PPC::sub_un; SwapOps = true; break;
578   }
579
580   unsigned FirstReg =  SwapOps ? FalseReg : TrueReg,
581            SecondReg = SwapOps ? TrueReg  : FalseReg;
582
583   // The first input register of isel cannot be r0. If it is a member
584   // of a register class that can be r0, then copy it first (the
585   // register allocator should eliminate the copy).
586   if (MRI.getRegClass(FirstReg)->contains(PPC::R0) ||
587       MRI.getRegClass(FirstReg)->contains(PPC::X0)) {
588     const TargetRegisterClass *FirstRC =
589       MRI.getRegClass(FirstReg)->contains(PPC::X0) ?
590         &PPC::G8RC_NOX0RegClass : &PPC::GPRC_NOR0RegClass;
591     unsigned OldFirstReg = FirstReg;
592     FirstReg = MRI.createVirtualRegister(FirstRC);
593     BuildMI(MBB, MI, dl, get(TargetOpcode::COPY), FirstReg)
594       .addReg(OldFirstReg);
595   }
596
597   BuildMI(MBB, MI, dl, get(OpCode), DestReg)
598     .addReg(FirstReg).addReg(SecondReg)
599     .addReg(Cond[1].getReg(), 0, SubIdx);
600 }
601
602 void PPCInstrInfo::copyPhysReg(MachineBasicBlock &MBB,
603                                MachineBasicBlock::iterator I, DebugLoc DL,
604                                unsigned DestReg, unsigned SrcReg,
605                                bool KillSrc) const {
606   unsigned Opc;
607   if (PPC::GPRCRegClass.contains(DestReg, SrcReg))
608     Opc = PPC::OR;
609   else if (PPC::G8RCRegClass.contains(DestReg, SrcReg))
610     Opc = PPC::OR8;
611   else if (PPC::F4RCRegClass.contains(DestReg, SrcReg))
612     Opc = PPC::FMR;
613   else if (PPC::CRRCRegClass.contains(DestReg, SrcReg))
614     Opc = PPC::MCRF;
615   else if (PPC::VRRCRegClass.contains(DestReg, SrcReg))
616     Opc = PPC::VOR;
617   else if (PPC::CRBITRCRegClass.contains(DestReg, SrcReg))
618     Opc = PPC::CROR;
619   else
620     llvm_unreachable("Impossible reg-to-reg copy");
621
622   const MCInstrDesc &MCID = get(Opc);
623   if (MCID.getNumOperands() == 3)
624     BuildMI(MBB, I, DL, MCID, DestReg)
625       .addReg(SrcReg).addReg(SrcReg, getKillRegState(KillSrc));
626   else
627     BuildMI(MBB, I, DL, MCID, DestReg).addReg(SrcReg, getKillRegState(KillSrc));
628 }
629
630 // This function returns true if a CR spill is necessary and false otherwise.
631 bool
632 PPCInstrInfo::StoreRegToStackSlot(MachineFunction &MF,
633                                   unsigned SrcReg, bool isKill,
634                                   int FrameIdx,
635                                   const TargetRegisterClass *RC,
636                                   SmallVectorImpl<MachineInstr*> &NewMIs,
637                                   bool &NonRI, bool &SpillsVRS) const{
638   // Note: If additional store instructions are added here,
639   // update isStoreToStackSlot.
640
641   DebugLoc DL;
642   if (PPC::GPRCRegClass.hasSubClassEq(RC)) {
643     NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STW))
644                                        .addReg(SrcReg,
645                                                getKillRegState(isKill)),
646                                        FrameIdx));
647   } else if (PPC::G8RCRegClass.hasSubClassEq(RC)) {
648     NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STD))
649                                        .addReg(SrcReg,
650                                                getKillRegState(isKill)),
651                                        FrameIdx));
652   } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
653     NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFD))
654                                        .addReg(SrcReg,
655                                                getKillRegState(isKill)),
656                                        FrameIdx));
657   } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
658     NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STFS))
659                                        .addReg(SrcReg,
660                                                getKillRegState(isKill)),
661                                        FrameIdx));
662   } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
663     NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_CR))
664                                        .addReg(SrcReg,
665                                                getKillRegState(isKill)),
666                                        FrameIdx));
667     return true;
668   } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
669     // FIXME: We use CRi here because there is no mtcrf on a bit. Since the
670     // backend currently only uses CR1EQ as an individual bit, this should
671     // not cause any bug. If we need other uses of CR bits, the following
672     // code may be invalid.
673     unsigned Reg = 0;
674     if (SrcReg == PPC::CR0LT || SrcReg == PPC::CR0GT ||
675         SrcReg == PPC::CR0EQ || SrcReg == PPC::CR0UN)
676       Reg = PPC::CR0;
677     else if (SrcReg == PPC::CR1LT || SrcReg == PPC::CR1GT ||
678              SrcReg == PPC::CR1EQ || SrcReg == PPC::CR1UN)
679       Reg = PPC::CR1;
680     else if (SrcReg == PPC::CR2LT || SrcReg == PPC::CR2GT ||
681              SrcReg == PPC::CR2EQ || SrcReg == PPC::CR2UN)
682       Reg = PPC::CR2;
683     else if (SrcReg == PPC::CR3LT || SrcReg == PPC::CR3GT ||
684              SrcReg == PPC::CR3EQ || SrcReg == PPC::CR3UN)
685       Reg = PPC::CR3;
686     else if (SrcReg == PPC::CR4LT || SrcReg == PPC::CR4GT ||
687              SrcReg == PPC::CR4EQ || SrcReg == PPC::CR4UN)
688       Reg = PPC::CR4;
689     else if (SrcReg == PPC::CR5LT || SrcReg == PPC::CR5GT ||
690              SrcReg == PPC::CR5EQ || SrcReg == PPC::CR5UN)
691       Reg = PPC::CR5;
692     else if (SrcReg == PPC::CR6LT || SrcReg == PPC::CR6GT ||
693              SrcReg == PPC::CR6EQ || SrcReg == PPC::CR6UN)
694       Reg = PPC::CR6;
695     else if (SrcReg == PPC::CR7LT || SrcReg == PPC::CR7GT ||
696              SrcReg == PPC::CR7EQ || SrcReg == PPC::CR7UN)
697       Reg = PPC::CR7;
698
699     return StoreRegToStackSlot(MF, Reg, isKill, FrameIdx,
700                                &PPC::CRRCRegClass, NewMIs, NonRI, SpillsVRS);
701
702   } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
703     NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::STVX))
704                                        .addReg(SrcReg,
705                                                getKillRegState(isKill)),
706                                        FrameIdx));
707     NonRI = true;
708   } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
709     assert(TM.getSubtargetImpl()->isDarwin() &&
710            "VRSAVE only needs spill/restore on Darwin");
711     NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::SPILL_VRSAVE))
712                                        .addReg(SrcReg,
713                                                getKillRegState(isKill)),
714                                        FrameIdx));
715     SpillsVRS = true;
716   } else {
717     llvm_unreachable("Unknown regclass!");
718   }
719
720   return false;
721 }
722
723 void
724 PPCInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB,
725                                   MachineBasicBlock::iterator MI,
726                                   unsigned SrcReg, bool isKill, int FrameIdx,
727                                   const TargetRegisterClass *RC,
728                                   const TargetRegisterInfo *TRI) const {
729   MachineFunction &MF = *MBB.getParent();
730   SmallVector<MachineInstr*, 4> NewMIs;
731
732   PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
733   FuncInfo->setHasSpills();
734
735   bool NonRI = false, SpillsVRS = false;
736   if (StoreRegToStackSlot(MF, SrcReg, isKill, FrameIdx, RC, NewMIs,
737                           NonRI, SpillsVRS))
738     FuncInfo->setSpillsCR();
739
740   if (SpillsVRS)
741     FuncInfo->setSpillsVRSAVE();
742
743   if (NonRI)
744     FuncInfo->setHasNonRISpills();
745
746   for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
747     MBB.insert(MI, NewMIs[i]);
748
749   const MachineFrameInfo &MFI = *MF.getFrameInfo();
750   MachineMemOperand *MMO =
751     MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
752                             MachineMemOperand::MOStore,
753                             MFI.getObjectSize(FrameIdx),
754                             MFI.getObjectAlignment(FrameIdx));
755   NewMIs.back()->addMemOperand(MF, MMO);
756 }
757
758 bool
759 PPCInstrInfo::LoadRegFromStackSlot(MachineFunction &MF, DebugLoc DL,
760                                    unsigned DestReg, int FrameIdx,
761                                    const TargetRegisterClass *RC,
762                                    SmallVectorImpl<MachineInstr*> &NewMIs,
763                                    bool &NonRI, bool &SpillsVRS) const{
764   // Note: If additional load instructions are added here,
765   // update isLoadFromStackSlot.
766
767   if (PPC::GPRCRegClass.hasSubClassEq(RC)) {
768     NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LWZ),
769                                                DestReg), FrameIdx));
770   } else if (PPC::G8RCRegClass.hasSubClassEq(RC)) {
771     NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LD), DestReg),
772                                        FrameIdx));
773   } else if (PPC::F8RCRegClass.hasSubClassEq(RC)) {
774     NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFD), DestReg),
775                                        FrameIdx));
776   } else if (PPC::F4RCRegClass.hasSubClassEq(RC)) {
777     NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LFS), DestReg),
778                                        FrameIdx));
779   } else if (PPC::CRRCRegClass.hasSubClassEq(RC)) {
780     NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
781                                                get(PPC::RESTORE_CR), DestReg),
782                                        FrameIdx));
783     return true;
784   } else if (PPC::CRBITRCRegClass.hasSubClassEq(RC)) {
785
786     unsigned Reg = 0;
787     if (DestReg == PPC::CR0LT || DestReg == PPC::CR0GT ||
788         DestReg == PPC::CR0EQ || DestReg == PPC::CR0UN)
789       Reg = PPC::CR0;
790     else if (DestReg == PPC::CR1LT || DestReg == PPC::CR1GT ||
791              DestReg == PPC::CR1EQ || DestReg == PPC::CR1UN)
792       Reg = PPC::CR1;
793     else if (DestReg == PPC::CR2LT || DestReg == PPC::CR2GT ||
794              DestReg == PPC::CR2EQ || DestReg == PPC::CR2UN)
795       Reg = PPC::CR2;
796     else if (DestReg == PPC::CR3LT || DestReg == PPC::CR3GT ||
797              DestReg == PPC::CR3EQ || DestReg == PPC::CR3UN)
798       Reg = PPC::CR3;
799     else if (DestReg == PPC::CR4LT || DestReg == PPC::CR4GT ||
800              DestReg == PPC::CR4EQ || DestReg == PPC::CR4UN)
801       Reg = PPC::CR4;
802     else if (DestReg == PPC::CR5LT || DestReg == PPC::CR5GT ||
803              DestReg == PPC::CR5EQ || DestReg == PPC::CR5UN)
804       Reg = PPC::CR5;
805     else if (DestReg == PPC::CR6LT || DestReg == PPC::CR6GT ||
806              DestReg == PPC::CR6EQ || DestReg == PPC::CR6UN)
807       Reg = PPC::CR6;
808     else if (DestReg == PPC::CR7LT || DestReg == PPC::CR7GT ||
809              DestReg == PPC::CR7EQ || DestReg == PPC::CR7UN)
810       Reg = PPC::CR7;
811
812     return LoadRegFromStackSlot(MF, DL, Reg, FrameIdx,
813                                 &PPC::CRRCRegClass, NewMIs, NonRI, SpillsVRS);
814
815   } else if (PPC::VRRCRegClass.hasSubClassEq(RC)) {
816     NewMIs.push_back(addFrameReference(BuildMI(MF, DL, get(PPC::LVX), DestReg),
817                                        FrameIdx));
818     NonRI = true;
819   } else if (PPC::VRSAVERCRegClass.hasSubClassEq(RC)) {
820     assert(TM.getSubtargetImpl()->isDarwin() &&
821            "VRSAVE only needs spill/restore on Darwin");
822     NewMIs.push_back(addFrameReference(BuildMI(MF, DL,
823                                                get(PPC::RESTORE_VRSAVE),
824                                                DestReg),
825                                        FrameIdx));
826     SpillsVRS = true;
827   } else {
828     llvm_unreachable("Unknown regclass!");
829   }
830
831   return false;
832 }
833
834 void
835 PPCInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB,
836                                    MachineBasicBlock::iterator MI,
837                                    unsigned DestReg, int FrameIdx,
838                                    const TargetRegisterClass *RC,
839                                    const TargetRegisterInfo *TRI) const {
840   MachineFunction &MF = *MBB.getParent();
841   SmallVector<MachineInstr*, 4> NewMIs;
842   DebugLoc DL;
843   if (MI != MBB.end()) DL = MI->getDebugLoc();
844
845   PPCFunctionInfo *FuncInfo = MF.getInfo<PPCFunctionInfo>();
846   FuncInfo->setHasSpills();
847
848   bool NonRI = false, SpillsVRS = false;
849   if (LoadRegFromStackSlot(MF, DL, DestReg, FrameIdx, RC, NewMIs,
850                            NonRI, SpillsVRS))
851     FuncInfo->setSpillsCR();
852
853   if (SpillsVRS)
854     FuncInfo->setSpillsVRSAVE();
855
856   if (NonRI)
857     FuncInfo->setHasNonRISpills();
858
859   for (unsigned i = 0, e = NewMIs.size(); i != e; ++i)
860     MBB.insert(MI, NewMIs[i]);
861
862   const MachineFrameInfo &MFI = *MF.getFrameInfo();
863   MachineMemOperand *MMO =
864     MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FrameIdx),
865                             MachineMemOperand::MOLoad,
866                             MFI.getObjectSize(FrameIdx),
867                             MFI.getObjectAlignment(FrameIdx));
868   NewMIs.back()->addMemOperand(MF, MMO);
869 }
870
871 bool PPCInstrInfo::
872 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const {
873   assert(Cond.size() == 2 && "Invalid PPC branch opcode!");
874   if (Cond[1].getReg() == PPC::CTR8 || Cond[1].getReg() == PPC::CTR)
875     Cond[0].setImm(Cond[0].getImm() == 0 ? 1 : 0);
876   else
877     // Leave the CR# the same, but invert the condition.
878     Cond[0].setImm(PPC::InvertPredicate((PPC::Predicate)Cond[0].getImm()));
879   return false;
880 }
881
882 bool PPCInstrInfo::FoldImmediate(MachineInstr *UseMI, MachineInstr *DefMI,
883                              unsigned Reg, MachineRegisterInfo *MRI) const {
884   // For some instructions, it is legal to fold ZERO into the RA register field.
885   // A zero immediate should always be loaded with a single li.
886   unsigned DefOpc = DefMI->getOpcode();
887   if (DefOpc != PPC::LI && DefOpc != PPC::LI8)
888     return false;
889   if (!DefMI->getOperand(1).isImm())
890     return false;
891   if (DefMI->getOperand(1).getImm() != 0)
892     return false;
893
894   // Note that we cannot here invert the arguments of an isel in order to fold
895   // a ZERO into what is presented as the second argument. All we have here
896   // is the condition bit, and that might come from a CR-logical bit operation.
897
898   const MCInstrDesc &UseMCID = UseMI->getDesc();
899
900   // Only fold into real machine instructions.
901   if (UseMCID.isPseudo())
902     return false;
903
904   unsigned UseIdx;
905   for (UseIdx = 0; UseIdx < UseMI->getNumOperands(); ++UseIdx)
906     if (UseMI->getOperand(UseIdx).isReg() &&
907         UseMI->getOperand(UseIdx).getReg() == Reg)
908       break;
909
910   assert(UseIdx < UseMI->getNumOperands() && "Cannot find Reg in UseMI");
911   assert(UseIdx < UseMCID.getNumOperands() && "No operand description for Reg");
912
913   const MCOperandInfo *UseInfo = &UseMCID.OpInfo[UseIdx];
914
915   // We can fold the zero if this register requires a GPRC_NOR0/G8RC_NOX0
916   // register (which might also be specified as a pointer class kind).
917   if (UseInfo->isLookupPtrRegClass()) {
918     if (UseInfo->RegClass /* Kind */ != 1)
919       return false;
920   } else {
921     if (UseInfo->RegClass != PPC::GPRC_NOR0RegClassID &&
922         UseInfo->RegClass != PPC::G8RC_NOX0RegClassID)
923       return false;
924   }
925
926   // Make sure this is not tied to an output register (or otherwise
927   // constrained). This is true for ST?UX registers, for example, which
928   // are tied to their output registers.
929   if (UseInfo->Constraints != 0)
930     return false;
931
932   unsigned ZeroReg;
933   if (UseInfo->isLookupPtrRegClass()) {
934     bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
935     ZeroReg = isPPC64 ? PPC::ZERO8 : PPC::ZERO;
936   } else {
937     ZeroReg = UseInfo->RegClass == PPC::G8RC_NOX0RegClassID ?
938               PPC::ZERO8 : PPC::ZERO;
939   }
940
941   bool DeleteDef = MRI->hasOneNonDBGUse(Reg);
942   UseMI->getOperand(UseIdx).setReg(ZeroReg);
943
944   if (DeleteDef)
945     DefMI->eraseFromParent();
946
947   return true;
948 }
949
950 static bool MBBDefinesCTR(MachineBasicBlock &MBB) {
951   for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
952        I != IE; ++I)
953     if (I->definesRegister(PPC::CTR) || I->definesRegister(PPC::CTR8))
954       return true;
955   return false;
956 }
957
958 // We should make sure that, if we're going to predicate both sides of a
959 // condition (a diamond), that both sides don't define the counter register. We
960 // can predicate counter-decrement-based branches, but while that predicates
961 // the branching, it does not predicate the counter decrement. If we tried to
962 // merge the triangle into one predicated block, we'd decrement the counter
963 // twice.
964 bool PPCInstrInfo::isProfitableToIfCvt(MachineBasicBlock &TMBB,
965                      unsigned NumT, unsigned ExtraT,
966                      MachineBasicBlock &FMBB,
967                      unsigned NumF, unsigned ExtraF,
968                      const BranchProbability &Probability) const {
969   return !(MBBDefinesCTR(TMBB) && MBBDefinesCTR(FMBB));
970 }
971
972
973 bool PPCInstrInfo::isPredicated(const MachineInstr *MI) const {
974   // The predicated branches are identified by their type, not really by the
975   // explicit presence of a predicate. Furthermore, some of them can be
976   // predicated more than once. Because if conversion won't try to predicate
977   // any instruction which already claims to be predicated (by returning true
978   // here), always return false. In doing so, we let isPredicable() be the
979   // final word on whether not the instruction can be (further) predicated.
980
981   return false;
982 }
983
984 bool PPCInstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
985   if (!MI->isTerminator())
986     return false;
987
988   // Conditional branch is a special case.
989   if (MI->isBranch() && !MI->isBarrier())
990     return true;
991
992   return !isPredicated(MI);
993 }
994
995 bool PPCInstrInfo::PredicateInstruction(
996                      MachineInstr *MI,
997                      const SmallVectorImpl<MachineOperand> &Pred) const {
998   unsigned OpC = MI->getOpcode();
999   if (OpC == PPC::BLR) {
1000     if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
1001       bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
1002       MI->setDesc(get(Pred[0].getImm() ?
1003                       (isPPC64 ? PPC::BDNZLR8 : PPC::BDNZLR) :
1004                       (isPPC64 ? PPC::BDZLR8  : PPC::BDZLR)));
1005     } else {
1006       MI->setDesc(get(PPC::BCLR));
1007       MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1008         .addImm(Pred[0].getImm())
1009         .addReg(Pred[1].getReg());
1010     }
1011
1012     return true;
1013   } else if (OpC == PPC::B) {
1014     if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR) {
1015       bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
1016       MI->setDesc(get(Pred[0].getImm() ?
1017                       (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
1018                       (isPPC64 ? PPC::BDZ8  : PPC::BDZ)));
1019     } else {
1020       MachineBasicBlock *MBB = MI->getOperand(0).getMBB();
1021       MI->RemoveOperand(0);
1022
1023       MI->setDesc(get(PPC::BCC));
1024       MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1025         .addImm(Pred[0].getImm())
1026         .addReg(Pred[1].getReg())
1027         .addMBB(MBB);
1028     }
1029
1030     return true;
1031   } else if (OpC == PPC::BCTR  || OpC == PPC::BCTR8 ||
1032              OpC == PPC::BCTRL || OpC == PPC::BCTRL8) {
1033     if (Pred[1].getReg() == PPC::CTR8 || Pred[1].getReg() == PPC::CTR)
1034       llvm_unreachable("Cannot predicate bctr[l] on the ctr register");
1035
1036     bool setLR = OpC == PPC::BCTRL || OpC == PPC::BCTRL8;
1037     bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
1038     MI->setDesc(get(isPPC64 ? (setLR ? PPC::BCCTRL8 : PPC::BCCTR8) :
1039                               (setLR ? PPC::BCCTRL  : PPC::BCCTR)));
1040     MachineInstrBuilder(*MI->getParent()->getParent(), MI)
1041       .addImm(Pred[0].getImm())
1042       .addReg(Pred[1].getReg());
1043     return true;
1044   }
1045
1046   return false;
1047 }
1048
1049 bool PPCInstrInfo::SubsumesPredicate(
1050                      const SmallVectorImpl<MachineOperand> &Pred1,
1051                      const SmallVectorImpl<MachineOperand> &Pred2) const {
1052   assert(Pred1.size() == 2 && "Invalid PPC first predicate");
1053   assert(Pred2.size() == 2 && "Invalid PPC second predicate");
1054
1055   if (Pred1[1].getReg() == PPC::CTR8 || Pred1[1].getReg() == PPC::CTR)
1056     return false;
1057   if (Pred2[1].getReg() == PPC::CTR8 || Pred2[1].getReg() == PPC::CTR)
1058     return false;
1059
1060   // P1 can only subsume P2 if they test the same condition register.
1061   if (Pred1[1].getReg() != Pred2[1].getReg())
1062     return false;
1063
1064   PPC::Predicate P1 = (PPC::Predicate) Pred1[0].getImm();
1065   PPC::Predicate P2 = (PPC::Predicate) Pred2[0].getImm();
1066
1067   if (P1 == P2)
1068     return true;
1069
1070   // Does P1 subsume P2, e.g. GE subsumes GT.
1071   if (P1 == PPC::PRED_LE &&
1072       (P2 == PPC::PRED_LT || P2 == PPC::PRED_EQ))
1073     return true;
1074   if (P1 == PPC::PRED_GE &&
1075       (P2 == PPC::PRED_GT || P2 == PPC::PRED_EQ))
1076     return true;
1077
1078   return false;
1079 }
1080
1081 bool PPCInstrInfo::DefinesPredicate(MachineInstr *MI,
1082                                     std::vector<MachineOperand> &Pred) const {
1083   // Note: At the present time, the contents of Pred from this function is
1084   // unused by IfConversion. This implementation follows ARM by pushing the
1085   // CR-defining operand. Because the 'DZ' and 'DNZ' count as types of
1086   // predicate, instructions defining CTR or CTR8 are also included as
1087   // predicate-defining instructions.
1088
1089   const TargetRegisterClass *RCs[] =
1090     { &PPC::CRRCRegClass, &PPC::CRBITRCRegClass,
1091       &PPC::CTRRCRegClass, &PPC::CTRRC8RegClass };
1092
1093   bool Found = false;
1094   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
1095     const MachineOperand &MO = MI->getOperand(i);
1096     for (unsigned c = 0; c < array_lengthof(RCs) && !Found; ++c) {
1097       const TargetRegisterClass *RC = RCs[c];
1098       if (MO.isReg()) {
1099         if (MO.isDef() && RC->contains(MO.getReg())) {
1100           Pred.push_back(MO);
1101           Found = true;
1102         }
1103       } else if (MO.isRegMask()) {
1104         for (TargetRegisterClass::iterator I = RC->begin(),
1105              IE = RC->end(); I != IE; ++I)
1106           if (MO.clobbersPhysReg(*I)) {
1107             Pred.push_back(MO);
1108             Found = true;
1109           }
1110       }
1111     }
1112   }
1113
1114   return Found;
1115 }
1116
1117 bool PPCInstrInfo::isPredicable(MachineInstr *MI) const {
1118   unsigned OpC = MI->getOpcode();
1119   switch (OpC) {
1120   default:
1121     return false;
1122   case PPC::B:
1123   case PPC::BLR:
1124   case PPC::BCTR:
1125   case PPC::BCTR8:
1126   case PPC::BCTRL:
1127   case PPC::BCTRL8:
1128     return true;
1129   }
1130 }
1131
1132 bool PPCInstrInfo::analyzeCompare(const MachineInstr *MI,
1133                                   unsigned &SrcReg, unsigned &SrcReg2,
1134                                   int &Mask, int &Value) const {
1135   unsigned Opc = MI->getOpcode();
1136
1137   switch (Opc) {
1138   default: return false;
1139   case PPC::CMPWI:
1140   case PPC::CMPLWI:
1141   case PPC::CMPDI:
1142   case PPC::CMPLDI:
1143     SrcReg = MI->getOperand(1).getReg();
1144     SrcReg2 = 0;
1145     Value = MI->getOperand(2).getImm();
1146     Mask = 0xFFFF;
1147     return true;
1148   case PPC::CMPW:
1149   case PPC::CMPLW:
1150   case PPC::CMPD:
1151   case PPC::CMPLD:
1152   case PPC::FCMPUS:
1153   case PPC::FCMPUD:
1154     SrcReg = MI->getOperand(1).getReg();
1155     SrcReg2 = MI->getOperand(2).getReg();
1156     return true;
1157   }
1158 }
1159
1160 bool PPCInstrInfo::optimizeCompareInstr(MachineInstr *CmpInstr,
1161                                         unsigned SrcReg, unsigned SrcReg2,
1162                                         int Mask, int Value,
1163                                         const MachineRegisterInfo *MRI) const {
1164   if (DisableCmpOpt)
1165     return false;
1166
1167   int OpC = CmpInstr->getOpcode();
1168   unsigned CRReg = CmpInstr->getOperand(0).getReg();
1169
1170   // FP record forms set CR1 based on the execption status bits, not a
1171   // comparison with zero.
1172   if (OpC == PPC::FCMPUS || OpC == PPC::FCMPUD)
1173     return false;
1174
1175   // The record forms set the condition register based on a signed comparison
1176   // with zero (so says the ISA manual). This is not as straightforward as it
1177   // seems, however, because this is always a 64-bit comparison on PPC64, even
1178   // for instructions that are 32-bit in nature (like slw for example).
1179   // So, on PPC32, for unsigned comparisons, we can use the record forms only
1180   // for equality checks (as those don't depend on the sign). On PPC64,
1181   // we are restricted to equality for unsigned 64-bit comparisons and for
1182   // signed 32-bit comparisons the applicability is more restricted.
1183   bool isPPC64 = TM.getSubtargetImpl()->isPPC64();
1184   bool is32BitSignedCompare   = OpC ==  PPC::CMPWI || OpC == PPC::CMPW;
1185   bool is32BitUnsignedCompare = OpC == PPC::CMPLWI || OpC == PPC::CMPLW;
1186   bool is64BitUnsignedCompare = OpC == PPC::CMPLDI || OpC == PPC::CMPLD;
1187
1188   // Get the unique definition of SrcReg.
1189   MachineInstr *MI = MRI->getUniqueVRegDef(SrcReg);
1190   if (!MI) return false;
1191   int MIOpC = MI->getOpcode();
1192
1193   bool equalityOnly = false;
1194   bool noSub = false;
1195   if (isPPC64) {
1196     if (is32BitSignedCompare) {
1197       // We can perform this optimization only if MI is sign-extending.
1198       if (MIOpC == PPC::SRAW  || MIOpC == PPC::SRAWo ||
1199           MIOpC == PPC::SRAWI || MIOpC == PPC::SRAWIo ||
1200           MIOpC == PPC::EXTSB || MIOpC == PPC::EXTSBo ||
1201           MIOpC == PPC::EXTSH || MIOpC == PPC::EXTSHo ||
1202           MIOpC == PPC::EXTSW || MIOpC == PPC::EXTSWo) {
1203         noSub = true;
1204       } else
1205         return false;
1206     } else if (is32BitUnsignedCompare) {
1207       // We can perform this optimization, equality only, if MI is
1208       // zero-extending.
1209       if (MIOpC == PPC::CNTLZW || MIOpC == PPC::CNTLZWo ||
1210           MIOpC == PPC::SLW    || MIOpC == PPC::SLWo ||
1211           MIOpC == PPC::SRW    || MIOpC == PPC::SRWo) {
1212         noSub = true;
1213         equalityOnly = true;
1214       } else
1215         return false;
1216     } else
1217       equalityOnly = is64BitUnsignedCompare;
1218   } else
1219     equalityOnly = is32BitUnsignedCompare;
1220
1221   if (equalityOnly) {
1222     // We need to check the uses of the condition register in order to reject
1223     // non-equality comparisons.
1224     for (MachineRegisterInfo::use_iterator I = MRI->use_begin(CRReg),
1225          IE = MRI->use_end(); I != IE; ++I) {
1226       MachineInstr *UseMI = &*I;
1227       if (UseMI->getOpcode() == PPC::BCC) {
1228         unsigned Pred = UseMI->getOperand(0).getImm();
1229         if (Pred != PPC::PRED_EQ && Pred != PPC::PRED_NE)
1230           return false;
1231       } else if (UseMI->getOpcode() == PPC::ISEL ||
1232                  UseMI->getOpcode() == PPC::ISEL8) {
1233         unsigned SubIdx = UseMI->getOperand(3).getSubReg();
1234         if (SubIdx != PPC::sub_eq)
1235           return false;
1236       } else
1237         return false;
1238     }
1239   }
1240
1241   MachineBasicBlock::iterator I = CmpInstr;
1242
1243   // Scan forward to find the first use of the compare.
1244   for (MachineBasicBlock::iterator EL = CmpInstr->getParent()->end();
1245        I != EL; ++I) {
1246     bool FoundUse = false;
1247     for (MachineRegisterInfo::use_iterator J = MRI->use_begin(CRReg),
1248          JE = MRI->use_end(); J != JE; ++J)
1249       if (&*J == &*I) {
1250         FoundUse = true;
1251         break;
1252       }
1253
1254     if (FoundUse)
1255       break;
1256   }
1257
1258   // There are two possible candidates which can be changed to set CR[01].
1259   // One is MI, the other is a SUB instruction.
1260   // For CMPrr(r1,r2), we are looking for SUB(r1,r2) or SUB(r2,r1).
1261   MachineInstr *Sub = NULL;
1262   if (SrcReg2 != 0)
1263     // MI is not a candidate for CMPrr.
1264     MI = NULL;
1265   // FIXME: Conservatively refuse to convert an instruction which isn't in the
1266   // same BB as the comparison. This is to allow the check below to avoid calls
1267   // (and other explicit clobbers); instead we should really check for these
1268   // more explicitly (in at least a few predecessors).
1269   else if (MI->getParent() != CmpInstr->getParent() || Value != 0) {
1270     // PPC does not have a record-form SUBri.
1271     return false;
1272   }
1273
1274   // Search for Sub.
1275   const TargetRegisterInfo *TRI = &getRegisterInfo();
1276   --I;
1277
1278   // Get ready to iterate backward from CmpInstr.
1279   MachineBasicBlock::iterator E = MI,
1280                               B = CmpInstr->getParent()->begin();
1281
1282   for (; I != E && !noSub; --I) {
1283     const MachineInstr &Instr = *I;
1284     unsigned IOpC = Instr.getOpcode();
1285
1286     if (&*I != CmpInstr && (
1287         Instr.modifiesRegister(PPC::CR0, TRI) ||
1288         Instr.readsRegister(PPC::CR0, TRI)))
1289       // This instruction modifies or uses the record condition register after
1290       // the one we want to change. While we could do this transformation, it
1291       // would likely not be profitable. This transformation removes one
1292       // instruction, and so even forcing RA to generate one move probably
1293       // makes it unprofitable.
1294       return false;
1295
1296     // Check whether CmpInstr can be made redundant by the current instruction.
1297     if ((OpC == PPC::CMPW || OpC == PPC::CMPLW ||
1298          OpC == PPC::CMPD || OpC == PPC::CMPLD) &&
1299         (IOpC == PPC::SUBF || IOpC == PPC::SUBF8) &&
1300         ((Instr.getOperand(1).getReg() == SrcReg &&
1301           Instr.getOperand(2).getReg() == SrcReg2) ||
1302         (Instr.getOperand(1).getReg() == SrcReg2 &&
1303          Instr.getOperand(2).getReg() == SrcReg))) {
1304       Sub = &*I;
1305       break;
1306     }
1307
1308     if (I == B)
1309       // The 'and' is below the comparison instruction.
1310       return false;
1311   }
1312
1313   // Return false if no candidates exist.
1314   if (!MI && !Sub)
1315     return false;
1316
1317   // The single candidate is called MI.
1318   if (!MI) MI = Sub;
1319
1320   int NewOpC = -1;
1321   MIOpC = MI->getOpcode();
1322   if (MIOpC == PPC::ANDIo || MIOpC == PPC::ANDIo8)
1323     NewOpC = MIOpC;
1324   else {
1325     NewOpC = PPC::getRecordFormOpcode(MIOpC);
1326     if (NewOpC == -1 && PPC::getNonRecordFormOpcode(MIOpC) != -1)
1327       NewOpC = MIOpC;
1328   }
1329
1330   // FIXME: On the non-embedded POWER architectures, only some of the record
1331   // forms are fast, and we should use only the fast ones.
1332
1333   // The defining instruction has a record form (or is already a record
1334   // form). It is possible, however, that we'll need to reverse the condition
1335   // code of the users.
1336   if (NewOpC == -1)
1337     return false;
1338
1339   SmallVector<std::pair<MachineOperand*, PPC::Predicate>, 4> PredsToUpdate;
1340   SmallVector<std::pair<MachineOperand*, unsigned>, 4> SubRegsToUpdate;
1341
1342   // If we have SUB(r1, r2) and CMP(r2, r1), the condition code based on CMP
1343   // needs to be updated to be based on SUB.  Push the condition code
1344   // operands to OperandsToUpdate.  If it is safe to remove CmpInstr, the
1345   // condition code of these operands will be modified.
1346   bool ShouldSwap = false;
1347   if (Sub) {
1348     ShouldSwap = SrcReg2 != 0 && Sub->getOperand(1).getReg() == SrcReg2 &&
1349       Sub->getOperand(2).getReg() == SrcReg;
1350
1351     // The operands to subf are the opposite of sub, so only in the fixed-point
1352     // case, invert the order.
1353     ShouldSwap = !ShouldSwap;
1354   }
1355
1356   if (ShouldSwap)
1357     for (MachineRegisterInfo::use_iterator I = MRI->use_begin(CRReg),
1358          IE = MRI->use_end(); I != IE; ++I) {
1359       MachineInstr *UseMI = &*I;
1360       if (UseMI->getOpcode() == PPC::BCC) {
1361         PPC::Predicate Pred = (PPC::Predicate) UseMI->getOperand(0).getImm();
1362         assert((!equalityOnly ||
1363                 Pred == PPC::PRED_EQ || Pred == PPC::PRED_NE) &&
1364                "Invalid predicate for equality-only optimization");
1365         PredsToUpdate.push_back(std::make_pair(&((*I).getOperand(0)),
1366                                 PPC::getSwappedPredicate(Pred)));
1367       } else if (UseMI->getOpcode() == PPC::ISEL ||
1368                  UseMI->getOpcode() == PPC::ISEL8) {
1369         unsigned NewSubReg = UseMI->getOperand(3).getSubReg();
1370         assert((!equalityOnly || NewSubReg == PPC::sub_eq) &&
1371                "Invalid CR bit for equality-only optimization");
1372
1373         if (NewSubReg == PPC::sub_lt)
1374           NewSubReg = PPC::sub_gt;
1375         else if (NewSubReg == PPC::sub_gt)
1376           NewSubReg = PPC::sub_lt;
1377
1378         SubRegsToUpdate.push_back(std::make_pair(&((*I).getOperand(3)),
1379                                                  NewSubReg));
1380       } else // We need to abort on a user we don't understand.
1381         return false;
1382     }
1383
1384   // Create a new virtual register to hold the value of the CR set by the
1385   // record-form instruction. If the instruction was not previously in
1386   // record form, then set the kill flag on the CR.
1387   CmpInstr->eraseFromParent();
1388
1389   MachineBasicBlock::iterator MII = MI;
1390   BuildMI(*MI->getParent(), llvm::next(MII), MI->getDebugLoc(),
1391           get(TargetOpcode::COPY), CRReg)
1392     .addReg(PPC::CR0, MIOpC != NewOpC ? RegState::Kill : 0);
1393
1394   if (MIOpC != NewOpC) {
1395     // We need to be careful here: we're replacing one instruction with
1396     // another, and we need to make sure that we get all of the right
1397     // implicit uses and defs. On the other hand, the caller may be holding
1398     // an iterator to this instruction, and so we can't delete it (this is
1399     // specifically the case if this is the instruction directly after the
1400     // compare).
1401
1402     const MCInstrDesc &NewDesc = get(NewOpC);
1403     MI->setDesc(NewDesc);
1404
1405     if (NewDesc.ImplicitDefs)
1406       for (const uint16_t *ImpDefs = NewDesc.getImplicitDefs();
1407            *ImpDefs; ++ImpDefs)
1408         if (!MI->definesRegister(*ImpDefs))
1409           MI->addOperand(*MI->getParent()->getParent(),
1410                          MachineOperand::CreateReg(*ImpDefs, true, true));
1411     if (NewDesc.ImplicitUses)
1412       for (const uint16_t *ImpUses = NewDesc.getImplicitUses();
1413            *ImpUses; ++ImpUses)
1414         if (!MI->readsRegister(*ImpUses))
1415           MI->addOperand(*MI->getParent()->getParent(),
1416                          MachineOperand::CreateReg(*ImpUses, false, true));
1417   }
1418
1419   // Modify the condition code of operands in OperandsToUpdate.
1420   // Since we have SUB(r1, r2) and CMP(r2, r1), the condition code needs to
1421   // be changed from r2 > r1 to r1 < r2, from r2 < r1 to r1 > r2, etc.
1422   for (unsigned i = 0, e = PredsToUpdate.size(); i < e; i++)
1423     PredsToUpdate[i].first->setImm(PredsToUpdate[i].second);
1424
1425   for (unsigned i = 0, e = SubRegsToUpdate.size(); i < e; i++)
1426     SubRegsToUpdate[i].first->setSubReg(SubRegsToUpdate[i].second);
1427
1428   return true;
1429 }
1430
1431 /// GetInstSize - Return the number of bytes of code the specified
1432 /// instruction may be.  This returns the maximum number of bytes.
1433 ///
1434 unsigned PPCInstrInfo::GetInstSizeInBytes(const MachineInstr *MI) const {
1435   switch (MI->getOpcode()) {
1436   case PPC::INLINEASM: {       // Inline Asm: Variable size.
1437     const MachineFunction *MF = MI->getParent()->getParent();
1438     const char *AsmStr = MI->getOperand(0).getSymbolName();
1439     return getInlineAsmLength(AsmStr, *MF->getTarget().getMCAsmInfo());
1440   }
1441   case PPC::PROLOG_LABEL:
1442   case PPC::EH_LABEL:
1443   case PPC::GC_LABEL:
1444   case PPC::DBG_VALUE:
1445     return 0;
1446   case PPC::BL8_NOP:
1447   case PPC::BLA8_NOP:
1448     return 8;
1449   default:
1450     return 4; // PowerPC instructions are all 4 bytes
1451   }
1452 }
1453
1454 #undef DEBUG_TYPE
1455 #define DEBUG_TYPE "ppc-early-ret"
1456 STATISTIC(NumBCLR, "Number of early conditional returns");
1457 STATISTIC(NumBLR,  "Number of early returns");
1458
1459 namespace llvm {
1460   void initializePPCEarlyReturnPass(PassRegistry&);
1461 }
1462
1463 namespace {
1464   // PPCEarlyReturn pass - For simple functions without epilogue code, move
1465   // returns up, and create conditional returns, to avoid unnecessary
1466   // branch-to-blr sequences.
1467   struct PPCEarlyReturn : public MachineFunctionPass {
1468     static char ID;
1469     PPCEarlyReturn() : MachineFunctionPass(ID) {
1470       initializePPCEarlyReturnPass(*PassRegistry::getPassRegistry());
1471     }
1472
1473     const PPCTargetMachine *TM;
1474     const PPCInstrInfo *TII;
1475
1476 protected:
1477     bool processBlock(MachineBasicBlock &ReturnMBB) {
1478       bool Changed = false;
1479
1480       MachineBasicBlock::iterator I = ReturnMBB.begin();
1481       I = ReturnMBB.SkipPHIsAndLabels(I);
1482
1483       // The block must be essentially empty except for the blr.
1484       if (I == ReturnMBB.end() || I->getOpcode() != PPC::BLR ||
1485           I != ReturnMBB.getLastNonDebugInstr())
1486         return Changed;
1487
1488       SmallVector<MachineBasicBlock*, 8> PredToRemove;
1489       for (MachineBasicBlock::pred_iterator PI = ReturnMBB.pred_begin(),
1490            PIE = ReturnMBB.pred_end(); PI != PIE; ++PI) {
1491         bool OtherReference = false, BlockChanged = false;
1492         for (MachineBasicBlock::iterator J = (*PI)->getLastNonDebugInstr();;) {
1493           if (J->getOpcode() == PPC::B) {
1494             if (J->getOperand(0).getMBB() == &ReturnMBB) {
1495               // This is an unconditional branch to the return. Replace the
1496               // branch with a blr.
1497               BuildMI(**PI, J, J->getDebugLoc(), TII->get(PPC::BLR));
1498               MachineBasicBlock::iterator K = J--;
1499               K->eraseFromParent();
1500               BlockChanged = true;
1501               ++NumBLR;
1502               continue;
1503             }
1504           } else if (J->getOpcode() == PPC::BCC) {
1505             if (J->getOperand(2).getMBB() == &ReturnMBB) {
1506               // This is a conditional branch to the return. Replace the branch
1507               // with a bclr.
1508               BuildMI(**PI, J, J->getDebugLoc(), TII->get(PPC::BCLR))
1509                 .addImm(J->getOperand(0).getImm())
1510                 .addReg(J->getOperand(1).getReg());
1511               MachineBasicBlock::iterator K = J--;
1512               K->eraseFromParent();
1513               BlockChanged = true;
1514               ++NumBCLR;
1515               continue;
1516             }
1517           } else if (J->isBranch()) {
1518             if (J->isIndirectBranch()) {
1519               if (ReturnMBB.hasAddressTaken())
1520                 OtherReference = true;
1521             } else
1522               for (unsigned i = 0; i < J->getNumOperands(); ++i)
1523                 if (J->getOperand(i).isMBB() &&
1524                     J->getOperand(i).getMBB() == &ReturnMBB)
1525                   OtherReference = true;
1526           } else if (!J->isTerminator() && !J->isDebugValue())
1527             break;
1528
1529           if (J == (*PI)->begin())
1530             break;
1531
1532           --J;
1533         }
1534
1535         if ((*PI)->canFallThrough() && (*PI)->isLayoutSuccessor(&ReturnMBB))
1536           OtherReference = true;
1537
1538         // Predecessors are stored in a vector and can't be removed here.
1539         if (!OtherReference && BlockChanged) {
1540           PredToRemove.push_back(*PI);
1541         }
1542
1543         if (BlockChanged)
1544           Changed = true;
1545       }
1546
1547       for (unsigned i = 0, ie = PredToRemove.size(); i != ie; ++i)
1548         PredToRemove[i]->removeSuccessor(&ReturnMBB);
1549
1550       if (Changed && !ReturnMBB.hasAddressTaken()) {
1551         // We now might be able to merge this blr-only block into its
1552         // by-layout predecessor.
1553         if (ReturnMBB.pred_size() == 1 &&
1554             (*ReturnMBB.pred_begin())->isLayoutSuccessor(&ReturnMBB)) {
1555           // Move the blr into the preceding block.
1556           MachineBasicBlock &PrevMBB = **ReturnMBB.pred_begin();
1557           PrevMBB.splice(PrevMBB.end(), &ReturnMBB, I);
1558           PrevMBB.removeSuccessor(&ReturnMBB);
1559         }
1560
1561         if (ReturnMBB.pred_empty())
1562           ReturnMBB.eraseFromParent();
1563       }
1564
1565       return Changed;
1566     }
1567
1568 public:
1569     virtual bool runOnMachineFunction(MachineFunction &MF) {
1570       TM = static_cast<const PPCTargetMachine *>(&MF.getTarget());
1571       TII = TM->getInstrInfo();
1572
1573       bool Changed = false;
1574
1575       // If the function does not have at least two blocks, then there is
1576       // nothing to do.
1577       if (MF.size() < 2)
1578         return Changed;
1579
1580       for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
1581         MachineBasicBlock &B = *I++;
1582         if (processBlock(B))
1583           Changed = true;
1584       }
1585
1586       return Changed;
1587     }
1588
1589     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
1590       MachineFunctionPass::getAnalysisUsage(AU);
1591     }
1592   };
1593 }
1594
1595 INITIALIZE_PASS(PPCEarlyReturn, DEBUG_TYPE,
1596                 "PowerPC Early-Return Creation", false, false)
1597
1598 char PPCEarlyReturn::ID = 0;
1599 FunctionPass*
1600 llvm::createPPCEarlyReturnPass() { return new PPCEarlyReturn(); }