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;
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() : GV(0) {}
62 explicit DIDescriptor(GlobalVariable *gv) : GV(gv) {}
64 bool isNull() const { return GV == 0; }
66 GlobalVariable *getGV() const { return GV; }
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 /// Verify - Verify that a subprogram descriptor is well formed.
336 /// dump - print subprogram.
339 /// describes - Return true if this subprogram provides debugging
340 /// information for the function F.
341 bool describes(const Function *F);
344 /// DIGlobalVariable - This is a wrapper for a global variable.
345 class DIGlobalVariable : public DIGlobal {
347 explicit DIGlobalVariable(GlobalVariable *GV = 0)
348 : DIGlobal(GV, dwarf::DW_TAG_variable) {}
350 GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
352 /// Verify - Verify that a global variable descriptor is well formed.
355 /// dump - print global variable.
359 /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
361 class DIVariable : public DIDescriptor {
363 explicit DIVariable(GlobalVariable *gv = 0)
365 if (gv && !isVariable(getTag()))
369 DIDescriptor getContext() const { return getDescriptorField(1); }
370 const std::string &getName(std::string &F) const {
371 return getStringField(2, F);
373 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
374 unsigned getLineNumber() const { return getUnsignedField(4); }
375 DIType getType() const { return getFieldAs<DIType>(5); }
377 /// isVariable - Return true if the specified tag is legal for DIVariable.
378 static bool isVariable(unsigned Tag);
380 /// Verify - Verify that a variable descriptor is well formed.
383 /// dump - print variable.
387 /// DIBlock - This is a wrapper for a block (e.g. a function, scope, etc).
388 class DIBlock : public DIDescriptor {
390 explicit DIBlock(GlobalVariable *GV = 0)
391 : DIDescriptor(GV, dwarf::DW_TAG_lexical_block) {}
393 DIDescriptor getContext() const { return getDescriptorField(1); }
396 /// DIFactory - This object assists with the construction of the various
400 // Cached values for uniquing and faster lookups.
401 DIAnchor CompileUnitAnchor, SubProgramAnchor, GlobalVariableAnchor;
402 const Type *EmptyStructPtr; // "{}*".
403 Function *StopPointFn; // llvm.dbg.stoppoint
404 Function *FuncStartFn; // llvm.dbg.func.start
405 Function *RegionStartFn; // llvm.dbg.region.start
406 Function *RegionEndFn; // llvm.dbg.region.end
407 Function *DeclareFn; // llvm.dbg.declare
408 StringMap<Constant*> StringCache;
409 DenseMap<Constant*, DIDescriptor> SimpleConstantCache;
411 DIFactory(const DIFactory &); // DO NOT IMPLEMENT
412 void operator=(const DIFactory&); // DO NOT IMPLEMENT
414 explicit DIFactory(Module &m);
416 /// GetOrCreateCompileUnitAnchor - Return the anchor for compile units,
417 /// creating a new one if there isn't already one in the module.
418 DIAnchor GetOrCreateCompileUnitAnchor();
420 /// GetOrCreateSubprogramAnchor - Return the anchor for subprograms,
421 /// creating a new one if there isn't already one in the module.
422 DIAnchor GetOrCreateSubprogramAnchor();
424 /// GetOrCreateGlobalVariableAnchor - Return the anchor for globals,
425 /// creating a new one if there isn't already one in the module.
426 DIAnchor GetOrCreateGlobalVariableAnchor();
428 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
429 /// This implicitly uniques the arrays created.
430 DIArray GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys);
432 /// GetOrCreateSubrange - Create a descriptor for a value range. This
433 /// implicitly uniques the values returned.
434 DISubrange GetOrCreateSubrange(int64_t Lo, int64_t Hi);
436 /// CreateCompileUnit - Create a new descriptor for the specified compile
438 DICompileUnit CreateCompileUnit(unsigned LangID,
439 const std::string &Filename,
440 const std::string &Directory,
441 const std::string &Producer,
443 bool isOptimized = false,
444 const char *Flags = "",
445 unsigned RunTimeVer = 0);
447 /// CreateEnumerator - Create a single enumerator value.
448 DIEnumerator CreateEnumerator(const std::string &Name, uint64_t Val);
450 /// CreateBasicType - Create a basic type like int, float, etc.
451 DIBasicType CreateBasicType(DIDescriptor Context, const std::string &Name,
452 DICompileUnit CompileUnit, unsigned LineNumber,
453 uint64_t SizeInBits, uint64_t AlignInBits,
454 uint64_t OffsetInBits, unsigned Flags,
457 /// CreateDerivedType - Create a derived type like const qualified type,
458 /// pointer, typedef, etc.
459 DIDerivedType CreateDerivedType(unsigned Tag, DIDescriptor Context,
460 const std::string &Name,
461 DICompileUnit CompileUnit,
463 uint64_t SizeInBits, uint64_t AlignInBits,
464 uint64_t OffsetInBits, unsigned Flags,
467 /// CreateCompositeType - Create a composite type like array, struct, etc.
468 DICompositeType CreateCompositeType(unsigned Tag, DIDescriptor Context,
469 const std::string &Name,
470 DICompileUnit CompileUnit,
473 uint64_t AlignInBits,
474 uint64_t OffsetInBits, unsigned Flags,
477 unsigned RunTimeLang = 0);
479 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
480 /// See comments in DISubprogram for descriptions of these fields.
481 DISubprogram CreateSubprogram(DIDescriptor Context, const std::string &Name,
482 const std::string &DisplayName,
483 const std::string &LinkageName,
484 DICompileUnit CompileUnit, unsigned LineNo,
485 DIType Type, bool isLocalToUnit,
488 /// CreateGlobalVariable - Create a new descriptor for the specified global.
490 CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
491 const std::string &DisplayName,
492 const std::string &LinkageName,
493 DICompileUnit CompileUnit,
494 unsigned LineNo, DIType Type, bool isLocalToUnit,
495 bool isDefinition, llvm::GlobalVariable *GV);
497 /// CreateVariable - Create a new descriptor for the specified variable.
498 DIVariable CreateVariable(unsigned Tag, DIDescriptor Context,
499 const std::string &Name,
500 DICompileUnit CompileUnit, unsigned LineNo,
503 /// CreateBlock - This creates a descriptor for a lexical block with the
504 /// specified parent context.
505 DIBlock CreateBlock(DIDescriptor Context);
507 /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
508 /// inserting it at the end of the specified basic block.
509 void InsertStopPoint(DICompileUnit CU, unsigned LineNo, unsigned ColNo,
512 /// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
513 /// mark the start of the specified subprogram.
514 void InsertSubprogramStart(DISubprogram SP, BasicBlock *BB);
516 /// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
517 /// mark the start of a region for the specified scoping descriptor.
518 void InsertRegionStart(DIDescriptor D, BasicBlock *BB);
520 /// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
521 /// mark the end of a region for the specified scoping descriptor.
522 void InsertRegionEnd(DIDescriptor D, BasicBlock *BB);
524 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
525 void InsertDeclare(llvm::Value *Storage, DIVariable D, BasicBlock *BB);
528 Constant *GetTagConstant(unsigned TAG);
529 Constant *GetStringConstant(const std::string &String);
530 DIAnchor GetOrCreateAnchor(unsigned TAG, const char *Name);
532 /// getCastToEmpty - Return the descriptor as a Constant* with type '{}*'.
533 Constant *getCastToEmpty(DIDescriptor D);
536 /// Finds the stoppoint coressponding to this instruction, that is the
537 /// stoppoint that dominates this instruction
538 const DbgStopPointInst *findStopPoint(const Instruction *Inst);
540 /// Finds the stoppoint corresponding to first real (non-debug intrinsic)
541 /// instruction in this Basic Block, and returns the stoppoint for it.
542 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB);
544 /// Finds the dbg.declare intrinsic corresponding to this value if any.
545 /// It looks through pointer casts too.
546 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts = true);
548 /// Find the debug info descriptor corresponding to this global variable.
549 Value *findDbgGlobalDeclare(GlobalVariable *V);
551 bool getLocationInfo(const Value *V, std::string &DisplayName, std::string &Type,
552 unsigned &LineNo, std::string &File, std::string &Dir);
553 } // end namespace llvm