blob: 8f6f161428e89daf665fa93a087379ee20cd5359 [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/SmallPtrSet.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000025#include "llvm/IR/Constants.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000026#include "llvm/IR/DebugInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000027#include "llvm/IR/DerivedTypes.h"
28#include "llvm/IR/Instructions.h"
29#include "llvm/IR/Module.h"
Chandler Carruthdcb603f2013-01-07 15:43:51 +000030#include "llvm/IR/TypeFinder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000031#include "llvm/IR/ValueSymbolTable.h"
Chris Lattnere8ebcb32004-12-02 21:25:03 +000032#include "llvm/Pass.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000033#include "llvm/Transforms/Utils/Local.h"
Chris Lattnere8ebcb32004-12-02 21:25:03 +000034using namespace llvm;
35
36namespace {
Nick Lewycky88214fb2009-09-03 06:43:15 +000037 class StripSymbols : public ModulePass {
Chris Lattnere8ebcb32004-12-02 21:25:03 +000038 bool OnlyDebugInfo;
39 public:
Nick Lewyckye7da2d62007-05-06 13:37:16 +000040 static char ID; // Pass identification, replacement for typeid
Michael Gottesman0900993c2013-08-21 22:53:29 +000041 explicit StripSymbols(bool ODI = false)
Owen Anderson6c18d1a2010-10-19 17:21:58 +000042 : ModulePass(ID), OnlyDebugInfo(ODI) {
43 initializeStripSymbolsPass(*PassRegistry::getPassRegistry());
44 }
Chris Lattnere8ebcb32004-12-02 21:25:03 +000045
Craig Topper3e4c6972014-03-05 09:10:37 +000046 bool runOnModule(Module &M) override;
Devang Patel8ada1d52008-11-14 22:49:37 +000047
Craig Topper3e4c6972014-03-05 09:10:37 +000048 void getAnalysisUsage(AnalysisUsage &AU) const override {
Devang Patelb5e867a2008-11-18 21:34:39 +000049 AU.setPreservesAll();
50 }
51 };
52
Nick Lewycky88214fb2009-09-03 06:43:15 +000053 class StripNonDebugSymbols : public ModulePass {
Devang Patelb5e867a2008-11-18 21:34:39 +000054 public:
55 static char ID; // Pass identification, replacement for typeid
56 explicit StripNonDebugSymbols()
Owen Anderson6c18d1a2010-10-19 17:21:58 +000057 : ModulePass(ID) {
58 initializeStripNonDebugSymbolsPass(*PassRegistry::getPassRegistry());
59 }
Devang Patel8ada1d52008-11-14 22:49:37 +000060
Craig Topper3e4c6972014-03-05 09:10:37 +000061 bool runOnModule(Module &M) override;
Chris Lattnere8ebcb32004-12-02 21:25:03 +000062
Craig Topper3e4c6972014-03-05 09:10:37 +000063 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chris Lattnere8ebcb32004-12-02 21:25:03 +000064 AU.setPreservesAll();
65 }
66 };
Devang Patel66f84e72009-03-09 20:49:37 +000067
Nick Lewycky88214fb2009-09-03 06:43:15 +000068 class StripDebugDeclare : public ModulePass {
Devang Patel66f84e72009-03-09 20:49:37 +000069 public:
70 static char ID; // Pass identification, replacement for typeid
71 explicit StripDebugDeclare()
Owen Anderson6c18d1a2010-10-19 17:21:58 +000072 : ModulePass(ID) {
73 initializeStripDebugDeclarePass(*PassRegistry::getPassRegistry());
74 }
Devang Patel66f84e72009-03-09 20:49:37 +000075
Craig Topper3e4c6972014-03-05 09:10:37 +000076 bool runOnModule(Module &M) override;
Devang Patel66f84e72009-03-09 20:49:37 +000077
Craig Topper3e4c6972014-03-05 09:10:37 +000078 void getAnalysisUsage(AnalysisUsage &AU) const override {
Devang Patel66f84e72009-03-09 20:49:37 +000079 AU.setPreservesAll();
80 }
81 };
Devang Patel2b434e12010-07-01 19:49:20 +000082
83 class StripDeadDebugInfo : public ModulePass {
84 public:
85 static char ID; // Pass identification, replacement for typeid
86 explicit StripDeadDebugInfo()
Owen Anderson6c18d1a2010-10-19 17:21:58 +000087 : ModulePass(ID) {
88 initializeStripDeadDebugInfoPass(*PassRegistry::getPassRegistry());
89 }
Devang Patel2b434e12010-07-01 19:49:20 +000090
Craig Topper3e4c6972014-03-05 09:10:37 +000091 bool runOnModule(Module &M) override;
Devang Patel2b434e12010-07-01 19:49:20 +000092
Craig Topper3e4c6972014-03-05 09:10:37 +000093 void getAnalysisUsage(AnalysisUsage &AU) const override {
Devang Patel2b434e12010-07-01 19:49:20 +000094 AU.setPreservesAll();
95 }
96 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000097}
Chris Lattnere8ebcb32004-12-02 21:25:03 +000098
Dan Gohmand78c4002008-05-13 00:00:25 +000099char StripSymbols::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +0000100INITIALIZE_PASS(StripSymbols, "strip",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000101 "Strip all symbols from a module", false, false)
Dan Gohmand78c4002008-05-13 00:00:25 +0000102
Chris Lattnere8ebcb32004-12-02 21:25:03 +0000103ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
104 return new StripSymbols(OnlyDebugInfo);
105}
106
Devang Patelb5e867a2008-11-18 21:34:39 +0000107char StripNonDebugSymbols::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +0000108INITIALIZE_PASS(StripNonDebugSymbols, "strip-nondebug",
109 "Strip all symbols, except dbg symbols, from a module",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000110 false, false)
Devang Patelb5e867a2008-11-18 21:34:39 +0000111
112ModulePass *llvm::createStripNonDebugSymbolsPass() {
113 return new StripNonDebugSymbols();
114}
115
Devang Patel66f84e72009-03-09 20:49:37 +0000116char StripDebugDeclare::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +0000117INITIALIZE_PASS(StripDebugDeclare, "strip-debug-declare",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000118 "Strip all llvm.dbg.declare intrinsics", false, false)
Devang Patel66f84e72009-03-09 20:49:37 +0000119
120ModulePass *llvm::createStripDebugDeclarePass() {
121 return new StripDebugDeclare();
122}
123
Devang Patel2b434e12010-07-01 19:49:20 +0000124char StripDeadDebugInfo::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +0000125INITIALIZE_PASS(StripDeadDebugInfo, "strip-dead-debug-info",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000126 "Strip debug info for unused symbols", false, false)
Devang Patel2b434e12010-07-01 19:49:20 +0000127
128ModulePass *llvm::createStripDeadDebugInfoPass() {
129 return new StripDeadDebugInfo();
130}
131
Devang Patel3dd51c52008-11-13 01:28:40 +0000132/// OnlyUsedBy - Return true if V is only used by Usr.
133static bool OnlyUsedBy(Value *V, Value *Usr) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000134 for (User *U : V->users())
Devang Patel3dd51c52008-11-13 01:28:40 +0000135 if (U != Usr)
136 return false;
Chandler Carruthcdf47882014-03-09 03:16:01 +0000137
Devang Patel3dd51c52008-11-13 01:28:40 +0000138 return true;
139}
140
Chris Lattner9019e5c2004-12-03 16:22:08 +0000141static void RemoveDeadConstant(Constant *C) {
142 assert(C->use_empty() && "Constant is not dead!");
Chris Lattnera91a5632009-10-28 05:14:34 +0000143 SmallPtrSet<Constant*, 4> Operands;
Pete Cooper125ad172015-06-25 20:51:38 +0000144 for (Value *Op : C->operands())
145 if (OnlyUsedBy(Op, C))
146 Operands.insert(cast<Constant>(Op));
Chris Lattner9019e5c2004-12-03 16:22:08 +0000147 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
Alp Tokerf907b892013-12-05 05:44:44 +0000148 if (!GV->hasLocalLinkage()) return; // Don't delete non-static globals.
Chris Lattner9019e5c2004-12-03 16:22:08 +0000149 GV->eraseFromParent();
150 }
151 else if (!isa<Function>(C))
Devang Patelc8b2fe12008-11-20 01:20:42 +0000152 if (isa<CompositeType>(C->getType()))
153 C->destroyConstant();
Misha Brukmanb1c93172005-04-21 23:48:37 +0000154
Chris Lattner9019e5c2004-12-03 16:22:08 +0000155 // If the constant referenced anything, see if we can delete it as well.
Craig Topper46276792014-08-24 23:23:06 +0000156 for (Constant *O : Operands)
157 RemoveDeadConstant(O);
Chris Lattner9019e5c2004-12-03 16:22:08 +0000158}
Chris Lattnere8ebcb32004-12-02 21:25:03 +0000159
Chris Lattner88051b02007-02-07 06:22:45 +0000160// Strip the symbol table of its names.
161//
Devang Patelb5e867a2008-11-18 21:34:39 +0000162static void StripSymtab(ValueSymbolTable &ST, bool PreserveDbgInfo) {
Chris Lattner88051b02007-02-07 06:22:45 +0000163 for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
Chris Lattner32ab6432007-02-12 05:18:08 +0000164 Value *V = VI->getValue();
Chris Lattner88051b02007-02-07 06:22:45 +0000165 ++VI;
Rafael Espindola6de96a12009-01-15 20:18:42 +0000166 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasLocalLinkage()) {
Daniel Dunbar6115b392009-07-26 09:48:23 +0000167 if (!PreserveDbgInfo || !V->getName().startswith("llvm.dbg"))
Devang Patelb5e867a2008-11-18 21:34:39 +0000168 // Set name to "", removing from symbol table!
169 V->setName("");
Chris Lattner88051b02007-02-07 06:22:45 +0000170 }
171 }
172}
173
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000174// Strip any named types of their names.
175static void StripTypeNames(Module &M, bool PreserveDbgInfo) {
Bill Wendling8555a372012-08-03 00:30:35 +0000176 TypeFinder StructTypes;
177 StructTypes.run(M, false);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000178
179 for (unsigned i = 0, e = StructTypes.size(); i != e; ++i) {
180 StructType *STy = StructTypes[i];
Chris Lattner335d3992011-08-12 18:06:37 +0000181 if (STy->isLiteral() || STy->getName().empty()) continue;
Michael Gottesman0900993c2013-08-21 22:53:29 +0000182
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000183 if (PreserveDbgInfo && STy->getName().startswith("llvm.dbg"))
184 continue;
185
186 STy->setName("");
Devang Patelb5e867a2008-11-18 21:34:39 +0000187 }
Chris Lattner88051b02007-02-07 06:22:45 +0000188}
189
Devang Patel3b7a2be2008-11-18 21:13:41 +0000190/// Find values that are marked as llvm.used.
Chris Lattner58f9bb22009-07-20 06:14:25 +0000191static void findUsedValues(GlobalVariable *LLVMUsed,
Craig Topper71b7b682014-08-21 05:55:13 +0000192 SmallPtrSetImpl<const GlobalValue*> &UsedValues) {
Craig Topperf40110f2014-04-25 05:29:35 +0000193 if (!LLVMUsed) return;
Chris Lattner58f9bb22009-07-20 06:14:25 +0000194 UsedValues.insert(LLVMUsed);
Rafael Espindola74f2e462013-04-22 14:58:02 +0000195
196 ConstantArray *Inits = cast<ConstantArray>(LLVMUsed->getInitializer());
197
Chris Lattner58f9bb22009-07-20 06:14:25 +0000198 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
Michael Gottesman0900993c2013-08-21 22:53:29 +0000199 if (GlobalValue *GV =
Chris Lattner58f9bb22009-07-20 06:14:25 +0000200 dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
201 UsedValues.insert(GV);
Devang Patel3b7a2be2008-11-18 21:13:41 +0000202}
203
204/// StripSymbolNames - Strip symbol names.
Dan Gohmana6d0afc2009-08-07 01:32:21 +0000205static bool StripSymbolNames(Module &M, bool PreserveDbgInfo) {
Devang Patel3b7a2be2008-11-18 21:13:41 +0000206
207 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
Chris Lattner58f9bb22009-07-20 06:14:25 +0000208 findUsedValues(M.getGlobalVariable("llvm.used"), llvmUsedValues);
209 findUsedValues(M.getGlobalVariable("llvm.compiler.used"), llvmUsedValues);
Devang Patel3b7a2be2008-11-18 21:13:41 +0000210
Devang Patel8ada1d52008-11-14 22:49:37 +0000211 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
212 I != E; ++I) {
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000213 if (I->hasLocalLinkage() && llvmUsedValues.count(&*I) == 0)
Daniel Dunbar6115b392009-07-26 09:48:23 +0000214 if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg"))
Devang Patelb5e867a2008-11-18 21:34:39 +0000215 I->setName(""); // Internal symbols can't participate in linkage
Devang Patel8ada1d52008-11-14 22:49:37 +0000216 }
Michael Gottesman0900993c2013-08-21 22:53:29 +0000217
Benjamin Kramer135f7352016-06-26 12:28:59 +0000218 for (Function &I : M) {
219 if (I.hasLocalLinkage() && llvmUsedValues.count(&I) == 0)
220 if (!PreserveDbgInfo || !I.getName().startswith("llvm.dbg"))
221 I.setName(""); // Internal symbols can't participate in linkage
Mehdi Aminia53d49e2016-09-17 06:00:02 +0000222 if (auto *Symtab = I.getValueSymbolTable())
223 StripSymtab(*Symtab, PreserveDbgInfo);
Devang Patel8ada1d52008-11-14 22:49:37 +0000224 }
Michael Gottesman0900993c2013-08-21 22:53:29 +0000225
Devang Patel8ada1d52008-11-14 22:49:37 +0000226 // Remove all names from types.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000227 StripTypeNames(M, PreserveDbgInfo);
Chris Lattnere8ebcb32004-12-02 21:25:03 +0000228
Devang Patel8ada1d52008-11-14 22:49:37 +0000229 return true;
230}
231
Devang Patelb5e867a2008-11-18 21:34:39 +0000232bool StripSymbols::runOnModule(Module &M) {
Andrew Kayloraa641a52016-04-22 22:06:11 +0000233 if (skipModule(M))
234 return false;
235
Devang Patelb5e867a2008-11-18 21:34:39 +0000236 bool Changed = false;
237 Changed |= StripDebugInfo(M);
238 if (!OnlyDebugInfo)
239 Changed |= StripSymbolNames(M, false);
240 return Changed;
241}
242
243bool StripNonDebugSymbols::runOnModule(Module &M) {
Andrew Kayloraa641a52016-04-22 22:06:11 +0000244 if (skipModule(M))
245 return false;
246
Devang Patelb5e867a2008-11-18 21:34:39 +0000247 return StripSymbolNames(M, true);
248}
Devang Patel66f84e72009-03-09 20:49:37 +0000249
250bool StripDebugDeclare::runOnModule(Module &M) {
Andrew Kayloraa641a52016-04-22 22:06:11 +0000251 if (skipModule(M))
252 return false;
Devang Patel66f84e72009-03-09 20:49:37 +0000253
254 Function *Declare = M.getFunction("llvm.dbg.declare");
Devang Patel66f84e72009-03-09 20:49:37 +0000255 std::vector<Constant*> DeadConstants;
256
Dale Johannesena4ac7352009-03-13 22:59:47 +0000257 if (Declare) {
258 while (!Declare->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000259 CallInst *CI = cast<CallInst>(Declare->user_back());
Gabor Greifd5057282010-06-30 12:40:35 +0000260 Value *Arg1 = CI->getArgOperand(0);
261 Value *Arg2 = CI->getArgOperand(1);
Dale Johannesena4ac7352009-03-13 22:59:47 +0000262 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
263 CI->eraseFromParent();
264 if (Arg1->use_empty()) {
Michael Gottesman0900993c2013-08-21 22:53:29 +0000265 if (Constant *C = dyn_cast<Constant>(Arg1))
Dale Johannesena4ac7352009-03-13 22:59:47 +0000266 DeadConstants.push_back(C);
Michael Gottesman0900993c2013-08-21 22:53:29 +0000267 else
Dan Gohmanb17dcbd2009-05-02 20:22:10 +0000268 RecursivelyDeleteTriviallyDeadInstructions(Arg1);
Dale Johannesena4ac7352009-03-13 22:59:47 +0000269 }
270 if (Arg2->use_empty())
Michael Gottesman0900993c2013-08-21 22:53:29 +0000271 if (Constant *C = dyn_cast<Constant>(Arg2))
Dale Johannesena4ac7352009-03-13 22:59:47 +0000272 DeadConstants.push_back(C);
Devang Patel66f84e72009-03-09 20:49:37 +0000273 }
Dale Johannesena4ac7352009-03-13 22:59:47 +0000274 Declare->eraseFromParent();
Devang Patel66f84e72009-03-09 20:49:37 +0000275 }
Devang Patel66f84e72009-03-09 20:49:37 +0000276
277 while (!DeadConstants.empty()) {
278 Constant *C = DeadConstants.back();
279 DeadConstants.pop_back();
280 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
281 if (GV->hasLocalLinkage())
282 RemoveDeadConstant(GV);
Chris Lattnera91a5632009-10-28 05:14:34 +0000283 } else
Devang Patel66f84e72009-03-09 20:49:37 +0000284 RemoveDeadConstant(C);
285 }
286
287 return true;
288}
Devang Patel2b434e12010-07-01 19:49:20 +0000289
Michael Gottesman823aaff2013-08-23 00:23:24 +0000290/// Remove any debug info for global variables/functions in the given module for
291/// which said global variable/function no longer exists (i.e. is null).
292///
293/// Debugging information is encoded in llvm IR using metadata. This is designed
294/// such a way that debug info for symbols preserved even if symbols are
295/// optimized away by the optimizer. This special pass removes debug info for
296/// such symbols.
Devang Patel2b434e12010-07-01 19:49:20 +0000297bool StripDeadDebugInfo::runOnModule(Module &M) {
Andrew Kayloraa641a52016-04-22 22:06:11 +0000298 if (skipModule(M))
299 return false;
300
Devang Patel2b434e12010-07-01 19:49:20 +0000301 bool Changed = false;
302
Michael Gottesman823aaff2013-08-23 00:23:24 +0000303 LLVMContext &C = M.getContext();
Devang Patel2b434e12010-07-01 19:49:20 +0000304
Michael Gottesman823aaff2013-08-23 00:23:24 +0000305 // Find all debug info in F. This is actually overkill in terms of what we
Michael Gottesmaneab9a7f2013-08-27 04:43:03 +0000306 // want to do, but we want to try and be as resilient as possible in the face
Michael Gottesman823aaff2013-08-23 00:23:24 +0000307 // of potential debug info changes by using the formal interfaces given to us
308 // as much as possible.
309 DebugInfoFinder F;
310 F.processModule(M);
Devang Patel2b434e12010-07-01 19:49:20 +0000311
Michael Gottesman823aaff2013-08-23 00:23:24 +0000312 // For each compile unit, find the live set of global variables/functions and
313 // replace the current list of potentially dead global variables/functions
314 // with the live list.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000315 SmallVector<Metadata *, 64> LiveGlobalVariables;
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000316 DenseSet<DIGlobalVariableExpression *> VisitedSet;
Michael Gottesman823aaff2013-08-23 00:23:24 +0000317
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000318 std::set<DIGlobalVariableExpression *> LiveGVs;
Peter Collingbourned4135bb2016-09-13 01:12:59 +0000319 for (GlobalVariable &GV : M.globals()) {
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000320 SmallVector<DIGlobalVariableExpression *, 1> GVEs;
321 GV.getDebugInfo(GVEs);
322 for (auto *GVE : GVEs)
323 LiveGVs.insert(GVE);
Peter Collingbourned4bff302015-11-05 22:03:56 +0000324 }
325
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000326 for (DICompileUnit *DIC : F.compile_units()) {
Michael Gottesman823aaff2013-08-23 00:23:24 +0000327 // Create our live global variable list.
Michael Gottesman823aaff2013-08-23 00:23:24 +0000328 bool GlobalVariableChange = false;
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000329 for (auto *DIG : DIC->getGlobalVariables()) {
330 if (DIG->getExpression() && DIG->getExpression()->isConstant())
331 LiveGVs.insert(DIG);
332
Michael Gottesman823aaff2013-08-23 00:23:24 +0000333 // Make sure we only visit each global variable only once.
334 if (!VisitedSet.insert(DIG).second)
335 continue;
336
Peter Collingbourned4135bb2016-09-13 01:12:59 +0000337 // If a global variable references DIG, the global variable is live.
338 if (LiveGVs.count(DIG))
Michael Gottesman823aaff2013-08-23 00:23:24 +0000339 LiveGlobalVariables.push_back(DIG);
Devang Patel2b434e12010-07-01 19:49:20 +0000340 else
Michael Gottesman823aaff2013-08-23 00:23:24 +0000341 GlobalVariableChange = true;
Devang Patel2b434e12010-07-01 19:49:20 +0000342 }
Michael Gottesman823aaff2013-08-23 00:23:24 +0000343
Adrian Prantl75819ae2016-04-15 15:57:41 +0000344 // If we found dead global variables, replace the current global
345 // variable list with our new live global variable list.
Michael Gottesman823aaff2013-08-23 00:23:24 +0000346 if (GlobalVariableChange) {
Duncan P. N. Exon Smith35ef22c2015-04-15 23:19:27 +0000347 DIC->replaceGlobalVariables(MDTuple::get(C, LiveGlobalVariables));
Michael Gottesman823aaff2013-08-23 00:23:24 +0000348 Changed = true;
349 }
350
351 // Reset lists for the next iteration.
Michael Gottesman823aaff2013-08-23 00:23:24 +0000352 LiveGlobalVariables.clear();
Devang Patel2b434e12010-07-01 19:49:20 +0000353 }
354
355 return Changed;
356}