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