Skip to content

Commit

Permalink
[NFC][llvm][MIRVRegNamerUtils] Refactoring GetHashableMO into switch-…
Browse files Browse the repository at this point in the history
…statement.

This refactors the if-statements handling the hashing of various
MachineOperand types into a switch-statement. The purpose is to cover
all the basis for all MachineOperand types while being very deliberate
about which MachineOperand types we are not handling and why (better
added comments). This patch is a NFC redo of https://reviews.llvm.org/D71396.
Much of the changes present in D71396 will come in smaller follow-up patches
that will add support for hashing the MachineOperand types that aren't
covered piece-meal with tests for each new case.
  • Loading branch information
plotfi committed Dec 14, 2019
1 parent 6cc2b1d commit 816985c
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions llvm/lib/CodeGen/MIRVRegNamerUtils.cpp
Expand Up @@ -52,21 +52,48 @@ std::string VRegRenamer::getInstructionOpcodeHash(MachineInstr &MI) {

// Gets a hashable artifact from a given MachineOperand (ie an unsigned).
auto GetHashableMO = [this](const MachineOperand &MO) -> unsigned {
if (MO.isImm())
switch (MO.getType()) {
case MachineOperand::MO_Immediate:
return MO.getImm();
if (MO.isTargetIndex())
case MachineOperand::MO_TargetIndex:
return MO.getOffset() | (MO.getTargetFlags() << 16);
if (MO.isReg() && Register::isVirtualRegister(MO.getReg()))
return MRI.getVRegDef(MO.getReg())->getOpcode();
if (MO.isReg())
case MachineOperand::MO_Register:
if (Register::isVirtualRegister(MO.getReg()))
return MRI.getVRegDef(MO.getReg())->getOpcode();
return MO.getReg();
// TODO:

// We could explicitly handle all the types of the MachineOperand,
// here but we can just return a common number until we find a
// compelling test case where this is bad. The only side effect here
// is contributing to a hash collision but there's enough information
// (Opcodes,other registers etc) that this will likely not be a problem.
return 0;

// TODO: Handle the following Immediate/Index/ID/Predicate cases. They can
// be hashed on in a stable manner.
case MachineOperand::MO_CImmediate:
case MachineOperand::MO_FPImmediate:
case MachineOperand::MO_FrameIndex:
case MachineOperand::MO_ConstantPoolIndex:
case MachineOperand::MO_JumpTableIndex:
case MachineOperand::MO_CFIIndex:
case MachineOperand::MO_IntrinsicID:
case MachineOperand::MO_Predicate:

// In the cases below we havn't found a way to produce an artifact that will
// result in a stable hash, in most cases because they are pointers. We want
// stable hashes because we want the hash to be the same run to run.
case MachineOperand::MO_MachineBasicBlock:
case MachineOperand::MO_ExternalSymbol:
case MachineOperand::MO_GlobalAddress:
case MachineOperand::MO_BlockAddress:
case MachineOperand::MO_RegisterMask:
case MachineOperand::MO_RegisterLiveOut:
case MachineOperand::MO_Metadata:
case MachineOperand::MO_MCSymbol:
case MachineOperand::MO_ShuffleMask:
return 0;
}
llvm_unreachable("Unexpected MachineOperandType.");
};

SmallVector<unsigned, 16> MIOperands = {MI.getOpcode(), MI.getFlags()};
Expand Down

0 comments on commit 816985c

Please sign in to comment.