Movatterモバイル変換


[0]ホーム

URL:


LLVM 20.0.0git
FunctionId.h
Go to the documentation of this file.
1//===--- FunctionId.h - Sample profile function object ----------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8///
9/// \file
10///
11/// Defines FunctionId class.
12///
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_PROFILEDATA_FUNCTIONID_H
16#define LLVM_PROFILEDATA_FUNCTIONID_H
17
18#include "llvm/ADT/DenseMapInfo.h"
19#include "llvm/ADT/Hashing.h"
20#include "llvm/ADT/StringRef.h"
21#include "llvm/Support/MD5.h"
22#include "llvm/Support/raw_ostream.h"
23#include <cstdint>
24
25namespacellvm {
26namespacesampleprof {
27
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
35/// StringRef.
36classFunctionId {
37
38constchar *Data =nullptr;
39
40// Use uint64_t instead of size_t so that it can also hold a MD5 value on
41// 32-bit system.
42uint64_t LengthOrHashCode = 0;
43
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) {
49if (Lhs == Rhs)
50return 0;
51if (!Lhs)
52return -1;
53if (!Rhs)
54return 1;
55 return ::memcmp(Lhs, Rhs, (size_t)Length);
56 }
57
58public:
59FunctionId() =default;
60
61 /// Constructor from a StringRef.
62explicitFunctionId(StringRef Str)
63 : Data(Str.data()), LengthOrHashCode(Str.size()) {
64 }
65
66 /// Constructor from a hash code.
67explicitFunctionId(uint64_t HashCode)
68 : LengthOrHashCode(HashCode) {
69assert(HashCode != 0);
70 }
71
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.
76boolequals(constFunctionId &Other) const{
77return LengthOrHashCode ==Other.LengthOrHashCode &&
78 compareMemory(Data,Other.Data, LengthOrHashCode) == 0;
79 }
80
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.
85intcompare(constFunctionId &Other) const{
86auto Res = compareMemory(
87 Data,Other.Data, std::min(LengthOrHashCode,Other.LengthOrHashCode));
88if (Res != 0)
89return Res;
90if (LengthOrHashCode ==Other.LengthOrHashCode)
91return 0;
92return LengthOrHashCode <Other.LengthOrHashCode ? -1 : 1;
93 }
94
95 /// Convert to a string, usually for output purpose. Use caution on return
96 /// value's lifetime when converting to StringRef.
97 std::stringstr() const{
98if (Data)
99return std::string(Data, LengthOrHashCode);
100if (LengthOrHashCode != 0)
101return std::to_string(LengthOrHashCode);
102return std::string();
103 }
104
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.
108StringRefstringRef() const{
109if (Data)
110returnStringRef(Data, LengthOrHashCode);
111assert(LengthOrHashCode == 0 &&
112"Cannot convert MD5 FunctionId to StringRef");
113returnStringRef();
114 }
115
116friendraw_ostream &operator<<(raw_ostream &OS,constFunctionId &Obj);
117
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.
123uint64_tgetHashCode() const{
124if (Data)
125returnMD5Hash(StringRef(Data, LengthOrHashCode));
126return LengthOrHashCode;
127 }
128
129boolempty() const{return LengthOrHashCode == 0; }
130
131 /// Check if this object represents a StringRef, or a hash code.
132boolisStringRef() const{return Data !=nullptr; }
133};
134
135inlinebooloperator==(constFunctionId &LHS,constFunctionId &RHS) {
136returnLHS.equals(RHS);
137}
138
139inlinebooloperator!=(constFunctionId &LHS,constFunctionId &RHS) {
140return !LHS.equals(RHS);
141}
142
143inlinebooloperator<(constFunctionId &LHS,constFunctionId &RHS) {
144returnLHS.compare(RHS) < 0;
145}
146
147inlinebooloperator<=(constFunctionId &LHS,constFunctionId &RHS) {
148returnLHS.compare(RHS) <= 0;
149}
150
151inlinebooloperator>(constFunctionId &LHS,constFunctionId &RHS) {
152returnLHS.compare(RHS) > 0;
153}
154
155inlinebooloperator>=(constFunctionId &LHS,constFunctionId &RHS) {
156returnLHS.compare(RHS) >= 0;
157}
158
159inlineraw_ostream &operator<<(raw_ostream &OS,constFunctionId &Obj) {
160if (Obj.Data)
161returnOS <<StringRef(Obj.Data, Obj.LengthOrHashCode);
162if (Obj.LengthOrHashCode != 0)
163returnOS << Obj.LengthOrHashCode;
164returnOS;
165}
166
167inlineuint64_tMD5Hash(constFunctionId &Obj) {
168return Obj.getHashCode();
169}
170
171inlineuint64_thash_value(constFunctionId &Obj) {
172return Obj.getHashCode();
173}
174
175}// end namespace sampleprof
176
177/// Template specialization for FunctionId so that it can be used in LLVM map
178/// containers.
179template <>structDenseMapInfo<sampleprof::FunctionId, void> {
180
181staticinlinesampleprof::FunctionIdgetEmptyKey() {
182returnsampleprof::FunctionId(~0ULL);
183 }
184
185staticinlinesampleprof::FunctionIdgetTombstoneKey() {
186returnsampleprof::FunctionId(~1ULL);
187 }
188
189staticunsignedgetHashValue(constsampleprof::FunctionId &Val) {
190return Val.getHashCode();
191 }
192
193staticboolisEqual(constsampleprof::FunctionId &LHS,
194constsampleprof::FunctionId &RHS) {
195returnLHS ==RHS;
196 }
197};
198
199}// end namespace llvm
200
201namespacestd {
202
203/// Template specialization for FunctionId so that it can be used in STL
204/// containers.
205template <>structhash<llvm::sampleprof::FunctionId> {
206size_toperator()(constllvm::sampleprof::FunctionId &Val) const{
207return Val.getHashCode();
208 }
209};
210
211}// end namespace std
212
213#endif// LLVM_PROFILEDATA_FUNCTIONID_H
DenseMapInfo.h
This file defines DenseMapInfo traits for DenseMap.
Hashing.h
MD5.h
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
OS
raw_pwrite_stream & OS
Definition:SampleProfWriter.cpp:51
data
static Split data
Definition:StaticDataSplitter.cpp:176
StringRef.h
RHS
Value * RHS
Definition:X86PartialReduction.cpp:74
LHS
Value * LHS
Definition:X86PartialReduction.cpp:73
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition:StringRef.h:51
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition:raw_ostream.h:52
llvm::sampleprof::FunctionId
This class represents a function that is read from a sample profile.
Definition:FunctionId.h:36
llvm::sampleprof::FunctionId::FunctionId
FunctionId(StringRef Str)
Constructor from a StringRef.
Definition:FunctionId.h:62
llvm::sampleprof::FunctionId::stringRef
StringRef stringRef() const
Convert to StringRef.
Definition:FunctionId.h:108
llvm::sampleprof::FunctionId::FunctionId
FunctionId(uint64_t HashCode)
Constructor from a hash code.
Definition:FunctionId.h:67
llvm::sampleprof::FunctionId::getHashCode
uint64_t getHashCode() const
Get hash code of this object.
Definition:FunctionId.h:123
llvm::sampleprof::FunctionId::FunctionId
FunctionId()=default
llvm::sampleprof::FunctionId::isStringRef
bool isStringRef() const
Check if this object represents a StringRef, or a hash code.
Definition:FunctionId.h:132
llvm::sampleprof::FunctionId::operator<<
friend raw_ostream & operator<<(raw_ostream &OS, const FunctionId &Obj)
Definition:FunctionId.h:159
llvm::sampleprof::FunctionId::compare
int compare(const FunctionId &Other) const
Total order comparison.
Definition:FunctionId.h:85
llvm::sampleprof::FunctionId::empty
bool empty() const
Definition:FunctionId.h:129
llvm::sampleprof::FunctionId::equals
bool equals(const FunctionId &Other) const
Check for equality.
Definition:FunctionId.h:76
llvm::sampleprof::FunctionId::str
std::string str() const
Convert to a string, usually for output purpose.
Definition:FunctionId.h:97
uint64_t
llvm::sampleprof::operator>
bool operator>(const FunctionId &LHS, const FunctionId &RHS)
Definition:FunctionId.h:151
llvm::sampleprof::operator<=
bool operator<=(const FunctionId &LHS, const FunctionId &RHS)
Definition:FunctionId.h:147
llvm::sampleprof::operator<
bool operator<(const FunctionId &LHS, const FunctionId &RHS)
Definition:FunctionId.h:143
llvm::sampleprof::operator!=
bool operator!=(const FunctionId &LHS, const FunctionId &RHS)
Definition:FunctionId.h:139
llvm::sampleprof::operator>=
bool operator>=(const FunctionId &LHS, const FunctionId &RHS)
Definition:FunctionId.h:155
llvm::sampleprof::MD5Hash
uint64_t MD5Hash(const FunctionId &Obj)
Definition:FunctionId.h:167
llvm::sampleprof::operator<<
raw_ostream & operator<<(raw_ostream &OS, const FunctionId &Obj)
Definition:FunctionId.h:159
llvm::sampleprof::hash_value
uint64_t hash_value(const FunctionId &Obj)
Definition:FunctionId.h:171
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition:AddressRanges.h:18
llvm::Length
@ Length
Definition:DWP.cpp:480
llvm::size
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.
Definition:STLExtras.h:1697
llvm::operator==
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)
Definition:AddressRanges.h:153
llvm::IRMemLocation::Other
@ Other
Any other memory.
std
Implement std::hash so that hash_code can be used in STL containers.
Definition:BitVector.h:858
raw_ostream.h
llvm::DenseMapInfo< sampleprof::FunctionId, void >::getEmptyKey
static sampleprof::FunctionId getEmptyKey()
Definition:FunctionId.h:181
llvm::DenseMapInfo< sampleprof::FunctionId, void >::getTombstoneKey
static sampleprof::FunctionId getTombstoneKey()
Definition:FunctionId.h:185
llvm::DenseMapInfo< sampleprof::FunctionId, void >::isEqual
static bool isEqual(const sampleprof::FunctionId &LHS, const sampleprof::FunctionId &RHS)
Definition:FunctionId.h:193
llvm::DenseMapInfo< sampleprof::FunctionId, void >::getHashValue
static unsigned getHashValue(const sampleprof::FunctionId &Val)
Definition:FunctionId.h:189
llvm::DenseMapInfo
An information struct used to provide DenseMap with the various necessary components for a given valu...
Definition:DenseMapInfo.h:52
std::hash< llvm::sampleprof::FunctionId >::operator()
size_t operator()(const llvm::sampleprof::FunctionId &Val) const
Definition:FunctionId.h:206

Generated on Thu Jul 17 2025 10:11:30 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp