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