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 are marked
7 //===----------------------------------------------------------------------===//
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"
15 static Statistic<> NumChanged("internalize\t- Number of functions internal'd");
17 class InternalizePass : public Pass {
18 const char *getPassName() const { return "Internalize Functions"; }
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()) {
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)->setInternalLinkage(true);
45 Pass *createInternalizePass() {
46 return new InternalizePass();