blob: 0b9c12ff4bf3b29b8add0e6b4134f8fd76bddca5 [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:
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.
Misha Brukman2b37d7c2005-04-21 21:13:18 +000020//
Vikram S. Advec405daf2002-11-04 14:20:22 +000021// and similarly:
22// analyze -callscc [-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"
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 {
Devang Patel19974732007-05-03 01:11:54 +000038 static char ID; // Pass identification, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +000039 CFGSCC() : FunctionPass((intptr_t)&ID) {}
Chris Lattner8d0a23a2003-08-31 19:27:11 +000040 bool runOnFunction(Function& func);
41
Reid Spencerce9653c2004-12-07 04:03:45 +000042 void print(std::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
Devang Patel794fd752007-05-01 21:15:47 +000051 CallGraphSCC() : ModulePass((intptr_t)&ID) {}
52
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
Reid Spencerce9653c2004-12-07 04:03:45 +000056 void print(std::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>
Chris Lattner23ebd752003-08-31 19:23:41 +000067 Y("cfgscc", "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>
Chris Lattner23ebd752003-08-31 19:23:41 +000071 Z("callscc", "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;
76 std::cout << "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;
Chris Lattner8d0a23a2003-08-31 19:27:11 +000080 std::cout << "\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)
83 std::cout << (*I)->getName() << ", ";
Chris Lattner9f2a06e2003-08-31 19:51:38 +000084 if (nextSCC.size() == 1 && SCCI.hasLoop())
Chris Lattner8d0a23a2003-08-31 19:27:11 +000085 std::cout << " (Has self-loop).";
86 }
87 std::cout << "\n";
88
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;
97 std::cout << "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;
Chris Lattner8d0a23a2003-08-31 19:27:11 +0000101 std::cout << "\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)
104 std::cout << ((*I)->getFunction() ? (*I)->getFunction()->getName()
105 : std::string("Indirect CallGraph node")) << ", ";
Chris Lattner9f2a06e2003-08-31 19:51:38 +0000106 if (nextSCC.size() == 1 && SCCI.hasLoop())
Chris Lattner8d0a23a2003-08-31 19:27:11 +0000107 std::cout << " (Has self-loop).";
108 }
109 std::cout << "\n";
110
111 return true;
112}