blob: 00282140730f1322a3c05ed485cbd2e471c53a01 [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();
Chandler Carruth6378cf52013-11-26 04:19:30 +000061 AU.addRequired<CallGraphWrapperPass>();
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:";
Duncan P. N. Exon Smith8e661ef2014-02-04 19:19:07 +000077 for (scc_iterator<Function*> SCCI = scc_begin(&F); !SCCI.isAtEnd(); ++SCCI) {
Chris Lattnerbb6fe252003-08-31 19:55:06 +000078 std::vector<BasicBlock*> &nextSCC = *SCCI;
Dan Gohmand3ee4232010-08-20 01:03:44 +000079 errs() << "\nSCC #" << ++sccNum << " : ";
Chris Lattner4336db82003-08-31 19:51:38 +000080 for (std::vector<BasicBlock*>::const_iterator I = nextSCC.begin(),
Chris Lattner2f120bb2003-08-31 19:27:11 +000081 E = nextSCC.end(); I != E; ++I)
Dan Gohmand3ee4232010-08-20 01:03:44 +000082 errs() << (*I)->getName() << ", ";
Chris Lattner4336db82003-08-31 19:51:38 +000083 if (nextSCC.size() == 1 && SCCI.hasLoop())
Dan Gohmand3ee4232010-08-20 01:03:44 +000084 errs() << " (Has self-loop).";
Chris Lattner2f120bb2003-08-31 19:27:11 +000085 }
Dan Gohmand3ee4232010-08-20 01:03:44 +000086 errs() << "\n";
Chris Lattner2f120bb2003-08-31 19:27:11 +000087
88 return true;
89}
90
91
92// run - Print out SCCs in the call graph for the specified module.
Chris Lattner13541912004-09-20 04:44:31 +000093bool CallGraphSCC::runOnModule(Module &M) {
Chandler Carruth16f56b42013-11-27 01:32:17 +000094 CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
Chris Lattner2f120bb2003-08-31 19:27:11 +000095 unsigned sccNum = 0;
Dan Gohmand3ee4232010-08-20 01:03:44 +000096 errs() << "SCCs for the program in PostOrder:";
Duncan P. N. Exon Smith8e661ef2014-02-04 19:19:07 +000097 for (scc_iterator<CallGraph*> SCCI = scc_begin(&CG); !SCCI.isAtEnd();
98 ++SCCI) {
Chris Lattnerbb6fe252003-08-31 19:55:06 +000099 const std::vector<CallGraphNode*> &nextSCC = *SCCI;
Dan Gohmand3ee4232010-08-20 01:03:44 +0000100 errs() << "\nSCC #" << ++sccNum << " : ";
Chris Lattner4336db82003-08-31 19:51:38 +0000101 for (std::vector<CallGraphNode*>::const_iterator I = nextSCC.begin(),
Chris Lattner2f120bb2003-08-31 19:27:11 +0000102 E = nextSCC.end(); I != E; ++I)
Benjamin Kramer1f97a5a2011-11-15 16:27:03 +0000103 errs() << ((*I)->getFunction() ? (*I)->getFunction()->getName()
104 : "external node") << ", ";
Chris Lattner4336db82003-08-31 19:51:38 +0000105 if (nextSCC.size() == 1 && SCCI.hasLoop())
Dan Gohmand3ee4232010-08-20 01:03:44 +0000106 errs() << " (Has self-loop).";
Chris Lattner2f120bb2003-08-31 19:27:11 +0000107 }
Dan Gohmand3ee4232010-08-20 01:03:44 +0000108 errs() << "\n";
Chris Lattner2f120bb2003-08-31 19:27:11 +0000109
110 return true;
111}