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 unsigned getVersion() const {
52 return getUnsignedField(0) & VersionMask;
55 std::string getStringField(unsigned Elt) const;
56 unsigned getUnsignedField(unsigned Elt) const {
57 return (unsigned)getUInt64Field(Elt);
59 uint64_t getUInt64Field(unsigned Elt) const;
60 DIDescriptor getDescriptorField(unsigned Elt) const;
62 template <typename DescTy>
63 DescTy getFieldAs(unsigned Elt) const {
64 return DescTy(getDescriptorField(Elt).getGV());
67 GlobalVariable *getGlobalVariableField(unsigned Elt) const;
70 explicit DIDescriptor() : GV(0) {}
71 explicit DIDescriptor(GlobalVariable *gv) : GV(gv) {}
73 bool isNull() const { return GV == 0; }
75 GlobalVariable *getGV() const { return GV; }
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);
151 DIDescriptor getContext() const { return getDescriptorField(1); }
152 std::string getName() const { return getStringField(2); }
153 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
154 unsigned getLineNumber() const { return getUnsignedField(4); }
155 uint64_t getSizeInBits() const { return getUInt64Field(5); }
156 uint64_t getAlignInBits() const { return getUInt64Field(6); }
157 // FIXME: Offset is only used for DW_TAG_member nodes. Making every type
158 // carry this is just plain insane.
159 uint64_t getOffsetInBits() const { return getUInt64Field(7); }
160 unsigned getFlags() const { return getUnsignedField(8); }
163 /// DIBasicType - A basic type, like 'int' or 'float'.
164 class DIBasicType : public DIType {
166 explicit DIBasicType(GlobalVariable *GV);
168 unsigned getEncoding() const { return getUnsignedField(9); }
169 std::string getFileName() const { return getStringField(10); }
170 std::string getDirectory() const { return getStringField(11); }
173 /// DIDerivedType - A simple derived type, like a const qualified type,
174 /// a typedef, a pointer or reference, etc.
175 class DIDerivedType : public DIType {
177 explicit DIDerivedType(GlobalVariable *GV, bool, bool)
178 : DIType(GV, true, true) {}
180 explicit DIDerivedType(GlobalVariable *GV);
182 DIType getTypeDerivedFrom() const { return getFieldAs<DIType>(9); }
183 std::string getFileName() const { return getStringField(10); }
184 std::string getDirectory() const { return getStringField(11); }
186 /// isDerivedType - Return true if the specified tag is legal for
188 static bool isDerivedType(unsigned TAG);
190 static inline bool classof(const DIDerivedType *) { return true; }
191 static inline bool classof(const DIDescriptor *D) {
192 return isDerivedType(D->getTag());
197 /// DICompositeType - This descriptor holds a type that can refer to multiple
198 /// other types, like a function or struct.
199 /// FIXME: Why is this a DIDerivedType??
200 class DICompositeType : public DIDerivedType {
202 explicit DICompositeType(GlobalVariable *GV);
204 DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
205 std::string getFileName() const { return getStringField(11); }
206 std::string getDirectory() const { return getStringField(12); }
208 /// isCompositeType - Return true if the specified tag is legal for
210 static bool isCompositeType(unsigned TAG);
211 static inline bool classof(const DIDerivedType *) { return true; }
212 static inline bool classof(const DIDescriptor *D) {
213 return isCompositeType(D->getTag());
217 /// DIGlobal - This is a common class for global variables and subprograms.
218 class DIGlobal : public DIDescriptor {
220 explicit DIGlobal(GlobalVariable *GV, unsigned RequiredTag)
221 : DIDescriptor(GV, RequiredTag) {}
224 DIDescriptor getContext() const { return getDescriptorField(2); }
225 std::string getName() const { return getStringField(3); }
226 std::string getDisplayName() const { return getStringField(4); }
227 std::string getLinkageName() const { return getStringField(5); }
229 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(6); }
230 unsigned getLineNumber() const { return getUnsignedField(7); }
231 DIType getType() const { return getFieldAs<DIType>(8); }
233 /// isLocalToUnit - Return true if this subprogram is local to the current
234 /// compile unit, like 'static' in C.
235 unsigned isLocalToUnit() const { return getUnsignedField(9); }
236 unsigned isDefinition() const { return getUnsignedField(10); }
240 /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
241 class DISubprogram : public DIGlobal {
243 explicit DISubprogram(GlobalVariable *GV = 0);
244 std::string getFilename() const { return getStringField(11); }
245 std::string getDirectory() const { return getStringField(12); }
246 DICompositeType getType() const { return getFieldAs<DICompositeType>(8); }
249 /// DIGlobalVariable - This is a wrapper for a global variable.
250 class DIGlobalVariable : public DIGlobal {
252 explicit DIGlobalVariable(GlobalVariable *GV = 0);
254 GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
255 std::string getFilename() const { return getStringField(12); }
256 std::string getDirectory() const { return getStringField(13); }
260 /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
262 class DIVariable : public DIDescriptor {
264 explicit DIVariable(GlobalVariable *GV = 0);
266 DIDescriptor getContext() const { return getDescriptorField(1); }
267 std::string getName() const { return getStringField(2); }
269 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
270 unsigned getLineNumber() const { return getUnsignedField(4); }
271 DIType getType() const { return getFieldAs<DIType>(5); }
272 std::string getFilename() const { return getStringField(6); }
273 std::string getDirectory() const { return getStringField(7); }
275 /// isVariable - Return true if the specified tag is legal for DIVariable.
276 static bool isVariable(unsigned Tag);
280 /// DIBlock - This is a wrapper for a block (e.g. a function, scope, etc).
281 class DIBlock : public DIDescriptor {
283 explicit DIBlock(GlobalVariable *GV = 0);
285 DIDescriptor getContext() const { return getDescriptorField(1); }
288 /// DIFactory - This object assists with the construction of the various
292 // Cached values for uniquing and faster lookups.
293 DIAnchor CompileUnitAnchor, SubProgramAnchor, GlobalVariableAnchor;
294 const Type *EmptyStructPtr; // "{}*".
295 Function *StopPointFn; // llvm.dbg.stoppoint
296 Function *FuncStartFn; // llvm.dbg.func.start
297 Function *RegionStartFn; // llvm.dbg.region.start
298 Function *RegionEndFn; // llvm.dbg.region.end
299 Function *DeclareFn; // llvm.dbg.declare
300 StringMap<Constant*> StringCache;
301 DenseMap<Constant*, DIDescriptor> SimpleConstantCache;
303 DIFactory(const DIFactory &); // DO NOT IMPLEMENT
304 void operator=(const DIFactory&); // DO NOT IMPLEMENT
306 explicit DIFactory(Module &m);
308 /// GetOrCreateCompileUnitAnchor - Return the anchor for compile units,
309 /// creating a new one if there isn't already one in the module.
310 DIAnchor GetOrCreateCompileUnitAnchor();
312 /// GetOrCreateSubprogramAnchor - Return the anchor for subprograms,
313 /// creating a new one if there isn't already one in the module.
314 DIAnchor GetOrCreateSubprogramAnchor();
316 /// GetOrCreateGlobalVariableAnchor - Return the anchor for globals,
317 /// creating a new one if there isn't already one in the module.
318 DIAnchor GetOrCreateGlobalVariableAnchor();
320 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
321 /// This implicitly uniques the arrays created.
322 DIArray GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys);
324 /// GetOrCreateSubrange - Create a descriptor for a value range. This
325 /// implicitly uniques the values returned.
326 DISubrange GetOrCreateSubrange(int64_t Lo, int64_t Hi);
329 /// CreateCompileUnit - Create a new descriptor for the specified compile
331 DICompileUnit CreateCompileUnit(unsigned LangID,
332 const std::string &Filename,
333 const std::string &Directory,
334 const std::string &Producer);
336 /// CreateEnumerator - Create a single enumerator value.
337 DIEnumerator CreateEnumerator(const std::string &Name, uint64_t Val);
339 /// CreateBasicType - Create a basic type like int, float, etc.
340 DIBasicType CreateBasicType(DIDescriptor Context, const std::string &Name,
341 DICompileUnit CompileUnit, unsigned LineNumber,
342 uint64_t SizeInBits, uint64_t AlignInBits,
343 uint64_t OffsetInBits, unsigned Flags,
345 const std::string *FileName = 0,
346 const std::string *Directory = 0);
348 /// CreateDerivedType - Create a derived type like const qualified type,
349 /// pointer, typedef, etc.
350 DIDerivedType CreateDerivedType(unsigned Tag, DIDescriptor Context,
351 const std::string &Name,
352 DICompileUnit CompileUnit,
354 uint64_t SizeInBits, uint64_t AlignInBits,
355 uint64_t OffsetInBits, unsigned Flags,
357 const std::string *FileName = 0,
358 const std::string *Directory = 0);
360 /// CreateCompositeType - Create a composite type like array, struct, etc.
361 DICompositeType CreateCompositeType(unsigned Tag, DIDescriptor Context,
362 const std::string &Name,
363 DICompileUnit CompileUnit,
366 uint64_t AlignInBits,
367 uint64_t OffsetInBits, unsigned Flags,
370 const std::string *FileName = 0,
371 const std::string *Directory = 0);
373 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
374 /// See comments in DISubprogram for descriptions of these fields.
375 DISubprogram CreateSubprogram(DIDescriptor Context, const std::string &Name,
376 const std::string &DisplayName,
377 const std::string &LinkageName,
378 DICompileUnit CompileUnit, unsigned LineNo,
379 DIType Type, bool isLocalToUnit,
381 const std::string *FileName = 0,
382 const std::string *Directory = 0);
384 /// CreateGlobalVariable - Create a new descriptor for the specified global.
386 CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
387 const std::string &DisplayName,
388 const std::string &LinkageName,
389 DICompileUnit CompileUnit,
390 unsigned LineNo, DIType Type, bool isLocalToUnit,
391 bool isDefinition, llvm::GlobalVariable *GV,
392 const std::string *FileName = 0,
393 const std::string *Directory = 0);
396 /// CreateVariable - Create a new descriptor for the specified variable.
397 DIVariable CreateVariable(unsigned Tag, DIDescriptor Context,
398 const std::string &Name,
399 DICompileUnit CompileUnit, unsigned LineNo,
401 const std::string *FileName = 0,
402 const std::string *Directory = 0);
404 /// CreateBlock - This creates a descriptor for a lexical block with the
405 /// specified parent context.
406 DIBlock CreateBlock(DIDescriptor Context);
408 /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
409 /// inserting it at the end of the specified basic block.
410 void InsertStopPoint(DICompileUnit CU, unsigned LineNo, unsigned ColNo,
413 /// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
414 /// mark the start of the specified subprogram.
415 void InsertSubprogramStart(DISubprogram SP, BasicBlock *BB);
417 /// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
418 /// mark the start of a region for the specified scoping descriptor.
419 void InsertRegionStart(DIDescriptor D, BasicBlock *BB);
421 /// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
422 /// mark the end of a region for the specified scoping descriptor.
423 void InsertRegionEnd(DIDescriptor D, BasicBlock *BB);
425 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
426 void InsertDeclare(llvm::Value *Storage, DIVariable D, BasicBlock *BB);
429 Constant *GetTagConstant(unsigned TAG);
430 Constant *GetStringConstant(const std::string &String);
431 DIAnchor GetOrCreateAnchor(unsigned TAG, const char *Name);
433 /// getCastToEmpty - Return the descriptor as a Constant* with type '{}*'.
434 Constant *getCastToEmpty(DIDescriptor D);
437 /// Finds the stoppoint coressponding to this instruction, that is the
438 /// stoppoint that dominates this instruction
439 const DbgStopPointInst *findStopPoint(const Instruction *Inst);
441 /// Finds the stoppoint corresponding to first real (non-debug intrinsic)
442 /// instruction in this Basic Block, and returns the stoppoint for it.
443 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB);
445 /// Finds the dbg.declare intrinsic corresponding to this value if any.
446 /// It looks through pointer casts too.
447 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts = true);
448 } // end namespace llvm