From c003628a612d3687fb77088a5894314210a65385 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Wed, 4 Aug 2004 07:05:54 +0000 Subject: [PATCH] Factor some code out, no substantial change. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@15466 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Linker/LinkModules.cpp | 44 ++++++++++++++++++++++---------------- lib/VMCore/Linker.cpp | 44 ++++++++++++++++++++++---------------- 2 files changed, 52 insertions(+), 36 deletions(-) diff --git a/lib/Linker/LinkModules.cpp b/lib/Linker/LinkModules.cpp index bc1a6467e5c..cda5d7116a5 100644 --- a/lib/Linker/LinkModules.cpp +++ b/lib/Linker/LinkModules.cpp @@ -378,7 +378,7 @@ static GlobalValue *FindGlobalNamed(const std::string &Name, const Type *Ty, SymbolTable *ST) { // See if an exact match exists in the symbol table... if (Value *V = ST->lookup(Ty, Name)) return cast(V); - + // It doesn't exist exactly, scan through all of the type planes in the symbol // table, checking each of them for a type-compatible version. // @@ -403,6 +403,27 @@ static GlobalValue *FindGlobalNamed(const std::string &Name, const Type *Ty, return 0; // Otherwise, nothing could be found. } +/// ForceRenaming - The LLVM SymbolTable class autorenames globals that conflict +/// in the symbol table. This is good for all clients except for us. Go +/// through the trouble to force this back. +static void ForceRenaming(GlobalValue *GV, const std::string &Name) { + assert(GV->getName() != Name && "Can't force rename to self"); + SymbolTable &ST = GV->getParent()->getSymbolTable(); + + // If there is a conflict, rename the conflict. + Value *ConflictVal = ST.lookup(GV->getType(), Name); + assert(ConflictVal&&"Why do we have to force rename if there is no conflic?"); + GlobalValue *ConflictGV = cast(ConflictVal); + assert(ConflictGV->hasInternalLinkage() && + "Not conflicting with a static global, should link instead!"); + + ConflictGV->setName(""); // Eliminate the conflict + GV->setName(Name); // Force the name back + ConflictGV->setName(Name); // This will cause ConflictGV to get renamed + assert(GV->getName() == Name() && ConflictGV->getName() != Name && + "ForceRenaming didn't work"); +} + // LinkGlobals - Loop through the global variables in the src module and merge // them into the dest module. @@ -448,15 +469,8 @@ static bool LinkGlobals(Module *Dest, const Module *Src, // If the LLVM runtime renamed the global, but it is an externally visible // symbol, DGV must be an existing global with internal linkage. Rename // it. - if (NewDGV->getName() != SGV->getName() && !NewDGV->hasInternalLinkage()){ - assert(DGV && DGV->getName() == SGV->getName() && - DGV->hasInternalLinkage()); - DGV->setName(""); - NewDGV->setName(SGV->getName()); // Force the name back - DGV->setName(SGV->getName()); // This will cause a renaming - assert(NewDGV->getName() == SGV->getName() && - DGV->getName() != SGV->getName()); - } + if (NewDGV->getName() != SGV->getName() && !NewDGV->hasInternalLinkage()) + ForceRenaming(NewDGV, SGV->getName()); // Make sure to remember this mapping... ValueMap.insert(std::make_pair(SGV, NewDGV)); @@ -622,14 +636,8 @@ static bool LinkFunctionProtos(Module *Dest, const Module *Src, // If the LLVM runtime renamed the function, but it is an externally // visible symbol, DF must be an existing function with internal linkage. // Rename it. - if (NewDF->getName() != SF->getName() && !NewDF->hasInternalLinkage()) { - assert(DF && DF->getName() == SF->getName() &&DF->hasInternalLinkage()); - DF->setName(""); - NewDF->setName(SF->getName()); // Force the name back - DF->setName(SF->getName()); // This will cause a renaming - assert(NewDF->getName() == SF->getName() && - DF->getName() != SF->getName()); - } + if (NewDF->getName() != SF->getName() && !NewDF->hasInternalLinkage()) + ForceRenaming(DF, SF->getName()); // ... and remember this mapping... ValueMap.insert(std::make_pair(SF, NewDF)); diff --git a/lib/VMCore/Linker.cpp b/lib/VMCore/Linker.cpp index bc1a6467e5c..cda5d7116a5 100644 --- a/lib/VMCore/Linker.cpp +++ b/lib/VMCore/Linker.cpp @@ -378,7 +378,7 @@ static GlobalValue *FindGlobalNamed(const std::string &Name, const Type *Ty, SymbolTable *ST) { // See if an exact match exists in the symbol table... if (Value *V = ST->lookup(Ty, Name)) return cast(V); - + // It doesn't exist exactly, scan through all of the type planes in the symbol // table, checking each of them for a type-compatible version. // @@ -403,6 +403,27 @@ static GlobalValue *FindGlobalNamed(const std::string &Name, const Type *Ty, return 0; // Otherwise, nothing could be found. } +/// ForceRenaming - The LLVM SymbolTable class autorenames globals that conflict +/// in the symbol table. This is good for all clients except for us. Go +/// through the trouble to force this back. +static void ForceRenaming(GlobalValue *GV, const std::string &Name) { + assert(GV->getName() != Name && "Can't force rename to self"); + SymbolTable &ST = GV->getParent()->getSymbolTable(); + + // If there is a conflict, rename the conflict. + Value *ConflictVal = ST.lookup(GV->getType(), Name); + assert(ConflictVal&&"Why do we have to force rename if there is no conflic?"); + GlobalValue *ConflictGV = cast(ConflictVal); + assert(ConflictGV->hasInternalLinkage() && + "Not conflicting with a static global, should link instead!"); + + ConflictGV->setName(""); // Eliminate the conflict + GV->setName(Name); // Force the name back + ConflictGV->setName(Name); // This will cause ConflictGV to get renamed + assert(GV->getName() == Name() && ConflictGV->getName() != Name && + "ForceRenaming didn't work"); +} + // LinkGlobals - Loop through the global variables in the src module and merge // them into the dest module. @@ -448,15 +469,8 @@ static bool LinkGlobals(Module *Dest, const Module *Src, // If the LLVM runtime renamed the global, but it is an externally visible // symbol, DGV must be an existing global with internal linkage. Rename // it. - if (NewDGV->getName() != SGV->getName() && !NewDGV->hasInternalLinkage()){ - assert(DGV && DGV->getName() == SGV->getName() && - DGV->hasInternalLinkage()); - DGV->setName(""); - NewDGV->setName(SGV->getName()); // Force the name back - DGV->setName(SGV->getName()); // This will cause a renaming - assert(NewDGV->getName() == SGV->getName() && - DGV->getName() != SGV->getName()); - } + if (NewDGV->getName() != SGV->getName() && !NewDGV->hasInternalLinkage()) + ForceRenaming(NewDGV, SGV->getName()); // Make sure to remember this mapping... ValueMap.insert(std::make_pair(SGV, NewDGV)); @@ -622,14 +636,8 @@ static bool LinkFunctionProtos(Module *Dest, const Module *Src, // If the LLVM runtime renamed the function, but it is an externally // visible symbol, DF must be an existing function with internal linkage. // Rename it. - if (NewDF->getName() != SF->getName() && !NewDF->hasInternalLinkage()) { - assert(DF && DF->getName() == SF->getName() &&DF->hasInternalLinkage()); - DF->setName(""); - NewDF->setName(SF->getName()); // Force the name back - DF->setName(SF->getName()); // This will cause a renaming - assert(NewDF->getName() == SF->getName() && - DF->getName() != SF->getName()); - } + if (NewDF->getName() != SF->getName() && !NewDF->hasInternalLinkage()) + ForceRenaming(DF, SF->getName()); // ... and remember this mapping... ValueMap.insert(std::make_pair(SF, NewDF));