Chris Lattner | 20abc41 | 2008-08-23 06:07:02 +0000 | [diff] [blame] | 1 | //===- InstructionNamer.cpp - Give anonymous instructions names -----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This is a little utility pass that gives instructions names, this is mostly |
| 11 | // useful when diffing the effect of an optimization because deleting an |
| 12 | // unnamed instruction can change all other instruction numbering, making the |
| 13 | // diff very noisy. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 17 | #include "llvm/IR/Function.h" |
| 18 | #include "llvm/IR/Type.h" |
Chris Lattner | 20abc41 | 2008-08-23 06:07:02 +0000 | [diff] [blame] | 19 | #include "llvm/Pass.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 20 | #include "llvm/Transforms/Scalar.h" |
Chris Lattner | 20abc41 | 2008-08-23 06:07:02 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | |
| 23 | namespace { |
| 24 | struct InstNamer : public FunctionPass { |
| 25 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 26 | InstNamer() : FunctionPass(ID) { |
| 27 | initializeInstNamerPass(*PassRegistry::getPassRegistry()); |
| 28 | } |
Sanjoy Das | 7be961b | 2017-05-08 23:18:36 +0000 | [diff] [blame] | 29 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 30 | void getAnalysisUsage(AnalysisUsage &Info) const override { |
Devang Patel | 5a5ab73 | 2008-11-06 01:00:16 +0000 | [diff] [blame] | 31 | Info.setPreservesAll(); |
| 32 | } |
| 33 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 34 | bool runOnFunction(Function &F) override { |
Sanjoy Das | 76bbdd1 | 2017-05-08 23:18:43 +0000 | [diff] [blame] | 35 | for (auto &Arg : F.args()) |
| 36 | if (!Arg.hasName()) |
| 37 | Arg.setName("arg"); |
Devang Patel | 38642e5 | 2008-11-20 19:50:17 +0000 | [diff] [blame] | 38 | |
Benjamin Kramer | 135f735 | 2016-06-26 12:28:59 +0000 | [diff] [blame] | 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"); |
Chris Lattner | c1c6404 | 2008-12-18 00:33:11 +0000 | [diff] [blame] | 46 | } |
Chris Lattner | 20abc41 | 2008-08-23 06:07:02 +0000 | [diff] [blame] | 47 | return true; |
| 48 | } |
| 49 | }; |
Sanjoy Das | 7be961b | 2017-05-08 23:18:36 +0000 | [diff] [blame] | 50 | |
Chris Lattner | 20abc41 | 2008-08-23 06:07:02 +0000 | [diff] [blame] | 51 | char InstNamer::ID = 0; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 52 | } |
Chris Lattner | 20abc41 | 2008-08-23 06:07:02 +0000 | [diff] [blame] | 53 | |
Sanjoy Das | 7be961b | 2017-05-08 23:18:36 +0000 | [diff] [blame] | 54 | INITIALIZE_PASS(InstNamer, "instnamer", |
Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 55 | "Assign names to anonymous instructions", false, false) |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 56 | char &llvm::InstructionNamerID = InstNamer::ID; |
Chris Lattner | 20abc41 | 2008-08-23 06:07:02 +0000 | [diff] [blame] | 57 | //===----------------------------------------------------------------------===// |
| 58 | // |
| 59 | // InstructionNamer - Give any unnamed non-void instructions "tmp" names. |
| 60 | // |
| 61 | FunctionPass *llvm::createInstructionNamerPass() { |
| 62 | return new InstNamer(); |
| 63 | } |