Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:10

0001 //===- llvm/IR/ProfDataUtils.h - Profiling Metadata Utilities ---*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 /// @file
0010 /// This file contains the declarations for profiling metadata utility
0011 /// functions.
0012 //
0013 //===----------------------------------------------------------------------===//
0014 
0015 #ifndef LLVM_IR_PROFDATAUTILS_H
0016 #define LLVM_IR_PROFDATAUTILS_H
0017 
0018 #include "llvm/ADT/SmallVector.h"
0019 #include "llvm/ADT/Twine.h"
0020 #include "llvm/IR/Metadata.h"
0021 
0022 namespace llvm {
0023 
0024 /// Checks if an Instruction has MD_prof Metadata
0025 bool hasProfMD(const Instruction &I);
0026 
0027 /// Checks if an MDNode contains Branch Weight Metadata
0028 bool isBranchWeightMD(const MDNode *ProfileData);
0029 
0030 /// Checks if an instructions has Branch Weight Metadata
0031 ///
0032 /// \param I The instruction to check
0033 /// \returns True if I has an MD_prof node containing Branch Weights. False
0034 /// otherwise.
0035 bool hasBranchWeightMD(const Instruction &I);
0036 
0037 /// Checks if an instructions has valid Branch Weight Metadata
0038 ///
0039 /// \param I The instruction to check
0040 /// \returns True if I has an MD_prof node containing valid Branch Weights,
0041 /// i.e., one weight for each successor. False otherwise.
0042 bool hasValidBranchWeightMD(const Instruction &I);
0043 
0044 /// Get the branch weights metadata node
0045 ///
0046 /// \param I The Instruction to get the weights from.
0047 /// \returns A pointer to I's branch weights metadata node, if it exists.
0048 /// Nullptr otherwise.
0049 MDNode *getBranchWeightMDNode(const Instruction &I);
0050 
0051 /// Get the valid branch weights metadata node
0052 ///
0053 /// \param I The Instruction to get the weights from.
0054 /// \returns A pointer to I's valid branch weights metadata node, if it exists.
0055 /// Nullptr otherwise.
0056 MDNode *getValidBranchWeightMDNode(const Instruction &I);
0057 
0058 /// Check if Branch Weight Metadata has an "expected" field from an llvm.expect*
0059 /// intrinsic
0060 bool hasBranchWeightOrigin(const Instruction &I);
0061 
0062 /// Check if Branch Weight Metadata has an "expected" field from an llvm.expect*
0063 /// intrinsic
0064 bool hasBranchWeightOrigin(const MDNode *ProfileData);
0065 
0066 /// Return the offset to the first branch weight data
0067 unsigned getBranchWeightOffset(const MDNode *ProfileData);
0068 
0069 unsigned getNumBranchWeights(const MDNode &ProfileData);
0070 
0071 /// Extract branch weights from MD_prof metadata
0072 ///
0073 /// \param ProfileData A pointer to an MDNode.
0074 /// \param [out] Weights An output vector to fill with branch weights
0075 /// \returns True if weights were extracted, False otherwise. When false Weights
0076 /// will be cleared.
0077 bool extractBranchWeights(const MDNode *ProfileData,
0078                           SmallVectorImpl<uint32_t> &Weights);
0079 
0080 /// Faster version of extractBranchWeights() that skips checks and must only
0081 /// be called with "branch_weights" metadata nodes. Supports uint32_t.
0082 void extractFromBranchWeightMD32(const MDNode *ProfileData,
0083                                  SmallVectorImpl<uint32_t> &Weights);
0084 
0085 /// Faster version of extractBranchWeights() that skips checks and must only
0086 /// be called with "branch_weights" metadata nodes. Supports uint64_t.
0087 void extractFromBranchWeightMD64(const MDNode *ProfileData,
0088                                  SmallVectorImpl<uint64_t> &Weights);
0089 
0090 /// Extract branch weights attatched to an Instruction
0091 ///
0092 /// \param I The Instruction to extract weights from.
0093 /// \param [out] Weights An output vector to fill with branch weights
0094 /// \returns True if weights were extracted, False otherwise. When false Weights
0095 /// will be cleared.
0096 bool extractBranchWeights(const Instruction &I,
0097                           SmallVectorImpl<uint32_t> &Weights);
0098 
0099 /// Extract branch weights from a conditional branch or select Instruction.
0100 ///
0101 /// \param I The instruction to extract branch weights from.
0102 /// \param [out] TrueVal will contain the branch weight for the True branch
0103 /// \param [out] FalseVal will contain the branch weight for the False branch
0104 /// \returns True on success with profile weights filled in. False if no
0105 /// metadata or invalid metadata was found.
0106 bool extractBranchWeights(const Instruction &I, uint64_t &TrueVal,
0107                           uint64_t &FalseVal);
0108 
0109 /// Retrieve the total of all weights from MD_prof data.
0110 ///
0111 /// \param ProfileData The profile data to extract the total weight from
0112 /// \param [out] TotalWeights input variable to fill with total weights
0113 /// \returns True on success with profile total weights filled in. False if no
0114 /// metadata was found.
0115 bool extractProfTotalWeight(const MDNode *ProfileData, uint64_t &TotalWeights);
0116 
0117 /// Retrieve the total of all weights from an instruction.
0118 ///
0119 /// \param I The instruction to extract the total weight from
0120 /// \param [out] TotalWeights input variable to fill with total weights
0121 /// \returns True on success with profile total weights filled in. False if no
0122 /// metadata was found.
0123 bool extractProfTotalWeight(const Instruction &I, uint64_t &TotalWeights);
0124 
0125 /// Create a new `branch_weights` metadata node and add or overwrite
0126 /// a `prof` metadata reference to instruction `I`.
0127 /// \param I the Instruction to set branch weights on.
0128 /// \param Weights an array of weights to set on instruction I.
0129 /// \param IsExpected were these weights added from an llvm.expect* intrinsic.
0130 void setBranchWeights(Instruction &I, ArrayRef<uint32_t> Weights,
0131                       bool IsExpected);
0132 
0133 /// Scaling the profile data attached to 'I' using the ratio of S/T.
0134 void scaleProfData(Instruction &I, uint64_t S, uint64_t T);
0135 
0136 } // namespace llvm
0137 #endif