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_SUPPORT_DEBUGINFO_H
16 #define LLVM_SUPPORT_DEBUGINFO_H
18 #include "llvm/ADT/StringMap.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/Support/Dwarf.h"
30 class DbgStopPointInst;
37 Version7 = 7 << 16, // Current version of debug information.
38 Version6 = 6 << 16, // Constant for version 6.
39 Version5 = 5 << 16, // Constant for version 5.
40 Version4 = 4 << 16, // Constant for version 4.
41 VersionMask = 0xffff0000 // Mask for version number.
47 /// DIDescriptor constructor. If the specified GV is non-null, this checks
48 /// to make sure that the tag in the descriptor matches 'RequiredTag'. If
49 /// not, the debug info is corrupt and we ignore it.
50 DIDescriptor(GlobalVariable *GV, unsigned RequiredTag);
52 std::string getStringField(unsigned Elt) const;
53 unsigned getUnsignedField(unsigned Elt) const {
54 return (unsigned)getUInt64Field(Elt);
56 uint64_t getUInt64Field(unsigned Elt) const;
57 DIDescriptor getDescriptorField(unsigned Elt) const;
59 template <typename DescTy>
60 DescTy getFieldAs(unsigned Elt) const {
61 return DescTy(getDescriptorField(Elt).getGV());
64 GlobalVariable *getGlobalVariableField(unsigned Elt) const;
67 explicit DIDescriptor() : GV(0) {}
68 explicit DIDescriptor(GlobalVariable *gv) : GV(gv) {}
70 bool isNull() const { return GV == 0; }
72 GlobalVariable *getGV() const { return GV; }
74 unsigned getVersion() const {
75 return getUnsignedField(0) & VersionMask;
78 unsigned getTag() const {
79 return getUnsignedField(0) & ~VersionMask;
84 /// DIAnchor - A wrapper for various anchor descriptors.
85 class DIAnchor : public DIDescriptor {
87 explicit DIAnchor(GlobalVariable *GV = 0);
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);
97 int64_t getLo() const { return (int64_t)getUInt64Field(1); }
98 int64_t getHi() const { return (int64_t)getUInt64Field(2); }
101 /// DIArray - This descriptor holds an array of descriptors.
102 class DIArray : public DIDescriptor {
104 explicit DIArray(GlobalVariable *GV = 0) : DIDescriptor(GV) {}
106 unsigned getNumElements() const;
107 DIDescriptor getElement(unsigned Idx) const {
108 return getDescriptorField(Idx);
112 /// DICompileUnit - A wrapper for a compile unit.
113 class DICompileUnit : public DIDescriptor {
115 explicit DICompileUnit(GlobalVariable *GV = 0);
117 unsigned getLanguage() const { return getUnsignedField(2); }
118 std::string getFilename() const { return getStringField(3); }
119 std::string getDirectory() const { return getStringField(4); }
120 std::string getProducer() const { return getStringField(5); }
122 /// Verify - Verify that a compile unit is well formed.
126 /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
127 /// FIXME: it seems strange that this doesn't have either a reference to the
128 /// type/precision or a file/line pair for location info.
129 class DIEnumerator : public DIDescriptor {
131 explicit DIEnumerator(GlobalVariable *GV = 0);
133 std::string getName() const { return getStringField(1); }
134 uint64_t getEnumValue() const { return getUInt64Field(2); }
137 /// DIType - This is a wrapper for a type.
138 /// FIXME: Types should be factored much better so that CV qualifiers and
139 /// others do not require a huge and empty descriptor full of zeros.
140 class DIType : public DIDescriptor {
142 DIType(GlobalVariable *GV, unsigned Tag) : DIDescriptor(GV, Tag) {}
143 // This ctor is used when the Tag has already been validated by a derived
145 DIType(GlobalVariable *GV, bool, bool) : DIDescriptor(GV) {}
148 /// isDerivedType - Return true if the specified tag is legal for
150 static bool isDerivedType(unsigned TAG);
152 /// isCompositeType - Return true if the specified tag is legal for
154 static bool isCompositeType(unsigned TAG);
156 /// isBasicType - Return true if the specified tag is legal for
158 static bool isBasicType(unsigned TAG) {
159 return TAG == dwarf::DW_TAG_base_type;
162 /// Verify - Verify that a type descriptor is well formed.
165 explicit DIType(GlobalVariable *GV);
169 DIDescriptor getContext() const { return getDescriptorField(1); }
170 std::string getName() const { return getStringField(2); }
171 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
172 unsigned getLineNumber() const { return getUnsignedField(4); }
173 uint64_t getSizeInBits() const { return getUInt64Field(5); }
174 uint64_t getAlignInBits() const { return getUInt64Field(6); }
175 // FIXME: Offset is only used for DW_TAG_member nodes. Making every type
176 // carry this is just plain insane.
177 uint64_t getOffsetInBits() const { return getUInt64Field(7); }
178 unsigned getFlags() const { return getUnsignedField(8); }
180 virtual std::string getFilename() const {
181 assert (0 && "Invalid DIDescriptor");
185 virtual std::string getDirectory() const {
186 assert (0 && "Invalid DIDescriptor");
191 /// DIBasicType - A basic type, like 'int' or 'float'.
192 class DIBasicType : public DIType {
194 explicit DIBasicType(GlobalVariable *GV);
196 unsigned getEncoding() const { return getUnsignedField(9); }
197 std::string getFilename() const { return getStringField(10); }
198 std::string getDirectory() const { return getStringField(11); }
201 /// DIDerivedType - A simple derived type, like a const qualified type,
202 /// a typedef, a pointer or reference, etc.
203 class DIDerivedType : public DIType {
205 explicit DIDerivedType(GlobalVariable *GV, bool, bool)
206 : DIType(GV, true, true) {}
208 explicit DIDerivedType(GlobalVariable *GV);
210 DIType getTypeDerivedFrom() const { return getFieldAs<DIType>(9); }
211 std::string getFilename() const { return getStringField(10); }
212 std::string getDirectory() const { return getStringField(11); }
215 /// DICompositeType - This descriptor holds a type that can refer to multiple
216 /// other types, like a function or struct.
217 /// FIXME: Why is this a DIDerivedType??
218 class DICompositeType : public DIDerivedType {
220 explicit DICompositeType(GlobalVariable *GV);
222 DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
223 std::string getFilename() const { return getStringField(11); }
224 std::string getDirectory() const { return getStringField(12); }
226 /// Verify - Verify that a composite type descriptor is well formed.
230 /// DIGlobal - This is a common class for global variables and subprograms.
231 class DIGlobal : public DIDescriptor {
233 explicit DIGlobal(GlobalVariable *GV, unsigned RequiredTag)
234 : DIDescriptor(GV, RequiredTag) {}
236 /// isSubprogram - Return true if the specified tag is legal for
238 static bool isSubprogram(unsigned TAG) {
239 return TAG == dwarf::DW_TAG_subprogram;
242 /// isGlobalVariable - Return true if the specified tag is legal for
243 /// DIGlobalVariable.
244 static bool isGlobalVariable(unsigned TAG) {
245 return TAG == dwarf::DW_TAG_variable;
249 virtual ~DIGlobal() {}
251 DIDescriptor getContext() const { return getDescriptorField(2); }
252 std::string getName() const { return getStringField(3); }
253 std::string getDisplayName() const { return getStringField(4); }
254 std::string getLinkageName() const { return getStringField(5); }
255 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(6); }
256 unsigned getLineNumber() const { return getUnsignedField(7); }
257 DIType getType() const { return getFieldAs<DIType>(8); }
259 /// isLocalToUnit - Return true if this subprogram is local to the current
260 /// compile unit, like 'static' in C.
261 unsigned isLocalToUnit() const { return getUnsignedField(9); }
262 unsigned isDefinition() const { return getUnsignedField(10); }
264 virtual std::string getFilename() const {
265 assert (0 && "Invalid DIDescriptor");
269 virtual std::string getDirectory() const {
270 assert (0 && "Invalid DIDescriptor");
275 /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
276 class DISubprogram : public DIGlobal {
278 explicit DISubprogram(GlobalVariable *GV = 0);
279 std::string getFilename() const { return getStringField(11); }
280 std::string getDirectory() const { return getStringField(12); }
281 DICompositeType getType() const { return getFieldAs<DICompositeType>(8); }
283 /// Verify - Verify that a subprogram descriptor is well formed.
287 /// DIGlobalVariable - This is a wrapper for a global variable.
288 class DIGlobalVariable : public DIGlobal {
290 explicit DIGlobalVariable(GlobalVariable *GV = 0);
292 GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
293 std::string getFilename() const { return getStringField(12); }
294 std::string getDirectory() const { return getStringField(13); }
296 /// Verify - Verify that a global variable descriptor is well formed.
300 /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
302 class DIVariable : public DIDescriptor {
304 explicit DIVariable(GlobalVariable *GV = 0);
306 DIDescriptor getContext() const { return getDescriptorField(1); }
307 std::string getName() const { return getStringField(2); }
308 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
309 unsigned getLineNumber() const { return getUnsignedField(4); }
310 DIType getType() const { return getFieldAs<DIType>(5); }
311 std::string getFilename() const { return getStringField(6); }
312 std::string getDirectory() const { return getStringField(7); }
314 /// isVariable - Return true if the specified tag is legal for DIVariable.
315 static bool isVariable(unsigned Tag);
317 /// Verify - Verify that a variable descriptor is well formed.
321 /// DIBlock - This is a wrapper for a block (e.g. a function, scope, etc).
322 class DIBlock : public DIDescriptor {
324 explicit DIBlock(GlobalVariable *GV = 0);
326 DIDescriptor getContext() const { return getDescriptorField(1); }
329 /// DIFactory - This object assists with the construction of the various
333 // Cached values for uniquing and faster lookups.
334 DIAnchor CompileUnitAnchor, SubProgramAnchor, GlobalVariableAnchor;
335 const Type *EmptyStructPtr; // "{}*".
336 Function *StopPointFn; // llvm.dbg.stoppoint
337 Function *FuncStartFn; // llvm.dbg.func.start
338 Function *RegionStartFn; // llvm.dbg.region.start
339 Function *RegionEndFn; // llvm.dbg.region.end
340 Function *DeclareFn; // llvm.dbg.declare
341 StringMap<Constant*> StringCache;
342 DenseMap<Constant*, DIDescriptor> SimpleConstantCache;
344 DIFactory(const DIFactory &); // DO NOT IMPLEMENT
345 void operator=(const DIFactory&); // DO NOT IMPLEMENT
347 explicit DIFactory(Module &m);
349 /// GetOrCreateCompileUnitAnchor - Return the anchor for compile units,
350 /// creating a new one if there isn't already one in the module.
351 DIAnchor GetOrCreateCompileUnitAnchor();
353 /// GetOrCreateSubprogramAnchor - Return the anchor for subprograms,
354 /// creating a new one if there isn't already one in the module.
355 DIAnchor GetOrCreateSubprogramAnchor();
357 /// GetOrCreateGlobalVariableAnchor - Return the anchor for globals,
358 /// creating a new one if there isn't already one in the module.
359 DIAnchor GetOrCreateGlobalVariableAnchor();
361 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
362 /// This implicitly uniques the arrays created.
363 DIArray GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys);
365 /// GetOrCreateSubrange - Create a descriptor for a value range. This
366 /// implicitly uniques the values returned.
367 DISubrange GetOrCreateSubrange(int64_t Lo, int64_t Hi);
369 /// CreateCompileUnit - Create a new descriptor for the specified compile
371 DICompileUnit CreateCompileUnit(unsigned LangID,
372 const std::string &Filename,
373 const std::string &Directory,
374 const std::string &Producer);
376 /// CreateEnumerator - Create a single enumerator value.
377 DIEnumerator CreateEnumerator(const std::string &Name, uint64_t Val);
379 /// CreateBasicType - Create a basic type like int, float, etc.
380 DIBasicType CreateBasicType(DIDescriptor Context, const std::string &Name,
381 DICompileUnit CompileUnit, unsigned LineNumber,
382 uint64_t SizeInBits, uint64_t AlignInBits,
383 uint64_t OffsetInBits, unsigned Flags,
385 const std::string *FileName = 0,
386 const std::string *Directory = 0);
388 /// CreateDerivedType - Create a derived type like const qualified type,
389 /// pointer, typedef, etc.
390 DIDerivedType CreateDerivedType(unsigned Tag, DIDescriptor Context,
391 const std::string &Name,
392 DICompileUnit CompileUnit,
394 uint64_t SizeInBits, uint64_t AlignInBits,
395 uint64_t OffsetInBits, unsigned Flags,
397 const std::string *FileName = 0,
398 const std::string *Directory = 0);
400 /// CreateCompositeType - Create a composite type like array, struct, etc.
401 DICompositeType CreateCompositeType(unsigned Tag, DIDescriptor Context,
402 const std::string &Name,
403 DICompileUnit CompileUnit,
406 uint64_t AlignInBits,
407 uint64_t OffsetInBits, unsigned Flags,
410 const std::string *FileName = 0,
411 const std::string *Directory = 0);
413 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
414 /// See comments in DISubprogram for descriptions of these fields.
415 DISubprogram CreateSubprogram(DIDescriptor Context, const std::string &Name,
416 const std::string &DisplayName,
417 const std::string &LinkageName,
418 DICompileUnit CompileUnit, unsigned LineNo,
419 DIType Type, bool isLocalToUnit,
421 const std::string *FileName = 0,
422 const std::string *Directory = 0);
424 /// CreateGlobalVariable - Create a new descriptor for the specified global.
426 CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
427 const std::string &DisplayName,
428 const std::string &LinkageName,
429 DICompileUnit CompileUnit,
430 unsigned LineNo, DIType Type, bool isLocalToUnit,
431 bool isDefinition, llvm::GlobalVariable *GV,
432 const std::string *FileName = 0,
433 const std::string *Directory = 0);
435 /// CreateVariable - Create a new descriptor for the specified variable.
436 DIVariable CreateVariable(unsigned Tag, DIDescriptor Context,
437 const std::string &Name,
438 DICompileUnit CompileUnit, unsigned LineNo,
440 const std::string *FileName = 0,
441 const std::string *Directory = 0);
443 /// CreateBlock - This creates a descriptor for a lexical block with the
444 /// specified parent context.
445 DIBlock CreateBlock(DIDescriptor Context);
447 /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
448 /// inserting it at the end of the specified basic block.
449 void InsertStopPoint(DICompileUnit CU, unsigned LineNo, unsigned ColNo,
452 /// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
453 /// mark the start of the specified subprogram.
454 void InsertSubprogramStart(DISubprogram SP, BasicBlock *BB);
456 /// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
457 /// mark the start of a region for the specified scoping descriptor.
458 void InsertRegionStart(DIDescriptor D, BasicBlock *BB);
460 /// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
461 /// mark the end of a region for the specified scoping descriptor.
462 void InsertRegionEnd(DIDescriptor D, BasicBlock *BB);
464 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
465 void InsertDeclare(llvm::Value *Storage, DIVariable D, BasicBlock *BB);
468 Constant *GetTagConstant(unsigned TAG);
469 Constant *GetStringConstant(const std::string &String);
470 DIAnchor GetOrCreateAnchor(unsigned TAG, const char *Name);
472 /// getCastToEmpty - Return the descriptor as a Constant* with type '{}*'.
473 Constant *getCastToEmpty(DIDescriptor D);
476 /// Finds the stoppoint coressponding to this instruction, that is the
477 /// stoppoint that dominates this instruction
478 const DbgStopPointInst *findStopPoint(const Instruction *Inst);
480 /// Finds the stoppoint corresponding to first real (non-debug intrinsic)
481 /// instruction in this Basic Block, and returns the stoppoint for it.
482 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB);
484 /// Finds the dbg.declare intrinsic corresponding to this value if any.
485 /// It looks through pointer casts too.
486 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts = true);
487 } // end namespace llvm