CallContext.h 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. #ifndef LLVM_TOOLS_LLVM_PROFGEN_CALLCONTEXT_H
  9. #define LLVM_TOOLS_LLVM_PROFGEN_CALLCONTEXT_H
  10. #include "llvm/ProfileData/SampleProf.h"
  11. #include <sstream>
  12. #include <string>
  13. #include <vector>
  14. namespace llvm {
  15. namespace sampleprof {
  16. inline std::string getCallSite(const SampleContextFrame &Callsite) {
  17. std::string CallsiteStr = Callsite.FuncName.str();
  18. CallsiteStr += ":";
  19. CallsiteStr += Twine(Callsite.Location.LineOffset).str();
  20. if (Callsite.Location.Discriminator > 0) {
  21. CallsiteStr += ".";
  22. CallsiteStr += Twine(Callsite.Location.Discriminator).str();
  23. }
  24. return CallsiteStr;
  25. }
  26. // TODO: This operation is expansive. If it ever gets called multiple times we
  27. // may think of making a class wrapper with internal states for it.
  28. inline std::string getLocWithContext(const SampleContextFrameVector &Context) {
  29. std::ostringstream OContextStr;
  30. for (const auto &Callsite : Context) {
  31. if (OContextStr.str().size())
  32. OContextStr << " @ ";
  33. OContextStr << getCallSite(Callsite);
  34. }
  35. return OContextStr.str();
  36. }
  37. // Reverse call context, i.e., in the order of callee frames to caller frames,
  38. // is useful during instruction printing or pseudo probe printing.
  39. inline std::string
  40. getReversedLocWithContext(const SampleContextFrameVector &Context) {
  41. std::ostringstream OContextStr;
  42. for (const auto &Callsite : reverse(Context)) {
  43. if (OContextStr.str().size())
  44. OContextStr << " @ ";
  45. OContextStr << getCallSite(Callsite);
  46. }
  47. return OContextStr.str();
  48. }
  49. } // end namespace sampleprof
  50. } // end namespace llvm
  51. #endif