95 "bbsections-cold-text-prefix",
96 cl::desc(
"The text prefix to use for cold basic block clusters"),
100 "bbsections-detect-source-drift",
101 cl::desc(
"This checks if there is a fdo instr. profile hash "
102 "mismatch for this function"),
115 StringRef getPassName()
const override {
116 return "Basic Block Sections Analysis";
119 void getAnalysisUsage(AnalysisUsage &AU)
const override;
123 bool runOnMachineFunction(MachineFunction &MF)
override;
126 bool handleBBSections(MachineFunction &MF);
127 bool handleBBAddrMap(MachineFunction &MF);
132char BasicBlockSections::ID = 0;
134 BasicBlockSections,
"bbsections-prepare",
135 "Prepares for basic block sections, by splitting functions "
136 "into clusters of basic blocks.",
140 "Prepares for basic block sections, by splitting functions "
141 "into clusters of basic blocks.",
151 for (
auto &
MBB : MF) {
152 auto NextMBBI = std::next(
MBB.getIterator());
153 auto *FTMBB = PreLayoutFallThroughs[
MBB.getNumber()];
160 if (FTMBB && (
MBB.isEndSection() || &*NextMBBI != FTMBB))
161 TII->insertUnconditionalBranch(
MBB, FTMBB,
MBB.findBranchDebugLoc());
165 if (
MBB.isEndSection())
174 MBB.updateTerminator(FTMBB);
187 return BBClusterInfos;
188 auto BlockWeights = OptWeightInfo->BlockWeights;
189 auto EdgeWeights = OptWeightInfo->EdgeWeights;
192 if (MF.
size() <= 2) {
193 for (
auto &
MBB : MF) {
194 if (
MBB.isEntryBlock() || BlockWeights[&
MBB] > 0) {
201 std::vector<const MachineBasicBlock *> OrigOrder;
202 OrigOrder.reserve(MF.
size());
209 for (
auto &
MBB : MF) {
212 int NumInsts = std::distance(NonDbgInsts.begin(), NonDbgInsts.end());
213 BlockSizes[
MBB.getNumber()] = 4 * NumInsts;
214 BlockCounts[
MBB.getNumber()] = BlockWeights[&
MBB];
215 OrigOrder.push_back(&
MBB);
219 for (
auto &
MBB : MF) {
220 for (
auto *Succ :
MBB.successors()) {
221 auto EdgeWeight = EdgeWeights[std::make_pair(&
MBB, Succ)];
223 static_cast<uint64_t>(Succ->getNumber()),
229 auto Result = computeExtTspLayout(BlockSizes, BlockCounts, JumpCounts);
231 auto Block = OrigOrder[R];
232 if (
Block->isEntryBlock() || BlockWeights[
Block] > 0)
238 if (!HotMBBs.
empty()) {
239 unsigned CurrentPosition = 0;
240 for (
auto &
MBB : HotMBBs) {
241 if (
MBB->getBBID()) {
242 BBClusterInfos.
push_back({*(
MBB->getBBID()), 0, CurrentPosition++});
246 return BBClusterInfos;
265 std::optional<MBBSectionID> EHPadsSectionID;
267 for (
auto &
MBB : MF) {
276 MBB.setSectionID(
MBB.getNumber());
278 auto I = FuncClusterInfo.
find(*
MBB.getBBID());
279 if (
I != FuncClusterInfo.
end()) {
280 MBB.setSectionID(
I->second.ClusterID);
283 *
MBB.getParent()->getSubtarget().getInstrInfo();
285 if (
TII.isMBBSafeToSplitToCold(
MBB)) {
293 if (
MBB.isEHPad() && EHPadsSectionID !=
MBB.getSectionID() &&
299 :
MBB.getSectionID();
308 MBB.setSectionID(*EHPadsSectionID);
316 PreLayoutFallThroughs[
MBB.getNumber()] =
317 MBB.getFallThrough(
false);
321 "Entry block should not be displaced by basic block sections");
337 std::optional<MBBSectionID> CurrentSection;
339 if (
MBB.empty() ||
MBB.getSectionID() == CurrentSection)
341 CurrentSection =
MBB.getSectionID();
345 for (
auto &
MBB : MF) {
346 if (IsFirstNonEmptyBBInSection(
MBB) &&
MBB.isEHPad()) {
348 while (!
MI->isEHLabel())
359 const char MetadataName[] =
"instr_prof_hash_mismatch";
363 for (
const auto &
N : Tuple->operands())
364 if (
N.equalsStr(MetadataName))
391 if (
auto *BMI = getAnalysisIfAvailable<BasicBlockMatchingAndInference>()) {
394 ClusterInfo = getAnalysis<BasicBlockSectionsProfileReaderWrapperPass>()
395 .getClusterInfoForFunction(MF.
getName());
400 FuncClusterInfo.
try_emplace(BBClusterInfo.BBID, BBClusterInfo);
411 const MachineBasicBlock &EntryBB = MF.
front();
420 auto MBBSectionOrder = [EntryBBSectionID](
const MBBSectionID &
LHS,
421 const MBBSectionID &
RHS) {
424 if (
LHS == EntryBBSectionID ||
RHS == EntryBBSectionID)
425 return LHS == EntryBBSectionID;
435 auto Comparator = [&](
const MachineBasicBlock &
X,
436 const MachineBasicBlock &
Y) {
437 auto XSectionID =
X.getSectionID();
438 auto YSectionID =
Y.getSectionID();
439 if (XSectionID != YSectionID)
440 return MBBSectionOrder(XSectionID, YSectionID);
442 if (&
X == &EntryBB || &
Y == &EntryBB)
443 return &
X == &EntryBB;
446 if (XSectionID.Type == MBBSectionID::SectionType::Default)
447 return FuncClusterInfo.
lookup(*
X.getBBID()).PositionInCluster <
448 FuncClusterInfo.
lookup(*
Y.getBBID()).PositionInCluster;
449 return X.getNumber() <
Y.getNumber();
461bool BasicBlockSections::handleBBAddrMap(MachineFunction &MF) {
468bool BasicBlockSections::runOnMachineFunction(MachineFunction &MF) {
470 auto R1 = handleBBSections(MF);
472 auto R2 = handleBBAddrMap(MF);
475 if (
auto *WP = getAnalysisIfAvailable<MachineDominatorTreeWrapperPass>())
476 WP->getDomTree().updateBlockNumbers();
477 if (
auto *WP = getAnalysisIfAvailable<MachinePostDominatorTreeWrapperPass>())
478 WP->getPostDomTree().updateBlockNumbers();
483void BasicBlockSections::getAnalysisUsage(AnalysisUsage &AU)
const {
485 AU.
addRequired<BasicBlockSectionsProfileReaderWrapperPass>();
493 return new BasicBlockSections();
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static void assignSections(MachineFunction &MF, const DenseMap< UniqueBBID, BBClusterInfo > &FuncClusterInfo)
static cl::opt< bool > BBSectionsDetectSourceDrift("bbsections-detect-source-drift", cl::desc("This checks if there is a fdo instr. profile hash " "mismatch for this function"), cl::init(true), cl::Hidden)
bbsections Prepares for basic block by splitting functions into clusters of basic static false void updateBranches(MachineFunction &MF, const SmallVector< MachineBasicBlock * > &PreLayoutFallThroughs)
static SmallVector< BBClusterInfo > createBBClusterInfoForFunction(MachineFunction &MF, const BasicBlockMatchingAndInference &BMI)
Declares methods and data structures for code layout algorithms.
const HexagonInstrInfo * TII
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
const SmallVectorImpl< MachineOperand > MachineBasicBlock * TBB
const SmallVectorImpl< MachineOperand > & Cond
This file defines the SmallVector class.
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
AnalysisUsage & addUsedIfAvailable()
Add the specified Pass class to the set of analyses used by this pass.
AnalysisUsage & addRequired()
void setPreservesAll()
Set by analyses that do not transform their input at all.
std::optional< WeightInfo > getWeightInfo(StringRef FuncName) const
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
iterator find(const_arg_type_t< KeyT > Val)
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
MDNode * getMetadata(unsigned KindID) const
Get the current metadata attachments for the given kind, if any.
MBBSectionID getSectionID() const
Returns the section ID of this basic block.
MachineInstrBundleIterator< MachineInstr > iterator
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
void setBBSectionsType(BasicBlockSection V)
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
bool hasBBSections() const
Returns true if this function has basic block sections enabled.
Function & getFunction()
Return the LLVM function that this machine code represents.
unsigned getNumBlockIDs() const
getNumBlockIDs - Return the number of MBB ID's allocated.
void RenumberBlocks(MachineBasicBlock *MBBFrom=nullptr)
RenumberBlocks - This discards all of the MachineBasicBlock numbers and recomputes them.
const MachineBasicBlock & front() const
void assignBeginEndSections()
Assign IsBeginSection IsEndSection fields for basic blocks in this function.
const TargetMachine & getTarget() const
getTarget - Return the target machine this machine code is compiled with
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
TargetInstrInfo - Interface to description of machine instruction set.
virtual void insertNoop(MachineBasicBlock &MBB, MachineBasicBlock::iterator MI) const
Insert a noop into the instruction stream at the specified point.
llvm::BasicBlockSection getBBSectionsType() const
If basic blocks should be emitted into their own section, corresponding to -fbasic-block-sections.
virtual const TargetInstrInfo * getInstrInfo() const
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI MachineFunctionPass * createBasicBlockSectionsPass()
createBasicBlockSections Pass - This pass assigns sections to machine basic blocks and is enabled wit...
auto instructionsWithoutDebug(IterT It, IterT End, bool SkipPseudoOp=true)
Construct a range iterator which begins at It and moves forwards until End is reached,...
bool hasInstrProfHashMismatch(MachineFunction &MF)
This checks if the source of this function has drifted since this binary was profiled previously.
SmallPtrSet< SUnit *, 8 > ClusterInfo
Keep record of which SUnit are in the same cluster group.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
void avoidZeroOffsetLandingPad(MachineFunction &MF)
cl::opt< std::string > BBSectionsColdTextPrefix
function_ref< bool(const MachineBasicBlock &, const MachineBasicBlock &)> MachineBasicBlockComparator
void sortBasicBlocksAndUpdateBranches(MachineFunction &MF, MachineBasicBlockComparator MBBCmp)
LLVM_ABI static const MBBSectionID ExceptionSectionID
LLVM_ABI static const MBBSectionID ColdSectionID