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");
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()) {
27 if (!FoundMain) return false; // No main found, must be a library...
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);
44 RegisterPass<InternalizePass> X("internalize", "Internalize Functions");
45 } // end anonymous namespace
47 Pass *createInternalizePass() {
48 return new InternalizePass();