95 lines · cpp
1//===- ReduceDIMetadata.cpp - Specialized Delta pass for DebugInfo --------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8//9// This file implements two functions used by the Generic Delta Debugging10// Algorithm, which are used to reduce DebugInfo metadata nodes.11//12//===----------------------------------------------------------------------===//13 14#include "ReduceDIMetadata.h"15#include "llvm/ADT/SetVector.h"16#include "llvm/ADT/SmallVector.h"17#include "llvm/IR/DebugInfoMetadata.h"18#include "llvm/IR/InstIterator.h"19#include <tuple>20#include <vector>21 22using namespace llvm;23 24using MDNodeList = SmallVector<MDNode *>;25 26void identifyUninterestingMDNodes(Oracle &O, MDNodeList &MDs) {27 SetVector<std::tuple<MDNode *, size_t, MDNode *>> Tuples;28 std::vector<MDNode *> ToLook;29 SetVector<MDNode *> Visited;30 31 // Start by looking at the attachments we collected32 for (const auto &NMD : MDs)33 if (NMD)34 ToLook.push_back(NMD);35 36 while (!ToLook.empty()) {37 MDNode *MD = ToLook.back();38 ToLook.pop_back();39 40 if (!Visited.insert(MD))41 continue;42 43 // Determine if the current MDNode is DebugInfo44 if (DINode *DIM = dyn_cast_or_null<DINode>(MD)) {45 // Scan operands and record attached tuples46 for (size_t I = 0; I < DIM->getNumOperands(); ++I)47 if (MDTuple *MDT = dyn_cast_or_null<MDTuple>(DIM->getOperand(I)))48 if (!Visited.count(MDT) && MDT->getNumOperands())49 Tuples.insert({DIM, I, MDT});50 }51 52 // Add all of the operands of the current node to the loop's todo list.53 for (Metadata *Op : MD->operands())54 if (MDNode *OMD = dyn_cast_or_null<MDNode>(Op))55 ToLook.push_back(OMD);56 }57 58 for (auto &T : Tuples) {59 auto [DbgNode, OpIdx, Tup] = T;60 // Remove the operands of the tuple that are not in the desired chunks.61 SmallVector<Metadata *, 16> TN;62 for (size_t I = 0; I < Tup->getNumOperands(); ++I) {63 // Ignore any operands that are not DebugInfo metadata nodes.64 if (Metadata *Op = Tup->getOperand(I).get()) {65 if (isa<DINode>(Op) || isa<DIGlobalVariableExpression>(Op))66 // Don't add uninteresting operands to the tuple.67 if (!O.shouldKeep())68 continue;69 }70 TN.push_back(Tup->getOperand(I));71 }72 if (TN.size() != Tup->getNumOperands())73 DbgNode->replaceOperandWith(OpIdx, DbgNode->get(DbgNode->getContext(), TN));74 }75}76 77void llvm::reduceDIMetadataDeltaPass(Oracle &O, ReducerWorkItem &WorkItem) {78 Module &Program = WorkItem.getModule();79 80 MDNodeList MDs;81 // Collect all !dbg metadata attachments.82 for (const auto &DC : Program.debug_compile_units())83 if (DC)84 MDs.push_back(DC);85 for (GlobalVariable &GV : Program.globals())86 GV.getMetadata(llvm::LLVMContext::MD_dbg, MDs);87 for (Function &F : Program.functions()) {88 F.getMetadata(llvm::LLVMContext::MD_dbg, MDs);89 for (Instruction &I : instructions(F))90 if (auto *DI = I.getMetadata(llvm::LLVMContext::MD_dbg))91 MDs.push_back(DI);92 }93 identifyUninterestingMDNodes(O, MDs);94}95