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"
17 namespace sampleprof
{
19 inline std::string
getCallSite(const SampleContextFrame
&Callsite
) {
20 std::string CallsiteStr
= Callsite
.Func
.str();
22 CallsiteStr
+= Twine(Callsite
.Location
.LineOffset
).str();
23 if (Callsite
.Location
.Discriminator
> 0) {
25 CallsiteStr
+= Twine(Callsite
.Location
.Discriminator
).str();
30 // TODO: This operation is expansive. If it ever gets called multiple times we
31 // may think of making a class wrapper with internal states for it.
32 inline std::string
getLocWithContext(const SampleContextFrameVector
&Context
) {
33 std::ostringstream OContextStr
;
34 for (const auto &Callsite
: Context
) {
35 if (OContextStr
.str().size())
37 OContextStr
<< getCallSite(Callsite
);
39 return OContextStr
.str();
42 // Reverse call context, i.e., in the order of callee frames to caller frames,
43 // is useful during instruction printing or pseudo probe printing.
45 getReversedLocWithContext(const SampleContextFrameVector
&Context
) {
46 std::ostringstream OContextStr
;
47 for (const auto &Callsite
: reverse(Context
)) {
48 if (OContextStr
.str().size())
50 OContextStr
<< getCallSite(Callsite
);
52 return OContextStr
.str();
55 } // end namespace sampleprof
56 } // end namespace llvm