OSDN Git Service

[PM] Add module and function printing passes for the new pass manager.
authorChandler Carruth <chandlerc@gmail.com>
Sun, 12 Jan 2014 12:15:39 +0000 (12:15 +0000)
committerChandler Carruth <chandlerc@gmail.com>
Sun, 12 Jan 2014 12:15:39 +0000 (12:15 +0000)
This implements the legacy passes in terms of the new ones. It adds
basic testing using explicit runs of the passes. Next up will be wiring
the basic output mechanism of opt up when the new pass manager is
engaged unless bitcode writing is requested.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@199049 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/IR/IRPrintingPasses.h
include/llvm/InitializePasses.h
lib/IR/Core.cpp
lib/IR/IRPrintingPasses.cpp
test/Other/new-pass-manager.ll [new file with mode: 0644]
tools/opt/Passes.cpp

index 1d6cf95..3d759ef 100644 (file)
 #ifndef LLVM_IR_PRINTMODULEPASS_H
 #define LLVM_IR_PRINTMODULEPASS_H
 
+#include "llvm/ADT/StringRef.h"
 #include <string>
 
 namespace llvm {
+class BasicBlockPass;
+class Function;
 class FunctionPass;
+class Module;
 class ModulePass;
-class BasicBlockPass;
+class PreservedAnalyses;
 class raw_ostream;
 
 /// \brief Create and return a pass that writes the module to the specified
@@ -42,6 +46,40 @@ FunctionPass *createPrintFunctionPass(raw_ostream &OS,
 BasicBlockPass *createPrintBasicBlockPass(raw_ostream &OS,
                                           const std::string &Banner = "");
 
+/// \brief Pass for printing a Module as LLVM's text IR assembly.
+///
+/// Note: This pass is for use with the new pass manager. Use the create...Pass
+/// functions above to create passes for use with the legacy pass manager.
+class PrintModulePass {
+  raw_ostream &OS;
+  std::string Banner;
+
+public:
+  PrintModulePass();
+  PrintModulePass(raw_ostream &OS, const std::string &Banner = "");
+
+  PreservedAnalyses run(Module *M);
+
+  static StringRef name() { return "PrintModulePass"; }
+};
+
+/// \brief Pass for printing a Function as LLVM's text IR assembly.
+///
+/// Note: This pass is for use with the new pass manager. Use the create...Pass
+/// functions above to create passes for use with the legacy pass manager.
+class PrintFunctionPass {
+  raw_ostream &OS;
+  std::string Banner;
+
+public:
+  PrintFunctionPass();
+  PrintFunctionPass(raw_ostream &OS, const std::string &Banner = "");
+
+  PreservedAnalyses run(Function *F);
+
+  static StringRef name() { return "PrintFunctionPass"; }
+};
+
 } // End llvm namespace
 
 #endif
index e27d6b7..6c7ab11 100644 (file)
@@ -212,8 +212,8 @@ void initializePostDominatorTreePass(PassRegistry&);
 void initializePostRASchedulerPass(PassRegistry&);
 void initializePostMachineSchedulerPass(PassRegistry&);
 void initializePreVerifierPass(PassRegistry&);
-void initializePrintFunctionPassPass(PassRegistry&);
-void initializePrintModulePassPass(PassRegistry&);
+void initializePrintFunctionPassWrapperPass(PassRegistry&);
+void initializePrintModulePassWrapperPass(PassRegistry&);
 void initializePrintBasicBlockPassPass(PassRegistry&);
 void initializeProcessImplicitDefsPass(PassRegistry&);
 void initializePromotePassPass(PassRegistry&);
index c53ea48..31e445e 100644 (file)
@@ -41,8 +41,8 @@ using namespace llvm;
 
 void llvm::initializeCore(PassRegistry &Registry) {
   initializeDominatorTreePass(Registry);
-  initializePrintModulePassPass(Registry);
-  initializePrintFunctionPassPass(Registry);
+  initializePrintModulePassWrapperPass(Registry);
+  initializePrintFunctionPassWrapperPass(Registry);
   initializePrintBasicBlockPassPass(Registry);
   initializeVerifierPass(Registry);
   initializePreVerifierPass(Registry);
index b8e2a2b..13336ba 100644 (file)
 #include "llvm/IR/IRPrintingPasses.h"
 #include "llvm/IR/Function.h"
 #include "llvm/IR/Module.h"
+#include "llvm/IR/PassManager.h"
 #include "llvm/Pass.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/raw_ostream.h"
 using namespace llvm;
 
+PrintModulePass::PrintModulePass() : OS(dbgs()) {}
+PrintModulePass::PrintModulePass(raw_ostream &OS, const std::string &Banner)
+    : OS(OS), Banner(Banner) {}
+
+PreservedAnalyses PrintModulePass::run(Module *M) {
+  OS << Banner << *M;
+  return PreservedAnalyses::all();
+}
+
+PrintFunctionPass::PrintFunctionPass() : OS(dbgs()) {}
+PrintFunctionPass::PrintFunctionPass(raw_ostream &OS, const std::string &Banner)
+    : OS(OS), Banner(Banner) {}
+
+PreservedAnalyses PrintFunctionPass::run(Function *F) {
+  OS << Banner << static_cast<Value &>(*F);
+  return PreservedAnalyses::all();
+}
+
 namespace {
 
-class PrintModulePass : public ModulePass {
-  raw_ostream &Out;
-  std::string Banner;
+class PrintModulePassWrapper : public ModulePass {
+  PrintModulePass P;
 
 public:
   static char ID;
-  PrintModulePass() : ModulePass(ID), Out(dbgs()) {}
-  PrintModulePass(raw_ostream &Out, const std::string &Banner)
-      : ModulePass(ID), Out(Out), Banner(Banner) {}
+  PrintModulePassWrapper() : ModulePass(ID) {}
+  PrintModulePassWrapper(raw_ostream &OS, const std::string &Banner)
+      : ModulePass(ID), P(OS, Banner) {}
 
   bool runOnModule(Module &M) {
-    Out << Banner << M;
+    P.run(&M);
     return false;
   }
 
@@ -41,19 +59,18 @@ public:
   }
 };
 
-class PrintFunctionPass : public FunctionPass {
-  raw_ostream &Out;
-  std::string Banner;
+class PrintFunctionPassWrapper : public FunctionPass {
+  PrintFunctionPass P;
 
 public:
   static char ID;
-  PrintFunctionPass() : FunctionPass(ID), Out(dbgs()) {}
-  PrintFunctionPass(raw_ostream &Out, const std::string &Banner)
-      : FunctionPass(ID), Out(Out), Banner(Banner) {}
+  PrintFunctionPassWrapper() : FunctionPass(ID) {}
+  PrintFunctionPassWrapper(raw_ostream &OS, const std::string &Banner)
+      : FunctionPass(ID), P(OS, Banner) {}
 
   // This pass just prints a banner followed by the function as it's processed.
   bool runOnFunction(Function &F) {
-    Out << Banner << static_cast<Value &>(F);
+    P.run(&F);
     return false;
   }
 
@@ -84,24 +101,24 @@ public:
 
 }
 
-char PrintModulePass::ID = 0;
-INITIALIZE_PASS(PrintModulePass, "print-module", "Print module to stderr",
-                false, false)
-char PrintFunctionPass::ID = 0;
-INITIALIZE_PASS(PrintFunctionPass, "print-function", "Print function to stderr",
-                false, false)
+char PrintModulePassWrapper::ID = 0;
+INITIALIZE_PASS(PrintModulePassWrapper, "print-module",
+                "Print module to stderr", false, false)
+char PrintFunctionPassWrapper::ID = 0;
+INITIALIZE_PASS(PrintFunctionPassWrapper, "print-function",
+                "Print function to stderr", false, false)
 char PrintBasicBlockPass::ID = 0;
 INITIALIZE_PASS(PrintBasicBlockPass, "print-bb", "Print BB to stderr", false,
                 false)
 
 ModulePass *llvm::createPrintModulePass(llvm::raw_ostream &OS,
                                         const std::string &Banner) {
-  return new PrintModulePass(OS, Banner);
+  return new PrintModulePassWrapper(OS, Banner);
 }
 
 FunctionPass *llvm::createPrintFunctionPass(llvm::raw_ostream &OS,
                                             const std::string &Banner) {
-  return new PrintFunctionPass(OS, Banner);
+  return new PrintFunctionPassWrapper(OS, Banner);
 }
 
 BasicBlockPass *llvm::createPrintBasicBlockPass(llvm::raw_ostream &OS,
diff --git a/test/Other/new-pass-manager.ll b/test/Other/new-pass-manager.ll
new file mode 100644 (file)
index 0000000..48b9762
--- /dev/null
@@ -0,0 +1,28 @@
+; This test is essentially doing very basic things with the opt tool and the
+; new pass manager pipeline. It will be used to flesh out the feature
+; completeness of the opt tool when the new pass manager is engaged. The tests
+; may not be useful once it becomes the default or may get spread out into other
+; files, but for now this is just going to step the new process through its
+; paces.
+
+; RUN: opt -disable-output -debug-pass-manager -passes=print %s 2>&1 \
+; RUN:     | FileCheck %s --check-prefix=CHECK-MODULE-PRINT
+; CHECK-MODULE-PRINT: Starting module pass manager
+; CHECK-MODULE-PRINT: Running module pass: PrintModulePass
+; CHECK-MODULE-PRINT: ModuleID
+; CHECK-MODULE-PRINT: define void @foo()
+; CHECK-MODULE-PRINT: Finished module pass manager
+
+; RUN: opt -disable-output -debug-pass-manager -passes='function(print)' %s 2>&1 \
+; RUN:     | FileCheck %s --check-prefix=CHECK-FUNCTION-PRINT
+; CHECK-FUNCTION-PRINT: Starting module pass manager
+; CHECK-FUNCTION-PRINT: Starting function pass manager
+; CHECK-FUNCTION-PRINT: Running function pass: PrintFunctionPass
+; CHECK-FUNCTION-PRINT-NOT: ModuleID
+; CHECK-FUNCTION-PRINT: define void @foo()
+; CHECK-FUNCTION-PRINT: Finished function pass manager
+; CHECK-FUNCTION-PRINT: Finished module pass manager
+
+define void @foo() {
+  ret void
+}
index 29be9de..e79ac42 100644 (file)
@@ -15,7 +15,9 @@
 //===----------------------------------------------------------------------===//
 
 #include "Passes.h"
+#include "llvm/IR/IRPrintingPasses.h"
 #include "llvm/IR/PassManager.h"
+#include "llvm/Support/Debug.h"
 
 using namespace llvm;
 
@@ -39,12 +41,14 @@ struct NoOpFunctionPass {
 // under different macros.
 static bool isModulePassName(StringRef Name) {
   if (Name == "no-op-module") return true;
+  if (Name == "print") return true;
 
   return false;
 }
 
 static bool isFunctionPassName(StringRef Name) {
   if (Name == "no-op-function") return true;
+  if (Name == "print") return true;
 
   return false;
 }
@@ -54,6 +58,10 @@ static bool parseModulePassName(ModulePassManager &MPM, StringRef Name) {
     MPM.addPass(NoOpModulePass());
     return true;
   }
+  if (Name == "print") {
+    MPM.addPass(PrintModulePass(dbgs()));
+    return true;
+  }
   return false;
 }
 
@@ -62,6 +70,10 @@ static bool parseFunctionPassName(FunctionPassManager &FPM, StringRef Name) {
     FPM.addPass(NoOpFunctionPass());
     return true;
   }
+  if (Name == "print") {
+    FPM.addPass(PrintFunctionPass(dbgs()));
+    return true;
+  }
   return false;
 }