[llvm] 743087f - Port print-cfg-sccs to new pass manager

Arthur Eubanks via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 18 08:47:20 PDT 2022


Author: Arthur Eubanks
Date: 2022-10-18T08:47:08-07:00
New Revision: 743087fb6306dd03b1aaf2bdb9c877390649bc77

URL: https://github.com/llvm/llvm-project/commit/743087fb6306dd03b1aaf2bdb9c877390649bc77
DIFF: https://github.com/llvm/llvm-project/commit/743087fb6306dd03b1aaf2bdb9c877390649bc77.diff

LOG: 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

Added: 
    llvm/include/llvm/Analysis/CFGSCCPrinter.h
    llvm/lib/Analysis/CFGSCCPrinter.cpp
    llvm/test/Other/print-cfg-scc.ll

Modified: 
    llvm/lib/Analysis/CMakeLists.txt
    llvm/lib/Passes/PassBuilder.cpp
    llvm/lib/Passes/PassRegistry.def
    llvm/tools/opt/CMakeLists.txt
    llvm/utils/gn/secondary/llvm/lib/Analysis/BUILD.gn
    llvm/utils/gn/secondary/llvm/tools/opt/BUILD.gn

Removed: 
    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 0000000000000..d98071461f578
--- /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<CFGSCCPrinterPass> {
+  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 0000000000000..e1a5f5e7432f1
--- /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<Function *> SCCI = scc_begin(&F); !SCCI.isAtEnd(); ++SCCI) {
+    const std::vector<BasicBlock *> &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 dccd173aa72dd..f8806804b43d9 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 1a4e7eff33edf..46e8b52ac7e86 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 76fc890899c1e..e7a65ae7b7000 100644
--- a/llvm/lib/Passes/PassRegistry.def
+++ b/llvm/lib/Passes/PassRegistry.def
@@ -360,6 +360,7 @@ FUNCTION_PASS("print<stack-safety-local>", StackSafetyPrinterPass(dbgs()))
 FUNCTION_PASS("print<access-info>", LoopAccessInfoPrinterPass(dbgs()))
 // TODO: rename to print<foo> 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 0000000000000..19ac00e3d7641
--- /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 48b090b37639e..e5d9d28d50065 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 f9daa249e7865..0000000000000
--- 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<CFGSCC>
-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<Function*> SCCI = scc_begin(&F); !SCCI.isAtEnd(); ++SCCI) {
-    const std::vector<BasicBlock *> &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 e92ac886fba36..b67b274086b8c 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 9ad220d767280..49b13e1105634 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",
   ]
 


        


More information about the llvm-commits mailing list