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,
178 DIType(GlobalVariable *GV, unsigned Tag) : DIDescriptor(GV, Tag) {}
179 // This ctor is used when the Tag has already been validated by a derived
181 DIType(GlobalVariable *GV, bool, bool) : DIDescriptor(GV) {}
184 /// isDerivedType - Return true if the specified tag is legal for
186 static bool isDerivedType(unsigned TAG);
188 /// isCompositeType - Return true if the specified tag is legal for
190 static bool isCompositeType(unsigned TAG);
192 /// isBasicType - Return true if the specified tag is legal for
194 static bool isBasicType(unsigned TAG) {
195 return TAG == dwarf::DW_TAG_base_type;
198 /// Verify - Verify that a type descriptor is well formed.
201 explicit DIType(GlobalVariable *GV);
205 DIDescriptor getContext() const { return getDescriptorField(1); }
206 const std::string &getName(std::string &F) const {
207 return getStringField(2, F);
209 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
210 unsigned getLineNumber() const { return getUnsignedField(4); }
211 uint64_t getSizeInBits() const { return getUInt64Field(5); }
212 uint64_t getAlignInBits() const { return getUInt64Field(6); }
213 // FIXME: Offset is only used for DW_TAG_member nodes. Making every type
214 // carry this is just plain insane.
215 uint64_t getOffsetInBits() const { return getUInt64Field(7); }
216 unsigned getFlags() const { return getUnsignedField(8); }
217 bool isPrivate() const { return
218 (getFlags() & FlagPrivate) != 0;
220 bool isProtected() const {
221 return (getFlags() & FlagProtected) != 0;
223 bool isForwardDecl() const {
224 return (getFlags() & FlagFwdDecl) != 0;
227 /// dump - print type.
231 /// DIBasicType - A basic type, like 'int' or 'float'.
232 class DIBasicType : public DIType {
234 explicit DIBasicType(GlobalVariable *GV)
235 : DIType(GV, dwarf::DW_TAG_base_type) {}
237 unsigned getEncoding() const { return getUnsignedField(9); }
239 /// dump - print basic type.
243 /// DIDerivedType - A simple derived type, like a const qualified type,
244 /// a typedef, a pointer or reference, etc.
245 class DIDerivedType : public DIType {
247 explicit DIDerivedType(GlobalVariable *GV, bool, bool)
248 : DIType(GV, true, true) {}
250 explicit DIDerivedType(GlobalVariable *GV)
251 : DIType(GV, true, true) {
252 if (GV && !isDerivedType(getTag()))
256 DIType getTypeDerivedFrom() const { return getFieldAs<DIType>(9); }
258 /// getOriginalTypeSize - If this type is derived from a base type then
259 /// return base type size.
260 uint64_t getOriginalTypeSize() const;
261 /// dump - print derived type.
264 /// replaceAllUsesWith - Replace all uses of debug info referenced by
265 /// this descriptor. After this completes, the current debug info value
267 void replaceAllUsesWith(DIDescriptor &D);
270 /// DICompositeType - This descriptor holds a type that can refer to multiple
271 /// other types, like a function or struct.
272 /// FIXME: Why is this a DIDerivedType??
273 class DICompositeType : public DIDerivedType {
275 explicit DICompositeType(GlobalVariable *GV)
276 : DIDerivedType(GV, true, true) {
277 if (GV && !isCompositeType(getTag()))
281 DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
282 unsigned getRunTimeLang() const { return getUnsignedField(11); }
284 /// Verify - Verify that a composite type descriptor is well formed.
287 /// dump - print composite type.
291 /// DIGlobal - This is a common class for global variables and subprograms.
292 class DIGlobal : public DIDescriptor {
294 explicit DIGlobal(GlobalVariable *GV, unsigned RequiredTag)
295 : DIDescriptor(GV, RequiredTag) {}
297 /// isSubprogram - Return true if the specified tag is legal for
299 static bool isSubprogram(unsigned TAG) {
300 return TAG == dwarf::DW_TAG_subprogram;
303 /// isGlobalVariable - Return true if the specified tag is legal for
304 /// DIGlobalVariable.
305 static bool isGlobalVariable(unsigned TAG) {
306 return TAG == dwarf::DW_TAG_variable;
310 virtual ~DIGlobal() {}
312 DIDescriptor getContext() const { return getDescriptorField(2); }
313 const std::string &getName(std::string &F) const {
314 return getStringField(3, F);
316 const std::string &getDisplayName(std::string &F) const {
317 return getStringField(4, F);
319 const std::string &getLinkageName(std::string &F) const {
320 return getStringField(5, F);
322 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(6); }
323 unsigned getLineNumber() const { return getUnsignedField(7); }
324 DIType getType() const { return getFieldAs<DIType>(8); }
326 /// isLocalToUnit - Return true if this subprogram is local to the current
327 /// compile unit, like 'static' in C.
328 unsigned isLocalToUnit() const { return getUnsignedField(9); }
329 unsigned isDefinition() const { return getUnsignedField(10); }
331 /// dump - print global.
335 /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
336 class DISubprogram : public DIGlobal {
338 explicit DISubprogram(GlobalVariable *GV = 0)
339 : DIGlobal(GV, dwarf::DW_TAG_subprogram) {}
341 DICompositeType getType() const { return getFieldAs<DICompositeType>(8); }
343 /// getReturnTypeName - Subprogram return types are encoded either as
344 /// DIType or as DICompositeType.
345 const std::string &getReturnTypeName(std::string &F) const {
346 DICompositeType DCT(getFieldAs<DICompositeType>(8));
348 DIArray A = DCT.getTypeArray();
349 DIType T(A.getElement(0).getGV());
352 DIType T(getFieldAs<DIType>(8));
356 /// Verify - Verify that a subprogram descriptor is well formed.
359 /// dump - print subprogram.
362 /// describes - Return true if this subprogram provides debugging
363 /// information for the function F.
364 bool describes(const Function *F);
367 /// DIGlobalVariable - This is a wrapper for a global variable.
368 class DIGlobalVariable : public DIGlobal {
370 explicit DIGlobalVariable(GlobalVariable *GV = 0)
371 : DIGlobal(GV, dwarf::DW_TAG_variable) {}
373 GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
375 /// Verify - Verify that a global variable descriptor is well formed.
378 /// dump - print global variable.
382 /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
384 class DIVariable : public DIDescriptor {
386 explicit DIVariable(GlobalVariable *GV = 0)
388 if (GV && !isVariable(getTag()))
392 DIDescriptor getContext() const { return getDescriptorField(1); }
393 const std::string &getName(std::string &F) const {
394 return getStringField(2, F);
396 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
397 unsigned getLineNumber() const { return getUnsignedField(4); }
398 DIType getType() const { return getFieldAs<DIType>(5); }
400 /// isVariable - Return true if the specified tag is legal for DIVariable.
401 static bool isVariable(unsigned Tag);
403 /// Verify - Verify that a variable descriptor is well formed.
406 /// dump - print variable.
410 /// DIBlock - This is a wrapper for a block (e.g. a function, scope, etc).
411 class DIBlock : public DIDescriptor {
413 explicit DIBlock(GlobalVariable *GV = 0)
414 : DIDescriptor(GV, dwarf::DW_TAG_lexical_block) {}
416 DIDescriptor getContext() const { return getDescriptorField(1); }
419 /// DIFactory - This object assists with the construction of the various
423 LLVMContext& VMContext;
425 // Cached values for uniquing and faster lookups.
426 const Type *EmptyStructPtr; // "{}*".
427 Function *StopPointFn; // llvm.dbg.stoppoint
428 Function *FuncStartFn; // llvm.dbg.func.start
429 Function *RegionStartFn; // llvm.dbg.region.start
430 Function *RegionEndFn; // llvm.dbg.region.end
431 Function *DeclareFn; // llvm.dbg.declare
432 StringMap<Constant*> StringCache;
433 DenseMap<Constant*, DIDescriptor> SimpleConstantCache;
435 DIFactory(const DIFactory &); // DO NOT IMPLEMENT
436 void operator=(const DIFactory&); // DO NOT IMPLEMENT
438 explicit DIFactory(Module &m);
440 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
441 /// This implicitly uniques the arrays created.
442 DIArray GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys);
444 /// GetOrCreateSubrange - Create a descriptor for a value range. This
445 /// implicitly uniques the values returned.
446 DISubrange GetOrCreateSubrange(int64_t Lo, int64_t Hi);
448 /// CreateCompileUnit - Create a new descriptor for the specified compile
450 DICompileUnit CreateCompileUnit(unsigned LangID,
451 const std::string &Filename,
452 const std::string &Directory,
453 const std::string &Producer,
455 bool isOptimized = false,
456 const char *Flags = "",
457 unsigned RunTimeVer = 0);
459 /// CreateEnumerator - Create a single enumerator value.
460 DIEnumerator CreateEnumerator(const std::string &Name, uint64_t Val);
462 /// CreateBasicType - Create a basic type like int, float, etc.
463 DIBasicType CreateBasicType(DIDescriptor Context, const std::string &Name,
464 DICompileUnit CompileUnit, unsigned LineNumber,
465 uint64_t SizeInBits, uint64_t AlignInBits,
466 uint64_t OffsetInBits, unsigned Flags,
469 /// CreateDerivedType - Create a derived type like const qualified type,
470 /// pointer, typedef, etc.
471 DIDerivedType CreateDerivedType(unsigned Tag, DIDescriptor Context,
472 const std::string &Name,
473 DICompileUnit CompileUnit,
475 uint64_t SizeInBits, uint64_t AlignInBits,
476 uint64_t OffsetInBits, unsigned Flags,
479 /// CreateCompositeType - Create a composite type like array, struct, etc.
480 DICompositeType CreateCompositeType(unsigned Tag, DIDescriptor Context,
481 const std::string &Name,
482 DICompileUnit CompileUnit,
485 uint64_t AlignInBits,
486 uint64_t OffsetInBits, unsigned Flags,
489 unsigned RunTimeLang = 0);
491 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
492 /// See comments in DISubprogram for descriptions of these fields.
493 DISubprogram CreateSubprogram(DIDescriptor Context, const std::string &Name,
494 const std::string &DisplayName,
495 const std::string &LinkageName,
496 DICompileUnit CompileUnit, unsigned LineNo,
497 DIType Type, bool isLocalToUnit,
500 /// CreateGlobalVariable - Create a new descriptor for the specified global.
502 CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
503 const std::string &DisplayName,
504 const std::string &LinkageName,
505 DICompileUnit CompileUnit,
506 unsigned LineNo, DIType Type, bool isLocalToUnit,
507 bool isDefinition, llvm::GlobalVariable *GV);
509 /// CreateVariable - Create a new descriptor for the specified variable.
510 DIVariable CreateVariable(unsigned Tag, DIDescriptor Context,
511 const std::string &Name,
512 DICompileUnit CompileUnit, unsigned LineNo,
515 /// CreateBlock - This creates a descriptor for a lexical block with the
516 /// specified parent context.
517 DIBlock CreateBlock(DIDescriptor Context);
519 /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
520 /// inserting it at the end of the specified basic block.
521 void InsertStopPoint(DICompileUnit CU, unsigned LineNo, unsigned ColNo,
524 /// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
525 /// mark the start of the specified subprogram.
526 void InsertSubprogramStart(DISubprogram SP, BasicBlock *BB);
528 /// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
529 /// mark the start of a region for the specified scoping descriptor.
530 void InsertRegionStart(DIDescriptor D, BasicBlock *BB);
532 /// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
533 /// mark the end of a region for the specified scoping descriptor.
534 void InsertRegionEnd(DIDescriptor D, BasicBlock *BB);
536 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
537 void InsertDeclare(llvm::Value *Storage, DIVariable D, BasicBlock *BB);
540 Constant *GetTagConstant(unsigned TAG);
541 Constant *GetStringConstant(const std::string &String);
543 /// getCastToEmpty - Return the descriptor as a Constant* with type '{}*'.
544 Constant *getCastToEmpty(DIDescriptor D);
547 /// Finds the stoppoint coressponding to this instruction, that is the
548 /// stoppoint that dominates this instruction
549 const DbgStopPointInst *findStopPoint(const Instruction *Inst);
551 /// Finds the stoppoint corresponding to first real (non-debug intrinsic)
552 /// instruction in this Basic Block, and returns the stoppoint for it.
553 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB);
555 /// Finds the dbg.declare intrinsic corresponding to this value if any.
556 /// It looks through pointer casts too.
557 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts = true);
559 /// Find the debug info descriptor corresponding to this global variable.
560 Value *findDbgGlobalDeclare(GlobalVariable *V);
562 bool getLocationInfo(const Value *V, std::string &DisplayName,
563 std::string &Type, unsigned &LineNo, std::string &File,
566 /// CollectDebugInfoAnchors - Collect debugging information anchors.
567 void CollectDebugInfoAnchors(Module &M,
568 SmallVector<GlobalVariable *, 2> &CompileUnits,
569 SmallVector<GlobalVariable *, 4> &GlobalVars,
570 SmallVector<GlobalVariable *, 4> &Subprograms);
572 /// isValidDebugInfoIntrinsic - Return true if SPI is a valid debug
574 bool isValidDebugInfoIntrinsic(DbgStopPointInst &SPI,
575 CodeGenOpt::Level OptLev);
577 /// isValidDebugInfoIntrinsic - Return true if FSI is a valid debug
579 bool isValidDebugInfoIntrinsic(DbgFuncStartInst &FSI,
580 CodeGenOpt::Level OptLev);
582 /// isValidDebugInfoIntrinsic - Return true if RSI is a valid debug
584 bool isValidDebugInfoIntrinsic(DbgRegionStartInst &RSI,
585 CodeGenOpt::Level OptLev);
587 /// isValidDebugInfoIntrinsic - Return true if REI is a valid debug
589 bool isValidDebugInfoIntrinsic(DbgRegionEndInst &REI,
590 CodeGenOpt::Level OptLev);
592 /// isValidDebugInfoIntrinsic - Return true if DI is a valid debug
594 bool isValidDebugInfoIntrinsic(DbgDeclareInst &DI,
595 CodeGenOpt::Level OptLev);
597 /// ExtractDebugLocation - Extract debug location information
598 /// from llvm.dbg.stoppoint intrinsic.
599 DebugLoc ExtractDebugLocation(DbgStopPointInst &SPI,
600 DebugLocTracker &DebugLocInfo);
602 /// ExtractDebugLocation - Extract debug location information
603 /// from llvm.dbg.func_start intrinsic.
604 DebugLoc ExtractDebugLocation(DbgFuncStartInst &FSI,
605 DebugLocTracker &DebugLocInfo);
607 /// isInlinedFnStart - Return true if FSI is starting an inlined function.
608 bool isInlinedFnStart(DbgFuncStartInst &FSI, const Function *CurrentFn);
610 /// isInlinedFnEnd - Return true if REI is ending an inlined function.
611 bool isInlinedFnEnd(DbgRegionEndInst &REI, const Function *CurrentFn);
613 /// DebugInfoEnumrator - This object collects DebugInfo from
615 class DebugInfoFinder {
618 /// processModule - Process entire module and collect debug info
620 void processModule(Module &M);
623 /// processType - Process DIType.
624 void processType(DIType DT);
626 /// processSubprogram - Enumberate DISubprogram.
627 void processSubprogram(DISubprogram SP);
629 /// processStopPoint - Process DbgStopPointInst.
630 void processStopPoint(DbgStopPointInst *SPI);
632 /// processFuncStart - Process DbgFuncStartInst.
633 void processFuncStart(DbgFuncStartInst *FSI);
635 /// processRegionStart - Process DbgRegionStart.
636 void processRegionStart(DbgRegionStartInst *DRS);
638 /// processRegionEnd - Process DbgRegionEnd.
639 void processRegionEnd(DbgRegionEndInst *DRE);
641 /// processDeclare - Process DbgDeclareInst.
642 void processDeclare(DbgDeclareInst *DDI);
644 /// addCompileUnit - Add compile unit into CUs.
645 bool addCompileUnit(DICompileUnit CU);
647 /// addGlobalVariable - Add global variable into GVs.
648 bool addGlobalVariable(DIGlobalVariable DIG);
650 // addSubprogram - Add subprgoram into SPs.
651 bool addSubprogram(DISubprogram SP);
654 typedef SmallVector<GlobalVariable *, 8>::iterator iterator;
655 iterator compile_unit_begin() { return CUs.begin(); }
656 iterator compile_unit_end() { return CUs.end(); }
657 iterator subprogram_begin() { return SPs.begin(); }
658 iterator subprogram_end() { return SPs.end(); }
659 iterator global_variable_begin() { return GVs.begin(); }
660 iterator global_variable_end() { return GVs.end(); }
662 unsigned compile_unit_count() { return CUs.size(); }
663 unsigned global_variable_count() { return GVs.size(); }
664 unsigned subprogram_count() { return SPs.size(); }
667 SmallVector<GlobalVariable *, 8> CUs; // Compile Units
668 SmallVector<GlobalVariable *, 8> SPs; // Subprograms
669 SmallVector<GlobalVariable *, 8> GVs; // Global Variables;
670 SmallPtrSet<GlobalVariable *, 64> NodesSeen;
673 } // end namespace llvm