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.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #include "llvm/IR/Function.h"
17 #include "llvm/IR/Type.h"
18 #include "llvm/InitializePasses.h"
19 #include "llvm/Pass.h"
20 #include "llvm/Transforms/Utils.h"
21 using namespace llvm;
22 
23 namespace {
24   struct InstNamer : public FunctionPass {
25     static char ID; // Pass identification, replacement for typeid
26     InstNamer() : FunctionPass(ID) {
27       initializeInstNamerPass(*PassRegistry::getPassRegistry());
28     }
29 
30     void getAnalysisUsage(AnalysisUsage &Info) const override {
31       Info.setPreservesAll();
32     }
33 
34     bool runOnFunction(Function &F) override {
35       for (auto &Arg : F.args())
36         if (!Arg.hasName())
37           Arg.setName("arg");
38 
39       for (BasicBlock &BB : F) {
40         if (!BB.hasName())
41           BB.setName("bb");
42 
43         for (Instruction &I : BB)
44           if (!I.hasName() && !I.getType()->isVoidTy())
45             I.setName("tmp");
46       }
47       return true;
48     }
49   };
50 
51   char InstNamer::ID = 0;
52 }
53 
54 INITIALIZE_PASS(InstNamer, "instnamer",
55                 "Assign names to anonymous instructions", false, false)
56 char &llvm::InstructionNamerID = InstNamer::ID;
57 //===----------------------------------------------------------------------===//
58 //
59 // InstructionNamer - Give any unnamed non-void instructions "tmp" names.
60 //
61 FunctionPass *llvm::createInstructionNamerPass() {
62   return new InstNamer();
63 }
64