1 //===--- 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
15 #include "llvm/IR/DebugInfo.h"
16 #include "LLVMContextImpl.h"
17 #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"
22 #include "llvm/IR/DIBuilder.h"
23 #include "llvm/IR/DerivedTypes.h"
24 #include "llvm/IR/Instructions.h"
25 #include "llvm/IR/IntrinsicInst.h"
26 #include "llvm/IR/Intrinsics.h"
27 #include "llvm/IR/GVMaterializer.h"
28 #include "llvm/IR/Module.h"
29 #include "llvm/IR/ValueHandle.h"
30 #include "llvm/Support/Debug.h"
31 #include "llvm/Support/Dwarf.h"
32 #include "llvm/Support/raw_ostream.h"
33 using namespace llvm;
34 using namespace llvm::dwarf;
35
getDISubprogram(const MDNode * Scope)36 DISubprogram llvm::getDISubprogram(const MDNode *Scope) {
37 if (auto *LocalScope = dyn_cast_or_null<MDLocalScope>(Scope))
38 return LocalScope->getSubprogram();
39 return nullptr;
40 }
41
getDISubprogram(const Function * F)42 DISubprogram llvm::getDISubprogram(const Function *F) {
43 // We look for the first instr that has a debug annotation leading back to F.
44 for (auto &BB : *F) {
45 auto Inst = std::find_if(BB.begin(), BB.end(), [](const Instruction &Inst) {
46 return Inst.getDebugLoc();
47 });
48 if (Inst == BB.end())
49 continue;
50 DebugLoc DLoc = Inst->getDebugLoc();
51 const MDNode *Scope = DLoc.getInlinedAtScope();
52 DISubprogram Subprogram = getDISubprogram(Scope);
53 return Subprogram->describes(F) ? Subprogram : DISubprogram();
54 }
55
56 return DISubprogram();
57 }
58
getDICompositeType(DIType T)59 DICompositeType llvm::getDICompositeType(DIType T) {
60 if (auto *C = dyn_cast_or_null<MDCompositeTypeBase>(T))
61 return C;
62
63 if (auto *D = dyn_cast_or_null<MDDerivedTypeBase>(T)) {
64 // This function is currently used by dragonegg and dragonegg does
65 // not generate identifier for types, so using an empty map to resolve
66 // DerivedFrom should be fine.
67 DITypeIdentifierMap EmptyMap;
68 return getDICompositeType(D->getBaseType().resolve(EmptyMap));
69 }
70
71 return nullptr;
72 }
73
74 DITypeIdentifierMap
generateDITypeIdentifierMap(const NamedMDNode * CU_Nodes)75 llvm::generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes) {
76 DITypeIdentifierMap Map;
77 for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
78 auto *CU = cast<MDCompileUnit>(CU_Nodes->getOperand(CUi));
79 DIArray Retain = CU->getRetainedTypes();
80 for (unsigned Ti = 0, Te = Retain.size(); Ti != Te; ++Ti) {
81 if (!isa<MDCompositeType>(Retain[Ti]))
82 continue;
83 auto *Ty = cast<MDCompositeType>(Retain[Ti]);
84 if (MDString *TypeId = Ty->getRawIdentifier()) {
85 // Definition has priority over declaration.
86 // Try to insert (TypeId, Ty) to Map.
87 std::pair<DITypeIdentifierMap::iterator, bool> P =
88 Map.insert(std::make_pair(TypeId, Ty));
89 // If TypeId already exists in Map and this is a definition, replace
90 // whatever we had (declaration or definition) with the definition.
91 if (!P.second && !Ty->isForwardDecl())
92 P.first->second = Ty;
93 }
94 }
95 }
96 return Map;
97 }
98
99 //===----------------------------------------------------------------------===//
100 // DebugInfoFinder implementations.
101 //===----------------------------------------------------------------------===//
102
reset()103 void DebugInfoFinder::reset() {
104 CUs.clear();
105 SPs.clear();
106 GVs.clear();
107 TYs.clear();
108 Scopes.clear();
109 NodesSeen.clear();
110 TypeIdentifierMap.clear();
111 TypeMapInitialized = false;
112 }
113
InitializeTypeMap(const Module & M)114 void DebugInfoFinder::InitializeTypeMap(const Module &M) {
115 if (!TypeMapInitialized)
116 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
117 TypeIdentifierMap = generateDITypeIdentifierMap(CU_Nodes);
118 TypeMapInitialized = true;
119 }
120 }
121
processModule(const Module & M)122 void DebugInfoFinder::processModule(const Module &M) {
123 InitializeTypeMap(M);
124 if (NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu")) {
125 for (unsigned i = 0, e = CU_Nodes->getNumOperands(); i != e; ++i) {
126 DICompileUnit CU = cast<MDCompileUnit>(CU_Nodes->getOperand(i));
127 addCompileUnit(CU);
128 for (DIGlobalVariable DIG : CU->getGlobalVariables()) {
129 if (addGlobalVariable(DIG)) {
130 processScope(DIG->getScope());
131 processType(DIG->getType().resolve(TypeIdentifierMap));
132 }
133 }
134 for (auto *SP : CU->getSubprograms())
135 processSubprogram(SP);
136 for (auto *ET : CU->getEnumTypes())
137 processType(ET);
138 for (auto *RT : CU->getRetainedTypes())
139 processType(RT);
140 for (DIImportedEntity Import : CU->getImportedEntities()) {
141 auto *Entity = Import->getEntity().resolve(TypeIdentifierMap);
142 if (auto *T = dyn_cast<MDType>(Entity))
143 processType(T);
144 else if (auto *SP = dyn_cast<MDSubprogram>(Entity))
145 processSubprogram(SP);
146 else if (auto *NS = dyn_cast<MDNamespace>(Entity))
147 processScope(NS->getScope());
148 }
149 }
150 }
151 }
152
processLocation(const Module & M,DILocation Loc)153 void DebugInfoFinder::processLocation(const Module &M, DILocation Loc) {
154 if (!Loc)
155 return;
156 InitializeTypeMap(M);
157 processScope(Loc->getScope());
158 processLocation(M, Loc->getInlinedAt());
159 }
160
processType(DIType DT)161 void DebugInfoFinder::processType(DIType DT) {
162 if (!addType(DT))
163 return;
164 processScope(DT->getScope().resolve(TypeIdentifierMap));
165 if (auto *DCT = dyn_cast<MDCompositeTypeBase>(DT)) {
166 processType(DCT->getBaseType().resolve(TypeIdentifierMap));
167 if (auto *ST = dyn_cast<MDSubroutineType>(DCT)) {
168 for (MDTypeRef Ref : ST->getTypeArray())
169 processType(Ref.resolve(TypeIdentifierMap));
170 return;
171 }
172 for (Metadata *D : DCT->getElements()) {
173 if (auto *T = dyn_cast<MDType>(D))
174 processType(T);
175 else if (DISubprogram SP = dyn_cast<MDSubprogram>(D))
176 processSubprogram(SP);
177 }
178 } else if (auto *DDT = dyn_cast<MDDerivedTypeBase>(DT)) {
179 processType(DDT->getBaseType().resolve(TypeIdentifierMap));
180 }
181 }
182
processScope(DIScope Scope)183 void DebugInfoFinder::processScope(DIScope Scope) {
184 if (!Scope)
185 return;
186 if (DIType Ty = dyn_cast<MDType>(Scope)) {
187 processType(Ty);
188 return;
189 }
190 if (DICompileUnit CU = dyn_cast<MDCompileUnit>(Scope)) {
191 addCompileUnit(CU);
192 return;
193 }
194 if (DISubprogram SP = dyn_cast<MDSubprogram>(Scope)) {
195 processSubprogram(SP);
196 return;
197 }
198 if (!addScope(Scope))
199 return;
200 if (auto *LB = dyn_cast<MDLexicalBlockBase>(Scope)) {
201 processScope(LB->getScope());
202 } else if (auto *NS = dyn_cast<MDNamespace>(Scope)) {
203 processScope(NS->getScope());
204 }
205 }
206
processSubprogram(DISubprogram SP)207 void DebugInfoFinder::processSubprogram(DISubprogram SP) {
208 if (!addSubprogram(SP))
209 return;
210 processScope(SP->getScope().resolve(TypeIdentifierMap));
211 processType(SP->getType());
212 for (auto *Element : SP->getTemplateParams()) {
213 if (auto *TType = dyn_cast<MDTemplateTypeParameter>(Element)) {
214 processType(TType->getType().resolve(TypeIdentifierMap));
215 } else if (auto *TVal = dyn_cast<MDTemplateValueParameter>(Element)) {
216 processType(TVal->getType().resolve(TypeIdentifierMap));
217 }
218 }
219 }
220
processDeclare(const Module & M,const DbgDeclareInst * DDI)221 void DebugInfoFinder::processDeclare(const Module &M,
222 const DbgDeclareInst *DDI) {
223 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
224 if (!N)
225 return;
226 InitializeTypeMap(M);
227
228 DIVariable DV = dyn_cast<MDLocalVariable>(N);
229 if (!DV)
230 return;
231
232 if (!NodesSeen.insert(DV).second)
233 return;
234 processScope(DV->getScope());
235 processType(DV->getType().resolve(TypeIdentifierMap));
236 }
237
processValue(const Module & M,const DbgValueInst * DVI)238 void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
239 MDNode *N = dyn_cast<MDNode>(DVI->getVariable());
240 if (!N)
241 return;
242 InitializeTypeMap(M);
243
244 DIVariable DV = dyn_cast<MDLocalVariable>(N);
245 if (!DV)
246 return;
247
248 if (!NodesSeen.insert(DV).second)
249 return;
250 processScope(DV->getScope());
251 processType(DV->getType().resolve(TypeIdentifierMap));
252 }
253
addType(DIType DT)254 bool DebugInfoFinder::addType(DIType DT) {
255 if (!DT)
256 return false;
257
258 if (!NodesSeen.insert(DT).second)
259 return false;
260
261 TYs.push_back(DT);
262 return true;
263 }
264
addCompileUnit(DICompileUnit CU)265 bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
266 if (!CU)
267 return false;
268 if (!NodesSeen.insert(CU).second)
269 return false;
270
271 CUs.push_back(CU);
272 return true;
273 }
274
addGlobalVariable(DIGlobalVariable DIG)275 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
276 if (!DIG)
277 return false;
278
279 if (!NodesSeen.insert(DIG).second)
280 return false;
281
282 GVs.push_back(DIG);
283 return true;
284 }
285
addSubprogram(DISubprogram SP)286 bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
287 if (!SP)
288 return false;
289
290 if (!NodesSeen.insert(SP).second)
291 return false;
292
293 SPs.push_back(SP);
294 return true;
295 }
296
addScope(DIScope Scope)297 bool DebugInfoFinder::addScope(DIScope Scope) {
298 if (!Scope)
299 return false;
300 // FIXME: Ocaml binding generates a scope with no content, we treat it
301 // as null for now.
302 if (Scope->getNumOperands() == 0)
303 return false;
304 if (!NodesSeen.insert(Scope).second)
305 return false;
306 Scopes.push_back(Scope);
307 return true;
308 }
309
stripDebugInfo(Function & F)310 bool llvm::stripDebugInfo(Function &F) {
311 bool Changed = false;
312 for (BasicBlock &BB : F) {
313 for (Instruction &I : BB) {
314 if (I.getDebugLoc()) {
315 Changed = true;
316 I.setDebugLoc(DebugLoc());
317 }
318 }
319 }
320 return Changed;
321 }
322
StripDebugInfo(Module & M)323 bool llvm::StripDebugInfo(Module &M) {
324 bool Changed = false;
325
326 // Remove all of the calls to the debugger intrinsics, and remove them from
327 // the module.
328 if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
329 while (!Declare->use_empty()) {
330 CallInst *CI = cast<CallInst>(Declare->user_back());
331 CI->eraseFromParent();
332 }
333 Declare->eraseFromParent();
334 Changed = true;
335 }
336
337 if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
338 while (!DbgVal->use_empty()) {
339 CallInst *CI = cast<CallInst>(DbgVal->user_back());
340 CI->eraseFromParent();
341 }
342 DbgVal->eraseFromParent();
343 Changed = true;
344 }
345
346 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
347 NME = M.named_metadata_end(); NMI != NME;) {
348 NamedMDNode *NMD = NMI;
349 ++NMI;
350 if (NMD->getName().startswith("llvm.dbg.")) {
351 NMD->eraseFromParent();
352 Changed = true;
353 }
354 }
355
356 for (Function &F : M)
357 Changed |= stripDebugInfo(F);
358
359 if (GVMaterializer *Materializer = M.getMaterializer())
360 Materializer->setStripDebugInfo();
361
362 return Changed;
363 }
364
getDebugMetadataVersionFromModule(const Module & M)365 unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
366 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
367 M.getModuleFlag("Debug Info Version")))
368 return Val->getZExtValue();
369 return 0;
370 }
371
372 llvm::DenseMap<const llvm::Function *, llvm::DISubprogram>
makeSubprogramMap(const Module & M)373 llvm::makeSubprogramMap(const Module &M) {
374 DenseMap<const Function *, DISubprogram> R;
375
376 NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
377 if (!CU_Nodes)
378 return R;
379
380 for (MDNode *N : CU_Nodes->operands()) {
381 DICompileUnit CUNode = cast<MDCompileUnit>(N);
382 for (DISubprogram SP : CUNode->getSubprograms()) {
383 if (Function *F = SP->getFunction())
384 R.insert(std::make_pair(F, SP));
385 }
386 }
387 return R;
388 }
389