db5a0cbb21476f034f58d972ec9d287de6366f77
[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/LLVMContext.h"
22 #include "llvm/Module.h"
23 #include "llvm/Analysis/ValueTracking.h"
24 #include "llvm/ADT/SmallPtrSet.h"
25 #include "llvm/Support/Dwarf.h"
26 #include "llvm/Support/DebugLoc.h"
27 #include "llvm/Support/raw_ostream.h"
28 using namespace llvm;
29 using namespace llvm::dwarf;
30
31 //===----------------------------------------------------------------------===//
32 // DIDescriptor
33 //===----------------------------------------------------------------------===//
34
35 /// ValidDebugInfo - Return true if V represents valid debug info value.
36 /// FIXME : Add DIDescriptor.isValid()
37 bool DIDescriptor::ValidDebugInfo(MDNode *N, CodeGenOpt::Level OptLevel) {
38   if (!N)
39     return false;
40
41   DIDescriptor DI(N);
42
43   // Check current version. Allow Version6 for now.
44   unsigned Version = DI.getVersion();
45   if (Version != LLVMDebugVersion && Version != LLVMDebugVersion6)
46     return false;
47
48   unsigned Tag = DI.getTag();
49   switch (Tag) {
50   case DW_TAG_variable:
51     assert(DIVariable(N).Verify() && "Invalid DebugInfo value");
52     break;
53   case DW_TAG_compile_unit:
54     assert(DICompileUnit(N).Verify() && "Invalid DebugInfo value");
55     break;
56   case DW_TAG_subprogram:
57     assert(DISubprogram(N).Verify() && "Invalid DebugInfo value");
58     break;
59   case DW_TAG_lexical_block:
60     // FIXME: This interfers with the quality of generated code during
61     // optimization.
62     if (OptLevel != CodeGenOpt::None)
63       return false;
64     // FALLTHROUGH
65   default:
66     break;
67   }
68
69   return true;
70 }
71
72 DIDescriptor::DIDescriptor(MDNode *N, unsigned RequiredTag) {
73   DbgNode = N;
74
75   // If this is non-null, check to see if the Tag matches. If not, set to null.
76   if (N && getTag() != RequiredTag) {
77     DbgNode = 0;
78   }
79 }
80
81 const char *
82 DIDescriptor::getStringField(unsigned Elt) const {
83   if (DbgNode == 0)
84     return NULL;
85
86   if (Elt < DbgNode->getNumElements())
87     if (MDString *MDS = dyn_cast_or_null<MDString>(DbgNode->getElement(Elt)))
88       return MDS->getString().data();
89
90   return NULL;
91 }
92
93 uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
94   if (DbgNode == 0)
95     return 0;
96
97   if (Elt < DbgNode->getNumElements())
98     if (ConstantInt *CI = dyn_cast<ConstantInt>(DbgNode->getElement(Elt)))
99       return CI->getZExtValue();
100
101   return 0;
102 }
103
104 DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
105   if (DbgNode == 0)
106     return DIDescriptor();
107
108   if (Elt < DbgNode->getNumElements() && DbgNode->getElement(Elt))
109     return DIDescriptor(dyn_cast<MDNode>(DbgNode->getElement(Elt)));
110
111   return DIDescriptor();
112 }
113
114 GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
115   if (DbgNode == 0)
116     return 0;
117
118   if (Elt < DbgNode->getNumElements())
119       return dyn_cast<GlobalVariable>(DbgNode->getElement(Elt));
120   return 0;
121 }
122
123 //===----------------------------------------------------------------------===//
124 // Predicates
125 //===----------------------------------------------------------------------===//
126
127 /// isBasicType - Return true if the specified tag is legal for
128 /// DIBasicType.
129 bool DIDescriptor::isBasicType() const {
130   assert (!isNull() && "Invalid descriptor!");
131   unsigned Tag = getTag();
132
133   return Tag == 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   assert (!isNull() && "Invalid descriptor!");
139   unsigned Tag = getTag();
140
141   switch (Tag) {
142   case dwarf::DW_TAG_typedef:
143   case dwarf::DW_TAG_pointer_type:
144   case dwarf::DW_TAG_reference_type:
145   case dwarf::DW_TAG_const_type:
146   case dwarf::DW_TAG_volatile_type:
147   case dwarf::DW_TAG_restrict_type:
148   case dwarf::DW_TAG_member:
149   case dwarf::DW_TAG_inheritance:
150     return true;
151   default:
152     // CompositeTypes are currently modelled as DerivedTypes.
153     return isCompositeType();
154   }
155 }
156
157 /// isCompositeType - Return true if the specified tag is legal for
158 /// DICompositeType.
159 bool DIDescriptor::isCompositeType() const {
160   assert (!isNull() && "Invalid descriptor!");
161   unsigned Tag = getTag();
162
163   switch (Tag) {
164   case dwarf::DW_TAG_array_type:
165   case dwarf::DW_TAG_structure_type:
166   case dwarf::DW_TAG_union_type:
167   case dwarf::DW_TAG_enumeration_type:
168   case dwarf::DW_TAG_vector_type:
169   case dwarf::DW_TAG_subroutine_type:
170   case dwarf::DW_TAG_class_type:
171     return true;
172   default:
173     return false;
174   }
175 }
176
177 /// isVariable - Return true if the specified tag is legal for DIVariable.
178 bool DIDescriptor::isVariable() const {
179   assert (!isNull() && "Invalid descriptor!");
180   unsigned Tag = getTag();
181
182   switch (Tag) {
183   case dwarf::DW_TAG_auto_variable:
184   case dwarf::DW_TAG_arg_variable:
185   case dwarf::DW_TAG_return_variable:
186     return true;
187   default:
188     return false;
189   }
190 }
191
192 /// isSubprogram - Return true if the specified tag is legal for
193 /// DISubprogram.
194 bool DIDescriptor::isSubprogram() const {
195   assert (!isNull() && "Invalid descriptor!");
196   unsigned Tag = getTag();
197
198   return Tag == dwarf::DW_TAG_subprogram;
199 }
200
201 /// isGlobalVariable - Return true if the specified tag is legal for
202 /// DIGlobalVariable.
203 bool DIDescriptor::isGlobalVariable() const {
204   assert (!isNull() && "Invalid descriptor!");
205   unsigned Tag = getTag();
206
207   return Tag == dwarf::DW_TAG_variable;
208 }
209
210 /// isScope - Return true if the specified tag is one of the scope
211 /// related tag.
212 bool DIDescriptor::isScope() const {
213   assert (!isNull() && "Invalid descriptor!");
214   unsigned Tag = getTag();
215
216   switch (Tag) {
217     case dwarf::DW_TAG_compile_unit:
218     case dwarf::DW_TAG_lexical_block:
219     case dwarf::DW_TAG_subprogram:
220       return true;
221     default:
222       break;
223   }
224   return false;
225 }
226
227 /// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
228 bool DIDescriptor::isCompileUnit() const {
229   assert (!isNull() && "Invalid descriptor!");
230   unsigned Tag = getTag();
231
232   return Tag == dwarf::DW_TAG_compile_unit;
233 }
234
235 /// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
236 bool DIDescriptor::isLexicalBlock() const {
237   assert (!isNull() && "Invalid descriptor!");
238   unsigned Tag = getTag();
239
240   return Tag == dwarf::DW_TAG_lexical_block;
241 }
242
243 //===----------------------------------------------------------------------===//
244 // Simple Descriptor Constructors and other Methods
245 //===----------------------------------------------------------------------===//
246
247 DIType::DIType(MDNode *N) : DIDescriptor(N) {
248   if (!N) return;
249   if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
250     DbgNode = 0;
251   }
252 }
253
254 unsigned DIArray::getNumElements() const {
255   assert (DbgNode && "Invalid DIArray");
256   return DbgNode->getNumElements();
257 }
258
259 /// replaceAllUsesWith - Replace all uses of debug info referenced by
260 /// this descriptor. After this completes, the current debug info value
261 /// is erased.
262 void DIDerivedType::replaceAllUsesWith(DIDescriptor &D) {
263   if (isNull())
264     return;
265
266   assert (!D.isNull() && "Can not replace with null");
267
268   // Since we use a TrackingVH for the node, its easy for clients to manufacture
269   // legitimate situations where they want to replaceAllUsesWith() on something
270   // which, due to uniquing, has merged with the source. We shield clients from
271   // this detail by allowing a value to be replaced with replaceAllUsesWith()
272   // itself.
273   if (getNode() != D.getNode()) {
274     MDNode *Node = DbgNode;
275     Node->replaceAllUsesWith(D.getNode());
276     delete Node;
277   }
278 }
279
280 /// Verify - Verify that a compile unit is well formed.
281 bool DICompileUnit::Verify() const {
282   if (isNull())
283     return false;
284   const char *N = getFilename();
285   if (!N)
286     return false;
287   // It is possible that directory and produce string is empty.
288   return true;
289 }
290
291 /// Verify - Verify that a type descriptor is well formed.
292 bool DIType::Verify() const {
293   if (isNull())
294     return false;
295   if (getContext().isNull())
296     return false;
297
298   DICompileUnit CU = getCompileUnit();
299   if (!CU.isNull() && !CU.Verify())
300     return false;
301   return true;
302 }
303
304 /// Verify - Verify that a composite type descriptor is well formed.
305 bool DICompositeType::Verify() const {
306   if (isNull())
307     return false;
308   if (getContext().isNull())
309     return false;
310
311   DICompileUnit CU = getCompileUnit();
312   if (!CU.isNull() && !CU.Verify())
313     return false;
314   return true;
315 }
316
317 /// Verify - Verify that a subprogram descriptor is well formed.
318 bool DISubprogram::Verify() const {
319   if (isNull())
320     return false;
321
322   if (getContext().isNull())
323     return false;
324
325   DICompileUnit CU = getCompileUnit();
326   if (!CU.Verify())
327     return false;
328
329   DICompositeType Ty = getType();
330   if (!Ty.isNull() && !Ty.Verify())
331     return false;
332   return true;
333 }
334
335 /// Verify - Verify that a global variable descriptor is well formed.
336 bool DIGlobalVariable::Verify() const {
337   if (isNull())
338     return false;
339
340   if (getContext().isNull())
341     return false;
342
343   DICompileUnit CU = getCompileUnit();
344   if (!CU.isNull() && !CU.Verify())
345     return false;
346
347   DIType Ty = getType();
348   if (!Ty.Verify())
349     return false;
350
351   if (!getGlobal())
352     return false;
353
354   return true;
355 }
356
357 /// Verify - Verify that a variable descriptor is well formed.
358 bool DIVariable::Verify() const {
359   if (isNull())
360     return false;
361
362   if (getContext().isNull())
363     return false;
364
365   DIType Ty = getType();
366   if (!Ty.Verify())
367     return false;
368
369   return true;
370 }
371
372 /// getOriginalTypeSize - If this type is derived from a base type then
373 /// return base type size.
374 uint64_t DIDerivedType::getOriginalTypeSize() const {
375   if (getTag() != dwarf::DW_TAG_member)
376     return getSizeInBits();
377   DIType BT = getTypeDerivedFrom();
378   if (BT.getTag() != dwarf::DW_TAG_base_type)
379     return getSizeInBits();
380   return BT.getSizeInBits();
381 }
382
383 /// describes - Return true if this subprogram provides debugging
384 /// information for the function F.
385 bool DISubprogram::describes(const Function *F) {
386   assert (F && "Invalid function");
387   const char *Name = getLinkageName();
388   if (!Name)
389     Name = getName();
390   if (strcmp(F->getName().data(), Name) == 0)
391     return true;
392   return false;
393 }
394
395 //===----------------------------------------------------------------------===//
396 // DIDescriptor: dump routines for all descriptors.
397 //===----------------------------------------------------------------------===//
398
399
400 /// dump - Print descriptor.
401 void DIDescriptor::dump() const {
402   errs() << "[" << dwarf::TagString(getTag()) << "] ";
403   errs().write_hex((intptr_t) &*DbgNode) << ']';
404 }
405
406 /// dump - Print compile unit.
407 void DICompileUnit::dump() const {
408   if (getLanguage())
409     errs() << " [" << dwarf::LanguageString(getLanguage()) << "] ";
410
411   errs() << " [" << getDirectory() << "/" << getFilename() << " ]";
412 }
413
414 /// dump - Print type.
415 void DIType::dump() const {
416   if (isNull()) return;
417
418   if (const char *Res = getName())
419     errs() << " [" << Res << "] ";
420
421   unsigned Tag = getTag();
422   errs() << " [" << dwarf::TagString(Tag) << "] ";
423
424   // TODO : Print context
425   getCompileUnit().dump();
426   errs() << " ["
427          << getLineNumber() << ", "
428          << getSizeInBits() << ", "
429          << getAlignInBits() << ", "
430          << getOffsetInBits()
431          << "] ";
432
433   if (isPrivate())
434     errs() << " [private] ";
435   else if (isProtected())
436     errs() << " [protected] ";
437
438   if (isForwardDecl())
439     errs() << " [fwd] ";
440
441   if (isBasicType())
442     DIBasicType(DbgNode).dump();
443   else if (isDerivedType())
444     DIDerivedType(DbgNode).dump();
445   else if (isCompositeType())
446     DICompositeType(DbgNode).dump();
447   else {
448     errs() << "Invalid DIType\n";
449     return;
450   }
451
452   errs() << "\n";
453 }
454
455 /// dump - Print basic type.
456 void DIBasicType::dump() const {
457   errs() << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
458 }
459
460 /// dump - Print derived type.
461 void DIDerivedType::dump() const {
462   errs() << "\n\t Derived From: "; getTypeDerivedFrom().dump();
463 }
464
465 /// dump - Print composite type.
466 void DICompositeType::dump() const {
467   DIArray A = getTypeArray();
468   if (A.isNull())
469     return;
470   errs() << " [" << A.getNumElements() << " elements]";
471 }
472
473 /// dump - Print global.
474 void DIGlobal::dump() const {
475   if (const char *Res = getName())
476     errs() << " [" << Res << "] ";
477
478   unsigned Tag = getTag();
479   errs() << " [" << dwarf::TagString(Tag) << "] ";
480
481   // TODO : Print context
482   getCompileUnit().dump();
483   errs() << " [" << getLineNumber() << "] ";
484
485   if (isLocalToUnit())
486     errs() << " [local] ";
487
488   if (isDefinition())
489     errs() << " [def] ";
490
491   if (isGlobalVariable())
492     DIGlobalVariable(DbgNode).dump();
493
494   errs() << "\n";
495 }
496
497 /// dump - Print subprogram.
498 void DISubprogram::dump() const {
499   if (const char *Res = getName())
500     errs() << " [" << Res << "] ";
501
502   unsigned Tag = getTag();
503   errs() << " [" << dwarf::TagString(Tag) << "] ";
504
505   // TODO : Print context
506   getCompileUnit().dump();
507   errs() << " [" << getLineNumber() << "] ";
508
509   if (isLocalToUnit())
510     errs() << " [local] ";
511
512   if (isDefinition())
513     errs() << " [def] ";
514
515   errs() << "\n";
516 }
517
518 /// dump - Print global variable.
519 void DIGlobalVariable::dump() const {
520   errs() << " [";
521   getGlobal()->dump();
522   errs() << "] ";
523 }
524
525 /// dump - Print variable.
526 void DIVariable::dump() const {
527   if (const char *Res = getName())
528     errs() << " [" << Res << "] ";
529
530   getCompileUnit().dump();
531   errs() << " [" << getLineNumber() << "] ";
532   getType().dump();
533   errs() << "\n";
534
535   // FIXME: Dump complex addresses
536 }
537
538 //===----------------------------------------------------------------------===//
539 // DIFactory: Basic Helpers
540 //===----------------------------------------------------------------------===//
541
542 DIFactory::DIFactory(Module &m)
543   : M(m), VMContext(M.getContext()), StopPointFn(0), FuncStartFn(0),
544     RegionStartFn(0), RegionEndFn(0),
545     DeclareFn(0) {
546   EmptyStructPtr = PointerType::getUnqual(StructType::get(VMContext));
547 }
548
549 Constant *DIFactory::GetTagConstant(unsigned TAG) {
550   assert((TAG & LLVMDebugVersionMask) == 0 &&
551          "Tag too large for debug encoding!");
552   return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
553 }
554
555 //===----------------------------------------------------------------------===//
556 // DIFactory: Primary Constructors
557 //===----------------------------------------------------------------------===//
558
559 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
560 /// This implicitly uniques the arrays created.
561 DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
562   SmallVector<Value*, 16> Elts;
563
564   if (NumTys == 0)
565     Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)));
566   else
567     for (unsigned i = 0; i != NumTys; ++i)
568       Elts.push_back(Tys[i].getNode());
569
570   return DIArray(MDNode::get(VMContext,Elts.data(), Elts.size()));
571 }
572
573 /// GetOrCreateSubrange - Create a descriptor for a value range.  This
574 /// implicitly uniques the values returned.
575 DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
576   Value *Elts[] = {
577     GetTagConstant(dwarf::DW_TAG_subrange_type),
578     ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
579     ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
580   };
581
582   return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
583 }
584
585
586
587 /// CreateCompileUnit - Create a new descriptor for the specified compile
588 /// unit.  Note that this does not unique compile units within the module.
589 DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
590                                            StringRef Filename,
591                                            StringRef Directory,
592                                            StringRef Producer,
593                                            bool isMain,
594                                            bool isOptimized,
595                                            const char *Flags,
596                                            unsigned RunTimeVer) {
597   Value *Elts[] = {
598     GetTagConstant(dwarf::DW_TAG_compile_unit),
599     llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
600     ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
601     MDString::get(VMContext, Filename),
602     MDString::get(VMContext, Directory),
603     MDString::get(VMContext, Producer),
604     ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
605     ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
606     MDString::get(VMContext, Flags),
607     ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
608   };
609
610   return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
611 }
612
613 /// CreateEnumerator - Create a single enumerator value.
614 DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
615   Value *Elts[] = {
616     GetTagConstant(dwarf::DW_TAG_enumerator),
617     MDString::get(VMContext, Name),
618     ConstantInt::get(Type::getInt64Ty(VMContext), Val)
619   };
620   return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
621 }
622
623
624 /// CreateBasicType - Create a basic type like int, float, etc.
625 DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
626                                        StringRef Name,
627                                        DICompileUnit CompileUnit,
628                                        unsigned LineNumber,
629                                        uint64_t SizeInBits,
630                                        uint64_t AlignInBits,
631                                        uint64_t OffsetInBits, unsigned Flags,
632                                        unsigned Encoding) {
633   Value *Elts[] = {
634     GetTagConstant(dwarf::DW_TAG_base_type),
635     Context.getNode(),
636     MDString::get(VMContext, Name),
637     CompileUnit.getNode(),
638     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
639     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
640     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
641     ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
642     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
643     ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
644   };
645   return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
646 }
647
648 /// CreateDerivedType - Create a derived type like const qualified type,
649 /// pointer, typedef, etc.
650 DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
651                                            DIDescriptor Context,
652                                            StringRef Name,
653                                            DICompileUnit CompileUnit,
654                                            unsigned LineNumber,
655                                            uint64_t SizeInBits,
656                                            uint64_t AlignInBits,
657                                            uint64_t OffsetInBits,
658                                            unsigned Flags,
659                                            DIType DerivedFrom) {
660   Value *Elts[] = {
661     GetTagConstant(Tag),
662     Context.getNode(),
663     MDString::get(VMContext, Name),
664     CompileUnit.getNode(),
665     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
666     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
667     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
668     ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
669     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
670     DerivedFrom.getNode(),
671   };
672   return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
673 }
674
675 /// CreateCompositeType - Create a composite type like array, struct, etc.
676 DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
677                                                DIDescriptor Context,
678                                                StringRef Name,
679                                                DICompileUnit CompileUnit,
680                                                unsigned LineNumber,
681                                                uint64_t SizeInBits,
682                                                uint64_t AlignInBits,
683                                                uint64_t OffsetInBits,
684                                                unsigned Flags,
685                                                DIType DerivedFrom,
686                                                DIArray Elements,
687                                                unsigned RuntimeLang) {
688
689   Value *Elts[] = {
690     GetTagConstant(Tag),
691     Context.getNode(),
692     MDString::get(VMContext, Name),
693     CompileUnit.getNode(),
694     ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
695     ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
696     ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
697     ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
698     ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
699     DerivedFrom.getNode(),
700     Elements.getNode(),
701     ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
702   };
703   return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
704 }
705
706
707 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
708 /// See comments in DISubprogram for descriptions of these fields.  This
709 /// method does not unique the generated descriptors.
710 DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
711                                          StringRef Name,
712                                          StringRef DisplayName,
713                                          StringRef LinkageName,
714                                          DICompileUnit CompileUnit,
715                                          unsigned LineNo, DIType Type,
716                                          bool isLocalToUnit,
717                                          bool isDefinition) {
718
719   Value *Elts[] = {
720     GetTagConstant(dwarf::DW_TAG_subprogram),
721     llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
722     Context.getNode(),
723     MDString::get(VMContext, Name),
724     MDString::get(VMContext, DisplayName),
725     MDString::get(VMContext, LinkageName),
726     CompileUnit.getNode(),
727     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
728     Type.getNode(),
729     ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
730     ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition)
731   };
732   return DISubprogram(MDNode::get(VMContext, &Elts[0], 11));
733 }
734
735 /// CreateGlobalVariable - Create a new descriptor for the specified global.
736 DIGlobalVariable
737 DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
738                                 StringRef DisplayName,
739                                 StringRef LinkageName,
740                                 DICompileUnit CompileUnit,
741                                 unsigned LineNo, DIType Type,bool isLocalToUnit,
742                                 bool isDefinition, llvm::GlobalVariable *Val) {
743   Value *Elts[] = {
744     GetTagConstant(dwarf::DW_TAG_variable),
745     llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
746     Context.getNode(),
747     MDString::get(VMContext, Name),
748     MDString::get(VMContext, DisplayName),
749     MDString::get(VMContext, LinkageName),
750     CompileUnit.getNode(),
751     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
752     Type.getNode(),
753     ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
754     ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
755     Val
756   };
757
758   Value *const *Vs = &Elts[0];
759   MDNode *Node = MDNode::get(VMContext,Vs, 12);
760
761   // Create a named metadata so that we do not lose this mdnode.
762   NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
763   NMD->addElement(Node);
764
765   return DIGlobalVariable(Node);
766 }
767
768
769 /// CreateVariable - Create a new descriptor for the specified variable.
770 DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
771                                      StringRef Name,
772                                      DICompileUnit CompileUnit, unsigned LineNo,
773                                      DIType Type) {
774   Value *Elts[] = {
775     GetTagConstant(Tag),
776     Context.getNode(),
777     MDString::get(VMContext, Name),
778     CompileUnit.getNode(),
779     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
780     Type.getNode(),
781   };
782   return DIVariable(MDNode::get(VMContext, &Elts[0], 6));
783 }
784
785
786 /// CreateComplexVariable - Create a new descriptor for the specified variable
787 /// which has a complex address expression for its address.
788 DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
789                                             const std::string &Name,
790                                             DICompileUnit CompileUnit,
791                                             unsigned LineNo,
792                                    DIType Type, SmallVector<Value *, 9> &addr) {
793   SmallVector<Value *, 9> Elts;
794   Elts.push_back(GetTagConstant(Tag));
795   Elts.push_back(Context.getNode());
796   Elts.push_back(MDString::get(VMContext, Name));
797   Elts.push_back(CompileUnit.getNode());
798   Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
799   Elts.push_back(Type.getNode());
800   Elts.insert(Elts.end(), addr.begin(), addr.end());
801
802   return DIVariable(MDNode::get(VMContext, &Elts[0], 6+addr.size()));
803 }
804
805
806 /// CreateBlock - This creates a descriptor for a lexical block with the
807 /// specified parent VMContext.
808 DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context) {
809   Value *Elts[] = {
810     GetTagConstant(dwarf::DW_TAG_lexical_block),
811     Context.getNode()
812   };
813   return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 2));
814 }
815
816 /// CreateLocation - Creates a debug info location.
817 DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
818                                      DIScope S, DILocation OrigLoc) {
819   Value *Elts[] = {
820     ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
821     ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
822     S.getNode(),
823     OrigLoc.getNode(),
824   };
825   return DILocation(MDNode::get(VMContext, &Elts[0], 4));
826 }
827
828
829 //===----------------------------------------------------------------------===//
830 // DIFactory: Routines for inserting code into a function
831 //===----------------------------------------------------------------------===//
832
833 /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
834 /// inserting it at the end of the specified basic block.
835 void DIFactory::InsertStopPoint(DICompileUnit CU, unsigned LineNo,
836                                 unsigned ColNo, BasicBlock *BB) {
837
838   // Lazily construct llvm.dbg.stoppoint function.
839   if (!StopPointFn)
840     StopPointFn = llvm::Intrinsic::getDeclaration(&M,
841                                               llvm::Intrinsic::dbg_stoppoint);
842
843   // Invoke llvm.dbg.stoppoint
844   Value *Args[] = {
845     ConstantInt::get(llvm::Type::getInt32Ty(VMContext), LineNo),
846     ConstantInt::get(llvm::Type::getInt32Ty(VMContext), ColNo),
847     CU.getNode()
848   };
849   CallInst::Create(StopPointFn, Args, Args+3, "", BB);
850 }
851
852 /// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
853 /// mark the start of the specified subprogram.
854 void DIFactory::InsertSubprogramStart(DISubprogram SP, BasicBlock *BB) {
855   // Lazily construct llvm.dbg.func.start.
856   if (!FuncStartFn)
857     FuncStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_func_start);
858
859   // Call llvm.dbg.func.start which also implicitly sets a stoppoint.
860   CallInst::Create(FuncStartFn, SP.getNode(), "", BB);
861 }
862
863 /// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
864 /// mark the start of a region for the specified scoping descriptor.
865 void DIFactory::InsertRegionStart(DIDescriptor D, BasicBlock *BB) {
866   // Lazily construct llvm.dbg.region.start function.
867   if (!RegionStartFn)
868     RegionStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_start);
869
870   // Call llvm.dbg.func.start.
871   CallInst::Create(RegionStartFn, D.getNode(), "", BB);
872 }
873
874 /// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
875 /// mark the end of a region for the specified scoping descriptor.
876 void DIFactory::InsertRegionEnd(DIDescriptor D, BasicBlock *BB) {
877   // Lazily construct llvm.dbg.region.end function.
878   if (!RegionEndFn)
879     RegionEndFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_end);
880
881   // Call llvm.dbg.region.end.
882   CallInst::Create(RegionEndFn, D.getNode(), "", BB);
883 }
884
885 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
886 void DIFactory::InsertDeclare(Value *Storage, DIVariable D, BasicBlock *BB) {
887   // Cast the storage to a {}* for the call to llvm.dbg.declare.
888   Storage = new BitCastInst(Storage, EmptyStructPtr, "", BB);
889
890   if (!DeclareFn)
891     DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
892
893   Value *Args[] = { Storage, D.getNode() };
894   CallInst::Create(DeclareFn, Args, Args+2, "", BB);
895 }
896
897
898 //===----------------------------------------------------------------------===//
899 // DebugInfoFinder implementations.
900 //===----------------------------------------------------------------------===//
901
902 /// processModule - Process entire module and collect debug info.
903 void DebugInfoFinder::processModule(Module &M) {
904
905
906   for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
907     for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
908       for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
909            ++BI) {
910         if (DbgStopPointInst *SPI = dyn_cast<DbgStopPointInst>(BI))
911           processStopPoint(SPI);
912         else if (DbgFuncStartInst *FSI = dyn_cast<DbgFuncStartInst>(BI))
913           processFuncStart(FSI);
914         else if (DbgRegionStartInst *DRS = dyn_cast<DbgRegionStartInst>(BI))
915           processRegionStart(DRS);
916         else if (DbgRegionEndInst *DRE = dyn_cast<DbgRegionEndInst>(BI))
917           processRegionEnd(DRE);
918         else if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
919           processDeclare(DDI);
920       }
921
922   NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");
923   if (!NMD)
924     return;
925
926   for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
927     DIGlobalVariable DIG(cast<MDNode>(NMD->getElement(i)));
928     if (addGlobalVariable(DIG)) {
929       addCompileUnit(DIG.getCompileUnit());
930       processType(DIG.getType());
931     }
932   }
933 }
934
935 /// processType - Process DIType.
936 void DebugInfoFinder::processType(DIType DT) {
937   if (!addType(DT))
938     return;
939
940   addCompileUnit(DT.getCompileUnit());
941   if (DT.isCompositeType()) {
942     DICompositeType DCT(DT.getNode());
943     processType(DCT.getTypeDerivedFrom());
944     DIArray DA = DCT.getTypeArray();
945     if (!DA.isNull())
946       for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
947         DIDescriptor D = DA.getElement(i);
948         DIType TypeE = DIType(D.getNode());
949         if (!TypeE.isNull())
950           processType(TypeE);
951         else
952           processSubprogram(DISubprogram(D.getNode()));
953       }
954   } else if (DT.isDerivedType()) {
955     DIDerivedType DDT(DT.getNode());
956     if (!DDT.isNull())
957       processType(DDT.getTypeDerivedFrom());
958   }
959 }
960
961 /// processSubprogram - Process DISubprogram.
962 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
963   if (SP.isNull())
964     return;
965   if (!addSubprogram(SP))
966     return;
967   addCompileUnit(SP.getCompileUnit());
968   processType(SP.getType());
969 }
970
971 /// processStopPoint - Process DbgStopPointInst.
972 void DebugInfoFinder::processStopPoint(DbgStopPointInst *SPI) {
973   MDNode *Context = dyn_cast<MDNode>(SPI->getContext());
974   addCompileUnit(DICompileUnit(Context));
975 }
976
977 /// processFuncStart - Process DbgFuncStartInst.
978 void DebugInfoFinder::processFuncStart(DbgFuncStartInst *FSI) {
979   MDNode *SP = dyn_cast<MDNode>(FSI->getSubprogram());
980   processSubprogram(DISubprogram(SP));
981 }
982
983 /// processRegionStart - Process DbgRegionStart.
984 void DebugInfoFinder::processRegionStart(DbgRegionStartInst *DRS) {
985   MDNode *SP = dyn_cast<MDNode>(DRS->getContext());
986   processSubprogram(DISubprogram(SP));
987 }
988
989 /// processRegionEnd - Process DbgRegionEnd.
990 void DebugInfoFinder::processRegionEnd(DbgRegionEndInst *DRE) {
991   MDNode *SP = dyn_cast<MDNode>(DRE->getContext());
992   processSubprogram(DISubprogram(SP));
993 }
994
995 /// processDeclare - Process DbgDeclareInst.
996 void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
997   DIVariable DV(cast<MDNode>(DDI->getVariable()));
998   if (DV.isNull())
999     return;
1000
1001   if (!NodesSeen.insert(DV.getNode()))
1002     return;
1003
1004   addCompileUnit(DV.getCompileUnit());
1005   processType(DV.getType());
1006 }
1007
1008 /// addType - Add type into Tys.
1009 bool DebugInfoFinder::addType(DIType DT) {
1010   if (DT.isNull())
1011     return false;
1012
1013   if (!NodesSeen.insert(DT.getNode()))
1014     return false;
1015
1016   TYs.push_back(DT.getNode());
1017   return true;
1018 }
1019
1020 /// addCompileUnit - Add compile unit into CUs.
1021 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1022   if (CU.isNull())
1023     return false;
1024
1025   if (!NodesSeen.insert(CU.getNode()))
1026     return false;
1027
1028   CUs.push_back(CU.getNode());
1029   return true;
1030 }
1031
1032 /// addGlobalVariable - Add global variable into GVs.
1033 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1034   if (DIG.isNull())
1035     return false;
1036
1037   if (!NodesSeen.insert(DIG.getNode()))
1038     return false;
1039
1040   GVs.push_back(DIG.getNode());
1041   return true;
1042 }
1043
1044 // addSubprogram - Add subprgoram into SPs.
1045 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1046   if (SP.isNull())
1047     return false;
1048
1049   if (!NodesSeen.insert(SP.getNode()))
1050     return false;
1051
1052   SPs.push_back(SP.getNode());
1053   return true;
1054 }
1055
1056 namespace llvm {
1057   /// findStopPoint - Find the stoppoint coressponding to this instruction, that
1058   /// is the stoppoint that dominates this instruction.
1059   const DbgStopPointInst *findStopPoint(const Instruction *Inst) {
1060     if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(Inst))
1061       return DSI;
1062
1063     const BasicBlock *BB = Inst->getParent();
1064     BasicBlock::const_iterator I = Inst, B;
1065     while (BB) {
1066       B = BB->begin();
1067
1068       // A BB consisting only of a terminator can't have a stoppoint.
1069       while (I != B) {
1070         --I;
1071         if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
1072           return DSI;
1073       }
1074
1075       // This BB didn't have a stoppoint: if there is only one predecessor, look
1076       // for a stoppoint there. We could use getIDom(), but that would require
1077       // dominator info.
1078       BB = I->getParent()->getUniquePredecessor();
1079       if (BB)
1080         I = BB->getTerminator();
1081     }
1082
1083     return 0;
1084   }
1085
1086   /// findBBStopPoint - Find the stoppoint corresponding to first real
1087   /// (non-debug intrinsic) instruction in this Basic Block, and return the
1088   /// stoppoint for it.
1089   const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB) {
1090     for(BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
1091       if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
1092         return DSI;
1093
1094     // Fallback to looking for stoppoint of unique predecessor. Useful if this
1095     // BB contains no stoppoints, but unique predecessor does.
1096     BB = BB->getUniquePredecessor();
1097     if (BB)
1098       return findStopPoint(BB->getTerminator());
1099
1100     return 0;
1101   }
1102
1103   Value *findDbgGlobalDeclare(GlobalVariable *V) {
1104     const Module *M = V->getParent();
1105     NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1106     if (!NMD)
1107       return 0;
1108
1109     for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
1110       DIGlobalVariable DIG(cast_or_null<MDNode>(NMD->getElement(i)));
1111       if (DIG.isNull())
1112         continue;
1113       if (DIG.getGlobal() == V)
1114         return DIG.getNode();
1115     }
1116     return 0;
1117   }
1118
1119   /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
1120   /// It looks through pointer casts too.
1121   const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts) {
1122     if (stripCasts) {
1123       V = V->stripPointerCasts();
1124
1125       // Look for the bitcast.
1126       for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
1127             I != E; ++I)
1128         if (isa<BitCastInst>(I))
1129           return findDbgDeclare(*I, false);
1130
1131       return 0;
1132     }
1133
1134     // Find llvm.dbg.declare among uses of the instruction.
1135     for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
1136           I != E; ++I)
1137       if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I))
1138         return DDI;
1139
1140     return 0;
1141   }
1142
1143 bool getLocationInfo(const Value *V, std::string &DisplayName,
1144                      std::string &Type, unsigned &LineNo, std::string &File,
1145                        std::string &Dir) {
1146     DICompileUnit Unit;
1147     DIType TypeD;
1148
1149     if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1150       Value *DIGV = findDbgGlobalDeclare(GV);
1151       if (!DIGV) return false;
1152       DIGlobalVariable Var(cast<MDNode>(DIGV));
1153
1154       if (const char *D = Var.getDisplayName())
1155         DisplayName = D;
1156       LineNo = Var.getLineNumber();
1157       Unit = Var.getCompileUnit();
1158       TypeD = Var.getType();
1159     } else {
1160       const DbgDeclareInst *DDI = findDbgDeclare(V);
1161       if (!DDI) return false;
1162       DIVariable Var(cast<MDNode>(DDI->getVariable()));
1163
1164       if (const char *D = Var.getName())
1165         DisplayName = D;
1166       LineNo = Var.getLineNumber();
1167       Unit = Var.getCompileUnit();
1168       TypeD = Var.getType();
1169     }
1170
1171     if (const char *T = TypeD.getName())
1172       Type = T;
1173     if (const char *F = Unit.getFilename())
1174       File = F;
1175     if (const char *D = Unit.getDirectory())
1176       Dir = D;
1177     return true;
1178   }
1179
1180   /// isValidDebugInfoIntrinsic - Return true if SPI is a valid debug
1181   /// info intrinsic.
1182   bool isValidDebugInfoIntrinsic(DbgStopPointInst &SPI,
1183                                  CodeGenOpt::Level OptLev) {
1184     return DIDescriptor::ValidDebugInfo(SPI.getContext(), OptLev);
1185   }
1186
1187   /// isValidDebugInfoIntrinsic - Return true if FSI is a valid debug
1188   /// info intrinsic.
1189   bool isValidDebugInfoIntrinsic(DbgFuncStartInst &FSI,
1190                                  CodeGenOpt::Level OptLev) {
1191     return DIDescriptor::ValidDebugInfo(FSI.getSubprogram(), OptLev);
1192   }
1193
1194   /// isValidDebugInfoIntrinsic - Return true if RSI is a valid debug
1195   /// info intrinsic.
1196   bool isValidDebugInfoIntrinsic(DbgRegionStartInst &RSI,
1197                                  CodeGenOpt::Level OptLev) {
1198     return DIDescriptor::ValidDebugInfo(RSI.getContext(), OptLev);
1199   }
1200
1201   /// isValidDebugInfoIntrinsic - Return true if REI is a valid debug
1202   /// info intrinsic.
1203   bool isValidDebugInfoIntrinsic(DbgRegionEndInst &REI,
1204                                  CodeGenOpt::Level OptLev) {
1205     return DIDescriptor::ValidDebugInfo(REI.getContext(), OptLev);
1206   }
1207
1208
1209   /// isValidDebugInfoIntrinsic - Return true if DI is a valid debug
1210   /// info intrinsic.
1211   bool isValidDebugInfoIntrinsic(DbgDeclareInst &DI,
1212                                  CodeGenOpt::Level OptLev) {
1213     return DIDescriptor::ValidDebugInfo(DI.getVariable(), OptLev);
1214   }
1215
1216   /// ExtractDebugLocation - Extract debug location information
1217   /// from llvm.dbg.stoppoint intrinsic.
1218   DebugLoc ExtractDebugLocation(DbgStopPointInst &SPI,
1219                                 DebugLocTracker &DebugLocInfo) {
1220     DebugLoc DL;
1221     Value *Context = SPI.getContext();
1222
1223     // If this location is already tracked then use it.
1224     DebugLocTuple Tuple(cast<MDNode>(Context), SPI.getLine(),
1225                         SPI.getColumn());
1226     DenseMap<DebugLocTuple, unsigned>::iterator II
1227       = DebugLocInfo.DebugIdMap.find(Tuple);
1228     if (II != DebugLocInfo.DebugIdMap.end())
1229       return DebugLoc::get(II->second);
1230
1231     // Add a new location entry.
1232     unsigned Id = DebugLocInfo.DebugLocations.size();
1233     DebugLocInfo.DebugLocations.push_back(Tuple);
1234     DebugLocInfo.DebugIdMap[Tuple] = Id;
1235
1236     return DebugLoc::get(Id);
1237   }
1238
1239   /// ExtractDebugLocation - Extract debug location information
1240   /// from DILocation.
1241   DebugLoc ExtractDebugLocation(DILocation &Loc,
1242                                 DebugLocTracker &DebugLocInfo) {
1243     DebugLoc DL;
1244     MDNode *Context = Loc.getScope().getNode();
1245
1246     // If this location is already tracked then use it.
1247     DebugLocTuple Tuple(Context, Loc.getLineNumber(),
1248                         Loc.getColumnNumber());
1249     DenseMap<DebugLocTuple, unsigned>::iterator II
1250       = DebugLocInfo.DebugIdMap.find(Tuple);
1251     if (II != DebugLocInfo.DebugIdMap.end())
1252       return DebugLoc::get(II->second);
1253
1254     // Add a new location entry.
1255     unsigned Id = DebugLocInfo.DebugLocations.size();
1256     DebugLocInfo.DebugLocations.push_back(Tuple);
1257     DebugLocInfo.DebugIdMap[Tuple] = Id;
1258
1259     return DebugLoc::get(Id);
1260   }
1261
1262   /// ExtractDebugLocation - Extract debug location information
1263   /// from llvm.dbg.func_start intrinsic.
1264   DebugLoc ExtractDebugLocation(DbgFuncStartInst &FSI,
1265                                 DebugLocTracker &DebugLocInfo) {
1266     DebugLoc DL;
1267     Value *SP = FSI.getSubprogram();
1268
1269     DISubprogram Subprogram(cast<MDNode>(SP));
1270     unsigned Line = Subprogram.getLineNumber();
1271     DICompileUnit CU(Subprogram.getCompileUnit());
1272
1273     // If this location is already tracked then use it.
1274     DebugLocTuple Tuple(CU.getNode(), Line, /* Column */ 0);
1275     DenseMap<DebugLocTuple, unsigned>::iterator II
1276       = DebugLocInfo.DebugIdMap.find(Tuple);
1277     if (II != DebugLocInfo.DebugIdMap.end())
1278       return DebugLoc::get(II->second);
1279
1280     // Add a new location entry.
1281     unsigned Id = DebugLocInfo.DebugLocations.size();
1282     DebugLocInfo.DebugLocations.push_back(Tuple);
1283     DebugLocInfo.DebugIdMap[Tuple] = Id;
1284
1285     return DebugLoc::get(Id);
1286   }
1287
1288   /// isInlinedFnStart - Return true if FSI is starting an inlined function.
1289   bool isInlinedFnStart(DbgFuncStartInst &FSI, const Function *CurrentFn) {
1290     DISubprogram Subprogram(cast<MDNode>(FSI.getSubprogram()));
1291     if (Subprogram.describes(CurrentFn))
1292       return false;
1293
1294     return true;
1295   }
1296
1297   /// isInlinedFnEnd - Return true if REI is ending an inlined function.
1298   bool isInlinedFnEnd(DbgRegionEndInst &REI, const Function *CurrentFn) {
1299     DISubprogram Subprogram(cast<MDNode>(REI.getContext()));
1300     if (Subprogram.isNull() || Subprogram.describes(CurrentFn))
1301       return false;
1302
1303     return true;
1304   }
1305 }