Line data Source code
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-c/DebugInfo.h"
16 : #include "llvm/ADT/DenseMap.h"
17 : #include "llvm/ADT/DenseSet.h"
18 : #include "llvm/ADT/None.h"
19 : #include "llvm/ADT/STLExtras.h"
20 : #include "llvm/ADT/SmallPtrSet.h"
21 : #include "llvm/ADT/SmallVector.h"
22 : #include "llvm/ADT/StringRef.h"
23 : #include "llvm/IR/BasicBlock.h"
24 : #include "llvm/IR/Constants.h"
25 : #include "llvm/IR/DebugInfoMetadata.h"
26 : #include "llvm/IR/DebugLoc.h"
27 : #include "llvm/IR/DebugInfo.h"
28 : #include "llvm/IR/DIBuilder.h"
29 : #include "llvm/IR/Function.h"
30 : #include "llvm/IR/GVMaterializer.h"
31 : #include "llvm/IR/Instruction.h"
32 : #include "llvm/IR/IntrinsicInst.h"
33 : #include "llvm/IR/LLVMContext.h"
34 : #include "llvm/IR/Metadata.h"
35 : #include "llvm/IR/Module.h"
36 : #include "llvm/Support/Casting.h"
37 : #include <algorithm>
38 : #include <cassert>
39 : #include <utility>
40 :
41 : using namespace llvm;
42 : using namespace llvm::dwarf;
43 :
44 434803 : DISubprogram *llvm::getDISubprogram(const MDNode *Scope) {
45 : if (auto *LocalScope = dyn_cast_or_null<DILocalScope>(Scope))
46 434803 : return LocalScope->getSubprogram();
47 : return nullptr;
48 : }
49 :
50 : //===----------------------------------------------------------------------===//
51 : // DebugInfoFinder implementations.
52 : //===----------------------------------------------------------------------===//
53 :
54 0 : void DebugInfoFinder::reset() {
55 : CUs.clear();
56 : SPs.clear();
57 : GVs.clear();
58 : TYs.clear();
59 : Scopes.clear();
60 0 : NodesSeen.clear();
61 0 : }
62 :
63 28 : void DebugInfoFinder::processModule(const Module &M) {
64 48 : for (auto *CU : M.debug_compile_units())
65 20 : processCompileUnit(CU);
66 82 : for (auto &F : M.functions()) {
67 54 : if (auto *SP = cast_or_null<DISubprogram>(F.getSubprogram()))
68 15 : processSubprogram(SP);
69 : // There could be subprograms from inlined functions referenced from
70 : // instructions only. Walk the function to find them.
71 94 : for (const BasicBlock &BB : F)
72 151 : for (const Instruction &I : BB)
73 111 : processInstruction(M, I);
74 : }
75 28 : }
76 :
77 105 : void DebugInfoFinder::processCompileUnit(DICompileUnit *CU) {
78 105 : if (!addCompileUnit(CU))
79 : return;
80 85 : for (auto DIG : CU->getGlobalVariables()) {
81 8 : if (!addGlobalVariable(DIG))
82 : continue;
83 : auto *GV = DIG->getVariable();
84 8 : processScope(GV->getScope());
85 8 : processType(GV->getType().resolve());
86 : }
87 77 : for (auto *ET : CU->getEnumTypes())
88 0 : processType(ET);
89 77 : for (auto *RT : CU->getRetainedTypes())
90 : if (auto *T = dyn_cast<DIType>(RT))
91 0 : processType(T);
92 : else
93 0 : processSubprogram(cast<DISubprogram>(RT));
94 78 : for (auto *Import : CU->getImportedEntities()) {
95 : auto *Entity = Import->getEntity().resolve();
96 : if (auto *T = dyn_cast<DIType>(Entity))
97 0 : processType(T);
98 : else if (auto *SP = dyn_cast<DISubprogram>(Entity))
99 0 : processSubprogram(SP);
100 : else if (auto *NS = dyn_cast<DINamespace>(Entity))
101 0 : processScope(NS->getScope());
102 : else if (auto *M = dyn_cast<DIModule>(Entity))
103 0 : processScope(M->getScope());
104 : }
105 : }
106 :
107 22264 : void DebugInfoFinder::processInstruction(const Module &M,
108 : const Instruction &I) {
109 : if (auto *DDI = dyn_cast<DbgDeclareInst>(&I))
110 24 : processDeclare(M, DDI);
111 : else if (auto *DVI = dyn_cast<DbgValueInst>(&I))
112 41 : processValue(M, DVI);
113 :
114 22264 : if (auto DbgLoc = I.getDebugLoc())
115 522 : processLocation(M, DbgLoc.get());
116 22264 : }
117 :
118 522 : void DebugInfoFinder::processLocation(const Module &M, const DILocation *Loc) {
119 1088 : if (!Loc)
120 : return;
121 566 : processScope(Loc->getScope());
122 : processLocation(M, Loc->getInlinedAt());
123 : }
124 :
125 233 : void DebugInfoFinder::processType(DIType *DT) {
126 242 : if (!addType(DT))
127 : return;
128 142 : processScope(DT->getScope().resolve());
129 : if (auto *ST = dyn_cast<DISubroutineType>(DT)) {
130 161 : for (DITypeRef Ref : ST->getTypeArray())
131 91 : processType(Ref.resolve());
132 : return;
133 : }
134 : if (auto *DCT = dyn_cast<DICompositeType>(DT)) {
135 10 : processType(DCT->getBaseType().resolve());
136 11 : for (Metadata *D : DCT->getElements()) {
137 : if (auto *T = dyn_cast<DIType>(D))
138 1 : processType(T);
139 : else if (auto *SP = dyn_cast<DISubprogram>(D))
140 0 : processSubprogram(SP);
141 : }
142 : return;
143 : }
144 : if (auto *DDT = dyn_cast<DIDerivedType>(DT)) {
145 : processType(DDT->getBaseType().resolve());
146 : }
147 : }
148 :
149 838 : void DebugInfoFinder::processScope(DIScope *Scope) {
150 883 : if (!Scope)
151 : return;
152 : if (auto *Ty = dyn_cast<DIType>(Scope)) {
153 1 : processType(Ty);
154 1 : return;
155 : }
156 : if (auto *CU = dyn_cast<DICompileUnit>(Scope)) {
157 1 : addCompileUnit(CU);
158 1 : return;
159 : }
160 : if (auto *SP = dyn_cast<DISubprogram>(Scope)) {
161 516 : processSubprogram(SP);
162 516 : return;
163 : }
164 170 : if (!addScope(Scope))
165 : return;
166 : if (auto *LB = dyn_cast<DILexicalBlockBase>(Scope)) {
167 : processScope(LB->getScope());
168 : } else if (auto *NS = dyn_cast<DINamespace>(Scope)) {
169 : processScope(NS->getScope());
170 : } else if (auto *M = dyn_cast<DIModule>(Scope)) {
171 : processScope(M->getScope());
172 : }
173 : }
174 :
175 531 : void DebugInfoFinder::processSubprogram(DISubprogram *SP) {
176 531 : if (!addSubprogram(SP))
177 : return;
178 85 : processScope(SP->getScope().resolve());
179 : // Some of the users, e.g. CloneFunctionInto / CloneModule, need to set up a
180 : // ValueMap containing identity mappings for all of the DICompileUnit's, not
181 : // just DISubprogram's, referenced from anywhere within the Function being
182 : // cloned prior to calling MapMetadata / RemapInstruction to avoid their
183 : // duplication later as DICompileUnit's are also directly referenced by
184 : // llvm.dbg.cu list. Thefore we need to collect DICompileUnit's here as well.
185 : // Also, DICompileUnit's may reference DISubprogram's too and therefore need
186 : // to be at least looked through.
187 85 : processCompileUnit(SP->getUnit());
188 85 : processType(SP->getType());
189 85 : for (auto *Element : SP->getTemplateParams()) {
190 : if (auto *TType = dyn_cast<DITemplateTypeParameter>(Element)) {
191 0 : processType(TType->getType().resolve());
192 : } else if (auto *TVal = dyn_cast<DITemplateValueParameter>(Element)) {
193 0 : processType(TVal->getType().resolve());
194 : }
195 : }
196 : }
197 :
198 24 : void DebugInfoFinder::processDeclare(const Module &M,
199 : const DbgDeclareInst *DDI) {
200 : auto *N = dyn_cast<MDNode>(DDI->getVariable());
201 24 : if (!N)
202 : return;
203 :
204 : auto *DV = dyn_cast<DILocalVariable>(N);
205 : if (!DV)
206 : return;
207 :
208 24 : if (!NodesSeen.insert(DV).second)
209 : return;
210 20 : processScope(DV->getScope());
211 20 : processType(DV->getType().resolve());
212 : }
213 :
214 41 : void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
215 : auto *N = dyn_cast<MDNode>(DVI->getVariable());
216 41 : if (!N)
217 : return;
218 :
219 : auto *DV = dyn_cast<DILocalVariable>(N);
220 : if (!DV)
221 : return;
222 :
223 41 : if (!NodesSeen.insert(DV).second)
224 : return;
225 17 : processScope(DV->getScope());
226 17 : processType(DV->getType().resolve());
227 : }
228 :
229 242 : bool DebugInfoFinder::addType(DIType *DT) {
230 242 : if (!DT)
231 : return false;
232 :
233 221 : if (!NodesSeen.insert(DT).second)
234 : return false;
235 :
236 142 : TYs.push_back(const_cast<DIType *>(DT));
237 142 : return true;
238 : }
239 :
240 106 : bool DebugInfoFinder::addCompileUnit(DICompileUnit *CU) {
241 106 : if (!CU)
242 : return false;
243 106 : if (!NodesSeen.insert(CU).second)
244 : return false;
245 :
246 77 : CUs.push_back(CU);
247 77 : return true;
248 : }
249 :
250 8 : bool DebugInfoFinder::addGlobalVariable(DIGlobalVariableExpression *DIG) {
251 8 : if (!NodesSeen.insert(DIG).second)
252 : return false;
253 :
254 8 : GVs.push_back(DIG);
255 8 : return true;
256 : }
257 :
258 531 : bool DebugInfoFinder::addSubprogram(DISubprogram *SP) {
259 531 : if (!SP)
260 : return false;
261 :
262 531 : if (!NodesSeen.insert(SP).second)
263 : return false;
264 :
265 85 : SPs.push_back(SP);
266 85 : return true;
267 : }
268 :
269 170 : bool DebugInfoFinder::addScope(DIScope *Scope) {
270 170 : if (!Scope)
271 : return false;
272 : // FIXME: Ocaml binding generates a scope with no content, we treat it
273 : // as null for now.
274 170 : if (Scope->getNumOperands() == 0)
275 : return false;
276 170 : if (!NodesSeen.insert(Scope).second)
277 : return false;
278 74 : Scopes.push_back(Scope);
279 74 : return true;
280 : }
281 :
282 1142 : static MDNode *stripDebugLocFromLoopID(MDNode *N) {
283 : assert(N->op_begin() != N->op_end() && "Missing self reference?");
284 :
285 : // if there is no debug location, we do not have to rewrite this MDNode.
286 2284 : if (std::none_of(N->op_begin() + 1, N->op_end(), [](const MDOperand &Op) {
287 0 : return isa<DILocation>(Op.get());
288 : }))
289 : return N;
290 :
291 : // If there is only the debug location without any actual loop metadata, we
292 : // can remove the metadata.
293 4 : if (std::none_of(N->op_begin() + 1, N->op_end(), [](const MDOperand &Op) {
294 0 : return !isa<DILocation>(Op.get());
295 : }))
296 : return nullptr;
297 :
298 : SmallVector<Metadata *, 4> Args;
299 : // Reserve operand 0 for loop id self reference.
300 : auto TempNode = MDNode::getTemporary(N->getContext(), None);
301 2 : Args.push_back(TempNode.get());
302 : // Add all non-debug location operands back.
303 7 : for (auto Op = N->op_begin() + 1; Op != N->op_end(); Op++) {
304 5 : if (!isa<DILocation>(*Op))
305 2 : Args.push_back(*Op);
306 : }
307 :
308 : // Set the first operand to itself.
309 : MDNode *LoopID = MDNode::get(N->getContext(), Args);
310 2 : LoopID->replaceOperandWith(0, LoopID);
311 : return LoopID;
312 : }
313 :
314 335040 : bool llvm::stripDebugInfo(Function &F) {
315 : bool Changed = false;
316 335040 : if (F.hasMetadata(LLVMContext::MD_dbg)) {
317 : Changed = true;
318 762 : F.setSubprogram(nullptr);
319 : }
320 :
321 : DenseMap<MDNode*, MDNode*> LoopIDsMap;
322 714071 : for (BasicBlock &BB : F) {
323 2129888 : for (auto II = BB.begin(), End = BB.end(); II != End;) {
324 : Instruction &I = *II++; // We may delete the instruction, increment now.
325 : if (isa<DbgInfoIntrinsic>(&I)) {
326 5207 : I.eraseFromParent();
327 : Changed = true;
328 5207 : continue;
329 : }
330 1745650 : if (I.getDebugLoc()) {
331 : Changed = true;
332 12922 : I.setDebugLoc(DebugLoc());
333 : }
334 : }
335 :
336 : auto *TermInst = BB.getTerminator();
337 379031 : if (!TermInst)
338 : // This is invalid IR, but we may not have run the verifier yet
339 : continue;
340 379031 : if (auto *LoopID = TermInst->getMetadata(LLVMContext::MD_loop)) {
341 1142 : auto *NewLoopID = LoopIDsMap.lookup(LoopID);
342 19 : if (!NewLoopID)
343 1142 : NewLoopID = LoopIDsMap[LoopID] = stripDebugLocFromLoopID(LoopID);
344 1161 : if (NewLoopID != LoopID)
345 5 : TermInst->setMetadata(LLVMContext::MD_loop, NewLoopID);
346 : }
347 : }
348 335040 : return Changed;
349 : }
350 :
351 36912 : bool llvm::StripDebugInfo(Module &M) {
352 : bool Changed = false;
353 :
354 : for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
355 41424 : NME = M.named_metadata_end(); NMI != NME;) {
356 : NamedMDNode *NMD = &*NMI;
357 : ++NMI;
358 :
359 : // We're stripping debug info, and without them, coverage information
360 : // doesn't quite make sense.
361 8319 : if (NMD->getName().startswith("llvm.dbg.") ||
362 3808 : NMD->getName() == "llvm.gcov") {
363 705 : NMD->eraseFromParent();
364 : Changed = true;
365 : }
366 : }
367 :
368 371427 : for (Function &F : M)
369 334515 : Changed |= stripDebugInfo(F);
370 :
371 78215 : for (auto &GV : M.globals()) {
372 41303 : Changed |= GV.eraseMetadata(LLVMContext::MD_dbg);
373 : }
374 :
375 36912 : if (GVMaterializer *Materializer = M.getMaterializer())
376 654 : Materializer->setStripDebugInfo();
377 :
378 36912 : return Changed;
379 : }
380 :
381 : namespace {
382 :
383 : /// Helper class to downgrade -g metadata to -gline-tables-only metadata.
384 : class DebugTypeInfoRemoval {
385 : DenseMap<Metadata *, Metadata *> Replacements;
386 :
387 : public:
388 : /// The (void)() type.
389 : MDNode *EmptySubroutineType;
390 :
391 : private:
392 : /// Remember what linkage name we originally had before stripping. If we end
393 : /// up making two subprograms identical who originally had different linkage
394 : /// names, then we need to make one of them distinct, to avoid them getting
395 : /// uniqued. Maps the new node to the old linkage name.
396 : DenseMap<DISubprogram *, StringRef> NewToLinkageName;
397 :
398 : // TODO: Remember the distinct subprogram we created for a given linkage name,
399 : // so that we can continue to unique whenever possible. Map <newly created
400 : // node, old linkage name> to the first (possibly distinct) mdsubprogram
401 : // created for that combination. This is not strictly needed for correctness,
402 : // but can cut down on the number of MDNodes and let us diff cleanly with the
403 : // output of -gline-tables-only.
404 :
405 : public:
406 13 : DebugTypeInfoRemoval(LLVMContext &C)
407 13 : : EmptySubroutineType(DISubroutineType::get(C, DINode::FlagZero, 0,
408 26 : MDNode::get(C, {}))) {}
409 :
410 183 : Metadata *map(Metadata *M) {
411 183 : if (!M)
412 : return nullptr;
413 171 : auto Replacement = Replacements.find(M);
414 171 : if (Replacement != Replacements.end())
415 114 : return Replacement->second;
416 :
417 : return M;
418 : }
419 5 : MDNode *mapNode(Metadata *N) { return dyn_cast_or_null<MDNode>(map(N)); }
420 :
421 : /// Recursively remap N and all its referenced children. Does a DF post-order
422 : /// traversal, so as to remap bottoms up.
423 5 : void traverseAndRemap(MDNode *N) { traverse(N); }
424 :
425 : private:
426 : // Create a new DISubprogram, to replace the one given.
427 8 : DISubprogram *getReplacementSubprogram(DISubprogram *MDS) {
428 16 : auto *FileAndScope = cast_or_null<DIFile>(map(MDS->getFile()));
429 8 : StringRef LinkageName = MDS->getName().empty() ? MDS->getLinkageName() : "";
430 8 : DISubprogram *Declaration = nullptr;
431 9 : auto *Type = cast_or_null<DISubroutineType>(map(MDS->getType()));
432 9 : DITypeRef ContainingType(map(MDS->getContainingType()));
433 10 : auto *Unit = cast_or_null<DICompileUnit>(map(MDS->getUnit()));
434 8 : auto Variables = nullptr;
435 8 : auto TemplateParams = nullptr;
436 :
437 : // Make a distinct DISubprogram, for situations that warrent it.
438 : auto distinctMDSubprogram = [&]() {
439 : return DISubprogram::getDistinct(
440 : MDS->getContext(), FileAndScope, MDS->getName(), LinkageName,
441 : FileAndScope, MDS->getLine(), Type, MDS->isLocalToUnit(),
442 : MDS->isDefinition(), MDS->getScopeLine(), ContainingType,
443 : MDS->getVirtuality(), MDS->getVirtualIndex(),
444 : MDS->getThisAdjustment(), MDS->getFlags(), MDS->isOptimized(), Unit,
445 : TemplateParams, Declaration, Variables);
446 8 : };
447 :
448 16 : if (MDS->isDistinct())
449 6 : return distinctMDSubprogram();
450 :
451 10 : auto *NewMDS = DISubprogram::get(
452 : MDS->getContext(), FileAndScope, MDS->getName(), LinkageName,
453 : FileAndScope, MDS->getLine(), Type, MDS->isLocalToUnit(),
454 : MDS->isDefinition(), MDS->getScopeLine(), ContainingType,
455 : MDS->getVirtuality(), MDS->getVirtualIndex(), MDS->getThisAdjustment(),
456 : MDS->getFlags(), MDS->isOptimized(), Unit, TemplateParams, Declaration,
457 : Variables);
458 :
459 2 : StringRef OldLinkageName = MDS->getLinkageName();
460 :
461 : // See if we need to make a distinct one.
462 2 : auto OrigLinkage = NewToLinkageName.find(NewMDS);
463 2 : if (OrigLinkage != NewToLinkageName.end()) {
464 : if (OrigLinkage->second == OldLinkageName)
465 : // We're good.
466 : return NewMDS;
467 :
468 : // Otherwise, need to make a distinct one.
469 : // TODO: Query the map to see if we already have one.
470 0 : return distinctMDSubprogram();
471 : }
472 :
473 2 : NewToLinkageName.insert({NewMDS, MDS->getLinkageName()});
474 2 : return NewMDS;
475 : }
476 :
477 : /// Create a new compile unit, to replace the one given
478 19 : DICompileUnit *getReplacementCU(DICompileUnit *CU) {
479 : // Drop skeleton CUs.
480 19 : if (CU->getDWOId())
481 : return nullptr;
482 :
483 18 : auto *File = cast_or_null<DIFile>(map(CU->getFile()));
484 : MDTuple *EnumTypes = nullptr;
485 : MDTuple *RetainedTypes = nullptr;
486 : MDTuple *GlobalVariables = nullptr;
487 : MDTuple *ImportedEntities = nullptr;
488 54 : return DICompileUnit::getDistinct(
489 : CU->getContext(), CU->getSourceLanguage(), File, CU->getProducer(),
490 18 : CU->isOptimized(), CU->getFlags(), CU->getRuntimeVersion(),
491 : CU->getSplitDebugFilename(), DICompileUnit::LineTablesOnly, EnumTypes,
492 : RetainedTypes, GlobalVariables, ImportedEntities, CU->getMacros(),
493 18 : CU->getDWOId(), CU->getSplitDebugInlining(),
494 18 : CU->getDebugInfoForProfiling(), CU->getNameTableKind());
495 : }
496 :
497 0 : DILocation *getReplacementMDLocation(DILocation *MLD) {
498 0 : auto *Scope = map(MLD->getScope());
499 0 : auto *InlinedAt = map(MLD->getInlinedAt());
500 0 : if (MLD->isDistinct())
501 0 : return DILocation::getDistinct(MLD->getContext(), MLD->getLine(),
502 0 : MLD->getColumn(), Scope, InlinedAt);
503 0 : return DILocation::get(MLD->getContext(), MLD->getLine(), MLD->getColumn(),
504 0 : Scope, InlinedAt);
505 : }
506 :
507 : /// Create a new generic MDNode, to replace the one given
508 31 : MDNode *getReplacementMDNode(MDNode *N) {
509 : SmallVector<Metadata *, 8> Ops;
510 31 : Ops.reserve(N->getNumOperands());
511 102 : for (auto &I : N->operands())
512 71 : if (I)
513 67 : Ops.push_back(map(I));
514 : auto *Ret = MDNode::get(N->getContext(), Ops);
515 31 : return Ret;
516 : }
517 :
518 : /// Attempt to re-map N to a newly created node.
519 102 : void remap(MDNode *N) {
520 102 : if (Replacements.count(N))
521 13 : return;
522 :
523 : auto doRemap = [&](MDNode *N) -> MDNode * {
524 : if (!N)
525 : return nullptr;
526 : if (auto *MDSub = dyn_cast<DISubprogram>(N)) {
527 : remap(MDSub->getUnit());
528 : return getReplacementSubprogram(MDSub);
529 : }
530 : if (isa<DISubroutineType>(N))
531 : return EmptySubroutineType;
532 : if (auto *CU = dyn_cast<DICompileUnit>(N))
533 : return getReplacementCU(CU);
534 : if (isa<DIFile>(N))
535 : return N;
536 : if (auto *MDLB = dyn_cast<DILexicalBlockBase>(N))
537 : // Remap to our referenced scope (recursively).
538 : return mapNode(MDLB->getScope());
539 : if (auto *MLD = dyn_cast<DILocation>(N))
540 : return getReplacementMDLocation(MLD);
541 :
542 : // Otherwise, if we see these, just drop them now. Not strictly necessary,
543 : // but this speeds things up a little.
544 : if (isa<DINode>(N))
545 : return nullptr;
546 :
547 : return getReplacementMDNode(N);
548 89 : };
549 89 : Replacements[N] = doRemap(N);
550 : }
551 :
552 : /// Do the remapping traversal.
553 : void traverse(MDNode *);
554 : };
555 :
556 : } // end anonymous namespace
557 :
558 66 : void DebugTypeInfoRemoval::traverse(MDNode *N) {
559 132 : if (!N || Replacements.count(N))
560 26 : return;
561 :
562 : // To avoid cycles, as well as for efficiency sake, we will sometimes prune
563 : // parts of the graph.
564 : auto prune = [](MDNode *Parent, MDNode *Child) {
565 : if (auto *MDS = dyn_cast<DISubprogram>(Parent))
566 29 : return Child == MDS->getRetainedNodes().get();
567 : return false;
568 : };
569 :
570 : SmallVector<MDNode *, 16> ToVisit;
571 : DenseSet<MDNode *> Opened;
572 :
573 : // Visit each node starting at N in post order, and map them.
574 40 : ToVisit.push_back(N);
575 216 : while (!ToVisit.empty()) {
576 176 : auto *N = ToVisit.back();
577 176 : if (!Opened.insert(N).second) {
578 : // Close it.
579 94 : remap(N);
580 : ToVisit.pop_back();
581 94 : continue;
582 : }
583 480 : for (auto &I : N->operands())
584 398 : if (auto *MDN = dyn_cast_or_null<MDNode>(I))
585 181 : if (!Opened.count(MDN) && !Replacements.count(MDN) && !prune(N, MDN) &&
586 : !isa<DICompileUnit>(MDN))
587 54 : ToVisit.push_back(MDN);
588 : }
589 : }
590 :
591 13 : bool llvm::stripNonLineTableDebugInfo(Module &M) {
592 13 : bool Changed = false;
593 :
594 : // First off, delete the debug intrinsics.
595 : auto RemoveUses = [&](StringRef Name) {
596 : if (auto *DbgVal = M.getFunction(Name)) {
597 : while (!DbgVal->use_empty())
598 : cast<Instruction>(DbgVal->user_back())->eraseFromParent();
599 : DbgVal->eraseFromParent();
600 : Changed = true;
601 : }
602 13 : };
603 13 : RemoveUses("llvm.dbg.declare");
604 13 : RemoveUses("llvm.dbg.value");
605 :
606 : // Delete non-CU debug info named metadata nodes.
607 : for (auto NMI = M.named_metadata_begin(), NME = M.named_metadata_end();
608 35 : NMI != NME;) {
609 : NamedMDNode *NMD = &*NMI;
610 : ++NMI;
611 : // Specifically keep dbg.cu around.
612 22 : if (NMD->getName() == "llvm.dbg.cu")
613 : continue;
614 : }
615 :
616 : // Drop all dbg attachments from global variables.
617 15 : for (auto &GV : M.globals())
618 2 : GV.eraseMetadata(LLVMContext::MD_dbg);
619 :
620 13 : DebugTypeInfoRemoval Mapper(M.getContext());
621 : auto remap = [&](MDNode *Node) -> MDNode * {
622 : if (!Node)
623 : return nullptr;
624 : Mapper.traverseAndRemap(Node);
625 : auto *NewNode = Mapper.mapNode(Node);
626 : Changed |= Node != NewNode;
627 : Node = NewNode;
628 : return NewNode;
629 13 : };
630 :
631 : // Rewrite the DebugLocs to be equivalent to what
632 : // -gline-tables-only would have created.
633 32 : for (auto &F : M) {
634 19 : if (auto *SP = F.getSubprogram()) {
635 : Mapper.traverseAndRemap(SP);
636 : auto *NewSP = cast<DISubprogram>(Mapper.mapNode(SP));
637 5 : Changed |= SP != NewSP;
638 5 : F.setSubprogram(NewSP);
639 : }
640 34 : for (auto &BB : F) {
641 45 : for (auto &I : BB) {
642 : auto remapDebugLoc = [&](DebugLoc DL) -> DebugLoc {
643 : auto *Scope = DL.getScope();
644 : MDNode *InlinedAt = DL.getInlinedAt();
645 : Scope = remap(Scope);
646 : InlinedAt = remap(InlinedAt);
647 : return DebugLoc::get(DL.getLine(), DL.getCol(), Scope, InlinedAt);
648 30 : };
649 :
650 30 : if (I.getDebugLoc() != DebugLoc())
651 39 : I.setDebugLoc(remapDebugLoc(I.getDebugLoc()));
652 :
653 : // Remap DILocations in untyped MDNodes (e.g., llvm.loop).
654 : SmallVector<std::pair<unsigned, MDNode *>, 2> MDs;
655 : I.getAllMetadata(MDs);
656 48 : for (auto Attachment : MDs)
657 : if (auto *T = dyn_cast_or_null<MDTuple>(Attachment.second))
658 16 : for (unsigned N = 0; N < T->getNumOperands(); ++N)
659 11 : if (auto *Loc = dyn_cast_or_null<DILocation>(T->getOperand(N)))
660 4 : if (Loc != DebugLoc())
661 8 : T->replaceOperandWith(N, remapDebugLoc(Loc));
662 : }
663 : }
664 : }
665 :
666 : // Create a new llvm.dbg.cu, which is equivalent to the one
667 : // -gline-tables-only would have created.
668 35 : for (auto &NMD : M.getNamedMDList()) {
669 : SmallVector<MDNode *, 8> Ops;
670 68 : for (MDNode *Op : NMD.operands())
671 46 : Ops.push_back(remap(Op));
672 :
673 22 : if (!Changed)
674 : continue;
675 :
676 21 : NMD.clearOperands();
677 66 : for (auto *Op : Ops)
678 45 : if (Op)
679 44 : NMD.addOperand(Op);
680 : }
681 13 : return Changed;
682 : }
683 :
684 38255 : unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
685 : if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
686 76510 : M.getModuleFlag("Debug Info Version")))
687 2088 : return Val->getZExtValue();
688 : return 0;
689 : }
690 :
691 58603 : void Instruction::applyMergedLocation(const DILocation *LocA,
692 : const DILocation *LocB) {
693 58603 : setDebugLoc(DILocation::getMergedLocation(LocA, LocB));
694 58603 : }
695 :
696 : //===----------------------------------------------------------------------===//
697 : // LLVM C API implementations.
698 : //===----------------------------------------------------------------------===//
699 :
700 : static unsigned map_from_llvmDWARFsourcelanguage(LLVMDWARFSourceLanguage lang) {
701 : switch (lang) {
702 : #define HANDLE_DW_LANG(ID, NAME, VERSION, VENDOR) \
703 : case LLVMDWARFSourceLanguage##NAME: return ID;
704 : #include "llvm/BinaryFormat/Dwarf.def"
705 : #undef HANDLE_DW_LANG
706 : }
707 0 : llvm_unreachable("Unhandled Tag");
708 : }
709 :
710 : template <typename DIT> DIT *unwrapDI(LLVMMetadataRef Ref) {
711 24 : return (DIT *)(Ref ? unwrap<MDNode>(Ref) : nullptr);
712 : }
713 :
714 : static DINode::DIFlags map_from_llvmDIFlags(LLVMDIFlags Flags) {
715 : return static_cast<DINode::DIFlags>(Flags);
716 : }
717 :
718 : static LLVMDIFlags map_to_llvmDIFlags(DINode::DIFlags Flags) {
719 : return static_cast<LLVMDIFlags>(Flags);
720 : }
721 :
722 0 : unsigned LLVMDebugMetadataVersion() {
723 0 : return DEBUG_METADATA_VERSION;
724 : }
725 :
726 0 : LLVMDIBuilderRef LLVMCreateDIBuilderDisallowUnresolved(LLVMModuleRef M) {
727 0 : return wrap(new DIBuilder(*unwrap(M), false));
728 : }
729 :
730 1 : LLVMDIBuilderRef LLVMCreateDIBuilder(LLVMModuleRef M) {
731 1 : return wrap(new DIBuilder(*unwrap(M)));
732 : }
733 :
734 0 : unsigned LLVMGetModuleDebugMetadataVersion(LLVMModuleRef M) {
735 0 : return getDebugMetadataVersionFromModule(*unwrap(M));
736 : }
737 :
738 0 : LLVMBool LLVMStripModuleDebugInfo(LLVMModuleRef M) {
739 0 : return StripDebugInfo(*unwrap(M));
740 : }
741 :
742 1 : void LLVMDisposeDIBuilder(LLVMDIBuilderRef Builder) {
743 1 : delete unwrap(Builder);
744 1 : }
745 :
746 1 : void LLVMDIBuilderFinalize(LLVMDIBuilderRef Builder) {
747 1 : unwrap(Builder)->finalize();
748 1 : }
749 :
750 1 : LLVMMetadataRef LLVMDIBuilderCreateCompileUnit(
751 : LLVMDIBuilderRef Builder, LLVMDWARFSourceLanguage Lang,
752 : LLVMMetadataRef FileRef, const char *Producer, size_t ProducerLen,
753 : LLVMBool isOptimized, const char *Flags, size_t FlagsLen,
754 : unsigned RuntimeVer, const char *SplitName, size_t SplitNameLen,
755 : LLVMDWARFEmissionKind Kind, unsigned DWOId, LLVMBool SplitDebugInlining,
756 : LLVMBool DebugInfoForProfiling) {
757 : auto File = unwrapDI<DIFile>(FileRef);
758 :
759 2 : return wrap(unwrap(Builder)->createCompileUnit(
760 : map_from_llvmDWARFsourcelanguage(Lang), File,
761 : StringRef(Producer, ProducerLen), isOptimized,
762 : StringRef(Flags, FlagsLen), RuntimeVer,
763 : StringRef(SplitName, SplitNameLen),
764 : static_cast<DICompileUnit::DebugEmissionKind>(Kind), DWOId,
765 1 : SplitDebugInlining, DebugInfoForProfiling));
766 : }
767 :
768 : LLVMMetadataRef
769 1 : LLVMDIBuilderCreateFile(LLVMDIBuilderRef Builder, const char *Filename,
770 : size_t FilenameLen, const char *Directory,
771 : size_t DirectoryLen) {
772 1 : return wrap(unwrap(Builder)->createFile(StringRef(Filename, FilenameLen),
773 1 : StringRef(Directory, DirectoryLen)));
774 : }
775 :
776 : LLVMMetadataRef
777 2 : LLVMDIBuilderCreateModule(LLVMDIBuilderRef Builder, LLVMMetadataRef ParentScope,
778 : const char *Name, size_t NameLen,
779 : const char *ConfigMacros, size_t ConfigMacrosLen,
780 : const char *IncludePath, size_t IncludePathLen,
781 : const char *ISysRoot, size_t ISysRootLen) {
782 2 : return wrap(unwrap(Builder)->createModule(
783 : unwrapDI<DIScope>(ParentScope), StringRef(Name, NameLen),
784 : StringRef(ConfigMacros, ConfigMacrosLen),
785 : StringRef(IncludePath, IncludePathLen),
786 2 : StringRef(ISysRoot, ISysRootLen)));
787 : }
788 :
789 1 : LLVMMetadataRef LLVMDIBuilderCreateNameSpace(LLVMDIBuilderRef Builder,
790 : LLVMMetadataRef ParentScope,
791 : const char *Name, size_t NameLen,
792 : LLVMBool ExportSymbols) {
793 1 : return wrap(unwrap(Builder)->createNameSpace(
794 1 : unwrapDI<DIScope>(ParentScope), StringRef(Name, NameLen), ExportSymbols));
795 : }
796 :
797 1 : LLVMMetadataRef LLVMDIBuilderCreateFunction(
798 : LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
799 : size_t NameLen, const char *LinkageName, size_t LinkageNameLen,
800 : LLVMMetadataRef File, unsigned LineNo, LLVMMetadataRef Ty,
801 : LLVMBool IsLocalToUnit, LLVMBool IsDefinition,
802 : unsigned ScopeLine, LLVMDIFlags Flags, LLVMBool IsOptimized) {
803 2 : return wrap(unwrap(Builder)->createFunction(
804 : unwrapDI<DIScope>(Scope), {Name, NameLen}, {LinkageName, LinkageNameLen},
805 : unwrapDI<DIFile>(File), LineNo, unwrapDI<DISubroutineType>(Ty),
806 : IsLocalToUnit, IsDefinition, ScopeLine, map_from_llvmDIFlags(Flags),
807 1 : IsOptimized, nullptr, nullptr, nullptr));
808 : }
809 :
810 :
811 1 : LLVMMetadataRef LLVMDIBuilderCreateLexicalBlock(
812 : LLVMDIBuilderRef Builder, LLVMMetadataRef Scope,
813 : LLVMMetadataRef File, unsigned Line, unsigned Col) {
814 1 : return wrap(unwrap(Builder)->createLexicalBlock(unwrapDI<DIScope>(Scope),
815 : unwrapDI<DIFile>(File),
816 1 : Line, Col));
817 : }
818 :
819 : LLVMMetadataRef
820 0 : LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef Builder,
821 : LLVMMetadataRef Scope,
822 : LLVMMetadataRef File,
823 : unsigned Discriminator) {
824 0 : return wrap(unwrap(Builder)->createLexicalBlockFile(unwrapDI<DIScope>(Scope),
825 : unwrapDI<DIFile>(File),
826 0 : Discriminator));
827 : }
828 :
829 : LLVMMetadataRef
830 0 : LLVMDIBuilderCreateImportedModuleFromNamespace(LLVMDIBuilderRef Builder,
831 : LLVMMetadataRef Scope,
832 : LLVMMetadataRef NS,
833 : LLVMMetadataRef File,
834 : unsigned Line) {
835 0 : return wrap(unwrap(Builder)->createImportedModule(unwrapDI<DIScope>(Scope),
836 : unwrapDI<DINamespace>(NS),
837 : unwrapDI<DIFile>(File),
838 0 : Line));
839 : }
840 :
841 : LLVMMetadataRef
842 1 : LLVMDIBuilderCreateImportedModuleFromAlias(LLVMDIBuilderRef Builder,
843 : LLVMMetadataRef Scope,
844 : LLVMMetadataRef ImportedEntity,
845 : LLVMMetadataRef File,
846 : unsigned Line) {
847 1 : return wrap(unwrap(Builder)->createImportedModule(
848 : unwrapDI<DIScope>(Scope),
849 : unwrapDI<DIImportedEntity>(ImportedEntity),
850 1 : unwrapDI<DIFile>(File), Line));
851 : }
852 :
853 : LLVMMetadataRef
854 1 : LLVMDIBuilderCreateImportedModuleFromModule(LLVMDIBuilderRef Builder,
855 : LLVMMetadataRef Scope,
856 : LLVMMetadataRef M,
857 : LLVMMetadataRef File,
858 : unsigned Line) {
859 1 : return wrap(unwrap(Builder)->createImportedModule(unwrapDI<DIScope>(Scope),
860 : unwrapDI<DIModule>(M),
861 : unwrapDI<DIFile>(File),
862 1 : Line));
863 : }
864 :
865 : LLVMMetadataRef
866 0 : LLVMDIBuilderCreateImportedDeclaration(LLVMDIBuilderRef Builder,
867 : LLVMMetadataRef Scope,
868 : LLVMMetadataRef Decl,
869 : LLVMMetadataRef File,
870 : unsigned Line,
871 : const char *Name, size_t NameLen) {
872 0 : return wrap(unwrap(Builder)->createImportedDeclaration(
873 : unwrapDI<DIScope>(Scope),
874 : unwrapDI<DINode>(Decl),
875 0 : unwrapDI<DIFile>(File), Line, {Name, NameLen}));
876 : }
877 :
878 : LLVMMetadataRef
879 2 : LLVMDIBuilderCreateDebugLocation(LLVMContextRef Ctx, unsigned Line,
880 : unsigned Column, LLVMMetadataRef Scope,
881 : LLVMMetadataRef InlinedAt) {
882 : return wrap(DILocation::get(*unwrap(Ctx), Line, Column, unwrap(Scope),
883 2 : unwrap(InlinedAt)));
884 : }
885 :
886 0 : unsigned LLVMDILocationGetLine(LLVMMetadataRef Location) {
887 0 : return unwrapDI<DILocation>(Location)->getLine();
888 : }
889 :
890 0 : unsigned LLVMDILocationGetColumn(LLVMMetadataRef Location) {
891 0 : return unwrapDI<DILocation>(Location)->getColumn();
892 : }
893 :
894 0 : LLVMMetadataRef LLVMDILocationGetScope(LLVMMetadataRef Location) {
895 0 : return wrap(unwrapDI<DILocation>(Location)->getScope());
896 : }
897 :
898 0 : LLVMMetadataRef LLVMDIBuilderCreateEnumerationType(
899 : LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
900 : size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
901 : uint64_t SizeInBits, uint32_t AlignInBits, LLVMMetadataRef *Elements,
902 : unsigned NumElements, LLVMMetadataRef ClassTy) {
903 : auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements),
904 0 : NumElements});
905 0 : return wrap(unwrap(Builder)->createEnumerationType(
906 : unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File),
907 0 : LineNumber, SizeInBits, AlignInBits, Elts, unwrapDI<DIType>(ClassTy)));
908 : }
909 :
910 0 : LLVMMetadataRef LLVMDIBuilderCreateUnionType(
911 : LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
912 : size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
913 : uint64_t SizeInBits, uint32_t AlignInBits, LLVMDIFlags Flags,
914 : LLVMMetadataRef *Elements, unsigned NumElements, unsigned RunTimeLang,
915 : const char *UniqueId, size_t UniqueIdLen) {
916 : auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements),
917 0 : NumElements});
918 0 : return wrap(unwrap(Builder)->createUnionType(
919 : unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File),
920 : LineNumber, SizeInBits, AlignInBits, map_from_llvmDIFlags(Flags),
921 0 : Elts, RunTimeLang, {UniqueId, UniqueIdLen}));
922 : }
923 :
924 :
925 : LLVMMetadataRef
926 0 : LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Builder, uint64_t Size,
927 : uint32_t AlignInBits, LLVMMetadataRef Ty,
928 : LLVMMetadataRef *Subscripts,
929 : unsigned NumSubscripts) {
930 : auto Subs = unwrap(Builder)->getOrCreateArray({unwrap(Subscripts),
931 0 : NumSubscripts});
932 0 : return wrap(unwrap(Builder)->createArrayType(Size, AlignInBits,
933 0 : unwrapDI<DIType>(Ty), Subs));
934 : }
935 :
936 : LLVMMetadataRef
937 1 : LLVMDIBuilderCreateVectorType(LLVMDIBuilderRef Builder, uint64_t Size,
938 : uint32_t AlignInBits, LLVMMetadataRef Ty,
939 : LLVMMetadataRef *Subscripts,
940 : unsigned NumSubscripts) {
941 : auto Subs = unwrap(Builder)->getOrCreateArray({unwrap(Subscripts),
942 2 : NumSubscripts});
943 1 : return wrap(unwrap(Builder)->createVectorType(Size, AlignInBits,
944 1 : unwrapDI<DIType>(Ty), Subs));
945 : }
946 :
947 : LLVMMetadataRef
948 1 : LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef Builder, const char *Name,
949 : size_t NameLen, uint64_t SizeInBits,
950 : LLVMDWARFTypeEncoding Encoding,
951 : LLVMDIFlags Flags) {
952 1 : return wrap(unwrap(Builder)->createBasicType({Name, NameLen},
953 : SizeInBits, Encoding,
954 1 : map_from_llvmDIFlags(Flags)));
955 : }
956 :
957 1 : LLVMMetadataRef LLVMDIBuilderCreatePointerType(
958 : LLVMDIBuilderRef Builder, LLVMMetadataRef PointeeTy,
959 : uint64_t SizeInBits, uint32_t AlignInBits, unsigned AddressSpace,
960 : const char *Name, size_t NameLen) {
961 1 : return wrap(unwrap(Builder)->createPointerType(unwrapDI<DIType>(PointeeTy),
962 : SizeInBits, AlignInBits,
963 1 : AddressSpace, {Name, NameLen}));
964 : }
965 :
966 3 : LLVMMetadataRef LLVMDIBuilderCreateStructType(
967 : LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
968 : size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
969 : uint64_t SizeInBits, uint32_t AlignInBits, LLVMDIFlags Flags,
970 : LLVMMetadataRef DerivedFrom, LLVMMetadataRef *Elements,
971 : unsigned NumElements, unsigned RunTimeLang, LLVMMetadataRef VTableHolder,
972 : const char *UniqueId, size_t UniqueIdLen) {
973 : auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements),
974 6 : NumElements});
975 3 : return wrap(unwrap(Builder)->createStructType(
976 : unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File),
977 : LineNumber, SizeInBits, AlignInBits, map_from_llvmDIFlags(Flags),
978 : unwrapDI<DIType>(DerivedFrom), Elts, RunTimeLang,
979 3 : unwrapDI<DIType>(VTableHolder), {UniqueId, UniqueIdLen}));
980 : }
981 :
982 0 : LLVMMetadataRef LLVMDIBuilderCreateMemberType(
983 : LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
984 : size_t NameLen, LLVMMetadataRef File, unsigned LineNo, uint64_t SizeInBits,
985 : uint32_t AlignInBits, uint64_t OffsetInBits, LLVMDIFlags Flags,
986 : LLVMMetadataRef Ty) {
987 0 : return wrap(unwrap(Builder)->createMemberType(unwrapDI<DIScope>(Scope),
988 : {Name, NameLen}, unwrapDI<DIFile>(File), LineNo, SizeInBits, AlignInBits,
989 0 : OffsetInBits, map_from_llvmDIFlags(Flags), unwrapDI<DIType>(Ty)));
990 : }
991 :
992 : LLVMMetadataRef
993 0 : LLVMDIBuilderCreateUnspecifiedType(LLVMDIBuilderRef Builder, const char *Name,
994 : size_t NameLen) {
995 0 : return wrap(unwrap(Builder)->createUnspecifiedType({Name, NameLen}));
996 : }
997 :
998 : LLVMMetadataRef
999 0 : LLVMDIBuilderCreateStaticMemberType(
1000 : LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
1001 : size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
1002 : LLVMMetadataRef Type, LLVMDIFlags Flags, LLVMValueRef ConstantVal,
1003 : uint32_t AlignInBits) {
1004 0 : return wrap(unwrap(Builder)->createStaticMemberType(
1005 : unwrapDI<DIScope>(Scope), {Name, NameLen},
1006 : unwrapDI<DIFile>(File), LineNumber, unwrapDI<DIType>(Type),
1007 : map_from_llvmDIFlags(Flags), unwrap<Constant>(ConstantVal),
1008 0 : AlignInBits));
1009 : }
1010 :
1011 : LLVMMetadataRef
1012 1 : LLVMDIBuilderCreateObjCIVar(LLVMDIBuilderRef Builder,
1013 : const char *Name, size_t NameLen,
1014 : LLVMMetadataRef File, unsigned LineNo,
1015 : uint64_t SizeInBits, uint32_t AlignInBits,
1016 : uint64_t OffsetInBits, LLVMDIFlags Flags,
1017 : LLVMMetadataRef Ty, LLVMMetadataRef PropertyNode) {
1018 1 : return wrap(unwrap(Builder)->createObjCIVar(
1019 : {Name, NameLen}, unwrapDI<DIFile>(File), LineNo,
1020 : SizeInBits, AlignInBits, OffsetInBits,
1021 : map_from_llvmDIFlags(Flags), unwrapDI<DIType>(Ty),
1022 1 : unwrapDI<MDNode>(PropertyNode)));
1023 : }
1024 :
1025 : LLVMMetadataRef
1026 1 : LLVMDIBuilderCreateObjCProperty(LLVMDIBuilderRef Builder,
1027 : const char *Name, size_t NameLen,
1028 : LLVMMetadataRef File, unsigned LineNo,
1029 : const char *GetterName, size_t GetterNameLen,
1030 : const char *SetterName, size_t SetterNameLen,
1031 : unsigned PropertyAttributes,
1032 : LLVMMetadataRef Ty) {
1033 1 : return wrap(unwrap(Builder)->createObjCProperty(
1034 : {Name, NameLen}, unwrapDI<DIFile>(File), LineNo,
1035 : {GetterName, GetterNameLen}, {SetterName, SetterNameLen},
1036 1 : PropertyAttributes, unwrapDI<DIType>(Ty)));
1037 : }
1038 :
1039 : LLVMMetadataRef
1040 0 : LLVMDIBuilderCreateObjectPointerType(LLVMDIBuilderRef Builder,
1041 : LLVMMetadataRef Type) {
1042 0 : return wrap(unwrap(Builder)->createObjectPointerType(unwrapDI<DIType>(Type)));
1043 : }
1044 :
1045 : LLVMMetadataRef
1046 1 : LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef Builder, LLVMMetadataRef Type,
1047 : const char *Name, size_t NameLen,
1048 : LLVMMetadataRef File, unsigned LineNo,
1049 : LLVMMetadataRef Scope) {
1050 1 : return wrap(unwrap(Builder)->createTypedef(
1051 : unwrapDI<DIType>(Type), {Name, NameLen},
1052 : unwrapDI<DIFile>(File), LineNo,
1053 1 : unwrapDI<DIScope>(Scope)));
1054 : }
1055 :
1056 : LLVMMetadataRef
1057 1 : LLVMDIBuilderCreateInheritance(LLVMDIBuilderRef Builder,
1058 : LLVMMetadataRef Ty, LLVMMetadataRef BaseTy,
1059 : uint64_t BaseOffset, uint32_t VBPtrOffset,
1060 : LLVMDIFlags Flags) {
1061 1 : return wrap(unwrap(Builder)->createInheritance(
1062 : unwrapDI<DIType>(Ty), unwrapDI<DIType>(BaseTy),
1063 1 : BaseOffset, VBPtrOffset, map_from_llvmDIFlags(Flags)));
1064 : }
1065 :
1066 : LLVMMetadataRef
1067 0 : LLVMDIBuilderCreateForwardDecl(
1068 : LLVMDIBuilderRef Builder, unsigned Tag, const char *Name,
1069 : size_t NameLen, LLVMMetadataRef Scope, LLVMMetadataRef File, unsigned Line,
1070 : unsigned RuntimeLang, uint64_t SizeInBits, uint32_t AlignInBits,
1071 : const char *UniqueIdentifier, size_t UniqueIdentifierLen) {
1072 0 : return wrap(unwrap(Builder)->createForwardDecl(
1073 : Tag, {Name, NameLen}, unwrapDI<DIScope>(Scope),
1074 : unwrapDI<DIFile>(File), Line, RuntimeLang, SizeInBits,
1075 0 : AlignInBits, {UniqueIdentifier, UniqueIdentifierLen}));
1076 : }
1077 :
1078 : LLVMMetadataRef
1079 1 : LLVMDIBuilderCreateReplaceableCompositeType(
1080 : LLVMDIBuilderRef Builder, unsigned Tag, const char *Name,
1081 : size_t NameLen, LLVMMetadataRef Scope, LLVMMetadataRef File, unsigned Line,
1082 : unsigned RuntimeLang, uint64_t SizeInBits, uint32_t AlignInBits,
1083 : LLVMDIFlags Flags, const char *UniqueIdentifier,
1084 : size_t UniqueIdentifierLen) {
1085 1 : return wrap(unwrap(Builder)->createReplaceableCompositeType(
1086 : Tag, {Name, NameLen}, unwrapDI<DIScope>(Scope),
1087 : unwrapDI<DIFile>(File), Line, RuntimeLang, SizeInBits,
1088 : AlignInBits, map_from_llvmDIFlags(Flags),
1089 1 : {UniqueIdentifier, UniqueIdentifierLen}));
1090 : }
1091 :
1092 : LLVMMetadataRef
1093 0 : LLVMDIBuilderCreateQualifiedType(LLVMDIBuilderRef Builder, unsigned Tag,
1094 : LLVMMetadataRef Type) {
1095 0 : return wrap(unwrap(Builder)->createQualifiedType(Tag,
1096 0 : unwrapDI<DIType>(Type)));
1097 : }
1098 :
1099 : LLVMMetadataRef
1100 0 : LLVMDIBuilderCreateReferenceType(LLVMDIBuilderRef Builder, unsigned Tag,
1101 : LLVMMetadataRef Type) {
1102 0 : return wrap(unwrap(Builder)->createReferenceType(Tag,
1103 0 : unwrapDI<DIType>(Type)));
1104 : }
1105 :
1106 : LLVMMetadataRef
1107 0 : LLVMDIBuilderCreateNullPtrType(LLVMDIBuilderRef Builder) {
1108 0 : return wrap(unwrap(Builder)->createNullPtrType());
1109 : }
1110 :
1111 : LLVMMetadataRef
1112 0 : LLVMDIBuilderCreateMemberPointerType(LLVMDIBuilderRef Builder,
1113 : LLVMMetadataRef PointeeType,
1114 : LLVMMetadataRef ClassType,
1115 : uint64_t SizeInBits,
1116 : uint32_t AlignInBits,
1117 : LLVMDIFlags Flags) {
1118 0 : return wrap(unwrap(Builder)->createMemberPointerType(
1119 : unwrapDI<DIType>(PointeeType),
1120 : unwrapDI<DIType>(ClassType), AlignInBits, SizeInBits,
1121 0 : map_from_llvmDIFlags(Flags)));
1122 : }
1123 :
1124 : LLVMMetadataRef
1125 0 : LLVMDIBuilderCreateBitFieldMemberType(LLVMDIBuilderRef Builder,
1126 : LLVMMetadataRef Scope,
1127 : const char *Name, size_t NameLen,
1128 : LLVMMetadataRef File, unsigned LineNumber,
1129 : uint64_t SizeInBits,
1130 : uint64_t OffsetInBits,
1131 : uint64_t StorageOffsetInBits,
1132 : LLVMDIFlags Flags, LLVMMetadataRef Type) {
1133 0 : return wrap(unwrap(Builder)->createBitFieldMemberType(
1134 : unwrapDI<DIScope>(Scope), {Name, NameLen},
1135 : unwrapDI<DIFile>(File), LineNumber,
1136 : SizeInBits, OffsetInBits, StorageOffsetInBits,
1137 0 : map_from_llvmDIFlags(Flags), unwrapDI<DIType>(Type)));
1138 : }
1139 :
1140 0 : LLVMMetadataRef LLVMDIBuilderCreateClassType(LLVMDIBuilderRef Builder,
1141 : LLVMMetadataRef Scope, const char *Name, size_t NameLen,
1142 : LLVMMetadataRef File, unsigned LineNumber, uint64_t SizeInBits,
1143 : uint32_t AlignInBits, uint64_t OffsetInBits, LLVMDIFlags Flags,
1144 : LLVMMetadataRef DerivedFrom,
1145 : LLVMMetadataRef *Elements, unsigned NumElements,
1146 : LLVMMetadataRef VTableHolder, LLVMMetadataRef TemplateParamsNode,
1147 : const char *UniqueIdentifier, size_t UniqueIdentifierLen) {
1148 : auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements),
1149 0 : NumElements});
1150 0 : return wrap(unwrap(Builder)->createClassType(
1151 : unwrapDI<DIScope>(Scope), {Name, NameLen},
1152 : unwrapDI<DIFile>(File), LineNumber,
1153 : SizeInBits, AlignInBits, OffsetInBits,
1154 : map_from_llvmDIFlags(Flags), unwrapDI<DIType>(DerivedFrom),
1155 : Elts, unwrapDI<DIType>(VTableHolder),
1156 : unwrapDI<MDNode>(TemplateParamsNode),
1157 0 : {UniqueIdentifier, UniqueIdentifierLen}));
1158 : }
1159 :
1160 : LLVMMetadataRef
1161 0 : LLVMDIBuilderCreateArtificialType(LLVMDIBuilderRef Builder,
1162 : LLVMMetadataRef Type) {
1163 0 : return wrap(unwrap(Builder)->createArtificialType(unwrapDI<DIType>(Type)));
1164 : }
1165 :
1166 0 : const char *LLVMDITypeGetName(LLVMMetadataRef DType, size_t *Length) {
1167 : StringRef Str = unwrap<DIType>(DType)->getName();
1168 0 : *Length = Str.size();
1169 0 : return Str.data();
1170 : }
1171 :
1172 0 : uint64_t LLVMDITypeGetSizeInBits(LLVMMetadataRef DType) {
1173 0 : return unwrapDI<DIType>(DType)->getSizeInBits();
1174 : }
1175 :
1176 0 : uint64_t LLVMDITypeGetOffsetInBits(LLVMMetadataRef DType) {
1177 0 : return unwrapDI<DIType>(DType)->getOffsetInBits();
1178 : }
1179 :
1180 0 : uint32_t LLVMDITypeGetAlignInBits(LLVMMetadataRef DType) {
1181 0 : return unwrapDI<DIType>(DType)->getAlignInBits();
1182 : }
1183 :
1184 0 : unsigned LLVMDITypeGetLine(LLVMMetadataRef DType) {
1185 0 : return unwrapDI<DIType>(DType)->getLine();
1186 : }
1187 :
1188 0 : LLVMDIFlags LLVMDITypeGetFlags(LLVMMetadataRef DType) {
1189 0 : return map_to_llvmDIFlags(unwrapDI<DIType>(DType)->getFlags());
1190 : }
1191 :
1192 0 : LLVMMetadataRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef Builder,
1193 : LLVMMetadataRef *Types,
1194 : size_t Length) {
1195 : return wrap(
1196 0 : unwrap(Builder)->getOrCreateTypeArray({unwrap(Types), Length}).get());
1197 : }
1198 :
1199 : LLVMMetadataRef
1200 1 : LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Builder,
1201 : LLVMMetadataRef File,
1202 : LLVMMetadataRef *ParameterTypes,
1203 : unsigned NumParameterTypes,
1204 : LLVMDIFlags Flags) {
1205 : auto Elts = unwrap(Builder)->getOrCreateTypeArray({unwrap(ParameterTypes),
1206 2 : NumParameterTypes});
1207 1 : return wrap(unwrap(Builder)->createSubroutineType(
1208 1 : Elts, map_from_llvmDIFlags(Flags)));
1209 : }
1210 :
1211 1 : LLVMMetadataRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Builder,
1212 : int64_t *Addr, size_t Length) {
1213 1 : return wrap(unwrap(Builder)->createExpression(ArrayRef<int64_t>(Addr,
1214 1 : Length)));
1215 : }
1216 :
1217 : LLVMMetadataRef
1218 3 : LLVMDIBuilderCreateConstantValueExpression(LLVMDIBuilderRef Builder,
1219 : int64_t Value) {
1220 3 : return wrap(unwrap(Builder)->createConstantValueExpression(Value));
1221 : }
1222 :
1223 2 : LLVMMetadataRef LLVMDIBuilderCreateGlobalVariableExpression(
1224 : LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
1225 : size_t NameLen, const char *Linkage, size_t LinkLen, LLVMMetadataRef File,
1226 : unsigned LineNo, LLVMMetadataRef Ty, LLVMBool LocalToUnit,
1227 : LLVMMetadataRef Expr, LLVMMetadataRef Decl, uint32_t AlignInBits) {
1228 4 : return wrap(unwrap(Builder)->createGlobalVariableExpression(
1229 : unwrapDI<DIScope>(Scope), {Name, NameLen}, {Linkage, LinkLen},
1230 : unwrapDI<DIFile>(File), LineNo, unwrapDI<DIType>(Ty), LocalToUnit,
1231 : unwrap<DIExpression>(Expr), unwrapDI<MDNode>(Decl),
1232 2 : nullptr, AlignInBits));
1233 : }
1234 :
1235 0 : LLVMMetadataRef LLVMTemporaryMDNode(LLVMContextRef Ctx, LLVMMetadataRef *Data,
1236 : size_t Count) {
1237 : return wrap(
1238 0 : MDTuple::getTemporary(*unwrap(Ctx), {unwrap(Data), Count}).release());
1239 : }
1240 :
1241 0 : void LLVMDisposeTemporaryMDNode(LLVMMetadataRef TempNode) {
1242 0 : MDNode::deleteTemporary(unwrapDI<MDNode>(TempNode));
1243 0 : }
1244 :
1245 1 : void LLVMMetadataReplaceAllUsesWith(LLVMMetadataRef TargetMetadata,
1246 : LLVMMetadataRef Replacement) {
1247 : auto *Node = unwrapDI<MDNode>(TargetMetadata);
1248 : Node->replaceAllUsesWith(unwrap<Metadata>(Replacement));
1249 1 : MDNode::deleteTemporary(Node);
1250 1 : }
1251 :
1252 0 : LLVMMetadataRef LLVMDIBuilderCreateTempGlobalVariableFwdDecl(
1253 : LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
1254 : size_t NameLen, const char *Linkage, size_t LnkLen, LLVMMetadataRef File,
1255 : unsigned LineNo, LLVMMetadataRef Ty, LLVMBool LocalToUnit,
1256 : LLVMMetadataRef Decl, uint32_t AlignInBits) {
1257 0 : return wrap(unwrap(Builder)->createTempGlobalVariableFwdDecl(
1258 : unwrapDI<DIScope>(Scope), {Name, NameLen}, {Linkage, LnkLen},
1259 : unwrapDI<DIFile>(File), LineNo, unwrapDI<DIType>(Ty), LocalToUnit,
1260 0 : unwrapDI<MDNode>(Decl), nullptr, AlignInBits));
1261 : }
1262 :
1263 : LLVMValueRef
1264 0 : LLVMDIBuilderInsertDeclareBefore(LLVMDIBuilderRef Builder, LLVMValueRef Storage,
1265 : LLVMMetadataRef VarInfo, LLVMMetadataRef Expr,
1266 : LLVMMetadataRef DL, LLVMValueRef Instr) {
1267 0 : return wrap(unwrap(Builder)->insertDeclare(
1268 : unwrap(Storage), unwrap<DILocalVariable>(VarInfo),
1269 : unwrap<DIExpression>(Expr), unwrap<DILocation>(DL),
1270 0 : unwrap<Instruction>(Instr)));
1271 : }
1272 :
1273 3 : LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd(
1274 : LLVMDIBuilderRef Builder, LLVMValueRef Storage, LLVMMetadataRef VarInfo,
1275 : LLVMMetadataRef Expr, LLVMMetadataRef DL, LLVMBasicBlockRef Block) {
1276 3 : return wrap(unwrap(Builder)->insertDeclare(
1277 : unwrap(Storage), unwrap<DILocalVariable>(VarInfo),
1278 : unwrap<DIExpression>(Expr), unwrap<DILocation>(DL),
1279 3 : unwrap(Block)));
1280 : }
1281 :
1282 0 : LLVMValueRef LLVMDIBuilderInsertDbgValueBefore(LLVMDIBuilderRef Builder,
1283 : LLVMValueRef Val,
1284 : LLVMMetadataRef VarInfo,
1285 : LLVMMetadataRef Expr,
1286 : LLVMMetadataRef DebugLoc,
1287 : LLVMValueRef Instr) {
1288 0 : return wrap(unwrap(Builder)->insertDbgValueIntrinsic(
1289 : unwrap(Val), unwrap<DILocalVariable>(VarInfo),
1290 : unwrap<DIExpression>(Expr), unwrap<DILocation>(DebugLoc),
1291 0 : unwrap<Instruction>(Instr)));
1292 : }
1293 :
1294 1 : LLVMValueRef LLVMDIBuilderInsertDbgValueAtEnd(LLVMDIBuilderRef Builder,
1295 : LLVMValueRef Val,
1296 : LLVMMetadataRef VarInfo,
1297 : LLVMMetadataRef Expr,
1298 : LLVMMetadataRef DebugLoc,
1299 : LLVMBasicBlockRef Block) {
1300 1 : return wrap(unwrap(Builder)->insertDbgValueIntrinsic(
1301 : unwrap(Val), unwrap<DILocalVariable>(VarInfo),
1302 : unwrap<DIExpression>(Expr), unwrap<DILocation>(DebugLoc),
1303 1 : unwrap(Block)));
1304 : }
1305 :
1306 1 : LLVMMetadataRef LLVMDIBuilderCreateAutoVariable(
1307 : LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
1308 : size_t NameLen, LLVMMetadataRef File, unsigned LineNo, LLVMMetadataRef Ty,
1309 : LLVMBool AlwaysPreserve, LLVMDIFlags Flags, uint32_t AlignInBits) {
1310 2 : return wrap(unwrap(Builder)->createAutoVariable(
1311 : unwrap<DIScope>(Scope), {Name, NameLen}, unwrap<DIFile>(File),
1312 : LineNo, unwrap<DIType>(Ty), AlwaysPreserve,
1313 1 : map_from_llvmDIFlags(Flags), AlignInBits));
1314 : }
1315 :
1316 3 : LLVMMetadataRef LLVMDIBuilderCreateParameterVariable(
1317 : LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
1318 : size_t NameLen, unsigned ArgNo, LLVMMetadataRef File, unsigned LineNo,
1319 : LLVMMetadataRef Ty, LLVMBool AlwaysPreserve, LLVMDIFlags Flags) {
1320 6 : return wrap(unwrap(Builder)->createParameterVariable(
1321 : unwrap<DIScope>(Scope), {Name, NameLen}, ArgNo, unwrap<DIFile>(File),
1322 : LineNo, unwrap<DIType>(Ty), AlwaysPreserve,
1323 3 : map_from_llvmDIFlags(Flags)));
1324 : }
1325 :
1326 1 : LLVMMetadataRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Builder,
1327 : int64_t Lo, int64_t Count) {
1328 1 : return wrap(unwrap(Builder)->getOrCreateSubrange(Lo, Count));
1329 : }
1330 :
1331 0 : LLVMMetadataRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef Builder,
1332 : LLVMMetadataRef *Data,
1333 : size_t Length) {
1334 : Metadata **DataValue = unwrap(Data);
1335 0 : return wrap(unwrap(Builder)->getOrCreateArray({DataValue, Length}).get());
1336 : }
1337 :
1338 0 : LLVMMetadataRef LLVMGetSubprogram(LLVMValueRef Func) {
1339 0 : return wrap(unwrap<Function>(Func)->getSubprogram());
1340 : }
1341 :
1342 1 : void LLVMSetSubprogram(LLVMValueRef Func, LLVMMetadataRef SP) {
1343 1 : unwrap<Function>(Func)->setSubprogram(unwrap<DISubprogram>(SP));
1344 1 : }
1345 :
1346 0 : LLVMMetadataKind LLVMGetMetadataKind(LLVMMetadataRef Metadata) {
1347 0 : switch(unwrap(Metadata)->getMetadataID()) {
1348 : #define HANDLE_METADATA_LEAF(CLASS) \
1349 : case Metadata::CLASS##Kind: \
1350 : return (LLVMMetadataKind)LLVM##CLASS##MetadataKind;
1351 : #include "llvm/IR/Metadata.def"
1352 : default:
1353 : return (LLVMMetadataKind)LLVMGenericDINodeMetadataKind;
1354 : }
1355 : }
|