blob: 78ede2b72f84905dee1cb8dea29a9faeec7f80f8 [file] [log] [blame]
Chris Lattner23ebd752003-08-31 19:23:41 +00001//===- PrintSCC.cpp - Enumerate SCCs in some key graphs -------------------===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner21c62da2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Vikram S. Advec405daf2002-11-04 14:20:22 +00009//
10// This file provides passes to print out SCCs in a CFG or a CallGraph.
11// Normally, you would not use these passes; instead, you would use the
Chris Lattner55b2eb32003-08-31 20:01:57 +000012// scc_iterator directly to enumerate SCCs and process them in some way. These
13// passes serve three purposes:
14//
15// (1) As a reference for how to use the scc_iterator.
Vikram S. Advec405daf2002-11-04 14:20:22 +000016// (2) To print out the SCCs for a CFG or a CallGraph:
Duncan Sands3ee8fc92008-09-23 12:47:39 +000017// analyze -print-cfg-sccs to print the SCCs in each CFG of a module.
18// analyze -print-cfg-sccs -stats to print the #SCCs and the maximum SCC size.
19// analyze -print-cfg-sccs -debug > /dev/null to watch the algorithm in action.
Misha Brukman2b37d7c2005-04-21 21:13:18 +000020//
Vikram S. Advec405daf2002-11-04 14:20:22 +000021// and similarly:
Duncan Sands3ee8fc92008-09-23 12:47:39 +000022// analyze -print-callgraph-sccs [-stats] [-debug] to print SCCs in the CallGraph
Misha Brukman2b37d7c2005-04-21 21:13:18 +000023//
Chris Lattner55b2eb32003-08-31 20:01:57 +000024// (3) To test the scc_iterator.
Chris Lattner23ebd752003-08-31 19:23:41 +000025//
Vikram S. Advec405daf2002-11-04 14:20:22 +000026//===----------------------------------------------------------------------===//
27
Chandler Carruthf010c462012-12-04 10:44:52 +000028#include "llvm/ADT/SCCIterator.h"
Vikram S. Advec405daf2002-11-04 14:20:22 +000029#include "llvm/Analysis/CallGraph.h"
Chandler Carruth03e36d72014-03-04 11:45:46 +000030#include "llvm/IR/CFG.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000031#include "llvm/IR/Module.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000032#include "llvm/Pass.h"
Dan Gohmanac95cc72009-07-16 15:30:09 +000033#include "llvm/Support/raw_ostream.h"
Chris Lattner68d033c2004-09-20 04:44:31 +000034using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000035
Vikram S. Advec405daf2002-11-04 14:20:22 +000036namespace {
Chris Lattner8d0a23a2003-08-31 19:27:11 +000037 struct CFGSCC : public FunctionPass {
Devang Patel19974732007-05-03 01:11:54 +000038 static char ID; // Pass identification, replacement for typeid
Owen Anderson90c579d2010-08-06 18:33:48 +000039 CFGSCC() : FunctionPass(ID) {}
Craig Topperc83e68f2014-03-08 08:27:28 +000040 bool runOnFunction(Function& func) override;
Chris Lattner8d0a23a2003-08-31 19:27:11 +000041
Craig Topper573faec2014-04-25 04:24:47 +000042 void print(raw_ostream &O, const Module* = nullptr) const override { }
Chris Lattner8d0a23a2003-08-31 19:27:11 +000043
Craig Topperc83e68f2014-03-08 08:27:28 +000044 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chris Lattner8d0a23a2003-08-31 19:27:11 +000045 AU.setPreservesAll();
Chris Lattner23ebd752003-08-31 19:23:41 +000046 }
Chris Lattner8d0a23a2003-08-31 19:27:11 +000047 };
Vikram S. Advec405daf2002-11-04 14:20:22 +000048
Chris Lattner68d033c2004-09-20 04:44:31 +000049 struct CallGraphSCC : public ModulePass {
Devang Patel19974732007-05-03 01:11:54 +000050 static char ID; // Pass identification, replacement for typeid
Owen Anderson90c579d2010-08-06 18:33:48 +000051 CallGraphSCC() : ModulePass(ID) {}
Devang Patel794fd752007-05-01 21:15:47 +000052
Chris Lattner8d0a23a2003-08-31 19:27:11 +000053 // run - Print out SCCs in the call graph for the specified module.
Craig Topperc83e68f2014-03-08 08:27:28 +000054 bool runOnModule(Module &M) override;
Vikram S. Advec405daf2002-11-04 14:20:22 +000055
Craig Topper573faec2014-04-25 04:24:47 +000056 void print(raw_ostream &O, const Module* = nullptr) const override { }
Vikram S. Advec405daf2002-11-04 14:20:22 +000057
Chris Lattner8d0a23a2003-08-31 19:27:11 +000058 // getAnalysisUsage - This pass requires the CallGraph.
Craig Topperc83e68f2014-03-08 08:27:28 +000059 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chris Lattner8d0a23a2003-08-31 19:27:11 +000060 AU.setPreservesAll();
Chandler Carruth54fec072013-11-26 04:19:30 +000061 AU.addRequired<CallGraphWrapperPass>();
Chris Lattner23ebd752003-08-31 19:23:41 +000062 }
Chris Lattner8d0a23a2003-08-31 19:27:11 +000063 };
Vikram S. Advec405daf2002-11-04 14:20:22 +000064}
Chris Lattner8d0a23a2003-08-31 19:27:11 +000065
Dan Gohmancfbe4012010-08-20 01:00:03 +000066char CFGSCC::ID = 0;
67static RegisterPass<CFGSCC>
68Y("print-cfg-sccs", "Print SCCs of each function CFG");
69
70char CallGraphSCC::ID = 0;
71static RegisterPass<CallGraphSCC>
72Z("print-callgraph-sccs", "Print SCCs of the Call Graph");
73
Chris Lattner8d0a23a2003-08-31 19:27:11 +000074bool CFGSCC::runOnFunction(Function &F) {
75 unsigned sccNum = 0;
Dan Gohman52fdaed2010-08-20 01:03:44 +000076 errs() << "SCCs for Function " << F.getName() << " in PostOrder:";
Duncan P. N. Exon Smith483727d2014-02-04 19:19:07 +000077 for (scc_iterator<Function*> SCCI = scc_begin(&F); !SCCI.isAtEnd(); ++SCCI) {
Duncan P. N. Exon Smithdb8c1ae2014-04-25 18:24:50 +000078 const std::vector<BasicBlock *> &nextSCC = *SCCI;
Dan Gohman52fdaed2010-08-20 01:03:44 +000079 errs() << "\nSCC #" << ++sccNum << " : ";
Chris Lattner9f2a06e2003-08-31 19:51:38 +000080 for (std::vector<BasicBlock*>::const_iterator I = nextSCC.begin(),
Chris Lattner8d0a23a2003-08-31 19:27:11 +000081 E = nextSCC.end(); I != E; ++I)
Dan Gohman52fdaed2010-08-20 01:03:44 +000082 errs() << (*I)->getName() << ", ";
Chris Lattner9f2a06e2003-08-31 19:51:38 +000083 if (nextSCC.size() == 1 && SCCI.hasLoop())
Dan Gohman52fdaed2010-08-20 01:03:44 +000084 errs() << " (Has self-loop).";
Chris Lattner8d0a23a2003-08-31 19:27:11 +000085 }
Dan Gohman52fdaed2010-08-20 01:03:44 +000086 errs() << "\n";
Chris Lattner8d0a23a2003-08-31 19:27:11 +000087
88 return true;
89}
90
91
92// run - Print out SCCs in the call graph for the specified module.
Chris Lattner68d033c2004-09-20 04:44:31 +000093bool CallGraphSCC::runOnModule(Module &M) {
Chandler Carruthdf130802013-11-27 01:32:17 +000094 CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
Chris Lattner8d0a23a2003-08-31 19:27:11 +000095 unsigned sccNum = 0;
Dan Gohman52fdaed2010-08-20 01:03:44 +000096 errs() << "SCCs for the program in PostOrder:";
Duncan P. N. Exon Smith483727d2014-02-04 19:19:07 +000097 for (scc_iterator<CallGraph*> SCCI = scc_begin(&CG); !SCCI.isAtEnd();
98 ++SCCI) {
Chris Lattner729d73d2003-08-31 19:55:06 +000099 const std::vector<CallGraphNode*> &nextSCC = *SCCI;
Dan Gohman52fdaed2010-08-20 01:03:44 +0000100 errs() << "\nSCC #" << ++sccNum << " : ";
Chris Lattner9f2a06e2003-08-31 19:51:38 +0000101 for (std::vector<CallGraphNode*>::const_iterator I = nextSCC.begin(),
Chris Lattner8d0a23a2003-08-31 19:27:11 +0000102 E = nextSCC.end(); I != E; ++I)
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000103 errs() << ((*I)->getFunction() ? (*I)->getFunction()->getName()
104 : "external node") << ", ";
Chris Lattner9f2a06e2003-08-31 19:51:38 +0000105 if (nextSCC.size() == 1 && SCCI.hasLoop())
Dan Gohman52fdaed2010-08-20 01:03:44 +0000106 errs() << " (Has self-loop).";
Chris Lattner8d0a23a2003-08-31 19:27:11 +0000107 }
Dan Gohman52fdaed2010-08-20 01:03:44 +0000108 errs() << "\n";
Chris Lattner8d0a23a2003-08-31 19:27:11 +0000109
110 return true;
111}