blob: 78dc910807f62d6c4f108fdbd82aeaae9db88023 [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"
Bill Wendling523bea82013-11-08 08:13:15 +000019#include "llvm/IR/Constants.h"
Adrian Prantlb1416832014-08-01 22:11:58 +000020#include "llvm/IR/DIBuilder.h"
Bill Wendling523bea82013-11-08 08:13:15 +000021#include "llvm/IR/DerivedTypes.h"
Mehdi Aminib550cb12016-04-18 09:17:29 +000022#include "llvm/IR/GVMaterializer.h"
Bill Wendling523bea82013-11-08 08:13:15 +000023#include "llvm/IR/Instructions.h"
24#include "llvm/IR/IntrinsicInst.h"
25#include "llvm/IR/Intrinsics.h"
26#include "llvm/IR/Module.h"
Chandler Carruth4220e9c2014-03-04 11:17:44 +000027#include "llvm/IR/ValueHandle.h"
Bill Wendling523bea82013-11-08 08:13:15 +000028#include "llvm/Support/Debug.h"
29#include "llvm/Support/Dwarf.h"
Bill Wendling523bea82013-11-08 08:13:15 +000030#include "llvm/Support/raw_ostream.h"
31using namespace llvm;
32using namespace llvm::dwarf;
33
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000034DISubprogram *llvm::getDISubprogram(const MDNode *Scope) {
35 if (auto *LocalScope = dyn_cast_or_null<DILocalScope>(Scope))
Duncan P. N. Exon Smithdd77af82015-03-31 02:06:28 +000036 return LocalScope->getSubprogram();
37 return nullptr;
Bill Wendling523bea82013-11-08 08:13:15 +000038}
39
Bill Wendling523bea82013-11-08 08:13:15 +000040DITypeIdentifierMap
Adrian Prantl5992a722016-04-08 22:43:03 +000041llvm::generateDITypeIdentifierMap(const Module &M) {
Bill Wendling523bea82013-11-08 08:13:15 +000042 DITypeIdentifierMap Map;
Adrian Prantl5992a722016-04-08 22:43:03 +000043 for (DICompileUnit *CU : M.debug_compile_units()) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000044 DINodeArray Retain = CU->getRetainedTypes();
Duncan P. N. Exon Smith000fa2c2015-04-07 04:14:33 +000045 for (unsigned Ti = 0, Te = Retain.size(); Ti != Te; ++Ti) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000046 if (!isa<DICompositeType>(Retain[Ti]))
Bill Wendling523bea82013-11-08 08:13:15 +000047 continue;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +000048 auto *Ty = cast<DICompositeType>(Retain[Ti]);
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +000049 if (MDString *TypeId = Ty->getRawIdentifier()) {
Bill Wendling523bea82013-11-08 08:13:15 +000050 // Definition has priority over declaration.
51 // Try to insert (TypeId, Ty) to Map.
52 std::pair<DITypeIdentifierMap::iterator, bool> P =
53 Map.insert(std::make_pair(TypeId, Ty));
54 // If TypeId already exists in Map and this is a definition, replace
55 // whatever we had (declaration or definition) with the definition.
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +000056 if (!P.second && !Ty->isForwardDecl())
Bill Wendling523bea82013-11-08 08:13:15 +000057 P.first->second = Ty;
58 }
59 }
60 }
61 return Map;
62}
63
64//===----------------------------------------------------------------------===//
65// DebugInfoFinder implementations.
66//===----------------------------------------------------------------------===//
67
68void DebugInfoFinder::reset() {
69 CUs.clear();
70 SPs.clear();
71 GVs.clear();
72 TYs.clear();
73 Scopes.clear();
74 NodesSeen.clear();
75 TypeIdentifierMap.clear();
Manman Ren2085ccc2013-11-17 18:42:37 +000076 TypeMapInitialized = false;
77}
78
Manman Renb46e5502013-11-17 19:35:03 +000079void DebugInfoFinder::InitializeTypeMap(const Module &M) {
Adrian Prantl5992a722016-04-08 22:43:03 +000080 if (TypeMapInitialized)
81 return;
82 TypeIdentifierMap = generateDITypeIdentifierMap(M);
83 TypeMapInitialized = true;
Bill Wendling523bea82013-11-08 08:13:15 +000084}
85
Bill Wendling523bea82013-11-08 08:13:15 +000086void DebugInfoFinder::processModule(const Module &M) {
Manman Renb46e5502013-11-17 19:35:03 +000087 InitializeTypeMap(M);
Adrian Prantl5992a722016-04-08 22:43:03 +000088 for (auto *CU : M.debug_compile_units()) {
89 addCompileUnit(CU);
90 for (auto *DIG : CU->getGlobalVariables()) {
91 if (addGlobalVariable(DIG)) {
92 processScope(DIG->getScope());
93 processType(DIG->getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +000094 }
Adrian Prantl5992a722016-04-08 22:43:03 +000095 }
Adrian Prantl5992a722016-04-08 22:43:03 +000096 for (auto *ET : CU->getEnumTypes())
97 processType(ET);
98 for (auto *RT : CU->getRetainedTypes())
Adrian Prantl75819ae2016-04-15 15:57:41 +000099 if (auto *T = dyn_cast<DIType>(RT))
100 processType(T);
101 else
102 processSubprogram(cast<DISubprogram>(RT));
Adrian Prantl5992a722016-04-08 22:43:03 +0000103 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 }
Adrian Prantl75819ae2016-04-15 15:57:41 +0000115 for (auto &F : M.functions())
116 if (auto *SP = cast_or_null<DISubprogram>(F.getSubprogram()))
117 processSubprogram(SP);
Bill Wendling523bea82013-11-08 08:13:15 +0000118}
119
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000120void DebugInfoFinder::processLocation(const Module &M, const DILocation *Loc) {
Bill Wendling523bea82013-11-08 08:13:15 +0000121 if (!Loc)
122 return;
Manman Renb46e5502013-11-17 19:35:03 +0000123 InitializeTypeMap(M);
Duncan P. N. Exon Smithb7e221b2015-04-14 01:35:55 +0000124 processScope(Loc->getScope());
125 processLocation(M, Loc->getInlinedAt());
Bill Wendling523bea82013-11-08 08:13:15 +0000126}
127
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000128void DebugInfoFinder::processType(DIType *DT) {
Bill Wendling523bea82013-11-08 08:13:15 +0000129 if (!addType(DT))
130 return;
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000131 processScope(DT->getScope().resolve(TypeIdentifierMap));
Duncan P. N. Exon Smith260fa8a2015-07-24 20:56:10 +0000132 if (auto *ST = dyn_cast<DISubroutineType>(DT)) {
133 for (DITypeRef Ref : ST->getTypeArray())
134 processType(Ref.resolve(TypeIdentifierMap));
135 return;
136 }
137 if (auto *DCT = dyn_cast<DICompositeType>(DT)) {
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000138 processType(DCT->getBaseType().resolve(TypeIdentifierMap));
Anders Waldenborg1433fd42015-04-14 09:18:17 +0000139 for (Metadata *D : DCT->getElements()) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000140 if (auto *T = dyn_cast<DIType>(D))
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000141 processType(T);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000142 else if (auto *SP = dyn_cast<DISubprogram>(D))
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000143 processSubprogram(SP);
Bill Wendling523bea82013-11-08 08:13:15 +0000144 }
Duncan P. N. Exon Smith260fa8a2015-07-24 20:56:10 +0000145 return;
146 }
147 if (auto *DDT = dyn_cast<DIDerivedType>(DT)) {
Duncan P. N. Exon Smithb1055642015-04-16 01:01:28 +0000148 processType(DDT->getBaseType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000149 }
150}
151
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000152void DebugInfoFinder::processScope(DIScope *Scope) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000153 if (!Scope)
154 return;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000155 if (auto *Ty = dyn_cast<DIType>(Scope)) {
Bill Wendling523bea82013-11-08 08:13:15 +0000156 processType(Ty);
157 return;
158 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000159 if (auto *CU = dyn_cast<DICompileUnit>(Scope)) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000160 addCompileUnit(CU);
Bill Wendling523bea82013-11-08 08:13:15 +0000161 return;
162 }
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000163 if (auto *SP = dyn_cast<DISubprogram>(Scope)) {
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000164 processSubprogram(SP);
Bill Wendling523bea82013-11-08 08:13:15 +0000165 return;
166 }
167 if (!addScope(Scope))
168 return;
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000169 if (auto *LB = dyn_cast<DILexicalBlockBase>(Scope)) {
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000170 processScope(LB->getScope());
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000171 } else if (auto *NS = dyn_cast<DINamespace>(Scope)) {
Duncan P. N. Exon Smith20caafb2015-04-14 03:01:27 +0000172 processScope(NS->getScope());
Adrian Prantlab1243f2015-06-29 23:03:47 +0000173 } else if (auto *M = dyn_cast<DIModule>(Scope)) {
174 processScope(M->getScope());
Bill Wendling523bea82013-11-08 08:13:15 +0000175 }
176}
177
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000178void DebugInfoFinder::processSubprogram(DISubprogram *SP) {
Bill Wendling523bea82013-11-08 08:13:15 +0000179 if (!addSubprogram(SP))
180 return;
Duncan P. N. Exon Smith537b4a82015-04-14 03:40:37 +0000181 processScope(SP->getScope().resolve(TypeIdentifierMap));
182 processType(SP->getType());
183 for (auto *Element : SP->getTemplateParams()) {
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000184 if (auto *TType = dyn_cast<DITemplateTypeParameter>(Element)) {
Duncan P. N. Exon Smith20caafb2015-04-14 03:01:27 +0000185 processType(TType->getType().resolve(TypeIdentifierMap));
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000186 } else if (auto *TVal = dyn_cast<DITemplateValueParameter>(Element)) {
Duncan P. N. Exon Smith20caafb2015-04-14 03:01:27 +0000187 processType(TVal->getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000188 }
189 }
190}
191
Manman Ren2085ccc2013-11-17 18:42:37 +0000192void DebugInfoFinder::processDeclare(const Module &M,
193 const DbgDeclareInst *DDI) {
Duncan P. N. Exon Smithed557b52015-04-17 23:20:10 +0000194 auto *N = dyn_cast<MDNode>(DDI->getVariable());
Bill Wendling523bea82013-11-08 08:13:15 +0000195 if (!N)
196 return;
Manman Renb46e5502013-11-17 19:35:03 +0000197 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000198
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000199 auto *DV = dyn_cast<DILocalVariable>(N);
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000200 if (!DV)
Bill Wendling523bea82013-11-08 08:13:15 +0000201 return;
202
David Blaikie70573dc2014-11-19 07:49:26 +0000203 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000204 return;
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000205 processScope(DV->getScope());
206 processType(DV->getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000207}
208
Manman Ren2085ccc2013-11-17 18:42:37 +0000209void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
Duncan P. N. Exon Smithed557b52015-04-17 23:20:10 +0000210 auto *N = dyn_cast<MDNode>(DVI->getVariable());
Bill Wendling523bea82013-11-08 08:13:15 +0000211 if (!N)
212 return;
Manman Renb46e5502013-11-17 19:35:03 +0000213 InitializeTypeMap(M);
Bill Wendling523bea82013-11-08 08:13:15 +0000214
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000215 auto *DV = dyn_cast<DILocalVariable>(N);
Duncan P. N. Exon Smith9d1cf4c2015-04-06 23:18:49 +0000216 if (!DV)
Bill Wendling523bea82013-11-08 08:13:15 +0000217 return;
218
David Blaikie70573dc2014-11-19 07:49:26 +0000219 if (!NodesSeen.insert(DV).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000220 return;
Duncan P. N. Exon Smith7348dda2015-04-14 02:22:36 +0000221 processScope(DV->getScope());
222 processType(DV->getType().resolve(TypeIdentifierMap));
Bill Wendling523bea82013-11-08 08:13:15 +0000223}
224
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000225bool DebugInfoFinder::addType(DIType *DT) {
Bill Wendling523bea82013-11-08 08:13:15 +0000226 if (!DT)
227 return false;
228
David Blaikie70573dc2014-11-19 07:49:26 +0000229 if (!NodesSeen.insert(DT).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000230 return false;
231
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000232 TYs.push_back(const_cast<DIType *>(DT));
Bill Wendling523bea82013-11-08 08:13:15 +0000233 return true;
234}
235
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000236bool DebugInfoFinder::addCompileUnit(DICompileUnit *CU) {
Bill Wendling523bea82013-11-08 08:13:15 +0000237 if (!CU)
238 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000239 if (!NodesSeen.insert(CU).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000240 return false;
241
242 CUs.push_back(CU);
243 return true;
244}
245
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000246bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable *DIG) {
Bill Wendling523bea82013-11-08 08:13:15 +0000247 if (!DIG)
248 return false;
249
David Blaikie70573dc2014-11-19 07:49:26 +0000250 if (!NodesSeen.insert(DIG).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000251 return false;
252
253 GVs.push_back(DIG);
254 return true;
255}
256
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000257bool DebugInfoFinder::addSubprogram(DISubprogram *SP) {
Bill Wendling523bea82013-11-08 08:13:15 +0000258 if (!SP)
259 return false;
260
David Blaikie70573dc2014-11-19 07:49:26 +0000261 if (!NodesSeen.insert(SP).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000262 return false;
263
264 SPs.push_back(SP);
265 return true;
266}
267
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000268bool DebugInfoFinder::addScope(DIScope *Scope) {
Bill Wendling523bea82013-11-08 08:13:15 +0000269 if (!Scope)
270 return false;
271 // FIXME: Ocaml binding generates a scope with no content, we treat it
272 // as null for now.
273 if (Scope->getNumOperands() == 0)
274 return false;
David Blaikie70573dc2014-11-19 07:49:26 +0000275 if (!NodesSeen.insert(Scope).second)
Bill Wendling523bea82013-11-08 08:13:15 +0000276 return false;
277 Scopes.push_back(Scope);
278 return true;
279}
280
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000281bool llvm::stripDebugInfo(Function &F) {
282 bool Changed = false;
Peter Collingbourned4bff302015-11-05 22:03:56 +0000283 if (F.getSubprogram()) {
284 Changed = true;
285 F.setSubprogram(nullptr);
286 }
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000287 for (BasicBlock &BB : F) {
288 for (Instruction &I : BB) {
289 if (I.getDebugLoc()) {
290 Changed = true;
291 I.setDebugLoc(DebugLoc());
292 }
293 }
294 }
295 return Changed;
296}
297
Manman Rencb14bbc2013-11-22 22:06:31 +0000298bool llvm::StripDebugInfo(Module &M) {
Manman Rencb14bbc2013-11-22 22:06:31 +0000299 bool Changed = false;
300
301 // Remove all of the calls to the debugger intrinsics, and remove them from
302 // the module.
303 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
304 while (!Declare->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000305 CallInst *CI = cast<CallInst>(Declare->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +0000306 CI->eraseFromParent();
307 }
308 Declare->eraseFromParent();
309 Changed = true;
310 }
311
312 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
313 while (!DbgVal->use_empty()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000314 CallInst *CI = cast<CallInst>(DbgVal->user_back());
Manman Rencb14bbc2013-11-22 22:06:31 +0000315 CI->eraseFromParent();
316 }
317 DbgVal->eraseFromParent();
318 Changed = true;
319 }
320
321 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
322 NME = M.named_metadata_end(); NMI != NME;) {
Duncan P. N. Exon Smith52888a62015-10-08 23:49:46 +0000323 NamedMDNode *NMD = &*NMI;
Manman Rencb14bbc2013-11-22 22:06:31 +0000324 ++NMI;
325 if (NMD->getName().startswith("llvm.dbg.")) {
326 NMD->eraseFromParent();
327 Changed = true;
328 }
329 }
330
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000331 for (Function &F : M)
332 Changed |= stripDebugInfo(F);
333
Rafael Espindola468b8682015-04-01 14:44:59 +0000334 if (GVMaterializer *Materializer = M.getMaterializer())
Rafael Espindola0d68b4c2015-03-30 21:36:43 +0000335 Materializer->setStripDebugInfo();
Manman Rencb14bbc2013-11-22 22:06:31 +0000336
337 return Changed;
338}
Manman Ren8b4306c2013-12-02 21:29:56 +0000339
Manman Renbd4daf82013-12-03 00:12:14 +0000340unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
David Majnemere7a9cdb2015-02-16 06:04:53 +0000341 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000342 M.getModuleFlag("Debug Info Version")))
343 return Val->getZExtValue();
344 return 0;
Manman Ren8b4306c2013-12-02 21:29:56 +0000345}