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