blob: 284f0443939e1bb6b47ddc86f77a49937aa73cb8 [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() ||
52 DIImportedEntity(DbgNode).Verify());
53}
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
Adrian Prantlda7d92e2014-06-30 17:17:35 +0000141uint64_t DIVariable::getAddrElement(unsigned Idx) const {
142 DIDescriptor ComplexExpr = getDescriptorField(8);
143 if (Idx < ComplexExpr->getNumOperands())
144 if (auto *CI = dyn_cast_or_null<ConstantInt>(ComplexExpr->getOperand(Idx)))
145 return CI->getZExtValue();
146
147 assert(false && "non-existing complex address element requested");
148 return 0;
Bill Wendling523bea82013-11-08 08:13:15 +0000149}
150
151/// getInlinedAt - If this variable is inlined then return inline location.
152MDNode *DIVariable::getInlinedAt() const { return getNodeField(DbgNode, 7); }
153
Adrian Prantlb1416832014-08-01 22:11:58 +0000154bool DIVariable::isVariablePiece() const {
155 return hasComplexAddress() && getAddrElement(0) == DIBuilder::OpPiece;
156}
157
158uint64_t DIVariable::getPieceOffset() const {
159 assert(isVariablePiece());
160 return getAddrElement(1);
161}
162
163uint64_t DIVariable::getPieceSize() const {
164 assert(isVariablePiece());
165 return getAddrElement(2);
166}
167
168/// Return the size reported by the variable's type.
169unsigned DIVariable::getSizeInBits(const DITypeIdentifierMap &Map) {
170 DIType Ty = getType().resolve(Map);
171 // Follow derived types until we reach a type that
172 // reports back a size.
173 while (Ty.isDerivedType() && !Ty.getSizeInBits()) {
174 DIDerivedType DT(&*Ty);
175 Ty = DT.getTypeDerivedFrom().resolve(Map);
176 }
177 assert(Ty.getSizeInBits() && "type with size 0");
178 return Ty.getSizeInBits();
179}
180
181
182
183
Bill Wendling523bea82013-11-08 08:13:15 +0000184//===----------------------------------------------------------------------===//
185// Predicates
186//===----------------------------------------------------------------------===//
187
Manman Renf8a19672014-07-28 22:24:06 +0000188bool DIDescriptor::isSubroutineType() const {
189 return isCompositeType() && getTag() == dwarf::DW_TAG_subroutine_type;
190}
191
Bill Wendling523bea82013-11-08 08:13:15 +0000192/// isBasicType - Return true if the specified tag is legal for
193/// DIBasicType.
194bool DIDescriptor::isBasicType() const {
195 if (!DbgNode)
196 return false;
197 switch (getTag()) {
198 case dwarf::DW_TAG_base_type:
199 case dwarf::DW_TAG_unspecified_type:
200 return true;
201 default:
202 return false;
203 }
204}
205
206/// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
207bool DIDescriptor::isDerivedType() const {
208 if (!DbgNode)
209 return false;
210 switch (getTag()) {
211 case dwarf::DW_TAG_typedef:
212 case dwarf::DW_TAG_pointer_type:
213 case dwarf::DW_TAG_ptr_to_member_type:
214 case dwarf::DW_TAG_reference_type:
215 case dwarf::DW_TAG_rvalue_reference_type:
216 case dwarf::DW_TAG_const_type:
217 case dwarf::DW_TAG_volatile_type:
218 case dwarf::DW_TAG_restrict_type:
219 case dwarf::DW_TAG_member:
220 case dwarf::DW_TAG_inheritance:
221 case dwarf::DW_TAG_friend:
222 return true;
223 default:
224 // CompositeTypes are currently modelled as DerivedTypes.
225 return isCompositeType();
226 }
227}
228
229/// isCompositeType - Return true if the specified tag is legal for
230/// DICompositeType.
231bool DIDescriptor::isCompositeType() const {
232 if (!DbgNode)
233 return false;
234 switch (getTag()) {
235 case dwarf::DW_TAG_array_type:
236 case dwarf::DW_TAG_structure_type:
237 case dwarf::DW_TAG_union_type:
238 case dwarf::DW_TAG_enumeration_type:
239 case dwarf::DW_TAG_subroutine_type:
240 case dwarf::DW_TAG_class_type:
241 return true;
242 default:
243 return false;
244 }
245}
246
247/// isVariable - Return true if the specified tag is legal for DIVariable.
248bool DIDescriptor::isVariable() const {
249 if (!DbgNode)
250 return false;
251 switch (getTag()) {
252 case dwarf::DW_TAG_auto_variable:
253 case dwarf::DW_TAG_arg_variable:
254 return true;
255 default:
256 return false;
257 }
258}
259
260/// isType - Return true if the specified tag is legal for DIType.
261bool DIDescriptor::isType() const {
Manman Renf93ac4b2014-07-29 18:20:39 +0000262 return isBasicType() || isCompositeType() || isDerivedType();
Bill Wendling523bea82013-11-08 08:13:15 +0000263}
264
265/// isSubprogram - Return true if the specified tag is legal for
266/// DISubprogram.
267bool DIDescriptor::isSubprogram() const {
268 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
269}
270
271/// isGlobalVariable - Return true if the specified tag is legal for
272/// DIGlobalVariable.
273bool DIDescriptor::isGlobalVariable() const {
274 return DbgNode && (getTag() == dwarf::DW_TAG_variable ||
275 getTag() == dwarf::DW_TAG_constant);
276}
277
Bill Wendling523bea82013-11-08 08:13:15 +0000278/// isScope - Return true if the specified tag is one of the scope
279/// related tag.
280bool DIDescriptor::isScope() const {
281 if (!DbgNode)
282 return false;
283 switch (getTag()) {
284 case dwarf::DW_TAG_compile_unit:
285 case dwarf::DW_TAG_lexical_block:
286 case dwarf::DW_TAG_subprogram:
287 case dwarf::DW_TAG_namespace:
288 case dwarf::DW_TAG_file_type:
289 return true;
290 default:
291 break;
292 }
293 return isType();
294}
295
296/// isTemplateTypeParameter - Return true if the specified tag is
297/// DW_TAG_template_type_parameter.
298bool DIDescriptor::isTemplateTypeParameter() const {
299 return DbgNode && getTag() == dwarf::DW_TAG_template_type_parameter;
300}
301
302/// isTemplateValueParameter - Return true if the specified tag is
303/// DW_TAG_template_value_parameter.
304bool DIDescriptor::isTemplateValueParameter() const {
305 return DbgNode && (getTag() == dwarf::DW_TAG_template_value_parameter ||
306 getTag() == dwarf::DW_TAG_GNU_template_template_param ||
307 getTag() == dwarf::DW_TAG_GNU_template_parameter_pack);
308}
309
310/// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
311bool DIDescriptor::isCompileUnit() const {
312 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
313}
314
315/// isFile - Return true if the specified tag is DW_TAG_file_type.
316bool DIDescriptor::isFile() const {
317 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
318}
319
320/// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
321bool DIDescriptor::isNameSpace() const {
322 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
323}
324
325/// isLexicalBlockFile - Return true if the specified descriptor is a
326/// lexical block with an extra file.
327bool DIDescriptor::isLexicalBlockFile() const {
328 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
David Blaikie2f3f76f2014-08-21 22:45:21 +0000329 (DbgNode->getNumOperands() == 4);
Bill Wendling523bea82013-11-08 08:13:15 +0000330}
331
332/// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
333bool DIDescriptor::isLexicalBlock() const {
334 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block &&
335 (DbgNode->getNumOperands() > 3);
336}
337
338/// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
339bool DIDescriptor::isSubrange() const {
340 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
341}
342
343/// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
344bool DIDescriptor::isEnumerator() const {
345 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
346}
347
348/// isObjCProperty - Return true if the specified tag is DW_TAG_APPLE_property.
349bool DIDescriptor::isObjCProperty() const {
350 return DbgNode && getTag() == dwarf::DW_TAG_APPLE_property;
351}
352
353/// \brief Return true if the specified tag is DW_TAG_imported_module or
354/// DW_TAG_imported_declaration.
355bool DIDescriptor::isImportedEntity() const {
356 return DbgNode && (getTag() == dwarf::DW_TAG_imported_module ||
357 getTag() == dwarf::DW_TAG_imported_declaration);
358}
359
360//===----------------------------------------------------------------------===//
361// Simple Descriptor Constructors and other Methods
362//===----------------------------------------------------------------------===//
363
Bill Wendling523bea82013-11-08 08:13:15 +0000364/// replaceAllUsesWith - Replace all uses of the MDNode used by this
365/// type with the one in the passed descriptor.
David Blaikied3f094a2014-05-06 03:41:57 +0000366void DIType::replaceAllUsesWith(LLVMContext &VMContext, DIDescriptor D) {
Bill Wendling523bea82013-11-08 08:13:15 +0000367
368 assert(DbgNode && "Trying to replace an unverified type!");
369
370 // Since we use a TrackingVH for the node, its easy for clients to manufacture
371 // legitimate situations where they want to replaceAllUsesWith() on something
372 // which, due to uniquing, has merged with the source. We shield clients from
373 // this detail by allowing a value to be replaced with replaceAllUsesWith()
374 // itself.
David Blaikied3f094a2014-05-06 03:41:57 +0000375 const MDNode *DN = D;
376 if (DbgNode == DN) {
377 SmallVector<Value*, 10> Ops(DbgNode->getNumOperands());
378 for (size_t i = 0; i != Ops.size(); ++i)
379 Ops[i] = DbgNode->getOperand(i);
380 DN = MDNode::get(VMContext, Ops);
Bill Wendling523bea82013-11-08 08:13:15 +0000381 }
David Blaikied3f094a2014-05-06 03:41:57 +0000382
383 MDNode *Node = const_cast<MDNode *>(DbgNode);
384 const Value *V = cast_or_null<Value>(DN);
385 Node->replaceAllUsesWith(const_cast<Value *>(V));
386 MDNode::deleteTemporary(Node);
387 DbgNode = D;
Bill Wendling523bea82013-11-08 08:13:15 +0000388}
389
390/// replaceAllUsesWith - Replace all uses of the MDNode used by this
391/// type with the one in D.
392void DIType::replaceAllUsesWith(MDNode *D) {
393
394 assert(DbgNode && "Trying to replace an unverified type!");
David Blaikied3f094a2014-05-06 03:41:57 +0000395 assert(DbgNode != D && "This replacement should always happen");
396 MDNode *Node = const_cast<MDNode *>(DbgNode);
397 const MDNode *DN = D;
398 const Value *V = cast_or_null<Value>(DN);
399 Node->replaceAllUsesWith(const_cast<Value *>(V));
400 MDNode::deleteTemporary(Node);
Bill Wendling523bea82013-11-08 08:13:15 +0000401}
402
403/// Verify - Verify that a compile unit is well formed.
404bool DICompileUnit::Verify() const {
405 if (!isCompileUnit())
406 return false;
407
408 // Don't bother verifying the compilation directory or producer string
409 // as those could be empty.
410 if (getFilename().empty())
411 return false;
412
Eric Christopher75d49db2014-02-27 01:24:56 +0000413 return DbgNode->getNumOperands() == 14;
Bill Wendling523bea82013-11-08 08:13:15 +0000414}
415
416/// Verify - Verify that an ObjC property is well formed.
417bool DIObjCProperty::Verify() const {
418 if (!isObjCProperty())
419 return false;
420
421 // Don't worry about the rest of the strings for now.
422 return DbgNode->getNumOperands() == 8;
423}
424
425/// Check if a field at position Elt of a MDNode is a MDNode.
426/// We currently allow an empty string and an integer.
427/// But we don't allow a non-empty string in a MDNode field.
428static bool fieldIsMDNode(const MDNode *DbgNode, unsigned Elt) {
429 // FIXME: This function should return true, if the field is null or the field
430 // is indeed a MDNode: return !Fld || isa<MDNode>(Fld).
431 Value *Fld = getField(DbgNode, Elt);
432 if (Fld && isa<MDString>(Fld) && !cast<MDString>(Fld)->getString().empty())
433 return false;
434 return true;
435}
436
437/// Check if a field at position Elt of a MDNode is a MDString.
438static bool fieldIsMDString(const MDNode *DbgNode, unsigned Elt) {
439 Value *Fld = getField(DbgNode, Elt);
440 return !Fld || isa<MDString>(Fld);
441}
442
443/// Check if a value can be a reference to a type.
444static bool isTypeRef(const Value *Val) {
445 return !Val ||
446 (isa<MDString>(Val) && !cast<MDString>(Val)->getString().empty()) ||
447 (isa<MDNode>(Val) && DIType(cast<MDNode>(Val)).isType());
448}
449
450/// Check if a field at position Elt of a MDNode can be a reference to a type.
451static bool fieldIsTypeRef(const MDNode *DbgNode, unsigned Elt) {
452 Value *Fld = getField(DbgNode, Elt);
453 return isTypeRef(Fld);
454}
455
456/// Check if a value can be a ScopeRef.
457static bool isScopeRef(const Value *Val) {
458 return !Val ||
Adrian Prantl6b444c52014-04-01 21:04:24 +0000459 (isa<MDString>(Val) && !cast<MDString>(Val)->getString().empty()) ||
460 // Not checking for Val->isScope() here, because it would work
461 // only for lexical scopes and not all subclasses of DIScope.
462 isa<MDNode>(Val);
Bill Wendling523bea82013-11-08 08:13:15 +0000463}
464
465/// Check if a field at position Elt of a MDNode can be a ScopeRef.
466static bool fieldIsScopeRef(const MDNode *DbgNode, unsigned Elt) {
467 Value *Fld = getField(DbgNode, Elt);
468 return isScopeRef(Fld);
469}
470
471/// Verify - Verify that a type descriptor is well formed.
472bool DIType::Verify() const {
473 if (!isType())
474 return false;
475 // Make sure Context @ field 2 is MDNode.
476 if (!fieldIsScopeRef(DbgNode, 2))
477 return false;
478
479 // FIXME: Sink this into the various subclass verifies.
480 uint16_t Tag = getTag();
Manman Renf93ac4b2014-07-29 18:20:39 +0000481 if (!isBasicType() && Tag != dwarf::DW_TAG_const_type &&
Bill Wendling523bea82013-11-08 08:13:15 +0000482 Tag != dwarf::DW_TAG_volatile_type && Tag != dwarf::DW_TAG_pointer_type &&
483 Tag != dwarf::DW_TAG_ptr_to_member_type &&
484 Tag != dwarf::DW_TAG_reference_type &&
485 Tag != dwarf::DW_TAG_rvalue_reference_type &&
486 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_array_type &&
487 Tag != dwarf::DW_TAG_enumeration_type &&
488 Tag != dwarf::DW_TAG_subroutine_type &&
489 Tag != dwarf::DW_TAG_inheritance && Tag != dwarf::DW_TAG_friend &&
490 getFilename().empty())
491 return false;
492 // DIType is abstract, it should be a BasicType, a DerivedType or
493 // a CompositeType.
494 if (isBasicType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000495 return DIBasicType(DbgNode).Verify();
Bill Wendling523bea82013-11-08 08:13:15 +0000496 else if (isCompositeType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000497 return DICompositeType(DbgNode).Verify();
Bill Wendling523bea82013-11-08 08:13:15 +0000498 else if (isDerivedType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000499 return DIDerivedType(DbgNode).Verify();
Bill Wendling523bea82013-11-08 08:13:15 +0000500 else
501 return false;
Bill Wendling523bea82013-11-08 08:13:15 +0000502}
503
504/// Verify - Verify that a basic type descriptor is well formed.
505bool DIBasicType::Verify() const {
506 return isBasicType() && DbgNode->getNumOperands() == 10;
507}
508
509/// Verify - Verify that a derived type descriptor is well formed.
510bool DIDerivedType::Verify() const {
511 // Make sure DerivedFrom @ field 9 is TypeRef.
512 if (!fieldIsTypeRef(DbgNode, 9))
513 return false;
514 if (getTag() == dwarf::DW_TAG_ptr_to_member_type)
515 // Make sure ClassType @ field 10 is a TypeRef.
516 if (!fieldIsTypeRef(DbgNode, 10))
517 return false;
518
519 return isDerivedType() && DbgNode->getNumOperands() >= 10 &&
520 DbgNode->getNumOperands() <= 14;
521}
522
523/// Verify - Verify that a composite type descriptor is well formed.
524bool DICompositeType::Verify() const {
525 if (!isCompositeType())
526 return false;
527
528 // Make sure DerivedFrom @ field 9 and ContainingType @ field 12 are TypeRef.
529 if (!fieldIsTypeRef(DbgNode, 9))
530 return false;
531 if (!fieldIsTypeRef(DbgNode, 12))
532 return false;
533
534 // Make sure the type identifier at field 14 is MDString, it can be null.
535 if (!fieldIsMDString(DbgNode, 14))
536 return false;
537
Adrian Prantl99c7af22013-12-18 21:48:19 +0000538 // A subroutine type can't be both & and &&.
539 if (isLValueReference() && isRValueReference())
540 return false;
541
Bill Wendling523bea82013-11-08 08:13:15 +0000542 return DbgNode->getNumOperands() == 15;
543}
544
545/// Verify - Verify that a subprogram descriptor is well formed.
546bool DISubprogram::Verify() const {
547 if (!isSubprogram())
548 return false;
549
550 // Make sure context @ field 2 is a ScopeRef and type @ field 7 is a MDNode.
551 if (!fieldIsScopeRef(DbgNode, 2))
552 return false;
553 if (!fieldIsMDNode(DbgNode, 7))
554 return false;
555 // Containing type @ field 12.
556 if (!fieldIsTypeRef(DbgNode, 12))
557 return false;
Adrian Prantl99c7af22013-12-18 21:48:19 +0000558
559 // A subprogram can't be both & and &&.
560 if (isLValueReference() && isRValueReference())
561 return false;
562
Bill Wendling523bea82013-11-08 08:13:15 +0000563 return DbgNode->getNumOperands() == 20;
564}
565
566/// Verify - Verify that a global variable descriptor is well formed.
567bool DIGlobalVariable::Verify() const {
568 if (!isGlobalVariable())
569 return false;
570
571 if (getDisplayName().empty())
572 return false;
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000573 // Make sure context @ field 2 is an MDNode.
Bill Wendling523bea82013-11-08 08:13:15 +0000574 if (!fieldIsMDNode(DbgNode, 2))
575 return false;
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000576 // Make sure that type @ field 8 is a DITypeRef.
577 if (!fieldIsTypeRef(DbgNode, 8))
Bill Wendling523bea82013-11-08 08:13:15 +0000578 return false;
579 // Make sure StaticDataMemberDeclaration @ field 12 is MDNode.
580 if (!fieldIsMDNode(DbgNode, 12))
581 return false;
582
583 return DbgNode->getNumOperands() == 13;
584}
585
586/// Verify - Verify that a variable descriptor is well formed.
587bool DIVariable::Verify() const {
588 if (!isVariable())
589 return false;
590
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000591 // Make sure context @ field 1 is an MDNode.
Bill Wendling523bea82013-11-08 08:13:15 +0000592 if (!fieldIsMDNode(DbgNode, 1))
593 return false;
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000594 // Make sure that type @ field 5 is a DITypeRef.
595 if (!fieldIsTypeRef(DbgNode, 5))
Bill Wendling523bea82013-11-08 08:13:15 +0000596 return false;
Adrian Prantlda7d92e2014-06-30 17:17:35 +0000597
598 // Variable without a complex expression.
599 if (DbgNode->getNumOperands() == 8)
600 return true;
601
602 // Make sure the complex expression is an MDNode.
603 return (DbgNode->getNumOperands() == 9 && fieldIsMDNode(DbgNode, 8));
Bill Wendling523bea82013-11-08 08:13:15 +0000604}
605
606/// Verify - Verify that a location descriptor is well formed.
607bool DILocation::Verify() const {
608 if (!DbgNode)
609 return false;
610
611 return DbgNode->getNumOperands() == 4;
612}
613
614/// Verify - Verify that a namespace descriptor is well formed.
615bool DINameSpace::Verify() const {
616 if (!isNameSpace())
617 return false;
618 return DbgNode->getNumOperands() == 5;
619}
620
621/// \brief Retrieve the MDNode for the directory/file pair.
622MDNode *DIFile::getFileNode() const { return getNodeField(DbgNode, 1); }
623
624/// \brief Verify that the file descriptor is well formed.
625bool DIFile::Verify() const {
626 return isFile() && DbgNode->getNumOperands() == 2;
627}
628
629/// \brief Verify that the enumerator descriptor is well formed.
630bool DIEnumerator::Verify() const {
631 return isEnumerator() && DbgNode->getNumOperands() == 3;
632}
633
634/// \brief Verify that the subrange descriptor is well formed.
635bool DISubrange::Verify() const {
636 return isSubrange() && DbgNode->getNumOperands() == 3;
637}
638
639/// \brief Verify that the lexical block descriptor is well formed.
640bool DILexicalBlock::Verify() const {
David Blaikie2f3f76f2014-08-21 22:45:21 +0000641 return isLexicalBlock() && DbgNode->getNumOperands() == 6;
Bill Wendling523bea82013-11-08 08:13:15 +0000642}
643
644/// \brief Verify that the file-scoped lexical block descriptor is well formed.
645bool DILexicalBlockFile::Verify() const {
David Blaikie2f3f76f2014-08-21 22:45:21 +0000646 return isLexicalBlockFile() && DbgNode->getNumOperands() == 4;
Bill Wendling523bea82013-11-08 08:13:15 +0000647}
648
649/// \brief Verify that the template type parameter descriptor is well formed.
650bool DITemplateTypeParameter::Verify() const {
651 return isTemplateTypeParameter() && DbgNode->getNumOperands() == 7;
652}
653
654/// \brief Verify that the template value parameter descriptor is well formed.
655bool DITemplateValueParameter::Verify() const {
656 return isTemplateValueParameter() && DbgNode->getNumOperands() == 8;
657}
658
659/// \brief Verify that the imported module descriptor is well formed.
660bool DIImportedEntity::Verify() const {
661 return isImportedEntity() &&
662 (DbgNode->getNumOperands() == 4 || DbgNode->getNumOperands() == 5);
663}
664
665/// getObjCProperty - Return property node, if this ivar is associated with one.
666MDNode *DIDerivedType::getObjCProperty() const {
667 return getNodeField(DbgNode, 10);
668}
669
670MDString *DICompositeType::getIdentifier() const {
671 return cast_or_null<MDString>(getField(DbgNode, 14));
672}
673
674#ifndef NDEBUG
675static void VerifySubsetOf(const MDNode *LHS, const MDNode *RHS) {
676 for (unsigned i = 0; i != LHS->getNumOperands(); ++i) {
677 // Skip the 'empty' list (that's a single i32 0, rather than truly empty).
678 if (i == 0 && isa<ConstantInt>(LHS->getOperand(i)))
679 continue;
680 const MDNode *E = cast<MDNode>(LHS->getOperand(i));
681 bool found = false;
682 for (unsigned j = 0; !found && j != RHS->getNumOperands(); ++j)
683 found = E == RHS->getOperand(j);
684 assert(found && "Losing a member during member list replacement");
685 }
686}
687#endif
688
689/// \brief Set the array of member DITypes.
Manman Ren1a125c92014-07-28 19:33:20 +0000690void DICompositeType::setArraysHelper(MDNode *Elements, MDNode *TParams) {
Bill Wendling523bea82013-11-08 08:13:15 +0000691 TrackingVH<MDNode> N(*this);
692 if (Elements) {
693#ifndef NDEBUG
694 // Check that the new list of members contains all the old members as well.
695 if (const MDNode *El = cast_or_null<MDNode>(N->getOperand(10)))
696 VerifySubsetOf(El, Elements);
697#endif
698 N->replaceOperandWith(10, Elements);
699 }
700 if (TParams)
701 N->replaceOperandWith(13, TParams);
702 DbgNode = N;
703}
704
Bill Wendling523bea82013-11-08 08:13:15 +0000705/// Generate a reference to this DIType. Uses the type identifier instead
706/// of the actual MDNode if possible, to help type uniquing.
707DIScopeRef DIScope::getRef() const {
708 if (!isCompositeType())
709 return DIScopeRef(*this);
710 DICompositeType DTy(DbgNode);
711 if (!DTy.getIdentifier())
712 return DIScopeRef(*this);
713 return DIScopeRef(DTy.getIdentifier());
714}
715
716/// \brief Set the containing type.
717void DICompositeType::setContainingType(DICompositeType ContainingType) {
718 TrackingVH<MDNode> N(*this);
719 N->replaceOperandWith(12, ContainingType.getRef());
720 DbgNode = N;
721}
722
723/// isInlinedFnArgument - Return true if this variable provides debugging
724/// information for an inlined function arguments.
725bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
726 assert(CurFn && "Invalid function");
727 if (!getContext().isSubprogram())
728 return false;
729 // This variable is not inlined function argument if its scope
730 // does not describe current function.
731 return !DISubprogram(getContext()).describes(CurFn);
732}
733
734/// describes - Return true if this subprogram provides debugging
735/// information for the function F.
736bool DISubprogram::describes(const Function *F) {
737 assert(F && "Invalid function");
738 if (F == getFunction())
739 return true;
740 StringRef Name = getLinkageName();
741 if (Name.empty())
742 Name = getName();
743 if (F->getName() == Name)
744 return true;
745 return false;
746}
747
748unsigned DISubprogram::isOptimized() const {
749 assert(DbgNode && "Invalid subprogram descriptor!");
750 if (DbgNode->getNumOperands() == 15)
751 return getUnsignedField(14);
752 return 0;
753}
754
755MDNode *DISubprogram::getVariablesNodes() const {
756 return getNodeField(DbgNode, 18);
757}
758
759DIArray DISubprogram::getVariables() const {
760 return DIArray(getNodeField(DbgNode, 18));
761}
762
763Value *DITemplateValueParameter::getValue() const {
764 return getField(DbgNode, 4);
765}
766
767// If the current node has a parent scope then return that,
768// else return an empty scope.
769DIScopeRef DIScope::getContext() const {
770
771 if (isType())
772 return DIType(DbgNode).getContext();
773
774 if (isSubprogram())
775 return DIScopeRef(DISubprogram(DbgNode).getContext());
776
777 if (isLexicalBlock())
778 return DIScopeRef(DILexicalBlock(DbgNode).getContext());
779
780 if (isLexicalBlockFile())
781 return DIScopeRef(DILexicalBlockFile(DbgNode).getContext());
782
783 if (isNameSpace())
784 return DIScopeRef(DINameSpace(DbgNode).getContext());
785
786 assert((isFile() || isCompileUnit()) && "Unhandled type of scope.");
Craig Topperc6207612014-04-09 06:08:46 +0000787 return DIScopeRef(nullptr);
Bill Wendling523bea82013-11-08 08:13:15 +0000788}
789
790// If the scope node has a name, return that, else return an empty string.
791StringRef DIScope::getName() const {
792 if (isType())
793 return DIType(DbgNode).getName();
794 if (isSubprogram())
795 return DISubprogram(DbgNode).getName();
796 if (isNameSpace())
797 return DINameSpace(DbgNode).getName();
798 assert((isLexicalBlock() || isLexicalBlockFile() || isFile() ||
799 isCompileUnit()) &&
800 "Unhandled type of scope.");
801 return StringRef();
802}
803
804StringRef DIScope::getFilename() const {
805 if (!DbgNode)
806 return StringRef();
807 return ::getStringField(getNodeField(DbgNode, 1), 0);
808}
809
810StringRef DIScope::getDirectory() const {
811 if (!DbgNode)
812 return StringRef();
813 return ::getStringField(getNodeField(DbgNode, 1), 1);
814}
815
816DIArray DICompileUnit::getEnumTypes() const {
817 if (!DbgNode || DbgNode->getNumOperands() < 13)
818 return DIArray();
819
820 return DIArray(getNodeField(DbgNode, 7));
821}
822
823DIArray DICompileUnit::getRetainedTypes() const {
824 if (!DbgNode || DbgNode->getNumOperands() < 13)
825 return DIArray();
826
827 return DIArray(getNodeField(DbgNode, 8));
828}
829
830DIArray DICompileUnit::getSubprograms() const {
831 if (!DbgNode || DbgNode->getNumOperands() < 13)
832 return DIArray();
833
834 return DIArray(getNodeField(DbgNode, 9));
835}
836
837DIArray DICompileUnit::getGlobalVariables() const {
838 if (!DbgNode || DbgNode->getNumOperands() < 13)
839 return DIArray();
840
841 return DIArray(getNodeField(DbgNode, 10));
842}
843
844DIArray DICompileUnit::getImportedEntities() const {
845 if (!DbgNode || DbgNode->getNumOperands() < 13)
846 return DIArray();
847
848 return DIArray(getNodeField(DbgNode, 11));
849}
850
Diego Novillof5041ce2014-03-03 20:06:11 +0000851/// copyWithNewScope - Return a copy of this location, replacing the
852/// current scope with the given one.
853DILocation DILocation::copyWithNewScope(LLVMContext &Ctx,
David Blaikie2f3f76f2014-08-21 22:45:21 +0000854 DILexicalBlockFile NewScope) {
Diego Novillof5041ce2014-03-03 20:06:11 +0000855 SmallVector<Value *, 10> Elts;
856 assert(Verify());
857 for (unsigned I = 0; I < DbgNode->getNumOperands(); ++I) {
858 if (I != 2)
859 Elts.push_back(DbgNode->getOperand(I));
860 else
861 Elts.push_back(NewScope);
862 }
863 MDNode *NewDIL = MDNode::get(Ctx, Elts);
864 return DILocation(NewDIL);
865}
866
867/// computeNewDiscriminator - Generate a new discriminator value for this
868/// file and line location.
869unsigned DILocation::computeNewDiscriminator(LLVMContext &Ctx) {
870 std::pair<const char *, unsigned> Key(getFilename().data(), getLineNumber());
871 return ++Ctx.pImpl->DiscriminatorTable[Key];
872}
873
Bill Wendling523bea82013-11-08 08:13:15 +0000874/// fixupSubprogramName - Replace contains special characters used
875/// in a typical Objective-C names with '.' in a given string.
876static void fixupSubprogramName(DISubprogram Fn, SmallVectorImpl<char> &Out) {
877 StringRef FName =
878 Fn.getFunction() ? Fn.getFunction()->getName() : Fn.getName();
879 FName = Function::getRealLinkageName(FName);
880
881 StringRef Prefix("llvm.dbg.lv.");
882 Out.reserve(FName.size() + Prefix.size());
883 Out.append(Prefix.begin(), Prefix.end());
884
885 bool isObjCLike = false;
886 for (size_t i = 0, e = FName.size(); i < e; ++i) {
887 char C = FName[i];
888 if (C == '[')
889 isObjCLike = true;
890
891 if (isObjCLike && (C == '[' || C == ']' || C == ' ' || C == ':' ||
892 C == '+' || C == '(' || C == ')'))
893 Out.push_back('.');
894 else
895 Out.push_back(C);
896 }
897}
898
899/// getFnSpecificMDNode - Return a NameMDNode, if available, that is
900/// suitable to hold function specific information.
901NamedMDNode *llvm::getFnSpecificMDNode(const Module &M, DISubprogram Fn) {
902 SmallString<32> Name;
903 fixupSubprogramName(Fn, Name);
904 return M.getNamedMetadata(Name.str());
905}
906
907/// getOrInsertFnSpecificMDNode - Return a NameMDNode that is suitable
908/// to hold function specific information.
909NamedMDNode *llvm::getOrInsertFnSpecificMDNode(Module &M, DISubprogram Fn) {
910 SmallString<32> Name;
911 fixupSubprogramName(Fn, Name);
912 return M.getOrInsertNamedMetadata(Name.str());
913}
914
915/// createInlinedVariable - Create a new inlined variable based on current
916/// variable.
917/// @param DV Current Variable.
918/// @param InlinedScope Location at current variable is inlined.
919DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
920 LLVMContext &VMContext) {
921 SmallVector<Value *, 16> Elts;
922 // Insert inlined scope as 7th element.
923 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
924 i == 7 ? Elts.push_back(InlinedScope) : Elts.push_back(DV->getOperand(i));
925 return DIVariable(MDNode::get(VMContext, Elts));
926}
927
928/// cleanseInlinedVariable - Remove inlined scope from the variable.
929DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
930 SmallVector<Value *, 16> Elts;
931 // Insert inlined scope as 7th element.
932 for (unsigned i = 0, e = DV->getNumOperands(); i != e; ++i)
933 i == 7 ? Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)))
934 : Elts.push_back(DV->getOperand(i));
935 return DIVariable(MDNode::get(VMContext, Elts));
936}
937
Adrian Prantlb1416832014-08-01 22:11:58 +0000938
939/// getEntireVariable - Remove OpPiece exprs from the variable.
940DIVariable llvm::getEntireVariable(DIVariable DV) {
941 if (!DV.isVariablePiece())
942 return DV;
943
944 SmallVector<Value *, 8> Elts;
945 for (unsigned i = 0; i < 8; ++i)
946 Elts.push_back(DV->getOperand(i));
947
948 return DIVariable(MDNode::get(DV->getContext(), Elts));
949}
950
Bill Wendling523bea82013-11-08 08:13:15 +0000951/// getDISubprogram - Find subprogram that is enclosing this scope.
952DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
953 DIDescriptor D(Scope);
954 if (D.isSubprogram())
955 return DISubprogram(Scope);
956
957 if (D.isLexicalBlockFile())
958 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
959
960 if (D.isLexicalBlock())
961 return getDISubprogram(DILexicalBlock(Scope).getContext());
962
963 return DISubprogram();
964}
965
966/// getDICompositeType - Find underlying composite type.
967DICompositeType llvm::getDICompositeType(DIType T) {
968 if (T.isCompositeType())
969 return DICompositeType(T);
970
971 if (T.isDerivedType()) {
972 // This function is currently used by dragonegg and dragonegg does
973 // not generate identifier for types, so using an empty map to resolve
974 // DerivedFrom should be fine.
975 DITypeIdentifierMap EmptyMap;
976 return getDICompositeType(
977 DIDerivedType(T).getTypeDerivedFrom().resolve(EmptyMap));
978 }
979
980 return DICompositeType();
981}
982
983/// Update DITypeIdentifierMap by going through retained types of each CU.
984DITypeIdentifierMap
985llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
986 DITypeIdentifierMap Map;
987 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
988 DICompileUnit CU(CU_Nodes->getOperand(CUi));
989 DIArray Retain = CU.getRetainedTypes();
990 for (unsigned Ti = 0, Te = Retain.getNumElements(); Ti != Te; ++Ti) {
991 if (!Retain.getElement(Ti).isCompositeType())
992 continue;
993 DICompositeType Ty(Retain.getElement(Ti));
994 if (MDString *TypeId = Ty.getIdentifier()) {
995 // Definition has priority over declaration.
996 // Try to insert (TypeId, Ty) to Map.
997 std::pair<DITypeIdentifierMap::iterator, bool> P =
998 Map.insert(std::make_pair(TypeId, Ty));
999 // If TypeId already exists in Map and this is a definition, replace
1000 // whatever we had (declaration or definition) with the definition.
1001 if (!P.second && !Ty.isForwardDecl())
1002 P.first->second = Ty;
1003 }
1004 }
1005 }
1006 return Map;
1007}
1008
1009//===----------------------------------------------------------------------===//
1010// DebugInfoFinder implementations.
1011//===----------------------------------------------------------------------===//
1012
1013void DebugInfoFinder::reset() {
1014 CUs.clear();
1015 SPs.clear();
1016 GVs.clear();
1017 TYs.clear();
1018 Scopes.clear();
1019 NodesSeen.clear();
1020 TypeIdentifierMap.clear();
Manman Ren2085ccc2013-11-17 18:42:37 +00001021 TypeMapInitialized = false;
1022}
1023
Manman Renb46e5502013-11-17 19:35:03 +00001024void DebugInfoFinder::InitializeTypeMap(const Module &M) {
Manman Ren2085ccc2013-11-17 18:42:37 +00001025 if (!TypeMapInitialized)
1026 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
1027 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
1028 TypeMapInitialized = true;
1029 }
Bill Wendling523bea82013-11-08 08:13:15 +00001030}
1031
1032/// processModule - Process entire module and collect debug info.
1033void DebugInfoFinder::processModule(const Module &M) {
Manman Renb46e5502013-11-17 19:35:03 +00001034 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001035 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
Bill Wendling523bea82013-11-08 08:13:15 +00001036 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
1037 DICompileUnit CU(CU_Nodes->getOperand(i));
1038 addCompileUnit(CU);
1039 DIArray GVs = CU.getGlobalVariables();
1040 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
1041 DIGlobalVariable DIG(GVs.getElement(i));
1042 if (addGlobalVariable(DIG)) {
1043 processScope(DIG.getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +00001044 processType(DIG.getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +00001045 }
1046 }
1047 DIArray SPs = CU.getSubprograms();
1048 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
1049 processSubprogram(DISubprogram(SPs.getElement(i)));
1050 DIArray EnumTypes = CU.getEnumTypes();
1051 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
1052 processType(DIType(EnumTypes.getElement(i)));
1053 DIArray RetainedTypes = CU.getRetainedTypes();
1054 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
1055 processType(DIType(RetainedTypes.getElement(i)));
1056 DIArray Imports = CU.getImportedEntities();
1057 for (unsigned i = 0, e = Imports.getNumElements(); i != e; ++i) {
1058 DIImportedEntity Import = DIImportedEntity(Imports.getElement(i));
Adrian Prantld09ba232014-04-01 03:41:04 +00001059 DIDescriptor Entity = Import.getEntity().resolve(TypeIdentifierMap);
Bill Wendling523bea82013-11-08 08:13:15 +00001060 if (Entity.isType())
1061 processType(DIType(Entity));
1062 else if (Entity.isSubprogram())
1063 processSubprogram(DISubprogram(Entity));
1064 else if (Entity.isNameSpace())
1065 processScope(DINameSpace(Entity).getContext());
1066 }
1067 }
1068 }
1069}
1070
1071/// processLocation - Process DILocation.
Manman Ren2085ccc2013-11-17 18:42:37 +00001072void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) {
Bill Wendling523bea82013-11-08 08:13:15 +00001073 if (!Loc)
1074 return;
Manman Renb46e5502013-11-17 19:35:03 +00001075 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001076 processScope(Loc.getScope());
Manman Ren2085ccc2013-11-17 18:42:37 +00001077 processLocation(M, Loc.getOrigLocation());
Bill Wendling523bea82013-11-08 08:13:15 +00001078}
1079
1080/// processType - Process DIType.
1081void DebugInfoFinder::processType(DIType DT) {
1082 if (!addType(DT))
1083 return;
1084 processScope(DT.getContext().resolve(TypeIdentifierMap));
1085 if (DT.isCompositeType()) {
1086 DICompositeType DCT(DT);
1087 processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
Manman Renf8a19672014-07-28 22:24:06 +00001088 if (DT.isSubroutineType()) {
1089 DITypeArray DTA = DISubroutineType(DT).getTypeArray();
1090 for (unsigned i = 0, e = DTA.getNumElements(); i != e; ++i)
1091 processType(DTA.getElement(i).resolve(TypeIdentifierMap));
1092 return;
1093 }
Manman Renab8ffba2014-07-28 19:14:13 +00001094 DIArray DA = DCT.getElements();
Bill Wendling523bea82013-11-08 08:13:15 +00001095 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1096 DIDescriptor D = DA.getElement(i);
1097 if (D.isType())
1098 processType(DIType(D));
1099 else if (D.isSubprogram())
1100 processSubprogram(DISubprogram(D));
1101 }
1102 } else if (DT.isDerivedType()) {
1103 DIDerivedType DDT(DT);
1104 processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
1105 }
1106}
1107
1108void DebugInfoFinder::processScope(DIScope Scope) {
1109 if (Scope.isType()) {
1110 DIType Ty(Scope);
1111 processType(Ty);
1112 return;
1113 }
1114 if (Scope.isCompileUnit()) {
1115 addCompileUnit(DICompileUnit(Scope));
1116 return;
1117 }
1118 if (Scope.isSubprogram()) {
1119 processSubprogram(DISubprogram(Scope));
1120 return;
1121 }
1122 if (!addScope(Scope))
1123 return;
1124 if (Scope.isLexicalBlock()) {
1125 DILexicalBlock LB(Scope);
1126 processScope(LB.getContext());
1127 } else if (Scope.isLexicalBlockFile()) {
1128 DILexicalBlockFile LBF = DILexicalBlockFile(Scope);
1129 processScope(LBF.getScope());
1130 } else if (Scope.isNameSpace()) {
1131 DINameSpace NS(Scope);
1132 processScope(NS.getContext());
1133 }
1134}
1135
Bill Wendling523bea82013-11-08 08:13:15 +00001136/// processSubprogram - Process DISubprogram.
1137void DebugInfoFinder::processSubprogram(DISubprogram SP) {
1138 if (!addSubprogram(SP))
1139 return;
1140 processScope(SP.getContext().resolve(TypeIdentifierMap));
1141 processType(SP.getType());
1142 DIArray TParams = SP.getTemplateParams();
1143 for (unsigned I = 0, E = TParams.getNumElements(); I != E; ++I) {
1144 DIDescriptor Element = TParams.getElement(I);
1145 if (Element.isTemplateTypeParameter()) {
1146 DITemplateTypeParameter TType(Element);
1147 processScope(TType.getContext().resolve(TypeIdentifierMap));
1148 processType(TType.getType().resolve(TypeIdentifierMap));
1149 } else if (Element.isTemplateValueParameter()) {
1150 DITemplateValueParameter TVal(Element);
1151 processScope(TVal.getContext().resolve(TypeIdentifierMap));
1152 processType(TVal.getType().resolve(TypeIdentifierMap));
1153 }
1154 }
1155}
1156
1157/// processDeclare - Process DbgDeclareInst.
Manman Ren2085ccc2013-11-17 18:42:37 +00001158void DebugInfoFinder::processDeclare(const Module &M,
1159 const DbgDeclareInst *DDI) {
Bill Wendling523bea82013-11-08 08:13:15 +00001160 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1161 if (!N)
1162 return;
Manman Renb46e5502013-11-17 19:35:03 +00001163 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001164
1165 DIDescriptor DV(N);
1166 if (!DV.isVariable())
1167 return;
1168
1169 if (!NodesSeen.insert(DV))
1170 return;
1171 processScope(DIVariable(N).getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +00001172 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +00001173}
1174
Manman Ren2085ccc2013-11-17 18:42:37 +00001175void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
Bill Wendling523bea82013-11-08 08:13:15 +00001176 MDNode *N = dyn_cast<MDNode>(DVI->getVariable());
1177 if (!N)
1178 return;
Manman Renb46e5502013-11-17 19:35:03 +00001179 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +00001180
1181 DIDescriptor DV(N);
1182 if (!DV.isVariable())
1183 return;
1184
1185 if (!NodesSeen.insert(DV))
1186 return;
1187 processScope(DIVariable(N).getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +00001188 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +00001189}
1190
1191/// addType - Add type into Tys.
1192bool DebugInfoFinder::addType(DIType DT) {
1193 if (!DT)
1194 return false;
1195
1196 if (!NodesSeen.insert(DT))
1197 return false;
1198
1199 TYs.push_back(DT);
1200 return true;
1201}
1202
1203/// addCompileUnit - Add compile unit into CUs.
1204bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
1205 if (!CU)
1206 return false;
1207 if (!NodesSeen.insert(CU))
1208 return false;
1209
1210 CUs.push_back(CU);
1211 return true;
1212}
1213
1214/// addGlobalVariable - Add global variable into GVs.
1215bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
1216 if (!DIG)
1217 return false;
1218
1219 if (!NodesSeen.insert(DIG))
1220 return false;
1221
1222 GVs.push_back(DIG);
1223 return true;
1224}
1225
1226// addSubprogram - Add subprgoram into SPs.
1227bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
1228 if (!SP)
1229 return false;
1230
1231 if (!NodesSeen.insert(SP))
1232 return false;
1233
1234 SPs.push_back(SP);
1235 return true;
1236}
1237
1238bool DebugInfoFinder::addScope(DIScope Scope) {
1239 if (!Scope)
1240 return false;
1241 // FIXME: Ocaml binding generates a scope with no content, we treat it
1242 // as null for now.
1243 if (Scope->getNumOperands() == 0)
1244 return false;
1245 if (!NodesSeen.insert(Scope))
1246 return false;
1247 Scopes.push_back(Scope);
1248 return true;
1249}
1250
1251//===----------------------------------------------------------------------===//
1252// DIDescriptor: dump routines for all descriptors.
1253//===----------------------------------------------------------------------===//
1254
1255/// dump - Print descriptor to dbgs() with a newline.
1256void DIDescriptor::dump() const {
1257 print(dbgs());
1258 dbgs() << '\n';
1259}
1260
1261/// print - Print descriptor.
1262void DIDescriptor::print(raw_ostream &OS) const {
1263 if (!DbgNode)
1264 return;
1265
1266 if (const char *Tag = dwarf::TagString(getTag()))
1267 OS << "[ " << Tag << " ]";
1268
1269 if (this->isSubrange()) {
1270 DISubrange(DbgNode).printInternal(OS);
1271 } else if (this->isCompileUnit()) {
1272 DICompileUnit(DbgNode).printInternal(OS);
1273 } else if (this->isFile()) {
1274 DIFile(DbgNode).printInternal(OS);
1275 } else if (this->isEnumerator()) {
1276 DIEnumerator(DbgNode).printInternal(OS);
1277 } else if (this->isBasicType()) {
1278 DIType(DbgNode).printInternal(OS);
1279 } else if (this->isDerivedType()) {
1280 DIDerivedType(DbgNode).printInternal(OS);
1281 } else if (this->isCompositeType()) {
1282 DICompositeType(DbgNode).printInternal(OS);
1283 } else if (this->isSubprogram()) {
1284 DISubprogram(DbgNode).printInternal(OS);
1285 } else if (this->isGlobalVariable()) {
1286 DIGlobalVariable(DbgNode).printInternal(OS);
1287 } else if (this->isVariable()) {
1288 DIVariable(DbgNode).printInternal(OS);
1289 } else if (this->isObjCProperty()) {
1290 DIObjCProperty(DbgNode).printInternal(OS);
1291 } else if (this->isNameSpace()) {
1292 DINameSpace(DbgNode).printInternal(OS);
1293 } else if (this->isScope()) {
1294 DIScope(DbgNode).printInternal(OS);
1295 }
1296}
1297
1298void DISubrange::printInternal(raw_ostream &OS) const {
1299 int64_t Count = getCount();
1300 if (Count != -1)
1301 OS << " [" << getLo() << ", " << Count - 1 << ']';
1302 else
1303 OS << " [unbounded]";
1304}
1305
1306void DIScope::printInternal(raw_ostream &OS) const {
1307 OS << " [" << getDirectory() << "/" << getFilename() << ']';
1308}
1309
1310void DICompileUnit::printInternal(raw_ostream &OS) const {
1311 DIScope::printInternal(OS);
1312 OS << " [";
1313 unsigned Lang = getLanguage();
1314 if (const char *LangStr = dwarf::LanguageString(Lang))
1315 OS << LangStr;
1316 else
1317 (OS << "lang 0x").write_hex(Lang);
1318 OS << ']';
1319}
1320
1321void DIEnumerator::printInternal(raw_ostream &OS) const {
1322 OS << " [" << getName() << " :: " << getEnumValue() << ']';
1323}
1324
1325void DIType::printInternal(raw_ostream &OS) const {
Manman Renf93ac4b2014-07-29 18:20:39 +00001326 if (!DbgNode)
Bill Wendling523bea82013-11-08 08:13:15 +00001327 return;
1328
1329 StringRef Res = getName();
1330 if (!Res.empty())
1331 OS << " [" << Res << "]";
1332
1333 // TODO: Print context?
1334
1335 OS << " [line " << getLineNumber() << ", size " << getSizeInBits()
1336 << ", align " << getAlignInBits() << ", offset " << getOffsetInBits();
1337 if (isBasicType())
1338 if (const char *Enc =
1339 dwarf::AttributeEncodingString(DIBasicType(DbgNode).getEncoding()))
1340 OS << ", enc " << Enc;
1341 OS << "]";
1342
1343 if (isPrivate())
1344 OS << " [private]";
1345 else if (isProtected())
1346 OS << " [protected]";
1347
1348 if (isArtificial())
1349 OS << " [artificial]";
1350
1351 if (isForwardDecl())
1352 OS << " [decl]";
1353 else if (getTag() == dwarf::DW_TAG_structure_type ||
1354 getTag() == dwarf::DW_TAG_union_type ||
1355 getTag() == dwarf::DW_TAG_enumeration_type ||
1356 getTag() == dwarf::DW_TAG_class_type)
1357 OS << " [def]";
1358 if (isVector())
1359 OS << " [vector]";
1360 if (isStaticMember())
1361 OS << " [static]";
Adrian Prantl99c7af22013-12-18 21:48:19 +00001362
1363 if (isLValueReference())
1364 OS << " [reference]";
1365
1366 if (isRValueReference())
1367 OS << " [rvalue reference]";
Bill Wendling523bea82013-11-08 08:13:15 +00001368}
1369
1370void DIDerivedType::printInternal(raw_ostream &OS) const {
1371 DIType::printInternal(OS);
1372 OS << " [from " << getTypeDerivedFrom().getName() << ']';
1373}
1374
1375void DICompositeType::printInternal(raw_ostream &OS) const {
1376 DIType::printInternal(OS);
Manman Renab8ffba2014-07-28 19:14:13 +00001377 DIArray A = getElements();
Bill Wendling523bea82013-11-08 08:13:15 +00001378 OS << " [" << A.getNumElements() << " elements]";
1379}
1380
1381void DINameSpace::printInternal(raw_ostream &OS) const {
1382 StringRef Name = getName();
1383 if (!Name.empty())
1384 OS << " [" << Name << ']';
1385
1386 OS << " [line " << getLineNumber() << ']';
1387}
1388
1389void DISubprogram::printInternal(raw_ostream &OS) const {
1390 // TODO : Print context
1391 OS << " [line " << getLineNumber() << ']';
1392
1393 if (isLocalToUnit())
1394 OS << " [local]";
1395
1396 if (isDefinition())
1397 OS << " [def]";
1398
1399 if (getScopeLineNumber() != getLineNumber())
1400 OS << " [scope " << getScopeLineNumber() << "]";
1401
1402 if (isPrivate())
1403 OS << " [private]";
1404 else if (isProtected())
1405 OS << " [protected]";
1406
Adrian Prantl99c7af22013-12-18 21:48:19 +00001407 if (isLValueReference())
1408 OS << " [reference]";
1409
1410 if (isRValueReference())
1411 OS << " [rvalue reference]";
1412
Bill Wendling523bea82013-11-08 08:13:15 +00001413 StringRef Res = getName();
1414 if (!Res.empty())
1415 OS << " [" << Res << ']';
1416}
1417
1418void DIGlobalVariable::printInternal(raw_ostream &OS) const {
1419 StringRef Res = getName();
1420 if (!Res.empty())
1421 OS << " [" << Res << ']';
1422
1423 OS << " [line " << getLineNumber() << ']';
1424
1425 // TODO : Print context
1426
1427 if (isLocalToUnit())
1428 OS << " [local]";
1429
1430 if (isDefinition())
1431 OS << " [def]";
1432}
1433
1434void DIVariable::printInternal(raw_ostream &OS) const {
1435 StringRef Res = getName();
1436 if (!Res.empty())
1437 OS << " [" << Res << ']';
1438
1439 OS << " [line " << getLineNumber() << ']';
Adrian Prantlb1416832014-08-01 22:11:58 +00001440
1441 if (isVariablePiece())
1442 OS << " [piece, size " << getPieceSize()
1443 << ", offset " << getPieceOffset() << ']';
Bill Wendling523bea82013-11-08 08:13:15 +00001444}
1445
1446void DIObjCProperty::printInternal(raw_ostream &OS) const {
1447 StringRef Name = getObjCPropertyName();
1448 if (!Name.empty())
1449 OS << " [" << Name << ']';
1450
1451 OS << " [line " << getLineNumber() << ", properties " << getUnsignedField(6)
1452 << ']';
1453}
1454
1455static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
1456 const LLVMContext &Ctx) {
1457 if (!DL.isUnknown()) { // Print source line info.
1458 DIScope Scope(DL.getScope(Ctx));
1459 assert(Scope.isScope() && "Scope of a DebugLoc should be a DIScope.");
1460 // Omit the directory, because it's likely to be long and uninteresting.
1461 CommentOS << Scope.getFilename();
1462 CommentOS << ':' << DL.getLine();
1463 if (DL.getCol() != 0)
1464 CommentOS << ':' << DL.getCol();
1465 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
1466 if (!InlinedAtDL.isUnknown()) {
1467 CommentOS << " @[ ";
1468 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
1469 CommentOS << " ]";
1470 }
1471 }
1472}
1473
1474void DIVariable::printExtendedName(raw_ostream &OS) const {
1475 const LLVMContext &Ctx = DbgNode->getContext();
1476 StringRef Res = getName();
1477 if (!Res.empty())
1478 OS << Res << "," << getLineNumber();
1479 if (MDNode *InlinedAt = getInlinedAt()) {
1480 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
1481 if (!InlinedAtDL.isUnknown()) {
1482 OS << " @[";
1483 printDebugLoc(InlinedAtDL, OS, Ctx);
1484 OS << "]";
1485 }
1486 }
1487}
1488
1489/// Specialize constructor to make sure it has the correct type.
1490template <> DIRef<DIScope>::DIRef(const Value *V) : Val(V) {
1491 assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode");
1492}
1493template <> DIRef<DIType>::DIRef(const Value *V) : Val(V) {
1494 assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode");
1495}
1496
1497/// Specialize getFieldAs to handle fields that are references to DIScopes.
1498template <>
1499DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const {
1500 return DIScopeRef(getField(DbgNode, Elt));
1501}
1502/// Specialize getFieldAs to handle fields that are references to DITypes.
1503template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const {
1504 return DITypeRef(getField(DbgNode, Elt));
1505}
Manman Rencb14bbc2013-11-22 22:06:31 +00001506
1507/// Strip debug info in the module if it exists.
1508/// To do this, we remove all calls to the debugger intrinsics and any named
1509/// metadata for debugging. We also remove debug locations for instructions.
1510/// Return true if module is modified.
1511bool llvm::StripDebugInfo(Module &M) {
1512
1513 bool Changed = false;
1514
1515 // Remove all of the calls to the debugger intrinsics, and remove them from
1516 // the module.
1517 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
1518 while (!Declare->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +00001519 CallInst *CI = cast<CallInst>(Declare->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +00001520 CI->eraseFromParent();
1521 }
1522 Declare->eraseFromParent();
1523 Changed = true;
1524 }
1525
1526 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
1527 while (!DbgVal->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +00001528 CallInst *CI = cast<CallInst>(DbgVal->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +00001529 CI->eraseFromParent();
1530 }
1531 DbgVal->eraseFromParent();
1532 Changed = true;
1533 }
1534
1535 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
1536 NME = M.named_metadata_end(); NMI != NME;) {
1537 NamedMDNode *NMD = NMI;
1538 ++NMI;
1539 if (NMD->getName().startswith("llvm.dbg.")) {
1540 NMD->eraseFromParent();
1541 Changed = true;
1542 }
1543 }
1544
1545 for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
1546 for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE;
1547 ++FI)
1548 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE;
1549 ++BI) {
1550 if (!BI->getDebugLoc().isUnknown()) {
1551 Changed = true;
1552 BI->setDebugLoc(DebugLoc());
1553 }
1554 }
1555
1556 return Changed;
1557}
Manman Ren8b4306c2013-12-02 21:29:56 +00001558
Manman Renbd4daf82013-12-03 00:12:14 +00001559/// Return Debug Info Metadata Version by checking module flags.
1560unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
Manman Ren8b4306c2013-12-02 21:29:56 +00001561 Value *Val = M.getModuleFlag("Debug Info Version");
1562 if (!Val)
1563 return 0;
1564 return cast<ConstantInt>(Val)->getZExtValue();
1565}
David Blaikie6876b3b2014-07-01 20:05:26 +00001566
David Blaikiea8c35092014-07-02 18:30:05 +00001567llvm::DenseMap<const llvm::Function *, llvm::DISubprogram>
1568llvm::makeSubprogramMap(const Module &M) {
1569 DenseMap<const Function *, DISubprogram> R;
David Blaikie6876b3b2014-07-01 20:05:26 +00001570
1571 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
1572 if (!CU_Nodes)
1573 return R;
1574
1575 for (MDNode *N : CU_Nodes->operands()) {
1576 DICompileUnit CUNode(N);
1577 DIArray SPs = CUNode.getSubprograms();
1578 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
1579 DISubprogram SP(SPs.getElement(i));
1580 if (Function *F = SP.getFunction())
1581 R.insert(std::make_pair(F, SP));
1582 }
1583 }
1584 return R;
1585}