Change errs() to dbgs().
[oota-llvm.git] / lib / CodeGen / IfConversion.cpp
1 //===-- IfConversion.cpp - Machine code if conversion pass. ---------------===//
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 implements the machine instruction level if-conversion pass.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "ifcvt"
15 #include "BranchFolding.h"
16 #include "llvm/Function.h"
17 #include "llvm/CodeGen/Passes.h"
18 #include "llvm/CodeGen/MachineModuleInfo.h"
19 #include "llvm/CodeGen/MachineFunctionPass.h"
20 #include "llvm/Target/TargetInstrInfo.h"
21 #include "llvm/Target/TargetLowering.h"
22 #include "llvm/Target/TargetMachine.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include "llvm/ADT/DepthFirstIterator.h"
28 #include "llvm/ADT/Statistic.h"
29 #include "llvm/ADT/STLExtras.h"
30 using namespace llvm;
31
32 // Hidden options for help debugging.
33 static cl::opt<int> IfCvtFnStart("ifcvt-fn-start", cl::init(-1), cl::Hidden);
34 static cl::opt<int> IfCvtFnStop("ifcvt-fn-stop", cl::init(-1), cl::Hidden);
35 static cl::opt<int> IfCvtLimit("ifcvt-limit", cl::init(-1), cl::Hidden);
36 static cl::opt<bool> DisableSimple("disable-ifcvt-simple", 
37                                    cl::init(false), cl::Hidden);
38 static cl::opt<bool> DisableSimpleF("disable-ifcvt-simple-false", 
39                                     cl::init(false), cl::Hidden);
40 static cl::opt<bool> DisableTriangle("disable-ifcvt-triangle", 
41                                      cl::init(false), cl::Hidden);
42 static cl::opt<bool> DisableTriangleR("disable-ifcvt-triangle-rev", 
43                                       cl::init(false), cl::Hidden);
44 static cl::opt<bool> DisableTriangleF("disable-ifcvt-triangle-false", 
45                                       cl::init(false), cl::Hidden);
46 static cl::opt<bool> DisableTriangleFR("disable-ifcvt-triangle-false-rev", 
47                                        cl::init(false), cl::Hidden);
48 static cl::opt<bool> DisableDiamond("disable-ifcvt-diamond", 
49                                     cl::init(false), cl::Hidden);
50
51 STATISTIC(NumSimple,       "Number of simple if-conversions performed");
52 STATISTIC(NumSimpleFalse,  "Number of simple (F) if-conversions performed");
53 STATISTIC(NumTriangle,     "Number of triangle if-conversions performed");
54 STATISTIC(NumTriangleRev,  "Number of triangle (R) if-conversions performed");
55 STATISTIC(NumTriangleFalse,"Number of triangle (F) if-conversions performed");
56 STATISTIC(NumTriangleFRev, "Number of triangle (F/R) if-conversions performed");
57 STATISTIC(NumDiamonds,     "Number of diamond if-conversions performed");
58 STATISTIC(NumIfConvBBs,    "Number of if-converted blocks");
59 STATISTIC(NumDupBBs,       "Number of duplicated blocks");
60
61 namespace {
62   class IfConverter : public MachineFunctionPass {
63     enum IfcvtKind {
64       ICNotClassfied,  // BB data valid, but not classified.
65       ICSimpleFalse,   // Same as ICSimple, but on the false path.
66       ICSimple,        // BB is entry of an one split, no rejoin sub-CFG.
67       ICTriangleFRev,  // Same as ICTriangleFalse, but false path rev condition.
68       ICTriangleRev,   // Same as ICTriangle, but true path rev condition.
69       ICTriangleFalse, // Same as ICTriangle, but on the false path.
70       ICTriangle,      // BB is entry of a triangle sub-CFG.
71       ICDiamond        // BB is entry of a diamond sub-CFG.
72     };
73
74     /// BBInfo - One per MachineBasicBlock, this is used to cache the result
75     /// if-conversion feasibility analysis. This includes results from
76     /// TargetInstrInfo::AnalyzeBranch() (i.e. TBB, FBB, and Cond), and its
77     /// classification, and common tail block of its successors (if it's a
78     /// diamond shape), its size, whether it's predicable, and whether any
79     /// instruction can clobber the 'would-be' predicate.
80     ///
81     /// IsDone          - True if BB is not to be considered for ifcvt.
82     /// IsBeingAnalyzed - True if BB is currently being analyzed.
83     /// IsAnalyzed      - True if BB has been analyzed (info is still valid).
84     /// IsEnqueued      - True if BB has been enqueued to be ifcvt'ed.
85     /// IsBrAnalyzable  - True if AnalyzeBranch() returns false.
86     /// HasFallThrough  - True if BB may fallthrough to the following BB.
87     /// IsUnpredicable  - True if BB is known to be unpredicable.
88     /// ClobbersPred    - True if BB could modify predicates (e.g. has
89     ///                   cmp, call, etc.)
90     /// NonPredSize     - Number of non-predicated instructions.
91     /// BB              - Corresponding MachineBasicBlock.
92     /// TrueBB / FalseBB- See AnalyzeBranch().
93     /// BrCond          - Conditions for end of block conditional branches.
94     /// Predicate       - Predicate used in the BB.
95     struct BBInfo {
96       bool IsDone          : 1;
97       bool IsBeingAnalyzed : 1;
98       bool IsAnalyzed      : 1;
99       bool IsEnqueued      : 1;
100       bool IsBrAnalyzable  : 1;
101       bool HasFallThrough  : 1;
102       bool IsUnpredicable  : 1;
103       bool CannotBeCopied  : 1;
104       bool ClobbersPred    : 1;
105       unsigned NonPredSize;
106       MachineBasicBlock *BB;
107       MachineBasicBlock *TrueBB;
108       MachineBasicBlock *FalseBB;
109       SmallVector<MachineOperand, 4> BrCond;
110       SmallVector<MachineOperand, 4> Predicate;
111       BBInfo() : IsDone(false), IsBeingAnalyzed(false),
112                  IsAnalyzed(false), IsEnqueued(false), IsBrAnalyzable(false),
113                  HasFallThrough(false), IsUnpredicable(false),
114                  CannotBeCopied(false), ClobbersPred(false), NonPredSize(0),
115                  BB(0), TrueBB(0), FalseBB(0) {}
116     };
117
118     /// IfcvtToken - Record information about pending if-conversions to attemp:
119     /// BBI             - Corresponding BBInfo.
120     /// Kind            - Type of block. See IfcvtKind.
121     /// NeedSubsumption - True if the to-be-predicated BB has already been
122     ///                   predicated.
123     /// NumDups      - Number of instructions that would be duplicated due
124     ///                   to this if-conversion. (For diamonds, the number of
125     ///                   identical instructions at the beginnings of both
126     ///                   paths).
127     /// NumDups2     - For diamonds, the number of identical instructions
128     ///                   at the ends of both paths.
129     struct IfcvtToken {
130       BBInfo &BBI;
131       IfcvtKind Kind;
132       bool NeedSubsumption;
133       unsigned NumDups;
134       unsigned NumDups2;
135       IfcvtToken(BBInfo &b, IfcvtKind k, bool s, unsigned d, unsigned d2 = 0)
136         : BBI(b), Kind(k), NeedSubsumption(s), NumDups(d), NumDups2(d2) {}
137     };
138
139     /// Roots - Basic blocks that do not have successors. These are the starting
140     /// points of Graph traversal.
141     std::vector<MachineBasicBlock*> Roots;
142
143     /// BBAnalysis - Results of if-conversion feasibility analysis indexed by
144     /// basic block number.
145     std::vector<BBInfo> BBAnalysis;
146
147     const TargetLowering *TLI;
148     const TargetInstrInfo *TII;
149     bool MadeChange;
150     int FnNum;
151   public:
152     static char ID;
153     IfConverter() : MachineFunctionPass(&ID), FnNum(-1) {}
154
155     virtual bool runOnMachineFunction(MachineFunction &MF);
156     virtual const char *getPassName() const { return "If Converter"; }
157
158   private:
159     bool ReverseBranchCondition(BBInfo &BBI);
160     bool ValidSimple(BBInfo &TrueBBI, unsigned &Dups) const;
161     bool ValidTriangle(BBInfo &TrueBBI, BBInfo &FalseBBI,
162                        bool FalseBranch, unsigned &Dups) const;
163     bool ValidDiamond(BBInfo &TrueBBI, BBInfo &FalseBBI,
164                       unsigned &Dups1, unsigned &Dups2) const;
165     void ScanInstructions(BBInfo &BBI);
166     BBInfo &AnalyzeBlock(MachineBasicBlock *BB,
167                          std::vector<IfcvtToken*> &Tokens);
168     bool FeasibilityAnalysis(BBInfo &BBI, SmallVectorImpl<MachineOperand> &Cond,
169                              bool isTriangle = false, bool RevBranch = false);
170     bool AnalyzeBlocks(MachineFunction &MF,
171                        std::vector<IfcvtToken*> &Tokens);
172     void InvalidatePreds(MachineBasicBlock *BB);
173     void RemoveExtraEdges(BBInfo &BBI);
174     bool IfConvertSimple(BBInfo &BBI, IfcvtKind Kind);
175     bool IfConvertTriangle(BBInfo &BBI, IfcvtKind Kind);
176     bool IfConvertDiamond(BBInfo &BBI, IfcvtKind Kind,
177                           unsigned NumDups1, unsigned NumDups2);
178     void PredicateBlock(BBInfo &BBI,
179                         MachineBasicBlock::iterator E,
180                         SmallVectorImpl<MachineOperand> &Cond);
181     void CopyAndPredicateBlock(BBInfo &ToBBI, BBInfo &FromBBI,
182                                SmallVectorImpl<MachineOperand> &Cond,
183                                bool IgnoreBr = false);
184     void MergeBlocks(BBInfo &ToBBI, BBInfo &FromBBI);
185
186     bool MeetIfcvtSizeLimit(unsigned Size) const {
187       return Size > 0 && Size <= TLI->getIfCvtBlockSizeLimit();
188     }
189
190     // blockAlwaysFallThrough - Block ends without a terminator.
191     bool blockAlwaysFallThrough(BBInfo &BBI) const {
192       return BBI.IsBrAnalyzable && BBI.TrueBB == NULL;
193     }
194
195     // IfcvtTokenCmp - Used to sort if-conversion candidates.
196     static bool IfcvtTokenCmp(IfcvtToken *C1, IfcvtToken *C2) {
197       int Incr1 = (C1->Kind == ICDiamond)
198         ? -(int)(C1->NumDups + C1->NumDups2) : (int)C1->NumDups;
199       int Incr2 = (C2->Kind == ICDiamond)
200         ? -(int)(C2->NumDups + C2->NumDups2) : (int)C2->NumDups;
201       if (Incr1 > Incr2)
202         return true;
203       else if (Incr1 == Incr2) {
204         // Favors subsumption.
205         if (C1->NeedSubsumption == false && C2->NeedSubsumption == true)
206           return true;
207         else if (C1->NeedSubsumption == C2->NeedSubsumption) {
208           // Favors diamond over triangle, etc.
209           if ((unsigned)C1->Kind < (unsigned)C2->Kind)
210             return true;
211           else if (C1->Kind == C2->Kind)
212             return C1->BBI.BB->getNumber() < C2->BBI.BB->getNumber();
213         }
214       }
215       return false;
216     }
217   };
218
219   char IfConverter::ID = 0;
220 }
221
222 static RegisterPass<IfConverter>
223 X("if-converter", "If Converter");
224
225 FunctionPass *llvm::createIfConverterPass() { return new IfConverter(); }
226
227 bool IfConverter::runOnMachineFunction(MachineFunction &MF) {
228   TLI = MF.getTarget().getTargetLowering();
229   TII = MF.getTarget().getInstrInfo();
230   if (!TII) return false;
231
232   DEBUG(dbgs() << "\nIfcvt: function (" << ++FnNum <<  ") \'"
233                << MF.getFunction()->getName() << "\'");
234
235   if (FnNum < IfCvtFnStart || (IfCvtFnStop != -1 && FnNum > IfCvtFnStop)) {
236     DEBUG(dbgs() << " skipped\n");
237     return false;
238   }
239   DEBUG(dbgs() << "\n");
240
241   MF.RenumberBlocks();
242   BBAnalysis.resize(MF.getNumBlockIDs());
243
244   // Look for root nodes, i.e. blocks without successors.
245   for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I)
246     if (I->succ_empty())
247       Roots.push_back(I);
248
249   std::vector<IfcvtToken*> Tokens;
250   MadeChange = false;
251   unsigned NumIfCvts = NumSimple + NumSimpleFalse + NumTriangle +
252     NumTriangleRev + NumTriangleFalse + NumTriangleFRev + NumDiamonds;
253   while (IfCvtLimit == -1 || (int)NumIfCvts < IfCvtLimit) {
254     // Do an initial analysis for each basic block and find all the potential
255     // candidates to perform if-conversion.
256     bool Change = AnalyzeBlocks(MF, Tokens);
257     while (!Tokens.empty()) {
258       IfcvtToken *Token = Tokens.back();
259       Tokens.pop_back();
260       BBInfo &BBI = Token->BBI;
261       IfcvtKind Kind = Token->Kind;
262       unsigned NumDups = Token->NumDups;
263       unsigned NumDups2 = Token->NumDups2;
264
265       delete Token;
266
267       // If the block has been evicted out of the queue or it has already been
268       // marked dead (due to it being predicated), then skip it.
269       if (BBI.IsDone)
270         BBI.IsEnqueued = false;
271       if (!BBI.IsEnqueued)
272         continue;
273
274       BBI.IsEnqueued = false;
275
276       bool RetVal = false;
277       switch (Kind) {
278       default: assert(false && "Unexpected!");
279         break;
280       case ICSimple:
281       case ICSimpleFalse: {
282         bool isFalse = Kind == ICSimpleFalse;
283         if ((isFalse && DisableSimpleF) || (!isFalse && DisableSimple)) break;
284         DEBUG(dbgs() << "Ifcvt (Simple" << (Kind == ICSimpleFalse ? " false" :"")
285                      << "): BB#" << BBI.BB->getNumber() << " ("
286                      << ((Kind == ICSimpleFalse)
287                          ? BBI.FalseBB->getNumber()
288                          : BBI.TrueBB->getNumber()) << ") ");
289         RetVal = IfConvertSimple(BBI, Kind);
290         DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n");
291         if (RetVal) {
292           if (isFalse) NumSimpleFalse++;
293           else         NumSimple++;
294         }
295        break;
296       }
297       case ICTriangle:
298       case ICTriangleRev:
299       case ICTriangleFalse:
300       case ICTriangleFRev: {
301         bool isFalse = Kind == ICTriangleFalse;
302         bool isRev   = (Kind == ICTriangleRev || Kind == ICTriangleFRev);
303         if (DisableTriangle && !isFalse && !isRev) break;
304         if (DisableTriangleR && !isFalse && isRev) break;
305         if (DisableTriangleF && isFalse && !isRev) break;
306         if (DisableTriangleFR && isFalse && isRev) break;
307         DEBUG(dbgs() << "Ifcvt (Triangle");
308         if (isFalse)
309           DEBUG(dbgs() << " false");
310         if (isRev)
311           DEBUG(dbgs() << " rev");
312         DEBUG(dbgs() << "): BB#" << BBI.BB->getNumber() << " (T:"
313                      << BBI.TrueBB->getNumber() << ",F:"
314                      << BBI.FalseBB->getNumber() << ") ");
315         RetVal = IfConvertTriangle(BBI, Kind);
316         DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n");
317         if (RetVal) {
318           if (isFalse) {
319             if (isRev) NumTriangleFRev++;
320             else       NumTriangleFalse++;
321           } else {
322             if (isRev) NumTriangleRev++;
323             else       NumTriangle++;
324           }
325         }
326         break;
327       }
328       case ICDiamond: {
329         if (DisableDiamond) break;
330         DEBUG(dbgs() << "Ifcvt (Diamond): BB#" << BBI.BB->getNumber() << " (T:"
331                      << BBI.TrueBB->getNumber() << ",F:"
332                      << BBI.FalseBB->getNumber() << ") ");
333         RetVal = IfConvertDiamond(BBI, Kind, NumDups, NumDups2);
334         DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n");
335         if (RetVal) NumDiamonds++;
336         break;
337       }
338       }
339
340       Change |= RetVal;
341
342       NumIfCvts = NumSimple + NumSimpleFalse + NumTriangle + NumTriangleRev +
343         NumTriangleFalse + NumTriangleFRev + NumDiamonds;
344       if (IfCvtLimit != -1 && (int)NumIfCvts >= IfCvtLimit)
345         break;
346     }
347
348     if (!Change)
349       break;
350     MadeChange |= Change;
351   }
352
353   // Delete tokens in case of early exit.
354   while (!Tokens.empty()) {
355     IfcvtToken *Token = Tokens.back();
356     Tokens.pop_back();
357     delete Token;
358   }
359
360   Tokens.clear();
361   Roots.clear();
362   BBAnalysis.clear();
363
364   if (MadeChange) {
365     BranchFolder BF(false);
366     BF.OptimizeFunction(MF, TII,
367                         MF.getTarget().getRegisterInfo(),
368                         getAnalysisIfAvailable<MachineModuleInfo>());
369   }
370
371   return MadeChange;
372 }
373
374 /// findFalseBlock - BB has a fallthrough. Find its 'false' successor given
375 /// its 'true' successor.
376 static MachineBasicBlock *findFalseBlock(MachineBasicBlock *BB,
377                                          MachineBasicBlock *TrueBB) {
378   for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
379          E = BB->succ_end(); SI != E; ++SI) {
380     MachineBasicBlock *SuccBB = *SI;
381     if (SuccBB != TrueBB)
382       return SuccBB;
383   }
384   return NULL;
385 }
386
387 /// ReverseBranchCondition - Reverse the condition of the end of the block
388 /// branch. Swap block's 'true' and 'false' successors.
389 bool IfConverter::ReverseBranchCondition(BBInfo &BBI) {
390   if (!TII->ReverseBranchCondition(BBI.BrCond)) {
391     TII->RemoveBranch(*BBI.BB);
392     TII->InsertBranch(*BBI.BB, BBI.FalseBB, BBI.TrueBB, BBI.BrCond);
393     std::swap(BBI.TrueBB, BBI.FalseBB);
394     return true;
395   }
396   return false;
397 }
398
399 /// getNextBlock - Returns the next block in the function blocks ordering. If
400 /// it is the end, returns NULL.
401 static inline MachineBasicBlock *getNextBlock(MachineBasicBlock *BB) {
402   MachineFunction::iterator I = BB;
403   MachineFunction::iterator E = BB->getParent()->end();
404   if (++I == E)
405     return NULL;
406   return I;
407 }
408
409 /// ValidSimple - Returns true if the 'true' block (along with its
410 /// predecessor) forms a valid simple shape for ifcvt. It also returns the
411 /// number of instructions that the ifcvt would need to duplicate if performed
412 /// in Dups.
413 bool IfConverter::ValidSimple(BBInfo &TrueBBI, unsigned &Dups) const {
414   Dups = 0;
415   if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone)
416     return false;
417
418   if (TrueBBI.IsBrAnalyzable)
419     return false;
420
421   if (TrueBBI.BB->pred_size() > 1) {
422     if (TrueBBI.CannotBeCopied ||
423         TrueBBI.NonPredSize > TLI->getIfCvtDupBlockSizeLimit())
424       return false;
425     Dups = TrueBBI.NonPredSize;
426   }
427
428   return true;
429 }
430
431 /// ValidTriangle - Returns true if the 'true' and 'false' blocks (along
432 /// with their common predecessor) forms a valid triangle shape for ifcvt.
433 /// If 'FalseBranch' is true, it checks if 'true' block's false branch
434 /// branches to the false branch rather than the other way around. It also
435 /// returns the number of instructions that the ifcvt would need to duplicate
436 /// if performed in 'Dups'.
437 bool IfConverter::ValidTriangle(BBInfo &TrueBBI, BBInfo &FalseBBI,
438                                 bool FalseBranch, unsigned &Dups) const {
439   Dups = 0;
440   if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone)
441     return false;
442
443   if (TrueBBI.BB->pred_size() > 1) {
444     if (TrueBBI.CannotBeCopied)
445       return false;
446
447     unsigned Size = TrueBBI.NonPredSize;
448     if (TrueBBI.IsBrAnalyzable) {
449       if (TrueBBI.TrueBB && TrueBBI.BrCond.empty())
450         // Ends with an unconditional branch. It will be removed.
451         --Size;
452       else {
453         MachineBasicBlock *FExit = FalseBranch
454           ? TrueBBI.TrueBB : TrueBBI.FalseBB;
455         if (FExit)
456           // Require a conditional branch
457           ++Size;
458       }
459     }
460     if (Size > TLI->getIfCvtDupBlockSizeLimit())
461       return false;
462     Dups = Size;
463   }
464
465   MachineBasicBlock *TExit = FalseBranch ? TrueBBI.FalseBB : TrueBBI.TrueBB;
466   if (!TExit && blockAlwaysFallThrough(TrueBBI)) {
467     MachineFunction::iterator I = TrueBBI.BB;
468     if (++I == TrueBBI.BB->getParent()->end())
469       return false;
470     TExit = I;
471   }
472   return TExit && TExit == FalseBBI.BB;
473 }
474
475 static
476 MachineBasicBlock::iterator firstNonBranchInst(MachineBasicBlock *BB,
477                                                const TargetInstrInfo *TII) {
478   MachineBasicBlock::iterator I = BB->end();
479   while (I != BB->begin()) {
480     --I;
481     if (!I->getDesc().isBranch())
482       break;
483   }
484   return I;
485 }
486
487 /// ValidDiamond - Returns true if the 'true' and 'false' blocks (along
488 /// with their common predecessor) forms a valid diamond shape for ifcvt.
489 bool IfConverter::ValidDiamond(BBInfo &TrueBBI, BBInfo &FalseBBI,
490                                unsigned &Dups1, unsigned &Dups2) const {
491   Dups1 = Dups2 = 0;
492   if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone ||
493       FalseBBI.IsBeingAnalyzed || FalseBBI.IsDone)
494     return false;
495
496   MachineBasicBlock *TT = TrueBBI.TrueBB;
497   MachineBasicBlock *FT = FalseBBI.TrueBB;
498
499   if (!TT && blockAlwaysFallThrough(TrueBBI))
500     TT = getNextBlock(TrueBBI.BB);
501   if (!FT && blockAlwaysFallThrough(FalseBBI))
502     FT = getNextBlock(FalseBBI.BB);
503   if (TT != FT)
504     return false;
505   if (TT == NULL && (TrueBBI.IsBrAnalyzable || FalseBBI.IsBrAnalyzable))
506     return false;
507   if  (TrueBBI.BB->pred_size() > 1 || FalseBBI.BB->pred_size() > 1)
508     return false;
509
510   // FIXME: Allow true block to have an early exit?
511   if (TrueBBI.FalseBB || FalseBBI.FalseBB ||
512       (TrueBBI.ClobbersPred && FalseBBI.ClobbersPred))
513     return false;
514
515   MachineBasicBlock::iterator TI = TrueBBI.BB->begin();
516   MachineBasicBlock::iterator FI = FalseBBI.BB->begin();
517   while (TI != TrueBBI.BB->end() && FI != FalseBBI.BB->end()) {
518     if (!TI->isIdenticalTo(FI))
519       break;
520     ++Dups1;
521     ++TI;
522     ++FI;
523   }
524
525   TI = firstNonBranchInst(TrueBBI.BB, TII);
526   FI = firstNonBranchInst(FalseBBI.BB, TII);
527   while (TI != TrueBBI.BB->begin() && FI != FalseBBI.BB->begin()) {
528     if (!TI->isIdenticalTo(FI))
529       break;
530     ++Dups2;
531     --TI;
532     --FI;
533   }
534
535   return true;
536 }
537
538 /// ScanInstructions - Scan all the instructions in the block to determine if
539 /// the block is predicable. In most cases, that means all the instructions
540 /// in the block are isPredicable(). Also checks if the block contains any
541 /// instruction which can clobber a predicate (e.g. condition code register).
542 /// If so, the block is not predicable unless it's the last instruction.
543 void IfConverter::ScanInstructions(BBInfo &BBI) {
544   if (BBI.IsDone)
545     return;
546
547   bool AlreadyPredicated = BBI.Predicate.size() > 0;
548   // First analyze the end of BB branches.
549   BBI.TrueBB = BBI.FalseBB = NULL;
550   BBI.BrCond.clear();
551   BBI.IsBrAnalyzable =
552     !TII->AnalyzeBranch(*BBI.BB, BBI.TrueBB, BBI.FalseBB, BBI.BrCond);
553   BBI.HasFallThrough = BBI.IsBrAnalyzable && BBI.FalseBB == NULL;
554
555   if (BBI.BrCond.size()) {
556     // No false branch. This BB must end with a conditional branch and a
557     // fallthrough.
558     if (!BBI.FalseBB)
559       BBI.FalseBB = findFalseBlock(BBI.BB, BBI.TrueBB);  
560     if (!BBI.FalseBB) {
561       // Malformed bcc? True and false blocks are the same?
562       BBI.IsUnpredicable = true;
563       return;
564     }
565   }
566
567   // Then scan all the instructions.
568   BBI.NonPredSize = 0;
569   BBI.ClobbersPred = false;
570   for (MachineBasicBlock::iterator I = BBI.BB->begin(), E = BBI.BB->end();
571        I != E; ++I) {
572     const TargetInstrDesc &TID = I->getDesc();
573     if (TID.isNotDuplicable())
574       BBI.CannotBeCopied = true;
575
576     bool isPredicated = TII->isPredicated(I);
577     bool isCondBr = BBI.IsBrAnalyzable && TID.isConditionalBranch();
578
579     if (!isCondBr) {
580       if (!isPredicated)
581         BBI.NonPredSize++;
582       else if (!AlreadyPredicated) {
583         // FIXME: This instruction is already predicated before the
584         // if-conversion pass. It's probably something like a conditional move.
585         // Mark this block unpredicable for now.
586         BBI.IsUnpredicable = true;
587         return;
588       }
589     }
590
591     if (BBI.ClobbersPred && !isPredicated) {
592       // Predicate modification instruction should end the block (except for
593       // already predicated instructions and end of block branches).
594       if (isCondBr) {
595         // A conditional branch is not predicable, but it may be eliminated.
596         continue;
597       }
598
599       // Predicate may have been modified, the subsequent (currently)
600       // unpredicated instructions cannot be correctly predicated.
601       BBI.IsUnpredicable = true;
602       return;
603     }
604
605     // FIXME: Make use of PredDefs? e.g. ADDC, SUBC sets predicates but are
606     // still potentially predicable.
607     std::vector<MachineOperand> PredDefs;
608     if (TII->DefinesPredicate(I, PredDefs))
609       BBI.ClobbersPred = true;
610
611     if (!TII->isPredicable(I)) {
612       BBI.IsUnpredicable = true;
613       return;
614     }
615   }
616 }
617
618 /// FeasibilityAnalysis - Determine if the block is a suitable candidate to be
619 /// predicated by the specified predicate.
620 bool IfConverter::FeasibilityAnalysis(BBInfo &BBI,
621                                       SmallVectorImpl<MachineOperand> &Pred,
622                                       bool isTriangle, bool RevBranch) {
623   // If the block is dead or unpredicable, then it cannot be predicated.
624   if (BBI.IsDone || BBI.IsUnpredicable)
625     return false;
626
627   // If it is already predicated, check if its predicate subsumes the new
628   // predicate.
629   if (BBI.Predicate.size() && !TII->SubsumesPredicate(BBI.Predicate, Pred))
630     return false;
631
632   if (BBI.BrCond.size()) {
633     if (!isTriangle)
634       return false;
635
636     // Test predicate subsumption.
637     SmallVector<MachineOperand, 4> RevPred(Pred.begin(), Pred.end());
638     SmallVector<MachineOperand, 4> Cond(BBI.BrCond.begin(), BBI.BrCond.end());
639     if (RevBranch) {
640       if (TII->ReverseBranchCondition(Cond))
641         return false;
642     }
643     if (TII->ReverseBranchCondition(RevPred) ||
644         !TII->SubsumesPredicate(Cond, RevPred))
645       return false;
646   }
647
648   return true;
649 }
650
651 /// AnalyzeBlock - Analyze the structure of the sub-CFG starting from
652 /// the specified block. Record its successors and whether it looks like an
653 /// if-conversion candidate.
654 IfConverter::BBInfo &IfConverter::AnalyzeBlock(MachineBasicBlock *BB,
655                                              std::vector<IfcvtToken*> &Tokens) {
656   BBInfo &BBI = BBAnalysis[BB->getNumber()];
657
658   if (BBI.IsAnalyzed || BBI.IsBeingAnalyzed)
659     return BBI;
660
661   BBI.BB = BB;
662   BBI.IsBeingAnalyzed = true;
663
664   ScanInstructions(BBI);
665
666   // Unanalyzable or ends with fallthrough or unconditional branch.
667   if (!BBI.IsBrAnalyzable || BBI.BrCond.empty()) {
668     BBI.IsBeingAnalyzed = false;
669     BBI.IsAnalyzed = true;
670     return BBI;
671   }
672
673   // Do not ifcvt if either path is a back edge to the entry block.
674   if (BBI.TrueBB == BB || BBI.FalseBB == BB) {
675     BBI.IsBeingAnalyzed = false;
676     BBI.IsAnalyzed = true;
677     return BBI;
678   }
679
680   // Do not ifcvt if true and false fallthrough blocks are the same.
681   if (!BBI.FalseBB) {
682     BBI.IsBeingAnalyzed = false;
683     BBI.IsAnalyzed = true;
684     return BBI;
685   }
686
687   BBInfo &TrueBBI  = AnalyzeBlock(BBI.TrueBB, Tokens);
688   BBInfo &FalseBBI = AnalyzeBlock(BBI.FalseBB, Tokens);
689
690   if (TrueBBI.IsDone && FalseBBI.IsDone) {
691     BBI.IsBeingAnalyzed = false;
692     BBI.IsAnalyzed = true;
693     return BBI;
694   }
695
696   SmallVector<MachineOperand, 4> RevCond(BBI.BrCond.begin(), BBI.BrCond.end());
697   bool CanRevCond = !TII->ReverseBranchCondition(RevCond);
698
699   unsigned Dups = 0;
700   unsigned Dups2 = 0;
701   bool TNeedSub = TrueBBI.Predicate.size() > 0;
702   bool FNeedSub = FalseBBI.Predicate.size() > 0;
703   bool Enqueued = false;
704   if (CanRevCond && ValidDiamond(TrueBBI, FalseBBI, Dups, Dups2) &&
705       MeetIfcvtSizeLimit(TrueBBI.NonPredSize - (Dups + Dups2)) &&
706       MeetIfcvtSizeLimit(FalseBBI.NonPredSize - (Dups + Dups2)) &&
707       FeasibilityAnalysis(TrueBBI, BBI.BrCond) &&
708       FeasibilityAnalysis(FalseBBI, RevCond)) {
709     // Diamond:
710     //   EBB
711     //   / \_
712     //  |   |
713     // TBB FBB
714     //   \ /
715     //  TailBB
716     // Note TailBB can be empty.
717     Tokens.push_back(new IfcvtToken(BBI, ICDiamond, TNeedSub|FNeedSub, Dups,
718                                     Dups2));
719     Enqueued = true;
720   }
721
722   if (ValidTriangle(TrueBBI, FalseBBI, false, Dups) &&
723       MeetIfcvtSizeLimit(TrueBBI.NonPredSize) &&
724       FeasibilityAnalysis(TrueBBI, BBI.BrCond, true)) {
725     // Triangle:
726     //   EBB
727     //   | \_
728     //   |  |
729     //   | TBB
730     //   |  /
731     //   FBB
732     Tokens.push_back(new IfcvtToken(BBI, ICTriangle, TNeedSub, Dups));
733     Enqueued = true;
734   }
735   
736   if (ValidTriangle(TrueBBI, FalseBBI, true, Dups) &&
737       MeetIfcvtSizeLimit(TrueBBI.NonPredSize) &&
738       FeasibilityAnalysis(TrueBBI, BBI.BrCond, true, true)) {
739     Tokens.push_back(new IfcvtToken(BBI, ICTriangleRev, TNeedSub, Dups));
740     Enqueued = true;
741   }
742
743   if (ValidSimple(TrueBBI, Dups) &&
744       MeetIfcvtSizeLimit(TrueBBI.NonPredSize) &&
745       FeasibilityAnalysis(TrueBBI, BBI.BrCond)) {
746     // Simple (split, no rejoin):
747     //   EBB
748     //   | \_
749     //   |  |
750     //   | TBB---> exit
751     //   |    
752     //   FBB
753     Tokens.push_back(new IfcvtToken(BBI, ICSimple, TNeedSub, Dups));
754     Enqueued = true;
755   }
756
757   if (CanRevCond) {
758     // Try the other path...
759     if (ValidTriangle(FalseBBI, TrueBBI, false, Dups) &&
760         MeetIfcvtSizeLimit(FalseBBI.NonPredSize) &&
761         FeasibilityAnalysis(FalseBBI, RevCond, true)) {
762       Tokens.push_back(new IfcvtToken(BBI, ICTriangleFalse, FNeedSub, Dups));
763       Enqueued = true;
764     }
765
766     if (ValidTriangle(FalseBBI, TrueBBI, true, Dups) &&
767         MeetIfcvtSizeLimit(FalseBBI.NonPredSize) &&
768         FeasibilityAnalysis(FalseBBI, RevCond, true, true)) {
769       Tokens.push_back(new IfcvtToken(BBI, ICTriangleFRev, FNeedSub, Dups));
770       Enqueued = true;
771     }
772
773     if (ValidSimple(FalseBBI, Dups) &&
774         MeetIfcvtSizeLimit(FalseBBI.NonPredSize) &&
775         FeasibilityAnalysis(FalseBBI, RevCond)) {
776       Tokens.push_back(new IfcvtToken(BBI, ICSimpleFalse, FNeedSub, Dups));
777       Enqueued = true;
778     }
779   }
780
781   BBI.IsEnqueued = Enqueued;
782   BBI.IsBeingAnalyzed = false;
783   BBI.IsAnalyzed = true;
784   return BBI;
785 }
786
787 /// AnalyzeBlocks - Analyze all blocks and find entries for all if-conversion
788 /// candidates. It returns true if any CFG restructuring is done to expose more
789 /// if-conversion opportunities.
790 bool IfConverter::AnalyzeBlocks(MachineFunction &MF,
791                                 std::vector<IfcvtToken*> &Tokens) {
792   bool Change = false;
793   std::set<MachineBasicBlock*> Visited;
794   for (unsigned i = 0, e = Roots.size(); i != e; ++i) {
795     for (idf_ext_iterator<MachineBasicBlock*> I=idf_ext_begin(Roots[i],Visited),
796            E = idf_ext_end(Roots[i], Visited); I != E; ++I) {
797       MachineBasicBlock *BB = *I;
798       AnalyzeBlock(BB, Tokens);
799     }
800   }
801
802   // Sort to favor more complex ifcvt scheme.
803   std::stable_sort(Tokens.begin(), Tokens.end(), IfcvtTokenCmp);
804
805   return Change;
806 }
807
808 /// canFallThroughTo - Returns true either if ToBB is the next block after BB or
809 /// that all the intervening blocks are empty (given BB can fall through to its
810 /// next block).
811 static bool canFallThroughTo(MachineBasicBlock *BB, MachineBasicBlock *ToBB) {
812   MachineFunction::iterator I = BB;
813   MachineFunction::iterator TI = ToBB;
814   MachineFunction::iterator E = BB->getParent()->end();
815   while (++I != TI)
816     if (I == E || !I->empty())
817       return false;
818   return true;
819 }
820
821 /// InvalidatePreds - Invalidate predecessor BB info so it would be re-analyzed
822 /// to determine if it can be if-converted. If predecessor is already enqueued,
823 /// dequeue it!
824 void IfConverter::InvalidatePreds(MachineBasicBlock *BB) {
825   for (MachineBasicBlock::pred_iterator PI = BB->pred_begin(),
826          E = BB->pred_end(); PI != E; ++PI) {
827     BBInfo &PBBI = BBAnalysis[(*PI)->getNumber()];
828     if (PBBI.IsDone || PBBI.BB == BB)
829       continue;
830     PBBI.IsAnalyzed = false;
831     PBBI.IsEnqueued = false;
832   }
833 }
834
835 /// InsertUncondBranch - Inserts an unconditional branch from BB to ToBB.
836 ///
837 static void InsertUncondBranch(MachineBasicBlock *BB, MachineBasicBlock *ToBB,
838                                const TargetInstrInfo *TII) {
839   SmallVector<MachineOperand, 0> NoCond;
840   TII->InsertBranch(*BB, ToBB, NULL, NoCond);
841 }
842
843 /// RemoveExtraEdges - Remove true / false edges if either / both are no longer
844 /// successors.
845 void IfConverter::RemoveExtraEdges(BBInfo &BBI) {
846   MachineBasicBlock *TBB = NULL, *FBB = NULL;
847   SmallVector<MachineOperand, 4> Cond;
848   if (!TII->AnalyzeBranch(*BBI.BB, TBB, FBB, Cond))
849     BBI.BB->CorrectExtraCFGEdges(TBB, FBB, !Cond.empty());
850 }
851
852 /// IfConvertSimple - If convert a simple (split, no rejoin) sub-CFG.
853 ///
854 bool IfConverter::IfConvertSimple(BBInfo &BBI, IfcvtKind Kind) {
855   BBInfo &TrueBBI  = BBAnalysis[BBI.TrueBB->getNumber()];
856   BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
857   BBInfo *CvtBBI = &TrueBBI;
858   BBInfo *NextBBI = &FalseBBI;
859
860   SmallVector<MachineOperand, 4> Cond(BBI.BrCond.begin(), BBI.BrCond.end());
861   if (Kind == ICSimpleFalse)
862     std::swap(CvtBBI, NextBBI);
863
864   if (CvtBBI->IsDone ||
865       (CvtBBI->CannotBeCopied && CvtBBI->BB->pred_size() > 1)) {
866     // Something has changed. It's no longer safe to predicate this block.
867     BBI.IsAnalyzed = false;
868     CvtBBI->IsAnalyzed = false;
869     return false;
870   }
871
872   if (Kind == ICSimpleFalse)
873     if (TII->ReverseBranchCondition(Cond))
874       assert(false && "Unable to reverse branch condition!");
875
876   if (CvtBBI->BB->pred_size() > 1) {
877     BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
878     // Copy instructions in the true block, predicate them, and add them to
879     // the entry block.
880     CopyAndPredicateBlock(BBI, *CvtBBI, Cond);
881   } else {
882     PredicateBlock(*CvtBBI, CvtBBI->BB->end(), Cond);
883
884     // Merge converted block into entry block.
885     BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
886     MergeBlocks(BBI, *CvtBBI);
887   }
888
889   bool IterIfcvt = true;
890   if (!canFallThroughTo(BBI.BB, NextBBI->BB)) {
891     InsertUncondBranch(BBI.BB, NextBBI->BB, TII);
892     BBI.HasFallThrough = false;
893     // Now ifcvt'd block will look like this:
894     // BB:
895     // ...
896     // t, f = cmp
897     // if t op
898     // b BBf
899     //
900     // We cannot further ifcvt this block because the unconditional branch
901     // will have to be predicated on the new condition, that will not be
902     // available if cmp executes.
903     IterIfcvt = false;
904   }
905
906   RemoveExtraEdges(BBI);
907
908   // Update block info. BB can be iteratively if-converted.
909   if (!IterIfcvt)
910     BBI.IsDone = true;
911   InvalidatePreds(BBI.BB);
912   CvtBBI->IsDone = true;
913
914   // FIXME: Must maintain LiveIns.
915   return true;
916 }
917
918 /// IfConvertTriangle - If convert a triangle sub-CFG.
919 ///
920 bool IfConverter::IfConvertTriangle(BBInfo &BBI, IfcvtKind Kind) {
921   BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
922   BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
923   BBInfo *CvtBBI = &TrueBBI;
924   BBInfo *NextBBI = &FalseBBI;
925
926   SmallVector<MachineOperand, 4> Cond(BBI.BrCond.begin(), BBI.BrCond.end());
927   if (Kind == ICTriangleFalse || Kind == ICTriangleFRev)
928     std::swap(CvtBBI, NextBBI);
929
930   if (CvtBBI->IsDone ||
931       (CvtBBI->CannotBeCopied && CvtBBI->BB->pred_size() > 1)) {
932     // Something has changed. It's no longer safe to predicate this block.
933     BBI.IsAnalyzed = false;
934     CvtBBI->IsAnalyzed = false;
935     return false;
936   }
937
938   if (Kind == ICTriangleFalse || Kind == ICTriangleFRev)
939     if (TII->ReverseBranchCondition(Cond))
940       assert(false && "Unable to reverse branch condition!");
941
942   if (Kind == ICTriangleRev || Kind == ICTriangleFRev) {
943     if (ReverseBranchCondition(*CvtBBI)) {
944       // BB has been changed, modify its predecessors (except for this
945       // one) so they don't get ifcvt'ed based on bad intel.
946       for (MachineBasicBlock::pred_iterator PI = CvtBBI->BB->pred_begin(),
947              E = CvtBBI->BB->pred_end(); PI != E; ++PI) {
948         MachineBasicBlock *PBB = *PI;
949         if (PBB == BBI.BB)
950           continue;
951         BBInfo &PBBI = BBAnalysis[PBB->getNumber()];
952         if (PBBI.IsEnqueued) {
953           PBBI.IsAnalyzed = false;
954           PBBI.IsEnqueued = false;
955         }
956       }
957     }
958   }
959
960   bool HasEarlyExit = CvtBBI->FalseBB != NULL;
961   bool DupBB = CvtBBI->BB->pred_size() > 1;
962   if (DupBB) {
963     BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
964     // Copy instructions in the true block, predicate them, and add them to
965     // the entry block.
966     CopyAndPredicateBlock(BBI, *CvtBBI, Cond, true);
967   } else {
968     // Predicate the 'true' block after removing its branch.
969     CvtBBI->NonPredSize -= TII->RemoveBranch(*CvtBBI->BB);
970     PredicateBlock(*CvtBBI, CvtBBI->BB->end(), Cond);
971
972     // Now merge the entry of the triangle with the true block.
973     BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
974     MergeBlocks(BBI, *CvtBBI);
975   }
976
977   // If 'true' block has a 'false' successor, add an exit branch to it.
978   if (HasEarlyExit) {
979     SmallVector<MachineOperand, 4> RevCond(CvtBBI->BrCond.begin(),
980                                            CvtBBI->BrCond.end());
981     if (TII->ReverseBranchCondition(RevCond))
982       assert(false && "Unable to reverse branch condition!");
983     TII->InsertBranch(*BBI.BB, CvtBBI->FalseBB, NULL, RevCond);
984     BBI.BB->addSuccessor(CvtBBI->FalseBB);
985   }
986
987   // Merge in the 'false' block if the 'false' block has no other
988   // predecessors. Otherwise, add an unconditional branch to 'false'.
989   bool FalseBBDead = false;
990   bool IterIfcvt = true;
991   bool isFallThrough = canFallThroughTo(BBI.BB, NextBBI->BB);
992   if (!isFallThrough) {
993     // Only merge them if the true block does not fallthrough to the false
994     // block. By not merging them, we make it possible to iteratively
995     // ifcvt the blocks.
996     if (!HasEarlyExit &&
997         NextBBI->BB->pred_size() == 1 && !NextBBI->HasFallThrough) {
998       MergeBlocks(BBI, *NextBBI);
999       FalseBBDead = true;
1000     } else {
1001       InsertUncondBranch(BBI.BB, NextBBI->BB, TII);
1002       BBI.HasFallThrough = false;
1003     }
1004     // Mixed predicated and unpredicated code. This cannot be iteratively
1005     // predicated.
1006     IterIfcvt = false;
1007   }
1008
1009   RemoveExtraEdges(BBI);
1010
1011   // Update block info. BB can be iteratively if-converted.
1012   if (!IterIfcvt) 
1013     BBI.IsDone = true;
1014   InvalidatePreds(BBI.BB);
1015   CvtBBI->IsDone = true;
1016   if (FalseBBDead)
1017     NextBBI->IsDone = true;
1018
1019   // FIXME: Must maintain LiveIns.
1020   return true;
1021 }
1022
1023 /// IfConvertDiamond - If convert a diamond sub-CFG.
1024 ///
1025 bool IfConverter::IfConvertDiamond(BBInfo &BBI, IfcvtKind Kind,
1026                                    unsigned NumDups1, unsigned NumDups2) {
1027   BBInfo &TrueBBI  = BBAnalysis[BBI.TrueBB->getNumber()];
1028   BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
1029   MachineBasicBlock *TailBB = TrueBBI.TrueBB;
1030   // True block must fall through or end with an unanalyzable terminator.
1031   if (!TailBB) {
1032     if (blockAlwaysFallThrough(TrueBBI))
1033       TailBB = FalseBBI.TrueBB;
1034     assert((TailBB || !TrueBBI.IsBrAnalyzable) && "Unexpected!");
1035   }
1036
1037   if (TrueBBI.IsDone || FalseBBI.IsDone ||
1038       TrueBBI.BB->pred_size() > 1 ||
1039       FalseBBI.BB->pred_size() > 1) {
1040     // Something has changed. It's no longer safe to predicate these blocks.
1041     BBI.IsAnalyzed = false;
1042     TrueBBI.IsAnalyzed = false;
1043     FalseBBI.IsAnalyzed = false;
1044     return false;
1045   }
1046
1047   // Merge the 'true' and 'false' blocks by copying the instructions
1048   // from the 'false' block to the 'true' block. That is, unless the true
1049   // block would clobber the predicate, in that case, do the opposite.
1050   BBInfo *BBI1 = &TrueBBI;
1051   BBInfo *BBI2 = &FalseBBI;
1052   SmallVector<MachineOperand, 4> RevCond(BBI.BrCond.begin(), BBI.BrCond.end());
1053   if (TII->ReverseBranchCondition(RevCond))
1054     assert(false && "Unable to reverse branch condition!");
1055   SmallVector<MachineOperand, 4> *Cond1 = &BBI.BrCond;
1056   SmallVector<MachineOperand, 4> *Cond2 = &RevCond;
1057
1058   // Figure out the more profitable ordering.
1059   bool DoSwap = false;
1060   if (TrueBBI.ClobbersPred && !FalseBBI.ClobbersPred)
1061     DoSwap = true;
1062   else if (TrueBBI.ClobbersPred == FalseBBI.ClobbersPred) {
1063     if (TrueBBI.NonPredSize > FalseBBI.NonPredSize)
1064       DoSwap = true;
1065   }
1066   if (DoSwap) {
1067     std::swap(BBI1, BBI2);
1068     std::swap(Cond1, Cond2);
1069   }
1070
1071   // Remove the conditional branch from entry to the blocks.
1072   BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
1073
1074   // Remove the duplicated instructions at the beginnings of both paths.
1075   MachineBasicBlock::iterator DI1 = BBI1->BB->begin();
1076   MachineBasicBlock::iterator DI2 = BBI2->BB->begin();
1077   BBI1->NonPredSize -= NumDups1;
1078   BBI2->NonPredSize -= NumDups1;
1079   while (NumDups1 != 0) {
1080     ++DI1;
1081     ++DI2;
1082     --NumDups1;
1083   }
1084   BBI.BB->splice(BBI.BB->end(), BBI1->BB, BBI1->BB->begin(), DI1);
1085   BBI2->BB->erase(BBI2->BB->begin(), DI2);
1086
1087   // Predicate the 'true' block after removing its branch.
1088   BBI1->NonPredSize -= TII->RemoveBranch(*BBI1->BB);
1089   DI1 = BBI1->BB->end();
1090   for (unsigned i = 0; i != NumDups2; ++i)
1091     --DI1;
1092   BBI1->BB->erase(DI1, BBI1->BB->end());
1093   PredicateBlock(*BBI1, BBI1->BB->end(), *Cond1);
1094
1095   // Predicate the 'false' block.
1096   BBI2->NonPredSize -= TII->RemoveBranch(*BBI2->BB);
1097   DI2 = BBI2->BB->end();
1098   while (NumDups2 != 0) {
1099     --DI2;
1100     --NumDups2;
1101   }
1102   PredicateBlock(*BBI2, DI2, *Cond2);
1103
1104   // Merge the true block into the entry of the diamond.
1105   MergeBlocks(BBI, *BBI1);
1106   MergeBlocks(BBI, *BBI2);
1107
1108   // If the if-converted block falls through or unconditionally branches into
1109   // the tail block, and the tail block does not have other predecessors, then
1110   // fold the tail block in as well. Otherwise, unless it falls through to the
1111   // tail, add a unconditional branch to it.
1112   if (TailBB) {
1113     BBInfo TailBBI = BBAnalysis[TailBB->getNumber()];
1114     if (TailBB->pred_size() == 1 && !TailBBI.HasFallThrough) {
1115       BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
1116       MergeBlocks(BBI, TailBBI);
1117       TailBBI.IsDone = true;
1118     } else {
1119       InsertUncondBranch(BBI.BB, TailBB, TII);
1120       BBI.HasFallThrough = false;
1121     }
1122   }
1123
1124   RemoveExtraEdges(BBI);
1125
1126   // Update block info.
1127   BBI.IsDone = TrueBBI.IsDone = FalseBBI.IsDone = true;
1128   InvalidatePreds(BBI.BB);
1129
1130   // FIXME: Must maintain LiveIns.
1131   return true;
1132 }
1133
1134 /// PredicateBlock - Predicate instructions from the start of the block to the
1135 /// specified end with the specified condition.
1136 void IfConverter::PredicateBlock(BBInfo &BBI,
1137                                  MachineBasicBlock::iterator E,
1138                                  SmallVectorImpl<MachineOperand> &Cond) {
1139   for (MachineBasicBlock::iterator I = BBI.BB->begin(); I != E; ++I) {
1140     if (TII->isPredicated(I))
1141       continue;
1142     if (!TII->PredicateInstruction(I, Cond)) {
1143 #ifndef NDEBUG
1144       dbgs() << "Unable to predicate " << *I << "!\n";
1145 #endif
1146       llvm_unreachable(0);
1147     }
1148   }
1149
1150   std::copy(Cond.begin(), Cond.end(), std::back_inserter(BBI.Predicate));
1151
1152   BBI.IsAnalyzed = false;
1153   BBI.NonPredSize = 0;
1154
1155   NumIfConvBBs++;
1156 }
1157
1158 /// CopyAndPredicateBlock - Copy and predicate instructions from source BB to
1159 /// the destination block. Skip end of block branches if IgnoreBr is true.
1160 void IfConverter::CopyAndPredicateBlock(BBInfo &ToBBI, BBInfo &FromBBI,
1161                                         SmallVectorImpl<MachineOperand> &Cond,
1162                                         bool IgnoreBr) {
1163   MachineFunction &MF = *ToBBI.BB->getParent();
1164
1165   for (MachineBasicBlock::iterator I = FromBBI.BB->begin(),
1166          E = FromBBI.BB->end(); I != E; ++I) {
1167     const TargetInstrDesc &TID = I->getDesc();
1168     bool isPredicated = TII->isPredicated(I);
1169     // Do not copy the end of the block branches.
1170     if (IgnoreBr && !isPredicated && TID.isBranch())
1171       break;
1172
1173     MachineInstr *MI = MF.CloneMachineInstr(I);
1174     ToBBI.BB->insert(ToBBI.BB->end(), MI);
1175     ToBBI.NonPredSize++;
1176
1177     if (!isPredicated)
1178       if (!TII->PredicateInstruction(MI, Cond)) {
1179 #ifndef NDEBUG
1180         dbgs() << "Unable to predicate " << *I << "!\n";
1181 #endif
1182         llvm_unreachable(0);
1183       }
1184   }
1185
1186   std::vector<MachineBasicBlock *> Succs(FromBBI.BB->succ_begin(),
1187                                          FromBBI.BB->succ_end());
1188   MachineBasicBlock *NBB = getNextBlock(FromBBI.BB);
1189   MachineBasicBlock *FallThrough = FromBBI.HasFallThrough ? NBB : NULL;
1190
1191   for (unsigned i = 0, e = Succs.size(); i != e; ++i) {
1192     MachineBasicBlock *Succ = Succs[i];
1193     // Fallthrough edge can't be transferred.
1194     if (Succ == FallThrough)
1195       continue;
1196     ToBBI.BB->addSuccessor(Succ);
1197   }
1198
1199   std::copy(FromBBI.Predicate.begin(), FromBBI.Predicate.end(),
1200             std::back_inserter(ToBBI.Predicate));
1201   std::copy(Cond.begin(), Cond.end(), std::back_inserter(ToBBI.Predicate));
1202
1203   ToBBI.ClobbersPred |= FromBBI.ClobbersPred;
1204   ToBBI.IsAnalyzed = false;
1205
1206   NumDupBBs++;
1207 }
1208
1209 /// MergeBlocks - Move all instructions from FromBB to the end of ToBB.
1210 ///
1211 void IfConverter::MergeBlocks(BBInfo &ToBBI, BBInfo &FromBBI) {
1212   ToBBI.BB->splice(ToBBI.BB->end(),
1213                    FromBBI.BB, FromBBI.BB->begin(), FromBBI.BB->end());
1214
1215   // Redirect all branches to FromBB to ToBB.
1216   std::vector<MachineBasicBlock *> Preds(FromBBI.BB->pred_begin(),
1217                                          FromBBI.BB->pred_end());
1218   for (unsigned i = 0, e = Preds.size(); i != e; ++i) {
1219     MachineBasicBlock *Pred = Preds[i];
1220     if (Pred == ToBBI.BB)
1221       continue;
1222     Pred->ReplaceUsesOfBlockWith(FromBBI.BB, ToBBI.BB);
1223   }
1224  
1225   std::vector<MachineBasicBlock *> Succs(FromBBI.BB->succ_begin(),
1226                                          FromBBI.BB->succ_end());
1227   MachineBasicBlock *NBB = getNextBlock(FromBBI.BB);
1228   MachineBasicBlock *FallThrough = FromBBI.HasFallThrough ? NBB : NULL;
1229
1230   for (unsigned i = 0, e = Succs.size(); i != e; ++i) {
1231     MachineBasicBlock *Succ = Succs[i];
1232     // Fallthrough edge can't be transferred.
1233     if (Succ == FallThrough)
1234       continue;
1235     FromBBI.BB->removeSuccessor(Succ);
1236     ToBBI.BB->addSuccessor(Succ);
1237   }
1238
1239   // Now FromBBI always falls through to the next block!
1240   if (NBB && !FromBBI.BB->isSuccessor(NBB))
1241     FromBBI.BB->addSuccessor(NBB);
1242
1243   std::copy(FromBBI.Predicate.begin(), FromBBI.Predicate.end(),
1244             std::back_inserter(ToBBI.Predicate));
1245   FromBBI.Predicate.clear();
1246
1247   ToBBI.NonPredSize += FromBBI.NonPredSize;
1248   FromBBI.NonPredSize = 0;
1249
1250   ToBBI.ClobbersPred |= FromBBI.ClobbersPred;
1251   ToBBI.HasFallThrough = FromBBI.HasFallThrough;
1252   ToBBI.IsAnalyzed = false;
1253   FromBBI.IsAnalyzed = false;
1254 }