From 033805508a673d2dbb566de16cbcc0fc321d296c Mon Sep 17 00:00:00 2001 From: Kazu Hirata Date: Sat, 22 Mar 2025 08:47:12 -0700 Subject: [PATCH] [ExecutionEngine] Avoid repeated hash lookups (NFC) --- .../RuntimeDyld/RuntimeDyldELF.cpp | 24 +++++++++---------- .../RuntimeDyld/Targets/RuntimeDyldMachOARM.h | 8 +++---- .../Targets/RuntimeDyldMachOX86_64.h | 8 +++---- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp b/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp index def117448ab6a..6333bda0270f8 100644 --- a/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp +++ b/llvm/lib/ExecutionEngine/RuntimeDyld/RuntimeDyldELF.cpp @@ -1515,15 +1515,15 @@ void RuntimeDyldELF::resolveAArch64Branch(unsigned SectionID, uint64_t Offset = RelI->getOffset(); unsigned RelType = RelI->getType(); // Look for an existing stub. - StubMap::const_iterator i = Stubs.find(Value); - if (i != Stubs.end()) { + auto [It, Inserted] = Stubs.try_emplace(Value); + if (!Inserted) { resolveRelocation(Section, Offset, - Section.getLoadAddressWithOffset(i->second), RelType, 0); + Section.getLoadAddressWithOffset(It->second), RelType, 0); LLVM_DEBUG(dbgs() << " Stub function found\n"); } else if (!resolveAArch64ShortBranch(SectionID, RelI, Value)) { // Create a new stub function. LLVM_DEBUG(dbgs() << " Create a new stub function\n"); - Stubs[Value] = Section.getStubOffset(); + It->second = Section.getStubOffset(); uint8_t *StubTargetAddr = createStubFunction( Section.getAddressWithOffset(Section.getStubOffset())); @@ -1837,15 +1837,15 @@ RuntimeDyldELF::processRelocationRef( SectionEntry &Section = Sections[SectionID]; // Look up for existing stub. - StubMap::const_iterator i = Stubs.find(Value); - if (i != Stubs.end()) { - RelocationEntry RE(SectionID, Offset, RelType, i->second); + auto [It, Inserted] = Stubs.try_emplace(Value); + if (!Inserted) { + RelocationEntry RE(SectionID, Offset, RelType, It->second); addRelocationForSection(RE, SectionID); LLVM_DEBUG(dbgs() << " Stub function found\n"); } else { // Create a new stub function. LLVM_DEBUG(dbgs() << " Create a new stub function\n"); - Stubs[Value] = Section.getStubOffset(); + It->second = Section.getStubOffset(); unsigned AbiVariant = Obj.getPlatformFlags(); @@ -2075,10 +2075,10 @@ RuntimeDyldELF::processRelocationRef( SectionEntry &Section = Sections[SectionID]; // Look for an existing stub. - StubMap::const_iterator i = Stubs.find(Value); + auto [It, Inserted] = Stubs.try_emplace(Value); uintptr_t StubAddress; - if (i != Stubs.end()) { - StubAddress = uintptr_t(Section.getAddressWithOffset(i->second)); + if (!Inserted) { + StubAddress = uintptr_t(Section.getAddressWithOffset(It->second)); LLVM_DEBUG(dbgs() << " Stub function found\n"); } else { // Create a new stub function. @@ -2089,7 +2089,7 @@ RuntimeDyldELF::processRelocationRef( alignTo(BaseAddress + Section.getStubOffset(), getStubAlignment()); unsigned StubOffset = StubAddress - BaseAddress; - Stubs[Value] = StubOffset; + It->second = StubOffset; createStubFunction((uint8_t *)StubAddress); RelocationEntry RE(SectionID, StubOffset + 8, ELF::R_390_64, Value.Offset); diff --git a/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOARM.h b/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOARM.h index 79b558eb7796d..e0d9f2af988fb 100644 --- a/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOARM.h +++ b/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOARM.h @@ -307,14 +307,14 @@ class RuntimeDyldMachOARM // This is an ARM branch relocation, need to use a stub function. // Look up for existing stub. SectionEntry &Section = Sections[RE.SectionID]; - RuntimeDyldMachO::StubMap::const_iterator i = Stubs.find(Value); + auto [It, Inserted] = Stubs.try_emplace(Value); uint8_t *Addr; - if (i != Stubs.end()) { - Addr = Section.getAddressWithOffset(i->second); + if (!Inserted) { + Addr = Section.getAddressWithOffset(It->second); } else { // Create a new stub function. assert(Section.getStubOffset() % 4 == 0 && "Misaligned stub"); - Stubs[Value] = Section.getStubOffset(); + It->second = Section.getStubOffset(); uint32_t StubOpcode = 0; if (RE.RelType == MachO::ARM_RELOC_BR24) StubOpcode = 0xe51ff004; // ldr pc, [pc, #-4] diff --git a/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOX86_64.h b/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOX86_64.h index bd0d72f9e1172..4b0d4d884607a 100644 --- a/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOX86_64.h +++ b/llvm/lib/ExecutionEngine/RuntimeDyld/Targets/RuntimeDyldMachOX86_64.h @@ -131,12 +131,12 @@ class RuntimeDyldMachOX86_64 assert(RE.IsPCRel); assert(RE.Size == 2); Value.Offset -= RE.Addend; - RuntimeDyldMachO::StubMap::const_iterator i = Stubs.find(Value); + auto [It, Inserted] = Stubs.try_emplace(Value); uint8_t *Addr; - if (i != Stubs.end()) { - Addr = Section.getAddressWithOffset(i->second); + if (!Inserted) { + Addr = Section.getAddressWithOffset(It->second); } else { - Stubs[Value] = Section.getStubOffset(); + It->second = Section.getStubOffset(); uint8_t *GOTEntry = Section.getAddressWithOffset(Section.getStubOffset()); RelocationEntry GOTRE(RE.SectionID, Section.getStubOffset(), MachO::X86_64_RELOC_UNSIGNED, Value.Offset, false,