diff --git a/llvm/include/llvm/IR/OptBisect.h b/llvm/include/llvm/IR/OptBisect.h --- a/llvm/include/llvm/IR/OptBisect.h +++ b/llvm/include/llvm/IR/OptBisect.h @@ -16,6 +16,7 @@ #include "llvm/ADT/StringRef.h" #include "llvm/Support/ManagedStatic.h" +#include namespace llvm { @@ -43,14 +44,12 @@ /// optimization-related problems. class OptBisect : public OptPassGate { public: - /// Default constructor, initializes the OptBisect state based on the - /// -opt-bisect-limit command line argument. - /// - /// By default, bisection is disabled. - /// + /// Default constructor. Initializes the state to "disabled". The bisection + /// will be enabled by the cl::opt call-back when the command line option + /// is processed. /// Clients should not instantiate this class directly. All access should go /// through LLVMContext. - OptBisect(); + OptBisect() = default; virtual ~OptBisect() = default; @@ -60,7 +59,11 @@ bool shouldRunPass(const Pass *P, StringRef IRDescription) override; /// isEnabled() should return true before calling shouldRunPass(). - bool isEnabled() const override { return BisectEnabled; } + bool isEnabled() const override { return BisectLimit != Disabled; } + + /// Set the new optimization limit. Passing OptBisect::Disabled disables + /// the limiting. + void setLimit(int Limit) { BisectLimit = Limit; } /// Checks the bisect limit to determine if the specified pass should run. /// @@ -75,9 +78,11 @@ /// instance, function passes should call FunctionPass::skipFunction(). bool checkPass(const StringRef PassName, const StringRef TargetDesc); + static const int Disabled = std::numeric_limits::max(); + private: - bool BisectEnabled = false; - unsigned LastBisectNum = 0; + int BisectLimit = Disabled; + int LastBisectNum = 0; }; /// Singleton instance of the OptBisect class, so multiple pass managers don't diff --git a/llvm/lib/IR/OptBisect.cpp b/llvm/lib/IR/OptBisect.cpp --- a/llvm/lib/IR/OptBisect.cpp +++ b/llvm/lib/IR/OptBisect.cpp @@ -22,14 +22,12 @@ using namespace llvm; static cl::opt OptBisectLimit("opt-bisect-limit", cl::Hidden, - cl::init(std::numeric_limits::max()), - cl::Optional, + cl::init(OptBisect::Disabled), cl::Optional, + cl::cb([](int Limit) { + llvm::OptBisector->setLimit(Limit); + }), cl::desc("Maximum optimization to perform")); -OptBisect::OptBisect() : OptPassGate() { - BisectEnabled = OptBisectLimit != std::numeric_limits::max(); -} - static void printPassMessage(const StringRef &Name, int PassNum, StringRef TargetDesc, bool Running) { StringRef Status = Running ? "" : "NOT "; @@ -38,17 +36,17 @@ } bool OptBisect::shouldRunPass(const Pass *P, StringRef IRDescription) { - assert(BisectEnabled); + assert(isEnabled()); return checkPass(P->getPassName(), IRDescription); } bool OptBisect::checkPass(const StringRef PassName, const StringRef TargetDesc) { - assert(BisectEnabled); + assert(isEnabled()); int CurBisectNum = ++LastBisectNum; - bool ShouldRun = (OptBisectLimit == -1 || CurBisectNum <= OptBisectLimit); + bool ShouldRun = (BisectLimit == -1 || CurBisectNum <= BisectLimit); printPassMessage(PassName, CurBisectNum, TargetDesc, ShouldRun); return ShouldRun; }