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 /// DISubrange - This is used to represent ranges, for array bounds.
91 class DISubrange : public DIDescriptor {
93 explicit DISubrange(GlobalVariable *GV = 0);
95 int64_t getLo() const { return (int64_t)getUInt64Field(1); }
96 int64_t getHi() const { return (int64_t)getUInt64Field(2); }
99 /// DIArray - This descriptor holds an array of descriptors.
100 class DIArray : public DIDescriptor {
102 explicit DIArray(GlobalVariable *GV = 0) : DIDescriptor(GV) {}
104 unsigned getNumElements() const;
105 DISubrange getElement(unsigned Idx) const {
106 return getFieldAs<DISubrange>(Idx);
110 /// DICompileUnit - A wrapper for a compile unit.
111 class DICompileUnit : public DIDescriptor {
113 explicit DICompileUnit(GlobalVariable *GV = 0);
115 unsigned getLanguage() const { return getUnsignedField(2); }
116 std::string getFilename() const { return getStringField(3); }
117 std::string getDirectory() const { return getStringField(4); }
118 std::string getProducer() const { return getStringField(5); }
121 /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
122 /// FIXME: it seems strange that this doesn't have either a reference to the
123 /// type/precision or a file/line pair for location info.
124 class DIEnumerator : public DIDescriptor {
126 explicit DIEnumerator(GlobalVariable *GV = 0);
128 std::string getName() const { return getStringField(1); }
129 uint64_t getEnumValue() const { return getUInt64Field(2); }
132 /// DIType - This is a wrapper for a type.
133 /// FIXME: Types should be factored much better so that CV qualifiers and
134 /// others do not require a huge and empty descriptor full of zeros.
135 class DIType : public DIDescriptor {
137 DIType(GlobalVariable *GV, unsigned Tag) : DIDescriptor(GV, Tag) {}
138 // This ctor is used when the Tag has already been validated by a derived
140 DIType(GlobalVariable *GV, bool, bool) : DIDescriptor(GV) {}
142 explicit DIType(GlobalVariable *GV);
145 DIDescriptor getContext() const { return getDescriptorField(1); }
146 std::string getName() const { return getStringField(2); }
147 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
148 unsigned getLineNumber() const { return getUnsignedField(4); }
149 uint64_t getSizeInBits() const { return getUInt64Field(5); }
150 uint64_t getAlignInBits() const { return getUInt64Field(6); }
151 // FIXME: Offset is only used for DW_TAG_member nodes. Making every type
152 // carry this is just plain insane.
153 uint64_t getOffsetInBits() const { return getUInt64Field(7); }
154 unsigned getFlags() const { return getUnsignedField(8); }
157 /// DIBasicType - A basic type, like 'int' or 'float'.
158 class DIBasicType : public DIType {
160 explicit DIBasicType(GlobalVariable *GV);
162 unsigned getEncoding() const { return getUnsignedField(9); }
163 std::string getFileName() const { return getStringField(10); }
164 std::string getDirectory() const { return getStringField(11); }
167 /// DIDerivedType - A simple derived type, like a const qualified type,
168 /// a typedef, a pointer or reference, etc.
169 class DIDerivedType : public DIType {
171 explicit DIDerivedType(GlobalVariable *GV, bool, bool)
172 : DIType(GV, true, true) {}
174 explicit DIDerivedType(GlobalVariable *GV);
176 DIType getTypeDerivedFrom() const { return getFieldAs<DIType>(9); }
177 std::string getFileName() const { return getStringField(10); }
178 std::string getDirectory() const { return getStringField(11); }
180 /// isDerivedType - Return true if the specified tag is legal for
182 static bool isDerivedType(unsigned TAG);
186 /// DICompositeType - This descriptor holds a type that can refer to multiple
187 /// other types, like a function or struct.
188 /// FIXME: Why is this a DIDerivedType??
189 class DICompositeType : public DIDerivedType {
191 explicit DICompositeType(GlobalVariable *GV);
193 DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
194 std::string getFileName() const { return getStringField(11); }
195 std::string getDirectory() const { return getStringField(12); }
197 /// isCompositeType - Return true if the specified tag is legal for
199 static bool isCompositeType(unsigned TAG);
202 /// DIGlobal - This is a common class for global variables and subprograms.
203 class DIGlobal : public DIDescriptor {
205 explicit DIGlobal(GlobalVariable *GV, unsigned RequiredTag)
206 : DIDescriptor(GV, RequiredTag) {}
209 DIDescriptor getContext() const { return getDescriptorField(2); }
210 std::string getName() const { return getStringField(3); }
211 std::string getDisplayName() const { return getStringField(4); }
212 std::string getLinkageName() const { return getStringField(5); }
214 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(6); }
215 unsigned getLineNumber() const { return getUnsignedField(7); }
216 DIType getType() const { return getFieldAs<DIType>(8); }
218 /// isLocalToUnit - Return true if this subprogram is local to the current
219 /// compile unit, like 'static' in C.
220 unsigned isLocalToUnit() const { return getUnsignedField(9); }
221 unsigned isDefinition() const { return getUnsignedField(10); }
225 /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
226 class DISubprogram : public DIGlobal {
228 explicit DISubprogram(GlobalVariable *GV = 0);
229 std::string getFilename() const { return getStringField(11); }
230 std::string getDirectory() const { return getStringField(12); }
233 /// DIGlobalVariable - This is a wrapper for a global variable.
234 class DIGlobalVariable : public DIGlobal {
236 explicit DIGlobalVariable(GlobalVariable *GV = 0);
238 GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
239 std::string getFilename() const { return getStringField(12); }
240 std::string getDirectory() const { return getStringField(13); }
244 /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
246 class DIVariable : public DIDescriptor {
248 explicit DIVariable(GlobalVariable *GV = 0);
250 DIDescriptor getContext() const { return getDescriptorField(1); }
251 std::string getName() const { return getStringField(2); }
253 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
254 unsigned getLineNumber() const { return getUnsignedField(4); }
255 DIType getType() const { return getFieldAs<DIType>(5); }
256 std::string getFilename() const { return getStringField(6); }
257 std::string getDirectory() const { return getStringField(7); }
259 /// isVariable - Return true if the specified tag is legal for DIVariable.
260 static bool isVariable(unsigned Tag);
264 /// DIBlock - This is a wrapper for a block (e.g. a function, scope, etc).
265 class DIBlock : public DIDescriptor {
267 explicit DIBlock(GlobalVariable *GV = 0);
269 DIDescriptor getContext() const { return getDescriptorField(1); }
272 /// DIFactory - This object assists with the construction of the various
276 // Cached values for uniquing and faster lookups.
277 DIAnchor CompileUnitAnchor, SubProgramAnchor, GlobalVariableAnchor;
278 const Type *EmptyStructPtr; // "{}*".
279 Function *StopPointFn; // llvm.dbg.stoppoint
280 Function *FuncStartFn; // llvm.dbg.func.start
281 Function *RegionStartFn; // llvm.dbg.region.start
282 Function *RegionEndFn; // llvm.dbg.region.end
283 Function *DeclareFn; // llvm.dbg.declare
284 StringMap<Constant*> StringCache;
285 DenseMap<Constant*, DIDescriptor> SimpleConstantCache;
287 DIFactory(const DIFactory &); // DO NOT IMPLEMENT
288 void operator=(const DIFactory&); // DO NOT IMPLEMENT
290 explicit DIFactory(Module &m);
292 /// GetOrCreateCompileUnitAnchor - Return the anchor for compile units,
293 /// creating a new one if there isn't already one in the module.
294 DIAnchor GetOrCreateCompileUnitAnchor();
296 /// GetOrCreateSubprogramAnchor - Return the anchor for subprograms,
297 /// creating a new one if there isn't already one in the module.
298 DIAnchor GetOrCreateSubprogramAnchor();
300 /// GetOrCreateGlobalVariableAnchor - Return the anchor for globals,
301 /// creating a new one if there isn't already one in the module.
302 DIAnchor GetOrCreateGlobalVariableAnchor();
304 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
305 /// This implicitly uniques the arrays created.
306 DIArray GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys);
308 /// GetOrCreateSubrange - Create a descriptor for a value range. This
309 /// implicitly uniques the values returned.
310 DISubrange GetOrCreateSubrange(int64_t Lo, int64_t Hi);
313 /// CreateCompileUnit - Create a new descriptor for the specified compile
315 DICompileUnit CreateCompileUnit(unsigned LangID,
316 const std::string &Filename,
317 const std::string &Directory,
318 const std::string &Producer);
320 /// CreateEnumerator - Create a single enumerator value.
321 DIEnumerator CreateEnumerator(const std::string &Name, uint64_t Val);
323 /// CreateBasicType - Create a basic type like int, float, etc.
324 DIBasicType CreateBasicType(DIDescriptor Context, const std::string &Name,
325 DICompileUnit CompileUnit, unsigned LineNumber,
326 uint64_t SizeInBits, uint64_t AlignInBits,
327 uint64_t OffsetInBits, unsigned Flags,
329 const std::string *FileName = 0,
330 const std::string *Directory = 0);
332 /// CreateDerivedType - Create a derived type like const qualified type,
333 /// pointer, typedef, etc.
334 DIDerivedType CreateDerivedType(unsigned Tag, DIDescriptor Context,
335 const std::string &Name,
336 DICompileUnit CompileUnit,
338 uint64_t SizeInBits, uint64_t AlignInBits,
339 uint64_t OffsetInBits, unsigned Flags,
341 const std::string *FileName = 0,
342 const std::string *Directory = 0);
344 /// CreateCompositeType - Create a composite type like array, struct, etc.
345 DICompositeType CreateCompositeType(unsigned Tag, DIDescriptor Context,
346 const std::string &Name,
347 DICompileUnit CompileUnit,
350 uint64_t AlignInBits,
351 uint64_t OffsetInBits, unsigned Flags,
354 const std::string *FileName = 0,
355 const std::string *Directory = 0);
357 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
358 /// See comments in DISubprogram for descriptions of these fields.
359 DISubprogram CreateSubprogram(DIDescriptor Context, const std::string &Name,
360 const std::string &DisplayName,
361 const std::string &LinkageName,
362 DICompileUnit CompileUnit, unsigned LineNo,
363 DIType Type, bool isLocalToUnit,
365 const std::string *FileName = 0,
366 const std::string *Directory = 0);
368 /// CreateGlobalVariable - Create a new descriptor for the specified global.
370 CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
371 const std::string &DisplayName,
372 const std::string &LinkageName,
373 DICompileUnit CompileUnit,
374 unsigned LineNo, DIType Type, bool isLocalToUnit,
375 bool isDefinition, llvm::GlobalVariable *GV,
376 const std::string *FileName = 0,
377 const std::string *Directory = 0);
380 /// CreateVariable - Create a new descriptor for the specified variable.
381 DIVariable CreateVariable(unsigned Tag, DIDescriptor Context,
382 const std::string &Name,
383 DICompileUnit CompileUnit, unsigned LineNo,
385 const std::string *FileName = 0,
386 const std::string *Directory = 0);
388 /// CreateBlock - This creates a descriptor for a lexical block with the
389 /// specified parent context.
390 DIBlock CreateBlock(DIDescriptor Context);
392 /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
393 /// inserting it at the end of the specified basic block.
394 void InsertStopPoint(DICompileUnit CU, unsigned LineNo, unsigned ColNo,
397 /// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
398 /// mark the start of the specified subprogram.
399 void InsertSubprogramStart(DISubprogram SP, BasicBlock *BB);
401 /// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
402 /// mark the start of a region for the specified scoping descriptor.
403 void InsertRegionStart(DIDescriptor D, BasicBlock *BB);
405 /// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
406 /// mark the end of a region for the specified scoping descriptor.
407 void InsertRegionEnd(DIDescriptor D, BasicBlock *BB);
409 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
410 void InsertDeclare(llvm::Value *Storage, DIVariable D, BasicBlock *BB);
413 Constant *GetTagConstant(unsigned TAG);
414 Constant *GetStringConstant(const std::string &String);
415 DIAnchor GetOrCreateAnchor(unsigned TAG, const char *Name);
417 /// getCastToEmpty - Return the descriptor as a Constant* with type '{}*'.
418 Constant *getCastToEmpty(DIDescriptor D);
421 /// Finds the stoppoint coressponding to this instruction, that is the
422 /// stoppoint that dominates this instruction
423 const DbgStopPointInst *findStopPoint(const Instruction *Inst);
425 /// Finds the stoppoint corresponding to first real (non-debug intrinsic)
426 /// instruction in this Basic Block, and returns the stoppoint for it.
427 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB);
429 /// Finds the dbg.declare intrinsic corresponding to this value if any.
430 /// It looks through pointer casts too.
431 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts = true);
432 } // end namespace llvm