blob: 3ffd13064d4d4b2f487bf50e0714507184a31bca [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
Bill Wendling523bea82013-11-08 08:13:15 +000036//===----------------------------------------------------------------------===//
Bill Wendling523bea82013-11-08 08:13:15 +000037// Simple Descriptor Constructors and other Methods
38//===----------------------------------------------------------------------===//
39
Duncan P. N. Exon Smith930f3882015-04-06 18:02:43 +000040DIScopeRef DIScope::getRef() const { return MDScopeRef::get(get()); }
Bill Wendling523bea82013-11-08 08:13:15 +000041
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +000042void DICompileUnit::replaceSubprograms(DIArray Subprograms) {
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +000043 get()->replaceSubprograms(MDSubprogramArray(Subprograms));
Duncan P. N. Exon Smith176b6912014-10-03 20:01:09 +000044}
45
46void DICompileUnit::replaceGlobalVariables(DIArray GlobalVariables) {
Duncan P. N. Exon Smith11344732015-04-07 16:50:39 +000047 get()->replaceGlobalVariables(MDGlobalVariableArray(GlobalVariables));
Bill Wendling523bea82013-11-08 08:13:15 +000048}
49
Bill Wendling523bea82013-11-08 08:13:15 +000050DIVariable llvm::createInlinedVariable(MDNode *DV, MDNode *InlinedScope,
51 LLVMContext &VMContext) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +000052 return cast<MDLocalVariable>(DV)
53 ->withInline(cast_or_null<MDLocation>(InlinedScope));
Bill Wendling523bea82013-11-08 08:13:15 +000054}
55
Bill Wendling523bea82013-11-08 08:13:15 +000056DIVariable llvm::cleanseInlinedVariable(MDNode *DV, LLVMContext &VMContext) {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +000057 return cast<MDLocalVariable>(DV)->withoutInline();
Bill Wendling523bea82013-11-08 08:13:15 +000058}
59
Bill Wendling523bea82013-11-08 08:13:15 +000060DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
Duncan P. N. Exon Smithdd77af82015-03-31 02:06:28 +000061 if (auto *LocalScope = dyn_cast_or_null<MDLocalScope>(Scope))
62 return LocalScope->getSubprogram();
63 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +000064}
65
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +000066DISubprogram llvm::getDISubprogram(const Function *F) {
67 // We look for the first instr that has a debug annotation leading back to F.
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +000068 for (auto &BB : *F) {
David Majnemerc758df42014-11-01 07:57:14 +000069 auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) {
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +000070 return Inst.getDebugLoc();
David Majnemerc758df42014-11-01 07:57:14 +000071 });
72 if (Inst == BB.end())
73 continue;
74 DebugLoc DLoc = Inst->getDebugLoc();
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +000075 const MDNode *Scope = DLoc.getInlinedAtScope();
David Majnemerc758df42014-11-01 07:57:14 +000076 DISubprogram Subprogram = getDISubprogram(Scope);
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +000077 return Subprogram->describes(F) ? Subprogram : DISubprogram();
Timur Iskhodzhanoveb229ca2014-10-23 23:46:28 +000078 }
79
80 return DISubprogram();
81}
82
Bill Wendling523bea82013-11-08 08:13:15 +000083DICompositeType llvm::getDICompositeType(DIType T) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +000084 if (auto *C = dyn_cast_or_null<MDCompositeTypeBase>(T))
85 return C;
Bill Wendling523bea82013-11-08 08:13:15 +000086
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +000087 if (auto *D = dyn_cast_or_null<MDDerivedTypeBase>(T)) {
Bill Wendling523bea82013-11-08 08:13:15 +000088 // This function is currently used by dragonegg and dragonegg does
89 // not generate identifier for types, so using an empty map to resolve
90 // DerivedFrom should be fine.
91 DITypeIdentifierMap EmptyMap;
92 return getDICompositeType(
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +000093 DIDerivedType(D).getTypeDerivedFrom().resolve(EmptyMap));
Bill Wendling523bea82013-11-08 08:13:15 +000094 }
95
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +000096 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +000097}
98
Bill Wendling523bea82013-11-08 08:13:15 +000099DITypeIdentifierMap
100llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
101 DITypeIdentifierMap Map;
102 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000103 DICompileUnit CU = cast<MDCompileUnit>(CU_Nodes->getOperand(CUi));
Bill Wendling523bea82013-11-08 08:13:15 +0000104 DIArray Retain = CU.getRetainedTypes();
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000105 for (unsigned Ti = 0, Te = Retain.size(); Ti != Te; ++Ti) {
106 if (!isa<MDCompositeType>(Retain[Ti]))
Bill Wendling523bea82013-11-08 08:13:15 +0000107 continue;
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000108 DICompositeType Ty = cast<MDCompositeType>(Retain[Ti]);
Bill Wendling523bea82013-11-08 08:13:15 +0000109 if (MDString *TypeId = Ty.getIdentifier()) {
110 // Definition has priority over declaration.
111 // Try to insert (TypeId, Ty) to Map.
112 std::pair<DITypeIdentifierMap::iterator, bool> P =
113 Map.insert(std::make_pair(TypeId, Ty));
114 // If TypeId already exists in Map and this is a definition, replace
115 // whatever we had (declaration or definition) with the definition.
116 if (!P.second && !Ty.isForwardDecl())
117 P.first->second = Ty;
118 }
119 }
120 }
121 return Map;
122}
123
124//===----------------------------------------------------------------------===//
125// DebugInfoFinder implementations.
126//===----------------------------------------------------------------------===//
127
128void DebugInfoFinder::reset() {
129 CUs.clear();
130 SPs.clear();
131 GVs.clear();
132 TYs.clear();
133 Scopes.clear();
134 NodesSeen.clear();
135 TypeIdentifierMap.clear();
Manman Ren2085ccc2013-11-17 18:42:37 +0000136 TypeMapInitialized = false;
137}
138
Manman Renb46e5502013-11-17 19:35:03 +0000139void DebugInfoFinder::InitializeTypeMap(const Module &M) {
Manman Ren2085ccc2013-11-17 18:42:37 +0000140 if (!TypeMapInitialized)
141 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
142 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
143 TypeMapInitialized = true;
144 }
Bill Wendling523bea82013-11-08 08:13:15 +0000145}
146
Bill Wendling523bea82013-11-08 08:13:15 +0000147void DebugInfoFinder::processModule(const Module &M) {
Manman Renb46e5502013-11-17 19:35:03 +0000148 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000149 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
Bill Wendling523bea82013-11-08 08:13:15 +0000150 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000151 DICompileUnit CU = cast<MDCompileUnit>(CU_Nodes->getOperand(i));
Bill Wendling523bea82013-11-08 08:13:15 +0000152 addCompileUnit(CU);
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000153 for (DIGlobalVariable DIG : CU->getGlobalVariables()) {
Bill Wendling523bea82013-11-08 08:13:15 +0000154 if (addGlobalVariable(DIG)) {
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000155 processScope(DIG->getScope());
156 processType(DIG->getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000157 }
158 }
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000159 for (auto *SP : CU->getSubprograms())
160 processSubprogram(SP);
161 for (auto *ET : CU->getEnumTypes())
162 processType(ET);
163 for (auto *RT : CU->getRetainedTypes())
164 processType(RT);
165 for (DIImportedEntity Import : CU->getImportedEntities()) {
Duncan P. N. Exon Smithde8e4272015-04-14 01:46:44 +0000166 auto *Entity = Import->getEntity().resolve(TypeIdentifierMap);
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000167 if (auto *T = dyn_cast<MDType>(Entity))
168 processType(T);
169 else if (auto *SP = dyn_cast<MDSubprogram>(Entity))
170 processSubprogram(SP);
171 else if (auto *NS = dyn_cast<MDNamespace>(Entity))
172 processScope(NS->getScope());
Bill Wendling523bea82013-11-08 08:13:15 +0000173 }
174 }
175 }
176}
177
Manman Ren2085ccc2013-11-17 18:42:37 +0000178void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) {
Bill Wendling523bea82013-11-08 08:13:15 +0000179 if (!Loc)
180 return;
Manman Renb46e5502013-11-17 19:35:03 +0000181 InitializeTypeMap(M);
Duncan P. N. Exon Smithb7e221b2015-04-14 01:35:55 +0000182 processScope(Loc->getScope());
183 processLocation(M, Loc->getInlinedAt());
Bill Wendling523bea82013-11-08 08:13:15 +0000184}
185
Bill Wendling523bea82013-11-08 08:13:15 +0000186void DebugInfoFinder::processType(DIType DT) {
187 if (!addType(DT))
188 return;
189 processScope(DT.getContext().resolve(TypeIdentifierMap));
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000190 if (DICompositeType DCT = dyn_cast<MDCompositeTypeBase>(DT)) {
Bill Wendling523bea82013-11-08 08:13:15 +0000191 processType(DCT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000192 if (DISubroutineType ST = dyn_cast<MDSubroutineType>(DCT)) {
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000193 for (MDTypeRef Ref : ST->getTypeArray())
194 processType(Ref.resolve(TypeIdentifierMap));
Manman Renf8a19672014-07-28 22:24:06 +0000195 return;
196 }
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000197 for (Metadata *D : DCT->getElements()->operands()) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000198 if (DIType T = dyn_cast<MDType>(D))
199 processType(T);
200 else if (DISubprogram SP = dyn_cast<MDSubprogram>(D))
201 processSubprogram(SP);
Bill Wendling523bea82013-11-08 08:13:15 +0000202 }
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000203 } else if (DIDerivedType DDT = dyn_cast<MDDerivedTypeBase>(DT)) {
Bill Wendling523bea82013-11-08 08:13:15 +0000204 processType(DDT.getTypeDerivedFrom().resolve(TypeIdentifierMap));
205 }
206}
207
208void DebugInfoFinder::processScope(DIScope Scope) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000209 if (!Scope)
210 return;
211 if (DIType Ty = dyn_cast<MDType>(Scope)) {
Bill Wendling523bea82013-11-08 08:13:15 +0000212 processType(Ty);
213 return;
214 }
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000215 if (DICompileUnit CU = dyn_cast<MDCompileUnit>(Scope)) {
216 addCompileUnit(CU);
Bill Wendling523bea82013-11-08 08:13:15 +0000217 return;
218 }
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000219 if (DISubprogram SP = dyn_cast<MDSubprogram>(Scope)) {
220 processSubprogram(SP);
Bill Wendling523bea82013-11-08 08:13:15 +0000221 return;
222 }
223 if (!addScope(Scope))
224 return;
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000225 if (auto *LB = dyn_cast<MDLexicalBlockBase>(Scope)) {
226 processScope(LB->getScope());
Duncan P. N. Exon Smith20caafb2015-04-14 03:01:27 +0000227 } else if (auto *NS = dyn_cast<MDNamespace>(Scope)) {
228 processScope(NS->getScope());
Bill Wendling523bea82013-11-08 08:13:15 +0000229 }
230}
231
Bill Wendling523bea82013-11-08 08:13:15 +0000232void DebugInfoFinder::processSubprogram(DISubprogram SP) {
233 if (!addSubprogram(SP))
234 return;
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000235 processScope(SP->getScope().resolve(TypeIdentifierMap));
236 processType(SP->getType());
237 for (auto *Element : SP->getTemplateParams()) {
Duncan P. N. Exon Smith20caafb2015-04-14 03:01:27 +0000238 if (auto *TType = dyn_cast<MDTemplateTypeParameter>(Element)) {
239 processType(TType->getType().resolve(TypeIdentifierMap));
240 } else if (auto *TVal = dyn_cast<MDTemplateValueParameter>(Element)) {
241 processType(TVal->getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000242 }
243 }
244}
245
Manman Ren2085ccc2013-11-17 18:42:37 +0000246void DebugInfoFinder::processDeclare(const Module &M,
247 const DbgDeclareInst *DDI) {
Bill Wendling523bea82013-11-08 08:13:15 +0000248 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
249 if (!N)
250 return;
Manman Renb46e5502013-11-17 19:35:03 +0000251 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000252
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000253 DIVariable DV = dyn_cast<MDLocalVariable>(N);
254 if (!DV)
Bill Wendling523bea82013-11-08 08:13:15 +0000255 return;
256
David Blaikie70573dc2014-11-19 07:49:26 +0000257 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000258 return;
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000259 processScope(DV->getScope());
260 processType(DV->getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000261}
262
Manman Ren2085ccc2013-11-17 18:42:37 +0000263void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
Bill Wendling523bea82013-11-08 08:13:15 +0000264 MDNode *N = dyn_cast<MDNode>(DVI->getVariable());
265 if (!N)
266 return;
Manman Renb46e5502013-11-17 19:35:03 +0000267 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000268
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000269 DIVariable DV = dyn_cast<MDLocalVariable>(N);
270 if (!DV)
Bill Wendling523bea82013-11-08 08:13:15 +0000271 return;
272
David Blaikie70573dc2014-11-19 07:49:26 +0000273 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000274 return;
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000275 processScope(DV->getScope());
276 processType(DV->getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000277}
278
Bill Wendling523bea82013-11-08 08:13:15 +0000279bool DebugInfoFinder::addType(DIType DT) {
280 if (!DT)
281 return false;
282
David Blaikie70573dc2014-11-19 07:49:26 +0000283 if (!NodesSeen.insert(DT).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000284 return false;
285
286 TYs.push_back(DT);
287 return true;
288}
289
Bill Wendling523bea82013-11-08 08:13:15 +0000290bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
291 if (!CU)
292 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000293 if (!NodesSeen.insert(CU).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000294 return false;
295
296 CUs.push_back(CU);
297 return true;
298}
299
Bill Wendling523bea82013-11-08 08:13:15 +0000300bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
301 if (!DIG)
302 return false;
303
David Blaikie70573dc2014-11-19 07:49:26 +0000304 if (!NodesSeen.insert(DIG).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000305 return false;
306
307 GVs.push_back(DIG);
308 return true;
309}
310
Bill Wendling523bea82013-11-08 08:13:15 +0000311bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
312 if (!SP)
313 return false;
314
David Blaikie70573dc2014-11-19 07:49:26 +0000315 if (!NodesSeen.insert(SP).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000316 return false;
317
318 SPs.push_back(SP);
319 return true;
320}
321
322bool DebugInfoFinder::addScope(DIScope Scope) {
323 if (!Scope)
324 return false;
325 // FIXME: Ocaml binding generates a scope with no content, we treat it
326 // as null for now.
327 if (Scope->getNumOperands() == 0)
328 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000329 if (!NodesSeen.insert(Scope).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000330 return false;
331 Scopes.push_back(Scope);
332 return true;
333}
334
335//===----------------------------------------------------------------------===//
336// DIDescriptor: dump routines for all descriptors.
337//===----------------------------------------------------------------------===//
338
Bill Wendling523bea82013-11-08 08:13:15 +0000339void DIDescriptor::dump() const {
340 print(dbgs());
341 dbgs() << '\n';
342}
343
Bill Wendling523bea82013-11-08 08:13:15 +0000344void DIDescriptor::print(raw_ostream &OS) const {
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000345 if (!get())
Bill Wendling523bea82013-11-08 08:13:15 +0000346 return;
Duncan P. N. Exon Smithe2741802015-03-03 17:24:31 +0000347 get()->print(OS);
Bill Wendling523bea82013-11-08 08:13:15 +0000348}
349
Duncan P. N. Exon Smith5bf3cdc2015-04-06 22:27:37 +0000350template <>
351DIDescriptor
352DIRef<DIDescriptor>::resolve(const DITypeIdentifierMap &Map) const {
353 return DIDescriptor(DebugNodeRef(Val).resolve(Map));
354}
355template <>
356DIScope DIRef<DIScope>::resolve(const DITypeIdentifierMap &Map) const {
357 return MDScopeRef(Val).resolve(Map);
358}
359template <>
360DIType DIRef<DIType>::resolve(const DITypeIdentifierMap &Map) const {
361 return MDTypeRef(Val).resolve(Map);
362}
363
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000364bool llvm::stripDebugInfo(Function &F) {
365 bool Changed = false;
366 for (BasicBlock &BB : F) {
367 for (Instruction &I : BB) {
368 if (I.getDebugLoc()) {
369 Changed = true;
370 I.setDebugLoc(DebugLoc());
371 }
372 }
373 }
374 return Changed;
375}
376
Manman Rencb14bbc2013-11-22 22:06:31 +0000377bool llvm::StripDebugInfo(Module &M) {
Manman Rencb14bbc2013-11-22 22:06:31 +0000378 bool Changed = false;
379
380 // Remove all of the calls to the debugger intrinsics, and remove them from
381 // the module.
382 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
383 while (!Declare->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000384 CallInst *CI = cast<CallInst>(Declare->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +0000385 CI->eraseFromParent();
386 }
387 Declare->eraseFromParent();
388 Changed = true;
389 }
390
391 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
392 while (!DbgVal->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000393 CallInst *CI = cast<CallInst>(DbgVal->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +0000394 CI->eraseFromParent();
395 }
396 DbgVal->eraseFromParent();
397 Changed = true;
398 }
399
400 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
401 NME = M.named_metadata_end(); NMI != NME;) {
402 NamedMDNode *NMD = NMI;
403 ++NMI;
404 if (NMD->getName().startswith("llvm.dbg.")) {
405 NMD->eraseFromParent();
406 Changed = true;
407 }
408 }
409
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000410 for (Function &F : M)
411 Changed |= stripDebugInfo(F);
412
Rafael Espindola468b8682015-04-01 14:44:59 +0000413 if (GVMaterializer *Materializer = M.getMaterializer())
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000414 Materializer->setStripDebugInfo();
Manman Rencb14bbc2013-11-22 22:06:31 +0000415
416 return Changed;
417}
Manman Ren8b4306c2013-12-02 21:29:56 +0000418
Manman Renbd4daf82013-12-03 00:12:14 +0000419unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
David Majnemere7a9cdb2015-02-16 06:04:53 +0000420 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000421 M.getModuleFlag("Debug Info Version")))
422 return Val->getZExtValue();
423 return 0;
Manman Ren8b4306c2013-12-02 21:29:56 +0000424}
David Blaikie6876b3b2014-07-01 20:05:26 +0000425
David Blaikiea8c35092014-07-02 18:30:05 +0000426llvm::DenseMap<const llvm::Function *, llvm::DISubprogram>
427llvm::makeSubprogramMap(const Module &M) {
428 DenseMap<const Function *, DISubprogram> R;
David Blaikie6876b3b2014-07-01 20:05:26 +0000429
430 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
431 if (!CU_Nodes)
432 return R;
433
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000434 for (MDNode *N : CU_Nodes->operands()) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000435 DICompileUnit CUNode = cast<MDCompileUnit>(N);
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000436 for (DISubprogram SP : CUNode->getSubprograms()) {
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000437 if (Function *F = SP->getFunction())
David Blaikie6876b3b2014-07-01 20:05:26 +0000438 R.insert(std::make_pair(F, SP));
439 }
440 }
441 return R;
442}