blob: 998d391bf8349c9bd094dd2b5b2e90a4b6b7d788 [file] [log] [blame]
Puyan Lotfi028061d2019-09-04 21:29:10 +00001//===---------- MIRVRegNamerUtils.cpp - MIR VReg Renaming Utilities -------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "MIRVRegNamerUtils.h"
Reid Kleckner1d7b4132019-10-19 00:22:07 +000010#include "llvm/Support/Debug.h"
Puyan Lotfi028061d2019-09-04 21:29:10 +000011
12using namespace llvm;
13
14#define DEBUG_TYPE "mir-vregnamer-utils"
15
Puyan Lotfi479e3b82019-12-09 14:54:09 -050016using VRegRenameMap = std::map<unsigned, unsigned>;
17
18bool VRegRenamer::doVRegRenaming(const VRegRenameMap &VRM) {
Puyan Lotfi028061d2019-09-04 21:29:10 +000019 bool Changed = false;
Puyan Lotfi028061d2019-09-04 21:29:10 +000020
Puyan Lotfi479e3b82019-12-09 14:54:09 -050021 for (const auto &E : VRM) {
22 Changed = Changed || !MRI.reg_empty(E.first);
23 MRI.replaceRegWith(E.first, E.second);
Puyan Lotfi028061d2019-09-04 21:29:10 +000024 }
25
26 return Changed;
27}
28
Puyan Lotfi479e3b82019-12-09 14:54:09 -050029VRegRenameMap
Aditya Nandakumar72768682019-11-15 08:23:32 -080030VRegRenamer::getVRegRenameMap(const std::vector<NamedVReg> &VRegs) {
Puyan Lotfi028061d2019-09-04 21:29:10 +000031
Puyan Lotfi479e3b82019-12-09 14:54:09 -050032 StringMap<unsigned> VRegNameCollisionMap;
Puyan Lotfi028061d2019-09-04 21:29:10 +000033
Puyan Lotfi479e3b82019-12-09 14:54:09 -050034 auto GetUniqueVRegName = [&VRegNameCollisionMap](const NamedVReg &Reg) {
35 if (VRegNameCollisionMap.find(Reg.getName()) == VRegNameCollisionMap.end())
36 VRegNameCollisionMap[Reg.getName()] = 0;
37 const unsigned Counter = ++VRegNameCollisionMap[Reg.getName()];
Aditya Nandakumar72768682019-11-15 08:23:32 -080038 return Reg.getName() + "__" + std::to_string(Counter);
39 };
Puyan Lotfi028061d2019-09-04 21:29:10 +000040
Puyan Lotfi479e3b82019-12-09 14:54:09 -050041 VRegRenameMap VRM;
42 for (const auto &VReg : VRegs) {
43 const unsigned Reg = VReg.getReg();
44 VRM[Reg] = createVirtualRegisterWithLowerName(Reg, GetUniqueVRegName(VReg));
Aditya Nandakumar72768682019-11-15 08:23:32 -080045 }
Puyan Lotfi479e3b82019-12-09 14:54:09 -050046 return VRM;
Aditya Nandakumar72768682019-11-15 08:23:32 -080047}
Puyan Lotfi028061d2019-09-04 21:29:10 +000048
Aditya Nandakumar72768682019-11-15 08:23:32 -080049std::string VRegRenamer::getInstructionOpcodeHash(MachineInstr &MI) {
50 std::string S;
51 raw_string_ostream OS(S);
Puyan Lotfi479e3b82019-12-09 14:54:09 -050052
53 // Gets a hashable artifact from a given MachineOperand (ie an unsigned).
54 auto GetHashableMO = [this](const MachineOperand &MO) -> unsigned {
Aditya Nandakumar72768682019-11-15 08:23:32 -080055 if (MO.isImm())
56 return MO.getImm();
57 if (MO.isTargetIndex())
58 return MO.getOffset() | (MO.getTargetFlags() << 16);
Puyan Lotfi479e3b82019-12-09 14:54:09 -050059 if (MO.isReg() && Register::isVirtualRegister(MO.getReg()))
60 return MRI.getVRegDef(MO.getReg())->getOpcode();
61 if (MO.isReg())
62 return MO.getReg();
63 // TODO:
Aditya Nandakumar72768682019-11-15 08:23:32 -080064 // We could explicitly handle all the types of the MachineOperand,
65 // here but we can just return a common number until we find a
66 // compelling test case where this is bad. The only side effect here
Puyan Lotfi479e3b82019-12-09 14:54:09 -050067 // is contributing to a hash collision but there's enough information
Aditya Nandakumar72768682019-11-15 08:23:32 -080068 // (Opcodes,other registers etc) that this will likely not be a problem.
69 return 0;
70 };
Puyan Lotfi479e3b82019-12-09 14:54:09 -050071
72 SmallVector<unsigned, 16> MIOperands = {MI.getOpcode()};
73 llvm::transform(MI.uses(), std::back_inserter(MIOperands), GetHashableMO);
74
Aditya Nandakumar72768682019-11-15 08:23:32 -080075 auto HashMI = hash_combine_range(MIOperands.begin(), MIOperands.end());
76 return std::to_string(HashMI).substr(0, 5);
77}
Puyan Lotfi028061d2019-09-04 21:29:10 +000078
Aditya Nandakumar72768682019-11-15 08:23:32 -080079unsigned VRegRenamer::createVirtualRegister(unsigned VReg) {
Puyan Lotfi479e3b82019-12-09 14:54:09 -050080 assert(Register::isVirtualRegister(VReg) && "Expected Virtual Registers");
81 std::string Name = getInstructionOpcodeHash(*MRI.getVRegDef(VReg));
82 return createVirtualRegisterWithLowerName(VReg, Name);
Aditya Nandakumar72768682019-11-15 08:23:32 -080083}
84
85bool VRegRenamer::renameInstsInMBB(MachineBasicBlock *MBB) {
86 std::vector<NamedVReg> VRegs;
87 std::string Prefix = "bb" + std::to_string(getCurrentBBNumber()) + "_";
Puyan Lotfi479e3b82019-12-09 14:54:09 -050088 for (MachineInstr &Candidate : *MBB) {
Aditya Nandakumar72768682019-11-15 08:23:32 -080089 // Don't rename stores/branches.
90 if (Candidate.mayStore() || Candidate.isBranch())
91 continue;
92 if (!Candidate.getNumOperands())
93 continue;
94 // Look for instructions that define VRegs in operand 0.
95 MachineOperand &MO = Candidate.getOperand(0);
96 // Avoid non regs, instructions defining physical regs.
97 if (!MO.isReg() || !Register::isVirtualRegister(MO.getReg()))
98 continue;
99 VRegs.push_back(
100 NamedVReg(MO.getReg(), Prefix + getInstructionOpcodeHash(Candidate)));
Puyan Lotfi028061d2019-09-04 21:29:10 +0000101 }
102
Puyan Lotfi479e3b82019-12-09 14:54:09 -0500103 return VRegs.size() ? doVRegRenaming(getVRegRenameMap(VRegs)) : false;
Puyan Lotfi028061d2019-09-04 21:29:10 +0000104}
105
Aditya Nandakumar72768682019-11-15 08:23:32 -0800106bool VRegRenamer::renameVRegs(MachineBasicBlock *MBB, unsigned BBNum) {
107 CurrentBBNumber = BBNum;
108 return renameInstsInMBB(MBB);
109}
110
Puyan Lotfi479e3b82019-12-09 14:54:09 -0500111unsigned VRegRenamer::createVirtualRegisterWithLowerName(unsigned VReg,
112 StringRef Name) {
113 std::string LowerName = Name.lower();
114 const TargetRegisterClass *RC = MRI.getRegClassOrNull(VReg);
115 return RC ? MRI.createVirtualRegister(RC, LowerName)
116 : MRI.createGenericVirtualRegister(MRI.getType(VReg), LowerName);
Puyan Lotfi028061d2019-09-04 21:29:10 +0000117}