Index: llvm/include/llvm/IR/LegacyPassManager.h =================================================================== --- llvm/include/llvm/IR/LegacyPassManager.h +++ llvm/include/llvm/IR/LegacyPassManager.h @@ -17,7 +17,10 @@ #define LLVM_IR_LEGACYPASSMANAGER_H #include "llvm/Pass.h" +#include "llvm/Bitcode/BitcodeWriterPass.h" +#include "llvm/IR/IRPrintingPasses.h" #include "llvm/Support/CBindingWrapping.h" +#include "llvm/Transforms/Utils/Debugify.h" namespace llvm { @@ -92,6 +95,53 @@ Module *M; }; +/// DebugifyCustomPassManager wraps each pass with the debugify passes if +/// needed. +class DebugifyCustomPassManager : public legacy::PassManager { + DebugifyStatsMap DIStatsMap; + bool EnableDebugifyEach = false; + +public: + using super = legacy::PassManager; + + void add(Pass *P) override { + // Wrap each pass with (-check)-debugify passes if requested, making + // exceptions for passes which shouldn't see -debugify instrumentation. + bool WrapWithDebugify = EnableDebugifyEach && !P->getAsImmutablePass() && + !isIRPrintingPass(P) && !isBitcodeWriterPass(P); + if (!WrapWithDebugify) { + super::add(P); + return; + } + + // Apply -debugify/-check-debugify before/after each pass and collect + // debug info loss statistics. + PassKind Kind = P->getPassKind(); + StringRef Name = P->getPassName(); + + // TODO: Implement Debugify for LoopPass. + switch (Kind) { + case PT_Function: + super::add(createDebugifyFunctionPass()); + super::add(P); + super::add(createCheckDebugifyFunctionPass(true, Name, &DIStatsMap)); + break; + case PT_Module: + super::add(createDebugifyModulePass()); + super::add(P); + super::add(createCheckDebugifyModulePass(true, Name, &DIStatsMap)); + break; + default: + super::add(P); + break; + } + } + + void enableDebugifyEach() { EnableDebugifyEach = true; } + + const DebugifyStatsMap &getDebugifyStatsMap() const { return DIStatsMap; } +}; + } // End legacy namespace // Create wrappers for C Binding types (see CBindingWrapping.h). Index: llvm/lib/IR/LegacyPassManager.cpp =================================================================== --- llvm/lib/IR/LegacyPassManager.cpp +++ llvm/lib/IR/LegacyPassManager.cpp @@ -1316,7 +1316,7 @@ // FunctionPassManager implementation /// Create new Function pass manager -FunctionPassManager::FunctionPassManager(Module *m) : M(m) { +llvm::legacy::FunctionPassManager::FunctionPassManager(Module *m) : M(m) { FPM = new FunctionPassManagerImpl(); // FPM is the top level manager. FPM->setTopLevelManager(FPM); @@ -1325,11 +1325,11 @@ FPM->setResolver(AR); } -FunctionPassManager::~FunctionPassManager() { +llvm::legacy::FunctionPassManager::~FunctionPassManager() { delete FPM; } -void FunctionPassManager::add(Pass *P) { +void llvm::legacy::FunctionPassManager::add(Pass *P) { FPM->add(P); } @@ -1337,7 +1337,7 @@ /// track of whether any of the passes modifies the function, and if /// so, return true. /// -bool FunctionPassManager::run(Function &F) { +bool llvm::legacy::FunctionPassManager::run(Function &F) { handleAllErrors(F.materialize(), [&](ErrorInfoBase &EIB) { report_fatal_error("Error reading bitcode file: " + EIB.message()); }); @@ -1347,13 +1347,13 @@ /// doInitialization - Run all of the initializers for the function passes. /// -bool FunctionPassManager::doInitialization() { +bool llvm::legacy::FunctionPassManager::doInitialization() { return FPM->doInitialization(*M); } /// doFinalization - Run all of the finalizers for the function passes. /// -bool FunctionPassManager::doFinalization() { +bool llvm::legacy::FunctionPassManager::doFinalization() { return FPM->doFinalization(*M); } @@ -1706,23 +1706,23 @@ // PassManager implementation /// Create new pass manager -PassManager::PassManager() { +llvm::legacy::PassManager::PassManager() { PM = new PassManagerImpl(); // PM is the top level manager PM->setTopLevelManager(PM); } -PassManager::~PassManager() { +llvm::legacy::PassManager::~PassManager() { delete PM; } -void PassManager::add(Pass *P) { +void llvm::legacy::PassManager::add(Pass *P) { PM->add(P); } /// run - Execute all of the passes scheduled for execution. Keep track of /// whether any of the passes modifies the module, and if so, return true. -bool PassManager::run(Module &M) { +bool llvm::legacy::PassManager::run(Module &M) { return PM->run(M); } Index: llvm/tools/opt/opt.cpp =================================================================== --- llvm/tools/opt/opt.cpp +++ llvm/tools/opt/opt.cpp @@ -22,13 +22,11 @@ #include "llvm/Analysis/TargetLibraryInfo.h" #include "llvm/Analysis/TargetTransformInfo.h" #include "llvm/AsmParser/Parser.h" -#include "llvm/Bitcode/BitcodeWriterPass.h" #include "llvm/CodeGen/CommandFlags.h" #include "llvm/CodeGen/TargetPassConfig.h" #include "llvm/Config/llvm-config.h" #include "llvm/IR/DataLayout.h" #include "llvm/IR/DebugInfo.h" -#include "llvm/IR/IRPrintingPasses.h" #include "llvm/IR/LLVMContext.h" #include "llvm/IR/LLVMRemarkStreamer.h" #include "llvm/IR/LegacyPassManager.h" @@ -330,48 +328,6 @@ cl::desc("Path to the instrumented context sensitive profile."), cl::Hidden); -class OptCustomPassManager : public legacy::PassManager { - DebugifyStatsMap DIStatsMap; - -public: - using super = legacy::PassManager; - - void add(Pass *P) override { - // Wrap each pass with (-check)-debugify passes if requested, making - // exceptions for passes which shouldn't see -debugify instrumentation. - bool WrapWithDebugify = DebugifyEach && !P->getAsImmutablePass() && - !isIRPrintingPass(P) && !isBitcodeWriterPass(P); - if (!WrapWithDebugify) { - super::add(P); - return; - } - - // Apply -debugify/-check-debugify before/after each pass and collect - // debug info loss statistics. - PassKind Kind = P->getPassKind(); - StringRef Name = P->getPassName(); - - // TODO: Implement Debugify for LoopPass. - switch (Kind) { - case PT_Function: - super::add(createDebugifyFunctionPass()); - super::add(P); - super::add(createCheckDebugifyFunctionPass(true, Name, &DIStatsMap)); - break; - case PT_Module: - super::add(createDebugifyModulePass()); - super::add(P); - super::add(createCheckDebugifyModulePass(true, Name, &DIStatsMap)); - break; - default: - super::add(P); - break; - } - } - - const DebugifyStatsMap &getDebugifyStatsMap() const { return DIStatsMap; } -}; - static inline void addPass(legacy::PassManagerBase &PM, Pass *P) { // Add the pass to the pass manager... PM.add(P); @@ -760,8 +716,12 @@ } // Create a PassManager to hold and optimize the collection of passes we are - // about to build. - OptCustomPassManager Passes; + // about to build. If the -debugify-each option is set, wrap each pass with + // the (-check)-debugify passes. + llvm::legacy::DebugifyCustomPassManager Passes; + if (DebugifyEach) + Passes.enableDebugifyEach(); + bool AddOneTimeDebugifyPasses = EnableDebugify && !DebugifyEach; // Add an appropriate TargetLibraryInfo pass for the module's triple.