1 //===-- BasicBlockSectionsProfileReader.h - BB sections profile reader pass ==//
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
7 //===----------------------------------------------------------------------===//
9 // This pass creates the basic block cluster info by reading the basic block
10 // sections profile. The cluster info will be used by the basic-block-sections
11 // pass to arrange basic blocks in their sections.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CODEGEN_BASICBLOCKSECTIONSPROFILEREADER_H
16 #define LLVM_CODEGEN_BASICBLOCKSECTIONSPROFILEREADER_H
18 #include "llvm/ADT/SmallSet.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/StringMap.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/InitializePasses.h"
25 #include "llvm/Pass.h"
26 #include "llvm/Support/Error.h"
27 #include "llvm/Support/LineIterator.h"
28 #include "llvm/Support/MemoryBuffer.h"
33 // The cluster information for a machine basic block.
34 struct BBClusterInfo {
35 // Unique ID for this basic block.
37 // Cluster ID this basic block belongs to.
39 // Position of basic block within the cluster.
40 unsigned PositionInCluster;
43 using ProgramBBClusterInfoMapTy = StringMap<SmallVector<BBClusterInfo>>;
45 class BasicBlockSectionsProfileReader : public ImmutablePass {
49 BasicBlockSectionsProfileReader(const MemoryBuffer *Buf)
50 : ImmutablePass(ID), MBuf(Buf) {
51 initializeBasicBlockSectionsProfileReaderPass(
52 *PassRegistry::getPassRegistry());
55 BasicBlockSectionsProfileReader() : ImmutablePass(ID) {
56 initializeBasicBlockSectionsProfileReaderPass(
57 *PassRegistry::getPassRegistry());
60 StringRef getPassName() const override {
61 return "Basic Block Sections Profile Reader";
64 // Returns true if basic block sections profile exist for function \p
66 bool isFunctionHot(StringRef FuncName) const;
68 // Returns a pair with first element representing whether basic block sections
69 // profile exist for the function \p FuncName, and the second element
70 // representing the basic block sections profile (cluster info) for this
71 // function. If the first element is true and the second element is empty, it
72 // means unique basic block sections are desired for all basic blocks of the
74 std::pair<bool, SmallVector<BBClusterInfo>>
75 getBBClusterInfoForFunction(StringRef FuncName) const;
77 // Initializes the FunctionNameToDIFilename map for the current module and
78 // then reads the profile for matching functions.
79 bool doInitialization(Module &M) override;
82 StringRef getAliasName(StringRef FuncName) const {
83 auto R = FuncAliasMap.find(FuncName);
84 return R == FuncAliasMap.end() ? FuncName : R->second;
87 // Reads the basic block sections profile for functions in this module.
90 // This contains the basic-block-sections profile.
91 const MemoryBuffer *MBuf = nullptr;
93 // Map from every function name in the module to its debug info filename or
94 // empty string if no debug info is available.
95 StringMap<SmallString<128>> FunctionNameToDIFilename;
97 // This encapsulates the BB cluster information for the whole program.
99 // For every function name, it contains the cluster information for (all or
100 // some of) its basic blocks. The cluster information for every basic block
101 // includes its cluster ID along with the position of the basic block in that
103 ProgramBBClusterInfoMapTy ProgramBBClusterInfo;
105 // Some functions have alias names. We use this map to find the main alias
106 // name for which we have mapping in ProgramBBClusterInfo.
107 StringMap<StringRef> FuncAliasMap;
110 // Creates a BasicBlockSectionsProfileReader pass to parse the basic block
111 // sections profile. \p Buf is a memory buffer that contains the list of
112 // functions and basic block ids to selectively enable basic block sections.
114 createBasicBlockSectionsProfileReaderPass(const MemoryBuffer *Buf);
117 #endif // LLVM_CODEGEN_BASICBLOCKSECTIONSPROFILEREADER_H