-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathbefore_presolver.cpp
More file actions
253 lines (217 loc) · 7.82 KB
/
Copy pathbefore_presolver.cpp
File metadata and controls
253 lines (217 loc) · 7.82 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
* Main authors:
* Erik Ekstrom <eeks@sics.se>
* Roberto Castaneda Lozano <rcas@acm.org>
* Mats Carlsson <mats.carlsson@ri.se>
*
* This file is part of Unison, see http://unison-code.github.io
*
* Copyright (c) 2015-2016, Erik Ekstrom
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Generator of data to be used with before constraints. Part of C++ Presolver.
*/
#include "before_presolver.hpp"
#include "unsafe_temp.hpp"
BeforePresolver::BeforePresolver(Parameters& input) :
input(input) { };
void BeforePresolver::presolve(vector<presolver_conj>& Nogoods) {
// Prebuild congr congr_map
generate_congruence_operands(input, congr_map);
// Prebuild copyrelated operands maping
generate_copyrel_operands_map(input, copy_rel_operands);
beforeset b;
gen_before(b);
// TODO: should maybe not do it like this, instead
// have an invariant that everything is sorted during derivation.
// [MC] No, it's more convenient like this.
sort(b.begin(),b.end());
b.erase(unique(b.begin(), b.end()), b.end());
before_vs_nogoods(b, Nogoods);
}
void BeforePresolver::gen_before(beforeset& B) {
for(const vector<operand>& C : input.strictly_congr) {
for(unsigned i = 0; i < C.size(); i++) {
for(unsigned j = i+1; j < C.size(); j++) {
// {i, j} -> All pairs in C
operand p = C[i];
operand q = C[j];
// p are (in) and q are (out) of the same block
if(input.pb[p] == input.pb[q] &&
input.type[input.oper[p]] == IN &&
input.type[input.oper[q]] == OUT) {
// B <- B union Before1(p,q)
before1(p,q,B);
}
}
}
}
// Map block --> vector<p, r, w>
map<block, vector<vector<int> > > M;
for(const vector<int>& pr : input.preassign) {
if(pr.size() == 2) {
int p = pr[0];
int r = pr[1];
// width of min(temps_but_null(p))
int w = input.width[first_temp_but_null(input, p)];
block b = input.pb[p];
// map b -> <p,r,w> in M
vector_insert(M[b], {p, r, w});
}
}
for(auto const& it : M) {
const vector<vector<int> >& C = it.second;
const vector<vector<int> >& P = emit_before(C);
Digraph G = Digraph(P);
Digraph GR = G.reduction();
// B <- B union {<p,q,{{}}> | <p,q> in R}
for(const edge& e : GR.edges()) {
PresolverBefore pb;
pb.p = e.first;
pb.q = e.second;
B.push_back(pb);
}
}
// B <- B union {<p,q,{{}}> | p in input.last_use && the temporary of
// p can't be live past p, and q is def of the
// same operation as p.}
for(operand p : input.last_use) {
if(input.use[p] && !input.temps[p].empty()) {
operation o = input.oper[p];
unsigned int pi = find_index(input.operands[o], p);
bool negative_use_lat = false;
for (unsigned int ii = 0; ii < input.instructions[o].size(); ii++) {
if (input.lat[o][ii][pi] < 0) {
negative_use_lat = true;
break;
}
}
if(!negative_use_lat) {
for(operand q : oper_defs(input, o)) {
// q is def of the same operation as p is def in.
PresolverBefore pb;
pb.p = p;
pb.q = q;
B.push_back(pb);
}
}
}
}
// B <- B union {<p,q,{{}}> | p defines t, which is not unsafe and has its single use in the operation defining q.}
for(operand p : input.P) {
if(!input.use[p]) {
vector<temporary> ts = input.temps[p];
if(ts.size() == 1 && !temp_is_unsafe(input, ts[0])) {
vector<operand> uses = input.users[ts[0]];
if(uses.size() == 1) {
for(operand q : oper_defs(input, input.oper[uses[0]])) {
PresolverBefore pb;
pb.p = p;
pb.q = q;
B.push_back(pb);
}
}
}
}
}
// B <- B union {<p,q,{{eq(p(r),t(t)}}> | p,q are def, r is use,
// t is a temp, and HighCombine(p,q,r,t)}
//
for (operation o : input.O) if (input.type[o] == COMBINE) {
for (operand r : oper_uses(input, o)) {
for (temporary t : input.temps[r]) {
operation h = input.def_opr[t];
if(input.type[h] == HIGH) {
// p is the use operand of HIGH
// FIXME: fix spec (GENBEFORE()) when it says "p, q are def"!
operand p = first_use(input, h);
// q is the first use operand of COMBINE (according to
// high_combine_before).
// FIXME: fix spec (GENBEFORE()) when it says "p, q are def"!
operand q = first_use(input, o);
PresolverBefore pb;
UnisonConstraintExpr e(CONNECTS_EXPR, {r,t}, {});
pb.p = p;
pb.q = q;
pb.d.push_back({e});
B.push_back(pb);
}
}
}
}
}
void BeforePresolver::before1(operand p, operand q, vector<PresolverBefore>& B) {
if(!ord_contains(copy_rel_operands[p], q)) {
PresolverBefore pb;
pb.p = p;
pb.q = q;
B.push_back(pb);
}
}
vector<vector<operand> > BeforePresolver::emit_before(const vector<vector<int> >& C) {
vector<vector<operand> > V;
for(auto it1 = C.begin(); it1 != C.end(); it1++) {
for(auto it2 = next(it1); it2 != C.end(); it2++) {
const vector<int>& c1 = *it1; // <p, r, w>
const vector<int>& c2 = *it2;
int p = c1[0]; int r = c1[1]; int w = c1[2];
int pp = c2[0]; int rr = c2[1]; int ww = c2[2];
if(((r + w) > rr) &&
((rr + ww) > r) &&
!input.use[pp]) {
vector_insert(V, {p,pp});
}
else if(((r + w) > rr) &&
((rr + ww) > r) &&
!ord_intersect(input.temps[p], input.temps[pp])) {
vector_insert(V, {p,pp});
}
}
}
return V;
}
void BeforePresolver::before_vs_nogoods(beforeset& T, vector<presolver_conj>& Nogoods) {
for(PresolverBefore& t : T) {
// before <- {t | t = <p,q,{{}}> in T}
// nogoods <- {conj union {overlap(p(p), p(q))} | <p,q,conj> in T where conj = {_|_}}
if(t.d.empty()) {
UnisonConstraintExpr e(AND_EXPR, {}, {});
PresolverBeforeJSON u(t.p, t.q, e);
input.before.push_back(u);
} else {
UnisonConstraintExpr e(OPERAND_OVERLAP_EXPR, {t.p,t.q}, {});
presolver_conj _conj = {e};
for(const presolver_conj& c : t.d) {
_conj.insert(_conj.end(), c.begin(), c.end());
}
Nogoods.push_back(normal_conjunction(input, _conj));
}
}
}
void BeforePresolver::presolve(Parameters& input, vector<presolver_conj>& Nogoods) {
BeforePresolver bg(input);
bg.presolve(Nogoods);
}