aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/llvm16/tools/llvm-reduce/deltas/ReduceDIMetadata.cpp
blob: bbd928a046dd4333570a2953a7d09b095c86cbbb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//===- ReduceDIMetadata.cpp - Specialized Delta pass for DebugInfo --------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file implements two functions used by the Generic Delta Debugging
// Algorithm, which are used to reduce DebugInfo metadata nodes.
//
//===----------------------------------------------------------------------===//

#include "ReduceDIMetadata.h"
#include "Delta.h"
#include "llvm/ADT/Sequence.h"
#include "llvm/ADT/SetVector.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/InstIterator.h"
#include <set>
#include <stack>
#include <tuple>
#include <vector>

using namespace llvm;

using MDNodeList = SmallVector<MDNode *>;

void identifyUninterestingMDNodes(Oracle &O, MDNodeList &MDs) {
  SetVector<std::tuple<MDNode *, size_t, MDNode *>> Tuples;
  std::vector<MDNode *> ToLook;
  SetVector<MDNode *> Visited;

  // Start by looking at the attachments we collected
  for (const auto &NMD : MDs)
    if (NMD)
      ToLook.push_back(NMD);

  while (!ToLook.empty()) {
    MDNode *MD = ToLook.back();
    ToLook.pop_back();

    if (Visited.count(MD))
      continue;

    // Determine if the current MDNode is DebugInfo
    if (DINode *DIM = dyn_cast_or_null<DINode>(MD)) {
      // Scan operands and record attached tuples
      for (size_t I = 0; I < DIM->getNumOperands(); ++I)
        if (MDTuple *MDT = dyn_cast_or_null<MDTuple>(DIM->getOperand(I)))
          if (!Visited.count(MDT) && MDT->getNumOperands())
            Tuples.insert({DIM, I, MDT});
    }

    // Add all of the operands of the current node to the loop's todo list.
    for (Metadata *Op : MD->operands())
      if (MDNode *OMD = dyn_cast_or_null<MDNode>(Op))
        ToLook.push_back(OMD);

    Visited.insert(MD);
  }

  for (auto &T : Tuples) {
    auto [DbgNode, OpIdx, Tup] = T;
    // Remove the operands of the tuple that are not in the desired chunks.
    SmallVector<Metadata *, 16> TN;
    for (size_t I = 0; I < Tup->getNumOperands(); ++I) {
      // Ignore any operands that are not DebugInfo metadata nodes.
      if (isa_and_nonnull<DINode>(Tup->getOperand(I)))
        // Don't add uninteresting operands to the tuple.
        if (!O.shouldKeep())
          continue;

      TN.push_back(Tup->getOperand(I));
    }
    if (TN.size() != Tup->getNumOperands())
      DbgNode->replaceOperandWith(OpIdx, DbgNode->get(DbgNode->getContext(), TN));
  }
}

static void extractDIMetadataFromModule(Oracle &O, ReducerWorkItem &WorkItem) {
  Module &Program = WorkItem.getModule();

  MDNodeList MDs;
  // Collect all !dbg metadata attachments.
  for (const auto &DC : Program.debug_compile_units())
    if (DC)
      MDs.push_back(DC);
  for (GlobalVariable &GV : Program.globals())
    GV.getMetadata(llvm::LLVMContext::MD_dbg, MDs);
  for (Function &F : Program.functions()) {
    F.getMetadata(llvm::LLVMContext::MD_dbg, MDs);
    for (Instruction &I : instructions(F))
      if (auto *DI = I.getMetadata(llvm::LLVMContext::MD_dbg))
        MDs.push_back(DI);
  }
  identifyUninterestingMDNodes(O, MDs);
}

void llvm::reduceDIMetadataDeltaPass(TestRunner &Test) {
  runDeltaPass(Test, extractDIMetadataFromModule, "Reducing DIMetadata");
}