blob: e63c25484a5201bc8c539524e43b69daec1821e2 [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"
Bill Wendling523bea82013-11-08 08:13:15 +000020#include "llvm/IR/Constants.h"
Adrian Prantlb1416832014-08-01 22:11:58 +000021#include "llvm/IR/DIBuilder.h"
Bill Wendling523bea82013-11-08 08:13:15 +000022#include "llvm/IR/DerivedTypes.h"
23#include "llvm/IR/Instructions.h"
24#include "llvm/IR/IntrinsicInst.h"
25#include "llvm/IR/Intrinsics.h"
Rafael Espindola0d68b4c2015-03-30 21:36:43 +000026#include "llvm/IR/GVMaterializer.h"
Bill Wendling523bea82013-11-08 08:13:15 +000027#include "llvm/IR/Module.h"
Chandler Carruth4220e9c2014-03-04 11:17:44 +000028#include "llvm/IR/ValueHandle.h"
Bill Wendling523bea82013-11-08 08:13:15 +000029#include "llvm/Support/Debug.h"
30#include "llvm/Support/Dwarf.h"
Bill Wendling523bea82013-11-08 08:13:15 +000031#include "llvm/Support/raw_ostream.h"
32using namespace llvm;
33using namespace llvm::dwarf;
34
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000035DISubprogram *llvm::getDISubprogram(const MDNode *Scope) {
36 if (auto *LocalScope = dyn_cast_or_null<DILocalScope>(Scope))
Duncan P. N. Exon Smithdd77af82015-03-31 02:06:28 +000037 return LocalScope->getSubprogram();
38 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +000039}
40
Bill Wendling523bea82013-11-08 08:13:15 +000041DITypeIdentifierMap
Adrian Prantl5992a722016-04-08 22:43:03 +000042llvm::generateDITypeIdentifierMap(const Module &M) {
Bill Wendling523bea82013-11-08 08:13:15 +000043 DITypeIdentifierMap Map;
Adrian Prantl5992a722016-04-08 22:43:03 +000044 for (DICompileUnit *CU : M.debug_compile_units()) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000045 DINodeArray Retain = CU->getRetainedTypes();
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +000046 for (unsigned Ti = 0, Te = Retain.size(); Ti != Te; ++Ti) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000047 if (!isa<DICompositeType>(Retain[Ti]))
Bill Wendling523bea82013-11-08 08:13:15 +000048 continue;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000049 auto *Ty = cast<DICompositeType>(Retain[Ti]);
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +000050 if (MDString *TypeId = Ty->getRawIdentifier()) {
Bill Wendling523bea82013-11-08 08:13:15 +000051 // Definition has priority over declaration.
52 // Try to insert (TypeId, Ty) to Map.
53 std::pair<DITypeIdentifierMap::iterator, bool> P =
54 Map.insert(std::make_pair(TypeId, Ty));
55 // If TypeId already exists in Map and this is a definition, replace
56 // whatever we had (declaration or definition) with the definition.
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +000057 if (!P.second && !Ty->isForwardDecl())
Bill Wendling523bea82013-11-08 08:13:15 +000058 P.first->second = Ty;
59 }
60 }
61 }
62 return Map;
63}
64
65//===----------------------------------------------------------------------===//
66// DebugInfoFinder implementations.
67//===----------------------------------------------------------------------===//
68
69void DebugInfoFinder::reset() {
70 CUs.clear();
71 SPs.clear();
72 GVs.clear();
73 TYs.clear();
74 Scopes.clear();
75 NodesSeen.clear();
76 TypeIdentifierMap.clear();
Manman Ren2085ccc2013-11-17 18:42:37 +000077 TypeMapInitialized = false;
78}
79
Manman Renb46e5502013-11-17 19:35:03 +000080void DebugInfoFinder::InitializeTypeMap(const Module &M) {
Adrian Prantl5992a722016-04-08 22:43:03 +000081 if (TypeMapInitialized)
82 return;
83 TypeIdentifierMap = generateDITypeIdentifierMap(M);
84 TypeMapInitialized = true;
Bill Wendling523bea82013-11-08 08:13:15 +000085}
86
Bill Wendling523bea82013-11-08 08:13:15 +000087void DebugInfoFinder::processModule(const Module &M) {
Manman Renb46e5502013-11-17 19:35:03 +000088 InitializeTypeMap(M);
Adrian Prantl5992a722016-04-08 22:43:03 +000089 for (auto *CU : M.debug_compile_units()) {
90 addCompileUnit(CU);
91 for (auto *DIG : CU->getGlobalVariables()) {
92 if (addGlobalVariable(DIG)) {
93 processScope(DIG->getScope());
94 processType(DIG->getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +000095 }
Adrian Prantl5992a722016-04-08 22:43:03 +000096 }
97 for (auto *SP : CU->getSubprograms())
98 processSubprogram(SP);
99 for (auto *ET : CU->getEnumTypes())
100 processType(ET);
101 for (auto *RT : CU->getRetainedTypes())
102 processType(RT);
103 for (auto *Import : CU->getImportedEntities()) {
104 auto *Entity = Import->getEntity().resolve(TypeIdentifierMap);
105 if (auto *T = dyn_cast<DIType>(Entity))
106 processType(T);
107 else if (auto *SP = dyn_cast<DISubprogram>(Entity))
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000108 processSubprogram(SP);
Adrian Prantl5992a722016-04-08 22:43:03 +0000109 else if (auto *NS = dyn_cast<DINamespace>(Entity))
110 processScope(NS->getScope());
111 else if (auto *M = dyn_cast<DIModule>(Entity))
112 processScope(M->getScope());
Bill Wendling523bea82013-11-08 08:13:15 +0000113 }
114 }
115}
116
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000117void DebugInfoFinder::processLocation(const Module &M, const DILocation *Loc) {
Bill Wendling523bea82013-11-08 08:13:15 +0000118 if (!Loc)
119 return;
Manman Renb46e5502013-11-17 19:35:03 +0000120 InitializeTypeMap(M);
Duncan P. N. Exon Smithb7e221b2015-04-14 01:35:55 +0000121 processScope(Loc->getScope());
122 processLocation(M, Loc->getInlinedAt());
Bill Wendling523bea82013-11-08 08:13:15 +0000123}
124
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000125void DebugInfoFinder::processType(DIType *DT) {
Bill Wendling523bea82013-11-08 08:13:15 +0000126 if (!addType(DT))
127 return;
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000128 processScope(DT->getScope().resolve(TypeIdentifierMap));
Duncan P. N. Exon Smith260fa8a2015-07-24 20:56:10 +0000129 if (auto *ST = dyn_cast<DISubroutineType>(DT)) {
130 for (DITypeRef Ref : ST->getTypeArray())
131 processType(Ref.resolve(TypeIdentifierMap));
132 return;
133 }
134 if (auto *DCT = dyn_cast<DICompositeType>(DT)) {
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000135 processType(DCT->getBaseType().resolve(TypeIdentifierMap));
Anders Waldenborg1433fd42015-04-14 09:18:17 +0000136 for (Metadata *D : DCT->getElements()) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000137 if (auto *T = dyn_cast<DIType>(D))
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000138 processType(T);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000139 else if (auto *SP = dyn_cast<DISubprogram>(D))
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000140 processSubprogram(SP);
Bill Wendling523bea82013-11-08 08:13:15 +0000141 }
Duncan P. N. Exon Smith260fa8a2015-07-24 20:56:10 +0000142 return;
143 }
144 if (auto *DDT = dyn_cast<DIDerivedType>(DT)) {
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000145 processType(DDT->getBaseType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000146 }
147}
148
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000149void DebugInfoFinder::processScope(DIScope *Scope) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000150 if (!Scope)
151 return;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000152 if (auto *Ty = dyn_cast<DIType>(Scope)) {
Bill Wendling523bea82013-11-08 08:13:15 +0000153 processType(Ty);
154 return;
155 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000156 if (auto *CU = dyn_cast<DICompileUnit>(Scope)) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000157 addCompileUnit(CU);
Bill Wendling523bea82013-11-08 08:13:15 +0000158 return;
159 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000160 if (auto *SP = dyn_cast<DISubprogram>(Scope)) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000161 processSubprogram(SP);
Bill Wendling523bea82013-11-08 08:13:15 +0000162 return;
163 }
164 if (!addScope(Scope))
165 return;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000166 if (auto *LB = dyn_cast<DILexicalBlockBase>(Scope)) {
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000167 processScope(LB->getScope());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000168 } else if (auto *NS = dyn_cast<DINamespace>(Scope)) {
Duncan P. N. Exon Smith20caafb2015-04-14 03:01:27 +0000169 processScope(NS->getScope());
Adrian Prantlab1243f2015-06-29 23:03:47 +0000170 } else if (auto *M = dyn_cast<DIModule>(Scope)) {
171 processScope(M->getScope());
Bill Wendling523bea82013-11-08 08:13:15 +0000172 }
173}
174
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000175void DebugInfoFinder::processSubprogram(DISubprogram *SP) {
Bill Wendling523bea82013-11-08 08:13:15 +0000176 if (!addSubprogram(SP))
177 return;
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000178 processScope(SP->getScope().resolve(TypeIdentifierMap));
179 processType(SP->getType());
180 for (auto *Element : SP->getTemplateParams()) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000181 if (auto *TType = dyn_cast<DITemplateTypeParameter>(Element)) {
Duncan P. N. Exon Smith20caafb2015-04-14 03:01:27 +0000182 processType(TType->getType().resolve(TypeIdentifierMap));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000183 } else if (auto *TVal = dyn_cast<DITemplateValueParameter>(Element)) {
Duncan P. N. Exon Smith20caafb2015-04-14 03:01:27 +0000184 processType(TVal->getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000185 }
186 }
187}
188
Manman Ren2085ccc2013-11-17 18:42:37 +0000189void DebugInfoFinder::processDeclare(const Module &M,
190 const DbgDeclareInst *DDI) {
Duncan P. N. Exon Smithed557b52015-04-17 23:20:10 +0000191 auto *N = dyn_cast<MDNode>(DDI->getVariable());
Bill Wendling523bea82013-11-08 08:13:15 +0000192 if (!N)
193 return;
Manman Renb46e5502013-11-17 19:35:03 +0000194 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000195
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000196 auto *DV = dyn_cast<DILocalVariable>(N);
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000197 if (!DV)
Bill Wendling523bea82013-11-08 08:13:15 +0000198 return;
199
David Blaikie70573dc2014-11-19 07:49:26 +0000200 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000201 return;
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000202 processScope(DV->getScope());
203 processType(DV->getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000204}
205
Manman Ren2085ccc2013-11-17 18:42:37 +0000206void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
Duncan P. N. Exon Smithed557b52015-04-17 23:20:10 +0000207 auto *N = dyn_cast<MDNode>(DVI->getVariable());
Bill Wendling523bea82013-11-08 08:13:15 +0000208 if (!N)
209 return;
Manman Renb46e5502013-11-17 19:35:03 +0000210 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000211
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000212 auto *DV = dyn_cast<DILocalVariable>(N);
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000213 if (!DV)
Bill Wendling523bea82013-11-08 08:13:15 +0000214 return;
215
David Blaikie70573dc2014-11-19 07:49:26 +0000216 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000217 return;
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000218 processScope(DV->getScope());
219 processType(DV->getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000220}
221
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000222bool DebugInfoFinder::addType(DIType *DT) {
Bill Wendling523bea82013-11-08 08:13:15 +0000223 if (!DT)
224 return false;
225
David Blaikie70573dc2014-11-19 07:49:26 +0000226 if (!NodesSeen.insert(DT).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000227 return false;
228
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000229 TYs.push_back(const_cast<DIType *>(DT));
Bill Wendling523bea82013-11-08 08:13:15 +0000230 return true;
231}
232
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000233bool DebugInfoFinder::addCompileUnit(DICompileUnit *CU) {
Bill Wendling523bea82013-11-08 08:13:15 +0000234 if (!CU)
235 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000236 if (!NodesSeen.insert(CU).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000237 return false;
238
239 CUs.push_back(CU);
240 return true;
241}
242
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000243bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable *DIG) {
Bill Wendling523bea82013-11-08 08:13:15 +0000244 if (!DIG)
245 return false;
246
David Blaikie70573dc2014-11-19 07:49:26 +0000247 if (!NodesSeen.insert(DIG).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000248 return false;
249
250 GVs.push_back(DIG);
251 return true;
252}
253
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000254bool DebugInfoFinder::addSubprogram(DISubprogram *SP) {
Bill Wendling523bea82013-11-08 08:13:15 +0000255 if (!SP)
256 return false;
257
David Blaikie70573dc2014-11-19 07:49:26 +0000258 if (!NodesSeen.insert(SP).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000259 return false;
260
261 SPs.push_back(SP);
262 return true;
263}
264
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000265bool DebugInfoFinder::addScope(DIScope *Scope) {
Bill Wendling523bea82013-11-08 08:13:15 +0000266 if (!Scope)
267 return false;
268 // FIXME: Ocaml binding generates a scope with no content, we treat it
269 // as null for now.
270 if (Scope->getNumOperands() == 0)
271 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000272 if (!NodesSeen.insert(Scope).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000273 return false;
274 Scopes.push_back(Scope);
275 return true;
276}
277
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000278bool llvm::stripDebugInfo(Function &F) {
279 bool Changed = false;
Peter Collingbourned4bff302015-11-05 22:03:56 +0000280 if (F.getSubprogram()) {
281 Changed = true;
282 F.setSubprogram(nullptr);
283 }
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000284 for (BasicBlock &BB : F) {
285 for (Instruction &I : BB) {
286 if (I.getDebugLoc()) {
287 Changed = true;
288 I.setDebugLoc(DebugLoc());
289 }
290 }
291 }
292 return Changed;
293}
294
Manman Rencb14bbc2013-11-22 22:06:31 +0000295bool llvm::StripDebugInfo(Module &M) {
Manman Rencb14bbc2013-11-22 22:06:31 +0000296 bool Changed = false;
297
298 // Remove all of the calls to the debugger intrinsics, and remove them from
299 // the module.
300 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
301 while (!Declare->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000302 CallInst *CI = cast<CallInst>(Declare->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +0000303 CI->eraseFromParent();
304 }
305 Declare->eraseFromParent();
306 Changed = true;
307 }
308
309 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
310 while (!DbgVal->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000311 CallInst *CI = cast<CallInst>(DbgVal->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +0000312 CI->eraseFromParent();
313 }
314 DbgVal->eraseFromParent();
315 Changed = true;
316 }
317
318 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
319 NME = M.named_metadata_end(); NMI != NME;) {
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +0000320 NamedMDNode *NMD = &*NMI;
Manman Rencb14bbc2013-11-22 22:06:31 +0000321 ++NMI;
322 if (NMD->getName().startswith("llvm.dbg.")) {
323 NMD->eraseFromParent();
324 Changed = true;
325 }
326 }
327
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000328 for (Function &F : M)
329 Changed |= stripDebugInfo(F);
330
Rafael Espindola468b8682015-04-01 14:44:59 +0000331 if (GVMaterializer *Materializer = M.getMaterializer())
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000332 Materializer->setStripDebugInfo();
Manman Rencb14bbc2013-11-22 22:06:31 +0000333
334 return Changed;
335}
Manman Ren8b4306c2013-12-02 21:29:56 +0000336
Manman Renbd4daf82013-12-03 00:12:14 +0000337unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
David Majnemere7a9cdb2015-02-16 06:04:53 +0000338 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000339 M.getModuleFlag("Debug Info Version")))
340 return Val->getZExtValue();
341 return 0;
Manman Ren8b4306c2013-12-02 21:29:56 +0000342}