blob: c97cb2616f2ac4c938c2c8c94990ffa7c680716b [file] [log] [blame]
Chris Lattnera45664f2008-11-10 02:56:27 +00001//===--- llvm/Analysis/DebugInfo.h - Debug Information Helpers --*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines a bunch of datatypes that are useful for creating and
11// walking debug info in LLVM IR form.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_SUPPORT_DEBUGINFO_H
16#define LLVM_SUPPORT_DEBUGINFO_H
17
18#include "llvm/ADT/StringMap.h"
19#include "llvm/ADT/DenseMap.h"
20
21namespace llvm {
22 class BasicBlock;
23 class Constant;
24 class Function;
25 class GlobalVariable;
26 class Module;
Chris Lattner497a7a82008-11-10 04:10:34 +000027 class Type;
Chris Lattnera45664f2008-11-10 02:56:27 +000028 class Value;
Torok Edwin620f2802008-12-16 09:07:36 +000029 class DbgStopPointInst;
30 class DbgDeclareInst;
31 class Instruction;
Chris Lattnera45664f2008-11-10 02:56:27 +000032
33 class DIDescriptor {
34 public:
35 enum {
Devang Patel854967e2008-12-17 22:39:29 +000036 Version7 = 7 << 16, // Current version of debug information.
37 Version6 = 6 << 16, // Constant for version 6.
Chris Lattnera45664f2008-11-10 02:56:27 +000038 Version5 = 5 << 16, // Constant for version 5.
39 Version4 = 4 << 16, // Constant for version 4.
40 VersionMask = 0xffff0000 // Mask for version number.
41 };
42
43 protected:
44 GlobalVariable *GV;
45
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);
50
Chris Lattnera45664f2008-11-10 02:56:27 +000051 unsigned getVersion() const {
52 return getUnsignedField(0) & VersionMask;
53 }
54
55 std::string getStringField(unsigned Elt) const;
56 unsigned getUnsignedField(unsigned Elt) const {
57 return (unsigned)getUInt64Field(Elt);
58 }
59 uint64_t getUInt64Field(unsigned Elt) const;
60 DIDescriptor getDescriptorField(unsigned Elt) const;
61
62 template <typename DescTy>
63 DescTy getFieldAs(unsigned Elt) const {
Torok Edwinb07fbd92008-12-13 08:25:29 +000064 return DescTy(getDescriptorField(Elt).getGV());
Chris Lattnera45664f2008-11-10 02:56:27 +000065 }
66
67 GlobalVariable *getGlobalVariableField(unsigned Elt) const;
68
69 public:
70 explicit DIDescriptor() : GV(0) {}
71 explicit DIDescriptor(GlobalVariable *gv) : GV(gv) {}
72
73 bool isNull() const { return GV == 0; }
74
75 GlobalVariable *getGV() const { return GV; }
Devang Patel2c1623a2009-01-05 18:06:21 +000076
77 unsigned getTag() const {
78 return getUnsignedField(0) & ~VersionMask;
79 }
Chris Lattnera45664f2008-11-10 02:56:27 +000080 };
81
82 /// DIAnchor - A wrapper for various anchor descriptors.
83 class DIAnchor : public DIDescriptor {
84 public:
85 explicit DIAnchor(GlobalVariable *GV = 0);
86
87 unsigned getAnchorTag() const { return getUnsignedField(1); }
88 };
Devang Patel68afdc32009-01-05 18:33:01 +000089
90 /// DISubrange - This is used to represent ranges, for array bounds.
91 class DISubrange : public DIDescriptor {
92 public:
93 explicit DISubrange(GlobalVariable *GV = 0);
94
95 int64_t getLo() const { return (int64_t)getUInt64Field(1); }
96 int64_t getHi() const { return (int64_t)getUInt64Field(2); }
97 };
Chris Lattnera45664f2008-11-10 02:56:27 +000098
99 /// DIArray - This descriptor holds an array of descriptors.
100 class DIArray : public DIDescriptor {
101 public:
102 explicit DIArray(GlobalVariable *GV = 0) : DIDescriptor(GV) {}
103
104 unsigned getNumElements() const;
Devang Patel68afdc32009-01-05 18:33:01 +0000105 DISubrange getElement(unsigned Idx) const {
106 return getFieldAs<DISubrange>(Idx);
107 }
Chris Lattnera45664f2008-11-10 02:56:27 +0000108 };
109
110 /// DICompileUnit - A wrapper for a compile unit.
111 class DICompileUnit : public DIDescriptor {
112 public:
113 explicit DICompileUnit(GlobalVariable *GV = 0);
114
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); }
119 };
120
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 {
125 public:
126 explicit DIEnumerator(GlobalVariable *GV = 0);
127
128 std::string getName() const { return getStringField(1); }
Devang Patelc69bf2c2009-01-05 18:38:38 +0000129 uint64_t getEnumValue() const { return getUInt64Field(2); }
Chris Lattnera45664f2008-11-10 02:56:27 +0000130 };
131
Chris Lattnera45664f2008-11-10 02:56:27 +0000132 /// 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 {
136 protected:
137 DIType(GlobalVariable *GV, unsigned Tag) : DIDescriptor(GV, Tag) {}
138 // This ctor is used when the Tag has already been validated by a derived
139 // ctor.
140 DIType(GlobalVariable *GV, bool, bool) : DIDescriptor(GV) {}
141 public:
142 explicit DIType(GlobalVariable *GV);
143 explicit DIType() {}
144
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); }
155 };
156
157 /// DIBasicType - A basic type, like 'int' or 'float'.
158 class DIBasicType : public DIType {
159 public:
160 explicit DIBasicType(GlobalVariable *GV);
161
162 unsigned getEncoding() const { return getUnsignedField(9); }
Devang Patel854967e2008-12-17 22:39:29 +0000163 std::string getFileName() const { return getStringField(10); }
164 std::string getDirectory() const { return getStringField(11); }
Chris Lattnera45664f2008-11-10 02:56:27 +0000165 };
166
167 /// DIDerivedType - A simple derived type, like a const qualified type,
168 /// a typedef, a pointer or reference, etc.
169 class DIDerivedType : public DIType {
170 protected:
171 explicit DIDerivedType(GlobalVariable *GV, bool, bool)
172 : DIType(GV, true, true) {}
173 public:
174 explicit DIDerivedType(GlobalVariable *GV);
175
176 DIType getTypeDerivedFrom() const { return getFieldAs<DIType>(9); }
Devang Patel854967e2008-12-17 22:39:29 +0000177 std::string getFileName() const { return getStringField(10); }
178 std::string getDirectory() const { return getStringField(11); }
179
Chris Lattnera45664f2008-11-10 02:56:27 +0000180 /// isDerivedType - Return true if the specified tag is legal for
181 /// DIDerivedType.
182 static bool isDerivedType(unsigned TAG);
183 };
184
185
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 {
190 public:
191 explicit DICompositeType(GlobalVariable *GV);
192
193 DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
Devang Patel854967e2008-12-17 22:39:29 +0000194 std::string getFileName() const { return getStringField(11); }
195 std::string getDirectory() const { return getStringField(12); }
Chris Lattnera45664f2008-11-10 02:56:27 +0000196
197 /// isCompositeType - Return true if the specified tag is legal for
198 /// DICompositeType.
199 static bool isCompositeType(unsigned TAG);
200 };
201
202 /// DIGlobal - This is a common class for global variables and subprograms.
203 class DIGlobal : public DIDescriptor {
204 protected:
Chris Lattner3b781852008-11-10 03:11:39 +0000205 explicit DIGlobal(GlobalVariable *GV, unsigned RequiredTag)
Chris Lattnera45664f2008-11-10 02:56:27 +0000206 : DIDescriptor(GV, RequiredTag) {}
207 public:
208
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); }
213
214 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(6); }
215 unsigned getLineNumber() const { return getUnsignedField(7); }
216 DIType getType() const { return getFieldAs<DIType>(8); }
217
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); }
222 };
223
224
225 /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
226 class DISubprogram : public DIGlobal {
227 public:
228 explicit DISubprogram(GlobalVariable *GV = 0);
Devang Patel854967e2008-12-17 22:39:29 +0000229 std::string getFilename() const { return getStringField(11); }
230 std::string getDirectory() const { return getStringField(12); }
Chris Lattnera45664f2008-11-10 02:56:27 +0000231 };
232
233 /// DIGlobalVariable - This is a wrapper for a global variable.
234 class DIGlobalVariable : public DIGlobal {
235 public:
236 explicit DIGlobalVariable(GlobalVariable *GV = 0);
237
238 GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
Devang Patel854967e2008-12-17 22:39:29 +0000239 std::string getFilename() const { return getStringField(12); }
240 std::string getDirectory() const { return getStringField(13); }
Chris Lattnera45664f2008-11-10 02:56:27 +0000241 };
242
243
244 /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
245 /// global etc).
246 class DIVariable : public DIDescriptor {
247 public:
248 explicit DIVariable(GlobalVariable *GV = 0);
249
250 DIDescriptor getContext() const { return getDescriptorField(1); }
251 std::string getName() const { return getStringField(2); }
252
253 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
254 unsigned getLineNumber() const { return getUnsignedField(4); }
255 DIType getType() const { return getFieldAs<DIType>(5); }
Devang Patel854967e2008-12-17 22:39:29 +0000256 std::string getFilename() const { return getStringField(6); }
257 std::string getDirectory() const { return getStringField(7); }
Chris Lattnera45664f2008-11-10 02:56:27 +0000258
259 /// isVariable - Return true if the specified tag is legal for DIVariable.
260 static bool isVariable(unsigned Tag);
261 };
262
263
264 /// DIBlock - This is a wrapper for a block (e.g. a function, scope, etc).
265 class DIBlock : public DIDescriptor {
266 public:
267 explicit DIBlock(GlobalVariable *GV = 0);
268
269 DIDescriptor getContext() const { return getDescriptorField(1); }
270 };
271
272 /// DIFactory - This object assists with the construction of the various
273 /// descriptors.
274 class DIFactory {
275 Module &M;
276 // Cached values for uniquing and faster lookups.
277 DIAnchor CompileUnitAnchor, SubProgramAnchor, GlobalVariableAnchor;
Chris Lattner497a7a82008-11-10 04:10:34 +0000278 const Type *EmptyStructPtr; // "{}*".
Chris Lattnera45664f2008-11-10 02:56:27 +0000279 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;
286
287 DIFactory(const DIFactory &); // DO NOT IMPLEMENT
288 void operator=(const DIFactory&); // DO NOT IMPLEMENT
289 public:
Chris Lattner497a7a82008-11-10 04:10:34 +0000290 explicit DIFactory(Module &m);
Chris Lattnera45664f2008-11-10 02:56:27 +0000291
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();
295
296 /// GetOrCreateSubprogramAnchor - Return the anchor for subprograms,
297 /// creating a new one if there isn't already one in the module.
298 DIAnchor GetOrCreateSubprogramAnchor();
299
300 /// GetOrCreateGlobalVariableAnchor - Return the anchor for globals,
301 /// creating a new one if there isn't already one in the module.
302 DIAnchor GetOrCreateGlobalVariableAnchor();
303
304 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
305 /// This implicitly uniques the arrays created.
306 DIArray GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys);
307
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);
311
312
313 /// CreateCompileUnit - Create a new descriptor for the specified compile
314 /// unit.
315 DICompileUnit CreateCompileUnit(unsigned LangID,
316 const std::string &Filename,
317 const std::string &Directory,
318 const std::string &Producer);
319
320 /// CreateEnumerator - Create a single enumerator value.
321 DIEnumerator CreateEnumerator(const std::string &Name, uint64_t Val);
322
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,
Devang Patel854967e2008-12-17 22:39:29 +0000328 unsigned Encoding,
329 const std::string *FileName = 0,
330 const std::string *Directory = 0);
Chris Lattnera45664f2008-11-10 02:56:27 +0000331
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,
337 unsigned LineNumber,
338 uint64_t SizeInBits, uint64_t AlignInBits,
339 uint64_t OffsetInBits, unsigned Flags,
Devang Patel854967e2008-12-17 22:39:29 +0000340 DIType DerivedFrom,
341 const std::string *FileName = 0,
342 const std::string *Directory = 0);
Chris Lattnera45664f2008-11-10 02:56:27 +0000343
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,
348 unsigned LineNumber,
349 uint64_t SizeInBits,
350 uint64_t AlignInBits,
351 uint64_t OffsetInBits, unsigned Flags,
352 DIType DerivedFrom,
Devang Patel854967e2008-12-17 22:39:29 +0000353 DIArray Elements,
354 const std::string *FileName = 0,
355 const std::string *Directory = 0);
Chris Lattnera45664f2008-11-10 02:56:27 +0000356
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,
Devang Patel854967e2008-12-17 22:39:29 +0000364 bool isDefinition,
365 const std::string *FileName = 0,
366 const std::string *Directory = 0);
Chris Lattnera45664f2008-11-10 02:56:27 +0000367
368 /// CreateGlobalVariable - Create a new descriptor for the specified global.
369 DIGlobalVariable
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,
Devang Patel854967e2008-12-17 22:39:29 +0000375 bool isDefinition, llvm::GlobalVariable *GV,
376 const std::string *FileName = 0,
377 const std::string *Directory = 0);
Chris Lattnera45664f2008-11-10 02:56:27 +0000378
379
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,
Devang Patel854967e2008-12-17 22:39:29 +0000384 DIType Type,
385 const std::string *FileName = 0,
386 const std::string *Directory = 0);
Chris Lattnera45664f2008-11-10 02:56:27 +0000387
388 /// CreateBlock - This creates a descriptor for a lexical block with the
389 /// specified parent context.
390 DIBlock CreateBlock(DIDescriptor Context);
391
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,
395 BasicBlock *BB);
396
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);
400
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);
404
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);
408
409 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
410 void InsertDeclare(llvm::Value *Storage, DIVariable D, BasicBlock *BB);
411
412 private:
413 Constant *GetTagConstant(unsigned TAG);
414 Constant *GetStringConstant(const std::string &String);
415 DIAnchor GetOrCreateAnchor(unsigned TAG, const char *Name);
Chris Lattner497a7a82008-11-10 04:10:34 +0000416
417 /// getCastToEmpty - Return the descriptor as a Constant* with type '{}*'.
418 Constant *getCastToEmpty(DIDescriptor D);
Chris Lattnera45664f2008-11-10 02:56:27 +0000419 };
420
Torok Edwin620f2802008-12-16 09:07:36 +0000421 /// Finds the stoppoint coressponding to this instruction, that is the
422 /// stoppoint that dominates this instruction
423 const DbgStopPointInst *findStopPoint(const Instruction *Inst);
424
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);
428
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);
Chris Lattnera45664f2008-11-10 02:56:27 +0000432} // end namespace llvm
433
434#endif