1 //==- MemProfContextDisambiguation.h - Context Disambiguation ----*- C++ -*-==// 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 // Implements support for context disambiguation of allocation calls for profile 10 // guided heap optimization using memprof metadata. See implementation file for 11 // details. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_TRANSFORMS_IPO_MEMPROF_CONTEXT_DISAMBIGUATION_H 16 #define LLVM_TRANSFORMS_IPO_MEMPROF_CONTEXT_DISAMBIGUATION_H 17 18 #include "llvm/Analysis/IndirectCallPromotionAnalysis.h" 19 #include "llvm/IR/GlobalValue.h" 20 #include "llvm/IR/ModuleSummaryIndex.h" 21 #include "llvm/IR/PassManager.h" 22 #include "llvm/Transforms/Utils/ValueMapper.h" 23 #include <functional> 24 25 namespace llvm { 26 class GlobalValueSummary; 27 class Module; 28 class OptimizationRemarkEmitter; 29 30 class MemProfContextDisambiguation 31 : public PassInfoMixin<MemProfContextDisambiguation> { 32 /// Run the context disambiguator on \p M, returns true if any changes made. 33 bool processModule( 34 Module &M, 35 function_ref<OptimizationRemarkEmitter &(Function *)> OREGetter); 36 37 /// In the ThinLTO backend, apply the cloning decisions in ImportSummary to 38 /// the IR. 39 bool applyImport(Module &M); 40 41 // Builds the symtab and analysis used for ICP during ThinLTO backends. 42 bool initializeIndirectCallPromotionInfo(Module &M); 43 44 // Data structure for saving indirect call profile info for use in ICP with 45 // cloning. 46 struct ICallAnalysisData { 47 CallBase *CB; 48 std::vector<InstrProfValueData> CandidateProfileData; 49 uint32_t NumCandidates; 50 uint64_t TotalCount; 51 size_t CallsiteInfoStartIndex; 52 }; 53 54 // Record information needed for ICP of an indirect call, depending on its 55 // profile information and the clone information recorded in the corresponding 56 // CallsiteInfo records. The SI iterator point to the current iteration point 57 // through AllCallsites in this function, and will be updated in this method 58 // as we iterate through profiled targets. The number of clones recorded for 59 // this indirect call is returned. The necessary information is recorded in 60 // the ICallAnalysisInfo list for later ICP. 61 unsigned recordICPInfo(CallBase *CB, ArrayRef<CallsiteInfo> AllCallsites, 62 ArrayRef<CallsiteInfo>::iterator &SI, 63 SmallVector<ICallAnalysisData> &ICallAnalysisInfo); 64 65 // Actually performs any needed ICP in the function, using the information 66 // recorded in the ICallAnalysisInfo list. 67 void performICP(Module &M, ArrayRef<CallsiteInfo> AllCallsites, 68 ArrayRef<std::unique_ptr<ValueToValueMapTy>> VMaps, 69 ArrayRef<ICallAnalysisData> ICallAnalysisInfo, 70 OptimizationRemarkEmitter &ORE); 71 72 /// Import summary containing cloning decisions for the ThinLTO backend. 73 const ModuleSummaryIndex *ImportSummary; 74 75 // Owns the import summary specified by internal options for testing the 76 // ThinLTO backend via opt (to simulate distributed ThinLTO). 77 std::unique_ptr<ModuleSummaryIndex> ImportSummaryForTesting; 78 79 // Whether we are building with SamplePGO. This is needed for correctly 80 // updating profile metadata on speculatively promoted calls. 81 bool isSamplePGO; 82 83 // Used when performing indirect call analysis and promotion when cloning in 84 // the ThinLTO backend during applyImport. 85 std::unique_ptr<InstrProfSymtab> Symtab; 86 std::unique_ptr<ICallPromotionAnalysis> ICallAnalysis; 87 88 public: 89 MemProfContextDisambiguation(const ModuleSummaryIndex *Summary = nullptr, 90 bool isSamplePGO = false); 91 92 PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM); 93 94 void run(ModuleSummaryIndex &Index, 95 function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)> 96 isPrevailing); 97 }; 98 } // end namespace llvm 99 100 #endif // LLVM_TRANSFORMS_IPO_MEMPROF_CONTEXT_DISAMBIGUATION_H 101