377d77197d98237e28aec9affdec5841bcf45572
[oota-llvm.git] / lib / ProfileData / InstrProf.cpp
1 //=-- InstrProf.cpp - Instrumented profiling format support -----------------=//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains support for clang's instrumentation based PGO and
11 // coverage.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/IR/Constants.h"
16 #include "llvm/IR/Function.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/IR/GlobalVariable.h"
19 #include "llvm/ProfileData/InstrProf.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/ManagedStatic.h"
22
23 using namespace llvm;
24
25 namespace {
26 class InstrProfErrorCategoryType : public std::error_category {
27   const char *name() const LLVM_NOEXCEPT override { return "llvm.instrprof"; }
28   std::string message(int IE) const override {
29     instrprof_error E = static_cast<instrprof_error>(IE);
30     switch (E) {
31     case instrprof_error::success:
32       return "Success";
33     case instrprof_error::eof:
34       return "End of File";
35     case instrprof_error::bad_magic:
36       return "Invalid profile data (bad magic)";
37     case instrprof_error::bad_header:
38       return "Invalid profile data (file header is corrupt)";
39     case instrprof_error::unsupported_version:
40       return "Unsupported profiling format version";
41     case instrprof_error::unsupported_hash_type:
42       return "Unsupported profiling hash";
43     case instrprof_error::too_large:
44       return "Too much profile data";
45     case instrprof_error::truncated:
46       return "Truncated profile data";
47     case instrprof_error::malformed:
48       return "Malformed profile data";
49     case instrprof_error::unknown_function:
50       return "No profile data available for function";
51     case instrprof_error::hash_mismatch:
52       return "Function hash mismatch";
53     case instrprof_error::count_mismatch:
54       return "Function count mismatch";
55     case instrprof_error::counter_overflow:
56       return "Counter overflow";
57     case instrprof_error::value_site_count_mismatch:
58       return "Function's value site counts mismatch";
59     }
60     llvm_unreachable("A value of instrprof_error has no message.");
61   }
62 };
63 }
64
65 static ManagedStatic<InstrProfErrorCategoryType> ErrorCategory;
66
67 const std::error_category &llvm::instrprof_category() {
68   return *ErrorCategory;
69 }
70
71 namespace llvm {
72
73 std::string getPGOFuncName(StringRef RawFuncName,
74                            GlobalValue::LinkageTypes Linkage,
75                            StringRef FileName) {
76
77   // Function names may be prefixed with a binary '1' to indicate
78   // that the backend should not modify the symbols due to any platform
79   // naming convention. Do not include that '1' in the PGO profile name.
80   if (RawFuncName[0] == '\1')
81     RawFuncName = RawFuncName.substr(1);
82
83   std::string FuncName = RawFuncName;
84   if (llvm::GlobalValue::isLocalLinkage(Linkage)) {
85     // For local symbols, prepend the main file name to distinguish them.
86     // Do not include the full path in the file name since there's no guarantee
87     // that it will stay the same, e.g., if the files are checked out from
88     // version control in different locations.
89     if (FileName.empty())
90       FuncName = FuncName.insert(0, "<unknown>:");
91     else
92       FuncName = FuncName.insert(0, FileName.str() + ":");
93   }
94   return FuncName;
95 }
96
97 std::string getPGOFuncName(const Function &F) {
98   return getPGOFuncName(F.getName(), F.getLinkage(), F.getParent()->getName());
99 }
100
101 GlobalVariable *createPGOFuncNameVar(Module &M,
102                                      GlobalValue::LinkageTypes Linkage,
103                                      StringRef FuncName) {
104
105   // We generally want to match the function's linkage, but available_externally
106   // and extern_weak both have the wrong semantics, and anything that doesn't
107   // need to link across compilation units doesn't need to be visible at all.
108   if (Linkage == GlobalValue::ExternalWeakLinkage)
109     Linkage = GlobalValue::LinkOnceAnyLinkage;
110   else if (Linkage == GlobalValue::AvailableExternallyLinkage)
111     Linkage = GlobalValue::LinkOnceODRLinkage;
112   else if (Linkage == GlobalValue::InternalLinkage ||
113            Linkage == GlobalValue::ExternalLinkage)
114     Linkage = GlobalValue::PrivateLinkage;
115
116   auto *Value = ConstantDataArray::getString(M.getContext(), FuncName, false);
117   auto FuncNameVar =
118       new GlobalVariable(M, Value->getType(), true, Linkage, Value,
119                          Twine(getInstrProfNameVarPrefix()) + FuncName);
120
121   // Hide the symbol so that we correctly get a copy for each executable.
122   if (!GlobalValue::isLocalLinkage(FuncNameVar->getLinkage()))
123     FuncNameVar->setVisibility(GlobalValue::HiddenVisibility);
124
125   return FuncNameVar;
126 }
127
128 GlobalVariable *createPGOFuncNameVar(Function &F, StringRef FuncName) {
129   return createPGOFuncNameVar(*F.getParent(), F.getLinkage(), FuncName);
130 }
131 }