Updates to work with recent Statistic's changes:
[oota-llvm.git] / lib / Transforms / IPO / Internalize.cpp
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       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()) {
24           FoundMain = true;
25           break;
26         }
27       
28       if (!FoundMain) return false;  // No main found, must be a library...
29       
30       bool Changed = false;
31       
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);
38           Changed = true;
39           ++NumFunctions;
40           DEBUG(std::cerr << "Internalizing func " << I->getName() << "\n");
41         }
42
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);
47           Changed = true;
48           ++NumGlobals;
49           DEBUG(std::cerr << "Internalizing gvar " << I->getName() << "\n");
50         }
51       
52       return Changed;
53     }
54   };
55
56   RegisterOpt<InternalizePass> X("internalize", "Internalize Functions");
57 } // end anonymous namespace
58
59 Pass *createInternalizePass() {
60   return new InternalizePass();
61 }