Chris Lattner | 23ebd75 | 2003-08-31 19:23:41 +0000 | [diff] [blame] | 1 | //===- PrintSCC.cpp - Enumerate SCCs in some key graphs -------------------===// |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 21c62da | 2007-12-29 20:44:31 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Vikram S. Adve | c405daf | 2002-11-04 14:20:22 +0000 | [diff] [blame] | 9 | // |
| 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 Lattner | 55b2eb3 | 2003-08-31 20:01:57 +0000 | [diff] [blame] | 12 | // 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. Adve | c405daf | 2002-11-04 14:20:22 +0000 | [diff] [blame] | 16 | // (2) To print out the SCCs for a CFG or a CallGraph: |
Duncan Sands | 3ee8fc9 | 2008-09-23 12:47:39 +0000 | [diff] [blame] | 17 | // 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 Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 20 | // |
Vikram S. Adve | c405daf | 2002-11-04 14:20:22 +0000 | [diff] [blame] | 21 | // and similarly: |
Duncan Sands | 3ee8fc9 | 2008-09-23 12:47:39 +0000 | [diff] [blame] | 22 | // analyze -print-callgraph-sccs [-stats] [-debug] to print SCCs in the CallGraph |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 23 | // |
Chris Lattner | 55b2eb3 | 2003-08-31 20:01:57 +0000 | [diff] [blame] | 24 | // (3) To test the scc_iterator. |
Chris Lattner | 23ebd75 | 2003-08-31 19:23:41 +0000 | [diff] [blame] | 25 | // |
Vikram S. Adve | c405daf | 2002-11-04 14:20:22 +0000 | [diff] [blame] | 26 | //===----------------------------------------------------------------------===// |
| 27 | |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/SCCIterator.h" |
Vikram S. Adve | c405daf | 2002-11-04 14:20:22 +0000 | [diff] [blame] | 29 | #include "llvm/Analysis/CallGraph.h" |
Chandler Carruth | 03e36d7 | 2014-03-04 11:45:46 +0000 | [diff] [blame] | 30 | #include "llvm/IR/CFG.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 31 | #include "llvm/IR/Module.h" |
Chandler Carruth | f010c46 | 2012-12-04 10:44:52 +0000 | [diff] [blame] | 32 | #include "llvm/Pass.h" |
Dan Gohman | ac95cc7 | 2009-07-16 15:30:09 +0000 | [diff] [blame] | 33 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 68d033c | 2004-09-20 04:44:31 +0000 | [diff] [blame] | 34 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 35 | |
Vikram S. Adve | c405daf | 2002-11-04 14:20:22 +0000 | [diff] [blame] | 36 | namespace { |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 37 | struct CFGSCC : public FunctionPass { |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 38 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 39 | CFGSCC() : FunctionPass(ID) {} |
Craig Topper | c83e68f | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 40 | bool runOnFunction(Function& func) override; |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 41 | |
Craig Topper | 573faec | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 42 | void print(raw_ostream &O, const Module* = nullptr) const override { } |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 43 | |
Craig Topper | c83e68f | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 44 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 45 | AU.setPreservesAll(); |
Chris Lattner | 23ebd75 | 2003-08-31 19:23:41 +0000 | [diff] [blame] | 46 | } |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 47 | }; |
Vikram S. Adve | c405daf | 2002-11-04 14:20:22 +0000 | [diff] [blame] | 48 | |
Chris Lattner | 68d033c | 2004-09-20 04:44:31 +0000 | [diff] [blame] | 49 | struct CallGraphSCC : public ModulePass { |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 50 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 51 | CallGraphSCC() : ModulePass(ID) {} |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 52 | |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 53 | // run - Print out SCCs in the call graph for the specified module. |
Craig Topper | c83e68f | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 54 | bool runOnModule(Module &M) override; |
Vikram S. Adve | c405daf | 2002-11-04 14:20:22 +0000 | [diff] [blame] | 55 | |
Craig Topper | 573faec | 2014-04-25 04:24:47 +0000 | [diff] [blame] | 56 | void print(raw_ostream &O, const Module* = nullptr) const override { } |
Vikram S. Adve | c405daf | 2002-11-04 14:20:22 +0000 | [diff] [blame] | 57 | |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 58 | // getAnalysisUsage - This pass requires the CallGraph. |
Craig Topper | c83e68f | 2014-03-08 08:27:28 +0000 | [diff] [blame] | 59 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 60 | AU.setPreservesAll(); |
Chandler Carruth | 54fec07 | 2013-11-26 04:19:30 +0000 | [diff] [blame] | 61 | AU.addRequired<CallGraphWrapperPass>(); |
Chris Lattner | 23ebd75 | 2003-08-31 19:23:41 +0000 | [diff] [blame] | 62 | } |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 63 | }; |
Vikram S. Adve | c405daf | 2002-11-04 14:20:22 +0000 | [diff] [blame] | 64 | } |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 65 | |
Dan Gohman | cfbe401 | 2010-08-20 01:00:03 +0000 | [diff] [blame] | 66 | char CFGSCC::ID = 0; |
| 67 | static RegisterPass<CFGSCC> |
| 68 | Y("print-cfg-sccs", "Print SCCs of each function CFG"); |
| 69 | |
| 70 | char CallGraphSCC::ID = 0; |
| 71 | static RegisterPass<CallGraphSCC> |
| 72 | Z("print-callgraph-sccs", "Print SCCs of the Call Graph"); |
| 73 | |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 74 | bool CFGSCC::runOnFunction(Function &F) { |
| 75 | unsigned sccNum = 0; |
Dan Gohman | 52fdaed | 2010-08-20 01:03:44 +0000 | [diff] [blame] | 76 | errs() << "SCCs for Function " << F.getName() << " in PostOrder:"; |
Duncan P. N. Exon Smith | 483727d | 2014-02-04 19:19:07 +0000 | [diff] [blame] | 77 | for (scc_iterator<Function*> SCCI = scc_begin(&F); !SCCI.isAtEnd(); ++SCCI) { |
Duncan P. N. Exon Smith | db8c1ae | 2014-04-25 18:24:50 +0000 | [diff] [blame] | 78 | const std::vector<BasicBlock *> &nextSCC = *SCCI; |
Dan Gohman | 52fdaed | 2010-08-20 01:03:44 +0000 | [diff] [blame] | 79 | errs() << "\nSCC #" << ++sccNum << " : "; |
Chris Lattner | 9f2a06e | 2003-08-31 19:51:38 +0000 | [diff] [blame] | 80 | for (std::vector<BasicBlock*>::const_iterator I = nextSCC.begin(), |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 81 | E = nextSCC.end(); I != E; ++I) |
Dan Gohman | 52fdaed | 2010-08-20 01:03:44 +0000 | [diff] [blame] | 82 | errs() << (*I)->getName() << ", "; |
Chris Lattner | 9f2a06e | 2003-08-31 19:51:38 +0000 | [diff] [blame] | 83 | if (nextSCC.size() == 1 && SCCI.hasLoop()) |
Dan Gohman | 52fdaed | 2010-08-20 01:03:44 +0000 | [diff] [blame] | 84 | errs() << " (Has self-loop)."; |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 85 | } |
Dan Gohman | 52fdaed | 2010-08-20 01:03:44 +0000 | [diff] [blame] | 86 | errs() << "\n"; |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 87 | |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | |
| 92 | // run - Print out SCCs in the call graph for the specified module. |
Chris Lattner | 68d033c | 2004-09-20 04:44:31 +0000 | [diff] [blame] | 93 | bool CallGraphSCC::runOnModule(Module &M) { |
Chandler Carruth | df13080 | 2013-11-27 01:32:17 +0000 | [diff] [blame] | 94 | CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph(); |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 95 | unsigned sccNum = 0; |
Dan Gohman | 52fdaed | 2010-08-20 01:03:44 +0000 | [diff] [blame] | 96 | errs() << "SCCs for the program in PostOrder:"; |
Duncan P. N. Exon Smith | 483727d | 2014-02-04 19:19:07 +0000 | [diff] [blame] | 97 | for (scc_iterator<CallGraph*> SCCI = scc_begin(&CG); !SCCI.isAtEnd(); |
| 98 | ++SCCI) { |
Chris Lattner | 729d73d | 2003-08-31 19:55:06 +0000 | [diff] [blame] | 99 | const std::vector<CallGraphNode*> &nextSCC = *SCCI; |
Dan Gohman | 52fdaed | 2010-08-20 01:03:44 +0000 | [diff] [blame] | 100 | errs() << "\nSCC #" << ++sccNum << " : "; |
Chris Lattner | 9f2a06e | 2003-08-31 19:51:38 +0000 | [diff] [blame] | 101 | for (std::vector<CallGraphNode*>::const_iterator I = nextSCC.begin(), |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 102 | E = nextSCC.end(); I != E; ++I) |
Benjamin Kramer | a7b0cb7 | 2011-11-15 16:27:03 +0000 | [diff] [blame] | 103 | errs() << ((*I)->getFunction() ? (*I)->getFunction()->getName() |
| 104 | : "external node") << ", "; |
Chris Lattner | 9f2a06e | 2003-08-31 19:51:38 +0000 | [diff] [blame] | 105 | if (nextSCC.size() == 1 && SCCI.hasLoop()) |
Dan Gohman | 52fdaed | 2010-08-20 01:03:44 +0000 | [diff] [blame] | 106 | errs() << " (Has self-loop)."; |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 107 | } |
Dan Gohman | 52fdaed | 2010-08-20 01:03:44 +0000 | [diff] [blame] | 108 | errs() << "\n"; |
Chris Lattner | 8d0a23a | 2003-08-31 19:27:11 +0000 | [diff] [blame] | 109 | |
| 110 | return true; |
| 111 | } |