1 //===- llvm/IR/OptBisect/Bisect.cpp - LLVM Bisect support -----------------===//
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
7 //===----------------------------------------------------------------------===//
10 /// This file implements support for a bisecting optimizations based on a
11 /// command line option.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/IR/OptBisect.h"
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/Analysis/CallGraph.h"
18 #include "llvm/Analysis/CallGraphSCCPass.h"
19 #include "llvm/Analysis/LoopInfo.h"
20 #include "llvm/Analysis/RegionInfo.h"
21 #include "llvm/IR/BasicBlock.h"
22 #include "llvm/IR/Function.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/Pass.h"
25 #include "llvm/Support/CommandLine.h"
26 #include "llvm/Support/raw_ostream.h"
33 static cl::opt
<int> OptBisectLimit("opt-bisect-limit", cl::Hidden
,
34 cl::init(std::numeric_limits
<int>::max()),
36 cl::desc("Maximum optimization to perform"));
38 OptBisect::OptBisect() : OptPassGate() {
39 BisectEnabled
= OptBisectLimit
!= std::numeric_limits
<int>::max();
42 static void printPassMessage(const StringRef
&Name
, int PassNum
,
43 StringRef TargetDesc
, bool Running
) {
44 StringRef Status
= Running
? "" : "NOT ";
45 errs() << "BISECT: " << Status
<< "running pass "
46 << "(" << PassNum
<< ") " << Name
<< " on " << TargetDesc
<< "\n";
49 static std::string
getDescription(const Module
&M
) {
50 return "module (" + M
.getName().str() + ")";
53 static std::string
getDescription(const Function
&F
) {
54 return "function (" + F
.getName().str() + ")";
57 static std::string
getDescription(const BasicBlock
&BB
) {
58 return "basic block (" + BB
.getName().str() + ") in function (" +
59 BB
.getParent()->getName().str() + ")";
62 static std::string
getDescription(const Loop
&L
) {
63 // FIXME: Move into LoopInfo so we can get a better description
64 // (and avoid a circular dependency between IR and Analysis).
68 static std::string
getDescription(const Region
&R
) {
69 // FIXME: Move into RegionInfo so we can get a better description
70 // (and avoid a circular dependency between IR and Analysis).
74 static std::string
getDescription(const CallGraphSCC
&SCC
) {
75 // FIXME: Move into CallGraphSCCPass to avoid circular dependency between
77 std::string Desc
= "SCC (";
79 for (CallGraphNode
*CGN
: SCC
) {
84 Function
*F
= CGN
->getFunction();
88 Desc
+= "<<null function>>";
94 bool OptBisect::shouldRunPass(const Pass
*P
, const Module
&U
) {
95 return !BisectEnabled
|| checkPass(P
->getPassName(), getDescription(U
));
98 bool OptBisect::shouldRunPass(const Pass
*P
, const Function
&U
) {
99 return !BisectEnabled
|| checkPass(P
->getPassName(), getDescription(U
));
102 bool OptBisect::shouldRunPass(const Pass
*P
, const BasicBlock
&U
) {
103 return !BisectEnabled
|| checkPass(P
->getPassName(), getDescription(U
));
106 bool OptBisect::shouldRunPass(const Pass
*P
, const Region
&U
) {
107 return !BisectEnabled
|| checkPass(P
->getPassName(), getDescription(U
));
110 bool OptBisect::shouldRunPass(const Pass
*P
, const Loop
&U
) {
111 return !BisectEnabled
|| checkPass(P
->getPassName(), getDescription(U
));
114 bool OptBisect::shouldRunPass(const Pass
*P
, const CallGraphSCC
&U
) {
115 return !BisectEnabled
|| checkPass(P
->getPassName(), getDescription(U
));
118 bool OptBisect::checkPass(const StringRef PassName
,
119 const StringRef TargetDesc
) {
120 assert(BisectEnabled
);
122 int CurBisectNum
= ++LastBisectNum
;
123 bool ShouldRun
= (OptBisectLimit
== -1 || CurBisectNum
<= OptBisectLimit
);
124 printPassMessage(PassName
, CurBisectNum
, TargetDesc
, ShouldRun
);