xref: /freebsd/contrib/llvm-project/llvm/lib/CodeGen/MIRNamerPass.cpp (revision a7dea1671b87c07d2d266f836bfa8b58efc7c134)
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 
19 #include "MIRVRegNamerUtils.h"
20 #include "llvm/ADT/PostOrderIterator.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/CodeGen/MachineFunctionPass.h"
23 #include "llvm/CodeGen/MachineInstrBuilder.h"
24 #include "llvm/CodeGen/MachineRegisterInfo.h"
25 #include "llvm/CodeGen/Passes.h"
26 
27 using namespace llvm;
28 
29 namespace llvm {
30 extern char &MIRNamerID;
31 } // namespace llvm
32 
33 #define DEBUG_TYPE "mir-namer"
34 
35 namespace {
36 
37 class MIRNamer : public MachineFunctionPass {
38 public:
39   static char ID;
40   MIRNamer() : MachineFunctionPass(ID) {}
41 
42   StringRef getPassName() const override {
43     return "Rename virtual register operands";
44   }
45 
46   void getAnalysisUsage(AnalysisUsage &AU) const override {
47     AU.setPreservesCFG();
48     MachineFunctionPass::getAnalysisUsage(AU);
49   }
50 
51   bool runOnMachineFunction(MachineFunction &MF) override {
52     bool Changed = false;
53 
54     if (MF.empty())
55       return Changed;
56 
57     NamedVRegCursor NVC(MF.getRegInfo());
58 
59     ReversePostOrderTraversal<MachineBasicBlock *> RPOT(&*MF.begin());
60     for (auto &MBB : RPOT)
61       Changed |= NVC.renameVRegs(MBB);
62 
63     return Changed;
64   }
65 };
66 
67 } // end anonymous namespace
68 
69 char MIRNamer::ID;
70 
71 char &llvm::MIRNamerID = MIRNamer::ID;
72 
73 INITIALIZE_PASS_BEGIN(MIRNamer, "mir-namer", "Rename Register Operands", false,
74                       false)
75 
76 INITIALIZE_PASS_END(MIRNamer, "mir-namer", "Rename Register Operands", false,
77                     false)
78