InstrProfData.inc 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  1. /*===-- InstrProfData.inc - instr profiling runtime structures -*- C++ -*-=== *\
  2. |*
  3. |* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. |* See https://llvm.org/LICENSE.txt for license information.
  5. |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. |*
  7. \*===----------------------------------------------------------------------===*/
  8. /*
  9. * This is the main file that defines all the data structure, signature,
  10. * constant literals that are shared across profiling runtime library,
  11. * compiler (instrumentation), and host tools (reader/writer). The entities
  12. * defined in this file affect the profile runtime ABI, the raw profile format,
  13. * or both.
  14. *
  15. * The file has two identical copies. The primary copy lives in LLVM and
  16. * the other one sits in compiler-rt/lib/profile directory. To make changes
  17. * in this file, first modify the primary copy and copy it over to compiler-rt.
  18. * Testing of any change in this file can start only after the two copies are
  19. * synced up.
  20. *
  21. * The first part of the file includes macros that defines types, names, and
  22. * initializers for the member fields of the core data structures. The field
  23. * declarations for one structure is enabled by defining the field activation
  24. * macro associated with that structure. Only one field activation record
  25. * can be defined at one time and the rest definitions will be filtered out by
  26. * the preprocessor.
  27. *
  28. * Examples of how the template is used to instantiate structure definition:
  29. * 1. To declare a structure:
  30. *
  31. * struct ProfData {
  32. * #define INSTR_PROF_DATA(Type, LLVMType, Name, Initializer) \
  33. * Type Name;
  34. * #include "llvm/ProfileData/InstrProfData.inc"
  35. * };
  36. *
  37. * 2. To construct LLVM type arrays for the struct type:
  38. *
  39. * Type *DataTypes[] = {
  40. * #define INSTR_PROF_DATA(Type, LLVMType, Name, Initializer) \
  41. * LLVMType,
  42. * #include "llvm/ProfileData/InstrProfData.inc"
  43. * };
  44. *
  45. * 4. To construct constant array for the initializers:
  46. * #define INSTR_PROF_DATA(Type, LLVMType, Name, Initializer) \
  47. * Initializer,
  48. * Constant *ConstantVals[] = {
  49. * #include "llvm/ProfileData/InstrProfData.inc"
  50. * };
  51. *
  52. *
  53. * The second part of the file includes definitions all other entities that
  54. * are related to runtime ABI and format. When no field activation macro is
  55. * defined, this file can be included to introduce the definitions.
  56. *
  57. \*===----------------------------------------------------------------------===*/
  58. /* Functions marked with INSTR_PROF_VISIBILITY must have hidden visibility in
  59. * the compiler runtime. */
  60. #ifndef INSTR_PROF_VISIBILITY
  61. #define INSTR_PROF_VISIBILITY
  62. #endif
  63. /* INSTR_PROF_DATA start. */
  64. /* Definition of member fields of the per-function control structure. */
  65. #ifndef INSTR_PROF_DATA
  66. #define INSTR_PROF_DATA(Type, LLVMType, Name, Initializer)
  67. #else
  68. #define INSTR_PROF_DATA_DEFINED
  69. #endif
  70. INSTR_PROF_DATA(const uint64_t, llvm::Type::getInt64Ty(Ctx), NameRef, \
  71. ConstantInt::get(llvm::Type::getInt64Ty(Ctx), \
  72. IndexedInstrProf::ComputeHash(getPGOFuncNameVarInitializer(Inc->getName()))))
  73. INSTR_PROF_DATA(const uint64_t, llvm::Type::getInt64Ty(Ctx), FuncHash, \
  74. ConstantInt::get(llvm::Type::getInt64Ty(Ctx), \
  75. Inc->getHash()->getZExtValue()))
  76. INSTR_PROF_DATA(const IntPtrT, IntPtrTy, CounterPtr, RelativeCounterPtr)
  77. INSTR_PROF_DATA(const IntPtrT, IntPtrTy, BitmapPtr, RelativeBitmapPtr)
  78. /* This is used to map function pointers for the indirect call targets to
  79. * function name hashes during the conversion from raw to merged profile
  80. * data.
  81. */
  82. INSTR_PROF_DATA(const IntPtrT, llvm::PointerType::getUnqual(Ctx), FunctionPointer, \
  83. FunctionAddr)
  84. INSTR_PROF_DATA(IntPtrT, llvm::PointerType::getUnqual(Ctx), Values, \
  85. ValuesPtrExpr)
  86. INSTR_PROF_DATA(const uint32_t, llvm::Type::getInt32Ty(Ctx), NumCounters, \
  87. ConstantInt::get(llvm::Type::getInt32Ty(Ctx), NumCounters))
  88. INSTR_PROF_DATA(const uint16_t, Int16ArrayTy, NumValueSites[IPVK_Last+1], \
  89. ConstantArray::get(Int16ArrayTy, Int16ArrayVals)) \
  90. INSTR_PROF_DATA(const uint32_t, llvm::Type::getInt32Ty(Ctx), NumBitmapBytes, \
  91. ConstantInt::get(llvm::Type::getInt32Ty(Ctx), NumBitmapBytes))
  92. #undef INSTR_PROF_DATA
  93. /* INSTR_PROF_DATA end. */
  94. /* This is an internal data structure used by value profiler. It
  95. * is defined here to allow serialization code sharing by LLVM
  96. * to be used in unit test.
  97. *
  98. * typedef struct ValueProfNode {
  99. * // InstrProfValueData VData;
  100. * uint64_t Value;
  101. * uint64_t Count;
  102. * struct ValueProfNode *Next;
  103. * } ValueProfNode;
  104. */
  105. /* INSTR_PROF_VALUE_NODE start. */
  106. #ifndef INSTR_PROF_VALUE_NODE
  107. #define INSTR_PROF_VALUE_NODE(Type, LLVMType, Name, Initializer)
  108. #else
  109. #define INSTR_PROF_DATA_DEFINED
  110. #endif
  111. INSTR_PROF_VALUE_NODE(uint64_t, llvm::Type::getInt64Ty(Ctx), Value, \
  112. ConstantInt::get(llvm::Type::GetInt64Ty(Ctx), 0))
  113. INSTR_PROF_VALUE_NODE(uint64_t, llvm::Type::getInt64Ty(Ctx), Count, \
  114. ConstantInt::get(llvm::Type::GetInt64Ty(Ctx), 0))
  115. INSTR_PROF_VALUE_NODE(PtrToNodeT, llvm::PointerType::getUnqual(Ctx), Next, \
  116. ConstantInt::get(llvm::Type::GetInt8PtrTy(Ctx), 0))
  117. #undef INSTR_PROF_VALUE_NODE
  118. /* INSTR_PROF_VALUE_NODE end. */
  119. /* INSTR_PROF_RAW_HEADER start */
  120. /* Definition of member fields of the raw profile header data structure. */
  121. /* Please update llvm/docs/InstrProfileFormat.rst as appropriate when updating
  122. raw profile format. */
  123. #ifndef INSTR_PROF_RAW_HEADER
  124. #define INSTR_PROF_RAW_HEADER(Type, Name, Initializer)
  125. #else
  126. #define INSTR_PROF_DATA_DEFINED
  127. #endif
  128. INSTR_PROF_RAW_HEADER(uint64_t, Magic, __llvm_profile_get_magic())
  129. INSTR_PROF_RAW_HEADER(uint64_t, Version, __llvm_profile_get_version())
  130. INSTR_PROF_RAW_HEADER(uint64_t, BinaryIdsSize, __llvm_write_binary_ids(NULL))
  131. INSTR_PROF_RAW_HEADER(uint64_t, NumData, NumData)
  132. INSTR_PROF_RAW_HEADER(uint64_t, PaddingBytesBeforeCounters, PaddingBytesBeforeCounters)
  133. INSTR_PROF_RAW_HEADER(uint64_t, NumCounters, NumCounters)
  134. INSTR_PROF_RAW_HEADER(uint64_t, PaddingBytesAfterCounters, PaddingBytesAfterCounters)
  135. INSTR_PROF_RAW_HEADER(uint64_t, NumBitmapBytes, NumBitmapBytes)
  136. INSTR_PROF_RAW_HEADER(uint64_t, PaddingBytesAfterBitmapBytes, PaddingBytesAfterBitmapBytes)
  137. INSTR_PROF_RAW_HEADER(uint64_t, NamesSize, NamesSize)
  138. INSTR_PROF_RAW_HEADER(uint64_t, CountersDelta,
  139. (uintptr_t)CountersBegin - (uintptr_t)DataBegin)
  140. INSTR_PROF_RAW_HEADER(uint64_t, BitmapDelta,
  141. (uintptr_t)BitmapBegin - (uintptr_t)DataBegin)
  142. INSTR_PROF_RAW_HEADER(uint64_t, NamesDelta, (uintptr_t)NamesBegin)
  143. INSTR_PROF_RAW_HEADER(uint64_t, ValueKindLast, IPVK_Last)
  144. #undef INSTR_PROF_RAW_HEADER
  145. /* INSTR_PROF_RAW_HEADER end */
  146. /* VALUE_PROF_FUNC_PARAM start */
  147. /* Definition of parameter types of the runtime API used to do value profiling
  148. * for a given value site.
  149. */
  150. #ifndef VALUE_PROF_FUNC_PARAM
  151. #define VALUE_PROF_FUNC_PARAM(ArgType, ArgName, ArgLLVMType)
  152. #define INSTR_PROF_COMMA
  153. #else
  154. #define INSTR_PROF_DATA_DEFINED
  155. #define INSTR_PROF_COMMA ,
  156. #endif
  157. VALUE_PROF_FUNC_PARAM(uint64_t, TargetValue, Type::getInt64Ty(Ctx)) \
  158. INSTR_PROF_COMMA
  159. VALUE_PROF_FUNC_PARAM(void *, Data, PointerType::getUnqual(Ctx)) INSTR_PROF_COMMA
  160. VALUE_PROF_FUNC_PARAM(uint32_t, CounterIndex, Type::getInt32Ty(Ctx))
  161. #undef VALUE_PROF_FUNC_PARAM
  162. #undef INSTR_PROF_COMMA
  163. /* VALUE_PROF_FUNC_PARAM end */
  164. /* VALUE_PROF_KIND start */
  165. #ifndef VALUE_PROF_KIND
  166. #define VALUE_PROF_KIND(Enumerator, Value, Descr)
  167. #else
  168. #define INSTR_PROF_DATA_DEFINED
  169. #endif
  170. /* For indirect function call value profiling, the addresses of the target
  171. * functions are profiled by the instrumented code. The target addresses are
  172. * written in the raw profile data and converted to target function name's MD5
  173. * hash by the profile reader during deserialization. Typically, this happens
  174. * when the raw profile data is read during profile merging.
  175. *
  176. * For this remapping the ProfData is used. ProfData contains both the function
  177. * name hash and the function address.
  178. */
  179. VALUE_PROF_KIND(IPVK_IndirectCallTarget, 0, "indirect call target")
  180. /* For memory intrinsic functions size profiling. */
  181. VALUE_PROF_KIND(IPVK_MemOPSize, 1, "memory intrinsic functions size")
  182. /* These two kinds must be the last to be
  183. * declared. This is to make sure the string
  184. * array created with the template can be
  185. * indexed with the kind value.
  186. */
  187. VALUE_PROF_KIND(IPVK_First, IPVK_IndirectCallTarget, "first")
  188. VALUE_PROF_KIND(IPVK_Last, IPVK_MemOPSize, "last")
  189. #undef VALUE_PROF_KIND
  190. /* VALUE_PROF_KIND end */
  191. #undef COVMAP_V2_OR_V3
  192. #ifdef COVMAP_V2
  193. #define COVMAP_V2_OR_V3
  194. #endif
  195. #ifdef COVMAP_V3
  196. #define COVMAP_V2_OR_V3
  197. #endif
  198. /* COVMAP_FUNC_RECORD start */
  199. /* Definition of member fields of the function record structure in coverage
  200. * map.
  201. */
  202. #ifndef COVMAP_FUNC_RECORD
  203. #define COVMAP_FUNC_RECORD(Type, LLVMType, Name, Initializer)
  204. #else
  205. #define INSTR_PROF_DATA_DEFINED
  206. #endif
  207. #ifdef COVMAP_V1
  208. COVMAP_FUNC_RECORD(const IntPtrT, llvm::PointerType::getUnqual(Ctx), \
  209. NamePtr, llvm::ConstantExpr::getBitCast(NamePtr, \
  210. llvm::PointerType::getUnqual(Ctx)))
  211. COVMAP_FUNC_RECORD(const uint32_t, llvm::Type::getInt32Ty(Ctx), NameSize, \
  212. llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), \
  213. NameValue.size()))
  214. #endif
  215. #ifdef COVMAP_V2_OR_V3
  216. COVMAP_FUNC_RECORD(const int64_t, llvm::Type::getInt64Ty(Ctx), NameRef, \
  217. llvm::ConstantInt::get( \
  218. llvm::Type::getInt64Ty(Ctx), NameHash))
  219. #endif
  220. COVMAP_FUNC_RECORD(const uint32_t, llvm::Type::getInt32Ty(Ctx), DataSize, \
  221. llvm::ConstantInt::get( \
  222. llvm::Type::getInt32Ty(Ctx), CoverageMapping.size()))
  223. COVMAP_FUNC_RECORD(const uint64_t, llvm::Type::getInt64Ty(Ctx), FuncHash, \
  224. llvm::ConstantInt::get( \
  225. llvm::Type::getInt64Ty(Ctx), FuncHash))
  226. #ifdef COVMAP_V3
  227. COVMAP_FUNC_RECORD(const uint64_t, llvm::Type::getInt64Ty(Ctx), FilenamesRef, \
  228. llvm::ConstantInt::get( \
  229. llvm::Type::getInt64Ty(Ctx), FilenamesRef))
  230. COVMAP_FUNC_RECORD(const char, \
  231. llvm::ArrayType::get(llvm::Type::getInt8Ty(Ctx), \
  232. CoverageMapping.size()), \
  233. CoverageMapping,
  234. llvm::ConstantDataArray::getRaw( \
  235. CoverageMapping, CoverageMapping.size(), \
  236. llvm::Type::getInt8Ty(Ctx)))
  237. #endif
  238. #undef COVMAP_FUNC_RECORD
  239. /* COVMAP_FUNC_RECORD end. */
  240. /* COVMAP_HEADER start */
  241. /* Definition of member fields of coverage map header.
  242. */
  243. #ifndef COVMAP_HEADER
  244. #define COVMAP_HEADER(Type, LLVMType, Name, Initializer)
  245. #else
  246. #define INSTR_PROF_DATA_DEFINED
  247. #endif
  248. COVMAP_HEADER(uint32_t, Int32Ty, NRecords, \
  249. llvm::ConstantInt::get(Int32Ty, NRecords))
  250. COVMAP_HEADER(uint32_t, Int32Ty, FilenamesSize, \
  251. llvm::ConstantInt::get(Int32Ty, FilenamesSize))
  252. COVMAP_HEADER(uint32_t, Int32Ty, CoverageSize, \
  253. llvm::ConstantInt::get(Int32Ty, CoverageMappingSize))
  254. COVMAP_HEADER(uint32_t, Int32Ty, Version, \
  255. llvm::ConstantInt::get(Int32Ty, CovMapVersion::CurrentVersion))
  256. #undef COVMAP_HEADER
  257. /* COVMAP_HEADER end. */
  258. #ifdef INSTR_PROF_SECT_ENTRY
  259. #define INSTR_PROF_DATA_DEFINED
  260. INSTR_PROF_SECT_ENTRY(IPSK_data, \
  261. INSTR_PROF_QUOTE(INSTR_PROF_DATA_COMMON), \
  262. INSTR_PROF_DATA_COFF, "__DATA,")
  263. INSTR_PROF_SECT_ENTRY(IPSK_cnts, \
  264. INSTR_PROF_QUOTE(INSTR_PROF_CNTS_COMMON), \
  265. INSTR_PROF_CNTS_COFF, "__DATA,")
  266. INSTR_PROF_SECT_ENTRY(IPSK_bitmap, \
  267. INSTR_PROF_QUOTE(INSTR_PROF_BITS_COMMON), \
  268. INSTR_PROF_BITS_COFF, "__DATA,")
  269. INSTR_PROF_SECT_ENTRY(IPSK_name, \
  270. INSTR_PROF_QUOTE(INSTR_PROF_NAME_COMMON), \
  271. INSTR_PROF_NAME_COFF, "__DATA,")
  272. INSTR_PROF_SECT_ENTRY(IPSK_vals, \
  273. INSTR_PROF_QUOTE(INSTR_PROF_VALS_COMMON), \
  274. INSTR_PROF_VALS_COFF, "__DATA,")
  275. INSTR_PROF_SECT_ENTRY(IPSK_vnodes, \
  276. INSTR_PROF_QUOTE(INSTR_PROF_VNODES_COMMON), \
  277. INSTR_PROF_VNODES_COFF, "__DATA,")
  278. INSTR_PROF_SECT_ENTRY(IPSK_covmap, \
  279. INSTR_PROF_QUOTE(INSTR_PROF_COVMAP_COMMON), \
  280. INSTR_PROF_COVMAP_COFF, "__LLVM_COV,")
  281. INSTR_PROF_SECT_ENTRY(IPSK_covfun, \
  282. INSTR_PROF_QUOTE(INSTR_PROF_COVFUN_COMMON), \
  283. INSTR_PROF_COVFUN_COFF, "__LLVM_COV,")
  284. INSTR_PROF_SECT_ENTRY(IPSK_orderfile, \
  285. INSTR_PROF_QUOTE(INSTR_PROF_ORDERFILE_COMMON), \
  286. INSTR_PROF_QUOTE(INSTR_PROF_ORDERFILE_COFF), "__DATA,")
  287. INSTR_PROF_SECT_ENTRY(IPSK_covdata, \
  288. INSTR_PROF_QUOTE(INSTR_PROF_COVDATA_COMMON), \
  289. INSTR_PROF_COVDATA_COFF, "__LLVM_COV,")
  290. INSTR_PROF_SECT_ENTRY(IPSK_covname, \
  291. INSTR_PROF_QUOTE(INSTR_PROF_COVNAME_COMMON), \
  292. INSTR_PROF_COVNAME_COFF, "__LLVM_COV,")
  293. #undef INSTR_PROF_SECT_ENTRY
  294. #endif
  295. #ifdef INSTR_PROF_VALUE_PROF_DATA
  296. #define INSTR_PROF_DATA_DEFINED
  297. #define INSTR_PROF_MAX_NUM_VAL_PER_SITE 255
  298. /*!
  299. * This is the header of the data structure that defines the on-disk
  300. * layout of the value profile data of a particular kind for one function.
  301. */
  302. typedef struct ValueProfRecord {
  303. /* The kind of the value profile record. */
  304. uint32_t Kind;
  305. /*
  306. * The number of value profile sites. It is guaranteed to be non-zero;
  307. * otherwise the record for this kind won't be emitted.
  308. */
  309. uint32_t NumValueSites;
  310. /*
  311. * The first element of the array that stores the number of profiled
  312. * values for each value site. The size of the array is NumValueSites.
  313. * Since NumValueSites is greater than zero, there is at least one
  314. * element in the array.
  315. */
  316. uint8_t SiteCountArray[1];
  317. /*
  318. * The fake declaration is for documentation purpose only.
  319. * Align the start of next field to be on 8 byte boundaries.
  320. uint8_t Padding[X];
  321. */
  322. /* The array of value profile data. The size of the array is the sum
  323. * of all elements in SiteCountArray[].
  324. InstrProfValueData ValueData[];
  325. */
  326. #ifdef __cplusplus
  327. /*!
  328. * Return the number of value sites.
  329. */
  330. uint32_t getNumValueSites() const { return NumValueSites; }
  331. /*!
  332. * Read data from this record and save it to Record.
  333. */
  334. void deserializeTo(InstrProfRecord &Record,
  335. InstrProfSymtab *SymTab);
  336. /*
  337. * In-place byte swap:
  338. * Do byte swap for this instance. \c Old is the original order before
  339. * the swap, and \c New is the New byte order.
  340. */
  341. void swapBytes(llvm::endianness Old, llvm::endianness New);
  342. #endif
  343. } ValueProfRecord;
  344. /*!
  345. * Per-function header/control data structure for value profiling
  346. * data in indexed format.
  347. */
  348. typedef struct ValueProfData {
  349. /*
  350. * Total size in bytes including this field. It must be a multiple
  351. * of sizeof(uint64_t).
  352. */
  353. uint32_t TotalSize;
  354. /*
  355. *The number of value profile kinds that has value profile data.
  356. * In this implementation, a value profile kind is considered to
  357. * have profile data if the number of value profile sites for the
  358. * kind is not zero. More aggressively, the implementation can
  359. * choose to check the actual data value: if none of the value sites
  360. * has any profiled values, the kind can be skipped.
  361. */
  362. uint32_t NumValueKinds;
  363. /*
  364. * Following are a sequence of variable length records. The prefix/header
  365. * of each record is defined by ValueProfRecord type. The number of
  366. * records is NumValueKinds.
  367. * ValueProfRecord Record_1;
  368. * ValueProfRecord Record_N;
  369. */
  370. #if __cplusplus
  371. /*!
  372. * Return the total size in bytes of the on-disk value profile data
  373. * given the data stored in Record.
  374. */
  375. static uint32_t getSize(const InstrProfRecord &Record);
  376. /*!
  377. * Return a pointer to \c ValueProfData instance ready to be streamed.
  378. */
  379. static std::unique_ptr<ValueProfData>
  380. serializeFrom(const InstrProfRecord &Record);
  381. /*!
  382. * Check the integrity of the record.
  383. */
  384. Error checkIntegrity();
  385. /*!
  386. * Return a pointer to \c ValueProfileData instance ready to be read.
  387. * All data in the instance are properly byte swapped. The input
  388. * data is assumed to be in little endian order.
  389. */
  390. static Expected<std::unique_ptr<ValueProfData>>
  391. getValueProfData(const unsigned char *SrcBuffer,
  392. const unsigned char *const SrcBufferEnd,
  393. llvm::endianness SrcDataEndianness);
  394. /*!
  395. * Swap byte order from \c Endianness order to host byte order.
  396. */
  397. void swapBytesToHost(llvm::endianness Endianness);
  398. /*!
  399. * Swap byte order from host byte order to \c Endianness order.
  400. */
  401. void swapBytesFromHost(llvm::endianness Endianness);
  402. /*!
  403. * Return the total size of \c ValueProfileData.
  404. */
  405. uint32_t getSize() const { return TotalSize; }
  406. /*!
  407. * Read data from this data and save it to \c Record.
  408. */
  409. void deserializeTo(InstrProfRecord &Record,
  410. InstrProfSymtab *SymTab);
  411. void operator delete(void *ptr) { ::operator delete(ptr); }
  412. #endif
  413. } ValueProfData;
  414. /*
  415. * The closure is designed to abstact away two types of value profile data:
  416. * - InstrProfRecord which is the primary data structure used to
  417. * represent profile data in host tools (reader, writer, and profile-use)
  418. * - value profile runtime data structure suitable to be used by C
  419. * runtime library.
  420. *
  421. * Both sources of data need to serialize to disk/memory-buffer in common
  422. * format: ValueProfData. The abstraction allows compiler-rt's raw profiler
  423. * writer to share the same format and code with indexed profile writer.
  424. *
  425. * For documentation of the member methods below, refer to corresponding methods
  426. * in class InstrProfRecord.
  427. */
  428. typedef struct ValueProfRecordClosure {
  429. const void *Record;
  430. uint32_t (*GetNumValueKinds)(const void *Record);
  431. uint32_t (*GetNumValueSites)(const void *Record, uint32_t VKind);
  432. uint32_t (*GetNumValueData)(const void *Record, uint32_t VKind);
  433. uint32_t (*GetNumValueDataForSite)(const void *R, uint32_t VK, uint32_t S);
  434. /*
  435. * After extracting the value profile data from the value profile record,
  436. * this method is used to map the in-memory value to on-disk value. If
  437. * the method is null, value will be written out untranslated.
  438. */
  439. uint64_t (*RemapValueData)(uint32_t, uint64_t Value);
  440. void (*GetValueForSite)(const void *R, InstrProfValueData *Dst, uint32_t K,
  441. uint32_t S);
  442. ValueProfData *(*AllocValueProfData)(size_t TotalSizeInBytes);
  443. } ValueProfRecordClosure;
  444. INSTR_PROF_VISIBILITY ValueProfRecord *
  445. getFirstValueProfRecord(ValueProfData *VPD);
  446. INSTR_PROF_VISIBILITY ValueProfRecord *
  447. getValueProfRecordNext(ValueProfRecord *VPR);
  448. INSTR_PROF_VISIBILITY InstrProfValueData *
  449. getValueProfRecordValueData(ValueProfRecord *VPR);
  450. INSTR_PROF_VISIBILITY uint32_t
  451. getValueProfRecordHeaderSize(uint32_t NumValueSites);
  452. #undef INSTR_PROF_VALUE_PROF_DATA
  453. #endif /* INSTR_PROF_VALUE_PROF_DATA */
  454. #ifdef INSTR_PROF_COMMON_API_IMPL
  455. #define INSTR_PROF_DATA_DEFINED
  456. #ifdef __cplusplus
  457. #define INSTR_PROF_INLINE inline
  458. #define INSTR_PROF_NULLPTR nullptr
  459. #else
  460. #define INSTR_PROF_INLINE
  461. #define INSTR_PROF_NULLPTR NULL
  462. #endif
  463. #ifndef offsetof
  464. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  465. #endif
  466. /*!
  467. * Return the \c ValueProfRecord header size including the
  468. * padding bytes.
  469. */
  470. INSTR_PROF_VISIBILITY INSTR_PROF_INLINE
  471. uint32_t getValueProfRecordHeaderSize(uint32_t NumValueSites) {
  472. uint32_t Size = offsetof(ValueProfRecord, SiteCountArray) +
  473. sizeof(uint8_t) * NumValueSites;
  474. /* Round the size to multiple of 8 bytes. */
  475. Size = (Size + 7) & ~7;
  476. return Size;
  477. }
  478. /*!
  479. * Return the total size of the value profile record including the
  480. * header and the value data.
  481. */
  482. INSTR_PROF_VISIBILITY INSTR_PROF_INLINE
  483. uint32_t getValueProfRecordSize(uint32_t NumValueSites,
  484. uint32_t NumValueData) {
  485. return getValueProfRecordHeaderSize(NumValueSites) +
  486. sizeof(InstrProfValueData) * NumValueData;
  487. }
  488. /*!
  489. * Return the pointer to the start of value data array.
  490. */
  491. INSTR_PROF_VISIBILITY INSTR_PROF_INLINE
  492. InstrProfValueData *getValueProfRecordValueData(ValueProfRecord *This) {
  493. return (InstrProfValueData *)((char *)This + getValueProfRecordHeaderSize(
  494. This->NumValueSites));
  495. }
  496. /*!
  497. * Return the total number of value data for \c This record.
  498. */
  499. INSTR_PROF_VISIBILITY INSTR_PROF_INLINE
  500. uint32_t getValueProfRecordNumValueData(ValueProfRecord *This) {
  501. uint32_t NumValueData = 0;
  502. uint32_t I;
  503. for (I = 0; I < This->NumValueSites; I++)
  504. NumValueData += This->SiteCountArray[I];
  505. return NumValueData;
  506. }
  507. /*!
  508. * Use this method to advance to the next \c This \c ValueProfRecord.
  509. */
  510. INSTR_PROF_VISIBILITY INSTR_PROF_INLINE
  511. ValueProfRecord *getValueProfRecordNext(ValueProfRecord *This) {
  512. uint32_t NumValueData = getValueProfRecordNumValueData(This);
  513. return (ValueProfRecord *)((char *)This +
  514. getValueProfRecordSize(This->NumValueSites,
  515. NumValueData));
  516. }
  517. /*!
  518. * Return the first \c ValueProfRecord instance.
  519. */
  520. INSTR_PROF_VISIBILITY INSTR_PROF_INLINE
  521. ValueProfRecord *getFirstValueProfRecord(ValueProfData *This) {
  522. return (ValueProfRecord *)((char *)This + sizeof(ValueProfData));
  523. }
  524. /* Closure based interfaces. */
  525. /*!
  526. * Return the total size in bytes of the on-disk value profile data
  527. * given the data stored in Record.
  528. */
  529. INSTR_PROF_VISIBILITY uint32_t
  530. getValueProfDataSize(ValueProfRecordClosure *Closure) {
  531. uint32_t Kind;
  532. uint32_t TotalSize = sizeof(ValueProfData);
  533. const void *Record = Closure->Record;
  534. for (Kind = IPVK_First; Kind <= IPVK_Last; Kind++) {
  535. uint32_t NumValueSites = Closure->GetNumValueSites(Record, Kind);
  536. if (!NumValueSites)
  537. continue;
  538. TotalSize += getValueProfRecordSize(NumValueSites,
  539. Closure->GetNumValueData(Record, Kind));
  540. }
  541. return TotalSize;
  542. }
  543. /*!
  544. * Extract value profile data of a function for the profile kind \c ValueKind
  545. * from the \c Closure and serialize the data into \c This record instance.
  546. */
  547. INSTR_PROF_VISIBILITY void
  548. serializeValueProfRecordFrom(ValueProfRecord *This,
  549. ValueProfRecordClosure *Closure,
  550. uint32_t ValueKind, uint32_t NumValueSites) {
  551. uint32_t S;
  552. const void *Record = Closure->Record;
  553. This->Kind = ValueKind;
  554. This->NumValueSites = NumValueSites;
  555. InstrProfValueData *DstVD = getValueProfRecordValueData(This);
  556. for (S = 0; S < NumValueSites; S++) {
  557. uint32_t ND = Closure->GetNumValueDataForSite(Record, ValueKind, S);
  558. This->SiteCountArray[S] = ND;
  559. Closure->GetValueForSite(Record, DstVD, ValueKind, S);
  560. DstVD += ND;
  561. }
  562. }
  563. /*!
  564. * Extract value profile data of a function from the \c Closure
  565. * and serialize the data into \c DstData if it is not NULL or heap
  566. * memory allocated by the \c Closure's allocator method. If \c
  567. * DstData is not null, the caller is expected to set the TotalSize
  568. * in DstData.
  569. */
  570. INSTR_PROF_VISIBILITY ValueProfData *
  571. serializeValueProfDataFrom(ValueProfRecordClosure *Closure,
  572. ValueProfData *DstData) {
  573. uint32_t Kind;
  574. uint32_t TotalSize =
  575. DstData ? DstData->TotalSize : getValueProfDataSize(Closure);
  576. ValueProfData *VPD =
  577. DstData ? DstData : Closure->AllocValueProfData(TotalSize);
  578. VPD->TotalSize = TotalSize;
  579. VPD->NumValueKinds = Closure->GetNumValueKinds(Closure->Record);
  580. ValueProfRecord *VR = getFirstValueProfRecord(VPD);
  581. for (Kind = IPVK_First; Kind <= IPVK_Last; Kind++) {
  582. uint32_t NumValueSites = Closure->GetNumValueSites(Closure->Record, Kind);
  583. if (!NumValueSites)
  584. continue;
  585. serializeValueProfRecordFrom(VR, Closure, Kind, NumValueSites);
  586. VR = getValueProfRecordNext(VR);
  587. }
  588. return VPD;
  589. }
  590. #undef INSTR_PROF_COMMON_API_IMPL
  591. #endif /* INSTR_PROF_COMMON_API_IMPL */
  592. /*============================================================================*/
  593. #ifndef INSTR_PROF_DATA_DEFINED
  594. #ifndef INSTR_PROF_DATA_INC
  595. #define INSTR_PROF_DATA_INC
  596. /* Helper macros. */
  597. #define INSTR_PROF_SIMPLE_QUOTE(x) #x
  598. #define INSTR_PROF_QUOTE(x) INSTR_PROF_SIMPLE_QUOTE(x)
  599. #define INSTR_PROF_SIMPLE_CONCAT(x,y) x ## y
  600. #define INSTR_PROF_CONCAT(x,y) INSTR_PROF_SIMPLE_CONCAT(x,y)
  601. /* Magic number to detect file format and endianness.
  602. * Use 255 at one end, since no UTF-8 file can use that character. Avoid 0,
  603. * so that utilities, like strings, don't grab it as a string. 129 is also
  604. * invalid UTF-8, and high enough to be interesting.
  605. * Use "lprofr" in the centre to stand for "LLVM Profile Raw", or "lprofR"
  606. * for 32-bit platforms.
  607. */
  608. #define INSTR_PROF_RAW_MAGIC_64 (uint64_t)255 << 56 | (uint64_t)'l' << 48 | \
  609. (uint64_t)'p' << 40 | (uint64_t)'r' << 32 | (uint64_t)'o' << 24 | \
  610. (uint64_t)'f' << 16 | (uint64_t)'r' << 8 | (uint64_t)129
  611. #define INSTR_PROF_RAW_MAGIC_32 (uint64_t)255 << 56 | (uint64_t)'l' << 48 | \
  612. (uint64_t)'p' << 40 | (uint64_t)'r' << 32 | (uint64_t)'o' << 24 | \
  613. (uint64_t)'f' << 16 | (uint64_t)'R' << 8 | (uint64_t)129
  614. /* Raw profile format version (start from 1). */
  615. #define INSTR_PROF_RAW_VERSION 9
  616. /* Indexed profile format version (start from 1). */
  617. #define INSTR_PROF_INDEX_VERSION 11
  618. /* Coverage mapping format version (start from 0). */
  619. #define INSTR_PROF_COVMAP_VERSION 6
  620. /* Profile version is always of type uint64_t. Reserve the upper 32 bits in the
  621. * version for other variants of profile. We set the 8th most significant bit
  622. * (i.e. bit 56) to 1 to indicate if this is an IR-level instrumentation
  623. * generated profile, and 0 if this is a Clang FE generated profile.
  624. * 1 in bit 57 indicates there are context-sensitive records in the profile.
  625. * The 59th bit indicates whether to use debug info to correlate profiles.
  626. * The 60th bit indicates single byte coverage instrumentation.
  627. * The 61st bit indicates function entry instrumentation only.
  628. * The 62nd bit indicates whether memory profile information is present.
  629. * The 63rd bit indicates if this is a temporal profile.
  630. */
  631. #define VARIANT_MASKS_ALL 0xffffffff00000000ULL
  632. #define GET_VERSION(V) ((V) & ~VARIANT_MASKS_ALL)
  633. #define VARIANT_MASK_IR_PROF (0x1ULL << 56)
  634. #define VARIANT_MASK_CSIR_PROF (0x1ULL << 57)
  635. #define VARIANT_MASK_INSTR_ENTRY (0x1ULL << 58)
  636. #define VARIANT_MASK_DBG_CORRELATE (0x1ULL << 59)
  637. #define VARIANT_MASK_BYTE_COVERAGE (0x1ULL << 60)
  638. #define VARIANT_MASK_FUNCTION_ENTRY_ONLY (0x1ULL << 61)
  639. #define VARIANT_MASK_MEMPROF (0x1ULL << 62)
  640. #define VARIANT_MASK_TEMPORAL_PROF (0x1ULL << 63)
  641. #define INSTR_PROF_RAW_VERSION_VAR __llvm_profile_raw_version
  642. #define INSTR_PROF_PROFILE_RUNTIME_VAR __llvm_profile_runtime
  643. #define INSTR_PROF_PROFILE_COUNTER_BIAS_VAR __llvm_profile_counter_bias
  644. #define INSTR_PROF_PROFILE_SET_TIMESTAMP __llvm_profile_set_timestamp
  645. /* The variable that holds the name of the profile data
  646. * specified via command line. */
  647. #define INSTR_PROF_PROFILE_NAME_VAR __llvm_profile_filename
  648. /* section name strings common to all targets other
  649. than WIN32 */
  650. #define INSTR_PROF_DATA_COMMON __llvm_prf_data
  651. #define INSTR_PROF_NAME_COMMON __llvm_prf_names
  652. #define INSTR_PROF_CNTS_COMMON __llvm_prf_cnts
  653. #define INSTR_PROF_BITS_COMMON __llvm_prf_bits
  654. #define INSTR_PROF_VALS_COMMON __llvm_prf_vals
  655. #define INSTR_PROF_VNODES_COMMON __llvm_prf_vnds
  656. #define INSTR_PROF_COVMAP_COMMON __llvm_covmap
  657. #define INSTR_PROF_COVFUN_COMMON __llvm_covfun
  658. #define INSTR_PROF_COVDATA_COMMON __llvm_covdata
  659. #define INSTR_PROF_COVNAME_COMMON __llvm_covnames
  660. #define INSTR_PROF_ORDERFILE_COMMON __llvm_orderfile
  661. /* Windows section names. Because these section names contain dollar characters,
  662. * they must be quoted.
  663. */
  664. #define INSTR_PROF_DATA_COFF ".lprfd$M"
  665. #define INSTR_PROF_NAME_COFF ".lprfn$M"
  666. #define INSTR_PROF_CNTS_COFF ".lprfc$M"
  667. #define INSTR_PROF_BITS_COFF ".lprfb$M"
  668. #define INSTR_PROF_VALS_COFF ".lprfv$M"
  669. #define INSTR_PROF_VNODES_COFF ".lprfnd$M"
  670. #define INSTR_PROF_COVMAP_COFF ".lcovmap$M"
  671. #define INSTR_PROF_COVFUN_COFF ".lcovfun$M"
  672. /* Since cov data and cov names sections are not allocated, we don't need to
  673. * access them at runtime.
  674. */
  675. #define INSTR_PROF_COVDATA_COFF ".lcovd"
  676. #define INSTR_PROF_COVNAME_COFF ".lcovn"
  677. #define INSTR_PROF_ORDERFILE_COFF ".lorderfile$M"
  678. #ifdef _WIN32
  679. /* Runtime section names and name strings. */
  680. #define INSTR_PROF_DATA_SECT_NAME INSTR_PROF_DATA_COFF
  681. #define INSTR_PROF_NAME_SECT_NAME INSTR_PROF_NAME_COFF
  682. #define INSTR_PROF_CNTS_SECT_NAME INSTR_PROF_CNTS_COFF
  683. #define INSTR_PROF_BITS_SECT_NAME INSTR_PROF_BITS_COFF
  684. /* Array of pointers. Each pointer points to a list
  685. * of value nodes associated with one value site.
  686. */
  687. #define INSTR_PROF_VALS_SECT_NAME INSTR_PROF_VALS_COFF
  688. /* Value profile nodes section. */
  689. #define INSTR_PROF_VNODES_SECT_NAME INSTR_PROF_VNODES_COFF
  690. #define INSTR_PROF_COVMAP_SECT_NAME INSTR_PROF_COVMAP_COFF
  691. #define INSTR_PROF_COVFUN_SECT_NAME INSTR_PROF_COVFUN_COFF
  692. #define INSTR_PROF_COVDATA_SECT_NAME INSTR_PROF_COVDATA_COFF
  693. #define INSTR_PROF_COVNAME_SECT_NAME INSTR_PROF_COVNAME_COFF
  694. #define INSTR_PROF_ORDERFILE_SECT_NAME INSTR_PROF_ORDERFILE_COFF
  695. #else
  696. /* Runtime section names and name strings. */
  697. #define INSTR_PROF_DATA_SECT_NAME INSTR_PROF_QUOTE(INSTR_PROF_DATA_COMMON)
  698. #define INSTR_PROF_NAME_SECT_NAME INSTR_PROF_QUOTE(INSTR_PROF_NAME_COMMON)
  699. #define INSTR_PROF_CNTS_SECT_NAME INSTR_PROF_QUOTE(INSTR_PROF_CNTS_COMMON)
  700. #define INSTR_PROF_BITS_SECT_NAME INSTR_PROF_QUOTE(INSTR_PROF_BITS_COMMON)
  701. /* Array of pointers. Each pointer points to a list
  702. * of value nodes associated with one value site.
  703. */
  704. #define INSTR_PROF_VALS_SECT_NAME INSTR_PROF_QUOTE(INSTR_PROF_VALS_COMMON)
  705. /* Value profile nodes section. */
  706. #define INSTR_PROF_VNODES_SECT_NAME INSTR_PROF_QUOTE(INSTR_PROF_VNODES_COMMON)
  707. #define INSTR_PROF_COVMAP_SECT_NAME INSTR_PROF_QUOTE(INSTR_PROF_COVMAP_COMMON)
  708. #define INSTR_PROF_COVFUN_SECT_NAME INSTR_PROF_QUOTE(INSTR_PROF_COVFUN_COMMON)
  709. #define INSTR_PROF_COVDATA_SECT_NAME INSTR_PROF_QUOTE(INSTR_PROF_COVDATA_COMMON)
  710. #define INSTR_PROF_COVNAME_SECT_NAME INSTR_PROF_QUOTE(INSTR_PROF_COVNAME_COMMON)
  711. /* Order file instrumentation. */
  712. #define INSTR_PROF_ORDERFILE_SECT_NAME \
  713. INSTR_PROF_QUOTE(INSTR_PROF_ORDERFILE_COMMON)
  714. #endif
  715. #define INSTR_PROF_ORDERFILE_BUFFER_NAME _llvm_order_file_buffer
  716. #define INSTR_PROF_ORDERFILE_BUFFER_NAME_STR \
  717. INSTR_PROF_QUOTE(INSTR_PROF_ORDERFILE_BUFFER_NAME)
  718. #define INSTR_PROF_ORDERFILE_BUFFER_IDX_NAME _llvm_order_file_buffer_idx
  719. #define INSTR_PROF_ORDERFILE_BUFFER_IDX_NAME_STR \
  720. INSTR_PROF_QUOTE(INSTR_PROF_ORDERFILE_BUFFER_IDX_NAME)
  721. /* Macros to define start/stop section symbol for a given
  722. * section on Linux. For instance
  723. * INSTR_PROF_SECT_START(INSTR_PROF_DATA_SECT_NAME) will
  724. * expand to __start___llvm_prof_data
  725. */
  726. #define INSTR_PROF_SECT_START(Sect) \
  727. INSTR_PROF_CONCAT(__start_,Sect)
  728. #define INSTR_PROF_SECT_STOP(Sect) \
  729. INSTR_PROF_CONCAT(__stop_,Sect)
  730. /* Value Profiling API linkage name. */
  731. #define INSTR_PROF_VALUE_PROF_FUNC __llvm_profile_instrument_target
  732. #define INSTR_PROF_VALUE_PROF_FUNC_STR \
  733. INSTR_PROF_QUOTE(INSTR_PROF_VALUE_PROF_FUNC)
  734. #define INSTR_PROF_VALUE_PROF_MEMOP_FUNC __llvm_profile_instrument_memop
  735. #define INSTR_PROF_VALUE_PROF_MEMOP_FUNC_STR \
  736. INSTR_PROF_QUOTE(INSTR_PROF_VALUE_PROF_MEMOP_FUNC)
  737. /* InstrProfile per-function control data alignment. */
  738. #define INSTR_PROF_DATA_ALIGNMENT 8
  739. /* The data structure that represents a tracked value by the
  740. * value profiler.
  741. */
  742. typedef struct InstrProfValueData {
  743. /* Profiled value. */
  744. uint64_t Value;
  745. /* Number of times the value appears in the training run. */
  746. uint64_t Count;
  747. } InstrProfValueData;
  748. #endif /* INSTR_PROF_DATA_INC */
  749. #ifndef INSTR_ORDER_FILE_INC
  750. /* The maximal # of functions: 128*1024 (the buffer size will be 128*4 KB). */
  751. #define INSTR_ORDER_FILE_BUFFER_SIZE 131072
  752. #define INSTR_ORDER_FILE_BUFFER_BITS 17
  753. #define INSTR_ORDER_FILE_BUFFER_MASK 0x1ffff
  754. #endif /* INSTR_ORDER_FILE_INC */
  755. #else
  756. #undef INSTR_PROF_DATA_DEFINED
  757. #endif
  758. #undef COVMAP_V2_OR_V3
  759. #ifdef INSTR_PROF_VALUE_PROF_MEMOP_API
  760. #ifdef __cplusplus
  761. #define INSTR_PROF_INLINE inline
  762. #else
  763. #define INSTR_PROF_INLINE
  764. #endif
  765. /* The value range buckets (22 buckets) for the memop size value profiling looks
  766. * like:
  767. *
  768. * [0, 0]
  769. * [1, 1]
  770. * [2, 2]
  771. * [3, 3]
  772. * [4, 4]
  773. * [5, 5]
  774. * [6, 6]
  775. * [7, 7]
  776. * [8, 8]
  777. * [9, 15]
  778. * [16, 16]
  779. * [17, 31]
  780. * [32, 32]
  781. * [33, 63]
  782. * [64, 64]
  783. * [65, 127]
  784. * [128, 128]
  785. * [129, 255]
  786. * [256, 256]
  787. * [257, 511]
  788. * [512, 512]
  789. * [513, UINT64_MAX]
  790. *
  791. * Each range has a 'representative value' which is the lower end value of the
  792. * range and used to store in the runtime profile data records and the VP
  793. * metadata. For example, it's 2 for [2, 2] and 64 for [65, 127].
  794. */
  795. #define INSTR_PROF_NUM_BUCKETS 22
  796. /*
  797. * Clz and Popcount. This code was copied from
  798. * compiler-rt/lib/fuzzer/{FuzzerBuiltins.h,FuzzerBuiltinsMsvc.h} and
  799. * llvm/include/llvm/Support/MathExtras.h.
  800. */
  801. #if defined(_MSC_VER) && !defined(__clang__)
  802. #include <intrin.h>
  803. INSTR_PROF_VISIBILITY INSTR_PROF_INLINE
  804. int InstProfClzll(unsigned long long X) {
  805. unsigned long LeadZeroIdx = 0;
  806. #if !defined(_M_ARM64) && !defined(_M_X64)
  807. // Scan the high 32 bits.
  808. if (_BitScanReverse(&LeadZeroIdx, (unsigned long)(X >> 32)))
  809. return (int)(63 - (LeadZeroIdx + 32)); // Create a bit offset
  810. // from the MSB.
  811. // Scan the low 32 bits.
  812. if (_BitScanReverse(&LeadZeroIdx, (unsigned long)(X)))
  813. return (int)(63 - LeadZeroIdx);
  814. #else
  815. if (_BitScanReverse64(&LeadZeroIdx, X)) return 63 - LeadZeroIdx;
  816. #endif
  817. return 64;
  818. }
  819. INSTR_PROF_VISIBILITY INSTR_PROF_INLINE
  820. int InstProfPopcountll(unsigned long long X) {
  821. // This code originates from https://reviews.llvm.org/rG30626254510f.
  822. unsigned long long v = X;
  823. v = v - ((v >> 1) & 0x5555555555555555ULL);
  824. v = (v & 0x3333333333333333ULL) + ((v >> 2) & 0x3333333333333333ULL);
  825. v = (v + (v >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
  826. return (int)((unsigned long long)(v * 0x0101010101010101ULL) >> 56);
  827. }
  828. #else
  829. INSTR_PROF_VISIBILITY INSTR_PROF_INLINE
  830. int InstProfClzll(unsigned long long X) { return __builtin_clzll(X); }
  831. INSTR_PROF_VISIBILITY INSTR_PROF_INLINE
  832. int InstProfPopcountll(unsigned long long X) { return __builtin_popcountll(X); }
  833. #endif /* defined(_MSC_VER) && !defined(__clang__) */
  834. /* Map an (observed) memop size value to the representative value of its range.
  835. * For example, 5 -> 5, 22 -> 17, 99 -> 65, 256 -> 256, 1001 -> 513. */
  836. INSTR_PROF_VISIBILITY INSTR_PROF_INLINE uint64_t
  837. InstrProfGetRangeRepValue(uint64_t Value) {
  838. if (Value <= 8)
  839. // The first ranges are individually tracked. Use the value as is.
  840. return Value;
  841. else if (Value >= 513)
  842. // The last range is mapped to its lowest value.
  843. return 513;
  844. else if (InstProfPopcountll(Value) == 1)
  845. // If it's a power of two, use it as is.
  846. return Value;
  847. else
  848. // Otherwise, take to the previous power of two + 1.
  849. return (UINT64_C(1) << (64 - InstProfClzll(Value) - 1)) + 1;
  850. }
  851. /* Return true if the range that an (observed) memop size value belongs to has
  852. * only a single value in the range. For example, 0 -> true, 8 -> true, 10 ->
  853. * false, 64 -> true, 100 -> false, 513 -> false. */
  854. INSTR_PROF_VISIBILITY INSTR_PROF_INLINE unsigned
  855. InstrProfIsSingleValRange(uint64_t Value) {
  856. if (Value <= 8)
  857. // The first ranges are individually tracked.
  858. return 1;
  859. else if (InstProfPopcountll(Value) == 1)
  860. // If it's a power of two, there's only one value.
  861. return 1;
  862. else
  863. // Otherwise, there's more than one value in the range.
  864. return 0;
  865. }
  866. #endif /* INSTR_PROF_VALUE_PROF_MEMOP_API */