1dc97ca9fSPuyan Lotfi //===----------------------- MIRNamer.cpp - MIR Namer ---------------------===// 2dc97ca9fSPuyan Lotfi // 3dc97ca9fSPuyan Lotfi // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4dc97ca9fSPuyan Lotfi // See https://llvm.org/LICENSE.txt for license information. 5dc97ca9fSPuyan Lotfi // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6dc97ca9fSPuyan Lotfi // 7dc97ca9fSPuyan Lotfi //===----------------------------------------------------------------------===// 8dc97ca9fSPuyan Lotfi // 9dc97ca9fSPuyan Lotfi // The purpose of this pass is to rename virtual register operands with the goal 10dc97ca9fSPuyan Lotfi // of making it easier to author easier to read tests for MIR. This pass reuses 11dc97ca9fSPuyan Lotfi // the vreg renamer used by MIRCanonicalizerPass. 12dc97ca9fSPuyan Lotfi // 13dc97ca9fSPuyan Lotfi // Basic Usage: 14dc97ca9fSPuyan Lotfi // 15dc97ca9fSPuyan Lotfi // llc -o - -run-pass mir-namer example.mir 16dc97ca9fSPuyan Lotfi // 17dc97ca9fSPuyan Lotfi //===----------------------------------------------------------------------===// 18dc97ca9fSPuyan Lotfi 19dc97ca9fSPuyan Lotfi #include "MIRVRegNamerUtils.h" 20dc97ca9fSPuyan Lotfi #include "llvm/ADT/PostOrderIterator.h" 21dc97ca9fSPuyan Lotfi #include "llvm/CodeGen/MachineFunctionPass.h" 2205da2fe5SReid Kleckner #include "llvm/InitializePasses.h" 23dc97ca9fSPuyan Lotfi 24dc97ca9fSPuyan Lotfi using namespace llvm; 25dc97ca9fSPuyan Lotfi 26dc97ca9fSPuyan Lotfi namespace llvm { 27dc97ca9fSPuyan Lotfi extern char &MIRNamerID; 28dc97ca9fSPuyan Lotfi } // namespace llvm 29dc97ca9fSPuyan Lotfi 30dc97ca9fSPuyan Lotfi #define DEBUG_TYPE "mir-namer" 31dc97ca9fSPuyan Lotfi 32dc97ca9fSPuyan Lotfi namespace { 33dc97ca9fSPuyan Lotfi 34dc97ca9fSPuyan Lotfi class MIRNamer : public MachineFunctionPass { 35dc97ca9fSPuyan Lotfi public: 36dc97ca9fSPuyan Lotfi static char ID; MIRNamer()37dc97ca9fSPuyan Lotfi MIRNamer() : MachineFunctionPass(ID) {} 38dc97ca9fSPuyan Lotfi getPassName() const39dc97ca9fSPuyan Lotfi StringRef getPassName() const override { 40dc97ca9fSPuyan Lotfi return "Rename virtual register operands"; 41dc97ca9fSPuyan Lotfi } 42dc97ca9fSPuyan Lotfi getAnalysisUsage(AnalysisUsage & AU) const43dc97ca9fSPuyan Lotfi void getAnalysisUsage(AnalysisUsage &AU) const override { 44dc97ca9fSPuyan Lotfi AU.setPreservesCFG(); 45dc97ca9fSPuyan Lotfi MachineFunctionPass::getAnalysisUsage(AU); 46dc97ca9fSPuyan Lotfi } 47dc97ca9fSPuyan Lotfi runOnMachineFunction(MachineFunction & MF)48dc97ca9fSPuyan Lotfi bool runOnMachineFunction(MachineFunction &MF) override { 49dc97ca9fSPuyan Lotfi bool Changed = false; 50dc97ca9fSPuyan Lotfi 51dc97ca9fSPuyan Lotfi if (MF.empty()) 52dc97ca9fSPuyan Lotfi return Changed; 53dc97ca9fSPuyan Lotfi 5472768685SAditya Nandakumar VRegRenamer Renamer(MF.getRegInfo()); 55dc97ca9fSPuyan Lotfi 56*fdc6f4b9SPuyan Lotfi unsigned BBIndex = 0; 57dc97ca9fSPuyan Lotfi ReversePostOrderTraversal<MachineBasicBlock *> RPOT(&*MF.begin()); 58dc97ca9fSPuyan Lotfi for (auto &MBB : RPOT) 59*fdc6f4b9SPuyan Lotfi Changed |= Renamer.renameVRegs(MBB, BBIndex++); 60dc97ca9fSPuyan Lotfi 61dc97ca9fSPuyan Lotfi return Changed; 62dc97ca9fSPuyan Lotfi } 63dc97ca9fSPuyan Lotfi }; 64dc97ca9fSPuyan Lotfi 65dc97ca9fSPuyan Lotfi } // end anonymous namespace 66dc97ca9fSPuyan Lotfi 67dc97ca9fSPuyan Lotfi char MIRNamer::ID; 68dc97ca9fSPuyan Lotfi 69dc97ca9fSPuyan Lotfi char &llvm::MIRNamerID = MIRNamer::ID; 70dc97ca9fSPuyan Lotfi 71dc97ca9fSPuyan Lotfi INITIALIZE_PASS_BEGIN(MIRNamer, "mir-namer", "Rename Register Operands", false, 72dc97ca9fSPuyan Lotfi false) 73dc97ca9fSPuyan Lotfi 74dc97ca9fSPuyan Lotfi INITIALIZE_PASS_END(MIRNamer, "mir-namer", "Rename Register Operands", false, 75dc97ca9fSPuyan Lotfi false) 76