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 | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 15 | #include "llvm/ProfileData/InstrProf.h" |
Xinliang David Li | 0c67787 | 2016-01-04 21:31:09 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/StringExtras.h" |
Xinliang David Li | 441959d | 2015-11-09 00:01:22 +0000 | [diff] [blame] | 17 | #include "llvm/IR/Constants.h" |
| 18 | #include "llvm/IR/Function.h" |
Xinliang David Li | 441959d | 2015-11-09 00:01:22 +0000 | [diff] [blame] | 19 | #include "llvm/IR/GlobalVariable.h" |
Xinliang David Li | 402477d | 2016-02-04 19:11:43 +0000 | [diff] [blame] | 20 | #include "llvm/IR/MDBuilder.h" |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Module.h" |
| 22 | #include "llvm/Support/Compression.h" |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ErrorHandling.h" |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 24 | #include "llvm/Support/LEB128.h" |
Chris Bieneman | 1efe801 | 2014-09-19 23:19:24 +0000 | [diff] [blame] | 25 | #include "llvm/Support/ManagedStatic.h" |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 26 | |
| 27 | using namespace llvm; |
| 28 | |
| 29 | namespace { |
Rafael Espindola | 25188c9 | 2014-06-12 01:45:43 +0000 | [diff] [blame] | 30 | class InstrProfErrorCategoryType : public std::error_category { |
Rafael Espindola | f5d07fa | 2014-06-10 21:26:47 +0000 | [diff] [blame] | 31 | const char *name() const LLVM_NOEXCEPT override { return "llvm.instrprof"; } |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 32 | std::string message(int IE) const override { |
Rafael Espindola | 92512e8 | 2014-06-03 05:12:33 +0000 | [diff] [blame] | 33 | instrprof_error E = static_cast<instrprof_error>(IE); |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 34 | switch (E) { |
| 35 | case instrprof_error::success: |
| 36 | return "Success"; |
| 37 | case instrprof_error::eof: |
| 38 | return "End of File"; |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 39 | case instrprof_error::unrecognized_format: |
| 40 | return "Unrecognized instrumentation profile encoding format"; |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 41 | case instrprof_error::bad_magic: |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 42 | return "Invalid instrumentation profile data (bad magic)"; |
Duncan P. N. Exon Smith | 531bb48 | 2014-03-21 20:42:28 +0000 | [diff] [blame] | 43 | case instrprof_error::bad_header: |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 44 | return "Invalid instrumentation profile data (file header is corrupt)"; |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 45 | case instrprof_error::unsupported_version: |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 46 | return "Unsupported instrumentation profile format version"; |
Justin Bogner | b7aa263 | 2014-04-18 21:48:40 +0000 | [diff] [blame] | 47 | case instrprof_error::unsupported_hash_type: |
Nathan Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 48 | return "Unsupported instrumentation profile hash type"; |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 49 | 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 Slingerland | 4f82366 | 2015-11-13 03:47:58 +0000 | [diff] [blame] | 54 | return "Malformed instrumentation profile data"; |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 55 | case instrprof_error::unknown_function: |
| 56 | return "No profile data available for function"; |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 57 | case instrprof_error::hash_mismatch: |
Nathan Slingerland | e6e30d5 | 2015-11-17 22:08:53 +0000 | [diff] [blame] | 58 | return "Function control flow change detected (hash mismatch)"; |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 59 | case instrprof_error::count_mismatch: |
Nathan Slingerland | e6e30d5 | 2015-11-17 22:08:53 +0000 | [diff] [blame] | 60 | return "Function basic block count change detected (counter mismatch)"; |
Justin Bogner | b9bd7f8 | 2014-03-21 17:46:22 +0000 | [diff] [blame] | 61 | case instrprof_error::counter_overflow: |
| 62 | return "Counter overflow"; |
Justin Bogner | 9e9a057 | 2015-09-29 22:13:58 +0000 | [diff] [blame] | 63 | case instrprof_error::value_site_count_mismatch: |
Nathan Slingerland | e6e30d5 | 2015-11-17 22:08:53 +0000 | [diff] [blame] | 64 | return "Function value site count change detected (counter mismatch)"; |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 65 | } |
| 66 | llvm_unreachable("A value of instrprof_error has no message."); |
| 67 | } |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 68 | }; |
Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 69 | } // end anonymous namespace |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 70 | |
Chris Bieneman | 1efe801 | 2014-09-19 23:19:24 +0000 | [diff] [blame] | 71 | static ManagedStatic<InstrProfErrorCategoryType> ErrorCategory; |
| 72 | |
Rafael Espindola | 25188c9 | 2014-06-12 01:45:43 +0000 | [diff] [blame] | 73 | const std::error_category &llvm::instrprof_category() { |
Chris Bieneman | 1efe801 | 2014-09-19 23:19:24 +0000 | [diff] [blame] | 74 | return *ErrorCategory; |
Justin Bogner | f8d7919 | 2014-03-21 17:24:48 +0000 | [diff] [blame] | 75 | } |
Xinliang David Li | 441959d | 2015-11-09 00:01:22 +0000 | [diff] [blame] | 76 | |
| 77 | namespace llvm { |
| 78 | |
| 79 | std::string getPGOFuncName(StringRef RawFuncName, |
| 80 | GlobalValue::LinkageTypes Linkage, |
Xinliang David Li | a86545b | 2015-12-11 20:23:22 +0000 | [diff] [blame] | 81 | StringRef FileName, |
| 82 | uint64_t Version LLVM_ATTRIBUTE_UNUSED) { |
Teresa Johnson | b43027d | 2016-03-15 02:13:19 +0000 | [diff] [blame] | 83 | return GlobalValue::getGlobalIdentifier(RawFuncName, Linkage, FileName); |
Xinliang David Li | 441959d | 2015-11-09 00:01:22 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Xinliang David Li | 307902e | 2015-12-05 05:16:36 +0000 | [diff] [blame] | 86 | std::string getPGOFuncName(const Function &F, uint64_t Version) { |
| 87 | return getPGOFuncName(F.getName(), F.getLinkage(), F.getParent()->getName(), |
| 88 | Version); |
Xinliang David Li | 441959d | 2015-11-09 00:01:22 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Xinliang David Li | 4ec4014 | 2015-12-15 19:44:45 +0000 | [diff] [blame] | 91 | StringRef getFuncNameWithoutPrefix(StringRef PGOFuncName, StringRef FileName) { |
| 92 | if (FileName.empty()) |
| 93 | return PGOFuncName; |
| 94 | // 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 Li | d1bab96 | 2015-12-12 17:28:03 +0000 | [diff] [blame] | 100 | // \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 Kumar | aa0cae6 | 2016-03-16 20:49:26 +0000 | [diff] [blame^] | 102 | std::string getPGOFuncNameVarName(StringRef FuncName, |
| 103 | GlobalValue::LinkageTypes Linkage) { |
Xinliang David Li | d1bab96 | 2015-12-12 17:28:03 +0000 | [diff] [blame] | 104 | 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 Li | 441959d | 2015-11-09 00:01:22 +0000 | [diff] [blame] | 120 | GlobalVariable *createPGOFuncNameVar(Module &M, |
| 121 | GlobalValue::LinkageTypes Linkage, |
| 122 | StringRef FuncName) { |
| 123 | |
| 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 | |
| 135 | auto *Value = ConstantDataArray::getString(M.getContext(), FuncName, false); |
| 136 | auto FuncNameVar = |
| 137 | new GlobalVariable(M, Value->getType(), true, Linkage, Value, |
Xinliang David Li | d1bab96 | 2015-12-12 17:28:03 +0000 | [diff] [blame] | 138 | getPGOFuncNameVarName(FuncName, Linkage)); |
Xinliang David Li | 441959d | 2015-11-09 00:01:22 +0000 | [diff] [blame] | 139 | |
| 140 | // Hide the symbol so that we correctly get a copy for each executable. |
| 141 | if (!GlobalValue::isLocalLinkage(FuncNameVar->getLinkage())) |
| 142 | FuncNameVar->setVisibility(GlobalValue::HiddenVisibility); |
| 143 | |
| 144 | return FuncNameVar; |
| 145 | } |
| 146 | |
| 147 | GlobalVariable *createPGOFuncNameVar(Function &F, StringRef FuncName) { |
| 148 | return createPGOFuncNameVar(*F.getParent(), F.getLinkage(), FuncName); |
| 149 | } |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 150 | |
Xinliang David Li | 59411db | 2016-01-20 01:26:34 +0000 | [diff] [blame] | 151 | void InstrProfSymtab::create(const Module &M) { |
| 152 | for (const Function &F : M) |
| 153 | addFuncName(getPGOFuncName(F)); |
| 154 | |
| 155 | finalizeSymtab(); |
| 156 | } |
| 157 | |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 158 | int collectPGOFuncNameStrings(const std::vector<std::string> &NameStrs, |
| 159 | bool doCompression, std::string &Result) { |
| 160 | uint8_t Header[16], *P = Header; |
Xinliang David Li | 0c67787 | 2016-01-04 21:31:09 +0000 | [diff] [blame] | 161 | std::string UncompressedNameStrings = |
| 162 | join(NameStrs.begin(), NameStrs.end(), StringRef(" ")); |
Xinliang David Li | 13ea29b | 2016-01-04 20:26:05 +0000 | [diff] [blame] | 163 | |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 164 | unsigned EncLen = encodeULEB128(UncompressedNameStrings.length(), P); |
| 165 | P += EncLen; |
Xinliang David Li | 120fe2e | 2016-01-04 22:01:02 +0000 | [diff] [blame] | 166 | |
| 167 | auto WriteStringToResult = [&](size_t CompressedLen, |
| 168 | const std::string &InputStr) { |
| 169 | EncLen = encodeULEB128(CompressedLen, P); |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 170 | P += EncLen; |
Xinliang David Li | 120fe2e | 2016-01-04 22:01:02 +0000 | [diff] [blame] | 171 | char *HeaderStr = reinterpret_cast<char *>(&Header[0]); |
| 172 | unsigned HeaderLen = P - &Header[0]; |
| 173 | Result.append(HeaderStr, HeaderLen); |
| 174 | Result += InputStr; |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 175 | return 0; |
Xinliang David Li | 120fe2e | 2016-01-04 22:01:02 +0000 | [diff] [blame] | 176 | }; |
| 177 | |
| 178 | if (!doCompression) |
| 179 | return WriteStringToResult(0, UncompressedNameStrings); |
| 180 | |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 181 | SmallVector<char, 128> CompressedNameStrings; |
| 182 | zlib::Status Success = |
| 183 | zlib::compress(StringRef(UncompressedNameStrings), CompressedNameStrings, |
| 184 | zlib::BestSizeCompression); |
Xinliang David Li | 120fe2e | 2016-01-04 22:01:02 +0000 | [diff] [blame] | 185 | |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 186 | if (Success != zlib::StatusOK) |
| 187 | return 1; |
Xinliang David Li | 120fe2e | 2016-01-04 22:01:02 +0000 | [diff] [blame] | 188 | |
| 189 | return WriteStringToResult( |
| 190 | CompressedNameStrings.size(), |
| 191 | std::string(CompressedNameStrings.data(), CompressedNameStrings.size())); |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Xinliang David Li | eb7d7f8 | 2016-02-04 23:59:09 +0000 | [diff] [blame] | 194 | StringRef getPGOFuncNameVarInitializer(GlobalVariable *NameVar) { |
Xinliang David Li | 37c1fa0 | 2016-01-03 04:38:13 +0000 | [diff] [blame] | 195 | auto *Arr = cast<ConstantDataArray>(NameVar->getInitializer()); |
| 196 | StringRef NameStr = |
| 197 | Arr->isCString() ? Arr->getAsCString() : Arr->getAsString(); |
| 198 | return NameStr; |
| 199 | } |
| 200 | |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 201 | int collectPGOFuncNameStrings(const std::vector<GlobalVariable *> &NameVars, |
Xinliang David Li | 7316375 | 2016-01-26 23:13:00 +0000 | [diff] [blame] | 202 | std::string &Result, bool doCompression) { |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 203 | std::vector<std::string> NameStrs; |
| 204 | for (auto *NameVar : NameVars) { |
Xinliang David Li | eb7d7f8 | 2016-02-04 23:59:09 +0000 | [diff] [blame] | 205 | NameStrs.push_back(getPGOFuncNameVarInitializer(NameVar)); |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 206 | } |
Xinliang David Li | 7316375 | 2016-01-26 23:13:00 +0000 | [diff] [blame] | 207 | return collectPGOFuncNameStrings( |
| 208 | NameStrs, zlib::isAvailable() && doCompression, Result); |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | int readPGOFuncNameStrings(StringRef NameStrings, InstrProfSymtab &Symtab) { |
| 212 | const uint8_t *P = reinterpret_cast<const uint8_t *>(NameStrings.data()); |
| 213 | const uint8_t *EndP = reinterpret_cast<const uint8_t *>(NameStrings.data() + |
| 214 | NameStrings.size()); |
| 215 | while (P < EndP) { |
| 216 | uint32_t N; |
| 217 | uint64_t UncompressedSize = decodeULEB128(P, &N); |
| 218 | P += N; |
| 219 | uint64_t CompressedSize = decodeULEB128(P, &N); |
| 220 | P += N; |
| 221 | bool isCompressed = (CompressedSize != 0); |
| 222 | SmallString<128> UncompressedNameStrings; |
| 223 | StringRef NameStrings; |
| 224 | if (isCompressed) { |
| 225 | StringRef CompressedNameStrings(reinterpret_cast<const char *>(P), |
| 226 | CompressedSize); |
| 227 | if (zlib::uncompress(CompressedNameStrings, UncompressedNameStrings, |
| 228 | UncompressedSize) != zlib::StatusOK) |
| 229 | return 1; |
| 230 | P += CompressedSize; |
| 231 | NameStrings = StringRef(UncompressedNameStrings.data(), |
| 232 | UncompressedNameStrings.size()); |
| 233 | } else { |
| 234 | NameStrings = |
| 235 | StringRef(reinterpret_cast<const char *>(P), UncompressedSize); |
| 236 | P += UncompressedSize; |
| 237 | } |
| 238 | // Now parse the name strings. |
Xinliang David Li | 204efe2 | 2016-01-04 22:09:26 +0000 | [diff] [blame] | 239 | SmallVector<StringRef, 0> Names; |
| 240 | NameStrings.split(Names, ' '); |
| 241 | for (StringRef &Name : Names) |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 242 | Symtab.addFuncName(Name); |
Xinliang David Li | e413f1a | 2015-12-31 07:57:16 +0000 | [diff] [blame] | 243 | |
| 244 | while (P < EndP && *P == 0) |
| 245 | P++; |
| 246 | } |
| 247 | Symtab.finalizeSymtab(); |
| 248 | return 0; |
| 249 | } |
| 250 | |
Xinliang David Li | 51dc04c | 2016-01-08 03:49:59 +0000 | [diff] [blame] | 251 | instrprof_error InstrProfValueSiteRecord::merge(InstrProfValueSiteRecord &Input, |
| 252 | uint64_t Weight) { |
Xinliang David Li | 5c24da5 | 2015-12-20 05:15:45 +0000 | [diff] [blame] | 253 | this->sortByTargetValues(); |
| 254 | Input.sortByTargetValues(); |
| 255 | auto I = ValueData.begin(); |
| 256 | auto IE = ValueData.end(); |
| 257 | instrprof_error Result = instrprof_error::success; |
| 258 | for (auto J = Input.ValueData.begin(), JE = Input.ValueData.end(); J != JE; |
| 259 | ++J) { |
| 260 | while (I != IE && I->Value < J->Value) |
| 261 | ++I; |
| 262 | if (I != IE && I->Value == J->Value) { |
Xinliang David Li | 5c24da5 | 2015-12-20 05:15:45 +0000 | [diff] [blame] | 263 | bool Overflowed; |
Nathan Slingerland | 7bee316 | 2016-01-12 22:34:00 +0000 | [diff] [blame] | 264 | I->Count = SaturatingMultiplyAdd(J->Count, Weight, I->Count, &Overflowed); |
Xinliang David Li | 5c24da5 | 2015-12-20 05:15:45 +0000 | [diff] [blame] | 265 | if (Overflowed) |
| 266 | Result = instrprof_error::counter_overflow; |
| 267 | ++I; |
| 268 | continue; |
| 269 | } |
| 270 | ValueData.insert(I, *J); |
| 271 | } |
| 272 | return Result; |
| 273 | } |
| 274 | |
Xinliang David Li | 51dc04c | 2016-01-08 03:49:59 +0000 | [diff] [blame] | 275 | instrprof_error InstrProfValueSiteRecord::scale(uint64_t Weight) { |
| 276 | instrprof_error Result = instrprof_error::success; |
| 277 | for (auto I = ValueData.begin(), IE = ValueData.end(); I != IE; ++I) { |
| 278 | bool Overflowed; |
| 279 | I->Count = SaturatingMultiply(I->Count, Weight, &Overflowed); |
| 280 | if (Overflowed) |
| 281 | Result = instrprof_error::counter_overflow; |
| 282 | } |
| 283 | return Result; |
| 284 | } |
| 285 | |
Xinliang David Li | 020f22d | 2015-12-18 23:06:37 +0000 | [diff] [blame] | 286 | // Merge Value Profile data from Src record to this record for ValueKind. |
| 287 | // Scale merged value counts by \p Weight. |
| 288 | instrprof_error InstrProfRecord::mergeValueProfData(uint32_t ValueKind, |
| 289 | InstrProfRecord &Src, |
| 290 | uint64_t Weight) { |
| 291 | uint32_t ThisNumValueSites = getNumValueSites(ValueKind); |
| 292 | uint32_t OtherNumValueSites = Src.getNumValueSites(ValueKind); |
| 293 | if (ThisNumValueSites != OtherNumValueSites) |
| 294 | return instrprof_error::value_site_count_mismatch; |
| 295 | std::vector<InstrProfValueSiteRecord> &ThisSiteRecords = |
| 296 | getValueSitesForKind(ValueKind); |
| 297 | std::vector<InstrProfValueSiteRecord> &OtherSiteRecords = |
| 298 | Src.getValueSitesForKind(ValueKind); |
| 299 | instrprof_error Result = instrprof_error::success; |
| 300 | for (uint32_t I = 0; I < ThisNumValueSites; I++) |
Xinliang David Li | 51dc04c | 2016-01-08 03:49:59 +0000 | [diff] [blame] | 301 | MergeResult(Result, ThisSiteRecords[I].merge(OtherSiteRecords[I], Weight)); |
Xinliang David Li | 020f22d | 2015-12-18 23:06:37 +0000 | [diff] [blame] | 302 | return Result; |
| 303 | } |
| 304 | |
| 305 | instrprof_error InstrProfRecord::merge(InstrProfRecord &Other, |
| 306 | uint64_t Weight) { |
| 307 | // If the number of counters doesn't match we either have bad data |
| 308 | // or a hash collision. |
| 309 | if (Counts.size() != Other.Counts.size()) |
| 310 | return instrprof_error::count_mismatch; |
| 311 | |
| 312 | instrprof_error Result = instrprof_error::success; |
| 313 | |
| 314 | for (size_t I = 0, E = Other.Counts.size(); I < E; ++I) { |
| 315 | bool Overflowed; |
Nathan Slingerland | 7bee316 | 2016-01-12 22:34:00 +0000 | [diff] [blame] | 316 | Counts[I] = |
| 317 | SaturatingMultiplyAdd(Other.Counts[I], Weight, Counts[I], &Overflowed); |
Xinliang David Li | 020f22d | 2015-12-18 23:06:37 +0000 | [diff] [blame] | 318 | if (Overflowed) |
| 319 | Result = instrprof_error::counter_overflow; |
| 320 | } |
| 321 | |
| 322 | for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind) |
| 323 | MergeResult(Result, mergeValueProfData(Kind, Other, Weight)); |
| 324 | |
| 325 | return Result; |
| 326 | } |
Xinliang David Li | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 327 | |
Xinliang David Li | 51dc04c | 2016-01-08 03:49:59 +0000 | [diff] [blame] | 328 | instrprof_error InstrProfRecord::scaleValueProfData(uint32_t ValueKind, |
| 329 | uint64_t Weight) { |
| 330 | uint32_t ThisNumValueSites = getNumValueSites(ValueKind); |
| 331 | std::vector<InstrProfValueSiteRecord> &ThisSiteRecords = |
| 332 | getValueSitesForKind(ValueKind); |
| 333 | instrprof_error Result = instrprof_error::success; |
| 334 | for (uint32_t I = 0; I < ThisNumValueSites; I++) |
| 335 | MergeResult(Result, ThisSiteRecords[I].scale(Weight)); |
| 336 | return Result; |
| 337 | } |
| 338 | |
| 339 | instrprof_error InstrProfRecord::scale(uint64_t Weight) { |
| 340 | instrprof_error Result = instrprof_error::success; |
| 341 | for (auto &Count : this->Counts) { |
| 342 | bool Overflowed; |
| 343 | Count = SaturatingMultiply(Count, Weight, &Overflowed); |
| 344 | if (Overflowed && Result == instrprof_error::success) { |
| 345 | Result = instrprof_error::counter_overflow; |
| 346 | } |
| 347 | } |
| 348 | for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind) |
| 349 | MergeResult(Result, scaleValueProfData(Kind, Weight)); |
| 350 | |
| 351 | return Result; |
| 352 | } |
| 353 | |
Xinliang David Li | 020f22d | 2015-12-18 23:06:37 +0000 | [diff] [blame] | 354 | // Map indirect call target name hash to name string. |
| 355 | uint64_t InstrProfRecord::remapValue(uint64_t Value, uint32_t ValueKind, |
Xinliang David Li | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 356 | ValueMapType *ValueMap) { |
| 357 | if (!ValueMap) |
Xinliang David Li | 020f22d | 2015-12-18 23:06:37 +0000 | [diff] [blame] | 358 | return Value; |
| 359 | switch (ValueKind) { |
| 360 | case IPVK_IndirectCallTarget: { |
| 361 | auto Result = |
Xinliang David Li | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 362 | std::lower_bound(ValueMap->begin(), ValueMap->end(), Value, |
| 363 | [](const std::pair<uint64_t, uint64_t> &LHS, |
Xinliang David Li | 020f22d | 2015-12-18 23:06:37 +0000 | [diff] [blame] | 364 | uint64_t RHS) { return LHS.first < RHS; }); |
Xinliang David Li | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 365 | if (Result != ValueMap->end()) |
Xinliang David Li | 020f22d | 2015-12-18 23:06:37 +0000 | [diff] [blame] | 366 | Value = (uint64_t)Result->second; |
| 367 | break; |
| 368 | } |
| 369 | } |
| 370 | return Value; |
| 371 | } |
| 372 | |
Xinliang David Li | 020f22d | 2015-12-18 23:06:37 +0000 | [diff] [blame] | 373 | void InstrProfRecord::addValueData(uint32_t ValueKind, uint32_t Site, |
| 374 | InstrProfValueData *VData, uint32_t N, |
Xinliang David Li | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 375 | ValueMapType *ValueMap) { |
Xinliang David Li | 020f22d | 2015-12-18 23:06:37 +0000 | [diff] [blame] | 376 | for (uint32_t I = 0; I < N; I++) { |
Xinliang David Li | a716cc5 | 2015-12-20 06:22:13 +0000 | [diff] [blame] | 377 | VData[I].Value = remapValue(VData[I].Value, ValueKind, ValueMap); |
Xinliang David Li | 020f22d | 2015-12-18 23:06:37 +0000 | [diff] [blame] | 378 | } |
| 379 | std::vector<InstrProfValueSiteRecord> &ValueSites = |
| 380 | getValueSitesForKind(ValueKind); |
| 381 | if (N == 0) |
| 382 | ValueSites.push_back(InstrProfValueSiteRecord()); |
| 383 | else |
| 384 | ValueSites.emplace_back(VData, VData + N); |
| 385 | } |
| 386 | |
Xinliang David Li | b75544a | 2015-11-28 19:07:09 +0000 | [diff] [blame] | 387 | #define INSTR_PROF_COMMON_API_IMPL |
| 388 | #include "llvm/ProfileData/InstrProfData.inc" |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 389 | |
Xinliang David Li | 020f22d | 2015-12-18 23:06:37 +0000 | [diff] [blame] | 390 | /*! |
Xinliang David Li | b75544a | 2015-11-28 19:07:09 +0000 | [diff] [blame] | 391 | * \brief ValueProfRecordClosure Interface implementation for InstrProfRecord |
Xinliang David Li | ed96677 | 2015-11-25 23:31:18 +0000 | [diff] [blame] | 392 | * class. These C wrappers are used as adaptors so that C++ code can be |
| 393 | * invoked as callbacks. |
| 394 | */ |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 395 | uint32_t getNumValueKindsInstrProf(const void *Record) { |
| 396 | return reinterpret_cast<const InstrProfRecord *>(Record)->getNumValueKinds(); |
| 397 | } |
| 398 | |
| 399 | uint32_t getNumValueSitesInstrProf(const void *Record, uint32_t VKind) { |
| 400 | return reinterpret_cast<const InstrProfRecord *>(Record) |
| 401 | ->getNumValueSites(VKind); |
| 402 | } |
| 403 | |
| 404 | uint32_t getNumValueDataInstrProf(const void *Record, uint32_t VKind) { |
| 405 | return reinterpret_cast<const InstrProfRecord *>(Record) |
| 406 | ->getNumValueData(VKind); |
| 407 | } |
| 408 | |
| 409 | uint32_t getNumValueDataForSiteInstrProf(const void *R, uint32_t VK, |
| 410 | uint32_t S) { |
| 411 | return reinterpret_cast<const InstrProfRecord *>(R) |
| 412 | ->getNumValueDataForSite(VK, S); |
| 413 | } |
| 414 | |
| 415 | void getValueForSiteInstrProf(const void *R, InstrProfValueData *Dst, |
Xinliang David Li | 1e4c809 | 2016-02-04 05:29:51 +0000 | [diff] [blame] | 416 | uint32_t K, uint32_t S) { |
| 417 | reinterpret_cast<const InstrProfRecord *>(R)->getValueForSite(Dst, K, S); |
| 418 | return; |
Xinliang David Li | e809231 | 2015-11-25 19:13:00 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 421 | ValueProfData *allocValueProfDataInstrProf(size_t TotalSizeInBytes) { |
Xinliang David Li | 38b9a32 | 2015-12-15 21:57:08 +0000 | [diff] [blame] | 422 | ValueProfData *VD = |
| 423 | (ValueProfData *)(new (::operator new(TotalSizeInBytes)) ValueProfData()); |
| 424 | memset(VD, 0, TotalSizeInBytes); |
| 425 | return VD; |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | static ValueProfRecordClosure InstrProfRecordClosure = { |
Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 429 | nullptr, |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 430 | getNumValueKindsInstrProf, |
| 431 | getNumValueSitesInstrProf, |
| 432 | getNumValueDataInstrProf, |
| 433 | getNumValueDataForSiteInstrProf, |
Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 434 | nullptr, |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 435 | getValueForSiteInstrProf, |
Xinliang David Li | 38b9a32 | 2015-12-15 21:57:08 +0000 | [diff] [blame] | 436 | allocValueProfDataInstrProf}; |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 437 | |
Xinliang David Li | e809231 | 2015-11-25 19:13:00 +0000 | [diff] [blame] | 438 | // Wrapper implementation using the closure mechanism. |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 439 | uint32_t ValueProfData::getSize(const InstrProfRecord &Record) { |
| 440 | InstrProfRecordClosure.Record = &Record; |
| 441 | return getValueProfDataSize(&InstrProfRecordClosure); |
| 442 | } |
| 443 | |
Xinliang David Li | e809231 | 2015-11-25 19:13:00 +0000 | [diff] [blame] | 444 | // Wrapper implementation using the closure mechanism. |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 445 | std::unique_ptr<ValueProfData> |
| 446 | ValueProfData::serializeFrom(const InstrProfRecord &Record) { |
| 447 | InstrProfRecordClosure.Record = &Record; |
| 448 | |
| 449 | std::unique_ptr<ValueProfData> VPD( |
Xinliang David Li | 0e6a36e | 2015-12-01 19:47:32 +0000 | [diff] [blame] | 450 | serializeValueProfDataFrom(&InstrProfRecordClosure, nullptr)); |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 451 | return VPD; |
| 452 | } |
| 453 | |
Xinliang David Li | e809231 | 2015-11-25 19:13:00 +0000 | [diff] [blame] | 454 | void ValueProfRecord::deserializeTo(InstrProfRecord &Record, |
| 455 | InstrProfRecord::ValueMapType *VMap) { |
| 456 | Record.reserveSites(Kind, NumValueSites); |
| 457 | |
| 458 | InstrProfValueData *ValueData = getValueProfRecordValueData(this); |
| 459 | for (uint64_t VSite = 0; VSite < NumValueSites; ++VSite) { |
| 460 | uint8_t ValueDataCount = this->SiteCountArray[VSite]; |
| 461 | Record.addValueData(Kind, VSite, ValueData, ValueDataCount, VMap); |
| 462 | ValueData += ValueDataCount; |
| 463 | } |
| 464 | } |
Xinliang David Li | ed96677 | 2015-11-25 23:31:18 +0000 | [diff] [blame] | 465 | |
Xinliang David Li | e809231 | 2015-11-25 19:13:00 +0000 | [diff] [blame] | 466 | // For writing/serializing, Old is the host endianness, and New is |
| 467 | // byte order intended on disk. For Reading/deserialization, Old |
| 468 | // is the on-disk source endianness, and New is the host endianness. |
| 469 | void ValueProfRecord::swapBytes(support::endianness Old, |
| 470 | support::endianness New) { |
| 471 | using namespace support; |
| 472 | if (Old == New) |
| 473 | return; |
| 474 | |
| 475 | if (getHostEndianness() != Old) { |
| 476 | sys::swapByteOrder<uint32_t>(NumValueSites); |
| 477 | sys::swapByteOrder<uint32_t>(Kind); |
| 478 | } |
| 479 | uint32_t ND = getValueProfRecordNumValueData(this); |
| 480 | InstrProfValueData *VD = getValueProfRecordValueData(this); |
| 481 | |
| 482 | // No need to swap byte array: SiteCountArrray. |
| 483 | for (uint32_t I = 0; I < ND; I++) { |
| 484 | sys::swapByteOrder<uint64_t>(VD[I].Value); |
| 485 | sys::swapByteOrder<uint64_t>(VD[I].Count); |
| 486 | } |
| 487 | if (getHostEndianness() == Old) { |
| 488 | sys::swapByteOrder<uint32_t>(NumValueSites); |
| 489 | sys::swapByteOrder<uint32_t>(Kind); |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | void ValueProfData::deserializeTo(InstrProfRecord &Record, |
| 494 | InstrProfRecord::ValueMapType *VMap) { |
| 495 | if (NumValueKinds == 0) |
| 496 | return; |
| 497 | |
| 498 | ValueProfRecord *VR = getFirstValueProfRecord(this); |
| 499 | for (uint32_t K = 0; K < NumValueKinds; K++) { |
| 500 | VR->deserializeTo(Record, VMap); |
| 501 | VR = getValueProfRecordNext(VR); |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | template <class T> |
| 506 | static T swapToHostOrder(const unsigned char *&D, support::endianness Orig) { |
| 507 | using namespace support; |
| 508 | if (Orig == little) |
| 509 | return endian::readNext<T, little, unaligned>(D); |
| 510 | else |
| 511 | return endian::readNext<T, big, unaligned>(D); |
| 512 | } |
| 513 | |
| 514 | static std::unique_ptr<ValueProfData> allocValueProfData(uint32_t TotalSize) { |
| 515 | return std::unique_ptr<ValueProfData>(new (::operator new(TotalSize)) |
| 516 | ValueProfData()); |
| 517 | } |
| 518 | |
Xinliang David Li | 8e32f4d | 2015-11-28 04:56:07 +0000 | [diff] [blame] | 519 | instrprof_error ValueProfData::checkIntegrity() { |
| 520 | if (NumValueKinds > IPVK_Last + 1) |
| 521 | return instrprof_error::malformed; |
| 522 | // Total size needs to be mulltiple of quadword size. |
| 523 | if (TotalSize % sizeof(uint64_t)) |
| 524 | return instrprof_error::malformed; |
| 525 | |
| 526 | ValueProfRecord *VR = getFirstValueProfRecord(this); |
| 527 | for (uint32_t K = 0; K < this->NumValueKinds; K++) { |
| 528 | if (VR->Kind > IPVK_Last) |
| 529 | return instrprof_error::malformed; |
| 530 | VR = getValueProfRecordNext(VR); |
| 531 | if ((char *)VR - (char *)this > (ptrdiff_t)TotalSize) |
| 532 | return instrprof_error::malformed; |
| 533 | } |
| 534 | return instrprof_error::success; |
| 535 | } |
| 536 | |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 537 | ErrorOr<std::unique_ptr<ValueProfData>> |
| 538 | ValueProfData::getValueProfData(const unsigned char *D, |
| 539 | const unsigned char *const BufferEnd, |
| 540 | support::endianness Endianness) { |
| 541 | using namespace support; |
| 542 | if (D + sizeof(ValueProfData) > BufferEnd) |
| 543 | return instrprof_error::truncated; |
| 544 | |
Xinliang David Li | b8c3ad1 | 2015-11-17 03:47:21 +0000 | [diff] [blame] | 545 | const unsigned char *Header = D; |
| 546 | uint32_t TotalSize = swapToHostOrder<uint32_t>(Header, Endianness); |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 547 | if (D + TotalSize > BufferEnd) |
| 548 | return instrprof_error::too_large; |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 549 | |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 550 | std::unique_ptr<ValueProfData> VPD = allocValueProfData(TotalSize); |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 551 | memcpy(VPD.get(), D, TotalSize); |
| 552 | // Byte swap. |
| 553 | VPD->swapBytesToHost(Endianness); |
| 554 | |
Xinliang David Li | 8e32f4d | 2015-11-28 04:56:07 +0000 | [diff] [blame] | 555 | instrprof_error EC = VPD->checkIntegrity(); |
| 556 | if (EC != instrprof_error::success) |
| 557 | return EC; |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 558 | |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 559 | return std::move(VPD); |
| 560 | } |
| 561 | |
| 562 | void ValueProfData::swapBytesToHost(support::endianness Endianness) { |
| 563 | using namespace support; |
| 564 | if (Endianness == getHostEndianness()) |
| 565 | return; |
| 566 | |
| 567 | sys::swapByteOrder<uint32_t>(TotalSize); |
| 568 | sys::swapByteOrder<uint32_t>(NumValueKinds); |
| 569 | |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 570 | ValueProfRecord *VR = getFirstValueProfRecord(this); |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 571 | for (uint32_t K = 0; K < NumValueKinds; K++) { |
| 572 | VR->swapBytes(Endianness, getHostEndianness()); |
Xinliang David Li | ac5b860 | 2015-11-25 04:29:24 +0000 | [diff] [blame] | 573 | VR = getValueProfRecordNext(VR); |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 574 | } |
| 575 | } |
| 576 | |
| 577 | void ValueProfData::swapBytesFromHost(support::endianness Endianness) { |
| 578 | using namespace support; |
| 579 | if (Endianness == getHostEndianness()) |
| 580 | return; |
| 581 | |
Xinliang David Li | f47cf55 | 2015-11-25 06:23:38 +0000 | [diff] [blame] | 582 | ValueProfRecord *VR = getFirstValueProfRecord(this); |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 583 | for (uint32_t K = 0; K < NumValueKinds; K++) { |
Xinliang David Li | ac5b860 | 2015-11-25 04:29:24 +0000 | [diff] [blame] | 584 | ValueProfRecord *NVR = getValueProfRecordNext(VR); |
Xinliang David Li | ee41589 | 2015-11-10 00:24:45 +0000 | [diff] [blame] | 585 | VR->swapBytes(getHostEndianness(), Endianness); |
| 586 | VR = NVR; |
| 587 | } |
| 588 | sys::swapByteOrder<uint32_t>(TotalSize); |
| 589 | sys::swapByteOrder<uint32_t>(NumValueKinds); |
| 590 | } |
Xinliang David Li | e809231 | 2015-11-25 19:13:00 +0000 | [diff] [blame] | 591 | |
Xinliang David Li | 402477d | 2016-02-04 19:11:43 +0000 | [diff] [blame] | 592 | void annotateValueSite(Module &M, Instruction &Inst, |
| 593 | const InstrProfRecord &InstrProfR, |
Rong Xu | 69683f1 | 2016-02-10 22:19:43 +0000 | [diff] [blame] | 594 | InstrProfValueKind ValueKind, uint32_t SiteIdx, |
| 595 | uint32_t MaxMDCount) { |
Xinliang David Li | 402477d | 2016-02-04 19:11:43 +0000 | [diff] [blame] | 596 | uint32_t NV = InstrProfR.getNumValueDataForSite(ValueKind, SiteIdx); |
| 597 | |
| 598 | uint64_t Sum = 0; |
| 599 | std::unique_ptr<InstrProfValueData[]> VD = |
| 600 | InstrProfR.getValueForSite(ValueKind, SiteIdx, &Sum); |
| 601 | |
Rong Xu | bb49490 | 2016-02-12 21:36:17 +0000 | [diff] [blame] | 602 | annotateValueSite(M, Inst, VD.get(), NV, Sum, ValueKind, MaxMDCount); |
| 603 | } |
| 604 | |
| 605 | void annotateValueSite(Module &M, Instruction &Inst, |
| 606 | const InstrProfValueData VD[], uint32_t NV, |
| 607 | uint64_t Sum, InstrProfValueKind ValueKind, |
| 608 | uint32_t MaxMDCount) { |
Xinliang David Li | 402477d | 2016-02-04 19:11:43 +0000 | [diff] [blame] | 609 | LLVMContext &Ctx = M.getContext(); |
| 610 | MDBuilder MDHelper(Ctx); |
| 611 | SmallVector<Metadata *, 3> Vals; |
| 612 | // Tag |
| 613 | Vals.push_back(MDHelper.createString("VP")); |
| 614 | // Value Kind |
| 615 | Vals.push_back(MDHelper.createConstant( |
| 616 | ConstantInt::get(Type::getInt32Ty(Ctx), ValueKind))); |
| 617 | // Total Count |
| 618 | Vals.push_back( |
| 619 | MDHelper.createConstant(ConstantInt::get(Type::getInt64Ty(Ctx), Sum))); |
| 620 | |
| 621 | // Value Profile Data |
Rong Xu | 69683f1 | 2016-02-10 22:19:43 +0000 | [diff] [blame] | 622 | uint32_t MDCount = MaxMDCount; |
Xinliang David Li | 402477d | 2016-02-04 19:11:43 +0000 | [diff] [blame] | 623 | for (uint32_t I = 0; I < NV; ++I) { |
| 624 | Vals.push_back(MDHelper.createConstant( |
| 625 | ConstantInt::get(Type::getInt64Ty(Ctx), VD[I].Value))); |
| 626 | Vals.push_back(MDHelper.createConstant( |
| 627 | ConstantInt::get(Type::getInt64Ty(Ctx), VD[I].Count))); |
| 628 | if (--MDCount == 0) |
| 629 | break; |
| 630 | } |
| 631 | Inst.setMetadata(LLVMContext::MD_prof, MDNode::get(Ctx, Vals)); |
| 632 | } |
| 633 | |
| 634 | bool getValueProfDataFromInst(const Instruction &Inst, |
| 635 | InstrProfValueKind ValueKind, |
| 636 | uint32_t MaxNumValueData, |
| 637 | InstrProfValueData ValueData[], |
| 638 | uint32_t &ActualNumValueData, uint64_t &TotalC) { |
| 639 | MDNode *MD = Inst.getMetadata(LLVMContext::MD_prof); |
| 640 | if (!MD) |
| 641 | return false; |
| 642 | |
| 643 | unsigned NOps = MD->getNumOperands(); |
| 644 | |
| 645 | if (NOps < 5) |
| 646 | return false; |
| 647 | |
| 648 | // Operand 0 is a string tag "VP": |
| 649 | MDString *Tag = cast<MDString>(MD->getOperand(0)); |
| 650 | if (!Tag) |
| 651 | return false; |
| 652 | |
| 653 | if (!Tag->getString().equals("VP")) |
| 654 | return false; |
| 655 | |
| 656 | // Now check kind: |
| 657 | ConstantInt *KindInt = mdconst::dyn_extract<ConstantInt>(MD->getOperand(1)); |
| 658 | if (!KindInt) |
| 659 | return false; |
| 660 | if (KindInt->getZExtValue() != ValueKind) |
| 661 | return false; |
| 662 | |
| 663 | // Get total count |
| 664 | ConstantInt *TotalCInt = mdconst::dyn_extract<ConstantInt>(MD->getOperand(2)); |
| 665 | if (!TotalCInt) |
| 666 | return false; |
| 667 | TotalC = TotalCInt->getZExtValue(); |
| 668 | |
| 669 | ActualNumValueData = 0; |
| 670 | |
| 671 | for (unsigned I = 3; I < NOps; I += 2) { |
| 672 | if (ActualNumValueData >= MaxNumValueData) |
| 673 | break; |
| 674 | ConstantInt *Value = mdconst::dyn_extract<ConstantInt>(MD->getOperand(I)); |
| 675 | ConstantInt *Count = |
| 676 | mdconst::dyn_extract<ConstantInt>(MD->getOperand(I + 1)); |
| 677 | if (!Value || !Count) |
| 678 | return false; |
| 679 | ValueData[ActualNumValueData].Value = Value->getZExtValue(); |
| 680 | ValueData[ActualNumValueData].Count = Count->getZExtValue(); |
| 681 | ActualNumValueData++; |
| 682 | } |
| 683 | return true; |
| 684 | } |
Eugene Zelenko | 6ac3f73 | 2016-01-26 18:48:36 +0000 | [diff] [blame] | 685 | } // end namespace llvm |