blob: ad0b98e90c07cf098a95895012df43297338ee93 [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"
Duncan P. N. Exon Smithc22a5c22015-02-21 00:43:09 +000020#include "llvm/ADT/StringSwitch.h"
Bill Wendling523bea82013-11-08 08:13:15 +000021#include "llvm/Analysis/ValueTracking.h"
22#include "llvm/IR/Constants.h"
Adrian Prantlb1416832014-08-01 22:11:58 +000023#include "llvm/IR/DIBuilder.h"
Bill Wendling523bea82013-11-08 08:13:15 +000024#include "llvm/IR/DerivedTypes.h"
25#include "llvm/IR/Instructions.h"
26#include "llvm/IR/IntrinsicInst.h"
27#include "llvm/IR/Intrinsics.h"
28#include "llvm/IR/Module.h"
Chandler Carruth4220e9c2014-03-04 11:17:44 +000029#include "llvm/IR/ValueHandle.h"
Bill Wendling523bea82013-11-08 08:13:15 +000030#include "llvm/Support/Debug.h"
31#include "llvm/Support/Dwarf.h"
Bill Wendling523bea82013-11-08 08:13:15 +000032#include "llvm/Support/raw_ostream.h"
33using namespace llvm;
34using namespace llvm::dwarf;
35
36//===----------------------------------------------------------------------===//
37// DIDescriptor
38//===----------------------------------------------------------------------===//
39
Duncan P. N. Exon Smithc22a5c22015-02-21 00:43:09 +000040unsigned DIDescriptor::getFlag(StringRef Flag) {
41 return StringSwitch<unsigned>(Flag)
42#define HANDLE_DI_FLAG(ID, NAME) .Case("DIFlag" #NAME, Flag##NAME)
43#include "llvm/IR/DebugInfoFlags.def"
44 .Default(0);
45}
46
47const char *DIDescriptor::getFlagString(unsigned Flag) {
48 switch (Flag) {
49 default:
50 return "";
51#define HANDLE_DI_FLAG(ID, NAME) \
52 case Flag##NAME: \
53 return "DIFlag" #NAME;
54#include "llvm/IR/DebugInfoFlags.def"
55 }
56}
57
Duncan P. N. Exon Smith269e38d2015-02-21 00:45:26 +000058unsigned DIDescriptor::splitFlags(unsigned Flags,
59 SmallVectorImpl<unsigned> &SplitFlags) {
60 // Accessibility flags need to be specially handled, since they're packed
61 // together.
62 if (unsigned A = Flags & FlagAccessibility) {
63 if (A == FlagPrivate)
64 SplitFlags.push_back(FlagPrivate);
65 else if (A == FlagProtected)
66 SplitFlags.push_back(FlagProtected);
67 else
68 SplitFlags.push_back(FlagPublic);
69 Flags &= ~A;
70 }
71
72#define HANDLE_DI_FLAG(ID, NAME) \
73 if (unsigned Bit = Flags & ID) { \
74 SplitFlags.push_back(Bit); \
75 Flags &= ~Bit; \
76 }
77#include "llvm/IR/DebugInfoFlags.def"
78
79 return Flags;
80}
81
Bill Wendling523bea82013-11-08 08:13:15 +000082bool DIDescriptor::Verify() const {
83 return DbgNode &&
84 (DIDerivedType(DbgNode).Verify() ||
85 DICompositeType(DbgNode).Verify() || DIBasicType(DbgNode).Verify() ||
86 DIVariable(DbgNode).Verify() || DISubprogram(DbgNode).Verify() ||
87 DIGlobalVariable(DbgNode).Verify() || DIFile(DbgNode).Verify() ||
88 DICompileUnit(DbgNode).Verify() || DINameSpace(DbgNode).Verify() ||
89 DILexicalBlock(DbgNode).Verify() ||
90 DILexicalBlockFile(DbgNode).Verify() ||
91 DISubrange(DbgNode).Verify() || DIEnumerator(DbgNode).Verify() ||
92 DIObjCProperty(DbgNode).Verify() ||
93 DITemplateTypeParameter(DbgNode).Verify() ||
94 DITemplateValueParameter(DbgNode).Verify() ||
Duncan P. N. Exon Smithe9d379c2015-03-16 21:03:55 +000095 DIImportedEntity(DbgNode).Verify());
Bill Wendling523bea82013-11-08 08:13:15 +000096}
97
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000098static Metadata *getField(const MDNode *DbgNode, unsigned Elt) {
Craig Topperc6207612014-04-09 06:08:46 +000099 if (!DbgNode || Elt >= DbgNode->getNumOperands())
100 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +0000101 return DbgNode->getOperand(Elt);
102}
103
104static MDNode *getNodeField(const MDNode *DbgNode, unsigned Elt) {
105 return dyn_cast_or_null<MDNode>(getField(DbgNode, Elt));
106}
107
108static StringRef getStringField(const MDNode *DbgNode, unsigned Elt) {
109 if (MDString *MDS = dyn_cast_or_null<MDString>(getField(DbgNode, Elt)))
110 return MDS->getString();
111 return StringRef();
112}
113
114StringRef DIDescriptor::getStringField(unsigned Elt) const {
115 return ::getStringField(DbgNode, Elt);
116}
117
118uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000119 if (auto *C = getConstantField(Elt))
120 if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
Bill Wendling523bea82013-11-08 08:13:15 +0000121 return CI->getZExtValue();
122
123 return 0;
124}
125
126int64_t DIDescriptor::getInt64Field(unsigned Elt) const {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000127 if (auto *C = getConstantField(Elt))
128 if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
129 return CI->getZExtValue();
Bill Wendling523bea82013-11-08 08:13:15 +0000130
131 return 0;
132}
133
134DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
135 MDNode *Field = getNodeField(DbgNode, Elt);
136 return DIDescriptor(Field);
137}
138
139GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000140 return dyn_cast_or_null<GlobalVariable>(getConstantField(Elt));
Bill Wendling523bea82013-11-08 08:13:15 +0000141}
142
143Constant *DIDescriptor::getConstantField(unsigned Elt) const {
Craig Topperc6207612014-04-09 06:08:46 +0000144 if (!DbgNode)
145 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +0000146
147 if (Elt < DbgNode->getNumOperands())
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000148 if (auto *C =
149 dyn_cast_or_null<ConstantAsMetadata>(DbgNode->getOperand(Elt)))
150 return C->getValue();
Craig Topperc6207612014-04-09 06:08:46 +0000151 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +0000152}
153
154Function *DIDescriptor::getFunctionField(unsigned Elt) const {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000155 return dyn_cast_or_null<Function>(getConstantField(Elt));
Bill Wendling523bea82013-11-08 08:13:15 +0000156}
157
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000158/// \brief Return the size reported by the variable's type.
Adrian Prantlb1416832014-08-01 22:11:58 +0000159unsigned DIVariable::getSizeInBits(const DITypeIdentifierMap &Map) {
160 DIType Ty = getType().resolve(Map);
161 // Follow derived types until we reach a type that
162 // reports back a size.
163 while (Ty.isDerivedType() && !Ty.getSizeInBits()) {
164 DIDerivedType DT(&*Ty);
165 Ty = DT.getTypeDerivedFrom().resolve(Map);
166 }
167 assert(Ty.getSizeInBits() && "type with size 0");
168 return Ty.getSizeInBits();
169}
170
Adrian Prantl27bd01f2015-02-09 23:57:15 +0000171bool DIExpression::isBitPiece() const {
Adrian Prantl34bcbee2015-01-21 00:59:20 +0000172 unsigned N = getNumElements();
Adrian Prantl27bd01f2015-02-09 23:57:15 +0000173 return N >=3 && getElement(N-3) == dwarf::DW_OP_bit_piece;
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000174}
175
Adrian Prantl27bd01f2015-02-09 23:57:15 +0000176uint64_t DIExpression::getBitPieceOffset() const {
177 assert(isBitPiece() && "not a piece");
Adrian Prantl34bcbee2015-01-21 00:59:20 +0000178 return getElement(getNumElements()-2);
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000179}
180
Adrian Prantl27bd01f2015-02-09 23:57:15 +0000181uint64_t DIExpression::getBitPieceSize() const {
182 assert(isBitPiece() && "not a piece");
Adrian Prantl34bcbee2015-01-21 00:59:20 +0000183 return getElement(getNumElements()-1);
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000184}
Adrian Prantlb1416832014-08-01 22:11:58 +0000185
Adrian Prantl0f615792015-03-04 17:39:33 +0000186DIExpression::iterator DIExpression::Operand::getNext() const {
Adrian Prantl70f2a732015-01-23 23:40:47 +0000187 iterator it(I);
Adrian Prantl0f615792015-03-04 17:39:33 +0000188 return ++it;
Adrian Prantl70f2a732015-01-23 23:40:47 +0000189}
190
Bill Wendling523bea82013-11-08 08:13:15 +0000191//===----------------------------------------------------------------------===//
Bill Wendling523bea82013-11-08 08:13:15 +0000192// Simple Descriptor Constructors and other Methods
193//===----------------------------------------------------------------------===//
194
Duncan P. N. Exon Smith9c3b8942015-02-28 23:48:02 +0000195void DIDescriptor::replaceAllUsesWith(LLVMContext &, DIDescriptor D) {
Bill Wendling523bea82013-11-08 08:13:15 +0000196 assert(DbgNode && "Trying to replace an unverified type!");
Duncan P. N. Exon Smith9c3b8942015-02-28 23:48:02 +0000197 assert(DbgNode->isTemporary() && "Expected temporary node");
198 TempMDNode Temp(get());
Bill Wendling523bea82013-11-08 08:13:15 +0000199
200 // Since we use a TrackingVH for the node, its easy for clients to manufacture
201 // legitimate situations where they want to replaceAllUsesWith() on something
202 // which, due to uniquing, has merged with the source. We shield clients from
203 // this detail by allowing a value to be replaced with replaceAllUsesWith()
204 // itself.
Duncan P. N. Exon Smith9c3b8942015-02-28 23:48:02 +0000205 if (Temp.get() == D.get()) {
206 DbgNode = MDNode::replaceWithUniqued(std::move(Temp));
207 return;
Bill Wendling523bea82013-11-08 08:13:15 +0000208 }
David Blaikied3f094a2014-05-06 03:41:57 +0000209
Duncan P. N. Exon Smith9c3b8942015-02-28 23:48:02 +0000210 Temp->replaceAllUsesWith(D.get());
211 DbgNode = D.get();
Bill Wendling523bea82013-11-08 08:13:15 +0000212}
213
Frederic Riss36acf0f2014-09-15 07:50:36 +0000214void DIDescriptor::replaceAllUsesWith(MDNode *D) {
Bill Wendling523bea82013-11-08 08:13:15 +0000215 assert(DbgNode && "Trying to replace an unverified type!");
David Blaikied3f094a2014-05-06 03:41:57 +0000216 assert(DbgNode != D && "This replacement should always happen");
Duncan P. N. Exon Smith946fdcc2015-01-19 20:36:39 +0000217 assert(DbgNode->isTemporary() && "Expected temporary node");
Duncan P. N. Exon Smith9c3b8942015-02-28 23:48:02 +0000218 TempMDNode Node(get());
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000219 Node->replaceAllUsesWith(D);
Bill Wendling523bea82013-11-08 08:13:15 +0000220}
221
Bill Wendling523bea82013-11-08 08:13:15 +0000222bool DICompileUnit::Verify() const {
223 if (!isCompileUnit())
224 return false;
225
226 // Don't bother verifying the compilation directory or producer string
227 // as those could be empty.
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000228 return !getFilename().empty();
Bill Wendling523bea82013-11-08 08:13:15 +0000229}
230
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000231bool DIObjCProperty::Verify() const { return isObjCProperty(); }
Bill Wendling523bea82013-11-08 08:13:15 +0000232
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000233/// \brief Check if a value can be a reference to a type.
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000234static bool isTypeRef(const Metadata *MD) {
235 if (!MD)
236 return true;
237 if (auto *S = dyn_cast<MDString>(MD))
238 return !S->getString().empty();
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000239 return isa<MDType>(MD);
Bill Wendling523bea82013-11-08 08:13:15 +0000240}
241
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000242/// \brief Check if a value can be a ScopeRef.
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000243static bool isScopeRef(const Metadata *MD) {
244 if (!MD)
245 return true;
246 if (auto *S = dyn_cast<MDString>(MD))
247 return !S->getString().empty();
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000248 return isa<MDScope>(MD);
Bill Wendling523bea82013-11-08 08:13:15 +0000249}
250
Duncan P. N. Exon Smithe4450142015-02-18 19:56:50 +0000251#ifndef NDEBUG
Duncan P. N. Exon Smith2a78e9b2015-02-18 19:39:36 +0000252/// \brief Check if a value can be a DescriptorRef.
253static bool isDescriptorRef(const Metadata *MD) {
254 if (!MD)
255 return true;
256 if (auto *S = dyn_cast<MDString>(MD))
257 return !S->getString().empty();
258 return isa<MDNode>(MD);
259}
Duncan P. N. Exon Smithe4450142015-02-18 19:56:50 +0000260#endif
Duncan P. N. Exon Smith2a78e9b2015-02-18 19:39:36 +0000261
Bill Wendling523bea82013-11-08 08:13:15 +0000262bool DIType::Verify() const {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000263 auto *N = dyn_cast_or_null<MDType>(DbgNode);
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000264 if (!N)
Bill Wendling523bea82013-11-08 08:13:15 +0000265 return false;
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000266 if (!isScopeRef(N->getScope()))
Bill Wendling523bea82013-11-08 08:13:15 +0000267 return false;
268
Bill Wendling523bea82013-11-08 08:13:15 +0000269 // DIType is abstract, it should be a BasicType, a DerivedType or
270 // a CompositeType.
271 if (isBasicType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000272 return DIBasicType(DbgNode).Verify();
Duncan P. N. Exon Smithf3740402015-03-16 20:46:27 +0000273
274 // FIXME: Sink this into the various subclass verifies.
275 if (getFilename().empty()) {
276 // Check whether the filename is allowed to be empty.
277 uint16_t Tag = getTag();
278 if (Tag != dwarf::DW_TAG_const_type && Tag != dwarf::DW_TAG_volatile_type &&
279 Tag != dwarf::DW_TAG_pointer_type &&
280 Tag != dwarf::DW_TAG_ptr_to_member_type &&
281 Tag != dwarf::DW_TAG_reference_type &&
282 Tag != dwarf::DW_TAG_rvalue_reference_type &&
283 Tag != dwarf::DW_TAG_restrict_type && Tag != dwarf::DW_TAG_array_type &&
284 Tag != dwarf::DW_TAG_enumeration_type &&
285 Tag != dwarf::DW_TAG_subroutine_type &&
286 Tag != dwarf::DW_TAG_inheritance && Tag != dwarf::DW_TAG_friend)
287 return false;
288 }
289
290 if (isCompositeType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000291 return DICompositeType(DbgNode).Verify();
Duncan P. N. Exon Smithf3740402015-03-16 20:46:27 +0000292 if (isDerivedType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000293 return DIDerivedType(DbgNode).Verify();
Duncan P. N. Exon Smithf3740402015-03-16 20:46:27 +0000294 return false;
Bill Wendling523bea82013-11-08 08:13:15 +0000295}
296
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000297bool DIBasicType::Verify() const {
298 return dyn_cast_or_null<MDBasicType>(DbgNode);
299}
Bill Wendling523bea82013-11-08 08:13:15 +0000300
Bill Wendling523bea82013-11-08 08:13:15 +0000301bool DIDerivedType::Verify() const {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000302 auto *N = dyn_cast_or_null<MDDerivedTypeBase>(DbgNode);
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000303 if (!N)
Bill Wendling523bea82013-11-08 08:13:15 +0000304 return false;
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000305 if (getTag() == dwarf::DW_TAG_ptr_to_member_type) {
306 auto *D = dyn_cast<MDDerivedType>(N);
307 if (!D)
Bill Wendling523bea82013-11-08 08:13:15 +0000308 return false;
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000309 if (!isTypeRef(D->getExtraData()))
310 return false;
311 }
312 return isTypeRef(N->getBaseType());
Bill Wendling523bea82013-11-08 08:13:15 +0000313}
314
Bill Wendling523bea82013-11-08 08:13:15 +0000315bool DICompositeType::Verify() const {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000316 auto *N = dyn_cast_or_null<MDCompositeTypeBase>(DbgNode);
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000317 return N && isTypeRef(N->getBaseType()) && isTypeRef(N->getVTableHolder()) &&
318 !(isLValueReference() && isRValueReference());
Bill Wendling523bea82013-11-08 08:13:15 +0000319}
320
Bill Wendling523bea82013-11-08 08:13:15 +0000321bool DISubprogram::Verify() const {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000322 auto *N = dyn_cast_or_null<MDSubprogram>(DbgNode);
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000323 if (!N)
Bill Wendling523bea82013-11-08 08:13:15 +0000324 return false;
325
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000326 if (!isScopeRef(N->getScope()))
Bill Wendling523bea82013-11-08 08:13:15 +0000327 return false;
Adrian Prantl99c7af22013-12-18 21:48:19 +0000328
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000329 if (auto *Op = N->getType())
330 if (!isa<MDNode>(Op))
331 return false;
332
333 if (!isTypeRef(getContainingType()))
334 return false;
335
Adrian Prantl99c7af22013-12-18 21:48:19 +0000336 if (isLValueReference() && isRValueReference())
337 return false;
338
David Blaikie3dfe4782014-10-14 18:22:52 +0000339 // If a DISubprogram has an llvm::Function*, then scope chains from all
340 // instructions within the function should lead to this DISubprogram.
341 if (auto *F = getFunction()) {
David Blaikie3dfe4782014-10-14 18:22:52 +0000342 for (auto &BB : *F) {
343 for (auto &I : BB) {
344 DebugLoc DL = I.getDebugLoc();
345 if (DL.isUnknown())
346 continue;
347
348 MDNode *Scope = nullptr;
349 MDNode *IA = nullptr;
350 // walk the inlined-at scopes
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000351 while ((IA = DL.getInlinedAt()))
David Blaikie3dfe4782014-10-14 18:22:52 +0000352 DL = DebugLoc::getFromDILocation(IA);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000353 DL.getScopeAndInlinedAt(Scope, IA);
Adrian Prantlde200df2015-01-20 22:37:25 +0000354 if (!Scope)
355 return false;
David Blaikie3dfe4782014-10-14 18:22:52 +0000356 assert(!IA);
357 while (!DIDescriptor(Scope).isSubprogram()) {
358 DILexicalBlockFile D(Scope);
359 Scope = D.isLexicalBlockFile()
360 ? D.getScope()
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000361 : DebugLoc::getFromDILexicalBlock(Scope).getScope();
Adrian Prantl1292e242015-01-21 18:32:56 +0000362 if (!Scope)
363 return false;
David Blaikie3dfe4782014-10-14 18:22:52 +0000364 }
365 if (!DISubprogram(Scope).describes(F))
366 return false;
367 }
368 }
369 }
Bill Wendling523bea82013-11-08 08:13:15 +0000370
Adrian Prantl9260cca2015-01-22 00:00:52 +0000371 return true;
Bill Wendling523bea82013-11-08 08:13:15 +0000372}
373
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000374bool DIGlobalVariable::Verify() const {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000375 auto *N = dyn_cast_or_null<MDGlobalVariable>(DbgNode);
Bill Wendling523bea82013-11-08 08:13:15 +0000376
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000377 if (!N)
Bill Wendling523bea82013-11-08 08:13:15 +0000378 return false;
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000379
380 if (N->getDisplayName().empty())
381 return false;
382
383 if (auto *Op = N->getScope())
384 if (!isa<MDNode>(Op))
385 return false;
386
387 if (auto *Op = N->getStaticDataMemberDeclaration())
388 if (!isa<MDNode>(Op))
389 return false;
390
391 return isTypeRef(N->getType());
Bill Wendling523bea82013-11-08 08:13:15 +0000392}
393
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000394bool DIVariable::Verify() const {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000395 auto *N = dyn_cast_or_null<MDLocalVariable>(DbgNode);
Bill Wendling523bea82013-11-08 08:13:15 +0000396
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000397 if (!N)
398 return false;
399
400 if (auto *Op = N->getScope())
401 if (!isa<MDNode>(Op))
402 return false;
403
404 return isTypeRef(N->getType());
Bill Wendling523bea82013-11-08 08:13:15 +0000405}
406
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000407bool DILocation::Verify() const {
408 return dyn_cast_or_null<MDLocation>(DbgNode);
409}
410bool DINameSpace::Verify() const {
411 return dyn_cast_or_null<MDNamespace>(DbgNode);
412}
413bool DIFile::Verify() const { return dyn_cast_or_null<MDFile>(DbgNode); }
414bool DIEnumerator::Verify() const {
415 return dyn_cast_or_null<MDEnumerator>(DbgNode);
416}
417bool DISubrange::Verify() const {
418 return dyn_cast_or_null<MDSubrange>(DbgNode);
419}
420bool DILexicalBlock::Verify() const {
421 return dyn_cast_or_null<MDLexicalBlock>(DbgNode);
422}
423bool DILexicalBlockFile::Verify() const {
424 return dyn_cast_or_null<MDLexicalBlockFile>(DbgNode);
425}
426bool DITemplateTypeParameter::Verify() const {
427 return dyn_cast_or_null<MDTemplateTypeParameter>(DbgNode);
428}
429bool DITemplateValueParameter::Verify() const {
430 return dyn_cast_or_null<MDTemplateValueParameter>(DbgNode);
431}
432bool DIImportedEntity::Verify() const {
433 return dyn_cast_or_null<MDImportedEntity>(DbgNode);
434}
Bill Wendling523bea82013-11-08 08:13:15 +0000435
Manman Ren1a125c92014-07-28 19:33:20 +0000436void DICompositeType::setArraysHelper(MDNode *Elements, MDNode *TParams) {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000437 TypedTrackingMDRef<MDCompositeTypeBase> N(get());
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000438 if (Elements)
439 N->replaceElements(cast<MDTuple>(Elements));
Bill Wendling523bea82013-11-08 08:13:15 +0000440 if (TParams)
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000441 N->replaceTemplateParams(cast<MDTuple>(TParams));
Bill Wendling523bea82013-11-08 08:13:15 +0000442 DbgNode = N;
443}
444
Bill Wendling523bea82013-11-08 08:13:15 +0000445DIScopeRef DIScope::getRef() const {
446 if (!isCompositeType())
447 return DIScopeRef(*this);
448 DICompositeType DTy(DbgNode);
449 if (!DTy.getIdentifier())
450 return DIScopeRef(*this);
451 return DIScopeRef(DTy.getIdentifier());
452}
453
Bill Wendling523bea82013-11-08 08:13:15 +0000454void DICompositeType::setContainingType(DICompositeType ContainingType) {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000455 TypedTrackingMDRef<MDCompositeTypeBase> N(get());
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000456 N->replaceVTableHolder(ContainingType.getRef());
Bill Wendling523bea82013-11-08 08:13:15 +0000457 DbgNode = N;
458}
459
Bill Wendling523bea82013-11-08 08:13:15 +0000460bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
461 assert(CurFn && "Invalid function");
462 if (!getContext().isSubprogram())
463 return false;
464 // This variable is not inlined function argument if its scope
465 // does not describe current function.
466 return !DISubprogram(getContext()).describes(CurFn);
467}
468
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000469Function *DISubprogram::getFunction() const {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000470 if (auto *N = get())
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000471 if (auto *C = dyn_cast_or_null<ConstantAsMetadata>(N->getFunction()))
472 return dyn_cast<Function>(C->getValue());
473 return nullptr;
474}
475
Bill Wendling523bea82013-11-08 08:13:15 +0000476bool DISubprogram::describes(const Function *F) {
477 assert(F && "Invalid function");
478 if (F == getFunction())
479 return true;
480 StringRef Name = getLinkageName();
481 if (Name.empty())
482 Name = getName();
483 if (F->getName() == Name)
484 return true;
485 return false;
486}
487
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000488GlobalVariable *DIGlobalVariable::getGlobal() const {
489 return dyn_cast_or_null<GlobalVariable>(getConstant());
Bill Wendling523bea82013-11-08 08:13:15 +0000490}
491
Bill Wendling523bea82013-11-08 08:13:15 +0000492DIScopeRef DIScope::getContext() const {
493
494 if (isType())
495 return DIType(DbgNode).getContext();
496
497 if (isSubprogram())
498 return DIScopeRef(DISubprogram(DbgNode).getContext());
499
500 if (isLexicalBlock())
501 return DIScopeRef(DILexicalBlock(DbgNode).getContext());
502
503 if (isLexicalBlockFile())
504 return DIScopeRef(DILexicalBlockFile(DbgNode).getContext());
505
506 if (isNameSpace())
507 return DIScopeRef(DINameSpace(DbgNode).getContext());
508
509 assert((isFile() || isCompileUnit()) && "Unhandled type of scope.");
Craig Topperc6207612014-04-09 06:08:46 +0000510 return DIScopeRef(nullptr);
Bill Wendling523bea82013-11-08 08:13:15 +0000511}
512
Bill Wendling523bea82013-11-08 08:13:15 +0000513StringRef DIScope::getName() const {
514 if (isType())
515 return DIType(DbgNode).getName();
516 if (isSubprogram())
517 return DISubprogram(DbgNode).getName();
518 if (isNameSpace())
519 return DINameSpace(DbgNode).getName();
520 assert((isLexicalBlock() || isLexicalBlockFile() || isFile() ||
521 isCompileUnit()) &&
522 "Unhandled type of scope.");
523 return StringRef();
524}
525
526StringRef DIScope::getFilename() const {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000527 if (auto *N = get())
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000528 return ::getStringField(dyn_cast_or_null<MDNode>(N->getFile()), 0);
529 return "";
Bill Wendling523bea82013-11-08 08:13:15 +0000530}
531
532StringRef DIScope::getDirectory() const {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000533 if (auto *N = get())
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000534 return ::getStringField(dyn_cast_or_null<MDNode>(N->getFile()), 1);
535 return "";
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000536}
537
538void DICompileUnit::replaceSubprograms(DIArray Subprograms) {
539 assert(Verify() && "Expected compile unit");
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000540 get()->replaceSubprograms(cast_or_null<MDTuple>(Subprograms.get()));
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000541}
542
543void DICompileUnit::replaceGlobalVariables(DIArray GlobalVariables) {
544 assert(Verify() && "Expected compile unit");
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000545 get()->replaceGlobalVariables(cast_or_null<MDTuple>(GlobalVariables.get()));
Bill Wendling523bea82013-11-08 08:13:15 +0000546}
547
Diego Novillof5041ce2014-03-03 20:06:11 +0000548DILocation DILocation::copyWithNewScope(LLVMContext &Ctx,
David Blaikie2f3f76f2014-08-21 22:45:21 +0000549 DILexicalBlockFile NewScope) {
Diego Novillof5041ce2014-03-03 20:06:11 +0000550 assert(Verify());
Duncan P. N. Exon Smith98854692015-01-14 22:27:36 +0000551 assert(NewScope && "Expected valid scope");
552
553 const auto *Old = cast<MDLocation>(DbgNode);
554 return DILocation(MDLocation::get(Ctx, Old->getLine(), Old->getColumn(),
555 NewScope, Old->getInlinedAt()));
Diego Novillof5041ce2014-03-03 20:06:11 +0000556}
557
Diego Novillof5041ce2014-03-03 20:06:11 +0000558unsigned DILocation::computeNewDiscriminator(LLVMContext &Ctx) {
559 std::pair<const char *, unsigned> Key(getFilename().data(), getLineNumber());
560 return ++Ctx.pImpl->DiscriminatorTable[Key];
561}
562
Bill Wendling523bea82013-11-08 08:13:15 +0000563DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
564 LLVMContext &VMContext) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000565 assert(DIVariable(DV).Verify() && "Expected a DIVariable");
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000566 return cast<MDLocalVariable>(DV)
567 ->withInline(cast_or_null<MDLocation>(InlinedScope));
Bill Wendling523bea82013-11-08 08:13:15 +0000568}
569
Bill Wendling523bea82013-11-08 08:13:15 +0000570DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000571 assert(DIVariable(DV).Verify() && "Expected a DIVariable");
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000572 return cast<MDLocalVariable>(DV)->withoutInline();
Bill Wendling523bea82013-11-08 08:13:15 +0000573}
574
Bill Wendling523bea82013-11-08 08:13:15 +0000575DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
576 DIDescriptor D(Scope);
577 if (D.isSubprogram())
578 return DISubprogram(Scope);
579
580 if (D.isLexicalBlockFile())
581 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
582
583 if (D.isLexicalBlock())
584 return getDISubprogram(DILexicalBlock(Scope).getContext());
585
586 return DISubprogram();
587}
588
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000589DISubprogram llvm::getDISubprogram(const Function *F) {
590 // We look for the first instr that has a debug annotation leading back to F.
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000591 for (auto &BB : *F) {
David Majnemerc758df42014-11-01 07:57:14 +0000592 auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) {
593 return !Inst.getDebugLoc().isUnknown();
594 });
595 if (Inst == BB.end())
596 continue;
597 DebugLoc DLoc = Inst->getDebugLoc();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000598 const MDNode *Scope = DLoc.getScopeNode();
David Majnemerc758df42014-11-01 07:57:14 +0000599 DISubprogram Subprogram = getDISubprogram(Scope);
600 return Subprogram.describes(F) ? Subprogram : DISubprogram();
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000601 }
602
603 return DISubprogram();
604}
605
Bill Wendling523bea82013-11-08 08:13:15 +0000606DICompositeType llvm::getDICompositeType(DIType T) {
607 if (T.isCompositeType())
608 return DICompositeType(T);
609
610 if (T.isDerivedType()) {
611 // This function is currently used by dragonegg and dragonegg does
612 // not generate identifier for types, so using an empty map to resolve
613 // DerivedFrom should be fine.
614 DITypeIdentifierMap EmptyMap;
615 return getDICompositeType(
616 DIDerivedType(T).getTypeDerivedFrom().resolve(EmptyMap));
617 }
618
619 return DICompositeType();
620}
621
Bill Wendling523bea82013-11-08 08:13:15 +0000622DITypeIdentifierMap
623llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
624 DITypeIdentifierMap Map;
625 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000626 DICompileUnit CU(CU_Nodes->getOperand(CUi));
Bill Wendling523bea82013-11-08 08:13:15 +0000627 DIArray Retain = CU.getRetainedTypes();
628 for (unsigned Ti = 0, Te = Retain.getNumElements(); Ti != Te; ++Ti) {
629 if (!Retain.getElement(Ti).isCompositeType())
630 continue;
631 DICompositeType Ty(Retain.getElement(Ti));
632 if (MDString *TypeId = Ty.getIdentifier()) {
633 // Definition has priority over declaration.
634 // Try to insert (TypeId, Ty) to Map.
635 std::pair<DITypeIdentifierMap::iterator, bool> P =
636 Map.insert(std::make_pair(TypeId, Ty));
637 // If TypeId already exists in Map and this is a definition, replace
638 // whatever we had (declaration or definition) with the definition.
639 if (!P.second && !Ty.isForwardDecl())
640 P.first->second = Ty;
641 }
642 }
643 }
644 return Map;
645}
646
647//===----------------------------------------------------------------------===//
648// DebugInfoFinder implementations.
649//===----------------------------------------------------------------------===//
650
651void DebugInfoFinder::reset() {
652 CUs.clear();
653 SPs.clear();
654 GVs.clear();
655 TYs.clear();
656 Scopes.clear();
657 NodesSeen.clear();
658 TypeIdentifierMap.clear();
Manman Ren2085ccc2013-11-17 18:42:37 +0000659 TypeMapInitialized = false;
660}
661
Manman Renb46e5502013-11-17 19:35:03 +0000662void DebugInfoFinder::InitializeTypeMap(const Module &M) {
Manman Ren2085ccc2013-11-17 18:42:37 +0000663 if (!TypeMapInitialized)
664 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
665 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
666 TypeMapInitialized = true;
667 }
Bill Wendling523bea82013-11-08 08:13:15 +0000668}
669
Bill Wendling523bea82013-11-08 08:13:15 +0000670void DebugInfoFinder::processModule(const Module &M) {
Manman Renb46e5502013-11-17 19:35:03 +0000671 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000672 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
Bill Wendling523bea82013-11-08 08:13:15 +0000673 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000674 DICompileUnit CU(CU_Nodes->getOperand(i));
Bill Wendling523bea82013-11-08 08:13:15 +0000675 addCompileUnit(CU);
676 DIArray GVs = CU.getGlobalVariables();
677 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
678 DIGlobalVariable DIG(GVs.getElement(i));
679 if (addGlobalVariable(DIG)) {
Manman Renf0a582b2014-11-21 19:55:23 +0000680 processScope(DIG.getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000681 processType(DIG.getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000682 }
683 }
684 DIArray SPs = CU.getSubprograms();
685 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
686 processSubprogram(DISubprogram(SPs.getElement(i)));
687 DIArray EnumTypes = CU.getEnumTypes();
688 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
689 processType(DIType(EnumTypes.getElement(i)));
690 DIArray RetainedTypes = CU.getRetainedTypes();
691 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
692 processType(DIType(RetainedTypes.getElement(i)));
693 DIArray Imports = CU.getImportedEntities();
694 for (unsigned i = 0, e = Imports.getNumElements(); i != e; ++i) {
695 DIImportedEntity Import = DIImportedEntity(Imports.getElement(i));
Duncan P. N. Exon Smithd4e07c92015-03-20 19:13:53 +0000696 if (!Import)
697 continue;
Adrian Prantld09ba232014-04-01 03:41:04 +0000698 DIDescriptor Entity = Import.getEntity().resolve(TypeIdentifierMap);
Bill Wendling523bea82013-11-08 08:13:15 +0000699 if (Entity.isType())
700 processType(DIType(Entity));
701 else if (Entity.isSubprogram())
702 processSubprogram(DISubprogram(Entity));
703 else if (Entity.isNameSpace())
704 processScope(DINameSpace(Entity).getContext());
705 }
706 }
707 }
708}
709
Manman Ren2085ccc2013-11-17 18:42:37 +0000710void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) {
Bill Wendling523bea82013-11-08 08:13:15 +0000711 if (!Loc)
712 return;
Manman Renb46e5502013-11-17 19:35:03 +0000713 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000714 processScope(Loc.getScope());
Manman Ren2085ccc2013-11-17 18:42:37 +0000715 processLocation(M, Loc.getOrigLocation());
Bill Wendling523bea82013-11-08 08:13:15 +0000716}
717
Bill Wendling523bea82013-11-08 08:13:15 +0000718void DebugInfoFinder::processType(DIType DT) {
719 if (!addType(DT))
720 return;
721 processScope(DT.getContext().resolve(TypeIdentifierMap));
722 if (DT.isCompositeType()) {
723 DICompositeType DCT(DT);
724 processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
Manman Renf8a19672014-07-28 22:24:06 +0000725 if (DT.isSubroutineType()) {
726 DITypeArray DTA = DISubroutineType(DT).getTypeArray();
727 for (unsigned i = 0, e = DTA.getNumElements(); i != e; ++i)
728 processType(DTA.getElement(i).resolve(TypeIdentifierMap));
729 return;
730 }
Manman Renab8ffba2014-07-28 19:14:13 +0000731 DIArray DA = DCT.getElements();
Bill Wendling523bea82013-11-08 08:13:15 +0000732 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
733 DIDescriptor D = DA.getElement(i);
734 if (D.isType())
735 processType(DIType(D));
736 else if (D.isSubprogram())
737 processSubprogram(DISubprogram(D));
738 }
739 } else if (DT.isDerivedType()) {
740 DIDerivedType DDT(DT);
741 processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
742 }
743}
744
745void DebugInfoFinder::processScope(DIScope Scope) {
746 if (Scope.isType()) {
747 DIType Ty(Scope);
748 processType(Ty);
749 return;
750 }
751 if (Scope.isCompileUnit()) {
752 addCompileUnit(DICompileUnit(Scope));
753 return;
754 }
755 if (Scope.isSubprogram()) {
756 processSubprogram(DISubprogram(Scope));
757 return;
758 }
759 if (!addScope(Scope))
760 return;
761 if (Scope.isLexicalBlock()) {
762 DILexicalBlock LB(Scope);
763 processScope(LB.getContext());
764 } else if (Scope.isLexicalBlockFile()) {
765 DILexicalBlockFile LBF = DILexicalBlockFile(Scope);
766 processScope(LBF.getScope());
767 } else if (Scope.isNameSpace()) {
768 DINameSpace NS(Scope);
769 processScope(NS.getContext());
770 }
771}
772
Bill Wendling523bea82013-11-08 08:13:15 +0000773void DebugInfoFinder::processSubprogram(DISubprogram SP) {
774 if (!addSubprogram(SP))
775 return;
776 processScope(SP.getContext().resolve(TypeIdentifierMap));
777 processType(SP.getType());
778 DIArray TParams = SP.getTemplateParams();
779 for (unsigned I = 0, E = TParams.getNumElements(); I != E; ++I) {
780 DIDescriptor Element = TParams.getElement(I);
781 if (Element.isTemplateTypeParameter()) {
782 DITemplateTypeParameter TType(Element);
Bill Wendling523bea82013-11-08 08:13:15 +0000783 processType(TType.getType().resolve(TypeIdentifierMap));
784 } else if (Element.isTemplateValueParameter()) {
785 DITemplateValueParameter TVal(Element);
Bill Wendling523bea82013-11-08 08:13:15 +0000786 processType(TVal.getType().resolve(TypeIdentifierMap));
787 }
788 }
789}
790
Manman Ren2085ccc2013-11-17 18:42:37 +0000791void DebugInfoFinder::processDeclare(const Module &M,
792 const DbgDeclareInst *DDI) {
Bill Wendling523bea82013-11-08 08:13:15 +0000793 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
794 if (!N)
795 return;
Manman Renb46e5502013-11-17 19:35:03 +0000796 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000797
798 DIDescriptor DV(N);
799 if (!DV.isVariable())
800 return;
801
David Blaikie70573dc2014-11-19 07:49:26 +0000802 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000803 return;
804 processScope(DIVariable(N).getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000805 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000806}
807
Manman Ren2085ccc2013-11-17 18:42:37 +0000808void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
Bill Wendling523bea82013-11-08 08:13:15 +0000809 MDNode *N = dyn_cast<MDNode>(DVI->getVariable());
810 if (!N)
811 return;
Manman Renb46e5502013-11-17 19:35:03 +0000812 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000813
814 DIDescriptor DV(N);
815 if (!DV.isVariable())
816 return;
817
David Blaikie70573dc2014-11-19 07:49:26 +0000818 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000819 return;
820 processScope(DIVariable(N).getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000821 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000822}
823
Bill Wendling523bea82013-11-08 08:13:15 +0000824bool DebugInfoFinder::addType(DIType DT) {
825 if (!DT)
826 return false;
827
David Blaikie70573dc2014-11-19 07:49:26 +0000828 if (!NodesSeen.insert(DT).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000829 return false;
830
831 TYs.push_back(DT);
832 return true;
833}
834
Bill Wendling523bea82013-11-08 08:13:15 +0000835bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
836 if (!CU)
837 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000838 if (!NodesSeen.insert(CU).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000839 return false;
840
841 CUs.push_back(CU);
842 return true;
843}
844
Bill Wendling523bea82013-11-08 08:13:15 +0000845bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
846 if (!DIG)
847 return false;
848
David Blaikie70573dc2014-11-19 07:49:26 +0000849 if (!NodesSeen.insert(DIG).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000850 return false;
851
852 GVs.push_back(DIG);
853 return true;
854}
855
Bill Wendling523bea82013-11-08 08:13:15 +0000856bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
857 if (!SP)
858 return false;
859
David Blaikie70573dc2014-11-19 07:49:26 +0000860 if (!NodesSeen.insert(SP).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000861 return false;
862
863 SPs.push_back(SP);
864 return true;
865}
866
867bool DebugInfoFinder::addScope(DIScope Scope) {
868 if (!Scope)
869 return false;
870 // FIXME: Ocaml binding generates a scope with no content, we treat it
871 // as null for now.
872 if (Scope->getNumOperands() == 0)
873 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000874 if (!NodesSeen.insert(Scope).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000875 return false;
876 Scopes.push_back(Scope);
877 return true;
878}
879
880//===----------------------------------------------------------------------===//
881// DIDescriptor: dump routines for all descriptors.
882//===----------------------------------------------------------------------===//
883
Bill Wendling523bea82013-11-08 08:13:15 +0000884void DIDescriptor::dump() const {
885 print(dbgs());
886 dbgs() << '\n';
887}
888
Bill Wendling523bea82013-11-08 08:13:15 +0000889void DIDescriptor::print(raw_ostream &OS) const {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000890 if (!get())
Bill Wendling523bea82013-11-08 08:13:15 +0000891 return;
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000892 get()->print(OS);
Bill Wendling523bea82013-11-08 08:13:15 +0000893}
894
895static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
896 const LLVMContext &Ctx) {
897 if (!DL.isUnknown()) { // Print source line info.
898 DIScope Scope(DL.getScope(Ctx));
899 assert(Scope.isScope() && "Scope of a DebugLoc should be a DIScope.");
900 // Omit the directory, because it's likely to be long and uninteresting.
901 CommentOS << Scope.getFilename();
902 CommentOS << ':' << DL.getLine();
903 if (DL.getCol() != 0)
904 CommentOS << ':' << DL.getCol();
905 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
906 if (!InlinedAtDL.isUnknown()) {
907 CommentOS << " @[ ";
908 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
909 CommentOS << " ]";
910 }
911 }
912}
913
914void DIVariable::printExtendedName(raw_ostream &OS) const {
915 const LLVMContext &Ctx = DbgNode->getContext();
916 StringRef Res = getName();
917 if (!Res.empty())
918 OS << Res << "," << getLineNumber();
919 if (MDNode *InlinedAt = getInlinedAt()) {
920 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
921 if (!InlinedAtDL.isUnknown()) {
922 OS << " @[";
923 printDebugLoc(InlinedAtDL, OS, Ctx);
924 OS << "]";
925 }
926 }
927}
928
Duncan P. N. Exon Smith2a78e9b2015-02-18 19:39:36 +0000929template <> DIRef<DIDescriptor>::DIRef(const Metadata *V) : Val(V) {
930 assert(isDescriptorRef(V) &&
931 "DIDescriptorRef should be a MDString or MDNode");
932}
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000933template <> DIRef<DIScope>::DIRef(const Metadata *V) : Val(V) {
Bill Wendling523bea82013-11-08 08:13:15 +0000934 assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode");
935}
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000936template <> DIRef<DIType>::DIRef(const Metadata *V) : Val(V) {
Bill Wendling523bea82013-11-08 08:13:15 +0000937 assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode");
938}
939
Bill Wendling523bea82013-11-08 08:13:15 +0000940template <>
Duncan P. N. Exon Smith2a78e9b2015-02-18 19:39:36 +0000941DIDescriptorRef DIDescriptor::getFieldAs<DIDescriptorRef>(unsigned Elt) const {
942 return DIDescriptorRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
943}
944template <>
Bill Wendling523bea82013-11-08 08:13:15 +0000945DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000946 return DIScopeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +0000947}
Bill Wendling523bea82013-11-08 08:13:15 +0000948template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000949 return DITypeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +0000950}
Manman Rencb14bbc2013-11-22 22:06:31 +0000951
Manman Rencb14bbc2013-11-22 22:06:31 +0000952bool llvm::StripDebugInfo(Module &M) {
Manman Rencb14bbc2013-11-22 22:06:31 +0000953 bool Changed = false;
954
955 // Remove all of the calls to the debugger intrinsics, and remove them from
956 // the module.
957 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
958 while (!Declare->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000959 CallInst *CI = cast<CallInst>(Declare->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +0000960 CI->eraseFromParent();
961 }
962 Declare->eraseFromParent();
963 Changed = true;
964 }
965
966 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
967 while (!DbgVal->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000968 CallInst *CI = cast<CallInst>(DbgVal->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +0000969 CI->eraseFromParent();
970 }
971 DbgVal->eraseFromParent();
972 Changed = true;
973 }
974
975 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
976 NME = M.named_metadata_end(); NMI != NME;) {
977 NamedMDNode *NMD = NMI;
978 ++NMI;
979 if (NMD->getName().startswith("llvm.dbg.")) {
980 NMD->eraseFromParent();
981 Changed = true;
982 }
983 }
984
985 for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
986 for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE;
987 ++FI)
988 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE;
989 ++BI) {
990 if (!BI->getDebugLoc().isUnknown()) {
991 Changed = true;
992 BI->setDebugLoc(DebugLoc());
993 }
994 }
995
996 return Changed;
997}
Manman Ren8b4306c2013-12-02 21:29:56 +0000998
Manman Renbd4daf82013-12-03 00:12:14 +0000999unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
David Majnemere7a9cdb2015-02-16 06:04:53 +00001000 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +00001001 M.getModuleFlag("Debug Info Version")))
1002 return Val->getZExtValue();
1003 return 0;
Manman Ren8b4306c2013-12-02 21:29:56 +00001004}
David Blaikie6876b3b2014-07-01 20:05:26 +00001005
David Blaikiea8c35092014-07-02 18:30:05 +00001006llvm::DenseMap<const llvm::Function *, llvm::DISubprogram>
1007llvm::makeSubprogramMap(const Module &M) {
1008 DenseMap<const Function *, DISubprogram> R;
David Blaikie6876b3b2014-07-01 20:05:26 +00001009
1010 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
1011 if (!CU_Nodes)
1012 return R;
1013
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001014 for (MDNode *N : CU_Nodes->operands()) {
1015 DICompileUnit CUNode(N);
David Blaikie6876b3b2014-07-01 20:05:26 +00001016 DIArray SPs = CUNode.getSubprograms();
1017 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
1018 DISubprogram SP(SPs.getElement(i));
1019 if (Function *F = SP.getFunction())
1020 R.insert(std::make_pair(F, SP));
1021 }
1022 }
1023 return R;
1024}