Adding basic structure support.
[oota-llvm.git] / lib / CodeGen / MachineDebugInfo.cpp
1 //===-- llvm/CodeGen/MachineDebugInfo.cpp -----------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by James M. Laskey and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/CodeGen/MachineDebugInfo.h"
11
12 #include "llvm/Constants.h"
13 #include "llvm/DerivedTypes.h"
14 #include "llvm/GlobalVariable.h"
15 #include "llvm/Intrinsics.h"
16 #include "llvm/Instructions.h"
17 #include "llvm/Module.h"
18 #include "llvm/Support/Dwarf.h"
19
20 #include <iostream>
21
22 using namespace llvm;
23 using namespace llvm::dwarf;
24
25 // Handle the Pass registration stuff necessary to use TargetData's.
26 namespace {
27   RegisterPass<MachineDebugInfo> X("machinedebuginfo", "Debug Information");
28 }
29
30 //===----------------------------------------------------------------------===//
31
32 /// getGlobalVariablesUsing - Return all of the GlobalVariables which have the
33 /// specified value in their initializer somewhere.
34 static void
35 getGlobalVariablesUsing(Value *V, std::vector<GlobalVariable*> &Result) {
36   // Scan though value users.
37   for (Value::use_iterator I = V->use_begin(), E = V->use_end(); I != E; ++I) {
38     if (GlobalVariable *GV = dyn_cast<GlobalVariable>(*I)) {
39       // If the user is a GlobalVariable then add to result.
40       Result.push_back(GV);
41     } else if (Constant *C = dyn_cast<Constant>(*I)) {
42       // If the user is a constant variable then scan its users
43       getGlobalVariablesUsing(C, Result);
44     }
45   }
46 }
47
48 /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
49 /// named GlobalVariable.
50 static std::vector<GlobalVariable*>
51 getGlobalVariablesUsing(Module &M, const std::string &RootName) {
52   std::vector<GlobalVariable*> Result;  // GlobalVariables matching criteria.
53   
54   std::vector<const Type*> FieldTypes;
55   FieldTypes.push_back(Type::UIntTy);
56   FieldTypes.push_back(PointerType::get(Type::SByteTy));
57
58   // Get the GlobalVariable root.
59   GlobalVariable *UseRoot = M.getGlobalVariable(RootName,
60                                                 StructType::get(FieldTypes));
61
62   // If present and linkonce then scan for users.
63   if (UseRoot && UseRoot->hasLinkOnceLinkage()) {
64     getGlobalVariablesUsing(UseRoot, Result);
65   }
66   
67   return Result;
68 }
69   
70 /// getStringValue - Turn an LLVM constant pointer that eventually points to a
71 /// global into a string value.  Return an empty string if we can't do it.
72 ///
73 static const std::string getStringValue(Value *V, unsigned Offset = 0) {
74   if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
75     if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
76       ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
77       if (Init->isString()) {
78         std::string Result = Init->getAsString();
79         if (Offset < Result.size()) {
80           // If we are pointing INTO The string, erase the beginning...
81           Result.erase(Result.begin(), Result.begin()+Offset);
82
83           // Take off the null terminator, and any string fragments after it.
84           std::string::size_type NullPos = Result.find_first_of((char)0);
85           if (NullPos != std::string::npos)
86             Result.erase(Result.begin()+NullPos, Result.end());
87           return Result;
88         }
89       }
90     }
91   } else if (Constant *C = dyn_cast<Constant>(V)) {
92     if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
93       return getStringValue(GV, Offset);
94     else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
95       if (CE->getOpcode() == Instruction::GetElementPtr) {
96         // Turn a gep into the specified offset.
97         if (CE->getNumOperands() == 3 &&
98             cast<Constant>(CE->getOperand(1))->isNullValue() &&
99             isa<ConstantInt>(CE->getOperand(2))) {
100           return getStringValue(CE->getOperand(0),
101                    Offset+cast<ConstantInt>(CE->getOperand(2))->getRawValue());
102         }
103       }
104     }
105   }
106   return "";
107 }
108
109 /// isStringValue - Return true if the given value can be coerced to a string.
110 ///
111 static bool isStringValue(Value *V) {
112   if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
113     if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
114       ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
115       return Init->isString();
116     }
117   } else if (Constant *C = dyn_cast<Constant>(V)) {
118     if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
119       return isStringValue(GV);
120     else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
121       if (CE->getOpcode() == Instruction::GetElementPtr) {
122         if (CE->getNumOperands() == 3 &&
123             cast<Constant>(CE->getOperand(1))->isNullValue() &&
124             isa<ConstantInt>(CE->getOperand(2))) {
125           return isStringValue(CE->getOperand(0));
126         }
127       }
128     }
129   }
130   return false;
131 }
132
133 /// getGlobalVariable - Return either a direct or cast Global value.
134 ///
135 static GlobalVariable *getGlobalVariable(Value *V) {
136   if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
137     return GV;
138   } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
139     if (CE->getOpcode() == Instruction::Cast) {
140       return dyn_cast<GlobalVariable>(CE->getOperand(0));
141     }
142   }
143   return NULL;
144 }
145
146 /// isGlobalVariable - Return true if the given value can be coerced to a
147 /// GlobalVariable.
148 static bool isGlobalVariable(Value *V) {
149   if (isa<GlobalVariable>(V) || isa<ConstantPointerNull>(V)) {
150     return true;
151   } else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(V)) {
152     if (CE->getOpcode() == Instruction::Cast) {
153       return isa<GlobalVariable>(CE->getOperand(0));
154     }
155   }
156   return false;
157 }
158
159 /// getUIntOperand - Return ith operand if it is an unsigned integer.
160 ///
161 static ConstantUInt *getUIntOperand(GlobalVariable *GV, unsigned i) {
162   // Make sure the GlobalVariable has an initializer.
163   if (!GV->hasInitializer()) return NULL;
164   
165   // Get the initializer constant.
166   ConstantStruct *CI = dyn_cast<ConstantStruct>(GV->getInitializer());
167   if (!CI) return NULL;
168   
169   // Check if there is at least i + 1 operands.
170   unsigned N = CI->getNumOperands();
171   if (i >= N) return NULL;
172
173   // Check constant.
174   return dyn_cast<ConstantUInt>(CI->getOperand(i));
175 }
176 //===----------------------------------------------------------------------===//
177
178 /// ApplyToFields - Target the visitor to each field of the debug information
179 /// descriptor.
180 void DIVisitor::ApplyToFields(DebugInfoDesc *DD) {
181   DD->ApplyToFields(this);
182 }
183
184 //===----------------------------------------------------------------------===//
185 /// DICountVisitor - This DIVisitor counts all the fields in the supplied debug
186 /// the supplied DebugInfoDesc.
187 class DICountVisitor : public DIVisitor {
188 private:
189   unsigned Count;                       // Running count of fields.
190   
191 public:
192   DICountVisitor() : DIVisitor(), Count(0) {}
193   
194   // Accessors.
195   unsigned getCount() const { return Count; }
196   
197   /// Apply - Count each of the fields.
198   ///
199   virtual void Apply(int &Field)             { ++Count; }
200   virtual void Apply(unsigned &Field)        { ++Count; }
201   virtual void Apply(int64_t &Field)         { ++Count; }
202   virtual void Apply(uint64_t &Field)        { ++Count; }
203   virtual void Apply(bool &Field)            { ++Count; }
204   virtual void Apply(std::string &Field)     { ++Count; }
205   virtual void Apply(DebugInfoDesc *&Field)  { ++Count; }
206   virtual void Apply(GlobalVariable *&Field) { ++Count; }
207   virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
208     ++Count;
209   }
210 };
211
212 //===----------------------------------------------------------------------===//
213 /// DIDeserializeVisitor - This DIVisitor deserializes all the fields in the
214 /// supplied DebugInfoDesc.
215 class DIDeserializeVisitor : public DIVisitor {
216 private:
217   DIDeserializer &DR;                   // Active deserializer.
218   unsigned I;                           // Current operand index.
219   ConstantStruct *CI;                   // GlobalVariable constant initializer.
220
221 public:
222   DIDeserializeVisitor(DIDeserializer &D, GlobalVariable *GV)
223   : DIVisitor()
224   , DR(D)
225   , I(0)
226   , CI(cast<ConstantStruct>(GV->getInitializer()))
227   {}
228   
229   /// Apply - Set the value of each of the fields.
230   ///
231   virtual void Apply(int &Field) {
232     Constant *C = CI->getOperand(I++);
233     Field = cast<ConstantSInt>(C)->getValue();
234   }
235   virtual void Apply(unsigned &Field) {
236     Constant *C = CI->getOperand(I++);
237     Field = cast<ConstantUInt>(C)->getValue();
238   }
239   virtual void Apply(int64_t &Field) {
240     Constant *C = CI->getOperand(I++);
241     Field = cast<ConstantSInt>(C)->getValue();
242   }
243   virtual void Apply(uint64_t &Field) {
244     Constant *C = CI->getOperand(I++);
245     Field = cast<ConstantUInt>(C)->getValue();
246   }
247   virtual void Apply(bool &Field) {
248     Constant *C = CI->getOperand(I++);
249     Field = cast<ConstantBool>(C)->getValue();
250   }
251   virtual void Apply(std::string &Field) {
252     Constant *C = CI->getOperand(I++);
253     Field = getStringValue(C);
254   }
255   virtual void Apply(DebugInfoDesc *&Field) {
256     Constant *C = CI->getOperand(I++);
257     Field = DR.Deserialize(C);
258   }
259   virtual void Apply(GlobalVariable *&Field) {
260     Constant *C = CI->getOperand(I++);
261     Field = getGlobalVariable(C);
262   }
263   virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
264     Constant *C = CI->getOperand(I++);
265     GlobalVariable *GV = getGlobalVariable(C);
266     ConstantArray *CA = cast<ConstantArray>(GV->getInitializer());
267     Field.resize(0);
268     for (unsigned i = 0, N = CA->getNumOperands(); i < N; ++i) {
269       GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
270       DebugInfoDesc *DE = DR.Deserialize(GVE);
271       Field.push_back(DE);
272     }
273   }
274 };
275
276 //===----------------------------------------------------------------------===//
277 /// DISerializeVisitor - This DIVisitor serializes all the fields in
278 /// the supplied DebugInfoDesc.
279 class DISerializeVisitor : public DIVisitor {
280 private:
281   DISerializer &SR;                     // Active serializer.
282   std::vector<Constant*> &Elements;     // Element accumulator.
283   
284 public:
285   DISerializeVisitor(DISerializer &S, std::vector<Constant*> &E)
286   : DIVisitor()
287   , SR(S)
288   , Elements(E)
289   {}
290   
291   /// Apply - Set the value of each of the fields.
292   ///
293   virtual void Apply(int &Field) {
294     Elements.push_back(ConstantSInt::get(Type::IntTy, Field));
295   }
296   virtual void Apply(unsigned &Field) {
297     Elements.push_back(ConstantUInt::get(Type::UIntTy, Field));
298   }
299   virtual void Apply(int64_t &Field) {
300     Elements.push_back(ConstantSInt::get(Type::IntTy, Field));
301   }
302   virtual void Apply(uint64_t &Field) {
303     Elements.push_back(ConstantUInt::get(Type::UIntTy, Field));
304   }
305   virtual void Apply(bool &Field) {
306     Elements.push_back(ConstantBool::get(Field));
307   }
308   virtual void Apply(std::string &Field) {
309     Elements.push_back(SR.getString(Field));
310   }
311   virtual void Apply(DebugInfoDesc *&Field) {
312     GlobalVariable *GV = NULL;
313     
314     // If non-NULL the convert to global.
315     if (Field) GV = SR.Serialize(Field);
316     
317     // FIXME - At some point should use specific type.
318     const PointerType *EmptyTy = SR.getEmptyStructPtrType();
319     
320     if (GV) {
321       // Set to pointer to global.
322       Elements.push_back(ConstantExpr::getCast(GV, EmptyTy));
323     } else {
324       // Use NULL.
325       Elements.push_back(ConstantPointerNull::get(EmptyTy));
326     }
327   }
328   virtual void Apply(GlobalVariable *&Field) {
329     const PointerType *EmptyTy = SR.getEmptyStructPtrType();
330     if (Field) {
331       Elements.push_back(ConstantExpr::getCast(Field, EmptyTy));
332     } else {
333       Elements.push_back(ConstantPointerNull::get(EmptyTy));
334     }
335   }
336   virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
337     const PointerType *EmptyTy = SR.getEmptyStructPtrType();
338     unsigned N = Field.size();
339     ArrayType *AT = ArrayType::get(EmptyTy, N);
340     std::vector<Constant *> ArrayElements;
341
342     for (unsigned i = 0, N = Field.size(); i < N; ++i) {
343       GlobalVariable *GVE = SR.Serialize(Field[i]);
344       Constant *CE = ConstantExpr::getCast(GVE, EmptyTy);
345       ArrayElements.push_back(cast<Constant>(CE));
346     }
347     
348     Constant *CA = ConstantArray::get(AT, ArrayElements);
349     GlobalVariable *CAGV = new GlobalVariable(AT, true,
350                                               GlobalValue::InternalLinkage,
351                                               CA, "llvm.dbg.array",
352                                               SR.getModule());
353     Constant *CAE = ConstantExpr::getCast(CAGV, EmptyTy);
354     Elements.push_back(CAE);
355   }
356 };
357
358 //===----------------------------------------------------------------------===//
359 /// DIGetTypesVisitor - This DIVisitor gathers all the field types in
360 /// the supplied DebugInfoDesc.
361 class DIGetTypesVisitor : public DIVisitor {
362 private:
363   DISerializer &SR;                     // Active serializer.
364   std::vector<const Type*> &Fields;     // Type accumulator.
365   
366 public:
367   DIGetTypesVisitor(DISerializer &S, std::vector<const Type*> &F)
368   : DIVisitor()
369   , SR(S)
370   , Fields(F)
371   {}
372   
373   /// Apply - Set the value of each of the fields.
374   ///
375   virtual void Apply(int &Field) {
376     Fields.push_back(Type::IntTy);
377   }
378   virtual void Apply(unsigned &Field) {
379     Fields.push_back(Type::UIntTy);
380   }
381   virtual void Apply(int64_t &Field) {
382     Fields.push_back(Type::IntTy);
383   }
384   virtual void Apply(uint64_t &Field) {
385     Fields.push_back(Type::UIntTy);
386   }
387   virtual void Apply(bool &Field) {
388     Fields.push_back(Type::BoolTy);
389   }
390   virtual void Apply(std::string &Field) {
391     Fields.push_back(SR.getStrPtrType());
392   }
393   virtual void Apply(DebugInfoDesc *&Field) {
394     // FIXME - At some point should use specific type.
395     const PointerType *EmptyTy = SR.getEmptyStructPtrType();
396     Fields.push_back(EmptyTy);
397   }
398   virtual void Apply(GlobalVariable *&Field) {
399     const PointerType *EmptyTy = SR.getEmptyStructPtrType();
400     Fields.push_back(EmptyTy);
401   }
402   virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
403     const PointerType *EmptyTy = SR.getEmptyStructPtrType();
404     Fields.push_back(EmptyTy);
405   }
406 };
407
408 //===----------------------------------------------------------------------===//
409 /// DIVerifyVisitor - This DIVisitor verifies all the field types against
410 /// a constant initializer.
411 class DIVerifyVisitor : public DIVisitor {
412 private:
413   DIVerifier &VR;                       // Active verifier.
414   bool IsValid;                         // Validity status.
415   unsigned I;                           // Current operand index.
416   ConstantStruct *CI;                   // GlobalVariable constant initializer.
417   
418 public:
419   DIVerifyVisitor(DIVerifier &V, GlobalVariable *GV)
420   : DIVisitor()
421   , VR(V)
422   , IsValid(true)
423   , I(0)
424   , CI(cast<ConstantStruct>(GV->getInitializer()))
425   {
426   }
427   
428   // Accessors.
429   bool isValid() const { return IsValid; }
430   
431   /// Apply - Set the value of each of the fields.
432   ///
433   virtual void Apply(int &Field) {
434     Constant *C = CI->getOperand(I++);
435     IsValid = IsValid && isa<ConstantInt>(C);
436   }
437   virtual void Apply(unsigned &Field) {
438     Constant *C = CI->getOperand(I++);
439     IsValid = IsValid && isa<ConstantInt>(C);
440   }
441   virtual void Apply(int64_t &Field) {
442     Constant *C = CI->getOperand(I++);
443     IsValid = IsValid && isa<ConstantInt>(C);
444   }
445   virtual void Apply(uint64_t &Field) {
446     Constant *C = CI->getOperand(I++);
447     IsValid = IsValid && isa<ConstantInt>(C);
448   }
449   virtual void Apply(bool &Field) {
450     Constant *C = CI->getOperand(I++);
451     IsValid = IsValid && isa<ConstantBool>(C);
452   }
453   virtual void Apply(std::string &Field) {
454     Constant *C = CI->getOperand(I++);
455     IsValid = IsValid && isStringValue(C);
456   }
457   virtual void Apply(DebugInfoDesc *&Field) {
458     // FIXME - Prepare the correct descriptor.
459     Constant *C = CI->getOperand(I++);
460     IsValid = IsValid && isGlobalVariable(C);
461   }
462   virtual void Apply(GlobalVariable *&Field) {
463     Constant *C = CI->getOperand(I++);
464     IsValid = IsValid && isGlobalVariable(C);
465   }
466   virtual void Apply(std::vector<DebugInfoDesc *> &Field) {
467     Constant *C = CI->getOperand(I++);
468     IsValid = IsValid && isGlobalVariable(C);
469     if (!IsValid) return;
470
471     GlobalVariable *GV = getGlobalVariable(C);
472     IsValid = IsValid && GV && GV->hasInitializer();
473     if (!IsValid) return;
474     
475     ConstantArray *CA = dyn_cast<ConstantArray>(GV->getInitializer());
476     IsValid = IsValid && CA;
477     if (!IsValid) return;
478
479     for (unsigned i = 0, N = CA->getNumOperands(); IsValid && i < N; ++i) {
480       IsValid = IsValid && isGlobalVariable(CA->getOperand(i));
481       if (!IsValid) return;
482     
483       GlobalVariable *GVE = getGlobalVariable(CA->getOperand(i));
484       VR.Verify(GVE);
485     }
486   }
487 };
488
489
490 //===----------------------------------------------------------------------===//
491
492 /// TagFromGlobal - Returns the Tag number from a debug info descriptor
493 /// GlobalVariable.  
494 unsigned DebugInfoDesc::TagFromGlobal(GlobalVariable *GV) {
495   ConstantUInt *C = getUIntOperand(GV, 0);
496   return C ? (unsigned)C->getValue() : (unsigned)DW_TAG_invalid;
497 }
498
499 /// DescFactory - Create an instance of debug info descriptor based on Tag.
500 /// Return NULL if not a recognized Tag.
501 DebugInfoDesc *DebugInfoDesc::DescFactory(unsigned Tag) {
502   switch (Tag) {
503   case DW_TAG_anchor:           return new AnchorDesc();
504   case DW_TAG_compile_unit:     return new CompileUnitDesc();
505   case DW_TAG_variable:         return new GlobalVariableDesc();
506   case DW_TAG_subprogram:       return new SubprogramDesc();
507   case DW_TAG_base_type:        return new BasicTypeDesc();
508   case DW_TAG_typedef:
509   case DW_TAG_pointer_type:         
510   case DW_TAG_reference_type:
511   case DW_TAG_const_type:
512   case DW_TAG_volatile_type:         
513   case DW_TAG_restrict_type:    return new DerivedTypeDesc(Tag);
514   case DW_TAG_array_type:
515   case DW_TAG_structure_type:
516   case DW_TAG_union_type:
517   case DW_TAG_enumeration_type: return new CompositeTypeDesc(Tag);
518   case DW_TAG_subrange_type:    return new SubrangeDesc();
519   case DW_TAG_member:           return new DerivedTypeDesc(DW_TAG_member);
520   case DW_TAG_enumerator:       return new EnumeratorDesc();
521   default: break;
522   }
523   return NULL;
524 }
525
526 /// getLinkage - get linkage appropriate for this type of descriptor.
527 ///
528 GlobalValue::LinkageTypes DebugInfoDesc::getLinkage() const {
529   return GlobalValue::InternalLinkage;
530 }
531
532 /// ApplyToFields - Target the vistor to the fields of the descriptor.
533 ///
534 void DebugInfoDesc::ApplyToFields(DIVisitor *Visitor) {
535   Visitor->Apply(Tag);
536 }
537
538 //===----------------------------------------------------------------------===//
539
540 AnchorDesc::AnchorDesc()
541 : DebugInfoDesc(DW_TAG_anchor)
542 , Name("")
543 {}
544 AnchorDesc::AnchorDesc(const std::string &N)
545 : DebugInfoDesc(DW_TAG_anchor)
546 , Name(N)
547 {}
548
549 // Implement isa/cast/dyncast.
550 bool AnchorDesc::classof(const DebugInfoDesc *D) {
551   return D->getTag() == DW_TAG_anchor;
552 }
553   
554 /// getLinkage - get linkage appropriate for this type of descriptor.
555 ///
556 GlobalValue::LinkageTypes AnchorDesc::getLinkage() const {
557   return GlobalValue::LinkOnceLinkage;
558 }
559
560 /// ApplyToFields - Target the visitor to the fields of the TransUnitDesc.
561 ///
562 void AnchorDesc::ApplyToFields(DIVisitor *Visitor) {
563   DebugInfoDesc::ApplyToFields(Visitor);
564   
565   Visitor->Apply(Name);
566 }
567
568 /// getDescString - Return a string used to compose global names and labels.
569 ///
570 const char *AnchorDesc::getDescString() const {
571   return Name.c_str();
572 }
573
574 /// getTypeString - Return a string used to label this descriptors type.
575 ///
576 const char *AnchorDesc::getTypeString() const {
577   return "llvm.dbg.anchor.type";
578 }
579
580 #ifndef NDEBUG
581 void AnchorDesc::dump() {
582   std::cerr << getDescString() << " "
583             << "Tag(" << getTag() << "), "
584             << "Name(" << Name << ")\n";
585 }
586 #endif
587
588 //===----------------------------------------------------------------------===//
589
590 AnchoredDesc::AnchoredDesc(unsigned T)
591 : DebugInfoDesc(T)
592 , Anchor(NULL)
593 {}
594
595 /// ApplyToFields - Target the visitor to the fields of the AnchoredDesc.
596 ///
597 void AnchoredDesc::ApplyToFields(DIVisitor *Visitor) {
598   DebugInfoDesc::ApplyToFields(Visitor);
599
600   Visitor->Apply((DebugInfoDesc *&)Anchor);
601 }
602
603 //===----------------------------------------------------------------------===//
604
605 CompileUnitDesc::CompileUnitDesc()
606 : AnchoredDesc(DW_TAG_compile_unit)
607 , DebugVersion(LLVMDebugVersion)
608 , Language(0)
609 , FileName("")
610 , Directory("")
611 , Producer("")
612 {}
613
614 // Implement isa/cast/dyncast.
615 bool CompileUnitDesc::classof(const DebugInfoDesc *D) {
616   return D->getTag() == DW_TAG_compile_unit;
617 }
618
619 /// DebugVersionFromGlobal - Returns the version number from a compile unit
620 /// GlobalVariable.
621 unsigned CompileUnitDesc::DebugVersionFromGlobal(GlobalVariable *GV) {
622   ConstantUInt *C = getUIntOperand(GV, 2);
623   return C ? (unsigned)C->getValue() : (unsigned)DW_TAG_invalid;
624 }
625   
626 /// ApplyToFields - Target the visitor to the fields of the CompileUnitDesc.
627 ///
628 void CompileUnitDesc::ApplyToFields(DIVisitor *Visitor) {
629   AnchoredDesc::ApplyToFields(Visitor);
630
631   Visitor->Apply(DebugVersion);
632   Visitor->Apply(Language);
633   Visitor->Apply(FileName);
634   Visitor->Apply(Directory);
635   Visitor->Apply(Producer);
636 }
637
638 /// getDescString - Return a string used to compose global names and labels.
639 ///
640 const char *CompileUnitDesc::getDescString() const {
641   return "llvm.dbg.compile_unit";
642 }
643
644 /// getTypeString - Return a string used to label this descriptors type.
645 ///
646 const char *CompileUnitDesc::getTypeString() const {
647   return "llvm.dbg.compile_unit.type";
648 }
649
650 /// getAnchorString - Return a string used to label this descriptor's anchor.
651 ///
652 const char *CompileUnitDesc::getAnchorString() const {
653   return "llvm.dbg.compile_units";
654 }
655
656 #ifndef NDEBUG
657 void CompileUnitDesc::dump() {
658   std::cerr << getDescString() << " "
659             << "Tag(" << getTag() << "), "
660             << "Anchor(" << getAnchor() << "), "
661             << "DebugVersion(" << DebugVersion << "), "
662             << "Language(" << Language << "), "
663             << "FileName(\"" << FileName << "\"), "
664             << "Directory(\"" << Directory << "\"), "
665             << "Producer(\"" << Producer << "\")\n";
666 }
667 #endif
668
669 //===----------------------------------------------------------------------===//
670
671 TypeDesc::TypeDesc(unsigned T)
672 : DebugInfoDesc(T)
673 , Context(NULL)
674 , Name("")
675 , File(NULL)
676 , Size(0)
677 , Offset(0)
678 {}
679
680 /// ApplyToFields - Target the visitor to the fields of the TypeDesc.
681 ///
682 void TypeDesc::ApplyToFields(DIVisitor *Visitor) {
683   DebugInfoDesc::ApplyToFields(Visitor);
684   
685   Visitor->Apply(Context);
686   Visitor->Apply(Name);
687   Visitor->Apply((DebugInfoDesc *&)File);
688   Visitor->Apply(Line);
689   Visitor->Apply(Size);
690   Visitor->Apply(Offset);
691 }
692
693 /// getDescString - Return a string used to compose global names and labels.
694 ///
695 const char *TypeDesc::getDescString() const {
696   return "llvm.dbg.type";
697 }
698
699 /// getTypeString - Return a string used to label this descriptor's type.
700 ///
701 const char *TypeDesc::getTypeString() const {
702   return "llvm.dbg.type.type";
703 }
704
705 #ifndef NDEBUG
706 void TypeDesc::dump() {
707   std::cerr << getDescString() << " "
708             << "Tag(" << getTag() << "), "
709             << "Context(" << Context << "), "
710             << "Name(\"" << Name << "\"), "
711             << "File(" << File << "), "
712             << "Line(" << Line << "), "
713             << "Size(" << Size << "), "
714             << "Offset(" << Offset << ")\n";
715 }
716 #endif
717
718 //===----------------------------------------------------------------------===//
719
720 BasicTypeDesc::BasicTypeDesc()
721 : TypeDesc(DW_TAG_base_type)
722 , Encoding(0)
723 {}
724
725 // Implement isa/cast/dyncast.
726 bool BasicTypeDesc::classof(const DebugInfoDesc *D) {
727   return D->getTag() == DW_TAG_base_type;
728 }
729
730 /// ApplyToFields - Target the visitor to the fields of the BasicTypeDesc.
731 ///
732 void BasicTypeDesc::ApplyToFields(DIVisitor *Visitor) {
733   TypeDesc::ApplyToFields(Visitor);
734   
735   Visitor->Apply(Encoding);
736 }
737
738 /// getDescString - Return a string used to compose global names and labels.
739 ///
740 const char *BasicTypeDesc::getDescString() const {
741   return "llvm.dbg.basictype";
742 }
743
744 /// getTypeString - Return a string used to label this descriptor's type.
745 ///
746 const char *BasicTypeDesc::getTypeString() const {
747   return "llvm.dbg.basictype.type";
748 }
749
750 #ifndef NDEBUG
751 void BasicTypeDesc::dump() {
752   std::cerr << getDescString() << " "
753             << "Tag(" << getTag() << "), "
754             << "Context(" << getContext() << "), "
755             << "Name(\"" << getName() << "\"), "
756             << "Size(" << getSize() << "), "
757             << "Encoding(" << Encoding << ")\n";
758 }
759 #endif
760
761 //===----------------------------------------------------------------------===//
762
763 DerivedTypeDesc::DerivedTypeDesc(unsigned T)
764 : TypeDesc(T)
765 , FromType(NULL)
766 {}
767
768 // Implement isa/cast/dyncast.
769 bool DerivedTypeDesc::classof(const DebugInfoDesc *D) {
770   unsigned T =  D->getTag();
771   switch (T) {
772   case DW_TAG_typedef:
773   case DW_TAG_pointer_type:
774   case DW_TAG_reference_type:
775   case DW_TAG_const_type:
776   case DW_TAG_volatile_type:
777   case DW_TAG_restrict_type:
778   case DW_TAG_member:
779     return true;
780   default: break;
781   }
782   return false;
783 }
784
785 /// ApplyToFields - Target the visitor to the fields of the DerivedTypeDesc.
786 ///
787 void DerivedTypeDesc::ApplyToFields(DIVisitor *Visitor) {
788   TypeDesc::ApplyToFields(Visitor);
789   
790   Visitor->Apply((DebugInfoDesc *&)FromType);
791 }
792
793 /// getDescString - Return a string used to compose global names and labels.
794 ///
795 const char *DerivedTypeDesc::getDescString() const {
796   return "llvm.dbg.derivedtype";
797 }
798
799 /// getTypeString - Return a string used to label this descriptor's type.
800 ///
801 const char *DerivedTypeDesc::getTypeString() const {
802   return "llvm.dbg.derivedtype.type";
803 }
804
805 #ifndef NDEBUG
806 void DerivedTypeDesc::dump() {
807   std::cerr << getDescString() << " "
808             << "Tag(" << getTag() << "), "
809             << "Context(" << getContext() << "), "
810             << "Name(\"" << getName() << "\"), "
811             << "Size(" << getSize() << "), "
812             << "File(" << getFile() << "), "
813             << "Line(" << getLine() << "), "
814             << "FromType(" << FromType << ")\n";
815 }
816 #endif
817
818 //===----------------------------------------------------------------------===//
819
820 CompositeTypeDesc::CompositeTypeDesc(unsigned T)
821 : DerivedTypeDesc(T)
822 , Elements()
823 {}
824   
825 // Implement isa/cast/dyncast.
826 bool CompositeTypeDesc::classof(const DebugInfoDesc *D) {
827   unsigned T =  D->getTag();
828   switch (T) {
829   case DW_TAG_array_type:
830   case DW_TAG_structure_type:
831   case DW_TAG_union_type:
832   case DW_TAG_enumeration_type:
833     return true;
834   default: break;
835   }
836   return false;
837 }
838
839 /// ApplyToFields - Target the visitor to the fields of the CompositeTypeDesc.
840 ///
841 void CompositeTypeDesc::ApplyToFields(DIVisitor *Visitor) {
842   DerivedTypeDesc::ApplyToFields(Visitor);
843   
844   Visitor->Apply(Elements);
845 }
846
847 /// getDescString - Return a string used to compose global names and labels.
848 ///
849 const char *CompositeTypeDesc::getDescString() const {
850   return "llvm.dbg.compositetype";
851 }
852
853 /// getTypeString - Return a string used to label this descriptor's type.
854 ///
855 const char *CompositeTypeDesc::getTypeString() const {
856   return "llvm.dbg.compositetype.type";
857 }
858
859 #ifndef NDEBUG
860 void CompositeTypeDesc::dump() {
861   std::cerr << getDescString() << " "
862             << "Tag(" << getTag() << "), "
863             << "Context(" << getContext() << "), "
864             << "Name(\"" << getName() << "\"), "
865             << "Size(" << getSize() << "), "
866             << "File(" << getFile() << "), "
867             << "Line(" << getLine() << "), "
868             << "FromType(" << getFromType() << "), "
869             << "Elements.size(" << Elements.size() << ")\n";
870 }
871 #endif
872
873 //===----------------------------------------------------------------------===//
874
875 SubrangeDesc::SubrangeDesc()
876 : DebugInfoDesc(DW_TAG_subrange_type)
877 , Lo(0)
878 , Hi(0)
879 {}
880
881 // Implement isa/cast/dyncast.
882 bool SubrangeDesc::classof(const DebugInfoDesc *D) {
883   return D->getTag() == DW_TAG_subrange_type;
884 }
885
886 /// ApplyToFields - Target the visitor to the fields of the SubrangeDesc.
887 ///
888 void SubrangeDesc::ApplyToFields(DIVisitor *Visitor) {
889   DebugInfoDesc::ApplyToFields(Visitor);
890
891   Visitor->Apply(Lo);
892   Visitor->Apply(Hi);
893 }
894
895 /// getDescString - Return a string used to compose global names and labels.
896 ///
897 const char *SubrangeDesc::getDescString() const {
898   return "llvm.dbg.subrange";
899 }
900   
901 /// getTypeString - Return a string used to label this descriptor's type.
902 ///
903 const char *SubrangeDesc::getTypeString() const {
904   return "llvm.dbg.subrange.type";
905 }
906
907 #ifndef NDEBUG
908 void SubrangeDesc::dump() {
909   std::cerr << getDescString() << " "
910             << "Tag(" << getTag() << "), "
911             << "Lo(" << Lo << "), "
912             << "Hi(" << Hi << ")\n";
913 }
914 #endif
915
916 //===----------------------------------------------------------------------===//
917
918 EnumeratorDesc::EnumeratorDesc()
919 : DebugInfoDesc(DW_TAG_enumerator)
920 , Name("")
921 , Value(0)
922 {}
923
924 // Implement isa/cast/dyncast.
925 bool EnumeratorDesc::classof(const DebugInfoDesc *D) {
926   return D->getTag() == DW_TAG_enumerator;
927 }
928
929 /// ApplyToFields - Target the visitor to the fields of the EnumeratorDesc.
930 ///
931 void EnumeratorDesc::ApplyToFields(DIVisitor *Visitor) {
932   DebugInfoDesc::ApplyToFields(Visitor);
933
934   Visitor->Apply(Name);
935   Visitor->Apply(Value);
936 }
937
938 /// getDescString - Return a string used to compose global names and labels.
939 ///
940 const char *EnumeratorDesc::getDescString() const {
941   return "llvm.dbg.enumerator";
942 }
943   
944 /// getTypeString - Return a string used to label this descriptor's type.
945 ///
946 const char *EnumeratorDesc::getTypeString() const {
947   return "llvm.dbg.enumerator.type";
948 }
949
950 #ifndef NDEBUG
951 void EnumeratorDesc::dump() {
952   std::cerr << getDescString() << " "
953             << "Tag(" << getTag() << "), "
954             << "Name(" << Name << "), "
955             << "Value(" << Value << ")\n";
956 }
957 #endif
958
959 //===----------------------------------------------------------------------===//
960
961 GlobalDesc::GlobalDesc(unsigned T)
962 : AnchoredDesc(T)
963 , Context(0)
964 , Name("")
965 , TyDesc(NULL)
966 , IsStatic(false)
967 , IsDefinition(false)
968 {}
969
970 /// ApplyToFields - Target the visitor to the fields of the global.
971 ///
972 void GlobalDesc::ApplyToFields(DIVisitor *Visitor) {
973   AnchoredDesc::ApplyToFields(Visitor);
974
975   Visitor->Apply(Context);
976   Visitor->Apply(Name);
977   Visitor->Apply((DebugInfoDesc *&)TyDesc);
978   Visitor->Apply(IsStatic);
979   Visitor->Apply(IsDefinition);
980 }
981
982 //===----------------------------------------------------------------------===//
983
984 GlobalVariableDesc::GlobalVariableDesc()
985 : GlobalDesc(DW_TAG_variable)
986 , Global(NULL)
987 {}
988
989 // Implement isa/cast/dyncast.
990 bool GlobalVariableDesc::classof(const DebugInfoDesc *D) {
991   return D->getTag() == DW_TAG_variable; 
992 }
993
994 /// ApplyToFields - Target the visitor to the fields of the GlobalVariableDesc.
995 ///
996 void GlobalVariableDesc::ApplyToFields(DIVisitor *Visitor) {
997   GlobalDesc::ApplyToFields(Visitor);
998
999   Visitor->Apply(Global);
1000   Visitor->Apply(Line);
1001 }
1002
1003 /// getDescString - Return a string used to compose global names and labels.
1004 ///
1005 const char *GlobalVariableDesc::getDescString() const {
1006   return "llvm.dbg.global_variable";
1007 }
1008
1009 /// getTypeString - Return a string used to label this descriptors type.
1010 ///
1011 const char *GlobalVariableDesc::getTypeString() const {
1012   return "llvm.dbg.global_variable.type";
1013 }
1014
1015 /// getAnchorString - Return a string used to label this descriptor's anchor.
1016 ///
1017 const char *GlobalVariableDesc::getAnchorString() const {
1018   return "llvm.dbg.global_variables";
1019 }
1020
1021 #ifndef NDEBUG
1022 void GlobalVariableDesc::dump() {
1023   std::cerr << getDescString() << " "
1024             << "Tag(" << getTag() << "), "
1025             << "Anchor(" << getAnchor() << "), "
1026             << "Name(\"" << getName() << "\"), "
1027             << "Type(\"" << getTypeDesc() << "\"), "
1028             << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1029             << "IsDefinition(" << (isDefinition() ? "true" : "false") << "), "
1030             << "Global(" << Global << "), "
1031             << "Line(" << Line << ")\n";
1032 }
1033 #endif
1034
1035 //===----------------------------------------------------------------------===//
1036
1037 SubprogramDesc::SubprogramDesc()
1038 : GlobalDesc(DW_TAG_subprogram)
1039 {}
1040
1041 // Implement isa/cast/dyncast.
1042 bool SubprogramDesc::classof(const DebugInfoDesc *D) {
1043   return D->getTag() == DW_TAG_subprogram;
1044 }
1045
1046 /// ApplyToFields - Target the visitor to the fields of the
1047 /// SubprogramDesc.
1048 void SubprogramDesc::ApplyToFields(DIVisitor *Visitor) {
1049   GlobalDesc::ApplyToFields(Visitor);
1050 }
1051
1052 /// getDescString - Return a string used to compose global names and labels.
1053 ///
1054 const char *SubprogramDesc::getDescString() const {
1055   return "llvm.dbg.subprogram";
1056 }
1057
1058 /// getTypeString - Return a string used to label this descriptors type.
1059 ///
1060 const char *SubprogramDesc::getTypeString() const {
1061   return "llvm.dbg.subprogram.type";
1062 }
1063
1064 /// getAnchorString - Return a string used to label this descriptor's anchor.
1065 ///
1066 const char *SubprogramDesc::getAnchorString() const {
1067   return "llvm.dbg.subprograms";
1068 }
1069
1070 #ifndef NDEBUG
1071 void SubprogramDesc::dump() {
1072   std::cerr << getDescString() << " "
1073             << "Tag(" << getTag() << "), "
1074             << "Anchor(" << getAnchor() << "), "
1075             << "Name(\"" << getName() << "\"), "
1076             << "Type(\"" << getTypeDesc() << "\"), "
1077             << "IsStatic(" << (isStatic() ? "true" : "false") << "), "
1078             << "IsDefinition(" << (isDefinition() ? "true" : "false") << ")\n";
1079 }
1080 #endif
1081
1082 //===----------------------------------------------------------------------===//
1083
1084 DebugInfoDesc *DIDeserializer::Deserialize(Value *V) {
1085   return Deserialize(getGlobalVariable(V));
1086 }
1087 DebugInfoDesc *DIDeserializer::Deserialize(GlobalVariable *GV) {
1088   // Handle NULL.
1089   if (!GV) return NULL;
1090
1091   // Check to see if it has been already deserialized.
1092   DebugInfoDesc *&Slot = GlobalDescs[GV];
1093   if (Slot) return Slot;
1094
1095   // Get the Tag from the global.
1096   unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1097   
1098   // Get the debug version if a compile unit.
1099   if (Tag == DW_TAG_compile_unit) {
1100     DebugVersion = CompileUnitDesc::DebugVersionFromGlobal(GV);
1101   }
1102   
1103   // Create an empty instance of the correct sort.
1104   Slot = DebugInfoDesc::DescFactory(Tag);
1105   assert(Slot && "Unknown Tag");
1106   
1107   // Deserialize the fields.
1108   DIDeserializeVisitor DRAM(*this, GV);
1109   DRAM.ApplyToFields(Slot);
1110   
1111   return Slot;
1112 }
1113
1114 //===----------------------------------------------------------------------===//
1115
1116 /// getStrPtrType - Return a "sbyte *" type.
1117 ///
1118 const PointerType *DISerializer::getStrPtrType() {
1119   // If not already defined.
1120   if (!StrPtrTy) {
1121     // Construct the pointer to signed bytes.
1122     StrPtrTy = PointerType::get(Type::SByteTy);
1123   }
1124   
1125   return StrPtrTy;
1126 }
1127
1128 /// getEmptyStructPtrType - Return a "{ }*" type.
1129 ///
1130 const PointerType *DISerializer::getEmptyStructPtrType() {
1131   // If not already defined.
1132   if (!EmptyStructPtrTy) {
1133     // Construct the empty structure type.
1134     const StructType *EmptyStructTy =
1135                                     StructType::get(std::vector<const Type*>());
1136     // Construct the pointer to empty structure type.
1137     EmptyStructPtrTy = PointerType::get(EmptyStructTy);
1138   }
1139   
1140   return EmptyStructPtrTy;
1141 }
1142
1143 /// getTagType - Return the type describing the specified descriptor (via tag.)
1144 ///
1145 const StructType *DISerializer::getTagType(DebugInfoDesc *DD) {
1146   // Attempt to get the previously defined type.
1147   StructType *&Ty = TagTypes[DD->getTag()];
1148   
1149   // If not already defined.
1150   if (!Ty) {
1151     // Set up fields vector.
1152     std::vector<const Type*> Fields;
1153     // Get types of fields.
1154     DIGetTypesVisitor GTAM(*this, Fields);
1155     GTAM.ApplyToFields(DD);
1156
1157     // Construct structured type.
1158     Ty = StructType::get(Fields);
1159     
1160     // Register type name with module.
1161     M->addTypeName(DD->getTypeString(), Ty);
1162   }
1163   
1164   return Ty;
1165 }
1166
1167 /// getString - Construct the string as constant string global.
1168 ///
1169 Constant *DISerializer::getString(const std::string &String) {
1170   // Check string cache for previous edition.
1171   Constant *&Slot = StringCache[String];
1172   // return Constant if previously defined.
1173   if (Slot) return Slot;
1174   // Construct string as an llvm constant.
1175   Constant *ConstStr = ConstantArray::get(String);
1176   // Otherwise create and return a new string global.
1177   GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true,
1178                                              GlobalVariable::InternalLinkage,
1179                                              ConstStr, "str", M);
1180   // Convert to generic string pointer.
1181   Slot = ConstantExpr::getCast(StrGV, getStrPtrType());
1182   return Slot;
1183   
1184 }
1185
1186 /// Serialize - Recursively cast the specified descriptor into a GlobalVariable
1187 /// so that it can be serialized to a .bc or .ll file.
1188 GlobalVariable *DISerializer::Serialize(DebugInfoDesc *DD) {
1189   // Check if the DebugInfoDesc is already in the map.
1190   GlobalVariable *&Slot = DescGlobals[DD];
1191   
1192   // See if DebugInfoDesc exists, if so return prior GlobalVariable.
1193   if (Slot) return Slot;
1194   
1195   // Get the type associated with the Tag.
1196   const StructType *Ty = getTagType(DD);
1197
1198   // Create the GlobalVariable early to prevent infinite recursion.
1199   GlobalVariable *GV = new GlobalVariable(Ty, true, DD->getLinkage(),
1200                                           NULL, DD->getDescString(), M);
1201
1202   // Insert new GlobalVariable in DescGlobals map.
1203   Slot = GV;
1204  
1205   // Set up elements vector
1206   std::vector<Constant*> Elements;
1207   // Add fields.
1208   DISerializeVisitor SRAM(*this, Elements);
1209   SRAM.ApplyToFields(DD);
1210   
1211   // Set the globals initializer.
1212   GV->setInitializer(ConstantStruct::get(Ty, Elements));
1213   
1214   return GV;
1215 }
1216
1217 //===----------------------------------------------------------------------===//
1218
1219 /// markVisited - Return true if the GlobalVariable hase been "seen" before.
1220 /// Mark visited otherwise.
1221 bool DIVerifier::markVisited(GlobalVariable *GV) {
1222   // Check if the GlobalVariable is already in the Visited set.
1223   std::set<GlobalVariable *>::iterator VI = Visited.lower_bound(GV);
1224   
1225   // See if GlobalVariable exists.
1226   bool Exists = VI != Visited.end() && *VI == GV;
1227
1228   // Insert in set.
1229   if (!Exists) Visited.insert(VI, GV);
1230   
1231   return Exists;
1232 }
1233
1234 /// Verify - Return true if the GlobalVariable appears to be a valid
1235 /// serialization of a DebugInfoDesc.
1236 bool DIVerifier::Verify(Value *V) {
1237   return Verify(getGlobalVariable(V));
1238 }
1239 bool DIVerifier::Verify(GlobalVariable *GV) {
1240   // Check if seen before.
1241   if (markVisited(GV)) return true;
1242   
1243   // Get the Tag
1244   unsigned Tag = DebugInfoDesc::TagFromGlobal(GV);
1245   if (Tag == DW_TAG_invalid) return false;
1246
1247   // If a compile unit we need the debug version.
1248   if (Tag == DW_TAG_compile_unit) {
1249     DebugVersion = CompileUnitDesc::DebugVersionFromGlobal(GV);
1250     if (DebugVersion == DW_TAG_invalid) return false;
1251   }
1252
1253   // Construct an empty DebugInfoDesc.
1254   DebugInfoDesc *DD = DebugInfoDesc::DescFactory(Tag);
1255   if (!DD) return false;
1256   
1257   // Get the initializer constant.
1258   ConstantStruct *CI = cast<ConstantStruct>(GV->getInitializer());
1259   
1260   // Get the operand count.
1261   unsigned N = CI->getNumOperands();
1262   
1263   // Get the field count.
1264   unsigned &Slot = Counts[Tag];
1265   if (!Slot) {
1266     // Check the operand count to the field count
1267     DICountVisitor CTAM;
1268     CTAM.ApplyToFields(DD);
1269     Slot = CTAM.getCount();
1270   }
1271   
1272   // Field count must equal operand count.
1273   if (Slot != N) {
1274     delete DD;
1275     return false;
1276   }
1277   
1278   // Check each field for valid type.
1279   DIVerifyVisitor VRAM(*this, GV);
1280   VRAM.ApplyToFields(DD);
1281   
1282   // Release empty DebugInfoDesc.
1283   delete DD;
1284   
1285   // Return result of field tests.
1286   return VRAM.isValid();
1287 }
1288
1289 //===----------------------------------------------------------------------===//
1290
1291
1292 MachineDebugInfo::MachineDebugInfo()
1293 : DR()
1294 , CompileUnits()
1295 , Directories()
1296 , SourceFiles()
1297 , Lines()
1298 {
1299   
1300 }
1301 MachineDebugInfo::~MachineDebugInfo() {
1302
1303 }
1304
1305 /// doInitialization - Initialize the debug state for a new module.
1306 ///
1307 bool MachineDebugInfo::doInitialization() {
1308   return false;
1309 }
1310
1311 /// doFinalization - Tear down the debug state after completion of a module.
1312 ///
1313 bool MachineDebugInfo::doFinalization() {
1314   return false;
1315 }
1316
1317 /// getDescFor - Convert a Value to a debug information descriptor.
1318 ///
1319 // FIXME - use new Value type when available.
1320 DebugInfoDesc *MachineDebugInfo::getDescFor(Value *V) {
1321   return DR.Deserialize(V);
1322 }
1323
1324 /// Verify - Verify that a Value is debug information descriptor.
1325 ///
1326 bool MachineDebugInfo::Verify(Value *V) {
1327   DIVerifier VR;
1328   return VR.Verify(V);
1329 }
1330
1331 /// AnalyzeModule - Scan the module for global debug information.
1332 ///
1333 void MachineDebugInfo::AnalyzeModule(Module &M) {
1334   SetupCompileUnits(M);
1335 }
1336
1337 /// SetupCompileUnits - Set up the unique vector of compile units.
1338 ///
1339 void MachineDebugInfo::SetupCompileUnits(Module &M) {
1340   std::vector<CompileUnitDesc *>CU = getAnchoredDescriptors<CompileUnitDesc>(M);
1341   
1342   for (unsigned i = 0, N = CU.size(); i < N; i++) {
1343     CompileUnits.insert(CU[i]);
1344   }
1345 }
1346
1347 /// getCompileUnits - Return a vector of debug compile units.
1348 ///
1349 const UniqueVector<CompileUnitDesc *> MachineDebugInfo::getCompileUnits()const{
1350   return CompileUnits;
1351 }
1352
1353 /// getGlobalVariablesUsing - Return all of the GlobalVariables that use the
1354 /// named GlobalVariable.
1355 std::vector<GlobalVariable*>
1356 MachineDebugInfo::getGlobalVariablesUsing(Module &M,
1357                                           const std::string &RootName) {
1358   return ::getGlobalVariablesUsing(M, RootName);
1359 }