Spelling people's names right is kinda important
[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 //   * All functions in a module
14 //   * All non-essential symbols in a module (all function symbols + all module
15 //     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 using namespace llvm;
29
30 static bool StripSymbolTable(SymbolTable &SymTab) {
31   bool RemovedSymbol = false;
32
33   for (SymbolTable::iterator I = SymTab.begin(); I != SymTab.end();) {
34     // Removing items from the plane can cause the plane itself to get deleted.
35     // If this happens, make sure we incremented our plane iterator already!
36     std::map<const std::string, Value *> &Plane = (I++)->second;
37     
38     SymbolTable::type_iterator B = Plane.begin(), Bend = Plane.end();
39     while (B != Bend) {   // Found nonempty type plane!
40       Value *V = B->second;
41
42       if (isa<Constant>(V) || isa<Type>(V)) {
43         SymTab.type_remove(B++);
44         RemovedSymbol = true;
45       } else {
46         ++B;
47         if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasInternalLinkage()){
48           // Set name to "", removing from symbol table!
49           V->setName("", &SymTab);
50           RemovedSymbol = true;
51         }
52       }
53     }
54   }
55  
56   return RemovedSymbol;
57 }
58
59 namespace {
60   struct SymbolStripping : public FunctionPass {
61     virtual bool runOnFunction(Function &F) {
62       return StripSymbolTable(F.getSymbolTable());
63     }
64     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
65       AU.setPreservesAll();
66     }
67   };
68   RegisterOpt<SymbolStripping> X("strip", "Strip symbols from functions");
69
70   struct FullSymbolStripping : public SymbolStripping {
71     virtual bool doInitialization(Module &M) {
72       return StripSymbolTable(M.getSymbolTable());
73     }
74   };
75   RegisterOpt<FullSymbolStripping> Y("mstrip",
76                                      "Strip symbols from module and functions");
77 }
78
79 Pass *llvm::createSymbolStrippingPass() {
80   return new SymbolStripping();
81 }
82
83 Pass *llvm::createFullSymbolStrippingPass() {
84   return new FullSymbolStripping();
85 }