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 class DebugLocTracker;
45 GlobalVariable *DbgGV;
47 /// DIDescriptor constructor. If the specified GV is non-null, this checks
48 /// to make sure that the tag in the descriptor matches 'RequiredTag'. If
49 /// not, the debug info is corrupt and we ignore it.
50 DIDescriptor(GlobalVariable *GV, unsigned RequiredTag);
52 const std::string &getStringField(unsigned Elt, std::string &Result) const;
53 unsigned getUnsignedField(unsigned Elt) const {
54 return (unsigned)getUInt64Field(Elt);
56 uint64_t getUInt64Field(unsigned Elt) const;
57 DIDescriptor getDescriptorField(unsigned Elt) const;
59 template <typename DescTy>
60 DescTy getFieldAs(unsigned Elt) const {
61 return DescTy(getDescriptorField(Elt).getGV());
64 GlobalVariable *getGlobalVariableField(unsigned Elt) const;
67 explicit DIDescriptor() : DbgGV(0) {}
68 explicit DIDescriptor(GlobalVariable *GV) : DbgGV(GV) {}
70 bool isNull() const { return DbgGV == 0; }
72 GlobalVariable *getGV() const { return DbgGV; }
74 unsigned getVersion() const {
75 return getUnsignedField(0) & LLVMDebugVersionMask;
78 unsigned getTag() const {
79 return getUnsignedField(0) & ~LLVMDebugVersionMask;
82 /// ValidDebugInfo - Return true if V represents valid debug info value.
83 static bool ValidDebugInfo(Value *V, CodeGenOpt::Level OptLevel);
85 /// dump - print descriptor.
89 /// DISubrange - This is used to represent ranges, for array bounds.
90 class DISubrange : public DIDescriptor {
92 explicit DISubrange(GlobalVariable *GV = 0)
93 : DIDescriptor(GV, dwarf::DW_TAG_subrange_type) {}
95 int64_t getLo() const { return (int64_t)getUInt64Field(1); }
96 int64_t getHi() const { return (int64_t)getUInt64Field(2); }
99 /// DIArray - This descriptor holds an array of descriptors.
100 class DIArray : public DIDescriptor {
102 explicit DIArray(GlobalVariable *GV = 0) : DIDescriptor(GV) {}
104 unsigned getNumElements() const;
105 DIDescriptor getElement(unsigned Idx) const {
106 return getDescriptorField(Idx);
110 /// DICompileUnit - A wrapper for a compile unit.
111 class DICompileUnit : public DIDescriptor {
113 explicit DICompileUnit(GlobalVariable *GV = 0)
114 : DIDescriptor(GV, dwarf::DW_TAG_compile_unit) {}
116 unsigned getLanguage() const { return getUnsignedField(2); }
117 const std::string &getFilename(std::string &F) const {
118 return getStringField(3, F);
120 const std::string &getDirectory(std::string &F) const {
121 return getStringField(4, F);
123 const std::string &getProducer(std::string &F) const {
124 return getStringField(5, F);
127 /// isMain - Each input file is encoded as a separate compile unit in LLVM
128 /// debugging information output. However, many target specific tool chains
129 /// prefer to encode only one compile unit in an object file. In this
130 /// situation, the LLVM code generator will include debugging information
131 /// entities in the compile unit that is marked as main compile unit. The
132 /// code generator accepts maximum one main compile unit per module. If a
133 /// module does not contain any main compile unit then the code generator
134 /// will emit multiple compile units in the output object file.
136 bool isMain() const { return getUnsignedField(6); }
137 bool isOptimized() const { return getUnsignedField(7); }
138 const std::string &getFlags(std::string &F) const {
139 return getStringField(8, F);
141 unsigned getRunTimeVersion() const { return getUnsignedField(9); }
143 /// Verify - Verify that a compile unit is well formed.
146 /// dump - print compile unit.
150 /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
151 /// FIXME: it seems strange that this doesn't have either a reference to the
152 /// type/precision or a file/line pair for location info.
153 class DIEnumerator : public DIDescriptor {
155 explicit DIEnumerator(GlobalVariable *GV = 0)
156 : DIDescriptor(GV, dwarf::DW_TAG_enumerator) {}
158 const std::string &getName(std::string &F) const {
159 return getStringField(1, F);
161 uint64_t getEnumValue() const { return getUInt64Field(2); }
164 /// DIType - This is a wrapper for a type.
165 /// FIXME: Types should be factored much better so that CV qualifiers and
166 /// others do not require a huge and empty descriptor full of zeros.
167 class DIType : public DIDescriptor {
170 FlagPrivate = 1 << 0,
171 FlagProtected = 1 << 1,
176 DIType(GlobalVariable *GV, unsigned Tag) : DIDescriptor(GV, Tag) {}
177 // This ctor is used when the Tag has already been validated by a derived
179 DIType(GlobalVariable *GV, bool, bool) : DIDescriptor(GV) {}
182 /// isDerivedType - Return true if the specified tag is legal for
184 static bool isDerivedType(unsigned TAG);
186 /// isCompositeType - Return true if the specified tag is legal for
188 static bool isCompositeType(unsigned TAG);
190 /// isBasicType - Return true if the specified tag is legal for
192 static bool isBasicType(unsigned TAG) {
193 return TAG == dwarf::DW_TAG_base_type;
196 /// Verify - Verify that a type descriptor is well formed.
199 explicit DIType(GlobalVariable *GV);
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 { return (getFlags() & FlagPrivate) != 0; }
216 bool isProtected() const { return (getFlags() & FlagProtected) != 0; }
217 bool isForwardDecl() const { return (getFlags() & FlagFwdDecl) != 0; }
219 /// dump - print type.
223 /// DIBasicType - A basic type, like 'int' or 'float'.
224 class DIBasicType : public DIType {
226 explicit DIBasicType(GlobalVariable *GV)
227 : DIType(GV, dwarf::DW_TAG_base_type) {}
229 unsigned getEncoding() const { return getUnsignedField(9); }
231 /// dump - print basic type.
235 /// DIDerivedType - A simple derived type, like a const qualified type,
236 /// a typedef, a pointer or reference, etc.
237 class DIDerivedType : public DIType {
239 explicit DIDerivedType(GlobalVariable *GV, bool, bool)
240 : DIType(GV, true, true) {}
242 explicit DIDerivedType(GlobalVariable *GV)
243 : DIType(GV, true, true) {
244 if (GV && !isDerivedType(getTag()))
248 DIType getTypeDerivedFrom() const { return getFieldAs<DIType>(9); }
250 /// getOriginalTypeSize - If this type is derived from a base type then
251 /// return base type size.
252 uint64_t getOriginalTypeSize() const;
253 /// dump - print derived type.
257 /// DICompositeType - This descriptor holds a type that can refer to multiple
258 /// other types, like a function or struct.
259 /// FIXME: Why is this a DIDerivedType??
260 class DICompositeType : public DIDerivedType {
262 explicit DICompositeType(GlobalVariable *GV)
263 : DIDerivedType(GV, true, true) {
264 if (GV && !isCompositeType(getTag()))
268 DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
269 unsigned getRunTimeLang() const { return getUnsignedField(11); }
271 /// Verify - Verify that a composite type descriptor is well formed.
274 /// dump - print composite type.
278 /// DIGlobal - This is a common class for global variables and subprograms.
279 class DIGlobal : public DIDescriptor {
281 explicit DIGlobal(GlobalVariable *GV, unsigned RequiredTag)
282 : DIDescriptor(GV, RequiredTag) {}
284 /// isSubprogram - Return true if the specified tag is legal for
286 static bool isSubprogram(unsigned TAG) {
287 return TAG == dwarf::DW_TAG_subprogram;
290 /// isGlobalVariable - Return true if the specified tag is legal for
291 /// DIGlobalVariable.
292 static bool isGlobalVariable(unsigned TAG) {
293 return TAG == dwarf::DW_TAG_variable;
297 virtual ~DIGlobal() {}
299 DIDescriptor getContext() const { return getDescriptorField(2); }
300 const std::string &getName(std::string &F) const {
301 return getStringField(3, F);
303 const std::string &getDisplayName(std::string &F) const {
304 return getStringField(4, F);
306 const std::string &getLinkageName(std::string &F) const {
307 return getStringField(5, F);
309 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(6); }
310 unsigned getLineNumber() const { return getUnsignedField(7); }
311 DIType getType() const { return getFieldAs<DIType>(8); }
313 /// isLocalToUnit - Return true if this subprogram is local to the current
314 /// compile unit, like 'static' in C.
315 unsigned isLocalToUnit() const { return getUnsignedField(9); }
316 unsigned isDefinition() const { return getUnsignedField(10); }
318 /// dump - print global.
322 /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
323 class DISubprogram : public DIGlobal {
325 explicit DISubprogram(GlobalVariable *GV = 0)
326 : DIGlobal(GV, dwarf::DW_TAG_subprogram) {}
328 DICompositeType getType() const { return getFieldAs<DICompositeType>(8); }
330 /// getReturnTypeName - Subprogram return types are encoded either as
331 /// DIType or as DICompositeType.
332 const std::string &getReturnTypeName(std::string &F) const {
333 DICompositeType DCT(getFieldAs<DICompositeType>(8));
335 DIArray A = DCT.getTypeArray();
336 DIType T(A.getElement(0).getGV());
339 DIType T(getFieldAs<DIType>(8));
343 /// Verify - Verify that a subprogram descriptor is well formed.
346 /// dump - print subprogram.
349 /// describes - Return true if this subprogram provides debugging
350 /// information for the function F.
351 bool describes(const Function *F);
354 /// DIGlobalVariable - This is a wrapper for a global variable.
355 class DIGlobalVariable : public DIGlobal {
357 explicit DIGlobalVariable(GlobalVariable *GV = 0)
358 : DIGlobal(GV, dwarf::DW_TAG_variable) {}
360 GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
362 /// Verify - Verify that a global variable descriptor is well formed.
365 /// dump - print global variable.
369 /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
371 class DIVariable : public DIDescriptor {
373 explicit DIVariable(GlobalVariable *GV = 0)
375 if (GV && !isVariable(getTag()))
379 DIDescriptor getContext() const { return getDescriptorField(1); }
380 const std::string &getName(std::string &F) const {
381 return getStringField(2, F);
383 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
384 unsigned getLineNumber() const { return getUnsignedField(4); }
385 DIType getType() const { return getFieldAs<DIType>(5); }
387 /// isVariable - Return true if the specified tag is legal for DIVariable.
388 static bool isVariable(unsigned Tag);
390 /// Verify - Verify that a variable descriptor is well formed.
393 /// dump - print variable.
397 /// DIBlock - This is a wrapper for a block (e.g. a function, scope, etc).
398 class DIBlock : public DIDescriptor {
400 explicit DIBlock(GlobalVariable *GV = 0)
401 : DIDescriptor(GV, dwarf::DW_TAG_lexical_block) {}
403 DIDescriptor getContext() const { return getDescriptorField(1); }
406 /// DIFactory - This object assists with the construction of the various
410 // Cached values for uniquing and faster lookups.
411 const Type *EmptyStructPtr; // "{}*".
412 Function *StopPointFn; // llvm.dbg.stoppoint
413 Function *FuncStartFn; // llvm.dbg.func.start
414 Function *RegionStartFn; // llvm.dbg.region.start
415 Function *RegionEndFn; // llvm.dbg.region.end
416 Function *DeclareFn; // llvm.dbg.declare
417 StringMap<Constant*> StringCache;
418 DenseMap<Constant*, DIDescriptor> SimpleConstantCache;
420 DIFactory(const DIFactory &); // DO NOT IMPLEMENT
421 void operator=(const DIFactory&); // DO NOT IMPLEMENT
423 explicit DIFactory(Module &m);
425 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
426 /// This implicitly uniques the arrays created.
427 DIArray GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys);
429 /// GetOrCreateSubrange - Create a descriptor for a value range. This
430 /// implicitly uniques the values returned.
431 DISubrange GetOrCreateSubrange(int64_t Lo, int64_t Hi);
433 /// CreateCompileUnit - Create a new descriptor for the specified compile
435 DICompileUnit CreateCompileUnit(unsigned LangID,
436 const std::string &Filename,
437 const std::string &Directory,
438 const std::string &Producer,
440 bool isOptimized = false,
441 const char *Flags = "",
442 unsigned RunTimeVer = 0);
444 /// CreateEnumerator - Create a single enumerator value.
445 DIEnumerator CreateEnumerator(const std::string &Name, uint64_t Val);
447 /// CreateBasicType - Create a basic type like int, float, etc.
448 DIBasicType CreateBasicType(DIDescriptor Context, const std::string &Name,
449 DICompileUnit CompileUnit, unsigned LineNumber,
450 uint64_t SizeInBits, uint64_t AlignInBits,
451 uint64_t OffsetInBits, unsigned Flags,
454 /// CreateDerivedType - Create a derived type like const qualified type,
455 /// pointer, typedef, etc.
456 DIDerivedType CreateDerivedType(unsigned Tag, DIDescriptor Context,
457 const std::string &Name,
458 DICompileUnit CompileUnit,
460 uint64_t SizeInBits, uint64_t AlignInBits,
461 uint64_t OffsetInBits, unsigned Flags,
464 /// CreateCompositeType - Create a composite type like array, struct, etc.
465 DICompositeType CreateCompositeType(unsigned Tag, DIDescriptor Context,
466 const std::string &Name,
467 DICompileUnit CompileUnit,
470 uint64_t AlignInBits,
471 uint64_t OffsetInBits, unsigned Flags,
474 unsigned RunTimeLang = 0);
476 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
477 /// See comments in DISubprogram for descriptions of these fields.
478 DISubprogram CreateSubprogram(DIDescriptor Context, const std::string &Name,
479 const std::string &DisplayName,
480 const std::string &LinkageName,
481 DICompileUnit CompileUnit, unsigned LineNo,
482 DIType Type, bool isLocalToUnit,
485 /// CreateGlobalVariable - Create a new descriptor for the specified global.
487 CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
488 const std::string &DisplayName,
489 const std::string &LinkageName,
490 DICompileUnit CompileUnit,
491 unsigned LineNo, DIType Type, bool isLocalToUnit,
492 bool isDefinition, llvm::GlobalVariable *GV);
494 /// CreateVariable - Create a new descriptor for the specified variable.
495 DIVariable CreateVariable(unsigned Tag, DIDescriptor Context,
496 const std::string &Name,
497 DICompileUnit CompileUnit, unsigned LineNo,
500 /// CreateBlock - This creates a descriptor for a lexical block with the
501 /// specified parent context.
502 DIBlock CreateBlock(DIDescriptor Context);
504 /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
505 /// inserting it at the end of the specified basic block.
506 void InsertStopPoint(DICompileUnit CU, unsigned LineNo, unsigned ColNo,
509 /// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
510 /// mark the start of the specified subprogram.
511 void InsertSubprogramStart(DISubprogram SP, BasicBlock *BB);
513 /// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
514 /// mark the start of a region for the specified scoping descriptor.
515 void InsertRegionStart(DIDescriptor D, BasicBlock *BB);
517 /// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
518 /// mark the end of a region for the specified scoping descriptor.
519 void InsertRegionEnd(DIDescriptor D, BasicBlock *BB);
521 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
522 void InsertDeclare(llvm::Value *Storage, DIVariable D, BasicBlock *BB);
525 Constant *GetTagConstant(unsigned TAG);
526 Constant *GetStringConstant(const std::string &String);
528 /// getCastToEmpty - Return the descriptor as a Constant* with type '{}*'.
529 Constant *getCastToEmpty(DIDescriptor D);
532 /// Finds the stoppoint coressponding to this instruction, that is the
533 /// stoppoint that dominates this instruction
534 const DbgStopPointInst *findStopPoint(const Instruction *Inst);
536 /// Finds the stoppoint corresponding to first real (non-debug intrinsic)
537 /// instruction in this Basic Block, and returns the stoppoint for it.
538 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB);
540 /// Finds the dbg.declare intrinsic corresponding to this value if any.
541 /// It looks through pointer casts too.
542 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts = true);
544 /// Find the debug info descriptor corresponding to this global variable.
545 Value *findDbgGlobalDeclare(GlobalVariable *V);
547 bool getLocationInfo(const Value *V, std::string &DisplayName, std::string &Type,
548 unsigned &LineNo, std::string &File, std::string &Dir);
550 /// CollectDebugInfoAnchors - Collect debugging information anchors.
551 void CollectDebugInfoAnchors(Module &M,
552 SmallVector<GlobalVariable *, 2> &CompileUnits,
553 SmallVector<GlobalVariable *, 4> &GlobalVars,
554 SmallVector<GlobalVariable *, 4> &Subprograms);
556 /// isValidDebugInfoIntrinsic - Return true if SPI is a valid debug
558 bool isValidDebugInfoIntrinsic(DbgStopPointInst &SPI,
559 CodeGenOpt::Level OptLev);
561 /// isValidDebugInfoIntrinsic - Return true if FSI is a valid debug
563 bool isValidDebugInfoIntrinsic(DbgFuncStartInst &FSI,
564 CodeGenOpt::Level OptLev);
566 /// isValidDebugInfoIntrinsic - Return true if RSI is a valid debug
568 bool isValidDebugInfoIntrinsic(DbgRegionStartInst &RSI,
569 CodeGenOpt::Level OptLev);
571 /// isValidDebugInfoIntrinsic - Return true if REI is a valid debug
573 bool isValidDebugInfoIntrinsic(DbgRegionEndInst &REI,
574 CodeGenOpt::Level OptLev);
576 /// isValidDebugInfoIntrinsic - Return true if DI is a valid debug
578 bool isValidDebugInfoIntrinsic(DbgDeclareInst &DI,
579 CodeGenOpt::Level OptLev);
581 /// ExtractDebugLocation - Extract debug location information
582 /// from llvm.dbg.stoppoint intrinsic.
583 DebugLoc ExtractDebugLocation(DbgStopPointInst &SPI,
584 DebugLocTracker &DebugLocInfo);
586 /// ExtractDebugLocation - Extract debug location information
587 /// from llvm.dbg.func_start intrinsic.
588 DebugLoc ExtractDebugLocation(DbgFuncStartInst &FSI,
589 DebugLocTracker &DebugLocInfo);
591 /// isInlinedFnStart - Return true if FSI is starting an inlined function.
592 bool isInlinedFnStart(DbgFuncStartInst &FSI, const Function *CurrentFn);
594 /// isInlinedFnEnd - Return true if REI is ending an inlined function.
595 bool isInlinedFnEnd(DbgRegionEndInst &REI, const Function *CurrentFn);
597 } // end namespace llvm