15 #ifndef LLVM_PROFILEDATA_SAMPLEPROF_H
16 #define LLVM_PROFILEDATA_SAMPLEPROF_H
32 #include <system_error>
79 namespace sampleprof {
82 return uint64_t(
'S') << (64 - 8) | uint64_t(
'P') << (64 - 16) |
83 uint64_t(
'R') << (64 - 24) | uint64_t(
'O') << (64 - 32) |
84 uint64_t(
'F') << (64 - 40) | uint64_t(
'4') << (64 - 48) |
85 uint64_t(
'2') << (64 - 56) | uint64_t(0xff);
150 uint64_t Weight = 1) {
151 uint64_t &TargetSamples = CallTargets[
F];
179 uint64_t NumSamples = 0;
185 class FunctionSamples;
220 uint64_t Num, uint64_t Weight = 1) {
221 return BodySamples[
LineLocation(LineOffset, Discriminator)].addSamples(
227 const std::string &FName,
228 uint64_t Num, uint64_t Weight = 1) {
229 return BodySamples[
LineLocation(LineOffset, Discriminator)].addCalledTarget(
238 const auto &ret = BodySamples.find(
LineLocation(LineOffset, Discriminator));
239 if (ret == BodySamples.end())
240 return std::error_code();
242 return ret->second.getSamples();
250 const auto &ret = BodySamples.find(
LineLocation(LineOffset, Discriminator));
251 if (ret == BodySamples.end())
252 return std::error_code();
253 return ret->second.getCallTargets();
258 return CallsiteSamples[Loc];
264 auto iter = CallsiteSamples.find(Loc);
265 if (iter == CallsiteSamples.end())
267 return &iter->second;
276 auto iter = CallsiteSamples.find(Loc);
277 if (iter == CallsiteSamples.end())
279 auto FS = iter->second.find(CalleeName);
280 if (FS != iter->second.end())
281 return &FS->getValue();
284 uint64_t MaxTotalSamples = 0;
286 for (
const auto &NameFS : iter->second)
287 if (NameFS.second.getTotalSamples() >= MaxTotalSamples) {
294 bool empty()
const {
return TotalSamples == 0; }
308 return CallsiteSamples;
326 for (
const auto &Rec :
I.second)
327 MergeResult(Result, FSMap[Rec.first()].merge(Rec.second, Weight));
337 if (TotalSamples <= Threshold)
342 for (
auto CS : CallsiteSamples)
343 for (
const auto &NameFS : CS.second)
344 NameFS.second.findImportedFunctions(S, M, Threshold);
361 uint64_t TotalSamples = 0;
366 uint64_t TotalHeadSamples = 0;
406 for (
const auto &
I : Samples)
408 std::stable_sort(V.
begin(), V.
end(),
410 return A->first <
B->first;
423 #endif // LLVM_PROFILEDATA_SAMPLEPROF_H
void findImportedFunctions(DenseSet< GlobalValue::GUID > &S, const Module *M, uint64_t Threshold) const
Recursively traverses all children, if the corresponding function is not defined in module M...
void push_back(const T &Elt)
Represents either an error or a value T.
raw_ostream & operator<<(raw_ostream &OS, const LineLocation &Loc)
const BodySampleMap & getBodySamples() const
Return all the samples collected in the body of the function.
A Module instance is used to store all the information related to an LLVM module. ...
GUID getGUID() const
Return a 64-bit global unique ID constructed from global value name (i.e.
Implements a dense probed hash-table based set.
sampleprof_error merge(const SampleRecord &Other, uint64_t Weight=1)
Merge the samples in Other into this record.
StringMap< uint64_t > CallTargetMap
LineLocation(uint32_t L, uint32_t D)
SampleSorter(const std::map< LocationT, SampleT > &Samples)
std::pair< const LocationT, SampleT > SamplesWithLoc
const StringRef & getName() const
Return the function name.
const CallTargetMap & getCallTargets() const
Representation of the samples collected for a function.
std::error_code make_error_code(BitcodeError E)
uint64_t getHeadSamples() const
Return the total number of samples collected at the head of the function.
sampleprof_error addCalledTarget(StringRef F, uint64_t S, uint64_t Weight=1)
Add called function F with samples S.
Representation of a single sample record.
void setName(StringRef FunctionName)
Set the name of the function.
bool operator<(const LineLocation &O) const
std::map< LineLocation, SampleRecord > BodySampleMap
static uint64_t SPVersion()
Sort a LocationT->SampleT map by LocationT.
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
const FunctionSamples * findFunctionSamplesAt(const LineLocation &Loc, StringRef CalleeName) const
Returns a pointer to FunctionSamples at the given callsite location Loc with callee CalleeName...
void print(raw_ostream &OS=dbgs(), unsigned Indent=0) const
Print the samples collected for a function on stream OS.
sampleprof_error addBodySamples(uint32_t LineOffset, uint32_t Discriminator, uint64_t Num, uint64_t Weight=1)
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator begin()
sampleprof_error addSamples(uint64_t S, uint64_t Weight=1)
Increment the number of samples for this record by S.
std::pair< iterator, bool > insert(const ValueT &V)
static ManagedStatic< _object_error_category > error_category
uint64_t getTotalSamples() const
Return the total number of samples collected inside the function.
std::enable_if< std::is_unsigned< T >::value, T >::type SaturatingMultiplyAdd(T X, T Y, T A, bool *ResultOverflowed=nullptr)
Multiply two unsigned integers, X and Y, and add the unsigned integer, A to the product.
const CallsiteSampleMap & getCallsiteSamples() const
Return all the callsite samples collected in the body of the function.
sampleprof_error addCalledTargetSamples(uint32_t LineOffset, uint32_t Discriminator, const std::string &FName, uint64_t Num, uint64_t Weight=1)
ErrorOr< SampleRecord::CallTargetMap > findCallTargetMapAt(uint32_t LineOffset, uint32_t Discriminator) const
Returns the call target map collected at a given location.
sampleprof_error addTotalSamples(uint64_t Num, uint64_t Weight=1)
uint64_t getSamples() const
Module.h This file contains the declarations for the Module class.
FunctionSamplesMap & functionSamplesAt(const LineLocation &Loc)
Return the function samples at the given callsite location.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
const std::error_category & sampleprof_category()
DISubprogram * getSubprogram() const
Get the attached subprogram.
void print(raw_ostream &OS) const
bool hasCalls() const
Return true if this sample record contains function calls.
std::map< LineLocation, FunctionSamplesMap > CallsiteSampleMap
static uint64_t SPMagic()
LLVM_ATTRIBUTE_ALWAYS_INLINE iterator end()
Represents the relative location of an instruction.
SmallVector< const SamplesWithLoc *, 20 > SamplesWithLocList
sampleprof_error addHeadSamples(uint64_t Num, uint64_t Weight=1)
Provides ErrorOr<T> smart pointer.
static int const Threshold
TODO: Write a new FunctionPass AliasAnalysis so that it can keep a cache.
const FunctionSamplesMap * findFunctionSamplesMapAt(const LineLocation &Loc) const
Returns the FunctionSamplesMap at the given Loc.
This class implements an extremely fast bulk output stream that can only output to a stream...
ErrorOr< uint64_t > findSamplesAt(uint32_t LineOffset, uint32_t Discriminator) const
Return the number of samples collected at the given location.
StringRef - Represent a constant reference to a string, i.e.
void print(raw_ostream &OS, unsigned Indent) const
Print the sample record to the stream OS indented by Indent.
sampleprof_error merge(const FunctionSamples &Other, uint64_t Weight=1)
Merge the samples in Other into this one.
FunctionSamples()=default
sampleprof_error MergeResult(sampleprof_error &Accumulator, sampleprof_error Result)