1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-19 11:02:59 +02:00

Add support to the linker to lazily link in functions. This change only links functions marked with specific linkage (internal, private, linker_private, linker_private_weak, linker_private_weak_def_auto, linkonce, linkonce_odr, and available_externally) if they have uses in the destination module. Instead of automatically linking, these functions are placed onto a worklist to be processed in the final stage of linking. We iterate over the list and if any functions on the list have uses in the destination module, we link them in and repeat the process until no changes in the state (uses) has changed. This means that any functions in the LazilyLink worklist that have a use in the destination module will be linked in and none that don't.

llvm-svn: 143524
This commit is contained in:
Tanya Lattner 2011-11-02 00:24:56 +00:00
parent f72870096b
commit 81f9010d3a

View File

@ -341,6 +341,9 @@ namespace {
// Set of items not to link in from source. // Set of items not to link in from source.
SmallPtrSet<const Value*, 16> DoNotLinkFromSource; SmallPtrSet<const Value*, 16> DoNotLinkFromSource;
// Vector of functions to lazily link in.
std::vector<Function*> LazilyLinkFunctions;
public: public:
std::string ErrorMsg; std::string ErrorMsg;
@ -708,6 +711,13 @@ bool ModuleLinker::linkFunctionProto(Function *SF) {
// Any uses of DF need to change to NewDF, with cast. // Any uses of DF need to change to NewDF, with cast.
DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDF, DGV->getType())); DGV->replaceAllUsesWith(ConstantExpr::getBitCast(NewDF, DGV->getType()));
DGV->eraseFromParent(); DGV->eraseFromParent();
} else {
// Internal, LO_ODR, or LO linkage - stick in set to ignore and lazily link.
if (SF->hasLocalLinkage() || SF->hasLinkOnceLinkage() ||
SF->hasAvailableExternallyLinkage()) {
DoNotLinkFromSource.insert(SF);
LazilyLinkFunctions.push_back(SF);
}
} }
ValueMap[SF] = NewDF; ValueMap[SF] = NewDF;
@ -974,6 +984,54 @@ bool ModuleLinker::run() {
// are properly remapped. // are properly remapped.
linkNamedMDNodes(); linkNamedMDNodes();
// Process vector of lazily linked in functions.
bool LinkedInAnyFunctions;
do {
LinkedInAnyFunctions = false;
for(std::vector<Function*>::iterator I = LazilyLinkFunctions.begin(),
E = LazilyLinkFunctions.end(); I != E; ++I) {
if (!*I)
continue;
Function *SF = *I;
Function *DF = cast<Function>(ValueMap[SF]);
if (!DF->use_empty()) {
// Materialize if necessary.
if (SF->isDeclaration()) {
if (!SF->isMaterializable())
continue;
if (SF->Materialize(&ErrorMsg))
return true;
}
// Link in function body.
linkFunctionBody(DF, SF);
// "Remove" from vector by setting the element to 0.
*I = 0;
// Set flag to indicate we may have more functions to lazily link in
// since we linked in a function.
LinkedInAnyFunctions = true;
}
}
} while (LinkedInAnyFunctions);
// Remove any prototypes of functions that were not actually linked in.
for(std::vector<Function*>::iterator I = LazilyLinkFunctions.begin(),
E = LazilyLinkFunctions.end(); I != E; ++I) {
if (!*I)
continue;
Function *SF = *I;
Function *DF = cast<Function>(ValueMap[SF]);
if (DF->use_empty())
DF->eraseFromParent();
}
// Now that all of the types from the source are used, resolve any structs // Now that all of the types from the source are used, resolve any structs
// copied over to the dest that didn't exist there. // copied over to the dest that didn't exist there.
TypeMap.linkDefinedTypeBodies(); TypeMap.linkDefinedTypeBodies();