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/Support/Dwarf.h"
34 struct DbgStopPointInst;
35 struct DbgDeclareInst;
36 struct DbgFuncStartInst;
37 struct DbgRegionStartInst;
38 struct DbgRegionEndInst;
40 struct DebugLocTracker;
46 GlobalVariable *DbgGV;
48 /// DIDescriptor constructor. If the specified GV is non-null, this checks
49 /// to make sure that the tag in the descriptor matches 'RequiredTag'. If
50 /// not, the debug info is corrupt and we ignore it.
51 DIDescriptor(GlobalVariable *GV, unsigned RequiredTag);
53 const std::string &getStringField(unsigned Elt, std::string &Result) const;
54 unsigned getUnsignedField(unsigned Elt) const {
55 return (unsigned)getUInt64Field(Elt);
57 uint64_t getUInt64Field(unsigned Elt) const;
58 DIDescriptor getDescriptorField(unsigned Elt) const;
60 template <typename DescTy>
61 DescTy getFieldAs(unsigned Elt) const {
62 return DescTy(getDescriptorField(Elt).getGV());
65 GlobalVariable *getGlobalVariableField(unsigned Elt) const;
68 explicit DIDescriptor() : DbgGV(0) {}
69 explicit DIDescriptor(GlobalVariable *GV) : DbgGV(GV) {}
71 bool isNull() const { return DbgGV == 0; }
73 GlobalVariable *getGV() const { return DbgGV; }
75 unsigned getVersion() const {
76 return getUnsignedField(0) & LLVMDebugVersionMask;
79 unsigned getTag() const {
80 return getUnsignedField(0) & ~LLVMDebugVersionMask;
83 /// ValidDebugInfo - Return true if V represents valid debug info value.
84 static bool ValidDebugInfo(Value *V, CodeGenOpt::Level OptLevel);
86 /// dump - print descriptor.
90 /// DISubrange - This is used to represent ranges, for array bounds.
91 class DISubrange : public DIDescriptor {
93 explicit DISubrange(GlobalVariable *GV = 0)
94 : DIDescriptor(GV, dwarf::DW_TAG_subrange_type) {}
96 int64_t getLo() const { return (int64_t)getUInt64Field(1); }
97 int64_t getHi() const { return (int64_t)getUInt64Field(2); }
100 /// DIArray - This descriptor holds an array of descriptors.
101 class DIArray : public DIDescriptor {
103 explicit DIArray(GlobalVariable *GV = 0) : DIDescriptor(GV) {}
105 unsigned getNumElements() const;
106 DIDescriptor getElement(unsigned Idx) const {
107 return getDescriptorField(Idx);
111 /// DICompileUnit - A wrapper for a compile unit.
112 class DICompileUnit : public DIDescriptor {
114 explicit DICompileUnit(GlobalVariable *GV = 0)
115 : DIDescriptor(GV, dwarf::DW_TAG_compile_unit) {}
117 unsigned getLanguage() const { return getUnsignedField(2); }
118 const std::string &getFilename(std::string &F) const {
119 return getStringField(3, F);
121 const std::string &getDirectory(std::string &F) const {
122 return getStringField(4, F);
124 const std::string &getProducer(std::string &F) const {
125 return getStringField(5, F);
128 /// isMain - Each input file is encoded as a separate compile unit in LLVM
129 /// debugging information output. However, many target specific tool chains
130 /// prefer to encode only one compile unit in an object file. In this
131 /// situation, the LLVM code generator will include debugging information
132 /// entities in the compile unit that is marked as main compile unit. The
133 /// code generator accepts maximum one main compile unit per module. If a
134 /// module does not contain any main compile unit then the code generator
135 /// will emit multiple compile units in the output object file.
137 bool isMain() const { return getUnsignedField(6); }
138 bool isOptimized() const { return getUnsignedField(7); }
139 const std::string &getFlags(std::string &F) const {
140 return getStringField(8, F);
142 unsigned getRunTimeVersion() const { return getUnsignedField(9); }
144 /// Verify - Verify that a compile unit is well formed.
147 /// dump - print compile unit.
151 /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
152 /// FIXME: it seems strange that this doesn't have either a reference to the
153 /// type/precision or a file/line pair for location info.
154 class DIEnumerator : public DIDescriptor {
156 explicit DIEnumerator(GlobalVariable *GV = 0)
157 : DIDescriptor(GV, dwarf::DW_TAG_enumerator) {}
159 const std::string &getName(std::string &F) const {
160 return getStringField(1, F);
162 uint64_t getEnumValue() const { return getUInt64Field(2); }
165 /// DIType - This is a wrapper for a type.
166 /// FIXME: Types should be factored much better so that CV qualifiers and
167 /// others do not require a huge and empty descriptor full of zeros.
168 class DIType : public DIDescriptor {
171 FlagPrivate = 1 << 0,
172 FlagProtected = 1 << 1,
177 DIType(GlobalVariable *GV, unsigned Tag) : DIDescriptor(GV, Tag) {}
178 // This ctor is used when the Tag has already been validated by a derived
180 DIType(GlobalVariable *GV, bool, bool) : DIDescriptor(GV) {}
183 /// isDerivedType - Return true if the specified tag is legal for
185 static bool isDerivedType(unsigned TAG);
187 /// isCompositeType - Return true if the specified tag is legal for
189 static bool isCompositeType(unsigned TAG);
191 /// isBasicType - Return true if the specified tag is legal for
193 static bool isBasicType(unsigned TAG) {
194 return TAG == dwarf::DW_TAG_base_type;
197 /// Verify - Verify that a type descriptor is well formed.
200 explicit DIType(GlobalVariable *GV);
204 DIDescriptor getContext() const { return getDescriptorField(1); }
205 const std::string &getName(std::string &F) const {
206 return getStringField(2, F);
208 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
209 unsigned getLineNumber() const { return getUnsignedField(4); }
210 uint64_t getSizeInBits() const { return getUInt64Field(5); }
211 uint64_t getAlignInBits() const { return getUInt64Field(6); }
212 // FIXME: Offset is only used for DW_TAG_member nodes. Making every type
213 // carry this is just plain insane.
214 uint64_t getOffsetInBits() const { return getUInt64Field(7); }
215 unsigned getFlags() const { return getUnsignedField(8); }
216 bool isPrivate() const { return (getFlags() & FlagPrivate) != 0; }
217 bool isProtected() const { return (getFlags() & FlagProtected) != 0; }
218 bool isForwardDecl() const { return (getFlags() & FlagFwdDecl) != 0; }
220 /// dump - print type.
224 /// DIBasicType - A basic type, like 'int' or 'float'.
225 class DIBasicType : public DIType {
227 explicit DIBasicType(GlobalVariable *GV)
228 : DIType(GV, dwarf::DW_TAG_base_type) {}
230 unsigned getEncoding() const { return getUnsignedField(9); }
232 /// dump - print basic type.
236 /// DIDerivedType - A simple derived type, like a const qualified type,
237 /// a typedef, a pointer or reference, etc.
238 class DIDerivedType : public DIType {
240 explicit DIDerivedType(GlobalVariable *GV, bool, bool)
241 : DIType(GV, true, true) {}
243 explicit DIDerivedType(GlobalVariable *GV)
244 : DIType(GV, true, true) {
245 if (GV && !isDerivedType(getTag()))
249 DIType getTypeDerivedFrom() const { return getFieldAs<DIType>(9); }
251 /// getOriginalTypeSize - If this type is derived from a base type then
252 /// return base type size.
253 uint64_t getOriginalTypeSize() const;
254 /// dump - print derived type.
257 /// replaceAllUsesWith - Replace all uses of debug info referenced by
258 /// this descriptor. After this completes, the current debug info value
260 void replaceAllUsesWith(DIDescriptor &D);
263 /// DICompositeType - This descriptor holds a type that can refer to multiple
264 /// other types, like a function or struct.
265 /// FIXME: Why is this a DIDerivedType??
266 class DICompositeType : public DIDerivedType {
268 explicit DICompositeType(GlobalVariable *GV)
269 : DIDerivedType(GV, true, true) {
270 if (GV && !isCompositeType(getTag()))
274 DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
275 unsigned getRunTimeLang() const { return getUnsignedField(11); }
277 /// Verify - Verify that a composite type descriptor is well formed.
280 /// dump - print composite type.
284 /// DIGlobal - This is a common class for global variables and subprograms.
285 class DIGlobal : public DIDescriptor {
287 explicit DIGlobal(GlobalVariable *GV, unsigned RequiredTag)
288 : DIDescriptor(GV, RequiredTag) {}
290 /// isSubprogram - Return true if the specified tag is legal for
292 static bool isSubprogram(unsigned TAG) {
293 return TAG == dwarf::DW_TAG_subprogram;
296 /// isGlobalVariable - Return true if the specified tag is legal for
297 /// DIGlobalVariable.
298 static bool isGlobalVariable(unsigned TAG) {
299 return TAG == dwarf::DW_TAG_variable;
303 virtual ~DIGlobal() {}
305 DIDescriptor getContext() const { return getDescriptorField(2); }
306 const std::string &getName(std::string &F) const {
307 return getStringField(3, F);
309 const std::string &getDisplayName(std::string &F) const {
310 return getStringField(4, F);
312 const std::string &getLinkageName(std::string &F) const {
313 return getStringField(5, F);
315 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(6); }
316 unsigned getLineNumber() const { return getUnsignedField(7); }
317 DIType getType() const { return getFieldAs<DIType>(8); }
319 /// isLocalToUnit - Return true if this subprogram is local to the current
320 /// compile unit, like 'static' in C.
321 unsigned isLocalToUnit() const { return getUnsignedField(9); }
322 unsigned isDefinition() const { return getUnsignedField(10); }
324 /// dump - print global.
328 /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
329 class DISubprogram : public DIGlobal {
331 explicit DISubprogram(GlobalVariable *GV = 0)
332 : DIGlobal(GV, dwarf::DW_TAG_subprogram) {}
334 DICompositeType getType() const { return getFieldAs<DICompositeType>(8); }
336 /// getReturnTypeName - Subprogram return types are encoded either as
337 /// DIType or as DICompositeType.
338 const std::string &getReturnTypeName(std::string &F) const {
339 DICompositeType DCT(getFieldAs<DICompositeType>(8));
341 DIArray A = DCT.getTypeArray();
342 DIType T(A.getElement(0).getGV());
345 DIType T(getFieldAs<DIType>(8));
349 /// Verify - Verify that a subprogram descriptor is well formed.
352 /// dump - print subprogram.
355 /// describes - Return true if this subprogram provides debugging
356 /// information for the function F.
357 bool describes(const Function *F);
360 /// DIGlobalVariable - This is a wrapper for a global variable.
361 class DIGlobalVariable : public DIGlobal {
363 explicit DIGlobalVariable(GlobalVariable *GV = 0)
364 : DIGlobal(GV, dwarf::DW_TAG_variable) {}
366 GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
368 /// Verify - Verify that a global variable descriptor is well formed.
371 /// dump - print global variable.
375 /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
377 class DIVariable : public DIDescriptor {
379 explicit DIVariable(GlobalVariable *GV = 0)
381 if (GV && !isVariable(getTag()))
385 DIDescriptor getContext() const { return getDescriptorField(1); }
386 const std::string &getName(std::string &F) const {
387 return getStringField(2, F);
389 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
390 unsigned getLineNumber() const { return getUnsignedField(4); }
391 DIType getType() const { return getFieldAs<DIType>(5); }
393 /// isVariable - Return true if the specified tag is legal for DIVariable.
394 static bool isVariable(unsigned Tag);
396 /// Verify - Verify that a variable descriptor is well formed.
399 /// dump - print variable.
403 /// DIBlock - This is a wrapper for a block (e.g. a function, scope, etc).
404 class DIBlock : public DIDescriptor {
406 explicit DIBlock(GlobalVariable *GV = 0)
407 : DIDescriptor(GV, dwarf::DW_TAG_lexical_block) {}
409 DIDescriptor getContext() const { return getDescriptorField(1); }
412 /// DIFactory - This object assists with the construction of the various
416 LLVMContext& VMContext;
418 // Cached values for uniquing and faster lookups.
419 const Type *EmptyStructPtr; // "{}*".
420 Function *StopPointFn; // llvm.dbg.stoppoint
421 Function *FuncStartFn; // llvm.dbg.func.start
422 Function *RegionStartFn; // llvm.dbg.region.start
423 Function *RegionEndFn; // llvm.dbg.region.end
424 Function *DeclareFn; // llvm.dbg.declare
425 StringMap<Constant*> StringCache;
426 DenseMap<Constant*, DIDescriptor> SimpleConstantCache;
428 DIFactory(const DIFactory &); // DO NOT IMPLEMENT
429 void operator=(const DIFactory&); // DO NOT IMPLEMENT
431 explicit DIFactory(Module &m);
433 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
434 /// This implicitly uniques the arrays created.
435 DIArray GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys);
437 /// GetOrCreateSubrange - Create a descriptor for a value range. This
438 /// implicitly uniques the values returned.
439 DISubrange GetOrCreateSubrange(int64_t Lo, int64_t Hi);
441 /// CreateCompileUnit - Create a new descriptor for the specified compile
443 DICompileUnit CreateCompileUnit(unsigned LangID,
444 const std::string &Filename,
445 const std::string &Directory,
446 const std::string &Producer,
448 bool isOptimized = false,
449 const char *Flags = "",
450 unsigned RunTimeVer = 0);
452 /// CreateEnumerator - Create a single enumerator value.
453 DIEnumerator CreateEnumerator(const std::string &Name, uint64_t Val);
455 /// CreateBasicType - Create a basic type like int, float, etc.
456 DIBasicType CreateBasicType(DIDescriptor Context, const std::string &Name,
457 DICompileUnit CompileUnit, unsigned LineNumber,
458 uint64_t SizeInBits, uint64_t AlignInBits,
459 uint64_t OffsetInBits, unsigned Flags,
462 /// CreateDerivedType - Create a derived type like const qualified type,
463 /// pointer, typedef, etc.
464 DIDerivedType CreateDerivedType(unsigned Tag, DIDescriptor Context,
465 const std::string &Name,
466 DICompileUnit CompileUnit,
468 uint64_t SizeInBits, uint64_t AlignInBits,
469 uint64_t OffsetInBits, unsigned Flags,
472 /// CreateCompositeType - Create a composite type like array, struct, etc.
473 DICompositeType CreateCompositeType(unsigned Tag, DIDescriptor Context,
474 const std::string &Name,
475 DICompileUnit CompileUnit,
478 uint64_t AlignInBits,
479 uint64_t OffsetInBits, unsigned Flags,
482 unsigned RunTimeLang = 0);
484 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
485 /// See comments in DISubprogram for descriptions of these fields.
486 DISubprogram CreateSubprogram(DIDescriptor Context, const std::string &Name,
487 const std::string &DisplayName,
488 const std::string &LinkageName,
489 DICompileUnit CompileUnit, unsigned LineNo,
490 DIType Type, bool isLocalToUnit,
493 /// CreateGlobalVariable - Create a new descriptor for the specified global.
495 CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
496 const std::string &DisplayName,
497 const std::string &LinkageName,
498 DICompileUnit CompileUnit,
499 unsigned LineNo, DIType Type, bool isLocalToUnit,
500 bool isDefinition, llvm::GlobalVariable *GV);
502 /// CreateVariable - Create a new descriptor for the specified variable.
503 DIVariable CreateVariable(unsigned Tag, DIDescriptor Context,
504 const std::string &Name,
505 DICompileUnit CompileUnit, unsigned LineNo,
508 /// CreateBlock - This creates a descriptor for a lexical block with the
509 /// specified parent context.
510 DIBlock CreateBlock(DIDescriptor Context);
512 /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
513 /// inserting it at the end of the specified basic block.
514 void InsertStopPoint(DICompileUnit CU, unsigned LineNo, unsigned ColNo,
517 /// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
518 /// mark the start of the specified subprogram.
519 void InsertSubprogramStart(DISubprogram SP, BasicBlock *BB);
521 /// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
522 /// mark the start of a region for the specified scoping descriptor.
523 void InsertRegionStart(DIDescriptor D, BasicBlock *BB);
525 /// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
526 /// mark the end of a region for the specified scoping descriptor.
527 void InsertRegionEnd(DIDescriptor D, BasicBlock *BB);
529 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
530 void InsertDeclare(llvm::Value *Storage, DIVariable D, BasicBlock *BB);
533 Constant *GetTagConstant(unsigned TAG);
534 Constant *GetStringConstant(const std::string &String);
536 /// getCastToEmpty - Return the descriptor as a Constant* with type '{}*'.
537 Constant *getCastToEmpty(DIDescriptor D);
540 /// Finds the stoppoint coressponding to this instruction, that is the
541 /// stoppoint that dominates this instruction
542 const DbgStopPointInst *findStopPoint(const Instruction *Inst);
544 /// Finds the stoppoint corresponding to first real (non-debug intrinsic)
545 /// instruction in this Basic Block, and returns the stoppoint for it.
546 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB);
548 /// Finds the dbg.declare intrinsic corresponding to this value if any.
549 /// It looks through pointer casts too.
550 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts = true);
552 /// Find the debug info descriptor corresponding to this global variable.
553 Value *findDbgGlobalDeclare(GlobalVariable *V);
555 bool getLocationInfo(const Value *V, std::string &DisplayName, std::string &Type,
556 unsigned &LineNo, std::string &File, std::string &Dir);
558 /// CollectDebugInfoAnchors - Collect debugging information anchors.
559 void CollectDebugInfoAnchors(Module &M,
560 SmallVector<GlobalVariable *, 2> &CompileUnits,
561 SmallVector<GlobalVariable *, 4> &GlobalVars,
562 SmallVector<GlobalVariable *, 4> &Subprograms);
564 /// isValidDebugInfoIntrinsic - Return true if SPI is a valid debug
566 bool isValidDebugInfoIntrinsic(DbgStopPointInst &SPI,
567 CodeGenOpt::Level OptLev);
569 /// isValidDebugInfoIntrinsic - Return true if FSI is a valid debug
571 bool isValidDebugInfoIntrinsic(DbgFuncStartInst &FSI,
572 CodeGenOpt::Level OptLev);
574 /// isValidDebugInfoIntrinsic - Return true if RSI is a valid debug
576 bool isValidDebugInfoIntrinsic(DbgRegionStartInst &RSI,
577 CodeGenOpt::Level OptLev);
579 /// isValidDebugInfoIntrinsic - Return true if REI is a valid debug
581 bool isValidDebugInfoIntrinsic(DbgRegionEndInst &REI,
582 CodeGenOpt::Level OptLev);
584 /// isValidDebugInfoIntrinsic - Return true if DI is a valid debug
586 bool isValidDebugInfoIntrinsic(DbgDeclareInst &DI,
587 CodeGenOpt::Level OptLev);
589 /// ExtractDebugLocation - Extract debug location information
590 /// from llvm.dbg.stoppoint intrinsic.
591 DebugLoc ExtractDebugLocation(DbgStopPointInst &SPI,
592 DebugLocTracker &DebugLocInfo);
594 /// ExtractDebugLocation - Extract debug location information
595 /// from llvm.dbg.func_start intrinsic.
596 DebugLoc ExtractDebugLocation(DbgFuncStartInst &FSI,
597 DebugLocTracker &DebugLocInfo);
599 /// isInlinedFnStart - Return true if FSI is starting an inlined function.
600 bool isInlinedFnStart(DbgFuncStartInst &FSI, const Function *CurrentFn);
602 /// isInlinedFnEnd - Return true if REI is ending an inlined function.
603 bool isInlinedFnEnd(DbgRegionEndInst &REI, const Function *CurrentFn);
605 } // end namespace llvm