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