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 #include <vector> 16 17 namespace llvm { 18 namespace sampleprof { 19 getCallSite(const SampleContextFrame & Callsite)20inline 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(); 27 } 28 return CallsiteStr; 29 } 30 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. getLocWithContext(const SampleContextFrameVector & Context)33inline 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); 39 } 40 return OContextStr.str(); 41 } 42 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 getReversedLocWithContext(const SampleContextFrameVector & Context)46getReversedLocWithContext(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); 52 } 53 return OContextStr.str(); 54 } 55 56 } // end namespace sampleprof 57 } // end namespace llvm 58 59 #endif 60