blob: b8a6ce3cd543eb6446d47faaaeb041274ed7c461 [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
Bill Wendlingdc817b62009-05-14 18:26:15 +000011// walking debug info in LLVM IR form. They essentially provide wrappers around
12// the information in the global variables that's needed when constructing the
13// DWARF information.
Chris Lattnera45664f2008-11-10 02:56:27 +000014//
15//===----------------------------------------------------------------------===//
16
Evan Cheng891415b2009-01-26 07:31:20 +000017#ifndef LLVM_ANALYSIS_DEBUGINFO_H
18#define LLVM_ANALYSIS_DEBUGINFO_H
Chris Lattnera45664f2008-11-10 02:56:27 +000019
Devang Patele4b27562009-08-28 23:24:31 +000020#include "llvm/Metadata.h"
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000021#include "llvm/Target/TargetMachine.h"
Chris Lattnera45664f2008-11-10 02:56:27 +000022#include "llvm/ADT/StringMap.h"
23#include "llvm/ADT/DenseMap.h"
Devang Patel13e16b62009-06-26 01:49:18 +000024#include "llvm/ADT/SmallVector.h"
Devang Pateld2f79a12009-07-28 19:55:13 +000025#include "llvm/ADT/SmallPtrSet.h"
Devang Patel486938f2009-01-12 21:38:43 +000026#include "llvm/Support/Dwarf.h"
Chris Lattnera45664f2008-11-10 02:56:27 +000027
28namespace llvm {
29 class BasicBlock;
30 class Constant;
31 class Function;
32 class GlobalVariable;
33 class Module;
Chris Lattner497a7a82008-11-10 04:10:34 +000034 class Type;
Chris Lattnera45664f2008-11-10 02:56:27 +000035 class Value;
Cedric Venetaff9c272009-02-14 16:06:42 +000036 struct DbgStopPointInst;
37 struct DbgDeclareInst;
Devang Patel9e529c32009-07-02 01:15:24 +000038 struct DbgFuncStartInst;
39 struct DbgRegionStartInst;
40 struct DbgRegionEndInst;
41 class DebugLoc;
Daniel Dunbar460d16e2009-07-12 22:46:08 +000042 struct DebugLocTracker;
Torok Edwin620f2802008-12-16 09:07:36 +000043 class Instruction;
Benjamin Kramer12ddd402009-08-11 17:45:13 +000044 class LLVMContext;
Devang Patela913f4f2009-01-20 19:08:39 +000045
Chris Lattnera45664f2008-11-10 02:56:27 +000046 class DIDescriptor {
Chris Lattnera45664f2008-11-10 02:56:27 +000047 protected:
Devang Patele4b27562009-08-28 23:24:31 +000048 MDNode *DbgNode;
Devang Patela913f4f2009-01-20 19:08:39 +000049
Devang Patele4b27562009-08-28 23:24:31 +000050 /// DIDescriptor constructor. If the specified node is non-null, check
Chris Lattnera45664f2008-11-10 02:56:27 +000051 /// to make sure that the tag in the descriptor matches 'RequiredTag'. If
52 /// not, the debug info is corrupt and we ignore it.
Devang Patele4b27562009-08-28 23:24:31 +000053 DIDescriptor(MDNode *N, unsigned RequiredTag);
Devang Patela913f4f2009-01-20 19:08:39 +000054
Bill Wendling0582ae92009-03-13 04:39:26 +000055 const std::string &getStringField(unsigned Elt, std::string &Result) const;
Chris Lattnera45664f2008-11-10 02:56:27 +000056 unsigned getUnsignedField(unsigned Elt) const {
57 return (unsigned)getUInt64Field(Elt);
58 }
59 uint64_t getUInt64Field(unsigned Elt) const;
60 DIDescriptor getDescriptorField(unsigned Elt) const;
Devang Patela913f4f2009-01-20 19:08:39 +000061
Chris Lattnera45664f2008-11-10 02:56:27 +000062 template <typename DescTy>
63 DescTy getFieldAs(unsigned Elt) const {
Devang Patele4b27562009-08-28 23:24:31 +000064 return DescTy(getDescriptorField(Elt).getNode());
Chris Lattnera45664f2008-11-10 02:56:27 +000065 }
Devang Patela913f4f2009-01-20 19:08:39 +000066
Chris Lattnera45664f2008-11-10 02:56:27 +000067 GlobalVariable *getGlobalVariableField(unsigned Elt) const;
Devang Patela913f4f2009-01-20 19:08:39 +000068
Chris Lattnera45664f2008-11-10 02:56:27 +000069 public:
Devang Patele4b27562009-08-28 23:24:31 +000070 explicit DIDescriptor() : DbgNode(0) {}
71 explicit DIDescriptor(MDNode *N) : DbgNode(N) {}
Chris Lattnera45664f2008-11-10 02:56:27 +000072
Devang Patele4b27562009-08-28 23:24:31 +000073 bool isNull() const { return DbgNode == 0; }
Chris Lattnera45664f2008-11-10 02:56:27 +000074
Devang Patele4b27562009-08-28 23:24:31 +000075 MDNode *getNode() const { return DbgNode; }
Devang Patel2c1623a2009-01-05 18:06:21 +000076
Devang Patel8526cc02009-01-05 22:35:52 +000077 unsigned getVersion() const {
Devang Patel6906ba52009-01-20 19:22:03 +000078 return getUnsignedField(0) & LLVMDebugVersionMask;
Devang Patel8526cc02009-01-05 22:35:52 +000079 }
Devang Patela913f4f2009-01-20 19:08:39 +000080
Devang Patel2c1623a2009-01-05 18:06:21 +000081 unsigned getTag() const {
Devang Patel6906ba52009-01-20 19:22:03 +000082 return getUnsignedField(0) & ~LLVMDebugVersionMask;
Devang Patel2c1623a2009-01-05 18:06:21 +000083 }
Devang Patela913f4f2009-01-20 19:08:39 +000084
Devang Patele4b27562009-08-28 23:24:31 +000085 /// ValidDebugInfo - Return true if N represents valid debug info value.
86 static bool ValidDebugInfo(MDNode *N, CodeGenOpt::Level OptLevel);
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000087
Bill Wendling16de0132009-05-05 22:19:25 +000088 /// dump - print descriptor.
89 void dump() const;
Devang Patel6ceea332009-08-31 18:49:10 +000090
91 bool isDerivedType() const;
92 bool isCompositeType() const;
93 bool isBasicType() const;
94 bool isVariable() const;
95 bool isSubprogram() const;
96 bool isGlobalVariable() const;
Devang Patel43d98b32009-08-31 20:44:45 +000097 bool isScope() const;
Chris Lattnera45664f2008-11-10 02:56:27 +000098 };
Devang Patela913f4f2009-01-20 19:08:39 +000099
Devang Patel68afdc32009-01-05 18:33:01 +0000100 /// DISubrange - This is used to represent ranges, for array bounds.
101 class DISubrange : public DIDescriptor {
102 public:
Devang Patele4b27562009-08-28 23:24:31 +0000103 explicit DISubrange(MDNode *N = 0)
104 : DIDescriptor(N, dwarf::DW_TAG_subrange_type) {}
Devang Patela913f4f2009-01-20 19:08:39 +0000105
Devang Patel68afdc32009-01-05 18:33:01 +0000106 int64_t getLo() const { return (int64_t)getUInt64Field(1); }
107 int64_t getHi() const { return (int64_t)getUInt64Field(2); }
108 };
Devang Patela913f4f2009-01-20 19:08:39 +0000109
Chris Lattnera45664f2008-11-10 02:56:27 +0000110 /// DIArray - This descriptor holds an array of descriptors.
111 class DIArray : public DIDescriptor {
112 public:
Devang Patele4b27562009-08-28 23:24:31 +0000113 explicit DIArray(MDNode *N = 0)
114 : DIDescriptor(N) {}
Devang Patela913f4f2009-01-20 19:08:39 +0000115
Chris Lattnera45664f2008-11-10 02:56:27 +0000116 unsigned getNumElements() const;
Devang Patela22d57d2009-01-05 19:55:07 +0000117 DIDescriptor getElement(unsigned Idx) const {
118 return getDescriptorField(Idx);
Devang Patel68afdc32009-01-05 18:33:01 +0000119 }
Chris Lattnera45664f2008-11-10 02:56:27 +0000120 };
Devang Patela913f4f2009-01-20 19:08:39 +0000121
Devang Patel43d98b32009-08-31 20:44:45 +0000122 /// DIScope - A base class for various scopes.
123 class DIScope : public DIDescriptor {
124 public:
125 explicit DIScope(MDNode *N = 0) : DIDescriptor (N) {
126 if (DbgNode && !isScope())
127 DbgNode = 0;
128 }
129 };
130
Chris Lattnera45664f2008-11-10 02:56:27 +0000131 /// DICompileUnit - A wrapper for a compile unit.
132 class DICompileUnit : public DIDescriptor {
133 public:
Devang Patele4b27562009-08-28 23:24:31 +0000134 explicit DICompileUnit(MDNode *N = 0)
135 : DIDescriptor(N, dwarf::DW_TAG_compile_unit) {}
Devang Patela913f4f2009-01-20 19:08:39 +0000136
Chris Lattnera45664f2008-11-10 02:56:27 +0000137 unsigned getLanguage() const { return getUnsignedField(2); }
Bill Wendling0582ae92009-03-13 04:39:26 +0000138 const std::string &getFilename(std::string &F) const {
139 return getStringField(3, F);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +0000140 }
Bill Wendling0582ae92009-03-13 04:39:26 +0000141 const std::string &getDirectory(std::string &F) const {
142 return getStringField(4, F);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +0000143 }
Bill Wendling0582ae92009-03-13 04:39:26 +0000144 const std::string &getProducer(std::string &F) const {
145 return getStringField(5, F);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +0000146 }
Devang Pateldd9db662009-01-30 18:20:31 +0000147
148 /// isMain - Each input file is encoded as a separate compile unit in LLVM
149 /// debugging information output. However, many target specific tool chains
150 /// prefer to encode only one compile unit in an object file. In this
151 /// situation, the LLVM code generator will include debugging information
152 /// entities in the compile unit that is marked as main compile unit. The
153 /// code generator accepts maximum one main compile unit per module. If a
154 /// module does not contain any main compile unit then the code generator
155 /// will emit multiple compile units in the output object file.
Devang Patel13319ce2009-02-17 22:43:44 +0000156
Devang Patel36375ee2009-02-17 21:23:59 +0000157 bool isMain() const { return getUnsignedField(6); }
158 bool isOptimized() const { return getUnsignedField(7); }
Bill Wendling0582ae92009-03-13 04:39:26 +0000159 const std::string &getFlags(std::string &F) const {
160 return getStringField(8, F);
161 }
Devang Patel13319ce2009-02-17 22:43:44 +0000162 unsigned getRunTimeVersion() const { return getUnsignedField(9); }
Devang Patelce31b022009-01-20 18:13:03 +0000163
Devang Patelb79b5352009-01-19 23:21:49 +0000164 /// Verify - Verify that a compile unit is well formed.
165 bool Verify() const;
Devang Patelbf3f5a02009-01-30 01:03:10 +0000166
167 /// dump - print compile unit.
168 void dump() const;
Chris Lattnera45664f2008-11-10 02:56:27 +0000169 };
170
171 /// DIEnumerator - A wrapper for an enumerator (e.g. X and Y in 'enum {X,Y}').
172 /// FIXME: it seems strange that this doesn't have either a reference to the
173 /// type/precision or a file/line pair for location info.
174 class DIEnumerator : public DIDescriptor {
175 public:
Devang Patele4b27562009-08-28 23:24:31 +0000176 explicit DIEnumerator(MDNode *N = 0)
177 : DIDescriptor(N, dwarf::DW_TAG_enumerator) {}
Devang Patela913f4f2009-01-20 19:08:39 +0000178
Bill Wendling0582ae92009-03-13 04:39:26 +0000179 const std::string &getName(std::string &F) const {
180 return getStringField(1, F);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +0000181 }
Devang Patelc69bf2c2009-01-05 18:38:38 +0000182 uint64_t getEnumValue() const { return getUInt64Field(2); }
Chris Lattnera45664f2008-11-10 02:56:27 +0000183 };
Devang Patela913f4f2009-01-20 19:08:39 +0000184
Chris Lattnera45664f2008-11-10 02:56:27 +0000185 /// DIType - This is a wrapper for a type.
186 /// FIXME: Types should be factored much better so that CV qualifiers and
187 /// others do not require a huge and empty descriptor full of zeros.
188 class DIType : public DIDescriptor {
Devang Patel2a574662009-01-20 22:27:02 +0000189 public:
190 enum {
Devang Patela1ba2692009-08-27 23:51:51 +0000191 FlagPrivate = 1 << 0,
192 FlagProtected = 1 << 1,
193 FlagFwdDecl = 1 << 2,
194 FlagAppleBlock = 1 << 3
Devang Patel2a574662009-01-20 22:27:02 +0000195 };
196
Chris Lattnera45664f2008-11-10 02:56:27 +0000197 protected:
Devang Patele4b27562009-08-28 23:24:31 +0000198 DIType(MDNode *N, unsigned Tag)
199 : DIDescriptor(N, Tag) {}
Chris Lattnera45664f2008-11-10 02:56:27 +0000200 // This ctor is used when the Tag has already been validated by a derived
201 // ctor.
Devang Patele4b27562009-08-28 23:24:31 +0000202 DIType(MDNode *N, bool, bool) : DIDescriptor(N) {}
Devang Patel486938f2009-01-12 21:38:43 +0000203
Devang Patelf193ff02009-01-15 19:26:23 +0000204 public:
Devang Patel486938f2009-01-12 21:38:43 +0000205
Devang Patelb79b5352009-01-19 23:21:49 +0000206 /// Verify - Verify that a type descriptor is well formed.
207 bool Verify() const;
Chris Lattnera45664f2008-11-10 02:56:27 +0000208 public:
Devang Patele4b27562009-08-28 23:24:31 +0000209 explicit DIType(MDNode *N);
Chris Lattnera45664f2008-11-10 02:56:27 +0000210 explicit DIType() {}
Devang Patel8526cc02009-01-05 22:35:52 +0000211 virtual ~DIType() {}
212
Chris Lattnera45664f2008-11-10 02:56:27 +0000213 DIDescriptor getContext() const { return getDescriptorField(1); }
Bill Wendling0582ae92009-03-13 04:39:26 +0000214 const std::string &getName(std::string &F) const {
215 return getStringField(2, F);
216 }
Chris Lattnera45664f2008-11-10 02:56:27 +0000217 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
218 unsigned getLineNumber() const { return getUnsignedField(4); }
219 uint64_t getSizeInBits() const { return getUInt64Field(5); }
220 uint64_t getAlignInBits() const { return getUInt64Field(6); }
221 // FIXME: Offset is only used for DW_TAG_member nodes. Making every type
222 // carry this is just plain insane.
223 uint64_t getOffsetInBits() const { return getUInt64Field(7); }
224 unsigned getFlags() const { return getUnsignedField(8); }
Chris Lattnere1f515e2009-08-26 04:21:30 +0000225 bool isPrivate() const {
226 return (getFlags() & FlagPrivate) != 0;
Devang Patele2d5a6c2009-07-27 20:30:05 +0000227 }
Chris Lattnere1f515e2009-08-26 04:21:30 +0000228 bool isProtected() const {
Devang Patele2d5a6c2009-07-27 20:30:05 +0000229 return (getFlags() & FlagProtected) != 0;
230 }
Chris Lattnere1f515e2009-08-26 04:21:30 +0000231 bool isForwardDecl() const {
232 return (getFlags() & FlagFwdDecl) != 0;
Devang Patele2d5a6c2009-07-27 20:30:05 +0000233 }
Devang Patela1ba2692009-08-27 23:51:51 +0000234 // isAppleBlock - Return true if this is the Apple Blocks extension.
235 bool isAppleBlockExtension() const {
236 return (getFlags() & FlagAppleBlock) != 0;
Devang Patel8af76bd2009-08-26 00:39:50 +0000237 }
Devang Patel8526cc02009-01-05 22:35:52 +0000238
Devang Patelbf3f5a02009-01-30 01:03:10 +0000239 /// dump - print type.
240 void dump() const;
Chris Lattnera45664f2008-11-10 02:56:27 +0000241 };
Devang Patela913f4f2009-01-20 19:08:39 +0000242
Chris Lattnera45664f2008-11-10 02:56:27 +0000243 /// DIBasicType - A basic type, like 'int' or 'float'.
244 class DIBasicType : public DIType {
245 public:
Devang Patele4b27562009-08-28 23:24:31 +0000246 explicit DIBasicType(MDNode *N = 0)
247 : DIType(N, dwarf::DW_TAG_base_type) {}
Bill Wendlingdc817b62009-05-14 18:26:15 +0000248
Chris Lattnera45664f2008-11-10 02:56:27 +0000249 unsigned getEncoding() const { return getUnsignedField(9); }
Devang Patelbf3f5a02009-01-30 01:03:10 +0000250
251 /// dump - print basic type.
252 void dump() const;
Chris Lattnera45664f2008-11-10 02:56:27 +0000253 };
Devang Patela913f4f2009-01-20 19:08:39 +0000254
Chris Lattnera45664f2008-11-10 02:56:27 +0000255 /// DIDerivedType - A simple derived type, like a const qualified type,
256 /// a typedef, a pointer or reference, etc.
257 class DIDerivedType : public DIType {
258 protected:
Devang Patele4b27562009-08-28 23:24:31 +0000259 explicit DIDerivedType(MDNode *N, bool, bool)
260 : DIType(N, true, true) {}
Chris Lattnera45664f2008-11-10 02:56:27 +0000261 public:
Devang Patele4b27562009-08-28 23:24:31 +0000262 explicit DIDerivedType(MDNode *N = 0)
263 : DIType(N, true, true) {
Devang Patel6ceea332009-08-31 18:49:10 +0000264 if (DbgNode && !isDerivedType())
Devang Patele4b27562009-08-28 23:24:31 +0000265 DbgNode = 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000266 }
267
Chris Lattnera45664f2008-11-10 02:56:27 +0000268 DIType getTypeDerivedFrom() const { return getFieldAs<DIType>(9); }
Devang Patelbf3f5a02009-01-30 01:03:10 +0000269
Devang Patel36375ee2009-02-17 21:23:59 +0000270 /// getOriginalTypeSize - If this type is derived from a base type then
271 /// return base type size.
272 uint64_t getOriginalTypeSize() const;
Devang Patelbf3f5a02009-01-30 01:03:10 +0000273 /// dump - print derived type.
274 void dump() const;
Devang Patelc4999d72009-07-22 18:23:44 +0000275
276 /// replaceAllUsesWith - Replace all uses of debug info referenced by
277 /// this descriptor. After this completes, the current debug info value
278 /// is erased.
279 void replaceAllUsesWith(DIDescriptor &D);
Chris Lattnera45664f2008-11-10 02:56:27 +0000280 };
281
Chris Lattnera45664f2008-11-10 02:56:27 +0000282 /// DICompositeType - This descriptor holds a type that can refer to multiple
283 /// other types, like a function or struct.
284 /// FIXME: Why is this a DIDerivedType??
285 class DICompositeType : public DIDerivedType {
286 public:
Devang Patele4b27562009-08-28 23:24:31 +0000287 explicit DICompositeType(MDNode *N = 0)
288 : DIDerivedType(N, true, true) {
Devang Patel6ceea332009-08-31 18:49:10 +0000289 if (N && !isCompositeType())
Devang Patele4b27562009-08-28 23:24:31 +0000290 DbgNode = 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000291 }
292
Chris Lattnera45664f2008-11-10 02:56:27 +0000293 DIArray getTypeArray() const { return getFieldAs<DIArray>(10); }
Devang Patel13319ce2009-02-17 22:43:44 +0000294 unsigned getRunTimeLang() const { return getUnsignedField(11); }
Devang Patelb79b5352009-01-19 23:21:49 +0000295
296 /// Verify - Verify that a composite type descriptor is well formed.
297 bool Verify() const;
Devang Patelbf3f5a02009-01-30 01:03:10 +0000298
299 /// dump - print composite type.
300 void dump() const;
Chris Lattnera45664f2008-11-10 02:56:27 +0000301 };
Devang Patela913f4f2009-01-20 19:08:39 +0000302
Chris Lattnera45664f2008-11-10 02:56:27 +0000303 /// DIGlobal - This is a common class for global variables and subprograms.
304 class DIGlobal : public DIDescriptor {
305 protected:
Devang Patele4b27562009-08-28 23:24:31 +0000306 explicit DIGlobal(MDNode *N, unsigned RequiredTag)
307 : DIDescriptor(N, RequiredTag) {}
Devang Patel486938f2009-01-12 21:38:43 +0000308
Chris Lattnera45664f2008-11-10 02:56:27 +0000309 public:
Devang Patel8526cc02009-01-05 22:35:52 +0000310 virtual ~DIGlobal() {}
311
Chris Lattnera45664f2008-11-10 02:56:27 +0000312 DIDescriptor getContext() const { return getDescriptorField(2); }
Bill Wendling0582ae92009-03-13 04:39:26 +0000313 const std::string &getName(std::string &F) const {
314 return getStringField(3, F);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +0000315 }
Bill Wendling0582ae92009-03-13 04:39:26 +0000316 const std::string &getDisplayName(std::string &F) const {
317 return getStringField(4, F);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +0000318 }
Bill Wendling0582ae92009-03-13 04:39:26 +0000319 const std::string &getLinkageName(std::string &F) const {
320 return getStringField(5, F);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +0000321 }
Chris Lattnera45664f2008-11-10 02:56:27 +0000322 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(6); }
323 unsigned getLineNumber() const { return getUnsignedField(7); }
324 DIType getType() const { return getFieldAs<DIType>(8); }
Devang Patela913f4f2009-01-20 19:08:39 +0000325
Chris Lattnera45664f2008-11-10 02:56:27 +0000326 /// isLocalToUnit - Return true if this subprogram is local to the current
327 /// compile unit, like 'static' in C.
328 unsigned isLocalToUnit() const { return getUnsignedField(9); }
329 unsigned isDefinition() const { return getUnsignedField(10); }
Devang Patel8526cc02009-01-05 22:35:52 +0000330
Devang Patelbf3f5a02009-01-30 01:03:10 +0000331 /// dump - print global.
332 void dump() const;
Chris Lattnera45664f2008-11-10 02:56:27 +0000333 };
Devang Patela913f4f2009-01-20 19:08:39 +0000334
Chris Lattnera45664f2008-11-10 02:56:27 +0000335 /// DISubprogram - This is a wrapper for a subprogram (e.g. a function).
336 class DISubprogram : public DIGlobal {
337 public:
Devang Patele4b27562009-08-28 23:24:31 +0000338 explicit DISubprogram(MDNode *N = 0)
339 : DIGlobal(N, dwarf::DW_TAG_subprogram) {}
Bill Wendlingdc817b62009-05-14 18:26:15 +0000340
Devang Patel86ae1422009-01-05 18:59:44 +0000341 DICompositeType getType() const { return getFieldAs<DICompositeType>(8); }
Devang Patelb79b5352009-01-19 23:21:49 +0000342
Devang Patel0de4fa62009-06-23 22:07:48 +0000343 /// getReturnTypeName - Subprogram return types are encoded either as
344 /// DIType or as DICompositeType.
345 const std::string &getReturnTypeName(std::string &F) const {
346 DICompositeType DCT(getFieldAs<DICompositeType>(8));
347 if (!DCT.isNull()) {
348 DIArray A = DCT.getTypeArray();
Devang Patele4b27562009-08-28 23:24:31 +0000349 DIType T(A.getElement(0).getNode());
Devang Patel0de4fa62009-06-23 22:07:48 +0000350 return T.getName(F);
351 }
352 DIType T(getFieldAs<DIType>(8));
353 return T.getName(F);
354 }
355
Devang Patelb79b5352009-01-19 23:21:49 +0000356 /// Verify - Verify that a subprogram descriptor is well formed.
357 bool Verify() const;
Devang Patelbf3f5a02009-01-30 01:03:10 +0000358
359 /// dump - print subprogram.
360 void dump() const;
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000361
362 /// describes - Return true if this subprogram provides debugging
363 /// information for the function F.
364 bool describes(const Function *F);
Chris Lattnera45664f2008-11-10 02:56:27 +0000365 };
Devang Patela913f4f2009-01-20 19:08:39 +0000366
Chris Lattnera45664f2008-11-10 02:56:27 +0000367 /// DIGlobalVariable - This is a wrapper for a global variable.
368 class DIGlobalVariable : public DIGlobal {
369 public:
Devang Patele4b27562009-08-28 23:24:31 +0000370 explicit DIGlobalVariable(MDNode *N = 0)
371 : DIGlobal(N, dwarf::DW_TAG_variable) {}
Bill Wendlingdc817b62009-05-14 18:26:15 +0000372
Chris Lattnera45664f2008-11-10 02:56:27 +0000373 GlobalVariable *getGlobal() const { return getGlobalVariableField(11); }
Devang Patelb79b5352009-01-19 23:21:49 +0000374
375 /// Verify - Verify that a global variable descriptor is well formed.
376 bool Verify() const;
Devang Patelbf3f5a02009-01-30 01:03:10 +0000377
378 /// dump - print global variable.
379 void dump() const;
Chris Lattnera45664f2008-11-10 02:56:27 +0000380 };
Devang Patela913f4f2009-01-20 19:08:39 +0000381
Chris Lattnera45664f2008-11-10 02:56:27 +0000382 /// DIVariable - This is a wrapper for a variable (e.g. parameter, local,
383 /// global etc).
384 class DIVariable : public DIDescriptor {
385 public:
Devang Patele4b27562009-08-28 23:24:31 +0000386 explicit DIVariable(MDNode *N = 0)
387 : DIDescriptor(N) {
Devang Patel6ceea332009-08-31 18:49:10 +0000388 if (DbgNode && !isVariable())
Devang Patele4b27562009-08-28 23:24:31 +0000389 DbgNode = 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000390 }
Devang Patela913f4f2009-01-20 19:08:39 +0000391
Chris Lattnera45664f2008-11-10 02:56:27 +0000392 DIDescriptor getContext() const { return getDescriptorField(1); }
Bill Wendling0582ae92009-03-13 04:39:26 +0000393 const std::string &getName(std::string &F) const {
394 return getStringField(2, F);
Bill Wendlingccbdc7a2009-03-09 05:04:40 +0000395 }
Chris Lattnera45664f2008-11-10 02:56:27 +0000396 DICompileUnit getCompileUnit() const{ return getFieldAs<DICompileUnit>(3); }
397 unsigned getLineNumber() const { return getUnsignedField(4); }
398 DIType getType() const { return getFieldAs<DIType>(5); }
Devang Patela913f4f2009-01-20 19:08:39 +0000399
Devang Patelb79b5352009-01-19 23:21:49 +0000400
401 /// Verify - Verify that a variable descriptor is well formed.
402 bool Verify() const;
Devang Patelbf3f5a02009-01-30 01:03:10 +0000403
404 /// dump - print variable.
405 void dump() const;
Chris Lattnera45664f2008-11-10 02:56:27 +0000406 };
Devang Patela913f4f2009-01-20 19:08:39 +0000407
Chris Lattnera45664f2008-11-10 02:56:27 +0000408 /// DIBlock - This is a wrapper for a block (e.g. a function, scope, etc).
409 class DIBlock : public DIDescriptor {
410 public:
Devang Patele4b27562009-08-28 23:24:31 +0000411 explicit DIBlock(MDNode *N = 0)
412 : DIDescriptor(N, dwarf::DW_TAG_lexical_block) {}
Bill Wendlingdc817b62009-05-14 18:26:15 +0000413
Chris Lattnera45664f2008-11-10 02:56:27 +0000414 DIDescriptor getContext() const { return getDescriptorField(1); }
415 };
Devang Patela913f4f2009-01-20 19:08:39 +0000416
Chris Lattnera45664f2008-11-10 02:56:27 +0000417 /// DIFactory - This object assists with the construction of the various
418 /// descriptors.
419 class DIFactory {
420 Module &M;
Owen Anderson99035272009-07-07 17:12:53 +0000421 LLVMContext& VMContext;
422
Chris Lattnera45664f2008-11-10 02:56:27 +0000423 // Cached values for uniquing and faster lookups.
Chris Lattner497a7a82008-11-10 04:10:34 +0000424 const Type *EmptyStructPtr; // "{}*".
Chris Lattnera45664f2008-11-10 02:56:27 +0000425 Function *StopPointFn; // llvm.dbg.stoppoint
426 Function *FuncStartFn; // llvm.dbg.func.start
427 Function *RegionStartFn; // llvm.dbg.region.start
428 Function *RegionEndFn; // llvm.dbg.region.end
429 Function *DeclareFn; // llvm.dbg.declare
430 StringMap<Constant*> StringCache;
431 DenseMap<Constant*, DIDescriptor> SimpleConstantCache;
Devang Patela913f4f2009-01-20 19:08:39 +0000432
Chris Lattnera45664f2008-11-10 02:56:27 +0000433 DIFactory(const DIFactory &); // DO NOT IMPLEMENT
434 void operator=(const DIFactory&); // DO NOT IMPLEMENT
435 public:
Chris Lattner497a7a82008-11-10 04:10:34 +0000436 explicit DIFactory(Module &m);
Devang Patela913f4f2009-01-20 19:08:39 +0000437
Chris Lattnera45664f2008-11-10 02:56:27 +0000438 /// GetOrCreateArray - Create an descriptor for an array of descriptors.
439 /// This implicitly uniques the arrays created.
440 DIArray GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys);
441
442 /// GetOrCreateSubrange - Create a descriptor for a value range. This
443 /// implicitly uniques the values returned.
444 DISubrange GetOrCreateSubrange(int64_t Lo, int64_t Hi);
Devang Patela913f4f2009-01-20 19:08:39 +0000445
Chris Lattnera45664f2008-11-10 02:56:27 +0000446 /// CreateCompileUnit - Create a new descriptor for the specified compile
447 /// unit.
448 DICompileUnit CreateCompileUnit(unsigned LangID,
449 const std::string &Filename,
450 const std::string &Directory,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000451 const std::string &Producer,
Devang Pateldd9db662009-01-30 18:20:31 +0000452 bool isMain = false,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000453 bool isOptimized = false,
Devang Patel13319ce2009-02-17 22:43:44 +0000454 const char *Flags = "",
455 unsigned RunTimeVer = 0);
Chris Lattnera45664f2008-11-10 02:56:27 +0000456
457 /// CreateEnumerator - Create a single enumerator value.
458 DIEnumerator CreateEnumerator(const std::string &Name, uint64_t Val);
Devang Patela913f4f2009-01-20 19:08:39 +0000459
Chris Lattnera45664f2008-11-10 02:56:27 +0000460 /// CreateBasicType - Create a basic type like int, float, etc.
461 DIBasicType CreateBasicType(DIDescriptor Context, const std::string &Name,
462 DICompileUnit CompileUnit, unsigned LineNumber,
463 uint64_t SizeInBits, uint64_t AlignInBits,
464 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000465 unsigned Encoding);
Devang Patela913f4f2009-01-20 19:08:39 +0000466
Chris Lattnera45664f2008-11-10 02:56:27 +0000467 /// CreateDerivedType - Create a derived type like const qualified type,
468 /// pointer, typedef, etc.
469 DIDerivedType CreateDerivedType(unsigned Tag, DIDescriptor Context,
470 const std::string &Name,
471 DICompileUnit CompileUnit,
472 unsigned LineNumber,
473 uint64_t SizeInBits, uint64_t AlignInBits,
474 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000475 DIType DerivedFrom);
Chris Lattnera45664f2008-11-10 02:56:27 +0000476
477 /// CreateCompositeType - Create a composite type like array, struct, etc.
478 DICompositeType CreateCompositeType(unsigned Tag, DIDescriptor Context,
479 const std::string &Name,
480 DICompileUnit CompileUnit,
481 unsigned LineNumber,
482 uint64_t SizeInBits,
483 uint64_t AlignInBits,
484 uint64_t OffsetInBits, unsigned Flags,
485 DIType DerivedFrom,
Devang Patel13319ce2009-02-17 22:43:44 +0000486 DIArray Elements,
487 unsigned RunTimeLang = 0);
Devang Patela913f4f2009-01-20 19:08:39 +0000488
Chris Lattnera45664f2008-11-10 02:56:27 +0000489 /// CreateSubprogram - Create a new descriptor for the specified subprogram.
490 /// See comments in DISubprogram for descriptions of these fields.
491 DISubprogram CreateSubprogram(DIDescriptor Context, const std::string &Name,
492 const std::string &DisplayName,
493 const std::string &LinkageName,
494 DICompileUnit CompileUnit, unsigned LineNo,
495 DIType Type, bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000496 bool isDefinition);
Chris Lattnera45664f2008-11-10 02:56:27 +0000497
498 /// CreateGlobalVariable - Create a new descriptor for the specified global.
499 DIGlobalVariable
500 CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
501 const std::string &DisplayName,
502 const std::string &LinkageName,
503 DICompileUnit CompileUnit,
504 unsigned LineNo, DIType Type, bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000505 bool isDefinition, llvm::GlobalVariable *GV);
Devang Patela913f4f2009-01-20 19:08:39 +0000506
Chris Lattnera45664f2008-11-10 02:56:27 +0000507 /// CreateVariable - Create a new descriptor for the specified variable.
508 DIVariable CreateVariable(unsigned Tag, DIDescriptor Context,
509 const std::string &Name,
510 DICompileUnit CompileUnit, unsigned LineNo,
Devang Pateldd9db662009-01-30 18:20:31 +0000511 DIType Type);
Devang Patela913f4f2009-01-20 19:08:39 +0000512
Chris Lattnera45664f2008-11-10 02:56:27 +0000513 /// CreateBlock - This creates a descriptor for a lexical block with the
514 /// specified parent context.
515 DIBlock CreateBlock(DIDescriptor Context);
Devang Patela913f4f2009-01-20 19:08:39 +0000516
Chris Lattnera45664f2008-11-10 02:56:27 +0000517 /// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
518 /// inserting it at the end of the specified basic block.
519 void InsertStopPoint(DICompileUnit CU, unsigned LineNo, unsigned ColNo,
520 BasicBlock *BB);
Devang Patela913f4f2009-01-20 19:08:39 +0000521
Chris Lattnera45664f2008-11-10 02:56:27 +0000522 /// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
523 /// mark the start of the specified subprogram.
524 void InsertSubprogramStart(DISubprogram SP, BasicBlock *BB);
525
526 /// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
527 /// mark the start of a region for the specified scoping descriptor.
528 void InsertRegionStart(DIDescriptor D, BasicBlock *BB);
Devang Patela913f4f2009-01-20 19:08:39 +0000529
Chris Lattnera45664f2008-11-10 02:56:27 +0000530 /// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
531 /// mark the end of a region for the specified scoping descriptor.
532 void InsertRegionEnd(DIDescriptor D, BasicBlock *BB);
Devang Patela913f4f2009-01-20 19:08:39 +0000533
Chris Lattnera45664f2008-11-10 02:56:27 +0000534 /// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
535 void InsertDeclare(llvm::Value *Storage, DIVariable D, BasicBlock *BB);
Devang Patela913f4f2009-01-20 19:08:39 +0000536
Chris Lattnera45664f2008-11-10 02:56:27 +0000537 private:
538 Constant *GetTagConstant(unsigned TAG);
Chris Lattnera45664f2008-11-10 02:56:27 +0000539 };
Devang Patela913f4f2009-01-20 19:08:39 +0000540
Torok Edwin620f2802008-12-16 09:07:36 +0000541 /// Finds the stoppoint coressponding to this instruction, that is the
542 /// stoppoint that dominates this instruction
543 const DbgStopPointInst *findStopPoint(const Instruction *Inst);
544
545 /// Finds the stoppoint corresponding to first real (non-debug intrinsic)
546 /// instruction in this Basic Block, and returns the stoppoint for it.
547 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB);
548
549 /// Finds the dbg.declare intrinsic corresponding to this value if any.
550 /// It looks through pointer casts too.
551 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts = true);
Torok Edwinff7d0e92009-03-10 13:41:26 +0000552
553 /// Find the debug info descriptor corresponding to this global variable.
554 Value *findDbgGlobalDeclare(GlobalVariable *V);
555
Devang Patele2d5a6c2009-07-27 20:30:05 +0000556 bool getLocationInfo(const Value *V, std::string &DisplayName,
557 std::string &Type, unsigned &LineNo, std::string &File,
558 std::string &Dir);
Devang Patel13e16b62009-06-26 01:49:18 +0000559
Devang Patel9e529c32009-07-02 01:15:24 +0000560 /// isValidDebugInfoIntrinsic - Return true if SPI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +0000561 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +0000562 bool isValidDebugInfoIntrinsic(DbgStopPointInst &SPI,
563 CodeGenOpt::Level OptLev);
564
565 /// isValidDebugInfoIntrinsic - Return true if FSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +0000566 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +0000567 bool isValidDebugInfoIntrinsic(DbgFuncStartInst &FSI,
568 CodeGenOpt::Level OptLev);
569
570 /// isValidDebugInfoIntrinsic - Return true if RSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +0000571 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +0000572 bool isValidDebugInfoIntrinsic(DbgRegionStartInst &RSI,
573 CodeGenOpt::Level OptLev);
574
575 /// isValidDebugInfoIntrinsic - Return true if REI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +0000576 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +0000577 bool isValidDebugInfoIntrinsic(DbgRegionEndInst &REI,
578 CodeGenOpt::Level OptLev);
579
580 /// isValidDebugInfoIntrinsic - Return true if DI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +0000581 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +0000582 bool isValidDebugInfoIntrinsic(DbgDeclareInst &DI,
583 CodeGenOpt::Level OptLev);
584
585 /// ExtractDebugLocation - Extract debug location information
586 /// from llvm.dbg.stoppoint intrinsic.
587 DebugLoc ExtractDebugLocation(DbgStopPointInst &SPI,
Devang Patel9e529c32009-07-02 01:15:24 +0000588 DebugLocTracker &DebugLocInfo);
589
590 /// ExtractDebugLocation - Extract debug location information
591 /// from llvm.dbg.func_start intrinsic.
592 DebugLoc ExtractDebugLocation(DbgFuncStartInst &FSI,
Devang Patel9e529c32009-07-02 01:15:24 +0000593 DebugLocTracker &DebugLocInfo);
594
595 /// isInlinedFnStart - Return true if FSI is starting an inlined function.
596 bool isInlinedFnStart(DbgFuncStartInst &FSI, const Function *CurrentFn);
597
598 /// isInlinedFnEnd - Return true if REI is ending an inlined function.
599 bool isInlinedFnEnd(DbgRegionEndInst &REI, const Function *CurrentFn);
Devang Patel27a201d2009-08-06 15:39:34 +0000600 /// DebugInfoFinder - This object collects DebugInfo from a module.
Devang Patel98c65172009-07-30 18:25:15 +0000601 class DebugInfoFinder {
Devang Pateld2f79a12009-07-28 19:55:13 +0000602
603 public:
Devang Patel98c65172009-07-30 18:25:15 +0000604 /// processModule - Process entire module and collect debug info
Devang Pateld2f79a12009-07-28 19:55:13 +0000605 /// anchors.
Devang Patel98c65172009-07-30 18:25:15 +0000606 void processModule(Module &M);
Devang Pateld2f79a12009-07-28 19:55:13 +0000607
608 private:
Devang Patel98c65172009-07-30 18:25:15 +0000609 /// processType - Process DIType.
610 void processType(DIType DT);
Devang Pateld2f79a12009-07-28 19:55:13 +0000611
Devang Patel98c65172009-07-30 18:25:15 +0000612 /// processSubprogram - Enumberate DISubprogram.
613 void processSubprogram(DISubprogram SP);
Devang Pateld2f79a12009-07-28 19:55:13 +0000614
Devang Patel98c65172009-07-30 18:25:15 +0000615 /// processStopPoint - Process DbgStopPointInst.
616 void processStopPoint(DbgStopPointInst *SPI);
Devang Pateld2f79a12009-07-28 19:55:13 +0000617
Devang Patel98c65172009-07-30 18:25:15 +0000618 /// processFuncStart - Process DbgFuncStartInst.
619 void processFuncStart(DbgFuncStartInst *FSI);
Devang Pateld2f79a12009-07-28 19:55:13 +0000620
Devang Patel98c65172009-07-30 18:25:15 +0000621 /// processRegionStart - Process DbgRegionStart.
622 void processRegionStart(DbgRegionStartInst *DRS);
Devang Patele802f1c2009-07-30 17:30:23 +0000623
Devang Patel98c65172009-07-30 18:25:15 +0000624 /// processRegionEnd - Process DbgRegionEnd.
625 void processRegionEnd(DbgRegionEndInst *DRE);
Devang Patele802f1c2009-07-30 17:30:23 +0000626
Devang Patelb4d31302009-07-31 18:18:52 +0000627 /// processDeclare - Process DbgDeclareInst.
628 void processDeclare(DbgDeclareInst *DDI);
629
Devang Pateld2f79a12009-07-28 19:55:13 +0000630 /// addCompileUnit - Add compile unit into CUs.
631 bool addCompileUnit(DICompileUnit CU);
632
633 /// addGlobalVariable - Add global variable into GVs.
634 bool addGlobalVariable(DIGlobalVariable DIG);
635
636 // addSubprogram - Add subprgoram into SPs.
637 bool addSubprogram(DISubprogram SP);
638
Devang Patel72bcdb62009-08-10 22:09:58 +0000639 /// addType - Add type into Tys.
640 bool addType(DIType DT);
641
Devang Pateld2f79a12009-07-28 19:55:13 +0000642 public:
Devang Patele4b27562009-08-28 23:24:31 +0000643 typedef SmallVector<MDNode *, 8>::iterator iterator;
Devang Pateld2f79a12009-07-28 19:55:13 +0000644 iterator compile_unit_begin() { return CUs.begin(); }
645 iterator compile_unit_end() { return CUs.end(); }
646 iterator subprogram_begin() { return SPs.begin(); }
647 iterator subprogram_end() { return SPs.end(); }
648 iterator global_variable_begin() { return GVs.begin(); }
649 iterator global_variable_end() { return GVs.end(); }
Devang Patel72bcdb62009-08-10 22:09:58 +0000650 iterator type_begin() { return TYs.begin(); }
651 iterator type_end() { return TYs.end(); }
Devang Pateld2f79a12009-07-28 19:55:13 +0000652
653 unsigned compile_unit_count() { return CUs.size(); }
654 unsigned global_variable_count() { return GVs.size(); }
655 unsigned subprogram_count() { return SPs.size(); }
Devang Patel72bcdb62009-08-10 22:09:58 +0000656 unsigned type_count() { return TYs.size(); }
Devang Pateld2f79a12009-07-28 19:55:13 +0000657
658 private:
Devang Patele4b27562009-08-28 23:24:31 +0000659 SmallVector<MDNode *, 8> CUs; // Compile Units
660 SmallVector<MDNode *, 8> SPs; // Subprograms
661 SmallVector<MDNode *, 8> GVs; // Global Variables;
662 SmallVector<MDNode *, 8> TYs; // Types
663 SmallPtrSet<MDNode *, 64> NodesSeen;
Devang Pateld2f79a12009-07-28 19:55:13 +0000664 };
Chris Lattnera45664f2008-11-10 02:56:27 +0000665} // end namespace llvm
666
667#endif