1 //===--- MisExpect.h - Check the use of llvm.expect with PGO data ---------===//
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 contains code to emit diagnostic messages for potentially incorrect
10 // usage of the llvm.expect intrinsic. This utility extracts the threshold
11 // values from metadata associated with the instrumented Branch or Switch
12 // instruction. The threshold values are then used to determine if a diagnostic
13 // should be emitted.
14 //
15 //===----------------------------------------------------------------------===//
16 
17 #ifndef LLVM_TRANSFORMS_UTILS_MISEXPECT_H
18 #define LLVM_TRANSFORMS_UTILS_MISEXPECT_H
19 
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/Instructions.h"
23 #include "llvm/IR/LLVMContext.h"
24 
25 namespace llvm {
26 namespace misexpect {
27 
28 /// checkBackendInstrumentation - compares PGO counters to the thresholds used
29 /// for llvm.expect and warns if the PGO counters are outside of the expected
30 /// range. It extracts the expected weights from the MD_prof weights attatched
31 /// to the instruction, which are are assumed to come from lowered llvm.expect
32 /// intrinsics. The RealWeights parameter and the extracted expected weights are
33 /// then passed to verifyMisexpect() for verification
34 ///
35 /// \param I The Instruction being checked
36 /// \param RealWeights A vector of profile weights for each target block
37 void checkBackendInstrumentation(Instruction &I,
38                                  const llvm::ArrayRef<uint32_t> RealWeights);
39 
40 /// checkFrontendInstrumentation - compares PGO counters to the thresholds used
41 /// for llvm.expect and warns if the PGO counters are outside of the expected
42 /// range. It extracts the expected weights from the MD_prof weights attatched
43 /// to the instruction, which are are assumed to come from profiling data
44 /// attached by the frontend prior to llvm.expect intrinsic lowering. The
45 /// ExpectedWeights parameter and the extracted real weights are then passed to
46 /// verifyMisexpect() for verification
47 ///
48 /// \param I The Instruction being checked
49 /// \param ExpectedWeights A vector of the expected weights for each target
50 /// block, this determines the threshold values used when emiting diagnostics
51 void checkFrontendInstrumentation(Instruction &I,
52                                   const ArrayRef<uint32_t> ExpectedWeights);
53 
54 /// veryifyMisExpect - compares RealWeights to the thresholds used
55 /// for llvm.expect and warns if the PGO counters are outside of the expected
56 /// range.
57 ///
58 /// \param I The Instruction being checked
59 /// \param RealWeights A vector of profile weights from the profile data
60 /// \param ExpectedWeights A vector of the weights attatch by llvm.expect
61 void verifyMisExpect(Instruction &I, ArrayRef<uint32_t> RealWeights,
62                      const ArrayRef<uint32_t> ExpectedWeights);
63 
64 /// checkExpectAnnotations - compares PGO counters to the thresholds used
65 /// for llvm.expect and warns if the PGO counters are outside of the expected
66 /// range. It extracts the expected weights from the MD_prof weights attatched
67 /// to the instruction, which are are assumed to come from lowered llvm.expect
68 /// intrinsics. The RealWeights parameter and the extracted expected weights are
69 /// then passed to verifyMisexpect() for verification. It is a thin wrapper
70 /// around the checkFrontendInstrumentation and checkBackendInstrumentation APIs
71 ///
72 /// \param I The Instruction being checked
73 /// \param RealWeights A vector of profile weights for each target block
74 /// \param IsBackend A boolean describing if this is Frontend instrumentation
75 void checkExpectAnnotations(Instruction &I,
76                             const ArrayRef<uint32_t> ExistingWeights,
77                             bool IsFrontend);
78 
79 } // namespace misexpect
80 } // namespace llvm
81 
82 #endif
83