Fix PR66 & ScalarRepl/2003-10-29-ArrayProblem.ll
[oota-llvm.git] / lib / Transforms / Scalar / SymbolStripping.cpp
1 //===- SymbolStripping.cpp - Strip symbols for functions and modules ------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements stripping symbols out of symbol tables.
11 //
12 // Specifically, this allows you to strip all of the symbols out of:
13 //   * A function
14 //   * All functions in a module
15 //   * All symbols in a module (all function symbols + all module scope symbols)
16 //
17 // Notice that:
18 //   * This pass makes code much less readable, so it should only be used in
19 //     situations where the 'strip' utility would be used (such as reducing 
20 //     code size, and making it harder to reverse engineer code).
21 //
22 //===----------------------------------------------------------------------===//
23
24 #include "llvm/Transforms/Scalar.h"
25 #include "llvm/Module.h"
26 #include "llvm/SymbolTable.h"
27 #include "llvm/Pass.h"
28
29 static bool StripSymbolTable(SymbolTable &SymTab) {
30   bool RemovedSymbol = false;
31
32   for (SymbolTable::iterator I = SymTab.begin(); I != SymTab.end(); ++I) {
33     std::map<const std::string, Value *> &Plane = I->second;
34     
35     SymbolTable::type_iterator B;
36     while ((B = Plane.begin()) != Plane.end()) {   // Found nonempty type plane!
37       Value *V = B->second;
38       if (isa<Constant>(V) || isa<Type>(V))
39         SymTab.type_remove(B);
40       else 
41         V->setName("", &SymTab);  // Set name to "", removing from symbol table!
42       RemovedSymbol = true;
43       assert(Plane.begin() != B && "Symbol not removed from table!");
44     }
45   }
46  
47   return RemovedSymbol;
48 }
49
50 namespace {
51   struct SymbolStripping : public FunctionPass {
52     virtual bool runOnFunction(Function &F) {
53       return StripSymbolTable(F.getSymbolTable());
54     }
55     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
56       AU.setPreservesAll();
57     }
58   };
59   RegisterOpt<SymbolStripping> X("strip", "Strip symbols from functions");
60
61   struct FullSymbolStripping : public SymbolStripping {
62     virtual bool doInitialization(Module &M) {
63       return StripSymbolTable(M.getSymbolTable());
64     }
65   };
66   RegisterOpt<FullSymbolStripping> Y("mstrip",
67                                      "Strip symbols from module and functions");
68 }
69
70 Pass *createSymbolStrippingPass() {
71   return new SymbolStripping();
72 }
73
74 Pass *createFullSymbolStrippingPass() {
75   return new FullSymbolStripping();
76 }