| 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 { |
| Aditya Nandakumar | 7276868 | 2019-11-15 08:23:32 -0800 | [diff] [blame] | 55 | if (MO.isImm()) |
| 56 | return MO.getImm(); |
| 57 | if (MO.isTargetIndex()) |
| 58 | return MO.getOffset() | (MO.getTargetFlags() << 16); |
| Puyan Lotfi | 479e3b8 | 2019-12-09 14:54:09 -0500 | [diff] [blame^] | 59 | if (MO.isReg() && Register::isVirtualRegister(MO.getReg())) |
| 60 | return MRI.getVRegDef(MO.getReg())->getOpcode(); |
| 61 | if (MO.isReg()) |
| 62 | return MO.getReg(); |
| 63 | // TODO: |
| Aditya Nandakumar | 7276868 | 2019-11-15 08:23:32 -0800 | [diff] [blame] | 64 | // We could explicitly handle all the types of the MachineOperand, |
| 65 | // here but we can just return a common number until we find a |
| 66 | // compelling test case where this is bad. The only side effect here |
| Puyan Lotfi | 479e3b8 | 2019-12-09 14:54:09 -0500 | [diff] [blame^] | 67 | // is contributing to a hash collision but there's enough information |
| Aditya Nandakumar | 7276868 | 2019-11-15 08:23:32 -0800 | [diff] [blame] | 68 | // (Opcodes,other registers etc) that this will likely not be a problem. |
| 69 | return 0; |
| 70 | }; |
| Puyan Lotfi | 479e3b8 | 2019-12-09 14:54:09 -0500 | [diff] [blame^] | 71 | |
| 72 | SmallVector<unsigned, 16> MIOperands = {MI.getOpcode()}; |
| 73 | llvm::transform(MI.uses(), std::back_inserter(MIOperands), GetHashableMO); |
| 74 | |
| Aditya Nandakumar | 7276868 | 2019-11-15 08:23:32 -0800 | [diff] [blame] | 75 | auto HashMI = hash_combine_range(MIOperands.begin(), MIOperands.end()); |
| 76 | return std::to_string(HashMI).substr(0, 5); |
| 77 | } |
| Puyan Lotfi | 028061d | 2019-09-04 21:29:10 +0000 | [diff] [blame] | 78 | |
| Aditya Nandakumar | 7276868 | 2019-11-15 08:23:32 -0800 | [diff] [blame] | 79 | unsigned VRegRenamer::createVirtualRegister(unsigned VReg) { |
| Puyan Lotfi | 479e3b8 | 2019-12-09 14:54:09 -0500 | [diff] [blame^] | 80 | assert(Register::isVirtualRegister(VReg) && "Expected Virtual Registers"); |
| 81 | std::string Name = getInstructionOpcodeHash(*MRI.getVRegDef(VReg)); |
| 82 | return createVirtualRegisterWithLowerName(VReg, Name); |
| Aditya Nandakumar | 7276868 | 2019-11-15 08:23:32 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | bool VRegRenamer::renameInstsInMBB(MachineBasicBlock *MBB) { |
| 86 | std::vector<NamedVReg> VRegs; |
| 87 | std::string Prefix = "bb" + std::to_string(getCurrentBBNumber()) + "_"; |
| Puyan Lotfi | 479e3b8 | 2019-12-09 14:54:09 -0500 | [diff] [blame^] | 88 | for (MachineInstr &Candidate : *MBB) { |
| Aditya Nandakumar | 7276868 | 2019-11-15 08:23:32 -0800 | [diff] [blame] | 89 | // Don't rename stores/branches. |
| 90 | if (Candidate.mayStore() || Candidate.isBranch()) |
| 91 | continue; |
| 92 | if (!Candidate.getNumOperands()) |
| 93 | continue; |
| 94 | // Look for instructions that define VRegs in operand 0. |
| 95 | MachineOperand &MO = Candidate.getOperand(0); |
| 96 | // Avoid non regs, instructions defining physical regs. |
| 97 | if (!MO.isReg() || !Register::isVirtualRegister(MO.getReg())) |
| 98 | continue; |
| 99 | VRegs.push_back( |
| 100 | NamedVReg(MO.getReg(), Prefix + getInstructionOpcodeHash(Candidate))); |
| Puyan Lotfi | 028061d | 2019-09-04 21:29:10 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| Puyan Lotfi | 479e3b8 | 2019-12-09 14:54:09 -0500 | [diff] [blame^] | 103 | return VRegs.size() ? doVRegRenaming(getVRegRenameMap(VRegs)) : false; |
| Puyan Lotfi | 028061d | 2019-09-04 21:29:10 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| Aditya Nandakumar | 7276868 | 2019-11-15 08:23:32 -0800 | [diff] [blame] | 106 | bool VRegRenamer::renameVRegs(MachineBasicBlock *MBB, unsigned BBNum) { |
| 107 | CurrentBBNumber = BBNum; |
| 108 | return renameInstsInMBB(MBB); |
| 109 | } |
| 110 | |
| Puyan Lotfi | 479e3b8 | 2019-12-09 14:54:09 -0500 | [diff] [blame^] | 111 | unsigned VRegRenamer::createVirtualRegisterWithLowerName(unsigned VReg, |
| 112 | StringRef Name) { |
| 113 | std::string LowerName = Name.lower(); |
| 114 | const TargetRegisterClass *RC = MRI.getRegClassOrNull(VReg); |
| 115 | return RC ? MRI.createVirtualRegister(RC, LowerName) |
| 116 | : MRI.createGenericVirtualRegister(MRI.getType(VReg), LowerName); |
| Puyan Lotfi | 028061d | 2019-09-04 21:29:10 +0000 | [diff] [blame] | 117 | } |