Revert accidental commit.
[oota-llvm.git] / lib / Target / PowerPC / PPCCTRLoops.cpp
1 //===-- PPCCTRLoops.cpp - Identify and generate CTR loops -----------------===//
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 pass identifies loops where we can generate the PPC branch instructions
11 // that decrement and test the count register (CTR) (bdnz and friends).
12 // This pass is based on the HexagonHardwareLoops pass.
13 //
14 // The pattern that defines the induction variable can changed depending on
15 // prior optimizations.  For example, the IndVarSimplify phase run by 'opt'
16 // normalizes induction variables, and the Loop Strength Reduction pass
17 // run by 'llc' may also make changes to the induction variable.
18 // The pattern detected by this phase is due to running Strength Reduction.
19 //
20 // Criteria for CTR loops:
21 //  - Countable loops (w/ ind. var for a trip count)
22 //  - Assumes loops are normalized by IndVarSimplify
23 //  - Try inner-most loops first
24 //  - No nested CTR loops.
25 //  - No function calls in loops.
26 //
27 //  Note: As with unconverted loops, PPCBranchSelector must be run after this
28 //  pass in order to convert long-displacement jumps into jump pairs.
29 //
30 //===----------------------------------------------------------------------===//
31
32 #define DEBUG_TYPE "ctrloops"
33 #include "PPC.h"
34 #include "PPCTargetMachine.h"
35 #include "MCTargetDesc/PPCPredicates.h"
36 #include "llvm/Constants.h"
37 #include "llvm/PassSupport.h"
38 #include "llvm/ADT/DenseMap.h"
39 #include "llvm/ADT/Statistic.h"
40 #include "llvm/CodeGen/Passes.h"
41 #include "llvm/CodeGen/MachineDominators.h"
42 #include "llvm/CodeGen/MachineFunction.h"
43 #include "llvm/CodeGen/MachineFunctionPass.h"
44 #include "llvm/CodeGen/MachineInstrBuilder.h"
45 #include "llvm/CodeGen/MachineLoopInfo.h"
46 #include "llvm/CodeGen/MachineRegisterInfo.h"
47 #include "llvm/CodeGen/RegisterScavenging.h"
48 #include "llvm/Support/Debug.h"
49 #include "llvm/Support/raw_ostream.h"
50 #include "llvm/Target/TargetInstrInfo.h"
51 #include <algorithm>
52
53 using namespace llvm;
54
55 STATISTIC(NumCTRLoops, "Number of loops converted to CTR loops");
56
57 namespace {
58   class CountValue;
59   struct PPCCTRLoops : public MachineFunctionPass {
60     MachineLoopInfo       *MLI;
61     MachineRegisterInfo   *MRI;
62     const TargetInstrInfo *TII;
63
64   public:
65     static char ID;   // Pass identification, replacement for typeid
66
67     PPCCTRLoops() : MachineFunctionPass(ID) {}
68
69     virtual bool runOnMachineFunction(MachineFunction &MF);
70
71     const char *getPassName() const { return "PPC CTR Loops"; }
72
73     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
74       AU.setPreservesCFG();
75       AU.addRequired<MachineDominatorTree>();
76       AU.addPreserved<MachineDominatorTree>();
77       AU.addRequired<MachineLoopInfo>();
78       AU.addPreserved<MachineLoopInfo>();
79       MachineFunctionPass::getAnalysisUsage(AU);
80     }
81
82   private:
83     /// getCanonicalInductionVariable - Check to see if the loop has a canonical
84     /// induction variable.
85     /// Should be defined in MachineLoop. Based upon version in class Loop.
86     void getCanonicalInductionVariable(MachineLoop *L,
87                               SmallVector<MachineInstr *, 4> &IVars,
88                               SmallVector<MachineInstr *, 4> &IOps) const;
89
90     /// getTripCount - Return a loop-invariant LLVM register indicating the
91     /// number of times the loop will be executed.  If the trip-count cannot
92     /// be determined, this return null.
93     CountValue *getTripCount(MachineLoop *L,
94                              SmallVector<MachineInstr *, 2> &OldInsts) const;
95
96     /// isInductionOperation - Return true if the instruction matches the
97     /// pattern for an opertion that defines an induction variable.
98     bool isInductionOperation(const MachineInstr *MI, unsigned IVReg) const;
99
100     /// isInvalidOperation - Return true if the instruction is not valid within
101     /// a CTR loop.
102     bool isInvalidLoopOperation(const MachineInstr *MI) const;
103
104     /// containsInavlidInstruction - Return true if the loop contains an
105     /// instruction that inhibits using the CTR loop.
106     bool containsInvalidInstruction(MachineLoop *L) const;
107
108     /// converToCTRLoop - Given a loop, check if we can convert it to a
109     /// CTR loop.  If so, then perform the conversion and return true.
110     bool convertToCTRLoop(MachineLoop *L);
111
112     /// isDead - Return true if the instruction is now dead.
113     bool isDead(const MachineInstr *MI,
114                 SmallVector<MachineInstr *, 1> &DeadPhis) const;
115
116     /// removeIfDead - Remove the instruction if it is now dead.
117     void removeIfDead(MachineInstr *MI);
118   };
119
120   char PPCCTRLoops::ID = 0;
121
122
123   // CountValue class - Abstraction for a trip count of a loop. A
124   // smaller vesrsion of the MachineOperand class without the concerns
125   // of changing the operand representation.
126   class CountValue {
127   public:
128     enum CountValueType {
129       CV_Register,
130       CV_Immediate
131     };
132   private:
133     CountValueType Kind;
134     union Values {
135       unsigned RegNum;
136       int64_t ImmVal;
137       Values(unsigned r) : RegNum(r) {}
138       Values(int64_t i) : ImmVal(i) {}
139     } Contents;
140     bool isNegative;
141
142   public:
143     CountValue(unsigned r, bool neg) : Kind(CV_Register), Contents(r),
144                                        isNegative(neg) {}
145     explicit CountValue(int64_t i) : Kind(CV_Immediate), Contents(i),
146                                      isNegative(i < 0) {}
147     CountValueType getType() const { return Kind; }
148     bool isReg() const { return Kind == CV_Register; }
149     bool isImm() const { return Kind == CV_Immediate; }
150     bool isNeg() const { return isNegative; }
151
152     unsigned getReg() const {
153       assert(isReg() && "Wrong CountValue accessor");
154       return Contents.RegNum;
155     }
156     void setReg(unsigned Val) {
157       Contents.RegNum = Val;
158     }
159     int64_t getImm() const {
160       assert(isImm() && "Wrong CountValue accessor");
161       if (isNegative) {
162         return -Contents.ImmVal;
163       }
164       return Contents.ImmVal;
165     }
166     void setImm(int64_t Val) {
167       Contents.ImmVal = Val;
168     }
169
170     void print(raw_ostream &OS, const TargetMachine *TM = 0) const {
171       if (isReg()) { OS << PrintReg(getReg()); }
172       if (isImm()) { OS << getImm(); }
173     }
174   };
175 } // end anonymous namespace
176
177
178 /// isCompareEquals - Returns true if the instruction is a compare equals
179 /// instruction with an immediate operand.
180 static bool isCompareEqualsImm(const MachineInstr *MI, bool &SignedCmp) {
181   if (MI->getOpcode() == PPC::CMPWI || MI->getOpcode() == PPC::CMPDI) {
182     SignedCmp = true;
183     return true;
184   } else if (MI->getOpcode() == PPC::CMPLWI || MI->getOpcode() == PPC::CMPLDI) {
185     SignedCmp = false;
186     return true;
187   }
188
189   return false;
190 }
191
192
193 /// createPPCCTRLoops - Factory for creating
194 /// the CTR loop phase.
195 FunctionPass *llvm::createPPCCTRLoops() {
196   return new PPCCTRLoops();
197 }
198
199
200 bool PPCCTRLoops::runOnMachineFunction(MachineFunction &MF) {
201   DEBUG(dbgs() << "********* PPC CTR Loops *********\n");
202
203   bool Changed = false;
204
205   // get the loop information
206   MLI = &getAnalysis<MachineLoopInfo>();
207   // get the register information
208   MRI = &MF.getRegInfo();
209   // the target specific instructio info.
210   TII = MF.getTarget().getInstrInfo();
211
212   for (MachineLoopInfo::iterator I = MLI->begin(), E = MLI->end();
213        I != E; ++I) {
214     MachineLoop *L = *I;
215     if (!L->getParentLoop()) {
216       Changed |= convertToCTRLoop(L);
217     }
218   }
219
220   return Changed;
221 }
222
223 /// getCanonicalInductionVariable - Check to see if the loop has a canonical
224 /// induction variable. We check for a simple recurrence pattern - an
225 /// integer recurrence that decrements by one each time through the loop and
226 /// ends at zero.  If so, return the phi node that corresponds to it.
227 ///
228 /// Based upon the similar code in LoopInfo except this code is specific to
229 /// the machine.
230 /// This method assumes that the IndVarSimplify pass has been run by 'opt'.
231 ///
232 void
233 PPCCTRLoops::getCanonicalInductionVariable(MachineLoop *L,
234                                   SmallVector<MachineInstr *, 4> &IVars,
235                                   SmallVector<MachineInstr *, 4> &IOps) const {
236   MachineBasicBlock *TopMBB = L->getTopBlock();
237   MachineBasicBlock::pred_iterator PI = TopMBB->pred_begin();
238   assert(PI != TopMBB->pred_end() &&
239          "Loop must have more than one incoming edge!");
240   MachineBasicBlock *Backedge = *PI++;
241   if (PI == TopMBB->pred_end()) return;  // dead loop
242   MachineBasicBlock *Incoming = *PI++;
243   if (PI != TopMBB->pred_end()) return;  // multiple backedges?
244
245   // make sure there is one incoming and one backedge and determine which
246   // is which.
247   if (L->contains(Incoming)) {
248     if (L->contains(Backedge))
249       return;
250     std::swap(Incoming, Backedge);
251   } else if (!L->contains(Backedge))
252     return;
253
254   // Loop over all of the PHI nodes, looking for a canonical induction variable:
255   //   - The PHI node is "reg1 = PHI reg2, BB1, reg3, BB2".
256   //   - The recurrence comes from the backedge.
257   //   - the definition is an induction operatio.n
258   for (MachineBasicBlock::iterator I = TopMBB->begin(), E = TopMBB->end();
259        I != E && I->isPHI(); ++I) {
260     MachineInstr *MPhi = &*I;
261     unsigned DefReg = MPhi->getOperand(0).getReg();
262     for (unsigned i = 1; i != MPhi->getNumOperands(); i += 2) {
263       // Check each operand for the value from the backedge.
264       MachineBasicBlock *MBB = MPhi->getOperand(i+1).getMBB();
265       if (L->contains(MBB)) { // operands comes from the backedge
266         // Check if the definition is an induction operation.
267         MachineInstr *DI = MRI->getVRegDef(MPhi->getOperand(i).getReg());
268         if (isInductionOperation(DI, DefReg)) {
269           IOps.push_back(DI);
270           IVars.push_back(MPhi);
271         }
272       }
273     }
274   }
275   return;
276 }
277
278 /// getTripCount - Return a loop-invariant LLVM value indicating the
279 /// number of times the loop will be executed.  The trip count can
280 /// be either a register or a constant value.  If the trip-count
281 /// cannot be determined, this returns null.
282 ///
283 /// We find the trip count from the phi instruction that defines the
284 /// induction variable.  We follow the links to the CMP instruction
285 /// to get the trip count.
286 ///
287 /// Based upon getTripCount in LoopInfo.
288 ///
289 CountValue *PPCCTRLoops::getTripCount(MachineLoop *L,
290                            SmallVector<MachineInstr *, 2> &OldInsts) const {
291   MachineBasicBlock *LastMBB = L->getExitingBlock();
292   // Don't generate a CTR loop if the loop has more than one exit.
293   if (LastMBB == 0)
294     return 0;
295
296   MachineBasicBlock::iterator LastI = LastMBB->getFirstTerminator();
297   if (LastI->getOpcode() != PPC::BCC)
298     return 0;
299
300   // We need to make sure that this compare is defining the condition
301   // register actually used by the terminating branch.
302
303   unsigned PredReg = LastI->getOperand(1).getReg();
304   DEBUG(dbgs() << "Examining loop with first terminator: " << *LastI);
305
306   unsigned PredCond = LastI->getOperand(0).getImm();
307   if (PredCond != PPC::PRED_EQ && PredCond != PPC::PRED_NE)
308     return 0;
309
310   // Check that the loop has a induction variable.
311   SmallVector<MachineInstr *, 4> IVars, IOps;
312   getCanonicalInductionVariable(L, IVars, IOps);
313   for (unsigned i = 0; i < IVars.size(); ++i) {
314     MachineInstr *IOp = IOps[i];
315     MachineInstr *IV_Inst = IVars[i];
316
317     // Canonical loops will end with a 'cmpwi/cmpdi cr, IV, Imm',
318     //  if Imm is 0, get the count from the PHI opnd
319     //  if Imm is -M, than M is the count
320     //  Otherwise, Imm is the count
321     MachineOperand *IV_Opnd;
322     const MachineOperand *InitialValue;
323     if (!L->contains(IV_Inst->getOperand(2).getMBB())) {
324       InitialValue = &IV_Inst->getOperand(1);
325       IV_Opnd = &IV_Inst->getOperand(3);
326     } else {
327       InitialValue = &IV_Inst->getOperand(3);
328       IV_Opnd = &IV_Inst->getOperand(1);
329     }
330
331     DEBUG(dbgs() << "Considering:\n");
332     DEBUG(dbgs() << "  induction operation: " << *IOp);
333     DEBUG(dbgs() << "  induction variable: " << *IV_Inst);
334     DEBUG(dbgs() << "  initial value: " << *InitialValue << "\n");
335   
336     // Look for the cmp instruction to determine if we
337     // can get a useful trip count.  The trip count can
338     // be either a register or an immediate.  The location
339     // of the value depends upon the type (reg or imm).
340     while ((IV_Opnd = IV_Opnd->getNextOperandForReg())) {
341       bool SignedCmp;
342       MachineInstr *MI = IV_Opnd->getParent();
343       if (L->contains(MI) && isCompareEqualsImm(MI, SignedCmp) &&
344           MI->getOperand(0).getReg() == PredReg) {
345
346         OldInsts.push_back(MI);
347         OldInsts.push_back(IOp);
348  
349         DEBUG(dbgs() << "  compare: " << *MI);
350  
351         const MachineOperand &MO = MI->getOperand(2);
352         assert(MO.isImm() && "IV Cmp Operand should be an immediate");
353
354         int64_t ImmVal;
355         if (SignedCmp)
356           ImmVal = (short) MO.getImm();
357         else
358           ImmVal = MO.getImm();
359   
360         const MachineInstr *IV_DefInstr = MRI->getVRegDef(IV_Opnd->getReg());
361         assert(L->contains(IV_DefInstr->getParent()) &&
362                "IV definition should occurs in loop");
363         int64_t iv_value = (short) IV_DefInstr->getOperand(2).getImm();
364   
365         assert(InitialValue->isReg() && "Expecting register for init value");
366         unsigned InitialValueReg = InitialValue->getReg();
367   
368         const MachineInstr *DefInstr = MRI->getVRegDef(InitialValueReg);
369   
370         // Here we need to look for an immediate load (an li or lis/ori pair).
371         if (DefInstr && (DefInstr->getOpcode() == PPC::ORI8 ||
372                          DefInstr->getOpcode() == PPC::ORI)) {
373           int64_t start = (short) DefInstr->getOperand(2).getImm();
374           const MachineInstr *DefInstr2 =
375             MRI->getVRegDef(DefInstr->getOperand(0).getReg());
376           if (DefInstr2 && (DefInstr2->getOpcode() == PPC::LIS8 ||
377                             DefInstr2->getOpcode() == PPC::LIS)) {
378             DEBUG(dbgs() << "  initial constant: " << *DefInstr);
379             DEBUG(dbgs() << "  initial constant: " << *DefInstr2);
380
381             start |= int64_t(short(DefInstr2->getOperand(1).getImm())) << 16;
382   
383             int64_t count = ImmVal - start;
384             if ((count % iv_value) != 0) {
385               return 0;
386             }
387             return new CountValue(count/iv_value);
388           }
389         } else if (DefInstr && (DefInstr->getOpcode() == PPC::LI8 ||
390                                 DefInstr->getOpcode() == PPC::LI)) {
391           DEBUG(dbgs() << "  initial constant: " << *DefInstr);
392
393           int64_t count = ImmVal - int64_t(short(DefInstr->getOperand(1).getImm()));
394           if ((count % iv_value) != 0) {
395             return 0;
396           }
397           return new CountValue(count/iv_value);
398         } else if (iv_value == 1 || iv_value == -1) {
399           // We can't determine a constant starting value.
400           if (ImmVal == 0) {
401             return new CountValue(InitialValueReg, iv_value > 0);
402           }
403           // FIXME: handle non-zero end value.
404         }
405         // FIXME: handle non-unit increments (we might not want to introduce division
406         // but we can handle some 2^n cases with shifts).
407   
408       }
409     }
410   }
411   return 0;
412 }
413
414 /// isInductionOperation - return true if the operation is matches the
415 /// pattern that defines an induction variable:
416 ///    addi iv, c
417 ///
418 bool
419 PPCCTRLoops::isInductionOperation(const MachineInstr *MI,
420                                            unsigned IVReg) const {
421   return ((MI->getOpcode() == PPC::ADDI || MI->getOpcode() == PPC::ADDI8) &&
422           MI->getOperand(1).isReg() && // could be a frame index instead
423           MI->getOperand(1).getReg() == IVReg);
424 }
425
426 /// isInvalidOperation - Return true if the operation is invalid within
427 /// CTR loop.
428 bool
429 PPCCTRLoops::isInvalidLoopOperation(const MachineInstr *MI) const {
430
431   // call is not allowed because the callee may use a CTR loop
432   if (MI->getDesc().isCall()) {
433     return true;
434   }
435   // check if the instruction defines a CTR loop register
436   // (this will also catch nested CTR loops)
437   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
438     const MachineOperand &MO = MI->getOperand(i);
439     if (MO.isReg() && MO.isDef() &&
440         (MO.getReg() == PPC::CTR || MO.getReg() == PPC::CTR8)) {
441       return true;
442     }
443   }
444   return false;
445 }
446
447 /// containsInvalidInstruction - Return true if the loop contains
448 /// an instruction that inhibits the use of the CTR loop function.
449 ///
450 bool PPCCTRLoops::containsInvalidInstruction(MachineLoop *L) const {
451   const std::vector<MachineBasicBlock*> Blocks = L->getBlocks();
452   for (unsigned i = 0, e = Blocks.size(); i != e; ++i) {
453     MachineBasicBlock *MBB = Blocks[i];
454     for (MachineBasicBlock::iterator
455            MII = MBB->begin(), E = MBB->end(); MII != E; ++MII) {
456       const MachineInstr *MI = &*MII;
457       if (isInvalidLoopOperation(MI)) {
458         return true;
459       }
460     }
461   }
462   return false;
463 }
464
465 /// isDead returns true if the instruction is dead
466 /// (this was essentially copied from DeadMachineInstructionElim::isDead, but
467 /// with special cases for inline asm, physical registers and instructions with
468 /// side effects removed)
469 bool PPCCTRLoops::isDead(const MachineInstr *MI,
470                          SmallVector<MachineInstr *, 1> &DeadPhis) const {
471   // Examine each operand.
472   for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
473     const MachineOperand &MO = MI->getOperand(i);
474     if (MO.isReg() && MO.isDef()) {
475       unsigned Reg = MO.getReg();
476       if (!MRI->use_nodbg_empty(Reg)) {
477         // This instruction has users, but if the only user is the phi node for the
478         // parent block, and the only use of that phi node is this instruction, then
479         // this instruction is dead: both it (and the phi node) can be removed.
480         MachineRegisterInfo::use_iterator I = MRI->use_begin(Reg);
481         if (llvm::next(I) == MRI->use_end() &&
482             I.getOperand().getParent()->isPHI()) {
483           MachineInstr *OnePhi = I.getOperand().getParent();
484
485           for (unsigned j = 0, f = OnePhi->getNumOperands(); j != f; ++j) {
486             const MachineOperand &OPO = OnePhi->getOperand(j);
487             if (OPO.isReg() && OPO.isDef()) {
488               unsigned OPReg = OPO.getReg();
489
490               MachineRegisterInfo::use_iterator nextJ;
491               for (MachineRegisterInfo::use_iterator J = MRI->use_begin(OPReg),
492                    E = MRI->use_end(); J!=E; J=nextJ) {
493                 nextJ = llvm::next(J);
494                 MachineOperand& Use = J.getOperand();
495                 MachineInstr *UseMI = Use.getParent();
496
497                 if (MI != UseMI) {
498                   // The phi node has a user that is not MI, bail...
499                   return false;
500                 }
501               }
502             }
503           }
504
505           DeadPhis.push_back(OnePhi);
506         } else {
507           // This def has a non-debug use. Don't delete the instruction!
508           return false;
509         }
510       }
511     }
512   }
513
514   // If there are no defs with uses, the instruction is dead.
515   return true;
516 }
517
518 void PPCCTRLoops::removeIfDead(MachineInstr *MI) {
519   // This procedure was essentially copied from DeadMachineInstructionElim
520
521   SmallVector<MachineInstr *, 1> DeadPhis;
522   if (isDead(MI, DeadPhis)) {
523     DEBUG(dbgs() << "CTR looping will remove: " << *MI);
524
525     // It is possible that some DBG_VALUE instructions refer to this
526     // instruction.  Examine each def operand for such references;
527     // if found, mark the DBG_VALUE as undef (but don't delete it).
528     for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
529       const MachineOperand &MO = MI->getOperand(i);
530       if (!MO.isReg() || !MO.isDef())
531         continue;
532       unsigned Reg = MO.getReg();
533       MachineRegisterInfo::use_iterator nextI;
534       for (MachineRegisterInfo::use_iterator I = MRI->use_begin(Reg),
535            E = MRI->use_end(); I!=E; I=nextI) {
536         nextI = llvm::next(I);  // I is invalidated by the setReg
537         MachineOperand& Use = I.getOperand();
538         MachineInstr *UseMI = Use.getParent();
539         if (UseMI==MI)
540           continue;
541         if (Use.isDebug()) // this might also be a instr -> phi -> instr case
542                            // which can also be removed.
543           UseMI->getOperand(0).setReg(0U);
544       }
545     }
546
547     MI->eraseFromParent();
548     for (unsigned i = 0; i < DeadPhis.size(); ++i) {
549       DeadPhis[i]->eraseFromParent();
550     }
551   }
552 }
553
554 /// converToCTRLoop - check if the loop is a candidate for
555 /// converting to a CTR loop.  If so, then perform the
556 /// transformation.
557 ///
558 /// This function works on innermost loops first.  A loop can
559 /// be converted if it is a counting loop; either a register
560 /// value or an immediate.
561 ///
562 /// The code makes several assumptions about the representation
563 /// of the loop in llvm.
564 bool PPCCTRLoops::convertToCTRLoop(MachineLoop *L) {
565   bool Changed = false;
566   // Process nested loops first.
567   for (MachineLoop::iterator I = L->begin(), E = L->end(); I != E; ++I) {
568     Changed |= convertToCTRLoop(*I);
569   }
570   // If a nested loop has been converted, then we can't convert this loop.
571   if (Changed) {
572     return Changed;
573   }
574
575   SmallVector<MachineInstr *, 2> OldInsts;
576   // Are we able to determine the trip count for the loop?
577   CountValue *TripCount = getTripCount(L, OldInsts);
578   if (TripCount == 0) {
579     DEBUG(dbgs() << "failed to get trip count!\n");
580     return false;
581   }
582   // Does the loop contain any invalid instructions?
583   if (containsInvalidInstruction(L)) {
584     return false;
585   }
586   MachineBasicBlock *Preheader = L->getLoopPreheader();
587   // No preheader means there's not place for the loop instr.
588   if (Preheader == 0) {
589     return false;
590   }
591   MachineBasicBlock::iterator InsertPos = Preheader->getFirstTerminator();
592
593   DebugLoc dl;
594   if (InsertPos != Preheader->end())
595     dl = InsertPos->getDebugLoc();
596
597   MachineBasicBlock *LastMBB = L->getExitingBlock();
598   // Don't generate CTR loop if the loop has more than one exit.
599   if (LastMBB == 0) {
600     return false;
601   }
602   MachineBasicBlock::iterator LastI = LastMBB->getFirstTerminator();
603
604   // Determine the loop start.
605   MachineBasicBlock *LoopStart = L->getTopBlock();
606   if (L->getLoopLatch() != LastMBB) {
607     // When the exit and latch are not the same, use the latch block as the
608     // start.
609     // The loop start address is used only after the 1st iteration, and the loop
610     // latch may contains instrs. that need to be executed after the 1st iter.
611     LoopStart = L->getLoopLatch();
612     // Make sure the latch is a successor of the exit, otherwise it won't work.
613     if (!LastMBB->isSuccessor(LoopStart)) {
614       return false;
615     }
616   }
617
618   // Convert the loop to a CTR loop
619   DEBUG(dbgs() << "Change to CTR loop at "; L->dump());
620
621   MachineFunction *MF = LastMBB->getParent();
622   const PPCSubtarget &Subtarget = MF->getTarget().getSubtarget<PPCSubtarget>();
623   bool isPPC64 = Subtarget.isPPC64();
624
625   const TargetRegisterClass *GPRC = &PPC::GPRCRegClass;
626   const TargetRegisterClass *G8RC = &PPC::G8RCRegClass;
627   const TargetRegisterClass *RC = isPPC64 ? G8RC : GPRC;
628
629   unsigned CountReg;
630   if (TripCount->isReg()) {
631     // Create a copy of the loop count register.
632     const TargetRegisterClass *SrcRC =
633       MF->getRegInfo().getRegClass(TripCount->getReg());
634     CountReg = MF->getRegInfo().createVirtualRegister(RC);
635     unsigned CopyOp = (isPPC64 && SrcRC == GPRC) ?
636                         (unsigned) PPC::EXTSW_32_64 :
637                         (unsigned) TargetOpcode::COPY;
638     BuildMI(*Preheader, InsertPos, dl,
639             TII->get(CopyOp), CountReg).addReg(TripCount->getReg());
640     if (TripCount->isNeg()) {
641       unsigned CountReg1 = CountReg;
642       CountReg = MF->getRegInfo().createVirtualRegister(RC);
643       BuildMI(*Preheader, InsertPos, dl,
644               TII->get(isPPC64 ? PPC::NEG8 : PPC::NEG),
645                        CountReg).addReg(CountReg1);
646     }
647   } else {
648     assert(TripCount->isImm() && "Expecting immedate vaule for trip count");
649     // Put the trip count in a register for transfer into the count register.
650
651     int64_t CountImm = TripCount->getImm();
652     assert(!TripCount->isNeg() && "Constant trip count must be positive");
653
654     CountReg = MF->getRegInfo().createVirtualRegister(RC);
655     if (CountImm > 0xFFFF) {
656       BuildMI(*Preheader, InsertPos, dl,
657               TII->get(isPPC64 ? PPC::LIS8 : PPC::LIS),
658               CountReg).addImm(CountImm >> 16);
659       unsigned CountReg1 = CountReg;
660       CountReg = MF->getRegInfo().createVirtualRegister(RC);
661       BuildMI(*Preheader, InsertPos, dl,
662               TII->get(isPPC64 ? PPC::ORI8 : PPC::ORI),
663               CountReg).addReg(CountReg1).addImm(CountImm & 0xFFFF);
664     } else {
665       BuildMI(*Preheader, InsertPos, dl,
666               TII->get(isPPC64 ? PPC::LI8 : PPC::LI),
667               CountReg).addImm(CountImm);
668     }
669   }
670
671   // Add the mtctr instruction to the beginning of the loop.
672   BuildMI(*Preheader, InsertPos, dl,
673           TII->get(isPPC64 ? PPC::MTCTR8 : PPC::MTCTR)).addReg(CountReg,
674             TripCount->isImm() ? RegState::Kill : 0);
675
676   // Make sure the loop start always has a reference in the CFG.  We need to
677   // create a BlockAddress operand to get this mechanism to work both the
678   // MachineBasicBlock and BasicBlock objects need the flag set.
679   LoopStart->setHasAddressTaken();
680   // This line is needed to set the hasAddressTaken flag on the BasicBlock
681   // object
682   BlockAddress::get(const_cast<BasicBlock *>(LoopStart->getBasicBlock()));
683
684   // Replace the loop branch with a bdnz instruction.
685   dl = LastI->getDebugLoc();
686   const std::vector<MachineBasicBlock*> Blocks = L->getBlocks();
687   for (unsigned i = 0, e = Blocks.size(); i != e; ++i) {
688     MachineBasicBlock *MBB = Blocks[i];
689     if (MBB != Preheader)
690       MBB->addLiveIn(isPPC64 ? PPC::CTR8 : PPC::CTR);
691   }
692
693   // The loop ends with either:
694   //  - a conditional branch followed by an unconditional branch, or
695   //  - a conditional branch to the loop start.
696   assert(LastI->getOpcode() == PPC::BCC &&
697          "loop end must start with a BCC instruction");
698   // Either the BCC branches to the beginning of the loop, or it
699   // branches out of the loop and there is an unconditional branch
700   // to the start of the loop.
701   MachineBasicBlock *BranchTarget = LastI->getOperand(2).getMBB();
702   BuildMI(*LastMBB, LastI, dl,
703         TII->get((BranchTarget == LoopStart) ?
704                  (isPPC64 ? PPC::BDNZ8 : PPC::BDNZ) :
705                  (isPPC64 ? PPC::BDZ8 : PPC::BDZ))).addMBB(BranchTarget);
706
707   // Conditional branch; just delete it.
708   DEBUG(dbgs() << "Removing old branch: " << *LastI);
709   LastMBB->erase(LastI);
710
711   delete TripCount;
712
713   // The induction operation (add) and the comparison (cmpwi) may now be
714   // unneeded. If these are unneeded, then remove them.
715   for (unsigned i = 0; i < OldInsts.size(); ++i)
716     removeIfDead(OldInsts[i]);
717
718   ++NumCTRLoops;
719   return true;
720 }
721