[InstCombine] Signed saturation patterns
[llvm-complete.git] / lib / IR / DebugInfo.cpp
blob1bbe6b85d260084de815ed62c4c458fc9ce3df15
1 //===- DebugInfo.cpp - Debug Information Helper Classes -------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the helper classes used to build and interpret debug
10 // information in LLVM IR form.
12 //===----------------------------------------------------------------------===//
14 #include "llvm-c/DebugInfo.h"
15 #include "llvm/ADT/DenseMap.h"
16 #include "llvm/ADT/DenseSet.h"
17 #include "llvm/ADT/None.h"
18 #include "llvm/ADT/STLExtras.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/IR/BasicBlock.h"
23 #include "llvm/IR/Constants.h"
24 #include "llvm/IR/DebugInfoMetadata.h"
25 #include "llvm/IR/DebugLoc.h"
26 #include "llvm/IR/DebugInfo.h"
27 #include "llvm/IR/DIBuilder.h"
28 #include "llvm/IR/Function.h"
29 #include "llvm/IR/GVMaterializer.h"
30 #include "llvm/IR/Instruction.h"
31 #include "llvm/IR/IntrinsicInst.h"
32 #include "llvm/IR/LLVMContext.h"
33 #include "llvm/IR/Metadata.h"
34 #include "llvm/IR/Module.h"
35 #include "llvm/Support/Casting.h"
36 #include <algorithm>
37 #include <cassert>
38 #include <utility>
40 using namespace llvm;
41 using namespace llvm::dwarf;
43 DISubprogram *llvm::getDISubprogram(const MDNode *Scope) {
44 if (auto *LocalScope = dyn_cast_or_null<DILocalScope>(Scope))
45 return LocalScope->getSubprogram();
46 return nullptr;
49 //===----------------------------------------------------------------------===//
50 // DebugInfoFinder implementations.
51 //===----------------------------------------------------------------------===//
53 void DebugInfoFinder::reset() {
54 CUs.clear();
55 SPs.clear();
56 GVs.clear();
57 TYs.clear();
58 Scopes.clear();
59 NodesSeen.clear();
62 void DebugInfoFinder::processModule(const Module &M) {
63 for (auto *CU : M.debug_compile_units())
64 processCompileUnit(CU);
65 for (auto &F : M.functions()) {
66 if (auto *SP = cast_or_null<DISubprogram>(F.getSubprogram()))
67 processSubprogram(SP);
68 // There could be subprograms from inlined functions referenced from
69 // instructions only. Walk the function to find them.
70 for (const BasicBlock &BB : F)
71 for (const Instruction &I : BB)
72 processInstruction(M, I);
76 void DebugInfoFinder::processCompileUnit(DICompileUnit *CU) {
77 if (!addCompileUnit(CU))
78 return;
79 for (auto DIG : CU->getGlobalVariables()) {
80 if (!addGlobalVariable(DIG))
81 continue;
82 auto *GV = DIG->getVariable();
83 processScope(GV->getScope());
84 processType(GV->getType());
86 for (auto *ET : CU->getEnumTypes())
87 processType(ET);
88 for (auto *RT : CU->getRetainedTypes())
89 if (auto *T = dyn_cast<DIType>(RT))
90 processType(T);
91 else
92 processSubprogram(cast<DISubprogram>(RT));
93 for (auto *Import : CU->getImportedEntities()) {
94 auto *Entity = Import->getEntity();
95 if (auto *T = dyn_cast<DIType>(Entity))
96 processType(T);
97 else if (auto *SP = dyn_cast<DISubprogram>(Entity))
98 processSubprogram(SP);
99 else if (auto *NS = dyn_cast<DINamespace>(Entity))
100 processScope(NS->getScope());
101 else if (auto *M = dyn_cast<DIModule>(Entity))
102 processScope(M->getScope());
106 void DebugInfoFinder::processInstruction(const Module &M,
107 const Instruction &I) {
108 if (auto *DDI = dyn_cast<DbgDeclareInst>(&I))
109 processDeclare(M, DDI);
110 else if (auto *DVI = dyn_cast<DbgValueInst>(&I))
111 processValue(M, DVI);
113 if (auto DbgLoc = I.getDebugLoc())
114 processLocation(M, DbgLoc.get());
117 void DebugInfoFinder::processLocation(const Module &M, const DILocation *Loc) {
118 if (!Loc)
119 return;
120 processScope(Loc->getScope());
121 processLocation(M, Loc->getInlinedAt());
124 void DebugInfoFinder::processType(DIType *DT) {
125 if (!addType(DT))
126 return;
127 processScope(DT->getScope());
128 if (auto *ST = dyn_cast<DISubroutineType>(DT)) {
129 for (DIType *Ref : ST->getTypeArray())
130 processType(Ref);
131 return;
133 if (auto *DCT = dyn_cast<DICompositeType>(DT)) {
134 processType(DCT->getBaseType());
135 for (Metadata *D : DCT->getElements()) {
136 if (auto *T = dyn_cast<DIType>(D))
137 processType(T);
138 else if (auto *SP = dyn_cast<DISubprogram>(D))
139 processSubprogram(SP);
141 return;
143 if (auto *DDT = dyn_cast<DIDerivedType>(DT)) {
144 processType(DDT->getBaseType());
148 void DebugInfoFinder::processScope(DIScope *Scope) {
149 if (!Scope)
150 return;
151 if (auto *Ty = dyn_cast<DIType>(Scope)) {
152 processType(Ty);
153 return;
155 if (auto *CU = dyn_cast<DICompileUnit>(Scope)) {
156 addCompileUnit(CU);
157 return;
159 if (auto *SP = dyn_cast<DISubprogram>(Scope)) {
160 processSubprogram(SP);
161 return;
163 if (!addScope(Scope))
164 return;
165 if (auto *LB = dyn_cast<DILexicalBlockBase>(Scope)) {
166 processScope(LB->getScope());
167 } else if (auto *NS = dyn_cast<DINamespace>(Scope)) {
168 processScope(NS->getScope());
169 } else if (auto *M = dyn_cast<DIModule>(Scope)) {
170 processScope(M->getScope());
174 void DebugInfoFinder::processSubprogram(DISubprogram *SP) {
175 if (!addSubprogram(SP))
176 return;
177 processScope(SP->getScope());
178 // Some of the users, e.g. CloneFunctionInto / CloneModule, need to set up a
179 // ValueMap containing identity mappings for all of the DICompileUnit's, not
180 // just DISubprogram's, referenced from anywhere within the Function being
181 // cloned prior to calling MapMetadata / RemapInstruction to avoid their
182 // duplication later as DICompileUnit's are also directly referenced by
183 // llvm.dbg.cu list. Thefore we need to collect DICompileUnit's here as well.
184 // Also, DICompileUnit's may reference DISubprogram's too and therefore need
185 // to be at least looked through.
186 processCompileUnit(SP->getUnit());
187 processType(SP->getType());
188 for (auto *Element : SP->getTemplateParams()) {
189 if (auto *TType = dyn_cast<DITemplateTypeParameter>(Element)) {
190 processType(TType->getType());
191 } else if (auto *TVal = dyn_cast<DITemplateValueParameter>(Element)) {
192 processType(TVal->getType());
197 void DebugInfoFinder::processDeclare(const Module &M,
198 const DbgDeclareInst *DDI) {
199 auto *N = dyn_cast<MDNode>(DDI->getVariable());
200 if (!N)
201 return;
203 auto *DV = dyn_cast<DILocalVariable>(N);
204 if (!DV)
205 return;
207 if (!NodesSeen.insert(DV).second)
208 return;
209 processScope(DV->getScope());
210 processType(DV->getType());
213 void DebugInfoFinder::processValue(const Module &M, const DbgValueInst *DVI) {
214 auto *N = dyn_cast<MDNode>(DVI->getVariable());
215 if (!N)
216 return;
218 auto *DV = dyn_cast<DILocalVariable>(N);
219 if (!DV)
220 return;
222 if (!NodesSeen.insert(DV).second)
223 return;
224 processScope(DV->getScope());
225 processType(DV->getType());
228 bool DebugInfoFinder::addType(DIType *DT) {
229 if (!DT)
230 return false;
232 if (!NodesSeen.insert(DT).second)
233 return false;
235 TYs.push_back(const_cast<DIType *>(DT));
236 return true;
239 bool DebugInfoFinder::addCompileUnit(DICompileUnit *CU) {
240 if (!CU)
241 return false;
242 if (!NodesSeen.insert(CU).second)
243 return false;
245 CUs.push_back(CU);
246 return true;
249 bool DebugInfoFinder::addGlobalVariable(DIGlobalVariableExpression *DIG) {
250 if (!NodesSeen.insert(DIG).second)
251 return false;
253 GVs.push_back(DIG);
254 return true;
257 bool DebugInfoFinder::addSubprogram(DISubprogram *SP) {
258 if (!SP)
259 return false;
261 if (!NodesSeen.insert(SP).second)
262 return false;
264 SPs.push_back(SP);
265 return true;
268 bool DebugInfoFinder::addScope(DIScope *Scope) {
269 if (!Scope)
270 return false;
271 // FIXME: Ocaml binding generates a scope with no content, we treat it
272 // as null for now.
273 if (Scope->getNumOperands() == 0)
274 return false;
275 if (!NodesSeen.insert(Scope).second)
276 return false;
277 Scopes.push_back(Scope);
278 return true;
281 static MDNode *stripDebugLocFromLoopID(MDNode *N) {
282 assert(!N->operands().empty() && "Missing self reference?");
284 // if there is no debug location, we do not have to rewrite this MDNode.
285 if (std::none_of(N->op_begin() + 1, N->op_end(), [](const MDOperand &Op) {
286 return isa<DILocation>(Op.get());
288 return N;
290 // If there is only the debug location without any actual loop metadata, we
291 // can remove the metadata.
292 if (std::none_of(N->op_begin() + 1, N->op_end(), [](const MDOperand &Op) {
293 return !isa<DILocation>(Op.get());
295 return nullptr;
297 SmallVector<Metadata *, 4> Args;
298 // Reserve operand 0 for loop id self reference.
299 auto TempNode = MDNode::getTemporary(N->getContext(), None);
300 Args.push_back(TempNode.get());
301 // Add all non-debug location operands back.
302 for (auto Op = N->op_begin() + 1; Op != N->op_end(); Op++) {
303 if (!isa<DILocation>(*Op))
304 Args.push_back(*Op);
307 // Set the first operand to itself.
308 MDNode *LoopID = MDNode::get(N->getContext(), Args);
309 LoopID->replaceOperandWith(0, LoopID);
310 return LoopID;
313 bool llvm::stripDebugInfo(Function &F) {
314 bool Changed = false;
315 if (F.hasMetadata(LLVMContext::MD_dbg)) {
316 Changed = true;
317 F.setSubprogram(nullptr);
320 DenseMap<MDNode*, MDNode*> LoopIDsMap;
321 for (BasicBlock &BB : F) {
322 for (auto II = BB.begin(), End = BB.end(); II != End;) {
323 Instruction &I = *II++; // We may delete the instruction, increment now.
324 if (isa<DbgInfoIntrinsic>(&I)) {
325 I.eraseFromParent();
326 Changed = true;
327 continue;
329 if (I.getDebugLoc()) {
330 Changed = true;
331 I.setDebugLoc(DebugLoc());
335 auto *TermInst = BB.getTerminator();
336 if (!TermInst)
337 // This is invalid IR, but we may not have run the verifier yet
338 continue;
339 if (auto *LoopID = TermInst->getMetadata(LLVMContext::MD_loop)) {
340 auto *NewLoopID = LoopIDsMap.lookup(LoopID);
341 if (!NewLoopID)
342 NewLoopID = LoopIDsMap[LoopID] = stripDebugLocFromLoopID(LoopID);
343 if (NewLoopID != LoopID)
344 TermInst->setMetadata(LLVMContext::MD_loop, NewLoopID);
347 return Changed;
350 bool llvm::StripDebugInfo(Module &M) {
351 bool Changed = false;
353 for (Module::named_metadata_iterator NMI = M.named_metadata_begin(),
354 NME = M.named_metadata_end(); NMI != NME;) {
355 NamedMDNode *NMD = &*NMI;
356 ++NMI;
358 // We're stripping debug info, and without them, coverage information
359 // doesn't quite make sense.
360 if (NMD->getName().startswith("llvm.dbg.") ||
361 NMD->getName() == "llvm.gcov") {
362 NMD->eraseFromParent();
363 Changed = true;
367 for (Function &F : M)
368 Changed |= stripDebugInfo(F);
370 for (auto &GV : M.globals()) {
371 Changed |= GV.eraseMetadata(LLVMContext::MD_dbg);
374 if (GVMaterializer *Materializer = M.getMaterializer())
375 Materializer->setStripDebugInfo();
377 return Changed;
380 namespace {
382 /// Helper class to downgrade -g metadata to -gline-tables-only metadata.
383 class DebugTypeInfoRemoval {
384 DenseMap<Metadata *, Metadata *> Replacements;
386 public:
387 /// The (void)() type.
388 MDNode *EmptySubroutineType;
390 private:
391 /// Remember what linkage name we originally had before stripping. If we end
392 /// up making two subprograms identical who originally had different linkage
393 /// names, then we need to make one of them distinct, to avoid them getting
394 /// uniqued. Maps the new node to the old linkage name.
395 DenseMap<DISubprogram *, StringRef> NewToLinkageName;
397 // TODO: Remember the distinct subprogram we created for a given linkage name,
398 // so that we can continue to unique whenever possible. Map <newly created
399 // node, old linkage name> to the first (possibly distinct) mdsubprogram
400 // created for that combination. This is not strictly needed for correctness,
401 // but can cut down on the number of MDNodes and let us diff cleanly with the
402 // output of -gline-tables-only.
404 public:
405 DebugTypeInfoRemoval(LLVMContext &C)
406 : EmptySubroutineType(DISubroutineType::get(C, DINode::FlagZero, 0,
407 MDNode::get(C, {}))) {}
409 Metadata *map(Metadata *M) {
410 if (!M)
411 return nullptr;
412 auto Replacement = Replacements.find(M);
413 if (Replacement != Replacements.end())
414 return Replacement->second;
416 return M;
418 MDNode *mapNode(Metadata *N) { return dyn_cast_or_null<MDNode>(map(N)); }
420 /// Recursively remap N and all its referenced children. Does a DF post-order
421 /// traversal, so as to remap bottoms up.
422 void traverseAndRemap(MDNode *N) { traverse(N); }
424 private:
425 // Create a new DISubprogram, to replace the one given.
426 DISubprogram *getReplacementSubprogram(DISubprogram *MDS) {
427 auto *FileAndScope = cast_or_null<DIFile>(map(MDS->getFile()));
428 StringRef LinkageName = MDS->getName().empty() ? MDS->getLinkageName() : "";
429 DISubprogram *Declaration = nullptr;
430 auto *Type = cast_or_null<DISubroutineType>(map(MDS->getType()));
431 DIType *ContainingType =
432 cast_or_null<DIType>(map(MDS->getContainingType()));
433 auto *Unit = cast_or_null<DICompileUnit>(map(MDS->getUnit()));
434 auto Variables = nullptr;
435 auto TemplateParams = nullptr;
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->getScopeLine(),
442 ContainingType, MDS->getVirtualIndex(), MDS->getThisAdjustment(),
443 MDS->getFlags(), MDS->getSPFlags(), Unit, TemplateParams, Declaration,
444 Variables);
447 if (MDS->isDistinct())
448 return distinctMDSubprogram();
450 auto *NewMDS = DISubprogram::get(
451 MDS->getContext(), FileAndScope, MDS->getName(), LinkageName,
452 FileAndScope, MDS->getLine(), Type, MDS->getScopeLine(), ContainingType,
453 MDS->getVirtualIndex(), MDS->getThisAdjustment(), MDS->getFlags(),
454 MDS->getSPFlags(), Unit, TemplateParams, Declaration, Variables);
456 StringRef OldLinkageName = MDS->getLinkageName();
458 // See if we need to make a distinct one.
459 auto OrigLinkage = NewToLinkageName.find(NewMDS);
460 if (OrigLinkage != NewToLinkageName.end()) {
461 if (OrigLinkage->second == OldLinkageName)
462 // We're good.
463 return NewMDS;
465 // Otherwise, need to make a distinct one.
466 // TODO: Query the map to see if we already have one.
467 return distinctMDSubprogram();
470 NewToLinkageName.insert({NewMDS, MDS->getLinkageName()});
471 return NewMDS;
474 /// Create a new compile unit, to replace the one given
475 DICompileUnit *getReplacementCU(DICompileUnit *CU) {
476 // Drop skeleton CUs.
477 if (CU->getDWOId())
478 return nullptr;
480 auto *File = cast_or_null<DIFile>(map(CU->getFile()));
481 MDTuple *EnumTypes = nullptr;
482 MDTuple *RetainedTypes = nullptr;
483 MDTuple *GlobalVariables = nullptr;
484 MDTuple *ImportedEntities = nullptr;
485 return DICompileUnit::getDistinct(
486 CU->getContext(), CU->getSourceLanguage(), File, CU->getProducer(),
487 CU->isOptimized(), CU->getFlags(), CU->getRuntimeVersion(),
488 CU->getSplitDebugFilename(), DICompileUnit::LineTablesOnly, EnumTypes,
489 RetainedTypes, GlobalVariables, ImportedEntities, CU->getMacros(),
490 CU->getDWOId(), CU->getSplitDebugInlining(),
491 CU->getDebugInfoForProfiling(), CU->getNameTableKind(),
492 CU->getRangesBaseAddress());
495 DILocation *getReplacementMDLocation(DILocation *MLD) {
496 auto *Scope = map(MLD->getScope());
497 auto *InlinedAt = map(MLD->getInlinedAt());
498 if (MLD->isDistinct())
499 return DILocation::getDistinct(MLD->getContext(), MLD->getLine(),
500 MLD->getColumn(), Scope, InlinedAt);
501 return DILocation::get(MLD->getContext(), MLD->getLine(), MLD->getColumn(),
502 Scope, InlinedAt);
505 /// Create a new generic MDNode, to replace the one given
506 MDNode *getReplacementMDNode(MDNode *N) {
507 SmallVector<Metadata *, 8> Ops;
508 Ops.reserve(N->getNumOperands());
509 for (auto &I : N->operands())
510 if (I)
511 Ops.push_back(map(I));
512 auto *Ret = MDNode::get(N->getContext(), Ops);
513 return Ret;
516 /// Attempt to re-map N to a newly created node.
517 void remap(MDNode *N) {
518 if (Replacements.count(N))
519 return;
521 auto doRemap = [&](MDNode *N) -> MDNode * {
522 if (!N)
523 return nullptr;
524 if (auto *MDSub = dyn_cast<DISubprogram>(N)) {
525 remap(MDSub->getUnit());
526 return getReplacementSubprogram(MDSub);
528 if (isa<DISubroutineType>(N))
529 return EmptySubroutineType;
530 if (auto *CU = dyn_cast<DICompileUnit>(N))
531 return getReplacementCU(CU);
532 if (isa<DIFile>(N))
533 return N;
534 if (auto *MDLB = dyn_cast<DILexicalBlockBase>(N))
535 // Remap to our referenced scope (recursively).
536 return mapNode(MDLB->getScope());
537 if (auto *MLD = dyn_cast<DILocation>(N))
538 return getReplacementMDLocation(MLD);
540 // Otherwise, if we see these, just drop them now. Not strictly necessary,
541 // but this speeds things up a little.
542 if (isa<DINode>(N))
543 return nullptr;
545 return getReplacementMDNode(N);
547 Replacements[N] = doRemap(N);
550 /// Do the remapping traversal.
551 void traverse(MDNode *);
554 } // end anonymous namespace
556 void DebugTypeInfoRemoval::traverse(MDNode *N) {
557 if (!N || Replacements.count(N))
558 return;
560 // To avoid cycles, as well as for efficiency sake, we will sometimes prune
561 // parts of the graph.
562 auto prune = [](MDNode *Parent, MDNode *Child) {
563 if (auto *MDS = dyn_cast<DISubprogram>(Parent))
564 return Child == MDS->getRetainedNodes().get();
565 return false;
568 SmallVector<MDNode *, 16> ToVisit;
569 DenseSet<MDNode *> Opened;
571 // Visit each node starting at N in post order, and map them.
572 ToVisit.push_back(N);
573 while (!ToVisit.empty()) {
574 auto *N = ToVisit.back();
575 if (!Opened.insert(N).second) {
576 // Close it.
577 remap(N);
578 ToVisit.pop_back();
579 continue;
581 for (auto &I : N->operands())
582 if (auto *MDN = dyn_cast_or_null<MDNode>(I))
583 if (!Opened.count(MDN) && !Replacements.count(MDN) && !prune(N, MDN) &&
584 !isa<DICompileUnit>(MDN))
585 ToVisit.push_back(MDN);
589 bool llvm::stripNonLineTableDebugInfo(Module &M) {
590 bool Changed = false;
592 // First off, delete the debug intrinsics.
593 auto RemoveUses = [&](StringRef Name) {
594 if (auto *DbgVal = M.getFunction(Name)) {
595 while (!DbgVal->use_empty())
596 cast<Instruction>(DbgVal->user_back())->eraseFromParent();
597 DbgVal->eraseFromParent();
598 Changed = true;
601 RemoveUses("llvm.dbg.declare");
602 RemoveUses("llvm.dbg.value");
604 // Delete non-CU debug info named metadata nodes.
605 for (auto NMI = M.named_metadata_begin(), NME = M.named_metadata_end();
606 NMI != NME;) {
607 NamedMDNode *NMD = &*NMI;
608 ++NMI;
609 // Specifically keep dbg.cu around.
610 if (NMD->getName() == "llvm.dbg.cu")
611 continue;
614 // Drop all dbg attachments from global variables.
615 for (auto &GV : M.globals())
616 GV.eraseMetadata(LLVMContext::MD_dbg);
618 DebugTypeInfoRemoval Mapper(M.getContext());
619 auto remap = [&](MDNode *Node) -> MDNode * {
620 if (!Node)
621 return nullptr;
622 Mapper.traverseAndRemap(Node);
623 auto *NewNode = Mapper.mapNode(Node);
624 Changed |= Node != NewNode;
625 Node = NewNode;
626 return NewNode;
629 // Rewrite the DebugLocs to be equivalent to what
630 // -gline-tables-only would have created.
631 for (auto &F : M) {
632 if (auto *SP = F.getSubprogram()) {
633 Mapper.traverseAndRemap(SP);
634 auto *NewSP = cast<DISubprogram>(Mapper.mapNode(SP));
635 Changed |= SP != NewSP;
636 F.setSubprogram(NewSP);
638 for (auto &BB : F) {
639 for (auto &I : BB) {
640 auto remapDebugLoc = [&](DebugLoc DL) -> DebugLoc {
641 auto *Scope = DL.getScope();
642 MDNode *InlinedAt = DL.getInlinedAt();
643 Scope = remap(Scope);
644 InlinedAt = remap(InlinedAt);
645 return DebugLoc::get(DL.getLine(), DL.getCol(), Scope, InlinedAt);
648 if (I.getDebugLoc() != DebugLoc())
649 I.setDebugLoc(remapDebugLoc(I.getDebugLoc()));
651 // Remap DILocations in untyped MDNodes (e.g., llvm.loop).
652 SmallVector<std::pair<unsigned, MDNode *>, 2> MDs;
653 I.getAllMetadata(MDs);
654 for (auto Attachment : MDs)
655 if (auto *T = dyn_cast_or_null<MDTuple>(Attachment.second))
656 for (unsigned N = 0; N < T->getNumOperands(); ++N)
657 if (auto *Loc = dyn_cast_or_null<DILocation>(T->getOperand(N)))
658 if (Loc != DebugLoc())
659 T->replaceOperandWith(N, remapDebugLoc(Loc));
664 // Create a new llvm.dbg.cu, which is equivalent to the one
665 // -gline-tables-only would have created.
666 for (auto &NMD : M.getNamedMDList()) {
667 SmallVector<MDNode *, 8> Ops;
668 for (MDNode *Op : NMD.operands())
669 Ops.push_back(remap(Op));
671 if (!Changed)
672 continue;
674 NMD.clearOperands();
675 for (auto *Op : Ops)
676 if (Op)
677 NMD.addOperand(Op);
679 return Changed;
682 unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
683 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
684 M.getModuleFlag("Debug Info Version")))
685 return Val->getZExtValue();
686 return 0;
689 void Instruction::applyMergedLocation(const DILocation *LocA,
690 const DILocation *LocB) {
691 setDebugLoc(DILocation::getMergedLocation(LocA, LocB));
694 //===----------------------------------------------------------------------===//
695 // LLVM C API implementations.
696 //===----------------------------------------------------------------------===//
698 static unsigned map_from_llvmDWARFsourcelanguage(LLVMDWARFSourceLanguage lang) {
699 switch (lang) {
700 #define HANDLE_DW_LANG(ID, NAME, LOWER_BOUND, VERSION, VENDOR) \
701 case LLVMDWARFSourceLanguage##NAME: \
702 return ID;
703 #include "llvm/BinaryFormat/Dwarf.def"
704 #undef HANDLE_DW_LANG
706 llvm_unreachable("Unhandled Tag");
709 template <typename DIT> DIT *unwrapDI(LLVMMetadataRef Ref) {
710 return (DIT *)(Ref ? unwrap<MDNode>(Ref) : nullptr);
713 static DINode::DIFlags map_from_llvmDIFlags(LLVMDIFlags Flags) {
714 return static_cast<DINode::DIFlags>(Flags);
717 static LLVMDIFlags map_to_llvmDIFlags(DINode::DIFlags Flags) {
718 return static_cast<LLVMDIFlags>(Flags);
721 static DISubprogram::DISPFlags
722 pack_into_DISPFlags(bool IsLocalToUnit, bool IsDefinition, bool IsOptimized) {
723 return DISubprogram::toSPFlags(IsLocalToUnit, IsDefinition, IsOptimized);
726 unsigned LLVMDebugMetadataVersion() {
727 return DEBUG_METADATA_VERSION;
730 LLVMDIBuilderRef LLVMCreateDIBuilderDisallowUnresolved(LLVMModuleRef M) {
731 return wrap(new DIBuilder(*unwrap(M), false));
734 LLVMDIBuilderRef LLVMCreateDIBuilder(LLVMModuleRef M) {
735 return wrap(new DIBuilder(*unwrap(M)));
738 unsigned LLVMGetModuleDebugMetadataVersion(LLVMModuleRef M) {
739 return getDebugMetadataVersionFromModule(*unwrap(M));
742 LLVMBool LLVMStripModuleDebugInfo(LLVMModuleRef M) {
743 return StripDebugInfo(*unwrap(M));
746 void LLVMDisposeDIBuilder(LLVMDIBuilderRef Builder) {
747 delete unwrap(Builder);
750 void LLVMDIBuilderFinalize(LLVMDIBuilderRef Builder) {
751 unwrap(Builder)->finalize();
754 LLVMMetadataRef LLVMDIBuilderCreateCompileUnit(
755 LLVMDIBuilderRef Builder, LLVMDWARFSourceLanguage Lang,
756 LLVMMetadataRef FileRef, const char *Producer, size_t ProducerLen,
757 LLVMBool isOptimized, const char *Flags, size_t FlagsLen,
758 unsigned RuntimeVer, const char *SplitName, size_t SplitNameLen,
759 LLVMDWARFEmissionKind Kind, unsigned DWOId, LLVMBool SplitDebugInlining,
760 LLVMBool DebugInfoForProfiling) {
761 auto File = unwrapDI<DIFile>(FileRef);
763 return wrap(unwrap(Builder)->createCompileUnit(
764 map_from_llvmDWARFsourcelanguage(Lang), File,
765 StringRef(Producer, ProducerLen), isOptimized,
766 StringRef(Flags, FlagsLen), RuntimeVer,
767 StringRef(SplitName, SplitNameLen),
768 static_cast<DICompileUnit::DebugEmissionKind>(Kind), DWOId,
769 SplitDebugInlining, DebugInfoForProfiling));
772 LLVMMetadataRef
773 LLVMDIBuilderCreateFile(LLVMDIBuilderRef Builder, const char *Filename,
774 size_t FilenameLen, const char *Directory,
775 size_t DirectoryLen) {
776 return wrap(unwrap(Builder)->createFile(StringRef(Filename, FilenameLen),
777 StringRef(Directory, DirectoryLen)));
780 LLVMMetadataRef
781 LLVMDIBuilderCreateModule(LLVMDIBuilderRef Builder, LLVMMetadataRef ParentScope,
782 const char *Name, size_t NameLen,
783 const char *ConfigMacros, size_t ConfigMacrosLen,
784 const char *IncludePath, size_t IncludePathLen,
785 const char *ISysRoot, size_t ISysRootLen) {
786 return wrap(unwrap(Builder)->createModule(
787 unwrapDI<DIScope>(ParentScope), StringRef(Name, NameLen),
788 StringRef(ConfigMacros, ConfigMacrosLen),
789 StringRef(IncludePath, IncludePathLen),
790 StringRef(ISysRoot, ISysRootLen)));
793 LLVMMetadataRef LLVMDIBuilderCreateNameSpace(LLVMDIBuilderRef Builder,
794 LLVMMetadataRef ParentScope,
795 const char *Name, size_t NameLen,
796 LLVMBool ExportSymbols) {
797 return wrap(unwrap(Builder)->createNameSpace(
798 unwrapDI<DIScope>(ParentScope), StringRef(Name, NameLen), ExportSymbols));
801 LLVMMetadataRef LLVMDIBuilderCreateFunction(
802 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
803 size_t NameLen, const char *LinkageName, size_t LinkageNameLen,
804 LLVMMetadataRef File, unsigned LineNo, LLVMMetadataRef Ty,
805 LLVMBool IsLocalToUnit, LLVMBool IsDefinition,
806 unsigned ScopeLine, LLVMDIFlags Flags, LLVMBool IsOptimized) {
807 return wrap(unwrap(Builder)->createFunction(
808 unwrapDI<DIScope>(Scope), {Name, NameLen}, {LinkageName, LinkageNameLen},
809 unwrapDI<DIFile>(File), LineNo, unwrapDI<DISubroutineType>(Ty), ScopeLine,
810 map_from_llvmDIFlags(Flags),
811 pack_into_DISPFlags(IsLocalToUnit, IsDefinition, IsOptimized), nullptr,
812 nullptr, nullptr));
816 LLVMMetadataRef LLVMDIBuilderCreateLexicalBlock(
817 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope,
818 LLVMMetadataRef File, unsigned Line, unsigned Col) {
819 return wrap(unwrap(Builder)->createLexicalBlock(unwrapDI<DIScope>(Scope),
820 unwrapDI<DIFile>(File),
821 Line, Col));
824 LLVMMetadataRef
825 LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef Builder,
826 LLVMMetadataRef Scope,
827 LLVMMetadataRef File,
828 unsigned Discriminator) {
829 return wrap(unwrap(Builder)->createLexicalBlockFile(unwrapDI<DIScope>(Scope),
830 unwrapDI<DIFile>(File),
831 Discriminator));
834 LLVMMetadataRef
835 LLVMDIBuilderCreateImportedModuleFromNamespace(LLVMDIBuilderRef Builder,
836 LLVMMetadataRef Scope,
837 LLVMMetadataRef NS,
838 LLVMMetadataRef File,
839 unsigned Line) {
840 return wrap(unwrap(Builder)->createImportedModule(unwrapDI<DIScope>(Scope),
841 unwrapDI<DINamespace>(NS),
842 unwrapDI<DIFile>(File),
843 Line));
846 LLVMMetadataRef
847 LLVMDIBuilderCreateImportedModuleFromAlias(LLVMDIBuilderRef Builder,
848 LLVMMetadataRef Scope,
849 LLVMMetadataRef ImportedEntity,
850 LLVMMetadataRef File,
851 unsigned Line) {
852 return wrap(unwrap(Builder)->createImportedModule(
853 unwrapDI<DIScope>(Scope),
854 unwrapDI<DIImportedEntity>(ImportedEntity),
855 unwrapDI<DIFile>(File), Line));
858 LLVMMetadataRef
859 LLVMDIBuilderCreateImportedModuleFromModule(LLVMDIBuilderRef Builder,
860 LLVMMetadataRef Scope,
861 LLVMMetadataRef M,
862 LLVMMetadataRef File,
863 unsigned Line) {
864 return wrap(unwrap(Builder)->createImportedModule(unwrapDI<DIScope>(Scope),
865 unwrapDI<DIModule>(M),
866 unwrapDI<DIFile>(File),
867 Line));
870 LLVMMetadataRef
871 LLVMDIBuilderCreateImportedDeclaration(LLVMDIBuilderRef Builder,
872 LLVMMetadataRef Scope,
873 LLVMMetadataRef Decl,
874 LLVMMetadataRef File,
875 unsigned Line,
876 const char *Name, size_t NameLen) {
877 return wrap(unwrap(Builder)->createImportedDeclaration(
878 unwrapDI<DIScope>(Scope),
879 unwrapDI<DINode>(Decl),
880 unwrapDI<DIFile>(File), Line, {Name, NameLen}));
883 LLVMMetadataRef
884 LLVMDIBuilderCreateDebugLocation(LLVMContextRef Ctx, unsigned Line,
885 unsigned Column, LLVMMetadataRef Scope,
886 LLVMMetadataRef InlinedAt) {
887 return wrap(DILocation::get(*unwrap(Ctx), Line, Column, unwrap(Scope),
888 unwrap(InlinedAt)));
891 unsigned LLVMDILocationGetLine(LLVMMetadataRef Location) {
892 return unwrapDI<DILocation>(Location)->getLine();
895 unsigned LLVMDILocationGetColumn(LLVMMetadataRef Location) {
896 return unwrapDI<DILocation>(Location)->getColumn();
899 LLVMMetadataRef LLVMDILocationGetScope(LLVMMetadataRef Location) {
900 return wrap(unwrapDI<DILocation>(Location)->getScope());
903 LLVMMetadataRef LLVMDILocationGetInlinedAt(LLVMMetadataRef Location) {
904 return wrap(unwrapDI<DILocation>(Location)->getInlinedAt());
907 LLVMMetadataRef LLVMDIScopeGetFile(LLVMMetadataRef Scope) {
908 return wrap(unwrapDI<DIScope>(Scope)->getFile());
911 const char *LLVMDIFileGetDirectory(LLVMMetadataRef File, unsigned *Len) {
912 auto Dir = unwrapDI<DIFile>(File)->getDirectory();
913 *Len = Dir.size();
914 return Dir.data();
917 const char *LLVMDIFileGetFilename(LLVMMetadataRef File, unsigned *Len) {
918 auto Name = unwrapDI<DIFile>(File)->getFilename();
919 *Len = Name.size();
920 return Name.data();
923 const char *LLVMDIFileGetSource(LLVMMetadataRef File, unsigned *Len) {
924 if (auto Src = unwrapDI<DIFile>(File)->getSource()) {
925 *Len = Src->size();
926 return Src->data();
928 *Len = 0;
929 return "";
932 LLVMMetadataRef LLVMDIBuilderCreateMacro(LLVMDIBuilderRef Builder,
933 LLVMMetadataRef ParentMacroFile,
934 unsigned Line,
935 LLVMDWARFMacinfoRecordType RecordType,
936 const char *Name, size_t NameLen,
937 const char *Value, size_t ValueLen) {
938 return wrap(
939 unwrap(Builder)->createMacro(unwrapDI<DIMacroFile>(ParentMacroFile), Line,
940 static_cast<MacinfoRecordType>(RecordType),
941 {Name, NameLen}, {Value, ValueLen}));
944 LLVMMetadataRef
945 LLVMDIBuilderCreateTempMacroFile(LLVMDIBuilderRef Builder,
946 LLVMMetadataRef ParentMacroFile, unsigned Line,
947 LLVMMetadataRef File) {
948 return wrap(unwrap(Builder)->createTempMacroFile(
949 unwrapDI<DIMacroFile>(ParentMacroFile), Line, unwrapDI<DIFile>(File)));
952 LLVMMetadataRef LLVMDIBuilderCreateEnumerator(LLVMDIBuilderRef Builder,
953 const char *Name, size_t NameLen,
954 int64_t Value,
955 LLVMBool IsUnsigned) {
956 return wrap(unwrap(Builder)->createEnumerator({Name, NameLen}, Value,
957 IsUnsigned != 0));
960 LLVMMetadataRef LLVMDIBuilderCreateEnumerationType(
961 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
962 size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
963 uint64_t SizeInBits, uint32_t AlignInBits, LLVMMetadataRef *Elements,
964 unsigned NumElements, LLVMMetadataRef ClassTy) {
965 auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements),
966 NumElements});
967 return wrap(unwrap(Builder)->createEnumerationType(
968 unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File),
969 LineNumber, SizeInBits, AlignInBits, Elts, unwrapDI<DIType>(ClassTy)));
972 LLVMMetadataRef LLVMDIBuilderCreateUnionType(
973 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
974 size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
975 uint64_t SizeInBits, uint32_t AlignInBits, LLVMDIFlags Flags,
976 LLVMMetadataRef *Elements, unsigned NumElements, unsigned RunTimeLang,
977 const char *UniqueId, size_t UniqueIdLen) {
978 auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements),
979 NumElements});
980 return wrap(unwrap(Builder)->createUnionType(
981 unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File),
982 LineNumber, SizeInBits, AlignInBits, map_from_llvmDIFlags(Flags),
983 Elts, RunTimeLang, {UniqueId, UniqueIdLen}));
987 LLVMMetadataRef
988 LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Builder, uint64_t Size,
989 uint32_t AlignInBits, LLVMMetadataRef Ty,
990 LLVMMetadataRef *Subscripts,
991 unsigned NumSubscripts) {
992 auto Subs = unwrap(Builder)->getOrCreateArray({unwrap(Subscripts),
993 NumSubscripts});
994 return wrap(unwrap(Builder)->createArrayType(Size, AlignInBits,
995 unwrapDI<DIType>(Ty), Subs));
998 LLVMMetadataRef
999 LLVMDIBuilderCreateVectorType(LLVMDIBuilderRef Builder, uint64_t Size,
1000 uint32_t AlignInBits, LLVMMetadataRef Ty,
1001 LLVMMetadataRef *Subscripts,
1002 unsigned NumSubscripts) {
1003 auto Subs = unwrap(Builder)->getOrCreateArray({unwrap(Subscripts),
1004 NumSubscripts});
1005 return wrap(unwrap(Builder)->createVectorType(Size, AlignInBits,
1006 unwrapDI<DIType>(Ty), Subs));
1009 LLVMMetadataRef
1010 LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef Builder, const char *Name,
1011 size_t NameLen, uint64_t SizeInBits,
1012 LLVMDWARFTypeEncoding Encoding,
1013 LLVMDIFlags Flags) {
1014 return wrap(unwrap(Builder)->createBasicType({Name, NameLen},
1015 SizeInBits, Encoding,
1016 map_from_llvmDIFlags(Flags)));
1019 LLVMMetadataRef LLVMDIBuilderCreatePointerType(
1020 LLVMDIBuilderRef Builder, LLVMMetadataRef PointeeTy,
1021 uint64_t SizeInBits, uint32_t AlignInBits, unsigned AddressSpace,
1022 const char *Name, size_t NameLen) {
1023 return wrap(unwrap(Builder)->createPointerType(unwrapDI<DIType>(PointeeTy),
1024 SizeInBits, AlignInBits,
1025 AddressSpace, {Name, NameLen}));
1028 LLVMMetadataRef LLVMDIBuilderCreateStructType(
1029 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
1030 size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
1031 uint64_t SizeInBits, uint32_t AlignInBits, LLVMDIFlags Flags,
1032 LLVMMetadataRef DerivedFrom, LLVMMetadataRef *Elements,
1033 unsigned NumElements, unsigned RunTimeLang, LLVMMetadataRef VTableHolder,
1034 const char *UniqueId, size_t UniqueIdLen) {
1035 auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements),
1036 NumElements});
1037 return wrap(unwrap(Builder)->createStructType(
1038 unwrapDI<DIScope>(Scope), {Name, NameLen}, unwrapDI<DIFile>(File),
1039 LineNumber, SizeInBits, AlignInBits, map_from_llvmDIFlags(Flags),
1040 unwrapDI<DIType>(DerivedFrom), Elts, RunTimeLang,
1041 unwrapDI<DIType>(VTableHolder), {UniqueId, UniqueIdLen}));
1044 LLVMMetadataRef LLVMDIBuilderCreateMemberType(
1045 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
1046 size_t NameLen, LLVMMetadataRef File, unsigned LineNo, uint64_t SizeInBits,
1047 uint32_t AlignInBits, uint64_t OffsetInBits, LLVMDIFlags Flags,
1048 LLVMMetadataRef Ty) {
1049 return wrap(unwrap(Builder)->createMemberType(unwrapDI<DIScope>(Scope),
1050 {Name, NameLen}, unwrapDI<DIFile>(File), LineNo, SizeInBits, AlignInBits,
1051 OffsetInBits, map_from_llvmDIFlags(Flags), unwrapDI<DIType>(Ty)));
1054 LLVMMetadataRef
1055 LLVMDIBuilderCreateUnspecifiedType(LLVMDIBuilderRef Builder, const char *Name,
1056 size_t NameLen) {
1057 return wrap(unwrap(Builder)->createUnspecifiedType({Name, NameLen}));
1060 LLVMMetadataRef
1061 LLVMDIBuilderCreateStaticMemberType(
1062 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
1063 size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
1064 LLVMMetadataRef Type, LLVMDIFlags Flags, LLVMValueRef ConstantVal,
1065 uint32_t AlignInBits) {
1066 return wrap(unwrap(Builder)->createStaticMemberType(
1067 unwrapDI<DIScope>(Scope), {Name, NameLen},
1068 unwrapDI<DIFile>(File), LineNumber, unwrapDI<DIType>(Type),
1069 map_from_llvmDIFlags(Flags), unwrap<Constant>(ConstantVal),
1070 AlignInBits));
1073 LLVMMetadataRef
1074 LLVMDIBuilderCreateObjCIVar(LLVMDIBuilderRef Builder,
1075 const char *Name, size_t NameLen,
1076 LLVMMetadataRef File, unsigned LineNo,
1077 uint64_t SizeInBits, uint32_t AlignInBits,
1078 uint64_t OffsetInBits, LLVMDIFlags Flags,
1079 LLVMMetadataRef Ty, LLVMMetadataRef PropertyNode) {
1080 return wrap(unwrap(Builder)->createObjCIVar(
1081 {Name, NameLen}, unwrapDI<DIFile>(File), LineNo,
1082 SizeInBits, AlignInBits, OffsetInBits,
1083 map_from_llvmDIFlags(Flags), unwrapDI<DIType>(Ty),
1084 unwrapDI<MDNode>(PropertyNode)));
1087 LLVMMetadataRef
1088 LLVMDIBuilderCreateObjCProperty(LLVMDIBuilderRef Builder,
1089 const char *Name, size_t NameLen,
1090 LLVMMetadataRef File, unsigned LineNo,
1091 const char *GetterName, size_t GetterNameLen,
1092 const char *SetterName, size_t SetterNameLen,
1093 unsigned PropertyAttributes,
1094 LLVMMetadataRef Ty) {
1095 return wrap(unwrap(Builder)->createObjCProperty(
1096 {Name, NameLen}, unwrapDI<DIFile>(File), LineNo,
1097 {GetterName, GetterNameLen}, {SetterName, SetterNameLen},
1098 PropertyAttributes, unwrapDI<DIType>(Ty)));
1101 LLVMMetadataRef
1102 LLVMDIBuilderCreateObjectPointerType(LLVMDIBuilderRef Builder,
1103 LLVMMetadataRef Type) {
1104 return wrap(unwrap(Builder)->createObjectPointerType(unwrapDI<DIType>(Type)));
1107 LLVMMetadataRef
1108 LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef Builder, LLVMMetadataRef Type,
1109 const char *Name, size_t NameLen,
1110 LLVMMetadataRef File, unsigned LineNo,
1111 LLVMMetadataRef Scope) {
1112 return wrap(unwrap(Builder)->createTypedef(
1113 unwrapDI<DIType>(Type), {Name, NameLen},
1114 unwrapDI<DIFile>(File), LineNo,
1115 unwrapDI<DIScope>(Scope)));
1118 LLVMMetadataRef
1119 LLVMDIBuilderCreateInheritance(LLVMDIBuilderRef Builder,
1120 LLVMMetadataRef Ty, LLVMMetadataRef BaseTy,
1121 uint64_t BaseOffset, uint32_t VBPtrOffset,
1122 LLVMDIFlags Flags) {
1123 return wrap(unwrap(Builder)->createInheritance(
1124 unwrapDI<DIType>(Ty), unwrapDI<DIType>(BaseTy),
1125 BaseOffset, VBPtrOffset, map_from_llvmDIFlags(Flags)));
1128 LLVMMetadataRef
1129 LLVMDIBuilderCreateForwardDecl(
1130 LLVMDIBuilderRef Builder, unsigned Tag, const char *Name,
1131 size_t NameLen, LLVMMetadataRef Scope, LLVMMetadataRef File, unsigned Line,
1132 unsigned RuntimeLang, uint64_t SizeInBits, uint32_t AlignInBits,
1133 const char *UniqueIdentifier, size_t UniqueIdentifierLen) {
1134 return wrap(unwrap(Builder)->createForwardDecl(
1135 Tag, {Name, NameLen}, unwrapDI<DIScope>(Scope),
1136 unwrapDI<DIFile>(File), Line, RuntimeLang, SizeInBits,
1137 AlignInBits, {UniqueIdentifier, UniqueIdentifierLen}));
1140 LLVMMetadataRef
1141 LLVMDIBuilderCreateReplaceableCompositeType(
1142 LLVMDIBuilderRef Builder, unsigned Tag, const char *Name,
1143 size_t NameLen, LLVMMetadataRef Scope, LLVMMetadataRef File, unsigned Line,
1144 unsigned RuntimeLang, uint64_t SizeInBits, uint32_t AlignInBits,
1145 LLVMDIFlags Flags, const char *UniqueIdentifier,
1146 size_t UniqueIdentifierLen) {
1147 return wrap(unwrap(Builder)->createReplaceableCompositeType(
1148 Tag, {Name, NameLen}, unwrapDI<DIScope>(Scope),
1149 unwrapDI<DIFile>(File), Line, RuntimeLang, SizeInBits,
1150 AlignInBits, map_from_llvmDIFlags(Flags),
1151 {UniqueIdentifier, UniqueIdentifierLen}));
1154 LLVMMetadataRef
1155 LLVMDIBuilderCreateQualifiedType(LLVMDIBuilderRef Builder, unsigned Tag,
1156 LLVMMetadataRef Type) {
1157 return wrap(unwrap(Builder)->createQualifiedType(Tag,
1158 unwrapDI<DIType>(Type)));
1161 LLVMMetadataRef
1162 LLVMDIBuilderCreateReferenceType(LLVMDIBuilderRef Builder, unsigned Tag,
1163 LLVMMetadataRef Type) {
1164 return wrap(unwrap(Builder)->createReferenceType(Tag,
1165 unwrapDI<DIType>(Type)));
1168 LLVMMetadataRef
1169 LLVMDIBuilderCreateNullPtrType(LLVMDIBuilderRef Builder) {
1170 return wrap(unwrap(Builder)->createNullPtrType());
1173 LLVMMetadataRef
1174 LLVMDIBuilderCreateMemberPointerType(LLVMDIBuilderRef Builder,
1175 LLVMMetadataRef PointeeType,
1176 LLVMMetadataRef ClassType,
1177 uint64_t SizeInBits,
1178 uint32_t AlignInBits,
1179 LLVMDIFlags Flags) {
1180 return wrap(unwrap(Builder)->createMemberPointerType(
1181 unwrapDI<DIType>(PointeeType),
1182 unwrapDI<DIType>(ClassType), AlignInBits, SizeInBits,
1183 map_from_llvmDIFlags(Flags)));
1186 LLVMMetadataRef
1187 LLVMDIBuilderCreateBitFieldMemberType(LLVMDIBuilderRef Builder,
1188 LLVMMetadataRef Scope,
1189 const char *Name, size_t NameLen,
1190 LLVMMetadataRef File, unsigned LineNumber,
1191 uint64_t SizeInBits,
1192 uint64_t OffsetInBits,
1193 uint64_t StorageOffsetInBits,
1194 LLVMDIFlags Flags, LLVMMetadataRef Type) {
1195 return wrap(unwrap(Builder)->createBitFieldMemberType(
1196 unwrapDI<DIScope>(Scope), {Name, NameLen},
1197 unwrapDI<DIFile>(File), LineNumber,
1198 SizeInBits, OffsetInBits, StorageOffsetInBits,
1199 map_from_llvmDIFlags(Flags), unwrapDI<DIType>(Type)));
1202 LLVMMetadataRef LLVMDIBuilderCreateClassType(LLVMDIBuilderRef Builder,
1203 LLVMMetadataRef Scope, const char *Name, size_t NameLen,
1204 LLVMMetadataRef File, unsigned LineNumber, uint64_t SizeInBits,
1205 uint32_t AlignInBits, uint64_t OffsetInBits, LLVMDIFlags Flags,
1206 LLVMMetadataRef DerivedFrom,
1207 LLVMMetadataRef *Elements, unsigned NumElements,
1208 LLVMMetadataRef VTableHolder, LLVMMetadataRef TemplateParamsNode,
1209 const char *UniqueIdentifier, size_t UniqueIdentifierLen) {
1210 auto Elts = unwrap(Builder)->getOrCreateArray({unwrap(Elements),
1211 NumElements});
1212 return wrap(unwrap(Builder)->createClassType(
1213 unwrapDI<DIScope>(Scope), {Name, NameLen},
1214 unwrapDI<DIFile>(File), LineNumber,
1215 SizeInBits, AlignInBits, OffsetInBits,
1216 map_from_llvmDIFlags(Flags), unwrapDI<DIType>(DerivedFrom),
1217 Elts, unwrapDI<DIType>(VTableHolder),
1218 unwrapDI<MDNode>(TemplateParamsNode),
1219 {UniqueIdentifier, UniqueIdentifierLen}));
1222 LLVMMetadataRef
1223 LLVMDIBuilderCreateArtificialType(LLVMDIBuilderRef Builder,
1224 LLVMMetadataRef Type) {
1225 return wrap(unwrap(Builder)->createArtificialType(unwrapDI<DIType>(Type)));
1228 const char *LLVMDITypeGetName(LLVMMetadataRef DType, size_t *Length) {
1229 StringRef Str = unwrap<DIType>(DType)->getName();
1230 *Length = Str.size();
1231 return Str.data();
1234 uint64_t LLVMDITypeGetSizeInBits(LLVMMetadataRef DType) {
1235 return unwrapDI<DIType>(DType)->getSizeInBits();
1238 uint64_t LLVMDITypeGetOffsetInBits(LLVMMetadataRef DType) {
1239 return unwrapDI<DIType>(DType)->getOffsetInBits();
1242 uint32_t LLVMDITypeGetAlignInBits(LLVMMetadataRef DType) {
1243 return unwrapDI<DIType>(DType)->getAlignInBits();
1246 unsigned LLVMDITypeGetLine(LLVMMetadataRef DType) {
1247 return unwrapDI<DIType>(DType)->getLine();
1250 LLVMDIFlags LLVMDITypeGetFlags(LLVMMetadataRef DType) {
1251 return map_to_llvmDIFlags(unwrapDI<DIType>(DType)->getFlags());
1254 LLVMMetadataRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef Builder,
1255 LLVMMetadataRef *Types,
1256 size_t Length) {
1257 return wrap(
1258 unwrap(Builder)->getOrCreateTypeArray({unwrap(Types), Length}).get());
1261 LLVMMetadataRef
1262 LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Builder,
1263 LLVMMetadataRef File,
1264 LLVMMetadataRef *ParameterTypes,
1265 unsigned NumParameterTypes,
1266 LLVMDIFlags Flags) {
1267 auto Elts = unwrap(Builder)->getOrCreateTypeArray({unwrap(ParameterTypes),
1268 NumParameterTypes});
1269 return wrap(unwrap(Builder)->createSubroutineType(
1270 Elts, map_from_llvmDIFlags(Flags)));
1273 LLVMMetadataRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Builder,
1274 int64_t *Addr, size_t Length) {
1275 return wrap(unwrap(Builder)->createExpression(ArrayRef<int64_t>(Addr,
1276 Length)));
1279 LLVMMetadataRef
1280 LLVMDIBuilderCreateConstantValueExpression(LLVMDIBuilderRef Builder,
1281 int64_t Value) {
1282 return wrap(unwrap(Builder)->createConstantValueExpression(Value));
1285 LLVMMetadataRef LLVMDIBuilderCreateGlobalVariableExpression(
1286 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
1287 size_t NameLen, const char *Linkage, size_t LinkLen, LLVMMetadataRef File,
1288 unsigned LineNo, LLVMMetadataRef Ty, LLVMBool LocalToUnit,
1289 LLVMMetadataRef Expr, LLVMMetadataRef Decl, uint32_t AlignInBits) {
1290 return wrap(unwrap(Builder)->createGlobalVariableExpression(
1291 unwrapDI<DIScope>(Scope), {Name, NameLen}, {Linkage, LinkLen},
1292 unwrapDI<DIFile>(File), LineNo, unwrapDI<DIType>(Ty), LocalToUnit,
1293 unwrap<DIExpression>(Expr), unwrapDI<MDNode>(Decl),
1294 nullptr, AlignInBits));
1297 LLVMMetadataRef LLVMDIGlobalVariableExpressionGetVariable(LLVMMetadataRef GVE) {
1298 return wrap(unwrapDI<DIGlobalVariableExpression>(GVE)->getVariable());
1301 LLVMMetadataRef LLVMDIGlobalVariableExpressionGetExpression(
1302 LLVMMetadataRef GVE) {
1303 return wrap(unwrapDI<DIGlobalVariableExpression>(GVE)->getExpression());
1306 LLVMMetadataRef LLVMDIVariableGetFile(LLVMMetadataRef Var) {
1307 return wrap(unwrapDI<DIVariable>(Var)->getFile());
1310 LLVMMetadataRef LLVMDIVariableGetScope(LLVMMetadataRef Var) {
1311 return wrap(unwrapDI<DIVariable>(Var)->getScope());
1314 unsigned LLVMDIVariableGetLine(LLVMMetadataRef Var) {
1315 return unwrapDI<DIVariable>(Var)->getLine();
1318 LLVMMetadataRef LLVMTemporaryMDNode(LLVMContextRef Ctx, LLVMMetadataRef *Data,
1319 size_t Count) {
1320 return wrap(
1321 MDTuple::getTemporary(*unwrap(Ctx), {unwrap(Data), Count}).release());
1324 void LLVMDisposeTemporaryMDNode(LLVMMetadataRef TempNode) {
1325 MDNode::deleteTemporary(unwrapDI<MDNode>(TempNode));
1328 void LLVMMetadataReplaceAllUsesWith(LLVMMetadataRef TargetMetadata,
1329 LLVMMetadataRef Replacement) {
1330 auto *Node = unwrapDI<MDNode>(TargetMetadata);
1331 Node->replaceAllUsesWith(unwrap<Metadata>(Replacement));
1332 MDNode::deleteTemporary(Node);
1335 LLVMMetadataRef LLVMDIBuilderCreateTempGlobalVariableFwdDecl(
1336 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
1337 size_t NameLen, const char *Linkage, size_t LnkLen, LLVMMetadataRef File,
1338 unsigned LineNo, LLVMMetadataRef Ty, LLVMBool LocalToUnit,
1339 LLVMMetadataRef Decl, uint32_t AlignInBits) {
1340 return wrap(unwrap(Builder)->createTempGlobalVariableFwdDecl(
1341 unwrapDI<DIScope>(Scope), {Name, NameLen}, {Linkage, LnkLen},
1342 unwrapDI<DIFile>(File), LineNo, unwrapDI<DIType>(Ty), LocalToUnit,
1343 unwrapDI<MDNode>(Decl), nullptr, AlignInBits));
1346 LLVMValueRef
1347 LLVMDIBuilderInsertDeclareBefore(LLVMDIBuilderRef Builder, LLVMValueRef Storage,
1348 LLVMMetadataRef VarInfo, LLVMMetadataRef Expr,
1349 LLVMMetadataRef DL, LLVMValueRef Instr) {
1350 return wrap(unwrap(Builder)->insertDeclare(
1351 unwrap(Storage), unwrap<DILocalVariable>(VarInfo),
1352 unwrap<DIExpression>(Expr), unwrap<DILocation>(DL),
1353 unwrap<Instruction>(Instr)));
1356 LLVMValueRef LLVMDIBuilderInsertDeclareAtEnd(
1357 LLVMDIBuilderRef Builder, LLVMValueRef Storage, LLVMMetadataRef VarInfo,
1358 LLVMMetadataRef Expr, LLVMMetadataRef DL, LLVMBasicBlockRef Block) {
1359 return wrap(unwrap(Builder)->insertDeclare(
1360 unwrap(Storage), unwrap<DILocalVariable>(VarInfo),
1361 unwrap<DIExpression>(Expr), unwrap<DILocation>(DL),
1362 unwrap(Block)));
1365 LLVMValueRef LLVMDIBuilderInsertDbgValueBefore(LLVMDIBuilderRef Builder,
1366 LLVMValueRef Val,
1367 LLVMMetadataRef VarInfo,
1368 LLVMMetadataRef Expr,
1369 LLVMMetadataRef DebugLoc,
1370 LLVMValueRef Instr) {
1371 return wrap(unwrap(Builder)->insertDbgValueIntrinsic(
1372 unwrap(Val), unwrap<DILocalVariable>(VarInfo),
1373 unwrap<DIExpression>(Expr), unwrap<DILocation>(DebugLoc),
1374 unwrap<Instruction>(Instr)));
1377 LLVMValueRef LLVMDIBuilderInsertDbgValueAtEnd(LLVMDIBuilderRef Builder,
1378 LLVMValueRef Val,
1379 LLVMMetadataRef VarInfo,
1380 LLVMMetadataRef Expr,
1381 LLVMMetadataRef DebugLoc,
1382 LLVMBasicBlockRef Block) {
1383 return wrap(unwrap(Builder)->insertDbgValueIntrinsic(
1384 unwrap(Val), unwrap<DILocalVariable>(VarInfo),
1385 unwrap<DIExpression>(Expr), unwrap<DILocation>(DebugLoc),
1386 unwrap(Block)));
1389 LLVMMetadataRef LLVMDIBuilderCreateAutoVariable(
1390 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
1391 size_t NameLen, LLVMMetadataRef File, unsigned LineNo, LLVMMetadataRef Ty,
1392 LLVMBool AlwaysPreserve, LLVMDIFlags Flags, uint32_t AlignInBits) {
1393 return wrap(unwrap(Builder)->createAutoVariable(
1394 unwrap<DIScope>(Scope), {Name, NameLen}, unwrap<DIFile>(File),
1395 LineNo, unwrap<DIType>(Ty), AlwaysPreserve,
1396 map_from_llvmDIFlags(Flags), AlignInBits));
1399 LLVMMetadataRef LLVMDIBuilderCreateParameterVariable(
1400 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
1401 size_t NameLen, unsigned ArgNo, LLVMMetadataRef File, unsigned LineNo,
1402 LLVMMetadataRef Ty, LLVMBool AlwaysPreserve, LLVMDIFlags Flags) {
1403 return wrap(unwrap(Builder)->createParameterVariable(
1404 unwrap<DIScope>(Scope), {Name, NameLen}, ArgNo, unwrap<DIFile>(File),
1405 LineNo, unwrap<DIType>(Ty), AlwaysPreserve,
1406 map_from_llvmDIFlags(Flags)));
1409 LLVMMetadataRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Builder,
1410 int64_t Lo, int64_t Count) {
1411 return wrap(unwrap(Builder)->getOrCreateSubrange(Lo, Count));
1414 LLVMMetadataRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef Builder,
1415 LLVMMetadataRef *Data,
1416 size_t Length) {
1417 Metadata **DataValue = unwrap(Data);
1418 return wrap(unwrap(Builder)->getOrCreateArray({DataValue, Length}).get());
1421 LLVMMetadataRef LLVMGetSubprogram(LLVMValueRef Func) {
1422 return wrap(unwrap<Function>(Func)->getSubprogram());
1425 void LLVMSetSubprogram(LLVMValueRef Func, LLVMMetadataRef SP) {
1426 unwrap<Function>(Func)->setSubprogram(unwrap<DISubprogram>(SP));
1429 unsigned LLVMDISubprogramGetLine(LLVMMetadataRef Subprogram) {
1430 return unwrapDI<DISubprogram>(Subprogram)->getLine();
1433 LLVMMetadataRef LLVMInstructionGetDebugLoc(LLVMValueRef Inst) {
1434 return wrap(unwrap<Instruction>(Inst)->getDebugLoc().getAsMDNode());
1437 void LLVMInstructionSetDebugLoc(LLVMValueRef Inst, LLVMMetadataRef Loc) {
1438 if (Loc)
1439 unwrap<Instruction>(Inst)->setDebugLoc(DebugLoc(unwrap<MDNode>(Loc)));
1440 else
1441 unwrap<Instruction>(Inst)->setDebugLoc(DebugLoc());
1444 LLVMMetadataKind LLVMGetMetadataKind(LLVMMetadataRef Metadata) {
1445 switch(unwrap(Metadata)->getMetadataID()) {
1446 #define HANDLE_METADATA_LEAF(CLASS) \
1447 case Metadata::CLASS##Kind: \
1448 return (LLVMMetadataKind)LLVM##CLASS##MetadataKind;
1449 #include "llvm/IR/Metadata.def"
1450 default:
1451 return (LLVMMetadataKind)LLVMGenericDINodeMetadataKind;