1 //===------- llvm/IR/OptBisect/Bisect.cpp - LLVM Bisect support --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// This file implements support for a bisecting optimizations based on a
12 /// command line option.
14 //===----------------------------------------------------------------------===//
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"
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"));
31 OptBisect::OptBisect() {
32 BisectEnabled = OptBisectLimit != INT_MAX;
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";
42 static void printCaseMessage(int CaseNum, StringRef Msg, bool Running) {
44 errs() << "BISECT: running case (";
46 errs() << "BISECT: NOT running case (";
47 errs() << CaseNum << "): " << Msg << "\n";
50 static std::string getDescription(const Module &M) {
51 return "module (" + M.getName().str() + ")";
54 static std::string getDescription(const Function &F) {
55 return "function (" + F.getName().str() + ")";
58 static std::string getDescription(const BasicBlock &BB) {
59 return "basic block (" + BB.getName().str() + ") in function (" +
60 BB.getParent()->getName().str() + ")";
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
70 static std::string getDescription(const CallGraphSCC &SCC) {
71 std::string Desc = "SCC (";
73 for (CallGraphNode *CGN : SCC) {
78 Function *F = CGN->getFunction();
82 Desc += "<<null function>>";
88 // Force instantiations.
89 template bool OptBisect::shouldRunPass(const Pass *, const Module &);
90 template bool OptBisect::shouldRunPass(const Pass *, const Function &);
91 template bool OptBisect::shouldRunPass(const Pass *, const BasicBlock &);
92 template bool OptBisect::shouldRunPass(const Pass *, const Loop &);
93 template bool OptBisect::shouldRunPass(const Pass *, const CallGraphSCC &);
95 template <class UnitT>
96 bool OptBisect::shouldRunPass(const Pass *P, const UnitT &U) {
99 return checkPass(P->getPassName(), getDescription(U));
102 bool OptBisect::checkPass(const StringRef PassName,
103 const StringRef TargetDesc) {
104 assert(BisectEnabled);
106 int CurBisectNum = ++LastBisectNum;
107 bool ShouldRun = (OptBisectLimit == -1 || CurBisectNum <= OptBisectLimit);
108 printPassMessage(PassName, CurBisectNum, TargetDesc, ShouldRun);
112 bool OptBisect::shouldRunCase(const Twine &Msg) {
115 int CurFuelNum = ++LastBisectNum;
116 bool ShouldRun = (OptBisectLimit == -1 || CurFuelNum <= OptBisectLimit);
117 printCaseMessage(CurFuelNum, Msg.str(), ShouldRun);