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, which
11 // tries to convert conditional branches into predicated instructions.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/CodeGen/Passes.h"
16 #include "BranchFolding.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/SmallSet.h"
19 #include "llvm/ADT/Statistic.h"
20 #include "llvm/CodeGen/LivePhysRegs.h"
21 #include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
22 #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
23 #include "llvm/CodeGen/MachineFunctionPass.h"
24 #include "llvm/CodeGen/MachineInstrBuilder.h"
25 #include "llvm/CodeGen/MachineModuleInfo.h"
26 #include "llvm/CodeGen/MachineRegisterInfo.h"
27 #include "llvm/CodeGen/TargetSchedule.h"
28 #include "llvm/Support/CommandLine.h"
29 #include "llvm/Support/Debug.h"
30 #include "llvm/Support/ErrorHandling.h"
31 #include "llvm/Support/raw_ostream.h"
32 #include "llvm/Target/TargetInstrInfo.h"
33 #include "llvm/Target/TargetLowering.h"
34 #include "llvm/Target/TargetRegisterInfo.h"
35 #include "llvm/Target/TargetSubtargetInfo.h"
36 #include <algorithm>
37 #include <utility>
38 
39 using namespace llvm;
40 
41 #define DEBUG_TYPE "ifcvt"
42 
43 // Hidden options for help debugging.
44 static cl::opt<int> IfCvtFnStart("ifcvt-fn-start", cl::init(-1), cl::Hidden);
45 static cl::opt<int> IfCvtFnStop("ifcvt-fn-stop", cl::init(-1), cl::Hidden);
46 static cl::opt<int> IfCvtLimit("ifcvt-limit", cl::init(-1), cl::Hidden);
47 static cl::opt<bool> DisableSimple("disable-ifcvt-simple",
48                                    cl::init(false), cl::Hidden);
49 static cl::opt<bool> DisableSimpleF("disable-ifcvt-simple-false",
50                                     cl::init(false), cl::Hidden);
51 static cl::opt<bool> DisableTriangle("disable-ifcvt-triangle",
52                                      cl::init(false), cl::Hidden);
53 static cl::opt<bool> DisableTriangleR("disable-ifcvt-triangle-rev",
54                                       cl::init(false), cl::Hidden);
55 static cl::opt<bool> DisableTriangleF("disable-ifcvt-triangle-false",
56                                       cl::init(false), cl::Hidden);
57 static cl::opt<bool> DisableTriangleFR("disable-ifcvt-triangle-false-rev",
58                                        cl::init(false), cl::Hidden);
59 static cl::opt<bool> DisableDiamond("disable-ifcvt-diamond",
60                                     cl::init(false), cl::Hidden);
61 static cl::opt<bool> IfCvtBranchFold("ifcvt-branch-fold",
62                                      cl::init(true), cl::Hidden);
63 
64 STATISTIC(NumSimple,       "Number of simple if-conversions performed");
65 STATISTIC(NumSimpleFalse,  "Number of simple (F) if-conversions performed");
66 STATISTIC(NumTriangle,     "Number of triangle if-conversions performed");
67 STATISTIC(NumTriangleRev,  "Number of triangle (R) if-conversions performed");
68 STATISTIC(NumTriangleFalse,"Number of triangle (F) if-conversions performed");
69 STATISTIC(NumTriangleFRev, "Number of triangle (F/R) if-conversions performed");
70 STATISTIC(NumDiamonds,     "Number of diamond if-conversions performed");
71 STATISTIC(NumIfConvBBs,    "Number of if-converted blocks");
72 STATISTIC(NumDupBBs,       "Number of duplicated blocks");
73 STATISTIC(NumUnpred,       "Number of true blocks of diamonds unpredicated");
74 
75 namespace {
76   class IfConverter : public MachineFunctionPass {
77     enum IfcvtKind {
78       ICNotClassfied,  // BB data valid, but not classified.
79       ICSimpleFalse,   // Same as ICSimple, but on the false path.
80       ICSimple,        // BB is entry of an one split, no rejoin sub-CFG.
81       ICTriangleFRev,  // Same as ICTriangleFalse, but false path rev condition.
82       ICTriangleRev,   // Same as ICTriangle, but true path rev condition.
83       ICTriangleFalse, // Same as ICTriangle, but on the false path.
84       ICTriangle,      // BB is entry of a triangle sub-CFG.
85       ICDiamond        // BB is entry of a diamond sub-CFG.
86     };
87 
88     /// One per MachineBasicBlock, this is used to cache the result
89     /// if-conversion feasibility analysis. This includes results from
90     /// TargetInstrInfo::analyzeBranch() (i.e. TBB, FBB, and Cond), and its
91     /// classification, and common tail block of its successors (if it's a
92     /// diamond shape), its size, whether it's predicable, and whether any
93     /// instruction can clobber the 'would-be' predicate.
94     ///
95     /// IsDone          - True if BB is not to be considered for ifcvt.
96     /// IsBeingAnalyzed - True if BB is currently being analyzed.
97     /// IsAnalyzed      - True if BB has been analyzed (info is still valid).
98     /// IsEnqueued      - True if BB has been enqueued to be ifcvt'ed.
99     /// IsBrAnalyzable  - True if analyzeBranch() returns false.
100     /// HasFallThrough  - True if BB may fallthrough to the following BB.
101     /// IsUnpredicable  - True if BB is known to be unpredicable.
102     /// ClobbersPred    - True if BB could modify predicates (e.g. has
103     ///                   cmp, call, etc.)
104     /// NonPredSize     - Number of non-predicated instructions.
105     /// ExtraCost       - Extra cost for multi-cycle instructions.
106     /// ExtraCost2      - Some instructions are slower when predicated
107     /// BB              - Corresponding MachineBasicBlock.
108     /// TrueBB / FalseBB- See analyzeBranch().
109     /// BrCond          - Conditions for end of block conditional branches.
110     /// Predicate       - Predicate used in the BB.
111     struct BBInfo {
112       bool IsDone          : 1;
113       bool IsBeingAnalyzed : 1;
114       bool IsAnalyzed      : 1;
115       bool IsEnqueued      : 1;
116       bool IsBrAnalyzable  : 1;
117       bool HasFallThrough  : 1;
118       bool IsUnpredicable  : 1;
119       bool CannotBeCopied  : 1;
120       bool ClobbersPred    : 1;
121       unsigned NonPredSize;
122       unsigned ExtraCost;
123       unsigned ExtraCost2;
124       MachineBasicBlock *BB;
125       MachineBasicBlock *TrueBB;
126       MachineBasicBlock *FalseBB;
127       SmallVector<MachineOperand, 4> BrCond;
128       SmallVector<MachineOperand, 4> Predicate;
129       BBInfo() : IsDone(false), IsBeingAnalyzed(false),
130                  IsAnalyzed(false), IsEnqueued(false), IsBrAnalyzable(false),
131                  HasFallThrough(false), IsUnpredicable(false),
132                  CannotBeCopied(false), ClobbersPred(false), NonPredSize(0),
133                  ExtraCost(0), ExtraCost2(0), BB(nullptr), TrueBB(nullptr),
134                  FalseBB(nullptr) {}
135     };
136 
137     /// Record information about pending if-conversions to attempt:
138     /// BBI             - Corresponding BBInfo.
139     /// Kind            - Type of block. See IfcvtKind.
140     /// NeedSubsumption - True if the to-be-predicated BB has already been
141     ///                   predicated.
142     /// NumDups      - Number of instructions that would be duplicated due
143     ///                   to this if-conversion. (For diamonds, the number of
144     ///                   identical instructions at the beginnings of both
145     ///                   paths).
146     /// NumDups2     - For diamonds, the number of identical instructions
147     ///                   at the ends of both paths.
148     struct IfcvtToken {
149       BBInfo &BBI;
150       IfcvtKind Kind;
151       bool NeedSubsumption;
152       unsigned NumDups;
153       unsigned NumDups2;
154       IfcvtToken(BBInfo &b, IfcvtKind k, bool s, unsigned d, unsigned d2 = 0)
155         : BBI(b), Kind(k), NeedSubsumption(s), NumDups(d), NumDups2(d2) {}
156     };
157 
158     /// Results of if-conversion feasibility analysis indexed by basic block
159     /// number.
160     std::vector<BBInfo> BBAnalysis;
161     TargetSchedModel SchedModel;
162 
163     const TargetLoweringBase *TLI;
164     const TargetInstrInfo *TII;
165     const TargetRegisterInfo *TRI;
166     const MachineBranchProbabilityInfo *MBPI;
167     MachineRegisterInfo *MRI;
168 
169     LivePhysRegs Redefs;
170     LivePhysRegs DontKill;
171 
172     bool PreRegAlloc;
173     bool MadeChange;
174     int FnNum;
175     std::function<bool(const Function &)> PredicateFtor;
176 
177   public:
178     static char ID;
179     IfConverter(std::function<bool(const Function &)> Ftor = nullptr)
180         : MachineFunctionPass(ID), FnNum(-1), PredicateFtor(std::move(Ftor)) {
181       initializeIfConverterPass(*PassRegistry::getPassRegistry());
182     }
183 
184     void getAnalysisUsage(AnalysisUsage &AU) const override {
185       AU.addRequired<MachineBlockFrequencyInfo>();
186       AU.addRequired<MachineBranchProbabilityInfo>();
187       MachineFunctionPass::getAnalysisUsage(AU);
188     }
189 
190     bool runOnMachineFunction(MachineFunction &MF) override;
191 
192     MachineFunctionProperties getRequiredProperties() const override {
193       return MachineFunctionProperties().set(
194           MachineFunctionProperties::Property::AllVRegsAllocated);
195     }
196 
197   private:
198     bool ReverseBranchCondition(BBInfo &BBI) const;
199     bool ValidSimple(BBInfo &TrueBBI, unsigned &Dups,
200                      BranchProbability Prediction) const;
201     bool ValidTriangle(BBInfo &TrueBBI, BBInfo &FalseBBI,
202                        bool FalseBranch, unsigned &Dups,
203                        BranchProbability Prediction) const;
204     bool ValidDiamond(BBInfo &TrueBBI, BBInfo &FalseBBI,
205                       unsigned &Dups1, unsigned &Dups2) const;
206     void AnalyzeBranches(BBInfo &BBI);
207     void ScanInstructions(BBInfo &BBI,
208                           MachineBasicBlock::iterator &Begin,
209                           MachineBasicBlock::iterator &End) const;
210     void AnalyzeBlock(MachineBasicBlock &MBB,
211                       std::vector<std::unique_ptr<IfcvtToken>> &Tokens);
212     bool FeasibilityAnalysis(BBInfo &BBI, SmallVectorImpl<MachineOperand> &Cond,
213                              bool isTriangle = false, bool RevBranch = false);
214     void AnalyzeBlocks(MachineFunction &MF,
215                        std::vector<std::unique_ptr<IfcvtToken>> &Tokens);
216     void InvalidatePreds(MachineBasicBlock &MBB);
217     void RemoveExtraEdges(BBInfo &BBI);
218     bool IfConvertSimple(BBInfo &BBI, IfcvtKind Kind);
219     bool IfConvertTriangle(BBInfo &BBI, IfcvtKind Kind);
220     bool IfConvertDiamond(BBInfo &BBI, IfcvtKind Kind,
221                           unsigned NumDups1, unsigned NumDups2);
222     void PredicateBlock(BBInfo &BBI,
223                         MachineBasicBlock::iterator E,
224                         SmallVectorImpl<MachineOperand> &Cond,
225                         SmallSet<unsigned, 4> *LaterRedefs = nullptr);
226     void CopyAndPredicateBlock(BBInfo &ToBBI, BBInfo &FromBBI,
227                                SmallVectorImpl<MachineOperand> &Cond,
228                                bool IgnoreBr = false);
229     void MergeBlocks(BBInfo &ToBBI, BBInfo &FromBBI, bool AddEdges = true);
230 
231     bool MeetIfcvtSizeLimit(MachineBasicBlock &BB,
232                             unsigned Cycle, unsigned Extra,
233                             BranchProbability Prediction) const {
234       return Cycle > 0 && TII->isProfitableToIfCvt(BB, Cycle, Extra,
235                                                    Prediction);
236     }
237 
238     bool MeetIfcvtSizeLimit(MachineBasicBlock &TBB,
239                             unsigned TCycle, unsigned TExtra,
240                             MachineBasicBlock &FBB,
241                             unsigned FCycle, unsigned FExtra,
242                             BranchProbability Prediction) const {
243       return TCycle > 0 && FCycle > 0 &&
244         TII->isProfitableToIfCvt(TBB, TCycle, TExtra, FBB, FCycle, FExtra,
245                                  Prediction);
246     }
247 
248     /// Returns true if Block ends without a terminator.
249     bool blockAlwaysFallThrough(BBInfo &BBI) const {
250       return BBI.IsBrAnalyzable && BBI.TrueBB == nullptr;
251     }
252 
253     /// Used to sort if-conversion candidates.
254     static bool IfcvtTokenCmp(const std::unique_ptr<IfcvtToken> &C1,
255                               const std::unique_ptr<IfcvtToken> &C2) {
256       int Incr1 = (C1->Kind == ICDiamond)
257         ? -(int)(C1->NumDups + C1->NumDups2) : (int)C1->NumDups;
258       int Incr2 = (C2->Kind == ICDiamond)
259         ? -(int)(C2->NumDups + C2->NumDups2) : (int)C2->NumDups;
260       if (Incr1 > Incr2)
261         return true;
262       else if (Incr1 == Incr2) {
263         // Favors subsumption.
264         if (!C1->NeedSubsumption && C2->NeedSubsumption)
265           return true;
266         else if (C1->NeedSubsumption == C2->NeedSubsumption) {
267           // Favors diamond over triangle, etc.
268           if ((unsigned)C1->Kind < (unsigned)C2->Kind)
269             return true;
270           else if (C1->Kind == C2->Kind)
271             return C1->BBI.BB->getNumber() < C2->BBI.BB->getNumber();
272         }
273       }
274       return false;
275     }
276   };
277 
278   char IfConverter::ID = 0;
279 }
280 
281 char &llvm::IfConverterID = IfConverter::ID;
282 
283 INITIALIZE_PASS_BEGIN(IfConverter, "if-converter", "If Converter", false, false)
284 INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo)
285 INITIALIZE_PASS_END(IfConverter, "if-converter", "If Converter", false, false)
286 
287 bool IfConverter::runOnMachineFunction(MachineFunction &MF) {
288   if (skipFunction(*MF.getFunction()) ||
289       (PredicateFtor && !PredicateFtor(*MF.getFunction())))
290     return false;
291 
292   const TargetSubtargetInfo &ST = MF.getSubtarget();
293   TLI = ST.getTargetLowering();
294   TII = ST.getInstrInfo();
295   TRI = ST.getRegisterInfo();
296   BranchFolder::MBFIWrapper MBFI(getAnalysis<MachineBlockFrequencyInfo>());
297   MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
298   MRI = &MF.getRegInfo();
299   SchedModel.init(ST.getSchedModel(), &ST, TII);
300 
301   if (!TII) return false;
302 
303   PreRegAlloc = MRI->isSSA();
304 
305   bool BFChange = false;
306   if (!PreRegAlloc) {
307     // Tail merge tend to expose more if-conversion opportunities.
308     BranchFolder BF(true, false, MBFI, *MBPI);
309     BFChange = BF.OptimizeFunction(MF, TII, ST.getRegisterInfo(),
310                                    getAnalysisIfAvailable<MachineModuleInfo>());
311   }
312 
313   DEBUG(dbgs() << "\nIfcvt: function (" << ++FnNum <<  ") \'"
314                << MF.getName() << "\'");
315 
316   if (FnNum < IfCvtFnStart || (IfCvtFnStop != -1 && FnNum > IfCvtFnStop)) {
317     DEBUG(dbgs() << " skipped\n");
318     return false;
319   }
320   DEBUG(dbgs() << "\n");
321 
322   MF.RenumberBlocks();
323   BBAnalysis.resize(MF.getNumBlockIDs());
324 
325   std::vector<std::unique_ptr<IfcvtToken>> Tokens;
326   MadeChange = false;
327   unsigned NumIfCvts = NumSimple + NumSimpleFalse + NumTriangle +
328     NumTriangleRev + NumTriangleFalse + NumTriangleFRev + NumDiamonds;
329   while (IfCvtLimit == -1 || (int)NumIfCvts < IfCvtLimit) {
330     // Do an initial analysis for each basic block and find all the potential
331     // candidates to perform if-conversion.
332     bool Change = false;
333     AnalyzeBlocks(MF, Tokens);
334     while (!Tokens.empty()) {
335       std::unique_ptr<IfcvtToken> Token = std::move(Tokens.back());
336       Tokens.pop_back();
337       BBInfo &BBI = Token->BBI;
338       IfcvtKind Kind = Token->Kind;
339       unsigned NumDups = Token->NumDups;
340       unsigned NumDups2 = Token->NumDups2;
341 
342       // If the block has been evicted out of the queue or it has already been
343       // marked dead (due to it being predicated), then skip it.
344       if (BBI.IsDone)
345         BBI.IsEnqueued = false;
346       if (!BBI.IsEnqueued)
347         continue;
348 
349       BBI.IsEnqueued = false;
350 
351       bool RetVal = false;
352       switch (Kind) {
353       default: llvm_unreachable("Unexpected!");
354       case ICSimple:
355       case ICSimpleFalse: {
356         bool isFalse = Kind == ICSimpleFalse;
357         if ((isFalse && DisableSimpleF) || (!isFalse && DisableSimple)) break;
358         DEBUG(dbgs() << "Ifcvt (Simple" << (Kind == ICSimpleFalse ?
359                                             " false" : "")
360                      << "): BB#" << BBI.BB->getNumber() << " ("
361                      << ((Kind == ICSimpleFalse)
362                          ? BBI.FalseBB->getNumber()
363                          : BBI.TrueBB->getNumber()) << ") ");
364         RetVal = IfConvertSimple(BBI, Kind);
365         DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n");
366         if (RetVal) {
367           if (isFalse) ++NumSimpleFalse;
368           else         ++NumSimple;
369         }
370        break;
371       }
372       case ICTriangle:
373       case ICTriangleRev:
374       case ICTriangleFalse:
375       case ICTriangleFRev: {
376         bool isFalse = Kind == ICTriangleFalse;
377         bool isRev   = (Kind == ICTriangleRev || Kind == ICTriangleFRev);
378         if (DisableTriangle && !isFalse && !isRev) break;
379         if (DisableTriangleR && !isFalse && isRev) break;
380         if (DisableTriangleF && isFalse && !isRev) break;
381         if (DisableTriangleFR && isFalse && isRev) break;
382         DEBUG(dbgs() << "Ifcvt (Triangle");
383         if (isFalse)
384           DEBUG(dbgs() << " false");
385         if (isRev)
386           DEBUG(dbgs() << " rev");
387         DEBUG(dbgs() << "): BB#" << BBI.BB->getNumber() << " (T:"
388                      << BBI.TrueBB->getNumber() << ",F:"
389                      << BBI.FalseBB->getNumber() << ") ");
390         RetVal = IfConvertTriangle(BBI, Kind);
391         DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n");
392         if (RetVal) {
393           if (isFalse) {
394             if (isRev) ++NumTriangleFRev;
395             else       ++NumTriangleFalse;
396           } else {
397             if (isRev) ++NumTriangleRev;
398             else       ++NumTriangle;
399           }
400         }
401         break;
402       }
403       case ICDiamond: {
404         if (DisableDiamond) break;
405         DEBUG(dbgs() << "Ifcvt (Diamond): BB#" << BBI.BB->getNumber() << " (T:"
406                      << BBI.TrueBB->getNumber() << ",F:"
407                      << BBI.FalseBB->getNumber() << ") ");
408         RetVal = IfConvertDiamond(BBI, Kind, NumDups, NumDups2);
409         DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n");
410         if (RetVal) ++NumDiamonds;
411         break;
412       }
413       }
414 
415       Change |= RetVal;
416 
417       NumIfCvts = NumSimple + NumSimpleFalse + NumTriangle + NumTriangleRev +
418         NumTriangleFalse + NumTriangleFRev + NumDiamonds;
419       if (IfCvtLimit != -1 && (int)NumIfCvts >= IfCvtLimit)
420         break;
421     }
422 
423     if (!Change)
424       break;
425     MadeChange |= Change;
426   }
427 
428   Tokens.clear();
429   BBAnalysis.clear();
430 
431   if (MadeChange && IfCvtBranchFold) {
432     BranchFolder BF(false, false, MBFI, *MBPI);
433     BF.OptimizeFunction(MF, TII, MF.getSubtarget().getRegisterInfo(),
434                         getAnalysisIfAvailable<MachineModuleInfo>());
435   }
436 
437   MadeChange |= BFChange;
438   return MadeChange;
439 }
440 
441 /// BB has a fallthrough. Find its 'false' successor given its 'true' successor.
442 static MachineBasicBlock *findFalseBlock(MachineBasicBlock *BB,
443                                          MachineBasicBlock *TrueBB) {
444   for (MachineBasicBlock *SuccBB : BB->successors()) {
445     if (SuccBB != TrueBB)
446       return SuccBB;
447   }
448   return nullptr;
449 }
450 
451 /// Reverse the condition of the end of the block branch. Swap block's 'true'
452 /// and 'false' successors.
453 bool IfConverter::ReverseBranchCondition(BBInfo &BBI) const {
454   DebugLoc dl;  // FIXME: this is nowhere
455   if (!TII->ReverseBranchCondition(BBI.BrCond)) {
456     TII->RemoveBranch(*BBI.BB);
457     TII->InsertBranch(*BBI.BB, BBI.FalseBB, BBI.TrueBB, BBI.BrCond, dl);
458     std::swap(BBI.TrueBB, BBI.FalseBB);
459     return true;
460   }
461   return false;
462 }
463 
464 /// Returns the next block in the function blocks ordering. If it is the end,
465 /// returns NULL.
466 static inline MachineBasicBlock *getNextBlock(MachineBasicBlock &MBB) {
467   MachineFunction::iterator I = MBB.getIterator();
468   MachineFunction::iterator E = MBB.getParent()->end();
469   if (++I == E)
470     return nullptr;
471   return &*I;
472 }
473 
474 /// Returns true if the 'true' block (along with its predecessor) forms a valid
475 /// simple shape for ifcvt. It also returns the number of instructions that the
476 /// ifcvt would need to duplicate if performed in Dups.
477 bool IfConverter::ValidSimple(BBInfo &TrueBBI, unsigned &Dups,
478                               BranchProbability Prediction) const {
479   Dups = 0;
480   if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone)
481     return false;
482 
483   if (TrueBBI.IsBrAnalyzable)
484     return false;
485 
486   if (TrueBBI.BB->pred_size() > 1) {
487     if (TrueBBI.CannotBeCopied ||
488         !TII->isProfitableToDupForIfCvt(*TrueBBI.BB, TrueBBI.NonPredSize,
489                                         Prediction))
490       return false;
491     Dups = TrueBBI.NonPredSize;
492   }
493 
494   return true;
495 }
496 
497 /// Returns true if the 'true' and 'false' blocks (along with their common
498 /// predecessor) forms a valid triangle shape for ifcvt. If 'FalseBranch' is
499 /// true, it checks if 'true' block's false branch branches to the 'false' block
500 /// rather than the other way around. It also returns the number of instructions
501 /// that the ifcvt would need to duplicate if performed in 'Dups'.
502 bool IfConverter::ValidTriangle(BBInfo &TrueBBI, BBInfo &FalseBBI,
503                                 bool FalseBranch, unsigned &Dups,
504                                 BranchProbability Prediction) const {
505   Dups = 0;
506   if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone)
507     return false;
508 
509   if (TrueBBI.BB->pred_size() > 1) {
510     if (TrueBBI.CannotBeCopied)
511       return false;
512 
513     unsigned Size = TrueBBI.NonPredSize;
514     if (TrueBBI.IsBrAnalyzable) {
515       if (TrueBBI.TrueBB && TrueBBI.BrCond.empty())
516         // Ends with an unconditional branch. It will be removed.
517         --Size;
518       else {
519         MachineBasicBlock *FExit = FalseBranch
520           ? TrueBBI.TrueBB : TrueBBI.FalseBB;
521         if (FExit)
522           // Require a conditional branch
523           ++Size;
524       }
525     }
526     if (!TII->isProfitableToDupForIfCvt(*TrueBBI.BB, Size, Prediction))
527       return false;
528     Dups = Size;
529   }
530 
531   MachineBasicBlock *TExit = FalseBranch ? TrueBBI.FalseBB : TrueBBI.TrueBB;
532   if (!TExit && blockAlwaysFallThrough(TrueBBI)) {
533     MachineFunction::iterator I = TrueBBI.BB->getIterator();
534     if (++I == TrueBBI.BB->getParent()->end())
535       return false;
536     TExit = &*I;
537   }
538   return TExit && TExit == FalseBBI.BB;
539 }
540 
541 /// Increment \p It until it points to a non-debug instruction or to \p End.
542 /// @param It Iterator to increment
543 /// @param End Iterator that points to end. Will be compared to It
544 /// @returns true if It == End, false otherwise.
545 static inline bool skipDebugInstructionsForward(
546     MachineBasicBlock::iterator &It,
547     MachineBasicBlock::iterator &End) {
548   while (It != End && It->isDebugValue())
549     It++;
550   return It == End;
551 }
552 
553 /// Decrement \p It until it points to a non-debug instruction or to \p Begin.
554 /// @param It Iterator to decrement.
555 /// @param Begin Iterator that points to beginning. Will be compared to It
556 /// @returns true if It == Begin, false otherwise.
557 static inline bool skipDebugInstructionsBackward(
558     MachineBasicBlock::iterator &It,
559     MachineBasicBlock::iterator &Begin) {
560   while (It != Begin && It->isDebugValue())
561     It--;
562   return It == Begin;
563 }
564 
565 /// Count duplicated instructions and move the iterators to show where they
566 /// are.
567 /// @param TIB True Iterator Begin
568 /// @param FIB False Iterator Begin
569 /// These two iterators initially point to the first instruction of the two
570 /// blocks, and finally point to the first non-shared instruction.
571 /// @param TIE True Iterator End
572 /// @param FIE False Iterator End
573 /// These two iterators initially point to End() for the two blocks() and
574 /// finally point to the first shared instruction in the tail.
575 /// Upon return [TIB, TIE), and [FIB, FIE) mark the un-duplicated portions of
576 /// two blocks.
577 static void countDuplicatedInstructions(
578     MachineBasicBlock::iterator &TIB,
579     MachineBasicBlock::iterator &FIB,
580     MachineBasicBlock::iterator &TIE,
581     MachineBasicBlock::iterator &FIE,
582     unsigned &Dups1, unsigned &Dups2,
583     MachineBasicBlock &TBB, MachineBasicBlock &FBB,
584     bool SkipConditionalBranches) {
585 
586   while (TIB != TIE && FIB != FIE) {
587     // Skip dbg_value instructions. These do not count.
588     if(skipDebugInstructionsForward(TIB, TIE))
589       break;
590     if(skipDebugInstructionsForward(FIB, FIE))
591       break;
592     if (!TIB->isIdenticalTo(*FIB))
593       break;
594     ++Dups1;
595     ++TIB;
596     ++FIB;
597   }
598 
599   // Now, in preparation for counting duplicate instructions at the ends of the
600   // blocks, move the end iterators up past any branch instructions.
601   // If both blocks are returning don't skip the branches, since they will
602   // likely be both identical return instructions. In such cases the return
603   // can be left unpredicated.
604   // Check for already containing all of the block.
605   if (TIB == TIE || FIB == FIE)
606     return;
607   --TIE;
608   --FIE;
609   if (!TBB.succ_empty() || !FBB.succ_empty()) {
610     if (SkipConditionalBranches) {
611       while (TIE != TIB && TIE->isBranch())
612         --TIE;
613       while (FIE != FIB && FIE->isBranch())
614         --FIE;
615     } else {
616       while (TIE != TIB && TIE->isUnconditionalBranch())
617         --TIE;
618       while (FIE != FIB && FIE->isUnconditionalBranch())
619         --FIE;
620     }
621   }
622 
623   // If Dups1 includes all of a block, then don't count duplicate
624   // instructions at the end of the blocks.
625   if (TIB == TIE || FIB == FIE)
626     return;
627 
628   // Count duplicate instructions at the ends of the blocks.
629   while (TIE != TIB && FIE != FIB) {
630     // Skip dbg_value instructions. These do not count.
631     if (skipDebugInstructionsBackward(TIE, TIB))
632       break;
633     if (skipDebugInstructionsBackward(FIE, FIB))
634       break;
635     if (!TIE->isIdenticalTo(*FIE))
636       break;
637     // If we are trying to make sure the conditional branches are the same, we
638     // still don't want to count them.
639     if (SkipConditionalBranches || !TIE->isBranch())
640       ++Dups2;
641     --TIE;
642     --FIE;
643   }
644 }
645 
646 /// ValidDiamond - Returns true if the 'true' and 'false' blocks (along
647 /// with their common predecessor) forms a valid diamond shape for ifcvt.
648 bool IfConverter::ValidDiamond(BBInfo &TrueBBI, BBInfo &FalseBBI,
649                                unsigned &Dups1, unsigned &Dups2) const {
650   Dups1 = Dups2 = 0;
651   if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone ||
652       FalseBBI.IsBeingAnalyzed || FalseBBI.IsDone)
653     return false;
654 
655   MachineBasicBlock *TT = TrueBBI.TrueBB;
656   MachineBasicBlock *FT = FalseBBI.TrueBB;
657 
658   if (!TT && blockAlwaysFallThrough(TrueBBI))
659     TT = getNextBlock(*TrueBBI.BB);
660   if (!FT && blockAlwaysFallThrough(FalseBBI))
661     FT = getNextBlock(*FalseBBI.BB);
662   if (TT != FT)
663     return false;
664   if (!TT && (TrueBBI.IsBrAnalyzable || FalseBBI.IsBrAnalyzable))
665     return false;
666   if  (TrueBBI.BB->pred_size() > 1 || FalseBBI.BB->pred_size() > 1)
667     return false;
668 
669   // FIXME: Allow true block to have an early exit?
670   if (TrueBBI.FalseBB || FalseBBI.FalseBB ||
671       (TrueBBI.ClobbersPred && FalseBBI.ClobbersPred))
672     return false;
673 
674   // Count duplicate instructions at the beginning and end of the true and
675   // false blocks.
676   MachineBasicBlock::iterator TIB = TrueBBI.BB->begin();
677   MachineBasicBlock::iterator FIB = FalseBBI.BB->begin();
678   MachineBasicBlock::iterator TIE = TrueBBI.BB->end();
679   MachineBasicBlock::iterator FIE = FalseBBI.BB->end();
680   countDuplicatedInstructions(TIB, FIB, TIE, FIE, Dups1, Dups2,
681                               *TrueBBI.BB, *FalseBBI.BB,
682                               /* SkipConditionalBranches */ true);
683   return true;
684 }
685 
686 /// AnalyzeBranches - Look at the branches at the end of a block to determine if
687 /// the block is predicable.
688 void IfConverter::AnalyzeBranches(BBInfo &BBI) {
689   if (BBI.IsDone)
690     return;
691 
692   BBI.TrueBB = BBI.FalseBB = nullptr;
693   BBI.BrCond.clear();
694   BBI.IsBrAnalyzable =
695       !TII->analyzeBranch(*BBI.BB, BBI.TrueBB, BBI.FalseBB, BBI.BrCond);
696   BBI.HasFallThrough = BBI.IsBrAnalyzable && BBI.FalseBB == nullptr;
697 
698   if (BBI.BrCond.size()) {
699     // No false branch. This BB must end with a conditional branch and a
700     // fallthrough.
701     if (!BBI.FalseBB)
702       BBI.FalseBB = findFalseBlock(BBI.BB, BBI.TrueBB);
703     if (!BBI.FalseBB) {
704       // Malformed bcc? True and false blocks are the same?
705       BBI.IsUnpredicable = true;
706     }
707   }
708 }
709 
710 /// ScanInstructions - Scan all the instructions in the block to determine if
711 /// the block is predicable. In most cases, that means all the instructions
712 /// in the block are isPredicable(). Also checks if the block contains any
713 /// instruction which can clobber a predicate (e.g. condition code register).
714 /// If so, the block is not predicable unless it's the last instruction.
715 void IfConverter::ScanInstructions(BBInfo &BBI,
716                                    MachineBasicBlock::iterator &Begin,
717                                    MachineBasicBlock::iterator &End) const {
718   if (BBI.IsDone || BBI.IsUnpredicable)
719     return;
720 
721   bool AlreadyPredicated = !BBI.Predicate.empty();
722 
723   BBI.NonPredSize = 0;
724   BBI.ExtraCost = 0;
725   BBI.ExtraCost2 = 0;
726   BBI.ClobbersPred = false;
727   for (MachineInstr &MI : make_range(Begin, End)) {
728     if (MI.isDebugValue())
729       continue;
730 
731     // It's unsafe to duplicate convergent instructions in this context, so set
732     // BBI.CannotBeCopied to true if MI is convergent.  To see why, consider the
733     // following CFG, which is subject to our "simple" transformation.
734     //
735     //    BB0     // if (c1) goto BB1; else goto BB2;
736     //   /   \
737     //  BB1   |
738     //   |   BB2  // if (c2) goto TBB; else goto FBB;
739     //   |   / |
740     //   |  /  |
741     //   TBB   |
742     //    |    |
743     //    |   FBB
744     //    |
745     //    exit
746     //
747     // Suppose we want to move TBB's contents up into BB1 and BB2 (in BB1 they'd
748     // be unconditional, and in BB2, they'd be predicated upon c2), and suppose
749     // TBB contains a convergent instruction.  This is safe iff doing so does
750     // not add a control-flow dependency to the convergent instruction -- i.e.,
751     // it's safe iff the set of control flows that leads us to the convergent
752     // instruction does not get smaller after the transformation.
753     //
754     // Originally we executed TBB if c1 || c2.  After the transformation, there
755     // are two copies of TBB's instructions.  We get to the first if c1, and we
756     // get to the second if !c1 && c2.
757     //
758     // There are clearly fewer ways to satisfy the condition "c1" than
759     // "c1 || c2".  Since we've shrunk the set of control flows which lead to
760     // our convergent instruction, the transformation is unsafe.
761     if (MI.isNotDuplicable() || MI.isConvergent())
762       BBI.CannotBeCopied = true;
763 
764     bool isPredicated = TII->isPredicated(MI);
765     bool isCondBr = BBI.IsBrAnalyzable && MI.isConditionalBranch();
766 
767     // A conditional branch is not predicable, but it may be eliminated.
768     if (isCondBr)
769       continue;
770 
771     if (!isPredicated) {
772       BBI.NonPredSize++;
773       unsigned ExtraPredCost = TII->getPredicationCost(MI);
774       unsigned NumCycles = SchedModel.computeInstrLatency(&MI, false);
775       if (NumCycles > 1)
776         BBI.ExtraCost += NumCycles-1;
777       BBI.ExtraCost2 += ExtraPredCost;
778     } else if (!AlreadyPredicated) {
779       // FIXME: This instruction is already predicated before the
780       // if-conversion pass. It's probably something like a conditional move.
781       // Mark this block unpredicable for now.
782       BBI.IsUnpredicable = true;
783       return;
784     }
785 
786     if (BBI.ClobbersPred && !isPredicated) {
787       // Predicate modification instruction should end the block (except for
788       // already predicated instructions and end of block branches).
789       // Predicate may have been modified, the subsequent (currently)
790       // unpredicated instructions cannot be correctly predicated.
791       BBI.IsUnpredicable = true;
792       return;
793     }
794 
795     // FIXME: Make use of PredDefs? e.g. ADDC, SUBC sets predicates but are
796     // still potentially predicable.
797     std::vector<MachineOperand> PredDefs;
798     if (TII->DefinesPredicate(MI, PredDefs))
799       BBI.ClobbersPred = true;
800 
801     if (!TII->isPredicable(MI)) {
802       BBI.IsUnpredicable = true;
803       return;
804     }
805   }
806 }
807 
808 /// Determine if the block is a suitable candidate to be predicated by the
809 /// specified predicate.
810 bool IfConverter::FeasibilityAnalysis(BBInfo &BBI,
811                                       SmallVectorImpl<MachineOperand> &Pred,
812                                       bool isTriangle, bool RevBranch) {
813   // If the block is dead or unpredicable, then it cannot be predicated.
814   if (BBI.IsDone || BBI.IsUnpredicable)
815     return false;
816 
817   // If it is already predicated but we couldn't analyze its terminator, the
818   // latter might fallthrough, but we can't determine where to.
819   // Conservatively avoid if-converting again.
820   if (BBI.Predicate.size() && !BBI.IsBrAnalyzable)
821     return false;
822 
823   // If it is already predicated, check if the new predicate subsumes
824   // its predicate.
825   if (BBI.Predicate.size() && !TII->SubsumesPredicate(Pred, BBI.Predicate))
826     return false;
827 
828   if (BBI.BrCond.size()) {
829     if (!isTriangle)
830       return false;
831 
832     // Test predicate subsumption.
833     SmallVector<MachineOperand, 4> RevPred(Pred.begin(), Pred.end());
834     SmallVector<MachineOperand, 4> Cond(BBI.BrCond.begin(), BBI.BrCond.end());
835     if (RevBranch) {
836       if (TII->ReverseBranchCondition(Cond))
837         return false;
838     }
839     if (TII->ReverseBranchCondition(RevPred) ||
840         !TII->SubsumesPredicate(Cond, RevPred))
841       return false;
842   }
843 
844   return true;
845 }
846 
847 /// Analyze the structure of the sub-CFG starting from the specified block.
848 /// Record its successors and whether it looks like an if-conversion candidate.
849 void IfConverter::AnalyzeBlock(
850     MachineBasicBlock &MBB, std::vector<std::unique_ptr<IfcvtToken>> &Tokens) {
851   struct BBState {
852     BBState(MachineBasicBlock &MBB) : MBB(&MBB), SuccsAnalyzed(false) {}
853     MachineBasicBlock *MBB;
854 
855     /// This flag is true if MBB's successors have been analyzed.
856     bool SuccsAnalyzed;
857   };
858 
859   // Push MBB to the stack.
860   SmallVector<BBState, 16> BBStack(1, MBB);
861 
862   while (!BBStack.empty()) {
863     BBState &State = BBStack.back();
864     MachineBasicBlock *BB = State.MBB;
865     BBInfo &BBI = BBAnalysis[BB->getNumber()];
866 
867     if (!State.SuccsAnalyzed) {
868       if (BBI.IsAnalyzed || BBI.IsBeingAnalyzed) {
869         BBStack.pop_back();
870         continue;
871       }
872 
873       BBI.BB = BB;
874       BBI.IsBeingAnalyzed = true;
875 
876       AnalyzeBranches(BBI);
877       MachineBasicBlock::iterator Begin = BBI.BB->begin();
878       MachineBasicBlock::iterator End = BBI.BB->end();
879       ScanInstructions(BBI, Begin, End);
880 
881       // Unanalyzable or ends with fallthrough or unconditional branch, or if is
882       // not considered for ifcvt anymore.
883       if (!BBI.IsBrAnalyzable || BBI.BrCond.empty() || BBI.IsDone) {
884         BBI.IsBeingAnalyzed = false;
885         BBI.IsAnalyzed = true;
886         BBStack.pop_back();
887         continue;
888       }
889 
890       // Do not ifcvt if either path is a back edge to the entry block.
891       if (BBI.TrueBB == BB || BBI.FalseBB == BB) {
892         BBI.IsBeingAnalyzed = false;
893         BBI.IsAnalyzed = true;
894         BBStack.pop_back();
895         continue;
896       }
897 
898       // Do not ifcvt if true and false fallthrough blocks are the same.
899       if (!BBI.FalseBB) {
900         BBI.IsBeingAnalyzed = false;
901         BBI.IsAnalyzed = true;
902         BBStack.pop_back();
903         continue;
904       }
905 
906       // Push the False and True blocks to the stack.
907       State.SuccsAnalyzed = true;
908       BBStack.push_back(*BBI.FalseBB);
909       BBStack.push_back(*BBI.TrueBB);
910       continue;
911     }
912 
913     BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
914     BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
915 
916     if (TrueBBI.IsDone && FalseBBI.IsDone) {
917       BBI.IsBeingAnalyzed = false;
918       BBI.IsAnalyzed = true;
919       BBStack.pop_back();
920       continue;
921     }
922 
923     SmallVector<MachineOperand, 4>
924         RevCond(BBI.BrCond.begin(), BBI.BrCond.end());
925     bool CanRevCond = !TII->ReverseBranchCondition(RevCond);
926 
927     unsigned Dups = 0;
928     unsigned Dups2 = 0;
929     bool TNeedSub = !TrueBBI.Predicate.empty();
930     bool FNeedSub = !FalseBBI.Predicate.empty();
931     bool Enqueued = false;
932 
933     BranchProbability Prediction = MBPI->getEdgeProbability(BB, TrueBBI.BB);
934 
935     if (CanRevCond && ValidDiamond(TrueBBI, FalseBBI, Dups, Dups2) &&
936         MeetIfcvtSizeLimit(*TrueBBI.BB, (TrueBBI.NonPredSize - (Dups + Dups2) +
937                                          TrueBBI.ExtraCost), TrueBBI.ExtraCost2,
938                            *FalseBBI.BB, (FalseBBI.NonPredSize - (Dups + Dups2) +
939                                         FalseBBI.ExtraCost),FalseBBI.ExtraCost2,
940                          Prediction) &&
941         FeasibilityAnalysis(TrueBBI, BBI.BrCond) &&
942         FeasibilityAnalysis(FalseBBI, RevCond)) {
943       // Diamond:
944       //   EBB
945       //   / \_
946       //  |   |
947       // TBB FBB
948       //   \ /
949       //  TailBB
950       // Note TailBB can be empty.
951       Tokens.push_back(llvm::make_unique<IfcvtToken>(
952           BBI, ICDiamond, TNeedSub | FNeedSub, Dups, Dups2));
953       Enqueued = true;
954     }
955 
956     if (ValidTriangle(TrueBBI, FalseBBI, false, Dups, Prediction) &&
957         MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize + TrueBBI.ExtraCost,
958                            TrueBBI.ExtraCost2, Prediction) &&
959         FeasibilityAnalysis(TrueBBI, BBI.BrCond, true)) {
960       // Triangle:
961       //   EBB
962       //   | \_
963       //   |  |
964       //   | TBB
965       //   |  /
966       //   FBB
967       Tokens.push_back(
968           llvm::make_unique<IfcvtToken>(BBI, ICTriangle, TNeedSub, Dups));
969       Enqueued = true;
970     }
971 
972     if (ValidTriangle(TrueBBI, FalseBBI, true, Dups, Prediction) &&
973         MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize + TrueBBI.ExtraCost,
974                            TrueBBI.ExtraCost2, Prediction) &&
975         FeasibilityAnalysis(TrueBBI, BBI.BrCond, true, true)) {
976       Tokens.push_back(
977           llvm::make_unique<IfcvtToken>(BBI, ICTriangleRev, TNeedSub, Dups));
978       Enqueued = true;
979     }
980 
981     if (ValidSimple(TrueBBI, Dups, Prediction) &&
982         MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize + TrueBBI.ExtraCost,
983                            TrueBBI.ExtraCost2, Prediction) &&
984         FeasibilityAnalysis(TrueBBI, BBI.BrCond)) {
985       // Simple (split, no rejoin):
986       //   EBB
987       //   | \_
988       //   |  |
989       //   | TBB---> exit
990       //   |
991       //   FBB
992       Tokens.push_back(
993           llvm::make_unique<IfcvtToken>(BBI, ICSimple, TNeedSub, Dups));
994       Enqueued = true;
995     }
996 
997     if (CanRevCond) {
998       // Try the other path...
999       if (ValidTriangle(FalseBBI, TrueBBI, false, Dups,
1000                         Prediction.getCompl()) &&
1001           MeetIfcvtSizeLimit(*FalseBBI.BB,
1002                              FalseBBI.NonPredSize + FalseBBI.ExtraCost,
1003                              FalseBBI.ExtraCost2, Prediction.getCompl()) &&
1004           FeasibilityAnalysis(FalseBBI, RevCond, true)) {
1005         Tokens.push_back(llvm::make_unique<IfcvtToken>(BBI, ICTriangleFalse,
1006                                                        FNeedSub, Dups));
1007         Enqueued = true;
1008       }
1009 
1010       if (ValidTriangle(FalseBBI, TrueBBI, true, Dups,
1011                         Prediction.getCompl()) &&
1012           MeetIfcvtSizeLimit(*FalseBBI.BB,
1013                              FalseBBI.NonPredSize + FalseBBI.ExtraCost,
1014                            FalseBBI.ExtraCost2, Prediction.getCompl()) &&
1015         FeasibilityAnalysis(FalseBBI, RevCond, true, true)) {
1016         Tokens.push_back(
1017             llvm::make_unique<IfcvtToken>(BBI, ICTriangleFRev, FNeedSub, Dups));
1018         Enqueued = true;
1019       }
1020 
1021       if (ValidSimple(FalseBBI, Dups, Prediction.getCompl()) &&
1022           MeetIfcvtSizeLimit(*FalseBBI.BB,
1023                              FalseBBI.NonPredSize + FalseBBI.ExtraCost,
1024                              FalseBBI.ExtraCost2, Prediction.getCompl()) &&
1025           FeasibilityAnalysis(FalseBBI, RevCond)) {
1026         Tokens.push_back(
1027             llvm::make_unique<IfcvtToken>(BBI, ICSimpleFalse, FNeedSub, Dups));
1028         Enqueued = true;
1029       }
1030     }
1031 
1032     BBI.IsEnqueued = Enqueued;
1033     BBI.IsBeingAnalyzed = false;
1034     BBI.IsAnalyzed = true;
1035     BBStack.pop_back();
1036   }
1037 }
1038 
1039 /// Analyze all blocks and find entries for all if-conversion candidates.
1040 void IfConverter::AnalyzeBlocks(
1041     MachineFunction &MF, std::vector<std::unique_ptr<IfcvtToken>> &Tokens) {
1042   for (MachineBasicBlock &MBB : MF)
1043     AnalyzeBlock(MBB, Tokens);
1044 
1045   // Sort to favor more complex ifcvt scheme.
1046   std::stable_sort(Tokens.begin(), Tokens.end(), IfcvtTokenCmp);
1047 }
1048 
1049 /// Returns true either if ToMBB is the next block after MBB or that all the
1050 /// intervening blocks are empty (given MBB can fall through to its next block).
1051 static bool canFallThroughTo(MachineBasicBlock &MBB, MachineBasicBlock &ToMBB) {
1052   MachineFunction::iterator PI = MBB.getIterator();
1053   MachineFunction::iterator I = std::next(PI);
1054   MachineFunction::iterator TI = ToMBB.getIterator();
1055   MachineFunction::iterator E = MBB.getParent()->end();
1056   while (I != TI) {
1057     // Check isSuccessor to avoid case where the next block is empty, but
1058     // it's not a successor.
1059     if (I == E || !I->empty() || !PI->isSuccessor(&*I))
1060       return false;
1061     PI = I++;
1062   }
1063   return true;
1064 }
1065 
1066 /// Invalidate predecessor BB info so it would be re-analyzed to determine if it
1067 /// can be if-converted. If predecessor is already enqueued, dequeue it!
1068 void IfConverter::InvalidatePreds(MachineBasicBlock &MBB) {
1069   for (const MachineBasicBlock *Predecessor : MBB.predecessors()) {
1070     BBInfo &PBBI = BBAnalysis[Predecessor->getNumber()];
1071     if (PBBI.IsDone || PBBI.BB == &MBB)
1072       continue;
1073     PBBI.IsAnalyzed = false;
1074     PBBI.IsEnqueued = false;
1075   }
1076 }
1077 
1078 /// Inserts an unconditional branch from \p MBB to \p ToMBB.
1079 static void InsertUncondBranch(MachineBasicBlock &MBB, MachineBasicBlock &ToMBB,
1080                                const TargetInstrInfo *TII) {
1081   DebugLoc dl;  // FIXME: this is nowhere
1082   SmallVector<MachineOperand, 0> NoCond;
1083   TII->InsertBranch(MBB, &ToMBB, nullptr, NoCond, dl);
1084 }
1085 
1086 /// Remove true / false edges if either / both are no longer successors.
1087 void IfConverter::RemoveExtraEdges(BBInfo &BBI) {
1088   MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
1089   SmallVector<MachineOperand, 4> Cond;
1090   if (!TII->analyzeBranch(*BBI.BB, TBB, FBB, Cond))
1091     BBI.BB->CorrectExtraCFGEdges(TBB, FBB, !Cond.empty());
1092 }
1093 
1094 /// Behaves like LiveRegUnits::StepForward() but also adds implicit uses to all
1095 /// values defined in MI which are also live/used by MI.
1096 static void UpdatePredRedefs(MachineInstr &MI, LivePhysRegs &Redefs) {
1097   const TargetRegisterInfo *TRI = MI.getParent()->getParent()
1098     ->getSubtarget().getRegisterInfo();
1099 
1100   // Before stepping forward past MI, remember which regs were live
1101   // before MI. This is needed to set the Undef flag only when reg is
1102   // dead.
1103   SparseSet<unsigned> LiveBeforeMI;
1104   LiveBeforeMI.setUniverse(TRI->getNumRegs());
1105   for (unsigned Reg : Redefs)
1106     LiveBeforeMI.insert(Reg);
1107 
1108   SmallVector<std::pair<unsigned, const MachineOperand*>, 4> Clobbers;
1109   Redefs.stepForward(MI, Clobbers);
1110 
1111   // Now add the implicit uses for each of the clobbered values.
1112   for (auto Clobber : Clobbers) {
1113     // FIXME: Const cast here is nasty, but better than making StepForward
1114     // take a mutable instruction instead of const.
1115     unsigned Reg = Clobber.first;
1116     MachineOperand &Op = const_cast<MachineOperand&>(*Clobber.second);
1117     MachineInstr *OpMI = Op.getParent();
1118     MachineInstrBuilder MIB(*OpMI->getParent()->getParent(), OpMI);
1119     if (Op.isRegMask()) {
1120       // First handle regmasks.  They clobber any entries in the mask which
1121       // means that we need a def for those registers.
1122       if (LiveBeforeMI.count(Reg))
1123         MIB.addReg(Reg, RegState::Implicit);
1124 
1125       // We also need to add an implicit def of this register for the later
1126       // use to read from.
1127       // For the register allocator to have allocated a register clobbered
1128       // by the call which is used later, it must be the case that
1129       // the call doesn't return.
1130       MIB.addReg(Reg, RegState::Implicit | RegState::Define);
1131       continue;
1132     }
1133     assert(Op.isReg() && "Register operand required");
1134     if (Op.isDead()) {
1135       // If we found a dead def, but it needs to be live, then remove the dead
1136       // flag.
1137       if (Redefs.contains(Op.getReg()))
1138         Op.setIsDead(false);
1139     }
1140     if (LiveBeforeMI.count(Reg))
1141       MIB.addReg(Reg, RegState::Implicit);
1142   }
1143 }
1144 
1145 /// Remove kill flags from operands with a registers in the \p DontKill set.
1146 static void RemoveKills(MachineInstr &MI, const LivePhysRegs &DontKill) {
1147   for (MIBundleOperands O(MI); O.isValid(); ++O) {
1148     if (!O->isReg() || !O->isKill())
1149       continue;
1150     if (DontKill.contains(O->getReg()))
1151       O->setIsKill(false);
1152   }
1153 }
1154 
1155 /// Walks a range of machine instructions and removes kill flags for registers
1156 /// in the \p DontKill set.
1157 static void RemoveKills(MachineBasicBlock::iterator I,
1158                         MachineBasicBlock::iterator E,
1159                         const LivePhysRegs &DontKill,
1160                         const MCRegisterInfo &MCRI) {
1161   for (MachineInstr &MI : make_range(I, E))
1162     RemoveKills(MI, DontKill);
1163 }
1164 
1165 /// If convert a simple (split, no rejoin) sub-CFG.
1166 bool IfConverter::IfConvertSimple(BBInfo &BBI, IfcvtKind Kind) {
1167   BBInfo &TrueBBI  = BBAnalysis[BBI.TrueBB->getNumber()];
1168   BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
1169   BBInfo *CvtBBI = &TrueBBI;
1170   BBInfo *NextBBI = &FalseBBI;
1171 
1172   SmallVector<MachineOperand, 4> Cond(BBI.BrCond.begin(), BBI.BrCond.end());
1173   if (Kind == ICSimpleFalse)
1174     std::swap(CvtBBI, NextBBI);
1175 
1176   MachineBasicBlock &CvtMBB = *CvtBBI->BB;
1177   MachineBasicBlock &NextMBB = *NextBBI->BB;
1178   if (CvtBBI->IsDone ||
1179       (CvtBBI->CannotBeCopied && CvtMBB.pred_size() > 1)) {
1180     // Something has changed. It's no longer safe to predicate this block.
1181     BBI.IsAnalyzed = false;
1182     CvtBBI->IsAnalyzed = false;
1183     return false;
1184   }
1185 
1186   if (CvtMBB.hasAddressTaken())
1187     // Conservatively abort if-conversion if BB's address is taken.
1188     return false;
1189 
1190   if (Kind == ICSimpleFalse)
1191     if (TII->ReverseBranchCondition(Cond))
1192       llvm_unreachable("Unable to reverse branch condition!");
1193 
1194   // Initialize liveins to the first BB. These are potentiall redefined by
1195   // predicated instructions.
1196   Redefs.init(TRI);
1197   Redefs.addLiveIns(CvtMBB);
1198   Redefs.addLiveIns(NextMBB);
1199 
1200   // Compute a set of registers which must not be killed by instructions in
1201   // BB1: This is everything live-in to BB2.
1202   DontKill.init(TRI);
1203   DontKill.addLiveIns(NextMBB);
1204 
1205   if (CvtMBB.pred_size() > 1) {
1206     BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
1207     // Copy instructions in the true block, predicate them, and add them to
1208     // the entry block.
1209     CopyAndPredicateBlock(BBI, *CvtBBI, Cond);
1210 
1211     // RemoveExtraEdges won't work if the block has an unanalyzable branch, so
1212     // explicitly remove CvtBBI as a successor.
1213     BBI.BB->removeSuccessor(&CvtMBB, true);
1214   } else {
1215     RemoveKills(CvtMBB.begin(), CvtMBB.end(), DontKill, *TRI);
1216     PredicateBlock(*CvtBBI, CvtMBB.end(), Cond);
1217 
1218     // Merge converted block into entry block.
1219     BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
1220     MergeBlocks(BBI, *CvtBBI);
1221   }
1222 
1223   bool IterIfcvt = true;
1224   if (!canFallThroughTo(*BBI.BB, NextMBB)) {
1225     InsertUncondBranch(*BBI.BB, NextMBB, TII);
1226     BBI.HasFallThrough = false;
1227     // Now ifcvt'd block will look like this:
1228     // BB:
1229     // ...
1230     // t, f = cmp
1231     // if t op
1232     // b BBf
1233     //
1234     // We cannot further ifcvt this block because the unconditional branch
1235     // will have to be predicated on the new condition, that will not be
1236     // available if cmp executes.
1237     IterIfcvt = false;
1238   }
1239 
1240   RemoveExtraEdges(BBI);
1241 
1242   // Update block info. BB can be iteratively if-converted.
1243   if (!IterIfcvt)
1244     BBI.IsDone = true;
1245   InvalidatePreds(*BBI.BB);
1246   CvtBBI->IsDone = true;
1247 
1248   // FIXME: Must maintain LiveIns.
1249   return true;
1250 }
1251 
1252 /// If convert a triangle sub-CFG.
1253 bool IfConverter::IfConvertTriangle(BBInfo &BBI, IfcvtKind Kind) {
1254   BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()];
1255   BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
1256   BBInfo *CvtBBI = &TrueBBI;
1257   BBInfo *NextBBI = &FalseBBI;
1258   DebugLoc dl;  // FIXME: this is nowhere
1259 
1260   SmallVector<MachineOperand, 4> Cond(BBI.BrCond.begin(), BBI.BrCond.end());
1261   if (Kind == ICTriangleFalse || Kind == ICTriangleFRev)
1262     std::swap(CvtBBI, NextBBI);
1263 
1264   MachineBasicBlock &CvtMBB = *CvtBBI->BB;
1265   MachineBasicBlock &NextMBB = *NextBBI->BB;
1266   if (CvtBBI->IsDone ||
1267       (CvtBBI->CannotBeCopied && CvtMBB.pred_size() > 1)) {
1268     // Something has changed. It's no longer safe to predicate this block.
1269     BBI.IsAnalyzed = false;
1270     CvtBBI->IsAnalyzed = false;
1271     return false;
1272   }
1273 
1274   if (CvtMBB.hasAddressTaken())
1275     // Conservatively abort if-conversion if BB's address is taken.
1276     return false;
1277 
1278   if (Kind == ICTriangleFalse || Kind == ICTriangleFRev)
1279     if (TII->ReverseBranchCondition(Cond))
1280       llvm_unreachable("Unable to reverse branch condition!");
1281 
1282   if (Kind == ICTriangleRev || Kind == ICTriangleFRev) {
1283     if (ReverseBranchCondition(*CvtBBI)) {
1284       // BB has been changed, modify its predecessors (except for this
1285       // one) so they don't get ifcvt'ed based on bad intel.
1286       for (MachineBasicBlock *PBB : CvtMBB.predecessors()) {
1287         if (PBB == BBI.BB)
1288           continue;
1289         BBInfo &PBBI = BBAnalysis[PBB->getNumber()];
1290         if (PBBI.IsEnqueued) {
1291           PBBI.IsAnalyzed = false;
1292           PBBI.IsEnqueued = false;
1293         }
1294       }
1295     }
1296   }
1297 
1298   // Initialize liveins to the first BB. These are potentially redefined by
1299   // predicated instructions.
1300   Redefs.init(TRI);
1301   Redefs.addLiveIns(CvtMBB);
1302   Redefs.addLiveIns(NextMBB);
1303 
1304   DontKill.clear();
1305 
1306   bool HasEarlyExit = CvtBBI->FalseBB != nullptr;
1307   BranchProbability CvtNext, CvtFalse, BBNext, BBCvt;
1308 
1309   if (HasEarlyExit) {
1310     // Get probabilities before modifying CvtMBB and BBI.BB.
1311     CvtNext = MBPI->getEdgeProbability(&CvtMBB, &NextMBB);
1312     CvtFalse = MBPI->getEdgeProbability(&CvtMBB, CvtBBI->FalseBB);
1313     BBNext = MBPI->getEdgeProbability(BBI.BB, &NextMBB);
1314     BBCvt = MBPI->getEdgeProbability(BBI.BB, &CvtMBB);
1315   }
1316 
1317   if (CvtMBB.pred_size() > 1) {
1318     BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
1319     // Copy instructions in the true block, predicate them, and add them to
1320     // the entry block.
1321     CopyAndPredicateBlock(BBI, *CvtBBI, Cond, true);
1322 
1323     // RemoveExtraEdges won't work if the block has an unanalyzable branch, so
1324     // explicitly remove CvtBBI as a successor.
1325     BBI.BB->removeSuccessor(&CvtMBB, true);
1326   } else {
1327     // Predicate the 'true' block after removing its branch.
1328     CvtBBI->NonPredSize -= TII->RemoveBranch(CvtMBB);
1329     PredicateBlock(*CvtBBI, CvtMBB.end(), Cond);
1330 
1331     // Now merge the entry of the triangle with the true block.
1332     BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
1333     MergeBlocks(BBI, *CvtBBI, false);
1334   }
1335 
1336   // If 'true' block has a 'false' successor, add an exit branch to it.
1337   if (HasEarlyExit) {
1338     SmallVector<MachineOperand, 4> RevCond(CvtBBI->BrCond.begin(),
1339                                            CvtBBI->BrCond.end());
1340     if (TII->ReverseBranchCondition(RevCond))
1341       llvm_unreachable("Unable to reverse branch condition!");
1342 
1343     // Update the edge probability for both CvtBBI->FalseBB and NextBBI.
1344     // NewNext = New_Prob(BBI.BB, NextMBB) =
1345     //   Prob(BBI.BB, NextMBB) +
1346     //   Prob(BBI.BB, CvtMBB) * Prob(CvtMBB, NextMBB)
1347     // NewFalse = New_Prob(BBI.BB, CvtBBI->FalseBB) =
1348     //   Prob(BBI.BB, CvtMBB) * Prob(CvtMBB, CvtBBI->FalseBB)
1349     auto NewTrueBB = getNextBlock(*BBI.BB);
1350     auto NewNext = BBNext + BBCvt * CvtNext;
1351     auto NewTrueBBIter = find(BBI.BB->successors(), NewTrueBB);
1352     if (NewTrueBBIter != BBI.BB->succ_end())
1353       BBI.BB->setSuccProbability(NewTrueBBIter, NewNext);
1354 
1355     auto NewFalse = BBCvt * CvtFalse;
1356     TII->InsertBranch(*BBI.BB, CvtBBI->FalseBB, nullptr, RevCond, dl);
1357     BBI.BB->addSuccessor(CvtBBI->FalseBB, NewFalse);
1358   }
1359 
1360   // Merge in the 'false' block if the 'false' block has no other
1361   // predecessors. Otherwise, add an unconditional branch to 'false'.
1362   bool FalseBBDead = false;
1363   bool IterIfcvt = true;
1364   bool isFallThrough = canFallThroughTo(*BBI.BB, NextMBB);
1365   if (!isFallThrough) {
1366     // Only merge them if the true block does not fallthrough to the false
1367     // block. By not merging them, we make it possible to iteratively
1368     // ifcvt the blocks.
1369     if (!HasEarlyExit &&
1370         NextMBB.pred_size() == 1 && !NextBBI->HasFallThrough &&
1371         !NextMBB.hasAddressTaken()) {
1372       MergeBlocks(BBI, *NextBBI);
1373       FalseBBDead = true;
1374     } else {
1375       InsertUncondBranch(*BBI.BB, NextMBB, TII);
1376       BBI.HasFallThrough = false;
1377     }
1378     // Mixed predicated and unpredicated code. This cannot be iteratively
1379     // predicated.
1380     IterIfcvt = false;
1381   }
1382 
1383   RemoveExtraEdges(BBI);
1384 
1385   // Update block info. BB can be iteratively if-converted.
1386   if (!IterIfcvt)
1387     BBI.IsDone = true;
1388   InvalidatePreds(*BBI.BB);
1389   CvtBBI->IsDone = true;
1390   if (FalseBBDead)
1391     NextBBI->IsDone = true;
1392 
1393   // FIXME: Must maintain LiveIns.
1394   return true;
1395 }
1396 
1397 /// If convert a diamond sub-CFG.
1398 bool IfConverter::IfConvertDiamond(BBInfo &BBI, IfcvtKind Kind,
1399                                    unsigned NumDups1, unsigned NumDups2) {
1400   BBInfo &TrueBBI  = BBAnalysis[BBI.TrueBB->getNumber()];
1401   BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()];
1402   MachineBasicBlock *TailBB = TrueBBI.TrueBB;
1403   // True block must fall through or end with an unanalyzable terminator.
1404   if (!TailBB) {
1405     if (blockAlwaysFallThrough(TrueBBI))
1406       TailBB = FalseBBI.TrueBB;
1407     assert((TailBB || !TrueBBI.IsBrAnalyzable) && "Unexpected!");
1408   }
1409 
1410   if (TrueBBI.IsDone || FalseBBI.IsDone ||
1411       TrueBBI.BB->pred_size() > 1 ||
1412       FalseBBI.BB->pred_size() > 1) {
1413     // Something has changed. It's no longer safe to predicate these blocks.
1414     BBI.IsAnalyzed = false;
1415     TrueBBI.IsAnalyzed = false;
1416     FalseBBI.IsAnalyzed = false;
1417     return false;
1418   }
1419 
1420   if (TrueBBI.BB->hasAddressTaken() || FalseBBI.BB->hasAddressTaken())
1421     // Conservatively abort if-conversion if either BB has its address taken.
1422     return false;
1423 
1424   // Put the predicated instructions from the 'true' block before the
1425   // instructions from the 'false' block, unless the true block would clobber
1426   // the predicate, in which case, do the opposite.
1427   BBInfo *BBI1 = &TrueBBI;
1428   BBInfo *BBI2 = &FalseBBI;
1429   SmallVector<MachineOperand, 4> RevCond(BBI.BrCond.begin(), BBI.BrCond.end());
1430   if (TII->ReverseBranchCondition(RevCond))
1431     llvm_unreachable("Unable to reverse branch condition!");
1432   SmallVector<MachineOperand, 4> *Cond1 = &BBI.BrCond;
1433   SmallVector<MachineOperand, 4> *Cond2 = &RevCond;
1434 
1435   // Figure out the more profitable ordering.
1436   bool DoSwap = false;
1437   if (TrueBBI.ClobbersPred && !FalseBBI.ClobbersPred)
1438     DoSwap = true;
1439   else if (TrueBBI.ClobbersPred == FalseBBI.ClobbersPred) {
1440     if (TrueBBI.NonPredSize > FalseBBI.NonPredSize)
1441       DoSwap = true;
1442   }
1443   if (DoSwap) {
1444     std::swap(BBI1, BBI2);
1445     std::swap(Cond1, Cond2);
1446   }
1447 
1448   // Remove the conditional branch from entry to the blocks.
1449   BBI.NonPredSize -= TII->RemoveBranch(*BBI.BB);
1450 
1451   MachineBasicBlock &MBB1 = *BBI1->BB;
1452   MachineBasicBlock &MBB2 = *BBI2->BB;
1453 
1454   // Initialize the Redefs:
1455   // - BB2 live-in regs need implicit uses before being redefined by BB1
1456   //   instructions.
1457   // - BB1 live-out regs need implicit uses before being redefined by BB2
1458   //   instructions. We start with BB1 live-ins so we have the live-out regs
1459   //   after tracking the BB1 instructions.
1460   Redefs.init(TRI);
1461   Redefs.addLiveIns(MBB1);
1462   Redefs.addLiveIns(MBB2);
1463 
1464   // Remove the duplicated instructions at the beginnings of both paths.
1465   // Skip dbg_value instructions
1466   MachineBasicBlock::iterator DI1 = MBB1.getFirstNonDebugInstr();
1467   MachineBasicBlock::iterator DI2 = MBB2.getFirstNonDebugInstr();
1468   BBI1->NonPredSize -= NumDups1;
1469   BBI2->NonPredSize -= NumDups1;
1470 
1471   // Skip past the dups on each side separately since there may be
1472   // differing dbg_value entries.
1473   for (unsigned i = 0; i < NumDups1; ++DI1) {
1474     if (!DI1->isDebugValue())
1475       ++i;
1476   }
1477   while (NumDups1 != 0) {
1478     ++DI2;
1479     if (!DI2->isDebugValue())
1480       --NumDups1;
1481   }
1482 
1483   // Compute a set of registers which must not be killed by instructions in BB1:
1484   // This is everything used+live in BB2 after the duplicated instructions. We
1485   // can compute this set by simulating liveness backwards from the end of BB2.
1486   DontKill.init(TRI);
1487   for (const MachineInstr &MI :
1488        make_range(MBB2.rbegin(), MachineBasicBlock::reverse_iterator(DI2)))
1489     DontKill.stepBackward(MI);
1490 
1491   for (const MachineInstr &MI : make_range(MBB1.begin(), DI1)) {
1492     SmallVector<std::pair<unsigned, const MachineOperand*>, 4> IgnoredClobbers;
1493     Redefs.stepForward(MI, IgnoredClobbers);
1494   }
1495   BBI.BB->splice(BBI.BB->end(), &MBB1, MBB1.begin(), DI1);
1496   MBB2.erase(MBB2.begin(), DI2);
1497 
1498   // Remove branch from the 'true' block, unless it was not analyzable.
1499   // Non-analyzable branches need to be preserved, since in such cases,
1500   // the CFG structure is not an actual diamond (the join block may not
1501   // be present).
1502   if (BBI1->IsBrAnalyzable)
1503     BBI1->NonPredSize -= TII->RemoveBranch(MBB1);
1504   // Remove duplicated instructions.
1505   DI1 = MBB1.end();
1506   for (unsigned i = 0; i != NumDups2; ) {
1507     // NumDups2 only counted non-dbg_value instructions, so this won't
1508     // run off the head of the list.
1509     assert(DI1 != MBB1.begin());
1510     --DI1;
1511     // skip dbg_value instructions
1512     if (!DI1->isDebugValue())
1513       ++i;
1514   }
1515   MBB1.erase(DI1, MBB1.end());
1516 
1517   // Kill flags in the true block for registers living into the false block
1518   // must be removed.
1519   RemoveKills(MBB1.begin(), MBB1.end(), DontKill, *TRI);
1520 
1521   // Remove 'false' block branch (unless it was not analyzable), and find
1522   // the last instruction to predicate.
1523   if (BBI2->IsBrAnalyzable)
1524     BBI2->NonPredSize -= TII->RemoveBranch(MBB2);
1525   DI2 = MBB2.end();
1526   while (NumDups2 != 0) {
1527     // NumDups2 only counted non-dbg_value instructions, so this won't
1528     // run off the head of the list.
1529     assert(DI2 != MBB2.begin());
1530     --DI2;
1531     // skip dbg_value instructions
1532     if (!DI2->isDebugValue())
1533       --NumDups2;
1534   }
1535 
1536   // Remember which registers would later be defined by the false block.
1537   // This allows us not to predicate instructions in the true block that would
1538   // later be re-defined. That is, rather than
1539   //   subeq  r0, r1, #1
1540   //   addne  r0, r1, #1
1541   // generate:
1542   //   sub    r0, r1, #1
1543   //   addne  r0, r1, #1
1544   SmallSet<unsigned, 4> RedefsByFalse;
1545   SmallSet<unsigned, 4> ExtUses;
1546   if (TII->isProfitableToUnpredicate(MBB1, MBB2)) {
1547     for (const MachineInstr &FI : make_range(MBB2.begin(), DI2)) {
1548       if (FI.isDebugValue())
1549         continue;
1550       SmallVector<unsigned, 4> Defs;
1551       for (const MachineOperand &MO : FI.operands()) {
1552         if (!MO.isReg())
1553           continue;
1554         unsigned Reg = MO.getReg();
1555         if (!Reg)
1556           continue;
1557         if (MO.isDef()) {
1558           Defs.push_back(Reg);
1559         } else if (!RedefsByFalse.count(Reg)) {
1560           // These are defined before ctrl flow reach the 'false' instructions.
1561           // They cannot be modified by the 'true' instructions.
1562           for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
1563                SubRegs.isValid(); ++SubRegs)
1564             ExtUses.insert(*SubRegs);
1565         }
1566       }
1567 
1568       for (unsigned Reg : Defs) {
1569         if (!ExtUses.count(Reg)) {
1570           for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true);
1571                SubRegs.isValid(); ++SubRegs)
1572             RedefsByFalse.insert(*SubRegs);
1573         }
1574       }
1575     }
1576   }
1577 
1578   // Predicate the 'true' block.
1579   PredicateBlock(*BBI1, MBB1.end(), *Cond1, &RedefsByFalse);
1580 
1581   // After predicating BBI1, if there is a predicated terminator in BBI1 and
1582   // a non-predicated in BBI2, then we don't want to predicate the one from
1583   // BBI2. The reason is that if we merged these blocks, we would end up with
1584   // two predicated terminators in the same block.
1585   if (!MBB2.empty() && (DI2 == MBB2.end())) {
1586     MachineBasicBlock::iterator BBI1T = MBB1.getFirstTerminator();
1587     MachineBasicBlock::iterator BBI2T = MBB2.getFirstTerminator();
1588     if (BBI1T != MBB1.end() && TII->isPredicated(*BBI1T) &&
1589         BBI2T != MBB2.end() && !TII->isPredicated(*BBI2T))
1590       --DI2;
1591   }
1592 
1593   // Predicate the 'false' block.
1594   PredicateBlock(*BBI2, DI2, *Cond2);
1595 
1596   // Merge the true block into the entry of the diamond.
1597   MergeBlocks(BBI, *BBI1, TailBB == nullptr);
1598   MergeBlocks(BBI, *BBI2, TailBB == nullptr);
1599 
1600   // If the if-converted block falls through or unconditionally branches into
1601   // the tail block, and the tail block does not have other predecessors, then
1602   // fold the tail block in as well. Otherwise, unless it falls through to the
1603   // tail, add a unconditional branch to it.
1604   if (TailBB) {
1605     BBInfo &TailBBI = BBAnalysis[TailBB->getNumber()];
1606     bool CanMergeTail = !TailBBI.HasFallThrough &&
1607       !TailBBI.BB->hasAddressTaken();
1608     // The if-converted block can still have a predicated terminator
1609     // (e.g. a predicated return). If that is the case, we cannot merge
1610     // it with the tail block.
1611     MachineBasicBlock::const_iterator TI = BBI.BB->getFirstTerminator();
1612     if (TI != BBI.BB->end() && TII->isPredicated(*TI))
1613       CanMergeTail = false;
1614     // There may still be a fall-through edge from BBI1 or BBI2 to TailBB;
1615     // check if there are any other predecessors besides those.
1616     unsigned NumPreds = TailBB->pred_size();
1617     if (NumPreds > 1)
1618       CanMergeTail = false;
1619     else if (NumPreds == 1 && CanMergeTail) {
1620       MachineBasicBlock::pred_iterator PI = TailBB->pred_begin();
1621       if (*PI != &MBB1 && *PI != &MBB2)
1622         CanMergeTail = false;
1623     }
1624     if (CanMergeTail) {
1625       MergeBlocks(BBI, TailBBI);
1626       TailBBI.IsDone = true;
1627     } else {
1628       BBI.BB->addSuccessor(TailBB, BranchProbability::getOne());
1629       InsertUncondBranch(*BBI.BB, *TailBB, TII);
1630       BBI.HasFallThrough = false;
1631     }
1632   }
1633 
1634   // RemoveExtraEdges won't work if the block has an unanalyzable branch,
1635   // which can happen here if TailBB is unanalyzable and is merged, so
1636   // explicitly remove BBI1 and BBI2 as successors.
1637   BBI.BB->removeSuccessor(&MBB1);
1638   BBI.BB->removeSuccessor(&MBB2, true);
1639   RemoveExtraEdges(BBI);
1640 
1641   // Update block info.
1642   BBI.IsDone = TrueBBI.IsDone = FalseBBI.IsDone = true;
1643   InvalidatePreds(*BBI.BB);
1644 
1645   // FIXME: Must maintain LiveIns.
1646   return true;
1647 }
1648 
1649 static bool MaySpeculate(const MachineInstr &MI,
1650                          SmallSet<unsigned, 4> &LaterRedefs) {
1651   bool SawStore = true;
1652   if (!MI.isSafeToMove(nullptr, SawStore))
1653     return false;
1654 
1655   for (const MachineOperand &MO : MI.operands()) {
1656     if (!MO.isReg())
1657       continue;
1658     unsigned Reg = MO.getReg();
1659     if (!Reg)
1660       continue;
1661     if (MO.isDef() && !LaterRedefs.count(Reg))
1662       return false;
1663   }
1664 
1665   return true;
1666 }
1667 
1668 /// Predicate instructions from the start of the block to the specified end with
1669 /// the specified condition.
1670 void IfConverter::PredicateBlock(BBInfo &BBI,
1671                                  MachineBasicBlock::iterator E,
1672                                  SmallVectorImpl<MachineOperand> &Cond,
1673                                  SmallSet<unsigned, 4> *LaterRedefs) {
1674   bool AnyUnpred = false;
1675   bool MaySpec = LaterRedefs != nullptr;
1676   for (MachineInstr &I : make_range(BBI.BB->begin(), E)) {
1677     if (I.isDebugValue() || TII->isPredicated(I))
1678       continue;
1679     // It may be possible not to predicate an instruction if it's the 'true'
1680     // side of a diamond and the 'false' side may re-define the instruction's
1681     // defs.
1682     if (MaySpec && MaySpeculate(I, *LaterRedefs)) {
1683       AnyUnpred = true;
1684       continue;
1685     }
1686     // If any instruction is predicated, then every instruction after it must
1687     // be predicated.
1688     MaySpec = false;
1689     if (!TII->PredicateInstruction(I, Cond)) {
1690 #ifndef NDEBUG
1691       dbgs() << "Unable to predicate " << I << "!\n";
1692 #endif
1693       llvm_unreachable(nullptr);
1694     }
1695 
1696     // If the predicated instruction now redefines a register as the result of
1697     // if-conversion, add an implicit kill.
1698     UpdatePredRedefs(I, Redefs);
1699   }
1700 
1701   BBI.Predicate.append(Cond.begin(), Cond.end());
1702 
1703   BBI.IsAnalyzed = false;
1704   BBI.NonPredSize = 0;
1705 
1706   ++NumIfConvBBs;
1707   if (AnyUnpred)
1708     ++NumUnpred;
1709 }
1710 
1711 /// Copy and predicate instructions from source BB to the destination block.
1712 /// Skip end of block branches if IgnoreBr is true.
1713 void IfConverter::CopyAndPredicateBlock(BBInfo &ToBBI, BBInfo &FromBBI,
1714                                         SmallVectorImpl<MachineOperand> &Cond,
1715                                         bool IgnoreBr) {
1716   MachineFunction &MF = *ToBBI.BB->getParent();
1717 
1718   MachineBasicBlock &FromMBB = *FromBBI.BB;
1719   for (MachineInstr &I : FromMBB) {
1720     // Do not copy the end of the block branches.
1721     if (IgnoreBr && I.isBranch())
1722       break;
1723 
1724     MachineInstr *MI = MF.CloneMachineInstr(&I);
1725     ToBBI.BB->insert(ToBBI.BB->end(), MI);
1726     ToBBI.NonPredSize++;
1727     unsigned ExtraPredCost = TII->getPredicationCost(I);
1728     unsigned NumCycles = SchedModel.computeInstrLatency(&I, false);
1729     if (NumCycles > 1)
1730       ToBBI.ExtraCost += NumCycles-1;
1731     ToBBI.ExtraCost2 += ExtraPredCost;
1732 
1733     if (!TII->isPredicated(I) && !MI->isDebugValue()) {
1734       if (!TII->PredicateInstruction(*MI, Cond)) {
1735 #ifndef NDEBUG
1736         dbgs() << "Unable to predicate " << I << "!\n";
1737 #endif
1738         llvm_unreachable(nullptr);
1739       }
1740     }
1741 
1742     // If the predicated instruction now redefines a register as the result of
1743     // if-conversion, add an implicit kill.
1744     UpdatePredRedefs(*MI, Redefs);
1745 
1746     // Some kill flags may not be correct anymore.
1747     if (!DontKill.empty())
1748       RemoveKills(*MI, DontKill);
1749   }
1750 
1751   if (!IgnoreBr) {
1752     std::vector<MachineBasicBlock *> Succs(FromMBB.succ_begin(),
1753                                            FromMBB.succ_end());
1754     MachineBasicBlock *NBB = getNextBlock(FromMBB);
1755     MachineBasicBlock *FallThrough = FromBBI.HasFallThrough ? NBB : nullptr;
1756 
1757     for (MachineBasicBlock *Succ : Succs) {
1758       // Fallthrough edge can't be transferred.
1759       if (Succ == FallThrough)
1760         continue;
1761       ToBBI.BB->addSuccessor(Succ);
1762     }
1763   }
1764 
1765   ToBBI.Predicate.append(FromBBI.Predicate.begin(), FromBBI.Predicate.end());
1766   ToBBI.Predicate.append(Cond.begin(), Cond.end());
1767 
1768   ToBBI.ClobbersPred |= FromBBI.ClobbersPred;
1769   ToBBI.IsAnalyzed = false;
1770 
1771   ++NumDupBBs;
1772 }
1773 
1774 /// Move all instructions from FromBB to the end of ToBB.  This will leave
1775 /// FromBB as an empty block, so remove all of its successor edges except for
1776 /// the fall-through edge.  If AddEdges is true, i.e., when FromBBI's branch is
1777 /// being moved, add those successor edges to ToBBI.
1778 void IfConverter::MergeBlocks(BBInfo &ToBBI, BBInfo &FromBBI, bool AddEdges) {
1779   MachineBasicBlock &FromMBB = *FromBBI.BB;
1780   assert(!FromMBB.hasAddressTaken() &&
1781          "Removing a BB whose address is taken!");
1782 
1783   // In case FromMBB contains terminators (e.g. return instruction),
1784   // first move the non-terminator instructions, then the terminators.
1785   MachineBasicBlock::iterator FromTI = FromMBB.getFirstTerminator();
1786   MachineBasicBlock::iterator ToTI = ToBBI.BB->getFirstTerminator();
1787   ToBBI.BB->splice(ToTI, &FromMBB, FromMBB.begin(), FromTI);
1788 
1789   // If FromBB has non-predicated terminator we should copy it at the end.
1790   if (FromTI != FromMBB.end() && !TII->isPredicated(*FromTI))
1791     ToTI = ToBBI.BB->end();
1792   ToBBI.BB->splice(ToTI, &FromMBB, FromTI, FromMBB.end());
1793 
1794   // Force normalizing the successors' probabilities of ToBBI.BB to convert all
1795   // unknown probabilities into known ones.
1796   // FIXME: This usage is too tricky and in the future we would like to
1797   // eliminate all unknown probabilities in MBB.
1798   ToBBI.BB->normalizeSuccProbs();
1799 
1800   SmallVector<MachineBasicBlock *, 4> FromSuccs(FromMBB.succ_begin(),
1801                                                 FromMBB.succ_end());
1802   MachineBasicBlock *NBB = getNextBlock(FromMBB);
1803   MachineBasicBlock *FallThrough = FromBBI.HasFallThrough ? NBB : nullptr;
1804   // The edge probability from ToBBI.BB to FromMBB, which is only needed when
1805   // AddEdges is true and FromMBB is a successor of ToBBI.BB.
1806   auto To2FromProb = BranchProbability::getZero();
1807   if (AddEdges && ToBBI.BB->isSuccessor(&FromMBB)) {
1808     To2FromProb = MBPI->getEdgeProbability(ToBBI.BB, &FromMBB);
1809     // Set the edge probability from ToBBI.BB to FromMBB to zero to avoid the
1810     // edge probability being merged to other edges when this edge is removed
1811     // later.
1812     ToBBI.BB->setSuccProbability(find(ToBBI.BB->successors(), &FromMBB),
1813                                  BranchProbability::getZero());
1814   }
1815 
1816   for (MachineBasicBlock *Succ : FromSuccs) {
1817     // Fallthrough edge can't be transferred.
1818     if (Succ == FallThrough)
1819       continue;
1820 
1821     auto NewProb = BranchProbability::getZero();
1822     if (AddEdges) {
1823       // Calculate the edge probability for the edge from ToBBI.BB to Succ,
1824       // which is a portion of the edge probability from FromMBB to Succ. The
1825       // portion ratio is the edge probability from ToBBI.BB to FromMBB (if
1826       // FromBBI is a successor of ToBBI.BB. See comment below for excepion).
1827       NewProb = MBPI->getEdgeProbability(&FromMBB, Succ);
1828 
1829       // To2FromProb is 0 when FromMBB is not a successor of ToBBI.BB. This
1830       // only happens when if-converting a diamond CFG and FromMBB is the
1831       // tail BB.  In this case FromMBB post-dominates ToBBI.BB and hence we
1832       // could just use the probabilities on FromMBB's out-edges when adding
1833       // new successors.
1834       if (!To2FromProb.isZero())
1835         NewProb *= To2FromProb;
1836     }
1837 
1838     FromMBB.removeSuccessor(Succ);
1839 
1840     if (AddEdges) {
1841       // If the edge from ToBBI.BB to Succ already exists, update the
1842       // probability of this edge by adding NewProb to it. An example is shown
1843       // below, in which A is ToBBI.BB and B is FromMBB. In this case we
1844       // don't have to set C as A's successor as it already is. We only need to
1845       // update the edge probability on A->C. Note that B will not be
1846       // immediately removed from A's successors. It is possible that B->D is
1847       // not removed either if D is a fallthrough of B. Later the edge A->D
1848       // (generated here) and B->D will be combined into one edge. To maintain
1849       // correct edge probability of this combined edge, we need to set the edge
1850       // probability of A->B to zero, which is already done above. The edge
1851       // probability on A->D is calculated by scaling the original probability
1852       // on A->B by the probability of B->D.
1853       //
1854       // Before ifcvt:      After ifcvt (assume B->D is kept):
1855       //
1856       //       A                A
1857       //      /|               /|\
1858       //     / B              / B|
1859       //    | /|             |  ||
1860       //    |/ |             |  |/
1861       //    C  D             C  D
1862       //
1863       if (ToBBI.BB->isSuccessor(Succ))
1864         ToBBI.BB->setSuccProbability(
1865             find(ToBBI.BB->successors(), Succ),
1866             MBPI->getEdgeProbability(ToBBI.BB, Succ) + NewProb);
1867       else
1868         ToBBI.BB->addSuccessor(Succ, NewProb);
1869     }
1870   }
1871 
1872   // Now FromBBI always falls through to the next block!
1873   if (NBB && !FromMBB.isSuccessor(NBB))
1874     FromMBB.addSuccessor(NBB);
1875 
1876   // Normalize the probabilities of ToBBI.BB's successors with all adjustment
1877   // we've done above.
1878   ToBBI.BB->normalizeSuccProbs();
1879 
1880   ToBBI.Predicate.append(FromBBI.Predicate.begin(), FromBBI.Predicate.end());
1881   FromBBI.Predicate.clear();
1882 
1883   ToBBI.NonPredSize += FromBBI.NonPredSize;
1884   ToBBI.ExtraCost += FromBBI.ExtraCost;
1885   ToBBI.ExtraCost2 += FromBBI.ExtraCost2;
1886   FromBBI.NonPredSize = 0;
1887   FromBBI.ExtraCost = 0;
1888   FromBBI.ExtraCost2 = 0;
1889 
1890   ToBBI.ClobbersPred |= FromBBI.ClobbersPred;
1891   ToBBI.HasFallThrough = FromBBI.HasFallThrough;
1892   ToBBI.IsAnalyzed = false;
1893   FromBBI.IsAnalyzed = false;
1894 }
1895 
1896 FunctionPass *
1897 llvm::createIfConverter(std::function<bool(const Function &)> Ftor) {
1898   return new IfConverter(std::move(Ftor));
1899 }
1900