blob: 5e14723c3afc1bb64afa9f5718e064c14b600fdc [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);
Adrian Prantlbceaaa92016-12-20 02:09:43 +000056 for (auto DIG : CU->getGlobalVariables()) {
57 if (!addGlobalVariable(DIG))
58 continue;
59 auto *GV = DIG->getVariable();
60 processScope(GV->getScope());
61 processType(GV->getType().resolve());
Adrian Prantl5992a722016-04-08 22:43:03 +000062 }
Adrian Prantl5992a722016-04-08 22:43:03 +000063 for (auto *ET : CU->getEnumTypes())
64 processType(ET);
65 for (auto *RT : CU->getRetainedTypes())
Adrian Prantl75819ae2016-04-15 15:57:41 +000066 if (auto *T = dyn_cast<DIType>(RT))
67 processType(T);
68 else
69 processSubprogram(cast<DISubprogram>(RT));
Adrian Prantl5992a722016-04-08 22:43:03 +000070 for (auto *Import : CU->getImportedEntities()) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +000071 auto *Entity = Import->getEntity().resolve();
Adrian Prantl5992a722016-04-08 22:43:03 +000072 if (auto *T = dyn_cast<DIType>(Entity))
73 processType(T);
74 else if (auto *SP = dyn_cast<DISubprogram>(Entity))
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +000075 processSubprogram(SP);
Adrian Prantl5992a722016-04-08 22:43:03 +000076 else if (auto *NS = dyn_cast<DINamespace>(Entity))
77 processScope(NS->getScope());
78 else if (auto *M = dyn_cast<DIModule>(Entity))
79 processScope(M->getScope());
Bill Wendling523bea82013-11-08 08:13:15 +000080 }
81 }
Adrian Prantl75819ae2016-04-15 15:57:41 +000082 for (auto &F : M.functions())
83 if (auto *SP = cast_or_null<DISubprogram>(F.getSubprogram()))
84 processSubprogram(SP);
Bill Wendling523bea82013-11-08 08:13:15 +000085}
86
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000087void DebugInfoFinder::processLocation(const Module &M, const DILocation *Loc) {
Bill Wendling523bea82013-11-08 08:13:15 +000088 if (!Loc)
89 return;
Duncan P. N. Exon Smithb7e221b2015-04-14 01:35:55 +000090 processScope(Loc->getScope());
91 processLocation(M, Loc->getInlinedAt());
Bill Wendling523bea82013-11-08 08:13:15 +000092}
93
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000094void DebugInfoFinder::processType(DIType *DT) {
Bill Wendling523bea82013-11-08 08:13:15 +000095 if (!addType(DT))
96 return;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +000097 processScope(DT->getScope().resolve());
Duncan P. N. Exon Smith260fa8a2015-07-24 20:56:10 +000098 if (auto *ST = dyn_cast<DISubroutineType>(DT)) {
99 for (DITypeRef Ref : ST->getTypeArray())
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000100 processType(Ref.resolve());
Duncan P. N. Exon Smith260fa8a2015-07-24 20:56:10 +0000101 return;
102 }
103 if (auto *DCT = dyn_cast<DICompositeType>(DT)) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000104 processType(DCT->getBaseType().resolve());
Anders Waldenborg1433fd42015-04-14 09:18:17 +0000105 for (Metadata *D : DCT->getElements()) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000106 if (auto *T = dyn_cast<DIType>(D))
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000107 processType(T);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000108 else if (auto *SP = dyn_cast<DISubprogram>(D))
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000109 processSubprogram(SP);
Bill Wendling523bea82013-11-08 08:13:15 +0000110 }
Duncan P. N. Exon Smith260fa8a2015-07-24 20:56:10 +0000111 return;
112 }
113 if (auto *DDT = dyn_cast<DIDerivedType>(DT)) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000114 processType(DDT->getBaseType().resolve());
Bill Wendling523bea82013-11-08 08:13:15 +0000115 }
116}
117
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000118void DebugInfoFinder::processScope(DIScope *Scope) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000119 if (!Scope)
120 return;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000121 if (auto *Ty = dyn_cast<DIType>(Scope)) {
Bill Wendling523bea82013-11-08 08:13:15 +0000122 processType(Ty);
123 return;
124 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000125 if (auto *CU = dyn_cast<DICompileUnit>(Scope)) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000126 addCompileUnit(CU);
Bill Wendling523bea82013-11-08 08:13:15 +0000127 return;
128 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000129 if (auto *SP = dyn_cast<DISubprogram>(Scope)) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000130 processSubprogram(SP);
Bill Wendling523bea82013-11-08 08:13:15 +0000131 return;
132 }
133 if (!addScope(Scope))
134 return;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000135 if (auto *LB = dyn_cast<DILexicalBlockBase>(Scope)) {
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000136 processScope(LB->getScope());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000137 } else if (auto *NS = dyn_cast<DINamespace>(Scope)) {
Duncan P. N. Exon Smith20caafb2015-04-14 03:01:27 +0000138 processScope(NS->getScope());
Adrian Prantlab1243f2015-06-29 23:03:47 +0000139 } else if (auto *M = dyn_cast<DIModule>(Scope)) {
140 processScope(M->getScope());
Bill Wendling523bea82013-11-08 08:13:15 +0000141 }
142}
143
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000144void DebugInfoFinder::processSubprogram(DISubprogram *SP) {
Bill Wendling523bea82013-11-08 08:13:15 +0000145 if (!addSubprogram(SP))
146 return;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000147 processScope(SP->getScope().resolve());
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000148 processType(SP->getType());
149 for (auto *Element : SP->getTemplateParams()) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000150 if (auto *TType = dyn_cast<DITemplateTypeParameter>(Element)) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000151 processType(TType->getType().resolve());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000152 } else if (auto *TVal = dyn_cast<DITemplateValueParameter>(Element)) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000153 processType(TVal->getType().resolve());
Bill Wendling523bea82013-11-08 08:13:15 +0000154 }
155 }
156}
157
Manman Ren2085ccc2013-11-17 18:42:37 +0000158void DebugInfoFinder::processDeclare(const Module &M,
159 const DbgDeclareInst *DDI) {
Duncan P. N. Exon Smithed557b52015-04-17 23:20:10 +0000160 auto *N = dyn_cast<MDNode>(DDI->getVariable());
Bill Wendling523bea82013-11-08 08:13:15 +0000161 if (!N)
162 return;
163
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000164 auto *DV = dyn_cast<DILocalVariable>(N);
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000165 if (!DV)
Bill Wendling523bea82013-11-08 08:13:15 +0000166 return;
167
David Blaikie70573dc2014-11-19 07:49:26 +0000168 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000169 return;
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000170 processScope(DV->getScope());
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000171 processType(DV->getType().resolve());
Bill Wendling523bea82013-11-08 08:13:15 +0000172}
173
Manman Ren2085ccc2013-11-17 18:42:37 +0000174void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
Duncan P. N. Exon Smithed557b52015-04-17 23:20:10 +0000175 auto *N = dyn_cast<MDNode>(DVI->getVariable());
Bill Wendling523bea82013-11-08 08:13:15 +0000176 if (!N)
177 return;
178
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000179 auto *DV = dyn_cast<DILocalVariable>(N);
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000180 if (!DV)
Bill Wendling523bea82013-11-08 08:13:15 +0000181 return;
182
David Blaikie70573dc2014-11-19 07:49:26 +0000183 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000184 return;
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000185 processScope(DV->getScope());
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000186 processType(DV->getType().resolve());
Bill Wendling523bea82013-11-08 08:13:15 +0000187}
188
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000189bool DebugInfoFinder::addType(DIType *DT) {
Bill Wendling523bea82013-11-08 08:13:15 +0000190 if (!DT)
191 return false;
192
David Blaikie70573dc2014-11-19 07:49:26 +0000193 if (!NodesSeen.insert(DT).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000194 return false;
195
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000196 TYs.push_back(const_cast<DIType *>(DT));
Bill Wendling523bea82013-11-08 08:13:15 +0000197 return true;
198}
199
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000200bool DebugInfoFinder::addCompileUnit(DICompileUnit *CU) {
Bill Wendling523bea82013-11-08 08:13:15 +0000201 if (!CU)
202 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000203 if (!NodesSeen.insert(CU).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000204 return false;
205
206 CUs.push_back(CU);
207 return true;
208}
209
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000210bool DebugInfoFinder::addGlobalVariable(DIGlobalVariableExpression *DIG) {
David Blaikie70573dc2014-11-19 07:49:26 +0000211 if (!NodesSeen.insert(DIG).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000212 return false;
213
214 GVs.push_back(DIG);
215 return true;
216}
217
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000218bool DebugInfoFinder::addSubprogram(DISubprogram *SP) {
Bill Wendling523bea82013-11-08 08:13:15 +0000219 if (!SP)
220 return false;
221
David Blaikie70573dc2014-11-19 07:49:26 +0000222 if (!NodesSeen.insert(SP).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000223 return false;
224
225 SPs.push_back(SP);
226 return true;
227}
228
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000229bool DebugInfoFinder::addScope(DIScope *Scope) {
Bill Wendling523bea82013-11-08 08:13:15 +0000230 if (!Scope)
231 return false;
232 // FIXME: Ocaml binding generates a scope with no content, we treat it
233 // as null for now.
234 if (Scope->getNumOperands() == 0)
235 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000236 if (!NodesSeen.insert(Scope).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000237 return false;
238 Scopes.push_back(Scope);
239 return true;
240}
241
Daniel Sandersb96a9452017-01-28 11:22:05 +0000242static llvm::MDNode *stripDebugLocFromLoopID(llvm::MDNode *N) {
243 assert(N->op_begin() != N->op_end() && "Missing self reference?");
244 auto DebugLocOp =
245 std::find_if(N->op_begin() + 1, N->op_end(), [](const MDOperand &Op) {
246 return isa<DILocation>(Op.get());
247 });
248
249 // No debug location, we do not have to rewrite this MDNode.
250 if (DebugLocOp == N->op_end())
251 return N;
252
253 // There is only the debug location without any actual loop metadata, hence we
254 // can remove the metadata.
255 if (N->getNumOperands() == 2)
256 return nullptr;
257
258 SmallVector<Metadata *, 4> Args;
259 // Reserve operand 0 for loop id self reference.
260 auto TempNode = MDNode::getTemporary(N->getContext(), None);
261 Args.push_back(TempNode.get());
262 Args.append(N->op_begin() + 1, DebugLocOp);
263 Args.append(DebugLocOp + 1, N->op_end());
264
265 // Set the first operand to itself.
266 MDNode *LoopID = MDNode::get(N->getContext(), Args);
267 LoopID->replaceOperandWith(0, LoopID);
268 return LoopID;
269}
270
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000271bool llvm::stripDebugInfo(Function &F) {
272 bool Changed = false;
Peter Collingbourned4bff302015-11-05 22:03:56 +0000273 if (F.getSubprogram()) {
274 Changed = true;
275 F.setSubprogram(nullptr);
276 }
Mehdi Amini581f0e12016-05-07 04:10:52 +0000277
Daniel Sandersb96a9452017-01-28 11:22:05 +0000278 llvm::DenseMap<llvm::MDNode*, llvm::MDNode*> LoopIDsMap;
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000279 for (BasicBlock &BB : F) {
Mehdi Amini581f0e12016-05-07 04:10:52 +0000280 for (auto II = BB.begin(), End = BB.end(); II != End;) {
281 Instruction &I = *II++; // We may delete the instruction, increment now.
Mehdi Aminidb8dd552016-05-14 04:58:35 +0000282 if (isa<DbgInfoIntrinsic>(&I)) {
283 I.eraseFromParent();
Mehdi Amini581f0e12016-05-07 04:10:52 +0000284 Changed = true;
Mehdi Aminibbedb142016-05-07 05:07:47 +0000285 continue;
Mehdi Amini581f0e12016-05-07 04:10:52 +0000286 }
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000287 if (I.getDebugLoc()) {
288 Changed = true;
289 I.setDebugLoc(DebugLoc());
290 }
291 }
Daniel Sandersb96a9452017-01-28 11:22:05 +0000292
293 auto *TermInst = BB.getTerminator();
294 if (auto *LoopID = TermInst->getMetadata(LLVMContext::MD_loop)) {
295 auto *NewLoopID = LoopIDsMap.lookup(LoopID);
296 if (!NewLoopID)
297 NewLoopID = LoopIDsMap[LoopID] = stripDebugLocFromLoopID(LoopID);
298 if (NewLoopID != LoopID)
299 TermInst->setMetadata(LLVMContext::MD_loop, NewLoopID);
300 }
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000301 }
302 return Changed;
303}
304
Manman Rencb14bbc2013-11-22 22:06:31 +0000305bool llvm::StripDebugInfo(Module &M) {
Manman Rencb14bbc2013-11-22 22:06:31 +0000306 bool Changed = false;
307
Manman Rencb14bbc2013-11-22 22:06:31 +0000308 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
309 NME = M.named_metadata_end(); NMI != NME;) {
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +0000310 NamedMDNode *NMD = &*NMI;
Manman Rencb14bbc2013-11-22 22:06:31 +0000311 ++NMI;
Davide Italiano84bd58e2016-10-17 20:05:35 +0000312
313 // We're stripping debug info, and without them, coverage information
314 // doesn't quite make sense.
315 if (NMD->getName().startswith("llvm.dbg.") ||
316 NMD->getName() == "llvm.gcov") {
Manman Rencb14bbc2013-11-22 22:06:31 +0000317 NMD->eraseFromParent();
318 Changed = true;
319 }
320 }
321
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000322 for (Function &F : M)
323 Changed |= stripDebugInfo(F);
324
Adrian Prantl3bfe1092016-10-10 17:53:33 +0000325 for (auto &GV : M.globals()) {
326 SmallVector<MDNode *, 1> MDs;
327 GV.getMetadata(LLVMContext::MD_dbg, MDs);
328 if (!MDs.empty()) {
329 GV.eraseMetadata(LLVMContext::MD_dbg);
330 Changed = true;
331 }
332 }
333
Rafael Espindola468b8682015-04-01 14:44:59 +0000334 if (GVMaterializer *Materializer = M.getMaterializer())
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000335 Materializer->setStripDebugInfo();
Manman Rencb14bbc2013-11-22 22:06:31 +0000336
337 return Changed;
338}
Manman Ren8b4306c2013-12-02 21:29:56 +0000339
Michael Ilsemane5428042016-10-25 18:44:13 +0000340namespace {
341
342/// Helper class to downgrade -g metadata to -gline-tables-only metadata.
343class DebugTypeInfoRemoval {
344 DenseMap<Metadata *, Metadata *> Replacements;
345
346public:
347 /// The (void)() type.
348 MDNode *EmptySubroutineType;
349
350private:
351 /// Remember what linkage name we originally had before stripping. If we end
352 /// up making two subprograms identical who originally had different linkage
353 /// names, then we need to make one of them distinct, to avoid them getting
354 /// uniqued. Maps the new node to the old linkage name.
355 DenseMap<DISubprogram *, StringRef> NewToLinkageName;
356
357 // TODO: Remember the distinct subprogram we created for a given linkage name,
358 // so that we can continue to unique whenever possible. Map <newly created
359 // node, old linkage name> to the first (possibly distinct) mdsubprogram
360 // created for that combination. This is not strictly needed for correctness,
361 // but can cut down on the number of MDNodes and let us diff cleanly with the
362 // output of -gline-tables-only.
363
364public:
365 DebugTypeInfoRemoval(LLVMContext &C)
366 : EmptySubroutineType(DISubroutineType::get(C, DINode::FlagZero, 0,
367 MDNode::get(C, {}))) {}
368
369 Metadata *map(Metadata *M) {
370 if (!M)
371 return nullptr;
372 auto Replacement = Replacements.find(M);
373 if (Replacement != Replacements.end())
374 return Replacement->second;
375
376 return M;
377 }
378 MDNode *mapNode(Metadata *N) { return dyn_cast_or_null<MDNode>(map(N)); }
379
380 /// Recursively remap N and all its referenced children. Does a DF post-order
381 /// traversal, so as to remap bottoms up.
382 void traverseAndRemap(MDNode *N) { traverse(N); }
383
384private:
385 // Create a new DISubprogram, to replace the one given.
386 DISubprogram *getReplacementSubprogram(DISubprogram *MDS) {
387 auto *FileAndScope = cast_or_null<DIFile>(map(MDS->getFile()));
388 StringRef LinkageName = MDS->getName().empty() ? MDS->getLinkageName() : "";
389 DISubprogram *Declaration = nullptr;
390 auto *Type = cast_or_null<DISubroutineType>(map(MDS->getType()));
391 DITypeRef ContainingType(map(MDS->getContainingType()));
392 auto *Unit = cast_or_null<DICompileUnit>(map(MDS->getUnit()));
393 auto Variables = nullptr;
394 auto TemplateParams = nullptr;
395
396 // Make a distinct DISubprogram, for situations that warrent it.
397 auto distinctMDSubprogram = [&]() {
398 return DISubprogram::getDistinct(
399 MDS->getContext(), FileAndScope, MDS->getName(), LinkageName,
400 FileAndScope, MDS->getLine(), Type, MDS->isLocalToUnit(),
401 MDS->isDefinition(), MDS->getScopeLine(), ContainingType,
402 MDS->getVirtuality(), MDS->getVirtualIndex(),
403 MDS->getThisAdjustment(), MDS->getFlags(), MDS->isOptimized(), Unit,
404 TemplateParams, Declaration, Variables);
405 };
406
407 if (MDS->isDistinct())
408 return distinctMDSubprogram();
409
410 auto *NewMDS = DISubprogram::get(
411 MDS->getContext(), FileAndScope, MDS->getName(), LinkageName,
412 FileAndScope, MDS->getLine(), Type, MDS->isLocalToUnit(),
413 MDS->isDefinition(), MDS->getScopeLine(), ContainingType,
414 MDS->getVirtuality(), MDS->getVirtualIndex(), MDS->getThisAdjustment(),
415 MDS->getFlags(), MDS->isOptimized(), Unit, TemplateParams, Declaration,
416 Variables);
417
418 StringRef OldLinkageName = MDS->getLinkageName();
419
420 // See if we need to make a distinct one.
421 auto OrigLinkage = NewToLinkageName.find(NewMDS);
422 if (OrigLinkage != NewToLinkageName.end()) {
423 if (OrigLinkage->second == OldLinkageName)
424 // We're good.
425 return NewMDS;
426
427 // Otherwise, need to make a distinct one.
428 // TODO: Query the map to see if we already have one.
429 return distinctMDSubprogram();
430 }
431
432 NewToLinkageName.insert({NewMDS, MDS->getLinkageName()});
433 return NewMDS;
434 }
435
436 /// Create a new compile unit, to replace the one given
437 DICompileUnit *getReplacementCU(DICompileUnit *CU) {
438 // Drop skeleton CUs.
439 if (CU->getDWOId())
440 return nullptr;
441
442 auto *File = cast_or_null<DIFile>(map(CU->getFile()));
443 MDTuple *EnumTypes = nullptr;
444 MDTuple *RetainedTypes = nullptr;
445 MDTuple *GlobalVariables = nullptr;
446 MDTuple *ImportedEntities = nullptr;
447 return DICompileUnit::getDistinct(
448 CU->getContext(), CU->getSourceLanguage(), File, CU->getProducer(),
449 CU->isOptimized(), CU->getFlags(), CU->getRuntimeVersion(),
450 CU->getSplitDebugFilename(), DICompileUnit::LineTablesOnly, EnumTypes,
451 RetainedTypes, GlobalVariables, ImportedEntities, CU->getMacros(),
Dehao Chen0944a8c2017-02-01 22:45:09 +0000452 CU->getDWOId(), CU->getSplitDebugInlining(),
453 CU->getDebugInfoForProfiling());
Michael Ilsemane5428042016-10-25 18:44:13 +0000454 }
455
456 DILocation *getReplacementMDLocation(DILocation *MLD) {
457 auto *Scope = map(MLD->getScope());
458 auto *InlinedAt = map(MLD->getInlinedAt());
459 if (MLD->isDistinct())
460 return DILocation::getDistinct(MLD->getContext(), MLD->getLine(),
461 MLD->getColumn(), Scope, InlinedAt);
462 return DILocation::get(MLD->getContext(), MLD->getLine(), MLD->getColumn(),
463 Scope, InlinedAt);
464 }
465
466 /// Create a new generic MDNode, to replace the one given
467 MDNode *getReplacementMDNode(MDNode *N) {
468 SmallVector<Metadata *, 8> Ops;
469 Ops.reserve(N->getNumOperands());
470 for (auto &I : N->operands())
471 if (I)
472 Ops.push_back(map(I));
473 auto *Ret = MDNode::get(N->getContext(), Ops);
474 return Ret;
475 }
476
477 /// Attempt to re-map N to a newly created node.
478 void remap(MDNode *N) {
479 if (Replacements.count(N))
480 return;
481
482 auto doRemap = [&](MDNode *N) -> MDNode * {
483 if (!N)
484 return nullptr;
485 if (auto *MDSub = dyn_cast<DISubprogram>(N)) {
486 remap(MDSub->getUnit());
487 return getReplacementSubprogram(MDSub);
488 }
489 if (isa<DISubroutineType>(N))
490 return EmptySubroutineType;
491 if (auto *CU = dyn_cast<DICompileUnit>(N))
492 return getReplacementCU(CU);
493 if (isa<DIFile>(N))
494 return N;
495 if (auto *MDLB = dyn_cast<DILexicalBlockBase>(N))
496 // Remap to our referenced scope (recursively).
497 return mapNode(MDLB->getScope());
498 if (auto *MLD = dyn_cast<DILocation>(N))
499 return getReplacementMDLocation(MLD);
500
501 // Otherwise, if we see these, just drop them now. Not strictly necessary,
502 // but this speeds things up a little.
503 if (isa<DINode>(N))
504 return nullptr;
505
506 return getReplacementMDNode(N);
507 };
508 Replacements[N] = doRemap(N);
509 }
510
511 /// Do the remapping traversal.
512 void traverse(MDNode *);
513};
514
515} // Anonymous namespace.
516
517void DebugTypeInfoRemoval::traverse(MDNode *N) {
518 if (!N || Replacements.count(N))
519 return;
520
521 // To avoid cycles, as well as for efficiency sake, we will sometimes prune
522 // parts of the graph.
523 auto prune = [](MDNode *Parent, MDNode *Child) {
524 if (auto *MDS = dyn_cast<DISubprogram>(Parent))
525 return Child == MDS->getVariables().get();
526 return false;
527 };
528
529 SmallVector<MDNode *, 16> ToVisit;
530 DenseSet<MDNode *> Opened;
531
532 // Visit each node starting at N in post order, and map them.
533 ToVisit.push_back(N);
534 while (!ToVisit.empty()) {
535 auto *N = ToVisit.back();
536 if (!Opened.insert(N).second) {
537 // Close it.
538 remap(N);
539 ToVisit.pop_back();
540 continue;
541 }
542 for (auto &I : N->operands())
543 if (auto *MDN = dyn_cast_or_null<MDNode>(I))
544 if (!Opened.count(MDN) && !Replacements.count(MDN) && !prune(N, MDN) &&
545 !isa<DICompileUnit>(MDN))
546 ToVisit.push_back(MDN);
547 }
548}
549
550bool llvm::stripNonLineTableDebugInfo(Module &M) {
551 bool Changed = false;
552
553 // First off, delete the debug intrinsics.
554 auto RemoveUses = [&](StringRef Name) {
555 if (auto *DbgVal = M.getFunction(Name)) {
556 while (!DbgVal->use_empty())
557 cast<Instruction>(DbgVal->user_back())->eraseFromParent();
558 DbgVal->eraseFromParent();
559 Changed = true;
560 }
561 };
562 RemoveUses("llvm.dbg.declare");
563 RemoveUses("llvm.dbg.value");
564
565 // Delete non-CU debug info named metadata nodes.
566 for (auto NMI = M.named_metadata_begin(), NME = M.named_metadata_end();
567 NMI != NME;) {
568 NamedMDNode *NMD = &*NMI;
569 ++NMI;
570 // Specifically keep dbg.cu around.
571 if (NMD->getName() == "llvm.dbg.cu")
572 continue;
573 }
574
575 // Drop all dbg attachments from global variables.
576 for (auto &GV : M.globals())
577 GV.eraseMetadata(LLVMContext::MD_dbg);
578
579 DebugTypeInfoRemoval Mapper(M.getContext());
580 auto remap = [&](llvm::MDNode *Node) -> llvm::MDNode * {
581 if (!Node)
582 return nullptr;
583 Mapper.traverseAndRemap(Node);
584 auto *NewNode = Mapper.mapNode(Node);
585 Changed |= Node != NewNode;
586 Node = NewNode;
587 return NewNode;
588 };
589
590 // Rewrite the DebugLocs to be equivalent to what
591 // -gline-tables-only would have created.
592 for (auto &F : M) {
593 if (auto *SP = F.getSubprogram()) {
594 Mapper.traverseAndRemap(SP);
595 auto *NewSP = cast<DISubprogram>(Mapper.mapNode(SP));
596 Changed |= SP != NewSP;
597 F.setSubprogram(NewSP);
598 }
599 for (auto &BB : F) {
600 for (auto &I : BB) {
601 if (I.getDebugLoc() == DebugLoc())
602 continue;
603
604 // Make a replacement.
605 auto &DL = I.getDebugLoc();
606 auto *Scope = DL.getScope();
607 MDNode *InlinedAt = DL.getInlinedAt();
608 Scope = remap(Scope);
609 InlinedAt = remap(InlinedAt);
610 I.setDebugLoc(
611 DebugLoc::get(DL.getLine(), DL.getCol(), Scope, InlinedAt));
612 }
613 }
614 }
615
616 // Create a new llvm.dbg.cu, which is equivalent to the one
617 // -gline-tables-only would have created.
618 for (auto &NMD : M.getNamedMDList()) {
619 SmallVector<MDNode *, 8> Ops;
620 for (MDNode *Op : NMD.operands())
621 Ops.push_back(remap(Op));
622
623 if (!Changed)
624 continue;
625
626 NMD.clearOperands();
627 for (auto *Op : Ops)
628 if (Op)
629 NMD.addOperand(Op);
630 }
631 return Changed;
632}
633
Manman Renbd4daf82013-12-03 00:12:14 +0000634unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
David Majnemere7a9cdb2015-02-16 06:04:53 +0000635 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000636 M.getModuleFlag("Debug Info Version")))
637 return Val->getZExtValue();
638 return 0;
Manman Ren8b4306c2013-12-02 21:29:56 +0000639}