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