blob: 87e2ce0659436d70c16330537f2a7e2095cf8a87 [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"
20#include "llvm/Analysis/ValueTracking.h"
21#include "llvm/IR/Constants.h"
Adrian Prantlb1416832014-08-01 22:11:58 +000022#include "llvm/IR/DIBuilder.h"
Bill Wendling523bea82013-11-08 08:13:15 +000023#include "llvm/IR/DerivedTypes.h"
24#include "llvm/IR/Instructions.h"
25#include "llvm/IR/IntrinsicInst.h"
26#include "llvm/IR/Intrinsics.h"
Rafael Espindola0d68b4c2015-03-30 21:36:43 +000027#include "llvm/IR/GVMaterializer.h"
Bill Wendling523bea82013-11-08 08:13:15 +000028#include "llvm/IR/Module.h"
Chandler Carruth4220e9c2014-03-04 11:17:44 +000029#include "llvm/IR/ValueHandle.h"
Bill Wendling523bea82013-11-08 08:13:15 +000030#include "llvm/Support/Debug.h"
31#include "llvm/Support/Dwarf.h"
Bill Wendling523bea82013-11-08 08:13:15 +000032#include "llvm/Support/raw_ostream.h"
33using namespace llvm;
34using namespace llvm::dwarf;
35
Duncan P. N. Exon Smith930f3882015-04-06 18:02:43 +000036DIScopeRef DIScope::getRef() const { return MDScopeRef::get(get()); }
Bill Wendling523bea82013-11-08 08:13:15 +000037
Bill Wendling523bea82013-11-08 08:13:15 +000038DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
Duncan P. N. Exon Smithdd77af82015-03-31 02:06:28 +000039 if (auto *LocalScope = dyn_cast_or_null<MDLocalScope>(Scope))
40 return LocalScope->getSubprogram();
41 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +000042}
43
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +000044DISubprogram llvm::getDISubprogram(const Function *F) {
45 // We look for the first instr that has a debug annotation leading back to F.
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +000046 for (auto &BB : *F) {
David Majnemerc758df42014-11-01 07:57:14 +000047 auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) {
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +000048 return Inst.getDebugLoc();
David Majnemerc758df42014-11-01 07:57:14 +000049 });
50 if (Inst == BB.end())
51 continue;
52 DebugLoc DLoc = Inst->getDebugLoc();
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +000053 const MDNode *Scope = DLoc.getInlinedAtScope();
David Majnemerc758df42014-11-01 07:57:14 +000054 DISubprogram Subprogram = getDISubprogram(Scope);
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +000055 return Subprogram->describes(F) ? Subprogram : DISubprogram();
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +000056 }
57
58 return DISubprogram();
59}
60
Bill Wendling523bea82013-11-08 08:13:15 +000061DICompositeType llvm::getDICompositeType(DIType T) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +000062 if (auto *C = dyn_cast_or_null<MDCompositeTypeBase>(T))
63 return C;
Bill Wendling523bea82013-11-08 08:13:15 +000064
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +000065 if (auto *D = dyn_cast_or_null<MDDerivedTypeBase>(T)) {
Bill Wendling523bea82013-11-08 08:13:15 +000066 // This function is currently used by dragonegg and dragonegg does
67 // not generate identifier for types, so using an empty map to resolve
68 // DerivedFrom should be fine.
69 DITypeIdentifierMap EmptyMap;
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +000070 return getDICompositeType(D->getBaseType().resolve(EmptyMap));
Bill Wendling523bea82013-11-08 08:13:15 +000071 }
72
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +000073 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +000074}
75
Bill Wendling523bea82013-11-08 08:13:15 +000076DITypeIdentifierMap
77llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
78 DITypeIdentifierMap Map;
79 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
Duncan P. N. Exon Smith35ef22c2015-04-15 23:19:27 +000080 auto *CU = cast<MDCompileUnit>(CU_Nodes->getOperand(CUi));
81 DIArray Retain = CU->getRetainedTypes();
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +000082 for (unsigned Ti = 0, Te = Retain.size(); Ti != Te; ++Ti) {
83 if (!isa<MDCompositeType>(Retain[Ti]))
Bill Wendling523bea82013-11-08 08:13:15 +000084 continue;
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +000085 auto *Ty = cast<MDCompositeType>(Retain[Ti]);
86 if (MDString *TypeId = Ty->getRawIdentifier()) {
Bill Wendling523bea82013-11-08 08:13:15 +000087 // Definition has priority over declaration.
88 // Try to insert (TypeId, Ty) to Map.
89 std::pair<DITypeIdentifierMap::iterator, bool> P =
90 Map.insert(std::make_pair(TypeId, Ty));
91 // If TypeId already exists in Map and this is a definition, replace
92 // whatever we had (declaration or definition) with the definition.
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +000093 if (!P.second && !Ty->isForwardDecl())
Bill Wendling523bea82013-11-08 08:13:15 +000094 P.first->second = Ty;
95 }
96 }
97 }
98 return Map;
99}
100
101//===----------------------------------------------------------------------===//
102// DebugInfoFinder implementations.
103//===----------------------------------------------------------------------===//
104
105void DebugInfoFinder::reset() {
106 CUs.clear();
107 SPs.clear();
108 GVs.clear();
109 TYs.clear();
110 Scopes.clear();
111 NodesSeen.clear();
112 TypeIdentifierMap.clear();
Manman Ren2085ccc2013-11-17 18:42:37 +0000113 TypeMapInitialized = false;
114}
115
Manman Renb46e5502013-11-17 19:35:03 +0000116void DebugInfoFinder::InitializeTypeMap(const Module &M) {
Manman Ren2085ccc2013-11-17 18:42:37 +0000117 if (!TypeMapInitialized)
118 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
119 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
120 TypeMapInitialized = true;
121 }
Bill Wendling523bea82013-11-08 08:13:15 +0000122}
123
Bill Wendling523bea82013-11-08 08:13:15 +0000124void DebugInfoFinder::processModule(const Module &M) {
Manman Renb46e5502013-11-17 19:35:03 +0000125 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000126 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
Bill Wendling523bea82013-11-08 08:13:15 +0000127 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000128 DICompileUnit CU = cast<MDCompileUnit>(CU_Nodes->getOperand(i));
Bill Wendling523bea82013-11-08 08:13:15 +0000129 addCompileUnit(CU);
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000130 for (DIGlobalVariable DIG : CU->getGlobalVariables()) {
Bill Wendling523bea82013-11-08 08:13:15 +0000131 if (addGlobalVariable(DIG)) {
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000132 processScope(DIG->getScope());
133 processType(DIG->getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000134 }
135 }
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000136 for (auto *SP : CU->getSubprograms())
137 processSubprogram(SP);
138 for (auto *ET : CU->getEnumTypes())
139 processType(ET);
140 for (auto *RT : CU->getRetainedTypes())
141 processType(RT);
142 for (DIImportedEntity Import : CU->getImportedEntities()) {
Duncan P. N. Exon Smithde8e4272015-04-14 01:46:44 +0000143 auto *Entity = Import->getEntity().resolve(TypeIdentifierMap);
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000144 if (auto *T = dyn_cast<MDType>(Entity))
145 processType(T);
146 else if (auto *SP = dyn_cast<MDSubprogram>(Entity))
147 processSubprogram(SP);
148 else if (auto *NS = dyn_cast<MDNamespace>(Entity))
149 processScope(NS->getScope());
Bill Wendling523bea82013-11-08 08:13:15 +0000150 }
151 }
152 }
153}
154
Manman Ren2085ccc2013-11-17 18:42:37 +0000155void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) {
Bill Wendling523bea82013-11-08 08:13:15 +0000156 if (!Loc)
157 return;
Manman Renb46e5502013-11-17 19:35:03 +0000158 InitializeTypeMap(M);
Duncan P. N. Exon Smithb7e221b2015-04-14 01:35:55 +0000159 processScope(Loc->getScope());
160 processLocation(M, Loc->getInlinedAt());
Bill Wendling523bea82013-11-08 08:13:15 +0000161}
162
Bill Wendling523bea82013-11-08 08:13:15 +0000163void DebugInfoFinder::processType(DIType DT) {
164 if (!addType(DT))
165 return;
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000166 processScope(DT->getScope().resolve(TypeIdentifierMap));
167 if (auto *DCT = dyn_cast<MDCompositeTypeBase>(DT)) {
168 processType(DCT->getBaseType().resolve(TypeIdentifierMap));
169 if (auto *ST = dyn_cast<MDSubroutineType>(DCT)) {
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000170 for (MDTypeRef Ref : ST->getTypeArray())
171 processType(Ref.resolve(TypeIdentifierMap));
Manman Renf8a19672014-07-28 22:24:06 +0000172 return;
173 }
Anders Waldenborg1433fd42015-04-14 09:18:17 +0000174 for (Metadata *D : DCT->getElements()) {
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000175 if (auto *T = dyn_cast<MDType>(D))
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000176 processType(T);
177 else if (DISubprogram SP = dyn_cast<MDSubprogram>(D))
178 processSubprogram(SP);
Bill Wendling523bea82013-11-08 08:13:15 +0000179 }
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000180 } else if (auto *DDT = dyn_cast<MDDerivedTypeBase>(DT)) {
181 processType(DDT->getBaseType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000182 }
183}
184
185void DebugInfoFinder::processScope(DIScope Scope) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000186 if (!Scope)
187 return;
188 if (DIType Ty = dyn_cast<MDType>(Scope)) {
Bill Wendling523bea82013-11-08 08:13:15 +0000189 processType(Ty);
190 return;
191 }
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000192 if (DICompileUnit CU = dyn_cast<MDCompileUnit>(Scope)) {
193 addCompileUnit(CU);
Bill Wendling523bea82013-11-08 08:13:15 +0000194 return;
195 }
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000196 if (DISubprogram SP = dyn_cast<MDSubprogram>(Scope)) {
197 processSubprogram(SP);
Bill Wendling523bea82013-11-08 08:13:15 +0000198 return;
199 }
200 if (!addScope(Scope))
201 return;
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000202 if (auto *LB = dyn_cast<MDLexicalBlockBase>(Scope)) {
203 processScope(LB->getScope());
Duncan P. N. Exon Smith20caafb2015-04-14 03:01:27 +0000204 } else if (auto *NS = dyn_cast<MDNamespace>(Scope)) {
205 processScope(NS->getScope());
Bill Wendling523bea82013-11-08 08:13:15 +0000206 }
207}
208
Bill Wendling523bea82013-11-08 08:13:15 +0000209void DebugInfoFinder::processSubprogram(DISubprogram SP) {
210 if (!addSubprogram(SP))
211 return;
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000212 processScope(SP->getScope().resolve(TypeIdentifierMap));
213 processType(SP->getType());
214 for (auto *Element : SP->getTemplateParams()) {
Duncan P. N. Exon Smith20caafb2015-04-14 03:01:27 +0000215 if (auto *TType = dyn_cast<MDTemplateTypeParameter>(Element)) {
216 processType(TType->getType().resolve(TypeIdentifierMap));
217 } else if (auto *TVal = dyn_cast<MDTemplateValueParameter>(Element)) {
218 processType(TVal->getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000219 }
220 }
221}
222
Manman Ren2085ccc2013-11-17 18:42:37 +0000223void DebugInfoFinder::processDeclare(const Module &M,
224 const DbgDeclareInst *DDI) {
Bill Wendling523bea82013-11-08 08:13:15 +0000225 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
226 if (!N)
227 return;
Manman Renb46e5502013-11-17 19:35:03 +0000228 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000229
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000230 DIVariable DV = dyn_cast<MDLocalVariable>(N);
231 if (!DV)
Bill Wendling523bea82013-11-08 08:13:15 +0000232 return;
233
David Blaikie70573dc2014-11-19 07:49:26 +0000234 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000235 return;
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000236 processScope(DV->getScope());
237 processType(DV->getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000238}
239
Manman Ren2085ccc2013-11-17 18:42:37 +0000240void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
Bill Wendling523bea82013-11-08 08:13:15 +0000241 MDNode *N = dyn_cast<MDNode>(DVI->getVariable());
242 if (!N)
243 return;
Manman Renb46e5502013-11-17 19:35:03 +0000244 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000245
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000246 DIVariable DV = dyn_cast<MDLocalVariable>(N);
247 if (!DV)
Bill Wendling523bea82013-11-08 08:13:15 +0000248 return;
249
David Blaikie70573dc2014-11-19 07:49:26 +0000250 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000251 return;
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000252 processScope(DV->getScope());
253 processType(DV->getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000254}
255
Bill Wendling523bea82013-11-08 08:13:15 +0000256bool DebugInfoFinder::addType(DIType DT) {
257 if (!DT)
258 return false;
259
David Blaikie70573dc2014-11-19 07:49:26 +0000260 if (!NodesSeen.insert(DT).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000261 return false;
262
263 TYs.push_back(DT);
264 return true;
265}
266
Bill Wendling523bea82013-11-08 08:13:15 +0000267bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
268 if (!CU)
269 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000270 if (!NodesSeen.insert(CU).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000271 return false;
272
273 CUs.push_back(CU);
274 return true;
275}
276
Bill Wendling523bea82013-11-08 08:13:15 +0000277bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
278 if (!DIG)
279 return false;
280
David Blaikie70573dc2014-11-19 07:49:26 +0000281 if (!NodesSeen.insert(DIG).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000282 return false;
283
284 GVs.push_back(DIG);
285 return true;
286}
287
Bill Wendling523bea82013-11-08 08:13:15 +0000288bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
289 if (!SP)
290 return false;
291
David Blaikie70573dc2014-11-19 07:49:26 +0000292 if (!NodesSeen.insert(SP).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000293 return false;
294
295 SPs.push_back(SP);
296 return true;
297}
298
299bool DebugInfoFinder::addScope(DIScope Scope) {
300 if (!Scope)
301 return false;
302 // FIXME: Ocaml binding generates a scope with no content, we treat it
303 // as null for now.
304 if (Scope->getNumOperands() == 0)
305 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000306 if (!NodesSeen.insert(Scope).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000307 return false;
308 Scopes.push_back(Scope);
309 return true;
310}
311
312//===----------------------------------------------------------------------===//
313// DIDescriptor: dump routines for all descriptors.
314//===----------------------------------------------------------------------===//
315
Bill Wendling523bea82013-11-08 08:13:15 +0000316void DIDescriptor::dump() const {
317 print(dbgs());
318 dbgs() << '\n';
319}
320
Bill Wendling523bea82013-11-08 08:13:15 +0000321void DIDescriptor::print(raw_ostream &OS) const {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000322 if (!get())
Bill Wendling523bea82013-11-08 08:13:15 +0000323 return;
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000324 get()->print(OS);
Bill Wendling523bea82013-11-08 08:13:15 +0000325}
326
Duncan P. N. Exon Smith5bf3cdc2015-04-06 22:27:37 +0000327template <>
328DIDescriptor
329DIRef<DIDescriptor>::resolve(const DITypeIdentifierMap &Map) const {
330 return DIDescriptor(DebugNodeRef(Val).resolve(Map));
331}
332template <>
333DIScope DIRef<DIScope>::resolve(const DITypeIdentifierMap &Map) const {
334 return MDScopeRef(Val).resolve(Map);
335}
336template <>
337DIType DIRef<DIType>::resolve(const DITypeIdentifierMap &Map) const {
338 return MDTypeRef(Val).resolve(Map);
339}
340
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000341bool llvm::stripDebugInfo(Function &F) {
342 bool Changed = false;
343 for (BasicBlock &BB : F) {
344 for (Instruction &I : BB) {
345 if (I.getDebugLoc()) {
346 Changed = true;
347 I.setDebugLoc(DebugLoc());
348 }
349 }
350 }
351 return Changed;
352}
353
Manman Rencb14bbc2013-11-22 22:06:31 +0000354bool llvm::StripDebugInfo(Module &M) {
Manman Rencb14bbc2013-11-22 22:06:31 +0000355 bool Changed = false;
356
357 // Remove all of the calls to the debugger intrinsics, and remove them from
358 // the module.
359 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
360 while (!Declare->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000361 CallInst *CI = cast<CallInst>(Declare->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +0000362 CI->eraseFromParent();
363 }
364 Declare->eraseFromParent();
365 Changed = true;
366 }
367
368 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
369 while (!DbgVal->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000370 CallInst *CI = cast<CallInst>(DbgVal->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +0000371 CI->eraseFromParent();
372 }
373 DbgVal->eraseFromParent();
374 Changed = true;
375 }
376
377 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
378 NME = M.named_metadata_end(); NMI != NME;) {
379 NamedMDNode *NMD = NMI;
380 ++NMI;
381 if (NMD->getName().startswith("llvm.dbg.")) {
382 NMD->eraseFromParent();
383 Changed = true;
384 }
385 }
386
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000387 for (Function &F : M)
388 Changed |= stripDebugInfo(F);
389
Rafael Espindola468b8682015-04-01 14:44:59 +0000390 if (GVMaterializer *Materializer = M.getMaterializer())
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000391 Materializer->setStripDebugInfo();
Manman Rencb14bbc2013-11-22 22:06:31 +0000392
393 return Changed;
394}
Manman Ren8b4306c2013-12-02 21:29:56 +0000395
Manman Renbd4daf82013-12-03 00:12:14 +0000396unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
David Majnemere7a9cdb2015-02-16 06:04:53 +0000397 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000398 M.getModuleFlag("Debug Info Version")))
399 return Val->getZExtValue();
400 return 0;
Manman Ren8b4306c2013-12-02 21:29:56 +0000401}
David Blaikie6876b3b2014-07-01 20:05:26 +0000402
David Blaikiea8c35092014-07-02 18:30:05 +0000403llvm::DenseMap<const llvm::Function *, llvm::DISubprogram>
404llvm::makeSubprogramMap(const Module &M) {
405 DenseMap<const Function *, DISubprogram> R;
David Blaikie6876b3b2014-07-01 20:05:26 +0000406
407 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
408 if (!CU_Nodes)
409 return R;
410
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000411 for (MDNode *N : CU_Nodes->operands()) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000412 DICompileUnit CUNode = cast<MDCompileUnit>(N);
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000413 for (DISubprogram SP : CUNode->getSubprograms()) {
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000414 if (Function *F = SP->getFunction())
David Blaikie6876b3b2014-07-01 20:05:26 +0000415 R.insert(std::make_pair(F, SP));
416 }
417 }
418 return R;
419}