blob: 721fbc8c3a08e6fe6255189f6bf5ee9a00dc92ac [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
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000083static Metadata *getField(const MDNode *DbgNode, unsigned Elt) {
Craig Topperc6207612014-04-09 06:08:46 +000084 if (!DbgNode || Elt >= DbgNode->getNumOperands())
85 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +000086 return DbgNode->getOperand(Elt);
87}
88
89static MDNode *getNodeField(const MDNode *DbgNode, unsigned Elt) {
90 return dyn_cast_or_null<MDNode>(getField(DbgNode, Elt));
91}
92
Bill Wendling523bea82013-11-08 08:13:15 +000093DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
94 MDNode *Field = getNodeField(DbgNode, Elt);
95 return DIDescriptor(Field);
96}
97
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +000098/// \brief Return the size reported by the variable's type.
Adrian Prantlb1416832014-08-01 22:11:58 +000099unsigned DIVariable::getSizeInBits(const DITypeIdentifierMap &Map) {
100 DIType Ty = getType().resolve(Map);
101 // Follow derived types until we reach a type that
102 // reports back a size.
103 while (Ty.isDerivedType() && !Ty.getSizeInBits()) {
104 DIDerivedType DT(&*Ty);
105 Ty = DT.getTypeDerivedFrom().resolve(Map);
106 }
107 assert(Ty.getSizeInBits() && "type with size 0");
108 return Ty.getSizeInBits();
109}
110
Adrian Prantl27bd01f2015-02-09 23:57:15 +0000111bool DIExpression::isBitPiece() const {
Adrian Prantl34bcbee2015-01-21 00:59:20 +0000112 unsigned N = getNumElements();
Adrian Prantl27bd01f2015-02-09 23:57:15 +0000113 return N >=3 && getElement(N-3) == dwarf::DW_OP_bit_piece;
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000114}
115
Adrian Prantl27bd01f2015-02-09 23:57:15 +0000116uint64_t DIExpression::getBitPieceOffset() const {
117 assert(isBitPiece() && "not a piece");
Adrian Prantl34bcbee2015-01-21 00:59:20 +0000118 return getElement(getNumElements()-2);
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000119}
120
Adrian Prantl27bd01f2015-02-09 23:57:15 +0000121uint64_t DIExpression::getBitPieceSize() const {
122 assert(isBitPiece() && "not a piece");
Adrian Prantl34bcbee2015-01-21 00:59:20 +0000123 return getElement(getNumElements()-1);
Adrian Prantl87b7eb92014-10-01 18:55:02 +0000124}
Adrian Prantlb1416832014-08-01 22:11:58 +0000125
Adrian Prantl0f615792015-03-04 17:39:33 +0000126DIExpression::iterator DIExpression::Operand::getNext() const {
Adrian Prantl70f2a732015-01-23 23:40:47 +0000127 iterator it(I);
Adrian Prantl0f615792015-03-04 17:39:33 +0000128 return ++it;
Adrian Prantl70f2a732015-01-23 23:40:47 +0000129}
130
Bill Wendling523bea82013-11-08 08:13:15 +0000131//===----------------------------------------------------------------------===//
Bill Wendling523bea82013-11-08 08:13:15 +0000132// Simple Descriptor Constructors and other Methods
133//===----------------------------------------------------------------------===//
134
Duncan P. N. Exon Smith9c3b8942015-02-28 23:48:02 +0000135void DIDescriptor::replaceAllUsesWith(LLVMContext &, DIDescriptor D) {
Bill Wendling523bea82013-11-08 08:13:15 +0000136 assert(DbgNode && "Trying to replace an unverified type!");
Duncan P. N. Exon Smith9c3b8942015-02-28 23:48:02 +0000137 assert(DbgNode->isTemporary() && "Expected temporary node");
138 TempMDNode Temp(get());
Bill Wendling523bea82013-11-08 08:13:15 +0000139
140 // Since we use a TrackingVH for the node, its easy for clients to manufacture
141 // legitimate situations where they want to replaceAllUsesWith() on something
142 // which, due to uniquing, has merged with the source. We shield clients from
143 // this detail by allowing a value to be replaced with replaceAllUsesWith()
144 // itself.
Duncan P. N. Exon Smith9c3b8942015-02-28 23:48:02 +0000145 if (Temp.get() == D.get()) {
146 DbgNode = MDNode::replaceWithUniqued(std::move(Temp));
147 return;
Bill Wendling523bea82013-11-08 08:13:15 +0000148 }
David Blaikied3f094a2014-05-06 03:41:57 +0000149
Duncan P. N. Exon Smith9c3b8942015-02-28 23:48:02 +0000150 Temp->replaceAllUsesWith(D.get());
151 DbgNode = D.get();
Bill Wendling523bea82013-11-08 08:13:15 +0000152}
153
Frederic Riss36acf0f2014-09-15 07:50:36 +0000154void DIDescriptor::replaceAllUsesWith(MDNode *D) {
Bill Wendling523bea82013-11-08 08:13:15 +0000155 assert(DbgNode && "Trying to replace an unverified type!");
David Blaikied3f094a2014-05-06 03:41:57 +0000156 assert(DbgNode != D && "This replacement should always happen");
Duncan P. N. Exon Smith946fdcc2015-01-19 20:36:39 +0000157 assert(DbgNode->isTemporary() && "Expected temporary node");
Duncan P. N. Exon Smith9c3b8942015-02-28 23:48:02 +0000158 TempMDNode Node(get());
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000159 Node->replaceAllUsesWith(D);
Bill Wendling523bea82013-11-08 08:13:15 +0000160}
161
Duncan P. N. Exon Smith3b960c92015-03-31 01:47:55 +0000162#ifndef NDEBUG
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000163/// \brief Check if a value can be a reference to a type.
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000164static bool isTypeRef(const Metadata *MD) {
165 if (!MD)
166 return true;
167 if (auto *S = dyn_cast<MDString>(MD))
168 return !S->getString().empty();
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000169 return isa<MDType>(MD);
Bill Wendling523bea82013-11-08 08:13:15 +0000170}
171
Duncan P. N. Exon Smith7f637a92014-10-15 17:01:28 +0000172/// \brief Check if a value can be a ScopeRef.
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000173static bool isScopeRef(const Metadata *MD) {
174 if (!MD)
175 return true;
176 if (auto *S = dyn_cast<MDString>(MD))
177 return !S->getString().empty();
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000178 return isa<MDScope>(MD);
Bill Wendling523bea82013-11-08 08:13:15 +0000179}
180
Duncan P. N. Exon Smith2a78e9b2015-02-18 19:39:36 +0000181/// \brief Check if a value can be a DescriptorRef.
182static bool isDescriptorRef(const Metadata *MD) {
183 if (!MD)
184 return true;
185 if (auto *S = dyn_cast<MDString>(MD))
186 return !S->getString().empty();
187 return isa<MDNode>(MD);
188}
Duncan P. N. Exon Smithe4450142015-02-18 19:56:50 +0000189#endif
Duncan P. N. Exon Smith2a78e9b2015-02-18 19:39:36 +0000190
Manman Ren1a125c92014-07-28 19:33:20 +0000191void DICompositeType::setArraysHelper(MDNode *Elements, MDNode *TParams) {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000192 TypedTrackingMDRef<MDCompositeTypeBase> N(get());
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000193 if (Elements)
194 N->replaceElements(cast<MDTuple>(Elements));
Bill Wendling523bea82013-11-08 08:13:15 +0000195 if (TParams)
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000196 N->replaceTemplateParams(cast<MDTuple>(TParams));
Bill Wendling523bea82013-11-08 08:13:15 +0000197 DbgNode = N;
198}
199
Duncan P. N. Exon Smith930f3882015-04-06 18:02:43 +0000200DIScopeRef DIScope::getRef() const { return MDScopeRef::get(get()); }
Bill Wendling523bea82013-11-08 08:13:15 +0000201
Bill Wendling523bea82013-11-08 08:13:15 +0000202void DICompositeType::setContainingType(DICompositeType ContainingType) {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000203 TypedTrackingMDRef<MDCompositeTypeBase> N(get());
Duncan P. N. Exon Smith3ec5fa62015-04-06 19:03:45 +0000204 N->replaceVTableHolder(MDTypeRef::get(ContainingType));
Bill Wendling523bea82013-11-08 08:13:15 +0000205 DbgNode = N;
206}
207
Bill Wendling523bea82013-11-08 08:13:15 +0000208bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
209 assert(CurFn && "Invalid function");
210 if (!getContext().isSubprogram())
211 return false;
212 // This variable is not inlined function argument if its scope
213 // does not describe current function.
214 return !DISubprogram(getContext()).describes(CurFn);
215}
216
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000217Function *DISubprogram::getFunction() const {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000218 if (auto *N = get())
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000219 if (auto *C = dyn_cast_or_null<ConstantAsMetadata>(N->getFunction()))
220 return dyn_cast<Function>(C->getValue());
221 return nullptr;
222}
223
Bill Wendling523bea82013-11-08 08:13:15 +0000224bool DISubprogram::describes(const Function *F) {
225 assert(F && "Invalid function");
226 if (F == getFunction())
227 return true;
228 StringRef Name = getLinkageName();
229 if (Name.empty())
230 Name = getName();
231 if (F->getName() == Name)
232 return true;
233 return false;
234}
235
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000236GlobalVariable *DIGlobalVariable::getGlobal() const {
237 return dyn_cast_or_null<GlobalVariable>(getConstant());
Bill Wendling523bea82013-11-08 08:13:15 +0000238}
239
Bill Wendling523bea82013-11-08 08:13:15 +0000240DIScopeRef DIScope::getContext() const {
241
242 if (isType())
243 return DIType(DbgNode).getContext();
244
245 if (isSubprogram())
246 return DIScopeRef(DISubprogram(DbgNode).getContext());
247
248 if (isLexicalBlock())
249 return DIScopeRef(DILexicalBlock(DbgNode).getContext());
250
251 if (isLexicalBlockFile())
252 return DIScopeRef(DILexicalBlockFile(DbgNode).getContext());
253
254 if (isNameSpace())
255 return DIScopeRef(DINameSpace(DbgNode).getContext());
256
257 assert((isFile() || isCompileUnit()) && "Unhandled type of scope.");
Craig Topperc6207612014-04-09 06:08:46 +0000258 return DIScopeRef(nullptr);
Bill Wendling523bea82013-11-08 08:13:15 +0000259}
260
Bill Wendling523bea82013-11-08 08:13:15 +0000261StringRef DIScope::getName() const {
262 if (isType())
263 return DIType(DbgNode).getName();
264 if (isSubprogram())
265 return DISubprogram(DbgNode).getName();
266 if (isNameSpace())
267 return DINameSpace(DbgNode).getName();
268 assert((isLexicalBlock() || isLexicalBlockFile() || isFile() ||
269 isCompileUnit()) &&
270 "Unhandled type of scope.");
271 return StringRef();
272}
273
274StringRef DIScope::getFilename() const {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000275 if (auto *N = get())
Duncan P. N. Exon Smith897030c2015-04-06 16:43:40 +0000276 if (auto *F = N->getFile())
277 return F->getFilename();
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000278 return "";
Bill Wendling523bea82013-11-08 08:13:15 +0000279}
280
281StringRef DIScope::getDirectory() const {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000282 if (auto *N = get())
Duncan P. N. Exon Smith897030c2015-04-06 16:43:40 +0000283 if (auto *F = N->getFile())
284 return F->getDirectory();
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000285 return "";
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000286}
287
288void DICompileUnit::replaceSubprograms(DIArray Subprograms) {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000289 get()->replaceSubprograms(cast_or_null<MDTuple>(Subprograms.get()));
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +0000290}
291
292void DICompileUnit::replaceGlobalVariables(DIArray GlobalVariables) {
Duncan P. N. Exon Smith9b9cc2d2015-03-23 21:54:07 +0000293 get()->replaceGlobalVariables(cast_or_null<MDTuple>(GlobalVariables.get()));
Bill Wendling523bea82013-11-08 08:13:15 +0000294}
295
Diego Novillof5041ce2014-03-03 20:06:11 +0000296DILocation DILocation::copyWithNewScope(LLVMContext &Ctx,
David Blaikie2f3f76f2014-08-21 22:45:21 +0000297 DILexicalBlockFile NewScope) {
Duncan P. N. Exon Smith98854692015-01-14 22:27:36 +0000298 assert(NewScope && "Expected valid scope");
299
300 const auto *Old = cast<MDLocation>(DbgNode);
301 return DILocation(MDLocation::get(Ctx, Old->getLine(), Old->getColumn(),
302 NewScope, Old->getInlinedAt()));
Diego Novillof5041ce2014-03-03 20:06:11 +0000303}
304
Diego Novillof5041ce2014-03-03 20:06:11 +0000305unsigned DILocation::computeNewDiscriminator(LLVMContext &Ctx) {
306 std::pair<const char *, unsigned> Key(getFilename().data(), getLineNumber());
307 return ++Ctx.pImpl->DiscriminatorTable[Key];
308}
309
Bill Wendling523bea82013-11-08 08:13:15 +0000310DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
311 LLVMContext &VMContext) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000312 return cast<MDLocalVariable>(DV)
313 ->withInline(cast_or_null<MDLocation>(InlinedScope));
Bill Wendling523bea82013-11-08 08:13:15 +0000314}
315
Bill Wendling523bea82013-11-08 08:13:15 +0000316DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000317 return cast<MDLocalVariable>(DV)->withoutInline();
Bill Wendling523bea82013-11-08 08:13:15 +0000318}
319
Bill Wendling523bea82013-11-08 08:13:15 +0000320DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
Duncan P. N. Exon Smithdd77af82015-03-31 02:06:28 +0000321 if (auto *LocalScope = dyn_cast_or_null<MDLocalScope>(Scope))
322 return LocalScope->getSubprogram();
323 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +0000324}
325
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000326DISubprogram llvm::getDISubprogram(const Function *F) {
327 // We look for the first instr that has a debug annotation leading back to F.
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000328 for (auto &BB : *F) {
David Majnemerc758df42014-11-01 07:57:14 +0000329 auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) {
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +0000330 return Inst.getDebugLoc();
David Majnemerc758df42014-11-01 07:57:14 +0000331 });
332 if (Inst == BB.end())
333 continue;
334 DebugLoc DLoc = Inst->getDebugLoc();
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +0000335 const MDNode *Scope = DLoc.getInlinedAtScope();
David Majnemerc758df42014-11-01 07:57:14 +0000336 DISubprogram Subprogram = getDISubprogram(Scope);
337 return Subprogram.describes(F) ? Subprogram : DISubprogram();
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +0000338 }
339
340 return DISubprogram();
341}
342
Bill Wendling523bea82013-11-08 08:13:15 +0000343DICompositeType llvm::getDICompositeType(DIType T) {
344 if (T.isCompositeType())
345 return DICompositeType(T);
346
347 if (T.isDerivedType()) {
348 // This function is currently used by dragonegg and dragonegg does
349 // not generate identifier for types, so using an empty map to resolve
350 // DerivedFrom should be fine.
351 DITypeIdentifierMap EmptyMap;
352 return getDICompositeType(
353 DIDerivedType(T).getTypeDerivedFrom().resolve(EmptyMap));
354 }
355
356 return DICompositeType();
357}
358
Bill Wendling523bea82013-11-08 08:13:15 +0000359DITypeIdentifierMap
360llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
361 DITypeIdentifierMap Map;
362 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000363 DICompileUnit CU(CU_Nodes->getOperand(CUi));
Bill Wendling523bea82013-11-08 08:13:15 +0000364 DIArray Retain = CU.getRetainedTypes();
365 for (unsigned Ti = 0, Te = Retain.getNumElements(); Ti != Te; ++Ti) {
366 if (!Retain.getElement(Ti).isCompositeType())
367 continue;
368 DICompositeType Ty(Retain.getElement(Ti));
369 if (MDString *TypeId = Ty.getIdentifier()) {
370 // Definition has priority over declaration.
371 // Try to insert (TypeId, Ty) to Map.
372 std::pair<DITypeIdentifierMap::iterator, bool> P =
373 Map.insert(std::make_pair(TypeId, Ty));
374 // If TypeId already exists in Map and this is a definition, replace
375 // whatever we had (declaration or definition) with the definition.
376 if (!P.second && !Ty.isForwardDecl())
377 P.first->second = Ty;
378 }
379 }
380 }
381 return Map;
382}
383
384//===----------------------------------------------------------------------===//
385// DebugInfoFinder implementations.
386//===----------------------------------------------------------------------===//
387
388void DebugInfoFinder::reset() {
389 CUs.clear();
390 SPs.clear();
391 GVs.clear();
392 TYs.clear();
393 Scopes.clear();
394 NodesSeen.clear();
395 TypeIdentifierMap.clear();
Manman Ren2085ccc2013-11-17 18:42:37 +0000396 TypeMapInitialized = false;
397}
398
Manman Renb46e5502013-11-17 19:35:03 +0000399void DebugInfoFinder::InitializeTypeMap(const Module &M) {
Manman Ren2085ccc2013-11-17 18:42:37 +0000400 if (!TypeMapInitialized)
401 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
402 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
403 TypeMapInitialized = true;
404 }
Bill Wendling523bea82013-11-08 08:13:15 +0000405}
406
Bill Wendling523bea82013-11-08 08:13:15 +0000407void DebugInfoFinder::processModule(const Module &M) {
Manman Renb46e5502013-11-17 19:35:03 +0000408 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000409 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
Bill Wendling523bea82013-11-08 08:13:15 +0000410 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000411 DICompileUnit CU(CU_Nodes->getOperand(i));
Bill Wendling523bea82013-11-08 08:13:15 +0000412 addCompileUnit(CU);
413 DIArray GVs = CU.getGlobalVariables();
414 for (unsigned i = 0, e = GVs.getNumElements(); i != e; ++i) {
415 DIGlobalVariable DIG(GVs.getElement(i));
416 if (addGlobalVariable(DIG)) {
Manman Renf0a582b2014-11-21 19:55:23 +0000417 processScope(DIG.getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000418 processType(DIG.getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000419 }
420 }
421 DIArray SPs = CU.getSubprograms();
422 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i)
423 processSubprogram(DISubprogram(SPs.getElement(i)));
424 DIArray EnumTypes = CU.getEnumTypes();
425 for (unsigned i = 0, e = EnumTypes.getNumElements(); i != e; ++i)
426 processType(DIType(EnumTypes.getElement(i)));
427 DIArray RetainedTypes = CU.getRetainedTypes();
428 for (unsigned i = 0, e = RetainedTypes.getNumElements(); i != e; ++i)
429 processType(DIType(RetainedTypes.getElement(i)));
430 DIArray Imports = CU.getImportedEntities();
431 for (unsigned i = 0, e = Imports.getNumElements(); i != e; ++i) {
432 DIImportedEntity Import = DIImportedEntity(Imports.getElement(i));
Duncan P. N. Exon Smithd4e07c92015-03-20 19:13:53 +0000433 if (!Import)
434 continue;
Adrian Prantld09ba232014-04-01 03:41:04 +0000435 DIDescriptor Entity = Import.getEntity().resolve(TypeIdentifierMap);
Bill Wendling523bea82013-11-08 08:13:15 +0000436 if (Entity.isType())
437 processType(DIType(Entity));
438 else if (Entity.isSubprogram())
439 processSubprogram(DISubprogram(Entity));
440 else if (Entity.isNameSpace())
441 processScope(DINameSpace(Entity).getContext());
442 }
443 }
444 }
445}
446
Manman Ren2085ccc2013-11-17 18:42:37 +0000447void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) {
Bill Wendling523bea82013-11-08 08:13:15 +0000448 if (!Loc)
449 return;
Manman Renb46e5502013-11-17 19:35:03 +0000450 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000451 processScope(Loc.getScope());
Manman Ren2085ccc2013-11-17 18:42:37 +0000452 processLocation(M, Loc.getOrigLocation());
Bill Wendling523bea82013-11-08 08:13:15 +0000453}
454
Bill Wendling523bea82013-11-08 08:13:15 +0000455void DebugInfoFinder::processType(DIType DT) {
456 if (!addType(DT))
457 return;
458 processScope(DT.getContext().resolve(TypeIdentifierMap));
459 if (DT.isCompositeType()) {
460 DICompositeType DCT(DT);
461 processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
Manman Renf8a19672014-07-28 22:24:06 +0000462 if (DT.isSubroutineType()) {
463 DITypeArray DTA = DISubroutineType(DT).getTypeArray();
464 for (unsigned i = 0, e = DTA.getNumElements(); i != e; ++i)
465 processType(DTA.getElement(i).resolve(TypeIdentifierMap));
466 return;
467 }
Manman Renab8ffba2014-07-28 19:14:13 +0000468 DIArray DA = DCT.getElements();
Bill Wendling523bea82013-11-08 08:13:15 +0000469 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
470 DIDescriptor D = DA.getElement(i);
471 if (D.isType())
472 processType(DIType(D));
473 else if (D.isSubprogram())
474 processSubprogram(DISubprogram(D));
475 }
476 } else if (DT.isDerivedType()) {
477 DIDerivedType DDT(DT);
478 processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
479 }
480}
481
482void DebugInfoFinder::processScope(DIScope Scope) {
483 if (Scope.isType()) {
484 DIType Ty(Scope);
485 processType(Ty);
486 return;
487 }
488 if (Scope.isCompileUnit()) {
489 addCompileUnit(DICompileUnit(Scope));
490 return;
491 }
492 if (Scope.isSubprogram()) {
493 processSubprogram(DISubprogram(Scope));
494 return;
495 }
496 if (!addScope(Scope))
497 return;
498 if (Scope.isLexicalBlock()) {
499 DILexicalBlock LB(Scope);
500 processScope(LB.getContext());
Bill Wendling523bea82013-11-08 08:13:15 +0000501 } else if (Scope.isNameSpace()) {
502 DINameSpace NS(Scope);
503 processScope(NS.getContext());
504 }
505}
506
Bill Wendling523bea82013-11-08 08:13:15 +0000507void DebugInfoFinder::processSubprogram(DISubprogram SP) {
508 if (!addSubprogram(SP))
509 return;
510 processScope(SP.getContext().resolve(TypeIdentifierMap));
511 processType(SP.getType());
512 DIArray TParams = SP.getTemplateParams();
513 for (unsigned I = 0, E = TParams.getNumElements(); I != E; ++I) {
514 DIDescriptor Element = TParams.getElement(I);
515 if (Element.isTemplateTypeParameter()) {
516 DITemplateTypeParameter TType(Element);
Bill Wendling523bea82013-11-08 08:13:15 +0000517 processType(TType.getType().resolve(TypeIdentifierMap));
518 } else if (Element.isTemplateValueParameter()) {
519 DITemplateValueParameter TVal(Element);
Bill Wendling523bea82013-11-08 08:13:15 +0000520 processType(TVal.getType().resolve(TypeIdentifierMap));
521 }
522 }
523}
524
Manman Ren2085ccc2013-11-17 18:42:37 +0000525void DebugInfoFinder::processDeclare(const Module &M,
526 const DbgDeclareInst *DDI) {
Bill Wendling523bea82013-11-08 08:13:15 +0000527 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
528 if (!N)
529 return;
Manman Renb46e5502013-11-17 19:35:03 +0000530 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000531
532 DIDescriptor DV(N);
533 if (!DV.isVariable())
534 return;
535
David Blaikie70573dc2014-11-19 07:49:26 +0000536 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000537 return;
538 processScope(DIVariable(N).getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000539 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000540}
541
Manman Ren2085ccc2013-11-17 18:42:37 +0000542void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
Bill Wendling523bea82013-11-08 08:13:15 +0000543 MDNode *N = dyn_cast<MDNode>(DVI->getVariable());
544 if (!N)
545 return;
Manman Renb46e5502013-11-17 19:35:03 +0000546 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000547
548 DIDescriptor DV(N);
549 if (!DV.isVariable())
550 return;
551
David Blaikie70573dc2014-11-19 07:49:26 +0000552 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000553 return;
554 processScope(DIVariable(N).getContext());
Adrian Prantl1a1647c2014-03-18 02:34:58 +0000555 processType(DIVariable(N).getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000556}
557
Bill Wendling523bea82013-11-08 08:13:15 +0000558bool DebugInfoFinder::addType(DIType DT) {
559 if (!DT)
560 return false;
561
David Blaikie70573dc2014-11-19 07:49:26 +0000562 if (!NodesSeen.insert(DT).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000563 return false;
564
565 TYs.push_back(DT);
566 return true;
567}
568
Bill Wendling523bea82013-11-08 08:13:15 +0000569bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
570 if (!CU)
571 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000572 if (!NodesSeen.insert(CU).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000573 return false;
574
575 CUs.push_back(CU);
576 return true;
577}
578
Bill Wendling523bea82013-11-08 08:13:15 +0000579bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
580 if (!DIG)
581 return false;
582
David Blaikie70573dc2014-11-19 07:49:26 +0000583 if (!NodesSeen.insert(DIG).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000584 return false;
585
586 GVs.push_back(DIG);
587 return true;
588}
589
Bill Wendling523bea82013-11-08 08:13:15 +0000590bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
591 if (!SP)
592 return false;
593
David Blaikie70573dc2014-11-19 07:49:26 +0000594 if (!NodesSeen.insert(SP).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000595 return false;
596
597 SPs.push_back(SP);
598 return true;
599}
600
601bool DebugInfoFinder::addScope(DIScope Scope) {
602 if (!Scope)
603 return false;
604 // FIXME: Ocaml binding generates a scope with no content, we treat it
605 // as null for now.
606 if (Scope->getNumOperands() == 0)
607 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000608 if (!NodesSeen.insert(Scope).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000609 return false;
610 Scopes.push_back(Scope);
611 return true;
612}
613
614//===----------------------------------------------------------------------===//
615// DIDescriptor: dump routines for all descriptors.
616//===----------------------------------------------------------------------===//
617
Bill Wendling523bea82013-11-08 08:13:15 +0000618void DIDescriptor::dump() const {
619 print(dbgs());
620 dbgs() << '\n';
621}
622
Bill Wendling523bea82013-11-08 08:13:15 +0000623void DIDescriptor::print(raw_ostream &OS) const {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000624 if (!get())
Bill Wendling523bea82013-11-08 08:13:15 +0000625 return;
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000626 get()->print(OS);
Bill Wendling523bea82013-11-08 08:13:15 +0000627}
628
629static void printDebugLoc(DebugLoc DL, raw_ostream &CommentOS,
630 const LLVMContext &Ctx) {
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +0000631 if (!DL)
Duncan P. N. Exon Smith51306ef2015-03-30 18:45:11 +0000632 return;
633
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +0000634 DIScope Scope(DL.getScope());
Duncan P. N. Exon Smith51306ef2015-03-30 18:45:11 +0000635 assert(Scope.isScope() && "Scope of a DebugLoc should be a DIScope.");
636 // Omit the directory, because it's likely to be long and uninteresting.
637 CommentOS << Scope.getFilename();
638 CommentOS << ':' << DL.getLine();
639 if (DL.getCol() != 0)
640 CommentOS << ':' << DL.getCol();
641
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +0000642 DebugLoc InlinedAtDL = DL.getInlinedAt();
643 if (!InlinedAtDL)
Duncan P. N. Exon Smith51306ef2015-03-30 18:45:11 +0000644 return;
645
646 CommentOS << " @[ ";
647 printDebugLoc(InlinedAtDL, CommentOS, Ctx);
648 CommentOS << " ]";
Bill Wendling523bea82013-11-08 08:13:15 +0000649}
650
651void DIVariable::printExtendedName(raw_ostream &OS) const {
652 const LLVMContext &Ctx = DbgNode->getContext();
653 StringRef Res = getName();
654 if (!Res.empty())
655 OS << Res << "," << getLineNumber();
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +0000656 if (auto *InlinedAt = get()->getInlinedAt()) {
657 if (DebugLoc InlinedAtDL = InlinedAt) {
Bill Wendling523bea82013-11-08 08:13:15 +0000658 OS << " @[";
659 printDebugLoc(InlinedAtDL, OS, Ctx);
660 OS << "]";
661 }
662 }
663}
664
Duncan P. N. Exon Smith2a78e9b2015-02-18 19:39:36 +0000665template <> DIRef<DIDescriptor>::DIRef(const Metadata *V) : Val(V) {
666 assert(isDescriptorRef(V) &&
667 "DIDescriptorRef should be a MDString or MDNode");
668}
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000669template <> DIRef<DIScope>::DIRef(const Metadata *V) : Val(V) {
Bill Wendling523bea82013-11-08 08:13:15 +0000670 assert(isScopeRef(V) && "DIScopeRef should be a MDString or MDNode");
671}
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000672template <> DIRef<DIType>::DIRef(const Metadata *V) : Val(V) {
Bill Wendling523bea82013-11-08 08:13:15 +0000673 assert(isTypeRef(V) && "DITypeRef should be a MDString or MDNode");
674}
675
Bill Wendling523bea82013-11-08 08:13:15 +0000676template <>
Duncan P. N. Exon Smith2a78e9b2015-02-18 19:39:36 +0000677DIDescriptorRef DIDescriptor::getFieldAs<DIDescriptorRef>(unsigned Elt) const {
678 return DIDescriptorRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
679}
680template <>
Bill Wendling523bea82013-11-08 08:13:15 +0000681DIScopeRef DIDescriptor::getFieldAs<DIScopeRef>(unsigned Elt) const {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000682 return DIScopeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +0000683}
Bill Wendling523bea82013-11-08 08:13:15 +0000684template <> DITypeRef DIDescriptor::getFieldAs<DITypeRef>(unsigned Elt) const {
Duncan P. N. Exon Smithc81307a2014-11-14 23:55:03 +0000685 return DITypeRef(cast_or_null<Metadata>(getField(DbgNode, Elt)));
Bill Wendling523bea82013-11-08 08:13:15 +0000686}
Manman Rencb14bbc2013-11-22 22:06:31 +0000687
Duncan P. N. Exon Smith5bf3cdc2015-04-06 22:27:37 +0000688template <>
689DIDescriptor
690DIRef<DIDescriptor>::resolve(const DITypeIdentifierMap &Map) const {
691 return DIDescriptor(DebugNodeRef(Val).resolve(Map));
692}
693template <>
694DIScope DIRef<DIScope>::resolve(const DITypeIdentifierMap &Map) const {
695 return MDScopeRef(Val).resolve(Map);
696}
697template <>
698DIType DIRef<DIType>::resolve(const DITypeIdentifierMap &Map) const {
699 return MDTypeRef(Val).resolve(Map);
700}
701
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000702bool llvm::stripDebugInfo(Function &F) {
703 bool Changed = false;
704 for (BasicBlock &BB : F) {
705 for (Instruction &I : BB) {
706 if (I.getDebugLoc()) {
707 Changed = true;
708 I.setDebugLoc(DebugLoc());
709 }
710 }
711 }
712 return Changed;
713}
714
Manman Rencb14bbc2013-11-22 22:06:31 +0000715bool llvm::StripDebugInfo(Module &M) {
Manman Rencb14bbc2013-11-22 22:06:31 +0000716 bool Changed = false;
717
718 // Remove all of the calls to the debugger intrinsics, and remove them from
719 // the module.
720 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
721 while (!Declare->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000722 CallInst *CI = cast<CallInst>(Declare->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +0000723 CI->eraseFromParent();
724 }
725 Declare->eraseFromParent();
726 Changed = true;
727 }
728
729 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
730 while (!DbgVal->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000731 CallInst *CI = cast<CallInst>(DbgVal->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +0000732 CI->eraseFromParent();
733 }
734 DbgVal->eraseFromParent();
735 Changed = true;
736 }
737
738 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
739 NME = M.named_metadata_end(); NMI != NME;) {
740 NamedMDNode *NMD = NMI;
741 ++NMI;
742 if (NMD->getName().startswith("llvm.dbg.")) {
743 NMD->eraseFromParent();
744 Changed = true;
745 }
746 }
747
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000748 for (Function &F : M)
749 Changed |= stripDebugInfo(F);
750
Rafael Espindola468b8682015-04-01 14:44:59 +0000751 if (GVMaterializer *Materializer = M.getMaterializer())
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000752 Materializer->setStripDebugInfo();
Manman Rencb14bbc2013-11-22 22:06:31 +0000753
754 return Changed;
755}
Manman Ren8b4306c2013-12-02 21:29:56 +0000756
Manman Renbd4daf82013-12-03 00:12:14 +0000757unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
David Majnemere7a9cdb2015-02-16 06:04:53 +0000758 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000759 M.getModuleFlag("Debug Info Version")))
760 return Val->getZExtValue();
761 return 0;
Manman Ren8b4306c2013-12-02 21:29:56 +0000762}
David Blaikie6876b3b2014-07-01 20:05:26 +0000763
David Blaikiea8c35092014-07-02 18:30:05 +0000764llvm::DenseMap<const llvm::Function *, llvm::DISubprogram>
765llvm::makeSubprogramMap(const Module &M) {
766 DenseMap<const Function *, DISubprogram> R;
David Blaikie6876b3b2014-07-01 20:05:26 +0000767
768 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
769 if (!CU_Nodes)
770 return R;
771
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000772 for (MDNode *N : CU_Nodes->operands()) {
773 DICompileUnit CUNode(N);
David Blaikie6876b3b2014-07-01 20:05:26 +0000774 DIArray SPs = CUNode.getSubprograms();
775 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
776 DISubprogram SP(SPs.getElement(i));
777 if (Function *F = SP.getFunction())
778 R.insert(std::make_pair(F, SP));
779 }
780 }
781 return R;
782}