blob: e4af831752c9381d2d48189c9dfa0e388485809e [file] [log] [blame]
Chris Lattner23ebd752003-08-31 19:23:41 +00001//===- PrintSCC.cpp - Enumerate SCCs in some key graphs -------------------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
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:
17// analyze -cfgscc to print the SCCs in each CFG of a module.
18// analyze -cfgscc -stats to print the #SCCs and the maximum SCC size.
19// analyze -cfgscc -debug > /dev/null to watch the algorithm in action.
20//
21// and similarly:
22// analyze -callscc [-stats] [-debug] to print SCCs in the CallGraph
23//
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"
Reid Spencer551ccae2004-09-01 22:55:40 +000032#include "llvm/ADT/SCCIterator.h"
Reid Spencer954da372004-07-04 12:19:56 +000033#include <iostream>
Vikram S. Advec405daf2002-11-04 14:20:22 +000034
Brian Gaeked0fde302003-11-11 22:41:34 +000035namespace llvm {
36
Vikram S. Advec405daf2002-11-04 14:20:22 +000037namespace {
Chris Lattner8d0a23a2003-08-31 19:27:11 +000038 struct CFGSCC : public FunctionPass {
39 bool runOnFunction(Function& func);
40
41 void print(std::ostream &O) const { }
42
43 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
44 AU.setPreservesAll();
Chris Lattner23ebd752003-08-31 19:23:41 +000045 }
Chris Lattner8d0a23a2003-08-31 19:27:11 +000046 };
Vikram S. Advec405daf2002-11-04 14:20:22 +000047
Chris Lattner8d0a23a2003-08-31 19:27:11 +000048 struct CallGraphSCC : public Pass {
49 // run - Print out SCCs in the call graph for the specified module.
50 bool run(Module &M);
Vikram S. Advec405daf2002-11-04 14:20:22 +000051
Chris Lattner8d0a23a2003-08-31 19:27:11 +000052 void print(std::ostream &O) const { }
Vikram S. Advec405daf2002-11-04 14:20:22 +000053
Chris Lattner8d0a23a2003-08-31 19:27:11 +000054 // getAnalysisUsage - This pass requires the CallGraph.
55 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
56 AU.setPreservesAll();
57 AU.addRequired<CallGraph>();
Chris Lattner23ebd752003-08-31 19:23:41 +000058 }
Chris Lattner8d0a23a2003-08-31 19:27:11 +000059 };
Vikram S. Advec405daf2002-11-04 14:20:22 +000060
Chris Lattner23ebd752003-08-31 19:23:41 +000061 RegisterAnalysis<CFGSCC>
62 Y("cfgscc", "Print SCCs of each function CFG");
Vikram S. Advec405daf2002-11-04 14:20:22 +000063
Chris Lattner23ebd752003-08-31 19:23:41 +000064 RegisterAnalysis<CallGraphSCC>
65 Z("callscc", "Print SCCs of the Call Graph");
Vikram S. Advec405daf2002-11-04 14:20:22 +000066}
Chris Lattner8d0a23a2003-08-31 19:27:11 +000067
68bool CFGSCC::runOnFunction(Function &F) {
69 unsigned sccNum = 0;
70 std::cout << "SCCs for Function " << F.getName() << " in PostOrder:";
Chris Lattner55b2eb32003-08-31 20:01:57 +000071 for (scc_iterator<Function*> SCCI = scc_begin(&F),
72 E = scc_end(&F); SCCI != E; ++SCCI) {
Chris Lattner729d73d2003-08-31 19:55:06 +000073 std::vector<BasicBlock*> &nextSCC = *SCCI;
Chris Lattner8d0a23a2003-08-31 19:27:11 +000074 std::cout << "\nSCC #" << ++sccNum << " : ";
Chris Lattner9f2a06e2003-08-31 19:51:38 +000075 for (std::vector<BasicBlock*>::const_iterator I = nextSCC.begin(),
Chris Lattner8d0a23a2003-08-31 19:27:11 +000076 E = nextSCC.end(); I != E; ++I)
77 std::cout << (*I)->getName() << ", ";
Chris Lattner9f2a06e2003-08-31 19:51:38 +000078 if (nextSCC.size() == 1 && SCCI.hasLoop())
Chris Lattner8d0a23a2003-08-31 19:27:11 +000079 std::cout << " (Has self-loop).";
80 }
81 std::cout << "\n";
82
83 return true;
84}
85
86
87// run - Print out SCCs in the call graph for the specified module.
88bool CallGraphSCC::run(Module &M) {
89 CallGraphNode* rootNode = getAnalysis<CallGraph>().getRoot();
90 unsigned sccNum = 0;
91 std::cout << "SCCs for the program in PostOrder:";
Chris Lattner55b2eb32003-08-31 20:01:57 +000092 for (scc_iterator<CallGraphNode*> SCCI = scc_begin(rootNode),
93 E = scc_end(rootNode); SCCI != E; ++SCCI) {
Chris Lattner729d73d2003-08-31 19:55:06 +000094 const std::vector<CallGraphNode*> &nextSCC = *SCCI;
Chris Lattner8d0a23a2003-08-31 19:27:11 +000095 std::cout << "\nSCC #" << ++sccNum << " : ";
Chris Lattner9f2a06e2003-08-31 19:51:38 +000096 for (std::vector<CallGraphNode*>::const_iterator I = nextSCC.begin(),
Chris Lattner8d0a23a2003-08-31 19:27:11 +000097 E = nextSCC.end(); I != E; ++I)
98 std::cout << ((*I)->getFunction() ? (*I)->getFunction()->getName()
99 : std::string("Indirect CallGraph node")) << ", ";
Chris Lattner9f2a06e2003-08-31 19:51:38 +0000100 if (nextSCC.size() == 1 && SCCI.hasLoop())
Chris Lattner8d0a23a2003-08-31 19:27:11 +0000101 std::cout << " (Has self-loop).";
102 }
103 std::cout << "\n";
104
105 return true;
106}
Brian Gaeked0fde302003-11-11 22:41:34 +0000107
108} // End llvm namespace