blob: f396721efec045617d36d33b5ac65a9dfbf08cd7 [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;
70 return getDICompositeType(
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +000071 DIDerivedType(D).getTypeDerivedFrom().resolve(EmptyMap));
Bill Wendling523bea82013-11-08 08:13:15 +000072 }
73
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +000074 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +000075}
76
Bill Wendling523bea82013-11-08 08:13:15 +000077DITypeIdentifierMap
78llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
79 DITypeIdentifierMap Map;
80 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
Duncan P. N. Exon Smith35ef22c2015-04-15 23:19:27 +000081 auto *CU = cast<MDCompileUnit>(CU_Nodes->getOperand(CUi));
82 DIArray Retain = CU->getRetainedTypes();
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +000083 for (unsigned Ti = 0, Te = Retain.size(); Ti != Te; ++Ti) {
84 if (!isa<MDCompositeType>(Retain[Ti]))
Bill Wendling523bea82013-11-08 08:13:15 +000085 continue;
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +000086 DICompositeType Ty = cast<MDCompositeType>(Retain[Ti]);
Bill Wendling523bea82013-11-08 08:13:15 +000087 if (MDString *TypeId = Ty.getIdentifier()) {
88 // Definition has priority over declaration.
89 // Try to insert (TypeId, Ty) to Map.
90 std::pair<DITypeIdentifierMap::iterator, bool> P =
91 Map.insert(std::make_pair(TypeId, Ty));
92 // If TypeId already exists in Map and this is a definition, replace
93 // whatever we had (declaration or definition) with the definition.
94 if (!P.second && !Ty.isForwardDecl())
95 P.first->second = Ty;
96 }
97 }
98 }
99 return Map;
100}
101
102//===----------------------------------------------------------------------===//
103// DebugInfoFinder implementations.
104//===----------------------------------------------------------------------===//
105
106void DebugInfoFinder::reset() {
107 CUs.clear();
108 SPs.clear();
109 GVs.clear();
110 TYs.clear();
111 Scopes.clear();
112 NodesSeen.clear();
113 TypeIdentifierMap.clear();
Manman Ren2085ccc2013-11-17 18:42:37 +0000114 TypeMapInitialized = false;
115}
116
Manman Renb46e5502013-11-17 19:35:03 +0000117void DebugInfoFinder::InitializeTypeMap(const Module &M) {
Manman Ren2085ccc2013-11-17 18:42:37 +0000118 if (!TypeMapInitialized)
119 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
120 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
121 TypeMapInitialized = true;
122 }
Bill Wendling523bea82013-11-08 08:13:15 +0000123}
124
Bill Wendling523bea82013-11-08 08:13:15 +0000125void DebugInfoFinder::processModule(const Module &M) {
Manman Renb46e5502013-11-17 19:35:03 +0000126 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000127 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
Bill Wendling523bea82013-11-08 08:13:15 +0000128 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000129 DICompileUnit CU = cast<MDCompileUnit>(CU_Nodes->getOperand(i));
Bill Wendling523bea82013-11-08 08:13:15 +0000130 addCompileUnit(CU);
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000131 for (DIGlobalVariable DIG : CU->getGlobalVariables()) {
Bill Wendling523bea82013-11-08 08:13:15 +0000132 if (addGlobalVariable(DIG)) {
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000133 processScope(DIG->getScope());
134 processType(DIG->getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000135 }
136 }
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000137 for (auto *SP : CU->getSubprograms())
138 processSubprogram(SP);
139 for (auto *ET : CU->getEnumTypes())
140 processType(ET);
141 for (auto *RT : CU->getRetainedTypes())
142 processType(RT);
143 for (DIImportedEntity Import : CU->getImportedEntities()) {
Duncan P. N. Exon Smithde8e4272015-04-14 01:46:44 +0000144 auto *Entity = Import->getEntity().resolve(TypeIdentifierMap);
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000145 if (auto *T = dyn_cast<MDType>(Entity))
146 processType(T);
147 else if (auto *SP = dyn_cast<MDSubprogram>(Entity))
148 processSubprogram(SP);
149 else if (auto *NS = dyn_cast<MDNamespace>(Entity))
150 processScope(NS->getScope());
Bill Wendling523bea82013-11-08 08:13:15 +0000151 }
152 }
153 }
154}
155
Manman Ren2085ccc2013-11-17 18:42:37 +0000156void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) {
Bill Wendling523bea82013-11-08 08:13:15 +0000157 if (!Loc)
158 return;
Manman Renb46e5502013-11-17 19:35:03 +0000159 InitializeTypeMap(M);
Duncan P. N. Exon Smithb7e221b2015-04-14 01:35:55 +0000160 processScope(Loc->getScope());
161 processLocation(M, Loc->getInlinedAt());
Bill Wendling523bea82013-11-08 08:13:15 +0000162}
163
Bill Wendling523bea82013-11-08 08:13:15 +0000164void DebugInfoFinder::processType(DIType DT) {
165 if (!addType(DT))
166 return;
167 processScope(DT.getContext().resolve(TypeIdentifierMap));
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000168 if (DICompositeType DCT = dyn_cast<MDCompositeTypeBase>(DT)) {
Bill Wendling523bea82013-11-08 08:13:15 +0000169 processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000170 if (DISubroutineType ST = dyn_cast<MDSubroutineType>(DCT)) {
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000171 for (MDTypeRef Ref : ST->getTypeArray())
172 processType(Ref.resolve(TypeIdentifierMap));
Manman Renf8a19672014-07-28 22:24:06 +0000173 return;
174 }
Anders Waldenborg1433fd42015-04-14 09:18:17 +0000175 for (Metadata *D : DCT->getElements()) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000176 if (DIType T = dyn_cast<MDType>(D))
177 processType(T);
178 else if (DISubprogram SP = dyn_cast<MDSubprogram>(D))
179 processSubprogram(SP);
Bill Wendling523bea82013-11-08 08:13:15 +0000180 }
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000181 } else if (DIDerivedType DDT = dyn_cast<MDDerivedTypeBase>(DT)) {
Bill Wendling523bea82013-11-08 08:13:15 +0000182 processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
183 }
184}
185
186void DebugInfoFinder::processScope(DIScope Scope) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000187 if (!Scope)
188 return;
189 if (DIType Ty = dyn_cast<MDType>(Scope)) {
Bill Wendling523bea82013-11-08 08:13:15 +0000190 processType(Ty);
191 return;
192 }
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000193 if (DICompileUnit CU = dyn_cast<MDCompileUnit>(Scope)) {
194 addCompileUnit(CU);
Bill Wendling523bea82013-11-08 08:13:15 +0000195 return;
196 }
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000197 if (DISubprogram SP = dyn_cast<MDSubprogram>(Scope)) {
198 processSubprogram(SP);
Bill Wendling523bea82013-11-08 08:13:15 +0000199 return;
200 }
201 if (!addScope(Scope))
202 return;
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000203 if (auto *LB = dyn_cast<MDLexicalBlockBase>(Scope)) {
204 processScope(LB->getScope());
Duncan P. N. Exon Smith20caafb2015-04-14 03:01:27 +0000205 } else if (auto *NS = dyn_cast<MDNamespace>(Scope)) {
206 processScope(NS->getScope());
Bill Wendling523bea82013-11-08 08:13:15 +0000207 }
208}
209
Bill Wendling523bea82013-11-08 08:13:15 +0000210void DebugInfoFinder::processSubprogram(DISubprogram SP) {
211 if (!addSubprogram(SP))
212 return;
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000213 processScope(SP->getScope().resolve(TypeIdentifierMap));
214 processType(SP->getType());
215 for (auto *Element : SP->getTemplateParams()) {
Duncan P. N. Exon Smith20caafb2015-04-14 03:01:27 +0000216 if (auto *TType = dyn_cast<MDTemplateTypeParameter>(Element)) {
217 processType(TType->getType().resolve(TypeIdentifierMap));
218 } else if (auto *TVal = dyn_cast<MDTemplateValueParameter>(Element)) {
219 processType(TVal->getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000220 }
221 }
222}
223
Manman Ren2085ccc2013-11-17 18:42:37 +0000224void DebugInfoFinder::processDeclare(const Module &M,
225 const DbgDeclareInst *DDI) {
Bill Wendling523bea82013-11-08 08:13:15 +0000226 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
227 if (!N)
228 return;
Manman Renb46e5502013-11-17 19:35:03 +0000229 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000230
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000231 DIVariable DV = dyn_cast<MDLocalVariable>(N);
232 if (!DV)
Bill Wendling523bea82013-11-08 08:13:15 +0000233 return;
234
David Blaikie70573dc2014-11-19 07:49:26 +0000235 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000236 return;
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000237 processScope(DV->getScope());
238 processType(DV->getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000239}
240
Manman Ren2085ccc2013-11-17 18:42:37 +0000241void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
Bill Wendling523bea82013-11-08 08:13:15 +0000242 MDNode *N = dyn_cast<MDNode>(DVI->getVariable());
243 if (!N)
244 return;
Manman Renb46e5502013-11-17 19:35:03 +0000245 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000246
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000247 DIVariable DV = dyn_cast<MDLocalVariable>(N);
248 if (!DV)
Bill Wendling523bea82013-11-08 08:13:15 +0000249 return;
250
David Blaikie70573dc2014-11-19 07:49:26 +0000251 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000252 return;
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000253 processScope(DV->getScope());
254 processType(DV->getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000255}
256
Bill Wendling523bea82013-11-08 08:13:15 +0000257bool DebugInfoFinder::addType(DIType DT) {
258 if (!DT)
259 return false;
260
David Blaikie70573dc2014-11-19 07:49:26 +0000261 if (!NodesSeen.insert(DT).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000262 return false;
263
264 TYs.push_back(DT);
265 return true;
266}
267
Bill Wendling523bea82013-11-08 08:13:15 +0000268bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
269 if (!CU)
270 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000271 if (!NodesSeen.insert(CU).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000272 return false;
273
274 CUs.push_back(CU);
275 return true;
276}
277
Bill Wendling523bea82013-11-08 08:13:15 +0000278bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
279 if (!DIG)
280 return false;
281
David Blaikie70573dc2014-11-19 07:49:26 +0000282 if (!NodesSeen.insert(DIG).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000283 return false;
284
285 GVs.push_back(DIG);
286 return true;
287}
288
Bill Wendling523bea82013-11-08 08:13:15 +0000289bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
290 if (!SP)
291 return false;
292
David Blaikie70573dc2014-11-19 07:49:26 +0000293 if (!NodesSeen.insert(SP).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000294 return false;
295
296 SPs.push_back(SP);
297 return true;
298}
299
300bool DebugInfoFinder::addScope(DIScope Scope) {
301 if (!Scope)
302 return false;
303 // FIXME: Ocaml binding generates a scope with no content, we treat it
304 // as null for now.
305 if (Scope->getNumOperands() == 0)
306 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000307 if (!NodesSeen.insert(Scope).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000308 return false;
309 Scopes.push_back(Scope);
310 return true;
311}
312
313//===----------------------------------------------------------------------===//
314// DIDescriptor: dump routines for all descriptors.
315//===----------------------------------------------------------------------===//
316
Bill Wendling523bea82013-11-08 08:13:15 +0000317void DIDescriptor::dump() const {
318 print(dbgs());
319 dbgs() << '\n';
320}
321
Bill Wendling523bea82013-11-08 08:13:15 +0000322void DIDescriptor::print(raw_ostream &OS) const {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000323 if (!get())
Bill Wendling523bea82013-11-08 08:13:15 +0000324 return;
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000325 get()->print(OS);
Bill Wendling523bea82013-11-08 08:13:15 +0000326}
327
Duncan P. N. Exon Smith5bf3cdc2015-04-06 22:27:37 +0000328template <>
329DIDescriptor
330DIRef<DIDescriptor>::resolve(const DITypeIdentifierMap &Map) const {
331 return DIDescriptor(DebugNodeRef(Val).resolve(Map));
332}
333template <>
334DIScope DIRef<DIScope>::resolve(const DITypeIdentifierMap &Map) const {
335 return MDScopeRef(Val).resolve(Map);
336}
337template <>
338DIType DIRef<DIType>::resolve(const DITypeIdentifierMap &Map) const {
339 return MDTypeRef(Val).resolve(Map);
340}
341
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000342bool llvm::stripDebugInfo(Function &F) {
343 bool Changed = false;
344 for (BasicBlock &BB : F) {
345 for (Instruction &I : BB) {
346 if (I.getDebugLoc()) {
347 Changed = true;
348 I.setDebugLoc(DebugLoc());
349 }
350 }
351 }
352 return Changed;
353}
354
Manman Rencb14bbc2013-11-22 22:06:31 +0000355bool llvm::StripDebugInfo(Module &M) {
Manman Rencb14bbc2013-11-22 22:06:31 +0000356 bool Changed = false;
357
358 // Remove all of the calls to the debugger intrinsics, and remove them from
359 // the module.
360 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
361 while (!Declare->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000362 CallInst *CI = cast<CallInst>(Declare->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +0000363 CI->eraseFromParent();
364 }
365 Declare->eraseFromParent();
366 Changed = true;
367 }
368
369 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
370 while (!DbgVal->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000371 CallInst *CI = cast<CallInst>(DbgVal->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +0000372 CI->eraseFromParent();
373 }
374 DbgVal->eraseFromParent();
375 Changed = true;
376 }
377
378 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
379 NME = M.named_metadata_end(); NMI != NME;) {
380 NamedMDNode *NMD = NMI;
381 ++NMI;
382 if (NMD->getName().startswith("llvm.dbg.")) {
383 NMD->eraseFromParent();
384 Changed = true;
385 }
386 }
387
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000388 for (Function &F : M)
389 Changed |= stripDebugInfo(F);
390
Rafael Espindola468b8682015-04-01 14:44:59 +0000391 if (GVMaterializer *Materializer = M.getMaterializer())
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000392 Materializer->setStripDebugInfo();
Manman Rencb14bbc2013-11-22 22:06:31 +0000393
394 return Changed;
395}
Manman Ren8b4306c2013-12-02 21:29:56 +0000396
Manman Renbd4daf82013-12-03 00:12:14 +0000397unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
David Majnemere7a9cdb2015-02-16 06:04:53 +0000398 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000399 M.getModuleFlag("Debug Info Version")))
400 return Val->getZExtValue();
401 return 0;
Manman Ren8b4306c2013-12-02 21:29:56 +0000402}
David Blaikie6876b3b2014-07-01 20:05:26 +0000403
David Blaikiea8c35092014-07-02 18:30:05 +0000404llvm::DenseMap<const llvm::Function *, llvm::DISubprogram>
405llvm::makeSubprogramMap(const Module &M) {
406 DenseMap<const Function *, DISubprogram> R;
David Blaikie6876b3b2014-07-01 20:05:26 +0000407
408 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
409 if (!CU_Nodes)
410 return R;
411
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000412 for (MDNode *N : CU_Nodes->operands()) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000413 DICompileUnit CUNode = cast<MDCompileUnit>(N);
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000414 for (DISubprogram SP : CUNode->getSubprograms()) {
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000415 if (Function *F = SP->getFunction())
David Blaikie6876b3b2014-07-01 20:05:26 +0000416 R.insert(std::make_pair(F, SP));
417 }
418 }
419 return R;
420}