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 2888bcb0991SDimitry Andric bool MeetIfcvtSizeLimit(BBInfo &TBBInfo, BBInfo &FBBInfo, 2898bcb0991SDimitry Andric MachineBasicBlock &CommBB, unsigned Dups, 2908bcb0991SDimitry Andric BranchProbability Prediction, bool Forked) const { 2918bcb0991SDimitry Andric const MachineFunction &MF = *TBBInfo.BB->getParent(); 2928bcb0991SDimitry Andric if (MF.getFunction().hasMinSize()) { 2938bcb0991SDimitry Andric MachineBasicBlock::iterator TIB = TBBInfo.BB->begin(); 2948bcb0991SDimitry Andric MachineBasicBlock::iterator FIB = FBBInfo.BB->begin(); 2958bcb0991SDimitry Andric MachineBasicBlock::iterator TIE = TBBInfo.BB->end(); 2968bcb0991SDimitry Andric MachineBasicBlock::iterator FIE = FBBInfo.BB->end(); 2978bcb0991SDimitry Andric 2988bcb0991SDimitry Andric unsigned Dups1, Dups2; 2998bcb0991SDimitry Andric if (!CountDuplicatedInstructions(TIB, FIB, TIE, FIE, Dups1, Dups2, 3008bcb0991SDimitry Andric *TBBInfo.BB, *FBBInfo.BB, 3018bcb0991SDimitry Andric /*SkipUnconditionalBranches*/ true)) 3028bcb0991SDimitry Andric llvm_unreachable("should already have been checked by ValidDiamond"); 3038bcb0991SDimitry Andric 3048bcb0991SDimitry Andric unsigned BranchBytes = 0; 3058bcb0991SDimitry Andric unsigned CommonBytes = 0; 3068bcb0991SDimitry Andric 3078bcb0991SDimitry Andric // Count common instructions at the start of the true and false blocks. 3088bcb0991SDimitry Andric for (auto &I : make_range(TBBInfo.BB->begin(), TIB)) { 3098bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Common inst: " << I); 3108bcb0991SDimitry Andric CommonBytes += TII->getInstSizeInBytes(I); 3118bcb0991SDimitry Andric } 3128bcb0991SDimitry Andric for (auto &I : make_range(FBBInfo.BB->begin(), FIB)) { 3138bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Common inst: " << I); 3148bcb0991SDimitry Andric CommonBytes += TII->getInstSizeInBytes(I); 3158bcb0991SDimitry Andric } 3168bcb0991SDimitry Andric 3178bcb0991SDimitry Andric // Count instructions at the end of the true and false blocks, after 3188bcb0991SDimitry Andric // the ones we plan to predicate. Analyzable branches will be removed 3198bcb0991SDimitry Andric // (unless this is a forked diamond), and all other instructions are 3208bcb0991SDimitry Andric // common between the two blocks. 3218bcb0991SDimitry Andric for (auto &I : make_range(TIE, TBBInfo.BB->end())) { 3228bcb0991SDimitry Andric if (I.isBranch() && TBBInfo.IsBrAnalyzable && !Forked) { 3238bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Saving branch: " << I); 3248bcb0991SDimitry Andric BranchBytes += TII->predictBranchSizeForIfCvt(I); 3258bcb0991SDimitry Andric } else { 3268bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Common inst: " << I); 3278bcb0991SDimitry Andric CommonBytes += TII->getInstSizeInBytes(I); 3288bcb0991SDimitry Andric } 3298bcb0991SDimitry Andric } 3308bcb0991SDimitry Andric for (auto &I : make_range(FIE, FBBInfo.BB->end())) { 3318bcb0991SDimitry Andric if (I.isBranch() && FBBInfo.IsBrAnalyzable && !Forked) { 3328bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Saving branch: " << I); 3338bcb0991SDimitry Andric BranchBytes += TII->predictBranchSizeForIfCvt(I); 3348bcb0991SDimitry Andric } else { 3358bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Common inst: " << I); 3368bcb0991SDimitry Andric CommonBytes += TII->getInstSizeInBytes(I); 3378bcb0991SDimitry Andric } 3388bcb0991SDimitry Andric } 3398bcb0991SDimitry Andric for (auto &I : CommBB.terminators()) { 3408bcb0991SDimitry Andric if (I.isBranch()) { 3418bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Saving branch: " << I); 3428bcb0991SDimitry Andric BranchBytes += TII->predictBranchSizeForIfCvt(I); 3438bcb0991SDimitry Andric } 3448bcb0991SDimitry Andric } 3458bcb0991SDimitry Andric 3468bcb0991SDimitry Andric // The common instructions in one branch will be eliminated, halving 3478bcb0991SDimitry Andric // their code size. 3488bcb0991SDimitry Andric CommonBytes /= 2; 3498bcb0991SDimitry Andric 3508bcb0991SDimitry Andric // Count the instructions which we need to predicate. 3518bcb0991SDimitry Andric unsigned NumPredicatedInstructions = 0; 3528bcb0991SDimitry Andric for (auto &I : make_range(TIB, TIE)) { 3538bcb0991SDimitry Andric if (!I.isDebugInstr()) { 3548bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Predicating: " << I); 3558bcb0991SDimitry Andric NumPredicatedInstructions++; 3568bcb0991SDimitry Andric } 3578bcb0991SDimitry Andric } 3588bcb0991SDimitry Andric for (auto &I : make_range(FIB, FIE)) { 3598bcb0991SDimitry Andric if (!I.isDebugInstr()) { 3608bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "Predicating: " << I); 3618bcb0991SDimitry Andric NumPredicatedInstructions++; 3628bcb0991SDimitry Andric } 3638bcb0991SDimitry Andric } 3648bcb0991SDimitry Andric 3658bcb0991SDimitry Andric // Even though we're optimising for size at the expense of performance, 3668bcb0991SDimitry Andric // avoid creating really long predicated blocks. 3678bcb0991SDimitry Andric if (NumPredicatedInstructions > 15) 3688bcb0991SDimitry Andric return false; 3698bcb0991SDimitry Andric 3708bcb0991SDimitry Andric // Some targets (e.g. Thumb2) need to insert extra instructions to 3718bcb0991SDimitry Andric // start predicated blocks. 3728bcb0991SDimitry Andric unsigned ExtraPredicateBytes = TII->extraSizeToPredicateInstructions( 3738bcb0991SDimitry Andric MF, NumPredicatedInstructions); 3748bcb0991SDimitry Andric 3758bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "MeetIfcvtSizeLimit(BranchBytes=" << BranchBytes 3768bcb0991SDimitry Andric << ", CommonBytes=" << CommonBytes 3778bcb0991SDimitry Andric << ", NumPredicatedInstructions=" 3788bcb0991SDimitry Andric << NumPredicatedInstructions 3798bcb0991SDimitry Andric << ", ExtraPredicateBytes=" << ExtraPredicateBytes 3808bcb0991SDimitry Andric << ")\n"); 3818bcb0991SDimitry Andric return (BranchBytes + CommonBytes) > ExtraPredicateBytes; 3828bcb0991SDimitry Andric } else { 3838bcb0991SDimitry Andric unsigned TCycle = TBBInfo.NonPredSize + TBBInfo.ExtraCost - Dups; 3848bcb0991SDimitry Andric unsigned FCycle = FBBInfo.NonPredSize + FBBInfo.ExtraCost - Dups; 3858bcb0991SDimitry Andric bool Res = TCycle > 0 && FCycle > 0 && 3868bcb0991SDimitry Andric TII->isProfitableToIfCvt( 3878bcb0991SDimitry Andric *TBBInfo.BB, TCycle, TBBInfo.ExtraCost2, *FBBInfo.BB, 3888bcb0991SDimitry Andric FCycle, FBBInfo.ExtraCost2, Prediction); 3898bcb0991SDimitry Andric LLVM_DEBUG(dbgs() << "MeetIfcvtSizeLimit(TCycle=" << TCycle 3908bcb0991SDimitry Andric << ", FCycle=" << FCycle 3918bcb0991SDimitry Andric << ", TExtra=" << TBBInfo.ExtraCost2 << ", FExtra=" 3928bcb0991SDimitry Andric << FBBInfo.ExtraCost2 << ") = " << Res << "\n"); 3938bcb0991SDimitry Andric return Res; 3948bcb0991SDimitry Andric } 395*0b57cec5SDimitry Andric } 396*0b57cec5SDimitry Andric 397*0b57cec5SDimitry Andric /// Returns true if Block ends without a terminator. 398*0b57cec5SDimitry Andric bool blockAlwaysFallThrough(BBInfo &BBI) const { 399*0b57cec5SDimitry Andric return BBI.IsBrAnalyzable && BBI.TrueBB == nullptr; 400*0b57cec5SDimitry Andric } 401*0b57cec5SDimitry Andric 402*0b57cec5SDimitry Andric /// Used to sort if-conversion candidates. 403*0b57cec5SDimitry Andric static bool IfcvtTokenCmp(const std::unique_ptr<IfcvtToken> &C1, 404*0b57cec5SDimitry Andric const std::unique_ptr<IfcvtToken> &C2) { 405*0b57cec5SDimitry Andric int Incr1 = (C1->Kind == ICDiamond) 406*0b57cec5SDimitry Andric ? -(int)(C1->NumDups + C1->NumDups2) : (int)C1->NumDups; 407*0b57cec5SDimitry Andric int Incr2 = (C2->Kind == ICDiamond) 408*0b57cec5SDimitry Andric ? -(int)(C2->NumDups + C2->NumDups2) : (int)C2->NumDups; 409*0b57cec5SDimitry Andric if (Incr1 > Incr2) 410*0b57cec5SDimitry Andric return true; 411*0b57cec5SDimitry Andric else if (Incr1 == Incr2) { 412*0b57cec5SDimitry Andric // Favors subsumption. 413*0b57cec5SDimitry Andric if (!C1->NeedSubsumption && C2->NeedSubsumption) 414*0b57cec5SDimitry Andric return true; 415*0b57cec5SDimitry Andric else if (C1->NeedSubsumption == C2->NeedSubsumption) { 416*0b57cec5SDimitry Andric // Favors diamond over triangle, etc. 417*0b57cec5SDimitry Andric if ((unsigned)C1->Kind < (unsigned)C2->Kind) 418*0b57cec5SDimitry Andric return true; 419*0b57cec5SDimitry Andric else if (C1->Kind == C2->Kind) 420*0b57cec5SDimitry Andric return C1->BBI.BB->getNumber() < C2->BBI.BB->getNumber(); 421*0b57cec5SDimitry Andric } 422*0b57cec5SDimitry Andric } 423*0b57cec5SDimitry Andric return false; 424*0b57cec5SDimitry Andric } 425*0b57cec5SDimitry Andric }; 426*0b57cec5SDimitry Andric 427*0b57cec5SDimitry Andric } // end anonymous namespace 428*0b57cec5SDimitry Andric 429*0b57cec5SDimitry Andric char IfConverter::ID = 0; 430*0b57cec5SDimitry Andric 431*0b57cec5SDimitry Andric char &llvm::IfConverterID = IfConverter::ID; 432*0b57cec5SDimitry Andric 433*0b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(IfConverter, DEBUG_TYPE, "If Converter", false, false) 434*0b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(MachineBranchProbabilityInfo) 435*0b57cec5SDimitry Andric INITIALIZE_PASS_END(IfConverter, DEBUG_TYPE, "If Converter", false, false) 436*0b57cec5SDimitry Andric 437*0b57cec5SDimitry Andric bool IfConverter::runOnMachineFunction(MachineFunction &MF) { 438*0b57cec5SDimitry Andric if (skipFunction(MF.getFunction()) || (PredicateFtor && !PredicateFtor(MF))) 439*0b57cec5SDimitry Andric return false; 440*0b57cec5SDimitry Andric 441*0b57cec5SDimitry Andric const TargetSubtargetInfo &ST = MF.getSubtarget(); 442*0b57cec5SDimitry Andric TLI = ST.getTargetLowering(); 443*0b57cec5SDimitry Andric TII = ST.getInstrInfo(); 444*0b57cec5SDimitry Andric TRI = ST.getRegisterInfo(); 445*0b57cec5SDimitry Andric BranchFolder::MBFIWrapper MBFI(getAnalysis<MachineBlockFrequencyInfo>()); 446*0b57cec5SDimitry Andric MBPI = &getAnalysis<MachineBranchProbabilityInfo>(); 447*0b57cec5SDimitry Andric MRI = &MF.getRegInfo(); 448*0b57cec5SDimitry Andric SchedModel.init(&ST); 449*0b57cec5SDimitry Andric 450*0b57cec5SDimitry Andric if (!TII) return false; 451*0b57cec5SDimitry Andric 452*0b57cec5SDimitry Andric PreRegAlloc = MRI->isSSA(); 453*0b57cec5SDimitry Andric 454*0b57cec5SDimitry Andric bool BFChange = false; 455*0b57cec5SDimitry Andric if (!PreRegAlloc) { 456*0b57cec5SDimitry Andric // Tail merge tend to expose more if-conversion opportunities. 457*0b57cec5SDimitry Andric BranchFolder BF(true, false, MBFI, *MBPI); 4588bcb0991SDimitry Andric auto *MMIWP = getAnalysisIfAvailable<MachineModuleInfoWrapperPass>(); 4598bcb0991SDimitry Andric BFChange = BF.OptimizeFunction( 4608bcb0991SDimitry Andric MF, TII, ST.getRegisterInfo(), 4618bcb0991SDimitry Andric MMIWP ? &MMIWP->getMMI() : nullptr); 462*0b57cec5SDimitry Andric } 463*0b57cec5SDimitry Andric 464*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\nIfcvt: function (" << ++FnNum << ") \'" 465*0b57cec5SDimitry Andric << MF.getName() << "\'"); 466*0b57cec5SDimitry Andric 467*0b57cec5SDimitry Andric if (FnNum < IfCvtFnStart || (IfCvtFnStop != -1 && FnNum > IfCvtFnStop)) { 468*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " skipped\n"); 469*0b57cec5SDimitry Andric return false; 470*0b57cec5SDimitry Andric } 471*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "\n"); 472*0b57cec5SDimitry Andric 473*0b57cec5SDimitry Andric MF.RenumberBlocks(); 474*0b57cec5SDimitry Andric BBAnalysis.resize(MF.getNumBlockIDs()); 475*0b57cec5SDimitry Andric 476*0b57cec5SDimitry Andric std::vector<std::unique_ptr<IfcvtToken>> Tokens; 477*0b57cec5SDimitry Andric MadeChange = false; 478*0b57cec5SDimitry Andric unsigned NumIfCvts = NumSimple + NumSimpleFalse + NumTriangle + 479*0b57cec5SDimitry Andric NumTriangleRev + NumTriangleFalse + NumTriangleFRev + NumDiamonds; 480*0b57cec5SDimitry Andric while (IfCvtLimit == -1 || (int)NumIfCvts < IfCvtLimit) { 481*0b57cec5SDimitry Andric // Do an initial analysis for each basic block and find all the potential 482*0b57cec5SDimitry Andric // candidates to perform if-conversion. 483*0b57cec5SDimitry Andric bool Change = false; 484*0b57cec5SDimitry Andric AnalyzeBlocks(MF, Tokens); 485*0b57cec5SDimitry Andric while (!Tokens.empty()) { 486*0b57cec5SDimitry Andric std::unique_ptr<IfcvtToken> Token = std::move(Tokens.back()); 487*0b57cec5SDimitry Andric Tokens.pop_back(); 488*0b57cec5SDimitry Andric BBInfo &BBI = Token->BBI; 489*0b57cec5SDimitry Andric IfcvtKind Kind = Token->Kind; 490*0b57cec5SDimitry Andric unsigned NumDups = Token->NumDups; 491*0b57cec5SDimitry Andric unsigned NumDups2 = Token->NumDups2; 492*0b57cec5SDimitry Andric 493*0b57cec5SDimitry Andric // If the block has been evicted out of the queue or it has already been 494*0b57cec5SDimitry Andric // marked dead (due to it being predicated), then skip it. 495*0b57cec5SDimitry Andric if (BBI.IsDone) 496*0b57cec5SDimitry Andric BBI.IsEnqueued = false; 497*0b57cec5SDimitry Andric if (!BBI.IsEnqueued) 498*0b57cec5SDimitry Andric continue; 499*0b57cec5SDimitry Andric 500*0b57cec5SDimitry Andric BBI.IsEnqueued = false; 501*0b57cec5SDimitry Andric 502*0b57cec5SDimitry Andric bool RetVal = false; 503*0b57cec5SDimitry Andric switch (Kind) { 504*0b57cec5SDimitry Andric default: llvm_unreachable("Unexpected!"); 505*0b57cec5SDimitry Andric case ICSimple: 506*0b57cec5SDimitry Andric case ICSimpleFalse: { 507*0b57cec5SDimitry Andric bool isFalse = Kind == ICSimpleFalse; 508*0b57cec5SDimitry Andric if ((isFalse && DisableSimpleF) || (!isFalse && DisableSimple)) break; 509*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Ifcvt (Simple" 510*0b57cec5SDimitry Andric << (Kind == ICSimpleFalse ? " false" : "") 511*0b57cec5SDimitry Andric << "): " << printMBBReference(*BBI.BB) << " (" 512*0b57cec5SDimitry Andric << ((Kind == ICSimpleFalse) ? BBI.FalseBB->getNumber() 513*0b57cec5SDimitry Andric : BBI.TrueBB->getNumber()) 514*0b57cec5SDimitry Andric << ") "); 515*0b57cec5SDimitry Andric RetVal = IfConvertSimple(BBI, Kind); 516*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n"); 517*0b57cec5SDimitry Andric if (RetVal) { 518*0b57cec5SDimitry Andric if (isFalse) ++NumSimpleFalse; 519*0b57cec5SDimitry Andric else ++NumSimple; 520*0b57cec5SDimitry Andric } 521*0b57cec5SDimitry Andric break; 522*0b57cec5SDimitry Andric } 523*0b57cec5SDimitry Andric case ICTriangle: 524*0b57cec5SDimitry Andric case ICTriangleRev: 525*0b57cec5SDimitry Andric case ICTriangleFalse: 526*0b57cec5SDimitry Andric case ICTriangleFRev: { 527*0b57cec5SDimitry Andric bool isFalse = Kind == ICTriangleFalse; 528*0b57cec5SDimitry Andric bool isRev = (Kind == ICTriangleRev || Kind == ICTriangleFRev); 529*0b57cec5SDimitry Andric if (DisableTriangle && !isFalse && !isRev) break; 530*0b57cec5SDimitry Andric if (DisableTriangleR && !isFalse && isRev) break; 531*0b57cec5SDimitry Andric if (DisableTriangleF && isFalse && !isRev) break; 532*0b57cec5SDimitry Andric if (DisableTriangleFR && isFalse && isRev) break; 533*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Ifcvt (Triangle"); 534*0b57cec5SDimitry Andric if (isFalse) 535*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " false"); 536*0b57cec5SDimitry Andric if (isRev) 537*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " rev"); 538*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "): " << printMBBReference(*BBI.BB) 539*0b57cec5SDimitry Andric << " (T:" << BBI.TrueBB->getNumber() 540*0b57cec5SDimitry Andric << ",F:" << BBI.FalseBB->getNumber() << ") "); 541*0b57cec5SDimitry Andric RetVal = IfConvertTriangle(BBI, Kind); 542*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n"); 543*0b57cec5SDimitry Andric if (RetVal) { 544*0b57cec5SDimitry Andric if (isFalse) { 545*0b57cec5SDimitry Andric if (isRev) ++NumTriangleFRev; 546*0b57cec5SDimitry Andric else ++NumTriangleFalse; 547*0b57cec5SDimitry Andric } else { 548*0b57cec5SDimitry Andric if (isRev) ++NumTriangleRev; 549*0b57cec5SDimitry Andric else ++NumTriangle; 550*0b57cec5SDimitry Andric } 551*0b57cec5SDimitry Andric } 552*0b57cec5SDimitry Andric break; 553*0b57cec5SDimitry Andric } 554*0b57cec5SDimitry Andric case ICDiamond: 555*0b57cec5SDimitry Andric if (DisableDiamond) break; 556*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Ifcvt (Diamond): " << printMBBReference(*BBI.BB) 557*0b57cec5SDimitry Andric << " (T:" << BBI.TrueBB->getNumber() 558*0b57cec5SDimitry Andric << ",F:" << BBI.FalseBB->getNumber() << ") "); 559*0b57cec5SDimitry Andric RetVal = IfConvertDiamond(BBI, Kind, NumDups, NumDups2, 560*0b57cec5SDimitry Andric Token->TClobbersPred, 561*0b57cec5SDimitry Andric Token->FClobbersPred); 562*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n"); 563*0b57cec5SDimitry Andric if (RetVal) ++NumDiamonds; 564*0b57cec5SDimitry Andric break; 565*0b57cec5SDimitry Andric case ICForkedDiamond: 566*0b57cec5SDimitry Andric if (DisableForkedDiamond) break; 567*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Ifcvt (Forked Diamond): " 568*0b57cec5SDimitry Andric << printMBBReference(*BBI.BB) 569*0b57cec5SDimitry Andric << " (T:" << BBI.TrueBB->getNumber() 570*0b57cec5SDimitry Andric << ",F:" << BBI.FalseBB->getNumber() << ") "); 571*0b57cec5SDimitry Andric RetVal = IfConvertForkedDiamond(BBI, Kind, NumDups, NumDups2, 572*0b57cec5SDimitry Andric Token->TClobbersPred, 573*0b57cec5SDimitry Andric Token->FClobbersPred); 574*0b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << (RetVal ? "succeeded!" : "failed!") << "\n"); 575*0b57cec5SDimitry Andric if (RetVal) ++NumForkedDiamonds; 576*0b57cec5SDimitry Andric break; 577*0b57cec5SDimitry Andric } 578*0b57cec5SDimitry Andric 579*0b57cec5SDimitry Andric if (RetVal && MRI->tracksLiveness()) 580*0b57cec5SDimitry Andric recomputeLivenessFlags(*BBI.BB); 581*0b57cec5SDimitry Andric 582*0b57cec5SDimitry Andric Change |= RetVal; 583*0b57cec5SDimitry Andric 584*0b57cec5SDimitry Andric NumIfCvts = NumSimple + NumSimpleFalse + NumTriangle + NumTriangleRev + 585*0b57cec5SDimitry Andric NumTriangleFalse + NumTriangleFRev + NumDiamonds; 586*0b57cec5SDimitry Andric if (IfCvtLimit != -1 && (int)NumIfCvts >= IfCvtLimit) 587*0b57cec5SDimitry Andric break; 588*0b57cec5SDimitry Andric } 589*0b57cec5SDimitry Andric 590*0b57cec5SDimitry Andric if (!Change) 591*0b57cec5SDimitry Andric break; 592*0b57cec5SDimitry Andric MadeChange |= Change; 593*0b57cec5SDimitry Andric } 594*0b57cec5SDimitry Andric 595*0b57cec5SDimitry Andric Tokens.clear(); 596*0b57cec5SDimitry Andric BBAnalysis.clear(); 597*0b57cec5SDimitry Andric 598*0b57cec5SDimitry Andric if (MadeChange && IfCvtBranchFold) { 599*0b57cec5SDimitry Andric BranchFolder BF(false, false, MBFI, *MBPI); 6008bcb0991SDimitry Andric auto *MMIWP = getAnalysisIfAvailable<MachineModuleInfoWrapperPass>(); 6018bcb0991SDimitry Andric BF.OptimizeFunction( 6028bcb0991SDimitry Andric MF, TII, MF.getSubtarget().getRegisterInfo(), 6038bcb0991SDimitry Andric MMIWP ? &MMIWP->getMMI() : nullptr); 604*0b57cec5SDimitry Andric } 605*0b57cec5SDimitry Andric 606*0b57cec5SDimitry Andric MadeChange |= BFChange; 607*0b57cec5SDimitry Andric return MadeChange; 608*0b57cec5SDimitry Andric } 609*0b57cec5SDimitry Andric 610*0b57cec5SDimitry Andric /// BB has a fallthrough. Find its 'false' successor given its 'true' successor. 611*0b57cec5SDimitry Andric static MachineBasicBlock *findFalseBlock(MachineBasicBlock *BB, 612*0b57cec5SDimitry Andric MachineBasicBlock *TrueBB) { 613*0b57cec5SDimitry Andric for (MachineBasicBlock *SuccBB : BB->successors()) { 614*0b57cec5SDimitry Andric if (SuccBB != TrueBB) 615*0b57cec5SDimitry Andric return SuccBB; 616*0b57cec5SDimitry Andric } 617*0b57cec5SDimitry Andric return nullptr; 618*0b57cec5SDimitry Andric } 619*0b57cec5SDimitry Andric 620*0b57cec5SDimitry Andric /// Reverse the condition of the end of the block branch. Swap block's 'true' 621*0b57cec5SDimitry Andric /// and 'false' successors. 622*0b57cec5SDimitry Andric bool IfConverter::reverseBranchCondition(BBInfo &BBI) const { 623*0b57cec5SDimitry Andric DebugLoc dl; // FIXME: this is nowhere 624*0b57cec5SDimitry Andric if (!TII->reverseBranchCondition(BBI.BrCond)) { 625*0b57cec5SDimitry Andric TII->removeBranch(*BBI.BB); 626*0b57cec5SDimitry Andric TII->insertBranch(*BBI.BB, BBI.FalseBB, BBI.TrueBB, BBI.BrCond, dl); 627*0b57cec5SDimitry Andric std::swap(BBI.TrueBB, BBI.FalseBB); 628*0b57cec5SDimitry Andric return true; 629*0b57cec5SDimitry Andric } 630*0b57cec5SDimitry Andric return false; 631*0b57cec5SDimitry Andric } 632*0b57cec5SDimitry Andric 633*0b57cec5SDimitry Andric /// Returns the next block in the function blocks ordering. If it is the end, 634*0b57cec5SDimitry Andric /// returns NULL. 635*0b57cec5SDimitry Andric static inline MachineBasicBlock *getNextBlock(MachineBasicBlock &MBB) { 636*0b57cec5SDimitry Andric MachineFunction::iterator I = MBB.getIterator(); 637*0b57cec5SDimitry Andric MachineFunction::iterator E = MBB.getParent()->end(); 638*0b57cec5SDimitry Andric if (++I == E) 639*0b57cec5SDimitry Andric return nullptr; 640*0b57cec5SDimitry Andric return &*I; 641*0b57cec5SDimitry Andric } 642*0b57cec5SDimitry Andric 643*0b57cec5SDimitry Andric /// Returns true if the 'true' block (along with its predecessor) forms a valid 644*0b57cec5SDimitry Andric /// simple shape for ifcvt. It also returns the number of instructions that the 645*0b57cec5SDimitry Andric /// ifcvt would need to duplicate if performed in Dups. 646*0b57cec5SDimitry Andric bool IfConverter::ValidSimple(BBInfo &TrueBBI, unsigned &Dups, 647*0b57cec5SDimitry Andric BranchProbability Prediction) const { 648*0b57cec5SDimitry Andric Dups = 0; 649*0b57cec5SDimitry Andric if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone) 650*0b57cec5SDimitry Andric return false; 651*0b57cec5SDimitry Andric 652*0b57cec5SDimitry Andric if (TrueBBI.IsBrAnalyzable) 653*0b57cec5SDimitry Andric return false; 654*0b57cec5SDimitry Andric 655*0b57cec5SDimitry Andric if (TrueBBI.BB->pred_size() > 1) { 656*0b57cec5SDimitry Andric if (TrueBBI.CannotBeCopied || 657*0b57cec5SDimitry Andric !TII->isProfitableToDupForIfCvt(*TrueBBI.BB, TrueBBI.NonPredSize, 658*0b57cec5SDimitry Andric Prediction)) 659*0b57cec5SDimitry Andric return false; 660*0b57cec5SDimitry Andric Dups = TrueBBI.NonPredSize; 661*0b57cec5SDimitry Andric } 662*0b57cec5SDimitry Andric 663*0b57cec5SDimitry Andric return true; 664*0b57cec5SDimitry Andric } 665*0b57cec5SDimitry Andric 666*0b57cec5SDimitry Andric /// Returns true if the 'true' and 'false' blocks (along with their common 667*0b57cec5SDimitry Andric /// predecessor) forms a valid triangle shape for ifcvt. If 'FalseBranch' is 668*0b57cec5SDimitry Andric /// true, it checks if 'true' block's false branch branches to the 'false' block 669*0b57cec5SDimitry Andric /// rather than the other way around. It also returns the number of instructions 670*0b57cec5SDimitry Andric /// that the ifcvt would need to duplicate if performed in 'Dups'. 671*0b57cec5SDimitry Andric bool IfConverter::ValidTriangle(BBInfo &TrueBBI, BBInfo &FalseBBI, 672*0b57cec5SDimitry Andric bool FalseBranch, unsigned &Dups, 673*0b57cec5SDimitry Andric BranchProbability Prediction) const { 674*0b57cec5SDimitry Andric Dups = 0; 6758bcb0991SDimitry Andric if (TrueBBI.BB == FalseBBI.BB) 6768bcb0991SDimitry Andric return false; 6778bcb0991SDimitry Andric 678*0b57cec5SDimitry Andric if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone) 679*0b57cec5SDimitry Andric return false; 680*0b57cec5SDimitry Andric 681*0b57cec5SDimitry Andric if (TrueBBI.BB->pred_size() > 1) { 682*0b57cec5SDimitry Andric if (TrueBBI.CannotBeCopied) 683*0b57cec5SDimitry Andric return false; 684*0b57cec5SDimitry Andric 685*0b57cec5SDimitry Andric unsigned Size = TrueBBI.NonPredSize; 686*0b57cec5SDimitry Andric if (TrueBBI.IsBrAnalyzable) { 687*0b57cec5SDimitry Andric if (TrueBBI.TrueBB && TrueBBI.BrCond.empty()) 688*0b57cec5SDimitry Andric // Ends with an unconditional branch. It will be removed. 689*0b57cec5SDimitry Andric --Size; 690*0b57cec5SDimitry Andric else { 691*0b57cec5SDimitry Andric MachineBasicBlock *FExit = FalseBranch 692*0b57cec5SDimitry Andric ? TrueBBI.TrueBB : TrueBBI.FalseBB; 693*0b57cec5SDimitry Andric if (FExit) 694*0b57cec5SDimitry Andric // Require a conditional branch 695*0b57cec5SDimitry Andric ++Size; 696*0b57cec5SDimitry Andric } 697*0b57cec5SDimitry Andric } 698*0b57cec5SDimitry Andric if (!TII->isProfitableToDupForIfCvt(*TrueBBI.BB, Size, Prediction)) 699*0b57cec5SDimitry Andric return false; 700*0b57cec5SDimitry Andric Dups = Size; 701*0b57cec5SDimitry Andric } 702*0b57cec5SDimitry Andric 703*0b57cec5SDimitry Andric MachineBasicBlock *TExit = FalseBranch ? TrueBBI.FalseBB : TrueBBI.TrueBB; 704*0b57cec5SDimitry Andric if (!TExit && blockAlwaysFallThrough(TrueBBI)) { 705*0b57cec5SDimitry Andric MachineFunction::iterator I = TrueBBI.BB->getIterator(); 706*0b57cec5SDimitry Andric if (++I == TrueBBI.BB->getParent()->end()) 707*0b57cec5SDimitry Andric return false; 708*0b57cec5SDimitry Andric TExit = &*I; 709*0b57cec5SDimitry Andric } 710*0b57cec5SDimitry Andric return TExit && TExit == FalseBBI.BB; 711*0b57cec5SDimitry Andric } 712*0b57cec5SDimitry Andric 713*0b57cec5SDimitry Andric /// Count duplicated instructions and move the iterators to show where they 714*0b57cec5SDimitry Andric /// are. 715*0b57cec5SDimitry Andric /// @param TIB True Iterator Begin 716*0b57cec5SDimitry Andric /// @param FIB False Iterator Begin 717*0b57cec5SDimitry Andric /// These two iterators initially point to the first instruction of the two 718*0b57cec5SDimitry Andric /// blocks, and finally point to the first non-shared instruction. 719*0b57cec5SDimitry Andric /// @param TIE True Iterator End 720*0b57cec5SDimitry Andric /// @param FIE False Iterator End 721*0b57cec5SDimitry Andric /// These two iterators initially point to End() for the two blocks() and 722*0b57cec5SDimitry Andric /// finally point to the first shared instruction in the tail. 723*0b57cec5SDimitry Andric /// Upon return [TIB, TIE), and [FIB, FIE) mark the un-duplicated portions of 724*0b57cec5SDimitry Andric /// two blocks. 725*0b57cec5SDimitry Andric /// @param Dups1 count of duplicated instructions at the beginning of the 2 726*0b57cec5SDimitry Andric /// blocks. 727*0b57cec5SDimitry Andric /// @param Dups2 count of duplicated instructions at the end of the 2 blocks. 728*0b57cec5SDimitry Andric /// @param SkipUnconditionalBranches if true, Don't make sure that 729*0b57cec5SDimitry Andric /// unconditional branches at the end of the blocks are the same. True is 730*0b57cec5SDimitry Andric /// passed when the blocks are analyzable to allow for fallthrough to be 731*0b57cec5SDimitry Andric /// handled. 732*0b57cec5SDimitry Andric /// @return false if the shared portion prevents if conversion. 733*0b57cec5SDimitry Andric bool IfConverter::CountDuplicatedInstructions( 734*0b57cec5SDimitry Andric MachineBasicBlock::iterator &TIB, 735*0b57cec5SDimitry Andric MachineBasicBlock::iterator &FIB, 736*0b57cec5SDimitry Andric MachineBasicBlock::iterator &TIE, 737*0b57cec5SDimitry Andric MachineBasicBlock::iterator &FIE, 738*0b57cec5SDimitry Andric unsigned &Dups1, unsigned &Dups2, 739*0b57cec5SDimitry Andric MachineBasicBlock &TBB, MachineBasicBlock &FBB, 740*0b57cec5SDimitry Andric bool SkipUnconditionalBranches) const { 741*0b57cec5SDimitry Andric while (TIB != TIE && FIB != FIE) { 742*0b57cec5SDimitry Andric // Skip dbg_value instructions. These do not count. 743*0b57cec5SDimitry Andric TIB = skipDebugInstructionsForward(TIB, TIE); 744*0b57cec5SDimitry Andric FIB = skipDebugInstructionsForward(FIB, FIE); 745*0b57cec5SDimitry Andric if (TIB == TIE || FIB == FIE) 746*0b57cec5SDimitry Andric break; 747*0b57cec5SDimitry Andric if (!TIB->isIdenticalTo(*FIB)) 748*0b57cec5SDimitry Andric break; 749*0b57cec5SDimitry Andric // A pred-clobbering instruction in the shared portion prevents 750*0b57cec5SDimitry Andric // if-conversion. 751*0b57cec5SDimitry Andric std::vector<MachineOperand> PredDefs; 752*0b57cec5SDimitry Andric if (TII->DefinesPredicate(*TIB, PredDefs)) 753*0b57cec5SDimitry Andric return false; 754*0b57cec5SDimitry Andric // If we get all the way to the branch instructions, don't count them. 755*0b57cec5SDimitry Andric if (!TIB->isBranch()) 756*0b57cec5SDimitry Andric ++Dups1; 757*0b57cec5SDimitry Andric ++TIB; 758*0b57cec5SDimitry Andric ++FIB; 759*0b57cec5SDimitry Andric } 760*0b57cec5SDimitry Andric 761*0b57cec5SDimitry Andric // Check for already containing all of the block. 762*0b57cec5SDimitry Andric if (TIB == TIE || FIB == FIE) 763*0b57cec5SDimitry Andric return true; 764*0b57cec5SDimitry Andric // Now, in preparation for counting duplicate instructions at the ends of the 765*0b57cec5SDimitry Andric // blocks, switch to reverse_iterators. Note that getReverse() returns an 766*0b57cec5SDimitry Andric // iterator that points to the same instruction, unlike std::reverse_iterator. 767*0b57cec5SDimitry Andric // We have to do our own shifting so that we get the same range. 768*0b57cec5SDimitry Andric MachineBasicBlock::reverse_iterator RTIE = std::next(TIE.getReverse()); 769*0b57cec5SDimitry Andric MachineBasicBlock::reverse_iterator RFIE = std::next(FIE.getReverse()); 770*0b57cec5SDimitry Andric const MachineBasicBlock::reverse_iterator RTIB = std::next(TIB.getReverse()); 771*0b57cec5SDimitry Andric const MachineBasicBlock::reverse_iterator RFIB = std::next(FIB.getReverse()); 772*0b57cec5SDimitry Andric 773*0b57cec5SDimitry Andric if (!TBB.succ_empty() || !FBB.succ_empty()) { 774*0b57cec5SDimitry Andric if (SkipUnconditionalBranches) { 775*0b57cec5SDimitry Andric while (RTIE != RTIB && RTIE->isUnconditionalBranch()) 776*0b57cec5SDimitry Andric ++RTIE; 777*0b57cec5SDimitry Andric while (RFIE != RFIB && RFIE->isUnconditionalBranch()) 778*0b57cec5SDimitry Andric ++RFIE; 779*0b57cec5SDimitry Andric } 780*0b57cec5SDimitry Andric } 781*0b57cec5SDimitry Andric 782*0b57cec5SDimitry Andric // Count duplicate instructions at the ends of the blocks. 783*0b57cec5SDimitry Andric while (RTIE != RTIB && RFIE != RFIB) { 784*0b57cec5SDimitry Andric // Skip dbg_value instructions. These do not count. 785*0b57cec5SDimitry Andric // Note that these are reverse iterators going forward. 786*0b57cec5SDimitry Andric RTIE = skipDebugInstructionsForward(RTIE, RTIB); 787*0b57cec5SDimitry Andric RFIE = skipDebugInstructionsForward(RFIE, RFIB); 788*0b57cec5SDimitry Andric if (RTIE == RTIB || RFIE == RFIB) 789*0b57cec5SDimitry Andric break; 790*0b57cec5SDimitry Andric if (!RTIE->isIdenticalTo(*RFIE)) 791*0b57cec5SDimitry Andric break; 792*0b57cec5SDimitry Andric // We have to verify that any branch instructions are the same, and then we 793*0b57cec5SDimitry Andric // don't count them toward the # of duplicate instructions. 794*0b57cec5SDimitry Andric if (!RTIE->isBranch()) 795*0b57cec5SDimitry Andric ++Dups2; 796*0b57cec5SDimitry Andric ++RTIE; 797*0b57cec5SDimitry Andric ++RFIE; 798*0b57cec5SDimitry Andric } 799*0b57cec5SDimitry Andric TIE = std::next(RTIE.getReverse()); 800*0b57cec5SDimitry Andric FIE = std::next(RFIE.getReverse()); 801*0b57cec5SDimitry Andric return true; 802*0b57cec5SDimitry Andric } 803*0b57cec5SDimitry Andric 804*0b57cec5SDimitry Andric /// RescanInstructions - Run ScanInstructions on a pair of blocks. 805*0b57cec5SDimitry Andric /// @param TIB - True Iterator Begin, points to first non-shared instruction 806*0b57cec5SDimitry Andric /// @param FIB - False Iterator Begin, points to first non-shared instruction 807*0b57cec5SDimitry Andric /// @param TIE - True Iterator End, points past last non-shared instruction 808*0b57cec5SDimitry Andric /// @param FIE - False Iterator End, points past last non-shared instruction 809*0b57cec5SDimitry Andric /// @param TrueBBI - BBInfo to update for the true block. 810*0b57cec5SDimitry Andric /// @param FalseBBI - BBInfo to update for the false block. 811*0b57cec5SDimitry Andric /// @returns - false if either block cannot be predicated or if both blocks end 812*0b57cec5SDimitry Andric /// with a predicate-clobbering instruction. 813*0b57cec5SDimitry Andric bool IfConverter::RescanInstructions( 814*0b57cec5SDimitry Andric MachineBasicBlock::iterator &TIB, MachineBasicBlock::iterator &FIB, 815*0b57cec5SDimitry Andric MachineBasicBlock::iterator &TIE, MachineBasicBlock::iterator &FIE, 816*0b57cec5SDimitry Andric BBInfo &TrueBBI, BBInfo &FalseBBI) const { 817*0b57cec5SDimitry Andric bool BranchUnpredicable = true; 818*0b57cec5SDimitry Andric TrueBBI.IsUnpredicable = FalseBBI.IsUnpredicable = false; 819*0b57cec5SDimitry Andric ScanInstructions(TrueBBI, TIB, TIE, BranchUnpredicable); 820*0b57cec5SDimitry Andric if (TrueBBI.IsUnpredicable) 821*0b57cec5SDimitry Andric return false; 822*0b57cec5SDimitry Andric ScanInstructions(FalseBBI, FIB, FIE, BranchUnpredicable); 823*0b57cec5SDimitry Andric if (FalseBBI.IsUnpredicable) 824*0b57cec5SDimitry Andric return false; 825*0b57cec5SDimitry Andric if (TrueBBI.ClobbersPred && FalseBBI.ClobbersPred) 826*0b57cec5SDimitry Andric return false; 827*0b57cec5SDimitry Andric return true; 828*0b57cec5SDimitry Andric } 829*0b57cec5SDimitry Andric 830*0b57cec5SDimitry Andric #ifndef NDEBUG 831*0b57cec5SDimitry Andric static void verifySameBranchInstructions( 832*0b57cec5SDimitry Andric MachineBasicBlock *MBB1, 833*0b57cec5SDimitry Andric MachineBasicBlock *MBB2) { 834*0b57cec5SDimitry Andric const MachineBasicBlock::reverse_iterator B1 = MBB1->rend(); 835*0b57cec5SDimitry Andric const MachineBasicBlock::reverse_iterator B2 = MBB2->rend(); 836*0b57cec5SDimitry Andric MachineBasicBlock::reverse_iterator E1 = MBB1->rbegin(); 837*0b57cec5SDimitry Andric MachineBasicBlock::reverse_iterator E2 = MBB2->rbegin(); 838*0b57cec5SDimitry Andric while (E1 != B1 && E2 != B2) { 839*0b57cec5SDimitry Andric skipDebugInstructionsForward(E1, B1); 840*0b57cec5SDimitry Andric skipDebugInstructionsForward(E2, B2); 841*0b57cec5SDimitry Andric if (E1 == B1 && E2 == B2) 842*0b57cec5SDimitry Andric break; 843*0b57cec5SDimitry Andric 844*0b57cec5SDimitry Andric if (E1 == B1) { 845*0b57cec5SDimitry Andric assert(!E2->isBranch() && "Branch mis-match, one block is empty."); 846*0b57cec5SDimitry Andric break; 847*0b57cec5SDimitry Andric } 848*0b57cec5SDimitry Andric if (E2 == B2) { 849*0b57cec5SDimitry Andric assert(!E1->isBranch() && "Branch mis-match, one block is empty."); 850*0b57cec5SDimitry Andric break; 851*0b57cec5SDimitry Andric } 852*0b57cec5SDimitry Andric 853*0b57cec5SDimitry Andric if (E1->isBranch() || E2->isBranch()) 854*0b57cec5SDimitry Andric assert(E1->isIdenticalTo(*E2) && 855*0b57cec5SDimitry Andric "Branch mis-match, branch instructions don't match."); 856*0b57cec5SDimitry Andric else 857*0b57cec5SDimitry Andric break; 858*0b57cec5SDimitry Andric ++E1; 859*0b57cec5SDimitry Andric ++E2; 860*0b57cec5SDimitry Andric } 861*0b57cec5SDimitry Andric } 862*0b57cec5SDimitry Andric #endif 863*0b57cec5SDimitry Andric 864*0b57cec5SDimitry Andric /// ValidForkedDiamond - Returns true if the 'true' and 'false' blocks (along 865*0b57cec5SDimitry Andric /// with their common predecessor) form a diamond if a common tail block is 866*0b57cec5SDimitry Andric /// extracted. 867*0b57cec5SDimitry Andric /// While not strictly a diamond, this pattern would form a diamond if 868*0b57cec5SDimitry Andric /// tail-merging had merged the shared tails. 869*0b57cec5SDimitry Andric /// EBB 870*0b57cec5SDimitry Andric /// _/ \_ 871*0b57cec5SDimitry Andric /// | | 872*0b57cec5SDimitry Andric /// TBB FBB 873*0b57cec5SDimitry Andric /// / \ / \ 874*0b57cec5SDimitry Andric /// FalseBB TrueBB FalseBB 875*0b57cec5SDimitry Andric /// Currently only handles analyzable branches. 876*0b57cec5SDimitry Andric /// Specifically excludes actual diamonds to avoid overlap. 877*0b57cec5SDimitry Andric bool IfConverter::ValidForkedDiamond( 878*0b57cec5SDimitry Andric BBInfo &TrueBBI, BBInfo &FalseBBI, 879*0b57cec5SDimitry Andric unsigned &Dups1, unsigned &Dups2, 880*0b57cec5SDimitry Andric BBInfo &TrueBBICalc, BBInfo &FalseBBICalc) const { 881*0b57cec5SDimitry Andric Dups1 = Dups2 = 0; 882*0b57cec5SDimitry Andric if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone || 883*0b57cec5SDimitry Andric FalseBBI.IsBeingAnalyzed || FalseBBI.IsDone) 884*0b57cec5SDimitry Andric return false; 885*0b57cec5SDimitry Andric 886*0b57cec5SDimitry Andric if (!TrueBBI.IsBrAnalyzable || !FalseBBI.IsBrAnalyzable) 887*0b57cec5SDimitry Andric return false; 888*0b57cec5SDimitry Andric // Don't IfConvert blocks that can't be folded into their predecessor. 889*0b57cec5SDimitry Andric if (TrueBBI.BB->pred_size() > 1 || FalseBBI.BB->pred_size() > 1) 890*0b57cec5SDimitry Andric return false; 891*0b57cec5SDimitry Andric 892*0b57cec5SDimitry Andric // This function is specifically looking for conditional tails, as 893*0b57cec5SDimitry Andric // unconditional tails are already handled by the standard diamond case. 894*0b57cec5SDimitry Andric if (TrueBBI.BrCond.size() == 0 || 895*0b57cec5SDimitry Andric FalseBBI.BrCond.size() == 0) 896*0b57cec5SDimitry Andric return false; 897*0b57cec5SDimitry Andric 898*0b57cec5SDimitry Andric MachineBasicBlock *TT = TrueBBI.TrueBB; 899*0b57cec5SDimitry Andric MachineBasicBlock *TF = TrueBBI.FalseBB; 900*0b57cec5SDimitry Andric MachineBasicBlock *FT = FalseBBI.TrueBB; 901*0b57cec5SDimitry Andric MachineBasicBlock *FF = FalseBBI.FalseBB; 902*0b57cec5SDimitry Andric 903*0b57cec5SDimitry Andric if (!TT) 904*0b57cec5SDimitry Andric TT = getNextBlock(*TrueBBI.BB); 905*0b57cec5SDimitry Andric if (!TF) 906*0b57cec5SDimitry Andric TF = getNextBlock(*TrueBBI.BB); 907*0b57cec5SDimitry Andric if (!FT) 908*0b57cec5SDimitry Andric FT = getNextBlock(*FalseBBI.BB); 909*0b57cec5SDimitry Andric if (!FF) 910*0b57cec5SDimitry Andric FF = getNextBlock(*FalseBBI.BB); 911*0b57cec5SDimitry Andric 912*0b57cec5SDimitry Andric if (!TT || !TF) 913*0b57cec5SDimitry Andric return false; 914*0b57cec5SDimitry Andric 915*0b57cec5SDimitry Andric // Check successors. If they don't match, bail. 916*0b57cec5SDimitry Andric if (!((TT == FT && TF == FF) || (TF == FT && TT == FF))) 917*0b57cec5SDimitry Andric return false; 918*0b57cec5SDimitry Andric 919*0b57cec5SDimitry Andric bool FalseReversed = false; 920*0b57cec5SDimitry Andric if (TF == FT && TT == FF) { 921*0b57cec5SDimitry Andric // If the branches are opposing, but we can't reverse, don't do it. 922*0b57cec5SDimitry Andric if (!FalseBBI.IsBrReversible) 923*0b57cec5SDimitry Andric return false; 924*0b57cec5SDimitry Andric FalseReversed = true; 925*0b57cec5SDimitry Andric reverseBranchCondition(FalseBBI); 926*0b57cec5SDimitry Andric } 927*0b57cec5SDimitry Andric auto UnReverseOnExit = make_scope_exit([&]() { 928*0b57cec5SDimitry Andric if (FalseReversed) 929*0b57cec5SDimitry Andric reverseBranchCondition(FalseBBI); 930*0b57cec5SDimitry Andric }); 931*0b57cec5SDimitry Andric 932*0b57cec5SDimitry Andric // Count duplicate instructions at the beginning of the true and false blocks. 933*0b57cec5SDimitry Andric MachineBasicBlock::iterator TIB = TrueBBI.BB->begin(); 934*0b57cec5SDimitry Andric MachineBasicBlock::iterator FIB = FalseBBI.BB->begin(); 935*0b57cec5SDimitry Andric MachineBasicBlock::iterator TIE = TrueBBI.BB->end(); 936*0b57cec5SDimitry Andric MachineBasicBlock::iterator FIE = FalseBBI.BB->end(); 937*0b57cec5SDimitry Andric if(!CountDuplicatedInstructions(TIB, FIB, TIE, FIE, Dups1, Dups2, 938*0b57cec5SDimitry Andric *TrueBBI.BB, *FalseBBI.BB, 939*0b57cec5SDimitry Andric /* SkipUnconditionalBranches */ true)) 940*0b57cec5SDimitry Andric return false; 941*0b57cec5SDimitry Andric 942*0b57cec5SDimitry Andric TrueBBICalc.BB = TrueBBI.BB; 943*0b57cec5SDimitry Andric FalseBBICalc.BB = FalseBBI.BB; 9448bcb0991SDimitry Andric TrueBBICalc.IsBrAnalyzable = TrueBBI.IsBrAnalyzable; 9458bcb0991SDimitry Andric FalseBBICalc.IsBrAnalyzable = FalseBBI.IsBrAnalyzable; 946*0b57cec5SDimitry Andric if (!RescanInstructions(TIB, FIB, TIE, FIE, TrueBBICalc, FalseBBICalc)) 947*0b57cec5SDimitry Andric return false; 948*0b57cec5SDimitry Andric 949*0b57cec5SDimitry Andric // The size is used to decide whether to if-convert, and the shared portions 950*0b57cec5SDimitry Andric // are subtracted off. Because of the subtraction, we just use the size that 951*0b57cec5SDimitry Andric // was calculated by the original ScanInstructions, as it is correct. 952*0b57cec5SDimitry Andric TrueBBICalc.NonPredSize = TrueBBI.NonPredSize; 953*0b57cec5SDimitry Andric FalseBBICalc.NonPredSize = FalseBBI.NonPredSize; 954*0b57cec5SDimitry Andric return true; 955*0b57cec5SDimitry Andric } 956*0b57cec5SDimitry Andric 957*0b57cec5SDimitry Andric /// ValidDiamond - Returns true if the 'true' and 'false' blocks (along 958*0b57cec5SDimitry Andric /// with their common predecessor) forms a valid diamond shape for ifcvt. 959*0b57cec5SDimitry Andric bool IfConverter::ValidDiamond( 960*0b57cec5SDimitry Andric BBInfo &TrueBBI, BBInfo &FalseBBI, 961*0b57cec5SDimitry Andric unsigned &Dups1, unsigned &Dups2, 962*0b57cec5SDimitry Andric BBInfo &TrueBBICalc, BBInfo &FalseBBICalc) const { 963*0b57cec5SDimitry Andric Dups1 = Dups2 = 0; 964*0b57cec5SDimitry Andric if (TrueBBI.IsBeingAnalyzed || TrueBBI.IsDone || 965*0b57cec5SDimitry Andric FalseBBI.IsBeingAnalyzed || FalseBBI.IsDone) 966*0b57cec5SDimitry Andric return false; 967*0b57cec5SDimitry Andric 968*0b57cec5SDimitry Andric MachineBasicBlock *TT = TrueBBI.TrueBB; 969*0b57cec5SDimitry Andric MachineBasicBlock *FT = FalseBBI.TrueBB; 970*0b57cec5SDimitry Andric 971*0b57cec5SDimitry Andric if (!TT && blockAlwaysFallThrough(TrueBBI)) 972*0b57cec5SDimitry Andric TT = getNextBlock(*TrueBBI.BB); 973*0b57cec5SDimitry Andric if (!FT && blockAlwaysFallThrough(FalseBBI)) 974*0b57cec5SDimitry Andric FT = getNextBlock(*FalseBBI.BB); 975*0b57cec5SDimitry Andric if (TT != FT) 976*0b57cec5SDimitry Andric return false; 977*0b57cec5SDimitry Andric if (!TT && (TrueBBI.IsBrAnalyzable || FalseBBI.IsBrAnalyzable)) 978*0b57cec5SDimitry Andric return false; 979*0b57cec5SDimitry Andric if (TrueBBI.BB->pred_size() > 1 || FalseBBI.BB->pred_size() > 1) 980*0b57cec5SDimitry Andric return false; 981*0b57cec5SDimitry Andric 982*0b57cec5SDimitry Andric // FIXME: Allow true block to have an early exit? 983*0b57cec5SDimitry Andric if (TrueBBI.FalseBB || FalseBBI.FalseBB) 984*0b57cec5SDimitry Andric return false; 985*0b57cec5SDimitry Andric 986*0b57cec5SDimitry Andric // Count duplicate instructions at the beginning and end of the true and 987*0b57cec5SDimitry Andric // false blocks. 988*0b57cec5SDimitry Andric // Skip unconditional branches only if we are considering an analyzable 989*0b57cec5SDimitry Andric // diamond. Otherwise the branches must be the same. 990*0b57cec5SDimitry Andric bool SkipUnconditionalBranches = 991*0b57cec5SDimitry Andric TrueBBI.IsBrAnalyzable && FalseBBI.IsBrAnalyzable; 992*0b57cec5SDimitry Andric MachineBasicBlock::iterator TIB = TrueBBI.BB->begin(); 993*0b57cec5SDimitry Andric MachineBasicBlock::iterator FIB = FalseBBI.BB->begin(); 994*0b57cec5SDimitry Andric MachineBasicBlock::iterator TIE = TrueBBI.BB->end(); 995*0b57cec5SDimitry Andric MachineBasicBlock::iterator FIE = FalseBBI.BB->end(); 996*0b57cec5SDimitry Andric if(!CountDuplicatedInstructions(TIB, FIB, TIE, FIE, Dups1, Dups2, 997*0b57cec5SDimitry Andric *TrueBBI.BB, *FalseBBI.BB, 998*0b57cec5SDimitry Andric SkipUnconditionalBranches)) 999*0b57cec5SDimitry Andric return false; 1000*0b57cec5SDimitry Andric 1001*0b57cec5SDimitry Andric TrueBBICalc.BB = TrueBBI.BB; 1002*0b57cec5SDimitry Andric FalseBBICalc.BB = FalseBBI.BB; 10038bcb0991SDimitry Andric TrueBBICalc.IsBrAnalyzable = TrueBBI.IsBrAnalyzable; 10048bcb0991SDimitry Andric FalseBBICalc.IsBrAnalyzable = FalseBBI.IsBrAnalyzable; 1005*0b57cec5SDimitry Andric if (!RescanInstructions(TIB, FIB, TIE, FIE, TrueBBICalc, FalseBBICalc)) 1006*0b57cec5SDimitry Andric return false; 1007*0b57cec5SDimitry Andric // The size is used to decide whether to if-convert, and the shared portions 1008*0b57cec5SDimitry Andric // are subtracted off. Because of the subtraction, we just use the size that 1009*0b57cec5SDimitry Andric // was calculated by the original ScanInstructions, as it is correct. 1010*0b57cec5SDimitry Andric TrueBBICalc.NonPredSize = TrueBBI.NonPredSize; 1011*0b57cec5SDimitry Andric FalseBBICalc.NonPredSize = FalseBBI.NonPredSize; 1012*0b57cec5SDimitry Andric return true; 1013*0b57cec5SDimitry Andric } 1014*0b57cec5SDimitry Andric 1015*0b57cec5SDimitry Andric /// AnalyzeBranches - Look at the branches at the end of a block to determine if 1016*0b57cec5SDimitry Andric /// the block is predicable. 1017*0b57cec5SDimitry Andric void IfConverter::AnalyzeBranches(BBInfo &BBI) { 1018*0b57cec5SDimitry Andric if (BBI.IsDone) 1019*0b57cec5SDimitry Andric return; 1020*0b57cec5SDimitry Andric 1021*0b57cec5SDimitry Andric BBI.TrueBB = BBI.FalseBB = nullptr; 1022*0b57cec5SDimitry Andric BBI.BrCond.clear(); 1023*0b57cec5SDimitry Andric BBI.IsBrAnalyzable = 1024*0b57cec5SDimitry Andric !TII->analyzeBranch(*BBI.BB, BBI.TrueBB, BBI.FalseBB, BBI.BrCond); 1025*0b57cec5SDimitry Andric if (!BBI.IsBrAnalyzable) { 1026*0b57cec5SDimitry Andric BBI.TrueBB = nullptr; 1027*0b57cec5SDimitry Andric BBI.FalseBB = nullptr; 1028*0b57cec5SDimitry Andric BBI.BrCond.clear(); 1029*0b57cec5SDimitry Andric } 1030*0b57cec5SDimitry Andric 1031*0b57cec5SDimitry Andric SmallVector<MachineOperand, 4> RevCond(BBI.BrCond.begin(), BBI.BrCond.end()); 1032*0b57cec5SDimitry Andric BBI.IsBrReversible = (RevCond.size() == 0) || 1033*0b57cec5SDimitry Andric !TII->reverseBranchCondition(RevCond); 1034*0b57cec5SDimitry Andric BBI.HasFallThrough = BBI.IsBrAnalyzable && BBI.FalseBB == nullptr; 1035*0b57cec5SDimitry Andric 1036*0b57cec5SDimitry Andric if (BBI.BrCond.size()) { 1037*0b57cec5SDimitry Andric // No false branch. This BB must end with a conditional branch and a 1038*0b57cec5SDimitry Andric // fallthrough. 1039*0b57cec5SDimitry Andric if (!BBI.FalseBB) 1040*0b57cec5SDimitry Andric BBI.FalseBB = findFalseBlock(BBI.BB, BBI.TrueBB); 1041*0b57cec5SDimitry Andric if (!BBI.FalseBB) { 1042*0b57cec5SDimitry Andric // Malformed bcc? True and false blocks are the same? 1043*0b57cec5SDimitry Andric BBI.IsUnpredicable = true; 1044*0b57cec5SDimitry Andric } 1045*0b57cec5SDimitry Andric } 1046*0b57cec5SDimitry Andric } 1047*0b57cec5SDimitry Andric 1048*0b57cec5SDimitry Andric /// ScanInstructions - Scan all the instructions in the block to determine if 1049*0b57cec5SDimitry Andric /// the block is predicable. In most cases, that means all the instructions 1050*0b57cec5SDimitry Andric /// in the block are isPredicable(). Also checks if the block contains any 1051*0b57cec5SDimitry Andric /// instruction which can clobber a predicate (e.g. condition code register). 1052*0b57cec5SDimitry Andric /// If so, the block is not predicable unless it's the last instruction. 1053*0b57cec5SDimitry Andric void IfConverter::ScanInstructions(BBInfo &BBI, 1054*0b57cec5SDimitry Andric MachineBasicBlock::iterator &Begin, 1055*0b57cec5SDimitry Andric MachineBasicBlock::iterator &End, 1056*0b57cec5SDimitry Andric bool BranchUnpredicable) const { 1057*0b57cec5SDimitry Andric if (BBI.IsDone || BBI.IsUnpredicable) 1058*0b57cec5SDimitry Andric return; 1059*0b57cec5SDimitry Andric 1060*0b57cec5SDimitry Andric bool AlreadyPredicated = !BBI.Predicate.empty(); 1061*0b57cec5SDimitry Andric 1062*0b57cec5SDimitry Andric BBI.NonPredSize = 0; 1063*0b57cec5SDimitry Andric BBI.ExtraCost = 0; 1064*0b57cec5SDimitry Andric BBI.ExtraCost2 = 0; 1065*0b57cec5SDimitry Andric BBI.ClobbersPred = false; 1066*0b57cec5SDimitry Andric for (MachineInstr &MI : make_range(Begin, End)) { 1067*0b57cec5SDimitry Andric if (MI.isDebugInstr()) 1068*0b57cec5SDimitry Andric continue; 1069*0b57cec5SDimitry Andric 1070*0b57cec5SDimitry Andric // It's unsafe to duplicate convergent instructions in this context, so set 1071*0b57cec5SDimitry Andric // BBI.CannotBeCopied to true if MI is convergent. To see why, consider the 1072*0b57cec5SDimitry Andric // following CFG, which is subject to our "simple" transformation. 1073*0b57cec5SDimitry Andric // 1074*0b57cec5SDimitry Andric // BB0 // if (c1) goto BB1; else goto BB2; 1075*0b57cec5SDimitry Andric // / \ 1076*0b57cec5SDimitry Andric // BB1 | 1077*0b57cec5SDimitry Andric // | BB2 // if (c2) goto TBB; else goto FBB; 1078*0b57cec5SDimitry Andric // | / | 1079*0b57cec5SDimitry Andric // | / | 1080*0b57cec5SDimitry Andric // TBB | 1081*0b57cec5SDimitry Andric // | | 1082*0b57cec5SDimitry Andric // | FBB 1083*0b57cec5SDimitry Andric // | 1084*0b57cec5SDimitry Andric // exit 1085*0b57cec5SDimitry Andric // 1086*0b57cec5SDimitry Andric // Suppose we want to move TBB's contents up into BB1 and BB2 (in BB1 they'd 1087*0b57cec5SDimitry Andric // be unconditional, and in BB2, they'd be predicated upon c2), and suppose 1088*0b57cec5SDimitry Andric // TBB contains a convergent instruction. This is safe iff doing so does 1089*0b57cec5SDimitry Andric // not add a control-flow dependency to the convergent instruction -- i.e., 1090*0b57cec5SDimitry Andric // it's safe iff the set of control flows that leads us to the convergent 1091*0b57cec5SDimitry Andric // instruction does not get smaller after the transformation. 1092*0b57cec5SDimitry Andric // 1093*0b57cec5SDimitry Andric // Originally we executed TBB if c1 || c2. After the transformation, there 1094*0b57cec5SDimitry Andric // are two copies of TBB's instructions. We get to the first if c1, and we 1095*0b57cec5SDimitry Andric // get to the second if !c1 && c2. 1096*0b57cec5SDimitry Andric // 1097*0b57cec5SDimitry Andric // There are clearly fewer ways to satisfy the condition "c1" than 1098*0b57cec5SDimitry Andric // "c1 || c2". Since we've shrunk the set of control flows which lead to 1099*0b57cec5SDimitry Andric // our convergent instruction, the transformation is unsafe. 1100*0b57cec5SDimitry Andric if (MI.isNotDuplicable() || MI.isConvergent()) 1101*0b57cec5SDimitry Andric BBI.CannotBeCopied = true; 1102*0b57cec5SDimitry Andric 1103*0b57cec5SDimitry Andric bool isPredicated = TII->isPredicated(MI); 1104*0b57cec5SDimitry Andric bool isCondBr = BBI.IsBrAnalyzable && MI.isConditionalBranch(); 1105*0b57cec5SDimitry Andric 1106*0b57cec5SDimitry Andric if (BranchUnpredicable && MI.isBranch()) { 1107*0b57cec5SDimitry Andric BBI.IsUnpredicable = true; 1108*0b57cec5SDimitry Andric return; 1109*0b57cec5SDimitry Andric } 1110*0b57cec5SDimitry Andric 1111*0b57cec5SDimitry Andric // A conditional branch is not predicable, but it may be eliminated. 1112*0b57cec5SDimitry Andric if (isCondBr) 1113*0b57cec5SDimitry Andric continue; 1114*0b57cec5SDimitry Andric 1115*0b57cec5SDimitry Andric if (!isPredicated) { 1116*0b57cec5SDimitry Andric BBI.NonPredSize++; 1117*0b57cec5SDimitry Andric unsigned ExtraPredCost = TII->getPredicationCost(MI); 1118*0b57cec5SDimitry Andric unsigned NumCycles = SchedModel.computeInstrLatency(&MI, false); 1119*0b57cec5SDimitry Andric if (NumCycles > 1) 1120*0b57cec5SDimitry Andric BBI.ExtraCost += NumCycles-1; 1121*0b57cec5SDimitry Andric BBI.ExtraCost2 += ExtraPredCost; 1122*0b57cec5SDimitry Andric } else if (!AlreadyPredicated) { 1123*0b57cec5SDimitry Andric // FIXME: This instruction is already predicated before the 1124*0b57cec5SDimitry Andric // if-conversion pass. It's probably something like a conditional move. 1125*0b57cec5SDimitry Andric // Mark this block unpredicable for now. 1126*0b57cec5SDimitry Andric BBI.IsUnpredicable = true; 1127*0b57cec5SDimitry Andric return; 1128*0b57cec5SDimitry Andric } 1129*0b57cec5SDimitry Andric 1130*0b57cec5SDimitry Andric if (BBI.ClobbersPred && !isPredicated) { 1131*0b57cec5SDimitry Andric // Predicate modification instruction should end the block (except for 1132*0b57cec5SDimitry Andric // already predicated instructions and end of block branches). 1133*0b57cec5SDimitry Andric // Predicate may have been modified, the subsequent (currently) 1134*0b57cec5SDimitry Andric // unpredicated instructions cannot be correctly predicated. 1135*0b57cec5SDimitry Andric BBI.IsUnpredicable = true; 1136*0b57cec5SDimitry Andric return; 1137*0b57cec5SDimitry Andric } 1138*0b57cec5SDimitry Andric 1139*0b57cec5SDimitry Andric // FIXME: Make use of PredDefs? e.g. ADDC, SUBC sets predicates but are 1140*0b57cec5SDimitry Andric // still potentially predicable. 1141*0b57cec5SDimitry Andric std::vector<MachineOperand> PredDefs; 1142*0b57cec5SDimitry Andric if (TII->DefinesPredicate(MI, PredDefs)) 1143*0b57cec5SDimitry Andric BBI.ClobbersPred = true; 1144*0b57cec5SDimitry Andric 1145*0b57cec5SDimitry Andric if (!TII->isPredicable(MI)) { 1146*0b57cec5SDimitry Andric BBI.IsUnpredicable = true; 1147*0b57cec5SDimitry Andric return; 1148*0b57cec5SDimitry Andric } 1149*0b57cec5SDimitry Andric } 1150*0b57cec5SDimitry Andric } 1151*0b57cec5SDimitry Andric 1152*0b57cec5SDimitry Andric /// Determine if the block is a suitable candidate to be predicated by the 1153*0b57cec5SDimitry Andric /// specified predicate. 1154*0b57cec5SDimitry Andric /// @param BBI BBInfo for the block to check 1155*0b57cec5SDimitry Andric /// @param Pred Predicate array for the branch that leads to BBI 1156*0b57cec5SDimitry Andric /// @param isTriangle true if the Analysis is for a triangle 1157*0b57cec5SDimitry Andric /// @param RevBranch true if Reverse(Pred) leads to BBI (e.g. BBI is the false 1158*0b57cec5SDimitry Andric /// case 1159*0b57cec5SDimitry Andric /// @param hasCommonTail true if BBI shares a tail with a sibling block that 1160*0b57cec5SDimitry Andric /// contains any instruction that would make the block unpredicable. 1161*0b57cec5SDimitry Andric bool IfConverter::FeasibilityAnalysis(BBInfo &BBI, 1162*0b57cec5SDimitry Andric SmallVectorImpl<MachineOperand> &Pred, 1163*0b57cec5SDimitry Andric bool isTriangle, bool RevBranch, 1164*0b57cec5SDimitry Andric bool hasCommonTail) { 1165*0b57cec5SDimitry Andric // If the block is dead or unpredicable, then it cannot be predicated. 1166*0b57cec5SDimitry Andric // Two blocks may share a common unpredicable tail, but this doesn't prevent 1167*0b57cec5SDimitry Andric // them from being if-converted. The non-shared portion is assumed to have 1168*0b57cec5SDimitry Andric // been checked 1169*0b57cec5SDimitry Andric if (BBI.IsDone || (BBI.IsUnpredicable && !hasCommonTail)) 1170*0b57cec5SDimitry Andric return false; 1171*0b57cec5SDimitry Andric 1172*0b57cec5SDimitry Andric // If it is already predicated but we couldn't analyze its terminator, the 1173*0b57cec5SDimitry Andric // latter might fallthrough, but we can't determine where to. 1174*0b57cec5SDimitry Andric // Conservatively avoid if-converting again. 1175*0b57cec5SDimitry Andric if (BBI.Predicate.size() && !BBI.IsBrAnalyzable) 1176*0b57cec5SDimitry Andric return false; 1177*0b57cec5SDimitry Andric 1178*0b57cec5SDimitry Andric // If it is already predicated, check if the new predicate subsumes 1179*0b57cec5SDimitry Andric // its predicate. 1180*0b57cec5SDimitry Andric if (BBI.Predicate.size() && !TII->SubsumesPredicate(Pred, BBI.Predicate)) 1181*0b57cec5SDimitry Andric return false; 1182*0b57cec5SDimitry Andric 1183*0b57cec5SDimitry Andric if (!hasCommonTail && BBI.BrCond.size()) { 1184*0b57cec5SDimitry Andric if (!isTriangle) 1185*0b57cec5SDimitry Andric return false; 1186*0b57cec5SDimitry Andric 1187*0b57cec5SDimitry Andric // Test predicate subsumption. 1188*0b57cec5SDimitry Andric SmallVector<MachineOperand, 4> RevPred(Pred.begin(), Pred.end()); 1189*0b57cec5SDimitry Andric SmallVector<MachineOperand, 4> Cond(BBI.BrCond.begin(), BBI.BrCond.end()); 1190*0b57cec5SDimitry Andric if (RevBranch) { 1191*0b57cec5SDimitry Andric if (TII->reverseBranchCondition(Cond)) 1192*0b57cec5SDimitry Andric return false; 1193*0b57cec5SDimitry Andric } 1194*0b57cec5SDimitry Andric if (TII->reverseBranchCondition(RevPred) || 1195*0b57cec5SDimitry Andric !TII->SubsumesPredicate(Cond, RevPred)) 1196*0b57cec5SDimitry Andric return false; 1197*0b57cec5SDimitry Andric } 1198*0b57cec5SDimitry Andric 1199*0b57cec5SDimitry Andric return true; 1200*0b57cec5SDimitry Andric } 1201*0b57cec5SDimitry Andric 1202*0b57cec5SDimitry Andric /// Analyze the structure of the sub-CFG starting from the specified block. 1203*0b57cec5SDimitry Andric /// Record its successors and whether it looks like an if-conversion candidate. 1204*0b57cec5SDimitry Andric void IfConverter::AnalyzeBlock( 1205*0b57cec5SDimitry Andric MachineBasicBlock &MBB, std::vector<std::unique_ptr<IfcvtToken>> &Tokens) { 1206*0b57cec5SDimitry Andric struct BBState { 1207*0b57cec5SDimitry Andric BBState(MachineBasicBlock &MBB) : MBB(&MBB), SuccsAnalyzed(false) {} 1208*0b57cec5SDimitry Andric MachineBasicBlock *MBB; 1209*0b57cec5SDimitry Andric 1210*0b57cec5SDimitry Andric /// This flag is true if MBB's successors have been analyzed. 1211*0b57cec5SDimitry Andric bool SuccsAnalyzed; 1212*0b57cec5SDimitry Andric }; 1213*0b57cec5SDimitry Andric 1214*0b57cec5SDimitry Andric // Push MBB to the stack. 1215*0b57cec5SDimitry Andric SmallVector<BBState, 16> BBStack(1, MBB); 1216*0b57cec5SDimitry Andric 1217*0b57cec5SDimitry Andric while (!BBStack.empty()) { 1218*0b57cec5SDimitry Andric BBState &State = BBStack.back(); 1219*0b57cec5SDimitry Andric MachineBasicBlock *BB = State.MBB; 1220*0b57cec5SDimitry Andric BBInfo &BBI = BBAnalysis[BB->getNumber()]; 1221*0b57cec5SDimitry Andric 1222*0b57cec5SDimitry Andric if (!State.SuccsAnalyzed) { 1223*0b57cec5SDimitry Andric if (BBI.IsAnalyzed || BBI.IsBeingAnalyzed) { 1224*0b57cec5SDimitry Andric BBStack.pop_back(); 1225*0b57cec5SDimitry Andric continue; 1226*0b57cec5SDimitry Andric } 1227*0b57cec5SDimitry Andric 1228*0b57cec5SDimitry Andric BBI.BB = BB; 1229*0b57cec5SDimitry Andric BBI.IsBeingAnalyzed = true; 1230*0b57cec5SDimitry Andric 1231*0b57cec5SDimitry Andric AnalyzeBranches(BBI); 1232*0b57cec5SDimitry Andric MachineBasicBlock::iterator Begin = BBI.BB->begin(); 1233*0b57cec5SDimitry Andric MachineBasicBlock::iterator End = BBI.BB->end(); 1234*0b57cec5SDimitry Andric ScanInstructions(BBI, Begin, End); 1235*0b57cec5SDimitry Andric 1236*0b57cec5SDimitry Andric // Unanalyzable or ends with fallthrough or unconditional branch, or if is 1237*0b57cec5SDimitry Andric // not considered for ifcvt anymore. 1238*0b57cec5SDimitry Andric if (!BBI.IsBrAnalyzable || BBI.BrCond.empty() || BBI.IsDone) { 1239*0b57cec5SDimitry Andric BBI.IsBeingAnalyzed = false; 1240*0b57cec5SDimitry Andric BBI.IsAnalyzed = true; 1241*0b57cec5SDimitry Andric BBStack.pop_back(); 1242*0b57cec5SDimitry Andric continue; 1243*0b57cec5SDimitry Andric } 1244*0b57cec5SDimitry Andric 1245*0b57cec5SDimitry Andric // Do not ifcvt if either path is a back edge to the entry block. 1246*0b57cec5SDimitry Andric if (BBI.TrueBB == BB || BBI.FalseBB == BB) { 1247*0b57cec5SDimitry Andric BBI.IsBeingAnalyzed = false; 1248*0b57cec5SDimitry Andric BBI.IsAnalyzed = true; 1249*0b57cec5SDimitry Andric BBStack.pop_back(); 1250*0b57cec5SDimitry Andric continue; 1251*0b57cec5SDimitry Andric } 1252*0b57cec5SDimitry Andric 1253*0b57cec5SDimitry Andric // Do not ifcvt if true and false fallthrough blocks are the same. 1254*0b57cec5SDimitry Andric if (!BBI.FalseBB) { 1255*0b57cec5SDimitry Andric BBI.IsBeingAnalyzed = false; 1256*0b57cec5SDimitry Andric BBI.IsAnalyzed = true; 1257*0b57cec5SDimitry Andric BBStack.pop_back(); 1258*0b57cec5SDimitry Andric continue; 1259*0b57cec5SDimitry Andric } 1260*0b57cec5SDimitry Andric 1261*0b57cec5SDimitry Andric // Push the False and True blocks to the stack. 1262*0b57cec5SDimitry Andric State.SuccsAnalyzed = true; 1263*0b57cec5SDimitry Andric BBStack.push_back(*BBI.FalseBB); 1264*0b57cec5SDimitry Andric BBStack.push_back(*BBI.TrueBB); 1265*0b57cec5SDimitry Andric continue; 1266*0b57cec5SDimitry Andric } 1267*0b57cec5SDimitry Andric 1268*0b57cec5SDimitry Andric BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()]; 1269*0b57cec5SDimitry Andric BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()]; 1270*0b57cec5SDimitry Andric 1271*0b57cec5SDimitry Andric if (TrueBBI.IsDone && FalseBBI.IsDone) { 1272*0b57cec5SDimitry Andric BBI.IsBeingAnalyzed = false; 1273*0b57cec5SDimitry Andric BBI.IsAnalyzed = true; 1274*0b57cec5SDimitry Andric BBStack.pop_back(); 1275*0b57cec5SDimitry Andric continue; 1276*0b57cec5SDimitry Andric } 1277*0b57cec5SDimitry Andric 1278*0b57cec5SDimitry Andric SmallVector<MachineOperand, 4> 1279*0b57cec5SDimitry Andric RevCond(BBI.BrCond.begin(), BBI.BrCond.end()); 1280*0b57cec5SDimitry Andric bool CanRevCond = !TII->reverseBranchCondition(RevCond); 1281*0b57cec5SDimitry Andric 1282*0b57cec5SDimitry Andric unsigned Dups = 0; 1283*0b57cec5SDimitry Andric unsigned Dups2 = 0; 1284*0b57cec5SDimitry Andric bool TNeedSub = !TrueBBI.Predicate.empty(); 1285*0b57cec5SDimitry Andric bool FNeedSub = !FalseBBI.Predicate.empty(); 1286*0b57cec5SDimitry Andric bool Enqueued = false; 1287*0b57cec5SDimitry Andric 1288*0b57cec5SDimitry Andric BranchProbability Prediction = MBPI->getEdgeProbability(BB, TrueBBI.BB); 1289*0b57cec5SDimitry Andric 1290*0b57cec5SDimitry Andric if (CanRevCond) { 1291*0b57cec5SDimitry Andric BBInfo TrueBBICalc, FalseBBICalc; 12928bcb0991SDimitry Andric auto feasibleDiamond = [&](bool Forked) { 12938bcb0991SDimitry Andric bool MeetsSize = MeetIfcvtSizeLimit(TrueBBICalc, FalseBBICalc, *BB, 12948bcb0991SDimitry Andric Dups + Dups2, Prediction, Forked); 1295*0b57cec5SDimitry Andric bool TrueFeasible = FeasibilityAnalysis(TrueBBI, BBI.BrCond, 1296*0b57cec5SDimitry Andric /* IsTriangle */ false, /* RevCond */ false, 1297*0b57cec5SDimitry Andric /* hasCommonTail */ true); 1298*0b57cec5SDimitry Andric bool FalseFeasible = FeasibilityAnalysis(FalseBBI, RevCond, 1299*0b57cec5SDimitry Andric /* IsTriangle */ false, /* RevCond */ false, 1300*0b57cec5SDimitry Andric /* hasCommonTail */ true); 1301*0b57cec5SDimitry Andric return MeetsSize && TrueFeasible && FalseFeasible; 1302*0b57cec5SDimitry Andric }; 1303*0b57cec5SDimitry Andric 1304*0b57cec5SDimitry Andric if (ValidDiamond(TrueBBI, FalseBBI, Dups, Dups2, 1305*0b57cec5SDimitry Andric TrueBBICalc, FalseBBICalc)) { 13068bcb0991SDimitry Andric if (feasibleDiamond(false)) { 1307*0b57cec5SDimitry Andric // Diamond: 1308*0b57cec5SDimitry Andric // EBB 1309*0b57cec5SDimitry Andric // / \_ 1310*0b57cec5SDimitry Andric // | | 1311*0b57cec5SDimitry Andric // TBB FBB 1312*0b57cec5SDimitry Andric // \ / 1313*0b57cec5SDimitry Andric // TailBB 1314*0b57cec5SDimitry Andric // Note TailBB can be empty. 13158bcb0991SDimitry Andric Tokens.push_back(std::make_unique<IfcvtToken>( 1316*0b57cec5SDimitry Andric BBI, ICDiamond, TNeedSub | FNeedSub, Dups, Dups2, 1317*0b57cec5SDimitry Andric (bool) TrueBBICalc.ClobbersPred, (bool) FalseBBICalc.ClobbersPred)); 1318*0b57cec5SDimitry Andric Enqueued = true; 1319*0b57cec5SDimitry Andric } 1320*0b57cec5SDimitry Andric } else if (ValidForkedDiamond(TrueBBI, FalseBBI, Dups, Dups2, 1321*0b57cec5SDimitry Andric TrueBBICalc, FalseBBICalc)) { 13228bcb0991SDimitry Andric if (feasibleDiamond(true)) { 1323*0b57cec5SDimitry Andric // ForkedDiamond: 1324*0b57cec5SDimitry Andric // if TBB and FBB have a common tail that includes their conditional 1325*0b57cec5SDimitry Andric // branch instructions, then we can If Convert this pattern. 1326*0b57cec5SDimitry Andric // EBB 1327*0b57cec5SDimitry Andric // _/ \_ 1328*0b57cec5SDimitry Andric // | | 1329*0b57cec5SDimitry Andric // TBB FBB 1330*0b57cec5SDimitry Andric // / \ / \ 1331*0b57cec5SDimitry Andric // FalseBB TrueBB FalseBB 1332*0b57cec5SDimitry Andric // 13338bcb0991SDimitry Andric Tokens.push_back(std::make_unique<IfcvtToken>( 1334*0b57cec5SDimitry Andric BBI, ICForkedDiamond, TNeedSub | FNeedSub, Dups, Dups2, 1335*0b57cec5SDimitry Andric (bool) TrueBBICalc.ClobbersPred, (bool) FalseBBICalc.ClobbersPred)); 1336*0b57cec5SDimitry Andric Enqueued = true; 1337*0b57cec5SDimitry Andric } 1338*0b57cec5SDimitry Andric } 1339*0b57cec5SDimitry Andric } 1340*0b57cec5SDimitry Andric 1341*0b57cec5SDimitry Andric if (ValidTriangle(TrueBBI, FalseBBI, false, Dups, Prediction) && 1342*0b57cec5SDimitry Andric MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize + TrueBBI.ExtraCost, 1343*0b57cec5SDimitry Andric TrueBBI.ExtraCost2, Prediction) && 1344*0b57cec5SDimitry Andric FeasibilityAnalysis(TrueBBI, BBI.BrCond, true)) { 1345*0b57cec5SDimitry Andric // Triangle: 1346*0b57cec5SDimitry Andric // EBB 1347*0b57cec5SDimitry Andric // | \_ 1348*0b57cec5SDimitry Andric // | | 1349*0b57cec5SDimitry Andric // | TBB 1350*0b57cec5SDimitry Andric // | / 1351*0b57cec5SDimitry Andric // FBB 1352*0b57cec5SDimitry Andric Tokens.push_back( 13538bcb0991SDimitry Andric std::make_unique<IfcvtToken>(BBI, ICTriangle, TNeedSub, Dups)); 1354*0b57cec5SDimitry Andric Enqueued = true; 1355*0b57cec5SDimitry Andric } 1356*0b57cec5SDimitry Andric 1357*0b57cec5SDimitry Andric if (ValidTriangle(TrueBBI, FalseBBI, true, Dups, Prediction) && 1358*0b57cec5SDimitry Andric MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize + TrueBBI.ExtraCost, 1359*0b57cec5SDimitry Andric TrueBBI.ExtraCost2, Prediction) && 1360*0b57cec5SDimitry Andric FeasibilityAnalysis(TrueBBI, BBI.BrCond, true, true)) { 1361*0b57cec5SDimitry Andric Tokens.push_back( 13628bcb0991SDimitry Andric std::make_unique<IfcvtToken>(BBI, ICTriangleRev, TNeedSub, Dups)); 1363*0b57cec5SDimitry Andric Enqueued = true; 1364*0b57cec5SDimitry Andric } 1365*0b57cec5SDimitry Andric 1366*0b57cec5SDimitry Andric if (ValidSimple(TrueBBI, Dups, Prediction) && 1367*0b57cec5SDimitry Andric MeetIfcvtSizeLimit(*TrueBBI.BB, TrueBBI.NonPredSize + TrueBBI.ExtraCost, 1368*0b57cec5SDimitry Andric TrueBBI.ExtraCost2, Prediction) && 1369*0b57cec5SDimitry Andric FeasibilityAnalysis(TrueBBI, BBI.BrCond)) { 1370*0b57cec5SDimitry Andric // Simple (split, no rejoin): 1371*0b57cec5SDimitry Andric // EBB 1372*0b57cec5SDimitry Andric // | \_ 1373*0b57cec5SDimitry Andric // | | 1374*0b57cec5SDimitry Andric // | TBB---> exit 1375*0b57cec5SDimitry Andric // | 1376*0b57cec5SDimitry Andric // FBB 1377*0b57cec5SDimitry Andric Tokens.push_back( 13788bcb0991SDimitry Andric std::make_unique<IfcvtToken>(BBI, ICSimple, TNeedSub, Dups)); 1379*0b57cec5SDimitry Andric Enqueued = true; 1380*0b57cec5SDimitry Andric } 1381*0b57cec5SDimitry Andric 1382*0b57cec5SDimitry Andric if (CanRevCond) { 1383*0b57cec5SDimitry Andric // Try the other path... 1384*0b57cec5SDimitry Andric if (ValidTriangle(FalseBBI, TrueBBI, false, Dups, 1385*0b57cec5SDimitry Andric Prediction.getCompl()) && 1386*0b57cec5SDimitry Andric MeetIfcvtSizeLimit(*FalseBBI.BB, 1387*0b57cec5SDimitry Andric FalseBBI.NonPredSize + FalseBBI.ExtraCost, 1388*0b57cec5SDimitry Andric FalseBBI.ExtraCost2, Prediction.getCompl()) && 1389*0b57cec5SDimitry Andric FeasibilityAnalysis(FalseBBI, RevCond, true)) { 13908bcb0991SDimitry Andric Tokens.push_back(std::make_unique<IfcvtToken>(BBI, ICTriangleFalse, 1391*0b57cec5SDimitry Andric FNeedSub, Dups)); 1392*0b57cec5SDimitry Andric Enqueued = true; 1393*0b57cec5SDimitry Andric } 1394*0b57cec5SDimitry Andric 1395*0b57cec5SDimitry Andric if (ValidTriangle(FalseBBI, TrueBBI, true, Dups, 1396*0b57cec5SDimitry Andric Prediction.getCompl()) && 1397*0b57cec5SDimitry Andric MeetIfcvtSizeLimit(*FalseBBI.BB, 1398*0b57cec5SDimitry Andric FalseBBI.NonPredSize + FalseBBI.ExtraCost, 1399*0b57cec5SDimitry Andric FalseBBI.ExtraCost2, Prediction.getCompl()) && 1400*0b57cec5SDimitry Andric FeasibilityAnalysis(FalseBBI, RevCond, true, true)) { 1401*0b57cec5SDimitry Andric Tokens.push_back( 14028bcb0991SDimitry Andric std::make_unique<IfcvtToken>(BBI, ICTriangleFRev, FNeedSub, Dups)); 1403*0b57cec5SDimitry Andric Enqueued = true; 1404*0b57cec5SDimitry Andric } 1405*0b57cec5SDimitry Andric 1406*0b57cec5SDimitry Andric if (ValidSimple(FalseBBI, Dups, Prediction.getCompl()) && 1407*0b57cec5SDimitry Andric MeetIfcvtSizeLimit(*FalseBBI.BB, 1408*0b57cec5SDimitry Andric FalseBBI.NonPredSize + FalseBBI.ExtraCost, 1409*0b57cec5SDimitry Andric FalseBBI.ExtraCost2, Prediction.getCompl()) && 1410*0b57cec5SDimitry Andric FeasibilityAnalysis(FalseBBI, RevCond)) { 1411*0b57cec5SDimitry Andric Tokens.push_back( 14128bcb0991SDimitry Andric std::make_unique<IfcvtToken>(BBI, ICSimpleFalse, FNeedSub, Dups)); 1413*0b57cec5SDimitry Andric Enqueued = true; 1414*0b57cec5SDimitry Andric } 1415*0b57cec5SDimitry Andric } 1416*0b57cec5SDimitry Andric 1417*0b57cec5SDimitry Andric BBI.IsEnqueued = Enqueued; 1418*0b57cec5SDimitry Andric BBI.IsBeingAnalyzed = false; 1419*0b57cec5SDimitry Andric BBI.IsAnalyzed = true; 1420*0b57cec5SDimitry Andric BBStack.pop_back(); 1421*0b57cec5SDimitry Andric } 1422*0b57cec5SDimitry Andric } 1423*0b57cec5SDimitry Andric 1424*0b57cec5SDimitry Andric /// Analyze all blocks and find entries for all if-conversion candidates. 1425*0b57cec5SDimitry Andric void IfConverter::AnalyzeBlocks( 1426*0b57cec5SDimitry Andric MachineFunction &MF, std::vector<std::unique_ptr<IfcvtToken>> &Tokens) { 1427*0b57cec5SDimitry Andric for (MachineBasicBlock &MBB : MF) 1428*0b57cec5SDimitry Andric AnalyzeBlock(MBB, Tokens); 1429*0b57cec5SDimitry Andric 1430*0b57cec5SDimitry Andric // Sort to favor more complex ifcvt scheme. 1431*0b57cec5SDimitry Andric llvm::stable_sort(Tokens, IfcvtTokenCmp); 1432*0b57cec5SDimitry Andric } 1433*0b57cec5SDimitry Andric 1434*0b57cec5SDimitry Andric /// Returns true either if ToMBB is the next block after MBB or that all the 1435*0b57cec5SDimitry Andric /// intervening blocks are empty (given MBB can fall through to its next block). 1436*0b57cec5SDimitry Andric static bool canFallThroughTo(MachineBasicBlock &MBB, MachineBasicBlock &ToMBB) { 1437*0b57cec5SDimitry Andric MachineFunction::iterator PI = MBB.getIterator(); 1438*0b57cec5SDimitry Andric MachineFunction::iterator I = std::next(PI); 1439*0b57cec5SDimitry Andric MachineFunction::iterator TI = ToMBB.getIterator(); 1440*0b57cec5SDimitry Andric MachineFunction::iterator E = MBB.getParent()->end(); 1441*0b57cec5SDimitry Andric while (I != TI) { 1442*0b57cec5SDimitry Andric // Check isSuccessor to avoid case where the next block is empty, but 1443*0b57cec5SDimitry Andric // it's not a successor. 1444*0b57cec5SDimitry Andric if (I == E || !I->empty() || !PI->isSuccessor(&*I)) 1445*0b57cec5SDimitry Andric return false; 1446*0b57cec5SDimitry Andric PI = I++; 1447*0b57cec5SDimitry Andric } 1448*0b57cec5SDimitry Andric // Finally see if the last I is indeed a successor to PI. 1449*0b57cec5SDimitry Andric return PI->isSuccessor(&*I); 1450*0b57cec5SDimitry Andric } 1451*0b57cec5SDimitry Andric 1452*0b57cec5SDimitry Andric /// Invalidate predecessor BB info so it would be re-analyzed to determine if it 1453*0b57cec5SDimitry Andric /// can be if-converted. If predecessor is already enqueued, dequeue it! 1454*0b57cec5SDimitry Andric void IfConverter::InvalidatePreds(MachineBasicBlock &MBB) { 1455*0b57cec5SDimitry Andric for (const MachineBasicBlock *Predecessor : MBB.predecessors()) { 1456*0b57cec5SDimitry Andric BBInfo &PBBI = BBAnalysis[Predecessor->getNumber()]; 1457*0b57cec5SDimitry Andric if (PBBI.IsDone || PBBI.BB == &MBB) 1458*0b57cec5SDimitry Andric continue; 1459*0b57cec5SDimitry Andric PBBI.IsAnalyzed = false; 1460*0b57cec5SDimitry Andric PBBI.IsEnqueued = false; 1461*0b57cec5SDimitry Andric } 1462*0b57cec5SDimitry Andric } 1463*0b57cec5SDimitry Andric 1464*0b57cec5SDimitry Andric /// Inserts an unconditional branch from \p MBB to \p ToMBB. 1465*0b57cec5SDimitry Andric static void InsertUncondBranch(MachineBasicBlock &MBB, MachineBasicBlock &ToMBB, 1466*0b57cec5SDimitry Andric const TargetInstrInfo *TII) { 1467*0b57cec5SDimitry Andric DebugLoc dl; // FIXME: this is nowhere 1468*0b57cec5SDimitry Andric SmallVector<MachineOperand, 0> NoCond; 1469*0b57cec5SDimitry Andric TII->insertBranch(MBB, &ToMBB, nullptr, NoCond, dl); 1470*0b57cec5SDimitry Andric } 1471*0b57cec5SDimitry Andric 1472*0b57cec5SDimitry Andric /// Behaves like LiveRegUnits::StepForward() but also adds implicit uses to all 1473*0b57cec5SDimitry Andric /// values defined in MI which are also live/used by MI. 1474*0b57cec5SDimitry Andric static void UpdatePredRedefs(MachineInstr &MI, LivePhysRegs &Redefs) { 1475*0b57cec5SDimitry Andric const TargetRegisterInfo *TRI = MI.getMF()->getSubtarget().getRegisterInfo(); 1476*0b57cec5SDimitry Andric 1477*0b57cec5SDimitry Andric // Before stepping forward past MI, remember which regs were live 1478*0b57cec5SDimitry Andric // before MI. This is needed to set the Undef flag only when reg is 1479*0b57cec5SDimitry Andric // dead. 1480*0b57cec5SDimitry Andric SparseSet<MCPhysReg, identity<MCPhysReg>> LiveBeforeMI; 1481*0b57cec5SDimitry Andric LiveBeforeMI.setUniverse(TRI->getNumRegs()); 1482*0b57cec5SDimitry Andric for (unsigned Reg : Redefs) 1483*0b57cec5SDimitry Andric LiveBeforeMI.insert(Reg); 1484*0b57cec5SDimitry Andric 1485*0b57cec5SDimitry Andric SmallVector<std::pair<MCPhysReg, const MachineOperand*>, 4> Clobbers; 1486*0b57cec5SDimitry Andric Redefs.stepForward(MI, Clobbers); 1487*0b57cec5SDimitry Andric 1488*0b57cec5SDimitry Andric // Now add the implicit uses for each of the clobbered values. 1489*0b57cec5SDimitry Andric for (auto Clobber : Clobbers) { 1490*0b57cec5SDimitry Andric // FIXME: Const cast here is nasty, but better than making StepForward 1491*0b57cec5SDimitry Andric // take a mutable instruction instead of const. 1492*0b57cec5SDimitry Andric unsigned Reg = Clobber.first; 1493*0b57cec5SDimitry Andric MachineOperand &Op = const_cast<MachineOperand&>(*Clobber.second); 1494*0b57cec5SDimitry Andric MachineInstr *OpMI = Op.getParent(); 1495*0b57cec5SDimitry Andric MachineInstrBuilder MIB(*OpMI->getMF(), OpMI); 1496*0b57cec5SDimitry Andric if (Op.isRegMask()) { 1497*0b57cec5SDimitry Andric // First handle regmasks. They clobber any entries in the mask which 1498*0b57cec5SDimitry Andric // means that we need a def for those registers. 1499*0b57cec5SDimitry Andric if (LiveBeforeMI.count(Reg)) 1500*0b57cec5SDimitry Andric MIB.addReg(Reg, RegState::Implicit); 1501*0b57cec5SDimitry Andric 1502*0b57cec5SDimitry Andric // We also need to add an implicit def of this register for the later 1503*0b57cec5SDimitry Andric // use to read from. 1504*0b57cec5SDimitry Andric // For the register allocator to have allocated a register clobbered 1505*0b57cec5SDimitry Andric // by the call which is used later, it must be the case that 1506*0b57cec5SDimitry Andric // the call doesn't return. 1507*0b57cec5SDimitry Andric MIB.addReg(Reg, RegState::Implicit | RegState::Define); 1508*0b57cec5SDimitry Andric continue; 1509*0b57cec5SDimitry Andric } 1510*0b57cec5SDimitry Andric if (LiveBeforeMI.count(Reg)) 1511*0b57cec5SDimitry Andric MIB.addReg(Reg, RegState::Implicit); 1512*0b57cec5SDimitry Andric else { 1513*0b57cec5SDimitry Andric bool HasLiveSubReg = false; 1514*0b57cec5SDimitry Andric for (MCSubRegIterator S(Reg, TRI); S.isValid(); ++S) { 1515*0b57cec5SDimitry Andric if (!LiveBeforeMI.count(*S)) 1516*0b57cec5SDimitry Andric continue; 1517*0b57cec5SDimitry Andric HasLiveSubReg = true; 1518*0b57cec5SDimitry Andric break; 1519*0b57cec5SDimitry Andric } 1520*0b57cec5SDimitry Andric if (HasLiveSubReg) 1521*0b57cec5SDimitry Andric MIB.addReg(Reg, RegState::Implicit); 1522*0b57cec5SDimitry Andric } 1523*0b57cec5SDimitry Andric } 1524*0b57cec5SDimitry Andric } 1525*0b57cec5SDimitry Andric 1526*0b57cec5SDimitry Andric /// If convert a simple (split, no rejoin) sub-CFG. 1527*0b57cec5SDimitry Andric bool IfConverter::IfConvertSimple(BBInfo &BBI, IfcvtKind Kind) { 1528*0b57cec5SDimitry Andric BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()]; 1529*0b57cec5SDimitry Andric BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()]; 1530*0b57cec5SDimitry Andric BBInfo *CvtBBI = &TrueBBI; 1531*0b57cec5SDimitry Andric BBInfo *NextBBI = &FalseBBI; 1532*0b57cec5SDimitry Andric 1533*0b57cec5SDimitry Andric SmallVector<MachineOperand, 4> Cond(BBI.BrCond.begin(), BBI.BrCond.end()); 1534*0b57cec5SDimitry Andric if (Kind == ICSimpleFalse) 1535*0b57cec5SDimitry Andric std::swap(CvtBBI, NextBBI); 1536*0b57cec5SDimitry Andric 1537*0b57cec5SDimitry Andric MachineBasicBlock &CvtMBB = *CvtBBI->BB; 1538*0b57cec5SDimitry Andric MachineBasicBlock &NextMBB = *NextBBI->BB; 1539*0b57cec5SDimitry Andric if (CvtBBI->IsDone || 1540*0b57cec5SDimitry Andric (CvtBBI->CannotBeCopied && CvtMBB.pred_size() > 1)) { 1541*0b57cec5SDimitry Andric // Something has changed. It's no longer safe to predicate this block. 1542*0b57cec5SDimitry Andric BBI.IsAnalyzed = false; 1543*0b57cec5SDimitry Andric CvtBBI->IsAnalyzed = false; 1544*0b57cec5SDimitry Andric return false; 1545*0b57cec5SDimitry Andric } 1546*0b57cec5SDimitry Andric 1547*0b57cec5SDimitry Andric if (CvtMBB.hasAddressTaken()) 1548*0b57cec5SDimitry Andric // Conservatively abort if-conversion if BB's address is taken. 1549*0b57cec5SDimitry Andric return false; 1550*0b57cec5SDimitry Andric 1551*0b57cec5SDimitry Andric if (Kind == ICSimpleFalse) 1552*0b57cec5SDimitry Andric if (TII->reverseBranchCondition(Cond)) 1553*0b57cec5SDimitry Andric llvm_unreachable("Unable to reverse branch condition!"); 1554*0b57cec5SDimitry Andric 1555*0b57cec5SDimitry Andric Redefs.init(*TRI); 1556*0b57cec5SDimitry Andric 1557*0b57cec5SDimitry Andric if (MRI->tracksLiveness()) { 1558*0b57cec5SDimitry Andric // Initialize liveins to the first BB. These are potentially redefined by 1559*0b57cec5SDimitry Andric // predicated instructions. 1560*0b57cec5SDimitry Andric Redefs.addLiveIns(CvtMBB); 1561*0b57cec5SDimitry Andric Redefs.addLiveIns(NextMBB); 1562*0b57cec5SDimitry Andric } 1563*0b57cec5SDimitry Andric 1564*0b57cec5SDimitry Andric // Remove the branches from the entry so we can add the contents of the true 1565*0b57cec5SDimitry Andric // block to it. 1566*0b57cec5SDimitry Andric BBI.NonPredSize -= TII->removeBranch(*BBI.BB); 1567*0b57cec5SDimitry Andric 1568*0b57cec5SDimitry Andric if (CvtMBB.pred_size() > 1) { 1569*0b57cec5SDimitry Andric // Copy instructions in the true block, predicate them, and add them to 1570*0b57cec5SDimitry Andric // the entry block. 1571*0b57cec5SDimitry Andric CopyAndPredicateBlock(BBI, *CvtBBI, Cond); 1572*0b57cec5SDimitry Andric 1573*0b57cec5SDimitry Andric // Keep the CFG updated. 1574*0b57cec5SDimitry Andric BBI.BB->removeSuccessor(&CvtMBB, true); 1575*0b57cec5SDimitry Andric } else { 1576*0b57cec5SDimitry Andric // Predicate the instructions in the true block. 1577*0b57cec5SDimitry Andric PredicateBlock(*CvtBBI, CvtMBB.end(), Cond); 1578*0b57cec5SDimitry Andric 1579*0b57cec5SDimitry Andric // Merge converted block into entry block. The BB to Cvt edge is removed 1580*0b57cec5SDimitry Andric // by MergeBlocks. 1581*0b57cec5SDimitry Andric MergeBlocks(BBI, *CvtBBI); 1582*0b57cec5SDimitry Andric } 1583*0b57cec5SDimitry Andric 1584*0b57cec5SDimitry Andric bool IterIfcvt = true; 1585*0b57cec5SDimitry Andric if (!canFallThroughTo(*BBI.BB, NextMBB)) { 1586*0b57cec5SDimitry Andric InsertUncondBranch(*BBI.BB, NextMBB, TII); 1587*0b57cec5SDimitry Andric BBI.HasFallThrough = false; 1588*0b57cec5SDimitry Andric // Now ifcvt'd block will look like this: 1589*0b57cec5SDimitry Andric // BB: 1590*0b57cec5SDimitry Andric // ... 1591*0b57cec5SDimitry Andric // t, f = cmp 1592*0b57cec5SDimitry Andric // if t op 1593*0b57cec5SDimitry Andric // b BBf 1594*0b57cec5SDimitry Andric // 1595*0b57cec5SDimitry Andric // We cannot further ifcvt this block because the unconditional branch 1596*0b57cec5SDimitry Andric // will have to be predicated on the new condition, that will not be 1597*0b57cec5SDimitry Andric // available if cmp executes. 1598*0b57cec5SDimitry Andric IterIfcvt = false; 1599*0b57cec5SDimitry Andric } 1600*0b57cec5SDimitry Andric 1601*0b57cec5SDimitry Andric // Update block info. BB can be iteratively if-converted. 1602*0b57cec5SDimitry Andric if (!IterIfcvt) 1603*0b57cec5SDimitry Andric BBI.IsDone = true; 1604*0b57cec5SDimitry Andric InvalidatePreds(*BBI.BB); 1605*0b57cec5SDimitry Andric CvtBBI->IsDone = true; 1606*0b57cec5SDimitry Andric 1607*0b57cec5SDimitry Andric // FIXME: Must maintain LiveIns. 1608*0b57cec5SDimitry Andric return true; 1609*0b57cec5SDimitry Andric } 1610*0b57cec5SDimitry Andric 1611*0b57cec5SDimitry Andric /// If convert a triangle sub-CFG. 1612*0b57cec5SDimitry Andric bool IfConverter::IfConvertTriangle(BBInfo &BBI, IfcvtKind Kind) { 1613*0b57cec5SDimitry Andric BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()]; 1614*0b57cec5SDimitry Andric BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()]; 1615*0b57cec5SDimitry Andric BBInfo *CvtBBI = &TrueBBI; 1616*0b57cec5SDimitry Andric BBInfo *NextBBI = &FalseBBI; 1617*0b57cec5SDimitry Andric DebugLoc dl; // FIXME: this is nowhere 1618*0b57cec5SDimitry Andric 1619*0b57cec5SDimitry Andric SmallVector<MachineOperand, 4> Cond(BBI.BrCond.begin(), BBI.BrCond.end()); 1620*0b57cec5SDimitry Andric if (Kind == ICTriangleFalse || Kind == ICTriangleFRev) 1621*0b57cec5SDimitry Andric std::swap(CvtBBI, NextBBI); 1622*0b57cec5SDimitry Andric 1623*0b57cec5SDimitry Andric MachineBasicBlock &CvtMBB = *CvtBBI->BB; 1624*0b57cec5SDimitry Andric MachineBasicBlock &NextMBB = *NextBBI->BB; 1625*0b57cec5SDimitry Andric if (CvtBBI->IsDone || 1626*0b57cec5SDimitry Andric (CvtBBI->CannotBeCopied && CvtMBB.pred_size() > 1)) { 1627*0b57cec5SDimitry Andric // Something has changed. It's no longer safe to predicate this block. 1628*0b57cec5SDimitry Andric BBI.IsAnalyzed = false; 1629*0b57cec5SDimitry Andric CvtBBI->IsAnalyzed = false; 1630*0b57cec5SDimitry Andric return false; 1631*0b57cec5SDimitry Andric } 1632*0b57cec5SDimitry Andric 1633*0b57cec5SDimitry Andric if (CvtMBB.hasAddressTaken()) 1634*0b57cec5SDimitry Andric // Conservatively abort if-conversion if BB's address is taken. 1635*0b57cec5SDimitry Andric return false; 1636*0b57cec5SDimitry Andric 1637*0b57cec5SDimitry Andric if (Kind == ICTriangleFalse || Kind == ICTriangleFRev) 1638*0b57cec5SDimitry Andric if (TII->reverseBranchCondition(Cond)) 1639*0b57cec5SDimitry Andric llvm_unreachable("Unable to reverse branch condition!"); 1640*0b57cec5SDimitry Andric 1641*0b57cec5SDimitry Andric if (Kind == ICTriangleRev || Kind == ICTriangleFRev) { 1642*0b57cec5SDimitry Andric if (reverseBranchCondition(*CvtBBI)) { 1643*0b57cec5SDimitry Andric // BB has been changed, modify its predecessors (except for this 1644*0b57cec5SDimitry Andric // one) so they don't get ifcvt'ed based on bad intel. 1645*0b57cec5SDimitry Andric for (MachineBasicBlock *PBB : CvtMBB.predecessors()) { 1646*0b57cec5SDimitry Andric if (PBB == BBI.BB) 1647*0b57cec5SDimitry Andric continue; 1648*0b57cec5SDimitry Andric BBInfo &PBBI = BBAnalysis[PBB->getNumber()]; 1649*0b57cec5SDimitry Andric if (PBBI.IsEnqueued) { 1650*0b57cec5SDimitry Andric PBBI.IsAnalyzed = false; 1651*0b57cec5SDimitry Andric PBBI.IsEnqueued = false; 1652*0b57cec5SDimitry Andric } 1653*0b57cec5SDimitry Andric } 1654*0b57cec5SDimitry Andric } 1655*0b57cec5SDimitry Andric } 1656*0b57cec5SDimitry Andric 1657*0b57cec5SDimitry Andric // Initialize liveins to the first BB. These are potentially redefined by 1658*0b57cec5SDimitry Andric // predicated instructions. 1659*0b57cec5SDimitry Andric Redefs.init(*TRI); 1660*0b57cec5SDimitry Andric if (MRI->tracksLiveness()) { 1661*0b57cec5SDimitry Andric Redefs.addLiveIns(CvtMBB); 1662*0b57cec5SDimitry Andric Redefs.addLiveIns(NextMBB); 1663*0b57cec5SDimitry Andric } 1664*0b57cec5SDimitry Andric 1665*0b57cec5SDimitry Andric bool HasEarlyExit = CvtBBI->FalseBB != nullptr; 1666*0b57cec5SDimitry Andric BranchProbability CvtNext, CvtFalse, BBNext, BBCvt; 1667*0b57cec5SDimitry Andric 1668*0b57cec5SDimitry Andric if (HasEarlyExit) { 1669*0b57cec5SDimitry Andric // Get probabilities before modifying CvtMBB and BBI.BB. 1670*0b57cec5SDimitry Andric CvtNext = MBPI->getEdgeProbability(&CvtMBB, &NextMBB); 1671*0b57cec5SDimitry Andric CvtFalse = MBPI->getEdgeProbability(&CvtMBB, CvtBBI->FalseBB); 1672*0b57cec5SDimitry Andric BBNext = MBPI->getEdgeProbability(BBI.BB, &NextMBB); 1673*0b57cec5SDimitry Andric BBCvt = MBPI->getEdgeProbability(BBI.BB, &CvtMBB); 1674*0b57cec5SDimitry Andric } 1675*0b57cec5SDimitry Andric 1676*0b57cec5SDimitry Andric // Remove the branches from the entry so we can add the contents of the true 1677*0b57cec5SDimitry Andric // block to it. 1678*0b57cec5SDimitry Andric BBI.NonPredSize -= TII->removeBranch(*BBI.BB); 1679*0b57cec5SDimitry Andric 1680*0b57cec5SDimitry Andric if (CvtMBB.pred_size() > 1) { 1681*0b57cec5SDimitry Andric // Copy instructions in the true block, predicate them, and add them to 1682*0b57cec5SDimitry Andric // the entry block. 1683*0b57cec5SDimitry Andric CopyAndPredicateBlock(BBI, *CvtBBI, Cond, true); 1684*0b57cec5SDimitry Andric } else { 1685*0b57cec5SDimitry Andric // Predicate the 'true' block after removing its branch. 1686*0b57cec5SDimitry Andric CvtBBI->NonPredSize -= TII->removeBranch(CvtMBB); 1687*0b57cec5SDimitry Andric PredicateBlock(*CvtBBI, CvtMBB.end(), Cond); 1688*0b57cec5SDimitry Andric 1689*0b57cec5SDimitry Andric // Now merge the entry of the triangle with the true block. 1690*0b57cec5SDimitry Andric MergeBlocks(BBI, *CvtBBI, false); 1691*0b57cec5SDimitry Andric } 1692*0b57cec5SDimitry Andric 1693*0b57cec5SDimitry Andric // Keep the CFG updated. 1694*0b57cec5SDimitry Andric BBI.BB->removeSuccessor(&CvtMBB, true); 1695*0b57cec5SDimitry Andric 1696*0b57cec5SDimitry Andric // If 'true' block has a 'false' successor, add an exit branch to it. 1697*0b57cec5SDimitry Andric if (HasEarlyExit) { 1698*0b57cec5SDimitry Andric SmallVector<MachineOperand, 4> RevCond(CvtBBI->BrCond.begin(), 1699*0b57cec5SDimitry Andric CvtBBI->BrCond.end()); 1700*0b57cec5SDimitry Andric if (TII->reverseBranchCondition(RevCond)) 1701*0b57cec5SDimitry Andric llvm_unreachable("Unable to reverse branch condition!"); 1702*0b57cec5SDimitry Andric 1703*0b57cec5SDimitry Andric // Update the edge probability for both CvtBBI->FalseBB and NextBBI. 1704*0b57cec5SDimitry Andric // NewNext = New_Prob(BBI.BB, NextMBB) = 1705*0b57cec5SDimitry Andric // Prob(BBI.BB, NextMBB) + 1706*0b57cec5SDimitry Andric // Prob(BBI.BB, CvtMBB) * Prob(CvtMBB, NextMBB) 1707*0b57cec5SDimitry Andric // NewFalse = New_Prob(BBI.BB, CvtBBI->FalseBB) = 1708*0b57cec5SDimitry Andric // Prob(BBI.BB, CvtMBB) * Prob(CvtMBB, CvtBBI->FalseBB) 1709*0b57cec5SDimitry Andric auto NewTrueBB = getNextBlock(*BBI.BB); 1710*0b57cec5SDimitry Andric auto NewNext = BBNext + BBCvt * CvtNext; 1711*0b57cec5SDimitry Andric auto NewTrueBBIter = find(BBI.BB->successors(), NewTrueBB); 1712*0b57cec5SDimitry Andric if (NewTrueBBIter != BBI.BB->succ_end()) 1713*0b57cec5SDimitry Andric BBI.BB->setSuccProbability(NewTrueBBIter, NewNext); 1714*0b57cec5SDimitry Andric 1715*0b57cec5SDimitry Andric auto NewFalse = BBCvt * CvtFalse; 1716*0b57cec5SDimitry Andric TII->insertBranch(*BBI.BB, CvtBBI->FalseBB, nullptr, RevCond, dl); 1717*0b57cec5SDimitry Andric BBI.BB->addSuccessor(CvtBBI->FalseBB, NewFalse); 1718*0b57cec5SDimitry Andric } 1719*0b57cec5SDimitry Andric 1720*0b57cec5SDimitry Andric // Merge in the 'false' block if the 'false' block has no other 1721*0b57cec5SDimitry Andric // predecessors. Otherwise, add an unconditional branch to 'false'. 1722*0b57cec5SDimitry Andric bool FalseBBDead = false; 1723*0b57cec5SDimitry Andric bool IterIfcvt = true; 1724*0b57cec5SDimitry Andric bool isFallThrough = canFallThroughTo(*BBI.BB, NextMBB); 1725*0b57cec5SDimitry Andric if (!isFallThrough) { 1726*0b57cec5SDimitry Andric // Only merge them if the true block does not fallthrough to the false 1727*0b57cec5SDimitry Andric // block. By not merging them, we make it possible to iteratively 1728*0b57cec5SDimitry Andric // ifcvt the blocks. 1729*0b57cec5SDimitry Andric if (!HasEarlyExit && 1730*0b57cec5SDimitry Andric NextMBB.pred_size() == 1 && !NextBBI->HasFallThrough && 1731*0b57cec5SDimitry Andric !NextMBB.hasAddressTaken()) { 1732*0b57cec5SDimitry Andric MergeBlocks(BBI, *NextBBI); 1733*0b57cec5SDimitry Andric FalseBBDead = true; 1734*0b57cec5SDimitry Andric } else { 1735*0b57cec5SDimitry Andric InsertUncondBranch(*BBI.BB, NextMBB, TII); 1736*0b57cec5SDimitry Andric BBI.HasFallThrough = false; 1737*0b57cec5SDimitry Andric } 1738*0b57cec5SDimitry Andric // Mixed predicated and unpredicated code. This cannot be iteratively 1739*0b57cec5SDimitry Andric // predicated. 1740*0b57cec5SDimitry Andric IterIfcvt = false; 1741*0b57cec5SDimitry Andric } 1742*0b57cec5SDimitry Andric 1743*0b57cec5SDimitry Andric // Update block info. BB can be iteratively if-converted. 1744*0b57cec5SDimitry Andric if (!IterIfcvt) 1745*0b57cec5SDimitry Andric BBI.IsDone = true; 1746*0b57cec5SDimitry Andric InvalidatePreds(*BBI.BB); 1747*0b57cec5SDimitry Andric CvtBBI->IsDone = true; 1748*0b57cec5SDimitry Andric if (FalseBBDead) 1749*0b57cec5SDimitry Andric NextBBI->IsDone = true; 1750*0b57cec5SDimitry Andric 1751*0b57cec5SDimitry Andric // FIXME: Must maintain LiveIns. 1752*0b57cec5SDimitry Andric return true; 1753*0b57cec5SDimitry Andric } 1754*0b57cec5SDimitry Andric 1755*0b57cec5SDimitry Andric /// Common code shared between diamond conversions. 1756*0b57cec5SDimitry Andric /// \p BBI, \p TrueBBI, and \p FalseBBI form the diamond shape. 1757*0b57cec5SDimitry Andric /// \p NumDups1 - number of shared instructions at the beginning of \p TrueBBI 1758*0b57cec5SDimitry Andric /// and FalseBBI 1759*0b57cec5SDimitry Andric /// \p NumDups2 - number of shared instructions at the end of \p TrueBBI 1760*0b57cec5SDimitry Andric /// and \p FalseBBI 1761*0b57cec5SDimitry Andric /// \p RemoveBranch - Remove the common branch of the two blocks before 1762*0b57cec5SDimitry Andric /// predicating. Only false for unanalyzable fallthrough 1763*0b57cec5SDimitry Andric /// cases. The caller will replace the branch if necessary. 1764*0b57cec5SDimitry Andric /// \p MergeAddEdges - Add successor edges when merging blocks. Only false for 1765*0b57cec5SDimitry Andric /// unanalyzable fallthrough 1766*0b57cec5SDimitry Andric bool IfConverter::IfConvertDiamondCommon( 1767*0b57cec5SDimitry Andric BBInfo &BBI, BBInfo &TrueBBI, BBInfo &FalseBBI, 1768*0b57cec5SDimitry Andric unsigned NumDups1, unsigned NumDups2, 1769*0b57cec5SDimitry Andric bool TClobbersPred, bool FClobbersPred, 1770*0b57cec5SDimitry Andric bool RemoveBranch, bool MergeAddEdges) { 1771*0b57cec5SDimitry Andric 1772*0b57cec5SDimitry Andric if (TrueBBI.IsDone || FalseBBI.IsDone || 1773*0b57cec5SDimitry Andric TrueBBI.BB->pred_size() > 1 || FalseBBI.BB->pred_size() > 1) { 1774*0b57cec5SDimitry Andric // Something has changed. It's no longer safe to predicate these blocks. 1775*0b57cec5SDimitry Andric BBI.IsAnalyzed = false; 1776*0b57cec5SDimitry Andric TrueBBI.IsAnalyzed = false; 1777*0b57cec5SDimitry Andric FalseBBI.IsAnalyzed = false; 1778*0b57cec5SDimitry Andric return false; 1779*0b57cec5SDimitry Andric } 1780*0b57cec5SDimitry Andric 1781*0b57cec5SDimitry Andric if (TrueBBI.BB->hasAddressTaken() || FalseBBI.BB->hasAddressTaken()) 1782*0b57cec5SDimitry Andric // Conservatively abort if-conversion if either BB has its address taken. 1783*0b57cec5SDimitry Andric return false; 1784*0b57cec5SDimitry Andric 1785*0b57cec5SDimitry Andric // Put the predicated instructions from the 'true' block before the 1786*0b57cec5SDimitry Andric // instructions from the 'false' block, unless the true block would clobber 1787*0b57cec5SDimitry Andric // the predicate, in which case, do the opposite. 1788*0b57cec5SDimitry Andric BBInfo *BBI1 = &TrueBBI; 1789*0b57cec5SDimitry Andric BBInfo *BBI2 = &FalseBBI; 1790*0b57cec5SDimitry Andric SmallVector<MachineOperand, 4> RevCond(BBI.BrCond.begin(), BBI.BrCond.end()); 1791*0b57cec5SDimitry Andric if (TII->reverseBranchCondition(RevCond)) 1792*0b57cec5SDimitry Andric llvm_unreachable("Unable to reverse branch condition!"); 1793*0b57cec5SDimitry Andric SmallVector<MachineOperand, 4> *Cond1 = &BBI.BrCond; 1794*0b57cec5SDimitry Andric SmallVector<MachineOperand, 4> *Cond2 = &RevCond; 1795*0b57cec5SDimitry Andric 1796*0b57cec5SDimitry Andric // Figure out the more profitable ordering. 1797*0b57cec5SDimitry Andric bool DoSwap = false; 1798*0b57cec5SDimitry Andric if (TClobbersPred && !FClobbersPred) 1799*0b57cec5SDimitry Andric DoSwap = true; 1800*0b57cec5SDimitry Andric else if (!TClobbersPred && !FClobbersPred) { 1801*0b57cec5SDimitry Andric if (TrueBBI.NonPredSize > FalseBBI.NonPredSize) 1802*0b57cec5SDimitry Andric DoSwap = true; 1803*0b57cec5SDimitry Andric } else if (TClobbersPred && FClobbersPred) 1804*0b57cec5SDimitry Andric llvm_unreachable("Predicate info cannot be clobbered by both sides."); 1805*0b57cec5SDimitry Andric if (DoSwap) { 1806*0b57cec5SDimitry Andric std::swap(BBI1, BBI2); 1807*0b57cec5SDimitry Andric std::swap(Cond1, Cond2); 1808*0b57cec5SDimitry Andric } 1809*0b57cec5SDimitry Andric 1810*0b57cec5SDimitry Andric // Remove the conditional branch from entry to the blocks. 1811*0b57cec5SDimitry Andric BBI.NonPredSize -= TII->removeBranch(*BBI.BB); 1812*0b57cec5SDimitry Andric 1813*0b57cec5SDimitry Andric MachineBasicBlock &MBB1 = *BBI1->BB; 1814*0b57cec5SDimitry Andric MachineBasicBlock &MBB2 = *BBI2->BB; 1815*0b57cec5SDimitry Andric 1816*0b57cec5SDimitry Andric // Initialize the Redefs: 1817*0b57cec5SDimitry Andric // - BB2 live-in regs need implicit uses before being redefined by BB1 1818*0b57cec5SDimitry Andric // instructions. 1819*0b57cec5SDimitry Andric // - BB1 live-out regs need implicit uses before being redefined by BB2 1820*0b57cec5SDimitry Andric // instructions. We start with BB1 live-ins so we have the live-out regs 1821*0b57cec5SDimitry Andric // after tracking the BB1 instructions. 1822*0b57cec5SDimitry Andric Redefs.init(*TRI); 1823*0b57cec5SDimitry Andric if (MRI->tracksLiveness()) { 1824*0b57cec5SDimitry Andric Redefs.addLiveIns(MBB1); 1825*0b57cec5SDimitry Andric Redefs.addLiveIns(MBB2); 1826*0b57cec5SDimitry Andric } 1827*0b57cec5SDimitry Andric 1828*0b57cec5SDimitry Andric // Remove the duplicated instructions at the beginnings of both paths. 1829*0b57cec5SDimitry Andric // Skip dbg_value instructions. 1830*0b57cec5SDimitry Andric MachineBasicBlock::iterator DI1 = MBB1.getFirstNonDebugInstr(); 1831*0b57cec5SDimitry Andric MachineBasicBlock::iterator DI2 = MBB2.getFirstNonDebugInstr(); 1832*0b57cec5SDimitry Andric BBI1->NonPredSize -= NumDups1; 1833*0b57cec5SDimitry Andric BBI2->NonPredSize -= NumDups1; 1834*0b57cec5SDimitry Andric 1835*0b57cec5SDimitry Andric // Skip past the dups on each side separately since there may be 1836*0b57cec5SDimitry Andric // differing dbg_value entries. NumDups1 can include a "return" 1837*0b57cec5SDimitry Andric // instruction, if it's not marked as "branch". 1838*0b57cec5SDimitry Andric for (unsigned i = 0; i < NumDups1; ++DI1) { 1839*0b57cec5SDimitry Andric if (DI1 == MBB1.end()) 1840*0b57cec5SDimitry Andric break; 1841*0b57cec5SDimitry Andric if (!DI1->isDebugInstr()) 1842*0b57cec5SDimitry Andric ++i; 1843*0b57cec5SDimitry Andric } 1844*0b57cec5SDimitry Andric while (NumDups1 != 0) { 18458bcb0991SDimitry Andric // Since this instruction is going to be deleted, update call 18468bcb0991SDimitry Andric // site info state if the instruction is call instruction. 18478bcb0991SDimitry Andric if (DI2->isCall(MachineInstr::IgnoreBundle)) 18488bcb0991SDimitry Andric MBB2.getParent()->eraseCallSiteInfo(&*DI2); 18498bcb0991SDimitry Andric 1850*0b57cec5SDimitry Andric ++DI2; 1851*0b57cec5SDimitry Andric if (DI2 == MBB2.end()) 1852*0b57cec5SDimitry Andric break; 1853*0b57cec5SDimitry Andric if (!DI2->isDebugInstr()) 1854*0b57cec5SDimitry Andric --NumDups1; 1855*0b57cec5SDimitry Andric } 1856*0b57cec5SDimitry Andric 1857*0b57cec5SDimitry Andric if (MRI->tracksLiveness()) { 1858*0b57cec5SDimitry Andric for (const MachineInstr &MI : make_range(MBB1.begin(), DI1)) { 1859*0b57cec5SDimitry Andric SmallVector<std::pair<MCPhysReg, const MachineOperand*>, 4> Dummy; 1860*0b57cec5SDimitry Andric Redefs.stepForward(MI, Dummy); 1861*0b57cec5SDimitry Andric } 1862*0b57cec5SDimitry Andric } 1863*0b57cec5SDimitry Andric 1864*0b57cec5SDimitry Andric BBI.BB->splice(BBI.BB->end(), &MBB1, MBB1.begin(), DI1); 1865*0b57cec5SDimitry Andric MBB2.erase(MBB2.begin(), DI2); 1866*0b57cec5SDimitry Andric 1867*0b57cec5SDimitry Andric // The branches have been checked to match, so it is safe to remove the 1868*0b57cec5SDimitry Andric // branch in BB1 and rely on the copy in BB2. The complication is that 1869*0b57cec5SDimitry Andric // the blocks may end with a return instruction, which may or may not 1870*0b57cec5SDimitry Andric // be marked as "branch". If it's not, then it could be included in 1871*0b57cec5SDimitry Andric // "dups1", leaving the blocks potentially empty after moving the common 1872*0b57cec5SDimitry Andric // duplicates. 1873*0b57cec5SDimitry Andric #ifndef NDEBUG 1874*0b57cec5SDimitry Andric // Unanalyzable branches must match exactly. Check that now. 1875*0b57cec5SDimitry Andric if (!BBI1->IsBrAnalyzable) 1876*0b57cec5SDimitry Andric verifySameBranchInstructions(&MBB1, &MBB2); 1877*0b57cec5SDimitry Andric #endif 1878*0b57cec5SDimitry Andric // Remove duplicated instructions from the tail of MBB1: any branch 1879*0b57cec5SDimitry Andric // instructions, and the common instructions counted by NumDups2. 1880*0b57cec5SDimitry Andric DI1 = MBB1.end(); 1881*0b57cec5SDimitry Andric while (DI1 != MBB1.begin()) { 1882*0b57cec5SDimitry Andric MachineBasicBlock::iterator Prev = std::prev(DI1); 1883*0b57cec5SDimitry Andric if (!Prev->isBranch() && !Prev->isDebugInstr()) 1884*0b57cec5SDimitry Andric break; 1885*0b57cec5SDimitry Andric DI1 = Prev; 1886*0b57cec5SDimitry Andric } 1887*0b57cec5SDimitry Andric for (unsigned i = 0; i != NumDups2; ) { 1888*0b57cec5SDimitry Andric // NumDups2 only counted non-dbg_value instructions, so this won't 1889*0b57cec5SDimitry Andric // run off the head of the list. 1890*0b57cec5SDimitry Andric assert(DI1 != MBB1.begin()); 18918bcb0991SDimitry Andric 1892*0b57cec5SDimitry Andric --DI1; 18938bcb0991SDimitry Andric 18948bcb0991SDimitry Andric // Since this instruction is going to be deleted, update call 18958bcb0991SDimitry Andric // site info state if the instruction is call instruction. 18968bcb0991SDimitry Andric if (DI1->isCall(MachineInstr::IgnoreBundle)) 18978bcb0991SDimitry Andric MBB1.getParent()->eraseCallSiteInfo(&*DI1); 18988bcb0991SDimitry Andric 1899*0b57cec5SDimitry Andric // skip dbg_value instructions 1900*0b57cec5SDimitry Andric if (!DI1->isDebugInstr()) 1901*0b57cec5SDimitry Andric ++i; 1902*0b57cec5SDimitry Andric } 1903*0b57cec5SDimitry Andric MBB1.erase(DI1, MBB1.end()); 1904*0b57cec5SDimitry Andric 1905*0b57cec5SDimitry Andric DI2 = BBI2->BB->end(); 1906*0b57cec5SDimitry Andric // The branches have been checked to match. Skip over the branch in the false 1907*0b57cec5SDimitry Andric // block so that we don't try to predicate it. 1908*0b57cec5SDimitry Andric if (RemoveBranch) 1909*0b57cec5SDimitry Andric BBI2->NonPredSize -= TII->removeBranch(*BBI2->BB); 1910*0b57cec5SDimitry Andric else { 1911*0b57cec5SDimitry Andric // Make DI2 point to the end of the range where the common "tail" 1912*0b57cec5SDimitry Andric // instructions could be found. 1913*0b57cec5SDimitry Andric while (DI2 != MBB2.begin()) { 1914*0b57cec5SDimitry Andric MachineBasicBlock::iterator Prev = std::prev(DI2); 1915*0b57cec5SDimitry Andric if (!Prev->isBranch() && !Prev->isDebugInstr()) 1916*0b57cec5SDimitry Andric break; 1917*0b57cec5SDimitry Andric DI2 = Prev; 1918*0b57cec5SDimitry Andric } 1919*0b57cec5SDimitry Andric } 1920*0b57cec5SDimitry Andric while (NumDups2 != 0) { 1921*0b57cec5SDimitry Andric // NumDups2 only counted non-dbg_value instructions, so this won't 1922*0b57cec5SDimitry Andric // run off the head of the list. 1923*0b57cec5SDimitry Andric assert(DI2 != MBB2.begin()); 1924*0b57cec5SDimitry Andric --DI2; 1925*0b57cec5SDimitry Andric // skip dbg_value instructions 1926*0b57cec5SDimitry Andric if (!DI2->isDebugInstr()) 1927*0b57cec5SDimitry Andric --NumDups2; 1928*0b57cec5SDimitry Andric } 1929*0b57cec5SDimitry Andric 1930*0b57cec5SDimitry Andric // Remember which registers would later be defined by the false block. 1931*0b57cec5SDimitry Andric // This allows us not to predicate instructions in the true block that would 1932*0b57cec5SDimitry Andric // later be re-defined. That is, rather than 1933*0b57cec5SDimitry Andric // subeq r0, r1, #1 1934*0b57cec5SDimitry Andric // addne r0, r1, #1 1935*0b57cec5SDimitry Andric // generate: 1936*0b57cec5SDimitry Andric // sub r0, r1, #1 1937*0b57cec5SDimitry Andric // addne r0, r1, #1 1938*0b57cec5SDimitry Andric SmallSet<MCPhysReg, 4> RedefsByFalse; 1939*0b57cec5SDimitry Andric SmallSet<MCPhysReg, 4> ExtUses; 1940*0b57cec5SDimitry Andric if (TII->isProfitableToUnpredicate(MBB1, MBB2)) { 1941*0b57cec5SDimitry Andric for (const MachineInstr &FI : make_range(MBB2.begin(), DI2)) { 1942*0b57cec5SDimitry Andric if (FI.isDebugInstr()) 1943*0b57cec5SDimitry Andric continue; 1944*0b57cec5SDimitry Andric SmallVector<MCPhysReg, 4> Defs; 1945*0b57cec5SDimitry Andric for (const MachineOperand &MO : FI.operands()) { 1946*0b57cec5SDimitry Andric if (!MO.isReg()) 1947*0b57cec5SDimitry Andric continue; 19488bcb0991SDimitry Andric Register Reg = MO.getReg(); 1949*0b57cec5SDimitry Andric if (!Reg) 1950*0b57cec5SDimitry Andric continue; 1951*0b57cec5SDimitry Andric if (MO.isDef()) { 1952*0b57cec5SDimitry Andric Defs.push_back(Reg); 1953*0b57cec5SDimitry Andric } else if (!RedefsByFalse.count(Reg)) { 1954*0b57cec5SDimitry Andric // These are defined before ctrl flow reach the 'false' instructions. 1955*0b57cec5SDimitry Andric // They cannot be modified by the 'true' instructions. 1956*0b57cec5SDimitry Andric for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true); 1957*0b57cec5SDimitry Andric SubRegs.isValid(); ++SubRegs) 1958*0b57cec5SDimitry Andric ExtUses.insert(*SubRegs); 1959*0b57cec5SDimitry Andric } 1960*0b57cec5SDimitry Andric } 1961*0b57cec5SDimitry Andric 1962*0b57cec5SDimitry Andric for (MCPhysReg Reg : Defs) { 1963*0b57cec5SDimitry Andric if (!ExtUses.count(Reg)) { 1964*0b57cec5SDimitry Andric for (MCSubRegIterator SubRegs(Reg, TRI, /*IncludeSelf=*/true); 1965*0b57cec5SDimitry Andric SubRegs.isValid(); ++SubRegs) 1966*0b57cec5SDimitry Andric RedefsByFalse.insert(*SubRegs); 1967*0b57cec5SDimitry Andric } 1968*0b57cec5SDimitry Andric } 1969*0b57cec5SDimitry Andric } 1970*0b57cec5SDimitry Andric } 1971*0b57cec5SDimitry Andric 1972*0b57cec5SDimitry Andric // Predicate the 'true' block. 1973*0b57cec5SDimitry Andric PredicateBlock(*BBI1, MBB1.end(), *Cond1, &RedefsByFalse); 1974*0b57cec5SDimitry Andric 1975*0b57cec5SDimitry Andric // After predicating BBI1, if there is a predicated terminator in BBI1 and 1976*0b57cec5SDimitry Andric // a non-predicated in BBI2, then we don't want to predicate the one from 1977*0b57cec5SDimitry Andric // BBI2. The reason is that if we merged these blocks, we would end up with 1978*0b57cec5SDimitry Andric // two predicated terminators in the same block. 1979*0b57cec5SDimitry Andric // Also, if the branches in MBB1 and MBB2 were non-analyzable, then don't 1980*0b57cec5SDimitry Andric // predicate them either. They were checked to be identical, and so the 1981*0b57cec5SDimitry Andric // same branch would happen regardless of which path was taken. 1982*0b57cec5SDimitry Andric if (!MBB2.empty() && (DI2 == MBB2.end())) { 1983*0b57cec5SDimitry Andric MachineBasicBlock::iterator BBI1T = MBB1.getFirstTerminator(); 1984*0b57cec5SDimitry Andric MachineBasicBlock::iterator BBI2T = MBB2.getFirstTerminator(); 1985*0b57cec5SDimitry Andric bool BB1Predicated = BBI1T != MBB1.end() && TII->isPredicated(*BBI1T); 1986*0b57cec5SDimitry Andric bool BB2NonPredicated = BBI2T != MBB2.end() && !TII->isPredicated(*BBI2T); 1987*0b57cec5SDimitry Andric if (BB2NonPredicated && (BB1Predicated || !BBI2->IsBrAnalyzable)) 1988*0b57cec5SDimitry Andric --DI2; 1989*0b57cec5SDimitry Andric } 1990*0b57cec5SDimitry Andric 1991*0b57cec5SDimitry Andric // Predicate the 'false' block. 1992*0b57cec5SDimitry Andric PredicateBlock(*BBI2, DI2, *Cond2); 1993*0b57cec5SDimitry Andric 1994*0b57cec5SDimitry Andric // Merge the true block into the entry of the diamond. 1995*0b57cec5SDimitry Andric MergeBlocks(BBI, *BBI1, MergeAddEdges); 1996*0b57cec5SDimitry Andric MergeBlocks(BBI, *BBI2, MergeAddEdges); 1997*0b57cec5SDimitry Andric return true; 1998*0b57cec5SDimitry Andric } 1999*0b57cec5SDimitry Andric 2000*0b57cec5SDimitry Andric /// If convert an almost-diamond sub-CFG where the true 2001*0b57cec5SDimitry Andric /// and false blocks share a common tail. 2002*0b57cec5SDimitry Andric bool IfConverter::IfConvertForkedDiamond( 2003*0b57cec5SDimitry Andric BBInfo &BBI, IfcvtKind Kind, 2004*0b57cec5SDimitry Andric unsigned NumDups1, unsigned NumDups2, 2005*0b57cec5SDimitry Andric bool TClobbersPred, bool FClobbersPred) { 2006*0b57cec5SDimitry Andric BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()]; 2007*0b57cec5SDimitry Andric BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()]; 2008*0b57cec5SDimitry Andric 2009*0b57cec5SDimitry Andric // Save the debug location for later. 2010*0b57cec5SDimitry Andric DebugLoc dl; 2011*0b57cec5SDimitry Andric MachineBasicBlock::iterator TIE = TrueBBI.BB->getFirstTerminator(); 2012*0b57cec5SDimitry Andric if (TIE != TrueBBI.BB->end()) 2013*0b57cec5SDimitry Andric dl = TIE->getDebugLoc(); 2014*0b57cec5SDimitry Andric // Removing branches from both blocks is safe, because we have already 2015*0b57cec5SDimitry Andric // determined that both blocks have the same branch instructions. The branch 2016*0b57cec5SDimitry Andric // will be added back at the end, unpredicated. 2017*0b57cec5SDimitry Andric if (!IfConvertDiamondCommon( 2018*0b57cec5SDimitry Andric BBI, TrueBBI, FalseBBI, 2019*0b57cec5SDimitry Andric NumDups1, NumDups2, 2020*0b57cec5SDimitry Andric TClobbersPred, FClobbersPred, 2021*0b57cec5SDimitry Andric /* RemoveBranch */ true, /* MergeAddEdges */ true)) 2022*0b57cec5SDimitry Andric return false; 2023*0b57cec5SDimitry Andric 2024*0b57cec5SDimitry Andric // Add back the branch. 2025*0b57cec5SDimitry Andric // Debug location saved above when removing the branch from BBI2 2026*0b57cec5SDimitry Andric TII->insertBranch(*BBI.BB, TrueBBI.TrueBB, TrueBBI.FalseBB, 2027*0b57cec5SDimitry Andric TrueBBI.BrCond, dl); 2028*0b57cec5SDimitry Andric 2029*0b57cec5SDimitry Andric // Update block info. 2030*0b57cec5SDimitry Andric BBI.IsDone = TrueBBI.IsDone = FalseBBI.IsDone = true; 2031*0b57cec5SDimitry Andric InvalidatePreds(*BBI.BB); 2032*0b57cec5SDimitry Andric 2033*0b57cec5SDimitry Andric // FIXME: Must maintain LiveIns. 2034*0b57cec5SDimitry Andric return true; 2035*0b57cec5SDimitry Andric } 2036*0b57cec5SDimitry Andric 2037*0b57cec5SDimitry Andric /// If convert a diamond sub-CFG. 2038*0b57cec5SDimitry Andric bool IfConverter::IfConvertDiamond(BBInfo &BBI, IfcvtKind Kind, 2039*0b57cec5SDimitry Andric unsigned NumDups1, unsigned NumDups2, 2040*0b57cec5SDimitry Andric bool TClobbersPred, bool FClobbersPred) { 2041*0b57cec5SDimitry Andric BBInfo &TrueBBI = BBAnalysis[BBI.TrueBB->getNumber()]; 2042*0b57cec5SDimitry Andric BBInfo &FalseBBI = BBAnalysis[BBI.FalseBB->getNumber()]; 2043*0b57cec5SDimitry Andric MachineBasicBlock *TailBB = TrueBBI.TrueBB; 2044*0b57cec5SDimitry Andric 2045*0b57cec5SDimitry Andric // True block must fall through or end with an unanalyzable terminator. 2046*0b57cec5SDimitry Andric if (!TailBB) { 2047*0b57cec5SDimitry Andric if (blockAlwaysFallThrough(TrueBBI)) 2048*0b57cec5SDimitry Andric TailBB = FalseBBI.TrueBB; 2049*0b57cec5SDimitry Andric assert((TailBB || !TrueBBI.IsBrAnalyzable) && "Unexpected!"); 2050*0b57cec5SDimitry Andric } 2051*0b57cec5SDimitry Andric 2052*0b57cec5SDimitry Andric if (!IfConvertDiamondCommon( 2053*0b57cec5SDimitry Andric BBI, TrueBBI, FalseBBI, 2054*0b57cec5SDimitry Andric NumDups1, NumDups2, 2055*0b57cec5SDimitry Andric TClobbersPred, FClobbersPred, 2056*0b57cec5SDimitry Andric /* RemoveBranch */ TrueBBI.IsBrAnalyzable, 2057*0b57cec5SDimitry Andric /* MergeAddEdges */ TailBB == nullptr)) 2058*0b57cec5SDimitry Andric return false; 2059*0b57cec5SDimitry Andric 2060*0b57cec5SDimitry Andric // If the if-converted block falls through or unconditionally branches into 2061*0b57cec5SDimitry Andric // the tail block, and the tail block does not have other predecessors, then 2062*0b57cec5SDimitry Andric // fold the tail block in as well. Otherwise, unless it falls through to the 2063*0b57cec5SDimitry Andric // tail, add a unconditional branch to it. 2064*0b57cec5SDimitry Andric if (TailBB) { 2065*0b57cec5SDimitry Andric // We need to remove the edges to the true and false blocks manually since 2066*0b57cec5SDimitry Andric // we didn't let IfConvertDiamondCommon update the CFG. 2067*0b57cec5SDimitry Andric BBI.BB->removeSuccessor(TrueBBI.BB); 2068*0b57cec5SDimitry Andric BBI.BB->removeSuccessor(FalseBBI.BB, true); 2069*0b57cec5SDimitry Andric 2070*0b57cec5SDimitry Andric BBInfo &TailBBI = BBAnalysis[TailBB->getNumber()]; 2071*0b57cec5SDimitry Andric bool CanMergeTail = !TailBBI.HasFallThrough && 2072*0b57cec5SDimitry Andric !TailBBI.BB->hasAddressTaken(); 2073*0b57cec5SDimitry Andric // The if-converted block can still have a predicated terminator 2074*0b57cec5SDimitry Andric // (e.g. a predicated return). If that is the case, we cannot merge 2075*0b57cec5SDimitry Andric // it with the tail block. 2076*0b57cec5SDimitry Andric MachineBasicBlock::const_iterator TI = BBI.BB->getFirstTerminator(); 2077*0b57cec5SDimitry Andric if (TI != BBI.BB->end() && TII->isPredicated(*TI)) 2078*0b57cec5SDimitry Andric CanMergeTail = false; 2079*0b57cec5SDimitry Andric // There may still be a fall-through edge from BBI1 or BBI2 to TailBB; 2080*0b57cec5SDimitry Andric // check if there are any other predecessors besides those. 2081*0b57cec5SDimitry Andric unsigned NumPreds = TailBB->pred_size(); 2082*0b57cec5SDimitry Andric if (NumPreds > 1) 2083*0b57cec5SDimitry Andric CanMergeTail = false; 2084*0b57cec5SDimitry Andric else if (NumPreds == 1 && CanMergeTail) { 2085*0b57cec5SDimitry Andric MachineBasicBlock::pred_iterator PI = TailBB->pred_begin(); 2086*0b57cec5SDimitry Andric if (*PI != TrueBBI.BB && *PI != FalseBBI.BB) 2087*0b57cec5SDimitry Andric CanMergeTail = false; 2088*0b57cec5SDimitry Andric } 2089*0b57cec5SDimitry Andric if (CanMergeTail) { 2090*0b57cec5SDimitry Andric MergeBlocks(BBI, TailBBI); 2091*0b57cec5SDimitry Andric TailBBI.IsDone = true; 2092*0b57cec5SDimitry Andric } else { 2093*0b57cec5SDimitry Andric BBI.BB->addSuccessor(TailBB, BranchProbability::getOne()); 2094*0b57cec5SDimitry Andric InsertUncondBranch(*BBI.BB, *TailBB, TII); 2095*0b57cec5SDimitry Andric BBI.HasFallThrough = false; 2096*0b57cec5SDimitry Andric } 2097*0b57cec5SDimitry Andric } 2098*0b57cec5SDimitry Andric 2099*0b57cec5SDimitry Andric // Update block info. 2100*0b57cec5SDimitry Andric BBI.IsDone = TrueBBI.IsDone = FalseBBI.IsDone = true; 2101*0b57cec5SDimitry Andric InvalidatePreds(*BBI.BB); 2102*0b57cec5SDimitry Andric 2103*0b57cec5SDimitry Andric // FIXME: Must maintain LiveIns. 2104*0b57cec5SDimitry Andric return true; 2105*0b57cec5SDimitry Andric } 2106*0b57cec5SDimitry Andric 2107*0b57cec5SDimitry Andric static bool MaySpeculate(const MachineInstr &MI, 2108*0b57cec5SDimitry Andric SmallSet<MCPhysReg, 4> &LaterRedefs) { 2109*0b57cec5SDimitry Andric bool SawStore = true; 2110*0b57cec5SDimitry Andric if (!MI.isSafeToMove(nullptr, SawStore)) 2111*0b57cec5SDimitry Andric return false; 2112*0b57cec5SDimitry Andric 2113*0b57cec5SDimitry Andric for (const MachineOperand &MO : MI.operands()) { 2114*0b57cec5SDimitry Andric if (!MO.isReg()) 2115*0b57cec5SDimitry Andric continue; 21168bcb0991SDimitry Andric Register Reg = MO.getReg(); 2117*0b57cec5SDimitry Andric if (!Reg) 2118*0b57cec5SDimitry Andric continue; 2119*0b57cec5SDimitry Andric if (MO.isDef() && !LaterRedefs.count(Reg)) 2120*0b57cec5SDimitry Andric return false; 2121*0b57cec5SDimitry Andric } 2122*0b57cec5SDimitry Andric 2123*0b57cec5SDimitry Andric return true; 2124*0b57cec5SDimitry Andric } 2125*0b57cec5SDimitry Andric 2126*0b57cec5SDimitry Andric /// Predicate instructions from the start of the block to the specified end with 2127*0b57cec5SDimitry Andric /// the specified condition. 2128*0b57cec5SDimitry Andric void IfConverter::PredicateBlock(BBInfo &BBI, 2129*0b57cec5SDimitry Andric MachineBasicBlock::iterator E, 2130*0b57cec5SDimitry Andric SmallVectorImpl<MachineOperand> &Cond, 2131*0b57cec5SDimitry Andric SmallSet<MCPhysReg, 4> *LaterRedefs) { 2132*0b57cec5SDimitry Andric bool AnyUnpred = false; 2133*0b57cec5SDimitry Andric bool MaySpec = LaterRedefs != nullptr; 2134*0b57cec5SDimitry Andric for (MachineInstr &I : make_range(BBI.BB->begin(), E)) { 2135*0b57cec5SDimitry Andric if (I.isDebugInstr() || TII->isPredicated(I)) 2136*0b57cec5SDimitry Andric continue; 2137*0b57cec5SDimitry Andric // It may be possible not to predicate an instruction if it's the 'true' 2138*0b57cec5SDimitry Andric // side of a diamond and the 'false' side may re-define the instruction's 2139*0b57cec5SDimitry Andric // defs. 2140*0b57cec5SDimitry Andric if (MaySpec && MaySpeculate(I, *LaterRedefs)) { 2141*0b57cec5SDimitry Andric AnyUnpred = true; 2142*0b57cec5SDimitry Andric continue; 2143*0b57cec5SDimitry Andric } 2144*0b57cec5SDimitry Andric // If any instruction is predicated, then every instruction after it must 2145*0b57cec5SDimitry Andric // be predicated. 2146*0b57cec5SDimitry Andric MaySpec = false; 2147*0b57cec5SDimitry Andric if (!TII->PredicateInstruction(I, Cond)) { 2148*0b57cec5SDimitry Andric #ifndef NDEBUG 2149*0b57cec5SDimitry Andric dbgs() << "Unable to predicate " << I << "!\n"; 2150*0b57cec5SDimitry Andric #endif 2151*0b57cec5SDimitry Andric llvm_unreachable(nullptr); 2152*0b57cec5SDimitry Andric } 2153*0b57cec5SDimitry Andric 2154*0b57cec5SDimitry Andric // If the predicated instruction now redefines a register as the result of 2155*0b57cec5SDimitry Andric // if-conversion, add an implicit kill. 2156*0b57cec5SDimitry Andric UpdatePredRedefs(I, Redefs); 2157*0b57cec5SDimitry Andric } 2158*0b57cec5SDimitry Andric 2159*0b57cec5SDimitry Andric BBI.Predicate.append(Cond.begin(), Cond.end()); 2160*0b57cec5SDimitry Andric 2161*0b57cec5SDimitry Andric BBI.IsAnalyzed = false; 2162*0b57cec5SDimitry Andric BBI.NonPredSize = 0; 2163*0b57cec5SDimitry Andric 2164*0b57cec5SDimitry Andric ++NumIfConvBBs; 2165*0b57cec5SDimitry Andric if (AnyUnpred) 2166*0b57cec5SDimitry Andric ++NumUnpred; 2167*0b57cec5SDimitry Andric } 2168*0b57cec5SDimitry Andric 2169*0b57cec5SDimitry Andric /// Copy and predicate instructions from source BB to the destination block. 2170*0b57cec5SDimitry Andric /// Skip end of block branches if IgnoreBr is true. 2171*0b57cec5SDimitry Andric void IfConverter::CopyAndPredicateBlock(BBInfo &ToBBI, BBInfo &FromBBI, 2172*0b57cec5SDimitry Andric SmallVectorImpl<MachineOperand> &Cond, 2173*0b57cec5SDimitry Andric bool IgnoreBr) { 2174*0b57cec5SDimitry Andric MachineFunction &MF = *ToBBI.BB->getParent(); 2175*0b57cec5SDimitry Andric 2176*0b57cec5SDimitry Andric MachineBasicBlock &FromMBB = *FromBBI.BB; 2177*0b57cec5SDimitry Andric for (MachineInstr &I : FromMBB) { 2178*0b57cec5SDimitry Andric // Do not copy the end of the block branches. 2179*0b57cec5SDimitry Andric if (IgnoreBr && I.isBranch()) 2180*0b57cec5SDimitry Andric break; 2181*0b57cec5SDimitry Andric 2182*0b57cec5SDimitry Andric MachineInstr *MI = MF.CloneMachineInstr(&I); 21838bcb0991SDimitry Andric // Make a copy of the call site info. 21848bcb0991SDimitry Andric if (MI->isCall(MachineInstr::IgnoreBundle)) 21858bcb0991SDimitry Andric MF.copyCallSiteInfo(&I,MI); 21868bcb0991SDimitry Andric 2187*0b57cec5SDimitry Andric ToBBI.BB->insert(ToBBI.BB->end(), MI); 2188*0b57cec5SDimitry Andric ToBBI.NonPredSize++; 2189*0b57cec5SDimitry Andric unsigned ExtraPredCost = TII->getPredicationCost(I); 2190*0b57cec5SDimitry Andric unsigned NumCycles = SchedModel.computeInstrLatency(&I, false); 2191*0b57cec5SDimitry Andric if (NumCycles > 1) 2192*0b57cec5SDimitry Andric ToBBI.ExtraCost += NumCycles-1; 2193*0b57cec5SDimitry Andric ToBBI.ExtraCost2 += ExtraPredCost; 2194*0b57cec5SDimitry Andric 2195*0b57cec5SDimitry Andric if (!TII->isPredicated(I) && !MI->isDebugInstr()) { 2196*0b57cec5SDimitry Andric if (!TII->PredicateInstruction(*MI, Cond)) { 2197*0b57cec5SDimitry Andric #ifndef NDEBUG 2198*0b57cec5SDimitry Andric dbgs() << "Unable to predicate " << I << "!\n"; 2199*0b57cec5SDimitry Andric #endif 2200*0b57cec5SDimitry Andric llvm_unreachable(nullptr); 2201*0b57cec5SDimitry Andric } 2202*0b57cec5SDimitry Andric } 2203*0b57cec5SDimitry Andric 2204*0b57cec5SDimitry Andric // If the predicated instruction now redefines a register as the result of 2205*0b57cec5SDimitry Andric // if-conversion, add an implicit kill. 2206*0b57cec5SDimitry Andric UpdatePredRedefs(*MI, Redefs); 2207*0b57cec5SDimitry Andric } 2208*0b57cec5SDimitry Andric 2209*0b57cec5SDimitry Andric if (!IgnoreBr) { 2210*0b57cec5SDimitry Andric std::vector<MachineBasicBlock *> Succs(FromMBB.succ_begin(), 2211*0b57cec5SDimitry Andric FromMBB.succ_end()); 2212*0b57cec5SDimitry Andric MachineBasicBlock *NBB = getNextBlock(FromMBB); 2213*0b57cec5SDimitry Andric MachineBasicBlock *FallThrough = FromBBI.HasFallThrough ? NBB : nullptr; 2214*0b57cec5SDimitry Andric 2215*0b57cec5SDimitry Andric for (MachineBasicBlock *Succ : Succs) { 2216*0b57cec5SDimitry Andric // Fallthrough edge can't be transferred. 2217*0b57cec5SDimitry Andric if (Succ == FallThrough) 2218*0b57cec5SDimitry Andric continue; 2219*0b57cec5SDimitry Andric ToBBI.BB->addSuccessor(Succ); 2220*0b57cec5SDimitry Andric } 2221*0b57cec5SDimitry Andric } 2222*0b57cec5SDimitry Andric 2223*0b57cec5SDimitry Andric ToBBI.Predicate.append(FromBBI.Predicate.begin(), FromBBI.Predicate.end()); 2224*0b57cec5SDimitry Andric ToBBI.Predicate.append(Cond.begin(), Cond.end()); 2225*0b57cec5SDimitry Andric 2226*0b57cec5SDimitry Andric ToBBI.ClobbersPred |= FromBBI.ClobbersPred; 2227*0b57cec5SDimitry Andric ToBBI.IsAnalyzed = false; 2228*0b57cec5SDimitry Andric 2229*0b57cec5SDimitry Andric ++NumDupBBs; 2230*0b57cec5SDimitry Andric } 2231*0b57cec5SDimitry Andric 2232*0b57cec5SDimitry Andric /// Move all instructions from FromBB to the end of ToBB. This will leave 2233*0b57cec5SDimitry Andric /// FromBB as an empty block, so remove all of its successor edges except for 2234*0b57cec5SDimitry Andric /// the fall-through edge. If AddEdges is true, i.e., when FromBBI's branch is 2235*0b57cec5SDimitry Andric /// being moved, add those successor edges to ToBBI and remove the old edge 2236*0b57cec5SDimitry Andric /// from ToBBI to FromBBI. 2237*0b57cec5SDimitry Andric void IfConverter::MergeBlocks(BBInfo &ToBBI, BBInfo &FromBBI, bool AddEdges) { 2238*0b57cec5SDimitry Andric MachineBasicBlock &FromMBB = *FromBBI.BB; 2239*0b57cec5SDimitry Andric assert(!FromMBB.hasAddressTaken() && 2240*0b57cec5SDimitry Andric "Removing a BB whose address is taken!"); 2241*0b57cec5SDimitry Andric 2242*0b57cec5SDimitry Andric // In case FromMBB contains terminators (e.g. return instruction), 2243*0b57cec5SDimitry Andric // first move the non-terminator instructions, then the terminators. 2244*0b57cec5SDimitry Andric MachineBasicBlock::iterator FromTI = FromMBB.getFirstTerminator(); 2245*0b57cec5SDimitry Andric MachineBasicBlock::iterator ToTI = ToBBI.BB->getFirstTerminator(); 2246*0b57cec5SDimitry Andric ToBBI.BB->splice(ToTI, &FromMBB, FromMBB.begin(), FromTI); 2247*0b57cec5SDimitry Andric 2248*0b57cec5SDimitry Andric // If FromBB has non-predicated terminator we should copy it at the end. 2249*0b57cec5SDimitry Andric if (FromTI != FromMBB.end() && !TII->isPredicated(*FromTI)) 2250*0b57cec5SDimitry Andric ToTI = ToBBI.BB->end(); 2251*0b57cec5SDimitry Andric ToBBI.BB->splice(ToTI, &FromMBB, FromTI, FromMBB.end()); 2252*0b57cec5SDimitry Andric 2253*0b57cec5SDimitry Andric // Force normalizing the successors' probabilities of ToBBI.BB to convert all 2254*0b57cec5SDimitry Andric // unknown probabilities into known ones. 2255*0b57cec5SDimitry Andric // FIXME: This usage is too tricky and in the future we would like to 2256*0b57cec5SDimitry Andric // eliminate all unknown probabilities in MBB. 2257*0b57cec5SDimitry Andric if (ToBBI.IsBrAnalyzable) 2258*0b57cec5SDimitry Andric ToBBI.BB->normalizeSuccProbs(); 2259*0b57cec5SDimitry Andric 2260*0b57cec5SDimitry Andric SmallVector<MachineBasicBlock *, 4> FromSuccs(FromMBB.succ_begin(), 2261*0b57cec5SDimitry Andric FromMBB.succ_end()); 2262*0b57cec5SDimitry Andric MachineBasicBlock *NBB = getNextBlock(FromMBB); 2263*0b57cec5SDimitry Andric MachineBasicBlock *FallThrough = FromBBI.HasFallThrough ? NBB : nullptr; 2264*0b57cec5SDimitry Andric // The edge probability from ToBBI.BB to FromMBB, which is only needed when 2265*0b57cec5SDimitry Andric // AddEdges is true and FromMBB is a successor of ToBBI.BB. 2266*0b57cec5SDimitry Andric auto To2FromProb = BranchProbability::getZero(); 2267*0b57cec5SDimitry Andric if (AddEdges && ToBBI.BB->isSuccessor(&FromMBB)) { 2268*0b57cec5SDimitry Andric // Remove the old edge but remember the edge probability so we can calculate 2269*0b57cec5SDimitry Andric // the correct weights on the new edges being added further down. 2270*0b57cec5SDimitry Andric To2FromProb = MBPI->getEdgeProbability(ToBBI.BB, &FromMBB); 2271*0b57cec5SDimitry Andric ToBBI.BB->removeSuccessor(&FromMBB); 2272*0b57cec5SDimitry Andric } 2273*0b57cec5SDimitry Andric 2274*0b57cec5SDimitry Andric for (MachineBasicBlock *Succ : FromSuccs) { 2275*0b57cec5SDimitry Andric // Fallthrough edge can't be transferred. 2276*0b57cec5SDimitry Andric if (Succ == FallThrough) 2277*0b57cec5SDimitry Andric continue; 2278*0b57cec5SDimitry Andric 2279*0b57cec5SDimitry Andric auto NewProb = BranchProbability::getZero(); 2280*0b57cec5SDimitry Andric if (AddEdges) { 2281*0b57cec5SDimitry Andric // Calculate the edge probability for the edge from ToBBI.BB to Succ, 2282*0b57cec5SDimitry Andric // which is a portion of the edge probability from FromMBB to Succ. The 2283*0b57cec5SDimitry Andric // portion ratio is the edge probability from ToBBI.BB to FromMBB (if 2284*0b57cec5SDimitry Andric // FromBBI is a successor of ToBBI.BB. See comment below for exception). 2285*0b57cec5SDimitry Andric NewProb = MBPI->getEdgeProbability(&FromMBB, Succ); 2286*0b57cec5SDimitry Andric 2287*0b57cec5SDimitry Andric // To2FromProb is 0 when FromMBB is not a successor of ToBBI.BB. This 2288*0b57cec5SDimitry Andric // only happens when if-converting a diamond CFG and FromMBB is the 2289*0b57cec5SDimitry Andric // tail BB. In this case FromMBB post-dominates ToBBI.BB and hence we 2290*0b57cec5SDimitry Andric // could just use the probabilities on FromMBB's out-edges when adding 2291*0b57cec5SDimitry Andric // new successors. 2292*0b57cec5SDimitry Andric if (!To2FromProb.isZero()) 2293*0b57cec5SDimitry Andric NewProb *= To2FromProb; 2294*0b57cec5SDimitry Andric } 2295*0b57cec5SDimitry Andric 2296*0b57cec5SDimitry Andric FromMBB.removeSuccessor(Succ); 2297*0b57cec5SDimitry Andric 2298*0b57cec5SDimitry Andric if (AddEdges) { 2299*0b57cec5SDimitry Andric // If the edge from ToBBI.BB to Succ already exists, update the 2300*0b57cec5SDimitry Andric // probability of this edge by adding NewProb to it. An example is shown 2301*0b57cec5SDimitry Andric // below, in which A is ToBBI.BB and B is FromMBB. In this case we 2302*0b57cec5SDimitry Andric // don't have to set C as A's successor as it already is. We only need to 2303*0b57cec5SDimitry Andric // update the edge probability on A->C. Note that B will not be 2304*0b57cec5SDimitry Andric // immediately removed from A's successors. It is possible that B->D is 2305*0b57cec5SDimitry Andric // not removed either if D is a fallthrough of B. Later the edge A->D 2306*0b57cec5SDimitry Andric // (generated here) and B->D will be combined into one edge. To maintain 2307*0b57cec5SDimitry Andric // correct edge probability of this combined edge, we need to set the edge 2308*0b57cec5SDimitry Andric // probability of A->B to zero, which is already done above. The edge 2309*0b57cec5SDimitry Andric // probability on A->D is calculated by scaling the original probability 2310*0b57cec5SDimitry Andric // on A->B by the probability of B->D. 2311*0b57cec5SDimitry Andric // 2312*0b57cec5SDimitry Andric // Before ifcvt: After ifcvt (assume B->D is kept): 2313*0b57cec5SDimitry Andric // 2314*0b57cec5SDimitry Andric // A A 2315*0b57cec5SDimitry Andric // /| /|\ 2316*0b57cec5SDimitry Andric // / B / B| 2317*0b57cec5SDimitry Andric // | /| | || 2318*0b57cec5SDimitry Andric // |/ | | |/ 2319*0b57cec5SDimitry Andric // C D C D 2320*0b57cec5SDimitry Andric // 2321*0b57cec5SDimitry Andric if (ToBBI.BB->isSuccessor(Succ)) 2322*0b57cec5SDimitry Andric ToBBI.BB->setSuccProbability( 2323*0b57cec5SDimitry Andric find(ToBBI.BB->successors(), Succ), 2324*0b57cec5SDimitry Andric MBPI->getEdgeProbability(ToBBI.BB, Succ) + NewProb); 2325*0b57cec5SDimitry Andric else 2326*0b57cec5SDimitry Andric ToBBI.BB->addSuccessor(Succ, NewProb); 2327*0b57cec5SDimitry Andric } 2328*0b57cec5SDimitry Andric } 2329*0b57cec5SDimitry Andric 2330*0b57cec5SDimitry Andric // Move the now empty FromMBB out of the way to the end of the function so 2331*0b57cec5SDimitry Andric // it doesn't interfere with fallthrough checks done by canFallThroughTo(). 2332*0b57cec5SDimitry Andric MachineBasicBlock *Last = &*FromMBB.getParent()->rbegin(); 2333*0b57cec5SDimitry Andric if (Last != &FromMBB) 2334*0b57cec5SDimitry Andric FromMBB.moveAfter(Last); 2335*0b57cec5SDimitry Andric 2336*0b57cec5SDimitry Andric // Normalize the probabilities of ToBBI.BB's successors with all adjustment 2337*0b57cec5SDimitry Andric // we've done above. 2338*0b57cec5SDimitry Andric if (ToBBI.IsBrAnalyzable && FromBBI.IsBrAnalyzable) 2339*0b57cec5SDimitry Andric ToBBI.BB->normalizeSuccProbs(); 2340*0b57cec5SDimitry Andric 2341*0b57cec5SDimitry Andric ToBBI.Predicate.append(FromBBI.Predicate.begin(), FromBBI.Predicate.end()); 2342*0b57cec5SDimitry Andric FromBBI.Predicate.clear(); 2343*0b57cec5SDimitry Andric 2344*0b57cec5SDimitry Andric ToBBI.NonPredSize += FromBBI.NonPredSize; 2345*0b57cec5SDimitry Andric ToBBI.ExtraCost += FromBBI.ExtraCost; 2346*0b57cec5SDimitry Andric ToBBI.ExtraCost2 += FromBBI.ExtraCost2; 2347*0b57cec5SDimitry Andric FromBBI.NonPredSize = 0; 2348*0b57cec5SDimitry Andric FromBBI.ExtraCost = 0; 2349*0b57cec5SDimitry Andric FromBBI.ExtraCost2 = 0; 2350*0b57cec5SDimitry Andric 2351*0b57cec5SDimitry Andric ToBBI.ClobbersPred |= FromBBI.ClobbersPred; 2352*0b57cec5SDimitry Andric ToBBI.HasFallThrough = FromBBI.HasFallThrough; 2353*0b57cec5SDimitry Andric ToBBI.IsAnalyzed = false; 2354*0b57cec5SDimitry Andric FromBBI.IsAnalyzed = false; 2355*0b57cec5SDimitry Andric } 2356*0b57cec5SDimitry Andric 2357*0b57cec5SDimitry Andric FunctionPass * 2358*0b57cec5SDimitry Andric llvm::createIfConverter(std::function<bool(const MachineFunction &)> Ftor) { 2359*0b57cec5SDimitry Andric return new IfConverter(std::move(Ftor)); 2360*0b57cec5SDimitry Andric } 2361