blob: a502fa743c192fc3d72fb78b0ea2e73d6f3633a9 [file] [log] [blame]
Chris Lattnerc4caf3a2003-08-31 19:23:41 +00001//===- PrintSCC.cpp - Enumerate SCCs in some key graphs -------------------===//
Misha Brukman01808ca2005-04-21 21:13:18 +00002//
John Criswell482202a2003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner345353d2007-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 Brukman01808ca2005-04-21 21:13:18 +00007//
John Criswell482202a2003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Vikram S. Adve5beed602002-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 Lattner78a5c1a2003-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. Adve5beed602002-11-04 14:20:22 +000016// (2) To print out the SCCs for a CFG or a CallGraph:
Duncan Sands9c40c282008-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 Brukman01808ca2005-04-21 21:13:18 +000020//
Vikram S. Adve5beed602002-11-04 14:20:22 +000021// and similarly:
Duncan Sands9c40c282008-09-23 12:47:39 +000022// analyze -print-callgraph-sccs [-stats] [-debug] to print SCCs in the CallGraph
Misha Brukman01808ca2005-04-21 21:13:18 +000023//
Chris Lattner78a5c1a2003-08-31 20:01:57 +000024// (3) To test the scc_iterator.
Chris Lattnerc4caf3a2003-08-31 19:23:41 +000025//
Vikram S. Adve5beed602002-11-04 14:20:22 +000026//===----------------------------------------------------------------------===//
27
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000028#include "llvm/ADT/SCCIterator.h"
Vikram S. Adve5beed602002-11-04 14:20:22 +000029#include "llvm/Analysis/CallGraph.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000030#include "llvm/IR/Module.h"
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000031#include "llvm/Pass.h"
Vikram S. Adve5beed602002-11-04 14:20:22 +000032#include "llvm/Support/CFG.h"
Dan Gohmanee051522009-07-16 15:30:09 +000033#include "llvm/Support/raw_ostream.h"
Chris Lattner13541912004-09-20 04:44:31 +000034using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000035
Vikram S. Adve5beed602002-11-04 14:20:22 +000036namespace {
Chris Lattner2f120bb2003-08-31 19:27:11 +000037 struct CFGSCC : public FunctionPass {
Devang Patel8c78a0b2007-05-03 01:11:54 +000038 static char ID; // Pass identification, replacement for typeid
Owen Andersona7aed182010-08-06 18:33:48 +000039 CFGSCC() : FunctionPass(ID) {}
Chris Lattner2f120bb2003-08-31 19:27:11 +000040 bool runOnFunction(Function& func);
41
Chris Lattner13626022009-08-23 06:03:38 +000042 void print(raw_ostream &O, const Module* = 0) const { }
Chris Lattner2f120bb2003-08-31 19:27:11 +000043
44 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
45 AU.setPreservesAll();
Chris Lattnerc4caf3a2003-08-31 19:23:41 +000046 }
Chris Lattner2f120bb2003-08-31 19:27:11 +000047 };
Vikram S. Adve5beed602002-11-04 14:20:22 +000048
Chris Lattner13541912004-09-20 04:44:31 +000049 struct CallGraphSCC : public ModulePass {
Devang Patel8c78a0b2007-05-03 01:11:54 +000050 static char ID; // Pass identification, replacement for typeid
Owen Andersona7aed182010-08-06 18:33:48 +000051 CallGraphSCC() : ModulePass(ID) {}
Devang Patel09f162c2007-05-01 21:15:47 +000052
Chris Lattner2f120bb2003-08-31 19:27:11 +000053 // run - Print out SCCs in the call graph for the specified module.
Chris Lattner13541912004-09-20 04:44:31 +000054 bool runOnModule(Module &M);
Vikram S. Adve5beed602002-11-04 14:20:22 +000055
Chris Lattner13626022009-08-23 06:03:38 +000056 void print(raw_ostream &O, const Module* = 0) const { }
Vikram S. Adve5beed602002-11-04 14:20:22 +000057
Chris Lattner2f120bb2003-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 Lattnerc4caf3a2003-08-31 19:23:41 +000062 }
Chris Lattner2f120bb2003-08-31 19:27:11 +000063 };
Vikram S. Adve5beed602002-11-04 14:20:22 +000064}
Chris Lattner2f120bb2003-08-31 19:27:11 +000065
Dan Gohman061cb1c2010-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 Lattner2f120bb2003-08-31 19:27:11 +000074bool CFGSCC::runOnFunction(Function &F) {
75 unsigned sccNum = 0;
Dan Gohmand3ee4232010-08-20 01:03:44 +000076 errs() << "SCCs for Function " << F.getName() << " in PostOrder:";
Chris Lattner78a5c1a2003-08-31 20:01:57 +000077 for (scc_iterator<Function*> SCCI = scc_begin(&F),
78 E = scc_end(&F); SCCI != E; ++SCCI) {
Chris Lattnerbb6fe252003-08-31 19:55:06 +000079 std::vector<BasicBlock*> &nextSCC = *SCCI;
Dan Gohmand3ee4232010-08-20 01:03:44 +000080 errs() << "\nSCC #" << ++sccNum << " : ";
Chris Lattner4336db82003-08-31 19:51:38 +000081 for (std::vector<BasicBlock*>::const_iterator I = nextSCC.begin(),
Chris Lattner2f120bb2003-08-31 19:27:11 +000082 E = nextSCC.end(); I != E; ++I)
Dan Gohmand3ee4232010-08-20 01:03:44 +000083 errs() << (*I)->getName() << ", ";
Chris Lattner4336db82003-08-31 19:51:38 +000084 if (nextSCC.size() == 1 && SCCI.hasLoop())
Dan Gohmand3ee4232010-08-20 01:03:44 +000085 errs() << " (Has self-loop).";
Chris Lattner2f120bb2003-08-31 19:27:11 +000086 }
Dan Gohmand3ee4232010-08-20 01:03:44 +000087 errs() << "\n";
Chris Lattner2f120bb2003-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 Lattner13541912004-09-20 04:44:31 +000094bool CallGraphSCC::runOnModule(Module &M) {
Chris Lattner2f120bb2003-08-31 19:27:11 +000095 CallGraphNode* rootNode = getAnalysis<CallGraph>().getRoot();
96 unsigned sccNum = 0;
Dan Gohmand3ee4232010-08-20 01:03:44 +000097 errs() << "SCCs for the program in PostOrder:";
Chris Lattner78a5c1a2003-08-31 20:01:57 +000098 for (scc_iterator<CallGraphNode*> SCCI = scc_begin(rootNode),
99 E = scc_end(rootNode); SCCI != E; ++SCCI) {
Chris Lattnerbb6fe252003-08-31 19:55:06 +0000100 const std::vector<CallGraphNode*> &nextSCC = *SCCI;
Dan Gohmand3ee4232010-08-20 01:03:44 +0000101 errs() << "\nSCC #" << ++sccNum << " : ";
Chris Lattner4336db82003-08-31 19:51:38 +0000102 for (std::vector<CallGraphNode*>::const_iterator I = nextSCC.begin(),
Chris Lattner2f120bb2003-08-31 19:27:11 +0000103 E = nextSCC.end(); I != E; ++I)
Benjamin Kramer1f97a5a2011-11-15 16:27:03 +0000104 errs() << ((*I)->getFunction() ? (*I)->getFunction()->getName()
105 : "external node") << ", ";
Chris Lattner4336db82003-08-31 19:51:38 +0000106 if (nextSCC.size() == 1 && SCCI.hasLoop())
Dan Gohmand3ee4232010-08-20 01:03:44 +0000107 errs() << " (Has self-loop).";
Chris Lattner2f120bb2003-08-31 19:27:11 +0000108 }
Dan Gohmand3ee4232010-08-20 01:03:44 +0000109 errs() << "\n";
Chris Lattner2f120bb2003-08-31 19:27:11 +0000110
111 return true;
112}