* Standardize how analysis results/passes as printed with the print() virtual
[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 namespace {
46   struct SymbolStripping : public FunctionPass {
47     virtual bool runOnFunction(Function &F) {
48       return StripSymbolTable(F.getSymbolTable());
49     }
50     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
51       AU.setPreservesAll();
52     }
53   };
54   RegisterOpt<SymbolStripping> X("strip", "Strip symbols from functions");
55
56   struct FullSymbolStripping : public SymbolStripping {
57     virtual bool doInitialization(Module &M) {
58       return StripSymbolTable(M.getSymbolTable());
59     }
60   };
61   RegisterOpt<FullSymbolStripping> Y("mstrip",
62                                      "Strip symbols from module and functions");
63 }
64
65 Pass *createSymbolStrippingPass() {
66   return new SymbolStripping();
67 }
68
69 Pass *createFullSymbolStrippingPass() {
70   return new FullSymbolStripping();
71 }