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 
3325ee8613SDaniel Sanders raw_ostream &dbg() { return Quiet ? nulls() : errs(); }
3425ee8613SDaniel Sanders 
3525ee8613SDaniel Sanders uint64_t getAllocSizeInBits(Module &M, Type *Ty) {
3625ee8613SDaniel Sanders   return Ty->isSized() ? M.getDataLayout().getTypeAllocSizeInBits(Ty) : 0;
3725ee8613SDaniel Sanders }
3825ee8613SDaniel Sanders 
3925ee8613SDaniel Sanders bool isFunctionSkipped(Function &F) {
4025ee8613SDaniel Sanders   return F.isDeclaration() || !F.hasExactDefinition();
4125ee8613SDaniel Sanders }
4225ee8613SDaniel Sanders 
4325ee8613SDaniel Sanders /// Find the basic block's terminating instruction.
4425ee8613SDaniel Sanders ///
4525ee8613SDaniel Sanders /// Special care is needed to handle musttail and deopt calls, as these behave
4625ee8613SDaniel Sanders /// like (but are in fact not) terminators.
4725ee8613SDaniel Sanders Instruction *findTerminatingInstruction(BasicBlock &BB) {
4825ee8613SDaniel Sanders   if (auto *I = BB.getTerminatingMustTailCall())
4925ee8613SDaniel Sanders     return I;
5025ee8613SDaniel Sanders   if (auto *I = BB.getTerminatingDeoptimizeCall())
5125ee8613SDaniel Sanders     return I;
5225ee8613SDaniel Sanders   return BB.getTerminator();
5325ee8613SDaniel Sanders }
5425ee8613SDaniel Sanders 
5525ee8613SDaniel Sanders bool applyDebugifyMetadata(Module &M,
5625ee8613SDaniel Sanders                            iterator_range<Module::iterator> Functions,
5725ee8613SDaniel Sanders                            StringRef Banner) {
5825ee8613SDaniel Sanders   // Skip modules with debug info.
5925ee8613SDaniel Sanders   if (M.getNamedMetadata("llvm.dbg.cu")) {
6025ee8613SDaniel Sanders     dbg() << Banner << "Skipping module with debug info\n";
6125ee8613SDaniel Sanders     return false;
6225ee8613SDaniel Sanders   }
6325ee8613SDaniel Sanders 
6425ee8613SDaniel Sanders   DIBuilder DIB(M);
6525ee8613SDaniel Sanders   LLVMContext &Ctx = M.getContext();
6625ee8613SDaniel Sanders 
6725ee8613SDaniel Sanders   // Get a DIType which corresponds to Ty.
6825ee8613SDaniel Sanders   DenseMap<uint64_t, DIType *> TypeCache;
6925ee8613SDaniel Sanders   auto getCachedDIType = [&](Type *Ty) -> DIType * {
7025ee8613SDaniel Sanders     uint64_t Size = getAllocSizeInBits(M, Ty);
7125ee8613SDaniel Sanders     DIType *&DTy = TypeCache[Size];
7225ee8613SDaniel Sanders     if (!DTy) {
7325ee8613SDaniel Sanders       std::string Name = "ty" + utostr(Size);
7425ee8613SDaniel Sanders       DTy = DIB.createBasicType(Name, Size, dwarf::DW_ATE_unsigned);
7525ee8613SDaniel Sanders     }
7625ee8613SDaniel Sanders     return DTy;
7725ee8613SDaniel Sanders   };
7825ee8613SDaniel Sanders 
7925ee8613SDaniel Sanders   unsigned NextLine = 1;
8025ee8613SDaniel Sanders   unsigned NextVar = 1;
8125ee8613SDaniel Sanders   auto File = DIB.createFile(M.getName(), "/");
8225ee8613SDaniel Sanders   auto CU = DIB.createCompileUnit(dwarf::DW_LANG_C, File, "debugify",
8325ee8613SDaniel Sanders                                   /*isOptimized=*/true, "", 0);
8425ee8613SDaniel Sanders 
8525ee8613SDaniel Sanders   // Visit each instruction.
8625ee8613SDaniel Sanders   for (Function &F : Functions) {
8725ee8613SDaniel Sanders     if (isFunctionSkipped(F))
8825ee8613SDaniel Sanders       continue;
8925ee8613SDaniel Sanders 
9025ee8613SDaniel Sanders     auto SPType = DIB.createSubroutineType(DIB.getOrCreateTypeArray(None));
9125ee8613SDaniel Sanders     DISubprogram::DISPFlags SPFlags =
9225ee8613SDaniel Sanders         DISubprogram::SPFlagDefinition | DISubprogram::SPFlagOptimized;
9325ee8613SDaniel Sanders     if (F.hasPrivateLinkage() || F.hasInternalLinkage())
9425ee8613SDaniel Sanders       SPFlags |= DISubprogram::SPFlagLocalToUnit;
9525ee8613SDaniel Sanders     auto SP = DIB.createFunction(CU, F.getName(), F.getName(), File, NextLine,
9625ee8613SDaniel Sanders                                  SPType, NextLine, DINode::FlagZero, SPFlags);
9725ee8613SDaniel Sanders     F.setSubprogram(SP);
9825ee8613SDaniel Sanders     for (BasicBlock &BB : F) {
9925ee8613SDaniel Sanders       // Attach debug locations.
10025ee8613SDaniel Sanders       for (Instruction &I : BB)
10125ee8613SDaniel Sanders         I.setDebugLoc(DILocation::get(Ctx, NextLine++, 1, SP));
10225ee8613SDaniel Sanders 
10325ee8613SDaniel Sanders       // Inserting debug values into EH pads can break IR invariants.
10425ee8613SDaniel Sanders       if (BB.isEHPad())
10525ee8613SDaniel Sanders         continue;
10625ee8613SDaniel Sanders 
10725ee8613SDaniel Sanders       // Find the terminating instruction, after which no debug values are
10825ee8613SDaniel Sanders       // attached.
10925ee8613SDaniel Sanders       Instruction *LastInst = findTerminatingInstruction(BB);
11025ee8613SDaniel Sanders       assert(LastInst && "Expected basic block with a terminator");
11125ee8613SDaniel Sanders 
11225ee8613SDaniel Sanders       // Maintain an insertion point which can't be invalidated when updates
11325ee8613SDaniel Sanders       // are made.
11425ee8613SDaniel Sanders       BasicBlock::iterator InsertPt = BB.getFirstInsertionPt();
11525ee8613SDaniel Sanders       assert(InsertPt != BB.end() && "Expected to find an insertion point");
11625ee8613SDaniel Sanders       Instruction *InsertBefore = &*InsertPt;
11725ee8613SDaniel Sanders 
11825ee8613SDaniel Sanders       // Attach debug values.
11925ee8613SDaniel Sanders       for (Instruction *I = &*BB.begin(); I != LastInst; I = I->getNextNode()) {
12025ee8613SDaniel Sanders         // Skip void-valued instructions.
12125ee8613SDaniel Sanders         if (I->getType()->isVoidTy())
12225ee8613SDaniel Sanders           continue;
12325ee8613SDaniel Sanders 
12425ee8613SDaniel Sanders         // Phis and EH pads must be grouped at the beginning of the block.
12525ee8613SDaniel Sanders         // Only advance the insertion point when we finish visiting these.
12625ee8613SDaniel Sanders         if (!isa<PHINode>(I) && !I->isEHPad())
12725ee8613SDaniel Sanders           InsertBefore = I->getNextNode();
12825ee8613SDaniel Sanders 
12925ee8613SDaniel Sanders         std::string Name = utostr(NextVar++);
13025ee8613SDaniel Sanders         const DILocation *Loc = I->getDebugLoc().get();
13125ee8613SDaniel Sanders         auto LocalVar = DIB.createAutoVariable(SP, Name, File, Loc->getLine(),
13225ee8613SDaniel Sanders                                                getCachedDIType(I->getType()),
13325ee8613SDaniel Sanders                                                /*AlwaysPreserve=*/true);
13425ee8613SDaniel Sanders         DIB.insertDbgValueIntrinsic(I, LocalVar, DIB.createExpression(), Loc,
13525ee8613SDaniel Sanders                                     InsertBefore);
13625ee8613SDaniel Sanders       }
13725ee8613SDaniel Sanders     }
13825ee8613SDaniel Sanders     DIB.finalizeSubprogram(SP);
13925ee8613SDaniel Sanders   }
14025ee8613SDaniel Sanders   DIB.finalize();
14125ee8613SDaniel Sanders 
14225ee8613SDaniel Sanders   // Track the number of distinct lines and variables.
14325ee8613SDaniel Sanders   NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.debugify");
14425ee8613SDaniel Sanders   auto *IntTy = Type::getInt32Ty(Ctx);
14525ee8613SDaniel Sanders   auto addDebugifyOperand = [&](unsigned N) {
14625ee8613SDaniel Sanders     NMD->addOperand(MDNode::get(
14725ee8613SDaniel Sanders         Ctx, ValueAsMetadata::getConstant(ConstantInt::get(IntTy, N))));
14825ee8613SDaniel Sanders   };
14925ee8613SDaniel Sanders   addDebugifyOperand(NextLine - 1); // Original number of lines.
15025ee8613SDaniel Sanders   addDebugifyOperand(NextVar - 1);  // Original number of variables.
15125ee8613SDaniel Sanders   assert(NMD->getNumOperands() == 2 &&
15225ee8613SDaniel Sanders          "llvm.debugify should have exactly 2 operands!");
15325ee8613SDaniel Sanders 
15425ee8613SDaniel Sanders   // Claim that this synthetic debug info is valid.
15525ee8613SDaniel Sanders   StringRef DIVersionKey = "Debug Info Version";
15625ee8613SDaniel Sanders   if (!M.getModuleFlag(DIVersionKey))
15725ee8613SDaniel Sanders     M.addModuleFlag(Module::Warning, DIVersionKey, DEBUG_METADATA_VERSION);
15825ee8613SDaniel Sanders 
15925ee8613SDaniel Sanders   return true;
16025ee8613SDaniel Sanders }
16125ee8613SDaniel Sanders 
16225ee8613SDaniel Sanders /// Return true if a mis-sized diagnostic is issued for \p DVI.
16325ee8613SDaniel Sanders bool diagnoseMisSizedDbgValue(Module &M, DbgValueInst *DVI) {
16425ee8613SDaniel Sanders   // The size of a dbg.value's value operand should match the size of the
16525ee8613SDaniel Sanders   // variable it corresponds to.
16625ee8613SDaniel Sanders   //
16725ee8613SDaniel Sanders   // TODO: This, along with a check for non-null value operands, should be
16825ee8613SDaniel Sanders   // promoted to verifier failures.
16925ee8613SDaniel Sanders   Value *V = DVI->getValue();
17025ee8613SDaniel Sanders   if (!V)
17125ee8613SDaniel Sanders     return false;
17225ee8613SDaniel Sanders 
17325ee8613SDaniel Sanders   // For now, don't try to interpret anything more complicated than an empty
17425ee8613SDaniel Sanders   // DIExpression. Eventually we should try to handle OP_deref and fragments.
17525ee8613SDaniel Sanders   if (DVI->getExpression()->getNumElements())
17625ee8613SDaniel Sanders     return false;
17725ee8613SDaniel Sanders 
17825ee8613SDaniel Sanders   Type *Ty = V->getType();
17925ee8613SDaniel Sanders   uint64_t ValueOperandSize = getAllocSizeInBits(M, Ty);
18025ee8613SDaniel Sanders   Optional<uint64_t> DbgVarSize = DVI->getFragmentSizeInBits();
18125ee8613SDaniel Sanders   if (!ValueOperandSize || !DbgVarSize)
18225ee8613SDaniel Sanders     return false;
18325ee8613SDaniel Sanders 
18425ee8613SDaniel Sanders   bool HasBadSize = false;
18525ee8613SDaniel Sanders   if (Ty->isIntegerTy()) {
18625ee8613SDaniel Sanders     auto Signedness = DVI->getVariable()->getSignedness();
18725ee8613SDaniel Sanders     if (Signedness && *Signedness == DIBasicType::Signedness::Signed)
18825ee8613SDaniel Sanders       HasBadSize = ValueOperandSize < *DbgVarSize;
18925ee8613SDaniel Sanders   } else {
19025ee8613SDaniel Sanders     HasBadSize = ValueOperandSize != *DbgVarSize;
19125ee8613SDaniel Sanders   }
19225ee8613SDaniel Sanders 
19325ee8613SDaniel Sanders   if (HasBadSize) {
19425ee8613SDaniel Sanders     dbg() << "ERROR: dbg.value operand has size " << ValueOperandSize
19525ee8613SDaniel Sanders           << ", but its variable has size " << *DbgVarSize << ": ";
19625ee8613SDaniel Sanders     DVI->print(dbg());
19725ee8613SDaniel Sanders     dbg() << "\n";
19825ee8613SDaniel Sanders   }
19925ee8613SDaniel Sanders   return HasBadSize;
20025ee8613SDaniel Sanders }
20125ee8613SDaniel Sanders 
20225ee8613SDaniel Sanders bool checkDebugifyMetadata(Module &M,
20325ee8613SDaniel Sanders                            iterator_range<Module::iterator> Functions,
20425ee8613SDaniel Sanders                            StringRef NameOfWrappedPass, StringRef Banner,
20525ee8613SDaniel Sanders                            bool Strip, DebugifyStatsMap *StatsMap) {
20625ee8613SDaniel Sanders   // Skip modules without debugify metadata.
20725ee8613SDaniel Sanders   NamedMDNode *NMD = M.getNamedMetadata("llvm.debugify");
20825ee8613SDaniel Sanders   if (!NMD) {
20925ee8613SDaniel Sanders     dbg() << Banner << "Skipping module without debugify metadata\n";
21025ee8613SDaniel Sanders     return false;
21125ee8613SDaniel Sanders   }
21225ee8613SDaniel Sanders 
21325ee8613SDaniel Sanders   auto getDebugifyOperand = [&](unsigned Idx) -> unsigned {
21425ee8613SDaniel Sanders     return mdconst::extract<ConstantInt>(NMD->getOperand(Idx)->getOperand(0))
21525ee8613SDaniel Sanders         ->getZExtValue();
21625ee8613SDaniel Sanders   };
21725ee8613SDaniel Sanders   assert(NMD->getNumOperands() == 2 &&
21825ee8613SDaniel Sanders          "llvm.debugify should have exactly 2 operands!");
21925ee8613SDaniel Sanders   unsigned OriginalNumLines = getDebugifyOperand(0);
22025ee8613SDaniel Sanders   unsigned OriginalNumVars = getDebugifyOperand(1);
22125ee8613SDaniel Sanders   bool HasErrors = false;
22225ee8613SDaniel Sanders 
22325ee8613SDaniel Sanders   // Track debug info loss statistics if able.
22425ee8613SDaniel Sanders   DebugifyStatistics *Stats = nullptr;
22525ee8613SDaniel Sanders   if (StatsMap && !NameOfWrappedPass.empty())
22625ee8613SDaniel Sanders     Stats = &StatsMap->operator[](NameOfWrappedPass);
22725ee8613SDaniel Sanders 
22825ee8613SDaniel Sanders   BitVector MissingLines{OriginalNumLines, true};
22925ee8613SDaniel Sanders   BitVector MissingVars{OriginalNumVars, true};
23025ee8613SDaniel Sanders   for (Function &F : Functions) {
23125ee8613SDaniel Sanders     if (isFunctionSkipped(F))
23225ee8613SDaniel Sanders       continue;
23325ee8613SDaniel Sanders 
23425ee8613SDaniel Sanders     // Find missing lines.
23525ee8613SDaniel Sanders     for (Instruction &I : instructions(F)) {
236*f64e457cSPierre-vh       if (isa<DbgValueInst>(&I) || isa<PHINode>(&I))
23725ee8613SDaniel Sanders         continue;
23825ee8613SDaniel Sanders 
23925ee8613SDaniel Sanders       auto DL = I.getDebugLoc();
24025ee8613SDaniel Sanders       if (DL && DL.getLine() != 0) {
24125ee8613SDaniel Sanders         MissingLines.reset(DL.getLine() - 1);
24225ee8613SDaniel Sanders         continue;
24325ee8613SDaniel Sanders       }
24425ee8613SDaniel Sanders 
24525ee8613SDaniel Sanders       if (!DL) {
24625ee8613SDaniel Sanders         dbg() << "ERROR: Instruction with empty DebugLoc in function ";
24725ee8613SDaniel Sanders         dbg() << F.getName() << " --";
24825ee8613SDaniel Sanders         I.print(dbg());
24925ee8613SDaniel Sanders         dbg() << "\n";
25025ee8613SDaniel Sanders         HasErrors = true;
25125ee8613SDaniel Sanders       }
25225ee8613SDaniel Sanders     }
25325ee8613SDaniel Sanders 
25425ee8613SDaniel Sanders     // Find missing variables and mis-sized debug values.
25525ee8613SDaniel Sanders     for (Instruction &I : instructions(F)) {
25625ee8613SDaniel Sanders       auto *DVI = dyn_cast<DbgValueInst>(&I);
25725ee8613SDaniel Sanders       if (!DVI)
25825ee8613SDaniel Sanders         continue;
25925ee8613SDaniel Sanders 
26025ee8613SDaniel Sanders       unsigned Var = ~0U;
26125ee8613SDaniel Sanders       (void)to_integer(DVI->getVariable()->getName(), Var, 10);
26225ee8613SDaniel Sanders       assert(Var <= OriginalNumVars && "Unexpected name for DILocalVariable");
26325ee8613SDaniel Sanders       bool HasBadSize = diagnoseMisSizedDbgValue(M, DVI);
26425ee8613SDaniel Sanders       if (!HasBadSize)
26525ee8613SDaniel Sanders         MissingVars.reset(Var - 1);
26625ee8613SDaniel Sanders       HasErrors |= HasBadSize;
26725ee8613SDaniel Sanders     }
26825ee8613SDaniel Sanders   }
26925ee8613SDaniel Sanders 
27025ee8613SDaniel Sanders   // Print the results.
27125ee8613SDaniel Sanders   for (unsigned Idx : MissingLines.set_bits())
27225ee8613SDaniel Sanders     dbg() << "WARNING: Missing line " << Idx + 1 << "\n";
27325ee8613SDaniel Sanders 
27425ee8613SDaniel Sanders   for (unsigned Idx : MissingVars.set_bits())
27525ee8613SDaniel Sanders     dbg() << "WARNING: Missing variable " << Idx + 1 << "\n";
27625ee8613SDaniel Sanders 
27725ee8613SDaniel Sanders   // Update DI loss statistics.
27825ee8613SDaniel Sanders   if (Stats) {
27925ee8613SDaniel Sanders     Stats->NumDbgLocsExpected += OriginalNumLines;
28025ee8613SDaniel Sanders     Stats->NumDbgLocsMissing += MissingLines.count();
28125ee8613SDaniel Sanders     Stats->NumDbgValuesExpected += OriginalNumVars;
28225ee8613SDaniel Sanders     Stats->NumDbgValuesMissing += MissingVars.count();
28325ee8613SDaniel Sanders   }
28425ee8613SDaniel Sanders 
28525ee8613SDaniel Sanders   dbg() << Banner;
28625ee8613SDaniel Sanders   if (!NameOfWrappedPass.empty())
28725ee8613SDaniel Sanders     dbg() << " [" << NameOfWrappedPass << "]";
28825ee8613SDaniel Sanders   dbg() << ": " << (HasErrors ? "FAIL" : "PASS") << '\n';
28925ee8613SDaniel Sanders 
29025ee8613SDaniel Sanders   // Strip the Debugify Metadata if required.
29125ee8613SDaniel Sanders   if (Strip) {
29225ee8613SDaniel Sanders     StripDebugInfo(M);
29325ee8613SDaniel Sanders     M.eraseNamedMetadata(NMD);
29425ee8613SDaniel Sanders     return true;
29525ee8613SDaniel Sanders   }
29625ee8613SDaniel Sanders 
29725ee8613SDaniel Sanders   return false;
29825ee8613SDaniel Sanders }
29925ee8613SDaniel Sanders 
30025ee8613SDaniel Sanders /// ModulePass for attaching synthetic debug info to everything, used with the
30125ee8613SDaniel Sanders /// legacy module pass manager.
30225ee8613SDaniel Sanders struct DebugifyModulePass : public ModulePass {
30325ee8613SDaniel Sanders   bool runOnModule(Module &M) override {
30425ee8613SDaniel Sanders     return applyDebugifyMetadata(M, M.functions(), "ModuleDebugify: ");
30525ee8613SDaniel Sanders   }
30625ee8613SDaniel Sanders 
30725ee8613SDaniel Sanders   DebugifyModulePass() : ModulePass(ID) {}
30825ee8613SDaniel Sanders 
30925ee8613SDaniel Sanders   void getAnalysisUsage(AnalysisUsage &AU) const override {
31025ee8613SDaniel Sanders     AU.setPreservesAll();
31125ee8613SDaniel Sanders   }
31225ee8613SDaniel Sanders 
31325ee8613SDaniel Sanders   static char ID; // Pass identification.
31425ee8613SDaniel Sanders };
31525ee8613SDaniel Sanders 
31625ee8613SDaniel Sanders /// FunctionPass for attaching synthetic debug info to instructions within a
31725ee8613SDaniel Sanders /// single function, used with the legacy module pass manager.
31825ee8613SDaniel Sanders struct DebugifyFunctionPass : public FunctionPass {
31925ee8613SDaniel Sanders   bool runOnFunction(Function &F) override {
32025ee8613SDaniel Sanders     Module &M = *F.getParent();
32125ee8613SDaniel Sanders     auto FuncIt = F.getIterator();
32225ee8613SDaniel Sanders     return applyDebugifyMetadata(M, make_range(FuncIt, std::next(FuncIt)),
32325ee8613SDaniel Sanders                                  "FunctionDebugify: ");
32425ee8613SDaniel Sanders   }
32525ee8613SDaniel Sanders 
32625ee8613SDaniel Sanders   DebugifyFunctionPass() : FunctionPass(ID) {}
32725ee8613SDaniel Sanders 
32825ee8613SDaniel Sanders   void getAnalysisUsage(AnalysisUsage &AU) const override {
32925ee8613SDaniel Sanders     AU.setPreservesAll();
33025ee8613SDaniel Sanders   }
33125ee8613SDaniel Sanders 
33225ee8613SDaniel Sanders   static char ID; // Pass identification.
33325ee8613SDaniel Sanders };
33425ee8613SDaniel Sanders 
33525ee8613SDaniel Sanders /// ModulePass for checking debug info inserted by -debugify, used with the
33625ee8613SDaniel Sanders /// legacy module pass manager.
33725ee8613SDaniel Sanders struct CheckDebugifyModulePass : public ModulePass {
33825ee8613SDaniel Sanders   bool runOnModule(Module &M) override {
33925ee8613SDaniel Sanders     return checkDebugifyMetadata(M, M.functions(), NameOfWrappedPass,
34025ee8613SDaniel Sanders                                  "CheckModuleDebugify", Strip, StatsMap);
34125ee8613SDaniel Sanders   }
34225ee8613SDaniel Sanders 
34325ee8613SDaniel Sanders   CheckDebugifyModulePass(bool Strip = false, StringRef NameOfWrappedPass = "",
34425ee8613SDaniel Sanders                           DebugifyStatsMap *StatsMap = nullptr)
34525ee8613SDaniel Sanders       : ModulePass(ID), Strip(Strip), NameOfWrappedPass(NameOfWrappedPass),
34625ee8613SDaniel Sanders         StatsMap(StatsMap) {}
34725ee8613SDaniel Sanders 
34825ee8613SDaniel Sanders   void getAnalysisUsage(AnalysisUsage &AU) const override {
34925ee8613SDaniel Sanders     AU.setPreservesAll();
35025ee8613SDaniel Sanders   }
35125ee8613SDaniel Sanders 
35225ee8613SDaniel Sanders   static char ID; // Pass identification.
35325ee8613SDaniel Sanders 
35425ee8613SDaniel Sanders private:
35525ee8613SDaniel Sanders   bool Strip;
35625ee8613SDaniel Sanders   StringRef NameOfWrappedPass;
35725ee8613SDaniel Sanders   DebugifyStatsMap *StatsMap;
35825ee8613SDaniel Sanders };
35925ee8613SDaniel Sanders 
36025ee8613SDaniel Sanders /// FunctionPass for checking debug info inserted by -debugify-function, used
36125ee8613SDaniel Sanders /// with the legacy module pass manager.
36225ee8613SDaniel Sanders struct CheckDebugifyFunctionPass : public FunctionPass {
36325ee8613SDaniel Sanders   bool runOnFunction(Function &F) override {
36425ee8613SDaniel Sanders     Module &M = *F.getParent();
36525ee8613SDaniel Sanders     auto FuncIt = F.getIterator();
36625ee8613SDaniel Sanders     return checkDebugifyMetadata(M, make_range(FuncIt, std::next(FuncIt)),
36725ee8613SDaniel Sanders                                  NameOfWrappedPass, "CheckFunctionDebugify",
36825ee8613SDaniel Sanders                                  Strip, StatsMap);
36925ee8613SDaniel Sanders   }
37025ee8613SDaniel Sanders 
37125ee8613SDaniel Sanders   CheckDebugifyFunctionPass(bool Strip = false,
37225ee8613SDaniel Sanders                             StringRef NameOfWrappedPass = "",
37325ee8613SDaniel Sanders                             DebugifyStatsMap *StatsMap = nullptr)
37425ee8613SDaniel Sanders       : FunctionPass(ID), Strip(Strip), NameOfWrappedPass(NameOfWrappedPass),
37525ee8613SDaniel Sanders         StatsMap(StatsMap) {}
37625ee8613SDaniel Sanders 
37725ee8613SDaniel Sanders   void getAnalysisUsage(AnalysisUsage &AU) const override {
37825ee8613SDaniel Sanders     AU.setPreservesAll();
37925ee8613SDaniel Sanders   }
38025ee8613SDaniel Sanders 
38125ee8613SDaniel Sanders   static char ID; // Pass identification.
38225ee8613SDaniel Sanders 
38325ee8613SDaniel Sanders private:
38425ee8613SDaniel Sanders   bool Strip;
38525ee8613SDaniel Sanders   StringRef NameOfWrappedPass;
38625ee8613SDaniel Sanders   DebugifyStatsMap *StatsMap;
38725ee8613SDaniel Sanders };
38825ee8613SDaniel Sanders 
38925ee8613SDaniel Sanders } // end anonymous namespace
39025ee8613SDaniel Sanders 
39125ee8613SDaniel Sanders ModulePass *createDebugifyModulePass() { return new DebugifyModulePass(); }
39225ee8613SDaniel Sanders 
39325ee8613SDaniel Sanders FunctionPass *createDebugifyFunctionPass() {
39425ee8613SDaniel Sanders   return new DebugifyFunctionPass();
39525ee8613SDaniel Sanders }
39625ee8613SDaniel Sanders 
39725ee8613SDaniel Sanders PreservedAnalyses NewPMDebugifyPass::run(Module &M, ModuleAnalysisManager &) {
39825ee8613SDaniel Sanders   applyDebugifyMetadata(M, M.functions(), "ModuleDebugify: ");
39925ee8613SDaniel Sanders   return PreservedAnalyses::all();
40025ee8613SDaniel Sanders }
40125ee8613SDaniel Sanders 
40225ee8613SDaniel Sanders ModulePass *createCheckDebugifyModulePass(bool Strip,
40325ee8613SDaniel Sanders                                           StringRef NameOfWrappedPass,
40425ee8613SDaniel Sanders                                           DebugifyStatsMap *StatsMap) {
40525ee8613SDaniel Sanders   return new CheckDebugifyModulePass(Strip, NameOfWrappedPass, StatsMap);
40625ee8613SDaniel Sanders }
40725ee8613SDaniel Sanders 
40825ee8613SDaniel Sanders FunctionPass *createCheckDebugifyFunctionPass(bool Strip,
40925ee8613SDaniel Sanders                                               StringRef NameOfWrappedPass,
41025ee8613SDaniel Sanders                                               DebugifyStatsMap *StatsMap) {
41125ee8613SDaniel Sanders   return new CheckDebugifyFunctionPass(Strip, NameOfWrappedPass, StatsMap);
41225ee8613SDaniel Sanders }
41325ee8613SDaniel Sanders 
41425ee8613SDaniel Sanders PreservedAnalyses NewPMCheckDebugifyPass::run(Module &M,
41525ee8613SDaniel Sanders                                               ModuleAnalysisManager &) {
41625ee8613SDaniel Sanders   checkDebugifyMetadata(M, M.functions(), "", "CheckModuleDebugify", false,
41725ee8613SDaniel Sanders                         nullptr);
41825ee8613SDaniel Sanders   return PreservedAnalyses::all();
41925ee8613SDaniel Sanders }
42025ee8613SDaniel Sanders 
42125ee8613SDaniel Sanders char DebugifyModulePass::ID = 0;
42225ee8613SDaniel Sanders static RegisterPass<DebugifyModulePass> DM("debugify",
42325ee8613SDaniel Sanders                                            "Attach debug info to everything");
42425ee8613SDaniel Sanders 
42525ee8613SDaniel Sanders char CheckDebugifyModulePass::ID = 0;
42625ee8613SDaniel Sanders static RegisterPass<CheckDebugifyModulePass>
42725ee8613SDaniel Sanders     CDM("check-debugify", "Check debug info from -debugify");
42825ee8613SDaniel Sanders 
42925ee8613SDaniel Sanders char DebugifyFunctionPass::ID = 0;
43025ee8613SDaniel Sanders static RegisterPass<DebugifyFunctionPass> DF("debugify-function",
43125ee8613SDaniel Sanders                                              "Attach debug info to a function");
43225ee8613SDaniel Sanders 
43325ee8613SDaniel Sanders char CheckDebugifyFunctionPass::ID = 0;
43425ee8613SDaniel Sanders static RegisterPass<CheckDebugifyFunctionPass>
43525ee8613SDaniel Sanders     CDF("check-debugify-function", "Check debug info from -debugify-function");
436