From 42382d3e37d3e110cabac876fb081b1358489649 Mon Sep 17 00:00:00 2001 From: David Blaikie Date: Thu, 10 Apr 2014 23:55:11 +0000 Subject: [PATCH] Remove lazy-initialization of section caches in MCContext This seems to have been a cargo-culted habit from the very first such cache which didn't have any specific justification (but might've been a layering constraint at the time). git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@206003 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/MC/MCContext.h | 6 +++- lib/MC/MCContext.cpp | 70 ++++++++++--------------------------- 2 files changed, 23 insertions(+), 53 deletions(-) diff --git a/include/llvm/MC/MCContext.h b/include/llvm/MC/MCContext.h index ea0217693d1..1f90a2273c1 100644 --- a/include/llvm/MC/MCContext.h +++ b/include/llvm/MC/MCContext.h @@ -155,7 +155,11 @@ namespace llvm { /// The Compile Unit ID that we are currently processing. unsigned DwarfCompileUnitID; - void *MachOUniquingMap, *ELFUniquingMap, *COFFUniquingMap; + typedef std::pair SectionGroupPair; + + StringMap MachOUniquingMap; + std::map ELFUniquingMap; + std::map COFFUniquingMap; /// Do automatic reset in destructor bool AutoReset; diff --git a/lib/MC/MCContext.cpp b/lib/MC/MCContext.cpp index 8c2b5095304..36e6b565adb 100644 --- a/lib/MC/MCContext.cpp +++ b/lib/MC/MCContext.cpp @@ -29,12 +29,6 @@ using namespace llvm; -typedef std::pair SectionGroupPair; - -typedef StringMap MachOUniqueMapTy; -typedef std::map ELFUniqueMapTy; -typedef std::map COFFUniqueMapTy; - MCContext::MCContext(const MCAsmInfo *mai, const MCRegisterInfo *mri, const MCObjectFileInfo *mofi, const SourceMgr *mgr, bool DoAutoReset) @@ -49,10 +43,6 @@ MCContext::MCContext(const MCAsmInfo *mai, const MCRegisterInfo *mri, if (EC) CompilationDir.clear(); - MachOUniquingMap = 0; - ELFUniquingMap = 0; - COFFUniquingMap = 0; - SecureLogFile = getenv("AS_SECURE_LOG_FILE"); SecureLog = 0; SecureLogUsed = false; @@ -88,13 +78,9 @@ void MCContext::reset() { DwarfCompileUnitID = 0; CurrentDwarfLoc = MCDwarfLoc(0,0,0,DWARF2_FLAG_IS_STMT,0,0); - // If we have the MachO uniquing map, free it. - delete (MachOUniqueMapTy*)MachOUniquingMap; - delete (ELFUniqueMapTy*)ELFUniquingMap; - delete (COFFUniqueMapTy*)COFFUniquingMap; - MachOUniquingMap = 0; - ELFUniquingMap = 0; - COFFUniquingMap = 0; + MachOUniquingMap.clear(); + ELFUniquingMap.clear(); + COFFUniquingMap.clear(); NextUniqueID = 0; AllowTemporaryLabels = true; @@ -225,11 +211,6 @@ getMachOSection(StringRef Segment, StringRef Section, // may not have the same flags as the requested section, if so this should be // diagnosed by the client as an error. - // Create the map if it doesn't already exist. - if (MachOUniquingMap == 0) - MachOUniquingMap = new MachOUniqueMapTy(); - MachOUniqueMapTy &Map = *(MachOUniqueMapTy*)MachOUniquingMap; - // Form the name to look up. SmallString<64> Name; Name += Segment; @@ -237,7 +218,7 @@ getMachOSection(StringRef Segment, StringRef Section, Name += Section; // Do the lookup, if we have a hit, return it. - const MCSectionMachO *&Entry = Map[Name.str()]; + const MCSectionMachO *&Entry = MachOUniquingMap[Name.str()]; if (Entry) return Entry; // Otherwise, return a new section. @@ -252,31 +233,25 @@ getELFSection(StringRef Section, unsigned Type, unsigned Flags, } void MCContext::renameELFSection(const MCSectionELF *Section, StringRef Name) { - if (ELFUniquingMap == 0) - ELFUniquingMap = new ELFUniqueMapTy(); - ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)ELFUniquingMap; - StringRef GroupName; if (const MCSymbol *Group = Section->getGroup()) GroupName = Group->getName(); - Map.erase(SectionGroupPair(Section->getSectionName(), GroupName)); - auto I = Map.insert(std::make_pair(SectionGroupPair(Name, GroupName), - Section)).first; + ELFUniquingMap.erase(SectionGroupPair(Section->getSectionName(), GroupName)); + auto I = + ELFUniquingMap.insert(std::make_pair(SectionGroupPair(Name, GroupName), + Section)).first; const_cast(Section)->setSectionName(I->first.first); } const MCSectionELF *MCContext:: getELFSection(StringRef Section, unsigned Type, unsigned Flags, SectionKind Kind, unsigned EntrySize, StringRef Group) { - if (ELFUniquingMap == 0) - ELFUniquingMap = new ELFUniqueMapTy(); - ELFUniqueMapTy &Map = *(ELFUniqueMapTy*)ELFUniquingMap; - // Do the lookup, if we have a hit, return it. - std::pair Entry = Map.insert( + auto IterBool = ELFUniquingMap.insert( std::make_pair(SectionGroupPair(Section, Group), (MCSectionELF *)0)); - if (!Entry.second) return Entry.first->second; + auto &Entry = *IterBool.first; + if (!IterBool.second) return Entry.second; // Possibly refine the entry size first. if (!EntrySize) { @@ -288,8 +263,8 @@ getELFSection(StringRef Section, unsigned Type, unsigned Flags, GroupSym = GetOrCreateSymbol(Group); MCSectionELF *Result = new (*this) MCSectionELF( - Entry.first->first.first, Type, Flags, Kind, EntrySize, GroupSym); - Entry.first->second = Result; + Entry.first.first, Type, Flags, Kind, EntrySize, GroupSym); + Entry.second = Result; return Result; } @@ -304,17 +279,12 @@ const MCSectionCOFF * MCContext::getCOFFSection(StringRef Section, unsigned Characteristics, SectionKind Kind, StringRef COMDATSymName, int Selection, const MCSectionCOFF *Assoc) { - if (COFFUniquingMap == 0) - COFFUniquingMap = new COFFUniqueMapTy(); - COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)COFFUniquingMap; - // Do the lookup, if we have a hit, return it. SectionGroupPair P(Section, COMDATSymName); - std::pair Entry = - Map.insert(std::make_pair(P, (MCSectionCOFF *)0)); - COFFUniqueMapTy::iterator Iter = Entry.first; - if (!Entry.second) + auto IterBool = COFFUniquingMap.insert(std::make_pair(P, (MCSectionCOFF *)0)); + auto Iter = IterBool.first; + if (!IterBool.second) return Iter->second; const MCSymbol *COMDATSymbol = NULL; @@ -336,13 +306,9 @@ MCContext::getCOFFSection(StringRef Section, unsigned Characteristics, } const MCSectionCOFF *MCContext::getCOFFSection(StringRef Section) { - if (COFFUniquingMap == 0) - COFFUniquingMap = new COFFUniqueMapTy(); - COFFUniqueMapTy &Map = *(COFFUniqueMapTy*)COFFUniquingMap; - SectionGroupPair P(Section, ""); - COFFUniqueMapTy::iterator Iter = Map.find(P); - if (Iter == Map.end()) + auto Iter = COFFUniquingMap.find(P); + if (Iter == COFFUniquingMap.end()) return 0; return Iter->second; }