blob: 6c4fc1ceb991351e3c66cf37ef50637bccefb66b [file] [log] [blame]
Chris Lattner20abc412008-08-23 06:07:02 +00001//===- InstructionNamer.cpp - Give anonymous instructions names -----------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Chris Lattner20abc412008-08-23 06:07:02 +00006//
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
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#include "llvm/IR/Function.h"
17#include "llvm/IR/Type.h"
Chris Lattner20abc412008-08-23 06:07:02 +000018#include "llvm/Pass.h"
David Blaikiea373d182018-03-28 17:44:36 +000019#include "llvm/Transforms/Utils.h"
Chris Lattner20abc412008-08-23 06:07:02 +000020using namespace llvm;
21
22namespace {
23 struct InstNamer : public FunctionPass {
24 static char ID; // Pass identification, replacement for typeid
Owen Anderson6c18d1a2010-10-19 17:21:58 +000025 InstNamer() : FunctionPass(ID) {
26 initializeInstNamerPass(*PassRegistry::getPassRegistry());
27 }
Sanjoy Das7be961b2017-05-08 23:18:36 +000028
Craig Topper3e4c6972014-03-05 09:10:37 +000029 void getAnalysisUsage(AnalysisUsage &Info) const override {
Devang Patel5a5ab732008-11-06 01:00:16 +000030 Info.setPreservesAll();
31 }
32
Craig Topper3e4c6972014-03-05 09:10:37 +000033 bool runOnFunction(Function &F) override {
Sanjoy Das76bbdd12017-05-08 23:18:43 +000034 for (auto &Arg : F.args())
35 if (!Arg.hasName())
36 Arg.setName("arg");
Devang Patel38642e52008-11-20 19:50:17 +000037
Benjamin Kramer135f7352016-06-26 12:28:59 +000038 for (BasicBlock &BB : F) {
39 if (!BB.hasName())
40 BB.setName("bb");
41
42 for (Instruction &I : BB)
43 if (!I.hasName() && !I.getType()->isVoidTy())
44 I.setName("tmp");
Chris Lattnerc1c64042008-12-18 00:33:11 +000045 }
Chris Lattner20abc412008-08-23 06:07:02 +000046 return true;
47 }
48 };
Sanjoy Das7be961b2017-05-08 23:18:36 +000049
Chris Lattner20abc412008-08-23 06:07:02 +000050 char InstNamer::ID = 0;
Alexander Kornienkof00654e2015-06-23 09:49:53 +000051}
Chris Lattner20abc412008-08-23 06:07:02 +000052
Sanjoy Das7be961b2017-05-08 23:18:36 +000053INITIALIZE_PASS(InstNamer, "instnamer",
Owen Andersondf7a4f22010-10-07 22:25:06 +000054 "Assign names to anonymous instructions", false, false)
Owen Andersona7aed182010-08-06 18:33:48 +000055char &llvm::InstructionNamerID = InstNamer::ID;
Chris Lattner20abc412008-08-23 06:07:02 +000056//===----------------------------------------------------------------------===//
57//
58// InstructionNamer - Give any unnamed non-void instructions "tmp" names.
59//
60FunctionPass *llvm::createInstructionNamerPass() {
61 return new InstNamer();
62}