xref: /llvm-project/llvm/include/llvm/Analysis/IndirectCallPromotionAnalysis.h (revision 61759513c8166a6420ded480802de72859a45499)
1 //===- IndirectCallPromotionAnalysis.h - Indirect call analysis -*- C++ -*-===//
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 /// \file
9 /// Interface to identify indirect call promotion candidates.
10 ///
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_ANALYSIS_INDIRECTCALLPROMOTIONANALYSIS_H
14 #define LLVM_ANALYSIS_INDIRECTCALLPROMOTIONANALYSIS_H
15 
16 #include "llvm/ProfileData/InstrProf.h"
17 
18 namespace llvm {
19 
20 class Instruction;
21 
22 // Class for identifying profitable indirect call promotion candidates when
23 // the indirect-call value profile metadata is available.
24 class ICallPromotionAnalysis {
25 private:
26   // Allocate space to read the profile annotation.
27   SmallVector<InstrProfValueData, 4> ValueDataArray;
28 
29   // Count is the call count for the direct-call target.
30   // TotalCount is the total call count for the indirect-call callsite.
31   // RemainingCount is the TotalCount minus promoted-direct-call count.
32   // Return true we should promote this indirect-call target.
33   bool isPromotionProfitable(uint64_t Count, uint64_t TotalCount,
34                              uint64_t RemainingCount);
35 
36   // Returns the number of profitable candidates to promote for the
37   // current ValueDataArray and the given \p Inst.
38   uint32_t getProfitablePromotionCandidates(const Instruction *Inst,
39                                             uint64_t TotalCount);
40 
41   // Noncopyable
42   ICallPromotionAnalysis(const ICallPromotionAnalysis &other) = delete;
43   ICallPromotionAnalysis &
44   operator=(const ICallPromotionAnalysis &other) = delete;
45 
46 public:
47   ICallPromotionAnalysis() = default;
48 
49   /// Returns reference to array of InstrProfValueData for the given
50   /// instruction \p I.
51   ///
52   /// The \p TotalCount and \p NumCandidates are set to the the total profile
53   /// count of the indirect call \p I and the number of profitable candidates
54   /// in the given array (which is sorted in reverse order of profitability).
55   ///
56   /// The returned array space is owned by this class, and overwritten on
57   /// subsequent calls.
58   MutableArrayRef<InstrProfValueData> getPromotionCandidatesForInstruction(
59       const Instruction *I, uint64_t &TotalCount, uint32_t &NumCandidates);
60 };
61 
62 } // end namespace llvm
63 
64 #endif
65