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" 17 #include "llvm/Analysis/CallGraph.h" 18 #include "llvm/Analysis/CallGraphSCCPass.h" 19 #include "llvm/Analysis/LoopInfo.h" 20 #include "llvm/Analysis/RegionInfo.h" 21 #include "llvm/IR/BasicBlock.h" 22 #include "llvm/IR/Function.h" 23 #include "llvm/IR/Module.h" 24 #include "llvm/Pass.h" 25 #include "llvm/Support/CommandLine.h" 26 #include "llvm/Support/raw_ostream.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, 34 cl::init(std::numeric_limits<int>::max()), 35 cl::Optional, 36 cl::desc("Maximum optimization to perform")); 37 38 OptBisect::OptBisect() : OptPassGate() { 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 } 127