blob: f1c70058fac2c1257732678413f10cbd3e73bcef [file] [log] [blame]
Andrew Kayloraa641a52016-04-22 22:06:11 +00001//===------- 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
Chandler Carruth6bda14b2017-06-06 11:49:48 +000016#include "llvm/IR/OptBisect.h"
Andrew Kayloraa641a52016-04-22 22:06:11 +000017#include "llvm/Analysis/CallGraphSCCPass.h"
18#include "llvm/Analysis/LazyCallGraph.h"
19#include "llvm/Analysis/LoopInfo.h"
Eli Friedman0d823d62017-06-01 21:22:26 +000020#include "llvm/Analysis/RegionInfo.h"
Andrew Kayloraa641a52016-04-22 22:06:11 +000021#include "llvm/IR/Module.h"
Andrew Kayloraa641a52016-04-22 22:06:11 +000022#include "llvm/Pass.h"
23#include "llvm/Support/CommandLine.h"
24#include "llvm/Support/raw_ostream.h"
25
26using namespace llvm;
27
28static cl::opt<int> OptBisectLimit("opt-bisect-limit", cl::Hidden,
29 cl::init(INT_MAX), cl::Optional,
30 cl::desc("Maximum optimization to perform"));
31
32OptBisect::OptBisect() {
33 BisectEnabled = OptBisectLimit != INT_MAX;
34}
35
36static 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";
41}
42
Andrew Kayloraa641a52016-04-22 22:06:11 +000043static std::string getDescription(const Module &M) {
44 return "module (" + M.getName().str() + ")";
45}
46
47static std::string getDescription(const Function &F) {
48 return "function (" + F.getName().str() + ")";
49}
50
51static std::string getDescription(const BasicBlock &BB) {
52 return "basic block (" + BB.getName().str() + ") in function (" +
53 BB.getParent()->getName().str() + ")";
54}
55
56static std::string getDescription(const Loop &L) {
Eli Friedman0d823d62017-06-01 21:22:26 +000057 // FIXME: Move into LoopInfo so we can get a better description
58 // (and avoid a circular dependency between IR and Analysis).
Andrew Kayloraa641a52016-04-22 22:06:11 +000059 return "loop";
60}
61
Eli Friedman0d823d62017-06-01 21:22:26 +000062static 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).
65 return "region";
66}
67
Andrew Kayloraa641a52016-04-22 22:06:11 +000068static std::string getDescription(const CallGraphSCC &SCC) {
Eli Friedman0d823d62017-06-01 21:22:26 +000069 // FIXME: Move into CallGraphSCCPass to avoid circular dependency between
70 // IR and Analysis.
Andrew Kayloraa641a52016-04-22 22:06:11 +000071 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
Andrew Kayloraa641a52016-04-22 22:06:11 +000088// Force instantiations.
89template bool OptBisect::shouldRunPass(const Pass *, const Module &);
90template bool OptBisect::shouldRunPass(const Pass *, const Function &);
91template bool OptBisect::shouldRunPass(const Pass *, const BasicBlock &);
92template bool OptBisect::shouldRunPass(const Pass *, const Loop &);
93template bool OptBisect::shouldRunPass(const Pass *, const CallGraphSCC &);
Eli Friedman0d823d62017-06-01 21:22:26 +000094template bool OptBisect::shouldRunPass(const Pass *, const Region &);
Andrew Kayloraa641a52016-04-22 22:06:11 +000095
96template <class UnitT>
97bool OptBisect::shouldRunPass(const Pass *P, const UnitT &U) {
98 if (!BisectEnabled)
99 return true;
100 return checkPass(P->getPassName(), getDescription(U));
101}
102
103bool OptBisect::checkPass(const StringRef PassName,
104 const StringRef TargetDesc) {
105 assert(BisectEnabled);
106
107 int CurBisectNum = ++LastBisectNum;
108 bool ShouldRun = (OptBisectLimit == -1 || CurBisectNum <= OptBisectLimit);
109 printPassMessage(PassName, CurBisectNum, TargetDesc, ShouldRun);
110 return ShouldRun;
111}