Use %ull here.
[llvm/stm8.git] / lib / Transforms / IPO / StripSymbols.cpp
bloba69076510806d729ac93ac208d09f52813cdb6c6
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/TypeSymbolTable.h"
32 #include "llvm/Transforms/Utils/Local.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 the symbol table of its names.
178 static void StripTypeSymtab(TypeSymbolTable &ST, bool PreserveDbgInfo) {
179 for (TypeSymbolTable::iterator TI = ST.begin(), E = ST.end(); TI != E; ) {
180 if (PreserveDbgInfo && StringRef(TI->first).startswith("llvm.dbg"))
181 ++TI;
182 else
183 ST.remove(TI++);
187 /// Find values that are marked as llvm.used.
188 static void findUsedValues(GlobalVariable *LLVMUsed,
189 SmallPtrSet<const GlobalValue*, 8> &UsedValues) {
190 if (LLVMUsed == 0) return;
191 UsedValues.insert(LLVMUsed);
193 ConstantArray *Inits = dyn_cast<ConstantArray>(LLVMUsed->getInitializer());
194 if (Inits == 0) return;
196 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
197 if (GlobalValue *GV =
198 dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
199 UsedValues.insert(GV);
202 /// StripSymbolNames - Strip symbol names.
203 static bool StripSymbolNames(Module &M, bool PreserveDbgInfo) {
205 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
206 findUsedValues(M.getGlobalVariable("llvm.used"), llvmUsedValues);
207 findUsedValues(M.getGlobalVariable("llvm.compiler.used"), llvmUsedValues);
209 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
210 I != E; ++I) {
211 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
212 if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg"))
213 I->setName(""); // Internal symbols can't participate in linkage
216 for (Module::iterator I = M.begin(), E = M.end(); 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
220 StripSymtab(I->getValueSymbolTable(), PreserveDbgInfo);
223 // Remove all names from types.
224 StripTypeSymtab(M.getTypeSymbolTable(), PreserveDbgInfo);
226 return true;
229 // StripDebugInfo - Strip debug info in the module if it exists.
230 // To do this, we remove llvm.dbg.func.start, llvm.dbg.stoppoint, and
231 // llvm.dbg.region.end calls, and any globals they point to if now dead.
232 static bool StripDebugInfo(Module &M) {
234 bool Changed = false;
236 // Remove all of the calls to the debugger intrinsics, and remove them from
237 // the module.
238 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
239 while (!Declare->use_empty()) {
240 CallInst *CI = cast<CallInst>(Declare->use_back());
241 CI->eraseFromParent();
243 Declare->eraseFromParent();
244 Changed = true;
247 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
248 while (!DbgVal->use_empty()) {
249 CallInst *CI = cast<CallInst>(DbgVal->use_back());
250 CI->eraseFromParent();
252 DbgVal->eraseFromParent();
253 Changed = true;
256 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
257 NME = M.named_metadata_end(); NMI != NME;) {
258 NamedMDNode *NMD = NMI;
259 ++NMI;
260 if (NMD->getName().startswith("llvm.dbg.")) {
261 NMD->eraseFromParent();
262 Changed = true;
266 for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
267 for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE;
268 ++FI)
269 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE;
270 ++BI) {
271 if (!BI->getDebugLoc().isUnknown()) {
272 Changed = true;
273 BI->setDebugLoc(DebugLoc());
277 return Changed;
280 bool StripSymbols::runOnModule(Module &M) {
281 bool Changed = false;
282 Changed |= StripDebugInfo(M);
283 if (!OnlyDebugInfo)
284 Changed |= StripSymbolNames(M, false);
285 return Changed;
288 bool StripNonDebugSymbols::runOnModule(Module &M) {
289 return StripSymbolNames(M, true);
292 bool StripDebugDeclare::runOnModule(Module &M) {
294 Function *Declare = M.getFunction("llvm.dbg.declare");
295 std::vector<Constant*> DeadConstants;
297 if (Declare) {
298 while (!Declare->use_empty()) {
299 CallInst *CI = cast<CallInst>(Declare->use_back());
300 Value *Arg1 = CI->getArgOperand(0);
301 Value *Arg2 = CI->getArgOperand(1);
302 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
303 CI->eraseFromParent();
304 if (Arg1->use_empty()) {
305 if (Constant *C = dyn_cast<Constant>(Arg1))
306 DeadConstants.push_back(C);
307 else
308 RecursivelyDeleteTriviallyDeadInstructions(Arg1);
310 if (Arg2->use_empty())
311 if (Constant *C = dyn_cast<Constant>(Arg2))
312 DeadConstants.push_back(C);
314 Declare->eraseFromParent();
317 while (!DeadConstants.empty()) {
318 Constant *C = DeadConstants.back();
319 DeadConstants.pop_back();
320 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
321 if (GV->hasLocalLinkage())
322 RemoveDeadConstant(GV);
323 } else
324 RemoveDeadConstant(C);
327 return true;
330 /// getRealLinkageName - If special LLVM prefix that is used to inform the asm
331 /// printer to not emit usual symbol prefix before the symbol name is used then
332 /// return linkage name after skipping this special LLVM prefix.
333 static StringRef getRealLinkageName(StringRef LinkageName) {
334 char One = '\1';
335 if (LinkageName.startswith(StringRef(&One, 1)))
336 return LinkageName.substr(1);
337 return LinkageName;
340 bool StripDeadDebugInfo::runOnModule(Module &M) {
341 bool Changed = false;
343 // Debugging infomration is encoded in llvm IR using metadata. This is designed
344 // such a way that debug info for symbols preserved even if symbols are
345 // optimized away by the optimizer. This special pass removes debug info for
346 // such symbols.
348 // llvm.dbg.gv keeps track of debug info for global variables.
349 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
350 SmallVector<MDNode *, 8> MDs;
351 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
352 if (DIGlobalVariable(NMD->getOperand(i)).Verify())
353 MDs.push_back(NMD->getOperand(i));
354 else
355 Changed = true;
356 NMD->eraseFromParent();
357 NMD = NULL;
359 for (SmallVector<MDNode *, 8>::iterator I = MDs.begin(),
360 E = MDs.end(); I != E; ++I) {
361 GlobalVariable *GV = DIGlobalVariable(*I).getGlobal();
362 if (GV && M.getGlobalVariable(GV->getName(), true)) {
363 if (!NMD)
364 NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
365 NMD->addOperand(*I);
367 else
368 Changed = true;
372 // llvm.dbg.sp keeps track of debug info for subprograms.
373 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp")) {
374 SmallVector<MDNode *, 8> MDs;
375 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
376 if (DISubprogram(NMD->getOperand(i)).Verify())
377 MDs.push_back(NMD->getOperand(i));
378 else
379 Changed = true;
380 NMD->eraseFromParent();
381 NMD = NULL;
383 for (SmallVector<MDNode *, 8>::iterator I = MDs.begin(),
384 E = MDs.end(); I != E; ++I) {
385 bool FnIsLive = false;
386 if (Function *F = DISubprogram(*I).getFunction())
387 if (M.getFunction(F->getName()))
388 FnIsLive = true;
389 if (FnIsLive) {
390 if (!NMD)
391 NMD = M.getOrInsertNamedMetadata("llvm.dbg.sp");
392 NMD->addOperand(*I);
393 } else {
394 // Remove llvm.dbg.lv.fnname named mdnode which may have been used
395 // to hold debug info for dead function's local variables.
396 StringRef FName = DISubprogram(*I).getLinkageName();
397 if (FName.empty())
398 FName = DISubprogram(*I).getName();
399 if (NamedMDNode *LVNMD =
400 M.getNamedMetadata(Twine("llvm.dbg.lv.",
401 getRealLinkageName(FName))))
402 LVNMD->eraseFromParent();
407 return Changed;