xref: /openbsd-src/gnu/llvm/llvm/lib/CodeGen/MIRVRegNamerUtils.h (revision 097a140d792de8b2bbe59ad827d39eabf9b4280a)
109467b48Spatrick 
209467b48Spatrick //===------------ MIRVRegNamerUtils.h - MIR VReg Renaming Utilities -------===//
309467b48Spatrick //
409467b48Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
509467b48Spatrick // See https://llvm.org/LICENSE.txt for license information.
609467b48Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
709467b48Spatrick //
809467b48Spatrick //===----------------------------------------------------------------------===//
909467b48Spatrick //
1009467b48Spatrick // The purpose of these utilities is to abstract out parts of the MIRCanon pass
1109467b48Spatrick // that are responsible for renaming virtual registers with the purpose of
1209467b48Spatrick // sharing code with a MIRVRegNamer pass that could be the analog of the
1309467b48Spatrick // opt -instnamer pass.
1409467b48Spatrick //
1509467b48Spatrick //===----------------------------------------------------------------------===//
1609467b48Spatrick 
1709467b48Spatrick #ifndef LLVM_LIB_CODEGEN_MIRVREGNAMERUTILS_H
1809467b48Spatrick #define LLVM_LIB_CODEGEN_MIRVREGNAMERUTILS_H
1909467b48Spatrick 
20*097a140dSpatrick #include "llvm/CodeGen/Register.h"
21*097a140dSpatrick #include <map>
22*097a140dSpatrick #include <vector>
23*097a140dSpatrick #include <string>
2409467b48Spatrick 
2509467b48Spatrick namespace llvm {
26*097a140dSpatrick 
27*097a140dSpatrick class MachineBasicBlock;
28*097a140dSpatrick class MachineInstr;
29*097a140dSpatrick class MachineRegisterInfo;
30*097a140dSpatrick class StringRef;
31*097a140dSpatrick 
3209467b48Spatrick /// VRegRenamer - This class is used for renaming vregs in a machine basic
3309467b48Spatrick /// block according to semantics of the instruction.
3409467b48Spatrick class VRegRenamer {
3509467b48Spatrick   class NamedVReg {
3609467b48Spatrick     Register Reg;
3709467b48Spatrick     std::string Name;
3809467b48Spatrick 
3909467b48Spatrick   public:
Reg(Reg)4009467b48Spatrick     NamedVReg(Register Reg, std::string Name = "") : Reg(Reg), Name(Name) {}
4109467b48Spatrick     NamedVReg(std::string Name = "") : Reg(~0U), Name(Name) {}
4209467b48Spatrick 
getName()4309467b48Spatrick     const std::string &getName() const { return Name; }
4409467b48Spatrick 
getReg()4509467b48Spatrick     Register getReg() const { return Reg; }
4609467b48Spatrick   };
4709467b48Spatrick 
4809467b48Spatrick   MachineRegisterInfo &MRI;
4909467b48Spatrick 
5009467b48Spatrick   unsigned CurrentBBNumber = 0;
5109467b48Spatrick 
5209467b48Spatrick   /// Given an Instruction, construct a hash of the operands
5309467b48Spatrick   /// of the instructions along with the opcode.
5409467b48Spatrick   /// When dealing with virtual registers, just hash the opcode of
5509467b48Spatrick   /// the instruction defining that vreg.
5609467b48Spatrick   /// Handle immediates, registers (physical and virtual) explicitly,
5709467b48Spatrick   /// and return a common value for the other cases.
5809467b48Spatrick   /// Instruction will be named in the following scheme
5909467b48Spatrick   /// bb<block_no>_hash_<collission_count>.
6009467b48Spatrick   std::string getInstructionOpcodeHash(MachineInstr &MI);
6109467b48Spatrick 
6209467b48Spatrick   /// For all the VRegs that are candidates for renaming,
6309467b48Spatrick   /// return a mapping from old vregs to new vregs with names.
6409467b48Spatrick   std::map<unsigned, unsigned>
6509467b48Spatrick   getVRegRenameMap(const std::vector<NamedVReg> &VRegs);
6609467b48Spatrick 
6709467b48Spatrick   /// Perform replacing of registers based on the <old,new> vreg map.
6809467b48Spatrick   bool doVRegRenaming(const std::map<unsigned, unsigned> &VRegRenameMap);
6909467b48Spatrick 
7009467b48Spatrick   /// createVirtualRegister - Given an existing vreg, create a named vreg to
7109467b48Spatrick   /// take its place. The name is determined by calling
7209467b48Spatrick   /// getInstructionOpcodeHash.
7309467b48Spatrick   unsigned createVirtualRegister(unsigned VReg);
7409467b48Spatrick 
7509467b48Spatrick   /// Create a vreg with name and return it.
7609467b48Spatrick   unsigned createVirtualRegisterWithLowerName(unsigned VReg, StringRef Name);
77*097a140dSpatrick 
7809467b48Spatrick   /// Linearly traverse the MachineBasicBlock and rename each instruction's
7909467b48Spatrick   /// vreg definition based on the semantics of the instruction.
8009467b48Spatrick   /// Names are as follows bb<BBNum>_hash_[0-9]+
8109467b48Spatrick   bool renameInstsInMBB(MachineBasicBlock *MBB);
8209467b48Spatrick 
8309467b48Spatrick public:
8409467b48Spatrick   VRegRenamer() = delete;
VRegRenamer(MachineRegisterInfo & MRI)8509467b48Spatrick   VRegRenamer(MachineRegisterInfo &MRI) : MRI(MRI) {}
8609467b48Spatrick 
8709467b48Spatrick   /// Same as the above, but sets a BBNum depending on BB traversal that
8809467b48Spatrick   /// will be used as prefix for the vreg names.
renameVRegs(MachineBasicBlock * MBB,unsigned BBNum)8909467b48Spatrick   bool renameVRegs(MachineBasicBlock *MBB, unsigned BBNum) {
9009467b48Spatrick     CurrentBBNumber = BBNum;
9109467b48Spatrick     return renameInstsInMBB(MBB);
9209467b48Spatrick   }
9309467b48Spatrick };
9409467b48Spatrick 
9509467b48Spatrick } // namespace llvm
9609467b48Spatrick 
9709467b48Spatrick #endif
98