1 //===-- CallContext.h - Call Context Handler ---------------------*- 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 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_TOOLS_LLVM_PROFGEN_CALLCONTEXT_H
10 #define LLVM_TOOLS_LLVM_PROFGEN_CALLCONTEXT_H
12 #include "llvm/ProfileData/SampleProf.h"
18 namespace sampleprof
{
20 inline std::string
getCallSite(const SampleContextFrame
&Callsite
) {
21 std::string CallsiteStr
= Callsite
.FuncName
.str();
23 CallsiteStr
+= Twine(Callsite
.Location
.LineOffset
).str();
24 if (Callsite
.Location
.Discriminator
> 0) {
26 CallsiteStr
+= Twine(Callsite
.Location
.Discriminator
).str();
31 // TODO: This operation is expansive. If it ever gets called multiple times we
32 // may think of making a class wrapper with internal states for it.
33 inline std::string
getLocWithContext(const SampleContextFrameVector
&Context
) {
34 std::ostringstream OContextStr
;
35 for (const auto &Callsite
: Context
) {
36 if (OContextStr
.str().size())
38 OContextStr
<< getCallSite(Callsite
);
40 return OContextStr
.str();
43 // Reverse call context, i.e., in the order of callee frames to caller frames,
44 // is useful during instruction printing or pseudo probe printing.
46 getReversedLocWithContext(const SampleContextFrameVector
&Context
) {
47 std::ostringstream OContextStr
;
48 for (const auto &Callsite
: reverse(Context
)) {
49 if (OContextStr
.str().size())
51 OContextStr
<< getCallSite(Callsite
);
53 return OContextStr
.str();
56 } // end namespace sampleprof
57 } // end namespace llvm