4 #include "../include/cloog/cloog.h"
6 #define ALLOC(type) (type*)malloc(sizeof(type))
7 #define ALLOCN(type,n) (type*)malloc((n)*sizeof(type))
10 * CloogInfos structure:
11 * this structure contains all the informations necessary for pretty printing,
12 * they come from the original CloogProgram structure (language, names), from
13 * genereral options (options) or are built only for pretty printing (stride).
14 * This structure is mainly there to reduce the number of function parameters,
15 * since most pprint.c functions need most of its field.
18 CloogState
*state
; /**< State. */
19 cloog_int_t
*stride
; /**< The stride for each iterator. */
20 int nb_scattdims
; /**< Scattering dimension number. */
21 int * scaldims
; /**< Boolean array saying whether a given
22 * scattering dimension is scalar or not.
24 CloogNames
* names
; /**< Names of iterators and parameters. */
25 CloogOptions
* options
; /**< Options on CLooG's behaviour. */
26 CloogEqualities
*equal
; /**< Matrix of equalities. */
29 typedef struct clooginfos CloogInfos
;
31 static int clast_expr_cmp(struct clast_expr
*e1
, struct clast_expr
*e2
);
32 static int clast_term_cmp(struct clast_term
*t1
, struct clast_term
*t2
);
33 static int clast_binary_cmp(struct clast_binary
*b1
, struct clast_binary
*b2
);
34 static int clast_reduction_cmp(struct clast_reduction
*r1
,
35 struct clast_reduction
*r2
);
37 static int clast_equal_add(CloogEqualities
*equal
,
38 CloogConstraintSet
*constraints
,
39 int level
, CloogConstraint constraint
,
42 static struct clast_stmt
*clast_equal(int level
, CloogInfos
*infos
);
43 static struct clast_expr
*clast_minmax(CloogConstraintSet
*constraints
,
44 int level
, int max
, int guard
,
46 static void insert_guard(CloogConstraintSet
*constraints
, int level
,
47 struct clast_stmt
***next
, CloogInfos
*infos
);
48 static void insert_modulo_guard(CloogConstraint upper
,
49 CloogConstraint lower
, int level
,
50 struct clast_stmt
***next
, CloogInfos
*infos
);
51 static void insert_equation(CloogConstraint upper
, CloogConstraint lower
,
52 int level
, struct clast_stmt
***next
, CloogInfos
*infos
);
53 static int insert_for(CloogConstraintSet
*constraints
, int level
,
54 struct clast_stmt
***next
, CloogInfos
*infos
);
55 static void insert_block(CloogBlock
*block
, int level
,
56 struct clast_stmt
***next
, CloogInfos
*infos
);
57 static void insert_loop(CloogLoop
* loop
, int level
,
58 struct clast_stmt
***next
, CloogInfos
*infos
);
61 struct clast_name
*new_clast_name(const char *name
)
63 struct clast_name
*n
= malloc(sizeof(struct clast_name
));
64 n
->expr
.type
= clast_expr_name
;
69 struct clast_term
*new_clast_term(cloog_int_t c
, struct clast_expr
*v
)
71 struct clast_term
*t
= malloc(sizeof(struct clast_term
));
72 t
->expr
.type
= clast_expr_term
;
73 cloog_int_init(t
->val
);
74 cloog_int_set(t
->val
, c
);
79 struct clast_binary
*new_clast_binary(enum clast_bin_type t
,
80 struct clast_expr
*lhs
, cloog_int_t rhs
)
82 struct clast_binary
*b
= malloc(sizeof(struct clast_binary
));
83 b
->expr
.type
= clast_expr_bin
;
86 cloog_int_init(b
->RHS
);
87 cloog_int_set(b
->RHS
, rhs
);
91 struct clast_reduction
*new_clast_reduction(enum clast_red_type t
, int n
)
94 struct clast_reduction
*r
;
95 r
= malloc(sizeof(struct clast_reduction
)+(n
-1)*sizeof(struct clast_expr
*));
96 r
->expr
.type
= clast_expr_red
;
99 for (i
= 0; i
< n
; ++i
)
104 static void free_clast_root(struct clast_stmt
*s
);
106 const struct clast_stmt_op stmt_root
= { free_clast_root
};
108 static void free_clast_root(struct clast_stmt
*s
)
110 struct clast_root
*r
= (struct clast_root
*)s
;
111 assert(CLAST_STMT_IS_A(s
, stmt_root
));
112 cloog_names_free(r
->names
);
116 struct clast_root
*new_clast_root(CloogNames
*names
)
118 struct clast_root
*r
= malloc(sizeof(struct clast_root
));
119 r
->stmt
.op
= &stmt_root
;
121 r
->names
= cloog_names_copy(names
);
125 static void free_clast_assignment(struct clast_stmt
*s
);
127 const struct clast_stmt_op stmt_ass
= { free_clast_assignment
};
129 static void free_clast_assignment(struct clast_stmt
*s
)
131 struct clast_assignment
*a
= (struct clast_assignment
*)s
;
132 assert(CLAST_STMT_IS_A(s
, stmt_ass
));
133 free_clast_expr(a
->RHS
);
137 struct clast_assignment
*new_clast_assignment(const char *lhs
,
138 struct clast_expr
*rhs
)
140 struct clast_assignment
*a
= malloc(sizeof(struct clast_assignment
));
141 a
->stmt
.op
= &stmt_ass
;
148 static void free_clast_user_stmt(struct clast_stmt
*s
);
150 const struct clast_stmt_op stmt_user
= { free_clast_user_stmt
};
152 static void free_clast_user_stmt(struct clast_stmt
*s
)
154 struct clast_user_stmt
*u
= (struct clast_user_stmt
*)s
;
155 assert(CLAST_STMT_IS_A(s
, stmt_user
));
156 cloog_statement_free(u
->statement
);
157 cloog_clast_free(u
->substitutions
);
161 struct clast_user_stmt
*new_clast_user_stmt(CloogStatement
*stmt
,
162 struct clast_stmt
*subs
)
164 struct clast_user_stmt
*u
= malloc(sizeof(struct clast_user_stmt
));
165 u
->stmt
.op
= &stmt_user
;
167 u
->statement
= cloog_statement_copy(stmt
);
168 u
->substitutions
= subs
;
172 static void free_clast_block(struct clast_stmt
*b
);
174 const struct clast_stmt_op stmt_block
= { free_clast_block
};
176 static void free_clast_block(struct clast_stmt
*s
)
178 struct clast_block
*b
= (struct clast_block
*)s
;
179 assert(CLAST_STMT_IS_A(s
, stmt_block
));
180 cloog_clast_free(b
->body
);
184 struct clast_block
*new_clast_block()
186 struct clast_block
*b
= malloc(sizeof(struct clast_block
));
187 b
->stmt
.op
= &stmt_block
;
193 static void free_clast_for(struct clast_stmt
*s
);
195 const struct clast_stmt_op stmt_for
= { free_clast_for
};
197 static void free_clast_for(struct clast_stmt
*s
)
199 struct clast_for
*f
= (struct clast_for
*)s
;
200 assert(CLAST_STMT_IS_A(s
, stmt_for
));
201 free_clast_expr(f
->LB
);
202 free_clast_expr(f
->UB
);
203 cloog_int_clear(f
->stride
);
204 cloog_clast_free(f
->body
);
208 struct clast_for
*new_clast_for(const char *it
, struct clast_expr
*LB
,
209 struct clast_expr
*UB
, cloog_int_t stride
)
211 struct clast_for
*f
= malloc(sizeof(struct clast_for
));
212 f
->stmt
.op
= &stmt_for
;
218 cloog_int_init(f
->stride
);
219 cloog_int_set(f
->stride
, stride
);
223 static void free_clast_guard(struct clast_stmt
*s
);
225 const struct clast_stmt_op stmt_guard
= { free_clast_guard
};
227 static void free_clast_guard(struct clast_stmt
*s
)
230 struct clast_guard
*g
= (struct clast_guard
*)s
;
231 assert(CLAST_STMT_IS_A(s
, stmt_guard
));
232 cloog_clast_free(g
->then
);
233 for (i
= 0; i
< g
->n
; ++i
) {
234 free_clast_expr(g
->eq
[i
].LHS
);
235 free_clast_expr(g
->eq
[i
].RHS
);
240 struct clast_guard
*new_clast_guard(int n
)
243 struct clast_guard
*g
= malloc(sizeof(struct clast_guard
) +
244 (n
-1) * sizeof(struct clast_equation
));
245 g
->stmt
.op
= &stmt_guard
;
249 for (i
= 0; i
< n
; ++i
) {
256 void free_clast_name(struct clast_name
*n
)
261 void free_clast_term(struct clast_term
*t
)
263 cloog_int_clear(t
->val
);
264 free_clast_expr(t
->var
);
268 void free_clast_binary(struct clast_binary
*b
)
270 cloog_int_clear(b
->RHS
);
271 free_clast_expr(b
->LHS
);
275 void free_clast_reduction(struct clast_reduction
*r
)
278 for (i
= 0; i
< r
->n
; ++i
)
279 free_clast_expr(r
->elts
[i
]);
283 void free_clast_expr(struct clast_expr
*e
)
288 case clast_expr_name
:
289 free_clast_name((struct clast_name
*) e
);
291 case clast_expr_term
:
292 free_clast_term((struct clast_term
*) e
);
295 free_clast_reduction((struct clast_reduction
*) e
);
298 free_clast_binary((struct clast_binary
*) e
);
305 void free_clast_stmt(struct clast_stmt
*s
)
312 void cloog_clast_free(struct clast_stmt
*s
)
314 struct clast_stmt
*next
;
322 static int clast_name_cmp(struct clast_name
*n1
, struct clast_name
*n2
)
324 return n1
->name
== n2
->name
? 0 : strcmp(n1
->name
, n2
->name
);
327 static int clast_term_cmp(struct clast_term
*t1
, struct clast_term
*t2
)
330 if (!t1
->var
&& t2
->var
)
332 if (t1
->var
&& !t2
->var
)
334 c
= clast_expr_cmp(t1
->var
, t2
->var
);
337 return cloog_int_cmp(t1
->val
, t2
->val
);
340 static int clast_binary_cmp(struct clast_binary
*b1
, struct clast_binary
*b2
)
344 if (b1
->type
!= b2
->type
)
345 return b1
->type
- b2
->type
;
346 if ((c
= cloog_int_cmp(b1
->RHS
, b2
->RHS
)))
348 return clast_expr_cmp(b1
->LHS
, b2
->LHS
);
351 static int clast_reduction_cmp(struct clast_reduction
*r1
, struct clast_reduction
*r2
)
356 if (r1
->n
== 1 && r2
->n
== 1)
357 return clast_expr_cmp(r1
->elts
[0], r2
->elts
[0]);
358 if (r1
->type
!= r2
->type
)
359 return r1
->type
- r2
->type
;
361 return r1
->n
- r2
->n
;
362 for (i
= 0; i
< r1
->n
; ++i
)
363 if ((c
= clast_expr_cmp(r1
->elts
[i
], r2
->elts
[i
])))
368 static int clast_expr_cmp(struct clast_expr
*e1
, struct clast_expr
*e2
)
376 if (e1
->type
!= e2
->type
)
377 return e1
->type
- e2
->type
;
379 case clast_expr_name
:
380 return clast_name_cmp((struct clast_name
*) e1
,
381 (struct clast_name
*) e2
);
382 case clast_expr_term
:
383 return clast_term_cmp((struct clast_term
*) e1
,
384 (struct clast_term
*) e2
);
386 return clast_binary_cmp((struct clast_binary
*) e1
,
387 (struct clast_binary
*) e2
);
389 return clast_reduction_cmp((struct clast_reduction
*) e1
,
390 (struct clast_reduction
*) e2
);
396 int clast_expr_equal(struct clast_expr
*e1
, struct clast_expr
*e2
)
398 return clast_expr_cmp(e1
, e2
) == 0;
402 * Return 1 is both expressions are constant terms and e1 is bigger than e2.
404 int clast_expr_is_bigger_constant(struct clast_expr
*e1
, struct clast_expr
*e2
)
406 struct clast_term
*t1
, *t2
;
407 struct clast_reduction
*r
;
411 if (e1
->type
== clast_expr_red
) {
412 r
= (struct clast_reduction
*)e1
;
413 return r
->n
== 1 && clast_expr_is_bigger_constant(r
->elts
[0], e2
);
415 if (e2
->type
== clast_expr_red
) {
416 r
= (struct clast_reduction
*)e2
;
417 return r
->n
== 1 && clast_expr_is_bigger_constant(e1
, r
->elts
[0]);
419 if (e1
->type
!= clast_expr_term
|| e2
->type
!= clast_expr_term
)
421 t1
= (struct clast_term
*)e1
;
422 t2
= (struct clast_term
*)e2
;
423 if (t1
->var
|| t2
->var
)
425 return cloog_int_gt(t1
->val
, t2
->val
);
428 static int qsort_expr_cmp(const void *p1
, const void *p2
)
430 return clast_expr_cmp(*(struct clast_expr
**)p1
, *(struct clast_expr
**)p2
);
433 static void clast_reduction_sort(struct clast_reduction
*r
)
435 qsort(&r
->elts
[0], r
->n
, sizeof(struct clast_expr
*), qsort_expr_cmp
);
438 static int qsort_eq_cmp(const void *p1
, const void *p2
)
440 struct clast_equation
*eq1
= (struct clast_equation
*)p1
;
441 struct clast_equation
*eq2
= (struct clast_equation
*)p2
;
444 cmp
= clast_expr_cmp(eq1
->LHS
, eq2
->LHS
);
448 cmp
= clast_expr_cmp(eq1
->RHS
, eq2
->RHS
);
452 return eq1
->sign
- eq2
->sign
;
456 * Sort equations in a clast_guard.
458 static void clast_guard_sort(struct clast_guard
*g
)
460 qsort(&g
->eq
[0], g
->n
, sizeof(struct clast_equation
), qsort_eq_cmp
);
464 /******************************************************************************
465 * Equalities spreading functions *
466 ******************************************************************************/
470 * clast_equal_allow function:
471 * This function checks whether the options allow us to spread the equality or
472 * not. It returns 1 if so, 0 otherwise.
473 * - equal is the matrix of equalities,
474 * - level is the column number in equal of the element which is 'equal to',
475 * - line is the line number in equal of the constraint we want to study,
476 * - the infos structure gives the user all options on code printing and more.
478 * - October 27th 2005: first version (extracted from old pprint_equal_add).
480 static int clast_equal_allow(CloogEqualities
*equal
, int level
, int line
,
483 if (level
< infos
->options
->fsp
)
486 if ((cloog_equal_type(equal
, level
) == EQTYPE_EXAFFINE
) &&
487 !infos
->options
->esp
)
495 * clast_equal_add function:
496 * This function updates the row (level-1) of the equality matrix (equal) with
497 * the row that corresponds to the row (line) of the matrix (matrix). It returns
498 * 1 if the row can be updated, 0 otherwise.
499 * - equal is the matrix of equalities,
500 * - matrix is the matrix of constraints,
501 * - level is the column number in matrix of the element which is 'equal to',
502 * - line is the line number in matrix of the constraint we want to study,
503 * - the infos structure gives the user all options on code printing and more.
505 static int clast_equal_add(CloogEqualities
*equal
,
506 CloogConstraintSet
*constraints
,
507 int level
, CloogConstraint constraint
,
510 cloog_equal_add(equal
, constraints
, level
, constraint
,
511 infos
->names
->nb_parameters
);
513 return clast_equal_allow(equal
, level
, level
-1, infos
);
519 * clast_equal function:
520 * This function prints the substitution data of a statement into a clast_stmt.
521 * Using this function instead of pprint_equal is useful for generating
522 * a compilable pseudo-code by using preprocessor macro for each statement.
523 * By opposition to pprint_equal, the result is less human-readable. For
524 * instance this function will print (i,i+3,k,3) where pprint_equal would
525 * return (j=i+3,l=3).
526 * - level is the number of loops enclosing the statement,
527 * - the infos structure gives the user all options on code printing and more.
529 * - March 12th 2004: first version.
530 * - November 21th 2005: (debug) now works well with GMP version.
532 static struct clast_stmt
*clast_equal(int level
, CloogInfos
*infos
)
535 struct clast_expr
*e
;
536 struct clast_stmt
*a
= NULL
;
537 struct clast_stmt
**next
= &a
;
538 CloogEqualities
*equal
= infos
->equal
;
539 CloogConstraint equal_constraint
;
541 for (i
=infos
->names
->nb_scattering
;i
<level
-1;i
++)
542 { if (cloog_equal_type(equal
, i
+1)) {
543 equal_constraint
= cloog_equal_constraint(equal
, i
);
544 e
= clast_bound_from_constraint(equal_constraint
, i
+1, infos
->names
);
545 cloog_constraint_release(equal_constraint
);
547 e
= &new_clast_term(infos
->state
->one
, &new_clast_name(
548 cloog_names_name_at_level(infos
->names
, i
+1))->expr
)->expr
;
550 *next
= &new_clast_assignment(NULL
, e
)->stmt
;
551 next
= &(*next
)->next
;
559 * clast_bound_from_constraint function:
560 * This function returns a clast_expr containing the printing of the
561 * 'right part' of a constraint according to an element.
562 * For instance, for the constraint -3*i + 2*j - M >=0 and the element j,
563 * we have j >= (3*i + M)/2. As we are looking for integral solutions, this
564 * function should return 'ceild(3*i+M,2)'.
565 * - matrix is the polyhedron containing all the constraints,
566 * - line_num is the line number in domain of the constraint we want to print,
567 * - level is the column number in domain of the element we want to use,
568 * - names structure gives the user some options about code printing,
569 * the number of parameters in domain (nb_par), and the arrays of iterator
570 * names and parameters (iters and params).
572 * - November 2nd 2001: first version.
573 * - June 27th 2003: 64 bits version ready.
575 struct clast_expr
*clast_bound_from_constraint(CloogConstraint constraint
,
576 int level
, CloogNames
*names
)
578 int i
, sign
, nb_elts
=0, len
;
579 cloog_int_t
*line
, numerator
, denominator
, temp
, division
;
580 struct clast_expr
*e
= NULL
;
581 struct cloog_vec
*line_vector
;
583 len
= cloog_constraint_total_dimension(constraint
) + 2;
584 line_vector
= cloog_vec_alloc(len
);
585 line
= line_vector
->p
;
586 cloog_constraint_copy_coefficients(constraint
, line
+1);
587 cloog_int_init(temp
);
588 cloog_int_init(numerator
);
589 cloog_int_init(denominator
);
591 if (!cloog_int_is_zero(line
[level
])) {
592 struct clast_reduction
*r
;
593 /* Maybe we need to invert signs in such a way that the element sign is>0.*/
594 sign
= -cloog_int_sgn(line
[level
]);
596 for (i
= 1, nb_elts
= 0; i
<= len
- 1; ++i
)
597 if (i
!= level
&& !cloog_int_is_zero(line
[i
]))
599 r
= new_clast_reduction(clast_red_sum
, nb_elts
);
602 /* First, we have to print the iterators and the parameters. */
603 for (i
= 1; i
<= len
- 2; i
++) {
604 struct clast_expr
*v
;
606 if (i
== level
|| cloog_int_is_zero(line
[i
]))
609 v
= cloog_constraint_variable_expr(constraint
, i
, names
);
612 cloog_int_neg(temp
,line
[i
]);
614 cloog_int_set(temp
,line
[i
]);
616 r
->elts
[nb_elts
++] = &new_clast_term(temp
, v
)->expr
;
620 cloog_int_neg(numerator
, line
[len
- 1]);
621 cloog_int_set(denominator
, line
[level
]);
624 cloog_int_set(numerator
, line
[len
- 1]);
625 cloog_int_neg(denominator
, line
[level
]);
628 /* Finally, the constant, and the final printing. */
630 if (!cloog_int_is_zero(numerator
))
631 r
->elts
[nb_elts
++] = &new_clast_term(numerator
, NULL
)->expr
;
633 if (!cloog_int_is_one(line
[level
]) && !cloog_int_is_neg_one(line
[level
]))
634 { if (!cloog_constraint_is_equality(constraint
))
635 { if (cloog_int_is_pos(line
[level
]))
636 e
= &new_clast_binary(clast_bin_cdiv
, &r
->expr
, denominator
)->expr
;
638 e
= &new_clast_binary(clast_bin_fdiv
, &r
->expr
, denominator
)->expr
;
640 e
= &new_clast_binary(clast_bin_div
, &r
->expr
, denominator
)->expr
;
645 free_clast_reduction(r
);
646 if (cloog_int_is_zero(numerator
))
647 e
= &new_clast_term(numerator
, NULL
)->expr
;
649 { if (!cloog_int_is_one(denominator
))
650 { if (!cloog_constraint_is_equality(constraint
)) { /* useful? */
651 if (cloog_int_is_divisible_by(numerator
, denominator
)) {
652 cloog_int_divexact(temp
, numerator
, denominator
);
653 e
= &new_clast_term(temp
, NULL
)->expr
;
656 cloog_int_init(division
);
657 cloog_int_tdiv_q(division
, numerator
, denominator
);
658 if (cloog_int_is_neg(numerator
)) {
659 if (cloog_int_is_pos(line
[level
])) {
661 e
= &new_clast_term(division
, NULL
)->expr
;
664 cloog_int_sub_ui(temp
, division
, 1);
665 e
= &new_clast_term(temp
, NULL
)->expr
;
669 { if (cloog_int_is_pos(line
[level
]))
670 { /* nb>0 need max */
671 cloog_int_add_ui(temp
, division
, 1);
672 e
= &new_clast_term(temp
, NULL
)->expr
;
676 e
= &new_clast_term(division
, NULL
)->expr
;
678 cloog_int_clear(division
);
682 e
= &new_clast_binary(clast_bin_div
,
683 &new_clast_term(numerator
, NULL
)->expr
,
687 e
= &new_clast_term(numerator
, NULL
)->expr
;
692 cloog_vec_free(line_vector
);
694 cloog_int_clear(temp
);
695 cloog_int_clear(numerator
);
696 cloog_int_clear(denominator
);
703 * clast_minmax function:
704 * This function returns a clast_expr containing the printing of a minimum or a
705 * maximum of the 'right parts' of all constraints according to an element.
706 * For instance consider the constraints:
710 * if we are looking for the minimum for the element j, the function should
711 * return 'max(ceild(3*i+M,2),-2*i)'.
712 * - constraints is the constraints,
713 * - level is the column number in domain of the element we want to use,
714 * - max is a boolean set to 1 if we are looking for a maximum, 0 for a minimum,
715 * - guard is set to 0 if there is no guard, and set to the level of the element
716 * with a guard otherwise (then the function gives the max or the min only
717 * for the constraint where the guarded coefficient is 0),
718 * - the infos structure gives the user some options about code printing,
719 * the number of parameters in domain (nb_par), and the arrays of iterator
720 * names and parameters (iters and params).
722 * - November 2nd 2001: first version.
724 static struct clast_expr
*clast_minmax(CloogConstraintSet
*constraints
,
725 int level
, int max
, int guard
,
728 struct clast_reduction
*r
;
729 CloogConstraint constraint
;
732 for (constraint
= cloog_constraint_first(constraints
);
733 cloog_constraint_is_valid(constraint
);
734 constraint
= cloog_constraint_next(constraint
))
735 if (((max
&& cloog_constraint_is_lower_bound(constraint
, level
-1)) ||
736 (!max
&& cloog_constraint_is_upper_bound(constraint
, level
-1))) &&
737 (!guard
|| !cloog_constraint_involves(constraint
, guard
-1)) &&
738 (!cloog_constraint_is_equality(constraint
)))
742 r
= new_clast_reduction(max
? clast_red_max
: clast_red_min
, n
);
745 for (constraint
= cloog_constraint_first(constraints
);
746 cloog_constraint_is_valid(constraint
);
747 constraint
= cloog_constraint_next(constraint
))
748 if (((max
&& cloog_constraint_is_lower_bound(constraint
, level
-1)) ||
749 (!max
&& cloog_constraint_is_upper_bound(constraint
, level
-1))) &&
750 (!guard
|| !cloog_constraint_involves(constraint
, guard
-1)) &&
751 (!cloog_constraint_is_equality(constraint
)))
752 r
->elts
[n
++] = clast_bound_from_constraint(constraint
, level
,
755 clast_reduction_sort(r
);
761 * Insert modulo guards defined by existentially quantified dimensions,
762 * not involving the given level.
764 * This function is called from within insert_guard or insert_for.
765 * Any constraint used in constructing * a modulo guard is removed
766 * from the constraint set to avoid insert_guard or insert_for
767 * adding a duplicate (pair of) constraint(s).
769 static void insert_extra_modulo_guards(CloogConstraintSet
*constraints
,
770 int level
, struct clast_stmt
***next
, CloogInfos
*infos
)
775 CloogConstraint upper
, lower
;
777 total_dim
= cloog_constraint_set_total_dimension(constraints
);
778 nb_iter
= cloog_constraint_set_n_iterators(constraints
,
779 infos
->names
->nb_parameters
);
781 for (i
= total_dim
- infos
->names
->nb_parameters
; i
>= nb_iter
+ 1; i
--) {
782 if (cloog_constraint_is_valid(upper
=
783 cloog_constraint_set_defining_equality(constraints
, i
))) {
784 if (!level
|| (nb_iter
< level
) ||
785 !cloog_constraint_involves(upper
, level
-1)) {
786 insert_modulo_guard(upper
,
787 cloog_constraint_invalid(), i
, next
, infos
);
788 cloog_constraint_clear(upper
);
790 cloog_constraint_release(upper
);
791 } else if (cloog_constraint_is_valid(upper
=
792 cloog_constraint_set_defining_inequalities(constraints
,
793 i
, &lower
, infos
->names
->nb_parameters
))) {
794 if (!level
|| (nb_iter
< level
) ||
795 !cloog_constraint_involves(upper
, level
-1)) {
796 insert_modulo_guard(upper
, lower
, i
, next
, infos
);
797 cloog_constraint_clear(upper
);
798 cloog_constraint_clear(lower
);
800 cloog_constraint_release(upper
);
801 cloog_constraint_release(lower
);
808 * insert_guard function:
809 * This function inserts a guard in the clast.
810 * A guard on an element (level) is :
811 * -> the conjunction of all the existing constraints where the coefficient of
812 * this element is 0 if the element is an iterator,
813 * -> the conjunction of all the existing constraints if the element isn't an
815 * For instance, considering these constraints and the element j:
818 * this function should return 'if (2*i+M>=0) {'.
819 * - matrix is the polyhedron containing all the constraints,
820 * - level is the column number of the element in matrix we want to use,
821 * - the infos structure gives the user some options about code printing,
822 * the number of parameters in matrix (nb_par), and the arrays of iterator
823 * names and parameters (iters and params).
825 * - November 3rd 2001: first version.
826 * - November 14th 2001: a lot of 'purifications'.
827 * - July 31th 2002: (debug) some guard parts are no more redundants.
828 * - August 12th 2002: polyhedra union ('or' conditions) are now supported.
829 * - October 27th 2005: polyhedra union ('or' conditions) are no more supported
830 * (the need came from loop_simplify that may result in
831 * domain unions, now it should be fixed directly in
832 * cloog_loop_simplify).
834 static void insert_guard(CloogConstraintSet
*constraints
, int level
,
835 struct clast_stmt
***next
, CloogInfos
*infos
)
837 int i
, guarded
, minmax
=-1, nb_and
= 0, nb_iter
;
839 CloogConstraintSet
*copy
;
840 CloogConstraint j
, l
;
841 struct clast_guard
*g
;
843 if (constraints
== NULL
)
846 copy
= cloog_constraint_set_copy(constraints
);
848 insert_extra_modulo_guards(copy
, level
, next
, infos
);
850 total_dim
= cloog_constraint_set_total_dimension(constraints
);
851 g
= new_clast_guard(2 * total_dim
);
853 /* Well, it looks complicated because I wanted to have a particular, more
854 * readable, ordering, obviously this function may be far much simpler !
856 nb_iter
= cloog_constraint_set_n_iterators(constraints
,
857 infos
->names
->nb_parameters
);
860 /* We search for guard parts. */
861 for (i
= 1; i
<= total_dim
; i
++)
862 for (j
= cloog_constraint_first(copy
); cloog_constraint_is_valid(j
);
863 j
= cloog_constraint_next(j
))
864 if (cloog_constraint_involves(j
, i
-1) &&
865 (!level
|| (nb_iter
< level
) ||
866 !cloog_constraint_involves(j
, level
-1))) {
867 struct clast_expr
*v
;
868 struct clast_term
*t
;
870 v
= cloog_constraint_variable_expr(j
, i
, infos
->names
);
871 g
->eq
[nb_and
].LHS
= &(t
= new_clast_term(infos
->state
->one
, v
))->expr
;
872 if (!level
|| cloog_constraint_is_equality(j
)) {
873 /* put the "denominator" in the LHS */
874 cloog_constraint_coefficient_get(j
, i
-1, &t
->val
);
875 cloog_constraint_coefficient_set(j
, i
-1, infos
->state
->one
);
876 if (cloog_int_is_neg(t
->val
)) {
877 cloog_int_neg(t
->val
, t
->val
);
878 cloog_constraint_coefficient_set(j
, i
-1, infos
->state
->negone
);
880 if (level
|| cloog_constraint_is_equality(j
))
881 g
->eq
[nb_and
].sign
= 0;
882 else if (cloog_constraint_is_lower_bound(j
, i
-1))
883 g
->eq
[nb_and
].sign
= 1;
885 g
->eq
[nb_and
].sign
= -1;
886 g
->eq
[nb_and
].RHS
= clast_bound_from_constraint(j
, i
, infos
->names
);
888 if (cloog_constraint_is_lower_bound(j
, i
-1)) {
890 g
->eq
[nb_and
].sign
= 1;
893 g
->eq
[nb_and
].sign
= -1;
896 guarded
= (nb_iter
>= level
) ? level
: 0 ;
897 g
->eq
[nb_and
].RHS
= clast_minmax(copy
,i
,minmax
,guarded
,infos
) ;
901 /* 'elimination' of the current constraint, this avoid to use one
902 * constraint more than once. The current line is always eliminated,
903 * and the next lines if they are in a min or a max.
905 cloog_constraint_clear(j
);
909 l
= cloog_constraint_copy(j
);
910 for (l
= cloog_constraint_next(l
); cloog_constraint_is_valid(l
);
911 l
= cloog_constraint_next(l
))
912 if (((minmax
== 1) && cloog_constraint_is_lower_bound(l
, i
-1)) ||
913 ((minmax
== 0) && cloog_constraint_is_upper_bound(l
, i
-1)))
914 cloog_constraint_clear(l
);
916 cloog_constraint_set_free(copy
);
924 free_clast_stmt(&g
->stmt
);
931 * insert_modulo_guard:
932 * This function inserts a modulo guard corresponding to an equality
933 * or a pair of inequalities.
934 * See insert_equation.
935 * - matrix is the polyhedron containing all the constraints,
936 * - upper and lower are the line numbers of the constraint in matrix
937 * we want to print; in particular, if we want to print an equality,
938 * then lower == -1 and upper is the row of the equality; if we want
939 * to print an inequality, then upper is the row of the upper bound
940 * and lower in the row of the lower bound
941 * - level is the column number of the element in matrix we want to use,
942 * - the infos structure gives the user some options about code printing,
943 * the number of parameters in matrix (nb_par), and the arrays of iterator
944 * names and parameters (iters and params).
946 static void insert_modulo_guard(CloogConstraint upper
,
947 CloogConstraint lower
, int level
,
948 struct clast_stmt
***next
, CloogInfos
*infos
)
950 int i
, nb_elts
= 0, len
, len2
, nb_iter
, in_stride
= 0, nb_par
;
951 struct cloog_vec
*line_vector
;
952 cloog_int_t
*line
, val
, bound
;
953 CloogConstraintSet
*set
;
956 cloog_constraint_coefficient_get(upper
, level
-1, &val
);
957 if (cloog_int_is_one(val
) || cloog_int_is_neg_one(val
)) {
958 cloog_int_clear(val
);
962 len
= cloog_constraint_total_dimension(upper
) + 2;
963 len2
= cloog_equal_total_dimension(infos
->equal
) + 2;
964 nb_par
= infos
->names
->nb_parameters
;
965 nb_iter
= len
- 2 - nb_par
;
967 cloog_int_init(bound
);
968 /* Check if would be emitting the redundant constraint mod(e,m) <= m-1 */
969 if (cloog_constraint_is_valid(lower
)) {
970 cloog_constraint_constant_get(upper
, &val
);
971 cloog_constraint_constant_get(lower
, &bound
);
972 cloog_int_add(bound
, val
, bound
);
973 cloog_constraint_coefficient_get(lower
, level
-1, &val
);
974 cloog_int_sub_ui(val
, val
, 1);
975 if (cloog_int_eq(val
, bound
)) {
976 cloog_int_clear(val
);
977 cloog_int_clear(bound
);
982 set
= cloog_constraint_set_for_reduction(upper
, lower
);
983 set
= cloog_constraint_set_reduce(set
, level
, infos
->equal
, nb_par
, &bound
);
984 upper
= cloog_constraint_first(set
);
985 if (!cloog_constraint_is_valid(upper
)) {
986 cloog_int_clear(val
);
987 cloog_int_clear(bound
);
988 cloog_constraint_set_free(set
);
992 line_vector
= cloog_vec_alloc(len
);
993 line
= line_vector
->p
;
994 cloog_constraint_copy_coefficients(upper
, line
+1);
995 if (cloog_int_is_pos(line
[level
]))
996 cloog_seq_neg(line
+1, line
+1, len
-1);
997 cloog_int_neg(line
[level
], line
[level
]);
998 assert(cloog_int_is_pos(line
[level
]));
1001 for (i
= 1; i
<= len
-1; ++i
) {
1004 cloog_int_fdiv_r(line
[i
], line
[i
], line
[level
]);
1005 if (cloog_int_is_zero(line
[i
]))
1010 /* We need to know if an element of the equality has not to be printed
1011 * because of a stride that guarantees that this element can be divided by
1012 * the current coefficient. Because when there is a constant element, it
1013 * is included in the stride calculation (more exactly in the strided
1014 * iterator new lower bound: the 'offset') and we have not to print it.
1016 if (i
<= nb_iter
&& !cloog_constraint_is_valid(lower
) &&
1017 cloog_int_is_divisible_by(infos
->stride
[i
-1], line
[level
])) {
1025 if (nb_elts
|| (!cloog_int_is_zero(line
[len
-1]) && (!in_stride
))) {
1026 struct clast_reduction
*r
;
1027 struct clast_expr
*e
;
1028 struct clast_guard
*g
;
1031 r
= new_clast_reduction(clast_red_sum
, nb_elts
+1);
1034 /* First, the modulo guard : the iterators... */
1035 for (i
=1;i
<=nb_iter
;i
++) {
1036 if (i
== level
|| cloog_int_is_zero(line
[i
]))
1038 if (cloog_int_is_divisible_by(infos
->stride
[i
-1], line
[level
]))
1041 name
= cloog_names_name_at_level(infos
->names
, i
);
1043 r
->elts
[nb_elts
++] = &new_clast_term(line
[i
],
1044 &new_clast_name(name
)->expr
)->expr
;
1047 /* ...the parameters... */
1048 for (i
=nb_iter
+1;i
<=len
-2;i
++) {
1049 if (cloog_int_is_zero(line
[i
]))
1052 name
= infos
->names
->parameters
[i
-nb_iter
-1] ;
1053 r
->elts
[nb_elts
++] = &new_clast_term(line
[i
],
1054 &new_clast_name(name
)->expr
)->expr
;
1057 /* ...the constant. */
1058 if (!cloog_int_is_zero(line
[len
-1]))
1059 r
->elts
[nb_elts
++] = &new_clast_term(line
[len
-1], NULL
)->expr
;
1061 /* our initial computation may have been an overestimate */
1064 e
= &new_clast_binary(clast_bin_mod
, &r
->expr
, line
[level
])->expr
;
1065 g
= new_clast_guard(1);
1066 if (!cloog_constraint_is_valid(lower
)) {
1068 cloog_int_set_si(val
, 0);
1069 g
->eq
[0].RHS
= &new_clast_term(val
, NULL
)->expr
;
1073 g
->eq
[0].RHS
= &new_clast_term(bound
, NULL
)->expr
;
1081 cloog_constraint_release(upper
);
1082 cloog_constraint_set_free(set
);
1083 cloog_vec_free(line_vector
);
1084 cloog_int_clear(val
);
1085 cloog_int_clear(bound
);
1090 * insert_equation function:
1091 * This function inserts an equality
1092 * constraint according to an element in the clast.
1093 * An equality can be preceded by a 'modulo guard'.
1094 * For instance, consider the constraint i -2*j = 0 and the
1095 * element j: pprint_equality should return 'if(i%2==0) { j = i/2 ;'.
1096 * - matrix is the polyhedron containing all the constraints,
1097 * - num is the line number of the constraint in matrix we want to print,
1098 * - level is the column number of the element in matrix we want to use,
1099 * - the infos structure gives the user some options about code printing,
1100 * the number of parameters in matrix (nb_par), and the arrays of iterator
1101 * names and parameters (iters and params).
1103 * - November 13th 2001: first version.
1104 * - June 26th 2003: simplification of the modulo guards (remove parts such as
1105 * modulo is 0, compare vivien or vivien2 with a previous
1106 * version for an idea).
1107 * - June 29th 2003: non-unit strides support.
1108 * - July 14th 2003: (debug) no more print the constant in the modulo guard when
1109 * it was previously included in a stride calculation.
1111 static void insert_equation(CloogConstraint upper
, CloogConstraint lower
,
1112 int level
, struct clast_stmt
***next
, CloogInfos
*infos
)
1114 struct clast_expr
*e
;
1115 struct clast_assignment
*ass
;
1117 insert_modulo_guard(upper
, lower
, level
, next
, infos
);
1119 if (cloog_constraint_is_valid(lower
) ||
1120 !clast_equal_add(infos
->equal
, NULL
, level
, upper
, infos
))
1121 { /* Finally, the equality. */
1123 /* If we have to make a block by dimension, we start the block. Function
1124 * pprint knows if there is an equality, if this is the case, it checks
1125 * for the same following condition to close the brace.
1127 if (infos
->options
->block
) {
1128 struct clast_block
*b
= new_clast_block();
1133 e
= clast_bound_from_constraint(upper
, level
, infos
->names
);
1134 ass
= new_clast_assignment(cloog_names_name_at_level(infos
->names
, level
), e
);
1136 **next
= &ass
->stmt
;
1137 *next
= &(**next
)->next
;
1140 cloog_constraint_release(lower
);
1141 cloog_constraint_release(upper
);
1148 * insert_for function:
1149 * This function inserts a for loop in the clast.
1150 * Returns 1 if the calling function should recurse into inner loops.
1152 * A loop header according to an element is the conjunction of a minimum and a
1153 * maximum on a given element (they give the loop bounds).
1154 * For instance, considering these constraints and the element j:
1158 * this function should return 'for (j=max(-i+9*M,4*M),j<=5*M;j++) {'.
1159 * If the given element is involved in modulo guards defined by
1160 * existentially quantified variables, then these guards should be
1161 * inserted inside the for loop. However, the constraints involved
1162 * in this guard should not be used in determining the lower and upper
1163 * bound of the loop. We therefore insert the guards first (which
1164 * removes the corresponding constraints from the constraint set)
1165 * and then reattach the guard inside the loop.
1166 * - constraints contains all constraints,
1167 * - level is the column number of the element in matrix we want to use,
1168 * - the infos structure gives the user some options about code printing,
1169 * the number of parameters in matrix (nb_par), and the arrays of iterator
1170 * names and parameters (iters and params).
1172 static int insert_for(CloogConstraintSet
*constraints
, int level
,
1173 struct clast_stmt
***next
, CloogInfos
*infos
)
1175 const char *iterator
;
1176 struct clast_expr
*e1
;
1177 struct clast_expr
*e2
;
1178 struct clast_assignment
*ass
;
1179 struct clast_stmt
**old_next
= *next
;
1180 struct clast_stmt
*guard
;
1182 insert_extra_modulo_guards(constraints
, 0, next
, infos
);
1185 iterator
= cloog_names_name_at_level(infos
->names
, level
);
1187 e1
= clast_minmax(constraints
, level
, 1, 0, infos
);
1188 e2
= clast_minmax(constraints
, level
, 0, 0, infos
);
1190 if (clast_expr_is_bigger_constant(e1
, e2
)) {
1191 free_clast_expr(e1
);
1192 free_clast_expr(e2
);
1196 /* If min and max are not equal there is a 'for' else, there is a '='.
1197 * In the special case e1 = e2 = NULL, this is an infinite loop
1198 * so this is not a '='.
1200 if (!clast_expr_equal(e1
, e2
) || !infos
->options
->otl
|| (!e1
&& !e2
)) {
1201 struct clast_for
*f
= new_clast_for(iterator
, e1
, e2
, infos
->stride
[level
-1]);
1202 *old_next
= &f
->stmt
;
1208 else if (!clast_equal_add(infos
->equal
, constraints
, level
,
1209 cloog_constraint_invalid(), infos
)) {
1210 if (infos
->options
->block
) {
1211 struct clast_block
*b
= new_clast_block();
1212 *old_next
= &b
->stmt
;
1218 ass
= new_clast_assignment(iterator
, e1
);
1219 free_clast_expr(e2
);
1220 *old_next
= &ass
->stmt
;
1222 ass
->stmt
.next
= guard
;
1224 *next
= &(**next
)->next
;
1226 free_clast_expr(e1
);
1227 free_clast_expr(e2
);
1235 * insert_block function:
1236 * This function inserts a statement block.
1237 * - block is the statement block,
1238 * - level is the number of loops enclosing the statement,
1239 * - the infos structure gives the user some options about code printing,
1240 * the number of parameters in domain (nb_par), and the arrays of iterator
1241 * names and parameters (iters and params).
1243 * - September 21th 2003: first version (pick from pprint function).
1245 static void insert_block(CloogBlock
*block
, int level
,
1246 struct clast_stmt
***next
, CloogInfos
*infos
)
1248 CloogStatement
* statement
;
1249 struct clast_stmt
*subs
;
1254 for (statement
= block
->statement
; statement
; statement
= statement
->next
) {
1255 CloogStatement
*s_next
= statement
->next
;
1257 subs
= clast_equal(level
,infos
);
1259 statement
->next
= NULL
;
1260 **next
= &new_clast_user_stmt(statement
, subs
)->stmt
;
1261 statement
->next
= s_next
;
1262 *next
= &(**next
)->next
;
1268 * insert_loop function:
1269 * This function converts the content of a CloogLoop structure (loop) into a
1270 * clast_stmt (inserted at **next).
1271 * The iterator (level) of
1272 * the current loop is given by 'level': this is the column number of the
1273 * domain corresponding to the current loop iterator. The data of a loop are
1274 * written in this order:
1275 * 1. The guard of the loop, i.e. each constraint in the domain that does not
1276 * depend on the iterator (when the entry in the column 'level' is 0).
1277 * 2. The iteration domain of the iterator, given by the constraints in the
1278 * domain depending on the iterator, i.e.:
1279 * * an equality if the iterator has only one value (possibly preceded by
1280 * a guard verifying if this value is integral), *OR*
1281 * * a loop from the minimum possible value of the iterator to the maximum
1283 * 3. The included statement block.
1284 * 4. The inner loops (recursive call).
1285 * 5. The following loops (recursive call).
1286 * - level is the recursion level or the iteration level that we are printing,
1287 * - the infos structure gives the user some options about code printing,
1288 * the number of parameters in domain (nb_par), and the arrays of iterator
1289 * names and parameters (iters and params).
1291 * - November 2nd 2001: first version.
1292 * - March 6th 2003: infinite domain support.
1293 * - April 19th 2003: (debug) NULL loop support.
1294 * - June 29th 2003: non-unit strides support.
1295 * - April 28th 2005: (debug) level is level+equality when print statement!
1296 * - June 16th 2005: (debug) the N. Vasilache normalization step has been
1297 * added to avoid iteration duplication (see DaeGon Kim
1298 * bug in cloog_program_generate). Try vasilache.cloog
1299 * with and without the call to cloog_matrix_normalize,
1300 * using -f 8 -l 9 options for an idea.
1301 * - September 15th 2005: (debug) don't close equality braces when unnecessary.
1302 * - October 16th 2005: (debug) scalar value is saved for next loops.
1304 static void insert_loop(CloogLoop
* loop
, int level
,
1305 struct clast_stmt
***next
, CloogInfos
*infos
)
1308 CloogConstraintSet
*constraints
, *temp
;
1309 struct clast_stmt
**top
= *next
;
1310 CloogConstraint i
, j
;
1313 /* It can happen that loop be NULL when an input polyhedron is empty. */
1317 /* The constraints do not always have a shape that allows us to generate code from it,
1318 * thus we normalize it, we also simplify it with the equalities.
1320 temp
= cloog_domain_constraints(loop
->domain
);
1321 cloog_constraint_set_normalize(temp
,level
);
1322 constraints
= cloog_constraint_set_simplify(temp
,infos
->equal
,level
,
1323 infos
->names
->nb_parameters
);
1324 cloog_constraint_set_free(temp
);
1326 cloog_int_set(infos
->stride
[level
-1], loop
->stride
);
1328 /* First of all we have to print the guard. */
1329 insert_guard(constraints
,level
, next
, infos
);
1331 if (level
&& cloog_constraint_set_contains_level(constraints
, level
,
1332 infos
->names
->nb_parameters
)) {
1333 /* We scan all the constraints to know in which case we are :
1334 * [[if] equation] or [for].
1336 if (cloog_constraint_is_valid(i
=
1337 cloog_constraint_set_defining_equality(constraints
, level
))) {
1338 insert_equation(i
, cloog_constraint_invalid(), level
, next
, infos
);
1340 } else if (cloog_constraint_is_valid(i
=
1341 cloog_constraint_set_defining_inequalities(constraints
,
1342 level
, &j
, infos
->names
->nb_parameters
))) {
1343 insert_equation(i
, j
, level
, next
, infos
);
1345 empty_loop
= !insert_for(constraints
, level
, next
, infos
);
1349 /* Finally, if there is an included statement block, print it. */
1350 insert_block(loop
->block
, level
+equality
, next
, infos
);
1352 /* Go to the next level. */
1353 if (loop
->inner
!= NULL
)
1354 insert_loop(loop
->inner
, level
+1, next
, infos
);
1358 cloog_equal_del(infos
->equal
,level
);
1359 cloog_constraint_set_free(constraints
);
1361 /* Go to the next loop on the same level. */
1363 top
= &(*top
)->next
;
1364 if (loop
->next
!= NULL
)
1365 insert_loop(loop
->next
, level
, &top
,infos
);
1369 struct clast_stmt
*cloog_clast_create(CloogProgram
*program
,
1370 CloogOptions
*options
)
1372 CloogInfos
*infos
= ALLOC(CloogInfos
);
1374 struct clast_stmt
*root
= &new_clast_root(program
->names
)->stmt
;
1375 struct clast_stmt
**next
= &root
->next
;
1377 infos
->state
= options
->state
;
1378 infos
->names
= program
->names
;
1379 infos
->options
= options
;
1380 infos
->scaldims
= program
->scaldims
;
1381 infos
->nb_scattdims
= program
->nb_scattdims
;
1383 /* Allocation for the array of strides, there is a +1 since the statement can
1384 * be included inside an external loop without iteration domain.
1386 nb_levels
= program
->names
->nb_scattering
+program
->names
->nb_iterators
+1;
1387 infos
->stride
= ALLOCN(cloog_int_t
, nb_levels
);
1388 for (i
= 0; i
< nb_levels
; ++i
)
1389 cloog_int_init(infos
->stride
[i
]);
1391 infos
->equal
= cloog_equal_alloc(nb_levels
,
1392 nb_levels
, program
->names
->nb_parameters
);
1394 insert_loop(program
->loop
, 0, &next
, infos
);
1396 cloog_equal_free(infos
->equal
);
1398 for (i
= 0; i
< nb_levels
; ++i
)
1399 cloog_int_clear(infos
->stride
[i
]);
1400 free(infos
->stride
);