DebugInfo: Gut DIDescriptor
[oota-llvm.git] / lib / IR / DebugInfo.cpp
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
310 template <>
311 DIDescriptor
312 DIRef<DIDescriptor>::resolve(const DITypeIdentifierMap &Map) const {
313   return DIDescriptor(DebugNodeRef(Val).resolve(Map));
314 }
315 template <>
316 DIScope DIRef<DIScope>::resolve(const DITypeIdentifierMap &Map) const {
317   return MDScopeRef(Val).resolve(Map);
318 }
319 template <>
320 DIType DIRef<DIType>::resolve(const DITypeIdentifierMap &Map) const {
321   return MDTypeRef(Val).resolve(Map);
322 }
323
324 bool llvm::stripDebugInfo(Function &F) {
325   bool Changed = false;
326   for (BasicBlock &BB : F) {
327     for (Instruction &I : BB) {
328       if (I.getDebugLoc()) {
329         Changed = true;
330         I.setDebugLoc(DebugLoc());
331       }
332     }
333   }
334   return Changed;
335 }
336
337 bool llvm::StripDebugInfo(Module &M) {
338   bool Changed = false;
339
340   // Remove all of the calls to the debugger intrinsics, and remove them from
341   // the module.
342   if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
343     while (!Declare->use_empty()) {
344       CallInst *CI = cast<CallInst>(Declare->user_back());
345       CI->eraseFromParent();
346     }
347     Declare->eraseFromParent();
348     Changed = true;
349   }
350
351   if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
352     while (!DbgVal->use_empty()) {
353       CallInst *CI = cast<CallInst>(DbgVal->user_back());
354       CI->eraseFromParent();
355     }
356     DbgVal->eraseFromParent();
357     Changed = true;
358   }
359
360   for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
361          NME = M.named_metadata_end(); NMI != NME;) {
362     NamedMDNode *NMD = NMI;
363     ++NMI;
364     if (NMD->getName().startswith("llvm.dbg.")) {
365       NMD->eraseFromParent();
366       Changed = true;
367     }
368   }
369
370   for (Function &F : M)
371     Changed |= stripDebugInfo(F);
372
373   if (GVMaterializer *Materializer = M.getMaterializer())
374     Materializer->setStripDebugInfo();
375
376   return Changed;
377 }
378
379 unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
380   if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
381           M.getModuleFlag("Debug Info Version")))
382     return Val->getZExtValue();
383   return 0;
384 }
385
386 llvm::DenseMap<const llvm::Function *, llvm::DISubprogram>
387 llvm::makeSubprogramMap(const Module &M) {
388   DenseMap<const Function *, DISubprogram> R;
389
390   NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
391   if (!CU_Nodes)
392     return R;
393
394   for (MDNode *N : CU_Nodes->operands()) {
395     DICompileUnit CUNode = cast<MDCompileUnit>(N);
396     for (DISubprogram SP : CUNode->getSubprograms()) {
397       if (Function *F = SP->getFunction())
398         R.insert(std::make_pair(F, SP));
399     }
400   }
401   return R;
402 }