[NFC][Py Reformat] Reformat python files in llvm
[llvm-project.git] / llvm / tools / llvm-profgen / CallContext.h
blob5e552130d03c75756ff1214e329de7158a6a24b2
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>
15 #include <vector>
17 namespace llvm {
18 namespace sampleprof {
20 inline std::string getCallSite(const SampleContextFrame &Callsite) {
21 std::string CallsiteStr = Callsite.FuncName.str();
22 CallsiteStr += ":";
23 CallsiteStr += Twine(Callsite.Location.LineOffset).str();
24 if (Callsite.Location.Discriminator > 0) {
25 CallsiteStr += ".";
26 CallsiteStr += Twine(Callsite.Location.Discriminator).str();
28 return CallsiteStr;
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())
37 OContextStr << " @ ";
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.
45 inline std::string
46 getReversedLocWithContext(const SampleContextFrameVector &Context) {
47 std::ostringstream OContextStr;
48 for (const auto &Callsite : reverse(Context)) {
49 if (OContextStr.str().size())
50 OContextStr << " @ ";
51 OContextStr << getCallSite(Callsite);
53 return OContextStr.str();
56 } // end namespace sampleprof
57 } // end namespace llvm
59 #endif