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/Target/TargetMachine.h"
19 #include "llvm/ADT/StringMap.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/Support/Dwarf.h"
31 struct DbgStopPointInst;
32 struct DbgDeclareInst;
39 /// DIDescriptor constructor. If the specified GV is non-null, this checks
40 /// to make sure that the tag in the descriptor matches 'RequiredTag'. If
41 /// not, the debug info is corrupt and we ignore it.
42 DIDescriptor(GlobalVariable *GV, unsigned RequiredTag);
44 const std::string &getStringField(unsigned Elt, std::string &Result) const;
45 unsigned getUnsignedField(unsigned Elt) const {
46 return (unsigned)getUInt64Field(Elt);
48 uint64_t getUInt64Field(unsigned Elt) const;
49 DIDescriptor getDescriptorField(unsigned Elt) const;
51 template <typename DescTy>
52 DescTy getFieldAs(unsigned Elt) const {
53 return DescTy(getDescriptorField(Elt).getGV());
56 GlobalVariable *getGlobalVariableField(unsigned Elt) const;
59 explicit DIDescriptor() : GV(0) {}
60 explicit DIDescriptor(GlobalVariable *gv) : GV(gv) {}
62 bool isNull() const { return GV == 0; }
64 GlobalVariable *getGV() const { return GV; }
66 unsigned getVersion() const {
67 return getUnsignedField(0) & LLVMDebugVersionMask;
70 unsigned getTag() const {
71 return getUnsignedField(0) & ~LLVMDebugVersionMask;
74 /// ValidDebugInfo - Return true if V represents valid debug info value.
75 static bool ValidDebugInfo(Value *V, CodeGenOpt::Level OptLevel);
77 /// dump - print descriptor.
81 /// DIAnchor - A wrapper for various anchor descriptors.
82 class DIAnchor : public DIDescriptor {
84 explicit DIAnchor(GlobalVariable *GV = 0);
86 unsigned getAnchorTag() const { return getUnsignedField(1); }
89 /// DISubrange - This is used to represent ranges, for array bounds.
90 class DISubrange : public DIDescriptor {
92 explicit DISubrange(GlobalVariable *GV = 0);
94 int64_t getLo() const { return (int64_t)getUInt64Field(1); }
95 int64_t getHi() const { return (int64_t)getUInt64Field(2); }
98 /// DIArray - This descriptor holds an array of descriptors.
99 class DIArray : public DIDescriptor {
101 explicit DIArray(GlobalVariable *GV = 0) : DIDescriptor(GV) {}
103 unsigned getNumElements() const;
104 DIDescriptor getElement(unsigned Idx) const {
105 return getDescriptorField(Idx);
109 /// DICompileUnit - A wrapper for a compile unit.
110 class DICompileUnit : public DIDescriptor {
112 explicit DICompileUnit(GlobalVariable *GV = 0);
114 unsigned getLanguage() const { return getUnsignedField(2); }
115 const std::string &getFilename(std::string &F) const {
116 return getStringField(3, F);
118 const std::string &getDirectory(std::string &F) const {
119 return getStringField(4, F);
121 const std::string &getProducer(std::string &F) const {
122 return getStringField(5, F);
125 /// isMain - Each input file is encoded as a separate compile unit in LLVM
126 /// debugging information output. However, many target specific tool chains
127 /// prefer to encode only one compile unit in an object file. In this
128 /// situation, the LLVM code generator will include debugging information
129 /// entities in the compile unit that is marked as main compile unit. The
130 /// code generator accepts maximum one main compile unit per module. If a
131 /// module does not contain any main compile unit then the code generator
132 /// will emit multiple compile units in the output object file.
134 bool isMain() const { return getUnsignedField(6); }
135 bool isOptimized() const { return getUnsignedField(7); }
136 const std::string &getFlags(std::string &F) const {
137 return getStringField(8, F);
139 unsigned getRunTimeVersion() const { return getUnsignedField(9); }
141 /// Verify - Verify that a compile unit is well formed.
144 /// dump - print compile unit.
148 /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
149 /// FIXME: it seems strange that this doesn't have either a reference to the
150 /// type/precision or a file/line pair for location info.
151 class DIEnumerator : public DIDescriptor {
153 explicit DIEnumerator(GlobalVariable *GV = 0);
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 unsigned getEncoding() const { return getUnsignedField(9); }
226 /// dump - print basic type.
230 /// DIDerivedType - A simple derived type, like a const qualified type,
231 /// a typedef, a pointer or reference, etc.
232 class DIDerivedType : public DIType {
234 explicit DIDerivedType(GlobalVariable *GV, bool, bool)
235 : DIType(GV, true, true) {}
237 explicit DIDerivedType(GlobalVariable *GV);
238 DIType getTypeDerivedFrom() const { return getFieldAs<DIType>(9); }
240 /// getOriginalTypeSize - If this type is derived from a base type then
241 /// return base type size.
242 uint64_t getOriginalTypeSize() const;
243 /// dump - print derived type.
247 /// DICompositeType - This descriptor holds a type that can refer to multiple
248 /// other types, like a function or struct.
249 /// FIXME: Why is this a DIDerivedType??
250 class DICompositeType : public DIDerivedType {
252 explicit DICompositeType(GlobalVariable *GV);
253 DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
254 unsigned getRunTimeLang() const { return getUnsignedField(11); }
256 /// Verify - Verify that a composite type descriptor is well formed.
259 /// dump - print composite type.
263 /// DIGlobal - This is a common class for global variables and subprograms.
264 class DIGlobal : public DIDescriptor {
266 explicit DIGlobal(GlobalVariable *GV, unsigned RequiredTag)
267 : DIDescriptor(GV, RequiredTag) {}
269 /// isSubprogram - Return true if the specified tag is legal for
271 static bool isSubprogram(unsigned TAG) {
272 return TAG == dwarf::DW_TAG_subprogram;
275 /// isGlobalVariable - Return true if the specified tag is legal for
276 /// DIGlobalVariable.
277 static bool isGlobalVariable(unsigned TAG) {
278 return TAG == dwarf::DW_TAG_variable;
282 virtual ~DIGlobal() {}
284 DIDescriptor getContext() const { return getDescriptorField(2); }
285 const std::string &getName(std::string &F) const {
286 return getStringField(3, F);
288 const std::string &getDisplayName(std::string &F) const {
289 return getStringField(4, F);
291 const std::string &getLinkageName(std::string &F) const {
292 return getStringField(5, F);
294 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(6); }
295 unsigned getLineNumber() const { return getUnsignedField(7); }
296 DIType getType() const { return getFieldAs<DIType>(8); }
298 /// isLocalToUnit - Return true if this subprogram is local to the current
299 /// compile unit, like 'static' in C.
300 unsigned isLocalToUnit() const { return getUnsignedField(9); }
301 unsigned isDefinition() const { return getUnsignedField(10); }
303 /// dump - print global.
307 /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
308 class DISubprogram : public DIGlobal {
310 explicit DISubprogram(GlobalVariable *GV = 0);
311 DICompositeType getType() const { return getFieldAs<DICompositeType>(8); }
313 /// Verify - Verify that a subprogram descriptor is well formed.
316 /// dump - print subprogram.
319 /// describes - Return true if this subprogram provides debugging
320 /// information for the function F.
321 bool describes(const Function *F);
324 /// DIGlobalVariable - This is a wrapper for a global variable.
325 class DIGlobalVariable : public DIGlobal {
327 explicit DIGlobalVariable(GlobalVariable *GV = 0);
328 GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
330 /// Verify - Verify that a global variable descriptor is well formed.
333 /// dump - print global variable.
337 /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
339 class DIVariable : public DIDescriptor {
341 explicit DIVariable(GlobalVariable *GV = 0);
343 DIDescriptor getContext() const { return getDescriptorField(1); }
344 const std::string &getName(std::string &F) const {
345 return getStringField(2, F);
347 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
348 unsigned getLineNumber() const { return getUnsignedField(4); }
349 DIType getType() const { return getFieldAs<DIType>(5); }
351 /// isVariable - Return true if the specified tag is legal for DIVariable.
352 static bool isVariable(unsigned Tag);
354 /// Verify - Verify that a variable descriptor is well formed.
357 /// dump - print variable.
361 /// DIBlock - This is a wrapper for a block (e.g. a function, scope, etc).
362 class DIBlock : public DIDescriptor {
364 explicit DIBlock(GlobalVariable *GV = 0);
366 DIDescriptor getContext() const { return getDescriptorField(1); }
369 /// DIFactory - This object assists with the construction of the various
373 // Cached values for uniquing and faster lookups.
374 DIAnchor CompileUnitAnchor, SubProgramAnchor, GlobalVariableAnchor;
375 const Type *EmptyStructPtr; // "{}*".
376 Function *StopPointFn; // llvm.dbg.stoppoint
377 Function *FuncStartFn; // llvm.dbg.func.start
378 Function *RegionStartFn; // llvm.dbg.region.start
379 Function *RegionEndFn; // llvm.dbg.region.end
380 Function *DeclareFn; // llvm.dbg.declare
381 StringMap<Constant*> StringCache;
382 DenseMap<Constant*, DIDescriptor> SimpleConstantCache;
384 DIFactory(const DIFactory &); // DO NOT IMPLEMENT
385 void operator=(const DIFactory&); // DO NOT IMPLEMENT
387 explicit DIFactory(Module &m);
389 /// GetOrCreateCompileUnitAnchor - Return the anchor for compile units,
390 /// creating a new one if there isn't already one in the module.
391 DIAnchor GetOrCreateCompileUnitAnchor();
393 /// GetOrCreateSubprogramAnchor - Return the anchor for subprograms,
394 /// creating a new one if there isn't already one in the module.
395 DIAnchor GetOrCreateSubprogramAnchor();
397 /// GetOrCreateGlobalVariableAnchor - Return the anchor for globals,
398 /// creating a new one if there isn't already one in the module.
399 DIAnchor GetOrCreateGlobalVariableAnchor();
401 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
402 /// This implicitly uniques the arrays created.
403 DIArray GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys);
405 /// GetOrCreateSubrange - Create a descriptor for a value range. This
406 /// implicitly uniques the values returned.
407 DISubrange GetOrCreateSubrange(int64_t Lo, int64_t Hi);
409 /// CreateCompileUnit - Create a new descriptor for the specified compile
411 DICompileUnit CreateCompileUnit(unsigned LangID,
412 const std::string &Filename,
413 const std::string &Directory,
414 const std::string &Producer,
416 bool isOptimized = false,
417 const char *Flags = "",
418 unsigned RunTimeVer = 0);
420 /// CreateEnumerator - Create a single enumerator value.
421 DIEnumerator CreateEnumerator(const std::string &Name, uint64_t Val);
423 /// CreateBasicType - Create a basic type like int, float, etc.
424 DIBasicType CreateBasicType(DIDescriptor Context, const std::string &Name,
425 DICompileUnit CompileUnit, unsigned LineNumber,
426 uint64_t SizeInBits, uint64_t AlignInBits,
427 uint64_t OffsetInBits, unsigned Flags,
430 /// CreateDerivedType - Create a derived type like const qualified type,
431 /// pointer, typedef, etc.
432 DIDerivedType CreateDerivedType(unsigned Tag, DIDescriptor Context,
433 const std::string &Name,
434 DICompileUnit CompileUnit,
436 uint64_t SizeInBits, uint64_t AlignInBits,
437 uint64_t OffsetInBits, unsigned Flags,
440 /// CreateCompositeType - Create a composite type like array, struct, etc.
441 DICompositeType CreateCompositeType(unsigned Tag, DIDescriptor Context,
442 const std::string &Name,
443 DICompileUnit CompileUnit,
446 uint64_t AlignInBits,
447 uint64_t OffsetInBits, unsigned Flags,
450 unsigned RunTimeLang = 0);
452 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
453 /// See comments in DISubprogram for descriptions of these fields.
454 DISubprogram CreateSubprogram(DIDescriptor Context, const std::string &Name,
455 const std::string &DisplayName,
456 const std::string &LinkageName,
457 DICompileUnit CompileUnit, unsigned LineNo,
458 DIType Type, bool isLocalToUnit,
461 /// CreateGlobalVariable - Create a new descriptor for the specified global.
463 CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
464 const std::string &DisplayName,
465 const std::string &LinkageName,
466 DICompileUnit CompileUnit,
467 unsigned LineNo, DIType Type, bool isLocalToUnit,
468 bool isDefinition, llvm::GlobalVariable *GV);
470 /// CreateVariable - Create a new descriptor for the specified variable.
471 DIVariable CreateVariable(unsigned Tag, DIDescriptor Context,
472 const std::string &Name,
473 DICompileUnit CompileUnit, unsigned LineNo,
476 /// CreateBlock - This creates a descriptor for a lexical block with the
477 /// specified parent context.
478 DIBlock CreateBlock(DIDescriptor Context);
480 /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
481 /// inserting it at the end of the specified basic block.
482 void InsertStopPoint(DICompileUnit CU, unsigned LineNo, unsigned ColNo,
485 /// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
486 /// mark the start of the specified subprogram.
487 void InsertSubprogramStart(DISubprogram SP, BasicBlock *BB);
489 /// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
490 /// mark the start of a region for the specified scoping descriptor.
491 void InsertRegionStart(DIDescriptor D, BasicBlock *BB);
493 /// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
494 /// mark the end of a region for the specified scoping descriptor.
495 void InsertRegionEnd(DIDescriptor D, BasicBlock *BB);
497 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
498 void InsertDeclare(llvm::Value *Storage, DIVariable D, BasicBlock *BB);
501 Constant *GetTagConstant(unsigned TAG);
502 Constant *GetStringConstant(const std::string &String);
503 DIAnchor GetOrCreateAnchor(unsigned TAG, const char *Name);
505 /// getCastToEmpty - Return the descriptor as a Constant* with type '{}*'.
506 Constant *getCastToEmpty(DIDescriptor D);
509 /// Finds the stoppoint coressponding to this instruction, that is the
510 /// stoppoint that dominates this instruction
511 const DbgStopPointInst *findStopPoint(const Instruction *Inst);
513 /// Finds the stoppoint corresponding to first real (non-debug intrinsic)
514 /// instruction in this Basic Block, and returns the stoppoint for it.
515 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB);
517 /// Finds the dbg.declare intrinsic corresponding to this value if any.
518 /// It looks through pointer casts too.
519 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts = true);
521 /// Find the debug info descriptor corresponding to this global variable.
522 Value *findDbgGlobalDeclare(GlobalVariable *V);
524 bool getLocationInfo(const Value *V, std::string &DisplayName, std::string &Type,
525 unsigned &LineNo, std::string &File, std::string &Dir);
526 } // end namespace llvm