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/IR/OptBisect.h"
17 #include "llvm/Analysis/CallGraphSCCPass.h"
18 #include "llvm/Analysis/LazyCallGraph.h"
19 #include "llvm/Analysis/LoopInfo.h"
20 #include "llvm/Analysis/RegionInfo.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/Pass.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/raw_ostream.h"
28 static cl::opt<int> OptBisectLimit("opt-bisect-limit", cl::Hidden,
29 cl::init(INT_MAX), cl::Optional,
30 cl::desc("Maximum optimization to perform"));
32 OptBisect::OptBisect() {
33 BisectEnabled = OptBisectLimit != INT_MAX;
36 static void printPassMessage(const StringRef &Name, int PassNum,
37 StringRef TargetDesc, bool Running) {
38 StringRef Status = Running ? "" : "NOT ";
39 errs() << "BISECT: " << Status << "running pass "
40 << "(" << PassNum << ") " << Name << " on " << TargetDesc << "\n";
43 static std::string getDescription(const Module &M) {
44 return "module (" + M.getName().str() + ")";
47 static std::string getDescription(const Function &F) {
48 return "function (" + F.getName().str() + ")";
51 static std::string getDescription(const BasicBlock &BB) {
52 return "basic block (" + BB.getName().str() + ") in function (" +
53 BB.getParent()->getName().str() + ")";
56 static std::string getDescription(const Loop &L) {
57 // FIXME: Move into LoopInfo so we can get a better description
58 // (and avoid a circular dependency between IR and Analysis).
62 static std::string getDescription(const Region &R) {
63 // FIXME: Move into RegionInfo so we can get a better description
64 // (and avoid a circular dependency between IR and Analysis).
68 static std::string getDescription(const CallGraphSCC &SCC) {
69 // FIXME: Move into CallGraphSCCPass to avoid circular dependency between
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 &);
94 template bool OptBisect::shouldRunPass(const Pass *, const Region &);
96 template <class UnitT>
97 bool OptBisect::shouldRunPass(const Pass *P, const UnitT &U) {
100 return checkPass(P->getPassName(), getDescription(U));
103 bool OptBisect::checkPass(const StringRef PassName,
104 const StringRef TargetDesc) {
105 assert(BisectEnabled);
107 int CurBisectNum = ++LastBisectNum;
108 bool ShouldRun = (OptBisectLimit == -1 || CurBisectNum <= OptBisectLimit);
109 printPassMessage(PassName, CurBisectNum, TargetDesc, ShouldRun);