// sample profile data.
//
//===----------------------------------------------------------------------===//
+
#ifndef LLVM_PROFILEDATA_SAMPLEPROF_H_
#define LLVM_PROFILEDATA_SAMPLEPROF_H_
truncated,
malformed,
unrecognized_format,
+ unsupported_writing_format,
+ truncated_name_table,
not_implemented
};
uint64_t('2') << (64 - 56) | uint64_t(0xff);
}
-static inline uint64_t SPVersion() { return 100; }
+static inline uint64_t SPVersion() { return 102; }
/// Represents the relative location of an instruction.
///
/// that are on the same line but belong to different basic blocks
/// (e.g., the two post-increment instructions in "if (p) x++; else y++;").
struct LineLocation {
- LineLocation(int L, unsigned D) : LineOffset(L), Discriminator(D) {}
- int LineOffset;
- unsigned Discriminator;
+ LineLocation(uint32_t L, uint32_t D) : LineOffset(L), Discriminator(D) {}
+ void print(raw_ostream &OS) const;
+ void dump() const;
+
+ uint32_t LineOffset;
+ uint32_t Discriminator;
};
+raw_ostream &operator<<(raw_ostream &OS, const LineLocation &Loc);
+
/// Represents the relative location of a callsite.
///
/// Callsite locations are specified by the line offset from the
/// head is), the discriminator value within that line, and the callee
/// function name.
struct CallsiteLocation : public LineLocation {
- CallsiteLocation(int L, unsigned D, StringRef N)
+ CallsiteLocation(uint32_t L, uint32_t D, StringRef N)
: LineLocation(L, D), CalleeName(N) {}
+ void print(raw_ostream &OS) const;
+ void dump() const;
+
StringRef CalleeName;
};
+raw_ostream &operator<<(raw_ostream &OS, const CallsiteLocation &Loc);
+
} // End namespace sampleprof
template <> struct DenseMapInfo<sampleprof::LineLocation> {
- typedef DenseMapInfo<int> OffsetInfo;
- typedef DenseMapInfo<unsigned> DiscriminatorInfo;
+ typedef DenseMapInfo<uint32_t> OffsetInfo;
+ typedef DenseMapInfo<uint32_t> DiscriminatorInfo;
static inline sampleprof::LineLocation getEmptyKey() {
return sampleprof::LineLocation(OffsetInfo::getEmptyKey(),
DiscriminatorInfo::getEmptyKey());
DiscriminatorInfo::getTombstoneKey());
}
static inline unsigned getHashValue(sampleprof::LineLocation Val) {
- return DenseMapInfo<std::pair<int, unsigned>>::getHashValue(
- std::pair<int, unsigned>(Val.LineOffset, Val.Discriminator));
+ return DenseMapInfo<std::pair<uint32_t, uint32_t>>::getHashValue(
+ std::pair<uint32_t, uint32_t>(Val.LineOffset, Val.Discriminator));
}
static inline bool isEqual(sampleprof::LineLocation LHS,
sampleprof::LineLocation RHS) {
};
template <> struct DenseMapInfo<sampleprof::CallsiteLocation> {
- typedef DenseMapInfo<int> OffsetInfo;
- typedef DenseMapInfo<unsigned> DiscriminatorInfo;
+ typedef DenseMapInfo<uint32_t> OffsetInfo;
+ typedef DenseMapInfo<uint32_t> DiscriminatorInfo;
typedef DenseMapInfo<StringRef> CalleeNameInfo;
static inline sampleprof::CallsiteLocation getEmptyKey() {
return sampleprof::CallsiteLocation(OffsetInfo::getEmptyKey(),
"");
}
static inline unsigned getHashValue(sampleprof::CallsiteLocation Val) {
- return DenseMapInfo<std::pair<int, unsigned>>::getHashValue(
- std::pair<int, unsigned>(Val.LineOffset, Val.Discriminator));
+ return DenseMapInfo<std::pair<uint32_t, uint32_t>>::getHashValue(
+ std::pair<uint32_t, uint32_t>(Val.LineOffset, Val.Discriminator));
}
static inline bool isEqual(sampleprof::CallsiteLocation LHS,
sampleprof::CallsiteLocation RHS) {
/// will be a list of one or more functions.
class SampleRecord {
public:
- typedef StringMap<unsigned> CallTargetMap;
+ typedef StringMap<uint64_t> CallTargetMap;
SampleRecord() : NumSamples(0), CallTargets() {}
///
/// Sample counts accumulate using saturating arithmetic, to avoid wrapping
/// around unsigned integers.
- void addSamples(unsigned S) {
- if (NumSamples <= std::numeric_limits<unsigned>::max() - S)
- NumSamples += S;
- else
- NumSamples = std::numeric_limits<unsigned>::max();
+ void addSamples(uint64_t S) {
+ NumSamples = SaturatingAdd(NumSamples, S);
}
/// Add called function \p F with samples \p S.
///
/// Sample counts accumulate using saturating arithmetic, to avoid wrapping
/// around unsigned integers.
- void addCalledTarget(StringRef F, unsigned S) {
- unsigned &TargetSamples = CallTargets[F];
- if (TargetSamples <= std::numeric_limits<unsigned>::max() - S)
- TargetSamples += S;
- else
- TargetSamples = std::numeric_limits<unsigned>::max();
+ void addCalledTarget(StringRef F, uint64_t S) {
+ uint64_t &TargetSamples = CallTargets[F];
+ TargetSamples = SaturatingAdd(TargetSamples, S);
}
/// Return true if this sample record contains function calls.
bool hasCalls() const { return CallTargets.size() > 0; }
- unsigned getSamples() const { return NumSamples; }
+ uint64_t getSamples() const { return NumSamples; }
const CallTargetMap &getCallTargets() const { return CallTargets; }
/// Merge the samples in \p Other into this record.
addCalledTarget(I.first(), I.second);
}
+ void print(raw_ostream &OS, unsigned Indent) const;
+ void dump() const;
+
private:
- unsigned NumSamples;
+ uint64_t NumSamples;
CallTargetMap CallTargets;
};
+raw_ostream &operator<<(raw_ostream &OS, const SampleRecord &Sample);
+
typedef DenseMap<LineLocation, SampleRecord> BodySampleMap;
class FunctionSamples;
typedef DenseMap<CallsiteLocation, FunctionSamples> CallsiteSampleMap;
class FunctionSamples {
public:
FunctionSamples() : TotalSamples(0), TotalHeadSamples(0) {}
- void print(raw_ostream &OS = dbgs());
- void addTotalSamples(unsigned Num) { TotalSamples += Num; }
- void addHeadSamples(unsigned Num) { TotalHeadSamples += Num; }
- void addBodySamples(int LineOffset, unsigned Discriminator, unsigned Num) {
- assert(LineOffset >= 0);
+ void print(raw_ostream &OS = dbgs(), unsigned Indent = 0) const;
+ void dump() const;
+ void addTotalSamples(uint64_t Num) { TotalSamples += Num; }
+ void addHeadSamples(uint64_t Num) { TotalHeadSamples += Num; }
+ void addBodySamples(uint32_t LineOffset, uint32_t Discriminator,
+ uint64_t Num) {
BodySamples[LineLocation(LineOffset, Discriminator)].addSamples(Num);
}
- void addCalledTargetSamples(int LineOffset, unsigned Discriminator,
- std::string FName, unsigned Num) {
- assert(LineOffset >= 0);
+ void addCalledTargetSamples(uint32_t LineOffset, uint32_t Discriminator,
+ std::string FName, uint64_t Num) {
BodySamples[LineLocation(LineOffset, Discriminator)].addCalledTarget(FName,
Num);
}
/// Return the number of samples collected at the given location.
/// Each location is specified by \p LineOffset and \p Discriminator.
/// If the location is not found in profile, return error.
- ErrorOr<unsigned> findSamplesAt(int LineOffset,
- unsigned Discriminator) const {
+ ErrorOr<uint64_t> findSamplesAt(uint32_t LineOffset,
+ uint32_t Discriminator) const {
const auto &ret = BodySamples.find(LineLocation(LineOffset, Discriminator));
if (ret == BodySamples.end())
return std::error_code();
findFunctionSamplesAt(const CallsiteLocation &Loc) const {
auto iter = CallsiteSamples.find(Loc);
if (iter == CallsiteSamples.end()) {
- return NULL;
+ return nullptr;
} else {
return &iter->second;
}
bool empty() const { return TotalSamples == 0; }
/// Return the total number of samples collected inside the function.
- unsigned getTotalSamples() const { return TotalSamples; }
+ uint64_t getTotalSamples() const { return TotalSamples; }
/// Return the total number of samples collected at the head of the
/// function.
- unsigned getHeadSamples() const { return TotalHeadSamples; }
+ uint64_t getHeadSamples() const { return TotalHeadSamples; }
/// Return all the samples collected in the body of the function.
const BodySampleMap &getBodySamples() const { return BodySamples; }
///
/// Samples are cumulative, they include all the samples collected
/// inside this function and all its inlined callees.
- unsigned TotalSamples;
+ uint64_t TotalSamples;
/// Total number of samples collected at the head of the function.
/// This is an approximation of the number of calls made to this function
/// at runtime.
- unsigned TotalHeadSamples;
+ uint64_t TotalHeadSamples;
/// Map instruction locations to collected samples.
///
/// are an offset from the start of the function.
BodySampleMap BodySamples;
+ /// Map call sites to collected samples for the called function.
+ ///
+ /// Each entry in this map corresponds to all the samples
+ /// collected for the inlined function call at the given
+ /// location. For example, given:
+ ///
+ /// void foo() {
+ /// 1 bar();
+ /// ...
+ /// 8 baz();
+ /// }
+ ///
+ /// If the bar() and baz() calls were inlined inside foo(), this
+ /// map will contain two entries. One for all the samples collected
+ /// in the call to bar() at line offset 1, the other for all the samples
+ /// collected in the call to baz() at line offset 8.
CallsiteSampleMap CallsiteSamples;
};
-} // End namespace sampleprof
+raw_ostream &operator<<(raw_ostream &OS, const FunctionSamples &FS);
+
+} // end namespace sampleprof
-} // End namespace llvm
+} // end namespace llvm
#endif // LLVM_PROFILEDATA_SAMPLEPROF_H_