Be careful not to make "external" function internal
[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
14 class InternalizePass : public Pass {
15   virtual bool run(Module *M) {
16     bool FoundMain = false;   // Look for a function named main...
17     for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
18       if ((*I)->getName() == "main" && !(*I)->isExternal()) {
19         FoundMain = true;
20         break;
21       }
22     
23     if (!FoundMain) 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)->getName() != "main" &&  // Leave the main function external
30           !(*I)->isExternal())          // Function must be defined here
31         (*I)->setInternalLinkage(Changed = true);
32
33     return Changed;
34   }
35 };
36
37 Pass *createInternalizePass() {
38   return new InternalizePass();
39 }