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