diff --git a/lib/Support/Mangler.cpp b/lib/Support/Mangler.cpp index 336066ed869..0982efd8cd9 100644 --- a/lib/Support/Mangler.cpp +++ b/lib/Support/Mangler.cpp @@ -80,22 +80,37 @@ std::string Mangler::getValueName(const Value *V) { return name; } +void Mangler::InsertName(GlobalValue *GV, + std::map &Names) { + if (!GV->hasName()) { // We must mangle unnamed globals. + MangledGlobals.insert(GV); + return; + } + + // Figure out if this is already used. + GlobalValue *&ExistingValue = Names[GV->getName()]; + if (!ExistingValue) { + ExistingValue = GV; + } else { + // If GV is external but the existing one is static, mangle the existing one + if (GV->hasExternalLinkage() && !ExistingValue->hasExternalLinkage()) { + MangledGlobals.insert(ExistingValue); + ExistingValue = GV; + } else { + // Otherwise, mangle GV + MangledGlobals.insert(GV); + } + } +} + + Mangler::Mangler(Module &m, bool addUnderscorePrefix) : M(m), AddUnderscorePrefix(addUnderscorePrefix), Count(0) { // Calculate which global values have names that will collide when we throw // away type information. - std::set FoundNames; + std::map Names; for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) - if (I->hasName()) // If the global has a name... - if (FoundNames.count(I->getName())) // And the name is already used - MangledGlobals.insert(I); // Mangle the name - else - FoundNames.insert(I->getName()); // Otherwise, keep track of name - + InsertName(I, Names); for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I) - if (I->hasName()) // If the global has a name... - if (FoundNames.count(I->getName())) // And the name is already used - MangledGlobals.insert(I); // Mangle the name - else - FoundNames.insert(I->getName()); // Otherwise, keep track of name + InsertName(I, Names); } diff --git a/lib/VMCore/Mangler.cpp b/lib/VMCore/Mangler.cpp index 336066ed869..0982efd8cd9 100644 --- a/lib/VMCore/Mangler.cpp +++ b/lib/VMCore/Mangler.cpp @@ -80,22 +80,37 @@ std::string Mangler::getValueName(const Value *V) { return name; } +void Mangler::InsertName(GlobalValue *GV, + std::map &Names) { + if (!GV->hasName()) { // We must mangle unnamed globals. + MangledGlobals.insert(GV); + return; + } + + // Figure out if this is already used. + GlobalValue *&ExistingValue = Names[GV->getName()]; + if (!ExistingValue) { + ExistingValue = GV; + } else { + // If GV is external but the existing one is static, mangle the existing one + if (GV->hasExternalLinkage() && !ExistingValue->hasExternalLinkage()) { + MangledGlobals.insert(ExistingValue); + ExistingValue = GV; + } else { + // Otherwise, mangle GV + MangledGlobals.insert(GV); + } + } +} + + Mangler::Mangler(Module &m, bool addUnderscorePrefix) : M(m), AddUnderscorePrefix(addUnderscorePrefix), Count(0) { // Calculate which global values have names that will collide when we throw // away type information. - std::set FoundNames; + std::map Names; for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) - if (I->hasName()) // If the global has a name... - if (FoundNames.count(I->getName())) // And the name is already used - MangledGlobals.insert(I); // Mangle the name - else - FoundNames.insert(I->getName()); // Otherwise, keep track of name - + InsertName(I, Names); for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I) - if (I->hasName()) // If the global has a name... - if (FoundNames.count(I->getName())) // And the name is already used - MangledGlobals.insert(I); // Mangle the name - else - FoundNames.insert(I->getName()); // Otherwise, keep track of name + InsertName(I, Names); }