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