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 bool FoundMain = false; // Look for a function named main...
21 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
22 if (I->getName() == "main" && !I->isExternal() &&
23 I->hasExternalLinkage()) {
28 if (!FoundMain) return false; // No main found, must be a library...
32 // Found a main function, mark all functions not named main as internal.
33 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
34 if (I->getName() != "main" && // Leave the main function external
35 !I->isExternal() && // Function must be defined here
36 !I->hasInternalLinkage()) { // Can't already have internal linkage
37 I->setInternalLinkage(true);
40 DEBUG(std::cerr << "Internalizing func " << I->getName() << "\n");
43 // Mark all global variables with initializers as internal as well...
44 for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
45 if (I->hasInitializer() && I->hasExternalLinkage()) {
46 I->setInternalLinkage(true);
49 DEBUG(std::cerr << "Internalizing gvar " << I->getName() << "\n");
56 RegisterOpt<InternalizePass> X("internalize", "Internalize Functions");
57 } // end anonymous namespace
59 Pass *createInternalizePass() {
60 return new InternalizePass();