CallContext.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. // Function name, LineLocation
  17. typedef std::pair<std::string, LineLocation> FrameLocation;
  18. typedef SmallVector<FrameLocation, 4> FrameLocationStack;
  19. inline std::string getCallSite(const FrameLocation &Callsite) {
  20. std::string CallsiteStr = Callsite.first;
  21. CallsiteStr += ":";
  22. CallsiteStr += Twine(Callsite.second.LineOffset).str();
  23. if (Callsite.second.Discriminator > 0) {
  24. CallsiteStr += ".";
  25. CallsiteStr += Twine(Callsite.second.Discriminator).str();
  26. }
  27. return CallsiteStr;
  28. }
  29. // TODO: This operation is expansive. If it ever gets called multiple times we
  30. // may think of making a class wrapper with internal states for it.
  31. inline std::string getLocWithContext(const FrameLocationStack &Context) {
  32. std::ostringstream OContextStr;
  33. for (const auto &Callsite : Context) {
  34. if (OContextStr.str().size())
  35. OContextStr << " @ ";
  36. OContextStr << getCallSite(Callsite);
  37. }
  38. return OContextStr.str();
  39. }
  40. // Reverse call context, i.e., in the order of callee frames to caller frames,
  41. // is useful during instruction printing or pseudo probe printing.
  42. inline std::string
  43. getReversedLocWithContext(const FrameLocationStack &Context) {
  44. std::ostringstream OContextStr;
  45. for (const auto &Callsite : reverse(Context)) {
  46. if (OContextStr.str().size())
  47. OContextStr << " @ ";
  48. OContextStr << getCallSite(Callsite);
  49. }
  50. return OContextStr.str();
  51. }
  52. } // end namespace sampleprof
  53. } // end namespace llvm
  54. #endif