annotate gen/passes/StripExternals.cpp @ 1483:defafbabbe32

Add a pass to strip the bodies of `available_externally` functions so string literals and `TypeInfo`s only referenced by them can be deleted by `-globaldce`.
author Frits van Bommel <fvbommel wxs.nl>
date Sun, 07 Jun 2009 16:00:13 +0200
parents
children 7cca8cf730de
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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 #if LLVM_REV >= 68940
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
defafbabbe32 Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
23 #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
24
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 "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
26
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/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
28 #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
29 #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
30 #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
31 #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
32 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
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 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
35 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
36
defafbabbe32 Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
37 namespace {
defafbabbe32 Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
38 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
39 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
40 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
41
defafbabbe32 Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
42 // 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
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 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
45 };
defafbabbe32 Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
46 }
defafbabbe32 Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
47
defafbabbe32 Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
48 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
49 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
50 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
51
defafbabbe32 Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
52 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
53
defafbabbe32 Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
54 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
55 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
56
defafbabbe32 Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
57 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
defafbabbe32 Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
58 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
59 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
60 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
61 ++NumFunctions;
defafbabbe32 Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
62 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
63 DOUT << "Deleting function: " << *I;
defafbabbe32 Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
64 I->eraseFromParent();
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 }
defafbabbe32 Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
70 }
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 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
defafbabbe32 Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
73 I != E; ++I) {
defafbabbe32 Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
74 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
75 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
76 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
77 ++NumVariables;
defafbabbe32 Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
78 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
79 DOUT << "Deleting global: " << *I;
defafbabbe32 Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
80 I->eraseFromParent();
defafbabbe32 Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
81 } else {
defafbabbe32 Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
82 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
83 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
84 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
85 }
defafbabbe32 Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
86 }
defafbabbe32 Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
87 }
defafbabbe32 Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
88
defafbabbe32 Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
89 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
90 }
defafbabbe32 Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
91
defafbabbe32 Add a pass to strip the bodies of `available_externally` functions so string
Frits van Bommel <fvbommel wxs.nl>
parents:
diff changeset
92 #endif //LLVM_REV >= 68940