Chris Lattner | 20abc41 | 2008-08-23 06:07:02 +0000 | [diff] [blame] | 1 | //===- InstructionNamer.cpp - Give anonymous instructions names -----------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Chris Lattner | 20abc41 | 2008-08-23 06:07:02 +0000 | [diff] [blame] | 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 | |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 16 | #include "llvm/IR/Function.h" |
| 17 | #include "llvm/IR/Type.h" |
Chris Lattner | 20abc41 | 2008-08-23 06:07:02 +0000 | [diff] [blame] | 18 | #include "llvm/Pass.h" |
David Blaikie | a373d18 | 2018-03-28 17:44:36 +0000 | [diff] [blame] | 19 | #include "llvm/Transforms/Utils.h" |
Chris Lattner | 20abc41 | 2008-08-23 06:07:02 +0000 | [diff] [blame] | 20 | using namespace llvm; |
| 21 | |
| 22 | namespace { |
| 23 | struct InstNamer : public FunctionPass { |
| 24 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 25 | InstNamer() : FunctionPass(ID) { |
| 26 | initializeInstNamerPass(*PassRegistry::getPassRegistry()); |
| 27 | } |
Sanjoy Das | 7be961b | 2017-05-08 23:18:36 +0000 | [diff] [blame] | 28 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 29 | void getAnalysisUsage(AnalysisUsage &Info) const override { |
Devang Patel | 5a5ab73 | 2008-11-06 01:00:16 +0000 | [diff] [blame] | 30 | Info.setPreservesAll(); |
| 31 | } |
| 32 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 33 | bool runOnFunction(Function &F) override { |
Sanjoy Das | 76bbdd1 | 2017-05-08 23:18:43 +0000 | [diff] [blame] | 34 | for (auto &Arg : F.args()) |
| 35 | if (!Arg.hasName()) |
| 36 | Arg.setName("arg"); |
Devang Patel | 38642e5 | 2008-11-20 19:50:17 +0000 | [diff] [blame] | 37 | |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 38 | 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 Lattner | c1c6404 | 2008-12-18 00:33:11 +0000 | [diff] [blame] | 45 | } |
Chris Lattner | 20abc41 | 2008-08-23 06:07:02 +0000 | [diff] [blame] | 46 | return true; |
| 47 | } |
| 48 | }; |
Sanjoy Das | 7be961b | 2017-05-08 23:18:36 +0000 | [diff] [blame] | 49 | |
Chris Lattner | 20abc41 | 2008-08-23 06:07:02 +0000 | [diff] [blame] | 50 | char InstNamer::ID = 0; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 51 | } |
Chris Lattner | 20abc41 | 2008-08-23 06:07:02 +0000 | [diff] [blame] | 52 | |
Sanjoy Das | 7be961b | 2017-05-08 23:18:36 +0000 | [diff] [blame] | 53 | INITIALIZE_PASS(InstNamer, "instnamer", |
Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 54 | "Assign names to anonymous instructions", false, false) |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 55 | char &llvm::InstructionNamerID = InstNamer::ID; |
Chris Lattner | 20abc41 | 2008-08-23 06:07:02 +0000 | [diff] [blame] | 56 | //===----------------------------------------------------------------------===// |
| 57 | // |
| 58 | // InstructionNamer - Give any unnamed non-void instructions "tmp" names. |
| 59 | // |
| 60 | FunctionPass *llvm::createInstructionNamerPass() { |
| 61 | return new InstNamer(); |
| 62 | } |