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. */
20 int stride_level
; /**< Number of valid entries in stride array. */
21 int nb_scattdims
; /**< Scattering dimension number. */
22 int * scaldims
; /**< Boolean array saying whether a given
23 * scattering dimension is scalar or not.
25 CloogNames
* names
; /**< Names of iterators and parameters. */
26 CloogOptions
* options
; /**< Options on CLooG's behaviour. */
27 CloogEqualities
*equal
; /**< Matrix of equalities. */
30 typedef struct clooginfos CloogInfos
;
32 static int clast_expr_cmp(struct clast_expr
*e1
, struct clast_expr
*e2
);
33 static int clast_term_cmp(struct clast_term
*t1
, struct clast_term
*t2
);
34 static int clast_binary_cmp(struct clast_binary
*b1
, struct clast_binary
*b2
);
35 static int clast_reduction_cmp(struct clast_reduction
*r1
,
36 struct clast_reduction
*r2
);
38 static struct clast_expr
*clast_expr_copy(struct clast_expr
*e
);
40 static int clast_equal_add(CloogEqualities
*equal
,
41 CloogConstraintSet
*constraints
,
42 int level
, CloogConstraint
*constraint
,
45 static struct clast_stmt
*clast_equal(int level
, CloogInfos
*infos
);
46 static struct clast_expr
*clast_minmax(CloogConstraintSet
*constraints
,
47 int level
, int max
, int guard
,
48 int lower_bound
, int no_earlier
,
50 static void insert_guard(CloogConstraintSet
*constraints
, int level
,
51 struct clast_stmt
***next
, CloogInfos
*infos
);
52 static int insert_modulo_guard(CloogConstraint
*upper
,
53 CloogConstraint
*lower
, int level
,
54 struct clast_stmt
***next
, CloogInfos
*infos
);
55 static int insert_equation(CloogDomain
*domain
, CloogConstraint
*upper
,
56 CloogConstraint
*lower
, int level
,
57 struct clast_stmt
***next
, CloogInfos
*infos
);
58 static int insert_for(CloogDomain
*domain
, CloogConstraintSet
*constraints
,
59 int level
, int otl
, struct clast_stmt
***next
,
61 static void insert_block(CloogDomain
*domain
, CloogBlock
*block
, int level
,
62 struct clast_stmt
***next
, CloogInfos
*infos
);
63 static void insert_loop(CloogLoop
* loop
, int level
,
64 struct clast_stmt
***next
, CloogInfos
*infos
);
67 struct clast_name
*new_clast_name(const char *name
)
69 struct clast_name
*n
= malloc(sizeof(struct clast_name
));
70 n
->expr
.type
= clast_expr_name
;
75 struct clast_term
*new_clast_term(cloog_int_t c
, struct clast_expr
*v
)
77 struct clast_term
*t
= malloc(sizeof(struct clast_term
));
78 t
->expr
.type
= clast_expr_term
;
79 cloog_int_init(t
->val
);
80 cloog_int_set(t
->val
, c
);
85 struct clast_binary
*new_clast_binary(enum clast_bin_type t
,
86 struct clast_expr
*lhs
, cloog_int_t rhs
)
88 struct clast_binary
*b
= malloc(sizeof(struct clast_binary
));
89 b
->expr
.type
= clast_expr_bin
;
92 cloog_int_init(b
->RHS
);
93 cloog_int_set(b
->RHS
, rhs
);
97 struct clast_reduction
*new_clast_reduction(enum clast_red_type t
, int n
)
100 struct clast_reduction
*r
;
101 r
= malloc(sizeof(struct clast_reduction
)+(n
-1)*sizeof(struct clast_expr
*));
102 r
->expr
.type
= clast_expr_red
;
105 for (i
= 0; i
< n
; ++i
)
110 static void free_clast_root(struct clast_stmt
*s
);
112 const struct clast_stmt_op stmt_root
= { free_clast_root
};
114 static void free_clast_root(struct clast_stmt
*s
)
116 struct clast_root
*r
= (struct clast_root
*)s
;
117 assert(CLAST_STMT_IS_A(s
, stmt_root
));
118 cloog_names_free(r
->names
);
122 struct clast_root
*new_clast_root(CloogNames
*names
)
124 struct clast_root
*r
= malloc(sizeof(struct clast_root
));
125 r
->stmt
.op
= &stmt_root
;
127 r
->names
= cloog_names_copy(names
);
131 static void free_clast_assignment(struct clast_stmt
*s
);
133 const struct clast_stmt_op stmt_ass
= { free_clast_assignment
};
135 static void free_clast_assignment(struct clast_stmt
*s
)
137 struct clast_assignment
*a
= (struct clast_assignment
*)s
;
138 assert(CLAST_STMT_IS_A(s
, stmt_ass
));
139 free_clast_expr(a
->RHS
);
143 struct clast_assignment
*new_clast_assignment(const char *lhs
,
144 struct clast_expr
*rhs
)
146 struct clast_assignment
*a
= malloc(sizeof(struct clast_assignment
));
147 a
->stmt
.op
= &stmt_ass
;
154 static void free_clast_user_stmt(struct clast_stmt
*s
);
156 const struct clast_stmt_op stmt_user
= { free_clast_user_stmt
};
158 static void free_clast_user_stmt(struct clast_stmt
*s
)
160 struct clast_user_stmt
*u
= (struct clast_user_stmt
*)s
;
161 assert(CLAST_STMT_IS_A(s
, stmt_user
));
162 cloog_domain_free(u
->domain
);
163 cloog_statement_free(u
->statement
);
164 cloog_clast_free(u
->substitutions
);
168 struct clast_user_stmt
*new_clast_user_stmt(CloogDomain
*domain
,
169 CloogStatement
*stmt
, struct clast_stmt
*subs
)
171 struct clast_user_stmt
*u
= malloc(sizeof(struct clast_user_stmt
));
172 u
->stmt
.op
= &stmt_user
;
174 u
->domain
= cloog_domain_copy(domain
);
175 u
->statement
= cloog_statement_copy(stmt
);
176 u
->substitutions
= subs
;
180 static void free_clast_block(struct clast_stmt
*b
);
182 const struct clast_stmt_op stmt_block
= { free_clast_block
};
184 static void free_clast_block(struct clast_stmt
*s
)
186 struct clast_block
*b
= (struct clast_block
*)s
;
187 assert(CLAST_STMT_IS_A(s
, stmt_block
));
188 cloog_clast_free(b
->body
);
192 struct clast_block
*new_clast_block()
194 struct clast_block
*b
= malloc(sizeof(struct clast_block
));
195 b
->stmt
.op
= &stmt_block
;
201 static void free_clast_for(struct clast_stmt
*s
);
203 const struct clast_stmt_op stmt_for
= { free_clast_for
};
205 static void free_clast_for(struct clast_stmt
*s
)
207 struct clast_for
*f
= (struct clast_for
*)s
;
208 assert(CLAST_STMT_IS_A(s
, stmt_for
));
209 cloog_domain_free(f
->domain
);
210 free_clast_expr(f
->LB
);
211 free_clast_expr(f
->UB
);
212 cloog_int_clear(f
->stride
);
213 cloog_clast_free(f
->body
);
217 struct clast_for
*new_clast_for(CloogDomain
*domain
, const char *it
,
218 struct clast_expr
*LB
, struct clast_expr
*UB
,
221 struct clast_for
*f
= malloc(sizeof(struct clast_for
));
222 f
->stmt
.op
= &stmt_for
;
224 f
->domain
= cloog_domain_copy(domain
);
229 cloog_int_init(f
->stride
);
231 cloog_int_set(f
->stride
, stride
->stride
);
233 cloog_int_set_si(f
->stride
, 1);
237 static void free_clast_guard(struct clast_stmt
*s
);
239 const struct clast_stmt_op stmt_guard
= { free_clast_guard
};
241 static void free_clast_guard(struct clast_stmt
*s
)
244 struct clast_guard
*g
= (struct clast_guard
*)s
;
245 assert(CLAST_STMT_IS_A(s
, stmt_guard
));
246 cloog_clast_free(g
->then
);
247 for (i
= 0; i
< g
->n
; ++i
) {
248 free_clast_expr(g
->eq
[i
].LHS
);
249 free_clast_expr(g
->eq
[i
].RHS
);
254 struct clast_guard
*new_clast_guard(int n
)
257 struct clast_guard
*g
= malloc(sizeof(struct clast_guard
) +
258 (n
-1) * sizeof(struct clast_equation
));
259 g
->stmt
.op
= &stmt_guard
;
263 for (i
= 0; i
< n
; ++i
) {
270 void free_clast_name(struct clast_name
*n
)
275 void free_clast_term(struct clast_term
*t
)
277 cloog_int_clear(t
->val
);
278 free_clast_expr(t
->var
);
282 void free_clast_binary(struct clast_binary
*b
)
284 cloog_int_clear(b
->RHS
);
285 free_clast_expr(b
->LHS
);
289 void free_clast_reduction(struct clast_reduction
*r
)
292 for (i
= 0; i
< r
->n
; ++i
)
293 free_clast_expr(r
->elts
[i
]);
297 void free_clast_expr(struct clast_expr
*e
)
302 case clast_expr_name
:
303 free_clast_name((struct clast_name
*) e
);
305 case clast_expr_term
:
306 free_clast_term((struct clast_term
*) e
);
309 free_clast_reduction((struct clast_reduction
*) e
);
312 free_clast_binary((struct clast_binary
*) e
);
319 void free_clast_stmt(struct clast_stmt
*s
)
326 void cloog_clast_free(struct clast_stmt
*s
)
328 struct clast_stmt
*next
;
336 static int clast_name_cmp(struct clast_name
*n1
, struct clast_name
*n2
)
338 return n1
->name
== n2
->name
? 0 : strcmp(n1
->name
, n2
->name
);
341 static int clast_term_cmp(struct clast_term
*t1
, struct clast_term
*t2
)
344 if (!t1
->var
&& t2
->var
)
346 if (t1
->var
&& !t2
->var
)
348 c
= clast_expr_cmp(t1
->var
, t2
->var
);
351 return cloog_int_cmp(t1
->val
, t2
->val
);
354 static int clast_binary_cmp(struct clast_binary
*b1
, struct clast_binary
*b2
)
358 if (b1
->type
!= b2
->type
)
359 return b1
->type
- b2
->type
;
360 if ((c
= cloog_int_cmp(b1
->RHS
, b2
->RHS
)))
362 return clast_expr_cmp(b1
->LHS
, b2
->LHS
);
365 static int clast_reduction_cmp(struct clast_reduction
*r1
, struct clast_reduction
*r2
)
370 if (r1
->n
== 1 && r2
->n
== 1)
371 return clast_expr_cmp(r1
->elts
[0], r2
->elts
[0]);
372 if (r1
->type
!= r2
->type
)
373 return r1
->type
- r2
->type
;
375 return r1
->n
- r2
->n
;
376 for (i
= 0; i
< r1
->n
; ++i
)
377 if ((c
= clast_expr_cmp(r1
->elts
[i
], r2
->elts
[i
])))
382 static int clast_expr_cmp(struct clast_expr
*e1
, struct clast_expr
*e2
)
390 if (e1
->type
!= e2
->type
)
391 return e1
->type
- e2
->type
;
393 case clast_expr_name
:
394 return clast_name_cmp((struct clast_name
*) e1
,
395 (struct clast_name
*) e2
);
396 case clast_expr_term
:
397 return clast_term_cmp((struct clast_term
*) e1
,
398 (struct clast_term
*) e2
);
400 return clast_binary_cmp((struct clast_binary
*) e1
,
401 (struct clast_binary
*) e2
);
403 return clast_reduction_cmp((struct clast_reduction
*) e1
,
404 (struct clast_reduction
*) e2
);
410 int clast_expr_equal(struct clast_expr
*e1
, struct clast_expr
*e2
)
412 return clast_expr_cmp(e1
, e2
) == 0;
416 * Return 1 is both expressions are constant terms and e1 is bigger than e2.
418 int clast_expr_is_bigger_constant(struct clast_expr
*e1
, struct clast_expr
*e2
)
420 struct clast_term
*t1
, *t2
;
421 struct clast_reduction
*r
;
425 if (e1
->type
== clast_expr_red
) {
426 r
= (struct clast_reduction
*)e1
;
427 return r
->n
== 1 && clast_expr_is_bigger_constant(r
->elts
[0], e2
);
429 if (e2
->type
== clast_expr_red
) {
430 r
= (struct clast_reduction
*)e2
;
431 return r
->n
== 1 && clast_expr_is_bigger_constant(e1
, r
->elts
[0]);
433 if (e1
->type
!= clast_expr_term
|| e2
->type
!= clast_expr_term
)
435 t1
= (struct clast_term
*)e1
;
436 t2
= (struct clast_term
*)e2
;
437 if (t1
->var
|| t2
->var
)
439 return cloog_int_gt(t1
->val
, t2
->val
);
442 static int qsort_expr_cmp(const void *p1
, const void *p2
)
444 return clast_expr_cmp(*(struct clast_expr
**)p1
, *(struct clast_expr
**)p2
);
447 static void clast_reduction_sort(struct clast_reduction
*r
)
449 qsort(&r
->elts
[0], r
->n
, sizeof(struct clast_expr
*), qsort_expr_cmp
);
452 static int qsort_eq_cmp(const void *p1
, const void *p2
)
454 struct clast_equation
*eq1
= (struct clast_equation
*)p1
;
455 struct clast_equation
*eq2
= (struct clast_equation
*)p2
;
458 cmp
= clast_expr_cmp(eq1
->LHS
, eq2
->LHS
);
462 cmp
= clast_expr_cmp(eq1
->RHS
, eq2
->RHS
);
466 return eq1
->sign
- eq2
->sign
;
470 * Sort equations in a clast_guard.
472 static void clast_guard_sort(struct clast_guard
*g
)
474 qsort(&g
->eq
[0], g
->n
, sizeof(struct clast_equation
), qsort_eq_cmp
);
479 * Construct a (deep) copy of an expression clast.
481 static struct clast_expr
*clast_expr_copy(struct clast_expr
*e
)
486 case clast_expr_name
: {
487 struct clast_name
* n
= (struct clast_name
*) e
;
488 return &new_clast_name(n
->name
)->expr
;
490 case clast_expr_term
: {
491 struct clast_term
* t
= (struct clast_term
*) e
;
492 return &new_clast_term(t
->val
, clast_expr_copy(t
->var
))->expr
;
494 case clast_expr_red
: {
496 struct clast_reduction
*r
= (struct clast_reduction
*) e
;
497 struct clast_reduction
*r2
= new_clast_reduction(r
->type
, r
->n
);
498 for (i
= 0; i
< r
->n
; ++i
)
499 r2
->elts
[i
] = clast_expr_copy(r
->elts
[i
]);
502 case clast_expr_bin
: {
503 struct clast_binary
*b
= (struct clast_binary
*) e
;
504 return &new_clast_binary(b
->type
, clast_expr_copy(b
->LHS
), b
->RHS
)->expr
;
512 /******************************************************************************
513 * Equalities spreading functions *
514 ******************************************************************************/
518 * clast_equal_allow function:
519 * This function checks whether the options allow us to spread the equality or
520 * not. It returns 1 if so, 0 otherwise.
521 * - equal is the matrix of equalities,
522 * - level is the column number in equal of the element which is 'equal to',
523 * - line is the line number in equal of the constraint we want to study,
524 * - the infos structure gives the user all options on code printing and more.
526 * - October 27th 2005: first version (extracted from old pprint_equal_add).
528 static int clast_equal_allow(CloogEqualities
*equal
, int level
, int line
,
531 if (level
< infos
->options
->fsp
)
534 if ((cloog_equal_type(equal
, level
) == EQTYPE_EXAFFINE
) &&
535 !infos
->options
->esp
)
543 * clast_equal_add function:
544 * This function updates the row (level-1) of the equality matrix (equal) with
545 * the row that corresponds to the row (line) of the matrix (matrix). It returns
546 * 1 if the row can be updated, 0 otherwise.
547 * - equal is the matrix of equalities,
548 * - matrix is the matrix of constraints,
549 * - level is the column number in matrix of the element which is 'equal to',
550 * - line is the line number in matrix of the constraint we want to study,
551 * - the infos structure gives the user all options on code printing and more.
553 static int clast_equal_add(CloogEqualities
*equal
,
554 CloogConstraintSet
*constraints
,
555 int level
, CloogConstraint
*constraint
,
558 cloog_equal_add(equal
, constraints
, level
, constraint
,
559 infos
->names
->nb_parameters
);
561 return clast_equal_allow(equal
, level
, level
-1, infos
);
567 * clast_equal function:
568 * This function prints the substitution data of a statement into a clast_stmt.
569 * Using this function instead of pprint_equal is useful for generating
570 * a compilable pseudo-code by using preprocessor macro for each statement.
571 * By opposition to pprint_equal, the result is less human-readable. For
572 * instance this function will print (i,i+3,k,3) where pprint_equal would
573 * return (j=i+3,l=3).
574 * - level is the number of loops enclosing the statement,
575 * - the infos structure gives the user all options on code printing and more.
577 * - March 12th 2004: first version.
578 * - November 21th 2005: (debug) now works well with GMP version.
580 static struct clast_stmt
*clast_equal(int level
, CloogInfos
*infos
)
583 struct clast_expr
*e
;
584 struct clast_stmt
*a
= NULL
;
585 struct clast_stmt
**next
= &a
;
586 CloogEqualities
*equal
= infos
->equal
;
587 CloogConstraint
*equal_constraint
;
589 for (i
=infos
->names
->nb_scattering
;i
<level
-1;i
++)
590 { if (cloog_equal_type(equal
, i
+1)) {
591 equal_constraint
= cloog_equal_constraint(equal
, i
);
592 e
= clast_bound_from_constraint(equal_constraint
, i
+1, infos
->names
);
593 cloog_constraint_release(equal_constraint
);
595 e
= &new_clast_term(infos
->state
->one
, &new_clast_name(
596 cloog_names_name_at_level(infos
->names
, i
+1))->expr
)->expr
;
598 *next
= &new_clast_assignment(NULL
, e
)->stmt
;
599 next
= &(*next
)->next
;
607 * clast_bound_from_constraint function:
608 * This function returns a clast_expr containing the printing of the
609 * 'right part' of a constraint according to an element.
610 * For instance, for the constraint -3*i + 2*j - M >=0 and the element j,
611 * we have j >= (3*i + M)/2. As we are looking for integral solutions, this
612 * function should return 'ceild(3*i+M,2)'.
613 * - matrix is the polyhedron containing all the constraints,
614 * - line_num is the line number in domain of the constraint we want to print,
615 * - level is the column number in domain of the element we want to use,
616 * - names structure gives the user some options about code printing,
617 * the number of parameters in domain (nb_par), and the arrays of iterator
618 * names and parameters (iters and params).
620 * - November 2nd 2001: first version.
621 * - June 27th 2003: 64 bits version ready.
623 struct clast_expr
*clast_bound_from_constraint(CloogConstraint
*constraint
,
624 int level
, CloogNames
*names
)
626 int i
, sign
, nb_elts
=0, len
;
627 cloog_int_t
*line
, numerator
, denominator
, temp
, division
;
628 struct clast_expr
*e
= NULL
;
629 struct cloog_vec
*line_vector
;
631 len
= cloog_constraint_total_dimension(constraint
) + 2;
632 line_vector
= cloog_vec_alloc(len
);
633 line
= line_vector
->p
;
634 cloog_constraint_copy_coefficients(constraint
, line
+1);
635 cloog_int_init(temp
);
636 cloog_int_init(numerator
);
637 cloog_int_init(denominator
);
639 if (!cloog_int_is_zero(line
[level
])) {
640 struct clast_reduction
*r
;
641 /* Maybe we need to invert signs in such a way that the element sign is>0.*/
642 sign
= -cloog_int_sgn(line
[level
]);
644 for (i
= 1, nb_elts
= 0; i
<= len
- 1; ++i
)
645 if (i
!= level
&& !cloog_int_is_zero(line
[i
]))
647 r
= new_clast_reduction(clast_red_sum
, nb_elts
);
650 /* First, we have to print the iterators and the parameters. */
651 for (i
= 1; i
<= len
- 2; i
++) {
652 struct clast_expr
*v
;
654 if (i
== level
|| cloog_int_is_zero(line
[i
]))
657 v
= cloog_constraint_variable_expr(constraint
, i
, names
);
660 cloog_int_neg(temp
,line
[i
]);
662 cloog_int_set(temp
,line
[i
]);
664 r
->elts
[nb_elts
++] = &new_clast_term(temp
, v
)->expr
;
668 cloog_int_neg(numerator
, line
[len
- 1]);
669 cloog_int_set(denominator
, line
[level
]);
672 cloog_int_set(numerator
, line
[len
- 1]);
673 cloog_int_neg(denominator
, line
[level
]);
676 /* Finally, the constant, and the final printing. */
678 if (!cloog_int_is_zero(numerator
))
679 r
->elts
[nb_elts
++] = &new_clast_term(numerator
, NULL
)->expr
;
681 if (!cloog_int_is_one(line
[level
]) && !cloog_int_is_neg_one(line
[level
]))
682 { if (!cloog_constraint_is_equality(constraint
))
683 { if (cloog_int_is_pos(line
[level
]))
684 e
= &new_clast_binary(clast_bin_cdiv
, &r
->expr
, denominator
)->expr
;
686 e
= &new_clast_binary(clast_bin_fdiv
, &r
->expr
, denominator
)->expr
;
688 e
= &new_clast_binary(clast_bin_div
, &r
->expr
, denominator
)->expr
;
693 free_clast_reduction(r
);
694 if (cloog_int_is_zero(numerator
))
695 e
= &new_clast_term(numerator
, NULL
)->expr
;
697 { if (!cloog_int_is_one(denominator
))
698 { if (!cloog_constraint_is_equality(constraint
)) { /* useful? */
699 if (cloog_int_is_divisible_by(numerator
, denominator
)) {
700 cloog_int_divexact(temp
, numerator
, denominator
);
701 e
= &new_clast_term(temp
, NULL
)->expr
;
704 cloog_int_init(division
);
705 cloog_int_tdiv_q(division
, numerator
, denominator
);
706 if (cloog_int_is_neg(numerator
)) {
707 if (cloog_int_is_pos(line
[level
])) {
709 e
= &new_clast_term(division
, NULL
)->expr
;
712 cloog_int_sub_ui(temp
, division
, 1);
713 e
= &new_clast_term(temp
, NULL
)->expr
;
717 { if (cloog_int_is_pos(line
[level
]))
718 { /* nb>0 need max */
719 cloog_int_add_ui(temp
, division
, 1);
720 e
= &new_clast_term(temp
, NULL
)->expr
;
724 e
= &new_clast_term(division
, NULL
)->expr
;
726 cloog_int_clear(division
);
730 e
= &new_clast_binary(clast_bin_div
,
731 &new_clast_term(numerator
, NULL
)->expr
,
735 e
= &new_clast_term(numerator
, NULL
)->expr
;
740 cloog_vec_free(line_vector
);
742 cloog_int_clear(temp
);
743 cloog_int_clear(numerator
);
744 cloog_int_clear(denominator
);
750 /* Temporary structure for communication between clast_minmax and
751 * its cloog_constraint_set_foreach_constraint callback functions.
753 struct clast_minmax_data
{
761 struct clast_reduction
*r
;
765 /* Should constraint "c" be considered by clast_minmax?
767 * If d->no_earlier is set, then the constraint may not involve
768 * any earlier variables.
770 static int valid_bound(CloogConstraint
*c
, struct clast_minmax_data
*d
)
774 if (d
->max
&& !cloog_constraint_is_lower_bound(c
, d
->level
- 1))
776 if (!d
->max
&& !cloog_constraint_is_upper_bound(c
, d
->level
- 1))
778 if (cloog_constraint_is_equality(c
))
780 if (d
->guard
&& cloog_constraint_involves(c
, d
->guard
- 1))
784 for (i
= 0; i
< d
->level
- 1; ++i
)
785 if (cloog_constraint_involves(c
, i
))
792 /* Increment n for each bound that should be considered by clast_minmax.
794 static int count_bounds(CloogConstraint
*c
, void *user
)
796 struct clast_minmax_data
*d
= (struct clast_minmax_data
*) user
;
798 if (!valid_bound(c
, d
))
807 /* Update the given lower bound based on stride information,
808 * for those cases where the stride offset is represented by
810 * Note that cloog_loop_stride may have already performed a
811 * similar update of the lower bounds, but the updated lower
812 * bounds may have been eliminated because they are redundant
813 * by definition. On the other hand, performing the update
814 * on an already updated constraint is an identity operation
815 * and is therefore harmless.
817 static CloogConstraint
*update_lower_bound_c(CloogConstraint
*c
, int level
,
820 if (!stride
->constraint
)
822 return cloog_constraint_stride_lower_bound(c
, level
, stride
);
826 /* Update the given lower bound based on stride information.
827 * If the stride offset is represented by a constraint,
828 * then we have already performed the update in update_lower_bound_c.
829 * Otherwise, the original lower bound is known to be a constant.
830 * If the bound has already been updated and it just happens
831 * to be a constant, then this function performs an identity
832 * operation on the constant.
834 static void update_lower_bound(struct clast_expr
*expr
, int level
,
837 struct clast_term
*t
;
838 if (stride
->constraint
)
840 if (expr
->type
!= clast_expr_term
)
842 t
= (struct clast_term
*)expr
;
845 cloog_int_sub(t
->val
, t
->val
, stride
->offset
);
846 cloog_int_cdiv_q(t
->val
, t
->val
, stride
->stride
);
847 cloog_int_mul(t
->val
, t
->val
, stride
->stride
);
848 cloog_int_add(t
->val
, t
->val
, stride
->offset
);
852 /* Add all relevant bounds to r->elts and update lower bounds
853 * based on stride information.
855 static int collect_bounds(CloogConstraint
*c
, void *user
)
857 struct clast_minmax_data
*d
= (struct clast_minmax_data
*) user
;
859 if (!valid_bound(c
, d
))
862 c
= cloog_constraint_copy(c
);
864 if (d
->lower_bound
&& d
->infos
->stride
[d
->level
- 1])
865 c
= update_lower_bound_c(c
, d
->level
, d
->infos
->stride
[d
->level
- 1]);
867 d
->r
->elts
[d
->n
] = clast_bound_from_constraint(c
, d
->level
,
869 if (d
->lower_bound
&& d
->infos
->stride
[d
->level
- 1]) {
870 update_lower_bound(d
->r
->elts
[d
->n
], d
->level
,
871 d
->infos
->stride
[d
->level
- 1]);
874 cloog_constraint_release(c
);
883 * clast_minmax function:
884 * This function returns a clast_expr containing the printing of a minimum or a
885 * maximum of the 'right parts' of all constraints according to an element.
886 * For instance consider the constraints:
890 * if we are looking for the minimum for the element j, the function should
891 * return 'max(ceild(3*i+M,2),-2*i)'.
892 * - constraints is the constraints,
893 * - level is the column number in domain of the element we want to use,
894 * - max is a boolean set to 1 if we are looking for a maximum, 0 for a minimum,
895 * - guard is set to 0 if there is no guard, and set to the level of the element
896 * with a guard otherwise (then the function gives the max or the min only
897 * for the constraint where the guarded coefficient is 0),
898 * - lower is set to 1 if the maximum is to be used a lower bound on a loop
899 * - no_earlier is set if no constraints should be used that involve
900 * earlier dimensions,
901 * - the infos structure gives the user some options about code printing,
902 * the number of parameters in domain (nb_par), and the arrays of iterator
903 * names and parameters (iters and params).
905 * - November 2nd 2001: first version.
907 static struct clast_expr
*clast_minmax(CloogConstraintSet
*constraints
,
908 int level
, int max
, int guard
,
909 int lower_bound
, int no_earlier
,
912 struct clast_minmax_data data
= { level
, max
, guard
, lower_bound
,
917 cloog_constraint_set_foreach_constraint(constraints
, count_bounds
, &data
);
921 data
.r
= new_clast_reduction(max
? clast_red_max
: clast_red_min
, data
.n
);
924 cloog_constraint_set_foreach_constraint(constraints
, collect_bounds
, &data
);
926 clast_reduction_sort(data
.r
);
927 return &data
.r
->expr
;
932 * Insert modulo guards defined by existentially quantified dimensions,
933 * not involving the given level.
935 * This function is called from within insert_guard.
936 * Any constraint used in constructing a modulo guard is removed
937 * from the constraint set to avoid insert_guard
938 * adding a duplicate (pair of) constraint(s).
940 * Return the updated CloogConstraintSet.
942 static CloogConstraintSet
*insert_extra_modulo_guards(
943 CloogConstraintSet
*constraints
, int level
,
944 struct clast_stmt
***next
, CloogInfos
*infos
)
949 CloogConstraint
*upper
, *lower
;
951 total_dim
= cloog_constraint_set_total_dimension(constraints
);
952 nb_iter
= cloog_constraint_set_n_iterators(constraints
,
953 infos
->names
->nb_parameters
);
955 for (i
= total_dim
- infos
->names
->nb_parameters
; i
>= nb_iter
+ 1; i
--) {
956 if (cloog_constraint_is_valid(upper
=
957 cloog_constraint_set_defining_equality(constraints
, i
))) {
958 if (!level
|| (nb_iter
< level
) ||
959 !cloog_constraint_involves(upper
, level
-1)) {
960 insert_modulo_guard(upper
,
961 cloog_constraint_invalid(), i
, next
, infos
);
962 constraints
= cloog_constraint_set_drop_constraint(constraints
,
965 cloog_constraint_release(upper
);
966 } else if (cloog_constraint_is_valid(upper
=
967 cloog_constraint_set_defining_inequalities(constraints
,
968 i
, &lower
, infos
->names
->nb_parameters
))) {
969 if (!level
|| (nb_iter
< level
) ||
970 !cloog_constraint_involves(upper
, level
-1)) {
971 insert_modulo_guard(upper
, lower
, i
, next
, infos
);
972 constraints
= cloog_constraint_set_drop_constraint(constraints
,
974 constraints
= cloog_constraint_set_drop_constraint(constraints
,
977 cloog_constraint_release(upper
);
978 cloog_constraint_release(lower
);
986 /* Temporary structure for communication between insert_guard and
987 * its cloog_constraint_set_foreach_constraint callback function.
989 struct clast_guard_data
{
995 CloogConstraintSet
*copy
;
996 struct clast_guard
*g
;
1003 static int guard_count_bounds(CloogConstraint
*c
, void *user
)
1005 struct clast_guard_data
*d
= (struct clast_guard_data
*) user
;
1013 /* Insert a guard, if necesessary, for constraint j.
1015 * If the constraint involves any earlier dimensions, then we have
1016 * already considered it during a previous iteration over the constraints.
1018 * If we have already generated a min [max] for the current level d->i
1019 * and if the current constraint is an upper [lower] bound, then we
1020 * can skip the constraint as it will already have been used
1021 * in that previously generated min [max].
1023 static int insert_guard_constraint(CloogConstraint
*j
, void *user
)
1026 struct clast_guard_data
*d
= (struct clast_guard_data
*) user
;
1028 int individual_constraint
;
1029 struct clast_expr
*v
;
1030 struct clast_term
*t
;
1032 if (!cloog_constraint_involves(j
, d
->i
- 1))
1035 for (i
= 0; i
< d
->i
- 1; ++i
)
1036 if (cloog_constraint_involves(j
, i
))
1039 if (d
->level
&& d
->nb_iter
>= d
->level
&&
1040 cloog_constraint_involves(j
, d
->level
- 1))
1043 individual_constraint
= !d
->level
|| cloog_constraint_is_equality(j
);
1044 if (!individual_constraint
) {
1045 if (d
->max
&& cloog_constraint_is_lower_bound(j
, d
->i
- 1))
1047 if (d
->min
&& cloog_constraint_is_upper_bound(j
, d
->i
- 1))
1051 v
= cloog_constraint_variable_expr(j
, d
->i
, d
->infos
->names
);
1052 d
->g
->eq
[d
->n
].LHS
= &(t
= new_clast_term(d
->infos
->state
->one
, v
))->expr
;
1053 if (individual_constraint
) {
1054 /* put the "denominator" in the LHS */
1055 cloog_constraint_coefficient_get(j
, d
->i
- 1, &t
->val
);
1056 cloog_constraint_coefficient_set(j
, d
->i
- 1, d
->infos
->state
->one
);
1057 if (cloog_int_is_neg(t
->val
)) {
1058 cloog_int_neg(t
->val
, t
->val
);
1059 cloog_constraint_coefficient_set(j
, d
->i
- 1, d
->infos
->state
->negone
);
1061 if (d
->level
|| cloog_constraint_is_equality(j
))
1062 d
->g
->eq
[d
->n
].sign
= 0;
1063 else if (cloog_constraint_is_lower_bound(j
, d
->i
- 1))
1064 d
->g
->eq
[d
->n
].sign
= 1;
1066 d
->g
->eq
[d
->n
].sign
= -1;
1067 d
->g
->eq
[d
->n
].RHS
= clast_bound_from_constraint(j
, d
->i
, d
->infos
->names
);
1071 if (cloog_constraint_is_lower_bound(j
, d
->i
- 1)) {
1074 d
->g
->eq
[d
->n
].sign
= 1;
1078 d
->g
->eq
[d
->n
].sign
= -1;
1081 guarded
= (d
->nb_iter
>= d
->level
) ? d
->level
: 0 ;
1082 d
->g
->eq
[d
->n
].RHS
= clast_minmax(d
->copy
, d
->i
, minmax
, guarded
, 0, 1,
1092 * insert_guard function:
1093 * This function inserts a guard in the clast.
1094 * A guard on an element (level) is :
1095 * -> the conjunction of all the existing constraints where the coefficient of
1096 * this element is 0 if the element is an iterator,
1097 * -> the conjunction of all the existing constraints if the element isn't an
1099 * For instance, considering these constraints and the element j:
1102 * this function should return 'if (2*i+M>=0) {'.
1103 * - matrix is the polyhedron containing all the constraints,
1104 * - level is the column number of the element in matrix we want to use,
1105 * - the infos structure gives the user some options about code printing,
1106 * the number of parameters in matrix (nb_par), and the arrays of iterator
1107 * names and parameters (iters and params).
1109 * - November 3rd 2001: first version.
1110 * - November 14th 2001: a lot of 'purifications'.
1111 * - July 31th 2002: (debug) some guard parts are no more redundants.
1112 * - August 12th 2002: polyhedra union ('or' conditions) are now supported.
1113 * - October 27th 2005: polyhedra union ('or' conditions) are no more supported
1114 * (the need came from loop_simplify that may result in
1115 * domain unions, now it should be fixed directly in
1116 * cloog_loop_simplify).
1118 static void insert_guard(CloogConstraintSet
*constraints
, int level
,
1119 struct clast_stmt
***next
, CloogInfos
*infos
)
1122 struct clast_guard_data data
= { level
, infos
, 0 };
1127 data
.copy
= cloog_constraint_set_copy(constraints
);
1129 data
.copy
= insert_extra_modulo_guards(data
.copy
, level
, next
, infos
);
1131 cloog_constraint_set_foreach_constraint(constraints
,
1132 guard_count_bounds
, &data
);
1134 data
.g
= new_clast_guard(data
.n
);
1137 /* Well, it looks complicated because I wanted to have a particular, more
1138 * readable, ordering, obviously this function may be far much simpler !
1140 data
.nb_iter
= cloog_constraint_set_n_iterators(constraints
,
1141 infos
->names
->nb_parameters
);
1143 /* We search for guard parts. */
1144 total_dim
= cloog_constraint_set_total_dimension(constraints
);
1145 for (data
.i
= 1; data
.i
<= total_dim
; data
.i
++) {
1148 cloog_constraint_set_foreach_constraint(data
.copy
,
1149 insert_guard_constraint
, &data
);
1152 cloog_constraint_set_free(data
.copy
);
1156 clast_guard_sort(data
.g
);
1157 **next
= &data
.g
->stmt
;
1158 *next
= &data
.g
->then
;
1160 free_clast_stmt(&data
.g
->stmt
);
1164 * Check if the constant "cst" satisfies the modulo guard that
1165 * would be introduced by insert_computed_modulo_guard.
1166 * The constant is assumed to have been reduced prior to calling
1169 static int constant_modulo_guard_is_satisfied(CloogConstraint
*lower
,
1170 cloog_int_t bound
, cloog_int_t cst
)
1172 if (cloog_constraint_is_valid(lower
))
1173 return cloog_int_le(cst
, bound
);
1175 return cloog_int_is_zero(cst
);
1179 * Insert a modulo guard "r % mod == 0" or "r % mod <= bound",
1180 * depending on whether lower represents a valid constraint.
1182 static void insert_computed_modulo_guard(struct clast_reduction
*r
,
1183 CloogConstraint
*lower
, cloog_int_t mod
, cloog_int_t bound
,
1184 struct clast_stmt
***next
)
1186 struct clast_expr
*e
;
1187 struct clast_guard
*g
;
1189 e
= &new_clast_binary(clast_bin_mod
, &r
->expr
, mod
)->expr
;
1190 g
= new_clast_guard(1);
1191 if (!cloog_constraint_is_valid(lower
)) {
1193 cloog_int_set_si(bound
, 0);
1194 g
->eq
[0].RHS
= &new_clast_term(bound
, NULL
)->expr
;
1198 g
->eq
[0].RHS
= &new_clast_term(bound
, NULL
)->expr
;
1207 /* Try and eliminate coefficients from a modulo constraint based on
1208 * stride information of an earlier level.
1209 * The modulo of the constraint being constructed is "m".
1210 * The stride information at level "level" is given by "stride"
1211 * and indicated that the iterator i at level "level" is equal to
1212 * some expression modulo stride->stride.
1213 * If stride->stride is a multiple of "m' then i is also equal to
1214 * the expression modulo m and so we can eliminate the coefficient of i.
1216 * If stride->constraint is NULL, then i has a constant value modulo m, stored
1217 * stride->offset. We simply multiply this constant with the coefficient
1218 * of i and add the result to the constant term, reducing it modulo m.
1220 * If stride->constraint is not NULL, then it is a constraint of the form
1224 * with s equal to stride->stride, e an expression in terms of the
1225 * parameters and earlier iterators and a some arbitrary expression
1226 * in terms of existentially quantified variables.
1227 * stride->factor is a value f such that f * k = -1 mod s.
1228 * Adding stride->constraint f * c times to the current modulo constraint,
1229 * with c the coefficient of i eliminates i in favor of parameters and
1230 * earlier variables.
1232 static void eliminate_using_stride_constraint(cloog_int_t
*line
, int len
,
1233 int nb_iter
, CloogStride
*stride
, int level
, cloog_int_t m
)
1237 if (!cloog_int_is_divisible_by(stride
->stride
, m
))
1240 if (stride
->constraint
) {
1246 cloog_int_mul(t
, line
[level
], stride
->factor
);
1247 for (i
= 1; i
< level
; ++i
) {
1248 cloog_constraint_coefficient_get(stride
->constraint
,
1250 cloog_int_addmul(line
[i
], t
, v
);
1251 cloog_int_fdiv_r(line
[i
], line
[i
], m
);
1253 s_len
= cloog_constraint_total_dimension(stride
->constraint
)+2;
1254 for (i
= nb_iter
+ 1; i
<= len
- 2; ++i
) {
1255 cloog_constraint_coefficient_get(stride
->constraint
,
1256 i
- (len
- s_len
) - 1, &v
);
1257 cloog_int_addmul(line
[i
], t
, v
);
1258 cloog_int_fdiv_r(line
[i
], line
[i
], m
);
1260 cloog_constraint_constant_get(stride
->constraint
, &v
);
1261 cloog_int_addmul(line
[len
- 1], t
, v
);
1262 cloog_int_fdiv_r(line
[len
- 1], line
[len
- 1], m
);
1266 cloog_int_addmul(line
[len
- 1], line
[level
], stride
->offset
);
1267 cloog_int_fdiv_r(line
[len
- 1], line
[len
- 1], m
);
1270 cloog_int_set_si(line
[level
], 0);
1274 /* Temporary structure for communication between insert_modulo_guard and
1275 * its cloog_constraint_set_foreach_constraint callback function.
1277 struct clast_modulo_guard_data
{
1278 CloogConstraint
*lower
;
1280 struct clast_stmt
***next
;
1283 cloog_int_t val
, bound
;
1287 /* Insert a modulo guard for constraint c.
1288 * The constraint may be either an equality or an inequality.
1289 * Since this function returns -1, it is only called on a single constraint.
1290 * In case of an inequality, the constraint is usually an upper bound
1291 * on d->level. However, if this variable is an existentially
1292 * quantified variable, the upper bound constraint may get removed
1293 * as trivially holding and then this function is called with
1294 * a lower bound instead. In this case, we need to adjust the constraint
1295 * based on the sum of the constant terms of the lower and upper bound
1296 * stored in d->bound.
1298 static int insert_modulo_guard_constraint(CloogConstraint
*c
, void *user
)
1300 struct clast_modulo_guard_data
*d
= (struct clast_modulo_guard_data
*) user
;
1301 int level
= d
->level
;
1302 CloogInfos
*infos
= d
->infos
;
1303 int i
, nb_elts
= 0, len
, nb_iter
, nb_par
;
1305 struct cloog_vec
*line_vector
;
1308 len
= cloog_constraint_total_dimension(c
) + 2;
1309 nb_par
= infos
->names
->nb_parameters
;
1310 nb_iter
= len
- 2 - nb_par
;
1312 line_vector
= cloog_vec_alloc(len
);
1313 line
= line_vector
->p
;
1314 cloog_constraint_copy_coefficients(c
, line
+ 1);
1316 if (cloog_int_is_pos(line
[level
])) {
1317 cloog_seq_neg(line
+ 1, line
+ 1, len
- 1);
1318 if (!cloog_constraint_is_equality(c
))
1319 cloog_int_add(line
[len
- 1], line
[len
- 1], d
->bound
);
1321 cloog_int_neg(line
[level
], line
[level
]);
1322 assert(cloog_int_is_pos(line
[level
]));
1325 for (i
= 1; i
<= len
-1; ++i
) {
1328 cloog_int_fdiv_r(line
[i
], line
[i
], line
[level
]);
1329 if (cloog_int_is_zero(line
[i
]))
1337 if (nb_elts
|| !cloog_int_is_zero(line
[len
-1])) {
1338 struct clast_reduction
*r
;
1341 r
= new_clast_reduction(clast_red_sum
, nb_elts
+ 1);
1344 /* First, the modulo guard : the iterators... */
1346 if (i
> infos
->stride_level
)
1347 i
= infos
->stride_level
;
1349 eliminate_using_stride_constraint(line
, len
, nb_iter
,
1350 infos
->stride
[i
- 1], i
, line
[level
]);
1351 for (i
=1;i
<=nb_iter
;i
++) {
1352 if (i
== level
|| cloog_int_is_zero(line
[i
]))
1355 name
= cloog_names_name_at_level(infos
->names
, i
);
1357 r
->elts
[nb_elts
++] = &new_clast_term(line
[i
],
1358 &new_clast_name(name
)->expr
)->expr
;
1361 /* ...the parameters... */
1362 for (i
=nb_iter
+1;i
<=len
-2;i
++) {
1363 if (cloog_int_is_zero(line
[i
]))
1366 name
= infos
->names
->parameters
[i
-nb_iter
-1] ;
1367 r
->elts
[nb_elts
++] = &new_clast_term(line
[i
],
1368 &new_clast_name(name
)->expr
)->expr
;
1371 constant
= nb_elts
== 0;
1372 /* ...the constant. */
1373 if (!cloog_int_is_zero(line
[len
-1]))
1374 r
->elts
[nb_elts
++] = &new_clast_term(line
[len
-1], NULL
)->expr
;
1376 /* our initial computation may have been an overestimate */
1380 d
->empty
= !constant_modulo_guard_is_satisfied(d
->lower
, d
->bound
,
1382 free_clast_reduction(r
);
1384 insert_computed_modulo_guard(r
, d
->lower
, line
[level
], d
->bound
,
1388 cloog_vec_free(line_vector
);
1395 * insert_modulo_guard:
1396 * This function inserts a modulo guard corresponding to an equality
1397 * or a pair of inequalities.
1398 * Returns 0 if the modulo guard is discovered to be unsatisfiable.
1400 * See insert_equation.
1401 * - matrix is the polyhedron containing all the constraints,
1402 * - upper and lower are the line numbers of the constraint in matrix
1403 * we want to print; in particular, if we want to print an equality,
1404 * then lower == -1 and upper is the row of the equality; if we want
1405 * to print an inequality, then upper is the row of the upper bound
1406 * and lower in the row of the lower bound
1407 * - level is the column number of the element in matrix we want to use,
1408 * - the infos structure gives the user some options about code printing,
1409 * the number of parameters in matrix (nb_par), and the arrays of iterator
1410 * names and parameters (iters and params).
1412 static int insert_modulo_guard(CloogConstraint
*upper
,
1413 CloogConstraint
*lower
, int level
,
1414 struct clast_stmt
***next
, CloogInfos
*infos
)
1417 CloogConstraintSet
*set
;
1418 struct clast_modulo_guard_data data
= { lower
, level
, next
, infos
, 0 };
1420 cloog_int_init(data
.val
);
1421 cloog_constraint_coefficient_get(upper
, level
-1, &data
.val
);
1422 if (cloog_int_is_one(data
.val
) || cloog_int_is_neg_one(data
.val
)) {
1423 cloog_int_clear(data
.val
);
1427 nb_par
= infos
->names
->nb_parameters
;
1429 cloog_int_init(data
.bound
);
1430 /* Check if would be emitting the redundant constraint mod(e,m) <= m-1 */
1431 if (cloog_constraint_is_valid(lower
)) {
1432 cloog_constraint_constant_get(upper
, &data
.val
);
1433 cloog_constraint_constant_get(lower
, &data
.bound
);
1434 cloog_int_add(data
.bound
, data
.val
, data
.bound
);
1435 cloog_constraint_coefficient_get(lower
, level
-1, &data
.val
);
1436 cloog_int_sub_ui(data
.val
, data
.val
, 1);
1437 if (cloog_int_eq(data
.val
, data
.bound
)) {
1438 cloog_int_clear(data
.val
);
1439 cloog_int_clear(data
.bound
);
1444 if (cloog_constraint_needs_reduction(upper
, level
)) {
1445 set
= cloog_constraint_set_for_reduction(upper
, lower
);
1446 set
= cloog_constraint_set_reduce(set
, level
, infos
->equal
,
1447 nb_par
, &data
.bound
);
1448 cloog_constraint_set_foreach_constraint(set
,
1449 insert_modulo_guard_constraint
, &data
);
1450 cloog_constraint_set_free(set
);
1452 insert_modulo_guard_constraint(upper
, &data
);
1454 cloog_int_clear(data
.val
);
1455 cloog_int_clear(data
.bound
);
1462 * We found an equality or a pair of inequalities identifying
1463 * a loop with a single iteration, but the user wants us to generate
1464 * a loop anyway, so we do it here.
1466 static int insert_equation_as_loop(CloogDomain
*domain
, CloogConstraint
*upper
,
1467 CloogConstraint
*lower
, int level
, struct clast_stmt
***next
,
1470 const char *iterator
= cloog_names_name_at_level(infos
->names
, level
);
1471 struct clast_expr
*e1
, *e2
;
1472 struct clast_for
*f
;
1474 e2
= clast_bound_from_constraint(upper
, level
, infos
->names
);
1475 if (!cloog_constraint_is_valid(lower
))
1476 e1
= clast_expr_copy(e2
);
1478 e1
= clast_bound_from_constraint(lower
, level
, infos
->names
);
1480 f
= new_clast_for(domain
, iterator
, e1
, e2
, infos
->stride
[level
-1]);
1484 cloog_constraint_release(lower
);
1485 cloog_constraint_release(upper
);
1491 * insert_equation function:
1492 * This function inserts an equality
1493 * constraint according to an element in the clast.
1494 * Returns 1 if the calling function should recurse into inner loops.
1496 * An equality can be preceded by a 'modulo guard'.
1497 * For instance, consider the constraint i -2*j = 0 and the
1498 * element j: pprint_equality should return 'if(i%2==0) { j = i/2 ;'.
1499 * - matrix is the polyhedron containing all the constraints,
1500 * - num is the line number of the constraint in matrix we want to print,
1501 * - level is the column number of the element in matrix we want to use,
1502 * - the infos structure gives the user some options about code printing,
1503 * the number of parameters in matrix (nb_par), and the arrays of iterator
1504 * names and parameters (iters and params).
1506 * - November 13th 2001: first version.
1507 * - June 26th 2003: simplification of the modulo guards (remove parts such as
1508 * modulo is 0, compare vivien or vivien2 with a previous
1509 * version for an idea).
1510 * - June 29th 2003: non-unit strides support.
1511 * - July 14th 2003: (debug) no more print the constant in the modulo guard when
1512 * it was previously included in a stride calculation.
1514 static int insert_equation(CloogDomain
*domain
, CloogConstraint
*upper
,
1515 CloogConstraint
*lower
, int level
, struct clast_stmt
1516 ***next
, CloogInfos
*infos
)
1518 struct clast_expr
*e
;
1519 struct clast_assignment
*ass
;
1521 if (!infos
->options
->otl
)
1522 return insert_equation_as_loop(domain
, upper
, lower
, level
, next
, infos
);
1524 if (!insert_modulo_guard(upper
, lower
, level
, next
, infos
)) {
1525 cloog_constraint_release(lower
);
1526 cloog_constraint_release(upper
);
1531 if (cloog_constraint_is_valid(lower
) ||
1532 !clast_equal_add(infos
->equal
, NULL
, level
, upper
, infos
))
1533 { /* Finally, the equality. */
1535 /* If we have to make a block by dimension, we start the block. Function
1536 * pprint knows if there is an equality, if this is the case, it checks
1537 * for the same following condition to close the brace.
1539 if (infos
->options
->block
) {
1540 struct clast_block
*b
= new_clast_block();
1545 e
= clast_bound_from_constraint(upper
, level
, infos
->names
);
1546 ass
= new_clast_assignment(cloog_names_name_at_level(infos
->names
, level
), e
);
1548 **next
= &ass
->stmt
;
1549 *next
= &(**next
)->next
;
1552 cloog_constraint_release(lower
);
1553 cloog_constraint_release(upper
);
1560 * Insert a loop that is executed exactly once as an assignment.
1561 * In particular, the loop
1563 * for (i = e; i <= e; ++i) {
1573 static void insert_otl_for(CloogConstraintSet
*constraints
, int level
,
1574 struct clast_expr
*e
, struct clast_stmt
***next
, CloogInfos
*infos
)
1576 const char *iterator
;
1578 iterator
= cloog_names_name_at_level(infos
->names
, level
);
1580 if (!clast_equal_add(infos
->equal
, constraints
, level
,
1581 cloog_constraint_invalid(), infos
)) {
1582 struct clast_assignment
*ass
;
1583 if (infos
->options
->block
) {
1584 struct clast_block
*b
= new_clast_block();
1588 ass
= new_clast_assignment(iterator
, e
);
1589 **next
= &ass
->stmt
;
1590 *next
= &(**next
)->next
;
1598 * Insert a loop that is executed at most once as an assignment followed
1599 * by a guard. In particular, the loop
1601 * for (i = e1; i <= e2; ++i) {
1613 static void insert_guarded_otl_for(CloogConstraintSet
*constraints
, int level
,
1614 struct clast_expr
*e1
, struct clast_expr
*e2
,
1615 struct clast_stmt
***next
, CloogInfos
*infos
)
1617 const char *iterator
;
1618 struct clast_assignment
*ass
;
1619 struct clast_guard
*guard
;
1621 iterator
= cloog_names_name_at_level(infos
->names
, level
);
1623 if (infos
->options
->block
) {
1624 struct clast_block
*b
= new_clast_block();
1628 ass
= new_clast_assignment(iterator
, e1
);
1629 **next
= &ass
->stmt
;
1630 *next
= &(**next
)->next
;
1632 guard
= new_clast_guard(1);
1633 guard
->eq
[0].sign
= -1;
1634 guard
->eq
[0].LHS
= &new_clast_term(infos
->state
->one
,
1635 &new_clast_name(iterator
)->expr
)->expr
;
1636 guard
->eq
[0].RHS
= e2
;
1638 **next
= &guard
->stmt
;
1639 *next
= &guard
->then
;
1644 * insert_for function:
1645 * This function inserts a for loop in the clast.
1646 * Returns 1 if the calling function should recurse into inner loops.
1648 * A loop header according to an element is the conjunction of a minimum and a
1649 * maximum on a given element (they give the loop bounds).
1650 * For instance, considering these constraints and the element j:
1654 * this function should return 'for (j=max(-i+9*M,4*M),j<=5*M;j++) {'.
1655 * - constraints contains all constraints,
1656 * - level is the column number of the element in matrix we want to use,
1657 * - otl is set if the loop is executed at most once,
1658 * - the infos structure gives the user some options about code printing,
1659 * the number of parameters in matrix (nb_par), and the arrays of iterator
1660 * names and parameters (iters and params).
1662 static int insert_for(CloogDomain
*domain
, CloogConstraintSet
*constraints
,
1663 int level
, int otl
, struct clast_stmt
***next
,
1666 const char *iterator
;
1667 struct clast_expr
*e1
;
1668 struct clast_expr
*e2
;
1670 e1
= clast_minmax(constraints
, level
, 1, 0, 1, 0, infos
);
1671 e2
= clast_minmax(constraints
, level
, 0, 0, 0, 0, infos
);
1673 if (clast_expr_is_bigger_constant(e1
, e2
)) {
1674 free_clast_expr(e1
);
1675 free_clast_expr(e2
);
1679 /* If min and max are not equal there is a 'for' else, there is a '='.
1680 * In the special case e1 = e2 = NULL, this is an infinite loop
1681 * so this is not a '='.
1683 if (e1
&& e2
&& infos
->options
->otl
&& clast_expr_equal(e1
, e2
)) {
1684 free_clast_expr(e2
);
1685 insert_otl_for(constraints
, level
, e1
, next
, infos
);
1687 insert_guarded_otl_for(constraints
, level
, e1
, e2
, next
, infos
);
1689 struct clast_for
*f
;
1690 iterator
= cloog_names_name_at_level(infos
->names
, level
);
1692 f
= new_clast_for(domain
, iterator
, e1
, e2
, infos
->stride
[level
-1]);
1702 * insert_block function:
1703 * This function inserts a statement block.
1704 * - block is the statement block,
1705 * - level is the number of loops enclosing the statement,
1706 * - the infos structure gives the user some options about code printing,
1707 * the number of parameters in domain (nb_par), and the arrays of iterator
1708 * names and parameters (iters and params).
1710 * - September 21th 2003: first version (pick from pprint function).
1712 static void insert_block(CloogDomain
*domain
, CloogBlock
*block
, int level
,
1713 struct clast_stmt
***next
, CloogInfos
*infos
)
1715 CloogStatement
* statement
;
1716 struct clast_stmt
*subs
;
1721 for (statement
= block
->statement
; statement
; statement
= statement
->next
) {
1722 CloogStatement
*s_next
= statement
->next
;
1724 subs
= clast_equal(level
,infos
);
1726 statement
->next
= NULL
;
1727 **next
= &new_clast_user_stmt(domain
, statement
, subs
)->stmt
;
1728 statement
->next
= s_next
;
1729 *next
= &(**next
)->next
;
1735 * insert_loop function:
1736 * This function converts the content of a CloogLoop structure (loop) into a
1737 * clast_stmt (inserted at **next).
1738 * The iterator (level) of
1739 * the current loop is given by 'level': this is the column number of the
1740 * domain corresponding to the current loop iterator. The data of a loop are
1741 * written in this order:
1742 * 1. The guard of the loop, i.e. each constraint in the domain that does not
1743 * depend on the iterator (when the entry in the column 'level' is 0).
1744 * 2. The iteration domain of the iterator, given by the constraints in the
1745 * domain depending on the iterator, i.e.:
1746 * * an equality if the iterator has only one value (possibly preceded by
1747 * a guard verifying if this value is integral), *OR*
1748 * * a loop from the minimum possible value of the iterator to the maximum
1750 * 3. The included statement block.
1751 * 4. The inner loops (recursive call).
1752 * 5. The following loops (recursive call).
1753 * - level is the recursion level or the iteration level that we are printing,
1754 * - the infos structure gives the user some options about code printing,
1755 * the number of parameters in domain (nb_par), and the arrays of iterator
1756 * names and parameters (iters and params).
1758 * - November 2nd 2001: first version.
1759 * - March 6th 2003: infinite domain support.
1760 * - April 19th 2003: (debug) NULL loop support.
1761 * - June 29th 2003: non-unit strides support.
1762 * - April 28th 2005: (debug) level is level+equality when print statement!
1763 * - June 16th 2005: (debug) the N. Vasilache normalization step has been
1764 * added to avoid iteration duplication (see DaeGon Kim
1765 * bug in cloog_program_generate). Try vasilache.cloog
1766 * with and without the call to cloog_polylib_matrix_normalize,
1767 * using -f 8 -l 9 options for an idea.
1768 * - September 15th 2005: (debug) don't close equality braces when unnecessary.
1769 * - October 16th 2005: (debug) scalar value is saved for next loops.
1771 static void insert_loop(CloogLoop
* loop
, int level
,
1772 struct clast_stmt
***next
, CloogInfos
*infos
)
1775 CloogConstraintSet
*constraints
, *temp
;
1776 struct clast_stmt
**top
= *next
;
1777 CloogConstraint
*i
, *j
;
1780 /* It can happen that loop be NULL when an input polyhedron is empty. */
1784 /* The constraints do not always have a shape that allows us to generate code from it,
1785 * thus we normalize it, we also simplify it with the equalities.
1787 temp
= cloog_domain_constraints(loop
->domain
);
1788 cloog_constraint_set_normalize(temp
,level
);
1789 constraints
= cloog_constraint_set_simplify(temp
,infos
->equal
,level
,
1790 infos
->names
->nb_parameters
);
1791 cloog_constraint_set_free(temp
);
1793 infos
->stride
[level
- 1] = loop
->stride
;
1794 infos
->stride_level
++;
1797 /* First of all we have to print the guard. */
1798 insert_guard(constraints
,level
, next
, infos
);
1800 if (level
&& cloog_constraint_set_contains_level(constraints
, level
,
1801 infos
->names
->nb_parameters
)) {
1802 /* We scan all the constraints to know in which case we are :
1803 * [[if] equation] or [for].
1805 if (cloog_constraint_is_valid(i
=
1806 cloog_constraint_set_defining_equality(constraints
, level
))) {
1807 empty_loop
= !insert_equation(loop
->unsimplified
, i
,
1808 cloog_constraint_invalid(), level
, next
,
1811 } else if (cloog_constraint_is_valid(i
=
1812 cloog_constraint_set_defining_inequalities(constraints
,
1813 level
, &j
, infos
->names
->nb_parameters
))) {
1814 empty_loop
= !insert_equation(loop
->unsimplified
, i
, j
, level
, next
,
1817 empty_loop
= !insert_for(loop
->unsimplified
, constraints
, level
,
1818 loop
->otl
, next
, infos
);
1822 /* Finally, if there is an included statement block, print it. */
1823 insert_block(loop
->unsimplified
, loop
->block
, level
+equality
, next
, infos
);
1825 /* Go to the next level. */
1826 if (loop
->inner
!= NULL
)
1827 insert_loop(loop
->inner
, level
+1, next
, infos
);
1831 cloog_equal_del(infos
->equal
,level
);
1832 infos
->stride_level
--;
1834 cloog_constraint_set_free(constraints
);
1836 /* Go to the next loop on the same level. */
1838 top
= &(*top
)->next
;
1839 if (loop
->next
!= NULL
)
1840 insert_loop(loop
->next
, level
, &top
,infos
);
1844 struct clast_stmt
*cloog_clast_create(CloogProgram
*program
,
1845 CloogOptions
*options
)
1847 CloogInfos
*infos
= ALLOC(CloogInfos
);
1849 struct clast_stmt
*root
= &new_clast_root(program
->names
)->stmt
;
1850 struct clast_stmt
**next
= &root
->next
;
1852 infos
->state
= options
->state
;
1853 infos
->names
= program
->names
;
1854 infos
->options
= options
;
1855 infos
->scaldims
= program
->scaldims
;
1856 infos
->nb_scattdims
= program
->nb_scattdims
;
1858 /* Allocation for the array of strides, there is a +1 since the statement can
1859 * be included inside an external loop without iteration domain.
1861 nb_levels
= program
->names
->nb_scattering
+program
->names
->nb_iterators
+1;
1862 infos
->stride
= ALLOCN(CloogStride
*, nb_levels
);
1863 infos
->stride_level
= 0;
1865 infos
->equal
= cloog_equal_alloc(nb_levels
,
1866 nb_levels
, program
->names
->nb_parameters
);
1868 insert_loop(program
->loop
, 0, &next
, infos
);
1870 cloog_equal_free(infos
->equal
);
1872 free(infos
->stride
);
1879 struct clast_stmt
*cloog_clast_create_from_input(CloogInput
*input
,
1880 CloogOptions
*options
)
1882 CloogProgram
*program
;
1883 struct clast_stmt
*root
;
1885 program
= cloog_program_alloc(input
->context
, input
->ud
, options
);
1888 program
= cloog_program_generate(program
, options
);
1890 root
= cloog_clast_create(program
, options
);
1891 cloog_program_free(program
);