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/Module.h"
21 #include "llvm/GlobalVariable.h"
22 #include "llvm/Target/TargetMachine.h"
23 #include "llvm/ADT/StringMap.h"
24 #include "llvm/ADT/DenseMap.h"
25 #include "llvm/ADT/SmallVector.h"
26 #include "llvm/Support/Dwarf.h"
36 struct DbgStopPointInst;
37 struct DbgDeclareInst;
42 GlobalVariable *DbgGV;
44 /// DIDescriptor constructor. If the specified GV is non-null, this checks
45 /// to make sure that the tag in the descriptor matches 'RequiredTag'. If
46 /// not, the debug info is corrupt and we ignore it.
47 DIDescriptor(GlobalVariable *GV, unsigned RequiredTag);
49 const std::string &getStringField(unsigned Elt, std::string &Result) const;
50 unsigned getUnsignedField(unsigned Elt) const {
51 return (unsigned)getUInt64Field(Elt);
53 uint64_t getUInt64Field(unsigned Elt) const;
54 DIDescriptor getDescriptorField(unsigned Elt) const;
56 template <typename DescTy>
57 DescTy getFieldAs(unsigned Elt) const {
58 return DescTy(getDescriptorField(Elt).getGV());
61 GlobalVariable *getGlobalVariableField(unsigned Elt) const;
64 explicit DIDescriptor() : DbgGV(0) {}
65 explicit DIDescriptor(GlobalVariable *GV) : DbgGV(GV) {}
67 bool isNull() const { return DbgGV == 0; }
69 GlobalVariable *getGV() const { return DbgGV; }
71 unsigned getVersion() const {
72 return getUnsignedField(0) & LLVMDebugVersionMask;
75 unsigned getTag() const {
76 return getUnsignedField(0) & ~LLVMDebugVersionMask;
79 /// ValidDebugInfo - Return true if V represents valid debug info value.
80 static bool ValidDebugInfo(Value *V, CodeGenOpt::Level OptLevel);
82 /// dump - print descriptor.
86 /// DISubrange - This is used to represent ranges, for array bounds.
87 class DISubrange : public DIDescriptor {
89 explicit DISubrange(GlobalVariable *GV = 0)
90 : DIDescriptor(GV, dwarf::DW_TAG_subrange_type) {}
92 int64_t getLo() const { return (int64_t)getUInt64Field(1); }
93 int64_t getHi() const { return (int64_t)getUInt64Field(2); }
96 /// DIArray - This descriptor holds an array of descriptors.
97 class DIArray : public DIDescriptor {
99 explicit DIArray(GlobalVariable *GV = 0) : DIDescriptor(GV) {}
101 unsigned getNumElements() const;
102 DIDescriptor getElement(unsigned Idx) const {
103 return getDescriptorField(Idx);
107 /// DICompileUnit - A wrapper for a compile unit.
108 class DICompileUnit : public DIDescriptor {
110 explicit DICompileUnit(GlobalVariable *GV = 0)
111 : DIDescriptor(GV, dwarf::DW_TAG_compile_unit) {}
113 unsigned getLanguage() const { return getUnsignedField(2); }
114 const std::string &getFilename(std::string &F) const {
115 return getStringField(3, F);
117 const std::string &getDirectory(std::string &F) const {
118 return getStringField(4, F);
120 const std::string &getProducer(std::string &F) const {
121 return getStringField(5, F);
124 /// isMain - Each input file is encoded as a separate compile unit in LLVM
125 /// debugging information output. However, many target specific tool chains
126 /// prefer to encode only one compile unit in an object file. In this
127 /// situation, the LLVM code generator will include debugging information
128 /// entities in the compile unit that is marked as main compile unit. The
129 /// code generator accepts maximum one main compile unit per module. If a
130 /// module does not contain any main compile unit then the code generator
131 /// will emit multiple compile units in the output object file.
133 bool isMain() const { return getUnsignedField(6); }
134 bool isOptimized() const { return getUnsignedField(7); }
135 const std::string &getFlags(std::string &F) const {
136 return getStringField(8, F);
138 unsigned getRunTimeVersion() const { return getUnsignedField(9); }
140 /// Verify - Verify that a compile unit is well formed.
143 /// dump - print compile unit.
147 /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
148 /// FIXME: it seems strange that this doesn't have either a reference to the
149 /// type/precision or a file/line pair for location info.
150 class DIEnumerator : public DIDescriptor {
152 explicit DIEnumerator(GlobalVariable *GV = 0)
153 : DIDescriptor(GV, dwarf::DW_TAG_enumerator) {}
155 const std::string &getName(std::string &F) const {
156 return getStringField(1, F);
158 uint64_t getEnumValue() const { return getUInt64Field(2); }
161 /// DIType - This is a wrapper for a type.
162 /// FIXME: Types should be factored much better so that CV qualifiers and
163 /// others do not require a huge and empty descriptor full of zeros.
164 class DIType : public DIDescriptor {
167 FlagPrivate = 1 << 0,
168 FlagProtected = 1 << 1,
173 DIType(GlobalVariable *GV, unsigned Tag) : DIDescriptor(GV, Tag) {}
174 // This ctor is used when the Tag has already been validated by a derived
176 DIType(GlobalVariable *GV, bool, bool) : DIDescriptor(GV) {}
179 /// isDerivedType - Return true if the specified tag is legal for
181 static bool isDerivedType(unsigned TAG);
183 /// isCompositeType - Return true if the specified tag is legal for
185 static bool isCompositeType(unsigned TAG);
187 /// isBasicType - Return true if the specified tag is legal for
189 static bool isBasicType(unsigned TAG) {
190 return TAG == dwarf::DW_TAG_base_type;
193 /// Verify - Verify that a type descriptor is well formed.
196 explicit DIType(GlobalVariable *GV);
200 DIDescriptor getContext() const { return getDescriptorField(1); }
201 const std::string &getName(std::string &F) const {
202 return getStringField(2, F);
204 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
205 unsigned getLineNumber() const { return getUnsignedField(4); }
206 uint64_t getSizeInBits() const { return getUInt64Field(5); }
207 uint64_t getAlignInBits() const { return getUInt64Field(6); }
208 // FIXME: Offset is only used for DW_TAG_member nodes. Making every type
209 // carry this is just plain insane.
210 uint64_t getOffsetInBits() const { return getUInt64Field(7); }
211 unsigned getFlags() const { return getUnsignedField(8); }
212 bool isPrivate() const { return (getFlags() & FlagPrivate) != 0; }
213 bool isProtected() const { return (getFlags() & FlagProtected) != 0; }
214 bool isForwardDecl() const { return (getFlags() & FlagFwdDecl) != 0; }
216 /// dump - print type.
220 /// DIBasicType - A basic type, like 'int' or 'float'.
221 class DIBasicType : public DIType {
223 explicit DIBasicType(GlobalVariable *GV)
224 : DIType(GV, dwarf::DW_TAG_base_type) {}
226 unsigned getEncoding() const { return getUnsignedField(9); }
228 /// dump - print basic type.
232 /// DIDerivedType - A simple derived type, like a const qualified type,
233 /// a typedef, a pointer or reference, etc.
234 class DIDerivedType : public DIType {
236 explicit DIDerivedType(GlobalVariable *GV, bool, bool)
237 : DIType(GV, true, true) {}
239 explicit DIDerivedType(GlobalVariable *GV)
240 : DIType(GV, true, true) {
241 if (GV && !isDerivedType(getTag()))
245 DIType getTypeDerivedFrom() const { return getFieldAs<DIType>(9); }
247 /// getOriginalTypeSize - If this type is derived from a base type then
248 /// return base type size.
249 uint64_t getOriginalTypeSize() const;
250 /// dump - print derived type.
254 /// DICompositeType - This descriptor holds a type that can refer to multiple
255 /// other types, like a function or struct.
256 /// FIXME: Why is this a DIDerivedType??
257 class DICompositeType : public DIDerivedType {
259 explicit DICompositeType(GlobalVariable *GV)
260 : DIDerivedType(GV, true, true) {
261 if (GV && !isCompositeType(getTag()))
265 DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
266 unsigned getRunTimeLang() const { return getUnsignedField(11); }
268 /// Verify - Verify that a composite type descriptor is well formed.
271 /// dump - print composite type.
275 /// DIGlobal - This is a common class for global variables and subprograms.
276 class DIGlobal : public DIDescriptor {
278 explicit DIGlobal(GlobalVariable *GV, unsigned RequiredTag)
279 : DIDescriptor(GV, RequiredTag) {}
281 /// isSubprogram - Return true if the specified tag is legal for
283 static bool isSubprogram(unsigned TAG) {
284 return TAG == dwarf::DW_TAG_subprogram;
287 /// isGlobalVariable - Return true if the specified tag is legal for
288 /// DIGlobalVariable.
289 static bool isGlobalVariable(unsigned TAG) {
290 return TAG == dwarf::DW_TAG_variable;
294 virtual ~DIGlobal() {}
296 DIDescriptor getContext() const { return getDescriptorField(2); }
297 const std::string &getName(std::string &F) const {
298 return getStringField(3, F);
300 const std::string &getDisplayName(std::string &F) const {
301 return getStringField(4, F);
303 const std::string &getLinkageName(std::string &F) const {
304 return getStringField(5, F);
306 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(6); }
307 unsigned getLineNumber() const { return getUnsignedField(7); }
308 DIType getType() const { return getFieldAs<DIType>(8); }
310 /// isLocalToUnit - Return true if this subprogram is local to the current
311 /// compile unit, like 'static' in C.
312 unsigned isLocalToUnit() const { return getUnsignedField(9); }
313 unsigned isDefinition() const { return getUnsignedField(10); }
315 /// dump - print global.
319 /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
320 class DISubprogram : public DIGlobal {
322 explicit DISubprogram(GlobalVariable *GV = 0)
323 : DIGlobal(GV, dwarf::DW_TAG_subprogram) {}
325 DICompositeType getType() const { return getFieldAs<DICompositeType>(8); }
327 /// getReturnTypeName - Subprogram return types are encoded either as
328 /// DIType or as DICompositeType.
329 const std::string &getReturnTypeName(std::string &F) const {
330 DICompositeType DCT(getFieldAs<DICompositeType>(8));
332 DIArray A = DCT.getTypeArray();
333 DIType T(A.getElement(0).getGV());
336 DIType T(getFieldAs<DIType>(8));
340 /// Verify - Verify that a subprogram descriptor is well formed.
343 /// dump - print subprogram.
346 /// describes - Return true if this subprogram provides debugging
347 /// information for the function F.
348 bool describes(const Function *F);
351 /// DIGlobalVariable - This is a wrapper for a global variable.
352 class DIGlobalVariable : public DIGlobal {
354 explicit DIGlobalVariable(GlobalVariable *GV = 0)
355 : DIGlobal(GV, dwarf::DW_TAG_variable) {}
357 GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
359 /// Verify - Verify that a global variable descriptor is well formed.
362 /// dump - print global variable.
366 /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
368 class DIVariable : public DIDescriptor {
370 explicit DIVariable(GlobalVariable *GV = 0)
372 if (GV && !isVariable(getTag()))
376 DIDescriptor getContext() const { return getDescriptorField(1); }
377 const std::string &getName(std::string &F) const {
378 return getStringField(2, F);
380 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
381 unsigned getLineNumber() const { return getUnsignedField(4); }
382 DIType getType() const { return getFieldAs<DIType>(5); }
384 /// isVariable - Return true if the specified tag is legal for DIVariable.
385 static bool isVariable(unsigned Tag);
387 /// Verify - Verify that a variable descriptor is well formed.
390 /// dump - print variable.
394 /// DIBlock - This is a wrapper for a block (e.g. a function, scope, etc).
395 class DIBlock : public DIDescriptor {
397 explicit DIBlock(GlobalVariable *GV = 0)
398 : DIDescriptor(GV, dwarf::DW_TAG_lexical_block) {}
400 DIDescriptor getContext() const { return getDescriptorField(1); }
403 /// DIFactory - This object assists with the construction of the various
407 // Cached values for uniquing and faster lookups.
408 const Type *EmptyStructPtr; // "{}*".
409 Function *StopPointFn; // llvm.dbg.stoppoint
410 Function *FuncStartFn; // llvm.dbg.func.start
411 Function *RegionStartFn; // llvm.dbg.region.start
412 Function *RegionEndFn; // llvm.dbg.region.end
413 Function *DeclareFn; // llvm.dbg.declare
414 StringMap<Constant*> StringCache;
415 DenseMap<Constant*, DIDescriptor> SimpleConstantCache;
417 DIFactory(const DIFactory &); // DO NOT IMPLEMENT
418 void operator=(const DIFactory&); // DO NOT IMPLEMENT
420 explicit DIFactory(Module &m);
422 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
423 /// This implicitly uniques the arrays created.
424 DIArray GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys);
426 /// GetOrCreateSubrange - Create a descriptor for a value range. This
427 /// implicitly uniques the values returned.
428 DISubrange GetOrCreateSubrange(int64_t Lo, int64_t Hi);
430 /// CreateCompileUnit - Create a new descriptor for the specified compile
432 DICompileUnit CreateCompileUnit(unsigned LangID,
433 const std::string &Filename,
434 const std::string &Directory,
435 const std::string &Producer,
437 bool isOptimized = false,
438 const char *Flags = "",
439 unsigned RunTimeVer = 0);
441 /// CreateEnumerator - Create a single enumerator value.
442 DIEnumerator CreateEnumerator(const std::string &Name, uint64_t Val);
444 /// CreateBasicType - Create a basic type like int, float, etc.
445 DIBasicType CreateBasicType(DIDescriptor Context, const std::string &Name,
446 DICompileUnit CompileUnit, unsigned LineNumber,
447 uint64_t SizeInBits, uint64_t AlignInBits,
448 uint64_t OffsetInBits, unsigned Flags,
451 /// CreateDerivedType - Create a derived type like const qualified type,
452 /// pointer, typedef, etc.
453 DIDerivedType CreateDerivedType(unsigned Tag, DIDescriptor Context,
454 const std::string &Name,
455 DICompileUnit CompileUnit,
457 uint64_t SizeInBits, uint64_t AlignInBits,
458 uint64_t OffsetInBits, unsigned Flags,
461 /// CreateCompositeType - Create a composite type like array, struct, etc.
462 DICompositeType CreateCompositeType(unsigned Tag, DIDescriptor Context,
463 const std::string &Name,
464 DICompileUnit CompileUnit,
467 uint64_t AlignInBits,
468 uint64_t OffsetInBits, unsigned Flags,
471 unsigned RunTimeLang = 0);
473 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
474 /// See comments in DISubprogram for descriptions of these fields.
475 DISubprogram CreateSubprogram(DIDescriptor Context, const std::string &Name,
476 const std::string &DisplayName,
477 const std::string &LinkageName,
478 DICompileUnit CompileUnit, unsigned LineNo,
479 DIType Type, bool isLocalToUnit,
482 /// CreateGlobalVariable - Create a new descriptor for the specified global.
484 CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
485 const std::string &DisplayName,
486 const std::string &LinkageName,
487 DICompileUnit CompileUnit,
488 unsigned LineNo, DIType Type, bool isLocalToUnit,
489 bool isDefinition, llvm::GlobalVariable *GV);
491 /// CreateVariable - Create a new descriptor for the specified variable.
492 DIVariable CreateVariable(unsigned Tag, DIDescriptor Context,
493 const std::string &Name,
494 DICompileUnit CompileUnit, unsigned LineNo,
497 /// CreateBlock - This creates a descriptor for a lexical block with the
498 /// specified parent context.
499 DIBlock CreateBlock(DIDescriptor Context);
501 /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
502 /// inserting it at the end of the specified basic block.
503 void InsertStopPoint(DICompileUnit CU, unsigned LineNo, unsigned ColNo,
506 /// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
507 /// mark the start of the specified subprogram.
508 void InsertSubprogramStart(DISubprogram SP, BasicBlock *BB);
510 /// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
511 /// mark the start of a region for the specified scoping descriptor.
512 void InsertRegionStart(DIDescriptor D, BasicBlock *BB);
514 /// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
515 /// mark the end of a region for the specified scoping descriptor.
516 void InsertRegionEnd(DIDescriptor D, BasicBlock *BB);
518 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
519 void InsertDeclare(llvm::Value *Storage, DIVariable D, BasicBlock *BB);
522 Constant *GetTagConstant(unsigned TAG);
523 Constant *GetStringConstant(const std::string &String);
525 /// getCastToEmpty - Return the descriptor as a Constant* with type '{}*'.
526 Constant *getCastToEmpty(DIDescriptor D);
529 /// Finds the stoppoint coressponding to this instruction, that is the
530 /// stoppoint that dominates this instruction
531 const DbgStopPointInst *findStopPoint(const Instruction *Inst);
533 /// Finds the stoppoint corresponding to first real (non-debug intrinsic)
534 /// instruction in this Basic Block, and returns the stoppoint for it.
535 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB);
537 /// Finds the dbg.declare intrinsic corresponding to this value if any.
538 /// It looks through pointer casts too.
539 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts = true);
541 /// Find the debug info descriptor corresponding to this global variable.
542 Value *findDbgGlobalDeclare(GlobalVariable *V);
544 bool getLocationInfo(const Value *V, std::string &DisplayName, std::string &Type,
545 unsigned &LineNo, std::string &File, std::string &Dir);
547 /// CollectDebugInfoAnchors - Collect debugging information anchors.
548 void CollectDebugInfoAnchors(Module &M,
549 SmallVector<GlobalVariable *, 2> &CompileUnits,
550 SmallVector<GlobalVariable *, 4> &GlobalVars,
551 SmallVector<GlobalVariable *, 4> &Subprograms);
553 } // end namespace llvm