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;
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 std::string getStringField(unsigned Elt) 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 std::string getFilename() const { return getStringField(3); }
110 std::string getDirectory() const { return getStringField(4); }
111 std::string getProducer() const { return getStringField(5); }
113 /// Verify - Verify that a compile unit is well formed.
117 /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
118 /// FIXME: it seems strange that this doesn't have either a reference to the
119 /// type/precision or a file/line pair for location info.
120 class DIEnumerator : public DIDescriptor {
122 explicit DIEnumerator(GlobalVariable *GV = 0);
124 std::string getName() const { return getStringField(1); }
125 uint64_t getEnumValue() const { return getUInt64Field(2); }
128 /// DIType - This is a wrapper for a type.
129 /// FIXME: Types should be factored much better so that CV qualifiers and
130 /// others do not require a huge and empty descriptor full of zeros.
131 class DIType : public DIDescriptor {
134 FlagPrivate = 1 << 0,
135 FlagProtected = 1 << 1,
140 DIType(GlobalVariable *GV, unsigned Tag) : DIDescriptor(GV, Tag) {}
141 // This ctor is used when the Tag has already been validated by a derived
143 DIType(GlobalVariable *GV, bool, bool) : DIDescriptor(GV) {}
146 /// isDerivedType - Return true if the specified tag is legal for
148 static bool isDerivedType(unsigned TAG);
150 /// isCompositeType - Return true if the specified tag is legal for
152 static bool isCompositeType(unsigned TAG);
154 /// isBasicType - Return true if the specified tag is legal for
156 static bool isBasicType(unsigned TAG) {
157 return TAG == dwarf::DW_TAG_base_type;
160 /// Verify - Verify that a type descriptor is well formed.
163 explicit DIType(GlobalVariable *GV);
167 DIDescriptor getContext() const { return getDescriptorField(1); }
168 std::string getName() const { return getStringField(2); }
169 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
170 unsigned getLineNumber() const { return getUnsignedField(4); }
171 uint64_t getSizeInBits() const { return getUInt64Field(5); }
172 uint64_t getAlignInBits() const { return getUInt64Field(6); }
173 // FIXME: Offset is only used for DW_TAG_member nodes. Making every type
174 // carry this is just plain insane.
175 uint64_t getOffsetInBits() const { return getUInt64Field(7); }
176 unsigned getFlags() const { return getUnsignedField(8); }
177 bool isPrivate() const { return (getFlags() & FlagPrivate) != 0; }
178 bool isProtected() const { return (getFlags() & FlagProtected) != 0; }
179 bool isForwardDecl() const { return (getFlags() & FlagFwdDecl) != 0; }
181 virtual std::string getFilename() const {
182 assert (0 && "Invalid DIDescriptor");
186 virtual std::string getDirectory() const {
187 assert (0 && "Invalid DIDescriptor");
192 /// DIBasicType - A basic type, like 'int' or 'float'.
193 class DIBasicType : public DIType {
195 explicit DIBasicType(GlobalVariable *GV);
197 unsigned getEncoding() const { return getUnsignedField(9); }
198 std::string getFilename() const { return getStringField(10); }
199 std::string getDirectory() const { return getStringField(11); }
202 /// DIDerivedType - A simple derived type, like a const qualified type,
203 /// a typedef, a pointer or reference, etc.
204 class DIDerivedType : public DIType {
206 explicit DIDerivedType(GlobalVariable *GV, bool, bool)
207 : DIType(GV, true, true) {}
209 explicit DIDerivedType(GlobalVariable *GV);
211 DIType getTypeDerivedFrom() const { return getFieldAs<DIType>(9); }
212 std::string getFilename() const { return getStringField(10); }
213 std::string getDirectory() const { return getStringField(11); }
216 /// DICompositeType - This descriptor holds a type that can refer to multiple
217 /// other types, like a function or struct.
218 /// FIXME: Why is this a DIDerivedType??
219 class DICompositeType : public DIDerivedType {
221 explicit DICompositeType(GlobalVariable *GV);
223 DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
224 std::string getFilename() const { return getStringField(11); }
225 std::string getDirectory() const { return getStringField(12); }
227 /// Verify - Verify that a composite type descriptor is well formed.
231 /// DIGlobal - This is a common class for global variables and subprograms.
232 class DIGlobal : public DIDescriptor {
234 explicit DIGlobal(GlobalVariable *GV, unsigned RequiredTag)
235 : DIDescriptor(GV, RequiredTag) {}
237 /// isSubprogram - Return true if the specified tag is legal for
239 static bool isSubprogram(unsigned TAG) {
240 return TAG == dwarf::DW_TAG_subprogram;
243 /// isGlobalVariable - Return true if the specified tag is legal for
244 /// DIGlobalVariable.
245 static bool isGlobalVariable(unsigned TAG) {
246 return TAG == dwarf::DW_TAG_variable;
250 virtual ~DIGlobal() {}
252 DIDescriptor getContext() const { return getDescriptorField(2); }
253 std::string getName() const { return getStringField(3); }
254 std::string getDisplayName() const { return getStringField(4); }
255 std::string getLinkageName() const { return getStringField(5); }
256 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(6); }
257 unsigned getLineNumber() const { return getUnsignedField(7); }
258 DIType getType() const { return getFieldAs<DIType>(8); }
260 /// isLocalToUnit - Return true if this subprogram is local to the current
261 /// compile unit, like 'static' in C.
262 unsigned isLocalToUnit() const { return getUnsignedField(9); }
263 unsigned isDefinition() const { return getUnsignedField(10); }
265 virtual std::string getFilename() const {
266 assert (0 && "Invalid DIDescriptor");
270 virtual std::string getDirectory() const {
271 assert (0 && "Invalid DIDescriptor");
276 /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
277 class DISubprogram : public DIGlobal {
279 explicit DISubprogram(GlobalVariable *GV = 0);
280 std::string getFilename() const { return getStringField(11); }
281 std::string getDirectory() const { return getStringField(12); }
282 DICompositeType getType() const { return getFieldAs<DICompositeType>(8); }
284 /// Verify - Verify that a subprogram descriptor is well formed.
288 /// DIGlobalVariable - This is a wrapper for a global variable.
289 class DIGlobalVariable : public DIGlobal {
291 explicit DIGlobalVariable(GlobalVariable *GV = 0);
293 GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
294 std::string getFilename() const { return getStringField(12); }
295 std::string getDirectory() const { return getStringField(13); }
297 /// Verify - Verify that a global variable descriptor is well formed.
301 /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
303 class DIVariable : public DIDescriptor {
305 explicit DIVariable(GlobalVariable *GV = 0);
307 DIDescriptor getContext() const { return getDescriptorField(1); }
308 std::string getName() const { return getStringField(2); }
309 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
310 unsigned getLineNumber() const { return getUnsignedField(4); }
311 DIType getType() const { return getFieldAs<DIType>(5); }
312 std::string getFilename() const { return getStringField(6); }
313 std::string getDirectory() const { return getStringField(7); }
315 /// isVariable - Return true if the specified tag is legal for DIVariable.
316 static bool isVariable(unsigned Tag);
318 /// Verify - Verify that a variable descriptor is well formed.
322 /// DIBlock - This is a wrapper for a block (e.g. a function, scope, etc).
323 class DIBlock : public DIDescriptor {
325 explicit DIBlock(GlobalVariable *GV = 0);
327 DIDescriptor getContext() const { return getDescriptorField(1); }
330 /// DIFactory - This object assists with the construction of the various
334 // Cached values for uniquing and faster lookups.
335 DIAnchor CompileUnitAnchor, SubProgramAnchor, GlobalVariableAnchor;
336 const Type *EmptyStructPtr; // "{}*".
337 Function *StopPointFn; // llvm.dbg.stoppoint
338 Function *FuncStartFn; // llvm.dbg.func.start
339 Function *RegionStartFn; // llvm.dbg.region.start
340 Function *RegionEndFn; // llvm.dbg.region.end
341 Function *DeclareFn; // llvm.dbg.declare
342 StringMap<Constant*> StringCache;
343 DenseMap<Constant*, DIDescriptor> SimpleConstantCache;
345 DIFactory(const DIFactory &); // DO NOT IMPLEMENT
346 void operator=(const DIFactory&); // DO NOT IMPLEMENT
348 explicit DIFactory(Module &m);
350 /// GetOrCreateCompileUnitAnchor - Return the anchor for compile units,
351 /// creating a new one if there isn't already one in the module.
352 DIAnchor GetOrCreateCompileUnitAnchor();
354 /// GetOrCreateSubprogramAnchor - Return the anchor for subprograms,
355 /// creating a new one if there isn't already one in the module.
356 DIAnchor GetOrCreateSubprogramAnchor();
358 /// GetOrCreateGlobalVariableAnchor - Return the anchor for globals,
359 /// creating a new one if there isn't already one in the module.
360 DIAnchor GetOrCreateGlobalVariableAnchor();
362 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
363 /// This implicitly uniques the arrays created.
364 DIArray GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys);
366 /// GetOrCreateSubrange - Create a descriptor for a value range. This
367 /// implicitly uniques the values returned.
368 DISubrange GetOrCreateSubrange(int64_t Lo, int64_t Hi);
370 /// CreateCompileUnit - Create a new descriptor for the specified compile
372 DICompileUnit CreateCompileUnit(unsigned LangID,
373 const std::string &Filename,
374 const std::string &Directory,
375 const std::string &Producer);
377 /// CreateEnumerator - Create a single enumerator value.
378 DIEnumerator CreateEnumerator(const std::string &Name, uint64_t Val);
380 /// CreateBasicType - Create a basic type like int, float, etc.
381 DIBasicType CreateBasicType(DIDescriptor Context, const std::string &Name,
382 DICompileUnit CompileUnit, unsigned LineNumber,
383 uint64_t SizeInBits, uint64_t AlignInBits,
384 uint64_t OffsetInBits, unsigned Flags,
386 const std::string *FileName = 0,
387 const std::string *Directory = 0);
389 /// CreateDerivedType - Create a derived type like const qualified type,
390 /// pointer, typedef, etc.
391 DIDerivedType CreateDerivedType(unsigned Tag, DIDescriptor Context,
392 const std::string &Name,
393 DICompileUnit CompileUnit,
395 uint64_t SizeInBits, uint64_t AlignInBits,
396 uint64_t OffsetInBits, unsigned Flags,
398 const std::string *FileName = 0,
399 const std::string *Directory = 0);
401 /// CreateCompositeType - Create a composite type like array, struct, etc.
402 DICompositeType CreateCompositeType(unsigned Tag, DIDescriptor Context,
403 const std::string &Name,
404 DICompileUnit CompileUnit,
407 uint64_t AlignInBits,
408 uint64_t OffsetInBits, unsigned Flags,
411 const std::string *FileName = 0,
412 const std::string *Directory = 0);
414 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
415 /// See comments in DISubprogram for descriptions of these fields.
416 DISubprogram CreateSubprogram(DIDescriptor Context, const std::string &Name,
417 const std::string &DisplayName,
418 const std::string &LinkageName,
419 DICompileUnit CompileUnit, unsigned LineNo,
420 DIType Type, bool isLocalToUnit,
422 const std::string *FileName = 0,
423 const std::string *Directory = 0);
425 /// CreateGlobalVariable - Create a new descriptor for the specified global.
427 CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
428 const std::string &DisplayName,
429 const std::string &LinkageName,
430 DICompileUnit CompileUnit,
431 unsigned LineNo, DIType Type, bool isLocalToUnit,
432 bool isDefinition, llvm::GlobalVariable *GV,
433 const std::string *FileName = 0,
434 const std::string *Directory = 0);
436 /// CreateVariable - Create a new descriptor for the specified variable.
437 DIVariable CreateVariable(unsigned Tag, DIDescriptor Context,
438 const std::string &Name,
439 DICompileUnit CompileUnit, unsigned LineNo,
441 const std::string *FileName = 0,
442 const std::string *Directory = 0);
444 /// CreateBlock - This creates a descriptor for a lexical block with the
445 /// specified parent context.
446 DIBlock CreateBlock(DIDescriptor Context);
448 /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
449 /// inserting it at the end of the specified basic block.
450 void InsertStopPoint(DICompileUnit CU, unsigned LineNo, unsigned ColNo,
453 /// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
454 /// mark the start of the specified subprogram.
455 void InsertSubprogramStart(DISubprogram SP, BasicBlock *BB);
457 /// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
458 /// mark the start of a region for the specified scoping descriptor.
459 void InsertRegionStart(DIDescriptor D, BasicBlock *BB);
461 /// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
462 /// mark the end of a region for the specified scoping descriptor.
463 void InsertRegionEnd(DIDescriptor D, BasicBlock *BB);
465 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
466 void InsertDeclare(llvm::Value *Storage, DIVariable D, BasicBlock *BB);
469 Constant *GetTagConstant(unsigned TAG);
470 Constant *GetStringConstant(const std::string &String);
471 DIAnchor GetOrCreateAnchor(unsigned TAG, const char *Name);
473 /// getCastToEmpty - Return the descriptor as a Constant* with type '{}*'.
474 Constant *getCastToEmpty(DIDescriptor D);
477 /// Finds the stoppoint coressponding to this instruction, that is the
478 /// stoppoint that dominates this instruction
479 const DbgStopPointInst *findStopPoint(const Instruction *Inst);
481 /// Finds the stoppoint corresponding to first real (non-debug intrinsic)
482 /// instruction in this Basic Block, and returns the stoppoint for it.
483 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB);
485 /// Finds the dbg.declare intrinsic corresponding to this value if any.
486 /// It looks through pointer casts too.
487 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts = true);
488 } // end namespace llvm