Mercurial > projects > ldc
annotate gen/passes/StripExternals.cpp @ 1532:c88b16d4a13c
Remove all of the now unecessary #if LLVM_REV
author | Benjamin Kramer <benny.kra@gmail.com> |
---|---|
date | Sat, 11 Jul 2009 14:09:33 +0200 |
parents | 7cca8cf730de |
children | 259b031f3d22 |
rev | line source |
---|---|
1483
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
1 //===-- StripExternals.cpp - Strip available_externally symbols -----------===// |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
2 // |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
3 // The LLVM D Compiler |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
4 // |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
5 // This file is distributed under the University of Illinois Open Source |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
6 // License. See LICENSE.TXT for details. |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
7 // |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
8 //===----------------------------------------------------------------------===// |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
9 // |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
10 // This transform stips the bodies of available_externally functions and |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
11 // initializers of available_externally globals, turning them into external |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
12 // declarations. |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
13 // This is useful to allow Global DCE (-globaldce) to clean up references to |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
14 // globals only used by available_externally functions and initializers. |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
15 // |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
16 //===----------------------------------------------------------------------===// |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
17 |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
18 #include "gen/llvm-version.h" |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
19 |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
20 #define DEBUG_TYPE "strip-externals" |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
21 |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
22 #include "Passes.h" |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
23 |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
24 #include "llvm/Module.h" |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
25 #include "llvm/Pass.h" |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
26 #include "llvm/ADT/Statistic.h" |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
27 #include "llvm/Support/Compiler.h" |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
28 #include "llvm/Support/Debug.h" |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
29 using namespace llvm; |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
30 |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
31 STATISTIC(NumFunctions, "Number of function bodies removed"); |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
32 STATISTIC(NumVariables, "Number of global initializers removed"); |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
33 |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
34 namespace { |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
35 struct VISIBILITY_HIDDEN StripExternals : public ModulePass { |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
36 static char ID; // Pass identification, replacement for typeid |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
37 StripExternals() : ModulePass(&ID) {} |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
38 |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
39 // run - Do the StripExternals pass on the specified module. |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
40 // |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
41 bool runOnModule(Module &M); |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
42 }; |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
43 } |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
44 |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
45 char StripExternals::ID = 0; |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
46 static RegisterPass<StripExternals> |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
47 X("strip-externals", "Strip available_externally bodies and initializers"); |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
48 |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
49 ModulePass *createStripExternalsPass() { return new StripExternals(); } |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
50 |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
51 bool StripExternals::runOnModule(Module &M) { |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
52 bool Changed = false; |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
53 |
1493
7cca8cf730de
Increment the iterator before deleting redundant functions or globals in the StripExternals pass.
Christian Kamm <kamm incasoftware de>
parents:
1483
diff
changeset
|
54 for (Module::iterator I = M.begin(); I != M.end(); ) { |
1483
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
55 if (I->hasAvailableExternallyLinkage()) { |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
56 assert(!I->isDeclaration()&&"Declarations can't be available_externally"); |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
57 Changed = true; |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
58 ++NumFunctions; |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
59 if (I->use_empty()) { |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
60 DOUT << "Deleting function: " << *I; |
1493
7cca8cf730de
Increment the iterator before deleting redundant functions or globals in the StripExternals pass.
Christian Kamm <kamm incasoftware de>
parents:
1483
diff
changeset
|
61 Module::iterator todelete = I; |
7cca8cf730de
Increment the iterator before deleting redundant functions or globals in the StripExternals pass.
Christian Kamm <kamm incasoftware de>
parents:
1483
diff
changeset
|
62 ++I; |
7cca8cf730de
Increment the iterator before deleting redundant functions or globals in the StripExternals pass.
Christian Kamm <kamm incasoftware de>
parents:
1483
diff
changeset
|
63 todelete->eraseFromParent(); |
7cca8cf730de
Increment the iterator before deleting redundant functions or globals in the StripExternals pass.
Christian Kamm <kamm incasoftware de>
parents:
1483
diff
changeset
|
64 continue; |
1483
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
65 } else { |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
66 I->deleteBody(); |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
67 DOUT << "Deleted function body: " << *I; |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
68 } |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
69 } |
1493
7cca8cf730de
Increment the iterator before deleting redundant functions or globals in the StripExternals pass.
Christian Kamm <kamm incasoftware de>
parents:
1483
diff
changeset
|
70 ++I; |
1483
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
71 } |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
72 |
1493
7cca8cf730de
Increment the iterator before deleting redundant functions or globals in the StripExternals pass.
Christian Kamm <kamm incasoftware de>
parents:
1483
diff
changeset
|
73 for (Module::global_iterator I = M.global_begin(); |
7cca8cf730de
Increment the iterator before deleting redundant functions or globals in the StripExternals pass.
Christian Kamm <kamm incasoftware de>
parents:
1483
diff
changeset
|
74 I != M.global_end(); ) { |
1483
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
75 if (I->hasAvailableExternallyLinkage()) { |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
76 assert(!I->isDeclaration()&&"Declarations can't be available_externally"); |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
77 Changed = true; |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
78 ++NumVariables; |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
79 if (I->use_empty()) { |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
80 DOUT << "Deleting global: " << *I; |
1493
7cca8cf730de
Increment the iterator before deleting redundant functions or globals in the StripExternals pass.
Christian Kamm <kamm incasoftware de>
parents:
1483
diff
changeset
|
81 Module::global_iterator todelete = I; |
7cca8cf730de
Increment the iterator before deleting redundant functions or globals in the StripExternals pass.
Christian Kamm <kamm incasoftware de>
parents:
1483
diff
changeset
|
82 ++I; |
7cca8cf730de
Increment the iterator before deleting redundant functions or globals in the StripExternals pass.
Christian Kamm <kamm incasoftware de>
parents:
1483
diff
changeset
|
83 todelete->eraseFromParent(); |
7cca8cf730de
Increment the iterator before deleting redundant functions or globals in the StripExternals pass.
Christian Kamm <kamm incasoftware de>
parents:
1483
diff
changeset
|
84 continue; |
1483
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
85 } else { |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
86 I->setInitializer(0); |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
87 I->setLinkage(GlobalValue::ExternalLinkage); |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
88 DOUT << "Deleted initializer: " << *I; |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
89 } |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
90 } |
1493
7cca8cf730de
Increment the iterator before deleting redundant functions or globals in the StripExternals pass.
Christian Kamm <kamm incasoftware de>
parents:
1483
diff
changeset
|
91 ++I; |
1483
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
92 } |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
93 |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
94 return Changed; |
defafbabbe32
Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff
changeset
|
95 } |