From 743087fb6306dd03b1aaf2bdb9c877390649bc77 Mon Sep 17 00:00:00 2001 From: Arthur Eubanks Date: Tue, 11 Oct 2022 15:07:13 -0700 Subject: [PATCH] Port print-cfg-sccs to new pass manager This is actually used, see https://discourse.llvm.org/t/use-print-callgrapg-sccs-from-opt/65782. Reviewed By: asbirlea Differential Revision: https://reviews.llvm.org/D135718 --- llvm/include/llvm/Analysis/CFGSCCPrinter.h | 25 ++++++++ llvm/lib/Analysis/CFGSCCPrinter.cpp | 36 ++++++++++++ llvm/lib/Analysis/CMakeLists.txt | 1 + llvm/lib/Passes/PassBuilder.cpp | 1 + llvm/lib/Passes/PassRegistry.def | 1 + llvm/test/Other/print-cfg-scc.ll | 31 ++++++++++ llvm/tools/opt/CMakeLists.txt | 1 - llvm/tools/opt/PrintSCC.cpp | 68 ---------------------- llvm/utils/gn/secondary/llvm/lib/Analysis/BUILD.gn | 1 + llvm/utils/gn/secondary/llvm/tools/opt/BUILD.gn | 1 - 10 files changed, 96 insertions(+), 70 deletions(-) create mode 100644 llvm/include/llvm/Analysis/CFGSCCPrinter.h create mode 100644 llvm/lib/Analysis/CFGSCCPrinter.cpp create mode 100644 llvm/test/Other/print-cfg-scc.ll delete mode 100644 llvm/tools/opt/PrintSCC.cpp diff --git a/llvm/include/llvm/Analysis/CFGSCCPrinter.h b/llvm/include/llvm/Analysis/CFGSCCPrinter.h new file mode 100644 index 0000000..d980714 --- /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 index 0000000..e1a5f5e --- /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 index dccd173..f880680 100644 --- a/llvm/lib/Analysis/CMakeLists.txt +++ b/llvm/lib/Analysis/CMakeLists.txt @@ -46,6 +46,7 @@ add_llvm_component_library(LLVMAnalysis 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 index 1a4e7ef..46e8b52 100644 --- 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 index 76fc890..e7a65ae 100644 --- a/llvm/lib/Passes/PassRegistry.def +++ b/llvm/lib/Passes/PassRegistry.def @@ -360,6 +360,7 @@ FUNCTION_PASS("print", StackSafetyPrinterPass(dbgs())) 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 index 0000000..19ac00e --- /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 index 48b090b..e5d9d28 100644 --- a/llvm/tools/opt/CMakeLists.txt +++ b/llvm/tools/opt/CMakeLists.txt @@ -31,7 +31,6 @@ add_llvm_tool(opt 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 index f9daa24..0000000 --- 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 index e92ac88..b67b274 100644 --- a/llvm/utils/gn/secondary/llvm/lib/Analysis/BUILD.gn +++ b/llvm/utils/gn/secondary/llvm/lib/Analysis/BUILD.gn @@ -25,6 +25,7 @@ static_library("Analysis") { "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 index 9ad220d..49b13e1 100644 --- a/llvm/utils/gn/secondary/llvm/tools/opt/BUILD.gn +++ b/llvm/utils/gn/secondary/llvm/tools/opt/BUILD.gn @@ -23,7 +23,6 @@ executable("opt") { "AnalysisWrappers.cpp", "BreakpointPrinter.cpp", "NewPMDriver.cpp", - "PrintSCC.cpp", "opt.cpp", ] -- 2.7.4