InstructionNamer.cpp 2.3 KB

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