2424
2525using namespace llvm ;
2626
27- namespace {
28-
2927// MD_prof nodes have the following layout
3028//
3129// In general:
@@ -41,14 +39,15 @@ namespace {
4139// correctly, and can change the behavior in the future if the layout changes
4240
4341// the minimum number of operands for MD_prof nodes with branch weights
44- constexpr unsigned MinBWOps = 3 ;
42+ static constexpr unsigned MinBWOps = 3 ;
4543
4644// the minimum number of operands for MD_prof nodes with value profiles
47- constexpr unsigned MinVPOps = 5 ;
45+ static constexpr unsigned MinVPOps = 5 ;
4846
4947// We may want to add support for other MD_prof types, so provide an abstraction
5048// for checking the metadata type.
51- bool isTargetMD (const MDNode *ProfData, const char *Name, unsigned MinOps) {
49+ static bool isTargetMD (const MDNode *ProfData, const char *Name,
50+ unsigned MinOps) {
5251 // TODO: This routine may be simplified if MD_prof used an enum instead of a
5352 // string to differentiate the types of MD_prof nodes.
5453 if (!ProfData || !Name || MinOps < 2 )
@@ -101,14 +100,11 @@ static SmallVector<uint32_t> fitWeights(ArrayRef<uint64_t> Weights) {
101100 return Ret;
102101}
103102
104- } // namespace
105-
106- namespace llvm {
107- cl::opt<bool > ElideAllZeroBranchWeights (" elide-all-zero-branch-weights" ,
103+ static cl::opt<bool > ElideAllZeroBranchWeights (" elide-all-zero-branch-weights" ,
108104#if defined(LLVM_ENABLE_PROFCHECK)
109- cl::init (false )
105+ cl::init (false )
110106#else
111- cl::init (true )
107+ cl::init (true )
112108#endif
113109);
114110const char *MDProfLabels::BranchWeights = " branch_weights" ;
@@ -118,21 +114,21 @@ const char *MDProfLabels::FunctionEntryCount = "function_entry_count";
118114const char *MDProfLabels::SyntheticFunctionEntryCount =
119115 " synthetic_function_entry_count" ;
120116const char *MDProfLabels::UnknownBranchWeightsMarker = " unknown" ;
121- const char *LLVMLoopEstimatedTripCount = " llvm.loop.estimated_trip_count" ;
117+ const char *llvm:: LLVMLoopEstimatedTripCount = " llvm.loop.estimated_trip_count" ;
122118
123- bool hasProfMD (const Instruction &I) {
119+ bool llvm:: hasProfMD (const Instruction &I) {
124120 return I.hasMetadata (LLVMContext::MD_prof);
125121}
126122
127- bool isBranchWeightMD (const MDNode *ProfileData) {
123+ bool llvm:: isBranchWeightMD (const MDNode *ProfileData) {
128124 return isTargetMD (ProfileData, MDProfLabels::BranchWeights, MinBWOps);
129125}
130126
131- bool isValueProfileMD (const MDNode *ProfileData) {
127+ bool llvm:: isValueProfileMD (const MDNode *ProfileData) {
132128 return isTargetMD (ProfileData, MDProfLabels::ValueProfile, MinVPOps);
133129}
134130
135- bool hasBranchWeightMD (const Instruction &I) {
131+ bool llvm:: hasBranchWeightMD (const Instruction &I) {
136132 auto *ProfileData = I.getMetadata (LLVMContext::MD_prof);
137133 return isBranchWeightMD (ProfileData);
138134}
@@ -147,16 +143,16 @@ static bool hasCountTypeMD(const Instruction &I) {
147143 return isa<CallBase>(I) && !isBranchWeightMD (ProfileData);
148144}
149145
150- bool hasValidBranchWeightMD (const Instruction &I) {
146+ bool llvm:: hasValidBranchWeightMD (const Instruction &I) {
151147 return getValidBranchWeightMDNode (I);
152148}
153149
154- bool hasBranchWeightOrigin (const Instruction &I) {
150+ bool llvm:: hasBranchWeightOrigin (const Instruction &I) {
155151 auto *ProfileData = I.getMetadata (LLVMContext::MD_prof);
156152 return hasBranchWeightOrigin (ProfileData);
157153}
158154
159- bool hasBranchWeightOrigin (const MDNode *ProfileData) {
155+ bool llvm:: hasBranchWeightOrigin (const MDNode *ProfileData) {
160156 if (!isBranchWeightMD (ProfileData))
161157 return false ;
162158 auto *ProfDataName = dyn_cast<MDString>(ProfileData->getOperand (1 ));
@@ -168,54 +164,54 @@ bool hasBranchWeightOrigin(const MDNode *ProfileData) {
168164 return ProfDataName != nullptr ;
169165}
170166
171- unsigned getBranchWeightOffset (const MDNode *ProfileData) {
167+ unsigned llvm:: getBranchWeightOffset (const MDNode *ProfileData) {
172168 return hasBranchWeightOrigin (ProfileData) ? 2 : 1 ;
173169}
174170
175- unsigned getNumBranchWeights (const MDNode &ProfileData) {
171+ unsigned llvm:: getNumBranchWeights (const MDNode &ProfileData) {
176172 return ProfileData.getNumOperands () - getBranchWeightOffset (&ProfileData);
177173}
178174
179- MDNode *getBranchWeightMDNode (const Instruction &I) {
175+ MDNode *llvm:: getBranchWeightMDNode (const Instruction &I) {
180176 auto *ProfileData = I.getMetadata (LLVMContext::MD_prof);
181177 if (!isBranchWeightMD (ProfileData))
182178 return nullptr ;
183179 return ProfileData;
184180}
185181
186- MDNode *getValidBranchWeightMDNode (const Instruction &I) {
182+ MDNode *llvm:: getValidBranchWeightMDNode (const Instruction &I) {
187183 auto *ProfileData = getBranchWeightMDNode (I);
188184 if (ProfileData && getNumBranchWeights (*ProfileData) == I.getNumSuccessors ())
189185 return ProfileData;
190186 return nullptr ;
191187}
192188
193- void extractFromBranchWeightMD32 (const MDNode *ProfileData,
194- SmallVectorImpl<uint32_t > &Weights) {
189+ void llvm:: extractFromBranchWeightMD32 (const MDNode *ProfileData,
190+ SmallVectorImpl<uint32_t > &Weights) {
195191 extractFromBranchWeightMD (ProfileData, Weights);
196192}
197193
198- void extractFromBranchWeightMD64 (const MDNode *ProfileData,
199- SmallVectorImpl<uint64_t > &Weights) {
194+ void llvm:: extractFromBranchWeightMD64 (const MDNode *ProfileData,
195+ SmallVectorImpl<uint64_t > &Weights) {
200196 extractFromBranchWeightMD (ProfileData, Weights);
201197}
202198
203- bool extractBranchWeights (const MDNode *ProfileData,
204- SmallVectorImpl<uint32_t > &Weights) {
199+ bool llvm:: extractBranchWeights (const MDNode *ProfileData,
200+ SmallVectorImpl<uint32_t > &Weights) {
205201 if (!isBranchWeightMD (ProfileData))
206202 return false ;
207203 extractFromBranchWeightMD (ProfileData, Weights);
208204 return true ;
209205}
210206
211- bool extractBranchWeights (const Instruction &I,
212- SmallVectorImpl<uint32_t > &Weights) {
207+ bool llvm:: extractBranchWeights (const Instruction &I,
208+ SmallVectorImpl<uint32_t > &Weights) {
213209 auto *ProfileData = I.getMetadata (LLVMContext::MD_prof);
214210 return extractBranchWeights (ProfileData, Weights);
215211}
216212
217- bool extractBranchWeights (const Instruction &I, uint64_t &TrueVal,
218- uint64_t &FalseVal) {
213+ bool llvm:: extractBranchWeights (const Instruction &I, uint64_t &TrueVal,
214+ uint64_t &FalseVal) {
219215 assert ((I.getOpcode () == Instruction::Br ||
220216 I.getOpcode () == Instruction::Select) &&
221217 " Looking for branch weights on something besides branch, select, or "
@@ -234,7 +230,8 @@ bool extractBranchWeights(const Instruction &I, uint64_t &TrueVal,
234230 return true ;
235231}
236232
237- bool extractProfTotalWeight (const MDNode *ProfileData, uint64_t &TotalVal) {
233+ bool llvm::extractProfTotalWeight (const MDNode *ProfileData,
234+ uint64_t &TotalVal) {
238235 TotalVal = 0 ;
239236 if (!ProfileData)
240237 return false ;
@@ -262,11 +259,12 @@ bool extractProfTotalWeight(const MDNode *ProfileData, uint64_t &TotalVal) {
262259 return false ;
263260}
264261
265- bool extractProfTotalWeight (const Instruction &I, uint64_t &TotalVal) {
262+ bool llvm:: extractProfTotalWeight (const Instruction &I, uint64_t &TotalVal) {
266263 return extractProfTotalWeight (I.getMetadata (LLVMContext::MD_prof), TotalVal);
267264}
268265
269- void setExplicitlyUnknownBranchWeights (Instruction &I, StringRef PassName) {
266+ void llvm::setExplicitlyUnknownBranchWeights (Instruction &I,
267+ StringRef PassName) {
270268 MDBuilder MDB (I.getContext ());
271269 I.setMetadata (
272270 LLVMContext::MD_prof,
@@ -275,14 +273,16 @@ void setExplicitlyUnknownBranchWeights(Instruction &I, StringRef PassName) {
275273 MDB.createString (PassName)}));
276274}
277275
278- void setExplicitlyUnknownBranchWeightsIfProfiled (Instruction &I, Function &F,
279- StringRef PassName) {
276+ void llvm::setExplicitlyUnknownBranchWeightsIfProfiled (Instruction &I,
277+ Function &F,
278+ StringRef PassName) {
280279 if (std::optional<Function::ProfileCount> EC = F.getEntryCount ();
281280 EC && EC->getCount () > 0 )
282281 setExplicitlyUnknownBranchWeights (I, PassName);
283282}
284283
285- void setExplicitlyUnknownFunctionEntryCount (Function &F, StringRef PassName) {
284+ void llvm::setExplicitlyUnknownFunctionEntryCount (Function &F,
285+ StringRef PassName) {
286286 MDBuilder MDB (F.getContext ());
287287 F.setMetadata (
288288 LLVMContext::MD_prof,
@@ -291,21 +291,21 @@ void setExplicitlyUnknownFunctionEntryCount(Function &F, StringRef PassName) {
291291 MDB.createString (PassName)}));
292292}
293293
294- bool isExplicitlyUnknownProfileMetadata (const MDNode &MD) {
294+ bool llvm:: isExplicitlyUnknownProfileMetadata (const MDNode &MD) {
295295 if (MD.getNumOperands () != 2 )
296296 return false ;
297297 return MD.getOperand (0 ).equalsStr (MDProfLabels::UnknownBranchWeightsMarker);
298298}
299299
300- bool hasExplicitlyUnknownBranchWeights (const Instruction &I) {
300+ bool llvm:: hasExplicitlyUnknownBranchWeights (const Instruction &I) {
301301 auto *MD = I.getMetadata (LLVMContext::MD_prof);
302302 if (!MD)
303303 return false ;
304304 return isExplicitlyUnknownProfileMetadata (*MD);
305305}
306306
307- void setBranchWeights (Instruction &I, ArrayRef<uint32_t > Weights,
308- bool IsExpected, bool ElideAllZero) {
307+ void llvm:: setBranchWeights (Instruction &I, ArrayRef<uint32_t > Weights,
308+ bool IsExpected, bool ElideAllZero) {
309309 if ((ElideAllZeroBranchWeights && ElideAllZero) &&
310310 llvm::all_of (Weights, [](uint32_t V) { return V == 0 ; })) {
311311 I.setMetadata (LLVMContext::MD_prof, nullptr );
@@ -317,13 +317,14 @@ void setBranchWeights(Instruction &I, ArrayRef<uint32_t> Weights,
317317 I.setMetadata (LLVMContext::MD_prof, BranchWeights);
318318}
319319
320- void setFittedBranchWeights (Instruction &I, ArrayRef<uint64_t > Weights,
321- bool IsExpected, bool ElideAllZero) {
320+ void llvm:: setFittedBranchWeights (Instruction &I, ArrayRef<uint64_t > Weights,
321+ bool IsExpected, bool ElideAllZero) {
322322 setBranchWeights (I, fitWeights (Weights), IsExpected, ElideAllZero);
323323}
324324
325- SmallVector<uint32_t > downscaleWeights (ArrayRef<uint64_t > Weights,
326- std::optional<uint64_t > KnownMaxCount) {
325+ SmallVector<uint32_t >
326+ llvm::downscaleWeights (ArrayRef<uint64_t > Weights,
327+ std::optional<uint64_t > KnownMaxCount) {
327328 uint64_t MaxCount = KnownMaxCount.has_value () ? KnownMaxCount.value ()
328329 : *llvm::max_element (Weights);
329330 assert (MaxCount > 0 && " Bad max count" );
@@ -334,7 +335,7 @@ SmallVector<uint32_t> downscaleWeights(ArrayRef<uint64_t> Weights,
334335 return DownscaledWeights;
335336}
336337
337- void scaleProfData (Instruction &I, uint64_t S, uint64_t T) {
338+ void llvm:: scaleProfData (Instruction &I, uint64_t S, uint64_t T) {
338339 assert (T != 0 && " Caller should guarantee" );
339340 auto *ProfileData = I.getMetadata (LLVMContext::MD_prof);
340341 if (ProfileData == nullptr )
@@ -387,5 +388,3 @@ void scaleProfData(Instruction &I, uint64_t S, uint64_t T) {
387388 }
388389 I.setMetadata (LLVMContext::MD_prof, MDNode::get (C, Vals));
389390}
390-
391- } // namespace llvm
0 commit comments