blob: 0d2d72b8d83f483238b6e253bdef80a58a3f207a [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 }
Adrian Prantl5992a722016-04-08 22:43:03 +000097 for (auto *ET : CU->getEnumTypes())
98 processType(ET);
99 for (auto *RT : CU->getRetainedTypes())
Adrian Prantl75819ae2016-04-15 15:57:41 +0000100 if (auto *T = dyn_cast<DIType>(RT))
101 processType(T);
102 else
103 processSubprogram(cast<DISubprogram>(RT));
Adrian Prantl5992a722016-04-08 22:43:03 +0000104 for (auto *Import : CU->getImportedEntities()) {
105 auto *Entity = Import->getEntity().resolve(TypeIdentifierMap);
106 if (auto *T = dyn_cast<DIType>(Entity))
107 processType(T);
108 else if (auto *SP = dyn_cast<DISubprogram>(Entity))
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +0000109 processSubprogram(SP);
Adrian Prantl5992a722016-04-08 22:43:03 +0000110 else if (auto *NS = dyn_cast<DINamespace>(Entity))
111 processScope(NS->getScope());
112 else if (auto *M = dyn_cast<DIModule>(Entity))
113 processScope(M->getScope());
Bill Wendling523bea82013-11-08 08:13:15 +0000114 }
115 }
Adrian Prantl75819ae2016-04-15 15:57:41 +0000116 for (auto &F : M.functions())
117 if (auto *SP = cast_or_null<DISubprogram>(F.getSubprogram()))
118 processSubprogram(SP);
Bill Wendling523bea82013-11-08 08:13:15 +0000119}
120
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000121void DebugInfoFinder::processLocation(const Module &M, const DILocation *Loc) {
Bill Wendling523bea82013-11-08 08:13:15 +0000122 if (!Loc)
123 return;
Manman Renb46e5502013-11-17 19:35:03 +0000124 InitializeTypeMap(M);
Duncan P. N. Exon Smithb7e221b2015-04-14 01:35:55 +0000125 processScope(Loc->getScope());
126 processLocation(M, Loc->getInlinedAt());
Bill Wendling523bea82013-11-08 08:13:15 +0000127}
128
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000129void DebugInfoFinder::processType(DIType *DT) {
Bill Wendling523bea82013-11-08 08:13:15 +0000130 if (!addType(DT))
131 return;
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000132 processScope(DT->getScope().resolve(TypeIdentifierMap));
Duncan P. N. Exon Smith260fa8a2015-07-24 20:56:10 +0000133 if (auto *ST = dyn_cast<DISubroutineType>(DT)) {
134 for (DITypeRef Ref : ST->getTypeArray())
135 processType(Ref.resolve(TypeIdentifierMap));
136 return;
137 }
138 if (auto *DCT = dyn_cast<DICompositeType>(DT)) {
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000139 processType(DCT->getBaseType().resolve(TypeIdentifierMap));
Anders Waldenborg1433fd42015-04-14 09:18:17 +0000140 for (Metadata *D : DCT->getElements()) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000141 if (auto *T = dyn_cast<DIType>(D))
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000142 processType(T);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000143 else if (auto *SP = dyn_cast<DISubprogram>(D))
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000144 processSubprogram(SP);
Bill Wendling523bea82013-11-08 08:13:15 +0000145 }
Duncan P. N. Exon Smith260fa8a2015-07-24 20:56:10 +0000146 return;
147 }
148 if (auto *DDT = dyn_cast<DIDerivedType>(DT)) {
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000149 processType(DDT->getBaseType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000150 }
151}
152
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000153void DebugInfoFinder::processScope(DIScope *Scope) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000154 if (!Scope)
155 return;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000156 if (auto *Ty = dyn_cast<DIType>(Scope)) {
Bill Wendling523bea82013-11-08 08:13:15 +0000157 processType(Ty);
158 return;
159 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000160 if (auto *CU = dyn_cast<DICompileUnit>(Scope)) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000161 addCompileUnit(CU);
Bill Wendling523bea82013-11-08 08:13:15 +0000162 return;
163 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000164 if (auto *SP = dyn_cast<DISubprogram>(Scope)) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000165 processSubprogram(SP);
Bill Wendling523bea82013-11-08 08:13:15 +0000166 return;
167 }
168 if (!addScope(Scope))
169 return;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000170 if (auto *LB = dyn_cast<DILexicalBlockBase>(Scope)) {
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000171 processScope(LB->getScope());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000172 } else if (auto *NS = dyn_cast<DINamespace>(Scope)) {
Duncan P. N. Exon Smith20caafb2015-04-14 03:01:27 +0000173 processScope(NS->getScope());
Adrian Prantlab1243f2015-06-29 23:03:47 +0000174 } else if (auto *M = dyn_cast<DIModule>(Scope)) {
175 processScope(M->getScope());
Bill Wendling523bea82013-11-08 08:13:15 +0000176 }
177}
178
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000179void DebugInfoFinder::processSubprogram(DISubprogram *SP) {
Bill Wendling523bea82013-11-08 08:13:15 +0000180 if (!addSubprogram(SP))
181 return;
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000182 processScope(SP->getScope().resolve(TypeIdentifierMap));
183 processType(SP->getType());
184 for (auto *Element : SP->getTemplateParams()) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000185 if (auto *TType = dyn_cast<DITemplateTypeParameter>(Element)) {
Duncan P. N. Exon Smith20caafb2015-04-14 03:01:27 +0000186 processType(TType->getType().resolve(TypeIdentifierMap));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000187 } else if (auto *TVal = dyn_cast<DITemplateValueParameter>(Element)) {
Duncan P. N. Exon Smith20caafb2015-04-14 03:01:27 +0000188 processType(TVal->getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000189 }
190 }
191}
192
Manman Ren2085ccc2013-11-17 18:42:37 +0000193void DebugInfoFinder::processDeclare(const Module &M,
194 const DbgDeclareInst *DDI) {
Duncan P. N. Exon Smithed557b52015-04-17 23:20:10 +0000195 auto *N = dyn_cast<MDNode>(DDI->getVariable());
Bill Wendling523bea82013-11-08 08:13:15 +0000196 if (!N)
197 return;
Manman Renb46e5502013-11-17 19:35:03 +0000198 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000199
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000200 auto *DV = dyn_cast<DILocalVariable>(N);
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000201 if (!DV)
Bill Wendling523bea82013-11-08 08:13:15 +0000202 return;
203
David Blaikie70573dc2014-11-19 07:49:26 +0000204 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000205 return;
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000206 processScope(DV->getScope());
207 processType(DV->getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000208}
209
Manman Ren2085ccc2013-11-17 18:42:37 +0000210void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
Duncan P. N. Exon Smithed557b52015-04-17 23:20:10 +0000211 auto *N = dyn_cast<MDNode>(DVI->getVariable());
Bill Wendling523bea82013-11-08 08:13:15 +0000212 if (!N)
213 return;
Manman Renb46e5502013-11-17 19:35:03 +0000214 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000215
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000216 auto *DV = dyn_cast<DILocalVariable>(N);
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000217 if (!DV)
Bill Wendling523bea82013-11-08 08:13:15 +0000218 return;
219
David Blaikie70573dc2014-11-19 07:49:26 +0000220 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000221 return;
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000222 processScope(DV->getScope());
223 processType(DV->getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000224}
225
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000226bool DebugInfoFinder::addType(DIType *DT) {
Bill Wendling523bea82013-11-08 08:13:15 +0000227 if (!DT)
228 return false;
229
David Blaikie70573dc2014-11-19 07:49:26 +0000230 if (!NodesSeen.insert(DT).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000231 return false;
232
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000233 TYs.push_back(const_cast<DIType *>(DT));
Bill Wendling523bea82013-11-08 08:13:15 +0000234 return true;
235}
236
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000237bool DebugInfoFinder::addCompileUnit(DICompileUnit *CU) {
Bill Wendling523bea82013-11-08 08:13:15 +0000238 if (!CU)
239 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000240 if (!NodesSeen.insert(CU).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000241 return false;
242
243 CUs.push_back(CU);
244 return true;
245}
246
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000247bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable *DIG) {
Bill Wendling523bea82013-11-08 08:13:15 +0000248 if (!DIG)
249 return false;
250
David Blaikie70573dc2014-11-19 07:49:26 +0000251 if (!NodesSeen.insert(DIG).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000252 return false;
253
254 GVs.push_back(DIG);
255 return true;
256}
257
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000258bool DebugInfoFinder::addSubprogram(DISubprogram *SP) {
Bill Wendling523bea82013-11-08 08:13:15 +0000259 if (!SP)
260 return false;
261
David Blaikie70573dc2014-11-19 07:49:26 +0000262 if (!NodesSeen.insert(SP).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000263 return false;
264
265 SPs.push_back(SP);
266 return true;
267}
268
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000269bool DebugInfoFinder::addScope(DIScope *Scope) {
Bill Wendling523bea82013-11-08 08:13:15 +0000270 if (!Scope)
271 return false;
272 // FIXME: Ocaml binding generates a scope with no content, we treat it
273 // as null for now.
274 if (Scope->getNumOperands() == 0)
275 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000276 if (!NodesSeen.insert(Scope).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000277 return false;
278 Scopes.push_back(Scope);
279 return true;
280}
281
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000282bool llvm::stripDebugInfo(Function &F) {
283 bool Changed = false;
Peter Collingbourned4bff302015-11-05 22:03:56 +0000284 if (F.getSubprogram()) {
285 Changed = true;
286 F.setSubprogram(nullptr);
287 }
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000288 for (BasicBlock &BB : F) {
289 for (Instruction &I : BB) {
290 if (I.getDebugLoc()) {
291 Changed = true;
292 I.setDebugLoc(DebugLoc());
293 }
294 }
295 }
296 return Changed;
297}
298
Manman Rencb14bbc2013-11-22 22:06:31 +0000299bool llvm::StripDebugInfo(Module &M) {
Manman Rencb14bbc2013-11-22 22:06:31 +0000300 bool Changed = false;
301
302 // Remove all of the calls to the debugger intrinsics, and remove them from
303 // the module.
304 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
305 while (!Declare->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000306 CallInst *CI = cast<CallInst>(Declare->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +0000307 CI->eraseFromParent();
308 }
309 Declare->eraseFromParent();
310 Changed = true;
311 }
312
313 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
314 while (!DbgVal->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000315 CallInst *CI = cast<CallInst>(DbgVal->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +0000316 CI->eraseFromParent();
317 }
318 DbgVal->eraseFromParent();
319 Changed = true;
320 }
321
322 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
323 NME = M.named_metadata_end(); NMI != NME;) {
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +0000324 NamedMDNode *NMD = &*NMI;
Manman Rencb14bbc2013-11-22 22:06:31 +0000325 ++NMI;
326 if (NMD->getName().startswith("llvm.dbg.")) {
327 NMD->eraseFromParent();
328 Changed = true;
329 }
330 }
331
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000332 for (Function &F : M)
333 Changed |= stripDebugInfo(F);
334
Rafael Espindola468b8682015-04-01 14:44:59 +0000335 if (GVMaterializer *Materializer = M.getMaterializer())
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000336 Materializer->setStripDebugInfo();
Manman Rencb14bbc2013-11-22 22:06:31 +0000337
338 return Changed;
339}
Manman Ren8b4306c2013-12-02 21:29:56 +0000340
Manman Renbd4daf82013-12-03 00:12:14 +0000341unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
David Majnemere7a9cdb2015-02-16 06:04:53 +0000342 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000343 M.getModuleFlag("Debug Info Version")))
344 return Val->getZExtValue();
345 return 0;
Manman Ren8b4306c2013-12-02 21:29:56 +0000346}