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" 26 #include "llvm/Support/LineIterator.h" 27 #include "llvm/Support/MemoryBuffer.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. 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 /// 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. 105 ImmutablePass * 106 createBasicBlockSectionsProfileReaderPass(const MemoryBuffer *Buf); 107 108 } // namespace llvm 109 #endif // LLVM_CODEGEN_BASICBLOCKSECTIONSPROFILEREADER_H 110