blob: 66709ffa196a9f5d00fb3f1e00a1abfb5bb60f38 [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
28#include "llvm/Pass.h"
29#include "llvm/Module.h"
Vikram S. Advec405daf2002-11-04 14:20:22 +000030#include "llvm/Analysis/CallGraph.h"
31#include "llvm/Support/CFG.h"
Dan Gohmanac95cc72009-07-16 15:30:09 +000032#include "llvm/Support/raw_ostream.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000033#include "llvm/ADT/SCCIterator.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
Dan Gohman865f0062009-02-18 05:09:16 +000039 CFGSCC() : FunctionPass(&ID) {}
Chris Lattner8d0a23a2003-08-31 19:27:11 +000040 bool runOnFunction(Function& func);
41
Chris Lattner45cfe542009-08-23 06:03:38 +000042 void print(raw_ostream &O, const Module* = 0) const { }
Chris Lattner8d0a23a2003-08-31 19:27:11 +000043
44 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
45 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
Dan Gohman865f0062009-02-18 05:09:16 +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.
Chris Lattner68d033c2004-09-20 04:44:31 +000054 bool runOnModule(Module &M);
Vikram S. Advec405daf2002-11-04 14:20:22 +000055
Chris Lattner45cfe542009-08-23 06:03:38 +000056 void print(raw_ostream &O, const Module* = 0) const { }
Vikram S. Advec405daf2002-11-04 14:20:22 +000057
Chris Lattner8d0a23a2003-08-31 19:27:11 +000058 // getAnalysisUsage - This pass requires the CallGraph.
59 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
60 AU.setPreservesAll();
61 AU.addRequired<CallGraph>();
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
Devang Patel19974732007-05-03 01:11:54 +000065 char CFGSCC::ID = 0;
Chris Lattner5d8925c2006-08-27 22:30:17 +000066 RegisterPass<CFGSCC>
Duncan Sands3ee8fc92008-09-23 12:47:39 +000067 Y("print-cfg-sccs", "Print SCCs of each function CFG");
Vikram S. Advec405daf2002-11-04 14:20:22 +000068
Devang Patel19974732007-05-03 01:11:54 +000069 char CallGraphSCC::ID = 0;
Chris Lattner5d8925c2006-08-27 22:30:17 +000070 RegisterPass<CallGraphSCC>
Duncan Sands3ee8fc92008-09-23 12:47:39 +000071 Z("print-callgraph-sccs", "Print SCCs of the Call Graph");
Vikram S. Advec405daf2002-11-04 14:20:22 +000072}
Chris Lattner8d0a23a2003-08-31 19:27:11 +000073
74bool CFGSCC::runOnFunction(Function &F) {
75 unsigned sccNum = 0;
Dan Gohmanac95cc72009-07-16 15:30:09 +000076 outs() << "SCCs for Function " << F.getName() << " in PostOrder:";
Chris Lattner55b2eb32003-08-31 20:01:57 +000077 for (scc_iterator<Function*> SCCI = scc_begin(&F),
78 E = scc_end(&F); SCCI != E; ++SCCI) {
Chris Lattner729d73d2003-08-31 19:55:06 +000079 std::vector<BasicBlock*> &nextSCC = *SCCI;
Dan Gohmanac95cc72009-07-16 15:30:09 +000080 outs() << "\nSCC #" << ++sccNum << " : ";
Chris Lattner9f2a06e2003-08-31 19:51:38 +000081 for (std::vector<BasicBlock*>::const_iterator I = nextSCC.begin(),
Chris Lattner8d0a23a2003-08-31 19:27:11 +000082 E = nextSCC.end(); I != E; ++I)
Dan Gohmanac95cc72009-07-16 15:30:09 +000083 outs() << (*I)->getName() << ", ";
Chris Lattner9f2a06e2003-08-31 19:51:38 +000084 if (nextSCC.size() == 1 && SCCI.hasLoop())
Dan Gohmanac95cc72009-07-16 15:30:09 +000085 outs() << " (Has self-loop).";
Chris Lattner8d0a23a2003-08-31 19:27:11 +000086 }
Dan Gohmanac95cc72009-07-16 15:30:09 +000087 outs() << "\n";
Chris Lattner8d0a23a2003-08-31 19:27:11 +000088
89 return true;
90}
91
92
93// run - Print out SCCs in the call graph for the specified module.
Chris Lattner68d033c2004-09-20 04:44:31 +000094bool CallGraphSCC::runOnModule(Module &M) {
Chris Lattner8d0a23a2003-08-31 19:27:11 +000095 CallGraphNode* rootNode = getAnalysis<CallGraph>().getRoot();
96 unsigned sccNum = 0;
Dan Gohmanac95cc72009-07-16 15:30:09 +000097 outs() << "SCCs for the program in PostOrder:";
Chris Lattner55b2eb32003-08-31 20:01:57 +000098 for (scc_iterator<CallGraphNode*> SCCI = scc_begin(rootNode),
99 E = scc_end(rootNode); SCCI != E; ++SCCI) {
Chris Lattner729d73d2003-08-31 19:55:06 +0000100 const std::vector<CallGraphNode*> &nextSCC = *SCCI;
Dan Gohmanac95cc72009-07-16 15:30:09 +0000101 outs() << "\nSCC #" << ++sccNum << " : ";
Chris Lattner9f2a06e2003-08-31 19:51:38 +0000102 for (std::vector<CallGraphNode*>::const_iterator I = nextSCC.begin(),
Chris Lattner8d0a23a2003-08-31 19:27:11 +0000103 E = nextSCC.end(); I != E; ++I)
Daniel Dunbar3d5126f2009-07-22 21:33:09 +0000104 outs() << ((*I)->getFunction() ? (*I)->getFunction()->getNameStr()
Dan Gohmanac95cc72009-07-16 15:30:09 +0000105 : std::string("Indirect CallGraph node")) << ", ";
Chris Lattner9f2a06e2003-08-31 19:51:38 +0000106 if (nextSCC.size() == 1 && SCCI.hasLoop())
Dan Gohmanac95cc72009-07-16 15:30:09 +0000107 outs() << " (Has self-loop).";
Chris Lattner8d0a23a2003-08-31 19:27:11 +0000108 }
Dan Gohmanac95cc72009-07-16 15:30:09 +0000109 outs() << "\n";
Chris Lattner8d0a23a2003-08-31 19:27:11 +0000110
111 return true;
112}