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 Version6 = 6 << 16, // Current version of debug information.
37 Version5 = 5 << 16, // Constant for version 5.
38 Version4 = 4 << 16, // Constant for version 4.
39 VersionMask = 0xffff0000 // Mask for version number.
45 /// DIDescriptor constructor. If the specified GV is non-null, this checks
46 /// to make sure that the tag in the descriptor matches 'RequiredTag'. If
47 /// not, the debug info is corrupt and we ignore it.
48 DIDescriptor(GlobalVariable *GV, unsigned RequiredTag);
50 unsigned getTag() const {
51 return getUnsignedField(0) & ~VersionMask;
53 unsigned getVersion() const {
54 return getUnsignedField(0) & VersionMask;
57 std::string getStringField(unsigned Elt) const;
58 unsigned getUnsignedField(unsigned Elt) const {
59 return (unsigned)getUInt64Field(Elt);
61 uint64_t getUInt64Field(unsigned Elt) const;
62 DIDescriptor getDescriptorField(unsigned Elt) const;
64 template <typename DescTy>
65 DescTy getFieldAs(unsigned Elt) const {
66 return DescTy(getDescriptorField(Elt).getGV());
69 GlobalVariable *getGlobalVariableField(unsigned Elt) const;
72 explicit DIDescriptor() : GV(0) {}
73 explicit DIDescriptor(GlobalVariable *gv) : GV(gv) {}
75 bool isNull() const { return GV == 0; }
77 GlobalVariable *getGV() const { return GV; }
80 /// DIAnchor - A wrapper for various anchor descriptors.
81 class DIAnchor : public DIDescriptor {
83 explicit DIAnchor(GlobalVariable *GV = 0);
85 unsigned getAnchorTag() const { return getUnsignedField(1); }
88 /// DIArray - This descriptor holds an array of descriptors.
89 class DIArray : public DIDescriptor {
91 explicit DIArray(GlobalVariable *GV = 0) : DIDescriptor(GV) {}
93 unsigned getNumElements() const;
94 DIDescriptor getElement(unsigned Idx) const;
97 /// DICompileUnit - A wrapper for a compile unit.
98 class DICompileUnit : public DIDescriptor {
100 explicit DICompileUnit(GlobalVariable *GV = 0);
102 unsigned getLanguage() const { return getUnsignedField(2); }
103 std::string getFilename() const { return getStringField(3); }
104 std::string getDirectory() const { return getStringField(4); }
105 std::string getProducer() const { return getStringField(5); }
108 /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
109 /// FIXME: it seems strange that this doesn't have either a reference to the
110 /// type/precision or a file/line pair for location info.
111 class DIEnumerator : public DIDescriptor {
113 explicit DIEnumerator(GlobalVariable *GV = 0);
115 std::string getName() const { return getStringField(1); }
116 uint64_t getLanguage() const { return getUInt64Field(2); }
119 /// DISubrange - This is used to represent ranges, for array bounds.
120 class DISubrange : public DIDescriptor {
122 explicit DISubrange(GlobalVariable *GV = 0);
124 int64_t getLo() const { return (int64_t)getUInt64Field(1); }
125 int64_t getHi() const { return (int64_t)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 {
133 DIType(GlobalVariable *GV, unsigned Tag) : DIDescriptor(GV, Tag) {}
134 // This ctor is used when the Tag has already been validated by a derived
136 DIType(GlobalVariable *GV, bool, bool) : DIDescriptor(GV) {}
138 explicit DIType(GlobalVariable *GV);
141 DIDescriptor getContext() const { return getDescriptorField(1); }
142 std::string getName() const { return getStringField(2); }
143 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
144 unsigned getLineNumber() const { return getUnsignedField(4); }
145 uint64_t getSizeInBits() const { return getUInt64Field(5); }
146 uint64_t getAlignInBits() const { return getUInt64Field(6); }
147 // FIXME: Offset is only used for DW_TAG_member nodes. Making every type
148 // carry this is just plain insane.
149 uint64_t getOffsetInBits() const { return getUInt64Field(7); }
150 unsigned getFlags() const { return getUnsignedField(8); }
153 /// DIBasicType - A basic type, like 'int' or 'float'.
154 class DIBasicType : public DIType {
156 explicit DIBasicType(GlobalVariable *GV);
158 unsigned getEncoding() const { return getUnsignedField(9); }
161 /// DIDerivedType - A simple derived type, like a const qualified type,
162 /// a typedef, a pointer or reference, etc.
163 class DIDerivedType : public DIType {
165 explicit DIDerivedType(GlobalVariable *GV, bool, bool)
166 : DIType(GV, true, true) {}
168 explicit DIDerivedType(GlobalVariable *GV);
170 DIType getTypeDerivedFrom() const { return getFieldAs<DIType>(9); }
172 /// isDerivedType - Return true if the specified tag is legal for
174 static bool isDerivedType(unsigned TAG);
178 /// DICompositeType - This descriptor holds a type that can refer to multiple
179 /// other types, like a function or struct.
180 /// FIXME: Why is this a DIDerivedType??
181 class DICompositeType : public DIDerivedType {
183 explicit DICompositeType(GlobalVariable *GV);
185 DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
187 /// isCompositeType - Return true if the specified tag is legal for
189 static bool isCompositeType(unsigned TAG);
192 /// DIGlobal - This is a common class for global variables and subprograms.
193 class DIGlobal : public DIDescriptor {
195 explicit DIGlobal(GlobalVariable *GV, unsigned RequiredTag)
196 : DIDescriptor(GV, RequiredTag) {}
199 DIDescriptor getContext() const { return getDescriptorField(2); }
200 std::string getName() const { return getStringField(3); }
201 std::string getDisplayName() const { return getStringField(4); }
202 std::string getLinkageName() const { return getStringField(5); }
204 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(6); }
205 unsigned getLineNumber() const { return getUnsignedField(7); }
206 DIType getType() const { return getFieldAs<DIType>(8); }
208 /// isLocalToUnit - Return true if this subprogram is local to the current
209 /// compile unit, like 'static' in C.
210 unsigned isLocalToUnit() const { return getUnsignedField(9); }
211 unsigned isDefinition() const { return getUnsignedField(10); }
215 /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
216 class DISubprogram : public DIGlobal {
218 explicit DISubprogram(GlobalVariable *GV = 0);
222 /// DIGlobalVariable - This is a wrapper for a global variable.
223 class DIGlobalVariable : public DIGlobal {
225 explicit DIGlobalVariable(GlobalVariable *GV = 0);
227 GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
231 /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
233 class DIVariable : public DIDescriptor {
235 explicit DIVariable(GlobalVariable *GV = 0);
237 DIDescriptor getContext() const { return getDescriptorField(1); }
238 std::string getName() const { return getStringField(2); }
240 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
241 unsigned getLineNumber() const { return getUnsignedField(4); }
242 DIType getType() const { return getFieldAs<DIType>(5); }
244 /// isVariable - Return true if the specified tag is legal for DIVariable.
245 static bool isVariable(unsigned Tag);
249 /// DIBlock - This is a wrapper for a block (e.g. a function, scope, etc).
250 class DIBlock : public DIDescriptor {
252 explicit DIBlock(GlobalVariable *GV = 0);
254 DIDescriptor getContext() const { return getDescriptorField(1); }
257 /// DIFactory - This object assists with the construction of the various
261 // Cached values for uniquing and faster lookups.
262 DIAnchor CompileUnitAnchor, SubProgramAnchor, GlobalVariableAnchor;
263 const Type *EmptyStructPtr; // "{}*".
264 Function *StopPointFn; // llvm.dbg.stoppoint
265 Function *FuncStartFn; // llvm.dbg.func.start
266 Function *RegionStartFn; // llvm.dbg.region.start
267 Function *RegionEndFn; // llvm.dbg.region.end
268 Function *DeclareFn; // llvm.dbg.declare
269 StringMap<Constant*> StringCache;
270 DenseMap<Constant*, DIDescriptor> SimpleConstantCache;
272 DIFactory(const DIFactory &); // DO NOT IMPLEMENT
273 void operator=(const DIFactory&); // DO NOT IMPLEMENT
275 explicit DIFactory(Module &m);
277 /// GetOrCreateCompileUnitAnchor - Return the anchor for compile units,
278 /// creating a new one if there isn't already one in the module.
279 DIAnchor GetOrCreateCompileUnitAnchor();
281 /// GetOrCreateSubprogramAnchor - Return the anchor for subprograms,
282 /// creating a new one if there isn't already one in the module.
283 DIAnchor GetOrCreateSubprogramAnchor();
285 /// GetOrCreateGlobalVariableAnchor - Return the anchor for globals,
286 /// creating a new one if there isn't already one in the module.
287 DIAnchor GetOrCreateGlobalVariableAnchor();
289 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
290 /// This implicitly uniques the arrays created.
291 DIArray GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys);
293 /// GetOrCreateSubrange - Create a descriptor for a value range. This
294 /// implicitly uniques the values returned.
295 DISubrange GetOrCreateSubrange(int64_t Lo, int64_t Hi);
298 /// CreateCompileUnit - Create a new descriptor for the specified compile
300 DICompileUnit CreateCompileUnit(unsigned LangID,
301 const std::string &Filename,
302 const std::string &Directory,
303 const std::string &Producer);
305 /// CreateEnumerator - Create a single enumerator value.
306 DIEnumerator CreateEnumerator(const std::string &Name, uint64_t Val);
308 /// CreateBasicType - Create a basic type like int, float, etc.
309 DIBasicType CreateBasicType(DIDescriptor Context, const std::string &Name,
310 DICompileUnit CompileUnit, unsigned LineNumber,
311 uint64_t SizeInBits, uint64_t AlignInBits,
312 uint64_t OffsetInBits, unsigned Flags,
315 /// CreateDerivedType - Create a derived type like const qualified type,
316 /// pointer, typedef, etc.
317 DIDerivedType CreateDerivedType(unsigned Tag, DIDescriptor Context,
318 const std::string &Name,
319 DICompileUnit CompileUnit,
321 uint64_t SizeInBits, uint64_t AlignInBits,
322 uint64_t OffsetInBits, unsigned Flags,
325 /// CreateCompositeType - Create a composite type like array, struct, etc.
326 DICompositeType CreateCompositeType(unsigned Tag, DIDescriptor Context,
327 const std::string &Name,
328 DICompileUnit CompileUnit,
331 uint64_t AlignInBits,
332 uint64_t OffsetInBits, unsigned Flags,
336 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
337 /// See comments in DISubprogram for descriptions of these fields.
338 DISubprogram CreateSubprogram(DIDescriptor Context, const std::string &Name,
339 const std::string &DisplayName,
340 const std::string &LinkageName,
341 DICompileUnit CompileUnit, unsigned LineNo,
342 DIType Type, bool isLocalToUnit,
345 /// CreateGlobalVariable - Create a new descriptor for the specified global.
347 CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
348 const std::string &DisplayName,
349 const std::string &LinkageName,
350 DICompileUnit CompileUnit,
351 unsigned LineNo, DIType Type, bool isLocalToUnit,
352 bool isDefinition, llvm::GlobalVariable *GV);
355 /// CreateVariable - Create a new descriptor for the specified variable.
356 DIVariable CreateVariable(unsigned Tag, DIDescriptor Context,
357 const std::string &Name,
358 DICompileUnit CompileUnit, unsigned LineNo,
361 /// CreateBlock - This creates a descriptor for a lexical block with the
362 /// specified parent context.
363 DIBlock CreateBlock(DIDescriptor Context);
365 /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
366 /// inserting it at the end of the specified basic block.
367 void InsertStopPoint(DICompileUnit CU, unsigned LineNo, unsigned ColNo,
370 /// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
371 /// mark the start of the specified subprogram.
372 void InsertSubprogramStart(DISubprogram SP, BasicBlock *BB);
374 /// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
375 /// mark the start of a region for the specified scoping descriptor.
376 void InsertRegionStart(DIDescriptor D, BasicBlock *BB);
378 /// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
379 /// mark the end of a region for the specified scoping descriptor.
380 void InsertRegionEnd(DIDescriptor D, BasicBlock *BB);
382 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
383 void InsertDeclare(llvm::Value *Storage, DIVariable D, BasicBlock *BB);
386 Constant *GetTagConstant(unsigned TAG);
387 Constant *GetStringConstant(const std::string &String);
388 DIAnchor GetOrCreateAnchor(unsigned TAG, const char *Name);
390 /// getCastToEmpty - Return the descriptor as a Constant* with type '{}*'.
391 Constant *getCastToEmpty(DIDescriptor D);
394 /// Finds the stoppoint coressponding to this instruction, that is the
395 /// stoppoint that dominates this instruction
396 const DbgStopPointInst *findStopPoint(const Instruction *Inst);
398 /// Finds the stoppoint corresponding to first real (non-debug intrinsic)
399 /// instruction in this Basic Block, and returns the stoppoint for it.
400 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB);
402 /// Finds the dbg.declare intrinsic corresponding to this value if any.
403 /// It looks through pointer casts too.
404 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts = true);
405 } // end namespace llvm