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/SmallVector.h" 20 #include "llvm/ADT/StringMap.h" 21 #include "llvm/ADT/StringRef.h" 22 #include "llvm/InitializePasses.h" 23 #include "llvm/Pass.h" 24 #include "llvm/Support/Error.h" 25 #include "llvm/Support/LineIterator.h" 26 #include "llvm/Support/MemoryBuffer.h" 27 28 using namespace llvm; 29 30 namespace llvm { 31 32 // The cluster information for a machine basic block. 33 struct BBClusterInfo { 34 // Unique ID for this basic block. 35 unsigned BBID; 36 // Cluster ID this basic block belongs to. 37 unsigned ClusterID; 38 // Position of basic block within the cluster. 39 unsigned PositionInCluster; 40 }; 41 42 using ProgramBBClusterInfoMapTy = StringMap<SmallVector<BBClusterInfo>>; 43 44 class BasicBlockSectionsProfileReader : public ImmutablePass { 45 public: 46 static char ID; 47 48 BasicBlockSectionsProfileReader(const MemoryBuffer *Buf) 49 : ImmutablePass(ID), MBuf(Buf) { 50 initializeBasicBlockSectionsProfileReaderPass( 51 *PassRegistry::getPassRegistry()); 52 }; 53 54 BasicBlockSectionsProfileReader() : ImmutablePass(ID) { 55 initializeBasicBlockSectionsProfileReaderPass( 56 *PassRegistry::getPassRegistry()); 57 } 58 59 StringRef getPassName() const override { 60 return "Basic Block Sections Profile Reader"; 61 } 62 63 // Returns true if basic block sections profile exist for function \p 64 // FuncName. 65 bool isFunctionHot(StringRef FuncName) const; 66 67 // Returns a pair with first element representing whether basic block sections 68 // profile exist for the function \p FuncName, and the second element 69 // representing the basic block sections profile (cluster info) for this 70 // function. If the first element is true and the second element is empty, it 71 // means unique basic block sections are desired for all basic blocks of the 72 // function. 73 std::pair<bool, SmallVector<BBClusterInfo>> 74 getBBClusterInfoForFunction(StringRef FuncName) const; 75 76 /// Read profiles of basic blocks if available here. 77 void initializePass() override; 78 79 private: 80 StringRef getAliasName(StringRef FuncName) const { 81 auto R = FuncAliasMap.find(FuncName); 82 return R == FuncAliasMap.end() ? FuncName : R->second; 83 } 84 85 // This contains the basic-block-sections profile. 86 const MemoryBuffer *MBuf = nullptr; 87 88 // This encapsulates the BB cluster information for the whole program. 89 // 90 // For every function name, it contains the cluster information for (all or 91 // some of) its basic blocks. The cluster information for every basic block 92 // includes its cluster ID along with the position of the basic block in that 93 // cluster. 94 ProgramBBClusterInfoMapTy ProgramBBClusterInfo; 95 96 // Some functions have alias names. We use this map to find the main alias 97 // name for which we have mapping in ProgramBBClusterInfo. 98 StringMap<StringRef> FuncAliasMap; 99 }; 100 101 // Creates a BasicBlockSectionsProfileReader pass to parse the basic block 102 // sections profile. \p Buf is a memory buffer that contains the list of 103 // functions and basic block ids to selectively enable basic block sections. 104 ImmutablePass * 105 createBasicBlockSectionsProfileReaderPass(const MemoryBuffer *Buf); 106 107 } // namespace llvm 108 #endif // LLVM_CODEGEN_BASICBLOCKSECTIONSPROFILEREADER_H 109