xref: /llvm-project/llvm/lib/IR/OptBisect.cpp (revision f0f279291c7ca1a0b2c125f53cd08deafcc9e44f)
1 //===------- llvm/IR/OptBisect/Bisect.cpp - LLVM Bisect support --------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// This file implements support for a bisecting optimizations based on a
12 /// command line option.
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #include "llvm/Analysis/CallGraphSCCPass.h"
17 #include "llvm/Analysis/LazyCallGraph.h"
18 #include "llvm/Analysis/LoopInfo.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/IR/OptBisect.h"
21 #include "llvm/Pass.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/raw_ostream.h"
24 
25 using namespace llvm;
26 
27 static cl::opt<int> OptBisectLimit("opt-bisect-limit", cl::Hidden,
28                                    cl::init(INT_MAX), cl::Optional,
29                                    cl::desc("Maximum optimization to perform"));
30 
31 OptBisect::OptBisect() {
32   BisectEnabled = OptBisectLimit != INT_MAX;
33 }
34 
35 static void printPassMessage(const StringRef &Name, int PassNum,
36                              StringRef TargetDesc, bool Running) {
37   StringRef Status = Running ? "" : "NOT ";
38   errs() << "BISECT: " << Status << "running pass "
39          << "(" << PassNum << ") " << Name << " on " << TargetDesc << "\n";
40 }
41 
42 static void printCaseMessage(int CaseNum, StringRef Msg, bool Running) {
43   if (Running)
44     errs() << "BISECT: running case (";
45   else
46     errs() << "BISECT: NOT running case (";
47   errs() << CaseNum << "): " << Msg << "\n";
48 }
49 
50 static std::string getDescription(const Module &M) {
51   return "module (" + M.getName().str() + ")";
52 }
53 
54 static std::string getDescription(const Function &F) {
55   return "function (" + F.getName().str() + ")";
56 }
57 
58 static std::string getDescription(const BasicBlock &BB) {
59   return "basic block (" + BB.getName().str() + ") in function (" +
60          BB.getParent()->getName().str() + ")";
61 }
62 
63 static std::string getDescription(const Loop &L) {
64   // FIXME: I'd like to be able to provide a better description here, but
65   //        calling L->getHeader() would introduce a new dependency on the
66   //        LLVMCore library.
67   return "loop";
68 }
69 
70 static std::string getDescription(const CallGraphSCC &SCC) {
71   std::string Desc = "SCC (";
72   bool First = true;
73   for (CallGraphNode *CGN : SCC) {
74     if (First)
75       First = false;
76     else
77       Desc += ", ";
78     Function *F = CGN->getFunction();
79     if (F)
80       Desc += F->getName();
81     else
82       Desc += "<<null function>>";
83   }
84   Desc += ")";
85   return Desc;
86 }
87 
88 static std::string getDescription(const LazyCallGraph::SCC &SCC) {
89   std::string Desc = "SCC (";
90   bool First = true;
91   for (LazyCallGraph::Node &CGN : SCC) {
92     if (First)
93       First = false;
94     else
95       Desc += ", ";
96     Function &F = CGN.getFunction();
97     Desc += F.getName();
98   }
99   Desc += ")";
100   return Desc;
101 }
102 
103 // Force instantiations.
104 template bool OptBisect::shouldRunPass(const Pass *, const Module &);
105 template bool OptBisect::shouldRunPass(const Pass *, const Function &);
106 template bool OptBisect::shouldRunPass(const Pass *, const BasicBlock &);
107 template bool OptBisect::shouldRunPass(const Pass *, const Loop &);
108 template bool OptBisect::shouldRunPass(const Pass *, const CallGraphSCC &);
109 template bool OptBisect::shouldRunPass(const StringRef PassName,
110                                        const Module &);
111 template bool OptBisect::shouldRunPass(const StringRef PassName,
112                                        const Function &);
113 template bool OptBisect::shouldRunPass(const StringRef PassName,
114                                        const BasicBlock &);
115 template bool OptBisect::shouldRunPass(const StringRef PassName, const Loop &);
116 template bool OptBisect::shouldRunPass(const StringRef PassName,
117                                        const LazyCallGraph::SCC &);
118 
119 template <class UnitT>
120 bool OptBisect::shouldRunPass(const Pass *P, const UnitT &U) {
121   if (!BisectEnabled)
122     return true;
123   return checkPass(P->getPassName(), getDescription(U));
124 }
125 
126 // Interface function for the new pass manager.
127 template <class UnitT>
128 bool OptBisect::shouldRunPass(const StringRef PassName, const UnitT &U) {
129   if (!BisectEnabled)
130     return true;
131   return checkPass(PassName, getDescription(U));
132 }
133 
134 bool OptBisect::checkPass(const StringRef PassName,
135                           const StringRef TargetDesc) {
136   assert(BisectEnabled);
137 
138   int CurBisectNum = ++LastBisectNum;
139   bool ShouldRun = (OptBisectLimit == -1 || CurBisectNum <= OptBisectLimit);
140   printPassMessage(PassName, CurBisectNum, TargetDesc, ShouldRun);
141   return ShouldRun;
142 }
143 
144 bool OptBisect::shouldRunCase(const Twine &Msg) {
145   if (!BisectEnabled)
146     return true;
147   int CurFuelNum = ++LastBisectNum;
148   bool ShouldRun = (OptBisectLimit == -1 || CurFuelNum <= OptBisectLimit);
149   printCaseMessage(CurFuelNum, Msg.str(), ShouldRun);
150   return ShouldRun;
151 }
152 
153 bool llvm::skipPassForModule(const StringRef PassName, const Module &M) {
154   return !M.getContext().getOptBisect().shouldRunPass(PassName, M);
155 }
156 
157 bool llvm::skipPassForFunction(const StringRef PassName, const Function &F) {
158   return !F.getContext().getOptBisect().shouldRunPass(PassName, F);
159 }
160 #if 0
161 bool llvm::skipPassForBasicBlock(const StringRef PassName, const BasicBlock &BB) {
162   return !BB.getContext().getOptBisect().shouldRunPass(PassName, BB);
163 }
164 
165 bool llvm::skipPassForLoop(const StringRef PassName, const Loop &L) {
166   const Function *F = L.getHeader()->getParent();
167   if (!F)
168     return false;
169   return !F->getContext().getOptBisect().shouldRunPass(PassName, L);
170 }
171 #endif
172 bool llvm::skipPassForSCC(const StringRef PassName, const LazyCallGraph::SCC &SCC) {
173   LLVMContext &Context = SCC.begin()->getFunction().getContext();
174   return !Context.getOptBisect().shouldRunPass(PassName, SCC);
175 }
176 
177