Skip to content

Commit 2c03c89

Browse files
committed
[MBFI] Move BranchFolding::MBFIWrapper to its own files. NFC.
Summary: To avoid header file circular dependency issues in passing updated MBFI (in MBFIWrapper) to the interface of profile guided size optimizations. A prep step for (and split off of) D73381. Reviewers: davidxl Subscribers: mgorny, hiraditya, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D73494
1 parent b8d9ac0 commit 2c03c89

File tree

7 files changed

+102
-65
lines changed

7 files changed

+102
-65
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//===- llvm/CodeGen/MBFIWrapper.h -------------------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This class keeps track of branch frequencies of newly created blocks and
10+
// tail-merged blocks. Used by the TailDuplication and MachineBlockPlacement.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef LLVM_CODEGEN_MBFIWRAPPER_H
15+
#define LLVM_CODEGEN_MBFIWRAPPER_H
16+
17+
#include "llvm/ADT/DenseMap.h"
18+
#include "llvm/Support/BlockFrequency.h"
19+
20+
namespace llvm {
21+
22+
class MachineBasicBlock;
23+
class MachineBlockFrequencyInfo;
24+
25+
class MBFIWrapper {
26+
public:
27+
MBFIWrapper(const MachineBlockFrequencyInfo &I) : MBFI(I) {}
28+
29+
BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const;
30+
void setBlockFreq(const MachineBasicBlock *MBB, BlockFrequency F);
31+
raw_ostream &printBlockFreq(raw_ostream &OS,
32+
const MachineBasicBlock *MBB) const;
33+
raw_ostream &printBlockFreq(raw_ostream &OS,
34+
const BlockFrequency Freq) const;
35+
void view(const Twine &Name, bool isSimple = true);
36+
uint64_t getEntryFreq() const;
37+
const MachineBlockFrequencyInfo &getMBFI() { return MBFI; }
38+
39+
private:
40+
const MachineBlockFrequencyInfo &MBFI;
41+
DenseMap<const MachineBasicBlock *, BlockFrequency> MergedBBFreq;
42+
};
43+
44+
} // end namespace llvm
45+
46+
#endif // LLVM_CODEGEN_MBFIWRAPPER_H

llvm/lib/CodeGen/BranchFolding.cpp

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ bool BranchFolderPass::runOnMachineFunction(MachineFunction &MF) {
129129
// HW that requires structurized CFG.
130130
bool EnableTailMerge = !MF.getTarget().requiresStructuredCFG() &&
131131
PassConfig->getEnableTailMerge();
132-
BranchFolder::MBFIWrapper MBBFreqInfo(
132+
MBFIWrapper MBBFreqInfo(
133133
getAnalysis<MachineBlockFrequencyInfo>());
134134
BranchFolder Folder(EnableTailMerge, /*CommonHoist=*/true, MBBFreqInfo,
135135
getAnalysis<MachineBranchProbabilityInfo>(),
@@ -501,42 +501,6 @@ BranchFolder::MergePotentialsElt::operator<(const MergePotentialsElt &o) const {
501501
#endif
502502
}
503503

504-
BlockFrequency
505-
BranchFolder::MBFIWrapper::getBlockFreq(const MachineBasicBlock *MBB) const {
506-
auto I = MergedBBFreq.find(MBB);
507-
508-
if (I != MergedBBFreq.end())
509-
return I->second;
510-
511-
return MBFI.getBlockFreq(MBB);
512-
}
513-
514-
void BranchFolder::MBFIWrapper::setBlockFreq(const MachineBasicBlock *MBB,
515-
BlockFrequency F) {
516-
MergedBBFreq[MBB] = F;
517-
}
518-
519-
raw_ostream &
520-
BranchFolder::MBFIWrapper::printBlockFreq(raw_ostream &OS,
521-
const MachineBasicBlock *MBB) const {
522-
return MBFI.printBlockFreq(OS, getBlockFreq(MBB));
523-
}
524-
525-
raw_ostream &
526-
BranchFolder::MBFIWrapper::printBlockFreq(raw_ostream &OS,
527-
const BlockFrequency Freq) const {
528-
return MBFI.printBlockFreq(OS, Freq);
529-
}
530-
531-
void BranchFolder::MBFIWrapper::view(const Twine &Name, bool isSimple) {
532-
MBFI.view(Name, isSimple);
533-
}
534-
535-
uint64_t
536-
BranchFolder::MBFIWrapper::getEntryFreq() const {
537-
return MBFI.getEntryFreq();
538-
}
539-
540504
/// CountTerminators - Count the number of terminators in the given
541505
/// block and set I to the position of the first non-terminator, if there
542506
/// is one, or MBB->end() otherwise.
@@ -591,7 +555,7 @@ ProfitableToMerge(MachineBasicBlock *MBB1, MachineBasicBlock *MBB2,
591555
MachineBasicBlock *PredBB,
592556
DenseMap<const MachineBasicBlock *, int> &EHScopeMembership,
593557
bool AfterPlacement,
594-
BranchFolder::MBFIWrapper &MBBFreqInfo,
558+
MBFIWrapper &MBBFreqInfo,
595559
ProfileSummaryInfo *PSI) {
596560
// It is never profitable to tail-merge blocks from two different EH scopes.
597561
if (!EHScopeMembership.empty()) {

llvm/lib/CodeGen/BranchFolding.h

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "llvm/ADT/SmallPtrSet.h"
1414
#include "llvm/CodeGen/LivePhysRegs.h"
1515
#include "llvm/CodeGen/MachineBasicBlock.h"
16+
#include "llvm/CodeGen/MBFIWrapper.h"
1617
#include "llvm/Support/BlockFrequency.h"
1718
#include "llvm/Support/Compiler.h"
1819
#include <cstdint>
@@ -34,8 +35,6 @@ class TargetRegisterInfo;
3435

3536
class LLVM_LIBRARY_VISIBILITY BranchFolder {
3637
public:
37-
class MBFIWrapper;
38-
3938
explicit BranchFolder(bool defaultEnableTailMerge,
4039
bool CommonHoist,
4140
MBFIWrapper &FreqInfo,
@@ -132,28 +131,6 @@ class TargetRegisterInfo;
132131
MachineLoopInfo *MLI;
133132
LivePhysRegs LiveRegs;
134133

135-
public:
136-
/// This class keeps track of branch frequencies of newly created
137-
/// blocks and tail-merged blocks.
138-
class MBFIWrapper {
139-
public:
140-
MBFIWrapper(const MachineBlockFrequencyInfo &I) : MBFI(I) {}
141-
142-
BlockFrequency getBlockFreq(const MachineBasicBlock *MBB) const;
143-
void setBlockFreq(const MachineBasicBlock *MBB, BlockFrequency F);
144-
raw_ostream &printBlockFreq(raw_ostream &OS,
145-
const MachineBasicBlock *MBB) const;
146-
raw_ostream &printBlockFreq(raw_ostream &OS,
147-
const BlockFrequency Freq) const;
148-
void view(const Twine &Name, bool isSimple = true);
149-
uint64_t getEntryFreq() const;
150-
const MachineBlockFrequencyInfo &getMBFI() { return MBFI; }
151-
152-
private:
153-
const MachineBlockFrequencyInfo &MBFI;
154-
DenseMap<const MachineBasicBlock *, BlockFrequency> MergedBBFreq;
155-
};
156-
157134
private:
158135
MBFIWrapper &MBBFreqInfo;
159136
const MachineBranchProbabilityInfo &MBPI;

llvm/lib/CodeGen/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ add_llvm_component_library(LLVMCodeGen
9999
MachineVerifier.cpp
100100
ModuloSchedule.cpp
101101
PatchableFunction.cpp
102+
MBFIWrapper.cpp
102103
MIRPrinter.cpp
103104
MIRPrintingPass.cpp
104105
MacroFusion.cpp

llvm/lib/CodeGen/IfConversion.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ bool IfConverter::runOnMachineFunction(MachineFunction &MF) {
447447
TLI = ST.getTargetLowering();
448448
TII = ST.getInstrInfo();
449449
TRI = ST.getRegisterInfo();
450-
BranchFolder::MBFIWrapper MBFI(getAnalysis<MachineBlockFrequencyInfo>());
450+
MBFIWrapper MBFI(getAnalysis<MachineBlockFrequencyInfo>());
451451
MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
452452
ProfileSummaryInfo *PSI =
453453
&getAnalysis<ProfileSummaryInfoWrapperPass>().getPSI();

llvm/lib/CodeGen/MBFIWrapper.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//===- MBFIWrapper.cpp - MachineBlockFrequencyInfo wrapper ----------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This class keeps track of branch frequencies of newly created blocks and
10+
// tail-merged blocks. Used by the TailDuplication and MachineBlockPlacement.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#include "llvm/CodeGen/MBFIWrapper.h"
15+
#include "llvm/CodeGen/MachineBlockFrequencyInfo.h"
16+
17+
using namespace llvm;
18+
19+
BlockFrequency MBFIWrapper::getBlockFreq(const MachineBasicBlock *MBB) const {
20+
auto I = MergedBBFreq.find(MBB);
21+
22+
if (I != MergedBBFreq.end())
23+
return I->second;
24+
25+
return MBFI.getBlockFreq(MBB);
26+
}
27+
28+
void MBFIWrapper::setBlockFreq(const MachineBasicBlock *MBB,
29+
BlockFrequency F) {
30+
MergedBBFreq[MBB] = F;
31+
}
32+
33+
raw_ostream & MBFIWrapper::printBlockFreq(raw_ostream &OS,
34+
const MachineBasicBlock *MBB) const {
35+
return MBFI.printBlockFreq(OS, getBlockFreq(MBB));
36+
}
37+
38+
raw_ostream & MBFIWrapper::printBlockFreq(raw_ostream &OS,
39+
const BlockFrequency Freq) const {
40+
return MBFI.printBlockFreq(OS, Freq);
41+
}
42+
43+
void MBFIWrapper::view(const Twine &Name, bool isSimple) {
44+
MBFI.view(Name, isSimple);
45+
}
46+
47+
uint64_t MBFIWrapper::getEntryFreq() const {
48+
return MBFI.getEntryFreq();
49+
}

llvm/lib/CodeGen/MachineBlockPlacement.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ class MachineBlockPlacement : public MachineFunctionPass {
346346
const MachineBranchProbabilityInfo *MBPI;
347347

348348
/// A handle to the function-wide block frequency pass.
349-
std::unique_ptr<BranchFolder::MBFIWrapper> MBFI;
349+
std::unique_ptr<MBFIWrapper> MBFI;
350350

351351
/// A handle to the loop info.
352352
MachineLoopInfo *MLI;
@@ -3046,7 +3046,7 @@ bool MachineBlockPlacement::runOnMachineFunction(MachineFunction &MF) {
30463046

30473047
F = &MF;
30483048
MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
3049-
MBFI = std::make_unique<BranchFolder::MBFIWrapper>(
3049+
MBFI = std::make_unique<MBFIWrapper>(
30503050
getAnalysis<MachineBlockFrequencyInfo>());
30513051
MLI = &getAnalysis<MachineLoopInfo>();
30523052
TII = MF.getSubtarget().getInstrInfo();

0 commit comments

Comments
 (0)