xref: /llvm-project/llvm/lib/Transforms/Utils/Debugify.cpp (revision 91ea247039dbcc756107616d39baecbb3a4bfb7e)
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 
16 #include "llvm/Transforms/Utils/Debugify.h"
17 #include "llvm/ADT/BitVector.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/IR/DIBuilder.h"
20 #include "llvm/IR/DebugInfo.h"
21 #include "llvm/IR/InstIterator.h"
22 #include "llvm/IR/Instructions.h"
23 #include "llvm/IR/IntrinsicInst.h"
24 #include "llvm/IR/Module.h"
25 #include "llvm/IR/PassInstrumentation.h"
26 #include "llvm/Pass.h"
27 #include "llvm/Support/CommandLine.h"
28 #include "llvm/Support/FileSystem.h"
29 #include "llvm/Support/JSON.h"
30 
31 #define DEBUG_TYPE "debugify"
32 
33 using namespace llvm;
34 
35 namespace {
36 
37 cl::opt<bool> Quiet("debugify-quiet",
38                     cl::desc("Suppress verbose debugify output"));
39 
40 enum class Level {
41   Locations,
42   LocationsAndVariables
43 };
44 
45 cl::opt<Level> DebugifyLevel(
46     "debugify-level", cl::desc("Kind of debug info to add"),
47     cl::values(clEnumValN(Level::Locations, "locations", "Locations only"),
48                clEnumValN(Level::LocationsAndVariables, "location+variables",
49                           "Locations and Variables")),
50     cl::init(Level::LocationsAndVariables));
51 
52 raw_ostream &dbg() { return Quiet ? nulls() : errs(); }
53 
54 uint64_t getAllocSizeInBits(Module &M, Type *Ty) {
55   return Ty->isSized() ? M.getDataLayout().getTypeAllocSizeInBits(Ty) : 0;
56 }
57 
58 bool isFunctionSkipped(Function &F) {
59   return F.isDeclaration() || !F.hasExactDefinition();
60 }
61 
62 /// Find the basic block's terminating instruction.
63 ///
64 /// Special care is needed to handle musttail and deopt calls, as these behave
65 /// like (but are in fact not) terminators.
66 Instruction *findTerminatingInstruction(BasicBlock &BB) {
67   if (auto *I = BB.getTerminatingMustTailCall())
68     return I;
69   if (auto *I = BB.getTerminatingDeoptimizeCall())
70     return I;
71   return BB.getTerminator();
72 }
73 } // end anonymous namespace
74 
75 bool llvm::applyDebugifyMetadata(
76     Module &M, iterator_range<Module::iterator> Functions, StringRef Banner,
77     std::function<bool(DIBuilder &DIB, Function &F)> ApplyToMF) {
78   // Skip modules with debug info.
79   if (M.getNamedMetadata("llvm.dbg.cu")) {
80     dbg() << Banner << "Skipping module with debug info\n";
81     return false;
82   }
83 
84   DIBuilder DIB(M);
85   LLVMContext &Ctx = M.getContext();
86   auto *Int32Ty = Type::getInt32Ty(Ctx);
87 
88   // Get a DIType which corresponds to Ty.
89   DenseMap<uint64_t, DIType *> TypeCache;
90   auto getCachedDIType = [&](Type *Ty) -> DIType * {
91     uint64_t Size = getAllocSizeInBits(M, Ty);
92     DIType *&DTy = TypeCache[Size];
93     if (!DTy) {
94       std::string Name = "ty" + utostr(Size);
95       DTy = DIB.createBasicType(Name, Size, dwarf::DW_ATE_unsigned);
96     }
97     return DTy;
98   };
99 
100   unsigned NextLine = 1;
101   unsigned NextVar = 1;
102   auto File = DIB.createFile(M.getName(), "/");
103   auto CU = DIB.createCompileUnit(dwarf::DW_LANG_C, File, "debugify",
104                                   /*isOptimized=*/true, "", 0);
105 
106   // Visit each instruction.
107   for (Function &F : Functions) {
108     if (isFunctionSkipped(F))
109       continue;
110 
111     bool InsertedDbgVal = false;
112     auto SPType = DIB.createSubroutineType(DIB.getOrCreateTypeArray(None));
113     DISubprogram::DISPFlags SPFlags =
114         DISubprogram::SPFlagDefinition | DISubprogram::SPFlagOptimized;
115     if (F.hasPrivateLinkage() || F.hasInternalLinkage())
116       SPFlags |= DISubprogram::SPFlagLocalToUnit;
117     auto SP = DIB.createFunction(CU, F.getName(), F.getName(), File, NextLine,
118                                  SPType, NextLine, DINode::FlagZero, SPFlags);
119     F.setSubprogram(SP);
120 
121     // Helper that inserts a dbg.value before \p InsertBefore, copying the
122     // location (and possibly the type, if it's non-void) from \p TemplateInst.
123     auto insertDbgVal = [&](Instruction &TemplateInst,
124                             Instruction *InsertBefore) {
125       std::string Name = utostr(NextVar++);
126       Value *V = &TemplateInst;
127       if (TemplateInst.getType()->isVoidTy())
128         V = ConstantInt::get(Int32Ty, 0);
129       const DILocation *Loc = TemplateInst.getDebugLoc().get();
130       auto LocalVar = DIB.createAutoVariable(SP, Name, File, Loc->getLine(),
131                                              getCachedDIType(V->getType()),
132                                              /*AlwaysPreserve=*/true);
133       DIB.insertDbgValueIntrinsic(V, LocalVar, DIB.createExpression(), Loc,
134                                   InsertBefore);
135     };
136 
137     for (BasicBlock &BB : F) {
138       // Attach debug locations.
139       for (Instruction &I : BB)
140         I.setDebugLoc(DILocation::get(Ctx, NextLine++, 1, SP));
141 
142       if (DebugifyLevel < Level::LocationsAndVariables)
143         continue;
144 
145       // Inserting debug values into EH pads can break IR invariants.
146       if (BB.isEHPad())
147         continue;
148 
149       // Find the terminating instruction, after which no debug values are
150       // attached.
151       Instruction *LastInst = findTerminatingInstruction(BB);
152       assert(LastInst && "Expected basic block with a terminator");
153 
154       // Maintain an insertion point which can't be invalidated when updates
155       // are made.
156       BasicBlock::iterator InsertPt = BB.getFirstInsertionPt();
157       assert(InsertPt != BB.end() && "Expected to find an insertion point");
158       Instruction *InsertBefore = &*InsertPt;
159 
160       // Attach debug values.
161       for (Instruction *I = &*BB.begin(); I != LastInst; I = I->getNextNode()) {
162         // Skip void-valued instructions.
163         if (I->getType()->isVoidTy())
164           continue;
165 
166         // Phis and EH pads must be grouped at the beginning of the block.
167         // Only advance the insertion point when we finish visiting these.
168         if (!isa<PHINode>(I) && !I->isEHPad())
169           InsertBefore = I->getNextNode();
170 
171         insertDbgVal(*I, InsertBefore);
172         InsertedDbgVal = true;
173       }
174     }
175     // Make sure we emit at least one dbg.value, otherwise MachineDebugify may
176     // not have anything to work with as it goes about inserting DBG_VALUEs.
177     // (It's common for MIR tests to be written containing skeletal IR with
178     // empty functions -- we're still interested in debugifying the MIR within
179     // those tests, and this helps with that.)
180     if (DebugifyLevel == Level::LocationsAndVariables && !InsertedDbgVal) {
181       auto *Term = findTerminatingInstruction(F.getEntryBlock());
182       insertDbgVal(*Term, Term);
183     }
184     if (ApplyToMF)
185       ApplyToMF(DIB, F);
186     DIB.finalizeSubprogram(SP);
187   }
188   DIB.finalize();
189 
190   // Track the number of distinct lines and variables.
191   NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.debugify");
192   auto addDebugifyOperand = [&](unsigned N) {
193     NMD->addOperand(MDNode::get(
194         Ctx, ValueAsMetadata::getConstant(ConstantInt::get(Int32Ty, N))));
195   };
196   addDebugifyOperand(NextLine - 1); // Original number of lines.
197   addDebugifyOperand(NextVar - 1);  // Original number of variables.
198   assert(NMD->getNumOperands() == 2 &&
199          "llvm.debugify should have exactly 2 operands!");
200 
201   // Claim that this synthetic debug info is valid.
202   StringRef DIVersionKey = "Debug Info Version";
203   if (!M.getModuleFlag(DIVersionKey))
204     M.addModuleFlag(Module::Warning, DIVersionKey, DEBUG_METADATA_VERSION);
205 
206   return true;
207 }
208 
209 static bool
210 applyDebugify(Function &F,
211               enum DebugifyMode Mode = DebugifyMode::SyntheticDebugInfo,
212               DebugInfoPerPass *DebugInfoBeforePass = nullptr,
213               StringRef NameOfWrappedPass = "") {
214   Module &M = *F.getParent();
215   auto FuncIt = F.getIterator();
216   if (Mode == DebugifyMode::SyntheticDebugInfo)
217     return applyDebugifyMetadata(M, make_range(FuncIt, std::next(FuncIt)),
218                                  "FunctionDebugify: ", /*ApplyToMF*/ nullptr);
219   assert(DebugInfoBeforePass);
220   return collectDebugInfoMetadata(M, M.functions(), *DebugInfoBeforePass,
221                                   "FunctionDebugify (original debuginfo)",
222                                   NameOfWrappedPass);
223 }
224 
225 static bool
226 applyDebugify(Module &M,
227               enum DebugifyMode Mode = DebugifyMode::SyntheticDebugInfo,
228               DebugInfoPerPass *DebugInfoBeforePass = nullptr,
229               StringRef NameOfWrappedPass = "") {
230   if (Mode == DebugifyMode::SyntheticDebugInfo)
231     return applyDebugifyMetadata(M, M.functions(),
232                                  "ModuleDebugify: ", /*ApplyToMF*/ nullptr);
233   return collectDebugInfoMetadata(M, M.functions(), *DebugInfoBeforePass,
234                                   "ModuleDebugify (original debuginfo)",
235                                   NameOfWrappedPass);
236 }
237 
238 bool llvm::stripDebugifyMetadata(Module &M) {
239   bool Changed = false;
240 
241   // Remove the llvm.debugify module-level named metadata.
242   NamedMDNode *DebugifyMD = M.getNamedMetadata("llvm.debugify");
243   if (DebugifyMD) {
244     M.eraseNamedMetadata(DebugifyMD);
245     Changed = true;
246   }
247 
248   // Strip out all debug intrinsics and supporting metadata (subprograms, types,
249   // variables, etc).
250   Changed |= StripDebugInfo(M);
251 
252   // Strip out the dead dbg.value prototype.
253   Function *DbgValF = M.getFunction("llvm.dbg.value");
254   if (DbgValF) {
255     assert(DbgValF->isDeclaration() && DbgValF->use_empty() &&
256            "Not all debug info stripped?");
257     DbgValF->eraseFromParent();
258     Changed = true;
259   }
260 
261   // Strip out the module-level Debug Info Version metadata.
262   // FIXME: There must be an easier way to remove an operand from a NamedMDNode.
263   NamedMDNode *NMD = M.getModuleFlagsMetadata();
264   if (!NMD)
265     return Changed;
266   SmallVector<MDNode *, 4> Flags(NMD->operands());
267   NMD->clearOperands();
268   for (MDNode *Flag : Flags) {
269     MDString *Key = dyn_cast_or_null<MDString>(Flag->getOperand(1));
270     if (Key->getString() == "Debug Info Version") {
271       Changed = true;
272       continue;
273     }
274     NMD->addOperand(Flag);
275   }
276   // If we left it empty we might as well remove it.
277   if (NMD->getNumOperands() == 0)
278     NMD->eraseFromParent();
279 
280   return Changed;
281 }
282 
283 bool llvm::collectDebugInfoMetadata(Module &M,
284                                     iterator_range<Module::iterator> Functions,
285                                     DebugInfoPerPass &DebugInfoBeforePass,
286                                     StringRef Banner,
287                                     StringRef NameOfWrappedPass) {
288   LLVM_DEBUG(dbgs() << Banner << ": (before) " << NameOfWrappedPass << '\n');
289 
290   if (!M.getNamedMetadata("llvm.dbg.cu")) {
291     dbg() << Banner << ": Skipping module without debug info\n";
292     return false;
293   }
294 
295   // Visit each instruction.
296   for (Function &F : Functions) {
297     // Use DI collected after previous Pass (when -debugify-each is used).
298     if (DebugInfoBeforePass.DIFunctions.count(&F))
299       continue;
300 
301     if (isFunctionSkipped(F))
302       continue;
303 
304     // Collect the DISubprogram.
305     auto *SP = F.getSubprogram();
306     DebugInfoBeforePass.DIFunctions.insert({&F, SP});
307     if (SP) {
308       LLVM_DEBUG(dbgs() << "  Collecting subprogram: " << *SP << '\n');
309       for (const DINode *DN : SP->getRetainedNodes()) {
310         if (const auto *DV = dyn_cast<DILocalVariable>(DN)) {
311           DebugInfoBeforePass.DIVariables[DV] = 0;
312         }
313       }
314     }
315 
316     for (BasicBlock &BB : F) {
317       // Collect debug locations (!dbg) and debug variable intrinsics.
318       for (Instruction &I : BB) {
319         // Skip PHIs.
320         if (isa<PHINode>(I))
321           continue;
322 
323         // Cllect dbg.values and dbg.declare.
324         if (DebugifyLevel > Level::Locations) {
325           if (auto *DVI = dyn_cast<DbgVariableIntrinsic>(&I)) {
326             if (!SP)
327               continue;
328             // Skip inlined variables.
329             if (I.getDebugLoc().getInlinedAt())
330               continue;
331             // Skip undef values.
332             if (DVI->isUndef())
333               continue;
334 
335             auto *Var = DVI->getVariable();
336             DebugInfoBeforePass.DIVariables[Var]++;
337             continue;
338           }
339         }
340 
341         // Skip debug instructions other than dbg.value and dbg.declare.
342         if (isa<DbgInfoIntrinsic>(&I))
343           continue;
344 
345         LLVM_DEBUG(dbgs() << "  Collecting info for inst: " << I << '\n');
346         DebugInfoBeforePass.InstToDelete.insert({&I, &I});
347 
348         const DILocation *Loc = I.getDebugLoc().get();
349         bool HasLoc = Loc != nullptr;
350         DebugInfoBeforePass.DILocations.insert({&I, HasLoc});
351       }
352     }
353   }
354 
355   return true;
356 }
357 
358 // This checks the preservation of original debug info attached to functions.
359 static bool checkFunctions(const DebugFnMap &DIFunctionsBefore,
360                            const DebugFnMap &DIFunctionsAfter,
361                            StringRef NameOfWrappedPass,
362                            StringRef FileNameFromCU, bool ShouldWriteIntoJSON,
363                            llvm::json::Array &Bugs) {
364   bool Preserved = true;
365   for (const auto &F : DIFunctionsAfter) {
366     if (F.second)
367       continue;
368     auto SPIt = DIFunctionsBefore.find(F.first);
369     if (SPIt == DIFunctionsBefore.end()) {
370       if (ShouldWriteIntoJSON)
371         Bugs.push_back(llvm::json::Object({{"metadata", "DISubprogram"},
372                                            {"name", F.first->getName()},
373                                            {"action", "not-generate"}}));
374       else
375         dbg() << "ERROR: " << NameOfWrappedPass
376               << " did not generate DISubprogram for " << F.first->getName()
377               << " from " << FileNameFromCU << '\n';
378       Preserved = false;
379     } else {
380       auto SP = SPIt->second;
381       if (!SP)
382         continue;
383       // If the function had the SP attached before the pass, consider it as
384       // a debug info bug.
385       if (ShouldWriteIntoJSON)
386         Bugs.push_back(llvm::json::Object({{"metadata", "DISubprogram"},
387                                            {"name", F.first->getName()},
388                                            {"action", "drop"}}));
389       else
390         dbg() << "ERROR: " << NameOfWrappedPass << " dropped DISubprogram of "
391               << F.first->getName() << " from " << FileNameFromCU << '\n';
392       Preserved = false;
393     }
394   }
395 
396   return Preserved;
397 }
398 
399 // This checks the preservation of the original debug info attached to
400 // instructions.
401 static bool checkInstructions(const DebugInstMap &DILocsBefore,
402                               const DebugInstMap &DILocsAfter,
403                               const WeakInstValueMap &InstToDelete,
404                               StringRef NameOfWrappedPass,
405                               StringRef FileNameFromCU,
406                               bool ShouldWriteIntoJSON,
407                               llvm::json::Array &Bugs) {
408   bool Preserved = true;
409   for (const auto &L : DILocsAfter) {
410     if (L.second)
411       continue;
412     auto Instr = L.first;
413 
414     // In order to avoid pointer reuse/recycling, skip the values that might
415     // have been deleted during a pass.
416     auto WeakInstrPtr = InstToDelete.find(Instr);
417     if (WeakInstrPtr != InstToDelete.end() && !WeakInstrPtr->second)
418       continue;
419 
420     auto FnName = Instr->getFunction()->getName();
421     auto BB = Instr->getParent();
422     auto BBName = BB->hasName() ? BB->getName() : "no-name";
423     auto InstName = Instruction::getOpcodeName(Instr->getOpcode());
424 
425     auto InstrIt = DILocsBefore.find(Instr);
426     if (InstrIt == DILocsBefore.end()) {
427       if (ShouldWriteIntoJSON)
428         Bugs.push_back(llvm::json::Object({{"metadata", "DILocation"},
429                                            {"fn-name", FnName.str()},
430                                            {"bb-name", BBName.str()},
431                                            {"instr", InstName},
432                                            {"action", "not-generate"}}));
433       else
434         dbg() << "WARNING: " << NameOfWrappedPass
435               << " did not generate DILocation for " << *Instr
436               << " (BB: " << BBName << ", Fn: " << FnName
437               << ", File: " << FileNameFromCU << ")\n";
438       Preserved = false;
439     } else {
440       if (!InstrIt->second)
441         continue;
442       // If the instr had the !dbg attached before the pass, consider it as
443       // a debug info issue.
444       if (ShouldWriteIntoJSON)
445         Bugs.push_back(llvm::json::Object({{"metadata", "DILocation"},
446                                            {"fn-name", FnName.str()},
447                                            {"bb-name", BBName.str()},
448                                            {"instr", InstName},
449                                            {"action", "drop"}}));
450       else
451         dbg() << "WARNING: " << NameOfWrappedPass << " dropped DILocation of "
452               << *Instr << " (BB: " << BBName << ", Fn: " << FnName
453               << ", File: " << FileNameFromCU << ")\n";
454       Preserved = false;
455     }
456   }
457 
458   return Preserved;
459 }
460 
461 // This checks the preservation of original debug variable intrinsics.
462 static bool checkVars(const DebugVarMap &DIVarsBefore,
463                       const DebugVarMap &DIVarsAfter,
464                       StringRef NameOfWrappedPass, StringRef FileNameFromCU,
465                       bool ShouldWriteIntoJSON, llvm::json::Array &Bugs) {
466   bool Preserved = true;
467   for (const auto &V : DIVarsBefore) {
468     auto VarIt = DIVarsAfter.find(V.first);
469     if (VarIt == DIVarsAfter.end())
470       continue;
471 
472     unsigned NumOfDbgValsAfter = VarIt->second;
473 
474     if (V.second > NumOfDbgValsAfter) {
475       if (ShouldWriteIntoJSON)
476         Bugs.push_back(llvm::json::Object(
477             {{"metadata", "dbg-var-intrinsic"},
478              {"name", V.first->getName()},
479              {"fn-name", V.first->getScope()->getSubprogram()->getName()},
480              {"action", "drop"}}));
481       else
482         dbg() << "WARNING: " << NameOfWrappedPass
483               << " drops dbg.value()/dbg.declare() for " << V.first->getName()
484               << " from "
485               << "function " << V.first->getScope()->getSubprogram()->getName()
486               << " (file " << FileNameFromCU << ")\n";
487       Preserved = false;
488     }
489   }
490 
491   return Preserved;
492 }
493 
494 // Write the json data into the specifed file.
495 static void writeJSON(StringRef OrigDIVerifyBugsReportFilePath,
496                       StringRef FileNameFromCU, StringRef NameOfWrappedPass,
497                       llvm::json::Array &Bugs) {
498   std::error_code EC;
499   raw_fd_ostream OS_FILE{OrigDIVerifyBugsReportFilePath, EC,
500                          sys::fs::OF_Append | sys::fs::OF_TextWithCRLF};
501   if (EC) {
502     errs() << "Could not open file: " << EC.message() << ", "
503            << OrigDIVerifyBugsReportFilePath << '\n';
504     return;
505   }
506 
507   OS_FILE << "{\"file\":\"" << FileNameFromCU << "\", ";
508 
509   StringRef PassName = NameOfWrappedPass != "" ? NameOfWrappedPass : "no-name";
510   OS_FILE << "\"pass\":\"" << PassName << "\", ";
511 
512   llvm::json::Value BugsToPrint{std::move(Bugs)};
513   OS_FILE << "\"bugs\": " << BugsToPrint;
514 
515   OS_FILE << "}\n";
516 }
517 
518 bool llvm::checkDebugInfoMetadata(Module &M,
519                                   iterator_range<Module::iterator> Functions,
520                                   DebugInfoPerPass &DebugInfoBeforePass,
521                                   StringRef Banner, StringRef NameOfWrappedPass,
522                                   StringRef OrigDIVerifyBugsReportFilePath) {
523   LLVM_DEBUG(dbgs() << Banner << ": (after) " << NameOfWrappedPass << '\n');
524 
525   if (!M.getNamedMetadata("llvm.dbg.cu")) {
526     dbg() << Banner << ": Skipping module without debug info\n";
527     return false;
528   }
529 
530   // Map the debug info holding DIs after a pass.
531   DebugInfoPerPass DebugInfoAfterPass;
532 
533   // Visit each instruction.
534   for (Function &F : Functions) {
535     if (isFunctionSkipped(F))
536       continue;
537 
538     // TODO: Collect metadata other than DISubprograms.
539     // Collect the DISubprogram.
540     auto *SP = F.getSubprogram();
541     DebugInfoAfterPass.DIFunctions.insert({&F, SP});
542 
543     if (SP) {
544       LLVM_DEBUG(dbgs() << "  Collecting subprogram: " << *SP << '\n');
545       for (const DINode *DN : SP->getRetainedNodes()) {
546         if (const auto *DV = dyn_cast<DILocalVariable>(DN)) {
547           DebugInfoAfterPass.DIVariables[DV] = 0;
548         }
549       }
550     }
551 
552     for (BasicBlock &BB : F) {
553       // Collect debug locations (!dbg) and debug variable intrinsics.
554       for (Instruction &I : BB) {
555         // Skip PHIs.
556         if (isa<PHINode>(I))
557           continue;
558 
559         // Collect dbg.values and dbg.declares.
560         if (DebugifyLevel > Level::Locations) {
561           if (auto *DVI = dyn_cast<DbgVariableIntrinsic>(&I)) {
562             if (!SP)
563               continue;
564             // Skip inlined variables.
565             if (I.getDebugLoc().getInlinedAt())
566               continue;
567             // Skip undef values.
568             if (DVI->isUndef())
569               continue;
570 
571             auto *Var = DVI->getVariable();
572             DebugInfoAfterPass.DIVariables[Var]++;
573             continue;
574           }
575         }
576 
577         // Skip debug instructions other than dbg.value and dbg.declare.
578         if (isa<DbgInfoIntrinsic>(&I))
579           continue;
580 
581         LLVM_DEBUG(dbgs() << "  Collecting info for inst: " << I << '\n');
582 
583         const DILocation *Loc = I.getDebugLoc().get();
584         bool HasLoc = Loc != nullptr;
585 
586         DebugInfoAfterPass.DILocations.insert({&I, HasLoc});
587       }
588     }
589   }
590 
591   // TODO: The name of the module could be read better?
592   StringRef FileNameFromCU =
593       (cast<DICompileUnit>(M.getNamedMetadata("llvm.dbg.cu")->getOperand(0)))
594           ->getFilename();
595 
596   auto DIFunctionsBefore = DebugInfoBeforePass.DIFunctions;
597   auto DIFunctionsAfter = DebugInfoAfterPass.DIFunctions;
598 
599   auto DILocsBefore = DebugInfoBeforePass.DILocations;
600   auto DILocsAfter = DebugInfoAfterPass.DILocations;
601 
602   auto InstToDelete = DebugInfoBeforePass.InstToDelete;
603 
604   auto DIVarsBefore = DebugInfoBeforePass.DIVariables;
605   auto DIVarsAfter = DebugInfoAfterPass.DIVariables;
606 
607   bool ShouldWriteIntoJSON = !OrigDIVerifyBugsReportFilePath.empty();
608   llvm::json::Array Bugs;
609 
610   bool ResultForFunc =
611       checkFunctions(DIFunctionsBefore, DIFunctionsAfter, NameOfWrappedPass,
612                      FileNameFromCU, ShouldWriteIntoJSON, Bugs);
613   bool ResultForInsts = checkInstructions(
614       DILocsBefore, DILocsAfter, InstToDelete, NameOfWrappedPass,
615       FileNameFromCU, ShouldWriteIntoJSON, Bugs);
616 
617   bool ResultForVars = checkVars(DIVarsBefore, DIVarsAfter, NameOfWrappedPass,
618                                  FileNameFromCU, ShouldWriteIntoJSON, Bugs);
619 
620   bool Result = ResultForFunc && ResultForInsts && ResultForVars;
621 
622   StringRef ResultBanner = NameOfWrappedPass != "" ? NameOfWrappedPass : Banner;
623   if (ShouldWriteIntoJSON && !Bugs.empty())
624     writeJSON(OrigDIVerifyBugsReportFilePath, FileNameFromCU, NameOfWrappedPass,
625               Bugs);
626 
627   if (Result)
628     dbg() << ResultBanner << ": PASS\n";
629   else
630     dbg() << ResultBanner << ": FAIL\n";
631 
632   // In the case of the `debugify-each`, no need to go over all the instructions
633   // again in the collectDebugInfoMetadata(), since as an input we can use
634   // the debugging information from the previous pass.
635   DebugInfoBeforePass = DebugInfoAfterPass;
636 
637   LLVM_DEBUG(dbgs() << "\n\n");
638   return Result;
639 }
640 
641 namespace {
642 /// Return true if a mis-sized diagnostic is issued for \p DVI.
643 bool diagnoseMisSizedDbgValue(Module &M, DbgValueInst *DVI) {
644   // The size of a dbg.value's value operand should match the size of the
645   // variable it corresponds to.
646   //
647   // TODO: This, along with a check for non-null value operands, should be
648   // promoted to verifier failures.
649 
650   // For now, don't try to interpret anything more complicated than an empty
651   // DIExpression. Eventually we should try to handle OP_deref and fragments.
652   if (DVI->getExpression()->getNumElements())
653     return false;
654 
655   Value *V = DVI->getVariableLocationOp(0);
656   if (!V)
657     return false;
658 
659   Type *Ty = V->getType();
660   uint64_t ValueOperandSize = getAllocSizeInBits(M, Ty);
661   Optional<uint64_t> DbgVarSize = DVI->getFragmentSizeInBits();
662   if (!ValueOperandSize || !DbgVarSize)
663     return false;
664 
665   bool HasBadSize = false;
666   if (Ty->isIntegerTy()) {
667     auto Signedness = DVI->getVariable()->getSignedness();
668     if (Signedness && *Signedness == DIBasicType::Signedness::Signed)
669       HasBadSize = ValueOperandSize < *DbgVarSize;
670   } else {
671     HasBadSize = ValueOperandSize != *DbgVarSize;
672   }
673 
674   if (HasBadSize) {
675     dbg() << "ERROR: dbg.value operand has size " << ValueOperandSize
676           << ", but its variable has size " << *DbgVarSize << ": ";
677     DVI->print(dbg());
678     dbg() << "\n";
679   }
680   return HasBadSize;
681 }
682 
683 bool checkDebugifyMetadata(Module &M,
684                            iterator_range<Module::iterator> Functions,
685                            StringRef NameOfWrappedPass, StringRef Banner,
686                            bool Strip, DebugifyStatsMap *StatsMap) {
687   // Skip modules without debugify metadata.
688   NamedMDNode *NMD = M.getNamedMetadata("llvm.debugify");
689   if (!NMD) {
690     dbg() << Banner << ": Skipping module without debugify metadata\n";
691     return false;
692   }
693 
694   auto getDebugifyOperand = [&](unsigned Idx) -> unsigned {
695     return mdconst::extract<ConstantInt>(NMD->getOperand(Idx)->getOperand(0))
696         ->getZExtValue();
697   };
698   assert(NMD->getNumOperands() == 2 &&
699          "llvm.debugify should have exactly 2 operands!");
700   unsigned OriginalNumLines = getDebugifyOperand(0);
701   unsigned OriginalNumVars = getDebugifyOperand(1);
702   bool HasErrors = false;
703 
704   // Track debug info loss statistics if able.
705   DebugifyStatistics *Stats = nullptr;
706   if (StatsMap && !NameOfWrappedPass.empty())
707     Stats = &StatsMap->operator[](NameOfWrappedPass);
708 
709   BitVector MissingLines{OriginalNumLines, true};
710   BitVector MissingVars{OriginalNumVars, true};
711   for (Function &F : Functions) {
712     if (isFunctionSkipped(F))
713       continue;
714 
715     // Find missing lines.
716     for (Instruction &I : instructions(F)) {
717       if (isa<DbgValueInst>(&I))
718         continue;
719 
720       auto DL = I.getDebugLoc();
721       if (DL && DL.getLine() != 0) {
722         MissingLines.reset(DL.getLine() - 1);
723         continue;
724       }
725 
726       if (!isa<PHINode>(&I) && !DL) {
727         dbg() << "WARNING: Instruction with empty DebugLoc in function ";
728         dbg() << F.getName() << " --";
729         I.print(dbg());
730         dbg() << "\n";
731       }
732     }
733 
734     // Find missing variables and mis-sized debug values.
735     for (Instruction &I : instructions(F)) {
736       auto *DVI = dyn_cast<DbgValueInst>(&I);
737       if (!DVI)
738         continue;
739 
740       unsigned Var = ~0U;
741       (void)to_integer(DVI->getVariable()->getName(), Var, 10);
742       assert(Var <= OriginalNumVars && "Unexpected name for DILocalVariable");
743       bool HasBadSize = diagnoseMisSizedDbgValue(M, DVI);
744       if (!HasBadSize)
745         MissingVars.reset(Var - 1);
746       HasErrors |= HasBadSize;
747     }
748   }
749 
750   // Print the results.
751   for (unsigned Idx : MissingLines.set_bits())
752     dbg() << "WARNING: Missing line " << Idx + 1 << "\n";
753 
754   for (unsigned Idx : MissingVars.set_bits())
755     dbg() << "WARNING: Missing variable " << Idx + 1 << "\n";
756 
757   // Update DI loss statistics.
758   if (Stats) {
759     Stats->NumDbgLocsExpected += OriginalNumLines;
760     Stats->NumDbgLocsMissing += MissingLines.count();
761     Stats->NumDbgValuesExpected += OriginalNumVars;
762     Stats->NumDbgValuesMissing += MissingVars.count();
763   }
764 
765   dbg() << Banner;
766   if (!NameOfWrappedPass.empty())
767     dbg() << " [" << NameOfWrappedPass << "]";
768   dbg() << ": " << (HasErrors ? "FAIL" : "PASS") << '\n';
769 
770   // Strip debugify metadata if required.
771   if (Strip)
772     return stripDebugifyMetadata(M);
773 
774   return false;
775 }
776 
777 /// ModulePass for attaching synthetic debug info to everything, used with the
778 /// legacy module pass manager.
779 struct DebugifyModulePass : public ModulePass {
780   bool runOnModule(Module &M) override {
781     return applyDebugify(M, Mode, DebugInfoBeforePass, NameOfWrappedPass);
782   }
783 
784   DebugifyModulePass(enum DebugifyMode Mode = DebugifyMode::SyntheticDebugInfo,
785                      StringRef NameOfWrappedPass = "",
786                      DebugInfoPerPass *DebugInfoBeforePass = nullptr)
787       : ModulePass(ID), NameOfWrappedPass(NameOfWrappedPass),
788         DebugInfoBeforePass(DebugInfoBeforePass), Mode(Mode) {}
789 
790   void getAnalysisUsage(AnalysisUsage &AU) const override {
791     AU.setPreservesAll();
792   }
793 
794   static char ID; // Pass identification.
795 
796 private:
797   StringRef NameOfWrappedPass;
798   DebugInfoPerPass *DebugInfoBeforePass;
799   enum DebugifyMode Mode;
800 };
801 
802 /// FunctionPass for attaching synthetic debug info to instructions within a
803 /// single function, used with the legacy module pass manager.
804 struct DebugifyFunctionPass : public FunctionPass {
805   bool runOnFunction(Function &F) override {
806     return applyDebugify(F, Mode, DebugInfoBeforePass, NameOfWrappedPass);
807   }
808 
809   DebugifyFunctionPass(
810       enum DebugifyMode Mode = DebugifyMode::SyntheticDebugInfo,
811       StringRef NameOfWrappedPass = "",
812       DebugInfoPerPass *DebugInfoBeforePass = nullptr)
813       : FunctionPass(ID), NameOfWrappedPass(NameOfWrappedPass),
814         DebugInfoBeforePass(DebugInfoBeforePass), Mode(Mode) {}
815 
816   void getAnalysisUsage(AnalysisUsage &AU) const override {
817     AU.setPreservesAll();
818   }
819 
820   static char ID; // Pass identification.
821 
822 private:
823   StringRef NameOfWrappedPass;
824   DebugInfoPerPass *DebugInfoBeforePass;
825   enum DebugifyMode Mode;
826 };
827 
828 /// ModulePass for checking debug info inserted by -debugify, used with the
829 /// legacy module pass manager.
830 struct CheckDebugifyModulePass : public ModulePass {
831   bool runOnModule(Module &M) override {
832     if (Mode == DebugifyMode::SyntheticDebugInfo)
833       return checkDebugifyMetadata(M, M.functions(), NameOfWrappedPass,
834                                    "CheckModuleDebugify", Strip, StatsMap);
835     return checkDebugInfoMetadata(
836         M, M.functions(), *DebugInfoBeforePass,
837         "CheckModuleDebugify (original debuginfo)", NameOfWrappedPass,
838         OrigDIVerifyBugsReportFilePath);
839   }
840 
841   CheckDebugifyModulePass(
842       bool Strip = false, StringRef NameOfWrappedPass = "",
843       DebugifyStatsMap *StatsMap = nullptr,
844       enum DebugifyMode Mode = DebugifyMode::SyntheticDebugInfo,
845       DebugInfoPerPass *DebugInfoBeforePass = nullptr,
846       StringRef OrigDIVerifyBugsReportFilePath = "")
847       : ModulePass(ID), NameOfWrappedPass(NameOfWrappedPass),
848         OrigDIVerifyBugsReportFilePath(OrigDIVerifyBugsReportFilePath),
849         StatsMap(StatsMap), DebugInfoBeforePass(DebugInfoBeforePass), Mode(Mode),
850         Strip(Strip) {}
851 
852   void getAnalysisUsage(AnalysisUsage &AU) const override {
853     AU.setPreservesAll();
854   }
855 
856   static char ID; // Pass identification.
857 
858 private:
859   StringRef NameOfWrappedPass;
860   StringRef OrigDIVerifyBugsReportFilePath;
861   DebugifyStatsMap *StatsMap;
862   DebugInfoPerPass *DebugInfoBeforePass;
863   enum DebugifyMode Mode;
864   bool Strip;
865 };
866 
867 /// FunctionPass for checking debug info inserted by -debugify-function, used
868 /// with the legacy module pass manager.
869 struct CheckDebugifyFunctionPass : public FunctionPass {
870   bool runOnFunction(Function &F) override {
871     Module &M = *F.getParent();
872     auto FuncIt = F.getIterator();
873     if (Mode == DebugifyMode::SyntheticDebugInfo)
874       return checkDebugifyMetadata(M, make_range(FuncIt, std::next(FuncIt)),
875                                    NameOfWrappedPass, "CheckFunctionDebugify",
876                                    Strip, StatsMap);
877     return checkDebugInfoMetadata(
878         M, make_range(FuncIt, std::next(FuncIt)), *DebugInfoBeforePass,
879         "CheckFunctionDebugify (original debuginfo)", NameOfWrappedPass,
880         OrigDIVerifyBugsReportFilePath);
881   }
882 
883   CheckDebugifyFunctionPass(
884       bool Strip = false, StringRef NameOfWrappedPass = "",
885       DebugifyStatsMap *StatsMap = nullptr,
886       enum DebugifyMode Mode = DebugifyMode::SyntheticDebugInfo,
887       DebugInfoPerPass *DebugInfoBeforePass = nullptr,
888       StringRef OrigDIVerifyBugsReportFilePath = "")
889       : FunctionPass(ID), NameOfWrappedPass(NameOfWrappedPass),
890         OrigDIVerifyBugsReportFilePath(OrigDIVerifyBugsReportFilePath),
891         StatsMap(StatsMap), DebugInfoBeforePass(DebugInfoBeforePass), Mode(Mode),
892         Strip(Strip) {}
893 
894   void getAnalysisUsage(AnalysisUsage &AU) const override {
895     AU.setPreservesAll();
896   }
897 
898   static char ID; // Pass identification.
899 
900 private:
901   StringRef NameOfWrappedPass;
902   StringRef OrigDIVerifyBugsReportFilePath;
903   DebugifyStatsMap *StatsMap;
904   DebugInfoPerPass *DebugInfoBeforePass;
905   enum DebugifyMode Mode;
906   bool Strip;
907 };
908 
909 } // end anonymous namespace
910 
911 void llvm::exportDebugifyStats(StringRef Path, const DebugifyStatsMap &Map) {
912   std::error_code EC;
913   raw_fd_ostream OS{Path, EC};
914   if (EC) {
915     errs() << "Could not open file: " << EC.message() << ", " << Path << '\n';
916     return;
917   }
918 
919   OS << "Pass Name" << ',' << "# of missing debug values" << ','
920      << "# of missing locations" << ',' << "Missing/Expected value ratio" << ','
921      << "Missing/Expected location ratio" << '\n';
922   for (const auto &Entry : Map) {
923     StringRef Pass = Entry.first;
924     DebugifyStatistics Stats = Entry.second;
925 
926     OS << Pass << ',' << Stats.NumDbgValuesMissing << ','
927        << Stats.NumDbgLocsMissing << ',' << Stats.getMissingValueRatio() << ','
928        << Stats.getEmptyLocationRatio() << '\n';
929   }
930 }
931 
932 ModulePass *createDebugifyModulePass(enum DebugifyMode Mode,
933                                      llvm::StringRef NameOfWrappedPass,
934                                      DebugInfoPerPass *DebugInfoBeforePass) {
935   if (Mode == DebugifyMode::SyntheticDebugInfo)
936     return new DebugifyModulePass();
937   assert(Mode == DebugifyMode::OriginalDebugInfo && "Must be original mode");
938   return new DebugifyModulePass(Mode, NameOfWrappedPass, DebugInfoBeforePass);
939 }
940 
941 FunctionPass *
942 createDebugifyFunctionPass(enum DebugifyMode Mode,
943                            llvm::StringRef NameOfWrappedPass,
944                            DebugInfoPerPass *DebugInfoBeforePass) {
945   if (Mode == DebugifyMode::SyntheticDebugInfo)
946     return new DebugifyFunctionPass();
947   assert(Mode == DebugifyMode::OriginalDebugInfo && "Must be original mode");
948   return new DebugifyFunctionPass(Mode, NameOfWrappedPass, DebugInfoBeforePass);
949 }
950 
951 PreservedAnalyses NewPMDebugifyPass::run(Module &M, ModuleAnalysisManager &) {
952   applyDebugifyMetadata(M, M.functions(),
953                         "ModuleDebugify: ", /*ApplyToMF*/ nullptr);
954   return PreservedAnalyses::all();
955 }
956 
957 ModulePass *createCheckDebugifyModulePass(
958     bool Strip, StringRef NameOfWrappedPass, DebugifyStatsMap *StatsMap,
959     enum DebugifyMode Mode, DebugInfoPerPass *DebugInfoBeforePass,
960     StringRef OrigDIVerifyBugsReportFilePath) {
961   if (Mode == DebugifyMode::SyntheticDebugInfo)
962     return new CheckDebugifyModulePass(Strip, NameOfWrappedPass, StatsMap);
963   assert(Mode == DebugifyMode::OriginalDebugInfo && "Must be original mode");
964   return new CheckDebugifyModulePass(false, NameOfWrappedPass, nullptr, Mode,
965                                      DebugInfoBeforePass,
966                                      OrigDIVerifyBugsReportFilePath);
967 }
968 
969 FunctionPass *createCheckDebugifyFunctionPass(
970     bool Strip, StringRef NameOfWrappedPass, DebugifyStatsMap *StatsMap,
971     enum DebugifyMode Mode, DebugInfoPerPass *DebugInfoBeforePass,
972     StringRef OrigDIVerifyBugsReportFilePath) {
973   if (Mode == DebugifyMode::SyntheticDebugInfo)
974     return new CheckDebugifyFunctionPass(Strip, NameOfWrappedPass, StatsMap);
975   assert(Mode == DebugifyMode::OriginalDebugInfo && "Must be original mode");
976   return new CheckDebugifyFunctionPass(false, NameOfWrappedPass, nullptr, Mode,
977                                        DebugInfoBeforePass,
978                                        OrigDIVerifyBugsReportFilePath);
979 }
980 
981 PreservedAnalyses NewPMCheckDebugifyPass::run(Module &M,
982                                               ModuleAnalysisManager &) {
983   checkDebugifyMetadata(M, M.functions(), "", "CheckModuleDebugify", false,
984                         nullptr);
985   return PreservedAnalyses::all();
986 }
987 
988 static bool isIgnoredPass(StringRef PassID) {
989   return isSpecialPass(PassID, {"PassManager", "PassAdaptor",
990                                 "AnalysisManagerProxy", "PrintFunctionPass",
991                                 "PrintModulePass", "BitcodeWriterPass",
992                                 "ThinLTOBitcodeWriterPass", "VerifierPass"});
993 }
994 
995 void DebugifyEachInstrumentation::registerCallbacks(
996     PassInstrumentationCallbacks &PIC) {
997   PIC.registerBeforeNonSkippedPassCallback([](StringRef P, Any IR) {
998     if (isIgnoredPass(P))
999       return;
1000     if (any_isa<const Function *>(IR))
1001       applyDebugify(*const_cast<Function *>(any_cast<const Function *>(IR)));
1002     else if (any_isa<const Module *>(IR))
1003       applyDebugify(*const_cast<Module *>(any_cast<const Module *>(IR)));
1004   });
1005   PIC.registerAfterPassCallback([this](StringRef P, Any IR,
1006                                        const PreservedAnalyses &PassPA) {
1007     if (isIgnoredPass(P))
1008       return;
1009     if (any_isa<const Function *>(IR)) {
1010       auto &F = *const_cast<Function *>(any_cast<const Function *>(IR));
1011       Module &M = *F.getParent();
1012       auto It = F.getIterator();
1013       checkDebugifyMetadata(M, make_range(It, std::next(It)), P,
1014                             "CheckFunctionDebugify", /*Strip=*/true, &StatsMap);
1015     } else if (any_isa<const Module *>(IR)) {
1016       auto &M = *const_cast<Module *>(any_cast<const Module *>(IR));
1017       checkDebugifyMetadata(M, M.functions(), P, "CheckModuleDebugify",
1018                             /*Strip=*/true, &StatsMap);
1019     }
1020   });
1021 }
1022 
1023 char DebugifyModulePass::ID = 0;
1024 static RegisterPass<DebugifyModulePass> DM("debugify",
1025                                            "Attach debug info to everything");
1026 
1027 char CheckDebugifyModulePass::ID = 0;
1028 static RegisterPass<CheckDebugifyModulePass>
1029     CDM("check-debugify", "Check debug info from -debugify");
1030 
1031 char DebugifyFunctionPass::ID = 0;
1032 static RegisterPass<DebugifyFunctionPass> DF("debugify-function",
1033                                              "Attach debug info to a function");
1034 
1035 char CheckDebugifyFunctionPass::ID = 0;
1036 static RegisterPass<CheckDebugifyFunctionPass>
1037     CDF("check-debugify-function", "Check debug info from -debugify-function");
1038