| Puyan Lotfi | 028061d | 2019-09-04 21:29:10 +0000 | [diff] [blame] | 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" |
| Reid Kleckner | 1d7b413 | 2019-10-19 00:22:07 +0000 | [diff] [blame] | 10 | #include "llvm/Support/Debug.h" |
| Puyan Lotfi | 028061d | 2019-09-04 21:29:10 +0000 | [diff] [blame] | 11 | |
| 12 | using namespace llvm; |
| 13 | |
| 14 | #define DEBUG_TYPE "mir-vregnamer-utils" |
| 15 | |
| Puyan Lotfi | 479e3b8 | 2019-12-09 14:54:09 -0500 | [diff] [blame] | 16 | using VRegRenameMap = std::map<unsigned, unsigned>; |
| 17 | |
| 18 | bool VRegRenamer::doVRegRenaming(const VRegRenameMap &VRM) { |
| Puyan Lotfi | 028061d | 2019-09-04 21:29:10 +0000 | [diff] [blame] | 19 | bool Changed = false; |
| Puyan Lotfi | 028061d | 2019-09-04 21:29:10 +0000 | [diff] [blame] | 20 | |
| Puyan Lotfi | 479e3b8 | 2019-12-09 14:54:09 -0500 | [diff] [blame] | 21 | for (const auto &E : VRM) { |
| 22 | Changed = Changed || !MRI.reg_empty(E.first); |
| 23 | MRI.replaceRegWith(E.first, E.second); |
| Puyan Lotfi | 028061d | 2019-09-04 21:29:10 +0000 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | return Changed; |
| 27 | } |
| 28 | |
| Puyan Lotfi | 479e3b8 | 2019-12-09 14:54:09 -0500 | [diff] [blame] | 29 | VRegRenameMap |
| Aditya Nandakumar | 7276868 | 2019-11-15 08:23:32 -0800 | [diff] [blame] | 30 | VRegRenamer::getVRegRenameMap(const std::vector<NamedVReg> &VRegs) { |
| Puyan Lotfi | 028061d | 2019-09-04 21:29:10 +0000 | [diff] [blame] | 31 | |
| Puyan Lotfi | 479e3b8 | 2019-12-09 14:54:09 -0500 | [diff] [blame] | 32 | StringMap<unsigned> VRegNameCollisionMap; |
| Puyan Lotfi | 028061d | 2019-09-04 21:29:10 +0000 | [diff] [blame] | 33 | |
| Puyan Lotfi | 479e3b8 | 2019-12-09 14:54:09 -0500 | [diff] [blame] | 34 | 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 Nandakumar | 7276868 | 2019-11-15 08:23:32 -0800 | [diff] [blame] | 38 | return Reg.getName() + "__" + std::to_string(Counter); |
| 39 | }; |
| Puyan Lotfi | 028061d | 2019-09-04 21:29:10 +0000 | [diff] [blame] | 40 | |
| Puyan Lotfi | 479e3b8 | 2019-12-09 14:54:09 -0500 | [diff] [blame] | 41 | VRegRenameMap VRM; |
| 42 | for (const auto &VReg : VRegs) { |
| 43 | const unsigned Reg = VReg.getReg(); |
| 44 | VRM[Reg] = createVirtualRegisterWithLowerName(Reg, GetUniqueVRegName(VReg)); |
| Aditya Nandakumar | 7276868 | 2019-11-15 08:23:32 -0800 | [diff] [blame] | 45 | } |
| Puyan Lotfi | 479e3b8 | 2019-12-09 14:54:09 -0500 | [diff] [blame] | 46 | return VRM; |
| Aditya Nandakumar | 7276868 | 2019-11-15 08:23:32 -0800 | [diff] [blame] | 47 | } |
| Puyan Lotfi | 028061d | 2019-09-04 21:29:10 +0000 | [diff] [blame] | 48 | |
| Aditya Nandakumar | 7276868 | 2019-11-15 08:23:32 -0800 | [diff] [blame] | 49 | std::string VRegRenamer::getInstructionOpcodeHash(MachineInstr &MI) { |
| 50 | std::string S; |
| 51 | raw_string_ostream OS(S); |
| Puyan Lotfi | 479e3b8 | 2019-12-09 14:54:09 -0500 | [diff] [blame] | 52 | |
| 53 | // Gets a hashable artifact from a given MachineOperand (ie an unsigned). |
| 54 | auto GetHashableMO = [this](const MachineOperand &MO) -> unsigned { |
| Puyan Lotfi | 816985c | 2019-12-14 00:58:44 -0500 | [diff] [blame] | 55 | switch (MO.getType()) { |
| Puyan Lotfi | f63b64c | 2019-12-16 13:23:03 -0500 | [diff] [blame] | 56 | 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 Lotfi | 816985c | 2019-12-14 00:58:44 -0500 | [diff] [blame] | 63 | case MachineOperand::MO_Register: |
| 64 | if (Register::isVirtualRegister(MO.getReg())) |
| 65 | return MRI.getVRegDef(MO.getReg())->getOpcode(); |
| Puyan Lotfi | 479e3b8 | 2019-12-09 14:54:09 -0500 | [diff] [blame] | 66 | return MO.getReg(); |
| Puyan Lotfi | 204dfab | 2019-12-16 18:49:03 -0500 | [diff] [blame] | 67 | case MachineOperand::MO_Immediate: |
| 68 | return MO.getImm(); |
| 69 | case MachineOperand::MO_TargetIndex: |
| 70 | return MO.getOffset() | (MO.getTargetFlags() << 16); |
| Puyan Lotfi | 484a747 | 2020-01-13 13:30:20 -0500 | [diff] [blame] | 71 | case MachineOperand::MO_FrameIndex: |
| Puyan Lotfi | 4b8af31 | 2020-03-10 00:31:01 -0400 | [diff] [blame] | 72 | case MachineOperand::MO_ConstantPoolIndex: |
| Puyan Lotfi | 484a747 | 2020-01-13 13:30:20 -0500 | [diff] [blame] | 73 | return llvm::hash_value(MO); |
| Puyan Lotfi | 816985c | 2019-12-14 00:58:44 -0500 | [diff] [blame] | 74 | |
| Aditya Nandakumar | 7276868 | 2019-11-15 08:23:32 -0800 | [diff] [blame] | 75 | // We could explicitly handle all the types of the MachineOperand, |
| 76 | // here but we can just return a common number until we find a |
| 77 | // compelling test case where this is bad. The only side effect here |
| Puyan Lotfi | 479e3b8 | 2019-12-09 14:54:09 -0500 | [diff] [blame] | 78 | // is contributing to a hash collision but there's enough information |
| Aditya Nandakumar | 7276868 | 2019-11-15 08:23:32 -0800 | [diff] [blame] | 79 | // (Opcodes,other registers etc) that this will likely not be a problem. |
| Puyan Lotfi | 816985c | 2019-12-14 00:58:44 -0500 | [diff] [blame] | 80 | |
| Puyan Lotfi | 204dfab | 2019-12-16 18:49:03 -0500 | [diff] [blame] | 81 | // TODO: Handle the following Index/ID/Predicate cases. They can |
| Puyan Lotfi | 816985c | 2019-12-14 00:58:44 -0500 | [diff] [blame] | 82 | // be hashed on in a stable manner. |
| Puyan Lotfi | 816985c | 2019-12-14 00:58:44 -0500 | [diff] [blame] | 83 | case MachineOperand::MO_JumpTableIndex: |
| 84 | case MachineOperand::MO_CFIIndex: |
| 85 | case MachineOperand::MO_IntrinsicID: |
| 86 | case MachineOperand::MO_Predicate: |
| 87 | |
| 88 | // In the cases below we havn't found a way to produce an artifact that will |
| 89 | // result in a stable hash, in most cases because they are pointers. We want |
| 90 | // stable hashes because we want the hash to be the same run to run. |
| 91 | case MachineOperand::MO_MachineBasicBlock: |
| 92 | case MachineOperand::MO_ExternalSymbol: |
| 93 | case MachineOperand::MO_GlobalAddress: |
| 94 | case MachineOperand::MO_BlockAddress: |
| 95 | case MachineOperand::MO_RegisterMask: |
| 96 | case MachineOperand::MO_RegisterLiveOut: |
| 97 | case MachineOperand::MO_Metadata: |
| 98 | case MachineOperand::MO_MCSymbol: |
| 99 | case MachineOperand::MO_ShuffleMask: |
| 100 | return 0; |
| 101 | } |
| 102 | llvm_unreachable("Unexpected MachineOperandType."); |
| Aditya Nandakumar | 7276868 | 2019-11-15 08:23:32 -0800 | [diff] [blame] | 103 | }; |
| Puyan Lotfi | 479e3b8 | 2019-12-09 14:54:09 -0500 | [diff] [blame] | 104 | |
| Puyan Lotfi | f364686 | 2019-12-10 04:40:36 -0500 | [diff] [blame] | 105 | SmallVector<unsigned, 16> MIOperands = {MI.getOpcode(), MI.getFlags()}; |
| Puyan Lotfi | 479e3b8 | 2019-12-09 14:54:09 -0500 | [diff] [blame] | 106 | llvm::transform(MI.uses(), std::back_inserter(MIOperands), GetHashableMO); |
| 107 | |
| Puyan Lotfi | f5b7a46 | 2019-12-11 01:39:17 -0500 | [diff] [blame] | 108 | for (const auto *Op : MI.memoperands()) { |
| 109 | MIOperands.push_back((unsigned)Op->getSize()); |
| 110 | MIOperands.push_back((unsigned)Op->getFlags()); |
| 111 | MIOperands.push_back((unsigned)Op->getOffset()); |
| 112 | MIOperands.push_back((unsigned)Op->getOrdering()); |
| 113 | MIOperands.push_back((unsigned)Op->getAddrSpace()); |
| 114 | MIOperands.push_back((unsigned)Op->getSyncScopeID()); |
| 115 | MIOperands.push_back((unsigned)Op->getBaseAlignment()); |
| 116 | MIOperands.push_back((unsigned)Op->getFailureOrdering()); |
| 117 | } |
| 118 | |
| Aditya Nandakumar | 7276868 | 2019-11-15 08:23:32 -0800 | [diff] [blame] | 119 | auto HashMI = hash_combine_range(MIOperands.begin(), MIOperands.end()); |
| 120 | return std::to_string(HashMI).substr(0, 5); |
| 121 | } |
| Puyan Lotfi | 028061d | 2019-09-04 21:29:10 +0000 | [diff] [blame] | 122 | |
| Aditya Nandakumar | 7276868 | 2019-11-15 08:23:32 -0800 | [diff] [blame] | 123 | unsigned VRegRenamer::createVirtualRegister(unsigned VReg) { |
| Puyan Lotfi | 479e3b8 | 2019-12-09 14:54:09 -0500 | [diff] [blame] | 124 | assert(Register::isVirtualRegister(VReg) && "Expected Virtual Registers"); |
| 125 | std::string Name = getInstructionOpcodeHash(*MRI.getVRegDef(VReg)); |
| 126 | return createVirtualRegisterWithLowerName(VReg, Name); |
| Aditya Nandakumar | 7276868 | 2019-11-15 08:23:32 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | bool VRegRenamer::renameInstsInMBB(MachineBasicBlock *MBB) { |
| 130 | std::vector<NamedVReg> VRegs; |
| Puyan Lotfi | 756db63 | 2019-12-12 03:27:47 -0500 | [diff] [blame] | 131 | std::string Prefix = "bb" + std::to_string(CurrentBBNumber) + "_"; |
| Puyan Lotfi | 479e3b8 | 2019-12-09 14:54:09 -0500 | [diff] [blame] | 132 | for (MachineInstr &Candidate : *MBB) { |
| Aditya Nandakumar | 7276868 | 2019-11-15 08:23:32 -0800 | [diff] [blame] | 133 | // Don't rename stores/branches. |
| 134 | if (Candidate.mayStore() || Candidate.isBranch()) |
| 135 | continue; |
| 136 | if (!Candidate.getNumOperands()) |
| 137 | continue; |
| 138 | // Look for instructions that define VRegs in operand 0. |
| 139 | MachineOperand &MO = Candidate.getOperand(0); |
| 140 | // Avoid non regs, instructions defining physical regs. |
| 141 | if (!MO.isReg() || !Register::isVirtualRegister(MO.getReg())) |
| 142 | continue; |
| 143 | VRegs.push_back( |
| 144 | NamedVReg(MO.getReg(), Prefix + getInstructionOpcodeHash(Candidate))); |
| Puyan Lotfi | 028061d | 2019-09-04 21:29:10 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| Puyan Lotfi | 479e3b8 | 2019-12-09 14:54:09 -0500 | [diff] [blame] | 147 | return VRegs.size() ? doVRegRenaming(getVRegRenameMap(VRegs)) : false; |
| Puyan Lotfi | 028061d | 2019-09-04 21:29:10 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| Puyan Lotfi | 479e3b8 | 2019-12-09 14:54:09 -0500 | [diff] [blame] | 150 | unsigned VRegRenamer::createVirtualRegisterWithLowerName(unsigned VReg, |
| 151 | StringRef Name) { |
| 152 | std::string LowerName = Name.lower(); |
| 153 | const TargetRegisterClass *RC = MRI.getRegClassOrNull(VReg); |
| 154 | return RC ? MRI.createVirtualRegister(RC, LowerName) |
| 155 | : MRI.createGenericVirtualRegister(MRI.getType(VReg), LowerName); |
| Puyan Lotfi | 028061d | 2019-09-04 21:29:10 +0000 | [diff] [blame] | 156 | } |