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 
21 using namespace llvm;
22 
23 static cl::opt<int> OptBisectLimit("opt-bisect-limit", cl::Hidden,
24                                    cl::init(OptBisect::Disabled), cl::Optional,
25                                    cl::cb<void, int>([](int Limit) {
26                                      llvm::getOptBisector().setLimit(Limit);
27                                    }),
28                                    cl::desc("Maximum optimization to perform"));
29 
30 static void printPassMessage(const StringRef &Name, int PassNum,
31                              StringRef TargetDesc, bool Running) {
32   StringRef Status = Running ? "" : "NOT ";
33   errs() << "BISECT: " << Status << "running pass "
34          << "(" << PassNum << ") " << Name << " on " << TargetDesc << "\n";
35 }
36 
37 bool OptBisect::shouldRunPass(const Pass *P, StringRef IRDescription) {
38   assert(isEnabled());
39 
40   return checkPass(P->getPassName(), IRDescription);
41 }
42 
43 bool OptBisect::checkPass(const StringRef PassName,
44                           const StringRef TargetDesc) {
45   assert(isEnabled());
46 
47   int CurBisectNum = ++LastBisectNum;
48   bool ShouldRun = (BisectLimit == -1 || CurBisectNum <= BisectLimit);
49   printPassMessage(PassName, CurBisectNum, TargetDesc, ShouldRun);
50   return ShouldRun;
51 }
52 
53 const int OptBisect::Disabled;
54 
55 OptBisect &llvm::getOptBisector() {
56   static OptBisect OptBisector;
57   return OptBisector;
58 }
59