Instrumentation.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. //===- Transforms/Instrumentation.h - Instrumentation passes ----*- 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 file defines constructor functions for instrumentation passes.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #ifndef LLVM_TRANSFORMS_INSTRUMENTATION_H
  13. #define LLVM_TRANSFORMS_INSTRUMENTATION_H
  14. #include "llvm/ADT/StringRef.h"
  15. #include "llvm/IR/BasicBlock.h"
  16. #include <cassert>
  17. #include <cstdint>
  18. #include <limits>
  19. #include <string>
  20. #include <vector>
  21. namespace llvm {
  22. class Triple;
  23. class FunctionPass;
  24. class ModulePass;
  25. class OptimizationRemarkEmitter;
  26. class Comdat;
  27. class CallBase;
  28. /// Instrumentation passes often insert conditional checks into entry blocks.
  29. /// Call this function before splitting the entry block to move instructions
  30. /// that must remain in the entry block up before the split point. Static
  31. /// allocas and llvm.localescape calls, for example, must remain in the entry
  32. /// block.
  33. BasicBlock::iterator PrepareToSplitEntryBlock(BasicBlock &BB,
  34. BasicBlock::iterator IP);
  35. // Create a constant for Str so that we can pass it to the run-time lib.
  36. GlobalVariable *createPrivateGlobalForString(Module &M, StringRef Str,
  37. bool AllowMerging,
  38. const char *NamePrefix = "");
  39. // Returns F.getComdat() if it exists.
  40. // Otherwise creates a new comdat, sets F's comdat, and returns it.
  41. // Returns nullptr on failure.
  42. Comdat *getOrCreateFunctionComdat(Function &F, Triple &T);
  43. // Insert GCOV profiling instrumentation
  44. struct GCOVOptions {
  45. static GCOVOptions getDefault();
  46. // Specify whether to emit .gcno files.
  47. bool EmitNotes;
  48. // Specify whether to modify the program to emit .gcda files when run.
  49. bool EmitData;
  50. // A four-byte version string. The meaning of a version string is described in
  51. // gcc's gcov-io.h
  52. char Version[4];
  53. // Add the 'noredzone' attribute to added runtime library calls.
  54. bool NoRedZone;
  55. // Use atomic profile counter increments.
  56. bool Atomic = false;
  57. // Regexes separated by a semi-colon to filter the files to instrument.
  58. std::string Filter;
  59. // Regexes separated by a semi-colon to filter the files to not instrument.
  60. std::string Exclude;
  61. };
  62. ModulePass *createGCOVProfilerPass(const GCOVOptions &Options =
  63. GCOVOptions::getDefault());
  64. // PGO Instrumention. Parameter IsCS indicates if this is the context senstive
  65. // instrumentation.
  66. ModulePass *createPGOInstrumentationGenLegacyPass(bool IsCS = false);
  67. ModulePass *
  68. createPGOInstrumentationUseLegacyPass(StringRef Filename = StringRef(""),
  69. bool IsCS = false);
  70. ModulePass *createPGOInstrumentationGenCreateVarLegacyPass(
  71. StringRef CSInstrName = StringRef(""));
  72. ModulePass *createPGOIndirectCallPromotionLegacyPass(bool InLTO = false,
  73. bool SamplePGO = false);
  74. FunctionPass *createPGOMemOPSizeOptLegacyPass();
  75. ModulePass *createCGProfileLegacyPass();
  76. // The pgo-specific indirect call promotion function declared below is used by
  77. // the pgo-driven indirect call promotion and sample profile passes. It's a
  78. // wrapper around llvm::promoteCall, et al. that additionally computes !prof
  79. // metadata. We place it in a pgo namespace so it's not confused with the
  80. // generic utilities.
  81. namespace pgo {
  82. // Helper function that transforms CB (either an indirect-call instruction, or
  83. // an invoke instruction , to a conditional call to F. This is like:
  84. // if (Inst.CalledValue == F)
  85. // F(...);
  86. // else
  87. // Inst(...);
  88. // end
  89. // TotalCount is the profile count value that the instruction executes.
  90. // Count is the profile count value that F is the target function.
  91. // These two values are used to update the branch weight.
  92. // If \p AttachProfToDirectCall is true, a prof metadata is attached to the
  93. // new direct call to contain \p Count.
  94. // Returns the promoted direct call instruction.
  95. CallBase &promoteIndirectCall(CallBase &CB, Function *F, uint64_t Count,
  96. uint64_t TotalCount, bool AttachProfToDirectCall,
  97. OptimizationRemarkEmitter *ORE);
  98. } // namespace pgo
  99. /// Options for the frontend instrumentation based profiling pass.
  100. struct InstrProfOptions {
  101. // Add the 'noredzone' attribute to added runtime library calls.
  102. bool NoRedZone = false;
  103. // Do counter register promotion
  104. bool DoCounterPromotion = false;
  105. // Use atomic profile counter increments.
  106. bool Atomic = false;
  107. // Use BFI to guide register promotion
  108. bool UseBFIInPromotion = false;
  109. // Name of the profile file to use as output
  110. std::string InstrProfileOutput;
  111. InstrProfOptions() = default;
  112. };
  113. /// Insert frontend instrumentation based profiling. Parameter IsCS indicates if
  114. // this is the context senstive instrumentation.
  115. ModulePass *createInstrProfilingLegacyPass(
  116. const InstrProfOptions &Options = InstrProfOptions(), bool IsCS = false);
  117. ModulePass *createInstrOrderFilePass();
  118. // Insert DataFlowSanitizer (dynamic data flow analysis) instrumentation
  119. ModulePass *createDataFlowSanitizerLegacyPassPass(
  120. const std::vector<std::string> &ABIListFiles = std::vector<std::string>());
  121. // Options for sanitizer coverage instrumentation.
  122. struct SanitizerCoverageOptions {
  123. enum Type {
  124. SCK_None = 0,
  125. SCK_Function,
  126. SCK_BB,
  127. SCK_Edge
  128. } CoverageType = SCK_None;
  129. bool IndirectCalls = false;
  130. bool TraceBB = false;
  131. bool TraceCmp = false;
  132. bool TraceDiv = false;
  133. bool TraceGep = false;
  134. bool Use8bitCounters = false;
  135. bool TracePC = false;
  136. bool TracePCGuard = false;
  137. bool Inline8bitCounters = false;
  138. bool InlineBoolFlag = false;
  139. bool PCTable = false;
  140. bool NoPrune = false;
  141. bool StackDepth = false;
  142. SanitizerCoverageOptions() = default;
  143. };
  144. /// Calculate what to divide by to scale counts.
  145. ///
  146. /// Given the maximum count, calculate a divisor that will scale all the
  147. /// weights to strictly less than std::numeric_limits<uint32_t>::max().
  148. static inline uint64_t calculateCountScale(uint64_t MaxCount) {
  149. return MaxCount < std::numeric_limits<uint32_t>::max()
  150. ? 1
  151. : MaxCount / std::numeric_limits<uint32_t>::max() + 1;
  152. }
  153. /// Scale an individual branch count.
  154. ///
  155. /// Scale a 64-bit weight down to 32-bits using \c Scale.
  156. ///
  157. static inline uint32_t scaleBranchCount(uint64_t Count, uint64_t Scale) {
  158. uint64_t Scaled = Count / Scale;
  159. assert(Scaled <= std::numeric_limits<uint32_t>::max() && "overflow 32-bits");
  160. return Scaled;
  161. }
  162. } // end namespace llvm
  163. #endif // LLVM_TRANSFORMS_INSTRUMENTATION_H