Path: blob/main/contrib/llvm-project/llvm/lib/Transforms/Instrumentation/ValueProfileCollector.h
35266 views
//===- ValueProfileCollector.h - determine what to value profile ----------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file contains a utility class, ValueProfileCollector, that is used to9// determine what kind of llvm::Value's are worth value-profiling, at which10// point in the program, and which instruction holds the Value Profile metadata.11// Currently, the only users of this utility is the PGOInstrumentation[Gen|Use]12// passes.13//===----------------------------------------------------------------------===//1415#ifndef LLVM_ANALYSIS_PROFILE_GEN_ANALYSIS_H16#define LLVM_ANALYSIS_PROFILE_GEN_ANALYSIS_H1718#include "llvm/ProfileData/InstrProf.h"19#include <memory>20#include <vector>2122namespace llvm {2324class Function;25class Instruction;26class TargetLibraryInfo;27class Value;2829/// Utility analysis that determines what values are worth profiling.30/// The actual logic is inside the ValueProfileCollectorImpl, whose job is to31/// populate the Candidates vector.32///33/// Value profiling an expression means to track the values that this expression34/// takes at runtime and the frequency of each value.35/// It is important to distinguish between two sets of value profiles for a36/// particular expression:37/// 1) The set of values at the point of evaluation.38/// 2) The set of values at the point of use.39/// In some cases, the two sets are identical, but it's not unusual for the two40/// to differ.41///42/// To elaborate more, consider this C code, and focus on the expression `nn`:43/// void foo(int nn, bool b) {44/// if (b) memcpy(x, y, nn);45/// }46/// The point of evaluation can be as early as the start of the function, and47/// let's say the value profile for `nn` is:48/// total=100; (value,freq) set = {(8,10), (32,50)}49/// The point of use is right before we call memcpy, and since we execute the50/// memcpy conditionally, the value profile of `nn` can be:51/// total=15; (value,freq) set = {(8,10), (4,5)}52///53/// For this reason, a plugin is responsible for computing the insertion point54/// for each value to be profiled. The `CandidateInfo` structure encapsulates55/// all the information needed for each value profile site.56class ValueProfileCollector {57public:58struct CandidateInfo {59Value *V; // The value to profile.60Instruction *InsertPt; // Insert the VP lib call before this instr.61Instruction *AnnotatedInst; // Where metadata is attached.62};6364ValueProfileCollector(Function &Fn, TargetLibraryInfo &TLI);65ValueProfileCollector(ValueProfileCollector &&) = delete;66ValueProfileCollector &operator=(ValueProfileCollector &&) = delete;6768ValueProfileCollector(const ValueProfileCollector &) = delete;69ValueProfileCollector &operator=(const ValueProfileCollector &) = delete;70~ValueProfileCollector();7172/// returns a list of value profiling candidates of the given kind73std::vector<CandidateInfo> get(InstrProfValueKind Kind) const;7475private:76class ValueProfileCollectorImpl;77std::unique_ptr<ValueProfileCollectorImpl> PImpl;78};7980} // namespace llvm8182#endif838485