1
//===--- DAGDeltaAlgorithm.cpp - A DAG Minimization Algorithm --*- C++ -*--===//
3
// The LLVM Compiler Infrastructure
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
7
//===----------------------------------------------------------------------===//
9
// The algorithm we use attempts to exploit the dependency information by
10
// minimizing top-down. We start by constructing an initial root set R, and
13
// 1. Minimize the set R using the test predicate:
14
// P'(S) = P(S union pred*(S))
16
// 2. Extend R to R' = R union pred(R).
18
// until a fixed point is reached.
20
// The idea is that we want to quickly prune entire portions of the graph, so we
21
// try to find high-level nodes that can be eliminated with all of their
24
// FIXME: The current algorithm doesn't actually provide a strong guarantee
25
// about the minimality of the result. The problem is that after adding nodes to
26
// the required set, we no longer consider them for elimination. For strictly
27
// well formed predicates, this doesn't happen, but it commonly occurs in
28
// practice when there are unmodelled dependencies. I believe we can resolve
29
// this by allowing the required set to be minimized as well, but need more test
32
//===----------------------------------------------------------------------===//
34
#include "llvm/ADT/DAGDeltaAlgorithm.h"
35
#include "llvm/ADT/DeltaAlgorithm.h"
36
#include "llvm/Support/Debug.h"
37
#include "llvm/Support/Format.h"
38
#include "llvm/Support/raw_ostream.h"
47
class DAGDeltaAlgorithmImpl {
48
friend class DeltaActiveSetHelper;
51
typedef DAGDeltaAlgorithm::change_ty change_ty;
52
typedef DAGDeltaAlgorithm::changeset_ty changeset_ty;
53
typedef DAGDeltaAlgorithm::changesetlist_ty changesetlist_ty;
54
typedef DAGDeltaAlgorithm::edge_ty edge_ty;
57
typedef std::vector<change_ty>::iterator pred_iterator_ty;
58
typedef std::vector<change_ty>::iterator succ_iterator_ty;
59
typedef std::set<change_ty>::iterator pred_closure_iterator_ty;
60
typedef std::set<change_ty>::iterator succ_closure_iterator_ty;
62
DAGDeltaAlgorithm &DDA;
64
const changeset_ty &Changes;
65
const std::vector<edge_ty> &Dependencies;
67
std::vector<change_ty> Roots;
69
/// Cache of failed test results. Successful test results are never cached
70
/// since we always reduce following a success. We maintain an independent
71
/// cache from that used by the individual delta passes because we may get
72
/// hits across multiple individual delta invocations.
73
mutable std::set<changeset_ty> FailedTestsCache;
76
std::map<change_ty, std::vector<change_ty> > Predecessors;
77
std::map<change_ty, std::vector<change_ty> > Successors;
79
std::map<change_ty, std::set<change_ty> > PredClosure;
80
std::map<change_ty, std::set<change_ty> > SuccClosure;
83
pred_iterator_ty pred_begin(change_ty Node) {
84
assert(Predecessors.count(Node) && "Invalid node!");
85
return Predecessors[Node].begin();
87
pred_iterator_ty pred_end(change_ty Node) {
88
assert(Predecessors.count(Node) && "Invalid node!");
89
return Predecessors[Node].end();
92
pred_closure_iterator_ty pred_closure_begin(change_ty Node) {
93
assert(PredClosure.count(Node) && "Invalid node!");
94
return PredClosure[Node].begin();
96
pred_closure_iterator_ty pred_closure_end(change_ty Node) {
97
assert(PredClosure.count(Node) && "Invalid node!");
98
return PredClosure[Node].end();
101
succ_iterator_ty succ_begin(change_ty Node) {
102
assert(Successors.count(Node) && "Invalid node!");
103
return Successors[Node].begin();
105
succ_iterator_ty succ_end(change_ty Node) {
106
assert(Successors.count(Node) && "Invalid node!");
107
return Successors[Node].end();
110
succ_closure_iterator_ty succ_closure_begin(change_ty Node) {
111
assert(SuccClosure.count(Node) && "Invalid node!");
112
return SuccClosure[Node].begin();
114
succ_closure_iterator_ty succ_closure_end(change_ty Node) {
115
assert(SuccClosure.count(Node) && "Invalid node!");
116
return SuccClosure[Node].end();
119
void UpdatedSearchState(const changeset_ty &Changes,
120
const changesetlist_ty &Sets,
121
const changeset_ty &Required) {
122
DDA.UpdatedSearchState(Changes, Sets, Required);
125
/// ExecuteOneTest - Execute a single test predicate on the change set \arg S.
126
bool ExecuteOneTest(const changeset_ty &S) {
127
// Check dependencies invariant.
129
for (changeset_ty::const_iterator it = S.begin(),
130
ie = S.end(); it != ie; ++it)
131
for (succ_iterator_ty it2 = succ_begin(*it),
132
ie2 = succ_end(*it); it2 != ie2; ++it2)
133
assert(S.count(*it2) && "Attempt to run invalid changeset!");
136
return DDA.ExecuteOneTest(S);
140
DAGDeltaAlgorithmImpl(DAGDeltaAlgorithm &_DDA,
141
const changeset_ty &_Changes,
142
const std::vector<edge_ty> &_Dependencies);
146
/// GetTestResult - Get the test result for the active set \arg Changes with
147
/// \arg Required changes from the cache, executing the test if necessary.
149
/// \param Changes - The set of active changes being minimized, which should
150
/// have their pred closure included in the test.
151
/// \param Required - The set of changes which have previously been
152
/// established to be required.
153
/// \return - The test result.
154
bool GetTestResult(const changeset_ty &Changes, const changeset_ty &Required);
157
/// Helper object for minimizing an active set of changes.
158
class DeltaActiveSetHelper : public DeltaAlgorithm {
159
DAGDeltaAlgorithmImpl &DDAI;
161
const changeset_ty &Required;
164
/// UpdatedSearchState - Callback used when the search state changes.
165
virtual void UpdatedSearchState(const changeset_ty &Changes,
166
const changesetlist_ty &Sets) {
167
DDAI.UpdatedSearchState(Changes, Sets, Required);
170
virtual bool ExecuteOneTest(const changeset_ty &S) {
171
return DDAI.GetTestResult(S, Required);
175
DeltaActiveSetHelper(DAGDeltaAlgorithmImpl &_DDAI,
176
const changeset_ty &_Required)
177
: DDAI(_DDAI), Required(_Required) {}
182
DAGDeltaAlgorithmImpl::DAGDeltaAlgorithmImpl(DAGDeltaAlgorithm &_DDA,
183
const changeset_ty &_Changes,
184
const std::vector<edge_ty>
188
Dependencies(_Dependencies)
190
for (changeset_ty::const_iterator it = Changes.begin(),
191
ie = Changes.end(); it != ie; ++it) {
192
Predecessors.insert(std::make_pair(*it, std::vector<change_ty>()));
193
Successors.insert(std::make_pair(*it, std::vector<change_ty>()));
195
for (std::vector<edge_ty>::const_iterator it = Dependencies.begin(),
196
ie = Dependencies.end(); it != ie; ++it) {
197
Predecessors[it->second].push_back(it->first);
198
Successors[it->first].push_back(it->second);
201
// Compute the roots.
202
for (changeset_ty::const_iterator it = Changes.begin(),
203
ie = Changes.end(); it != ie; ++it)
204
if (succ_begin(*it) == succ_end(*it))
205
Roots.push_back(*it);
207
// Pre-compute the closure of the successor relation.
208
std::vector<change_ty> Worklist(Roots.begin(), Roots.end());
209
while (!Worklist.empty()) {
210
change_ty Change = Worklist.back();
213
std::set<change_ty> &ChangeSuccs = SuccClosure[Change];
214
for (pred_iterator_ty it = pred_begin(Change),
215
ie = pred_end(Change); it != ie; ++it) {
216
SuccClosure[*it].insert(Change);
217
SuccClosure[*it].insert(ChangeSuccs.begin(), ChangeSuccs.end());
218
Worklist.push_back(*it);
222
// Invert to form the predecessor closure map.
223
for (changeset_ty::const_iterator it = Changes.begin(),
224
ie = Changes.end(); it != ie; ++it)
225
PredClosure.insert(std::make_pair(*it, std::set<change_ty>()));
226
for (changeset_ty::const_iterator it = Changes.begin(),
227
ie = Changes.end(); it != ie; ++it)
228
for (succ_closure_iterator_ty it2 = succ_closure_begin(*it),
229
ie2 = succ_closure_end(*it); it2 != ie2; ++it2)
230
PredClosure[*it2].insert(*it);
232
// Dump useful debug info.
234
llvm::errs() << "-- DAGDeltaAlgorithmImpl --\n";
235
llvm::errs() << "Changes: [";
236
for (changeset_ty::const_iterator it = Changes.begin(),
237
ie = Changes.end(); it != ie; ++it) {
238
if (it != Changes.begin()) llvm::errs() << ", ";
241
if (succ_begin(*it) != succ_end(*it)) {
243
for (succ_iterator_ty it2 = succ_begin(*it),
244
ie2 = succ_end(*it); it2 != ie2; ++it2) {
245
if (it2 != succ_begin(*it)) llvm::errs() << ", ";
246
llvm::errs() << "->" << *it2;
251
llvm::errs() << "]\n";
253
llvm::errs() << "Roots: [";
254
for (std::vector<change_ty>::const_iterator it = Roots.begin(),
255
ie = Roots.end(); it != ie; ++it) {
256
if (it != Roots.begin()) llvm::errs() << ", ";
259
llvm::errs() << "]\n";
261
llvm::errs() << "Predecessor Closure:\n";
262
for (changeset_ty::const_iterator it = Changes.begin(),
263
ie = Changes.end(); it != ie; ++it) {
264
llvm::errs() << format(" %-4d: [", *it);
265
for (pred_closure_iterator_ty it2 = pred_closure_begin(*it),
266
ie2 = pred_closure_end(*it); it2 != ie2; ++it2) {
267
if (it2 != pred_closure_begin(*it)) llvm::errs() << ", ";
268
llvm::errs() << *it2;
270
llvm::errs() << "]\n";
273
llvm::errs() << "Successor Closure:\n";
274
for (changeset_ty::const_iterator it = Changes.begin(),
275
ie = Changes.end(); it != ie; ++it) {
276
llvm::errs() << format(" %-4d: [", *it);
277
for (succ_closure_iterator_ty it2 = succ_closure_begin(*it),
278
ie2 = succ_closure_end(*it); it2 != ie2; ++it2) {
279
if (it2 != succ_closure_begin(*it)) llvm::errs() << ", ";
280
llvm::errs() << *it2;
282
llvm::errs() << "]\n";
285
llvm::errs() << "\n\n";
289
bool DAGDeltaAlgorithmImpl::GetTestResult(const changeset_ty &Changes,
290
const changeset_ty &Required) {
291
changeset_ty Extended(Required);
292
Extended.insert(Changes.begin(), Changes.end());
293
for (changeset_ty::const_iterator it = Changes.begin(),
294
ie = Changes.end(); it != ie; ++it)
295
Extended.insert(pred_closure_begin(*it), pred_closure_end(*it));
297
if (FailedTestsCache.count(Extended))
300
bool Result = ExecuteOneTest(Extended);
302
FailedTestsCache.insert(Extended);
307
DAGDeltaAlgorithm::changeset_ty
308
DAGDeltaAlgorithmImpl::Run() {
309
// The current set of changes we are minimizing, starting at the roots.
310
changeset_ty CurrentSet(Roots.begin(), Roots.end());
312
// The set of required changes.
313
changeset_ty Required;
315
// Iterate until the active set of changes is empty. Convergence is guaranteed
316
// assuming input was a DAG.
318
// Invariant: CurrentSet intersect Required == {}
319
// Invariant: Required == (Required union succ*(Required))
320
while (!CurrentSet.empty()) {
322
llvm::errs() << "DAG_DD - " << CurrentSet.size() << " active changes, "
323
<< Required.size() << " required changes\n";
326
// Minimize the current set of changes.
327
DeltaActiveSetHelper Helper(*this, Required);
328
changeset_ty CurrentMinSet = Helper.Run(CurrentSet);
330
// Update the set of required changes. Since
331
// CurrentMinSet subset CurrentSet
332
// and after the last iteration,
333
// succ(CurrentSet) subset Required
335
// succ(CurrentMinSet) subset Required
336
// and our invariant on Required is maintained.
337
Required.insert(CurrentMinSet.begin(), CurrentMinSet.end());
339
// Replace the current set with the predecssors of the minimized set of
342
for (changeset_ty::const_iterator it = CurrentMinSet.begin(),
343
ie = CurrentMinSet.end(); it != ie; ++it)
344
CurrentSet.insert(pred_begin(*it), pred_end(*it));
346
// FIXME: We could enforce CurrentSet intersect Required == {} here if we
347
// wanted to protect against cyclic graphs.
353
DAGDeltaAlgorithm::changeset_ty
354
DAGDeltaAlgorithm::Run(const changeset_ty &Changes,
355
const std::vector<edge_ty> &Dependencies) {
356
return DAGDeltaAlgorithmImpl(*this, Changes, Dependencies).Run();