blob: 419886d6cc60aa52a720543de3c9a7630f442f0a [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Misha Brukman01808ca2005-04-21 21:13:18 +00006//
John Criswell482202a2003-10-20 19:43:21 +00007//===----------------------------------------------------------------------===//
Vikram S. Adve5beed602002-11-04 14:20:22 +00008//
9// This file provides passes to print out SCCs in a CFG or a CallGraph.
10// Normally, you would not use these passes; instead, you would use the
Chris Lattner78a5c1a2003-08-31 20:01:57 +000011// scc_iterator directly to enumerate SCCs and process them in some way. These
12// passes serve three purposes:
13//
14// (1) As a reference for how to use the scc_iterator.
Vikram S. Adve5beed602002-11-04 14:20:22 +000015// (2) To print out the SCCs for a CFG or a CallGraph:
Duncan Sands9c40c282008-09-23 12:47:39 +000016// analyze -print-cfg-sccs to print the SCCs in each CFG of a module.
17// analyze -print-cfg-sccs -stats to print the #SCCs and the maximum SCC size.
18// analyze -print-cfg-sccs -debug > /dev/null to watch the algorithm in action.
Misha Brukman01808ca2005-04-21 21:13:18 +000019//
Vikram S. Adve5beed602002-11-04 14:20:22 +000020// and similarly:
Duncan Sands9c40c282008-09-23 12:47:39 +000021// analyze -print-callgraph-sccs [-stats] [-debug] to print SCCs in the CallGraph
Misha Brukman01808ca2005-04-21 21:13:18 +000022//
Chris Lattner78a5c1a2003-08-31 20:01:57 +000023// (3) To test the scc_iterator.
Chris Lattnerc4caf3a2003-08-31 19:23:41 +000024//
Vikram S. Adve5beed602002-11-04 14:20:22 +000025//===----------------------------------------------------------------------===//
26
Chandler Carruth4d88a1c2012-12-04 10:44:52 +000027#include "llvm/ADT/SCCIterator.h"
Vikram S. Adve5beed602002-11-04 14:20:22 +000028#include "llvm/Analysis/CallGraph.h"
Chandler Carruth1305dc32014-03-04 11:45:46 +000029#include "llvm/IR/CFG.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"
Dan Gohmanee051522009-07-16 15:30:09 +000032#include "llvm/Support/raw_ostream.h"
Chris Lattner13541912004-09-20 04:44:31 +000033using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000034
Vikram S. Adve5beed602002-11-04 14:20:22 +000035namespace {
Chris Lattner2f120bb2003-08-31 19:27:11 +000036 struct CFGSCC : public FunctionPass {
Devang Patel8c78a0b2007-05-03 01:11:54 +000037 static char ID; // Pass identification, replacement for typeid
Owen Andersona7aed182010-08-06 18:33:48 +000038 CFGSCC() : FunctionPass(ID) {}
Craig Toppere56917c2014-03-08 08:27:28 +000039 bool runOnFunction(Function& func) override;
Chris Lattner2f120bb2003-08-31 19:27:11 +000040
Craig Toppere6cb63e2014-04-25 04:24:47 +000041 void print(raw_ostream &O, const Module* = nullptr) const override { }
Chris Lattner2f120bb2003-08-31 19:27:11 +000042
Craig Toppere56917c2014-03-08 08:27:28 +000043 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chris Lattner2f120bb2003-08-31 19:27:11 +000044 AU.setPreservesAll();
Chris Lattnerc4caf3a2003-08-31 19:23:41 +000045 }
Chris Lattner2f120bb2003-08-31 19:27:11 +000046 };
Vikram S. Adve5beed602002-11-04 14:20:22 +000047
Chris Lattner13541912004-09-20 04:44:31 +000048 struct CallGraphSCC : public ModulePass {
Devang Patel8c78a0b2007-05-03 01:11:54 +000049 static char ID; // Pass identification, replacement for typeid
Owen Andersona7aed182010-08-06 18:33:48 +000050 CallGraphSCC() : ModulePass(ID) {}
Devang Patel09f162c2007-05-01 21:15:47 +000051
Chris Lattner2f120bb2003-08-31 19:27:11 +000052 // run - Print out SCCs in the call graph for the specified module.
Craig Toppere56917c2014-03-08 08:27:28 +000053 bool runOnModule(Module &M) override;
Vikram S. Adve5beed602002-11-04 14:20:22 +000054
Craig Toppere6cb63e2014-04-25 04:24:47 +000055 void print(raw_ostream &O, const Module* = nullptr) const override { }
Vikram S. Adve5beed602002-11-04 14:20:22 +000056
Chris Lattner2f120bb2003-08-31 19:27:11 +000057 // getAnalysisUsage - This pass requires the CallGraph.
Craig Toppere56917c2014-03-08 08:27:28 +000058 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chris Lattner2f120bb2003-08-31 19:27:11 +000059 AU.setPreservesAll();
Chandler Carruth6378cf52013-11-26 04:19:30 +000060 AU.addRequired<CallGraphWrapperPass>();
Chris Lattnerc4caf3a2003-08-31 19:23:41 +000061 }
Chris Lattner2f120bb2003-08-31 19:27:11 +000062 };
Vikram S. Adve5beed602002-11-04 14:20:22 +000063}
Chris Lattner2f120bb2003-08-31 19:27:11 +000064
Dan Gohman061cb1c2010-08-20 01:00:03 +000065char CFGSCC::ID = 0;
66static RegisterPass<CFGSCC>
67Y("print-cfg-sccs", "Print SCCs of each function CFG");
68
69char CallGraphSCC::ID = 0;
70static RegisterPass<CallGraphSCC>
71Z("print-callgraph-sccs", "Print SCCs of the Call Graph");
72
Chris Lattner2f120bb2003-08-31 19:27:11 +000073bool CFGSCC::runOnFunction(Function &F) {
74 unsigned sccNum = 0;
Dan Gohmand3ee4232010-08-20 01:03:44 +000075 errs() << "SCCs for Function " << F.getName() << " in PostOrder:";
Duncan P. N. Exon Smith8e661ef2014-02-04 19:19:07 +000076 for (scc_iterator<Function*> SCCI = scc_begin(&F); !SCCI.isAtEnd(); ++SCCI) {
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +000077 const std::vector<BasicBlock *> &nextSCC = *SCCI;
Dan Gohmand3ee4232010-08-20 01:03:44 +000078 errs() << "\nSCC #" << ++sccNum << " : ";
Chris Lattner4336db82003-08-31 19:51:38 +000079 for (std::vector<BasicBlock*>::const_iterator I = nextSCC.begin(),
Chris Lattner2f120bb2003-08-31 19:27:11 +000080 E = nextSCC.end(); I != E; ++I)
Dan Gohmand3ee4232010-08-20 01:03:44 +000081 errs() << (*I)->getName() << ", ";
Chris Lattner4336db82003-08-31 19:51:38 +000082 if (nextSCC.size() == 1 && SCCI.hasLoop())
Dan Gohmand3ee4232010-08-20 01:03:44 +000083 errs() << " (Has self-loop).";
Chris Lattner2f120bb2003-08-31 19:27:11 +000084 }
Dan Gohmand3ee4232010-08-20 01:03:44 +000085 errs() << "\n";
Chris Lattner2f120bb2003-08-31 19:27:11 +000086
87 return true;
88}
89
90
91// run - Print out SCCs in the call graph for the specified module.
Chris Lattner13541912004-09-20 04:44:31 +000092bool CallGraphSCC::runOnModule(Module &M) {
Chandler Carruth16f56b42013-11-27 01:32:17 +000093 CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
Chris Lattner2f120bb2003-08-31 19:27:11 +000094 unsigned sccNum = 0;
Dan Gohmand3ee4232010-08-20 01:03:44 +000095 errs() << "SCCs for the program in PostOrder:";
Duncan P. N. Exon Smith8e661ef2014-02-04 19:19:07 +000096 for (scc_iterator<CallGraph*> SCCI = scc_begin(&CG); !SCCI.isAtEnd();
97 ++SCCI) {
Chris Lattnerbb6fe252003-08-31 19:55:06 +000098 const std::vector<CallGraphNode*> &nextSCC = *SCCI;
Dan Gohmand3ee4232010-08-20 01:03:44 +000099 errs() << "\nSCC #" << ++sccNum << " : ";
Chris Lattner4336db82003-08-31 19:51:38 +0000100 for (std::vector<CallGraphNode*>::const_iterator I = nextSCC.begin(),
Chris Lattner2f120bb2003-08-31 19:27:11 +0000101 E = nextSCC.end(); I != E; ++I)
Benjamin Kramer1f97a5a2011-11-15 16:27:03 +0000102 errs() << ((*I)->getFunction() ? (*I)->getFunction()->getName()
103 : "external node") << ", ";
Chris Lattner4336db82003-08-31 19:51:38 +0000104 if (nextSCC.size() == 1 && SCCI.hasLoop())
Dan Gohmand3ee4232010-08-20 01:03:44 +0000105 errs() << " (Has self-loop).";
Chris Lattner2f120bb2003-08-31 19:27:11 +0000106 }
Dan Gohmand3ee4232010-08-20 01:03:44 +0000107 errs() << "\n";
Chris Lattner2f120bb2003-08-31 19:27:11 +0000108
109 return true;
110}