Move stuff out of the Optimizations directories into the appropriate Transforms
[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
22 static bool StripSymbolTable(SymbolTable *SymTab) {
23   if (SymTab == 0) return false;    // No symbol table?  No problem.
24   bool RemovedSymbol = false;
25
26   for (SymbolTable::iterator I = SymTab->begin(); I != SymTab->end(); ++I) {
27     std::map<const std::string, Value *> &Plane = I->second;
28     
29     SymbolTable::type_iterator B;
30     while ((B = Plane.begin()) != Plane.end()) {   // Found nonempty type plane!
31       Value *V = B->second;
32       if (isa<Constant>(V) || isa<Type>(V))
33         SymTab->type_remove(B);
34       else 
35         V->setName("", SymTab);   // Set name to "", removing from symbol table!
36       RemovedSymbol = true;
37       assert(Plane.begin() != B && "Symbol not removed from table!");
38     }
39   }
40  
41   return RemovedSymbol;
42 }
43
44
45 // DoSymbolStripping - Remove all symbolic information from a method
46 //
47 bool SymbolStripping::doSymbolStripping(Method *M) {
48   return StripSymbolTable(M->getSymbolTable());
49 }
50
51 // doStripGlobalSymbols - Remove all symbolic information from all methods 
52 // in a module, and all module level symbols. (method names, etc...)
53 //
54 bool FullSymbolStripping::doStripGlobalSymbols(Module *M) {
55   // Remove all symbols from methods in this module... and then strip all of the
56   // symbols in this module...
57   //  
58   return StripSymbolTable(M->getSymbolTable());
59 }