Centralize the handling of unique ids for temporary labels.

Before this patch code wanting to create temporary labels for a given entity
(function, cu, exception range, etc) had to keep its own counter to have stable
symbol names.

createTempSymbol would still add a suffix to make sure a new symbol was always
returned, but it kept a single counter. Because of that, if we were to use
just createTempSymbol("cu_begin"), the label could change from cu_begin42 to
cu_begin43 because some other code started using temporary labels.

Simplify this by just keeping one counter per prefix and removing the various
specialized counters.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@232535 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Rafael Espindola
2015-03-17 20:07:06 +00:00
parent 93920f0bcd
commit b415e6b2f2
14 changed files with 64 additions and 87 deletions
+31 -32
View File
@@ -34,7 +34,7 @@ MCContext::MCContext(const MCAsmInfo *mai, const MCRegisterInfo *mri,
const MCObjectFileInfo *mofi, const SourceMgr *mgr,
bool DoAutoReset)
: SrcMgr(mgr), MAI(mai), MRI(mri), MOFI(mofi), Allocator(),
Symbols(Allocator), UsedNames(Allocator), NextUniqueID(0),
Symbols(Allocator), UsedNames(Allocator),
CurrentDwarfLoc(0, 0, 0, DWARF2_FLAG_IS_STMT, 0, 0), DwarfLocSeen(false),
GenDwarfForAssembly(false), GenDwarfFileNumber(0), DwarfVersion(4),
AllowTemporaryLabels(true), DwarfCompileUnitID(0),
@@ -87,7 +87,7 @@ void MCContext::reset() {
ELFUniquingMap.clear();
COFFUniquingMap.clear();
NextUniqueID = 0;
NextID.clear();
AllowTemporaryLabels = true;
DwarfLocSeen = false;
GenDwarfForAssembly = false;
@@ -106,7 +106,7 @@ MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name) {
MCSymbol *&Sym = Symbols[NameRef];
if (!Sym)
Sym = CreateSymbol(NameRef);
Sym = CreateSymbol(NameRef, false);
return Sym;
}
@@ -139,49 +139,48 @@ MCSymbol *MCContext::getOrCreateFrameAllocSymbol(StringRef FuncName,
"$frame_escape_" + Twine(Idx));
}
MCSymbol *MCContext::CreateSymbol(StringRef Name) {
MCSymbol *MCContext::CreateSymbol(StringRef Name, bool AlwaysAddSuffix) {
// Determine whether this is an assembler temporary or normal label, if used.
bool isTemporary = false;
bool IsTemporary = false;
if (AllowTemporaryLabels)
isTemporary = Name.startswith(MAI->getPrivateGlobalPrefix());
IsTemporary = Name.startswith(MAI->getPrivateGlobalPrefix());
auto NameEntry = UsedNames.insert(std::make_pair(Name, true));
if (!NameEntry.second) {
assert(isTemporary && "Cannot rename non-temporary symbols");
SmallString<128> NewName = Name;
do {
SmallString<128> NewName = Name;
bool AddSuffix = AlwaysAddSuffix;
unsigned &NextUniqueID = NextID[Name];
for (;;) {
if (AddSuffix) {
NewName.resize(Name.size());
raw_svector_ostream(NewName) << NextUniqueID++;
NameEntry = UsedNames.insert(std::make_pair(NewName, true));
} while (!NameEntry.second);
}
auto NameEntry = UsedNames.insert(std::make_pair(NewName, true));
if (NameEntry.second) {
// Ok, we found a name. Have the MCSymbol object itself refer to the copy
// of the string that is embedded in the UsedNames entry.
MCSymbol *Result =
new (*this) MCSymbol(NameEntry.first->getKey(), IsTemporary);
return Result;
}
assert(IsTemporary && "Cannot rename non-temporary symbols");
AddSuffix = true;
}
// Ok, the entry doesn't already exist. Have the MCSymbol object itself refer
// to the copy of the string that is embedded in the UsedNames entry.
MCSymbol *Result =
new (*this) MCSymbol(NameEntry.first->getKey(), isTemporary);
return Result;
llvm_unreachable("Infinite loop");
}
MCSymbol *MCContext::createTempSymbol(const Twine &Name) {
MCSymbol *MCContext::createTempSymbol(const Twine &Name, bool AlwaysAddSuffix) {
SmallString<128> NameSV;
raw_svector_ostream(NameSV) << MAI->getPrivateGlobalPrefix() << Name;
return CreateSymbol(NameSV);
return CreateSymbol(NameSV, AlwaysAddSuffix);
}
MCSymbol *MCContext::CreateLinkerPrivateTempSymbol() {
SmallString<128> NameSV;
raw_svector_ostream(NameSV)
<< MAI->getLinkerPrivateGlobalPrefix() << "tmp" << NextUniqueID++;
return CreateSymbol(NameSV);
raw_svector_ostream(NameSV) << MAI->getLinkerPrivateGlobalPrefix() << "tmp";
return CreateSymbol(NameSV, true);
}
MCSymbol *MCContext::CreateTempSymbol() {
SmallString<128> NameSV;
raw_svector_ostream(NameSV)
<< MAI->getPrivateGlobalPrefix() << "tmp" << NextUniqueID++;
return CreateSymbol(NameSV);
return createTempSymbol("tmp", true);
}
unsigned MCContext::NextInstance(unsigned LocalLabelVal) {
@@ -251,7 +250,7 @@ MCContext::getMachOSection(StringRef Segment, StringRef Section,
MCSymbol *Begin = nullptr;
if (BeginSymName)
Begin = createTempSymbol(BeginSymName);
Begin = createTempSymbol(BeginSymName, false);
// Otherwise, return a new section.
return Entry = new (*this) MCSectionMachO(Segment, Section, TypeAndAttributes,
@@ -302,7 +301,7 @@ const MCSectionELF *MCContext::getELFSection(StringRef Section, unsigned Type,
MCSymbol *Begin = nullptr;
if (BeginSymName)
Begin = createTempSymbol(BeginSymName);
Begin = createTempSymbol(BeginSymName, false);
MCSectionELF *Result = new (*this) MCSectionELF(
CachedName, Type, Flags, Kind, EntrySize, GroupSym, Unique, Begin);
@@ -344,7 +343,7 @@ MCContext::getCOFFSection(StringRef Section, unsigned Characteristics,
MCSymbol *Begin = nullptr;
if (BeginSymName)
Begin = createTempSymbol(BeginSymName);
Begin = createTempSymbol(BeginSymName, false);
StringRef CachedName = std::get<0>(Iter->first);
MCSectionCOFF *Result = new (*this) MCSectionCOFF(