Really remove all debug information.
[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 is distributed under the University of Illinois Open Source
6 // 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 #include "llvm/ADT/SmallPtrSet.h"
33 using namespace llvm;
34
35 namespace {
36   class VISIBILITY_HIDDEN StripSymbols : public ModulePass {
37     bool OnlyDebugInfo;
38   public:
39     static char ID; // Pass identification, replacement for typeid
40     explicit StripSymbols(bool ODI = false) 
41       : ModulePass(&ID), OnlyDebugInfo(ODI) {}
42
43     virtual bool runOnModule(Module &M);
44
45     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
46       AU.setPreservesAll();
47     }
48   };
49 }
50
51 char StripSymbols::ID = 0;
52 static RegisterPass<StripSymbols>
53 X("strip", "Strip all symbols from a module");
54
55 ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
56   return new StripSymbols(OnlyDebugInfo);
57 }
58
59 /// OnlyUsedBy - Return true if V is only used by Usr.
60 static bool OnlyUsedBy(Value *V, Value *Usr) {
61   for(Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
62     User *U = *I;
63     if (U != Usr)
64       return false;
65   }
66   return true;
67 }
68
69 static void RemoveDeadConstant(Constant *C) {
70   assert(C->use_empty() && "Constant is not dead!");
71   SmallPtrSet<Constant *, 4> Operands;
72   for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
73     if (isa<DerivedType>(C->getOperand(i)->getType()) &&
74         OnlyUsedBy(C->getOperand(i), C)) 
75       Operands.insert(C->getOperand(i));
76   if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
77     if (!GV->hasInternalLinkage()) return;   // Don't delete non static globals.
78     GV->eraseFromParent();
79   }
80   else if (!isa<Function>(C))
81     C->destroyConstant();
82
83   // If the constant referenced anything, see if we can delete it as well.
84   for (SmallPtrSet<Constant *, 4>::iterator OI = Operands.begin(),
85          OE = Operands.end(); OI != OE; ++OI)
86     RemoveDeadConstant(*OI);
87 }
88
89 // Strip the symbol table of its names.
90 //
91 static void StripSymtab(ValueSymbolTable &ST) {
92   for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
93     Value *V = VI->getValue();
94     ++VI;
95     if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasInternalLinkage()) {
96       // Set name to "", removing from symbol table!
97       V->setName("");
98     }
99   }
100 }
101
102 // Strip the symbol table of its names.
103 static void StripTypeSymtab(TypeSymbolTable &ST) {
104   for (TypeSymbolTable::iterator TI = ST.begin(), E = ST.end(); TI != E; )
105     ST.remove(TI++);
106 }
107
108
109
110 bool StripSymbols::runOnModule(Module &M) {
111   // If we're not just stripping debug info, strip all symbols from the
112   // functions and the names from any internal globals.
113   if (!OnlyDebugInfo) {
114     SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
115     if (GlobalVariable *LLVMUsed = M.getGlobalVariable("llvm.used")) {
116       llvmUsedValues.insert(LLVMUsed);
117       // Collect values that are preserved as per explicit request.
118       // llvm.used is used to list these values.
119       if (ConstantArray *Inits = 
120             dyn_cast<ConstantArray>(LLVMUsed->getInitializer())) {
121         for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i) {
122           if (GlobalValue *GV = dyn_cast<GlobalValue>(Inits->getOperand(i)))
123             llvmUsedValues.insert(GV);
124           else if (ConstantExpr *CE =
125                        dyn_cast<ConstantExpr>(Inits->getOperand(i)))
126             if (CE->getOpcode() == Instruction::BitCast)
127               if (GlobalValue *GV = dyn_cast<GlobalValue>(CE->getOperand(0)))
128                 llvmUsedValues.insert(GV);
129         }
130       }
131     }
132
133     for (Module::global_iterator I = M.global_begin(), E = M.global_end();
134          I != E; ++I) {
135       if (I->hasInternalLinkage() && llvmUsedValues.count(I) == 0)
136         I->setName("");     // Internal symbols can't participate in linkage
137     }
138
139     for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
140       if (I->hasInternalLinkage() && llvmUsedValues.count(I) == 0)
141         I->setName("");     // Internal symbols can't participate in linkage
142       StripSymtab(I->getValueSymbolTable());
143     }
144     
145     // Remove all names from types.
146     StripTypeSymtab(M.getTypeSymbolTable());
147   }
148
149   // Strip debug info in the module if it exists.  To do this, we remove
150   // llvm.dbg.func.start, llvm.dbg.stoppoint, and llvm.dbg.region.end calls, and
151   // any globals they point to if now dead.
152   Function *FuncStart = M.getFunction("llvm.dbg.func.start");
153   Function *StopPoint = M.getFunction("llvm.dbg.stoppoint");
154   Function *RegionStart = M.getFunction("llvm.dbg.region.start");
155   Function *RegionEnd = M.getFunction("llvm.dbg.region.end");
156   Function *Declare = M.getFunction("llvm.dbg.declare");
157
158   std::vector<GlobalVariable*> DeadGlobals;
159
160   // Remove all of the calls to the debugger intrinsics, and remove them from
161   // the module.
162   if (FuncStart) {
163     while (!FuncStart->use_empty()) {
164       CallInst *CI = cast<CallInst>(FuncStart->use_back());
165       Value *Arg = CI->getOperand(1);
166       assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
167       CI->eraseFromParent();
168       if (Arg->use_empty())
169         if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
170           DeadGlobals.push_back(GV);
171     }
172     FuncStart->eraseFromParent();
173   }
174   if (StopPoint) {
175     while (!StopPoint->use_empty()) {
176       CallInst *CI = cast<CallInst>(StopPoint->use_back());
177       Value *Arg = CI->getOperand(3);
178       assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
179       CI->eraseFromParent();
180       if (Arg->use_empty())
181         if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
182           DeadGlobals.push_back(GV);
183     }
184     StopPoint->eraseFromParent();
185   }
186   if (RegionStart) {
187     while (!RegionStart->use_empty()) {
188       CallInst *CI = cast<CallInst>(RegionStart->use_back());
189       Value *Arg = CI->getOperand(1);
190       assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
191       CI->eraseFromParent();
192       if (Arg->use_empty())
193         if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
194           DeadGlobals.push_back(GV);
195     }
196     RegionStart->eraseFromParent();
197   }
198   if (RegionEnd) {
199     while (!RegionEnd->use_empty()) {
200       CallInst *CI = cast<CallInst>(RegionEnd->use_back());
201       Value *Arg = CI->getOperand(1);
202       assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
203       CI->eraseFromParent();
204       if (Arg->use_empty())
205         if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
206           DeadGlobals.push_back(GV);
207     }
208     RegionEnd->eraseFromParent();
209   }
210   if (Declare) {
211     while (!Declare->use_empty()) {
212       CallInst *CI = cast<CallInst>(Declare->use_back());
213       Value *Arg = CI->getOperand(2);
214       assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
215       CI->eraseFromParent();
216       if (Arg->use_empty())
217         if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
218           DeadGlobals.push_back(GV);
219     }
220     Declare->eraseFromParent();
221   }
222
223   // llvm.dbg.compile_units and llvm.dbg.subprograms are marked as linkonce
224   // but since we are removing all debug information, make them internal now.
225   if (Constant *C = M.getNamedGlobal("llvm.dbg.compile_units"))
226     if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
227       GV->setLinkage(GlobalValue::InternalLinkage);
228
229   if (Constant *C = M.getNamedGlobal("llvm.dbg.subprograms"))
230     if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
231       GV->setLinkage(GlobalValue::InternalLinkage);
232
233   // Delete all dbg variables.
234   const Type *DbgVTy = M.getTypeByName("llvm.dbg.variable.type");
235   const Type *DbgGVTy = M.getTypeByName("llvm.dbg.global_variable.type");
236   if (DbgVTy || DbgGVTy)
237     for (Module::global_iterator I = M.global_begin(), E = M.global_end(); 
238          I != E; ++I) 
239       if (GlobalVariable *GV = dyn_cast<GlobalVariable>(I))
240         if (GV->hasName() && GV->use_empty()
241             && !strncmp(GV->getNameStart(), "llvm.dbg", 8)
242             && (GV->getType()->getElementType() == DbgVTy
243                 || GV->getType()->getElementType() == DbgGVTy))
244           DeadGlobals.push_back(GV);
245
246   // Delete any internal globals that were only used by the debugger intrinsics.
247   while (!DeadGlobals.empty()) {
248     GlobalVariable *GV = DeadGlobals.back();
249     DeadGlobals.pop_back();
250     if (GV->hasInternalLinkage())
251       RemoveDeadConstant(GV);
252   }
253
254   // Remove all llvm.dbg types.
255   TypeSymbolTable &ST = M.getTypeSymbolTable();
256   TypeSymbolTable::iterator TI = ST.begin();
257   TypeSymbolTable::iterator TE = ST.end();
258   while ( TI != TE ) {
259     const std::string &Name = TI->first;
260     if (!strncmp(Name.c_str(), "llvm.dbg.", 9))
261       ST.remove(TI++);
262     else 
263       ++TI;
264   }
265   
266   return true;
267 }