blob: 91823773212b442d90216e81c1928fdaa0944f82 [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
Adrian Prantl99c7af22013-12-18 21:48:19 +0000278 if (isLValueReference() && isRValueReference())
279 return false;
280
David Blaikie3dfe4782014-10-14 18:22:52 +0000281 // If a DISubprogram has an llvm::Function*, then scope chains from all
282 // instructions within the function should lead to this DISubprogram.
283 if (auto *F = getFunction()) {
David Blaikie3dfe4782014-10-14 18:22:52 +0000284 for (auto &BB : *F) {
285 for (auto &I : BB) {
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +0000286 MDLocation *DL = I.getDebugLoc();
Duncan P. N. Exon Smith215e7ed2015-03-30 17:06:38 +0000287 if (!DL)
David Blaikie3dfe4782014-10-14 18:22:52 +0000288 continue;
289
David Blaikie3dfe4782014-10-14 18:22:52 +0000290 // walk the inlined-at scopes
Duncan P. N. Exon Smith8f7bc792015-03-30 17:41:24 +0000291 MDScope *Scope = DL->getInlinedAtScope();
Adrian Prantlde200df2015-01-20 22:37:25 +0000292 if (!Scope)
293 return false;
Duncan P. N. Exon Smith215e7ed2015-03-30 17:06:38 +0000294 while (!isa<MDSubprogram>(Scope)) {
295 Scope = cast<MDLexicalBlockBase>(Scope)->getScope();
Adrian Prantl1292e242015-01-21 18:32:56 +0000296 if (!Scope)
297 return false;
David Blaikie3dfe4782014-10-14 18:22:52 +0000298 }
299 if (!DISubprogram(Scope).describes(F))
300 return false;
301 }
302 }
303 }
Bill Wendling523bea82013-11-08 08:13:15 +0000304
Adrian Prantl9260cca2015-01-22 00:00:52 +0000305 return true;
Bill Wendling523bea82013-11-08 08:13:15 +0000306}
307
Duncan P. N. Exon Smith94d58f82015-03-31 01:28:22 +0000308bool DIGlobalVariable::Verify() const { return isGlobalVariable(); }
309bool DIVariable::Verify() const { return isVariable(); }
Bill Wendling523bea82013-11-08 08:13:15 +0000310
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000311bool DILocation::Verify() const {
312 return dyn_cast_or_null<MDLocation>(DbgNode);
313}
314bool DINameSpace::Verify() const {
315 return dyn_cast_or_null<MDNamespace>(DbgNode);
316}
317bool DIFile::Verify() const { return dyn_cast_or_null<MDFile>(DbgNode); }
318bool DIEnumerator::Verify() const {
319 return dyn_cast_or_null<MDEnumerator>(DbgNode);
320}
321bool DISubrange::Verify() const {
322 return dyn_cast_or_null<MDSubrange>(DbgNode);
323}
324bool DILexicalBlock::Verify() const {
325 return dyn_cast_or_null<MDLexicalBlock>(DbgNode);
326}
327bool DILexicalBlockFile::Verify() const {
328 return dyn_cast_or_null<MDLexicalBlockFile>(DbgNode);
329}
330bool DITemplateTypeParameter::Verify() const {
331 return dyn_cast_or_null<MDTemplateTypeParameter>(DbgNode);
332}
333bool DITemplateValueParameter::Verify() const {
334 return dyn_cast_or_null<MDTemplateValueParameter>(DbgNode);
335}
336bool DIImportedEntity::Verify() const {
337 return dyn_cast_or_null<MDImportedEntity>(DbgNode);
338}
Bill Wendling523bea82013-11-08 08:13:15 +0000339
Manman Ren1a125c92014-07-28 19:33:20 +0000340void DICompositeType::setArraysHelper(MDNode *Elements, MDNode *TParams) {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000341 TypedTrackingMDRef<MDCompositeTypeBase> N(get());
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000342 if (Elements)
343 N->replaceElements(cast<MDTuple>(Elements));
Bill Wendling523bea82013-11-08 08:13:15 +0000344 if (TParams)
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000345 N->replaceTemplateParams(cast<MDTuple>(TParams));
Bill Wendling523bea82013-11-08 08:13:15 +0000346 DbgNode = N;
347}
348
Bill Wendling523bea82013-11-08 08:13:15 +0000349DIScopeRef DIScope::getRef() const {
350 if (!isCompositeType())
351 return DIScopeRef(*this);
352 DICompositeType DTy(DbgNode);
353 if (!DTy.getIdentifier())
354 return DIScopeRef(*this);
355 return DIScopeRef(DTy.getIdentifier());
356}
357
Bill Wendling523bea82013-11-08 08:13:15 +0000358void DICompositeType::setContainingType(DICompositeType ContainingType) {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000359 TypedTrackingMDRef<MDCompositeTypeBase> N(get());
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000360 N->replaceVTableHolder(ContainingType.getRef());
Bill Wendling523bea82013-11-08 08:13:15 +0000361 DbgNode = N;
362}
363
Bill Wendling523bea82013-11-08 08:13:15 +0000364bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
365 assert(CurFn && "Invalid function");
366 if (!getContext().isSubprogram())
367 return false;
368 // This variable is not inlined function argument if its scope
369 // does not describe current function.
370 return !DISubprogram(getContext()).describes(CurFn);
371}
372
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000373Function *DISubprogram::getFunction() const {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000374 if (auto *N = get())
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000375 if (auto *C = dyn_cast_or_null<ConstantAsMetadata>(N->getFunction()))
376 return dyn_cast<Function>(C->getValue());
377 return nullptr;
378}
379
Bill Wendling523bea82013-11-08 08:13:15 +0000380bool DISubprogram::describes(const Function *F) {
381 assert(F && "Invalid function");
382 if (F == getFunction())
383 return true;
384 StringRef Name = getLinkageName();
385 if (Name.empty())
386 Name = getName();
387 if (F->getName() == Name)
388 return true;
389 return false;
390}
391
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000392GlobalVariable *DIGlobalVariable::getGlobal() const {
393 return dyn_cast_or_null<GlobalVariable>(getConstant());
Bill Wendling523bea82013-11-08 08:13:15 +0000394}
395
Bill Wendling523bea82013-11-08 08:13:15 +0000396DIScopeRef DIScope::getContext() const {
397
398 if (isType())
399 return DIType(DbgNode).getContext();
400
401 if (isSubprogram())
402 return DIScopeRef(DISubprogram(DbgNode).getContext());
403
404 if (isLexicalBlock())
405 return DIScopeRef(DILexicalBlock(DbgNode).getContext());
406
407 if (isLexicalBlockFile())
408 return DIScopeRef(DILexicalBlockFile(DbgNode).getContext());
409
410 if (isNameSpace())
411 return DIScopeRef(DINameSpace(DbgNode).getContext());
412
413 assert((isFile() || isCompileUnit()) && "Unhandled type of scope.");
Craig Topperc6207612014-04-09 06:08:46 +0000414 return DIScopeRef(nullptr);
Bill Wendling523bea82013-11-08 08:13:15 +0000415}
416
Bill Wendling523bea82013-11-08 08:13:15 +0000417StringRef DIScope::getName() const {
418 if (isType())
419 return DIType(DbgNode).getName();
420 if (isSubprogram())
421 return DISubprogram(DbgNode).getName();
422 if (isNameSpace())
423 return DINameSpace(DbgNode).getName();
424 assert((isLexicalBlock() || isLexicalBlockFile() || isFile() ||
425 isCompileUnit()) &&
426 "Unhandled type of scope.");
427 return StringRef();
428}
429
430StringRef DIScope::getFilename() const {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000431 if (auto *N = get())
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000432 return ::getStringField(dyn_cast_or_null<MDNode>(N->getFile()), 0);
433 return "";
Bill Wendling523bea82013-11-08 08:13:15 +0000434}
435
436StringRef DIScope::getDirectory() const {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000437 if (auto *N = get())
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000438 return ::getStringField(dyn_cast_or_null<MDNode>(N->getFile()), 1);
439 return "";
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000440}
441
442void DICompileUnit::replaceSubprograms(DIArray Subprograms) {
443 assert(Verify() && "Expected compile unit");
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000444 get()->replaceSubprograms(cast_or_null<MDTuple>(Subprograms.get()));
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000445}
446
447void DICompileUnit::replaceGlobalVariables(DIArray GlobalVariables) {
448 assert(Verify() && "Expected compile unit");
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000449 get()->replaceGlobalVariables(cast_or_null<MDTuple>(GlobalVariables.get()));
Bill Wendling523bea82013-11-08 08:13:15 +0000450}
451
Diego Novillof5041ce2014-03-03 20:06:11 +0000452DILocation DILocation::copyWithNewScope(LLVMContext &Ctx,
David Blaikie2f3f76f2014-08-21 22:45:21 +0000453 DILexicalBlockFile NewScope) {
Diego Novillof5041ce2014-03-03 20:06:11 +0000454 assert(Verify());
Duncan P. N. Exon Smith98854692015-01-14 22:27:36 +0000455 assert(NewScope && "Expected valid scope");
456
457 const auto *Old = cast<MDLocation>(DbgNode);
458 return DILocation(MDLocation::get(Ctx, Old->getLine(), Old->getColumn(),
459 NewScope, Old->getInlinedAt()));
Diego Novillof5041ce2014-03-03 20:06:11 +0000460}
461
Diego Novillof5041ce2014-03-03 20:06:11 +0000462unsigned DILocation::computeNewDiscriminator(LLVMContext &Ctx) {
463 std::pair<const char *, unsigned> Key(getFilename().data(), getLineNumber());
464 return ++Ctx.pImpl->DiscriminatorTable[Key];
465}
466
Bill Wendling523bea82013-11-08 08:13:15 +0000467DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
468 LLVMContext &VMContext) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000469 assert(DIVariable(DV).Verify() && "Expected a DIVariable");
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000470 return cast<MDLocalVariable>(DV)
471 ->withInline(cast_or_null<MDLocation>(InlinedScope));
Bill Wendling523bea82013-11-08 08:13:15 +0000472}
473
Bill Wendling523bea82013-11-08 08:13:15 +0000474DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000475 assert(DIVariable(DV).Verify() && "Expected a DIVariable");
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000476 return cast<MDLocalVariable>(DV)->withoutInline();
Bill Wendling523bea82013-11-08 08:13:15 +0000477}
478
Bill Wendling523bea82013-11-08 08:13:15 +0000479DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
480 DIDescriptor D(Scope);
481 if (D.isSubprogram())
482 return DISubprogram(Scope);
483
484 if (D.isLexicalBlockFile())
485 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
486
487 if (D.isLexicalBlock())
488 return getDISubprogram(DILexicalBlock(Scope).getContext());
489
490 return DISubprogram();
491}
492
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000493DISubprogram llvm::getDISubprogram(const Function *F) {
494 // We look for the first instr that has a debug annotation leading back to F.
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000495 for (auto &BB : *F) {
David Majnemerc758df42014-11-01 07:57:14 +0000496 auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) {
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +0000497 return Inst.getDebugLoc();
David Majnemerc758df42014-11-01 07:57:14 +0000498 });
499 if (Inst == BB.end())
500 continue;
501 DebugLoc DLoc = Inst->getDebugLoc();
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +0000502 const MDNode *Scope = DLoc.getInlinedAtScope();
David Majnemerc758df42014-11-01 07:57:14 +0000503 DISubprogram Subprogram = getDISubprogram(Scope);
504 return Subprogram.describes(F) ? Subprogram : DISubprogram();
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000505 }
506
507 return DISubprogram();
508}
509
Bill Wendling523bea82013-11-08 08:13:15 +0000510DICompositeType llvm::getDICompositeType(DIType T) {
511 if (T.isCompositeType())
512 return DICompositeType(T);
513
514 if (T.isDerivedType()) {
515 // This function is currently used by dragonegg and dragonegg does
516 // not generate identifier for types, so using an empty map to resolve
517 // DerivedFrom should be fine.
518 DITypeIdentifierMap EmptyMap;
519 return getDICompositeType(
520 DIDerivedType(T).getTypeDerivedFrom().resolve(EmptyMap));
521 }
522
523 return DICompositeType();
524}
525
Bill Wendling523bea82013-11-08 08:13:15 +0000526DITypeIdentifierMap
527llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
528 DITypeIdentifierMap Map;
529 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000530 DICompileUnit CU(CU_Nodes->getOperand(CUi));
Bill Wendling523bea82013-11-08 08:13:15 +0000531 DIArray Retain = CU.getRetainedTypes();
532 for (unsigned Ti = 0, Te = Retain.getNumElements(); Ti != Te; ++Ti) {
533 if (!Retain.getElement(Ti).isCompositeType())
534 continue;
535 DICompositeType Ty(Retain.getElement(Ti));
536 if (MDString *TypeId = Ty.getIdentifier()) {
537 // Definition has priority over declaration.
538 // Try to insert (TypeId, Ty) to Map.
539 std::pair<DITypeIdentifierMap::iterator, bool> P =
540 Map.insert(std::make_pair(TypeId, Ty));
541 // If TypeId already exists in Map and this is a definition, replace
542 // whatever we had (declaration or definition) with the definition.
543 if (!P.second && !Ty.isForwardDecl())
544 P.first->second = Ty;
545 }
546 }
547 }
548 return Map;
549}
550
551//===----------------------------------------------------------------------===//
552// DebugInfoFinder implementations.
553//===----------------------------------------------------------------------===//
554
555void DebugInfoFinder::reset() {
556 CUs.clear();
557 SPs.clear();
558 GVs.clear();
559 TYs.clear();
560 Scopes.clear();
561 NodesSeen.clear();
562 TypeIdentifierMap.clear();
Manman Ren2085ccc2013-11-17 18:42:37 +0000563 TypeMapInitialized = false;
564}
565
Manman Renb46e5502013-11-17 19:35:03 +0000566void DebugInfoFinder::InitializeTypeMap(const Module &M) {
Manman Ren2085ccc2013-11-17 18:42:37 +0000567 if (!TypeMapInitialized)
568 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
569 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
570 TypeMapInitialized = true;
571 }
Bill Wendling523bea82013-11-08 08:13:15 +0000572}
573
Bill Wendling523bea82013-11-08 08:13:15 +0000574void DebugInfoFinder::processModule(const Module &M) {
Manman Renb46e5502013-11-17 19:35:03 +0000575 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000576 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
Bill Wendling523bea82013-11-08 08:13:15 +0000577 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000578 DICompileUnit CU(CU_Nodes->getOperand(i));
Bill Wendling523bea82013-11-08 08:13:15 +0000579 addCompileUnit(CU);
580 DIArray GVs = CU.getGlobalVariables();
581 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
582 DIGlobalVariable DIG(GVs.getElement(i));
583 if (addGlobalVariable(DIG)) {
Manman Renf0a582b2014-11-21 19:55:23 +0000584 processScope(DIG.getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000585 processType(DIG.getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000586 }
587 }
588 DIArray SPs = CU.getSubprograms();
589 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
590 processSubprogram(DISubprogram(SPs.getElement(i)));
591 DIArray EnumTypes = CU.getEnumTypes();
592 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
593 processType(DIType(EnumTypes.getElement(i)));
594 DIArray RetainedTypes = CU.getRetainedTypes();
595 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
596 processType(DIType(RetainedTypes.getElement(i)));
597 DIArray Imports = CU.getImportedEntities();
598 for (unsigned i = 0, e = Imports.getNumElements(); i != e; ++i) {
599 DIImportedEntity Import = DIImportedEntity(Imports.getElement(i));
Duncan P. N. Exon Smithd4e07c92015-03-20 19:13:53 +0000600 if (!Import)
601 continue;
Adrian Prantld09ba232014-04-01 03:41:04 +0000602 DIDescriptor Entity = Import.getEntity().resolve(TypeIdentifierMap);
Bill Wendling523bea82013-11-08 08:13:15 +0000603 if (Entity.isType())
604 processType(DIType(Entity));
605 else if (Entity.isSubprogram())
606 processSubprogram(DISubprogram(Entity));
607 else if (Entity.isNameSpace())
608 processScope(DINameSpace(Entity).getContext());
609 }
610 }
611 }
612}
613
Manman Ren2085ccc2013-11-17 18:42:37 +0000614void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) {
Bill Wendling523bea82013-11-08 08:13:15 +0000615 if (!Loc)
616 return;
Manman Renb46e5502013-11-17 19:35:03 +0000617 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000618 processScope(Loc.getScope());
Manman Ren2085ccc2013-11-17 18:42:37 +0000619 processLocation(M, Loc.getOrigLocation());
Bill Wendling523bea82013-11-08 08:13:15 +0000620}
621
Bill Wendling523bea82013-11-08 08:13:15 +0000622void DebugInfoFinder::processType(DIType DT) {
623 if (!addType(DT))
624 return;
625 processScope(DT.getContext().resolve(TypeIdentifierMap));
626 if (DT.isCompositeType()) {
627 DICompositeType DCT(DT);
628 processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
Manman Renf8a19672014-07-28 22:24:06 +0000629 if (DT.isSubroutineType()) {
630 DITypeArray DTA = DISubroutineType(DT).getTypeArray();
631 for (unsigned i = 0, e = DTA.getNumElements(); i != e; ++i)
632 processType(DTA.getElement(i).resolve(TypeIdentifierMap));
633 return;
634 }
Manman Renab8ffba2014-07-28 19:14:13 +0000635 DIArray DA = DCT.getElements();
Bill Wendling523bea82013-11-08 08:13:15 +0000636 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
637 DIDescriptor D = DA.getElement(i);
638 if (D.isType())
639 processType(DIType(D));
640 else if (D.isSubprogram())
641 processSubprogram(DISubprogram(D));
642 }
643 } else if (DT.isDerivedType()) {
644 DIDerivedType DDT(DT);
645 processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
646 }
647}
648
649void DebugInfoFinder::processScope(DIScope Scope) {
650 if (Scope.isType()) {
651 DIType Ty(Scope);
652 processType(Ty);
653 return;
654 }
655 if (Scope.isCompileUnit()) {
656 addCompileUnit(DICompileUnit(Scope));
657 return;
658 }
659 if (Scope.isSubprogram()) {
660 processSubprogram(DISubprogram(Scope));
661 return;
662 }
663 if (!addScope(Scope))
664 return;
665 if (Scope.isLexicalBlock()) {
666 DILexicalBlock LB(Scope);
667 processScope(LB.getContext());
668 } else if (Scope.isLexicalBlockFile()) {
669 DILexicalBlockFile LBF = DILexicalBlockFile(Scope);
670 processScope(LBF.getScope());
671 } else if (Scope.isNameSpace()) {
672 DINameSpace NS(Scope);
673 processScope(NS.getContext());
674 }
675}
676
Bill Wendling523bea82013-11-08 08:13:15 +0000677void DebugInfoFinder::processSubprogram(DISubprogram SP) {
678 if (!addSubprogram(SP))
679 return;
680 processScope(SP.getContext().resolve(TypeIdentifierMap));
681 processType(SP.getType());
682 DIArray TParams = SP.getTemplateParams();
683 for (unsigned I = 0, E = TParams.getNumElements(); I != E; ++I) {
684 DIDescriptor Element = TParams.getElement(I);
685 if (Element.isTemplateTypeParameter()) {
686 DITemplateTypeParameter TType(Element);
Bill Wendling523bea82013-11-08 08:13:15 +0000687 processType(TType.getType().resolve(TypeIdentifierMap));
688 } else if (Element.isTemplateValueParameter()) {
689 DITemplateValueParameter TVal(Element);
Bill Wendling523bea82013-11-08 08:13:15 +0000690 processType(TVal.getType().resolve(TypeIdentifierMap));
691 }
692 }
693}
694
Manman Ren2085ccc2013-11-17 18:42:37 +0000695void DebugInfoFinder::processDeclare(const Module &M,
696 const DbgDeclareInst *DDI) {
Bill Wendling523bea82013-11-08 08:13:15 +0000697 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
698 if (!N)
699 return;
Manman Renb46e5502013-11-17 19:35:03 +0000700 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000701
702 DIDescriptor DV(N);
703 if (!DV.isVariable())
704 return;
705
David Blaikie70573dc2014-11-19 07:49:26 +0000706 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000707 return;
708 processScope(DIVariable(N).getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000709 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000710}
711
Manman Ren2085ccc2013-11-17 18:42:37 +0000712void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
Bill Wendling523bea82013-11-08 08:13:15 +0000713 MDNode *N = dyn_cast<MDNode>(DVI->getVariable());
714 if (!N)
715 return;
Manman Renb46e5502013-11-17 19:35:03 +0000716 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000717
718 DIDescriptor DV(N);
719 if (!DV.isVariable())
720 return;
721
David Blaikie70573dc2014-11-19 07:49:26 +0000722 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000723 return;
724 processScope(DIVariable(N).getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000725 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000726}
727
Bill Wendling523bea82013-11-08 08:13:15 +0000728bool DebugInfoFinder::addType(DIType DT) {
729 if (!DT)
730 return false;
731
David Blaikie70573dc2014-11-19 07:49:26 +0000732 if (!NodesSeen.insert(DT).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000733 return false;
734
735 TYs.push_back(DT);
736 return true;
737}
738
Bill Wendling523bea82013-11-08 08:13:15 +0000739bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
740 if (!CU)
741 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000742 if (!NodesSeen.insert(CU).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000743 return false;
744
745 CUs.push_back(CU);
746 return true;
747}
748
Bill Wendling523bea82013-11-08 08:13:15 +0000749bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
750 if (!DIG)
751 return false;
752
David Blaikie70573dc2014-11-19 07:49:26 +0000753 if (!NodesSeen.insert(DIG).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000754 return false;
755
756 GVs.push_back(DIG);
757 return true;
758}
759
Bill Wendling523bea82013-11-08 08:13:15 +0000760bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
761 if (!SP)
762 return false;
763
David Blaikie70573dc2014-11-19 07:49:26 +0000764 if (!NodesSeen.insert(SP).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000765 return false;
766
767 SPs.push_back(SP);
768 return true;
769}
770
771bool DebugInfoFinder::addScope(DIScope Scope) {
772 if (!Scope)
773 return false;
774 // FIXME: Ocaml binding generates a scope with no content, we treat it
775 // as null for now.
776 if (Scope->getNumOperands() == 0)
777 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000778 if (!NodesSeen.insert(Scope).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000779 return false;
780 Scopes.push_back(Scope);
781 return true;
782}
783
784//===----------------------------------------------------------------------===//
785// DIDescriptor: dump routines for all descriptors.
786//===----------------------------------------------------------------------===//
787
Bill Wendling523bea82013-11-08 08:13:15 +0000788void DIDescriptor::dump() const {
789 print(dbgs());
790 dbgs() << '\n';
791}
792
Bill Wendling523bea82013-11-08 08:13:15 +0000793void DIDescriptor::print(raw_ostream &OS) const {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000794 if (!get())
Bill Wendling523bea82013-11-08 08:13:15 +0000795 return;
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000796 get()->print(OS);
Bill Wendling523bea82013-11-08 08:13:15 +0000797}
798
799static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
800 const LLVMContext &Ctx) {
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +0000801 if (!DL)
Duncan P. N. Exon Smith51306ef2015-03-30 18:45:11 +0000802 return;
803
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +0000804 DIScope Scope(DL.getScope());
Duncan P. N. Exon Smith51306ef2015-03-30 18:45:11 +0000805 assert(Scope.isScope() && "Scope of a DebugLoc should be a DIScope.");
806 // Omit the directory, because it's likely to be long and uninteresting.
807 CommentOS << Scope.getFilename();
808 CommentOS << ':' << DL.getLine();
809 if (DL.getCol() != 0)
810 CommentOS << ':' << DL.getCol();
811
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +0000812 DebugLoc InlinedAtDL = DL.getInlinedAt();
813 if (!InlinedAtDL)
Duncan P. N. Exon Smith51306ef2015-03-30 18:45:11 +0000814 return;
815
816 CommentOS << " @[ ";
817 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
818 CommentOS << " ]";
Bill Wendling523bea82013-11-08 08:13:15 +0000819}
820
821void DIVariable::printExtendedName(raw_ostream &OS) const {
822 const LLVMContext &Ctx = DbgNode->getContext();
823 StringRef Res = getName();
824 if (!Res.empty())
825 OS << Res << "," << getLineNumber();
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +0000826 if (auto *InlinedAt = get()->getInlinedAt()) {
827 if (DebugLoc InlinedAtDL = InlinedAt) {
Bill Wendling523bea82013-11-08 08:13:15 +0000828 OS << " @[";
829 printDebugLoc(InlinedAtDL, OS, Ctx);
830 OS << "]";
831 }
832 }
833}
834
Duncan P. N. Exon Smith2a78e9b2015-02-18 19:39:36 +0000835template <> DIRef<DIDescriptor>::DIRef(const Metadata *V) : Val(V) {
836 assert(isDescriptorRef(V) &&
837 "DIDescriptorRef should be a MDString or MDNode");
838}
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000839template <> DIRef<DIScope>::DIRef(const Metadata *V) : Val(V) {
Bill Wendling523bea82013-11-08 08:13:15 +0000840 assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode");
841}
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000842template <> DIRef<DIType>::DIRef(const Metadata *V) : Val(V) {
Bill Wendling523bea82013-11-08 08:13:15 +0000843 assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode");
844}
845
Bill Wendling523bea82013-11-08 08:13:15 +0000846template <>
Duncan P. N. Exon Smith2a78e9b2015-02-18 19:39:36 +0000847DIDescriptorRef DIDescriptor::getFieldAs<DIDescriptorRef>(unsigned Elt) const {
848 return DIDescriptorRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
849}
850template <>
Bill Wendling523bea82013-11-08 08:13:15 +0000851DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000852 return DIScopeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +0000853}
Bill Wendling523bea82013-11-08 08:13:15 +0000854template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000855 return DITypeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +0000856}
Manman Rencb14bbc2013-11-22 22:06:31 +0000857
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000858bool llvm::stripDebugInfo(Function &F) {
859 bool Changed = false;
860 for (BasicBlock &BB : F) {
861 for (Instruction &I : BB) {
862 if (I.getDebugLoc()) {
863 Changed = true;
864 I.setDebugLoc(DebugLoc());
865 }
866 }
867 }
868 return Changed;
869}
870
Manman Rencb14bbc2013-11-22 22:06:31 +0000871bool llvm::StripDebugInfo(Module &M) {
Manman Rencb14bbc2013-11-22 22:06:31 +0000872 bool Changed = false;
873
874 // Remove all of the calls to the debugger intrinsics, and remove them from
875 // the module.
876 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
877 while (!Declare->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000878 CallInst *CI = cast<CallInst>(Declare->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +0000879 CI->eraseFromParent();
880 }
881 Declare->eraseFromParent();
882 Changed = true;
883 }
884
885 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
886 while (!DbgVal->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000887 CallInst *CI = cast<CallInst>(DbgVal->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +0000888 CI->eraseFromParent();
889 }
890 DbgVal->eraseFromParent();
891 Changed = true;
892 }
893
894 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
895 NME = M.named_metadata_end(); NMI != NME;) {
896 NamedMDNode *NMD = NMI;
897 ++NMI;
898 if (NMD->getName().startswith("llvm.dbg.")) {
899 NMD->eraseFromParent();
900 Changed = true;
901 }
902 }
903
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000904 for (Function &F : M)
905 Changed |= stripDebugInfo(F);
906
907 if ( GVMaterializer *Materializer = M.getMaterializer())
908 Materializer->setStripDebugInfo();
Manman Rencb14bbc2013-11-22 22:06:31 +0000909
910 return Changed;
911}
Manman Ren8b4306c2013-12-02 21:29:56 +0000912
Manman Renbd4daf82013-12-03 00:12:14 +0000913unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
David Majnemere7a9cdb2015-02-16 06:04:53 +0000914 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000915 M.getModuleFlag("Debug Info Version")))
916 return Val->getZExtValue();
917 return 0;
Manman Ren8b4306c2013-12-02 21:29:56 +0000918}
David Blaikie6876b3b2014-07-01 20:05:26 +0000919
David Blaikiea8c35092014-07-02 18:30:05 +0000920llvm::DenseMap<const llvm::Function *, llvm::DISubprogram>
921llvm::makeSubprogramMap(const Module &M) {
922 DenseMap<const Function *, DISubprogram> R;
David Blaikie6876b3b2014-07-01 20:05:26 +0000923
924 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
925 if (!CU_Nodes)
926 return R;
927
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000928 for (MDNode *N : CU_Nodes->operands()) {
929 DICompileUnit CUNode(N);
David Blaikie6876b3b2014-07-01 20:05:26 +0000930 DIArray SPs = CUNode.getSubprograms();
931 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
932 DISubprogram SP(SPs.getElement(i));
933 if (Function *F = SP.getFunction())
934 R.insert(std::make_pair(F, SP));
935 }
936 }
937 return R;
938}