181ad6265SDimitry Andric //===--- MisExpect.h - Check the use of llvm.expect with PGO data ---------===// 281ad6265SDimitry Andric // 381ad6265SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 481ad6265SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 581ad6265SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 681ad6265SDimitry Andric // 781ad6265SDimitry Andric //===----------------------------------------------------------------------===// 881ad6265SDimitry Andric // 981ad6265SDimitry Andric // This contains code to emit diagnostic messages for potentially incorrect 1081ad6265SDimitry Andric // usage of the llvm.expect intrinsic. This utility extracts the threshold 1181ad6265SDimitry Andric // values from metadata associated with the instrumented Branch or Switch 1281ad6265SDimitry Andric // instruction. The threshold values are then used to determine if a diagnostic 1381ad6265SDimitry Andric // should be emitted. 1481ad6265SDimitry Andric // 1581ad6265SDimitry Andric //===----------------------------------------------------------------------===// 1681ad6265SDimitry Andric 17fcaf7f86SDimitry Andric #ifndef LLVM_TRANSFORMS_UTILS_MISEXPECT_H 18fcaf7f86SDimitry Andric #define LLVM_TRANSFORMS_UTILS_MISEXPECT_H 19fcaf7f86SDimitry Andric 2081ad6265SDimitry Andric #include "llvm/ADT/SmallVector.h" 2181ad6265SDimitry Andric #include "llvm/IR/Function.h" 2281ad6265SDimitry Andric #include "llvm/IR/Instructions.h" 2381ad6265SDimitry Andric #include "llvm/IR/LLVMContext.h" 2481ad6265SDimitry Andric 2581ad6265SDimitry Andric namespace llvm { 2681ad6265SDimitry Andric namespace misexpect { 2781ad6265SDimitry Andric 2881ad6265SDimitry Andric /// checkBackendInstrumentation - compares PGO counters to the thresholds used 2981ad6265SDimitry Andric /// for llvm.expect and warns if the PGO counters are outside of the expected 30*0fca6ea1SDimitry Andric /// range. It extracts the expected weights from the MD_prof weights attached 315f757f3fSDimitry Andric /// to the instruction, which are assumed to come from lowered llvm.expect 3281ad6265SDimitry Andric /// intrinsics. The RealWeights parameter and the extracted expected weights are 3381ad6265SDimitry Andric /// then passed to verifyMisexpect() for verification 3481ad6265SDimitry Andric /// 3581ad6265SDimitry Andric /// \param I The Instruction being checked 3681ad6265SDimitry Andric /// \param RealWeights A vector of profile weights for each target block 3781ad6265SDimitry Andric void checkBackendInstrumentation(Instruction &I, 3881ad6265SDimitry Andric const llvm::ArrayRef<uint32_t> RealWeights); 3981ad6265SDimitry Andric 4081ad6265SDimitry Andric /// checkFrontendInstrumentation - compares PGO counters to the thresholds used 4181ad6265SDimitry Andric /// for llvm.expect and warns if the PGO counters are outside of the expected 42*0fca6ea1SDimitry Andric /// range. It extracts the expected weights from the MD_prof weights attached 435f757f3fSDimitry Andric /// to the instruction, which are assumed to come from profiling data 4481ad6265SDimitry Andric /// attached by the frontend prior to llvm.expect intrinsic lowering. The 4581ad6265SDimitry Andric /// ExpectedWeights parameter and the extracted real weights are then passed to 4681ad6265SDimitry Andric /// verifyMisexpect() for verification 4781ad6265SDimitry Andric /// 4881ad6265SDimitry Andric /// \param I The Instruction being checked 4981ad6265SDimitry Andric /// \param ExpectedWeights A vector of the expected weights for each target 50*0fca6ea1SDimitry Andric /// block, this determines the threshold values used when emitting diagnostics 5181ad6265SDimitry Andric void checkFrontendInstrumentation(Instruction &I, 5281ad6265SDimitry Andric const ArrayRef<uint32_t> ExpectedWeights); 5381ad6265SDimitry Andric 5481ad6265SDimitry Andric /// veryifyMisExpect - compares RealWeights to the thresholds used 5581ad6265SDimitry Andric /// for llvm.expect and warns if the PGO counters are outside of the expected 5681ad6265SDimitry Andric /// range. 5781ad6265SDimitry Andric /// 5881ad6265SDimitry Andric /// \param I The Instruction being checked 5981ad6265SDimitry Andric /// \param RealWeights A vector of profile weights from the profile data 6081ad6265SDimitry Andric /// \param ExpectedWeights A vector of the weights attatch by llvm.expect 6181ad6265SDimitry Andric void verifyMisExpect(Instruction &I, ArrayRef<uint32_t> RealWeights, 6281ad6265SDimitry Andric const ArrayRef<uint32_t> ExpectedWeights); 6381ad6265SDimitry Andric 6481ad6265SDimitry Andric /// checkExpectAnnotations - compares PGO counters to the thresholds used 6581ad6265SDimitry Andric /// for llvm.expect and warns if the PGO counters are outside of the expected 66*0fca6ea1SDimitry Andric /// range. It extracts the expected weights from the MD_prof weights attached 675f757f3fSDimitry Andric /// to the instruction, which are assumed to come from lowered llvm.expect 6881ad6265SDimitry Andric /// intrinsics. The RealWeights parameter and the extracted expected weights are 6981ad6265SDimitry Andric /// then passed to verifyMisexpect() for verification. It is a thin wrapper 7081ad6265SDimitry Andric /// around the checkFrontendInstrumentation and checkBackendInstrumentation APIs 7181ad6265SDimitry Andric /// 7281ad6265SDimitry Andric /// \param I The Instruction being checked 73bdd1243dSDimitry Andric /// \param ExistingWeights A vector of profile weights for each target block 74bdd1243dSDimitry Andric /// \param IsFrontend A boolean describing if this is Frontend instrumentation 7581ad6265SDimitry Andric void checkExpectAnnotations(Instruction &I, 7681ad6265SDimitry Andric const ArrayRef<uint32_t> ExistingWeights, 7781ad6265SDimitry Andric bool IsFrontend); 7881ad6265SDimitry Andric 7981ad6265SDimitry Andric } // namespace misexpect 8081ad6265SDimitry Andric } // namespace llvm 81fcaf7f86SDimitry Andric 82fcaf7f86SDimitry Andric #endif 83