1//===- Delta.h - Delta Debugging Algorithm Implementation -------*- C++ -*-===//
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-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains the implementation for the Delta Debugging Algorithm:
10// it splits a given set of Targets (i.e. Functions, Instructions, BBs, etc.)
11// into chunks and tries to reduce the number chunks that are interesting.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_TOOLS_LLVM_REDUCE_DELTAS_DELTA_H
16#define LLVM_TOOLS_LLVM_REDUCE_DELTAS_DELTA_H
17
18#include "ReducerWorkItem.h"
19#include "llvm/ADT/ArrayRef.h"
20#include "llvm/Support/raw_ostream.h"
21#include <functional>
22#include <utility>
23
24namespace llvm {
25
26class TestRunner;
27struct DeltaPass;
28
29struct Chunk {
30 int Begin;
31 int End;
32
33 /// Helper function to verify if a given Target-index is inside the Chunk
34 bool contains(int Index) const { return Index >= Begin && Index <= End; }
35
36 void print() const {
37 errs() << '[' << Begin;
38 if (End - Begin != 0)
39 errs() << ',' << End;
40 errs() << ']';
41 }
42
43 /// Operator when populating CurrentChunks in Generic Delta Pass
44 friend bool operator!=(const Chunk &C1, const Chunk &C2) {
45 return C1.Begin != C2.Begin || C1.End != C2.End;
46 }
47
48 friend bool operator==(const Chunk &C1, const Chunk &C2) {
49 return C1.Begin == C2.Begin && C1.End == C2.End;
50 }
51
52 /// Operator used for sets
53 friend bool operator<(const Chunk &C1, const Chunk &C2) {
54 return std::tie(args: C1.Begin, args: C1.End) < std::tie(args: C2.Begin, args: C2.End);
55 }
56};
57
58template <> struct DenseMapInfo<Chunk> {
59 static unsigned getHashValue(const Chunk Val) {
60 std::pair<int, int> PairVal = std::make_pair(x: Val.Begin, y: Val.End);
61 return DenseMapInfo<std::pair<int, int>>::getHashValue(PairVal);
62 }
63
64 static bool isEqual(const Chunk LHS, const Chunk RHS) {
65 return LHS == RHS;
66 }
67};
68
69/// Provides opaque interface for querying into ChunksToKeep without having to
70/// actually understand what is going on.
71class Oracle {
72 /// Out of all the features that we promised to be,
73 /// how many have we already processed?
74 int Index = 0;
75
76 /// The actual workhorse, contains the knowledge whether or not
77 /// some particular feature should be preserved this time.
78 ArrayRef<Chunk> ChunksToKeep;
79
80public:
81 explicit Oracle(ArrayRef<Chunk> ChunksToKeep) : ChunksToKeep(ChunksToKeep) {}
82
83 /// Should be called for each feature on which we are operating.
84 /// Name is self-explanatory - if returns true, then it should be preserved.
85 bool shouldKeep() {
86 if (ChunksToKeep.empty()) {
87 ++Index;
88 return false; // All further features are to be discarded.
89 }
90
91 // Does the current (front) chunk contain such a feature?
92 bool ShouldKeep = ChunksToKeep.front().contains(Index);
93
94 // Is this the last feature in the chunk?
95 if (ChunksToKeep.front().End == Index)
96 ChunksToKeep = ChunksToKeep.drop_front(); // Onto next chunk.
97
98 ++Index;
99
100 return ShouldKeep;
101 }
102
103 int count() { return Index; }
104};
105
106using ReductionFunc = function_ref<void(Oracle &, ReducerWorkItem &)>;
107
108/// This function implements the Delta Debugging algorithm, it receives a
109/// number of Targets (e.g. Functions, Instructions, Basic Blocks, etc.) and
110/// splits them in half; these chunks of targets are then tested while ignoring
111/// one chunk, if a chunk is proven to be uninteresting (i.e. fails the test)
112/// it is removed from consideration. The algorithm will attempt to split the
113/// Chunks in half and start the process again until it can't split chunks
114/// anymore.
115///
116/// This function is intended to be called by each specialized delta pass (e.g.
117/// RemoveFunctions) and receives three key parameters:
118/// * Test: The main TestRunner instance which is used to run the provided
119/// interesting-ness test, as well as to store and access the reduced Program.
120/// * ExtractChunksFromModule: A function used to tailor the main program so it
121/// only contains Targets that are inside Chunks of the given iteration.
122/// Note: This function is implemented by each specialized Delta pass
123///
124/// Other implementations of the Delta Debugging algorithm can also be found in
125/// the CReduce, Delta, and Lithium projects.
126void runDeltaPass(TestRunner &Test, const DeltaPass &Pass);
127} // namespace llvm
128
129#endif
130