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"
29 class DbgStopPointInst;
36 Version7 = 7 << 16, // Current version of debug information.
37 Version6 = 6 << 16, // Constant for version 6.
38 Version5 = 5 << 16, // Constant for version 5.
39 Version4 = 4 << 16, // Constant for version 4.
40 VersionMask = 0xffff0000 // Mask for version number.
46 /// DIDescriptor constructor. If the specified GV is non-null, this checks
47 /// to make sure that the tag in the descriptor matches 'RequiredTag'. If
48 /// not, the debug info is corrupt and we ignore it.
49 DIDescriptor(GlobalVariable *GV, unsigned RequiredTag);
51 std::string getStringField(unsigned Elt) const;
52 unsigned getUnsignedField(unsigned Elt) const {
53 return (unsigned)getUInt64Field(Elt);
55 uint64_t getUInt64Field(unsigned Elt) const;
56 DIDescriptor getDescriptorField(unsigned Elt) const;
58 template <typename DescTy>
59 DescTy getFieldAs(unsigned Elt) const {
60 return DescTy(getDescriptorField(Elt).getGV());
63 GlobalVariable *getGlobalVariableField(unsigned Elt) const;
66 explicit DIDescriptor() : GV(0) {}
67 explicit DIDescriptor(GlobalVariable *gv) : GV(gv) {}
69 bool isNull() const { return GV == 0; }
71 GlobalVariable *getGV() const { return GV; }
73 unsigned getVersion() const {
74 return getUnsignedField(0) & VersionMask;
77 unsigned getTag() const {
78 return getUnsignedField(0) & ~VersionMask;
80 static inline bool classof(const DIDescriptor *D) { return true; }
83 /// DIAnchor - A wrapper for various anchor descriptors.
84 class DIAnchor : public DIDescriptor {
86 explicit DIAnchor(GlobalVariable *GV = 0);
88 unsigned getAnchorTag() const { return getUnsignedField(1); }
91 /// DISubrange - This is used to represent ranges, for array bounds.
92 class DISubrange : public DIDescriptor {
94 explicit DISubrange(GlobalVariable *GV = 0);
96 int64_t getLo() const { return (int64_t)getUInt64Field(1); }
97 int64_t getHi() const { return (int64_t)getUInt64Field(2); }
98 static bool isSubrange(unsigned);
99 static inline bool classof(const DISubrange *) { return true; }
100 static inline bool classof(const DIDescriptor *D) {
101 return isSubrange(D->getTag());
105 /// DIArray - This descriptor holds an array of descriptors.
106 class DIArray : public DIDescriptor {
108 explicit DIArray(GlobalVariable *GV = 0) : DIDescriptor(GV) {}
110 unsigned getNumElements() const;
111 DIDescriptor getElement(unsigned Idx) const {
112 return getDescriptorField(Idx);
116 /// DICompileUnit - A wrapper for a compile unit.
117 class DICompileUnit : public DIDescriptor {
119 explicit DICompileUnit(GlobalVariable *GV = 0);
121 unsigned getLanguage() const { return getUnsignedField(2); }
122 std::string getFilename() const { return getStringField(3); }
123 std::string getDirectory() const { return getStringField(4); }
124 std::string getProducer() const { return getStringField(5); }
127 /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
128 /// FIXME: it seems strange that this doesn't have either a reference to the
129 /// type/precision or a file/line pair for location info.
130 class DIEnumerator : public DIDescriptor {
132 explicit DIEnumerator(GlobalVariable *GV = 0);
134 std::string getName() const { return getStringField(1); }
135 uint64_t getEnumValue() const { return getUInt64Field(2); }
138 /// DIType - This is a wrapper for a type.
139 /// FIXME: Types should be factored much better so that CV qualifiers and
140 /// others do not require a huge and empty descriptor full of zeros.
141 class DIType : public DIDescriptor {
143 DIType(GlobalVariable *GV, unsigned Tag) : DIDescriptor(GV, Tag) {}
144 // This ctor is used when the Tag has already been validated by a derived
146 DIType(GlobalVariable *GV, bool, bool) : DIDescriptor(GV) {}
148 explicit DIType(GlobalVariable *GV);
152 DIDescriptor getContext() const { return getDescriptorField(1); }
153 std::string getName() const { return getStringField(2); }
154 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
155 unsigned getLineNumber() const { return getUnsignedField(4); }
156 uint64_t getSizeInBits() const { return getUInt64Field(5); }
157 uint64_t getAlignInBits() const { return getUInt64Field(6); }
158 // FIXME: Offset is only used for DW_TAG_member nodes. Making every type
159 // carry this is just plain insane.
160 uint64_t getOffsetInBits() const { return getUInt64Field(7); }
161 unsigned getFlags() const { return getUnsignedField(8); }
163 virtual std::string getFilename() const {
164 assert (0 && "Invalid DIDescriptor");
168 virtual std::string getDirectory() const {
169 assert (0 && "Invalid DIDescriptor");
174 /// DIBasicType - A basic type, like 'int' or 'float'.
175 class DIBasicType : public DIType {
177 explicit DIBasicType(GlobalVariable *GV);
179 unsigned getEncoding() const { return getUnsignedField(9); }
180 std::string getFilename() const { return getStringField(10); }
181 std::string getDirectory() const { return getStringField(11); }
184 /// DIDerivedType - A simple derived type, like a const qualified type,
185 /// a typedef, a pointer or reference, etc.
186 class DIDerivedType : public DIType {
188 explicit DIDerivedType(GlobalVariable *GV, bool, bool)
189 : DIType(GV, true, true) {}
191 explicit DIDerivedType(GlobalVariable *GV);
193 DIType getTypeDerivedFrom() const { return getFieldAs<DIType>(9); }
194 std::string getFilename() const { return getStringField(10); }
195 std::string getDirectory() const { return getStringField(11); }
197 /// isDerivedType - Return true if the specified tag is legal for
199 static bool isDerivedType(unsigned TAG);
201 static inline bool classof(const DIDerivedType *) { return true; }
202 static inline bool classof(const DIDescriptor *D) {
203 return isDerivedType(D->getTag());
208 /// DICompositeType - This descriptor holds a type that can refer to multiple
209 /// other types, like a function or struct.
210 /// FIXME: Why is this a DIDerivedType??
211 class DICompositeType : public DIDerivedType {
213 explicit DICompositeType(GlobalVariable *GV);
215 DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
216 std::string getFilename() const { return getStringField(11); }
217 std::string getDirectory() const { return getStringField(12); }
219 /// isCompositeType - Return true if the specified tag is legal for
221 static bool isCompositeType(unsigned TAG);
222 static inline bool classof(const DIDerivedType *) { return true; }
223 static inline bool classof(const DIDescriptor *D) {
224 return isCompositeType(D->getTag());
228 /// DIGlobal - This is a common class for global variables and subprograms.
229 class DIGlobal : public DIDescriptor {
231 explicit DIGlobal(GlobalVariable *GV, unsigned RequiredTag)
232 : DIDescriptor(GV, RequiredTag) {}
234 virtual ~DIGlobal() {}
236 DIDescriptor getContext() const { return getDescriptorField(2); }
237 std::string getName() const { return getStringField(3); }
238 std::string getDisplayName() const { return getStringField(4); }
239 std::string getLinkageName() const { return getStringField(5); }
241 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(6); }
242 unsigned getLineNumber() const { return getUnsignedField(7); }
243 DIType getType() const { return getFieldAs<DIType>(8); }
245 /// isLocalToUnit - Return true if this subprogram is local to the current
246 /// compile unit, like 'static' in C.
247 unsigned isLocalToUnit() const { return getUnsignedField(9); }
248 unsigned isDefinition() const { return getUnsignedField(10); }
250 virtual std::string getFilename() const {
251 assert (0 && "Invalid DIDescriptor");
255 virtual std::string getDirectory() const {
256 assert (0 && "Invalid DIDescriptor");
262 /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
263 class DISubprogram : public DIGlobal {
265 explicit DISubprogram(GlobalVariable *GV = 0);
266 std::string getFilename() const { return getStringField(11); }
267 std::string getDirectory() const { return getStringField(12); }
268 DICompositeType getType() const { return getFieldAs<DICompositeType>(8); }
271 /// DIGlobalVariable - This is a wrapper for a global variable.
272 class DIGlobalVariable : public DIGlobal {
274 explicit DIGlobalVariable(GlobalVariable *GV = 0);
276 GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
277 std::string getFilename() const { return getStringField(12); }
278 std::string getDirectory() const { return getStringField(13); }
282 /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
284 class DIVariable : public DIDescriptor {
286 explicit DIVariable(GlobalVariable *GV = 0);
288 DIDescriptor getContext() const { return getDescriptorField(1); }
289 std::string getName() const { return getStringField(2); }
291 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
292 unsigned getLineNumber() const { return getUnsignedField(4); }
293 DIType getType() const { return getFieldAs<DIType>(5); }
294 std::string getFilename() const { return getStringField(6); }
295 std::string getDirectory() const { return getStringField(7); }
297 /// isVariable - Return true if the specified tag is legal for DIVariable.
298 static bool isVariable(unsigned Tag);
302 /// DIBlock - This is a wrapper for a block (e.g. a function, scope, etc).
303 class DIBlock : public DIDescriptor {
305 explicit DIBlock(GlobalVariable *GV = 0);
307 DIDescriptor getContext() const { return getDescriptorField(1); }
310 /// DIFactory - This object assists with the construction of the various
314 // Cached values for uniquing and faster lookups.
315 DIAnchor CompileUnitAnchor, SubProgramAnchor, GlobalVariableAnchor;
316 const Type *EmptyStructPtr; // "{}*".
317 Function *StopPointFn; // llvm.dbg.stoppoint
318 Function *FuncStartFn; // llvm.dbg.func.start
319 Function *RegionStartFn; // llvm.dbg.region.start
320 Function *RegionEndFn; // llvm.dbg.region.end
321 Function *DeclareFn; // llvm.dbg.declare
322 StringMap<Constant*> StringCache;
323 DenseMap<Constant*, DIDescriptor> SimpleConstantCache;
325 DIFactory(const DIFactory &); // DO NOT IMPLEMENT
326 void operator=(const DIFactory&); // DO NOT IMPLEMENT
328 explicit DIFactory(Module &m);
330 /// GetOrCreateCompileUnitAnchor - Return the anchor for compile units,
331 /// creating a new one if there isn't already one in the module.
332 DIAnchor GetOrCreateCompileUnitAnchor();
334 /// GetOrCreateSubprogramAnchor - Return the anchor for subprograms,
335 /// creating a new one if there isn't already one in the module.
336 DIAnchor GetOrCreateSubprogramAnchor();
338 /// GetOrCreateGlobalVariableAnchor - Return the anchor for globals,
339 /// creating a new one if there isn't already one in the module.
340 DIAnchor GetOrCreateGlobalVariableAnchor();
342 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
343 /// This implicitly uniques the arrays created.
344 DIArray GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys);
346 /// GetOrCreateSubrange - Create a descriptor for a value range. This
347 /// implicitly uniques the values returned.
348 DISubrange GetOrCreateSubrange(int64_t Lo, int64_t Hi);
351 /// CreateCompileUnit - Create a new descriptor for the specified compile
353 DICompileUnit CreateCompileUnit(unsigned LangID,
354 const std::string &Filename,
355 const std::string &Directory,
356 const std::string &Producer);
358 /// CreateEnumerator - Create a single enumerator value.
359 DIEnumerator CreateEnumerator(const std::string &Name, uint64_t Val);
361 /// CreateBasicType - Create a basic type like int, float, etc.
362 DIBasicType CreateBasicType(DIDescriptor Context, const std::string &Name,
363 DICompileUnit CompileUnit, unsigned LineNumber,
364 uint64_t SizeInBits, uint64_t AlignInBits,
365 uint64_t OffsetInBits, unsigned Flags,
367 const std::string *FileName = 0,
368 const std::string *Directory = 0);
370 /// CreateDerivedType - Create a derived type like const qualified type,
371 /// pointer, typedef, etc.
372 DIDerivedType CreateDerivedType(unsigned Tag, DIDescriptor Context,
373 const std::string &Name,
374 DICompileUnit CompileUnit,
376 uint64_t SizeInBits, uint64_t AlignInBits,
377 uint64_t OffsetInBits, unsigned Flags,
379 const std::string *FileName = 0,
380 const std::string *Directory = 0);
382 /// CreateCompositeType - Create a composite type like array, struct, etc.
383 DICompositeType CreateCompositeType(unsigned Tag, DIDescriptor Context,
384 const std::string &Name,
385 DICompileUnit CompileUnit,
388 uint64_t AlignInBits,
389 uint64_t OffsetInBits, unsigned Flags,
392 const std::string *FileName = 0,
393 const std::string *Directory = 0);
395 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
396 /// See comments in DISubprogram for descriptions of these fields.
397 DISubprogram CreateSubprogram(DIDescriptor Context, const std::string &Name,
398 const std::string &DisplayName,
399 const std::string &LinkageName,
400 DICompileUnit CompileUnit, unsigned LineNo,
401 DIType Type, bool isLocalToUnit,
403 const std::string *FileName = 0,
404 const std::string *Directory = 0);
406 /// CreateGlobalVariable - Create a new descriptor for the specified global.
408 CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
409 const std::string &DisplayName,
410 const std::string &LinkageName,
411 DICompileUnit CompileUnit,
412 unsigned LineNo, DIType Type, bool isLocalToUnit,
413 bool isDefinition, llvm::GlobalVariable *GV,
414 const std::string *FileName = 0,
415 const std::string *Directory = 0);
418 /// CreateVariable - Create a new descriptor for the specified variable.
419 DIVariable CreateVariable(unsigned Tag, DIDescriptor Context,
420 const std::string &Name,
421 DICompileUnit CompileUnit, unsigned LineNo,
423 const std::string *FileName = 0,
424 const std::string *Directory = 0);
426 /// CreateBlock - This creates a descriptor for a lexical block with the
427 /// specified parent context.
428 DIBlock CreateBlock(DIDescriptor Context);
430 /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
431 /// inserting it at the end of the specified basic block.
432 void InsertStopPoint(DICompileUnit CU, unsigned LineNo, unsigned ColNo,
435 /// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
436 /// mark the start of the specified subprogram.
437 void InsertSubprogramStart(DISubprogram SP, BasicBlock *BB);
439 /// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
440 /// mark the start of a region for the specified scoping descriptor.
441 void InsertRegionStart(DIDescriptor D, BasicBlock *BB);
443 /// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
444 /// mark the end of a region for the specified scoping descriptor.
445 void InsertRegionEnd(DIDescriptor D, BasicBlock *BB);
447 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
448 void InsertDeclare(llvm::Value *Storage, DIVariable D, BasicBlock *BB);
451 Constant *GetTagConstant(unsigned TAG);
452 Constant *GetStringConstant(const std::string &String);
453 DIAnchor GetOrCreateAnchor(unsigned TAG, const char *Name);
455 /// getCastToEmpty - Return the descriptor as a Constant* with type '{}*'.
456 Constant *getCastToEmpty(DIDescriptor D);
459 /// Finds the stoppoint coressponding to this instruction, that is the
460 /// stoppoint that dominates this instruction
461 const DbgStopPointInst *findStopPoint(const Instruction *Inst);
463 /// Finds the stoppoint corresponding to first real (non-debug intrinsic)
464 /// instruction in this Basic Block, and returns the stoppoint for it.
465 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB);
467 /// Finds the dbg.declare intrinsic corresponding to this value if any.
468 /// It looks through pointer casts too.
469 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts = true);
470 } // end namespace llvm