16#define DEBUG_TYPE "mir-vregnamer-utils"
21 cl::desc(
"Use Stable Hashing for MIR VReg Renaming"));
28 for (
const auto &E : VRM) {
29 Changed = Changed || !MRI.
reg_empty(E.first);
37VRegRenamer::getVRegRenameMap(
const std::vector<NamedVReg> &VRegs) {
41 auto GetUniqueVRegName = [&VRegNameCollisionMap](
const NamedVReg &
Reg) {
42 const unsigned Counter = ++VRegNameCollisionMap[
Reg.getName()];
43 return Reg.getName() +
"__" + std::to_string(Counter);
47 for (
const auto &VReg : VRegs) {
48 const unsigned Reg = VReg.getReg();
49 VRM[
Reg] = createVirtualRegisterWithLowerName(Reg, GetUniqueVRegName(VReg));
54std::string VRegRenamer::getInstructionOpcodeHash(
MachineInstr &
MI) {
62 assert(Hash &&
"Expected non-zero Hash");
68 auto GetHashableMO = [
this](
const MachineOperand &MO) ->
unsigned {
69 switch (MO.getType()) {
72 MO.getCImm()->getZExtValue());
75 MO.getType(), MO.getTargetFlags(),
76 MO.getFPImm()->getValueAPF().bitcastToAPInt().getZExtValue());
78 if (MO.getReg().isVirtual())
84 return MO.getOffset() | (MO.getTargetFlags() << 16);
123 for (
const auto *
Op :
MI.memoperands()) {
124 MIOperands.
push_back((
unsigned)
Op->getSize().getValue());
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());
139unsigned VRegRenamer::createVirtualRegister(
unsigned VReg) {
141 std::string
Name = getInstructionOpcodeHash(*MRI.
getVRegDef(VReg));
142 return createVirtualRegisterWithLowerName(VReg,
Name);
146 std::vector<NamedVReg> VRegs;
147 std::string
Prefix =
"bb" + std::to_string(CurrentBBNumber) +
"_";
150 if (Candidate.mayStore() || Candidate.isBranch())
152 if (!Candidate.getNumOperands())
160 NamedVReg(MO.
getReg(), Prefix + getInstructionOpcodeHash(Candidate)));
163 return VRegs.size() ? doVRegRenaming(getVRegRenameMap(VRegs)) :
false;
166unsigned VRegRenamer::createVirtualRegisterWithLowerName(
unsigned VReg,
168 std::string LowerName =
Name.lower();
This file contains the declarations for the subclasses of Constant, which represent the different fla...
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< unsigned, unsigned > VRegRenameMap
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This class represents an Operation in the Expression.
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
MachineOperand class - Representation of each machine instruction operand.
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.
MachineInstr * getVRegDef(Register Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
LLT getType(Register Reg) const
Get the low-level type of Reg or LLT{} if Reg is not a generic (target independent) virtual register.
Register createGenericVirtualRegister(LLT Ty, StringRef Name="")
Create and return a new generic virtual register with low-level type Ty.
const TargetRegisterClass * getRegClassOrNull(Register Reg) const
Return the register class of Reg, or null if Reg has not been assigned a register class yet.
bool reg_empty(Register RegNo) const
reg_empty - Return true if there are no instructions using or defining the specified register (it may...
void replaceRegWith(Register FromReg, Register ToReg)
replaceRegWith - Replace all instances of FromReg with ToReg in the machine function.
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
static constexpr bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
StringRef - Represent a constant reference to a string, i.e.
A raw_ostream that writes to an std::string.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Reg
All possible values of the reg field in the ModR/M byte.
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.
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.
hash_code hash_combine(const Ts &...args)
Combine values into a single hash_code.
hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)
Compute a hash_code for a sequence of values.