2003-08-31 21:23:41 +02:00
|
|
|
//===- PrintSCC.cpp - Enumerate SCCs in some key graphs -------------------===//
|
2005-04-21 23:13:18 +02:00
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2005-04-21 23:13:18 +02:00
|
|
|
//
|
2003-10-20 21:43:21 +02:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-11-04 15:20:22 +01:00
|
|
|
//
|
|
|
|
// This file provides passes to print out SCCs in a CFG or a CallGraph.
|
|
|
|
// Normally, you would not use these passes; instead, you would use the
|
2003-08-31 22:01:57 +02:00
|
|
|
// scc_iterator directly to enumerate SCCs and process them in some way. These
|
|
|
|
// passes serve three purposes:
|
|
|
|
//
|
|
|
|
// (1) As a reference for how to use the scc_iterator.
|
2002-11-04 15:20:22 +01:00
|
|
|
// (2) To print out the SCCs for a CFG or a CallGraph:
|
2008-09-23 14:47:39 +02:00
|
|
|
// analyze -print-cfg-sccs to print the SCCs in each CFG of a module.
|
|
|
|
// analyze -print-cfg-sccs -stats to print the #SCCs and the maximum SCC size.
|
|
|
|
// analyze -print-cfg-sccs -debug > /dev/null to watch the algorithm in action.
|
2005-04-21 23:13:18 +02:00
|
|
|
//
|
2002-11-04 15:20:22 +01:00
|
|
|
// and similarly:
|
2008-09-23 14:47:39 +02:00
|
|
|
// analyze -print-callgraph-sccs [-stats] [-debug] to print SCCs in the CallGraph
|
2005-04-21 23:13:18 +02:00
|
|
|
//
|
2003-08-31 22:01:57 +02:00
|
|
|
// (3) To test the scc_iterator.
|
2003-08-31 21:23:41 +02:00
|
|
|
//
|
2002-11-04 15:20:22 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-04 11:44:52 +01:00
|
|
|
#include "llvm/ADT/SCCIterator.h"
|
2002-11-04 15:20:22 +01:00
|
|
|
#include "llvm/Analysis/CallGraph.h"
|
2014-03-04 12:45:46 +01:00
|
|
|
#include "llvm/IR/CFG.h"
|
2013-01-02 12:36:10 +01:00
|
|
|
#include "llvm/IR/Module.h"
|
2012-12-04 11:44:52 +01:00
|
|
|
#include "llvm/Pass.h"
|
2009-07-16 17:30:09 +02:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2004-09-20 06:44:31 +02:00
|
|
|
using namespace llvm;
|
2003-11-11 23:41:34 +01:00
|
|
|
|
2002-11-04 15:20:22 +01:00
|
|
|
namespace {
|
2003-08-31 21:27:11 +02:00
|
|
|
struct CFGSCC : public FunctionPass {
|
2007-05-03 03:11:54 +02:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2010-08-06 20:33:48 +02:00
|
|
|
CFGSCC() : FunctionPass(ID) {}
|
2014-03-08 09:27:28 +01:00
|
|
|
bool runOnFunction(Function& func) override;
|
2002-11-04 15:20:22 +01:00
|
|
|
|
2014-04-25 06:24:47 +02:00
|
|
|
void print(raw_ostream &O, const Module* = nullptr) const override { }
|
2002-11-04 15:20:22 +01:00
|
|
|
|
2014-03-08 09:27:28 +01:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2003-08-31 21:27:11 +02:00
|
|
|
AU.setPreservesAll();
|
2003-08-31 21:23:41 +02:00
|
|
|
}
|
2003-08-31 21:27:11 +02:00
|
|
|
};
|
2002-11-04 15:20:22 +01:00
|
|
|
|
2004-09-20 06:44:31 +02:00
|
|
|
struct CallGraphSCC : public ModulePass {
|
2007-05-03 03:11:54 +02:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2010-08-06 20:33:48 +02:00
|
|
|
CallGraphSCC() : ModulePass(ID) {}
|
2007-05-01 23:15:47 +02:00
|
|
|
|
2003-08-31 21:27:11 +02:00
|
|
|
// run - Print out SCCs in the call graph for the specified module.
|
2014-03-08 09:27:28 +01:00
|
|
|
bool runOnModule(Module &M) override;
|
2002-11-04 15:20:22 +01:00
|
|
|
|
2014-04-25 06:24:47 +02:00
|
|
|
void print(raw_ostream &O, const Module* = nullptr) const override { }
|
2002-11-04 15:20:22 +01:00
|
|
|
|
2003-08-31 21:27:11 +02:00
|
|
|
// getAnalysisUsage - This pass requires the CallGraph.
|
2014-03-08 09:27:28 +01:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2003-08-31 21:27:11 +02:00
|
|
|
AU.setPreservesAll();
|
2013-11-26 05:19:30 +01:00
|
|
|
AU.addRequired<CallGraphWrapperPass>();
|
2003-08-31 21:27:11 +02:00
|
|
|
}
|
|
|
|
};
|
2010-08-20 03:00:03 +02:00
|
|
|
}
|
2002-11-04 15:20:22 +01:00
|
|
|
|
2010-08-20 03:00:03 +02:00
|
|
|
char CFGSCC::ID = 0;
|
|
|
|
static RegisterPass<CFGSCC>
|
|
|
|
Y("print-cfg-sccs", "Print SCCs of each function CFG");
|
2002-11-04 15:20:22 +01:00
|
|
|
|
2010-08-20 03:00:03 +02:00
|
|
|
char CallGraphSCC::ID = 0;
|
|
|
|
static RegisterPass<CallGraphSCC>
|
|
|
|
Z("print-callgraph-sccs", "Print SCCs of the Call Graph");
|
2003-08-31 21:27:11 +02:00
|
|
|
|
|
|
|
bool CFGSCC::runOnFunction(Function &F) {
|
|
|
|
unsigned sccNum = 0;
|
2010-08-20 03:03:44 +02:00
|
|
|
errs() << "SCCs for Function " << F.getName() << " in PostOrder:";
|
2014-02-04 20:19:07 +01:00
|
|
|
for (scc_iterator<Function*> SCCI = scc_begin(&F); !SCCI.isAtEnd(); ++SCCI) {
|
2014-04-25 20:24:50 +02:00
|
|
|
const std::vector<BasicBlock *> &nextSCC = *SCCI;
|
2010-08-20 03:03:44 +02:00
|
|
|
errs() << "\nSCC #" << ++sccNum << " : ";
|
2020-05-29 19:12:44 +02:00
|
|
|
for (BasicBlock *BB : nextSCC) {
|
|
|
|
BB->printAsOperand(errs(), false);
|
|
|
|
errs() << ", ";
|
|
|
|
}
|
2020-03-01 18:17:21 +01:00
|
|
|
if (nextSCC.size() == 1 && SCCI.hasCycle())
|
2010-08-20 03:03:44 +02:00
|
|
|
errs() << " (Has self-loop).";
|
2003-08-31 21:27:11 +02:00
|
|
|
}
|
2010-08-20 03:03:44 +02:00
|
|
|
errs() << "\n";
|
2003-08-31 21:27:11 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// run - Print out SCCs in the call graph for the specified module.
|
2004-09-20 06:44:31 +02:00
|
|
|
bool CallGraphSCC::runOnModule(Module &M) {
|
2013-11-27 02:32:17 +01:00
|
|
|
CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
|
2003-08-31 21:27:11 +02:00
|
|
|
unsigned sccNum = 0;
|
2010-08-20 03:03:44 +02:00
|
|
|
errs() << "SCCs for the program in PostOrder:";
|
2014-02-04 20:19:07 +01:00
|
|
|
for (scc_iterator<CallGraph*> SCCI = scc_begin(&CG); !SCCI.isAtEnd();
|
|
|
|
++SCCI) {
|
2003-08-31 21:55:06 +02:00
|
|
|
const std::vector<CallGraphNode*> &nextSCC = *SCCI;
|
2010-08-20 03:03:44 +02:00
|
|
|
errs() << "\nSCC #" << ++sccNum << " : ";
|
2003-08-31 21:51:38 +02:00
|
|
|
for (std::vector<CallGraphNode*>::const_iterator I = nextSCC.begin(),
|
2003-08-31 21:27:11 +02:00
|
|
|
E = nextSCC.end(); I != E; ++I)
|
2011-11-15 17:27:03 +01:00
|
|
|
errs() << ((*I)->getFunction() ? (*I)->getFunction()->getName()
|
|
|
|
: "external node") << ", ";
|
2020-03-01 18:17:21 +01:00
|
|
|
if (nextSCC.size() == 1 && SCCI.hasCycle())
|
2010-08-20 03:03:44 +02:00
|
|
|
errs() << " (Has self-loop).";
|
2003-08-31 21:27:11 +02:00
|
|
|
}
|
2010-08-20 03:03:44 +02:00
|
|
|
errs() << "\n";
|
2003-08-31 21:27:11 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|