blob: 206d04e3749f4581464ff5a4ffe1329b74a2c3a1 [file] [log] [blame]
Bill Wendling523bea82013-11-08 08:13:15 +00001//===--- DebugInfo.cpp - Debug Information Helper Classes -----------------===//
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 implements the helper classes used to build and interpret debug
11// information in LLVM IR form.
12//
13//===----------------------------------------------------------------------===//
14
Chandler Carruth9a4c9e52014-03-06 00:46:21 +000015#include "llvm/IR/DebugInfo.h"
Chandler Carruth442f7842014-03-04 10:07:28 +000016#include "LLVMContextImpl.h"
Bill Wendling523bea82013-11-08 08:13:15 +000017#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/SmallPtrSet.h"
19#include "llvm/ADT/SmallString.h"
20#include "llvm/Analysis/ValueTracking.h"
21#include "llvm/IR/Constants.h"
Adrian Prantlb1416832014-08-01 22:11:58 +000022#include "llvm/IR/DIBuilder.h"
Bill Wendling523bea82013-11-08 08:13:15 +000023#include "llvm/IR/DerivedTypes.h"
24#include "llvm/IR/Instructions.h"
25#include "llvm/IR/IntrinsicInst.h"
26#include "llvm/IR/Intrinsics.h"
27#include "llvm/IR/Module.h"
Chandler Carruth4220e9c2014-03-04 11:17:44 +000028#include "llvm/IR/ValueHandle.h"
Bill Wendling523bea82013-11-08 08:13:15 +000029#include "llvm/Support/Debug.h"
30#include "llvm/Support/Dwarf.h"
Bill Wendling523bea82013-11-08 08:13:15 +000031#include "llvm/Support/raw_ostream.h"
32using namespace llvm;
33using namespace llvm::dwarf;
34
35//===----------------------------------------------------------------------===//
36// DIDescriptor
37//===----------------------------------------------------------------------===//
38
39bool DIDescriptor::Verify() const {
40 return DbgNode &&
41 (DIDerivedType(DbgNode).Verify() ||
42 DICompositeType(DbgNode).Verify() || DIBasicType(DbgNode).Verify() ||
43 DIVariable(DbgNode).Verify() || DISubprogram(DbgNode).Verify() ||
44 DIGlobalVariable(DbgNode).Verify() || DIFile(DbgNode).Verify() ||
45 DICompileUnit(DbgNode).Verify() || DINameSpace(DbgNode).Verify() ||
46 DILexicalBlock(DbgNode).Verify() ||
47 DILexicalBlockFile(DbgNode).Verify() ||
48 DISubrange(DbgNode).Verify() || DIEnumerator(DbgNode).Verify() ||
49 DIObjCProperty(DbgNode).Verify() ||
50 DITemplateTypeParameter(DbgNode).Verify() ||
51 DITemplateValueParameter(DbgNode).Verify() ||
Adrian Prantl87b7eb92014-10-01 18:55:02 +000052 DIImportedEntity(DbgNode).Verify() || DIExpression(DbgNode).Verify());
Bill Wendling523bea82013-11-08 08:13:15 +000053}
54
55static Value *getField(const MDNode *DbgNode, unsigned Elt) {
Craig Topperc6207612014-04-09 06:08:46 +000056 if (!DbgNode || Elt >= DbgNode->getNumOperands())
57 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +000058 return DbgNode->getOperand(Elt);
59}
60
61static MDNode *getNodeField(const MDNode *DbgNode, unsigned Elt) {
62 return dyn_cast_or_null<MDNode>(getField(DbgNode, Elt));
63}
64
65static StringRef getStringField(const MDNode *DbgNode, unsigned Elt) {
66 if (MDString *MDS = dyn_cast_or_null<MDString>(getField(DbgNode, Elt)))
67 return MDS->getString();
68 return StringRef();
69}
70
71StringRef DIDescriptor::getStringField(unsigned Elt) const {
72 return ::getStringField(DbgNode, Elt);
73}
74
75uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
Craig Topperc6207612014-04-09 06:08:46 +000076 if (!DbgNode)
Bill Wendling523bea82013-11-08 08:13:15 +000077 return 0;
78
79 if (Elt < DbgNode->getNumOperands())
80 if (ConstantInt *CI =
81 dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt)))
82 return CI->getZExtValue();
83
84 return 0;
85}
86
87int64_t DIDescriptor::getInt64Field(unsigned Elt) const {
Craig Topperc6207612014-04-09 06:08:46 +000088 if (!DbgNode)
Bill Wendling523bea82013-11-08 08:13:15 +000089 return 0;
90
91 if (Elt < DbgNode->getNumOperands())
92 if (ConstantInt *CI =
93 dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(Elt)))
94 return CI->getSExtValue();
95
96 return 0;
97}
98
99DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
100 MDNode *Field = getNodeField(DbgNode, Elt);
101 return DIDescriptor(Field);
102}
103
104GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
Craig Topperc6207612014-04-09 06:08:46 +0000105 if (!DbgNode)
106 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +0000107
108 if (Elt < DbgNode->getNumOperands())
109 return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt));
Craig Topperc6207612014-04-09 06:08:46 +0000110 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +0000111}
112
113Constant *DIDescriptor::getConstantField(unsigned Elt) const {
Craig Topperc6207612014-04-09 06:08:46 +0000114 if (!DbgNode)
115 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +0000116
117 if (Elt < DbgNode->getNumOperands())
118 return dyn_cast_or_null<Constant>(DbgNode->getOperand(Elt));
Craig Topperc6207612014-04-09 06:08:46 +0000119 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +0000120}
121
122Function *DIDescriptor::getFunctionField(unsigned Elt) const {
Craig Topperc6207612014-04-09 06:08:46 +0000123 if (!DbgNode)
124 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +0000125
126 if (Elt < DbgNode->getNumOperands())
127 return dyn_cast_or_null<Function>(DbgNode->getOperand(Elt));
Craig Topperc6207612014-04-09 06:08:46 +0000128 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +0000129}
130
131void DIDescriptor::replaceFunctionField(unsigned Elt, Function *F) {
Craig Topperc6207612014-04-09 06:08:46 +0000132 if (!DbgNode)
Bill Wendling523bea82013-11-08 08:13:15 +0000133 return;
134
135 if (Elt < DbgNode->getNumOperands()) {
136 MDNode *Node = const_cast<MDNode *>(DbgNode);
137 Node->replaceOperandWith(Elt, F);
138 }
139}
140
Bill Wendling523bea82013-11-08 08:13:15 +0000141/// getInlinedAt - If this variable is inlined then return inline location.
142MDNode *DIVariable::getInlinedAt() const { return getNodeField(DbgNode, 7); }
143
Adrian Prantlb1416832014-08-01 22:11:58 +0000144/// Return the size reported by the variable's type.
145unsigned DIVariable::getSizeInBits(const DITypeIdentifierMap &Map) {
146 DIType Ty = getType().resolve(Map);
147 // Follow derived types until we reach a type that
148 // reports back a size.
149 while (Ty.isDerivedType() && !Ty.getSizeInBits()) {
150 DIDerivedType DT(&*Ty);
151 Ty = DT.getTypeDerivedFrom().resolve(Map);
152 }
153 assert(Ty.getSizeInBits() && "type with size 0");
154 return Ty.getSizeInBits();
155}
156
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000157uint64_t DIExpression::getElement(unsigned Idx) const {
158 unsigned I = Idx + 1;
159 if (I < DbgNode->getNumOperands())
160 if (auto *CI = dyn_cast_or_null<ConstantInt>(DbgNode->getOperand(I)))
161 return CI->getZExtValue();
Adrian Prantlb1416832014-08-01 22:11:58 +0000162
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000163 assert(false && "non-existing complex address element requested");
164 return 0;
165}
Adrian Prantlb1416832014-08-01 22:11:58 +0000166
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000167bool DIExpression::isVariablePiece() const {
168 return getNumElements() && getElement(0) == dwarf::DW_OP_piece;
169}
170
171uint64_t DIExpression::getPieceOffset() const {
172 assert(isVariablePiece());
173 return getElement(1);
174}
175
176uint64_t DIExpression::getPieceSize() const {
177 assert(isVariablePiece());
178 return getElement(2);
179}
Adrian Prantlb1416832014-08-01 22:11:58 +0000180
Bill Wendling523bea82013-11-08 08:13:15 +0000181//===----------------------------------------------------------------------===//
182// Predicates
183//===----------------------------------------------------------------------===//
184
Manman Renf8a19672014-07-28 22:24:06 +0000185bool DIDescriptor::isSubroutineType() const {
186 return isCompositeType() && getTag() == dwarf::DW_TAG_subroutine_type;
187}
188
Bill Wendling523bea82013-11-08 08:13:15 +0000189/// isBasicType - Return true if the specified tag is legal for
190/// DIBasicType.
191bool DIDescriptor::isBasicType() const {
192 if (!DbgNode)
193 return false;
194 switch (getTag()) {
195 case dwarf::DW_TAG_base_type:
196 case dwarf::DW_TAG_unspecified_type:
197 return true;
198 default:
199 return false;
200 }
201}
202
203/// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
204bool DIDescriptor::isDerivedType() const {
205 if (!DbgNode)
206 return false;
207 switch (getTag()) {
208 case dwarf::DW_TAG_typedef:
209 case dwarf::DW_TAG_pointer_type:
210 case dwarf::DW_TAG_ptr_to_member_type:
211 case dwarf::DW_TAG_reference_type:
212 case dwarf::DW_TAG_rvalue_reference_type:
213 case dwarf::DW_TAG_const_type:
214 case dwarf::DW_TAG_volatile_type:
215 case dwarf::DW_TAG_restrict_type:
216 case dwarf::DW_TAG_member:
217 case dwarf::DW_TAG_inheritance:
218 case dwarf::DW_TAG_friend:
219 return true;
220 default:
221 // CompositeTypes are currently modelled as DerivedTypes.
222 return isCompositeType();
223 }
224}
225
226/// isCompositeType - Return true if the specified tag is legal for
227/// DICompositeType.
228bool DIDescriptor::isCompositeType() const {
229 if (!DbgNode)
230 return false;
231 switch (getTag()) {
232 case dwarf::DW_TAG_array_type:
233 case dwarf::DW_TAG_structure_type:
234 case dwarf::DW_TAG_union_type:
235 case dwarf::DW_TAG_enumeration_type:
236 case dwarf::DW_TAG_subroutine_type:
237 case dwarf::DW_TAG_class_type:
238 return true;
239 default:
240 return false;
241 }
242}
243
244/// isVariable - Return true if the specified tag is legal for DIVariable.
245bool DIDescriptor::isVariable() const {
246 if (!DbgNode)
247 return false;
248 switch (getTag()) {
249 case dwarf::DW_TAG_auto_variable:
250 case dwarf::DW_TAG_arg_variable:
251 return true;
252 default:
253 return false;
254 }
255}
256
257/// isType - Return true if the specified tag is legal for DIType.
258bool DIDescriptor::isType() const {
Manman Renf93ac4b2014-07-29 18:20:39 +0000259 return isBasicType() || isCompositeType() || isDerivedType();
Bill Wendling523bea82013-11-08 08:13:15 +0000260}
261
262/// isSubprogram - Return true if the specified tag is legal for
263/// DISubprogram.
264bool DIDescriptor::isSubprogram() const {
265 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
266}
267
268/// isGlobalVariable - Return true if the specified tag is legal for
269/// DIGlobalVariable.
270bool DIDescriptor::isGlobalVariable() const {
271 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
272 getTag() == dwarf::DW_TAG_constant);
273}
274
Bill Wendling523bea82013-11-08 08:13:15 +0000275/// isScope - Return true if the specified tag is one of the scope
276/// related tag.
277bool DIDescriptor::isScope() const {
278 if (!DbgNode)
279 return false;
280 switch (getTag()) {
281 case dwarf::DW_TAG_compile_unit:
282 case dwarf::DW_TAG_lexical_block:
283 case dwarf::DW_TAG_subprogram:
284 case dwarf::DW_TAG_namespace:
285 case dwarf::DW_TAG_file_type:
286 return true;
287 default:
288 break;
289 }
290 return isType();
291}
292
293/// isTemplateTypeParameter - Return true if the specified tag is
294/// DW_TAG_template_type_parameter.
295bool DIDescriptor::isTemplateTypeParameter() const {
296 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
297}
298
299/// isTemplateValueParameter - Return true if the specified tag is
300/// DW_TAG_template_value_parameter.
301bool DIDescriptor::isTemplateValueParameter() const {
302 return DbgNode && (getTag() == dwarf::DW_TAG_template_value_parameter ||
303 getTag() == dwarf::DW_TAG_GNU_template_template_param ||
304 getTag() == dwarf::DW_TAG_GNU_template_parameter_pack);
305}
306
307/// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
308bool DIDescriptor::isCompileUnit() const {
309 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
310}
311
312/// isFile - Return true if the specified tag is DW_TAG_file_type.
313bool DIDescriptor::isFile() const {
314 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
315}
316
317/// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
318bool DIDescriptor::isNameSpace() const {
319 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
320}
321
322/// isLexicalBlockFile - Return true if the specified descriptor is a
323/// lexical block with an extra file.
324bool DIDescriptor::isLexicalBlockFile() const {
325 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
David Blaikie2f3f76f2014-08-21 22:45:21 +0000326 (DbgNode->getNumOperands() == 4);
Bill Wendling523bea82013-11-08 08:13:15 +0000327}
328
329/// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
330bool DIDescriptor::isLexicalBlock() const {
331 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
332 (DbgNode->getNumOperands() > 3);
333}
334
335/// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
336bool DIDescriptor::isSubrange() const {
337 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
338}
339
340/// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
341bool DIDescriptor::isEnumerator() const {
342 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
343}
344
345/// isObjCProperty - Return true if the specified tag is DW_TAG_APPLE_property.
346bool DIDescriptor::isObjCProperty() const {
347 return DbgNode && getTag() == dwarf::DW_TAG_APPLE_property;
348}
349
350/// \brief Return true if the specified tag is DW_TAG_imported_module or
351/// DW_TAG_imported_declaration.
352bool DIDescriptor::isImportedEntity() const {
353 return DbgNode && (getTag() == dwarf::DW_TAG_imported_module ||
354 getTag() == dwarf::DW_TAG_imported_declaration);
355}
356
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000357/// \brief Return true if the specified tag is DW_TAG_imported_module or
358/// DW_TAG_imported_declaration.
359bool DIDescriptor::isExpression() const {
360 return DbgNode && (getTag() == dwarf::DW_TAG_expression);
361}
362
Bill Wendling523bea82013-11-08 08:13:15 +0000363//===----------------------------------------------------------------------===//
364// Simple Descriptor Constructors and other Methods
365//===----------------------------------------------------------------------===//
366
Bill Wendling523bea82013-11-08 08:13:15 +0000367/// replaceAllUsesWith - Replace all uses of the MDNode used by this
368/// type with the one in the passed descriptor.
Frederic Riss36acf0f2014-09-15 07:50:36 +0000369void DIDescriptor::replaceAllUsesWith(LLVMContext &VMContext, DIDescriptor D) {
Bill Wendling523bea82013-11-08 08:13:15 +0000370
371 assert(DbgNode && "Trying to replace an unverified type!");
372
373 // Since we use a TrackingVH for the node, its easy for clients to manufacture
374 // legitimate situations where they want to replaceAllUsesWith() on something
375 // which, due to uniquing, has merged with the source. We shield clients from
376 // this detail by allowing a value to be replaced with replaceAllUsesWith()
377 // itself.
David Blaikied3f094a2014-05-06 03:41:57 +0000378 const MDNode *DN = D;
379 if (DbgNode == DN) {
380 SmallVector<Value*, 10> Ops(DbgNode->getNumOperands());
381 for (size_t i = 0; i != Ops.size(); ++i)
382 Ops[i] = DbgNode->getOperand(i);
383 DN = MDNode::get(VMContext, Ops);
Bill Wendling523bea82013-11-08 08:13:15 +0000384 }
David Blaikied3f094a2014-05-06 03:41:57 +0000385
386 MDNode *Node = const_cast<MDNode *>(DbgNode);
387 const Value *V = cast_or_null<Value>(DN);
388 Node->replaceAllUsesWith(const_cast<Value *>(V));
389 MDNode::deleteTemporary(Node);
Frederic Rissdd7aec52014-09-15 07:50:42 +0000390 DbgNode = DN;
Bill Wendling523bea82013-11-08 08:13:15 +0000391}
392
393/// replaceAllUsesWith - Replace all uses of the MDNode used by this
394/// type with the one in D.
Frederic Riss36acf0f2014-09-15 07:50:36 +0000395void DIDescriptor::replaceAllUsesWith(MDNode *D) {
Bill Wendling523bea82013-11-08 08:13:15 +0000396
397 assert(DbgNode && "Trying to replace an unverified type!");
David Blaikied3f094a2014-05-06 03:41:57 +0000398 assert(DbgNode != D && "This replacement should always happen");
399 MDNode *Node = const_cast<MDNode *>(DbgNode);
400 const MDNode *DN = D;
401 const Value *V = cast_or_null<Value>(DN);
402 Node->replaceAllUsesWith(const_cast<Value *>(V));
403 MDNode::deleteTemporary(Node);
Bill Wendling523bea82013-11-08 08:13:15 +0000404}
405
406/// Verify - Verify that a compile unit is well formed.
407bool DICompileUnit::Verify() const {
408 if (!isCompileUnit())
409 return false;
410
411 // Don't bother verifying the compilation directory or producer string
412 // as those could be empty.
413 if (getFilename().empty())
414 return false;
415
Eric Christopher75d49db2014-02-27 01:24:56 +0000416 return DbgNode->getNumOperands() == 14;
Bill Wendling523bea82013-11-08 08:13:15 +0000417}
418
419/// Verify - Verify that an ObjC property is well formed.
420bool DIObjCProperty::Verify() const {
421 if (!isObjCProperty())
422 return false;
423
424 // Don't worry about the rest of the strings for now.
425 return DbgNode->getNumOperands() == 8;
426}
427
428/// Check if a field at position Elt of a MDNode is a MDNode.
429/// We currently allow an empty string and an integer.
430/// But we don't allow a non-empty string in a MDNode field.
431static bool fieldIsMDNode(const MDNode *DbgNode, unsigned Elt) {
432 // FIXME: This function should return true, if the field is null or the field
433 // is indeed a MDNode: return !Fld || isa<MDNode>(Fld).
434 Value *Fld = getField(DbgNode, Elt);
435 if (Fld && isa<MDString>(Fld) && !cast<MDString>(Fld)->getString().empty())
436 return false;
437 return true;
438}
439
440/// Check if a field at position Elt of a MDNode is a MDString.
441static bool fieldIsMDString(const MDNode *DbgNode, unsigned Elt) {
442 Value *Fld = getField(DbgNode, Elt);
443 return !Fld || isa<MDString>(Fld);
444}
445
446/// Check if a value can be a reference to a type.
447static bool isTypeRef(const Value *Val) {
448 return !Val ||
449 (isa<MDString>(Val) && !cast<MDString>(Val)->getString().empty()) ||
450 (isa<MDNode>(Val) && DIType(cast<MDNode>(Val)).isType());
451}
452
453/// Check if a field at position Elt of a MDNode can be a reference to a type.
454static bool fieldIsTypeRef(const MDNode *DbgNode, unsigned Elt) {
455 Value *Fld = getField(DbgNode, Elt);
456 return isTypeRef(Fld);
457}
458
459/// Check if a value can be a ScopeRef.
460static bool isScopeRef(const Value *Val) {
461 return !Val ||
Adrian Prantl6b444c52014-04-01 21:04:24 +0000462 (isa<MDString>(Val) && !cast<MDString>(Val)->getString().empty()) ||
463 // Not checking for Val->isScope() here, because it would work
464 // only for lexical scopes and not all subclasses of DIScope.
465 isa<MDNode>(Val);
Bill Wendling523bea82013-11-08 08:13:15 +0000466}
467
468/// Check if a field at position Elt of a MDNode can be a ScopeRef.
469static bool fieldIsScopeRef(const MDNode *DbgNode, unsigned Elt) {
470 Value *Fld = getField(DbgNode, Elt);
471 return isScopeRef(Fld);
472}
473
474/// Verify - Verify that a type descriptor is well formed.
475bool DIType::Verify() const {
476 if (!isType())
477 return false;
478 // Make sure Context @ field 2 is MDNode.
479 if (!fieldIsScopeRef(DbgNode, 2))
480 return false;
481
482 // FIXME: Sink this into the various subclass verifies.
483 uint16_t Tag = getTag();
Manman Renf93ac4b2014-07-29 18:20:39 +0000484 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
Bill Wendling523bea82013-11-08 08:13:15 +0000485 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
486 Tag != dwarf::DW_TAG_ptr_to_member_type &&
487 Tag != dwarf::DW_TAG_reference_type &&
488 Tag != dwarf::DW_TAG_rvalue_reference_type &&
489 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_array_type &&
490 Tag != dwarf::DW_TAG_enumeration_type &&
491 Tag != dwarf::DW_TAG_subroutine_type &&
492 Tag != dwarf::DW_TAG_inheritance && Tag != dwarf::DW_TAG_friend &&
493 getFilename().empty())
494 return false;
Adrian Prantldaedfda2014-08-29 22:44:07 +0000495
Bill Wendling523bea82013-11-08 08:13:15 +0000496 // DIType is abstract, it should be a BasicType, a DerivedType or
497 // a CompositeType.
498 if (isBasicType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000499 return DIBasicType(DbgNode).Verify();
Bill Wendling523bea82013-11-08 08:13:15 +0000500 else if (isCompositeType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000501 return DICompositeType(DbgNode).Verify();
Bill Wendling523bea82013-11-08 08:13:15 +0000502 else if (isDerivedType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000503 return DIDerivedType(DbgNode).Verify();
Bill Wendling523bea82013-11-08 08:13:15 +0000504 else
505 return false;
Bill Wendling523bea82013-11-08 08:13:15 +0000506}
507
508/// Verify - Verify that a basic type descriptor is well formed.
509bool DIBasicType::Verify() const {
510 return isBasicType() && DbgNode->getNumOperands() == 10;
511}
512
513/// Verify - Verify that a derived type descriptor is well formed.
514bool DIDerivedType::Verify() const {
515 // Make sure DerivedFrom @ field 9 is TypeRef.
516 if (!fieldIsTypeRef(DbgNode, 9))
517 return false;
518 if (getTag() == dwarf::DW_TAG_ptr_to_member_type)
519 // Make sure ClassType @ field 10 is a TypeRef.
520 if (!fieldIsTypeRef(DbgNode, 10))
521 return false;
522
523 return isDerivedType() && DbgNode->getNumOperands() >= 10 &&
524 DbgNode->getNumOperands() <= 14;
525}
526
527/// Verify - Verify that a composite type descriptor is well formed.
528bool DICompositeType::Verify() const {
529 if (!isCompositeType())
530 return false;
531
532 // Make sure DerivedFrom @ field 9 and ContainingType @ field 12 are TypeRef.
533 if (!fieldIsTypeRef(DbgNode, 9))
534 return false;
535 if (!fieldIsTypeRef(DbgNode, 12))
536 return false;
537
538 // Make sure the type identifier at field 14 is MDString, it can be null.
539 if (!fieldIsMDString(DbgNode, 14))
540 return false;
541
Adrian Prantl99c7af22013-12-18 21:48:19 +0000542 // A subroutine type can't be both & and &&.
543 if (isLValueReference() && isRValueReference())
544 return false;
545
Bill Wendling523bea82013-11-08 08:13:15 +0000546 return DbgNode->getNumOperands() == 15;
547}
548
549/// Verify - Verify that a subprogram descriptor is well formed.
550bool DISubprogram::Verify() const {
551 if (!isSubprogram())
552 return false;
553
554 // Make sure context @ field 2 is a ScopeRef and type @ field 7 is a MDNode.
555 if (!fieldIsScopeRef(DbgNode, 2))
556 return false;
557 if (!fieldIsMDNode(DbgNode, 7))
558 return false;
559 // Containing type @ field 12.
560 if (!fieldIsTypeRef(DbgNode, 12))
561 return false;
Adrian Prantl99c7af22013-12-18 21:48:19 +0000562
563 // A subprogram can't be both & and &&.
564 if (isLValueReference() && isRValueReference())
565 return false;
566
Bill Wendling523bea82013-11-08 08:13:15 +0000567 return DbgNode->getNumOperands() == 20;
568}
569
570/// Verify - Verify that a global variable descriptor is well formed.
571bool DIGlobalVariable::Verify() const {
572 if (!isGlobalVariable())
573 return false;
574
575 if (getDisplayName().empty())
576 return false;
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000577 // Make sure context @ field 2 is an MDNode.
Bill Wendling523bea82013-11-08 08:13:15 +0000578 if (!fieldIsMDNode(DbgNode, 2))
579 return false;
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000580 // Make sure that type @ field 8 is a DITypeRef.
581 if (!fieldIsTypeRef(DbgNode, 8))
Bill Wendling523bea82013-11-08 08:13:15 +0000582 return false;
583 // Make sure StaticDataMemberDeclaration @ field 12 is MDNode.
584 if (!fieldIsMDNode(DbgNode, 12))
585 return false;
586
587 return DbgNode->getNumOperands() == 13;
588}
589
590/// Verify - Verify that a variable descriptor is well formed.
591bool DIVariable::Verify() const {
592 if (!isVariable())
593 return false;
594
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000595 // Make sure context @ field 1 is an MDNode.
Bill Wendling523bea82013-11-08 08:13:15 +0000596 if (!fieldIsMDNode(DbgNode, 1))
597 return false;
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000598 // Make sure that type @ field 5 is a DITypeRef.
599 if (!fieldIsTypeRef(DbgNode, 5))
Bill Wendling523bea82013-11-08 08:13:15 +0000600 return false;
Adrian Prantlda7d92e2014-06-30 17:17:35 +0000601
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000602 // Variable without an inline location.
603 if (DbgNode->getNumOperands() == 7)
Adrian Prantlda7d92e2014-06-30 17:17:35 +0000604 return true;
605
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000606 return DbgNode->getNumOperands() == 8;
607}
608
609/// Verify - Verify that a variable descriptor is well formed.
610bool DIExpression::Verify() const {
611 // Empty DIExpressions may be represented as a nullptr.
612 if (!DbgNode)
613 return true;
614
615 return isExpression();
Bill Wendling523bea82013-11-08 08:13:15 +0000616}
617
618/// Verify - Verify that a location descriptor is well formed.
619bool DILocation::Verify() const {
620 if (!DbgNode)
621 return false;
622
623 return DbgNode->getNumOperands() == 4;
624}
625
626/// Verify - Verify that a namespace descriptor is well formed.
627bool DINameSpace::Verify() const {
628 if (!isNameSpace())
629 return false;
630 return DbgNode->getNumOperands() == 5;
631}
632
633/// \brief Retrieve the MDNode for the directory/file pair.
634MDNode *DIFile::getFileNode() const { return getNodeField(DbgNode, 1); }
635
636/// \brief Verify that the file descriptor is well formed.
637bool DIFile::Verify() const {
638 return isFile() && DbgNode->getNumOperands() == 2;
639}
640
641/// \brief Verify that the enumerator descriptor is well formed.
642bool DIEnumerator::Verify() const {
643 return isEnumerator() && DbgNode->getNumOperands() == 3;
644}
645
646/// \brief Verify that the subrange descriptor is well formed.
647bool DISubrange::Verify() const {
648 return isSubrange() && DbgNode->getNumOperands() == 3;
649}
650
651/// \brief Verify that the lexical block descriptor is well formed.
652bool DILexicalBlock::Verify() const {
David Blaikie2f3f76f2014-08-21 22:45:21 +0000653 return isLexicalBlock() && DbgNode->getNumOperands() == 6;
Bill Wendling523bea82013-11-08 08:13:15 +0000654}
655
656/// \brief Verify that the file-scoped lexical block descriptor is well formed.
657bool DILexicalBlockFile::Verify() const {
David Blaikie2f3f76f2014-08-21 22:45:21 +0000658 return isLexicalBlockFile() && DbgNode->getNumOperands() == 4;
Bill Wendling523bea82013-11-08 08:13:15 +0000659}
660
661/// \brief Verify that the template type parameter descriptor is well formed.
662bool DITemplateTypeParameter::Verify() const {
663 return isTemplateTypeParameter() && DbgNode->getNumOperands() == 7;
664}
665
666/// \brief Verify that the template value parameter descriptor is well formed.
667bool DITemplateValueParameter::Verify() const {
668 return isTemplateValueParameter() && DbgNode->getNumOperands() == 8;
669}
670
671/// \brief Verify that the imported module descriptor is well formed.
672bool DIImportedEntity::Verify() const {
673 return isImportedEntity() &&
674 (DbgNode->getNumOperands() == 4 || DbgNode->getNumOperands() == 5);
675}
676
677/// getObjCProperty - Return property node, if this ivar is associated with one.
678MDNode *DIDerivedType::getObjCProperty() const {
679 return getNodeField(DbgNode, 10);
680}
681
682MDString *DICompositeType::getIdentifier() const {
683 return cast_or_null<MDString>(getField(DbgNode, 14));
684}
685
686#ifndef NDEBUG
687static void VerifySubsetOf(const MDNode *LHS, const MDNode *RHS) {
688 for (unsigned i = 0; i != LHS->getNumOperands(); ++i) {
689 // Skip the 'empty' list (that's a single i32 0, rather than truly empty).
690 if (i == 0 && isa<ConstantInt>(LHS->getOperand(i)))
691 continue;
692 const MDNode *E = cast<MDNode>(LHS->getOperand(i));
693 bool found = false;
694 for (unsigned j = 0; !found && j != RHS->getNumOperands(); ++j)
695 found = E == RHS->getOperand(j);
696 assert(found && "Losing a member during member list replacement");
697 }
698}
699#endif
700
701/// \brief Set the array of member DITypes.
Manman Ren1a125c92014-07-28 19:33:20 +0000702void DICompositeType::setArraysHelper(MDNode *Elements, MDNode *TParams) {
Bill Wendling523bea82013-11-08 08:13:15 +0000703 TrackingVH<MDNode> N(*this);
704 if (Elements) {
705#ifndef NDEBUG
706 // Check that the new list of members contains all the old members as well.
707 if (const MDNode *El = cast_or_null<MDNode>(N->getOperand(10)))
708 VerifySubsetOf(El, Elements);
709#endif
710 N->replaceOperandWith(10, Elements);
711 }
712 if (TParams)
713 N->replaceOperandWith(13, TParams);
714 DbgNode = N;
715}
716
Bill Wendling523bea82013-11-08 08:13:15 +0000717/// Generate a reference to this DIType. Uses the type identifier instead
718/// of the actual MDNode if possible, to help type uniquing.
719DIScopeRef DIScope::getRef() const {
720 if (!isCompositeType())
721 return DIScopeRef(*this);
722 DICompositeType DTy(DbgNode);
723 if (!DTy.getIdentifier())
724 return DIScopeRef(*this);
725 return DIScopeRef(DTy.getIdentifier());
726}
727
728/// \brief Set the containing type.
729void DICompositeType::setContainingType(DICompositeType ContainingType) {
730 TrackingVH<MDNode> N(*this);
731 N->replaceOperandWith(12, ContainingType.getRef());
732 DbgNode = N;
733}
734
735/// isInlinedFnArgument - Return true if this variable provides debugging
736/// information for an inlined function arguments.
737bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
738 assert(CurFn && "Invalid function");
739 if (!getContext().isSubprogram())
740 return false;
741 // This variable is not inlined function argument if its scope
742 // does not describe current function.
743 return !DISubprogram(getContext()).describes(CurFn);
744}
745
746/// describes - Return true if this subprogram provides debugging
747/// information for the function F.
748bool DISubprogram::describes(const Function *F) {
749 assert(F && "Invalid function");
750 if (F == getFunction())
751 return true;
752 StringRef Name = getLinkageName();
753 if (Name.empty())
754 Name = getName();
755 if (F->getName() == Name)
756 return true;
757 return false;
758}
759
760unsigned DISubprogram::isOptimized() const {
761 assert(DbgNode && "Invalid subprogram descriptor!");
762 if (DbgNode->getNumOperands() == 15)
763 return getUnsignedField(14);
764 return 0;
765}
766
767MDNode *DISubprogram::getVariablesNodes() const {
768 return getNodeField(DbgNode, 18);
769}
770
771DIArray DISubprogram::getVariables() const {
772 return DIArray(getNodeField(DbgNode, 18));
773}
774
775Value *DITemplateValueParameter::getValue() const {
776 return getField(DbgNode, 4);
777}
778
779// If the current node has a parent scope then return that,
780// else return an empty scope.
781DIScopeRef DIScope::getContext() const {
782
783 if (isType())
784 return DIType(DbgNode).getContext();
785
786 if (isSubprogram())
787 return DIScopeRef(DISubprogram(DbgNode).getContext());
788
789 if (isLexicalBlock())
790 return DIScopeRef(DILexicalBlock(DbgNode).getContext());
791
792 if (isLexicalBlockFile())
793 return DIScopeRef(DILexicalBlockFile(DbgNode).getContext());
794
795 if (isNameSpace())
796 return DIScopeRef(DINameSpace(DbgNode).getContext());
797
798 assert((isFile() || isCompileUnit()) && "Unhandled type of scope.");
Craig Topperc6207612014-04-09 06:08:46 +0000799 return DIScopeRef(nullptr);
Bill Wendling523bea82013-11-08 08:13:15 +0000800}
801
802// If the scope node has a name, return that, else return an empty string.
803StringRef DIScope::getName() const {
804 if (isType())
805 return DIType(DbgNode).getName();
806 if (isSubprogram())
807 return DISubprogram(DbgNode).getName();
808 if (isNameSpace())
809 return DINameSpace(DbgNode).getName();
810 assert((isLexicalBlock() || isLexicalBlockFile() || isFile() ||
811 isCompileUnit()) &&
812 "Unhandled type of scope.");
813 return StringRef();
814}
815
816StringRef DIScope::getFilename() const {
817 if (!DbgNode)
818 return StringRef();
819 return ::getStringField(getNodeField(DbgNode, 1), 0);
820}
821
822StringRef DIScope::getDirectory() const {
823 if (!DbgNode)
824 return StringRef();
825 return ::getStringField(getNodeField(DbgNode, 1), 1);
826}
827
828DIArray DICompileUnit::getEnumTypes() const {
829 if (!DbgNode || DbgNode->getNumOperands() < 13)
830 return DIArray();
831
832 return DIArray(getNodeField(DbgNode, 7));
833}
834
835DIArray DICompileUnit::getRetainedTypes() const {
836 if (!DbgNode || DbgNode->getNumOperands() < 13)
837 return DIArray();
838
839 return DIArray(getNodeField(DbgNode, 8));
840}
841
842DIArray DICompileUnit::getSubprograms() const {
843 if (!DbgNode || DbgNode->getNumOperands() < 13)
844 return DIArray();
845
846 return DIArray(getNodeField(DbgNode, 9));
847}
848
849DIArray DICompileUnit::getGlobalVariables() const {
850 if (!DbgNode || DbgNode->getNumOperands() < 13)
851 return DIArray();
852
853 return DIArray(getNodeField(DbgNode, 10));
854}
855
856DIArray DICompileUnit::getImportedEntities() const {
857 if (!DbgNode || DbgNode->getNumOperands() < 13)
858 return DIArray();
859
860 return DIArray(getNodeField(DbgNode, 11));
861}
862
Diego Novillof5041ce2014-03-03 20:06:11 +0000863/// copyWithNewScope - Return a copy of this location, replacing the
864/// current scope with the given one.
865DILocation DILocation::copyWithNewScope(LLVMContext &Ctx,
David Blaikie2f3f76f2014-08-21 22:45:21 +0000866 DILexicalBlockFile NewScope) {
Diego Novillof5041ce2014-03-03 20:06:11 +0000867 SmallVector<Value *, 10> Elts;
868 assert(Verify());
869 for (unsigned I = 0; I < DbgNode->getNumOperands(); ++I) {
870 if (I != 2)
871 Elts.push_back(DbgNode->getOperand(I));
872 else
873 Elts.push_back(NewScope);
874 }
875 MDNode *NewDIL = MDNode::get(Ctx, Elts);
876 return DILocation(NewDIL);
877}
878
879/// computeNewDiscriminator - Generate a new discriminator value for this
880/// file and line location.
881unsigned DILocation::computeNewDiscriminator(LLVMContext &Ctx) {
882 std::pair<const char *, unsigned> Key(getFilename().data(), getLineNumber());
883 return ++Ctx.pImpl->DiscriminatorTable[Key];
884}
885
Bill Wendling523bea82013-11-08 08:13:15 +0000886/// fixupSubprogramName - Replace contains special characters used
887/// in a typical Objective-C names with '.' in a given string.
888static void fixupSubprogramName(DISubprogram Fn, SmallVectorImpl<char> &Out) {
889 StringRef FName =
890 Fn.getFunction() ? Fn.getFunction()->getName() : Fn.getName();
891 FName = Function::getRealLinkageName(FName);
892
893 StringRef Prefix("llvm.dbg.lv.");
894 Out.reserve(FName.size() + Prefix.size());
895 Out.append(Prefix.begin(), Prefix.end());
896
897 bool isObjCLike = false;
898 for (size_t i = 0, e = FName.size(); i < e; ++i) {
899 char C = FName[i];
900 if (C == '[')
901 isObjCLike = true;
902
903 if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' ||
904 C == '+' || C == '(' || C == ')'))
905 Out.push_back('.');
906 else
907 Out.push_back(C);
908 }
909}
910
911/// getFnSpecificMDNode - Return a NameMDNode, if available, that is
912/// suitable to hold function specific information.
913NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, DISubprogram Fn) {
914 SmallString<32> Name;
915 fixupSubprogramName(Fn, Name);
916 return M.getNamedMetadata(Name.str());
917}
918
919/// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
920/// to hold function specific information.
921NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, DISubprogram Fn) {
922 SmallString<32> Name;
923 fixupSubprogramName(Fn, Name);
924 return M.getOrInsertNamedMetadata(Name.str());
925}
926
927/// createInlinedVariable - Create a new inlined variable based on current
928/// variable.
929/// @param DV Current Variable.
930/// @param InlinedScope Location at current variable is inlined.
931DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
932 LLVMContext &VMContext) {
933 SmallVector<Value *, 16> Elts;
934 // Insert inlined scope as 7th element.
935 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
936 i == 7 ? Elts.push_back(InlinedScope) : Elts.push_back(DV->getOperand(i));
937 return DIVariable(MDNode::get(VMContext, Elts));
938}
939
940/// cleanseInlinedVariable - Remove inlined scope from the variable.
941DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
942 SmallVector<Value *, 16> Elts;
943 // Insert inlined scope as 7th element.
944 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
945 i == 7 ? Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)))
946 : Elts.push_back(DV->getOperand(i));
947 return DIVariable(MDNode::get(VMContext, Elts));
948}
949
950/// getDISubprogram - Find subprogram that is enclosing this scope.
951DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
952 DIDescriptor D(Scope);
953 if (D.isSubprogram())
954 return DISubprogram(Scope);
955
956 if (D.isLexicalBlockFile())
957 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
958
959 if (D.isLexicalBlock())
960 return getDISubprogram(DILexicalBlock(Scope).getContext());
961
962 return DISubprogram();
963}
964
965/// getDICompositeType - Find underlying composite type.
966DICompositeType llvm::getDICompositeType(DIType T) {
967 if (T.isCompositeType())
968 return DICompositeType(T);
969
970 if (T.isDerivedType()) {
971 // This function is currently used by dragonegg and dragonegg does
972 // not generate identifier for types, so using an empty map to resolve
973 // DerivedFrom should be fine.
974 DITypeIdentifierMap EmptyMap;
975 return getDICompositeType(
976 DIDerivedType(T).getTypeDerivedFrom().resolve(EmptyMap));
977 }
978
979 return DICompositeType();
980}
981
982/// Update DITypeIdentifierMap by going through retained types of each CU.
983DITypeIdentifierMap
984llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
985 DITypeIdentifierMap Map;
986 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
987 DICompileUnit CU(CU_Nodes->getOperand(CUi));
988 DIArray Retain = CU.getRetainedTypes();
989 for (unsigned Ti = 0, Te = Retain.getNumElements(); Ti != Te; ++Ti) {
990 if (!Retain.getElement(Ti).isCompositeType())
991 continue;
992 DICompositeType Ty(Retain.getElement(Ti));
993 if (MDString *TypeId = Ty.getIdentifier()) {
994 // Definition has priority over declaration.
995 // Try to insert (TypeId, Ty) to Map.
996 std::pair<DITypeIdentifierMap::iterator, bool> P =
997 Map.insert(std::make_pair(TypeId, Ty));
998 // If TypeId already exists in Map and this is a definition, replace
999 // whatever we had (declaration or definition) with the definition.
1000 if (!P.second && !Ty.isForwardDecl())
1001 P.first->second = Ty;
1002 }
1003 }
1004 }
1005 return Map;
1006}
1007
1008//===----------------------------------------------------------------------===//
1009// DebugInfoFinder implementations.
1010//===----------------------------------------------------------------------===//
1011
1012void DebugInfoFinder::reset() {
1013 CUs.clear();
1014 SPs.clear();
1015 GVs.clear();
1016 TYs.clear();
1017 Scopes.clear();
1018 NodesSeen.clear();
1019 TypeIdentifierMap.clear();
Manman Ren2085ccc2013-11-17 18:42:37 +00001020 TypeMapInitialized = false;
1021}
1022
Manman Renb46e5502013-11-17 19:35:03 +00001023void DebugInfoFinder::InitializeTypeMap(const Module &M) {
Manman Ren2085ccc2013-11-17 18:42:37 +00001024 if (!TypeMapInitialized)
1025 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
1026 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
1027 TypeMapInitialized = true;
1028 }
Bill Wendling523bea82013-11-08 08:13:15 +00001029}
1030
1031/// processModule - Process entire module and collect debug info.
1032void DebugInfoFinder::processModule(const Module &M) {
Manman Renb46e5502013-11-17 19:35:03 +00001033 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001034 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
Bill Wendling523bea82013-11-08 08:13:15 +00001035 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
1036 DICompileUnit CU(CU_Nodes->getOperand(i));
1037 addCompileUnit(CU);
1038 DIArray GVs = CU.getGlobalVariables();
1039 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
1040 DIGlobalVariable DIG(GVs.getElement(i));
1041 if (addGlobalVariable(DIG)) {
1042 processScope(DIG.getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +00001043 processType(DIG.getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +00001044 }
1045 }
1046 DIArray SPs = CU.getSubprograms();
1047 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
1048 processSubprogram(DISubprogram(SPs.getElement(i)));
1049 DIArray EnumTypes = CU.getEnumTypes();
1050 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
1051 processType(DIType(EnumTypes.getElement(i)));
1052 DIArray RetainedTypes = CU.getRetainedTypes();
1053 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
1054 processType(DIType(RetainedTypes.getElement(i)));
1055 DIArray Imports = CU.getImportedEntities();
1056 for (unsigned i = 0, e = Imports.getNumElements(); i != e; ++i) {
1057 DIImportedEntity Import = DIImportedEntity(Imports.getElement(i));
Adrian Prantld09ba232014-04-01 03:41:04 +00001058 DIDescriptor Entity = Import.getEntity().resolve(TypeIdentifierMap);
Bill Wendling523bea82013-11-08 08:13:15 +00001059 if (Entity.isType())
1060 processType(DIType(Entity));
1061 else if (Entity.isSubprogram())
1062 processSubprogram(DISubprogram(Entity));
1063 else if (Entity.isNameSpace())
1064 processScope(DINameSpace(Entity).getContext());
1065 }
1066 }
1067 }
1068}
1069
1070/// processLocation - Process DILocation.
Manman Ren2085ccc2013-11-17 18:42:37 +00001071void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) {
Bill Wendling523bea82013-11-08 08:13:15 +00001072 if (!Loc)
1073 return;
Manman Renb46e5502013-11-17 19:35:03 +00001074 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001075 processScope(Loc.getScope());
Manman Ren2085ccc2013-11-17 18:42:37 +00001076 processLocation(M, Loc.getOrigLocation());
Bill Wendling523bea82013-11-08 08:13:15 +00001077}
1078
1079/// processType - Process DIType.
1080void DebugInfoFinder::processType(DIType DT) {
1081 if (!addType(DT))
1082 return;
1083 processScope(DT.getContext().resolve(TypeIdentifierMap));
1084 if (DT.isCompositeType()) {
1085 DICompositeType DCT(DT);
1086 processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
Manman Renf8a19672014-07-28 22:24:06 +00001087 if (DT.isSubroutineType()) {
1088 DITypeArray DTA = DISubroutineType(DT).getTypeArray();
1089 for (unsigned i = 0, e = DTA.getNumElements(); i != e; ++i)
1090 processType(DTA.getElement(i).resolve(TypeIdentifierMap));
1091 return;
1092 }
Manman Renab8ffba2014-07-28 19:14:13 +00001093 DIArray DA = DCT.getElements();
Bill Wendling523bea82013-11-08 08:13:15 +00001094 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1095 DIDescriptor D = DA.getElement(i);
1096 if (D.isType())
1097 processType(DIType(D));
1098 else if (D.isSubprogram())
1099 processSubprogram(DISubprogram(D));
1100 }
1101 } else if (DT.isDerivedType()) {
1102 DIDerivedType DDT(DT);
1103 processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
1104 }
1105}
1106
1107void DebugInfoFinder::processScope(DIScope Scope) {
1108 if (Scope.isType()) {
1109 DIType Ty(Scope);
1110 processType(Ty);
1111 return;
1112 }
1113 if (Scope.isCompileUnit()) {
1114 addCompileUnit(DICompileUnit(Scope));
1115 return;
1116 }
1117 if (Scope.isSubprogram()) {
1118 processSubprogram(DISubprogram(Scope));
1119 return;
1120 }
1121 if (!addScope(Scope))
1122 return;
1123 if (Scope.isLexicalBlock()) {
1124 DILexicalBlock LB(Scope);
1125 processScope(LB.getContext());
1126 } else if (Scope.isLexicalBlockFile()) {
1127 DILexicalBlockFile LBF = DILexicalBlockFile(Scope);
1128 processScope(LBF.getScope());
1129 } else if (Scope.isNameSpace()) {
1130 DINameSpace NS(Scope);
1131 processScope(NS.getContext());
1132 }
1133}
1134
Bill Wendling523bea82013-11-08 08:13:15 +00001135/// processSubprogram - Process DISubprogram.
1136void DebugInfoFinder::processSubprogram(DISubprogram SP) {
1137 if (!addSubprogram(SP))
1138 return;
1139 processScope(SP.getContext().resolve(TypeIdentifierMap));
1140 processType(SP.getType());
1141 DIArray TParams = SP.getTemplateParams();
1142 for (unsigned I = 0, E = TParams.getNumElements(); I != E; ++I) {
1143 DIDescriptor Element = TParams.getElement(I);
1144 if (Element.isTemplateTypeParameter()) {
1145 DITemplateTypeParameter TType(Element);
1146 processScope(TType.getContext().resolve(TypeIdentifierMap));
1147 processType(TType.getType().resolve(TypeIdentifierMap));
1148 } else if (Element.isTemplateValueParameter()) {
1149 DITemplateValueParameter TVal(Element);
1150 processScope(TVal.getContext().resolve(TypeIdentifierMap));
1151 processType(TVal.getType().resolve(TypeIdentifierMap));
1152 }
1153 }
1154}
1155
1156/// processDeclare - Process DbgDeclareInst.
Manman Ren2085ccc2013-11-17 18:42:37 +00001157void DebugInfoFinder::processDeclare(const Module &M,
1158 const DbgDeclareInst *DDI) {
Bill Wendling523bea82013-11-08 08:13:15 +00001159 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1160 if (!N)
1161 return;
Manman Renb46e5502013-11-17 19:35:03 +00001162 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001163
1164 DIDescriptor DV(N);
1165 if (!DV.isVariable())
1166 return;
1167
1168 if (!NodesSeen.insert(DV))
1169 return;
1170 processScope(DIVariable(N).getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +00001171 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +00001172}
1173
Manman Ren2085ccc2013-11-17 18:42:37 +00001174void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
Bill Wendling523bea82013-11-08 08:13:15 +00001175 MDNode *N = dyn_cast<MDNode>(DVI->getVariable());
1176 if (!N)
1177 return;
Manman Renb46e5502013-11-17 19:35:03 +00001178 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001179
1180 DIDescriptor DV(N);
1181 if (!DV.isVariable())
1182 return;
1183
1184 if (!NodesSeen.insert(DV))
1185 return;
1186 processScope(DIVariable(N).getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +00001187 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +00001188}
1189
1190/// addType - Add type into Tys.
1191bool DebugInfoFinder::addType(DIType DT) {
1192 if (!DT)
1193 return false;
1194
1195 if (!NodesSeen.insert(DT))
1196 return false;
1197
1198 TYs.push_back(DT);
1199 return true;
1200}
1201
1202/// addCompileUnit - Add compile unit into CUs.
1203bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1204 if (!CU)
1205 return false;
1206 if (!NodesSeen.insert(CU))
1207 return false;
1208
1209 CUs.push_back(CU);
1210 return true;
1211}
1212
1213/// addGlobalVariable - Add global variable into GVs.
1214bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1215 if (!DIG)
1216 return false;
1217
1218 if (!NodesSeen.insert(DIG))
1219 return false;
1220
1221 GVs.push_back(DIG);
1222 return true;
1223}
1224
1225// addSubprogram - Add subprgoram into SPs.
1226bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1227 if (!SP)
1228 return false;
1229
1230 if (!NodesSeen.insert(SP))
1231 return false;
1232
1233 SPs.push_back(SP);
1234 return true;
1235}
1236
1237bool DebugInfoFinder::addScope(DIScope Scope) {
1238 if (!Scope)
1239 return false;
1240 // FIXME: Ocaml binding generates a scope with no content, we treat it
1241 // as null for now.
1242 if (Scope->getNumOperands() == 0)
1243 return false;
1244 if (!NodesSeen.insert(Scope))
1245 return false;
1246 Scopes.push_back(Scope);
1247 return true;
1248}
1249
1250//===----------------------------------------------------------------------===//
1251// DIDescriptor: dump routines for all descriptors.
1252//===----------------------------------------------------------------------===//
1253
1254/// dump - Print descriptor to dbgs() with a newline.
1255void DIDescriptor::dump() const {
1256 print(dbgs());
1257 dbgs() << '\n';
1258}
1259
1260/// print - Print descriptor.
1261void DIDescriptor::print(raw_ostream &OS) const {
1262 if (!DbgNode)
1263 return;
1264
1265 if (const char *Tag = dwarf::TagString(getTag()))
1266 OS << "[ " << Tag << " ]";
1267
1268 if (this->isSubrange()) {
1269 DISubrange(DbgNode).printInternal(OS);
1270 } else if (this->isCompileUnit()) {
1271 DICompileUnit(DbgNode).printInternal(OS);
1272 } else if (this->isFile()) {
1273 DIFile(DbgNode).printInternal(OS);
1274 } else if (this->isEnumerator()) {
1275 DIEnumerator(DbgNode).printInternal(OS);
1276 } else if (this->isBasicType()) {
1277 DIType(DbgNode).printInternal(OS);
1278 } else if (this->isDerivedType()) {
1279 DIDerivedType(DbgNode).printInternal(OS);
1280 } else if (this->isCompositeType()) {
1281 DICompositeType(DbgNode).printInternal(OS);
1282 } else if (this->isSubprogram()) {
1283 DISubprogram(DbgNode).printInternal(OS);
1284 } else if (this->isGlobalVariable()) {
1285 DIGlobalVariable(DbgNode).printInternal(OS);
1286 } else if (this->isVariable()) {
1287 DIVariable(DbgNode).printInternal(OS);
1288 } else if (this->isObjCProperty()) {
1289 DIObjCProperty(DbgNode).printInternal(OS);
1290 } else if (this->isNameSpace()) {
1291 DINameSpace(DbgNode).printInternal(OS);
1292 } else if (this->isScope()) {
1293 DIScope(DbgNode).printInternal(OS);
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001294 } else if (this->isExpression()) {
1295 DIExpression(DbgNode).printInternal(OS);
Bill Wendling523bea82013-11-08 08:13:15 +00001296 }
1297}
1298
1299void DISubrange::printInternal(raw_ostream &OS) const {
1300 int64_t Count = getCount();
1301 if (Count != -1)
1302 OS << " [" << getLo() << ", " << Count - 1 << ']';
1303 else
1304 OS << " [unbounded]";
1305}
1306
1307void DIScope::printInternal(raw_ostream &OS) const {
1308 OS << " [" << getDirectory() << "/" << getFilename() << ']';
1309}
1310
1311void DICompileUnit::printInternal(raw_ostream &OS) const {
1312 DIScope::printInternal(OS);
1313 OS << " [";
1314 unsigned Lang = getLanguage();
1315 if (const char *LangStr = dwarf::LanguageString(Lang))
1316 OS << LangStr;
1317 else
1318 (OS << "lang 0x").write_hex(Lang);
1319 OS << ']';
1320}
1321
1322void DIEnumerator::printInternal(raw_ostream &OS) const {
1323 OS << " [" << getName() << " :: " << getEnumValue() << ']';
1324}
1325
1326void DIType::printInternal(raw_ostream &OS) const {
Manman Renf93ac4b2014-07-29 18:20:39 +00001327 if (!DbgNode)
Bill Wendling523bea82013-11-08 08:13:15 +00001328 return;
1329
1330 StringRef Res = getName();
1331 if (!Res.empty())
1332 OS << " [" << Res << "]";
1333
1334 // TODO: Print context?
1335
1336 OS << " [line " << getLineNumber() << ", size " << getSizeInBits()
1337 << ", align " << getAlignInBits() << ", offset " << getOffsetInBits();
1338 if (isBasicType())
1339 if (const char *Enc =
1340 dwarf::AttributeEncodingString(DIBasicType(DbgNode).getEncoding()))
1341 OS << ", enc " << Enc;
1342 OS << "]";
1343
1344 if (isPrivate())
1345 OS << " [private]";
1346 else if (isProtected())
1347 OS << " [protected]";
Adrian Prantldaedfda2014-08-29 22:44:07 +00001348 else if (isPublic())
1349 OS << " [public]";
Bill Wendling523bea82013-11-08 08:13:15 +00001350
1351 if (isArtificial())
1352 OS << " [artificial]";
1353
1354 if (isForwardDecl())
1355 OS << " [decl]";
1356 else if (getTag() == dwarf::DW_TAG_structure_type ||
1357 getTag() == dwarf::DW_TAG_union_type ||
1358 getTag() == dwarf::DW_TAG_enumeration_type ||
1359 getTag() == dwarf::DW_TAG_class_type)
1360 OS << " [def]";
1361 if (isVector())
1362 OS << " [vector]";
1363 if (isStaticMember())
1364 OS << " [static]";
Adrian Prantl99c7af22013-12-18 21:48:19 +00001365
1366 if (isLValueReference())
1367 OS << " [reference]";
1368
1369 if (isRValueReference())
1370 OS << " [rvalue reference]";
Bill Wendling523bea82013-11-08 08:13:15 +00001371}
1372
1373void DIDerivedType::printInternal(raw_ostream &OS) const {
1374 DIType::printInternal(OS);
1375 OS << " [from " << getTypeDerivedFrom().getName() << ']';
1376}
1377
1378void DICompositeType::printInternal(raw_ostream &OS) const {
1379 DIType::printInternal(OS);
Manman Renab8ffba2014-07-28 19:14:13 +00001380 DIArray A = getElements();
Bill Wendling523bea82013-11-08 08:13:15 +00001381 OS << " [" << A.getNumElements() << " elements]";
1382}
1383
1384void DINameSpace::printInternal(raw_ostream &OS) const {
1385 StringRef Name = getName();
1386 if (!Name.empty())
1387 OS << " [" << Name << ']';
1388
1389 OS << " [line " << getLineNumber() << ']';
1390}
1391
1392void DISubprogram::printInternal(raw_ostream &OS) const {
1393 // TODO : Print context
1394 OS << " [line " << getLineNumber() << ']';
1395
1396 if (isLocalToUnit())
1397 OS << " [local]";
1398
1399 if (isDefinition())
1400 OS << " [def]";
1401
1402 if (getScopeLineNumber() != getLineNumber())
1403 OS << " [scope " << getScopeLineNumber() << "]";
1404
1405 if (isPrivate())
1406 OS << " [private]";
1407 else if (isProtected())
1408 OS << " [protected]";
Adrian Prantldaedfda2014-08-29 22:44:07 +00001409 else if (isPublic())
1410 OS << " [public]";
Bill Wendling523bea82013-11-08 08:13:15 +00001411
Adrian Prantl99c7af22013-12-18 21:48:19 +00001412 if (isLValueReference())
1413 OS << " [reference]";
1414
1415 if (isRValueReference())
1416 OS << " [rvalue reference]";
1417
Bill Wendling523bea82013-11-08 08:13:15 +00001418 StringRef Res = getName();
1419 if (!Res.empty())
1420 OS << " [" << Res << ']';
1421}
1422
1423void DIGlobalVariable::printInternal(raw_ostream &OS) const {
1424 StringRef Res = getName();
1425 if (!Res.empty())
1426 OS << " [" << Res << ']';
1427
1428 OS << " [line " << getLineNumber() << ']';
1429
1430 // TODO : Print context
1431
1432 if (isLocalToUnit())
1433 OS << " [local]";
1434
1435 if (isDefinition())
1436 OS << " [def]";
1437}
1438
1439void DIVariable::printInternal(raw_ostream &OS) const {
1440 StringRef Res = getName();
1441 if (!Res.empty())
1442 OS << " [" << Res << ']';
1443
1444 OS << " [line " << getLineNumber() << ']';
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001445}
Adrian Prantlb1416832014-08-01 22:11:58 +00001446
Adrian Prantl87b7eb92014-10-01 18:55:02 +00001447void DIExpression::printInternal(raw_ostream &OS) const {
1448 for (unsigned I = 0; I < getNumElements(); ++I) {
1449 uint64_t OpCode = getElement(I);
1450 OS << " [" << OperationEncodingString(OpCode);
1451 switch (OpCode) {
1452 case DW_OP_plus: {
1453 OS << " " << getElement(++I);
1454 break;
1455 }
1456 case DW_OP_piece: {
1457 unsigned Offset = getElement(++I);
1458 unsigned Size = getElement(++I);
1459 OS << " offset=" << Offset << ", size= " << Size;
1460 break;
1461 }
1462 default:
1463 break;
1464 }
1465 OS << "]";
1466 }
Bill Wendling523bea82013-11-08 08:13:15 +00001467}
1468
1469void DIObjCProperty::printInternal(raw_ostream &OS) const {
1470 StringRef Name = getObjCPropertyName();
1471 if (!Name.empty())
1472 OS << " [" << Name << ']';
1473
1474 OS << " [line " << getLineNumber() << ", properties " << getUnsignedField(6)
1475 << ']';
1476}
1477
1478static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
1479 const LLVMContext &Ctx) {
1480 if (!DL.isUnknown()) { // Print source line info.
1481 DIScope Scope(DL.getScope(Ctx));
1482 assert(Scope.isScope() && "Scope of a DebugLoc should be a DIScope.");
1483 // Omit the directory, because it's likely to be long and uninteresting.
1484 CommentOS << Scope.getFilename();
1485 CommentOS << ':' << DL.getLine();
1486 if (DL.getCol() != 0)
1487 CommentOS << ':' << DL.getCol();
1488 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
1489 if (!InlinedAtDL.isUnknown()) {
1490 CommentOS << " @[ ";
1491 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
1492 CommentOS << " ]";
1493 }
1494 }
1495}
1496
1497void DIVariable::printExtendedName(raw_ostream &OS) const {
1498 const LLVMContext &Ctx = DbgNode->getContext();
1499 StringRef Res = getName();
1500 if (!Res.empty())
1501 OS << Res << "," << getLineNumber();
1502 if (MDNode *InlinedAt = getInlinedAt()) {
1503 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
1504 if (!InlinedAtDL.isUnknown()) {
1505 OS << " @[";
1506 printDebugLoc(InlinedAtDL, OS, Ctx);
1507 OS << "]";
1508 }
1509 }
1510}
1511
1512/// Specialize constructor to make sure it has the correct type.
1513template <> DIRef<DIScope>::DIRef(const Value *V) : Val(V) {
1514 assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode");
1515}
1516template <> DIRef<DIType>::DIRef(const Value *V) : Val(V) {
1517 assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode");
1518}
1519
1520/// Specialize getFieldAs to handle fields that are references to DIScopes.
1521template <>
1522DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const {
1523 return DIScopeRef(getField(DbgNode, Elt));
1524}
1525/// Specialize getFieldAs to handle fields that are references to DITypes.
1526template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const {
1527 return DITypeRef(getField(DbgNode, Elt));
1528}
Manman Rencb14bbc2013-11-22 22:06:31 +00001529
1530/// Strip debug info in the module if it exists.
1531/// To do this, we remove all calls to the debugger intrinsics and any named
1532/// metadata for debugging. We also remove debug locations for instructions.
1533/// Return true if module is modified.
1534bool llvm::StripDebugInfo(Module &M) {
1535
1536 bool Changed = false;
1537
1538 // Remove all of the calls to the debugger intrinsics, and remove them from
1539 // the module.
1540 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
1541 while (!Declare->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +00001542 CallInst *CI = cast<CallInst>(Declare->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +00001543 CI->eraseFromParent();
1544 }
1545 Declare->eraseFromParent();
1546 Changed = true;
1547 }
1548
1549 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
1550 while (!DbgVal->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +00001551 CallInst *CI = cast<CallInst>(DbgVal->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +00001552 CI->eraseFromParent();
1553 }
1554 DbgVal->eraseFromParent();
1555 Changed = true;
1556 }
1557
1558 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
1559 NME = M.named_metadata_end(); NMI != NME;) {
1560 NamedMDNode *NMD = NMI;
1561 ++NMI;
1562 if (NMD->getName().startswith("llvm.dbg.")) {
1563 NMD->eraseFromParent();
1564 Changed = true;
1565 }
1566 }
1567
1568 for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
1569 for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE;
1570 ++FI)
1571 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE;
1572 ++BI) {
1573 if (!BI->getDebugLoc().isUnknown()) {
1574 Changed = true;
1575 BI->setDebugLoc(DebugLoc());
1576 }
1577 }
1578
1579 return Changed;
1580}
Manman Ren8b4306c2013-12-02 21:29:56 +00001581
Manman Renbd4daf82013-12-03 00:12:14 +00001582/// Return Debug Info Metadata Version by checking module flags.
1583unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
Manman Ren8b4306c2013-12-02 21:29:56 +00001584 Value *Val = M.getModuleFlag("Debug Info Version");
1585 if (!Val)
1586 return 0;
1587 return cast<ConstantInt>(Val)->getZExtValue();
1588}
David Blaikie6876b3b2014-07-01 20:05:26 +00001589
David Blaikiea8c35092014-07-02 18:30:05 +00001590llvm::DenseMap<const llvm::Function *, llvm::DISubprogram>
1591llvm::makeSubprogramMap(const Module &M) {
1592 DenseMap<const Function *, DISubprogram> R;
David Blaikie6876b3b2014-07-01 20:05:26 +00001593
1594 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
1595 if (!CU_Nodes)
1596 return R;
1597
1598 for (MDNode *N : CU_Nodes->operands()) {
1599 DICompileUnit CUNode(N);
1600 DIArray SPs = CUNode.getSubprograms();
1601 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
1602 DISubprogram SP(SPs.getElement(i));
1603 if (Function *F = SP.getFunction())
1604 R.insert(std::make_pair(F, SP));
1605 }
1606 }
1607 return R;
1608}