blob: 7f91b49d82879f2d8681ab3bdfafb6326f8f625e [file] [log] [blame]
Bill Wendling523bea82013-11-08 08:13:15 +00001//===--- DebugInfo.cpp - Debug Information Helper Classes -----------------===//
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// This file implements the helper classes used to build and interpret debug
11// information in LLVM IR form.
12//
13//===----------------------------------------------------------------------===//
14
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000015#include "llvm/IR/DebugInfo.h"
Chandler Carruth442f7842014-03-04 10:07:28 +000016#include "LLVMContextImpl.h"
Bill Wendling523bea82013-11-08 08:13:15 +000017#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/SmallPtrSet.h"
Bill Wendling523bea82013-11-08 08:13:15 +000019#include "llvm/IR/Constants.h"
Adrian Prantlb1416832014-08-01 22:11:58 +000020#include "llvm/IR/DIBuilder.h"
Bill Wendling523bea82013-11-08 08:13:15 +000021#include "llvm/IR/DerivedTypes.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000022#include "llvm/IR/GVMaterializer.h"
Bill Wendling523bea82013-11-08 08:13:15 +000023#include "llvm/IR/Instructions.h"
24#include "llvm/IR/IntrinsicInst.h"
25#include "llvm/IR/Intrinsics.h"
26#include "llvm/IR/Module.h"
Chandler Carruth4220e9c2014-03-04 11:17:44 +000027#include "llvm/IR/ValueHandle.h"
Bill Wendling523bea82013-11-08 08:13:15 +000028#include "llvm/Support/Debug.h"
29#include "llvm/Support/Dwarf.h"
Bill Wendling523bea82013-11-08 08:13:15 +000030#include "llvm/Support/raw_ostream.h"
31using namespace llvm;
32using namespace llvm::dwarf;
33
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000034DISubprogram *llvm::getDISubprogram(const MDNode *Scope) {
35 if (auto *LocalScope = dyn_cast_or_null<DILocalScope>(Scope))
Duncan P. N. Exon Smithdd77af82015-03-31 02:06:28 +000036 return LocalScope->getSubprogram();
37 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +000038}
39
Bill Wendling523bea82013-11-08 08:13:15 +000040//===----------------------------------------------------------------------===//
41// DebugInfoFinder implementations.
42//===----------------------------------------------------------------------===//
43
44void DebugInfoFinder::reset() {
45 CUs.clear();
46 SPs.clear();
47 GVs.clear();
48 TYs.clear();
49 Scopes.clear();
50 NodesSeen.clear();
Bill Wendling523bea82013-11-08 08:13:15 +000051}
52
Bill Wendling523bea82013-11-08 08:13:15 +000053void DebugInfoFinder::processModule(const Module &M) {
Adrian Prantl5992a722016-04-08 22:43:03 +000054 for (auto *CU : M.debug_compile_units()) {
55 addCompileUnit(CU);
56 for (auto *DIG : CU->getGlobalVariables()) {
57 if (addGlobalVariable(DIG)) {
58 processScope(DIG->getScope());
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +000059 processType(DIG->getType().resolve());
Bill Wendling523bea82013-11-08 08:13:15 +000060 }
Adrian Prantl5992a722016-04-08 22:43:03 +000061 }
Adrian Prantl5992a722016-04-08 22:43:03 +000062 for (auto *ET : CU->getEnumTypes())
63 processType(ET);
64 for (auto *RT : CU->getRetainedTypes())
Adrian Prantl75819ae2016-04-15 15:57:41 +000065 if (auto *T = dyn_cast<DIType>(RT))
66 processType(T);
67 else
68 processSubprogram(cast<DISubprogram>(RT));
Adrian Prantl5992a722016-04-08 22:43:03 +000069 for (auto *Import : CU->getImportedEntities()) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +000070 auto *Entity = Import->getEntity().resolve();
Adrian Prantl5992a722016-04-08 22:43:03 +000071 if (auto *T = dyn_cast<DIType>(Entity))
72 processType(T);
73 else if (auto *SP = dyn_cast<DISubprogram>(Entity))
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +000074 processSubprogram(SP);
Adrian Prantl5992a722016-04-08 22:43:03 +000075 else if (auto *NS = dyn_cast<DINamespace>(Entity))
76 processScope(NS->getScope());
77 else if (auto *M = dyn_cast<DIModule>(Entity))
78 processScope(M->getScope());
Bill Wendling523bea82013-11-08 08:13:15 +000079 }
80 }
Adrian Prantl75819ae2016-04-15 15:57:41 +000081 for (auto &F : M.functions())
82 if (auto *SP = cast_or_null<DISubprogram>(F.getSubprogram()))
83 processSubprogram(SP);
Bill Wendling523bea82013-11-08 08:13:15 +000084}
85
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000086void DebugInfoFinder::processLocation(const Module &M, const DILocation *Loc) {
Bill Wendling523bea82013-11-08 08:13:15 +000087 if (!Loc)
88 return;
Duncan P. N. Exon Smithb7e221b2015-04-14 01:35:55 +000089 processScope(Loc->getScope());
90 processLocation(M, Loc->getInlinedAt());
Bill Wendling523bea82013-11-08 08:13:15 +000091}
92
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000093void DebugInfoFinder::processType(DIType *DT) {
Bill Wendling523bea82013-11-08 08:13:15 +000094 if (!addType(DT))
95 return;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +000096 processScope(DT->getScope().resolve());
Duncan P. N. Exon Smith260fa8a2015-07-24 20:56:10 +000097 if (auto *ST = dyn_cast<DISubroutineType>(DT)) {
98 for (DITypeRef Ref : ST->getTypeArray())
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +000099 processType(Ref.resolve());
Duncan P. N. Exon Smith260fa8a2015-07-24 20:56:10 +0000100 return;
101 }
102 if (auto *DCT = dyn_cast<DICompositeType>(DT)) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000103 processType(DCT->getBaseType().resolve());
Anders Waldenborg1433fd42015-04-14 09:18:17 +0000104 for (Metadata *D : DCT->getElements()) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000105 if (auto *T = dyn_cast<DIType>(D))
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000106 processType(T);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000107 else if (auto *SP = dyn_cast<DISubprogram>(D))
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000108 processSubprogram(SP);
Bill Wendling523bea82013-11-08 08:13:15 +0000109 }
Duncan P. N. Exon Smith260fa8a2015-07-24 20:56:10 +0000110 return;
111 }
112 if (auto *DDT = dyn_cast<DIDerivedType>(DT)) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000113 processType(DDT->getBaseType().resolve());
Bill Wendling523bea82013-11-08 08:13:15 +0000114 }
115}
116
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000117void DebugInfoFinder::processScope(DIScope *Scope) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000118 if (!Scope)
119 return;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000120 if (auto *Ty = dyn_cast<DIType>(Scope)) {
Bill Wendling523bea82013-11-08 08:13:15 +0000121 processType(Ty);
122 return;
123 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000124 if (auto *CU = dyn_cast<DICompileUnit>(Scope)) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000125 addCompileUnit(CU);
Bill Wendling523bea82013-11-08 08:13:15 +0000126 return;
127 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000128 if (auto *SP = dyn_cast<DISubprogram>(Scope)) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000129 processSubprogram(SP);
Bill Wendling523bea82013-11-08 08:13:15 +0000130 return;
131 }
132 if (!addScope(Scope))
133 return;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000134 if (auto *LB = dyn_cast<DILexicalBlockBase>(Scope)) {
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000135 processScope(LB->getScope());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000136 } else if (auto *NS = dyn_cast<DINamespace>(Scope)) {
Duncan P. N. Exon Smith20caafb2015-04-14 03:01:27 +0000137 processScope(NS->getScope());
Adrian Prantlab1243f2015-06-29 23:03:47 +0000138 } else if (auto *M = dyn_cast<DIModule>(Scope)) {
139 processScope(M->getScope());
Bill Wendling523bea82013-11-08 08:13:15 +0000140 }
141}
142
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000143void DebugInfoFinder::processSubprogram(DISubprogram *SP) {
Bill Wendling523bea82013-11-08 08:13:15 +0000144 if (!addSubprogram(SP))
145 return;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000146 processScope(SP->getScope().resolve());
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000147 processType(SP->getType());
148 for (auto *Element : SP->getTemplateParams()) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000149 if (auto *TType = dyn_cast<DITemplateTypeParameter>(Element)) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000150 processType(TType->getType().resolve());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000151 } else if (auto *TVal = dyn_cast<DITemplateValueParameter>(Element)) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000152 processType(TVal->getType().resolve());
Bill Wendling523bea82013-11-08 08:13:15 +0000153 }
154 }
155}
156
Manman Ren2085ccc2013-11-17 18:42:37 +0000157void DebugInfoFinder::processDeclare(const Module &M,
158 const DbgDeclareInst *DDI) {
Duncan P. N. Exon Smithed557b52015-04-17 23:20:10 +0000159 auto *N = dyn_cast<MDNode>(DDI->getVariable());
Bill Wendling523bea82013-11-08 08:13:15 +0000160 if (!N)
161 return;
162
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000163 auto *DV = dyn_cast<DILocalVariable>(N);
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000164 if (!DV)
Bill Wendling523bea82013-11-08 08:13:15 +0000165 return;
166
David Blaikie70573dc2014-11-19 07:49:26 +0000167 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000168 return;
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000169 processScope(DV->getScope());
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000170 processType(DV->getType().resolve());
Bill Wendling523bea82013-11-08 08:13:15 +0000171}
172
Manman Ren2085ccc2013-11-17 18:42:37 +0000173void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
Duncan P. N. Exon Smithed557b52015-04-17 23:20:10 +0000174 auto *N = dyn_cast<MDNode>(DVI->getVariable());
Bill Wendling523bea82013-11-08 08:13:15 +0000175 if (!N)
176 return;
177
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000178 auto *DV = dyn_cast<DILocalVariable>(N);
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000179 if (!DV)
Bill Wendling523bea82013-11-08 08:13:15 +0000180 return;
181
David Blaikie70573dc2014-11-19 07:49:26 +0000182 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000183 return;
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000184 processScope(DV->getScope());
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000185 processType(DV->getType().resolve());
Bill Wendling523bea82013-11-08 08:13:15 +0000186}
187
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000188bool DebugInfoFinder::addType(DIType *DT) {
Bill Wendling523bea82013-11-08 08:13:15 +0000189 if (!DT)
190 return false;
191
David Blaikie70573dc2014-11-19 07:49:26 +0000192 if (!NodesSeen.insert(DT).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000193 return false;
194
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000195 TYs.push_back(const_cast<DIType *>(DT));
Bill Wendling523bea82013-11-08 08:13:15 +0000196 return true;
197}
198
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000199bool DebugInfoFinder::addCompileUnit(DICompileUnit *CU) {
Bill Wendling523bea82013-11-08 08:13:15 +0000200 if (!CU)
201 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000202 if (!NodesSeen.insert(CU).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000203 return false;
204
205 CUs.push_back(CU);
206 return true;
207}
208
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000209bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable *DIG) {
Bill Wendling523bea82013-11-08 08:13:15 +0000210 if (!DIG)
211 return false;
212
David Blaikie70573dc2014-11-19 07:49:26 +0000213 if (!NodesSeen.insert(DIG).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000214 return false;
215
216 GVs.push_back(DIG);
217 return true;
218}
219
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000220bool DebugInfoFinder::addSubprogram(DISubprogram *SP) {
Bill Wendling523bea82013-11-08 08:13:15 +0000221 if (!SP)
222 return false;
223
David Blaikie70573dc2014-11-19 07:49:26 +0000224 if (!NodesSeen.insert(SP).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000225 return false;
226
227 SPs.push_back(SP);
228 return true;
229}
230
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000231bool DebugInfoFinder::addScope(DIScope *Scope) {
Bill Wendling523bea82013-11-08 08:13:15 +0000232 if (!Scope)
233 return false;
234 // FIXME: Ocaml binding generates a scope with no content, we treat it
235 // as null for now.
236 if (Scope->getNumOperands() == 0)
237 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000238 if (!NodesSeen.insert(Scope).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000239 return false;
240 Scopes.push_back(Scope);
241 return true;
242}
243
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000244bool llvm::stripDebugInfo(Function &F) {
245 bool Changed = false;
Peter Collingbourned4bff302015-11-05 22:03:56 +0000246 if (F.getSubprogram()) {
247 Changed = true;
248 F.setSubprogram(nullptr);
249 }
Mehdi Amini581f0e12016-05-07 04:10:52 +0000250
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000251 for (BasicBlock &BB : F) {
Mehdi Amini581f0e12016-05-07 04:10:52 +0000252 for (auto II = BB.begin(), End = BB.end(); II != End;) {
253 Instruction &I = *II++; // We may delete the instruction, increment now.
Mehdi Aminidb8dd552016-05-14 04:58:35 +0000254 if (isa<DbgInfoIntrinsic>(&I)) {
255 I.eraseFromParent();
Mehdi Amini581f0e12016-05-07 04:10:52 +0000256 Changed = true;
Mehdi Aminibbedb142016-05-07 05:07:47 +0000257 continue;
Mehdi Amini581f0e12016-05-07 04:10:52 +0000258 }
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000259 if (I.getDebugLoc()) {
260 Changed = true;
261 I.setDebugLoc(DebugLoc());
262 }
263 }
264 }
265 return Changed;
266}
267
Manman Rencb14bbc2013-11-22 22:06:31 +0000268bool llvm::StripDebugInfo(Module &M) {
Manman Rencb14bbc2013-11-22 22:06:31 +0000269 bool Changed = false;
270
Manman Rencb14bbc2013-11-22 22:06:31 +0000271 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
272 NME = M.named_metadata_end(); NMI != NME;) {
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +0000273 NamedMDNode *NMD = &*NMI;
Manman Rencb14bbc2013-11-22 22:06:31 +0000274 ++NMI;
Davide Italiano84bd58e2016-10-17 20:05:35 +0000275
276 // We're stripping debug info, and without them, coverage information
277 // doesn't quite make sense.
278 if (NMD->getName().startswith("llvm.dbg.") ||
279 NMD->getName() == "llvm.gcov") {
Manman Rencb14bbc2013-11-22 22:06:31 +0000280 NMD->eraseFromParent();
281 Changed = true;
282 }
283 }
284
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000285 for (Function &F : M)
286 Changed |= stripDebugInfo(F);
287
Adrian Prantl3bfe1092016-10-10 17:53:33 +0000288 for (auto &GV : M.globals()) {
289 SmallVector<MDNode *, 1> MDs;
290 GV.getMetadata(LLVMContext::MD_dbg, MDs);
291 if (!MDs.empty()) {
292 GV.eraseMetadata(LLVMContext::MD_dbg);
293 Changed = true;
294 }
295 }
296
Rafael Espindola468b8682015-04-01 14:44:59 +0000297 if (GVMaterializer *Materializer = M.getMaterializer())
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000298 Materializer->setStripDebugInfo();
Manman Rencb14bbc2013-11-22 22:06:31 +0000299
300 return Changed;
301}
Manman Ren8b4306c2013-12-02 21:29:56 +0000302
Michael Ilsemane5428042016-10-25 18:44:13 +0000303namespace {
304
305/// Helper class to downgrade -g metadata to -gline-tables-only metadata.
306class DebugTypeInfoRemoval {
307 DenseMap<Metadata *, Metadata *> Replacements;
308
309public:
310 /// The (void)() type.
311 MDNode *EmptySubroutineType;
312
313private:
314 /// Remember what linkage name we originally had before stripping. If we end
315 /// up making two subprograms identical who originally had different linkage
316 /// names, then we need to make one of them distinct, to avoid them getting
317 /// uniqued. Maps the new node to the old linkage name.
318 DenseMap<DISubprogram *, StringRef> NewToLinkageName;
319
320 // TODO: Remember the distinct subprogram we created for a given linkage name,
321 // so that we can continue to unique whenever possible. Map <newly created
322 // node, old linkage name> to the first (possibly distinct) mdsubprogram
323 // created for that combination. This is not strictly needed for correctness,
324 // but can cut down on the number of MDNodes and let us diff cleanly with the
325 // output of -gline-tables-only.
326
327public:
328 DebugTypeInfoRemoval(LLVMContext &C)
329 : EmptySubroutineType(DISubroutineType::get(C, DINode::FlagZero, 0,
330 MDNode::get(C, {}))) {}
331
332 Metadata *map(Metadata *M) {
333 if (!M)
334 return nullptr;
335 auto Replacement = Replacements.find(M);
336 if (Replacement != Replacements.end())
337 return Replacement->second;
338
339 return M;
340 }
341 MDNode *mapNode(Metadata *N) { return dyn_cast_or_null<MDNode>(map(N)); }
342
343 /// Recursively remap N and all its referenced children. Does a DF post-order
344 /// traversal, so as to remap bottoms up.
345 void traverseAndRemap(MDNode *N) { traverse(N); }
346
347private:
348 // Create a new DISubprogram, to replace the one given.
349 DISubprogram *getReplacementSubprogram(DISubprogram *MDS) {
350 auto *FileAndScope = cast_or_null<DIFile>(map(MDS->getFile()));
351 StringRef LinkageName = MDS->getName().empty() ? MDS->getLinkageName() : "";
352 DISubprogram *Declaration = nullptr;
353 auto *Type = cast_or_null<DISubroutineType>(map(MDS->getType()));
354 DITypeRef ContainingType(map(MDS->getContainingType()));
355 auto *Unit = cast_or_null<DICompileUnit>(map(MDS->getUnit()));
356 auto Variables = nullptr;
357 auto TemplateParams = nullptr;
358
359 // Make a distinct DISubprogram, for situations that warrent it.
360 auto distinctMDSubprogram = [&]() {
361 return DISubprogram::getDistinct(
362 MDS->getContext(), FileAndScope, MDS->getName(), LinkageName,
363 FileAndScope, MDS->getLine(), Type, MDS->isLocalToUnit(),
364 MDS->isDefinition(), MDS->getScopeLine(), ContainingType,
365 MDS->getVirtuality(), MDS->getVirtualIndex(),
366 MDS->getThisAdjustment(), MDS->getFlags(), MDS->isOptimized(), Unit,
367 TemplateParams, Declaration, Variables);
368 };
369
370 if (MDS->isDistinct())
371 return distinctMDSubprogram();
372
373 auto *NewMDS = DISubprogram::get(
374 MDS->getContext(), FileAndScope, MDS->getName(), LinkageName,
375 FileAndScope, MDS->getLine(), Type, MDS->isLocalToUnit(),
376 MDS->isDefinition(), MDS->getScopeLine(), ContainingType,
377 MDS->getVirtuality(), MDS->getVirtualIndex(), MDS->getThisAdjustment(),
378 MDS->getFlags(), MDS->isOptimized(), Unit, TemplateParams, Declaration,
379 Variables);
380
381 StringRef OldLinkageName = MDS->getLinkageName();
382
383 // See if we need to make a distinct one.
384 auto OrigLinkage = NewToLinkageName.find(NewMDS);
385 if (OrigLinkage != NewToLinkageName.end()) {
386 if (OrigLinkage->second == OldLinkageName)
387 // We're good.
388 return NewMDS;
389
390 // Otherwise, need to make a distinct one.
391 // TODO: Query the map to see if we already have one.
392 return distinctMDSubprogram();
393 }
394
395 NewToLinkageName.insert({NewMDS, MDS->getLinkageName()});
396 return NewMDS;
397 }
398
399 /// Create a new compile unit, to replace the one given
400 DICompileUnit *getReplacementCU(DICompileUnit *CU) {
401 // Drop skeleton CUs.
402 if (CU->getDWOId())
403 return nullptr;
404
405 auto *File = cast_or_null<DIFile>(map(CU->getFile()));
406 MDTuple *EnumTypes = nullptr;
407 MDTuple *RetainedTypes = nullptr;
408 MDTuple *GlobalVariables = nullptr;
409 MDTuple *ImportedEntities = nullptr;
410 return DICompileUnit::getDistinct(
411 CU->getContext(), CU->getSourceLanguage(), File, CU->getProducer(),
412 CU->isOptimized(), CU->getFlags(), CU->getRuntimeVersion(),
413 CU->getSplitDebugFilename(), DICompileUnit::LineTablesOnly, EnumTypes,
414 RetainedTypes, GlobalVariables, ImportedEntities, CU->getMacros(),
415 CU->getDWOId(), CU->getSplitDebugInlining());
416 }
417
418 DILocation *getReplacementMDLocation(DILocation *MLD) {
419 auto *Scope = map(MLD->getScope());
420 auto *InlinedAt = map(MLD->getInlinedAt());
421 if (MLD->isDistinct())
422 return DILocation::getDistinct(MLD->getContext(), MLD->getLine(),
423 MLD->getColumn(), Scope, InlinedAt);
424 return DILocation::get(MLD->getContext(), MLD->getLine(), MLD->getColumn(),
425 Scope, InlinedAt);
426 }
427
428 /// Create a new generic MDNode, to replace the one given
429 MDNode *getReplacementMDNode(MDNode *N) {
430 SmallVector<Metadata *, 8> Ops;
431 Ops.reserve(N->getNumOperands());
432 for (auto &I : N->operands())
433 if (I)
434 Ops.push_back(map(I));
435 auto *Ret = MDNode::get(N->getContext(), Ops);
436 return Ret;
437 }
438
439 /// Attempt to re-map N to a newly created node.
440 void remap(MDNode *N) {
441 if (Replacements.count(N))
442 return;
443
444 auto doRemap = [&](MDNode *N) -> MDNode * {
445 if (!N)
446 return nullptr;
447 if (auto *MDSub = dyn_cast<DISubprogram>(N)) {
448 remap(MDSub->getUnit());
449 return getReplacementSubprogram(MDSub);
450 }
451 if (isa<DISubroutineType>(N))
452 return EmptySubroutineType;
453 if (auto *CU = dyn_cast<DICompileUnit>(N))
454 return getReplacementCU(CU);
455 if (isa<DIFile>(N))
456 return N;
457 if (auto *MDLB = dyn_cast<DILexicalBlockBase>(N))
458 // Remap to our referenced scope (recursively).
459 return mapNode(MDLB->getScope());
460 if (auto *MLD = dyn_cast<DILocation>(N))
461 return getReplacementMDLocation(MLD);
462
463 // Otherwise, if we see these, just drop them now. Not strictly necessary,
464 // but this speeds things up a little.
465 if (isa<DINode>(N))
466 return nullptr;
467
468 return getReplacementMDNode(N);
469 };
470 Replacements[N] = doRemap(N);
471 }
472
473 /// Do the remapping traversal.
474 void traverse(MDNode *);
475};
476
477} // Anonymous namespace.
478
479void DebugTypeInfoRemoval::traverse(MDNode *N) {
480 if (!N || Replacements.count(N))
481 return;
482
483 // To avoid cycles, as well as for efficiency sake, we will sometimes prune
484 // parts of the graph.
485 auto prune = [](MDNode *Parent, MDNode *Child) {
486 if (auto *MDS = dyn_cast<DISubprogram>(Parent))
487 return Child == MDS->getVariables().get();
488 return false;
489 };
490
491 SmallVector<MDNode *, 16> ToVisit;
492 DenseSet<MDNode *> Opened;
493
494 // Visit each node starting at N in post order, and map them.
495 ToVisit.push_back(N);
496 while (!ToVisit.empty()) {
497 auto *N = ToVisit.back();
498 if (!Opened.insert(N).second) {
499 // Close it.
500 remap(N);
501 ToVisit.pop_back();
502 continue;
503 }
504 for (auto &I : N->operands())
505 if (auto *MDN = dyn_cast_or_null<MDNode>(I))
506 if (!Opened.count(MDN) && !Replacements.count(MDN) && !prune(N, MDN) &&
507 !isa<DICompileUnit>(MDN))
508 ToVisit.push_back(MDN);
509 }
510}
511
512bool llvm::stripNonLineTableDebugInfo(Module &M) {
513 bool Changed = false;
514
515 // First off, delete the debug intrinsics.
516 auto RemoveUses = [&](StringRef Name) {
517 if (auto *DbgVal = M.getFunction(Name)) {
518 while (!DbgVal->use_empty())
519 cast<Instruction>(DbgVal->user_back())->eraseFromParent();
520 DbgVal->eraseFromParent();
521 Changed = true;
522 }
523 };
524 RemoveUses("llvm.dbg.declare");
525 RemoveUses("llvm.dbg.value");
526
527 // Delete non-CU debug info named metadata nodes.
528 for (auto NMI = M.named_metadata_begin(), NME = M.named_metadata_end();
529 NMI != NME;) {
530 NamedMDNode *NMD = &*NMI;
531 ++NMI;
532 // Specifically keep dbg.cu around.
533 if (NMD->getName() == "llvm.dbg.cu")
534 continue;
535 }
536
537 // Drop all dbg attachments from global variables.
538 for (auto &GV : M.globals())
539 GV.eraseMetadata(LLVMContext::MD_dbg);
540
541 DebugTypeInfoRemoval Mapper(M.getContext());
542 auto remap = [&](llvm::MDNode *Node) -> llvm::MDNode * {
543 if (!Node)
544 return nullptr;
545 Mapper.traverseAndRemap(Node);
546 auto *NewNode = Mapper.mapNode(Node);
547 Changed |= Node != NewNode;
548 Node = NewNode;
549 return NewNode;
550 };
551
552 // Rewrite the DebugLocs to be equivalent to what
553 // -gline-tables-only would have created.
554 for (auto &F : M) {
555 if (auto *SP = F.getSubprogram()) {
556 Mapper.traverseAndRemap(SP);
557 auto *NewSP = cast<DISubprogram>(Mapper.mapNode(SP));
558 Changed |= SP != NewSP;
559 F.setSubprogram(NewSP);
560 }
561 for (auto &BB : F) {
562 for (auto &I : BB) {
563 if (I.getDebugLoc() == DebugLoc())
564 continue;
565
566 // Make a replacement.
567 auto &DL = I.getDebugLoc();
568 auto *Scope = DL.getScope();
569 MDNode *InlinedAt = DL.getInlinedAt();
570 Scope = remap(Scope);
571 InlinedAt = remap(InlinedAt);
572 I.setDebugLoc(
573 DebugLoc::get(DL.getLine(), DL.getCol(), Scope, InlinedAt));
574 }
575 }
576 }
577
578 // Create a new llvm.dbg.cu, which is equivalent to the one
579 // -gline-tables-only would have created.
580 for (auto &NMD : M.getNamedMDList()) {
581 SmallVector<MDNode *, 8> Ops;
582 for (MDNode *Op : NMD.operands())
583 Ops.push_back(remap(Op));
584
585 if (!Changed)
586 continue;
587
588 NMD.clearOperands();
589 for (auto *Op : Ops)
590 if (Op)
591 NMD.addOperand(Op);
592 }
593 return Changed;
594}
595
Manman Renbd4daf82013-12-03 00:12:14 +0000596unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
David Majnemere7a9cdb2015-02-16 06:04:53 +0000597 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000598 M.getModuleFlag("Debug Info Version")))
599 return Val->getZExtValue();
600 return 0;
Manman Ren8b4306c2013-12-02 21:29:56 +0000601}