MIRVRegNamerUtils.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. #include "MIRVRegNamerUtils.h"
  9. #include "llvm/CodeGen/MachineRegisterInfo.h"
  10. #include "llvm/CodeGen/MachineStableHash.h"
  11. #include "llvm/IR/Constants.h"
  12. #include "llvm/Support/Debug.h"
  13. using namespace llvm;
  14. #define DEBUG_TYPE "mir-vregnamer-utils"
  15. static cl::opt<bool>
  16. UseStableNamerHash("mir-vreg-namer-use-stable-hash", cl::init(false),
  17. cl::Hidden,
  18. cl::desc("Use Stable Hashing for MIR VReg Renaming"));
  19. using VRegRenameMap = std::map<unsigned, unsigned>;
  20. bool VRegRenamer::doVRegRenaming(const VRegRenameMap &VRM) {
  21. bool Changed = false;
  22. for (const auto &E : VRM) {
  23. Changed = Changed || !MRI.reg_empty(E.first);
  24. MRI.replaceRegWith(E.first, E.second);
  25. }
  26. return Changed;
  27. }
  28. VRegRenameMap
  29. VRegRenamer::getVRegRenameMap(const std::vector<NamedVReg> &VRegs) {
  30. StringMap<unsigned> VRegNameCollisionMap;
  31. auto GetUniqueVRegName = [&VRegNameCollisionMap](const NamedVReg &Reg) {
  32. if (VRegNameCollisionMap.find(Reg.getName()) == VRegNameCollisionMap.end())
  33. VRegNameCollisionMap[Reg.getName()] = 0;
  34. const unsigned Counter = ++VRegNameCollisionMap[Reg.getName()];
  35. return Reg.getName() + "__" + std::to_string(Counter);
  36. };
  37. VRegRenameMap VRM;
  38. for (const auto &VReg : VRegs) {
  39. const unsigned Reg = VReg.getReg();
  40. VRM[Reg] = createVirtualRegisterWithLowerName(Reg, GetUniqueVRegName(VReg));
  41. }
  42. return VRM;
  43. }
  44. std::string VRegRenamer::getInstructionOpcodeHash(MachineInstr &MI) {
  45. std::string S;
  46. raw_string_ostream OS(S);
  47. if (UseStableNamerHash) {
  48. auto Hash = stableHashValue(MI, /* HashVRegs */ true,
  49. /* HashConstantPoolIndices */ true,
  50. /* HashMemOperands */ true);
  51. assert(Hash && "Expected non-zero Hash");
  52. return std::to_string(Hash).substr(0, 5);
  53. }
  54. // Gets a hashable artifact from a given MachineOperand (ie an unsigned).
  55. auto GetHashableMO = [this](const MachineOperand &MO) -> unsigned {
  56. switch (MO.getType()) {
  57. case MachineOperand::MO_CImmediate:
  58. return hash_combine(MO.getType(), MO.getTargetFlags(),
  59. MO.getCImm()->getZExtValue());
  60. case MachineOperand::MO_FPImmediate:
  61. return hash_combine(
  62. MO.getType(), MO.getTargetFlags(),
  63. MO.getFPImm()->getValueAPF().bitcastToAPInt().getZExtValue());
  64. case MachineOperand::MO_Register:
  65. if (Register::isVirtualRegister(MO.getReg()))
  66. return MRI.getVRegDef(MO.getReg())->getOpcode();
  67. return MO.getReg();
  68. case MachineOperand::MO_Immediate:
  69. return MO.getImm();
  70. case MachineOperand::MO_TargetIndex:
  71. return MO.getOffset() | (MO.getTargetFlags() << 16);
  72. case MachineOperand::MO_FrameIndex:
  73. case MachineOperand::MO_ConstantPoolIndex:
  74. case MachineOperand::MO_JumpTableIndex:
  75. return llvm::hash_value(MO);
  76. // We could explicitly handle all the types of the MachineOperand,
  77. // here but we can just return a common number until we find a
  78. // compelling test case where this is bad. The only side effect here
  79. // is contributing to a hash collision but there's enough information
  80. // (Opcodes,other registers etc) that this will likely not be a problem.
  81. // TODO: Handle the following Index/ID/Predicate cases. They can
  82. // be hashed on in a stable manner.
  83. case MachineOperand::MO_CFIIndex:
  84. case MachineOperand::MO_IntrinsicID:
  85. case MachineOperand::MO_Predicate:
  86. // In the cases below we havn't found a way to produce an artifact that will
  87. // result in a stable hash, in most cases because they are pointers. We want
  88. // stable hashes because we want the hash to be the same run to run.
  89. case MachineOperand::MO_MachineBasicBlock:
  90. case MachineOperand::MO_ExternalSymbol:
  91. case MachineOperand::MO_GlobalAddress:
  92. case MachineOperand::MO_BlockAddress:
  93. case MachineOperand::MO_RegisterMask:
  94. case MachineOperand::MO_RegisterLiveOut:
  95. case MachineOperand::MO_Metadata:
  96. case MachineOperand::MO_MCSymbol:
  97. case MachineOperand::MO_ShuffleMask:
  98. return 0;
  99. }
  100. llvm_unreachable("Unexpected MachineOperandType.");
  101. };
  102. SmallVector<unsigned, 16> MIOperands = {MI.getOpcode(), MI.getFlags()};
  103. llvm::transform(MI.uses(), std::back_inserter(MIOperands), GetHashableMO);
  104. for (const auto *Op : MI.memoperands()) {
  105. MIOperands.push_back((unsigned)Op->getSize());
  106. MIOperands.push_back((unsigned)Op->getFlags());
  107. MIOperands.push_back((unsigned)Op->getOffset());
  108. MIOperands.push_back((unsigned)Op->getSuccessOrdering());
  109. MIOperands.push_back((unsigned)Op->getAddrSpace());
  110. MIOperands.push_back((unsigned)Op->getSyncScopeID());
  111. MIOperands.push_back((unsigned)Op->getBaseAlign().value());
  112. MIOperands.push_back((unsigned)Op->getFailureOrdering());
  113. }
  114. auto HashMI = hash_combine_range(MIOperands.begin(), MIOperands.end());
  115. return std::to_string(HashMI).substr(0, 5);
  116. }
  117. unsigned VRegRenamer::createVirtualRegister(unsigned VReg) {
  118. assert(Register::isVirtualRegister(VReg) && "Expected Virtual Registers");
  119. std::string Name = getInstructionOpcodeHash(*MRI.getVRegDef(VReg));
  120. return createVirtualRegisterWithLowerName(VReg, Name);
  121. }
  122. bool VRegRenamer::renameInstsInMBB(MachineBasicBlock *MBB) {
  123. std::vector<NamedVReg> VRegs;
  124. std::string Prefix = "bb" + std::to_string(CurrentBBNumber) + "_";
  125. for (MachineInstr &Candidate : *MBB) {
  126. // Don't rename stores/branches.
  127. if (Candidate.mayStore() || Candidate.isBranch())
  128. continue;
  129. if (!Candidate.getNumOperands())
  130. continue;
  131. // Look for instructions that define VRegs in operand 0.
  132. MachineOperand &MO = Candidate.getOperand(0);
  133. // Avoid non regs, instructions defining physical regs.
  134. if (!MO.isReg() || !Register::isVirtualRegister(MO.getReg()))
  135. continue;
  136. VRegs.push_back(
  137. NamedVReg(MO.getReg(), Prefix + getInstructionOpcodeHash(Candidate)));
  138. }
  139. return VRegs.size() ? doVRegRenaming(getVRegRenameMap(VRegs)) : false;
  140. }
  141. unsigned VRegRenamer::createVirtualRegisterWithLowerName(unsigned VReg,
  142. StringRef Name) {
  143. std::string LowerName = Name.lower();
  144. const TargetRegisterClass *RC = MRI.getRegClassOrNull(VReg);
  145. return RC ? MRI.createVirtualRegister(RC, LowerName)
  146. : MRI.createGenericVirtualRegister(MRI.getType(VReg), LowerName);
  147. }