blob: ec4f95eb71f1c3d6b2b94ce73a48f5a59e4f095b [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 &&
Peter Collingbourneb7360652015-03-25 17:44:49 +0000286 Tag != dwarf::DW_TAG_inheritance && Tag != dwarf::DW_TAG_friend &&
287 Tag != dwarf::DW_TAG_structure_type && Tag != dwarf::DW_TAG_member &&
288 Tag != dwarf::DW_TAG_typedef)
Duncan P. N. Exon Smithf3740402015-03-16 20:46:27 +0000289 return false;
290 }
291
292 if (isCompositeType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000293 return DICompositeType(DbgNode).Verify();
Duncan P. N. Exon Smithf3740402015-03-16 20:46:27 +0000294 if (isDerivedType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000295 return DIDerivedType(DbgNode).Verify();
Duncan P. N. Exon Smithf3740402015-03-16 20:46:27 +0000296 return false;
Bill Wendling523bea82013-11-08 08:13:15 +0000297}
298
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000299bool DIBasicType::Verify() const {
300 return dyn_cast_or_null<MDBasicType>(DbgNode);
301}
Bill Wendling523bea82013-11-08 08:13:15 +0000302
Bill Wendling523bea82013-11-08 08:13:15 +0000303bool DIDerivedType::Verify() const {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000304 auto *N = dyn_cast_or_null<MDDerivedTypeBase>(DbgNode);
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000305 if (!N)
Bill Wendling523bea82013-11-08 08:13:15 +0000306 return false;
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000307 if (getTag() == dwarf::DW_TAG_ptr_to_member_type) {
308 auto *D = dyn_cast<MDDerivedType>(N);
309 if (!D)
Bill Wendling523bea82013-11-08 08:13:15 +0000310 return false;
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000311 if (!isTypeRef(D->getExtraData()))
312 return false;
313 }
314 return isTypeRef(N->getBaseType());
Bill Wendling523bea82013-11-08 08:13:15 +0000315}
316
Bill Wendling523bea82013-11-08 08:13:15 +0000317bool DICompositeType::Verify() const {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000318 auto *N = dyn_cast_or_null<MDCompositeTypeBase>(DbgNode);
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000319 return N && isTypeRef(N->getBaseType()) && isTypeRef(N->getVTableHolder()) &&
320 !(isLValueReference() && isRValueReference());
Bill Wendling523bea82013-11-08 08:13:15 +0000321}
322
Bill Wendling523bea82013-11-08 08:13:15 +0000323bool DISubprogram::Verify() const {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000324 auto *N = dyn_cast_or_null<MDSubprogram>(DbgNode);
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000325 if (!N)
Bill Wendling523bea82013-11-08 08:13:15 +0000326 return false;
327
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000328 if (!isScopeRef(N->getScope()))
Bill Wendling523bea82013-11-08 08:13:15 +0000329 return false;
Adrian Prantl99c7af22013-12-18 21:48:19 +0000330
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000331 if (auto *Op = N->getType())
332 if (!isa<MDNode>(Op))
333 return false;
334
335 if (!isTypeRef(getContainingType()))
336 return false;
337
Adrian Prantl99c7af22013-12-18 21:48:19 +0000338 if (isLValueReference() && isRValueReference())
339 return false;
340
David Blaikie3dfe4782014-10-14 18:22:52 +0000341 // If a DISubprogram has an llvm::Function*, then scope chains from all
342 // instructions within the function should lead to this DISubprogram.
343 if (auto *F = getFunction()) {
David Blaikie3dfe4782014-10-14 18:22:52 +0000344 for (auto &BB : *F) {
345 for (auto &I : BB) {
Duncan P. N. Exon Smith215e7ed2015-03-30 17:06:38 +0000346 MDLocation *DL =
347 cast_or_null<MDLocation>(I.getDebugLoc().getAsMDNode());
348 if (!DL)
David Blaikie3dfe4782014-10-14 18:22:52 +0000349 continue;
350
David Blaikie3dfe4782014-10-14 18:22:52 +0000351 // walk the inlined-at scopes
Duncan P. N. Exon Smith215e7ed2015-03-30 17:06:38 +0000352 while (MDLocation *IA = DL->getInlinedAt())
353 DL = IA;
354 MDScope *Scope = DL->getScope();
Adrian Prantlde200df2015-01-20 22:37:25 +0000355 if (!Scope)
356 return false;
Duncan P. N. Exon Smith215e7ed2015-03-30 17:06:38 +0000357 while (!isa<MDSubprogram>(Scope)) {
358 Scope = cast<MDLexicalBlockBase>(Scope)->getScope();
Adrian Prantl1292e242015-01-21 18:32:56 +0000359 if (!Scope)
360 return false;
David Blaikie3dfe4782014-10-14 18:22:52 +0000361 }
362 if (!DISubprogram(Scope).describes(F))
363 return false;
364 }
365 }
366 }
Bill Wendling523bea82013-11-08 08:13:15 +0000367
Adrian Prantl9260cca2015-01-22 00:00:52 +0000368 return true;
Bill Wendling523bea82013-11-08 08:13:15 +0000369}
370
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000371bool DIGlobalVariable::Verify() const {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000372 auto *N = dyn_cast_or_null<MDGlobalVariable>(DbgNode);
Bill Wendling523bea82013-11-08 08:13:15 +0000373
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000374 if (!N)
Bill Wendling523bea82013-11-08 08:13:15 +0000375 return false;
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000376
377 if (N->getDisplayName().empty())
378 return false;
379
380 if (auto *Op = N->getScope())
381 if (!isa<MDNode>(Op))
382 return false;
383
384 if (auto *Op = N->getStaticDataMemberDeclaration())
385 if (!isa<MDNode>(Op))
386 return false;
387
388 return isTypeRef(N->getType());
Bill Wendling523bea82013-11-08 08:13:15 +0000389}
390
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000391bool DIVariable::Verify() const {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000392 auto *N = dyn_cast_or_null<MDLocalVariable>(DbgNode);
Bill Wendling523bea82013-11-08 08:13:15 +0000393
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000394 if (!N)
395 return false;
396
397 if (auto *Op = N->getScope())
398 if (!isa<MDNode>(Op))
399 return false;
400
401 return isTypeRef(N->getType());
Bill Wendling523bea82013-11-08 08:13:15 +0000402}
403
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000404bool DILocation::Verify() const {
405 return dyn_cast_or_null<MDLocation>(DbgNode);
406}
407bool DINameSpace::Verify() const {
408 return dyn_cast_or_null<MDNamespace>(DbgNode);
409}
410bool DIFile::Verify() const { return dyn_cast_or_null<MDFile>(DbgNode); }
411bool DIEnumerator::Verify() const {
412 return dyn_cast_or_null<MDEnumerator>(DbgNode);
413}
414bool DISubrange::Verify() const {
415 return dyn_cast_or_null<MDSubrange>(DbgNode);
416}
417bool DILexicalBlock::Verify() const {
418 return dyn_cast_or_null<MDLexicalBlock>(DbgNode);
419}
420bool DILexicalBlockFile::Verify() const {
421 return dyn_cast_or_null<MDLexicalBlockFile>(DbgNode);
422}
423bool DITemplateTypeParameter::Verify() const {
424 return dyn_cast_or_null<MDTemplateTypeParameter>(DbgNode);
425}
426bool DITemplateValueParameter::Verify() const {
427 return dyn_cast_or_null<MDTemplateValueParameter>(DbgNode);
428}
429bool DIImportedEntity::Verify() const {
430 return dyn_cast_or_null<MDImportedEntity>(DbgNode);
431}
Bill Wendling523bea82013-11-08 08:13:15 +0000432
Manman Ren1a125c92014-07-28 19:33:20 +0000433void DICompositeType::setArraysHelper(MDNode *Elements, MDNode *TParams) {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000434 TypedTrackingMDRef<MDCompositeTypeBase> N(get());
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000435 if (Elements)
436 N->replaceElements(cast<MDTuple>(Elements));
Bill Wendling523bea82013-11-08 08:13:15 +0000437 if (TParams)
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000438 N->replaceTemplateParams(cast<MDTuple>(TParams));
Bill Wendling523bea82013-11-08 08:13:15 +0000439 DbgNode = N;
440}
441
Bill Wendling523bea82013-11-08 08:13:15 +0000442DIScopeRef DIScope::getRef() const {
443 if (!isCompositeType())
444 return DIScopeRef(*this);
445 DICompositeType DTy(DbgNode);
446 if (!DTy.getIdentifier())
447 return DIScopeRef(*this);
448 return DIScopeRef(DTy.getIdentifier());
449}
450
Bill Wendling523bea82013-11-08 08:13:15 +0000451void DICompositeType::setContainingType(DICompositeType ContainingType) {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000452 TypedTrackingMDRef<MDCompositeTypeBase> N(get());
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000453 N->replaceVTableHolder(ContainingType.getRef());
Bill Wendling523bea82013-11-08 08:13:15 +0000454 DbgNode = N;
455}
456
Bill Wendling523bea82013-11-08 08:13:15 +0000457bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
458 assert(CurFn && "Invalid function");
459 if (!getContext().isSubprogram())
460 return false;
461 // This variable is not inlined function argument if its scope
462 // does not describe current function.
463 return !DISubprogram(getContext()).describes(CurFn);
464}
465
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000466Function *DISubprogram::getFunction() const {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000467 if (auto *N = get())
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000468 if (auto *C = dyn_cast_or_null<ConstantAsMetadata>(N->getFunction()))
469 return dyn_cast<Function>(C->getValue());
470 return nullptr;
471}
472
Bill Wendling523bea82013-11-08 08:13:15 +0000473bool DISubprogram::describes(const Function *F) {
474 assert(F && "Invalid function");
475 if (F == getFunction())
476 return true;
477 StringRef Name = getLinkageName();
478 if (Name.empty())
479 Name = getName();
480 if (F->getName() == Name)
481 return true;
482 return false;
483}
484
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000485GlobalVariable *DIGlobalVariable::getGlobal() const {
486 return dyn_cast_or_null<GlobalVariable>(getConstant());
Bill Wendling523bea82013-11-08 08:13:15 +0000487}
488
Bill Wendling523bea82013-11-08 08:13:15 +0000489DIScopeRef DIScope::getContext() const {
490
491 if (isType())
492 return DIType(DbgNode).getContext();
493
494 if (isSubprogram())
495 return DIScopeRef(DISubprogram(DbgNode).getContext());
496
497 if (isLexicalBlock())
498 return DIScopeRef(DILexicalBlock(DbgNode).getContext());
499
500 if (isLexicalBlockFile())
501 return DIScopeRef(DILexicalBlockFile(DbgNode).getContext());
502
503 if (isNameSpace())
504 return DIScopeRef(DINameSpace(DbgNode).getContext());
505
506 assert((isFile() || isCompileUnit()) && "Unhandled type of scope.");
Craig Topperc6207612014-04-09 06:08:46 +0000507 return DIScopeRef(nullptr);
Bill Wendling523bea82013-11-08 08:13:15 +0000508}
509
Bill Wendling523bea82013-11-08 08:13:15 +0000510StringRef DIScope::getName() const {
511 if (isType())
512 return DIType(DbgNode).getName();
513 if (isSubprogram())
514 return DISubprogram(DbgNode).getName();
515 if (isNameSpace())
516 return DINameSpace(DbgNode).getName();
517 assert((isLexicalBlock() || isLexicalBlockFile() || isFile() ||
518 isCompileUnit()) &&
519 "Unhandled type of scope.");
520 return StringRef();
521}
522
523StringRef DIScope::getFilename() const {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000524 if (auto *N = get())
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000525 return ::getStringField(dyn_cast_or_null<MDNode>(N->getFile()), 0);
526 return "";
Bill Wendling523bea82013-11-08 08:13:15 +0000527}
528
529StringRef DIScope::getDirectory() const {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000530 if (auto *N = get())
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000531 return ::getStringField(dyn_cast_or_null<MDNode>(N->getFile()), 1);
532 return "";
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000533}
534
535void DICompileUnit::replaceSubprograms(DIArray Subprograms) {
536 assert(Verify() && "Expected compile unit");
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000537 get()->replaceSubprograms(cast_or_null<MDTuple>(Subprograms.get()));
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000538}
539
540void DICompileUnit::replaceGlobalVariables(DIArray GlobalVariables) {
541 assert(Verify() && "Expected compile unit");
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000542 get()->replaceGlobalVariables(cast_or_null<MDTuple>(GlobalVariables.get()));
Bill Wendling523bea82013-11-08 08:13:15 +0000543}
544
Diego Novillof5041ce2014-03-03 20:06:11 +0000545DILocation DILocation::copyWithNewScope(LLVMContext &Ctx,
David Blaikie2f3f76f2014-08-21 22:45:21 +0000546 DILexicalBlockFile NewScope) {
Diego Novillof5041ce2014-03-03 20:06:11 +0000547 assert(Verify());
Duncan P. N. Exon Smith98854692015-01-14 22:27:36 +0000548 assert(NewScope && "Expected valid scope");
549
550 const auto *Old = cast<MDLocation>(DbgNode);
551 return DILocation(MDLocation::get(Ctx, Old->getLine(), Old->getColumn(),
552 NewScope, Old->getInlinedAt()));
Diego Novillof5041ce2014-03-03 20:06:11 +0000553}
554
Diego Novillof5041ce2014-03-03 20:06:11 +0000555unsigned DILocation::computeNewDiscriminator(LLVMContext &Ctx) {
556 std::pair<const char *, unsigned> Key(getFilename().data(), getLineNumber());
557 return ++Ctx.pImpl->DiscriminatorTable[Key];
558}
559
Bill Wendling523bea82013-11-08 08:13:15 +0000560DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
561 LLVMContext &VMContext) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000562 assert(DIVariable(DV).Verify() && "Expected a DIVariable");
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000563 return cast<MDLocalVariable>(DV)
564 ->withInline(cast_or_null<MDLocation>(InlinedScope));
Bill Wendling523bea82013-11-08 08:13:15 +0000565}
566
Bill Wendling523bea82013-11-08 08:13:15 +0000567DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000568 assert(DIVariable(DV).Verify() && "Expected a DIVariable");
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000569 return cast<MDLocalVariable>(DV)->withoutInline();
Bill Wendling523bea82013-11-08 08:13:15 +0000570}
571
Bill Wendling523bea82013-11-08 08:13:15 +0000572DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
573 DIDescriptor D(Scope);
574 if (D.isSubprogram())
575 return DISubprogram(Scope);
576
577 if (D.isLexicalBlockFile())
578 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
579
580 if (D.isLexicalBlock())
581 return getDISubprogram(DILexicalBlock(Scope).getContext());
582
583 return DISubprogram();
584}
585
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000586DISubprogram llvm::getDISubprogram(const Function *F) {
587 // We look for the first instr that has a debug annotation leading back to F.
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000588 for (auto &BB : *F) {
David Majnemerc758df42014-11-01 07:57:14 +0000589 auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) {
590 return !Inst.getDebugLoc().isUnknown();
591 });
592 if (Inst == BB.end())
593 continue;
594 DebugLoc DLoc = Inst->getDebugLoc();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000595 const MDNode *Scope = DLoc.getScopeNode();
David Majnemerc758df42014-11-01 07:57:14 +0000596 DISubprogram Subprogram = getDISubprogram(Scope);
597 return Subprogram.describes(F) ? Subprogram : DISubprogram();
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000598 }
599
600 return DISubprogram();
601}
602
Bill Wendling523bea82013-11-08 08:13:15 +0000603DICompositeType llvm::getDICompositeType(DIType T) {
604 if (T.isCompositeType())
605 return DICompositeType(T);
606
607 if (T.isDerivedType()) {
608 // This function is currently used by dragonegg and dragonegg does
609 // not generate identifier for types, so using an empty map to resolve
610 // DerivedFrom should be fine.
611 DITypeIdentifierMap EmptyMap;
612 return getDICompositeType(
613 DIDerivedType(T).getTypeDerivedFrom().resolve(EmptyMap));
614 }
615
616 return DICompositeType();
617}
618
Bill Wendling523bea82013-11-08 08:13:15 +0000619DITypeIdentifierMap
620llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
621 DITypeIdentifierMap Map;
622 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000623 DICompileUnit CU(CU_Nodes->getOperand(CUi));
Bill Wendling523bea82013-11-08 08:13:15 +0000624 DIArray Retain = CU.getRetainedTypes();
625 for (unsigned Ti = 0, Te = Retain.getNumElements(); Ti != Te; ++Ti) {
626 if (!Retain.getElement(Ti).isCompositeType())
627 continue;
628 DICompositeType Ty(Retain.getElement(Ti));
629 if (MDString *TypeId = Ty.getIdentifier()) {
630 // Definition has priority over declaration.
631 // Try to insert (TypeId, Ty) to Map.
632 std::pair<DITypeIdentifierMap::iterator, bool> P =
633 Map.insert(std::make_pair(TypeId, Ty));
634 // If TypeId already exists in Map and this is a definition, replace
635 // whatever we had (declaration or definition) with the definition.
636 if (!P.second && !Ty.isForwardDecl())
637 P.first->second = Ty;
638 }
639 }
640 }
641 return Map;
642}
643
644//===----------------------------------------------------------------------===//
645// DebugInfoFinder implementations.
646//===----------------------------------------------------------------------===//
647
648void DebugInfoFinder::reset() {
649 CUs.clear();
650 SPs.clear();
651 GVs.clear();
652 TYs.clear();
653 Scopes.clear();
654 NodesSeen.clear();
655 TypeIdentifierMap.clear();
Manman Ren2085ccc2013-11-17 18:42:37 +0000656 TypeMapInitialized = false;
657}
658
Manman Renb46e5502013-11-17 19:35:03 +0000659void DebugInfoFinder::InitializeTypeMap(const Module &M) {
Manman Ren2085ccc2013-11-17 18:42:37 +0000660 if (!TypeMapInitialized)
661 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
662 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
663 TypeMapInitialized = true;
664 }
Bill Wendling523bea82013-11-08 08:13:15 +0000665}
666
Bill Wendling523bea82013-11-08 08:13:15 +0000667void DebugInfoFinder::processModule(const Module &M) {
Manman Renb46e5502013-11-17 19:35:03 +0000668 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000669 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
Bill Wendling523bea82013-11-08 08:13:15 +0000670 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000671 DICompileUnit CU(CU_Nodes->getOperand(i));
Bill Wendling523bea82013-11-08 08:13:15 +0000672 addCompileUnit(CU);
673 DIArray GVs = CU.getGlobalVariables();
674 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
675 DIGlobalVariable DIG(GVs.getElement(i));
676 if (addGlobalVariable(DIG)) {
Manman Renf0a582b2014-11-21 19:55:23 +0000677 processScope(DIG.getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000678 processType(DIG.getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000679 }
680 }
681 DIArray SPs = CU.getSubprograms();
682 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
683 processSubprogram(DISubprogram(SPs.getElement(i)));
684 DIArray EnumTypes = CU.getEnumTypes();
685 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
686 processType(DIType(EnumTypes.getElement(i)));
687 DIArray RetainedTypes = CU.getRetainedTypes();
688 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
689 processType(DIType(RetainedTypes.getElement(i)));
690 DIArray Imports = CU.getImportedEntities();
691 for (unsigned i = 0, e = Imports.getNumElements(); i != e; ++i) {
692 DIImportedEntity Import = DIImportedEntity(Imports.getElement(i));
Duncan P. N. Exon Smithd4e07c92015-03-20 19:13:53 +0000693 if (!Import)
694 continue;
Adrian Prantld09ba232014-04-01 03:41:04 +0000695 DIDescriptor Entity = Import.getEntity().resolve(TypeIdentifierMap);
Bill Wendling523bea82013-11-08 08:13:15 +0000696 if (Entity.isType())
697 processType(DIType(Entity));
698 else if (Entity.isSubprogram())
699 processSubprogram(DISubprogram(Entity));
700 else if (Entity.isNameSpace())
701 processScope(DINameSpace(Entity).getContext());
702 }
703 }
704 }
705}
706
Manman Ren2085ccc2013-11-17 18:42:37 +0000707void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) {
Bill Wendling523bea82013-11-08 08:13:15 +0000708 if (!Loc)
709 return;
Manman Renb46e5502013-11-17 19:35:03 +0000710 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000711 processScope(Loc.getScope());
Manman Ren2085ccc2013-11-17 18:42:37 +0000712 processLocation(M, Loc.getOrigLocation());
Bill Wendling523bea82013-11-08 08:13:15 +0000713}
714
Bill Wendling523bea82013-11-08 08:13:15 +0000715void DebugInfoFinder::processType(DIType DT) {
716 if (!addType(DT))
717 return;
718 processScope(DT.getContext().resolve(TypeIdentifierMap));
719 if (DT.isCompositeType()) {
720 DICompositeType DCT(DT);
721 processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
Manman Renf8a19672014-07-28 22:24:06 +0000722 if (DT.isSubroutineType()) {
723 DITypeArray DTA = DISubroutineType(DT).getTypeArray();
724 for (unsigned i = 0, e = DTA.getNumElements(); i != e; ++i)
725 processType(DTA.getElement(i).resolve(TypeIdentifierMap));
726 return;
727 }
Manman Renab8ffba2014-07-28 19:14:13 +0000728 DIArray DA = DCT.getElements();
Bill Wendling523bea82013-11-08 08:13:15 +0000729 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
730 DIDescriptor D = DA.getElement(i);
731 if (D.isType())
732 processType(DIType(D));
733 else if (D.isSubprogram())
734 processSubprogram(DISubprogram(D));
735 }
736 } else if (DT.isDerivedType()) {
737 DIDerivedType DDT(DT);
738 processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
739 }
740}
741
742void DebugInfoFinder::processScope(DIScope Scope) {
743 if (Scope.isType()) {
744 DIType Ty(Scope);
745 processType(Ty);
746 return;
747 }
748 if (Scope.isCompileUnit()) {
749 addCompileUnit(DICompileUnit(Scope));
750 return;
751 }
752 if (Scope.isSubprogram()) {
753 processSubprogram(DISubprogram(Scope));
754 return;
755 }
756 if (!addScope(Scope))
757 return;
758 if (Scope.isLexicalBlock()) {
759 DILexicalBlock LB(Scope);
760 processScope(LB.getContext());
761 } else if (Scope.isLexicalBlockFile()) {
762 DILexicalBlockFile LBF = DILexicalBlockFile(Scope);
763 processScope(LBF.getScope());
764 } else if (Scope.isNameSpace()) {
765 DINameSpace NS(Scope);
766 processScope(NS.getContext());
767 }
768}
769
Bill Wendling523bea82013-11-08 08:13:15 +0000770void DebugInfoFinder::processSubprogram(DISubprogram SP) {
771 if (!addSubprogram(SP))
772 return;
773 processScope(SP.getContext().resolve(TypeIdentifierMap));
774 processType(SP.getType());
775 DIArray TParams = SP.getTemplateParams();
776 for (unsigned I = 0, E = TParams.getNumElements(); I != E; ++I) {
777 DIDescriptor Element = TParams.getElement(I);
778 if (Element.isTemplateTypeParameter()) {
779 DITemplateTypeParameter TType(Element);
Bill Wendling523bea82013-11-08 08:13:15 +0000780 processType(TType.getType().resolve(TypeIdentifierMap));
781 } else if (Element.isTemplateValueParameter()) {
782 DITemplateValueParameter TVal(Element);
Bill Wendling523bea82013-11-08 08:13:15 +0000783 processType(TVal.getType().resolve(TypeIdentifierMap));
784 }
785 }
786}
787
Manman Ren2085ccc2013-11-17 18:42:37 +0000788void DebugInfoFinder::processDeclare(const Module &M,
789 const DbgDeclareInst *DDI) {
Bill Wendling523bea82013-11-08 08:13:15 +0000790 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
791 if (!N)
792 return;
Manman Renb46e5502013-11-17 19:35:03 +0000793 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000794
795 DIDescriptor DV(N);
796 if (!DV.isVariable())
797 return;
798
David Blaikie70573dc2014-11-19 07:49:26 +0000799 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000800 return;
801 processScope(DIVariable(N).getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000802 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000803}
804
Manman Ren2085ccc2013-11-17 18:42:37 +0000805void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
Bill Wendling523bea82013-11-08 08:13:15 +0000806 MDNode *N = dyn_cast<MDNode>(DVI->getVariable());
807 if (!N)
808 return;
Manman Renb46e5502013-11-17 19:35:03 +0000809 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000810
811 DIDescriptor DV(N);
812 if (!DV.isVariable())
813 return;
814
David Blaikie70573dc2014-11-19 07:49:26 +0000815 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000816 return;
817 processScope(DIVariable(N).getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000818 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000819}
820
Bill Wendling523bea82013-11-08 08:13:15 +0000821bool DebugInfoFinder::addType(DIType DT) {
822 if (!DT)
823 return false;
824
David Blaikie70573dc2014-11-19 07:49:26 +0000825 if (!NodesSeen.insert(DT).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000826 return false;
827
828 TYs.push_back(DT);
829 return true;
830}
831
Bill Wendling523bea82013-11-08 08:13:15 +0000832bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
833 if (!CU)
834 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000835 if (!NodesSeen.insert(CU).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000836 return false;
837
838 CUs.push_back(CU);
839 return true;
840}
841
Bill Wendling523bea82013-11-08 08:13:15 +0000842bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
843 if (!DIG)
844 return false;
845
David Blaikie70573dc2014-11-19 07:49:26 +0000846 if (!NodesSeen.insert(DIG).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000847 return false;
848
849 GVs.push_back(DIG);
850 return true;
851}
852
Bill Wendling523bea82013-11-08 08:13:15 +0000853bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
854 if (!SP)
855 return false;
856
David Blaikie70573dc2014-11-19 07:49:26 +0000857 if (!NodesSeen.insert(SP).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000858 return false;
859
860 SPs.push_back(SP);
861 return true;
862}
863
864bool DebugInfoFinder::addScope(DIScope Scope) {
865 if (!Scope)
866 return false;
867 // FIXME: Ocaml binding generates a scope with no content, we treat it
868 // as null for now.
869 if (Scope->getNumOperands() == 0)
870 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000871 if (!NodesSeen.insert(Scope).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000872 return false;
873 Scopes.push_back(Scope);
874 return true;
875}
876
877//===----------------------------------------------------------------------===//
878// DIDescriptor: dump routines for all descriptors.
879//===----------------------------------------------------------------------===//
880
Bill Wendling523bea82013-11-08 08:13:15 +0000881void DIDescriptor::dump() const {
882 print(dbgs());
883 dbgs() << '\n';
884}
885
Bill Wendling523bea82013-11-08 08:13:15 +0000886void DIDescriptor::print(raw_ostream &OS) const {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000887 if (!get())
Bill Wendling523bea82013-11-08 08:13:15 +0000888 return;
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000889 get()->print(OS);
Bill Wendling523bea82013-11-08 08:13:15 +0000890}
891
892static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
893 const LLVMContext &Ctx) {
894 if (!DL.isUnknown()) { // Print source line info.
895 DIScope Scope(DL.getScope(Ctx));
896 assert(Scope.isScope() && "Scope of a DebugLoc should be a DIScope.");
897 // Omit the directory, because it's likely to be long and uninteresting.
898 CommentOS << Scope.getFilename();
899 CommentOS << ':' << DL.getLine();
900 if (DL.getCol() != 0)
901 CommentOS << ':' << DL.getCol();
902 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(DL.getInlinedAt(Ctx));
903 if (!InlinedAtDL.isUnknown()) {
904 CommentOS << " @[ ";
905 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
906 CommentOS << " ]";
907 }
908 }
909}
910
911void DIVariable::printExtendedName(raw_ostream &OS) const {
912 const LLVMContext &Ctx = DbgNode->getContext();
913 StringRef Res = getName();
914 if (!Res.empty())
915 OS << Res << "," << getLineNumber();
916 if (MDNode *InlinedAt = getInlinedAt()) {
917 DebugLoc InlinedAtDL = DebugLoc::getFromDILocation(InlinedAt);
918 if (!InlinedAtDL.isUnknown()) {
919 OS << " @[";
920 printDebugLoc(InlinedAtDL, OS, Ctx);
921 OS << "]";
922 }
923 }
924}
925
Duncan P. N. Exon Smith2a78e9b2015-02-18 19:39:36 +0000926template <> DIRef<DIDescriptor>::DIRef(const Metadata *V) : Val(V) {
927 assert(isDescriptorRef(V) &&
928 "DIDescriptorRef should be a MDString or MDNode");
929}
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000930template <> DIRef<DIScope>::DIRef(const Metadata *V) : Val(V) {
Bill Wendling523bea82013-11-08 08:13:15 +0000931 assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode");
932}
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000933template <> DIRef<DIType>::DIRef(const Metadata *V) : Val(V) {
Bill Wendling523bea82013-11-08 08:13:15 +0000934 assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode");
935}
936
Bill Wendling523bea82013-11-08 08:13:15 +0000937template <>
Duncan P. N. Exon Smith2a78e9b2015-02-18 19:39:36 +0000938DIDescriptorRef DIDescriptor::getFieldAs<DIDescriptorRef>(unsigned Elt) const {
939 return DIDescriptorRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
940}
941template <>
Bill Wendling523bea82013-11-08 08:13:15 +0000942DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000943 return DIScopeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +0000944}
Bill Wendling523bea82013-11-08 08:13:15 +0000945template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000946 return DITypeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +0000947}
Manman Rencb14bbc2013-11-22 22:06:31 +0000948
Manman Rencb14bbc2013-11-22 22:06:31 +0000949bool llvm::StripDebugInfo(Module &M) {
Manman Rencb14bbc2013-11-22 22:06:31 +0000950 bool Changed = false;
951
952 // Remove all of the calls to the debugger intrinsics, and remove them from
953 // the module.
954 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
955 while (!Declare->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000956 CallInst *CI = cast<CallInst>(Declare->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +0000957 CI->eraseFromParent();
958 }
959 Declare->eraseFromParent();
960 Changed = true;
961 }
962
963 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
964 while (!DbgVal->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000965 CallInst *CI = cast<CallInst>(DbgVal->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +0000966 CI->eraseFromParent();
967 }
968 DbgVal->eraseFromParent();
969 Changed = true;
970 }
971
972 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
973 NME = M.named_metadata_end(); NMI != NME;) {
974 NamedMDNode *NMD = NMI;
975 ++NMI;
976 if (NMD->getName().startswith("llvm.dbg.")) {
977 NMD->eraseFromParent();
978 Changed = true;
979 }
980 }
981
982 for (Module::iterator MI = M.begin(), ME = M.end(); MI != ME; ++MI)
983 for (Function::iterator FI = MI->begin(), FE = MI->end(); FI != FE;
984 ++FI)
985 for (BasicBlock::iterator BI = FI->begin(), BE = FI->end(); BI != BE;
986 ++BI) {
987 if (!BI->getDebugLoc().isUnknown()) {
988 Changed = true;
989 BI->setDebugLoc(DebugLoc());
990 }
991 }
992
993 return Changed;
994}
Manman Ren8b4306c2013-12-02 21:29:56 +0000995
Manman Renbd4daf82013-12-03 00:12:14 +0000996unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
David Majnemere7a9cdb2015-02-16 06:04:53 +0000997 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000998 M.getModuleFlag("Debug Info Version")))
999 return Val->getZExtValue();
1000 return 0;
Manman Ren8b4306c2013-12-02 21:29:56 +00001001}
David Blaikie6876b3b2014-07-01 20:05:26 +00001002
David Blaikiea8c35092014-07-02 18:30:05 +00001003llvm::DenseMap<const llvm::Function *, llvm::DISubprogram>
1004llvm::makeSubprogramMap(const Module &M) {
1005 DenseMap<const Function *, DISubprogram> R;
David Blaikie6876b3b2014-07-01 20:05:26 +00001006
1007 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
1008 if (!CU_Nodes)
1009 return R;
1010
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001011 for (MDNode *N : CU_Nodes->operands()) {
1012 DICompileUnit CUNode(N);
David Blaikie6876b3b2014-07-01 20:05:26 +00001013 DIArray SPs = CUNode.getSubprograms();
1014 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
1015 DISubprogram SP(SPs.getElement(i));
1016 if (Function *F = SP.getFunction())
1017 R.insert(std::make_pair(F, SP));
1018 }
1019 }
1020 return R;
1021}