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/Metadata.h"
21 #include "llvm/Target/TargetMachine.h"
22 #include "llvm/ADT/StringMap.h"
23 #include "llvm/ADT/DenseMap.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/ADT/SmallPtrSet.h"
26 #include "llvm/Support/Dwarf.h"
36 struct DbgStopPointInst;
37 struct DbgDeclareInst;
38 struct DbgFuncStartInst;
39 struct DbgRegionStartInst;
40 struct DbgRegionEndInst;
42 struct DebugLocTracker;
50 /// DIDescriptor constructor. If the specified node is non-null, check
51 /// to make sure that the tag in the descriptor matches 'RequiredTag'. If
52 /// not, the debug info is corrupt and we ignore it.
53 DIDescriptor(MDNode *N, unsigned RequiredTag);
55 const std::string &getStringField(unsigned Elt, std::string &Result) const;
56 unsigned getUnsignedField(unsigned Elt) const {
57 return (unsigned)getUInt64Field(Elt);
59 uint64_t getUInt64Field(unsigned Elt) const;
60 DIDescriptor getDescriptorField(unsigned Elt) const;
62 template <typename DescTy>
63 DescTy getFieldAs(unsigned Elt) const {
64 return DescTy(getDescriptorField(Elt).getNode());
67 GlobalVariable *getGlobalVariableField(unsigned Elt) const;
70 explicit DIDescriptor() : DbgNode(0) {}
71 explicit DIDescriptor(MDNode *N) : DbgNode(N) {}
73 bool isNull() const { return DbgNode == 0; }
75 MDNode *getNode() const { return DbgNode; }
77 unsigned getVersion() const {
78 return getUnsignedField(0) & LLVMDebugVersionMask;
81 unsigned getTag() const {
82 return getUnsignedField(0) & ~LLVMDebugVersionMask;
85 /// ValidDebugInfo - Return true if N represents valid debug info value.
86 static bool ValidDebugInfo(MDNode *N, CodeGenOpt::Level OptLevel);
88 /// dump - print descriptor.
92 /// DISubrange - This is used to represent ranges, for array bounds.
93 class DISubrange : public DIDescriptor {
95 explicit DISubrange(MDNode *N = 0)
96 : DIDescriptor(N, dwarf::DW_TAG_subrange_type) {}
98 int64_t getLo() const { return (int64_t)getUInt64Field(1); }
99 int64_t getHi() const { return (int64_t)getUInt64Field(2); }
102 /// DIArray - This descriptor holds an array of descriptors.
103 class DIArray : public DIDescriptor {
105 explicit DIArray(MDNode *N = 0)
108 unsigned getNumElements() const;
109 DIDescriptor getElement(unsigned Idx) const {
110 return getDescriptorField(Idx);
114 /// DICompileUnit - A wrapper for a compile unit.
115 class DICompileUnit : public DIDescriptor {
117 explicit DICompileUnit(MDNode *N = 0)
118 : DIDescriptor(N, dwarf::DW_TAG_compile_unit) {}
120 unsigned getLanguage() const { return getUnsignedField(2); }
121 const std::string &getFilename(std::string &F) const {
122 return getStringField(3, F);
124 const std::string &getDirectory(std::string &F) const {
125 return getStringField(4, F);
127 const std::string &getProducer(std::string &F) const {
128 return getStringField(5, F);
131 /// isMain - Each input file is encoded as a separate compile unit in LLVM
132 /// debugging information output. However, many target specific tool chains
133 /// prefer to encode only one compile unit in an object file. In this
134 /// situation, the LLVM code generator will include debugging information
135 /// entities in the compile unit that is marked as main compile unit. The
136 /// code generator accepts maximum one main compile unit per module. If a
137 /// module does not contain any main compile unit then the code generator
138 /// will emit multiple compile units in the output object file.
140 bool isMain() const { return getUnsignedField(6); }
141 bool isOptimized() const { return getUnsignedField(7); }
142 const std::string &getFlags(std::string &F) const {
143 return getStringField(8, F);
145 unsigned getRunTimeVersion() const { return getUnsignedField(9); }
147 /// Verify - Verify that a compile unit is well formed.
150 /// dump - print compile unit.
154 /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
155 /// FIXME: it seems strange that this doesn't have either a reference to the
156 /// type/precision or a file/line pair for location info.
157 class DIEnumerator : public DIDescriptor {
159 explicit DIEnumerator(MDNode *N = 0)
160 : DIDescriptor(N, dwarf::DW_TAG_enumerator) {}
162 const std::string &getName(std::string &F) const {
163 return getStringField(1, F);
165 uint64_t getEnumValue() const { return getUInt64Field(2); }
168 /// DIType - This is a wrapper for a type.
169 /// FIXME: Types should be factored much better so that CV qualifiers and
170 /// others do not require a huge and empty descriptor full of zeros.
171 class DIType : public DIDescriptor {
174 FlagPrivate = 1 << 0,
175 FlagProtected = 1 << 1,
180 DIType(MDNode *N, unsigned Tag)
181 : DIDescriptor(N, Tag) {}
182 // This ctor is used when the Tag has already been validated by a derived
184 DIType(MDNode *N, bool, bool) : DIDescriptor(N) {}
187 /// isDerivedType - Return true if the specified tag is legal for
189 static bool isDerivedType(unsigned TAG);
191 /// isCompositeType - Return true if the specified tag is legal for
193 static bool isCompositeType(unsigned TAG);
195 /// isBasicType - Return true if the specified tag is legal for
197 static bool isBasicType(unsigned TAG) {
198 return TAG == dwarf::DW_TAG_base_type;
201 /// Verify - Verify that a type descriptor is well formed.
204 explicit DIType(MDNode *N);
208 DIDescriptor getContext() const { return getDescriptorField(1); }
209 const std::string &getName(std::string &F) const {
210 return getStringField(2, F);
212 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
213 unsigned getLineNumber() const { return getUnsignedField(4); }
214 uint64_t getSizeInBits() const { return getUInt64Field(5); }
215 uint64_t getAlignInBits() const { return getUInt64Field(6); }
216 // FIXME: Offset is only used for DW_TAG_member nodes. Making every type
217 // carry this is just plain insane.
218 uint64_t getOffsetInBits() const { return getUInt64Field(7); }
219 unsigned getFlags() const { return getUnsignedField(8); }
220 bool isPrivate() const { return
221 (getFlags() & FlagPrivate) != 0;
223 bool isProtected() const {
224 return (getFlags() & FlagProtected) != 0;
226 bool isForwardDecl() const {
227 return (getFlags() & FlagFwdDecl) != 0;
230 /// dump - print type.
234 /// DIBasicType - A basic type, like 'int' or 'float'.
235 class DIBasicType : public DIType {
237 explicit DIBasicType(MDNode *N = 0)
238 : DIType(N, dwarf::DW_TAG_base_type) {}
240 unsigned getEncoding() const { return getUnsignedField(9); }
242 /// dump - print basic type.
246 /// DIDerivedType - A simple derived type, like a const qualified type,
247 /// a typedef, a pointer or reference, etc.
248 class DIDerivedType : public DIType {
250 explicit DIDerivedType(MDNode *N, bool, bool)
251 : DIType(N, true, true) {}
253 explicit DIDerivedType(MDNode *N = 0)
254 : DIType(N, true, true) {
255 if (DbgNode && !isDerivedType(getTag()))
259 DIType getTypeDerivedFrom() const { return getFieldAs<DIType>(9); }
261 /// getOriginalTypeSize - If this type is derived from a base type then
262 /// return base type size.
263 uint64_t getOriginalTypeSize() const;
264 /// dump - print derived type.
267 /// replaceAllUsesWith - Replace all uses of debug info referenced by
268 /// this descriptor. After this completes, the current debug info value
270 void replaceAllUsesWith(DIDescriptor &D);
273 /// DICompositeType - This descriptor holds a type that can refer to multiple
274 /// other types, like a function or struct.
275 /// FIXME: Why is this a DIDerivedType??
276 class DICompositeType : public DIDerivedType {
278 explicit DICompositeType(MDNode *N = 0)
279 : DIDerivedType(N, true, true) {
280 if (N && !isCompositeType(getTag()))
284 DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
285 unsigned getRunTimeLang() const { return getUnsignedField(11); }
287 /// Verify - Verify that a composite type descriptor is well formed.
290 /// dump - print composite type.
294 /// DIGlobal - This is a common class for global variables and subprograms.
295 class DIGlobal : public DIDescriptor {
297 explicit DIGlobal(MDNode *N, unsigned RequiredTag)
298 : DIDescriptor(N, RequiredTag) {}
300 /// isSubprogram - Return true if the specified tag is legal for
302 static bool isSubprogram(unsigned TAG) {
303 return TAG == dwarf::DW_TAG_subprogram;
306 /// isGlobalVariable - Return true if the specified tag is legal for
307 /// DIGlobalVariable.
308 static bool isGlobalVariable(unsigned TAG) {
309 return TAG == dwarf::DW_TAG_variable;
313 virtual ~DIGlobal() {}
315 DIDescriptor getContext() const { return getDescriptorField(2); }
316 const std::string &getName(std::string &F) const {
317 return getStringField(3, F);
319 const std::string &getDisplayName(std::string &F) const {
320 return getStringField(4, F);
322 const std::string &getLinkageName(std::string &F) const {
323 return getStringField(5, F);
325 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(6); }
326 unsigned getLineNumber() const { return getUnsignedField(7); }
327 DIType getType() const { return getFieldAs<DIType>(8); }
329 /// isLocalToUnit - Return true if this subprogram is local to the current
330 /// compile unit, like 'static' in C.
331 unsigned isLocalToUnit() const { return getUnsignedField(9); }
332 unsigned isDefinition() const { return getUnsignedField(10); }
334 /// dump - print global.
338 /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
339 class DISubprogram : public DIGlobal {
341 explicit DISubprogram(MDNode *N = 0)
342 : DIGlobal(N, dwarf::DW_TAG_subprogram) {}
344 DICompositeType getType() const { return getFieldAs<DICompositeType>(8); }
346 /// getReturnTypeName - Subprogram return types are encoded either as
347 /// DIType or as DICompositeType.
348 const std::string &getReturnTypeName(std::string &F) const {
349 DICompositeType DCT(getFieldAs<DICompositeType>(8));
351 DIArray A = DCT.getTypeArray();
352 DIType T(A.getElement(0).getNode());
355 DIType T(getFieldAs<DIType>(8));
359 /// Verify - Verify that a subprogram descriptor is well formed.
362 /// dump - print subprogram.
365 /// describes - Return true if this subprogram provides debugging
366 /// information for the function F.
367 bool describes(const Function *F);
370 /// DIGlobalVariable - This is a wrapper for a global variable.
371 class DIGlobalVariable : public DIGlobal {
373 explicit DIGlobalVariable(MDNode *N = 0)
374 : DIGlobal(N, dwarf::DW_TAG_variable) {}
376 GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
378 /// Verify - Verify that a global variable descriptor is well formed.
381 /// dump - print global variable.
385 /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
387 class DIVariable : public DIDescriptor {
389 explicit DIVariable(MDNode *N = 0)
391 if (DbgNode && !isVariable(getTag()))
395 DIDescriptor getContext() const { return getDescriptorField(1); }
396 const std::string &getName(std::string &F) const {
397 return getStringField(2, F);
399 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
400 unsigned getLineNumber() const { return getUnsignedField(4); }
401 DIType getType() const { return getFieldAs<DIType>(5); }
403 /// isVariable - Return true if the specified tag is legal for DIVariable.
404 static bool isVariable(unsigned Tag);
406 /// Verify - Verify that a variable descriptor is well formed.
409 /// dump - print variable.
413 /// DIBlock - This is a wrapper for a block (e.g. a function, scope, etc).
414 class DIBlock : public DIDescriptor {
416 explicit DIBlock(MDNode *N = 0)
417 : DIDescriptor(N, dwarf::DW_TAG_lexical_block) {}
419 DIDescriptor getContext() const { return getDescriptorField(1); }
422 /// DIFactory - This object assists with the construction of the various
426 LLVMContext& VMContext;
428 // Cached values for uniquing and faster lookups.
429 const Type *EmptyStructPtr; // "{}*".
430 Function *StopPointFn; // llvm.dbg.stoppoint
431 Function *FuncStartFn; // llvm.dbg.func.start
432 Function *RegionStartFn; // llvm.dbg.region.start
433 Function *RegionEndFn; // llvm.dbg.region.end
434 Function *DeclareFn; // llvm.dbg.declare
435 StringMap<Constant*> StringCache;
436 DenseMap<Constant*, DIDescriptor> SimpleConstantCache;
438 DIFactory(const DIFactory &); // DO NOT IMPLEMENT
439 void operator=(const DIFactory&); // DO NOT IMPLEMENT
441 explicit DIFactory(Module &m);
443 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
444 /// This implicitly uniques the arrays created.
445 DIArray GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys);
447 /// GetOrCreateSubrange - Create a descriptor for a value range. This
448 /// implicitly uniques the values returned.
449 DISubrange GetOrCreateSubrange(int64_t Lo, int64_t Hi);
451 /// CreateCompileUnit - Create a new descriptor for the specified compile
453 DICompileUnit CreateCompileUnit(unsigned LangID,
454 const std::string &Filename,
455 const std::string &Directory,
456 const std::string &Producer,
458 bool isOptimized = false,
459 const char *Flags = "",
460 unsigned RunTimeVer = 0);
462 /// CreateEnumerator - Create a single enumerator value.
463 DIEnumerator CreateEnumerator(const std::string &Name, uint64_t Val);
465 /// CreateBasicType - Create a basic type like int, float, etc.
466 DIBasicType CreateBasicType(DIDescriptor Context, const std::string &Name,
467 DICompileUnit CompileUnit, unsigned LineNumber,
468 uint64_t SizeInBits, uint64_t AlignInBits,
469 uint64_t OffsetInBits, unsigned Flags,
472 /// CreateDerivedType - Create a derived type like const qualified type,
473 /// pointer, typedef, etc.
474 DIDerivedType CreateDerivedType(unsigned Tag, DIDescriptor Context,
475 const std::string &Name,
476 DICompileUnit CompileUnit,
478 uint64_t SizeInBits, uint64_t AlignInBits,
479 uint64_t OffsetInBits, unsigned Flags,
482 /// CreateCompositeType - Create a composite type like array, struct, etc.
483 DICompositeType CreateCompositeType(unsigned Tag, DIDescriptor Context,
484 const std::string &Name,
485 DICompileUnit CompileUnit,
488 uint64_t AlignInBits,
489 uint64_t OffsetInBits, unsigned Flags,
492 unsigned RunTimeLang = 0);
494 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
495 /// See comments in DISubprogram for descriptions of these fields.
496 DISubprogram CreateSubprogram(DIDescriptor Context, const std::string &Name,
497 const std::string &DisplayName,
498 const std::string &LinkageName,
499 DICompileUnit CompileUnit, unsigned LineNo,
500 DIType Type, bool isLocalToUnit,
503 /// CreateGlobalVariable - Create a new descriptor for the specified global.
505 CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
506 const std::string &DisplayName,
507 const std::string &LinkageName,
508 DICompileUnit CompileUnit,
509 unsigned LineNo, DIType Type, bool isLocalToUnit,
510 bool isDefinition, llvm::GlobalVariable *GV);
512 /// CreateVariable - Create a new descriptor for the specified variable.
513 DIVariable CreateVariable(unsigned Tag, DIDescriptor Context,
514 const std::string &Name,
515 DICompileUnit CompileUnit, unsigned LineNo,
518 /// CreateBlock - This creates a descriptor for a lexical block with the
519 /// specified parent context.
520 DIBlock CreateBlock(DIDescriptor Context);
522 /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
523 /// inserting it at the end of the specified basic block.
524 void InsertStopPoint(DICompileUnit CU, unsigned LineNo, unsigned ColNo,
527 /// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
528 /// mark the start of the specified subprogram.
529 void InsertSubprogramStart(DISubprogram SP, BasicBlock *BB);
531 /// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
532 /// mark the start of a region for the specified scoping descriptor.
533 void InsertRegionStart(DIDescriptor D, BasicBlock *BB);
535 /// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
536 /// mark the end of a region for the specified scoping descriptor.
537 void InsertRegionEnd(DIDescriptor D, BasicBlock *BB);
539 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
540 void InsertDeclare(llvm::Value *Storage, DIVariable D, BasicBlock *BB);
543 Constant *GetTagConstant(unsigned TAG);
546 /// Finds the stoppoint coressponding to this instruction, that is the
547 /// stoppoint that dominates this instruction
548 const DbgStopPointInst *findStopPoint(const Instruction *Inst);
550 /// Finds the stoppoint corresponding to first real (non-debug intrinsic)
551 /// instruction in this Basic Block, and returns the stoppoint for it.
552 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB);
554 /// Finds the dbg.declare intrinsic corresponding to this value if any.
555 /// It looks through pointer casts too.
556 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts = true);
558 /// Find the debug info descriptor corresponding to this global variable.
559 Value *findDbgGlobalDeclare(GlobalVariable *V);
561 bool getLocationInfo(const Value *V, std::string &DisplayName,
562 std::string &Type, unsigned &LineNo, std::string &File,
565 /// isValidDebugInfoIntrinsic - Return true if SPI is a valid debug
567 bool isValidDebugInfoIntrinsic(DbgStopPointInst &SPI,
568 CodeGenOpt::Level OptLev);
570 /// isValidDebugInfoIntrinsic - Return true if FSI is a valid debug
572 bool isValidDebugInfoIntrinsic(DbgFuncStartInst &FSI,
573 CodeGenOpt::Level OptLev);
575 /// isValidDebugInfoIntrinsic - Return true if RSI is a valid debug
577 bool isValidDebugInfoIntrinsic(DbgRegionStartInst &RSI,
578 CodeGenOpt::Level OptLev);
580 /// isValidDebugInfoIntrinsic - Return true if REI is a valid debug
582 bool isValidDebugInfoIntrinsic(DbgRegionEndInst &REI,
583 CodeGenOpt::Level OptLev);
585 /// isValidDebugInfoIntrinsic - Return true if DI is a valid debug
587 bool isValidDebugInfoIntrinsic(DbgDeclareInst &DI,
588 CodeGenOpt::Level OptLev);
590 /// ExtractDebugLocation - Extract debug location information
591 /// from llvm.dbg.stoppoint intrinsic.
592 DebugLoc ExtractDebugLocation(DbgStopPointInst &SPI,
593 DebugLocTracker &DebugLocInfo);
595 /// ExtractDebugLocation - Extract debug location information
596 /// from llvm.dbg.func_start intrinsic.
597 DebugLoc ExtractDebugLocation(DbgFuncStartInst &FSI,
598 DebugLocTracker &DebugLocInfo);
600 /// isInlinedFnStart - Return true if FSI is starting an inlined function.
601 bool isInlinedFnStart(DbgFuncStartInst &FSI, const Function *CurrentFn);
603 /// isInlinedFnEnd - Return true if REI is ending an inlined function.
604 bool isInlinedFnEnd(DbgRegionEndInst &REI, const Function *CurrentFn);
605 /// DebugInfoFinder - This object collects DebugInfo from a module.
606 class DebugInfoFinder {
609 /// processModule - Process entire module and collect debug info
611 void processModule(Module &M);
614 /// processType - Process DIType.
615 void processType(DIType DT);
617 /// processSubprogram - Enumberate DISubprogram.
618 void processSubprogram(DISubprogram SP);
620 /// processStopPoint - Process DbgStopPointInst.
621 void processStopPoint(DbgStopPointInst *SPI);
623 /// processFuncStart - Process DbgFuncStartInst.
624 void processFuncStart(DbgFuncStartInst *FSI);
626 /// processRegionStart - Process DbgRegionStart.
627 void processRegionStart(DbgRegionStartInst *DRS);
629 /// processRegionEnd - Process DbgRegionEnd.
630 void processRegionEnd(DbgRegionEndInst *DRE);
632 /// processDeclare - Process DbgDeclareInst.
633 void processDeclare(DbgDeclareInst *DDI);
635 /// addCompileUnit - Add compile unit into CUs.
636 bool addCompileUnit(DICompileUnit CU);
638 /// addGlobalVariable - Add global variable into GVs.
639 bool addGlobalVariable(DIGlobalVariable DIG);
641 // addSubprogram - Add subprgoram into SPs.
642 bool addSubprogram(DISubprogram SP);
644 /// addType - Add type into Tys.
645 bool addType(DIType DT);
648 typedef SmallVector<MDNode *, 8>::iterator iterator;
649 iterator compile_unit_begin() { return CUs.begin(); }
650 iterator compile_unit_end() { return CUs.end(); }
651 iterator subprogram_begin() { return SPs.begin(); }
652 iterator subprogram_end() { return SPs.end(); }
653 iterator global_variable_begin() { return GVs.begin(); }
654 iterator global_variable_end() { return GVs.end(); }
655 iterator type_begin() { return TYs.begin(); }
656 iterator type_end() { return TYs.end(); }
658 unsigned compile_unit_count() { return CUs.size(); }
659 unsigned global_variable_count() { return GVs.size(); }
660 unsigned subprogram_count() { return SPs.size(); }
661 unsigned type_count() { return TYs.size(); }
664 SmallVector<MDNode *, 8> CUs; // Compile Units
665 SmallVector<MDNode *, 8> SPs; // Subprograms
666 SmallVector<MDNode *, 8> GVs; // Global Variables;
667 SmallVector<MDNode *, 8> TYs; // Types
668 SmallPtrSet<MDNode *, 64> NodesSeen;
670 } // end namespace llvm