comparison 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
comparison
equal deleted inserted replaced
1482:d9c5f5a43403 1483:defafbabbe32
1 //===-- StripExternals.cpp - Strip available_externally symbols -----------===//
2 //
3 // The LLVM D Compiler
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This transform stips the bodies of available_externally functions and
11 // initializers of available_externally globals, turning them into external
12 // declarations.
13 // This is useful to allow Global DCE (-globaldce) to clean up references to
14 // globals only used by available_externally functions and initializers.
15 //
16 //===----------------------------------------------------------------------===//
17
18 #include "gen/llvm-version.h"
19
20 #if LLVM_REV >= 68940
21
22
23 #define DEBUG_TYPE "strip-externals"
24
25 #include "Passes.h"
26
27 #include "llvm/Module.h"
28 #include "llvm/Pass.h"
29 #include "llvm/ADT/Statistic.h"
30 #include "llvm/Support/Compiler.h"
31 #include "llvm/Support/Debug.h"
32 using namespace llvm;
33
34 STATISTIC(NumFunctions, "Number of function bodies removed");
35 STATISTIC(NumVariables, "Number of global initializers removed");
36
37 namespace {
38 struct VISIBILITY_HIDDEN StripExternals : public ModulePass {
39 static char ID; // Pass identification, replacement for typeid
40 StripExternals() : ModulePass(&ID) {}
41
42 // run - Do the StripExternals pass on the specified module.
43 //
44 bool runOnModule(Module &M);
45 };
46 }
47
48 char StripExternals::ID = 0;
49 static RegisterPass<StripExternals>
50 X("strip-externals", "Strip available_externally bodies and initializers");
51
52 ModulePass *createStripExternalsPass() { return new StripExternals(); }
53
54 bool StripExternals::runOnModule(Module &M) {
55 bool Changed = false;
56
57 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
58 if (I->hasAvailableExternallyLinkage()) {
59 assert(!I->isDeclaration()&&"Declarations can't be available_externally");
60 Changed = true;
61 ++NumFunctions;
62 if (I->use_empty()) {
63 DOUT << "Deleting function: " << *I;
64 I->eraseFromParent();
65 } else {
66 I->deleteBody();
67 DOUT << "Deleted function body: " << *I;
68 }
69 }
70 }
71
72 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
73 I != E; ++I) {
74 if (I->hasAvailableExternallyLinkage()) {
75 assert(!I->isDeclaration()&&"Declarations can't be available_externally");
76 Changed = true;
77 ++NumVariables;
78 if (I->use_empty()) {
79 DOUT << "Deleting global: " << *I;
80 I->eraseFromParent();
81 } else {
82 I->setInitializer(0);
83 I->setLinkage(GlobalValue::ExternalLinkage);
84 DOUT << "Deleted initializer: " << *I;
85 }
86 }
87 }
88
89 return Changed;
90 }
91
92 #endif //LLVM_REV >= 68940