1 //===- Delta.h - Delta Debugging Algorithm Implementation -------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_TOOLS_LLVM_REDUCE_DELTAS_DELTA_H
16 #define LLVM_TOOLS_LLVM_REDUCE_DELTAS_DELTA_H
18 #include "ReducerWorkItem.h"
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/Support/raw_ostream.h"
32 /// Helper function to verify if a given Target-index is inside the Chunk
33 bool contains(int Index
) const { return Index
>= Begin
&& Index
<= End
; }
36 errs() << '[' << Begin
;
42 /// Operator when populating CurrentChunks in Generic Delta Pass
43 friend bool operator!=(const Chunk
&C1
, const Chunk
&C2
) {
44 return C1
.Begin
!= C2
.Begin
|| C1
.End
!= C2
.End
;
47 friend bool operator==(const Chunk
&C1
, const Chunk
&C2
) {
48 return C1
.Begin
== C2
.Begin
&& C1
.End
== C2
.End
;
51 /// Operator used for sets
52 friend bool operator<(const Chunk
&C1
, const Chunk
&C2
) {
53 return std::tie(C1
.Begin
, C1
.End
) < std::tie(C2
.Begin
, C2
.End
);
58 struct DenseMapInfo
<Chunk
> {
59 static inline Chunk
getEmptyKey() {
60 return {DenseMapInfo
<int>::getEmptyKey(),
61 DenseMapInfo
<int>::getEmptyKey()};
64 static inline Chunk
getTombstoneKey() {
65 return {DenseMapInfo
<int>::getTombstoneKey(),
66 DenseMapInfo
<int>::getTombstoneKey()};
69 static unsigned getHashValue(const Chunk Val
) {
70 std::pair
<int, int> PairVal
= std::make_pair(Val
.Begin
, Val
.End
);
71 return DenseMapInfo
<std::pair
<int, int>>::getHashValue(PairVal
);
74 static bool isEqual(const Chunk LHS
, const Chunk RHS
) {
80 /// Provides opaque interface for querying into ChunksToKeep without having to
81 /// actually understand what is going on.
83 /// Out of all the features that we promised to be,
84 /// how many have we already processed?
87 /// The actual workhorse, contains the knowledge whether or not
88 /// some particular feature should be preserved this time.
89 ArrayRef
<Chunk
> ChunksToKeep
;
92 explicit Oracle(ArrayRef
<Chunk
> ChunksToKeep
) : ChunksToKeep(ChunksToKeep
) {}
94 /// Should be called for each feature on which we are operating.
95 /// Name is self-explanatory - if returns true, then it should be preserved.
97 if (ChunksToKeep
.empty()) {
99 return false; // All further features are to be discarded.
102 // Does the current (front) chunk contain such a feature?
103 bool ShouldKeep
= ChunksToKeep
.front().contains(Index
);
105 // Is this the last feature in the chunk?
106 if (ChunksToKeep
.front().End
== Index
)
107 ChunksToKeep
= ChunksToKeep
.drop_front(); // Onto next chunk.
114 int count() { return Index
; }
117 using ReductionFunc
= function_ref
<void(Oracle
&, ReducerWorkItem
&)>;
119 /// This function implements the Delta Debugging algorithm, it receives a
120 /// number of Targets (e.g. Functions, Instructions, Basic Blocks, etc.) and
121 /// splits them in half; these chunks of targets are then tested while ignoring
122 /// one chunk, if a chunk is proven to be uninteresting (i.e. fails the test)
123 /// it is removed from consideration. The algorithm will attempt to split the
124 /// Chunks in half and start the process again until it can't split chunks
127 /// This function is intended to be called by each specialized delta pass (e.g.
128 /// RemoveFunctions) and receives three key parameters:
129 /// * Test: The main TestRunner instance which is used to run the provided
130 /// interesting-ness test, as well as to store and access the reduced Program.
131 /// * ExtractChunksFromModule: A function used to tailor the main program so it
132 /// only contains Targets that are inside Chunks of the given iteration.
133 /// Note: This function is implemented by each specialized Delta pass
135 /// Other implementations of the Delta Debugging algorithm can also be found in
136 /// the CReduce, Delta, and Lithium projects.
137 void runDeltaPass(TestRunner
&Test
, ReductionFunc ExtractChunksFromModule
,