LLVM  5.0.0
OptBisect.cpp
Go to the documentation of this file.
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/IR/OptBisect.h"
19 #include "llvm/Analysis/LoopInfo.h"
21 #include "llvm/IR/Module.h"
22 #include "llvm/Pass.h"
25 
26 using namespace llvm;
27 
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"));
31 
33  BisectEnabled = OptBisectLimit != INT_MAX;
34 }
35 
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";
41 }
42 
43 static std::string getDescription(const Module &M) {
44  return "module (" + M.getName().str() + ")";
45 }
46 
47 static std::string getDescription(const Function &F) {
48  return "function (" + F.getName().str() + ")";
49 }
50 
51 static std::string getDescription(const BasicBlock &BB) {
52  return "basic block (" + BB.getName().str() + ") in function (" +
53  BB.getParent()->getName().str() + ")";
54 }
55 
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).
59  return "loop";
60 }
61 
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).
65  return "region";
66 }
67 
68 static std::string getDescription(const CallGraphSCC &SCC) {
69  // FIXME: Move into CallGraphSCCPass to avoid circular dependency between
70  // IR and Analysis.
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 // 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 &);
95 
96 template <class UnitT>
97 bool OptBisect::shouldRunPass(const Pass *P, const UnitT &U) {
98  if (!BisectEnabled)
99  return true;
100  return checkPass(P->getPassName(), getDescription(U));
101 }
102 
103 bool 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 }
MachineLoop * L
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:81
bool shouldRunPass(const Pass *P, const UnitT &U)
Checks the bisect limit to determine if the specified pass should run.
Definition: OptBisect.cpp:97
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
Implements a lazy call graph analysis and related passes for the new pass manager.
MachineInstrBuilder MachineInstrBuilder &DefMI const MCInstrDesc & Desc
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:67
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:108
A node in the call graph for a module.
Definition: CallGraph.h:161
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:218
StringRef getName() const
Get a short "name" for the module.
Definition: Module.h:220
#define F(x, y, z)
Definition: MD5.cpp:55
static void printPassMessage(const StringRef &Name, int PassNum, StringRef TargetDesc, bool Running)
Definition: OptBisect.cpp:36
const char * Name
#define P(N)
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:401
LLVM Basic Block Representation.
Definition: BasicBlock.h:59
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:228
OptBisect()
Default constructor, initializes the OptBisect state based on the -opt-bisect-limit command line argu...
Definition: OptBisect.cpp:32
Module.h This file contains the declarations for the Module class.
This file declares the interface for bisecting optimizations.
static std::string getDescription(const Module &M)
Definition: OptBisect.cpp:43
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:360
static cl::opt< int > OptBisectLimit("opt-bisect-limit", cl::Hidden, cl::init(INT_MAX), cl::Optional, cl::desc("Maximum optimization to perform"))
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
CallGraphSCC - This is a single SCC that a CallGraphSCCPass is run on.
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49