shrink vmcore by moving symbol table stripping support out of VMCore into
[oota-llvm.git] / lib / Transforms / IPO / StripSymbols.cpp
1 //===- StripSymbols.cpp - Strip symbols and debug info from a module ------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements stripping symbols out of symbol tables.
11 //
12 // Specifically, this allows you to strip all of the symbols out of:
13 //   * All functions in a module
14 //   * All non-essential symbols in a module (all function symbols + all module
15 //     scope symbols)
16 //   * Debug information.
17 //
18 // Notice that:
19 //   * This pass makes code much less readable, so it should only be used in
20 //     situations where the 'strip' utility would be used (such as reducing
21 //     code size, and making it harder to reverse engineer code).
22 //
23 //===----------------------------------------------------------------------===//
24
25 #include "llvm/Transforms/IPO.h"
26 #include "llvm/Constants.h"
27 #include "llvm/DerivedTypes.h"
28 #include "llvm/Instructions.h"
29 #include "llvm/Module.h"
30 #include "llvm/Pass.h"
31 #include "llvm/ValueSymbolTable.h"
32 #include "llvm/TypeSymbolTable.h"
33 #include "llvm/Support/Compiler.h"
34 using namespace llvm;
35
36 namespace {
37   class VISIBILITY_HIDDEN StripSymbols : public ModulePass {
38     bool OnlyDebugInfo;
39   public:
40     StripSymbols(bool ODI = false) : OnlyDebugInfo(ODI) {}
41
42     virtual bool runOnModule(Module &M);
43
44     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
45       AU.setPreservesAll();
46     }
47   };
48   RegisterPass<StripSymbols> X("strip", "Strip all symbols from a module");
49 }
50
51 ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
52   return new StripSymbols(OnlyDebugInfo);
53 }
54
55 static void RemoveDeadConstant(Constant *C) {
56   assert(C->use_empty() && "Constant is not dead!");
57   std::vector<Constant*> Operands;
58   for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
59     if (isa<DerivedType>(C->getOperand(i)->getType()) &&
60         C->getOperand(i)->hasOneUse())
61       Operands.push_back(C->getOperand(i));
62   if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
63     if (!GV->hasInternalLinkage()) return;   // Don't delete non static globals.
64     GV->eraseFromParent();
65   }
66   else if (!isa<Function>(C))
67     C->destroyConstant();
68
69   // If the constant referenced anything, see if we can delete it as well.
70   while (!Operands.empty()) {
71     RemoveDeadConstant(Operands.back());
72     Operands.pop_back();
73   }
74 }
75
76 // Strip the symbol table of its names.
77 //
78 static void StripSymtab(ValueSymbolTable &ST) {
79   for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
80     Value *V = VI->second;
81     ++VI;
82     if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasInternalLinkage()) {
83       // Set name to "", removing from symbol table!
84       V->setName("");
85     }
86   }
87 }
88
89 // Strip the symbol table of its names.
90 static void StripTypeSymtab(TypeSymbolTable &ST) {
91   for (TypeSymbolTable::iterator TI = ST.begin(), E = ST.end(); TI != E; )
92     ST.remove(TI++);
93 }
94
95
96
97 bool StripSymbols::runOnModule(Module &M) {
98   // If we're not just stripping debug info, strip all symbols from the
99   // functions and the names from any internal globals.
100   if (!OnlyDebugInfo) {
101     for (Module::global_iterator I = M.global_begin(), E = M.global_end();
102          I != E; ++I)
103       if (I->hasInternalLinkage())
104         I->setName("");     // Internal symbols can't participate in linkage
105
106     for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
107       if (I->hasInternalLinkage())
108         I->setName("");     // Internal symbols can't participate in linkage
109       StripSymtab(I->getValueSymbolTable());
110     }
111     
112     // Remove all names from types.
113     StripTypeSymtab(M.getTypeSymbolTable());
114   }
115
116   // Strip debug info in the module if it exists.  To do this, we remove
117   // llvm.dbg.func.start, llvm.dbg.stoppoint, and llvm.dbg.region.end calls, and
118   // any globals they point to if now dead.
119   Function *FuncStart = M.getFunction("llvm.dbg.func.start");
120   Function *StopPoint = M.getFunction("llvm.dbg.stoppoint");
121   Function *RegionStart = M.getFunction("llvm.dbg.region.start");
122   Function *RegionEnd = M.getFunction("llvm.dbg.region.end");
123   Function *Declare = M.getFunction("llvm.dbg.declare");
124   if (!FuncStart && !StopPoint && !RegionStart && !RegionEnd && !Declare)
125     return true;
126
127   std::vector<GlobalVariable*> DeadGlobals;
128
129   // Remove all of the calls to the debugger intrinsics, and remove them from
130   // the module.
131   if (FuncStart) {
132     while (!FuncStart->use_empty()) {
133       CallInst *CI = cast<CallInst>(FuncStart->use_back());
134       Value *Arg = CI->getOperand(1);
135       assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
136       CI->eraseFromParent();
137       if (Arg->use_empty())
138         if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
139           DeadGlobals.push_back(GV);
140     }
141     FuncStart->eraseFromParent();
142   }
143   if (StopPoint) {
144     while (!StopPoint->use_empty()) {
145       CallInst *CI = cast<CallInst>(StopPoint->use_back());
146       Value *Arg = CI->getOperand(3);
147       assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
148       CI->eraseFromParent();
149       if (Arg->use_empty())
150         if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
151           DeadGlobals.push_back(GV);
152     }
153     StopPoint->eraseFromParent();
154   }
155   if (RegionStart) {
156     while (!RegionStart->use_empty()) {
157       CallInst *CI = cast<CallInst>(RegionStart->use_back());
158       Value *Arg = CI->getOperand(1);
159       assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
160       CI->eraseFromParent();
161       if (Arg->use_empty())
162         if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
163           DeadGlobals.push_back(GV);
164     }
165     RegionStart->eraseFromParent();
166   }
167   if (RegionEnd) {
168     while (!RegionEnd->use_empty()) {
169       CallInst *CI = cast<CallInst>(RegionEnd->use_back());
170       Value *Arg = CI->getOperand(1);
171       assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
172       CI->eraseFromParent();
173       if (Arg->use_empty())
174         if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
175           DeadGlobals.push_back(GV);
176     }
177     RegionEnd->eraseFromParent();
178   }
179   if (Declare) {
180     while (!Declare->use_empty()) {
181       CallInst *CI = cast<CallInst>(Declare->use_back());
182       Value *Arg = CI->getOperand(2);
183       assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
184       CI->eraseFromParent();
185       if (Arg->use_empty())
186         if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
187           DeadGlobals.push_back(GV);
188     }
189     Declare->eraseFromParent();
190   }
191
192   // Finally, delete any internal globals that were only used by the debugger
193   // intrinsics.
194   while (!DeadGlobals.empty()) {
195     GlobalVariable *GV = DeadGlobals.back();
196     DeadGlobals.pop_back();
197     if (GV->hasInternalLinkage())
198       RemoveDeadConstant(GV);
199   }
200
201   return true;
202 }