diff --git a/llvm/include/llvm/Analysis/CFGSCCPrinter.h b/llvm/include/llvm/Analysis/CFGSCCPrinter.h new file mode 100644 --- /dev/null +++ b/llvm/include/llvm/Analysis/CFGSCCPrinter.h @@ -0,0 +1,25 @@ +//===-- CFGSCCPrinter.h ---------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_ANALYSIS_CFGSCCPRINTER_H +#define LLVM_ANALYSIS_CFGSCCPRINTER_H + +#include "llvm/IR/PassManager.h" + +namespace llvm { + +class CFGSCCPrinterPass : public PassInfoMixin { + raw_ostream &OS; + +public: + explicit CFGSCCPrinterPass(raw_ostream &OS) : OS(OS) {} + PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM); +}; +} // namespace llvm + +#endif diff --git a/llvm/lib/Analysis/CFGSCCPrinter.cpp b/llvm/lib/Analysis/CFGSCCPrinter.cpp new file mode 100644 --- /dev/null +++ b/llvm/lib/Analysis/CFGSCCPrinter.cpp @@ -0,0 +1,36 @@ +//===- CFGSCCPrinter.cpp --------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "llvm/Analysis/CFGSCCPrinter.h" +#include "llvm/ADT/SCCIterator.h" +#include "llvm/IR/CFG.h" + +using namespace llvm; + +PreservedAnalyses CFGSCCPrinterPass::run(Function &F, + FunctionAnalysisManager &AM) { + unsigned SccNum = 0; + OS << "SCCs for Function " << F.getName() << " in PostOrder:"; + for (scc_iterator SCCI = scc_begin(&F); !SCCI.isAtEnd(); ++SCCI) { + const std::vector &NextSCC = *SCCI; + OS << "\nSCC #" << ++SccNum << ": "; + bool First = true; + for (BasicBlock *BB : NextSCC) { + if (First) + First = false; + else + OS << ", "; + BB->printAsOperand(OS, false); + } + if (NextSCC.size() == 1 && SCCI.hasCycle()) + OS << " (Has self-loop)."; + } + OS << "\n"; + + return PreservedAnalyses::all(); +} \ No newline at end of file diff --git a/llvm/lib/Analysis/CMakeLists.txt b/llvm/lib/Analysis/CMakeLists.txt --- a/llvm/lib/Analysis/CMakeLists.txt +++ b/llvm/lib/Analysis/CMakeLists.txt @@ -46,6 +46,7 @@ BranchProbabilityInfo.cpp CFG.cpp CFGPrinter.cpp + CFGSCCPrinter.cpp CFLAndersAliasAnalysis.cpp CFLSteensAliasAnalysis.cpp CGSCCPassManager.cpp diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp --- a/llvm/lib/Passes/PassBuilder.cpp +++ b/llvm/lib/Passes/PassBuilder.cpp @@ -23,6 +23,7 @@ #include "llvm/Analysis/BlockFrequencyInfo.h" #include "llvm/Analysis/BranchProbabilityInfo.h" #include "llvm/Analysis/CFGPrinter.h" +#include "llvm/Analysis/CFGSCCPrinter.h" #include "llvm/Analysis/CFLAndersAliasAnalysis.h" #include "llvm/Analysis/CFLSteensAliasAnalysis.h" #include "llvm/Analysis/CGSCCPassManager.h" diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def --- a/llvm/lib/Passes/PassRegistry.def +++ b/llvm/lib/Passes/PassRegistry.def @@ -360,6 +360,7 @@ FUNCTION_PASS("print", LoopAccessInfoPrinterPass(dbgs())) // TODO: rename to print after NPM switch FUNCTION_PASS("print-alias-sets", AliasSetsPrinterPass(dbgs())) +FUNCTION_PASS("print-cfg-sccs", CFGSCCPrinterPass(dbgs())) FUNCTION_PASS("print-predicateinfo", PredicateInfoPrinterPass(dbgs())) FUNCTION_PASS("print-mustexecute", MustExecutePrinterPass(dbgs())) FUNCTION_PASS("print-memderefs", MemDerefPrinterPass(dbgs())) diff --git a/llvm/test/Other/print-cfg-scc.ll b/llvm/test/Other/print-cfg-scc.ll new file mode 100644 --- /dev/null +++ b/llvm/test/Other/print-cfg-scc.ll @@ -0,0 +1,31 @@ +; RUN: opt < %s -passes=print-cfg-sccs -disable-output 2>&1 | FileCheck %s + +; CHECK: SCC #1: %UnifiedExitNode +; CHECK: SCC #2: %loopexit.5, %loopexit.6, %loopentry.7, %loopentry.6, %loopentry.5, %endif.2 +; CHECK: SCC #3: %entry + +define void @getAndMoveToFrontDecode() { +entry: + br label %endif.2 + +endif.2: ; preds = %loopexit.5, %entry + br i1 false, label %loopentry.5, label %UnifiedExitNode + +loopentry.5: ; preds = %loopexit.6, %endif.2 + br i1 false, label %loopentry.6, label %UnifiedExitNode + +loopentry.6: ; preds = %loopentry.7, %loopentry.5 + br i1 false, label %loopentry.7, label %loopexit.6 + +loopentry.7: ; preds = %loopentry.7, %loopentry.6 + br i1 false, label %loopentry.7, label %loopentry.6 + +loopexit.6: ; preds = %loopentry.6 + br i1 false, label %loopentry.5, label %loopexit.5 + +loopexit.5: ; preds = %loopexit.6 + br i1 false, label %endif.2, label %UnifiedExitNode + +UnifiedExitNode: ; preds = %loopexit.5, %loopentry.5, %endif.2 + ret void +} diff --git a/llvm/tools/opt/CMakeLists.txt b/llvm/tools/opt/CMakeLists.txt --- a/llvm/tools/opt/CMakeLists.txt +++ b/llvm/tools/opt/CMakeLists.txt @@ -31,7 +31,6 @@ AnalysisWrappers.cpp BreakpointPrinter.cpp NewPMDriver.cpp - PrintSCC.cpp opt.cpp DEPENDS diff --git a/llvm/tools/opt/PrintSCC.cpp b/llvm/tools/opt/PrintSCC.cpp deleted file mode 100644 --- a/llvm/tools/opt/PrintSCC.cpp +++ /dev/null @@ -1,68 +0,0 @@ -//===- PrintSCC.cpp - Enumerate SCCs in some key graphs -------------------===// -// -// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. -// See https://llvm.org/LICENSE.txt for license information. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception -// -//===----------------------------------------------------------------------===// -// -// This file provides passes to print out SCCs in a CFG or a CallGraph. -// Normally, you would not use these passes; instead, you would use the -// scc_iterator directly to enumerate SCCs and process them in some way. These -// passes serve three purposes: -// -// (1) As a reference for how to use the scc_iterator. -// (2) To print out the SCCs for a CFG or a CallGraph: -// analyze -print-cfg-sccs to print the SCCs in each CFG of a module. -// analyze -print-cfg-sccs -stats to print the #SCCs and the maximum SCC size. -// analyze -print-cfg-sccs -debug > /dev/null to watch the algorithm in action. -// -// and similarly: -// analyze -print-callgraph-sccs [-stats] [-debug] to print SCCs in the CallGraph -// -// (3) To test the scc_iterator. -// -//===----------------------------------------------------------------------===// - -#include "llvm/ADT/SCCIterator.h" -#include "llvm/IR/CFG.h" -#include "llvm/IR/Module.h" -#include "llvm/Pass.h" -#include "llvm/Support/raw_ostream.h" -using namespace llvm; - -namespace { - struct CFGSCC : public FunctionPass { - static char ID; // Pass identification, replacement for typeid - CFGSCC() : FunctionPass(ID) {} - bool runOnFunction(Function& func) override; - - void print(raw_ostream &O, const Module* = nullptr) const override { } - - void getAnalysisUsage(AnalysisUsage &AU) const override { - AU.setPreservesAll(); - } - }; -} - -char CFGSCC::ID = 0; -static RegisterPass -Y("print-cfg-sccs", "Print SCCs of each function CFG"); - -bool CFGSCC::runOnFunction(Function &F) { - unsigned sccNum = 0; - errs() << "SCCs for Function " << F.getName() << " in PostOrder:"; - for (scc_iterator SCCI = scc_begin(&F); !SCCI.isAtEnd(); ++SCCI) { - const std::vector &nextSCC = *SCCI; - errs() << "\nSCC #" << ++sccNum << " : "; - for (BasicBlock *BB : nextSCC) { - BB->printAsOperand(errs(), false); - errs() << ", "; - } - if (nextSCC.size() == 1 && SCCI.hasCycle()) - errs() << " (Has self-loop)."; - } - errs() << "\n"; - - return true; -} diff --git a/llvm/utils/gn/secondary/llvm/lib/Analysis/BUILD.gn b/llvm/utils/gn/secondary/llvm/lib/Analysis/BUILD.gn --- a/llvm/utils/gn/secondary/llvm/lib/Analysis/BUILD.gn +++ b/llvm/utils/gn/secondary/llvm/lib/Analysis/BUILD.gn @@ -25,6 +25,7 @@ "BranchProbabilityInfo.cpp", "CFG.cpp", "CFGPrinter.cpp", + "CFGSCCPrinter.cpp", "CFLAndersAliasAnalysis.cpp", "CFLSteensAliasAnalysis.cpp", "CGSCCPassManager.cpp", diff --git a/llvm/utils/gn/secondary/llvm/tools/opt/BUILD.gn b/llvm/utils/gn/secondary/llvm/tools/opt/BUILD.gn --- a/llvm/utils/gn/secondary/llvm/tools/opt/BUILD.gn +++ b/llvm/utils/gn/secondary/llvm/tools/opt/BUILD.gn @@ -23,7 +23,6 @@ "AnalysisWrappers.cpp", "BreakpointPrinter.cpp", "NewPMDriver.cpp", - "PrintSCC.cpp", "opt.cpp", ]