CGProfile.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 (NewCount == 0)
  48. return;
  49. if (!CalledF || !TTI.isLoweredToCall(CalledF) ||
  50. CalledF->hasDLLImportStorageClass())
  51. return;
  52. uint64_t &Count = Counts[std::make_pair(F, CalledF)];
  53. Count = SaturatingAdd(Count, NewCount);
  54. };
  55. // Ignore error here. Indirect calls are ignored if this fails.
  56. (void)(bool) Symtab.create(M);
  57. for (auto &F : M) {
  58. // Avoid extra cost of running passes for BFI when the function doesn't have
  59. // entry count. Since LazyBlockFrequencyInfoPass only exists in LPM, check
  60. // if using LazyBlockFrequencyInfoPass.
  61. // TODO: Remove LazyBFI when LazyBlockFrequencyInfoPass is available in NPM.
  62. if (F.isDeclaration() || (LazyBFI && !F.getEntryCount()))
  63. continue;
  64. auto &BFI = GetBFI(F);
  65. if (BFI.getEntryFreq() == 0)
  66. continue;
  67. TargetTransformInfo &TTI = GetTTI(F);
  68. for (auto &BB : F) {
  69. Optional<uint64_t> BBCount = BFI.getBlockProfileCount(&BB);
  70. if (!BBCount)
  71. continue;
  72. for (auto &I : BB) {
  73. CallBase *CB = dyn_cast<CallBase>(&I);
  74. if (!CB)
  75. continue;
  76. if (CB->isIndirectCall()) {
  77. InstrProfValueData ValueData[8];
  78. uint32_t ActualNumValueData;
  79. uint64_t TotalC;
  80. if (!getValueProfDataFromInst(*CB, IPVK_IndirectCallTarget, 8,
  81. ValueData, ActualNumValueData, TotalC))
  82. continue;
  83. for (const auto &VD :
  84. ArrayRef<InstrProfValueData>(ValueData, ActualNumValueData)) {
  85. UpdateCounts(TTI, &F, Symtab.getFunction(VD.Value), VD.Count);
  86. }
  87. continue;
  88. }
  89. UpdateCounts(TTI, &F, CB->getCalledFunction(), *BBCount);
  90. }
  91. }
  92. }
  93. return addModuleFlags(M, Counts);
  94. }
  95. namespace {
  96. struct CGProfileLegacyPass final : public ModulePass {
  97. static char ID;
  98. CGProfileLegacyPass() : ModulePass(ID) {
  99. initializeCGProfileLegacyPassPass(*PassRegistry::getPassRegistry());
  100. }
  101. void getAnalysisUsage(AnalysisUsage &AU) const override {
  102. AU.setPreservesCFG();
  103. AU.addRequired<LazyBlockFrequencyInfoPass>();
  104. AU.addRequired<TargetTransformInfoWrapperPass>();
  105. }
  106. bool runOnModule(Module &M) override {
  107. auto GetBFI = [this](Function &F) -> BlockFrequencyInfo & {
  108. return this->getAnalysis<LazyBlockFrequencyInfoPass>(F).getBFI();
  109. };
  110. auto GetTTI = [this](Function &F) -> TargetTransformInfo & {
  111. return this->getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
  112. };
  113. return runCGProfilePass(M, GetBFI, GetTTI, true);
  114. }
  115. };
  116. } // namespace
  117. char CGProfileLegacyPass::ID = 0;
  118. INITIALIZE_PASS(CGProfileLegacyPass, "cg-profile", "Call Graph Profile", false,
  119. false)
  120. ModulePass *llvm::createCGProfileLegacyPass() {
  121. return new CGProfileLegacyPass();
  122. }
  123. PreservedAnalyses CGProfilePass::run(Module &M, ModuleAnalysisManager &MAM) {
  124. FunctionAnalysisManager &FAM =
  125. MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
  126. auto GetBFI = [&FAM](Function &F) -> BlockFrequencyInfo & {
  127. return FAM.getResult<BlockFrequencyAnalysis>(F);
  128. };
  129. auto GetTTI = [&FAM](Function &F) -> TargetTransformInfo & {
  130. return FAM.getResult<TargetIRAnalysis>(F);
  131. };
  132. runCGProfilePass(M, GetBFI, GetTTI, false);
  133. return PreservedAnalyses::all();
  134. }