blob: 9e058e7939d115cf8f2ab13a2476f6c1845a90c3 [file] [log] [blame]
Chris Lattner2fbfdcf2002-04-07 20:49:59 +00001//===- SymbolStripping.cpp - Strip symbols for functions and modules ------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
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//===----------------------------------------------------------------------===//
Chris Lattner00950542001-06-06 20:29:01 +00009//
10// This file implements stripping symbols out of symbol tables.
11//
12// Specifically, this allows you to strip all of the symbols out of:
Chris Lattner2fbfdcf2002-04-07 20:49:59 +000013// * A function
14// * All functions in a module
15// * All symbols in a module (all function symbols + all module scope symbols)
Chris Lattner00950542001-06-06 20:29:01 +000016//
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
Chris Lattner022103b2002-05-07 20:03:00 +000024#include "llvm/Transforms/Scalar.h"
Chris Lattner00950542001-06-06 20:29:01 +000025#include "llvm/Module.h"
Chris Lattner00950542001-06-06 20:29:01 +000026#include "llvm/SymbolTable.h"
Chris Lattnerbd0ef772002-02-26 21:46:54 +000027#include "llvm/Pass.h"
Chris Lattner00950542001-06-06 20:29:01 +000028
Brian Gaeked0fde302003-11-11 22:41:34 +000029namespace llvm {
30
Chris Lattner6e6026b2002-11-20 18:36:02 +000031static bool StripSymbolTable(SymbolTable &SymTab) {
Chris Lattner00950542001-06-06 20:29:01 +000032 bool RemovedSymbol = false;
33
Chris Lattner6e6026b2002-11-20 18:36:02 +000034 for (SymbolTable::iterator I = SymTab.begin(); I != SymTab.end(); ++I) {
Chris Lattner697954c2002-01-20 22:54:45 +000035 std::map<const std::string, Value *> &Plane = I->second;
Chris Lattner00950542001-06-06 20:29:01 +000036
Chris Lattner13b1f0c2001-09-07 16:43:50 +000037 SymbolTable::type_iterator B;
Chris Lattner00950542001-06-06 20:29:01 +000038 while ((B = Plane.begin()) != Plane.end()) { // Found nonempty type plane!
Chris Lattner13b1f0c2001-09-07 16:43:50 +000039 Value *V = B->second;
Chris Lattnere9bb2df2001-12-03 22:26:30 +000040 if (isa<Constant>(V) || isa<Type>(V))
Chris Lattner6e6026b2002-11-20 18:36:02 +000041 SymTab.type_remove(B);
Chris Lattner13b1f0c2001-09-07 16:43:50 +000042 else
Chris Lattner6e6026b2002-11-20 18:36:02 +000043 V->setName("", &SymTab); // Set name to "", removing from symbol table!
Chris Lattner00950542001-06-06 20:29:01 +000044 RemovedSymbol = true;
Chris Lattner13b1f0c2001-09-07 16:43:50 +000045 assert(Plane.begin() != B && "Symbol not removed from table!");
Chris Lattner00950542001-06-06 20:29:01 +000046 }
47 }
48
49 return RemovedSymbol;
50}
51
Chris Lattnerbd0ef772002-02-26 21:46:54 +000052namespace {
Chris Lattnerf57b8452002-04-27 06:56:12 +000053 struct SymbolStripping : public FunctionPass {
Chris Lattner7e708292002-06-25 16:13:24 +000054 virtual bool runOnFunction(Function &F) {
55 return StripSymbolTable(F.getSymbolTable());
Chris Lattnerbd0ef772002-02-26 21:46:54 +000056 }
Chris Lattner97e52e42002-04-28 21:27:06 +000057 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
58 AU.setPreservesAll();
59 }
Chris Lattnerbd0ef772002-02-26 21:46:54 +000060 };
Chris Lattnera6275cc2002-07-26 21:12:46 +000061 RegisterOpt<SymbolStripping> X("strip", "Strip symbols from functions");
Chris Lattnerbd0ef772002-02-26 21:46:54 +000062
63 struct FullSymbolStripping : public SymbolStripping {
Chris Lattner7e708292002-06-25 16:13:24 +000064 virtual bool doInitialization(Module &M) {
65 return StripSymbolTable(M.getSymbolTable());
Chris Lattnerbd0ef772002-02-26 21:46:54 +000066 }
67 };
Chris Lattnera6275cc2002-07-26 21:12:46 +000068 RegisterOpt<FullSymbolStripping> Y("mstrip",
69 "Strip symbols from module and functions");
Chris Lattnerbd0ef772002-02-26 21:46:54 +000070}
71
72Pass *createSymbolStrippingPass() {
73 return new SymbolStripping();
74}
75
76Pass *createFullSymbolStrippingPass() {
77 return new FullSymbolStripping();
78}
Brian Gaeked0fde302003-11-11 22:41:34 +000079
80} // End llvm namespace