blob: 34a354da896d8aa18079e0b0dc3a6310ed9ff12a [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 Li4945b162015-11-25 00:08:49 +0000134uint64_t stringToHash(uint32_t ValueKind, uint64_t Value) {
Xinliang David Li28b70032015-11-24 23:36:52 +0000135 switch (ValueKind) {
136 case IPVK_IndirectCallTarget:
137 return IndexedInstrProf::ComputeHash(IndexedInstrProf::HashType,
138 (const char *)Value);
139 break;
140 default:
141 llvm_unreachable("value kind not handled !");
142 }
143 return Value;
144}
145
Xinliang David Liee415892015-11-10 00:24:45 +0000146void ValueProfRecord::deserializeTo(InstrProfRecord &Record,
147 InstrProfRecord::ValueMapType *VMap) {
148 Record.reserveSites(Kind, NumValueSites);
149
Xinliang David Liac5b8602015-11-25 04:29:24 +0000150 InstrProfValueData *ValueData = getValueProfRecordValueData(this);
Xinliang David Liee415892015-11-10 00:24:45 +0000151 for (uint64_t VSite = 0; VSite < NumValueSites; ++VSite) {
152 uint8_t ValueDataCount = this->SiteCountArray[VSite];
153 Record.addValueData(Kind, VSite, ValueData, ValueDataCount, VMap);
154 ValueData += ValueDataCount;
155 }
156}
157
158void ValueProfRecord::serializeFrom(const InstrProfRecord &Record,
159 uint32_t ValueKind,
160 uint32_t NumValueSites) {
161 Kind = ValueKind;
162 this->NumValueSites = NumValueSites;
Xinliang David Liac5b8602015-11-25 04:29:24 +0000163 InstrProfValueData *DstVD = getValueProfRecordValueData(this);
Xinliang David Liee415892015-11-10 00:24:45 +0000164 for (uint32_t S = 0; S < NumValueSites; S++) {
165 uint32_t ND = Record.getNumValueDataForSite(ValueKind, S);
166 SiteCountArray[S] = ND;
Xinliang David Li4945b162015-11-25 00:08:49 +0000167 Record.getValueForSite(DstVD, ValueKind, S, stringToHash);
Xinliang David Liee415892015-11-10 00:24:45 +0000168 DstVD += ND;
169 }
170}
171
Xinliang David Lib8c3ad12015-11-17 03:47:21 +0000172template <class T>
173static T swapToHostOrder(const unsigned char *&D, support::endianness Orig) {
174 using namespace support;
175 if (Orig == little)
176 return endian::readNext<T, little, unaligned>(D);
177 else
178 return endian::readNext<T, big, unaligned>(D);
Xinliang David Liee415892015-11-10 00:24:45 +0000179}
180
181// For writing/serializing, Old is the host endianness, and New is
182// byte order intended on disk. For Reading/deserialization, Old
183// is the on-disk source endianness, and New is the host endianness.
184void ValueProfRecord::swapBytes(support::endianness Old,
185 support::endianness New) {
186 using namespace support;
187 if (Old == New)
188 return;
189
190 if (getHostEndianness() != Old) {
191 sys::swapByteOrder<uint32_t>(NumValueSites);
192 sys::swapByteOrder<uint32_t>(Kind);
193 }
Xinliang David Liac5b8602015-11-25 04:29:24 +0000194 uint32_t ND = getValueProfRecordNumValueData(this);
195 InstrProfValueData *VD = getValueProfRecordValueData(this);
Xinliang David Liee415892015-11-10 00:24:45 +0000196
197 // No need to swap byte array: SiteCountArrray.
198 for (uint32_t I = 0; I < ND; I++) {
199 sys::swapByteOrder<uint64_t>(VD[I].Value);
200 sys::swapByteOrder<uint64_t>(VD[I].Count);
201 }
202 if (getHostEndianness() == Old) {
203 sys::swapByteOrder<uint32_t>(NumValueSites);
204 sys::swapByteOrder<uint32_t>(Kind);
205 }
206}
207
208uint32_t ValueProfData::getSize(const InstrProfRecord &Record) {
209 uint32_t TotalSize = sizeof(ValueProfData);
210 uint32_t NumValueKinds = Record.getNumValueKinds();
211 if (NumValueKinds == 0)
212 return TotalSize;
213
214 for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; Kind++) {
215 uint32_t NumValueSites = Record.getNumValueSites(Kind);
216 if (!NumValueSites)
217 continue;
218 TotalSize +=
Xinliang David Li759dc622015-11-24 18:15:46 +0000219 getValueProfRecordSize(NumValueSites, Record.getNumValueData(Kind));
Xinliang David Liee415892015-11-10 00:24:45 +0000220 }
221 return TotalSize;
222}
223
224void ValueProfData::deserializeTo(InstrProfRecord &Record,
225 InstrProfRecord::ValueMapType *VMap) {
226 if (NumValueKinds == 0)
227 return;
228
229 ValueProfRecord *VR = getFirstValueProfRecord();
230 for (uint32_t K = 0; K < NumValueKinds; K++) {
231 VR->deserializeTo(Record, VMap);
Xinliang David Liac5b8602015-11-25 04:29:24 +0000232 VR = getValueProfRecordNext(VR);
Xinliang David Liee415892015-11-10 00:24:45 +0000233 }
234}
235
David Blaikie1070a092015-11-11 20:44:52 +0000236static std::unique_ptr<ValueProfData> AllocValueProfData(uint32_t TotalSize) {
237 return std::unique_ptr<ValueProfData>(new (::operator new(TotalSize))
238 ValueProfData());
Xinliang David Li4d1bef32015-11-11 19:31:53 +0000239}
240
Xinliang David Liee415892015-11-10 00:24:45 +0000241std::unique_ptr<ValueProfData>
242ValueProfData::serializeFrom(const InstrProfRecord &Record) {
243 uint32_t TotalSize = getSize(Record);
Xinliang David Li4d1bef32015-11-11 19:31:53 +0000244
David Blaikie1070a092015-11-11 20:44:52 +0000245 std::unique_ptr<ValueProfData> VPD = AllocValueProfData(TotalSize);
Xinliang David Liee415892015-11-10 00:24:45 +0000246
247 VPD->TotalSize = TotalSize;
248 VPD->NumValueKinds = Record.getNumValueKinds();
249 ValueProfRecord *VR = VPD->getFirstValueProfRecord();
250 for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; Kind++) {
251 uint32_t NumValueSites = Record.getNumValueSites(Kind);
252 if (!NumValueSites)
253 continue;
254 VR->serializeFrom(Record, Kind, NumValueSites);
Xinliang David Liac5b8602015-11-25 04:29:24 +0000255 VR = getValueProfRecordNext(VR);
Xinliang David Liee415892015-11-10 00:24:45 +0000256 }
257 return VPD;
258}
259
260ErrorOr<std::unique_ptr<ValueProfData>>
261ValueProfData::getValueProfData(const unsigned char *D,
262 const unsigned char *const BufferEnd,
263 support::endianness Endianness) {
264 using namespace support;
265 if (D + sizeof(ValueProfData) > BufferEnd)
266 return instrprof_error::truncated;
267
Xinliang David Lib8c3ad12015-11-17 03:47:21 +0000268 const unsigned char *Header = D;
269 uint32_t TotalSize = swapToHostOrder<uint32_t>(Header, Endianness);
270 uint32_t NumValueKinds = swapToHostOrder<uint32_t>(Header, Endianness);
Xinliang David Liee415892015-11-10 00:24:45 +0000271
272 if (D + TotalSize > BufferEnd)
273 return instrprof_error::too_large;
274 if (NumValueKinds > IPVK_Last + 1)
275 return instrprof_error::malformed;
276 // Total size needs to be mulltiple of quadword size.
277 if (TotalSize % sizeof(uint64_t))
278 return instrprof_error::malformed;
279
David Blaikie1070a092015-11-11 20:44:52 +0000280 std::unique_ptr<ValueProfData> VPD = AllocValueProfData(TotalSize);
Xinliang David Li4d1bef32015-11-11 19:31:53 +0000281
Xinliang David Liee415892015-11-10 00:24:45 +0000282 memcpy(VPD.get(), D, TotalSize);
283 // Byte swap.
284 VPD->swapBytesToHost(Endianness);
285
Xinliang David Liac5b8602015-11-25 04:29:24 +0000286 // Data integrity check:
Xinliang David Liee415892015-11-10 00:24:45 +0000287 ValueProfRecord *VR = VPD->getFirstValueProfRecord();
288 for (uint32_t K = 0; K < VPD->NumValueKinds; K++) {
289 if (VR->Kind > IPVK_Last)
290 return instrprof_error::malformed;
Xinliang David Liac5b8602015-11-25 04:29:24 +0000291 VR = getValueProfRecordNext(VR);
Aaron Ballman470b5f12015-11-11 14:57:28 +0000292 if ((char *)VR - (char *)VPD.get() > (ptrdiff_t)TotalSize)
Xinliang David Liee415892015-11-10 00:24:45 +0000293 return instrprof_error::malformed;
294 }
295
Xinliang David Liee415892015-11-10 00:24:45 +0000296 return std::move(VPD);
297}
298
299void ValueProfData::swapBytesToHost(support::endianness Endianness) {
300 using namespace support;
301 if (Endianness == getHostEndianness())
302 return;
303
304 sys::swapByteOrder<uint32_t>(TotalSize);
305 sys::swapByteOrder<uint32_t>(NumValueKinds);
306
307 ValueProfRecord *VR = getFirstValueProfRecord();
308 for (uint32_t K = 0; K < NumValueKinds; K++) {
309 VR->swapBytes(Endianness, getHostEndianness());
Xinliang David Liac5b8602015-11-25 04:29:24 +0000310 VR = getValueProfRecordNext(VR);
Xinliang David Liee415892015-11-10 00:24:45 +0000311 }
312}
313
314void ValueProfData::swapBytesFromHost(support::endianness Endianness) {
315 using namespace support;
316 if (Endianness == getHostEndianness())
317 return;
318
319 ValueProfRecord *VR = getFirstValueProfRecord();
320 for (uint32_t K = 0; K < NumValueKinds; K++) {
Xinliang David Liac5b8602015-11-25 04:29:24 +0000321 ValueProfRecord *NVR = getValueProfRecordNext(VR);
Xinliang David Liee415892015-11-10 00:24:45 +0000322 VR->swapBytes(getHostEndianness(), Endianness);
323 VR = NVR;
324 }
325 sys::swapByteOrder<uint32_t>(TotalSize);
326 sys::swapByteOrder<uint32_t>(NumValueKinds);
327}
328
329ValueProfRecord *ValueProfData::getFirstValueProfRecord() {
330 return reinterpret_cast<ValueProfRecord *>((char *)this +
331 sizeof(ValueProfData));
332}
Xinliang David Liee415892015-11-10 00:24:45 +0000333}