LLVM  3.7.0
DebugInfo.cpp
Go to the documentation of this file.
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"
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"
33 using namespace llvm;
34 using namespace llvm::dwarf;
35 
37  if (auto *LocalScope = dyn_cast_or_null<DILocalScope>(Scope))
38  return LocalScope->getSubprogram();
39  return nullptr;
40 }
41 
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  auto *Subprogram = getDISubprogram(Scope);
53  return Subprogram->describes(F) ? Subprogram : nullptr;
54  }
55 
56  return nullptr;
57 }
58 
60  if (auto *C = dyn_cast_or_null<DICompositeTypeBase>(T))
61  return C;
62 
63  if (auto *D = dyn_cast_or_null<DIDerivedTypeBase>(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 
77  for (unsigned CUi = 0, CUe = CU_Nodes->getNumOperands(); CUi != CUe; ++CUi) {
78  auto *CU = cast<DICompileUnit>(CU_Nodes->getOperand(CUi));
79  DINodeArray Retain = CU->getRetainedTypes();
80  for (unsigned Ti = 0, Te = Retain.size(); Ti != Te; ++Ti) {
81  if (!isa<DICompositeType>(Retain[Ti]))
82  continue;
83  auto *Ty = cast<DICompositeType>(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 
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 
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  auto *CU = cast<DICompileUnit>(CU_Nodes->getOperand(i));
127  addCompileUnit(CU);
128  for (auto *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 (auto *Import : CU->getImportedEntities()) {
141  auto *Entity = Import->getEntity().resolve(TypeIdentifierMap);
142  if (auto *T = dyn_cast<DIType>(Entity))
143  processType(T);
144  else if (auto *SP = dyn_cast<DISubprogram>(Entity))
145  processSubprogram(SP);
146  else if (auto *NS = dyn_cast<DINamespace>(Entity))
147  processScope(NS->getScope());
148  else if (auto *M = dyn_cast<DIModule>(Entity))
149  processScope(M->getScope());
150  }
151  }
152  }
153 }
154 
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<DICompositeTypeBase>(DT)) {
168  processType(DCT->getBaseType().resolve(TypeIdentifierMap));
169  if (auto *ST = dyn_cast<DISubroutineType>(DCT)) {
170  for (DITypeRef Ref : ST->getTypeArray())
171  processType(Ref.resolve(TypeIdentifierMap));
172  return;
173  }
174  for (Metadata *D : DCT->getElements()) {
175  if (auto *T = dyn_cast<DIType>(D))
176  processType(T);
177  else if (auto *SP = dyn_cast<DISubprogram>(D))
178  processSubprogram(SP);
179  }
180  } else if (auto *DDT = dyn_cast<DIDerivedTypeBase>(DT)) {
181  processType(DDT->getBaseType().resolve(TypeIdentifierMap));
182  }
183 }
184 
185 void DebugInfoFinder::processScope(DIScope *Scope) {
186  if (!Scope)
187  return;
188  if (auto *Ty = dyn_cast<DIType>(Scope)) {
189  processType(Ty);
190  return;
191  }
192  if (auto *CU = dyn_cast<DICompileUnit>(Scope)) {
193  addCompileUnit(CU);
194  return;
195  }
196  if (auto *SP = dyn_cast<DISubprogram>(Scope)) {
197  processSubprogram(SP);
198  return;
199  }
200  if (!addScope(Scope))
201  return;
202  if (auto *LB = dyn_cast<DILexicalBlockBase>(Scope)) {
203  processScope(LB->getScope());
204  } else if (auto *NS = dyn_cast<DINamespace>(Scope)) {
205  processScope(NS->getScope());
206  } else if (auto *M = dyn_cast<DIModule>(Scope)) {
207  processScope(M->getScope());
208  }
209 }
210 
211 void DebugInfoFinder::processSubprogram(DISubprogram *SP) {
212  if (!addSubprogram(SP))
213  return;
214  processScope(SP->getScope().resolve(TypeIdentifierMap));
215  processType(SP->getType());
216  for (auto *Element : SP->getTemplateParams()) {
217  if (auto *TType = dyn_cast<DITemplateTypeParameter>(Element)) {
218  processType(TType->getType().resolve(TypeIdentifierMap));
219  } else if (auto *TVal = dyn_cast<DITemplateValueParameter>(Element)) {
220  processType(TVal->getType().resolve(TypeIdentifierMap));
221  }
222  }
223 }
224 
226  const DbgDeclareInst *DDI) {
227  auto *N = dyn_cast<MDNode>(DDI->getVariable());
228  if (!N)
229  return;
230  InitializeTypeMap(M);
231 
232  auto *DV = dyn_cast<DILocalVariable>(N);
233  if (!DV)
234  return;
235 
236  if (!NodesSeen.insert(DV).second)
237  return;
238  processScope(DV->getScope());
239  processType(DV->getType().resolve(TypeIdentifierMap));
240 }
241 
243  auto *N = dyn_cast<MDNode>(DVI->getVariable());
244  if (!N)
245  return;
246  InitializeTypeMap(M);
247 
248  auto *DV = dyn_cast<DILocalVariable>(N);
249  if (!DV)
250  return;
251 
252  if (!NodesSeen.insert(DV).second)
253  return;
254  processScope(DV->getScope());
255  processType(DV->getType().resolve(TypeIdentifierMap));
256 }
257 
258 bool DebugInfoFinder::addType(DIType *DT) {
259  if (!DT)
260  return false;
261 
262  if (!NodesSeen.insert(DT).second)
263  return false;
264 
265  TYs.push_back(const_cast<DIType *>(DT));
266  return true;
267 }
268 
269 bool DebugInfoFinder::addCompileUnit(DICompileUnit *CU) {
270  if (!CU)
271  return false;
272  if (!NodesSeen.insert(CU).second)
273  return false;
274 
275  CUs.push_back(CU);
276  return true;
277 }
278 
279 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable *DIG) {
280  if (!DIG)
281  return false;
282 
283  if (!NodesSeen.insert(DIG).second)
284  return false;
285 
286  GVs.push_back(DIG);
287  return true;
288 }
289 
290 bool DebugInfoFinder::addSubprogram(DISubprogram *SP) {
291  if (!SP)
292  return false;
293 
294  if (!NodesSeen.insert(SP).second)
295  return false;
296 
297  SPs.push_back(SP);
298  return true;
299 }
300 
301 bool DebugInfoFinder::addScope(DIScope *Scope) {
302  if (!Scope)
303  return false;
304  // FIXME: Ocaml binding generates a scope with no content, we treat it
305  // as null for now.
306  if (Scope->getNumOperands() == 0)
307  return false;
308  if (!NodesSeen.insert(Scope).second)
309  return false;
310  Scopes.push_back(Scope);
311  return true;
312 }
313 
315  bool Changed = false;
316  for (BasicBlock &BB : F) {
317  for (Instruction &I : BB) {
318  if (I.getDebugLoc()) {
319  Changed = true;
320  I.setDebugLoc(DebugLoc());
321  }
322  }
323  }
324  return Changed;
325 }
326 
328  bool Changed = false;
329 
330  // Remove all of the calls to the debugger intrinsics, and remove them from
331  // the module.
332  if (Function *Declare = M.getFunction("llvm.dbg.declare")) {
333  while (!Declare->use_empty()) {
334  CallInst *CI = cast<CallInst>(Declare->user_back());
335  CI->eraseFromParent();
336  }
337  Declare->eraseFromParent();
338  Changed = true;
339  }
340 
341  if (Function *DbgVal = M.getFunction("llvm.dbg.value")) {
342  while (!DbgVal->use_empty()) {
343  CallInst *CI = cast<CallInst>(DbgVal->user_back());
344  CI->eraseFromParent();
345  }
346  DbgVal->eraseFromParent();
347  Changed = true;
348  }
349 
351  NME = M.named_metadata_end(); NMI != NME;) {
352  NamedMDNode *NMD = NMI;
353  ++NMI;
354  if (NMD->getName().startswith("llvm.dbg.")) {
355  NMD->eraseFromParent();
356  Changed = true;
357  }
358  }
359 
360  for (Function &F : M)
361  Changed |= stripDebugInfo(F);
362 
363  if (GVMaterializer *Materializer = M.getMaterializer())
364  Materializer->setStripDebugInfo();
365 
366  return Changed;
367 }
368 
370  if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
371  M.getModuleFlag("Debug Info Version")))
372  return Val->getZExtValue();
373  return 0;
374 }
375 
379 
380  NamedMDNode *CU_Nodes = M.getNamedMetadata("llvm.dbg.cu");
381  if (!CU_Nodes)
382  return R;
383 
384  for (MDNode *N : CU_Nodes->operands()) {
385  auto *CUNode = cast<DICompileUnit>(N);
386  for (auto *SP : CUNode->getSubprograms()) {
387  if (Function *F = SP->getFunction())
388  R.insert(std::make_pair(F, SP));
389  }
390  }
391  return R;
392 }
iplist< Instruction >::iterator eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing basic block and deletes it...
Definition: Instruction.cpp:70
StringRef getName() const
Definition: Metadata.cpp:986
void processLocation(const Module &M, const DILocation *Loc)
Process debug info location.
Definition: DebugInfo.cpp:155
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:942
CallInst - This class represents a function call, abstracting a target machine's calling convention...
named_metadata_iterator named_metadata_end()
Definition: Module.h:614
unsigned getDebugMetadataVersionFromModule(const Module &M)
Return Debug Info Metadata Version by checking module flags.
Definition: DebugInfo.cpp:369
void reset()
Clear all lists.
Definition: DebugInfo.cpp:103
A debug info location.
Definition: DebugLoc.h:34
Metadata node.
Definition: Metadata.h:740
F(f)
bool stripDebugInfo(Function &F)
Definition: DebugInfo.cpp:314
void processModule(const Module &M)
Process entire module and collect debug info anchors.
Definition: DebugInfo.cpp:122
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: DenseMap.h:169
A tuple of MDNodes.
Definition: Metadata.h:1127
void eraseFromParent()
Drop all references and remove the node from parent module.
Definition: Metadata.cpp:978
DICompositeTypeBase * getDICompositeType(DIType *T)
Find underlying composite type.
Definition: DebugInfo.cpp:59
Pointer union between a subclass of DINode and MDString.
DISubprogram * getDISubprogram(const MDNode *Scope)
Find subprogram that is enclosing this scope.
Definition: DebugInfo.cpp:36
T * resolve(const MapTy &Map) const
Subprogram description.
bool StripDebugInfo(Module &M)
Strip debug info in the module if it exists.
Definition: DebugInfo.cpp:327
DIScopeRef getScope() const
Debug location.
iterator_range< op_iterator > operands()
Definition: Metadata.h:1210
DIScopeRef getScope() const
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
Definition: Module.cpp:188
#define P(N)
LLVM Basic Block Representation.
Definition: BasicBlock.h:65
This file contains the declarations for the subclasses of Constant, which represent the different fla...
MDNode * getOperand(unsigned i) const
Definition: Metadata.cpp:965
MDNode * getInlinedAtScope() const
Get the fully inlined-at scope for a DebugLoc.
Definition: DebugLoc.cpp:46
void processValue(const Module &M, const DbgValueInst *DVI)
Process DbgValueInst.
Definition: DebugInfo.cpp:242
Base class for scope-like contexts.
Metadata * getModuleFlag(StringRef Key) const
Return the corresponding value if Key appears in module flags, otherwise return null.
Definition: Module.cpp:313
bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:215
Base class for types.
Module.h This file contains the declarations for the Module class.
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:285
NamedMDNode * getNamedMetadata(const Twine &Name) const
Return the first NamedMDNode in the module with the specified name.
Definition: Module.cpp:253
Base class for DICompositeType and DISubroutineType.
DbgValueInst - This represents the llvm.dbg.value instruction.
void processDeclare(const Module &M, const DbgDeclareInst *DDI)
Process DbgDeclareInst.
Definition: DebugInfo.cpp:225
#define I(x, y, z)
Definition: MD5.cpp:54
#define N
NamedMDListType::iterator named_metadata_iterator
The named metadata iterators.
Definition: Module.h:150
DILocalVariable * getVariable() const
Definition: IntrinsicInst.h:85
DenseMap< const Function *, DISubprogram * > makeSubprogramMap(const Module &M)
Definition: DebugInfo.cpp:377
DILocalVariable * getVariable() const
unsigned getNumOperands() const
Definition: Metadata.cpp:961
C - The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
A single uniqued string.
Definition: Metadata.h:508
DITypeIdentifierMap generateDITypeIdentifierMap(const NamedMDNode *CU_Nodes)
Generate map by visiting all retained types.
Definition: DebugInfo.cpp:75
DISubprogram * getSubprogram() const
Get the subprogram for this scope.
DbgDeclareInst - This represents the llvm.dbg.declare instruction.
Definition: IntrinsicInst.h:82
Root of the metadata hierarchy.
Definition: Metadata.h:45
named_metadata_iterator named_metadata_begin()
Definition: Module.h:609