1 //===- llvm/IR/ProfDataUtils.h - Profiling Metadata Utilities ---*- 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 //
9 /// @file
10 /// This file contains the declarations for profiling metadata utility
11 /// functions.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_IR_PROFDATAUTILS_H
16 #define LLVM_IR_PROFDATAUTILS_H
17 
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/Twine.h"
20 #include "llvm/IR/Metadata.h"
21 
22 namespace llvm {
23 
24 /// Checks if an Instruction has MD_prof Metadata
25 bool hasProfMD(const Instruction &I);
26 
27 /// Checks if an MDNode contains Branch Weight Metadata
28 bool isBranchWeightMD(const MDNode *ProfileData);
29 
30 /// Checks if an instructions has Branch Weight Metadata
31 ///
32 /// \param I The instruction to check
33 /// \returns True if I has an MD_prof node containing Branch Weights. False
34 /// otherwise.
35 bool hasBranchWeightMD(const Instruction &I);
36 
37 /// Checks if an instructions has valid Branch Weight Metadata
38 ///
39 /// \param I The instruction to check
40 /// \returns True if I has an MD_prof node containing valid Branch Weights,
41 /// i.e., one weight for each successor. False otherwise.
42 bool hasValidBranchWeightMD(const Instruction &I);
43 
44 /// Get the branch weights metadata node
45 ///
46 /// \param I The Instruction to get the weights from.
47 /// \returns A pointer to I's branch weights metadata node, if it exists.
48 /// Nullptr otherwise.
49 MDNode *getBranchWeightMDNode(const Instruction &I);
50 
51 /// Get the valid branch weights metadata node
52 ///
53 /// \param I The Instruction to get the weights from.
54 /// \returns A pointer to I's valid branch weights metadata node, if it exists.
55 /// Nullptr otherwise.
56 MDNode *getValidBranchWeightMDNode(const Instruction &I);
57 
58 /// Extract branch weights from MD_prof metadata
59 ///
60 /// \param ProfileData A pointer to an MDNode.
61 /// \param [out] Weights An output vector to fill with branch weights
62 /// \returns True if weights were extracted, False otherwise. When false Weights
63 /// will be cleared.
64 bool extractBranchWeights(const MDNode *ProfileData,
65                           SmallVectorImpl<uint32_t> &Weights);
66 
67 /// Faster version of extractBranchWeights() that skips checks and must only
68 /// be called with "branch_weights" metadata nodes.
69 void extractFromBranchWeightMD(const MDNode *ProfileData,
70                                SmallVectorImpl<uint32_t> &Weights);
71 
72 /// Extract branch weights attatched to an Instruction
73 ///
74 /// \param I The Instruction to extract weights from.
75 /// \param [out] Weights An output vector to fill with branch weights
76 /// \returns True if weights were extracted, False otherwise. When false Weights
77 /// will be cleared.
78 bool extractBranchWeights(const Instruction &I,
79                           SmallVectorImpl<uint32_t> &Weights);
80 
81 /// Extract branch weights from a conditional branch or select Instruction.
82 ///
83 /// \param I The instruction to extract branch weights from.
84 /// \param [out] TrueVal will contain the branch weight for the True branch
85 /// \param [out] FalseVal will contain the branch weight for the False branch
86 /// \returns True on success with profile weights filled in. False if no
87 /// metadata or invalid metadata was found.
88 bool extractBranchWeights(const Instruction &I, uint64_t &TrueVal,
89                           uint64_t &FalseVal);
90 
91 /// Retrieve the total of all weights from MD_prof data.
92 ///
93 /// \param ProfileData The profile data to extract the total weight from
94 /// \param [out] TotalWeights input variable to fill with total weights
95 /// \returns True on success with profile total weights filled in. False if no
96 /// metadata was found.
97 bool extractProfTotalWeight(const MDNode *ProfileData, uint64_t &TotalWeights);
98 
99 /// Retrieve the total of all weights from an instruction.
100 ///
101 /// \param I The instruction to extract the total weight from
102 /// \param [out] TotalWeights input variable to fill with total weights
103 /// \returns True on success with profile total weights filled in. False if no
104 /// metadata was found.
105 bool extractProfTotalWeight(const Instruction &I, uint64_t &TotalWeights);
106 
107 /// Create a new `branch_weights` metadata node and add or overwrite
108 /// a `prof` metadata reference to instruction `I`.
109 void setBranchWeights(Instruction &I, ArrayRef<uint32_t> Weights);
110 
111 } // namespace llvm
112 #endif
113