LLVM 17.0.0git
Debugify.cpp
Go to the documentation of this file.
1//===- Debugify.cpp - Check debug info preservation in optimizations ------===//
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/// \file In the `synthetic` mode, the `-debugify` attaches synthetic debug info
10/// to everything. It can be used to create targeted tests for debug info
11/// preservation. In addition, when using the `original` mode, it can check
12/// original debug info preservation. The `synthetic` mode is default one.
13///
14//===----------------------------------------------------------------------===//
15
17#include "llvm/ADT/BitVector.h"
19#include "llvm/IR/DIBuilder.h"
20#include "llvm/IR/DebugInfo.h"
24#include "llvm/IR/Module.h"
26#include "llvm/Pass.h"
29#include "llvm/Support/JSON.h"
30#include <optional>
31
32#define DEBUG_TYPE "debugify"
33
34using namespace llvm;
35
36namespace {
37
38cl::opt<bool> Quiet("debugify-quiet",
39 cl::desc("Suppress verbose debugify output"));
40
41cl::opt<uint64_t> DebugifyFunctionsLimit(
42 "debugify-func-limit",
43 cl::desc("Set max number of processed functions per pass."),
44 cl::init(UINT_MAX));
45
46enum class Level {
47 Locations,
48 LocationsAndVariables
49};
50
51cl::opt<Level> DebugifyLevel(
52 "debugify-level", cl::desc("Kind of debug info to add"),
53 cl::values(clEnumValN(Level::Locations, "locations", "Locations only"),
54 clEnumValN(Level::LocationsAndVariables, "location+variables",
55 "Locations and Variables")),
56 cl::init(Level::LocationsAndVariables));
57
58raw_ostream &dbg() { return Quiet ? nulls() : errs(); }
59
60uint64_t getAllocSizeInBits(Module &M, Type *Ty) {
61 return Ty->isSized() ? M.getDataLayout().getTypeAllocSizeInBits(Ty) : 0;
62}
63
64bool isFunctionSkipped(Function &F) {
65 return F.isDeclaration() || !F.hasExactDefinition();
66}
67
68/// Find the basic block's terminating instruction.
69///
70/// Special care is needed to handle musttail and deopt calls, as these behave
71/// like (but are in fact not) terminators.
72Instruction *findTerminatingInstruction(BasicBlock &BB) {
73 if (auto *I = BB.getTerminatingMustTailCall())
74 return I;
75 if (auto *I = BB.getTerminatingDeoptimizeCall())
76 return I;
77 return BB.getTerminator();
78}
79} // end anonymous namespace
80
83 std::function<bool(DIBuilder &DIB, Function &F)> ApplyToMF) {
84 // Skip modules with debug info.
85 if (M.getNamedMetadata("llvm.dbg.cu")) {
86 dbg() << Banner << "Skipping module with debug info\n";
87 return false;
88 }
89
90 DIBuilder DIB(M);
91 LLVMContext &Ctx = M.getContext();
92 auto *Int32Ty = Type::getInt32Ty(Ctx);
93
94 // Get a DIType which corresponds to Ty.
96 auto getCachedDIType = [&](Type *Ty) -> DIType * {
97 uint64_t Size = getAllocSizeInBits(M, Ty);
98 DIType *&DTy = TypeCache[Size];
99 if (!DTy) {
100 std::string Name = "ty" + utostr(Size);
101 DTy = DIB.createBasicType(Name, Size, dwarf::DW_ATE_unsigned);
102 }
103 return DTy;
104 };
105
106 unsigned NextLine = 1;
107 unsigned NextVar = 1;
108 auto File = DIB.createFile(M.getName(), "/");
109 auto CU = DIB.createCompileUnit(dwarf::DW_LANG_C, File, "debugify",
110 /*isOptimized=*/true, "", 0);
111
112 // Visit each instruction.
113 for (Function &F : Functions) {
114 if (isFunctionSkipped(F))
115 continue;
116
117 bool InsertedDbgVal = false;
118 auto SPType =
119 DIB.createSubroutineType(DIB.getOrCreateTypeArray(std::nullopt));
121 DISubprogram::SPFlagDefinition | DISubprogram::SPFlagOptimized;
122 if (F.hasPrivateLinkage() || F.hasInternalLinkage())
123 SPFlags |= DISubprogram::SPFlagLocalToUnit;
124 auto SP = DIB.createFunction(CU, F.getName(), F.getName(), File, NextLine,
125 SPType, NextLine, DINode::FlagZero, SPFlags);
126 F.setSubprogram(SP);
127
128 // Helper that inserts a dbg.value before \p InsertBefore, copying the
129 // location (and possibly the type, if it's non-void) from \p TemplateInst.
130 auto insertDbgVal = [&](Instruction &TemplateInst,
131 Instruction *InsertBefore) {
132 std::string Name = utostr(NextVar++);
133 Value *V = &TemplateInst;
134 if (TemplateInst.getType()->isVoidTy())
136 const DILocation *Loc = TemplateInst.getDebugLoc().get();
137 auto LocalVar = DIB.createAutoVariable(SP, Name, File, Loc->getLine(),
138 getCachedDIType(V->getType()),
139 /*AlwaysPreserve=*/true);
140 DIB.insertDbgValueIntrinsic(V, LocalVar, DIB.createExpression(), Loc,
141 InsertBefore);
142 };
143
144 for (BasicBlock &BB : F) {
145 // Attach debug locations.
146 for (Instruction &I : BB)
147 I.setDebugLoc(DILocation::get(Ctx, NextLine++, 1, SP));
148
149 if (DebugifyLevel < Level::LocationsAndVariables)
150 continue;
151
152 // Inserting debug values into EH pads can break IR invariants.
153 if (BB.isEHPad())
154 continue;
155
156 // Find the terminating instruction, after which no debug values are
157 // attached.
158 Instruction *LastInst = findTerminatingInstruction(BB);
159 assert(LastInst && "Expected basic block with a terminator");
160
161 // Maintain an insertion point which can't be invalidated when updates
162 // are made.
163 BasicBlock::iterator InsertPt = BB.getFirstInsertionPt();
164 assert(InsertPt != BB.end() && "Expected to find an insertion point");
165 Instruction *InsertBefore = &*InsertPt;
166
167 // Attach debug values.
168 for (Instruction *I = &*BB.begin(); I != LastInst; I = I->getNextNode()) {
169 // Skip void-valued instructions.
170 if (I->getType()->isVoidTy())
171 continue;
172
173 // Phis and EH pads must be grouped at the beginning of the block.
174 // Only advance the insertion point when we finish visiting these.
175 if (!isa<PHINode>(I) && !I->isEHPad())
176 InsertBefore = I->getNextNode();
177
178 insertDbgVal(*I, InsertBefore);
179 InsertedDbgVal = true;
180 }
181 }
182 // Make sure we emit at least one dbg.value, otherwise MachineDebugify may
183 // not have anything to work with as it goes about inserting DBG_VALUEs.
184 // (It's common for MIR tests to be written containing skeletal IR with
185 // empty functions -- we're still interested in debugifying the MIR within
186 // those tests, and this helps with that.)
187 if (DebugifyLevel == Level::LocationsAndVariables && !InsertedDbgVal) {
188 auto *Term = findTerminatingInstruction(F.getEntryBlock());
189 insertDbgVal(*Term, Term);
190 }
191 if (ApplyToMF)
192 ApplyToMF(DIB, F);
193 DIB.finalizeSubprogram(SP);
194 }
195 DIB.finalize();
196
197 // Track the number of distinct lines and variables.
198 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.debugify");
199 auto addDebugifyOperand = [&](unsigned N) {
202 };
203 addDebugifyOperand(NextLine - 1); // Original number of lines.
204 addDebugifyOperand(NextVar - 1); // Original number of variables.
205 assert(NMD->getNumOperands() == 2 &&
206 "llvm.debugify should have exactly 2 operands!");
207
208 // Claim that this synthetic debug info is valid.
209 StringRef DIVersionKey = "Debug Info Version";
210 if (!M.getModuleFlag(DIVersionKey))
211 M.addModuleFlag(Module::Warning, DIVersionKey, DEBUG_METADATA_VERSION);
212
213 return true;
214}
215
216static bool
219 DebugInfoPerPass *DebugInfoBeforePass = nullptr,
220 StringRef NameOfWrappedPass = "") {
221 Module &M = *F.getParent();
222 auto FuncIt = F.getIterator();
224 return applyDebugifyMetadata(M, make_range(FuncIt, std::next(FuncIt)),
225 "FunctionDebugify: ", /*ApplyToMF*/ nullptr);
226 assert(DebugInfoBeforePass);
227 return collectDebugInfoMetadata(M, M.functions(), *DebugInfoBeforePass,
228 "FunctionDebugify (original debuginfo)",
229 NameOfWrappedPass);
230}
231
232static bool
235 DebugInfoPerPass *DebugInfoBeforePass = nullptr,
236 StringRef NameOfWrappedPass = "") {
238 return applyDebugifyMetadata(M, M.functions(),
239 "ModuleDebugify: ", /*ApplyToMF*/ nullptr);
240 return collectDebugInfoMetadata(M, M.functions(), *DebugInfoBeforePass,
241 "ModuleDebugify (original debuginfo)",
242 NameOfWrappedPass);
243}
244
246 bool Changed = false;
247
248 // Remove the llvm.debugify and llvm.mir.debugify module-level named metadata.
249 NamedMDNode *DebugifyMD = M.getNamedMetadata("llvm.debugify");
250 if (DebugifyMD) {
251 M.eraseNamedMetadata(DebugifyMD);
252 Changed = true;
253 }
254
255 if (auto *MIRDebugifyMD = M.getNamedMetadata("llvm.mir.debugify")) {
256 M.eraseNamedMetadata(MIRDebugifyMD);
257 Changed = true;
258 }
259
260 // Strip out all debug intrinsics and supporting metadata (subprograms, types,
261 // variables, etc).
262 Changed |= StripDebugInfo(M);
263
264 // Strip out the dead dbg.value prototype.
265 Function *DbgValF = M.getFunction("llvm.dbg.value");
266 if (DbgValF) {
267 assert(DbgValF->isDeclaration() && DbgValF->use_empty() &&
268 "Not all debug info stripped?");
269 DbgValF->eraseFromParent();
270 Changed = true;
271 }
272
273 // Strip out the module-level Debug Info Version metadata.
274 // FIXME: There must be an easier way to remove an operand from a NamedMDNode.
275 NamedMDNode *NMD = M.getModuleFlagsMetadata();
276 if (!NMD)
277 return Changed;
279 NMD->clearOperands();
280 for (MDNode *Flag : Flags) {
281 auto *Key = cast<MDString>(Flag->getOperand(1));
282 if (Key->getString() == "Debug Info Version") {
283 Changed = true;
284 continue;
285 }
286 NMD->addOperand(Flag);
287 }
288 // If we left it empty we might as well remove it.
289 if (NMD->getNumOperands() == 0)
290 NMD->eraseFromParent();
291
292 return Changed;
293}
294
297 DebugInfoPerPass &DebugInfoBeforePass,
298 StringRef Banner,
299 StringRef NameOfWrappedPass) {
300 LLVM_DEBUG(dbgs() << Banner << ": (before) " << NameOfWrappedPass << '\n');
301
302 if (!M.getNamedMetadata("llvm.dbg.cu")) {
303 dbg() << Banner << ": Skipping module without debug info\n";
304 return false;
305 }
306
307 uint64_t FunctionsCnt = DebugInfoBeforePass.DIFunctions.size();
308 // Visit each instruction.
309 for (Function &F : Functions) {
310 // Use DI collected after previous Pass (when -debugify-each is used).
311 if (DebugInfoBeforePass.DIFunctions.count(&F))
312 continue;
313
314 if (isFunctionSkipped(F))
315 continue;
316
317 // Stop collecting DI if the Functions number reached the limit.
318 if (++FunctionsCnt >= DebugifyFunctionsLimit)
319 break;
320 // Collect the DISubprogram.
321 auto *SP = F.getSubprogram();
322 DebugInfoBeforePass.DIFunctions.insert({&F, SP});
323 if (SP) {
324 LLVM_DEBUG(dbgs() << " Collecting subprogram: " << *SP << '\n');
325 for (const DINode *DN : SP->getRetainedNodes()) {
326 if (const auto *DV = dyn_cast<DILocalVariable>(DN)) {
327 DebugInfoBeforePass.DIVariables[DV] = 0;
328 }
329 }
330 }
331
332 for (BasicBlock &BB : F) {
333 // Collect debug locations (!dbg) and debug variable intrinsics.
334 for (Instruction &I : BB) {
335 // Skip PHIs.
336 if (isa<PHINode>(I))
337 continue;
338
339 // Cllect dbg.values and dbg.declare.
340 if (DebugifyLevel > Level::Locations) {
341 if (auto *DVI = dyn_cast<DbgVariableIntrinsic>(&I)) {
342 if (!SP)
343 continue;
344 // Skip inlined variables.
345 if (I.getDebugLoc().getInlinedAt())
346 continue;
347 // Skip undef values.
348 if (DVI->isKillLocation())
349 continue;
350
351 auto *Var = DVI->getVariable();
352 DebugInfoBeforePass.DIVariables[Var]++;
353 continue;
354 }
355 }
356
357 // Skip debug instructions other than dbg.value and dbg.declare.
358 if (isa<DbgInfoIntrinsic>(&I))
359 continue;
360
361 LLVM_DEBUG(dbgs() << " Collecting info for inst: " << I << '\n');
362 DebugInfoBeforePass.InstToDelete.insert({&I, &I});
363
364 const DILocation *Loc = I.getDebugLoc().get();
365 bool HasLoc = Loc != nullptr;
366 DebugInfoBeforePass.DILocations.insert({&I, HasLoc});
367 }
368 }
369 }
370
371 return true;
372}
373
374// This checks the preservation of original debug info attached to functions.
375static bool checkFunctions(const DebugFnMap &DIFunctionsBefore,
376 const DebugFnMap &DIFunctionsAfter,
377 StringRef NameOfWrappedPass,
378 StringRef FileNameFromCU, bool ShouldWriteIntoJSON,
379 llvm::json::Array &Bugs) {
380 bool Preserved = true;
381 for (const auto &F : DIFunctionsAfter) {
382 if (F.second)
383 continue;
384 auto SPIt = DIFunctionsBefore.find(F.first);
385 if (SPIt == DIFunctionsBefore.end()) {
386 if (ShouldWriteIntoJSON)
387 Bugs.push_back(llvm::json::Object({{"metadata", "DISubprogram"},
388 {"name", F.first->getName()},
389 {"action", "not-generate"}}));
390 else
391 dbg() << "ERROR: " << NameOfWrappedPass
392 << " did not generate DISubprogram for " << F.first->getName()
393 << " from " << FileNameFromCU << '\n';
394 Preserved = false;
395 } else {
396 auto SP = SPIt->second;
397 if (!SP)
398 continue;
399 // If the function had the SP attached before the pass, consider it as
400 // a debug info bug.
401 if (ShouldWriteIntoJSON)
402 Bugs.push_back(llvm::json::Object({{"metadata", "DISubprogram"},
403 {"name", F.first->getName()},
404 {"action", "drop"}}));
405 else
406 dbg() << "ERROR: " << NameOfWrappedPass << " dropped DISubprogram of "
407 << F.first->getName() << " from " << FileNameFromCU << '\n';
408 Preserved = false;
409 }
410 }
411
412 return Preserved;
413}
414
415// This checks the preservation of the original debug info attached to
416// instructions.
417static bool checkInstructions(const DebugInstMap &DILocsBefore,
418 const DebugInstMap &DILocsAfter,
419 const WeakInstValueMap &InstToDelete,
420 StringRef NameOfWrappedPass,
421 StringRef FileNameFromCU,
422 bool ShouldWriteIntoJSON,
423 llvm::json::Array &Bugs) {
424 bool Preserved = true;
425 for (const auto &L : DILocsAfter) {
426 if (L.second)
427 continue;
428 auto Instr = L.first;
429
430 // In order to avoid pointer reuse/recycling, skip the values that might
431 // have been deleted during a pass.
432 auto WeakInstrPtr = InstToDelete.find(Instr);
433 if (WeakInstrPtr != InstToDelete.end() && !WeakInstrPtr->second)
434 continue;
435
436 auto FnName = Instr->getFunction()->getName();
437 auto BB = Instr->getParent();
438 auto BBName = BB->hasName() ? BB->getName() : "no-name";
439 auto InstName = Instruction::getOpcodeName(Instr->getOpcode());
440
441 auto InstrIt = DILocsBefore.find(Instr);
442 if (InstrIt == DILocsBefore.end()) {
443 if (ShouldWriteIntoJSON)
444 Bugs.push_back(llvm::json::Object({{"metadata", "DILocation"},
445 {"fn-name", FnName.str()},
446 {"bb-name", BBName.str()},
447 {"instr", InstName},
448 {"action", "not-generate"}}));
449 else
450 dbg() << "WARNING: " << NameOfWrappedPass
451 << " did not generate DILocation for " << *Instr
452 << " (BB: " << BBName << ", Fn: " << FnName
453 << ", File: " << FileNameFromCU << ")\n";
454 Preserved = false;
455 } else {
456 if (!InstrIt->second)
457 continue;
458 // If the instr had the !dbg attached before the pass, consider it as
459 // a debug info issue.
460 if (ShouldWriteIntoJSON)
461 Bugs.push_back(llvm::json::Object({{"metadata", "DILocation"},
462 {"fn-name", FnName.str()},
463 {"bb-name", BBName.str()},
464 {"instr", InstName},
465 {"action", "drop"}}));
466 else
467 dbg() << "WARNING: " << NameOfWrappedPass << " dropped DILocation of "
468 << *Instr << " (BB: " << BBName << ", Fn: " << FnName
469 << ", File: " << FileNameFromCU << ")\n";
470 Preserved = false;
471 }
472 }
473
474 return Preserved;
475}
476
477// This checks the preservation of original debug variable intrinsics.
478static bool checkVars(const DebugVarMap &DIVarsBefore,
479 const DebugVarMap &DIVarsAfter,
480 StringRef NameOfWrappedPass, StringRef FileNameFromCU,
481 bool ShouldWriteIntoJSON, llvm::json::Array &Bugs) {
482 bool Preserved = true;
483 for (const auto &V : DIVarsBefore) {
484 auto VarIt = DIVarsAfter.find(V.first);
485 if (VarIt == DIVarsAfter.end())
486 continue;
487
488 unsigned NumOfDbgValsAfter = VarIt->second;
489
490 if (V.second > NumOfDbgValsAfter) {
491 if (ShouldWriteIntoJSON)
493 {{"metadata", "dbg-var-intrinsic"},
494 {"name", V.first->getName()},
495 {"fn-name", V.first->getScope()->getSubprogram()->getName()},
496 {"action", "drop"}}));
497 else
498 dbg() << "WARNING: " << NameOfWrappedPass
499 << " drops dbg.value()/dbg.declare() for " << V.first->getName()
500 << " from "
501 << "function " << V.first->getScope()->getSubprogram()->getName()
502 << " (file " << FileNameFromCU << ")\n";
503 Preserved = false;
504 }
505 }
506
507 return Preserved;
508}
509
510// Write the json data into the specifed file.
511static void writeJSON(StringRef OrigDIVerifyBugsReportFilePath,
512 StringRef FileNameFromCU, StringRef NameOfWrappedPass,
513 llvm::json::Array &Bugs) {
514 std::error_code EC;
515 raw_fd_ostream OS_FILE{OrigDIVerifyBugsReportFilePath, EC,
517 if (EC) {
518 errs() << "Could not open file: " << EC.message() << ", "
519 << OrigDIVerifyBugsReportFilePath << '\n';
520 return;
521 }
522
523 if (auto L = OS_FILE.lock()) {
524 OS_FILE << "{\"file\":\"" << FileNameFromCU << "\", ";
525
527 NameOfWrappedPass != "" ? NameOfWrappedPass : "no-name";
528 OS_FILE << "\"pass\":\"" << PassName << "\", ";
529
530 llvm::json::Value BugsToPrint{std::move(Bugs)};
531 OS_FILE << "\"bugs\": " << BugsToPrint;
532
533 OS_FILE << "}\n";
534 }
535 OS_FILE.close();
536}
537
540 DebugInfoPerPass &DebugInfoBeforePass,
541 StringRef Banner, StringRef NameOfWrappedPass,
542 StringRef OrigDIVerifyBugsReportFilePath) {
543 LLVM_DEBUG(dbgs() << Banner << ": (after) " << NameOfWrappedPass << '\n');
544
545 if (!M.getNamedMetadata("llvm.dbg.cu")) {
546 dbg() << Banner << ": Skipping module without debug info\n";
547 return false;
548 }
549
550 // Map the debug info holding DIs after a pass.
551 DebugInfoPerPass DebugInfoAfterPass;
552
553 // Visit each instruction.
554 for (Function &F : Functions) {
555 if (isFunctionSkipped(F))
556 continue;
557
558 // Don't process functions without DI collected before the Pass.
559 if (!DebugInfoBeforePass.DIFunctions.count(&F))
560 continue;
561 // TODO: Collect metadata other than DISubprograms.
562 // Collect the DISubprogram.
563 auto *SP = F.getSubprogram();
564 DebugInfoAfterPass.DIFunctions.insert({&F, SP});
565
566 if (SP) {
567 LLVM_DEBUG(dbgs() << " Collecting subprogram: " << *SP << '\n');
568 for (const DINode *DN : SP->getRetainedNodes()) {
569 if (const auto *DV = dyn_cast<DILocalVariable>(DN)) {
570 DebugInfoAfterPass.DIVariables[DV] = 0;
571 }
572 }
573 }
574
575 for (BasicBlock &BB : F) {
576 // Collect debug locations (!dbg) and debug variable intrinsics.
577 for (Instruction &I : BB) {
578 // Skip PHIs.
579 if (isa<PHINode>(I))
580 continue;
581
582 // Collect dbg.values and dbg.declares.
583 if (DebugifyLevel > Level::Locations) {
584 if (auto *DVI = dyn_cast<DbgVariableIntrinsic>(&I)) {
585 if (!SP)
586 continue;
587 // Skip inlined variables.
588 if (I.getDebugLoc().getInlinedAt())
589 continue;
590 // Skip undef values.
591 if (DVI->isKillLocation())
592 continue;
593
594 auto *Var = DVI->getVariable();
595 DebugInfoAfterPass.DIVariables[Var]++;
596 continue;
597 }
598 }
599
600 // Skip debug instructions other than dbg.value and dbg.declare.
601 if (isa<DbgInfoIntrinsic>(&I))
602 continue;
603
604 LLVM_DEBUG(dbgs() << " Collecting info for inst: " << I << '\n');
605
606 const DILocation *Loc = I.getDebugLoc().get();
607 bool HasLoc = Loc != nullptr;
608
609 DebugInfoAfterPass.DILocations.insert({&I, HasLoc});
610 }
611 }
612 }
613
614 // TODO: The name of the module could be read better?
615 StringRef FileNameFromCU =
616 (cast<DICompileUnit>(M.getNamedMetadata("llvm.dbg.cu")->getOperand(0)))
617 ->getFilename();
618
619 auto DIFunctionsBefore = DebugInfoBeforePass.DIFunctions;
620 auto DIFunctionsAfter = DebugInfoAfterPass.DIFunctions;
621
622 auto DILocsBefore = DebugInfoBeforePass.DILocations;
623 auto DILocsAfter = DebugInfoAfterPass.DILocations;
624
625 auto InstToDelete = DebugInfoBeforePass.InstToDelete;
626
627 auto DIVarsBefore = DebugInfoBeforePass.DIVariables;
628 auto DIVarsAfter = DebugInfoAfterPass.DIVariables;
629
630 bool ShouldWriteIntoJSON = !OrigDIVerifyBugsReportFilePath.empty();
632
633 bool ResultForFunc =
634 checkFunctions(DIFunctionsBefore, DIFunctionsAfter, NameOfWrappedPass,
635 FileNameFromCU, ShouldWriteIntoJSON, Bugs);
636 bool ResultForInsts = checkInstructions(
637 DILocsBefore, DILocsAfter, InstToDelete, NameOfWrappedPass,
638 FileNameFromCU, ShouldWriteIntoJSON, Bugs);
639
640 bool ResultForVars = checkVars(DIVarsBefore, DIVarsAfter, NameOfWrappedPass,
641 FileNameFromCU, ShouldWriteIntoJSON, Bugs);
642
643 bool Result = ResultForFunc && ResultForInsts && ResultForVars;
644
645 StringRef ResultBanner = NameOfWrappedPass != "" ? NameOfWrappedPass : Banner;
646 if (ShouldWriteIntoJSON && !Bugs.empty())
647 writeJSON(OrigDIVerifyBugsReportFilePath, FileNameFromCU, NameOfWrappedPass,
648 Bugs);
649
650 if (Result)
651 dbg() << ResultBanner << ": PASS\n";
652 else
653 dbg() << ResultBanner << ": FAIL\n";
654
655 // In the case of the `debugify-each`, no need to go over all the instructions
656 // again in the collectDebugInfoMetadata(), since as an input we can use
657 // the debugging information from the previous pass.
658 DebugInfoBeforePass = DebugInfoAfterPass;
659
660 LLVM_DEBUG(dbgs() << "\n\n");
661 return Result;
662}
663
664namespace {
665/// Return true if a mis-sized diagnostic is issued for \p DVI.
666bool diagnoseMisSizedDbgValue(Module &M, DbgValueInst *DVI) {
667 // The size of a dbg.value's value operand should match the size of the
668 // variable it corresponds to.
669 //
670 // TODO: This, along with a check for non-null value operands, should be
671 // promoted to verifier failures.
672
673 // For now, don't try to interpret anything more complicated than an empty
674 // DIExpression. Eventually we should try to handle OP_deref and fragments.
675 if (DVI->getExpression()->getNumElements())
676 return false;
677
678 Value *V = DVI->getVariableLocationOp(0);
679 if (!V)
680 return false;
681
682 Type *Ty = V->getType();
683 uint64_t ValueOperandSize = getAllocSizeInBits(M, Ty);
684 std::optional<uint64_t> DbgVarSize = DVI->getFragmentSizeInBits();
685 if (!ValueOperandSize || !DbgVarSize)
686 return false;
687
688 bool HasBadSize = false;
689 if (Ty->isIntegerTy()) {
690 auto Signedness = DVI->getVariable()->getSignedness();
691 if (Signedness && *Signedness == DIBasicType::Signedness::Signed)
692 HasBadSize = ValueOperandSize < *DbgVarSize;
693 } else {
694 HasBadSize = ValueOperandSize != *DbgVarSize;
695 }
696
697 if (HasBadSize) {
698 dbg() << "ERROR: dbg.value operand has size " << ValueOperandSize
699 << ", but its variable has size " << *DbgVarSize << ": ";
700 DVI->print(dbg());
701 dbg() << "\n";
702 }
703 return HasBadSize;
704}
705
706bool checkDebugifyMetadata(Module &M,
708 StringRef NameOfWrappedPass, StringRef Banner,
709 bool Strip, DebugifyStatsMap *StatsMap) {
710 // Skip modules without debugify metadata.
711 NamedMDNode *NMD = M.getNamedMetadata("llvm.debugify");
712 if (!NMD) {
713 dbg() << Banner << ": Skipping module without debugify metadata\n";
714 return false;
715 }
716
717 auto getDebugifyOperand = [&](unsigned Idx) -> unsigned {
718 return mdconst::extract<ConstantInt>(NMD->getOperand(Idx)->getOperand(0))
719 ->getZExtValue();
720 };
721 assert(NMD->getNumOperands() == 2 &&
722 "llvm.debugify should have exactly 2 operands!");
723 unsigned OriginalNumLines = getDebugifyOperand(0);
724 unsigned OriginalNumVars = getDebugifyOperand(1);
725 bool HasErrors = false;
726
727 // Track debug info loss statistics if able.
728 DebugifyStatistics *Stats = nullptr;
729 if (StatsMap && !NameOfWrappedPass.empty())
730 Stats = &StatsMap->operator[](NameOfWrappedPass);
731
732 BitVector MissingLines{OriginalNumLines, true};
733 BitVector MissingVars{OriginalNumVars, true};
734 for (Function &F : Functions) {
735 if (isFunctionSkipped(F))
736 continue;
737
738 // Find missing lines.
739 for (Instruction &I : instructions(F)) {
740 if (isa<DbgValueInst>(&I))
741 continue;
742
743 auto DL = I.getDebugLoc();
744 if (DL && DL.getLine() != 0) {
745 MissingLines.reset(DL.getLine() - 1);
746 continue;
747 }
748
749 if (!isa<PHINode>(&I) && !DL) {
750 dbg() << "WARNING: Instruction with empty DebugLoc in function ";
751 dbg() << F.getName() << " --";
752 I.print(dbg());
753 dbg() << "\n";
754 }
755 }
756
757 // Find missing variables and mis-sized debug values.
758 for (Instruction &I : instructions(F)) {
759 auto *DVI = dyn_cast<DbgValueInst>(&I);
760 if (!DVI)
761 continue;
762
763 unsigned Var = ~0U;
764 (void)to_integer(DVI->getVariable()->getName(), Var, 10);
765 assert(Var <= OriginalNumVars && "Unexpected name for DILocalVariable");
766 bool HasBadSize = diagnoseMisSizedDbgValue(M, DVI);
767 if (!HasBadSize)
768 MissingVars.reset(Var - 1);
769 HasErrors |= HasBadSize;
770 }
771 }
772
773 // Print the results.
774 for (unsigned Idx : MissingLines.set_bits())
775 dbg() << "WARNING: Missing line " << Idx + 1 << "\n";
776
777 for (unsigned Idx : MissingVars.set_bits())
778 dbg() << "WARNING: Missing variable " << Idx + 1 << "\n";
779
780 // Update DI loss statistics.
781 if (Stats) {
782 Stats->NumDbgLocsExpected += OriginalNumLines;
783 Stats->NumDbgLocsMissing += MissingLines.count();
784 Stats->NumDbgValuesExpected += OriginalNumVars;
785 Stats->NumDbgValuesMissing += MissingVars.count();
786 }
787
788 dbg() << Banner;
789 if (!NameOfWrappedPass.empty())
790 dbg() << " [" << NameOfWrappedPass << "]";
791 dbg() << ": " << (HasErrors ? "FAIL" : "PASS") << '\n';
792
793 // Strip debugify metadata if required.
794 if (Strip)
795 return stripDebugifyMetadata(M);
796
797 return false;
798}
799
800/// ModulePass for attaching synthetic debug info to everything, used with the
801/// legacy module pass manager.
802struct DebugifyModulePass : public ModulePass {
803 bool runOnModule(Module &M) override {
804 return applyDebugify(M, Mode, DebugInfoBeforePass, NameOfWrappedPass);
805 }
806
807 DebugifyModulePass(enum DebugifyMode Mode = DebugifyMode::SyntheticDebugInfo,
808 StringRef NameOfWrappedPass = "",
809 DebugInfoPerPass *DebugInfoBeforePass = nullptr)
810 : ModulePass(ID), NameOfWrappedPass(NameOfWrappedPass),
811 DebugInfoBeforePass(DebugInfoBeforePass), Mode(Mode) {}
812
813 void getAnalysisUsage(AnalysisUsage &AU) const override {
814 AU.setPreservesAll();
815 }
816
817 static char ID; // Pass identification.
818
819private:
820 StringRef NameOfWrappedPass;
821 DebugInfoPerPass *DebugInfoBeforePass;
822 enum DebugifyMode Mode;
823};
824
825/// FunctionPass for attaching synthetic debug info to instructions within a
826/// single function, used with the legacy module pass manager.
827struct DebugifyFunctionPass : public FunctionPass {
828 bool runOnFunction(Function &F) override {
829 return applyDebugify(F, Mode, DebugInfoBeforePass, NameOfWrappedPass);
830 }
831
832 DebugifyFunctionPass(
833 enum DebugifyMode Mode = DebugifyMode::SyntheticDebugInfo,
834 StringRef NameOfWrappedPass = "",
835 DebugInfoPerPass *DebugInfoBeforePass = nullptr)
836 : FunctionPass(ID), NameOfWrappedPass(NameOfWrappedPass),
837 DebugInfoBeforePass(DebugInfoBeforePass), Mode(Mode) {}
838
839 void getAnalysisUsage(AnalysisUsage &AU) const override {
840 AU.setPreservesAll();
841 }
842
843 static char ID; // Pass identification.
844
845private:
846 StringRef NameOfWrappedPass;
847 DebugInfoPerPass *DebugInfoBeforePass;
848 enum DebugifyMode Mode;
849};
850
851/// ModulePass for checking debug info inserted by -debugify, used with the
852/// legacy module pass manager.
853struct CheckDebugifyModulePass : public ModulePass {
854 bool runOnModule(Module &M) override {
855 if (Mode == DebugifyMode::SyntheticDebugInfo)
856 return checkDebugifyMetadata(M, M.functions(), NameOfWrappedPass,
857 "CheckModuleDebugify", Strip, StatsMap);
859 M, M.functions(), *DebugInfoBeforePass,
860 "CheckModuleDebugify (original debuginfo)", NameOfWrappedPass,
861 OrigDIVerifyBugsReportFilePath);
862 }
863
864 CheckDebugifyModulePass(
865 bool Strip = false, StringRef NameOfWrappedPass = "",
866 DebugifyStatsMap *StatsMap = nullptr,
867 enum DebugifyMode Mode = DebugifyMode::SyntheticDebugInfo,
868 DebugInfoPerPass *DebugInfoBeforePass = nullptr,
869 StringRef OrigDIVerifyBugsReportFilePath = "")
870 : ModulePass(ID), NameOfWrappedPass(NameOfWrappedPass),
871 OrigDIVerifyBugsReportFilePath(OrigDIVerifyBugsReportFilePath),
872 StatsMap(StatsMap), DebugInfoBeforePass(DebugInfoBeforePass), Mode(Mode),
873 Strip(Strip) {}
874
875 void getAnalysisUsage(AnalysisUsage &AU) const override {
876 AU.setPreservesAll();
877 }
878
879 static char ID; // Pass identification.
880
881private:
882 StringRef NameOfWrappedPass;
883 StringRef OrigDIVerifyBugsReportFilePath;
884 DebugifyStatsMap *StatsMap;
885 DebugInfoPerPass *DebugInfoBeforePass;
886 enum DebugifyMode Mode;
887 bool Strip;
888};
889
890/// FunctionPass for checking debug info inserted by -debugify-function, used
891/// with the legacy module pass manager.
892struct CheckDebugifyFunctionPass : public FunctionPass {
893 bool runOnFunction(Function &F) override {
894 Module &M = *F.getParent();
895 auto FuncIt = F.getIterator();
896 if (Mode == DebugifyMode::SyntheticDebugInfo)
897 return checkDebugifyMetadata(M, make_range(FuncIt, std::next(FuncIt)),
898 NameOfWrappedPass, "CheckFunctionDebugify",
899 Strip, StatsMap);
901 M, make_range(FuncIt, std::next(FuncIt)), *DebugInfoBeforePass,
902 "CheckFunctionDebugify (original debuginfo)", NameOfWrappedPass,
903 OrigDIVerifyBugsReportFilePath);
904 }
905
906 CheckDebugifyFunctionPass(
907 bool Strip = false, StringRef NameOfWrappedPass = "",
908 DebugifyStatsMap *StatsMap = nullptr,
909 enum DebugifyMode Mode = DebugifyMode::SyntheticDebugInfo,
910 DebugInfoPerPass *DebugInfoBeforePass = nullptr,
911 StringRef OrigDIVerifyBugsReportFilePath = "")
912 : FunctionPass(ID), NameOfWrappedPass(NameOfWrappedPass),
913 OrigDIVerifyBugsReportFilePath(OrigDIVerifyBugsReportFilePath),
914 StatsMap(StatsMap), DebugInfoBeforePass(DebugInfoBeforePass), Mode(Mode),
915 Strip(Strip) {}
916
917 void getAnalysisUsage(AnalysisUsage &AU) const override {
918 AU.setPreservesAll();
919 }
920
921 static char ID; // Pass identification.
922
923private:
924 StringRef NameOfWrappedPass;
925 StringRef OrigDIVerifyBugsReportFilePath;
926 DebugifyStatsMap *StatsMap;
927 DebugInfoPerPass *DebugInfoBeforePass;
928 enum DebugifyMode Mode;
929 bool Strip;
930};
931
932} // end anonymous namespace
933
935 std::error_code EC;
936 raw_fd_ostream OS{Path, EC};
937 if (EC) {
938 errs() << "Could not open file: " << EC.message() << ", " << Path << '\n';
939 return;
940 }
941
942 OS << "Pass Name" << ',' << "# of missing debug values" << ','
943 << "# of missing locations" << ',' << "Missing/Expected value ratio" << ','
944 << "Missing/Expected location ratio" << '\n';
945 for (const auto &Entry : Map) {
946 StringRef Pass = Entry.first;
947 DebugifyStatistics Stats = Entry.second;
948
949 OS << Pass << ',' << Stats.NumDbgValuesMissing << ','
950 << Stats.NumDbgLocsMissing << ',' << Stats.getMissingValueRatio() << ','
951 << Stats.getEmptyLocationRatio() << '\n';
952 }
953}
954
956 llvm::StringRef NameOfWrappedPass,
957 DebugInfoPerPass *DebugInfoBeforePass) {
959 return new DebugifyModulePass();
960 assert(Mode == DebugifyMode::OriginalDebugInfo && "Must be original mode");
961 return new DebugifyModulePass(Mode, NameOfWrappedPass, DebugInfoBeforePass);
962}
963
966 llvm::StringRef NameOfWrappedPass,
967 DebugInfoPerPass *DebugInfoBeforePass) {
969 return new DebugifyFunctionPass();
970 assert(Mode == DebugifyMode::OriginalDebugInfo && "Must be original mode");
971 return new DebugifyFunctionPass(Mode, NameOfWrappedPass, DebugInfoBeforePass);
972}
973
975 if (Mode == DebugifyMode::SyntheticDebugInfo)
976 applyDebugifyMetadata(M, M.functions(),
977 "ModuleDebugify: ", /*ApplyToMF*/ nullptr);
978 else
979 collectDebugInfoMetadata(M, M.functions(), *DebugInfoBeforePass,
980 "ModuleDebugify (original debuginfo)",
981 NameOfWrappedPass);
982 return PreservedAnalyses::all();
983}
984
986 bool Strip, StringRef NameOfWrappedPass, DebugifyStatsMap *StatsMap,
987 enum DebugifyMode Mode, DebugInfoPerPass *DebugInfoBeforePass,
988 StringRef OrigDIVerifyBugsReportFilePath) {
990 return new CheckDebugifyModulePass(Strip, NameOfWrappedPass, StatsMap);
991 assert(Mode == DebugifyMode::OriginalDebugInfo && "Must be original mode");
992 return new CheckDebugifyModulePass(false, NameOfWrappedPass, nullptr, Mode,
993 DebugInfoBeforePass,
994 OrigDIVerifyBugsReportFilePath);
995}
996
998 bool Strip, StringRef NameOfWrappedPass, DebugifyStatsMap *StatsMap,
999 enum DebugifyMode Mode, DebugInfoPerPass *DebugInfoBeforePass,
1000 StringRef OrigDIVerifyBugsReportFilePath) {
1002 return new CheckDebugifyFunctionPass(Strip, NameOfWrappedPass, StatsMap);
1003 assert(Mode == DebugifyMode::OriginalDebugInfo && "Must be original mode");
1004 return new CheckDebugifyFunctionPass(false, NameOfWrappedPass, nullptr, Mode,
1005 DebugInfoBeforePass,
1006 OrigDIVerifyBugsReportFilePath);
1007}
1008
1011 if (Mode == DebugifyMode::SyntheticDebugInfo)
1012 checkDebugifyMetadata(M, M.functions(), NameOfWrappedPass,
1013 "CheckModuleDebugify", Strip, StatsMap);
1014 else
1016 M, M.functions(), *DebugInfoBeforePass,
1017 "CheckModuleDebugify (original debuginfo)", NameOfWrappedPass,
1018 OrigDIVerifyBugsReportFilePath);
1019 return PreservedAnalyses::all();
1020}
1021
1022static bool isIgnoredPass(StringRef PassID) {
1023 return isSpecialPass(PassID, {"PassManager", "PassAdaptor",
1024 "AnalysisManagerProxy", "PrintFunctionPass",
1025 "PrintModulePass", "BitcodeWriterPass",
1026 "ThinLTOBitcodeWriterPass", "VerifierPass"});
1027}
1028
1032 if (isIgnoredPass(P))
1033 return;
1034 if (const auto **F = any_cast<const Function *>(&IR))
1035 applyDebugify(*const_cast<Function *>(*F),
1036 Mode, DebugInfoBeforePass, P);
1037 else if (const auto **M = any_cast<const Module *>(&IR))
1038 applyDebugify(*const_cast<Module *>(*M),
1039 Mode, DebugInfoBeforePass, P);
1040 });
1042 const PreservedAnalyses &PassPA) {
1043 if (isIgnoredPass(P))
1044 return;
1045 if (const auto **CF = any_cast<const Function *>(&IR)) {
1046 auto &F = *const_cast<Function *>(*CF);
1047 Module &M = *F.getParent();
1048 auto It = F.getIterator();
1049 if (Mode == DebugifyMode::SyntheticDebugInfo)
1050 checkDebugifyMetadata(M, make_range(It, std::next(It)), P,
1051 "CheckFunctionDebugify", /*Strip=*/true, DIStatsMap);
1052 else
1054 M, make_range(It, std::next(It)), *DebugInfoBeforePass,
1055 "CheckModuleDebugify (original debuginfo)",
1056 P, OrigDIVerifyBugsReportFilePath);
1057 } else if (const auto **CM = any_cast<const Module *>(&IR)) {
1058 auto &M = *const_cast<Module *>(*CM);
1059 if (Mode == DebugifyMode::SyntheticDebugInfo)
1060 checkDebugifyMetadata(M, M.functions(), P, "CheckModuleDebugify",
1061 /*Strip=*/true, DIStatsMap);
1062 else
1064 M, M.functions(), *DebugInfoBeforePass,
1065 "CheckModuleDebugify (original debuginfo)",
1066 P, OrigDIVerifyBugsReportFilePath);
1067 }
1068 });
1069}
1070
1071char DebugifyModulePass::ID = 0;
1073 "Attach debug info to everything");
1074
1075char CheckDebugifyModulePass::ID = 0;
1077 CDM("check-debugify", "Check debug info from -debugify");
1078
1079char DebugifyFunctionPass::ID = 0;
1080static RegisterPass<DebugifyFunctionPass> DF("debugify-function",
1081 "Attach debug info to a function");
1082
1083char CheckDebugifyFunctionPass::ID = 0;
1085 CDF("check-debugify-function", "Check debug info from -debugify-function");
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file implements the BitVector class.
#define clEnumValN(ENUMVAL, FLAGNAME, DESC)
Definition: CommandLine.h:678
static DISubprogram * getSubprogram(bool IsDistinct, Ts &&...Args)
Definition: DIBuilder.cpp:842
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
#define LLVM_DEBUG(X)
Definition: Debug.h:101
static RegisterPass< CheckDebugifyModulePass > CDM("check-debugify", "Check debug info from -debugify")
ModulePass * createDebugifyModulePass(enum DebugifyMode Mode, llvm::StringRef NameOfWrappedPass, DebugInfoPerPass *DebugInfoBeforePass)
Definition: Debugify.cpp:955
FunctionPass * createDebugifyFunctionPass(enum DebugifyMode Mode, llvm::StringRef NameOfWrappedPass, DebugInfoPerPass *DebugInfoBeforePass)
Definition: Debugify.cpp:965
static bool isIgnoredPass(StringRef PassID)
Definition: Debugify.cpp:1022
static bool applyDebugify(Function &F, enum DebugifyMode Mode=DebugifyMode::SyntheticDebugInfo, DebugInfoPerPass *DebugInfoBeforePass=nullptr, StringRef NameOfWrappedPass="")
Definition: Debugify.cpp:217
ModulePass * createCheckDebugifyModulePass(bool Strip, StringRef NameOfWrappedPass, DebugifyStatsMap *StatsMap, enum DebugifyMode Mode, DebugInfoPerPass *DebugInfoBeforePass, StringRef OrigDIVerifyBugsReportFilePath)
Definition: Debugify.cpp:985
static void writeJSON(StringRef OrigDIVerifyBugsReportFilePath, StringRef FileNameFromCU, StringRef NameOfWrappedPass, llvm::json::Array &Bugs)
Definition: Debugify.cpp:511
static RegisterPass< DebugifyFunctionPass > DF("debugify-function", "Attach debug info to a function")
static RegisterPass< DebugifyModulePass > DM("debugify", "Attach debug info to everything")
static bool checkFunctions(const DebugFnMap &DIFunctionsBefore, const DebugFnMap &DIFunctionsAfter, StringRef NameOfWrappedPass, StringRef FileNameFromCU, bool ShouldWriteIntoJSON, llvm::json::Array &Bugs)
Definition: Debugify.cpp:375
static RegisterPass< CheckDebugifyFunctionPass > CDF("check-debugify-function", "Check debug info from -debugify-function")
FunctionPass * createCheckDebugifyFunctionPass(bool Strip, StringRef NameOfWrappedPass, DebugifyStatsMap *StatsMap, enum DebugifyMode Mode, DebugInfoPerPass *DebugInfoBeforePass, StringRef OrigDIVerifyBugsReportFilePath)
Definition: Debugify.cpp:997
static bool checkVars(const DebugVarMap &DIVarsBefore, const DebugVarMap &DIVarsAfter, StringRef NameOfWrappedPass, StringRef FileNameFromCU, bool ShouldWriteIntoJSON, llvm::json::Array &Bugs)
Definition: Debugify.cpp:478
static bool checkInstructions(const DebugInstMap &DILocsBefore, const DebugInstMap &DILocsAfter, const WeakInstValueMap &InstToDelete, StringRef NameOfWrappedPass, StringRef FileNameFromCU, bool ShouldWriteIntoJSON, llvm::json::Array &Bugs)
Definition: Debugify.cpp:417
DebugifyMode
Used to check whether we track synthetic or original debug info.
Definition: Debugify.h:93
std::string Name
uint64_t Size
static SmallString< 128 > getFilename(const DISubprogram *SP)
Extract a filename for a DISubprogram.
This file supports working with JSON data.
Statically lint checks LLVM IR
Definition: Lint.cpp:746
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
block placement Basic Block Placement Stats
Module.h This file contains the declarations for the Module class.
print must be executed print the must be executed context for all instructions
IntegerType * Int32Ty
#define P(N)
PassInstrumentationCallbacks PIC
This file defines the Pass Instrumentation classes that provide instrumentation points into the pass ...
static StringRef getName(Value *V)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some functions that are useful when dealing with strings.
static const char PassName[]
llvm::PreservedAnalyses run(llvm::Module &M, llvm::ModuleAnalysisManager &AM)
Definition: Debugify.cpp:1009
llvm::PreservedAnalyses run(llvm::Module &M, llvm::ModuleAnalysisManager &AM)
Definition: Debugify.cpp:974
A container for analyses that lazily runs them and caches their results.
Definition: PassManager.h:620
Represent the analysis usage information of a pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
Definition: Any.h:28
LLVM Basic Block Representation.
Definition: BasicBlock.h:56
const CallInst * getTerminatingDeoptimizeCall() const
Returns the call instruction calling @llvm.experimental.deoptimize prior to the terminating return in...
Definition: BasicBlock.cpp:181
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:87
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.h:127
const CallInst * getTerminatingMustTailCall() const
Returns the call instruction marked 'musttail' prior to the terminating return instruction of this ba...
Definition: BasicBlock.cpp:150
static Constant * get(Type *Ty, uint64_t V, bool IsSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:887
void finalize()
Construct any deferred debug info descriptors.
Definition: DIBuilder.cpp:79
DISubroutineType * createSubroutineType(DITypeRefArray ParameterTypes, DINode::DIFlags Flags=DINode::FlagZero, unsigned CC=0)
Create subroutine type.
Definition: DIBuilder.cpp:548
void finalizeSubprogram(DISubprogram *SP)
Finalize a specific subprogram - no new variables may be added to this subprogram afterwards.
Definition: DIBuilder.cpp:59
DICompileUnit * createCompileUnit(unsigned Lang, DIFile *File, StringRef Producer, bool isOptimized, StringRef Flags, unsigned RV, StringRef SplitName=StringRef(), DICompileUnit::DebugEmissionKind Kind=DICompileUnit::DebugEmissionKind::FullDebug, uint64_t DWOId=0, bool SplitDebugInlining=true, bool DebugInfoForProfiling=false, DICompileUnit::DebugNameTableKind NameTableKind=DICompileUnit::DebugNameTableKind::Default, bool RangesBaseAddress=false, StringRef SysRoot={}, StringRef SDK={})
A CompileUnit provides an anchor for all debugging information generated during this instance of comp...
Definition: DIBuilder.cpp:151
DISubprogram * createFunction(DIScope *Scope, StringRef Name, StringRef LinkageName, DIFile *File, unsigned LineNo, DISubroutineType *Ty, unsigned ScopeLine, DINode::DIFlags Flags=DINode::FlagZero, DISubprogram::DISPFlags SPFlags=DISubprogram::SPFlagZero, DITemplateParameterArray TParams=nullptr, DISubprogram *Decl=nullptr, DITypeArray ThrownTypes=nullptr, DINodeArray Annotations=nullptr, StringRef TargetFuncName="")
Create a new descriptor for the specified subprogram.
Definition: DIBuilder.cpp:848
DIExpression * createExpression(ArrayRef< uint64_t > Addr=std::nullopt)
Create a new descriptor for the specified variable which has a complex address expression for its add...
Definition: DIBuilder.cpp:837
DIBasicType * createBasicType(StringRef Name, uint64_t SizeInBits, unsigned Encoding, DINode::DIFlags Flags=DINode::FlagZero)
Create debugging information entry for a basic type.
Definition: DIBuilder.cpp:283
DITypeRefArray getOrCreateTypeArray(ArrayRef< Metadata * > Elements)
Get a DITypeRefArray, create one if required.
Definition: DIBuilder.cpp:689
DILocalVariable * createAutoVariable(DIScope *Scope, StringRef Name, DIFile *File, unsigned LineNo, DIType *Ty, bool AlwaysPreserve=false, DINode::DIFlags Flags=DINode::FlagZero, uint32_t AlignInBits=0)
Create a new descriptor for an auto variable.
Definition: DIBuilder.cpp:799
DIFile * createFile(StringRef Filename, StringRef Directory, std::optional< DIFile::ChecksumInfo< StringRef > > Checksum=std::nullopt, std::optional< StringRef > Source=std::nullopt)
Create a file descriptor to hold debugging information for a file.
Definition: DIBuilder.cpp:231
unsigned getNumElements() const
Debug location.
Tagged DWARF-like metadata node.
DISPFlags
Debug info subprogram flags.
Base class for types.
std::optional< DIBasicType::Signedness > getSignedness() const
Return the signedness of this variable's type, or std::nullopt if this type is neither signed nor uns...
StringRef getName() const
This represents the llvm.dbg.value instruction.
Value * getVariableLocationOp(unsigned OpIdx) const
DILocalVariable * getVariable() const
std::optional< uint64_t > getFragmentSizeInBits() const
Get the size (in bits) of the variable, or fragment of the variable that is described.
DIExpression * getExpression() const
DILocation * get() const
Get the underlying DILocation.
Definition: DebugLoc.cpp:20
void registerCallbacks(PassInstrumentationCallbacks &PIC)
Definition: Debugify.cpp:1029
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:308
virtual bool runOnFunction(Function &F)=0
runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
void eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing module and deletes it.
Definition: Function.cpp:362
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:275
const DebugLoc & getDebugLoc() const
Return the debug location for this node as a DebugLoc.
Definition: Instruction.h:358
const char * getOpcodeName() const
Definition: Instruction.h:170
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
Metadata node.
Definition: Metadata.h:943
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1291
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1399
size_type count(const KeyT &Key) const
Definition: MapVector.h:143
iterator end()
Definition: MapVector.h:72
iterator find(const KeyT &Key)
Definition: MapVector.h:148
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition: MapVector.h:118
size_type size() const
Definition: MapVector.h:61
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:248
virtual bool runOnModule(Module &M)=0
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
@ Warning
Emits a warning if two values disagree.
Definition: Module.h:122
A tuple of MDNodes.
Definition: Metadata.h:1587
void eraseFromParent()
Drop all references and remove the node from parent module.
Definition: Metadata.cpp:1228
MDNode * getOperand(unsigned i) const
Definition: Metadata.cpp:1215
unsigned getNumOperands() const
Definition: Metadata.cpp:1211
void clearOperands()
Drop all references to this node's operands.
Definition: Metadata.cpp:1230
iterator_range< op_iterator > operands()
Definition: Metadata.h:1683
void addOperand(MDNode *M)
Definition: Metadata.cpp:1221
This class manages callbacks registration, as well as provides a way for PassInstrumentation to pass ...
void registerBeforeNonSkippedPassCallback(CallableT C)
void registerAfterPassCallback(CallableT C, bool ToFront=false)
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:91
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:98
A set of analyses that are preserved following a run of a transformation pass.
Definition: PassManager.h:152
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition: PassManager.h:158
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition: Type.h:295
static IntegerType * getInt32Ty(LLVMContext &C)
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:222
bool isVoidTy() const
Return true if this is 'void'.
Definition: Type.h:140
static ConstantAsMetadata * getConstant(Value *C)
Definition: Metadata.h:366
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
void print(raw_ostream &O, bool IsForDebug=false) const
Implement operator<< on Value.
Definition: AsmWriter.cpp:4695
bool use_empty() const
Definition: Value.h:344
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:308
A range adaptor for a pair of iterators.
An Array is a JSON array, which contains heterogeneous JSON values.
Definition: JSON.h:158
bool empty() const
Definition: JSON.h:526
void push_back(const Value &E)
Definition: JSON.h:531
An Object is a JSON object, which maps strings to heterogenous JSON values.
Definition: JSON.h:92
A Value is an JSON value of unknown type.
Definition: JSON.h:282
A raw_ostream that writes to a file descriptor.
Definition: raw_ostream.h:454
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
ValuesClass values(OptsTy... Options)
Helper to build a ValuesClass by forwarding a variable number of arguments as an initializer list to ...
Definition: CommandLine.h:703
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:445
@ OF_TextWithCRLF
The file should be opened in text mode and use a carriage linefeed '\r '.
Definition: FileSystem.h:770
@ OF_Append
The file should be opened in append mode.
Definition: FileSystem.h:773
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
bool applyDebugifyMetadata(Module &M, iterator_range< Module::iterator > Functions, StringRef Banner, std::function< bool(DIBuilder &, Function &)> ApplyToMF)
Add synthesized debug information to a module.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
bool stripDebugifyMetadata(Module &M)
Strip out all of the metadata and debug info inserted by debugify.
Definition: Debugify.cpp:245
void exportDebugifyStats(StringRef Path, const DebugifyStatsMap &Map)
Definition: Debugify.cpp:934
bool collectDebugInfoMetadata(Module &M, iterator_range< Module::iterator > Functions, DebugInfoPerPass &DebugInfoBeforePass, StringRef Banner, StringRef NameOfWrappedPass)
Collect original debug information before a pass.
Definition: Debugify.cpp:295
bool checkDebugInfoMetadata(Module &M, iterator_range< Module::iterator > Functions, DebugInfoPerPass &DebugInfoBeforePass, StringRef Banner, StringRef NameOfWrappedPass, StringRef OrigDIVerifyBugsReportFilePath)
Check original debug information after a pass.
Definition: Debugify.cpp:538
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
bool isSpecialPass(StringRef PassID, const std::vector< StringRef > &Specials)
raw_ostream & nulls()
This returns a reference to a raw_ostream which simply discards output.
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
bool StripDebugInfo(Module &M)
Strip debug info in the module if it exists.
Definition: DebugInfo.cpp:557
@ DEBUG_METADATA_VERSION
Definition: Metadata.h:51
#define N
Used to track the Debug Info Metadata information.
Definition: Debugify.h:34
DebugInstMap DILocations
Definition: Debugify.h:38
DebugFnMap DIFunctions
Definition: Debugify.h:36
DebugVarMap DIVariables
Definition: Debugify.h:43
WeakInstValueMap InstToDelete
Definition: Debugify.h:41
Track how much debugify information (in the synthetic mode only) has been lost.
Definition: Debugify.h:121
RegisterPass<t> template - This template class is used to notify the system that a Pass is available ...
Definition: PassSupport.h:109