Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 1 | //=-- 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 Li | 441959d | 2015-11-09 00:01:22 +0000 | [diff] [blame] | 15 | #include "llvm/IR/Constants.h" |
| 16 | #include "llvm/IR/Function.h" |
| 17 | #include "llvm/IR/Module.h" |
| 18 | #include "llvm/IR/GlobalVariable.h" |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 19 | #include "llvm/ProfileData/InstrProf.h" |
| 20 | #include "llvm/Support/ErrorHandling.h" |
Chris Bieneman | 1efe801 | 2014-09-19 23:19:24 +0000 | [diff] [blame] | 21 | #include "llvm/Support/ManagedStatic.h" |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 22 | |
| 23 | using namespace llvm; |
| 24 | |
| 25 | namespace { |
Rafael Espindola | 25188c9 | 2014-06-12 01:45:43 +0000 | [diff] [blame] | 26 | class InstrProfErrorCategoryType : public std::error_category { |
Rafael Espindola | f5d07fa | 2014-06-10 21:26:47 +0000 | [diff] [blame] | 27 | const char *name() const LLVM_NOEXCEPT override { return "llvm.instrprof"; } |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 28 | std::string message(int IE) const override { |
Rafael Espindola | 92512e8 | 2014-06-03 05:12:33 +0000 | [diff] [blame] | 29 | instrprof_error E = static_cast<instrprof_error>(IE); |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 30 | switch (E) { |
| 31 | case instrprof_error::success: |
| 32 | return "Success"; |
| 33 | case instrprof_error::eof: |
| 34 | return "End of File"; |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 35 | case instrprof_error::unrecognized_format: |
| 36 | return "Unrecognized instrumentation profile encoding format"; |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 37 | case instrprof_error::bad_magic: |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 38 | return "Invalid instrumentation profile data (bad magic)"; |
Duncan P. N. Exon Smith | 531bb48 | 2014-03-21 20:42:28 +0000 | [diff] [blame] | 39 | case instrprof_error::bad_header: |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 40 | return "Invalid instrumentation profile data (file header is corrupt)"; |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 41 | case instrprof_error::unsupported_version: |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 42 | return "Unsupported instrumentation profile format version"; |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 43 | case instrprof_error::unsupported_hash_type: |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 44 | return "Unsupported instrumentation profile hash type"; |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 45 | 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 Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 50 | return "Malformed instrumentation profile data"; |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 51 | case instrprof_error::unknown_function: |
| 52 | return "No profile data available for function"; |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 53 | case instrprof_error::hash_mismatch: |
Nathan Slingerland | e6e30d5 | 2015-11-17 22:08:53 +0000 | [diff] [blame] | 54 | return "Function control flow change detected (hash mismatch)"; |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 55 | case instrprof_error::count_mismatch: |
Nathan Slingerland | e6e30d5 | 2015-11-17 22:08:53 +0000 | [diff] [blame] | 56 | return "Function basic block count change detected (counter mismatch)"; |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 57 | case instrprof_error::counter_overflow: |
| 58 | return "Counter overflow"; |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 59 | case instrprof_error::value_site_count_mismatch: |
Nathan Slingerland | e6e30d5 | 2015-11-17 22:08:53 +0000 | [diff] [blame] | 60 | return "Function value site count change detected (counter mismatch)"; |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 61 | } |
| 62 | llvm_unreachable("A value of instrprof_error has no message."); |
| 63 | } |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 64 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 65 | } |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 66 | |
Chris Bieneman | 1efe801 | 2014-09-19 23:19:24 +0000 | [diff] [blame] | 67 | static ManagedStatic<InstrProfErrorCategoryType> ErrorCategory; |
| 68 | |
Rafael Espindola | 25188c9 | 2014-06-12 01:45:43 +0000 | [diff] [blame] | 69 | const std::error_category &llvm::instrprof_category() { |
Chris Bieneman | 1efe801 | 2014-09-19 23:19:24 +0000 | [diff] [blame] | 70 | return *ErrorCategory; |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 71 | } |
Xinliang David Li | 441959d | 2015-11-09 00:01:22 +0000 | [diff] [blame] | 72 | |
| 73 | namespace llvm { |
| 74 | |
| 75 | std::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 | |
| 99 | std::string getPGOFuncName(const Function &F) { |
| 100 | return getPGOFuncName(F.getName(), F.getLinkage(), F.getParent()->getName()); |
| 101 | } |
| 102 | |
| 103 | GlobalVariable *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 | |
| 130 | GlobalVariable *createPGOFuncNameVar(Function &F, StringRef FuncName) { |
| 131 | return createPGOFuncNameVar(*F.getParent(), F.getLinkage(), FuncName); |
| 132 | } |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 133 | |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 134 | /// Return the total size in bytes of the on-disk value profile data |
| 135 | /// given the data stored in Record. |
| 136 | uint32_t getValueProfDataSize(ValueProfRecordClosure *Closure) { |
| 137 | uint32_t Kind; |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 138 | uint32_t TotalSize = sizeof(ValueProfData); |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 139 | const void *Record = Closure->Record; |
| 140 | uint32_t NumValueKinds = Closure->GetNumValueKinds(Record); |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 141 | if (NumValueKinds == 0) |
| 142 | return TotalSize; |
| 143 | |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 144 | for (Kind = IPVK_First; Kind <= IPVK_Last; Kind++) { |
| 145 | uint32_t NumValueSites = Closure->GetNumValueSites(Record, Kind); |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 146 | if (!NumValueSites) |
| 147 | continue; |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 148 | TotalSize += getValueProfRecordSize(NumValueSites, |
| 149 | Closure->GetNumValueData(Record, Kind)); |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 150 | } |
| 151 | return TotalSize; |
| 152 | } |
| 153 | |
Xinliang David Li | e809231 | 2015-11-25 19:13:00 +0000 | [diff] [blame] | 154 | // Extract data from \c Closure and serialize into \c This instance. |
| 155 | void serializeValueProfRecordFrom(ValueProfRecord *This, |
| 156 | ValueProfRecordClosure *Closure, |
| 157 | uint32_t ValueKind, uint32_t NumValueSites) { |
| 158 | uint32_t S; |
| 159 | const void *Record = Closure->Record; |
| 160 | This->Kind = ValueKind; |
| 161 | This->NumValueSites = NumValueSites; |
| 162 | InstrProfValueData *DstVD = getValueProfRecordValueData(This); |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 163 | |
Xinliang David Li | e809231 | 2015-11-25 19:13:00 +0000 | [diff] [blame] | 164 | for (S = 0; S < NumValueSites; S++) { |
| 165 | uint32_t ND = Closure->GetNumValueDataForSite(Record, ValueKind, S); |
| 166 | This->SiteCountArray[S] = ND; |
| 167 | Closure->GetValueForSite(Record, DstVD, ValueKind, S, |
| 168 | Closure->RemapValueData); |
| 169 | DstVD += ND; |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 173 | ValueProfData *serializeValueProfDataFrom(ValueProfRecordClosure *Closure) { |
| 174 | uint32_t TotalSize = getValueProfDataSize(Closure); |
Xinliang David Li | 4d1bef3 | 2015-11-11 19:31:53 +0000 | [diff] [blame] | 175 | |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 176 | ValueProfData *VPD = Closure->AllocValueProfData(TotalSize); |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 177 | |
| 178 | VPD->TotalSize = TotalSize; |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 179 | VPD->NumValueKinds = Closure->GetNumValueKinds(Closure->Record); |
| 180 | ValueProfRecord *VR = getFirstValueProfRecord(VPD); |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 181 | for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; Kind++) { |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 182 | uint32_t NumValueSites = Closure->GetNumValueSites(Closure->Record, Kind); |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 183 | if (!NumValueSites) |
| 184 | continue; |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 185 | serializeValueProfRecordFrom(VR, Closure, Kind, NumValueSites); |
Xinliang David Li | ac5b860 | 2015-11-25 04:29:24 +0000 | [diff] [blame] | 186 | VR = getValueProfRecordNext(VR); |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 187 | } |
| 188 | return VPD; |
| 189 | } |
| 190 | |
Xinliang David Li | ed96677 | 2015-11-25 23:31:18 +0000 | [diff] [blame] | 191 | /*! \brief ValueProfRecordClosure Interface implementation for InstrProfRecord |
| 192 | * class. These C wrappers are used as adaptors so that C++ code can be |
| 193 | * invoked as callbacks. |
| 194 | */ |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 195 | uint32_t getNumValueKindsInstrProf(const void *Record) { |
| 196 | return reinterpret_cast<const InstrProfRecord *>(Record)->getNumValueKinds(); |
| 197 | } |
| 198 | |
| 199 | uint32_t getNumValueSitesInstrProf(const void *Record, uint32_t VKind) { |
| 200 | return reinterpret_cast<const InstrProfRecord *>(Record) |
| 201 | ->getNumValueSites(VKind); |
| 202 | } |
| 203 | |
| 204 | uint32_t getNumValueDataInstrProf(const void *Record, uint32_t VKind) { |
| 205 | return reinterpret_cast<const InstrProfRecord *>(Record) |
| 206 | ->getNumValueData(VKind); |
| 207 | } |
| 208 | |
| 209 | uint32_t getNumValueDataForSiteInstrProf(const void *R, uint32_t VK, |
| 210 | uint32_t S) { |
| 211 | return reinterpret_cast<const InstrProfRecord *>(R) |
| 212 | ->getNumValueDataForSite(VK, S); |
| 213 | } |
| 214 | |
| 215 | void getValueForSiteInstrProf(const void *R, InstrProfValueData *Dst, |
| 216 | uint32_t K, uint32_t S, |
| 217 | uint64_t (*Mapper)(uint32_t, uint64_t)) { |
| 218 | return reinterpret_cast<const InstrProfRecord *>(R) |
| 219 | ->getValueForSite(Dst, K, S, Mapper); |
| 220 | } |
| 221 | |
Xinliang David Li | e809231 | 2015-11-25 19:13:00 +0000 | [diff] [blame] | 222 | uint64_t stringToHash(uint32_t ValueKind, uint64_t Value) { |
| 223 | switch (ValueKind) { |
| 224 | case IPVK_IndirectCallTarget: |
| 225 | return IndexedInstrProf::ComputeHash(IndexedInstrProf::HashType, |
| 226 | (const char *)Value); |
| 227 | break; |
| 228 | default: |
| 229 | llvm_unreachable("value kind not handled !"); |
| 230 | } |
| 231 | return Value; |
| 232 | } |
| 233 | |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 234 | ValueProfData *allocValueProfDataInstrProf(size_t TotalSizeInBytes) { |
| 235 | return (ValueProfData *)(new (::operator new(TotalSizeInBytes)) |
| 236 | ValueProfData()); |
| 237 | } |
| 238 | |
| 239 | static ValueProfRecordClosure InstrProfRecordClosure = { |
| 240 | 0, |
| 241 | getNumValueKindsInstrProf, |
| 242 | getNumValueSitesInstrProf, |
| 243 | getNumValueDataInstrProf, |
| 244 | getNumValueDataForSiteInstrProf, |
| 245 | stringToHash, |
| 246 | getValueForSiteInstrProf, |
Xinliang David Li | e809231 | 2015-11-25 19:13:00 +0000 | [diff] [blame] | 247 | allocValueProfDataInstrProf |
| 248 | }; |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 249 | |
Xinliang David Li | e809231 | 2015-11-25 19:13:00 +0000 | [diff] [blame] | 250 | // Wrapper implementation using the closure mechanism. |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 251 | uint32_t ValueProfData::getSize(const InstrProfRecord &Record) { |
| 252 | InstrProfRecordClosure.Record = &Record; |
| 253 | return getValueProfDataSize(&InstrProfRecordClosure); |
| 254 | } |
| 255 | |
Xinliang David Li | e809231 | 2015-11-25 19:13:00 +0000 | [diff] [blame] | 256 | // Wrapper implementation using the closure mechanism. |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 257 | std::unique_ptr<ValueProfData> |
| 258 | ValueProfData::serializeFrom(const InstrProfRecord &Record) { |
| 259 | InstrProfRecordClosure.Record = &Record; |
| 260 | |
| 261 | std::unique_ptr<ValueProfData> VPD( |
| 262 | serializeValueProfDataFrom(&InstrProfRecordClosure)); |
| 263 | return VPD; |
| 264 | } |
| 265 | |
Xinliang David Li | ed96677 | 2015-11-25 23:31:18 +0000 | [diff] [blame] | 266 | /* The value profiler runtime library stores the value profile data |
| 267 | * for a given function in NumValueSites and Nodes. This is the |
| 268 | * method to initialize the RuntimeRecord with the runtime data to |
| 269 | * pre-compute the information needed to efficiently implement |
| 270 | * ValueProfRecordClosure's callback interfaces. |
| 271 | */ |
| 272 | void initializeValueProfRuntimeRecord(ValueProfRuntimeRecord *RuntimeRecord, |
| 273 | uint16_t *NumValueSites, |
| 274 | ValueProfNode **Nodes) { |
| 275 | unsigned I, J, S = 0, NumValueKinds = 0; |
| 276 | RuntimeRecord->NumValueSites = NumValueSites; |
| 277 | RuntimeRecord->Nodes = Nodes; |
| 278 | for (I = 0; I <= IPVK_Last; I++) { |
| 279 | uint16_t N = NumValueSites[I]; |
| 280 | if (!N) { |
| 281 | RuntimeRecord->SiteCountArray[I] = 0; |
| 282 | continue; |
| 283 | } |
| 284 | NumValueKinds++; |
| 285 | RuntimeRecord->SiteCountArray[I] = (uint8_t *)calloc(N, 1); |
| 286 | RuntimeRecord->NodesKind[I] = &RuntimeRecord->Nodes[S]; |
| 287 | for (J = 0; J < N; J++) { |
| 288 | uint8_t C = 0; |
| 289 | ValueProfNode *Site = RuntimeRecord->Nodes[S + J]; |
| 290 | while (Site) { |
| 291 | C++; |
| 292 | Site = Site->Next; |
| 293 | } |
| 294 | if (C > UCHAR_MAX) |
| 295 | C = UCHAR_MAX; |
| 296 | RuntimeRecord->SiteCountArray[I][J] = C; |
| 297 | } |
| 298 | S += N; |
| 299 | } |
| 300 | RuntimeRecord->NumValueKinds = NumValueKinds; |
| 301 | } |
| 302 | |
| 303 | void finalizeValueProfRuntimeRecord(ValueProfRuntimeRecord *RuntimeRecord) { |
| 304 | unsigned I; |
| 305 | for (I = 0; I <= IPVK_Last; I++) { |
| 306 | if (RuntimeRecord->SiteCountArray[I]) |
| 307 | free(RuntimeRecord->SiteCountArray[I]); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | /* ValueProfRecordClosure Interface implementation for |
| 312 | * ValueProfDataRuntimeRecord. */ |
| 313 | uint32_t getNumValueKindsRT(const void *R) { |
| 314 | return ((const ValueProfRuntimeRecord *)R)->NumValueKinds; |
| 315 | } |
| 316 | |
| 317 | uint32_t getNumValueSitesRT(const void *R, uint32_t VK) { |
| 318 | return ((const ValueProfRuntimeRecord *)R)->NumValueSites[VK]; |
| 319 | } |
| 320 | |
| 321 | uint32_t getNumValueDataForSiteRT(const void *R, uint32_t VK, uint32_t S) { |
| 322 | const ValueProfRuntimeRecord *Record = (const ValueProfRuntimeRecord *)R; |
| 323 | return Record->SiteCountArray[VK][S]; |
| 324 | } |
| 325 | |
| 326 | uint32_t getNumValueDataRT(const void *R, uint32_t VK) { |
| 327 | unsigned I, S = 0; |
| 328 | const ValueProfRuntimeRecord *Record = (const ValueProfRuntimeRecord *)R; |
| 329 | if (Record->SiteCountArray[VK] == 0) |
| 330 | return 0; |
| 331 | for (I = 0; I < Record->NumValueSites[VK]; I++) |
| 332 | S += Record->SiteCountArray[VK][I]; |
| 333 | return S; |
| 334 | } |
| 335 | |
| 336 | void getValueForSiteRT(const void *R, InstrProfValueData *Dst, uint32_t VK, |
| 337 | uint32_t S, uint64_t (*Mapper)(uint32_t, uint64_t)) { |
| 338 | unsigned I, N = 0; |
| 339 | const ValueProfRuntimeRecord *Record = (const ValueProfRuntimeRecord *)R; |
| 340 | N = getNumValueDataForSiteRT(R, VK, S); |
| 341 | ValueProfNode *VNode = Record->NodesKind[VK][S]; |
| 342 | for (I = 0; I < N; I++) { |
| 343 | Dst[I] = VNode->VData; |
| 344 | VNode = VNode->Next; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | ValueProfData *allocValueProfDataRT(size_t TotalSizeInBytes) { |
| 349 | return (ValueProfData *)calloc(TotalSizeInBytes, 1); |
| 350 | } |
| 351 | |
| 352 | static ValueProfRecordClosure RTRecordClosure = {0, |
| 353 | getNumValueKindsRT, |
| 354 | getNumValueSitesRT, |
| 355 | getNumValueDataRT, |
| 356 | getNumValueDataForSiteRT, |
| 357 | 0, |
| 358 | getValueForSiteRT, |
| 359 | allocValueProfDataRT}; |
| 360 | |
| 361 | /* Return the size of ValueProfData structure to store data |
| 362 | * recorded in the runtime record. |
| 363 | */ |
| 364 | uint32_t getValueProfDataSizeRT(const ValueProfRuntimeRecord *Record) { |
| 365 | RTRecordClosure.Record = Record; |
| 366 | return getValueProfDataSize(&RTRecordClosure); |
| 367 | } |
| 368 | |
| 369 | /* Return a ValueProfData instance that stores the data collected |
| 370 | from runtime. */ |
| 371 | ValueProfData * |
| 372 | serializeValueProfDataFromRT(const ValueProfRuntimeRecord *Record) { |
| 373 | RTRecordClosure.Record = Record; |
| 374 | return serializeValueProfDataFrom(&RTRecordClosure); |
| 375 | } |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
Xinliang David Li | e809231 | 2015-11-25 19:13:00 +0000 | [diff] [blame] | 380 | void ValueProfRecord::deserializeTo(InstrProfRecord &Record, |
| 381 | InstrProfRecord::ValueMapType *VMap) { |
| 382 | Record.reserveSites(Kind, NumValueSites); |
| 383 | |
| 384 | InstrProfValueData *ValueData = getValueProfRecordValueData(this); |
| 385 | for (uint64_t VSite = 0; VSite < NumValueSites; ++VSite) { |
| 386 | uint8_t ValueDataCount = this->SiteCountArray[VSite]; |
| 387 | Record.addValueData(Kind, VSite, ValueData, ValueDataCount, VMap); |
| 388 | ValueData += ValueDataCount; |
| 389 | } |
| 390 | } |
Xinliang David Li | ed96677 | 2015-11-25 23:31:18 +0000 | [diff] [blame] | 391 | |
Xinliang David Li | e809231 | 2015-11-25 19:13:00 +0000 | [diff] [blame] | 392 | // For writing/serializing, Old is the host endianness, and New is |
| 393 | // byte order intended on disk. For Reading/deserialization, Old |
| 394 | // is the on-disk source endianness, and New is the host endianness. |
| 395 | void ValueProfRecord::swapBytes(support::endianness Old, |
| 396 | support::endianness New) { |
| 397 | using namespace support; |
| 398 | if (Old == New) |
| 399 | return; |
| 400 | |
| 401 | if (getHostEndianness() != Old) { |
| 402 | sys::swapByteOrder<uint32_t>(NumValueSites); |
| 403 | sys::swapByteOrder<uint32_t>(Kind); |
| 404 | } |
| 405 | uint32_t ND = getValueProfRecordNumValueData(this); |
| 406 | InstrProfValueData *VD = getValueProfRecordValueData(this); |
| 407 | |
| 408 | // No need to swap byte array: SiteCountArrray. |
| 409 | for (uint32_t I = 0; I < ND; I++) { |
| 410 | sys::swapByteOrder<uint64_t>(VD[I].Value); |
| 411 | sys::swapByteOrder<uint64_t>(VD[I].Count); |
| 412 | } |
| 413 | if (getHostEndianness() == Old) { |
| 414 | sys::swapByteOrder<uint32_t>(NumValueSites); |
| 415 | sys::swapByteOrder<uint32_t>(Kind); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | void ValueProfData::deserializeTo(InstrProfRecord &Record, |
| 420 | InstrProfRecord::ValueMapType *VMap) { |
| 421 | if (NumValueKinds == 0) |
| 422 | return; |
| 423 | |
| 424 | ValueProfRecord *VR = getFirstValueProfRecord(this); |
| 425 | for (uint32_t K = 0; K < NumValueKinds; K++) { |
| 426 | VR->deserializeTo(Record, VMap); |
| 427 | VR = getValueProfRecordNext(VR); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | template <class T> |
| 432 | static T swapToHostOrder(const unsigned char *&D, support::endianness Orig) { |
| 433 | using namespace support; |
| 434 | if (Orig == little) |
| 435 | return endian::readNext<T, little, unaligned>(D); |
| 436 | else |
| 437 | return endian::readNext<T, big, unaligned>(D); |
| 438 | } |
| 439 | |
| 440 | static std::unique_ptr<ValueProfData> allocValueProfData(uint32_t TotalSize) { |
| 441 | return std::unique_ptr<ValueProfData>(new (::operator new(TotalSize)) |
| 442 | ValueProfData()); |
| 443 | } |
| 444 | |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 445 | ErrorOr<std::unique_ptr<ValueProfData>> |
| 446 | ValueProfData::getValueProfData(const unsigned char *D, |
| 447 | const unsigned char *const BufferEnd, |
| 448 | support::endianness Endianness) { |
| 449 | using namespace support; |
| 450 | if (D + sizeof(ValueProfData) > BufferEnd) |
| 451 | return instrprof_error::truncated; |
| 452 | |
Xinliang David Li | b8c3ad1 | 2015-11-17 03:47:21 +0000 | [diff] [blame] | 453 | const unsigned char *Header = D; |
| 454 | uint32_t TotalSize = swapToHostOrder<uint32_t>(Header, Endianness); |
| 455 | uint32_t NumValueKinds = swapToHostOrder<uint32_t>(Header, Endianness); |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 456 | |
| 457 | if (D + TotalSize > BufferEnd) |
| 458 | return instrprof_error::too_large; |
| 459 | if (NumValueKinds > IPVK_Last + 1) |
| 460 | return instrprof_error::malformed; |
| 461 | // Total size needs to be mulltiple of quadword size. |
| 462 | if (TotalSize % sizeof(uint64_t)) |
| 463 | return instrprof_error::malformed; |
| 464 | |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 465 | std::unique_ptr<ValueProfData> VPD = allocValueProfData(TotalSize); |
Xinliang David Li | 4d1bef3 | 2015-11-11 19:31:53 +0000 | [diff] [blame] | 466 | |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 467 | memcpy(VPD.get(), D, TotalSize); |
| 468 | // Byte swap. |
| 469 | VPD->swapBytesToHost(Endianness); |
| 470 | |
Xinliang David Li | ac5b860 | 2015-11-25 04:29:24 +0000 | [diff] [blame] | 471 | // Data integrity check: |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 472 | ValueProfRecord *VR = getFirstValueProfRecord(VPD.get()); |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 473 | for (uint32_t K = 0; K < VPD->NumValueKinds; K++) { |
| 474 | if (VR->Kind > IPVK_Last) |
| 475 | return instrprof_error::malformed; |
Xinliang David Li | ac5b860 | 2015-11-25 04:29:24 +0000 | [diff] [blame] | 476 | VR = getValueProfRecordNext(VR); |
Aaron Ballman | 470b5f1 | 2015-11-11 14:57:28 +0000 | [diff] [blame] | 477 | if ((char *)VR - (char *)VPD.get() > (ptrdiff_t)TotalSize) |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 478 | return instrprof_error::malformed; |
| 479 | } |
| 480 | |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 481 | return std::move(VPD); |
| 482 | } |
| 483 | |
| 484 | void ValueProfData::swapBytesToHost(support::endianness Endianness) { |
| 485 | using namespace support; |
| 486 | if (Endianness == getHostEndianness()) |
| 487 | return; |
| 488 | |
| 489 | sys::swapByteOrder<uint32_t>(TotalSize); |
| 490 | sys::swapByteOrder<uint32_t>(NumValueKinds); |
| 491 | |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 492 | ValueProfRecord *VR = getFirstValueProfRecord(this); |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 493 | for (uint32_t K = 0; K < NumValueKinds; K++) { |
| 494 | VR->swapBytes(Endianness, getHostEndianness()); |
Xinliang David Li | ac5b860 | 2015-11-25 04:29:24 +0000 | [diff] [blame] | 495 | VR = getValueProfRecordNext(VR); |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 496 | } |
| 497 | } |
| 498 | |
| 499 | void ValueProfData::swapBytesFromHost(support::endianness Endianness) { |
| 500 | using namespace support; |
| 501 | if (Endianness == getHostEndianness()) |
| 502 | return; |
| 503 | |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 504 | ValueProfRecord *VR = getFirstValueProfRecord(this); |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 505 | for (uint32_t K = 0; K < NumValueKinds; K++) { |
Xinliang David Li | ac5b860 | 2015-11-25 04:29:24 +0000 | [diff] [blame] | 506 | ValueProfRecord *NVR = getValueProfRecordNext(VR); |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 507 | VR->swapBytes(getHostEndianness(), Endianness); |
| 508 | VR = NVR; |
| 509 | } |
| 510 | sys::swapByteOrder<uint32_t>(TotalSize); |
| 511 | sys::swapByteOrder<uint32_t>(NumValueKinds); |
| 512 | } |
Xinliang David Li | e809231 | 2015-11-25 19:13:00 +0000 | [diff] [blame] | 513 | |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 514 | } |