-
- // Found a main function, mark all functions not named main as internal.
- for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
- if (!I->isDeclaration() && // Function must be defined here
- !I->hasInternalLinkage() && // Can't already have internal linkage
- !ExternalNames.count(I->getName())) {// Not marked to keep external?
- I->setLinkage(GlobalValue::InternalLinkage);
- Changed = true;
- ++NumFunctions;
- DOUT << "Internalizing func " << I->getName() << "\n";
- }
-
+
+ SmallPtrSet<GlobalValue *, 8> Used;
+ collectUsedGlobalVariables(M, Used, false);
+
+ // We must assume that globals in llvm.used have a reference that not even
+ // the linker can see, so we don't internalize them.
+ // For llvm.compiler.used the situation is a bit fuzzy. The assembler and
+ // linker can drop those symbols. If this pass is running as part of LTO,
+ // one might think that it could just drop llvm.compiler.used. The problem
+ // is that even in LTO llvm doesn't see every reference. For example,
+ // we don't see references from function local inline assembly. To be
+ // conservative, we internalize symbols in llvm.compiler.used, but we
+ // keep llvm.compiler.used so that the symbol is not deleted by llvm.
+ for (GlobalValue *V : Used) {
+ ExternalNames.insert(V->getName());
+ }
+
+ // Mark all functions not in the api as internal.
+ for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
+ if (!shouldInternalize(*I, ExternalNames))
+ continue;
+
+ I->setVisibility(GlobalValue::DefaultVisibility);
+ I->setLinkage(GlobalValue::InternalLinkage);
+
+ if (ExternalNode)
+ // Remove a callgraph edge from the external node to this function.
+ ExternalNode->removeOneAbstractEdgeTo((*CG)[I]);
+
+ Changed = true;
+ ++NumFunctions;
+ DEBUG(dbgs() << "Internalizing func " << I->getName() << "\n");
+ }
+