Temporarily revert r135528 which distinguishes between two copies of one
[oota-llvm.git] / lib / Analysis / 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/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;
31
32 //===----------------------------------------------------------------------===//
33 // DIDescriptor
34 //===----------------------------------------------------------------------===//
35
36 DIDescriptor::DIDescriptor(const DIFile F) : DbgNode(F.DbgNode) {
37 }
38
39 DIDescriptor::DIDescriptor(const DISubprogram F) : DbgNode(F.DbgNode) {
40 }
41
42 DIDescriptor::DIDescriptor(const DILexicalBlock F) : DbgNode(F.DbgNode) {
43 }
44
45 DIDescriptor::DIDescriptor(const DIVariable F) : DbgNode(F.DbgNode) {
46 }
47
48 DIDescriptor::DIDescriptor(const DIType F) : DbgNode(F.DbgNode) {
49 }
50
51 StringRef
52 DIDescriptor::getStringField(unsigned Elt) const {
53   if (DbgNode == 0)
54     return StringRef();
55
56   if (Elt < DbgNode->getNumOperands())
57     if (MDString *MDS = dyn_cast_or_null<MDString>(DbgNode->getOperand(Elt)))
58       return MDS->getString();
59
60   return StringRef();
61 }
62
63 uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
64   if (DbgNode == 0)
65     return 0;
66
67   if (Elt < DbgNode->getNumOperands())
68     if (ConstantInt *CI = dyn_cast<ConstantInt>(DbgNode->getOperand(Elt)))
69       return CI->getZExtValue();
70
71   return 0;
72 }
73
74 DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
75   if (DbgNode == 0)
76     return DIDescriptor();
77
78   if (Elt < DbgNode->getNumOperands())
79     return
80       DIDescriptor(dyn_cast_or_null<const MDNode>(DbgNode->getOperand(Elt)));
81   return DIDescriptor();
82 }
83
84 GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
85   if (DbgNode == 0)
86     return 0;
87
88   if (Elt < DbgNode->getNumOperands())
89       return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt));
90   return 0;
91 }
92
93 Constant *DIDescriptor::getConstantField(unsigned Elt) const {
94   if (DbgNode == 0)
95     return 0;
96
97   if (Elt < DbgNode->getNumOperands())
98       return dyn_cast_or_null<Constant>(DbgNode->getOperand(Elt));
99   return 0;
100 }
101
102 Function *DIDescriptor::getFunctionField(unsigned Elt) const {
103   if (DbgNode == 0)
104     return 0;
105
106   if (Elt < DbgNode->getNumOperands())
107       return dyn_cast_or_null<Function>(DbgNode->getOperand(Elt));
108   return 0;
109 }
110
111 unsigned DIVariable::getNumAddrElements() const {
112   if (getVersion() <= llvm::LLVMDebugVersion8)
113     return DbgNode->getNumOperands()-6;
114   if (getVersion() == llvm::LLVMDebugVersion9)
115     return DbgNode->getNumOperands()-7;
116   return DbgNode->getNumOperands()-8;
117 }
118
119 /// getInlinedAt - If this variable is inlined then return inline location.
120 MDNode *DIVariable::getInlinedAt() {
121   if (getVersion() <= llvm::LLVMDebugVersion9)
122     return NULL;
123   return dyn_cast_or_null<MDNode>(DbgNode->getOperand(7));
124 }
125
126 //===----------------------------------------------------------------------===//
127 // Predicates
128 //===----------------------------------------------------------------------===//
129
130 /// isBasicType - Return true if the specified tag is legal for
131 /// DIBasicType.
132 bool DIDescriptor::isBasicType() const {
133   return DbgNode && getTag() == dwarf::DW_TAG_base_type;
134 }
135
136 /// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
137 bool DIDescriptor::isDerivedType() const {
138   if (!DbgNode) return false;
139   switch (getTag()) {
140   case dwarf::DW_TAG_typedef:
141   case dwarf::DW_TAG_pointer_type:
142   case dwarf::DW_TAG_reference_type:
143   case dwarf::DW_TAG_const_type:
144   case dwarf::DW_TAG_volatile_type:
145   case dwarf::DW_TAG_restrict_type:
146   case dwarf::DW_TAG_member:
147   case dwarf::DW_TAG_inheritance:
148   case dwarf::DW_TAG_friend:
149     return true;
150   default:
151     // CompositeTypes are currently modelled as DerivedTypes.
152     return isCompositeType();
153   }
154 }
155
156 /// isCompositeType - Return true if the specified tag is legal for
157 /// DICompositeType.
158 bool DIDescriptor::isCompositeType() const {
159   if (!DbgNode) return false;
160   switch (getTag()) {
161   case dwarf::DW_TAG_array_type:
162   case dwarf::DW_TAG_structure_type:
163   case dwarf::DW_TAG_union_type:
164   case dwarf::DW_TAG_enumeration_type:
165   case dwarf::DW_TAG_vector_type:
166   case dwarf::DW_TAG_subroutine_type:
167   case dwarf::DW_TAG_class_type:
168     return true;
169   default:
170     return false;
171   }
172 }
173
174 /// isVariable - Return true if the specified tag is legal for DIVariable.
175 bool DIDescriptor::isVariable() const {
176   if (!DbgNode) return false;
177   switch (getTag()) {
178   case dwarf::DW_TAG_auto_variable:
179   case dwarf::DW_TAG_arg_variable:
180   case dwarf::DW_TAG_return_variable:
181     return true;
182   default:
183     return false;
184   }
185 }
186
187 /// isType - Return true if the specified tag is legal for DIType.
188 bool DIDescriptor::isType() const {
189   return isBasicType() || isCompositeType() || isDerivedType();
190 }
191
192 /// isSubprogram - Return true if the specified tag is legal for
193 /// DISubprogram.
194 bool DIDescriptor::isSubprogram() const {
195   return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
196 }
197
198 /// isGlobalVariable - Return true if the specified tag is legal for
199 /// DIGlobalVariable.
200 bool DIDescriptor::isGlobalVariable() const {
201   return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
202                      getTag() == dwarf::DW_TAG_constant);
203 }
204
205 /// isGlobal - Return true if the specified tag is legal for DIGlobal.
206 bool DIDescriptor::isGlobal() const {
207   return isGlobalVariable();
208 }
209
210 /// isUnspecifiedParmeter - Return true if the specified tag is
211 /// DW_TAG_unspecified_parameters.
212 bool DIDescriptor::isUnspecifiedParameter() const {
213   return DbgNode && getTag() == dwarf::DW_TAG_unspecified_parameters;
214 }
215
216 /// isScope - Return true if the specified tag is one of the scope
217 /// related tag.
218 bool DIDescriptor::isScope() const {
219   if (!DbgNode) return false;
220   switch (getTag()) {
221   case dwarf::DW_TAG_compile_unit:
222   case dwarf::DW_TAG_lexical_block:
223   case dwarf::DW_TAG_subprogram:
224   case dwarf::DW_TAG_namespace:
225     return true;
226   default:
227     break;
228   }
229   return false;
230 }
231
232 /// isTemplateTypeParameter - Return true if the specified tag is
233 /// DW_TAG_template_type_parameter.
234 bool DIDescriptor::isTemplateTypeParameter() const {
235   return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
236 }
237
238 /// isTemplateValueParameter - Return true if the specified tag is
239 /// DW_TAG_template_value_parameter.
240 bool DIDescriptor::isTemplateValueParameter() const {
241   return DbgNode && getTag() == dwarf::DW_TAG_template_value_parameter;
242 }
243
244 /// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
245 bool DIDescriptor::isCompileUnit() const {
246   return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
247 }
248
249 /// isFile - Return true if the specified tag is DW_TAG_file_type.
250 bool DIDescriptor::isFile() const {
251   return DbgNode && getTag() == dwarf::DW_TAG_file_type;
252 }
253
254 /// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
255 bool DIDescriptor::isNameSpace() const {
256   return DbgNode && getTag() == dwarf::DW_TAG_namespace;
257 }
258
259 /// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
260 bool DIDescriptor::isLexicalBlock() const {
261   return DbgNode && getTag() == dwarf::DW_TAG_lexical_block;
262 }
263
264 /// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
265 bool DIDescriptor::isSubrange() const {
266   return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
267 }
268
269 /// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
270 bool DIDescriptor::isEnumerator() const {
271   return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
272 }
273
274 //===----------------------------------------------------------------------===//
275 // Simple Descriptor Constructors and other Methods
276 //===----------------------------------------------------------------------===//
277
278 DIType::DIType(const MDNode *N) : DIScope(N) {
279   if (!N) return;
280   if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
281     DbgNode = 0;
282   }
283 }
284
285 unsigned DIArray::getNumElements() const {
286   if (!DbgNode)
287     return 0;
288   return DbgNode->getNumOperands();
289 }
290
291 /// replaceAllUsesWith - Replace all uses of debug info referenced by
292 /// this descriptor.
293 void DIType::replaceAllUsesWith(DIDescriptor &D) {
294   if (!DbgNode)
295     return;
296
297   // Since we use a TrackingVH for the node, its easy for clients to manufacture
298   // legitimate situations where they want to replaceAllUsesWith() on something
299   // which, due to uniquing, has merged with the source. We shield clients from
300   // this detail by allowing a value to be replaced with replaceAllUsesWith()
301   // itself.
302   if (DbgNode != D) {
303     MDNode *Node = const_cast<MDNode*>(DbgNode);
304     const MDNode *DN = D;
305     const Value *V = cast_or_null<Value>(DN);
306     Node->replaceAllUsesWith(const_cast<Value*>(V));
307     MDNode::deleteTemporary(Node);
308   }
309 }
310
311 /// replaceAllUsesWith - Replace all uses of debug info referenced by
312 /// this descriptor.
313 void DIType::replaceAllUsesWith(MDNode *D) {
314   if (!DbgNode)
315     return;
316
317   // Since we use a TrackingVH for the node, its easy for clients to manufacture
318   // legitimate situations where they want to replaceAllUsesWith() on something
319   // which, due to uniquing, has merged with the source. We shield clients from
320   // this detail by allowing a value to be replaced with replaceAllUsesWith()
321   // itself.
322   if (DbgNode != D) {
323     MDNode *Node = const_cast<MDNode*>(DbgNode);
324     const MDNode *DN = D;
325     const Value *V = cast_or_null<Value>(DN);
326     Node->replaceAllUsesWith(const_cast<Value*>(V));
327     MDNode::deleteTemporary(Node);
328   }
329 }
330
331 /// Verify - Verify that a compile unit is well formed.
332 bool DICompileUnit::Verify() const {
333   if (!DbgNode)
334     return false;
335   StringRef N = getFilename();
336   if (N.empty())
337     return false;
338   // It is possible that directory and produce string is empty.
339   return true;
340 }
341
342 /// Verify - Verify that a type descriptor is well formed.
343 bool DIType::Verify() const {
344   if (!DbgNode)
345     return false;
346   if (!getContext().Verify())
347     return false;
348   unsigned Tag = getTag();
349   if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
350       Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
351       Tag != dwarf::DW_TAG_reference_type && Tag != dwarf::DW_TAG_restrict_type 
352       && Tag != dwarf::DW_TAG_vector_type && Tag != dwarf::DW_TAG_array_type
353       && Tag != dwarf::DW_TAG_enumeration_type 
354       && getFilename().empty())
355     return false;
356   return true;
357 }
358
359 /// Verify - Verify that a basic type descriptor is well formed.
360 bool DIBasicType::Verify() const {
361   return isBasicType();
362 }
363
364 /// Verify - Verify that a derived type descriptor is well formed.
365 bool DIDerivedType::Verify() const {
366   return isDerivedType();
367 }
368
369 /// Verify - Verify that a composite type descriptor is well formed.
370 bool DICompositeType::Verify() const {
371   if (!DbgNode)
372     return false;
373   if (!getContext().Verify())
374     return false;
375
376   DICompileUnit CU = getCompileUnit();
377   if (!CU.Verify())
378     return false;
379   return true;
380 }
381
382 /// Verify - Verify that a subprogram descriptor is well formed.
383 bool DISubprogram::Verify() const {
384   if (!DbgNode)
385     return false;
386
387   if (!getContext().Verify())
388     return false;
389
390   DICompileUnit CU = getCompileUnit();
391   if (!CU.Verify())
392     return false;
393
394   DICompositeType Ty = getType();
395   if (!Ty.Verify())
396     return false;
397   return true;
398 }
399
400 /// Verify - Verify that a global variable descriptor is well formed.
401 bool DIGlobalVariable::Verify() const {
402   if (!DbgNode)
403     return false;
404
405   if (getDisplayName().empty())
406     return false;
407
408   if (!getContext().Verify())
409     return false;
410
411   DICompileUnit CU = getCompileUnit();
412   if (!CU.Verify())
413     return false;
414
415   DIType Ty = getType();
416   if (!Ty.Verify())
417     return false;
418
419   if (!getGlobal() && !getConstant())
420     return false;
421
422   return true;
423 }
424
425 /// Verify - Verify that a variable descriptor is well formed.
426 bool DIVariable::Verify() const {
427   if (!DbgNode)
428     return false;
429
430   if (!getContext().Verify())
431     return false;
432
433   if (!getCompileUnit().Verify())
434     return false;
435
436   DIType Ty = getType();
437   if (!Ty.Verify())
438     return false;
439
440   return true;
441 }
442
443 /// Verify - Verify that a location descriptor is well formed.
444 bool DILocation::Verify() const {
445   if (!DbgNode)
446     return false;
447
448   return DbgNode->getNumOperands() == 4;
449 }
450
451 /// Verify - Verify that a namespace descriptor is well formed.
452 bool DINameSpace::Verify() const {
453   if (!DbgNode)
454     return false;
455   if (getName().empty())
456     return false;
457   if (!getCompileUnit().Verify())
458     return false;
459   return true;
460 }
461
462 /// getOriginalTypeSize - If this type is derived from a base type then
463 /// return base type size.
464 uint64_t DIDerivedType::getOriginalTypeSize() const {
465   unsigned Tag = getTag();
466   if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
467       Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
468       Tag == dwarf::DW_TAG_restrict_type) {
469     DIType BaseType = getTypeDerivedFrom();
470     // If this type is not derived from any type then take conservative
471     // approach.
472     if (!BaseType.isValid())
473       return getSizeInBits();
474     if (BaseType.isDerivedType())
475       return DIDerivedType(BaseType).getOriginalTypeSize();
476     else
477       return BaseType.getSizeInBits();
478   }
479
480   return getSizeInBits();
481 }
482
483 /// isInlinedFnArgument - Return true if this variable provides debugging
484 /// information for an inlined function arguments.
485 bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
486   assert(CurFn && "Invalid function");
487   if (!getContext().isSubprogram())
488     return false;
489   // This variable is not inlined function argument if its scope
490   // does not describe current function.
491   return !(DISubprogram(getContext()).describes(CurFn));
492 }
493
494 /// describes - Return true if this subprogram provides debugging
495 /// information for the function F.
496 bool DISubprogram::describes(const Function *F) {
497   assert(F && "Invalid function");
498   if (F == getFunction())
499     return true;
500   StringRef Name = getLinkageName();
501   if (Name.empty())
502     Name = getName();
503   if (F->getName() == Name)
504     return true;
505   return false;
506 }
507
508 unsigned DISubprogram::isOptimized() const {
509   assert (DbgNode && "Invalid subprogram descriptor!");
510   if (DbgNode->getNumOperands() == 16)
511     return getUnsignedField(15);
512   return 0;
513 }
514
515 StringRef DIScope::getFilename() const {
516   if (!DbgNode)
517     return StringRef();
518   if (isLexicalBlock())
519     return DILexicalBlock(DbgNode).getFilename();
520   if (isSubprogram())
521     return DISubprogram(DbgNode).getFilename();
522   if (isCompileUnit())
523     return DICompileUnit(DbgNode).getFilename();
524   if (isNameSpace())
525     return DINameSpace(DbgNode).getFilename();
526   if (isType())
527     return DIType(DbgNode).getFilename();
528   if (isFile())
529     return DIFile(DbgNode).getFilename();
530   assert(0 && "Invalid DIScope!");
531   return StringRef();
532 }
533
534 StringRef DIScope::getDirectory() const {
535   if (!DbgNode)
536     return StringRef();
537   if (isLexicalBlock())
538     return DILexicalBlock(DbgNode).getDirectory();
539   if (isSubprogram())
540     return DISubprogram(DbgNode).getDirectory();
541   if (isCompileUnit())
542     return DICompileUnit(DbgNode).getDirectory();
543   if (isNameSpace())
544     return DINameSpace(DbgNode).getDirectory();
545   if (isType())
546     return DIType(DbgNode).getDirectory();
547   if (isFile())
548     return DIFile(DbgNode).getDirectory();
549   assert(0 && "Invalid DIScope!");
550   return StringRef();
551 }
552
553 //===----------------------------------------------------------------------===//
554 // DIDescriptor: dump routines for all descriptors.
555 //===----------------------------------------------------------------------===//
556
557
558 /// print - Print descriptor.
559 void DIDescriptor::print(raw_ostream &OS) const {
560   OS << "[" << dwarf::TagString(getTag()) << "] ";
561   OS.write_hex((intptr_t) &*DbgNode) << ']';
562 }
563
564 /// print - Print compile unit.
565 void DICompileUnit::print(raw_ostream &OS) const {
566   if (getLanguage())
567     OS << " [" << dwarf::LanguageString(getLanguage()) << "] ";
568
569   OS << " [" << getDirectory() << "/" << getFilename() << "]";
570 }
571
572 /// print - Print type.
573 void DIType::print(raw_ostream &OS) const {
574   if (!DbgNode) return;
575
576   StringRef Res = getName();
577   if (!Res.empty())
578     OS << " [" << Res << "] ";
579
580   unsigned Tag = getTag();
581   OS << " [" << dwarf::TagString(Tag) << "] ";
582
583   // TODO : Print context
584   getCompileUnit().print(OS);
585   OS << " ["
586          << "line " << getLineNumber() << ", "
587          << getSizeInBits() << " bits, "
588          << getAlignInBits() << " bit alignment, "
589          << getOffsetInBits() << " bit offset"
590          << "] ";
591
592   if (isPrivate())
593     OS << " [private] ";
594   else if (isProtected())
595     OS << " [protected] ";
596
597   if (isForwardDecl())
598     OS << " [fwd] ";
599
600   if (isBasicType())
601     DIBasicType(DbgNode).print(OS);
602   else if (isDerivedType())
603     DIDerivedType(DbgNode).print(OS);
604   else if (isCompositeType())
605     DICompositeType(DbgNode).print(OS);
606   else {
607     OS << "Invalid DIType\n";
608     return;
609   }
610
611   OS << "\n";
612 }
613
614 /// print - Print basic type.
615 void DIBasicType::print(raw_ostream &OS) const {
616   OS << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
617 }
618
619 /// print - Print derived type.
620 void DIDerivedType::print(raw_ostream &OS) const {
621   OS << "\n\t Derived From: "; getTypeDerivedFrom().print(OS);
622 }
623
624 /// print - Print composite type.
625 void DICompositeType::print(raw_ostream &OS) const {
626   DIArray A = getTypeArray();
627   OS << " [" << A.getNumElements() << " elements]";
628 }
629
630 /// print - Print subprogram.
631 void DISubprogram::print(raw_ostream &OS) const {
632   StringRef Res = getName();
633   if (!Res.empty())
634     OS << " [" << Res << "] ";
635
636   unsigned Tag = getTag();
637   OS << " [" << dwarf::TagString(Tag) << "] ";
638
639   // TODO : Print context
640   getCompileUnit().print(OS);
641   OS << " [" << getLineNumber() << "] ";
642
643   if (isLocalToUnit())
644     OS << " [local] ";
645
646   if (isDefinition())
647     OS << " [def] ";
648
649   OS << "\n";
650 }
651
652 /// print - Print global variable.
653 void DIGlobalVariable::print(raw_ostream &OS) const {
654   OS << " [";
655   StringRef Res = getName();
656   if (!Res.empty())
657     OS << " [" << Res << "] ";
658
659   unsigned Tag = getTag();
660   OS << " [" << dwarf::TagString(Tag) << "] ";
661
662   // TODO : Print context
663   getCompileUnit().print(OS);
664   OS << " [" << getLineNumber() << "] ";
665
666   if (isLocalToUnit())
667     OS << " [local] ";
668
669   if (isDefinition())
670     OS << " [def] ";
671
672   if (isGlobalVariable())
673     DIGlobalVariable(DbgNode).print(OS);
674   OS << "]\n";
675 }
676
677 /// print - Print variable.
678 void DIVariable::print(raw_ostream &OS) const {
679   StringRef Res = getName();
680   if (!Res.empty())
681     OS << " [" << Res << "] ";
682
683   getCompileUnit().print(OS);
684   OS << " [" << getLineNumber() << "] ";
685   getType().print(OS);
686   OS << "\n";
687
688   // FIXME: Dump complex addresses
689 }
690
691 /// dump - Print descriptor to dbgs() with a newline.
692 void DIDescriptor::dump() const {
693   print(dbgs()); dbgs() << '\n';
694 }
695
696 /// dump - Print compile unit to dbgs() with a newline.
697 void DICompileUnit::dump() const {
698   print(dbgs()); dbgs() << '\n';
699 }
700
701 /// dump - Print type to dbgs() with a newline.
702 void DIType::dump() const {
703   print(dbgs()); dbgs() << '\n';
704 }
705
706 /// dump - Print basic type to dbgs() with a newline.
707 void DIBasicType::dump() const {
708   print(dbgs()); dbgs() << '\n';
709 }
710
711 /// dump - Print derived type to dbgs() with a newline.
712 void DIDerivedType::dump() const {
713   print(dbgs()); dbgs() << '\n';
714 }
715
716 /// dump - Print composite type to dbgs() with a newline.
717 void DICompositeType::dump() const {
718   print(dbgs()); dbgs() << '\n';
719 }
720
721 /// dump - Print subprogram to dbgs() with a newline.
722 void DISubprogram::dump() const {
723   print(dbgs()); dbgs() << '\n';
724 }
725
726 /// dump - Print global variable.
727 void DIGlobalVariable::dump() const {
728   print(dbgs()); dbgs() << '\n';
729 }
730
731 /// dump - Print variable.
732 void DIVariable::dump() const {
733   print(dbgs()); dbgs() << '\n';
734 }
735
736 /// fixupObjcLikeName - Replace contains special characters used
737 /// in a typical Objective-C names with '.' in a given string.
738 static void fixupObjcLikeName(StringRef Str, SmallVectorImpl<char> &Out) {
739   bool isObjCLike = false;
740   for (size_t i = 0, e = Str.size(); i < e; ++i) {
741     char C = Str[i];
742     if (C == '[')
743       isObjCLike = true;
744
745     if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' ||
746                        C == '+' || C == '(' || C == ')'))
747       Out.push_back('.');
748     else
749       Out.push_back(C);
750   }
751 }
752
753 /// getFnSpecificMDNode - Return a NameMDNode, if available, that is 
754 /// suitable to hold function specific information.
755 NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, StringRef FuncName) {
756   SmallString<32> Name = StringRef("llvm.dbg.lv.");
757   fixupObjcLikeName(FuncName, Name);
758
759   return M.getNamedMetadata(Name.str());
760 }
761
762 /// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
763 /// to hold function specific information.
764 NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, StringRef FuncName) {
765   SmallString<32> Name = StringRef("llvm.dbg.lv.");
766   fixupObjcLikeName(FuncName, Name);
767
768   return M.getOrInsertNamedMetadata(Name.str());
769 }
770
771 /// createInlinedVariable - Create a new inlined variable based on current
772 /// variable.
773 /// @param DV            Current Variable.
774 /// @param InlinedScope  Location at current variable is inlined.
775 DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
776                                        LLVMContext &VMContext) {
777   SmallVector<Value *, 16> Elts;
778   // Insert inlined scope as 7th element.
779   for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
780     i == 7 ? Elts.push_back(InlinedScope) :
781              Elts.push_back(DV->getOperand(i));
782   return DIVariable(MDNode::get(VMContext, Elts));
783 }
784
785 //===----------------------------------------------------------------------===//
786 // DebugInfoFinder implementations.
787 //===----------------------------------------------------------------------===//
788
789 /// processModule - Process entire module and collect debug info.
790 void DebugInfoFinder::processModule(Module &M) {
791   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
792     for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
793       for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
794            ++BI) {
795         if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
796           processDeclare(DDI);
797
798         DebugLoc Loc = BI->getDebugLoc();
799         if (Loc.isUnknown())
800           continue;
801
802         LLVMContext &Ctx = BI->getContext();
803         DIDescriptor Scope(Loc.getScope(Ctx));
804
805         if (Scope.isCompileUnit())
806           addCompileUnit(DICompileUnit(Scope));
807         else if (Scope.isSubprogram())
808           processSubprogram(DISubprogram(Scope));
809         else if (Scope.isLexicalBlock())
810           processLexicalBlock(DILexicalBlock(Scope));
811
812         if (MDNode *IA = Loc.getInlinedAt(Ctx))
813           processLocation(DILocation(IA));
814       }
815
816   if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv")) {
817     for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
818       DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
819       if (addGlobalVariable(DIG)) {
820         addCompileUnit(DIG.getCompileUnit());
821         processType(DIG.getType());
822       }
823     }
824   }
825
826   if (NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.sp"))
827     for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i)
828       processSubprogram(DISubprogram(NMD->getOperand(i)));
829 }
830
831 /// processLocation - Process DILocation.
832 void DebugInfoFinder::processLocation(DILocation Loc) {
833   if (!Loc.Verify()) return;
834   DIDescriptor S(Loc.getScope());
835   if (S.isCompileUnit())
836     addCompileUnit(DICompileUnit(S));
837   else if (S.isSubprogram())
838     processSubprogram(DISubprogram(S));
839   else if (S.isLexicalBlock())
840     processLexicalBlock(DILexicalBlock(S));
841   processLocation(Loc.getOrigLocation());
842 }
843
844 /// processType - Process DIType.
845 void DebugInfoFinder::processType(DIType DT) {
846   if (!addType(DT))
847     return;
848
849   addCompileUnit(DT.getCompileUnit());
850   if (DT.isCompositeType()) {
851     DICompositeType DCT(DT);
852     processType(DCT.getTypeDerivedFrom());
853     DIArray DA = DCT.getTypeArray();
854     for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
855       DIDescriptor D = DA.getElement(i);
856       if (D.isType())
857         processType(DIType(D));
858       else if (D.isSubprogram())
859         processSubprogram(DISubprogram(D));
860     }
861   } else if (DT.isDerivedType()) {
862     DIDerivedType DDT(DT);
863     processType(DDT.getTypeDerivedFrom());
864   }
865 }
866
867 /// processLexicalBlock
868 void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
869   DIScope Context = LB.getContext();
870   if (Context.isLexicalBlock())
871     return processLexicalBlock(DILexicalBlock(Context));
872   else
873     return processSubprogram(DISubprogram(Context));
874 }
875
876 /// processSubprogram - Process DISubprogram.
877 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
878   if (!addSubprogram(SP))
879     return;
880   addCompileUnit(SP.getCompileUnit());
881   processType(SP.getType());
882 }
883
884 /// processDeclare - Process DbgDeclareInst.
885 void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
886   MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
887   if (!N) return;
888
889   DIDescriptor DV(N);
890   if (!DV.isVariable())
891     return;
892
893   if (!NodesSeen.insert(DV))
894     return;
895
896   addCompileUnit(DIVariable(N).getCompileUnit());
897   processType(DIVariable(N).getType());
898 }
899
900 /// addType - Add type into Tys.
901 bool DebugInfoFinder::addType(DIType DT) {
902   if (!DT.isValid())
903     return false;
904
905   if (!NodesSeen.insert(DT))
906     return false;
907
908   TYs.push_back(DT);
909   return true;
910 }
911
912 /// addCompileUnit - Add compile unit into CUs.
913 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
914   if (!CU.Verify())
915     return false;
916
917   if (!NodesSeen.insert(CU))
918     return false;
919
920   CUs.push_back(CU);
921   return true;
922 }
923
924 /// addGlobalVariable - Add global variable into GVs.
925 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
926   if (!DIDescriptor(DIG).isGlobalVariable())
927     return false;
928
929   if (!NodesSeen.insert(DIG))
930     return false;
931
932   GVs.push_back(DIG);
933   return true;
934 }
935
936 // addSubprogram - Add subprgoram into SPs.
937 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
938   if (!DIDescriptor(SP).isSubprogram())
939     return false;
940
941   if (!NodesSeen.insert(SP))
942     return false;
943
944   SPs.push_back(SP);
945   return true;
946 }
947
948 /// getDISubprogram - Find subprogram that is enclosing this scope.
949 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
950   DIDescriptor D(Scope);
951   if (D.isSubprogram())
952     return DISubprogram(Scope);
953
954   if (D.isLexicalBlock())
955     return getDISubprogram(DILexicalBlock(Scope).getContext());
956
957   return DISubprogram();
958 }
959
960 /// getDICompositeType - Find underlying composite type.
961 DICompositeType llvm::getDICompositeType(DIType T) {
962   if (T.isCompositeType())
963     return DICompositeType(T);
964
965   if (T.isDerivedType())
966     return getDICompositeType(DIDerivedType(T).getTypeDerivedFrom());
967
968   return DICompositeType();
969 }