Instrumentation.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- Transforms/Instrumentation.h - Instrumentation passes ----*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file defines constructor functions for instrumentation passes.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_TRANSFORMS_INSTRUMENTATION_H
  18. #define LLVM_TRANSFORMS_INSTRUMENTATION_H
  19. #include "llvm/ADT/StringRef.h"
  20. #include "llvm/IR/BasicBlock.h"
  21. #include "llvm/IR/DebugInfoMetadata.h"
  22. #include "llvm/IR/Function.h"
  23. #include "llvm/IR/IRBuilder.h"
  24. #include "llvm/IR/Instruction.h"
  25. #include <cassert>
  26. #include <cstdint>
  27. #include <limits>
  28. #include <string>
  29. #include <vector>
  30. namespace llvm {
  31. class Triple;
  32. class OptimizationRemarkEmitter;
  33. class Comdat;
  34. class CallBase;
  35. /// Instrumentation passes often insert conditional checks into entry blocks.
  36. /// Call this function before splitting the entry block to move instructions
  37. /// that must remain in the entry block up before the split point. Static
  38. /// allocas and llvm.localescape calls, for example, must remain in the entry
  39. /// block.
  40. BasicBlock::iterator PrepareToSplitEntryBlock(BasicBlock &BB,
  41. BasicBlock::iterator IP);
  42. // Create a constant for Str so that we can pass it to the run-time lib.
  43. GlobalVariable *createPrivateGlobalForString(Module &M, StringRef Str,
  44. bool AllowMerging,
  45. const char *NamePrefix = "");
  46. // Returns F.getComdat() if it exists.
  47. // Otherwise creates a new comdat, sets F's comdat, and returns it.
  48. // Returns nullptr on failure.
  49. Comdat *getOrCreateFunctionComdat(Function &F, Triple &T);
  50. // Insert GCOV profiling instrumentation
  51. struct GCOVOptions {
  52. static GCOVOptions getDefault();
  53. // Specify whether to emit .gcno files.
  54. bool EmitNotes;
  55. // Specify whether to modify the program to emit .gcda files when run.
  56. bool EmitData;
  57. // A four-byte version string. The meaning of a version string is described in
  58. // gcc's gcov-io.h
  59. char Version[4];
  60. // Add the 'noredzone' attribute to added runtime library calls.
  61. bool NoRedZone;
  62. // Use atomic profile counter increments.
  63. bool Atomic = false;
  64. // Regexes separated by a semi-colon to filter the files to instrument.
  65. std::string Filter;
  66. // Regexes separated by a semi-colon to filter the files to not instrument.
  67. std::string Exclude;
  68. };
  69. // The pgo-specific indirect call promotion function declared below is used by
  70. // the pgo-driven indirect call promotion and sample profile passes. It's a
  71. // wrapper around llvm::promoteCall, et al. that additionally computes !prof
  72. // metadata. We place it in a pgo namespace so it's not confused with the
  73. // generic utilities.
  74. namespace pgo {
  75. // Helper function that transforms CB (either an indirect-call instruction, or
  76. // an invoke instruction , to a conditional call to F. This is like:
  77. // if (Inst.CalledValue == F)
  78. // F(...);
  79. // else
  80. // Inst(...);
  81. // end
  82. // TotalCount is the profile count value that the instruction executes.
  83. // Count is the profile count value that F is the target function.
  84. // These two values are used to update the branch weight.
  85. // If \p AttachProfToDirectCall is true, a prof metadata is attached to the
  86. // new direct call to contain \p Count.
  87. // Returns the promoted direct call instruction.
  88. CallBase &promoteIndirectCall(CallBase &CB, Function *F, uint64_t Count,
  89. uint64_t TotalCount, bool AttachProfToDirectCall,
  90. OptimizationRemarkEmitter *ORE);
  91. } // namespace pgo
  92. /// Options for the frontend instrumentation based profiling pass.
  93. struct InstrProfOptions {
  94. // Add the 'noredzone' attribute to added runtime library calls.
  95. bool NoRedZone = false;
  96. // Do counter register promotion
  97. bool DoCounterPromotion = false;
  98. // Use atomic profile counter increments.
  99. bool Atomic = false;
  100. // Use BFI to guide register promotion
  101. bool UseBFIInPromotion = false;
  102. // Name of the profile file to use as output
  103. std::string InstrProfileOutput;
  104. InstrProfOptions() = default;
  105. };
  106. // Options for sanitizer coverage instrumentation.
  107. struct SanitizerCoverageOptions {
  108. enum Type {
  109. SCK_None = 0,
  110. SCK_Function,
  111. SCK_BB,
  112. SCK_Edge
  113. } CoverageType = SCK_None;
  114. bool IndirectCalls = false;
  115. bool TraceBB = false;
  116. bool TraceCmp = false;
  117. bool TraceDiv = false;
  118. bool TraceGep = false;
  119. bool Use8bitCounters = false;
  120. bool TracePC = false;
  121. bool TracePCGuard = false;
  122. bool Inline8bitCounters = false;
  123. bool InlineBoolFlag = false;
  124. bool PCTable = false;
  125. bool NoPrune = false;
  126. bool StackDepth = false;
  127. bool TraceLoads = false;
  128. bool TraceStores = false;
  129. bool CollectControlFlow = false;
  130. SanitizerCoverageOptions() = default;
  131. };
  132. /// Calculate what to divide by to scale counts.
  133. ///
  134. /// Given the maximum count, calculate a divisor that will scale all the
  135. /// weights to strictly less than std::numeric_limits<uint32_t>::max().
  136. static inline uint64_t calculateCountScale(uint64_t MaxCount) {
  137. return MaxCount < std::numeric_limits<uint32_t>::max()
  138. ? 1
  139. : MaxCount / std::numeric_limits<uint32_t>::max() + 1;
  140. }
  141. /// Scale an individual branch count.
  142. ///
  143. /// Scale a 64-bit weight down to 32-bits using \c Scale.
  144. ///
  145. static inline uint32_t scaleBranchCount(uint64_t Count, uint64_t Scale) {
  146. uint64_t Scaled = Count / Scale;
  147. assert(Scaled <= std::numeric_limits<uint32_t>::max() && "overflow 32-bits");
  148. return Scaled;
  149. }
  150. // Use to ensure the inserted instrumentation has a DebugLocation; if none is
  151. // attached to the source instruction, try to use a DILocation with offset 0
  152. // scoped to surrounding function (if it has a DebugLocation).
  153. //
  154. // Some non-call instructions may be missing debug info, but when inserting
  155. // instrumentation calls, some builds (e.g. LTO) want calls to have debug info
  156. // if the enclosing function does.
  157. struct InstrumentationIRBuilder : IRBuilder<> {
  158. static void ensureDebugInfo(IRBuilder<> &IRB, const Function &F) {
  159. if (IRB.getCurrentDebugLocation())
  160. return;
  161. if (DISubprogram *SP = F.getSubprogram())
  162. IRB.SetCurrentDebugLocation(DILocation::get(SP->getContext(), 0, 0, SP));
  163. }
  164. explicit InstrumentationIRBuilder(Instruction *IP) : IRBuilder<>(IP) {
  165. ensureDebugInfo(*this, *IP->getFunction());
  166. }
  167. };
  168. } // end namespace llvm
  169. #endif // LLVM_TRANSFORMS_INSTRUMENTATION_H
  170. #ifdef __GNUC__
  171. #pragma GCC diagnostic pop
  172. #endif