LLVM  16.0.0git
BasicBlockSectionsProfileReader.h
Go to the documentation of this file.
1 //===-- BasicBlockSectionsProfileReader.h - BB sections profile reader pass ==//
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 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.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_CODEGEN_BASICBLOCKSECTIONSPROFILEREADER_H
16 #define LLVM_CODEGEN_BASICBLOCKSECTIONSPROFILEREADER_H
17 
18 #include "llvm/ADT/Optional.h"
19 #include "llvm/ADT/SmallSet.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/StringMap.h"
22 #include "llvm/ADT/StringRef.h"
23 #include "llvm/InitializePasses.h"
24 #include "llvm/Pass.h"
25 #include "llvm/Support/Error.h"
28 
29 using namespace llvm;
30 
31 namespace llvm {
32 
33 // The cluster information for a machine basic block.
34 struct BBClusterInfo {
35  // MachineBasicBlock ID.
36  unsigned MBBNumber;
37  // Cluster ID this basic block belongs to.
38  unsigned ClusterID;
39  // Position of basic block within the cluster.
41 };
42 
44 
46 public:
47  static char ID;
48 
50  : ImmutablePass(ID), MBuf(Buf) {
53  };
54 
58  }
59 
60  StringRef getPassName() const override {
61  return "Basic Block Sections Profile Reader";
62  }
63 
64  // Returns true if basic block sections profile exist for function \p
65  // FuncName.
66  bool isFunctionHot(StringRef FuncName) const;
67 
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
73  // function.
74  std::pair<bool, SmallVector<BBClusterInfo>>
75  getBBClusterInfoForFunction(StringRef FuncName) const;
76 
77  /// Read profiles of basic blocks if available here.
78  void initializePass() override;
79 
80 private:
81  StringRef getAliasName(StringRef FuncName) const {
82  auto R = FuncAliasMap.find(FuncName);
83  return R == FuncAliasMap.end() ? FuncName : R->second;
84  }
85 
86  // This contains the basic-block-sections profile.
87  const MemoryBuffer *MBuf = nullptr;
88 
89  // This encapsulates the BB cluster information for the whole program.
90  //
91  // For every function name, it contains the cluster information for (all or
92  // some of) its basic blocks. The cluster information for every basic block
93  // includes its cluster ID along with the position of the basic block in that
94  // cluster.
95  ProgramBBClusterInfoMapTy ProgramBBClusterInfo;
96 
97  // Some functions have alias names. We use this map to find the main alias
98  // name for which we have mapping in ProgramBBClusterInfo.
99  StringMap<StringRef> FuncAliasMap;
100 };
101 
102 // Creates a BasicBlockSectionsProfileReader pass to parse the basic block
103 // sections profile. \p Buf is a memory buffer that contains the list of
104 // functions and basic block ids to selectively enable basic block sections.
107 
108 } // namespace llvm
109 #endif // LLVM_CODEGEN_BASICBLOCKSECTIONSPROFILEREADER_H
MemoryBuffer.h
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Optional.h
llvm::BasicBlockSectionsProfileReader::BasicBlockSectionsProfileReader
BasicBlockSectionsProfileReader(const MemoryBuffer *Buf)
Definition: BasicBlockSectionsProfileReader.h:49
llvm::ImmutablePass
ImmutablePass class - This class is used to provide information that does not need to be run.
Definition: Pass.h:279
llvm::initializeBasicBlockSectionsProfileReaderPass
void initializeBasicBlockSectionsProfileReaderPass(PassRegistry &)
StringRef.h
Pass.h
llvm::BasicBlockSectionsProfileReader::getPassName
StringRef getPassName() const override
getPassName - Return a nice clean name for a pass.
Definition: BasicBlockSectionsProfileReader.h:60
Error.h
llvm::BasicBlockSectionsProfileReader::BasicBlockSectionsProfileReader
BasicBlockSectionsProfileReader()
Definition: BasicBlockSectionsProfileReader.h:55
llvm::MemoryBuffer
This interface provides simple read-only access to a block of memory, and provides simple methods for...
Definition: MemoryBuffer.h:51
llvm::createBasicBlockSectionsProfileReaderPass
ImmutablePass * createBasicBlockSectionsProfileReaderPass(const MemoryBuffer *Buf)
Definition: BasicBlockSectionsProfileReader.cpp:142
llvm::PassRegistry::getPassRegistry
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
Definition: PassRegistry.cpp:24
llvm::BBClusterInfo::ClusterID
unsigned ClusterID
Definition: BasicBlockSectionsProfileReader.h:38
llvm::BasicBlockSectionsProfileReader::ID
static char ID
Definition: BasicBlockSectionsProfileReader.h:47
LineIterator.h
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
llvm::StringMap< SmallVector< BBClusterInfo > >
llvm::BasicBlockSectionsProfileReader::isFunctionHot
bool isFunctionHot(StringRef FuncName) const
llvm::BasicBlockSectionsProfileReader::initializePass
void initializePass() override
Read profiles of basic blocks if available here.
Definition: BasicBlockSectionsProfileReader.cpp:134
llvm::BBClusterInfo
Definition: BasicBlockSectionsProfileReader.h:34
llvm::BasicBlockSectionsProfileReader
Definition: BasicBlockSectionsProfileReader.h:45
llvm::BBClusterInfo::PositionInCluster
unsigned PositionInCluster
Definition: BasicBlockSectionsProfileReader.h:40
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::BasicBlockSectionsProfileReader::getBBClusterInfoForFunction
std::pair< bool, SmallVector< BBClusterInfo > > getBBClusterInfoForFunction(StringRef FuncName) const
llvm::BBClusterInfo::MBBNumber
unsigned MBBNumber
Definition: BasicBlockSectionsProfileReader.h:36
SmallVector.h
StringMap.h
InitializePasses.h
SmallSet.h