1 //===-- Internalize.cpp - Mark functions internal -------------------------===//
3 // This pass loops over all of the functions in the input module, looking for a
4 // main function. If a main function is found, all other functions and all
5 // global variables with initializers are marked as internal.
7 //===----------------------------------------------------------------------===//
9 #include "llvm/Transforms/IPO.h"
10 #include "llvm/Pass.h"
11 #include "llvm/Module.h"
12 #include "Support/Statistic.h"
15 Statistic<> NumFunctions("internalize", "Number of functions internalized");
16 Statistic<> NumGlobals ("internalize", "Number of global vars internalized");
18 class InternalizePass : public Pass {
19 virtual bool run(Module &M) {
20 Function *MainFunc = M.getMainFunction();
22 if (MainFunc == 0 || MainFunc->isExternal())
23 return false; // No main found, must be a library...
27 // Found a main function, mark all functions not named main as internal.
28 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
29 if (&*I != MainFunc && // Leave the main function external
30 !I->isExternal() && // Function must be defined here
31 !I->hasInternalLinkage()) { // Can't already have internal linkage
32 I->setLinkage(GlobalValue::InternalLinkage);
35 DEBUG(std::cerr << "Internalizing func " << I->getName() << "\n");
38 // Mark all global variables with initializers as internal as well...
39 for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
40 if (!I->isExternal() && I->hasExternalLinkage()) {
41 I->setLinkage(GlobalValue::InternalLinkage);
44 DEBUG(std::cerr << "Internalizing gvar " << I->getName() << "\n");
51 RegisterOpt<InternalizePass> X("internalize", "Internalize Functions");
52 } // end anonymous namespace
54 Pass *createInternalizePass() {
55 return new InternalizePass();