xref: /llvm-project/llvm/unittests/Passes/Plugins/DoublerPlugin/DoublerPlugin.cpp (revision 74deadf19650f6f3b6392ba09caa20dd38ae41e0)
1 //===- unittests/Passes/DoublerPlugin.cpp
2 //--------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "llvm/IR/Module.h"
11 #include "llvm/Passes/PassBuilder.h"
12 #include "llvm/Passes/PassPlugin.h"
13 
14 using namespace llvm;
15 
16 struct DoublerModulePass : public PassInfoMixin<DoublerModulePass> {
17 
18   // Double the value of the initializer
runDoublerModulePass19   PreservedAnalyses run(Module &M, ModuleAnalysisManager &MAM) {
20     auto *GV = cast<GlobalVariable>(M.getNamedValue("doubleme"));
21     auto *Init = GV->getInitializer();
22     auto *Init2 = ConstantExpr::getAdd(Init, Init);
23     GV->setInitializer(Init2);
24 
25     return PreservedAnalyses::none();
26   }
27 
registerCallbacksDoublerModulePass28   static void registerCallbacks(PassBuilder &PB) {
29     PB.registerPipelineParsingCallback(
30         [](StringRef Name, ModulePassManager &PM,
31            ArrayRef<PassBuilder::PipelineElement> InnerPipeline) {
32           if (Name == "doubler-pass") {
33             PM.addPass(DoublerModulePass());
34             return true;
35           }
36           return false;
37         });
38   }
39 };
40 
41 extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK
llvmGetPassPluginInfo()42 llvmGetPassPluginInfo() {
43   return {LLVM_PLUGIN_API_VERSION, "DoublerPlugin", "2.2-unit",
44           DoublerModulePass::registerCallbacks};
45 }
46