[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / llvm / lib / Transforms / Utils / InstructionNamer.cpp
blobf3499c9c8aed9bf2328c0c9d13975b85c660823c
1 //===- InstructionNamer.cpp - Give anonymous instructions names -----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This is a little utility pass that gives instructions names, this is mostly
10 // useful when diffing the effect of an optimization because deleting an
11 // unnamed instruction can change all other instruction numbering, making the
12 // diff very noisy.
14 //===----------------------------------------------------------------------===//
16 #include "llvm/Transforms/Utils/InstructionNamer.h"
17 #include "llvm/IR/Function.h"
18 #include "llvm/IR/PassManager.h"
19 #include "llvm/IR/Type.h"
20 #include "llvm/InitializePasses.h"
21 #include "llvm/Pass.h"
22 #include "llvm/Transforms/Utils.h"
24 using namespace llvm;
26 namespace {
27 void nameInstructions(Function &F) {
28 for (auto &Arg : F.args()) {
29 if (!Arg.hasName())
30 Arg.setName("arg");
33 for (BasicBlock &BB : F) {
34 if (!BB.hasName())
35 BB.setName("bb");
37 for (Instruction &I : BB) {
38 if (!I.hasName() && !I.getType()->isVoidTy())
39 I.setName("i");
44 struct InstNamer : public FunctionPass {
45 static char ID; // Pass identification, replacement for typeid
46 InstNamer() : FunctionPass(ID) {
47 initializeInstNamerPass(*PassRegistry::getPassRegistry());
50 void getAnalysisUsage(AnalysisUsage &Info) const override {
51 Info.setPreservesAll();
54 bool runOnFunction(Function &F) override {
55 nameInstructions(F);
56 return true;
60 char InstNamer::ID = 0;
61 } // namespace
63 INITIALIZE_PASS(InstNamer, "instnamer",
64 "Assign names to anonymous instructions", false, false)
65 char &llvm::InstructionNamerID = InstNamer::ID;
66 //===----------------------------------------------------------------------===//
68 // InstructionNamer - Give any unnamed non-void instructions "tmp" names.
70 FunctionPass *llvm::createInstructionNamerPass() {
71 return new InstNamer();
74 PreservedAnalyses InstructionNamerPass::run(Function &F,
75 FunctionAnalysisManager &FAM) {
76 nameInstructions(F);
77 return PreservedAnalyses::all();