diff --git a/llvm/lib/CodeGen/MIRVRegNamerUtils.h b/llvm/lib/CodeGen/MIRVRegNamerUtils.h --- a/llvm/lib/CodeGen/MIRVRegNamerUtils.h +++ b/llvm/lib/CodeGen/MIRVRegNamerUtils.h @@ -75,8 +75,7 @@ unsigned createVirtualRegister(unsigned VReg); /// Create a vreg with name and return it. - unsigned createVirtualRegisterWithName(unsigned VReg, - const std::string &Name); + unsigned createVirtualRegisterWithName(unsigned VReg, StringRef Name); /// Linearly traverse the MachineBasicBlock and rename each instruction's /// vreg definition based on the semantics of the instruction. /// Names are as follows bb_hash_[0-9]+ diff --git a/llvm/lib/CodeGen/MIRVRegNamerUtils.cpp b/llvm/lib/CodeGen/MIRVRegNamerUtils.cpp --- a/llvm/lib/CodeGen/MIRVRegNamerUtils.cpp +++ b/llvm/lib/CodeGen/MIRVRegNamerUtils.cpp @@ -13,26 +13,29 @@ #define DEBUG_TYPE "mir-vregnamer-utils" -bool VRegRenamer::doVRegRenaming( - const std::map &VRegRenameMap) { +using VRegRenameMap = std::map; + +bool VRegRenamer::doVRegRenaming(const VRegRenameMap &VRM) { bool Changed = false; - for (auto I = VRegRenameMap.begin(), E = VRegRenameMap.end(); I != E; ++I) { - auto VReg = I->first; - auto Rename = I->second; + for (const auto &E : VRM) { + unsigned VReg = E.first; + unsigned Rename = E.second; + assert(Register::isVirtualRegister(VReg) && "Expected Virtual Registers"); + if (MRI.reg_empty(VReg)) + continue; + Changed = true; + MRI.replaceRegWith(VReg, Rename); + // TODO: Not sure if this makes sense anymore. Do we really want to drop + // semantic information like def/kill? Remove the code below in a followup + // commit. std::vector RenameMOs; - for (auto &MO : MRI.reg_operands(VReg)) { + for (auto &MO : MRI.reg_operands(VReg)) RenameMOs.push_back(&MO); - } - - for (auto *MO : RenameMOs) { - Changed = true; - MO->setReg(Rename); - + for (auto *MO : RenameMOs) if (!MO->isDef()) MO->setIsKill(false); - } } return Changed; @@ -40,32 +43,25 @@ std::map VRegRenamer::getVRegRenameMap(const std::vector &VRegs) { - std::map VRegRenameMap; std::map VRegNameCollisionMap; auto GetUniqueVRegName = [&VRegNameCollisionMap](const NamedVReg &Reg) -> std::string { auto It = VRegNameCollisionMap.find(Reg.getName()); - unsigned Counter = 0; - if (It != VRegNameCollisionMap.end()) { - Counter = It->second; - } + unsigned Counter = (It != VRegNameCollisionMap.end()) ? It->second : 0; ++Counter; VRegNameCollisionMap[Reg.getName()] = Counter; return Reg.getName() + "__" + std::to_string(Counter); }; - for (auto &Vreg : VRegs) { - auto Reg = Vreg.getReg(); - assert(Register::isVirtualRegister(Reg) && - "Expecting Virtual Registers Only"); - auto NewNameForReg = GetUniqueVRegName(Vreg); - auto Rename = createVirtualRegisterWithName(Reg, NewNameForReg); - - VRegRenameMap.insert(std::pair(Reg, Rename)); + VRegRenameMap VRM; + for (const auto &Vreg : VRegs) { + unsigned Reg = Vreg.getReg(); + assert(Register::isVirtualRegister(Reg) && "Expected Virtual Registers"); + VRM[Reg] = createVirtualRegisterWithName(Reg, GetUniqueVRegName(Vreg)); } - return VRegRenameMap; + return VRM; } std::string VRegRenamer::getInstructionOpcodeHash(MachineInstr &MI) { @@ -88,25 +84,23 @@ // (Opcodes,other registers etc) that this will likely not be a problem. return 0; }; - SmallVector MIOperands; - MIOperands.push_back(MI.getOpcode()); - for (auto &Op : MI.uses()) { - MIOperands.push_back(HashOperand(Op)); - } + + SmallVector MIOperands = {MI.getOpcode()}; + llvm::transform(MI.uses(), std::back_inserter(MIOperands), HashOperand); + auto HashMI = hash_combine_range(MIOperands.begin(), MIOperands.end()); return std::to_string(HashMI).substr(0, 5); } unsigned VRegRenamer::createVirtualRegister(unsigned VReg) { - return createVirtualRegisterWithName( - VReg, getInstructionOpcodeHash(*MRI.getVRegDef(VReg))); + std::string Name = getInstructionOpcodeHash(*MRI.getVRegDef(VReg)); + return createVirtualRegisterWithName(VReg, StringRef(Name)); } bool VRegRenamer::renameInstsInMBB(MachineBasicBlock *MBB) { std::vector VRegs; std::string Prefix = "bb" + std::to_string(getCurrentBBNumber()) + "_"; - for (auto &MII : *MBB) { - MachineInstr &Candidate = MII; + for (MachineInstr &Candidate : *MBB) { // Don't rename stores/branches. if (Candidate.mayStore() || Candidate.isBranch()) continue; @@ -121,13 +115,7 @@ NamedVReg(MO.getReg(), Prefix + getInstructionOpcodeHash(Candidate))); } - // If we have populated no vregs to rename then bail. - // The rest of this function does the vreg remaping. - if (VRegs.size() == 0) - return false; - - auto VRegRenameMap = getVRegRenameMap(VRegs); - return doVRegRenaming(VRegRenameMap); + return VRegs.size() ? doVRegRenaming(getVRegRenameMap(VRegs)) : false; } bool VRegRenamer::renameVRegs(MachineBasicBlock *MBB, unsigned BBNum) { @@ -135,11 +123,8 @@ return renameInstsInMBB(MBB); } -unsigned VRegRenamer::createVirtualRegisterWithName(unsigned VReg, - const std::string &Name) { - std::string Temp(Name); - std::transform(Temp.begin(), Temp.end(), Temp.begin(), ::tolower); +unsigned VRegRenamer::createVirtualRegisterWithName(unsigned VReg, StringRef Name) { if (auto RC = MRI.getRegClassOrNull(VReg)) - return MRI.createVirtualRegister(RC, Temp); + return MRI.createVirtualRegister(RC, Name.lower()); return MRI.createGenericVirtualRegister(MRI.getType(VReg), Name); }