125ee8613SDaniel Sanders //===- Debugify.cpp - Attach synthetic debug info to everything -----------===//
225ee8613SDaniel Sanders //
325ee8613SDaniel Sanders // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
425ee8613SDaniel Sanders // See https://llvm.org/LICENSE.txt for license information.
525ee8613SDaniel Sanders // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
625ee8613SDaniel Sanders //
725ee8613SDaniel Sanders //===----------------------------------------------------------------------===//
825ee8613SDaniel Sanders ///
925ee8613SDaniel Sanders /// \file This pass attaches synthetic debug info to everything. It can be used
1025ee8613SDaniel Sanders /// to create targeted tests for debug info preservation.
1125ee8613SDaniel Sanders ///
1225ee8613SDaniel Sanders //===----------------------------------------------------------------------===//
1325ee8613SDaniel Sanders 
144c1a1d3cSReid Kleckner #include "llvm/Transforms/Utils/Debugify.h"
1525ee8613SDaniel Sanders #include "llvm/ADT/BitVector.h"
1625ee8613SDaniel Sanders #include "llvm/ADT/StringExtras.h"
1725ee8613SDaniel Sanders #include "llvm/IR/DIBuilder.h"
1825ee8613SDaniel Sanders #include "llvm/IR/DebugInfo.h"
1925ee8613SDaniel Sanders #include "llvm/IR/InstIterator.h"
2025ee8613SDaniel Sanders #include "llvm/IR/Instructions.h"
2125ee8613SDaniel Sanders #include "llvm/IR/IntrinsicInst.h"
2225ee8613SDaniel Sanders #include "llvm/IR/Module.h"
2325ee8613SDaniel Sanders #include "llvm/Pass.h"
244c1a1d3cSReid Kleckner #include "llvm/Support/CommandLine.h"
2525ee8613SDaniel Sanders 
2625ee8613SDaniel Sanders using namespace llvm;
2725ee8613SDaniel Sanders 
2825ee8613SDaniel Sanders namespace {
2925ee8613SDaniel Sanders 
3025ee8613SDaniel Sanders cl::opt<bool> Quiet("debugify-quiet",
3125ee8613SDaniel Sanders                     cl::desc("Suppress verbose debugify output"));
3225ee8613SDaniel Sanders 
3315f7bc78SDaniel Sanders enum class Level {
3415f7bc78SDaniel Sanders   Locations,
3515f7bc78SDaniel Sanders   LocationsAndVariables
3615f7bc78SDaniel Sanders };
3715f7bc78SDaniel Sanders cl::opt<Level> DebugifyLevel(
3815f7bc78SDaniel Sanders     "debugify-level", cl::desc("Kind of debug info to add"),
3915f7bc78SDaniel Sanders     cl::values(clEnumValN(Level::Locations, "locations", "Locations only"),
4015f7bc78SDaniel Sanders                clEnumValN(Level::LocationsAndVariables, "location+variables",
4115f7bc78SDaniel Sanders                           "Locations and Variables")),
4215f7bc78SDaniel Sanders     cl::init(Level::LocationsAndVariables));
4315f7bc78SDaniel Sanders 
4425ee8613SDaniel Sanders raw_ostream &dbg() { return Quiet ? nulls() : errs(); }
4525ee8613SDaniel Sanders 
4625ee8613SDaniel Sanders uint64_t getAllocSizeInBits(Module &M, Type *Ty) {
4725ee8613SDaniel Sanders   return Ty->isSized() ? M.getDataLayout().getTypeAllocSizeInBits(Ty) : 0;
4825ee8613SDaniel Sanders }
4925ee8613SDaniel Sanders 
5025ee8613SDaniel Sanders bool isFunctionSkipped(Function &F) {
5125ee8613SDaniel Sanders   return F.isDeclaration() || !F.hasExactDefinition();
5225ee8613SDaniel Sanders }
5325ee8613SDaniel Sanders 
5425ee8613SDaniel Sanders /// Find the basic block's terminating instruction.
5525ee8613SDaniel Sanders ///
5625ee8613SDaniel Sanders /// Special care is needed to handle musttail and deopt calls, as these behave
5725ee8613SDaniel Sanders /// like (but are in fact not) terminators.
5825ee8613SDaniel Sanders Instruction *findTerminatingInstruction(BasicBlock &BB) {
5925ee8613SDaniel Sanders   if (auto *I = BB.getTerminatingMustTailCall())
6025ee8613SDaniel Sanders     return I;
6125ee8613SDaniel Sanders   if (auto *I = BB.getTerminatingDeoptimizeCall())
6225ee8613SDaniel Sanders     return I;
6325ee8613SDaniel Sanders   return BB.getTerminator();
6425ee8613SDaniel Sanders }
651adeeabbSDaniel Sanders } // end anonymous namespace
6625ee8613SDaniel Sanders 
671adeeabbSDaniel Sanders bool llvm::applyDebugifyMetadata(
681adeeabbSDaniel Sanders     Module &M, iterator_range<Module::iterator> Functions, StringRef Banner,
691adeeabbSDaniel Sanders     std::function<bool(DIBuilder &DIB, Function &F)> ApplyToMF) {
7025ee8613SDaniel Sanders   // Skip modules with debug info.
7125ee8613SDaniel Sanders   if (M.getNamedMetadata("llvm.dbg.cu")) {
7225ee8613SDaniel Sanders     dbg() << Banner << "Skipping module with debug info\n";
7325ee8613SDaniel Sanders     return false;
7425ee8613SDaniel Sanders   }
7525ee8613SDaniel Sanders 
7625ee8613SDaniel Sanders   DIBuilder DIB(M);
7725ee8613SDaniel Sanders   LLVMContext &Ctx = M.getContext();
782a5675f1SVedant Kumar   auto *Int32Ty = Type::getInt32Ty(Ctx);
7925ee8613SDaniel Sanders 
8025ee8613SDaniel Sanders   // Get a DIType which corresponds to Ty.
8125ee8613SDaniel Sanders   DenseMap<uint64_t, DIType *> TypeCache;
8225ee8613SDaniel Sanders   auto getCachedDIType = [&](Type *Ty) -> DIType * {
8325ee8613SDaniel Sanders     uint64_t Size = getAllocSizeInBits(M, Ty);
8425ee8613SDaniel Sanders     DIType *&DTy = TypeCache[Size];
8525ee8613SDaniel Sanders     if (!DTy) {
8625ee8613SDaniel Sanders       std::string Name = "ty" + utostr(Size);
8725ee8613SDaniel Sanders       DTy = DIB.createBasicType(Name, Size, dwarf::DW_ATE_unsigned);
8825ee8613SDaniel Sanders     }
8925ee8613SDaniel Sanders     return DTy;
9025ee8613SDaniel Sanders   };
9125ee8613SDaniel Sanders 
9225ee8613SDaniel Sanders   unsigned NextLine = 1;
9325ee8613SDaniel Sanders   unsigned NextVar = 1;
9425ee8613SDaniel Sanders   auto File = DIB.createFile(M.getName(), "/");
9525ee8613SDaniel Sanders   auto CU = DIB.createCompileUnit(dwarf::DW_LANG_C, File, "debugify",
9625ee8613SDaniel Sanders                                   /*isOptimized=*/true, "", 0);
9725ee8613SDaniel Sanders 
9825ee8613SDaniel Sanders   // Visit each instruction.
9925ee8613SDaniel Sanders   for (Function &F : Functions) {
10025ee8613SDaniel Sanders     if (isFunctionSkipped(F))
10125ee8613SDaniel Sanders       continue;
10225ee8613SDaniel Sanders 
1032a5675f1SVedant Kumar     bool InsertedDbgVal = false;
10425ee8613SDaniel Sanders     auto SPType = DIB.createSubroutineType(DIB.getOrCreateTypeArray(None));
10525ee8613SDaniel Sanders     DISubprogram::DISPFlags SPFlags =
10625ee8613SDaniel Sanders         DISubprogram::SPFlagDefinition | DISubprogram::SPFlagOptimized;
10725ee8613SDaniel Sanders     if (F.hasPrivateLinkage() || F.hasInternalLinkage())
10825ee8613SDaniel Sanders       SPFlags |= DISubprogram::SPFlagLocalToUnit;
10925ee8613SDaniel Sanders     auto SP = DIB.createFunction(CU, F.getName(), F.getName(), File, NextLine,
11025ee8613SDaniel Sanders                                  SPType, NextLine, DINode::FlagZero, SPFlags);
11125ee8613SDaniel Sanders     F.setSubprogram(SP);
1122a5675f1SVedant Kumar 
1132a5675f1SVedant Kumar     // Helper that inserts a dbg.value before \p InsertBefore, copying the
1142a5675f1SVedant Kumar     // location (and possibly the type, if it's non-void) from \p TemplateInst.
1152a5675f1SVedant Kumar     auto insertDbgVal = [&](Instruction &TemplateInst,
1162a5675f1SVedant Kumar                             Instruction *InsertBefore) {
1172a5675f1SVedant Kumar       std::string Name = utostr(NextVar++);
1182a5675f1SVedant Kumar       Value *V = &TemplateInst;
1192a5675f1SVedant Kumar       if (TemplateInst.getType()->isVoidTy())
1202a5675f1SVedant Kumar         V = ConstantInt::get(Int32Ty, 0);
1212a5675f1SVedant Kumar       const DILocation *Loc = TemplateInst.getDebugLoc().get();
1222a5675f1SVedant Kumar       auto LocalVar = DIB.createAutoVariable(SP, Name, File, Loc->getLine(),
1232a5675f1SVedant Kumar                                              getCachedDIType(V->getType()),
1242a5675f1SVedant Kumar                                              /*AlwaysPreserve=*/true);
1252a5675f1SVedant Kumar       DIB.insertDbgValueIntrinsic(V, LocalVar, DIB.createExpression(), Loc,
1262a5675f1SVedant Kumar                                   InsertBefore);
1272a5675f1SVedant Kumar     };
1282a5675f1SVedant Kumar 
12925ee8613SDaniel Sanders     for (BasicBlock &BB : F) {
13025ee8613SDaniel Sanders       // Attach debug locations.
13125ee8613SDaniel Sanders       for (Instruction &I : BB)
13225ee8613SDaniel Sanders         I.setDebugLoc(DILocation::get(Ctx, NextLine++, 1, SP));
13325ee8613SDaniel Sanders 
13415f7bc78SDaniel Sanders       if (DebugifyLevel < Level::LocationsAndVariables)
13515f7bc78SDaniel Sanders         continue;
13615f7bc78SDaniel Sanders 
13725ee8613SDaniel Sanders       // Inserting debug values into EH pads can break IR invariants.
13825ee8613SDaniel Sanders       if (BB.isEHPad())
13925ee8613SDaniel Sanders         continue;
14025ee8613SDaniel Sanders 
14125ee8613SDaniel Sanders       // Find the terminating instruction, after which no debug values are
14225ee8613SDaniel Sanders       // attached.
14325ee8613SDaniel Sanders       Instruction *LastInst = findTerminatingInstruction(BB);
14425ee8613SDaniel Sanders       assert(LastInst && "Expected basic block with a terminator");
14525ee8613SDaniel Sanders 
14625ee8613SDaniel Sanders       // Maintain an insertion point which can't be invalidated when updates
14725ee8613SDaniel Sanders       // are made.
14825ee8613SDaniel Sanders       BasicBlock::iterator InsertPt = BB.getFirstInsertionPt();
14925ee8613SDaniel Sanders       assert(InsertPt != BB.end() && "Expected to find an insertion point");
15025ee8613SDaniel Sanders       Instruction *InsertBefore = &*InsertPt;
15125ee8613SDaniel Sanders 
15225ee8613SDaniel Sanders       // Attach debug values.
15325ee8613SDaniel Sanders       for (Instruction *I = &*BB.begin(); I != LastInst; I = I->getNextNode()) {
15425ee8613SDaniel Sanders         // Skip void-valued instructions.
15525ee8613SDaniel Sanders         if (I->getType()->isVoidTy())
15625ee8613SDaniel Sanders           continue;
15725ee8613SDaniel Sanders 
15825ee8613SDaniel Sanders         // Phis and EH pads must be grouped at the beginning of the block.
15925ee8613SDaniel Sanders         // Only advance the insertion point when we finish visiting these.
16025ee8613SDaniel Sanders         if (!isa<PHINode>(I) && !I->isEHPad())
16125ee8613SDaniel Sanders           InsertBefore = I->getNextNode();
16225ee8613SDaniel Sanders 
1632a5675f1SVedant Kumar         insertDbgVal(*I, InsertBefore);
1642a5675f1SVedant Kumar         InsertedDbgVal = true;
16525ee8613SDaniel Sanders       }
16625ee8613SDaniel Sanders     }
1672a5675f1SVedant Kumar     // Make sure we emit at least one dbg.value, otherwise MachineDebugify may
1682a5675f1SVedant Kumar     // not have anything to work with as it goes about inserting DBG_VALUEs.
1692a5675f1SVedant Kumar     // (It's common for MIR tests to be written containing skeletal IR with
1702a5675f1SVedant Kumar     // empty functions -- we're still interested in debugifying the MIR within
1712a5675f1SVedant Kumar     // those tests, and this helps with that.)
1722a5675f1SVedant Kumar     if (DebugifyLevel == Level::LocationsAndVariables && !InsertedDbgVal) {
1732a5675f1SVedant Kumar       auto *Term = findTerminatingInstruction(F.getEntryBlock());
1742a5675f1SVedant Kumar       insertDbgVal(*Term, Term);
1752a5675f1SVedant Kumar     }
1761adeeabbSDaniel Sanders     if (ApplyToMF)
1771adeeabbSDaniel Sanders       ApplyToMF(DIB, F);
17825ee8613SDaniel Sanders     DIB.finalizeSubprogram(SP);
17925ee8613SDaniel Sanders   }
18025ee8613SDaniel Sanders   DIB.finalize();
18125ee8613SDaniel Sanders 
18225ee8613SDaniel Sanders   // Track the number of distinct lines and variables.
18325ee8613SDaniel Sanders   NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.debugify");
18425ee8613SDaniel Sanders   auto addDebugifyOperand = [&](unsigned N) {
18525ee8613SDaniel Sanders     NMD->addOperand(MDNode::get(
1862a5675f1SVedant Kumar         Ctx, ValueAsMetadata::getConstant(ConstantInt::get(Int32Ty, N))));
18725ee8613SDaniel Sanders   };
18825ee8613SDaniel Sanders   addDebugifyOperand(NextLine - 1); // Original number of lines.
18925ee8613SDaniel Sanders   addDebugifyOperand(NextVar - 1);  // Original number of variables.
19025ee8613SDaniel Sanders   assert(NMD->getNumOperands() == 2 &&
19125ee8613SDaniel Sanders          "llvm.debugify should have exactly 2 operands!");
19225ee8613SDaniel Sanders 
19325ee8613SDaniel Sanders   // Claim that this synthetic debug info is valid.
19425ee8613SDaniel Sanders   StringRef DIVersionKey = "Debug Info Version";
19525ee8613SDaniel Sanders   if (!M.getModuleFlag(DIVersionKey))
19625ee8613SDaniel Sanders     M.addModuleFlag(Module::Warning, DIVersionKey, DEBUG_METADATA_VERSION);
19725ee8613SDaniel Sanders 
19825ee8613SDaniel Sanders   return true;
19925ee8613SDaniel Sanders }
20025ee8613SDaniel Sanders 
201122a6bfbSVedant Kumar bool llvm::stripDebugifyMetadata(Module &M) {
202122a6bfbSVedant Kumar   bool Changed = false;
203122a6bfbSVedant Kumar 
204122a6bfbSVedant Kumar   // Remove the llvm.debugify module-level named metadata.
205122a6bfbSVedant Kumar   NamedMDNode *DebugifyMD = M.getNamedMetadata("llvm.debugify");
206122a6bfbSVedant Kumar   if (DebugifyMD) {
207122a6bfbSVedant Kumar     M.eraseNamedMetadata(DebugifyMD);
208122a6bfbSVedant Kumar     Changed = true;
209122a6bfbSVedant Kumar   }
210122a6bfbSVedant Kumar 
211122a6bfbSVedant Kumar   // Strip out all debug intrinsics and supporting metadata (subprograms, types,
212122a6bfbSVedant Kumar   // variables, etc).
213122a6bfbSVedant Kumar   Changed |= StripDebugInfo(M);
214122a6bfbSVedant Kumar 
215122a6bfbSVedant Kumar   // Strip out the dead dbg.value prototype.
216122a6bfbSVedant Kumar   Function *DbgValF = M.getFunction("llvm.dbg.value");
217122a6bfbSVedant Kumar   if (DbgValF) {
218122a6bfbSVedant Kumar     assert(DbgValF->isDeclaration() && DbgValF->use_empty() &&
219122a6bfbSVedant Kumar            "Not all debug info stripped?");
220122a6bfbSVedant Kumar     DbgValF->eraseFromParent();
221122a6bfbSVedant Kumar     Changed = true;
222122a6bfbSVedant Kumar   }
223122a6bfbSVedant Kumar 
224122a6bfbSVedant Kumar   // Strip out the module-level Debug Info Version metadata.
225122a6bfbSVedant Kumar   // FIXME: There must be an easier way to remove an operand from a NamedMDNode.
226122a6bfbSVedant Kumar   NamedMDNode *NMD = M.getModuleFlagsMetadata();
227*2fa656cdSVedant Kumar   if (!NMD)
228*2fa656cdSVedant Kumar     return Changed;
229122a6bfbSVedant Kumar   SmallVector<MDNode *, 4> Flags;
230122a6bfbSVedant Kumar   for (MDNode *Flag : NMD->operands())
231122a6bfbSVedant Kumar     Flags.push_back(Flag);
232122a6bfbSVedant Kumar   NMD->clearOperands();
233122a6bfbSVedant Kumar   for (MDNode *Flag : Flags) {
234122a6bfbSVedant Kumar     MDString *Key = dyn_cast_or_null<MDString>(Flag->getOperand(1));
235122a6bfbSVedant Kumar     if (Key->getString() == "Debug Info Version") {
236122a6bfbSVedant Kumar       Changed = true;
237122a6bfbSVedant Kumar       continue;
238122a6bfbSVedant Kumar     }
239122a6bfbSVedant Kumar     NMD->addOperand(Flag);
240122a6bfbSVedant Kumar   }
241122a6bfbSVedant Kumar   // If we left it empty we might as well remove it.
242122a6bfbSVedant Kumar   if (NMD->getNumOperands() == 0)
243122a6bfbSVedant Kumar     NMD->eraseFromParent();
244122a6bfbSVedant Kumar 
245122a6bfbSVedant Kumar   return Changed;
246122a6bfbSVedant Kumar }
247122a6bfbSVedant Kumar 
2481adeeabbSDaniel Sanders namespace {
24925ee8613SDaniel Sanders /// Return true if a mis-sized diagnostic is issued for \p DVI.
25025ee8613SDaniel Sanders bool diagnoseMisSizedDbgValue(Module &M, DbgValueInst *DVI) {
25125ee8613SDaniel Sanders   // The size of a dbg.value's value operand should match the size of the
25225ee8613SDaniel Sanders   // variable it corresponds to.
25325ee8613SDaniel Sanders   //
25425ee8613SDaniel Sanders   // TODO: This, along with a check for non-null value operands, should be
25525ee8613SDaniel Sanders   // promoted to verifier failures.
25625ee8613SDaniel Sanders   Value *V = DVI->getValue();
25725ee8613SDaniel Sanders   if (!V)
25825ee8613SDaniel Sanders     return false;
25925ee8613SDaniel Sanders 
26025ee8613SDaniel Sanders   // For now, don't try to interpret anything more complicated than an empty
26125ee8613SDaniel Sanders   // DIExpression. Eventually we should try to handle OP_deref and fragments.
26225ee8613SDaniel Sanders   if (DVI->getExpression()->getNumElements())
26325ee8613SDaniel Sanders     return false;
26425ee8613SDaniel Sanders 
26525ee8613SDaniel Sanders   Type *Ty = V->getType();
26625ee8613SDaniel Sanders   uint64_t ValueOperandSize = getAllocSizeInBits(M, Ty);
26725ee8613SDaniel Sanders   Optional<uint64_t> DbgVarSize = DVI->getFragmentSizeInBits();
26825ee8613SDaniel Sanders   if (!ValueOperandSize || !DbgVarSize)
26925ee8613SDaniel Sanders     return false;
27025ee8613SDaniel Sanders 
27125ee8613SDaniel Sanders   bool HasBadSize = false;
27225ee8613SDaniel Sanders   if (Ty->isIntegerTy()) {
27325ee8613SDaniel Sanders     auto Signedness = DVI->getVariable()->getSignedness();
27425ee8613SDaniel Sanders     if (Signedness && *Signedness == DIBasicType::Signedness::Signed)
27525ee8613SDaniel Sanders       HasBadSize = ValueOperandSize < *DbgVarSize;
27625ee8613SDaniel Sanders   } else {
27725ee8613SDaniel Sanders     HasBadSize = ValueOperandSize != *DbgVarSize;
27825ee8613SDaniel Sanders   }
27925ee8613SDaniel Sanders 
28025ee8613SDaniel Sanders   if (HasBadSize) {
28125ee8613SDaniel Sanders     dbg() << "ERROR: dbg.value operand has size " << ValueOperandSize
28225ee8613SDaniel Sanders           << ", but its variable has size " << *DbgVarSize << ": ";
28325ee8613SDaniel Sanders     DVI->print(dbg());
28425ee8613SDaniel Sanders     dbg() << "\n";
28525ee8613SDaniel Sanders   }
28625ee8613SDaniel Sanders   return HasBadSize;
28725ee8613SDaniel Sanders }
28825ee8613SDaniel Sanders 
28925ee8613SDaniel Sanders bool checkDebugifyMetadata(Module &M,
29025ee8613SDaniel Sanders                            iterator_range<Module::iterator> Functions,
29125ee8613SDaniel Sanders                            StringRef NameOfWrappedPass, StringRef Banner,
29225ee8613SDaniel Sanders                            bool Strip, DebugifyStatsMap *StatsMap) {
29325ee8613SDaniel Sanders   // Skip modules without debugify metadata.
29425ee8613SDaniel Sanders   NamedMDNode *NMD = M.getNamedMetadata("llvm.debugify");
29525ee8613SDaniel Sanders   if (!NMD) {
29625ee8613SDaniel Sanders     dbg() << Banner << "Skipping module without debugify metadata\n";
29725ee8613SDaniel Sanders     return false;
29825ee8613SDaniel Sanders   }
29925ee8613SDaniel Sanders 
30025ee8613SDaniel Sanders   auto getDebugifyOperand = [&](unsigned Idx) -> unsigned {
30125ee8613SDaniel Sanders     return mdconst::extract<ConstantInt>(NMD->getOperand(Idx)->getOperand(0))
30225ee8613SDaniel Sanders         ->getZExtValue();
30325ee8613SDaniel Sanders   };
30425ee8613SDaniel Sanders   assert(NMD->getNumOperands() == 2 &&
30525ee8613SDaniel Sanders          "llvm.debugify should have exactly 2 operands!");
30625ee8613SDaniel Sanders   unsigned OriginalNumLines = getDebugifyOperand(0);
30725ee8613SDaniel Sanders   unsigned OriginalNumVars = getDebugifyOperand(1);
30825ee8613SDaniel Sanders   bool HasErrors = false;
30925ee8613SDaniel Sanders 
31025ee8613SDaniel Sanders   // Track debug info loss statistics if able.
31125ee8613SDaniel Sanders   DebugifyStatistics *Stats = nullptr;
31225ee8613SDaniel Sanders   if (StatsMap && !NameOfWrappedPass.empty())
31325ee8613SDaniel Sanders     Stats = &StatsMap->operator[](NameOfWrappedPass);
31425ee8613SDaniel Sanders 
31525ee8613SDaniel Sanders   BitVector MissingLines{OriginalNumLines, true};
31625ee8613SDaniel Sanders   BitVector MissingVars{OriginalNumVars, true};
31725ee8613SDaniel Sanders   for (Function &F : Functions) {
31825ee8613SDaniel Sanders     if (isFunctionSkipped(F))
31925ee8613SDaniel Sanders       continue;
32025ee8613SDaniel Sanders 
32125ee8613SDaniel Sanders     // Find missing lines.
32225ee8613SDaniel Sanders     for (Instruction &I : instructions(F)) {
323f64e457cSPierre-vh       if (isa<DbgValueInst>(&I) || isa<PHINode>(&I))
32425ee8613SDaniel Sanders         continue;
32525ee8613SDaniel Sanders 
32625ee8613SDaniel Sanders       auto DL = I.getDebugLoc();
32725ee8613SDaniel Sanders       if (DL && DL.getLine() != 0) {
32825ee8613SDaniel Sanders         MissingLines.reset(DL.getLine() - 1);
32925ee8613SDaniel Sanders         continue;
33025ee8613SDaniel Sanders       }
33125ee8613SDaniel Sanders 
33225ee8613SDaniel Sanders       if (!DL) {
33325ee8613SDaniel Sanders         dbg() << "ERROR: Instruction with empty DebugLoc in function ";
33425ee8613SDaniel Sanders         dbg() << F.getName() << " --";
33525ee8613SDaniel Sanders         I.print(dbg());
33625ee8613SDaniel Sanders         dbg() << "\n";
33725ee8613SDaniel Sanders         HasErrors = true;
33825ee8613SDaniel Sanders       }
33925ee8613SDaniel Sanders     }
34025ee8613SDaniel Sanders 
34125ee8613SDaniel Sanders     // Find missing variables and mis-sized debug values.
34225ee8613SDaniel Sanders     for (Instruction &I : instructions(F)) {
34325ee8613SDaniel Sanders       auto *DVI = dyn_cast<DbgValueInst>(&I);
34425ee8613SDaniel Sanders       if (!DVI)
34525ee8613SDaniel Sanders         continue;
34625ee8613SDaniel Sanders 
34725ee8613SDaniel Sanders       unsigned Var = ~0U;
34825ee8613SDaniel Sanders       (void)to_integer(DVI->getVariable()->getName(), Var, 10);
34925ee8613SDaniel Sanders       assert(Var <= OriginalNumVars && "Unexpected name for DILocalVariable");
35025ee8613SDaniel Sanders       bool HasBadSize = diagnoseMisSizedDbgValue(M, DVI);
35125ee8613SDaniel Sanders       if (!HasBadSize)
35225ee8613SDaniel Sanders         MissingVars.reset(Var - 1);
35325ee8613SDaniel Sanders       HasErrors |= HasBadSize;
35425ee8613SDaniel Sanders     }
35525ee8613SDaniel Sanders   }
35625ee8613SDaniel Sanders 
35725ee8613SDaniel Sanders   // Print the results.
35825ee8613SDaniel Sanders   for (unsigned Idx : MissingLines.set_bits())
35925ee8613SDaniel Sanders     dbg() << "WARNING: Missing line " << Idx + 1 << "\n";
36025ee8613SDaniel Sanders 
36125ee8613SDaniel Sanders   for (unsigned Idx : MissingVars.set_bits())
36225ee8613SDaniel Sanders     dbg() << "WARNING: Missing variable " << Idx + 1 << "\n";
36325ee8613SDaniel Sanders 
36425ee8613SDaniel Sanders   // Update DI loss statistics.
36525ee8613SDaniel Sanders   if (Stats) {
36625ee8613SDaniel Sanders     Stats->NumDbgLocsExpected += OriginalNumLines;
36725ee8613SDaniel Sanders     Stats->NumDbgLocsMissing += MissingLines.count();
36825ee8613SDaniel Sanders     Stats->NumDbgValuesExpected += OriginalNumVars;
36925ee8613SDaniel Sanders     Stats->NumDbgValuesMissing += MissingVars.count();
37025ee8613SDaniel Sanders   }
37125ee8613SDaniel Sanders 
37225ee8613SDaniel Sanders   dbg() << Banner;
37325ee8613SDaniel Sanders   if (!NameOfWrappedPass.empty())
37425ee8613SDaniel Sanders     dbg() << " [" << NameOfWrappedPass << "]";
37525ee8613SDaniel Sanders   dbg() << ": " << (HasErrors ? "FAIL" : "PASS") << '\n';
37625ee8613SDaniel Sanders 
377122a6bfbSVedant Kumar   // Strip debugify metadata if required.
378122a6bfbSVedant Kumar   if (Strip)
379122a6bfbSVedant Kumar     return stripDebugifyMetadata(M);
38025ee8613SDaniel Sanders 
38125ee8613SDaniel Sanders   return false;
38225ee8613SDaniel Sanders }
38325ee8613SDaniel Sanders 
38425ee8613SDaniel Sanders /// ModulePass for attaching synthetic debug info to everything, used with the
38525ee8613SDaniel Sanders /// legacy module pass manager.
38625ee8613SDaniel Sanders struct DebugifyModulePass : public ModulePass {
38725ee8613SDaniel Sanders   bool runOnModule(Module &M) override {
3881adeeabbSDaniel Sanders     return applyDebugifyMetadata(M, M.functions(),
3891adeeabbSDaniel Sanders                                  "ModuleDebugify: ", /*ApplyToMF*/ nullptr);
39025ee8613SDaniel Sanders   }
39125ee8613SDaniel Sanders 
39225ee8613SDaniel Sanders   DebugifyModulePass() : ModulePass(ID) {}
39325ee8613SDaniel Sanders 
39425ee8613SDaniel Sanders   void getAnalysisUsage(AnalysisUsage &AU) const override {
39525ee8613SDaniel Sanders     AU.setPreservesAll();
39625ee8613SDaniel Sanders   }
39725ee8613SDaniel Sanders 
39825ee8613SDaniel Sanders   static char ID; // Pass identification.
39925ee8613SDaniel Sanders };
40025ee8613SDaniel Sanders 
40125ee8613SDaniel Sanders /// FunctionPass for attaching synthetic debug info to instructions within a
40225ee8613SDaniel Sanders /// single function, used with the legacy module pass manager.
40325ee8613SDaniel Sanders struct DebugifyFunctionPass : public FunctionPass {
40425ee8613SDaniel Sanders   bool runOnFunction(Function &F) override {
40525ee8613SDaniel Sanders     Module &M = *F.getParent();
40625ee8613SDaniel Sanders     auto FuncIt = F.getIterator();
40725ee8613SDaniel Sanders     return applyDebugifyMetadata(M, make_range(FuncIt, std::next(FuncIt)),
4081adeeabbSDaniel Sanders                                  "FunctionDebugify: ", /*ApplyToMF*/ nullptr);
40925ee8613SDaniel Sanders   }
41025ee8613SDaniel Sanders 
41125ee8613SDaniel Sanders   DebugifyFunctionPass() : FunctionPass(ID) {}
41225ee8613SDaniel Sanders 
41325ee8613SDaniel Sanders   void getAnalysisUsage(AnalysisUsage &AU) const override {
41425ee8613SDaniel Sanders     AU.setPreservesAll();
41525ee8613SDaniel Sanders   }
41625ee8613SDaniel Sanders 
41725ee8613SDaniel Sanders   static char ID; // Pass identification.
41825ee8613SDaniel Sanders };
41925ee8613SDaniel Sanders 
42025ee8613SDaniel Sanders /// ModulePass for checking debug info inserted by -debugify, used with the
42125ee8613SDaniel Sanders /// legacy module pass manager.
42225ee8613SDaniel Sanders struct CheckDebugifyModulePass : public ModulePass {
42325ee8613SDaniel Sanders   bool runOnModule(Module &M) override {
42425ee8613SDaniel Sanders     return checkDebugifyMetadata(M, M.functions(), NameOfWrappedPass,
42525ee8613SDaniel Sanders                                  "CheckModuleDebugify", Strip, StatsMap);
42625ee8613SDaniel Sanders   }
42725ee8613SDaniel Sanders 
42825ee8613SDaniel Sanders   CheckDebugifyModulePass(bool Strip = false, StringRef NameOfWrappedPass = "",
42925ee8613SDaniel Sanders                           DebugifyStatsMap *StatsMap = nullptr)
43025ee8613SDaniel Sanders       : ModulePass(ID), Strip(Strip), NameOfWrappedPass(NameOfWrappedPass),
43125ee8613SDaniel Sanders         StatsMap(StatsMap) {}
43225ee8613SDaniel Sanders 
43325ee8613SDaniel Sanders   void getAnalysisUsage(AnalysisUsage &AU) const override {
43425ee8613SDaniel Sanders     AU.setPreservesAll();
43525ee8613SDaniel Sanders   }
43625ee8613SDaniel Sanders 
43725ee8613SDaniel Sanders   static char ID; // Pass identification.
43825ee8613SDaniel Sanders 
43925ee8613SDaniel Sanders private:
44025ee8613SDaniel Sanders   bool Strip;
44125ee8613SDaniel Sanders   StringRef NameOfWrappedPass;
44225ee8613SDaniel Sanders   DebugifyStatsMap *StatsMap;
44325ee8613SDaniel Sanders };
44425ee8613SDaniel Sanders 
44525ee8613SDaniel Sanders /// FunctionPass for checking debug info inserted by -debugify-function, used
44625ee8613SDaniel Sanders /// with the legacy module pass manager.
44725ee8613SDaniel Sanders struct CheckDebugifyFunctionPass : public FunctionPass {
44825ee8613SDaniel Sanders   bool runOnFunction(Function &F) override {
44925ee8613SDaniel Sanders     Module &M = *F.getParent();
45025ee8613SDaniel Sanders     auto FuncIt = F.getIterator();
45125ee8613SDaniel Sanders     return checkDebugifyMetadata(M, make_range(FuncIt, std::next(FuncIt)),
45225ee8613SDaniel Sanders                                  NameOfWrappedPass, "CheckFunctionDebugify",
45325ee8613SDaniel Sanders                                  Strip, StatsMap);
45425ee8613SDaniel Sanders   }
45525ee8613SDaniel Sanders 
45625ee8613SDaniel Sanders   CheckDebugifyFunctionPass(bool Strip = false,
45725ee8613SDaniel Sanders                             StringRef NameOfWrappedPass = "",
45825ee8613SDaniel Sanders                             DebugifyStatsMap *StatsMap = nullptr)
45925ee8613SDaniel Sanders       : FunctionPass(ID), Strip(Strip), NameOfWrappedPass(NameOfWrappedPass),
46025ee8613SDaniel Sanders         StatsMap(StatsMap) {}
46125ee8613SDaniel Sanders 
46225ee8613SDaniel Sanders   void getAnalysisUsage(AnalysisUsage &AU) const override {
46325ee8613SDaniel Sanders     AU.setPreservesAll();
46425ee8613SDaniel Sanders   }
46525ee8613SDaniel Sanders 
46625ee8613SDaniel Sanders   static char ID; // Pass identification.
46725ee8613SDaniel Sanders 
46825ee8613SDaniel Sanders private:
46925ee8613SDaniel Sanders   bool Strip;
47025ee8613SDaniel Sanders   StringRef NameOfWrappedPass;
47125ee8613SDaniel Sanders   DebugifyStatsMap *StatsMap;
47225ee8613SDaniel Sanders };
47325ee8613SDaniel Sanders 
47425ee8613SDaniel Sanders } // end anonymous namespace
47525ee8613SDaniel Sanders 
47625ee8613SDaniel Sanders ModulePass *createDebugifyModulePass() { return new DebugifyModulePass(); }
47725ee8613SDaniel Sanders 
47825ee8613SDaniel Sanders FunctionPass *createDebugifyFunctionPass() {
47925ee8613SDaniel Sanders   return new DebugifyFunctionPass();
48025ee8613SDaniel Sanders }
48125ee8613SDaniel Sanders 
48225ee8613SDaniel Sanders PreservedAnalyses NewPMDebugifyPass::run(Module &M, ModuleAnalysisManager &) {
4831adeeabbSDaniel Sanders   applyDebugifyMetadata(M, M.functions(),
4841adeeabbSDaniel Sanders                         "ModuleDebugify: ", /*ApplyToMF*/ nullptr);
48525ee8613SDaniel Sanders   return PreservedAnalyses::all();
48625ee8613SDaniel Sanders }
48725ee8613SDaniel Sanders 
48825ee8613SDaniel Sanders ModulePass *createCheckDebugifyModulePass(bool Strip,
48925ee8613SDaniel Sanders                                           StringRef NameOfWrappedPass,
49025ee8613SDaniel Sanders                                           DebugifyStatsMap *StatsMap) {
49125ee8613SDaniel Sanders   return new CheckDebugifyModulePass(Strip, NameOfWrappedPass, StatsMap);
49225ee8613SDaniel Sanders }
49325ee8613SDaniel Sanders 
49425ee8613SDaniel Sanders FunctionPass *createCheckDebugifyFunctionPass(bool Strip,
49525ee8613SDaniel Sanders                                               StringRef NameOfWrappedPass,
49625ee8613SDaniel Sanders                                               DebugifyStatsMap *StatsMap) {
49725ee8613SDaniel Sanders   return new CheckDebugifyFunctionPass(Strip, NameOfWrappedPass, StatsMap);
49825ee8613SDaniel Sanders }
49925ee8613SDaniel Sanders 
50025ee8613SDaniel Sanders PreservedAnalyses NewPMCheckDebugifyPass::run(Module &M,
50125ee8613SDaniel Sanders                                               ModuleAnalysisManager &) {
50225ee8613SDaniel Sanders   checkDebugifyMetadata(M, M.functions(), "", "CheckModuleDebugify", false,
50325ee8613SDaniel Sanders                         nullptr);
50425ee8613SDaniel Sanders   return PreservedAnalyses::all();
50525ee8613SDaniel Sanders }
50625ee8613SDaniel Sanders 
50725ee8613SDaniel Sanders char DebugifyModulePass::ID = 0;
50825ee8613SDaniel Sanders static RegisterPass<DebugifyModulePass> DM("debugify",
50925ee8613SDaniel Sanders                                            "Attach debug info to everything");
51025ee8613SDaniel Sanders 
51125ee8613SDaniel Sanders char CheckDebugifyModulePass::ID = 0;
51225ee8613SDaniel Sanders static RegisterPass<CheckDebugifyModulePass>
51325ee8613SDaniel Sanders     CDM("check-debugify", "Check debug info from -debugify");
51425ee8613SDaniel Sanders 
51525ee8613SDaniel Sanders char DebugifyFunctionPass::ID = 0;
51625ee8613SDaniel Sanders static RegisterPass<DebugifyFunctionPass> DF("debugify-function",
51725ee8613SDaniel Sanders                                              "Attach debug info to a function");
51825ee8613SDaniel Sanders 
51925ee8613SDaniel Sanders char CheckDebugifyFunctionPass::ID = 0;
52025ee8613SDaniel Sanders static RegisterPass<CheckDebugifyFunctionPass>
52125ee8613SDaniel Sanders     CDF("check-debugify-function", "Check debug info from -debugify-function");
522