When promoting an alloca to registers discard any lifetime intrinsics.
[llvm/stm8.git] / lib / Analysis / DebugInfo.cpp
blob67f8147f4d6108eb3fcdebabdd058bc107d7fd4d
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.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Analysis/DebugInfo.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/Intrinsics.h"
19 #include "llvm/IntrinsicInst.h"
20 #include "llvm/Instructions.h"
21 #include "llvm/Module.h"
22 #include "llvm/Analysis/ValueTracking.h"
23 #include "llvm/ADT/SmallPtrSet.h"
24 #include "llvm/ADT/SmallString.h"
25 #include "llvm/ADT/STLExtras.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/Dwarf.h"
28 #include "llvm/Support/raw_ostream.h"
29 using namespace llvm;
30 using namespace llvm::dwarf;
32 //===----------------------------------------------------------------------===//
33 // DIDescriptor
34 //===----------------------------------------------------------------------===//
36 DIDescriptor::DIDescriptor(const DIFile F) : DbgNode(F.DbgNode) {
39 DIDescriptor::DIDescriptor(const DISubprogram F) : DbgNode(F.DbgNode) {
42 DIDescriptor::DIDescriptor(const DILexicalBlock F) : DbgNode(F.DbgNode) {
45 DIDescriptor::DIDescriptor(const DIVariable F) : DbgNode(F.DbgNode) {
48 DIDescriptor::DIDescriptor(const DIType F) : DbgNode(F.DbgNode) {
51 StringRef
52 DIDescriptor::getStringField(unsigned Elt) const {
53 if (DbgNode == 0)
54 return StringRef();
56 if (Elt < DbgNode->getNumOperands())
57 if (MDString *MDS = dyn_cast_or_null<MDString>(DbgNode->getOperand(Elt)))
58 return MDS->getString();
60 return StringRef();
63 uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
64 if (DbgNode == 0)
65 return 0;
67 if (Elt < DbgNode->getNumOperands())
68 if (ConstantInt *CI = dyn_cast<ConstantInt>(DbgNode->getOperand(Elt)))
69 return CI->getZExtValue();
71 return 0;
74 DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
75 if (DbgNode == 0)
76 return DIDescriptor();
78 if (Elt < DbgNode->getNumOperands())
79 return
80 DIDescriptor(dyn_cast_or_null<const MDNode>(DbgNode->getOperand(Elt)));
81 return DIDescriptor();
84 GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
85 if (DbgNode == 0)
86 return 0;
88 if (Elt < DbgNode->getNumOperands())
89 return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt));
90 return 0;
93 Constant *DIDescriptor::getConstantField(unsigned Elt) const {
94 if (DbgNode == 0)
95 return 0;
97 if (Elt < DbgNode->getNumOperands())
98 return dyn_cast_or_null<Constant>(DbgNode->getOperand(Elt));
99 return 0;
102 Function *DIDescriptor::getFunctionField(unsigned Elt) const {
103 if (DbgNode == 0)
104 return 0;
106 if (Elt < DbgNode->getNumOperands())
107 return dyn_cast_or_null<Function>(DbgNode->getOperand(Elt));
108 return 0;
111 unsigned DIVariable::getNumAddrElements() const {
112 if (getVersion() <= llvm::LLVMDebugVersion8)
113 return DbgNode->getNumOperands()-6;
114 return DbgNode->getNumOperands()-7;
118 //===----------------------------------------------------------------------===//
119 // Predicates
120 //===----------------------------------------------------------------------===//
122 /// isBasicType - Return true if the specified tag is legal for
123 /// DIBasicType.
124 bool DIDescriptor::isBasicType() const {
125 return DbgNode && getTag() == dwarf::DW_TAG_base_type;
128 /// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
129 bool DIDescriptor::isDerivedType() const {
130 if (!DbgNode) return false;
131 switch (getTag()) {
132 case dwarf::DW_TAG_typedef:
133 case dwarf::DW_TAG_pointer_type:
134 case dwarf::DW_TAG_reference_type:
135 case dwarf::DW_TAG_const_type:
136 case dwarf::DW_TAG_volatile_type:
137 case dwarf::DW_TAG_restrict_type:
138 case dwarf::DW_TAG_member:
139 case dwarf::DW_TAG_inheritance:
140 case dwarf::DW_TAG_friend:
141 return true;
142 default:
143 // CompositeTypes are currently modelled as DerivedTypes.
144 return isCompositeType();
148 /// isCompositeType - Return true if the specified tag is legal for
149 /// DICompositeType.
150 bool DIDescriptor::isCompositeType() const {
151 if (!DbgNode) return false;
152 switch (getTag()) {
153 case dwarf::DW_TAG_array_type:
154 case dwarf::DW_TAG_structure_type:
155 case dwarf::DW_TAG_union_type:
156 case dwarf::DW_TAG_enumeration_type:
157 case dwarf::DW_TAG_vector_type:
158 case dwarf::DW_TAG_subroutine_type:
159 case dwarf::DW_TAG_class_type:
160 return true;
161 default:
162 return false;
166 /// isVariable - Return true if the specified tag is legal for DIVariable.
167 bool DIDescriptor::isVariable() const {
168 if (!DbgNode) return false;
169 switch (getTag()) {
170 case dwarf::DW_TAG_auto_variable:
171 case dwarf::DW_TAG_arg_variable:
172 case dwarf::DW_TAG_return_variable:
173 return true;
174 default:
175 return false;
179 /// isType - Return true if the specified tag is legal for DIType.
180 bool DIDescriptor::isType() const {
181 return isBasicType() || isCompositeType() || isDerivedType();
184 /// isSubprogram - Return true if the specified tag is legal for
185 /// DISubprogram.
186 bool DIDescriptor::isSubprogram() const {
187 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
190 /// isGlobalVariable - Return true if the specified tag is legal for
191 /// DIGlobalVariable.
192 bool DIDescriptor::isGlobalVariable() const {
193 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
194 getTag() == dwarf::DW_TAG_constant);
197 /// isGlobal - Return true if the specified tag is legal for DIGlobal.
198 bool DIDescriptor::isGlobal() const {
199 return isGlobalVariable();
202 /// isUnspecifiedParmeter - Return true if the specified tag is
203 /// DW_TAG_unspecified_parameters.
204 bool DIDescriptor::isUnspecifiedParameter() const {
205 return DbgNode && getTag() == dwarf::DW_TAG_unspecified_parameters;
208 /// isScope - Return true if the specified tag is one of the scope
209 /// related tag.
210 bool DIDescriptor::isScope() const {
211 if (!DbgNode) return false;
212 switch (getTag()) {
213 case dwarf::DW_TAG_compile_unit:
214 case dwarf::DW_TAG_lexical_block:
215 case dwarf::DW_TAG_subprogram:
216 case dwarf::DW_TAG_namespace:
217 return true;
218 default:
219 break;
221 return false;
224 /// isTemplateTypeParameter - Return true if the specified tag is
225 /// DW_TAG_template_type_parameter.
226 bool DIDescriptor::isTemplateTypeParameter() const {
227 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
230 /// isTemplateValueParameter - Return true if the specified tag is
231 /// DW_TAG_template_value_parameter.
232 bool DIDescriptor::isTemplateValueParameter() const {
233 return DbgNode && getTag() == dwarf::DW_TAG_template_value_parameter;
236 /// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
237 bool DIDescriptor::isCompileUnit() const {
238 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
241 /// isFile - Return true if the specified tag is DW_TAG_file_type.
242 bool DIDescriptor::isFile() const {
243 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
246 /// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
247 bool DIDescriptor::isNameSpace() const {
248 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
251 /// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
252 bool DIDescriptor::isLexicalBlock() const {
253 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block;
256 /// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
257 bool DIDescriptor::isSubrange() const {
258 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
261 /// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
262 bool DIDescriptor::isEnumerator() const {
263 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
266 //===----------------------------------------------------------------------===//
267 // Simple Descriptor Constructors and other Methods
268 //===----------------------------------------------------------------------===//
270 DIType::DIType(const MDNode *N) : DIScope(N) {
271 if (!N) return;
272 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
273 DbgNode = 0;
277 unsigned DIArray::getNumElements() const {
278 if (!DbgNode)
279 return 0;
280 return DbgNode->getNumOperands();
283 /// replaceAllUsesWith - Replace all uses of debug info referenced by
284 /// this descriptor.
285 void DIType::replaceAllUsesWith(DIDescriptor &D) {
286 if (!DbgNode)
287 return;
289 // Since we use a TrackingVH for the node, its easy for clients to manufacture
290 // legitimate situations where they want to replaceAllUsesWith() on something
291 // which, due to uniquing, has merged with the source. We shield clients from
292 // this detail by allowing a value to be replaced with replaceAllUsesWith()
293 // itself.
294 if (DbgNode != D) {
295 MDNode *Node = const_cast<MDNode*>(DbgNode);
296 const MDNode *DN = D;
297 const Value *V = cast_or_null<Value>(DN);
298 Node->replaceAllUsesWith(const_cast<Value*>(V));
299 MDNode::deleteTemporary(Node);
303 /// replaceAllUsesWith - Replace all uses of debug info referenced by
304 /// this descriptor.
305 void DIType::replaceAllUsesWith(MDNode *D) {
306 if (!DbgNode)
307 return;
309 // Since we use a TrackingVH for the node, its easy for clients to manufacture
310 // legitimate situations where they want to replaceAllUsesWith() on something
311 // which, due to uniquing, has merged with the source. We shield clients from
312 // this detail by allowing a value to be replaced with replaceAllUsesWith()
313 // itself.
314 if (DbgNode != D) {
315 MDNode *Node = const_cast<MDNode*>(DbgNode);
316 const MDNode *DN = D;
317 const Value *V = cast_or_null<Value>(DN);
318 Node->replaceAllUsesWith(const_cast<Value*>(V));
319 MDNode::deleteTemporary(Node);
323 /// Verify - Verify that a compile unit is well formed.
324 bool DICompileUnit::Verify() const {
325 if (!DbgNode)
326 return false;
327 StringRef N = getFilename();
328 if (N.empty())
329 return false;
330 // It is possible that directory and produce string is empty.
331 return true;
334 /// Verify - Verify that a type descriptor is well formed.
335 bool DIType::Verify() const {
336 if (!DbgNode)
337 return false;
338 if (!getContext().Verify())
339 return false;
340 unsigned Tag = getTag();
341 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
342 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
343 Tag != dwarf::DW_TAG_reference_type && Tag != dwarf::DW_TAG_restrict_type
344 && Tag != dwarf::DW_TAG_vector_type && Tag != dwarf::DW_TAG_array_type
345 && Tag != dwarf::DW_TAG_enumeration_type
346 && getFilename().empty())
347 return false;
348 return true;
351 /// Verify - Verify that a basic type descriptor is well formed.
352 bool DIBasicType::Verify() const {
353 return isBasicType();
356 /// Verify - Verify that a derived type descriptor is well formed.
357 bool DIDerivedType::Verify() const {
358 return isDerivedType();
361 /// Verify - Verify that a composite type descriptor is well formed.
362 bool DICompositeType::Verify() const {
363 if (!DbgNode)
364 return false;
365 if (!getContext().Verify())
366 return false;
368 DICompileUnit CU = getCompileUnit();
369 if (!CU.Verify())
370 return false;
371 return true;
374 /// Verify - Verify that a subprogram descriptor is well formed.
375 bool DISubprogram::Verify() const {
376 if (!DbgNode)
377 return false;
379 if (!getContext().Verify())
380 return false;
382 DICompileUnit CU = getCompileUnit();
383 if (!CU.Verify())
384 return false;
386 DICompositeType Ty = getType();
387 if (!Ty.Verify())
388 return false;
389 return true;
392 /// Verify - Verify that a global variable descriptor is well formed.
393 bool DIGlobalVariable::Verify() const {
394 if (!DbgNode)
395 return false;
397 if (getDisplayName().empty())
398 return false;
400 if (!getContext().Verify())
401 return false;
403 DICompileUnit CU = getCompileUnit();
404 if (!CU.Verify())
405 return false;
407 DIType Ty = getType();
408 if (!Ty.Verify())
409 return false;
411 if (!getGlobal() && !getConstant())
412 return false;
414 return true;
417 /// Verify - Verify that a variable descriptor is well formed.
418 bool DIVariable::Verify() const {
419 if (!DbgNode)
420 return false;
422 if (!getContext().Verify())
423 return false;
425 if (!getCompileUnit().Verify())
426 return false;
428 DIType Ty = getType();
429 if (!Ty.Verify())
430 return false;
432 return true;
435 /// Verify - Verify that a location descriptor is well formed.
436 bool DILocation::Verify() const {
437 if (!DbgNode)
438 return false;
440 return DbgNode->getNumOperands() == 4;
443 /// Verify - Verify that a namespace descriptor is well formed.
444 bool DINameSpace::Verify() const {
445 if (!DbgNode)
446 return false;
447 if (getName().empty())
448 return false;
449 if (!getCompileUnit().Verify())
450 return false;
451 return true;
454 /// getOriginalTypeSize - If this type is derived from a base type then
455 /// return base type size.
456 uint64_t DIDerivedType::getOriginalTypeSize() const {
457 unsigned Tag = getTag();
458 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
459 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
460 Tag == dwarf::DW_TAG_restrict_type) {
461 DIType BaseType = getTypeDerivedFrom();
462 // If this type is not derived from any type then take conservative
463 // approach.
464 if (!BaseType.isValid())
465 return getSizeInBits();
466 if (BaseType.isDerivedType())
467 return DIDerivedType(BaseType).getOriginalTypeSize();
468 else
469 return BaseType.getSizeInBits();
472 return getSizeInBits();
475 /// isInlinedFnArgument - Return true if this variable provides debugging
476 /// information for an inlined function arguments.
477 bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
478 assert(CurFn && "Invalid function");
479 if (!getContext().isSubprogram())
480 return false;
481 // This variable is not inlined function argument if its scope
482 // does not describe current function.
483 return !(DISubprogram(getContext()).describes(CurFn));
486 /// describes - Return true if this subprogram provides debugging
487 /// information for the function F.
488 bool DISubprogram::describes(const Function *F) {
489 assert(F && "Invalid function");
490 if (F == getFunction())
491 return true;
492 StringRef Name = getLinkageName();
493 if (Name.empty())
494 Name = getName();
495 if (F->getName() == Name)
496 return true;
497 return false;
500 unsigned DISubprogram::isOptimized() const {
501 assert (DbgNode && "Invalid subprogram descriptor!");
502 if (DbgNode->getNumOperands() == 16)
503 return getUnsignedField(15);
504 return 0;
507 StringRef DIScope::getFilename() const {
508 if (!DbgNode)
509 return StringRef();
510 if (isLexicalBlock())
511 return DILexicalBlock(DbgNode).getFilename();
512 if (isSubprogram())
513 return DISubprogram(DbgNode).getFilename();
514 if (isCompileUnit())
515 return DICompileUnit(DbgNode).getFilename();
516 if (isNameSpace())
517 return DINameSpace(DbgNode).getFilename();
518 if (isType())
519 return DIType(DbgNode).getFilename();
520 if (isFile())
521 return DIFile(DbgNode).getFilename();
522 assert(0 && "Invalid DIScope!");
523 return StringRef();
526 StringRef DIScope::getDirectory() const {
527 if (!DbgNode)
528 return StringRef();
529 if (isLexicalBlock())
530 return DILexicalBlock(DbgNode).getDirectory();
531 if (isSubprogram())
532 return DISubprogram(DbgNode).getDirectory();
533 if (isCompileUnit())
534 return DICompileUnit(DbgNode).getDirectory();
535 if (isNameSpace())
536 return DINameSpace(DbgNode).getDirectory();
537 if (isType())
538 return DIType(DbgNode).getDirectory();
539 if (isFile())
540 return DIFile(DbgNode).getDirectory();
541 assert(0 && "Invalid DIScope!");
542 return StringRef();
545 //===----------------------------------------------------------------------===//
546 // DIDescriptor: dump routines for all descriptors.
547 //===----------------------------------------------------------------------===//
550 /// print - Print descriptor.
551 void DIDescriptor::print(raw_ostream &OS) const {
552 OS << "[" << dwarf::TagString(getTag()) << "] ";
553 OS.write_hex((intptr_t) &*DbgNode) << ']';
556 /// print - Print compile unit.
557 void DICompileUnit::print(raw_ostream &OS) const {
558 if (getLanguage())
559 OS << " [" << dwarf::LanguageString(getLanguage()) << "] ";
561 OS << " [" << getDirectory() << "/" << getFilename() << "]";
564 /// print - Print type.
565 void DIType::print(raw_ostream &OS) const {
566 if (!DbgNode) return;
568 StringRef Res = getName();
569 if (!Res.empty())
570 OS << " [" << Res << "] ";
572 unsigned Tag = getTag();
573 OS << " [" << dwarf::TagString(Tag) << "] ";
575 // TODO : Print context
576 getCompileUnit().print(OS);
577 OS << " ["
578 << "line " << getLineNumber() << ", "
579 << getSizeInBits() << " bits, "
580 << getAlignInBits() << " bit alignment, "
581 << getOffsetInBits() << " bit offset"
582 << "] ";
584 if (isPrivate())
585 OS << " [private] ";
586 else if (isProtected())
587 OS << " [protected] ";
589 if (isForwardDecl())
590 OS << " [fwd] ";
592 if (isBasicType())
593 DIBasicType(DbgNode).print(OS);
594 else if (isDerivedType())
595 DIDerivedType(DbgNode).print(OS);
596 else if (isCompositeType())
597 DICompositeType(DbgNode).print(OS);
598 else {
599 OS << "Invalid DIType\n";
600 return;
603 OS << "\n";
606 /// print - Print basic type.
607 void DIBasicType::print(raw_ostream &OS) const {
608 OS << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
611 /// print - Print derived type.
612 void DIDerivedType::print(raw_ostream &OS) const {
613 OS << "\n\t Derived From: "; getTypeDerivedFrom().print(OS);
616 /// print - Print composite type.
617 void DICompositeType::print(raw_ostream &OS) const {
618 DIArray A = getTypeArray();
619 OS << " [" << A.getNumElements() << " elements]";
622 /// print - Print subprogram.
623 void DISubprogram::print(raw_ostream &OS) const {
624 StringRef Res = getName();
625 if (!Res.empty())
626 OS << " [" << Res << "] ";
628 unsigned Tag = getTag();
629 OS << " [" << dwarf::TagString(Tag) << "] ";
631 // TODO : Print context
632 getCompileUnit().print(OS);
633 OS << " [" << getLineNumber() << "] ";
635 if (isLocalToUnit())
636 OS << " [local] ";
638 if (isDefinition())
639 OS << " [def] ";
641 OS << "\n";
644 /// print - Print global variable.
645 void DIGlobalVariable::print(raw_ostream &OS) const {
646 OS << " [";
647 StringRef Res = getName();
648 if (!Res.empty())
649 OS << " [" << Res << "] ";
651 unsigned Tag = getTag();
652 OS << " [" << dwarf::TagString(Tag) << "] ";
654 // TODO : Print context
655 getCompileUnit().print(OS);
656 OS << " [" << getLineNumber() << "] ";
658 if (isLocalToUnit())
659 OS << " [local] ";
661 if (isDefinition())
662 OS << " [def] ";
664 if (isGlobalVariable())
665 DIGlobalVariable(DbgNode).print(OS);
666 OS << "]\n";
669 /// print - Print variable.
670 void DIVariable::print(raw_ostream &OS) const {
671 StringRef Res = getName();
672 if (!Res.empty())
673 OS << " [" << Res << "] ";
675 getCompileUnit().print(OS);
676 OS << " [" << getLineNumber() << "] ";
677 getType().print(OS);
678 OS << "\n";
680 // FIXME: Dump complex addresses
683 /// dump - Print descriptor to dbgs() with a newline.
684 void DIDescriptor::dump() const {
685 print(dbgs()); dbgs() << '\n';
688 /// dump - Print compile unit to dbgs() with a newline.
689 void DICompileUnit::dump() const {
690 print(dbgs()); dbgs() << '\n';
693 /// dump - Print type to dbgs() with a newline.
694 void DIType::dump() const {
695 print(dbgs()); dbgs() << '\n';
698 /// dump - Print basic type to dbgs() with a newline.
699 void DIBasicType::dump() const {
700 print(dbgs()); dbgs() << '\n';
703 /// dump - Print derived type to dbgs() with a newline.
704 void DIDerivedType::dump() const {
705 print(dbgs()); dbgs() << '\n';
708 /// dump - Print composite type to dbgs() with a newline.
709 void DICompositeType::dump() const {
710 print(dbgs()); dbgs() << '\n';
713 /// dump - Print subprogram to dbgs() with a newline.
714 void DISubprogram::dump() const {
715 print(dbgs()); dbgs() << '\n';
718 /// dump - Print global variable.
719 void DIGlobalVariable::dump() const {
720 print(dbgs()); dbgs() << '\n';
723 /// dump - Print variable.
724 void DIVariable::dump() const {
725 print(dbgs()); dbgs() << '\n';
728 /// fixupObjcLikeName - Replace contains special characters used
729 /// in a typical Objective-C names with '.' in a given string.
730 static void fixupObjcLikeName(std::string &Str) {
731 for (size_t i = 0, e = Str.size(); i < e; ++i) {
732 char C = Str[i];
733 if (C == '[' || C == ']' || C == ' ' || C == ':' || C == '+' ||
734 C == '(' || C == ')')
735 Str[i] = '.';
739 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is
740 /// suitable to hold function specific information.
741 NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, StringRef FuncName) {
742 if (FuncName.find('[') == StringRef::npos)
743 return M.getNamedMetadata(Twine("llvm.dbg.lv.", FuncName));
744 std::string Name = FuncName;
745 fixupObjcLikeName(Name);
746 return M.getNamedMetadata(Twine("llvm.dbg.lv.", Name));
749 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
750 /// to hold function specific information.
751 NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, StringRef FuncName) {
752 SmallString<32> Out;
753 if (FuncName.find('[') == StringRef::npos)
754 return M.getOrInsertNamedMetadata(Twine("llvm.dbg.lv.", FuncName)
755 .toStringRef(Out));
757 std::string Name = FuncName;
758 fixupObjcLikeName(Name);
759 return M.getOrInsertNamedMetadata(Twine("llvm.dbg.lv.", Name)
760 .toStringRef(Out));
764 //===----------------------------------------------------------------------===//
765 // DebugInfoFinder implementations.
766 //===----------------------------------------------------------------------===//
768 /// processModule - Process entire module and collect debug info.
769 void DebugInfoFinder::processModule(Module &M) {
770 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
771 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
772 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
773 ++BI) {
774 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
775 processDeclare(DDI);
777 DebugLoc Loc = BI->getDebugLoc();
778 if (Loc.isUnknown())
779 continue;
781 LLVMContext &Ctx = BI->getContext();
782 DIDescriptor Scope(Loc.getScope(Ctx));
784 if (Scope.isCompileUnit())
785 addCompileUnit(DICompileUnit(Scope));
786 else if (Scope.isSubprogram())
787 processSubprogram(DISubprogram(Scope));
788 else if (Scope.isLexicalBlock())
789 processLexicalBlock(DILexicalBlock(Scope));
791 if (MDNode *IA = Loc.getInlinedAt(Ctx))
792 processLocation(DILocation(IA));
795 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
796 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
797 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
798 if (addGlobalVariable(DIG)) {
799 addCompileUnit(DIG.getCompileUnit());
800 processType(DIG.getType());
805 if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
806 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
807 processSubprogram(DISubprogram(NMD->getOperand(i)));
810 /// processLocation - Process DILocation.
811 void DebugInfoFinder::processLocation(DILocation Loc) {
812 if (!Loc.Verify()) return;
813 DIDescriptor S(Loc.getScope());
814 if (S.isCompileUnit())
815 addCompileUnit(DICompileUnit(S));
816 else if (S.isSubprogram())
817 processSubprogram(DISubprogram(S));
818 else if (S.isLexicalBlock())
819 processLexicalBlock(DILexicalBlock(S));
820 processLocation(Loc.getOrigLocation());
823 /// processType - Process DIType.
824 void DebugInfoFinder::processType(DIType DT) {
825 if (!addType(DT))
826 return;
828 addCompileUnit(DT.getCompileUnit());
829 if (DT.isCompositeType()) {
830 DICompositeType DCT(DT);
831 processType(DCT.getTypeDerivedFrom());
832 DIArray DA = DCT.getTypeArray();
833 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
834 DIDescriptor D = DA.getElement(i);
835 if (D.isType())
836 processType(DIType(D));
837 else if (D.isSubprogram())
838 processSubprogram(DISubprogram(D));
840 } else if (DT.isDerivedType()) {
841 DIDerivedType DDT(DT);
842 processType(DDT.getTypeDerivedFrom());
846 /// processLexicalBlock
847 void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
848 DIScope Context = LB.getContext();
849 if (Context.isLexicalBlock())
850 return processLexicalBlock(DILexicalBlock(Context));
851 else
852 return processSubprogram(DISubprogram(Context));
855 /// processSubprogram - Process DISubprogram.
856 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
857 if (!addSubprogram(SP))
858 return;
859 addCompileUnit(SP.getCompileUnit());
860 processType(SP.getType());
863 /// processDeclare - Process DbgDeclareInst.
864 void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
865 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
866 if (!N) return;
868 DIDescriptor DV(N);
869 if (!DV.isVariable())
870 return;
872 if (!NodesSeen.insert(DV))
873 return;
875 addCompileUnit(DIVariable(N).getCompileUnit());
876 processType(DIVariable(N).getType());
879 /// addType - Add type into Tys.
880 bool DebugInfoFinder::addType(DIType DT) {
881 if (!DT.isValid())
882 return false;
884 if (!NodesSeen.insert(DT))
885 return false;
887 TYs.push_back(DT);
888 return true;
891 /// addCompileUnit - Add compile unit into CUs.
892 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
893 if (!CU.Verify())
894 return false;
896 if (!NodesSeen.insert(CU))
897 return false;
899 CUs.push_back(CU);
900 return true;
903 /// addGlobalVariable - Add global variable into GVs.
904 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
905 if (!DIDescriptor(DIG).isGlobalVariable())
906 return false;
908 if (!NodesSeen.insert(DIG))
909 return false;
911 GVs.push_back(DIG);
912 return true;
915 // addSubprogram - Add subprgoram into SPs.
916 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
917 if (!DIDescriptor(SP).isSubprogram())
918 return false;
920 if (!NodesSeen.insert(SP))
921 return false;
923 SPs.push_back(SP);
924 return true;
927 /// getDISubprogram - Find subprogram that is enclosing this scope.
928 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
929 DIDescriptor D(Scope);
930 if (D.isSubprogram())
931 return DISubprogram(Scope);
933 if (D.isLexicalBlock())
934 return getDISubprogram(DILexicalBlock(Scope).getContext());
936 return DISubprogram();
939 /// getDICompositeType - Find underlying composite type.
940 DICompositeType llvm::getDICompositeType(DIType T) {
941 if (T.isCompositeType())
942 return DICompositeType(T);
944 if (T.isDerivedType())
945 return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
947 return DICompositeType();