llvm.org GIT mirror llvm / e5551ed
Change the internalize pass to internalize all symbols when given an empty list of externals. This makes sense since a shared library with no symbols can still be useful if it has static constructors. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@166795 91177308-0d34-0410-b5e6-96231b3b80d8 Rafael Espindola 6 years ago
8 changed file(s) with 41 addition(s) and 50 deletion(s). Raw diff Collapse all Expand all
8181 (void) llvm::createIPSCCPPass();
8282 (void) llvm::createIndVarSimplifyPass();
8383 (void) llvm::createInstructionCombiningPass();
84 (void) llvm::createInternalizePass(false);
84 (void) llvm::createInternalizePass();
8585 (void) llvm::createLCSSAPass();
8686 (void) llvm::createLICMPass();
8787 (void) llvm::createLazyValueInfoPass();
103103
104104 //===----------------------------------------------------------------------===//
105105 /// createInternalizePass - This pass loops over all of the functions in the
106 /// input module, internalizing all globals (functions and variables) not part
107 /// of the api. If a list of symbols is specified with the
108 /// -internalize-public-api-* command line options, those symbols are not
109 /// internalized and all others are. Otherwise if AllButMain is set and the
110 /// main function is found, all other globals are marked as internal. If no api
111 /// is supplied and AllButMain is not set, or no main function is found, nothing
112 /// is internalized.
113 ///
114 ModulePass *createInternalizePass(bool AllButMain);
115
116 /// createInternalizePass - This pass loops over all of the functions in the
117106 /// input module, internalizing all globals (functions and variables) not in the
118107 /// given exportList.
119108 ///
120109 /// Note that commandline options that are used with the above function are not
121 /// used now! Also, when exportList is empty, nothing is internalized.
110 /// used now!
122111 ModulePass *createInternalizePass(const std::vector &exportList);
112 /// createInternalizePass - Same as above, but with an empty exportList.
113 ModulePass *createInternalizePass();
123114
124115 //===----------------------------------------------------------------------===//
125116 /// createDeadArgEliminationPass - This pass removes arguments from functions
9494 }
9595
9696 void LLVMAddInternalizePass(LLVMPassManagerRef PM, unsigned AllButMain) {
97 unwrap(PM)->add(createInternalizePass(AllButMain != 0));
97 std::vector Export;
98 if (AllButMain)
99 Export.push_back("main");
100 unwrap(PM)->add(createInternalizePass(Export));
98101 }
99102
100103 void LLVMAddStripDeadPrototypesPass(LLVMPassManagerRef PM) {
66 //
77 //===----------------------------------------------------------------------===//
88 //
9 // This pass loops over all of the functions in the input module, looking for a
10 // main function. If a main function is found, all other functions and all
11 // global variables with initializers are marked as internal.
9 // This pass loops over all of the functions and variables in the input module.
10 // If the function or variable is not in the list of external names given to
11 // the pass it is marked as internal.
1212 //
1313 //===----------------------------------------------------------------------===//
1414
4444 namespace {
4545 class InternalizePass : public ModulePass {
4646 std::set ExternalNames;
47 /// If no api symbols were specified and a main function is defined,
48 /// assume the main function is the only API
49 bool AllButMain;
5047 public:
5148 static char ID; // Pass identification, replacement for typeid
52 explicit InternalizePass(bool AllButMain = true);
49 explicit InternalizePass();
5350 explicit InternalizePass(const std::vector & exportList);
5451 void LoadFile(const char *Filename);
5552 virtual bool runOnModule(Module &M);
6562 INITIALIZE_PASS(InternalizePass, "internalize",
6663 "Internalize Global Symbols", false, false)
6764
68 InternalizePass::InternalizePass(bool AllButMain)
69 : ModulePass(ID), AllButMain(AllButMain){
65 InternalizePass::InternalizePass()
66 : ModulePass(ID) {
7067 initializeInternalizePassPass(*PassRegistry::getPassRegistry());
7168 if (!APIFile.empty()) // If a filename is specified, use it.
7269 LoadFile(APIFile.c_str());
7572 }
7673
7774 InternalizePass::InternalizePass(const std::vector&exportList)
78 : ModulePass(ID), AllButMain(false){
75 : ModulePass(ID){
7976 initializeInternalizePassPass(*PassRegistry::getPassRegistry());
8077 for(std::vector::const_iterator itr = exportList.begin();
8178 itr != exportList.end(); itr++) {
10299 bool InternalizePass::runOnModule(Module &M) {
103100 CallGraph *CG = getAnalysisIfAvailable();
104101 CallGraphNode *ExternalNode = CG ? CG->getExternalCallingNode() : 0;
105
106 if (ExternalNames.empty()) {
107 // Return if we're not in 'all but main' mode and have no external api
108 if (!AllButMain)
109 return false;
110 // If no list or file of symbols was specified, check to see if there is a
111 // "main" symbol defined in the module. If so, use it, otherwise do not
112 // internalize the module, it must be a library or something.
113 //
114 Function *MainFunc = M.getFunction("main");
115 if (MainFunc == 0 || MainFunc->isDeclaration())
116 return false; // No main found, must be a library...
117
118 // Preserve main, internalize all else.
119 ExternalNames.insert(MainFunc->getName());
120 }
121
122102 bool Changed = false;
123103
124104 // Never internalize functions which code-gen might insert.
188168 return Changed;
189169 }
190170
191 ModulePass *llvm::createInternalizePass(bool AllButMain) {
192 return new InternalizePass(AllButMain);
171 ModulePass *llvm::createInternalizePass() {
172 return new InternalizePass();
193173 }
194174
195175 ModulePass *llvm::createInternalizePass(const std::vector &el) {
244244 // Now that composite has been compiled, scan through the module, looking
245245 // for a main function. If main is defined, mark all other functions
246246 // internal.
247 if (Internalize)
248 PM.add(createInternalizePass(true));
247 if (Internalize) {
248 std::vector E;
249 E.push_back("main");
250 PM.add(createInternalizePass(E));
251 }
249252
250253 // Propagate constants at call sites into the functions they call. This
251254 // opens opportunities for globalopt (and inlining) by substituting function
0 ;RUN: opt -S -std-link-opts < %s | FileCheck %s
1 ; Simple test to check that -std-link-opts keeps only the main function.
2
3 ; CHECK-NOT: define
4 ; CHECK: define void @main
5 ; CHECK-NOT: define
6 define void @main() {
7 ret void
8 }
9
10 define void @foo() {
11 ret void
12 }
None ; No arguments means internalize all but main
0 ; No arguments means internalize everything
11 ; RUN: opt < %s -internalize -S | FileCheck --check-prefix=NOARGS %s
22
33 ; Internalize all but foo and j
44 ; RUN: opt < %s -internalize -internalize-public-api-list foo -internalize-public-api-list j -S | FileCheck --check-prefix=LIST %s
55
6 ; Non existent files should be treated as if they were empty (so internalize all but main)
6 ; Non existent files should be treated as if they were empty (so internalize
7 ; everything)
78 ; RUN: opt < %s -internalize -internalize-public-api-file /nonexistent/file 2> /dev/null -S | FileCheck --check-prefix=EMPTYFILE %s
89
910 ; RUN: opt < %s -S -internalize -internalize-public-api-list bar -internalize-public-api-list foo -internalize-public-api-file /nonexistent/file 2> /dev/null | FileCheck --check-prefix=LIST2 %s
2526 ; MERGE: @j = global
2627 @j = global i32 0
2728
28 ; NOARGS: define void @main
29 ; NOARGS: define internal void @main
2930 ; LIST: define internal void @main
30 ; EMPTYFILE: define void @main
31 ; EMPTYFILE: define internal void @main
3132 ; LIST2: define internal void @main
3233 ; MERGE: define internal void @main
3334 define void @main() {
None ; RUN: opt < %s -internalize -S | grep internal | count 3
0 ; RUN: opt < %s -internalize -internalize-public-api-list main -S | grep internal | count 3
11
22 @A = global i32 0
33 @B = alias i32* @A