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