blob: fc7fc8aa1e25dd4e621048d183c9a97daa324c93 [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 Li441959d2015-11-09 00:01:22 +000015#include "llvm/IR/Constants.h"
16#include "llvm/IR/Function.h"
17#include "llvm/IR/Module.h"
18#include "llvm/IR/GlobalVariable.h"
Justin Bognerf8d79192014-03-21 17:24:48 +000019#include "llvm/ProfileData/InstrProf.h"
20#include "llvm/Support/ErrorHandling.h"
Chris Bieneman1efe8012014-09-19 23:19:24 +000021#include "llvm/Support/ManagedStatic.h"
Justin Bognerf8d79192014-03-21 17:24:48 +000022
23using namespace llvm;
24
25namespace {
Rafael Espindola25188c92014-06-12 01:45:43 +000026class InstrProfErrorCategoryType : public std::error_category {
Rafael Espindolaf5d07fa2014-06-10 21:26:47 +000027 const char *name() const LLVM_NOEXCEPT override { return "llvm.instrprof"; }
Justin Bognerf8d79192014-03-21 17:24:48 +000028 std::string message(int IE) const override {
Rafael Espindola92512e82014-06-03 05:12:33 +000029 instrprof_error E = static_cast<instrprof_error>(IE);
Justin Bognerf8d79192014-03-21 17:24:48 +000030 switch (E) {
31 case instrprof_error::success:
32 return "Success";
33 case instrprof_error::eof:
34 return "End of File";
Nathan Slingerland4f823662015-11-13 03:47:58 +000035 case instrprof_error::unrecognized_format:
36 return "Unrecognized instrumentation profile encoding format";
Justin Bognerf8d79192014-03-21 17:24:48 +000037 case instrprof_error::bad_magic:
Nathan Slingerland4f823662015-11-13 03:47:58 +000038 return "Invalid instrumentation profile data (bad magic)";
Duncan P. N. Exon Smith531bb482014-03-21 20:42:28 +000039 case instrprof_error::bad_header:
Nathan Slingerland4f823662015-11-13 03:47:58 +000040 return "Invalid instrumentation profile data (file header is corrupt)";
Justin Bognerf8d79192014-03-21 17:24:48 +000041 case instrprof_error::unsupported_version:
Nathan Slingerland4f823662015-11-13 03:47:58 +000042 return "Unsupported instrumentation profile format version";
Justin Bognerb7aa2632014-04-18 21:48:40 +000043 case instrprof_error::unsupported_hash_type:
Nathan Slingerland4f823662015-11-13 03:47:58 +000044 return "Unsupported instrumentation profile hash type";
Justin Bognerf8d79192014-03-21 17:24:48 +000045 case instrprof_error::too_large:
46 return "Too much profile data";
47 case instrprof_error::truncated:
48 return "Truncated profile data";
49 case instrprof_error::malformed:
Nathan Slingerland4f823662015-11-13 03:47:58 +000050 return "Malformed instrumentation profile data";
Justin Bognerf8d79192014-03-21 17:24:48 +000051 case instrprof_error::unknown_function:
52 return "No profile data available for function";
Justin Bognerb9bd7f82014-03-21 17:46:22 +000053 case instrprof_error::hash_mismatch:
Nathan Slingerlande6e30d52015-11-17 22:08:53 +000054 return "Function control flow change detected (hash mismatch)";
Justin Bognerb9bd7f82014-03-21 17:46:22 +000055 case instrprof_error::count_mismatch:
Nathan Slingerlande6e30d52015-11-17 22:08:53 +000056 return "Function basic block count change detected (counter mismatch)";
Justin Bognerb9bd7f82014-03-21 17:46:22 +000057 case instrprof_error::counter_overflow:
58 return "Counter overflow";
Justin Bogner9e9a0572015-09-29 22:13:58 +000059 case instrprof_error::value_site_count_mismatch:
Nathan Slingerlande6e30d52015-11-17 22:08:53 +000060 return "Function value site count change detected (counter mismatch)";
Justin Bognerf8d79192014-03-21 17:24:48 +000061 }
62 llvm_unreachable("A value of instrprof_error has no message.");
63 }
Justin Bognerf8d79192014-03-21 17:24:48 +000064};
Alexander Kornienkof00654e2015-06-23 09:49:53 +000065}
Justin Bognerf8d79192014-03-21 17:24:48 +000066
Chris Bieneman1efe8012014-09-19 23:19:24 +000067static ManagedStatic<InstrProfErrorCategoryType> ErrorCategory;
68
Rafael Espindola25188c92014-06-12 01:45:43 +000069const std::error_category &llvm::instrprof_category() {
Chris Bieneman1efe8012014-09-19 23:19:24 +000070 return *ErrorCategory;
Justin Bognerf8d79192014-03-21 17:24:48 +000071}
Xinliang David Li441959d2015-11-09 00:01:22 +000072
73namespace llvm {
74
75std::string getPGOFuncName(StringRef RawFuncName,
76 GlobalValue::LinkageTypes Linkage,
77 StringRef FileName) {
78
79 // Function names may be prefixed with a binary '1' to indicate
80 // that the backend should not modify the symbols due to any platform
81 // naming convention. Do not include that '1' in the PGO profile name.
82 if (RawFuncName[0] == '\1')
83 RawFuncName = RawFuncName.substr(1);
84
85 std::string FuncName = RawFuncName;
86 if (llvm::GlobalValue::isLocalLinkage(Linkage)) {
87 // For local symbols, prepend the main file name to distinguish them.
88 // Do not include the full path in the file name since there's no guarantee
89 // that it will stay the same, e.g., if the files are checked out from
90 // version control in different locations.
91 if (FileName.empty())
92 FuncName = FuncName.insert(0, "<unknown>:");
93 else
94 FuncName = FuncName.insert(0, FileName.str() + ":");
95 }
96 return FuncName;
97}
98
99std::string getPGOFuncName(const Function &F) {
100 return getPGOFuncName(F.getName(), F.getLinkage(), F.getParent()->getName());
101}
102
103GlobalVariable *createPGOFuncNameVar(Module &M,
104 GlobalValue::LinkageTypes Linkage,
105 StringRef FuncName) {
106
107 // We generally want to match the function's linkage, but available_externally
108 // and extern_weak both have the wrong semantics, and anything that doesn't
109 // need to link across compilation units doesn't need to be visible at all.
110 if (Linkage == GlobalValue::ExternalWeakLinkage)
111 Linkage = GlobalValue::LinkOnceAnyLinkage;
112 else if (Linkage == GlobalValue::AvailableExternallyLinkage)
113 Linkage = GlobalValue::LinkOnceODRLinkage;
114 else if (Linkage == GlobalValue::InternalLinkage ||
115 Linkage == GlobalValue::ExternalLinkage)
116 Linkage = GlobalValue::PrivateLinkage;
117
118 auto *Value = ConstantDataArray::getString(M.getContext(), FuncName, false);
119 auto FuncNameVar =
120 new GlobalVariable(M, Value->getType(), true, Linkage, Value,
121 Twine(getInstrProfNameVarPrefix()) + FuncName);
122
123 // Hide the symbol so that we correctly get a copy for each executable.
124 if (!GlobalValue::isLocalLinkage(FuncNameVar->getLinkage()))
125 FuncNameVar->setVisibility(GlobalValue::HiddenVisibility);
126
127 return FuncNameVar;
128}
129
130GlobalVariable *createPGOFuncNameVar(Function &F, StringRef FuncName) {
131 return createPGOFuncNameVar(*F.getParent(), F.getLinkage(), FuncName);
132}
Xinliang David Liee415892015-11-10 00:24:45 +0000133
Xinliang David Liee415892015-11-10 00:24:45 +0000134uint32_t ValueProfRecord::getHeaderSize(uint32_t NumValueSites) {
135 uint32_t Size = offsetof(ValueProfRecord, SiteCountArray) +
136 sizeof(uint8_t) * NumValueSites;
137 // Round the size to multiple of 8 bytes.
138 Size = (Size + 7) & ~7;
139 return Size;
140}
141
142uint32_t ValueProfRecord::getSize(uint32_t NumValueSites,
143 uint32_t NumValueData) {
144 return getHeaderSize(NumValueSites) +
145 sizeof(InstrProfValueData) * NumValueData;
146}
147
148void ValueProfRecord::deserializeTo(InstrProfRecord &Record,
149 InstrProfRecord::ValueMapType *VMap) {
150 Record.reserveSites(Kind, NumValueSites);
151
152 InstrProfValueData *ValueData = this->getValueData();
153 for (uint64_t VSite = 0; VSite < NumValueSites; ++VSite) {
154 uint8_t ValueDataCount = this->SiteCountArray[VSite];
155 Record.addValueData(Kind, VSite, ValueData, ValueDataCount, VMap);
156 ValueData += ValueDataCount;
157 }
158}
159
160void ValueProfRecord::serializeFrom(const InstrProfRecord &Record,
161 uint32_t ValueKind,
162 uint32_t NumValueSites) {
163 Kind = ValueKind;
164 this->NumValueSites = NumValueSites;
165 InstrProfValueData *DstVD = getValueData();
166 for (uint32_t S = 0; S < NumValueSites; S++) {
167 uint32_t ND = Record.getNumValueDataForSite(ValueKind, S);
168 SiteCountArray[S] = ND;
169 std::unique_ptr<InstrProfValueData[]> SrcVD =
170 Record.getValueForSite(ValueKind, S);
171 for (uint32_t I = 0; I < ND; I++) {
172 DstVD[I] = SrcVD[I];
173 switch (ValueKind) {
174 case IPVK_IndirectCallTarget:
Xinliang David Li99556872015-11-17 23:00:40 +0000175 DstVD[I].Value = IndexedInstrProf::ComputeHash(
176 IndexedInstrProf::HashType, (const char *)DstVD[I].Value);
Xinliang David Liee415892015-11-10 00:24:45 +0000177 break;
178 default:
179 llvm_unreachable("value kind not handled !");
180 }
181 }
182 DstVD += ND;
183 }
184}
185
Xinliang David Lib8c3ad12015-11-17 03:47:21 +0000186template <class T>
187static T swapToHostOrder(const unsigned char *&D, support::endianness Orig) {
188 using namespace support;
189 if (Orig == little)
190 return endian::readNext<T, little, unaligned>(D);
191 else
192 return endian::readNext<T, big, unaligned>(D);
Xinliang David Liee415892015-11-10 00:24:45 +0000193}
194
195// For writing/serializing, Old is the host endianness, and New is
196// byte order intended on disk. For Reading/deserialization, Old
197// is the on-disk source endianness, and New is the host endianness.
198void ValueProfRecord::swapBytes(support::endianness Old,
199 support::endianness New) {
200 using namespace support;
201 if (Old == New)
202 return;
203
204 if (getHostEndianness() != Old) {
205 sys::swapByteOrder<uint32_t>(NumValueSites);
206 sys::swapByteOrder<uint32_t>(Kind);
207 }
208 uint32_t ND = getNumValueData();
209 InstrProfValueData *VD = getValueData();
210
211 // No need to swap byte array: SiteCountArrray.
212 for (uint32_t I = 0; I < ND; I++) {
213 sys::swapByteOrder<uint64_t>(VD[I].Value);
214 sys::swapByteOrder<uint64_t>(VD[I].Count);
215 }
216 if (getHostEndianness() == Old) {
217 sys::swapByteOrder<uint32_t>(NumValueSites);
218 sys::swapByteOrder<uint32_t>(Kind);
219 }
220}
221
222uint32_t ValueProfData::getSize(const InstrProfRecord &Record) {
223 uint32_t TotalSize = sizeof(ValueProfData);
224 uint32_t NumValueKinds = Record.getNumValueKinds();
225 if (NumValueKinds == 0)
226 return TotalSize;
227
228 for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; Kind++) {
229 uint32_t NumValueSites = Record.getNumValueSites(Kind);
230 if (!NumValueSites)
231 continue;
232 TotalSize +=
233 ValueProfRecord::getSize(NumValueSites, Record.getNumValueData(Kind));
234 }
235 return TotalSize;
236}
237
238void ValueProfData::deserializeTo(InstrProfRecord &Record,
239 InstrProfRecord::ValueMapType *VMap) {
240 if (NumValueKinds == 0)
241 return;
242
243 ValueProfRecord *VR = getFirstValueProfRecord();
244 for (uint32_t K = 0; K < NumValueKinds; K++) {
245 VR->deserializeTo(Record, VMap);
246 VR = VR->getNext();
247 }
248}
249
David Blaikie1070a092015-11-11 20:44:52 +0000250static std::unique_ptr<ValueProfData> AllocValueProfData(uint32_t TotalSize) {
251 return std::unique_ptr<ValueProfData>(new (::operator new(TotalSize))
252 ValueProfData());
Xinliang David Li4d1bef32015-11-11 19:31:53 +0000253}
254
Xinliang David Liee415892015-11-10 00:24:45 +0000255std::unique_ptr<ValueProfData>
256ValueProfData::serializeFrom(const InstrProfRecord &Record) {
257 uint32_t TotalSize = getSize(Record);
Xinliang David Li4d1bef32015-11-11 19:31:53 +0000258
David Blaikie1070a092015-11-11 20:44:52 +0000259 std::unique_ptr<ValueProfData> VPD = AllocValueProfData(TotalSize);
Xinliang David Liee415892015-11-10 00:24:45 +0000260
261 VPD->TotalSize = TotalSize;
262 VPD->NumValueKinds = Record.getNumValueKinds();
263 ValueProfRecord *VR = VPD->getFirstValueProfRecord();
264 for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; Kind++) {
265 uint32_t NumValueSites = Record.getNumValueSites(Kind);
266 if (!NumValueSites)
267 continue;
268 VR->serializeFrom(Record, Kind, NumValueSites);
269 VR = VR->getNext();
270 }
271 return VPD;
272}
273
274ErrorOr<std::unique_ptr<ValueProfData>>
275ValueProfData::getValueProfData(const unsigned char *D,
276 const unsigned char *const BufferEnd,
277 support::endianness Endianness) {
278 using namespace support;
279 if (D + sizeof(ValueProfData) > BufferEnd)
280 return instrprof_error::truncated;
281
Xinliang David Lib8c3ad12015-11-17 03:47:21 +0000282 const unsigned char *Header = D;
283 uint32_t TotalSize = swapToHostOrder<uint32_t>(Header, Endianness);
284 uint32_t NumValueKinds = swapToHostOrder<uint32_t>(Header, Endianness);
Xinliang David Liee415892015-11-10 00:24:45 +0000285
286 if (D + TotalSize > BufferEnd)
287 return instrprof_error::too_large;
288 if (NumValueKinds > IPVK_Last + 1)
289 return instrprof_error::malformed;
290 // Total size needs to be mulltiple of quadword size.
291 if (TotalSize % sizeof(uint64_t))
292 return instrprof_error::malformed;
293
David Blaikie1070a092015-11-11 20:44:52 +0000294 std::unique_ptr<ValueProfData> VPD = AllocValueProfData(TotalSize);
Xinliang David Li4d1bef32015-11-11 19:31:53 +0000295
Xinliang David Liee415892015-11-10 00:24:45 +0000296 memcpy(VPD.get(), D, TotalSize);
297 // Byte swap.
298 VPD->swapBytesToHost(Endianness);
299
300 // Data integrety check:
301 ValueProfRecord *VR = VPD->getFirstValueProfRecord();
302 for (uint32_t K = 0; K < VPD->NumValueKinds; K++) {
303 if (VR->Kind > IPVK_Last)
304 return instrprof_error::malformed;
305 VR = VR->getNext();
Aaron Ballman470b5f12015-11-11 14:57:28 +0000306 if ((char *)VR - (char *)VPD.get() > (ptrdiff_t)TotalSize)
Xinliang David Liee415892015-11-10 00:24:45 +0000307 return instrprof_error::malformed;
308 }
309
Xinliang David Liee415892015-11-10 00:24:45 +0000310 return std::move(VPD);
311}
312
313void ValueProfData::swapBytesToHost(support::endianness Endianness) {
314 using namespace support;
315 if (Endianness == getHostEndianness())
316 return;
317
318 sys::swapByteOrder<uint32_t>(TotalSize);
319 sys::swapByteOrder<uint32_t>(NumValueKinds);
320
321 ValueProfRecord *VR = getFirstValueProfRecord();
322 for (uint32_t K = 0; K < NumValueKinds; K++) {
323 VR->swapBytes(Endianness, getHostEndianness());
324 VR = VR->getNext();
325 }
326}
327
328void ValueProfData::swapBytesFromHost(support::endianness Endianness) {
329 using namespace support;
330 if (Endianness == getHostEndianness())
331 return;
332
333 ValueProfRecord *VR = getFirstValueProfRecord();
334 for (uint32_t K = 0; K < NumValueKinds; K++) {
335 ValueProfRecord *NVR = VR->getNext();
336 VR->swapBytes(getHostEndianness(), Endianness);
337 VR = NVR;
338 }
339 sys::swapByteOrder<uint32_t>(TotalSize);
340 sys::swapByteOrder<uint32_t>(NumValueKinds);
341}
342
343ValueProfRecord *ValueProfData::getFirstValueProfRecord() {
344 return reinterpret_cast<ValueProfRecord *>((char *)this +
345 sizeof(ValueProfData));
346}
347
348uint32_t ValueProfRecord::getNumValueData() const {
349 uint32_t NumValueData = 0;
350 for (uint32_t I = 0; I < NumValueSites; I++)
351 NumValueData += SiteCountArray[I];
352 return NumValueData;
353}
354
355ValueProfRecord *ValueProfRecord::getNext() {
356 return reinterpret_cast<ValueProfRecord *>((char *)this + getSize());
357}
358
359InstrProfValueData *ValueProfRecord::getValueData() {
360 return reinterpret_cast<InstrProfValueData *>((char *)this +
361 getHeaderSize(NumValueSites));
362}
Xinliang David Li441959d2015-11-09 00:01:22 +0000363}