4 #include <cloog/isl/cloog.h>
5 #include <cloog/isl/backend.h>
9 #define ALLOC(type) (type*)malloc(sizeof(type))
10 #define ALLOCN(type,n) (type*)malloc((n)*sizeof(type))
12 CloogConstraintSet
*cloog_constraint_set_from_isl_basic_set(struct isl_basic_set
*bset
)
14 return (CloogConstraintSet
*)bset
;
17 CloogConstraint
*cloog_constraint_from_isl_constraint(struct isl_constraint
*constraint
)
19 return (CloogConstraint
*)constraint
;
23 /******************************************************************************
24 * Memory leaks hunting *
25 ******************************************************************************/
29 void cloog_constraint_set_free(CloogConstraintSet
*constraints
)
31 isl_basic_set_free(&constraints
->bset
);
35 int cloog_constraint_set_contains_level(CloogConstraintSet
*constraints
,
36 int level
, int nb_parameters
)
38 return isl_basic_set_n_dim(&constraints
->bset
) >= level
;
41 struct cloog_isl_dim
{
42 enum isl_dim_type type
;
46 static struct cloog_isl_dim
set_cloog_dim_to_isl_dim(
47 CloogConstraintSet
*bset
, int pos
)
49 enum isl_dim_type types
[] = { isl_dim_set
, isl_dim_div
, isl_dim_param
};
51 struct cloog_isl_dim ci_dim
;
53 for (i
= 0; i
< 3; ++i
) {
54 unsigned dim
= isl_basic_set_dim(&bset
->bset
, types
[i
]);
56 ci_dim
.type
= types
[i
];
65 /* Check if the variable at position level is defined by an
66 * equality. If so, return the row number. Otherwise, return -1.
68 CloogConstraint
*cloog_constraint_set_defining_equality(
69 CloogConstraintSet
*constraints
, int level
)
71 struct isl_constraint
*c
;
72 struct cloog_isl_dim dim
;
73 struct isl_basic_set
*bset
= &constraints
->bset
;
75 dim
= set_cloog_dim_to_isl_dim(constraints
, level
- 1);
76 if (isl_basic_set_has_defining_equality(bset
, dim
.type
, dim
.pos
, &c
))
77 return cloog_constraint_from_isl_constraint(c
);
83 /* Check if the variable (e) at position level is defined by a
84 * pair of inequalities
85 * <a, i> + -m e + <b, p> + k1 >= 0
86 * <-a, i> + m e + <-b, p> + k2 >= 0
87 * with 0 <= k1 + k2 < m
88 * If so return the row number of the upper bound and set *lower
89 * to the row number of the lower bound. If not, return -1.
91 * If the variable at position level occurs in any other constraint,
92 * then we currently return -1. The modulo guard that we would generate
93 * would still be correct, but we would also need to generate
94 * guards corresponding to the other constraints, and this has not
95 * been implemented yet.
97 CloogConstraint
*cloog_constraint_set_defining_inequalities(
98 CloogConstraintSet
*constraints
,
99 int level
, CloogConstraint
**lower
, int nb_par
)
101 struct isl_constraint
*u
;
102 struct isl_constraint
*l
;
103 struct isl_constraint
*c
;
104 struct cloog_isl_dim dim
;
105 struct isl_basic_set
*bset
= &constraints
->bset
;
107 dim
= set_cloog_dim_to_isl_dim(constraints
, level
- 1);
108 if (!isl_basic_set_has_defining_inequalities(bset
, dim
.type
, dim
.pos
,
110 return cloog_constraint_invalid();
111 for (c
= isl_basic_set_first_constraint(isl_basic_set_copy(bset
)); c
;
112 c
= isl_constraint_next(c
)) {
113 if (isl_constraint_is_equal(c
, l
))
115 if (isl_constraint_is_equal(c
, u
))
117 *lower
= cloog_constraint_from_isl_constraint(c
);
118 if (cloog_constraint_involves(*lower
, level
-1)) {
119 isl_constraint_free(l
);
120 isl_constraint_free(u
);
122 isl_constraint_free(c
);
126 *lower
= cloog_constraint_from_isl_constraint(l
);
127 return cloog_constraint_from_isl_constraint(u
);
130 int cloog_constraint_set_total_dimension(CloogConstraintSet
*constraints
)
132 return isl_basic_set_total_dim(&constraints
->bset
);
135 int cloog_constraint_set_n_iterators(CloogConstraintSet
*constraints
, int n_par
)
137 return isl_basic_set_n_dim(&constraints
->bset
);
141 /******************************************************************************
142 * Equalities spreading functions *
143 ******************************************************************************/
146 /* Equalities are stored inside a Matrix data structure called "equal".
147 * This matrix has (nb_scattering + nb_iterators + 1) rows (i.e. total
148 * dimensions + 1, the "+ 1" is because a statement can be included inside an
149 * external loop without iteration domain), and (nb_scattering + nb_iterators +
150 * nb_parameters + 2) columns (all unknowns plus the scalar plus the equality
151 * type). The ith row corresponds to the equality "= 0" for the ith dimension
152 * iterator. The first column gives the equality type (0: no equality, then
153 * EQTYPE_* -see pprint.h-). At each recursion of pprint, if an equality for
154 * the current level is found, the corresponding row is updated. Then the
155 * equality if it exists is used to simplify expressions (e.g. if we have
156 * "i+1" while we know that "i=2", we simplify it in "3"). At the end of
157 * the pprint call, the corresponding row is reset to zero.
160 CloogEqualities
*cloog_equal_alloc(int n
, int nb_levels
, int nb_parameters
)
163 CloogEqualities
*equal
= ALLOC(CloogEqualities
);
165 equal
->total_dim
= nb_levels
- 1 + nb_parameters
;
167 equal
->constraints
= ALLOCN(CloogConstraintSet
*, n
);
168 equal
->types
= ALLOCN(int, n
);
169 for (i
= 0; i
< n
; ++i
) {
170 equal
->constraints
[i
] = NULL
;
171 equal
->types
[i
] = EQTYPE_NONE
;
176 int cloog_equal_total_dimension(CloogEqualities
*equal
)
178 return equal
->total_dim
;
181 void cloog_equal_free(CloogEqualities
*equal
)
185 for (i
= 0; i
< equal
->n
; ++i
)
186 isl_basic_set_free(&equal
->constraints
[i
]->bset
);
187 free(equal
->constraints
);
192 int cloog_equal_count(CloogEqualities
*equal
)
199 * cloog_constraint_equal_type function :
200 * This function returns the type of the equality in the constraint (line) of
201 * (constraints) for the element (level). An equality is 'constant' iff all
202 * other factors are null except the constant one. It is a 'pure item' iff
203 * it is equal or opposite to a single variable or parameter.
204 * Otherwise it is an 'affine expression'.
206 * i = -13 is constant, i = j, j = -M are pure items,
207 * j = 2*M, i = j+1, 2*j = M are affine expressions.
209 * - constraints is the matrix of constraints,
210 * - level is the column number in equal of the element which is 'equal to',
212 static int cloog_constraint_equal_type(CloogConstraint
*cc
, int level
)
216 int type
= EQTYPE_NONE
;
217 struct isl_constraint
*constraint
= &cc
->isl
;
220 isl_constraint_get_constant(constraint
, &c
);
221 if (!isl_int_is_zero(c
))
222 type
= EQTYPE_CONSTANT
;
223 isl_constraint_get_coefficient(constraint
, isl_dim_set
, level
- 1, &c
);
224 if (!isl_int_is_one(c
) && !isl_int_is_negone(c
))
225 type
= EQTYPE_EXAFFINE
;
226 for (i
= 0; i
< isl_constraint_dim(constraint
, isl_dim_param
); ++i
) {
227 isl_constraint_get_coefficient(constraint
, isl_dim_param
, i
, &c
);
228 if (isl_int_is_zero(c
))
230 if ((!isl_int_is_one(c
) && !isl_int_is_negone(c
)) ||
231 type
!= EQTYPE_NONE
) {
232 type
= EQTYPE_EXAFFINE
;
235 type
= EQTYPE_PUREITEM
;
237 for (i
= 0; i
< isl_constraint_dim(constraint
, isl_dim_set
); ++i
) {
240 isl_constraint_get_coefficient(constraint
, isl_dim_set
, i
, &c
);
241 if (isl_int_is_zero(c
))
243 if ((!isl_int_is_one(c
) && !isl_int_is_negone(c
)) ||
244 type
!= EQTYPE_NONE
) {
245 type
= EQTYPE_EXAFFINE
;
248 type
= EQTYPE_PUREITEM
;
250 for (i
= 0; i
< isl_constraint_dim(constraint
, isl_dim_div
); ++i
) {
251 isl_constraint_get_coefficient(constraint
, isl_dim_div
, i
, &c
);
252 if (isl_int_is_zero(c
))
254 if ((!isl_int_is_one(c
) && !isl_int_is_negone(c
)) ||
255 type
!= EQTYPE_NONE
) {
256 type
= EQTYPE_EXAFFINE
;
259 type
= EQTYPE_PUREITEM
;
263 if (type
== EQTYPE_NONE
)
264 type
= EQTYPE_CONSTANT
;
270 int cloog_equal_type(CloogEqualities
*equal
, int level
)
272 return equal
->types
[level
-1];
277 * cloog_equal_add function:
278 * This function updates the row (level-1) of the equality matrix (equal) with
279 * the row that corresponds to the row (line) of the matrix (matrix).
280 * - equal is the matrix of equalities,
281 * - matrix is the matrix of constraints,
282 * - level is the column number in matrix of the element which is 'equal to',
283 * - line is the line number in matrix of the constraint we want to study,
284 * - the infos structure gives the user all options on code printing and more.
286 * line is set to an invalid constraint for equalities that CLooG itself has
287 * discovered because the lower and upper bound of a loop happened to be equal.
288 * This situation shouldn't happen in the isl port since isl should
289 * have found the equality itself.
291 void cloog_equal_add(CloogEqualities
*equal
, CloogConstraintSet
*matrix
,
292 int level
, CloogConstraint
*line
, int nb_par
)
294 struct isl_basic_set
*bset
;
296 assert(cloog_constraint_is_valid(line
));
298 equal
->types
[level
-1] = cloog_constraint_equal_type(line
, level
);
299 bset
= isl_basic_set_from_constraint(isl_constraint_copy(&line
->isl
));
300 nparam
= isl_basic_set_n_param(bset
);
301 bset
= isl_basic_set_extend(bset
, nparam
,
302 equal
->total_dim
- nparam
, 0, 0, 0);
303 bset
= isl_basic_set_finalize(bset
);
304 equal
->constraints
[level
-1] =
305 cloog_constraint_set_from_isl_basic_set(bset
);
310 * cloog_equal_del function :
311 * This function reset the equality corresponding to the iterator (level)
312 * in the equality matrix (equal).
313 * - July 2nd 2002: first version.
315 void cloog_equal_del(CloogEqualities
*equal
, int level
)
317 equal
->types
[level
-1] = EQTYPE_NONE
;
318 isl_basic_set_free(&equal
->constraints
[level
-1]->bset
);
319 equal
->constraints
[level
-1] = NULL
;
324 /******************************************************************************
325 * Processing functions *
326 ******************************************************************************/
329 * Function cloog_constraint_set_normalize:
330 * This function will modify the constraint system in such a way that when
331 * there is an equality depending on the element at level 'level', there are
332 * no more (in)equalities depending on this element.
334 * The simplified form of isl automatically satisfies this condition.
336 void cloog_constraint_set_normalize(CloogConstraintSet
*matrix
, int level
)
343 * cloog_constraint_set_copy function:
344 * this functions builds and returns a "hard copy" (not a pointer copy) of a
345 * CloogConstraintSet data structure.
347 CloogConstraintSet
*cloog_constraint_set_copy(CloogConstraintSet
*constraints
)
349 return cloog_constraint_set_from_isl_basic_set(
350 isl_basic_set_dup(&constraints
->bset
));
355 * cloog_constraint_set_simplify function:
356 * this function simplify all constraints inside the matrix "matrix" thanks to
357 * an equality matrix "equal" that gives for some elements of the affine
358 * constraint an equality with other elements, preferably constants.
359 * For instance, if a row of the matrix contains i+j+3>=0 and the equality
360 * matrix gives i=n and j=2, the constraint is simplified to n+3>=0. The
361 * simplified constraints are returned back inside a new simplified matrix.
362 * - matrix is the set of constraints to simplify,
363 * - equal is the matrix of equalities,
364 * - level is a level we don't want to simplify (-1 if none),
365 * - nb_par is the number of parameters of the program.
367 * isl should have performed these simplifications already in isl_set_gist.
369 CloogConstraintSet
*cloog_constraint_set_simplify(CloogConstraintSet
*matrix
,
370 CloogEqualities
*equal
, int level
, int nb_par
)
372 return cloog_constraint_set_copy(matrix
);
376 static struct cloog_isl_dim
constraint_cloog_dim_to_isl_dim(
377 CloogConstraint
*constraint
, int pos
)
379 enum isl_dim_type types
[] = { isl_dim_set
, isl_dim_div
, isl_dim_param
};
381 struct cloog_isl_dim ci_dim
;
383 for (i
= 0; i
< 3; ++i
) {
384 unsigned dim
= isl_constraint_dim(&constraint
->isl
, types
[i
]);
386 ci_dim
.type
= types
[i
];
395 static struct clast_expr
*div_expr(CloogConstraint
*constraint
, int pos
,
399 unsigned dim
= cloog_constraint_total_dimension(constraint
);
401 struct clast_reduction
*r
;
402 struct clast_expr
*e
= NULL
;
405 div
= isl_constraint_div(&constraint
->isl
, pos
);
408 for (i
= 0, nb_elts
= 0; i
< dim
; ++i
) {
409 struct cloog_isl_dim dim
;
411 dim
= constraint_cloog_dim_to_isl_dim(constraint
, i
);
412 isl_div_get_coefficient(div
, dim
.type
, dim
.pos
, &c
);
413 if (!cloog_int_is_zero(c
))
416 isl_div_get_constant(div
, &c
);
417 if (!cloog_int_is_zero(c
))
420 r
= new_clast_reduction(clast_red_sum
, nb_elts
);
421 for (i
= 0, nb_elts
= 0; i
< dim
; ++i
) {
422 struct clast_expr
*v
;
423 struct cloog_isl_dim dim
;
425 dim
= constraint_cloog_dim_to_isl_dim(constraint
, i
);
426 isl_div_get_coefficient(div
, dim
.type
, dim
.pos
, &c
);
427 if (cloog_int_is_zero(c
))
430 v
= cloog_constraint_variable_expr(constraint
, 1 + i
, names
);
432 r
->elts
[nb_elts
++] = &new_clast_term(c
, v
)->expr
;
434 isl_div_get_constant(div
, &c
);
435 if (!cloog_int_is_zero(c
))
436 r
->elts
[nb_elts
++] = &new_clast_term(c
, NULL
)->expr
;
438 isl_div_get_denominator(div
, &c
);
439 e
= &new_clast_binary(clast_bin_fdiv
, &r
->expr
, c
)->expr
;
449 * Return clast_expr corresponding to the variable "level" (1 based) in
450 * the given constraint.
452 struct clast_expr
*cloog_constraint_variable_expr(CloogConstraint
*constraint
,
453 int level
, CloogNames
*names
)
455 struct cloog_isl_dim dim
;
460 dim
= constraint_cloog_dim_to_isl_dim(constraint
, level
- 1);
461 if (dim
.type
== isl_dim_div
)
462 return div_expr(constraint
, dim
.pos
, names
);
464 if (dim
.type
== isl_dim_set
)
465 name
= cloog_names_name_at_level(names
, level
);
467 name
= names
->parameters
[dim
.pos
];
469 return &new_clast_name(name
)->expr
;
474 * Return true if constraint c involves variable v (zero-based).
476 int cloog_constraint_involves(CloogConstraint
*constraint
, int v
)
482 cloog_constraint_coefficient_get(constraint
, v
, &c
);
483 res
= !isl_int_is_zero(c
);
488 int cloog_constraint_is_lower_bound(CloogConstraint
*constraint
, int v
)
494 cloog_constraint_coefficient_get(constraint
, v
, &c
);
495 res
= isl_int_is_pos(c
);
500 int cloog_constraint_is_upper_bound(CloogConstraint
*constraint
, int v
)
506 cloog_constraint_coefficient_get(constraint
, v
, &c
);
507 res
= isl_int_is_neg(c
);
512 int cloog_constraint_is_equality(CloogConstraint
*constraint
)
514 return isl_constraint_is_equality(&constraint
->isl
);
517 void cloog_constraint_clear(CloogConstraint
*constraint
)
519 isl_constraint_clear(&constraint
->isl
);
522 void cloog_constraint_coefficient_get(CloogConstraint
*constraint
,
523 int var
, cloog_int_t
*val
)
525 struct cloog_isl_dim dim
;
530 dim
= constraint_cloog_dim_to_isl_dim(constraint
, var
);
531 isl_constraint_get_coefficient(&constraint
->isl
, dim
.type
, dim
.pos
, val
);
534 void cloog_constraint_coefficient_set(CloogConstraint
*constraint
,
535 int var
, cloog_int_t val
)
537 struct cloog_isl_dim dim
;
541 dim
= constraint_cloog_dim_to_isl_dim(constraint
, var
);
542 isl_constraint_set_coefficient(&constraint
->isl
, dim
.type
, dim
.pos
, val
);
545 void cloog_constraint_constant_get(CloogConstraint
*constraint
, cloog_int_t
*val
)
547 isl_constraint_get_constant(&constraint
->isl
, val
);
551 * Copy the coefficient of constraint c into dst in PolyLib order,
552 * i.e., first the coefficients of the variables, then the coefficients
553 * of the parameters and finally the constant.
555 void cloog_constraint_copy_coefficients(CloogConstraint
*constraint
,
561 dim
= isl_constraint_dim(&constraint
->isl
, isl_dim_all
);
563 for (i
= 0; i
< dim
; ++i
)
564 cloog_constraint_coefficient_get(constraint
, i
, dst
+i
);
565 cloog_constraint_constant_get(constraint
, dst
+dim
);
568 CloogConstraint
*cloog_constraint_invalid(void)
573 int cloog_constraint_is_valid(CloogConstraint
*constraint
)
575 return constraint
!= NULL
;
578 int cloog_constraint_total_dimension(CloogConstraint
*constraint
)
580 return isl_constraint_dim(&constraint
->isl
, isl_dim_all
);
584 * Create a CloogConstraintSet containing enough information to perform
585 * a reduction on the upper equality (in this case lower is an invalid
586 * CloogConstraint) or the pair of inequalities upper and lower
587 * from within insert_modulo_guard.
588 * In the isl backend, we return a CloogConstraintSet containting both
589 * bounds, as the stride may change during the reduction and we may
590 * need to recompute the bound on the modulo expression.
592 CloogConstraintSet
*cloog_constraint_set_for_reduction(CloogConstraint
*upper
,
593 CloogConstraint
*lower
)
595 struct isl_basic_set
*bset
;
597 bset
= isl_basic_set_from_constraint(isl_constraint_copy(&upper
->isl
));
598 if (cloog_constraint_is_valid(lower
))
599 bset
= isl_basic_set_add_constraint(bset
,
600 isl_constraint_copy(&lower
->isl
));
601 return cloog_constraint_set_from_isl_basic_set(bset
);
605 static int add_constant_term(CloogConstraint
*c
, void *user
)
607 isl_int
*bound
= (isl_int
*)user
;
612 cloog_constraint_constant_get(c
, &v
);
613 isl_int_add(*bound
, *bound
, v
);
621 * Reduce the modulo guard expressed by "contraints" using equalities
622 * found in outer nesting levels (stored in "equal").
623 * The modulo guard may be an equality or a pair of inequalities.
624 * In case of a pair of inequalities, *bound contains the bound on the
625 * corresponding modulo expression. If any reduction is performed
626 * then this bound is recomputed.
628 * We first check if "level" corresponds to an existentially quantified
629 * variable. If so, there is no need to reduce it as it would have
630 * been removed already if it had been redundant.
631 * Then we check if there are any equalities we can use. If not,
632 * there is again nothing to reduce.
633 * For the actual reduction, we use isl_basic_set_gist, but this
634 * function will only perform the reduction we want hear if the
635 * the variable that imposes the modulo constraint has been projected
636 * out (i.e., turned into an existentially quantified variable).
637 * After the call to isl_basic_set_gist, we need to move the
638 * existential variable back into the position where the calling
639 * function expects it (assuming there are any constraints left).
640 * We do this by adding equality between the given dimension and
641 * the existentially quantified variable.
643 CloogConstraintSet
*cloog_constraint_set_reduce(CloogConstraintSet
*constraints
,
644 int level
, CloogEqualities
*equal
, int nb_par
, cloog_int_t
*bound
)
647 struct isl_basic_set
*eq
;
648 struct isl_basic_map
*id
;
649 struct cloog_isl_dim dim
;
650 struct isl_constraint
*c
;
652 unsigned constraints_dim
;
654 struct isl_basic_set
*bset
= &constraints
->bset
;
656 dim
= set_cloog_dim_to_isl_dim(constraints
, level
- 1);
657 if (dim
.type
!= isl_dim_set
)
661 for (j
= 0; j
< level
- 1; ++j
) {
662 if (equal
->types
[j
] != EQTYPE_EXAFFINE
)
665 eq
= isl_basic_set_copy(&equal
->constraints
[j
]->bset
);
667 eq
= isl_basic_set_intersect(eq
,
668 isl_basic_set_copy(&equal
->constraints
[j
]->bset
));
673 id
= isl_basic_map_identity(isl_basic_set_get_dim(bset
));
674 id
= isl_basic_map_remove_dims(id
, isl_dim_out
, dim
.pos
, 1);
675 bset
= isl_basic_set_apply(bset
, isl_basic_map_copy(id
));
676 bset
= isl_basic_set_apply(bset
, isl_basic_map_reverse(id
));
678 constraints_dim
= isl_basic_set_dim(bset
, isl_dim_set
);
679 eq
= isl_basic_set_remove_dims(eq
, isl_dim_set
, constraints_dim
,
680 isl_basic_set_dim(eq
, isl_dim_set
) - constraints_dim
);
681 bset
= isl_basic_set_gist(bset
, eq
);
682 if (isl_basic_set_dim(bset
, isl_dim_div
) != 1)
683 return cloog_constraint_set_from_isl_basic_set(bset
);
685 div
= isl_basic_set_div(isl_basic_set_copy(bset
), 0);
686 c
= isl_equality_alloc(isl_basic_set_get_dim(bset
));
687 c
= isl_constraint_add_div(c
, div
, &pos
);
688 isl_constraint_set_coefficient(c
, isl_dim_set
, dim
.pos
,
690 isl_constraint_set_coefficient(c
, isl_dim_div
, pos
,
692 bset
= isl_basic_set_add_constraint(bset
, c
);
694 isl_int_set_si(*bound
, 0);
695 constraints
= cloog_constraint_set_from_isl_basic_set(bset
);
696 cloog_constraint_set_foreach_constraint(constraints
,
697 add_constant_term
, bound
);
699 return cloog_constraint_set_from_isl_basic_set(bset
);
702 CloogConstraint
*cloog_constraint_copy(CloogConstraint
*constraint
)
704 return cloog_constraint_from_isl_constraint(
705 isl_constraint_copy(&constraint
->isl
));
708 void cloog_constraint_release(CloogConstraint
*constraint
)
710 isl_constraint_free(&constraint
->isl
);
713 struct cloog_isl_foreach
{
714 int (*fn
)(CloogConstraint
*constraint
, void *user
);
718 static int cloog_isl_foreach_cb(__isl_take isl_constraint
*c
, void *user
)
720 struct cloog_isl_foreach
*data
= (struct cloog_isl_foreach
*)user
;
723 if (isl_constraint_is_div_constraint(c
)) {
724 isl_constraint_free(c
);
728 ret
= data
->fn(cloog_constraint_from_isl_constraint(c
), data
->user
);
730 isl_constraint_free(c
);
735 int cloog_constraint_set_foreach_constraint(CloogConstraintSet
*constraints
,
736 int (*fn
)(CloogConstraint
*constraint
, void *user
), void *user
)
738 struct cloog_isl_foreach data
= { fn
, user
};
740 return isl_basic_set_foreach_constraint(&constraints
->bset
,
741 cloog_isl_foreach_cb
, &data
);
744 CloogConstraint
*cloog_equal_constraint(CloogEqualities
*equal
, int j
)
746 return cloog_constraint_from_isl_constraint(
747 isl_basic_set_first_constraint(
748 isl_basic_set_copy(&equal
->constraints
[j
]->bset
)));