InstrProfiling.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- Transforms/Instrumentation/InstrProfiling.h --------------*- 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. /// \file
  14. /// This file provides the interface for LLVM's PGO Instrumentation lowering
  15. /// pass.
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_TRANSFORMS_INSTRUMENTATION_INSTRPROFILING_H
  18. #define LLVM_TRANSFORMS_INSTRUMENTATION_INSTRPROFILING_H
  19. #include "llvm/ADT/DenseMap.h"
  20. #include "llvm/ADT/StringRef.h"
  21. #include "llvm/IR/IntrinsicInst.h"
  22. #include "llvm/IR/PassManager.h"
  23. #include "llvm/ProfileData/InstrProf.h"
  24. #include "llvm/Transforms/Instrumentation.h"
  25. #include <cstdint>
  26. #include <cstring>
  27. #include <vector>
  28. namespace llvm {
  29. class TargetLibraryInfo;
  30. using LoadStorePair = std::pair<Instruction *, Instruction *>;
  31. /// Instrumentation based profiling lowering pass. This pass lowers
  32. /// the profile instrumented code generated by FE or the IR based
  33. /// instrumentation pass.
  34. class InstrProfiling : public PassInfoMixin<InstrProfiling> {
  35. public:
  36. InstrProfiling() : IsCS(false) {}
  37. InstrProfiling(const InstrProfOptions &Options, bool IsCS = false)
  38. : Options(Options), IsCS(IsCS) {}
  39. PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM);
  40. bool run(Module &M,
  41. std::function<const TargetLibraryInfo &(Function &F)> GetTLI);
  42. private:
  43. InstrProfOptions Options;
  44. Module *M;
  45. Triple TT;
  46. std::function<const TargetLibraryInfo &(Function &F)> GetTLI;
  47. struct PerFunctionProfileData {
  48. uint32_t NumValueSites[IPVK_Last + 1];
  49. GlobalVariable *RegionCounters = nullptr;
  50. GlobalVariable *DataVar = nullptr;
  51. PerFunctionProfileData() {
  52. memset(NumValueSites, 0, sizeof(uint32_t) * (IPVK_Last + 1));
  53. }
  54. };
  55. DenseMap<GlobalVariable *, PerFunctionProfileData> ProfileDataMap;
  56. /// If runtime relocation is enabled, this maps functions to the load
  57. /// instruction that produces the profile relocation bias.
  58. DenseMap<const Function *, LoadInst *> FunctionToProfileBiasMap;
  59. std::vector<GlobalValue *> CompilerUsedVars;
  60. std::vector<GlobalValue *> UsedVars;
  61. std::vector<GlobalVariable *> ReferencedNames;
  62. GlobalVariable *NamesVar;
  63. size_t NamesSize;
  64. // Is this lowering for the context-sensitive instrumentation.
  65. bool IsCS;
  66. // vector of counter load/store pairs to be register promoted.
  67. std::vector<LoadStorePair> PromotionCandidates;
  68. int64_t TotalCountersPromoted = 0;
  69. /// Lower instrumentation intrinsics in the function. Returns true if there
  70. /// any lowering.
  71. bool lowerIntrinsics(Function *F);
  72. /// Register-promote counter loads and stores in loops.
  73. void promoteCounterLoadStores(Function *F);
  74. /// Returns true if relocating counters at runtime is enabled.
  75. bool isRuntimeCounterRelocationEnabled() const;
  76. /// Returns true if profile counter update register promotion is enabled.
  77. bool isCounterPromotionEnabled() const;
  78. /// Count the number of instrumented value sites for the function.
  79. void computeNumValueSiteCounts(InstrProfValueProfileInst *Ins);
  80. /// Replace instrprof.value.profile with a call to runtime library.
  81. void lowerValueProfileInst(InstrProfValueProfileInst *Ins);
  82. /// Replace instrprof.cover with a store instruction to the coverage byte.
  83. void lowerCover(InstrProfCoverInst *Inc);
  84. /// Replace instrprof.increment with an increment of the appropriate value.
  85. void lowerIncrement(InstrProfIncrementInst *Inc);
  86. /// Force emitting of name vars for unused functions.
  87. void lowerCoverageData(GlobalVariable *CoverageNamesVar);
  88. /// Compute the address of the counter value that this profiling instruction
  89. /// acts on.
  90. Value *getCounterAddress(InstrProfInstBase *I);
  91. /// Get the region counters for an increment, creating them if necessary.
  92. ///
  93. /// If the counter array doesn't yet exist, the profile data variables
  94. /// referring to them will also be created.
  95. GlobalVariable *getOrCreateRegionCounters(InstrProfInstBase *Inc);
  96. /// Create the region counters.
  97. GlobalVariable *createRegionCounters(InstrProfInstBase *Inc, StringRef Name,
  98. GlobalValue::LinkageTypes Linkage);
  99. /// Emit the section with compressed function names.
  100. void emitNameData();
  101. /// Emit value nodes section for value profiling.
  102. void emitVNodes();
  103. /// Emit runtime registration functions for each profile data variable.
  104. void emitRegistration();
  105. /// Emit the necessary plumbing to pull in the runtime initialization.
  106. /// Returns true if a change was made.
  107. bool emitRuntimeHook();
  108. /// Add uses of our data variables and runtime hook.
  109. void emitUses();
  110. /// Create a static initializer for our data, on platforms that need it,
  111. /// and for any profile output file that was specified.
  112. void emitInitialization();
  113. };
  114. } // end namespace llvm
  115. #endif // LLVM_TRANSFORMS_INSTRUMENTATION_INSTRPROFILING_H
  116. #ifdef __GNUC__
  117. #pragma GCC diagnostic pop
  118. #endif