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
,
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
{
760 struct clast_reduction
*r
;
764 /* Should constraint "c" be considered by clast_minmax?
766 static int valid_bound(CloogConstraint
*c
, struct clast_minmax_data
*d
)
768 if (d
->max
&& !cloog_constraint_is_lower_bound(c
, d
->level
- 1))
770 if (!d
->max
&& !cloog_constraint_is_upper_bound(c
, d
->level
- 1))
772 if (cloog_constraint_is_equality(c
))
774 if (d
->guard
&& cloog_constraint_involves(c
, d
->guard
- 1))
781 /* Increment n for each bound that should be considered by clast_minmax.
783 static int count_bounds(CloogConstraint
*c
, void *user
)
785 struct clast_minmax_data
*d
= (struct clast_minmax_data
*) user
;
787 if (!valid_bound(c
, d
))
796 /* Update the given lower bound based on stride information.
797 * In some backends, the lower bounds are updated from within
798 * cloog_loop_stride, but other backends leave the updating to
799 * this function. In the later case, the original lower bound
800 * is known to be a constant.
801 * If the bound turns out not to be a constant, we know we
802 * are in the former case and nothing needs to be done.
803 * If the bound has already been updated and it just happens
804 * to be a constant, then this function performs an identity
805 * operation on the constant.
807 static void update_lower_bound(struct clast_expr
*expr
, int level
,
810 struct clast_term
*t
;
811 if (stride
->constraint
)
813 if (expr
->type
!= clast_expr_term
)
815 t
= (struct clast_term
*)expr
;
818 cloog_int_sub(t
->val
, t
->val
, stride
->offset
);
819 cloog_int_cdiv_q(t
->val
, t
->val
, stride
->stride
);
820 cloog_int_mul(t
->val
, t
->val
, stride
->stride
);
821 cloog_int_add(t
->val
, t
->val
, stride
->offset
);
825 /* Add all relevant bounds to r->elts and update lower bounds
826 * based on stride information.
828 static int collect_bounds(CloogConstraint
*c
, void *user
)
830 struct clast_minmax_data
*d
= (struct clast_minmax_data
*) user
;
832 if (!valid_bound(c
, d
))
835 d
->r
->elts
[d
->n
] = clast_bound_from_constraint(c
, d
->level
,
837 if (d
->lower_bound
&& d
->infos
->stride
[d
->level
- 1]) {
838 update_lower_bound(d
->r
->elts
[d
->n
], d
->level
,
839 d
->infos
->stride
[d
->level
- 1]);
849 * clast_minmax function:
850 * This function returns a clast_expr containing the printing of a minimum or a
851 * maximum of the 'right parts' of all constraints according to an element.
852 * For instance consider the constraints:
856 * if we are looking for the minimum for the element j, the function should
857 * return 'max(ceild(3*i+M,2),-2*i)'.
858 * - constraints is the constraints,
859 * - level is the column number in domain of the element we want to use,
860 * - max is a boolean set to 1 if we are looking for a maximum, 0 for a minimum,
861 * - guard is set to 0 if there is no guard, and set to the level of the element
862 * with a guard otherwise (then the function gives the max or the min only
863 * for the constraint where the guarded coefficient is 0),
864 * - lower is set to 1 if the maximum is to be used a lower bound on a loop
865 * - the infos structure gives the user some options about code printing,
866 * the number of parameters in domain (nb_par), and the arrays of iterator
867 * names and parameters (iters and params).
869 * - November 2nd 2001: first version.
871 static struct clast_expr
*clast_minmax(CloogConstraintSet
*constraints
,
872 int level
, int max
, int guard
,
876 struct clast_minmax_data data
= { level
, max
, guard
, lower_bound
, infos
};
880 cloog_constraint_set_foreach_constraint(constraints
, count_bounds
, &data
);
884 data
.r
= new_clast_reduction(max
? clast_red_max
: clast_red_min
, data
.n
);
887 cloog_constraint_set_foreach_constraint(constraints
, collect_bounds
, &data
);
889 clast_reduction_sort(data
.r
);
890 return &data
.r
->expr
;
895 * Insert modulo guards defined by existentially quantified dimensions,
896 * not involving the given level.
898 * This function is called from within insert_guard.
899 * Any constraint used in constructing a modulo guard is removed
900 * from the constraint set to avoid insert_guard
901 * adding a duplicate (pair of) constraint(s).
903 static void insert_extra_modulo_guards(CloogConstraintSet
*constraints
,
904 int level
, struct clast_stmt
***next
, CloogInfos
*infos
)
909 CloogConstraint
*upper
, *lower
;
911 total_dim
= cloog_constraint_set_total_dimension(constraints
);
912 nb_iter
= cloog_constraint_set_n_iterators(constraints
,
913 infos
->names
->nb_parameters
);
915 for (i
= total_dim
- infos
->names
->nb_parameters
; i
>= nb_iter
+ 1; i
--) {
916 if (cloog_constraint_is_valid(upper
=
917 cloog_constraint_set_defining_equality(constraints
, i
))) {
918 if (!level
|| (nb_iter
< level
) ||
919 !cloog_constraint_involves(upper
, level
-1)) {
920 insert_modulo_guard(upper
,
921 cloog_constraint_invalid(), i
, next
, infos
);
922 cloog_constraint_clear(upper
);
924 cloog_constraint_release(upper
);
925 } else if (cloog_constraint_is_valid(upper
=
926 cloog_constraint_set_defining_inequalities(constraints
,
927 i
, &lower
, infos
->names
->nb_parameters
))) {
928 if (!level
|| (nb_iter
< level
) ||
929 !cloog_constraint_involves(upper
, level
-1)) {
930 insert_modulo_guard(upper
, lower
, i
, next
, infos
);
931 cloog_constraint_clear(upper
);
932 cloog_constraint_clear(lower
);
934 cloog_constraint_release(upper
);
935 cloog_constraint_release(lower
);
941 static int clear_lower_bound_at_level(CloogConstraint
*c
, void *user
)
943 int level
= *(int *)user
;
945 if (cloog_constraint_is_lower_bound(c
, level
- 1))
946 cloog_constraint_clear(c
);
952 static int clear_upper_bound_at_level(CloogConstraint
*c
, void *user
)
954 int level
= *(int *)user
;
956 if (cloog_constraint_is_upper_bound(c
, level
- 1))
957 cloog_constraint_clear(c
);
963 /* Temporary structure for communication between insert_guard and
964 * its cloog_constraint_set_foreach_constraint callback function.
966 struct clast_guard_data
{
972 CloogConstraintSet
*copy
;
973 struct clast_guard
*g
;
977 static int guard_count_bounds(CloogConstraint
*c
, void *user
)
979 struct clast_guard_data
*d
= (struct clast_guard_data
*) user
;
987 /* Insert a guard, if necesessary, for constraint j.
989 static int insert_guard_constraint(CloogConstraint
*j
, void *user
)
991 struct clast_guard_data
*d
= (struct clast_guard_data
*) user
;
993 struct clast_expr
*v
;
994 struct clast_term
*t
;
996 if (!cloog_constraint_involves(j
, d
->i
- 1))
999 if (d
->level
&& d
->nb_iter
>= d
->level
&&
1000 cloog_constraint_involves(j
, d
->level
- 1))
1003 v
= cloog_constraint_variable_expr(j
, d
->i
, d
->infos
->names
);
1004 d
->g
->eq
[d
->n
].LHS
= &(t
= new_clast_term(d
->infos
->state
->one
, v
))->expr
;
1005 if (!d
->level
|| cloog_constraint_is_equality(j
)) {
1006 /* put the "denominator" in the LHS */
1007 cloog_constraint_coefficient_get(j
, d
->i
- 1, &t
->val
);
1008 cloog_constraint_coefficient_set(j
, d
->i
- 1, d
->infos
->state
->one
);
1009 if (cloog_int_is_neg(t
->val
)) {
1010 cloog_int_neg(t
->val
, t
->val
);
1011 cloog_constraint_coefficient_set(j
, d
->i
- 1, d
->infos
->state
->negone
);
1013 if (d
->level
|| cloog_constraint_is_equality(j
))
1014 d
->g
->eq
[d
->n
].sign
= 0;
1015 else if (cloog_constraint_is_lower_bound(j
, d
->i
- 1))
1016 d
->g
->eq
[d
->n
].sign
= 1;
1018 d
->g
->eq
[d
->n
].sign
= -1;
1019 d
->g
->eq
[d
->n
].RHS
= clast_bound_from_constraint(j
, d
->i
, d
->infos
->names
);
1023 if (cloog_constraint_is_lower_bound(j
, d
->i
- 1)) {
1025 d
->g
->eq
[d
->n
].sign
= 1;
1028 d
->g
->eq
[d
->n
].sign
= -1;
1031 guarded
= (d
->nb_iter
>= d
->level
) ? d
->level
: 0 ;
1032 d
->g
->eq
[d
->n
].RHS
= clast_minmax(d
->copy
, d
->i
, minmax
, guarded
, 0,
1037 /* 'elimination' of the current constraint, this avoid to use one
1038 * constraint more than once. The current line is always eliminated,
1039 * and the next lines if they are in a min or a max.
1041 cloog_constraint_clear(j
);
1046 cloog_constraint_set_foreach_constraint(d
->copy
,
1047 clear_lower_bound_at_level
, &d
->i
);
1048 else if (minmax
== 0)
1049 cloog_constraint_set_foreach_constraint(d
->copy
,
1050 clear_upper_bound_at_level
, &d
->i
);
1057 * insert_guard function:
1058 * This function inserts a guard in the clast.
1059 * A guard on an element (level) is :
1060 * -> the conjunction of all the existing constraints where the coefficient of
1061 * this element is 0 if the element is an iterator,
1062 * -> the conjunction of all the existing constraints if the element isn't an
1064 * For instance, considering these constraints and the element j:
1067 * this function should return 'if (2*i+M>=0) {'.
1068 * - matrix is the polyhedron containing all the constraints,
1069 * - level is the column number of the element in matrix we want to use,
1070 * - the infos structure gives the user some options about code printing,
1071 * the number of parameters in matrix (nb_par), and the arrays of iterator
1072 * names and parameters (iters and params).
1074 * - November 3rd 2001: first version.
1075 * - November 14th 2001: a lot of 'purifications'.
1076 * - July 31th 2002: (debug) some guard parts are no more redundants.
1077 * - August 12th 2002: polyhedra union ('or' conditions) are now supported.
1078 * - October 27th 2005: polyhedra union ('or' conditions) are no more supported
1079 * (the need came from loop_simplify that may result in
1080 * domain unions, now it should be fixed directly in
1081 * cloog_loop_simplify).
1083 static void insert_guard(CloogConstraintSet
*constraints
, int level
,
1084 struct clast_stmt
***next
, CloogInfos
*infos
)
1087 struct clast_guard_data data
= { level
, infos
, 0 };
1092 data
.copy
= cloog_constraint_set_copy(constraints
);
1094 insert_extra_modulo_guards(data
.copy
, level
, next
, infos
);
1096 cloog_constraint_set_foreach_constraint(constraints
,
1097 guard_count_bounds
, &data
);
1099 data
.g
= new_clast_guard(data
.n
);
1102 /* Well, it looks complicated because I wanted to have a particular, more
1103 * readable, ordering, obviously this function may be far much simpler !
1105 data
.nb_iter
= cloog_constraint_set_n_iterators(constraints
,
1106 infos
->names
->nb_parameters
);
1108 /* We search for guard parts. */
1109 total_dim
= cloog_constraint_set_total_dimension(constraints
);
1110 for (data
.i
= 1; data
.i
<= total_dim
; data
.i
++)
1111 cloog_constraint_set_foreach_constraint(data
.copy
,
1112 insert_guard_constraint
, &data
);
1114 cloog_constraint_set_free(data
.copy
);
1118 clast_guard_sort(data
.g
);
1119 **next
= &data
.g
->stmt
;
1120 *next
= &data
.g
->then
;
1122 free_clast_stmt(&data
.g
->stmt
);
1126 * Check if the constant "cst" satisfies the modulo guard that
1127 * would be introduced by insert_computed_modulo_guard.
1128 * The constant is assumed to have been reduced prior to calling
1131 static int constant_modulo_guard_is_satisfied(CloogConstraint
*lower
,
1132 cloog_int_t bound
, cloog_int_t cst
)
1134 if (cloog_constraint_is_valid(lower
))
1135 return cloog_int_le(cst
, bound
);
1137 return cloog_int_is_zero(cst
);
1141 * Insert a modulo guard "r % mod == 0" or "r % mod <= bound",
1142 * depending on whether lower represents a valid constraint.
1144 static void insert_computed_modulo_guard(struct clast_reduction
*r
,
1145 CloogConstraint
*lower
, cloog_int_t mod
, cloog_int_t bound
,
1146 struct clast_stmt
***next
)
1148 struct clast_expr
*e
;
1149 struct clast_guard
*g
;
1151 e
= &new_clast_binary(clast_bin_mod
, &r
->expr
, mod
)->expr
;
1152 g
= new_clast_guard(1);
1153 if (!cloog_constraint_is_valid(lower
)) {
1155 cloog_int_set_si(bound
, 0);
1156 g
->eq
[0].RHS
= &new_clast_term(bound
, NULL
)->expr
;
1160 g
->eq
[0].RHS
= &new_clast_term(bound
, NULL
)->expr
;
1169 /* Try and eliminate coefficients from a modulo constraint based on
1170 * stride information of an earlier level.
1171 * The modulo of the constraint being constructed is "m".
1172 * The stride information at level "level" is given by "stride"
1173 * and indicated that the iterator i at level "level" is equal to
1174 * some expression modulo stride->stride.
1175 * If stride->stride is a multiple of "m' then i is also equal to
1176 * the expression modulo m and so we can eliminate the coefficient of i.
1178 * If stride->constraint is NULL, then i has a constant value modulo m, stored
1179 * stride->offset. We simply multiply this constant with the coefficient
1180 * of i and add the result to the constant term, reducing it modulo m.
1182 * If stride->constraint is not NULL, then it is a constraint of the form
1186 * with s equal to stride->stride, e an expression in terms of the
1187 * parameters and earlier iterators and a some arbitrary expression
1188 * in terms of existentially quantified variables.
1189 * stride->factor is a value f such that f * k = -1 mod s.
1190 * Adding stride->constraint f * c times to the current modulo constraint,
1191 * with c the coefficient of i eliminates i in favor of parameters and
1192 * earlier variables.
1194 static void eliminate_using_stride_constraint(cloog_int_t
*line
, int len
,
1195 int nb_iter
, CloogStride
*stride
, int level
, cloog_int_t m
)
1199 if (!cloog_int_is_divisible_by(stride
->stride
, m
))
1202 if (stride
->constraint
) {
1208 cloog_int_mul(t
, line
[level
], stride
->factor
);
1209 for (i
= 1; i
< level
; ++i
) {
1210 cloog_constraint_coefficient_get(stride
->constraint
,
1212 cloog_int_addmul(line
[i
], t
, v
);
1213 cloog_int_fdiv_r(line
[i
], line
[i
], m
);
1215 s_len
= cloog_constraint_total_dimension(stride
->constraint
)+2;
1216 for (i
= nb_iter
+ 1; i
<= len
- 2; ++i
) {
1217 cloog_constraint_coefficient_get(stride
->constraint
,
1218 i
- (len
- s_len
) - 1, &v
);
1219 cloog_int_addmul(line
[i
], t
, v
);
1220 cloog_int_fdiv_r(line
[i
], line
[i
], m
);
1222 cloog_constraint_constant_get(stride
->constraint
, &v
);
1223 cloog_int_addmul(line
[len
- 1], t
, v
);
1224 cloog_int_fdiv_r(line
[len
- 1], line
[len
- 1], m
);
1228 cloog_int_addmul(line
[len
- 1], line
[level
], stride
->offset
);
1229 cloog_int_fdiv_r(line
[len
- 1], line
[len
- 1], m
);
1232 cloog_int_set_si(line
[level
], 0);
1236 /* Temporary structure for communication between insert_modulo_guard and
1237 * its cloog_constraint_set_foreach_constraint callback function.
1239 struct clast_modulo_guard_data
{
1240 CloogConstraint
*lower
;
1242 struct clast_stmt
***next
;
1245 cloog_int_t val
, bound
;
1249 /* Insert a modulo guard for constraint c.
1250 * The constraint may be either an equality or an inequality.
1251 * Since this function returns -1, it is only called on a single constraint.
1252 * In case of an inequality, the constraint is usually an upper bound
1253 * on d->level. However, if this variable is an existentially
1254 * quantified variable, the upper bound constraint may get removed
1255 * as trivially holding and then this function is called with
1256 * a lower bound instead. In this case, we need to adjust the constraint
1257 * based on the sum of the constant terms of the lower and upper bound
1258 * stored in d->bound.
1260 static int insert_modulo_guard_constraint(CloogConstraint
*c
, void *user
)
1262 struct clast_modulo_guard_data
*d
= (struct clast_modulo_guard_data
*) user
;
1263 int level
= d
->level
;
1264 CloogInfos
*infos
= d
->infos
;
1265 int i
, nb_elts
= 0, len
, len2
, nb_iter
, nb_par
;
1267 struct cloog_vec
*line_vector
;
1270 len
= cloog_constraint_total_dimension(c
) + 2;
1271 len2
= cloog_equal_total_dimension(infos
->equal
) + 2;
1272 nb_par
= infos
->names
->nb_parameters
;
1273 nb_iter
= len
- 2 - nb_par
;
1275 line_vector
= cloog_vec_alloc(len
);
1276 line
= line_vector
->p
;
1277 cloog_constraint_copy_coefficients(c
, line
+ 1);
1279 if (cloog_int_is_pos(line
[level
])) {
1280 cloog_seq_neg(line
+ 1, line
+ 1, len
- 1);
1281 if (!cloog_constraint_is_equality(c
))
1282 cloog_int_add(line
[len
- 1], line
[len
- 1], d
->bound
);
1284 cloog_int_neg(line
[level
], line
[level
]);
1285 assert(cloog_int_is_pos(line
[level
]));
1288 for (i
= 1; i
<= len
-1; ++i
) {
1291 cloog_int_fdiv_r(line
[i
], line
[i
], line
[level
]);
1292 if (cloog_int_is_zero(line
[i
]))
1300 if (nb_elts
|| !cloog_int_is_zero(line
[len
-1])) {
1301 struct clast_reduction
*r
;
1304 r
= new_clast_reduction(clast_red_sum
, nb_elts
+ 1);
1307 /* First, the modulo guard : the iterators... */
1309 if (i
> infos
->stride_level
)
1310 i
= infos
->stride_level
;
1312 eliminate_using_stride_constraint(line
, len
, nb_iter
,
1313 infos
->stride
[i
- 1], i
, line
[level
]);
1314 for (i
=1;i
<=nb_iter
;i
++) {
1315 if (i
== level
|| cloog_int_is_zero(line
[i
]))
1318 name
= cloog_names_name_at_level(infos
->names
, i
);
1320 r
->elts
[nb_elts
++] = &new_clast_term(line
[i
],
1321 &new_clast_name(name
)->expr
)->expr
;
1324 /* ...the parameters... */
1325 for (i
=nb_iter
+1;i
<=len
-2;i
++) {
1326 if (cloog_int_is_zero(line
[i
]))
1329 name
= infos
->names
->parameters
[i
-nb_iter
-1] ;
1330 r
->elts
[nb_elts
++] = &new_clast_term(line
[i
],
1331 &new_clast_name(name
)->expr
)->expr
;
1334 constant
= nb_elts
== 0;
1335 /* ...the constant. */
1336 if (!cloog_int_is_zero(line
[len
-1]))
1337 r
->elts
[nb_elts
++] = &new_clast_term(line
[len
-1], NULL
)->expr
;
1339 /* our initial computation may have been an overestimate */
1343 d
->empty
= !constant_modulo_guard_is_satisfied(d
->lower
, d
->bound
,
1345 free_clast_reduction(r
);
1347 insert_computed_modulo_guard(r
, d
->lower
, line
[level
], d
->bound
,
1351 cloog_vec_free(line_vector
);
1358 * insert_modulo_guard:
1359 * This function inserts a modulo guard corresponding to an equality
1360 * or a pair of inequalities.
1361 * Returns 0 if the modulo guard is discovered to be unsatisfiable.
1363 * See insert_equation.
1364 * - matrix is the polyhedron containing all the constraints,
1365 * - upper and lower are the line numbers of the constraint in matrix
1366 * we want to print; in particular, if we want to print an equality,
1367 * then lower == -1 and upper is the row of the equality; if we want
1368 * to print an inequality, then upper is the row of the upper bound
1369 * and lower in the row of the lower bound
1370 * - level is the column number of the element in matrix we want to use,
1371 * - the infos structure gives the user some options about code printing,
1372 * the number of parameters in matrix (nb_par), and the arrays of iterator
1373 * names and parameters (iters and params).
1375 static int insert_modulo_guard(CloogConstraint
*upper
,
1376 CloogConstraint
*lower
, int level
,
1377 struct clast_stmt
***next
, CloogInfos
*infos
)
1380 CloogConstraintSet
*set
;
1381 struct clast_modulo_guard_data data
= { lower
, level
, next
, infos
, 0 };
1383 cloog_int_init(data
.val
);
1384 cloog_constraint_coefficient_get(upper
, level
-1, &data
.val
);
1385 if (cloog_int_is_one(data
.val
) || cloog_int_is_neg_one(data
.val
)) {
1386 cloog_int_clear(data
.val
);
1390 nb_par
= infos
->names
->nb_parameters
;
1392 cloog_int_init(data
.bound
);
1393 /* Check if would be emitting the redundant constraint mod(e,m) <= m-1 */
1394 if (cloog_constraint_is_valid(lower
)) {
1395 cloog_constraint_constant_get(upper
, &data
.val
);
1396 cloog_constraint_constant_get(lower
, &data
.bound
);
1397 cloog_int_add(data
.bound
, data
.val
, data
.bound
);
1398 cloog_constraint_coefficient_get(lower
, level
-1, &data
.val
);
1399 cloog_int_sub_ui(data
.val
, data
.val
, 1);
1400 if (cloog_int_eq(data
.val
, data
.bound
)) {
1401 cloog_int_clear(data
.val
);
1402 cloog_int_clear(data
.bound
);
1407 if (cloog_constraint_needs_reduction(upper
, level
)) {
1408 set
= cloog_constraint_set_for_reduction(upper
, lower
);
1409 set
= cloog_constraint_set_reduce(set
, level
, infos
->equal
,
1410 nb_par
, &data
.bound
);
1411 cloog_constraint_set_foreach_constraint(set
,
1412 insert_modulo_guard_constraint
, &data
);
1413 cloog_constraint_set_free(set
);
1415 insert_modulo_guard_constraint(upper
, &data
);
1417 cloog_int_clear(data
.val
);
1418 cloog_int_clear(data
.bound
);
1425 * We found an equality or a pair of inequalities identifying
1426 * a loop with a single iteration, but the user wants us to generate
1427 * a loop anyway, so we do it here.
1429 static int insert_equation_as_loop(CloogDomain
*domain
, CloogConstraint
*upper
,
1430 CloogConstraint
*lower
, int level
, struct clast_stmt
***next
,
1433 const char *iterator
= cloog_names_name_at_level(infos
->names
, level
);
1434 struct clast_expr
*e1
, *e2
;
1435 struct clast_for
*f
;
1437 e2
= clast_bound_from_constraint(upper
, level
, infos
->names
);
1438 if (!cloog_constraint_is_valid(lower
))
1439 e1
= clast_expr_copy(e2
);
1441 e1
= clast_bound_from_constraint(lower
, level
, infos
->names
);
1443 f
= new_clast_for(domain
, iterator
, e1
, e2
, infos
->stride
[level
-1]);
1447 cloog_constraint_release(lower
);
1448 cloog_constraint_release(upper
);
1454 * insert_equation function:
1455 * This function inserts an equality
1456 * constraint according to an element in the clast.
1457 * Returns 1 if the calling function should recurse into inner loops.
1459 * An equality can be preceded by a 'modulo guard'.
1460 * For instance, consider the constraint i -2*j = 0 and the
1461 * element j: pprint_equality should return 'if(i%2==0) { j = i/2 ;'.
1462 * - matrix is the polyhedron containing all the constraints,
1463 * - num is the line number of the constraint in matrix we want to print,
1464 * - level is the column number of the element in matrix we want to use,
1465 * - the infos structure gives the user some options about code printing,
1466 * the number of parameters in matrix (nb_par), and the arrays of iterator
1467 * names and parameters (iters and params).
1469 * - November 13th 2001: first version.
1470 * - June 26th 2003: simplification of the modulo guards (remove parts such as
1471 * modulo is 0, compare vivien or vivien2 with a previous
1472 * version for an idea).
1473 * - June 29th 2003: non-unit strides support.
1474 * - July 14th 2003: (debug) no more print the constant in the modulo guard when
1475 * it was previously included in a stride calculation.
1477 static int insert_equation(CloogDomain
*domain
, CloogConstraint
*upper
,
1478 CloogConstraint
*lower
, int level
, struct clast_stmt
1479 ***next
, CloogInfos
*infos
)
1481 struct clast_expr
*e
;
1482 struct clast_assignment
*ass
;
1484 if (!infos
->options
->otl
)
1485 return insert_equation_as_loop(domain
, upper
, lower
, level
, next
, infos
);
1487 if (!insert_modulo_guard(upper
, lower
, level
, next
, infos
)) {
1488 cloog_constraint_release(lower
);
1489 cloog_constraint_release(upper
);
1494 if (cloog_constraint_is_valid(lower
) ||
1495 !clast_equal_add(infos
->equal
, NULL
, level
, upper
, infos
))
1496 { /* Finally, the equality. */
1498 /* If we have to make a block by dimension, we start the block. Function
1499 * pprint knows if there is an equality, if this is the case, it checks
1500 * for the same following condition to close the brace.
1502 if (infos
->options
->block
) {
1503 struct clast_block
*b
= new_clast_block();
1508 e
= clast_bound_from_constraint(upper
, level
, infos
->names
);
1509 ass
= new_clast_assignment(cloog_names_name_at_level(infos
->names
, level
), e
);
1511 **next
= &ass
->stmt
;
1512 *next
= &(**next
)->next
;
1515 cloog_constraint_release(lower
);
1516 cloog_constraint_release(upper
);
1523 * Insert a loop that is executed exactly once as an assignment.
1524 * In particular, the loop
1526 * for (i = e; i <= e; ++i) {
1536 static void insert_otl_for(CloogConstraintSet
*constraints
, int level
,
1537 struct clast_expr
*e
, struct clast_stmt
***next
, CloogInfos
*infos
)
1539 const char *iterator
;
1541 iterator
= cloog_names_name_at_level(infos
->names
, level
);
1543 if (!clast_equal_add(infos
->equal
, constraints
, level
,
1544 cloog_constraint_invalid(), infos
)) {
1545 struct clast_assignment
*ass
;
1546 if (infos
->options
->block
) {
1547 struct clast_block
*b
= new_clast_block();
1551 ass
= new_clast_assignment(iterator
, e
);
1552 **next
= &ass
->stmt
;
1553 *next
= &(**next
)->next
;
1561 * Insert a loop that is executed at most once as an assignment followed
1562 * by a guard. In particular, the loop
1564 * for (i = e1; i <= e2; ++i) {
1576 static void insert_guarded_otl_for(CloogConstraintSet
*constraints
, int level
,
1577 struct clast_expr
*e1
, struct clast_expr
*e2
,
1578 struct clast_stmt
***next
, CloogInfos
*infos
)
1580 const char *iterator
;
1581 struct clast_assignment
*ass
;
1582 struct clast_guard
*guard
;
1584 iterator
= cloog_names_name_at_level(infos
->names
, level
);
1586 if (infos
->options
->block
) {
1587 struct clast_block
*b
= new_clast_block();
1591 ass
= new_clast_assignment(iterator
, e1
);
1592 **next
= &ass
->stmt
;
1593 *next
= &(**next
)->next
;
1595 guard
= new_clast_guard(1);
1596 guard
->eq
[0].sign
= -1;
1597 guard
->eq
[0].LHS
= &new_clast_term(infos
->state
->one
,
1598 &new_clast_name(iterator
)->expr
)->expr
;
1599 guard
->eq
[0].RHS
= e2
;
1601 **next
= &guard
->stmt
;
1602 *next
= &guard
->then
;
1607 * insert_for function:
1608 * This function inserts a for loop in the clast.
1609 * Returns 1 if the calling function should recurse into inner loops.
1611 * A loop header according to an element is the conjunction of a minimum and a
1612 * maximum on a given element (they give the loop bounds).
1613 * For instance, considering these constraints and the element j:
1617 * this function should return 'for (j=max(-i+9*M,4*M),j<=5*M;j++) {'.
1618 * - constraints contains all constraints,
1619 * - level is the column number of the element in matrix we want to use,
1620 * - otl is set if the loop is executed at most once,
1621 * - the infos structure gives the user some options about code printing,
1622 * the number of parameters in matrix (nb_par), and the arrays of iterator
1623 * names and parameters (iters and params).
1625 static int insert_for(CloogDomain
*domain
, CloogConstraintSet
*constraints
,
1626 int level
, int otl
, struct clast_stmt
***next
,
1629 const char *iterator
;
1630 struct clast_expr
*e1
;
1631 struct clast_expr
*e2
;
1633 e1
= clast_minmax(constraints
, level
, 1, 0, 1, infos
);
1634 e2
= clast_minmax(constraints
, level
, 0, 0, 0, infos
);
1636 if (clast_expr_is_bigger_constant(e1
, e2
)) {
1637 free_clast_expr(e1
);
1638 free_clast_expr(e2
);
1642 /* If min and max are not equal there is a 'for' else, there is a '='.
1643 * In the special case e1 = e2 = NULL, this is an infinite loop
1644 * so this is not a '='.
1646 if (e1
&& e2
&& infos
->options
->otl
&& clast_expr_equal(e1
, e2
)) {
1647 free_clast_expr(e2
);
1648 insert_otl_for(constraints
, level
, e1
, next
, infos
);
1650 insert_guarded_otl_for(constraints
, level
, e1
, e2
, next
, infos
);
1652 struct clast_for
*f
;
1653 iterator
= cloog_names_name_at_level(infos
->names
, level
);
1655 f
= new_clast_for(domain
, iterator
, e1
, e2
, infos
->stride
[level
-1]);
1665 * insert_block function:
1666 * This function inserts a statement block.
1667 * - block is the statement block,
1668 * - level is the number of loops enclosing the statement,
1669 * - the infos structure gives the user some options about code printing,
1670 * the number of parameters in domain (nb_par), and the arrays of iterator
1671 * names and parameters (iters and params).
1673 * - September 21th 2003: first version (pick from pprint function).
1675 static void insert_block(CloogDomain
*domain
, CloogBlock
*block
, int level
,
1676 struct clast_stmt
***next
, CloogInfos
*infos
)
1678 CloogStatement
* statement
;
1679 struct clast_stmt
*subs
;
1684 for (statement
= block
->statement
; statement
; statement
= statement
->next
) {
1685 CloogStatement
*s_next
= statement
->next
;
1687 subs
= clast_equal(level
,infos
);
1689 statement
->next
= NULL
;
1690 **next
= &new_clast_user_stmt(domain
, statement
, subs
)->stmt
;
1691 statement
->next
= s_next
;
1692 *next
= &(**next
)->next
;
1698 * insert_loop function:
1699 * This function converts the content of a CloogLoop structure (loop) into a
1700 * clast_stmt (inserted at **next).
1701 * The iterator (level) of
1702 * the current loop is given by 'level': this is the column number of the
1703 * domain corresponding to the current loop iterator. The data of a loop are
1704 * written in this order:
1705 * 1. The guard of the loop, i.e. each constraint in the domain that does not
1706 * depend on the iterator (when the entry in the column 'level' is 0).
1707 * 2. The iteration domain of the iterator, given by the constraints in the
1708 * domain depending on the iterator, i.e.:
1709 * * an equality if the iterator has only one value (possibly preceded by
1710 * a guard verifying if this value is integral), *OR*
1711 * * a loop from the minimum possible value of the iterator to the maximum
1713 * 3. The included statement block.
1714 * 4. The inner loops (recursive call).
1715 * 5. The following loops (recursive call).
1716 * - level is the recursion level or the iteration level that we are printing,
1717 * - the infos structure gives the user some options about code printing,
1718 * the number of parameters in domain (nb_par), and the arrays of iterator
1719 * names and parameters (iters and params).
1721 * - November 2nd 2001: first version.
1722 * - March 6th 2003: infinite domain support.
1723 * - April 19th 2003: (debug) NULL loop support.
1724 * - June 29th 2003: non-unit strides support.
1725 * - April 28th 2005: (debug) level is level+equality when print statement!
1726 * - June 16th 2005: (debug) the N. Vasilache normalization step has been
1727 * added to avoid iteration duplication (see DaeGon Kim
1728 * bug in cloog_program_generate). Try vasilache.cloog
1729 * with and without the call to cloog_polylib_matrix_normalize,
1730 * using -f 8 -l 9 options for an idea.
1731 * - September 15th 2005: (debug) don't close equality braces when unnecessary.
1732 * - October 16th 2005: (debug) scalar value is saved for next loops.
1734 static void insert_loop(CloogLoop
* loop
, int level
,
1735 struct clast_stmt
***next
, CloogInfos
*infos
)
1738 CloogConstraintSet
*constraints
, *temp
;
1739 struct clast_stmt
**top
= *next
;
1740 CloogConstraint
*i
, *j
;
1743 /* It can happen that loop be NULL when an input polyhedron is empty. */
1747 /* The constraints do not always have a shape that allows us to generate code from it,
1748 * thus we normalize it, we also simplify it with the equalities.
1750 temp
= cloog_domain_constraints(loop
->domain
);
1751 cloog_constraint_set_normalize(temp
,level
);
1752 constraints
= cloog_constraint_set_simplify(temp
,infos
->equal
,level
,
1753 infos
->names
->nb_parameters
);
1754 cloog_constraint_set_free(temp
);
1756 infos
->stride
[level
- 1] = loop
->stride
;
1757 infos
->stride_level
++;
1760 /* First of all we have to print the guard. */
1761 insert_guard(constraints
,level
, next
, infos
);
1763 if (level
&& cloog_constraint_set_contains_level(constraints
, level
,
1764 infos
->names
->nb_parameters
)) {
1765 /* We scan all the constraints to know in which case we are :
1766 * [[if] equation] or [for].
1768 if (cloog_constraint_is_valid(i
=
1769 cloog_constraint_set_defining_equality(constraints
, level
))) {
1770 empty_loop
= !insert_equation(loop
->unsimplified
, i
,
1771 cloog_constraint_invalid(), level
, next
,
1774 } else if (cloog_constraint_is_valid(i
=
1775 cloog_constraint_set_defining_inequalities(constraints
,
1776 level
, &j
, infos
->names
->nb_parameters
))) {
1777 empty_loop
= !insert_equation(loop
->unsimplified
, i
, j
, level
, next
,
1780 empty_loop
= !insert_for(loop
->unsimplified
, constraints
, level
,
1781 loop
->otl
, next
, infos
);
1785 /* Finally, if there is an included statement block, print it. */
1786 insert_block(loop
->unsimplified
, loop
->block
, level
+equality
, next
, infos
);
1788 /* Go to the next level. */
1789 if (loop
->inner
!= NULL
)
1790 insert_loop(loop
->inner
, level
+1, next
, infos
);
1794 cloog_equal_del(infos
->equal
,level
);
1795 infos
->stride_level
--;
1797 cloog_constraint_set_free(constraints
);
1799 /* Go to the next loop on the same level. */
1801 top
= &(*top
)->next
;
1802 if (loop
->next
!= NULL
)
1803 insert_loop(loop
->next
, level
, &top
,infos
);
1807 struct clast_stmt
*cloog_clast_create(CloogProgram
*program
,
1808 CloogOptions
*options
)
1810 CloogInfos
*infos
= ALLOC(CloogInfos
);
1812 struct clast_stmt
*root
= &new_clast_root(program
->names
)->stmt
;
1813 struct clast_stmt
**next
= &root
->next
;
1815 infos
->state
= options
->state
;
1816 infos
->names
= program
->names
;
1817 infos
->options
= options
;
1818 infos
->scaldims
= program
->scaldims
;
1819 infos
->nb_scattdims
= program
->nb_scattdims
;
1821 /* Allocation for the array of strides, there is a +1 since the statement can
1822 * be included inside an external loop without iteration domain.
1824 nb_levels
= program
->names
->nb_scattering
+program
->names
->nb_iterators
+1;
1825 infos
->stride
= ALLOCN(CloogStride
*, nb_levels
);
1826 infos
->stride_level
= 0;
1828 infos
->equal
= cloog_equal_alloc(nb_levels
,
1829 nb_levels
, program
->names
->nb_parameters
);
1831 insert_loop(program
->loop
, 0, &next
, infos
);
1833 cloog_equal_free(infos
->equal
);
1835 free(infos
->stride
);
1842 struct clast_stmt
*cloog_clast_create_from_input(CloogInput
*input
,
1843 CloogOptions
*options
)
1845 CloogProgram
*program
;
1846 struct clast_stmt
*root
;
1848 program
= cloog_program_alloc(input
->context
, input
->ud
, options
);
1851 program
= cloog_program_generate(program
, options
);
1853 root
= cloog_clast_create(program
, options
);
1854 cloog_program_free(program
);