Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / llvm / lib / IR / ProfDataUtils.cpp
blob77b3c1cb95d686c776e0b61f2565f5cf8d47e241
1 //===- ProfDataUtils.cpp - Utility functions for MD_prof Metadata ---------===//
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 // This file implements utilities for working with Profiling Metadata.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/IR/ProfDataUtils.h"
14 #include "llvm/ADT/SmallVector.h"
15 #include "llvm/ADT/Twine.h"
16 #include "llvm/IR/Constants.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/Instructions.h"
19 #include "llvm/IR/LLVMContext.h"
20 #include "llvm/IR/Metadata.h"
21 #include "llvm/Support/BranchProbability.h"
22 #include "llvm/Support/CommandLine.h"
24 using namespace llvm;
26 namespace {
28 // MD_prof nodes have the following layout
30 // In general:
31 // { String name, Array of i32 }
33 // In terms of Types:
34 // { MDString, [i32, i32, ...]}
36 // Concretely for Branch Weights
37 // { "branch_weights", [i32 1, i32 10000]}
39 // We maintain some constants here to ensure that we access the branch weights
40 // correctly, and can change the behavior in the future if the layout changes
42 // The index at which the weights vector starts
43 constexpr unsigned WeightsIdx = 1;
45 // the minimum number of operands for MD_prof nodes with branch weights
46 constexpr unsigned MinBWOps = 3;
48 // We may want to add support for other MD_prof types, so provide an abstraction
49 // for checking the metadata type.
50 bool isTargetMD(const MDNode *ProfData, const char *Name, unsigned MinOps) {
51 // TODO: This routine may be simplified if MD_prof used an enum instead of a
52 // string to differentiate the types of MD_prof nodes.
53 if (!ProfData || !Name || MinOps < 2)
54 return false;
56 unsigned NOps = ProfData->getNumOperands();
57 if (NOps < MinOps)
58 return false;
60 auto *ProfDataName = dyn_cast<MDString>(ProfData->getOperand(0));
61 if (!ProfDataName)
62 return false;
64 return ProfDataName->getString().equals(Name);
67 } // namespace
69 namespace llvm {
71 bool hasProfMD(const Instruction &I) {
72 return nullptr != I.getMetadata(LLVMContext::MD_prof);
75 bool isBranchWeightMD(const MDNode *ProfileData) {
76 return isTargetMD(ProfileData, "branch_weights", MinBWOps);
79 bool hasBranchWeightMD(const Instruction &I) {
80 auto *ProfileData = I.getMetadata(LLVMContext::MD_prof);
81 return isBranchWeightMD(ProfileData);
84 bool hasValidBranchWeightMD(const Instruction &I) {
85 return getValidBranchWeightMDNode(I);
88 MDNode *getBranchWeightMDNode(const Instruction &I) {
89 auto *ProfileData = I.getMetadata(LLVMContext::MD_prof);
90 if (!isBranchWeightMD(ProfileData))
91 return nullptr;
92 return ProfileData;
95 MDNode *getValidBranchWeightMDNode(const Instruction &I) {
96 auto *ProfileData = getBranchWeightMDNode(I);
97 if (ProfileData && ProfileData->getNumOperands() == 1 + I.getNumSuccessors())
98 return ProfileData;
99 return nullptr;
102 void extractFromBranchWeightMD(const MDNode *ProfileData,
103 SmallVectorImpl<uint32_t> &Weights) {
104 assert(isBranchWeightMD(ProfileData) && "wrong metadata");
106 unsigned NOps = ProfileData->getNumOperands();
107 assert(WeightsIdx < NOps && "Weights Index must be less than NOps.");
108 Weights.resize(NOps - WeightsIdx);
110 for (unsigned Idx = WeightsIdx, E = NOps; Idx != E; ++Idx) {
111 ConstantInt *Weight =
112 mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(Idx));
113 assert(Weight && "Malformed branch_weight in MD_prof node");
114 assert(Weight->getValue().getActiveBits() <= 32 &&
115 "Too many bits for uint32_t");
116 Weights[Idx - WeightsIdx] = Weight->getZExtValue();
120 bool extractBranchWeights(const MDNode *ProfileData,
121 SmallVectorImpl<uint32_t> &Weights) {
122 if (!isBranchWeightMD(ProfileData))
123 return false;
124 extractFromBranchWeightMD(ProfileData, Weights);
125 return true;
128 bool extractBranchWeights(const Instruction &I,
129 SmallVectorImpl<uint32_t> &Weights) {
130 auto *ProfileData = I.getMetadata(LLVMContext::MD_prof);
131 return extractBranchWeights(ProfileData, Weights);
134 bool extractBranchWeights(const Instruction &I, uint64_t &TrueVal,
135 uint64_t &FalseVal) {
136 assert((I.getOpcode() == Instruction::Br ||
137 I.getOpcode() == Instruction::Select) &&
138 "Looking for branch weights on something besides branch, select, or "
139 "switch");
141 SmallVector<uint32_t, 2> Weights;
142 auto *ProfileData = I.getMetadata(LLVMContext::MD_prof);
143 if (!extractBranchWeights(ProfileData, Weights))
144 return false;
146 if (Weights.size() > 2)
147 return false;
149 TrueVal = Weights[0];
150 FalseVal = Weights[1];
151 return true;
154 bool extractProfTotalWeight(const MDNode *ProfileData, uint64_t &TotalVal) {
155 TotalVal = 0;
156 if (!ProfileData)
157 return false;
159 auto *ProfDataName = dyn_cast<MDString>(ProfileData->getOperand(0));
160 if (!ProfDataName)
161 return false;
163 if (ProfDataName->getString().equals("branch_weights")) {
164 for (unsigned Idx = 1; Idx < ProfileData->getNumOperands(); Idx++) {
165 auto *V = mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(Idx));
166 assert(V && "Malformed branch_weight in MD_prof node");
167 TotalVal += V->getValue().getZExtValue();
169 return true;
172 if (ProfDataName->getString().equals("VP") &&
173 ProfileData->getNumOperands() > 3) {
174 TotalVal = mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(2))
175 ->getValue()
176 .getZExtValue();
177 return true;
179 return false;
182 bool extractProfTotalWeight(const Instruction &I, uint64_t &TotalVal) {
183 return extractProfTotalWeight(I.getMetadata(LLVMContext::MD_prof), TotalVal);
186 } // namespace llvm