xref: /aosp_15_r20/external/llvm/lib/Transforms/Utils/InstructionNamer.cpp (revision 9880d6810fe72a1726cb53787c6711e909410d58)
1*9880d681SAndroid Build Coastguard Worker //===- InstructionNamer.cpp - Give anonymous instructions names -----------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This is a little utility pass that gives instructions names, this is mostly
11*9880d681SAndroid Build Coastguard Worker // useful when diffing the effect of an optimization because deleting an
12*9880d681SAndroid Build Coastguard Worker // unnamed instruction can change all other instruction numbering, making the
13*9880d681SAndroid Build Coastguard Worker // diff very noisy.
14*9880d681SAndroid Build Coastguard Worker //
15*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
16*9880d681SAndroid Build Coastguard Worker 
17*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Scalar.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Function.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Type.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/Pass.h"
21*9880d681SAndroid Build Coastguard Worker using namespace llvm;
22*9880d681SAndroid Build Coastguard Worker 
23*9880d681SAndroid Build Coastguard Worker namespace {
24*9880d681SAndroid Build Coastguard Worker   struct InstNamer : public FunctionPass {
25*9880d681SAndroid Build Coastguard Worker     static char ID; // Pass identification, replacement for typeid
InstNamer__anon40fe029b0111::InstNamer26*9880d681SAndroid Build Coastguard Worker     InstNamer() : FunctionPass(ID) {
27*9880d681SAndroid Build Coastguard Worker       initializeInstNamerPass(*PassRegistry::getPassRegistry());
28*9880d681SAndroid Build Coastguard Worker     }
29*9880d681SAndroid Build Coastguard Worker 
getAnalysisUsage__anon40fe029b0111::InstNamer30*9880d681SAndroid Build Coastguard Worker     void getAnalysisUsage(AnalysisUsage &Info) const override {
31*9880d681SAndroid Build Coastguard Worker       Info.setPreservesAll();
32*9880d681SAndroid Build Coastguard Worker     }
33*9880d681SAndroid Build Coastguard Worker 
runOnFunction__anon40fe029b0111::InstNamer34*9880d681SAndroid Build Coastguard Worker     bool runOnFunction(Function &F) override {
35*9880d681SAndroid Build Coastguard Worker       for (Function::arg_iterator AI = F.arg_begin(), AE = F.arg_end();
36*9880d681SAndroid Build Coastguard Worker            AI != AE; ++AI)
37*9880d681SAndroid Build Coastguard Worker         if (!AI->hasName() && !AI->getType()->isVoidTy())
38*9880d681SAndroid Build Coastguard Worker           AI->setName("arg");
39*9880d681SAndroid Build Coastguard Worker 
40*9880d681SAndroid Build Coastguard Worker       for (BasicBlock &BB : F) {
41*9880d681SAndroid Build Coastguard Worker         if (!BB.hasName())
42*9880d681SAndroid Build Coastguard Worker           BB.setName("bb");
43*9880d681SAndroid Build Coastguard Worker 
44*9880d681SAndroid Build Coastguard Worker         for (Instruction &I : BB)
45*9880d681SAndroid Build Coastguard Worker           if (!I.hasName() && !I.getType()->isVoidTy())
46*9880d681SAndroid Build Coastguard Worker             I.setName("tmp");
47*9880d681SAndroid Build Coastguard Worker       }
48*9880d681SAndroid Build Coastguard Worker       return true;
49*9880d681SAndroid Build Coastguard Worker     }
50*9880d681SAndroid Build Coastguard Worker   };
51*9880d681SAndroid Build Coastguard Worker 
52*9880d681SAndroid Build Coastguard Worker   char InstNamer::ID = 0;
53*9880d681SAndroid Build Coastguard Worker }
54*9880d681SAndroid Build Coastguard Worker 
55*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS(InstNamer, "instnamer",
56*9880d681SAndroid Build Coastguard Worker                 "Assign names to anonymous instructions", false, false)
57*9880d681SAndroid Build Coastguard Worker char &llvm::InstructionNamerID = InstNamer::ID;
58*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
59*9880d681SAndroid Build Coastguard Worker //
60*9880d681SAndroid Build Coastguard Worker // InstructionNamer - Give any unnamed non-void instructions "tmp" names.
61*9880d681SAndroid Build Coastguard Worker //
createInstructionNamerPass()62*9880d681SAndroid Build Coastguard Worker FunctionPass *llvm::createInstructionNamerPass() {
63*9880d681SAndroid Build Coastguard Worker   return new InstNamer();
64*9880d681SAndroid Build Coastguard Worker }
65