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/JSON.h"
29 
30 #define DEBUG_TYPE "debugify"
31 
32 using namespace llvm;
33 
34 namespace {
35 
36 cl::opt<bool> Quiet("debugify-quiet",
37                     cl::desc("Suppress verbose debugify output"));
38 
39 enum class Level {
40   Locations,
41   LocationsAndVariables
42 };
43 
44 // Used for the synthetic mode only.
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               DebugInfoPerPassMap *DIPreservationMap = 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(DIPreservationMap);
220   return collectDebugInfoMetadata(M, M.functions(), *DIPreservationMap,
221                                   "FunctionDebugify (original debuginfo)",
222                                   NameOfWrappedPass);
223 }
224 
225 static bool
226 applyDebugify(Module &M,
227               enum DebugifyMode Mode = DebugifyMode::SyntheticDebugInfo,
228               DebugInfoPerPassMap *DIPreservationMap = 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(), *DIPreservationMap,
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                                     DebugInfoPerPassMap &DIPreservationMap,
286                                     StringRef Banner,
287                                     StringRef NameOfWrappedPass) {
288   LLVM_DEBUG(dbgs() << Banner << ": (before) " << NameOfWrappedPass << '\n');
289 
290   // Clear the map with the debug info before every single pass.
291   DIPreservationMap.clear();
292 
293   if (!M.getNamedMetadata("llvm.dbg.cu")) {
294     dbg() << Banner << ": Skipping module without debug info\n";
295     return false;
296   }
297 
298   // Visit each instruction.
299   for (Function &F : Functions) {
300     if (isFunctionSkipped(F))
301       continue;
302 
303     // Collect the DISubprogram.
304     auto *SP = F.getSubprogram();
305     DIPreservationMap[NameOfWrappedPass].DIFunctions.insert({F.getName(), SP});
306     if (SP)
307       LLVM_DEBUG(dbgs() << "  Collecting subprogram: " << *SP << '\n');
308 
309     for (BasicBlock &BB : F) {
310       // Collect debug locations (!dbg).
311       // TODO: Collect dbg.values.
312       for (Instruction &I : BB) {
313         // Skip PHIs.
314         if (isa<PHINode>(I))
315           continue;
316 
317         // Skip debug instructions.
318         if (isa<DbgInfoIntrinsic>(&I))
319           continue;
320 
321         LLVM_DEBUG(dbgs() << "  Collecting info for inst: " << I << '\n');
322         DIPreservationMap[NameOfWrappedPass].InstToDelete.insert({&I, &I});
323 
324         const DILocation *Loc = I.getDebugLoc().get();
325         bool HasLoc = Loc != nullptr;
326         DIPreservationMap[NameOfWrappedPass].DILocations.insert({&I, HasLoc});
327       }
328     }
329   }
330 
331   return true;
332 }
333 
334 // This checks the preservation of original debug info attached to functions.
335 static bool checkFunctions(const DebugFnMap &DIFunctionsBefore,
336                            const DebugFnMap &DIFunctionsAfter,
337                            StringRef NameOfWrappedPass,
338                            StringRef FileNameFromCU, bool ShouldWriteIntoJSON,
339                            llvm::json::Array &Bugs) {
340   bool Preserved = true;
341   for (const auto &F : DIFunctionsAfter) {
342     if (F.second)
343       continue;
344     auto SPIt = DIFunctionsBefore.find(F.first);
345     if (SPIt == DIFunctionsBefore.end()) {
346       if (ShouldWriteIntoJSON)
347         Bugs.push_back(llvm::json::Object({{"metadata", "DISubprogram"},
348                                            {"name", F.first},
349                                            {"action", "not-generate"}}));
350       else
351         dbg() << "ERROR: " << NameOfWrappedPass
352               << " did not generate DISubprogram for " << F.first << " from "
353               << FileNameFromCU << '\n';
354       Preserved = false;
355     } else {
356       auto SP = SPIt->second;
357       if (!SP)
358         continue;
359       // If the function had the SP attached before the pass, consider it as
360       // a debug info bug.
361       if (ShouldWriteIntoJSON)
362         Bugs.push_back(llvm::json::Object({{"metadata", "DISubprogram"},
363                                            {"name", F.first},
364                                            {"action", "drop"}}));
365       else
366         dbg() << "ERROR: " << NameOfWrappedPass << " dropped DISubprogram of "
367               << F.first << " from " << FileNameFromCU << '\n';
368       Preserved = false;
369     }
370   }
371 
372   return Preserved;
373 }
374 
375 // This checks the preservation of the original debug info attached to
376 // instructions.
377 static bool checkInstructions(const DebugInstMap &DILocsBefore,
378                               const DebugInstMap &DILocsAfter,
379                               const WeakInstValueMap &InstToDelete,
380                               StringRef NameOfWrappedPass,
381                               StringRef FileNameFromCU,
382                               bool ShouldWriteIntoJSON,
383                               llvm::json::Array &Bugs) {
384   bool Preserved = true;
385   for (const auto &L : DILocsAfter) {
386     if (L.second)
387       continue;
388     auto Instr = L.first;
389 
390     // In order to avoid pointer reuse/recycling, skip the values that might
391     // have been deleted during a pass.
392     auto WeakInstrPtr = InstToDelete.find(Instr);
393     if (WeakInstrPtr != InstToDelete.end() && !WeakInstrPtr->second)
394       continue;
395 
396     auto FnName = Instr->getFunction()->getName();
397     auto BB = Instr->getParent();
398     auto BBName = BB->hasName() ? BB->getName() : "no-name";
399     auto InstName = Instruction::getOpcodeName(Instr->getOpcode());
400 
401     auto InstrIt = DILocsBefore.find(Instr);
402     if (InstrIt == DILocsBefore.end()) {
403       if (ShouldWriteIntoJSON)
404         Bugs.push_back(llvm::json::Object({{"metadata", "DILocation"},
405                                            {"fn-name", FnName.str()},
406                                            {"bb-name", BBName.str()},
407                                            {"instr", InstName},
408                                            {"action", "not-generate"}}));
409       else
410         dbg() << "WARNING: " << NameOfWrappedPass
411               << " did not generate DILocation for " << *Instr
412               << " (BB: " << BBName << ", Fn: " << FnName
413               << ", File: " << FileNameFromCU << ")\n";
414       Preserved = false;
415     } else {
416       if (!InstrIt->second)
417         continue;
418       // If the instr had the !dbg attached before the pass, consider it as
419       // a debug info issue.
420       if (ShouldWriteIntoJSON)
421         Bugs.push_back(llvm::json::Object({{"metadata", "DILocation"},
422                                            {"fn-name", FnName.str()},
423                                            {"bb-name", BBName.str()},
424                                            {"instr", InstName},
425                                            {"action", "drop"}}));
426       else
427         dbg() << "WARNING: " << NameOfWrappedPass << " dropped DILocation of "
428               << *Instr << " (BB: " << BBName << ", Fn: " << FnName
429               << ", File: " << FileNameFromCU << ")\n";
430       Preserved = false;
431     }
432   }
433 
434   return Preserved;
435 }
436 
437 // Write the json data into the specifed file.
438 static void writeJSON(StringRef OrigDIVerifyBugsReportFilePath,
439                       StringRef FileNameFromCU, StringRef NameOfWrappedPass,
440                       llvm::json::Array &Bugs) {
441   std::error_code EC;
442   raw_fd_ostream OS_FILE{OrigDIVerifyBugsReportFilePath, EC,
443                          sys::fs::OF_Append | sys::fs::OF_Text};
444   if (EC) {
445     errs() << "Could not open file: " << EC.message() << ", "
446            << OrigDIVerifyBugsReportFilePath << '\n';
447     return;
448   }
449 
450   OS_FILE << "{\"file\":\"" << FileNameFromCU << "\", ";
451 
452   StringRef PassName = NameOfWrappedPass != "" ? NameOfWrappedPass : "no-name";
453   OS_FILE << "\"pass\":\"" << PassName << "\", ";
454 
455   llvm::json::Value BugsToPrint{std::move(Bugs)};
456   OS_FILE << "\"bugs\": " << BugsToPrint;
457 
458   OS_FILE << "}\n";
459 }
460 
461 bool llvm::checkDebugInfoMetadata(Module &M,
462                                   iterator_range<Module::iterator> Functions,
463                                   DebugInfoPerPassMap &DIPreservationMap,
464                                   StringRef Banner, StringRef NameOfWrappedPass,
465                                   StringRef OrigDIVerifyBugsReportFilePath) {
466   LLVM_DEBUG(dbgs() << Banner << ": (after) " << NameOfWrappedPass << '\n');
467 
468   if (!M.getNamedMetadata("llvm.dbg.cu")) {
469     dbg() << Banner << ": Skipping module without debug info\n";
470     return false;
471   }
472 
473   // Map the debug info holding DIs after a pass.
474   DebugInfoPerPassMap DIPreservationAfter;
475 
476   // Visit each instruction.
477   for (Function &F : Functions) {
478     if (isFunctionSkipped(F))
479       continue;
480 
481     // TODO: Collect metadata other than DISubprograms.
482     // Collect the DISubprogram.
483     auto *SP = F.getSubprogram();
484     DIPreservationAfter[NameOfWrappedPass].DIFunctions.insert(
485         {F.getName(), SP});
486     if (SP)
487       LLVM_DEBUG(dbgs() << "  Collecting subprogram: " << *SP << '\n');
488 
489     for (BasicBlock &BB : F) {
490       // Collect debug locations (!dbg attachments).
491       // TODO: Collect dbg.values.
492       for (Instruction &I : BB) {
493         // Skip PHIs.
494         if (isa<PHINode>(I))
495           continue;
496 
497         // Skip debug instructions.
498         if (isa<DbgInfoIntrinsic>(&I))
499           continue;
500 
501         LLVM_DEBUG(dbgs() << "  Collecting info for inst: " << I << '\n');
502 
503         const DILocation *Loc = I.getDebugLoc().get();
504         bool HasLoc = Loc != nullptr;
505 
506         DIPreservationAfter[NameOfWrappedPass].DILocations.insert({&I, HasLoc});
507       }
508     }
509   }
510 
511   // TODO: The name of the module could be read better?
512   StringRef FileNameFromCU =
513       (cast<DICompileUnit>(M.getNamedMetadata("llvm.dbg.cu")->getOperand(0)))
514           ->getFilename();
515 
516   auto DIFunctionsBefore = DIPreservationMap[NameOfWrappedPass].DIFunctions;
517   auto DIFunctionsAfter = DIPreservationAfter[NameOfWrappedPass].DIFunctions;
518 
519   auto DILocsBefore = DIPreservationMap[NameOfWrappedPass].DILocations;
520   auto DILocsAfter = DIPreservationAfter[NameOfWrappedPass].DILocations;
521 
522   auto InstToDelete = DIPreservationAfter[NameOfWrappedPass].InstToDelete;
523 
524   bool ShouldWriteIntoJSON = !OrigDIVerifyBugsReportFilePath.empty();
525   llvm::json::Array Bugs;
526 
527   bool ResultForFunc =
528       checkFunctions(DIFunctionsBefore, DIFunctionsAfter, NameOfWrappedPass,
529                      FileNameFromCU, ShouldWriteIntoJSON, Bugs);
530   bool ResultForInsts = checkInstructions(
531       DILocsBefore, DILocsAfter, InstToDelete, NameOfWrappedPass,
532       FileNameFromCU, ShouldWriteIntoJSON, Bugs);
533   bool Result = ResultForFunc && ResultForInsts;
534 
535   StringRef ResultBanner = NameOfWrappedPass != "" ? NameOfWrappedPass : Banner;
536   if (ShouldWriteIntoJSON && !Bugs.empty())
537     writeJSON(OrigDIVerifyBugsReportFilePath, FileNameFromCU, NameOfWrappedPass,
538               Bugs);
539 
540   if (Result)
541     dbg() << ResultBanner << ": PASS\n";
542   else
543     dbg() << ResultBanner << ": FAIL\n";
544 
545   LLVM_DEBUG(dbgs() << "\n\n");
546   return Result;
547 }
548 
549 namespace {
550 /// Return true if a mis-sized diagnostic is issued for \p DVI.
551 bool diagnoseMisSizedDbgValue(Module &M, DbgValueInst *DVI) {
552   // The size of a dbg.value's value operand should match the size of the
553   // variable it corresponds to.
554   //
555   // TODO: This, along with a check for non-null value operands, should be
556   // promoted to verifier failures.
557 
558   // For now, don't try to interpret anything more complicated than an empty
559   // DIExpression. Eventually we should try to handle OP_deref and fragments.
560   if (DVI->getExpression()->getNumElements())
561     return false;
562 
563   Value *V = DVI->getVariableLocationOp(0);
564   if (!V)
565     return false;
566 
567   Type *Ty = V->getType();
568   uint64_t ValueOperandSize = getAllocSizeInBits(M, Ty);
569   Optional<uint64_t> DbgVarSize = DVI->getFragmentSizeInBits();
570   if (!ValueOperandSize || !DbgVarSize)
571     return false;
572 
573   bool HasBadSize = false;
574   if (Ty->isIntegerTy()) {
575     auto Signedness = DVI->getVariable()->getSignedness();
576     if (Signedness && *Signedness == DIBasicType::Signedness::Signed)
577       HasBadSize = ValueOperandSize < *DbgVarSize;
578   } else {
579     HasBadSize = ValueOperandSize != *DbgVarSize;
580   }
581 
582   if (HasBadSize) {
583     dbg() << "ERROR: dbg.value operand has size " << ValueOperandSize
584           << ", but its variable has size " << *DbgVarSize << ": ";
585     DVI->print(dbg());
586     dbg() << "\n";
587   }
588   return HasBadSize;
589 }
590 
591 bool checkDebugifyMetadata(Module &M,
592                            iterator_range<Module::iterator> Functions,
593                            StringRef NameOfWrappedPass, StringRef Banner,
594                            bool Strip, DebugifyStatsMap *StatsMap) {
595   // Skip modules without debugify metadata.
596   NamedMDNode *NMD = M.getNamedMetadata("llvm.debugify");
597   if (!NMD) {
598     dbg() << Banner << ": Skipping module without debugify metadata\n";
599     return false;
600   }
601 
602   auto getDebugifyOperand = [&](unsigned Idx) -> unsigned {
603     return mdconst::extract<ConstantInt>(NMD->getOperand(Idx)->getOperand(0))
604         ->getZExtValue();
605   };
606   assert(NMD->getNumOperands() == 2 &&
607          "llvm.debugify should have exactly 2 operands!");
608   unsigned OriginalNumLines = getDebugifyOperand(0);
609   unsigned OriginalNumVars = getDebugifyOperand(1);
610   bool HasErrors = false;
611 
612   // Track debug info loss statistics if able.
613   DebugifyStatistics *Stats = nullptr;
614   if (StatsMap && !NameOfWrappedPass.empty())
615     Stats = &StatsMap->operator[](NameOfWrappedPass);
616 
617   BitVector MissingLines{OriginalNumLines, true};
618   BitVector MissingVars{OriginalNumVars, true};
619   for (Function &F : Functions) {
620     if (isFunctionSkipped(F))
621       continue;
622 
623     // Find missing lines.
624     for (Instruction &I : instructions(F)) {
625       if (isa<DbgValueInst>(&I) || isa<PHINode>(&I))
626         continue;
627 
628       auto DL = I.getDebugLoc();
629       if (DL && DL.getLine() != 0) {
630         MissingLines.reset(DL.getLine() - 1);
631         continue;
632       }
633 
634       if (!DL) {
635         dbg() << "WARNING: Instruction with empty DebugLoc in function ";
636         dbg() << F.getName() << " --";
637         I.print(dbg());
638         dbg() << "\n";
639       }
640     }
641 
642     // Find missing variables and mis-sized debug values.
643     for (Instruction &I : instructions(F)) {
644       auto *DVI = dyn_cast<DbgValueInst>(&I);
645       if (!DVI)
646         continue;
647 
648       unsigned Var = ~0U;
649       (void)to_integer(DVI->getVariable()->getName(), Var, 10);
650       assert(Var <= OriginalNumVars && "Unexpected name for DILocalVariable");
651       bool HasBadSize = diagnoseMisSizedDbgValue(M, DVI);
652       if (!HasBadSize)
653         MissingVars.reset(Var - 1);
654       HasErrors |= HasBadSize;
655     }
656   }
657 
658   // Print the results.
659   for (unsigned Idx : MissingLines.set_bits())
660     dbg() << "WARNING: Missing line " << Idx + 1 << "\n";
661 
662   for (unsigned Idx : MissingVars.set_bits())
663     dbg() << "WARNING: Missing variable " << Idx + 1 << "\n";
664 
665   // Update DI loss statistics.
666   if (Stats) {
667     Stats->NumDbgLocsExpected += OriginalNumLines;
668     Stats->NumDbgLocsMissing += MissingLines.count();
669     Stats->NumDbgValuesExpected += OriginalNumVars;
670     Stats->NumDbgValuesMissing += MissingVars.count();
671   }
672 
673   dbg() << Banner;
674   if (!NameOfWrappedPass.empty())
675     dbg() << " [" << NameOfWrappedPass << "]";
676   dbg() << ": " << (HasErrors ? "FAIL" : "PASS") << '\n';
677 
678   // Strip debugify metadata if required.
679   if (Strip)
680     return stripDebugifyMetadata(M);
681 
682   return false;
683 }
684 
685 /// ModulePass for attaching synthetic debug info to everything, used with the
686 /// legacy module pass manager.
687 struct DebugifyModulePass : public ModulePass {
688   bool runOnModule(Module &M) override {
689     return applyDebugify(M, Mode, DIPreservationMap, NameOfWrappedPass);
690   }
691 
692   DebugifyModulePass(enum DebugifyMode Mode = DebugifyMode::SyntheticDebugInfo,
693                      StringRef NameOfWrappedPass = "",
694                      DebugInfoPerPassMap *DIPreservationMap = nullptr)
695       : ModulePass(ID), NameOfWrappedPass(NameOfWrappedPass),
696         DIPreservationMap(DIPreservationMap), Mode(Mode) {}
697 
698   void getAnalysisUsage(AnalysisUsage &AU) const override {
699     AU.setPreservesAll();
700   }
701 
702   static char ID; // Pass identification.
703 
704 private:
705   StringRef NameOfWrappedPass;
706   DebugInfoPerPassMap *DIPreservationMap;
707   enum DebugifyMode Mode;
708 };
709 
710 /// FunctionPass for attaching synthetic debug info to instructions within a
711 /// single function, used with the legacy module pass manager.
712 struct DebugifyFunctionPass : public FunctionPass {
713   bool runOnFunction(Function &F) override {
714     return applyDebugify(F, Mode, DIPreservationMap, NameOfWrappedPass);
715   }
716 
717   DebugifyFunctionPass(
718       enum DebugifyMode Mode = DebugifyMode::SyntheticDebugInfo,
719       StringRef NameOfWrappedPass = "",
720       DebugInfoPerPassMap *DIPreservationMap = nullptr)
721       : FunctionPass(ID), NameOfWrappedPass(NameOfWrappedPass),
722         DIPreservationMap(DIPreservationMap), Mode(Mode) {}
723 
724   void getAnalysisUsage(AnalysisUsage &AU) const override {
725     AU.setPreservesAll();
726   }
727 
728   static char ID; // Pass identification.
729 
730 private:
731   StringRef NameOfWrappedPass;
732   DebugInfoPerPassMap *DIPreservationMap;
733   enum DebugifyMode Mode;
734 };
735 
736 /// ModulePass for checking debug info inserted by -debugify, used with the
737 /// legacy module pass manager.
738 struct CheckDebugifyModulePass : public ModulePass {
739   bool runOnModule(Module &M) override {
740     if (Mode == DebugifyMode::SyntheticDebugInfo)
741       return checkDebugifyMetadata(M, M.functions(), NameOfWrappedPass,
742                                    "CheckModuleDebugify", Strip, StatsMap);
743     return checkDebugInfoMetadata(
744         M, M.functions(), *DIPreservationMap,
745         "CheckModuleDebugify (original debuginfo)", NameOfWrappedPass,
746         OrigDIVerifyBugsReportFilePath);
747   }
748 
749   CheckDebugifyModulePass(
750       bool Strip = false, StringRef NameOfWrappedPass = "",
751       DebugifyStatsMap *StatsMap = nullptr,
752       enum DebugifyMode Mode = DebugifyMode::SyntheticDebugInfo,
753       DebugInfoPerPassMap *DIPreservationMap = nullptr,
754       StringRef OrigDIVerifyBugsReportFilePath = "")
755       : ModulePass(ID), NameOfWrappedPass(NameOfWrappedPass),
756         OrigDIVerifyBugsReportFilePath(OrigDIVerifyBugsReportFilePath),
757         StatsMap(StatsMap), DIPreservationMap(DIPreservationMap), Mode(Mode),
758         Strip(Strip) {}
759 
760   void getAnalysisUsage(AnalysisUsage &AU) const override {
761     AU.setPreservesAll();
762   }
763 
764   static char ID; // Pass identification.
765 
766 private:
767   StringRef NameOfWrappedPass;
768   StringRef OrigDIVerifyBugsReportFilePath;
769   DebugifyStatsMap *StatsMap;
770   DebugInfoPerPassMap *DIPreservationMap;
771   enum DebugifyMode Mode;
772   bool Strip;
773 };
774 
775 /// FunctionPass for checking debug info inserted by -debugify-function, used
776 /// with the legacy module pass manager.
777 struct CheckDebugifyFunctionPass : public FunctionPass {
778   bool runOnFunction(Function &F) override {
779     Module &M = *F.getParent();
780     auto FuncIt = F.getIterator();
781     if (Mode == DebugifyMode::SyntheticDebugInfo)
782       return checkDebugifyMetadata(M, make_range(FuncIt, std::next(FuncIt)),
783                                    NameOfWrappedPass, "CheckFunctionDebugify",
784                                    Strip, StatsMap);
785     return checkDebugInfoMetadata(
786         M, make_range(FuncIt, std::next(FuncIt)), *DIPreservationMap,
787         "CheckFunctionDebugify (original debuginfo)", NameOfWrappedPass,
788         OrigDIVerifyBugsReportFilePath);
789   }
790 
791   CheckDebugifyFunctionPass(
792       bool Strip = false, StringRef NameOfWrappedPass = "",
793       DebugifyStatsMap *StatsMap = nullptr,
794       enum DebugifyMode Mode = DebugifyMode::SyntheticDebugInfo,
795       DebugInfoPerPassMap *DIPreservationMap = nullptr,
796       StringRef OrigDIVerifyBugsReportFilePath = "")
797       : FunctionPass(ID), NameOfWrappedPass(NameOfWrappedPass),
798         OrigDIVerifyBugsReportFilePath(OrigDIVerifyBugsReportFilePath),
799         StatsMap(StatsMap), DIPreservationMap(DIPreservationMap), Mode(Mode),
800         Strip(Strip) {}
801 
802   void getAnalysisUsage(AnalysisUsage &AU) const override {
803     AU.setPreservesAll();
804   }
805 
806   static char ID; // Pass identification.
807 
808 private:
809   StringRef NameOfWrappedPass;
810   StringRef OrigDIVerifyBugsReportFilePath;
811   DebugifyStatsMap *StatsMap;
812   DebugInfoPerPassMap *DIPreservationMap;
813   enum DebugifyMode Mode;
814   bool Strip;
815 };
816 
817 } // end anonymous namespace
818 
819 void llvm::exportDebugifyStats(StringRef Path, const DebugifyStatsMap &Map) {
820   std::error_code EC;
821   raw_fd_ostream OS{Path, EC};
822   if (EC) {
823     errs() << "Could not open file: " << EC.message() << ", " << Path << '\n';
824     return;
825   }
826 
827   OS << "Pass Name" << ',' << "# of missing debug values" << ','
828      << "# of missing locations" << ',' << "Missing/Expected value ratio" << ','
829      << "Missing/Expected location ratio" << '\n';
830   for (const auto &Entry : Map) {
831     StringRef Pass = Entry.first;
832     DebugifyStatistics Stats = Entry.second;
833 
834     OS << Pass << ',' << Stats.NumDbgValuesMissing << ','
835        << Stats.NumDbgLocsMissing << ',' << Stats.getMissingValueRatio() << ','
836        << Stats.getEmptyLocationRatio() << '\n';
837   }
838 }
839 
840 ModulePass *createDebugifyModulePass(enum DebugifyMode Mode,
841                                      llvm::StringRef NameOfWrappedPass,
842                                      DebugInfoPerPassMap *DIPreservationMap) {
843   if (Mode == DebugifyMode::SyntheticDebugInfo)
844     return new DebugifyModulePass();
845   assert(Mode == DebugifyMode::OriginalDebugInfo && "Must be original mode");
846   return new DebugifyModulePass(Mode, NameOfWrappedPass, DIPreservationMap);
847 }
848 
849 FunctionPass *
850 createDebugifyFunctionPass(enum DebugifyMode Mode,
851                            llvm::StringRef NameOfWrappedPass,
852                            DebugInfoPerPassMap *DIPreservationMap) {
853   if (Mode == DebugifyMode::SyntheticDebugInfo)
854     return new DebugifyFunctionPass();
855   assert(Mode == DebugifyMode::OriginalDebugInfo && "Must be original mode");
856   return new DebugifyFunctionPass(Mode, NameOfWrappedPass, DIPreservationMap);
857 }
858 
859 PreservedAnalyses NewPMDebugifyPass::run(Module &M, ModuleAnalysisManager &) {
860   applyDebugifyMetadata(M, M.functions(),
861                         "ModuleDebugify: ", /*ApplyToMF*/ nullptr);
862   return PreservedAnalyses::all();
863 }
864 
865 ModulePass *createCheckDebugifyModulePass(
866     bool Strip, StringRef NameOfWrappedPass, DebugifyStatsMap *StatsMap,
867     enum DebugifyMode Mode, DebugInfoPerPassMap *DIPreservationMap,
868     StringRef OrigDIVerifyBugsReportFilePath) {
869   if (Mode == DebugifyMode::SyntheticDebugInfo)
870     return new CheckDebugifyModulePass(Strip, NameOfWrappedPass, StatsMap);
871   assert(Mode == DebugifyMode::OriginalDebugInfo && "Must be original mode");
872   return new CheckDebugifyModulePass(false, NameOfWrappedPass, nullptr, Mode,
873                                      DIPreservationMap,
874                                      OrigDIVerifyBugsReportFilePath);
875 }
876 
877 FunctionPass *createCheckDebugifyFunctionPass(
878     bool Strip, StringRef NameOfWrappedPass, DebugifyStatsMap *StatsMap,
879     enum DebugifyMode Mode, DebugInfoPerPassMap *DIPreservationMap,
880     StringRef OrigDIVerifyBugsReportFilePath) {
881   if (Mode == DebugifyMode::SyntheticDebugInfo)
882     return new CheckDebugifyFunctionPass(Strip, NameOfWrappedPass, StatsMap);
883   assert(Mode == DebugifyMode::OriginalDebugInfo && "Must be original mode");
884   return new CheckDebugifyFunctionPass(false, NameOfWrappedPass, nullptr, Mode,
885                                        DIPreservationMap,
886                                        OrigDIVerifyBugsReportFilePath);
887 }
888 
889 PreservedAnalyses NewPMCheckDebugifyPass::run(Module &M,
890                                               ModuleAnalysisManager &) {
891   checkDebugifyMetadata(M, M.functions(), "", "CheckModuleDebugify", false,
892                         nullptr);
893   return PreservedAnalyses::all();
894 }
895 
896 static bool isIgnoredPass(StringRef PassID) {
897   return isSpecialPass(PassID, {"PassManager", "PassAdaptor",
898                                 "AnalysisManagerProxy", "PrintFunctionPass",
899                                 "PrintModulePass", "BitcodeWriterPass",
900                                 "ThinLTOBitcodeWriterPass", "VerifierPass"});
901 }
902 
903 void DebugifyEachInstrumentation::registerCallbacks(
904     PassInstrumentationCallbacks &PIC) {
905   PIC.registerBeforeNonSkippedPassCallback([](StringRef P, Any IR) {
906     if (isIgnoredPass(P))
907       return;
908     if (any_isa<const Function *>(IR))
909       applyDebugify(*const_cast<Function *>(any_cast<const Function *>(IR)));
910     else if (any_isa<const Module *>(IR))
911       applyDebugify(*const_cast<Module *>(any_cast<const Module *>(IR)));
912   });
913   PIC.registerAfterPassCallback([this](StringRef P, Any IR,
914                                        const PreservedAnalyses &PassPA) {
915     if (isIgnoredPass(P))
916       return;
917     if (any_isa<const Function *>(IR)) {
918       auto &F = *const_cast<Function *>(any_cast<const Function *>(IR));
919       Module &M = *F.getParent();
920       auto It = F.getIterator();
921       checkDebugifyMetadata(M, make_range(It, std::next(It)), P,
922                             "CheckFunctionDebugify", /*Strip=*/true, &StatsMap);
923     } else if (any_isa<const Module *>(IR)) {
924       auto &M = *const_cast<Module *>(any_cast<const Module *>(IR));
925       checkDebugifyMetadata(M, M.functions(), P, "CheckModuleDebugify",
926                             /*Strip=*/true, &StatsMap);
927     }
928   });
929 }
930 
931 char DebugifyModulePass::ID = 0;
932 static RegisterPass<DebugifyModulePass> DM("debugify",
933                                            "Attach debug info to everything");
934 
935 char CheckDebugifyModulePass::ID = 0;
936 static RegisterPass<CheckDebugifyModulePass>
937     CDM("check-debugify", "Check debug info from -debugify");
938 
939 char DebugifyFunctionPass::ID = 0;
940 static RegisterPass<DebugifyFunctionPass> DF("debugify-function",
941                                              "Attach debug info to a function");
942 
943 char CheckDebugifyFunctionPass::ID = 0;
944 static RegisterPass<CheckDebugifyFunctionPass>
945     CDF("check-debugify-function", "Check debug info from -debugify-function");
946