blob: 2a60af4b7f37bee284f5c4dfec54e897a5e0282e [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
42llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
43 DITypeIdentifierMap Map;
44 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000045 auto *CU = cast<DICompileUnit>(CU_Nodes->getOperand(CUi));
46 DINodeArray Retain = CU->getRetainedTypes();
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +000047 for (unsigned Ti = 0, Te = Retain.size(); Ti != Te; ++Ti) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000048 if (!isa<DICompositeType>(Retain[Ti]))
Bill Wendling523bea82013-11-08 08:13:15 +000049 continue;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000050 auto *Ty = cast<DICompositeType>(Retain[Ti]);
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +000051 if (MDString *TypeId = Ty->getRawIdentifier()) {
Bill Wendling523bea82013-11-08 08:13:15 +000052 // Definition has priority over declaration.
53 // Try to insert (TypeId, Ty) to Map.
54 std::pair<DITypeIdentifierMap::iterator, bool> P =
55 Map.insert(std::make_pair(TypeId, Ty));
56 // If TypeId already exists in Map and this is a definition, replace
57 // whatever we had (declaration or definition) with the definition.
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +000058 if (!P.second && !Ty->isForwardDecl())
Bill Wendling523bea82013-11-08 08:13:15 +000059 P.first->second = Ty;
60 }
61 }
62 }
63 return Map;
64}
65
66//===----------------------------------------------------------------------===//
67// DebugInfoFinder implementations.
68//===----------------------------------------------------------------------===//
69
70void DebugInfoFinder::reset() {
71 CUs.clear();
72 SPs.clear();
73 GVs.clear();
74 TYs.clear();
75 Scopes.clear();
76 NodesSeen.clear();
77 TypeIdentifierMap.clear();
Manman Ren2085ccc2013-11-17 18:42:37 +000078 TypeMapInitialized = false;
79}
80
Manman Renb46e5502013-11-17 19:35:03 +000081void DebugInfoFinder::InitializeTypeMap(const Module &M) {
Manman Ren2085ccc2013-11-17 18:42:37 +000082 if (!TypeMapInitialized)
83 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
84 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
85 TypeMapInitialized = true;
86 }
Bill Wendling523bea82013-11-08 08:13:15 +000087}
88
Bill Wendling523bea82013-11-08 08:13:15 +000089void DebugInfoFinder::processModule(const Module &M) {
Manman Renb46e5502013-11-17 19:35:03 +000090 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +000091 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
Bill Wendling523bea82013-11-08 08:13:15 +000092 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000093 auto *CU = cast<DICompileUnit>(CU_Nodes->getOperand(i));
Bill Wendling523bea82013-11-08 08:13:15 +000094 addCompileUnit(CU);
Duncan P. N. Exon Smithed557b52015-04-17 23:20:10 +000095 for (auto *DIG : CU->getGlobalVariables()) {
Bill Wendling523bea82013-11-08 08:13:15 +000096 if (addGlobalVariable(DIG)) {
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +000097 processScope(DIG->getScope());
98 processType(DIG->getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +000099 }
100 }
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000101 for (auto *SP : CU->getSubprograms())
102 processSubprogram(SP);
103 for (auto *ET : CU->getEnumTypes())
104 processType(ET);
105 for (auto *RT : CU->getRetainedTypes())
106 processType(RT);
Duncan P. N. Exon Smithed557b52015-04-17 23:20:10 +0000107 for (auto *Import : CU->getImportedEntities()) {
Duncan P. N. Exon Smithde8e4272015-04-14 01:46:44 +0000108 auto *Entity = Import->getEntity().resolve(TypeIdentifierMap);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000109 if (auto *T = dyn_cast<DIType>(Entity))
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000110 processType(T);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000111 else if (auto *SP = dyn_cast<DISubprogram>(Entity))
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000112 processSubprogram(SP);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000113 else if (auto *NS = dyn_cast<DINamespace>(Entity))
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000114 processScope(NS->getScope());
Adrian Prantlab1243f2015-06-29 23:03:47 +0000115 else if (auto *M = dyn_cast<DIModule>(Entity))
116 processScope(M->getScope());
Bill Wendling523bea82013-11-08 08:13:15 +0000117 }
118 }
119 }
120}
121
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000122void DebugInfoFinder::processLocation(const Module &M, const DILocation *Loc) {
Bill Wendling523bea82013-11-08 08:13:15 +0000123 if (!Loc)
124 return;
Manman Renb46e5502013-11-17 19:35:03 +0000125 InitializeTypeMap(M);
Duncan P. N. Exon Smithb7e221b2015-04-14 01:35:55 +0000126 processScope(Loc->getScope());
127 processLocation(M, Loc->getInlinedAt());
Bill Wendling523bea82013-11-08 08:13:15 +0000128}
129
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000130void DebugInfoFinder::processType(DIType *DT) {
Bill Wendling523bea82013-11-08 08:13:15 +0000131 if (!addType(DT))
132 return;
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000133 processScope(DT->getScope().resolve(TypeIdentifierMap));
Duncan P. N. Exon Smith260fa8a2015-07-24 20:56:10 +0000134 if (auto *ST = dyn_cast<DISubroutineType>(DT)) {
135 for (DITypeRef Ref : ST->getTypeArray())
136 processType(Ref.resolve(TypeIdentifierMap));
137 return;
138 }
139 if (auto *DCT = dyn_cast<DICompositeType>(DT)) {
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000140 processType(DCT->getBaseType().resolve(TypeIdentifierMap));
Anders Waldenborg1433fd42015-04-14 09:18:17 +0000141 for (Metadata *D : DCT->getElements()) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000142 if (auto *T = dyn_cast<DIType>(D))
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>(D))
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000145 processSubprogram(SP);
Bill Wendling523bea82013-11-08 08:13:15 +0000146 }
Duncan P. N. Exon Smith260fa8a2015-07-24 20:56:10 +0000147 return;
148 }
149 if (auto *DDT = dyn_cast<DIDerivedType>(DT)) {
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000150 processType(DDT->getBaseType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000151 }
152}
153
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000154void DebugInfoFinder::processScope(DIScope *Scope) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000155 if (!Scope)
156 return;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000157 if (auto *Ty = dyn_cast<DIType>(Scope)) {
Bill Wendling523bea82013-11-08 08:13:15 +0000158 processType(Ty);
159 return;
160 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000161 if (auto *CU = dyn_cast<DICompileUnit>(Scope)) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000162 addCompileUnit(CU);
Bill Wendling523bea82013-11-08 08:13:15 +0000163 return;
164 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000165 if (auto *SP = dyn_cast<DISubprogram>(Scope)) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000166 processSubprogram(SP);
Bill Wendling523bea82013-11-08 08:13:15 +0000167 return;
168 }
169 if (!addScope(Scope))
170 return;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000171 if (auto *LB = dyn_cast<DILexicalBlockBase>(Scope)) {
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000172 processScope(LB->getScope());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000173 } else if (auto *NS = dyn_cast<DINamespace>(Scope)) {
Duncan P. N. Exon Smith20caafb2015-04-14 03:01:27 +0000174 processScope(NS->getScope());
Adrian Prantlab1243f2015-06-29 23:03:47 +0000175 } else if (auto *M = dyn_cast<DIModule>(Scope)) {
176 processScope(M->getScope());
Bill Wendling523bea82013-11-08 08:13:15 +0000177 }
178}
179
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000180void DebugInfoFinder::processSubprogram(DISubprogram *SP) {
Bill Wendling523bea82013-11-08 08:13:15 +0000181 if (!addSubprogram(SP))
182 return;
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000183 processScope(SP->getScope().resolve(TypeIdentifierMap));
184 processType(SP->getType());
185 for (auto *Element : SP->getTemplateParams()) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000186 if (auto *TType = dyn_cast<DITemplateTypeParameter>(Element)) {
Duncan P. N. Exon Smith20caafb2015-04-14 03:01:27 +0000187 processType(TType->getType().resolve(TypeIdentifierMap));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000188 } else if (auto *TVal = dyn_cast<DITemplateValueParameter>(Element)) {
Duncan P. N. Exon Smith20caafb2015-04-14 03:01:27 +0000189 processType(TVal->getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000190 }
191 }
192}
193
Manman Ren2085ccc2013-11-17 18:42:37 +0000194void DebugInfoFinder::processDeclare(const Module &M,
195 const DbgDeclareInst *DDI) {
Duncan P. N. Exon Smithed557b52015-04-17 23:20:10 +0000196 auto *N = dyn_cast<MDNode>(DDI->getVariable());
Bill Wendling523bea82013-11-08 08:13:15 +0000197 if (!N)
198 return;
Manman Renb46e5502013-11-17 19:35:03 +0000199 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000200
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000201 auto *DV = dyn_cast<DILocalVariable>(N);
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000202 if (!DV)
Bill Wendling523bea82013-11-08 08:13:15 +0000203 return;
204
David Blaikie70573dc2014-11-19 07:49:26 +0000205 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000206 return;
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000207 processScope(DV->getScope());
208 processType(DV->getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000209}
210
Manman Ren2085ccc2013-11-17 18:42:37 +0000211void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
Duncan P. N. Exon Smithed557b52015-04-17 23:20:10 +0000212 auto *N = dyn_cast<MDNode>(DVI->getVariable());
Bill Wendling523bea82013-11-08 08:13:15 +0000213 if (!N)
214 return;
Manman Renb46e5502013-11-17 19:35:03 +0000215 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000216
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000217 auto *DV = dyn_cast<DILocalVariable>(N);
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000218 if (!DV)
Bill Wendling523bea82013-11-08 08:13:15 +0000219 return;
220
David Blaikie70573dc2014-11-19 07:49:26 +0000221 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000222 return;
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000223 processScope(DV->getScope());
224 processType(DV->getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000225}
226
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000227bool DebugInfoFinder::addType(DIType *DT) {
Bill Wendling523bea82013-11-08 08:13:15 +0000228 if (!DT)
229 return false;
230
David Blaikie70573dc2014-11-19 07:49:26 +0000231 if (!NodesSeen.insert(DT).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000232 return false;
233
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000234 TYs.push_back(const_cast<DIType *>(DT));
Bill Wendling523bea82013-11-08 08:13:15 +0000235 return true;
236}
237
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000238bool DebugInfoFinder::addCompileUnit(DICompileUnit *CU) {
Bill Wendling523bea82013-11-08 08:13:15 +0000239 if (!CU)
240 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000241 if (!NodesSeen.insert(CU).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000242 return false;
243
244 CUs.push_back(CU);
245 return true;
246}
247
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000248bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable *DIG) {
Bill Wendling523bea82013-11-08 08:13:15 +0000249 if (!DIG)
250 return false;
251
David Blaikie70573dc2014-11-19 07:49:26 +0000252 if (!NodesSeen.insert(DIG).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000253 return false;
254
255 GVs.push_back(DIG);
256 return true;
257}
258
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000259bool DebugInfoFinder::addSubprogram(DISubprogram *SP) {
Bill Wendling523bea82013-11-08 08:13:15 +0000260 if (!SP)
261 return false;
262
David Blaikie70573dc2014-11-19 07:49:26 +0000263 if (!NodesSeen.insert(SP).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000264 return false;
265
266 SPs.push_back(SP);
267 return true;
268}
269
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000270bool DebugInfoFinder::addScope(DIScope *Scope) {
Bill Wendling523bea82013-11-08 08:13:15 +0000271 if (!Scope)
272 return false;
273 // FIXME: Ocaml binding generates a scope with no content, we treat it
274 // as null for now.
275 if (Scope->getNumOperands() == 0)
276 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000277 if (!NodesSeen.insert(Scope).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000278 return false;
279 Scopes.push_back(Scope);
280 return true;
281}
282
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000283bool llvm::stripDebugInfo(Function &F) {
284 bool Changed = false;
Peter Collingbourned4bff302015-11-05 22:03:56 +0000285 if (F.getSubprogram()) {
286 Changed = true;
287 F.setSubprogram(nullptr);
288 }
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000289 for (BasicBlock &BB : F) {
290 for (Instruction &I : BB) {
291 if (I.getDebugLoc()) {
292 Changed = true;
293 I.setDebugLoc(DebugLoc());
294 }
295 }
296 }
297 return Changed;
298}
299
Manman Rencb14bbc2013-11-22 22:06:31 +0000300bool llvm::StripDebugInfo(Module &M) {
Manman Rencb14bbc2013-11-22 22:06:31 +0000301 bool Changed = false;
302
303 // Remove all of the calls to the debugger intrinsics, and remove them from
304 // the module.
305 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
306 while (!Declare->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000307 CallInst *CI = cast<CallInst>(Declare->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +0000308 CI->eraseFromParent();
309 }
310 Declare->eraseFromParent();
311 Changed = true;
312 }
313
314 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
315 while (!DbgVal->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000316 CallInst *CI = cast<CallInst>(DbgVal->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +0000317 CI->eraseFromParent();
318 }
319 DbgVal->eraseFromParent();
320 Changed = true;
321 }
322
323 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
324 NME = M.named_metadata_end(); NMI != NME;) {
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +0000325 NamedMDNode *NMD = &*NMI;
Manman Rencb14bbc2013-11-22 22:06:31 +0000326 ++NMI;
327 if (NMD->getName().startswith("llvm.dbg.")) {
328 NMD->eraseFromParent();
329 Changed = true;
330 }
331 }
332
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000333 for (Function &F : M)
334 Changed |= stripDebugInfo(F);
335
Rafael Espindola468b8682015-04-01 14:44:59 +0000336 if (GVMaterializer *Materializer = M.getMaterializer())
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000337 Materializer->setStripDebugInfo();
Manman Rencb14bbc2013-11-22 22:06:31 +0000338
339 return Changed;
340}
Manman Ren8b4306c2013-12-02 21:29:56 +0000341
Manman Renbd4daf82013-12-03 00:12:14 +0000342unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
David Majnemere7a9cdb2015-02-16 06:04:53 +0000343 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000344 M.getModuleFlag("Debug Info Version")))
345 return Val->getZExtValue();
346 return 0;
Manman Ren8b4306c2013-12-02 21:29:56 +0000347}