18bcb0991SDimitry Andric //===- ValueProfileCollector.h - determine what to value profile ----------===// 28bcb0991SDimitry Andric // 38bcb0991SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 48bcb0991SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 58bcb0991SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 68bcb0991SDimitry Andric // 78bcb0991SDimitry Andric //===----------------------------------------------------------------------===// 88bcb0991SDimitry Andric // 98bcb0991SDimitry Andric // This file contains a utility class, ValueProfileCollector, that is used to 108bcb0991SDimitry Andric // determine what kind of llvm::Value's are worth value-profiling, at which 118bcb0991SDimitry Andric // point in the program, and which instruction holds the Value Profile metadata. 128bcb0991SDimitry Andric // Currently, the only users of this utility is the PGOInstrumentation[Gen|Use] 138bcb0991SDimitry Andric // passes. 148bcb0991SDimitry Andric //===----------------------------------------------------------------------===// 158bcb0991SDimitry Andric 168bcb0991SDimitry Andric #ifndef LLVM_ANALYSIS_PROFILE_GEN_ANALYSIS_H 178bcb0991SDimitry Andric #define LLVM_ANALYSIS_PROFILE_GEN_ANALYSIS_H 188bcb0991SDimitry Andric 198bcb0991SDimitry Andric #include "llvm/ProfileData/InstrProf.h" 20e8d8bef9SDimitry Andric #include <memory> 21e8d8bef9SDimitry Andric #include <vector> 228bcb0991SDimitry Andric 238bcb0991SDimitry Andric namespace llvm { 248bcb0991SDimitry Andric 25e8d8bef9SDimitry Andric class Function; 26e8d8bef9SDimitry Andric class Instruction; 27*81ad6265SDimitry Andric class TargetLibraryInfo; 28e8d8bef9SDimitry Andric class Value; 29e8d8bef9SDimitry Andric 308bcb0991SDimitry Andric /// Utility analysis that determines what values are worth profiling. 318bcb0991SDimitry Andric /// The actual logic is inside the ValueProfileCollectorImpl, whose job is to 328bcb0991SDimitry Andric /// populate the Candidates vector. 338bcb0991SDimitry Andric /// 348bcb0991SDimitry Andric /// Value profiling an expression means to track the values that this expression 358bcb0991SDimitry Andric /// takes at runtime and the frequency of each value. 368bcb0991SDimitry Andric /// It is important to distinguish between two sets of value profiles for a 378bcb0991SDimitry Andric /// particular expression: 388bcb0991SDimitry Andric /// 1) The set of values at the point of evaluation. 398bcb0991SDimitry Andric /// 2) The set of values at the point of use. 408bcb0991SDimitry Andric /// In some cases, the two sets are identical, but it's not unusual for the two 418bcb0991SDimitry Andric /// to differ. 428bcb0991SDimitry Andric /// 438bcb0991SDimitry Andric /// To elaborate more, consider this C code, and focus on the expression `nn`: 448bcb0991SDimitry Andric /// void foo(int nn, bool b) { 458bcb0991SDimitry Andric /// if (b) memcpy(x, y, nn); 468bcb0991SDimitry Andric /// } 478bcb0991SDimitry Andric /// The point of evaluation can be as early as the start of the function, and 488bcb0991SDimitry Andric /// let's say the value profile for `nn` is: 498bcb0991SDimitry Andric /// total=100; (value,freq) set = {(8,10), (32,50)} 508bcb0991SDimitry Andric /// The point of use is right before we call memcpy, and since we execute the 518bcb0991SDimitry Andric /// memcpy conditionally, the value profile of `nn` can be: 528bcb0991SDimitry Andric /// total=15; (value,freq) set = {(8,10), (4,5)} 538bcb0991SDimitry Andric /// 548bcb0991SDimitry Andric /// For this reason, a plugin is responsible for computing the insertion point 558bcb0991SDimitry Andric /// for each value to be profiled. The `CandidateInfo` structure encapsulates 568bcb0991SDimitry Andric /// all the information needed for each value profile site. 578bcb0991SDimitry Andric class ValueProfileCollector { 588bcb0991SDimitry Andric public: 598bcb0991SDimitry Andric struct CandidateInfo { 608bcb0991SDimitry Andric Value *V; // The value to profile. 618bcb0991SDimitry Andric Instruction *InsertPt; // Insert the VP lib call before this instr. 628bcb0991SDimitry Andric Instruction *AnnotatedInst; // Where metadata is attached. 638bcb0991SDimitry Andric }; 648bcb0991SDimitry Andric 655ffd83dbSDimitry Andric ValueProfileCollector(Function &Fn, TargetLibraryInfo &TLI); 668bcb0991SDimitry Andric ValueProfileCollector(ValueProfileCollector &&) = delete; 678bcb0991SDimitry Andric ValueProfileCollector &operator=(ValueProfileCollector &&) = delete; 688bcb0991SDimitry Andric 698bcb0991SDimitry Andric ValueProfileCollector(const ValueProfileCollector &) = delete; 708bcb0991SDimitry Andric ValueProfileCollector &operator=(const ValueProfileCollector &) = delete; 718bcb0991SDimitry Andric ~ValueProfileCollector(); 728bcb0991SDimitry Andric 738bcb0991SDimitry Andric /// returns a list of value profiling candidates of the given kind 748bcb0991SDimitry Andric std::vector<CandidateInfo> get(InstrProfValueKind Kind) const; 758bcb0991SDimitry Andric 768bcb0991SDimitry Andric private: 778bcb0991SDimitry Andric class ValueProfileCollectorImpl; 788bcb0991SDimitry Andric std::unique_ptr<ValueProfileCollectorImpl> PImpl; 798bcb0991SDimitry Andric }; 808bcb0991SDimitry Andric 818bcb0991SDimitry Andric } // namespace llvm 828bcb0991SDimitry Andric 838bcb0991SDimitry Andric #endif 84