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/Support/Dwarf.h"
33 struct DbgStopPointInst;
34 struct DbgDeclareInst;
39 GlobalVariable *DbgGV;
41 /// DIDescriptor constructor. If the specified GV is non-null, this checks
42 /// to make sure that the tag in the descriptor matches 'RequiredTag'. If
43 /// not, the debug info is corrupt and we ignore it.
44 DIDescriptor(GlobalVariable *GV, unsigned RequiredTag);
46 const std::string &getStringField(unsigned Elt, std::string &Result) const;
47 unsigned getUnsignedField(unsigned Elt) const {
48 return (unsigned)getUInt64Field(Elt);
50 uint64_t getUInt64Field(unsigned Elt) const;
51 DIDescriptor getDescriptorField(unsigned Elt) const;
53 template <typename DescTy>
54 DescTy getFieldAs(unsigned Elt) const {
55 return DescTy(getDescriptorField(Elt).getGV());
58 GlobalVariable *getGlobalVariableField(unsigned Elt) const;
61 explicit DIDescriptor() : DbgGV(0) {}
62 explicit DIDescriptor(GlobalVariable *GV) : DbgGV(GV) {}
64 bool isNull() const { return DbgGV == 0; }
66 GlobalVariable *getGV() const { return DbgGV; }
68 unsigned getVersion() const {
69 return getUnsignedField(0) & LLVMDebugVersionMask;
72 unsigned getTag() const {
73 return getUnsignedField(0) & ~LLVMDebugVersionMask;
76 /// ValidDebugInfo - Return true if V represents valid debug info value.
77 static bool ValidDebugInfo(Value *V, CodeGenOpt::Level OptLevel);
79 /// dump - print descriptor.
83 /// DIAnchor - A wrapper for various anchor descriptors.
84 class DIAnchor : public DIDescriptor {
86 explicit DIAnchor(GlobalVariable *GV = 0)
87 : DIDescriptor(GV, dwarf::DW_TAG_anchor) {}
89 unsigned getAnchorTag() const { return getUnsignedField(1); }
92 /// DISubrange - This is used to represent ranges, for array bounds.
93 class DISubrange : public DIDescriptor {
95 explicit DISubrange(GlobalVariable *GV = 0)
96 : DIDescriptor(GV, 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(GlobalVariable *GV = 0) : DIDescriptor(GV) {}
107 unsigned getNumElements() const;
108 DIDescriptor getElement(unsigned Idx) const {
109 return getDescriptorField(Idx);
113 /// DICompileUnit - A wrapper for a compile unit.
114 class DICompileUnit : public DIDescriptor {
116 explicit DICompileUnit(GlobalVariable *GV = 0)
117 : DIDescriptor(GV, dwarf::DW_TAG_compile_unit) {}
119 unsigned getLanguage() const { return getUnsignedField(2); }
120 const std::string &getFilename(std::string &F) const {
121 return getStringField(3, F);
123 const std::string &getDirectory(std::string &F) const {
124 return getStringField(4, F);
126 const std::string &getProducer(std::string &F) const {
127 return getStringField(5, F);
130 /// isMain - Each input file is encoded as a separate compile unit in LLVM
131 /// debugging information output. However, many target specific tool chains
132 /// prefer to encode only one compile unit in an object file. In this
133 /// situation, the LLVM code generator will include debugging information
134 /// entities in the compile unit that is marked as main compile unit. The
135 /// code generator accepts maximum one main compile unit per module. If a
136 /// module does not contain any main compile unit then the code generator
137 /// will emit multiple compile units in the output object file.
139 bool isMain() const { return getUnsignedField(6); }
140 bool isOptimized() const { return getUnsignedField(7); }
141 const std::string &getFlags(std::string &F) const {
142 return getStringField(8, F);
144 unsigned getRunTimeVersion() const { return getUnsignedField(9); }
146 /// Verify - Verify that a compile unit is well formed.
149 /// dump - print compile unit.
153 /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
154 /// FIXME: it seems strange that this doesn't have either a reference to the
155 /// type/precision or a file/line pair for location info.
156 class DIEnumerator : public DIDescriptor {
158 explicit DIEnumerator(GlobalVariable *GV = 0)
159 : DIDescriptor(GV, dwarf::DW_TAG_enumerator) {}
161 const std::string &getName(std::string &F) const {
162 return getStringField(1, F);
164 uint64_t getEnumValue() const { return getUInt64Field(2); }
167 /// DIType - This is a wrapper for a type.
168 /// FIXME: Types should be factored much better so that CV qualifiers and
169 /// others do not require a huge and empty descriptor full of zeros.
170 class DIType : public DIDescriptor {
173 FlagPrivate = 1 << 0,
174 FlagProtected = 1 << 1,
179 DIType(GlobalVariable *GV, unsigned Tag) : DIDescriptor(GV, Tag) {}
180 // This ctor is used when the Tag has already been validated by a derived
182 DIType(GlobalVariable *GV, bool, bool) : DIDescriptor(GV) {}
185 /// isDerivedType - Return true if the specified tag is legal for
187 static bool isDerivedType(unsigned TAG);
189 /// isCompositeType - Return true if the specified tag is legal for
191 static bool isCompositeType(unsigned TAG);
193 /// isBasicType - Return true if the specified tag is legal for
195 static bool isBasicType(unsigned TAG) {
196 return TAG == dwarf::DW_TAG_base_type;
199 /// Verify - Verify that a type descriptor is well formed.
202 explicit DIType(GlobalVariable *GV);
206 DIDescriptor getContext() const { return getDescriptorField(1); }
207 const std::string &getName(std::string &F) const {
208 return getStringField(2, F);
210 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
211 unsigned getLineNumber() const { return getUnsignedField(4); }
212 uint64_t getSizeInBits() const { return getUInt64Field(5); }
213 uint64_t getAlignInBits() const { return getUInt64Field(6); }
214 // FIXME: Offset is only used for DW_TAG_member nodes. Making every type
215 // carry this is just plain insane.
216 uint64_t getOffsetInBits() const { return getUInt64Field(7); }
217 unsigned getFlags() const { return getUnsignedField(8); }
218 bool isPrivate() const { return (getFlags() & FlagPrivate) != 0; }
219 bool isProtected() const { return (getFlags() & FlagProtected) != 0; }
220 bool isForwardDecl() const { return (getFlags() & FlagFwdDecl) != 0; }
222 /// dump - print type.
226 /// DIBasicType - A basic type, like 'int' or 'float'.
227 class DIBasicType : public DIType {
229 explicit DIBasicType(GlobalVariable *GV)
230 : DIType(GV, dwarf::DW_TAG_base_type) {}
232 unsigned getEncoding() const { return getUnsignedField(9); }
234 /// dump - print basic type.
238 /// DIDerivedType - A simple derived type, like a const qualified type,
239 /// a typedef, a pointer or reference, etc.
240 class DIDerivedType : public DIType {
242 explicit DIDerivedType(GlobalVariable *GV, bool, bool)
243 : DIType(GV, true, true) {}
245 explicit DIDerivedType(GlobalVariable *GV)
246 : DIType(GV, true, true) {
247 if (GV && !isDerivedType(getTag()))
251 DIType getTypeDerivedFrom() const { return getFieldAs<DIType>(9); }
253 /// getOriginalTypeSize - If this type is derived from a base type then
254 /// return base type size.
255 uint64_t getOriginalTypeSize() const;
256 /// dump - print derived type.
260 /// DICompositeType - This descriptor holds a type that can refer to multiple
261 /// other types, like a function or struct.
262 /// FIXME: Why is this a DIDerivedType??
263 class DICompositeType : public DIDerivedType {
265 explicit DICompositeType(GlobalVariable *GV)
266 : DIDerivedType(GV, true, true) {
267 if (GV && !isCompositeType(getTag()))
271 DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
272 unsigned getRunTimeLang() const { return getUnsignedField(11); }
274 /// Verify - Verify that a composite type descriptor is well formed.
277 /// dump - print composite type.
281 /// DIGlobal - This is a common class for global variables and subprograms.
282 class DIGlobal : public DIDescriptor {
284 explicit DIGlobal(GlobalVariable *GV, unsigned RequiredTag)
285 : DIDescriptor(GV, RequiredTag) {}
287 /// isSubprogram - Return true if the specified tag is legal for
289 static bool isSubprogram(unsigned TAG) {
290 return TAG == dwarf::DW_TAG_subprogram;
293 /// isGlobalVariable - Return true if the specified tag is legal for
294 /// DIGlobalVariable.
295 static bool isGlobalVariable(unsigned TAG) {
296 return TAG == dwarf::DW_TAG_variable;
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(GlobalVariable *GV = 0)
329 : DIGlobal(GV, 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).getGV());
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(GlobalVariable *GV = 0)
361 : DIGlobal(GV, 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(GlobalVariable *GV = 0)
378 if (GV && !isVariable(getTag()))
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); }
390 /// isVariable - Return true if the specified tag is legal for DIVariable.
391 static bool isVariable(unsigned Tag);
393 /// Verify - Verify that a variable descriptor is well formed.
396 /// dump - print variable.
400 /// DIBlock - This is a wrapper for a block (e.g. a function, scope, etc).
401 class DIBlock : public DIDescriptor {
403 explicit DIBlock(GlobalVariable *GV = 0)
404 : DIDescriptor(GV, dwarf::DW_TAG_lexical_block) {}
406 DIDescriptor getContext() const { return getDescriptorField(1); }
409 /// DIFactory - This object assists with the construction of the various
413 // Cached values for uniquing and faster lookups.
414 DIAnchor CompileUnitAnchor, SubProgramAnchor, GlobalVariableAnchor;
415 const Type *EmptyStructPtr; // "{}*".
416 Function *StopPointFn; // llvm.dbg.stoppoint
417 Function *FuncStartFn; // llvm.dbg.func.start
418 Function *RegionStartFn; // llvm.dbg.region.start
419 Function *RegionEndFn; // llvm.dbg.region.end
420 Function *DeclareFn; // llvm.dbg.declare
421 StringMap<Constant*> StringCache;
422 DenseMap<Constant*, DIDescriptor> SimpleConstantCache;
424 DIFactory(const DIFactory &); // DO NOT IMPLEMENT
425 void operator=(const DIFactory&); // DO NOT IMPLEMENT
427 explicit DIFactory(Module &m);
429 /// GetOrCreateCompileUnitAnchor - Return the anchor for compile units,
430 /// creating a new one if there isn't already one in the module.
431 DIAnchor GetOrCreateCompileUnitAnchor();
433 /// GetOrCreateSubprogramAnchor - Return the anchor for subprograms,
434 /// creating a new one if there isn't already one in the module.
435 DIAnchor GetOrCreateSubprogramAnchor();
437 /// GetOrCreateGlobalVariableAnchor - Return the anchor for globals,
438 /// creating a new one if there isn't already one in the module.
439 DIAnchor GetOrCreateGlobalVariableAnchor();
441 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
442 /// This implicitly uniques the arrays created.
443 DIArray GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys);
445 /// GetOrCreateSubrange - Create a descriptor for a value range. This
446 /// implicitly uniques the values returned.
447 DISubrange GetOrCreateSubrange(int64_t Lo, int64_t Hi);
449 /// CreateCompileUnit - Create a new descriptor for the specified compile
451 DICompileUnit CreateCompileUnit(unsigned LangID,
452 const std::string &Filename,
453 const std::string &Directory,
454 const std::string &Producer,
456 bool isOptimized = false,
457 const char *Flags = "",
458 unsigned RunTimeVer = 0);
460 /// CreateEnumerator - Create a single enumerator value.
461 DIEnumerator CreateEnumerator(const std::string &Name, uint64_t Val);
463 /// CreateBasicType - Create a basic type like int, float, etc.
464 DIBasicType CreateBasicType(DIDescriptor Context, const std::string &Name,
465 DICompileUnit CompileUnit, unsigned LineNumber,
466 uint64_t SizeInBits, uint64_t AlignInBits,
467 uint64_t OffsetInBits, unsigned Flags,
470 /// CreateDerivedType - Create a derived type like const qualified type,
471 /// pointer, typedef, etc.
472 DIDerivedType CreateDerivedType(unsigned Tag, DIDescriptor Context,
473 const std::string &Name,
474 DICompileUnit CompileUnit,
476 uint64_t SizeInBits, uint64_t AlignInBits,
477 uint64_t OffsetInBits, unsigned Flags,
480 /// CreateCompositeType - Create a composite type like array, struct, etc.
481 DICompositeType CreateCompositeType(unsigned Tag, DIDescriptor Context,
482 const std::string &Name,
483 DICompileUnit CompileUnit,
486 uint64_t AlignInBits,
487 uint64_t OffsetInBits, unsigned Flags,
490 unsigned RunTimeLang = 0);
492 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
493 /// See comments in DISubprogram for descriptions of these fields.
494 DISubprogram CreateSubprogram(DIDescriptor Context, const std::string &Name,
495 const std::string &DisplayName,
496 const std::string &LinkageName,
497 DICompileUnit CompileUnit, unsigned LineNo,
498 DIType Type, bool isLocalToUnit,
501 /// CreateGlobalVariable - Create a new descriptor for the specified global.
503 CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
504 const std::string &DisplayName,
505 const std::string &LinkageName,
506 DICompileUnit CompileUnit,
507 unsigned LineNo, DIType Type, bool isLocalToUnit,
508 bool isDefinition, llvm::GlobalVariable *GV);
510 /// CreateVariable - Create a new descriptor for the specified variable.
511 DIVariable CreateVariable(unsigned Tag, DIDescriptor Context,
512 const std::string &Name,
513 DICompileUnit CompileUnit, unsigned LineNo,
516 /// CreateBlock - This creates a descriptor for a lexical block with the
517 /// specified parent context.
518 DIBlock CreateBlock(DIDescriptor Context);
520 /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
521 /// inserting it at the end of the specified basic block.
522 void InsertStopPoint(DICompileUnit CU, unsigned LineNo, unsigned ColNo,
525 /// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
526 /// mark the start of the specified subprogram.
527 void InsertSubprogramStart(DISubprogram SP, BasicBlock *BB);
529 /// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
530 /// mark the start of a region for the specified scoping descriptor.
531 void InsertRegionStart(DIDescriptor D, BasicBlock *BB);
533 /// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
534 /// mark the end of a region for the specified scoping descriptor.
535 void InsertRegionEnd(DIDescriptor D, BasicBlock *BB);
537 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
538 void InsertDeclare(llvm::Value *Storage, DIVariable D, BasicBlock *BB);
541 Constant *GetTagConstant(unsigned TAG);
542 Constant *GetStringConstant(const std::string &String);
543 DIAnchor GetOrCreateAnchor(unsigned TAG, const char *Name);
545 /// getCastToEmpty - Return the descriptor as a Constant* with type '{}*'.
546 Constant *getCastToEmpty(DIDescriptor D);
549 /// Finds the stoppoint coressponding to this instruction, that is the
550 /// stoppoint that dominates this instruction
551 const DbgStopPointInst *findStopPoint(const Instruction *Inst);
553 /// Finds the stoppoint corresponding to first real (non-debug intrinsic)
554 /// instruction in this Basic Block, and returns the stoppoint for it.
555 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB);
557 /// Finds the dbg.declare intrinsic corresponding to this value if any.
558 /// It looks through pointer casts too.
559 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts = true);
561 /// Find the debug info descriptor corresponding to this global variable.
562 Value *findDbgGlobalDeclare(GlobalVariable *V);
564 bool getLocationInfo(const Value *V, std::string &DisplayName, std::string &Type,
565 unsigned &LineNo, std::string &File, std::string &Dir);
566 } // end namespace llvm