brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.9 KiB · 574833b Raw
59 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef LLVM_TOOLS_LLVM_PROFGEN_CALLCONTEXT_H10#define LLVM_TOOLS_LLVM_PROFGEN_CALLCONTEXT_H11 12#include "llvm/ProfileData/SampleProf.h"13#include <sstream>14#include <string>15 16namespace llvm {17namespace sampleprof {18 19inline 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 we31// may think of making a class wrapper with internal states for it.32inline 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.44inline std::string45getReversedLocWithContext(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);51  }52  return OContextStr.str();53}54 55} // end namespace sampleprof56} // end namespace llvm57 58#endif59