blob: 4e5b5f1f97561b0f453a57938e68cff329bfe2a8 [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"
Rafael Espindola0d68b4c2015-03-30 21:36:43 +000028#include "llvm/IR/GVMaterializer.h"
Bill Wendling523bea82013-11-08 08:13:15 +000029#include "llvm/IR/Module.h"
Chandler Carruth4220e9c2014-03-04 11:17:44 +000030#include "llvm/IR/ValueHandle.h"
Bill Wendling523bea82013-11-08 08:13:15 +000031#include "llvm/Support/Debug.h"
32#include "llvm/Support/Dwarf.h"
Bill Wendling523bea82013-11-08 08:13:15 +000033#include "llvm/Support/raw_ostream.h"
34using namespace llvm;
35using namespace llvm::dwarf;
36
37//===----------------------------------------------------------------------===//
38// DIDescriptor
39//===----------------------------------------------------------------------===//
40
Duncan P. N. Exon Smithc22a5c22015-02-21 00:43:09 +000041unsigned DIDescriptor::getFlag(StringRef Flag) {
42 return StringSwitch<unsigned>(Flag)
43#define HANDLE_DI_FLAG(ID, NAME) .Case("DIFlag" #NAME, Flag##NAME)
44#include "llvm/IR/DebugInfoFlags.def"
45 .Default(0);
46}
47
48const char *DIDescriptor::getFlagString(unsigned Flag) {
49 switch (Flag) {
50 default:
51 return "";
52#define HANDLE_DI_FLAG(ID, NAME) \
53 case Flag##NAME: \
54 return "DIFlag" #NAME;
55#include "llvm/IR/DebugInfoFlags.def"
56 }
57}
58
Duncan P. N. Exon Smith269e38d2015-02-21 00:45:26 +000059unsigned DIDescriptor::splitFlags(unsigned Flags,
60 SmallVectorImpl<unsigned> &SplitFlags) {
61 // Accessibility flags need to be specially handled, since they're packed
62 // together.
63 if (unsigned A = Flags & FlagAccessibility) {
64 if (A == FlagPrivate)
65 SplitFlags.push_back(FlagPrivate);
66 else if (A == FlagProtected)
67 SplitFlags.push_back(FlagProtected);
68 else
69 SplitFlags.push_back(FlagPublic);
70 Flags &= ~A;
71 }
72
73#define HANDLE_DI_FLAG(ID, NAME) \
74 if (unsigned Bit = Flags & ID) { \
75 SplitFlags.push_back(Bit); \
76 Flags &= ~Bit; \
77 }
78#include "llvm/IR/DebugInfoFlags.def"
79
80 return Flags;
81}
82
Bill Wendling523bea82013-11-08 08:13:15 +000083bool DIDescriptor::Verify() const {
84 return DbgNode &&
85 (DIDerivedType(DbgNode).Verify() ||
86 DICompositeType(DbgNode).Verify() || DIBasicType(DbgNode).Verify() ||
87 DIVariable(DbgNode).Verify() || DISubprogram(DbgNode).Verify() ||
88 DIGlobalVariable(DbgNode).Verify() || DIFile(DbgNode).Verify() ||
89 DICompileUnit(DbgNode).Verify() || DINameSpace(DbgNode).Verify() ||
90 DILexicalBlock(DbgNode).Verify() ||
91 DILexicalBlockFile(DbgNode).Verify() ||
92 DISubrange(DbgNode).Verify() || DIEnumerator(DbgNode).Verify() ||
93 DIObjCProperty(DbgNode).Verify() ||
94 DITemplateTypeParameter(DbgNode).Verify() ||
95 DITemplateValueParameter(DbgNode).Verify() ||
Duncan P. N. Exon Smithe9d379c2015-03-16 21:03:55 +000096 DIImportedEntity(DbgNode).Verify());
Bill Wendling523bea82013-11-08 08:13:15 +000097}
98
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000099static Metadata *getField(const MDNode *DbgNode, unsigned Elt) {
Craig Topperc6207612014-04-09 06:08:46 +0000100 if (!DbgNode || Elt >= DbgNode->getNumOperands())
101 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +0000102 return DbgNode->getOperand(Elt);
103}
104
105static MDNode *getNodeField(const MDNode *DbgNode, unsigned Elt) {
106 return dyn_cast_or_null<MDNode>(getField(DbgNode, Elt));
107}
108
109static StringRef getStringField(const MDNode *DbgNode, unsigned Elt) {
110 if (MDString *MDS = dyn_cast_or_null<MDString>(getField(DbgNode, Elt)))
111 return MDS->getString();
112 return StringRef();
113}
114
115StringRef DIDescriptor::getStringField(unsigned Elt) const {
116 return ::getStringField(DbgNode, Elt);
117}
118
119uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000120 if (auto *C = getConstantField(Elt))
121 if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
Bill Wendling523bea82013-11-08 08:13:15 +0000122 return CI->getZExtValue();
123
124 return 0;
125}
126
127int64_t DIDescriptor::getInt64Field(unsigned Elt) const {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000128 if (auto *C = getConstantField(Elt))
129 if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
130 return CI->getZExtValue();
Bill Wendling523bea82013-11-08 08:13:15 +0000131
132 return 0;
133}
134
135DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
136 MDNode *Field = getNodeField(DbgNode, Elt);
137 return DIDescriptor(Field);
138}
139
140GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000141 return dyn_cast_or_null<GlobalVariable>(getConstantField(Elt));
Bill Wendling523bea82013-11-08 08:13:15 +0000142}
143
144Constant *DIDescriptor::getConstantField(unsigned Elt) const {
Craig Topperc6207612014-04-09 06:08:46 +0000145 if (!DbgNode)
146 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +0000147
148 if (Elt < DbgNode->getNumOperands())
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000149 if (auto *C =
150 dyn_cast_or_null<ConstantAsMetadata>(DbgNode->getOperand(Elt)))
151 return C->getValue();
Craig Topperc6207612014-04-09 06:08:46 +0000152 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +0000153}
154
155Function *DIDescriptor::getFunctionField(unsigned Elt) const {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000156 return dyn_cast_or_null<Function>(getConstantField(Elt));
Bill Wendling523bea82013-11-08 08:13:15 +0000157}
158
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000159/// \brief Return the size reported by the variable's type.
Adrian Prantlb1416832014-08-01 22:11:58 +0000160unsigned DIVariable::getSizeInBits(const DITypeIdentifierMap &Map) {
161 DIType Ty = getType().resolve(Map);
162 // Follow derived types until we reach a type that
163 // reports back a size.
164 while (Ty.isDerivedType() && !Ty.getSizeInBits()) {
165 DIDerivedType DT(&*Ty);
166 Ty = DT.getTypeDerivedFrom().resolve(Map);
167 }
168 assert(Ty.getSizeInBits() && "type with size 0");
169 return Ty.getSizeInBits();
170}
171
Adrian Prantl27bd01f2015-02-09 23:57:15 +0000172bool DIExpression::isBitPiece() const {
Adrian Prantl34bcbee2015-01-21 00:59:20 +0000173 unsigned N = getNumElements();
Adrian Prantl27bd01f2015-02-09 23:57:15 +0000174 return N >=3 && getElement(N-3) == dwarf::DW_OP_bit_piece;
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000175}
176
Adrian Prantl27bd01f2015-02-09 23:57:15 +0000177uint64_t DIExpression::getBitPieceOffset() const {
178 assert(isBitPiece() && "not a piece");
Adrian Prantl34bcbee2015-01-21 00:59:20 +0000179 return getElement(getNumElements()-2);
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000180}
181
Adrian Prantl27bd01f2015-02-09 23:57:15 +0000182uint64_t DIExpression::getBitPieceSize() const {
183 assert(isBitPiece() && "not a piece");
Adrian Prantl34bcbee2015-01-21 00:59:20 +0000184 return getElement(getNumElements()-1);
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000185}
Adrian Prantlb1416832014-08-01 22:11:58 +0000186
Adrian Prantl0f615792015-03-04 17:39:33 +0000187DIExpression::iterator DIExpression::Operand::getNext() const {
Adrian Prantl70f2a732015-01-23 23:40:47 +0000188 iterator it(I);
Adrian Prantl0f615792015-03-04 17:39:33 +0000189 return ++it;
Adrian Prantl70f2a732015-01-23 23:40:47 +0000190}
191
Bill Wendling523bea82013-11-08 08:13:15 +0000192//===----------------------------------------------------------------------===//
Bill Wendling523bea82013-11-08 08:13:15 +0000193// Simple Descriptor Constructors and other Methods
194//===----------------------------------------------------------------------===//
195
Duncan P. N. Exon Smith9c3b8942015-02-28 23:48:02 +0000196void DIDescriptor::replaceAllUsesWith(LLVMContext &, DIDescriptor D) {
Bill Wendling523bea82013-11-08 08:13:15 +0000197 assert(DbgNode && "Trying to replace an unverified type!");
Duncan P. N. Exon Smith9c3b8942015-02-28 23:48:02 +0000198 assert(DbgNode->isTemporary() && "Expected temporary node");
199 TempMDNode Temp(get());
Bill Wendling523bea82013-11-08 08:13:15 +0000200
201 // Since we use a TrackingVH for the node, its easy for clients to manufacture
202 // legitimate situations where they want to replaceAllUsesWith() on something
203 // which, due to uniquing, has merged with the source. We shield clients from
204 // this detail by allowing a value to be replaced with replaceAllUsesWith()
205 // itself.
Duncan P. N. Exon Smith9c3b8942015-02-28 23:48:02 +0000206 if (Temp.get() == D.get()) {
207 DbgNode = MDNode::replaceWithUniqued(std::move(Temp));
208 return;
Bill Wendling523bea82013-11-08 08:13:15 +0000209 }
David Blaikied3f094a2014-05-06 03:41:57 +0000210
Duncan P. N. Exon Smith9c3b8942015-02-28 23:48:02 +0000211 Temp->replaceAllUsesWith(D.get());
212 DbgNode = D.get();
Bill Wendling523bea82013-11-08 08:13:15 +0000213}
214
Frederic Riss36acf0f2014-09-15 07:50:36 +0000215void DIDescriptor::replaceAllUsesWith(MDNode *D) {
Bill Wendling523bea82013-11-08 08:13:15 +0000216 assert(DbgNode && "Trying to replace an unverified type!");
David Blaikied3f094a2014-05-06 03:41:57 +0000217 assert(DbgNode != D && "This replacement should always happen");
Duncan P. N. Exon Smith946fdcc2015-01-19 20:36:39 +0000218 assert(DbgNode->isTemporary() && "Expected temporary node");
Duncan P. N. Exon Smith9c3b8942015-02-28 23:48:02 +0000219 TempMDNode Node(get());
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000220 Node->replaceAllUsesWith(D);
Bill Wendling523bea82013-11-08 08:13:15 +0000221}
222
Duncan P. N. Exon Smithcd07efa12015-03-31 00:47:15 +0000223bool DICompileUnit::Verify() const { return isCompileUnit(); }
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000224bool DIObjCProperty::Verify() const { return isObjCProperty(); }
Bill Wendling523bea82013-11-08 08:13:15 +0000225
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000226/// \brief Check if a value can be a reference to a type.
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000227static bool isTypeRef(const Metadata *MD) {
228 if (!MD)
229 return true;
230 if (auto *S = dyn_cast<MDString>(MD))
231 return !S->getString().empty();
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000232 return isa<MDType>(MD);
Bill Wendling523bea82013-11-08 08:13:15 +0000233}
234
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000235/// \brief Check if a value can be a ScopeRef.
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000236static bool isScopeRef(const Metadata *MD) {
237 if (!MD)
238 return true;
239 if (auto *S = dyn_cast<MDString>(MD))
240 return !S->getString().empty();
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000241 return isa<MDScope>(MD);
Bill Wendling523bea82013-11-08 08:13:15 +0000242}
243
Duncan P. N. Exon Smithe4450142015-02-18 19:56:50 +0000244#ifndef NDEBUG
Duncan P. N. Exon Smith2a78e9b2015-02-18 19:39:36 +0000245/// \brief Check if a value can be a DescriptorRef.
246static bool isDescriptorRef(const Metadata *MD) {
247 if (!MD)
248 return true;
249 if (auto *S = dyn_cast<MDString>(MD))
250 return !S->getString().empty();
251 return isa<MDNode>(MD);
252}
Duncan P. N. Exon Smithe4450142015-02-18 19:56:50 +0000253#endif
Duncan P. N. Exon Smith2a78e9b2015-02-18 19:39:36 +0000254
Bill Wendling523bea82013-11-08 08:13:15 +0000255bool DIType::Verify() const {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000256 auto *N = dyn_cast_or_null<MDType>(DbgNode);
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000257 if (!N)
Bill Wendling523bea82013-11-08 08:13:15 +0000258 return false;
Duncan P. N. Exon Smithf3740402015-03-16 20:46:27 +0000259
260 if (isCompositeType())
Renato Golin47f46fd2013-11-26 16:47:00 +0000261 return DICompositeType(DbgNode).Verify();
Duncan P. N. Exon Smithcd07efa12015-03-31 00:47:15 +0000262 return true;
Bill Wendling523bea82013-11-08 08:13:15 +0000263}
264
Duncan P. N. Exon Smithcd07efa12015-03-31 00:47:15 +0000265bool DIBasicType::Verify() const { return isBasicType(); }
266bool DIDerivedType::Verify() const { return isDerivedType(); }
Bill Wendling523bea82013-11-08 08:13:15 +0000267
Bill Wendling523bea82013-11-08 08:13:15 +0000268bool DICompositeType::Verify() const {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000269 auto *N = dyn_cast_or_null<MDCompositeTypeBase>(DbgNode);
Duncan P. N. Exon Smithcd07efa12015-03-31 00:47:15 +0000270 return N && !(isLValueReference() && isRValueReference());
Bill Wendling523bea82013-11-08 08:13:15 +0000271}
272
Bill Wendling523bea82013-11-08 08:13:15 +0000273bool DISubprogram::Verify() const {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000274 auto *N = dyn_cast_or_null<MDSubprogram>(DbgNode);
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000275 if (!N)
Bill Wendling523bea82013-11-08 08:13:15 +0000276 return false;
277
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000278 if (!isScopeRef(N->getScope()))
Bill Wendling523bea82013-11-08 08:13:15 +0000279 return false;
Adrian Prantl99c7af22013-12-18 21:48:19 +0000280
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000281 if (auto *Op = N->getType())
282 if (!isa<MDNode>(Op))
283 return false;
284
285 if (!isTypeRef(getContainingType()))
286 return false;
287
Adrian Prantl99c7af22013-12-18 21:48:19 +0000288 if (isLValueReference() && isRValueReference())
289 return false;
290
David Blaikie3dfe4782014-10-14 18:22:52 +0000291 // If a DISubprogram has an llvm::Function*, then scope chains from all
292 // instructions within the function should lead to this DISubprogram.
293 if (auto *F = getFunction()) {
David Blaikie3dfe4782014-10-14 18:22:52 +0000294 for (auto &BB : *F) {
295 for (auto &I : BB) {
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +0000296 MDLocation *DL = I.getDebugLoc();
Duncan P. N. Exon Smith215e7ed2015-03-30 17:06:38 +0000297 if (!DL)
David Blaikie3dfe4782014-10-14 18:22:52 +0000298 continue;
299
David Blaikie3dfe4782014-10-14 18:22:52 +0000300 // walk the inlined-at scopes
Duncan P. N. Exon Smith8f7bc792015-03-30 17:41:24 +0000301 MDScope *Scope = DL->getInlinedAtScope();
Adrian Prantlde200df2015-01-20 22:37:25 +0000302 if (!Scope)
303 return false;
Duncan P. N. Exon Smith215e7ed2015-03-30 17:06:38 +0000304 while (!isa<MDSubprogram>(Scope)) {
305 Scope = cast<MDLexicalBlockBase>(Scope)->getScope();
Adrian Prantl1292e242015-01-21 18:32:56 +0000306 if (!Scope)
307 return false;
David Blaikie3dfe4782014-10-14 18:22:52 +0000308 }
309 if (!DISubprogram(Scope).describes(F))
310 return false;
311 }
312 }
313 }
Bill Wendling523bea82013-11-08 08:13:15 +0000314
Adrian Prantl9260cca2015-01-22 00:00:52 +0000315 return true;
Bill Wendling523bea82013-11-08 08:13:15 +0000316}
317
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000318bool DIGlobalVariable::Verify() const {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000319 auto *N = dyn_cast_or_null<MDGlobalVariable>(DbgNode);
Bill Wendling523bea82013-11-08 08:13:15 +0000320
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000321 if (!N)
Bill Wendling523bea82013-11-08 08:13:15 +0000322 return false;
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000323
324 if (N->getDisplayName().empty())
325 return false;
326
327 if (auto *Op = N->getScope())
328 if (!isa<MDNode>(Op))
329 return false;
330
331 if (auto *Op = N->getStaticDataMemberDeclaration())
332 if (!isa<MDNode>(Op))
333 return false;
334
335 return isTypeRef(N->getType());
Bill Wendling523bea82013-11-08 08:13:15 +0000336}
337
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000338bool DIVariable::Verify() const {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000339 auto *N = dyn_cast_or_null<MDLocalVariable>(DbgNode);
Bill Wendling523bea82013-11-08 08:13:15 +0000340
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000341 if (!N)
342 return false;
343
344 if (auto *Op = N->getScope())
345 if (!isa<MDNode>(Op))
346 return false;
347
348 return isTypeRef(N->getType());
Bill Wendling523bea82013-11-08 08:13:15 +0000349}
350
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000351bool DILocation::Verify() const {
352 return dyn_cast_or_null<MDLocation>(DbgNode);
353}
354bool DINameSpace::Verify() const {
355 return dyn_cast_or_null<MDNamespace>(DbgNode);
356}
357bool DIFile::Verify() const { return dyn_cast_or_null<MDFile>(DbgNode); }
358bool DIEnumerator::Verify() const {
359 return dyn_cast_or_null<MDEnumerator>(DbgNode);
360}
361bool DISubrange::Verify() const {
362 return dyn_cast_or_null<MDSubrange>(DbgNode);
363}
364bool DILexicalBlock::Verify() const {
365 return dyn_cast_or_null<MDLexicalBlock>(DbgNode);
366}
367bool DILexicalBlockFile::Verify() const {
368 return dyn_cast_or_null<MDLexicalBlockFile>(DbgNode);
369}
370bool DITemplateTypeParameter::Verify() const {
371 return dyn_cast_or_null<MDTemplateTypeParameter>(DbgNode);
372}
373bool DITemplateValueParameter::Verify() const {
374 return dyn_cast_or_null<MDTemplateValueParameter>(DbgNode);
375}
376bool DIImportedEntity::Verify() const {
377 return dyn_cast_or_null<MDImportedEntity>(DbgNode);
378}
Bill Wendling523bea82013-11-08 08:13:15 +0000379
Manman Ren1a125c92014-07-28 19:33:20 +0000380void DICompositeType::setArraysHelper(MDNode *Elements, MDNode *TParams) {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000381 TypedTrackingMDRef<MDCompositeTypeBase> N(get());
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000382 if (Elements)
383 N->replaceElements(cast<MDTuple>(Elements));
Bill Wendling523bea82013-11-08 08:13:15 +0000384 if (TParams)
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000385 N->replaceTemplateParams(cast<MDTuple>(TParams));
Bill Wendling523bea82013-11-08 08:13:15 +0000386 DbgNode = N;
387}
388
Bill Wendling523bea82013-11-08 08:13:15 +0000389DIScopeRef DIScope::getRef() const {
390 if (!isCompositeType())
391 return DIScopeRef(*this);
392 DICompositeType DTy(DbgNode);
393 if (!DTy.getIdentifier())
394 return DIScopeRef(*this);
395 return DIScopeRef(DTy.getIdentifier());
396}
397
Bill Wendling523bea82013-11-08 08:13:15 +0000398void DICompositeType::setContainingType(DICompositeType ContainingType) {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000399 TypedTrackingMDRef<MDCompositeTypeBase> N(get());
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000400 N->replaceVTableHolder(ContainingType.getRef());
Bill Wendling523bea82013-11-08 08:13:15 +0000401 DbgNode = N;
402}
403
Bill Wendling523bea82013-11-08 08:13:15 +0000404bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
405 assert(CurFn && "Invalid function");
406 if (!getContext().isSubprogram())
407 return false;
408 // This variable is not inlined function argument if its scope
409 // does not describe current function.
410 return !DISubprogram(getContext()).describes(CurFn);
411}
412
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000413Function *DISubprogram::getFunction() const {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000414 if (auto *N = get())
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000415 if (auto *C = dyn_cast_or_null<ConstantAsMetadata>(N->getFunction()))
416 return dyn_cast<Function>(C->getValue());
417 return nullptr;
418}
419
Bill Wendling523bea82013-11-08 08:13:15 +0000420bool DISubprogram::describes(const Function *F) {
421 assert(F && "Invalid function");
422 if (F == getFunction())
423 return true;
424 StringRef Name = getLinkageName();
425 if (Name.empty())
426 Name = getName();
427 if (F->getName() == Name)
428 return true;
429 return false;
430}
431
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000432GlobalVariable *DIGlobalVariable::getGlobal() const {
433 return dyn_cast_or_null<GlobalVariable>(getConstant());
Bill Wendling523bea82013-11-08 08:13:15 +0000434}
435
Bill Wendling523bea82013-11-08 08:13:15 +0000436DIScopeRef DIScope::getContext() const {
437
438 if (isType())
439 return DIType(DbgNode).getContext();
440
441 if (isSubprogram())
442 return DIScopeRef(DISubprogram(DbgNode).getContext());
443
444 if (isLexicalBlock())
445 return DIScopeRef(DILexicalBlock(DbgNode).getContext());
446
447 if (isLexicalBlockFile())
448 return DIScopeRef(DILexicalBlockFile(DbgNode).getContext());
449
450 if (isNameSpace())
451 return DIScopeRef(DINameSpace(DbgNode).getContext());
452
453 assert((isFile() || isCompileUnit()) && "Unhandled type of scope.");
Craig Topperc6207612014-04-09 06:08:46 +0000454 return DIScopeRef(nullptr);
Bill Wendling523bea82013-11-08 08:13:15 +0000455}
456
Bill Wendling523bea82013-11-08 08:13:15 +0000457StringRef DIScope::getName() const {
458 if (isType())
459 return DIType(DbgNode).getName();
460 if (isSubprogram())
461 return DISubprogram(DbgNode).getName();
462 if (isNameSpace())
463 return DINameSpace(DbgNode).getName();
464 assert((isLexicalBlock() || isLexicalBlockFile() || isFile() ||
465 isCompileUnit()) &&
466 "Unhandled type of scope.");
467 return StringRef();
468}
469
470StringRef DIScope::getFilename() const {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000471 if (auto *N = get())
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000472 return ::getStringField(dyn_cast_or_null<MDNode>(N->getFile()), 0);
473 return "";
Bill Wendling523bea82013-11-08 08:13:15 +0000474}
475
476StringRef DIScope::getDirectory() const {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000477 if (auto *N = get())
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000478 return ::getStringField(dyn_cast_or_null<MDNode>(N->getFile()), 1);
479 return "";
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000480}
481
482void DICompileUnit::replaceSubprograms(DIArray Subprograms) {
483 assert(Verify() && "Expected compile unit");
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000484 get()->replaceSubprograms(cast_or_null<MDTuple>(Subprograms.get()));
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000485}
486
487void DICompileUnit::replaceGlobalVariables(DIArray GlobalVariables) {
488 assert(Verify() && "Expected compile unit");
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000489 get()->replaceGlobalVariables(cast_or_null<MDTuple>(GlobalVariables.get()));
Bill Wendling523bea82013-11-08 08:13:15 +0000490}
491
Diego Novillof5041ce2014-03-03 20:06:11 +0000492DILocation DILocation::copyWithNewScope(LLVMContext &Ctx,
David Blaikie2f3f76f2014-08-21 22:45:21 +0000493 DILexicalBlockFile NewScope) {
Diego Novillof5041ce2014-03-03 20:06:11 +0000494 assert(Verify());
Duncan P. N. Exon Smith98854692015-01-14 22:27:36 +0000495 assert(NewScope && "Expected valid scope");
496
497 const auto *Old = cast<MDLocation>(DbgNode);
498 return DILocation(MDLocation::get(Ctx, Old->getLine(), Old->getColumn(),
499 NewScope, Old->getInlinedAt()));
Diego Novillof5041ce2014-03-03 20:06:11 +0000500}
501
Diego Novillof5041ce2014-03-03 20:06:11 +0000502unsigned DILocation::computeNewDiscriminator(LLVMContext &Ctx) {
503 std::pair<const char *, unsigned> Key(getFilename().data(), getLineNumber());
504 return ++Ctx.pImpl->DiscriminatorTable[Key];
505}
506
Bill Wendling523bea82013-11-08 08:13:15 +0000507DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
508 LLVMContext &VMContext) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000509 assert(DIVariable(DV).Verify() && "Expected a DIVariable");
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000510 return cast<MDLocalVariable>(DV)
511 ->withInline(cast_or_null<MDLocation>(InlinedScope));
Bill Wendling523bea82013-11-08 08:13:15 +0000512}
513
Bill Wendling523bea82013-11-08 08:13:15 +0000514DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000515 assert(DIVariable(DV).Verify() && "Expected a DIVariable");
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000516 return cast<MDLocalVariable>(DV)->withoutInline();
Bill Wendling523bea82013-11-08 08:13:15 +0000517}
518
Bill Wendling523bea82013-11-08 08:13:15 +0000519DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
520 DIDescriptor D(Scope);
521 if (D.isSubprogram())
522 return DISubprogram(Scope);
523
524 if (D.isLexicalBlockFile())
525 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
526
527 if (D.isLexicalBlock())
528 return getDISubprogram(DILexicalBlock(Scope).getContext());
529
530 return DISubprogram();
531}
532
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000533DISubprogram llvm::getDISubprogram(const Function *F) {
534 // We look for the first instr that has a debug annotation leading back to F.
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000535 for (auto &BB : *F) {
David Majnemerc758df42014-11-01 07:57:14 +0000536 auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) {
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +0000537 return Inst.getDebugLoc();
David Majnemerc758df42014-11-01 07:57:14 +0000538 });
539 if (Inst == BB.end())
540 continue;
541 DebugLoc DLoc = Inst->getDebugLoc();
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +0000542 const MDNode *Scope = DLoc.getInlinedAtScope();
David Majnemerc758df42014-11-01 07:57:14 +0000543 DISubprogram Subprogram = getDISubprogram(Scope);
544 return Subprogram.describes(F) ? Subprogram : DISubprogram();
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000545 }
546
547 return DISubprogram();
548}
549
Bill Wendling523bea82013-11-08 08:13:15 +0000550DICompositeType llvm::getDICompositeType(DIType T) {
551 if (T.isCompositeType())
552 return DICompositeType(T);
553
554 if (T.isDerivedType()) {
555 // This function is currently used by dragonegg and dragonegg does
556 // not generate identifier for types, so using an empty map to resolve
557 // DerivedFrom should be fine.
558 DITypeIdentifierMap EmptyMap;
559 return getDICompositeType(
560 DIDerivedType(T).getTypeDerivedFrom().resolve(EmptyMap));
561 }
562
563 return DICompositeType();
564}
565
Bill Wendling523bea82013-11-08 08:13:15 +0000566DITypeIdentifierMap
567llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
568 DITypeIdentifierMap Map;
569 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000570 DICompileUnit CU(CU_Nodes->getOperand(CUi));
Bill Wendling523bea82013-11-08 08:13:15 +0000571 DIArray Retain = CU.getRetainedTypes();
572 for (unsigned Ti = 0, Te = Retain.getNumElements(); Ti != Te; ++Ti) {
573 if (!Retain.getElement(Ti).isCompositeType())
574 continue;
575 DICompositeType Ty(Retain.getElement(Ti));
576 if (MDString *TypeId = Ty.getIdentifier()) {
577 // Definition has priority over declaration.
578 // Try to insert (TypeId, Ty) to Map.
579 std::pair<DITypeIdentifierMap::iterator, bool> P =
580 Map.insert(std::make_pair(TypeId, Ty));
581 // If TypeId already exists in Map and this is a definition, replace
582 // whatever we had (declaration or definition) with the definition.
583 if (!P.second && !Ty.isForwardDecl())
584 P.first->second = Ty;
585 }
586 }
587 }
588 return Map;
589}
590
591//===----------------------------------------------------------------------===//
592// DebugInfoFinder implementations.
593//===----------------------------------------------------------------------===//
594
595void DebugInfoFinder::reset() {
596 CUs.clear();
597 SPs.clear();
598 GVs.clear();
599 TYs.clear();
600 Scopes.clear();
601 NodesSeen.clear();
602 TypeIdentifierMap.clear();
Manman Ren2085ccc2013-11-17 18:42:37 +0000603 TypeMapInitialized = false;
604}
605
Manman Renb46e5502013-11-17 19:35:03 +0000606void DebugInfoFinder::InitializeTypeMap(const Module &M) {
Manman Ren2085ccc2013-11-17 18:42:37 +0000607 if (!TypeMapInitialized)
608 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
609 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
610 TypeMapInitialized = true;
611 }
Bill Wendling523bea82013-11-08 08:13:15 +0000612}
613
Bill Wendling523bea82013-11-08 08:13:15 +0000614void DebugInfoFinder::processModule(const Module &M) {
Manman Renb46e5502013-11-17 19:35:03 +0000615 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000616 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
Bill Wendling523bea82013-11-08 08:13:15 +0000617 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000618 DICompileUnit CU(CU_Nodes->getOperand(i));
Bill Wendling523bea82013-11-08 08:13:15 +0000619 addCompileUnit(CU);
620 DIArray GVs = CU.getGlobalVariables();
621 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
622 DIGlobalVariable DIG(GVs.getElement(i));
623 if (addGlobalVariable(DIG)) {
Manman Renf0a582b2014-11-21 19:55:23 +0000624 processScope(DIG.getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000625 processType(DIG.getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000626 }
627 }
628 DIArray SPs = CU.getSubprograms();
629 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
630 processSubprogram(DISubprogram(SPs.getElement(i)));
631 DIArray EnumTypes = CU.getEnumTypes();
632 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
633 processType(DIType(EnumTypes.getElement(i)));
634 DIArray RetainedTypes = CU.getRetainedTypes();
635 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
636 processType(DIType(RetainedTypes.getElement(i)));
637 DIArray Imports = CU.getImportedEntities();
638 for (unsigned i = 0, e = Imports.getNumElements(); i != e; ++i) {
639 DIImportedEntity Import = DIImportedEntity(Imports.getElement(i));
Duncan P. N. Exon Smithd4e07c92015-03-20 19:13:53 +0000640 if (!Import)
641 continue;
Adrian Prantld09ba232014-04-01 03:41:04 +0000642 DIDescriptor Entity = Import.getEntity().resolve(TypeIdentifierMap);
Bill Wendling523bea82013-11-08 08:13:15 +0000643 if (Entity.isType())
644 processType(DIType(Entity));
645 else if (Entity.isSubprogram())
646 processSubprogram(DISubprogram(Entity));
647 else if (Entity.isNameSpace())
648 processScope(DINameSpace(Entity).getContext());
649 }
650 }
651 }
652}
653
Manman Ren2085ccc2013-11-17 18:42:37 +0000654void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) {
Bill Wendling523bea82013-11-08 08:13:15 +0000655 if (!Loc)
656 return;
Manman Renb46e5502013-11-17 19:35:03 +0000657 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000658 processScope(Loc.getScope());
Manman Ren2085ccc2013-11-17 18:42:37 +0000659 processLocation(M, Loc.getOrigLocation());
Bill Wendling523bea82013-11-08 08:13:15 +0000660}
661
Bill Wendling523bea82013-11-08 08:13:15 +0000662void DebugInfoFinder::processType(DIType DT) {
663 if (!addType(DT))
664 return;
665 processScope(DT.getContext().resolve(TypeIdentifierMap));
666 if (DT.isCompositeType()) {
667 DICompositeType DCT(DT);
668 processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
Manman Renf8a19672014-07-28 22:24:06 +0000669 if (DT.isSubroutineType()) {
670 DITypeArray DTA = DISubroutineType(DT).getTypeArray();
671 for (unsigned i = 0, e = DTA.getNumElements(); i != e; ++i)
672 processType(DTA.getElement(i).resolve(TypeIdentifierMap));
673 return;
674 }
Manman Renab8ffba2014-07-28 19:14:13 +0000675 DIArray DA = DCT.getElements();
Bill Wendling523bea82013-11-08 08:13:15 +0000676 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
677 DIDescriptor D = DA.getElement(i);
678 if (D.isType())
679 processType(DIType(D));
680 else if (D.isSubprogram())
681 processSubprogram(DISubprogram(D));
682 }
683 } else if (DT.isDerivedType()) {
684 DIDerivedType DDT(DT);
685 processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
686 }
687}
688
689void DebugInfoFinder::processScope(DIScope Scope) {
690 if (Scope.isType()) {
691 DIType Ty(Scope);
692 processType(Ty);
693 return;
694 }
695 if (Scope.isCompileUnit()) {
696 addCompileUnit(DICompileUnit(Scope));
697 return;
698 }
699 if (Scope.isSubprogram()) {
700 processSubprogram(DISubprogram(Scope));
701 return;
702 }
703 if (!addScope(Scope))
704 return;
705 if (Scope.isLexicalBlock()) {
706 DILexicalBlock LB(Scope);
707 processScope(LB.getContext());
708 } else if (Scope.isLexicalBlockFile()) {
709 DILexicalBlockFile LBF = DILexicalBlockFile(Scope);
710 processScope(LBF.getScope());
711 } else if (Scope.isNameSpace()) {
712 DINameSpace NS(Scope);
713 processScope(NS.getContext());
714 }
715}
716
Bill Wendling523bea82013-11-08 08:13:15 +0000717void DebugInfoFinder::processSubprogram(DISubprogram SP) {
718 if (!addSubprogram(SP))
719 return;
720 processScope(SP.getContext().resolve(TypeIdentifierMap));
721 processType(SP.getType());
722 DIArray TParams = SP.getTemplateParams();
723 for (unsigned I = 0, E = TParams.getNumElements(); I != E; ++I) {
724 DIDescriptor Element = TParams.getElement(I);
725 if (Element.isTemplateTypeParameter()) {
726 DITemplateTypeParameter TType(Element);
Bill Wendling523bea82013-11-08 08:13:15 +0000727 processType(TType.getType().resolve(TypeIdentifierMap));
728 } else if (Element.isTemplateValueParameter()) {
729 DITemplateValueParameter TVal(Element);
Bill Wendling523bea82013-11-08 08:13:15 +0000730 processType(TVal.getType().resolve(TypeIdentifierMap));
731 }
732 }
733}
734
Manman Ren2085ccc2013-11-17 18:42:37 +0000735void DebugInfoFinder::processDeclare(const Module &M,
736 const DbgDeclareInst *DDI) {
Bill Wendling523bea82013-11-08 08:13:15 +0000737 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
738 if (!N)
739 return;
Manman Renb46e5502013-11-17 19:35:03 +0000740 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000741
742 DIDescriptor DV(N);
743 if (!DV.isVariable())
744 return;
745
David Blaikie70573dc2014-11-19 07:49:26 +0000746 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000747 return;
748 processScope(DIVariable(N).getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000749 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000750}
751
Manman Ren2085ccc2013-11-17 18:42:37 +0000752void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
Bill Wendling523bea82013-11-08 08:13:15 +0000753 MDNode *N = dyn_cast<MDNode>(DVI->getVariable());
754 if (!N)
755 return;
Manman Renb46e5502013-11-17 19:35:03 +0000756 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000757
758 DIDescriptor DV(N);
759 if (!DV.isVariable())
760 return;
761
David Blaikie70573dc2014-11-19 07:49:26 +0000762 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000763 return;
764 processScope(DIVariable(N).getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000765 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000766}
767
Bill Wendling523bea82013-11-08 08:13:15 +0000768bool DebugInfoFinder::addType(DIType DT) {
769 if (!DT)
770 return false;
771
David Blaikie70573dc2014-11-19 07:49:26 +0000772 if (!NodesSeen.insert(DT).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000773 return false;
774
775 TYs.push_back(DT);
776 return true;
777}
778
Bill Wendling523bea82013-11-08 08:13:15 +0000779bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
780 if (!CU)
781 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000782 if (!NodesSeen.insert(CU).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000783 return false;
784
785 CUs.push_back(CU);
786 return true;
787}
788
Bill Wendling523bea82013-11-08 08:13:15 +0000789bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
790 if (!DIG)
791 return false;
792
David Blaikie70573dc2014-11-19 07:49:26 +0000793 if (!NodesSeen.insert(DIG).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000794 return false;
795
796 GVs.push_back(DIG);
797 return true;
798}
799
Bill Wendling523bea82013-11-08 08:13:15 +0000800bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
801 if (!SP)
802 return false;
803
David Blaikie70573dc2014-11-19 07:49:26 +0000804 if (!NodesSeen.insert(SP).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000805 return false;
806
807 SPs.push_back(SP);
808 return true;
809}
810
811bool DebugInfoFinder::addScope(DIScope Scope) {
812 if (!Scope)
813 return false;
814 // FIXME: Ocaml binding generates a scope with no content, we treat it
815 // as null for now.
816 if (Scope->getNumOperands() == 0)
817 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000818 if (!NodesSeen.insert(Scope).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000819 return false;
820 Scopes.push_back(Scope);
821 return true;
822}
823
824//===----------------------------------------------------------------------===//
825// DIDescriptor: dump routines for all descriptors.
826//===----------------------------------------------------------------------===//
827
Bill Wendling523bea82013-11-08 08:13:15 +0000828void DIDescriptor::dump() const {
829 print(dbgs());
830 dbgs() << '\n';
831}
832
Bill Wendling523bea82013-11-08 08:13:15 +0000833void DIDescriptor::print(raw_ostream &OS) const {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000834 if (!get())
Bill Wendling523bea82013-11-08 08:13:15 +0000835 return;
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000836 get()->print(OS);
Bill Wendling523bea82013-11-08 08:13:15 +0000837}
838
839static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
840 const LLVMContext &Ctx) {
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +0000841 if (!DL)
Duncan P. N. Exon Smith51306ef2015-03-30 18:45:11 +0000842 return;
843
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +0000844 DIScope Scope(DL.getScope());
Duncan P. N. Exon Smith51306ef2015-03-30 18:45:11 +0000845 assert(Scope.isScope() && "Scope of a DebugLoc should be a DIScope.");
846 // Omit the directory, because it's likely to be long and uninteresting.
847 CommentOS << Scope.getFilename();
848 CommentOS << ':' << DL.getLine();
849 if (DL.getCol() != 0)
850 CommentOS << ':' << DL.getCol();
851
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +0000852 DebugLoc InlinedAtDL = DL.getInlinedAt();
853 if (!InlinedAtDL)
Duncan P. N. Exon Smith51306ef2015-03-30 18:45:11 +0000854 return;
855
856 CommentOS << " @[ ";
857 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
858 CommentOS << " ]";
Bill Wendling523bea82013-11-08 08:13:15 +0000859}
860
861void DIVariable::printExtendedName(raw_ostream &OS) const {
862 const LLVMContext &Ctx = DbgNode->getContext();
863 StringRef Res = getName();
864 if (!Res.empty())
865 OS << Res << "," << getLineNumber();
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +0000866 if (auto *InlinedAt = get()->getInlinedAt()) {
867 if (DebugLoc InlinedAtDL = InlinedAt) {
Bill Wendling523bea82013-11-08 08:13:15 +0000868 OS << " @[";
869 printDebugLoc(InlinedAtDL, OS, Ctx);
870 OS << "]";
871 }
872 }
873}
874
Duncan P. N. Exon Smith2a78e9b2015-02-18 19:39:36 +0000875template <> DIRef<DIDescriptor>::DIRef(const Metadata *V) : Val(V) {
876 assert(isDescriptorRef(V) &&
877 "DIDescriptorRef should be a MDString or MDNode");
878}
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000879template <> DIRef<DIScope>::DIRef(const Metadata *V) : Val(V) {
Bill Wendling523bea82013-11-08 08:13:15 +0000880 assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode");
881}
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000882template <> DIRef<DIType>::DIRef(const Metadata *V) : Val(V) {
Bill Wendling523bea82013-11-08 08:13:15 +0000883 assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode");
884}
885
Bill Wendling523bea82013-11-08 08:13:15 +0000886template <>
Duncan P. N. Exon Smith2a78e9b2015-02-18 19:39:36 +0000887DIDescriptorRef DIDescriptor::getFieldAs<DIDescriptorRef>(unsigned Elt) const {
888 return DIDescriptorRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
889}
890template <>
Bill Wendling523bea82013-11-08 08:13:15 +0000891DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000892 return DIScopeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +0000893}
Bill Wendling523bea82013-11-08 08:13:15 +0000894template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000895 return DITypeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +0000896}
Manman Rencb14bbc2013-11-22 22:06:31 +0000897
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000898bool llvm::stripDebugInfo(Function &F) {
899 bool Changed = false;
900 for (BasicBlock &BB : F) {
901 for (Instruction &I : BB) {
902 if (I.getDebugLoc()) {
903 Changed = true;
904 I.setDebugLoc(DebugLoc());
905 }
906 }
907 }
908 return Changed;
909}
910
Manman Rencb14bbc2013-11-22 22:06:31 +0000911bool llvm::StripDebugInfo(Module &M) {
Manman Rencb14bbc2013-11-22 22:06:31 +0000912 bool Changed = false;
913
914 // Remove all of the calls to the debugger intrinsics, and remove them from
915 // the module.
916 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
917 while (!Declare->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000918 CallInst *CI = cast<CallInst>(Declare->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +0000919 CI->eraseFromParent();
920 }
921 Declare->eraseFromParent();
922 Changed = true;
923 }
924
925 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
926 while (!DbgVal->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000927 CallInst *CI = cast<CallInst>(DbgVal->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +0000928 CI->eraseFromParent();
929 }
930 DbgVal->eraseFromParent();
931 Changed = true;
932 }
933
934 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
935 NME = M.named_metadata_end(); NMI != NME;) {
936 NamedMDNode *NMD = NMI;
937 ++NMI;
938 if (NMD->getName().startswith("llvm.dbg.")) {
939 NMD->eraseFromParent();
940 Changed = true;
941 }
942 }
943
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000944 for (Function &F : M)
945 Changed |= stripDebugInfo(F);
946
947 if ( GVMaterializer *Materializer = M.getMaterializer())
948 Materializer->setStripDebugInfo();
Manman Rencb14bbc2013-11-22 22:06:31 +0000949
950 return Changed;
951}
Manman Ren8b4306c2013-12-02 21:29:56 +0000952
Manman Renbd4daf82013-12-03 00:12:14 +0000953unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
David Majnemere7a9cdb2015-02-16 06:04:53 +0000954 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000955 M.getModuleFlag("Debug Info Version")))
956 return Val->getZExtValue();
957 return 0;
Manman Ren8b4306c2013-12-02 21:29:56 +0000958}
David Blaikie6876b3b2014-07-01 20:05:26 +0000959
David Blaikiea8c35092014-07-02 18:30:05 +0000960llvm::DenseMap<const llvm::Function *, llvm::DISubprogram>
961llvm::makeSubprogramMap(const Module &M) {
962 DenseMap<const Function *, DISubprogram> R;
David Blaikie6876b3b2014-07-01 20:05:26 +0000963
964 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
965 if (!CU_Nodes)
966 return R;
967
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000968 for (MDNode *N : CU_Nodes->operands()) {
969 DICompileUnit CUNode(N);
David Blaikie6876b3b2014-07-01 20:05:26 +0000970 DIArray SPs = CUNode.getSubprograms();
971 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
972 DISubprogram SP(SPs.getElement(i));
973 if (Function *F = SP.getFunction())
974 R.insert(std::make_pair(F, SP));
975 }
976 }
977 return R;
978}