1 //===--- 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 
15 #include "llvm/IR/DebugInfo.h"
16 #include "LLVMContextImpl.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/SmallPtrSet.h"
19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/DIBuilder.h"
21 #include "llvm/IR/DerivedTypes.h"
22 #include "llvm/IR/GVMaterializer.h"
23 #include "llvm/IR/Instructions.h"
24 #include "llvm/IR/IntrinsicInst.h"
25 #include "llvm/IR/Intrinsics.h"
26 #include "llvm/IR/Module.h"
27 #include "llvm/IR/ValueHandle.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/Dwarf.h"
30 #include "llvm/Support/raw_ostream.h"
31 using namespace llvm;
32 using namespace llvm::dwarf;
33 
34 DISubprogram *llvm::getDISubprogram(const MDNode *Scope) {
35   if (auto *LocalScope = dyn_cast_or_null<DILocalScope>(Scope))
36     return LocalScope->getSubprogram();
37   return nullptr;
38 }
39 
40 //===----------------------------------------------------------------------===//
41 // DebugInfoFinder implementations.
42 //===----------------------------------------------------------------------===//
43 
44 void DebugInfoFinder::reset() {
45   CUs.clear();
46   SPs.clear();
47   GVs.clear();
48   TYs.clear();
49   Scopes.clear();
50   NodesSeen.clear();
51 }
52 
53 void DebugInfoFinder::processModule(const Module &M) {
54   for (auto *CU : M.debug_compile_units()) {
55     addCompileUnit(CU);
56     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());
62     }
63     for (auto *ET : CU->getEnumTypes())
64       processType(ET);
65     for (auto *RT : CU->getRetainedTypes())
66       if (auto *T = dyn_cast<DIType>(RT))
67         processType(T);
68       else
69         processSubprogram(cast<DISubprogram>(RT));
70     for (auto *Import : CU->getImportedEntities()) {
71       auto *Entity = Import->getEntity().resolve();
72       if (auto *T = dyn_cast<DIType>(Entity))
73         processType(T);
74       else if (auto *SP = dyn_cast<DISubprogram>(Entity))
75         processSubprogram(SP);
76       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());
80     }
81   }
82   for (auto &F : M.functions())
83     if (auto *SP = cast_or_null<DISubprogram>(F.getSubprogram()))
84       processSubprogram(SP);
85 }
86 
87 void DebugInfoFinder::processLocation(const Module &M, const DILocation *Loc) {
88   if (!Loc)
89     return;
90   processScope(Loc->getScope());
91   processLocation(M, Loc->getInlinedAt());
92 }
93 
94 void DebugInfoFinder::processType(DIType *DT) {
95   if (!addType(DT))
96     return;
97   processScope(DT->getScope().resolve());
98   if (auto *ST = dyn_cast<DISubroutineType>(DT)) {
99     for (DITypeRef Ref : ST->getTypeArray())
100       processType(Ref.resolve());
101     return;
102   }
103   if (auto *DCT = dyn_cast<DICompositeType>(DT)) {
104     processType(DCT->getBaseType().resolve());
105     for (Metadata *D : DCT->getElements()) {
106       if (auto *T = dyn_cast<DIType>(D))
107         processType(T);
108       else if (auto *SP = dyn_cast<DISubprogram>(D))
109         processSubprogram(SP);
110     }
111     return;
112   }
113   if (auto *DDT = dyn_cast<DIDerivedType>(DT)) {
114     processType(DDT->getBaseType().resolve());
115   }
116 }
117 
118 void DebugInfoFinder::processScope(DIScope *Scope) {
119   if (!Scope)
120     return;
121   if (auto *Ty = dyn_cast<DIType>(Scope)) {
122     processType(Ty);
123     return;
124   }
125   if (auto *CU = dyn_cast<DICompileUnit>(Scope)) {
126     addCompileUnit(CU);
127     return;
128   }
129   if (auto *SP = dyn_cast<DISubprogram>(Scope)) {
130     processSubprogram(SP);
131     return;
132   }
133   if (!addScope(Scope))
134     return;
135   if (auto *LB = dyn_cast<DILexicalBlockBase>(Scope)) {
136     processScope(LB->getScope());
137   } else if (auto *NS = dyn_cast<DINamespace>(Scope)) {
138     processScope(NS->getScope());
139   } else if (auto *M = dyn_cast<DIModule>(Scope)) {
140     processScope(M->getScope());
141   }
142 }
143 
144 void DebugInfoFinder::processSubprogram(DISubprogram *SP) {
145   if (!addSubprogram(SP))
146     return;
147   processScope(SP->getScope().resolve());
148   processType(SP->getType());
149   for (auto *Element : SP->getTemplateParams()) {
150     if (auto *TType = dyn_cast<DITemplateTypeParameter>(Element)) {
151       processType(TType->getType().resolve());
152     } else if (auto *TVal = dyn_cast<DITemplateValueParameter>(Element)) {
153       processType(TVal->getType().resolve());
154     }
155   }
156 }
157 
158 void DebugInfoFinder::processDeclare(const Module &M,
159                                      const DbgDeclareInst *DDI) {
160   auto *N = dyn_cast<MDNode>(DDI->getVariable());
161   if (!N)
162     return;
163 
164   auto *DV = dyn_cast<DILocalVariable>(N);
165   if (!DV)
166     return;
167 
168   if (!NodesSeen.insert(DV).second)
169     return;
170   processScope(DV->getScope());
171   processType(DV->getType().resolve());
172 }
173 
174 void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
175   auto *N = dyn_cast<MDNode>(DVI->getVariable());
176   if (!N)
177     return;
178 
179   auto *DV = dyn_cast<DILocalVariable>(N);
180   if (!DV)
181     return;
182 
183   if (!NodesSeen.insert(DV).second)
184     return;
185   processScope(DV->getScope());
186   processType(DV->getType().resolve());
187 }
188 
189 bool DebugInfoFinder::addType(DIType *DT) {
190   if (!DT)
191     return false;
192 
193   if (!NodesSeen.insert(DT).second)
194     return false;
195 
196   TYs.push_back(const_cast<DIType *>(DT));
197   return true;
198 }
199 
200 bool DebugInfoFinder::addCompileUnit(DICompileUnit *CU) {
201   if (!CU)
202     return false;
203   if (!NodesSeen.insert(CU).second)
204     return false;
205 
206   CUs.push_back(CU);
207   return true;
208 }
209 
210 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariableExpression *DIG) {
211   if (!NodesSeen.insert(DIG).second)
212     return false;
213 
214   GVs.push_back(DIG);
215   return true;
216 }
217 
218 bool DebugInfoFinder::addSubprogram(DISubprogram *SP) {
219   if (!SP)
220     return false;
221 
222   if (!NodesSeen.insert(SP).second)
223     return false;
224 
225   SPs.push_back(SP);
226   return true;
227 }
228 
229 bool DebugInfoFinder::addScope(DIScope *Scope) {
230   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;
236   if (!NodesSeen.insert(Scope).second)
237     return false;
238   Scopes.push_back(Scope);
239   return true;
240 }
241 
242 static 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 
271 bool llvm::stripDebugInfo(Function &F) {
272   bool Changed = false;
273   if (F.getSubprogram()) {
274     Changed = true;
275     F.setSubprogram(nullptr);
276   }
277 
278   llvm::DenseMap<llvm::MDNode*, llvm::MDNode*> LoopIDsMap;
279   for (BasicBlock &BB : F) {
280     for (auto II = BB.begin(), End = BB.end(); II != End;) {
281       Instruction &I = *II++; // We may delete the instruction, increment now.
282       if (isa<DbgInfoIntrinsic>(&I)) {
283         I.eraseFromParent();
284         Changed = true;
285         continue;
286       }
287       if (I.getDebugLoc()) {
288         Changed = true;
289         I.setDebugLoc(DebugLoc());
290       }
291     }
292 
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     }
301   }
302   return Changed;
303 }
304 
305 bool llvm::StripDebugInfo(Module &M) {
306   bool Changed = false;
307 
308   for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
309          NME = M.named_metadata_end(); NMI != NME;) {
310     NamedMDNode *NMD = &*NMI;
311     ++NMI;
312 
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") {
317       NMD->eraseFromParent();
318       Changed = true;
319     }
320   }
321 
322   for (Function &F : M)
323     Changed |= stripDebugInfo(F);
324 
325   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 
334   if (GVMaterializer *Materializer = M.getMaterializer())
335     Materializer->setStripDebugInfo();
336 
337   return Changed;
338 }
339 
340 namespace {
341 
342 /// Helper class to downgrade -g metadata to -gline-tables-only metadata.
343 class DebugTypeInfoRemoval {
344   DenseMap<Metadata *, Metadata *> Replacements;
345 
346 public:
347   /// The (void)() type.
348   MDNode *EmptySubroutineType;
349 
350 private:
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 
364 public:
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 
384 private:
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(),
452         CU->getDWOId(), CU->getSplitDebugInlining());
453   }
454 
455   DILocation *getReplacementMDLocation(DILocation *MLD) {
456     auto *Scope = map(MLD->getScope());
457     auto *InlinedAt = map(MLD->getInlinedAt());
458     if (MLD->isDistinct())
459       return DILocation::getDistinct(MLD->getContext(), MLD->getLine(),
460                                      MLD->getColumn(), Scope, InlinedAt);
461     return DILocation::get(MLD->getContext(), MLD->getLine(), MLD->getColumn(),
462                            Scope, InlinedAt);
463   }
464 
465   /// Create a new generic MDNode, to replace the one given
466   MDNode *getReplacementMDNode(MDNode *N) {
467     SmallVector<Metadata *, 8> Ops;
468     Ops.reserve(N->getNumOperands());
469     for (auto &I : N->operands())
470       if (I)
471         Ops.push_back(map(I));
472     auto *Ret = MDNode::get(N->getContext(), Ops);
473     return Ret;
474   }
475 
476   /// Attempt to re-map N to a newly created node.
477   void remap(MDNode *N) {
478     if (Replacements.count(N))
479       return;
480 
481     auto doRemap = [&](MDNode *N) -> MDNode * {
482       if (!N)
483         return nullptr;
484       if (auto *MDSub = dyn_cast<DISubprogram>(N)) {
485         remap(MDSub->getUnit());
486         return getReplacementSubprogram(MDSub);
487       }
488       if (isa<DISubroutineType>(N))
489         return EmptySubroutineType;
490       if (auto *CU = dyn_cast<DICompileUnit>(N))
491         return getReplacementCU(CU);
492       if (isa<DIFile>(N))
493         return N;
494       if (auto *MDLB = dyn_cast<DILexicalBlockBase>(N))
495         // Remap to our referenced scope (recursively).
496         return mapNode(MDLB->getScope());
497       if (auto *MLD = dyn_cast<DILocation>(N))
498         return getReplacementMDLocation(MLD);
499 
500       // Otherwise, if we see these, just drop them now. Not strictly necessary,
501       // but this speeds things up a little.
502       if (isa<DINode>(N))
503         return nullptr;
504 
505       return getReplacementMDNode(N);
506     };
507     Replacements[N] = doRemap(N);
508   }
509 
510   /// Do the remapping traversal.
511   void traverse(MDNode *);
512 };
513 
514 } // Anonymous namespace.
515 
516 void DebugTypeInfoRemoval::traverse(MDNode *N) {
517   if (!N || Replacements.count(N))
518     return;
519 
520   // To avoid cycles, as well as for efficiency sake, we will sometimes prune
521   // parts of the graph.
522   auto prune = [](MDNode *Parent, MDNode *Child) {
523     if (auto *MDS = dyn_cast<DISubprogram>(Parent))
524       return Child == MDS->getVariables().get();
525     return false;
526   };
527 
528   SmallVector<MDNode *, 16> ToVisit;
529   DenseSet<MDNode *> Opened;
530 
531   // Visit each node starting at N in post order, and map them.
532   ToVisit.push_back(N);
533   while (!ToVisit.empty()) {
534     auto *N = ToVisit.back();
535     if (!Opened.insert(N).second) {
536       // Close it.
537       remap(N);
538       ToVisit.pop_back();
539       continue;
540     }
541     for (auto &I : N->operands())
542       if (auto *MDN = dyn_cast_or_null<MDNode>(I))
543         if (!Opened.count(MDN) && !Replacements.count(MDN) && !prune(N, MDN) &&
544             !isa<DICompileUnit>(MDN))
545           ToVisit.push_back(MDN);
546   }
547 }
548 
549 bool llvm::stripNonLineTableDebugInfo(Module &M) {
550   bool Changed = false;
551 
552   // First off, delete the debug intrinsics.
553   auto RemoveUses = [&](StringRef Name) {
554     if (auto *DbgVal = M.getFunction(Name)) {
555       while (!DbgVal->use_empty())
556         cast<Instruction>(DbgVal->user_back())->eraseFromParent();
557       DbgVal->eraseFromParent();
558       Changed = true;
559     }
560   };
561   RemoveUses("llvm.dbg.declare");
562   RemoveUses("llvm.dbg.value");
563 
564   // Delete non-CU debug info named metadata nodes.
565   for (auto NMI = M.named_metadata_begin(), NME = M.named_metadata_end();
566        NMI != NME;) {
567     NamedMDNode *NMD = &*NMI;
568     ++NMI;
569     // Specifically keep dbg.cu around.
570     if (NMD->getName() == "llvm.dbg.cu")
571       continue;
572   }
573 
574   // Drop all dbg attachments from global variables.
575   for (auto &GV : M.globals())
576     GV.eraseMetadata(LLVMContext::MD_dbg);
577 
578   DebugTypeInfoRemoval Mapper(M.getContext());
579   auto remap = [&](llvm::MDNode *Node) -> llvm::MDNode * {
580     if (!Node)
581       return nullptr;
582     Mapper.traverseAndRemap(Node);
583     auto *NewNode = Mapper.mapNode(Node);
584     Changed |= Node != NewNode;
585     Node = NewNode;
586     return NewNode;
587   };
588 
589   // Rewrite the DebugLocs to be equivalent to what
590   // -gline-tables-only would have created.
591   for (auto &F : M) {
592     if (auto *SP = F.getSubprogram()) {
593       Mapper.traverseAndRemap(SP);
594       auto *NewSP = cast<DISubprogram>(Mapper.mapNode(SP));
595       Changed |= SP != NewSP;
596       F.setSubprogram(NewSP);
597     }
598     for (auto &BB : F) {
599       for (auto &I : BB) {
600         if (I.getDebugLoc() == DebugLoc())
601           continue;
602 
603         // Make a replacement.
604         auto &DL = I.getDebugLoc();
605         auto *Scope = DL.getScope();
606         MDNode *InlinedAt = DL.getInlinedAt();
607         Scope = remap(Scope);
608         InlinedAt = remap(InlinedAt);
609         I.setDebugLoc(
610             DebugLoc::get(DL.getLine(), DL.getCol(), Scope, InlinedAt));
611       }
612     }
613   }
614 
615   // Create a new llvm.dbg.cu, which is equivalent to the one
616   // -gline-tables-only would have created.
617   for (auto &NMD : M.getNamedMDList()) {
618     SmallVector<MDNode *, 8> Ops;
619     for (MDNode *Op : NMD.operands())
620       Ops.push_back(remap(Op));
621 
622     if (!Changed)
623       continue;
624 
625     NMD.clearOperands();
626     for (auto *Op : Ops)
627       if (Op)
628         NMD.addOperand(Op);
629   }
630   return Changed;
631 }
632 
633 unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
634   if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
635           M.getModuleFlag("Debug Info Version")))
636     return Val->getZExtValue();
637   return 0;
638 }
639