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"
220128b950SSimon Pilgrim #include "llvm/IR/Module.h"
23*7de6dcd2SArthur Eubanks #include "llvm/IR/PassInstrumentation.h"
2425ee8613SDaniel Sanders #include "llvm/Pass.h"
254c1a1d3cSReid Kleckner #include "llvm/Support/CommandLine.h"
2625ee8613SDaniel Sanders 
2725ee8613SDaniel Sanders using namespace llvm;
2825ee8613SDaniel Sanders 
2925ee8613SDaniel Sanders namespace {
3025ee8613SDaniel Sanders 
3125ee8613SDaniel Sanders cl::opt<bool> Quiet("debugify-quiet",
3225ee8613SDaniel Sanders                     cl::desc("Suppress verbose debugify output"));
3325ee8613SDaniel Sanders 
3415f7bc78SDaniel Sanders enum class Level {
3515f7bc78SDaniel Sanders   Locations,
3615f7bc78SDaniel Sanders   LocationsAndVariables
3715f7bc78SDaniel Sanders };
3815f7bc78SDaniel Sanders cl::opt<Level> DebugifyLevel(
3915f7bc78SDaniel Sanders     "debugify-level", cl::desc("Kind of debug info to add"),
4015f7bc78SDaniel Sanders     cl::values(clEnumValN(Level::Locations, "locations", "Locations only"),
4115f7bc78SDaniel Sanders                clEnumValN(Level::LocationsAndVariables, "location+variables",
4215f7bc78SDaniel Sanders                           "Locations and Variables")),
4315f7bc78SDaniel Sanders     cl::init(Level::LocationsAndVariables));
4415f7bc78SDaniel Sanders 
4525ee8613SDaniel Sanders raw_ostream &dbg() { return Quiet ? nulls() : errs(); }
4625ee8613SDaniel Sanders 
4725ee8613SDaniel Sanders uint64_t getAllocSizeInBits(Module &M, Type *Ty) {
4825ee8613SDaniel Sanders   return Ty->isSized() ? M.getDataLayout().getTypeAllocSizeInBits(Ty) : 0;
4925ee8613SDaniel Sanders }
5025ee8613SDaniel Sanders 
5125ee8613SDaniel Sanders bool isFunctionSkipped(Function &F) {
5225ee8613SDaniel Sanders   return F.isDeclaration() || !F.hasExactDefinition();
5325ee8613SDaniel Sanders }
5425ee8613SDaniel Sanders 
5525ee8613SDaniel Sanders /// Find the basic block's terminating instruction.
5625ee8613SDaniel Sanders ///
5725ee8613SDaniel Sanders /// Special care is needed to handle musttail and deopt calls, as these behave
5825ee8613SDaniel Sanders /// like (but are in fact not) terminators.
5925ee8613SDaniel Sanders Instruction *findTerminatingInstruction(BasicBlock &BB) {
6025ee8613SDaniel Sanders   if (auto *I = BB.getTerminatingMustTailCall())
6125ee8613SDaniel Sanders     return I;
6225ee8613SDaniel Sanders   if (auto *I = BB.getTerminatingDeoptimizeCall())
6325ee8613SDaniel Sanders     return I;
6425ee8613SDaniel Sanders   return BB.getTerminator();
6525ee8613SDaniel Sanders }
661adeeabbSDaniel Sanders } // end anonymous namespace
6725ee8613SDaniel Sanders 
681adeeabbSDaniel Sanders bool llvm::applyDebugifyMetadata(
691adeeabbSDaniel Sanders     Module &M, iterator_range<Module::iterator> Functions, StringRef Banner,
701adeeabbSDaniel Sanders     std::function<bool(DIBuilder &DIB, Function &F)> ApplyToMF) {
7125ee8613SDaniel Sanders   // Skip modules with debug info.
7225ee8613SDaniel Sanders   if (M.getNamedMetadata("llvm.dbg.cu")) {
7325ee8613SDaniel Sanders     dbg() << Banner << "Skipping module with debug info\n";
7425ee8613SDaniel Sanders     return false;
7525ee8613SDaniel Sanders   }
7625ee8613SDaniel Sanders 
7725ee8613SDaniel Sanders   DIBuilder DIB(M);
7825ee8613SDaniel Sanders   LLVMContext &Ctx = M.getContext();
792a5675f1SVedant Kumar   auto *Int32Ty = Type::getInt32Ty(Ctx);
8025ee8613SDaniel Sanders 
8125ee8613SDaniel Sanders   // Get a DIType which corresponds to Ty.
8225ee8613SDaniel Sanders   DenseMap<uint64_t, DIType *> TypeCache;
8325ee8613SDaniel Sanders   auto getCachedDIType = [&](Type *Ty) -> DIType * {
8425ee8613SDaniel Sanders     uint64_t Size = getAllocSizeInBits(M, Ty);
8525ee8613SDaniel Sanders     DIType *&DTy = TypeCache[Size];
8625ee8613SDaniel Sanders     if (!DTy) {
8725ee8613SDaniel Sanders       std::string Name = "ty" + utostr(Size);
8825ee8613SDaniel Sanders       DTy = DIB.createBasicType(Name, Size, dwarf::DW_ATE_unsigned);
8925ee8613SDaniel Sanders     }
9025ee8613SDaniel Sanders     return DTy;
9125ee8613SDaniel Sanders   };
9225ee8613SDaniel Sanders 
9325ee8613SDaniel Sanders   unsigned NextLine = 1;
9425ee8613SDaniel Sanders   unsigned NextVar = 1;
9525ee8613SDaniel Sanders   auto File = DIB.createFile(M.getName(), "/");
9625ee8613SDaniel Sanders   auto CU = DIB.createCompileUnit(dwarf::DW_LANG_C, File, "debugify",
9725ee8613SDaniel Sanders                                   /*isOptimized=*/true, "", 0);
9825ee8613SDaniel Sanders 
9925ee8613SDaniel Sanders   // Visit each instruction.
10025ee8613SDaniel Sanders   for (Function &F : Functions) {
10125ee8613SDaniel Sanders     if (isFunctionSkipped(F))
10225ee8613SDaniel Sanders       continue;
10325ee8613SDaniel Sanders 
1042a5675f1SVedant Kumar     bool InsertedDbgVal = false;
10525ee8613SDaniel Sanders     auto SPType = DIB.createSubroutineType(DIB.getOrCreateTypeArray(None));
10625ee8613SDaniel Sanders     DISubprogram::DISPFlags SPFlags =
10725ee8613SDaniel Sanders         DISubprogram::SPFlagDefinition | DISubprogram::SPFlagOptimized;
10825ee8613SDaniel Sanders     if (F.hasPrivateLinkage() || F.hasInternalLinkage())
10925ee8613SDaniel Sanders       SPFlags |= DISubprogram::SPFlagLocalToUnit;
11025ee8613SDaniel Sanders     auto SP = DIB.createFunction(CU, F.getName(), F.getName(), File, NextLine,
11125ee8613SDaniel Sanders                                  SPType, NextLine, DINode::FlagZero, SPFlags);
11225ee8613SDaniel Sanders     F.setSubprogram(SP);
1132a5675f1SVedant Kumar 
1142a5675f1SVedant Kumar     // Helper that inserts a dbg.value before \p InsertBefore, copying the
1152a5675f1SVedant Kumar     // location (and possibly the type, if it's non-void) from \p TemplateInst.
1162a5675f1SVedant Kumar     auto insertDbgVal = [&](Instruction &TemplateInst,
1172a5675f1SVedant Kumar                             Instruction *InsertBefore) {
1182a5675f1SVedant Kumar       std::string Name = utostr(NextVar++);
1192a5675f1SVedant Kumar       Value *V = &TemplateInst;
1202a5675f1SVedant Kumar       if (TemplateInst.getType()->isVoidTy())
1212a5675f1SVedant Kumar         V = ConstantInt::get(Int32Ty, 0);
1222a5675f1SVedant Kumar       const DILocation *Loc = TemplateInst.getDebugLoc().get();
1232a5675f1SVedant Kumar       auto LocalVar = DIB.createAutoVariable(SP, Name, File, Loc->getLine(),
1242a5675f1SVedant Kumar                                              getCachedDIType(V->getType()),
1252a5675f1SVedant Kumar                                              /*AlwaysPreserve=*/true);
1262a5675f1SVedant Kumar       DIB.insertDbgValueIntrinsic(V, LocalVar, DIB.createExpression(), Loc,
1272a5675f1SVedant Kumar                                   InsertBefore);
1282a5675f1SVedant Kumar     };
1292a5675f1SVedant Kumar 
13025ee8613SDaniel Sanders     for (BasicBlock &BB : F) {
13125ee8613SDaniel Sanders       // Attach debug locations.
13225ee8613SDaniel Sanders       for (Instruction &I : BB)
13325ee8613SDaniel Sanders         I.setDebugLoc(DILocation::get(Ctx, NextLine++, 1, SP));
13425ee8613SDaniel Sanders 
13515f7bc78SDaniel Sanders       if (DebugifyLevel < Level::LocationsAndVariables)
13615f7bc78SDaniel Sanders         continue;
13715f7bc78SDaniel Sanders 
13825ee8613SDaniel Sanders       // Inserting debug values into EH pads can break IR invariants.
13925ee8613SDaniel Sanders       if (BB.isEHPad())
14025ee8613SDaniel Sanders         continue;
14125ee8613SDaniel Sanders 
14225ee8613SDaniel Sanders       // Find the terminating instruction, after which no debug values are
14325ee8613SDaniel Sanders       // attached.
14425ee8613SDaniel Sanders       Instruction *LastInst = findTerminatingInstruction(BB);
14525ee8613SDaniel Sanders       assert(LastInst && "Expected basic block with a terminator");
14625ee8613SDaniel Sanders 
14725ee8613SDaniel Sanders       // Maintain an insertion point which can't be invalidated when updates
14825ee8613SDaniel Sanders       // are made.
14925ee8613SDaniel Sanders       BasicBlock::iterator InsertPt = BB.getFirstInsertionPt();
15025ee8613SDaniel Sanders       assert(InsertPt != BB.end() && "Expected to find an insertion point");
15125ee8613SDaniel Sanders       Instruction *InsertBefore = &*InsertPt;
15225ee8613SDaniel Sanders 
15325ee8613SDaniel Sanders       // Attach debug values.
15425ee8613SDaniel Sanders       for (Instruction *I = &*BB.begin(); I != LastInst; I = I->getNextNode()) {
15525ee8613SDaniel Sanders         // Skip void-valued instructions.
15625ee8613SDaniel Sanders         if (I->getType()->isVoidTy())
15725ee8613SDaniel Sanders           continue;
15825ee8613SDaniel Sanders 
15925ee8613SDaniel Sanders         // Phis and EH pads must be grouped at the beginning of the block.
16025ee8613SDaniel Sanders         // Only advance the insertion point when we finish visiting these.
16125ee8613SDaniel Sanders         if (!isa<PHINode>(I) && !I->isEHPad())
16225ee8613SDaniel Sanders           InsertBefore = I->getNextNode();
16325ee8613SDaniel Sanders 
1642a5675f1SVedant Kumar         insertDbgVal(*I, InsertBefore);
1652a5675f1SVedant Kumar         InsertedDbgVal = true;
16625ee8613SDaniel Sanders       }
16725ee8613SDaniel Sanders     }
1682a5675f1SVedant Kumar     // Make sure we emit at least one dbg.value, otherwise MachineDebugify may
1692a5675f1SVedant Kumar     // not have anything to work with as it goes about inserting DBG_VALUEs.
1702a5675f1SVedant Kumar     // (It's common for MIR tests to be written containing skeletal IR with
1712a5675f1SVedant Kumar     // empty functions -- we're still interested in debugifying the MIR within
1722a5675f1SVedant Kumar     // those tests, and this helps with that.)
1732a5675f1SVedant Kumar     if (DebugifyLevel == Level::LocationsAndVariables && !InsertedDbgVal) {
1742a5675f1SVedant Kumar       auto *Term = findTerminatingInstruction(F.getEntryBlock());
1752a5675f1SVedant Kumar       insertDbgVal(*Term, Term);
1762a5675f1SVedant Kumar     }
1771adeeabbSDaniel Sanders     if (ApplyToMF)
1781adeeabbSDaniel Sanders       ApplyToMF(DIB, F);
17925ee8613SDaniel Sanders     DIB.finalizeSubprogram(SP);
18025ee8613SDaniel Sanders   }
18125ee8613SDaniel Sanders   DIB.finalize();
18225ee8613SDaniel Sanders 
18325ee8613SDaniel Sanders   // Track the number of distinct lines and variables.
18425ee8613SDaniel Sanders   NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.debugify");
18525ee8613SDaniel Sanders   auto addDebugifyOperand = [&](unsigned N) {
18625ee8613SDaniel Sanders     NMD->addOperand(MDNode::get(
1872a5675f1SVedant Kumar         Ctx, ValueAsMetadata::getConstant(ConstantInt::get(Int32Ty, N))));
18825ee8613SDaniel Sanders   };
18925ee8613SDaniel Sanders   addDebugifyOperand(NextLine - 1); // Original number of lines.
19025ee8613SDaniel Sanders   addDebugifyOperand(NextVar - 1);  // Original number of variables.
19125ee8613SDaniel Sanders   assert(NMD->getNumOperands() == 2 &&
19225ee8613SDaniel Sanders          "llvm.debugify should have exactly 2 operands!");
19325ee8613SDaniel Sanders 
19425ee8613SDaniel Sanders   // Claim that this synthetic debug info is valid.
19525ee8613SDaniel Sanders   StringRef DIVersionKey = "Debug Info Version";
19625ee8613SDaniel Sanders   if (!M.getModuleFlag(DIVersionKey))
19725ee8613SDaniel Sanders     M.addModuleFlag(Module::Warning, DIVersionKey, DEBUG_METADATA_VERSION);
19825ee8613SDaniel Sanders 
19925ee8613SDaniel Sanders   return true;
20025ee8613SDaniel Sanders }
20125ee8613SDaniel Sanders 
20298b93385SFangrui Song static bool applyDebugify(Function &F) {
20398b93385SFangrui Song   Module &M = *F.getParent();
20498b93385SFangrui Song   auto FuncIt = F.getIterator();
20598b93385SFangrui Song   return applyDebugifyMetadata(M, make_range(FuncIt, std::next(FuncIt)),
20698b93385SFangrui Song                                "FunctionDebugify: ", /*ApplyToMF=*/nullptr);
20798b93385SFangrui Song }
20898b93385SFangrui Song 
20998b93385SFangrui Song static bool applyDebugify(Module &M) {
21098b93385SFangrui Song   return applyDebugifyMetadata(M, M.functions(),
21198b93385SFangrui Song                                "ModuleDebugify: ", /*ApplyToMF=*/nullptr);
21298b93385SFangrui Song }
21398b93385SFangrui Song 
214122a6bfbSVedant Kumar bool llvm::stripDebugifyMetadata(Module &M) {
215122a6bfbSVedant Kumar   bool Changed = false;
216122a6bfbSVedant Kumar 
217122a6bfbSVedant Kumar   // Remove the llvm.debugify module-level named metadata.
218122a6bfbSVedant Kumar   NamedMDNode *DebugifyMD = M.getNamedMetadata("llvm.debugify");
219122a6bfbSVedant Kumar   if (DebugifyMD) {
220122a6bfbSVedant Kumar     M.eraseNamedMetadata(DebugifyMD);
221122a6bfbSVedant Kumar     Changed = true;
222122a6bfbSVedant Kumar   }
223122a6bfbSVedant Kumar 
224122a6bfbSVedant Kumar   // Strip out all debug intrinsics and supporting metadata (subprograms, types,
225122a6bfbSVedant Kumar   // variables, etc).
226122a6bfbSVedant Kumar   Changed |= StripDebugInfo(M);
227122a6bfbSVedant Kumar 
228122a6bfbSVedant Kumar   // Strip out the dead dbg.value prototype.
229122a6bfbSVedant Kumar   Function *DbgValF = M.getFunction("llvm.dbg.value");
230122a6bfbSVedant Kumar   if (DbgValF) {
231122a6bfbSVedant Kumar     assert(DbgValF->isDeclaration() && DbgValF->use_empty() &&
232122a6bfbSVedant Kumar            "Not all debug info stripped?");
233122a6bfbSVedant Kumar     DbgValF->eraseFromParent();
234122a6bfbSVedant Kumar     Changed = true;
235122a6bfbSVedant Kumar   }
236122a6bfbSVedant Kumar 
237122a6bfbSVedant Kumar   // Strip out the module-level Debug Info Version metadata.
238122a6bfbSVedant Kumar   // FIXME: There must be an easier way to remove an operand from a NamedMDNode.
239122a6bfbSVedant Kumar   NamedMDNode *NMD = M.getModuleFlagsMetadata();
2402fa656cdSVedant Kumar   if (!NMD)
2412fa656cdSVedant Kumar     return Changed;
242122a6bfbSVedant Kumar   SmallVector<MDNode *, 4> Flags;
243122a6bfbSVedant Kumar   for (MDNode *Flag : NMD->operands())
244122a6bfbSVedant Kumar     Flags.push_back(Flag);
245122a6bfbSVedant Kumar   NMD->clearOperands();
246122a6bfbSVedant Kumar   for (MDNode *Flag : Flags) {
247122a6bfbSVedant Kumar     MDString *Key = dyn_cast_or_null<MDString>(Flag->getOperand(1));
248122a6bfbSVedant Kumar     if (Key->getString() == "Debug Info Version") {
249122a6bfbSVedant Kumar       Changed = true;
250122a6bfbSVedant Kumar       continue;
251122a6bfbSVedant Kumar     }
252122a6bfbSVedant Kumar     NMD->addOperand(Flag);
253122a6bfbSVedant Kumar   }
254122a6bfbSVedant Kumar   // If we left it empty we might as well remove it.
255122a6bfbSVedant Kumar   if (NMD->getNumOperands() == 0)
256122a6bfbSVedant Kumar     NMD->eraseFromParent();
257122a6bfbSVedant Kumar 
258122a6bfbSVedant Kumar   return Changed;
259122a6bfbSVedant Kumar }
260122a6bfbSVedant Kumar 
2611adeeabbSDaniel Sanders namespace {
26225ee8613SDaniel Sanders /// Return true if a mis-sized diagnostic is issued for \p DVI.
26325ee8613SDaniel Sanders bool diagnoseMisSizedDbgValue(Module &M, DbgValueInst *DVI) {
26425ee8613SDaniel Sanders   // The size of a dbg.value's value operand should match the size of the
26525ee8613SDaniel Sanders   // variable it corresponds to.
26625ee8613SDaniel Sanders   //
26725ee8613SDaniel Sanders   // TODO: This, along with a check for non-null value operands, should be
26825ee8613SDaniel Sanders   // promoted to verifier failures.
26925ee8613SDaniel Sanders   Value *V = DVI->getValue();
27025ee8613SDaniel Sanders   if (!V)
27125ee8613SDaniel Sanders     return false;
27225ee8613SDaniel Sanders 
27325ee8613SDaniel Sanders   // For now, don't try to interpret anything more complicated than an empty
27425ee8613SDaniel Sanders   // DIExpression. Eventually we should try to handle OP_deref and fragments.
27525ee8613SDaniel Sanders   if (DVI->getExpression()->getNumElements())
27625ee8613SDaniel Sanders     return false;
27725ee8613SDaniel Sanders 
27825ee8613SDaniel Sanders   Type *Ty = V->getType();
27925ee8613SDaniel Sanders   uint64_t ValueOperandSize = getAllocSizeInBits(M, Ty);
28025ee8613SDaniel Sanders   Optional<uint64_t> DbgVarSize = DVI->getFragmentSizeInBits();
28125ee8613SDaniel Sanders   if (!ValueOperandSize || !DbgVarSize)
28225ee8613SDaniel Sanders     return false;
28325ee8613SDaniel Sanders 
28425ee8613SDaniel Sanders   bool HasBadSize = false;
28525ee8613SDaniel Sanders   if (Ty->isIntegerTy()) {
28625ee8613SDaniel Sanders     auto Signedness = DVI->getVariable()->getSignedness();
28725ee8613SDaniel Sanders     if (Signedness && *Signedness == DIBasicType::Signedness::Signed)
28825ee8613SDaniel Sanders       HasBadSize = ValueOperandSize < *DbgVarSize;
28925ee8613SDaniel Sanders   } else {
29025ee8613SDaniel Sanders     HasBadSize = ValueOperandSize != *DbgVarSize;
29125ee8613SDaniel Sanders   }
29225ee8613SDaniel Sanders 
29325ee8613SDaniel Sanders   if (HasBadSize) {
29425ee8613SDaniel Sanders     dbg() << "ERROR: dbg.value operand has size " << ValueOperandSize
29525ee8613SDaniel Sanders           << ", but its variable has size " << *DbgVarSize << ": ";
29625ee8613SDaniel Sanders     DVI->print(dbg());
29725ee8613SDaniel Sanders     dbg() << "\n";
29825ee8613SDaniel Sanders   }
29925ee8613SDaniel Sanders   return HasBadSize;
30025ee8613SDaniel Sanders }
30125ee8613SDaniel Sanders 
30225ee8613SDaniel Sanders bool checkDebugifyMetadata(Module &M,
30325ee8613SDaniel Sanders                            iterator_range<Module::iterator> Functions,
30425ee8613SDaniel Sanders                            StringRef NameOfWrappedPass, StringRef Banner,
30525ee8613SDaniel Sanders                            bool Strip, DebugifyStatsMap *StatsMap) {
30625ee8613SDaniel Sanders   // Skip modules without debugify metadata.
30725ee8613SDaniel Sanders   NamedMDNode *NMD = M.getNamedMetadata("llvm.debugify");
30825ee8613SDaniel Sanders   if (!NMD) {
30965030821SDjordje Todorovic     dbg() << Banner << ": Skipping module without debugify metadata\n";
31025ee8613SDaniel Sanders     return false;
31125ee8613SDaniel Sanders   }
31225ee8613SDaniel Sanders 
31325ee8613SDaniel Sanders   auto getDebugifyOperand = [&](unsigned Idx) -> unsigned {
31425ee8613SDaniel Sanders     return mdconst::extract<ConstantInt>(NMD->getOperand(Idx)->getOperand(0))
31525ee8613SDaniel Sanders         ->getZExtValue();
31625ee8613SDaniel Sanders   };
31725ee8613SDaniel Sanders   assert(NMD->getNumOperands() == 2 &&
31825ee8613SDaniel Sanders          "llvm.debugify should have exactly 2 operands!");
31925ee8613SDaniel Sanders   unsigned OriginalNumLines = getDebugifyOperand(0);
32025ee8613SDaniel Sanders   unsigned OriginalNumVars = getDebugifyOperand(1);
32125ee8613SDaniel Sanders   bool HasErrors = false;
32225ee8613SDaniel Sanders 
32325ee8613SDaniel Sanders   // Track debug info loss statistics if able.
32425ee8613SDaniel Sanders   DebugifyStatistics *Stats = nullptr;
32525ee8613SDaniel Sanders   if (StatsMap && !NameOfWrappedPass.empty())
32625ee8613SDaniel Sanders     Stats = &StatsMap->operator[](NameOfWrappedPass);
32725ee8613SDaniel Sanders 
32825ee8613SDaniel Sanders   BitVector MissingLines{OriginalNumLines, true};
32925ee8613SDaniel Sanders   BitVector MissingVars{OriginalNumVars, true};
33025ee8613SDaniel Sanders   for (Function &F : Functions) {
33125ee8613SDaniel Sanders     if (isFunctionSkipped(F))
33225ee8613SDaniel Sanders       continue;
33325ee8613SDaniel Sanders 
33425ee8613SDaniel Sanders     // Find missing lines.
33525ee8613SDaniel Sanders     for (Instruction &I : instructions(F)) {
336f64e457cSPierre-vh       if (isa<DbgValueInst>(&I) || isa<PHINode>(&I))
33725ee8613SDaniel Sanders         continue;
33825ee8613SDaniel Sanders 
33925ee8613SDaniel Sanders       auto DL = I.getDebugLoc();
34025ee8613SDaniel Sanders       if (DL && DL.getLine() != 0) {
34125ee8613SDaniel Sanders         MissingLines.reset(DL.getLine() - 1);
34225ee8613SDaniel Sanders         continue;
34325ee8613SDaniel Sanders       }
34425ee8613SDaniel Sanders 
34525ee8613SDaniel Sanders       if (!DL) {
346c1cad151SVedant Kumar         dbg() << "WARNING: Instruction with empty DebugLoc in function ";
34725ee8613SDaniel Sanders         dbg() << F.getName() << " --";
34825ee8613SDaniel Sanders         I.print(dbg());
34925ee8613SDaniel Sanders         dbg() << "\n";
35025ee8613SDaniel Sanders       }
35125ee8613SDaniel Sanders     }
35225ee8613SDaniel Sanders 
35325ee8613SDaniel Sanders     // Find missing variables and mis-sized debug values.
35425ee8613SDaniel Sanders     for (Instruction &I : instructions(F)) {
35525ee8613SDaniel Sanders       auto *DVI = dyn_cast<DbgValueInst>(&I);
35625ee8613SDaniel Sanders       if (!DVI)
35725ee8613SDaniel Sanders         continue;
35825ee8613SDaniel Sanders 
35925ee8613SDaniel Sanders       unsigned Var = ~0U;
36025ee8613SDaniel Sanders       (void)to_integer(DVI->getVariable()->getName(), Var, 10);
36125ee8613SDaniel Sanders       assert(Var <= OriginalNumVars && "Unexpected name for DILocalVariable");
36225ee8613SDaniel Sanders       bool HasBadSize = diagnoseMisSizedDbgValue(M, DVI);
36325ee8613SDaniel Sanders       if (!HasBadSize)
36425ee8613SDaniel Sanders         MissingVars.reset(Var - 1);
36525ee8613SDaniel Sanders       HasErrors |= HasBadSize;
36625ee8613SDaniel Sanders     }
36725ee8613SDaniel Sanders   }
36825ee8613SDaniel Sanders 
36925ee8613SDaniel Sanders   // Print the results.
37025ee8613SDaniel Sanders   for (unsigned Idx : MissingLines.set_bits())
37125ee8613SDaniel Sanders     dbg() << "WARNING: Missing line " << Idx + 1 << "\n";
37225ee8613SDaniel Sanders 
37325ee8613SDaniel Sanders   for (unsigned Idx : MissingVars.set_bits())
37425ee8613SDaniel Sanders     dbg() << "WARNING: Missing variable " << Idx + 1 << "\n";
37525ee8613SDaniel Sanders 
37625ee8613SDaniel Sanders   // Update DI loss statistics.
37725ee8613SDaniel Sanders   if (Stats) {
37825ee8613SDaniel Sanders     Stats->NumDbgLocsExpected += OriginalNumLines;
37925ee8613SDaniel Sanders     Stats->NumDbgLocsMissing += MissingLines.count();
38025ee8613SDaniel Sanders     Stats->NumDbgValuesExpected += OriginalNumVars;
38125ee8613SDaniel Sanders     Stats->NumDbgValuesMissing += MissingVars.count();
38225ee8613SDaniel Sanders   }
38325ee8613SDaniel Sanders 
38425ee8613SDaniel Sanders   dbg() << Banner;
38525ee8613SDaniel Sanders   if (!NameOfWrappedPass.empty())
38625ee8613SDaniel Sanders     dbg() << " [" << NameOfWrappedPass << "]";
38725ee8613SDaniel Sanders   dbg() << ": " << (HasErrors ? "FAIL" : "PASS") << '\n';
38825ee8613SDaniel Sanders 
389122a6bfbSVedant Kumar   // Strip debugify metadata if required.
390122a6bfbSVedant Kumar   if (Strip)
391122a6bfbSVedant Kumar     return stripDebugifyMetadata(M);
39225ee8613SDaniel Sanders 
39325ee8613SDaniel Sanders   return false;
39425ee8613SDaniel Sanders }
39525ee8613SDaniel Sanders 
39625ee8613SDaniel Sanders /// ModulePass for attaching synthetic debug info to everything, used with the
39725ee8613SDaniel Sanders /// legacy module pass manager.
39825ee8613SDaniel Sanders struct DebugifyModulePass : public ModulePass {
39998b93385SFangrui Song   bool runOnModule(Module &M) override { return applyDebugify(M); }
40025ee8613SDaniel Sanders 
40125ee8613SDaniel Sanders   DebugifyModulePass() : ModulePass(ID) {}
40225ee8613SDaniel Sanders 
40325ee8613SDaniel Sanders   void getAnalysisUsage(AnalysisUsage &AU) const override {
40425ee8613SDaniel Sanders     AU.setPreservesAll();
40525ee8613SDaniel Sanders   }
40625ee8613SDaniel Sanders 
40725ee8613SDaniel Sanders   static char ID; // Pass identification.
40825ee8613SDaniel Sanders };
40925ee8613SDaniel Sanders 
41025ee8613SDaniel Sanders /// FunctionPass for attaching synthetic debug info to instructions within a
41125ee8613SDaniel Sanders /// single function, used with the legacy module pass manager.
41225ee8613SDaniel Sanders struct DebugifyFunctionPass : public FunctionPass {
41398b93385SFangrui Song   bool runOnFunction(Function &F) override { return applyDebugify(F); }
41425ee8613SDaniel Sanders 
41525ee8613SDaniel Sanders   DebugifyFunctionPass() : FunctionPass(ID) {}
41625ee8613SDaniel Sanders 
41725ee8613SDaniel Sanders   void getAnalysisUsage(AnalysisUsage &AU) const override {
41825ee8613SDaniel Sanders     AU.setPreservesAll();
41925ee8613SDaniel Sanders   }
42025ee8613SDaniel Sanders 
42125ee8613SDaniel Sanders   static char ID; // Pass identification.
42225ee8613SDaniel Sanders };
42325ee8613SDaniel Sanders 
42425ee8613SDaniel Sanders /// ModulePass for checking debug info inserted by -debugify, used with the
42525ee8613SDaniel Sanders /// legacy module pass manager.
42625ee8613SDaniel Sanders struct CheckDebugifyModulePass : public ModulePass {
42725ee8613SDaniel Sanders   bool runOnModule(Module &M) override {
42825ee8613SDaniel Sanders     return checkDebugifyMetadata(M, M.functions(), NameOfWrappedPass,
42925ee8613SDaniel Sanders                                  "CheckModuleDebugify", Strip, StatsMap);
43025ee8613SDaniel Sanders   }
43125ee8613SDaniel Sanders 
43225ee8613SDaniel Sanders   CheckDebugifyModulePass(bool Strip = false, StringRef NameOfWrappedPass = "",
43325ee8613SDaniel Sanders                           DebugifyStatsMap *StatsMap = nullptr)
43425ee8613SDaniel Sanders       : ModulePass(ID), Strip(Strip), NameOfWrappedPass(NameOfWrappedPass),
43525ee8613SDaniel Sanders         StatsMap(StatsMap) {}
43625ee8613SDaniel Sanders 
43725ee8613SDaniel Sanders   void getAnalysisUsage(AnalysisUsage &AU) const override {
43825ee8613SDaniel Sanders     AU.setPreservesAll();
43925ee8613SDaniel Sanders   }
44025ee8613SDaniel Sanders 
44125ee8613SDaniel Sanders   static char ID; // Pass identification.
44225ee8613SDaniel Sanders 
44325ee8613SDaniel Sanders private:
44425ee8613SDaniel Sanders   bool Strip;
44525ee8613SDaniel Sanders   StringRef NameOfWrappedPass;
44625ee8613SDaniel Sanders   DebugifyStatsMap *StatsMap;
44725ee8613SDaniel Sanders };
44825ee8613SDaniel Sanders 
44925ee8613SDaniel Sanders /// FunctionPass for checking debug info inserted by -debugify-function, used
45025ee8613SDaniel Sanders /// with the legacy module pass manager.
45125ee8613SDaniel Sanders struct CheckDebugifyFunctionPass : public FunctionPass {
45225ee8613SDaniel Sanders   bool runOnFunction(Function &F) override {
45325ee8613SDaniel Sanders     Module &M = *F.getParent();
45425ee8613SDaniel Sanders     auto FuncIt = F.getIterator();
45525ee8613SDaniel Sanders     return checkDebugifyMetadata(M, make_range(FuncIt, std::next(FuncIt)),
45625ee8613SDaniel Sanders                                  NameOfWrappedPass, "CheckFunctionDebugify",
45725ee8613SDaniel Sanders                                  Strip, StatsMap);
45825ee8613SDaniel Sanders   }
45925ee8613SDaniel Sanders 
46025ee8613SDaniel Sanders   CheckDebugifyFunctionPass(bool Strip = false,
46125ee8613SDaniel Sanders                             StringRef NameOfWrappedPass = "",
46225ee8613SDaniel Sanders                             DebugifyStatsMap *StatsMap = nullptr)
46325ee8613SDaniel Sanders       : FunctionPass(ID), Strip(Strip), NameOfWrappedPass(NameOfWrappedPass),
46425ee8613SDaniel Sanders         StatsMap(StatsMap) {}
46525ee8613SDaniel Sanders 
46625ee8613SDaniel Sanders   void getAnalysisUsage(AnalysisUsage &AU) const override {
46725ee8613SDaniel Sanders     AU.setPreservesAll();
46825ee8613SDaniel Sanders   }
46925ee8613SDaniel Sanders 
47025ee8613SDaniel Sanders   static char ID; // Pass identification.
47125ee8613SDaniel Sanders 
47225ee8613SDaniel Sanders private:
47325ee8613SDaniel Sanders   bool Strip;
47425ee8613SDaniel Sanders   StringRef NameOfWrappedPass;
47525ee8613SDaniel Sanders   DebugifyStatsMap *StatsMap;
47625ee8613SDaniel Sanders };
47725ee8613SDaniel Sanders 
47825ee8613SDaniel Sanders } // end anonymous namespace
47925ee8613SDaniel Sanders 
48039856d5dSFangrui Song void llvm::exportDebugifyStats(StringRef Path, const DebugifyStatsMap &Map) {
48139856d5dSFangrui Song   std::error_code EC;
48239856d5dSFangrui Song   raw_fd_ostream OS{Path, EC};
48339856d5dSFangrui Song   if (EC) {
48439856d5dSFangrui Song     errs() << "Could not open file: " << EC.message() << ", " << Path << '\n';
48539856d5dSFangrui Song     return;
48639856d5dSFangrui Song   }
48725ee8613SDaniel Sanders 
48839856d5dSFangrui Song   OS << "Pass Name" << ',' << "# of missing debug values" << ','
48939856d5dSFangrui Song      << "# of missing locations" << ',' << "Missing/Expected value ratio" << ','
49039856d5dSFangrui Song      << "Missing/Expected location ratio" << '\n';
49139856d5dSFangrui Song   for (const auto &Entry : Map) {
49239856d5dSFangrui Song     StringRef Pass = Entry.first;
49339856d5dSFangrui Song     DebugifyStatistics Stats = Entry.second;
49439856d5dSFangrui Song 
49539856d5dSFangrui Song     OS << Pass << ',' << Stats.NumDbgValuesMissing << ','
49639856d5dSFangrui Song        << Stats.NumDbgLocsMissing << ',' << Stats.getMissingValueRatio() << ','
49739856d5dSFangrui Song        << Stats.getEmptyLocationRatio() << '\n';
49839856d5dSFangrui Song   }
49939856d5dSFangrui Song }
50039856d5dSFangrui Song 
50139856d5dSFangrui Song ModulePass *llvm::createDebugifyModulePass() {
50239856d5dSFangrui Song   return new DebugifyModulePass();
50339856d5dSFangrui Song }
50439856d5dSFangrui Song 
50539856d5dSFangrui Song FunctionPass *llvm::createDebugifyFunctionPass() {
50625ee8613SDaniel Sanders   return new DebugifyFunctionPass();
50725ee8613SDaniel Sanders }
50825ee8613SDaniel Sanders 
50925ee8613SDaniel Sanders PreservedAnalyses NewPMDebugifyPass::run(Module &M, ModuleAnalysisManager &) {
5101adeeabbSDaniel Sanders   applyDebugifyMetadata(M, M.functions(),
5111adeeabbSDaniel Sanders                         "ModuleDebugify: ", /*ApplyToMF*/ nullptr);
51225ee8613SDaniel Sanders   return PreservedAnalyses::all();
51325ee8613SDaniel Sanders }
51425ee8613SDaniel Sanders 
51539856d5dSFangrui Song ModulePass *llvm::createCheckDebugifyModulePass(bool Strip,
51625ee8613SDaniel Sanders                                                 StringRef NameOfWrappedPass,
51725ee8613SDaniel Sanders                                                 DebugifyStatsMap *StatsMap) {
51825ee8613SDaniel Sanders   return new CheckDebugifyModulePass(Strip, NameOfWrappedPass, StatsMap);
51925ee8613SDaniel Sanders }
52025ee8613SDaniel Sanders 
52139856d5dSFangrui Song FunctionPass *
52239856d5dSFangrui Song llvm::createCheckDebugifyFunctionPass(bool Strip, StringRef NameOfWrappedPass,
52325ee8613SDaniel Sanders                                       DebugifyStatsMap *StatsMap) {
52425ee8613SDaniel Sanders   return new CheckDebugifyFunctionPass(Strip, NameOfWrappedPass, StatsMap);
52525ee8613SDaniel Sanders }
52625ee8613SDaniel Sanders 
52725ee8613SDaniel Sanders PreservedAnalyses NewPMCheckDebugifyPass::run(Module &M,
52825ee8613SDaniel Sanders                                               ModuleAnalysisManager &) {
52925ee8613SDaniel Sanders   checkDebugifyMetadata(M, M.functions(), "", "CheckModuleDebugify", false,
53025ee8613SDaniel Sanders                         nullptr);
53125ee8613SDaniel Sanders   return PreservedAnalyses::all();
53225ee8613SDaniel Sanders }
53325ee8613SDaniel Sanders 
534*7de6dcd2SArthur Eubanks static bool isIgnoredPass(StringRef PassID) {
535*7de6dcd2SArthur Eubanks   return isSpecialPass(PassID, {"PassManager", "PassAdaptor",
536*7de6dcd2SArthur Eubanks                                 "AnalysisManagerProxy", "PrintFunctionPass",
537*7de6dcd2SArthur Eubanks                                 "PrintModulePass", "BitcodeWriterPass",
538*7de6dcd2SArthur Eubanks                                 "ThinLTOBitcodeWriterPass", "VerifierPass"});
539*7de6dcd2SArthur Eubanks }
540*7de6dcd2SArthur Eubanks 
54198b93385SFangrui Song void DebugifyEachInstrumentation::registerCallbacks(
54298b93385SFangrui Song     PassInstrumentationCallbacks &PIC) {
54398b93385SFangrui Song   PIC.registerBeforeNonSkippedPassCallback([](StringRef P, Any IR) {
544*7de6dcd2SArthur Eubanks     if (isIgnoredPass(P))
545*7de6dcd2SArthur Eubanks       return;
54698b93385SFangrui Song     if (any_isa<const Function *>(IR))
54798b93385SFangrui Song       applyDebugify(*const_cast<Function *>(any_cast<const Function *>(IR)));
54898b93385SFangrui Song     else if (any_isa<const Module *>(IR))
54998b93385SFangrui Song       applyDebugify(*const_cast<Module *>(any_cast<const Module *>(IR)));
55098b93385SFangrui Song   });
55198b93385SFangrui Song   PIC.registerAfterPassCallback([this](StringRef P, Any IR,
55298b93385SFangrui Song                                        const PreservedAnalyses &PassPA) {
553*7de6dcd2SArthur Eubanks     if (isIgnoredPass(P))
554*7de6dcd2SArthur Eubanks       return;
55598b93385SFangrui Song     if (any_isa<const Function *>(IR)) {
55698b93385SFangrui Song       auto &F = *const_cast<Function *>(any_cast<const Function *>(IR));
55798b93385SFangrui Song       Module &M = *F.getParent();
55898b93385SFangrui Song       auto It = F.getIterator();
55998b93385SFangrui Song       checkDebugifyMetadata(M, make_range(It, std::next(It)), P,
56098b93385SFangrui Song                             "CheckFunctionDebugify", /*Strip=*/true, &StatsMap);
56198b93385SFangrui Song     } else if (any_isa<const Module *>(IR)) {
56298b93385SFangrui Song       auto &M = *const_cast<Module *>(any_cast<const Module *>(IR));
56398b93385SFangrui Song       checkDebugifyMetadata(M, M.functions(), P, "CheckModuleDebugify",
56498b93385SFangrui Song                             /*Strip=*/true, &StatsMap);
56598b93385SFangrui Song     }
56698b93385SFangrui Song   });
56798b93385SFangrui Song }
56898b93385SFangrui Song 
56925ee8613SDaniel Sanders char DebugifyModulePass::ID = 0;
57025ee8613SDaniel Sanders static RegisterPass<DebugifyModulePass> DM("debugify",
57125ee8613SDaniel Sanders                                            "Attach debug info to everything");
57225ee8613SDaniel Sanders 
57325ee8613SDaniel Sanders char CheckDebugifyModulePass::ID = 0;
57425ee8613SDaniel Sanders static RegisterPass<CheckDebugifyModulePass>
57525ee8613SDaniel Sanders     CDM("check-debugify", "Check debug info from -debugify");
57625ee8613SDaniel Sanders 
57725ee8613SDaniel Sanders char DebugifyFunctionPass::ID = 0;
57825ee8613SDaniel Sanders static RegisterPass<DebugifyFunctionPass> DF("debugify-function",
57925ee8613SDaniel Sanders                                              "Attach debug info to a function");
58025ee8613SDaniel Sanders 
58125ee8613SDaniel Sanders char CheckDebugifyFunctionPass::ID = 0;
58225ee8613SDaniel Sanders static RegisterPass<CheckDebugifyFunctionPass>
58325ee8613SDaniel Sanders     CDF("check-debugify-function", "Check debug info from -debugify-function");
584