MEGAPATCH checkin.
[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 are marked
5 // as internal.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/Transforms/IPO/Internalize.h"
10 #include "llvm/Pass.h"
11 #include "llvm/Module.h"
12 #include "llvm/Function.h"
13 #include "Support/StatisticReporter.h"
14
15 static Statistic<> NumChanged("internalize\t- Number of functions internal'd");
16
17 class InternalizePass : public Pass {
18   const char *getPassName() const { return "Internalize Functions"; }
19
20   virtual bool run(Module &M) {
21     bool FoundMain = false;   // Look for a function named main...
22     for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
23       if (I->getName() == "main" && !I->isExternal()) {
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->setInternalLinkage(true);
37         Changed = true;
38         ++NumChanged;
39       }
40
41     return Changed;
42   }
43 };
44
45 Pass *createInternalizePass() {
46   return new InternalizePass();
47 }