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.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm-c/DebugInfo.h"
15#include "LLVMContextImpl.h"
16#include "llvm/ADT/APSInt.h"
17#include "llvm/ADT/DenseMap.h"
18#include "llvm/ADT/DenseSet.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/DIBuilder.h"
26#include "llvm/IR/DebugInfo.h"
27#include "llvm/IR/DebugInfoMetadata.h"
28#include "llvm/IR/DebugLoc.h"
29#include "llvm/IR/DebugProgramInstruction.h"
30#include "llvm/IR/Function.h"
31#include "llvm/IR/GVMaterializer.h"
32#include "llvm/IR/Instruction.h"
33#include "llvm/IR/IntrinsicInst.h"
34#include "llvm/IR/LLVMContext.h"
35#include "llvm/IR/Metadata.h"
36#include "llvm/IR/Module.h"
37#include "llvm/IR/PassManager.h"
38#include "llvm/Support/Casting.h"
39#include "llvm/Support/TimeProfiler.h"
40#include <algorithm>
41#include <cassert>
42#include <optional>
43
44using namespace llvm;
45using namespace llvm::at;
46using namespace llvm::dwarf;
47
48TinyPtrVector<DbgVariableRecord *> llvm::findDVRDeclares(Value *V) {
49 // This function is hot. Check whether the value has any metadata to avoid a
50 // DenseMap lookup. This check is a bitfield datamember lookup.
51 if (!V->isUsedByMetadata())
52 return {};
53 auto *L = ValueAsMetadata::getIfExists(V);
54 if (!L)
55 return {};
56
57 TinyPtrVector<DbgVariableRecord *> Declares;
58 for (DbgVariableRecord *DVR : L->getAllDbgVariableRecordUsers())
59 if (DVR->getType() == DbgVariableRecord::LocationType::Declare)
60 Declares.push_back(NewVal: DVR);
61
62 return Declares;
63}
64
65TinyPtrVector<DbgVariableRecord *> llvm::findDVRDeclareValues(Value *V) {
66 // This function is hot. Check whether the value has any metadata to avoid a
67 // DenseMap lookup. This check is a bitfield datamember lookup.
68 if (!V->isUsedByMetadata())
69 return {};
70 auto *L = ValueAsMetadata::getIfExists(V);
71 if (!L)
72 return {};
73
74 TinyPtrVector<DbgVariableRecord *> DEclareValues;
75 for (DbgVariableRecord *DVR : L->getAllDbgVariableRecordUsers())
76 if (DVR->getType() == DbgVariableRecord::LocationType::DeclareValue)
77 DEclareValues.push_back(NewVal: DVR);
78
79 return DEclareValues;
80}
81
82TinyPtrVector<DbgVariableRecord *> llvm::findDVRValues(Value *V) {
83 // This function is hot. Check whether the value has any metadata to avoid a
84 // DenseMap lookup. This check is a bitfield datamember lookup.
85 if (!V->isUsedByMetadata())
86 return {};
87 auto *L = ValueAsMetadata::getIfExists(V);
88 if (!L)
89 return {};
90
91 TinyPtrVector<DbgVariableRecord *> Values;
92 for (DbgVariableRecord *DVR : L->getAllDbgVariableRecordUsers())
93 if (DVR->isValueOfVariable())
94 Values.push_back(NewVal: DVR);
95
96 return Values;
97}
98
99template <bool DbgAssignAndValuesOnly>
100static void
101findDbgIntrinsics(Value *V,
102 SmallVectorImpl<DbgVariableRecord *> &DbgVariableRecords) {
103 // This function is hot. Check whether the value has any metadata to avoid a
104 // DenseMap lookup.
105 if (!V->isUsedByMetadata())
106 return;
107
108 // TODO: If this value appears multiple times in a DIArgList, we should still
109 // only add the owning dbg.value once; use this set to track ArgListUsers.
110 // This behaviour can be removed when we can automatically remove duplicates.
111 // V will also appear twice in a dbg.assign if its used in the both the value
112 // and address components.
113 SmallPtrSet<DbgVariableRecord *, 4> EncounteredDbgVariableRecords;
114
115 /// Append users of MetadataAsValue(MD).
116 auto AppendUsers = [&EncounteredDbgVariableRecords,
117 &DbgVariableRecords](Metadata *MD) {
118 // Get DbgVariableRecords that use this as a single value.
119 if (LocalAsMetadata *L = dyn_cast<LocalAsMetadata>(Val: MD)) {
120 for (DbgVariableRecord *DVR : L->getAllDbgVariableRecordUsers()) {
121 if (!DbgAssignAndValuesOnly || DVR->isDbgValue() || DVR->isDbgAssign())
122 if (EncounteredDbgVariableRecords.insert(Ptr: DVR).second)
123 DbgVariableRecords.push_back(Elt: DVR);
124 }
125 }
126 };
127
128 if (auto *L = LocalAsMetadata::getIfExists(Local: V)) {
129 AppendUsers(L);
130 for (Metadata *AL : L->getAllArgListUsers()) {
131 AppendUsers(AL);
132 DIArgList *DI = cast<DIArgList>(Val: AL);
133 for (DbgVariableRecord *DVR : DI->getAllDbgVariableRecordUsers())
134 if (!DbgAssignAndValuesOnly || DVR->isDbgValue() || DVR->isDbgAssign())
135 if (EncounteredDbgVariableRecords.insert(Ptr: DVR).second)
136 DbgVariableRecords.push_back(Elt: DVR);
137 }
138 }
139}
140
141void llvm::findDbgValues(
142 Value *V, SmallVectorImpl<DbgVariableRecord *> &DbgVariableRecords) {
143 findDbgIntrinsics</*DbgAssignAndValuesOnly=*/true>(V, DbgVariableRecords);
144}
145
146void llvm::findDbgUsers(
147 Value *V, SmallVectorImpl<DbgVariableRecord *> &DbgVariableRecords) {
148 findDbgIntrinsics</*DbgAssignAndValuesOnly=*/false>(V, DbgVariableRecords);
149}
150
151DISubprogram *llvm::getDISubprogram(const MDNode *Scope) {
152 if (auto *LocalScope = dyn_cast_or_null<DILocalScope>(Val: Scope))
153 return LocalScope->getSubprogram();
154 return nullptr;
155}
156
157DebugLoc llvm::getDebugValueLoc(DbgVariableRecord *DVR) {
158 // Original dbg.declare must have a location.
159 const DebugLoc &DeclareLoc = DVR->getDebugLoc();
160 MDNode *Scope = DeclareLoc.getScope();
161 DILocation *InlinedAt = DeclareLoc.getInlinedAt();
162 // Because no machine insts can come from debug intrinsics, only the scope
163 // and inlinedAt is significant. Zero line numbers are used in case this
164 // DebugLoc leaks into any adjacent instructions. Produce an unknown location
165 // with the correct scope / inlinedAt fields.
166 return DILocation::get(Context&: DVR->getContext(), Line: 0, Column: 0, Scope, InlinedAt);
167}
168
169//===----------------------------------------------------------------------===//
170// DebugInfoFinder implementations.
171//===----------------------------------------------------------------------===//
172
173void DebugInfoFinder::reset() {
174 CUs.clear();
175 SPs.clear();
176 GVs.clear();
177 TYs.clear();
178 Scopes.clear();
179 Macros.clear();
180 NodesSeen.clear();
181}
182
183void DebugInfoFinder::processModule(const Module &M) {
184 for (auto *CU : M.debug_compile_units())
185 processCompileUnit(CU);
186 for (auto &F : M.functions()) {
187 if (auto *SP = cast_or_null<DISubprogram>(Val: F.getSubprogram()))
188 processSubprogram(SP);
189 // There could be subprograms from inlined functions referenced from
190 // instructions only. Walk the function to find them.
191 for (const BasicBlock &BB : F)
192 for (const Instruction &I : BB)
193 processInstruction(M, I);
194 }
195}
196
197void DebugInfoFinder::processCompileUnit(DICompileUnit *CU) {
198 if (!addCompileUnit(CU))
199 return;
200 for (auto *DIG : CU->getGlobalVariables()) {
201 if (!addGlobalVariable(DIG))
202 continue;
203 auto *GV = DIG->getVariable();
204 processScope(Scope: GV->getScope());
205 processType(DT: GV->getType());
206 }
207 for (auto *ET : CU->getEnumTypes())
208 processType(DT: ET);
209 for (auto *RT : CU->getRetainedTypes())
210 if (auto *T = dyn_cast<DIType>(Val: RT))
211 processType(DT: T);
212 else
213 processSubprogram(SP: cast<DISubprogram>(Val: RT));
214 for (auto *Import : CU->getImportedEntities())
215 processImportedEntity(Import);
216 for (auto *Macro : CU->getMacros())
217 processMacroNode(Macro, CurrentMacroFile: nullptr);
218}
219
220void DebugInfoFinder::processInstruction(const Module &M,
221 const Instruction &I) {
222 if (auto *DVI = dyn_cast<DbgVariableIntrinsic>(Val: &I))
223 processVariable(DVI: DVI->getVariable());
224
225 if (auto DbgLoc = I.getDebugLoc())
226 processLocation(M, Loc: DbgLoc.get());
227
228 for (const DbgRecord &DPR : I.getDbgRecordRange())
229 processDbgRecord(M, DR: DPR);
230}
231
232void DebugInfoFinder::processLocation(const Module &M, const DILocation *Loc) {
233 if (!Loc)
234 return;
235 processScope(Scope: Loc->getScope());
236 processLocation(M, Loc: Loc->getInlinedAt());
237}
238
239void DebugInfoFinder::processDbgRecord(const Module &M, const DbgRecord &DR) {
240 if (const DbgVariableRecord *DVR = dyn_cast<const DbgVariableRecord>(Val: &DR))
241 processVariable(DVI: DVR->getVariable());
242 processLocation(M, Loc: DR.getDebugLoc().get());
243}
244
245void DebugInfoFinder::processVariable(DIVariable *DV) {
246 if (auto *DLV = dyn_cast_or_null<DILocalVariable>(Val: DV))
247 processVariable(DVI: DLV);
248}
249
250void DebugInfoFinder::processType(DIType *DT) {
251 if (!addType(DT))
252 return;
253 processScope(Scope: DT->getScope());
254 if (auto *ST = dyn_cast<DISubroutineType>(Val: DT)) {
255 for (DIType *Ref : ST->getTypeArray())
256 processType(DT: Ref);
257 return;
258 }
259 if (auto *DCT = dyn_cast<DICompositeType>(Val: DT)) {
260 processType(DT: DCT->getBaseType());
261 processType(DT: DCT->getVTableHolder());
262 processType(DT: DCT->getDiscriminator());
263 processType(DT: DCT->getSpecification());
264 processVariable(DV: DCT->getDataLocation());
265 processVariable(DV: DCT->getAssociated());
266 processVariable(DV: DCT->getAllocated());
267 for (Metadata *D : DCT->getElements()) {
268 if (auto *T = dyn_cast<DIType>(Val: D))
269 processType(DT: T);
270 else if (auto *SP = dyn_cast<DISubprogram>(Val: D))
271 processSubprogram(SP);
272 else if (auto *SR = dyn_cast_or_null<DISubrange>(Val: D)) {
273 auto VisitBound = [&](DISubrange::BoundType Bound) {
274 if (auto *BV = dyn_cast_if_present<DIVariable *>(Val&: Bound))
275 processVariable(DV: BV);
276 };
277 VisitBound(SR->getLowerBound());
278 VisitBound(SR->getCount());
279 VisitBound(SR->getUpperBound());
280 VisitBound(SR->getStride());
281 } else if (auto *GSR = dyn_cast_or_null<DIGenericSubrange>(Val: D)) {
282 auto VisitBound = [&](DIGenericSubrange::BoundType Bound) {
283 if (auto *BV = dyn_cast_if_present<DIVariable *>(Val&: Bound))
284 processVariable(DV: BV);
285 };
286 VisitBound(GSR->getLowerBound());
287 VisitBound(GSR->getCount());
288 VisitBound(GSR->getUpperBound());
289 VisitBound(GSR->getStride());
290 }
291 }
292 return;
293 }
294 if (auto *ST = dyn_cast<DIStringType>(Val: DT)) {
295 processVariable(DV: ST->getStringLength());
296 return;
297 }
298 if (auto *SRT = dyn_cast<DISubrangeType>(Val: DT)) {
299 processType(DT: SRT->getBaseType());
300 auto VisitBound = [&](DISubrangeType::BoundType Bound) {
301 if (auto *V = dyn_cast_if_present<DIVariable *>(Val&: Bound))
302 processVariable(DV: V);
303 else if (auto *T = dyn_cast_if_present<DIDerivedType *>(Val&: Bound))
304 processType(DT: T);
305 };
306 VisitBound(SRT->getLowerBound());
307 VisitBound(SRT->getUpperBound());
308 VisitBound(SRT->getStride());
309 VisitBound(SRT->getBias());
310 return;
311 }
312 if (auto *DDT = dyn_cast<DIDerivedType>(Val: DT)) {
313 processType(DT: DDT->getBaseType());
314 }
315}
316
317void DebugInfoFinder::processImportedEntity(const DIImportedEntity *Import) {
318 auto *Entity = Import->getEntity();
319 if (auto *T = dyn_cast<DIType>(Val: Entity))
320 processType(DT: T);
321 else if (auto *SP = dyn_cast<DISubprogram>(Val: Entity))
322 processSubprogram(SP);
323 else if (auto *NS = dyn_cast<DINamespace>(Val: Entity))
324 processScope(Scope: NS->getScope());
325 else if (auto *M = dyn_cast<DIModule>(Val: Entity))
326 processScope(Scope: M->getScope());
327}
328
329/// Process a macro debug info node (DIMacroNode).
330///
331/// A DIMacroNode is one of two types:
332/// - DIMacro: A single macro definition. Add it to the Macros list along with
333/// its containing DIMacroFile.
334/// - DIMacroFile: A file containing macros. Recursively process all nested
335/// macro nodes within it (avoiding duplicates by tracking visited nodes).
336void DebugInfoFinder::processMacroNode(DIMacroNode *Macro,
337 DIMacroFile *CurrentMacroFile) {
338 if (!Macro)
339 return;
340
341 if (auto *M = dyn_cast<DIMacro>(Val: Macro)) {
342 addMacro(Macro: M, MacroFile: CurrentMacroFile);
343 return;
344 }
345
346 auto *MF = dyn_cast<DIMacroFile>(Val: Macro);
347 assert(MF &&
348 "Expected a DIMacroFile (it can't be any other type at this point)");
349
350 // Check if we've already seen this macro file to avoid infinite recursion
351 if (!NodesSeen.insert(Ptr: MF).second)
352 return;
353
354 // Recursively process nested macros in the macro file
355 for (auto *Element : MF->getElements())
356 processMacroNode(Macro: Element, CurrentMacroFile: MF);
357}
358
359void DebugInfoFinder::processScope(DIScope *Scope) {
360 if (!Scope)
361 return;
362 if (auto *Ty = dyn_cast<DIType>(Val: Scope)) {
363 processType(DT: Ty);
364 return;
365 }
366 if (auto *CU = dyn_cast<DICompileUnit>(Val: Scope)) {
367 addCompileUnit(CU);
368 return;
369 }
370 if (auto *SP = dyn_cast<DISubprogram>(Val: Scope)) {
371 processSubprogram(SP);
372 return;
373 }
374 if (!addScope(Scope))
375 return;
376 if (auto *LB = dyn_cast<DILexicalBlockBase>(Val: Scope)) {
377 processScope(Scope: LB->getScope());
378 } else if (auto *NS = dyn_cast<DINamespace>(Val: Scope)) {
379 processScope(Scope: NS->getScope());
380 } else if (auto *M = dyn_cast<DIModule>(Val: Scope)) {
381 processScope(Scope: M->getScope());
382 }
383}
384
385void DebugInfoFinder::processSubprogram(DISubprogram *SP) {
386 if (!addSubprogram(SP))
387 return;
388 processScope(Scope: SP->getScope());
389 // Some of the users, e.g. CloneFunctionInto / CloneModule, need to set up a
390 // ValueMap containing identity mappings for all of the DICompileUnit's, not
391 // just DISubprogram's, referenced from anywhere within the Function being
392 // cloned prior to calling MapMetadata / RemapInstruction to avoid their
393 // duplication later as DICompileUnit's are also directly referenced by
394 // llvm.dbg.cu list. Therefore we need to collect DICompileUnit's here as
395 // well. Also, DICompileUnit's may reference DISubprogram's too and therefore
396 // need to be at least looked through.
397 processCompileUnit(CU: SP->getUnit());
398 processType(DT: SP->getType());
399 for (auto *Element : SP->getTemplateParams()) {
400 if (auto *TType = dyn_cast<DITemplateTypeParameter>(Val: Element)) {
401 processType(DT: TType->getType());
402 } else if (auto *TVal = dyn_cast<DITemplateValueParameter>(Val: Element)) {
403 processType(DT: TVal->getType());
404 }
405 }
406
407 SP->forEachRetainedNode(
408 FuncLV: [this](DILocalVariable *LV) { processVariable(DVI: LV); }, FuncLabel: [](DILabel *L) {},
409 FuncIE: [this](DIImportedEntity *IE) { processImportedEntity(Import: IE); },
410 FuncType: [this](DIType *T) { processType(DT: T); });
411}
412
413void DebugInfoFinder::processVariable(const DILocalVariable *DV) {
414 if (!NodesSeen.insert(Ptr: DV).second)
415 return;
416 processScope(Scope: DV->getScope());
417 processType(DT: DV->getType());
418}
419
420bool DebugInfoFinder::addType(DIType *DT) {
421 if (!DT)
422 return false;
423
424 if (!NodesSeen.insert(Ptr: DT).second)
425 return false;
426
427 TYs.push_back(Elt: DT);
428 return true;
429}
430
431bool DebugInfoFinder::addCompileUnit(DICompileUnit *CU) {
432 if (!CU)
433 return false;
434 if (!NodesSeen.insert(Ptr: CU).second)
435 return false;
436
437 CUs.push_back(Elt: CU);
438 return true;
439}
440
441bool DebugInfoFinder::addGlobalVariable(DIGlobalVariableExpression *DIG) {
442 if (!NodesSeen.insert(Ptr: DIG).second)
443 return false;
444
445 GVs.push_back(Elt: DIG);
446 return true;
447}
448
449bool DebugInfoFinder::addSubprogram(DISubprogram *SP) {
450 if (!SP)
451 return false;
452
453 if (!NodesSeen.insert(Ptr: SP).second)
454 return false;
455
456 SPs.push_back(Elt: SP);
457 return true;
458}
459
460bool DebugInfoFinder::addScope(DIScope *Scope) {
461 if (!Scope)
462 return false;
463 // FIXME: Ocaml binding generates a scope with no content, we treat it
464 // as null for now.
465 if (Scope->getNumOperands() == 0)
466 return false;
467 if (!NodesSeen.insert(Ptr: Scope).second)
468 return false;
469 Scopes.push_back(Elt: Scope);
470 return true;
471}
472
473bool DebugInfoFinder::addMacro(DIMacro *Macro, DIMacroFile *MacroFile) {
474 if (!Macro)
475 return false;
476
477 if (!NodesSeen.insert(Ptr: Macro).second)
478 return false;
479
480 Macros.push_back(Elt: std::make_pair(x&: Macro, y&: MacroFile));
481 return true;
482}
483
484/// Recursively handle DILocations in followup metadata etc.
485///
486/// TODO: If for example a followup loop metadata would reference itself this
487/// function would go into infinite recursion. We do not expect such cycles in
488/// the loop metadata (except for the self-referencing first element
489/// "LoopID"). However, we could at least handle such situations more gracefully
490/// somehow (e.g. by keeping track of visited nodes and dropping metadata).
491static Metadata *updateLoopMetadataDebugLocationsRecursive(
492 Metadata *MetadataIn, function_ref<Metadata *(Metadata *)> Updater) {
493 const MDTuple *M = dyn_cast_or_null<MDTuple>(Val: MetadataIn);
494 // The loop metadata options should start with a MDString.
495 if (!M || M->getNumOperands() < 1 || !isa<MDString>(Val: M->getOperand(I: 0)))
496 return MetadataIn;
497
498 bool Updated = false;
499 SmallVector<Metadata *, 4> MDs{M->getOperand(I: 0)};
500 for (Metadata *MD : llvm::drop_begin(RangeOrContainer: M->operands())) {
501 if (!MD) {
502 MDs.push_back(Elt: nullptr);
503 continue;
504 }
505 Metadata *NewMD =
506 Updater(updateLoopMetadataDebugLocationsRecursive(MetadataIn: MD, Updater));
507 if (NewMD)
508 MDs.push_back(Elt: NewMD);
509 Updated |= NewMD != MD;
510 }
511
512 assert(!M->isDistinct() && "M should not be distinct.");
513 return Updated ? MDNode::get(Context&: M->getContext(), MDs) : MetadataIn;
514}
515
516static MDNode *updateLoopMetadataDebugLocationsImpl(
517 MDNode *OrigLoopID, function_ref<Metadata *(Metadata *)> Updater) {
518 assert(OrigLoopID && OrigLoopID->getNumOperands() > 0 &&
519 "Loop ID needs at least one operand");
520 assert(OrigLoopID && OrigLoopID->getOperand(0).get() == OrigLoopID &&
521 "Loop ID should refer to itself");
522
523 // Save space for the self-referential LoopID.
524 SmallVector<Metadata *, 4> MDs = {nullptr};
525
526 for (Metadata *MD : llvm::drop_begin(RangeOrContainer: OrigLoopID->operands())) {
527 if (!MD)
528 MDs.push_back(Elt: nullptr);
529 else if (Metadata *NewMD = Updater(
530 updateLoopMetadataDebugLocationsRecursive(MetadataIn: MD, Updater)))
531 MDs.push_back(Elt: NewMD);
532 }
533
534 MDNode *NewLoopID = MDNode::getDistinct(Context&: OrigLoopID->getContext(), MDs);
535 // Insert the self-referential LoopID.
536 NewLoopID->replaceOperandWith(I: 0, New: NewLoopID);
537 return NewLoopID;
538}
539
540void llvm::updateLoopMetadataDebugLocations(
541 Instruction &I, function_ref<Metadata *(Metadata *)> Updater) {
542 MDNode *OrigLoopID = I.getMetadata(KindID: LLVMContext::MD_loop);
543 if (!OrigLoopID)
544 return;
545 MDNode *NewLoopID = updateLoopMetadataDebugLocationsImpl(OrigLoopID, Updater);
546 I.setMetadata(KindID: LLVMContext::MD_loop, Node: NewLoopID);
547}
548
549/// Return true if a node is a DILocation or if a DILocation is
550/// indirectly referenced by one of the node's children.
551static bool isDILocationReachable(SmallPtrSetImpl<Metadata *> &Visited,
552 SmallPtrSetImpl<Metadata *> &Reachable,
553 Metadata *MD) {
554 MDNode *N = dyn_cast_or_null<MDNode>(Val: MD);
555 if (!N)
556 return false;
557 if (isa<DILocation>(Val: N) || Reachable.count(Ptr: N))
558 return true;
559 if (!Visited.insert(Ptr: N).second)
560 return false;
561 for (auto &OpIt : N->operands()) {
562 Metadata *Op = OpIt.get();
563 if (isDILocationReachable(Visited, Reachable, MD: Op)) {
564 // Don't return just yet as we want to visit all MD's children to
565 // initialize DILocationReachable in stripDebugLocFromLoopID
566 Reachable.insert(Ptr: N);
567 }
568 }
569 return Reachable.count(Ptr: N);
570}
571
572static bool isAllDILocation(SmallPtrSetImpl<Metadata *> &Visited,
573 SmallPtrSetImpl<Metadata *> &AllDILocation,
574 const SmallPtrSetImpl<Metadata *> &DIReachable,
575 Metadata *MD) {
576 MDNode *N = dyn_cast_or_null<MDNode>(Val: MD);
577 if (!N)
578 return false;
579 if (isa<DILocation>(Val: N) || AllDILocation.count(Ptr: N))
580 return true;
581 if (!DIReachable.count(Ptr: N))
582 return false;
583 if (!Visited.insert(Ptr: N).second)
584 return false;
585 for (auto &OpIt : N->operands()) {
586 Metadata *Op = OpIt.get();
587 if (Op == MD)
588 continue;
589 if (!isAllDILocation(Visited, AllDILocation, DIReachable, MD: Op)) {
590 return false;
591 }
592 }
593 AllDILocation.insert(Ptr: N);
594 return true;
595}
596
597static Metadata *
598stripLoopMDLoc(const SmallPtrSetImpl<Metadata *> &AllDILocation,
599 const SmallPtrSetImpl<Metadata *> &DIReachable, Metadata *MD) {
600 if (isa<DILocation>(Val: MD) || AllDILocation.count(Ptr: MD))
601 return nullptr;
602
603 if (!DIReachable.count(Ptr: MD))
604 return MD;
605
606 MDNode *N = dyn_cast_or_null<MDNode>(Val: MD);
607 if (!N)
608 return MD;
609
610 SmallVector<Metadata *, 4> Args;
611 bool HasSelfRef = false;
612 for (unsigned i = 0; i < N->getNumOperands(); ++i) {
613 Metadata *A = N->getOperand(I: i);
614 if (!A) {
615 Args.push_back(Elt: nullptr);
616 } else if (A == MD) {
617 assert(i == 0 && "expected i==0 for self-reference");
618 HasSelfRef = true;
619 Args.push_back(Elt: nullptr);
620 } else if (Metadata *NewArg =
621 stripLoopMDLoc(AllDILocation, DIReachable, MD: A)) {
622 Args.push_back(Elt: NewArg);
623 }
624 }
625 if (Args.empty() || (HasSelfRef && Args.size() == 1))
626 return nullptr;
627
628 MDNode *NewMD = N->isDistinct() ? MDNode::getDistinct(Context&: N->getContext(), MDs: Args)
629 : MDNode::get(Context&: N->getContext(), MDs: Args);
630 if (HasSelfRef)
631 NewMD->replaceOperandWith(I: 0, New: NewMD);
632 return NewMD;
633}
634
635static MDNode *stripDebugLocFromLoopID(MDNode *N) {
636 assert(!N->operands().empty() && "Missing self reference?");
637 SmallPtrSet<Metadata *, 8> Visited, DILocationReachable, AllDILocation;
638 // If we already visited N, there is nothing to do.
639 if (!Visited.insert(Ptr: N).second)
640 return N;
641
642 // If there is no debug location, we do not have to rewrite this
643 // MDNode. This loop also initializes DILocationReachable, later
644 // needed by updateLoopMetadataDebugLocationsImpl; the use of
645 // count_if avoids an early exit.
646 if (!llvm::count_if(Range: llvm::drop_begin(RangeOrContainer: N->operands()),
647 P: [&Visited, &DILocationReachable](const MDOperand &Op) {
648 return isDILocationReachable(
649 Visited, Reachable&: DILocationReachable, MD: Op.get());
650 }))
651 return N;
652
653 Visited.clear();
654 // If there is only the debug location without any actual loop metadata, we
655 // can remove the metadata.
656 if (llvm::all_of(Range: llvm::drop_begin(RangeOrContainer: N->operands()),
657 P: [&Visited, &AllDILocation,
658 &DILocationReachable](const MDOperand &Op) {
659 return isAllDILocation(Visited, AllDILocation,
660 DIReachable: DILocationReachable, MD: Op.get());
661 }))
662 return nullptr;
663
664 return updateLoopMetadataDebugLocationsImpl(
665 OrigLoopID: N, Updater: [&AllDILocation, &DILocationReachable](Metadata *MD) -> Metadata * {
666 return stripLoopMDLoc(AllDILocation, DIReachable: DILocationReachable, MD);
667 });
668}
669
670bool llvm::stripDebugInfo(Function &F) {
671 bool Changed = false;
672 if (F.hasMetadata(KindID: LLVMContext::MD_dbg)) {
673 Changed = true;
674 F.setSubprogram(nullptr);
675 }
676
677 DenseMap<MDNode *, MDNode *> LoopIDsMap;
678 for (BasicBlock &BB : F) {
679 for (Instruction &I : llvm::make_early_inc_range(Range&: BB)) {
680 if (I.getDebugLoc()) {
681 Changed = true;
682 I.setDebugLoc(DebugLoc());
683 }
684 if (auto *LoopID = I.getMetadata(KindID: LLVMContext::MD_loop)) {
685 auto *NewLoopID = LoopIDsMap.lookup(Val: LoopID);
686 if (!NewLoopID)
687 NewLoopID = LoopIDsMap[LoopID] = stripDebugLocFromLoopID(N: LoopID);
688 if (NewLoopID != LoopID)
689 I.setMetadata(KindID: LLVMContext::MD_loop, Node: NewLoopID);
690 }
691 // Strip other attachments that are or use debug info.
692 if (I.hasMetadataOtherThanDebugLoc()) {
693 // Heapallocsites point into the DIType system.
694 I.setMetadata(Kind: "heapallocsite", Node: nullptr);
695 // DIAssignID are debug info metadata primitives.
696 I.setMetadata(KindID: LLVMContext::MD_DIAssignID, Node: nullptr);
697 }
698 I.dropDbgRecords();
699 }
700 }
701 return Changed;
702}
703
704bool llvm::StripDebugInfo(Module &M) {
705 llvm::TimeTraceScope timeScope("Strip debug info");
706 bool Changed = false;
707
708 for (NamedMDNode &NMD : llvm::make_early_inc_range(Range: M.named_metadata())) {
709 // We're stripping debug info, and without them, coverage information
710 // doesn't quite make sense.
711 if (NMD.getName().starts_with(Prefix: "llvm.dbg.") ||
712 NMD.getName() == "llvm.gcov") {
713 NMD.eraseFromParent();
714 Changed = true;
715 }
716 }
717
718 for (Function &F : M)
719 Changed |= stripDebugInfo(F);
720
721 for (auto &GV : M.globals()) {
722 Changed |= GV.eraseMetadata(KindID: LLVMContext::MD_dbg);
723 }
724
725 if (GVMaterializer *Materializer = M.getMaterializer())
726 Materializer->setStripDebugInfo();
727
728 return Changed;
729}
730
731namespace {
732
733/// Helper class to downgrade -g metadata to -gline-tables-only metadata.
734class DebugTypeInfoRemoval {
735 DenseMap<Metadata *, Metadata *> Replacements;
736
737public:
738 /// The (void)() type.
739 MDNode *EmptySubroutineType;
740
741private:
742 /// Remember what linkage name we originally had before stripping. If we end
743 /// up making two subprograms identical who originally had different linkage
744 /// names, then we need to make one of them distinct, to avoid them getting
745 /// uniqued. Maps the new node to the old linkage name.
746 DenseMap<DISubprogram *, StringRef> NewToLinkageName;
747
748 // TODO: Remember the distinct subprogram we created for a given linkage name,
749 // so that we can continue to unique whenever possible. Map <newly created
750 // node, old linkage name> to the first (possibly distinct) mdsubprogram
751 // created for that combination. This is not strictly needed for correctness,
752 // but can cut down on the number of MDNodes and let us diff cleanly with the
753 // output of -gline-tables-only.
754
755public:
756 DebugTypeInfoRemoval(LLVMContext &C)
757 : EmptySubroutineType(DISubroutineType::get(Context&: C, Flags: DINode::FlagZero, CC: 0,
758 TypeArray: MDNode::get(Context&: C, MDs: {}))) {}
759
760 Metadata *map(Metadata *M) {
761 if (!M)
762 return nullptr;
763 auto Replacement = Replacements.find(Val: M);
764 if (Replacement != Replacements.end())
765 return Replacement->second;
766
767 return M;
768 }
769 MDNode *mapNode(Metadata *N) { return dyn_cast_or_null<MDNode>(Val: map(M: N)); }
770
771 /// Recursively remap N and all its referenced children. Does a DF post-order
772 /// traversal, so as to remap bottoms up.
773 void traverseAndRemap(MDNode *N) { traverse(N); }
774
775private:
776 // Create a new DISubprogram, to replace the one given.
777 DISubprogram *getReplacementSubprogram(DISubprogram *MDS) {
778 auto *FileAndScope = cast_or_null<DIFile>(Val: map(M: MDS->getFile()));
779 StringRef LinkageName = MDS->getName().empty() ? MDS->getLinkageName() : "";
780 DISubprogram *Declaration = nullptr;
781 auto *Type = cast_or_null<DISubroutineType>(Val: map(M: MDS->getType()));
782 DIType *ContainingType =
783 cast_or_null<DIType>(Val: map(M: MDS->getContainingType()));
784 auto *Unit = cast_or_null<DICompileUnit>(Val: map(M: MDS->getUnit()));
785 auto Variables = nullptr;
786 auto TemplateParams = nullptr;
787
788 // Make a distinct DISubprogram, for situations that warrant it.
789 auto distinctMDSubprogram = [&]() {
790 return DISubprogram::getDistinct(
791 Context&: MDS->getContext(), Scope: FileAndScope, Name: MDS->getName(), LinkageName,
792 File: FileAndScope, Line: MDS->getLine(), Type, ScopeLine: MDS->getScopeLine(),
793 ContainingType, VirtualIndex: MDS->getVirtualIndex(), ThisAdjustment: MDS->getThisAdjustment(),
794 Flags: MDS->getFlags(), SPFlags: MDS->getSPFlags(), Unit, TemplateParams, Declaration,
795 RetainedNodes: Variables);
796 };
797
798 if (MDS->isDistinct())
799 return distinctMDSubprogram();
800
801 auto *NewMDS = DISubprogram::get(
802 Context&: MDS->getContext(), Scope: FileAndScope, Name: MDS->getName(), LinkageName,
803 File: FileAndScope, Line: MDS->getLine(), Type, ScopeLine: MDS->getScopeLine(), ContainingType,
804 VirtualIndex: MDS->getVirtualIndex(), ThisAdjustment: MDS->getThisAdjustment(), Flags: MDS->getFlags(),
805 SPFlags: MDS->getSPFlags(), Unit, TemplateParams, Declaration, RetainedNodes: Variables);
806
807 StringRef OldLinkageName = MDS->getLinkageName();
808
809 // See if we need to make a distinct one.
810 auto OrigLinkage = NewToLinkageName.find(Val: NewMDS);
811 if (OrigLinkage != NewToLinkageName.end()) {
812 if (OrigLinkage->second == OldLinkageName)
813 // We're good.
814 return NewMDS;
815
816 // Otherwise, need to make a distinct one.
817 // TODO: Query the map to see if we already have one.
818 return distinctMDSubprogram();
819 }
820
821 NewToLinkageName.insert(KV: {NewMDS, MDS->getLinkageName()});
822 return NewMDS;
823 }
824
825 /// Create a new compile unit, to replace the one given
826 DICompileUnit *getReplacementCU(DICompileUnit *CU) {
827 // Drop skeleton CUs.
828 if (CU->getDWOId())
829 return nullptr;
830
831 auto *File = cast_or_null<DIFile>(Val: map(M: CU->getFile()));
832 MDTuple *EnumTypes = nullptr;
833 MDTuple *RetainedTypes = nullptr;
834 MDTuple *GlobalVariables = nullptr;
835 MDTuple *ImportedEntities = nullptr;
836 return DICompileUnit::getDistinct(
837 Context&: CU->getContext(), SourceLanguage: CU->getSourceLanguage(), File, Producer: CU->getProducer(),
838 IsOptimized: CU->isOptimized(), Flags: CU->getFlags(), RuntimeVersion: CU->getRuntimeVersion(),
839 SplitDebugFilename: CU->getSplitDebugFilename(), EmissionKind: DICompileUnit::LineTablesOnly, EnumTypes,
840 RetainedTypes, GlobalVariables, ImportedEntities, Macros: CU->getMacros(),
841 DWOId: CU->getDWOId(), SplitDebugInlining: CU->getSplitDebugInlining(),
842 DebugInfoForProfiling: CU->getDebugInfoForProfiling(), NameTableKind: CU->getNameTableKind(),
843 RangesBaseAddress: CU->getRangesBaseAddress(), SysRoot: CU->getSysRoot(), SDK: CU->getSDK());
844 }
845
846 DILocation *getReplacementMDLocation(DILocation *MLD) {
847 auto *Scope = map(M: MLD->getScope());
848 auto *InlinedAt = map(M: MLD->getInlinedAt());
849 if (MLD->isDistinct())
850 return DILocation::getDistinct(Context&: MLD->getContext(), Line: MLD->getLine(),
851 Column: MLD->getColumn(), Scope, InlinedAt);
852 return DILocation::get(Context&: MLD->getContext(), Line: MLD->getLine(), Column: MLD->getColumn(),
853 Scope, InlinedAt);
854 }
855
856 /// Create a new generic MDNode, to replace the one given
857 MDNode *getReplacementMDNode(MDNode *N) {
858 SmallVector<Metadata *, 8> Ops;
859 Ops.reserve(N: N->getNumOperands());
860 for (auto &I : N->operands())
861 if (I)
862 Ops.push_back(Elt: map(M: I));
863 auto *Ret = MDNode::get(Context&: N->getContext(), MDs: Ops);
864 return Ret;
865 }
866
867 /// Attempt to re-map N to a newly created node.
868 void remap(MDNode *N) {
869 if (Replacements.count(Val: N))
870 return;
871
872 auto doRemap = [&](MDNode *N) -> MDNode * {
873 if (!N)
874 return nullptr;
875 if (auto *MDSub = dyn_cast<DISubprogram>(Val: N)) {
876 remap(N: MDSub->getUnit());
877 return getReplacementSubprogram(MDS: MDSub);
878 }
879 if (isa<DISubroutineType>(Val: N))
880 return EmptySubroutineType;
881 if (auto *CU = dyn_cast<DICompileUnit>(Val: N))
882 return getReplacementCU(CU);
883 if (isa<DIFile>(Val: N))
884 return N;
885 if (auto *MDLB = dyn_cast<DILexicalBlockBase>(Val: N))
886 // Remap to our referenced scope (recursively).
887 return mapNode(N: MDLB->getScope());
888 if (auto *MLD = dyn_cast<DILocation>(Val: N))
889 return getReplacementMDLocation(MLD);
890
891 // Otherwise, if we see these, just drop them now. Not strictly necessary,
892 // but this speeds things up a little.
893 if (isa<DINode>(Val: N))
894 return nullptr;
895
896 return getReplacementMDNode(N);
897 };
898 // Separate recursive doRemap and operator [] into 2 lines to avoid
899 // out-of-order evaluations since both of them can access the same memory
900 // location in map Replacements.
901 auto Value = doRemap(N);
902 Replacements[N] = Value;
903 }
904
905 /// Do the remapping traversal.
906 void traverse(MDNode *);
907};
908
909} // end anonymous namespace
910
911void DebugTypeInfoRemoval::traverse(MDNode *N) {
912 if (!N || Replacements.count(Val: N))
913 return;
914
915 // To avoid cycles, as well as for efficiency sake, we will sometimes prune
916 // parts of the graph.
917 auto prune = [](MDNode *Parent, MDNode *Child) {
918 if (auto *MDS = dyn_cast<DISubprogram>(Val: Parent))
919 return Child == MDS->getRetainedNodes().get();
920 return false;
921 };
922
923 SmallVector<MDNode *, 16> ToVisit;
924 DenseSet<MDNode *> Opened;
925
926 // Visit each node starting at N in post order, and map them.
927 ToVisit.push_back(Elt: N);
928 while (!ToVisit.empty()) {
929 auto *N = ToVisit.back();
930 if (!Opened.insert(V: N).second) {
931 // Close it.
932 remap(N);
933 ToVisit.pop_back();
934 continue;
935 }
936 for (auto &I : N->operands())
937 if (auto *MDN = dyn_cast_or_null<MDNode>(Val: I))
938 if (!Opened.count(V: MDN) && !Replacements.count(Val: MDN) && !prune(N, MDN) &&
939 !isa<DICompileUnit>(Val: MDN))
940 ToVisit.push_back(Elt: MDN);
941 }
942}
943
944bool llvm::stripNonLineTableDebugInfo(Module &M) {
945 bool Changed = false;
946
947 // Delete non-CU debug info named metadata nodes.
948 for (auto NMI = M.named_metadata_begin(), NME = M.named_metadata_end();
949 NMI != NME;) {
950 NamedMDNode *NMD = &*NMI;
951 ++NMI;
952 // Specifically keep dbg.cu around.
953 if (NMD->getName() == "llvm.dbg.cu")
954 continue;
955 }
956
957 // Drop all dbg attachments from global variables.
958 for (auto &GV : M.globals())
959 GV.eraseMetadata(KindID: LLVMContext::MD_dbg);
960
961 DebugTypeInfoRemoval Mapper(M.getContext());
962 auto remap = [&](MDNode *Node) -> MDNode * {
963 if (!Node)
964 return nullptr;
965 Mapper.traverseAndRemap(N: Node);
966 auto *NewNode = Mapper.mapNode(N: Node);
967 Changed |= Node != NewNode;
968 Node = NewNode;
969 return NewNode;
970 };
971
972 // Rewrite the DebugLocs to be equivalent to what
973 // -gline-tables-only would have created.
974 for (auto &F : M) {
975 if (auto *SP = F.getSubprogram()) {
976 Mapper.traverseAndRemap(N: SP);
977 auto *NewSP = cast<DISubprogram>(Val: Mapper.mapNode(N: SP));
978 Changed |= SP != NewSP;
979 F.setSubprogram(NewSP);
980 }
981 for (auto &BB : F) {
982 for (auto &I : BB) {
983 auto remapDebugLoc = [&](const DebugLoc &DL) -> DebugLoc {
984 auto *Scope = DL.getScope();
985 MDNode *InlinedAt = DL.getInlinedAt();
986 Scope = remap(Scope);
987 InlinedAt = remap(InlinedAt);
988 return DILocation::get(Context&: M.getContext(), Line: DL.getLine(), Column: DL.getCol(),
989 Scope, InlinedAt);
990 };
991
992 if (I.getDebugLoc() != DebugLoc())
993 I.setDebugLoc(remapDebugLoc(I.getDebugLoc()));
994
995 // Remap DILocations in llvm.loop attachments.
996 updateLoopMetadataDebugLocations(I, Updater: [&](Metadata *MD) -> Metadata * {
997 if (auto *Loc = dyn_cast_or_null<DILocation>(Val: MD))
998 return remapDebugLoc(Loc).get();
999 return MD;
1000 });
1001
1002 // Strip heapallocsite attachments, they point into the DIType system.
1003 if (I.hasMetadataOtherThanDebugLoc())
1004 I.setMetadata(Kind: "heapallocsite", Node: nullptr);
1005
1006 // Strip any DbgRecords attached.
1007 I.dropDbgRecords();
1008 }
1009 }
1010 }
1011
1012 // Create a new llvm.dbg.cu, which is equivalent to the one
1013 // -gline-tables-only would have created.
1014 for (auto &NMD : M.named_metadata()) {
1015 SmallVector<MDNode *, 8> Ops;
1016 for (MDNode *Op : NMD.operands())
1017 Ops.push_back(Elt: remap(Op));
1018
1019 if (!Changed)
1020 continue;
1021
1022 NMD.clearOperands();
1023 for (auto *Op : Ops)
1024 if (Op)
1025 NMD.addOperand(M: Op);
1026 }
1027 return Changed;
1028}
1029
1030unsigned llvm::getDebugMetadataVersionFromModule(const Module &M) {
1031 if (auto *Val = mdconst::dyn_extract_or_null<ConstantInt>(
1032 MD: M.getModuleFlag(Key: "Debug Info Version")))
1033 return Val->getZExtValue();
1034 return 0;
1035}
1036
1037void Instruction::applyMergedLocation(DebugLoc LocA, DebugLoc LocB) {
1038 setDebugLoc(DebugLoc::getMergedLocation(LocA, LocB));
1039}
1040
1041void Instruction::mergeDIAssignID(
1042 ArrayRef<const Instruction *> SourceInstructions) {
1043 // Replace all uses (and attachments) of all the DIAssignIDs
1044 // on SourceInstructions with a single merged value.
1045 assert(getFunction() && "Uninserted instruction merged");
1046 // Collect up the DIAssignID tags.
1047 SmallVector<DIAssignID *, 4> IDs;
1048 for (const Instruction *I : SourceInstructions) {
1049 if (auto *MD = I->getMetadata(KindID: LLVMContext::MD_DIAssignID))
1050 IDs.push_back(Elt: cast<DIAssignID>(Val: MD));
1051 assert(getFunction() == I->getFunction() &&
1052 "Merging with instruction from another function not allowed");
1053 }
1054
1055 // Add this instruction's DIAssignID too, if it has one.
1056 if (auto *MD = getMetadata(KindID: LLVMContext::MD_DIAssignID))
1057 IDs.push_back(Elt: cast<DIAssignID>(Val: MD));
1058
1059 if (IDs.empty())
1060 return; // No DIAssignID tags to process.
1061
1062 DIAssignID *MergeID = IDs[0];
1063 for (DIAssignID *AssignID : drop_begin(RangeOrContainer&: IDs)) {
1064 if (AssignID != MergeID)
1065 at::RAUW(Old: AssignID, New: MergeID);
1066 }
1067 setMetadata(KindID: LLVMContext::MD_DIAssignID, Node: MergeID);
1068}
1069
1070void Instruction::updateLocationAfterHoist() { dropLocation(); }
1071
1072void Instruction::dropLocation() {
1073 const DebugLoc &DL = getDebugLoc();
1074 if (!DL) {
1075 setDebugLoc(DebugLoc::getDropped());
1076 return;
1077 }
1078
1079 // If this isn't a call, drop the location to allow a location from a
1080 // preceding instruction to propagate.
1081 bool MayLowerToCall = false;
1082 if (isa<CallBase>(Val: this)) {
1083 auto *II = dyn_cast<IntrinsicInst>(Val: this);
1084 MayLowerToCall =
1085 !II || IntrinsicInst::mayLowerToFunctionCall(IID: II->getIntrinsicID());
1086 }
1087
1088 if (!MayLowerToCall) {
1089 setDebugLoc(DebugLoc::getDropped());
1090 return;
1091 }
1092
1093 // Set a line 0 location for calls to preserve scope information in case
1094 // inlining occurs.
1095 DISubprogram *SP = getFunction()->getSubprogram();
1096 if (SP)
1097 // If a function scope is available, set it on the line 0 location. When
1098 // hoisting a call to a predecessor block, using the function scope avoids
1099 // making it look like the callee was reached earlier than it should be.
1100 setDebugLoc(DILocation::get(Context&: getContext(), Line: 0, Column: 0, Scope: SP));
1101 else
1102 // The parent function has no scope. Go ahead and drop the location. If
1103 // the parent function is inlined, and the callee has a subprogram, the
1104 // inliner will attach a location to the call.
1105 //
1106 // One alternative is to set a line 0 location with the existing scope and
1107 // inlinedAt info. The location might be sensitive to when inlining occurs.
1108 setDebugLoc(DebugLoc::getDropped());
1109}
1110
1111//===----------------------------------------------------------------------===//
1112// LLVM C API implementations.
1113//===----------------------------------------------------------------------===//
1114
1115static unsigned map_from_llvmDWARFsourcelanguage(LLVMDWARFSourceLanguage lang) {
1116 switch (lang) {
1117#define HANDLE_DW_LANG(ID, NAME, LOWER_BOUND, VERSION, VENDOR) \
1118 case LLVMDWARFSourceLanguage##NAME: \
1119 return ID;
1120#include "llvm/BinaryFormat/Dwarf.def"
1121#undef HANDLE_DW_LANG
1122 }
1123 llvm_unreachable("Unhandled Tag");
1124}
1125
1126template <typename DIT> DIT *unwrapDI(LLVMMetadataRef Ref) {
1127 return (DIT *)(Ref ? unwrap<MDNode>(P: Ref) : nullptr);
1128}
1129
1130static DINode::DIFlags map_from_llvmDIFlags(LLVMDIFlags Flags) {
1131 return static_cast<DINode::DIFlags>(Flags);
1132}
1133
1134static LLVMDIFlags map_to_llvmDIFlags(DINode::DIFlags Flags) {
1135 return static_cast<LLVMDIFlags>(Flags);
1136}
1137
1138static DISubprogram::DISPFlags
1139pack_into_DISPFlags(bool IsLocalToUnit, bool IsDefinition, bool IsOptimized) {
1140 return DISubprogram::toSPFlags(IsLocalToUnit, IsDefinition, IsOptimized);
1141}
1142
1143unsigned LLVMDebugMetadataVersion() {
1144 return DEBUG_METADATA_VERSION;
1145}
1146
1147LLVMDIBuilderRef LLVMCreateDIBuilderDisallowUnresolved(LLVMModuleRef M) {
1148 return wrap(P: new DIBuilder(*unwrap(P: M), false));
1149}
1150
1151LLVMDIBuilderRef LLVMCreateDIBuilder(LLVMModuleRef M) {
1152 return wrap(P: new DIBuilder(*unwrap(P: M)));
1153}
1154
1155unsigned LLVMGetModuleDebugMetadataVersion(LLVMModuleRef M) {
1156 return getDebugMetadataVersionFromModule(M: *unwrap(P: M));
1157}
1158
1159LLVMBool LLVMStripModuleDebugInfo(LLVMModuleRef M) {
1160 return StripDebugInfo(M&: *unwrap(P: M));
1161}
1162
1163void LLVMDisposeDIBuilder(LLVMDIBuilderRef Builder) {
1164 delete unwrap(P: Builder);
1165}
1166
1167void LLVMDIBuilderFinalize(LLVMDIBuilderRef Builder) {
1168 unwrap(P: Builder)->finalize();
1169}
1170
1171void LLVMDIBuilderFinalizeSubprogram(LLVMDIBuilderRef Builder,
1172 LLVMMetadataRef subprogram) {
1173 unwrap(P: Builder)->finalizeSubprogram(SP: unwrapDI<DISubprogram>(Ref: subprogram));
1174}
1175
1176LLVMMetadataRef LLVMDIBuilderCreateCompileUnit(
1177 LLVMDIBuilderRef Builder, LLVMDWARFSourceLanguage Lang,
1178 LLVMMetadataRef FileRef, const char *Producer, size_t ProducerLen,
1179 LLVMBool isOptimized, const char *Flags, size_t FlagsLen,
1180 unsigned RuntimeVer, const char *SplitName, size_t SplitNameLen,
1181 LLVMDWARFEmissionKind Kind, unsigned DWOId, LLVMBool SplitDebugInlining,
1182 LLVMBool DebugInfoForProfiling, const char *SysRoot, size_t SysRootLen,
1183 const char *SDK, size_t SDKLen) {
1184 auto File = unwrapDI<DIFile>(Ref: FileRef);
1185
1186 return wrap(P: unwrap(P: Builder)->createCompileUnit(
1187 Lang: DISourceLanguageName(map_from_llvmDWARFsourcelanguage(lang: Lang)), File,
1188 Producer: StringRef(Producer, ProducerLen), isOptimized, Flags: StringRef(Flags, FlagsLen),
1189 RV: RuntimeVer, SplitName: StringRef(SplitName, SplitNameLen),
1190 Kind: static_cast<DICompileUnit::DebugEmissionKind>(Kind), DWOId,
1191 SplitDebugInlining, DebugInfoForProfiling,
1192 NameTableKind: DICompileUnit::DebugNameTableKind::Default, RangesBaseAddress: false,
1193 SysRoot: StringRef(SysRoot, SysRootLen), SDK: StringRef(SDK, SDKLen)));
1194}
1195
1196LLVMMetadataRef
1197LLVMDIBuilderCreateFile(LLVMDIBuilderRef Builder, const char *Filename,
1198 size_t FilenameLen, const char *Directory,
1199 size_t DirectoryLen) {
1200 return wrap(P: unwrap(P: Builder)->createFile(Filename: StringRef(Filename, FilenameLen),
1201 Directory: StringRef(Directory, DirectoryLen)));
1202}
1203
1204static llvm::DIFile::ChecksumKind
1205map_from_llvmChecksumKind(LLVMChecksumKind CSKind) {
1206 switch (CSKind) {
1207 case LLVMChecksumKind::CSK_MD5:
1208 return llvm::DIFile::CSK_MD5;
1209 case LLVMChecksumKind::CSK_SHA1:
1210 return llvm::DIFile::CSK_SHA1;
1211 case LLVMChecksumKind::CSK_SHA256:
1212 return llvm::DIFile::CSK_SHA256;
1213 }
1214 llvm_unreachable("Unhandled Checksum Kind");
1215}
1216
1217LLVMMetadataRef LLVMDIBuilderCreateFileWithChecksum(
1218 LLVMDIBuilderRef Builder, const char *Filename, size_t FilenameLen,
1219 const char *Directory, size_t DirectoryLen, LLVMChecksumKind ChecksumKind,
1220 const char *Checksum, size_t ChecksumLen, const char *Source,
1221 size_t SourceLen) {
1222 StringRef ChkSum = StringRef(Checksum, ChecksumLen);
1223 auto CSK = map_from_llvmChecksumKind(CSKind: ChecksumKind);
1224 llvm::DIFile::ChecksumInfo<StringRef> CSInfo(CSK, ChkSum);
1225 std::optional<StringRef> Src;
1226 if (SourceLen > 0)
1227 Src = StringRef(Source, SourceLen);
1228 return wrap(P: unwrap(P: Builder)->createFile(Filename: StringRef(Filename, FilenameLen),
1229 Directory: StringRef(Directory, DirectoryLen),
1230 Checksum: CSInfo, Source: Src));
1231}
1232
1233LLVMMetadataRef
1234LLVMDIBuilderCreateModule(LLVMDIBuilderRef Builder, LLVMMetadataRef ParentScope,
1235 const char *Name, size_t NameLen,
1236 const char *ConfigMacros, size_t ConfigMacrosLen,
1237 const char *IncludePath, size_t IncludePathLen,
1238 const char *APINotesFile, size_t APINotesFileLen) {
1239 return wrap(P: unwrap(P: Builder)->createModule(
1240 Scope: unwrapDI<DIScope>(Ref: ParentScope), Name: StringRef(Name, NameLen),
1241 ConfigurationMacros: StringRef(ConfigMacros, ConfigMacrosLen),
1242 IncludePath: StringRef(IncludePath, IncludePathLen),
1243 APINotesFile: StringRef(APINotesFile, APINotesFileLen)));
1244}
1245
1246LLVMMetadataRef LLVMDIBuilderCreateNameSpace(LLVMDIBuilderRef Builder,
1247 LLVMMetadataRef ParentScope,
1248 const char *Name, size_t NameLen,
1249 LLVMBool ExportSymbols) {
1250 return wrap(P: unwrap(P: Builder)->createNameSpace(
1251 Scope: unwrapDI<DIScope>(Ref: ParentScope), Name: StringRef(Name, NameLen), ExportSymbols));
1252}
1253
1254LLVMMetadataRef LLVMDIBuilderCreateFunction(
1255 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
1256 size_t NameLen, const char *LinkageName, size_t LinkageNameLen,
1257 LLVMMetadataRef File, unsigned LineNo, LLVMMetadataRef Ty,
1258 LLVMBool IsLocalToUnit, LLVMBool IsDefinition,
1259 unsigned ScopeLine, LLVMDIFlags Flags, LLVMBool IsOptimized) {
1260 return wrap(P: unwrap(P: Builder)->createFunction(
1261 Scope: unwrapDI<DIScope>(Ref: Scope), Name: {Name, NameLen}, LinkageName: {LinkageName, LinkageNameLen},
1262 File: unwrapDI<DIFile>(Ref: File), LineNo, Ty: unwrapDI<DISubroutineType>(Ref: Ty), ScopeLine,
1263 Flags: map_from_llvmDIFlags(Flags),
1264 SPFlags: pack_into_DISPFlags(IsLocalToUnit, IsDefinition, IsOptimized), TParams: nullptr,
1265 Decl: nullptr, ThrownTypes: nullptr));
1266}
1267
1268
1269LLVMMetadataRef LLVMDIBuilderCreateLexicalBlock(
1270 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope,
1271 LLVMMetadataRef File, unsigned Line, unsigned Col) {
1272 return wrap(P: unwrap(P: Builder)->createLexicalBlock(Scope: unwrapDI<DIScope>(Ref: Scope),
1273 File: unwrapDI<DIFile>(Ref: File),
1274 Line, Col));
1275}
1276
1277LLVMMetadataRef
1278LLVMDIBuilderCreateLexicalBlockFile(LLVMDIBuilderRef Builder,
1279 LLVMMetadataRef Scope,
1280 LLVMMetadataRef File,
1281 unsigned Discriminator) {
1282 return wrap(P: unwrap(P: Builder)->createLexicalBlockFile(Scope: unwrapDI<DIScope>(Ref: Scope),
1283 File: unwrapDI<DIFile>(Ref: File),
1284 Discriminator));
1285}
1286
1287LLVMMetadataRef
1288LLVMDIBuilderCreateImportedModuleFromNamespace(LLVMDIBuilderRef Builder,
1289 LLVMMetadataRef Scope,
1290 LLVMMetadataRef NS,
1291 LLVMMetadataRef File,
1292 unsigned Line) {
1293 return wrap(P: unwrap(P: Builder)->createImportedModule(Context: unwrapDI<DIScope>(Ref: Scope),
1294 NS: unwrapDI<DINamespace>(Ref: NS),
1295 File: unwrapDI<DIFile>(Ref: File),
1296 Line));
1297}
1298
1299LLVMMetadataRef LLVMDIBuilderCreateImportedModuleFromAlias(
1300 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope,
1301 LLVMMetadataRef ImportedEntity, LLVMMetadataRef File, unsigned Line,
1302 LLVMMetadataRef *Elements, unsigned NumElements) {
1303 auto Elts =
1304 (NumElements > 0)
1305 ? unwrap(P: Builder)->getOrCreateArray(Elements: {unwrap(MDs: Elements), NumElements})
1306 : nullptr;
1307 return wrap(P: unwrap(P: Builder)->createImportedModule(
1308 Context: unwrapDI<DIScope>(Ref: Scope), NS: unwrapDI<DIImportedEntity>(Ref: ImportedEntity),
1309 File: unwrapDI<DIFile>(Ref: File), Line, Elements: Elts));
1310}
1311
1312LLVMMetadataRef LLVMDIBuilderCreateImportedModuleFromModule(
1313 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, LLVMMetadataRef M,
1314 LLVMMetadataRef File, unsigned Line, LLVMMetadataRef *Elements,
1315 unsigned NumElements) {
1316 auto Elts =
1317 (NumElements > 0)
1318 ? unwrap(P: Builder)->getOrCreateArray(Elements: {unwrap(MDs: Elements), NumElements})
1319 : nullptr;
1320 return wrap(P: unwrap(P: Builder)->createImportedModule(
1321 Context: unwrapDI<DIScope>(Ref: Scope), M: unwrapDI<DIModule>(Ref: M), File: unwrapDI<DIFile>(Ref: File),
1322 Line, Elements: Elts));
1323}
1324
1325LLVMMetadataRef LLVMDIBuilderCreateImportedDeclaration(
1326 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, LLVMMetadataRef Decl,
1327 LLVMMetadataRef File, unsigned Line, const char *Name, size_t NameLen,
1328 LLVMMetadataRef *Elements, unsigned NumElements) {
1329 auto Elts =
1330 (NumElements > 0)
1331 ? unwrap(P: Builder)->getOrCreateArray(Elements: {unwrap(MDs: Elements), NumElements})
1332 : nullptr;
1333 return wrap(P: unwrap(P: Builder)->createImportedDeclaration(
1334 Context: unwrapDI<DIScope>(Ref: Scope), Decl: unwrapDI<DINode>(Ref: Decl), File: unwrapDI<DIFile>(Ref: File),
1335 Line, Name: {Name, NameLen}, Elements: Elts));
1336}
1337
1338LLVMMetadataRef
1339LLVMDIBuilderCreateDebugLocation(LLVMContextRef Ctx, unsigned Line,
1340 unsigned Column, LLVMMetadataRef Scope,
1341 LLVMMetadataRef InlinedAt) {
1342 return wrap(P: DILocation::get(Context&: *unwrap(P: Ctx), Line, Column, Scope: unwrap(P: Scope),
1343 InlinedAt: unwrap(P: InlinedAt)));
1344}
1345
1346unsigned LLVMDILocationGetLine(LLVMMetadataRef Location) {
1347 return unwrapDI<DILocation>(Ref: Location)->getLine();
1348}
1349
1350unsigned LLVMDILocationGetColumn(LLVMMetadataRef Location) {
1351 return unwrapDI<DILocation>(Ref: Location)->getColumn();
1352}
1353
1354LLVMMetadataRef LLVMDILocationGetScope(LLVMMetadataRef Location) {
1355 return wrap(P: unwrapDI<DILocation>(Ref: Location)->getScope());
1356}
1357
1358LLVMMetadataRef LLVMDILocationGetInlinedAt(LLVMMetadataRef Location) {
1359 return wrap(P: unwrapDI<DILocation>(Ref: Location)->getInlinedAt());
1360}
1361
1362LLVMMetadataRef LLVMDIScopeGetFile(LLVMMetadataRef Scope) {
1363 return wrap(P: unwrapDI<DIScope>(Ref: Scope)->getFile());
1364}
1365
1366const char *LLVMDIFileGetDirectory(LLVMMetadataRef File, unsigned *Len) {
1367 auto Dir = unwrapDI<DIFile>(Ref: File)->getDirectory();
1368 *Len = Dir.size();
1369 return Dir.data();
1370}
1371
1372const char *LLVMDIFileGetFilename(LLVMMetadataRef File, unsigned *Len) {
1373 auto Name = unwrapDI<DIFile>(Ref: File)->getFilename();
1374 *Len = Name.size();
1375 return Name.data();
1376}
1377
1378const char *LLVMDIFileGetSource(LLVMMetadataRef File, unsigned *Len) {
1379 if (auto Src = unwrapDI<DIFile>(Ref: File)->getSource()) {
1380 *Len = Src->size();
1381 return Src->data();
1382 }
1383 *Len = 0;
1384 return "";
1385}
1386
1387LLVMMetadataRef LLVMDIBuilderCreateMacro(LLVMDIBuilderRef Builder,
1388 LLVMMetadataRef ParentMacroFile,
1389 unsigned Line,
1390 LLVMDWARFMacinfoRecordType RecordType,
1391 const char *Name, size_t NameLen,
1392 const char *Value, size_t ValueLen) {
1393 return wrap(
1394 P: unwrap(P: Builder)->createMacro(Parent: unwrapDI<DIMacroFile>(Ref: ParentMacroFile), Line,
1395 MacroType: static_cast<MacinfoRecordType>(RecordType),
1396 Name: {Name, NameLen}, Value: {Value, ValueLen}));
1397}
1398
1399LLVMMetadataRef
1400LLVMDIBuilderCreateTempMacroFile(LLVMDIBuilderRef Builder,
1401 LLVMMetadataRef ParentMacroFile, unsigned Line,
1402 LLVMMetadataRef File) {
1403 return wrap(P: unwrap(P: Builder)->createTempMacroFile(
1404 Parent: unwrapDI<DIMacroFile>(Ref: ParentMacroFile), Line, File: unwrapDI<DIFile>(Ref: File)));
1405}
1406
1407LLVMMetadataRef LLVMDIBuilderCreateEnumerator(LLVMDIBuilderRef Builder,
1408 const char *Name, size_t NameLen,
1409 int64_t Value,
1410 LLVMBool IsUnsigned) {
1411 return wrap(P: unwrap(P: Builder)->createEnumerator(Name: {Name, NameLen}, Val: Value,
1412 IsUnsigned: IsUnsigned != 0));
1413}
1414
1415LLVMMetadataRef LLVMDIBuilderCreateEnumeratorOfArbitraryPrecision(
1416 LLVMDIBuilderRef Builder, const char *Name, size_t NameLen,
1417 uint64_t SizeInBits, const uint64_t Words[], LLVMBool IsUnsigned) {
1418 uint64_t NumWords = (SizeInBits + 63) / 64;
1419 return wrap(P: unwrap(P: Builder)->createEnumerator(
1420 Name: {Name, NameLen},
1421 Value: APSInt(APInt(SizeInBits, ArrayRef(Words, NumWords)), IsUnsigned != 0)));
1422}
1423
1424LLVMMetadataRef LLVMDIBuilderCreateEnumerationType(
1425 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
1426 size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
1427 uint64_t SizeInBits, uint32_t AlignInBits, LLVMMetadataRef *Elements,
1428 unsigned NumElements, LLVMMetadataRef ClassTy) {
1429auto Elts = unwrap(P: Builder)->getOrCreateArray(Elements: {unwrap(MDs: Elements),
1430 NumElements});
1431return wrap(P: unwrap(P: Builder)->createEnumerationType(
1432 Scope: unwrapDI<DIScope>(Ref: Scope), Name: {Name, NameLen}, File: unwrapDI<DIFile>(Ref: File),
1433 LineNumber, SizeInBits, AlignInBits, Elements: Elts, UnderlyingType: unwrapDI<DIType>(Ref: ClassTy)));
1434}
1435
1436LLVMMetadataRef LLVMDIBuilderCreateSetType(
1437 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
1438 size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
1439 uint64_t SizeInBits, uint32_t AlignInBits, LLVMMetadataRef BaseTy) {
1440 return wrap(P: unwrap(P: Builder)->createSetType(
1441 Scope: unwrapDI<DIScope>(Ref: Scope), Name: {Name, NameLen}, File: unwrapDI<DIFile>(Ref: File),
1442 LineNo: LineNumber, SizeInBits, AlignInBits, Ty: unwrapDI<DIType>(Ref: BaseTy)));
1443}
1444
1445LLVMMetadataRef LLVMDIBuilderCreateSubrangeType(
1446 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
1447 size_t NameLen, unsigned LineNo, LLVMMetadataRef File, uint64_t SizeInBits,
1448 uint32_t AlignInBits, LLVMDIFlags Flags, LLVMMetadataRef BaseTy,
1449 LLVMMetadataRef LowerBound, LLVMMetadataRef UpperBound,
1450 LLVMMetadataRef Stride, LLVMMetadataRef Bias) {
1451 return wrap(P: unwrap(P: Builder)->createSubrangeType(
1452 Name: {Name, NameLen}, File: unwrapDI<DIFile>(Ref: File), LineNo, Scope: unwrapDI<DIScope>(Ref: Scope),
1453 SizeInBits, AlignInBits, Flags: map_from_llvmDIFlags(Flags),
1454 Ty: unwrapDI<DIType>(Ref: BaseTy), LowerBound: unwrap(P: LowerBound), UpperBound: unwrap(P: UpperBound),
1455 Stride: unwrap(P: Stride), Bias: unwrap(P: Bias)));
1456}
1457
1458/// MD may be nullptr, a DIExpression or DIVariable.
1459PointerUnion<DIExpression *, DIVariable *> unwrapExprVar(LLVMMetadataRef MD) {
1460 if (!MD)
1461 return nullptr;
1462 MDNode *MDN = unwrapDI<MDNode>(Ref: MD);
1463 if (auto *E = dyn_cast<DIExpression>(Val: MDN))
1464 return E;
1465 assert(isa<DIVariable>(MDN) && "Expected DIExpression or DIVariable");
1466 return cast<DIVariable>(Val: MDN);
1467}
1468
1469LLVMMetadataRef LLVMDIBuilderCreateDynamicArrayType(
1470 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
1471 size_t NameLen, unsigned LineNo, LLVMMetadataRef File, uint64_t Size,
1472 uint32_t AlignInBits, LLVMMetadataRef Ty, LLVMMetadataRef *Subscripts,
1473 unsigned NumSubscripts, LLVMMetadataRef DataLocation,
1474 LLVMMetadataRef Associated, LLVMMetadataRef Allocated, LLVMMetadataRef Rank,
1475 LLVMMetadataRef BitStride) {
1476 auto Subs =
1477 unwrap(P: Builder)->getOrCreateArray(Elements: {unwrap(MDs: Subscripts), NumSubscripts});
1478 return wrap(P: unwrap(P: Builder)->createArrayType(
1479 Scope: unwrapDI<DIScope>(Ref: Scope), Name: {Name, NameLen}, File: unwrapDI<DIFile>(Ref: File), LineNumber: LineNo,
1480 Size, AlignInBits, Ty: unwrapDI<DIType>(Ref: Ty), Subscripts: Subs,
1481 DataLocation: unwrapExprVar(MD: DataLocation), Associated: unwrapExprVar(MD: Associated),
1482 Allocated: unwrapExprVar(MD: Allocated), Rank: unwrapExprVar(MD: Rank), BitStride: unwrap(P: BitStride)));
1483}
1484
1485void LLVMReplaceArrays(LLVMDIBuilderRef Builder, LLVMMetadataRef *T,
1486 LLVMMetadataRef *Elements, unsigned NumElements) {
1487 auto CT = unwrap<DICompositeType>(P: *T);
1488 auto Elts =
1489 unwrap(P: Builder)->getOrCreateArray(Elements: {unwrap(MDs: Elements), NumElements});
1490 unwrap(P: Builder)->replaceArrays(T&: CT, Elements: Elts);
1491}
1492
1493LLVMMetadataRef LLVMDIBuilderCreateUnionType(
1494 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
1495 size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
1496 uint64_t SizeInBits, uint32_t AlignInBits, LLVMDIFlags Flags,
1497 LLVMMetadataRef *Elements, unsigned NumElements, unsigned RunTimeLang,
1498 const char *UniqueId, size_t UniqueIdLen) {
1499 auto Elts = unwrap(P: Builder)->getOrCreateArray(Elements: {unwrap(MDs: Elements),
1500 NumElements});
1501 return wrap(P: unwrap(P: Builder)->createUnionType(
1502 Scope: unwrapDI<DIScope>(Ref: Scope), Name: {Name, NameLen}, File: unwrapDI<DIFile>(Ref: File),
1503 LineNumber, SizeInBits, AlignInBits, Flags: map_from_llvmDIFlags(Flags),
1504 Elements: Elts, RunTimeLang, UniqueIdentifier: {UniqueId, UniqueIdLen}));
1505}
1506
1507
1508LLVMMetadataRef
1509LLVMDIBuilderCreateArrayType(LLVMDIBuilderRef Builder, uint64_t Size,
1510 uint32_t AlignInBits, LLVMMetadataRef Ty,
1511 LLVMMetadataRef *Subscripts,
1512 unsigned NumSubscripts) {
1513 auto Subs = unwrap(P: Builder)->getOrCreateArray(Elements: {unwrap(MDs: Subscripts),
1514 NumSubscripts});
1515 return wrap(P: unwrap(P: Builder)->createArrayType(Size, AlignInBits,
1516 Ty: unwrapDI<DIType>(Ref: Ty), Subscripts: Subs));
1517}
1518
1519LLVMMetadataRef
1520LLVMDIBuilderCreateVectorType(LLVMDIBuilderRef Builder, uint64_t Size,
1521 uint32_t AlignInBits, LLVMMetadataRef Ty,
1522 LLVMMetadataRef *Subscripts,
1523 unsigned NumSubscripts) {
1524 auto Subs = unwrap(P: Builder)->getOrCreateArray(Elements: {unwrap(MDs: Subscripts),
1525 NumSubscripts});
1526 return wrap(P: unwrap(P: Builder)->createVectorType(Size, AlignInBits,
1527 Ty: unwrapDI<DIType>(Ref: Ty), Subscripts: Subs));
1528}
1529
1530LLVMMetadataRef
1531LLVMDIBuilderCreateBasicType(LLVMDIBuilderRef Builder, const char *Name,
1532 size_t NameLen, uint64_t SizeInBits,
1533 LLVMDWARFTypeEncoding Encoding,
1534 LLVMDIFlags Flags) {
1535 return wrap(P: unwrap(P: Builder)->createBasicType(Name: {Name, NameLen},
1536 SizeInBits, Encoding,
1537 Flags: map_from_llvmDIFlags(Flags)));
1538}
1539
1540LLVMMetadataRef LLVMDIBuilderCreatePointerType(
1541 LLVMDIBuilderRef Builder, LLVMMetadataRef PointeeTy,
1542 uint64_t SizeInBits, uint32_t AlignInBits, unsigned AddressSpace,
1543 const char *Name, size_t NameLen) {
1544 return wrap(P: unwrap(P: Builder)->createPointerType(
1545 PointeeTy: unwrapDI<DIType>(Ref: PointeeTy), SizeInBits, AlignInBits, DWARFAddressSpace: AddressSpace,
1546 Name: {Name, NameLen}));
1547}
1548
1549LLVMMetadataRef LLVMDIBuilderCreateStructType(
1550 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
1551 size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
1552 uint64_t SizeInBits, uint32_t AlignInBits, LLVMDIFlags Flags,
1553 LLVMMetadataRef DerivedFrom, LLVMMetadataRef *Elements,
1554 unsigned NumElements, unsigned RunTimeLang, LLVMMetadataRef VTableHolder,
1555 const char *UniqueId, size_t UniqueIdLen) {
1556 auto Elts = unwrap(P: Builder)->getOrCreateArray(Elements: {unwrap(MDs: Elements),
1557 NumElements});
1558 return wrap(P: unwrap(P: Builder)->createStructType(
1559 Scope: unwrapDI<DIScope>(Ref: Scope), Name: {Name, NameLen}, File: unwrapDI<DIFile>(Ref: File),
1560 LineNumber, SizeInBits, AlignInBits, Flags: map_from_llvmDIFlags(Flags),
1561 DerivedFrom: unwrapDI<DIType>(Ref: DerivedFrom), Elements: Elts, RunTimeLang,
1562 VTableHolder: unwrapDI<DIType>(Ref: VTableHolder), UniqueIdentifier: {UniqueId, UniqueIdLen}));
1563}
1564
1565LLVMMetadataRef LLVMDIBuilderCreateMemberType(
1566 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
1567 size_t NameLen, LLVMMetadataRef File, unsigned LineNo, uint64_t SizeInBits,
1568 uint32_t AlignInBits, uint64_t OffsetInBits, LLVMDIFlags Flags,
1569 LLVMMetadataRef Ty) {
1570 return wrap(P: unwrap(P: Builder)->createMemberType(Scope: unwrapDI<DIScope>(Ref: Scope),
1571 Name: {Name, NameLen}, File: unwrapDI<DIFile>(Ref: File), LineNo, SizeInBits, AlignInBits,
1572 OffsetInBits, Flags: map_from_llvmDIFlags(Flags), Ty: unwrapDI<DIType>(Ref: Ty)));
1573}
1574
1575LLVMMetadataRef
1576LLVMDIBuilderCreateUnspecifiedType(LLVMDIBuilderRef Builder, const char *Name,
1577 size_t NameLen) {
1578 return wrap(P: unwrap(P: Builder)->createUnspecifiedType(Name: {Name, NameLen}));
1579}
1580
1581LLVMMetadataRef LLVMDIBuilderCreateStaticMemberType(
1582 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
1583 size_t NameLen, LLVMMetadataRef File, unsigned LineNumber,
1584 LLVMMetadataRef Type, LLVMDIFlags Flags, LLVMValueRef ConstantVal,
1585 uint32_t AlignInBits) {
1586 return wrap(P: unwrap(P: Builder)->createStaticMemberType(
1587 Scope: unwrapDI<DIScope>(Ref: Scope), Name: {Name, NameLen}, File: unwrapDI<DIFile>(Ref: File),
1588 LineNo: LineNumber, Ty: unwrapDI<DIType>(Ref: Type), Flags: map_from_llvmDIFlags(Flags),
1589 Val: unwrap<Constant>(P: ConstantVal), Tag: DW_TAG_member, AlignInBits));
1590}
1591
1592LLVMMetadataRef
1593LLVMDIBuilderCreateObjCIVar(LLVMDIBuilderRef Builder,
1594 const char *Name, size_t NameLen,
1595 LLVMMetadataRef File, unsigned LineNo,
1596 uint64_t SizeInBits, uint32_t AlignInBits,
1597 uint64_t OffsetInBits, LLVMDIFlags Flags,
1598 LLVMMetadataRef Ty, LLVMMetadataRef PropertyNode) {
1599 return wrap(P: unwrap(P: Builder)->createObjCIVar(
1600 Name: {Name, NameLen}, File: unwrapDI<DIFile>(Ref: File), LineNo,
1601 SizeInBits, AlignInBits, OffsetInBits,
1602 Flags: map_from_llvmDIFlags(Flags), Ty: unwrapDI<DIType>(Ref: Ty),
1603 PropertyNode: unwrapDI<MDNode>(Ref: PropertyNode)));
1604}
1605
1606LLVMMetadataRef
1607LLVMDIBuilderCreateObjCProperty(LLVMDIBuilderRef Builder,
1608 const char *Name, size_t NameLen,
1609 LLVMMetadataRef File, unsigned LineNo,
1610 const char *GetterName, size_t GetterNameLen,
1611 const char *SetterName, size_t SetterNameLen,
1612 unsigned PropertyAttributes,
1613 LLVMMetadataRef Ty) {
1614 return wrap(P: unwrap(P: Builder)->createObjCProperty(
1615 Name: {Name, NameLen}, File: unwrapDI<DIFile>(Ref: File), LineNumber: LineNo,
1616 GetterName: {GetterName, GetterNameLen}, SetterName: {SetterName, SetterNameLen},
1617 PropertyAttributes, Ty: unwrapDI<DIType>(Ref: Ty)));
1618}
1619
1620LLVMMetadataRef LLVMDIBuilderCreateObjectPointerType(LLVMDIBuilderRef Builder,
1621 LLVMMetadataRef Type,
1622 LLVMBool Implicit) {
1623 return wrap(P: unwrap(P: Builder)->createObjectPointerType(Ty: unwrapDI<DIType>(Ref: Type),
1624 Implicit));
1625}
1626
1627LLVMMetadataRef
1628LLVMDIBuilderCreateTypedef(LLVMDIBuilderRef Builder, LLVMMetadataRef Type,
1629 const char *Name, size_t NameLen,
1630 LLVMMetadataRef File, unsigned LineNo,
1631 LLVMMetadataRef Scope, uint32_t AlignInBits) {
1632 return wrap(P: unwrap(P: Builder)->createTypedef(
1633 Ty: unwrapDI<DIType>(Ref: Type), Name: {Name, NameLen}, File: unwrapDI<DIFile>(Ref: File), LineNo,
1634 Context: unwrapDI<DIScope>(Ref: Scope), AlignInBits));
1635}
1636
1637LLVMMetadataRef
1638LLVMDIBuilderCreateInheritance(LLVMDIBuilderRef Builder,
1639 LLVMMetadataRef Ty, LLVMMetadataRef BaseTy,
1640 uint64_t BaseOffset, uint32_t VBPtrOffset,
1641 LLVMDIFlags Flags) {
1642 return wrap(P: unwrap(P: Builder)->createInheritance(
1643 Ty: unwrapDI<DIType>(Ref: Ty), BaseTy: unwrapDI<DIType>(Ref: BaseTy),
1644 BaseOffset, VBPtrOffset, Flags: map_from_llvmDIFlags(Flags)));
1645}
1646
1647LLVMMetadataRef
1648LLVMDIBuilderCreateForwardDecl(
1649 LLVMDIBuilderRef Builder, unsigned Tag, const char *Name,
1650 size_t NameLen, LLVMMetadataRef Scope, LLVMMetadataRef File, unsigned Line,
1651 unsigned RuntimeLang, uint64_t SizeInBits, uint32_t AlignInBits,
1652 const char *UniqueIdentifier, size_t UniqueIdentifierLen) {
1653 return wrap(P: unwrap(P: Builder)->createForwardDecl(
1654 Tag, Name: {Name, NameLen}, Scope: unwrapDI<DIScope>(Ref: Scope),
1655 F: unwrapDI<DIFile>(Ref: File), Line, RuntimeLang, SizeInBits,
1656 AlignInBits, UniqueIdentifier: {UniqueIdentifier, UniqueIdentifierLen}));
1657}
1658
1659LLVMMetadataRef
1660LLVMDIBuilderCreateReplaceableCompositeType(
1661 LLVMDIBuilderRef Builder, unsigned Tag, const char *Name,
1662 size_t NameLen, LLVMMetadataRef Scope, LLVMMetadataRef File, unsigned Line,
1663 unsigned RuntimeLang, uint64_t SizeInBits, uint32_t AlignInBits,
1664 LLVMDIFlags Flags, const char *UniqueIdentifier,
1665 size_t UniqueIdentifierLen) {
1666 return wrap(P: unwrap(P: Builder)->createReplaceableCompositeType(
1667 Tag, Name: {Name, NameLen}, Scope: unwrapDI<DIScope>(Ref: Scope),
1668 F: unwrapDI<DIFile>(Ref: File), Line, RuntimeLang, SizeInBits,
1669 AlignInBits, Flags: map_from_llvmDIFlags(Flags),
1670 UniqueIdentifier: {UniqueIdentifier, UniqueIdentifierLen}));
1671}
1672
1673LLVMMetadataRef
1674LLVMDIBuilderCreateQualifiedType(LLVMDIBuilderRef Builder, unsigned Tag,
1675 LLVMMetadataRef Type) {
1676 return wrap(P: unwrap(P: Builder)->createQualifiedType(Tag,
1677 FromTy: unwrapDI<DIType>(Ref: Type)));
1678}
1679
1680LLVMMetadataRef
1681LLVMDIBuilderCreateReferenceType(LLVMDIBuilderRef Builder, unsigned Tag,
1682 LLVMMetadataRef Type) {
1683 return wrap(P: unwrap(P: Builder)->createReferenceType(Tag,
1684 RTy: unwrapDI<DIType>(Ref: Type)));
1685}
1686
1687LLVMMetadataRef
1688LLVMDIBuilderCreateNullPtrType(LLVMDIBuilderRef Builder) {
1689 return wrap(P: unwrap(P: Builder)->createNullPtrType());
1690}
1691
1692LLVMMetadataRef
1693LLVMDIBuilderCreateMemberPointerType(LLVMDIBuilderRef Builder,
1694 LLVMMetadataRef PointeeType,
1695 LLVMMetadataRef ClassType,
1696 uint64_t SizeInBits,
1697 uint32_t AlignInBits,
1698 LLVMDIFlags Flags) {
1699 return wrap(P: unwrap(P: Builder)->createMemberPointerType(
1700 PointeeTy: unwrapDI<DIType>(Ref: PointeeType),
1701 Class: unwrapDI<DIType>(Ref: ClassType), SizeInBits: AlignInBits, AlignInBits: SizeInBits,
1702 Flags: map_from_llvmDIFlags(Flags)));
1703}
1704
1705LLVMMetadataRef
1706LLVMDIBuilderCreateBitFieldMemberType(LLVMDIBuilderRef Builder,
1707 LLVMMetadataRef Scope,
1708 const char *Name, size_t NameLen,
1709 LLVMMetadataRef File, unsigned LineNumber,
1710 uint64_t SizeInBits,
1711 uint64_t OffsetInBits,
1712 uint64_t StorageOffsetInBits,
1713 LLVMDIFlags Flags, LLVMMetadataRef Type) {
1714 return wrap(P: unwrap(P: Builder)->createBitFieldMemberType(
1715 Scope: unwrapDI<DIScope>(Ref: Scope), Name: {Name, NameLen},
1716 File: unwrapDI<DIFile>(Ref: File), LineNo: LineNumber,
1717 SizeInBits, OffsetInBits, StorageOffsetInBits,
1718 Flags: map_from_llvmDIFlags(Flags), Ty: unwrapDI<DIType>(Ref: Type)));
1719}
1720
1721LLVMMetadataRef LLVMDIBuilderCreateClassType(LLVMDIBuilderRef Builder,
1722 LLVMMetadataRef Scope, const char *Name, size_t NameLen,
1723 LLVMMetadataRef File, unsigned LineNumber, uint64_t SizeInBits,
1724 uint32_t AlignInBits, uint64_t OffsetInBits, LLVMDIFlags Flags,
1725 LLVMMetadataRef DerivedFrom,
1726 LLVMMetadataRef *Elements, unsigned NumElements,
1727 LLVMMetadataRef VTableHolder, LLVMMetadataRef TemplateParamsNode,
1728 const char *UniqueIdentifier, size_t UniqueIdentifierLen) {
1729 auto Elts = unwrap(P: Builder)->getOrCreateArray(Elements: {unwrap(MDs: Elements),
1730 NumElements});
1731 return wrap(P: unwrap(P: Builder)->createClassType(
1732 Scope: unwrapDI<DIScope>(Ref: Scope), Name: {Name, NameLen}, File: unwrapDI<DIFile>(Ref: File),
1733 LineNumber, SizeInBits, AlignInBits, OffsetInBits,
1734 Flags: map_from_llvmDIFlags(Flags), DerivedFrom: unwrapDI<DIType>(Ref: DerivedFrom), Elements: Elts,
1735 /*RunTimeLang=*/0, VTableHolder: unwrapDI<DIType>(Ref: VTableHolder),
1736 TemplateParms: unwrapDI<MDNode>(Ref: TemplateParamsNode),
1737 UniqueIdentifier: {UniqueIdentifier, UniqueIdentifierLen}));
1738}
1739
1740LLVMMetadataRef
1741LLVMDIBuilderCreateArtificialType(LLVMDIBuilderRef Builder,
1742 LLVMMetadataRef Type) {
1743 return wrap(P: unwrap(P: Builder)->createArtificialType(Ty: unwrapDI<DIType>(Ref: Type)));
1744}
1745
1746uint16_t LLVMGetDINodeTag(LLVMMetadataRef MD) {
1747 return unwrapDI<DINode>(Ref: MD)->getTag();
1748}
1749
1750const char *LLVMDITypeGetName(LLVMMetadataRef DType, size_t *Length) {
1751 StringRef Str = unwrapDI<DIType>(Ref: DType)->getName();
1752 *Length = Str.size();
1753 return Str.data();
1754}
1755
1756uint64_t LLVMDITypeGetSizeInBits(LLVMMetadataRef DType) {
1757 return unwrapDI<DIType>(Ref: DType)->getSizeInBits();
1758}
1759
1760uint64_t LLVMDITypeGetOffsetInBits(LLVMMetadataRef DType) {
1761 return unwrapDI<DIType>(Ref: DType)->getOffsetInBits();
1762}
1763
1764uint32_t LLVMDITypeGetAlignInBits(LLVMMetadataRef DType) {
1765 return unwrapDI<DIType>(Ref: DType)->getAlignInBits();
1766}
1767
1768unsigned LLVMDITypeGetLine(LLVMMetadataRef DType) {
1769 return unwrapDI<DIType>(Ref: DType)->getLine();
1770}
1771
1772LLVMDIFlags LLVMDITypeGetFlags(LLVMMetadataRef DType) {
1773 return map_to_llvmDIFlags(Flags: unwrapDI<DIType>(Ref: DType)->getFlags());
1774}
1775
1776LLVMMetadataRef LLVMDIBuilderGetOrCreateTypeArray(LLVMDIBuilderRef Builder,
1777 LLVMMetadataRef *Types,
1778 size_t Length) {
1779 return wrap(
1780 P: unwrap(P: Builder)->getOrCreateTypeArray(Elements: {unwrap(MDs: Types), Length}).get());
1781}
1782
1783LLVMMetadataRef
1784LLVMDIBuilderCreateSubroutineType(LLVMDIBuilderRef Builder,
1785 LLVMMetadataRef File,
1786 LLVMMetadataRef *ParameterTypes,
1787 unsigned NumParameterTypes,
1788 LLVMDIFlags Flags) {
1789 auto Elts = unwrap(P: Builder)->getOrCreateTypeArray(Elements: {unwrap(MDs: ParameterTypes),
1790 NumParameterTypes});
1791 return wrap(P: unwrap(P: Builder)->createSubroutineType(
1792 ParameterTypes: Elts, Flags: map_from_llvmDIFlags(Flags)));
1793}
1794
1795LLVMMetadataRef LLVMDIBuilderCreateExpression(LLVMDIBuilderRef Builder,
1796 uint64_t *Addr, size_t Length) {
1797 return wrap(
1798 P: unwrap(P: Builder)->createExpression(Addr: ArrayRef<uint64_t>(Addr, Length)));
1799}
1800
1801LLVMMetadataRef
1802LLVMDIBuilderCreateConstantValueExpression(LLVMDIBuilderRef Builder,
1803 uint64_t Value) {
1804 return wrap(P: unwrap(P: Builder)->createConstantValueExpression(Val: Value));
1805}
1806
1807LLVMMetadataRef LLVMDIBuilderCreateGlobalVariableExpression(
1808 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
1809 size_t NameLen, const char *Linkage, size_t LinkLen, LLVMMetadataRef File,
1810 unsigned LineNo, LLVMMetadataRef Ty, LLVMBool LocalToUnit,
1811 LLVMMetadataRef Expr, LLVMMetadataRef Decl, uint32_t AlignInBits) {
1812 return wrap(P: unwrap(P: Builder)->createGlobalVariableExpression(
1813 Context: unwrapDI<DIScope>(Ref: Scope), Name: {Name, NameLen}, LinkageName: {Linkage, LinkLen},
1814 File: unwrapDI<DIFile>(Ref: File), LineNo, Ty: unwrapDI<DIType>(Ref: Ty), IsLocalToUnit: LocalToUnit,
1815 isDefined: true, Expr: unwrap<DIExpression>(P: Expr), Decl: unwrapDI<MDNode>(Ref: Decl),
1816 TemplateParams: nullptr, AlignInBits));
1817}
1818
1819LLVMMetadataRef LLVMDIGlobalVariableExpressionGetVariable(LLVMMetadataRef GVE) {
1820 return wrap(P: unwrapDI<DIGlobalVariableExpression>(Ref: GVE)->getVariable());
1821}
1822
1823LLVMMetadataRef LLVMDIGlobalVariableExpressionGetExpression(
1824 LLVMMetadataRef GVE) {
1825 return wrap(P: unwrapDI<DIGlobalVariableExpression>(Ref: GVE)->getExpression());
1826}
1827
1828LLVMMetadataRef LLVMDIVariableGetFile(LLVMMetadataRef Var) {
1829 return wrap(P: unwrapDI<DIVariable>(Ref: Var)->getFile());
1830}
1831
1832LLVMMetadataRef LLVMDIVariableGetScope(LLVMMetadataRef Var) {
1833 return wrap(P: unwrapDI<DIVariable>(Ref: Var)->getScope());
1834}
1835
1836unsigned LLVMDIVariableGetLine(LLVMMetadataRef Var) {
1837 return unwrapDI<DIVariable>(Ref: Var)->getLine();
1838}
1839
1840LLVMMetadataRef LLVMTemporaryMDNode(LLVMContextRef Ctx, LLVMMetadataRef *Data,
1841 size_t Count) {
1842 return wrap(
1843 P: MDTuple::getTemporary(Context&: *unwrap(P: Ctx), MDs: {unwrap(MDs: Data), Count}).release());
1844}
1845
1846void LLVMDisposeTemporaryMDNode(LLVMMetadataRef TempNode) {
1847 MDNode::deleteTemporary(N: unwrapDI<MDNode>(Ref: TempNode));
1848}
1849
1850void LLVMMetadataReplaceAllUsesWith(LLVMMetadataRef TargetMetadata,
1851 LLVMMetadataRef Replacement) {
1852 auto *Node = unwrapDI<MDNode>(Ref: TargetMetadata);
1853 Node->replaceAllUsesWith(MD: unwrap(P: Replacement));
1854 MDNode::deleteTemporary(N: Node);
1855}
1856
1857LLVMMetadataRef LLVMDIBuilderCreateTempGlobalVariableFwdDecl(
1858 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
1859 size_t NameLen, const char *Linkage, size_t LnkLen, LLVMMetadataRef File,
1860 unsigned LineNo, LLVMMetadataRef Ty, LLVMBool LocalToUnit,
1861 LLVMMetadataRef Decl, uint32_t AlignInBits) {
1862 return wrap(P: unwrap(P: Builder)->createTempGlobalVariableFwdDecl(
1863 Context: unwrapDI<DIScope>(Ref: Scope), Name: {Name, NameLen}, LinkageName: {Linkage, LnkLen},
1864 File: unwrapDI<DIFile>(Ref: File), LineNo, Ty: unwrapDI<DIType>(Ref: Ty), IsLocalToUnit: LocalToUnit,
1865 Decl: unwrapDI<MDNode>(Ref: Decl), TemplateParams: nullptr, AlignInBits));
1866}
1867
1868LLVMDbgRecordRef LLVMDIBuilderInsertDeclareRecordBefore(
1869 LLVMDIBuilderRef Builder, LLVMValueRef Storage, LLVMMetadataRef VarInfo,
1870 LLVMMetadataRef Expr, LLVMMetadataRef DL, LLVMValueRef Instr) {
1871 DbgInstPtr DbgInst = unwrap(P: Builder)->insertDeclare(
1872 Storage: unwrap(P: Storage), VarInfo: unwrap<DILocalVariable>(P: VarInfo),
1873 Expr: unwrap<DIExpression>(P: Expr), DL: unwrap<DILocation>(P: DL),
1874 InsertPt: Instr ? InsertPosition(unwrap<Instruction>(P: Instr)->getIterator())
1875 : nullptr);
1876 // This assert will fail if the module is in the old debug info format.
1877 // This function should only be called if the module is in the new
1878 // debug info format.
1879 // See https://llvm.org/docs/RemoveDIsDebugInfo.html#c-api-changes,
1880 // LLVMIsNewDbgInfoFormat, and LLVMSetIsNewDbgInfoFormat for more info.
1881 assert(isa<DbgRecord *>(DbgInst) &&
1882 "Function unexpectedly in old debug info format");
1883 return wrap(P: cast<DbgRecord *>(Val&: DbgInst));
1884}
1885
1886LLVMDbgRecordRef LLVMDIBuilderInsertDeclareRecordAtEnd(
1887 LLVMDIBuilderRef Builder, LLVMValueRef Storage, LLVMMetadataRef VarInfo,
1888 LLVMMetadataRef Expr, LLVMMetadataRef DL, LLVMBasicBlockRef Block) {
1889 DbgInstPtr DbgInst = unwrap(P: Builder)->insertDeclare(
1890 Storage: unwrap(P: Storage), VarInfo: unwrap<DILocalVariable>(P: VarInfo),
1891 Expr: unwrap<DIExpression>(P: Expr), DL: unwrap<DILocation>(P: DL), InsertAtEnd: unwrap(P: Block));
1892 // This assert will fail if the module is in the old debug info format.
1893 // This function should only be called if the module is in the new
1894 // debug info format.
1895 // See https://llvm.org/docs/RemoveDIsDebugInfo.html#c-api-changes,
1896 // LLVMIsNewDbgInfoFormat, and LLVMSetIsNewDbgInfoFormat for more info.
1897 assert(isa<DbgRecord *>(DbgInst) &&
1898 "Function unexpectedly in old debug info format");
1899 return wrap(P: cast<DbgRecord *>(Val&: DbgInst));
1900}
1901
1902LLVMDbgRecordRef LLVMDIBuilderInsertDbgValueRecordBefore(
1903 LLVMDIBuilderRef Builder, LLVMValueRef Val, LLVMMetadataRef VarInfo,
1904 LLVMMetadataRef Expr, LLVMMetadataRef DebugLoc, LLVMValueRef Instr) {
1905 DbgInstPtr DbgInst = unwrap(P: Builder)->insertDbgValueIntrinsic(
1906 Val: unwrap(P: Val), VarInfo: unwrap<DILocalVariable>(P: VarInfo), Expr: unwrap<DIExpression>(P: Expr),
1907 DL: unwrap<DILocation>(P: DebugLoc),
1908 InsertPt: Instr ? InsertPosition(unwrap<Instruction>(P: Instr)->getIterator())
1909 : nullptr);
1910 // This assert will fail if the module is in the old debug info format.
1911 // This function should only be called if the module is in the new
1912 // debug info format.
1913 // See https://llvm.org/docs/RemoveDIsDebugInfo.html#c-api-changes,
1914 // LLVMIsNewDbgInfoFormat, and LLVMSetIsNewDbgInfoFormat for more info.
1915 assert(isa<DbgRecord *>(DbgInst) &&
1916 "Function unexpectedly in old debug info format");
1917 return wrap(P: cast<DbgRecord *>(Val&: DbgInst));
1918}
1919
1920LLVMDbgRecordRef LLVMDIBuilderInsertDbgValueRecordAtEnd(
1921 LLVMDIBuilderRef Builder, LLVMValueRef Val, LLVMMetadataRef VarInfo,
1922 LLVMMetadataRef Expr, LLVMMetadataRef DebugLoc, LLVMBasicBlockRef Block) {
1923 DbgInstPtr DbgInst = unwrap(P: Builder)->insertDbgValueIntrinsic(
1924 Val: unwrap(P: Val), VarInfo: unwrap<DILocalVariable>(P: VarInfo), Expr: unwrap<DIExpression>(P: Expr),
1925 DL: unwrap<DILocation>(P: DebugLoc),
1926 InsertPt: Block ? InsertPosition(unwrap(P: Block)->end()) : nullptr);
1927 // This assert will fail if the module is in the old debug info format.
1928 // This function should only be called if the module is in the new
1929 // debug info format.
1930 // See https://llvm.org/docs/RemoveDIsDebugInfo.html#c-api-changes,
1931 // LLVMIsNewDbgInfoFormat, and LLVMSetIsNewDbgInfoFormat for more info.
1932 assert(isa<DbgRecord *>(DbgInst) &&
1933 "Function unexpectedly in old debug info format");
1934 return wrap(P: cast<DbgRecord *>(Val&: DbgInst));
1935}
1936
1937LLVMMetadataRef LLVMDIBuilderCreateAutoVariable(
1938 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
1939 size_t NameLen, LLVMMetadataRef File, unsigned LineNo, LLVMMetadataRef Ty,
1940 LLVMBool AlwaysPreserve, LLVMDIFlags Flags, uint32_t AlignInBits) {
1941 return wrap(P: unwrap(P: Builder)->createAutoVariable(
1942 Scope: unwrap<DIScope>(P: Scope), Name: {Name, NameLen}, File: unwrap<DIFile>(P: File),
1943 LineNo, Ty: unwrap<DIType>(P: Ty), AlwaysPreserve,
1944 Flags: map_from_llvmDIFlags(Flags), AlignInBits));
1945}
1946
1947LLVMMetadataRef LLVMDIBuilderCreateParameterVariable(
1948 LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, const char *Name,
1949 size_t NameLen, unsigned ArgNo, LLVMMetadataRef File, unsigned LineNo,
1950 LLVMMetadataRef Ty, LLVMBool AlwaysPreserve, LLVMDIFlags Flags) {
1951 return wrap(P: unwrap(P: Builder)->createParameterVariable(
1952 Scope: unwrap<DIScope>(P: Scope), Name: {Name, NameLen}, ArgNo, File: unwrap<DIFile>(P: File),
1953 LineNo, Ty: unwrap<DIType>(P: Ty), AlwaysPreserve,
1954 Flags: map_from_llvmDIFlags(Flags)));
1955}
1956
1957LLVMMetadataRef LLVMDIBuilderGetOrCreateSubrange(LLVMDIBuilderRef Builder,
1958 int64_t Lo, int64_t Count) {
1959 return wrap(P: unwrap(P: Builder)->getOrCreateSubrange(Lo, Count));
1960}
1961
1962LLVMMetadataRef LLVMDIBuilderGetOrCreateArray(LLVMDIBuilderRef Builder,
1963 LLVMMetadataRef *Data,
1964 size_t Length) {
1965 Metadata **DataValue = unwrap(MDs: Data);
1966 return wrap(P: unwrap(P: Builder)->getOrCreateArray(Elements: {DataValue, Length}).get());
1967}
1968
1969LLVMMetadataRef LLVMGetSubprogram(LLVMValueRef Func) {
1970 return wrap(P: unwrap<Function>(P: Func)->getSubprogram());
1971}
1972
1973void LLVMSetSubprogram(LLVMValueRef Func, LLVMMetadataRef SP) {
1974 unwrap<Function>(P: Func)->setSubprogram(unwrap<DISubprogram>(P: SP));
1975}
1976
1977unsigned LLVMDISubprogramGetLine(LLVMMetadataRef Subprogram) {
1978 return unwrapDI<DISubprogram>(Ref: Subprogram)->getLine();
1979}
1980
1981void LLVMDISubprogramReplaceType(LLVMMetadataRef Subprogram,
1982 LLVMMetadataRef SubroutineType) {
1983 unwrapDI<DISubprogram>(Ref: Subprogram)
1984 ->replaceType(Ty: unwrapDI<DISubroutineType>(Ref: SubroutineType));
1985}
1986
1987LLVMMetadataRef LLVMInstructionGetDebugLoc(LLVMValueRef Inst) {
1988 return wrap(P: unwrap<Instruction>(P: Inst)->getDebugLoc().getAsMDNode());
1989}
1990
1991void LLVMInstructionSetDebugLoc(LLVMValueRef Inst, LLVMMetadataRef Loc) {
1992 if (Loc)
1993 unwrap<Instruction>(P: Inst)->setDebugLoc(DebugLoc(unwrap<DILocation>(P: Loc)));
1994 else
1995 unwrap<Instruction>(P: Inst)->setDebugLoc(DebugLoc());
1996}
1997
1998LLVMMetadataRef LLVMDIBuilderCreateLabel(LLVMDIBuilderRef Builder,
1999 LLVMMetadataRef Context,
2000 const char *Name, size_t NameLen,
2001 LLVMMetadataRef File, unsigned LineNo,
2002 LLVMBool AlwaysPreserve) {
2003 return wrap(P: unwrap(P: Builder)->createLabel(
2004 Scope: unwrapDI<DIScope>(Ref: Context), Name: StringRef(Name, NameLen),
2005 File: unwrapDI<DIFile>(Ref: File), LineNo, /*Column*/ 0, /*IsArtificial*/ false,
2006 /*CoroSuspendIdx*/ std::nullopt, AlwaysPreserve));
2007}
2008
2009LLVMDbgRecordRef LLVMDIBuilderInsertLabelBefore(LLVMDIBuilderRef Builder,
2010 LLVMMetadataRef LabelInfo,
2011 LLVMMetadataRef Location,
2012 LLVMValueRef InsertBefore) {
2013 DbgInstPtr DbgInst = unwrap(P: Builder)->insertLabel(
2014 LabelInfo: unwrapDI<DILabel>(Ref: LabelInfo), DL: unwrapDI<DILocation>(Ref: Location),
2015 InsertPt: InsertBefore
2016 ? InsertPosition(unwrap<Instruction>(P: InsertBefore)->getIterator())
2017 : nullptr);
2018 // This assert will fail if the module is in the old debug info format.
2019 // This function should only be called if the module is in the new
2020 // debug info format.
2021 // See https://llvm.org/docs/RemoveDIsDebugInfo.html#c-api-changes,
2022 // LLVMIsNewDbgInfoFormat, and LLVMSetIsNewDbgInfoFormat for more info.
2023 assert(isa<DbgRecord *>(DbgInst) &&
2024 "Function unexpectedly in old debug info format");
2025 return wrap(P: cast<DbgRecord *>(Val&: DbgInst));
2026}
2027
2028LLVMDbgRecordRef LLVMDIBuilderInsertLabelAtEnd(LLVMDIBuilderRef Builder,
2029 LLVMMetadataRef LabelInfo,
2030 LLVMMetadataRef Location,
2031 LLVMBasicBlockRef InsertAtEnd) {
2032 DbgInstPtr DbgInst = unwrap(P: Builder)->insertLabel(
2033 LabelInfo: unwrapDI<DILabel>(Ref: LabelInfo), DL: unwrapDI<DILocation>(Ref: Location),
2034 InsertPt: InsertAtEnd ? InsertPosition(unwrap(P: InsertAtEnd)->end()) : nullptr);
2035 // This assert will fail if the module is in the old debug info format.
2036 // This function should only be called if the module is in the new
2037 // debug info format.
2038 // See https://llvm.org/docs/RemoveDIsDebugInfo.html#c-api-changes,
2039 // LLVMIsNewDbgInfoFormat, and LLVMSetIsNewDbgInfoFormat for more info.
2040 assert(isa<DbgRecord *>(DbgInst) &&
2041 "Function unexpectedly in old debug info format");
2042 return wrap(P: cast<DbgRecord *>(Val&: DbgInst));
2043}
2044
2045LLVMMetadataKind LLVMGetMetadataKind(LLVMMetadataRef Metadata) {
2046 switch(unwrap(P: Metadata)->getMetadataID()) {
2047#define HANDLE_METADATA_LEAF(CLASS) \
2048 case Metadata::CLASS##Kind: \
2049 return (LLVMMetadataKind)LLVM##CLASS##MetadataKind;
2050#include "llvm/IR/Metadata.def"
2051 default:
2052 return (LLVMMetadataKind)LLVMGenericDINodeMetadataKind;
2053 }
2054}
2055
2056AssignmentInstRange at::getAssignmentInsts(DIAssignID *ID) {
2057 assert(ID && "Expected non-null ID");
2058 LLVMContext &Ctx = ID->getContext();
2059 auto &Map = Ctx.pImpl->AssignmentIDToInstrs;
2060
2061 auto MapIt = Map.find(Val: ID);
2062 if (MapIt == Map.end())
2063 return make_range(x: nullptr, y: nullptr);
2064
2065 return make_range(x: MapIt->second.begin(), y: MapIt->second.end());
2066}
2067
2068void at::deleteAssignmentMarkers(const Instruction *Inst) {
2069 for (auto *DVR : getDVRAssignmentMarkers(Inst))
2070 DVR->eraseFromParent();
2071}
2072
2073void at::RAUW(DIAssignID *Old, DIAssignID *New) {
2074 // Replace attachments.
2075 AssignmentInstRange InstRange = getAssignmentInsts(ID: Old);
2076 // Use intermediate storage for the instruction ptrs because the
2077 // getAssignmentInsts range iterators will be invalidated by adding and
2078 // removing DIAssignID attachments.
2079 SmallVector<Instruction *> InstVec(InstRange.begin(), InstRange.end());
2080 for (auto *I : InstVec)
2081 I->setMetadata(KindID: LLVMContext::MD_DIAssignID, Node: New);
2082
2083 Old->replaceAllUsesWith(MD: New);
2084}
2085
2086void at::deleteAll(Function *F) {
2087 for (BasicBlock &BB : *F) {
2088 for (Instruction &I : BB) {
2089 for (DbgVariableRecord &DVR :
2090 make_early_inc_range(Range: filterDbgVars(R: I.getDbgRecordRange())))
2091 if (DVR.isDbgAssign())
2092 DVR.eraseFromParent();
2093
2094 I.setMetadata(KindID: LLVMContext::MD_DIAssignID, Node: nullptr);
2095 }
2096 }
2097}
2098
2099bool at::calculateFragmentIntersect(
2100 const DataLayout &DL, const Value *Dest, uint64_t SliceOffsetInBits,
2101 uint64_t SliceSizeInBits, const DbgVariableRecord *AssignRecord,
2102 std::optional<DIExpression::FragmentInfo> &Result) {
2103 // No overlap if this DbgRecord describes a killed location.
2104 if (AssignRecord->isKillAddress())
2105 return false;
2106
2107 int64_t AddrOffsetInBits;
2108 {
2109 int64_t AddrOffsetInBytes;
2110 SmallVector<uint64_t> PostOffsetOps; //< Unused.
2111 // Bail if we can't find a constant offset (or none) in the expression.
2112 if (!AssignRecord->getAddressExpression()->extractLeadingOffset(
2113 OffsetInBytes&: AddrOffsetInBytes, RemainingOps&: PostOffsetOps))
2114 return false;
2115 AddrOffsetInBits = AddrOffsetInBytes * 8;
2116 }
2117
2118 Value *Addr = AssignRecord->getAddress();
2119 // FIXME: It may not always be zero.
2120 int64_t BitExtractOffsetInBits = 0;
2121 DIExpression::FragmentInfo VarFrag =
2122 AssignRecord->getFragmentOrEntireVariable();
2123
2124 int64_t OffsetFromLocationInBits; //< Unused.
2125 return DIExpression::calculateFragmentIntersect(
2126 DL, SliceStart: Dest, SliceOffsetInBits, SliceSizeInBits, DbgPtr: Addr, DbgPtrOffsetInBits: AddrOffsetInBits,
2127 DbgExtractOffsetInBits: BitExtractOffsetInBits, VarFrag, Result, OffsetFromLocationInBits);
2128}
2129
2130/// Update inlined instructions' DIAssignID metadata. We need to do this
2131/// otherwise a function inlined more than once into the same function
2132/// will cause DIAssignID to be shared by many instructions.
2133void at::remapAssignID(DenseMap<DIAssignID *, DIAssignID *> &Map,
2134 Instruction &I) {
2135 auto GetNewID = [&Map](Metadata *Old) {
2136 DIAssignID *OldID = cast<DIAssignID>(Val: Old);
2137 if (DIAssignID *NewID = Map.lookup(Val: OldID))
2138 return NewID;
2139 DIAssignID *NewID = DIAssignID::getDistinct(Context&: OldID->getContext());
2140 Map[OldID] = NewID;
2141 return NewID;
2142 };
2143 // If we find a DIAssignID attachment or use, replace it with a new version.
2144 for (DbgVariableRecord &DVR : filterDbgVars(R: I.getDbgRecordRange())) {
2145 if (DVR.isDbgAssign())
2146 DVR.setAssignId(GetNewID(DVR.getAssignID()));
2147 }
2148 if (auto *ID = I.getMetadata(KindID: LLVMContext::MD_DIAssignID))
2149 I.setMetadata(KindID: LLVMContext::MD_DIAssignID, Node: GetNewID(ID));
2150}
2151
2152/// Collect constant properties (base, size, offset) of \p StoreDest.
2153/// Return std::nullopt if any properties are not constants or the
2154/// offset from the base pointer is negative.
2155static std::optional<AssignmentInfo>
2156getAssignmentInfoImpl(const DataLayout &DL, const Value *StoreDest,
2157 TypeSize SizeInBits) {
2158 if (SizeInBits.isScalable())
2159 return std::nullopt;
2160 APInt GEPOffset(DL.getIndexTypeSizeInBits(Ty: StoreDest->getType()), 0);
2161 const Value *Base = StoreDest->stripAndAccumulateConstantOffsets(
2162 DL, Offset&: GEPOffset, /*AllowNonInbounds*/ true);
2163
2164 if (GEPOffset.isNegative())
2165 return std::nullopt;
2166
2167 uint64_t OffsetInBytes = GEPOffset.getLimitedValue();
2168 // Check for overflow.
2169 if (OffsetInBytes == UINT64_MAX)
2170 return std::nullopt;
2171 if (const auto *Alloca = dyn_cast<AllocaInst>(Val: Base))
2172 if (!DL.getTypeSizeInBits(Ty: Alloca->getAllocatedType()).isScalable())
2173 return AssignmentInfo(DL, Alloca, OffsetInBytes * 8, SizeInBits);
2174 return std::nullopt;
2175}
2176
2177std::optional<AssignmentInfo> at::getAssignmentInfo(const DataLayout &DL,
2178 const MemIntrinsic *I) {
2179 const Value *StoreDest = I->getRawDest();
2180 // Assume 8 bit bytes.
2181 auto *ConstLengthInBytes = dyn_cast<ConstantInt>(Val: I->getLength());
2182 if (!ConstLengthInBytes)
2183 // We can't use a non-const size, bail.
2184 return std::nullopt;
2185 uint64_t SizeInBits = 8 * ConstLengthInBytes->getZExtValue();
2186 return getAssignmentInfoImpl(DL, StoreDest, SizeInBits: TypeSize::getFixed(ExactSize: SizeInBits));
2187}
2188
2189std::optional<AssignmentInfo> at::getAssignmentInfo(const DataLayout &DL,
2190 const StoreInst *SI) {
2191 TypeSize SizeInBits = DL.getTypeSizeInBits(Ty: SI->getValueOperand()->getType());
2192 return getAssignmentInfoImpl(DL, StoreDest: SI->getPointerOperand(), SizeInBits);
2193}
2194
2195std::optional<AssignmentInfo> at::getAssignmentInfo(const DataLayout &DL,
2196 const AllocaInst *AI) {
2197 TypeSize SizeInBits = DL.getTypeSizeInBits(Ty: AI->getAllocatedType());
2198 return getAssignmentInfoImpl(DL, StoreDest: AI, SizeInBits);
2199}
2200
2201/// Returns nullptr if the assignment shouldn't be attributed to this variable.
2202static void emitDbgAssign(AssignmentInfo Info, Value *Val, Value *Dest,
2203 Instruction &StoreLikeInst, const VarRecord &VarRec,
2204 DIBuilder &DIB) {
2205 auto *ID = StoreLikeInst.getMetadata(KindID: LLVMContext::MD_DIAssignID);
2206 assert(ID && "Store instruction must have DIAssignID metadata");
2207 (void)ID;
2208
2209 const uint64_t StoreStartBit = Info.OffsetInBits;
2210 const uint64_t StoreEndBit = Info.OffsetInBits + Info.SizeInBits;
2211
2212 uint64_t FragStartBit = StoreStartBit;
2213 uint64_t FragEndBit = StoreEndBit;
2214
2215 bool StoreToWholeVariable = Info.StoreToWholeAlloca;
2216 if (auto Size = VarRec.Var->getSizeInBits()) {
2217 // NOTE: trackAssignments doesn't understand base expressions yet, so all
2218 // variables that reach here are guaranteed to start at offset 0 in the
2219 // alloca.
2220 const uint64_t VarStartBit = 0;
2221 const uint64_t VarEndBit = *Size;
2222
2223 // FIXME: trim FragStartBit when nonzero VarStartBit is supported.
2224 FragEndBit = std::min(a: FragEndBit, b: VarEndBit);
2225
2226 // Discard stores to bits outside this variable.
2227 if (FragStartBit >= FragEndBit)
2228 return;
2229
2230 StoreToWholeVariable = FragStartBit <= VarStartBit && FragEndBit >= *Size;
2231 }
2232
2233 DIExpression *Expr = DIExpression::get(Context&: StoreLikeInst.getContext(), Elements: {});
2234 if (!StoreToWholeVariable) {
2235 auto R = DIExpression::createFragmentExpression(Expr, OffsetInBits: FragStartBit,
2236 SizeInBits: FragEndBit - FragStartBit);
2237 assert(R.has_value() && "failed to create fragment expression");
2238 Expr = *R;
2239 }
2240 DIExpression *AddrExpr = DIExpression::get(Context&: StoreLikeInst.getContext(), Elements: {});
2241 auto *Assign = DbgVariableRecord::createLinkedDVRAssign(
2242 LinkedInstr: &StoreLikeInst, Val, Variable: VarRec.Var, Expression: Expr, Address: Dest, AddressExpression: AddrExpr, DI: VarRec.DL);
2243 (void)Assign;
2244 LLVM_DEBUG(if (Assign) errs() << " > INSERT: " << *Assign << "\n");
2245}
2246
2247#undef DEBUG_TYPE // Silence redefinition warning (from ConstantsContext.h).
2248#define DEBUG_TYPE "assignment-tracking"
2249
2250void at::trackAssignments(Function::iterator Start, Function::iterator End,
2251 const StorageToVarsMap &Vars, const DataLayout &DL,
2252 bool DebugPrints) {
2253 // Early-exit if there are no interesting variables.
2254 if (Vars.empty())
2255 return;
2256
2257 auto &Ctx = Start->getContext();
2258 auto &Module = *Start->getModule();
2259
2260 // Poison type doesn't matter, so long as it isn't void. Let's just use i1.
2261 auto *Poison = PoisonValue::get(T: Type::getInt1Ty(C&: Ctx));
2262 DIBuilder DIB(Module, /*AllowUnresolved*/ false);
2263
2264 // Scan the instructions looking for stores to local variables' storage.
2265 LLVM_DEBUG(errs() << "# Scanning instructions\n");
2266 for (auto BBI = Start; BBI != End; ++BBI) {
2267 for (Instruction &I : *BBI) {
2268
2269 std::optional<AssignmentInfo> Info;
2270 Value *ValueComponent = nullptr;
2271 Value *DestComponent = nullptr;
2272 if (auto *AI = dyn_cast<AllocaInst>(Val: &I)) {
2273 // We want to track the variable's stack home from its alloca's
2274 // position onwards so we treat it as an assignment (where the stored
2275 // value is poison).
2276 Info = getAssignmentInfo(DL, AI);
2277 ValueComponent = Poison;
2278 DestComponent = AI;
2279 } else if (auto *SI = dyn_cast<StoreInst>(Val: &I)) {
2280 Info = getAssignmentInfo(DL, SI);
2281 ValueComponent = SI->getValueOperand();
2282 DestComponent = SI->getPointerOperand();
2283 } else if (auto *MI = dyn_cast<MemTransferInst>(Val: &I)) {
2284 Info = getAssignmentInfo(DL, I: MI);
2285 // May not be able to represent this value easily.
2286 ValueComponent = Poison;
2287 DestComponent = MI->getOperand(i_nocapture: 0);
2288 } else if (auto *MI = dyn_cast<MemSetInst>(Val: &I)) {
2289 Info = getAssignmentInfo(DL, I: MI);
2290 // If we're zero-initing we can state the assigned value is zero,
2291 // otherwise use undef.
2292 auto *ConstValue = dyn_cast<ConstantInt>(Val: MI->getOperand(i_nocapture: 1));
2293 if (ConstValue && ConstValue->isZero())
2294 ValueComponent = ConstValue;
2295 else
2296 ValueComponent = Poison;
2297 DestComponent = MI->getOperand(i_nocapture: 0);
2298 } else {
2299 // Not a store-like instruction.
2300 continue;
2301 }
2302
2303 assert(ValueComponent && DestComponent);
2304 LLVM_DEBUG(errs() << "SCAN: Found store-like: " << I << "\n");
2305
2306 // Check if getAssignmentInfo failed to understand this store.
2307 if (!Info.has_value()) {
2308 LLVM_DEBUG(
2309 errs()
2310 << " | SKIP: Untrackable store (e.g. through non-const gep)\n");
2311 continue;
2312 }
2313 LLVM_DEBUG(errs() << " | BASE: " << *Info->Base << "\n");
2314
2315 // Check if the store destination is a local variable with debug info.
2316 auto LocalIt = Vars.find(Val: Info->Base);
2317 if (LocalIt == Vars.end()) {
2318 LLVM_DEBUG(
2319 errs()
2320 << " | SKIP: Base address not associated with local variable\n");
2321 continue;
2322 }
2323
2324 DIAssignID *ID =
2325 cast_or_null<DIAssignID>(Val: I.getMetadata(KindID: LLVMContext::MD_DIAssignID));
2326 if (!ID) {
2327 ID = DIAssignID::getDistinct(Context&: Ctx);
2328 I.setMetadata(KindID: LLVMContext::MD_DIAssignID, Node: ID);
2329 }
2330
2331 for (const VarRecord &R : LocalIt->second)
2332 emitDbgAssign(Info: *Info, Val: ValueComponent, Dest: DestComponent, StoreLikeInst&: I, VarRec: R, DIB);
2333 }
2334 }
2335}
2336
2337bool AssignmentTrackingPass::runOnFunction(Function &F) {
2338 // No value in assignment tracking without optimisations.
2339 if (F.hasFnAttribute(Kind: Attribute::OptimizeNone))
2340 return /*Changed*/ false;
2341
2342 bool Changed = false;
2343 auto *DL = &F.getDataLayout();
2344 // Collect a map of {backing storage : dbg.declares} (currently "backing
2345 // storage" is limited to Allocas). We'll use this to find dbg.declares to
2346 // delete after running `trackAssignments`.
2347 DenseMap<const AllocaInst *, SmallPtrSet<DbgVariableRecord *, 2>> DVRDeclares;
2348 // Create another similar map of {storage : variables} that we'll pass to
2349 // trackAssignments.
2350 StorageToVarsMap Vars;
2351 auto ProcessDeclare = [&](DbgVariableRecord &Declare) {
2352 // FIXME: trackAssignments doesn't let you specify any modifiers to the
2353 // variable (e.g. fragment) or location (e.g. offset), so we have to
2354 // leave dbg.declares with non-empty expressions in place.
2355 if (Declare.getExpression()->getNumElements() != 0)
2356 return;
2357 if (!Declare.getAddress())
2358 return;
2359 if (AllocaInst *Alloca =
2360 dyn_cast<AllocaInst>(Val: Declare.getAddress()->stripPointerCasts())) {
2361 // FIXME: Skip VLAs for now (let these variables use dbg.declares).
2362 if (!Alloca->isStaticAlloca())
2363 return;
2364 // Similarly, skip scalable vectors (use dbg.declares instead).
2365 if (auto Sz = Alloca->getAllocationSize(DL: *DL); Sz && Sz->isScalable())
2366 return;
2367 DVRDeclares[Alloca].insert(Ptr: &Declare);
2368 Vars[Alloca].insert(X: VarRecord(&Declare));
2369 }
2370 };
2371 for (auto &BB : F) {
2372 for (auto &I : BB) {
2373 for (DbgVariableRecord &DVR : filterDbgVars(R: I.getDbgRecordRange())) {
2374 if (DVR.isDbgDeclare())
2375 ProcessDeclare(DVR);
2376 }
2377 }
2378 }
2379
2380 // FIXME: Locals can be backed by caller allocas (sret, byval).
2381 // Note: trackAssignments doesn't respect dbg.declare's IR positions (as it
2382 // doesn't "understand" dbg.declares). However, this doesn't appear to break
2383 // any rules given this description of dbg.declare from
2384 // llvm/docs/SourceLevelDebugging.md:
2385 //
2386 // It is not control-dependent, meaning that if a call to llvm.dbg.declare
2387 // exists and has a valid location argument, that address is considered to
2388 // be the true home of the variable across its entire lifetime.
2389 trackAssignments(Start: F.begin(), End: F.end(), Vars, DL: *DL);
2390
2391 // Delete dbg.declares for variables now tracked with assignment tracking.
2392 for (auto &[Insts, Declares] : DVRDeclares) {
2393 auto Markers = at::getDVRAssignmentMarkers(Inst: Insts);
2394 for (auto *Declare : Declares) {
2395 // Assert that the alloca that Declare uses is now linked to a dbg.assign
2396 // describing the same variable (i.e. check that this dbg.declare has
2397 // been replaced by a dbg.assign). Use DebugVariableAggregate to Discard
2398 // the fragment part because trackAssignments may alter the
2399 // fragment. e.g. if the alloca is smaller than the variable, then
2400 // trackAssignments will create an alloca-sized fragment for the
2401 // dbg.assign.
2402 assert(llvm::any_of(Markers, [Declare](auto *Assign) {
2403 return DebugVariableAggregate(Assign) ==
2404 DebugVariableAggregate(Declare);
2405 }));
2406 // Delete Declare because the variable location is now tracked using
2407 // assignment tracking.
2408 Declare->eraseFromParent();
2409 Changed = true;
2410 }
2411 };
2412 return Changed;
2413}
2414
2415static const char *AssignmentTrackingModuleFlag =
2416 "debug-info-assignment-tracking";
2417
2418static void setAssignmentTrackingModuleFlag(Module &M) {
2419 M.setModuleFlag(Behavior: Module::ModFlagBehavior::Max, Key: AssignmentTrackingModuleFlag,
2420 Val: ConstantAsMetadata::get(
2421 C: ConstantInt::get(Ty: Type::getInt1Ty(C&: M.getContext()), V: 1)));
2422}
2423
2424static bool getAssignmentTrackingModuleFlag(const Module &M) {
2425 Metadata *Value = M.getModuleFlag(Key: AssignmentTrackingModuleFlag);
2426 return Value && !cast<ConstantAsMetadata>(Val: Value)->getValue()->isNullValue();
2427}
2428
2429bool llvm::isAssignmentTrackingEnabled(const Module &M) {
2430 return getAssignmentTrackingModuleFlag(M);
2431}
2432
2433PreservedAnalyses AssignmentTrackingPass::run(Function &F,
2434 FunctionAnalysisManager &AM) {
2435 if (!runOnFunction(F))
2436 return PreservedAnalyses::all();
2437
2438 // Record that this module uses assignment tracking. It doesn't matter that
2439 // some functions in the module may not use it - the debug info in those
2440 // functions will still be handled properly.
2441 setAssignmentTrackingModuleFlag(*F.getParent());
2442
2443 // Q: Can we return a less conservative set than just CFGAnalyses? Can we
2444 // return PreservedAnalyses::all()?
2445 PreservedAnalyses PA;
2446 PA.preserveSet<CFGAnalyses>();
2447 return PA;
2448}
2449
2450PreservedAnalyses AssignmentTrackingPass::run(Module &M,
2451 ModuleAnalysisManager &AM) {
2452 bool Changed = false;
2453 for (auto &F : M)
2454 Changed |= runOnFunction(F);
2455
2456 if (!Changed)
2457 return PreservedAnalyses::all();
2458
2459 // Record that this module uses assignment tracking.
2460 setAssignmentTrackingModuleFlag(M);
2461
2462 // Q: Can we return a less conservative set than just CFGAnalyses? Can we
2463 // return PreservedAnalyses::all()?
2464 PreservedAnalyses PA;
2465 PA.preserveSet<CFGAnalyses>();
2466 return PA;
2467}
2468
2469#undef DEBUG_TYPE
2470