blob: 2e161c2ba6c12a332bbbf87edf05dae793e868a0 [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 Smitha9308c42015-04-29 16:38:44 +000036DISubprogram *llvm::getDISubprogram(const MDNode *Scope) {
37 if (auto *LocalScope = dyn_cast_or_null<DILocalScope>(Scope))
Duncan P. N. Exon Smithdd77af82015-03-31 02:06:28 +000038 return LocalScope->getSubprogram();
39 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +000040}
41
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000042DISubprogram *llvm::getDISubprogram(const Function *F) {
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +000043 // We look for the first instr that has a debug annotation leading back to F.
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +000044 for (auto &BB : *F) {
David Majnemerc758df42014-11-01 07:57:14 +000045 auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) {
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +000046 return Inst.getDebugLoc();
David Majnemerc758df42014-11-01 07:57:14 +000047 });
48 if (Inst == BB.end())
49 continue;
50 DebugLoc DLoc = Inst->getDebugLoc();
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +000051 const MDNode *Scope = DLoc.getInlinedAtScope();
Duncan P. N. Exon Smithed557b52015-04-17 23:20:10 +000052 auto *Subprogram = getDISubprogram(Scope);
53 return Subprogram->describes(F) ? Subprogram : nullptr;
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +000054 }
55
Duncan P. N. Exon Smithed557b52015-04-17 23:20:10 +000056 return nullptr;
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +000057}
58
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000059DICompositeTypeBase *llvm::getDICompositeType(DIType *T) {
60 if (auto *C = dyn_cast_or_null<DICompositeTypeBase>(T))
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +000061 return C;
Bill Wendling523bea82013-11-08 08:13:15 +000062
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000063 if (auto *D = dyn_cast_or_null<DIDerivedTypeBase>(T)) {
Bill Wendling523bea82013-11-08 08:13:15 +000064 // This function is currently used by dragonegg and dragonegg does
65 // not generate identifier for types, so using an empty map to resolve
66 // DerivedFrom should be fine.
67 DITypeIdentifierMap EmptyMap;
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +000068 return getDICompositeType(D->getBaseType().resolve(EmptyMap));
Bill Wendling523bea82013-11-08 08:13:15 +000069 }
70
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +000071 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +000072}
73
Bill Wendling523bea82013-11-08 08:13:15 +000074DITypeIdentifierMap
75llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
76 DITypeIdentifierMap Map;
77 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000078 auto *CU = cast<DICompileUnit>(CU_Nodes->getOperand(CUi));
79 DINodeArray Retain = CU->getRetainedTypes();
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +000080 for (unsigned Ti = 0, Te = Retain.size(); Ti != Te; ++Ti) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000081 if (!isa<DICompositeType>(Retain[Ti]))
Bill Wendling523bea82013-11-08 08:13:15 +000082 continue;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000083 auto *Ty = cast<DICompositeType>(Retain[Ti]);
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +000084 if (MDString *TypeId = Ty->getRawIdentifier()) {
Bill Wendling523bea82013-11-08 08:13:15 +000085 // Definition has priority over declaration.
86 // Try to insert (TypeId, Ty) to Map.
87 std::pair<DITypeIdentifierMap::iterator, bool> P =
88 Map.insert(std::make_pair(TypeId, Ty));
89 // If TypeId already exists in Map and this is a definition, replace
90 // whatever we had (declaration or definition) with the definition.
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +000091 if (!P.second && !Ty->isForwardDecl())
Bill Wendling523bea82013-11-08 08:13:15 +000092 P.first->second = Ty;
93 }
94 }
95 }
96 return Map;
97}
98
99//===----------------------------------------------------------------------===//
100// DebugInfoFinder implementations.
101//===----------------------------------------------------------------------===//
102
103void DebugInfoFinder::reset() {
104 CUs.clear();
105 SPs.clear();
106 GVs.clear();
107 TYs.clear();
108 Scopes.clear();
109 NodesSeen.clear();
110 TypeIdentifierMap.clear();
Manman Ren2085ccc2013-11-17 18:42:37 +0000111 TypeMapInitialized = false;
112}
113
Manman Renb46e5502013-11-17 19:35:03 +0000114void DebugInfoFinder::InitializeTypeMap(const Module &M) {
Manman Ren2085ccc2013-11-17 18:42:37 +0000115 if (!TypeMapInitialized)
116 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
117 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
118 TypeMapInitialized = true;
119 }
Bill Wendling523bea82013-11-08 08:13:15 +0000120}
121
Bill Wendling523bea82013-11-08 08:13:15 +0000122void DebugInfoFinder::processModule(const Module &M) {
Manman Renb46e5502013-11-17 19:35:03 +0000123 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000124 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
Bill Wendling523bea82013-11-08 08:13:15 +0000125 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000126 auto *CU = cast<DICompileUnit>(CU_Nodes->getOperand(i));
Bill Wendling523bea82013-11-08 08:13:15 +0000127 addCompileUnit(CU);
Duncan P. N. Exon Smithed557b52015-04-17 23:20:10 +0000128 for (auto *DIG : CU->getGlobalVariables()) {
Bill Wendling523bea82013-11-08 08:13:15 +0000129 if (addGlobalVariable(DIG)) {
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000130 processScope(DIG->getScope());
131 processType(DIG->getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000132 }
133 }
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000134 for (auto *SP : CU->getSubprograms())
135 processSubprogram(SP);
136 for (auto *ET : CU->getEnumTypes())
137 processType(ET);
138 for (auto *RT : CU->getRetainedTypes())
139 processType(RT);
Duncan P. N. Exon Smithed557b52015-04-17 23:20:10 +0000140 for (auto *Import : CU->getImportedEntities()) {
Duncan P. N. Exon Smithde8e4272015-04-14 01:46:44 +0000141 auto *Entity = Import->getEntity().resolve(TypeIdentifierMap);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000142 if (auto *T = dyn_cast<DIType>(Entity))
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000143 processType(T);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000144 else if (auto *SP = dyn_cast<DISubprogram>(Entity))
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000145 processSubprogram(SP);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000146 else if (auto *NS = dyn_cast<DINamespace>(Entity))
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000147 processScope(NS->getScope());
Bill Wendling523bea82013-11-08 08:13:15 +0000148 }
149 }
150 }
151}
152
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000153void DebugInfoFinder::processLocation(const Module &M, const DILocation *Loc) {
Bill Wendling523bea82013-11-08 08:13:15 +0000154 if (!Loc)
155 return;
Manman Renb46e5502013-11-17 19:35:03 +0000156 InitializeTypeMap(M);
Duncan P. N. Exon Smithb7e221b2015-04-14 01:35:55 +0000157 processScope(Loc->getScope());
158 processLocation(M, Loc->getInlinedAt());
Bill Wendling523bea82013-11-08 08:13:15 +0000159}
160
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000161void DebugInfoFinder::processType(DIType *DT) {
Bill Wendling523bea82013-11-08 08:13:15 +0000162 if (!addType(DT))
163 return;
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000164 processScope(DT->getScope().resolve(TypeIdentifierMap));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000165 if (auto *DCT = dyn_cast<DICompositeTypeBase>(DT)) {
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000166 processType(DCT->getBaseType().resolve(TypeIdentifierMap));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000167 if (auto *ST = dyn_cast<DISubroutineType>(DCT)) {
168 for (DITypeRef Ref : ST->getTypeArray())
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000169 processType(Ref.resolve(TypeIdentifierMap));
Manman Renf8a19672014-07-28 22:24:06 +0000170 return;
171 }
Anders Waldenborg1433fd42015-04-14 09:18:17 +0000172 for (Metadata *D : DCT->getElements()) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000173 if (auto *T = dyn_cast<DIType>(D))
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000174 processType(T);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000175 else if (auto *SP = dyn_cast<DISubprogram>(D))
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000176 processSubprogram(SP);
Bill Wendling523bea82013-11-08 08:13:15 +0000177 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000178 } else if (auto *DDT = dyn_cast<DIDerivedTypeBase>(DT)) {
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000179 processType(DDT->getBaseType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000180 }
181}
182
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000183void DebugInfoFinder::processScope(DIScope *Scope) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000184 if (!Scope)
185 return;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000186 if (auto *Ty = dyn_cast<DIType>(Scope)) {
Bill Wendling523bea82013-11-08 08:13:15 +0000187 processType(Ty);
188 return;
189 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000190 if (auto *CU = dyn_cast<DICompileUnit>(Scope)) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000191 addCompileUnit(CU);
Bill Wendling523bea82013-11-08 08:13:15 +0000192 return;
193 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000194 if (auto *SP = dyn_cast<DISubprogram>(Scope)) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000195 processSubprogram(SP);
Bill Wendling523bea82013-11-08 08:13:15 +0000196 return;
197 }
198 if (!addScope(Scope))
199 return;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000200 if (auto *LB = dyn_cast<DILexicalBlockBase>(Scope)) {
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000201 processScope(LB->getScope());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000202 } else if (auto *NS = dyn_cast<DINamespace>(Scope)) {
Duncan P. N. Exon Smith20caafb2015-04-14 03:01:27 +0000203 processScope(NS->getScope());
Bill Wendling523bea82013-11-08 08:13:15 +0000204 }
205}
206
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000207void DebugInfoFinder::processSubprogram(DISubprogram *SP) {
Bill Wendling523bea82013-11-08 08:13:15 +0000208 if (!addSubprogram(SP))
209 return;
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000210 processScope(SP->getScope().resolve(TypeIdentifierMap));
211 processType(SP->getType());
212 for (auto *Element : SP->getTemplateParams()) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000213 if (auto *TType = dyn_cast<DITemplateTypeParameter>(Element)) {
Duncan P. N. Exon Smith20caafb2015-04-14 03:01:27 +0000214 processType(TType->getType().resolve(TypeIdentifierMap));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000215 } else if (auto *TVal = dyn_cast<DITemplateValueParameter>(Element)) {
Duncan P. N. Exon Smith20caafb2015-04-14 03:01:27 +0000216 processType(TVal->getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000217 }
218 }
219}
220
Manman Ren2085ccc2013-11-17 18:42:37 +0000221void DebugInfoFinder::processDeclare(const Module &M,
222 const DbgDeclareInst *DDI) {
Duncan P. N. Exon Smithed557b52015-04-17 23:20:10 +0000223 auto *N = dyn_cast<MDNode>(DDI->getVariable());
Bill Wendling523bea82013-11-08 08:13:15 +0000224 if (!N)
225 return;
Manman Renb46e5502013-11-17 19:35:03 +0000226 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000227
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000228 auto *DV = dyn_cast<DILocalVariable>(N);
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000229 if (!DV)
Bill Wendling523bea82013-11-08 08:13:15 +0000230 return;
231
David Blaikie70573dc2014-11-19 07:49:26 +0000232 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000233 return;
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000234 processScope(DV->getScope());
235 processType(DV->getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000236}
237
Manman Ren2085ccc2013-11-17 18:42:37 +0000238void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
Duncan P. N. Exon Smithed557b52015-04-17 23:20:10 +0000239 auto *N = dyn_cast<MDNode>(DVI->getVariable());
Bill Wendling523bea82013-11-08 08:13:15 +0000240 if (!N)
241 return;
Manman Renb46e5502013-11-17 19:35:03 +0000242 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000243
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000244 auto *DV = dyn_cast<DILocalVariable>(N);
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000245 if (!DV)
Bill Wendling523bea82013-11-08 08:13:15 +0000246 return;
247
David Blaikie70573dc2014-11-19 07:49:26 +0000248 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000249 return;
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000250 processScope(DV->getScope());
251 processType(DV->getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000252}
253
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000254bool DebugInfoFinder::addType(DIType *DT) {
Bill Wendling523bea82013-11-08 08:13:15 +0000255 if (!DT)
256 return false;
257
David Blaikie70573dc2014-11-19 07:49:26 +0000258 if (!NodesSeen.insert(DT).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000259 return false;
260
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000261 TYs.push_back(const_cast<DIType *>(DT));
Bill Wendling523bea82013-11-08 08:13:15 +0000262 return true;
263}
264
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000265bool DebugInfoFinder::addCompileUnit(DICompileUnit *CU) {
Bill Wendling523bea82013-11-08 08:13:15 +0000266 if (!CU)
267 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000268 if (!NodesSeen.insert(CU).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000269 return false;
270
271 CUs.push_back(CU);
272 return true;
273}
274
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000275bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable *DIG) {
Bill Wendling523bea82013-11-08 08:13:15 +0000276 if (!DIG)
277 return false;
278
David Blaikie70573dc2014-11-19 07:49:26 +0000279 if (!NodesSeen.insert(DIG).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000280 return false;
281
282 GVs.push_back(DIG);
283 return true;
284}
285
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000286bool DebugInfoFinder::addSubprogram(DISubprogram *SP) {
Bill Wendling523bea82013-11-08 08:13:15 +0000287 if (!SP)
288 return false;
289
David Blaikie70573dc2014-11-19 07:49:26 +0000290 if (!NodesSeen.insert(SP).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000291 return false;
292
293 SPs.push_back(SP);
294 return true;
295}
296
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000297bool DebugInfoFinder::addScope(DIScope *Scope) {
Bill Wendling523bea82013-11-08 08:13:15 +0000298 if (!Scope)
299 return false;
300 // FIXME: Ocaml binding generates a scope with no content, we treat it
301 // as null for now.
302 if (Scope->getNumOperands() == 0)
303 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000304 if (!NodesSeen.insert(Scope).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000305 return false;
306 Scopes.push_back(Scope);
307 return true;
308}
309
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000310bool llvm::stripDebugInfo(Function &F) {
311 bool Changed = false;
312 for (BasicBlock &BB : F) {
313 for (Instruction &I : BB) {
314 if (I.getDebugLoc()) {
315 Changed = true;
316 I.setDebugLoc(DebugLoc());
317 }
318 }
319 }
320 return Changed;
321}
322
Manman Rencb14bbc2013-11-22 22:06:31 +0000323bool llvm::StripDebugInfo(Module &M) {
Manman Rencb14bbc2013-11-22 22:06:31 +0000324 bool Changed = false;
325
326 // Remove all of the calls to the debugger intrinsics, and remove them from
327 // the module.
328 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
329 while (!Declare->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000330 CallInst *CI = cast<CallInst>(Declare->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +0000331 CI->eraseFromParent();
332 }
333 Declare->eraseFromParent();
334 Changed = true;
335 }
336
337 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
338 while (!DbgVal->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000339 CallInst *CI = cast<CallInst>(DbgVal->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +0000340 CI->eraseFromParent();
341 }
342 DbgVal->eraseFromParent();
343 Changed = true;
344 }
345
346 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
347 NME = M.named_metadata_end(); NMI != NME;) {
348 NamedMDNode *NMD = NMI;
349 ++NMI;
350 if (NMD->getName().startswith("llvm.dbg.")) {
351 NMD->eraseFromParent();
352 Changed = true;
353 }
354 }
355
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000356 for (Function &F : M)
357 Changed |= stripDebugInfo(F);
358
Rafael Espindola468b8682015-04-01 14:44:59 +0000359 if (GVMaterializer *Materializer = M.getMaterializer())
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000360 Materializer->setStripDebugInfo();
Manman Rencb14bbc2013-11-22 22:06:31 +0000361
362 return Changed;
363}
Manman Ren8b4306c2013-12-02 21:29:56 +0000364
Manman Renbd4daf82013-12-03 00:12:14 +0000365unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
David Majnemere7a9cdb2015-02-16 06:04:53 +0000366 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000367 M.getModuleFlag("Debug Info Version")))
368 return Val->getZExtValue();
369 return 0;
Manman Ren8b4306c2013-12-02 21:29:56 +0000370}
David Blaikie6876b3b2014-07-01 20:05:26 +0000371
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000372DenseMap<const llvm::Function *, DISubprogram *>
David Blaikiea8c35092014-07-02 18:30:05 +0000373llvm::makeSubprogramMap(const Module &M) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000374 DenseMap<const Function *, DISubprogram *> R;
David Blaikie6876b3b2014-07-01 20:05:26 +0000375
376 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
377 if (!CU_Nodes)
378 return R;
379
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000380 for (MDNode *N : CU_Nodes->operands()) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000381 auto *CUNode = cast<DICompileUnit>(N);
Duncan P. N. Exon Smithed557b52015-04-17 23:20:10 +0000382 for (auto *SP : CUNode->getSubprograms()) {
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000383 if (Function *F = SP->getFunction())
David Blaikie6876b3b2014-07-01 20:05:26 +0000384 R.insert(std::make_pair(F, SP));
385 }
386 }
387 return R;
388}