2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 #include <isl_ctx_private.h>
11 #include <isl_map_private.h>
16 #include "isl_piplib.h"
17 #include "isl_map_piplib.h"
19 static void copy_values_from(isl_int
*dst
, Entier
*src
, unsigned n
)
23 for (i
= 0; i
< n
; ++i
)
24 entier_assign(dst
[i
], src
[i
]);
27 static void add_value(isl_int
*dst
, Entier
*src
)
29 mpz_add(*dst
, *dst
, *src
);
32 static void copy_constraint_from(isl_int
*dst
, PipVector
*src
,
33 unsigned nparam
, unsigned n_in
, unsigned n_out
,
34 unsigned extra
, int *pos
)
38 copy_values_from(dst
, src
->the_vector
+src
->nb_elements
-1, 1);
39 copy_values_from(dst
+1, src
->the_vector
, nparam
+n_in
);
40 isl_seq_clr(dst
+1+nparam
+n_in
, n_out
);
41 isl_seq_clr(dst
+1+nparam
+n_in
+n_out
, extra
);
42 for (i
= 0; i
+ n_in
+ nparam
< src
->nb_elements
-1; ++i
) {
44 add_value(&dst
[1+nparam
+n_in
+n_out
+p
],
45 &src
->the_vector
[n_in
+nparam
+i
]);
49 static int add_inequality(struct isl_ctx
*ctx
,
50 struct isl_basic_map
*bmap
, int *pos
, PipVector
*vec
)
52 unsigned nparam
= isl_basic_map_n_param(bmap
);
53 unsigned n_in
= isl_basic_map_n_in(bmap
);
54 unsigned n_out
= isl_basic_map_n_out(bmap
);
55 unsigned n_div
= isl_basic_map_n_div(bmap
);
56 int i
= isl_basic_map_alloc_inequality(bmap
);
59 copy_constraint_from(bmap
->ineq
[i
], vec
,
60 nparam
, n_in
, n_out
, n_div
, pos
);
65 /* For a div d = floor(f/m), add the constraints
68 * -(f-(n-1)) + m d >= 0
70 * Note that the second constraint is the negation of
74 static int add_div_constraints(struct isl_ctx
*ctx
,
75 struct isl_basic_map
*bmap
, int *pos
, PipNewparm
*p
, unsigned div
)
78 unsigned total
= isl_basic_map_total_dim(bmap
);
79 unsigned div_pos
= 1 + total
- bmap
->n_div
+ div
;
81 i
= add_inequality(ctx
, bmap
, pos
, p
->vector
);
84 copy_values_from(&bmap
->ineq
[i
][div_pos
], &p
->deno
, 1);
85 isl_int_neg(bmap
->ineq
[i
][div_pos
], bmap
->ineq
[i
][div_pos
]);
87 j
= isl_basic_map_alloc_inequality(bmap
);
90 isl_seq_neg(bmap
->ineq
[j
], bmap
->ineq
[i
], 1 + total
);
91 isl_int_add(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], bmap
->ineq
[j
][div_pos
]);
92 isl_int_sub_ui(bmap
->ineq
[j
][0], bmap
->ineq
[j
][0], 1);
96 static int add_equality(struct isl_ctx
*ctx
,
97 struct isl_basic_map
*bmap
, int *pos
,
98 unsigned var
, PipVector
*vec
)
101 unsigned nparam
= isl_basic_map_n_param(bmap
);
102 unsigned n_in
= isl_basic_map_n_in(bmap
);
103 unsigned n_out
= isl_basic_map_n_out(bmap
);
105 isl_assert(ctx
, var
< n_out
, return -1);
107 i
= isl_basic_map_alloc_equality(bmap
);
110 copy_constraint_from(bmap
->eq
[i
], vec
,
111 nparam
, n_in
, n_out
, bmap
->extra
, pos
);
112 isl_int_set_si(bmap
->eq
[i
][1+nparam
+n_in
+var
], -1);
117 static int find_div(struct isl_ctx
*ctx
,
118 struct isl_basic_map
*bmap
, int *pos
, PipNewparm
*p
)
121 unsigned nparam
= isl_basic_map_n_param(bmap
);
122 unsigned n_in
= isl_basic_map_n_in(bmap
);
123 unsigned n_out
= isl_basic_map_n_out(bmap
);
125 i
= isl_basic_map_alloc_div(bmap
);
129 copy_constraint_from(bmap
->div
[i
]+1, p
->vector
,
130 nparam
, n_in
, n_out
, bmap
->extra
, pos
);
132 copy_values_from(bmap
->div
[i
], &p
->deno
, 1);
133 for (j
= 0; j
< i
; ++j
)
134 if (isl_seq_eq(bmap
->div
[i
], bmap
->div
[j
],
135 1+1+isl_basic_map_total_dim(bmap
)+j
)) {
136 isl_basic_map_free_div(bmap
, 1);
140 if (add_div_constraints(ctx
, bmap
, pos
, p
, i
) < 0)
146 /* Count some properties of a quast
147 * - maximal number of new parameters
149 * - total number of solutions
150 * - total number of empty branches
152 static void quast_count(PipQuast
*q
, int *maxnew
, int depth
, int *maxdepth
,
153 int *sol
, int *nosol
)
157 for (p
= q
->newparm
; p
; p
= p
->next
)
158 if (p
->rank
> *maxnew
)
161 if (++depth
> *maxdepth
)
163 quast_count(q
->next_else
, maxnew
, depth
, maxdepth
, sol
, nosol
);
164 quast_count(q
->next_then
, maxnew
, depth
, maxdepth
, sol
, nosol
);
174 * pos: array of length bmap->set.extra, mapping each of the existential
175 * variables PIP proposes to an existential variable in bmap
176 * bmap: collects the currently active constraints
177 * rest: collects the empty leaves of the quast (if not NULL)
181 struct isl_basic_map
*bmap
;
182 struct isl_set
**rest
;
187 * New existentially quantified variables are places after the existing ones.
189 static struct isl_map
*scan_quast_r(struct scan_data
*data
, PipQuast
*q
,
193 struct isl_basic_map
*bmap
= data
->bmap
;
194 unsigned old_n_div
= bmap
->n_div
;
195 unsigned nparam
= isl_basic_map_n_param(bmap
);
196 unsigned n_in
= isl_basic_map_n_in(bmap
);
197 unsigned n_out
= isl_basic_map_n_out(bmap
);
202 for (p
= q
->newparm
; p
; p
= p
->next
) {
204 unsigned pip_param
= nparam
+ n_in
;
206 pos
= find_div(data
->ctx
, bmap
, data
->pos
, p
);
209 data
->pos
[p
->rank
- pip_param
] = pos
;
213 int pos
= add_inequality(data
->ctx
, bmap
, data
->pos
,
217 map
= scan_quast_r(data
, q
->next_then
, map
);
219 if (isl_inequality_negate(bmap
, pos
))
221 map
= scan_quast_r(data
, q
->next_else
, map
);
223 if (isl_basic_map_free_inequality(bmap
, 1))
225 } else if (q
->list
) {
228 /* if bmap->n_out is zero, we are only interested in the domains
229 * where a solution exists and not in the actual solution
231 for (j
= 0, l
= q
->list
; j
< n_out
&& l
; ++j
, l
= l
->next
)
232 if (add_equality(data
->ctx
, bmap
, data
->pos
, j
,
235 map
= isl_map_add_basic_map(map
, isl_basic_map_copy(bmap
));
236 if (isl_basic_map_free_equality(bmap
, n_out
))
238 } else if (data
->rest
) {
239 struct isl_basic_set
*bset
;
240 bset
= isl_basic_set_from_basic_map(isl_basic_map_copy(bmap
));
241 bset
= isl_basic_set_drop_dims(bset
, n_in
, n_out
);
244 *data
->rest
= isl_set_add_basic_set(*data
->rest
, bset
);
247 if (isl_basic_map_free_inequality(bmap
, 2*(bmap
->n_div
- old_n_div
)))
249 if (isl_basic_map_free_div(bmap
, bmap
->n_div
- old_n_div
))
258 * Returns a map of dimension "keep_dim" with "context" as domain and
259 * as range the first "isl_space_dim(keep_dim, isl_dim_out)" variables
260 * in the quast lists.
262 static struct isl_map
*isl_map_from_quast(struct isl_ctx
*ctx
, PipQuast
*q
,
264 struct isl_basic_set
*context
,
265 struct isl_set
**rest
)
271 struct scan_data data
;
272 struct isl_map
*map
= NULL
;
283 if (!context
|| !keep_dim
)
286 dim
= isl_basic_set_n_dim(context
);
287 nparam
= isl_basic_set_n_param(context
);
288 keep
= isl_space_dim(keep_dim
, isl_dim_out
);
289 pip_param
= nparam
+ dim
;
294 nexist
= pip_param
-1;
295 quast_count(q
, &nexist
, 0, &max_depth
, &n_sol
, &n_nosol
);
296 nexist
-= pip_param
-1;
299 *rest
= isl_set_alloc_space(isl_space_copy(context
->dim
), n_nosol
,
304 map
= isl_map_alloc_space(isl_space_copy(keep_dim
), n_sol
,
309 dims
= isl_space_reverse(isl_space_copy(context
->dim
));
310 data
.bmap
= isl_basic_map_from_basic_set(context
, dims
);
311 data
.bmap
= isl_basic_map_extend_space(data
.bmap
,
312 keep_dim
, nexist
, keep
, max_depth
+2*nexist
);
316 if (data
.bmap
->extra
) {
318 data
.pos
= isl_alloc_array(ctx
, int, data
.bmap
->extra
);
321 for (i
= 0; i
< data
.bmap
->n_div
; ++i
)
325 map
= scan_quast_r(&data
, q
, map
);
326 map
= isl_map_finalize(map
);
330 *rest
= isl_set_finalize(*rest
);
334 isl_basic_map_free(data
.bmap
);
339 isl_basic_set_free(context
);
340 isl_space_free(keep_dim
);
344 isl_basic_map_free(data
.bmap
);
353 static void copy_values_to(Entier
*dst
, isl_int
*src
, unsigned n
)
357 for (i
= 0; i
< n
; ++i
)
358 entier_assign(dst
[i
], src
[i
]);
361 static void copy_constraint_to(Entier
*dst
, isl_int
*src
,
362 unsigned pip_param
, unsigned pip_var
,
363 unsigned extra_front
, unsigned extra_back
)
365 copy_values_to(dst
+1+extra_front
+pip_var
+pip_param
+extra_back
, src
, 1);
366 copy_values_to(dst
+1+extra_front
+pip_var
, src
+1, pip_param
);
367 copy_values_to(dst
+1+extra_front
, src
+1+pip_param
, pip_var
);
370 PipMatrix
*isl_basic_map_to_pip(struct isl_basic_map
*bmap
, unsigned pip_param
,
371 unsigned extra_front
, unsigned extra_back
)
378 unsigned pip_var
= isl_basic_map_total_dim(bmap
) - pip_param
;
380 nrow
= extra_front
+ bmap
->n_eq
+ bmap
->n_ineq
;
381 ncol
= 1 + extra_front
+ pip_var
+ pip_param
+ extra_back
+ 1;
382 M
= pip_matrix_alloc(nrow
, ncol
);
387 for (i
= 0; i
< bmap
->n_eq
; ++i
) {
388 entier_set_si(M
->p
[off
+i
][0], 0);
389 copy_constraint_to(M
->p
[off
+i
], bmap
->eq
[i
],
390 pip_param
, pip_var
, extra_front
, extra_back
);
393 for (i
= 0; i
< bmap
->n_ineq
; ++i
) {
394 entier_set_si(M
->p
[off
+i
][0], 1);
395 copy_constraint_to(M
->p
[off
+i
], bmap
->ineq
[i
],
396 pip_param
, pip_var
, extra_front
, extra_back
);
401 PipMatrix
*isl_basic_set_to_pip(struct isl_basic_set
*bset
, unsigned pip_param
,
402 unsigned extra_front
, unsigned extra_back
)
404 return isl_basic_map_to_pip((struct isl_basic_map
*)bset
,
405 pip_param
, extra_front
, extra_back
);
408 struct isl_map
*isl_pip_basic_map_lexopt(
409 struct isl_basic_map
*bmap
, struct isl_basic_set
*dom
,
410 struct isl_set
**empty
, int max
)
416 PipMatrix
*domain
= NULL
, *context
= NULL
;
417 unsigned nparam
, n_in
, n_out
;
419 bmap
= isl_basic_map_detect_equalities(bmap
);
424 isl_assert(ctx
, isl_basic_map_compatible_domain(bmap
, dom
), goto error
);
425 nparam
= isl_basic_map_n_param(bmap
);
426 n_in
= isl_basic_map_n_in(bmap
);
427 n_out
= isl_basic_map_n_out(bmap
);
429 domain
= isl_basic_map_to_pip(bmap
, nparam
+ n_in
, 0, dom
->n_div
);
432 context
= isl_basic_map_to_pip((struct isl_basic_map
*)dom
, 0, 0, 0);
436 options
= pip_options_init();
437 options
->Simplify
= 1;
438 options
->Maximize
= max
;
439 options
->Urs_unknowns
= -1;
440 options
->Urs_parms
= -1;
441 sol
= pip_solve(domain
, context
, -1, options
);
444 struct isl_basic_set
*copy
;
445 copy
= isl_basic_set_copy(dom
);
446 map
= isl_map_from_quast(ctx
, sol
,
447 isl_space_copy(bmap
->dim
), copy
, empty
);
449 map
= isl_map_empty_like_basic_map(bmap
);
455 if (map
->n
== 0 && empty
) {
456 isl_set_free(*empty
);
457 *empty
= isl_set_from_basic_set(dom
);
459 isl_basic_set_free(dom
);
460 isl_basic_map_free(bmap
);
463 pip_options_free(options
);
464 pip_matrix_free(domain
);
465 pip_matrix_free(context
);
470 pip_matrix_free(domain
);
472 pip_matrix_free(context
);
473 isl_basic_map_free(bmap
);
474 isl_basic_set_free(dom
);