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/ADT/StringMap.h"
19 #include "llvm/ADT/DenseMap.h"
20 #include "llvm/Support/Dwarf.h"
30 struct DbgStopPointInst;
31 struct DbgDeclareInst;
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 /// isMain - Each input file is encoded as a separate compile unit in LLVM
114 /// debugging information output. However, many target specific tool chains
115 /// prefer to encode only one compile unit in an object file. In this
116 /// situation, the LLVM code generator will include debugging information
117 /// entities in the compile unit that is marked as main compile unit. The
118 /// code generator accepts maximum one main compile unit per module. If a
119 /// module does not contain any main compile unit then the code generator
120 /// will emit multiple compile units in the output object file.
121 bool isMain() const { return getUnsignedField(6); }
122 bool isOptimized() const { return getUnsignedField(7); }
123 std::string getFlags() const { return getStringField(8); }
125 /// Verify - Verify that a compile unit is well formed.
128 /// dump - print compile unit.
132 /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
133 /// FIXME: it seems strange that this doesn't have either a reference to the
134 /// type/precision or a file/line pair for location info.
135 class DIEnumerator : public DIDescriptor {
137 explicit DIEnumerator(GlobalVariable *GV = 0);
139 std::string getName() const { return getStringField(1); }
140 uint64_t getEnumValue() const { return getUInt64Field(2); }
143 /// DIType - This is a wrapper for a type.
144 /// FIXME: Types should be factored much better so that CV qualifiers and
145 /// others do not require a huge and empty descriptor full of zeros.
146 class DIType : public DIDescriptor {
149 FlagPrivate = 1 << 0,
150 FlagProtected = 1 << 1,
155 DIType(GlobalVariable *GV, unsigned Tag) : DIDescriptor(GV, Tag) {}
156 // This ctor is used when the Tag has already been validated by a derived
158 DIType(GlobalVariable *GV, bool, bool) : DIDescriptor(GV) {}
161 /// isDerivedType - Return true if the specified tag is legal for
163 static bool isDerivedType(unsigned TAG);
165 /// isCompositeType - Return true if the specified tag is legal for
167 static bool isCompositeType(unsigned TAG);
169 /// isBasicType - Return true if the specified tag is legal for
171 static bool isBasicType(unsigned TAG) {
172 return TAG == dwarf::DW_TAG_base_type;
175 /// Verify - Verify that a type descriptor is well formed.
178 explicit DIType(GlobalVariable *GV);
182 DIDescriptor getContext() const { return getDescriptorField(1); }
183 std::string getName() const { return getStringField(2); }
184 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
185 unsigned getLineNumber() const { return getUnsignedField(4); }
186 uint64_t getSizeInBits() const { return getUInt64Field(5); }
187 uint64_t getAlignInBits() const { return getUInt64Field(6); }
188 // FIXME: Offset is only used for DW_TAG_member nodes. Making every type
189 // carry this is just plain insane.
190 uint64_t getOffsetInBits() const { return getUInt64Field(7); }
191 unsigned getFlags() const { return getUnsignedField(8); }
192 bool isPrivate() const { return (getFlags() & FlagPrivate) != 0; }
193 bool isProtected() const { return (getFlags() & FlagProtected) != 0; }
194 bool isForwardDecl() const { return (getFlags() & FlagFwdDecl) != 0; }
196 /// dump - print type.
200 /// DIBasicType - A basic type, like 'int' or 'float'.
201 class DIBasicType : public DIType {
203 explicit DIBasicType(GlobalVariable *GV);
204 unsigned getEncoding() const { return getUnsignedField(9); }
206 /// dump - print basic type.
210 /// DIDerivedType - A simple derived type, like a const qualified type,
211 /// a typedef, a pointer or reference, etc.
212 class DIDerivedType : public DIType {
214 explicit DIDerivedType(GlobalVariable *GV, bool, bool)
215 : DIType(GV, true, true) {}
217 explicit DIDerivedType(GlobalVariable *GV);
218 DIType getTypeDerivedFrom() const { return getFieldAs<DIType>(9); }
220 /// dump - print derived type.
224 /// DICompositeType - This descriptor holds a type that can refer to multiple
225 /// other types, like a function or struct.
226 /// FIXME: Why is this a DIDerivedType??
227 class DICompositeType : public DIDerivedType {
229 explicit DICompositeType(GlobalVariable *GV);
230 DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
232 /// Verify - Verify that a composite type descriptor is well formed.
235 /// dump - print composite type.
239 /// DIGlobal - This is a common class for global variables and subprograms.
240 class DIGlobal : public DIDescriptor {
242 explicit DIGlobal(GlobalVariable *GV, unsigned RequiredTag)
243 : DIDescriptor(GV, RequiredTag) {}
245 /// isSubprogram - Return true if the specified tag is legal for
247 static bool isSubprogram(unsigned TAG) {
248 return TAG == dwarf::DW_TAG_subprogram;
251 /// isGlobalVariable - Return true if the specified tag is legal for
252 /// DIGlobalVariable.
253 static bool isGlobalVariable(unsigned TAG) {
254 return TAG == dwarf::DW_TAG_variable;
258 virtual ~DIGlobal() {}
260 DIDescriptor getContext() const { return getDescriptorField(2); }
261 std::string getName() const { return getStringField(3); }
262 std::string getDisplayName() const { return getStringField(4); }
263 std::string getLinkageName() const { return getStringField(5); }
264 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(6); }
265 unsigned getLineNumber() const { return getUnsignedField(7); }
266 DIType getType() const { return getFieldAs<DIType>(8); }
268 /// isLocalToUnit - Return true if this subprogram is local to the current
269 /// compile unit, like 'static' in C.
270 unsigned isLocalToUnit() const { return getUnsignedField(9); }
271 unsigned isDefinition() const { return getUnsignedField(10); }
273 /// dump - print global.
277 /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
278 class DISubprogram : public DIGlobal {
280 explicit DISubprogram(GlobalVariable *GV = 0);
281 DICompositeType getType() const { return getFieldAs<DICompositeType>(8); }
283 /// Verify - Verify that a subprogram descriptor is well formed.
286 /// dump - print subprogram.
290 /// DIGlobalVariable - This is a wrapper for a global variable.
291 class DIGlobalVariable : public DIGlobal {
293 explicit DIGlobalVariable(GlobalVariable *GV = 0);
294 GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
296 /// Verify - Verify that a global variable descriptor is well formed.
299 /// dump - print global variable.
303 /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
305 class DIVariable : public DIDescriptor {
307 explicit DIVariable(GlobalVariable *GV = 0);
309 DIDescriptor getContext() const { return getDescriptorField(1); }
310 std::string getName() const { return getStringField(2); }
311 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
312 unsigned getLineNumber() const { return getUnsignedField(4); }
313 DIType getType() const { return getFieldAs<DIType>(5); }
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.
321 /// dump - print variable.
325 /// DIBlock - This is a wrapper for a block (e.g. a function, scope, etc).
326 class DIBlock : public DIDescriptor {
328 explicit DIBlock(GlobalVariable *GV = 0);
330 DIDescriptor getContext() const { return getDescriptorField(1); }
333 /// DIFactory - This object assists with the construction of the various
337 // Cached values for uniquing and faster lookups.
338 DIAnchor CompileUnitAnchor, SubProgramAnchor, GlobalVariableAnchor;
339 const Type *EmptyStructPtr; // "{}*".
340 Function *StopPointFn; // llvm.dbg.stoppoint
341 Function *FuncStartFn; // llvm.dbg.func.start
342 Function *RegionStartFn; // llvm.dbg.region.start
343 Function *RegionEndFn; // llvm.dbg.region.end
344 Function *DeclareFn; // llvm.dbg.declare
345 StringMap<Constant*> StringCache;
346 DenseMap<Constant*, DIDescriptor> SimpleConstantCache;
348 DIFactory(const DIFactory &); // DO NOT IMPLEMENT
349 void operator=(const DIFactory&); // DO NOT IMPLEMENT
351 explicit DIFactory(Module &m);
353 /// GetOrCreateCompileUnitAnchor - Return the anchor for compile units,
354 /// creating a new one if there isn't already one in the module.
355 DIAnchor GetOrCreateCompileUnitAnchor();
357 /// GetOrCreateSubprogramAnchor - Return the anchor for subprograms,
358 /// creating a new one if there isn't already one in the module.
359 DIAnchor GetOrCreateSubprogramAnchor();
361 /// GetOrCreateGlobalVariableAnchor - Return the anchor for globals,
362 /// creating a new one if there isn't already one in the module.
363 DIAnchor GetOrCreateGlobalVariableAnchor();
365 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
366 /// This implicitly uniques the arrays created.
367 DIArray GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys);
369 /// GetOrCreateSubrange - Create a descriptor for a value range. This
370 /// implicitly uniques the values returned.
371 DISubrange GetOrCreateSubrange(int64_t Lo, int64_t Hi);
373 /// CreateCompileUnit - Create a new descriptor for the specified compile
375 DICompileUnit CreateCompileUnit(unsigned LangID,
376 const std::string &Filename,
377 const std::string &Directory,
378 const std::string &Producer,
380 bool isOptimized = false,
381 const char *Flags = "");
383 /// CreateEnumerator - Create a single enumerator value.
384 DIEnumerator CreateEnumerator(const std::string &Name, uint64_t Val);
386 /// CreateBasicType - Create a basic type like int, float, etc.
387 DIBasicType CreateBasicType(DIDescriptor Context, const std::string &Name,
388 DICompileUnit CompileUnit, unsigned LineNumber,
389 uint64_t SizeInBits, uint64_t AlignInBits,
390 uint64_t OffsetInBits, unsigned Flags,
393 /// CreateDerivedType - Create a derived type like const qualified type,
394 /// pointer, typedef, etc.
395 DIDerivedType CreateDerivedType(unsigned Tag, DIDescriptor Context,
396 const std::string &Name,
397 DICompileUnit CompileUnit,
399 uint64_t SizeInBits, uint64_t AlignInBits,
400 uint64_t OffsetInBits, unsigned Flags,
403 /// CreateCompositeType - Create a composite type like array, struct, etc.
404 DICompositeType CreateCompositeType(unsigned Tag, DIDescriptor Context,
405 const std::string &Name,
406 DICompileUnit CompileUnit,
409 uint64_t AlignInBits,
410 uint64_t OffsetInBits, unsigned Flags,
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,
423 /// CreateGlobalVariable - Create a new descriptor for the specified global.
425 CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
426 const std::string &DisplayName,
427 const std::string &LinkageName,
428 DICompileUnit CompileUnit,
429 unsigned LineNo, DIType Type, bool isLocalToUnit,
430 bool isDefinition, llvm::GlobalVariable *GV);
432 /// CreateVariable - Create a new descriptor for the specified variable.
433 DIVariable CreateVariable(unsigned Tag, DIDescriptor Context,
434 const std::string &Name,
435 DICompileUnit CompileUnit, unsigned LineNo,
438 /// CreateBlock - This creates a descriptor for a lexical block with the
439 /// specified parent context.
440 DIBlock CreateBlock(DIDescriptor Context);
442 /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
443 /// inserting it at the end of the specified basic block.
444 void InsertStopPoint(DICompileUnit CU, unsigned LineNo, unsigned ColNo,
447 /// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
448 /// mark the start of the specified subprogram.
449 void InsertSubprogramStart(DISubprogram SP, BasicBlock *BB);
451 /// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
452 /// mark the start of a region for the specified scoping descriptor.
453 void InsertRegionStart(DIDescriptor D, BasicBlock *BB);
455 /// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
456 /// mark the end of a region for the specified scoping descriptor.
457 void InsertRegionEnd(DIDescriptor D, BasicBlock *BB);
459 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
460 void InsertDeclare(llvm::Value *Storage, DIVariable D, BasicBlock *BB);
463 Constant *GetTagConstant(unsigned TAG);
464 Constant *GetStringConstant(const std::string &String);
465 DIAnchor GetOrCreateAnchor(unsigned TAG, const char *Name);
467 /// getCastToEmpty - Return the descriptor as a Constant* with type '{}*'.
468 Constant *getCastToEmpty(DIDescriptor D);
471 /// Finds the stoppoint coressponding to this instruction, that is the
472 /// stoppoint that dominates this instruction
473 const DbgStopPointInst *findStopPoint(const Instruction *Inst);
475 /// Finds the stoppoint corresponding to first real (non-debug intrinsic)
476 /// instruction in this Basic Block, and returns the stoppoint for it.
477 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB);
479 /// Finds the dbg.declare intrinsic corresponding to this value if any.
480 /// It looks through pointer casts too.
481 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts = true);
482 } // end namespace llvm