blob: 5fe1eecde2bb57384acf68392fb92378024082d1 [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>
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 {
38 bool runOnFunction(Function& func);
39
40 void print(std::ostream &O) const { }
41
42 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
43 AU.setPreservesAll();
Chris Lattner23ebd752003-08-31 19:23:41 +000044 }
Chris Lattner8d0a23a2003-08-31 19:27:11 +000045 };
Vikram S. Advec405daf2002-11-04 14:20:22 +000046
Chris Lattner68d033c2004-09-20 04:44:31 +000047 struct CallGraphSCC : public ModulePass {
Chris Lattner8d0a23a2003-08-31 19:27:11 +000048 // run - Print out SCCs in the call graph for the specified module.
Chris Lattner68d033c2004-09-20 04:44:31 +000049 bool runOnModule(Module &M);
Vikram S. Advec405daf2002-11-04 14:20:22 +000050
Chris Lattner8d0a23a2003-08-31 19:27:11 +000051 void print(std::ostream &O) const { }
Vikram S. Advec405daf2002-11-04 14:20:22 +000052
Chris Lattner8d0a23a2003-08-31 19:27:11 +000053 // getAnalysisUsage - This pass requires the CallGraph.
54 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
55 AU.setPreservesAll();
56 AU.addRequired<CallGraph>();
Chris Lattner23ebd752003-08-31 19:23:41 +000057 }
Chris Lattner8d0a23a2003-08-31 19:27:11 +000058 };
Vikram S. Advec405daf2002-11-04 14:20:22 +000059
Chris Lattner23ebd752003-08-31 19:23:41 +000060 RegisterAnalysis<CFGSCC>
61 Y("cfgscc", "Print SCCs of each function CFG");
Vikram S. Advec405daf2002-11-04 14:20:22 +000062
Chris Lattner23ebd752003-08-31 19:23:41 +000063 RegisterAnalysis<CallGraphSCC>
64 Z("callscc", "Print SCCs of the Call Graph");
Vikram S. Advec405daf2002-11-04 14:20:22 +000065}
Chris Lattner8d0a23a2003-08-31 19:27:11 +000066
67bool CFGSCC::runOnFunction(Function &F) {
68 unsigned sccNum = 0;
69 std::cout << "SCCs for Function " << F.getName() << " in PostOrder:";
Chris Lattner55b2eb32003-08-31 20:01:57 +000070 for (scc_iterator<Function*> SCCI = scc_begin(&F),
71 E = scc_end(&F); SCCI != E; ++SCCI) {
Chris Lattner729d73d2003-08-31 19:55:06 +000072 std::vector<BasicBlock*> &nextSCC = *SCCI;
Chris Lattner8d0a23a2003-08-31 19:27:11 +000073 std::cout << "\nSCC #" << ++sccNum << " : ";
Chris Lattner9f2a06e2003-08-31 19:51:38 +000074 for (std::vector<BasicBlock*>::const_iterator I = nextSCC.begin(),
Chris Lattner8d0a23a2003-08-31 19:27:11 +000075 E = nextSCC.end(); I != E; ++I)
76 std::cout << (*I)->getName() << ", ";
Chris Lattner9f2a06e2003-08-31 19:51:38 +000077 if (nextSCC.size() == 1 && SCCI.hasLoop())
Chris Lattner8d0a23a2003-08-31 19:27:11 +000078 std::cout << " (Has self-loop).";
79 }
80 std::cout << "\n";
81
82 return true;
83}
84
85
86// run - Print out SCCs in the call graph for the specified module.
Chris Lattner68d033c2004-09-20 04:44:31 +000087bool CallGraphSCC::runOnModule(Module &M) {
Chris Lattner8d0a23a2003-08-31 19:27:11 +000088 CallGraphNode* rootNode = getAnalysis<CallGraph>().getRoot();
89 unsigned sccNum = 0;
90 std::cout << "SCCs for the program in PostOrder:";
Chris Lattner55b2eb32003-08-31 20:01:57 +000091 for (scc_iterator<CallGraphNode*> SCCI = scc_begin(rootNode),
92 E = scc_end(rootNode); SCCI != E; ++SCCI) {
Chris Lattner729d73d2003-08-31 19:55:06 +000093 const std::vector<CallGraphNode*> &nextSCC = *SCCI;
Chris Lattner8d0a23a2003-08-31 19:27:11 +000094 std::cout << "\nSCC #" << ++sccNum << " : ";
Chris Lattner9f2a06e2003-08-31 19:51:38 +000095 for (std::vector<CallGraphNode*>::const_iterator I = nextSCC.begin(),
Chris Lattner8d0a23a2003-08-31 19:27:11 +000096 E = nextSCC.end(); I != E; ++I)
97 std::cout << ((*I)->getFunction() ? (*I)->getFunction()->getName()
98 : std::string("Indirect CallGraph node")) << ", ";
Chris Lattner9f2a06e2003-08-31 19:51:38 +000099 if (nextSCC.size() == 1 && SCCI.hasLoop())
Chris Lattner8d0a23a2003-08-31 19:27:11 +0000100 std::cout << " (Has self-loop).";
101 }
102 std::cout << "\n";
103
104 return true;
105}