[llvm-commits] [llvm] r51114 - in /llvm/trunk: include/llvm/Transforms/IPO.h lib/Transforms/IPO/Internalize.cpp test/Transforms/Internalize/ test/Transforms/Internalize/2008-05-09-AllButMain.ll test/Transforms/Internalize/2008-05-09-AllButMain.ll.apifile test/Transforms/Internalize/dg.exp
Devang Patel
dpatel at apple.com
Wed May 14 13:01:01 PDT 2008
Author: dpatel
Date: Wed May 14 15:01:01 2008
New Revision: 51114
URL: http://llvm.org/viewvc/llvm-project?rev=51114&view=rev
Log:
Simplify internalize pass. Add test case.
Patch by Matthijs Kooijman!
Added:
llvm/trunk/test/Transforms/Internalize/
llvm/trunk/test/Transforms/Internalize/2008-05-09-AllButMain.ll
llvm/trunk/test/Transforms/Internalize/2008-05-09-AllButMain.ll.apifile
llvm/trunk/test/Transforms/Internalize/dg.exp
Modified:
llvm/trunk/include/llvm/Transforms/IPO.h
llvm/trunk/lib/Transforms/IPO/Internalize.cpp
Modified: llvm/trunk/include/llvm/Transforms/IPO.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/IPO.h?rev=51114&r1=51113&r2=51114&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Transforms/IPO.h (original)
+++ llvm/trunk/include/llvm/Transforms/IPO.h Wed May 14 15:01:01 2008
@@ -102,12 +102,17 @@
//===----------------------------------------------------------------------===//
/// createInternalizePass - This pass loops over all of the functions in the
-/// input module, looking for a main function. If a list of symbols is
-/// specified with the -internalize-public-api-* command line options, those
-/// symbols are internalized. Otherwise if InternalizeEverything is set and
-/// the main function is found, all other globals are marked as internal.
+/// input module, internalizing all globals (functions and variables) not part
+/// of the api. If a list of symbols is specified with the
+/// -internalize-public-api-* command line options, those symbols are not
+/// internalized and all others are. Otherwise if AllButMain is set and the
+/// main function is found, all other globals are marked as internal.
///
ModulePass *createInternalizePass(bool InternalizeEverything);
+
+/// createInternalizePass - This pass loops over all of the functions in the
+/// input module, internalizing all globals (functions and variables) not in the
+/// given exportList.
ModulePass *createInternalizePass(const std::vector<const char *> &exportList);
//===----------------------------------------------------------------------===//
Modified: llvm/trunk/lib/Transforms/IPO/Internalize.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/IPO/Internalize.cpp?rev=51114&r1=51113&r2=51114&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/IPO/Internalize.cpp (original)
+++ llvm/trunk/lib/Transforms/IPO/Internalize.cpp Wed May 14 15:01:01 2008
@@ -43,7 +43,9 @@
namespace {
class VISIBILITY_HIDDEN InternalizePass : public ModulePass {
std::set<std::string> ExternalNames;
- bool DontInternalize;
+ /// If no api symbols were specified and a main function is defined,
+ /// assume the main function is the only API
+ bool AllButMain;
public:
static char ID; // Pass identification, replacement for typeid
explicit InternalizePass(bool InternalizeEverything = true);
@@ -57,19 +59,16 @@
static RegisterPass<InternalizePass>
X("internalize", "Internalize Global Symbols");
-InternalizePass::InternalizePass(bool InternalizeEverything)
- : ModulePass((intptr_t)&ID), DontInternalize(false){
- if (!APIFile.empty()) // If a filename is specified, use it
+InternalizePass::InternalizePass(bool AllButMain)
+ : ModulePass((intptr_t)&ID), AllButMain(AllButMain){
+ if (!APIFile.empty()) // If a filename is specified, use it.
LoadFile(APIFile.c_str());
- else if (!APIList.empty()) // Else, if a list is specified, use it.
+ if (!APIList.empty()) // If a list is specified, use it as well.
ExternalNames.insert(APIList.begin(), APIList.end());
- else if (!InternalizeEverything)
- // Finally, if we're allowed to, internalize all but main.
- DontInternalize = true;
}
InternalizePass::InternalizePass(const std::vector<const char *>&exportList)
- : ModulePass((intptr_t)&ID), DontInternalize(false){
+ : ModulePass((intptr_t)&ID), AllButMain(false){
for(std::vector<const char *>::const_iterator itr = exportList.begin();
itr != exportList.end(); itr++) {
ExternalNames.insert(*itr);
@@ -80,8 +79,9 @@
// Load the APIFile...
std::ifstream In(Filename);
if (!In.good()) {
- cerr << "WARNING: Internalize couldn't load file '" << Filename << "'!\n";
- return; // Do not internalize anything...
+ cerr << "WARNING: Internalize couldn't load file '" << Filename
+ << "'! Continuing as if it's empty.\n";
+ return; // Just continue as if the file were empty
}
while (In) {
std::string Symbol;
@@ -92,13 +92,14 @@
}
bool InternalizePass::runOnModule(Module &M) {
- if (DontInternalize) return false;
-
- // If no list or file of symbols was specified, check to see if there is a
- // "main" symbol defined in the module. If so, use it, otherwise do not
- // internalize the module, it must be a library or something.
- //
if (ExternalNames.empty()) {
+ // Return if we're not in 'all but main' mode and have no external api
+ if (!AllButMain)
+ return false;
+ // If no list or file of symbols was specified, check to see if there is a
+ // "main" symbol defined in the module. If so, use it, otherwise do not
+ // internalize the module, it must be a library or something.
+ //
Function *MainFunc = M.getFunction("main");
if (MainFunc == 0 || MainFunc->isDeclaration())
return false; // No main found, must be a library...
@@ -109,7 +110,7 @@
bool Changed = false;
- // Found a main function, mark all functions not named main as internal.
+ // Mark all functions not in the api as internal.
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
if (!I->isDeclaration() && // Function must be defined here
!I->hasInternalLinkage() && // Can't already have internal linkage
@@ -134,7 +135,8 @@
ExternalNames.insert("llvm.noinline");
ExternalNames.insert("llvm.global.annotations");
- // Mark all global variables with initializers as internal as well.
+ // Mark all global variables with initializers that are not in the api as
+ // internal as well.
for (Module::global_iterator I = M.global_begin(), E = M.global_end();
I != E; ++I)
if (!I->isDeclaration() && !I->hasInternalLinkage() &&
Added: llvm/trunk/test/Transforms/Internalize/2008-05-09-AllButMain.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/Internalize/2008-05-09-AllButMain.ll?rev=51114&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/Internalize/2008-05-09-AllButMain.ll (added)
+++ llvm/trunk/test/Transforms/Internalize/2008-05-09-AllButMain.ll Wed May 14 15:01:01 2008
@@ -0,0 +1,27 @@
+; No arguments means internalize all but main
+; RUN: llvm-as < %s | opt -internalize | llvm-dis | grep internal | count 4
+; Internalize all but foo and j
+; RUN: llvm-as < %s | opt -internalize -internalize-public-api-list foo -internalize-public-api-list j | llvm-dis | grep internal | count 3
+; Non existent files should be treated as if they were empty (so internalize all but main)
+; RUN: llvm-as < %s | opt -internalize -internalize-public-api-file /nonexistent/file | llvm-dis | grep internal | count 4
+; RUN: llvm-as < %s | opt -internalize -internalize-public-api-list bar -internalize-public-api-list foo -internalize-public-api-file /nonexistent/file | llvm-dis | grep internal | count 3
+; -file and -list options should be merged, the .apifile contains foo and j
+; RUN: llvm-as < %s | opt -internalize -internalize-public-api-list bar -internalize-public-api-file %s.apifile | llvm-dis | grep internal | count 2
+
+ at i = weak global i32 0 ; <i32*> [#uses=0]
+ at j = weak global i32 0 ; <i32*> [#uses=0]
+
+define void @main(...) {
+entry:
+ ret void
+}
+
+define void @foo(...) {
+entry:
+ ret void
+}
+
+define void @bar(...) {
+entry:
+ ret void
+}
Added: llvm/trunk/test/Transforms/Internalize/2008-05-09-AllButMain.ll.apifile
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/Internalize/2008-05-09-AllButMain.ll.apifile?rev=51114&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/Internalize/2008-05-09-AllButMain.ll.apifile (added)
+++ llvm/trunk/test/Transforms/Internalize/2008-05-09-AllButMain.ll.apifile Wed May 14 15:01:01 2008
@@ -0,0 +1,2 @@
+foo
+j
Added: llvm/trunk/test/Transforms/Internalize/dg.exp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/Internalize/dg.exp?rev=51114&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/Internalize/dg.exp (added)
+++ llvm/trunk/test/Transforms/Internalize/dg.exp Wed May 14 15:01:01 2008
@@ -0,0 +1,3 @@
+load_lib llvm.exp
+
+RunLLVMTests [lsort [glob -nocomplain $srcdir/$subdir/*.{ll,llx,c,cpp,tr}]]
More information about the llvm-commits
mailing list