summaryrefslogtreecommitdiffstats
path: root/llvm
diff options
context:
space:
mode:
authorXinliang David Li <davidxl@google.com>2015-11-09 00:01:22 +0000
committerXinliang David Li <davidxl@google.com>2015-11-09 00:01:22 +0000
commit441959d296d1d61b4e2f2bbfdf5e79af1cd30b51 (patch)
treeda9264f474902dd3c36e07db5d0295cc5681af6f /llvm
parenta4c85d4c96fd32d18c0e92792309cf9d65419a98 (diff)
downloadbcm5719-llvm-441959d296d1d61b4e2f2bbfdf5e79af1cd30b51.tar.gz
bcm5719-llvm-441959d296d1d61b4e2f2bbfdf5e79af1cd30b51.zip
[PGO] Instr func name var creation code refactoring
Move the code from cfe to LLMV and become shared interfaces. There is no functional change. llvm-svn: 252433
Diffstat (limited to 'llvm')
-rw-r--r--llvm/include/llvm/ProfileData/InstrProf.h30
-rw-r--r--llvm/lib/ProfileData/InstrProf.cpp66
2 files changed, 96 insertions, 0 deletions
diff --git a/llvm/include/llvm/ProfileData/InstrProf.h b/llvm/include/llvm/ProfileData/InstrProf.h
index 59baf30d7b3..0afbc38c374 100644
--- a/llvm/include/llvm/ProfileData/InstrProf.h
+++ b/llvm/include/llvm/ProfileData/InstrProf.h
@@ -18,6 +18,7 @@
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSet.h"
+#include "llvm/IR/GlobalValue.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MD5.h"
@@ -28,6 +29,10 @@
namespace llvm {
+class Function;
+class GlobalVariable;
+class Module;
+
/// Return the name of data section containing profile counter variables.
inline StringRef getInstrProfCountersSectionName(bool AddSegment) {
return AddSegment ? "__DATA,__llvm_prf_cnts" : "__llvm_prf_cnts";
@@ -113,6 +118,31 @@ inline StringRef getInstrProfFileOverriderFuncName() {
return "__llvm_profile_override_default_filename";
}
+/// Return the modified name for function \c F suitable to be
+/// used the key for profile lookup.
+std::string getPGOFuncName(const Function &F);
+
+/// Return the modified name for a function suitable to be
+/// used the key for profile lookup. The function's original
+/// name is \c RawFuncName and has linkage of type \c Linkage.
+/// The function is defined in module \c FileName.
+std::string getPGOFuncName(StringRef RawFuncName,
+ GlobalValue::LinkageTypes Linkage,
+ StringRef FileName);
+
+/// Create and return the global variable for function name used in PGO
+/// instrumentation. \c FuncName is the name of the function returned
+/// by \c getPGOFuncName call.
+GlobalVariable *createPGOFuncNameVar(Function &F, StringRef FuncName);
+
+/// Create and return the global variable for function name used in PGO
+/// instrumentation. /// \c FuncName is the name of the function
+/// returned by \c getPGOFuncName call, \c M is the owning module,
+/// and \c Linkage is the linkage of the instrumented function.
+GlobalVariable *createPGOFuncNameVar(Module &M,
+ GlobalValue::LinkageTypes Linkage,
+ StringRef FuncName);
+
const std::error_category &instrprof_category();
enum class instrprof_error {
diff --git a/llvm/lib/ProfileData/InstrProf.cpp b/llvm/lib/ProfileData/InstrProf.cpp
index 9652ba88483..377d77197d9 100644
--- a/llvm/lib/ProfileData/InstrProf.cpp
+++ b/llvm/lib/ProfileData/InstrProf.cpp
@@ -12,6 +12,10 @@
//
//===----------------------------------------------------------------------===//
+#include "llvm/IR/Constants.h"
+#include "llvm/IR/Function.h"
+#include "llvm/IR/Module.h"
+#include "llvm/IR/GlobalVariable.h"
#include "llvm/ProfileData/InstrProf.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/ManagedStatic.h"
@@ -63,3 +67,65 @@ static ManagedStatic<InstrProfErrorCategoryType> ErrorCategory;
const std::error_category &llvm::instrprof_category() {
return *ErrorCategory;
}
+
+namespace llvm {
+
+std::string getPGOFuncName(StringRef RawFuncName,
+ GlobalValue::LinkageTypes Linkage,
+ StringRef FileName) {
+
+ // Function names may be prefixed with a binary '1' to indicate
+ // that the backend should not modify the symbols due to any platform
+ // naming convention. Do not include that '1' in the PGO profile name.
+ if (RawFuncName[0] == '\1')
+ RawFuncName = RawFuncName.substr(1);
+
+ std::string FuncName = RawFuncName;
+ if (llvm::GlobalValue::isLocalLinkage(Linkage)) {
+ // For local symbols, prepend the main file name to distinguish them.
+ // Do not include the full path in the file name since there's no guarantee
+ // that it will stay the same, e.g., if the files are checked out from
+ // version control in different locations.
+ if (FileName.empty())
+ FuncName = FuncName.insert(0, "<unknown>:");
+ else
+ FuncName = FuncName.insert(0, FileName.str() + ":");
+ }
+ return FuncName;
+}
+
+std::string getPGOFuncName(const Function &F) {
+ return getPGOFuncName(F.getName(), F.getLinkage(), F.getParent()->getName());
+}
+
+GlobalVariable *createPGOFuncNameVar(Module &M,
+ GlobalValue::LinkageTypes Linkage,
+ StringRef FuncName) {
+
+ // We generally want to match the function's linkage, but available_externally
+ // and extern_weak both have the wrong semantics, and anything that doesn't
+ // need to link across compilation units doesn't need to be visible at all.
+ if (Linkage == GlobalValue::ExternalWeakLinkage)
+ Linkage = GlobalValue::LinkOnceAnyLinkage;
+ else if (Linkage == GlobalValue::AvailableExternallyLinkage)
+ Linkage = GlobalValue::LinkOnceODRLinkage;
+ else if (Linkage == GlobalValue::InternalLinkage ||
+ Linkage == GlobalValue::ExternalLinkage)
+ Linkage = GlobalValue::PrivateLinkage;
+
+ auto *Value = ConstantDataArray::getString(M.getContext(), FuncName, false);
+ auto FuncNameVar =
+ new GlobalVariable(M, Value->getType(), true, Linkage, Value,
+ Twine(getInstrProfNameVarPrefix()) + FuncName);
+
+ // Hide the symbol so that we correctly get a copy for each executable.
+ if (!GlobalValue::isLocalLinkage(FuncNameVar->getLinkage()))
+ FuncNameVar->setVisibility(GlobalValue::HiddenVisibility);
+
+ return FuncNameVar;
+}
+
+GlobalVariable *createPGOFuncNameVar(Function &F, StringRef FuncName) {
+ return createPGOFuncNameVar(*F.getParent(), F.getLinkage(), FuncName);
+}
+}
OpenPOWER on IntegriCloud