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.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_DEBUGINFO_H
16 #define LLVM_SUPPORT_DEBUGINFO_H
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/Support/Dwarf.h"
30 class DbgStopPointInst;
37 Version7 = 7 << 16, // Current version of debug information.
38 Version6 = 6 << 16, // Constant for version 6.
39 Version5 = 5 << 16, // Constant for version 5.
40 Version4 = 4 << 16, // Constant for version 4.
41 VersionMask = 0xffff0000 // Mask for version number.
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 std::string getStringField(unsigned Elt) 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() : GV(0) {}
68 explicit DIDescriptor(GlobalVariable *gv) : GV(gv) {}
70 bool isNull() const { return GV == 0; }
72 GlobalVariable *getGV() const { return GV; }
74 unsigned getVersion() const {
75 return getUnsignedField(0) & VersionMask;
78 unsigned getTag() const {
79 return getUnsignedField(0) & ~VersionMask;
81 static inline bool classof(const DIDescriptor *D) { return true; }
84 /// DIAnchor - A wrapper for various anchor descriptors.
85 class DIAnchor : public DIDescriptor {
87 explicit DIAnchor(GlobalVariable *GV = 0);
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);
97 int64_t getLo() const { return (int64_t)getUInt64Field(1); }
98 int64_t getHi() const { return (int64_t)getUInt64Field(2); }
99 static inline bool classof(const DISubrange *) { return true; }
100 static inline bool classof(const DIDescriptor *D) {
101 return D->getTag() == dwarf::DW_TAG_subrange_type;
105 /// DIArray - This descriptor holds an array of descriptors.
106 class DIArray : public DIDescriptor {
108 explicit DIArray(GlobalVariable *GV = 0) : DIDescriptor(GV) {}
110 unsigned getNumElements() const;
111 DIDescriptor getElement(unsigned Idx) const {
112 return getDescriptorField(Idx);
115 static inline bool classof(const DIArray *) { return true; }
116 static inline bool classof(const DIDescriptor *D) {
117 return D->getTag() == dwarf::DW_TAG_array_type
118 || D->getTag() == dwarf::DW_AT_GNU_vector;
122 /// DICompileUnit - A wrapper for a compile unit.
123 class DICompileUnit : public DIDescriptor {
125 explicit DICompileUnit(GlobalVariable *GV = 0);
127 unsigned getLanguage() const { return getUnsignedField(2); }
128 std::string getFilename() const { return getStringField(3); }
129 std::string getDirectory() const { return getStringField(4); }
130 std::string getProducer() const { return getStringField(5); }
132 static inline bool classof(const DICompileUnit *) { return true; }
133 static inline bool classof(const DIDescriptor *D) {
134 return D->getTag() == dwarf::DW_TAG_compile_unit;
138 /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
139 /// FIXME: it seems strange that this doesn't have either a reference to the
140 /// type/precision or a file/line pair for location info.
141 class DIEnumerator : public DIDescriptor {
143 explicit DIEnumerator(GlobalVariable *GV = 0);
145 std::string getName() const { return getStringField(1); }
146 uint64_t getEnumValue() const { return getUInt64Field(2); }
147 static inline bool classof(const DIEnumerator *) { return true; }
148 static inline bool classof(const DIDescriptor *D) {
149 return D->getTag() == dwarf::DW_TAG_enumerator;
153 /// DIType - This is a wrapper for a type.
154 /// FIXME: Types should be factored much better so that CV qualifiers and
155 /// others do not require a huge and empty descriptor full of zeros.
156 class DIType : public DIDescriptor {
158 DIType(GlobalVariable *GV, unsigned Tag) : DIDescriptor(GV, Tag) {}
159 // This ctor is used when the Tag has already been validated by a derived
161 DIType(GlobalVariable *GV, bool, bool) : DIDescriptor(GV) {}
163 /// isDerivedType - Return true if the specified tag is legal for
165 static bool isDerivedType(unsigned TAG);
167 /// isCompositeType - Return true if the specified tag is legal for
169 static bool isCompositeType(unsigned TAG);
171 /// isBasicType - Return true if the specified tag is legal for
173 static bool isBasicType(unsigned TAG) {
174 return TAG == dwarf::DW_TAG_base_type;
178 explicit DIType(GlobalVariable *GV);
182 DIDescriptor getContext() const { return getDescriptorField(1); }
183 std::string getName() const { return getStringField(2); }
184 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
185 unsigned getLineNumber() const { return getUnsignedField(4); }
186 uint64_t getSizeInBits() const { return getUInt64Field(5); }
187 uint64_t getAlignInBits() const { return getUInt64Field(6); }
188 // FIXME: Offset is only used for DW_TAG_member nodes. Making every type
189 // carry this is just plain insane.
190 uint64_t getOffsetInBits() const { return getUInt64Field(7); }
191 unsigned getFlags() const { return getUnsignedField(8); }
193 virtual std::string getFilename() const {
194 assert (0 && "Invalid DIDescriptor");
198 virtual std::string getDirectory() const {
199 assert (0 && "Invalid DIDescriptor");
203 static inline bool classof(const DIType *) { return true; }
204 static inline bool classof(const DIDescriptor *D) {
205 unsigned Tag = D->getTag();
206 return isBasicType(Tag) || isDerivedType(Tag) || isCompositeType(Tag);
211 /// DIBasicType - A basic type, like 'int' or 'float'.
212 class DIBasicType : public DIType {
214 explicit DIBasicType(GlobalVariable *GV);
216 unsigned getEncoding() const { return getUnsignedField(9); }
217 std::string getFilename() const { return getStringField(10); }
218 std::string getDirectory() const { return getStringField(11); }
221 /// DIDerivedType - A simple derived type, like a const qualified type,
222 /// a typedef, a pointer or reference, etc.
223 class DIDerivedType : public DIType {
225 explicit DIDerivedType(GlobalVariable *GV, bool, bool)
226 : DIType(GV, true, true) {}
228 explicit DIDerivedType(GlobalVariable *GV);
230 DIType getTypeDerivedFrom() const { return getFieldAs<DIType>(9); }
231 std::string getFilename() const { return getStringField(10); }
232 std::string getDirectory() const { return getStringField(11); }
234 static inline bool classof(const DIDerivedType *) { return true; }
235 static inline bool classof(const DIDescriptor *D) {
236 return isDerivedType(D->getTag());
241 /// DICompositeType - This descriptor holds a type that can refer to multiple
242 /// other types, like a function or struct.
243 /// FIXME: Why is this a DIDerivedType??
244 class DICompositeType : public DIDerivedType {
246 explicit DICompositeType(GlobalVariable *GV);
248 DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
249 std::string getFilename() const { return getStringField(11); }
250 std::string getDirectory() const { return getStringField(12); }
252 static inline bool classof(const DIDerivedType *) { return true; }
253 static inline bool classof(const DIDescriptor *D) {
254 return isCompositeType(D->getTag());
258 /// DIGlobal - This is a common class for global variables and subprograms.
259 class DIGlobal : public DIDescriptor {
261 explicit DIGlobal(GlobalVariable *GV, unsigned RequiredTag)
262 : DIDescriptor(GV, RequiredTag) {}
264 /// isSubprogram - Return true if the specified tag is legal for
266 static bool isSubprogram(unsigned TAG) {
267 return TAG == dwarf::DW_TAG_subprogram;
270 /// isGlobalVariable - Return true if the specified tag is legal for
271 /// DIGlobalVariable.
272 static bool isGlobalVariable(unsigned TAG) {
273 return TAG == dwarf::DW_TAG_variable;
277 virtual ~DIGlobal() {}
279 DIDescriptor getContext() const { return getDescriptorField(2); }
280 std::string getName() const { return getStringField(3); }
281 std::string getDisplayName() const { return getStringField(4); }
282 std::string getLinkageName() const { return getStringField(5); }
284 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(6); }
285 unsigned getLineNumber() const { return getUnsignedField(7); }
286 DIType getType() const { return getFieldAs<DIType>(8); }
288 /// isLocalToUnit - Return true if this subprogram is local to the current
289 /// compile unit, like 'static' in C.
290 unsigned isLocalToUnit() const { return getUnsignedField(9); }
291 unsigned isDefinition() const { return getUnsignedField(10); }
293 virtual std::string getFilename() const {
294 assert (0 && "Invalid DIDescriptor");
298 virtual std::string getDirectory() const {
299 assert (0 && "Invalid DIDescriptor");
303 static inline bool classof(const DIGlobal *) { return true; }
304 static inline bool classof(const DIDescriptor *D) {
305 unsigned Tag = D->getTag();
306 return isSubprogram(Tag) || isGlobalVariable(Tag);
311 /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
312 class DISubprogram : public DIGlobal {
314 explicit DISubprogram(GlobalVariable *GV = 0);
315 std::string getFilename() const { return getStringField(11); }
316 std::string getDirectory() const { return getStringField(12); }
317 DICompositeType getType() const { return getFieldAs<DICompositeType>(8); }
318 static inline bool classof(const DISubprogram *) { return true; }
319 static inline bool classof(const DIDescriptor *D) {
320 return isSubprogram(D->getTag());
324 /// DIGlobalVariable - This is a wrapper for a global variable.
325 class DIGlobalVariable : public DIGlobal {
327 explicit DIGlobalVariable(GlobalVariable *GV = 0);
329 GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
330 std::string getFilename() const { return getStringField(12); }
331 std::string getDirectory() const { return getStringField(13); }
332 static inline bool classof(const DIGlobalVariable *) { return true; }
333 static inline bool classof(const DIDescriptor *D) {
334 return isGlobalVariable(D->getTag());
339 /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
341 class DIVariable : public DIDescriptor {
343 explicit DIVariable(GlobalVariable *GV = 0);
345 DIDescriptor getContext() const { return getDescriptorField(1); }
346 std::string getName() const { return getStringField(2); }
348 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
349 unsigned getLineNumber() const { return getUnsignedField(4); }
350 DIType getType() const { return getFieldAs<DIType>(5); }
351 std::string getFilename() const { return getStringField(6); }
352 std::string getDirectory() const { return getStringField(7); }
354 /// isVariable - Return true if the specified tag is legal for DIVariable.
355 static bool isVariable(unsigned Tag);
356 static inline bool classof(const DIVariable *) { return true; }
357 static inline bool classof(const DIDescriptor *D) {
358 return isVariable(D->getTag());
363 /// DIBlock - This is a wrapper for a block (e.g. a function, scope, etc).
364 class DIBlock : public DIDescriptor {
366 explicit DIBlock(GlobalVariable *GV = 0);
368 DIDescriptor getContext() const { return getDescriptorField(1); }
369 static inline bool classof(const DIBlock *) { return true; }
370 static inline bool classof(const DIDescriptor *D) {
371 return D->getTag() == dwarf::DW_TAG_lexical_block;
375 /// DIFactory - This object assists with the construction of the various
379 // Cached values for uniquing and faster lookups.
380 DIAnchor CompileUnitAnchor, SubProgramAnchor, GlobalVariableAnchor;
381 const Type *EmptyStructPtr; // "{}*".
382 Function *StopPointFn; // llvm.dbg.stoppoint
383 Function *FuncStartFn; // llvm.dbg.func.start
384 Function *RegionStartFn; // llvm.dbg.region.start
385 Function *RegionEndFn; // llvm.dbg.region.end
386 Function *DeclareFn; // llvm.dbg.declare
387 StringMap<Constant*> StringCache;
388 DenseMap<Constant*, DIDescriptor> SimpleConstantCache;
390 DIFactory(const DIFactory &); // DO NOT IMPLEMENT
391 void operator=(const DIFactory&); // DO NOT IMPLEMENT
393 explicit DIFactory(Module &m);
395 /// GetOrCreateCompileUnitAnchor - Return the anchor for compile units,
396 /// creating a new one if there isn't already one in the module.
397 DIAnchor GetOrCreateCompileUnitAnchor();
399 /// GetOrCreateSubprogramAnchor - Return the anchor for subprograms,
400 /// creating a new one if there isn't already one in the module.
401 DIAnchor GetOrCreateSubprogramAnchor();
403 /// GetOrCreateGlobalVariableAnchor - Return the anchor for globals,
404 /// creating a new one if there isn't already one in the module.
405 DIAnchor GetOrCreateGlobalVariableAnchor();
407 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
408 /// This implicitly uniques the arrays created.
409 DIArray GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys);
411 /// GetOrCreateSubrange - Create a descriptor for a value range. This
412 /// implicitly uniques the values returned.
413 DISubrange GetOrCreateSubrange(int64_t Lo, int64_t Hi);
416 /// CreateCompileUnit - Create a new descriptor for the specified compile
418 DICompileUnit CreateCompileUnit(unsigned LangID,
419 const std::string &Filename,
420 const std::string &Directory,
421 const std::string &Producer);
423 /// CreateEnumerator - Create a single enumerator value.
424 DIEnumerator CreateEnumerator(const std::string &Name, uint64_t Val);
426 /// CreateBasicType - Create a basic type like int, float, etc.
427 DIBasicType CreateBasicType(DIDescriptor Context, const std::string &Name,
428 DICompileUnit CompileUnit, unsigned LineNumber,
429 uint64_t SizeInBits, uint64_t AlignInBits,
430 uint64_t OffsetInBits, unsigned Flags,
432 const std::string *FileName = 0,
433 const std::string *Directory = 0);
435 /// CreateDerivedType - Create a derived type like const qualified type,
436 /// pointer, typedef, etc.
437 DIDerivedType CreateDerivedType(unsigned Tag, DIDescriptor Context,
438 const std::string &Name,
439 DICompileUnit CompileUnit,
441 uint64_t SizeInBits, uint64_t AlignInBits,
442 uint64_t OffsetInBits, unsigned Flags,
444 const std::string *FileName = 0,
445 const std::string *Directory = 0);
447 /// CreateCompositeType - Create a composite type like array, struct, etc.
448 DICompositeType CreateCompositeType(unsigned Tag, DIDescriptor Context,
449 const std::string &Name,
450 DICompileUnit CompileUnit,
453 uint64_t AlignInBits,
454 uint64_t OffsetInBits, unsigned Flags,
457 const std::string *FileName = 0,
458 const std::string *Directory = 0);
460 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
461 /// See comments in DISubprogram for descriptions of these fields.
462 DISubprogram CreateSubprogram(DIDescriptor Context, const std::string &Name,
463 const std::string &DisplayName,
464 const std::string &LinkageName,
465 DICompileUnit CompileUnit, unsigned LineNo,
466 DIType Type, bool isLocalToUnit,
468 const std::string *FileName = 0,
469 const std::string *Directory = 0);
471 /// CreateGlobalVariable - Create a new descriptor for the specified global.
473 CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
474 const std::string &DisplayName,
475 const std::string &LinkageName,
476 DICompileUnit CompileUnit,
477 unsigned LineNo, DIType Type, bool isLocalToUnit,
478 bool isDefinition, llvm::GlobalVariable *GV,
479 const std::string *FileName = 0,
480 const std::string *Directory = 0);
483 /// CreateVariable - Create a new descriptor for the specified variable.
484 DIVariable CreateVariable(unsigned Tag, DIDescriptor Context,
485 const std::string &Name,
486 DICompileUnit CompileUnit, unsigned LineNo,
488 const std::string *FileName = 0,
489 const std::string *Directory = 0);
491 /// CreateBlock - This creates a descriptor for a lexical block with the
492 /// specified parent context.
493 DIBlock CreateBlock(DIDescriptor Context);
495 /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
496 /// inserting it at the end of the specified basic block.
497 void InsertStopPoint(DICompileUnit CU, unsigned LineNo, unsigned ColNo,
500 /// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
501 /// mark the start of the specified subprogram.
502 void InsertSubprogramStart(DISubprogram SP, BasicBlock *BB);
504 /// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
505 /// mark the start of a region for the specified scoping descriptor.
506 void InsertRegionStart(DIDescriptor D, BasicBlock *BB);
508 /// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
509 /// mark the end of a region for the specified scoping descriptor.
510 void InsertRegionEnd(DIDescriptor D, BasicBlock *BB);
512 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
513 void InsertDeclare(llvm::Value *Storage, DIVariable D, BasicBlock *BB);
516 Constant *GetTagConstant(unsigned TAG);
517 Constant *GetStringConstant(const std::string &String);
518 DIAnchor GetOrCreateAnchor(unsigned TAG, const char *Name);
520 /// getCastToEmpty - Return the descriptor as a Constant* with type '{}*'.
521 Constant *getCastToEmpty(DIDescriptor D);
524 /// Finds the stoppoint coressponding to this instruction, that is the
525 /// stoppoint that dominates this instruction
526 const DbgStopPointInst *findStopPoint(const Instruction *Inst);
528 /// Finds the stoppoint corresponding to first real (non-debug intrinsic)
529 /// instruction in this Basic Block, and returns the stoppoint for it.
530 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB);
532 /// Finds the dbg.declare intrinsic corresponding to this value if any.
533 /// It looks through pointer casts too.
534 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts = true);
535 } // end namespace llvm