diff --git a/include/llvm/Transforms/Utils/InstructionNamer.h b/include/llvm/Transforms/Utils/InstructionNamer.h new file mode 100644 index 00000000000..4f4cc2666f1 --- /dev/null +++ b/include/llvm/Transforms/Utils/InstructionNamer.h @@ -0,0 +1,20 @@ +//===- InstructionNamer.h - Give anonymous instructions names -------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_TRANSFORMS_UTILS_INSTRUCTIONNAMER_H +#define LLVM_TRANSFORMS_UTILS_INSTRUCTIONNAMER_H + +#include "llvm/IR/PassManager.h" + +namespace llvm { +struct InstructionNamerPass : PassInfoMixin { + PreservedAnalyses run(Function &, FunctionAnalysisManager &); +}; +} // namespace llvm + +#endif // LLVM_TRANSFORMS_UTILS_INSTRUCTIONNAMER_H diff --git a/lib/Passes/PassBuilder.cpp b/lib/Passes/PassBuilder.cpp index ab4b305f250..389d4843eb8 100644 --- a/lib/Passes/PassBuilder.cpp +++ b/lib/Passes/PassBuilder.cpp @@ -201,6 +201,7 @@ #include "llvm/Transforms/Utils/EntryExitInstrumenter.h" #include "llvm/Transforms/Utils/FixIrreducible.h" #include "llvm/Transforms/Utils/InjectTLIMappings.h" +#include "llvm/Transforms/Utils/InstructionNamer.h" #include "llvm/Transforms/Utils/LCSSA.h" #include "llvm/Transforms/Utils/LibCallsShrinkWrap.h" #include "llvm/Transforms/Utils/LoopSimplify.h" diff --git a/lib/Passes/PassRegistry.def b/lib/Passes/PassRegistry.def index 0cac7c46075..e40860c11d5 100644 --- a/lib/Passes/PassRegistry.def +++ b/lib/Passes/PassRegistry.def @@ -225,6 +225,7 @@ FUNCTION_PASS("no-op-function", NoOpFunctionPass()) FUNCTION_PASS("libcalls-shrinkwrap", LibCallsShrinkWrapPass()) FUNCTION_PASS("lint", LintPass()) FUNCTION_PASS("inject-tli-mappings", InjectTLIMappings()) +FUNCTION_PASS("instnamer", InstructionNamerPass()) FUNCTION_PASS("loweratomic", LowerAtomicPass()) FUNCTION_PASS("lower-expect", LowerExpectIntrinsicPass()) FUNCTION_PASS("lower-guard-intrinsic", LowerGuardIntrinsicPass()) diff --git a/lib/Transforms/Utils/InstructionNamer.cpp b/lib/Transforms/Utils/InstructionNamer.cpp index 8e339fe46d4..f3499c9c8ae 100644 --- a/lib/Transforms/Utils/InstructionNamer.cpp +++ b/lib/Transforms/Utils/InstructionNamer.cpp @@ -13,43 +13,52 @@ // //===----------------------------------------------------------------------===// +#include "llvm/Transforms/Utils/InstructionNamer.h" #include "llvm/IR/Function.h" +#include "llvm/IR/PassManager.h" #include "llvm/IR/Type.h" #include "llvm/InitializePasses.h" #include "llvm/Pass.h" #include "llvm/Transforms/Utils.h" + using namespace llvm; namespace { - struct InstNamer : public FunctionPass { - static char ID; // Pass identification, replacement for typeid - InstNamer() : FunctionPass(ID) { - initializeInstNamerPass(*PassRegistry::getPassRegistry()); +void nameInstructions(Function &F) { + for (auto &Arg : F.args()) { + if (!Arg.hasName()) + Arg.setName("arg"); + } + + for (BasicBlock &BB : F) { + if (!BB.hasName()) + BB.setName("bb"); + + for (Instruction &I : BB) { + if (!I.hasName() && !I.getType()->isVoidTy()) + I.setName("i"); } + } +} - void getAnalysisUsage(AnalysisUsage &Info) const override { - Info.setPreservesAll(); - } +struct InstNamer : public FunctionPass { + static char ID; // Pass identification, replacement for typeid + InstNamer() : FunctionPass(ID) { + initializeInstNamerPass(*PassRegistry::getPassRegistry()); + } - bool runOnFunction(Function &F) override { - for (auto &Arg : F.args()) - if (!Arg.hasName()) - Arg.setName("arg"); + void getAnalysisUsage(AnalysisUsage &Info) const override { + Info.setPreservesAll(); + } - for (BasicBlock &BB : F) { - if (!BB.hasName()) - BB.setName("bb"); - - for (Instruction &I : BB) - if (!I.hasName() && !I.getType()->isVoidTy()) - I.setName("i"); - } - return true; - } - }; + bool runOnFunction(Function &F) override { + nameInstructions(F); + return true; + } +}; char InstNamer::ID = 0; -} + } // namespace INITIALIZE_PASS(InstNamer, "instnamer", "Assign names to anonymous instructions", false, false) @@ -61,3 +70,9 @@ char &llvm::InstructionNamerID = InstNamer::ID; FunctionPass *llvm::createInstructionNamerPass() { return new InstNamer(); } + +PreservedAnalyses InstructionNamerPass::run(Function &F, + FunctionAnalysisManager &FAM) { + nameInstructions(F); + return PreservedAnalyses::all(); +} diff --git a/test/Transforms/InstNamer/basic.ll b/test/Transforms/InstNamer/basic.ll index 5fbcfca96a4..4fe0369bd67 100644 --- a/test/Transforms/InstNamer/basic.ll +++ b/test/Transforms/InstNamer/basic.ll @@ -1,4 +1,5 @@ ; RUN: opt -S -instnamer < %s | FileCheck %s +; RUN: opt -S -passes=instnamer < %s | FileCheck %s target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-unknown-linux-gnu"