Instrumentation.h 7.0 KB

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