Avoid deleting individual instructions until AFTER dead blocks have dropped
[oota-llvm.git] / lib / Transforms / Scalar / SymbolStripping.cpp
1 //===- SymbolStripping.cpp - Strip symbols for functions and modules ------===//
2 //
3 // This file implements stripping symbols out of symbol tables.
4 //
5 // Specifically, this allows you to strip all of the symbols out of:
6 //   * A function
7 //   * All functions in a module
8 //   * All symbols in a module (all function symbols + all module scope symbols)
9 //
10 // Notice that:
11 //   * This pass makes code much less readable, so it should only be used in
12 //     situations where the 'strip' utility would be used (such as reducing 
13 //     code size, and making it harder to reverse engineer code).
14 //
15 //===----------------------------------------------------------------------===//
16
17 #include "llvm/Transforms/Scalar.h"
18 #include "llvm/Module.h"
19 #include "llvm/Function.h"
20 #include "llvm/SymbolTable.h"
21 #include "llvm/Pass.h"
22
23 static bool StripSymbolTable(SymbolTable *SymTab) {
24   if (SymTab == 0) return false;    // No symbol table?  No problem.
25   bool RemovedSymbol = false;
26
27   for (SymbolTable::iterator I = SymTab->begin(); I != SymTab->end(); ++I) {
28     std::map<const std::string, Value *> &Plane = I->second;
29     
30     SymbolTable::type_iterator B;
31     while ((B = Plane.begin()) != Plane.end()) {   // Found nonempty type plane!
32       Value *V = B->second;
33       if (isa<Constant>(V) || isa<Type>(V))
34         SymTab->type_remove(B);
35       else 
36         V->setName("", SymTab);   // Set name to "", removing from symbol table!
37       RemovedSymbol = true;
38       assert(Plane.begin() != B && "Symbol not removed from table!");
39     }
40   }
41  
42   return RemovedSymbol;
43 }
44
45
46 // DoSymbolStripping - Remove all symbolic information from a function
47 //
48 static bool doSymbolStripping(Function *F) {
49   return StripSymbolTable(F->getSymbolTable());
50 }
51
52 // doStripGlobalSymbols - Remove all symbolic information from all functions 
53 // in a module, and all module level symbols. (function names, etc...)
54 //
55 static bool doStripGlobalSymbols(Module *M) {
56   // Remove all symbols from functions in this module... and then strip all of
57   // the symbols in this module...
58   //  
59   return StripSymbolTable(M->getSymbolTable());
60 }
61
62 namespace {
63   struct SymbolStripping : public FunctionPass {
64     const char *getPassName() const { return "Strip Symbols from Functions"; }
65
66     virtual bool runOnFunction(Function *F) {
67       return doSymbolStripping(F);
68     }
69     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
70       AU.setPreservesAll();
71     }
72   };
73
74   struct FullSymbolStripping : public SymbolStripping {
75     const char *getPassName() const { return "Strip Symbols from Module"; }
76     virtual bool doInitialization(Module *M) {
77       return doStripGlobalSymbols(M);
78     }
79   };
80 }
81
82 Pass *createSymbolStrippingPass() {
83   return new SymbolStripping();
84 }
85
86 Pass *createFullSymbolStrippingPass() {
87   return new FullSymbolStripping();
88 }