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