CGProfile.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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/MDBuilder.h"
  15. #include "llvm/IR/PassManager.h"
  16. #include "llvm/InitializePasses.h"
  17. #include "llvm/ProfileData/InstrProf.h"
  18. #include "llvm/Transforms/Instrumentation.h"
  19. #include <optional>
  20. using namespace llvm;
  21. static bool
  22. addModuleFlags(Module &M,
  23. MapVector<std::pair<Function *, Function *>, uint64_t> &Counts) {
  24. if (Counts.empty())
  25. return false;
  26. LLVMContext &Context = M.getContext();
  27. MDBuilder MDB(Context);
  28. std::vector<Metadata *> Nodes;
  29. for (auto E : Counts) {
  30. Metadata *Vals[] = {ValueAsMetadata::get(E.first.first),
  31. ValueAsMetadata::get(E.first.second),
  32. MDB.createConstant(ConstantInt::get(
  33. Type::getInt64Ty(Context), E.second))};
  34. Nodes.push_back(MDNode::get(Context, Vals));
  35. }
  36. M.addModuleFlag(Module::Append, "CG Profile",
  37. MDTuple::getDistinct(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. std::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. PreservedAnalyses CGProfilePass::run(Module &M, ModuleAnalysisManager &MAM) {
  96. FunctionAnalysisManager &FAM =
  97. MAM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
  98. auto GetBFI = [&FAM](Function &F) -> BlockFrequencyInfo & {
  99. return FAM.getResult<BlockFrequencyAnalysis>(F);
  100. };
  101. auto GetTTI = [&FAM](Function &F) -> TargetTransformInfo & {
  102. return FAM.getResult<TargetIRAnalysis>(F);
  103. };
  104. runCGProfilePass(M, GetBFI, GetTTI, false);
  105. return PreservedAnalyses::all();
  106. }