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;
40 GlobalVariable *DbgGV;
42 /// DIDescriptor constructor. If the specified GV is non-null, this checks
43 /// to make sure that the tag in the descriptor matches 'RequiredTag'. If
44 /// not, the debug info is corrupt and we ignore it.
45 DIDescriptor(GlobalVariable *GV, unsigned RequiredTag);
47 const std::string &getStringField(unsigned Elt, std::string &Result) const;
48 unsigned getUnsignedField(unsigned Elt) const {
49 return (unsigned)getUInt64Field(Elt);
51 uint64_t getUInt64Field(unsigned Elt) const;
52 DIDescriptor getDescriptorField(unsigned Elt) const;
54 template <typename DescTy>
55 DescTy getFieldAs(unsigned Elt) const {
56 return DescTy(getDescriptorField(Elt).getGV());
59 GlobalVariable *getGlobalVariableField(unsigned Elt) const;
62 explicit DIDescriptor() : DbgGV(0) {}
63 explicit DIDescriptor(GlobalVariable *GV) : DbgGV(GV) {}
65 bool isNull() const { return DbgGV == 0; }
67 GlobalVariable *getGV() const { return DbgGV; }
69 unsigned getVersion() const {
70 return getUnsignedField(0) & LLVMDebugVersionMask;
73 unsigned getTag() const {
74 return getUnsignedField(0) & ~LLVMDebugVersionMask;
77 /// ValidDebugInfo - Return true if V represents valid debug info value.
78 static bool ValidDebugInfo(Value *V, CodeGenOpt::Level OptLevel);
80 /// dump - print descriptor.
84 /// DISubrange - This is used to represent ranges, for array bounds.
85 class DISubrange : public DIDescriptor {
87 explicit DISubrange(GlobalVariable *GV = 0)
88 : DIDescriptor(GV, dwarf::DW_TAG_subrange_type) {}
90 int64_t getLo() const { return (int64_t)getUInt64Field(1); }
91 int64_t getHi() const { return (int64_t)getUInt64Field(2); }
94 /// DIArray - This descriptor holds an array of descriptors.
95 class DIArray : public DIDescriptor {
97 explicit DIArray(GlobalVariable *GV = 0) : DIDescriptor(GV) {}
99 unsigned getNumElements() const;
100 DIDescriptor getElement(unsigned Idx) const {
101 return getDescriptorField(Idx);
105 /// DICompileUnit - A wrapper for a compile unit.
106 class DICompileUnit : public DIDescriptor {
108 explicit DICompileUnit(GlobalVariable *GV = 0)
109 : DIDescriptor(GV, dwarf::DW_TAG_compile_unit) {}
111 unsigned getLanguage() const { return getUnsignedField(2); }
112 const std::string &getFilename(std::string &F) const {
113 return getStringField(3, F);
115 const std::string &getDirectory(std::string &F) const {
116 return getStringField(4, F);
118 const std::string &getProducer(std::string &F) const {
119 return getStringField(5, F);
122 /// isMain - Each input file is encoded as a separate compile unit in LLVM
123 /// debugging information output. However, many target specific tool chains
124 /// prefer to encode only one compile unit in an object file. In this
125 /// situation, the LLVM code generator will include debugging information
126 /// entities in the compile unit that is marked as main compile unit. The
127 /// code generator accepts maximum one main compile unit per module. If a
128 /// module does not contain any main compile unit then the code generator
129 /// will emit multiple compile units in the output object file.
131 bool isMain() const { return getUnsignedField(6); }
132 bool isOptimized() const { return getUnsignedField(7); }
133 const std::string &getFlags(std::string &F) const {
134 return getStringField(8, F);
136 unsigned getRunTimeVersion() const { return getUnsignedField(9); }
138 /// Verify - Verify that a compile unit is well formed.
141 /// dump - print compile unit.
145 /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
146 /// FIXME: it seems strange that this doesn't have either a reference to the
147 /// type/precision or a file/line pair for location info.
148 class DIEnumerator : public DIDescriptor {
150 explicit DIEnumerator(GlobalVariable *GV = 0)
151 : DIDescriptor(GV, dwarf::DW_TAG_enumerator) {}
153 const std::string &getName(std::string &F) const {
154 return getStringField(1, F);
156 uint64_t getEnumValue() const { return getUInt64Field(2); }
159 /// DIType - This is a wrapper for a type.
160 /// FIXME: Types should be factored much better so that CV qualifiers and
161 /// others do not require a huge and empty descriptor full of zeros.
162 class DIType : public DIDescriptor {
165 FlagPrivate = 1 << 0,
166 FlagProtected = 1 << 1,
171 DIType(GlobalVariable *GV, unsigned Tag) : DIDescriptor(GV, Tag) {}
172 // This ctor is used when the Tag has already been validated by a derived
174 DIType(GlobalVariable *GV, bool, bool) : DIDescriptor(GV) {}
177 /// isDerivedType - Return true if the specified tag is legal for
179 static bool isDerivedType(unsigned TAG);
181 /// isCompositeType - Return true if the specified tag is legal for
183 static bool isCompositeType(unsigned TAG);
185 /// isBasicType - Return true if the specified tag is legal for
187 static bool isBasicType(unsigned TAG) {
188 return TAG == dwarf::DW_TAG_base_type;
191 /// Verify - Verify that a type descriptor is well formed.
194 explicit DIType(GlobalVariable *GV);
198 DIDescriptor getContext() const { return getDescriptorField(1); }
199 const std::string &getName(std::string &F) const {
200 return getStringField(2, F);
202 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
203 unsigned getLineNumber() const { return getUnsignedField(4); }
204 uint64_t getSizeInBits() const { return getUInt64Field(5); }
205 uint64_t getAlignInBits() const { return getUInt64Field(6); }
206 // FIXME: Offset is only used for DW_TAG_member nodes. Making every type
207 // carry this is just plain insane.
208 uint64_t getOffsetInBits() const { return getUInt64Field(7); }
209 unsigned getFlags() const { return getUnsignedField(8); }
210 bool isPrivate() const { return (getFlags() & FlagPrivate) != 0; }
211 bool isProtected() const { return (getFlags() & FlagProtected) != 0; }
212 bool isForwardDecl() const { return (getFlags() & FlagFwdDecl) != 0; }
214 /// dump - print type.
218 /// DIBasicType - A basic type, like 'int' or 'float'.
219 class DIBasicType : public DIType {
221 explicit DIBasicType(GlobalVariable *GV)
222 : DIType(GV, dwarf::DW_TAG_base_type) {}
224 unsigned getEncoding() const { return getUnsignedField(9); }
226 /// dump - print basic type.
230 /// DIDerivedType - A simple derived type, like a const qualified type,
231 /// a typedef, a pointer or reference, etc.
232 class DIDerivedType : public DIType {
234 explicit DIDerivedType(GlobalVariable *GV, bool, bool)
235 : DIType(GV, true, true) {}
237 explicit DIDerivedType(GlobalVariable *GV)
238 : DIType(GV, true, true) {
239 if (GV && !isDerivedType(getTag()))
243 DIType getTypeDerivedFrom() const { return getFieldAs<DIType>(9); }
245 /// getOriginalTypeSize - If this type is derived from a base type then
246 /// return base type size.
247 uint64_t getOriginalTypeSize() const;
248 /// dump - print derived type.
252 /// DICompositeType - This descriptor holds a type that can refer to multiple
253 /// other types, like a function or struct.
254 /// FIXME: Why is this a DIDerivedType??
255 class DICompositeType : public DIDerivedType {
257 explicit DICompositeType(GlobalVariable *GV)
258 : DIDerivedType(GV, true, true) {
259 if (GV && !isCompositeType(getTag()))
263 DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
264 unsigned getRunTimeLang() const { return getUnsignedField(11); }
266 /// Verify - Verify that a composite type descriptor is well formed.
269 /// dump - print composite type.
273 /// DIGlobal - This is a common class for global variables and subprograms.
274 class DIGlobal : public DIDescriptor {
276 explicit DIGlobal(GlobalVariable *GV, unsigned RequiredTag)
277 : DIDescriptor(GV, RequiredTag) {}
279 /// isSubprogram - Return true if the specified tag is legal for
281 static bool isSubprogram(unsigned TAG) {
282 return TAG == dwarf::DW_TAG_subprogram;
285 /// isGlobalVariable - Return true if the specified tag is legal for
286 /// DIGlobalVariable.
287 static bool isGlobalVariable(unsigned TAG) {
288 return TAG == dwarf::DW_TAG_variable;
292 virtual ~DIGlobal() {}
294 DIDescriptor getContext() const { return getDescriptorField(2); }
295 const std::string &getName(std::string &F) const {
296 return getStringField(3, F);
298 const std::string &getDisplayName(std::string &F) const {
299 return getStringField(4, F);
301 const std::string &getLinkageName(std::string &F) const {
302 return getStringField(5, F);
304 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(6); }
305 unsigned getLineNumber() const { return getUnsignedField(7); }
306 DIType getType() const { return getFieldAs<DIType>(8); }
308 /// isLocalToUnit - Return true if this subprogram is local to the current
309 /// compile unit, like 'static' in C.
310 unsigned isLocalToUnit() const { return getUnsignedField(9); }
311 unsigned isDefinition() const { return getUnsignedField(10); }
313 /// dump - print global.
317 /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
318 class DISubprogram : public DIGlobal {
320 explicit DISubprogram(GlobalVariable *GV = 0)
321 : DIGlobal(GV, dwarf::DW_TAG_subprogram) {}
323 DICompositeType getType() const { return getFieldAs<DICompositeType>(8); }
325 /// getReturnTypeName - Subprogram return types are encoded either as
326 /// DIType or as DICompositeType.
327 const std::string &getReturnTypeName(std::string &F) const {
328 DICompositeType DCT(getFieldAs<DICompositeType>(8));
330 DIArray A = DCT.getTypeArray();
331 DIType T(A.getElement(0).getGV());
334 DIType T(getFieldAs<DIType>(8));
338 /// Verify - Verify that a subprogram descriptor is well formed.
341 /// dump - print subprogram.
344 /// describes - Return true if this subprogram provides debugging
345 /// information for the function F.
346 bool describes(const Function *F);
349 /// DIGlobalVariable - This is a wrapper for a global variable.
350 class DIGlobalVariable : public DIGlobal {
352 explicit DIGlobalVariable(GlobalVariable *GV = 0)
353 : DIGlobal(GV, dwarf::DW_TAG_variable) {}
355 GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
357 /// Verify - Verify that a global variable descriptor is well formed.
360 /// dump - print global variable.
364 /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
366 class DIVariable : public DIDescriptor {
368 explicit DIVariable(GlobalVariable *GV = 0)
370 if (GV && !isVariable(getTag()))
374 DIDescriptor getContext() const { return getDescriptorField(1); }
375 const std::string &getName(std::string &F) const {
376 return getStringField(2, F);
378 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
379 unsigned getLineNumber() const { return getUnsignedField(4); }
380 DIType getType() const { return getFieldAs<DIType>(5); }
382 /// isVariable - Return true if the specified tag is legal for DIVariable.
383 static bool isVariable(unsigned Tag);
385 /// Verify - Verify that a variable descriptor is well formed.
388 /// dump - print variable.
392 /// DIBlock - This is a wrapper for a block (e.g. a function, scope, etc).
393 class DIBlock : public DIDescriptor {
395 explicit DIBlock(GlobalVariable *GV = 0)
396 : DIDescriptor(GV, dwarf::DW_TAG_lexical_block) {}
398 DIDescriptor getContext() const { return getDescriptorField(1); }
401 /// DIFactory - This object assists with the construction of the various
405 // Cached values for uniquing and faster lookups.
406 const Type *EmptyStructPtr; // "{}*".
407 Function *StopPointFn; // llvm.dbg.stoppoint
408 Function *FuncStartFn; // llvm.dbg.func.start
409 Function *RegionStartFn; // llvm.dbg.region.start
410 Function *RegionEndFn; // llvm.dbg.region.end
411 Function *DeclareFn; // llvm.dbg.declare
412 StringMap<Constant*> StringCache;
413 DenseMap<Constant*, DIDescriptor> SimpleConstantCache;
415 DIFactory(const DIFactory &); // DO NOT IMPLEMENT
416 void operator=(const DIFactory&); // DO NOT IMPLEMENT
418 explicit DIFactory(Module &m);
420 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
421 /// This implicitly uniques the arrays created.
422 DIArray GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys);
424 /// GetOrCreateSubrange - Create a descriptor for a value range. This
425 /// implicitly uniques the values returned.
426 DISubrange GetOrCreateSubrange(int64_t Lo, int64_t Hi);
428 /// CreateCompileUnit - Create a new descriptor for the specified compile
430 DICompileUnit CreateCompileUnit(unsigned LangID,
431 const std::string &Filename,
432 const std::string &Directory,
433 const std::string &Producer,
435 bool isOptimized = false,
436 const char *Flags = "",
437 unsigned RunTimeVer = 0);
439 /// CreateEnumerator - Create a single enumerator value.
440 DIEnumerator CreateEnumerator(const std::string &Name, uint64_t Val);
442 /// CreateBasicType - Create a basic type like int, float, etc.
443 DIBasicType CreateBasicType(DIDescriptor Context, const std::string &Name,
444 DICompileUnit CompileUnit, unsigned LineNumber,
445 uint64_t SizeInBits, uint64_t AlignInBits,
446 uint64_t OffsetInBits, unsigned Flags,
449 /// CreateDerivedType - Create a derived type like const qualified type,
450 /// pointer, typedef, etc.
451 DIDerivedType CreateDerivedType(unsigned Tag, DIDescriptor Context,
452 const std::string &Name,
453 DICompileUnit CompileUnit,
455 uint64_t SizeInBits, uint64_t AlignInBits,
456 uint64_t OffsetInBits, unsigned Flags,
459 /// CreateCompositeType - Create a composite type like array, struct, etc.
460 DICompositeType CreateCompositeType(unsigned Tag, DIDescriptor Context,
461 const std::string &Name,
462 DICompileUnit CompileUnit,
465 uint64_t AlignInBits,
466 uint64_t OffsetInBits, unsigned Flags,
469 unsigned RunTimeLang = 0);
471 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
472 /// See comments in DISubprogram for descriptions of these fields.
473 DISubprogram CreateSubprogram(DIDescriptor Context, const std::string &Name,
474 const std::string &DisplayName,
475 const std::string &LinkageName,
476 DICompileUnit CompileUnit, unsigned LineNo,
477 DIType Type, bool isLocalToUnit,
480 /// CreateGlobalVariable - Create a new descriptor for the specified global.
482 CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
483 const std::string &DisplayName,
484 const std::string &LinkageName,
485 DICompileUnit CompileUnit,
486 unsigned LineNo, DIType Type, bool isLocalToUnit,
487 bool isDefinition, llvm::GlobalVariable *GV);
489 /// CreateVariable - Create a new descriptor for the specified variable.
490 DIVariable CreateVariable(unsigned Tag, DIDescriptor Context,
491 const std::string &Name,
492 DICompileUnit CompileUnit, unsigned LineNo,
495 /// CreateBlock - This creates a descriptor for a lexical block with the
496 /// specified parent context.
497 DIBlock CreateBlock(DIDescriptor Context);
499 /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
500 /// inserting it at the end of the specified basic block.
501 void InsertStopPoint(DICompileUnit CU, unsigned LineNo, unsigned ColNo,
504 /// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
505 /// mark the start of the specified subprogram.
506 void InsertSubprogramStart(DISubprogram SP, BasicBlock *BB);
508 /// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
509 /// mark the start of a region for the specified scoping descriptor.
510 void InsertRegionStart(DIDescriptor D, BasicBlock *BB);
512 /// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
513 /// mark the end of a region for the specified scoping descriptor.
514 void InsertRegionEnd(DIDescriptor D, BasicBlock *BB);
516 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
517 void InsertDeclare(llvm::Value *Storage, DIVariable D, BasicBlock *BB);
520 Constant *GetTagConstant(unsigned TAG);
521 Constant *GetStringConstant(const std::string &String);
523 /// getCastToEmpty - Return the descriptor as a Constant* with type '{}*'.
524 Constant *getCastToEmpty(DIDescriptor D);
527 /// Finds the stoppoint coressponding to this instruction, that is the
528 /// stoppoint that dominates this instruction
529 const DbgStopPointInst *findStopPoint(const Instruction *Inst);
531 /// Finds the stoppoint corresponding to first real (non-debug intrinsic)
532 /// instruction in this Basic Block, and returns the stoppoint for it.
533 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB);
535 /// Finds the dbg.declare intrinsic corresponding to this value if any.
536 /// It looks through pointer casts too.
537 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts = true);
539 /// Find the debug info descriptor corresponding to this global variable.
540 Value *findDbgGlobalDeclare(GlobalVariable *V);
542 bool getLocationInfo(const Value *V, std::string &DisplayName, std::string &Type,
543 unsigned &LineNo, std::string &File, std::string &Dir);
545 /// CollectDebugInfoAnchors - Collect debugging information anchors.
546 void CollectDebugInfoAnchors(Module &M,
547 SmallVector<GlobalVariable *, 2> &CompileUnits,
548 SmallVector<GlobalVariable *, 4> &GlobalVars,
549 SmallVector<GlobalVariable *, 4> &Subprograms);
551 } // end namespace llvm