1
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
17#ifndef LLVM_LIB_CODEGEN_MIRVREGNAMERUTILS_H
18#define LLVM_LIB_CODEGEN_MIRVREGNAMERUTILS_H
19
20#include "llvm/CodeGen/Register.h"
21#include <map>
22#include <vector>
23#include <string>
24
25namespace llvm {
26
27struct CodeGenOptions;
28class MachineBasicBlock;
29class MachineInstr;
30class MachineRegisterInfo;
31class StringRef;
32
33/// VRegRenamer - This class is used for renaming vregs in a machine basic
34/// block according to semantics of the instruction.
35class VRegRenamer {
36 class NamedVReg {
37 Register Reg;
38 std::string Name;
39
40 public:
41 NamedVReg(Register Reg, std::string Name = "") : Reg(Reg), Name(Name) {}
42 NamedVReg(std::string Name = "") : Reg(~0U), Name(Name) {}
43
44 const std::string &getName() const { return Name; }
45
46 Register getReg() const { return Reg; }
47 };
48
49 const CodeGenOptions &Opts;
50 MachineRegisterInfo &MRI;
51
52 unsigned CurrentBBNumber = 0;
53
54 /// Given an Instruction, construct a hash of the operands
55 /// of the instructions along with the opcode.
56 /// When dealing with virtual registers, just hash the opcode of
57 /// the instruction defining that vreg.
58 /// Handle immediates, registers (physical and virtual) explicitly,
59 /// and return a common value for the other cases.
60 /// Instruction will be named in the following scheme
61 /// bb<block_no>_hash_<collission_count>.
62 std::string getInstructionOpcodeHash(MachineInstr &MI);
63
64 /// For all the VRegs that are candidates for renaming,
65 /// return a mapping from old vregs to new vregs with names.
66 std::map<Register, Register>
67 getVRegRenameMap(const std::vector<NamedVReg> &VRegs);
68
69 /// Perform replacing of registers based on the <old,new> vreg map.
70 bool doVRegRenaming(const std::map<Register, Register> &VRegRenameMap);
71
72 /// createVirtualRegister - Given an existing vreg, create a named vreg to
73 /// take its place. The name is determined by calling
74 /// getInstructionOpcodeHash.
75 Register createVirtualRegister(Register VReg);
76
77 /// Create a vreg with name and return it.
78 Register createVirtualRegisterWithLowerName(Register VReg, StringRef Name);
79
80 /// Linearly traverse the MachineBasicBlock and rename each instruction's
81 /// vreg definition based on the semantics of the instruction.
82 /// Names are as follows bb<BBNum>_hash_[0-9]+
83 bool renameInstsInMBB(MachineBasicBlock *MBB);
84
85public:
86 VRegRenamer() = delete;
87 VRegRenamer(const CodeGenOptions &Opts, MachineRegisterInfo &MRI)
88 : Opts(Opts), MRI(MRI) {}
89
90 /// Same as the above, but sets a BBNum depending on BB traversal that
91 /// will be used as prefix for the vreg names.
92 bool renameVRegs(MachineBasicBlock *MBB, unsigned BBNum) {
93 CurrentBBNumber = BBNum;
94 return renameInstsInMBB(MBB);
95 }
96};
97
98} // namespace llvm
99
100#endif
101