StripSymbols.cpp revision 103d42ff0f73f10b6306e293e7563a9e7e512c01
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/Analysis/DebugInfo.h"
30#include "llvm/ValueSymbolTable.h"
31#include "llvm/TypeSymbolTable.h"
32#include "llvm/Transforms/Utils/Local.h"
33#include "llvm/ADT/SmallPtrSet.h"
34using namespace llvm;
35
36namespace {
37  class StripSymbols : public ModulePass {
38    bool OnlyDebugInfo;
39  public:
40    static char ID; // Pass identification, replacement for typeid
41    explicit StripSymbols(bool ODI = false)
42      : ModulePass(&ID), OnlyDebugInfo(ODI) {}
43
44    virtual bool runOnModule(Module &M);
45
46    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
47      AU.setPreservesAll();
48    }
49  };
50
51  class StripNonDebugSymbols : public ModulePass {
52  public:
53    static char ID; // Pass identification, replacement for typeid
54    explicit StripNonDebugSymbols()
55      : ModulePass(&ID) {}
56
57    virtual bool runOnModule(Module &M);
58
59    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
60      AU.setPreservesAll();
61    }
62  };
63
64  class StripDebugDeclare : public ModulePass {
65  public:
66    static char ID; // Pass identification, replacement for typeid
67    explicit StripDebugDeclare()
68      : ModulePass(&ID) {}
69
70    virtual bool runOnModule(Module &M);
71
72    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
73      AU.setPreservesAll();
74    }
75  };
76
77  class StripDeadDebugInfo : public ModulePass {
78  public:
79    static char ID; // Pass identification, replacement for typeid
80    explicit StripDeadDebugInfo()
81      : ModulePass(&ID) {}
82
83    virtual bool runOnModule(Module &M);
84
85    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
86      AU.setPreservesAll();
87    }
88  };
89}
90
91char StripSymbols::ID = 0;
92static RegisterPass<StripSymbols>
93X("strip", "Strip all symbols from a module");
94
95ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
96  return new StripSymbols(OnlyDebugInfo);
97}
98
99char StripNonDebugSymbols::ID = 0;
100static RegisterPass<StripNonDebugSymbols>
101Y("strip-nondebug", "Strip all symbols, except dbg symbols, from a module");
102
103ModulePass *llvm::createStripNonDebugSymbolsPass() {
104  return new StripNonDebugSymbols();
105}
106
107char StripDebugDeclare::ID = 0;
108static RegisterPass<StripDebugDeclare>
109Z("strip-debug-declare", "Strip all llvm.dbg.declare intrinsics");
110
111ModulePass *llvm::createStripDebugDeclarePass() {
112  return new StripDebugDeclare();
113}
114
115char StripDeadDebugInfo::ID = 0;
116static RegisterPass<StripDeadDebugInfo>
117A("strip-dead-debug-info", "Strip debug info for unused symbols");
118
119ModulePass *llvm::createStripDeadDebugInfoPass() {
120  return new StripDeadDebugInfo();
121}
122
123/// OnlyUsedBy - Return true if V is only used by Usr.
124static bool OnlyUsedBy(Value *V, Value *Usr) {
125  for(Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
126    User *U = *I;
127    if (U != Usr)
128      return false;
129  }
130  return true;
131}
132
133static void RemoveDeadConstant(Constant *C) {
134  assert(C->use_empty() && "Constant is not dead!");
135  SmallPtrSet<Constant*, 4> Operands;
136  for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
137    if (isa<DerivedType>(C->getOperand(i)->getType()) &&
138        OnlyUsedBy(C->getOperand(i), C))
139      Operands.insert(cast<Constant>(C->getOperand(i)));
140  if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
141    if (!GV->hasLocalLinkage()) return;   // Don't delete non static globals.
142    GV->eraseFromParent();
143  }
144  else if (!isa<Function>(C))
145    if (isa<CompositeType>(C->getType()))
146      C->destroyConstant();
147
148  // If the constant referenced anything, see if we can delete it as well.
149  for (SmallPtrSet<Constant*, 4>::iterator OI = Operands.begin(),
150         OE = Operands.end(); OI != OE; ++OI)
151    RemoveDeadConstant(*OI);
152}
153
154// Strip the symbol table of its names.
155//
156static void StripSymtab(ValueSymbolTable &ST, bool PreserveDbgInfo) {
157  for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
158    Value *V = VI->getValue();
159    ++VI;
160    if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasLocalLinkage()) {
161      if (!PreserveDbgInfo || !V->getName().startswith("llvm.dbg"))
162        // Set name to "", removing from symbol table!
163        V->setName("");
164    }
165  }
166}
167
168// Strip the symbol table of its names.
169static void StripTypeSymtab(TypeSymbolTable &ST, bool PreserveDbgInfo) {
170  for (TypeSymbolTable::iterator TI = ST.begin(), E = ST.end(); TI != E; ) {
171    if (PreserveDbgInfo && StringRef(TI->first).startswith("llvm.dbg"))
172      ++TI;
173    else
174      ST.remove(TI++);
175  }
176}
177
178/// Find values that are marked as llvm.used.
179static void findUsedValues(GlobalVariable *LLVMUsed,
180                           SmallPtrSet<const GlobalValue*, 8> &UsedValues) {
181  if (LLVMUsed == 0) return;
182  UsedValues.insert(LLVMUsed);
183
184  ConstantArray *Inits = dyn_cast<ConstantArray>(LLVMUsed->getInitializer());
185  if (Inits == 0) return;
186
187  for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
188    if (GlobalValue *GV =
189          dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
190      UsedValues.insert(GV);
191}
192
193/// StripSymbolNames - Strip symbol names.
194static bool StripSymbolNames(Module &M, bool PreserveDbgInfo) {
195
196  SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
197  findUsedValues(M.getGlobalVariable("llvm.used"), llvmUsedValues);
198  findUsedValues(M.getGlobalVariable("llvm.compiler.used"), llvmUsedValues);
199
200  for (Module::global_iterator I = M.global_begin(), E = M.global_end();
201       I != E; ++I) {
202    if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
203      if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg"))
204        I->setName("");     // Internal symbols can't participate in linkage
205  }
206
207  for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
208    if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
209      if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg"))
210        I->setName("");     // Internal symbols can't participate in linkage
211    StripSymtab(I->getValueSymbolTable(), PreserveDbgInfo);
212  }
213
214  // Remove all names from types.
215  StripTypeSymtab(M.getTypeSymbolTable(), PreserveDbgInfo);
216
217  return true;
218}
219
220// StripDebugInfo - Strip debug info in the module if it exists.
221// To do this, we remove llvm.dbg.func.start, llvm.dbg.stoppoint, and
222// llvm.dbg.region.end calls, and any globals they point to if now dead.
223static bool StripDebugInfo(Module &M) {
224
225  bool Changed = false;
226
227  // Remove all of the calls to the debugger intrinsics, and remove them from
228  // the module.
229  if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
230    while (!Declare->use_empty()) {
231      CallInst *CI = cast<CallInst>(Declare->use_back());
232      CI->eraseFromParent();
233    }
234    Declare->eraseFromParent();
235    Changed = true;
236  }
237
238  if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
239    while (!DbgVal->use_empty()) {
240      CallInst *CI = cast<CallInst>(DbgVal->use_back());
241      CI->eraseFromParent();
242    }
243    DbgVal->eraseFromParent();
244    Changed = true;
245  }
246
247  for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
248         NME = M.named_metadata_end(); NMI != NME;) {
249    NamedMDNode *NMD = NMI;
250    ++NMI;
251    if (NMD->getName().startswith("llvm.dbg.")) {
252      NMD->eraseFromParent();
253      Changed = true;
254    }
255  }
256
257  for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
258    for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE;
259         ++FI)
260      for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE;
261           ++BI) {
262        Changed |= !BI->getDebugLoc().isUnknown();
263        BI->setDebugLoc(DebugLoc());
264      }
265
266  return Changed;
267}
268
269bool StripSymbols::runOnModule(Module &M) {
270  bool Changed = false;
271  Changed |= StripDebugInfo(M);
272  if (!OnlyDebugInfo)
273    Changed |= StripSymbolNames(M, false);
274  return Changed;
275}
276
277bool StripNonDebugSymbols::runOnModule(Module &M) {
278  return StripSymbolNames(M, true);
279}
280
281bool StripDebugDeclare::runOnModule(Module &M) {
282
283  Function *Declare = M.getFunction("llvm.dbg.declare");
284  std::vector<Constant*> DeadConstants;
285
286  if (Declare) {
287    while (!Declare->use_empty()) {
288      CallInst *CI = cast<CallInst>(Declare->use_back());
289      Value *Arg1 = CI->getArgOperand(0);
290      Value *Arg2 = CI->getArgOperand(1);
291      assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
292      CI->eraseFromParent();
293      if (Arg1->use_empty()) {
294        if (Constant *C = dyn_cast<Constant>(Arg1))
295          DeadConstants.push_back(C);
296        else
297          RecursivelyDeleteTriviallyDeadInstructions(Arg1);
298      }
299      if (Arg2->use_empty())
300        if (Constant *C = dyn_cast<Constant>(Arg2))
301          DeadConstants.push_back(C);
302    }
303    Declare->eraseFromParent();
304  }
305
306  while (!DeadConstants.empty()) {
307    Constant *C = DeadConstants.back();
308    DeadConstants.pop_back();
309    if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
310      if (GV->hasLocalLinkage())
311        RemoveDeadConstant(GV);
312    } else
313      RemoveDeadConstant(C);
314  }
315
316  return true;
317}
318
319/// getRealLinkageName - If special LLVM prefix that is used to inform the asm
320/// printer to not emit usual symbol prefix before the symbol name is used then
321/// return linkage name after skipping this special LLVM prefix.
322static StringRef getRealLinkageName(StringRef LinkageName) {
323  char One = '\1';
324  if (LinkageName.startswith(StringRef(&One, 1)))
325    return LinkageName.substr(1);
326  return LinkageName;
327}
328
329bool StripDeadDebugInfo::runOnModule(Module &M) {
330  bool Changed = false;
331
332  // Debugging infomration is encoded in llvm IR using metadata. This is designed
333  // such a way that debug info for symbols preserved even if symbols are
334  // optimized away by the optimizer. This special pass removes debug info for
335  // such symbols.
336
337  // llvm.dbg.gv keeps track of debug info for global variables.
338  if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
339    SmallVector<MDNode *, 8> MDs;
340    for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
341      if (DIGlobalVariable(NMD->getOperand(i)).Verify())
342        MDs.push_back(NMD->getOperand(i));
343      else
344        Changed = true;
345    NMD->eraseFromParent();
346    NMD = NULL;
347
348    for (SmallVector<MDNode *, 8>::iterator I = MDs.begin(),
349           E = MDs.end(); I != E; ++I) {
350      if (M.getGlobalVariable(DIGlobalVariable(*I).getGlobal()->getName(),
351                              true)) {
352        if (!NMD)
353          NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
354        NMD->addOperand(*I);
355      }
356      else
357        Changed = true;
358    }
359  }
360
361  // llvm.dbg.sp keeps track of debug info for subprograms.
362  if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp")) {
363    SmallVector<MDNode *, 8> MDs;
364    for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
365      if (DISubprogram(NMD->getOperand(i)).Verify())
366        MDs.push_back(NMD->getOperand(i));
367      else
368        Changed = true;
369    NMD->eraseFromParent();
370    NMD = NULL;
371
372    for (SmallVector<MDNode *, 8>::iterator I = MDs.begin(),
373           E = MDs.end(); I != E; ++I) {
374      bool FnIsLive = false;
375      if (Function *F = DISubprogram(*I).getFunction())
376        if (M.getFunction(F->getName()))
377          FnIsLive = true;
378      if (FnIsLive) {
379          if (!NMD)
380            NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
381          NMD->addOperand(*I);
382      } else {
383        // Remove llvm.dbg.lv.fnname named mdnode which may have been used
384        // to hold debug info for dead function's local variables.
385        StringRef FName = DISubprogram(*I).getLinkageName();
386        if (FName.empty())
387          FName = DISubprogram(*I).getName();
388        if (NamedMDNode *LVNMD =
389            M.getNamedMetadata(Twine("llvm.dbg.lv.",
390                                     getRealLinkageName(FName))))
391          LVNMD->eraseFromParent();
392      }
393    }
394  }
395
396  return Changed;
397}
398