| 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 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef LLVM_TOOLS_LLVM_PROFGEN_CALLCONTEXT_H |
| 10 | #define LLVM_TOOLS_LLVM_PROFGEN_CALLCONTEXT_H |
| 11 | |
| 12 | #include "llvm/ProfileData/SampleProf.h" |
| 13 | #include <sstream> |
| 14 | #include <string> |
| 15 | |
| 16 | namespace llvm { |
| 17 | namespace sampleprof { |
| 18 | |
| 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(); |
| 26 | } |
| 27 | return CallsiteStr; |
| 28 | } |
| 29 | |
| 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); |
| 38 | } |
| 39 | return OContextStr.str(); |
| 40 | } |
| 41 | |
| 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(C: Context)) { |
| 48 | if (OContextStr.str().size()) |
| 49 | OContextStr << " @ "; |
| 50 | OContextStr << getCallSite(Callsite); |
| 51 | } |
| 52 | return OContextStr.str(); |
| 53 | } |
| 54 | |
| 55 | } // end namespace sampleprof |
| 56 | } // end namespace llvm |
| 57 | |
| 58 | #endif |
| 59 |