blob: dac919501838ef0900e68c7915f5a52b6da31afa [file] [log] [blame]
Justin Bognerf8d79192014-03-21 17:24:48 +00001//=-- 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
Xinliang David Lie413f1a2015-12-31 07:57:16 +000015#include "llvm/ProfileData/InstrProf.h"
Xinliang David Li0c677872016-01-04 21:31:09 +000016#include "llvm/ADT/StringExtras.h"
Rong Xu97b68c52016-07-21 20:50:02 +000017#include "llvm/ADT/Triple.h"
Xinliang David Li441959d2015-11-09 00:01:22 +000018#include "llvm/IR/Constants.h"
19#include "llvm/IR/Function.h"
Xinliang David Li441959d2015-11-09 00:01:22 +000020#include "llvm/IR/GlobalVariable.h"
Xinliang David Li402477d2016-02-04 19:11:43 +000021#include "llvm/IR/MDBuilder.h"
Xinliang David Lie413f1a2015-12-31 07:57:16 +000022#include "llvm/IR/Module.h"
23#include "llvm/Support/Compression.h"
Justin Bognerf8d79192014-03-21 17:24:48 +000024#include "llvm/Support/ErrorHandling.h"
Xinliang David Lie413f1a2015-12-31 07:57:16 +000025#include "llvm/Support/LEB128.h"
Chris Bieneman1efe8012014-09-19 23:19:24 +000026#include "llvm/Support/ManagedStatic.h"
Xinliang David Li9eb472b2016-07-12 17:14:51 +000027#include "llvm/Support/Path.h"
Justin Bognerf8d79192014-03-21 17:24:48 +000028
29using namespace llvm;
30
Xinliang David Li9eb472b2016-07-12 17:14:51 +000031static cl::opt<bool> StaticFuncFullModulePrefix(
32 "static-func-full-module-prefix", cl::init(false),
33 cl::desc("Use full module build paths in the profile counter names for "
34 "static functions."));
35
Justin Bognerf8d79192014-03-21 17:24:48 +000036namespace {
Vedant Kumar9152fd12016-05-19 03:54:45 +000037std::string getInstrProfErrString(instrprof_error Err) {
38 switch (Err) {
39 case instrprof_error::success:
40 return "Success";
41 case instrprof_error::eof:
42 return "End of File";
43 case instrprof_error::unrecognized_format:
44 return "Unrecognized instrumentation profile encoding format";
45 case instrprof_error::bad_magic:
46 return "Invalid instrumentation profile data (bad magic)";
47 case instrprof_error::bad_header:
48 return "Invalid instrumentation profile data (file header is corrupt)";
49 case instrprof_error::unsupported_version:
50 return "Unsupported instrumentation profile format version";
51 case instrprof_error::unsupported_hash_type:
52 return "Unsupported instrumentation profile hash type";
53 case instrprof_error::too_large:
54 return "Too much profile data";
55 case instrprof_error::truncated:
56 return "Truncated profile data";
57 case instrprof_error::malformed:
58 return "Malformed instrumentation profile data";
59 case instrprof_error::unknown_function:
60 return "No profile data available for function";
61 case instrprof_error::hash_mismatch:
62 return "Function control flow change detected (hash mismatch)";
63 case instrprof_error::count_mismatch:
64 return "Function basic block count change detected (counter mismatch)";
65 case instrprof_error::counter_overflow:
66 return "Counter overflow";
67 case instrprof_error::value_site_count_mismatch:
68 return "Function value site count change detected (counter mismatch)";
69 case instrprof_error::compress_failed:
70 return "Failed to compress data (zlib)";
71 case instrprof_error::uncompress_failed:
72 return "Failed to uncompress data (zlib)";
73 }
74 llvm_unreachable("A value of instrprof_error has no message.");
75}
76
Peter Collingbourne4718f8b2016-05-24 20:13:46 +000077// FIXME: This class is only here to support the transition to llvm::Error. It
78// will be removed once this transition is complete. Clients should prefer to
79// deal with the Error value directly, rather than converting to error_code.
Rafael Espindola25188c92014-06-12 01:45:43 +000080class InstrProfErrorCategoryType : public std::error_category {
Rafael Espindolaf5d07fa2014-06-10 21:26:47 +000081 const char *name() const LLVM_NOEXCEPT override { return "llvm.instrprof"; }
Justin Bognerf8d79192014-03-21 17:24:48 +000082 std::string message(int IE) const override {
Vedant Kumar9152fd12016-05-19 03:54:45 +000083 return getInstrProfErrString(static_cast<instrprof_error>(IE));
Justin Bognerf8d79192014-03-21 17:24:48 +000084 }
Justin Bognerf8d79192014-03-21 17:24:48 +000085};
Eugene Zelenko6ac3f732016-01-26 18:48:36 +000086} // end anonymous namespace
Justin Bognerf8d79192014-03-21 17:24:48 +000087
Chris Bieneman1efe8012014-09-19 23:19:24 +000088static ManagedStatic<InstrProfErrorCategoryType> ErrorCategory;
89
Rafael Espindola25188c92014-06-12 01:45:43 +000090const std::error_category &llvm::instrprof_category() {
Chris Bieneman1efe8012014-09-19 23:19:24 +000091 return *ErrorCategory;
Justin Bognerf8d79192014-03-21 17:24:48 +000092}
Xinliang David Li441959d2015-11-09 00:01:22 +000093
94namespace llvm {
95
Vedant Kumar42369db2016-05-11 19:42:19 +000096void SoftInstrProfErrors::addError(instrprof_error IE) {
97 if (IE == instrprof_error::success)
98 return;
99
100 if (FirstError == instrprof_error::success)
101 FirstError = IE;
102
103 switch (IE) {
104 case instrprof_error::hash_mismatch:
105 ++NumHashMismatches;
106 break;
107 case instrprof_error::count_mismatch:
108 ++NumCountMismatches;
109 break;
110 case instrprof_error::counter_overflow:
111 ++NumCounterOverflows;
112 break;
113 case instrprof_error::value_site_count_mismatch:
114 ++NumValueSiteCountMismatches;
115 break;
116 default:
117 llvm_unreachable("Not a soft error");
118 }
119}
120
Vedant Kumar9152fd12016-05-19 03:54:45 +0000121std::string InstrProfError::message() const {
122 return getInstrProfErrString(Err);
123}
124
125char InstrProfError::ID = 0;
126
Xinliang David Li441959d2015-11-09 00:01:22 +0000127std::string getPGOFuncName(StringRef RawFuncName,
128 GlobalValue::LinkageTypes Linkage,
Xinliang David Lia86545b2015-12-11 20:23:22 +0000129 StringRef FileName,
130 uint64_t Version LLVM_ATTRIBUTE_UNUSED) {
Teresa Johnsonb43027d2016-03-15 02:13:19 +0000131 return GlobalValue::getGlobalIdentifier(RawFuncName, Linkage, FileName);
Xinliang David Li441959d2015-11-09 00:01:22 +0000132}
133
Rong Xub5341662016-03-30 18:37:52 +0000134// Return the PGOFuncName. This function has some special handling when called
135// in LTO optimization. The following only applies when calling in LTO passes
136// (when \c InLTO is true): LTO's internalization privatizes many global linkage
137// symbols. This happens after value profile annotation, but those internal
138// linkage functions should not have a source prefix.
Teresa Johnson8c1bc982016-08-29 22:46:56 +0000139// Additionally, for ThinLTO mode, exported internal functions are promoted
140// and renamed. We need to ensure that the original internal PGO name is
141// used when computing the GUID that is compared against the profiled GUIDs.
Rong Xub5341662016-03-30 18:37:52 +0000142// To differentiate compiler generated internal symbols from original ones,
143// PGOFuncName meta data are created and attached to the original internal
144// symbols in the value profile annotation step
145// (PGOUseFunc::annotateIndirectCallSites). If a symbol does not have the meta
146// data, its original linkage must be non-internal.
147std::string getPGOFuncName(const Function &F, bool InLTO, uint64_t Version) {
Xinliang David Li9eb472b2016-07-12 17:14:51 +0000148 if (!InLTO) {
149 StringRef FileName = (StaticFuncFullModulePrefix
150 ? F.getParent()->getName()
151 : sys::path::filename(F.getParent()->getName()));
152 return getPGOFuncName(F.getName(), F.getLinkage(), FileName, Version);
153 }
Rong Xub5341662016-03-30 18:37:52 +0000154
Rong Xu8e8fe852016-04-01 16:43:30 +0000155 // In LTO mode (when InLTO is true), first check if there is a meta data.
156 if (MDNode *MD = getPGOFuncNameMetadata(F)) {
Rong Xub5341662016-03-30 18:37:52 +0000157 StringRef S = cast<MDString>(MD->getOperand(0))->getString();
158 return S.str();
159 }
160
161 // If there is no meta data, the function must be a global before the value
162 // profile annotation pass. Its current linkage may be internal if it is
163 // internalized in LTO mode.
Rong Xu8e8fe852016-04-01 16:43:30 +0000164 return getPGOFuncName(F.getName(), GlobalValue::ExternalLinkage, "");
Xinliang David Li441959d2015-11-09 00:01:22 +0000165}
166
Xinliang David Li4ec40142015-12-15 19:44:45 +0000167StringRef getFuncNameWithoutPrefix(StringRef PGOFuncName, StringRef FileName) {
168 if (FileName.empty())
Vedant Kumar43a85652016-03-28 15:49:08 +0000169 return PGOFuncName;
Xinliang David Li4ec40142015-12-15 19:44:45 +0000170 // Drop the file name including ':'. See also getPGOFuncName.
171 if (PGOFuncName.startswith(FileName))
172 PGOFuncName = PGOFuncName.drop_front(FileName.size() + 1);
173 return PGOFuncName;
174}
175
Xinliang David Lid1bab962015-12-12 17:28:03 +0000176// \p FuncName is the string used as profile lookup key for the function. A
177// symbol is created to hold the name. Return the legalized symbol name.
Vedant Kumaraa0cae62016-03-16 20:49:26 +0000178std::string getPGOFuncNameVarName(StringRef FuncName,
179 GlobalValue::LinkageTypes Linkage) {
Xinliang David Lid1bab962015-12-12 17:28:03 +0000180 std::string VarName = getInstrProfNameVarPrefix();
181 VarName += FuncName;
182
183 if (!GlobalValue::isLocalLinkage(Linkage))
184 return VarName;
185
186 // Now fix up illegal chars in local VarName that may upset the assembler.
Xinliang David Lieeaf0bc2016-06-10 06:32:26 +0000187 const char *InvalidChars = "-:<>/\"'";
Xinliang David Lid1bab962015-12-12 17:28:03 +0000188 size_t found = VarName.find_first_of(InvalidChars);
189 while (found != std::string::npos) {
190 VarName[found] = '_';
191 found = VarName.find_first_of(InvalidChars, found + 1);
192 }
193 return VarName;
194}
195
Xinliang David Li441959d2015-11-09 00:01:22 +0000196GlobalVariable *createPGOFuncNameVar(Module &M,
197 GlobalValue::LinkageTypes Linkage,
Xinliang David Li897d2922016-03-16 22:13:41 +0000198 StringRef PGOFuncName) {
Xinliang David Li441959d2015-11-09 00:01:22 +0000199
200 // We generally want to match the function's linkage, but available_externally
201 // and extern_weak both have the wrong semantics, and anything that doesn't
202 // need to link across compilation units doesn't need to be visible at all.
203 if (Linkage == GlobalValue::ExternalWeakLinkage)
204 Linkage = GlobalValue::LinkOnceAnyLinkage;
205 else if (Linkage == GlobalValue::AvailableExternallyLinkage)
206 Linkage = GlobalValue::LinkOnceODRLinkage;
207 else if (Linkage == GlobalValue::InternalLinkage ||
208 Linkage == GlobalValue::ExternalLinkage)
209 Linkage = GlobalValue::PrivateLinkage;
210
Xinliang David Li897d2922016-03-16 22:13:41 +0000211 auto *Value =
212 ConstantDataArray::getString(M.getContext(), PGOFuncName, false);
Xinliang David Li441959d2015-11-09 00:01:22 +0000213 auto FuncNameVar =
214 new GlobalVariable(M, Value->getType(), true, Linkage, Value,
Xinliang David Li897d2922016-03-16 22:13:41 +0000215 getPGOFuncNameVarName(PGOFuncName, Linkage));
Xinliang David Li441959d2015-11-09 00:01:22 +0000216
217 // Hide the symbol so that we correctly get a copy for each executable.
218 if (!GlobalValue::isLocalLinkage(FuncNameVar->getLinkage()))
219 FuncNameVar->setVisibility(GlobalValue::HiddenVisibility);
220
221 return FuncNameVar;
222}
223
Xinliang David Li897d2922016-03-16 22:13:41 +0000224GlobalVariable *createPGOFuncNameVar(Function &F, StringRef PGOFuncName) {
225 return createPGOFuncNameVar(*F.getParent(), F.getLinkage(), PGOFuncName);
Xinliang David Li441959d2015-11-09 00:01:22 +0000226}
Xinliang David Liee415892015-11-10 00:24:45 +0000227
Rong Xub5341662016-03-30 18:37:52 +0000228void InstrProfSymtab::create(Module &M, bool InLTO) {
229 for (Function &F : M) {
230 // Function may not have a name: like using asm("") to overwrite the name.
231 // Ignore in this case.
232 if (!F.hasName())
233 continue;
234 const std::string &PGOFuncName = getPGOFuncName(F, InLTO);
235 addFuncName(PGOFuncName);
Rong Xud5a57b52016-03-31 17:39:33 +0000236 MD5FuncMap.emplace_back(Function::getGUID(PGOFuncName), &F);
Rong Xub5341662016-03-30 18:37:52 +0000237 }
Xinliang David Li59411db2016-01-20 01:26:34 +0000238
239 finalizeSymtab();
240}
241
Vedant Kumar9152fd12016-05-19 03:54:45 +0000242Error collectPGOFuncNameStrings(const std::vector<std::string> &NameStrs,
243 bool doCompression, std::string &Result) {
Vedant Kumar86705ba2016-03-28 21:06:42 +0000244 assert(NameStrs.size() && "No name data to emit");
245
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000246 uint8_t Header[16], *P = Header;
Xinliang David Li0c677872016-01-04 21:31:09 +0000247 std::string UncompressedNameStrings =
Vedant Kumar86705ba2016-03-28 21:06:42 +0000248 join(NameStrs.begin(), NameStrs.end(), getInstrProfNameSeparator());
249
250 assert(StringRef(UncompressedNameStrings)
251 .count(getInstrProfNameSeparator()) == (NameStrs.size() - 1) &&
252 "PGO name is invalid (contains separator token)");
Xinliang David Li13ea29b2016-01-04 20:26:05 +0000253
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000254 unsigned EncLen = encodeULEB128(UncompressedNameStrings.length(), P);
255 P += EncLen;
Xinliang David Li120fe2e2016-01-04 22:01:02 +0000256
Benjamin Kramer0da23a22016-05-29 10:31:00 +0000257 auto WriteStringToResult = [&](size_t CompressedLen, StringRef InputStr) {
Xinliang David Li120fe2e2016-01-04 22:01:02 +0000258 EncLen = encodeULEB128(CompressedLen, P);
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000259 P += EncLen;
Xinliang David Li120fe2e2016-01-04 22:01:02 +0000260 char *HeaderStr = reinterpret_cast<char *>(&Header[0]);
261 unsigned HeaderLen = P - &Header[0];
262 Result.append(HeaderStr, HeaderLen);
263 Result += InputStr;
Vedant Kumar9152fd12016-05-19 03:54:45 +0000264 return Error::success();
Xinliang David Li120fe2e2016-01-04 22:01:02 +0000265 };
266
Vedant Kumar9152fd12016-05-19 03:54:45 +0000267 if (!doCompression) {
Xinliang David Li120fe2e2016-01-04 22:01:02 +0000268 return WriteStringToResult(0, UncompressedNameStrings);
Vedant Kumar9152fd12016-05-19 03:54:45 +0000269 }
Xinliang David Li120fe2e2016-01-04 22:01:02 +0000270
Benjamin Kramer0da23a22016-05-29 10:31:00 +0000271 SmallString<128> CompressedNameStrings;
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000272 zlib::Status Success =
273 zlib::compress(StringRef(UncompressedNameStrings), CompressedNameStrings,
274 zlib::BestSizeCompression);
Xinliang David Li120fe2e2016-01-04 22:01:02 +0000275
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000276 if (Success != zlib::StatusOK)
Vedant Kumar9152fd12016-05-19 03:54:45 +0000277 return make_error<InstrProfError>(instrprof_error::compress_failed);
Xinliang David Li120fe2e2016-01-04 22:01:02 +0000278
Benjamin Kramer0da23a22016-05-29 10:31:00 +0000279 return WriteStringToResult(CompressedNameStrings.size(),
280 CompressedNameStrings);
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000281}
282
Xinliang David Lieb7d7f82016-02-04 23:59:09 +0000283StringRef getPGOFuncNameVarInitializer(GlobalVariable *NameVar) {
Xinliang David Li37c1fa02016-01-03 04:38:13 +0000284 auto *Arr = cast<ConstantDataArray>(NameVar->getInitializer());
285 StringRef NameStr =
286 Arr->isCString() ? Arr->getAsCString() : Arr->getAsString();
287 return NameStr;
288}
289
Vedant Kumar9152fd12016-05-19 03:54:45 +0000290Error collectPGOFuncNameStrings(const std::vector<GlobalVariable *> &NameVars,
291 std::string &Result, bool doCompression) {
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000292 std::vector<std::string> NameStrs;
293 for (auto *NameVar : NameVars) {
Xinliang David Lieb7d7f82016-02-04 23:59:09 +0000294 NameStrs.push_back(getPGOFuncNameVarInitializer(NameVar));
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000295 }
Xinliang David Li73163752016-01-26 23:13:00 +0000296 return collectPGOFuncNameStrings(
297 NameStrs, zlib::isAvailable() && doCompression, Result);
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000298}
299
Vedant Kumar9152fd12016-05-19 03:54:45 +0000300Error readPGOFuncNameStrings(StringRef NameStrings, InstrProfSymtab &Symtab) {
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000301 const uint8_t *P = reinterpret_cast<const uint8_t *>(NameStrings.data());
302 const uint8_t *EndP = reinterpret_cast<const uint8_t *>(NameStrings.data() +
303 NameStrings.size());
304 while (P < EndP) {
305 uint32_t N;
306 uint64_t UncompressedSize = decodeULEB128(P, &N);
307 P += N;
308 uint64_t CompressedSize = decodeULEB128(P, &N);
309 P += N;
310 bool isCompressed = (CompressedSize != 0);
311 SmallString<128> UncompressedNameStrings;
312 StringRef NameStrings;
313 if (isCompressed) {
314 StringRef CompressedNameStrings(reinterpret_cast<const char *>(P),
315 CompressedSize);
316 if (zlib::uncompress(CompressedNameStrings, UncompressedNameStrings,
317 UncompressedSize) != zlib::StatusOK)
Vedant Kumar9152fd12016-05-19 03:54:45 +0000318 return make_error<InstrProfError>(instrprof_error::uncompress_failed);
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000319 P += CompressedSize;
320 NameStrings = StringRef(UncompressedNameStrings.data(),
321 UncompressedNameStrings.size());
322 } else {
323 NameStrings =
324 StringRef(reinterpret_cast<const char *>(P), UncompressedSize);
325 P += UncompressedSize;
326 }
327 // Now parse the name strings.
Xinliang David Li204efe22016-01-04 22:09:26 +0000328 SmallVector<StringRef, 0> Names;
Vedant Kumar86705ba2016-03-28 21:06:42 +0000329 NameStrings.split(Names, getInstrProfNameSeparator());
Xinliang David Li204efe22016-01-04 22:09:26 +0000330 for (StringRef &Name : Names)
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000331 Symtab.addFuncName(Name);
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000332
333 while (P < EndP && *P == 0)
334 P++;
335 }
336 Symtab.finalizeSymtab();
Vedant Kumar9152fd12016-05-19 03:54:45 +0000337 return Error::success();
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000338}
339
Vedant Kumar42369db2016-05-11 19:42:19 +0000340void InstrProfValueSiteRecord::merge(SoftInstrProfErrors &SIPE,
341 InstrProfValueSiteRecord &Input,
342 uint64_t Weight) {
Xinliang David Li5c24da52015-12-20 05:15:45 +0000343 this->sortByTargetValues();
344 Input.sortByTargetValues();
345 auto I = ValueData.begin();
346 auto IE = ValueData.end();
Xinliang David Li5c24da52015-12-20 05:15:45 +0000347 for (auto J = Input.ValueData.begin(), JE = Input.ValueData.end(); J != JE;
348 ++J) {
349 while (I != IE && I->Value < J->Value)
350 ++I;
351 if (I != IE && I->Value == J->Value) {
Xinliang David Li5c24da52015-12-20 05:15:45 +0000352 bool Overflowed;
Nathan Slingerland7bee3162016-01-12 22:34:00 +0000353 I->Count = SaturatingMultiplyAdd(J->Count, Weight, I->Count, &Overflowed);
Xinliang David Li5c24da52015-12-20 05:15:45 +0000354 if (Overflowed)
Vedant Kumar42369db2016-05-11 19:42:19 +0000355 SIPE.addError(instrprof_error::counter_overflow);
Xinliang David Li5c24da52015-12-20 05:15:45 +0000356 ++I;
357 continue;
358 }
359 ValueData.insert(I, *J);
360 }
Xinliang David Li5c24da52015-12-20 05:15:45 +0000361}
362
Vedant Kumar42369db2016-05-11 19:42:19 +0000363void InstrProfValueSiteRecord::scale(SoftInstrProfErrors &SIPE,
364 uint64_t Weight) {
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000365 for (auto I = ValueData.begin(), IE = ValueData.end(); I != IE; ++I) {
366 bool Overflowed;
367 I->Count = SaturatingMultiply(I->Count, Weight, &Overflowed);
368 if (Overflowed)
Vedant Kumar42369db2016-05-11 19:42:19 +0000369 SIPE.addError(instrprof_error::counter_overflow);
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000370 }
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000371}
372
Xinliang David Li020f22d2015-12-18 23:06:37 +0000373// Merge Value Profile data from Src record to this record for ValueKind.
374// Scale merged value counts by \p Weight.
Vedant Kumar42369db2016-05-11 19:42:19 +0000375void InstrProfRecord::mergeValueProfData(uint32_t ValueKind,
376 InstrProfRecord &Src,
377 uint64_t Weight) {
Xinliang David Li020f22d2015-12-18 23:06:37 +0000378 uint32_t ThisNumValueSites = getNumValueSites(ValueKind);
379 uint32_t OtherNumValueSites = Src.getNumValueSites(ValueKind);
Vedant Kumar42369db2016-05-11 19:42:19 +0000380 if (ThisNumValueSites != OtherNumValueSites) {
381 SIPE.addError(instrprof_error::value_site_count_mismatch);
382 return;
383 }
Xinliang David Li020f22d2015-12-18 23:06:37 +0000384 std::vector<InstrProfValueSiteRecord> &ThisSiteRecords =
385 getValueSitesForKind(ValueKind);
386 std::vector<InstrProfValueSiteRecord> &OtherSiteRecords =
387 Src.getValueSitesForKind(ValueKind);
Xinliang David Li020f22d2015-12-18 23:06:37 +0000388 for (uint32_t I = 0; I < ThisNumValueSites; I++)
Vedant Kumar42369db2016-05-11 19:42:19 +0000389 ThisSiteRecords[I].merge(SIPE, OtherSiteRecords[I], Weight);
Xinliang David Li020f22d2015-12-18 23:06:37 +0000390}
391
Vedant Kumar42369db2016-05-11 19:42:19 +0000392void InstrProfRecord::merge(InstrProfRecord &Other, uint64_t Weight) {
Xinliang David Li020f22d2015-12-18 23:06:37 +0000393 // If the number of counters doesn't match we either have bad data
394 // or a hash collision.
Vedant Kumar42369db2016-05-11 19:42:19 +0000395 if (Counts.size() != Other.Counts.size()) {
396 SIPE.addError(instrprof_error::count_mismatch);
397 return;
398 }
Xinliang David Li020f22d2015-12-18 23:06:37 +0000399
400 for (size_t I = 0, E = Other.Counts.size(); I < E; ++I) {
401 bool Overflowed;
Nathan Slingerland7bee3162016-01-12 22:34:00 +0000402 Counts[I] =
403 SaturatingMultiplyAdd(Other.Counts[I], Weight, Counts[I], &Overflowed);
Xinliang David Li020f22d2015-12-18 23:06:37 +0000404 if (Overflowed)
Vedant Kumar42369db2016-05-11 19:42:19 +0000405 SIPE.addError(instrprof_error::counter_overflow);
Xinliang David Li020f22d2015-12-18 23:06:37 +0000406 }
407
408 for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
Vedant Kumar42369db2016-05-11 19:42:19 +0000409 mergeValueProfData(Kind, Other, Weight);
Xinliang David Li020f22d2015-12-18 23:06:37 +0000410}
Xinliang David Lia716cc52015-12-20 06:22:13 +0000411
Vedant Kumar42369db2016-05-11 19:42:19 +0000412void InstrProfRecord::scaleValueProfData(uint32_t ValueKind, uint64_t Weight) {
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000413 uint32_t ThisNumValueSites = getNumValueSites(ValueKind);
414 std::vector<InstrProfValueSiteRecord> &ThisSiteRecords =
415 getValueSitesForKind(ValueKind);
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000416 for (uint32_t I = 0; I < ThisNumValueSites; I++)
Vedant Kumar42369db2016-05-11 19:42:19 +0000417 ThisSiteRecords[I].scale(SIPE, Weight);
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000418}
419
Vedant Kumar42369db2016-05-11 19:42:19 +0000420void InstrProfRecord::scale(uint64_t Weight) {
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000421 for (auto &Count : this->Counts) {
422 bool Overflowed;
423 Count = SaturatingMultiply(Count, Weight, &Overflowed);
Vedant Kumar42369db2016-05-11 19:42:19 +0000424 if (Overflowed)
425 SIPE.addError(instrprof_error::counter_overflow);
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000426 }
427 for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
Vedant Kumar42369db2016-05-11 19:42:19 +0000428 scaleValueProfData(Kind, Weight);
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000429}
430
Xinliang David Li020f22d2015-12-18 23:06:37 +0000431// Map indirect call target name hash to name string.
432uint64_t InstrProfRecord::remapValue(uint64_t Value, uint32_t ValueKind,
Xinliang David Lia716cc52015-12-20 06:22:13 +0000433 ValueMapType *ValueMap) {
434 if (!ValueMap)
Xinliang David Li020f22d2015-12-18 23:06:37 +0000435 return Value;
436 switch (ValueKind) {
437 case IPVK_IndirectCallTarget: {
438 auto Result =
Xinliang David Lia716cc52015-12-20 06:22:13 +0000439 std::lower_bound(ValueMap->begin(), ValueMap->end(), Value,
440 [](const std::pair<uint64_t, uint64_t> &LHS,
Xinliang David Li020f22d2015-12-18 23:06:37 +0000441 uint64_t RHS) { return LHS.first < RHS; });
Xinliang David Li8dd4ca82016-04-11 17:13:08 +0000442 // Raw function pointer collected by value profiler may be from
443 // external functions that are not instrumented. They won't have
444 // mapping data to be used by the deserializer. Force the value to
445 // be 0 in this case.
Xinliang David Li28464482016-04-10 03:32:02 +0000446 if (Result != ValueMap->end() && Result->first == Value)
Xinliang David Li020f22d2015-12-18 23:06:37 +0000447 Value = (uint64_t)Result->second;
Xinliang David Li28464482016-04-10 03:32:02 +0000448 else
449 Value = 0;
Xinliang David Li020f22d2015-12-18 23:06:37 +0000450 break;
451 }
452 }
453 return Value;
454}
455
Xinliang David Li020f22d2015-12-18 23:06:37 +0000456void InstrProfRecord::addValueData(uint32_t ValueKind, uint32_t Site,
457 InstrProfValueData *VData, uint32_t N,
Xinliang David Lia716cc52015-12-20 06:22:13 +0000458 ValueMapType *ValueMap) {
Xinliang David Li020f22d2015-12-18 23:06:37 +0000459 for (uint32_t I = 0; I < N; I++) {
Xinliang David Lia716cc52015-12-20 06:22:13 +0000460 VData[I].Value = remapValue(VData[I].Value, ValueKind, ValueMap);
Xinliang David Li020f22d2015-12-18 23:06:37 +0000461 }
462 std::vector<InstrProfValueSiteRecord> &ValueSites =
463 getValueSitesForKind(ValueKind);
464 if (N == 0)
Vedant Kumar6b22ba62016-05-11 16:03:02 +0000465 ValueSites.emplace_back();
Xinliang David Li020f22d2015-12-18 23:06:37 +0000466 else
467 ValueSites.emplace_back(VData, VData + N);
468}
469
Xinliang David Lib75544a2015-11-28 19:07:09 +0000470#define INSTR_PROF_COMMON_API_IMPL
471#include "llvm/ProfileData/InstrProfData.inc"
Xinliang David Liee415892015-11-10 00:24:45 +0000472
Xinliang David Li020f22d2015-12-18 23:06:37 +0000473/*!
Xinliang David Lib75544a2015-11-28 19:07:09 +0000474 * \brief ValueProfRecordClosure Interface implementation for InstrProfRecord
Xinliang David Lied966772015-11-25 23:31:18 +0000475 * class. These C wrappers are used as adaptors so that C++ code can be
476 * invoked as callbacks.
477 */
Xinliang David Lif47cf552015-11-25 06:23:38 +0000478uint32_t getNumValueKindsInstrProf(const void *Record) {
479 return reinterpret_cast<const InstrProfRecord *>(Record)->getNumValueKinds();
480}
481
482uint32_t getNumValueSitesInstrProf(const void *Record, uint32_t VKind) {
483 return reinterpret_cast<const InstrProfRecord *>(Record)
484 ->getNumValueSites(VKind);
485}
486
487uint32_t getNumValueDataInstrProf(const void *Record, uint32_t VKind) {
488 return reinterpret_cast<const InstrProfRecord *>(Record)
489 ->getNumValueData(VKind);
490}
491
492uint32_t getNumValueDataForSiteInstrProf(const void *R, uint32_t VK,
493 uint32_t S) {
494 return reinterpret_cast<const InstrProfRecord *>(R)
495 ->getNumValueDataForSite(VK, S);
496}
497
498void getValueForSiteInstrProf(const void *R, InstrProfValueData *Dst,
Xinliang David Li1e4c8092016-02-04 05:29:51 +0000499 uint32_t K, uint32_t S) {
500 reinterpret_cast<const InstrProfRecord *>(R)->getValueForSite(Dst, K, S);
Xinliang David Lie8092312015-11-25 19:13:00 +0000501}
502
Xinliang David Lif47cf552015-11-25 06:23:38 +0000503ValueProfData *allocValueProfDataInstrProf(size_t TotalSizeInBytes) {
Xinliang David Li38b9a322015-12-15 21:57:08 +0000504 ValueProfData *VD =
505 (ValueProfData *)(new (::operator new(TotalSizeInBytes)) ValueProfData());
506 memset(VD, 0, TotalSizeInBytes);
507 return VD;
Xinliang David Lif47cf552015-11-25 06:23:38 +0000508}
509
510static ValueProfRecordClosure InstrProfRecordClosure = {
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000511 nullptr,
Xinliang David Lif47cf552015-11-25 06:23:38 +0000512 getNumValueKindsInstrProf,
513 getNumValueSitesInstrProf,
514 getNumValueDataInstrProf,
515 getNumValueDataForSiteInstrProf,
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000516 nullptr,
Xinliang David Lif47cf552015-11-25 06:23:38 +0000517 getValueForSiteInstrProf,
Xinliang David Li38b9a322015-12-15 21:57:08 +0000518 allocValueProfDataInstrProf};
Xinliang David Lif47cf552015-11-25 06:23:38 +0000519
Xinliang David Lie8092312015-11-25 19:13:00 +0000520// Wrapper implementation using the closure mechanism.
Xinliang David Lif47cf552015-11-25 06:23:38 +0000521uint32_t ValueProfData::getSize(const InstrProfRecord &Record) {
522 InstrProfRecordClosure.Record = &Record;
523 return getValueProfDataSize(&InstrProfRecordClosure);
524}
525
Xinliang David Lie8092312015-11-25 19:13:00 +0000526// Wrapper implementation using the closure mechanism.
Xinliang David Lif47cf552015-11-25 06:23:38 +0000527std::unique_ptr<ValueProfData>
528ValueProfData::serializeFrom(const InstrProfRecord &Record) {
529 InstrProfRecordClosure.Record = &Record;
530
531 std::unique_ptr<ValueProfData> VPD(
Xinliang David Li0e6a36e2015-12-01 19:47:32 +0000532 serializeValueProfDataFrom(&InstrProfRecordClosure, nullptr));
Xinliang David Lif47cf552015-11-25 06:23:38 +0000533 return VPD;
534}
535
Xinliang David Lie8092312015-11-25 19:13:00 +0000536void ValueProfRecord::deserializeTo(InstrProfRecord &Record,
537 InstrProfRecord::ValueMapType *VMap) {
538 Record.reserveSites(Kind, NumValueSites);
539
540 InstrProfValueData *ValueData = getValueProfRecordValueData(this);
541 for (uint64_t VSite = 0; VSite < NumValueSites; ++VSite) {
542 uint8_t ValueDataCount = this->SiteCountArray[VSite];
543 Record.addValueData(Kind, VSite, ValueData, ValueDataCount, VMap);
544 ValueData += ValueDataCount;
545 }
546}
Xinliang David Lied966772015-11-25 23:31:18 +0000547
Xinliang David Lie8092312015-11-25 19:13:00 +0000548// For writing/serializing, Old is the host endianness, and New is
549// byte order intended on disk. For Reading/deserialization, Old
550// is the on-disk source endianness, and New is the host endianness.
551void ValueProfRecord::swapBytes(support::endianness Old,
552 support::endianness New) {
553 using namespace support;
554 if (Old == New)
555 return;
556
557 if (getHostEndianness() != Old) {
558 sys::swapByteOrder<uint32_t>(NumValueSites);
559 sys::swapByteOrder<uint32_t>(Kind);
560 }
561 uint32_t ND = getValueProfRecordNumValueData(this);
562 InstrProfValueData *VD = getValueProfRecordValueData(this);
563
564 // No need to swap byte array: SiteCountArrray.
565 for (uint32_t I = 0; I < ND; I++) {
566 sys::swapByteOrder<uint64_t>(VD[I].Value);
567 sys::swapByteOrder<uint64_t>(VD[I].Count);
568 }
569 if (getHostEndianness() == Old) {
570 sys::swapByteOrder<uint32_t>(NumValueSites);
571 sys::swapByteOrder<uint32_t>(Kind);
572 }
573}
574
575void ValueProfData::deserializeTo(InstrProfRecord &Record,
576 InstrProfRecord::ValueMapType *VMap) {
577 if (NumValueKinds == 0)
578 return;
579
580 ValueProfRecord *VR = getFirstValueProfRecord(this);
581 for (uint32_t K = 0; K < NumValueKinds; K++) {
582 VR->deserializeTo(Record, VMap);
583 VR = getValueProfRecordNext(VR);
584 }
585}
586
587template <class T>
588static T swapToHostOrder(const unsigned char *&D, support::endianness Orig) {
589 using namespace support;
590 if (Orig == little)
591 return endian::readNext<T, little, unaligned>(D);
592 else
593 return endian::readNext<T, big, unaligned>(D);
594}
595
596static std::unique_ptr<ValueProfData> allocValueProfData(uint32_t TotalSize) {
597 return std::unique_ptr<ValueProfData>(new (::operator new(TotalSize))
598 ValueProfData());
599}
600
Vedant Kumar9152fd12016-05-19 03:54:45 +0000601Error ValueProfData::checkIntegrity() {
Xinliang David Li8e32f4d2015-11-28 04:56:07 +0000602 if (NumValueKinds > IPVK_Last + 1)
Vedant Kumar9152fd12016-05-19 03:54:45 +0000603 return make_error<InstrProfError>(instrprof_error::malformed);
Xinliang David Li8e32f4d2015-11-28 04:56:07 +0000604 // Total size needs to be mulltiple of quadword size.
605 if (TotalSize % sizeof(uint64_t))
Vedant Kumar9152fd12016-05-19 03:54:45 +0000606 return make_error<InstrProfError>(instrprof_error::malformed);
Xinliang David Li8e32f4d2015-11-28 04:56:07 +0000607
608 ValueProfRecord *VR = getFirstValueProfRecord(this);
609 for (uint32_t K = 0; K < this->NumValueKinds; K++) {
610 if (VR->Kind > IPVK_Last)
Vedant Kumar9152fd12016-05-19 03:54:45 +0000611 return make_error<InstrProfError>(instrprof_error::malformed);
Xinliang David Li8e32f4d2015-11-28 04:56:07 +0000612 VR = getValueProfRecordNext(VR);
613 if ((char *)VR - (char *)this > (ptrdiff_t)TotalSize)
Vedant Kumar9152fd12016-05-19 03:54:45 +0000614 return make_error<InstrProfError>(instrprof_error::malformed);
Xinliang David Li8e32f4d2015-11-28 04:56:07 +0000615 }
Vedant Kumar9152fd12016-05-19 03:54:45 +0000616 return Error::success();
Xinliang David Li8e32f4d2015-11-28 04:56:07 +0000617}
618
Vedant Kumar9152fd12016-05-19 03:54:45 +0000619Expected<std::unique_ptr<ValueProfData>>
Xinliang David Liee415892015-11-10 00:24:45 +0000620ValueProfData::getValueProfData(const unsigned char *D,
621 const unsigned char *const BufferEnd,
622 support::endianness Endianness) {
623 using namespace support;
624 if (D + sizeof(ValueProfData) > BufferEnd)
Vedant Kumar9152fd12016-05-19 03:54:45 +0000625 return make_error<InstrProfError>(instrprof_error::truncated);
Xinliang David Liee415892015-11-10 00:24:45 +0000626
Xinliang David Lib8c3ad12015-11-17 03:47:21 +0000627 const unsigned char *Header = D;
628 uint32_t TotalSize = swapToHostOrder<uint32_t>(Header, Endianness);
Xinliang David Liee415892015-11-10 00:24:45 +0000629 if (D + TotalSize > BufferEnd)
Vedant Kumar9152fd12016-05-19 03:54:45 +0000630 return make_error<InstrProfError>(instrprof_error::too_large);
Xinliang David Liee415892015-11-10 00:24:45 +0000631
Xinliang David Lif47cf552015-11-25 06:23:38 +0000632 std::unique_ptr<ValueProfData> VPD = allocValueProfData(TotalSize);
Xinliang David Liee415892015-11-10 00:24:45 +0000633 memcpy(VPD.get(), D, TotalSize);
634 // Byte swap.
635 VPD->swapBytesToHost(Endianness);
636
Vedant Kumar9152fd12016-05-19 03:54:45 +0000637 Error E = VPD->checkIntegrity();
638 if (E)
639 return std::move(E);
Xinliang David Liee415892015-11-10 00:24:45 +0000640
Xinliang David Liee415892015-11-10 00:24:45 +0000641 return std::move(VPD);
642}
643
644void ValueProfData::swapBytesToHost(support::endianness Endianness) {
645 using namespace support;
646 if (Endianness == getHostEndianness())
647 return;
648
649 sys::swapByteOrder<uint32_t>(TotalSize);
650 sys::swapByteOrder<uint32_t>(NumValueKinds);
651
Xinliang David Lif47cf552015-11-25 06:23:38 +0000652 ValueProfRecord *VR = getFirstValueProfRecord(this);
Xinliang David Liee415892015-11-10 00:24:45 +0000653 for (uint32_t K = 0; K < NumValueKinds; K++) {
654 VR->swapBytes(Endianness, getHostEndianness());
Xinliang David Liac5b8602015-11-25 04:29:24 +0000655 VR = getValueProfRecordNext(VR);
Xinliang David Liee415892015-11-10 00:24:45 +0000656 }
657}
658
659void ValueProfData::swapBytesFromHost(support::endianness Endianness) {
660 using namespace support;
661 if (Endianness == getHostEndianness())
662 return;
663
Xinliang David Lif47cf552015-11-25 06:23:38 +0000664 ValueProfRecord *VR = getFirstValueProfRecord(this);
Xinliang David Liee415892015-11-10 00:24:45 +0000665 for (uint32_t K = 0; K < NumValueKinds; K++) {
Xinliang David Liac5b8602015-11-25 04:29:24 +0000666 ValueProfRecord *NVR = getValueProfRecordNext(VR);
Xinliang David Liee415892015-11-10 00:24:45 +0000667 VR->swapBytes(getHostEndianness(), Endianness);
668 VR = NVR;
669 }
670 sys::swapByteOrder<uint32_t>(TotalSize);
671 sys::swapByteOrder<uint32_t>(NumValueKinds);
672}
Xinliang David Lie8092312015-11-25 19:13:00 +0000673
Xinliang David Li402477d2016-02-04 19:11:43 +0000674void annotateValueSite(Module &M, Instruction &Inst,
675 const InstrProfRecord &InstrProfR,
Rong Xu69683f12016-02-10 22:19:43 +0000676 InstrProfValueKind ValueKind, uint32_t SiteIdx,
677 uint32_t MaxMDCount) {
Xinliang David Li402477d2016-02-04 19:11:43 +0000678 uint32_t NV = InstrProfR.getNumValueDataForSite(ValueKind, SiteIdx);
Betul Buyukkurt4f1e8c92016-04-14 16:25:45 +0000679 if (!NV)
680 return;
Xinliang David Li402477d2016-02-04 19:11:43 +0000681
682 uint64_t Sum = 0;
683 std::unique_ptr<InstrProfValueData[]> VD =
684 InstrProfR.getValueForSite(ValueKind, SiteIdx, &Sum);
685
Rong Xu311ada12016-03-30 16:56:31 +0000686 ArrayRef<InstrProfValueData> VDs(VD.get(), NV);
687 annotateValueSite(M, Inst, VDs, Sum, ValueKind, MaxMDCount);
Rong Xubb494902016-02-12 21:36:17 +0000688}
689
690void annotateValueSite(Module &M, Instruction &Inst,
Rong Xu311ada12016-03-30 16:56:31 +0000691 ArrayRef<InstrProfValueData> VDs,
Rong Xubb494902016-02-12 21:36:17 +0000692 uint64_t Sum, InstrProfValueKind ValueKind,
693 uint32_t MaxMDCount) {
Xinliang David Li402477d2016-02-04 19:11:43 +0000694 LLVMContext &Ctx = M.getContext();
695 MDBuilder MDHelper(Ctx);
696 SmallVector<Metadata *, 3> Vals;
697 // Tag
698 Vals.push_back(MDHelper.createString("VP"));
699 // Value Kind
700 Vals.push_back(MDHelper.createConstant(
701 ConstantInt::get(Type::getInt32Ty(Ctx), ValueKind)));
702 // Total Count
703 Vals.push_back(
704 MDHelper.createConstant(ConstantInt::get(Type::getInt64Ty(Ctx), Sum)));
705
706 // Value Profile Data
Rong Xu69683f12016-02-10 22:19:43 +0000707 uint32_t MDCount = MaxMDCount;
Rong Xu311ada12016-03-30 16:56:31 +0000708 for (auto &VD : VDs) {
Xinliang David Li402477d2016-02-04 19:11:43 +0000709 Vals.push_back(MDHelper.createConstant(
Rong Xu311ada12016-03-30 16:56:31 +0000710 ConstantInt::get(Type::getInt64Ty(Ctx), VD.Value)));
Xinliang David Li402477d2016-02-04 19:11:43 +0000711 Vals.push_back(MDHelper.createConstant(
Rong Xu311ada12016-03-30 16:56:31 +0000712 ConstantInt::get(Type::getInt64Ty(Ctx), VD.Count)));
Xinliang David Li402477d2016-02-04 19:11:43 +0000713 if (--MDCount == 0)
714 break;
715 }
716 Inst.setMetadata(LLVMContext::MD_prof, MDNode::get(Ctx, Vals));
717}
718
719bool getValueProfDataFromInst(const Instruction &Inst,
720 InstrProfValueKind ValueKind,
721 uint32_t MaxNumValueData,
722 InstrProfValueData ValueData[],
723 uint32_t &ActualNumValueData, uint64_t &TotalC) {
724 MDNode *MD = Inst.getMetadata(LLVMContext::MD_prof);
725 if (!MD)
726 return false;
727
728 unsigned NOps = MD->getNumOperands();
729
730 if (NOps < 5)
731 return false;
732
733 // Operand 0 is a string tag "VP":
734 MDString *Tag = cast<MDString>(MD->getOperand(0));
735 if (!Tag)
736 return false;
737
738 if (!Tag->getString().equals("VP"))
739 return false;
740
741 // Now check kind:
742 ConstantInt *KindInt = mdconst::dyn_extract<ConstantInt>(MD->getOperand(1));
743 if (!KindInt)
744 return false;
745 if (KindInt->getZExtValue() != ValueKind)
746 return false;
747
748 // Get total count
749 ConstantInt *TotalCInt = mdconst::dyn_extract<ConstantInt>(MD->getOperand(2));
750 if (!TotalCInt)
751 return false;
752 TotalC = TotalCInt->getZExtValue();
753
754 ActualNumValueData = 0;
755
756 for (unsigned I = 3; I < NOps; I += 2) {
757 if (ActualNumValueData >= MaxNumValueData)
758 break;
759 ConstantInt *Value = mdconst::dyn_extract<ConstantInt>(MD->getOperand(I));
760 ConstantInt *Count =
761 mdconst::dyn_extract<ConstantInt>(MD->getOperand(I + 1));
762 if (!Value || !Count)
763 return false;
764 ValueData[ActualNumValueData].Value = Value->getZExtValue();
765 ValueData[ActualNumValueData].Count = Count->getZExtValue();
766 ActualNumValueData++;
767 }
768 return true;
769}
Rong Xu8e8fe852016-04-01 16:43:30 +0000770
Rong Xu92c2eae2016-04-01 20:15:04 +0000771MDNode *getPGOFuncNameMetadata(const Function &F) {
772 return F.getMetadata(getPGOFuncNameMetadataName());
773}
774
Benjamin Kramer0da23a22016-05-29 10:31:00 +0000775void createPGOFuncNameMetadata(Function &F, StringRef PGOFuncName) {
Rong Xuf8f051c2016-04-22 21:00:17 +0000776 // Only for internal linkage functions.
777 if (PGOFuncName == F.getName())
778 return;
779 // Don't create duplicated meta-data.
780 if (getPGOFuncNameMetadata(F))
Rong Xu8e8fe852016-04-01 16:43:30 +0000781 return;
Rong Xu8e8fe852016-04-01 16:43:30 +0000782 LLVMContext &C = F.getContext();
Benjamin Kramer0da23a22016-05-29 10:31:00 +0000783 MDNode *N = MDNode::get(C, MDString::get(C, PGOFuncName));
Rong Xu8e8fe852016-04-01 16:43:30 +0000784 F.setMetadata(getPGOFuncNameMetadataName(), N);
785}
786
Rong Xu97b68c52016-07-21 20:50:02 +0000787bool needsComdatForCounter(const Function &F, const Module &M) {
788 if (F.hasComdat())
789 return true;
790
791 Triple TT(M.getTargetTriple());
792 if (!TT.isOSBinFormatELF())
793 return false;
794
795 // See createPGOFuncNameVar for more details. To avoid link errors, profile
796 // counters for function with available_externally linkage needs to be changed
797 // to linkonce linkage. On ELF based systems, this leads to weak symbols to be
798 // created. Without using comdat, duplicate entries won't be removed by the
799 // linker leading to increased data segement size and raw profile size. Even
800 // worse, since the referenced counter from profile per-function data object
801 // will be resolved to the common strong definition, the profile counts for
802 // available_externally functions will end up being duplicated in raw profile
803 // data. This can result in distorted profile as the counts of those dups
804 // will be accumulated by the profile merger.
805 GlobalValue::LinkageTypes Linkage = F.getLinkage();
806 if (Linkage != GlobalValue::ExternalWeakLinkage &&
807 Linkage != GlobalValue::AvailableExternallyLinkage)
808 return false;
809
810 return true;
811}
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000812} // end namespace llvm