blob: 8e76bfa2bbd44a3f3579924726411464fb6cb259 [file] [log] [blame]
Benjamin Kramerdf4b9a32019-09-17 12:56:29 +00001
Puyan Lotfi028061d2019-09-04 21:29:10 +00002//===------------ 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 Kramerdf4b9a32019-09-17 12:56:29 +000017#ifndef LLVM_LIB_CODEGEN_MIRVREGNAMERUTILS_H
18#define LLVM_LIB_CODEGEN_MIRVREGNAMERUTILS_H
19
Puyan Lotfi028061d2019-09-04 21:29:10 +000020#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
Puyan Lotfi028061d2019-09-04 21:29:10 +000028
Benjamin Kramerdf4b9a32019-09-17 12:56:29 +000029namespace llvm {
Aditya Nandakumar72768682019-11-15 08:23:32 -080030/// VRegRenamer - This class is used for renaming vregs in a machine basic
31/// block according to semantics of the instruction.
32class VRegRenamer {
33 class NamedVReg {
34 Register Reg;
35 std::string Name;
Puyan Lotfi028061d2019-09-04 21:29:10 +000036
Aditya Nandakumar72768682019-11-15 08:23:32 -080037 public:
38 NamedVReg(Register Reg, std::string Name = "") : Reg(Reg), Name(Name) {}
39 NamedVReg(std::string Name = "") : Reg(~0U), Name(Name) {}
40
41 const std::string &getName() const { return Name; }
42
43 Register getReg() const { return Reg; }
44 };
45
Puyan Lotfi028061d2019-09-04 21:29:10 +000046 MachineRegisterInfo &MRI;
47
Aditya Nandakumar72768682019-11-15 08:23:32 -080048 unsigned CurrentBBNumber = 0;
Puyan Lotfi028061d2019-09-04 21:29:10 +000049
Aditya Nandakumar72768682019-11-15 08:23:32 -080050 /// Given an Instruction, construct a hash of the operands
51 /// of the instructions along with the opcode.
52 /// When dealing with virtual registers, just hash the opcode of
53 /// the instruction defining that vreg.
54 /// Handle immediates, registers (physical and virtual) explicitly,
55 /// and return a common value for the other cases.
56 /// Instruction will be named in the following scheme
57 /// bb<block_no>_hash_<collission_count>.
58 std::string getInstructionOpcodeHash(MachineInstr &MI);
Puyan Lotfi028061d2019-09-04 21:29:10 +000059
Aditya Nandakumar72768682019-11-15 08:23:32 -080060 /// For all the VRegs that are candidates for renaming,
61 /// return a mapping from old vregs to new vregs with names.
62 std::map<unsigned, unsigned>
63 getVRegRenameMap(const std::vector<NamedVReg> &VRegs);
64
65 /// Perform replacing of registers based on the <old,new> vreg map.
66 bool doVRegRenaming(const std::map<unsigned, unsigned> &VRegRenameMap);
Puyan Lotfi028061d2019-09-04 21:29:10 +000067
68public:
Aditya Nandakumar72768682019-11-15 08:23:32 -080069 VRegRenamer() = delete;
70 VRegRenamer(MachineRegisterInfo &MRI) : MRI(MRI) {}
Puyan Lotfi028061d2019-09-04 21:29:10 +000071
72 /// createVirtualRegister - Given an existing vreg, create a named vreg to
Aditya Nandakumar72768682019-11-15 08:23:32 -080073 /// take its place. The name is determined by calling
74 /// getInstructionOpcodeHash.
Puyan Lotfi028061d2019-09-04 21:29:10 +000075 unsigned createVirtualRegister(unsigned VReg);
76
Aditya Nandakumar72768682019-11-15 08:23:32 -080077 /// Create a vreg with name and return it.
78 unsigned createVirtualRegisterWithName(unsigned VReg,
79 const std::string &Name);
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
85 /// Same as the above, but sets a BBNum depending on BB traversal that
86 /// will be used as prefix for the vreg names.
Puyan Lotfifdc6f4b2019-11-30 19:00:10 -050087 bool renameVRegs(MachineBasicBlock *MBB, unsigned BBNum);
Aditya Nandakumar72768682019-11-15 08:23:32 -080088
89 unsigned getCurrentBBNumber() const { return CurrentBBNumber; }
Puyan Lotfi028061d2019-09-04 21:29:10 +000090};
Benjamin Kramerdf4b9a32019-09-17 12:56:29 +000091
92} // namespace llvm
93
94#endif