Change over to use new style pass mechanism, now passes only expose small
[oota-llvm.git] / lib / Transforms / Scalar / SymbolStripping.cpp
1 //===- SymbolStripping.cpp - Code to string symbols for methods 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 method
7 //   * All methods in a module
8 //   * All symbols in a module (all method 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/SymbolStripping.h"
18 #include "llvm/Module.h"
19 #include "llvm/Method.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 method
47 //
48 static bool doSymbolStripping(Method *M) {
49   return StripSymbolTable(M->getSymbolTable());
50 }
51
52 // doStripGlobalSymbols - Remove all symbolic information from all methods 
53 // in a module, and all module level symbols. (method names, etc...)
54 //
55 static bool doStripGlobalSymbols(Module *M) {
56   // Remove all symbols from methods in this module... and then strip all of the
57   // symbols in this module...
58   //  
59   return StripSymbolTable(M->getSymbolTable());
60 }
61
62 namespace {
63   struct SymbolStripping : public MethodPass {
64     virtual bool runOnMethod(Method *M) {
65       return doSymbolStripping(M);
66     }
67   };
68
69   struct FullSymbolStripping : public SymbolStripping {
70     virtual bool doInitialization(Module *M) {
71       return doStripGlobalSymbols(M);
72     }
73   };
74 }
75
76 Pass *createSymbolStrippingPass() {
77   return new SymbolStripping();
78 }
79
80 Pass *createFullSymbolStrippingPass() {
81   return new FullSymbolStripping();
82 }