Land the long talked about "type system rewrite" patch. This
[llvm/stm8.git] / lib / Transforms / IPO / StripSymbols.cpp
blob5bacdf57fc21a66171381f63110f20dd687d077f
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
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.
21 //===----------------------------------------------------------------------===//
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/Transforms/Utils/Local.h"
32 #include "llvm/ADT/DenseMap.h"
33 #include "llvm/ADT/SmallPtrSet.h"
34 using namespace llvm;
36 namespace {
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 initializeStripSymbolsPass(*PassRegistry::getPassRegistry());
46 virtual bool runOnModule(Module &M);
48 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
49 AU.setPreservesAll();
53 class StripNonDebugSymbols : public ModulePass {
54 public:
55 static char ID; // Pass identification, replacement for typeid
56 explicit StripNonDebugSymbols()
57 : ModulePass(ID) {
58 initializeStripNonDebugSymbolsPass(*PassRegistry::getPassRegistry());
61 virtual bool runOnModule(Module &M);
63 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
64 AU.setPreservesAll();
68 class StripDebugDeclare : public ModulePass {
69 public:
70 static char ID; // Pass identification, replacement for typeid
71 explicit StripDebugDeclare()
72 : ModulePass(ID) {
73 initializeStripDebugDeclarePass(*PassRegistry::getPassRegistry());
76 virtual bool runOnModule(Module &M);
78 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
79 AU.setPreservesAll();
83 class StripDeadDebugInfo : public ModulePass {
84 public:
85 static char ID; // Pass identification, replacement for typeid
86 explicit StripDeadDebugInfo()
87 : ModulePass(ID) {
88 initializeStripDeadDebugInfoPass(*PassRegistry::getPassRegistry());
91 virtual bool runOnModule(Module &M);
93 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
94 AU.setPreservesAll();
99 char StripSymbols::ID = 0;
100 INITIALIZE_PASS(StripSymbols, "strip",
101 "Strip all symbols from a module", false, false)
103 ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
104 return new StripSymbols(OnlyDebugInfo);
107 char StripNonDebugSymbols::ID = 0;
108 INITIALIZE_PASS(StripNonDebugSymbols, "strip-nondebug",
109 "Strip all symbols, except dbg symbols, from a module",
110 false, false)
112 ModulePass *llvm::createStripNonDebugSymbolsPass() {
113 return new StripNonDebugSymbols();
116 char StripDebugDeclare::ID = 0;
117 INITIALIZE_PASS(StripDebugDeclare, "strip-debug-declare",
118 "Strip all llvm.dbg.declare intrinsics", false, false)
120 ModulePass *llvm::createStripDebugDeclarePass() {
121 return new StripDebugDeclare();
124 char StripDeadDebugInfo::ID = 0;
125 INITIALIZE_PASS(StripDeadDebugInfo, "strip-dead-debug-info",
126 "Strip debug info for unused symbols", false, false)
128 ModulePass *llvm::createStripDeadDebugInfoPass() {
129 return new StripDeadDebugInfo();
132 /// OnlyUsedBy - Return true if V is only used by Usr.
133 static bool OnlyUsedBy(Value *V, Value *Usr) {
134 for(Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
135 User *U = *I;
136 if (U != Usr)
137 return false;
139 return true;
142 static void RemoveDeadConstant(Constant *C) {
143 assert(C->use_empty() && "Constant is not dead!");
144 SmallPtrSet<Constant*, 4> Operands;
145 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
146 if (isa<DerivedType>(C->getOperand(i)->getType()) &&
147 OnlyUsedBy(C->getOperand(i), C))
148 Operands.insert(cast<Constant>(C->getOperand(i)));
149 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
150 if (!GV->hasLocalLinkage()) return; // Don't delete non static globals.
151 GV->eraseFromParent();
153 else if (!isa<Function>(C))
154 if (isa<CompositeType>(C->getType()))
155 C->destroyConstant();
157 // If the constant referenced anything, see if we can delete it as well.
158 for (SmallPtrSet<Constant*, 4>::iterator OI = Operands.begin(),
159 OE = Operands.end(); OI != OE; ++OI)
160 RemoveDeadConstant(*OI);
163 // Strip the symbol table of its names.
165 static void StripSymtab(ValueSymbolTable &ST, bool PreserveDbgInfo) {
166 for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
167 Value *V = VI->getValue();
168 ++VI;
169 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasLocalLinkage()) {
170 if (!PreserveDbgInfo || !V->getName().startswith("llvm.dbg"))
171 // Set name to "", removing from symbol table!
172 V->setName("");
177 // Strip any named types of their names.
178 static void StripTypeNames(Module &M, bool PreserveDbgInfo) {
179 std::vector<StructType*> StructTypes;
180 M.findUsedStructTypes(StructTypes);
182 for (unsigned i = 0, e = StructTypes.size(); i != e; ++i) {
183 StructType *STy = StructTypes[i];
184 if (STy->isAnonymous() || STy->getName().empty()) continue;
186 if (PreserveDbgInfo && STy->getName().startswith("llvm.dbg"))
187 continue;
189 STy->setName("");
193 /// Find values that are marked as llvm.used.
194 static void findUsedValues(GlobalVariable *LLVMUsed,
195 SmallPtrSet<const GlobalValue*, 8> &UsedValues) {
196 if (LLVMUsed == 0) return;
197 UsedValues.insert(LLVMUsed);
199 ConstantArray *Inits = dyn_cast<ConstantArray>(LLVMUsed->getInitializer());
200 if (Inits == 0) return;
202 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
203 if (GlobalValue *GV =
204 dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
205 UsedValues.insert(GV);
208 /// StripSymbolNames - Strip symbol names.
209 static bool StripSymbolNames(Module &M, bool PreserveDbgInfo) {
211 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
212 findUsedValues(M.getGlobalVariable("llvm.used"), llvmUsedValues);
213 findUsedValues(M.getGlobalVariable("llvm.compiler.used"), llvmUsedValues);
215 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
216 I != E; ++I) {
217 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
218 if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg"))
219 I->setName(""); // Internal symbols can't participate in linkage
222 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
223 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
224 if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg"))
225 I->setName(""); // Internal symbols can't participate in linkage
226 StripSymtab(I->getValueSymbolTable(), PreserveDbgInfo);
229 // Remove all names from types.
230 StripTypeNames(M, PreserveDbgInfo);
232 return true;
235 // StripDebugInfo - Strip debug info in the module if it exists.
236 // To do this, we remove llvm.dbg.func.start, llvm.dbg.stoppoint, and
237 // llvm.dbg.region.end calls, and any globals they point to if now dead.
238 static bool StripDebugInfo(Module &M) {
240 bool Changed = false;
242 // Remove all of the calls to the debugger intrinsics, and remove them from
243 // the module.
244 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
245 while (!Declare->use_empty()) {
246 CallInst *CI = cast<CallInst>(Declare->use_back());
247 CI->eraseFromParent();
249 Declare->eraseFromParent();
250 Changed = true;
253 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
254 while (!DbgVal->use_empty()) {
255 CallInst *CI = cast<CallInst>(DbgVal->use_back());
256 CI->eraseFromParent();
258 DbgVal->eraseFromParent();
259 Changed = true;
262 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
263 NME = M.named_metadata_end(); NMI != NME;) {
264 NamedMDNode *NMD = NMI;
265 ++NMI;
266 if (NMD->getName().startswith("llvm.dbg.")) {
267 NMD->eraseFromParent();
268 Changed = true;
272 for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
273 for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE;
274 ++FI)
275 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE;
276 ++BI) {
277 if (!BI->getDebugLoc().isUnknown()) {
278 Changed = true;
279 BI->setDebugLoc(DebugLoc());
283 return Changed;
286 bool StripSymbols::runOnModule(Module &M) {
287 bool Changed = false;
288 Changed |= StripDebugInfo(M);
289 if (!OnlyDebugInfo)
290 Changed |= StripSymbolNames(M, false);
291 return Changed;
294 bool StripNonDebugSymbols::runOnModule(Module &M) {
295 return StripSymbolNames(M, true);
298 bool StripDebugDeclare::runOnModule(Module &M) {
300 Function *Declare = M.getFunction("llvm.dbg.declare");
301 std::vector<Constant*> DeadConstants;
303 if (Declare) {
304 while (!Declare->use_empty()) {
305 CallInst *CI = cast<CallInst>(Declare->use_back());
306 Value *Arg1 = CI->getArgOperand(0);
307 Value *Arg2 = CI->getArgOperand(1);
308 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
309 CI->eraseFromParent();
310 if (Arg1->use_empty()) {
311 if (Constant *C = dyn_cast<Constant>(Arg1))
312 DeadConstants.push_back(C);
313 else
314 RecursivelyDeleteTriviallyDeadInstructions(Arg1);
316 if (Arg2->use_empty())
317 if (Constant *C = dyn_cast<Constant>(Arg2))
318 DeadConstants.push_back(C);
320 Declare->eraseFromParent();
323 while (!DeadConstants.empty()) {
324 Constant *C = DeadConstants.back();
325 DeadConstants.pop_back();
326 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
327 if (GV->hasLocalLinkage())
328 RemoveDeadConstant(GV);
329 } else
330 RemoveDeadConstant(C);
333 return true;
336 /// getRealLinkageName - If special LLVM prefix that is used to inform the asm
337 /// printer to not emit usual symbol prefix before the symbol name is used then
338 /// return linkage name after skipping this special LLVM prefix.
339 static StringRef getRealLinkageName(StringRef LinkageName) {
340 char One = '\1';
341 if (LinkageName.startswith(StringRef(&One, 1)))
342 return LinkageName.substr(1);
343 return LinkageName;
346 bool StripDeadDebugInfo::runOnModule(Module &M) {
347 bool Changed = false;
349 // Debugging infomration is encoded in llvm IR using metadata. This is designed
350 // such a way that debug info for symbols preserved even if symbols are
351 // optimized away by the optimizer. This special pass removes debug info for
352 // such symbols.
354 // llvm.dbg.gv keeps track of debug info for global variables.
355 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
356 SmallVector<MDNode *, 8> MDs;
357 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
358 if (DIGlobalVariable(NMD->getOperand(i)).Verify())
359 MDs.push_back(NMD->getOperand(i));
360 else
361 Changed = true;
362 NMD->eraseFromParent();
363 NMD = NULL;
365 for (SmallVector<MDNode *, 8>::iterator I = MDs.begin(),
366 E = MDs.end(); I != E; ++I) {
367 GlobalVariable *GV = DIGlobalVariable(*I).getGlobal();
368 if (GV && M.getGlobalVariable(GV->getName(), true)) {
369 if (!NMD)
370 NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
371 NMD->addOperand(*I);
373 else
374 Changed = true;
378 // llvm.dbg.sp keeps track of debug info for subprograms.
379 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp")) {
380 SmallVector<MDNode *, 8> MDs;
381 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
382 if (DISubprogram(NMD->getOperand(i)).Verify())
383 MDs.push_back(NMD->getOperand(i));
384 else
385 Changed = true;
386 NMD->eraseFromParent();
387 NMD = NULL;
389 for (SmallVector<MDNode *, 8>::iterator I = MDs.begin(),
390 E = MDs.end(); I != E; ++I) {
391 bool FnIsLive = false;
392 if (Function *F = DISubprogram(*I).getFunction())
393 if (M.getFunction(F->getName()))
394 FnIsLive = true;
395 if (FnIsLive) {
396 if (!NMD)
397 NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
398 NMD->addOperand(*I);
399 } else {
400 // Remove llvm.dbg.lv.fnname named mdnode which may have been used
401 // to hold debug info for dead function's local variables.
402 StringRef FName = DISubprogram(*I).getLinkageName();
403 if (FName.empty())
404 FName = DISubprogram(*I).getName();
405 if (NamedMDNode *LVNMD =
406 M.getNamedMetadata(Twine("llvm.dbg.lv.",
407 getRealLinkageName(FName))))
408 LVNMD->eraseFromParent();
413 return Changed;