/// Remove any debug info for global variables/functions in the given module for /// which said global variable/function no longer exists (i.e. is null). /// /// Debugging information is encoded in llvm IR using metadata. This is designed /// such a way that debug info for symbols preserved even if symbols are /// optimized away by the optimizer. This special pass removes debug info for /// such symbols. bool StripDeadDebugInfo::runOnModule(Module &M) { if (skipModule(M)) return false; bool Changed = false; LLVMContext &C = M.getContext(); // Find all debug info in F. This is actually overkill in terms of what we // want to do, but we want to try and be as resilient as possible in the face // of potential debug info changes by using the formal interfaces given to us // as much as possible. DebugInfoFinder F; F.processModule(M); // For each compile unit, find the live set of global variables/functions and // replace the current list of potentially dead global variables/functions // with the live list. SmallVector<Metadata *, 64> LiveGlobalVariables; DenseSet<DIGlobalVariableExpression *> VisitedSet; std::set<DIGlobalVariableExpression *> LiveGVs; for (GlobalVariable &GV : M.globals()) { SmallVector<DIGlobalVariableExpression *, 1> GVEs; GV.getDebugInfo(GVEs); for (auto *GVE : GVEs) LiveGVs.insert(GVE); } std::set<DICompileUnit *> LiveCUs; // Any CU referenced from a subprogram is live. for (DISubprogram *SP : F.subprograms()) { if (SP->getUnit()) LiveCUs.insert(SP->getUnit()); } bool HasDeadCUs = false; for (DICompileUnit *DIC : F.compile_units()) { // Create our live global variable list. bool GlobalVariableChange = false; for (auto *DIG : DIC->getGlobalVariables()) { if (DIG->getExpression() && DIG->getExpression()->isConstant()) LiveGVs.insert(DIG); // Make sure we only visit each global variable only once. if (!VisitedSet.insert(DIG).second) continue; // If a global variable references DIG, the global variable is live. if (LiveGVs.count(DIG)) LiveGlobalVariables.push_back(DIG); else GlobalVariableChange = true; } if (!LiveGlobalVariables.empty()) LiveCUs.insert(DIC); else if (!LiveCUs.count(DIC)) HasDeadCUs = true; // If we found dead global variables, replace the current global // variable list with our new live global variable list. if (GlobalVariableChange) { DIC->replaceGlobalVariables(MDTuple::get(C, LiveGlobalVariables)); Changed = true; } // Reset lists for the next iteration. LiveGlobalVariables.clear(); } if (HasDeadCUs) { // Delete the old node and replace it with a new one NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu"); NMD->clearOperands(); if (!LiveCUs.empty()) { for (DICompileUnit *CU : LiveCUs) NMD->addOperand(CU); } Changed = true; } return Changed; }