xref: /freebsd/contrib/llvm-project/llvm/include/llvm/IR/OptBisect.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===- llvm/IR/OptBisect.h - LLVM Bisect support ----------------*- C++ -*-===//
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 /// \file
10 /// This file declares the interface for bisecting optimizations.
11 ///
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_IR_OPTBISECT_H
15 #define LLVM_IR_OPTBISECT_H
16 
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Support/Compiler.h"
19 #include <limits>
20 
21 namespace llvm {
22 
23 /// Extensions to this class implement mechanisms to disable passes and
24 /// individual optimizations at compile time.
25 class OptPassGate {
26 public:
27   virtual ~OptPassGate() = default;
28 
29   /// IRDescription is a textual description of the IR unit the pass is running
30   /// over.
shouldRunPass(StringRef PassName,StringRef IRDescription)31   virtual bool shouldRunPass(StringRef PassName,
32                              StringRef IRDescription) const {
33     return true;
34   }
35 
36   /// isEnabled() should return true before calling shouldRunPass().
isEnabled()37   virtual bool isEnabled() const { return false; }
38 };
39 
40 /// This class implements a mechanism to disable passes and individual
41 /// optimizations at compile time based on a command line option
42 /// (-opt-bisect-limit) in order to perform a bisecting search for
43 /// optimization-related problems.
44 class LLVM_ABI OptBisect : public OptPassGate {
45 public:
46   /// Default constructor. Initializes the state to "disabled". The bisection
47   /// will be enabled by the cl::opt call-back when the command line option
48   /// is processed.
49   /// Clients should not instantiate this class directly.  All access should go
50   /// through LLVMContext.
51   OptBisect() = default;
52 
53   virtual ~OptBisect() = default;
54 
55   /// Checks the bisect limit to determine if the specified pass should run.
56   ///
57   /// The method prints the name of the pass, its assigned bisect number, and
58   /// whether or not the pass will be executed. It returns true if the pass
59   /// should run, i.e. if the bisect limit is set to -1 or has not yet been
60   /// exceeded.
61   ///
62   /// Most passes should not call this routine directly. Instead, it is called
63   /// through helper routines provided by the base classes of the pass. For
64   /// instance, function passes should call FunctionPass::skipFunction().
65   bool shouldRunPass(StringRef PassName,
66                      StringRef IRDescription) const override;
67 
68   /// isEnabled() should return true before calling shouldRunPass().
isEnabled()69   bool isEnabled() const override { return BisectLimit != Disabled; }
70 
71   /// Set the new optimization limit and reset the counter. Passing
72   /// OptBisect::Disabled disables the limiting.
setLimit(int Limit)73   void setLimit(int Limit) {
74     BisectLimit = Limit;
75     LastBisectNum = 0;
76   }
77 
78   static constexpr int Disabled = std::numeric_limits<int>::max();
79 
80 private:
81   int BisectLimit = Disabled;
82   mutable int LastBisectNum = 0;
83 };
84 
85 /// Singleton instance of the OptBisect class, so multiple pass managers don't
86 /// need to coordinate their uses of OptBisect.
87 LLVM_ABI OptPassGate &getGlobalPassGate();
88 
89 } // end namespace llvm
90 
91 #endif // LLVM_IR_OPTBISECT_H
92