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 getTag() const {
52 return getUnsignedField(0) & ~VersionMask;
54 unsigned getVersion() const {
55 return getUnsignedField(0) & VersionMask;
58 std::string getStringField(unsigned Elt) const;
59 unsigned getUnsignedField(unsigned Elt) const {
60 return (unsigned)getUInt64Field(Elt);
62 uint64_t getUInt64Field(unsigned Elt) const;
63 DIDescriptor getDescriptorField(unsigned Elt) const;
65 template <typename DescTy>
66 DescTy getFieldAs(unsigned Elt) const {
67 return DescTy(getDescriptorField(Elt).getGV());
70 GlobalVariable *getGlobalVariableField(unsigned Elt) const;
73 explicit DIDescriptor() : GV(0) {}
74 explicit DIDescriptor(GlobalVariable *gv) : GV(gv) {}
76 bool isNull() const { return GV == 0; }
78 GlobalVariable *getGV() const { return GV; }
81 /// DIAnchor - A wrapper for various anchor descriptors.
82 class DIAnchor : public DIDescriptor {
84 explicit DIAnchor(GlobalVariable *GV = 0);
86 unsigned getAnchorTag() const { return getUnsignedField(1); }
89 /// DIArray - This descriptor holds an array of descriptors.
90 class DIArray : public DIDescriptor {
92 explicit DIArray(GlobalVariable *GV = 0) : DIDescriptor(GV) {}
94 unsigned getNumElements() const;
95 DIDescriptor getElement(unsigned Idx) const;
98 /// DICompileUnit - A wrapper for a compile unit.
99 class DICompileUnit : public DIDescriptor {
101 explicit DICompileUnit(GlobalVariable *GV = 0);
103 unsigned getLanguage() const { return getUnsignedField(2); }
104 std::string getFilename() const { return getStringField(3); }
105 std::string getDirectory() const { return getStringField(4); }
106 std::string getProducer() const { return getStringField(5); }
109 /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
110 /// FIXME: it seems strange that this doesn't have either a reference to the
111 /// type/precision or a file/line pair for location info.
112 class DIEnumerator : public DIDescriptor {
114 explicit DIEnumerator(GlobalVariable *GV = 0);
116 std::string getName() const { return getStringField(1); }
117 uint64_t getLanguage() const { return getUInt64Field(2); }
120 /// DISubrange - This is used to represent ranges, for array bounds.
121 class DISubrange : public DIDescriptor {
123 explicit DISubrange(GlobalVariable *GV = 0);
125 int64_t getLo() const { return (int64_t)getUInt64Field(1); }
126 int64_t getHi() const { return (int64_t)getUInt64Field(2); }
129 /// DIType - This is a wrapper for a type.
130 /// FIXME: Types should be factored much better so that CV qualifiers and
131 /// others do not require a huge and empty descriptor full of zeros.
132 class DIType : public DIDescriptor {
134 DIType(GlobalVariable *GV, unsigned Tag) : DIDescriptor(GV, Tag) {}
135 // This ctor is used when the Tag has already been validated by a derived
137 DIType(GlobalVariable *GV, bool, bool) : DIDescriptor(GV) {}
139 explicit DIType(GlobalVariable *GV);
142 DIDescriptor getContext() const { return getDescriptorField(1); }
143 std::string getName() const { return getStringField(2); }
144 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
145 unsigned getLineNumber() const { return getUnsignedField(4); }
146 uint64_t getSizeInBits() const { return getUInt64Field(5); }
147 uint64_t getAlignInBits() const { return getUInt64Field(6); }
148 // FIXME: Offset is only used for DW_TAG_member nodes. Making every type
149 // carry this is just plain insane.
150 uint64_t getOffsetInBits() const { return getUInt64Field(7); }
151 unsigned getFlags() const { return getUnsignedField(8); }
154 /// DIBasicType - A basic type, like 'int' or 'float'.
155 class DIBasicType : public DIType {
157 explicit DIBasicType(GlobalVariable *GV);
159 unsigned getEncoding() const { return getUnsignedField(9); }
160 std::string getFileName() const { return getStringField(10); }
161 std::string getDirectory() const { return getStringField(11); }
164 /// DIDerivedType - A simple derived type, like a const qualified type,
165 /// a typedef, a pointer or reference, etc.
166 class DIDerivedType : public DIType {
168 explicit DIDerivedType(GlobalVariable *GV, bool, bool)
169 : DIType(GV, true, true) {}
171 explicit DIDerivedType(GlobalVariable *GV);
173 DIType getTypeDerivedFrom() const { return getFieldAs<DIType>(9); }
174 std::string getFileName() const { return getStringField(10); }
175 std::string getDirectory() const { return getStringField(11); }
177 /// isDerivedType - Return true if the specified tag is legal for
179 static bool isDerivedType(unsigned TAG);
183 /// DICompositeType - This descriptor holds a type that can refer to multiple
184 /// other types, like a function or struct.
185 /// FIXME: Why is this a DIDerivedType??
186 class DICompositeType : public DIDerivedType {
188 explicit DICompositeType(GlobalVariable *GV);
190 DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
191 std::string getFileName() const { return getStringField(11); }
192 std::string getDirectory() const { return getStringField(12); }
194 /// isCompositeType - Return true if the specified tag is legal for
196 static bool isCompositeType(unsigned TAG);
199 /// DIGlobal - This is a common class for global variables and subprograms.
200 class DIGlobal : public DIDescriptor {
202 explicit DIGlobal(GlobalVariable *GV, unsigned RequiredTag)
203 : DIDescriptor(GV, RequiredTag) {}
206 DIDescriptor getContext() const { return getDescriptorField(2); }
207 std::string getName() const { return getStringField(3); }
208 std::string getDisplayName() const { return getStringField(4); }
209 std::string getLinkageName() const { return getStringField(5); }
211 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(6); }
212 unsigned getLineNumber() const { return getUnsignedField(7); }
213 DIType getType() const { return getFieldAs<DIType>(8); }
215 /// isLocalToUnit - Return true if this subprogram is local to the current
216 /// compile unit, like 'static' in C.
217 unsigned isLocalToUnit() const { return getUnsignedField(9); }
218 unsigned isDefinition() const { return getUnsignedField(10); }
222 /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
223 class DISubprogram : public DIGlobal {
225 explicit DISubprogram(GlobalVariable *GV = 0);
226 std::string getFilename() const { return getStringField(11); }
227 std::string getDirectory() const { return getStringField(12); }
230 /// DIGlobalVariable - This is a wrapper for a global variable.
231 class DIGlobalVariable : public DIGlobal {
233 explicit DIGlobalVariable(GlobalVariable *GV = 0);
235 GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
236 std::string getFilename() const { return getStringField(12); }
237 std::string getDirectory() const { return getStringField(13); }
241 /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
243 class DIVariable : public DIDescriptor {
245 explicit DIVariable(GlobalVariable *GV = 0);
247 DIDescriptor getContext() const { return getDescriptorField(1); }
248 std::string getName() const { return getStringField(2); }
250 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
251 unsigned getLineNumber() const { return getUnsignedField(4); }
252 DIType getType() const { return getFieldAs<DIType>(5); }
253 std::string getFilename() const { return getStringField(6); }
254 std::string getDirectory() const { return getStringField(7); }
256 /// isVariable - Return true if the specified tag is legal for DIVariable.
257 static bool isVariable(unsigned Tag);
261 /// DIBlock - This is a wrapper for a block (e.g. a function, scope, etc).
262 class DIBlock : public DIDescriptor {
264 explicit DIBlock(GlobalVariable *GV = 0);
266 DIDescriptor getContext() const { return getDescriptorField(1); }
269 /// DIFactory - This object assists with the construction of the various
273 // Cached values for uniquing and faster lookups.
274 DIAnchor CompileUnitAnchor, SubProgramAnchor, GlobalVariableAnchor;
275 const Type *EmptyStructPtr; // "{}*".
276 Function *StopPointFn; // llvm.dbg.stoppoint
277 Function *FuncStartFn; // llvm.dbg.func.start
278 Function *RegionStartFn; // llvm.dbg.region.start
279 Function *RegionEndFn; // llvm.dbg.region.end
280 Function *DeclareFn; // llvm.dbg.declare
281 StringMap<Constant*> StringCache;
282 DenseMap<Constant*, DIDescriptor> SimpleConstantCache;
284 DIFactory(const DIFactory &); // DO NOT IMPLEMENT
285 void operator=(const DIFactory&); // DO NOT IMPLEMENT
287 explicit DIFactory(Module &m);
289 /// GetOrCreateCompileUnitAnchor - Return the anchor for compile units,
290 /// creating a new one if there isn't already one in the module.
291 DIAnchor GetOrCreateCompileUnitAnchor();
293 /// GetOrCreateSubprogramAnchor - Return the anchor for subprograms,
294 /// creating a new one if there isn't already one in the module.
295 DIAnchor GetOrCreateSubprogramAnchor();
297 /// GetOrCreateGlobalVariableAnchor - Return the anchor for globals,
298 /// creating a new one if there isn't already one in the module.
299 DIAnchor GetOrCreateGlobalVariableAnchor();
301 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
302 /// This implicitly uniques the arrays created.
303 DIArray GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys);
305 /// GetOrCreateSubrange - Create a descriptor for a value range. This
306 /// implicitly uniques the values returned.
307 DISubrange GetOrCreateSubrange(int64_t Lo, int64_t Hi);
310 /// CreateCompileUnit - Create a new descriptor for the specified compile
312 DICompileUnit CreateCompileUnit(unsigned LangID,
313 const std::string &Filename,
314 const std::string &Directory,
315 const std::string &Producer);
317 /// CreateEnumerator - Create a single enumerator value.
318 DIEnumerator CreateEnumerator(const std::string &Name, uint64_t Val);
320 /// CreateBasicType - Create a basic type like int, float, etc.
321 DIBasicType CreateBasicType(DIDescriptor Context, const std::string &Name,
322 DICompileUnit CompileUnit, unsigned LineNumber,
323 uint64_t SizeInBits, uint64_t AlignInBits,
324 uint64_t OffsetInBits, unsigned Flags,
326 const std::string *FileName = 0,
327 const std::string *Directory = 0);
329 /// CreateDerivedType - Create a derived type like const qualified type,
330 /// pointer, typedef, etc.
331 DIDerivedType CreateDerivedType(unsigned Tag, DIDescriptor Context,
332 const std::string &Name,
333 DICompileUnit CompileUnit,
335 uint64_t SizeInBits, uint64_t AlignInBits,
336 uint64_t OffsetInBits, unsigned Flags,
338 const std::string *FileName = 0,
339 const std::string *Directory = 0);
341 /// CreateCompositeType - Create a composite type like array, struct, etc.
342 DICompositeType CreateCompositeType(unsigned Tag, DIDescriptor Context,
343 const std::string &Name,
344 DICompileUnit CompileUnit,
347 uint64_t AlignInBits,
348 uint64_t OffsetInBits, unsigned Flags,
351 const std::string *FileName = 0,
352 const std::string *Directory = 0);
354 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
355 /// See comments in DISubprogram for descriptions of these fields.
356 DISubprogram CreateSubprogram(DIDescriptor Context, const std::string &Name,
357 const std::string &DisplayName,
358 const std::string &LinkageName,
359 DICompileUnit CompileUnit, unsigned LineNo,
360 DIType Type, bool isLocalToUnit,
362 const std::string *FileName = 0,
363 const std::string *Directory = 0);
365 /// CreateGlobalVariable - Create a new descriptor for the specified global.
367 CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
368 const std::string &DisplayName,
369 const std::string &LinkageName,
370 DICompileUnit CompileUnit,
371 unsigned LineNo, DIType Type, bool isLocalToUnit,
372 bool isDefinition, llvm::GlobalVariable *GV,
373 const std::string *FileName = 0,
374 const std::string *Directory = 0);
377 /// CreateVariable - Create a new descriptor for the specified variable.
378 DIVariable CreateVariable(unsigned Tag, DIDescriptor Context,
379 const std::string &Name,
380 DICompileUnit CompileUnit, unsigned LineNo,
382 const std::string *FileName = 0,
383 const std::string *Directory = 0);
385 /// CreateBlock - This creates a descriptor for a lexical block with the
386 /// specified parent context.
387 DIBlock CreateBlock(DIDescriptor Context);
389 /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
390 /// inserting it at the end of the specified basic block.
391 void InsertStopPoint(DICompileUnit CU, unsigned LineNo, unsigned ColNo,
394 /// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
395 /// mark the start of the specified subprogram.
396 void InsertSubprogramStart(DISubprogram SP, BasicBlock *BB);
398 /// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
399 /// mark the start of a region for the specified scoping descriptor.
400 void InsertRegionStart(DIDescriptor D, BasicBlock *BB);
402 /// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
403 /// mark the end of a region for the specified scoping descriptor.
404 void InsertRegionEnd(DIDescriptor D, BasicBlock *BB);
406 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
407 void InsertDeclare(llvm::Value *Storage, DIVariable D, BasicBlock *BB);
410 Constant *GetTagConstant(unsigned TAG);
411 Constant *GetStringConstant(const std::string &String);
412 DIAnchor GetOrCreateAnchor(unsigned TAG, const char *Name);
414 /// getCastToEmpty - Return the descriptor as a Constant* with type '{}*'.
415 Constant *getCastToEmpty(DIDescriptor D);
418 /// Finds the stoppoint coressponding to this instruction, that is the
419 /// stoppoint that dominates this instruction
420 const DbgStopPointInst *findStopPoint(const Instruction *Inst);
422 /// Finds the stoppoint corresponding to first real (non-debug intrinsic)
423 /// instruction in this Basic Block, and returns the stoppoint for it.
424 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB);
426 /// Finds the dbg.declare intrinsic corresponding to this value if any.
427 /// It looks through pointer casts too.
428 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts = true);
429 } // end namespace llvm