Fix -strip-debug-declare to work when there are
[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/Transforms/Utils/Local.h"
32 #include "llvm/Support/Compiler.h"
33 #include "llvm/ADT/SmallPtrSet.h"
34 using namespace llvm;
35
36 namespace {
37   class VISIBILITY_HIDDEN StripSymbols : public ModulePass {
38     bool OnlyDebugInfo;
39   public:
40     static char ID; // Pass identification, replacement for typeid
41     explicit StripSymbols(bool ODI = false) 
42       : ModulePass(&ID), OnlyDebugInfo(ODI) {}
43
44     virtual bool runOnModule(Module &M);
45
46     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
47       AU.setPreservesAll();
48     }
49   };
50
51   class VISIBILITY_HIDDEN StripNonDebugSymbols : public ModulePass {
52   public:
53     static char ID; // Pass identification, replacement for typeid
54     explicit StripNonDebugSymbols()
55       : ModulePass(&ID) {}
56
57     virtual bool runOnModule(Module &M);
58
59     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
60       AU.setPreservesAll();
61     }
62   };
63
64   class VISIBILITY_HIDDEN StripDebugDeclare : public ModulePass {
65   public:
66     static char ID; // Pass identification, replacement for typeid
67     explicit StripDebugDeclare()
68       : ModulePass(&ID) {}
69
70     virtual bool runOnModule(Module &M);
71
72     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
73       AU.setPreservesAll();
74     }
75   };
76 }
77
78 char StripSymbols::ID = 0;
79 static RegisterPass<StripSymbols>
80 X("strip", "Strip all symbols from a module");
81
82 ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
83   return new StripSymbols(OnlyDebugInfo);
84 }
85
86 char StripNonDebugSymbols::ID = 0;
87 static RegisterPass<StripNonDebugSymbols>
88 Y("strip-nondebug", "Strip all symbols, except dbg symbols, from a module");
89
90 ModulePass *llvm::createStripNonDebugSymbolsPass() {
91   return new StripNonDebugSymbols();
92 }
93
94 char StripDebugDeclare::ID = 0;
95 static RegisterPass<StripDebugDeclare>
96 Z("strip-debug-declare", "Strip all llvm.dbg.declare intrinsics");
97
98 ModulePass *llvm::createStripDebugDeclarePass() {
99   return new StripDebugDeclare();
100 }
101
102 /// OnlyUsedBy - Return true if V is only used by Usr.
103 static bool OnlyUsedBy(Value *V, Value *Usr) {
104   for(Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
105     User *U = *I;
106     if (U != Usr)
107       return false;
108   }
109   return true;
110 }
111
112 static void RemoveDeadConstant(Constant *C) {
113   assert(C->use_empty() && "Constant is not dead!");
114   SmallPtrSet<Constant *, 4> Operands;
115   for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
116     if (isa<DerivedType>(C->getOperand(i)->getType()) &&
117         OnlyUsedBy(C->getOperand(i), C)) 
118       Operands.insert(C->getOperand(i));
119   if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
120     if (!GV->hasLocalLinkage()) return;   // Don't delete non static globals.
121     GV->eraseFromParent();
122   }
123   else if (!isa<Function>(C))
124     if (isa<CompositeType>(C->getType()))
125       C->destroyConstant();
126
127   // If the constant referenced anything, see if we can delete it as well.
128   for (SmallPtrSet<Constant *, 4>::iterator OI = Operands.begin(),
129          OE = Operands.end(); OI != OE; ++OI)
130     RemoveDeadConstant(*OI);
131 }
132
133 // Strip the symbol table of its names.
134 //
135 static void StripSymtab(ValueSymbolTable &ST, bool PreserveDbgInfo) {
136   for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
137     Value *V = VI->getValue();
138     ++VI;
139     if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasLocalLinkage()) {
140       if (!PreserveDbgInfo || strncmp(V->getNameStart(), "llvm.dbg", 8))
141         // Set name to "", removing from symbol table!
142         V->setName("");
143     }
144   }
145 }
146
147 // Strip the symbol table of its names.
148 static void StripTypeSymtab(TypeSymbolTable &ST, bool PreserveDbgInfo) {
149   for (TypeSymbolTable::iterator TI = ST.begin(), E = ST.end(); TI != E; ) {
150     if (PreserveDbgInfo && strncmp(TI->first.c_str(), "llvm.dbg", 8) == 0)
151       ++TI;
152     else
153       ST.remove(TI++);
154   }
155 }
156
157 /// Find values that are marked as llvm.used.
158 void findUsedValues(Module &M,
159                     SmallPtrSet<const GlobalValue*, 8>& llvmUsedValues) {
160   if (GlobalVariable *LLVMUsed = M.getGlobalVariable("llvm.used")) {
161     llvmUsedValues.insert(LLVMUsed);
162     // Collect values that are preserved as per explicit request.
163     // llvm.used is used to list these values.
164     if (ConstantArray *Inits = 
165         dyn_cast<ConstantArray>(LLVMUsed->getInitializer())) {
166       for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i) {
167         if (GlobalValue *GV = dyn_cast<GlobalValue>(Inits->getOperand(i)))
168           llvmUsedValues.insert(GV);
169         else if (ConstantExpr *CE =
170                  dyn_cast<ConstantExpr>(Inits->getOperand(i)))
171           if (CE->getOpcode() == Instruction::BitCast)
172             if (GlobalValue *GV = dyn_cast<GlobalValue>(CE->getOperand(0)))
173               llvmUsedValues.insert(GV);
174       }
175     }
176   }
177 }
178
179 /// StripSymbolNames - Strip symbol names.
180 bool StripSymbolNames(Module &M, bool PreserveDbgInfo) {
181
182   SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
183   findUsedValues(M, llvmUsedValues);
184
185   for (Module::global_iterator I = M.global_begin(), E = M.global_end();
186        I != E; ++I) {
187     if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
188       if (!PreserveDbgInfo || strncmp(I->getNameStart(), "llvm.dbg", 8))
189         I->setName("");     // Internal symbols can't participate in linkage
190   }
191   
192   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
193     if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
194       if (!PreserveDbgInfo || strncmp(I->getNameStart(), "llvm.dbg", 8))
195         I->setName("");     // Internal symbols can't participate in linkage
196     StripSymtab(I->getValueSymbolTable(), PreserveDbgInfo);
197   }
198   
199   // Remove all names from types.
200   StripTypeSymtab(M.getTypeSymbolTable(), PreserveDbgInfo);
201
202   return true;
203 }
204
205 // StripDebugInfo - Strip debug info in the module if it exists.  
206 // To do this, we remove llvm.dbg.func.start, llvm.dbg.stoppoint, and 
207 // llvm.dbg.region.end calls, and any globals they point to if now dead.
208 bool StripDebugInfo(Module &M) {
209
210   SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
211   findUsedValues(M, llvmUsedValues);
212
213   // Delete all dbg variables.
214   for (Module::global_iterator I = M.global_begin(), E = M.global_end(); 
215        I != E; ++I) {
216     GlobalVariable *GV = dyn_cast<GlobalVariable>(I);
217     if (!GV) continue;
218     if (!GV->use_empty() && llvmUsedValues.count(I) == 0) {
219       if (strncmp(GV->getNameStart(), "llvm.dbg", 8) == 0) {
220         GV->replaceAllUsesWith(UndefValue::get(GV->getType()));
221       }
222     }
223   }
224
225   Function *FuncStart = M.getFunction("llvm.dbg.func.start");
226   Function *StopPoint = M.getFunction("llvm.dbg.stoppoint");
227   Function *RegionStart = M.getFunction("llvm.dbg.region.start");
228   Function *RegionEnd = M.getFunction("llvm.dbg.region.end");
229   Function *Declare = M.getFunction("llvm.dbg.declare");
230
231   std::vector<Constant*> DeadConstants;
232
233   // Remove all of the calls to the debugger intrinsics, and remove them from
234   // the module.
235   if (FuncStart) {
236     while (!FuncStart->use_empty()) {
237       CallInst *CI = cast<CallInst>(FuncStart->use_back());
238       Value *Arg = CI->getOperand(1);
239       assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
240       CI->eraseFromParent();
241       if (Arg->use_empty())
242         if (Constant *C = dyn_cast<Constant>(Arg)) 
243           DeadConstants.push_back(C);
244     }
245     FuncStart->eraseFromParent();
246   }
247   if (StopPoint) {
248     while (!StopPoint->use_empty()) {
249       CallInst *CI = cast<CallInst>(StopPoint->use_back());
250       Value *Arg = CI->getOperand(3);
251       assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
252       CI->eraseFromParent();
253       if (Arg->use_empty())
254         if (Constant *C = dyn_cast<Constant>(Arg)) 
255           DeadConstants.push_back(C);
256     }
257     StopPoint->eraseFromParent();
258   }
259   if (RegionStart) {
260     while (!RegionStart->use_empty()) {
261       CallInst *CI = cast<CallInst>(RegionStart->use_back());
262       Value *Arg = CI->getOperand(1);
263       assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
264       CI->eraseFromParent();
265       if (Arg->use_empty())
266         if (Constant *C = dyn_cast<Constant>(Arg)) 
267           DeadConstants.push_back(C);
268     }
269     RegionStart->eraseFromParent();
270   }
271   if (RegionEnd) {
272     while (!RegionEnd->use_empty()) {
273       CallInst *CI = cast<CallInst>(RegionEnd->use_back());
274       Value *Arg = CI->getOperand(1);
275       assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
276       CI->eraseFromParent();
277       if (Arg->use_empty())
278         if (Constant *C = dyn_cast<Constant>(Arg)) 
279           DeadConstants.push_back(C);
280     }
281     RegionEnd->eraseFromParent();
282   }
283   if (Declare) {
284     while (!Declare->use_empty()) {
285       CallInst *CI = cast<CallInst>(Declare->use_back());
286       Value *Arg1 = CI->getOperand(1);
287       Value *Arg2 = CI->getOperand(2);
288       assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
289       CI->eraseFromParent();
290       if (Arg1->use_empty()) {
291         if (Constant *C = dyn_cast<Constant>(Arg1)) 
292           DeadConstants.push_back(C);
293         else 
294           RecursivelyDeleteTriviallyDeadInstructions(Arg1, NULL);
295       }
296       if (Arg2->use_empty())
297         if (Constant *C = dyn_cast<Constant>(Arg2)) 
298           DeadConstants.push_back(C);
299     }
300     Declare->eraseFromParent();
301   }
302
303   // llvm.dbg.compile_units and llvm.dbg.subprograms are marked as linkonce
304   // but since we are removing all debug information, make them internal now.
305   // FIXME: Use private linkage maybe?
306   if (Constant *C = M.getNamedGlobal("llvm.dbg.compile_units"))
307     if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
308       GV->setLinkage(GlobalValue::InternalLinkage);
309
310   if (Constant *C = M.getNamedGlobal("llvm.dbg.subprograms"))
311     if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
312       GV->setLinkage(GlobalValue::InternalLinkage);
313  
314   if (Constant *C = M.getNamedGlobal("llvm.dbg.global_variables"))
315     if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
316       GV->setLinkage(GlobalValue::InternalLinkage);
317
318   // Delete all dbg variables.
319   for (Module::global_iterator I = M.global_begin(), E = M.global_end(); 
320        I != E; ++I) {
321     GlobalVariable *GV = dyn_cast<GlobalVariable>(I);
322     if (!GV) continue;
323     if (GV->use_empty() && llvmUsedValues.count(I) == 0
324         && (!GV->hasSection() 
325             || strcmp(GV->getSection().c_str(), "llvm.metadata") == 0))
326       DeadConstants.push_back(GV);
327   }
328
329   if (DeadConstants.empty())
330     return false;
331
332   // Delete any internal globals that were only used by the debugger intrinsics.
333   while (!DeadConstants.empty()) {
334     Constant *C = DeadConstants.back();
335     DeadConstants.pop_back();
336     if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
337       if (GV->hasLocalLinkage())
338         RemoveDeadConstant(GV);
339     }
340     else
341       RemoveDeadConstant(C);
342   }
343
344   // Remove all llvm.dbg types.
345   TypeSymbolTable &ST = M.getTypeSymbolTable();
346   for (TypeSymbolTable::iterator TI = ST.begin(), TE = ST.end(); TI != TE; ) {
347     if (!strncmp(TI->first.c_str(), "llvm.dbg.", 9))
348       ST.remove(TI++);
349     else 
350       ++TI;
351   }
352   
353   return true;
354 }
355
356 bool StripSymbols::runOnModule(Module &M) {
357   bool Changed = false;
358   Changed |= StripDebugInfo(M);
359   if (!OnlyDebugInfo)
360     Changed |= StripSymbolNames(M, false);
361   return Changed;
362 }
363
364 bool StripNonDebugSymbols::runOnModule(Module &M) {
365   return StripSymbolNames(M, true);
366 }
367
368 bool StripDebugDeclare::runOnModule(Module &M) {
369
370   Function *Declare = M.getFunction("llvm.dbg.declare");
371   std::vector<Constant*> DeadConstants;
372
373   if (Declare) {
374     while (!Declare->use_empty()) {
375       CallInst *CI = cast<CallInst>(Declare->use_back());
376       Value *Arg1 = CI->getOperand(1);
377       Value *Arg2 = CI->getOperand(2);
378       assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
379       CI->eraseFromParent();
380       if (Arg1->use_empty()) {
381         if (Constant *C = dyn_cast<Constant>(Arg1)) 
382           DeadConstants.push_back(C);
383         else 
384           RecursivelyDeleteTriviallyDeadInstructions(Arg1, NULL);
385       }
386       if (Arg2->use_empty())
387         if (Constant *C = dyn_cast<Constant>(Arg2)) 
388           DeadConstants.push_back(C);
389     }
390     Declare->eraseFromParent();
391   }
392
393   // Delete all llvm.dbg.global_variables.
394   for (Module::global_iterator I = M.global_begin(), E = M.global_end(); 
395        I != E; ++I) {
396     GlobalVariable *GV = dyn_cast<GlobalVariable>(I);
397     if (!GV) continue;
398     if (GV->use_empty() && GV->hasName() 
399         && strncmp(GV->getNameStart(), "llvm.dbg.global_variable", 24) == 0)
400       DeadConstants.push_back(GV);
401   }
402
403   while (!DeadConstants.empty()) {
404     Constant *C = DeadConstants.back();
405     DeadConstants.pop_back();
406     if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
407       if (GV->hasLocalLinkage())
408         RemoveDeadConstant(GV);
409     }
410     else
411       RemoveDeadConstant(C);
412   }
413
414   return true;
415 }