Internalize.cpp revision 568ddabc8f9015baf2d42cc425618412972f1b92
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.h"
10#include "llvm/Pass.h"
11#include "llvm/Module.h"
12#include "llvm/Function.h"
13#include "Support/StatisticReporter.h"
14
15static Statistic<> NumChanged("internalize\t- Number of functions internal'd");
16
17namespace {
18class 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        FoundMain = true;
24        break;
25      }
26
27    if (!FoundMain) return false;  // No main found, must be a library...
28
29    bool Changed = false;
30
31    // Found a main function, mark all functions not named main as internal.
32    for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
33      if (I->getName() != "main" &&   // Leave the main function external
34          !I->isExternal()) {         // Function must be defined here
35        I->setInternalLinkage(true);
36        Changed = true;
37        ++NumChanged;
38      }
39
40    return Changed;
41  }
42};
43
44RegisterPass<InternalizePass> X("internalize", "Internalize Functions");
45} // end anonymous namespace
46
47Pass *createInternalizePass() {
48  return new InternalizePass();
49}
50