Fix major problem with PHI node traversal: rechecking PHIs should go into
[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/SymbolTable.h"
20 #include "llvm/Pass.h"
21
22 static bool StripSymbolTable(SymbolTable &SymTab) {
23   bool RemovedSymbol = false;
24
25   for (SymbolTable::iterator I = SymTab.begin(); I != SymTab.end(); ++I) {
26     std::map<const std::string, Value *> &Plane = I->second;
27     
28     SymbolTable::type_iterator B;
29     while ((B = Plane.begin()) != Plane.end()) {   // Found nonempty type plane!
30       Value *V = B->second;
31       if (isa<Constant>(V) || isa<Type>(V))
32         SymTab.type_remove(B);
33       else 
34         V->setName("", &SymTab);  // Set name to "", removing from symbol table!
35       RemovedSymbol = true;
36       assert(Plane.begin() != B && "Symbol not removed from table!");
37     }
38   }
39  
40   return RemovedSymbol;
41 }
42
43 namespace {
44   struct SymbolStripping : public FunctionPass {
45     virtual bool runOnFunction(Function &F) {
46       return StripSymbolTable(F.getSymbolTable());
47     }
48     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
49       AU.setPreservesAll();
50     }
51   };
52   RegisterOpt<SymbolStripping> X("strip", "Strip symbols from functions");
53
54   struct FullSymbolStripping : public SymbolStripping {
55     virtual bool doInitialization(Module &M) {
56       return StripSymbolTable(M.getSymbolTable());
57     }
58   };
59   RegisterOpt<FullSymbolStripping> Y("mstrip",
60                                      "Strip symbols from module and functions");
61 }
62
63 Pass *createSymbolStrippingPass() {
64   return new SymbolStripping();
65 }
66
67 Pass *createFullSymbolStrippingPass() {
68   return new FullSymbolStripping();
69 }