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