LLVM  9.0.0svn
OptBisect.cpp
Go to the documentation of this file.
1 //===- llvm/IR/OptBisect/Bisect.cpp - LLVM Bisect support -----------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 /// \file
10 /// This file implements support for a bisecting optimizations based on a
11 /// command line option.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/IR/OptBisect.h"
16 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Analysis/LoopInfo.h"
21 #include "llvm/IR/BasicBlock.h"
22 #include "llvm/IR/Function.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/Pass.h"
27 #include <cassert>
28 #include <limits>
29 #include <string>
30 
31 using namespace llvm;
32 
33 static cl::opt<int> OptBisectLimit("opt-bisect-limit", cl::Hidden,
36  cl::desc("Maximum optimization to perform"));
37 
39  BisectEnabled = OptBisectLimit != std::numeric_limits<int>::max();
40 }
41 
42 static void printPassMessage(const StringRef &Name, int PassNum,
43  StringRef TargetDesc, bool Running) {
44  StringRef Status = Running ? "" : "NOT ";
45  errs() << "BISECT: " << Status << "running pass "
46  << "(" << PassNum << ") " << Name << " on " << TargetDesc << "\n";
47 }
48 
49 static std::string getDescription(const Module &M) {
50  return "module (" + M.getName().str() + ")";
51 }
52 
53 static std::string getDescription(const Function &F) {
54  return "function (" + F.getName().str() + ")";
55 }
56 
57 static std::string getDescription(const BasicBlock &BB) {
58  return "basic block (" + BB.getName().str() + ") in function (" +
59  BB.getParent()->getName().str() + ")";
60 }
61 
62 static std::string getDescription(const Loop &L) {
63  // FIXME: Move into LoopInfo so we can get a better description
64  // (and avoid a circular dependency between IR and Analysis).
65  return "loop";
66 }
67 
68 static std::string getDescription(const Region &R) {
69  // FIXME: Move into RegionInfo so we can get a better description
70  // (and avoid a circular dependency between IR and Analysis).
71  return "region";
72 }
73 
74 static std::string getDescription(const CallGraphSCC &SCC) {
75  // FIXME: Move into CallGraphSCCPass to avoid circular dependency between
76  // IR and Analysis.
77  std::string Desc = "SCC (";
78  bool First = true;
79  for (CallGraphNode *CGN : SCC) {
80  if (First)
81  First = false;
82  else
83  Desc += ", ";
84  Function *F = CGN->getFunction();
85  if (F)
86  Desc += F->getName();
87  else
88  Desc += "<<null function>>";
89  }
90  Desc += ")";
91  return Desc;
92 }
93 
94 bool OptBisect::shouldRunPass(const Pass *P, const Module &U) {
95  return !BisectEnabled || checkPass(P->getPassName(), getDescription(U));
96 }
97 
98 bool OptBisect::shouldRunPass(const Pass *P, const Function &U) {
99  return !BisectEnabled || checkPass(P->getPassName(), getDescription(U));
100 }
101 
102 bool OptBisect::shouldRunPass(const Pass *P, const BasicBlock &U) {
103  return !BisectEnabled || checkPass(P->getPassName(), getDescription(U));
104 }
105 
106 bool OptBisect::shouldRunPass(const Pass *P, const Region &U) {
107  return !BisectEnabled || checkPass(P->getPassName(), getDescription(U));
108 }
109 
110 bool OptBisect::shouldRunPass(const Pass *P, const Loop &U) {
111  return !BisectEnabled || checkPass(P->getPassName(), getDescription(U));
112 }
113 
114 bool OptBisect::shouldRunPass(const Pass *P, const CallGraphSCC &U) {
115  return !BisectEnabled || checkPass(P->getPassName(), getDescription(U));
116 }
117 
118 bool OptBisect::checkPass(const StringRef PassName,
119  const StringRef TargetDesc) {
120  assert(BisectEnabled);
121 
122  int CurBisectNum = ++LastBisectNum;
123  bool ShouldRun = (OptBisectLimit == -1 || CurBisectNum <= OptBisectLimit);
124  printPassMessage(PassName, CurBisectNum, TargetDesc, ShouldRun);
125  return ShouldRun;
126 }
Pass interface - Implemented by all &#39;passes&#39;.
Definition: Pass.h:80
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:218
This class represents lattice values for constants.
Definition: AllocatorList.h:23
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:64
Extensions to this class implement mechanisms to disable passes and individual optimizations at compi...
Definition: OptBisect.h:31
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
StringRef getName() const
Get a short "name" for the module.
Definition: Module.h:226
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:164
bool shouldRunPass(const Pass *P, const Module &U) override
Checks the bisect limit to determine if the specified pass should run.
Definition: OptBisect.cpp:94
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:42
#define P(N)
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:422
LLVM Basic Block Representation.
Definition: BasicBlock.h:57
OptBisect()
Default constructor, initializes the OptBisect state based on the -opt-bisect-limit command line argu...
Definition: OptBisect.cpp:38
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:49
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:464
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:214
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:106
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:48