Handle attribute(used) global variables that are i8.
[oota-llvm.git] / lib / Transforms / IPO / StripSymbols.cpp
1 //===- StripSymbols.cpp - Strip symbols and debug info from a module ------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // The StripSymbols transformation implements code stripping. Specifically, it
11 // can delete:
12 // 
13 //   * names for virtual registers
14 //   * symbols for internal globals and functions
15 //   * debug information
16 //
17 // Note that this transformation makes code much less readable, so it should
18 // only be used in situations where the 'strip' utility would be used, such as
19 // reducing code size or making it harder to reverse engineer code.
20 //
21 //===----------------------------------------------------------------------===//
22
23 #include "llvm/Transforms/IPO.h"
24 #include "llvm/Constants.h"
25 #include "llvm/DerivedTypes.h"
26 #include "llvm/Instructions.h"
27 #include "llvm/Module.h"
28 #include "llvm/Pass.h"
29 #include "llvm/ValueSymbolTable.h"
30 #include "llvm/TypeSymbolTable.h"
31 #include "llvm/Support/Compiler.h"
32 #include "llvm/ADT/SmallPtrSet.h"
33 using namespace llvm;
34
35 namespace {
36   class VISIBILITY_HIDDEN StripSymbols : public ModulePass {
37     bool OnlyDebugInfo;
38   public:
39     static char ID; // Pass identification, replacement for typeid
40     explicit StripSymbols(bool ODI = false) 
41       : ModulePass((intptr_t)&ID), OnlyDebugInfo(ODI) {}
42
43     virtual bool runOnModule(Module &M);
44
45     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
46       AU.setPreservesAll();
47     }
48   };
49
50   char StripSymbols::ID = 0;
51   RegisterPass<StripSymbols> X("strip", "Strip all symbols from a module");
52 }
53
54 ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
55   return new StripSymbols(OnlyDebugInfo);
56 }
57
58 static void RemoveDeadConstant(Constant *C) {
59   assert(C->use_empty() && "Constant is not dead!");
60   std::vector<Constant*> Operands;
61   for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
62     if (isa<DerivedType>(C->getOperand(i)->getType()) &&
63         C->getOperand(i)->hasOneUse())
64       Operands.push_back(C->getOperand(i));
65   if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
66     if (!GV->hasInternalLinkage()) return;   // Don't delete non static globals.
67     GV->eraseFromParent();
68   }
69   else if (!isa<Function>(C))
70     C->destroyConstant();
71
72   // If the constant referenced anything, see if we can delete it as well.
73   while (!Operands.empty()) {
74     RemoveDeadConstant(Operands.back());
75     Operands.pop_back();
76   }
77 }
78
79 // Strip the symbol table of its names.
80 //
81 static void StripSymtab(ValueSymbolTable &ST) {
82   for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
83     Value *V = VI->getValue();
84     ++VI;
85     if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasInternalLinkage()) {
86       // Set name to "", removing from symbol table!
87       V->setName("");
88     }
89   }
90 }
91
92 // Strip the symbol table of its names.
93 static void StripTypeSymtab(TypeSymbolTable &ST) {
94   for (TypeSymbolTable::iterator TI = ST.begin(), E = ST.end(); TI != E; )
95     ST.remove(TI++);
96 }
97
98
99
100 bool StripSymbols::runOnModule(Module &M) {
101   // If we're not just stripping debug info, strip all symbols from the
102   // functions and the names from any internal globals.
103   if (!OnlyDebugInfo) {
104     SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
105     if (GlobalVariable *LLVMUsed = M.getGlobalVariable("llvm.used")) {
106       llvmUsedValues.insert(LLVMUsed);
107       // Collect values that are preserved as per explicit request.
108       // llvm.used is used to list these values.
109       if (ConstantArray *Inits = 
110             dyn_cast<ConstantArray>(LLVMUsed->getInitializer())) {
111         for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i) {
112           if (GlobalValue *GV = dyn_cast<GlobalValue>(Inits->getOperand(i)))
113             llvmUsedValues.insert(GV);
114           else if (ConstantExpr *CE =
115                        dyn_cast<ConstantExpr>(Inits->getOperand(i)))
116             if (CE->getOpcode() == Instruction::BitCast)
117               if (GlobalValue *GV = dyn_cast<GlobalValue>(CE->getOperand(0)))
118                 llvmUsedValues.insert(GV);
119         }
120       }
121     }
122
123     for (Module::global_iterator I = M.global_begin(), E = M.global_end();
124          I != E; ++I) {
125       if (I->hasInternalLinkage() && llvmUsedValues.count(I) == 0)
126         I->setName("");     // Internal symbols can't participate in linkage
127     }
128
129     for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
130       if (I->hasInternalLinkage() && llvmUsedValues.count(I) == 0)
131         I->setName("");     // Internal symbols can't participate in linkage
132       StripSymtab(I->getValueSymbolTable());
133     }
134     
135     // Remove all names from types.
136     StripTypeSymtab(M.getTypeSymbolTable());
137   }
138
139   // Strip debug info in the module if it exists.  To do this, we remove
140   // llvm.dbg.func.start, llvm.dbg.stoppoint, and llvm.dbg.region.end calls, and
141   // any globals they point to if now dead.
142   Function *FuncStart = M.getFunction("llvm.dbg.func.start");
143   Function *StopPoint = M.getFunction("llvm.dbg.stoppoint");
144   Function *RegionStart = M.getFunction("llvm.dbg.region.start");
145   Function *RegionEnd = M.getFunction("llvm.dbg.region.end");
146   Function *Declare = M.getFunction("llvm.dbg.declare");
147   if (!FuncStart && !StopPoint && !RegionStart && !RegionEnd && !Declare)
148     return true;
149
150   std::vector<GlobalVariable*> DeadGlobals;
151
152   // Remove all of the calls to the debugger intrinsics, and remove them from
153   // the module.
154   if (FuncStart) {
155     while (!FuncStart->use_empty()) {
156       CallInst *CI = cast<CallInst>(FuncStart->use_back());
157       Value *Arg = CI->getOperand(1);
158       assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
159       CI->eraseFromParent();
160       if (Arg->use_empty())
161         if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
162           DeadGlobals.push_back(GV);
163     }
164     FuncStart->eraseFromParent();
165   }
166   if (StopPoint) {
167     while (!StopPoint->use_empty()) {
168       CallInst *CI = cast<CallInst>(StopPoint->use_back());
169       Value *Arg = CI->getOperand(3);
170       assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
171       CI->eraseFromParent();
172       if (Arg->use_empty())
173         if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
174           DeadGlobals.push_back(GV);
175     }
176     StopPoint->eraseFromParent();
177   }
178   if (RegionStart) {
179     while (!RegionStart->use_empty()) {
180       CallInst *CI = cast<CallInst>(RegionStart->use_back());
181       Value *Arg = CI->getOperand(1);
182       assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
183       CI->eraseFromParent();
184       if (Arg->use_empty())
185         if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
186           DeadGlobals.push_back(GV);
187     }
188     RegionStart->eraseFromParent();
189   }
190   if (RegionEnd) {
191     while (!RegionEnd->use_empty()) {
192       CallInst *CI = cast<CallInst>(RegionEnd->use_back());
193       Value *Arg = CI->getOperand(1);
194       assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
195       CI->eraseFromParent();
196       if (Arg->use_empty())
197         if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
198           DeadGlobals.push_back(GV);
199     }
200     RegionEnd->eraseFromParent();
201   }
202   if (Declare) {
203     while (!Declare->use_empty()) {
204       CallInst *CI = cast<CallInst>(Declare->use_back());
205       Value *Arg = CI->getOperand(2);
206       assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
207       CI->eraseFromParent();
208       if (Arg->use_empty())
209         if (GlobalVariable *GV = dyn_cast<GlobalVariable>(Arg))
210           DeadGlobals.push_back(GV);
211     }
212     Declare->eraseFromParent();
213   }
214
215   // Finally, delete any internal globals that were only used by the debugger
216   // intrinsics.
217   while (!DeadGlobals.empty()) {
218     GlobalVariable *GV = DeadGlobals.back();
219     DeadGlobals.pop_back();
220     if (GV->hasInternalLinkage())
221       RemoveDeadConstant(GV);
222   }
223
224   return true;
225 }