blob: 67a473612fc103cfe3d613a17e1f8a64d7f8e2ff [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Misha Brukmanb1c93172005-04-21 23:48:37 +00006//
Chris Lattnere8ebcb32004-12-02 21:25:03 +00007//===----------------------------------------------------------------------===//
8//
Gordon Henriksend5687672007-11-04 16:15:04 +00009// The StripSymbols transformation implements code stripping. Specifically, it
10// can delete:
Michael Gottesman0900993c2013-08-21 22:53:29 +000011//
Gordon Henriksend5687672007-11-04 16:15:04 +000012// * names for virtual registers
13// * symbols for internal globals and functions
14// * debug information
Chris Lattnere8ebcb32004-12-02 21:25:03 +000015//
Gordon Henriksend5687672007-11-04 16:15:04 +000016// Note that this transformation makes code much less readable, so it should
17// only be used in situations where the 'strip' utility would be used, such as
18// reducing code size or making it harder to reverse engineer code.
Chris Lattnere8ebcb32004-12-02 21:25:03 +000019//
20//===----------------------------------------------------------------------===//
21
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/ADT/SmallPtrSet.h"
David Blaikie31b98d22018-06-04 21:23:21 +000023#include "llvm/Transforms/Utils/Local.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000024#include "llvm/IR/Constants.h"
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000025#include "llvm/IR/DebugInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000026#include "llvm/IR/DerivedTypes.h"
27#include "llvm/IR/Instructions.h"
28#include "llvm/IR/Module.h"
Chandler Carruthdcb603f2013-01-07 15:43:51 +000029#include "llvm/IR/TypeFinder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000030#include "llvm/IR/ValueSymbolTable.h"
Chris Lattnere8ebcb32004-12-02 21:25:03 +000031#include "llvm/Pass.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000032#include "llvm/Transforms/IPO.h"
Chris Lattnere8ebcb32004-12-02 21:25:03 +000033using namespace llvm;
34
35namespace {
Nick Lewycky88214fb2009-09-03 06:43:15 +000036 class StripSymbols : public ModulePass {
Chris Lattnere8ebcb32004-12-02 21:25:03 +000037 bool OnlyDebugInfo;
38 public:
Nick Lewyckye7da2d62007-05-06 13:37:16 +000039 static char ID; // Pass identification, replacement for typeid
Michael Gottesman0900993c2013-08-21 22:53:29 +000040 explicit StripSymbols(bool ODI = false)
Owen Anderson6c18d1a2010-10-19 17:21:58 +000041 : ModulePass(ID), OnlyDebugInfo(ODI) {
42 initializeStripSymbolsPass(*PassRegistry::getPassRegistry());
43 }
Chris Lattnere8ebcb32004-12-02 21:25:03 +000044
Craig Topper3e4c6972014-03-05 09:10:37 +000045 bool runOnModule(Module &M) override;
Devang Patel8ada1d52008-11-14 22:49:37 +000046
Craig Topper3e4c6972014-03-05 09:10:37 +000047 void getAnalysisUsage(AnalysisUsage &AU) const override {
Devang Patelb5e867a2008-11-18 21:34:39 +000048 AU.setPreservesAll();
49 }
50 };
51
Nick Lewycky88214fb2009-09-03 06:43:15 +000052 class StripNonDebugSymbols : public ModulePass {
Devang Patelb5e867a2008-11-18 21:34:39 +000053 public:
54 static char ID; // Pass identification, replacement for typeid
55 explicit StripNonDebugSymbols()
Owen Anderson6c18d1a2010-10-19 17:21:58 +000056 : ModulePass(ID) {
57 initializeStripNonDebugSymbolsPass(*PassRegistry::getPassRegistry());
58 }
Devang Patel8ada1d52008-11-14 22:49:37 +000059
Craig Topper3e4c6972014-03-05 09:10:37 +000060 bool runOnModule(Module &M) override;
Chris Lattnere8ebcb32004-12-02 21:25:03 +000061
Craig Topper3e4c6972014-03-05 09:10:37 +000062 void getAnalysisUsage(AnalysisUsage &AU) const override {
Chris Lattnere8ebcb32004-12-02 21:25:03 +000063 AU.setPreservesAll();
64 }
65 };
Devang Patel66f84e72009-03-09 20:49:37 +000066
Nick Lewycky88214fb2009-09-03 06:43:15 +000067 class StripDebugDeclare : public ModulePass {
Devang Patel66f84e72009-03-09 20:49:37 +000068 public:
69 static char ID; // Pass identification, replacement for typeid
70 explicit StripDebugDeclare()
Owen Anderson6c18d1a2010-10-19 17:21:58 +000071 : ModulePass(ID) {
72 initializeStripDebugDeclarePass(*PassRegistry::getPassRegistry());
73 }
Devang Patel66f84e72009-03-09 20:49:37 +000074
Craig Topper3e4c6972014-03-05 09:10:37 +000075 bool runOnModule(Module &M) override;
Devang Patel66f84e72009-03-09 20:49:37 +000076
Craig Topper3e4c6972014-03-05 09:10:37 +000077 void getAnalysisUsage(AnalysisUsage &AU) const override {
Devang Patel66f84e72009-03-09 20:49:37 +000078 AU.setPreservesAll();
79 }
80 };
Devang Patel2b434e12010-07-01 19:49:20 +000081
82 class StripDeadDebugInfo : public ModulePass {
83 public:
84 static char ID; // Pass identification, replacement for typeid
85 explicit StripDeadDebugInfo()
Owen Anderson6c18d1a2010-10-19 17:21:58 +000086 : ModulePass(ID) {
87 initializeStripDeadDebugInfoPass(*PassRegistry::getPassRegistry());
88 }
Devang Patel2b434e12010-07-01 19:49:20 +000089
Craig Topper3e4c6972014-03-05 09:10:37 +000090 bool runOnModule(Module &M) override;
Devang Patel2b434e12010-07-01 19:49:20 +000091
Craig Topper3e4c6972014-03-05 09:10:37 +000092 void getAnalysisUsage(AnalysisUsage &AU) const override {
Devang Patel2b434e12010-07-01 19:49:20 +000093 AU.setPreservesAll();
94 }
95 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000096}
Chris Lattnere8ebcb32004-12-02 21:25:03 +000097
Dan Gohmand78c4002008-05-13 00:00:25 +000098char StripSymbols::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +000099INITIALIZE_PASS(StripSymbols, "strip",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000100 "Strip all symbols from a module", false, false)
Dan Gohmand78c4002008-05-13 00:00:25 +0000101
Chris Lattnere8ebcb32004-12-02 21:25:03 +0000102ModulePass *llvm::createStripSymbolsPass(bool OnlyDebugInfo) {
103 return new StripSymbols(OnlyDebugInfo);
104}
105
Devang Patelb5e867a2008-11-18 21:34:39 +0000106char StripNonDebugSymbols::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +0000107INITIALIZE_PASS(StripNonDebugSymbols, "strip-nondebug",
108 "Strip all symbols, except dbg symbols, from a module",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000109 false, false)
Devang Patelb5e867a2008-11-18 21:34:39 +0000110
111ModulePass *llvm::createStripNonDebugSymbolsPass() {
112 return new StripNonDebugSymbols();
113}
114
Devang Patel66f84e72009-03-09 20:49:37 +0000115char StripDebugDeclare::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +0000116INITIALIZE_PASS(StripDebugDeclare, "strip-debug-declare",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000117 "Strip all llvm.dbg.declare intrinsics", false, false)
Devang Patel66f84e72009-03-09 20:49:37 +0000118
119ModulePass *llvm::createStripDebugDeclarePass() {
120 return new StripDebugDeclare();
121}
122
Devang Patel2b434e12010-07-01 19:49:20 +0000123char StripDeadDebugInfo::ID = 0;
Owen Andersona57b97e2010-07-21 22:09:45 +0000124INITIALIZE_PASS(StripDeadDebugInfo, "strip-dead-debug-info",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000125 "Strip debug info for unused symbols", false, false)
Devang Patel2b434e12010-07-01 19:49:20 +0000126
127ModulePass *llvm::createStripDeadDebugInfoPass() {
128 return new StripDeadDebugInfo();
129}
130
Devang Patel3dd51c52008-11-13 01:28:40 +0000131/// OnlyUsedBy - Return true if V is only used by Usr.
132static bool OnlyUsedBy(Value *V, Value *Usr) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000133 for (User *U : V->users())
Devang Patel3dd51c52008-11-13 01:28:40 +0000134 if (U != Usr)
135 return false;
Chandler Carruthcdf47882014-03-09 03:16:01 +0000136
Devang Patel3dd51c52008-11-13 01:28:40 +0000137 return true;
138}
139
Chris Lattner9019e5c2004-12-03 16:22:08 +0000140static void RemoveDeadConstant(Constant *C) {
141 assert(C->use_empty() && "Constant is not dead!");
Chris Lattnera91a5632009-10-28 05:14:34 +0000142 SmallPtrSet<Constant*, 4> Operands;
Pete Cooper125ad172015-06-25 20:51:38 +0000143 for (Value *Op : C->operands())
144 if (OnlyUsedBy(Op, C))
145 Operands.insert(cast<Constant>(Op));
Chris Lattner9019e5c2004-12-03 16:22:08 +0000146 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
Alp Tokerf907b892013-12-05 05:44:44 +0000147 if (!GV->hasLocalLinkage()) return; // Don't delete non-static globals.
Chris Lattner9019e5c2004-12-03 16:22:08 +0000148 GV->eraseFromParent();
149 }
150 else if (!isa<Function>(C))
Devang Patelc8b2fe12008-11-20 01:20:42 +0000151 if (isa<CompositeType>(C->getType()))
152 C->destroyConstant();
Misha Brukmanb1c93172005-04-21 23:48:37 +0000153
Chris Lattner9019e5c2004-12-03 16:22:08 +0000154 // If the constant referenced anything, see if we can delete it as well.
Craig Topper46276792014-08-24 23:23:06 +0000155 for (Constant *O : Operands)
156 RemoveDeadConstant(O);
Chris Lattner9019e5c2004-12-03 16:22:08 +0000157}
Chris Lattnere8ebcb32004-12-02 21:25:03 +0000158
Chris Lattner88051b02007-02-07 06:22:45 +0000159// Strip the symbol table of its names.
160//
Devang Patelb5e867a2008-11-18 21:34:39 +0000161static void StripSymtab(ValueSymbolTable &ST, bool PreserveDbgInfo) {
Chris Lattner88051b02007-02-07 06:22:45 +0000162 for (ValueSymbolTable::iterator VI = ST.begin(), VE = ST.end(); VI != VE; ) {
Chris Lattner32ab6432007-02-12 05:18:08 +0000163 Value *V = VI->getValue();
Chris Lattner88051b02007-02-07 06:22:45 +0000164 ++VI;
Rafael Espindola6de96a12009-01-15 20:18:42 +0000165 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasLocalLinkage()) {
Daniel Dunbar6115b392009-07-26 09:48:23 +0000166 if (!PreserveDbgInfo || !V->getName().startswith("llvm.dbg"))
Devang Patelb5e867a2008-11-18 21:34:39 +0000167 // Set name to "", removing from symbol table!
168 V->setName("");
Chris Lattner88051b02007-02-07 06:22:45 +0000169 }
170 }
171}
172
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000173// Strip any named types of their names.
174static void StripTypeNames(Module &M, bool PreserveDbgInfo) {
Bill Wendling8555a372012-08-03 00:30:35 +0000175 TypeFinder StructTypes;
176 StructTypes.run(M, false);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000177
178 for (unsigned i = 0, e = StructTypes.size(); i != e; ++i) {
179 StructType *STy = StructTypes[i];
Chris Lattner335d3992011-08-12 18:06:37 +0000180 if (STy->isLiteral() || STy->getName().empty()) continue;
Michael Gottesman0900993c2013-08-21 22:53:29 +0000181
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000182 if (PreserveDbgInfo && STy->getName().startswith("llvm.dbg"))
183 continue;
184
185 STy->setName("");
Devang Patelb5e867a2008-11-18 21:34:39 +0000186 }
Chris Lattner88051b02007-02-07 06:22:45 +0000187}
188
Devang Patel3b7a2be2008-11-18 21:13:41 +0000189/// Find values that are marked as llvm.used.
Chris Lattner58f9bb22009-07-20 06:14:25 +0000190static void findUsedValues(GlobalVariable *LLVMUsed,
Craig Topper71b7b682014-08-21 05:55:13 +0000191 SmallPtrSetImpl<const GlobalValue*> &UsedValues) {
Craig Topperf40110f2014-04-25 05:29:35 +0000192 if (!LLVMUsed) return;
Chris Lattner58f9bb22009-07-20 06:14:25 +0000193 UsedValues.insert(LLVMUsed);
Rafael Espindola74f2e462013-04-22 14:58:02 +0000194
195 ConstantArray *Inits = cast<ConstantArray>(LLVMUsed->getInitializer());
196
Chris Lattner58f9bb22009-07-20 06:14:25 +0000197 for (unsigned i = 0, e = Inits->getNumOperands(); i != e; ++i)
Michael Gottesman0900993c2013-08-21 22:53:29 +0000198 if (GlobalValue *GV =
Chris Lattner58f9bb22009-07-20 06:14:25 +0000199 dyn_cast<GlobalValue>(Inits->getOperand(i)->stripPointerCasts()))
200 UsedValues.insert(GV);
Devang Patel3b7a2be2008-11-18 21:13:41 +0000201}
202
203/// StripSymbolNames - Strip symbol names.
Dan Gohmana6d0afc2009-08-07 01:32:21 +0000204static bool StripSymbolNames(Module &M, bool PreserveDbgInfo) {
Devang Patel3b7a2be2008-11-18 21:13:41 +0000205
206 SmallPtrSet<const GlobalValue*, 8> llvmUsedValues;
Chris Lattner58f9bb22009-07-20 06:14:25 +0000207 findUsedValues(M.getGlobalVariable("llvm.used"), llvmUsedValues);
208 findUsedValues(M.getGlobalVariable("llvm.compiler.used"), llvmUsedValues);
Devang Patel3b7a2be2008-11-18 21:13:41 +0000209
Devang Patel8ada1d52008-11-14 22:49:37 +0000210 for (Module::global_iterator I = M.global_begin(), E = M.global_end();
211 I != E; ++I) {
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000212 if (I->hasLocalLinkage() && llvmUsedValues.count(&*I) == 0)
Daniel Dunbar6115b392009-07-26 09:48:23 +0000213 if (!PreserveDbgInfo || !I->getName().startswith("llvm.dbg"))
Devang Patelb5e867a2008-11-18 21:34:39 +0000214 I->setName(""); // Internal symbols can't participate in linkage
Devang Patel8ada1d52008-11-14 22:49:37 +0000215 }
Michael Gottesman0900993c2013-08-21 22:53:29 +0000216
Benjamin Kramer135f7352016-06-26 12:28:59 +0000217 for (Function &I : M) {
218 if (I.hasLocalLinkage() && llvmUsedValues.count(&I) == 0)
219 if (!PreserveDbgInfo || !I.getName().startswith("llvm.dbg"))
220 I.setName(""); // Internal symbols can't participate in linkage
Mehdi Aminia53d49e2016-09-17 06:00:02 +0000221 if (auto *Symtab = I.getValueSymbolTable())
222 StripSymtab(*Symtab, PreserveDbgInfo);
Devang Patel8ada1d52008-11-14 22:49:37 +0000223 }
Michael Gottesman0900993c2013-08-21 22:53:29 +0000224
Devang Patel8ada1d52008-11-14 22:49:37 +0000225 // Remove all names from types.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000226 StripTypeNames(M, PreserveDbgInfo);
Chris Lattnere8ebcb32004-12-02 21:25:03 +0000227
Devang Patel8ada1d52008-11-14 22:49:37 +0000228 return true;
229}
230
Devang Patelb5e867a2008-11-18 21:34:39 +0000231bool StripSymbols::runOnModule(Module &M) {
Andrew Kayloraa641a52016-04-22 22:06:11 +0000232 if (skipModule(M))
233 return false;
234
Devang Patelb5e867a2008-11-18 21:34:39 +0000235 bool Changed = false;
236 Changed |= StripDebugInfo(M);
237 if (!OnlyDebugInfo)
238 Changed |= StripSymbolNames(M, false);
239 return Changed;
240}
241
242bool StripNonDebugSymbols::runOnModule(Module &M) {
Andrew Kayloraa641a52016-04-22 22:06:11 +0000243 if (skipModule(M))
244 return false;
245
Devang Patelb5e867a2008-11-18 21:34:39 +0000246 return StripSymbolNames(M, true);
247}
Devang Patel66f84e72009-03-09 20:49:37 +0000248
249bool StripDebugDeclare::runOnModule(Module &M) {
Andrew Kayloraa641a52016-04-22 22:06:11 +0000250 if (skipModule(M))
251 return false;
Devang Patel66f84e72009-03-09 20:49:37 +0000252
253 Function *Declare = M.getFunction("llvm.dbg.declare");
Devang Patel66f84e72009-03-09 20:49:37 +0000254 std::vector<Constant*> DeadConstants;
255
Dale Johannesena4ac7352009-03-13 22:59:47 +0000256 if (Declare) {
257 while (!Declare->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000258 CallInst *CI = cast<CallInst>(Declare->user_back());
Gabor Greifd5057282010-06-30 12:40:35 +0000259 Value *Arg1 = CI->getArgOperand(0);
260 Value *Arg2 = CI->getArgOperand(1);
Dale Johannesena4ac7352009-03-13 22:59:47 +0000261 assert(CI->use_empty() && "llvm.dbg intrinsic should have void result");
262 CI->eraseFromParent();
263 if (Arg1->use_empty()) {
Michael Gottesman0900993c2013-08-21 22:53:29 +0000264 if (Constant *C = dyn_cast<Constant>(Arg1))
Dale Johannesena4ac7352009-03-13 22:59:47 +0000265 DeadConstants.push_back(C);
Michael Gottesman0900993c2013-08-21 22:53:29 +0000266 else
Dan Gohmanb17dcbd2009-05-02 20:22:10 +0000267 RecursivelyDeleteTriviallyDeadInstructions(Arg1);
Dale Johannesena4ac7352009-03-13 22:59:47 +0000268 }
269 if (Arg2->use_empty())
Michael Gottesman0900993c2013-08-21 22:53:29 +0000270 if (Constant *C = dyn_cast<Constant>(Arg2))
Dale Johannesena4ac7352009-03-13 22:59:47 +0000271 DeadConstants.push_back(C);
Devang Patel66f84e72009-03-09 20:49:37 +0000272 }
Dale Johannesena4ac7352009-03-13 22:59:47 +0000273 Declare->eraseFromParent();
Devang Patel66f84e72009-03-09 20:49:37 +0000274 }
Devang Patel66f84e72009-03-09 20:49:37 +0000275
276 while (!DeadConstants.empty()) {
277 Constant *C = DeadConstants.back();
278 DeadConstants.pop_back();
279 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(C)) {
280 if (GV->hasLocalLinkage())
281 RemoveDeadConstant(GV);
Chris Lattnera91a5632009-10-28 05:14:34 +0000282 } else
Devang Patel66f84e72009-03-09 20:49:37 +0000283 RemoveDeadConstant(C);
284 }
285
286 return true;
287}
Devang Patel2b434e12010-07-01 19:49:20 +0000288
Michael Gottesman823aaff2013-08-23 00:23:24 +0000289/// Remove any debug info for global variables/functions in the given module for
290/// which said global variable/function no longer exists (i.e. is null).
291///
292/// Debugging information is encoded in llvm IR using metadata. This is designed
293/// such a way that debug info for symbols preserved even if symbols are
294/// optimized away by the optimizer. This special pass removes debug info for
295/// such symbols.
Devang Patel2b434e12010-07-01 19:49:20 +0000296bool StripDeadDebugInfo::runOnModule(Module &M) {
Andrew Kayloraa641a52016-04-22 22:06:11 +0000297 if (skipModule(M))
298 return false;
299
Devang Patel2b434e12010-07-01 19:49:20 +0000300 bool Changed = false;
301
Michael Gottesman823aaff2013-08-23 00:23:24 +0000302 LLVMContext &C = M.getContext();
Devang Patel2b434e12010-07-01 19:49:20 +0000303
Michael Gottesman823aaff2013-08-23 00:23:24 +0000304 // Find all debug info in F. This is actually overkill in terms of what we
Michael Gottesmaneab9a7f2013-08-27 04:43:03 +0000305 // want to do, but we want to try and be as resilient as possible in the face
Michael Gottesman823aaff2013-08-23 00:23:24 +0000306 // of potential debug info changes by using the formal interfaces given to us
307 // as much as possible.
308 DebugInfoFinder F;
309 F.processModule(M);
Devang Patel2b434e12010-07-01 19:49:20 +0000310
Michael Gottesman823aaff2013-08-23 00:23:24 +0000311 // For each compile unit, find the live set of global variables/functions and
312 // replace the current list of potentially dead global variables/functions
313 // with the live list.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000314 SmallVector<Metadata *, 64> LiveGlobalVariables;
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000315 DenseSet<DIGlobalVariableExpression *> VisitedSet;
Michael Gottesman823aaff2013-08-23 00:23:24 +0000316
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000317 std::set<DIGlobalVariableExpression *> LiveGVs;
Peter Collingbourned4135bb2016-09-13 01:12:59 +0000318 for (GlobalVariable &GV : M.globals()) {
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000319 SmallVector<DIGlobalVariableExpression *, 1> GVEs;
320 GV.getDebugInfo(GVEs);
321 for (auto *GVE : GVEs)
322 LiveGVs.insert(GVE);
Peter Collingbourned4bff302015-11-05 22:03:56 +0000323 }
324
Keno Fischerbacc64b2017-04-06 19:26:22 +0000325 std::set<DICompileUnit *> LiveCUs;
Keno Fischer30779772017-04-11 13:32:11 +0000326 // Any CU referenced from a subprogram is live.
327 for (DISubprogram *SP : F.subprograms()) {
328 if (SP->getUnit())
Keno Fischerbacc64b2017-04-06 19:26:22 +0000329 LiveCUs.insert(SP->getUnit());
330 }
331
332 bool HasDeadCUs = false;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000333 for (DICompileUnit *DIC : F.compile_units()) {
Michael Gottesman823aaff2013-08-23 00:23:24 +0000334 // Create our live global variable list.
Michael Gottesman823aaff2013-08-23 00:23:24 +0000335 bool GlobalVariableChange = false;
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000336 for (auto *DIG : DIC->getGlobalVariables()) {
337 if (DIG->getExpression() && DIG->getExpression()->isConstant())
338 LiveGVs.insert(DIG);
339
Michael Gottesman823aaff2013-08-23 00:23:24 +0000340 // Make sure we only visit each global variable only once.
341 if (!VisitedSet.insert(DIG).second)
342 continue;
343
Peter Collingbourned4135bb2016-09-13 01:12:59 +0000344 // If a global variable references DIG, the global variable is live.
345 if (LiveGVs.count(DIG))
Michael Gottesman823aaff2013-08-23 00:23:24 +0000346 LiveGlobalVariables.push_back(DIG);
Devang Patel2b434e12010-07-01 19:49:20 +0000347 else
Michael Gottesman823aaff2013-08-23 00:23:24 +0000348 GlobalVariableChange = true;
Devang Patel2b434e12010-07-01 19:49:20 +0000349 }
Michael Gottesman823aaff2013-08-23 00:23:24 +0000350
Keno Fischerbacc64b2017-04-06 19:26:22 +0000351 if (!LiveGlobalVariables.empty())
352 LiveCUs.insert(DIC);
353 else if (!LiveCUs.count(DIC))
354 HasDeadCUs = true;
355
Adrian Prantl75819ae2016-04-15 15:57:41 +0000356 // If we found dead global variables, replace the current global
357 // variable list with our new live global variable list.
Michael Gottesman823aaff2013-08-23 00:23:24 +0000358 if (GlobalVariableChange) {
Duncan P. N. Exon Smith35ef22c2015-04-15 23:19:27 +0000359 DIC->replaceGlobalVariables(MDTuple::get(C, LiveGlobalVariables));
Michael Gottesman823aaff2013-08-23 00:23:24 +0000360 Changed = true;
361 }
362
363 // Reset lists for the next iteration.
Michael Gottesman823aaff2013-08-23 00:23:24 +0000364 LiveGlobalVariables.clear();
Devang Patel2b434e12010-07-01 19:49:20 +0000365 }
366
Keno Fischerbacc64b2017-04-06 19:26:22 +0000367 if (HasDeadCUs) {
368 // Delete the old node and replace it with a new one
369 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu");
370 NMD->clearOperands();
371 if (!LiveCUs.empty()) {
372 for (DICompileUnit *CU : LiveCUs)
373 NMD->addOperand(CU);
374 }
375 Changed = true;
376 }
377
Devang Patel2b434e12010-07-01 19:49:20 +0000378 return Changed;
379}