#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
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
#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;
}
}
};
-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;
}
}
-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,
--- /dev/null
+; 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
+}
//===----------------------------------------------------------------------===//
#include "Passes.h"
+#include "llvm/IR/IRPrintingPasses.h"
#include "llvm/IR/PassManager.h"
+#include "llvm/Support/Debug.h"
using namespace llvm;
// 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;
}
MPM.addPass(NoOpModulePass());
return true;
}
+ if (Name == "print") {
+ MPM.addPass(PrintModulePass(dbgs()));
+ return true;
+ }
return false;
}
FPM.addPass(NoOpFunctionPass());
return true;
}
+ if (Name == "print") {
+ FPM.addPass(PrintFunctionPass(dbgs()));
+ return true;
+ }
return false;
}