LLVM 22.0.0git
MIRVRegNamerUtils.cpp
Go to the documentation of this file.
1//===---------- 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"
12#include "llvm/IR/Constants.h"
13
14using namespace llvm;
15
16#define DEBUG_TYPE "mir-vregnamer-utils"
17
18static cl::opt<bool>
19 UseStableNamerHash("mir-vreg-namer-use-stable-hash", cl::init(false),
21 cl::desc("Use Stable Hashing for MIR VReg Renaming"));
22
23using VRegRenameMap = std::map<Register, Register>;
24
25bool VRegRenamer::doVRegRenaming(const VRegRenameMap &VRM) {
26 bool Changed = false;
27
28 for (const auto &E : VRM) {
29 Changed = Changed || !MRI.reg_empty(E.first);
30 MRI.replaceRegWith(E.first, E.second);
31 }
32
33 return Changed;
34}
35
37VRegRenamer::getVRegRenameMap(const std::vector<NamedVReg> &VRegs) {
38
39 StringMap<unsigned> VRegNameCollisionMap;
40
41 auto GetUniqueVRegName = [&VRegNameCollisionMap](const NamedVReg &Reg) {
42 const unsigned Counter = ++VRegNameCollisionMap[Reg.getName()];
43 return Reg.getName() + "__" + std::to_string(Counter);
44 };
45
46 VRegRenameMap VRM;
47 for (const auto &VReg : VRegs) {
48 const Register Reg = VReg.getReg();
49 VRM[Reg] = createVirtualRegisterWithLowerName(Reg, GetUniqueVRegName(VReg));
50 }
51 return VRM;
52}
53
54std::string VRegRenamer::getInstructionOpcodeHash(MachineInstr &MI) {
55 std::string S;
56 raw_string_ostream OS(S);
57
59 auto Hash = stableHashValue(MI, /* HashVRegs */ true,
60 /* HashConstantPoolIndices */ true,
61 /* HashMemOperands */ true);
62 assert(Hash && "Expected non-zero Hash");
63 OS << format_hex_no_prefix(Hash, 16, true);
64 return OS.str();
65 }
66
67 // Gets a hashable artifact from a given MachineOperand (ie an unsigned).
68 auto GetHashableMO = [this](const MachineOperand &MO) -> unsigned {
69 switch (MO.getType()) {
71 return hash_combine(MO.getType(), MO.getTargetFlags(),
72 MO.getCImm()->getZExtValue());
74 return hash_combine(
75 MO.getType(), MO.getTargetFlags(),
76 MO.getFPImm()->getValueAPF().bitcastToAPInt().getZExtValue());
78 if (MO.getReg().isVirtual())
79 return MRI.getVRegDef(MO.getReg())->getOpcode();
80 return MO.getReg().id();
82 return MO.getImm();
84 return MO.getOffset() | (MO.getTargetFlags() << 16);
88 return llvm::hash_value(MO);
89
90 // We could explicitly handle all the types of the MachineOperand,
91 // here but we can just return a common number until we find a
92 // compelling test case where this is bad. The only side effect here
93 // is contributing to a hash collision but there's enough information
94 // (Opcodes,other registers etc) that this will likely not be a problem.
95
96 // TODO: Handle the following Index/ID/Predicate cases. They can
97 // be hashed on in a stable manner.
101
102 // In the cases below we havn't found a way to produce an artifact that will
103 // result in a stable hash, in most cases because they are pointers. We want
104 // stable hashes because we want the hash to be the same run to run.
115 return 0;
116 }
117 llvm_unreachable("Unexpected MachineOperandType.");
118 };
119
120 SmallVector<unsigned, 16> MIOperands = {MI.getOpcode(), MI.getFlags()};
121 llvm::transform(MI.uses(), std::back_inserter(MIOperands), GetHashableMO);
122
123 for (const auto *Op : MI.memoperands()) {
124 MIOperands.push_back((unsigned)Op->getSize().getValue());
125 MIOperands.push_back((unsigned)Op->getFlags());
126 MIOperands.push_back((unsigned)Op->getOffset());
127 MIOperands.push_back((unsigned)Op->getSuccessOrdering());
128 MIOperands.push_back((unsigned)Op->getAddrSpace());
129 MIOperands.push_back((unsigned)Op->getSyncScopeID());
130 MIOperands.push_back((unsigned)Op->getBaseAlign().value());
131 MIOperands.push_back((unsigned)Op->getFailureOrdering());
132 }
133
134 auto HashMI = hash_combine_range(MIOperands);
135 OS << format_hex_no_prefix(HashMI, 16, true);
136 return OS.str();
137}
138
139Register VRegRenamer::createVirtualRegister(Register VReg) {
140 assert(VReg.isVirtual() && "Expected Virtual Registers");
141 std::string Name = getInstructionOpcodeHash(*MRI.getVRegDef(VReg));
142 return createVirtualRegisterWithLowerName(VReg, Name);
143}
144
145bool VRegRenamer::renameInstsInMBB(MachineBasicBlock *MBB) {
146 std::vector<NamedVReg> VRegs;
147 std::string Prefix = "bb" + std::to_string(CurrentBBNumber) + "_";
148 for (MachineInstr &Candidate : *MBB) {
149 // Don't rename stores/branches.
150 if (Candidate.mayStore() || Candidate.isBranch())
151 continue;
152 if (!Candidate.getNumOperands())
153 continue;
154 // Look for instructions that define VRegs in operand 0.
155 MachineOperand &MO = Candidate.getOperand(0);
156 // Avoid non regs, instructions defining physical regs.
157 if (!MO.isReg() || !MO.getReg().isVirtual())
158 continue;
159 VRegs.push_back(
160 NamedVReg(MO.getReg(), Prefix + getInstructionOpcodeHash(Candidate)));
161 }
162
163 return !VRegs.empty() ? doVRegRenaming(getVRegRenameMap(VRegs)) : false;
164}
165
166Register VRegRenamer::createVirtualRegisterWithLowerName(Register VReg,
167 StringRef Name) {
168 std::string LowerName = Name.lower();
169 const TargetRegisterClass *RC = MRI.getRegClassOrNull(VReg);
170 return RC ? MRI.createVirtualRegister(RC, LowerName)
171 : MRI.createGenericVirtualRegister(MRI.getType(VReg), LowerName);
172}
unsigned const MachineRegisterInfo * MRI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
Function Alias Analysis false
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
IRTranslator LLVM IR MI
static cl::opt< bool > UseStableNamerHash("mir-vreg-namer-use-stable-hash", cl::init(false), cl::Hidden, cl::desc("Use Stable Hashing for MIR VReg Renaming"))
std::map< Register, Register > VRegRenameMap
Register Reg
Promote Memory to Register
Definition Mem2Reg.cpp:110
Representation of each machine instruction.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
Register getReg() const
getReg - Returns the register number.
@ MO_CFIIndex
MCCFIInstruction index.
@ MO_Immediate
Immediate operand.
@ MO_ConstantPoolIndex
Address of indexed Constant in Constant Pool.
@ MO_MCSymbol
MCSymbol reference (for debug/eh info)
@ MO_Predicate
Generic predicate for ISel.
@ MO_GlobalAddress
Address of a global value.
@ MO_RegisterMask
Mask of preserved registers.
@ MO_ShuffleMask
Other IR Constant for ISel (shuffle masks)
@ MO_CImmediate
Immediate >64bit operand.
@ MO_BlockAddress
Address of a basic block.
@ MO_DbgInstrRef
Integer indices referring to an instruction+operand.
@ MO_MachineBasicBlock
MachineBasicBlock reference.
@ MO_FrameIndex
Abstract Stack Frame Index.
@ MO_Register
Register operand.
@ MO_ExternalSymbol
Name of external global symbol.
@ MO_IntrinsicID
Intrinsic ID for ISel.
@ MO_JumpTableIndex
Address of indexed Jump Table for switch.
@ MO_TargetIndex
Target-dependent index+offset operand.
@ MO_Metadata
Metadata reference (for debug info)
@ MO_FPImmediate
Floating-point immediate operand.
@ MO_RegisterLiveOut
Mask of live-out registers.
Wrapper class representing virtual and physical registers.
Definition Register.h:19
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
Definition Register.h:74
void push_back(const T &Elt)
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
Changed
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
initializer< Ty > init(const Ty &Val)
This is an optimization pass for GlobalISel generic memory operations.
hash_code hash_value(const FixedPointSemantics &Val)
OutputIt transform(R &&Range, OutputIt d_first, UnaryFunction F)
Wrapper function around std::transform to apply a function to a range and store the result elsewhere.
Definition STLExtras.h:1948
LLVM_ABI stable_hash stableHashValue(const MachineOperand &MO)
FormattedNumber format_hex_no_prefix(uint64_t N, unsigned Width, bool Upper=false)
format_hex_no_prefix - Output N as a fixed width hexadecimal.
Definition Format.h:201
DWARFExpression::Operation Op
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
Definition Hashing.h:592
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.
Definition Hashing.h:466