blob: 813f30d223c5a08a638ab0192bbf0075a152898e [file] [log] [blame]
Chris Lattnere8ebcb32004-12-02 21:25:03 +00001//===- StripSymbols.cpp - Strip symbols and debug info from a module ------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Chris Lattnere8ebcb32004-12-02 21:25:03 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanb1c93172005-04-21 23:48:37 +00007//
Chris Lattnere8ebcb32004-12-02 21:25:03 +00008//===----------------------------------------------------------------------===//
9//
Gordon Henriksend5687672007-11-04 16:15:04 +000010// The StripSymbols transformation implements code stripping. Specifically, it
11// can delete:
Michael Gottesman0900993c2013-08-21 22:53:29 +000012//
Gordon Henriksend5687672007-11-04 16:15:04 +000013// * names for virtual registers
14// * symbols for internal globals and functions
15// * debug information
Chris Lattnere8ebcb32004-12-02 21:25:03 +000016//
Gordon Henriksend5687672007-11-04 16:15:04 +000017// 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.
Chris Lattnere8ebcb32004-12-02 21:25:03 +000020//
21//===----------------------------------------------------------------------===//
22
23#include "llvm/Transforms/IPO.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/ADT/DenseMap.h"
25#include "llvm/ADT/SmallPtrSet.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/Constants.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000027#include "llvm/IR/DebugInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000028#include "llvm/IR/DerivedTypes.h"
29#include "llvm/IR/Instructions.h"
30#include "llvm/IR/Module.h"
Chandler Carruthdcb603f2013-01-07 15:43:51 +000031#include "llvm/IR/TypeFinder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000032#include "llvm/IR/ValueSymbolTable.h"
Chris Lattnere8ebcb32004-12-02 21:25:03 +000033#include "llvm/Pass.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000034#include "llvm/Transforms/Utils/Local.h"
Chris Lattnere8ebcb32004-12-02 21:25:03 +000035using namespace llvm;
36
37namespace {
Nick Lewycky88214fb2009-09-03 06:43:15 +000038 class StripSymbols : public ModulePass {
Chris Lattnere8ebcb32004-12-02 21:25:03 +000039 bool OnlyDebugInfo;
40 public:
Nick Lewyckye7da2d62007-05-06 13:37:16 +000041 static char ID; // Pass identification, replacement for typeid
Michael Gottesman0900993c2013-08-21 22:53:29 +000042 explicit StripSymbols(bool ODI = false)
Owen Anderson6c18d1a2010-10-19 17:21:58 +000043 : ModulePass(ID), OnlyDebugInfo(ODI) {
44 initializeStripSymbolsPass(*PassRegistry::getPassRegistry());
45 }
Chris Lattnere8ebcb32004-12-02 21:25:03 +000046
Craig Topper3e4c6972014-03-05 09:10:37 +000047 bool runOnModule(Module &M) override;
Devang Patel8ada1d52008-11-14 22:49:37 +000048
Craig Topper3e4c6972014-03-05 09:10:37 +000049 void getAnalysisUsage(AnalysisUsage &AU) const override {
Devang Patelb5e867a2008-11-18 21:34:39 +000050 AU.setPreservesAll();
51 }
52 };
53
Nick Lewycky88214fb2009-09-03 06:43:15 +000054 class StripNonDebugSymbols : public ModulePass {
Devang Patelb5e867a2008-11-18 21:34:39 +000055 public:
56 static char ID; // Pass identification, replacement for typeid
57 explicit StripNonDebugSymbols()
Owen Anderson6c18d1a2010-10-19 17:21:58 +000058 : ModulePass(ID) {
59 initializeStripNonDebugSymbolsPass(*PassRegistry::getPassRegistry());
60 }
Devang Patel8ada1d52008-11-14 22:49:37 +000061
Craig Topper3e4c6972014-03-05 09:10:37 +000062 bool runOnModule(Module &M) override;
Chris Lattnere8ebcb32004-12-02 21:25:03 +000063
Craig Topper3e4c6972014-03-05 09:10:37 +000064 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chris Lattnere8ebcb32004-12-02 21:25:03 +000065 AU.setPreservesAll();
66 }
67 };
Devang Patel66f84e72009-03-09 20:49:37 +000068
Nick Lewycky88214fb2009-09-03 06:43:15 +000069 class StripDebugDeclare : public ModulePass {
Devang Patel66f84e72009-03-09 20:49:37 +000070 public:
71 static char ID; // Pass identification, replacement for typeid
72 explicit StripDebugDeclare()
Owen Anderson6c18d1a2010-10-19 17:21:58 +000073 : ModulePass(ID) {
74 initializeStripDebugDeclarePass(*PassRegistry::getPassRegistry());
75 }
Devang Patel66f84e72009-03-09 20:49:37 +000076
Craig Topper3e4c6972014-03-05 09:10:37 +000077 bool runOnModule(Module &M) override;
Devang Patel66f84e72009-03-09 20:49:37 +000078
Craig Topper3e4c6972014-03-05 09:10:37 +000079 void getAnalysisUsage(AnalysisUsage &AU) const override {
Devang Patel66f84e72009-03-09 20:49:37 +000080 AU.setPreservesAll();
81 }
82 };
Devang Patel2b434e12010-07-01 19:49:20 +000083
84 class StripDeadDebugInfo : public ModulePass {
85 public:
86 static char ID; // Pass identification, replacement for typeid
87 explicit StripDeadDebugInfo()
Owen Anderson6c18d1a2010-10-19 17:21:58 +000088 : ModulePass(ID) {
89 initializeStripDeadDebugInfoPass(*PassRegistry::getPassRegistry());
90 }
Devang Patel2b434e12010-07-01 19:49:20 +000091
Craig Topper3e4c6972014-03-05 09:10:37 +000092 bool runOnModule(Module &M) override;
Devang Patel2b434e12010-07-01 19:49:20 +000093
Craig Topper3e4c6972014-03-05 09:10:37 +000094 void getAnalysisUsage(AnalysisUsage &AU) const override {
Devang Patel2b434e12010-07-01 19:49:20 +000095 AU.setPreservesAll();
96 }
97 };
Chris Lattnere8ebcb32004-12-02 21:25:03 +000098}
99
Dan Gohmand78c4002008-05-13 00:00:25 +0000100char StripSymbols::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +0000101INITIALIZE_PASS(StripSymbols, "strip",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000102 "Strip all symbols from a module", false, false)
Dan Gohmand78c4002008-05-13 00:00:25 +0000103
Chris Lattnere8ebcb32004-12-02 21:25:03 +0000104ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
105 return new StripSymbols(OnlyDebugInfo);
106}
107
Devang Patelb5e867a2008-11-18 21:34:39 +0000108char StripNonDebugSymbols::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +0000109INITIALIZE_PASS(StripNonDebugSymbols, "strip-nondebug",
110 "Strip all symbols, except dbg symbols, from a module",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000111 false, false)
Devang Patelb5e867a2008-11-18 21:34:39 +0000112
113ModulePass *llvm::createStripNonDebugSymbolsPass() {
114 return new StripNonDebugSymbols();
115}
116
Devang Patel66f84e72009-03-09 20:49:37 +0000117char StripDebugDeclare::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +0000118INITIALIZE_PASS(StripDebugDeclare, "strip-debug-declare",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000119 "Strip all llvm.dbg.declare intrinsics", false, false)
Devang Patel66f84e72009-03-09 20:49:37 +0000120
121ModulePass *llvm::createStripDebugDeclarePass() {
122 return new StripDebugDeclare();
123}
124
Devang Patel2b434e12010-07-01 19:49:20 +0000125char StripDeadDebugInfo::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +0000126INITIALIZE_PASS(StripDeadDebugInfo, "strip-dead-debug-info",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000127 "Strip debug info for unused symbols", false, false)
Devang Patel2b434e12010-07-01 19:49:20 +0000128
129ModulePass *llvm::createStripDeadDebugInfoPass() {
130 return new StripDeadDebugInfo();
131}
132
Devang Patel3dd51c52008-11-13 01:28:40 +0000133/// OnlyUsedBy - Return true if V is only used by Usr.
134static bool OnlyUsedBy(Value *V, Value *Usr) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000135 for (User *U : V->users())
Devang Patel3dd51c52008-11-13 01:28:40 +0000136 if (U != Usr)
137 return false;
Chandler Carruthcdf47882014-03-09 03:16:01 +0000138
Devang Patel3dd51c52008-11-13 01:28:40 +0000139 return true;
140}
141
Chris Lattner9019e5c2004-12-03 16:22:08 +0000142static void RemoveDeadConstant(Constant *C) {
143 assert(C->use_empty() && "Constant is not dead!");
Chris Lattnera91a5632009-10-28 05:14:34 +0000144 SmallPtrSet<Constant*, 4> Operands;
Chris Lattner9019e5c2004-12-03 16:22:08 +0000145 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
Michael Gottesman0900993c2013-08-21 22:53:29 +0000146 if (OnlyUsedBy(C->getOperand(i), C))
Chris Lattnera91a5632009-10-28 05:14:34 +0000147 Operands.insert(cast<Constant>(C->getOperand(i)));
Chris Lattner9019e5c2004-12-03 16:22:08 +0000148 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
Alp Tokerf907b892013-12-05 05:44:44 +0000149 if (!GV->hasLocalLinkage()) return; // Don't delete non-static globals.
Chris Lattner9019e5c2004-12-03 16:22:08 +0000150 GV->eraseFromParent();
151 }
152 else if (!isa<Function>(C))
Devang Patelc8b2fe12008-11-20 01:20:42 +0000153 if (isa<CompositeType>(C->getType()))
154 C->destroyConstant();
Misha Brukmanb1c93172005-04-21 23:48:37 +0000155
Chris Lattner9019e5c2004-12-03 16:22:08 +0000156 // If the constant referenced anything, see if we can delete it as well.
Chris Lattnera91a5632009-10-28 05:14:34 +0000157 for (SmallPtrSet<Constant*, 4>::iterator OI = Operands.begin(),
Devang Patel3dd51c52008-11-13 01:28:40 +0000158 OE = Operands.end(); OI != OE; ++OI)
159 RemoveDeadConstant(*OI);
Chris Lattner9019e5c2004-12-03 16:22:08 +0000160}
Chris Lattnere8ebcb32004-12-02 21:25:03 +0000161
Chris Lattner88051b02007-02-07 06:22:45 +0000162// Strip the symbol table of its names.
163//
Devang Patelb5e867a2008-11-18 21:34:39 +0000164static void StripSymtab(ValueSymbolTable &ST, bool PreserveDbgInfo) {
Chris Lattner88051b02007-02-07 06:22:45 +0000165 for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
Chris Lattner32ab6432007-02-12 05:18:08 +0000166 Value *V = VI->getValue();
Chris Lattner88051b02007-02-07 06:22:45 +0000167 ++VI;
Rafael Espindola6de96a12009-01-15 20:18:42 +0000168 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasLocalLinkage()) {
Daniel Dunbar6115b392009-07-26 09:48:23 +0000169 if (!PreserveDbgInfo || !V->getName().startswith("llvm.dbg"))
Devang Patelb5e867a2008-11-18 21:34:39 +0000170 // Set name to "", removing from symbol table!
171 V->setName("");
Chris Lattner88051b02007-02-07 06:22:45 +0000172 }
173 }
174}
175
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000176// Strip any named types of their names.
177static void StripTypeNames(Module &M, bool PreserveDbgInfo) {
Bill Wendling8555a372012-08-03 00:30:35 +0000178 TypeFinder StructTypes;
179 StructTypes.run(M, false);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000180
181 for (unsigned i = 0, e = StructTypes.size(); i != e; ++i) {
182 StructType *STy = StructTypes[i];
Chris Lattner335d3992011-08-12 18:06:37 +0000183 if (STy->isLiteral() || STy->getName().empty()) continue;
Michael Gottesman0900993c2013-08-21 22:53:29 +0000184
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000185 if (PreserveDbgInfo && STy->getName().startswith("llvm.dbg"))
186 continue;
187
188 STy->setName("");
Devang Patelb5e867a2008-11-18 21:34:39 +0000189 }
Chris Lattner88051b02007-02-07 06:22:45 +0000190}
191
Devang Patel3b7a2be2008-11-18 21:13:41 +0000192/// Find values that are marked as llvm.used.
Chris Lattner58f9bb22009-07-20 06:14:25 +0000193static void findUsedValues(GlobalVariable *LLVMUsed,
Craig Topper71b7b682014-08-21 05:55:13 +0000194 SmallPtrSetImpl<const GlobalValue*> &UsedValues) {
Craig Topperf40110f2014-04-25 05:29:35 +0000195 if (!LLVMUsed) return;
Chris Lattner58f9bb22009-07-20 06:14:25 +0000196 UsedValues.insert(LLVMUsed);
Rafael Espindola74f2e462013-04-22 14:58:02 +0000197
198 ConstantArray *Inits = cast<ConstantArray>(LLVMUsed->getInitializer());
199
Chris Lattner58f9bb22009-07-20 06:14:25 +0000200 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
Michael Gottesman0900993c2013-08-21 22:53:29 +0000201 if (GlobalValue *GV =
Chris Lattner58f9bb22009-07-20 06:14:25 +0000202 dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
203 UsedValues.insert(GV);
Devang Patel3b7a2be2008-11-18 21:13:41 +0000204}
205
206/// StripSymbolNames - Strip symbol names.
Dan Gohmana6d0afc2009-08-07 01:32:21 +0000207static bool StripSymbolNames(Module &M, bool PreserveDbgInfo) {
Devang Patel3b7a2be2008-11-18 21:13:41 +0000208
209 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
Chris Lattner58f9bb22009-07-20 06:14:25 +0000210 findUsedValues(M.getGlobalVariable("llvm.used"), llvmUsedValues);
211 findUsedValues(M.getGlobalVariable("llvm.compiler.used"), llvmUsedValues);
Devang Patel3b7a2be2008-11-18 21:13:41 +0000212
Devang Patel8ada1d52008-11-14 22:49:37 +0000213 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
214 I != E; ++I) {
Rafael Espindola6de96a12009-01-15 20:18:42 +0000215 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
Daniel Dunbar6115b392009-07-26 09:48:23 +0000216 if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg"))
Devang Patelb5e867a2008-11-18 21:34:39 +0000217 I->setName(""); // Internal symbols can't participate in linkage
Devang Patel8ada1d52008-11-14 22:49:37 +0000218 }
Michael Gottesman0900993c2013-08-21 22:53:29 +0000219
Devang Patel8ada1d52008-11-14 22:49:37 +0000220 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
Rafael Espindola6de96a12009-01-15 20:18:42 +0000221 if (I->hasLocalLinkage() && llvmUsedValues.count(I) == 0)
Daniel Dunbar6115b392009-07-26 09:48:23 +0000222 if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg"))
Devang Patelb5e867a2008-11-18 21:34:39 +0000223 I->setName(""); // Internal symbols can't participate in linkage
224 StripSymtab(I->getValueSymbolTable(), PreserveDbgInfo);
Devang Patel8ada1d52008-11-14 22:49:37 +0000225 }
Michael Gottesman0900993c2013-08-21 22:53:29 +0000226
Devang Patel8ada1d52008-11-14 22:49:37 +0000227 // Remove all names from types.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000228 StripTypeNames(M, PreserveDbgInfo);
Chris Lattnere8ebcb32004-12-02 21:25:03 +0000229
Devang Patel8ada1d52008-11-14 22:49:37 +0000230 return true;
231}
232
Devang Patelb5e867a2008-11-18 21:34:39 +0000233bool StripSymbols::runOnModule(Module &M) {
234 bool Changed = false;
235 Changed |= StripDebugInfo(M);
236 if (!OnlyDebugInfo)
237 Changed |= StripSymbolNames(M, false);
238 return Changed;
239}
240
241bool StripNonDebugSymbols::runOnModule(Module &M) {
242 return StripSymbolNames(M, true);
243}
Devang Patel66f84e72009-03-09 20:49:37 +0000244
245bool StripDebugDeclare::runOnModule(Module &M) {
246
247 Function *Declare = M.getFunction("llvm.dbg.declare");
Devang Patel66f84e72009-03-09 20:49:37 +0000248 std::vector<Constant*> DeadConstants;
249
Dale Johannesena4ac7352009-03-13 22:59:47 +0000250 if (Declare) {
251 while (!Declare->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000252 CallInst *CI = cast<CallInst>(Declare->user_back());
Gabor Greifd5057282010-06-30 12:40:35 +0000253 Value *Arg1 = CI->getArgOperand(0);
254 Value *Arg2 = CI->getArgOperand(1);
Dale Johannesena4ac7352009-03-13 22:59:47 +0000255 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
256 CI->eraseFromParent();
257 if (Arg1->use_empty()) {
Michael Gottesman0900993c2013-08-21 22:53:29 +0000258 if (Constant *C = dyn_cast<Constant>(Arg1))
Dale Johannesena4ac7352009-03-13 22:59:47 +0000259 DeadConstants.push_back(C);
Michael Gottesman0900993c2013-08-21 22:53:29 +0000260 else
Dan Gohmanb17dcbd2009-05-02 20:22:10 +0000261 RecursivelyDeleteTriviallyDeadInstructions(Arg1);
Dale Johannesena4ac7352009-03-13 22:59:47 +0000262 }
263 if (Arg2->use_empty())
Michael Gottesman0900993c2013-08-21 22:53:29 +0000264 if (Constant *C = dyn_cast<Constant>(Arg2))
Dale Johannesena4ac7352009-03-13 22:59:47 +0000265 DeadConstants.push_back(C);
Devang Patel66f84e72009-03-09 20:49:37 +0000266 }
Dale Johannesena4ac7352009-03-13 22:59:47 +0000267 Declare->eraseFromParent();
Devang Patel66f84e72009-03-09 20:49:37 +0000268 }
Devang Patel66f84e72009-03-09 20:49:37 +0000269
270 while (!DeadConstants.empty()) {
271 Constant *C = DeadConstants.back();
272 DeadConstants.pop_back();
273 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
274 if (GV->hasLocalLinkage())
275 RemoveDeadConstant(GV);
Chris Lattnera91a5632009-10-28 05:14:34 +0000276 } else
Devang Patel66f84e72009-03-09 20:49:37 +0000277 RemoveDeadConstant(C);
278 }
279
280 return true;
281}
Devang Patel2b434e12010-07-01 19:49:20 +0000282
Michael Gottesman823aaff2013-08-23 00:23:24 +0000283/// Remove any debug info for global variables/functions in the given module for
284/// which said global variable/function no longer exists (i.e. is null).
285///
286/// Debugging information is encoded in llvm IR using metadata. This is designed
287/// such a way that debug info for symbols preserved even if symbols are
288/// optimized away by the optimizer. This special pass removes debug info for
289/// such symbols.
Devang Patel2b434e12010-07-01 19:49:20 +0000290bool StripDeadDebugInfo::runOnModule(Module &M) {
291 bool Changed = false;
292
Michael Gottesman823aaff2013-08-23 00:23:24 +0000293 LLVMContext &C = M.getContext();
Devang Patel2b434e12010-07-01 19:49:20 +0000294
Michael Gottesman823aaff2013-08-23 00:23:24 +0000295 // Find all debug info in F. This is actually overkill in terms of what we
Michael Gottesmaneab9a7f2013-08-27 04:43:03 +0000296 // want to do, but we want to try and be as resilient as possible in the face
Michael Gottesman823aaff2013-08-23 00:23:24 +0000297 // of potential debug info changes by using the formal interfaces given to us
298 // as much as possible.
299 DebugInfoFinder F;
300 F.processModule(M);
Devang Patel2b434e12010-07-01 19:49:20 +0000301
Michael Gottesman823aaff2013-08-23 00:23:24 +0000302 // For each compile unit, find the live set of global variables/functions and
303 // replace the current list of potentially dead global variables/functions
304 // with the live list.
305 SmallVector<Value *, 64> LiveGlobalVariables;
306 SmallVector<Value *, 64> LiveSubprograms;
307 DenseSet<const MDNode *> VisitedSet;
308
Alon Mishnead312152014-03-18 09:41:07 +0000309 for (DICompileUnit DIC : F.compile_units()) {
Michael Gottesman823aaff2013-08-23 00:23:24 +0000310 assert(DIC.Verify() && "DIC must verify as a DICompileUnit.");
311
312 // Create our live subprogram list.
313 DIArray SPs = DIC.getSubprograms();
314 bool SubprogramChange = false;
315 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
316 DISubprogram DISP(SPs.getElement(i));
317 assert(DISP.Verify() && "DISP must verify as a DISubprogram.");
318
319 // Make sure we visit each subprogram only once.
320 if (!VisitedSet.insert(DISP).second)
321 continue;
322
323 // If the function referenced by DISP is not null, the function is live.
324 if (DISP.getFunction())
325 LiveSubprograms.push_back(DISP);
Devang Patel2b434e12010-07-01 19:49:20 +0000326 else
Michael Gottesman823aaff2013-08-23 00:23:24 +0000327 SubprogramChange = true;
Devang Patel2b434e12010-07-01 19:49:20 +0000328 }
Devang Patel2b434e12010-07-01 19:49:20 +0000329
Michael Gottesman823aaff2013-08-23 00:23:24 +0000330 // Create our live global variable list.
331 DIArray GVs = DIC.getGlobalVariables();
332 bool GlobalVariableChange = false;
333 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
334 DIGlobalVariable DIG(GVs.getElement(i));
335 assert(DIG.Verify() && "DIG must verify as DIGlobalVariable.");
336
337 // Make sure we only visit each global variable only once.
338 if (!VisitedSet.insert(DIG).second)
339 continue;
340
341 // If the global variable referenced by DIG is not null, the global
342 // variable is live.
343 if (DIG.getGlobal())
344 LiveGlobalVariables.push_back(DIG);
Devang Patel2b434e12010-07-01 19:49:20 +0000345 else
Michael Gottesman823aaff2013-08-23 00:23:24 +0000346 GlobalVariableChange = true;
Devang Patel2b434e12010-07-01 19:49:20 +0000347 }
Michael Gottesman823aaff2013-08-23 00:23:24 +0000348
349 // If we found dead subprograms or global variables, replace the current
350 // subprogram list/global variable list with our new live subprogram/global
351 // variable list.
352 if (SubprogramChange) {
353 // Make sure that 9 is still the index of the subprograms. This is to make
354 // sure that an assert is hit if the location of the subprogram array
355 // changes. This is just to make sure that this is updated if such an
356 // event occurs.
357 assert(DIC->getNumOperands() >= 10 &&
358 SPs == DIC->getOperand(9) &&
359 "DICompileUnits is expected to store Subprograms in operand "
360 "9.");
361 DIC->replaceOperandWith(9, MDNode::get(C, LiveSubprograms));
362 Changed = true;
363 }
364
365 if (GlobalVariableChange) {
366 // Make sure that 10 is still the index of global variables. This is to
367 // make sure that an assert is hit if the location of the subprogram array
368 // changes. This is just to make sure that this index is updated if such
369 // an event occurs.
370 assert(DIC->getNumOperands() >= 11 &&
371 GVs == DIC->getOperand(10) &&
372 "DICompileUnits is expected to store Global Variables in operand "
373 "10.");
374 DIC->replaceOperandWith(10, MDNode::get(C, LiveGlobalVariables));
375 Changed = true;
376 }
377
378 // Reset lists for the next iteration.
379 LiveSubprograms.clear();
380 LiveGlobalVariables.clear();
Devang Patel2b434e12010-07-01 19:49:20 +0000381 }
382
383 return Changed;
384}