1//===--- FunctionId.h - Sample profile function object ----------*- C++ -*-===// 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 7//===----------------------------------------------------------------------===// 11/// Defines FunctionId class. 13//===----------------------------------------------------------------------===// 15#ifndef LLVM_PROFILEDATA_FUNCTIONID_H 16#define LLVM_PROFILEDATA_FUNCTIONID_H 28/// This class represents a function that is read from a sample profile. It 29/// comes with two forms: a string or a hash code. The latter form is the 64-bit 30/// MD5 of the function name for efficient storage supported by ExtBinary 31/// profile format, and when reading the profile, this class can represent it 32/// without converting it to a string first. 33/// When representing a hash code, we utilize the LengthOrHashCode field to 34/// store it, and Name is set to null. When representing a string, it is same as 38constchar *Data =
nullptr;
40// Use uint64_t instead of size_t so that it can also hold a MD5 value on 44 /// Extension to memcmp to handle hash code representation. If both are hash 45 /// values, Lhs and Rhs are both null, function returns 0 (and needs an extra 46 /// comparison using getIntValue). If only one is hash code, it is considered 47 /// less than the StringRef one. Otherwise perform normal string comparison. 48staticint compareMemory(
constchar *Lhs,
constchar *Rhs,
uint64_tLength) {
55 return ::memcmp(Lhs, Rhs, (
size_t)
Length);
61 /// Constructor from a StringRef. 63 : Data(Str.
data()), LengthOrHashCode(Str.
size()) {
66 /// Constructor from a hash code. 68 : LengthOrHashCode(HashCode) {
72 /// Check for equality. Similar to StringRef::equals, but will also cover for 73 /// the case where one or both are hash codes. Comparing their int values are 74 /// sufficient. A hash code FunctionId is considered not equal to a StringRef 75 /// FunctionId regardless of actual contents. 77return LengthOrHashCode ==
Other.LengthOrHashCode &&
78 compareMemory(Data,
Other.Data, LengthOrHashCode) == 0;
81 /// Total order comparison. If both FunctionId are StringRef, this is the same 82 /// as StringRef::compare. If one of them is StringRef, it is considered 83 /// greater than the hash code FunctionId. Otherwise this is the the same 84 /// as comparing their int values. 86auto Res = compareMemory(
87 Data,
Other.Data, std::min(LengthOrHashCode,
Other.LengthOrHashCode));
90if (LengthOrHashCode ==
Other.LengthOrHashCode)
92return LengthOrHashCode <
Other.LengthOrHashCode ? -1 : 1;
95 /// Convert to a string, usually for output purpose. Use caution on return 96 /// value's lifetime when converting to StringRef. 97 std::string
str()
const{
99return std::string(Data, LengthOrHashCode);
100if (LengthOrHashCode != 0)
101return std::to_string(LengthOrHashCode);
105 /// Convert to StringRef. This is only allowed when it is known this object is 106 /// representing a StringRef, not a hash code. Calling this function on a hash 107 /// code is considered an error. 111assert(LengthOrHashCode == 0 &&
112"Cannot convert MD5 FunctionId to StringRef");
118 /// Get hash code of this object. Returns this object's hash code if it is 119 /// already representing one, otherwise returns the MD5 of its string content. 120 /// Note that it is not the same as std::hash because we want to keep the 121 /// consistency that the same sample profile function in string form or MD5 122 /// form has the same hash code. 126return LengthOrHashCode;
129boolempty()
const{
return LengthOrHashCode == 0; }
131 /// Check if this object represents a StringRef, or a hash code. 144returnLHS.compare(
RHS) < 0;
148returnLHS.compare(
RHS) <= 0;
152returnLHS.compare(
RHS) > 0;
156returnLHS.compare(
RHS) >= 0;
161returnOS <<
StringRef(Obj.Data, Obj.LengthOrHashCode);
162if (Obj.LengthOrHashCode != 0)
163returnOS << Obj.LengthOrHashCode;
175}
// end namespace sampleprof 177/// Template specialization for FunctionId so that it can be used in LLVM map 199}
// end namespace llvm 203/// Template specialization for FunctionId so that it can be used in STL 205template <>
structhash<
llvm::sampleprof::FunctionId> {
213#endif// LLVM_PROFILEDATA_FUNCTIONID_H This file defines DenseMapInfo traits for DenseMap.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
StringRef - Represent a constant reference to a string, i.e.
This class implements an extremely fast bulk output stream that can only output to a stream.
This class represents a function that is read from a sample profile.
FunctionId(StringRef Str)
Constructor from a StringRef.
StringRef stringRef() const
Convert to StringRef.
FunctionId(uint64_t HashCode)
Constructor from a hash code.
uint64_t getHashCode() const
Get hash code of this object.
bool isStringRef() const
Check if this object represents a StringRef, or a hash code.
friend raw_ostream & operator<<(raw_ostream &OS, const FunctionId &Obj)
int compare(const FunctionId &Other) const
Total order comparison.
bool equals(const FunctionId &Other) const
Check for equality.
std::string str() const
Convert to a string, usually for output purpose.
bool operator>(const FunctionId &LHS, const FunctionId &RHS)
bool operator<=(const FunctionId &LHS, const FunctionId &RHS)
bool operator<(const FunctionId &LHS, const FunctionId &RHS)
bool operator!=(const FunctionId &LHS, const FunctionId &RHS)
bool operator>=(const FunctionId &LHS, const FunctionId &RHS)
uint64_t MD5Hash(const FunctionId &Obj)
raw_ostream & operator<<(raw_ostream &OS, const FunctionId &Obj)
uint64_t hash_value(const FunctionId &Obj)
This is an optimization pass for GlobalISel generic memory operations.
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)
Implement std::hash so that hash_code can be used in STL containers.
static sampleprof::FunctionId getEmptyKey()
static sampleprof::FunctionId getTombstoneKey()
static bool isEqual(const sampleprof::FunctionId &LHS, const sampleprof::FunctionId &RHS)
static unsigned getHashValue(const sampleprof::FunctionId &Val)
An information struct used to provide DenseMap with the various necessary components for a given valu...
size_t operator()(const llvm::sampleprof::FunctionId &Val) const