blob: d841e3ca9c6377ae2f2a60d37ab14d5ecde8bce1 [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 Smith3b960c92015-03-31 01:47:55 +0000226#ifndef NDEBUG
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000227/// \brief Check if a value can be a reference to a type.
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000228static bool isTypeRef(const Metadata *MD) {
229 if (!MD)
230 return true;
231 if (auto *S = dyn_cast<MDString>(MD))
232 return !S->getString().empty();
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000233 return isa<MDType>(MD);
Bill Wendling523bea82013-11-08 08:13:15 +0000234}
235
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000236/// \brief Check if a value can be a ScopeRef.
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000237static bool isScopeRef(const Metadata *MD) {
238 if (!MD)
239 return true;
240 if (auto *S = dyn_cast<MDString>(MD))
241 return !S->getString().empty();
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000242 return isa<MDScope>(MD);
Bill Wendling523bea82013-11-08 08:13:15 +0000243}
244
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) {
Duncan P. N. Exon Smithdd77af82015-03-31 02:06:28 +0000464 if (auto *LocalScope = dyn_cast_or_null<MDLocalScope>(Scope))
465 return LocalScope->getSubprogram();
466 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +0000467}
468
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000469DISubprogram llvm::getDISubprogram(const Function *F) {
470 // We look for the first instr that has a debug annotation leading back to F.
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000471 for (auto &BB : *F) {
David Majnemerc758df42014-11-01 07:57:14 +0000472 auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) {
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +0000473 return Inst.getDebugLoc();
David Majnemerc758df42014-11-01 07:57:14 +0000474 });
475 if (Inst == BB.end())
476 continue;
477 DebugLoc DLoc = Inst->getDebugLoc();
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +0000478 const MDNode *Scope = DLoc.getInlinedAtScope();
David Majnemerc758df42014-11-01 07:57:14 +0000479 DISubprogram Subprogram = getDISubprogram(Scope);
480 return Subprogram.describes(F) ? Subprogram : DISubprogram();
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000481 }
482
483 return DISubprogram();
484}
485
Bill Wendling523bea82013-11-08 08:13:15 +0000486DICompositeType llvm::getDICompositeType(DIType T) {
487 if (T.isCompositeType())
488 return DICompositeType(T);
489
490 if (T.isDerivedType()) {
491 // This function is currently used by dragonegg and dragonegg does
492 // not generate identifier for types, so using an empty map to resolve
493 // DerivedFrom should be fine.
494 DITypeIdentifierMap EmptyMap;
495 return getDICompositeType(
496 DIDerivedType(T).getTypeDerivedFrom().resolve(EmptyMap));
497 }
498
499 return DICompositeType();
500}
501
Bill Wendling523bea82013-11-08 08:13:15 +0000502DITypeIdentifierMap
503llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
504 DITypeIdentifierMap Map;
505 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000506 DICompileUnit CU(CU_Nodes->getOperand(CUi));
Bill Wendling523bea82013-11-08 08:13:15 +0000507 DIArray Retain = CU.getRetainedTypes();
508 for (unsigned Ti = 0, Te = Retain.getNumElements(); Ti != Te; ++Ti) {
509 if (!Retain.getElement(Ti).isCompositeType())
510 continue;
511 DICompositeType Ty(Retain.getElement(Ti));
512 if (MDString *TypeId = Ty.getIdentifier()) {
513 // Definition has priority over declaration.
514 // Try to insert (TypeId, Ty) to Map.
515 std::pair<DITypeIdentifierMap::iterator, bool> P =
516 Map.insert(std::make_pair(TypeId, Ty));
517 // If TypeId already exists in Map and this is a definition, replace
518 // whatever we had (declaration or definition) with the definition.
519 if (!P.second && !Ty.isForwardDecl())
520 P.first->second = Ty;
521 }
522 }
523 }
524 return Map;
525}
526
527//===----------------------------------------------------------------------===//
528// DebugInfoFinder implementations.
529//===----------------------------------------------------------------------===//
530
531void DebugInfoFinder::reset() {
532 CUs.clear();
533 SPs.clear();
534 GVs.clear();
535 TYs.clear();
536 Scopes.clear();
537 NodesSeen.clear();
538 TypeIdentifierMap.clear();
Manman Ren2085ccc2013-11-17 18:42:37 +0000539 TypeMapInitialized = false;
540}
541
Manman Renb46e5502013-11-17 19:35:03 +0000542void DebugInfoFinder::InitializeTypeMap(const Module &M) {
Manman Ren2085ccc2013-11-17 18:42:37 +0000543 if (!TypeMapInitialized)
544 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
545 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
546 TypeMapInitialized = true;
547 }
Bill Wendling523bea82013-11-08 08:13:15 +0000548}
549
Bill Wendling523bea82013-11-08 08:13:15 +0000550void DebugInfoFinder::processModule(const Module &M) {
Manman Renb46e5502013-11-17 19:35:03 +0000551 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000552 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
Bill Wendling523bea82013-11-08 08:13:15 +0000553 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000554 DICompileUnit CU(CU_Nodes->getOperand(i));
Bill Wendling523bea82013-11-08 08:13:15 +0000555 addCompileUnit(CU);
556 DIArray GVs = CU.getGlobalVariables();
557 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
558 DIGlobalVariable DIG(GVs.getElement(i));
559 if (addGlobalVariable(DIG)) {
Manman Renf0a582b2014-11-21 19:55:23 +0000560 processScope(DIG.getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000561 processType(DIG.getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000562 }
563 }
564 DIArray SPs = CU.getSubprograms();
565 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
566 processSubprogram(DISubprogram(SPs.getElement(i)));
567 DIArray EnumTypes = CU.getEnumTypes();
568 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
569 processType(DIType(EnumTypes.getElement(i)));
570 DIArray RetainedTypes = CU.getRetainedTypes();
571 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
572 processType(DIType(RetainedTypes.getElement(i)));
573 DIArray Imports = CU.getImportedEntities();
574 for (unsigned i = 0, e = Imports.getNumElements(); i != e; ++i) {
575 DIImportedEntity Import = DIImportedEntity(Imports.getElement(i));
Duncan P. N. Exon Smithd4e07c92015-03-20 19:13:53 +0000576 if (!Import)
577 continue;
Adrian Prantld09ba232014-04-01 03:41:04 +0000578 DIDescriptor Entity = Import.getEntity().resolve(TypeIdentifierMap);
Bill Wendling523bea82013-11-08 08:13:15 +0000579 if (Entity.isType())
580 processType(DIType(Entity));
581 else if (Entity.isSubprogram())
582 processSubprogram(DISubprogram(Entity));
583 else if (Entity.isNameSpace())
584 processScope(DINameSpace(Entity).getContext());
585 }
586 }
587 }
588}
589
Manman Ren2085ccc2013-11-17 18:42:37 +0000590void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) {
Bill Wendling523bea82013-11-08 08:13:15 +0000591 if (!Loc)
592 return;
Manman Renb46e5502013-11-17 19:35:03 +0000593 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000594 processScope(Loc.getScope());
Manman Ren2085ccc2013-11-17 18:42:37 +0000595 processLocation(M, Loc.getOrigLocation());
Bill Wendling523bea82013-11-08 08:13:15 +0000596}
597
Bill Wendling523bea82013-11-08 08:13:15 +0000598void DebugInfoFinder::processType(DIType DT) {
599 if (!addType(DT))
600 return;
601 processScope(DT.getContext().resolve(TypeIdentifierMap));
602 if (DT.isCompositeType()) {
603 DICompositeType DCT(DT);
604 processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
Manman Renf8a19672014-07-28 22:24:06 +0000605 if (DT.isSubroutineType()) {
606 DITypeArray DTA = DISubroutineType(DT).getTypeArray();
607 for (unsigned i = 0, e = DTA.getNumElements(); i != e; ++i)
608 processType(DTA.getElement(i).resolve(TypeIdentifierMap));
609 return;
610 }
Manman Renab8ffba2014-07-28 19:14:13 +0000611 DIArray DA = DCT.getElements();
Bill Wendling523bea82013-11-08 08:13:15 +0000612 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
613 DIDescriptor D = DA.getElement(i);
614 if (D.isType())
615 processType(DIType(D));
616 else if (D.isSubprogram())
617 processSubprogram(DISubprogram(D));
618 }
619 } else if (DT.isDerivedType()) {
620 DIDerivedType DDT(DT);
621 processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
622 }
623}
624
625void DebugInfoFinder::processScope(DIScope Scope) {
626 if (Scope.isType()) {
627 DIType Ty(Scope);
628 processType(Ty);
629 return;
630 }
631 if (Scope.isCompileUnit()) {
632 addCompileUnit(DICompileUnit(Scope));
633 return;
634 }
635 if (Scope.isSubprogram()) {
636 processSubprogram(DISubprogram(Scope));
637 return;
638 }
639 if (!addScope(Scope))
640 return;
641 if (Scope.isLexicalBlock()) {
642 DILexicalBlock LB(Scope);
643 processScope(LB.getContext());
644 } else if (Scope.isLexicalBlockFile()) {
645 DILexicalBlockFile LBF = DILexicalBlockFile(Scope);
646 processScope(LBF.getScope());
647 } else if (Scope.isNameSpace()) {
648 DINameSpace NS(Scope);
649 processScope(NS.getContext());
650 }
651}
652
Bill Wendling523bea82013-11-08 08:13:15 +0000653void DebugInfoFinder::processSubprogram(DISubprogram SP) {
654 if (!addSubprogram(SP))
655 return;
656 processScope(SP.getContext().resolve(TypeIdentifierMap));
657 processType(SP.getType());
658 DIArray TParams = SP.getTemplateParams();
659 for (unsigned I = 0, E = TParams.getNumElements(); I != E; ++I) {
660 DIDescriptor Element = TParams.getElement(I);
661 if (Element.isTemplateTypeParameter()) {
662 DITemplateTypeParameter TType(Element);
Bill Wendling523bea82013-11-08 08:13:15 +0000663 processType(TType.getType().resolve(TypeIdentifierMap));
664 } else if (Element.isTemplateValueParameter()) {
665 DITemplateValueParameter TVal(Element);
Bill Wendling523bea82013-11-08 08:13:15 +0000666 processType(TVal.getType().resolve(TypeIdentifierMap));
667 }
668 }
669}
670
Manman Ren2085ccc2013-11-17 18:42:37 +0000671void DebugInfoFinder::processDeclare(const Module &M,
672 const DbgDeclareInst *DDI) {
Bill Wendling523bea82013-11-08 08:13:15 +0000673 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
674 if (!N)
675 return;
Manman Renb46e5502013-11-17 19:35:03 +0000676 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000677
678 DIDescriptor DV(N);
679 if (!DV.isVariable())
680 return;
681
David Blaikie70573dc2014-11-19 07:49:26 +0000682 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000683 return;
684 processScope(DIVariable(N).getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000685 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000686}
687
Manman Ren2085ccc2013-11-17 18:42:37 +0000688void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
Bill Wendling523bea82013-11-08 08:13:15 +0000689 MDNode *N = dyn_cast<MDNode>(DVI->getVariable());
690 if (!N)
691 return;
Manman Renb46e5502013-11-17 19:35:03 +0000692 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000693
694 DIDescriptor DV(N);
695 if (!DV.isVariable())
696 return;
697
David Blaikie70573dc2014-11-19 07:49:26 +0000698 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000699 return;
700 processScope(DIVariable(N).getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000701 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000702}
703
Bill Wendling523bea82013-11-08 08:13:15 +0000704bool DebugInfoFinder::addType(DIType DT) {
705 if (!DT)
706 return false;
707
David Blaikie70573dc2014-11-19 07:49:26 +0000708 if (!NodesSeen.insert(DT).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000709 return false;
710
711 TYs.push_back(DT);
712 return true;
713}
714
Bill Wendling523bea82013-11-08 08:13:15 +0000715bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
716 if (!CU)
717 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000718 if (!NodesSeen.insert(CU).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000719 return false;
720
721 CUs.push_back(CU);
722 return true;
723}
724
Bill Wendling523bea82013-11-08 08:13:15 +0000725bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
726 if (!DIG)
727 return false;
728
David Blaikie70573dc2014-11-19 07:49:26 +0000729 if (!NodesSeen.insert(DIG).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000730 return false;
731
732 GVs.push_back(DIG);
733 return true;
734}
735
Bill Wendling523bea82013-11-08 08:13:15 +0000736bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
737 if (!SP)
738 return false;
739
David Blaikie70573dc2014-11-19 07:49:26 +0000740 if (!NodesSeen.insert(SP).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000741 return false;
742
743 SPs.push_back(SP);
744 return true;
745}
746
747bool DebugInfoFinder::addScope(DIScope Scope) {
748 if (!Scope)
749 return false;
750 // FIXME: Ocaml binding generates a scope with no content, we treat it
751 // as null for now.
752 if (Scope->getNumOperands() == 0)
753 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000754 if (!NodesSeen.insert(Scope).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000755 return false;
756 Scopes.push_back(Scope);
757 return true;
758}
759
760//===----------------------------------------------------------------------===//
761// DIDescriptor: dump routines for all descriptors.
762//===----------------------------------------------------------------------===//
763
Bill Wendling523bea82013-11-08 08:13:15 +0000764void DIDescriptor::dump() const {
765 print(dbgs());
766 dbgs() << '\n';
767}
768
Bill Wendling523bea82013-11-08 08:13:15 +0000769void DIDescriptor::print(raw_ostream &OS) const {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000770 if (!get())
Bill Wendling523bea82013-11-08 08:13:15 +0000771 return;
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000772 get()->print(OS);
Bill Wendling523bea82013-11-08 08:13:15 +0000773}
774
775static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
776 const LLVMContext &Ctx) {
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +0000777 if (!DL)
Duncan P. N. Exon Smith51306ef2015-03-30 18:45:11 +0000778 return;
779
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +0000780 DIScope Scope(DL.getScope());
Duncan P. N. Exon Smith51306ef2015-03-30 18:45:11 +0000781 assert(Scope.isScope() && "Scope of a DebugLoc should be a DIScope.");
782 // Omit the directory, because it's likely to be long and uninteresting.
783 CommentOS << Scope.getFilename();
784 CommentOS << ':' << DL.getLine();
785 if (DL.getCol() != 0)
786 CommentOS << ':' << DL.getCol();
787
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +0000788 DebugLoc InlinedAtDL = DL.getInlinedAt();
789 if (!InlinedAtDL)
Duncan P. N. Exon Smith51306ef2015-03-30 18:45:11 +0000790 return;
791
792 CommentOS << " @[ ";
793 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
794 CommentOS << " ]";
Bill Wendling523bea82013-11-08 08:13:15 +0000795}
796
797void DIVariable::printExtendedName(raw_ostream &OS) const {
798 const LLVMContext &Ctx = DbgNode->getContext();
799 StringRef Res = getName();
800 if (!Res.empty())
801 OS << Res << "," << getLineNumber();
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +0000802 if (auto *InlinedAt = get()->getInlinedAt()) {
803 if (DebugLoc InlinedAtDL = InlinedAt) {
Bill Wendling523bea82013-11-08 08:13:15 +0000804 OS << " @[";
805 printDebugLoc(InlinedAtDL, OS, Ctx);
806 OS << "]";
807 }
808 }
809}
810
Duncan P. N. Exon Smith2a78e9b2015-02-18 19:39:36 +0000811template <> DIRef<DIDescriptor>::DIRef(const Metadata *V) : Val(V) {
812 assert(isDescriptorRef(V) &&
813 "DIDescriptorRef should be a MDString or MDNode");
814}
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000815template <> DIRef<DIScope>::DIRef(const Metadata *V) : Val(V) {
Bill Wendling523bea82013-11-08 08:13:15 +0000816 assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode");
817}
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000818template <> DIRef<DIType>::DIRef(const Metadata *V) : Val(V) {
Bill Wendling523bea82013-11-08 08:13:15 +0000819 assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode");
820}
821
Bill Wendling523bea82013-11-08 08:13:15 +0000822template <>
Duncan P. N. Exon Smith2a78e9b2015-02-18 19:39:36 +0000823DIDescriptorRef DIDescriptor::getFieldAs<DIDescriptorRef>(unsigned Elt) const {
824 return DIDescriptorRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
825}
826template <>
Bill Wendling523bea82013-11-08 08:13:15 +0000827DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000828 return DIScopeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +0000829}
Bill Wendling523bea82013-11-08 08:13:15 +0000830template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000831 return DITypeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +0000832}
Manman Rencb14bbc2013-11-22 22:06:31 +0000833
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000834bool llvm::stripDebugInfo(Function &F) {
835 bool Changed = false;
836 for (BasicBlock &BB : F) {
837 for (Instruction &I : BB) {
838 if (I.getDebugLoc()) {
839 Changed = true;
840 I.setDebugLoc(DebugLoc());
841 }
842 }
843 }
844 return Changed;
845}
846
Manman Rencb14bbc2013-11-22 22:06:31 +0000847bool llvm::StripDebugInfo(Module &M) {
Manman Rencb14bbc2013-11-22 22:06:31 +0000848 bool Changed = false;
849
850 // Remove all of the calls to the debugger intrinsics, and remove them from
851 // the module.
852 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
853 while (!Declare->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000854 CallInst *CI = cast<CallInst>(Declare->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +0000855 CI->eraseFromParent();
856 }
857 Declare->eraseFromParent();
858 Changed = true;
859 }
860
861 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
862 while (!DbgVal->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000863 CallInst *CI = cast<CallInst>(DbgVal->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +0000864 CI->eraseFromParent();
865 }
866 DbgVal->eraseFromParent();
867 Changed = true;
868 }
869
870 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
871 NME = M.named_metadata_end(); NMI != NME;) {
872 NamedMDNode *NMD = NMI;
873 ++NMI;
874 if (NMD->getName().startswith("llvm.dbg.")) {
875 NMD->eraseFromParent();
876 Changed = true;
877 }
878 }
879
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000880 for (Function &F : M)
881 Changed |= stripDebugInfo(F);
882
883 if ( GVMaterializer *Materializer = M.getMaterializer())
884 Materializer->setStripDebugInfo();
Manman Rencb14bbc2013-11-22 22:06:31 +0000885
886 return Changed;
887}
Manman Ren8b4306c2013-12-02 21:29:56 +0000888
Manman Renbd4daf82013-12-03 00:12:14 +0000889unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
David Majnemere7a9cdb2015-02-16 06:04:53 +0000890 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000891 M.getModuleFlag("Debug Info Version")))
892 return Val->getZExtValue();
893 return 0;
Manman Ren8b4306c2013-12-02 21:29:56 +0000894}
David Blaikie6876b3b2014-07-01 20:05:26 +0000895
David Blaikiea8c35092014-07-02 18:30:05 +0000896llvm::DenseMap<const llvm::Function *, llvm::DISubprogram>
897llvm::makeSubprogramMap(const Module &M) {
898 DenseMap<const Function *, DISubprogram> R;
David Blaikie6876b3b2014-07-01 20:05:26 +0000899
900 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
901 if (!CU_Nodes)
902 return R;
903
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000904 for (MDNode *N : CU_Nodes->operands()) {
905 DICompileUnit CUNode(N);
David Blaikie6876b3b2014-07-01 20:05:26 +0000906 DIArray SPs = CUNode.getSubprograms();
907 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
908 DISubprogram SP(SPs.getElement(i));
909 if (Function *F = SP.getFunction())
910 R.insert(std::make_pair(F, SP));
911 }
912 }
913 return R;
914}