2004-12-02 21:25:03 +00:00
|
|
|
//===- StripSymbols.cpp - Strip symbols and debug info from a module ------===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2004-12-02 21:25:03 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2004-12-02 21:25:03 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2007-11-04 16:15:04 +00:00
|
|
|
// The StripSymbols transformation implements code stripping. Specifically, it
|
|
|
|
// can delete:
|
|
|
|
//
|
|
|
|
// * names for virtual registers
|
|
|
|
// * symbols for internal globals and functions
|
|
|
|
// * debug information
|
2004-12-02 21:25:03 +00:00
|
|
|
//
|
2007-11-04 16:15:04 +00:00
|
|
|
// Note that this transformation makes code much less readable, so it should
|
|
|
|
// only be used in situations where the 'strip' utility would be used, such as
|
|
|
|
// reducing code size or making it harder to reverse engineer code.
|
2004-12-02 21:25:03 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/IPO.h"
|
2004-12-03 16:22:08 +00:00
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/Instructions.h"
|
2009-07-06 01:34:54 +00:00
|
|
|
#include "llvm/LLVMContext.h"
|
2004-12-02 21:25:03 +00:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Pass.h"
|
2009-06-26 01:49:18 +00:00
|
|
|
#include "llvm/Analysis/DebugInfo.h"
|
2007-02-05 20:47:22 +00:00
|
|
|
#include "llvm/ValueSymbolTable.h"
|
2007-01-06 07:24:44 +00:00
|
|
|
#include "llvm/TypeSymbolTable.h"
|
2009-03-03 21:31:02 +00:00
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
2007-02-05 23:32:05 +00:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2008-01-16 03:33:05 +00:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2004-12-02 21:25:03 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
2007-02-05 23:32:05 +00:00
|
|
|
class VISIBILITY_HIDDEN StripSymbols : public ModulePass {
|
2004-12-02 21:25:03 +00:00
|
|
|
bool OnlyDebugInfo;
|
|
|
|
public:
|
2007-05-06 13:37:16 +00:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2007-08-01 15:32:29 +00:00
|
|
|
explicit StripSymbols(bool ODI = false)
|
2008-09-04 17:05:41 +00:00
|
|
|
: ModulePass(&ID), OnlyDebugInfo(ODI) {}
|
2004-12-02 21:25:03 +00:00
|
|
|
|
2008-11-18 21:34:39 +00:00
|
|
|
virtual bool runOnModule(Module &M);
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
2008-11-14 22:49:37 +00:00
|
|
|
|
2008-11-18 21:34:39 +00:00
|
|
|
class VISIBILITY_HIDDEN StripNonDebugSymbols : public ModulePass {
|
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
explicit StripNonDebugSymbols()
|
|
|
|
: ModulePass(&ID) {}
|
2008-11-14 22:49:37 +00:00
|
|
|
|
2004-12-02 21:25:03 +00:00
|
|
|
virtual bool runOnModule(Module &M);
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
2009-03-09 20:49:37 +00:00
|
|
|
|
|
|
|
class VISIBILITY_HIDDEN StripDebugDeclare : public ModulePass {
|
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
explicit StripDebugDeclare()
|
|
|
|
: ModulePass(&ID) {}
|
|
|
|
|
|
|
|
virtual bool runOnModule(Module &M);
|
|
|
|
|
|
|
|
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
|
|
|
}
|
|
|
|
};
|
2004-12-02 21:25:03 +00:00
|
|
|
}
|
|
|
|
|
2008-05-13 00:00:25 +00:00
|
|
|
char StripSymbols::ID = 0;
|
|
|
|
static RegisterPass<StripSymbols>
|
|
|
|
X("strip", "Strip all symbols from a module");
|
|
|
|
|
2004-12-02 21:25:03 +00:00
|
|
|
ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
|
|
|
|
return new StripSymbols(OnlyDebugInfo);
|
|
|
|
}
|
|
|
|
|
2008-11-18 21:34:39 +00:00
|
|
|
char StripNonDebugSymbols::ID = 0;
|
|
|
|
static RegisterPass<StripNonDebugSymbols>
|
|
|
|
Y("strip-nondebug", "Strip all symbols, except dbg symbols, from a module");
|
|
|
|
|
|
|
|
ModulePass *llvm::createStripNonDebugSymbolsPass() {
|
|
|
|
return new StripNonDebugSymbols();
|
|
|
|
}
|
|
|
|
|
2009-03-09 20:49:37 +00:00
|
|
|
char StripDebugDeclare::ID = 0;
|
|
|
|
static RegisterPass<StripDebugDeclare>
|
|
|
|
Z("strip-debug-declare", "Strip all llvm.dbg.declare intrinsics");
|
|
|
|
|
|
|
|
ModulePass *llvm::createStripDebugDeclarePass() {
|
|
|
|
return new StripDebugDeclare();
|
|
|
|
}
|
|
|
|
|
2008-11-13 01:28:40 +00:00
|
|
|
/// OnlyUsedBy - Return true if V is only used by Usr.
|
|
|
|
static bool OnlyUsedBy(Value *V, Value *Usr) {
|
|
|
|
for(Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
|
|
|
|
User *U = *I;
|
|
|
|
if (U != Usr)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-12-03 16:22:08 +00:00
|
|
|
static void RemoveDeadConstant(Constant *C) {
|
|
|
|
assert(C->use_empty() && "Constant is not dead!");
|
2008-11-13 01:28:40 +00:00
|
|
|
SmallPtrSet<Constant *, 4> Operands;
|
2004-12-03 16:22:08 +00:00
|
|
|
for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
|
|
|
|
if (isa<DerivedType>(C->getOperand(i)->getType()) &&
|
2008-11-13 01:28:40 +00:00
|
|
|
OnlyUsedBy(C->getOperand(i), C))
|
|
|
|
Operands.insert(C->getOperand(i));
|
2004-12-03 16:22:08 +00:00
|
|
|
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
|
2009-01-15 20:18:42 +00:00
|
|
|
if (!GV->hasLocalLinkage()) return; // Don't delete non static globals.
|
2004-12-03 16:22:08 +00:00
|
|
|
GV->eraseFromParent();
|
|
|
|
}
|
|
|
|
else if (!isa<Function>(C))
|
2008-11-20 01:20:42 +00:00
|
|
|
if (isa<CompositeType>(C->getType()))
|
|
|
|
C->destroyConstant();
|
2005-04-21 23:48:37 +00:00
|
|
|
|
2004-12-03 16:22:08 +00:00
|
|
|
// If the constant referenced anything, see if we can delete it as well.
|
2008-11-13 01:28:40 +00:00
|
|
|
for (SmallPtrSet<Constant *, 4>::iterator OI = Operands.begin(),
|
|
|
|
OE = Operands.end(); OI != OE; ++OI)
|
|
|
|
RemoveDeadConstant(*OI);
|
2004-12-03 16:22:08 +00:00
|
|
|
}
|
2004-12-02 21:25:03 +00:00
|
|
|
|
2007-02-07 06:22:45 +00:00
|
|
|
// Strip the symbol table of its names.
|
|
|
|
//
|
2008-11-18 21:34:39 +00:00
|
|
|
static void StripSymtab(ValueSymbolTable &ST, bool PreserveDbgInfo) {
|
2007-02-07 06:22:45 +00:00
|
|
|
for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
|
2007-02-12 05:18:08 +00:00
|
|
|
Value *V = VI->getValue();
|
2007-02-07 06:22:45 +00:00
|
|
|
++VI;
|
2009-01-15 20:18:42 +00:00
|
|
|
if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasLocalLinkage()) {
|
2008-11-18 21:34:39 +00:00
|
|
|
if (!PreserveDbgInfo || strncmp(V->getNameStart(), "llvm.dbg", 8))
|
|
|
|
// Set name to "", removing from symbol table!
|
|
|
|
V->setName("");
|
2007-02-07 06:22:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Strip the symbol table of its names.
|
2008-11-18 21:34:39 +00:00
|
|
|
static void StripTypeSymtab(TypeSymbolTable &ST, bool PreserveDbgInfo) {
|
|
|
|
for (TypeSymbolTable::iterator TI = ST.begin(), E = ST.end(); TI != E; ) {
|
|
|
|
if (PreserveDbgInfo && strncmp(TI->first.c_str(), "llvm.dbg", 8) == 0)
|
|
|
|
++TI;
|
|
|
|
else
|
|
|
|
ST.remove(TI++);
|
|
|
|
}
|
2007-02-07 06:22:45 +00:00
|
|
|
}
|
|
|
|
|
2008-11-18 21:13:41 +00:00
|
|
|
/// Find values that are marked as llvm.used.
|
2009-07-20 06:14:25 +00:00
|
|
|
static void findUsedValues(GlobalVariable *LLVMUsed,
|
|
|
|
SmallPtrSet<const GlobalValue*, 8> &UsedValues) {
|
|
|
|
if (LLVMUsed == 0) return;
|
|
|
|
UsedValues.insert(LLVMUsed);
|
|
|
|
|
|
|
|
ConstantArray *Inits = dyn_cast<ConstantArray>(LLVMUsed->getInitializer());
|
|
|
|
if (Inits == 0) return;
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
|
|
|
|
if (GlobalValue *GV =
|
|
|
|
dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
|
|
|
|
UsedValues.insert(GV);
|
2008-11-18 21:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// StripSymbolNames - Strip symbol names.
|
2008-11-18 21:34:39 +00:00
|
|
|
bool StripSymbolNames(Module &M, bool PreserveDbgInfo) {
|
2008-11-18 21:13:41 +00:00
|
|
|
|
|
|
|
SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
|
2009-07-20 06:14:25 +00:00
|
|
|
findUsedValues(M.getGlobalVariable("llvm.used"), llvmUsedValues);
|
|
|
|
findUsedValues(M.getGlobalVariable("llvm.compiler.used"), llvmUsedValues);
|
2008-11-18 21:13:41 +00:00
|
|
|
|
2008-11-14 22:49:37 +00:00
|
|
|
for (Module::global_iterator I = M.global_begin(), E = M.global_end();
|
|
|
|
I != E; ++I) {
|
2009-01-15 20:18:42 +00:00
|
|
|
if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
|
2008-11-18 21:34:39 +00:00
|
|
|
if (!PreserveDbgInfo || strncmp(I->getNameStart(), "llvm.dbg", 8))
|
|
|
|
I->setName(""); // Internal symbols can't participate in linkage
|
2008-11-14 22:49:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
|
2009-01-15 20:18:42 +00:00
|
|
|
if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
|
2008-11-18 21:34:39 +00:00
|
|
|
if (!PreserveDbgInfo || strncmp(I->getNameStart(), "llvm.dbg", 8))
|
|
|
|
I->setName(""); // Internal symbols can't participate in linkage
|
|
|
|
StripSymtab(I->getValueSymbolTable(), PreserveDbgInfo);
|
2008-11-14 22:49:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Remove all names from types.
|
2008-11-18 21:34:39 +00:00
|
|
|
StripTypeSymtab(M.getTypeSymbolTable(), PreserveDbgInfo);
|
2008-01-16 03:33:05 +00:00
|
|
|
|
2008-11-14 22:49:37 +00:00
|
|
|
return true;
|
|
|
|
}
|
2004-12-02 21:25:03 +00:00
|
|
|
|
2008-11-14 22:49:37 +00:00
|
|
|
// StripDebugInfo - Strip debug info in the module if it exists.
|
|
|
|
// To do this, we remove llvm.dbg.func.start, llvm.dbg.stoppoint, and
|
|
|
|
// llvm.dbg.region.end calls, and any globals they point to if now dead.
|
2008-11-18 21:34:39 +00:00
|
|
|
bool StripDebugInfo(Module &M) {
|
2004-12-02 21:25:03 +00:00
|
|
|
|
2009-03-02 22:50:58 +00:00
|
|
|
SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
|
2009-07-20 06:14:25 +00:00
|
|
|
findUsedValues(M.getGlobalVariable("llvm.used"), llvmUsedValues);
|
|
|
|
findUsedValues(M.getGlobalVariable("llvm.compiler.used"), llvmUsedValues);
|
2009-03-02 22:50:58 +00:00
|
|
|
|
2009-06-26 01:49:18 +00:00
|
|
|
SmallVector<GlobalVariable *, 2> CUs;
|
|
|
|
SmallVector<GlobalVariable *, 4> GVs;
|
|
|
|
SmallVector<GlobalVariable *, 4> SPs;
|
|
|
|
CollectDebugInfoAnchors(M, CUs, GVs, SPs);
|
|
|
|
// These anchors use LinkOnce linkage so that the optimizer does not
|
|
|
|
// remove them accidently. Set InternalLinkage for all these debug
|
|
|
|
// info anchors.
|
|
|
|
for (SmallVector<GlobalVariable *, 2>::iterator I = CUs.begin(),
|
|
|
|
E = CUs.end(); I != E; ++I)
|
|
|
|
(*I)->setLinkage(GlobalValue::InternalLinkage);
|
|
|
|
for (SmallVector<GlobalVariable *, 4>::iterator I = GVs.begin(),
|
|
|
|
E = GVs.end(); I != E; ++I)
|
|
|
|
(*I)->setLinkage(GlobalValue::InternalLinkage);
|
|
|
|
for (SmallVector<GlobalVariable *, 4>::iterator I = SPs.begin(),
|
|
|
|
E = SPs.end(); I != E; ++I)
|
|
|
|
(*I)->setLinkage(GlobalValue::InternalLinkage);
|
|
|
|
|
|
|
|
|
|
|
|
// Delete all dbg variables.
|
2009-03-02 22:50:58 +00:00
|
|
|
for (Module::global_iterator I = M.global_begin(), E = M.global_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
GlobalVariable *GV = dyn_cast<GlobalVariable>(I);
|
|
|
|
if (!GV) continue;
|
|
|
|
if (!GV->use_empty() && llvmUsedValues.count(I) == 0) {
|
|
|
|
if (strncmp(GV->getNameStart(), "llvm.dbg", 8) == 0) {
|
2009-07-06 01:34:54 +00:00
|
|
|
GV->replaceAllUsesWith(M.getContext().getUndef(GV->getType()));
|
2009-03-02 22:50:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-05 21:19:13 +00:00
|
|
|
Function *FuncStart = M.getFunction("llvm.dbg.func.start");
|
|
|
|
Function *StopPoint = M.getFunction("llvm.dbg.stoppoint");
|
|
|
|
Function *RegionStart = M.getFunction("llvm.dbg.region.start");
|
|
|
|
Function *RegionEnd = M.getFunction("llvm.dbg.region.end");
|
|
|
|
Function *Declare = M.getFunction("llvm.dbg.declare");
|
2004-12-03 16:22:08 +00:00
|
|
|
|
2008-11-18 21:13:41 +00:00
|
|
|
std::vector<Constant*> DeadConstants;
|
2004-12-03 16:22:08 +00:00
|
|
|
|
|
|
|
// Remove all of the calls to the debugger intrinsics, and remove them from
|
|
|
|
// the module.
|
|
|
|
if (FuncStart) {
|
|
|
|
while (!FuncStart->use_empty()) {
|
|
|
|
CallInst *CI = cast<CallInst>(FuncStart->use_back());
|
|
|
|
Value *Arg = CI->getOperand(1);
|
2006-03-23 18:11:33 +00:00
|
|
|
assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
|
2004-12-03 16:22:08 +00:00
|
|
|
CI->eraseFromParent();
|
|
|
|
if (Arg->use_empty())
|
2008-11-18 21:13:41 +00:00
|
|
|
if (Constant *C = dyn_cast<Constant>(Arg))
|
|
|
|
DeadConstants.push_back(C);
|
2004-12-03 16:22:08 +00:00
|
|
|
}
|
|
|
|
FuncStart->eraseFromParent();
|
|
|
|
}
|
|
|
|
if (StopPoint) {
|
|
|
|
while (!StopPoint->use_empty()) {
|
|
|
|
CallInst *CI = cast<CallInst>(StopPoint->use_back());
|
2006-03-13 13:07:37 +00:00
|
|
|
Value *Arg = CI->getOperand(3);
|
2006-03-23 18:11:33 +00:00
|
|
|
assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
|
2004-12-03 16:22:08 +00:00
|
|
|
CI->eraseFromParent();
|
|
|
|
if (Arg->use_empty())
|
2008-11-18 21:13:41 +00:00
|
|
|
if (Constant *C = dyn_cast<Constant>(Arg))
|
|
|
|
DeadConstants.push_back(C);
|
2004-12-03 16:22:08 +00:00
|
|
|
}
|
|
|
|
StopPoint->eraseFromParent();
|
|
|
|
}
|
2006-03-23 18:11:33 +00:00
|
|
|
if (RegionStart) {
|
|
|
|
while (!RegionStart->use_empty()) {
|
|
|
|
CallInst *CI = cast<CallInst>(RegionStart->use_back());
|
|
|
|
Value *Arg = CI->getOperand(1);
|
|
|
|
assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
|
|
|
|
CI->eraseFromParent();
|
|
|
|
if (Arg->use_empty())
|
2008-11-18 21:13:41 +00:00
|
|
|
if (Constant *C = dyn_cast<Constant>(Arg))
|
|
|
|
DeadConstants.push_back(C);
|
2006-03-23 18:11:33 +00:00
|
|
|
}
|
|
|
|
RegionStart->eraseFromParent();
|
|
|
|
}
|
2004-12-03 16:22:08 +00:00
|
|
|
if (RegionEnd) {
|
|
|
|
while (!RegionEnd->use_empty()) {
|
|
|
|
CallInst *CI = cast<CallInst>(RegionEnd->use_back());
|
2006-03-23 18:11:33 +00:00
|
|
|
Value *Arg = CI->getOperand(1);
|
|
|
|
assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
|
2004-12-03 16:22:08 +00:00
|
|
|
CI->eraseFromParent();
|
2006-03-23 18:11:33 +00:00
|
|
|
if (Arg->use_empty())
|
2008-11-18 21:13:41 +00:00
|
|
|
if (Constant *C = dyn_cast<Constant>(Arg))
|
|
|
|
DeadConstants.push_back(C);
|
2004-12-03 16:22:08 +00:00
|
|
|
}
|
|
|
|
RegionEnd->eraseFromParent();
|
|
|
|
}
|
2006-03-23 18:11:33 +00:00
|
|
|
if (Declare) {
|
|
|
|
while (!Declare->use_empty()) {
|
|
|
|
CallInst *CI = cast<CallInst>(Declare->use_back());
|
2008-11-20 01:20:42 +00:00
|
|
|
Value *Arg1 = CI->getOperand(1);
|
|
|
|
Value *Arg2 = CI->getOperand(2);
|
2006-03-23 18:11:33 +00:00
|
|
|
assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
|
|
|
|
CI->eraseFromParent();
|
2008-11-20 01:20:42 +00:00
|
|
|
if (Arg1->use_empty()) {
|
|
|
|
if (Constant *C = dyn_cast<Constant>(Arg1))
|
|
|
|
DeadConstants.push_back(C);
|
2009-03-03 21:31:02 +00:00
|
|
|
else
|
2009-05-02 20:22:10 +00:00
|
|
|
RecursivelyDeleteTriviallyDeadInstructions(Arg1);
|
2008-11-20 01:20:42 +00:00
|
|
|
}
|
|
|
|
if (Arg2->use_empty())
|
|
|
|
if (Constant *C = dyn_cast<Constant>(Arg2))
|
2008-11-18 21:13:41 +00:00
|
|
|
DeadConstants.push_back(C);
|
2006-03-23 18:11:33 +00:00
|
|
|
}
|
|
|
|
Declare->eraseFromParent();
|
|
|
|
}
|
2004-12-03 16:22:08 +00:00
|
|
|
|
2008-11-13 01:28:40 +00:00
|
|
|
// llvm.dbg.compile_units and llvm.dbg.subprograms are marked as linkonce
|
|
|
|
// but since we are removing all debug information, make them internal now.
|
2009-01-15 20:18:42 +00:00
|
|
|
// FIXME: Use private linkage maybe?
|
2008-11-13 01:28:40 +00:00
|
|
|
if (Constant *C = M.getNamedGlobal("llvm.dbg.compile_units"))
|
|
|
|
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
|
|
|
|
GV->setLinkage(GlobalValue::InternalLinkage);
|
|
|
|
|
|
|
|
if (Constant *C = M.getNamedGlobal("llvm.dbg.subprograms"))
|
|
|
|
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
|
|
|
|
GV->setLinkage(GlobalValue::InternalLinkage);
|
2008-11-18 21:13:41 +00:00
|
|
|
|
|
|
|
if (Constant *C = M.getNamedGlobal("llvm.dbg.global_variables"))
|
|
|
|
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C))
|
|
|
|
GV->setLinkage(GlobalValue::InternalLinkage);
|
2008-11-13 01:28:40 +00:00
|
|
|
|
|
|
|
// Delete all dbg variables.
|
2008-11-19 00:22:02 +00:00
|
|
|
for (Module::global_iterator I = M.global_begin(), E = M.global_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
GlobalVariable *GV = dyn_cast<GlobalVariable>(I);
|
|
|
|
if (!GV) continue;
|
|
|
|
if (GV->use_empty() && llvmUsedValues.count(I) == 0
|
|
|
|
&& (!GV->hasSection()
|
|
|
|
|| strcmp(GV->getSection().c_str(), "llvm.metadata") == 0))
|
|
|
|
DeadConstants.push_back(GV);
|
|
|
|
}
|
2008-11-13 01:28:40 +00:00
|
|
|
|
2008-11-18 21:13:41 +00:00
|
|
|
if (DeadConstants.empty())
|
2008-11-14 22:49:37 +00:00
|
|
|
return false;
|
|
|
|
|
2008-11-13 01:28:40 +00:00
|
|
|
// Delete any internal globals that were only used by the debugger intrinsics.
|
2008-11-18 21:13:41 +00:00
|
|
|
while (!DeadConstants.empty()) {
|
|
|
|
Constant *C = DeadConstants.back();
|
|
|
|
DeadConstants.pop_back();
|
|
|
|
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
|
2009-01-15 20:18:42 +00:00
|
|
|
if (GV->hasLocalLinkage())
|
2008-11-18 21:13:41 +00:00
|
|
|
RemoveDeadConstant(GV);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
RemoveDeadConstant(C);
|
2004-12-03 16:22:08 +00:00
|
|
|
}
|
|
|
|
|
2008-11-13 01:28:40 +00:00
|
|
|
// Remove all llvm.dbg types.
|
|
|
|
TypeSymbolTable &ST = M.getTypeSymbolTable();
|
2008-11-16 06:35:18 +00:00
|
|
|
for (TypeSymbolTable::iterator TI = ST.begin(), TE = ST.end(); TI != TE; ) {
|
|
|
|
if (!strncmp(TI->first.c_str(), "llvm.dbg.", 9))
|
2008-11-13 01:28:40 +00:00
|
|
|
ST.remove(TI++);
|
|
|
|
else
|
|
|
|
++TI;
|
|
|
|
}
|
|
|
|
|
2005-04-21 23:48:37 +00:00
|
|
|
return true;
|
2004-12-02 21:25:03 +00:00
|
|
|
}
|
2008-11-18 21:34:39 +00:00
|
|
|
|
|
|
|
bool StripSymbols::runOnModule(Module &M) {
|
|
|
|
bool Changed = false;
|
|
|
|
Changed |= StripDebugInfo(M);
|
|
|
|
if (!OnlyDebugInfo)
|
|
|
|
Changed |= StripSymbolNames(M, false);
|
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StripNonDebugSymbols::runOnModule(Module &M) {
|
|
|
|
return StripSymbolNames(M, true);
|
|
|
|
}
|
2009-03-09 20:49:37 +00:00
|
|
|
|
|
|
|
bool StripDebugDeclare::runOnModule(Module &M) {
|
|
|
|
|
|
|
|
Function *Declare = M.getFunction("llvm.dbg.declare");
|
|
|
|
std::vector<Constant*> DeadConstants;
|
|
|
|
|
2009-03-13 22:59:47 +00:00
|
|
|
if (Declare) {
|
|
|
|
while (!Declare->use_empty()) {
|
|
|
|
CallInst *CI = cast<CallInst>(Declare->use_back());
|
|
|
|
Value *Arg1 = CI->getOperand(1);
|
|
|
|
Value *Arg2 = CI->getOperand(2);
|
|
|
|
assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
|
|
|
|
CI->eraseFromParent();
|
|
|
|
if (Arg1->use_empty()) {
|
|
|
|
if (Constant *C = dyn_cast<Constant>(Arg1))
|
|
|
|
DeadConstants.push_back(C);
|
|
|
|
else
|
2009-05-02 20:22:10 +00:00
|
|
|
RecursivelyDeleteTriviallyDeadInstructions(Arg1);
|
2009-03-13 22:59:47 +00:00
|
|
|
}
|
|
|
|
if (Arg2->use_empty())
|
|
|
|
if (Constant *C = dyn_cast<Constant>(Arg2))
|
|
|
|
DeadConstants.push_back(C);
|
2009-03-09 20:49:37 +00:00
|
|
|
}
|
2009-03-13 22:59:47 +00:00
|
|
|
Declare->eraseFromParent();
|
2009-03-09 20:49:37 +00:00
|
|
|
}
|
|
|
|
|
2009-03-09 21:32:28 +00:00
|
|
|
// Delete all llvm.dbg.global_variables.
|
|
|
|
for (Module::global_iterator I = M.global_begin(), E = M.global_end();
|
|
|
|
I != E; ++I) {
|
|
|
|
GlobalVariable *GV = dyn_cast<GlobalVariable>(I);
|
|
|
|
if (!GV) continue;
|
|
|
|
if (GV->use_empty() && GV->hasName()
|
|
|
|
&& strncmp(GV->getNameStart(), "llvm.dbg.global_variable", 24) == 0)
|
|
|
|
DeadConstants.push_back(GV);
|
|
|
|
}
|
|
|
|
|
2009-03-09 20:49:37 +00:00
|
|
|
while (!DeadConstants.empty()) {
|
|
|
|
Constant *C = DeadConstants.back();
|
|
|
|
DeadConstants.pop_back();
|
|
|
|
if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
|
|
|
|
if (GV->hasLocalLinkage())
|
|
|
|
RemoveDeadConstant(GV);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
RemoveDeadConstant(C);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|