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,8 +99,8 @@ // HW that requires structurized CFG. bool EnableTailMerge = !MF.getTarget().requiresStructuredCFG() && PassConfig->getEnableTailMerge(); - BranchFolder Folder(EnableTailMerge, /*CommonHoist=*/true, - getAnalysis(), + MBFIWrapper MBBFreqInfo(getAnalysis()); + BranchFolder Folder(EnableTailMerge, /*CommonHoist=*/true, MBBFreqInfo, getAnalysis()); return Folder.OptimizeFunction(MF, MF.getSubtarget().getInstrInfo(), MF.getSubtarget().getRegisterInfo(), @@ -108,7 +108,7 @@ } BranchFolder::BranchFolder(bool defaultEnableTailMerge, bool CommonHoist, - const MachineBlockFrequencyInfo &FreqInfo, + MBFIWrapper &FreqInfo, const MachineBranchProbabilityInfo &ProbInfo) : EnableHoistCommonCode(CommonHoist), MBBFreqInfo(FreqInfo), MBPI(ProbInfo) { @@ -525,8 +525,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 +534,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, getBlockFreq(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. Index: lib/CodeGen/IfConversion.cpp =================================================================== --- lib/CodeGen/IfConversion.cpp +++ lib/CodeGen/IfConversion.cpp @@ -162,7 +162,6 @@ const TargetLoweringBase *TLI; const TargetInstrInfo *TII; const TargetRegisterInfo *TRI; - const MachineBlockFrequencyInfo *MBFI; const MachineBranchProbabilityInfo *MBPI; MachineRegisterInfo *MRI; @@ -290,7 +289,7 @@ TLI = ST.getTargetLowering(); TII = ST.getInstrInfo(); TRI = ST.getRegisterInfo(); - MBFI = &getAnalysis(); + MBFIWrapper MBFI(getAnalysis()); MBPI = &getAnalysis(); MRI = &MF.getRegInfo(); SchedModel.init(ST.getSchedModel(), &ST, TII); @@ -302,7 +301,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,7 +425,7 @@ 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()); }