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