xref: /freebsd/contrib/llvm-project/llvm/lib/IR/OptBisect.cpp (revision d11f81afd5a4a71d5f725950b0592ca212084780)
1 //===- llvm/IR/OptBisect/Bisect.cpp - LLVM Bisect support -----------------===//
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 implements support for a bisecting optimizations based on a
11 /// command line option.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/IR/OptBisect.h"
16 #include "llvm/Pass.h"
17 #include "llvm/Support/CommandLine.h"
18 #include "llvm/Support/raw_ostream.h"
19 #include <cassert>
20 #include <limits>
21 
22 using namespace llvm;
23 
24 static cl::opt<int> OptBisectLimit("opt-bisect-limit", cl::Hidden,
25                                    cl::init(std::numeric_limits<int>::max()),
26                                    cl::Optional,
27                                    cl::desc("Maximum optimization to perform"));
28 
29 OptBisect::OptBisect() : OptPassGate() {
30   BisectEnabled = OptBisectLimit != std::numeric_limits<int>::max();
31 }
32 
33 static void printPassMessage(const StringRef &Name, int PassNum,
34                              StringRef TargetDesc, bool Running) {
35   StringRef Status = Running ? "" : "NOT ";
36   errs() << "BISECT: " << Status << "running pass "
37          << "(" << PassNum << ") " << Name << " on " << TargetDesc << "\n";
38 }
39 
40 bool OptBisect::shouldRunPass(const Pass *P, StringRef IRDescription) {
41   assert(BisectEnabled);
42 
43   return checkPass(P->getPassName(), IRDescription);
44 }
45 
46 bool OptBisect::checkPass(const StringRef PassName,
47                           const StringRef TargetDesc) {
48   assert(BisectEnabled);
49 
50   int CurBisectNum = ++LastBisectNum;
51   bool ShouldRun = (OptBisectLimit == -1 || CurBisectNum <= OptBisectLimit);
52   printPassMessage(PassName, CurBisectNum, TargetDesc, ShouldRun);
53   return ShouldRun;
54 }
55 
56 ManagedStatic<OptBisect> llvm::OptBisector;
57