2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
14 #include "isl_piplib.h"
15 #include "isl_map_piplib.h"
16 #include "isl_map_private.h"
18 static void copy_values_from(isl_int
*dst
, Entier
*src
, unsigned n
)
22 for (i
= 0; i
< n
; ++i
)
23 entier_assign(dst
[i
], src
[i
]);
26 static void add_value(isl_int
*dst
, Entier
*src
)
28 mpz_add(*dst
, *dst
, *src
);
31 static void copy_constraint_from(isl_int
*dst
, PipVector
*src
,
32 unsigned nparam
, unsigned n_in
, unsigned n_out
,
33 unsigned extra
, int *pos
)
37 copy_values_from(dst
, src
->the_vector
+src
->nb_elements
-1, 1);
38 copy_values_from(dst
+1, src
->the_vector
, nparam
+n_in
);
39 isl_seq_clr(dst
+1+nparam
+n_in
, n_out
);
40 isl_seq_clr(dst
+1+nparam
+n_in
+n_out
, extra
);
41 for (i
= 0; i
+ n_in
+ nparam
< src
->nb_elements
-1; ++i
) {
43 add_value(&dst
[1+nparam
+n_in
+n_out
+p
],
44 &src
->the_vector
[n_in
+nparam
+i
]);
48 static int add_inequality(struct isl_ctx
*ctx
,
49 struct isl_basic_map
*bmap
, int *pos
, PipVector
*vec
)
51 unsigned nparam
= isl_basic_map_n_param(bmap
);
52 unsigned n_in
= isl_basic_map_n_in(bmap
);
53 unsigned n_out
= isl_basic_map_n_out(bmap
);
54 unsigned n_div
= isl_basic_map_n_div(bmap
);
55 int i
= isl_basic_map_alloc_inequality(bmap
);
58 copy_constraint_from(bmap
->ineq
[i
], vec
,
59 nparam
, n_in
, n_out
, n_div
, pos
);
64 /* For a div d = floor(f/m), add the constraints
67 * -(f-(n-1)) + m d >= 0
69 * Note that the second constraint is the negation of
73 static int add_div_constraints(struct isl_ctx
*ctx
,
74 struct isl_basic_map
*bmap
, int *pos
, PipNewparm
*p
, unsigned div
)
77 unsigned total
= isl_basic_map_total_dim(bmap
);
78 unsigned div_pos
= 1 + total
- bmap
->n_div
+ div
;
80 i
= add_inequality(ctx
, bmap
, pos
, p
->vector
);
83 copy_values_from(&bmap
->ineq
[i
][div_pos
], &p
->deno
, 1);
84 isl_int_neg(bmap
->ineq
[i
][div_pos
], bmap
->ineq
[i
][div_pos
]);
86 j
= isl_basic_map_alloc_inequality(bmap
);
89 isl_seq_neg(bmap
->ineq
[j
], bmap
->ineq
[i
], 1 + total
);
90 isl_int_add(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], bmap
->ineq
[j
][div_pos
]);
91 isl_int_sub_ui(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], 1);
95 static int add_equality(struct isl_ctx
*ctx
,
96 struct isl_basic_map
*bmap
, int *pos
,
97 unsigned var
, PipVector
*vec
)
100 unsigned nparam
= isl_basic_map_n_param(bmap
);
101 unsigned n_in
= isl_basic_map_n_in(bmap
);
102 unsigned n_out
= isl_basic_map_n_out(bmap
);
104 isl_assert(ctx
, var
< n_out
, return -1);
106 i
= isl_basic_map_alloc_equality(bmap
);
109 copy_constraint_from(bmap
->eq
[i
], vec
,
110 nparam
, n_in
, n_out
, bmap
->extra
, pos
);
111 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+var
], -1);
116 static int find_div(struct isl_ctx
*ctx
,
117 struct isl_basic_map
*bmap
, int *pos
, PipNewparm
*p
)
120 unsigned nparam
= isl_basic_map_n_param(bmap
);
121 unsigned n_in
= isl_basic_map_n_in(bmap
);
122 unsigned n_out
= isl_basic_map_n_out(bmap
);
124 i
= isl_basic_map_alloc_div(bmap
);
128 copy_constraint_from(bmap
->div
[i
]+1, p
->vector
,
129 nparam
, n_in
, n_out
, bmap
->extra
, pos
);
131 copy_values_from(bmap
->div
[i
], &p
->deno
, 1);
132 for (j
= 0; j
< i
; ++j
)
133 if (isl_seq_eq(bmap
->div
[i
], bmap
->div
[j
],
134 1+1+isl_basic_map_total_dim(bmap
)+j
)) {
135 isl_basic_map_free_div(bmap
, 1);
139 if (add_div_constraints(ctx
, bmap
, pos
, p
, i
) < 0)
145 /* Count some properties of a quast
146 * - maximal number of new parameters
148 * - total number of solutions
149 * - total number of empty branches
151 static void quast_count(PipQuast
*q
, int *maxnew
, int depth
, int *maxdepth
,
152 int *sol
, int *nosol
)
156 for (p
= q
->newparm
; p
; p
= p
->next
)
157 if (p
->rank
> *maxnew
)
160 if (++depth
> *maxdepth
)
162 quast_count(q
->next_else
, maxnew
, depth
, maxdepth
, sol
, nosol
);
163 quast_count(q
->next_then
, maxnew
, depth
, maxdepth
, sol
, nosol
);
173 * pos: array of length bmap->set.extra, mapping each of the existential
174 * variables PIP proposes to an existential variable in bmap
175 * bmap: collects the currently active constraints
176 * rest: collects the empty leaves of the quast (if not NULL)
180 struct isl_basic_map
*bmap
;
181 struct isl_set
**rest
;
186 * New existentially quantified variables are places after the existing ones.
188 static struct isl_map
*scan_quast_r(struct scan_data
*data
, PipQuast
*q
,
192 struct isl_basic_map
*bmap
= data
->bmap
;
193 unsigned old_n_div
= bmap
->n_div
;
194 unsigned nparam
= isl_basic_map_n_param(bmap
);
195 unsigned n_in
= isl_basic_map_n_in(bmap
);
196 unsigned n_out
= isl_basic_map_n_out(bmap
);
201 for (p
= q
->newparm
; p
; p
= p
->next
) {
203 unsigned pip_param
= nparam
+ n_in
;
205 pos
= find_div(data
->ctx
, bmap
, data
->pos
, p
);
208 data
->pos
[p
->rank
- pip_param
] = pos
;
212 int pos
= add_inequality(data
->ctx
, bmap
, data
->pos
,
216 map
= scan_quast_r(data
, q
->next_then
, map
);
218 if (isl_inequality_negate(bmap
, pos
))
220 map
= scan_quast_r(data
, q
->next_else
, map
);
222 if (isl_basic_map_free_inequality(bmap
, 1))
224 } else if (q
->list
) {
227 /* if bmap->n_out is zero, we are only interested in the domains
228 * where a solution exists and not in the actual solution
230 for (j
= 0, l
= q
->list
; j
< n_out
&& l
; ++j
, l
= l
->next
)
231 if (add_equality(data
->ctx
, bmap
, data
->pos
, j
,
234 map
= isl_map_add_basic_map(map
, isl_basic_map_copy(bmap
));
235 if (isl_basic_map_free_equality(bmap
, n_out
))
237 } else if (data
->rest
) {
238 struct isl_basic_set
*bset
;
239 bset
= isl_basic_set_from_basic_map(isl_basic_map_copy(bmap
));
240 bset
= isl_basic_set_drop_dims(bset
, n_in
, n_out
);
243 *data
->rest
= isl_set_add_basic_set(*data
->rest
, bset
);
246 if (isl_basic_map_free_inequality(bmap
, 2*(bmap
->n_div
- old_n_div
)))
248 if (isl_basic_map_free_div(bmap
, bmap
->n_div
- old_n_div
))
257 * Returns a map of dimension "keep_dim" with "context" as domain and
258 * as range the first "isl_dim_size(keep_dim, isl_dim_out)" variables
259 * in the quast lists.
261 static struct isl_map
*isl_map_from_quast(struct isl_ctx
*ctx
, PipQuast
*q
,
262 struct isl_dim
*keep_dim
,
263 struct isl_basic_set
*context
,
264 struct isl_set
**rest
)
270 struct scan_data data
;
271 struct isl_map
*map
= NULL
;
272 struct isl_dim
*dims
;
282 if (!context
|| !keep_dim
)
285 dim
= isl_basic_set_n_dim(context
);
286 nparam
= isl_basic_set_n_param(context
);
287 keep
= isl_dim_size(keep_dim
, isl_dim_out
);
288 pip_param
= nparam
+ dim
;
293 nexist
= pip_param
-1;
294 quast_count(q
, &nexist
, 0, &max_depth
, &n_sol
, &n_nosol
);
295 nexist
-= pip_param
-1;
298 *rest
= isl_set_alloc_dim(isl_dim_copy(context
->dim
), n_nosol
,
303 map
= isl_map_alloc_dim(isl_dim_copy(keep_dim
), n_sol
,
308 dims
= isl_dim_reverse(isl_dim_copy(context
->dim
));
309 data
.bmap
= isl_basic_map_from_basic_set(context
, dims
);
310 data
.bmap
= isl_basic_map_extend_dim(data
.bmap
,
311 keep_dim
, nexist
, keep
, max_depth
+2*nexist
);
315 if (data
.bmap
->extra
) {
317 data
.pos
= isl_alloc_array(ctx
, int, data
.bmap
->extra
);
320 for (i
= 0; i
< data
.bmap
->n_div
; ++i
)
324 map
= scan_quast_r(&data
, q
, map
);
325 map
= isl_map_finalize(map
);
329 *rest
= isl_set_finalize(*rest
);
333 isl_basic_map_free(data
.bmap
);
338 isl_basic_set_free(context
);
339 isl_dim_free(keep_dim
);
343 isl_basic_map_free(data
.bmap
);
352 static void copy_values_to(Entier
*dst
, isl_int
*src
, unsigned n
)
356 for (i
= 0; i
< n
; ++i
)
357 entier_assign(dst
[i
], src
[i
]);
360 static void copy_constraint_to(Entier
*dst
, isl_int
*src
,
361 unsigned pip_param
, unsigned pip_var
,
362 unsigned extra_front
, unsigned extra_back
)
364 copy_values_to(dst
+1+extra_front
+pip_var
+pip_param
+extra_back
, src
, 1);
365 copy_values_to(dst
+1+extra_front
+pip_var
, src
+1, pip_param
);
366 copy_values_to(dst
+1+extra_front
, src
+1+pip_param
, pip_var
);
369 PipMatrix
*isl_basic_map_to_pip(struct isl_basic_map
*bmap
, unsigned pip_param
,
370 unsigned extra_front
, unsigned extra_back
)
377 unsigned pip_var
= isl_basic_map_total_dim(bmap
) - pip_param
;
379 nrow
= extra_front
+ bmap
->n_eq
+ bmap
->n_ineq
;
380 ncol
= 1 + extra_front
+ pip_var
+ pip_param
+ extra_back
+ 1;
381 M
= pip_matrix_alloc(nrow
, ncol
);
386 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
387 entier_set_si(M
->p
[off
+i
][0], 0);
388 copy_constraint_to(M
->p
[off
+i
], bmap
->eq
[i
],
389 pip_param
, pip_var
, extra_front
, extra_back
);
392 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
393 entier_set_si(M
->p
[off
+i
][0], 1);
394 copy_constraint_to(M
->p
[off
+i
], bmap
->ineq
[i
],
395 pip_param
, pip_var
, extra_front
, extra_back
);
400 PipMatrix
*isl_basic_set_to_pip(struct isl_basic_set
*bset
, unsigned pip_param
,
401 unsigned extra_front
, unsigned extra_back
)
403 return isl_basic_map_to_pip((struct isl_basic_map
*)bset
,
404 pip_param
, extra_front
, extra_back
);
407 struct isl_map
*isl_pip_basic_map_lexopt(
408 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
409 struct isl_set
**empty
, int max
)
415 PipMatrix
*domain
= NULL
, *context
= NULL
;
416 unsigned nparam
, n_in
, n_out
;
418 bmap
= isl_basic_map_detect_equalities(bmap
);
423 isl_assert(ctx
, isl_basic_map_compatible_domain(bmap
, dom
), goto error
);
424 nparam
= isl_basic_map_n_param(bmap
);
425 n_in
= isl_basic_map_n_in(bmap
);
426 n_out
= isl_basic_map_n_out(bmap
);
428 domain
= isl_basic_map_to_pip(bmap
, nparam
+ n_in
, 0, dom
->n_div
);
431 context
= isl_basic_map_to_pip((struct isl_basic_map
*)dom
, 0, 0, 0);
435 options
= pip_options_init();
436 options
->Simplify
= 1;
437 options
->Maximize
= max
;
438 options
->Urs_unknowns
= -1;
439 options
->Urs_parms
= -1;
440 sol
= pip_solve(domain
, context
, -1, options
);
443 struct isl_basic_set
*copy
;
444 copy
= isl_basic_set_copy(dom
);
445 map
= isl_map_from_quast(ctx
, sol
,
446 isl_dim_copy(bmap
->dim
), copy
, empty
);
448 map
= isl_map_empty_like_basic_map(bmap
);
454 if (map
->n
== 0 && empty
) {
455 isl_set_free(*empty
);
456 *empty
= isl_set_from_basic_set(dom
);
458 isl_basic_set_free(dom
);
459 isl_basic_map_free(bmap
);
462 pip_options_free(options
);
463 pip_matrix_free(domain
);
464 pip_matrix_free(context
);
469 pip_matrix_free(domain
);
471 pip_matrix_free(context
);
472 isl_basic_map_free(bmap
);
473 isl_basic_set_free(dom
);