blob: dc7dcd2e4a97c758f5d4cd21f82e965a16781182 [file] [log] [blame]
Eugene Zelenko3e561692017-08-31 22:06:09 +00001//===- llvm/IR/OptBisect/Bisect.cpp - LLVM Bisect support -----------------===//
Andrew Kayloraa641a52016-04-22 22:06:11 +00002//
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//===----------------------------------------------------------------------===//
Eugene Zelenko3e561692017-08-31 22:06:09 +00009//
Andrew Kayloraa641a52016-04-22 22:06:11 +000010/// \file
11/// This file implements support for a bisecting optimizations based on a
12/// command line option.
Eugene Zelenko3e561692017-08-31 22:06:09 +000013//
Andrew Kayloraa641a52016-04-22 22:06:11 +000014//===----------------------------------------------------------------------===//
15
Chandler Carruth6bda14b2017-06-06 11:49:48 +000016#include "llvm/IR/OptBisect.h"
Eugene Zelenko3e561692017-08-31 22:06:09 +000017#include "llvm/ADT/StringRef.h"
18#include "llvm/Analysis/CallGraph.h"
Andrew Kayloraa641a52016-04-22 22:06:11 +000019#include "llvm/Analysis/CallGraphSCCPass.h"
Andrew Kayloraa641a52016-04-22 22:06:11 +000020#include "llvm/Analysis/LoopInfo.h"
Eli Friedman0d823d62017-06-01 21:22:26 +000021#include "llvm/Analysis/RegionInfo.h"
Eugene Zelenko3e561692017-08-31 22:06:09 +000022#include "llvm/IR/BasicBlock.h"
23#include "llvm/IR/Function.h"
Andrew Kayloraa641a52016-04-22 22:06:11 +000024#include "llvm/IR/Module.h"
Andrew Kayloraa641a52016-04-22 22:06:11 +000025#include "llvm/Pass.h"
26#include "llvm/Support/CommandLine.h"
27#include "llvm/Support/raw_ostream.h"
Eugene Zelenko3e561692017-08-31 22:06:09 +000028#include <cassert>
29#include <limits>
30#include <string>
Andrew Kayloraa641a52016-04-22 22:06:11 +000031
32using namespace llvm;
33
34static cl::opt<int> OptBisectLimit("opt-bisect-limit", cl::Hidden,
Eugene Zelenko3e561692017-08-31 22:06:09 +000035 cl::init(std::numeric_limits<int>::max()),
36 cl::Optional,
Andrew Kayloraa641a52016-04-22 22:06:11 +000037 cl::desc("Maximum optimization to perform"));
38
39OptBisect::OptBisect() {
Eugene Zelenko3e561692017-08-31 22:06:09 +000040 BisectEnabled = OptBisectLimit != std::numeric_limits<int>::max();
Andrew Kayloraa641a52016-04-22 22:06:11 +000041}
42
43static void printPassMessage(const StringRef &Name, int PassNum,
44 StringRef TargetDesc, bool Running) {
45 StringRef Status = Running ? "" : "NOT ";
46 errs() << "BISECT: " << Status << "running pass "
47 << "(" << PassNum << ") " << Name << " on " << TargetDesc << "\n";
48}
49
Andrew Kayloraa641a52016-04-22 22:06:11 +000050static std::string getDescription(const Module &M) {
51 return "module (" + M.getName().str() + ")";
52}
53
54static std::string getDescription(const Function &F) {
55 return "function (" + F.getName().str() + ")";
56}
57
58static std::string getDescription(const BasicBlock &BB) {
59 return "basic block (" + BB.getName().str() + ") in function (" +
60 BB.getParent()->getName().str() + ")";
61}
62
63static std::string getDescription(const Loop &L) {
Eli Friedman0d823d62017-06-01 21:22:26 +000064 // FIXME: Move into LoopInfo so we can get a better description
65 // (and avoid a circular dependency between IR and Analysis).
Andrew Kayloraa641a52016-04-22 22:06:11 +000066 return "loop";
67}
68
Eli Friedman0d823d62017-06-01 21:22:26 +000069static std::string getDescription(const Region &R) {
70 // FIXME: Move into RegionInfo so we can get a better description
71 // (and avoid a circular dependency between IR and Analysis).
72 return "region";
73}
74
Andrew Kayloraa641a52016-04-22 22:06:11 +000075static std::string getDescription(const CallGraphSCC &SCC) {
Eli Friedman0d823d62017-06-01 21:22:26 +000076 // FIXME: Move into CallGraphSCCPass to avoid circular dependency between
77 // IR and Analysis.
Andrew Kayloraa641a52016-04-22 22:06:11 +000078 std::string Desc = "SCC (";
79 bool First = true;
80 for (CallGraphNode *CGN : SCC) {
81 if (First)
82 First = false;
83 else
84 Desc += ", ";
85 Function *F = CGN->getFunction();
86 if (F)
87 Desc += F->getName();
88 else
89 Desc += "<<null function>>";
90 }
91 Desc += ")";
92 return Desc;
93}
94
Andrew Kayloraa641a52016-04-22 22:06:11 +000095// Force instantiations.
96template bool OptBisect::shouldRunPass(const Pass *, const Module &);
97template bool OptBisect::shouldRunPass(const Pass *, const Function &);
98template bool OptBisect::shouldRunPass(const Pass *, const BasicBlock &);
99template bool OptBisect::shouldRunPass(const Pass *, const Loop &);
100template bool OptBisect::shouldRunPass(const Pass *, const CallGraphSCC &);
Eli Friedman0d823d62017-06-01 21:22:26 +0000101template bool OptBisect::shouldRunPass(const Pass *, const Region &);
Andrew Kayloraa641a52016-04-22 22:06:11 +0000102
103template <class UnitT>
104bool OptBisect::shouldRunPass(const Pass *P, const UnitT &U) {
105 if (!BisectEnabled)
106 return true;
107 return checkPass(P->getPassName(), getDescription(U));
108}
109
110bool OptBisect::checkPass(const StringRef PassName,
111 const StringRef TargetDesc) {
112 assert(BisectEnabled);
113
114 int CurBisectNum = ++LastBisectNum;
115 bool ShouldRun = (OptBisectLimit == -1 || CurBisectNum <= OptBisectLimit);
116 printPassMessage(PassName, CurBisectNum, TargetDesc, ShouldRun);
117 return ShouldRun;
118}