blob: a04007211665526486c6fe6d679e75d8d21cb2c3 [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.
139// To differentiate compiler generated internal symbols from original ones,
140// PGOFuncName meta data are created and attached to the original internal
141// symbols in the value profile annotation step
142// (PGOUseFunc::annotateIndirectCallSites). If a symbol does not have the meta
143// data, its original linkage must be non-internal.
144std::string getPGOFuncName(const Function &F, bool InLTO, uint64_t Version) {
Xinliang David Li9eb472b2016-07-12 17:14:51 +0000145 if (!InLTO) {
146 StringRef FileName = (StaticFuncFullModulePrefix
147 ? F.getParent()->getName()
148 : sys::path::filename(F.getParent()->getName()));
149 return getPGOFuncName(F.getName(), F.getLinkage(), FileName, Version);
150 }
Rong Xub5341662016-03-30 18:37:52 +0000151
Rong Xu8e8fe852016-04-01 16:43:30 +0000152 // In LTO mode (when InLTO is true), first check if there is a meta data.
153 if (MDNode *MD = getPGOFuncNameMetadata(F)) {
Rong Xub5341662016-03-30 18:37:52 +0000154 StringRef S = cast<MDString>(MD->getOperand(0))->getString();
155 return S.str();
156 }
157
158 // If there is no meta data, the function must be a global before the value
159 // profile annotation pass. Its current linkage may be internal if it is
160 // internalized in LTO mode.
Rong Xu8e8fe852016-04-01 16:43:30 +0000161 return getPGOFuncName(F.getName(), GlobalValue::ExternalLinkage, "");
Xinliang David Li441959d2015-11-09 00:01:22 +0000162}
163
Xinliang David Li4ec40142015-12-15 19:44:45 +0000164StringRef getFuncNameWithoutPrefix(StringRef PGOFuncName, StringRef FileName) {
165 if (FileName.empty())
Vedant Kumar43a85652016-03-28 15:49:08 +0000166 return PGOFuncName;
Xinliang David Li4ec40142015-12-15 19:44:45 +0000167 // Drop the file name including ':'. See also getPGOFuncName.
168 if (PGOFuncName.startswith(FileName))
169 PGOFuncName = PGOFuncName.drop_front(FileName.size() + 1);
170 return PGOFuncName;
171}
172
Xinliang David Lid1bab962015-12-12 17:28:03 +0000173// \p FuncName is the string used as profile lookup key for the function. A
174// symbol is created to hold the name. Return the legalized symbol name.
Vedant Kumaraa0cae62016-03-16 20:49:26 +0000175std::string getPGOFuncNameVarName(StringRef FuncName,
176 GlobalValue::LinkageTypes Linkage) {
Xinliang David Lid1bab962015-12-12 17:28:03 +0000177 std::string VarName = getInstrProfNameVarPrefix();
178 VarName += FuncName;
179
180 if (!GlobalValue::isLocalLinkage(Linkage))
181 return VarName;
182
183 // Now fix up illegal chars in local VarName that may upset the assembler.
Xinliang David Lieeaf0bc2016-06-10 06:32:26 +0000184 const char *InvalidChars = "-:<>/\"'";
Xinliang David Lid1bab962015-12-12 17:28:03 +0000185 size_t found = VarName.find_first_of(InvalidChars);
186 while (found != std::string::npos) {
187 VarName[found] = '_';
188 found = VarName.find_first_of(InvalidChars, found + 1);
189 }
190 return VarName;
191}
192
Xinliang David Li441959d2015-11-09 00:01:22 +0000193GlobalVariable *createPGOFuncNameVar(Module &M,
194 GlobalValue::LinkageTypes Linkage,
Xinliang David Li897d2922016-03-16 22:13:41 +0000195 StringRef PGOFuncName) {
Xinliang David Li441959d2015-11-09 00:01:22 +0000196
197 // We generally want to match the function's linkage, but available_externally
198 // and extern_weak both have the wrong semantics, and anything that doesn't
199 // need to link across compilation units doesn't need to be visible at all.
200 if (Linkage == GlobalValue::ExternalWeakLinkage)
201 Linkage = GlobalValue::LinkOnceAnyLinkage;
202 else if (Linkage == GlobalValue::AvailableExternallyLinkage)
203 Linkage = GlobalValue::LinkOnceODRLinkage;
204 else if (Linkage == GlobalValue::InternalLinkage ||
205 Linkage == GlobalValue::ExternalLinkage)
206 Linkage = GlobalValue::PrivateLinkage;
207
Xinliang David Li897d2922016-03-16 22:13:41 +0000208 auto *Value =
209 ConstantDataArray::getString(M.getContext(), PGOFuncName, false);
Xinliang David Li441959d2015-11-09 00:01:22 +0000210 auto FuncNameVar =
211 new GlobalVariable(M, Value->getType(), true, Linkage, Value,
Xinliang David Li897d2922016-03-16 22:13:41 +0000212 getPGOFuncNameVarName(PGOFuncName, Linkage));
Xinliang David Li441959d2015-11-09 00:01:22 +0000213
214 // Hide the symbol so that we correctly get a copy for each executable.
215 if (!GlobalValue::isLocalLinkage(FuncNameVar->getLinkage()))
216 FuncNameVar->setVisibility(GlobalValue::HiddenVisibility);
217
218 return FuncNameVar;
219}
220
Xinliang David Li897d2922016-03-16 22:13:41 +0000221GlobalVariable *createPGOFuncNameVar(Function &F, StringRef PGOFuncName) {
222 return createPGOFuncNameVar(*F.getParent(), F.getLinkage(), PGOFuncName);
Xinliang David Li441959d2015-11-09 00:01:22 +0000223}
Xinliang David Liee415892015-11-10 00:24:45 +0000224
Rong Xub5341662016-03-30 18:37:52 +0000225void InstrProfSymtab::create(Module &M, bool InLTO) {
226 for (Function &F : M) {
227 // Function may not have a name: like using asm("") to overwrite the name.
228 // Ignore in this case.
229 if (!F.hasName())
230 continue;
231 const std::string &PGOFuncName = getPGOFuncName(F, InLTO);
232 addFuncName(PGOFuncName);
Rong Xud5a57b52016-03-31 17:39:33 +0000233 MD5FuncMap.emplace_back(Function::getGUID(PGOFuncName), &F);
Rong Xub5341662016-03-30 18:37:52 +0000234 }
Xinliang David Li59411db2016-01-20 01:26:34 +0000235
236 finalizeSymtab();
237}
238
Vedant Kumar9152fd12016-05-19 03:54:45 +0000239Error collectPGOFuncNameStrings(const std::vector<std::string> &NameStrs,
240 bool doCompression, std::string &Result) {
Vedant Kumar86705ba2016-03-28 21:06:42 +0000241 assert(NameStrs.size() && "No name data to emit");
242
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000243 uint8_t Header[16], *P = Header;
Xinliang David Li0c677872016-01-04 21:31:09 +0000244 std::string UncompressedNameStrings =
Vedant Kumar86705ba2016-03-28 21:06:42 +0000245 join(NameStrs.begin(), NameStrs.end(), getInstrProfNameSeparator());
246
247 assert(StringRef(UncompressedNameStrings)
248 .count(getInstrProfNameSeparator()) == (NameStrs.size() - 1) &&
249 "PGO name is invalid (contains separator token)");
Xinliang David Li13ea29b2016-01-04 20:26:05 +0000250
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000251 unsigned EncLen = encodeULEB128(UncompressedNameStrings.length(), P);
252 P += EncLen;
Xinliang David Li120fe2e2016-01-04 22:01:02 +0000253
Benjamin Kramer0da23a22016-05-29 10:31:00 +0000254 auto WriteStringToResult = [&](size_t CompressedLen, StringRef InputStr) {
Xinliang David Li120fe2e2016-01-04 22:01:02 +0000255 EncLen = encodeULEB128(CompressedLen, P);
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000256 P += EncLen;
Xinliang David Li120fe2e2016-01-04 22:01:02 +0000257 char *HeaderStr = reinterpret_cast<char *>(&Header[0]);
258 unsigned HeaderLen = P - &Header[0];
259 Result.append(HeaderStr, HeaderLen);
260 Result += InputStr;
Vedant Kumar9152fd12016-05-19 03:54:45 +0000261 return Error::success();
Xinliang David Li120fe2e2016-01-04 22:01:02 +0000262 };
263
Vedant Kumar9152fd12016-05-19 03:54:45 +0000264 if (!doCompression) {
Xinliang David Li120fe2e2016-01-04 22:01:02 +0000265 return WriteStringToResult(0, UncompressedNameStrings);
Vedant Kumar9152fd12016-05-19 03:54:45 +0000266 }
Xinliang David Li120fe2e2016-01-04 22:01:02 +0000267
Benjamin Kramer0da23a22016-05-29 10:31:00 +0000268 SmallString<128> CompressedNameStrings;
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000269 zlib::Status Success =
270 zlib::compress(StringRef(UncompressedNameStrings), CompressedNameStrings,
271 zlib::BestSizeCompression);
Xinliang David Li120fe2e2016-01-04 22:01:02 +0000272
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000273 if (Success != zlib::StatusOK)
Vedant Kumar9152fd12016-05-19 03:54:45 +0000274 return make_error<InstrProfError>(instrprof_error::compress_failed);
Xinliang David Li120fe2e2016-01-04 22:01:02 +0000275
Benjamin Kramer0da23a22016-05-29 10:31:00 +0000276 return WriteStringToResult(CompressedNameStrings.size(),
277 CompressedNameStrings);
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000278}
279
Xinliang David Lieb7d7f82016-02-04 23:59:09 +0000280StringRef getPGOFuncNameVarInitializer(GlobalVariable *NameVar) {
Xinliang David Li37c1fa02016-01-03 04:38:13 +0000281 auto *Arr = cast<ConstantDataArray>(NameVar->getInitializer());
282 StringRef NameStr =
283 Arr->isCString() ? Arr->getAsCString() : Arr->getAsString();
284 return NameStr;
285}
286
Vedant Kumar9152fd12016-05-19 03:54:45 +0000287Error collectPGOFuncNameStrings(const std::vector<GlobalVariable *> &NameVars,
288 std::string &Result, bool doCompression) {
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000289 std::vector<std::string> NameStrs;
290 for (auto *NameVar : NameVars) {
Xinliang David Lieb7d7f82016-02-04 23:59:09 +0000291 NameStrs.push_back(getPGOFuncNameVarInitializer(NameVar));
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000292 }
Xinliang David Li73163752016-01-26 23:13:00 +0000293 return collectPGOFuncNameStrings(
294 NameStrs, zlib::isAvailable() && doCompression, Result);
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000295}
296
Vedant Kumar9152fd12016-05-19 03:54:45 +0000297Error readPGOFuncNameStrings(StringRef NameStrings, InstrProfSymtab &Symtab) {
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000298 const uint8_t *P = reinterpret_cast<const uint8_t *>(NameStrings.data());
299 const uint8_t *EndP = reinterpret_cast<const uint8_t *>(NameStrings.data() +
300 NameStrings.size());
301 while (P < EndP) {
302 uint32_t N;
303 uint64_t UncompressedSize = decodeULEB128(P, &N);
304 P += N;
305 uint64_t CompressedSize = decodeULEB128(P, &N);
306 P += N;
307 bool isCompressed = (CompressedSize != 0);
308 SmallString<128> UncompressedNameStrings;
309 StringRef NameStrings;
310 if (isCompressed) {
311 StringRef CompressedNameStrings(reinterpret_cast<const char *>(P),
312 CompressedSize);
313 if (zlib::uncompress(CompressedNameStrings, UncompressedNameStrings,
314 UncompressedSize) != zlib::StatusOK)
Vedant Kumar9152fd12016-05-19 03:54:45 +0000315 return make_error<InstrProfError>(instrprof_error::uncompress_failed);
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000316 P += CompressedSize;
317 NameStrings = StringRef(UncompressedNameStrings.data(),
318 UncompressedNameStrings.size());
319 } else {
320 NameStrings =
321 StringRef(reinterpret_cast<const char *>(P), UncompressedSize);
322 P += UncompressedSize;
323 }
324 // Now parse the name strings.
Xinliang David Li204efe22016-01-04 22:09:26 +0000325 SmallVector<StringRef, 0> Names;
Vedant Kumar86705ba2016-03-28 21:06:42 +0000326 NameStrings.split(Names, getInstrProfNameSeparator());
Xinliang David Li204efe22016-01-04 22:09:26 +0000327 for (StringRef &Name : Names)
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000328 Symtab.addFuncName(Name);
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000329
330 while (P < EndP && *P == 0)
331 P++;
332 }
333 Symtab.finalizeSymtab();
Vedant Kumar9152fd12016-05-19 03:54:45 +0000334 return Error::success();
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000335}
336
Vedant Kumar42369db2016-05-11 19:42:19 +0000337void InstrProfValueSiteRecord::merge(SoftInstrProfErrors &SIPE,
338 InstrProfValueSiteRecord &Input,
339 uint64_t Weight) {
Xinliang David Li5c24da52015-12-20 05:15:45 +0000340 this->sortByTargetValues();
341 Input.sortByTargetValues();
342 auto I = ValueData.begin();
343 auto IE = ValueData.end();
Xinliang David Li5c24da52015-12-20 05:15:45 +0000344 for (auto J = Input.ValueData.begin(), JE = Input.ValueData.end(); J != JE;
345 ++J) {
346 while (I != IE && I->Value < J->Value)
347 ++I;
348 if (I != IE && I->Value == J->Value) {
Xinliang David Li5c24da52015-12-20 05:15:45 +0000349 bool Overflowed;
Nathan Slingerland7bee3162016-01-12 22:34:00 +0000350 I->Count = SaturatingMultiplyAdd(J->Count, Weight, I->Count, &Overflowed);
Xinliang David Li5c24da52015-12-20 05:15:45 +0000351 if (Overflowed)
Vedant Kumar42369db2016-05-11 19:42:19 +0000352 SIPE.addError(instrprof_error::counter_overflow);
Xinliang David Li5c24da52015-12-20 05:15:45 +0000353 ++I;
354 continue;
355 }
356 ValueData.insert(I, *J);
357 }
Xinliang David Li5c24da52015-12-20 05:15:45 +0000358}
359
Vedant Kumar42369db2016-05-11 19:42:19 +0000360void InstrProfValueSiteRecord::scale(SoftInstrProfErrors &SIPE,
361 uint64_t Weight) {
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000362 for (auto I = ValueData.begin(), IE = ValueData.end(); I != IE; ++I) {
363 bool Overflowed;
364 I->Count = SaturatingMultiply(I->Count, Weight, &Overflowed);
365 if (Overflowed)
Vedant Kumar42369db2016-05-11 19:42:19 +0000366 SIPE.addError(instrprof_error::counter_overflow);
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000367 }
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000368}
369
Xinliang David Li020f22d2015-12-18 23:06:37 +0000370// Merge Value Profile data from Src record to this record for ValueKind.
371// Scale merged value counts by \p Weight.
Vedant Kumar42369db2016-05-11 19:42:19 +0000372void InstrProfRecord::mergeValueProfData(uint32_t ValueKind,
373 InstrProfRecord &Src,
374 uint64_t Weight) {
Xinliang David Li020f22d2015-12-18 23:06:37 +0000375 uint32_t ThisNumValueSites = getNumValueSites(ValueKind);
376 uint32_t OtherNumValueSites = Src.getNumValueSites(ValueKind);
Vedant Kumar42369db2016-05-11 19:42:19 +0000377 if (ThisNumValueSites != OtherNumValueSites) {
378 SIPE.addError(instrprof_error::value_site_count_mismatch);
379 return;
380 }
Xinliang David Li020f22d2015-12-18 23:06:37 +0000381 std::vector<InstrProfValueSiteRecord> &ThisSiteRecords =
382 getValueSitesForKind(ValueKind);
383 std::vector<InstrProfValueSiteRecord> &OtherSiteRecords =
384 Src.getValueSitesForKind(ValueKind);
Xinliang David Li020f22d2015-12-18 23:06:37 +0000385 for (uint32_t I = 0; I < ThisNumValueSites; I++)
Vedant Kumar42369db2016-05-11 19:42:19 +0000386 ThisSiteRecords[I].merge(SIPE, OtherSiteRecords[I], Weight);
Xinliang David Li020f22d2015-12-18 23:06:37 +0000387}
388
Vedant Kumar42369db2016-05-11 19:42:19 +0000389void InstrProfRecord::merge(InstrProfRecord &Other, uint64_t Weight) {
Xinliang David Li020f22d2015-12-18 23:06:37 +0000390 // If the number of counters doesn't match we either have bad data
391 // or a hash collision.
Vedant Kumar42369db2016-05-11 19:42:19 +0000392 if (Counts.size() != Other.Counts.size()) {
393 SIPE.addError(instrprof_error::count_mismatch);
394 return;
395 }
Xinliang David Li020f22d2015-12-18 23:06:37 +0000396
397 for (size_t I = 0, E = Other.Counts.size(); I < E; ++I) {
398 bool Overflowed;
Nathan Slingerland7bee3162016-01-12 22:34:00 +0000399 Counts[I] =
400 SaturatingMultiplyAdd(Other.Counts[I], Weight, Counts[I], &Overflowed);
Xinliang David Li020f22d2015-12-18 23:06:37 +0000401 if (Overflowed)
Vedant Kumar42369db2016-05-11 19:42:19 +0000402 SIPE.addError(instrprof_error::counter_overflow);
Xinliang David Li020f22d2015-12-18 23:06:37 +0000403 }
404
405 for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
Vedant Kumar42369db2016-05-11 19:42:19 +0000406 mergeValueProfData(Kind, Other, Weight);
Xinliang David Li020f22d2015-12-18 23:06:37 +0000407}
Xinliang David Lia716cc52015-12-20 06:22:13 +0000408
Vedant Kumar42369db2016-05-11 19:42:19 +0000409void InstrProfRecord::scaleValueProfData(uint32_t ValueKind, uint64_t Weight) {
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000410 uint32_t ThisNumValueSites = getNumValueSites(ValueKind);
411 std::vector<InstrProfValueSiteRecord> &ThisSiteRecords =
412 getValueSitesForKind(ValueKind);
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000413 for (uint32_t I = 0; I < ThisNumValueSites; I++)
Vedant Kumar42369db2016-05-11 19:42:19 +0000414 ThisSiteRecords[I].scale(SIPE, Weight);
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000415}
416
Vedant Kumar42369db2016-05-11 19:42:19 +0000417void InstrProfRecord::scale(uint64_t Weight) {
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000418 for (auto &Count : this->Counts) {
419 bool Overflowed;
420 Count = SaturatingMultiply(Count, Weight, &Overflowed);
Vedant Kumar42369db2016-05-11 19:42:19 +0000421 if (Overflowed)
422 SIPE.addError(instrprof_error::counter_overflow);
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000423 }
424 for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
Vedant Kumar42369db2016-05-11 19:42:19 +0000425 scaleValueProfData(Kind, Weight);
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000426}
427
Xinliang David Li020f22d2015-12-18 23:06:37 +0000428// Map indirect call target name hash to name string.
429uint64_t InstrProfRecord::remapValue(uint64_t Value, uint32_t ValueKind,
Xinliang David Lia716cc52015-12-20 06:22:13 +0000430 ValueMapType *ValueMap) {
431 if (!ValueMap)
Xinliang David Li020f22d2015-12-18 23:06:37 +0000432 return Value;
433 switch (ValueKind) {
434 case IPVK_IndirectCallTarget: {
435 auto Result =
Xinliang David Lia716cc52015-12-20 06:22:13 +0000436 std::lower_bound(ValueMap->begin(), ValueMap->end(), Value,
437 [](const std::pair<uint64_t, uint64_t> &LHS,
Xinliang David Li020f22d2015-12-18 23:06:37 +0000438 uint64_t RHS) { return LHS.first < RHS; });
Xinliang David Li8dd4ca82016-04-11 17:13:08 +0000439 // Raw function pointer collected by value profiler may be from
440 // external functions that are not instrumented. They won't have
441 // mapping data to be used by the deserializer. Force the value to
442 // be 0 in this case.
Xinliang David Li28464482016-04-10 03:32:02 +0000443 if (Result != ValueMap->end() && Result->first == Value)
Xinliang David Li020f22d2015-12-18 23:06:37 +0000444 Value = (uint64_t)Result->second;
Xinliang David Li28464482016-04-10 03:32:02 +0000445 else
446 Value = 0;
Xinliang David Li020f22d2015-12-18 23:06:37 +0000447 break;
448 }
449 }
450 return Value;
451}
452
Xinliang David Li020f22d2015-12-18 23:06:37 +0000453void InstrProfRecord::addValueData(uint32_t ValueKind, uint32_t Site,
454 InstrProfValueData *VData, uint32_t N,
Xinliang David Lia716cc52015-12-20 06:22:13 +0000455 ValueMapType *ValueMap) {
Xinliang David Li020f22d2015-12-18 23:06:37 +0000456 for (uint32_t I = 0; I < N; I++) {
Xinliang David Lia716cc52015-12-20 06:22:13 +0000457 VData[I].Value = remapValue(VData[I].Value, ValueKind, ValueMap);
Xinliang David Li020f22d2015-12-18 23:06:37 +0000458 }
459 std::vector<InstrProfValueSiteRecord> &ValueSites =
460 getValueSitesForKind(ValueKind);
461 if (N == 0)
Vedant Kumar6b22ba62016-05-11 16:03:02 +0000462 ValueSites.emplace_back();
Xinliang David Li020f22d2015-12-18 23:06:37 +0000463 else
464 ValueSites.emplace_back(VData, VData + N);
465}
466
Xinliang David Lib75544a2015-11-28 19:07:09 +0000467#define INSTR_PROF_COMMON_API_IMPL
468#include "llvm/ProfileData/InstrProfData.inc"
Xinliang David Liee415892015-11-10 00:24:45 +0000469
Xinliang David Li020f22d2015-12-18 23:06:37 +0000470/*!
Xinliang David Lib75544a2015-11-28 19:07:09 +0000471 * \brief ValueProfRecordClosure Interface implementation for InstrProfRecord
Xinliang David Lied966772015-11-25 23:31:18 +0000472 * class. These C wrappers are used as adaptors so that C++ code can be
473 * invoked as callbacks.
474 */
Xinliang David Lif47cf552015-11-25 06:23:38 +0000475uint32_t getNumValueKindsInstrProf(const void *Record) {
476 return reinterpret_cast<const InstrProfRecord *>(Record)->getNumValueKinds();
477}
478
479uint32_t getNumValueSitesInstrProf(const void *Record, uint32_t VKind) {
480 return reinterpret_cast<const InstrProfRecord *>(Record)
481 ->getNumValueSites(VKind);
482}
483
484uint32_t getNumValueDataInstrProf(const void *Record, uint32_t VKind) {
485 return reinterpret_cast<const InstrProfRecord *>(Record)
486 ->getNumValueData(VKind);
487}
488
489uint32_t getNumValueDataForSiteInstrProf(const void *R, uint32_t VK,
490 uint32_t S) {
491 return reinterpret_cast<const InstrProfRecord *>(R)
492 ->getNumValueDataForSite(VK, S);
493}
494
495void getValueForSiteInstrProf(const void *R, InstrProfValueData *Dst,
Xinliang David Li1e4c8092016-02-04 05:29:51 +0000496 uint32_t K, uint32_t S) {
497 reinterpret_cast<const InstrProfRecord *>(R)->getValueForSite(Dst, K, S);
Xinliang David Lie8092312015-11-25 19:13:00 +0000498}
499
Xinliang David Lif47cf552015-11-25 06:23:38 +0000500ValueProfData *allocValueProfDataInstrProf(size_t TotalSizeInBytes) {
Xinliang David Li38b9a322015-12-15 21:57:08 +0000501 ValueProfData *VD =
502 (ValueProfData *)(new (::operator new(TotalSizeInBytes)) ValueProfData());
503 memset(VD, 0, TotalSizeInBytes);
504 return VD;
Xinliang David Lif47cf552015-11-25 06:23:38 +0000505}
506
507static ValueProfRecordClosure InstrProfRecordClosure = {
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000508 nullptr,
Xinliang David Lif47cf552015-11-25 06:23:38 +0000509 getNumValueKindsInstrProf,
510 getNumValueSitesInstrProf,
511 getNumValueDataInstrProf,
512 getNumValueDataForSiteInstrProf,
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000513 nullptr,
Xinliang David Lif47cf552015-11-25 06:23:38 +0000514 getValueForSiteInstrProf,
Xinliang David Li38b9a322015-12-15 21:57:08 +0000515 allocValueProfDataInstrProf};
Xinliang David Lif47cf552015-11-25 06:23:38 +0000516
Xinliang David Lie8092312015-11-25 19:13:00 +0000517// Wrapper implementation using the closure mechanism.
Xinliang David Lif47cf552015-11-25 06:23:38 +0000518uint32_t ValueProfData::getSize(const InstrProfRecord &Record) {
519 InstrProfRecordClosure.Record = &Record;
520 return getValueProfDataSize(&InstrProfRecordClosure);
521}
522
Xinliang David Lie8092312015-11-25 19:13:00 +0000523// Wrapper implementation using the closure mechanism.
Xinliang David Lif47cf552015-11-25 06:23:38 +0000524std::unique_ptr<ValueProfData>
525ValueProfData::serializeFrom(const InstrProfRecord &Record) {
526 InstrProfRecordClosure.Record = &Record;
527
528 std::unique_ptr<ValueProfData> VPD(
Xinliang David Li0e6a36e2015-12-01 19:47:32 +0000529 serializeValueProfDataFrom(&InstrProfRecordClosure, nullptr));
Xinliang David Lif47cf552015-11-25 06:23:38 +0000530 return VPD;
531}
532
Xinliang David Lie8092312015-11-25 19:13:00 +0000533void ValueProfRecord::deserializeTo(InstrProfRecord &Record,
534 InstrProfRecord::ValueMapType *VMap) {
535 Record.reserveSites(Kind, NumValueSites);
536
537 InstrProfValueData *ValueData = getValueProfRecordValueData(this);
538 for (uint64_t VSite = 0; VSite < NumValueSites; ++VSite) {
539 uint8_t ValueDataCount = this->SiteCountArray[VSite];
540 Record.addValueData(Kind, VSite, ValueData, ValueDataCount, VMap);
541 ValueData += ValueDataCount;
542 }
543}
Xinliang David Lied966772015-11-25 23:31:18 +0000544
Xinliang David Lie8092312015-11-25 19:13:00 +0000545// For writing/serializing, Old is the host endianness, and New is
546// byte order intended on disk. For Reading/deserialization, Old
547// is the on-disk source endianness, and New is the host endianness.
548void ValueProfRecord::swapBytes(support::endianness Old,
549 support::endianness New) {
550 using namespace support;
551 if (Old == New)
552 return;
553
554 if (getHostEndianness() != Old) {
555 sys::swapByteOrder<uint32_t>(NumValueSites);
556 sys::swapByteOrder<uint32_t>(Kind);
557 }
558 uint32_t ND = getValueProfRecordNumValueData(this);
559 InstrProfValueData *VD = getValueProfRecordValueData(this);
560
561 // No need to swap byte array: SiteCountArrray.
562 for (uint32_t I = 0; I < ND; I++) {
563 sys::swapByteOrder<uint64_t>(VD[I].Value);
564 sys::swapByteOrder<uint64_t>(VD[I].Count);
565 }
566 if (getHostEndianness() == Old) {
567 sys::swapByteOrder<uint32_t>(NumValueSites);
568 sys::swapByteOrder<uint32_t>(Kind);
569 }
570}
571
572void ValueProfData::deserializeTo(InstrProfRecord &Record,
573 InstrProfRecord::ValueMapType *VMap) {
574 if (NumValueKinds == 0)
575 return;
576
577 ValueProfRecord *VR = getFirstValueProfRecord(this);
578 for (uint32_t K = 0; K < NumValueKinds; K++) {
579 VR->deserializeTo(Record, VMap);
580 VR = getValueProfRecordNext(VR);
581 }
582}
583
584template <class T>
585static T swapToHostOrder(const unsigned char *&D, support::endianness Orig) {
586 using namespace support;
587 if (Orig == little)
588 return endian::readNext<T, little, unaligned>(D);
589 else
590 return endian::readNext<T, big, unaligned>(D);
591}
592
593static std::unique_ptr<ValueProfData> allocValueProfData(uint32_t TotalSize) {
594 return std::unique_ptr<ValueProfData>(new (::operator new(TotalSize))
595 ValueProfData());
596}
597
Vedant Kumar9152fd12016-05-19 03:54:45 +0000598Error ValueProfData::checkIntegrity() {
Xinliang David Li8e32f4d2015-11-28 04:56:07 +0000599 if (NumValueKinds > IPVK_Last + 1)
Vedant Kumar9152fd12016-05-19 03:54:45 +0000600 return make_error<InstrProfError>(instrprof_error::malformed);
Xinliang David Li8e32f4d2015-11-28 04:56:07 +0000601 // Total size needs to be mulltiple of quadword size.
602 if (TotalSize % sizeof(uint64_t))
Vedant Kumar9152fd12016-05-19 03:54:45 +0000603 return make_error<InstrProfError>(instrprof_error::malformed);
Xinliang David Li8e32f4d2015-11-28 04:56:07 +0000604
605 ValueProfRecord *VR = getFirstValueProfRecord(this);
606 for (uint32_t K = 0; K < this->NumValueKinds; K++) {
607 if (VR->Kind > IPVK_Last)
Vedant Kumar9152fd12016-05-19 03:54:45 +0000608 return make_error<InstrProfError>(instrprof_error::malformed);
Xinliang David Li8e32f4d2015-11-28 04:56:07 +0000609 VR = getValueProfRecordNext(VR);
610 if ((char *)VR - (char *)this > (ptrdiff_t)TotalSize)
Vedant Kumar9152fd12016-05-19 03:54:45 +0000611 return make_error<InstrProfError>(instrprof_error::malformed);
Xinliang David Li8e32f4d2015-11-28 04:56:07 +0000612 }
Vedant Kumar9152fd12016-05-19 03:54:45 +0000613 return Error::success();
Xinliang David Li8e32f4d2015-11-28 04:56:07 +0000614}
615
Vedant Kumar9152fd12016-05-19 03:54:45 +0000616Expected<std::unique_ptr<ValueProfData>>
Xinliang David Liee415892015-11-10 00:24:45 +0000617ValueProfData::getValueProfData(const unsigned char *D,
618 const unsigned char *const BufferEnd,
619 support::endianness Endianness) {
620 using namespace support;
621 if (D + sizeof(ValueProfData) > BufferEnd)
Vedant Kumar9152fd12016-05-19 03:54:45 +0000622 return make_error<InstrProfError>(instrprof_error::truncated);
Xinliang David Liee415892015-11-10 00:24:45 +0000623
Xinliang David Lib8c3ad12015-11-17 03:47:21 +0000624 const unsigned char *Header = D;
625 uint32_t TotalSize = swapToHostOrder<uint32_t>(Header, Endianness);
Xinliang David Liee415892015-11-10 00:24:45 +0000626 if (D + TotalSize > BufferEnd)
Vedant Kumar9152fd12016-05-19 03:54:45 +0000627 return make_error<InstrProfError>(instrprof_error::too_large);
Xinliang David Liee415892015-11-10 00:24:45 +0000628
Xinliang David Lif47cf552015-11-25 06:23:38 +0000629 std::unique_ptr<ValueProfData> VPD = allocValueProfData(TotalSize);
Xinliang David Liee415892015-11-10 00:24:45 +0000630 memcpy(VPD.get(), D, TotalSize);
631 // Byte swap.
632 VPD->swapBytesToHost(Endianness);
633
Vedant Kumar9152fd12016-05-19 03:54:45 +0000634 Error E = VPD->checkIntegrity();
635 if (E)
636 return std::move(E);
Xinliang David Liee415892015-11-10 00:24:45 +0000637
Xinliang David Liee415892015-11-10 00:24:45 +0000638 return std::move(VPD);
639}
640
641void ValueProfData::swapBytesToHost(support::endianness Endianness) {
642 using namespace support;
643 if (Endianness == getHostEndianness())
644 return;
645
646 sys::swapByteOrder<uint32_t>(TotalSize);
647 sys::swapByteOrder<uint32_t>(NumValueKinds);
648
Xinliang David Lif47cf552015-11-25 06:23:38 +0000649 ValueProfRecord *VR = getFirstValueProfRecord(this);
Xinliang David Liee415892015-11-10 00:24:45 +0000650 for (uint32_t K = 0; K < NumValueKinds; K++) {
651 VR->swapBytes(Endianness, getHostEndianness());
Xinliang David Liac5b8602015-11-25 04:29:24 +0000652 VR = getValueProfRecordNext(VR);
Xinliang David Liee415892015-11-10 00:24:45 +0000653 }
654}
655
656void ValueProfData::swapBytesFromHost(support::endianness Endianness) {
657 using namespace support;
658 if (Endianness == getHostEndianness())
659 return;
660
Xinliang David Lif47cf552015-11-25 06:23:38 +0000661 ValueProfRecord *VR = getFirstValueProfRecord(this);
Xinliang David Liee415892015-11-10 00:24:45 +0000662 for (uint32_t K = 0; K < NumValueKinds; K++) {
Xinliang David Liac5b8602015-11-25 04:29:24 +0000663 ValueProfRecord *NVR = getValueProfRecordNext(VR);
Xinliang David Liee415892015-11-10 00:24:45 +0000664 VR->swapBytes(getHostEndianness(), Endianness);
665 VR = NVR;
666 }
667 sys::swapByteOrder<uint32_t>(TotalSize);
668 sys::swapByteOrder<uint32_t>(NumValueKinds);
669}
Xinliang David Lie8092312015-11-25 19:13:00 +0000670
Xinliang David Li402477d2016-02-04 19:11:43 +0000671void annotateValueSite(Module &M, Instruction &Inst,
672 const InstrProfRecord &InstrProfR,
Rong Xu69683f12016-02-10 22:19:43 +0000673 InstrProfValueKind ValueKind, uint32_t SiteIdx,
674 uint32_t MaxMDCount) {
Xinliang David Li402477d2016-02-04 19:11:43 +0000675 uint32_t NV = InstrProfR.getNumValueDataForSite(ValueKind, SiteIdx);
Betul Buyukkurt4f1e8c92016-04-14 16:25:45 +0000676 if (!NV)
677 return;
Xinliang David Li402477d2016-02-04 19:11:43 +0000678
679 uint64_t Sum = 0;
680 std::unique_ptr<InstrProfValueData[]> VD =
681 InstrProfR.getValueForSite(ValueKind, SiteIdx, &Sum);
682
Rong Xu311ada12016-03-30 16:56:31 +0000683 ArrayRef<InstrProfValueData> VDs(VD.get(), NV);
684 annotateValueSite(M, Inst, VDs, Sum, ValueKind, MaxMDCount);
Rong Xubb494902016-02-12 21:36:17 +0000685}
686
687void annotateValueSite(Module &M, Instruction &Inst,
Rong Xu311ada12016-03-30 16:56:31 +0000688 ArrayRef<InstrProfValueData> VDs,
Rong Xubb494902016-02-12 21:36:17 +0000689 uint64_t Sum, InstrProfValueKind ValueKind,
690 uint32_t MaxMDCount) {
Xinliang David Li402477d2016-02-04 19:11:43 +0000691 LLVMContext &Ctx = M.getContext();
692 MDBuilder MDHelper(Ctx);
693 SmallVector<Metadata *, 3> Vals;
694 // Tag
695 Vals.push_back(MDHelper.createString("VP"));
696 // Value Kind
697 Vals.push_back(MDHelper.createConstant(
698 ConstantInt::get(Type::getInt32Ty(Ctx), ValueKind)));
699 // Total Count
700 Vals.push_back(
701 MDHelper.createConstant(ConstantInt::get(Type::getInt64Ty(Ctx), Sum)));
702
703 // Value Profile Data
Rong Xu69683f12016-02-10 22:19:43 +0000704 uint32_t MDCount = MaxMDCount;
Rong Xu311ada12016-03-30 16:56:31 +0000705 for (auto &VD : VDs) {
Xinliang David Li402477d2016-02-04 19:11:43 +0000706 Vals.push_back(MDHelper.createConstant(
Rong Xu311ada12016-03-30 16:56:31 +0000707 ConstantInt::get(Type::getInt64Ty(Ctx), VD.Value)));
Xinliang David Li402477d2016-02-04 19:11:43 +0000708 Vals.push_back(MDHelper.createConstant(
Rong Xu311ada12016-03-30 16:56:31 +0000709 ConstantInt::get(Type::getInt64Ty(Ctx), VD.Count)));
Xinliang David Li402477d2016-02-04 19:11:43 +0000710 if (--MDCount == 0)
711 break;
712 }
713 Inst.setMetadata(LLVMContext::MD_prof, MDNode::get(Ctx, Vals));
714}
715
716bool getValueProfDataFromInst(const Instruction &Inst,
717 InstrProfValueKind ValueKind,
718 uint32_t MaxNumValueData,
719 InstrProfValueData ValueData[],
720 uint32_t &ActualNumValueData, uint64_t &TotalC) {
721 MDNode *MD = Inst.getMetadata(LLVMContext::MD_prof);
722 if (!MD)
723 return false;
724
725 unsigned NOps = MD->getNumOperands();
726
727 if (NOps < 5)
728 return false;
729
730 // Operand 0 is a string tag "VP":
731 MDString *Tag = cast<MDString>(MD->getOperand(0));
732 if (!Tag)
733 return false;
734
735 if (!Tag->getString().equals("VP"))
736 return false;
737
738 // Now check kind:
739 ConstantInt *KindInt = mdconst::dyn_extract<ConstantInt>(MD->getOperand(1));
740 if (!KindInt)
741 return false;
742 if (KindInt->getZExtValue() != ValueKind)
743 return false;
744
745 // Get total count
746 ConstantInt *TotalCInt = mdconst::dyn_extract<ConstantInt>(MD->getOperand(2));
747 if (!TotalCInt)
748 return false;
749 TotalC = TotalCInt->getZExtValue();
750
751 ActualNumValueData = 0;
752
753 for (unsigned I = 3; I < NOps; I += 2) {
754 if (ActualNumValueData >= MaxNumValueData)
755 break;
756 ConstantInt *Value = mdconst::dyn_extract<ConstantInt>(MD->getOperand(I));
757 ConstantInt *Count =
758 mdconst::dyn_extract<ConstantInt>(MD->getOperand(I + 1));
759 if (!Value || !Count)
760 return false;
761 ValueData[ActualNumValueData].Value = Value->getZExtValue();
762 ValueData[ActualNumValueData].Count = Count->getZExtValue();
763 ActualNumValueData++;
764 }
765 return true;
766}
Rong Xu8e8fe852016-04-01 16:43:30 +0000767
Rong Xu92c2eae2016-04-01 20:15:04 +0000768MDNode *getPGOFuncNameMetadata(const Function &F) {
769 return F.getMetadata(getPGOFuncNameMetadataName());
770}
771
Benjamin Kramer0da23a22016-05-29 10:31:00 +0000772void createPGOFuncNameMetadata(Function &F, StringRef PGOFuncName) {
Rong Xuf8f051c2016-04-22 21:00:17 +0000773 // Only for internal linkage functions.
774 if (PGOFuncName == F.getName())
775 return;
776 // Don't create duplicated meta-data.
777 if (getPGOFuncNameMetadata(F))
Rong Xu8e8fe852016-04-01 16:43:30 +0000778 return;
Rong Xu8e8fe852016-04-01 16:43:30 +0000779 LLVMContext &C = F.getContext();
Benjamin Kramer0da23a22016-05-29 10:31:00 +0000780 MDNode *N = MDNode::get(C, MDString::get(C, PGOFuncName));
Rong Xu8e8fe852016-04-01 16:43:30 +0000781 F.setMetadata(getPGOFuncNameMetadataName(), N);
782}
783
Rong Xu97b68c52016-07-21 20:50:02 +0000784bool needsComdatForCounter(const Function &F, const Module &M) {
785 if (F.hasComdat())
786 return true;
787
788 Triple TT(M.getTargetTriple());
789 if (!TT.isOSBinFormatELF())
790 return false;
791
792 // See createPGOFuncNameVar for more details. To avoid link errors, profile
793 // counters for function with available_externally linkage needs to be changed
794 // to linkonce linkage. On ELF based systems, this leads to weak symbols to be
795 // created. Without using comdat, duplicate entries won't be removed by the
796 // linker leading to increased data segement size and raw profile size. Even
797 // worse, since the referenced counter from profile per-function data object
798 // will be resolved to the common strong definition, the profile counts for
799 // available_externally functions will end up being duplicated in raw profile
800 // data. This can result in distorted profile as the counts of those dups
801 // will be accumulated by the profile merger.
802 GlobalValue::LinkageTypes Linkage = F.getLinkage();
803 if (Linkage != GlobalValue::ExternalWeakLinkage &&
804 Linkage != GlobalValue::AvailableExternallyLinkage)
805 return false;
806
807 return true;
808}
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000809} // end namespace llvm