LLVM 20.0.0git
ProfDataUtils.h
Go to the documentation of this file.
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
19#include "llvm/ADT/Twine.h"
20#include "llvm/IR/Metadata.h"
21
22namespace llvm {
23
24/// Checks if an Instruction has MD_prof Metadata
25bool hasProfMD(const Instruction &I);
26
27/// Checks if an MDNode contains Branch Weight Metadata
28bool 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.
35bool 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.
42bool 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.
49MDNode *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.
56MDNode *getValidBranchWeightMDNode(const Instruction &I);
57
58/// Check if Branch Weight Metadata has an "expected" field from an llvm.expect*
59/// intrinsic
60bool hasBranchWeightOrigin(const Instruction &I);
61
62/// Check if Branch Weight Metadata has an "expected" field from an llvm.expect*
63/// intrinsic
64bool hasBranchWeightOrigin(const MDNode *ProfileData);
65
66/// Return the offset to the first branch weight data
67unsigned getBranchWeightOffset(const MDNode *ProfileData);
68
69unsigned getNumBranchWeights(const MDNode &ProfileData);
70
71/// Extract branch weights from MD_prof metadata
72///
73/// \param ProfileData A pointer to an MDNode.
74/// \param [out] Weights An output vector to fill with branch weights
75/// \returns True if weights were extracted, False otherwise. When false Weights
76/// will be cleared.
77bool extractBranchWeights(const MDNode *ProfileData,
78 SmallVectorImpl<uint32_t> &Weights);
79
80/// Faster version of extractBranchWeights() that skips checks and must only
81/// be called with "branch_weights" metadata nodes. Supports uint32_t.
82void extractFromBranchWeightMD32(const MDNode *ProfileData,
83 SmallVectorImpl<uint32_t> &Weights);
84
85/// Faster version of extractBranchWeights() that skips checks and must only
86/// be called with "branch_weights" metadata nodes. Supports uint64_t.
87void extractFromBranchWeightMD64(const MDNode *ProfileData,
88 SmallVectorImpl<uint64_t> &Weights);
89
90/// Extract branch weights attatched to an Instruction
91///
92/// \param I The Instruction to extract weights from.
93/// \param [out] Weights An output vector to fill with branch weights
94/// \returns True if weights were extracted, False otherwise. When false Weights
95/// will be cleared.
96bool extractBranchWeights(const Instruction &I,
97 SmallVectorImpl<uint32_t> &Weights);
98
99/// Extract branch weights from a conditional branch or select Instruction.
100///
101/// \param I The instruction to extract branch weights from.
102/// \param [out] TrueVal will contain the branch weight for the True branch
103/// \param [out] FalseVal will contain the branch weight for the False branch
104/// \returns True on success with profile weights filled in. False if no
105/// metadata or invalid metadata was found.
106bool extractBranchWeights(const Instruction &I, uint64_t &TrueVal,
107 uint64_t &FalseVal);
108
109/// Retrieve the total of all weights from MD_prof data.
110///
111/// \param ProfileData The profile data to extract the total weight from
112/// \param [out] TotalWeights input variable to fill with total weights
113/// \returns True on success with profile total weights filled in. False if no
114/// metadata was found.
115bool extractProfTotalWeight(const MDNode *ProfileData, uint64_t &TotalWeights);
116
117/// Retrieve the total of all weights from an instruction.
118///
119/// \param I The instruction to extract the total weight from
120/// \param [out] TotalWeights input variable to fill with total weights
121/// \returns True on success with profile total weights filled in. False if no
122/// metadata was found.
123bool extractProfTotalWeight(const Instruction &I, uint64_t &TotalWeights);
124
125/// Create a new `branch_weights` metadata node and add or overwrite
126/// a `prof` metadata reference to instruction `I`.
127/// \param I the Instruction to set branch weights on.
128/// \param Weights an array of weights to set on instruction I.
129/// \param IsExpected were these weights added from an llvm.expect* intrinsic.
130void setBranchWeights(Instruction &I, ArrayRef<uint32_t> Weights,
131 bool IsExpected);
132
133/// Scaling the profile data attached to 'I' using the ratio of S/T.
134void scaleProfData(Instruction &I, uint64_t S, uint64_t T);
135
136} // namespace llvm
137#endif
#define I(x, y, z)
Definition: MD5.cpp:58
This file contains the declarations for metadata subclasses.
This file defines the SmallVector class.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
bool extractProfTotalWeight(const MDNode *ProfileData, uint64_t &TotalWeights)
Retrieve the total of all weights from MD_prof data.
unsigned getBranchWeightOffset(const MDNode *ProfileData)
Return the offset to the first branch weight data.
bool isBranchWeightMD(const MDNode *ProfileData)
Checks if an MDNode contains Branch Weight Metadata.
MDNode * getBranchWeightMDNode(const Instruction &I)
Get the branch weights metadata node.
bool hasBranchWeightOrigin(const Instruction &I)
Check if Branch Weight Metadata has an "expected" field from an llvm.expect* intrinsic.
void setBranchWeights(Instruction &I, ArrayRef< uint32_t > Weights, bool IsExpected)
Create a new branch_weights metadata node and add or overwrite a prof metadata reference to instructi...
MDNode * getValidBranchWeightMDNode(const Instruction &I)
Get the valid branch weights metadata node.
bool hasValidBranchWeightMD(const Instruction &I)
Checks if an instructions has valid Branch Weight Metadata.
unsigned getNumBranchWeights(const MDNode &ProfileData)
void extractFromBranchWeightMD32(const MDNode *ProfileData, SmallVectorImpl< uint32_t > &Weights)
Faster version of extractBranchWeights() that skips checks and must only be called with "branch_weigh...
bool hasProfMD(const Instruction &I)
Checks if an Instruction has MD_prof Metadata.
bool extractBranchWeights(const MDNode *ProfileData, SmallVectorImpl< uint32_t > &Weights)
Extract branch weights from MD_prof metadata.
bool hasBranchWeightMD(const Instruction &I)
Checks if an instructions has Branch Weight Metadata.
void scaleProfData(Instruction &I, uint64_t S, uint64_t T)
Scaling the profile data attached to 'I' using the ratio of S/T.
void extractFromBranchWeightMD64(const MDNode *ProfileData, SmallVectorImpl< uint64_t > &Weights)
Faster version of extractBranchWeights() that skips checks and must only be called with "branch_weigh...