MIRNamerPass.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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/ADT/STLExtras.h"
  21. #include "llvm/CodeGen/MachineFunctionPass.h"
  22. #include "llvm/CodeGen/MachineInstrBuilder.h"
  23. #include "llvm/CodeGen/MachineRegisterInfo.h"
  24. #include "llvm/CodeGen/Passes.h"
  25. #include "llvm/InitializePasses.h"
  26. using namespace llvm;
  27. namespace llvm {
  28. extern char &MIRNamerID;
  29. } // namespace llvm
  30. #define DEBUG_TYPE "mir-namer"
  31. namespace {
  32. class MIRNamer : public MachineFunctionPass {
  33. public:
  34. static char ID;
  35. MIRNamer() : MachineFunctionPass(ID) {}
  36. StringRef getPassName() const override {
  37. return "Rename virtual register operands";
  38. }
  39. void getAnalysisUsage(AnalysisUsage &AU) const override {
  40. AU.setPreservesCFG();
  41. MachineFunctionPass::getAnalysisUsage(AU);
  42. }
  43. bool runOnMachineFunction(MachineFunction &MF) override {
  44. bool Changed = false;
  45. if (MF.empty())
  46. return Changed;
  47. VRegRenamer Renamer(MF.getRegInfo());
  48. unsigned BBIndex = 0;
  49. ReversePostOrderTraversal<MachineBasicBlock *> RPOT(&*MF.begin());
  50. for (auto &MBB : RPOT)
  51. Changed |= Renamer.renameVRegs(MBB, BBIndex++);
  52. return Changed;
  53. }
  54. };
  55. } // end anonymous namespace
  56. char MIRNamer::ID;
  57. char &llvm::MIRNamerID = MIRNamer::ID;
  58. INITIALIZE_PASS_BEGIN(MIRNamer, "mir-namer", "Rename Register Operands", false,
  59. false)
  60. INITIALIZE_PASS_END(MIRNamer, "mir-namer", "Rename Register Operands", false,
  61. false)