InstrProfiling.h 5.0 KB

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