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;
82 /// DIAnchor - A wrapper for various anchor descriptors.
83 class DIAnchor : public DIDescriptor {
85 explicit DIAnchor(GlobalVariable *GV = 0);
87 unsigned getAnchorTag() const { return getUnsignedField(1); }
90 /// DIArray - This descriptor holds an array of descriptors.
91 class DIArray : public DIDescriptor {
93 explicit DIArray(GlobalVariable *GV = 0) : DIDescriptor(GV) {}
95 unsigned getNumElements() const;
96 DIDescriptor getElement(unsigned Idx) const;
99 /// DICompileUnit - A wrapper for a compile unit.
100 class DICompileUnit : public DIDescriptor {
102 explicit DICompileUnit(GlobalVariable *GV = 0);
104 unsigned getLanguage() const { return getUnsignedField(2); }
105 std::string getFilename() const { return getStringField(3); }
106 std::string getDirectory() const { return getStringField(4); }
107 std::string getProducer() const { return getStringField(5); }
110 /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
111 /// FIXME: it seems strange that this doesn't have either a reference to the
112 /// type/precision or a file/line pair for location info.
113 class DIEnumerator : public DIDescriptor {
115 explicit DIEnumerator(GlobalVariable *GV = 0);
117 std::string getName() const { return getStringField(1); }
118 uint64_t getLanguage() const { return getUInt64Field(2); }
121 /// DISubrange - This is used to represent ranges, for array bounds.
122 class DISubrange : public DIDescriptor {
124 explicit DISubrange(GlobalVariable *GV = 0);
126 int64_t getLo() const { return (int64_t)getUInt64Field(1); }
127 int64_t getHi() const { return (int64_t)getUInt64Field(2); }
130 /// DIType - This is a wrapper for a type.
131 /// FIXME: Types should be factored much better so that CV qualifiers and
132 /// others do not require a huge and empty descriptor full of zeros.
133 class DIType : public DIDescriptor {
135 DIType(GlobalVariable *GV, unsigned Tag) : DIDescriptor(GV, Tag) {}
136 // This ctor is used when the Tag has already been validated by a derived
138 DIType(GlobalVariable *GV, bool, bool) : DIDescriptor(GV) {}
140 explicit DIType(GlobalVariable *GV);
143 DIDescriptor getContext() const { return getDescriptorField(1); }
144 std::string getName() const { return getStringField(2); }
145 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
146 unsigned getLineNumber() const { return getUnsignedField(4); }
147 uint64_t getSizeInBits() const { return getUInt64Field(5); }
148 uint64_t getAlignInBits() const { return getUInt64Field(6); }
149 // FIXME: Offset is only used for DW_TAG_member nodes. Making every type
150 // carry this is just plain insane.
151 uint64_t getOffsetInBits() const { return getUInt64Field(7); }
152 unsigned getFlags() const { return getUnsignedField(8); }
155 /// DIBasicType - A basic type, like 'int' or 'float'.
156 class DIBasicType : public DIType {
158 explicit DIBasicType(GlobalVariable *GV);
160 unsigned getEncoding() const { return getUnsignedField(9); }
161 std::string getFileName() const { return getStringField(10); }
162 std::string getDirectory() const { return getStringField(11); }
165 /// DIDerivedType - A simple derived type, like a const qualified type,
166 /// a typedef, a pointer or reference, etc.
167 class DIDerivedType : public DIType {
169 explicit DIDerivedType(GlobalVariable *GV, bool, bool)
170 : DIType(GV, true, true) {}
172 explicit DIDerivedType(GlobalVariable *GV);
174 DIType getTypeDerivedFrom() const { return getFieldAs<DIType>(9); }
175 std::string getFileName() const { return getStringField(10); }
176 std::string getDirectory() const { return getStringField(11); }
178 /// isDerivedType - Return true if the specified tag is legal for
180 static bool isDerivedType(unsigned TAG);
184 /// DICompositeType - This descriptor holds a type that can refer to multiple
185 /// other types, like a function or struct.
186 /// FIXME: Why is this a DIDerivedType??
187 class DICompositeType : public DIDerivedType {
189 explicit DICompositeType(GlobalVariable *GV);
191 DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
192 std::string getFileName() const { return getStringField(11); }
193 std::string getDirectory() const { return getStringField(12); }
195 /// isCompositeType - Return true if the specified tag is legal for
197 static bool isCompositeType(unsigned TAG);
200 /// DIGlobal - This is a common class for global variables and subprograms.
201 class DIGlobal : public DIDescriptor {
203 explicit DIGlobal(GlobalVariable *GV, unsigned RequiredTag)
204 : DIDescriptor(GV, RequiredTag) {}
207 DIDescriptor getContext() const { return getDescriptorField(2); }
208 std::string getName() const { return getStringField(3); }
209 std::string getDisplayName() const { return getStringField(4); }
210 std::string getLinkageName() const { return getStringField(5); }
212 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(6); }
213 unsigned getLineNumber() const { return getUnsignedField(7); }
214 DIType getType() const { return getFieldAs<DIType>(8); }
216 /// isLocalToUnit - Return true if this subprogram is local to the current
217 /// compile unit, like 'static' in C.
218 unsigned isLocalToUnit() const { return getUnsignedField(9); }
219 unsigned isDefinition() const { return getUnsignedField(10); }
223 /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
224 class DISubprogram : public DIGlobal {
226 explicit DISubprogram(GlobalVariable *GV = 0);
227 std::string getFilename() const { return getStringField(11); }
228 std::string getDirectory() const { return getStringField(12); }
231 /// DIGlobalVariable - This is a wrapper for a global variable.
232 class DIGlobalVariable : public DIGlobal {
234 explicit DIGlobalVariable(GlobalVariable *GV = 0);
236 GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
237 std::string getFilename() const { return getStringField(12); }
238 std::string getDirectory() const { return getStringField(13); }
242 /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
244 class DIVariable : public DIDescriptor {
246 explicit DIVariable(GlobalVariable *GV = 0);
248 DIDescriptor getContext() const { return getDescriptorField(1); }
249 std::string getName() const { return getStringField(2); }
251 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
252 unsigned getLineNumber() const { return getUnsignedField(4); }
253 DIType getType() const { return getFieldAs<DIType>(5); }
254 std::string getFilename() const { return getStringField(6); }
255 std::string getDirectory() const { return getStringField(7); }
257 /// isVariable - Return true if the specified tag is legal for DIVariable.
258 static bool isVariable(unsigned Tag);
262 /// DIBlock - This is a wrapper for a block (e.g. a function, scope, etc).
263 class DIBlock : public DIDescriptor {
265 explicit DIBlock(GlobalVariable *GV = 0);
267 DIDescriptor getContext() const { return getDescriptorField(1); }
270 /// DIFactory - This object assists with the construction of the various
274 // Cached values for uniquing and faster lookups.
275 DIAnchor CompileUnitAnchor, SubProgramAnchor, GlobalVariableAnchor;
276 const Type *EmptyStructPtr; // "{}*".
277 Function *StopPointFn; // llvm.dbg.stoppoint
278 Function *FuncStartFn; // llvm.dbg.func.start
279 Function *RegionStartFn; // llvm.dbg.region.start
280 Function *RegionEndFn; // llvm.dbg.region.end
281 Function *DeclareFn; // llvm.dbg.declare
282 StringMap<Constant*> StringCache;
283 DenseMap<Constant*, DIDescriptor> SimpleConstantCache;
285 DIFactory(const DIFactory &); // DO NOT IMPLEMENT
286 void operator=(const DIFactory&); // DO NOT IMPLEMENT
288 explicit DIFactory(Module &m);
290 /// GetOrCreateCompileUnitAnchor - Return the anchor for compile units,
291 /// creating a new one if there isn't already one in the module.
292 DIAnchor GetOrCreateCompileUnitAnchor();
294 /// GetOrCreateSubprogramAnchor - Return the anchor for subprograms,
295 /// creating a new one if there isn't already one in the module.
296 DIAnchor GetOrCreateSubprogramAnchor();
298 /// GetOrCreateGlobalVariableAnchor - Return the anchor for globals,
299 /// creating a new one if there isn't already one in the module.
300 DIAnchor GetOrCreateGlobalVariableAnchor();
302 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
303 /// This implicitly uniques the arrays created.
304 DIArray GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys);
306 /// GetOrCreateSubrange - Create a descriptor for a value range. This
307 /// implicitly uniques the values returned.
308 DISubrange GetOrCreateSubrange(int64_t Lo, int64_t Hi);
311 /// CreateCompileUnit - Create a new descriptor for the specified compile
313 DICompileUnit CreateCompileUnit(unsigned LangID,
314 const std::string &Filename,
315 const std::string &Directory,
316 const std::string &Producer);
318 /// CreateEnumerator - Create a single enumerator value.
319 DIEnumerator CreateEnumerator(const std::string &Name, uint64_t Val);
321 /// CreateBasicType - Create a basic type like int, float, etc.
322 DIBasicType CreateBasicType(DIDescriptor Context, const std::string &Name,
323 DICompileUnit CompileUnit, unsigned LineNumber,
324 uint64_t SizeInBits, uint64_t AlignInBits,
325 uint64_t OffsetInBits, unsigned Flags,
327 const std::string *FileName = 0,
328 const std::string *Directory = 0);
330 /// CreateDerivedType - Create a derived type like const qualified type,
331 /// pointer, typedef, etc.
332 DIDerivedType CreateDerivedType(unsigned Tag, DIDescriptor Context,
333 const std::string &Name,
334 DICompileUnit CompileUnit,
336 uint64_t SizeInBits, uint64_t AlignInBits,
337 uint64_t OffsetInBits, unsigned Flags,
339 const std::string *FileName = 0,
340 const std::string *Directory = 0);
342 /// CreateCompositeType - Create a composite type like array, struct, etc.
343 DICompositeType CreateCompositeType(unsigned Tag, DIDescriptor Context,
344 const std::string &Name,
345 DICompileUnit CompileUnit,
348 uint64_t AlignInBits,
349 uint64_t OffsetInBits, unsigned Flags,
352 const std::string *FileName = 0,
353 const std::string *Directory = 0);
355 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
356 /// See comments in DISubprogram for descriptions of these fields.
357 DISubprogram CreateSubprogram(DIDescriptor Context, const std::string &Name,
358 const std::string &DisplayName,
359 const std::string &LinkageName,
360 DICompileUnit CompileUnit, unsigned LineNo,
361 DIType Type, bool isLocalToUnit,
363 const std::string *FileName = 0,
364 const std::string *Directory = 0);
366 /// CreateGlobalVariable - Create a new descriptor for the specified global.
368 CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
369 const std::string &DisplayName,
370 const std::string &LinkageName,
371 DICompileUnit CompileUnit,
372 unsigned LineNo, DIType Type, bool isLocalToUnit,
373 bool isDefinition, llvm::GlobalVariable *GV,
374 const std::string *FileName = 0,
375 const std::string *Directory = 0);
378 /// CreateVariable - Create a new descriptor for the specified variable.
379 DIVariable CreateVariable(unsigned Tag, DIDescriptor Context,
380 const std::string &Name,
381 DICompileUnit CompileUnit, unsigned LineNo,
383 const std::string *FileName = 0,
384 const std::string *Directory = 0);
386 /// CreateBlock - This creates a descriptor for a lexical block with the
387 /// specified parent context.
388 DIBlock CreateBlock(DIDescriptor Context);
390 /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
391 /// inserting it at the end of the specified basic block.
392 void InsertStopPoint(DICompileUnit CU, unsigned LineNo, unsigned ColNo,
395 /// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
396 /// mark the start of the specified subprogram.
397 void InsertSubprogramStart(DISubprogram SP, BasicBlock *BB);
399 /// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
400 /// mark the start of a region for the specified scoping descriptor.
401 void InsertRegionStart(DIDescriptor D, BasicBlock *BB);
403 /// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
404 /// mark the end of a region for the specified scoping descriptor.
405 void InsertRegionEnd(DIDescriptor D, BasicBlock *BB);
407 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
408 void InsertDeclare(llvm::Value *Storage, DIVariable D, BasicBlock *BB);
411 Constant *GetTagConstant(unsigned TAG);
412 Constant *GetStringConstant(const std::string &String);
413 DIAnchor GetOrCreateAnchor(unsigned TAG, const char *Name);
415 /// getCastToEmpty - Return the descriptor as a Constant* with type '{}*'.
416 Constant *getCastToEmpty(DIDescriptor D);
419 /// Finds the stoppoint coressponding to this instruction, that is the
420 /// stoppoint that dominates this instruction
421 const DbgStopPointInst *findStopPoint(const Instruction *Inst);
423 /// Finds the stoppoint corresponding to first real (non-debug intrinsic)
424 /// instruction in this Basic Block, and returns the stoppoint for it.
425 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB);
427 /// Finds the dbg.declare intrinsic corresponding to this value if any.
428 /// It looks through pointer casts too.
429 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts = true);
430 } // end namespace llvm