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.
314 /// DIGlobalVariable - This is a wrapper for a global variable.
315 class DIGlobalVariable : public DIGlobal {
317 explicit DIGlobalVariable(GlobalVariable *GV = 0);
318 GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
320 /// Verify - Verify that a global variable descriptor is well formed.
323 /// dump - print global variable.
327 /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
329 class DIVariable : public DIDescriptor {
331 explicit DIVariable(GlobalVariable *GV = 0);
333 DIDescriptor getContext() const { return getDescriptorField(1); }
334 const std::string &getName(std::string &F) const {
335 return getStringField(2, F);
337 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
338 unsigned getLineNumber() const { return getUnsignedField(4); }
339 DIType getType() const { return getFieldAs<DIType>(5); }
341 /// isVariable - Return true if the specified tag is legal for DIVariable.
342 static bool isVariable(unsigned Tag);
344 /// Verify - Verify that a variable descriptor is well formed.
347 /// dump - print variable.
351 /// DIBlock - This is a wrapper for a block (e.g. a function, scope, etc).
352 class DIBlock : public DIDescriptor {
354 explicit DIBlock(GlobalVariable *GV = 0);
356 DIDescriptor getContext() const { return getDescriptorField(1); }
359 /// DIFactory - This object assists with the construction of the various
363 // Cached values for uniquing and faster lookups.
364 DIAnchor CompileUnitAnchor, SubProgramAnchor, GlobalVariableAnchor;
365 const Type *EmptyStructPtr; // "{}*".
366 Function *StopPointFn; // llvm.dbg.stoppoint
367 Function *FuncStartFn; // llvm.dbg.func.start
368 Function *RegionStartFn; // llvm.dbg.region.start
369 Function *RegionEndFn; // llvm.dbg.region.end
370 Function *DeclareFn; // llvm.dbg.declare
371 StringMap<Constant*> StringCache;
372 DenseMap<Constant*, DIDescriptor> SimpleConstantCache;
374 DIFactory(const DIFactory &); // DO NOT IMPLEMENT
375 void operator=(const DIFactory&); // DO NOT IMPLEMENT
377 explicit DIFactory(Module &m);
379 /// GetOrCreateCompileUnitAnchor - Return the anchor for compile units,
380 /// creating a new one if there isn't already one in the module.
381 DIAnchor GetOrCreateCompileUnitAnchor();
383 /// GetOrCreateSubprogramAnchor - Return the anchor for subprograms,
384 /// creating a new one if there isn't already one in the module.
385 DIAnchor GetOrCreateSubprogramAnchor();
387 /// GetOrCreateGlobalVariableAnchor - Return the anchor for globals,
388 /// creating a new one if there isn't already one in the module.
389 DIAnchor GetOrCreateGlobalVariableAnchor();
391 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
392 /// This implicitly uniques the arrays created.
393 DIArray GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys);
395 /// GetOrCreateSubrange - Create a descriptor for a value range. This
396 /// implicitly uniques the values returned.
397 DISubrange GetOrCreateSubrange(int64_t Lo, int64_t Hi);
399 /// CreateCompileUnit - Create a new descriptor for the specified compile
401 DICompileUnit CreateCompileUnit(unsigned LangID,
402 const std::string &Filename,
403 const std::string &Directory,
404 const std::string &Producer,
406 bool isOptimized = false,
407 const char *Flags = "",
408 unsigned RunTimeVer = 0);
410 /// CreateEnumerator - Create a single enumerator value.
411 DIEnumerator CreateEnumerator(const std::string &Name, uint64_t Val);
413 /// CreateBasicType - Create a basic type like int, float, etc.
414 DIBasicType CreateBasicType(DIDescriptor Context, const std::string &Name,
415 DICompileUnit CompileUnit, unsigned LineNumber,
416 uint64_t SizeInBits, uint64_t AlignInBits,
417 uint64_t OffsetInBits, unsigned Flags,
420 /// CreateDerivedType - Create a derived type like const qualified type,
421 /// pointer, typedef, etc.
422 DIDerivedType CreateDerivedType(unsigned Tag, DIDescriptor Context,
423 const std::string &Name,
424 DICompileUnit CompileUnit,
426 uint64_t SizeInBits, uint64_t AlignInBits,
427 uint64_t OffsetInBits, unsigned Flags,
430 /// CreateCompositeType - Create a composite type like array, struct, etc.
431 DICompositeType CreateCompositeType(unsigned Tag, DIDescriptor Context,
432 const std::string &Name,
433 DICompileUnit CompileUnit,
436 uint64_t AlignInBits,
437 uint64_t OffsetInBits, unsigned Flags,
440 unsigned RunTimeLang = 0);
442 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
443 /// See comments in DISubprogram for descriptions of these fields.
444 DISubprogram CreateSubprogram(DIDescriptor Context, const std::string &Name,
445 const std::string &DisplayName,
446 const std::string &LinkageName,
447 DICompileUnit CompileUnit, unsigned LineNo,
448 DIType Type, bool isLocalToUnit,
451 /// CreateGlobalVariable - Create a new descriptor for the specified global.
453 CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
454 const std::string &DisplayName,
455 const std::string &LinkageName,
456 DICompileUnit CompileUnit,
457 unsigned LineNo, DIType Type, bool isLocalToUnit,
458 bool isDefinition, llvm::GlobalVariable *GV);
460 /// CreateVariable - Create a new descriptor for the specified variable.
461 DIVariable CreateVariable(unsigned Tag, DIDescriptor Context,
462 const std::string &Name,
463 DICompileUnit CompileUnit, unsigned LineNo,
466 /// CreateBlock - This creates a descriptor for a lexical block with the
467 /// specified parent context.
468 DIBlock CreateBlock(DIDescriptor Context);
470 /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
471 /// inserting it at the end of the specified basic block.
472 void InsertStopPoint(DICompileUnit CU, unsigned LineNo, unsigned ColNo,
475 /// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
476 /// mark the start of the specified subprogram.
477 void InsertSubprogramStart(DISubprogram SP, BasicBlock *BB);
479 /// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
480 /// mark the start of a region for the specified scoping descriptor.
481 void InsertRegionStart(DIDescriptor D, BasicBlock *BB);
483 /// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
484 /// mark the end of a region for the specified scoping descriptor.
485 void InsertRegionEnd(DIDescriptor D, BasicBlock *BB);
487 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
488 void InsertDeclare(llvm::Value *Storage, DIVariable D, BasicBlock *BB);
491 Constant *GetTagConstant(unsigned TAG);
492 Constant *GetStringConstant(const std::string &String);
493 DIAnchor GetOrCreateAnchor(unsigned TAG, const char *Name);
495 /// getCastToEmpty - Return the descriptor as a Constant* with type '{}*'.
496 Constant *getCastToEmpty(DIDescriptor D);
499 /// Finds the stoppoint coressponding to this instruction, that is the
500 /// stoppoint that dominates this instruction
501 const DbgStopPointInst *findStopPoint(const Instruction *Inst);
503 /// Finds the stoppoint corresponding to first real (non-debug intrinsic)
504 /// instruction in this Basic Block, and returns the stoppoint for it.
505 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB);
507 /// Finds the dbg.declare intrinsic corresponding to this value if any.
508 /// It looks through pointer casts too.
509 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts = true);
511 /// Find the debug info descriptor corresponding to this global variable.
512 Value *findDbgGlobalDeclare(GlobalVariable *V);
514 bool getLocationInfo(const Value *V, std::string &DisplayName, std::string &Type,
515 unsigned &LineNo, std::string &File, std::string &Dir);
516 } // end namespace llvm