42 #define DEBUG_TYPE "cgscc-passmgr" 47 STATISTIC(MaxSCCIterations,
"Maximum CGSCCPassMgr iterations on one SCC");
64 bool runOnModule(
Module &M)
override;
79 StringRef getPassName()
const override {
return "CallGraph Pass Manager"; }
82 Pass *getAsPass()
override {
return this; }
85 void dumpPassStructure(
unsigned Offset)
override {
86 errs().
indent(Offset*2) <<
"Call Graph SCC Pass Manager\n";
90 dumpLastUses(P, Offset+1);
94 Pass *getContainedPass(
unsigned N) {
95 assert(N < PassVector.size() &&
"Pass number out of range!");
96 return static_cast<Pass *
>(PassVector[
N]);
105 bool &DevirtualizedCall);
109 bool &DevirtualizedCall);
111 bool IsCheckingMode);
120 bool &DevirtualizedCall) {
121 bool Changed =
false;
127 if (!CallGraphUpToDate) {
128 DevirtualizedCall |= RefreshCallGraph(CurSCC, CG,
false);
129 CallGraphUpToDate =
true;
137 InstrCount = initSizeRemarkInfo(M);
143 emitInstrCountChangedRemark(P, M, InstrCount);
150 RefreshCallGraph(CurSCC, CG,
true);
157 "Invalid CGPassManager member");
166 Changed |= FPP->runOnFunction(*
F);
168 F->getContext().yield();
174 if (Changed && CallGraphUpToDate) {
177 CallGraphUpToDate =
false;
198 : CurSCC) CGN->dump(););
200 bool MadeChange =
false;
201 bool DevirtualizedCall =
false;
204 unsigned FunctionNo = 0;
206 SCCIdx !=
E; ++SCCIdx, ++FunctionNo) {
216 unsigned NumDirectRemoved = 0, NumIndirectRemoved = 0;
226 CallSites.
count(
I->first) ||
238 "CallGraphSCCPass did not update the CallGraph correctly!");
241 if (!
I->second->getFunction())
242 ++NumIndirectRemoved;
248 bool WasLast =
I + 1 ==
E;
261 "Call site occurs in node multiple times");
268 CallSites.
insert(std::make_pair(
I->first,
I->second));
275 unsigned NumDirectAdded = 0, NumIndirectAdded = 0;
281 Function *Callee = CS.getCalledFunction();
287 CallSites.
find(CS.getInstruction());
288 if (ExistingIt != CallSites.
end()) {
292 CallSites.
erase(ExistingIt);
295 if (ExistingNode->
getFunction() == CS.getCalledFunction())
303 if (CheckingMode && CS.getCalledFunction() &&
308 "CallGraphSCCPass did not update the CallGraph correctly!");
313 if (
Function *Callee = CS.getCalledFunction()) {
318 DevirtualizedCall =
true;
320 << Callee->
getName() <<
"'\n");
333 "CallGraphSCCPass did not update the CallGraph correctly!");
337 if (
Function *Callee = CS.getCalledFunction()) {
359 if (NumIndirectRemoved > NumIndirectAdded &&
360 NumDirectRemoved < NumDirectAdded)
361 DevirtualizedCall =
true;
367 assert(CallSites.
empty() &&
"Dangling pointers found in call sites map");
371 if ((FunctionNo & 15) == 15)
376 dbgs() <<
"CGSCCPASSMGR: Refreshed SCC is now:\n";
379 if (DevirtualizedCall)
380 dbgs() <<
"CGSCCPASSMGR: Refresh devirtualized a call!\n";
382 dbgs() <<
"CGSCCPASSMGR: SCC Refresh didn't change call graph.\n";
386 return DevirtualizedCall;
393 bool &DevirtualizedCall) {
394 bool Changed =
false;
403 bool CallGraphUpToDate =
true;
406 for (
unsigned PassNo = 0, e = getNumContainedPasses();
407 PassNo != e; ++PassNo) {
408 Pass *P = getContainedPass(PassNo);
412 if (isPassDebuggingExecutionsOrMore()) {
413 std::string Functions;
418 if (
I != CurSCC.
begin()) OS <<
", ";
427 initializeAnalysisImpl(P);
430 Changed |= RunPassOnSCC(P, CurSCC, CG,
431 CallGraphUpToDate, DevirtualizedCall);
437 verifyPreservedAnalysis(P);
438 removeNotPreservedAnalysis(P);
439 recordAvailableAnalysis(P);
445 if (!CallGraphUpToDate)
446 DevirtualizedCall |= RefreshCallGraph(CurSCC, CG,
false);
452 bool CGPassManager::runOnModule(
Module &M) {
453 CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
454 bool Changed = doInitialization(CG);
463 const std::vector<CallGraphNode *> &NodeVec = *CGI;
479 unsigned Iteration = 0;
480 bool DevirtualizedCall =
false;
483 <<
" SCCPASSMGR: Re-visiting SCC, iteration #" << Iteration
485 DevirtualizedCall =
false;
486 Changed |= RunAllPassesOnSCC(CurSCC, CG, DevirtualizedCall);
489 if (DevirtualizedCall)
492 <<
" times, due to -max-cg-scc-iterations\n");
494 MaxSCCIterations.updateMax(Iteration);
496 Changed |= doFinalization(CG);
501 bool CGPassManager::doInitialization(
CallGraph &CG) {
502 bool Changed =
false;
503 for (
unsigned i = 0, e = getNumContainedPasses(); i != e; ++i) {
504 if (
PMDataManager *PM = getContainedPass(i)->getAsPMDataManager()) {
506 "Invalid CGPassManager member");
516 bool CGPassManager::doFinalization(
CallGraph &CG) {
517 bool Changed =
false;
518 for (
unsigned i = 0, e = getNumContainedPasses(); i != e; ++i) {
519 if (
PMDataManager *PM = getContainedPass(i)->getAsPMDataManager()) {
521 "Invalid CGPassManager member");
537 assert(Old != New &&
"Should not replace node with self");
538 for (
unsigned i = 0; ; ++i) {
539 assert(i != Nodes.size() &&
"Node not in SCC");
540 if (Nodes[i] != Old)
continue;
559 while (!PMS.
empty() &&
563 assert(!PMS.
empty() &&
"Unable to handle Call Graph Pass");
567 CGP = (CGPassManager*)PMS.
top();
570 assert(!PMS.
empty() &&
"Unable to create Call Graph Pass Manager");
574 CGP =
new CGPassManager();
615 PrintCallGraphPass(
const std::string &
B,
raw_ostream &OS)
623 bool BannerPrinted =
false;
624 auto PrintBannerOnce = [&] () {
628 BannerPrinted =
true;
638 OS <<
"\nPrinting <null> Function\n";
644 StringRef getPassName()
const override {
return "Print CallGraph IR"; }
652 const std::string &Banner)
const {
653 return new PrintCallGraphPass(Banner, OS);
Pass interface - Implemented by all 'passes'.
bool isIntrinsic() const
isIntrinsic - Returns true if the function's name starts with "llvm.".
This builds on the llvm/ADT/GraphTraits.h file to find the strongly connected components (SCCs) of a ...
PassManagerType
Different types of internal pass managers.
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Compute iterated dominance frontiers using a linear time algorithm.
virtual PMDataManager * getAsPMDataManager()
virtual void dumpPassStructure(unsigned Offset=0)
A Module instance is used to store all the information related to an LLVM module. ...
void ReplaceNode(NodeRef Old, NodeRef New)
This informs the scc_iterator that the specified Old node has been deleted, and New is to be used in ...
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
STATISTIC(NumFunctions, "Total number of functions")
static unsigned InstrCount
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
A node in the call graph for a module.
bool skipSCC(CallGraphSCC &SCC) const
Optional passes call this function to check whether the pass should be skipped.
void getAnalysisUsage(AnalysisUsage &Info) const override
getAnalysisUsage - For this class, we declare that we require and preserve the call graph...
Module & getModule() const
Returns the module the call graph corresponds to.
Pass * createPrinterPass(raw_ostream &OS, const std::string &Banner) const override
createPrinterPass - Get a pass that prints the Module corresponding to a CallGraph.
PMTopLevelManager manages LastUser info and collects common APIs used by top level pass managers...
Timer * getPassTimer(Pass *)
If TimingInfo is enabled then start pass timer.
The TimeRegion class is used as a helper class to call the startTimer() and stopTimer() methods of th...
virtual bool shouldRunPass(const Pass *P, const Module &U)
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
void addCalledFunction(CallSite CS, CallGraphNode *M)
Adds a function to the list of functions called by this one.
void schedulePass(Pass *P)
Schedule pass P for execution.
AnalysisUsage & addRequired()
LLVMContext & getContext() const
Get the global data context.
void replaceCallEdge(CallSite CS, CallSite NewCS, CallGraphNode *NewNode)
Replaces the edge in the node for the specified call site with a new one.
virtual bool doFinalization(Module &)
doFinalization - Virtual method overriden by subclasses to do any necessary clean up after all passes...
const CallGraph & getCallGraph()
FPPassManager manages BBPassManagers and FunctionPasses.
PMStack - This class implements a stack data structure of PMDataManager pointers. ...
scc_iterator< T > scc_begin(const T &G)
Construct the begin iterator for a deduced graph type T.
bool isAtEnd() const
Direct loop termination test which is more efficient than comparison with end().
void dump() const
Print out this call graph node.
This file provides interfaces used to build and manipulate a call graph, which is a very useful tool ...
OptPassGate & getOptPassGate() const
Access the object which can disable optional passes and individual optimizations at compile time...
amdgpu Simplify well known AMD library false Value * Callee
virtual bool doInitialization(Module &)
doInitialization - Virtual method overridden by subclasses to do any necessary initialization before ...
static cl::opt< unsigned > MaxIterations("max-cg-scc-iterations", cl::ReallyHidden, cl::init(4))
iterator find(const_arg_type_t< KeyT > Val)
initializer< Ty > init(const Ty &Val)
bool erase(const KeyT &Val)
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This pass is required by interprocedural register allocation.
The ModulePass which wraps up a CallGraph and the logic to build it.
bool isLeaf(ID id)
Returns true if the intrinsic is a leaf, i.e.
CallGraphNode * getCallsExternalNode() const
LLVM Basic Block Representation.
bool shouldEmitInstrCountChangedRemark()
Return true if size-info optimization remark is enabled, false otherwise.
void addIndirectPassManager(PMDataManager *Manager)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
virtual PassManagerType getPassManagerType() const
Represent the analysis usage information of a pass.
void initialize(ArrayRef< CallGraphNode *> NewNodes)
void assignPassManager(PMStack &PMS, PassManagerType PMT) override
Assign pass manager to manager this pass.
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Function * getFunction() const
Returns the function that this call graph node represents.
Module.h This file contains the declarations for the Module class.
This file declares the interface for bisecting optimizations.
Intrinsic::ID getIntrinsicID() const LLVM_READONLY
getIntrinsicID - This method returns the ID number of the specified function, or Intrinsic::not_intri...
bool isFunctionInPrintList(StringRef FunctionName)
isFunctionInPrintList - returns true if a function should be printed via
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void setPreservesAll()
Set by analyses that do not transform their input at all.
void removeCallEdge(iterator I)
The basic data container for the call graph of a Module of IR.
void ReplaceNode(CallGraphNode *Old, CallGraphNode *New)
ReplaceNode - This informs the SCC and the pass manager that the specified Old node has been deleted...
StringRef getName() const
Return a constant reference to the value's name.
std::vector< CallGraphNode * >::const_iterator iterator
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
std::vector< CallRecord >::iterator iterator
size_type count(const_arg_type_t< KeyT > Val) const
Return 1 if the specified key is in the map, 0 otherwise.
void push(PMDataManager *PM)
LLVM_NODISCARD bool empty() const
PMDataManager provides the common place to manage the analysis data used by pass managers.
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
FunTy * getCalledFunction() const
Return the function being called if this is a direct call, otherwise return null (if it's an indirect...
virtual bool runOnSCC(CallGraphSCC &SCC)=0
runOnSCC - This method should be implemented by the subclass to perform whatever action is necessary ...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A raw_ostream that writes to an std::string.
PMDataManager * top() const
CallGraphSCC - This is a single SCC that a CallGraphSCCPass is run on.
CallGraphNode * getOrInsertFunction(const Function *F)
Similar to operator[], but this will insert a new CallGraphNode for F if one does not already exist...
This class implements an extremely fast bulk output stream that can only output to a stream...
StringRef - Represent a constant reference to a string, i.e.
Enumerate the SCCs of a directed graph in reverse topological order of the SCC DAG.