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.
91 bool isDerivedType() const;
92 bool isCompositeType() const;
93 bool isBasicType() const;
94 bool isVariable() const;
95 bool isSubprogram() const;
96 bool isGlobalVariable() const;
99 /// DISubrange - This is used to represent ranges, for array bounds.
100 class DISubrange : public DIDescriptor {
102 explicit DISubrange(MDNode *N = 0)
103 : DIDescriptor(N, dwarf::DW_TAG_subrange_type) {}
105 int64_t getLo() const { return (int64_t)getUInt64Field(1); }
106 int64_t getHi() const { return (int64_t)getUInt64Field(2); }
109 /// DIArray - This descriptor holds an array of descriptors.
110 class DIArray : public DIDescriptor {
112 explicit DIArray(MDNode *N = 0)
115 unsigned getNumElements() const;
116 DIDescriptor getElement(unsigned Idx) const {
117 return getDescriptorField(Idx);
121 /// DICompileUnit - A wrapper for a compile unit.
122 class DICompileUnit : public DIDescriptor {
124 explicit DICompileUnit(MDNode *N = 0)
125 : DIDescriptor(N, dwarf::DW_TAG_compile_unit) {}
127 unsigned getLanguage() const { return getUnsignedField(2); }
128 const std::string &getFilename(std::string &F) const {
129 return getStringField(3, F);
131 const std::string &getDirectory(std::string &F) const {
132 return getStringField(4, F);
134 const std::string &getProducer(std::string &F) const {
135 return getStringField(5, F);
138 /// isMain - Each input file is encoded as a separate compile unit in LLVM
139 /// debugging information output. However, many target specific tool chains
140 /// prefer to encode only one compile unit in an object file. In this
141 /// situation, the LLVM code generator will include debugging information
142 /// entities in the compile unit that is marked as main compile unit. The
143 /// code generator accepts maximum one main compile unit per module. If a
144 /// module does not contain any main compile unit then the code generator
145 /// will emit multiple compile units in the output object file.
147 bool isMain() const { return getUnsignedField(6); }
148 bool isOptimized() const { return getUnsignedField(7); }
149 const std::string &getFlags(std::string &F) const {
150 return getStringField(8, F);
152 unsigned getRunTimeVersion() const { return getUnsignedField(9); }
154 /// Verify - Verify that a compile unit is well formed.
157 /// dump - print compile unit.
161 /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
162 /// FIXME: it seems strange that this doesn't have either a reference to the
163 /// type/precision or a file/line pair for location info.
164 class DIEnumerator : public DIDescriptor {
166 explicit DIEnumerator(MDNode *N = 0)
167 : DIDescriptor(N, dwarf::DW_TAG_enumerator) {}
169 const std::string &getName(std::string &F) const {
170 return getStringField(1, F);
172 uint64_t getEnumValue() const { return getUInt64Field(2); }
175 /// DIType - This is a wrapper for a type.
176 /// FIXME: Types should be factored much better so that CV qualifiers and
177 /// others do not require a huge and empty descriptor full of zeros.
178 class DIType : public DIDescriptor {
181 FlagPrivate = 1 << 0,
182 FlagProtected = 1 << 1,
183 FlagFwdDecl = 1 << 2,
184 FlagAppleBlock = 1 << 3
188 DIType(MDNode *N, unsigned Tag)
189 : DIDescriptor(N, Tag) {}
190 // This ctor is used when the Tag has already been validated by a derived
192 DIType(MDNode *N, bool, bool) : DIDescriptor(N) {}
196 /// Verify - Verify that a type descriptor is well formed.
199 explicit DIType(MDNode *N);
203 DIDescriptor getContext() const { return getDescriptorField(1); }
204 const std::string &getName(std::string &F) const {
205 return getStringField(2, F);
207 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
208 unsigned getLineNumber() const { return getUnsignedField(4); }
209 uint64_t getSizeInBits() const { return getUInt64Field(5); }
210 uint64_t getAlignInBits() const { return getUInt64Field(6); }
211 // FIXME: Offset is only used for DW_TAG_member nodes. Making every type
212 // carry this is just plain insane.
213 uint64_t getOffsetInBits() const { return getUInt64Field(7); }
214 unsigned getFlags() const { return getUnsignedField(8); }
215 bool isPrivate() const {
216 return (getFlags() & FlagPrivate) != 0;
218 bool isProtected() const {
219 return (getFlags() & FlagProtected) != 0;
221 bool isForwardDecl() const {
222 return (getFlags() & FlagFwdDecl) != 0;
224 // isAppleBlock - Return true if this is the Apple Blocks extension.
225 bool isAppleBlockExtension() const {
226 return (getFlags() & FlagAppleBlock) != 0;
229 /// dump - print type.
233 /// DIBasicType - A basic type, like 'int' or 'float'.
234 class DIBasicType : public DIType {
236 explicit DIBasicType(MDNode *N = 0)
237 : DIType(N, dwarf::DW_TAG_base_type) {}
239 unsigned getEncoding() const { return getUnsignedField(9); }
241 /// dump - print basic type.
245 /// DIDerivedType - A simple derived type, like a const qualified type,
246 /// a typedef, a pointer or reference, etc.
247 class DIDerivedType : public DIType {
249 explicit DIDerivedType(MDNode *N, bool, bool)
250 : DIType(N, true, true) {}
252 explicit DIDerivedType(MDNode *N = 0)
253 : DIType(N, true, true) {
254 if (DbgNode && !isDerivedType())
258 DIType getTypeDerivedFrom() const { return getFieldAs<DIType>(9); }
260 /// getOriginalTypeSize - If this type is derived from a base type then
261 /// return base type size.
262 uint64_t getOriginalTypeSize() const;
263 /// dump - print derived type.
266 /// replaceAllUsesWith - Replace all uses of debug info referenced by
267 /// this descriptor. After this completes, the current debug info value
269 void replaceAllUsesWith(DIDescriptor &D);
272 /// DICompositeType - This descriptor holds a type that can refer to multiple
273 /// other types, like a function or struct.
274 /// FIXME: Why is this a DIDerivedType??
275 class DICompositeType : public DIDerivedType {
277 explicit DICompositeType(MDNode *N = 0)
278 : DIDerivedType(N, true, true) {
279 if (N && !isCompositeType())
283 DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
284 unsigned getRunTimeLang() const { return getUnsignedField(11); }
286 /// Verify - Verify that a composite type descriptor is well formed.
289 /// dump - print composite type.
293 /// DIGlobal - This is a common class for global variables and subprograms.
294 class DIGlobal : public DIDescriptor {
296 explicit DIGlobal(MDNode *N, unsigned RequiredTag)
297 : DIDescriptor(N, RequiredTag) {}
300 virtual ~DIGlobal() {}
302 DIDescriptor getContext() const { return getDescriptorField(2); }
303 const std::string &getName(std::string &F) const {
304 return getStringField(3, F);
306 const std::string &getDisplayName(std::string &F) const {
307 return getStringField(4, F);
309 const std::string &getLinkageName(std::string &F) const {
310 return getStringField(5, F);
312 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(6); }
313 unsigned getLineNumber() const { return getUnsignedField(7); }
314 DIType getType() const { return getFieldAs<DIType>(8); }
316 /// isLocalToUnit - Return true if this subprogram is local to the current
317 /// compile unit, like 'static' in C.
318 unsigned isLocalToUnit() const { return getUnsignedField(9); }
319 unsigned isDefinition() const { return getUnsignedField(10); }
321 /// dump - print global.
325 /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
326 class DISubprogram : public DIGlobal {
328 explicit DISubprogram(MDNode *N = 0)
329 : DIGlobal(N, dwarf::DW_TAG_subprogram) {}
331 DICompositeType getType() const { return getFieldAs<DICompositeType>(8); }
333 /// getReturnTypeName - Subprogram return types are encoded either as
334 /// DIType or as DICompositeType.
335 const std::string &getReturnTypeName(std::string &F) const {
336 DICompositeType DCT(getFieldAs<DICompositeType>(8));
338 DIArray A = DCT.getTypeArray();
339 DIType T(A.getElement(0).getNode());
342 DIType T(getFieldAs<DIType>(8));
346 /// Verify - Verify that a subprogram descriptor is well formed.
349 /// dump - print subprogram.
352 /// describes - Return true if this subprogram provides debugging
353 /// information for the function F.
354 bool describes(const Function *F);
357 /// DIGlobalVariable - This is a wrapper for a global variable.
358 class DIGlobalVariable : public DIGlobal {
360 explicit DIGlobalVariable(MDNode *N = 0)
361 : DIGlobal(N, dwarf::DW_TAG_variable) {}
363 GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
365 /// Verify - Verify that a global variable descriptor is well formed.
368 /// dump - print global variable.
372 /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
374 class DIVariable : public DIDescriptor {
376 explicit DIVariable(MDNode *N = 0)
378 if (DbgNode && !isVariable())
382 DIDescriptor getContext() const { return getDescriptorField(1); }
383 const std::string &getName(std::string &F) const {
384 return getStringField(2, F);
386 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
387 unsigned getLineNumber() const { return getUnsignedField(4); }
388 DIType getType() const { return getFieldAs<DIType>(5); }
391 /// Verify - Verify that a variable descriptor is well formed.
394 /// dump - print variable.
398 /// DIBlock - This is a wrapper for a block (e.g. a function, scope, etc).
399 class DIBlock : public DIDescriptor {
401 explicit DIBlock(MDNode *N = 0)
402 : DIDescriptor(N, dwarf::DW_TAG_lexical_block) {}
404 DIDescriptor getContext() const { return getDescriptorField(1); }
407 /// DIFactory - This object assists with the construction of the various
411 LLVMContext& VMContext;
413 // Cached values for uniquing and faster lookups.
414 const Type *EmptyStructPtr; // "{}*".
415 Function *StopPointFn; // llvm.dbg.stoppoint
416 Function *FuncStartFn; // llvm.dbg.func.start
417 Function *RegionStartFn; // llvm.dbg.region.start
418 Function *RegionEndFn; // llvm.dbg.region.end
419 Function *DeclareFn; // llvm.dbg.declare
420 StringMap<Constant*> StringCache;
421 DenseMap<Constant*, DIDescriptor> SimpleConstantCache;
423 DIFactory(const DIFactory &); // DO NOT IMPLEMENT
424 void operator=(const DIFactory&); // DO NOT IMPLEMENT
426 explicit DIFactory(Module &m);
428 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
429 /// This implicitly uniques the arrays created.
430 DIArray GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys);
432 /// GetOrCreateSubrange - Create a descriptor for a value range. This
433 /// implicitly uniques the values returned.
434 DISubrange GetOrCreateSubrange(int64_t Lo, int64_t Hi);
436 /// CreateCompileUnit - Create a new descriptor for the specified compile
438 DICompileUnit CreateCompileUnit(unsigned LangID,
439 const std::string &Filename,
440 const std::string &Directory,
441 const std::string &Producer,
443 bool isOptimized = false,
444 const char *Flags = "",
445 unsigned RunTimeVer = 0);
447 /// CreateEnumerator - Create a single enumerator value.
448 DIEnumerator CreateEnumerator(const std::string &Name, uint64_t Val);
450 /// CreateBasicType - Create a basic type like int, float, etc.
451 DIBasicType CreateBasicType(DIDescriptor Context, const std::string &Name,
452 DICompileUnit CompileUnit, unsigned LineNumber,
453 uint64_t SizeInBits, uint64_t AlignInBits,
454 uint64_t OffsetInBits, unsigned Flags,
457 /// CreateDerivedType - Create a derived type like const qualified type,
458 /// pointer, typedef, etc.
459 DIDerivedType CreateDerivedType(unsigned Tag, DIDescriptor Context,
460 const std::string &Name,
461 DICompileUnit CompileUnit,
463 uint64_t SizeInBits, uint64_t AlignInBits,
464 uint64_t OffsetInBits, unsigned Flags,
467 /// CreateCompositeType - Create a composite type like array, struct, etc.
468 DICompositeType CreateCompositeType(unsigned Tag, DIDescriptor Context,
469 const std::string &Name,
470 DICompileUnit CompileUnit,
473 uint64_t AlignInBits,
474 uint64_t OffsetInBits, unsigned Flags,
477 unsigned RunTimeLang = 0);
479 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
480 /// See comments in DISubprogram for descriptions of these fields.
481 DISubprogram CreateSubprogram(DIDescriptor Context, const std::string &Name,
482 const std::string &DisplayName,
483 const std::string &LinkageName,
484 DICompileUnit CompileUnit, unsigned LineNo,
485 DIType Type, bool isLocalToUnit,
488 /// CreateGlobalVariable - Create a new descriptor for the specified global.
490 CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
491 const std::string &DisplayName,
492 const std::string &LinkageName,
493 DICompileUnit CompileUnit,
494 unsigned LineNo, DIType Type, bool isLocalToUnit,
495 bool isDefinition, llvm::GlobalVariable *GV);
497 /// CreateVariable - Create a new descriptor for the specified variable.
498 DIVariable CreateVariable(unsigned Tag, DIDescriptor Context,
499 const std::string &Name,
500 DICompileUnit CompileUnit, unsigned LineNo,
503 /// CreateBlock - This creates a descriptor for a lexical block with the
504 /// specified parent context.
505 DIBlock CreateBlock(DIDescriptor Context);
507 /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
508 /// inserting it at the end of the specified basic block.
509 void InsertStopPoint(DICompileUnit CU, unsigned LineNo, unsigned ColNo,
512 /// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
513 /// mark the start of the specified subprogram.
514 void InsertSubprogramStart(DISubprogram SP, BasicBlock *BB);
516 /// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
517 /// mark the start of a region for the specified scoping descriptor.
518 void InsertRegionStart(DIDescriptor D, BasicBlock *BB);
520 /// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
521 /// mark the end of a region for the specified scoping descriptor.
522 void InsertRegionEnd(DIDescriptor D, BasicBlock *BB);
524 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
525 void InsertDeclare(llvm::Value *Storage, DIVariable D, BasicBlock *BB);
528 Constant *GetTagConstant(unsigned TAG);
531 /// Finds the stoppoint coressponding to this instruction, that is the
532 /// stoppoint that dominates this instruction
533 const DbgStopPointInst *findStopPoint(const Instruction *Inst);
535 /// Finds the stoppoint corresponding to first real (non-debug intrinsic)
536 /// instruction in this Basic Block, and returns the stoppoint for it.
537 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB);
539 /// Finds the dbg.declare intrinsic corresponding to this value if any.
540 /// It looks through pointer casts too.
541 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts = true);
543 /// Find the debug info descriptor corresponding to this global variable.
544 Value *findDbgGlobalDeclare(GlobalVariable *V);
546 bool getLocationInfo(const Value *V, std::string &DisplayName,
547 std::string &Type, unsigned &LineNo, std::string &File,
550 /// isValidDebugInfoIntrinsic - Return true if SPI is a valid debug
552 bool isValidDebugInfoIntrinsic(DbgStopPointInst &SPI,
553 CodeGenOpt::Level OptLev);
555 /// isValidDebugInfoIntrinsic - Return true if FSI is a valid debug
557 bool isValidDebugInfoIntrinsic(DbgFuncStartInst &FSI,
558 CodeGenOpt::Level OptLev);
560 /// isValidDebugInfoIntrinsic - Return true if RSI is a valid debug
562 bool isValidDebugInfoIntrinsic(DbgRegionStartInst &RSI,
563 CodeGenOpt::Level OptLev);
565 /// isValidDebugInfoIntrinsic - Return true if REI is a valid debug
567 bool isValidDebugInfoIntrinsic(DbgRegionEndInst &REI,
568 CodeGenOpt::Level OptLev);
570 /// isValidDebugInfoIntrinsic - Return true if DI is a valid debug
572 bool isValidDebugInfoIntrinsic(DbgDeclareInst &DI,
573 CodeGenOpt::Level OptLev);
575 /// ExtractDebugLocation - Extract debug location information
576 /// from llvm.dbg.stoppoint intrinsic.
577 DebugLoc ExtractDebugLocation(DbgStopPointInst &SPI,
578 DebugLocTracker &DebugLocInfo);
580 /// ExtractDebugLocation - Extract debug location information
581 /// from llvm.dbg.func_start intrinsic.
582 DebugLoc ExtractDebugLocation(DbgFuncStartInst &FSI,
583 DebugLocTracker &DebugLocInfo);
585 /// isInlinedFnStart - Return true if FSI is starting an inlined function.
586 bool isInlinedFnStart(DbgFuncStartInst &FSI, const Function *CurrentFn);
588 /// isInlinedFnEnd - Return true if REI is ending an inlined function.
589 bool isInlinedFnEnd(DbgRegionEndInst &REI, const Function *CurrentFn);
590 /// DebugInfoFinder - This object collects DebugInfo from a module.
591 class DebugInfoFinder {
594 /// processModule - Process entire module and collect debug info
596 void processModule(Module &M);
599 /// processType - Process DIType.
600 void processType(DIType DT);
602 /// processSubprogram - Enumberate DISubprogram.
603 void processSubprogram(DISubprogram SP);
605 /// processStopPoint - Process DbgStopPointInst.
606 void processStopPoint(DbgStopPointInst *SPI);
608 /// processFuncStart - Process DbgFuncStartInst.
609 void processFuncStart(DbgFuncStartInst *FSI);
611 /// processRegionStart - Process DbgRegionStart.
612 void processRegionStart(DbgRegionStartInst *DRS);
614 /// processRegionEnd - Process DbgRegionEnd.
615 void processRegionEnd(DbgRegionEndInst *DRE);
617 /// processDeclare - Process DbgDeclareInst.
618 void processDeclare(DbgDeclareInst *DDI);
620 /// addCompileUnit - Add compile unit into CUs.
621 bool addCompileUnit(DICompileUnit CU);
623 /// addGlobalVariable - Add global variable into GVs.
624 bool addGlobalVariable(DIGlobalVariable DIG);
626 // addSubprogram - Add subprgoram into SPs.
627 bool addSubprogram(DISubprogram SP);
629 /// addType - Add type into Tys.
630 bool addType(DIType DT);
633 typedef SmallVector<MDNode *, 8>::iterator iterator;
634 iterator compile_unit_begin() { return CUs.begin(); }
635 iterator compile_unit_end() { return CUs.end(); }
636 iterator subprogram_begin() { return SPs.begin(); }
637 iterator subprogram_end() { return SPs.end(); }
638 iterator global_variable_begin() { return GVs.begin(); }
639 iterator global_variable_end() { return GVs.end(); }
640 iterator type_begin() { return TYs.begin(); }
641 iterator type_end() { return TYs.end(); }
643 unsigned compile_unit_count() { return CUs.size(); }
644 unsigned global_variable_count() { return GVs.size(); }
645 unsigned subprogram_count() { return SPs.size(); }
646 unsigned type_count() { return TYs.size(); }
649 SmallVector<MDNode *, 8> CUs; // Compile Units
650 SmallVector<MDNode *, 8> SPs; // Subprograms
651 SmallVector<MDNode *, 8> GVs; // Global Variables;
652 SmallVector<MDNode *, 8> TYs; // Types
653 SmallPtrSet<MDNode *, 64> NodesSeen;
655 } // end namespace llvm