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