1 //===-- Internalize.cpp - Mark functions internal -------------------------===// 2 // 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. 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/Transforms/IPO.h" 10 #include "llvm/Pass.h" 11 #include "llvm/Module.h" 12 #include "Support/Statistic.h" 13 14 namespace { 15 Statistic<> NumFunctions("internalize", "Number of functions internalized"); 16 Statistic<> NumGlobals ("internalize", "Number of global vars internalized"); 17 18 class InternalizePass : public Pass { 19 virtual bool run(Module &M) { 20 Function *MainFunc = M.getMainFunction(); 21 22 if (MainFunc == 0 || MainFunc->isExternal()) 23 return false; // No main found, must be a library... 24 25 bool Changed = false; 26 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->setInternalLinkage(true); 33 Changed = true; 34 ++NumFunctions; 35 DEBUG(std::cerr << "Internalizing func " << I->getName() << "\n"); 36 } 37 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->setInternalLinkage(true); 42 Changed = true; 43 ++NumGlobals; 44 DEBUG(std::cerr << "Internalizing gvar " << I->getName() << "\n"); 45 } 46 47 return Changed; 48 } 49 }; 50 51 RegisterOpt<InternalizePass> X("internalize", "Internalize Functions"); 52 } // end anonymous namespace 53 54 Pass *createInternalizePass() { 55 return new InternalizePass(); 56 } 57