Index: lib/CodeGen/BranchFolding.h =================================================================== --- lib/CodeGen/BranchFolding.h +++ lib/CodeGen/BranchFolding.h @@ -24,10 +24,27 @@ class TargetInstrInfo; class TargetRegisterInfo; + /// \brief This class keeps track of branch frequencies of newly created + /// blocks and tail-merged blocks. + class MBFIWrapper { + public: + MBFIWrapper(const MachineBlockFrequencyInfo &I) : MBFI(I) {} + BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const; + void setBlockFreq(const MachineBasicBlock *MBB, BlockFrequency F); + raw_ostream &printBlockFreq(raw_ostream &OS, + const MachineBasicBlock *MBB) const; + raw_ostream &printBlockFreq(raw_ostream &OS, + const BlockFrequency Freq) const; + + private: + const MachineBlockFrequencyInfo &MBFI; + DenseMap MergedBBFreq; + }; + class LLVM_LIBRARY_VISIBILITY BranchFolder { public: explicit BranchFolder(bool defaultEnableTailMerge, bool CommonHoist, - const MachineBlockFrequencyInfo &MBFI, + MBFIWrapper *MBFI, const MachineBranchProbabilityInfo &MBPI); bool OptimizeFunction(MachineFunction &MF, @@ -98,20 +115,7 @@ MachineModuleInfo *MMI; RegScavenger *RS; - /// \brief This class keeps track of branch frequencies of newly created - /// blocks and tail-merged blocks. - class MBFIWrapper { - public: - MBFIWrapper(const MachineBlockFrequencyInfo &I) : MBFI(I) {} - BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const; - void setBlockFreq(const MachineBasicBlock *MBB, BlockFrequency F); - - private: - const MachineBlockFrequencyInfo &MBFI; - DenseMap MergedBBFreq; - }; - - MBFIWrapper MBBFreqInfo; + MBFIWrapper *MBBFreqInfo; const MachineBranchProbabilityInfo &MBPI; bool TailMergeBlocks(MachineFunction &MF); Index: lib/CodeGen/BranchFolding.cpp =================================================================== --- lib/CodeGen/BranchFolding.cpp +++ lib/CodeGen/BranchFolding.cpp @@ -99,16 +99,20 @@ // HW that requires structurized CFG. bool EnableTailMerge = !MF.getTarget().requiresStructuredCFG() && PassConfig->getEnableTailMerge(); - BranchFolder Folder(EnableTailMerge, /*CommonHoist=*/true, - getAnalysis(), + MBFIWrapper *MBBFreqInfo = + new MBFIWrapper(getAnalysis()); + BranchFolder Folder(EnableTailMerge, /*CommonHoist=*/true, MBBFreqInfo, getAnalysis()); - return Folder.OptimizeFunction(MF, MF.getSubtarget().getInstrInfo(), - MF.getSubtarget().getRegisterInfo(), - getAnalysisIfAvailable()); + bool Change = Folder.OptimizeFunction( + MF, MF.getSubtarget().getInstrInfo(), MF.getSubtarget().getRegisterInfo(), + getAnalysisIfAvailable()); + + delete MBBFreqInfo; + return Change; } BranchFolder::BranchFolder(bool defaultEnableTailMerge, bool CommonHoist, - const MachineBlockFrequencyInfo &FreqInfo, + MBFIWrapper *FreqInfo, const MachineBranchProbabilityInfo &ProbInfo) : EnableHoistCommonCode(CommonHoist), MBBFreqInfo(FreqInfo), MBPI(ProbInfo) { @@ -447,7 +451,7 @@ NewMBB->splice(NewMBB->end(), &CurMBB, BBI1, CurMBB.end()); // NewMBB inherits CurMBB's block frequency. - MBBFreqInfo.setBlockFreq(NewMBB, MBBFreqInfo.getBlockFreq(&CurMBB)); + MBBFreqInfo->setBlockFreq(NewMBB, MBBFreqInfo->getBlockFreq(&CurMBB)); // For targets that use the register scavenger, we must maintain LiveIns. MaintainLiveIns(&CurMBB, NewMBB); @@ -525,8 +529,7 @@ #endif } -BlockFrequency -BranchFolder::MBFIWrapper::getBlockFreq(const MachineBasicBlock *MBB) const { +BlockFrequency MBFIWrapper::getBlockFreq(const MachineBasicBlock *MBB) const { auto I = MergedBBFreq.find(MBB); if (I != MergedBBFreq.end()) @@ -535,11 +538,20 @@ return MBFI.getBlockFreq(MBB); } -void BranchFolder::MBFIWrapper::setBlockFreq(const MachineBasicBlock *MBB, - BlockFrequency F) { +void MBFIWrapper::setBlockFreq(const MachineBasicBlock *MBB, BlockFrequency F) { MergedBBFreq[MBB] = F; } +raw_ostream &MBFIWrapper::printBlockFreq(raw_ostream &OS, + const MachineBasicBlock *MBB) const { + return MBFI.printBlockFreq(OS, MBB); +} + +raw_ostream &MBFIWrapper::printBlockFreq(raw_ostream &OS, + const BlockFrequency Freq) const { + return MBFI.printBlockFreq(OS, Freq); +} + /// CountTerminators - Count the number of terminators in the given /// block and set I to the position of the first non-terminator, if there /// is one, or MBB->end() otherwise. @@ -1067,7 +1079,7 @@ // where bb is a basic block that is in SameTails. for (const auto &Src : SameTails) { const MachineBasicBlock *SrcMBB = Src.getBlock(); - BlockFrequency BlockFreq = MBBFreqInfo.getBlockFreq(SrcMBB); + BlockFrequency BlockFreq = MBBFreqInfo->getBlockFreq(SrcMBB); AccumulatedMBBFreq += BlockFreq; // It is not necessary to recompute edge weights if TailBB has less than two @@ -1082,7 +1094,7 @@ *EdgeFreq += BlockFreq * MBPI.getEdgeProbability(SrcMBB, *SuccI); } - MBBFreqInfo.setBlockFreq(&TailMBB, AccumulatedMBBFreq); + MBBFreqInfo->setBlockFreq(&TailMBB, AccumulatedMBBFreq); if (TailMBB.succ_size() <= 1) return; Index: lib/CodeGen/IfConversion.cpp =================================================================== --- lib/CodeGen/IfConversion.cpp +++ lib/CodeGen/IfConversion.cpp @@ -162,7 +162,7 @@ const TargetLoweringBase *TLI; const TargetInstrInfo *TII; const TargetRegisterInfo *TRI; - const MachineBlockFrequencyInfo *MBFI; + MBFIWrapper *MBFI; const MachineBranchProbabilityInfo *MBPI; MachineRegisterInfo *MRI; @@ -290,7 +290,7 @@ TLI = ST.getTargetLowering(); TII = ST.getInstrInfo(); TRI = ST.getRegisterInfo(); - MBFI = &getAnalysis(); + MBFI = new MBFIWrapper(getAnalysis()); MBPI = &getAnalysis(); MRI = &MF.getRegInfo(); SchedModel.init(ST.getSchedModel(), &ST, TII); @@ -302,7 +302,7 @@ bool BFChange = false; if (!PreRegAlloc) { // Tail merge tend to expose more if-conversion opportunities. - BranchFolder BF(true, false, *MBFI, *MBPI); + BranchFolder BF(true, false, MBFI, *MBPI); BFChange = BF.OptimizeFunction(MF, TII, ST.getRegisterInfo(), getAnalysisIfAvailable()); } @@ -426,11 +426,12 @@ BBAnalysis.clear(); if (MadeChange && IfCvtBranchFold) { - BranchFolder BF(false, false, *MBFI, *MBPI); + BranchFolder BF(false, false, MBFI, *MBPI); BF.OptimizeFunction(MF, TII, MF.getSubtarget().getRegisterInfo(), getAnalysisIfAvailable()); } + delete MBFI; MadeChange |= BFChange; return MadeChange; }