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_ANALYSIS_DEBUGINFO_H
16 #define LLVM_ANALYSIS_DEBUGINFO_H
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/Support/Dwarf.h"
30 struct DbgStopPointInst;
31 struct DbgDeclareInst;
38 /// DIDescriptor constructor. If the specified GV is non-null, this checks
39 /// to make sure that the tag in the descriptor matches 'RequiredTag'. If
40 /// not, the debug info is corrupt and we ignore it.
41 DIDescriptor(GlobalVariable *GV, unsigned RequiredTag);
43 const std::string &getStringField(unsigned Elt, std::string &Result) const;
44 unsigned getUnsignedField(unsigned Elt) const {
45 return (unsigned)getUInt64Field(Elt);
47 uint64_t getUInt64Field(unsigned Elt) const;
48 DIDescriptor getDescriptorField(unsigned Elt) const;
50 template <typename DescTy>
51 DescTy getFieldAs(unsigned Elt) const {
52 return DescTy(getDescriptorField(Elt).getGV());
55 GlobalVariable *getGlobalVariableField(unsigned Elt) const;
58 explicit DIDescriptor() : GV(0) {}
59 explicit DIDescriptor(GlobalVariable *gv) : GV(gv) {}
61 bool isNull() const { return GV == 0; }
63 GlobalVariable *getGV() const { return GV; }
65 unsigned getVersion() const {
66 return getUnsignedField(0) & LLVMDebugVersionMask;
69 unsigned getTag() const {
70 return getUnsignedField(0) & ~LLVMDebugVersionMask;
75 /// DIAnchor - A wrapper for various anchor descriptors.
76 class DIAnchor : public DIDescriptor {
78 explicit DIAnchor(GlobalVariable *GV = 0);
80 unsigned getAnchorTag() const { return getUnsignedField(1); }
83 /// DISubrange - This is used to represent ranges, for array bounds.
84 class DISubrange : public DIDescriptor {
86 explicit DISubrange(GlobalVariable *GV = 0);
88 int64_t getLo() const { return (int64_t)getUInt64Field(1); }
89 int64_t getHi() const { return (int64_t)getUInt64Field(2); }
92 /// DIArray - This descriptor holds an array of descriptors.
93 class DIArray : public DIDescriptor {
95 explicit DIArray(GlobalVariable *GV = 0) : DIDescriptor(GV) {}
97 unsigned getNumElements() const;
98 DIDescriptor getElement(unsigned Idx) const {
99 return getDescriptorField(Idx);
103 /// DICompileUnit - A wrapper for a compile unit.
104 class DICompileUnit : public DIDescriptor {
106 explicit DICompileUnit(GlobalVariable *GV = 0);
108 unsigned getLanguage() const { return getUnsignedField(2); }
109 const std::string &getFilename(std::string &F) const {
110 return getStringField(3, F);
112 const std::string &getDirectory(std::string &F) const {
113 return getStringField(4, F);
115 const std::string &getProducer(std::string &F) const {
116 return getStringField(5, F);
119 /// isMain - Each input file is encoded as a separate compile unit in LLVM
120 /// debugging information output. However, many target specific tool chains
121 /// prefer to encode only one compile unit in an object file. In this
122 /// situation, the LLVM code generator will include debugging information
123 /// entities in the compile unit that is marked as main compile unit. The
124 /// code generator accepts maximum one main compile unit per module. If a
125 /// module does not contain any main compile unit then the code generator
126 /// will emit multiple compile units in the output object file.
128 bool isMain() const { return getUnsignedField(6); }
129 bool isOptimized() const { return getUnsignedField(7); }
130 const std::string &getFlags(std::string &F) const {
131 return getStringField(8, F);
133 unsigned getRunTimeVersion() const { return getUnsignedField(9); }
135 /// Verify - Verify that a compile unit is well formed.
138 /// dump - print compile unit.
142 /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
143 /// FIXME: it seems strange that this doesn't have either a reference to the
144 /// type/precision or a file/line pair for location info.
145 class DIEnumerator : public DIDescriptor {
147 explicit DIEnumerator(GlobalVariable *GV = 0);
149 const std::string &getName(std::string &F) const {
150 return getStringField(1, F);
152 uint64_t getEnumValue() const { return getUInt64Field(2); }
155 /// DIType - This is a wrapper for a type.
156 /// FIXME: Types should be factored much better so that CV qualifiers and
157 /// others do not require a huge and empty descriptor full of zeros.
158 class DIType : public DIDescriptor {
161 FlagPrivate = 1 << 0,
162 FlagProtected = 1 << 1,
167 DIType(GlobalVariable *GV, unsigned Tag) : DIDescriptor(GV, Tag) {}
168 // This ctor is used when the Tag has already been validated by a derived
170 DIType(GlobalVariable *GV, bool, bool) : DIDescriptor(GV) {}
173 /// isDerivedType - Return true if the specified tag is legal for
175 static bool isDerivedType(unsigned TAG);
177 /// isCompositeType - Return true if the specified tag is legal for
179 static bool isCompositeType(unsigned TAG);
181 /// isBasicType - Return true if the specified tag is legal for
183 static bool isBasicType(unsigned TAG) {
184 return TAG == dwarf::DW_TAG_base_type;
187 /// Verify - Verify that a type descriptor is well formed.
190 explicit DIType(GlobalVariable *GV);
194 DIDescriptor getContext() const { return getDescriptorField(1); }
195 const std::string &getName(std::string &F) const {
196 return getStringField(2, F);
198 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
199 unsigned getLineNumber() const { return getUnsignedField(4); }
200 uint64_t getSizeInBits() const { return getUInt64Field(5); }
201 uint64_t getAlignInBits() const { return getUInt64Field(6); }
202 // FIXME: Offset is only used for DW_TAG_member nodes. Making every type
203 // carry this is just plain insane.
204 uint64_t getOffsetInBits() const { return getUInt64Field(7); }
205 unsigned getFlags() const { return getUnsignedField(8); }
206 bool isPrivate() const { return (getFlags() & FlagPrivate) != 0; }
207 bool isProtected() const { return (getFlags() & FlagProtected) != 0; }
208 bool isForwardDecl() const { return (getFlags() & FlagFwdDecl) != 0; }
210 /// dump - print type.
214 /// DIBasicType - A basic type, like 'int' or 'float'.
215 class DIBasicType : public DIType {
217 explicit DIBasicType(GlobalVariable *GV);
218 unsigned getEncoding() const { return getUnsignedField(9); }
220 /// dump - print basic type.
224 /// DIDerivedType - A simple derived type, like a const qualified type,
225 /// a typedef, a pointer or reference, etc.
226 class DIDerivedType : public DIType {
228 explicit DIDerivedType(GlobalVariable *GV, bool, bool)
229 : DIType(GV, true, true) {}
231 explicit DIDerivedType(GlobalVariable *GV);
232 DIType getTypeDerivedFrom() const { return getFieldAs<DIType>(9); }
234 /// getOriginalTypeSize - If this type is derived from a base type then
235 /// return base type size.
236 uint64_t getOriginalTypeSize() const;
237 /// dump - print derived type.
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);
247 DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
248 unsigned getRunTimeLang() const { return getUnsignedField(11); }
250 /// Verify - Verify that a composite type descriptor is well formed.
253 /// dump - print composite type.
257 /// DIGlobal - This is a common class for global variables and subprograms.
258 class DIGlobal : public DIDescriptor {
260 explicit DIGlobal(GlobalVariable *GV, unsigned RequiredTag)
261 : DIDescriptor(GV, RequiredTag) {}
263 /// isSubprogram - Return true if the specified tag is legal for
265 static bool isSubprogram(unsigned TAG) {
266 return TAG == dwarf::DW_TAG_subprogram;
269 /// isGlobalVariable - Return true if the specified tag is legal for
270 /// DIGlobalVariable.
271 static bool isGlobalVariable(unsigned TAG) {
272 return TAG == dwarf::DW_TAG_variable;
276 virtual ~DIGlobal() {}
278 DIDescriptor getContext() const { return getDescriptorField(2); }
279 const std::string &getName(std::string &F) const {
280 return getStringField(3, F);
282 const std::string &getDisplayName(std::string &F) const {
283 return getStringField(4, F);
285 const std::string &getLinkageName(std::string &F) const {
286 return getStringField(5, F);
288 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(6); }
289 unsigned getLineNumber() const { return getUnsignedField(7); }
290 DIType getType() const { return getFieldAs<DIType>(8); }
292 /// isLocalToUnit - Return true if this subprogram is local to the current
293 /// compile unit, like 'static' in C.
294 unsigned isLocalToUnit() const { return getUnsignedField(9); }
295 unsigned isDefinition() const { return getUnsignedField(10); }
297 /// dump - print global.
301 /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
302 class DISubprogram : public DIGlobal {
304 explicit DISubprogram(GlobalVariable *GV = 0);
305 DICompositeType getType() const { return getFieldAs<DICompositeType>(8); }
307 /// Verify - Verify that a subprogram descriptor is well formed.
310 /// dump - print subprogram.
313 /// describes - Return true if this subprogram provides debugging
314 /// information for the function F.
315 bool describes(const Function *F);
318 /// DIGlobalVariable - This is a wrapper for a global variable.
319 class DIGlobalVariable : public DIGlobal {
321 explicit DIGlobalVariable(GlobalVariable *GV = 0);
322 GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
324 /// Verify - Verify that a global variable descriptor is well formed.
327 /// dump - print global variable.
331 /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
333 class DIVariable : public DIDescriptor {
335 explicit DIVariable(GlobalVariable *GV = 0);
337 DIDescriptor getContext() const { return getDescriptorField(1); }
338 const std::string &getName(std::string &F) const {
339 return getStringField(2, F);
341 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
342 unsigned getLineNumber() const { return getUnsignedField(4); }
343 DIType getType() const { return getFieldAs<DIType>(5); }
345 /// isVariable - Return true if the specified tag is legal for DIVariable.
346 static bool isVariable(unsigned Tag);
348 /// Verify - Verify that a variable descriptor is well formed.
351 /// dump - print variable.
355 /// DIBlock - This is a wrapper for a block (e.g. a function, scope, etc).
356 class DIBlock : public DIDescriptor {
358 explicit DIBlock(GlobalVariable *GV = 0);
360 DIDescriptor getContext() const { return getDescriptorField(1); }
363 /// DIFactory - This object assists with the construction of the various
367 // Cached values for uniquing and faster lookups.
368 DIAnchor CompileUnitAnchor, SubProgramAnchor, GlobalVariableAnchor;
369 const Type *EmptyStructPtr; // "{}*".
370 Function *StopPointFn; // llvm.dbg.stoppoint
371 Function *FuncStartFn; // llvm.dbg.func.start
372 Function *RegionStartFn; // llvm.dbg.region.start
373 Function *RegionEndFn; // llvm.dbg.region.end
374 Function *DeclareFn; // llvm.dbg.declare
375 StringMap<Constant*> StringCache;
376 DenseMap<Constant*, DIDescriptor> SimpleConstantCache;
378 DIFactory(const DIFactory &); // DO NOT IMPLEMENT
379 void operator=(const DIFactory&); // DO NOT IMPLEMENT
381 explicit DIFactory(Module &m);
383 /// GetOrCreateCompileUnitAnchor - Return the anchor for compile units,
384 /// creating a new one if there isn't already one in the module.
385 DIAnchor GetOrCreateCompileUnitAnchor();
387 /// GetOrCreateSubprogramAnchor - Return the anchor for subprograms,
388 /// creating a new one if there isn't already one in the module.
389 DIAnchor GetOrCreateSubprogramAnchor();
391 /// GetOrCreateGlobalVariableAnchor - Return the anchor for globals,
392 /// creating a new one if there isn't already one in the module.
393 DIAnchor GetOrCreateGlobalVariableAnchor();
395 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
396 /// This implicitly uniques the arrays created.
397 DIArray GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys);
399 /// GetOrCreateSubrange - Create a descriptor for a value range. This
400 /// implicitly uniques the values returned.
401 DISubrange GetOrCreateSubrange(int64_t Lo, int64_t Hi);
403 /// CreateCompileUnit - Create a new descriptor for the specified compile
405 DICompileUnit CreateCompileUnit(unsigned LangID,
406 const std::string &Filename,
407 const std::string &Directory,
408 const std::string &Producer,
410 bool isOptimized = false,
411 const char *Flags = "",
412 unsigned RunTimeVer = 0);
414 /// CreateEnumerator - Create a single enumerator value.
415 DIEnumerator CreateEnumerator(const std::string &Name, uint64_t Val);
417 /// CreateBasicType - Create a basic type like int, float, etc.
418 DIBasicType CreateBasicType(DIDescriptor Context, const std::string &Name,
419 DICompileUnit CompileUnit, unsigned LineNumber,
420 uint64_t SizeInBits, uint64_t AlignInBits,
421 uint64_t OffsetInBits, unsigned Flags,
424 /// CreateDerivedType - Create a derived type like const qualified type,
425 /// pointer, typedef, etc.
426 DIDerivedType CreateDerivedType(unsigned Tag, DIDescriptor Context,
427 const std::string &Name,
428 DICompileUnit CompileUnit,
430 uint64_t SizeInBits, uint64_t AlignInBits,
431 uint64_t OffsetInBits, unsigned Flags,
434 /// CreateCompositeType - Create a composite type like array, struct, etc.
435 DICompositeType CreateCompositeType(unsigned Tag, DIDescriptor Context,
436 const std::string &Name,
437 DICompileUnit CompileUnit,
440 uint64_t AlignInBits,
441 uint64_t OffsetInBits, unsigned Flags,
444 unsigned RunTimeLang = 0);
446 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
447 /// See comments in DISubprogram for descriptions of these fields.
448 DISubprogram CreateSubprogram(DIDescriptor Context, const std::string &Name,
449 const std::string &DisplayName,
450 const std::string &LinkageName,
451 DICompileUnit CompileUnit, unsigned LineNo,
452 DIType Type, bool isLocalToUnit,
455 /// CreateGlobalVariable - Create a new descriptor for the specified global.
457 CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
458 const std::string &DisplayName,
459 const std::string &LinkageName,
460 DICompileUnit CompileUnit,
461 unsigned LineNo, DIType Type, bool isLocalToUnit,
462 bool isDefinition, llvm::GlobalVariable *GV);
464 /// CreateVariable - Create a new descriptor for the specified variable.
465 DIVariable CreateVariable(unsigned Tag, DIDescriptor Context,
466 const std::string &Name,
467 DICompileUnit CompileUnit, unsigned LineNo,
470 /// CreateBlock - This creates a descriptor for a lexical block with the
471 /// specified parent context.
472 DIBlock CreateBlock(DIDescriptor Context);
474 /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
475 /// inserting it at the end of the specified basic block.
476 void InsertStopPoint(DICompileUnit CU, unsigned LineNo, unsigned ColNo,
479 /// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
480 /// mark the start of the specified subprogram.
481 void InsertSubprogramStart(DISubprogram SP, BasicBlock *BB);
483 /// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
484 /// mark the start of a region for the specified scoping descriptor.
485 void InsertRegionStart(DIDescriptor D, BasicBlock *BB);
487 /// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
488 /// mark the end of a region for the specified scoping descriptor.
489 void InsertRegionEnd(DIDescriptor D, BasicBlock *BB);
491 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
492 void InsertDeclare(llvm::Value *Storage, DIVariable D, BasicBlock *BB);
495 Constant *GetTagConstant(unsigned TAG);
496 Constant *GetStringConstant(const std::string &String);
497 DIAnchor GetOrCreateAnchor(unsigned TAG, const char *Name);
499 /// getCastToEmpty - Return the descriptor as a Constant* with type '{}*'.
500 Constant *getCastToEmpty(DIDescriptor D);
503 /// Finds the stoppoint coressponding to this instruction, that is the
504 /// stoppoint that dominates this instruction
505 const DbgStopPointInst *findStopPoint(const Instruction *Inst);
507 /// Finds the stoppoint corresponding to first real (non-debug intrinsic)
508 /// instruction in this Basic Block, and returns the stoppoint for it.
509 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB);
511 /// Finds the dbg.declare intrinsic corresponding to this value if any.
512 /// It looks through pointer casts too.
513 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts = true);
515 /// Find the debug info descriptor corresponding to this global variable.
516 Value *findDbgGlobalDeclare(GlobalVariable *V);
518 bool getLocationInfo(const Value *V, std::string &DisplayName, std::string &Type,
519 unsigned &LineNo, std::string &File, std::string &Dir);
520 } // end namespace llvm