1f6c34de1SBardia Mahjour //===- ValueProfileCollector.h - determine what to value profile ----------===// 2f6c34de1SBardia Mahjour // 3f6c34de1SBardia Mahjour // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4f6c34de1SBardia Mahjour // See https://llvm.org/LICENSE.txt for license information. 5f6c34de1SBardia Mahjour // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6f6c34de1SBardia Mahjour // 7f6c34de1SBardia Mahjour //===----------------------------------------------------------------------===// 8f6c34de1SBardia Mahjour // 9f6c34de1SBardia Mahjour // This file contains a utility class, ValueProfileCollector, that is used to 10f6c34de1SBardia Mahjour // determine what kind of llvm::Value's are worth value-profiling, at which 11f6c34de1SBardia Mahjour // point in the program, and which instruction holds the Value Profile metadata. 12f6c34de1SBardia Mahjour // Currently, the only users of this utility is the PGOInstrumentation[Gen|Use] 13f6c34de1SBardia Mahjour // passes. 14f6c34de1SBardia Mahjour //===----------------------------------------------------------------------===// 15f6c34de1SBardia Mahjour 16f6c34de1SBardia Mahjour #ifndef LLVM_ANALYSIS_PROFILE_GEN_ANALYSIS_H 17f6c34de1SBardia Mahjour #define LLVM_ANALYSIS_PROFILE_GEN_ANALYSIS_H 18f6c34de1SBardia Mahjour 19f6c34de1SBardia Mahjour #include "llvm/ProfileData/InstrProf.h" 205b20c145SSimon Pilgrim #include <memory> 215b20c145SSimon Pilgrim #include <vector> 22f6c34de1SBardia Mahjour 23f6c34de1SBardia Mahjour namespace llvm { 24f6c34de1SBardia Mahjour 255b20c145SSimon Pilgrim class Function; 265b20c145SSimon Pilgrim class Instruction; 27*1b89c832Sserge-sans-paille class TargetLibraryInfo; 285b20c145SSimon Pilgrim class Value; 295b20c145SSimon Pilgrim 30f6c34de1SBardia Mahjour /// Utility analysis that determines what values are worth profiling. 31f6c34de1SBardia Mahjour /// The actual logic is inside the ValueProfileCollectorImpl, whose job is to 32f6c34de1SBardia Mahjour /// populate the Candidates vector. 33f6c34de1SBardia Mahjour /// 34f6c34de1SBardia Mahjour /// Value profiling an expression means to track the values that this expression 35f6c34de1SBardia Mahjour /// takes at runtime and the frequency of each value. 36f6c34de1SBardia Mahjour /// It is important to distinguish between two sets of value profiles for a 37f6c34de1SBardia Mahjour /// particular expression: 38f6c34de1SBardia Mahjour /// 1) The set of values at the point of evaluation. 39f6c34de1SBardia Mahjour /// 2) The set of values at the point of use. 40f6c34de1SBardia Mahjour /// In some cases, the two sets are identical, but it's not unusual for the two 41f6c34de1SBardia Mahjour /// to differ. 42f6c34de1SBardia Mahjour /// 43f6c34de1SBardia Mahjour /// To elaborate more, consider this C code, and focus on the expression `nn`: 44f6c34de1SBardia Mahjour /// void foo(int nn, bool b) { 45f6c34de1SBardia Mahjour /// if (b) memcpy(x, y, nn); 46f6c34de1SBardia Mahjour /// } 47f6c34de1SBardia Mahjour /// The point of evaluation can be as early as the start of the function, and 48f6c34de1SBardia Mahjour /// let's say the value profile for `nn` is: 49f6c34de1SBardia Mahjour /// total=100; (value,freq) set = {(8,10), (32,50)} 50f6c34de1SBardia Mahjour /// The point of use is right before we call memcpy, and since we execute the 51f6c34de1SBardia Mahjour /// memcpy conditionally, the value profile of `nn` can be: 52f6c34de1SBardia Mahjour /// total=15; (value,freq) set = {(8,10), (4,5)} 53f6c34de1SBardia Mahjour /// 54f6c34de1SBardia Mahjour /// For this reason, a plugin is responsible for computing the insertion point 55f6c34de1SBardia Mahjour /// for each value to be profiled. The `CandidateInfo` structure encapsulates 56f6c34de1SBardia Mahjour /// all the information needed for each value profile site. 57f6c34de1SBardia Mahjour class ValueProfileCollector { 58f6c34de1SBardia Mahjour public: 59f6c34de1SBardia Mahjour struct CandidateInfo { 60f6c34de1SBardia Mahjour Value *V; // The value to profile. 61f6c34de1SBardia Mahjour Instruction *InsertPt; // Insert the VP lib call before this instr. 62f6c34de1SBardia Mahjour Instruction *AnnotatedInst; // Where metadata is attached. 63f6c34de1SBardia Mahjour }; 64f6c34de1SBardia Mahjour 65106ec64fSHiroshi Yamauchi ValueProfileCollector(Function &Fn, TargetLibraryInfo &TLI); 66f6c34de1SBardia Mahjour ValueProfileCollector(ValueProfileCollector &&) = delete; 67f6c34de1SBardia Mahjour ValueProfileCollector &operator=(ValueProfileCollector &&) = delete; 68f6c34de1SBardia Mahjour 69f6c34de1SBardia Mahjour ValueProfileCollector(const ValueProfileCollector &) = delete; 70f6c34de1SBardia Mahjour ValueProfileCollector &operator=(const ValueProfileCollector &) = delete; 71f6c34de1SBardia Mahjour ~ValueProfileCollector(); 72f6c34de1SBardia Mahjour 73f6c34de1SBardia Mahjour /// returns a list of value profiling candidates of the given kind 74f6c34de1SBardia Mahjour std::vector<CandidateInfo> get(InstrProfValueKind Kind) const; 75f6c34de1SBardia Mahjour 76f6c34de1SBardia Mahjour private: 77f6c34de1SBardia Mahjour class ValueProfileCollectorImpl; 78f6c34de1SBardia Mahjour std::unique_ptr<ValueProfileCollectorImpl> PImpl; 79f6c34de1SBardia Mahjour }; 80f6c34de1SBardia Mahjour 81f6c34de1SBardia Mahjour } // namespace llvm 82f6c34de1SBardia Mahjour 83f6c34de1SBardia Mahjour #endif 84