1 #include "llvm/IR/Function.h" 2 #include "llvm/IR/LegacyPassManager.h" 3 #include "llvm/Pass.h" 4 #include "llvm/Passes/PassBuilder.h" 5 #include "llvm/Passes/PassPlugin.h" 6 #include "llvm/Support/CommandLine.h" 7 #include "llvm/Support/raw_ostream.h" 8 #include "llvm/Transforms/IPO/PassManagerBuilder.h" 9 10 using namespace llvm; 11 12 static cl::opt<bool> Wave("wave-goodbye", cl::init(false), 13 cl::desc("wave good bye")); 14 15 namespace { 16 17 bool runBye(Function &F) { 18 if (Wave) { 19 errs() << "Bye: "; 20 errs().write_escaped(F.getName()) << '\n'; 21 } 22 return false; 23 } 24 25 struct LegacyBye : public FunctionPass { 26 static char ID; 27 LegacyBye() : FunctionPass(ID) {} 28 bool runOnFunction(Function &F) override { return runBye(F); } 29 }; 30 31 struct Bye : PassInfoMixin<Bye> { 32 PreservedAnalyses run(Function &F, FunctionAnalysisManager &) { 33 if (!runBye(F)) 34 return PreservedAnalyses::all(); 35 return PreservedAnalyses::none(); 36 } 37 }; 38 39 } // namespace 40 41 char LegacyBye::ID = 0; 42 43 static RegisterPass<LegacyBye> X("goodbye", "Good Bye World Pass", 44 false /* Only looks at CFG */, 45 false /* Analysis Pass */); 46 47 /* Legacy PM Registration */ 48 static llvm::RegisterStandardPasses RegisterBye( 49 llvm::PassManagerBuilder::EP_VectorizerStart, 50 [](const llvm::PassManagerBuilder &Builder, 51 llvm::legacy::PassManagerBase &PM) { PM.add(new LegacyBye()); }); 52 53 static llvm::RegisterStandardPasses RegisterByeLTO( 54 llvm::PassManagerBuilder::EP_ModuleOptimizerEarly, 55 [](const llvm::PassManagerBuilder &Builder, 56 llvm::legacy::PassManagerBase &PM) { PM.add(new LegacyBye()); }); 57 58 /* New PM Registration */ 59 llvm::PassPluginLibraryInfo getByePluginInfo() { 60 return {LLVM_PLUGIN_API_VERSION, "Bye", LLVM_VERSION_STRING, 61 [](PassBuilder &PB) { 62 PB.registerVectorizerStartEPCallback( 63 [](llvm::FunctionPassManager &PM, OptimizationLevel Level) { 64 PM.addPass(Bye()); 65 }); 66 PB.registerPipelineParsingCallback( 67 [](StringRef Name, llvm::FunctionPassManager &PM, 68 ArrayRef<llvm::PassBuilder::PipelineElement>) { 69 if (Name == "goodbye") { 70 PM.addPass(Bye()); 71 return true; 72 } 73 return false; 74 }); 75 }}; 76 } 77 78 #ifndef LLVM_BYE_LINK_INTO_TOOLS 79 extern "C" LLVM_ATTRIBUTE_WEAK ::llvm::PassPluginLibraryInfo 80 llvmGetPassPluginInfo() { 81 return getByePluginInfo(); 82 } 83 #endif 84