blob: 9381a5b580e50b2a081d4e1e23228da27d6df479 [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
Duncan P. N. Exon Smith85866b2a2015-03-31 01:28:58 +0000255bool DIType::Verify() const { return isType(); }
Duncan P. N. Exon Smithcd07efa12015-03-31 00:47:15 +0000256bool DIBasicType::Verify() const { return isBasicType(); }
257bool DIDerivedType::Verify() const { return isDerivedType(); }
Duncan P. N. Exon Smith85866b2a2015-03-31 01:28:58 +0000258bool DICompositeType::Verify() const { return isCompositeType(); }
Bill Wendling523bea82013-11-08 08:13:15 +0000259
Bill Wendling523bea82013-11-08 08:13:15 +0000260bool DISubprogram::Verify() const {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000261 auto *N = dyn_cast_or_null<MDSubprogram>(DbgNode);
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000262 if (!N)
Bill Wendling523bea82013-11-08 08:13:15 +0000263 return false;
264
David Blaikie3dfe4782014-10-14 18:22:52 +0000265 // If a DISubprogram has an llvm::Function*, then scope chains from all
266 // instructions within the function should lead to this DISubprogram.
267 if (auto *F = getFunction()) {
David Blaikie3dfe4782014-10-14 18:22:52 +0000268 for (auto &BB : *F) {
269 for (auto &I : BB) {
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +0000270 MDLocation *DL = I.getDebugLoc();
Duncan P. N. Exon Smith215e7ed2015-03-30 17:06:38 +0000271 if (!DL)
David Blaikie3dfe4782014-10-14 18:22:52 +0000272 continue;
273
David Blaikie3dfe4782014-10-14 18:22:52 +0000274 // walk the inlined-at scopes
Duncan P. N. Exon Smith8f7bc792015-03-30 17:41:24 +0000275 MDScope *Scope = DL->getInlinedAtScope();
Adrian Prantlde200df2015-01-20 22:37:25 +0000276 if (!Scope)
277 return false;
Duncan P. N. Exon Smith215e7ed2015-03-30 17:06:38 +0000278 while (!isa<MDSubprogram>(Scope)) {
279 Scope = cast<MDLexicalBlockBase>(Scope)->getScope();
Adrian Prantl1292e242015-01-21 18:32:56 +0000280 if (!Scope)
281 return false;
David Blaikie3dfe4782014-10-14 18:22:52 +0000282 }
283 if (!DISubprogram(Scope).describes(F))
284 return false;
285 }
286 }
287 }
Bill Wendling523bea82013-11-08 08:13:15 +0000288
Adrian Prantl9260cca2015-01-22 00:00:52 +0000289 return true;
Bill Wendling523bea82013-11-08 08:13:15 +0000290}
291
Duncan P. N. Exon Smith94d58f82015-03-31 01:28:22 +0000292bool DIGlobalVariable::Verify() const { return isGlobalVariable(); }
293bool DIVariable::Verify() const { return isVariable(); }
Bill Wendling523bea82013-11-08 08:13:15 +0000294
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000295bool DILocation::Verify() const {
296 return dyn_cast_or_null<MDLocation>(DbgNode);
297}
298bool DINameSpace::Verify() const {
299 return dyn_cast_or_null<MDNamespace>(DbgNode);
300}
301bool DIFile::Verify() const { return dyn_cast_or_null<MDFile>(DbgNode); }
302bool DIEnumerator::Verify() const {
303 return dyn_cast_or_null<MDEnumerator>(DbgNode);
304}
305bool DISubrange::Verify() const {
306 return dyn_cast_or_null<MDSubrange>(DbgNode);
307}
308bool DILexicalBlock::Verify() const {
309 return dyn_cast_or_null<MDLexicalBlock>(DbgNode);
310}
311bool DILexicalBlockFile::Verify() const {
312 return dyn_cast_or_null<MDLexicalBlockFile>(DbgNode);
313}
314bool DITemplateTypeParameter::Verify() const {
315 return dyn_cast_or_null<MDTemplateTypeParameter>(DbgNode);
316}
317bool DITemplateValueParameter::Verify() const {
318 return dyn_cast_or_null<MDTemplateValueParameter>(DbgNode);
319}
320bool DIImportedEntity::Verify() const {
321 return dyn_cast_or_null<MDImportedEntity>(DbgNode);
322}
Bill Wendling523bea82013-11-08 08:13:15 +0000323
Manman Ren1a125c92014-07-28 19:33:20 +0000324void DICompositeType::setArraysHelper(MDNode *Elements, MDNode *TParams) {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000325 TypedTrackingMDRef<MDCompositeTypeBase> N(get());
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000326 if (Elements)
327 N->replaceElements(cast<MDTuple>(Elements));
Bill Wendling523bea82013-11-08 08:13:15 +0000328 if (TParams)
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000329 N->replaceTemplateParams(cast<MDTuple>(TParams));
Bill Wendling523bea82013-11-08 08:13:15 +0000330 DbgNode = N;
331}
332
Bill Wendling523bea82013-11-08 08:13:15 +0000333DIScopeRef DIScope::getRef() const {
334 if (!isCompositeType())
335 return DIScopeRef(*this);
336 DICompositeType DTy(DbgNode);
337 if (!DTy.getIdentifier())
338 return DIScopeRef(*this);
339 return DIScopeRef(DTy.getIdentifier());
340}
341
Bill Wendling523bea82013-11-08 08:13:15 +0000342void DICompositeType::setContainingType(DICompositeType ContainingType) {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000343 TypedTrackingMDRef<MDCompositeTypeBase> N(get());
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000344 N->replaceVTableHolder(ContainingType.getRef());
Bill Wendling523bea82013-11-08 08:13:15 +0000345 DbgNode = N;
346}
347
Bill Wendling523bea82013-11-08 08:13:15 +0000348bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
349 assert(CurFn && "Invalid function");
350 if (!getContext().isSubprogram())
351 return false;
352 // This variable is not inlined function argument if its scope
353 // does not describe current function.
354 return !DISubprogram(getContext()).describes(CurFn);
355}
356
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000357Function *DISubprogram::getFunction() const {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000358 if (auto *N = get())
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000359 if (auto *C = dyn_cast_or_null<ConstantAsMetadata>(N->getFunction()))
360 return dyn_cast<Function>(C->getValue());
361 return nullptr;
362}
363
Bill Wendling523bea82013-11-08 08:13:15 +0000364bool DISubprogram::describes(const Function *F) {
365 assert(F && "Invalid function");
366 if (F == getFunction())
367 return true;
368 StringRef Name = getLinkageName();
369 if (Name.empty())
370 Name = getName();
371 if (F->getName() == Name)
372 return true;
373 return false;
374}
375
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000376GlobalVariable *DIGlobalVariable::getGlobal() const {
377 return dyn_cast_or_null<GlobalVariable>(getConstant());
Bill Wendling523bea82013-11-08 08:13:15 +0000378}
379
Bill Wendling523bea82013-11-08 08:13:15 +0000380DIScopeRef DIScope::getContext() const {
381
382 if (isType())
383 return DIType(DbgNode).getContext();
384
385 if (isSubprogram())
386 return DIScopeRef(DISubprogram(DbgNode).getContext());
387
388 if (isLexicalBlock())
389 return DIScopeRef(DILexicalBlock(DbgNode).getContext());
390
391 if (isLexicalBlockFile())
392 return DIScopeRef(DILexicalBlockFile(DbgNode).getContext());
393
394 if (isNameSpace())
395 return DIScopeRef(DINameSpace(DbgNode).getContext());
396
397 assert((isFile() || isCompileUnit()) && "Unhandled type of scope.");
Craig Topperc6207612014-04-09 06:08:46 +0000398 return DIScopeRef(nullptr);
Bill Wendling523bea82013-11-08 08:13:15 +0000399}
400
Bill Wendling523bea82013-11-08 08:13:15 +0000401StringRef DIScope::getName() const {
402 if (isType())
403 return DIType(DbgNode).getName();
404 if (isSubprogram())
405 return DISubprogram(DbgNode).getName();
406 if (isNameSpace())
407 return DINameSpace(DbgNode).getName();
408 assert((isLexicalBlock() || isLexicalBlockFile() || isFile() ||
409 isCompileUnit()) &&
410 "Unhandled type of scope.");
411 return StringRef();
412}
413
414StringRef DIScope::getFilename() const {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000415 if (auto *N = get())
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000416 return ::getStringField(dyn_cast_or_null<MDNode>(N->getFile()), 0);
417 return "";
Bill Wendling523bea82013-11-08 08:13:15 +0000418}
419
420StringRef DIScope::getDirectory() const {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000421 if (auto *N = get())
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000422 return ::getStringField(dyn_cast_or_null<MDNode>(N->getFile()), 1);
423 return "";
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000424}
425
426void DICompileUnit::replaceSubprograms(DIArray Subprograms) {
427 assert(Verify() && "Expected compile unit");
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000428 get()->replaceSubprograms(cast_or_null<MDTuple>(Subprograms.get()));
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000429}
430
431void DICompileUnit::replaceGlobalVariables(DIArray GlobalVariables) {
432 assert(Verify() && "Expected compile unit");
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000433 get()->replaceGlobalVariables(cast_or_null<MDTuple>(GlobalVariables.get()));
Bill Wendling523bea82013-11-08 08:13:15 +0000434}
435
Diego Novillof5041ce2014-03-03 20:06:11 +0000436DILocation DILocation::copyWithNewScope(LLVMContext &Ctx,
David Blaikie2f3f76f2014-08-21 22:45:21 +0000437 DILexicalBlockFile NewScope) {
Diego Novillof5041ce2014-03-03 20:06:11 +0000438 assert(Verify());
Duncan P. N. Exon Smith98854692015-01-14 22:27:36 +0000439 assert(NewScope && "Expected valid scope");
440
441 const auto *Old = cast<MDLocation>(DbgNode);
442 return DILocation(MDLocation::get(Ctx, Old->getLine(), Old->getColumn(),
443 NewScope, Old->getInlinedAt()));
Diego Novillof5041ce2014-03-03 20:06:11 +0000444}
445
Diego Novillof5041ce2014-03-03 20:06:11 +0000446unsigned DILocation::computeNewDiscriminator(LLVMContext &Ctx) {
447 std::pair<const char *, unsigned> Key(getFilename().data(), getLineNumber());
448 return ++Ctx.pImpl->DiscriminatorTable[Key];
449}
450
Bill Wendling523bea82013-11-08 08:13:15 +0000451DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
452 LLVMContext &VMContext) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000453 assert(DIVariable(DV).Verify() && "Expected a DIVariable");
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000454 return cast<MDLocalVariable>(DV)
455 ->withInline(cast_or_null<MDLocation>(InlinedScope));
Bill Wendling523bea82013-11-08 08:13:15 +0000456}
457
Bill Wendling523bea82013-11-08 08:13:15 +0000458DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000459 assert(DIVariable(DV).Verify() && "Expected a DIVariable");
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000460 return cast<MDLocalVariable>(DV)->withoutInline();
Bill Wendling523bea82013-11-08 08:13:15 +0000461}
462
Bill Wendling523bea82013-11-08 08:13:15 +0000463DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
464 DIDescriptor D(Scope);
465 if (D.isSubprogram())
466 return DISubprogram(Scope);
467
468 if (D.isLexicalBlockFile())
469 return getDISubprogram(DILexicalBlockFile(Scope).getContext());
470
471 if (D.isLexicalBlock())
472 return getDISubprogram(DILexicalBlock(Scope).getContext());
473
474 return DISubprogram();
475}
476
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000477DISubprogram llvm::getDISubprogram(const Function *F) {
478 // We look for the first instr that has a debug annotation leading back to F.
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000479 for (auto &BB : *F) {
David Majnemerc758df42014-11-01 07:57:14 +0000480 auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) {
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +0000481 return Inst.getDebugLoc();
David Majnemerc758df42014-11-01 07:57:14 +0000482 });
483 if (Inst == BB.end())
484 continue;
485 DebugLoc DLoc = Inst->getDebugLoc();
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +0000486 const MDNode *Scope = DLoc.getInlinedAtScope();
David Majnemerc758df42014-11-01 07:57:14 +0000487 DISubprogram Subprogram = getDISubprogram(Scope);
488 return Subprogram.describes(F) ? Subprogram : DISubprogram();
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000489 }
490
491 return DISubprogram();
492}
493
Bill Wendling523bea82013-11-08 08:13:15 +0000494DICompositeType llvm::getDICompositeType(DIType T) {
495 if (T.isCompositeType())
496 return DICompositeType(T);
497
498 if (T.isDerivedType()) {
499 // This function is currently used by dragonegg and dragonegg does
500 // not generate identifier for types, so using an empty map to resolve
501 // DerivedFrom should be fine.
502 DITypeIdentifierMap EmptyMap;
503 return getDICompositeType(
504 DIDerivedType(T).getTypeDerivedFrom().resolve(EmptyMap));
505 }
506
507 return DICompositeType();
508}
509
Bill Wendling523bea82013-11-08 08:13:15 +0000510DITypeIdentifierMap
511llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
512 DITypeIdentifierMap Map;
513 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000514 DICompileUnit CU(CU_Nodes->getOperand(CUi));
Bill Wendling523bea82013-11-08 08:13:15 +0000515 DIArray Retain = CU.getRetainedTypes();
516 for (unsigned Ti = 0, Te = Retain.getNumElements(); Ti != Te; ++Ti) {
517 if (!Retain.getElement(Ti).isCompositeType())
518 continue;
519 DICompositeType Ty(Retain.getElement(Ti));
520 if (MDString *TypeId = Ty.getIdentifier()) {
521 // Definition has priority over declaration.
522 // Try to insert (TypeId, Ty) to Map.
523 std::pair<DITypeIdentifierMap::iterator, bool> P =
524 Map.insert(std::make_pair(TypeId, Ty));
525 // If TypeId already exists in Map and this is a definition, replace
526 // whatever we had (declaration or definition) with the definition.
527 if (!P.second && !Ty.isForwardDecl())
528 P.first->second = Ty;
529 }
530 }
531 }
532 return Map;
533}
534
535//===----------------------------------------------------------------------===//
536// DebugInfoFinder implementations.
537//===----------------------------------------------------------------------===//
538
539void DebugInfoFinder::reset() {
540 CUs.clear();
541 SPs.clear();
542 GVs.clear();
543 TYs.clear();
544 Scopes.clear();
545 NodesSeen.clear();
546 TypeIdentifierMap.clear();
Manman Ren2085ccc2013-11-17 18:42:37 +0000547 TypeMapInitialized = false;
548}
549
Manman Renb46e5502013-11-17 19:35:03 +0000550void DebugInfoFinder::InitializeTypeMap(const Module &M) {
Manman Ren2085ccc2013-11-17 18:42:37 +0000551 if (!TypeMapInitialized)
552 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
553 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
554 TypeMapInitialized = true;
555 }
Bill Wendling523bea82013-11-08 08:13:15 +0000556}
557
Bill Wendling523bea82013-11-08 08:13:15 +0000558void DebugInfoFinder::processModule(const Module &M) {
Manman Renb46e5502013-11-17 19:35:03 +0000559 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000560 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
Bill Wendling523bea82013-11-08 08:13:15 +0000561 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000562 DICompileUnit CU(CU_Nodes->getOperand(i));
Bill Wendling523bea82013-11-08 08:13:15 +0000563 addCompileUnit(CU);
564 DIArray GVs = CU.getGlobalVariables();
565 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
566 DIGlobalVariable DIG(GVs.getElement(i));
567 if (addGlobalVariable(DIG)) {
Manman Renf0a582b2014-11-21 19:55:23 +0000568 processScope(DIG.getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000569 processType(DIG.getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000570 }
571 }
572 DIArray SPs = CU.getSubprograms();
573 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
574 processSubprogram(DISubprogram(SPs.getElement(i)));
575 DIArray EnumTypes = CU.getEnumTypes();
576 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
577 processType(DIType(EnumTypes.getElement(i)));
578 DIArray RetainedTypes = CU.getRetainedTypes();
579 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
580 processType(DIType(RetainedTypes.getElement(i)));
581 DIArray Imports = CU.getImportedEntities();
582 for (unsigned i = 0, e = Imports.getNumElements(); i != e; ++i) {
583 DIImportedEntity Import = DIImportedEntity(Imports.getElement(i));
Duncan P. N. Exon Smithd4e07c92015-03-20 19:13:53 +0000584 if (!Import)
585 continue;
Adrian Prantld09ba232014-04-01 03:41:04 +0000586 DIDescriptor Entity = Import.getEntity().resolve(TypeIdentifierMap);
Bill Wendling523bea82013-11-08 08:13:15 +0000587 if (Entity.isType())
588 processType(DIType(Entity));
589 else if (Entity.isSubprogram())
590 processSubprogram(DISubprogram(Entity));
591 else if (Entity.isNameSpace())
592 processScope(DINameSpace(Entity).getContext());
593 }
594 }
595 }
596}
597
Manman Ren2085ccc2013-11-17 18:42:37 +0000598void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) {
Bill Wendling523bea82013-11-08 08:13:15 +0000599 if (!Loc)
600 return;
Manman Renb46e5502013-11-17 19:35:03 +0000601 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000602 processScope(Loc.getScope());
Manman Ren2085ccc2013-11-17 18:42:37 +0000603 processLocation(M, Loc.getOrigLocation());
Bill Wendling523bea82013-11-08 08:13:15 +0000604}
605
Bill Wendling523bea82013-11-08 08:13:15 +0000606void DebugInfoFinder::processType(DIType DT) {
607 if (!addType(DT))
608 return;
609 processScope(DT.getContext().resolve(TypeIdentifierMap));
610 if (DT.isCompositeType()) {
611 DICompositeType DCT(DT);
612 processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
Manman Renf8a19672014-07-28 22:24:06 +0000613 if (DT.isSubroutineType()) {
614 DITypeArray DTA = DISubroutineType(DT).getTypeArray();
615 for (unsigned i = 0, e = DTA.getNumElements(); i != e; ++i)
616 processType(DTA.getElement(i).resolve(TypeIdentifierMap));
617 return;
618 }
Manman Renab8ffba2014-07-28 19:14:13 +0000619 DIArray DA = DCT.getElements();
Bill Wendling523bea82013-11-08 08:13:15 +0000620 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
621 DIDescriptor D = DA.getElement(i);
622 if (D.isType())
623 processType(DIType(D));
624 else if (D.isSubprogram())
625 processSubprogram(DISubprogram(D));
626 }
627 } else if (DT.isDerivedType()) {
628 DIDerivedType DDT(DT);
629 processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
630 }
631}
632
633void DebugInfoFinder::processScope(DIScope Scope) {
634 if (Scope.isType()) {
635 DIType Ty(Scope);
636 processType(Ty);
637 return;
638 }
639 if (Scope.isCompileUnit()) {
640 addCompileUnit(DICompileUnit(Scope));
641 return;
642 }
643 if (Scope.isSubprogram()) {
644 processSubprogram(DISubprogram(Scope));
645 return;
646 }
647 if (!addScope(Scope))
648 return;
649 if (Scope.isLexicalBlock()) {
650 DILexicalBlock LB(Scope);
651 processScope(LB.getContext());
652 } else if (Scope.isLexicalBlockFile()) {
653 DILexicalBlockFile LBF = DILexicalBlockFile(Scope);
654 processScope(LBF.getScope());
655 } else if (Scope.isNameSpace()) {
656 DINameSpace NS(Scope);
657 processScope(NS.getContext());
658 }
659}
660
Bill Wendling523bea82013-11-08 08:13:15 +0000661void DebugInfoFinder::processSubprogram(DISubprogram SP) {
662 if (!addSubprogram(SP))
663 return;
664 processScope(SP.getContext().resolve(TypeIdentifierMap));
665 processType(SP.getType());
666 DIArray TParams = SP.getTemplateParams();
667 for (unsigned I = 0, E = TParams.getNumElements(); I != E; ++I) {
668 DIDescriptor Element = TParams.getElement(I);
669 if (Element.isTemplateTypeParameter()) {
670 DITemplateTypeParameter TType(Element);
Bill Wendling523bea82013-11-08 08:13:15 +0000671 processType(TType.getType().resolve(TypeIdentifierMap));
672 } else if (Element.isTemplateValueParameter()) {
673 DITemplateValueParameter TVal(Element);
Bill Wendling523bea82013-11-08 08:13:15 +0000674 processType(TVal.getType().resolve(TypeIdentifierMap));
675 }
676 }
677}
678
Manman Ren2085ccc2013-11-17 18:42:37 +0000679void DebugInfoFinder::processDeclare(const Module &M,
680 const DbgDeclareInst *DDI) {
Bill Wendling523bea82013-11-08 08:13:15 +0000681 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
682 if (!N)
683 return;
Manman Renb46e5502013-11-17 19:35:03 +0000684 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000685
686 DIDescriptor DV(N);
687 if (!DV.isVariable())
688 return;
689
David Blaikie70573dc2014-11-19 07:49:26 +0000690 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000691 return;
692 processScope(DIVariable(N).getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000693 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000694}
695
Manman Ren2085ccc2013-11-17 18:42:37 +0000696void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
Bill Wendling523bea82013-11-08 08:13:15 +0000697 MDNode *N = dyn_cast<MDNode>(DVI->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
Bill Wendling523bea82013-11-08 08:13:15 +0000712bool DebugInfoFinder::addType(DIType DT) {
713 if (!DT)
714 return false;
715
David Blaikie70573dc2014-11-19 07:49:26 +0000716 if (!NodesSeen.insert(DT).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000717 return false;
718
719 TYs.push_back(DT);
720 return true;
721}
722
Bill Wendling523bea82013-11-08 08:13:15 +0000723bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
724 if (!CU)
725 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000726 if (!NodesSeen.insert(CU).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000727 return false;
728
729 CUs.push_back(CU);
730 return true;
731}
732
Bill Wendling523bea82013-11-08 08:13:15 +0000733bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
734 if (!DIG)
735 return false;
736
David Blaikie70573dc2014-11-19 07:49:26 +0000737 if (!NodesSeen.insert(DIG).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000738 return false;
739
740 GVs.push_back(DIG);
741 return true;
742}
743
Bill Wendling523bea82013-11-08 08:13:15 +0000744bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
745 if (!SP)
746 return false;
747
David Blaikie70573dc2014-11-19 07:49:26 +0000748 if (!NodesSeen.insert(SP).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000749 return false;
750
751 SPs.push_back(SP);
752 return true;
753}
754
755bool DebugInfoFinder::addScope(DIScope Scope) {
756 if (!Scope)
757 return false;
758 // FIXME: Ocaml binding generates a scope with no content, we treat it
759 // as null for now.
760 if (Scope->getNumOperands() == 0)
761 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000762 if (!NodesSeen.insert(Scope).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000763 return false;
764 Scopes.push_back(Scope);
765 return true;
766}
767
768//===----------------------------------------------------------------------===//
769// DIDescriptor: dump routines for all descriptors.
770//===----------------------------------------------------------------------===//
771
Bill Wendling523bea82013-11-08 08:13:15 +0000772void DIDescriptor::dump() const {
773 print(dbgs());
774 dbgs() << '\n';
775}
776
Bill Wendling523bea82013-11-08 08:13:15 +0000777void DIDescriptor::print(raw_ostream &OS) const {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000778 if (!get())
Bill Wendling523bea82013-11-08 08:13:15 +0000779 return;
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000780 get()->print(OS);
Bill Wendling523bea82013-11-08 08:13:15 +0000781}
782
783static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
784 const LLVMContext &Ctx) {
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +0000785 if (!DL)
Duncan P. N. Exon Smith51306ef2015-03-30 18:45:11 +0000786 return;
787
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +0000788 DIScope Scope(DL.getScope());
Duncan P. N. Exon Smith51306ef2015-03-30 18:45:11 +0000789 assert(Scope.isScope() && "Scope of a DebugLoc should be a DIScope.");
790 // Omit the directory, because it's likely to be long and uninteresting.
791 CommentOS << Scope.getFilename();
792 CommentOS << ':' << DL.getLine();
793 if (DL.getCol() != 0)
794 CommentOS << ':' << DL.getCol();
795
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +0000796 DebugLoc InlinedAtDL = DL.getInlinedAt();
797 if (!InlinedAtDL)
Duncan P. N. Exon Smith51306ef2015-03-30 18:45:11 +0000798 return;
799
800 CommentOS << " @[ ";
801 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
802 CommentOS << " ]";
Bill Wendling523bea82013-11-08 08:13:15 +0000803}
804
805void DIVariable::printExtendedName(raw_ostream &OS) const {
806 const LLVMContext &Ctx = DbgNode->getContext();
807 StringRef Res = getName();
808 if (!Res.empty())
809 OS << Res << "," << getLineNumber();
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +0000810 if (auto *InlinedAt = get()->getInlinedAt()) {
811 if (DebugLoc InlinedAtDL = InlinedAt) {
Bill Wendling523bea82013-11-08 08:13:15 +0000812 OS << " @[";
813 printDebugLoc(InlinedAtDL, OS, Ctx);
814 OS << "]";
815 }
816 }
817}
818
Duncan P. N. Exon Smith2a78e9b2015-02-18 19:39:36 +0000819template <> DIRef<DIDescriptor>::DIRef(const Metadata *V) : Val(V) {
820 assert(isDescriptorRef(V) &&
821 "DIDescriptorRef should be a MDString or MDNode");
822}
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000823template <> DIRef<DIScope>::DIRef(const Metadata *V) : Val(V) {
Bill Wendling523bea82013-11-08 08:13:15 +0000824 assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode");
825}
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000826template <> DIRef<DIType>::DIRef(const Metadata *V) : Val(V) {
Bill Wendling523bea82013-11-08 08:13:15 +0000827 assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode");
828}
829
Bill Wendling523bea82013-11-08 08:13:15 +0000830template <>
Duncan P. N. Exon Smith2a78e9b2015-02-18 19:39:36 +0000831DIDescriptorRef DIDescriptor::getFieldAs<DIDescriptorRef>(unsigned Elt) const {
832 return DIDescriptorRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
833}
834template <>
Bill Wendling523bea82013-11-08 08:13:15 +0000835DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000836 return DIScopeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +0000837}
Bill Wendling523bea82013-11-08 08:13:15 +0000838template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000839 return DITypeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +0000840}
Manman Rencb14bbc2013-11-22 22:06:31 +0000841
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000842bool llvm::stripDebugInfo(Function &F) {
843 bool Changed = false;
844 for (BasicBlock &BB : F) {
845 for (Instruction &I : BB) {
846 if (I.getDebugLoc()) {
847 Changed = true;
848 I.setDebugLoc(DebugLoc());
849 }
850 }
851 }
852 return Changed;
853}
854
Manman Rencb14bbc2013-11-22 22:06:31 +0000855bool llvm::StripDebugInfo(Module &M) {
Manman Rencb14bbc2013-11-22 22:06:31 +0000856 bool Changed = false;
857
858 // Remove all of the calls to the debugger intrinsics, and remove them from
859 // the module.
860 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
861 while (!Declare->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000862 CallInst *CI = cast<CallInst>(Declare->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +0000863 CI->eraseFromParent();
864 }
865 Declare->eraseFromParent();
866 Changed = true;
867 }
868
869 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
870 while (!DbgVal->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000871 CallInst *CI = cast<CallInst>(DbgVal->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +0000872 CI->eraseFromParent();
873 }
874 DbgVal->eraseFromParent();
875 Changed = true;
876 }
877
878 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
879 NME = M.named_metadata_end(); NMI != NME;) {
880 NamedMDNode *NMD = NMI;
881 ++NMI;
882 if (NMD->getName().startswith("llvm.dbg.")) {
883 NMD->eraseFromParent();
884 Changed = true;
885 }
886 }
887
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000888 for (Function &F : M)
889 Changed |= stripDebugInfo(F);
890
891 if ( GVMaterializer *Materializer = M.getMaterializer())
892 Materializer->setStripDebugInfo();
Manman Rencb14bbc2013-11-22 22:06:31 +0000893
894 return Changed;
895}
Manman Ren8b4306c2013-12-02 21:29:56 +0000896
Manman Renbd4daf82013-12-03 00:12:14 +0000897unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
David Majnemere7a9cdb2015-02-16 06:04:53 +0000898 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000899 M.getModuleFlag("Debug Info Version")))
900 return Val->getZExtValue();
901 return 0;
Manman Ren8b4306c2013-12-02 21:29:56 +0000902}
David Blaikie6876b3b2014-07-01 20:05:26 +0000903
David Blaikiea8c35092014-07-02 18:30:05 +0000904llvm::DenseMap<const llvm::Function *, llvm::DISubprogram>
905llvm::makeSubprogramMap(const Module &M) {
906 DenseMap<const Function *, DISubprogram> R;
David Blaikie6876b3b2014-07-01 20:05:26 +0000907
908 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
909 if (!CU_Nodes)
910 return R;
911
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000912 for (MDNode *N : CU_Nodes->operands()) {
913 DICompileUnit CUNode(N);
David Blaikie6876b3b2014-07-01 20:05:26 +0000914 DIArray SPs = CUNode.getSubprograms();
915 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
916 DISubprogram SP(SPs.getElement(i));
917 if (Function *F = SP.getFunction())
918 R.insert(std::make_pair(F, SP));
919 }
920 }
921 return R;
922}