DebugInfo: Remove DITypedArray<>, replace with typedefs
[oota-llvm.git] / lib / IR / DebugInfo.cpp
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/ADT/SmallString.h"
20 #include "llvm/Analysis/ValueTracking.h"
21 #include "llvm/IR/Constants.h"
22 #include "llvm/IR/DIBuilder.h"
23 #include "llvm/IR/DerivedTypes.h"
24 #include "llvm/IR/Instructions.h"
25 #include "llvm/IR/IntrinsicInst.h"
26 #include "llvm/IR/Intrinsics.h"
27 #include "llvm/IR/GVMaterializer.h"
28 #include "llvm/IR/Module.h"
29 #include "llvm/IR/ValueHandle.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/Dwarf.h"
32 #include "llvm/Support/raw_ostream.h"
33 using namespace llvm;
34 using namespace llvm::dwarf;
35
36 //===----------------------------------------------------------------------===//
37 // DIDescriptor
38 //===----------------------------------------------------------------------===//
39
40 static Metadata *getField(const MDNode *DbgNode, unsigned Elt) {
41   if (!DbgNode || Elt >= DbgNode->getNumOperands())
42     return nullptr;
43   return DbgNode->getOperand(Elt);
44 }
45
46 static MDNode *getNodeField(const MDNode *DbgNode, unsigned Elt) {
47   return dyn_cast_or_null<MDNode>(getField(DbgNode, Elt));
48 }
49
50 DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
51   MDNode *Field = getNodeField(DbgNode, Elt);
52   return DIDescriptor(Field);
53 }
54
55 /// \brief Return the size reported by the variable's type.
56 unsigned DIVariable::getSizeInBits(const DITypeIdentifierMap &Map) {
57   DIType Ty = getType().resolve(Map);
58   // Follow derived types until we reach a type that
59   // reports back a size.
60   while (isa<MDDerivedType>(Ty) && !Ty.getSizeInBits()) {
61     DIDerivedType DT = cast<MDDerivedType>(Ty);
62     Ty = DT.getTypeDerivedFrom().resolve(Map);
63   }
64   assert(Ty.getSizeInBits() && "type with size 0");
65   return Ty.getSizeInBits();
66 }
67
68 //===----------------------------------------------------------------------===//
69 // Simple Descriptor Constructors and other Methods
70 //===----------------------------------------------------------------------===//
71
72 void DIDescriptor::replaceAllUsesWith(LLVMContext &, DIDescriptor D) {
73   assert(DbgNode && "Trying to replace an unverified type!");
74   assert(DbgNode->isTemporary() && "Expected temporary node");
75   TempMDNode Temp(get());
76
77   // Since we use a TrackingVH for the node, its easy for clients to manufacture
78   // legitimate situations where they want to replaceAllUsesWith() on something
79   // which, due to uniquing, has merged with the source. We shield clients from
80   // this detail by allowing a value to be replaced with replaceAllUsesWith()
81   // itself.
82   if (Temp.get() == D.get()) {
83     DbgNode = MDNode::replaceWithUniqued(std::move(Temp));
84     return;
85   }
86
87   Temp->replaceAllUsesWith(D.get());
88   DbgNode = D.get();
89 }
90
91 void DIDescriptor::replaceAllUsesWith(MDNode *D) {
92   assert(DbgNode && "Trying to replace an unverified type!");
93   assert(DbgNode != D && "This replacement should always happen");
94   assert(DbgNode->isTemporary() && "Expected temporary node");
95   TempMDNode Node(get());
96   Node->replaceAllUsesWith(D);
97 }
98
99 #ifndef NDEBUG
100 /// \brief Check if a value can be a reference to a type.
101 static bool isTypeRef(const Metadata *MD) {
102   if (!MD)
103     return true;
104   if (auto *S = dyn_cast<MDString>(MD))
105     return !S->getString().empty();
106   return isa<MDType>(MD);
107 }
108
109 /// \brief Check if a value can be a ScopeRef.
110 static bool isScopeRef(const Metadata *MD) {
111   if (!MD)
112     return true;
113   if (auto *S = dyn_cast<MDString>(MD))
114     return !S->getString().empty();
115   return isa<MDScope>(MD);
116 }
117
118 /// \brief Check if a value can be a DescriptorRef.
119 static bool isDescriptorRef(const Metadata *MD) {
120   if (!MD)
121     return true;
122   if (auto *S = dyn_cast<MDString>(MD))
123     return !S->getString().empty();
124   return isa<MDNode>(MD);
125 }
126 #endif
127
128 DIScopeRef DIScope::getRef() const { return MDScopeRef::get(get()); }
129
130 bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
131   assert(CurFn && "Invalid function");
132   DISubprogram SP = dyn_cast<MDSubprogram>(getContext());
133   if (!SP)
134     return false;
135   // This variable is not inlined function argument if its scope
136   // does not describe current function.
137   return !SP.describes(CurFn);
138 }
139
140 Function *DISubprogram::getFunction() const {
141   if (auto *N = get())
142     if (auto *C = dyn_cast_or_null<ConstantAsMetadata>(N->getFunction()))
143       return dyn_cast<Function>(C->getValue());
144   return nullptr;
145 }
146
147 bool DISubprogram::describes(const Function *F) {
148   assert(F && "Invalid function");
149   if (F == getFunction())
150     return true;
151   StringRef Name = getLinkageName();
152   if (Name.empty())
153     Name = getName();
154   if (F->getName() == Name)
155     return true;
156   return false;
157 }
158
159 GlobalVariable *DIGlobalVariable::getGlobal() const {
160   return dyn_cast_or_null<GlobalVariable>(getConstant());
161 }
162
163 DIScopeRef DIScope::getContext() const {
164   if (DIType T = dyn_cast<MDType>(*this))
165     return T.getContext();
166
167   if (DISubprogram SP = dyn_cast<MDSubprogram>(*this))
168     return DIScopeRef(SP.getContext());
169
170   if (DILexicalBlock LB = dyn_cast<MDLexicalBlockBase>(*this))
171     return DIScopeRef(LB.getContext());
172
173   if (DINameSpace NS = dyn_cast<MDNamespace>(*this))
174     return DIScopeRef(NS.getContext());
175
176   assert((isa<MDFile>(*this) || isa<MDCompileUnit>(*this)) &&
177          "Unhandled type of scope.");
178   return DIScopeRef(nullptr);
179 }
180
181 StringRef DIScope::getName() const {
182   if (DIType T = dyn_cast<MDType>(*this))
183     return T.getName();
184   if (DISubprogram SP = dyn_cast<MDSubprogram>(*this))
185     return SP.getName();
186   if (DINameSpace NS = dyn_cast<MDNamespace>(*this))
187     return NS.getName();
188   assert((isa<MDLexicalBlockBase>(*this) || isa<MDFile>(*this) ||
189           isa<MDCompileUnit>(*this)) &&
190          "Unhandled type of scope.");
191   return StringRef();
192 }
193
194 StringRef DIScope::getFilename() const {
195   if (auto *N = get())
196     if (auto *F = N->getFile())
197       return F->getFilename();
198   return "";
199 }
200
201 StringRef DIScope::getDirectory() const {
202   if (auto *N = get())
203     if (auto *F = N->getFile())
204       return F->getDirectory();
205   return "";
206 }
207
208 void DICompileUnit::replaceSubprograms(DIArray Subprograms) {
209   get()->replaceSubprograms(Subprograms);
210 }
211
212 void DICompileUnit::replaceGlobalVariables(DIArray GlobalVariables) {
213   get()->replaceGlobalVariables(GlobalVariables);
214 }
215
216 DILocation DILocation::copyWithNewScope(LLVMContext &Ctx,
217                                         DILexicalBlockFile NewScope) {
218   assert(NewScope && "Expected valid scope");
219
220   const auto *Old = cast<MDLocation>(DbgNode);
221   return DILocation(MDLocation::get(Ctx, Old->getLine(), Old->getColumn(),
222                                     NewScope, Old->getInlinedAt()));
223 }
224
225 unsigned DILocation::computeNewDiscriminator(LLVMContext &Ctx) {
226   std::pair<const char *, unsigned> Key(getFilename().data(), getLineNumber());
227   return ++Ctx.pImpl->DiscriminatorTable[Key];
228 }
229
230 DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
231                                        LLVMContext &VMContext) {
232   return cast<MDLocalVariable>(DV)
233       ->withInline(cast_or_null<MDLocation>(InlinedScope));
234 }
235
236 DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
237   return cast<MDLocalVariable>(DV)->withoutInline();
238 }
239
240 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
241   if (auto *LocalScope = dyn_cast_or_null<MDLocalScope>(Scope))
242     return LocalScope->getSubprogram();
243   return nullptr;
244 }
245
246 DISubprogram llvm::getDISubprogram(const Function *F) {
247   // We look for the first instr that has a debug annotation leading back to F.
248   for (auto &BB : *F) {
249     auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) {
250       return Inst.getDebugLoc();
251     });
252     if (Inst == BB.end())
253       continue;
254     DebugLoc DLoc = Inst->getDebugLoc();
255     const MDNode *Scope = DLoc.getInlinedAtScope();
256     DISubprogram Subprogram = getDISubprogram(Scope);
257     return Subprogram.describes(F) ? Subprogram : DISubprogram();
258   }
259
260   return DISubprogram();
261 }
262
263 DICompositeType llvm::getDICompositeType(DIType T) {
264   if (auto *C = dyn_cast_or_null<MDCompositeTypeBase>(T))
265     return C;
266
267   if (auto *D = dyn_cast_or_null<MDDerivedTypeBase>(T)) {
268     // This function is currently used by dragonegg and dragonegg does
269     // not generate identifier for types, so using an empty map to resolve
270     // DerivedFrom should be fine.
271     DITypeIdentifierMap EmptyMap;
272     return getDICompositeType(
273         DIDerivedType(D).getTypeDerivedFrom().resolve(EmptyMap));
274   }
275
276   return nullptr;
277 }
278
279 DITypeIdentifierMap
280 llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
281   DITypeIdentifierMap Map;
282   for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
283     DICompileUnit CU = cast<MDCompileUnit>(CU_Nodes->getOperand(CUi));
284     DIArray Retain = CU.getRetainedTypes();
285     for (unsigned Ti = 0, Te = Retain.size(); Ti != Te; ++Ti) {
286       if (!isa<MDCompositeType>(Retain[Ti]))
287         continue;
288       DICompositeType Ty = cast<MDCompositeType>(Retain[Ti]);
289       if (MDString *TypeId = Ty.getIdentifier()) {
290         // Definition has priority over declaration.
291         // Try to insert (TypeId, Ty) to Map.
292         std::pair<DITypeIdentifierMap::iterator, bool> P =
293             Map.insert(std::make_pair(TypeId, Ty));
294         // If TypeId already exists in Map and this is a definition, replace
295         // whatever we had (declaration or definition) with the definition.
296         if (!P.second && !Ty.isForwardDecl())
297           P.first->second = Ty;
298       }
299     }
300   }
301   return Map;
302 }
303
304 //===----------------------------------------------------------------------===//
305 // DebugInfoFinder implementations.
306 //===----------------------------------------------------------------------===//
307
308 void DebugInfoFinder::reset() {
309   CUs.clear();
310   SPs.clear();
311   GVs.clear();
312   TYs.clear();
313   Scopes.clear();
314   NodesSeen.clear();
315   TypeIdentifierMap.clear();
316   TypeMapInitialized = false;
317 }
318
319 void DebugInfoFinder::InitializeTypeMap(const Module &M) {
320   if (!TypeMapInitialized)
321     if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
322       TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
323       TypeMapInitialized = true;
324     }
325 }
326
327 void DebugInfoFinder::processModule(const Module &M) {
328   InitializeTypeMap(M);
329   if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
330     for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
331       DICompileUnit CU = cast<MDCompileUnit>(CU_Nodes->getOperand(i));
332       addCompileUnit(CU);
333       for (DIGlobalVariable DIG : CU->getGlobalVariables()) {
334         if (addGlobalVariable(DIG)) {
335           processScope(DIG.getContext());
336           processType(DIG.getType().resolve(TypeIdentifierMap));
337         }
338       }
339       for (auto *SP : CU->getSubprograms())
340         processSubprogram(SP);
341       for (auto *ET : CU->getEnumTypes())
342         processType(ET);
343       for (auto *RT : CU->getRetainedTypes())
344         processType(RT);
345       for (DIImportedEntity Import : CU->getImportedEntities()) {
346         DIDescriptor Entity = Import.getEntity().resolve(TypeIdentifierMap);
347         if (auto *T = dyn_cast<MDType>(Entity))
348           processType(T);
349         else if (auto *SP = dyn_cast<MDSubprogram>(Entity))
350           processSubprogram(SP);
351         else if (auto *NS = dyn_cast<MDNamespace>(Entity))
352           processScope(NS->getScope());
353       }
354     }
355   }
356 }
357
358 void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) {
359   if (!Loc)
360     return;
361   InitializeTypeMap(M);
362   processScope(Loc.getScope());
363   processLocation(M, Loc.getOrigLocation());
364 }
365
366 void DebugInfoFinder::processType(DIType DT) {
367   if (!addType(DT))
368     return;
369   processScope(DT.getContext().resolve(TypeIdentifierMap));
370   if (DICompositeType DCT = dyn_cast<MDCompositeTypeBase>(DT)) {
371     processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
372     if (DISubroutineType ST = dyn_cast<MDSubroutineType>(DCT)) {
373       for (MDTypeRef Ref : ST->getTypeArray())
374         processType(Ref.resolve(TypeIdentifierMap));
375       return;
376     }
377     for (Metadata *D : DCT->getElements()->operands()) {
378       if (DIType T = dyn_cast<MDType>(D))
379         processType(T);
380       else if (DISubprogram SP = dyn_cast<MDSubprogram>(D))
381         processSubprogram(SP);
382     }
383   } else if (DIDerivedType DDT = dyn_cast<MDDerivedTypeBase>(DT)) {
384     processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
385   }
386 }
387
388 void DebugInfoFinder::processScope(DIScope Scope) {
389   if (!Scope)
390     return;
391   if (DIType Ty = dyn_cast<MDType>(Scope)) {
392     processType(Ty);
393     return;
394   }
395   if (DICompileUnit CU = dyn_cast<MDCompileUnit>(Scope)) {
396     addCompileUnit(CU);
397     return;
398   }
399   if (DISubprogram SP = dyn_cast<MDSubprogram>(Scope)) {
400     processSubprogram(SP);
401     return;
402   }
403   if (!addScope(Scope))
404     return;
405   if (DILexicalBlock LB = dyn_cast<MDLexicalBlockBase>(Scope)) {
406     processScope(LB.getContext());
407   } else if (DINameSpace NS = dyn_cast<MDNamespace>(Scope)) {
408     processScope(NS.getContext());
409   }
410 }
411
412 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
413   if (!addSubprogram(SP))
414     return;
415   processScope(SP.getContext().resolve(TypeIdentifierMap));
416   processType(SP.getType());
417   for (auto *Element : SP.getTemplateParams()) {
418     if (DITemplateTypeParameter TType =
419             dyn_cast<MDTemplateTypeParameter>(Element)) {
420       processType(TType.getType().resolve(TypeIdentifierMap));
421     } else if (DITemplateValueParameter TVal =
422                    dyn_cast<MDTemplateValueParameter>(Element)) {
423       processType(TVal.getType().resolve(TypeIdentifierMap));
424     }
425   }
426 }
427
428 void DebugInfoFinder::processDeclare(const Module &M,
429                                      const DbgDeclareInst *DDI) {
430   MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
431   if (!N)
432     return;
433   InitializeTypeMap(M);
434
435   DIVariable DV = dyn_cast<MDLocalVariable>(N);
436   if (!DV)
437     return;
438
439   if (!NodesSeen.insert(DV).second)
440     return;
441   processScope(DV.getContext());
442   processType(DV.getType().resolve(TypeIdentifierMap));
443 }
444
445 void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
446   MDNode *N = dyn_cast<MDNode>(DVI->getVariable());
447   if (!N)
448     return;
449   InitializeTypeMap(M);
450
451   DIVariable DV = dyn_cast<MDLocalVariable>(N);
452   if (!DV)
453     return;
454
455   if (!NodesSeen.insert(DV).second)
456     return;
457   processScope(DV.getContext());
458   processType(DV.getType().resolve(TypeIdentifierMap));
459 }
460
461 bool DebugInfoFinder::addType(DIType DT) {
462   if (!DT)
463     return false;
464
465   if (!NodesSeen.insert(DT).second)
466     return false;
467
468   TYs.push_back(DT);
469   return true;
470 }
471
472 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
473   if (!CU)
474     return false;
475   if (!NodesSeen.insert(CU).second)
476     return false;
477
478   CUs.push_back(CU);
479   return true;
480 }
481
482 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
483   if (!DIG)
484     return false;
485
486   if (!NodesSeen.insert(DIG).second)
487     return false;
488
489   GVs.push_back(DIG);
490   return true;
491 }
492
493 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
494   if (!SP)
495     return false;
496
497   if (!NodesSeen.insert(SP).second)
498     return false;
499
500   SPs.push_back(SP);
501   return true;
502 }
503
504 bool DebugInfoFinder::addScope(DIScope Scope) {
505   if (!Scope)
506     return false;
507   // FIXME: Ocaml binding generates a scope with no content, we treat it
508   // as null for now.
509   if (Scope->getNumOperands() == 0)
510     return false;
511   if (!NodesSeen.insert(Scope).second)
512     return false;
513   Scopes.push_back(Scope);
514   return true;
515 }
516
517 //===----------------------------------------------------------------------===//
518 // DIDescriptor: dump routines for all descriptors.
519 //===----------------------------------------------------------------------===//
520
521 void DIDescriptor::dump() const {
522   print(dbgs());
523   dbgs() << '\n';
524 }
525
526 void DIDescriptor::print(raw_ostream &OS) const {
527   if (!get())
528     return;
529   get()->print(OS);
530 }
531
532 static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
533                           const LLVMContext &Ctx) {
534   if (!DL)
535     return;
536
537   DIScope Scope = cast<MDScope>(DL.getScope());
538   // Omit the directory, because it's likely to be long and uninteresting.
539   CommentOS << Scope.getFilename();
540   CommentOS << ':' << DL.getLine();
541   if (DL.getCol() != 0)
542     CommentOS << ':' << DL.getCol();
543
544   DebugLoc InlinedAtDL = DL.getInlinedAt();
545   if (!InlinedAtDL)
546     return;
547
548   CommentOS << " @[ ";
549   printDebugLoc(InlinedAtDL, CommentOS, Ctx);
550   CommentOS << " ]";
551 }
552
553 void DIVariable::printExtendedName(raw_ostream &OS) const {
554   const LLVMContext &Ctx = DbgNode->getContext();
555   StringRef Res = getName();
556   if (!Res.empty())
557     OS << Res << "," << getLineNumber();
558   if (auto *InlinedAt = get()->getInlinedAt()) {
559     if (DebugLoc InlinedAtDL = InlinedAt) {
560       OS << " @[";
561       printDebugLoc(InlinedAtDL, OS, Ctx);
562       OS << "]";
563     }
564   }
565 }
566
567 template <> DIRef<DIDescriptor>::DIRef(const Metadata *V) : Val(V) {
568   assert(isDescriptorRef(V) &&
569          "DIDescriptorRef should be a MDString or MDNode");
570 }
571 template <> DIRef<DIScope>::DIRef(const Metadata *V) : Val(V) {
572   assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode");
573 }
574 template <> DIRef<DIType>::DIRef(const Metadata *V) : Val(V) {
575   assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode");
576 }
577
578 template <>
579 DIDescriptorRef DIDescriptor::getFieldAs<DIDescriptorRef>(unsigned Elt) const {
580   return DIDescriptorRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
581 }
582 template <>
583 DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const {
584   return DIScopeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
585 }
586 template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const {
587   return DITypeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
588 }
589
590 template <>
591 DIDescriptor
592 DIRef<DIDescriptor>::resolve(const DITypeIdentifierMap &Map) const {
593   return DIDescriptor(DebugNodeRef(Val).resolve(Map));
594 }
595 template <>
596 DIScope DIRef<DIScope>::resolve(const DITypeIdentifierMap &Map) const {
597   return MDScopeRef(Val).resolve(Map);
598 }
599 template <>
600 DIType DIRef<DIType>::resolve(const DITypeIdentifierMap &Map) const {
601   return MDTypeRef(Val).resolve(Map);
602 }
603
604 bool llvm::stripDebugInfo(Function &F) {
605   bool Changed = false;
606   for (BasicBlock &BB : F) {
607     for (Instruction &I : BB) {
608       if (I.getDebugLoc()) {
609         Changed = true;
610         I.setDebugLoc(DebugLoc());
611       }
612     }
613   }
614   return Changed;
615 }
616
617 bool llvm::StripDebugInfo(Module &M) {
618   bool Changed = false;
619
620   // Remove all of the calls to the debugger intrinsics, and remove them from
621   // the module.
622   if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
623     while (!Declare->use_empty()) {
624       CallInst *CI = cast<CallInst>(Declare->user_back());
625       CI->eraseFromParent();
626     }
627     Declare->eraseFromParent();
628     Changed = true;
629   }
630
631   if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
632     while (!DbgVal->use_empty()) {
633       CallInst *CI = cast<CallInst>(DbgVal->user_back());
634       CI->eraseFromParent();
635     }
636     DbgVal->eraseFromParent();
637     Changed = true;
638   }
639
640   for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
641          NME = M.named_metadata_end(); NMI != NME;) {
642     NamedMDNode *NMD = NMI;
643     ++NMI;
644     if (NMD->getName().startswith("llvm.dbg.")) {
645       NMD->eraseFromParent();
646       Changed = true;
647     }
648   }
649
650   for (Function &F : M)
651     Changed |= stripDebugInfo(F);
652
653   if (GVMaterializer *Materializer = M.getMaterializer())
654     Materializer->setStripDebugInfo();
655
656   return Changed;
657 }
658
659 unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
660   if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
661           M.getModuleFlag("Debug Info Version")))
662     return Val->getZExtValue();
663   return 0;
664 }
665
666 llvm::DenseMap<const llvm::Function *, llvm::DISubprogram>
667 llvm::makeSubprogramMap(const Module &M) {
668   DenseMap<const Function *, DISubprogram> R;
669
670   NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
671   if (!CU_Nodes)
672     return R;
673
674   for (MDNode *N : CU_Nodes->operands()) {
675     DICompileUnit CUNode = cast<MDCompileUnit>(N);
676     for (DISubprogram SP : CUNode->getSubprograms()) {
677       if (Function *F = SP.getFunction())
678         R.insert(std::make_pair(F, SP));
679     }
680   }
681   return R;
682 }