13e561694SEugene Zelenko //===- llvm/IR/OptBisect/Bisect.cpp - LLVM Bisect support -----------------===//
2aa641a51SAndrew Kaylor //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6aa641a51SAndrew Kaylor //
7aa641a51SAndrew Kaylor //===----------------------------------------------------------------------===//
83e561694SEugene Zelenko //
9aa641a51SAndrew Kaylor /// \file
10aa641a51SAndrew Kaylor /// This file implements support for a bisecting optimizations based on a
11aa641a51SAndrew Kaylor /// command line option.
123e561694SEugene Zelenko //
13aa641a51SAndrew Kaylor //===----------------------------------------------------------------------===//
14aa641a51SAndrew Kaylor 
156bda14b3SChandler Carruth #include "llvm/IR/OptBisect.h"
16aa641a51SAndrew Kaylor #include "llvm/Pass.h"
17aa641a51SAndrew Kaylor #include "llvm/Support/CommandLine.h"
18aa641a51SAndrew Kaylor #include "llvm/Support/raw_ostream.h"
193e561694SEugene Zelenko #include <cassert>
20aa641a51SAndrew Kaylor 
21aa641a51SAndrew Kaylor using namespace llvm;
22aa641a51SAndrew Kaylor 
23aa641a51SAndrew Kaylor static cl::opt<int> OptBisectLimit("opt-bisect-limit", cl::Hidden,
24d0c3b614SKrzysztof Parzyszek                                    cl::init(OptBisect::Disabled), cl::Optional,
__anon2e59b3b70102(int Limit) 25d0c3b614SKrzysztof Parzyszek                                    cl::cb<void, int>([](int Limit) {
26*ede60037SNicolai Hähnle                                      llvm::getOptBisector().setLimit(Limit);
27d0c3b614SKrzysztof Parzyszek                                    }),
28aa641a51SAndrew Kaylor                                    cl::desc("Maximum optimization to perform"));
29aa641a51SAndrew Kaylor 
printPassMessage(const StringRef & Name,int PassNum,StringRef TargetDesc,bool Running)30aa641a51SAndrew Kaylor static void printPassMessage(const StringRef &Name, int PassNum,
31aa641a51SAndrew Kaylor                              StringRef TargetDesc, bool Running) {
32aa641a51SAndrew Kaylor   StringRef Status = Running ? "" : "NOT ";
33aa641a51SAndrew Kaylor   errs() << "BISECT: " << Status << "running pass "
34aa641a51SAndrew Kaylor          << "(" << PassNum << ") " << Name << " on " << TargetDesc << "\n";
35aa641a51SAndrew Kaylor }
36aa641a51SAndrew Kaylor 
shouldRunPass(const Pass * P,StringRef IRDescription)37b37a70f4SRichard Trieu bool OptBisect::shouldRunPass(const Pass *P, StringRef IRDescription) {
38d0c3b614SKrzysztof Parzyszek   assert(isEnabled());
39aa641a51SAndrew Kaylor 
40b37a70f4SRichard Trieu   return checkPass(P->getPassName(), IRDescription);
41aa641a51SAndrew Kaylor }
42aa641a51SAndrew Kaylor 
checkPass(const StringRef PassName,const StringRef TargetDesc)43aa641a51SAndrew Kaylor bool OptBisect::checkPass(const StringRef PassName,
44aa641a51SAndrew Kaylor                           const StringRef TargetDesc) {
45d0c3b614SKrzysztof Parzyszek   assert(isEnabled());
46aa641a51SAndrew Kaylor 
47aa641a51SAndrew Kaylor   int CurBisectNum = ++LastBisectNum;
48d0c3b614SKrzysztof Parzyszek   bool ShouldRun = (BisectLimit == -1 || CurBisectNum <= BisectLimit);
49aa641a51SAndrew Kaylor   printPassMessage(PassName, CurBisectNum, TargetDesc, ShouldRun);
50aa641a51SAndrew Kaylor   return ShouldRun;
51aa641a51SAndrew Kaylor }
5247dbee67SSamuel Eubanks 
53d0c3b614SKrzysztof Parzyszek const int OptBisect::Disabled;
54d0c3b614SKrzysztof Parzyszek 
getOptBisector()55*ede60037SNicolai Hähnle OptBisect &llvm::getOptBisector() {
56*ede60037SNicolai Hähnle   static OptBisect OptBisector;
57*ede60037SNicolai Hähnle   return OptBisector;
58*ede60037SNicolai Hähnle }
59