2002-04-28 07:43:27 +02:00
|
|
|
//===-- Internalize.cpp - Mark functions internal -------------------------===//
|
|
|
|
//
|
|
|
|
// This pass loops over all of the functions in the input module, looking for a
|
2002-07-30 21:48:44 +02: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 07:43:27 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2002-07-24 19:12:05 +02:00
|
|
|
#include "llvm/Transforms/IPO.h"
|
2002-04-28 07:43:27 +02:00
|
|
|
#include "llvm/Pass.h"
|
|
|
|
#include "llvm/Module.h"
|
2002-10-02 00:38:41 +02:00
|
|
|
#include "Support/Statistic.h"
|
2002-05-10 17:38:35 +02:00
|
|
|
|
2002-07-23 20:06:35 +02:00
|
|
|
namespace {
|
2002-10-02 00:38:41 +02:00
|
|
|
Statistic<> NumFunctions("internalize", "Number of functions internalized");
|
|
|
|
Statistic<> NumGlobals ("internalize", "Number of global vars internalized");
|
2002-07-30 21:48:44 +02:00
|
|
|
|
|
|
|
class InternalizePass : public Pass {
|
|
|
|
virtual bool run(Module &M) {
|
2002-11-08 21:34:21 +01:00
|
|
|
Function *MainFunc = M.getMainFunction();
|
|
|
|
|
|
|
|
if (MainFunc == 0 || MainFunc->isExternal())
|
|
|
|
return false; // No main found, must be a library...
|
2002-07-30 21:48:44 +02: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-08 21:34:21 +01:00
|
|
|
if (&*I != MainFunc && // Leave the main function external
|
2002-07-30 21:48:44 +02:00
|
|
|
!I->isExternal() && // Function must be defined here
|
|
|
|
!I->hasInternalLinkage()) { // Can't already have internal linkage
|
2003-04-16 22:28:45 +02:00
|
|
|
I->setLinkage(GlobalValue::InternalLinkage);
|
2002-07-30 21:48:44 +02: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-07 20:34:32 +02:00
|
|
|
if (!I->isExternal() && I->hasExternalLinkage()) {
|
2003-04-16 22:28:45 +02:00
|
|
|
I->setLinkage(GlobalValue::InternalLinkage);
|
2002-07-30 21:48:44 +02:00
|
|
|
Changed = true;
|
|
|
|
++NumGlobals;
|
|
|
|
DEBUG(std::cerr << "Internalizing gvar " << I->getName() << "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
};
|
2002-04-28 07:43:27 +02:00
|
|
|
|
2002-07-26 23:12:44 +02:00
|
|
|
RegisterOpt<InternalizePass> X("internalize", "Internalize Functions");
|
2002-07-23 20:06:35 +02:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2002-04-28 07:43:27 +02:00
|
|
|
Pass *createInternalizePass() {
|
|
|
|
return new InternalizePass();
|
|
|
|
}
|