MIRNamerPass.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //===----------------------- MIRNamer.cpp - MIR Namer ---------------------===//
  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. // The purpose of this pass is to rename virtual register operands with the goal
  10. // of making it easier to author easier to read tests for MIR. This pass reuses
  11. // the vreg renamer used by MIRCanonicalizerPass.
  12. //
  13. // Basic Usage:
  14. //
  15. // llc -o - -run-pass mir-namer example.mir
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #include "MIRVRegNamerUtils.h"
  19. #include "llvm/ADT/PostOrderIterator.h"
  20. #include "llvm/CodeGen/MachineFunctionPass.h"
  21. #include "llvm/InitializePasses.h"
  22. using namespace llvm;
  23. namespace llvm {
  24. extern char &MIRNamerID;
  25. } // namespace llvm
  26. #define DEBUG_TYPE "mir-namer"
  27. namespace {
  28. class MIRNamer : public MachineFunctionPass {
  29. public:
  30. static char ID;
  31. MIRNamer() : MachineFunctionPass(ID) {}
  32. StringRef getPassName() const override {
  33. return "Rename virtual register operands";
  34. }
  35. void getAnalysisUsage(AnalysisUsage &AU) const override {
  36. AU.setPreservesCFG();
  37. MachineFunctionPass::getAnalysisUsage(AU);
  38. }
  39. bool runOnMachineFunction(MachineFunction &MF) override {
  40. bool Changed = false;
  41. if (MF.empty())
  42. return Changed;
  43. VRegRenamer Renamer(MF.getRegInfo());
  44. unsigned BBIndex = 0;
  45. ReversePostOrderTraversal<MachineBasicBlock *> RPOT(&*MF.begin());
  46. for (auto &MBB : RPOT)
  47. Changed |= Renamer.renameVRegs(MBB, BBIndex++);
  48. return Changed;
  49. }
  50. };
  51. } // end anonymous namespace
  52. char MIRNamer::ID;
  53. char &llvm::MIRNamerID = MIRNamer::ID;
  54. INITIALIZE_PASS_BEGIN(MIRNamer, "mir-namer", "Rename Register Operands", false,
  55. false)
  56. INITIALIZE_PASS_END(MIRNamer, "mir-namer", "Rename Register Operands", false,
  57. false)