LLVM  6.0.0svn
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"
17 #include "llvm/ADT/StringRef.h"
20 #include "llvm/Analysis/LoopInfo.h"
22 #include "llvm/IR/BasicBlock.h"
23 #include "llvm/IR/Function.h"
24 #include "llvm/IR/Module.h"
25 #include "llvm/Pass.h"
28 #include <cassert>
29 #include <limits>
30 #include <string>
31 
32 using namespace llvm;
33 
34 static cl::opt<int> OptBisectLimit("opt-bisect-limit", cl::Hidden,
37  cl::desc("Maximum optimization to perform"));
38 
40  BisectEnabled = OptBisectLimit != std::numeric_limits<int>::max();
41 }
42 
43 static 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 
50 static std::string getDescription(const Module &M) {
51  return "module (" + M.getName().str() + ")";
52 }
53 
54 static std::string getDescription(const Function &F) {
55  return "function (" + F.getName().str() + ")";
56 }
57 
58 static std::string getDescription(const BasicBlock &BB) {
59  return "basic block (" + BB.getName().str() + ") in function (" +
60  BB.getParent()->getName().str() + ")";
61 }
62 
63 static std::string getDescription(const Loop &L) {
64  // FIXME: Move into LoopInfo so we can get a better description
65  // (and avoid a circular dependency between IR and Analysis).
66  return "loop";
67 }
68 
69 static 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 
75 static std::string getDescription(const CallGraphSCC &SCC) {
76  // FIXME: Move into CallGraphSCCPass to avoid circular dependency between
77  // IR and Analysis.
78  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 
95 // Force instantiations.
96 template bool OptBisect::shouldRunPass(const Pass *, const Module &);
97 template bool OptBisect::shouldRunPass(const Pass *, const Function &);
98 template bool OptBisect::shouldRunPass(const Pass *, const BasicBlock &);
99 template bool OptBisect::shouldRunPass(const Pass *, const Loop &);
100 template bool OptBisect::shouldRunPass(const Pass *, const CallGraphSCC &);
101 template bool OptBisect::shouldRunPass(const Pass *, const Region &);
102 
103 template <class UnitT>
104 bool OptBisect::shouldRunPass(const Pass *P, const UnitT &U) {
105  if (!BisectEnabled)
106  return true;
107  return checkPass(P->getPassName(), getDescription(U));
108 }
109 
110 bool 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 }
Pass interface - Implemented by all &#39;passes&#39;.
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:104
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:228
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
StringRef getName() const
Get a short "name" for the module.
Definition: Module.h:220
F(f)
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:74
A node in the call graph for a module.
Definition: CallGraph.h:165
This file provides interfaces used to build and manipulate a call graph, which is a very useful tool ...
static void printPassMessage(const StringRef &Name, int PassNum, StringRef TargetDesc, bool Running)
Definition: OptBisect.cpp:43
#define P(N)
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:406
LLVM Basic Block Representation.
Definition: BasicBlock.h:59
OptBisect()
Default constructor, initializes the OptBisect state based on the -opt-bisect-limit command line argu...
Definition: OptBisect.cpp:39
static cl::opt< int > OptBisectLimit("opt-bisect-limit", cl::Hidden, cl::init(std::numeric_limits< int >::max()), cl::Optional, cl::desc("Maximum optimization to perform"))
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:50
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:439
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:220
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:108
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