45constexpr unsigned MinBWOps = 3;
48constexpr unsigned MinVPOps = 5;
52bool isTargetMD(
const MDNode *ProfData,
const char *
Name,
unsigned MinOps) {
55 if (!ProfData || !
Name || MinOps < 2)
62 auto *ProfDataName = dyn_cast<MDString>(ProfData->
getOperand(0));
66 return ProfDataName->getString() ==
Name;
70 typename =
typename std::enable_if<std::is_arithmetic_v<T>>>
71static void extractFromBranchWeightMD(
const MDNode *ProfileData,
77 assert(WeightsIdx < NOps &&
"Weights Index must be less than NOps.");
78 Weights.
resize(NOps - WeightsIdx);
80 for (
unsigned Idx = WeightsIdx, E = NOps;
Idx != E; ++
Idx) {
82 mdconst::dyn_extract<ConstantInt>(ProfileData->
getOperand(
Idx));
83 assert(Weight &&
"Malformed branch_weight in MD_prof node");
85 "Too many bits for MD_prof branch_weight");
95 return I.hasMetadata(LLVMContext::MD_prof);
99 return isTargetMD(ProfileData,
"branch_weights", MinBWOps);
103 return isTargetMD(ProfileData,
"VP", MinVPOps);
107 auto *ProfileData =
I.getMetadata(LLVMContext::MD_prof);
112 auto *ProfileData =
I.getMetadata(LLVMContext::MD_prof);
126 auto *ProfileData =
I.getMetadata(LLVMContext::MD_prof);
133 auto *ProfDataName = dyn_cast<MDString>(ProfileData->
getOperand(1));
137 assert(ProfDataName ==
nullptr || ProfDataName->getString() ==
"expected");
138 return ProfDataName !=
nullptr;
150 auto *ProfileData =
I.getMetadata(LLVMContext::MD_prof);
165 extractFromBranchWeightMD(ProfileData, Weights);
170 extractFromBranchWeightMD(ProfileData, Weights);
177 extractFromBranchWeightMD(ProfileData, Weights);
183 auto *ProfileData =
I.getMetadata(LLVMContext::MD_prof);
189 assert((
I.getOpcode() == Instruction::Br ||
190 I.getOpcode() == Instruction::Select) &&
191 "Looking for branch weights on something besides branch, select, or "
195 auto *ProfileData =
I.getMetadata(LLVMContext::MD_prof);
199 if (Weights.
size() > 2)
202 TrueVal = Weights[0];
203 FalseVal = Weights[1];
212 auto *ProfDataName = dyn_cast<MDString>(ProfileData->
getOperand(0));
216 if (ProfDataName->getString() ==
"branch_weights") {
219 auto *V = mdconst::dyn_extract<ConstantInt>(ProfileData->
getOperand(
Idx));
220 assert(V &&
"Malformed branch_weight in MD_prof node");
221 TotalVal += V->getValue().getZExtValue();
226 if (ProfDataName->getString() ==
"VP" && ProfileData->
getNumOperands() > 3) {
227 TotalVal = mdconst::dyn_extract<ConstantInt>(ProfileData->
getOperand(2))
243 I.setMetadata(LLVMContext::MD_prof, BranchWeights);
247 assert(
T != 0 &&
"Caller should guarantee");
248 auto *ProfileData =
I.getMetadata(LLVMContext::MD_prof);
249 if (ProfileData ==
nullptr)
252 auto *ProfDataName = dyn_cast<MDString>(ProfileData->
getOperand(0));
253 if (!ProfDataName || (ProfDataName->getString() !=
"branch_weights" &&
254 ProfDataName->getString() !=
"VP"))
265 APInt APS(128, S), APT(128,
T);
266 if (ProfDataName->getString() ==
"branch_weights" &&
270 mdconst::dyn_extract<ConstantInt>(
277 }
else if (ProfDataName->getString() ==
"VP")
282 mdconst::dyn_extract<ConstantInt>(ProfileData->
getOperand(i + 1))
291 APInt Val(128, Count);
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
This file contains the declarations for profiling metadata utility functions.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallVector class.
Class for arbitrary precision integers.
APInt udiv(const APInt &RHS) const
Unsigned division operation.
unsigned getActiveBits() const
Compute the number of active bits in the value.
uint64_t getLimitedValue(uint64_t Limit=UINT64_MAX) const
If this value is smaller than the specified limit, return it, otherwise return the limit value.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
This is the shared class of boolean and integer constants.
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
const APInt & getValue() const
Return the constant as an APInt value reference.
This is an important class for using LLVM in a threaded context.
ConstantAsMetadata * createConstant(Constant *C)
Return the given constant as metadata.
MDNode * createBranchWeights(uint32_t TrueWeight, uint32_t FalseWeight, bool IsExpected=false)
Return metadata containing two branch weights.
const MDOperand & getOperand(unsigned I) const
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
unsigned getNumOperands() const
Return number of MDNode operands.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
static IntegerType * getInt32Ty(LLVMContext &C)
static IntegerType * getInt64Ty(LLVMContext &C)
@ C
The default llvm calling convention, compatible with C.
This is an optimization pass for GlobalISel generic memory operations.
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.
bool isValueProfileMD(const MDNode *ProfileData)
bool hasCountTypeMD(const Instruction &I)
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.
const uint64_t NOMORE_ICP_MAGICNUM
Magic number in the value profile metadata showing a target has been promoted for the instruction and...
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...