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/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" 29 using namespace llvm; 30 31 namespace llvm { 32 33 // The cluster information for a machine basic block. 34 struct BBClusterInfo { 35 // Unique ID for this basic block. 36 unsigned BBID; 37 // Cluster ID this basic block belongs to. 38 unsigned ClusterID; 39 // Position of basic block within the cluster. 40 unsigned PositionInCluster; 41 }; 42 43 using ProgramBBClusterInfoMapTy = StringMap<SmallVector<BBClusterInfo>>; 44 45 class BasicBlockSectionsProfileReader : public ImmutablePass { 46 public: 47 static char ID; 48 49 BasicBlockSectionsProfileReader(const MemoryBuffer *Buf) 50 : ImmutablePass(ID), MBuf(Buf) { 51 initializeBasicBlockSectionsProfileReaderPass( 52 *PassRegistry::getPassRegistry()); 53 }; 54 55 BasicBlockSectionsProfileReader() : ImmutablePass(ID) { 56 initializeBasicBlockSectionsProfileReaderPass( 57 *PassRegistry::getPassRegistry()); 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 // Initializes the FunctionNameToDIFilename map for the current module and 78 // then reads the profile for matching functions. 79 bool doInitialization(Module &M) override; 80 81 private: 82 StringRef getAliasName(StringRef FuncName) const { 83 auto R = FuncAliasMap.find(FuncName); 84 return R == FuncAliasMap.end() ? FuncName : R->second; 85 } 86 87 // Reads the basic block sections profile for functions in this module. 88 Error ReadProfile(); 89 90 // This contains the basic-block-sections profile. 91 const MemoryBuffer *MBuf = nullptr; 92 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; 96 97 // This encapsulates the BB cluster information for the whole program. 98 // 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 102 // cluster. 103 ProgramBBClusterInfoMapTy ProgramBBClusterInfo; 104 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; 108 }; 109 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. 113 ImmutablePass * 114 createBasicBlockSectionsProfileReaderPass(const MemoryBuffer *Buf); 115 116 } // namespace llvm 117 #endif // LLVM_CODEGEN_BASICBLOCKSECTIONSPROFILEREADER_H 118