[llvm-shlib] Fix the version naming style of libLLVM for Windows (#85710)
[llvm-project.git] / llvm / tools / llvm-profgen / CallContext.h
blob574833bfe8be29f0f80c154bcaf37cc252494caa
1 //===-- CallContext.h - Call Context Handler ---------------------*- 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 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_TOOLS_LLVM_PROFGEN_CALLCONTEXT_H
10 #define LLVM_TOOLS_LLVM_PROFGEN_CALLCONTEXT_H
12 #include "llvm/ProfileData/SampleProf.h"
13 #include <sstream>
14 #include <string>
16 namespace llvm {
17 namespace sampleprof {
19 inline std::string getCallSite(const SampleContextFrame &Callsite) {
20 std::string CallsiteStr = Callsite.Func.str();
21 CallsiteStr += ":";
22 CallsiteStr += Twine(Callsite.Location.LineOffset).str();
23 if (Callsite.Location.Discriminator > 0) {
24 CallsiteStr += ".";
25 CallsiteStr += Twine(Callsite.Location.Discriminator).str();
27 return CallsiteStr;
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())
36 OContextStr << " @ ";
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.
44 inline std::string
45 getReversedLocWithContext(const SampleContextFrameVector &Context) {
46 std::ostringstream OContextStr;
47 for (const auto &Callsite : reverse(Context)) {
48 if (OContextStr.str().size())
49 OContextStr << " @ ";
50 OContextStr << getCallSite(Callsite);
52 return OContextStr.str();
55 } // end namespace sampleprof
56 } // end namespace llvm
58 #endif