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