blob: d0670dcc406eb1141430351676a2ea7b246c772a [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 {
Puyan Lotfi816985c2019-12-14 00:58:44 -050055 switch (MO.getType()) {
Puyan Lotfif63b64c2019-12-16 13:23:03 -050056 case MachineOperand::MO_CImmediate:
57 return hash_combine(MO.getType(), MO.getTargetFlags(),
58 MO.getCImm()->getZExtValue());
59 case MachineOperand::MO_FPImmediate:
60 return hash_combine(
61 MO.getType(), MO.getTargetFlags(),
62 MO.getFPImm()->getValueAPF().bitcastToAPInt().getZExtValue());
Puyan Lotfi816985c2019-12-14 00:58:44 -050063 case MachineOperand::MO_Register:
64 if (Register::isVirtualRegister(MO.getReg()))
65 return MRI.getVRegDef(MO.getReg())->getOpcode();
Puyan Lotfi479e3b82019-12-09 14:54:09 -050066 return MO.getReg();
Puyan Lotfi204dfab2019-12-16 18:49:03 -050067 case MachineOperand::MO_Immediate:
68 return MO.getImm();
69 case MachineOperand::MO_TargetIndex:
70 return MO.getOffset() | (MO.getTargetFlags() << 16);
Puyan Lotfi816985c2019-12-14 00:58:44 -050071
Aditya Nandakumar72768682019-11-15 08:23:32 -080072 // We could explicitly handle all the types of the MachineOperand,
73 // here but we can just return a common number until we find a
74 // compelling test case where this is bad. The only side effect here
Puyan Lotfi479e3b82019-12-09 14:54:09 -050075 // is contributing to a hash collision but there's enough information
Aditya Nandakumar72768682019-11-15 08:23:32 -080076 // (Opcodes,other registers etc) that this will likely not be a problem.
Puyan Lotfi816985c2019-12-14 00:58:44 -050077
Puyan Lotfi204dfab2019-12-16 18:49:03 -050078 // TODO: Handle the following Index/ID/Predicate cases. They can
Puyan Lotfi816985c2019-12-14 00:58:44 -050079 // be hashed on in a stable manner.
Puyan Lotfi816985c2019-12-14 00:58:44 -050080 case MachineOperand::MO_FrameIndex:
81 case MachineOperand::MO_ConstantPoolIndex:
82 case MachineOperand::MO_JumpTableIndex:
83 case MachineOperand::MO_CFIIndex:
84 case MachineOperand::MO_IntrinsicID:
85 case MachineOperand::MO_Predicate:
86
87 // In the cases below we havn't found a way to produce an artifact that will
88 // result in a stable hash, in most cases because they are pointers. We want
89 // stable hashes because we want the hash to be the same run to run.
90 case MachineOperand::MO_MachineBasicBlock:
91 case MachineOperand::MO_ExternalSymbol:
92 case MachineOperand::MO_GlobalAddress:
93 case MachineOperand::MO_BlockAddress:
94 case MachineOperand::MO_RegisterMask:
95 case MachineOperand::MO_RegisterLiveOut:
96 case MachineOperand::MO_Metadata:
97 case MachineOperand::MO_MCSymbol:
98 case MachineOperand::MO_ShuffleMask:
99 return 0;
100 }
101 llvm_unreachable("Unexpected MachineOperandType.");
Aditya Nandakumar72768682019-11-15 08:23:32 -0800102 };
Puyan Lotfi479e3b82019-12-09 14:54:09 -0500103
Puyan Lotfif3646862019-12-10 04:40:36 -0500104 SmallVector<unsigned, 16> MIOperands = {MI.getOpcode(), MI.getFlags()};
Puyan Lotfi479e3b82019-12-09 14:54:09 -0500105 llvm::transform(MI.uses(), std::back_inserter(MIOperands), GetHashableMO);
106
Puyan Lotfif5b7a462019-12-11 01:39:17 -0500107 for (const auto *Op : MI.memoperands()) {
108 MIOperands.push_back((unsigned)Op->getSize());
109 MIOperands.push_back((unsigned)Op->getFlags());
110 MIOperands.push_back((unsigned)Op->getOffset());
111 MIOperands.push_back((unsigned)Op->getOrdering());
112 MIOperands.push_back((unsigned)Op->getAddrSpace());
113 MIOperands.push_back((unsigned)Op->getSyncScopeID());
114 MIOperands.push_back((unsigned)Op->getBaseAlignment());
115 MIOperands.push_back((unsigned)Op->getFailureOrdering());
116 }
117
Aditya Nandakumar72768682019-11-15 08:23:32 -0800118 auto HashMI = hash_combine_range(MIOperands.begin(), MIOperands.end());
119 return std::to_string(HashMI).substr(0, 5);
120}
Puyan Lotfi028061d2019-09-04 21:29:10 +0000121
Aditya Nandakumar72768682019-11-15 08:23:32 -0800122unsigned VRegRenamer::createVirtualRegister(unsigned VReg) {
Puyan Lotfi479e3b82019-12-09 14:54:09 -0500123 assert(Register::isVirtualRegister(VReg) && "Expected Virtual Registers");
124 std::string Name = getInstructionOpcodeHash(*MRI.getVRegDef(VReg));
125 return createVirtualRegisterWithLowerName(VReg, Name);
Aditya Nandakumar72768682019-11-15 08:23:32 -0800126}
127
128bool VRegRenamer::renameInstsInMBB(MachineBasicBlock *MBB) {
129 std::vector<NamedVReg> VRegs;
Puyan Lotfi756db632019-12-12 03:27:47 -0500130 std::string Prefix = "bb" + std::to_string(CurrentBBNumber) + "_";
Puyan Lotfi479e3b82019-12-09 14:54:09 -0500131 for (MachineInstr &Candidate : *MBB) {
Aditya Nandakumar72768682019-11-15 08:23:32 -0800132 // Don't rename stores/branches.
133 if (Candidate.mayStore() || Candidate.isBranch())
134 continue;
135 if (!Candidate.getNumOperands())
136 continue;
137 // Look for instructions that define VRegs in operand 0.
138 MachineOperand &MO = Candidate.getOperand(0);
139 // Avoid non regs, instructions defining physical regs.
140 if (!MO.isReg() || !Register::isVirtualRegister(MO.getReg()))
141 continue;
142 VRegs.push_back(
143 NamedVReg(MO.getReg(), Prefix + getInstructionOpcodeHash(Candidate)));
Puyan Lotfi028061d2019-09-04 21:29:10 +0000144 }
145
Puyan Lotfi479e3b82019-12-09 14:54:09 -0500146 return VRegs.size() ? doVRegRenaming(getVRegRenameMap(VRegs)) : false;
Puyan Lotfi028061d2019-09-04 21:29:10 +0000147}
148
Puyan Lotfi479e3b82019-12-09 14:54:09 -0500149unsigned VRegRenamer::createVirtualRegisterWithLowerName(unsigned VReg,
150 StringRef Name) {
151 std::string LowerName = Name.lower();
152 const TargetRegisterClass *RC = MRI.getRegClassOrNull(VReg);
153 return RC ? MRI.createVirtualRegister(RC, LowerName)
154 : MRI.createGenericVirtualRegister(MRI.getType(VReg), LowerName);
Puyan Lotfi028061d2019-09-04 21:29:10 +0000155}