blob: 1dc6c5bdd51f490b0d0032f85a177a4cd187b05e [file] [log] [blame]
Eugene Zelenkof53a7b42017-05-05 22:30:37 +00001//===- DebugInfo.cpp - Debug Information Helper Classes -------------------===//
Bill Wendling523bea82013-11-08 08:13:15 +00002//
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 Carruth6bda14b2017-06-06 11:49:48 +000015#include "llvm/IR/DebugInfo.h"
Eugene Zelenkof53a7b42017-05-05 22:30:37 +000016#include "llvm/ADT/DenseMap.h"
17#include "llvm/ADT/DenseSet.h"
18#include "llvm/ADT/None.h"
Bill Wendling523bea82013-11-08 08:13:15 +000019#include "llvm/ADT/SmallPtrSet.h"
Eugene Zelenkof53a7b42017-05-05 22:30:37 +000020#include "llvm/ADT/SmallVector.h"
21#include "llvm/ADT/StringRef.h"
22#include "llvm/IR/BasicBlock.h"
Bill Wendling523bea82013-11-08 08:13:15 +000023#include "llvm/IR/Constants.h"
Eugene Zelenkof53a7b42017-05-05 22:30:37 +000024#include "llvm/IR/DebugInfoMetadata.h"
25#include "llvm/IR/DebugLoc.h"
26#include "llvm/IR/Function.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000027#include "llvm/IR/GVMaterializer.h"
Eugene Zelenkof53a7b42017-05-05 22:30:37 +000028#include "llvm/IR/Instruction.h"
Bill Wendling523bea82013-11-08 08:13:15 +000029#include "llvm/IR/IntrinsicInst.h"
Eugene Zelenkof53a7b42017-05-05 22:30:37 +000030#include "llvm/IR/LLVMContext.h"
31#include "llvm/IR/Metadata.h"
Bill Wendling523bea82013-11-08 08:13:15 +000032#include "llvm/IR/Module.h"
Eugene Zelenkof53a7b42017-05-05 22:30:37 +000033#include "llvm/Support/Casting.h"
34#include <algorithm>
35#include <cassert>
36#include <utility>
37
Bill Wendling523bea82013-11-08 08:13:15 +000038using namespace llvm;
39using namespace llvm::dwarf;
40
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000041DISubprogram *llvm::getDISubprogram(const MDNode *Scope) {
42 if (auto *LocalScope = dyn_cast_or_null<DILocalScope>(Scope))
Duncan P. N. Exon Smithdd77af82015-03-31 02:06:28 +000043 return LocalScope->getSubprogram();
44 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +000045}
46
Bill Wendling523bea82013-11-08 08:13:15 +000047//===----------------------------------------------------------------------===//
48// DebugInfoFinder implementations.
49//===----------------------------------------------------------------------===//
50
51void DebugInfoFinder::reset() {
52 CUs.clear();
53 SPs.clear();
54 GVs.clear();
55 TYs.clear();
56 Scopes.clear();
57 NodesSeen.clear();
Bill Wendling523bea82013-11-08 08:13:15 +000058}
59
Bill Wendling523bea82013-11-08 08:13:15 +000060void DebugInfoFinder::processModule(const Module &M) {
Adrian Prantl5992a722016-04-08 22:43:03 +000061 for (auto *CU : M.debug_compile_units()) {
62 addCompileUnit(CU);
Adrian Prantlbceaaa92016-12-20 02:09:43 +000063 for (auto DIG : CU->getGlobalVariables()) {
64 if (!addGlobalVariable(DIG))
65 continue;
66 auto *GV = DIG->getVariable();
67 processScope(GV->getScope());
68 processType(GV->getType().resolve());
Adrian Prantl5992a722016-04-08 22:43:03 +000069 }
Adrian Prantl5992a722016-04-08 22:43:03 +000070 for (auto *ET : CU->getEnumTypes())
71 processType(ET);
72 for (auto *RT : CU->getRetainedTypes())
Adrian Prantl75819ae2016-04-15 15:57:41 +000073 if (auto *T = dyn_cast<DIType>(RT))
74 processType(T);
75 else
76 processSubprogram(cast<DISubprogram>(RT));
Adrian Prantl5992a722016-04-08 22:43:03 +000077 for (auto *Import : CU->getImportedEntities()) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +000078 auto *Entity = Import->getEntity().resolve();
Adrian Prantl5992a722016-04-08 22:43:03 +000079 if (auto *T = dyn_cast<DIType>(Entity))
80 processType(T);
81 else if (auto *SP = dyn_cast<DISubprogram>(Entity))
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +000082 processSubprogram(SP);
Adrian Prantl5992a722016-04-08 22:43:03 +000083 else if (auto *NS = dyn_cast<DINamespace>(Entity))
84 processScope(NS->getScope());
85 else if (auto *M = dyn_cast<DIModule>(Entity))
86 processScope(M->getScope());
Bill Wendling523bea82013-11-08 08:13:15 +000087 }
88 }
Keno Fischer30779772017-04-11 13:32:11 +000089 for (auto &F : M.functions()) {
Adrian Prantl75819ae2016-04-15 15:57:41 +000090 if (auto *SP = cast_or_null<DISubprogram>(F.getSubprogram()))
91 processSubprogram(SP);
Keno Fischer30779772017-04-11 13:32:11 +000092 // There could be subprograms from inlined functions referenced from
93 // instructions only. Walk the function to find them.
94 for (const BasicBlock &BB : F) {
95 for (const Instruction &I : BB) {
96 if (!I.getDebugLoc())
97 continue;
98 processLocation(M, I.getDebugLoc().get());
99 }
100 }
101 }
Bill Wendling523bea82013-11-08 08:13:15 +0000102}
103
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000104void DebugInfoFinder::processLocation(const Module &M, const DILocation *Loc) {
Bill Wendling523bea82013-11-08 08:13:15 +0000105 if (!Loc)
106 return;
Duncan P. N. Exon Smithb7e221b2015-04-14 01:35:55 +0000107 processScope(Loc->getScope());
108 processLocation(M, Loc->getInlinedAt());
Bill Wendling523bea82013-11-08 08:13:15 +0000109}
110
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000111void DebugInfoFinder::processType(DIType *DT) {
Bill Wendling523bea82013-11-08 08:13:15 +0000112 if (!addType(DT))
113 return;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000114 processScope(DT->getScope().resolve());
Duncan P. N. Exon Smith260fa8a2015-07-24 20:56:10 +0000115 if (auto *ST = dyn_cast<DISubroutineType>(DT)) {
116 for (DITypeRef Ref : ST->getTypeArray())
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000117 processType(Ref.resolve());
Duncan P. N. Exon Smith260fa8a2015-07-24 20:56:10 +0000118 return;
119 }
120 if (auto *DCT = dyn_cast<DICompositeType>(DT)) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000121 processType(DCT->getBaseType().resolve());
Anders Waldenborg1433fd42015-04-14 09:18:17 +0000122 for (Metadata *D : DCT->getElements()) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000123 if (auto *T = dyn_cast<DIType>(D))
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000124 processType(T);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000125 else if (auto *SP = dyn_cast<DISubprogram>(D))
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000126 processSubprogram(SP);
Bill Wendling523bea82013-11-08 08:13:15 +0000127 }
Duncan P. N. Exon Smith260fa8a2015-07-24 20:56:10 +0000128 return;
129 }
130 if (auto *DDT = dyn_cast<DIDerivedType>(DT)) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000131 processType(DDT->getBaseType().resolve());
Bill Wendling523bea82013-11-08 08:13:15 +0000132 }
133}
134
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000135void DebugInfoFinder::processScope(DIScope *Scope) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000136 if (!Scope)
137 return;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000138 if (auto *Ty = dyn_cast<DIType>(Scope)) {
Bill Wendling523bea82013-11-08 08:13:15 +0000139 processType(Ty);
140 return;
141 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000142 if (auto *CU = dyn_cast<DICompileUnit>(Scope)) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000143 addCompileUnit(CU);
Bill Wendling523bea82013-11-08 08:13:15 +0000144 return;
145 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000146 if (auto *SP = dyn_cast<DISubprogram>(Scope)) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000147 processSubprogram(SP);
Bill Wendling523bea82013-11-08 08:13:15 +0000148 return;
149 }
150 if (!addScope(Scope))
151 return;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000152 if (auto *LB = dyn_cast<DILexicalBlockBase>(Scope)) {
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000153 processScope(LB->getScope());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000154 } else if (auto *NS = dyn_cast<DINamespace>(Scope)) {
Duncan P. N. Exon Smith20caafb2015-04-14 03:01:27 +0000155 processScope(NS->getScope());
Adrian Prantlab1243f2015-06-29 23:03:47 +0000156 } else if (auto *M = dyn_cast<DIModule>(Scope)) {
157 processScope(M->getScope());
Bill Wendling523bea82013-11-08 08:13:15 +0000158 }
159}
160
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000161void DebugInfoFinder::processSubprogram(DISubprogram *SP) {
Bill Wendling523bea82013-11-08 08:13:15 +0000162 if (!addSubprogram(SP))
163 return;
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000164 processScope(SP->getScope().resolve());
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000165 processType(SP->getType());
166 for (auto *Element : SP->getTemplateParams()) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000167 if (auto *TType = dyn_cast<DITemplateTypeParameter>(Element)) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000168 processType(TType->getType().resolve());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000169 } else if (auto *TVal = dyn_cast<DITemplateValueParameter>(Element)) {
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000170 processType(TVal->getType().resolve());
Bill Wendling523bea82013-11-08 08:13:15 +0000171 }
172 }
173}
174
Manman Ren2085ccc2013-11-17 18:42:37 +0000175void DebugInfoFinder::processDeclare(const Module &M,
176 const DbgDeclareInst *DDI) {
Duncan P. N. Exon Smithed557b52015-04-17 23:20:10 +0000177 auto *N = dyn_cast<MDNode>(DDI->getVariable());
Bill Wendling523bea82013-11-08 08:13:15 +0000178 if (!N)
179 return;
180
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000181 auto *DV = dyn_cast<DILocalVariable>(N);
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000182 if (!DV)
Bill Wendling523bea82013-11-08 08:13:15 +0000183 return;
184
David Blaikie70573dc2014-11-19 07:49:26 +0000185 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000186 return;
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000187 processScope(DV->getScope());
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000188 processType(DV->getType().resolve());
Bill Wendling523bea82013-11-08 08:13:15 +0000189}
190
Manman Ren2085ccc2013-11-17 18:42:37 +0000191void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
Duncan P. N. Exon Smithed557b52015-04-17 23:20:10 +0000192 auto *N = dyn_cast<MDNode>(DVI->getVariable());
Bill Wendling523bea82013-11-08 08:13:15 +0000193 if (!N)
194 return;
195
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000196 auto *DV = dyn_cast<DILocalVariable>(N);
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000197 if (!DV)
Bill Wendling523bea82013-11-08 08:13:15 +0000198 return;
199
David Blaikie70573dc2014-11-19 07:49:26 +0000200 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000201 return;
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000202 processScope(DV->getScope());
Duncan P. N. Exon Smitha59d3e52016-04-23 21:08:00 +0000203 processType(DV->getType().resolve());
Bill Wendling523bea82013-11-08 08:13:15 +0000204}
205
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000206bool DebugInfoFinder::addType(DIType *DT) {
Bill Wendling523bea82013-11-08 08:13:15 +0000207 if (!DT)
208 return false;
209
David Blaikie70573dc2014-11-19 07:49:26 +0000210 if (!NodesSeen.insert(DT).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000211 return false;
212
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000213 TYs.push_back(const_cast<DIType *>(DT));
Bill Wendling523bea82013-11-08 08:13:15 +0000214 return true;
215}
216
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000217bool DebugInfoFinder::addCompileUnit(DICompileUnit *CU) {
Bill Wendling523bea82013-11-08 08:13:15 +0000218 if (!CU)
219 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000220 if (!NodesSeen.insert(CU).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000221 return false;
222
223 CUs.push_back(CU);
224 return true;
225}
226
Adrian Prantlbceaaa92016-12-20 02:09:43 +0000227bool DebugInfoFinder::addGlobalVariable(DIGlobalVariableExpression *DIG) {
David Blaikie70573dc2014-11-19 07:49:26 +0000228 if (!NodesSeen.insert(DIG).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000229 return false;
230
231 GVs.push_back(DIG);
232 return true;
233}
234
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000235bool DebugInfoFinder::addSubprogram(DISubprogram *SP) {
Bill Wendling523bea82013-11-08 08:13:15 +0000236 if (!SP)
237 return false;
238
David Blaikie70573dc2014-11-19 07:49:26 +0000239 if (!NodesSeen.insert(SP).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000240 return false;
241
242 SPs.push_back(SP);
243 return true;
244}
245
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000246bool DebugInfoFinder::addScope(DIScope *Scope) {
Bill Wendling523bea82013-11-08 08:13:15 +0000247 if (!Scope)
248 return false;
249 // FIXME: Ocaml binding generates a scope with no content, we treat it
250 // as null for now.
251 if (Scope->getNumOperands() == 0)
252 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000253 if (!NodesSeen.insert(Scope).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000254 return false;
255 Scopes.push_back(Scope);
256 return true;
257}
258
Eugene Zelenkof53a7b42017-05-05 22:30:37 +0000259static MDNode *stripDebugLocFromLoopID(MDNode *N) {
Daniel Sandersb96a9452017-01-28 11:22:05 +0000260 assert(N->op_begin() != N->op_end() && "Missing self reference?");
Daniel Sandersb96a9452017-01-28 11:22:05 +0000261
Teresa Johnson9b4b8c82017-03-19 13:54:57 +0000262 // if there is no debug location, we do not have to rewrite this MDNode.
263 if (std::none_of(N->op_begin() + 1, N->op_end(), [](const MDOperand &Op) {
264 return isa<DILocation>(Op.get());
265 }))
Daniel Sandersb96a9452017-01-28 11:22:05 +0000266 return N;
267
Teresa Johnson9b4b8c82017-03-19 13:54:57 +0000268 // If there is only the debug location without any actual loop metadata, we
Daniel Sandersb96a9452017-01-28 11:22:05 +0000269 // can remove the metadata.
Teresa Johnson9b4b8c82017-03-19 13:54:57 +0000270 if (std::none_of(N->op_begin() + 1, N->op_end(), [](const MDOperand &Op) {
271 return !isa<DILocation>(Op.get());
272 }))
Daniel Sandersb96a9452017-01-28 11:22:05 +0000273 return nullptr;
274
275 SmallVector<Metadata *, 4> Args;
276 // Reserve operand 0 for loop id self reference.
277 auto TempNode = MDNode::getTemporary(N->getContext(), None);
278 Args.push_back(TempNode.get());
Teresa Johnson9b4b8c82017-03-19 13:54:57 +0000279 // Add all non-debug location operands back.
280 for (auto Op = N->op_begin() + 1; Op != N->op_end(); Op++) {
281 if (!isa<DILocation>(*Op))
282 Args.push_back(*Op);
283 }
Daniel Sandersb96a9452017-01-28 11:22:05 +0000284
285 // Set the first operand to itself.
286 MDNode *LoopID = MDNode::get(N->getContext(), Args);
287 LoopID->replaceOperandWith(0, LoopID);
288 return LoopID;
289}
290
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000291bool llvm::stripDebugInfo(Function &F) {
292 bool Changed = false;
Peter Collingbourned4bff302015-11-05 22:03:56 +0000293 if (F.getSubprogram()) {
294 Changed = true;
295 F.setSubprogram(nullptr);
296 }
Mehdi Amini581f0e12016-05-07 04:10:52 +0000297
Eugene Zelenkof53a7b42017-05-05 22:30:37 +0000298 DenseMap<MDNode*, MDNode*> LoopIDsMap;
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000299 for (BasicBlock &BB : F) {
Mehdi Amini581f0e12016-05-07 04:10:52 +0000300 for (auto II = BB.begin(), End = BB.end(); II != End;) {
301 Instruction &I = *II++; // We may delete the instruction, increment now.
Mehdi Aminidb8dd552016-05-14 04:58:35 +0000302 if (isa<DbgInfoIntrinsic>(&I)) {
303 I.eraseFromParent();
Mehdi Amini581f0e12016-05-07 04:10:52 +0000304 Changed = true;
Mehdi Aminibbedb142016-05-07 05:07:47 +0000305 continue;
Mehdi Amini581f0e12016-05-07 04:10:52 +0000306 }
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000307 if (I.getDebugLoc()) {
308 Changed = true;
309 I.setDebugLoc(DebugLoc());
310 }
311 }
Daniel Sandersb96a9452017-01-28 11:22:05 +0000312
313 auto *TermInst = BB.getTerminator();
Justin Bognerb29bebe2017-08-18 21:38:03 +0000314 if (!TermInst)
315 // This is invalid IR, but we may not have run the verifier yet
316 continue;
Daniel Sandersb96a9452017-01-28 11:22:05 +0000317 if (auto *LoopID = TermInst->getMetadata(LLVMContext::MD_loop)) {
318 auto *NewLoopID = LoopIDsMap.lookup(LoopID);
319 if (!NewLoopID)
320 NewLoopID = LoopIDsMap[LoopID] = stripDebugLocFromLoopID(LoopID);
321 if (NewLoopID != LoopID)
322 TermInst->setMetadata(LLVMContext::MD_loop, NewLoopID);
323 }
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000324 }
325 return Changed;
326}
327
Manman Rencb14bbc2013-11-22 22:06:31 +0000328bool llvm::StripDebugInfo(Module &M) {
Manman Rencb14bbc2013-11-22 22:06:31 +0000329 bool Changed = false;
330
Manman Rencb14bbc2013-11-22 22:06:31 +0000331 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
332 NME = M.named_metadata_end(); NMI != NME;) {
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +0000333 NamedMDNode *NMD = &*NMI;
Manman Rencb14bbc2013-11-22 22:06:31 +0000334 ++NMI;
Davide Italiano84bd58e2016-10-17 20:05:35 +0000335
336 // We're stripping debug info, and without them, coverage information
337 // doesn't quite make sense.
338 if (NMD->getName().startswith("llvm.dbg.") ||
339 NMD->getName() == "llvm.gcov") {
Manman Rencb14bbc2013-11-22 22:06:31 +0000340 NMD->eraseFromParent();
341 Changed = true;
342 }
343 }
344
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000345 for (Function &F : M)
346 Changed |= stripDebugInfo(F);
347
Adrian Prantl3bfe1092016-10-10 17:53:33 +0000348 for (auto &GV : M.globals()) {
349 SmallVector<MDNode *, 1> MDs;
350 GV.getMetadata(LLVMContext::MD_dbg, MDs);
351 if (!MDs.empty()) {
352 GV.eraseMetadata(LLVMContext::MD_dbg);
353 Changed = true;
354 }
355 }
356
Rafael Espindola468b8682015-04-01 14:44:59 +0000357 if (GVMaterializer *Materializer = M.getMaterializer())
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000358 Materializer->setStripDebugInfo();
Manman Rencb14bbc2013-11-22 22:06:31 +0000359
360 return Changed;
361}
Manman Ren8b4306c2013-12-02 21:29:56 +0000362
Michael Ilsemane5428042016-10-25 18:44:13 +0000363namespace {
364
365/// Helper class to downgrade -g metadata to -gline-tables-only metadata.
366class DebugTypeInfoRemoval {
367 DenseMap<Metadata *, Metadata *> Replacements;
368
369public:
370 /// The (void)() type.
371 MDNode *EmptySubroutineType;
372
373private:
374 /// Remember what linkage name we originally had before stripping. If we end
375 /// up making two subprograms identical who originally had different linkage
376 /// names, then we need to make one of them distinct, to avoid them getting
377 /// uniqued. Maps the new node to the old linkage name.
378 DenseMap<DISubprogram *, StringRef> NewToLinkageName;
379
380 // TODO: Remember the distinct subprogram we created for a given linkage name,
381 // so that we can continue to unique whenever possible. Map <newly created
382 // node, old linkage name> to the first (possibly distinct) mdsubprogram
383 // created for that combination. This is not strictly needed for correctness,
384 // but can cut down on the number of MDNodes and let us diff cleanly with the
385 // output of -gline-tables-only.
386
387public:
388 DebugTypeInfoRemoval(LLVMContext &C)
389 : EmptySubroutineType(DISubroutineType::get(C, DINode::FlagZero, 0,
390 MDNode::get(C, {}))) {}
391
392 Metadata *map(Metadata *M) {
393 if (!M)
394 return nullptr;
395 auto Replacement = Replacements.find(M);
396 if (Replacement != Replacements.end())
397 return Replacement->second;
398
399 return M;
400 }
401 MDNode *mapNode(Metadata *N) { return dyn_cast_or_null<MDNode>(map(N)); }
402
403 /// Recursively remap N and all its referenced children. Does a DF post-order
404 /// traversal, so as to remap bottoms up.
405 void traverseAndRemap(MDNode *N) { traverse(N); }
406
407private:
408 // Create a new DISubprogram, to replace the one given.
409 DISubprogram *getReplacementSubprogram(DISubprogram *MDS) {
410 auto *FileAndScope = cast_or_null<DIFile>(map(MDS->getFile()));
411 StringRef LinkageName = MDS->getName().empty() ? MDS->getLinkageName() : "";
412 DISubprogram *Declaration = nullptr;
413 auto *Type = cast_or_null<DISubroutineType>(map(MDS->getType()));
414 DITypeRef ContainingType(map(MDS->getContainingType()));
415 auto *Unit = cast_or_null<DICompileUnit>(map(MDS->getUnit()));
416 auto Variables = nullptr;
417 auto TemplateParams = nullptr;
418
419 // Make a distinct DISubprogram, for situations that warrent it.
420 auto distinctMDSubprogram = [&]() {
421 return DISubprogram::getDistinct(
422 MDS->getContext(), FileAndScope, MDS->getName(), LinkageName,
423 FileAndScope, MDS->getLine(), Type, MDS->isLocalToUnit(),
424 MDS->isDefinition(), MDS->getScopeLine(), ContainingType,
425 MDS->getVirtuality(), MDS->getVirtualIndex(),
426 MDS->getThisAdjustment(), MDS->getFlags(), MDS->isOptimized(), Unit,
427 TemplateParams, Declaration, Variables);
428 };
429
430 if (MDS->isDistinct())
431 return distinctMDSubprogram();
432
433 auto *NewMDS = DISubprogram::get(
434 MDS->getContext(), FileAndScope, MDS->getName(), LinkageName,
435 FileAndScope, MDS->getLine(), Type, MDS->isLocalToUnit(),
436 MDS->isDefinition(), MDS->getScopeLine(), ContainingType,
437 MDS->getVirtuality(), MDS->getVirtualIndex(), MDS->getThisAdjustment(),
438 MDS->getFlags(), MDS->isOptimized(), Unit, TemplateParams, Declaration,
439 Variables);
440
441 StringRef OldLinkageName = MDS->getLinkageName();
442
443 // See if we need to make a distinct one.
444 auto OrigLinkage = NewToLinkageName.find(NewMDS);
445 if (OrigLinkage != NewToLinkageName.end()) {
446 if (OrigLinkage->second == OldLinkageName)
447 // We're good.
448 return NewMDS;
449
450 // Otherwise, need to make a distinct one.
451 // TODO: Query the map to see if we already have one.
452 return distinctMDSubprogram();
453 }
454
455 NewToLinkageName.insert({NewMDS, MDS->getLinkageName()});
456 return NewMDS;
457 }
458
459 /// Create a new compile unit, to replace the one given
460 DICompileUnit *getReplacementCU(DICompileUnit *CU) {
461 // Drop skeleton CUs.
462 if (CU->getDWOId())
463 return nullptr;
464
465 auto *File = cast_or_null<DIFile>(map(CU->getFile()));
466 MDTuple *EnumTypes = nullptr;
467 MDTuple *RetainedTypes = nullptr;
468 MDTuple *GlobalVariables = nullptr;
469 MDTuple *ImportedEntities = nullptr;
470 return DICompileUnit::getDistinct(
471 CU->getContext(), CU->getSourceLanguage(), File, CU->getProducer(),
472 CU->isOptimized(), CU->getFlags(), CU->getRuntimeVersion(),
473 CU->getSplitDebugFilename(), DICompileUnit::LineTablesOnly, EnumTypes,
474 RetainedTypes, GlobalVariables, ImportedEntities, CU->getMacros(),
Dehao Chen0944a8c2017-02-01 22:45:09 +0000475 CU->getDWOId(), CU->getSplitDebugInlining(),
Peter Collingbourneb52e2362017-09-12 21:50:41 +0000476 CU->getDebugInfoForProfiling(), CU->getGnuPubnames());
Michael Ilsemane5428042016-10-25 18:44:13 +0000477 }
478
479 DILocation *getReplacementMDLocation(DILocation *MLD) {
480 auto *Scope = map(MLD->getScope());
481 auto *InlinedAt = map(MLD->getInlinedAt());
482 if (MLD->isDistinct())
483 return DILocation::getDistinct(MLD->getContext(), MLD->getLine(),
484 MLD->getColumn(), Scope, InlinedAt);
485 return DILocation::get(MLD->getContext(), MLD->getLine(), MLD->getColumn(),
486 Scope, InlinedAt);
487 }
488
489 /// Create a new generic MDNode, to replace the one given
490 MDNode *getReplacementMDNode(MDNode *N) {
491 SmallVector<Metadata *, 8> Ops;
492 Ops.reserve(N->getNumOperands());
493 for (auto &I : N->operands())
494 if (I)
495 Ops.push_back(map(I));
496 auto *Ret = MDNode::get(N->getContext(), Ops);
497 return Ret;
498 }
499
500 /// Attempt to re-map N to a newly created node.
501 void remap(MDNode *N) {
502 if (Replacements.count(N))
503 return;
504
505 auto doRemap = [&](MDNode *N) -> MDNode * {
506 if (!N)
507 return nullptr;
508 if (auto *MDSub = dyn_cast<DISubprogram>(N)) {
509 remap(MDSub->getUnit());
510 return getReplacementSubprogram(MDSub);
511 }
512 if (isa<DISubroutineType>(N))
513 return EmptySubroutineType;
514 if (auto *CU = dyn_cast<DICompileUnit>(N))
515 return getReplacementCU(CU);
516 if (isa<DIFile>(N))
517 return N;
518 if (auto *MDLB = dyn_cast<DILexicalBlockBase>(N))
519 // Remap to our referenced scope (recursively).
520 return mapNode(MDLB->getScope());
521 if (auto *MLD = dyn_cast<DILocation>(N))
522 return getReplacementMDLocation(MLD);
523
524 // Otherwise, if we see these, just drop them now. Not strictly necessary,
525 // but this speeds things up a little.
526 if (isa<DINode>(N))
527 return nullptr;
528
529 return getReplacementMDNode(N);
530 };
531 Replacements[N] = doRemap(N);
532 }
533
534 /// Do the remapping traversal.
535 void traverse(MDNode *);
536};
537
Eugene Zelenkof53a7b42017-05-05 22:30:37 +0000538} // end anonymous namespace
Michael Ilsemane5428042016-10-25 18:44:13 +0000539
540void DebugTypeInfoRemoval::traverse(MDNode *N) {
541 if (!N || Replacements.count(N))
542 return;
543
544 // To avoid cycles, as well as for efficiency sake, we will sometimes prune
545 // parts of the graph.
546 auto prune = [](MDNode *Parent, MDNode *Child) {
547 if (auto *MDS = dyn_cast<DISubprogram>(Parent))
548 return Child == MDS->getVariables().get();
549 return false;
550 };
551
552 SmallVector<MDNode *, 16> ToVisit;
553 DenseSet<MDNode *> Opened;
554
555 // Visit each node starting at N in post order, and map them.
556 ToVisit.push_back(N);
557 while (!ToVisit.empty()) {
558 auto *N = ToVisit.back();
559 if (!Opened.insert(N).second) {
560 // Close it.
561 remap(N);
562 ToVisit.pop_back();
563 continue;
564 }
565 for (auto &I : N->operands())
566 if (auto *MDN = dyn_cast_or_null<MDNode>(I))
567 if (!Opened.count(MDN) && !Replacements.count(MDN) && !prune(N, MDN) &&
568 !isa<DICompileUnit>(MDN))
569 ToVisit.push_back(MDN);
570 }
571}
572
573bool llvm::stripNonLineTableDebugInfo(Module &M) {
574 bool Changed = false;
575
576 // First off, delete the debug intrinsics.
577 auto RemoveUses = [&](StringRef Name) {
578 if (auto *DbgVal = M.getFunction(Name)) {
579 while (!DbgVal->use_empty())
580 cast<Instruction>(DbgVal->user_back())->eraseFromParent();
581 DbgVal->eraseFromParent();
582 Changed = true;
583 }
584 };
585 RemoveUses("llvm.dbg.declare");
586 RemoveUses("llvm.dbg.value");
587
588 // Delete non-CU debug info named metadata nodes.
589 for (auto NMI = M.named_metadata_begin(), NME = M.named_metadata_end();
590 NMI != NME;) {
591 NamedMDNode *NMD = &*NMI;
592 ++NMI;
593 // Specifically keep dbg.cu around.
594 if (NMD->getName() == "llvm.dbg.cu")
595 continue;
596 }
597
598 // Drop all dbg attachments from global variables.
599 for (auto &GV : M.globals())
600 GV.eraseMetadata(LLVMContext::MD_dbg);
601
602 DebugTypeInfoRemoval Mapper(M.getContext());
Eugene Zelenkof53a7b42017-05-05 22:30:37 +0000603 auto remap = [&](MDNode *Node) -> MDNode * {
Michael Ilsemane5428042016-10-25 18:44:13 +0000604 if (!Node)
605 return nullptr;
606 Mapper.traverseAndRemap(Node);
607 auto *NewNode = Mapper.mapNode(Node);
608 Changed |= Node != NewNode;
609 Node = NewNode;
610 return NewNode;
611 };
612
613 // Rewrite the DebugLocs to be equivalent to what
614 // -gline-tables-only would have created.
615 for (auto &F : M) {
616 if (auto *SP = F.getSubprogram()) {
617 Mapper.traverseAndRemap(SP);
618 auto *NewSP = cast<DISubprogram>(Mapper.mapNode(SP));
619 Changed |= SP != NewSP;
620 F.setSubprogram(NewSP);
621 }
622 for (auto &BB : F) {
623 for (auto &I : BB) {
Adrian Prantl346dcaf2017-03-30 20:10:56 +0000624 auto remapDebugLoc = [&](DebugLoc DL) -> DebugLoc {
625 auto *Scope = DL.getScope();
626 MDNode *InlinedAt = DL.getInlinedAt();
627 Scope = remap(Scope);
628 InlinedAt = remap(InlinedAt);
629 return DebugLoc::get(DL.getLine(), DL.getCol(), Scope, InlinedAt);
630 };
Michael Ilsemane5428042016-10-25 18:44:13 +0000631
Adrian Prantl346dcaf2017-03-30 20:10:56 +0000632 if (I.getDebugLoc() != DebugLoc())
633 I.setDebugLoc(remapDebugLoc(I.getDebugLoc()));
634
635 // Remap DILocations in untyped MDNodes (e.g., llvm.loop).
636 SmallVector<std::pair<unsigned, MDNode *>, 2> MDs;
637 I.getAllMetadata(MDs);
638 for (auto Attachment : MDs)
639 if (auto *T = dyn_cast_or_null<MDTuple>(Attachment.second))
640 for (unsigned N = 0; N < T->getNumOperands(); ++N)
641 if (auto *Loc = dyn_cast_or_null<DILocation>(T->getOperand(N)))
642 if (Loc != DebugLoc())
643 T->replaceOperandWith(N, remapDebugLoc(Loc));
Michael Ilsemane5428042016-10-25 18:44:13 +0000644 }
645 }
646 }
647
648 // Create a new llvm.dbg.cu, which is equivalent to the one
649 // -gline-tables-only would have created.
650 for (auto &NMD : M.getNamedMDList()) {
651 SmallVector<MDNode *, 8> Ops;
652 for (MDNode *Op : NMD.operands())
653 Ops.push_back(remap(Op));
654
655 if (!Changed)
656 continue;
657
658 NMD.clearOperands();
659 for (auto *Op : Ops)
660 if (Op)
661 NMD.addOperand(Op);
662 }
663 return Changed;
664}
665
Manman Renbd4daf82013-12-03 00:12:14 +0000666unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
David Majnemere7a9cdb2015-02-16 06:04:53 +0000667 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000668 M.getModuleFlag("Debug Info Version")))
669 return Val->getZExtValue();
670 return 0;
Manman Ren8b4306c2013-12-02 21:29:56 +0000671}