1 //===--- llvm/Analysis/DebugInfo.h - Debug Information Helpers --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines a bunch of datatypes that are useful for creating and
11 // walking debug info in LLVM IR form. They essentially provide wrappers around
12 // the information in the global variables that's needed when constructing the
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_ANALYSIS_DEBUGINFO_H
18 #define LLVM_ANALYSIS_DEBUGINFO_H
20 #include "llvm/Target/TargetMachine.h"
21 #include "llvm/ADT/StringMap.h"
22 #include "llvm/ADT/DenseMap.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/ADT/SmallPtrSet.h"
25 #include "llvm/Support/Dwarf.h"
35 struct DbgStopPointInst;
36 struct DbgDeclareInst;
37 struct DbgFuncStartInst;
38 struct DbgRegionStartInst;
39 struct DbgRegionEndInst;
41 struct DebugLocTracker;
47 GlobalVariable *DbgGV;
49 /// DIDescriptor constructor. If the specified GV is non-null, this checks
50 /// to make sure that the tag in the descriptor matches 'RequiredTag'. If
51 /// not, the debug info is corrupt and we ignore it.
52 DIDescriptor(GlobalVariable *GV, unsigned RequiredTag);
54 const std::string &getStringField(unsigned Elt, std::string &Result) const;
55 unsigned getUnsignedField(unsigned Elt) const {
56 return (unsigned)getUInt64Field(Elt);
58 uint64_t getUInt64Field(unsigned Elt) const;
59 DIDescriptor getDescriptorField(unsigned Elt) const;
61 template <typename DescTy>
62 DescTy getFieldAs(unsigned Elt) const {
63 return DescTy(getDescriptorField(Elt).getGV());
66 GlobalVariable *getGlobalVariableField(unsigned Elt) const;
69 explicit DIDescriptor() : DbgGV(0) {}
70 explicit DIDescriptor(GlobalVariable *GV) : DbgGV(GV) {}
72 bool isNull() const { return DbgGV == 0; }
74 GlobalVariable *getGV() const { return DbgGV; }
76 unsigned getVersion() const {
77 return getUnsignedField(0) & LLVMDebugVersionMask;
80 unsigned getTag() const {
81 return getUnsignedField(0) & ~LLVMDebugVersionMask;
84 /// ValidDebugInfo - Return true if V represents valid debug info value.
85 static bool ValidDebugInfo(Value *V, CodeGenOpt::Level OptLevel);
87 /// dump - print descriptor.
91 /// DISubrange - This is used to represent ranges, for array bounds.
92 class DISubrange : public DIDescriptor {
94 explicit DISubrange(GlobalVariable *GV = 0)
95 : DIDescriptor(GV, dwarf::DW_TAG_subrange_type) {}
97 int64_t getLo() const { return (int64_t)getUInt64Field(1); }
98 int64_t getHi() const { return (int64_t)getUInt64Field(2); }
101 /// DIArray - This descriptor holds an array of descriptors.
102 class DIArray : public DIDescriptor {
104 explicit DIArray(GlobalVariable *GV = 0) : DIDescriptor(GV) {}
106 unsigned getNumElements() const;
107 DIDescriptor getElement(unsigned Idx) const {
108 return getDescriptorField(Idx);
112 /// DICompileUnit - A wrapper for a compile unit.
113 class DICompileUnit : public DIDescriptor {
115 explicit DICompileUnit(GlobalVariable *GV = 0)
116 : DIDescriptor(GV, dwarf::DW_TAG_compile_unit) {}
118 unsigned getLanguage() const { return getUnsignedField(2); }
119 const std::string &getFilename(std::string &F) const {
120 return getStringField(3, F);
122 const std::string &getDirectory(std::string &F) const {
123 return getStringField(4, F);
125 const std::string &getProducer(std::string &F) const {
126 return getStringField(5, F);
129 /// isMain - Each input file is encoded as a separate compile unit in LLVM
130 /// debugging information output. However, many target specific tool chains
131 /// prefer to encode only one compile unit in an object file. In this
132 /// situation, the LLVM code generator will include debugging information
133 /// entities in the compile unit that is marked as main compile unit. The
134 /// code generator accepts maximum one main compile unit per module. If a
135 /// module does not contain any main compile unit then the code generator
136 /// will emit multiple compile units in the output object file.
138 bool isMain() const { return getUnsignedField(6); }
139 bool isOptimized() const { return getUnsignedField(7); }
140 const std::string &getFlags(std::string &F) const {
141 return getStringField(8, F);
143 unsigned getRunTimeVersion() const { return getUnsignedField(9); }
145 /// Verify - Verify that a compile unit is well formed.
148 /// dump - print compile unit.
152 /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
153 /// FIXME: it seems strange that this doesn't have either a reference to the
154 /// type/precision or a file/line pair for location info.
155 class DIEnumerator : public DIDescriptor {
157 explicit DIEnumerator(GlobalVariable *GV = 0)
158 : DIDescriptor(GV, dwarf::DW_TAG_enumerator) {}
160 const std::string &getName(std::string &F) const {
161 return getStringField(1, F);
163 uint64_t getEnumValue() const { return getUInt64Field(2); }
166 /// DIType - This is a wrapper for a type.
167 /// FIXME: Types should be factored much better so that CV qualifiers and
168 /// others do not require a huge and empty descriptor full of zeros.
169 class DIType : public DIDescriptor {
172 FlagPrivate = 1 << 0,
173 FlagProtected = 1 << 1,
174 FlagFwdDecl = 1 << 2,
175 FlagAppleBlock = 1 << 3
179 DIType(GlobalVariable *GV, unsigned Tag) : DIDescriptor(GV, Tag) {}
180 // This ctor is used when the Tag has already been validated by a derived
182 DIType(GlobalVariable *GV, bool, bool) : DIDescriptor(GV) {}
185 /// isDerivedType - Return true if the specified tag is legal for
187 static bool isDerivedType(unsigned TAG);
189 /// isCompositeType - Return true if the specified tag is legal for
191 static bool isCompositeType(unsigned TAG);
193 /// isBasicType - Return true if the specified tag is legal for
195 static bool isBasicType(unsigned TAG) {
196 return TAG == dwarf::DW_TAG_base_type;
199 /// Verify - Verify that a type descriptor is well formed.
202 explicit DIType(GlobalVariable *GV);
206 DIDescriptor getContext() const { return getDescriptorField(1); }
207 const std::string &getName(std::string &F) const {
208 return getStringField(2, F);
210 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
211 unsigned getLineNumber() const { return getUnsignedField(4); }
212 uint64_t getSizeInBits() const { return getUInt64Field(5); }
213 uint64_t getAlignInBits() const { return getUInt64Field(6); }
214 // FIXME: Offset is only used for DW_TAG_member nodes. Making every type
215 // carry this is just plain insane.
216 uint64_t getOffsetInBits() const { return getUInt64Field(7); }
217 unsigned getFlags() const { return getUnsignedField(8); }
218 bool isPrivate() const {
219 return (getFlags() & FlagPrivate) != 0;
221 bool isProtected() const {
222 return (getFlags() & FlagProtected) != 0;
224 bool isForwardDecl() const {
225 return (getFlags() & FlagFwdDecl) != 0;
227 // isAppleBlock - Return true if this is the Apple Blocks extension.
228 bool isAppleBlockExtension() const {
229 return (getFlags() & FlagAppleBlock) != 0;
232 /// dump - print type.
236 /// DIBasicType - A basic type, like 'int' or 'float'.
237 class DIBasicType : public DIType {
239 explicit DIBasicType(GlobalVariable *GV)
240 : DIType(GV, dwarf::DW_TAG_base_type) {}
242 unsigned getEncoding() const { return getUnsignedField(9); }
244 /// dump - print basic type.
248 /// DIDerivedType - A simple derived type, like a const qualified type,
249 /// a typedef, a pointer or reference, etc.
250 class DIDerivedType : public DIType {
252 explicit DIDerivedType(GlobalVariable *GV, bool, bool)
253 : DIType(GV, true, true) {}
255 explicit DIDerivedType(GlobalVariable *GV)
256 : DIType(GV, true, true) {
257 if (GV && !isDerivedType(getTag()))
261 DIType getTypeDerivedFrom() const { return getFieldAs<DIType>(9); }
263 /// getOriginalTypeSize - If this type is derived from a base type then
264 /// return base type size.
265 uint64_t getOriginalTypeSize() const;
266 /// dump - print derived type.
269 /// replaceAllUsesWith - Replace all uses of debug info referenced by
270 /// this descriptor. After this completes, the current debug info value
272 void replaceAllUsesWith(DIDescriptor &D);
275 /// DICompositeType - This descriptor holds a type that can refer to multiple
276 /// other types, like a function or struct.
277 /// FIXME: Why is this a DIDerivedType??
278 class DICompositeType : public DIDerivedType {
280 explicit DICompositeType(GlobalVariable *GV)
281 : DIDerivedType(GV, true, true) {
282 if (GV && !isCompositeType(getTag()))
286 DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
287 unsigned getRunTimeLang() const { return getUnsignedField(11); }
289 /// Verify - Verify that a composite type descriptor is well formed.
292 /// dump - print composite type.
296 /// DIGlobal - This is a common class for global variables and subprograms.
297 class DIGlobal : public DIDescriptor {
299 explicit DIGlobal(GlobalVariable *GV, unsigned RequiredTag)
300 : DIDescriptor(GV, RequiredTag) {}
302 /// isSubprogram - Return true if the specified tag is legal for
304 static bool isSubprogram(unsigned TAG) {
305 return TAG == dwarf::DW_TAG_subprogram;
308 /// isGlobalVariable - Return true if the specified tag is legal for
309 /// DIGlobalVariable.
310 static bool isGlobalVariable(unsigned TAG) {
311 return TAG == dwarf::DW_TAG_variable;
315 virtual ~DIGlobal() {}
317 DIDescriptor getContext() const { return getDescriptorField(2); }
318 const std::string &getName(std::string &F) const {
319 return getStringField(3, F);
321 const std::string &getDisplayName(std::string &F) const {
322 return getStringField(4, F);
324 const std::string &getLinkageName(std::string &F) const {
325 return getStringField(5, F);
327 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(6); }
328 unsigned getLineNumber() const { return getUnsignedField(7); }
329 DIType getType() const { return getFieldAs<DIType>(8); }
331 /// isLocalToUnit - Return true if this subprogram is local to the current
332 /// compile unit, like 'static' in C.
333 unsigned isLocalToUnit() const { return getUnsignedField(9); }
334 unsigned isDefinition() const { return getUnsignedField(10); }
336 /// dump - print global.
340 /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
341 class DISubprogram : public DIGlobal {
343 explicit DISubprogram(GlobalVariable *GV = 0)
344 : DIGlobal(GV, dwarf::DW_TAG_subprogram) {}
346 DICompositeType getType() const { return getFieldAs<DICompositeType>(8); }
348 /// getReturnTypeName - Subprogram return types are encoded either as
349 /// DIType or as DICompositeType.
350 const std::string &getReturnTypeName(std::string &F) const {
351 DICompositeType DCT(getFieldAs<DICompositeType>(8));
353 DIArray A = DCT.getTypeArray();
354 DIType T(A.getElement(0).getGV());
357 DIType T(getFieldAs<DIType>(8));
361 /// Verify - Verify that a subprogram descriptor is well formed.
364 /// dump - print subprogram.
367 /// describes - Return true if this subprogram provides debugging
368 /// information for the function F.
369 bool describes(const Function *F);
372 /// DIGlobalVariable - This is a wrapper for a global variable.
373 class DIGlobalVariable : public DIGlobal {
375 explicit DIGlobalVariable(GlobalVariable *GV = 0)
376 : DIGlobal(GV, dwarf::DW_TAG_variable) {}
378 GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
380 /// Verify - Verify that a global variable descriptor is well formed.
383 /// dump - print global variable.
387 /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
389 class DIVariable : public DIDescriptor {
391 explicit DIVariable(GlobalVariable *GV = 0)
393 if (GV && !isVariable(getTag()))
397 DIDescriptor getContext() const { return getDescriptorField(1); }
398 const std::string &getName(std::string &F) const {
399 return getStringField(2, F);
401 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
402 unsigned getLineNumber() const { return getUnsignedField(4); }
403 DIType getType() const { return getFieldAs<DIType>(5); }
405 /// isVariable - Return true if the specified tag is legal for DIVariable.
406 static bool isVariable(unsigned Tag);
408 /// Verify - Verify that a variable descriptor is well formed.
411 /// dump - print variable.
415 /// DIBlock - This is a wrapper for a block (e.g. a function, scope, etc).
416 class DIBlock : public DIDescriptor {
418 explicit DIBlock(GlobalVariable *GV = 0)
419 : DIDescriptor(GV, dwarf::DW_TAG_lexical_block) {}
421 DIDescriptor getContext() const { return getDescriptorField(1); }
424 /// DIFactory - This object assists with the construction of the various
428 LLVMContext& VMContext;
430 // Cached values for uniquing and faster lookups.
431 const Type *EmptyStructPtr; // "{}*".
432 Function *StopPointFn; // llvm.dbg.stoppoint
433 Function *FuncStartFn; // llvm.dbg.func.start
434 Function *RegionStartFn; // llvm.dbg.region.start
435 Function *RegionEndFn; // llvm.dbg.region.end
436 Function *DeclareFn; // llvm.dbg.declare
437 StringMap<Constant*> StringCache;
438 DenseMap<Constant*, DIDescriptor> SimpleConstantCache;
440 DIFactory(const DIFactory &); // DO NOT IMPLEMENT
441 void operator=(const DIFactory&); // DO NOT IMPLEMENT
443 explicit DIFactory(Module &m);
445 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
446 /// This implicitly uniques the arrays created.
447 DIArray GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys);
449 /// GetOrCreateSubrange - Create a descriptor for a value range. This
450 /// implicitly uniques the values returned.
451 DISubrange GetOrCreateSubrange(int64_t Lo, int64_t Hi);
453 /// CreateCompileUnit - Create a new descriptor for the specified compile
455 DICompileUnit CreateCompileUnit(unsigned LangID,
456 const std::string &Filename,
457 const std::string &Directory,
458 const std::string &Producer,
460 bool isOptimized = false,
461 const char *Flags = "",
462 unsigned RunTimeVer = 0);
464 /// CreateEnumerator - Create a single enumerator value.
465 DIEnumerator CreateEnumerator(const std::string &Name, uint64_t Val);
467 /// CreateBasicType - Create a basic type like int, float, etc.
468 DIBasicType CreateBasicType(DIDescriptor Context, const std::string &Name,
469 DICompileUnit CompileUnit, unsigned LineNumber,
470 uint64_t SizeInBits, uint64_t AlignInBits,
471 uint64_t OffsetInBits, unsigned Flags,
474 /// CreateDerivedType - Create a derived type like const qualified type,
475 /// pointer, typedef, etc.
476 DIDerivedType CreateDerivedType(unsigned Tag, DIDescriptor Context,
477 const std::string &Name,
478 DICompileUnit CompileUnit,
480 uint64_t SizeInBits, uint64_t AlignInBits,
481 uint64_t OffsetInBits, unsigned Flags,
484 /// CreateCompositeType - Create a composite type like array, struct, etc.
485 DICompositeType CreateCompositeType(unsigned Tag, DIDescriptor Context,
486 const std::string &Name,
487 DICompileUnit CompileUnit,
490 uint64_t AlignInBits,
491 uint64_t OffsetInBits, unsigned Flags,
494 unsigned RunTimeLang = 0);
496 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
497 /// See comments in DISubprogram for descriptions of these fields.
498 DISubprogram CreateSubprogram(DIDescriptor Context, const std::string &Name,
499 const std::string &DisplayName,
500 const std::string &LinkageName,
501 DICompileUnit CompileUnit, unsigned LineNo,
502 DIType Type, bool isLocalToUnit,
505 /// CreateGlobalVariable - Create a new descriptor for the specified global.
507 CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
508 const std::string &DisplayName,
509 const std::string &LinkageName,
510 DICompileUnit CompileUnit,
511 unsigned LineNo, DIType Type, bool isLocalToUnit,
512 bool isDefinition, llvm::GlobalVariable *GV);
514 /// CreateVariable - Create a new descriptor for the specified variable.
515 DIVariable CreateVariable(unsigned Tag, DIDescriptor Context,
516 const std::string &Name,
517 DICompileUnit CompileUnit, unsigned LineNo,
520 /// CreateBlock - This creates a descriptor for a lexical block with the
521 /// specified parent context.
522 DIBlock CreateBlock(DIDescriptor Context);
524 /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
525 /// inserting it at the end of the specified basic block.
526 void InsertStopPoint(DICompileUnit CU, unsigned LineNo, unsigned ColNo,
529 /// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
530 /// mark the start of the specified subprogram.
531 void InsertSubprogramStart(DISubprogram SP, BasicBlock *BB);
533 /// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
534 /// mark the start of a region for the specified scoping descriptor.
535 void InsertRegionStart(DIDescriptor D, BasicBlock *BB);
537 /// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
538 /// mark the end of a region for the specified scoping descriptor.
539 void InsertRegionEnd(DIDescriptor D, BasicBlock *BB);
541 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
542 void InsertDeclare(llvm::Value *Storage, DIVariable D, BasicBlock *BB);
545 Constant *GetTagConstant(unsigned TAG);
546 Constant *GetStringConstant(const std::string &String);
548 /// getCastToEmpty - Return the descriptor as a Constant* with type '{}*'.
549 Constant *getCastToEmpty(DIDescriptor D);
552 /// Finds the stoppoint coressponding to this instruction, that is the
553 /// stoppoint that dominates this instruction
554 const DbgStopPointInst *findStopPoint(const Instruction *Inst);
556 /// Finds the stoppoint corresponding to first real (non-debug intrinsic)
557 /// instruction in this Basic Block, and returns the stoppoint for it.
558 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB);
560 /// Finds the dbg.declare intrinsic corresponding to this value if any.
561 /// It looks through pointer casts too.
562 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts = true);
564 /// Find the debug info descriptor corresponding to this global variable.
565 Value *findDbgGlobalDeclare(GlobalVariable *V);
567 bool getLocationInfo(const Value *V, std::string &DisplayName,
568 std::string &Type, unsigned &LineNo, std::string &File,
571 /// isValidDebugInfoIntrinsic - Return true if SPI is a valid debug
573 bool isValidDebugInfoIntrinsic(DbgStopPointInst &SPI,
574 CodeGenOpt::Level OptLev);
576 /// isValidDebugInfoIntrinsic - Return true if FSI is a valid debug
578 bool isValidDebugInfoIntrinsic(DbgFuncStartInst &FSI,
579 CodeGenOpt::Level OptLev);
581 /// isValidDebugInfoIntrinsic - Return true if RSI is a valid debug
583 bool isValidDebugInfoIntrinsic(DbgRegionStartInst &RSI,
584 CodeGenOpt::Level OptLev);
586 /// isValidDebugInfoIntrinsic - Return true if REI is a valid debug
588 bool isValidDebugInfoIntrinsic(DbgRegionEndInst &REI,
589 CodeGenOpt::Level OptLev);
591 /// isValidDebugInfoIntrinsic - Return true if DI is a valid debug
593 bool isValidDebugInfoIntrinsic(DbgDeclareInst &DI,
594 CodeGenOpt::Level OptLev);
596 /// ExtractDebugLocation - Extract debug location information
597 /// from llvm.dbg.stoppoint intrinsic.
598 DebugLoc ExtractDebugLocation(DbgStopPointInst &SPI,
599 DebugLocTracker &DebugLocInfo);
601 /// ExtractDebugLocation - Extract debug location information
602 /// from llvm.dbg.func_start intrinsic.
603 DebugLoc ExtractDebugLocation(DbgFuncStartInst &FSI,
604 DebugLocTracker &DebugLocInfo);
606 /// isInlinedFnStart - Return true if FSI is starting an inlined function.
607 bool isInlinedFnStart(DbgFuncStartInst &FSI, const Function *CurrentFn);
609 /// isInlinedFnEnd - Return true if REI is ending an inlined function.
610 bool isInlinedFnEnd(DbgRegionEndInst &REI, const Function *CurrentFn);
612 /// DebugInfoFinder - This object collects DebugInfo from a module.
613 class DebugInfoFinder {
616 /// processModule - Process entire module and collect debug info
618 void processModule(Module &M);
621 /// processType - Process DIType.
622 void processType(DIType DT);
624 /// processSubprogram - Enumberate DISubprogram.
625 void processSubprogram(DISubprogram SP);
627 /// processStopPoint - Process DbgStopPointInst.
628 void processStopPoint(DbgStopPointInst *SPI);
630 /// processFuncStart - Process DbgFuncStartInst.
631 void processFuncStart(DbgFuncStartInst *FSI);
633 /// processRegionStart - Process DbgRegionStart.
634 void processRegionStart(DbgRegionStartInst *DRS);
636 /// processRegionEnd - Process DbgRegionEnd.
637 void processRegionEnd(DbgRegionEndInst *DRE);
639 /// processDeclare - Process DbgDeclareInst.
640 void processDeclare(DbgDeclareInst *DDI);
642 /// addCompileUnit - Add compile unit into CUs.
643 bool addCompileUnit(DICompileUnit CU);
645 /// addGlobalVariable - Add global variable into GVs.
646 bool addGlobalVariable(DIGlobalVariable DIG);
648 // addSubprogram - Add subprgoram into SPs.
649 bool addSubprogram(DISubprogram SP);
651 /// addType - Add type into Tys.
652 bool addType(DIType DT);
655 typedef SmallVector<GlobalVariable *, 8>::iterator iterator;
656 iterator compile_unit_begin() { return CUs.begin(); }
657 iterator compile_unit_end() { return CUs.end(); }
658 iterator subprogram_begin() { return SPs.begin(); }
659 iterator subprogram_end() { return SPs.end(); }
660 iterator global_variable_begin() { return GVs.begin(); }
661 iterator global_variable_end() { return GVs.end(); }
662 iterator type_begin() { return TYs.begin(); }
663 iterator type_end() { return TYs.end(); }
665 unsigned compile_unit_count() { return CUs.size(); }
666 unsigned global_variable_count() { return GVs.size(); }
667 unsigned subprogram_count() { return SPs.size(); }
668 unsigned type_count() { return TYs.size(); }
671 SmallVector<GlobalVariable *, 8> CUs; // Compile Units
672 SmallVector<GlobalVariable *, 8> SPs; // Subprograms
673 SmallVector<GlobalVariable *, 8> GVs; // Global Variables
674 SmallVector<GlobalVariable *, 8> TYs; // Types
675 SmallPtrSet<GlobalVariable *, 64> NodesSeen;
678 } // end namespace llvm