blob: 045ba1a7b345d9985fc108fb0368eae40a090a9f [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"
Xinliang David Li441959d2015-11-09 00:01:22 +000017#include "llvm/IR/Constants.h"
18#include "llvm/IR/Function.h"
Xinliang David Li441959d2015-11-09 00:01:22 +000019#include "llvm/IR/GlobalVariable.h"
Xinliang David Li402477d2016-02-04 19:11:43 +000020#include "llvm/IR/MDBuilder.h"
Xinliang David Lie413f1a2015-12-31 07:57:16 +000021#include "llvm/IR/Module.h"
22#include "llvm/Support/Compression.h"
Justin Bognerf8d79192014-03-21 17:24:48 +000023#include "llvm/Support/ErrorHandling.h"
Xinliang David Lie413f1a2015-12-31 07:57:16 +000024#include "llvm/Support/LEB128.h"
Chris Bieneman1efe8012014-09-19 23:19:24 +000025#include "llvm/Support/ManagedStatic.h"
Justin Bognerf8d79192014-03-21 17:24:48 +000026
27using namespace llvm;
28
29namespace {
Rafael Espindola25188c92014-06-12 01:45:43 +000030class InstrProfErrorCategoryType : public std::error_category {
Rafael Espindolaf5d07fa2014-06-10 21:26:47 +000031 const char *name() const LLVM_NOEXCEPT override { return "llvm.instrprof"; }
Justin Bognerf8d79192014-03-21 17:24:48 +000032 std::string message(int IE) const override {
Rafael Espindola92512e82014-06-03 05:12:33 +000033 instrprof_error E = static_cast<instrprof_error>(IE);
Justin Bognerf8d79192014-03-21 17:24:48 +000034 switch (E) {
35 case instrprof_error::success:
36 return "Success";
37 case instrprof_error::eof:
38 return "End of File";
Nathan Slingerland4f823662015-11-13 03:47:58 +000039 case instrprof_error::unrecognized_format:
40 return "Unrecognized instrumentation profile encoding format";
Justin Bognerf8d79192014-03-21 17:24:48 +000041 case instrprof_error::bad_magic:
Nathan Slingerland4f823662015-11-13 03:47:58 +000042 return "Invalid instrumentation profile data (bad magic)";
Duncan P. N. Exon Smith531bb482014-03-21 20:42:28 +000043 case instrprof_error::bad_header:
Nathan Slingerland4f823662015-11-13 03:47:58 +000044 return "Invalid instrumentation profile data (file header is corrupt)";
Justin Bognerf8d79192014-03-21 17:24:48 +000045 case instrprof_error::unsupported_version:
Nathan Slingerland4f823662015-11-13 03:47:58 +000046 return "Unsupported instrumentation profile format version";
Justin Bognerb7aa2632014-04-18 21:48:40 +000047 case instrprof_error::unsupported_hash_type:
Nathan Slingerland4f823662015-11-13 03:47:58 +000048 return "Unsupported instrumentation profile hash type";
Justin Bognerf8d79192014-03-21 17:24:48 +000049 case instrprof_error::too_large:
50 return "Too much profile data";
51 case instrprof_error::truncated:
52 return "Truncated profile data";
53 case instrprof_error::malformed:
Nathan Slingerland4f823662015-11-13 03:47:58 +000054 return "Malformed instrumentation profile data";
Justin Bognerf8d79192014-03-21 17:24:48 +000055 case instrprof_error::unknown_function:
56 return "No profile data available for function";
Justin Bognerb9bd7f82014-03-21 17:46:22 +000057 case instrprof_error::hash_mismatch:
Nathan Slingerlande6e30d52015-11-17 22:08:53 +000058 return "Function control flow change detected (hash mismatch)";
Justin Bognerb9bd7f82014-03-21 17:46:22 +000059 case instrprof_error::count_mismatch:
Nathan Slingerlande6e30d52015-11-17 22:08:53 +000060 return "Function basic block count change detected (counter mismatch)";
Justin Bognerb9bd7f82014-03-21 17:46:22 +000061 case instrprof_error::counter_overflow:
62 return "Counter overflow";
Justin Bogner9e9a0572015-09-29 22:13:58 +000063 case instrprof_error::value_site_count_mismatch:
Nathan Slingerlande6e30d52015-11-17 22:08:53 +000064 return "Function value site count change detected (counter mismatch)";
Justin Bognerf8d79192014-03-21 17:24:48 +000065 }
66 llvm_unreachable("A value of instrprof_error has no message.");
67 }
Justin Bognerf8d79192014-03-21 17:24:48 +000068};
Eugene Zelenko6ac3f732016-01-26 18:48:36 +000069} // end anonymous namespace
Justin Bognerf8d79192014-03-21 17:24:48 +000070
Chris Bieneman1efe8012014-09-19 23:19:24 +000071static ManagedStatic<InstrProfErrorCategoryType> ErrorCategory;
72
Rafael Espindola25188c92014-06-12 01:45:43 +000073const std::error_category &llvm::instrprof_category() {
Chris Bieneman1efe8012014-09-19 23:19:24 +000074 return *ErrorCategory;
Justin Bognerf8d79192014-03-21 17:24:48 +000075}
Xinliang David Li441959d2015-11-09 00:01:22 +000076
77namespace llvm {
78
79std::string getPGOFuncName(StringRef RawFuncName,
80 GlobalValue::LinkageTypes Linkage,
Xinliang David Lia86545b2015-12-11 20:23:22 +000081 StringRef FileName,
82 uint64_t Version LLVM_ATTRIBUTE_UNUSED) {
Teresa Johnsonb43027d2016-03-15 02:13:19 +000083 return GlobalValue::getGlobalIdentifier(RawFuncName, Linkage, FileName);
Xinliang David Li441959d2015-11-09 00:01:22 +000084}
85
Xinliang David Li307902e2015-12-05 05:16:36 +000086std::string getPGOFuncName(const Function &F, uint64_t Version) {
87 return getPGOFuncName(F.getName(), F.getLinkage(), F.getParent()->getName(),
88 Version);
Xinliang David Li441959d2015-11-09 00:01:22 +000089}
90
Xinliang David Li4ec40142015-12-15 19:44:45 +000091StringRef getFuncNameWithoutPrefix(StringRef PGOFuncName, StringRef FileName) {
92 if (FileName.empty())
Vedant Kumar141ed942016-03-28 01:16:12 +000093 FileName = "<unknown>";
Xinliang David Li4ec40142015-12-15 19:44:45 +000094 // Drop the file name including ':'. See also getPGOFuncName.
95 if (PGOFuncName.startswith(FileName))
96 PGOFuncName = PGOFuncName.drop_front(FileName.size() + 1);
97 return PGOFuncName;
98}
99
Xinliang David Lid1bab962015-12-12 17:28:03 +0000100// \p FuncName is the string used as profile lookup key for the function. A
101// symbol is created to hold the name. Return the legalized symbol name.
Vedant Kumaraa0cae62016-03-16 20:49:26 +0000102std::string getPGOFuncNameVarName(StringRef FuncName,
103 GlobalValue::LinkageTypes Linkage) {
Xinliang David Lid1bab962015-12-12 17:28:03 +0000104 std::string VarName = getInstrProfNameVarPrefix();
105 VarName += FuncName;
106
107 if (!GlobalValue::isLocalLinkage(Linkage))
108 return VarName;
109
110 // Now fix up illegal chars in local VarName that may upset the assembler.
111 const char *InvalidChars = "-:<>\"'";
112 size_t found = VarName.find_first_of(InvalidChars);
113 while (found != std::string::npos) {
114 VarName[found] = '_';
115 found = VarName.find_first_of(InvalidChars, found + 1);
116 }
117 return VarName;
118}
119
Xinliang David Li441959d2015-11-09 00:01:22 +0000120GlobalVariable *createPGOFuncNameVar(Module &M,
121 GlobalValue::LinkageTypes Linkage,
Xinliang David Li897d2922016-03-16 22:13:41 +0000122 StringRef PGOFuncName) {
Xinliang David Li441959d2015-11-09 00:01:22 +0000123
124 // We generally want to match the function's linkage, but available_externally
125 // and extern_weak both have the wrong semantics, and anything that doesn't
126 // need to link across compilation units doesn't need to be visible at all.
127 if (Linkage == GlobalValue::ExternalWeakLinkage)
128 Linkage = GlobalValue::LinkOnceAnyLinkage;
129 else if (Linkage == GlobalValue::AvailableExternallyLinkage)
130 Linkage = GlobalValue::LinkOnceODRLinkage;
131 else if (Linkage == GlobalValue::InternalLinkage ||
132 Linkage == GlobalValue::ExternalLinkage)
133 Linkage = GlobalValue::PrivateLinkage;
134
Xinliang David Li897d2922016-03-16 22:13:41 +0000135 auto *Value =
136 ConstantDataArray::getString(M.getContext(), PGOFuncName, false);
Xinliang David Li441959d2015-11-09 00:01:22 +0000137 auto FuncNameVar =
138 new GlobalVariable(M, Value->getType(), true, Linkage, Value,
Xinliang David Li897d2922016-03-16 22:13:41 +0000139 getPGOFuncNameVarName(PGOFuncName, Linkage));
Xinliang David Li441959d2015-11-09 00:01:22 +0000140
141 // Hide the symbol so that we correctly get a copy for each executable.
142 if (!GlobalValue::isLocalLinkage(FuncNameVar->getLinkage()))
143 FuncNameVar->setVisibility(GlobalValue::HiddenVisibility);
144
145 return FuncNameVar;
146}
147
Xinliang David Li897d2922016-03-16 22:13:41 +0000148GlobalVariable *createPGOFuncNameVar(Function &F, StringRef PGOFuncName) {
149 return createPGOFuncNameVar(*F.getParent(), F.getLinkage(), PGOFuncName);
Xinliang David Li441959d2015-11-09 00:01:22 +0000150}
Xinliang David Liee415892015-11-10 00:24:45 +0000151
Xinliang David Li59411db2016-01-20 01:26:34 +0000152void InstrProfSymtab::create(const Module &M) {
153 for (const Function &F : M)
154 addFuncName(getPGOFuncName(F));
155
156 finalizeSymtab();
157}
158
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000159int collectPGOFuncNameStrings(const std::vector<std::string> &NameStrs,
160 bool doCompression, std::string &Result) {
161 uint8_t Header[16], *P = Header;
Xinliang David Li0c677872016-01-04 21:31:09 +0000162 std::string UncompressedNameStrings =
163 join(NameStrs.begin(), NameStrs.end(), StringRef(" "));
Xinliang David Li13ea29b2016-01-04 20:26:05 +0000164
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000165 unsigned EncLen = encodeULEB128(UncompressedNameStrings.length(), P);
166 P += EncLen;
Xinliang David Li120fe2e2016-01-04 22:01:02 +0000167
168 auto WriteStringToResult = [&](size_t CompressedLen,
169 const std::string &InputStr) {
170 EncLen = encodeULEB128(CompressedLen, P);
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000171 P += EncLen;
Xinliang David Li120fe2e2016-01-04 22:01:02 +0000172 char *HeaderStr = reinterpret_cast<char *>(&Header[0]);
173 unsigned HeaderLen = P - &Header[0];
174 Result.append(HeaderStr, HeaderLen);
175 Result += InputStr;
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000176 return 0;
Xinliang David Li120fe2e2016-01-04 22:01:02 +0000177 };
178
179 if (!doCompression)
180 return WriteStringToResult(0, UncompressedNameStrings);
181
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000182 SmallVector<char, 128> CompressedNameStrings;
183 zlib::Status Success =
184 zlib::compress(StringRef(UncompressedNameStrings), CompressedNameStrings,
185 zlib::BestSizeCompression);
Xinliang David Li120fe2e2016-01-04 22:01:02 +0000186
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000187 if (Success != zlib::StatusOK)
188 return 1;
Xinliang David Li120fe2e2016-01-04 22:01:02 +0000189
190 return WriteStringToResult(
191 CompressedNameStrings.size(),
192 std::string(CompressedNameStrings.data(), CompressedNameStrings.size()));
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000193}
194
Xinliang David Lieb7d7f82016-02-04 23:59:09 +0000195StringRef getPGOFuncNameVarInitializer(GlobalVariable *NameVar) {
Xinliang David Li37c1fa02016-01-03 04:38:13 +0000196 auto *Arr = cast<ConstantDataArray>(NameVar->getInitializer());
197 StringRef NameStr =
198 Arr->isCString() ? Arr->getAsCString() : Arr->getAsString();
199 return NameStr;
200}
201
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000202int collectPGOFuncNameStrings(const std::vector<GlobalVariable *> &NameVars,
Xinliang David Li73163752016-01-26 23:13:00 +0000203 std::string &Result, bool doCompression) {
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000204 std::vector<std::string> NameStrs;
205 for (auto *NameVar : NameVars) {
Xinliang David Lieb7d7f82016-02-04 23:59:09 +0000206 NameStrs.push_back(getPGOFuncNameVarInitializer(NameVar));
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000207 }
Xinliang David Li73163752016-01-26 23:13:00 +0000208 return collectPGOFuncNameStrings(
209 NameStrs, zlib::isAvailable() && doCompression, Result);
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000210}
211
212int readPGOFuncNameStrings(StringRef NameStrings, InstrProfSymtab &Symtab) {
213 const uint8_t *P = reinterpret_cast<const uint8_t *>(NameStrings.data());
214 const uint8_t *EndP = reinterpret_cast<const uint8_t *>(NameStrings.data() +
215 NameStrings.size());
216 while (P < EndP) {
217 uint32_t N;
218 uint64_t UncompressedSize = decodeULEB128(P, &N);
219 P += N;
220 uint64_t CompressedSize = decodeULEB128(P, &N);
221 P += N;
222 bool isCompressed = (CompressedSize != 0);
223 SmallString<128> UncompressedNameStrings;
224 StringRef NameStrings;
225 if (isCompressed) {
226 StringRef CompressedNameStrings(reinterpret_cast<const char *>(P),
227 CompressedSize);
228 if (zlib::uncompress(CompressedNameStrings, UncompressedNameStrings,
229 UncompressedSize) != zlib::StatusOK)
230 return 1;
231 P += CompressedSize;
232 NameStrings = StringRef(UncompressedNameStrings.data(),
233 UncompressedNameStrings.size());
234 } else {
235 NameStrings =
236 StringRef(reinterpret_cast<const char *>(P), UncompressedSize);
237 P += UncompressedSize;
238 }
239 // Now parse the name strings.
Xinliang David Li204efe22016-01-04 22:09:26 +0000240 SmallVector<StringRef, 0> Names;
241 NameStrings.split(Names, ' ');
242 for (StringRef &Name : Names)
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000243 Symtab.addFuncName(Name);
Xinliang David Lie413f1a2015-12-31 07:57:16 +0000244
245 while (P < EndP && *P == 0)
246 P++;
247 }
248 Symtab.finalizeSymtab();
249 return 0;
250}
251
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000252instrprof_error InstrProfValueSiteRecord::merge(InstrProfValueSiteRecord &Input,
253 uint64_t Weight) {
Xinliang David Li5c24da52015-12-20 05:15:45 +0000254 this->sortByTargetValues();
255 Input.sortByTargetValues();
256 auto I = ValueData.begin();
257 auto IE = ValueData.end();
258 instrprof_error Result = instrprof_error::success;
259 for (auto J = Input.ValueData.begin(), JE = Input.ValueData.end(); J != JE;
260 ++J) {
261 while (I != IE && I->Value < J->Value)
262 ++I;
263 if (I != IE && I->Value == J->Value) {
Xinliang David Li5c24da52015-12-20 05:15:45 +0000264 bool Overflowed;
Nathan Slingerland7bee3162016-01-12 22:34:00 +0000265 I->Count = SaturatingMultiplyAdd(J->Count, Weight, I->Count, &Overflowed);
Xinliang David Li5c24da52015-12-20 05:15:45 +0000266 if (Overflowed)
267 Result = instrprof_error::counter_overflow;
268 ++I;
269 continue;
270 }
271 ValueData.insert(I, *J);
272 }
273 return Result;
274}
275
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000276instrprof_error InstrProfValueSiteRecord::scale(uint64_t Weight) {
277 instrprof_error Result = instrprof_error::success;
278 for (auto I = ValueData.begin(), IE = ValueData.end(); I != IE; ++I) {
279 bool Overflowed;
280 I->Count = SaturatingMultiply(I->Count, Weight, &Overflowed);
281 if (Overflowed)
282 Result = instrprof_error::counter_overflow;
283 }
284 return Result;
285}
286
Xinliang David Li020f22d2015-12-18 23:06:37 +0000287// Merge Value Profile data from Src record to this record for ValueKind.
288// Scale merged value counts by \p Weight.
289instrprof_error InstrProfRecord::mergeValueProfData(uint32_t ValueKind,
290 InstrProfRecord &Src,
291 uint64_t Weight) {
292 uint32_t ThisNumValueSites = getNumValueSites(ValueKind);
293 uint32_t OtherNumValueSites = Src.getNumValueSites(ValueKind);
294 if (ThisNumValueSites != OtherNumValueSites)
295 return instrprof_error::value_site_count_mismatch;
296 std::vector<InstrProfValueSiteRecord> &ThisSiteRecords =
297 getValueSitesForKind(ValueKind);
298 std::vector<InstrProfValueSiteRecord> &OtherSiteRecords =
299 Src.getValueSitesForKind(ValueKind);
300 instrprof_error Result = instrprof_error::success;
301 for (uint32_t I = 0; I < ThisNumValueSites; I++)
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000302 MergeResult(Result, ThisSiteRecords[I].merge(OtherSiteRecords[I], Weight));
Xinliang David Li020f22d2015-12-18 23:06:37 +0000303 return Result;
304}
305
306instrprof_error InstrProfRecord::merge(InstrProfRecord &Other,
307 uint64_t Weight) {
308 // If the number of counters doesn't match we either have bad data
309 // or a hash collision.
310 if (Counts.size() != Other.Counts.size())
311 return instrprof_error::count_mismatch;
312
313 instrprof_error Result = instrprof_error::success;
314
315 for (size_t I = 0, E = Other.Counts.size(); I < E; ++I) {
316 bool Overflowed;
Nathan Slingerland7bee3162016-01-12 22:34:00 +0000317 Counts[I] =
318 SaturatingMultiplyAdd(Other.Counts[I], Weight, Counts[I], &Overflowed);
Xinliang David Li020f22d2015-12-18 23:06:37 +0000319 if (Overflowed)
320 Result = instrprof_error::counter_overflow;
321 }
322
323 for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
324 MergeResult(Result, mergeValueProfData(Kind, Other, Weight));
325
326 return Result;
327}
Xinliang David Lia716cc52015-12-20 06:22:13 +0000328
Xinliang David Li51dc04c2016-01-08 03:49:59 +0000329instrprof_error InstrProfRecord::scaleValueProfData(uint32_t ValueKind,
330 uint64_t Weight) {
331 uint32_t ThisNumValueSites = getNumValueSites(ValueKind);
332 std::vector<InstrProfValueSiteRecord> &ThisSiteRecords =
333 getValueSitesForKind(ValueKind);
334 instrprof_error Result = instrprof_error::success;
335 for (uint32_t I = 0; I < ThisNumValueSites; I++)
336 MergeResult(Result, ThisSiteRecords[I].scale(Weight));
337 return Result;
338}
339
340instrprof_error InstrProfRecord::scale(uint64_t Weight) {
341 instrprof_error Result = instrprof_error::success;
342 for (auto &Count : this->Counts) {
343 bool Overflowed;
344 Count = SaturatingMultiply(Count, Weight, &Overflowed);
345 if (Overflowed && Result == instrprof_error::success) {
346 Result = instrprof_error::counter_overflow;
347 }
348 }
349 for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
350 MergeResult(Result, scaleValueProfData(Kind, Weight));
351
352 return Result;
353}
354
Xinliang David Li020f22d2015-12-18 23:06:37 +0000355// Map indirect call target name hash to name string.
356uint64_t InstrProfRecord::remapValue(uint64_t Value, uint32_t ValueKind,
Xinliang David Lia716cc52015-12-20 06:22:13 +0000357 ValueMapType *ValueMap) {
358 if (!ValueMap)
Xinliang David Li020f22d2015-12-18 23:06:37 +0000359 return Value;
360 switch (ValueKind) {
361 case IPVK_IndirectCallTarget: {
362 auto Result =
Xinliang David Lia716cc52015-12-20 06:22:13 +0000363 std::lower_bound(ValueMap->begin(), ValueMap->end(), Value,
364 [](const std::pair<uint64_t, uint64_t> &LHS,
Xinliang David Li020f22d2015-12-18 23:06:37 +0000365 uint64_t RHS) { return LHS.first < RHS; });
Xinliang David Lia716cc52015-12-20 06:22:13 +0000366 if (Result != ValueMap->end())
Xinliang David Li020f22d2015-12-18 23:06:37 +0000367 Value = (uint64_t)Result->second;
368 break;
369 }
370 }
371 return Value;
372}
373
Xinliang David Li020f22d2015-12-18 23:06:37 +0000374void InstrProfRecord::addValueData(uint32_t ValueKind, uint32_t Site,
375 InstrProfValueData *VData, uint32_t N,
Xinliang David Lia716cc52015-12-20 06:22:13 +0000376 ValueMapType *ValueMap) {
Xinliang David Li020f22d2015-12-18 23:06:37 +0000377 for (uint32_t I = 0; I < N; I++) {
Xinliang David Lia716cc52015-12-20 06:22:13 +0000378 VData[I].Value = remapValue(VData[I].Value, ValueKind, ValueMap);
Xinliang David Li020f22d2015-12-18 23:06:37 +0000379 }
380 std::vector<InstrProfValueSiteRecord> &ValueSites =
381 getValueSitesForKind(ValueKind);
382 if (N == 0)
383 ValueSites.push_back(InstrProfValueSiteRecord());
384 else
385 ValueSites.emplace_back(VData, VData + N);
386}
387
Xinliang David Lib75544a2015-11-28 19:07:09 +0000388#define INSTR_PROF_COMMON_API_IMPL
389#include "llvm/ProfileData/InstrProfData.inc"
Xinliang David Liee415892015-11-10 00:24:45 +0000390
Xinliang David Li020f22d2015-12-18 23:06:37 +0000391/*!
Xinliang David Lib75544a2015-11-28 19:07:09 +0000392 * \brief ValueProfRecordClosure Interface implementation for InstrProfRecord
Xinliang David Lied966772015-11-25 23:31:18 +0000393 * class. These C wrappers are used as adaptors so that C++ code can be
394 * invoked as callbacks.
395 */
Xinliang David Lif47cf552015-11-25 06:23:38 +0000396uint32_t getNumValueKindsInstrProf(const void *Record) {
397 return reinterpret_cast<const InstrProfRecord *>(Record)->getNumValueKinds();
398}
399
400uint32_t getNumValueSitesInstrProf(const void *Record, uint32_t VKind) {
401 return reinterpret_cast<const InstrProfRecord *>(Record)
402 ->getNumValueSites(VKind);
403}
404
405uint32_t getNumValueDataInstrProf(const void *Record, uint32_t VKind) {
406 return reinterpret_cast<const InstrProfRecord *>(Record)
407 ->getNumValueData(VKind);
408}
409
410uint32_t getNumValueDataForSiteInstrProf(const void *R, uint32_t VK,
411 uint32_t S) {
412 return reinterpret_cast<const InstrProfRecord *>(R)
413 ->getNumValueDataForSite(VK, S);
414}
415
416void getValueForSiteInstrProf(const void *R, InstrProfValueData *Dst,
Xinliang David Li1e4c8092016-02-04 05:29:51 +0000417 uint32_t K, uint32_t S) {
418 reinterpret_cast<const InstrProfRecord *>(R)->getValueForSite(Dst, K, S);
419 return;
Xinliang David Lie8092312015-11-25 19:13:00 +0000420}
421
Xinliang David Lif47cf552015-11-25 06:23:38 +0000422ValueProfData *allocValueProfDataInstrProf(size_t TotalSizeInBytes) {
Xinliang David Li38b9a322015-12-15 21:57:08 +0000423 ValueProfData *VD =
424 (ValueProfData *)(new (::operator new(TotalSizeInBytes)) ValueProfData());
425 memset(VD, 0, TotalSizeInBytes);
426 return VD;
Xinliang David Lif47cf552015-11-25 06:23:38 +0000427}
428
429static ValueProfRecordClosure InstrProfRecordClosure = {
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000430 nullptr,
Xinliang David Lif47cf552015-11-25 06:23:38 +0000431 getNumValueKindsInstrProf,
432 getNumValueSitesInstrProf,
433 getNumValueDataInstrProf,
434 getNumValueDataForSiteInstrProf,
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000435 nullptr,
Xinliang David Lif47cf552015-11-25 06:23:38 +0000436 getValueForSiteInstrProf,
Xinliang David Li38b9a322015-12-15 21:57:08 +0000437 allocValueProfDataInstrProf};
Xinliang David Lif47cf552015-11-25 06:23:38 +0000438
Xinliang David Lie8092312015-11-25 19:13:00 +0000439// Wrapper implementation using the closure mechanism.
Xinliang David Lif47cf552015-11-25 06:23:38 +0000440uint32_t ValueProfData::getSize(const InstrProfRecord &Record) {
441 InstrProfRecordClosure.Record = &Record;
442 return getValueProfDataSize(&InstrProfRecordClosure);
443}
444
Xinliang David Lie8092312015-11-25 19:13:00 +0000445// Wrapper implementation using the closure mechanism.
Xinliang David Lif47cf552015-11-25 06:23:38 +0000446std::unique_ptr<ValueProfData>
447ValueProfData::serializeFrom(const InstrProfRecord &Record) {
448 InstrProfRecordClosure.Record = &Record;
449
450 std::unique_ptr<ValueProfData> VPD(
Xinliang David Li0e6a36e2015-12-01 19:47:32 +0000451 serializeValueProfDataFrom(&InstrProfRecordClosure, nullptr));
Xinliang David Lif47cf552015-11-25 06:23:38 +0000452 return VPD;
453}
454
Xinliang David Lie8092312015-11-25 19:13:00 +0000455void ValueProfRecord::deserializeTo(InstrProfRecord &Record,
456 InstrProfRecord::ValueMapType *VMap) {
457 Record.reserveSites(Kind, NumValueSites);
458
459 InstrProfValueData *ValueData = getValueProfRecordValueData(this);
460 for (uint64_t VSite = 0; VSite < NumValueSites; ++VSite) {
461 uint8_t ValueDataCount = this->SiteCountArray[VSite];
462 Record.addValueData(Kind, VSite, ValueData, ValueDataCount, VMap);
463 ValueData += ValueDataCount;
464 }
465}
Xinliang David Lied966772015-11-25 23:31:18 +0000466
Xinliang David Lie8092312015-11-25 19:13:00 +0000467// For writing/serializing, Old is the host endianness, and New is
468// byte order intended on disk. For Reading/deserialization, Old
469// is the on-disk source endianness, and New is the host endianness.
470void ValueProfRecord::swapBytes(support::endianness Old,
471 support::endianness New) {
472 using namespace support;
473 if (Old == New)
474 return;
475
476 if (getHostEndianness() != Old) {
477 sys::swapByteOrder<uint32_t>(NumValueSites);
478 sys::swapByteOrder<uint32_t>(Kind);
479 }
480 uint32_t ND = getValueProfRecordNumValueData(this);
481 InstrProfValueData *VD = getValueProfRecordValueData(this);
482
483 // No need to swap byte array: SiteCountArrray.
484 for (uint32_t I = 0; I < ND; I++) {
485 sys::swapByteOrder<uint64_t>(VD[I].Value);
486 sys::swapByteOrder<uint64_t>(VD[I].Count);
487 }
488 if (getHostEndianness() == Old) {
489 sys::swapByteOrder<uint32_t>(NumValueSites);
490 sys::swapByteOrder<uint32_t>(Kind);
491 }
492}
493
494void ValueProfData::deserializeTo(InstrProfRecord &Record,
495 InstrProfRecord::ValueMapType *VMap) {
496 if (NumValueKinds == 0)
497 return;
498
499 ValueProfRecord *VR = getFirstValueProfRecord(this);
500 for (uint32_t K = 0; K < NumValueKinds; K++) {
501 VR->deserializeTo(Record, VMap);
502 VR = getValueProfRecordNext(VR);
503 }
504}
505
506template <class T>
507static T swapToHostOrder(const unsigned char *&D, support::endianness Orig) {
508 using namespace support;
509 if (Orig == little)
510 return endian::readNext<T, little, unaligned>(D);
511 else
512 return endian::readNext<T, big, unaligned>(D);
513}
514
515static std::unique_ptr<ValueProfData> allocValueProfData(uint32_t TotalSize) {
516 return std::unique_ptr<ValueProfData>(new (::operator new(TotalSize))
517 ValueProfData());
518}
519
Xinliang David Li8e32f4d2015-11-28 04:56:07 +0000520instrprof_error ValueProfData::checkIntegrity() {
521 if (NumValueKinds > IPVK_Last + 1)
522 return instrprof_error::malformed;
523 // Total size needs to be mulltiple of quadword size.
524 if (TotalSize % sizeof(uint64_t))
525 return instrprof_error::malformed;
526
527 ValueProfRecord *VR = getFirstValueProfRecord(this);
528 for (uint32_t K = 0; K < this->NumValueKinds; K++) {
529 if (VR->Kind > IPVK_Last)
530 return instrprof_error::malformed;
531 VR = getValueProfRecordNext(VR);
532 if ((char *)VR - (char *)this > (ptrdiff_t)TotalSize)
533 return instrprof_error::malformed;
534 }
535 return instrprof_error::success;
536}
537
Xinliang David Liee415892015-11-10 00:24:45 +0000538ErrorOr<std::unique_ptr<ValueProfData>>
539ValueProfData::getValueProfData(const unsigned char *D,
540 const unsigned char *const BufferEnd,
541 support::endianness Endianness) {
542 using namespace support;
543 if (D + sizeof(ValueProfData) > BufferEnd)
544 return instrprof_error::truncated;
545
Xinliang David Lib8c3ad12015-11-17 03:47:21 +0000546 const unsigned char *Header = D;
547 uint32_t TotalSize = swapToHostOrder<uint32_t>(Header, Endianness);
Xinliang David Liee415892015-11-10 00:24:45 +0000548 if (D + TotalSize > BufferEnd)
549 return instrprof_error::too_large;
Xinliang David Liee415892015-11-10 00:24:45 +0000550
Xinliang David Lif47cf552015-11-25 06:23:38 +0000551 std::unique_ptr<ValueProfData> VPD = allocValueProfData(TotalSize);
Xinliang David Liee415892015-11-10 00:24:45 +0000552 memcpy(VPD.get(), D, TotalSize);
553 // Byte swap.
554 VPD->swapBytesToHost(Endianness);
555
Xinliang David Li8e32f4d2015-11-28 04:56:07 +0000556 instrprof_error EC = VPD->checkIntegrity();
557 if (EC != instrprof_error::success)
558 return EC;
Xinliang David Liee415892015-11-10 00:24:45 +0000559
Xinliang David Liee415892015-11-10 00:24:45 +0000560 return std::move(VPD);
561}
562
563void ValueProfData::swapBytesToHost(support::endianness Endianness) {
564 using namespace support;
565 if (Endianness == getHostEndianness())
566 return;
567
568 sys::swapByteOrder<uint32_t>(TotalSize);
569 sys::swapByteOrder<uint32_t>(NumValueKinds);
570
Xinliang David Lif47cf552015-11-25 06:23:38 +0000571 ValueProfRecord *VR = getFirstValueProfRecord(this);
Xinliang David Liee415892015-11-10 00:24:45 +0000572 for (uint32_t K = 0; K < NumValueKinds; K++) {
573 VR->swapBytes(Endianness, getHostEndianness());
Xinliang David Liac5b8602015-11-25 04:29:24 +0000574 VR = getValueProfRecordNext(VR);
Xinliang David Liee415892015-11-10 00:24:45 +0000575 }
576}
577
578void ValueProfData::swapBytesFromHost(support::endianness Endianness) {
579 using namespace support;
580 if (Endianness == getHostEndianness())
581 return;
582
Xinliang David Lif47cf552015-11-25 06:23:38 +0000583 ValueProfRecord *VR = getFirstValueProfRecord(this);
Xinliang David Liee415892015-11-10 00:24:45 +0000584 for (uint32_t K = 0; K < NumValueKinds; K++) {
Xinliang David Liac5b8602015-11-25 04:29:24 +0000585 ValueProfRecord *NVR = getValueProfRecordNext(VR);
Xinliang David Liee415892015-11-10 00:24:45 +0000586 VR->swapBytes(getHostEndianness(), Endianness);
587 VR = NVR;
588 }
589 sys::swapByteOrder<uint32_t>(TotalSize);
590 sys::swapByteOrder<uint32_t>(NumValueKinds);
591}
Xinliang David Lie8092312015-11-25 19:13:00 +0000592
Xinliang David Li402477d2016-02-04 19:11:43 +0000593void annotateValueSite(Module &M, Instruction &Inst,
594 const InstrProfRecord &InstrProfR,
Rong Xu69683f12016-02-10 22:19:43 +0000595 InstrProfValueKind ValueKind, uint32_t SiteIdx,
596 uint32_t MaxMDCount) {
Xinliang David Li402477d2016-02-04 19:11:43 +0000597 uint32_t NV = InstrProfR.getNumValueDataForSite(ValueKind, SiteIdx);
598
599 uint64_t Sum = 0;
600 std::unique_ptr<InstrProfValueData[]> VD =
601 InstrProfR.getValueForSite(ValueKind, SiteIdx, &Sum);
602
Rong Xubb494902016-02-12 21:36:17 +0000603 annotateValueSite(M, Inst, VD.get(), NV, Sum, ValueKind, MaxMDCount);
604}
605
606void annotateValueSite(Module &M, Instruction &Inst,
607 const InstrProfValueData VD[], uint32_t NV,
608 uint64_t Sum, InstrProfValueKind ValueKind,
609 uint32_t MaxMDCount) {
Xinliang David Li402477d2016-02-04 19:11:43 +0000610 LLVMContext &Ctx = M.getContext();
611 MDBuilder MDHelper(Ctx);
612 SmallVector<Metadata *, 3> Vals;
613 // Tag
614 Vals.push_back(MDHelper.createString("VP"));
615 // Value Kind
616 Vals.push_back(MDHelper.createConstant(
617 ConstantInt::get(Type::getInt32Ty(Ctx), ValueKind)));
618 // Total Count
619 Vals.push_back(
620 MDHelper.createConstant(ConstantInt::get(Type::getInt64Ty(Ctx), Sum)));
621
622 // Value Profile Data
Rong Xu69683f12016-02-10 22:19:43 +0000623 uint32_t MDCount = MaxMDCount;
Xinliang David Li402477d2016-02-04 19:11:43 +0000624 for (uint32_t I = 0; I < NV; ++I) {
625 Vals.push_back(MDHelper.createConstant(
626 ConstantInt::get(Type::getInt64Ty(Ctx), VD[I].Value)));
627 Vals.push_back(MDHelper.createConstant(
628 ConstantInt::get(Type::getInt64Ty(Ctx), VD[I].Count)));
629 if (--MDCount == 0)
630 break;
631 }
632 Inst.setMetadata(LLVMContext::MD_prof, MDNode::get(Ctx, Vals));
633}
634
635bool getValueProfDataFromInst(const Instruction &Inst,
636 InstrProfValueKind ValueKind,
637 uint32_t MaxNumValueData,
638 InstrProfValueData ValueData[],
639 uint32_t &ActualNumValueData, uint64_t &TotalC) {
640 MDNode *MD = Inst.getMetadata(LLVMContext::MD_prof);
641 if (!MD)
642 return false;
643
644 unsigned NOps = MD->getNumOperands();
645
646 if (NOps < 5)
647 return false;
648
649 // Operand 0 is a string tag "VP":
650 MDString *Tag = cast<MDString>(MD->getOperand(0));
651 if (!Tag)
652 return false;
653
654 if (!Tag->getString().equals("VP"))
655 return false;
656
657 // Now check kind:
658 ConstantInt *KindInt = mdconst::dyn_extract<ConstantInt>(MD->getOperand(1));
659 if (!KindInt)
660 return false;
661 if (KindInt->getZExtValue() != ValueKind)
662 return false;
663
664 // Get total count
665 ConstantInt *TotalCInt = mdconst::dyn_extract<ConstantInt>(MD->getOperand(2));
666 if (!TotalCInt)
667 return false;
668 TotalC = TotalCInt->getZExtValue();
669
670 ActualNumValueData = 0;
671
672 for (unsigned I = 3; I < NOps; I += 2) {
673 if (ActualNumValueData >= MaxNumValueData)
674 break;
675 ConstantInt *Value = mdconst::dyn_extract<ConstantInt>(MD->getOperand(I));
676 ConstantInt *Count =
677 mdconst::dyn_extract<ConstantInt>(MD->getOperand(I + 1));
678 if (!Value || !Count)
679 return false;
680 ValueData[ActualNumValueData].Value = Value->getZExtValue();
681 ValueData[ActualNumValueData].Count = Count->getZExtValue();
682 ActualNumValueData++;
683 }
684 return true;
685}
Eugene Zelenko6ac3f732016-01-26 18:48:36 +0000686} // end namespace llvm