//===----------------------------------------------------------------------===//
/// createInternalizePass - This pass loops over all of the functions in the
-/// input module, looking for a main function. If a list of symbols is
-/// specified with the -internalize-public-api-* command line options, those
-/// symbols are internalized. Otherwise if InternalizeEverything is set and
-/// the main function is found, all other globals are marked as internal.
+/// input module, internalizing all globals (functions and variables) not part
+/// of the api. If a list of symbols is specified with the
+/// -internalize-public-api-* command line options, those symbols are not
+/// internalized and all others are. Otherwise if AllButMain is set and the
+/// main function is found, all other globals are marked as internal.
///
ModulePass *createInternalizePass(bool InternalizeEverything);
+
+/// createInternalizePass - This pass loops over all of the functions in the
+/// input module, internalizing all globals (functions and variables) not in the
+/// given exportList.
ModulePass *createInternalizePass(const std::vector<const char *> &exportList);
//===----------------------------------------------------------------------===//
namespace {
class VISIBILITY_HIDDEN InternalizePass : public ModulePass {
std::set<std::string> ExternalNames;
- bool DontInternalize;
+ /// If no api symbols were specified and a main function is defined,
+ /// assume the main function is the only API
+ bool AllButMain;
public:
static char ID; // Pass identification, replacement for typeid
explicit InternalizePass(bool InternalizeEverything = true);
static RegisterPass<InternalizePass>
X("internalize", "Internalize Global Symbols");
-InternalizePass::InternalizePass(bool InternalizeEverything)
- : ModulePass((intptr_t)&ID), DontInternalize(false){
- if (!APIFile.empty()) // If a filename is specified, use it
+InternalizePass::InternalizePass(bool AllButMain)
+ : ModulePass((intptr_t)&ID), AllButMain(AllButMain){
+ if (!APIFile.empty()) // If a filename is specified, use it.
LoadFile(APIFile.c_str());
- else if (!APIList.empty()) // Else, if a list is specified, use it.
+ if (!APIList.empty()) // If a list is specified, use it as well.
ExternalNames.insert(APIList.begin(), APIList.end());
- else if (!InternalizeEverything)
- // Finally, if we're allowed to, internalize all but main.
- DontInternalize = true;
}
InternalizePass::InternalizePass(const std::vector<const char *>&exportList)
- : ModulePass((intptr_t)&ID), DontInternalize(false){
+ : ModulePass((intptr_t)&ID), AllButMain(false){
for(std::vector<const char *>::const_iterator itr = exportList.begin();
itr != exportList.end(); itr++) {
ExternalNames.insert(*itr);
// Load the APIFile...
std::ifstream In(Filename);
if (!In.good()) {
- cerr << "WARNING: Internalize couldn't load file '" << Filename << "'!\n";
- return; // Do not internalize anything...
+ cerr << "WARNING: Internalize couldn't load file '" << Filename
+ << "'! Continuing as if it's empty.\n";
+ return; // Just continue as if the file were empty
}
while (In) {
std::string Symbol;
}
bool InternalizePass::runOnModule(Module &M) {
- if (DontInternalize) return false;
-
- // If no list or file of symbols was specified, check to see if there is a
- // "main" symbol defined in the module. If so, use it, otherwise do not
- // internalize the module, it must be a library or something.
- //
if (ExternalNames.empty()) {
+ // Return if we're not in 'all but main' mode and have no external api
+ if (!AllButMain)
+ return false;
+ // If no list or file of symbols was specified, check to see if there is a
+ // "main" symbol defined in the module. If so, use it, otherwise do not
+ // internalize the module, it must be a library or something.
+ //
Function *MainFunc = M.getFunction("main");
if (MainFunc == 0 || MainFunc->isDeclaration())
return false; // No main found, must be a library...
bool Changed = false;
- // Found a main function, mark all functions not named main as internal.
+ // Mark all functions not in the api 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.insert("llvm.noinline");
ExternalNames.insert("llvm.global.annotations");
- // Mark all global variables with initializers as internal as well.
+ // Mark all global variables with initializers that are not in the api as
+ // internal as well.
for (Module::global_iterator I = M.global_begin(), E = M.global_end();
I != E; ++I)
if (!I->isDeclaration() && !I->hasInternalLinkage() &&
--- /dev/null
+; No arguments means internalize all but main
+; RUN: llvm-as < %s | opt -internalize | llvm-dis | grep internal | count 4
+; Internalize all but foo and j
+; RUN: llvm-as < %s | opt -internalize -internalize-public-api-list foo -internalize-public-api-list j | llvm-dis | grep internal | count 3
+; Non existent files should be treated as if they were empty (so internalize all but main)
+; RUN: llvm-as < %s | opt -internalize -internalize-public-api-file /nonexistent/file | llvm-dis | grep internal | count 4
+; RUN: llvm-as < %s | opt -internalize -internalize-public-api-list bar -internalize-public-api-list foo -internalize-public-api-file /nonexistent/file | llvm-dis | grep internal | count 3
+; -file and -list options should be merged, the .apifile contains foo and j
+; RUN: llvm-as < %s | opt -internalize -internalize-public-api-list bar -internalize-public-api-file %s.apifile | llvm-dis | grep internal | count 2
+
+@i = weak global i32 0 ; <i32*> [#uses=0]
+@j = weak global i32 0 ; <i32*> [#uses=0]
+
+define void @main(...) {
+entry:
+ ret void
+}
+
+define void @foo(...) {
+entry:
+ ret void
+}
+
+define void @bar(...) {
+entry:
+ ret void
+}