2002-04-28 13:43:27 +08:00
|
|
|
//===-- Internalize.cpp - Mark functions internal -------------------------===//
|
|
|
|
//
|
|
|
|
// This pass loops over all of the functions in the input module, looking for a
|
2002-07-31 03:48:44 +08:00
|
|
|
// main function. If a main function is found, all other functions and all
|
|
|
|
// global variables with initializers are marked as internal.
|
2002-04-28 13:43:27 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2002-07-25 01:12:05 +08:00
|
|
|
#include "llvm/Transforms/IPO.h"
|
2002-04-28 13:43:27 +08:00
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Module.h"
|
2002-10-02 06:38:41 +08:00
|
|
|
#include "Support/Statistic.h"
|
2002-05-10 23:38:35 +08:00
|
|
|
|
2002-07-24 02:06:35 +08:00
|
|
|
namespace {
|
2002-10-02 06:38:41 +08:00
|
|
|
Statistic<> NumFunctions("internalize", "Number of functions internalized");
|
|
|
|
Statistic<> NumGlobals ("internalize", "Number of global vars internalized");
|
2002-07-31 03:48:44 +08:00
|
|
|
|
|
|
|
class InternalizePass : public Pass {
|
|
|
|
virtual bool run(Module &M) {
|
2002-11-09 04:34:21 +08:00
|
|
|
Function *MainFunc = M.getMainFunction();
|
|
|
|
|
|
|
|
if (MainFunc == 0 || MainFunc->isExternal())
|
|
|
|
return false; // No main found, must be a library...
|
2002-07-31 03:48:44 +08:00
|
|
|
|
|
|
|
bool Changed = false;
|
|
|
|
|
|
|
|
// Found a main function, mark all functions not named main as internal.
|
|
|
|
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
|
2002-11-09 04:34:21 +08:00
|
|
|
if (&*I != MainFunc && // Leave the main function external
|
2002-07-31 03:48:44 +08:00
|
|
|
!I->isExternal() && // Function must be defined here
|
|
|
|
!I->hasInternalLinkage()) { // Can't already have internal linkage
|
2003-04-17 04:28:45 +08:00
|
|
|
I->setLinkage(GlobalValue::InternalLinkage);
|
2002-07-31 03:48:44 +08:00
|
|
|
Changed = true;
|
|
|
|
++NumFunctions;
|
|
|
|
DEBUG(std::cerr << "Internalizing func " << I->getName() << "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark all global variables with initializers as internal as well...
|
|
|
|
for (Module::giterator I = M.gbegin(), E = M.gend(); I != E; ++I)
|
2002-10-08 02:34:32 +08:00
|
|
|
if (!I->isExternal() && I->hasExternalLinkage()) {
|
2003-04-17 04:28:45 +08:00
|
|
|
I->setLinkage(GlobalValue::InternalLinkage);
|
2002-07-31 03:48:44 +08:00
|
|
|
Changed = true;
|
|
|
|
++NumGlobals;
|
|
|
|
DEBUG(std::cerr << "Internalizing gvar " << I->getName() << "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
};
|
2002-04-28 13:43:27 +08:00
|
|
|
|
2002-07-27 05:12:44 +08:00
|
|
|
RegisterOpt<InternalizePass> X("internalize", "Internalize Functions");
|
2002-07-24 02:06:35 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2002-04-28 13:43:27 +08:00
|
|
|
Pass *createInternalizePass() {
|
|
|
|
return new InternalizePass();
|
|
|
|
}
|