CGProfile.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //===-- CGProfile.cpp -----------------------------------------------------===//
  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. #include "llvm/Transforms/Instrumentation/CGProfile.h"
  9. #include "llvm/ADT/MapVector.h"
  10. #include "llvm/Analysis/BlockFrequencyInfo.h"
  11. #include "llvm/Analysis/LazyBlockFrequencyInfo.h"
  12. #include "llvm/Analysis/TargetTransformInfo.h"
  13. #include "llvm/IR/Constants.h"
  14. #include "llvm/IR/Instructions.h"
  15. #include "llvm/IR/MDBuilder.h"
  16. #include "llvm/IR/PassManager.h"
  17. #include "llvm/InitializePasses.h"
  18. #include "llvm/ProfileData/InstrProf.h"
  19. #include "llvm/Transforms/Instrumentation.h"
  20. #include <array>
  21. using namespace llvm;
  22. static bool
  23. addModuleFlags(Module &M,
  24. MapVector<std::pair<Function *, Function *>, uint64_t> &Counts) {
  25. if (Counts.empty())
  26. return false;
  27. LLVMContext &Context = M.getContext();
  28. MDBuilder MDB(Context);
  29. std::vector<Metadata *> Nodes;
  30. for (auto E : Counts) {
  31. Metadata *Vals[] = {ValueAsMetadata::get(E.first.first),
  32. ValueAsMetadata::get(E.first.second),
  33. MDB.createConstant(ConstantInt::get(
  34. Type::getInt64Ty(Context), E.second))};
  35. Nodes.push_back(MDNode::get(Context, Vals));
  36. }
  37. M.addModuleFlag(Module::Append, "CG Profile", MDNode::get(Context, Nodes));
  38. return true;
  39. }
  40. static bool runCGProfilePass(
  41. Module &M, function_ref<BlockFrequencyInfo &(Function &)> GetBFI,
  42. function_ref<TargetTransformInfo &(Function &)> GetTTI, bool LazyBFI) {
  43. MapVector<std::pair<Function *, Function *>, uint64_t> Counts;
  44. InstrProfSymtab Symtab;
  45. auto UpdateCounts = [&](TargetTransformInfo &TTI, Function *F,
  46. Function *CalledF, uint64_t NewCount) {
  47. if (!CalledF || !TTI.isLoweredToCall(CalledF) ||
  48. CalledF->hasDLLImportStorageClass())
  49. return;
  50. uint64_t &Count = Counts[std::make_pair(F, CalledF)];
  51. Count = SaturatingAdd(Count, NewCount);
  52. };
  53. // Ignore error here. Indirect calls are ignored if this fails.
  54. (void)(bool) Symtab.create(M);
  55. for (auto &F : M) {
  56. // Avoid extra cost of running passes for BFI when the function doesn't have
  57. // entry count. Since LazyBlockFrequencyInfoPass only exists in LPM, check
  58. // if using LazyBlockFrequencyInfoPass.
  59. // TODO: Remove LazyBFI when LazyBlockFrequencyInfoPass is available in NPM.
  60. if (F.isDeclaration() || (LazyBFI && !F.getEntryCount()))
  61. continue;
  62. auto &BFI = GetBFI(F);
  63. if (BFI.getEntryFreq() == 0)
  64. continue;
  65. TargetTransformInfo &TTI = GetTTI(F);
  66. for (auto &BB : F) {
  67. Optional<uint64_t> BBCount = BFI.getBlockProfileCount(&BB);
  68. if (!BBCount)
  69. continue;
  70. for (auto &I : BB) {
  71. CallBase *CB = dyn_cast<CallBase>(&I);
  72. if (!CB)
  73. continue;
  74. if (CB->isIndirectCall()) {
  75. InstrProfValueData ValueData[8];
  76. uint32_t ActualNumValueData;
  77. uint64_t TotalC;
  78. if (!getValueProfDataFromInst(*CB, IPVK_IndirectCallTarget, 8,
  79. ValueData, ActualNumValueData, TotalC))
  80. continue;
  81. for (const auto &VD :
  82. ArrayRef<InstrProfValueData>(ValueData, ActualNumValueData)) {
  83. UpdateCounts(TTI, &F, Symtab.getFunction(VD.Value), VD.Count);
  84. }
  85. continue;
  86. }
  87. UpdateCounts(TTI, &F, CB->getCalledFunction(), *BBCount);
  88. }
  89. }
  90. }
  91. return addModuleFlags(M, Counts);
  92. }
  93. namespace {
  94. struct CGProfileLegacyPass final : public ModulePass {
  95. static char ID;
  96. CGProfileLegacyPass() : ModulePass(ID) {
  97. initializeCGProfileLegacyPassPass(*PassRegistry::getPassRegistry());
  98. }
  99. void getAnalysisUsage(AnalysisUsage &AU) const override {
  100. AU.setPreservesCFG();
  101. AU.addRequired<LazyBlockFrequencyInfoPass>();
  102. AU.addRequired<TargetTransformInfoWrapperPass>();
  103. }
  104. bool runOnModule(Module &M) override {
  105. auto GetBFI = [this](Function &F) -> BlockFrequencyInfo & {
  106. return this->getAnalysis<LazyBlockFrequencyInfoPass>(F).getBFI();
  107. };
  108. auto GetTTI = [this](Function &F) -> TargetTransformInfo & {
  109. return this->getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
  110. };
  111. return runCGProfilePass(M, GetBFI, GetTTI, true);
  112. }
  113. };
  114. } // namespace
  115. char CGProfileLegacyPass::ID = 0;
  116. INITIALIZE_PASS(CGProfileLegacyPass, "cg-profile", "Call Graph Profile", false,
  117. false)
  118. ModulePass *llvm::createCGProfileLegacyPass() {
  119. return new CGProfileLegacyPass();
  120. }
  121. PreservedAnalyses CGProfilePass::run(Module &M, ModuleAnalysisManager &MAM) {
  122. FunctionAnalysisManager &FAM =
  123. MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
  124. auto GetBFI = [&FAM](Function &F) -> BlockFrequencyInfo & {
  125. return FAM.getResult<BlockFrequencyAnalysis>(F);
  126. };
  127. auto GetTTI = [&FAM](Function &F) -> TargetTransformInfo & {
  128. return FAM.getResult<TargetIRAnalysis>(F);
  129. };
  130. runCGProfilePass(M, GetBFI, GetTTI, false);
  131. return PreservedAnalyses::all();
  132. }