| Benjamin Kramer | df4b9a3 | 2019-09-17 12:56:29 +0000 | [diff] [blame] | 1 | |
| Puyan Lotfi | 028061d | 2019-09-04 21:29:10 +0000 | [diff] [blame] | 2 | //===------------ MIRVRegNamerUtils.h - MIR VReg Renaming Utilities -------===// |
| 3 | // |
| 4 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | // See https://llvm.org/LICENSE.txt for license information. |
| 6 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // The purpose of these utilities is to abstract out parts of the MIRCanon pass |
| 11 | // that are responsible for renaming virtual registers with the purpose of |
| 12 | // sharing code with a MIRVRegNamer pass that could be the analog of the |
| 13 | // opt -instnamer pass. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
| Benjamin Kramer | df4b9a3 | 2019-09-17 12:56:29 +0000 | [diff] [blame] | 17 | #ifndef LLVM_LIB_CODEGEN_MIRVREGNAMERUTILS_H |
| 18 | #define LLVM_LIB_CODEGEN_MIRVREGNAMERUTILS_H |
| 19 | |
| Puyan Lotfi | 028061d | 2019-09-04 21:29:10 +0000 | [diff] [blame] | 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 | #include "llvm/Support/raw_ostream.h" |
| 27 | |
| Benjamin Kramer | df4b9a3 | 2019-09-17 12:56:29 +0000 | [diff] [blame] | 28 | namespace llvm { |
| Aditya Nandakumar | 7276868 | 2019-11-15 08:23:32 -0800 | [diff] [blame] | 29 | /// VRegRenamer - This class is used for renaming vregs in a machine basic |
| 30 | /// block according to semantics of the instruction. |
| 31 | class VRegRenamer { |
| 32 | class NamedVReg { |
| 33 | Register Reg; |
| 34 | std::string Name; |
| Puyan Lotfi | 028061d | 2019-09-04 21:29:10 +0000 | [diff] [blame] | 35 | |
| Aditya Nandakumar | 7276868 | 2019-11-15 08:23:32 -0800 | [diff] [blame] | 36 | public: |
| 37 | NamedVReg(Register Reg, std::string Name = "") : Reg(Reg), Name(Name) {} |
| 38 | NamedVReg(std::string Name = "") : Reg(~0U), Name(Name) {} |
| 39 | |
| 40 | const std::string &getName() const { return Name; } |
| 41 | |
| 42 | Register getReg() const { return Reg; } |
| 43 | }; |
| 44 | |
| Puyan Lotfi | 028061d | 2019-09-04 21:29:10 +0000 | [diff] [blame] | 45 | MachineRegisterInfo &MRI; |
| 46 | |
| Aditya Nandakumar | 7276868 | 2019-11-15 08:23:32 -0800 | [diff] [blame] | 47 | unsigned CurrentBBNumber = 0; |
| Puyan Lotfi | 028061d | 2019-09-04 21:29:10 +0000 | [diff] [blame] | 48 | |
| Aditya Nandakumar | 7276868 | 2019-11-15 08:23:32 -0800 | [diff] [blame] | 49 | /// Given an Instruction, construct a hash of the operands |
| 50 | /// of the instructions along with the opcode. |
| 51 | /// When dealing with virtual registers, just hash the opcode of |
| 52 | /// the instruction defining that vreg. |
| 53 | /// Handle immediates, registers (physical and virtual) explicitly, |
| 54 | /// and return a common value for the other cases. |
| 55 | /// Instruction will be named in the following scheme |
| 56 | /// bb<block_no>_hash_<collission_count>. |
| 57 | std::string getInstructionOpcodeHash(MachineInstr &MI); |
| Puyan Lotfi | 028061d | 2019-09-04 21:29:10 +0000 | [diff] [blame] | 58 | |
| Aditya Nandakumar | 7276868 | 2019-11-15 08:23:32 -0800 | [diff] [blame] | 59 | /// For all the VRegs that are candidates for renaming, |
| 60 | /// return a mapping from old vregs to new vregs with names. |
| 61 | std::map<unsigned, unsigned> |
| 62 | getVRegRenameMap(const std::vector<NamedVReg> &VRegs); |
| 63 | |
| 64 | /// Perform replacing of registers based on the <old,new> vreg map. |
| 65 | bool doVRegRenaming(const std::map<unsigned, unsigned> &VRegRenameMap); |
| Puyan Lotfi | 028061d | 2019-09-04 21:29:10 +0000 | [diff] [blame] | 66 | |
| Puyan Lotfi | 028061d | 2019-09-04 21:29:10 +0000 | [diff] [blame] | 67 | /// createVirtualRegister - Given an existing vreg, create a named vreg to |
| Aditya Nandakumar | 7276868 | 2019-11-15 08:23:32 -0800 | [diff] [blame] | 68 | /// take its place. The name is determined by calling |
| 69 | /// getInstructionOpcodeHash. |
| Puyan Lotfi | 028061d | 2019-09-04 21:29:10 +0000 | [diff] [blame] | 70 | unsigned createVirtualRegister(unsigned VReg); |
| 71 | |
| Aditya Nandakumar | 7276868 | 2019-11-15 08:23:32 -0800 | [diff] [blame] | 72 | /// Create a vreg with name and return it. |
| Puyan Lotfi | 479e3b8 | 2019-12-09 14:54:09 -0500 | [diff] [blame] | 73 | unsigned createVirtualRegisterWithLowerName(unsigned VReg, StringRef Name); |
| Aditya Nandakumar | 7276868 | 2019-11-15 08:23:32 -0800 | [diff] [blame] | 74 | /// Linearly traverse the MachineBasicBlock and rename each instruction's |
| 75 | /// vreg definition based on the semantics of the instruction. |
| 76 | /// Names are as follows bb<BBNum>_hash_[0-9]+ |
| 77 | bool renameInstsInMBB(MachineBasicBlock *MBB); |
| 78 | |
| Puyan Lotfi | 756db63 | 2019-12-12 03:27:47 -0500 | [diff] [blame] | 79 | public: |
| 80 | VRegRenamer() = delete; |
| 81 | VRegRenamer(MachineRegisterInfo &MRI) : MRI(MRI) {} |
| 82 | |
| Aditya Nandakumar | 7276868 | 2019-11-15 08:23:32 -0800 | [diff] [blame] | 83 | /// Same as the above, but sets a BBNum depending on BB traversal that |
| 84 | /// will be used as prefix for the vreg names. |
| Puyan Lotfi | 756db63 | 2019-12-12 03:27:47 -0500 | [diff] [blame] | 85 | bool renameVRegs(MachineBasicBlock *MBB, unsigned BBNum) { |
| 86 | CurrentBBNumber = BBNum; |
| 87 | return renameInstsInMBB(MBB); |
| 88 | } |
| Puyan Lotfi | 028061d | 2019-09-04 21:29:10 +0000 | [diff] [blame] | 89 | }; |
| Benjamin Kramer | df4b9a3 | 2019-09-17 12:56:29 +0000 | [diff] [blame] | 90 | |
| 91 | } // namespace llvm |
| 92 | |
| 93 | #endif |