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 nb_scattdims
; /**< Scattering dimension number. */
21 int * scaldims
; /**< Boolean array saying whether a given
22 * scattering dimension is scalar or not.
24 CloogNames
* names
; /**< Names of iterators and parameters. */
25 CloogOptions
* options
; /**< Options on CLooG's behaviour. */
26 CloogEqualities
*equal
; /**< Matrix of equalities. */
29 typedef struct clooginfos CloogInfos
;
31 static int clast_expr_cmp(struct clast_expr
*e1
, struct clast_expr
*e2
);
32 static int clast_term_cmp(struct clast_term
*t1
, struct clast_term
*t2
);
33 static int clast_binary_cmp(struct clast_binary
*b1
, struct clast_binary
*b2
);
34 static int clast_reduction_cmp(struct clast_reduction
*r1
,
35 struct clast_reduction
*r2
);
37 static struct clast_expr
*clast_expr_copy(struct clast_expr
*e
);
39 static int clast_equal_add(CloogEqualities
*equal
,
40 CloogConstraintSet
*constraints
,
41 int level
, CloogConstraint
*constraint
,
44 static struct clast_stmt
*clast_equal(int level
, CloogInfos
*infos
);
45 static struct clast_expr
*clast_minmax(CloogConstraintSet
*constraints
,
46 int level
, int max
, int guard
,
49 static void insert_guard(CloogConstraintSet
*constraints
, int level
,
50 struct clast_stmt
***next
, CloogInfos
*infos
);
51 static int insert_modulo_guard(CloogConstraint
*upper
,
52 CloogConstraint
*lower
, int level
,
53 struct clast_stmt
***next
, CloogInfos
*infos
);
54 static int insert_equation(CloogConstraint
*upper
, CloogConstraint
*lower
,
55 int level
, struct clast_stmt
***next
, CloogInfos
*infos
);
56 static int insert_for(CloogConstraintSet
*constraints
, int level
, int otl
,
57 struct clast_stmt
***next
, CloogInfos
*infos
);
58 static void insert_block(CloogBlock
*block
, int level
,
59 struct clast_stmt
***next
, CloogInfos
*infos
);
60 static void insert_loop(CloogLoop
* loop
, int level
,
61 struct clast_stmt
***next
, CloogInfos
*infos
);
64 struct clast_name
*new_clast_name(const char *name
)
66 struct clast_name
*n
= malloc(sizeof(struct clast_name
));
67 n
->expr
.type
= clast_expr_name
;
72 struct clast_term
*new_clast_term(cloog_int_t c
, struct clast_expr
*v
)
74 struct clast_term
*t
= malloc(sizeof(struct clast_term
));
75 t
->expr
.type
= clast_expr_term
;
76 cloog_int_init(t
->val
);
77 cloog_int_set(t
->val
, c
);
82 struct clast_binary
*new_clast_binary(enum clast_bin_type t
,
83 struct clast_expr
*lhs
, cloog_int_t rhs
)
85 struct clast_binary
*b
= malloc(sizeof(struct clast_binary
));
86 b
->expr
.type
= clast_expr_bin
;
89 cloog_int_init(b
->RHS
);
90 cloog_int_set(b
->RHS
, rhs
);
94 struct clast_reduction
*new_clast_reduction(enum clast_red_type t
, int n
)
97 struct clast_reduction
*r
;
98 r
= malloc(sizeof(struct clast_reduction
)+(n
-1)*sizeof(struct clast_expr
*));
99 r
->expr
.type
= clast_expr_red
;
102 for (i
= 0; i
< n
; ++i
)
107 static void free_clast_root(struct clast_stmt
*s
);
109 const struct clast_stmt_op stmt_root
= { free_clast_root
};
111 static void free_clast_root(struct clast_stmt
*s
)
113 struct clast_root
*r
= (struct clast_root
*)s
;
114 assert(CLAST_STMT_IS_A(s
, stmt_root
));
115 cloog_names_free(r
->names
);
119 struct clast_root
*new_clast_root(CloogNames
*names
)
121 struct clast_root
*r
= malloc(sizeof(struct clast_root
));
122 r
->stmt
.op
= &stmt_root
;
124 r
->names
= cloog_names_copy(names
);
128 static void free_clast_assignment(struct clast_stmt
*s
);
130 const struct clast_stmt_op stmt_ass
= { free_clast_assignment
};
132 static void free_clast_assignment(struct clast_stmt
*s
)
134 struct clast_assignment
*a
= (struct clast_assignment
*)s
;
135 assert(CLAST_STMT_IS_A(s
, stmt_ass
));
136 free_clast_expr(a
->RHS
);
140 struct clast_assignment
*new_clast_assignment(const char *lhs
,
141 struct clast_expr
*rhs
)
143 struct clast_assignment
*a
= malloc(sizeof(struct clast_assignment
));
144 a
->stmt
.op
= &stmt_ass
;
151 static void free_clast_user_stmt(struct clast_stmt
*s
);
153 const struct clast_stmt_op stmt_user
= { free_clast_user_stmt
};
155 static void free_clast_user_stmt(struct clast_stmt
*s
)
157 struct clast_user_stmt
*u
= (struct clast_user_stmt
*)s
;
158 assert(CLAST_STMT_IS_A(s
, stmt_user
));
159 cloog_statement_free(u
->statement
);
160 cloog_clast_free(u
->substitutions
);
164 struct clast_user_stmt
*new_clast_user_stmt(CloogStatement
*stmt
,
165 struct clast_stmt
*subs
)
167 struct clast_user_stmt
*u
= malloc(sizeof(struct clast_user_stmt
));
168 u
->stmt
.op
= &stmt_user
;
170 u
->statement
= cloog_statement_copy(stmt
);
171 u
->substitutions
= subs
;
175 static void free_clast_block(struct clast_stmt
*b
);
177 const struct clast_stmt_op stmt_block
= { free_clast_block
};
179 static void free_clast_block(struct clast_stmt
*s
)
181 struct clast_block
*b
= (struct clast_block
*)s
;
182 assert(CLAST_STMT_IS_A(s
, stmt_block
));
183 cloog_clast_free(b
->body
);
187 struct clast_block
*new_clast_block()
189 struct clast_block
*b
= malloc(sizeof(struct clast_block
));
190 b
->stmt
.op
= &stmt_block
;
196 static void free_clast_for(struct clast_stmt
*s
);
198 const struct clast_stmt_op stmt_for
= { free_clast_for
};
200 static void free_clast_for(struct clast_stmt
*s
)
202 struct clast_for
*f
= (struct clast_for
*)s
;
203 assert(CLAST_STMT_IS_A(s
, stmt_for
));
204 free_clast_expr(f
->LB
);
205 free_clast_expr(f
->UB
);
206 cloog_int_clear(f
->stride
);
207 cloog_clast_free(f
->body
);
211 struct clast_for
*new_clast_for(const char *it
, struct clast_expr
*LB
,
212 struct clast_expr
*UB
, CloogStride
*stride
)
214 struct clast_for
*f
= malloc(sizeof(struct clast_for
));
215 f
->stmt
.op
= &stmt_for
;
221 cloog_int_init(f
->stride
);
223 cloog_int_set(f
->stride
, stride
->stride
);
225 cloog_int_set_si(f
->stride
, 1);
229 static void free_clast_guard(struct clast_stmt
*s
);
231 const struct clast_stmt_op stmt_guard
= { free_clast_guard
};
233 static void free_clast_guard(struct clast_stmt
*s
)
236 struct clast_guard
*g
= (struct clast_guard
*)s
;
237 assert(CLAST_STMT_IS_A(s
, stmt_guard
));
238 cloog_clast_free(g
->then
);
239 for (i
= 0; i
< g
->n
; ++i
) {
240 free_clast_expr(g
->eq
[i
].LHS
);
241 free_clast_expr(g
->eq
[i
].RHS
);
246 struct clast_guard
*new_clast_guard(int n
)
249 struct clast_guard
*g
= malloc(sizeof(struct clast_guard
) +
250 (n
-1) * sizeof(struct clast_equation
));
251 g
->stmt
.op
= &stmt_guard
;
255 for (i
= 0; i
< n
; ++i
) {
262 void free_clast_name(struct clast_name
*n
)
267 void free_clast_term(struct clast_term
*t
)
269 cloog_int_clear(t
->val
);
270 free_clast_expr(t
->var
);
274 void free_clast_binary(struct clast_binary
*b
)
276 cloog_int_clear(b
->RHS
);
277 free_clast_expr(b
->LHS
);
281 void free_clast_reduction(struct clast_reduction
*r
)
284 for (i
= 0; i
< r
->n
; ++i
)
285 free_clast_expr(r
->elts
[i
]);
289 void free_clast_expr(struct clast_expr
*e
)
294 case clast_expr_name
:
295 free_clast_name((struct clast_name
*) e
);
297 case clast_expr_term
:
298 free_clast_term((struct clast_term
*) e
);
301 free_clast_reduction((struct clast_reduction
*) e
);
304 free_clast_binary((struct clast_binary
*) e
);
311 void free_clast_stmt(struct clast_stmt
*s
)
318 void cloog_clast_free(struct clast_stmt
*s
)
320 struct clast_stmt
*next
;
328 static int clast_name_cmp(struct clast_name
*n1
, struct clast_name
*n2
)
330 return n1
->name
== n2
->name
? 0 : strcmp(n1
->name
, n2
->name
);
333 static int clast_term_cmp(struct clast_term
*t1
, struct clast_term
*t2
)
336 if (!t1
->var
&& t2
->var
)
338 if (t1
->var
&& !t2
->var
)
340 c
= clast_expr_cmp(t1
->var
, t2
->var
);
343 return cloog_int_cmp(t1
->val
, t2
->val
);
346 static int clast_binary_cmp(struct clast_binary
*b1
, struct clast_binary
*b2
)
350 if (b1
->type
!= b2
->type
)
351 return b1
->type
- b2
->type
;
352 if ((c
= cloog_int_cmp(b1
->RHS
, b2
->RHS
)))
354 return clast_expr_cmp(b1
->LHS
, b2
->LHS
);
357 static int clast_reduction_cmp(struct clast_reduction
*r1
, struct clast_reduction
*r2
)
362 if (r1
->n
== 1 && r2
->n
== 1)
363 return clast_expr_cmp(r1
->elts
[0], r2
->elts
[0]);
364 if (r1
->type
!= r2
->type
)
365 return r1
->type
- r2
->type
;
367 return r1
->n
- r2
->n
;
368 for (i
= 0; i
< r1
->n
; ++i
)
369 if ((c
= clast_expr_cmp(r1
->elts
[i
], r2
->elts
[i
])))
374 static int clast_expr_cmp(struct clast_expr
*e1
, struct clast_expr
*e2
)
382 if (e1
->type
!= e2
->type
)
383 return e1
->type
- e2
->type
;
385 case clast_expr_name
:
386 return clast_name_cmp((struct clast_name
*) e1
,
387 (struct clast_name
*) e2
);
388 case clast_expr_term
:
389 return clast_term_cmp((struct clast_term
*) e1
,
390 (struct clast_term
*) e2
);
392 return clast_binary_cmp((struct clast_binary
*) e1
,
393 (struct clast_binary
*) e2
);
395 return clast_reduction_cmp((struct clast_reduction
*) e1
,
396 (struct clast_reduction
*) e2
);
402 int clast_expr_equal(struct clast_expr
*e1
, struct clast_expr
*e2
)
404 return clast_expr_cmp(e1
, e2
) == 0;
408 * Return 1 is both expressions are constant terms and e1 is bigger than e2.
410 int clast_expr_is_bigger_constant(struct clast_expr
*e1
, struct clast_expr
*e2
)
412 struct clast_term
*t1
, *t2
;
413 struct clast_reduction
*r
;
417 if (e1
->type
== clast_expr_red
) {
418 r
= (struct clast_reduction
*)e1
;
419 return r
->n
== 1 && clast_expr_is_bigger_constant(r
->elts
[0], e2
);
421 if (e2
->type
== clast_expr_red
) {
422 r
= (struct clast_reduction
*)e2
;
423 return r
->n
== 1 && clast_expr_is_bigger_constant(e1
, r
->elts
[0]);
425 if (e1
->type
!= clast_expr_term
|| e2
->type
!= clast_expr_term
)
427 t1
= (struct clast_term
*)e1
;
428 t2
= (struct clast_term
*)e2
;
429 if (t1
->var
|| t2
->var
)
431 return cloog_int_gt(t1
->val
, t2
->val
);
434 static int qsort_expr_cmp(const void *p1
, const void *p2
)
436 return clast_expr_cmp(*(struct clast_expr
**)p1
, *(struct clast_expr
**)p2
);
439 static void clast_reduction_sort(struct clast_reduction
*r
)
441 qsort(&r
->elts
[0], r
->n
, sizeof(struct clast_expr
*), qsort_expr_cmp
);
444 static int qsort_eq_cmp(const void *p1
, const void *p2
)
446 struct clast_equation
*eq1
= (struct clast_equation
*)p1
;
447 struct clast_equation
*eq2
= (struct clast_equation
*)p2
;
450 cmp
= clast_expr_cmp(eq1
->LHS
, eq2
->LHS
);
454 cmp
= clast_expr_cmp(eq1
->RHS
, eq2
->RHS
);
458 return eq1
->sign
- eq2
->sign
;
462 * Sort equations in a clast_guard.
464 static void clast_guard_sort(struct clast_guard
*g
)
466 qsort(&g
->eq
[0], g
->n
, sizeof(struct clast_equation
), qsort_eq_cmp
);
471 * Construct a (deep) copy of an expression clast.
473 static struct clast_expr
*clast_expr_copy(struct clast_expr
*e
)
478 case clast_expr_name
: {
479 struct clast_name
* n
= (struct clast_name
*) e
;
480 return &new_clast_name(n
->name
)->expr
;
482 case clast_expr_term
: {
483 struct clast_term
* t
= (struct clast_term
*) e
;
484 return &new_clast_term(t
->val
, clast_expr_copy(t
->var
))->expr
;
486 case clast_expr_red
: {
488 struct clast_reduction
*r
= (struct clast_reduction
*) e
;
489 struct clast_reduction
*r2
= new_clast_reduction(r
->type
, r
->n
);
490 for (i
= 0; i
< r
->n
; ++i
)
491 r2
->elts
[i
] = clast_expr_copy(r
->elts
[i
]);
494 case clast_expr_bin
: {
495 struct clast_binary
*b
= (struct clast_binary
*) e
;
496 return &new_clast_binary(b
->type
, clast_expr_copy(b
->LHS
), b
->RHS
)->expr
;
504 /******************************************************************************
505 * Equalities spreading functions *
506 ******************************************************************************/
510 * clast_equal_allow function:
511 * This function checks whether the options allow us to spread the equality or
512 * not. It returns 1 if so, 0 otherwise.
513 * - equal is the matrix of equalities,
514 * - level is the column number in equal of the element which is 'equal to',
515 * - line is the line number in equal of the constraint we want to study,
516 * - the infos structure gives the user all options on code printing and more.
518 * - October 27th 2005: first version (extracted from old pprint_equal_add).
520 static int clast_equal_allow(CloogEqualities
*equal
, int level
, int line
,
523 if (level
< infos
->options
->fsp
)
526 if ((cloog_equal_type(equal
, level
) == EQTYPE_EXAFFINE
) &&
527 !infos
->options
->esp
)
535 * clast_equal_add function:
536 * This function updates the row (level-1) of the equality matrix (equal) with
537 * the row that corresponds to the row (line) of the matrix (matrix). It returns
538 * 1 if the row can be updated, 0 otherwise.
539 * - equal is the matrix of equalities,
540 * - matrix is the matrix of constraints,
541 * - level is the column number in matrix of the element which is 'equal to',
542 * - line is the line number in matrix of the constraint we want to study,
543 * - the infos structure gives the user all options on code printing and more.
545 static int clast_equal_add(CloogEqualities
*equal
,
546 CloogConstraintSet
*constraints
,
547 int level
, CloogConstraint
*constraint
,
550 cloog_equal_add(equal
, constraints
, level
, constraint
,
551 infos
->names
->nb_parameters
);
553 return clast_equal_allow(equal
, level
, level
-1, infos
);
559 * clast_equal function:
560 * This function prints the substitution data of a statement into a clast_stmt.
561 * Using this function instead of pprint_equal is useful for generating
562 * a compilable pseudo-code by using preprocessor macro for each statement.
563 * By opposition to pprint_equal, the result is less human-readable. For
564 * instance this function will print (i,i+3,k,3) where pprint_equal would
565 * return (j=i+3,l=3).
566 * - level is the number of loops enclosing the statement,
567 * - the infos structure gives the user all options on code printing and more.
569 * - March 12th 2004: first version.
570 * - November 21th 2005: (debug) now works well with GMP version.
572 static struct clast_stmt
*clast_equal(int level
, CloogInfos
*infos
)
575 struct clast_expr
*e
;
576 struct clast_stmt
*a
= NULL
;
577 struct clast_stmt
**next
= &a
;
578 CloogEqualities
*equal
= infos
->equal
;
579 CloogConstraint
*equal_constraint
;
581 for (i
=infos
->names
->nb_scattering
;i
<level
-1;i
++)
582 { if (cloog_equal_type(equal
, i
+1)) {
583 equal_constraint
= cloog_equal_constraint(equal
, i
);
584 e
= clast_bound_from_constraint(equal_constraint
, i
+1, infos
->names
);
585 cloog_constraint_release(equal_constraint
);
587 e
= &new_clast_term(infos
->state
->one
, &new_clast_name(
588 cloog_names_name_at_level(infos
->names
, i
+1))->expr
)->expr
;
590 *next
= &new_clast_assignment(NULL
, e
)->stmt
;
591 next
= &(*next
)->next
;
599 * clast_bound_from_constraint function:
600 * This function returns a clast_expr containing the printing of the
601 * 'right part' of a constraint according to an element.
602 * For instance, for the constraint -3*i + 2*j - M >=0 and the element j,
603 * we have j >= (3*i + M)/2. As we are looking for integral solutions, this
604 * function should return 'ceild(3*i+M,2)'.
605 * - matrix is the polyhedron containing all the constraints,
606 * - line_num is the line number in domain of the constraint we want to print,
607 * - level is the column number in domain of the element we want to use,
608 * - names structure gives the user some options about code printing,
609 * the number of parameters in domain (nb_par), and the arrays of iterator
610 * names and parameters (iters and params).
612 * - November 2nd 2001: first version.
613 * - June 27th 2003: 64 bits version ready.
615 struct clast_expr
*clast_bound_from_constraint(CloogConstraint
*constraint
,
616 int level
, CloogNames
*names
)
618 int i
, sign
, nb_elts
=0, len
;
619 cloog_int_t
*line
, numerator
, denominator
, temp
, division
;
620 struct clast_expr
*e
= NULL
;
621 struct cloog_vec
*line_vector
;
623 len
= cloog_constraint_total_dimension(constraint
) + 2;
624 line_vector
= cloog_vec_alloc(len
);
625 line
= line_vector
->p
;
626 cloog_constraint_copy_coefficients(constraint
, line
+1);
627 cloog_int_init(temp
);
628 cloog_int_init(numerator
);
629 cloog_int_init(denominator
);
631 if (!cloog_int_is_zero(line
[level
])) {
632 struct clast_reduction
*r
;
633 /* Maybe we need to invert signs in such a way that the element sign is>0.*/
634 sign
= -cloog_int_sgn(line
[level
]);
636 for (i
= 1, nb_elts
= 0; i
<= len
- 1; ++i
)
637 if (i
!= level
&& !cloog_int_is_zero(line
[i
]))
639 r
= new_clast_reduction(clast_red_sum
, nb_elts
);
642 /* First, we have to print the iterators and the parameters. */
643 for (i
= 1; i
<= len
- 2; i
++) {
644 struct clast_expr
*v
;
646 if (i
== level
|| cloog_int_is_zero(line
[i
]))
649 v
= cloog_constraint_variable_expr(constraint
, i
, names
);
652 cloog_int_neg(temp
,line
[i
]);
654 cloog_int_set(temp
,line
[i
]);
656 r
->elts
[nb_elts
++] = &new_clast_term(temp
, v
)->expr
;
660 cloog_int_neg(numerator
, line
[len
- 1]);
661 cloog_int_set(denominator
, line
[level
]);
664 cloog_int_set(numerator
, line
[len
- 1]);
665 cloog_int_neg(denominator
, line
[level
]);
668 /* Finally, the constant, and the final printing. */
670 if (!cloog_int_is_zero(numerator
))
671 r
->elts
[nb_elts
++] = &new_clast_term(numerator
, NULL
)->expr
;
673 if (!cloog_int_is_one(line
[level
]) && !cloog_int_is_neg_one(line
[level
]))
674 { if (!cloog_constraint_is_equality(constraint
))
675 { if (cloog_int_is_pos(line
[level
]))
676 e
= &new_clast_binary(clast_bin_cdiv
, &r
->expr
, denominator
)->expr
;
678 e
= &new_clast_binary(clast_bin_fdiv
, &r
->expr
, denominator
)->expr
;
680 e
= &new_clast_binary(clast_bin_div
, &r
->expr
, denominator
)->expr
;
685 free_clast_reduction(r
);
686 if (cloog_int_is_zero(numerator
))
687 e
= &new_clast_term(numerator
, NULL
)->expr
;
689 { if (!cloog_int_is_one(denominator
))
690 { if (!cloog_constraint_is_equality(constraint
)) { /* useful? */
691 if (cloog_int_is_divisible_by(numerator
, denominator
)) {
692 cloog_int_divexact(temp
, numerator
, denominator
);
693 e
= &new_clast_term(temp
, NULL
)->expr
;
696 cloog_int_init(division
);
697 cloog_int_tdiv_q(division
, numerator
, denominator
);
698 if (cloog_int_is_neg(numerator
)) {
699 if (cloog_int_is_pos(line
[level
])) {
701 e
= &new_clast_term(division
, NULL
)->expr
;
704 cloog_int_sub_ui(temp
, division
, 1);
705 e
= &new_clast_term(temp
, NULL
)->expr
;
709 { if (cloog_int_is_pos(line
[level
]))
710 { /* nb>0 need max */
711 cloog_int_add_ui(temp
, division
, 1);
712 e
= &new_clast_term(temp
, NULL
)->expr
;
716 e
= &new_clast_term(division
, NULL
)->expr
;
718 cloog_int_clear(division
);
722 e
= &new_clast_binary(clast_bin_div
,
723 &new_clast_term(numerator
, NULL
)->expr
,
727 e
= &new_clast_term(numerator
, NULL
)->expr
;
732 cloog_vec_free(line_vector
);
734 cloog_int_clear(temp
);
735 cloog_int_clear(numerator
);
736 cloog_int_clear(denominator
);
742 /* Temporary structure for communication between clast_minmax and
743 * its cloog_constraint_set_foreach_constraint callback functions.
745 struct clast_minmax_data
{
752 struct clast_reduction
*r
;
756 /* Should constraint "c" be considered by clast_minmax?
758 static int valid_bound(CloogConstraint
*c
, struct clast_minmax_data
*d
)
760 if (d
->max
&& !cloog_constraint_is_lower_bound(c
, d
->level
- 1))
762 if (!d
->max
&& !cloog_constraint_is_upper_bound(c
, d
->level
- 1))
764 if (cloog_constraint_is_equality(c
))
766 if (d
->guard
&& cloog_constraint_involves(c
, d
->guard
- 1))
773 /* Increment n for each bound that should be considered by clast_minmax.
775 static int count_bounds(CloogConstraint
*c
, void *user
)
777 struct clast_minmax_data
*d
= (struct clast_minmax_data
*) user
;
779 if (!valid_bound(c
, d
))
788 /* Update the given lower bound based on stride information.
789 * In some backends, the lower bounds are updated from within
790 * cloog_loop_stride, but other backends leave the updating to
791 * this function. In the later case, the original lower bound
792 * is known to be a constant.
793 * If the bound turns out not to be a constant, we know we
794 * are in the former case and nothing needs to be done.
795 * If the bound has already been updated and it just happens
796 * to be a constant, then this function performs an identity
797 * operation on the constant.
799 static void update_lower_bound(struct clast_expr
*expr
, int level
,
802 struct clast_term
*t
;
803 if (stride
->constraint
)
805 if (expr
->type
!= clast_expr_term
)
807 t
= (struct clast_term
*)expr
;
810 cloog_int_sub(t
->val
, t
->val
, stride
->offset
);
811 cloog_int_cdiv_q(t
->val
, t
->val
, stride
->stride
);
812 cloog_int_mul(t
->val
, t
->val
, stride
->stride
);
813 cloog_int_add(t
->val
, t
->val
, stride
->offset
);
817 /* Add all relevant bounds to r->elts and update lower bounds
818 * based on stride information.
820 static int collect_bounds(CloogConstraint
*c
, void *user
)
822 struct clast_minmax_data
*d
= (struct clast_minmax_data
*) user
;
824 if (!valid_bound(c
, d
))
827 d
->r
->elts
[d
->n
] = clast_bound_from_constraint(c
, d
->level
,
829 if (d
->lower_bound
&& d
->infos
->stride
[d
->level
- 1]) {
830 update_lower_bound(d
->r
->elts
[d
->n
], d
->level
,
831 d
->infos
->stride
[d
->level
- 1]);
841 * clast_minmax function:
842 * This function returns a clast_expr containing the printing of a minimum or a
843 * maximum of the 'right parts' of all constraints according to an element.
844 * For instance consider the constraints:
848 * if we are looking for the minimum for the element j, the function should
849 * return 'max(ceild(3*i+M,2),-2*i)'.
850 * - constraints is the constraints,
851 * - level is the column number in domain of the element we want to use,
852 * - max is a boolean set to 1 if we are looking for a maximum, 0 for a minimum,
853 * - guard is set to 0 if there is no guard, and set to the level of the element
854 * with a guard otherwise (then the function gives the max or the min only
855 * for the constraint where the guarded coefficient is 0),
856 * - lower is set to 1 if the maximum is to be used a lower bound on a loop
857 * - the infos structure gives the user some options about code printing,
858 * the number of parameters in domain (nb_par), and the arrays of iterator
859 * names and parameters (iters and params).
861 * - November 2nd 2001: first version.
863 static struct clast_expr
*clast_minmax(CloogConstraintSet
*constraints
,
864 int level
, int max
, int guard
,
868 struct clast_minmax_data data
= { level
, max
, guard
, lower_bound
, infos
};
872 cloog_constraint_set_foreach_constraint(constraints
, count_bounds
, &data
);
876 data
.r
= new_clast_reduction(max
? clast_red_max
: clast_red_min
, data
.n
);
879 cloog_constraint_set_foreach_constraint(constraints
, collect_bounds
, &data
);
881 clast_reduction_sort(data
.r
);
882 return &data
.r
->expr
;
887 * Insert modulo guards defined by existentially quantified dimensions,
888 * not involving the given level.
890 * This function is called from within insert_guard or insert_for.
891 * Any constraint used in constructing * a modulo guard is removed
892 * from the constraint set to avoid insert_guard or insert_for
893 * adding a duplicate (pair of) constraint(s).
895 static void insert_extra_modulo_guards(CloogConstraintSet
*constraints
,
896 int level
, struct clast_stmt
***next
, CloogInfos
*infos
)
901 CloogConstraint
*upper
, *lower
;
903 total_dim
= cloog_constraint_set_total_dimension(constraints
);
904 nb_iter
= cloog_constraint_set_n_iterators(constraints
,
905 infos
->names
->nb_parameters
);
907 for (i
= total_dim
- infos
->names
->nb_parameters
; i
>= nb_iter
+ 1; i
--) {
908 if (cloog_constraint_is_valid(upper
=
909 cloog_constraint_set_defining_equality(constraints
, i
))) {
910 if (!level
|| (nb_iter
< level
) ||
911 !cloog_constraint_involves(upper
, level
-1)) {
912 insert_modulo_guard(upper
,
913 cloog_constraint_invalid(), i
, next
, infos
);
914 cloog_constraint_clear(upper
);
916 cloog_constraint_release(upper
);
917 } else if (cloog_constraint_is_valid(upper
=
918 cloog_constraint_set_defining_inequalities(constraints
,
919 i
, &lower
, infos
->names
->nb_parameters
))) {
920 if (!level
|| (nb_iter
< level
) ||
921 !cloog_constraint_involves(upper
, level
-1)) {
922 insert_modulo_guard(upper
, lower
, i
, next
, infos
);
923 cloog_constraint_clear(upper
);
924 cloog_constraint_clear(lower
);
926 cloog_constraint_release(upper
);
927 cloog_constraint_release(lower
);
933 static int clear_lower_bound_at_level(CloogConstraint
*c
, void *user
)
935 int level
= *(int *)user
;
937 if (cloog_constraint_is_lower_bound(c
, level
- 1))
938 cloog_constraint_clear(c
);
944 static int clear_upper_bound_at_level(CloogConstraint
*c
, void *user
)
946 int level
= *(int *)user
;
948 if (cloog_constraint_is_upper_bound(c
, level
- 1))
949 cloog_constraint_clear(c
);
955 /* Temporary structure for communication between insert_guard and
956 * its cloog_constraint_set_foreach_constraint callback function.
958 struct clast_guard_data
{
964 CloogConstraintSet
*copy
;
965 struct clast_guard
*g
;
969 static int guard_count_bounds(CloogConstraint
*c
, void *user
)
971 struct clast_guard_data
*d
= (struct clast_guard_data
*) user
;
979 /* Insert a guard, if necesessary, for constraint j.
981 static int insert_guard_constraint(CloogConstraint
*j
, void *user
)
983 struct clast_guard_data
*d
= (struct clast_guard_data
*) user
;
985 struct clast_expr
*v
;
986 struct clast_term
*t
;
988 if (!cloog_constraint_involves(j
, d
->i
- 1))
991 if (d
->level
&& d
->nb_iter
>= d
->level
&&
992 cloog_constraint_involves(j
, d
->level
- 1))
995 v
= cloog_constraint_variable_expr(j
, d
->i
, d
->infos
->names
);
996 d
->g
->eq
[d
->n
].LHS
= &(t
= new_clast_term(d
->infos
->state
->one
, v
))->expr
;
997 if (!d
->level
|| cloog_constraint_is_equality(j
)) {
998 /* put the "denominator" in the LHS */
999 cloog_constraint_coefficient_get(j
, d
->i
- 1, &t
->val
);
1000 cloog_constraint_coefficient_set(j
, d
->i
- 1, d
->infos
->state
->one
);
1001 if (cloog_int_is_neg(t
->val
)) {
1002 cloog_int_neg(t
->val
, t
->val
);
1003 cloog_constraint_coefficient_set(j
, d
->i
- 1, d
->infos
->state
->negone
);
1005 if (d
->level
|| cloog_constraint_is_equality(j
))
1006 d
->g
->eq
[d
->n
].sign
= 0;
1007 else if (cloog_constraint_is_lower_bound(j
, d
->i
- 1))
1008 d
->g
->eq
[d
->n
].sign
= 1;
1010 d
->g
->eq
[d
->n
].sign
= -1;
1011 d
->g
->eq
[d
->n
].RHS
= clast_bound_from_constraint(j
, d
->i
, d
->infos
->names
);
1015 if (cloog_constraint_is_lower_bound(j
, d
->i
- 1)) {
1017 d
->g
->eq
[d
->n
].sign
= 1;
1020 d
->g
->eq
[d
->n
].sign
= -1;
1023 guarded
= (d
->nb_iter
>= d
->level
) ? d
->level
: 0 ;
1024 d
->g
->eq
[d
->n
].RHS
= clast_minmax(d
->copy
, d
->i
, minmax
, guarded
, 0,
1029 /* 'elimination' of the current constraint, this avoid to use one
1030 * constraint more than once. The current line is always eliminated,
1031 * and the next lines if they are in a min or a max.
1033 cloog_constraint_clear(j
);
1038 cloog_constraint_set_foreach_constraint(d
->copy
,
1039 clear_lower_bound_at_level
, &d
->i
);
1040 else if (minmax
== 0)
1041 cloog_constraint_set_foreach_constraint(d
->copy
,
1042 clear_upper_bound_at_level
, &d
->i
);
1049 * insert_guard function:
1050 * This function inserts a guard in the clast.
1051 * A guard on an element (level) is :
1052 * -> the conjunction of all the existing constraints where the coefficient of
1053 * this element is 0 if the element is an iterator,
1054 * -> the conjunction of all the existing constraints if the element isn't an
1056 * For instance, considering these constraints and the element j:
1059 * this function should return 'if (2*i+M>=0) {'.
1060 * - matrix is the polyhedron containing all the constraints,
1061 * - level is the column number of the element in matrix we want to use,
1062 * - the infos structure gives the user some options about code printing,
1063 * the number of parameters in matrix (nb_par), and the arrays of iterator
1064 * names and parameters (iters and params).
1066 * - November 3rd 2001: first version.
1067 * - November 14th 2001: a lot of 'purifications'.
1068 * - July 31th 2002: (debug) some guard parts are no more redundants.
1069 * - August 12th 2002: polyhedra union ('or' conditions) are now supported.
1070 * - October 27th 2005: polyhedra union ('or' conditions) are no more supported
1071 * (the need came from loop_simplify that may result in
1072 * domain unions, now it should be fixed directly in
1073 * cloog_loop_simplify).
1075 static void insert_guard(CloogConstraintSet
*constraints
, int level
,
1076 struct clast_stmt
***next
, CloogInfos
*infos
)
1079 struct clast_guard_data data
= { level
, infos
, 0 };
1084 data
.copy
= cloog_constraint_set_copy(constraints
);
1086 insert_extra_modulo_guards(data
.copy
, level
, next
, infos
);
1088 cloog_constraint_set_foreach_constraint(constraints
,
1089 guard_count_bounds
, &data
);
1091 data
.g
= new_clast_guard(data
.n
);
1094 /* Well, it looks complicated because I wanted to have a particular, more
1095 * readable, ordering, obviously this function may be far much simpler !
1097 data
.nb_iter
= cloog_constraint_set_n_iterators(constraints
,
1098 infos
->names
->nb_parameters
);
1100 /* We search for guard parts. */
1101 total_dim
= cloog_constraint_set_total_dimension(constraints
);
1102 for (data
.i
= 1; data
.i
<= total_dim
; data
.i
++)
1103 cloog_constraint_set_foreach_constraint(data
.copy
,
1104 insert_guard_constraint
, &data
);
1106 cloog_constraint_set_free(data
.copy
);
1110 clast_guard_sort(data
.g
);
1111 **next
= &data
.g
->stmt
;
1112 *next
= &data
.g
->then
;
1114 free_clast_stmt(&data
.g
->stmt
);
1118 * Check if the constant "cst" satisfies the modulo guard that
1119 * would be introduced by insert_computed_modulo_guard.
1120 * The constant is assumed to have been reduced prior to calling
1123 static int constant_modulo_guard_is_satisfied(CloogConstraint
*lower
,
1124 cloog_int_t bound
, cloog_int_t cst
)
1126 if (cloog_constraint_is_valid(lower
))
1127 return cloog_int_le(cst
, bound
);
1129 return cloog_int_is_zero(cst
);
1133 * Insert a modulo guard "r % mod == 0" or "r % mod <= bound",
1134 * depending on whether lower represents a valid constraint.
1136 static void insert_computed_modulo_guard(struct clast_reduction
*r
,
1137 CloogConstraint
*lower
, cloog_int_t mod
, cloog_int_t bound
,
1138 struct clast_stmt
***next
)
1140 struct clast_expr
*e
;
1141 struct clast_guard
*g
;
1143 e
= &new_clast_binary(clast_bin_mod
, &r
->expr
, mod
)->expr
;
1144 g
= new_clast_guard(1);
1145 if (!cloog_constraint_is_valid(lower
)) {
1147 cloog_int_set_si(bound
, 0);
1148 g
->eq
[0].RHS
= &new_clast_term(bound
, NULL
)->expr
;
1152 g
->eq
[0].RHS
= &new_clast_term(bound
, NULL
)->expr
;
1161 /* Try and eliminate coefficients from a modulo constraint based on
1162 * stride information of an earlier level.
1163 * The modulo of the constraint being constructed is "m".
1164 * The stride information at level "level" is given by "stride"
1165 * and indicated that the iterator i at level "level" is equal to
1166 * some expression modulo stride->stride.
1167 * If stride->stride is a multiple of "m' then i is also equal to
1168 * the expression modulo m and so we can eliminate the coefficient of i.
1170 * If stride->constraint is NULL, then i has a constant value modulo m, stored
1171 * stride->offset. We simply multiply this constant with the coefficient
1172 * of i and add the result to the constant term, reducing it modulo m.
1174 * If stride->constraint is not NULL, then it is a constraint of the form
1178 * with s equal to stride->stride, e an expression in terms of the
1179 * parameters and earlier iterators and a some arbitrary expression
1180 * in terms of existentially quantified variables.
1181 * stride->factor is a value f such that f * k = -1 mod s.
1182 * Adding stride->constraint f * c times to the current modulo constraint,
1183 * with c the coefficient of i eliminates i in favor of parameters and
1184 * earlier variables.
1186 static void eliminate_using_stride_constraint(cloog_int_t
*line
, int len
,
1187 int nb_iter
, CloogStride
*stride
, int level
, cloog_int_t m
)
1191 if (!cloog_int_is_divisible_by(stride
->stride
, m
))
1194 if (stride
->constraint
) {
1200 cloog_int_mul(t
, line
[level
], stride
->factor
);
1201 for (i
= 1; i
< level
; ++i
) {
1202 cloog_constraint_coefficient_get(stride
->constraint
,
1204 cloog_int_addmul(line
[i
], t
, v
);
1205 cloog_int_fdiv_r(line
[i
], line
[i
], m
);
1207 for (i
= nb_iter
+ 1; i
<= len
- 2; ++i
) {
1208 cloog_constraint_coefficient_get(stride
->constraint
,
1209 i
- nb_iter
- 1 + level
, &v
);
1210 cloog_int_addmul(line
[i
], t
, v
);
1211 cloog_int_fdiv_r(line
[i
], line
[i
], m
);
1213 cloog_constraint_constant_get(stride
->constraint
, &v
);
1214 cloog_int_addmul(line
[len
- 1], t
, v
);
1215 cloog_int_fdiv_r(line
[len
- 1], line
[len
- 1], m
);
1219 cloog_int_addmul(line
[len
- 1], line
[level
], stride
->offset
);
1220 cloog_int_fdiv_r(line
[len
- 1], line
[len
- 1], m
);
1223 cloog_int_set_si(line
[level
], 0);
1227 /* Temporary structure for communication between insert_modulo_guard and
1228 * its cloog_constraint_set_foreach_constraint callback function.
1230 struct clast_modulo_guard_data
{
1231 CloogConstraint
*lower
;
1233 struct clast_stmt
***next
;
1236 cloog_int_t val
, bound
;
1240 /* Insert a modulo guard for constraint c.
1242 static int insert_modulo_guard_constraint(CloogConstraint
*c
, void *user
)
1244 struct clast_modulo_guard_data
*d
= (struct clast_modulo_guard_data
*) user
;
1245 int level
= d
->level
;
1246 CloogInfos
*infos
= d
->infos
;
1247 int i
, nb_elts
= 0, len
, len2
, nb_iter
, nb_par
;
1249 struct cloog_vec
*line_vector
;
1252 len
= cloog_constraint_total_dimension(c
) + 2;
1253 len2
= cloog_equal_total_dimension(infos
->equal
) + 2;
1254 nb_par
= infos
->names
->nb_parameters
;
1255 nb_iter
= len
- 2 - nb_par
;
1257 line_vector
= cloog_vec_alloc(len
);
1258 line
= line_vector
->p
;
1259 cloog_constraint_copy_coefficients(c
, line
+ 1);
1261 if (cloog_int_is_pos(line
[level
]))
1262 cloog_seq_neg(line
+ 1, line
+ 1, len
- 1);
1263 cloog_int_neg(line
[level
], line
[level
]);
1264 assert(cloog_int_is_pos(line
[level
]));
1267 for (i
= 1; i
<= len
-1; ++i
) {
1270 cloog_int_fdiv_r(line
[i
], line
[i
], line
[level
]);
1271 if (cloog_int_is_zero(line
[i
]))
1279 if (nb_elts
|| !cloog_int_is_zero(line
[len
-1])) {
1280 struct clast_reduction
*r
;
1283 r
= new_clast_reduction(clast_red_sum
, nb_elts
+ 1);
1286 /* First, the modulo guard : the iterators... */
1287 for (i
= level
- 1; i
>= 1; --i
)
1288 eliminate_using_stride_constraint(line
, len
, nb_iter
,
1289 infos
->stride
[i
- 1], i
, line
[level
]);
1290 for (i
=1;i
<=nb_iter
;i
++) {
1291 if (i
== level
|| cloog_int_is_zero(line
[i
]))
1294 name
= cloog_names_name_at_level(infos
->names
, i
);
1296 r
->elts
[nb_elts
++] = &new_clast_term(line
[i
],
1297 &new_clast_name(name
)->expr
)->expr
;
1300 /* ...the parameters... */
1301 for (i
=nb_iter
+1;i
<=len
-2;i
++) {
1302 if (cloog_int_is_zero(line
[i
]))
1305 name
= infos
->names
->parameters
[i
-nb_iter
-1] ;
1306 r
->elts
[nb_elts
++] = &new_clast_term(line
[i
],
1307 &new_clast_name(name
)->expr
)->expr
;
1310 constant
= nb_elts
== 0;
1311 /* ...the constant. */
1312 if (!cloog_int_is_zero(line
[len
-1]))
1313 r
->elts
[nb_elts
++] = &new_clast_term(line
[len
-1], NULL
)->expr
;
1315 /* our initial computation may have been an overestimate */
1319 d
->empty
= !constant_modulo_guard_is_satisfied(d
->lower
, d
->bound
,
1321 free_clast_reduction(r
);
1323 insert_computed_modulo_guard(r
, d
->lower
, line
[level
], d
->bound
,
1327 cloog_vec_free(line_vector
);
1334 * insert_modulo_guard:
1335 * This function inserts a modulo guard corresponding to an equality
1336 * or a pair of inequalities.
1337 * Returns 0 if the modulo guard is discovered to be unsatisfiable.
1339 * See insert_equation.
1340 * - matrix is the polyhedron containing all the constraints,
1341 * - upper and lower are the line numbers of the constraint in matrix
1342 * we want to print; in particular, if we want to print an equality,
1343 * then lower == -1 and upper is the row of the equality; if we want
1344 * to print an inequality, then upper is the row of the upper bound
1345 * and lower in the row of the lower bound
1346 * - level is the column number of the element in matrix we want to use,
1347 * - the infos structure gives the user some options about code printing,
1348 * the number of parameters in matrix (nb_par), and the arrays of iterator
1349 * names and parameters (iters and params).
1351 static int insert_modulo_guard(CloogConstraint
*upper
,
1352 CloogConstraint
*lower
, int level
,
1353 struct clast_stmt
***next
, CloogInfos
*infos
)
1356 CloogConstraintSet
*set
;
1357 struct clast_modulo_guard_data data
= { lower
, level
, next
, infos
, 0 };
1359 cloog_int_init(data
.val
);
1360 cloog_constraint_coefficient_get(upper
, level
-1, &data
.val
);
1361 if (cloog_int_is_one(data
.val
) || cloog_int_is_neg_one(data
.val
)) {
1362 cloog_int_clear(data
.val
);
1366 nb_par
= infos
->names
->nb_parameters
;
1368 cloog_int_init(data
.bound
);
1369 /* Check if would be emitting the redundant constraint mod(e,m) <= m-1 */
1370 if (cloog_constraint_is_valid(lower
)) {
1371 cloog_constraint_constant_get(upper
, &data
.val
);
1372 cloog_constraint_constant_get(lower
, &data
.bound
);
1373 cloog_int_add(data
.bound
, data
.val
, data
.bound
);
1374 cloog_constraint_coefficient_get(lower
, level
-1, &data
.val
);
1375 cloog_int_sub_ui(data
.val
, data
.val
, 1);
1376 if (cloog_int_eq(data
.val
, data
.bound
)) {
1377 cloog_int_clear(data
.val
);
1378 cloog_int_clear(data
.bound
);
1383 set
= cloog_constraint_set_for_reduction(upper
, lower
);
1384 set
= cloog_constraint_set_reduce(set
, level
, infos
->equal
, nb_par
, &data
.bound
);
1385 cloog_constraint_set_foreach_constraint(set
,
1386 insert_modulo_guard_constraint
, &data
);
1388 cloog_constraint_set_free(set
);
1389 cloog_int_clear(data
.val
);
1390 cloog_int_clear(data
.bound
);
1397 * We found an equality or a pair of inequalities identifying
1398 * a loop with a single iteration, but the user wants us to generate
1399 * a loop anyway, so we do it here.
1401 static int insert_equation_as_loop(CloogConstraint
*upper
,
1402 CloogConstraint
*lower
, int level
, struct clast_stmt
***next
,
1405 const char *iterator
= cloog_names_name_at_level(infos
->names
, level
);
1406 struct clast_expr
*e1
, *e2
;
1407 struct clast_for
*f
;
1409 e2
= clast_bound_from_constraint(upper
, level
, infos
->names
);
1410 if (!cloog_constraint_is_valid(lower
))
1411 e1
= clast_expr_copy(e2
);
1413 e1
= clast_bound_from_constraint(lower
, level
, infos
->names
);
1414 f
= new_clast_for(iterator
, e1
, e2
, infos
->stride
[level
-1]);
1418 cloog_constraint_release(lower
);
1419 cloog_constraint_release(upper
);
1425 * insert_equation function:
1426 * This function inserts an equality
1427 * constraint according to an element in the clast.
1428 * Returns 1 if the calling function should recurse into inner loops.
1430 * An equality can be preceded by a 'modulo guard'.
1431 * For instance, consider the constraint i -2*j = 0 and the
1432 * element j: pprint_equality should return 'if(i%2==0) { j = i/2 ;'.
1433 * - matrix is the polyhedron containing all the constraints,
1434 * - num is the line number of the constraint in matrix we want to print,
1435 * - level is the column number of the element in matrix we want to use,
1436 * - the infos structure gives the user some options about code printing,
1437 * the number of parameters in matrix (nb_par), and the arrays of iterator
1438 * names and parameters (iters and params).
1440 * - November 13th 2001: first version.
1441 * - June 26th 2003: simplification of the modulo guards (remove parts such as
1442 * modulo is 0, compare vivien or vivien2 with a previous
1443 * version for an idea).
1444 * - June 29th 2003: non-unit strides support.
1445 * - July 14th 2003: (debug) no more print the constant in the modulo guard when
1446 * it was previously included in a stride calculation.
1448 static int insert_equation(CloogConstraint
*upper
, CloogConstraint
*lower
,
1449 int level
, struct clast_stmt
***next
, CloogInfos
*infos
)
1451 struct clast_expr
*e
;
1452 struct clast_assignment
*ass
;
1454 if (!infos
->options
->otl
)
1455 return insert_equation_as_loop(upper
, lower
, level
, next
, infos
);
1457 if (!insert_modulo_guard(upper
, lower
, level
, next
, infos
)) {
1458 cloog_constraint_release(lower
);
1459 cloog_constraint_release(upper
);
1464 if (cloog_constraint_is_valid(lower
) ||
1465 !clast_equal_add(infos
->equal
, NULL
, level
, upper
, infos
))
1466 { /* Finally, the equality. */
1468 /* If we have to make a block by dimension, we start the block. Function
1469 * pprint knows if there is an equality, if this is the case, it checks
1470 * for the same following condition to close the brace.
1472 if (infos
->options
->block
) {
1473 struct clast_block
*b
= new_clast_block();
1478 e
= clast_bound_from_constraint(upper
, level
, infos
->names
);
1479 ass
= new_clast_assignment(cloog_names_name_at_level(infos
->names
, level
), e
);
1481 **next
= &ass
->stmt
;
1482 *next
= &(**next
)->next
;
1485 cloog_constraint_release(lower
);
1486 cloog_constraint_release(upper
);
1493 * Insert a loop that is executed exactly once as an assignment.
1494 * In particular, the loop
1496 * for (i = e; i <= e; ++i) {
1506 static void insert_otl_for(CloogConstraintSet
*constraints
, int level
,
1507 struct clast_expr
*e
, struct clast_stmt
***next
, CloogInfos
*infos
)
1509 const char *iterator
;
1511 iterator
= cloog_names_name_at_level(infos
->names
, level
);
1513 if (!clast_equal_add(infos
->equal
, constraints
, level
,
1514 cloog_constraint_invalid(), infos
)) {
1515 struct clast_assignment
*ass
;
1516 if (infos
->options
->block
) {
1517 struct clast_block
*b
= new_clast_block();
1521 ass
= new_clast_assignment(iterator
, e
);
1522 **next
= &ass
->stmt
;
1523 *next
= &(**next
)->next
;
1531 * Insert a loop that is executed at most once as an assignment followed
1532 * by a guard. In particular, the loop
1534 * for (i = e1; i <= e2; ++i) {
1546 static void insert_guarded_otl_for(CloogConstraintSet
*constraints
, int level
,
1547 struct clast_expr
*e1
, struct clast_expr
*e2
,
1548 struct clast_stmt
***next
, CloogInfos
*infos
)
1550 const char *iterator
;
1551 struct clast_assignment
*ass
;
1552 struct clast_guard
*guard
;
1554 iterator
= cloog_names_name_at_level(infos
->names
, level
);
1556 if (infos
->options
->block
) {
1557 struct clast_block
*b
= new_clast_block();
1561 ass
= new_clast_assignment(iterator
, e1
);
1562 **next
= &ass
->stmt
;
1563 *next
= &(**next
)->next
;
1565 guard
= new_clast_guard(1);
1566 guard
->eq
[0].sign
= -1;
1567 guard
->eq
[0].LHS
= &new_clast_term(infos
->state
->one
,
1568 &new_clast_name(iterator
)->expr
)->expr
;
1569 guard
->eq
[0].RHS
= e2
;
1571 **next
= &guard
->stmt
;
1572 *next
= &guard
->then
;
1577 * insert_for function:
1578 * This function inserts a for loop in the clast.
1579 * Returns 1 if the calling function should recurse into inner loops.
1581 * A loop header according to an element is the conjunction of a minimum and a
1582 * maximum on a given element (they give the loop bounds).
1583 * For instance, considering these constraints and the element j:
1587 * this function should return 'for (j=max(-i+9*M,4*M),j<=5*M;j++) {'.
1588 * If the given element is involved in modulo guards defined by
1589 * existentially quantified variables, then these guards should be
1590 * inserted inside the for loop. However, the constraints involved
1591 * in this guard should not be used in determining the lower and upper
1592 * bound of the loop. We therefore insert the guards first (which
1593 * removes the corresponding constraints from the constraint set)
1594 * and then reattach the guard inside the loop.
1595 * - constraints contains all constraints,
1596 * - level is the column number of the element in matrix we want to use,
1597 * - otl is set if the loop is executed at most once,
1598 * - the infos structure gives the user some options about code printing,
1599 * the number of parameters in matrix (nb_par), and the arrays of iterator
1600 * names and parameters (iters and params).
1602 static int insert_for(CloogConstraintSet
*constraints
, int level
, int otl
,
1603 struct clast_stmt
***next
, CloogInfos
*infos
)
1605 const char *iterator
;
1606 struct clast_expr
*e1
;
1607 struct clast_expr
*e2
;
1609 e1
= clast_minmax(constraints
, level
, 1, 0, 1, infos
);
1610 e2
= clast_minmax(constraints
, level
, 0, 0, 0, infos
);
1612 if (clast_expr_is_bigger_constant(e1
, e2
)) {
1613 free_clast_expr(e1
);
1614 free_clast_expr(e2
);
1618 /* If min and max are not equal there is a 'for' else, there is a '='.
1619 * In the special case e1 = e2 = NULL, this is an infinite loop
1620 * so this is not a '='.
1622 if (e1
&& e2
&& infos
->options
->otl
&& clast_expr_equal(e1
, e2
)) {
1623 free_clast_expr(e2
);
1624 insert_otl_for(constraints
, level
, e1
, next
, infos
);
1626 insert_guarded_otl_for(constraints
, level
, e1
, e2
, next
, infos
);
1628 struct clast_for
*f
;
1629 iterator
= cloog_names_name_at_level(infos
->names
, level
);
1630 f
= new_clast_for(iterator
, e1
, e2
, infos
->stride
[level
-1]);
1635 insert_extra_modulo_guards(constraints
, 0, next
, infos
);
1642 * insert_block function:
1643 * This function inserts a statement block.
1644 * - block is the statement block,
1645 * - level is the number of loops enclosing the statement,
1646 * - the infos structure gives the user some options about code printing,
1647 * the number of parameters in domain (nb_par), and the arrays of iterator
1648 * names and parameters (iters and params).
1650 * - September 21th 2003: first version (pick from pprint function).
1652 static void insert_block(CloogBlock
*block
, int level
,
1653 struct clast_stmt
***next
, CloogInfos
*infos
)
1655 CloogStatement
* statement
;
1656 struct clast_stmt
*subs
;
1661 for (statement
= block
->statement
; statement
; statement
= statement
->next
) {
1662 CloogStatement
*s_next
= statement
->next
;
1664 subs
= clast_equal(level
,infos
);
1666 statement
->next
= NULL
;
1667 **next
= &new_clast_user_stmt(statement
, subs
)->stmt
;
1668 statement
->next
= s_next
;
1669 *next
= &(**next
)->next
;
1675 * insert_loop function:
1676 * This function converts the content of a CloogLoop structure (loop) into a
1677 * clast_stmt (inserted at **next).
1678 * The iterator (level) of
1679 * the current loop is given by 'level': this is the column number of the
1680 * domain corresponding to the current loop iterator. The data of a loop are
1681 * written in this order:
1682 * 1. The guard of the loop, i.e. each constraint in the domain that does not
1683 * depend on the iterator (when the entry in the column 'level' is 0).
1684 * 2. The iteration domain of the iterator, given by the constraints in the
1685 * domain depending on the iterator, i.e.:
1686 * * an equality if the iterator has only one value (possibly preceded by
1687 * a guard verifying if this value is integral), *OR*
1688 * * a loop from the minimum possible value of the iterator to the maximum
1690 * 3. The included statement block.
1691 * 4. The inner loops (recursive call).
1692 * 5. The following loops (recursive call).
1693 * - level is the recursion level or the iteration level that we are printing,
1694 * - the infos structure gives the user some options about code printing,
1695 * the number of parameters in domain (nb_par), and the arrays of iterator
1696 * names and parameters (iters and params).
1698 * - November 2nd 2001: first version.
1699 * - March 6th 2003: infinite domain support.
1700 * - April 19th 2003: (debug) NULL loop support.
1701 * - June 29th 2003: non-unit strides support.
1702 * - April 28th 2005: (debug) level is level+equality when print statement!
1703 * - June 16th 2005: (debug) the N. Vasilache normalization step has been
1704 * added to avoid iteration duplication (see DaeGon Kim
1705 * bug in cloog_program_generate). Try vasilache.cloog
1706 * with and without the call to cloog_polylib_matrix_normalize,
1707 * using -f 8 -l 9 options for an idea.
1708 * - September 15th 2005: (debug) don't close equality braces when unnecessary.
1709 * - October 16th 2005: (debug) scalar value is saved for next loops.
1711 static void insert_loop(CloogLoop
* loop
, int level
,
1712 struct clast_stmt
***next
, CloogInfos
*infos
)
1715 CloogConstraintSet
*constraints
, *temp
;
1716 struct clast_stmt
**top
= *next
;
1717 CloogConstraint
*i
, *j
;
1720 /* It can happen that loop be NULL when an input polyhedron is empty. */
1724 /* The constraints do not always have a shape that allows us to generate code from it,
1725 * thus we normalize it, we also simplify it with the equalities.
1727 temp
= cloog_domain_constraints(loop
->domain
);
1728 cloog_constraint_set_normalize(temp
,level
);
1729 constraints
= cloog_constraint_set_simplify(temp
,infos
->equal
,level
,
1730 infos
->names
->nb_parameters
);
1731 cloog_constraint_set_free(temp
);
1733 infos
->stride
[level
- 1] = loop
->stride
;
1735 /* First of all we have to print the guard. */
1736 insert_guard(constraints
,level
, next
, infos
);
1738 if (level
&& cloog_constraint_set_contains_level(constraints
, level
,
1739 infos
->names
->nb_parameters
)) {
1740 /* We scan all the constraints to know in which case we are :
1741 * [[if] equation] or [for].
1743 if (cloog_constraint_is_valid(i
=
1744 cloog_constraint_set_defining_equality(constraints
, level
))) {
1745 empty_loop
= !insert_equation(i
, cloog_constraint_invalid(),
1746 level
, next
, infos
);
1748 } else if (cloog_constraint_is_valid(i
=
1749 cloog_constraint_set_defining_inequalities(constraints
,
1750 level
, &j
, infos
->names
->nb_parameters
))) {
1751 empty_loop
= !insert_equation(i
, j
, level
, next
, infos
);
1753 empty_loop
= !insert_for(constraints
, level
, loop
->otl
, next
, infos
);
1757 /* Finally, if there is an included statement block, print it. */
1758 insert_block(loop
->block
, level
+equality
, next
, infos
);
1760 /* Go to the next level. */
1761 if (loop
->inner
!= NULL
)
1762 insert_loop(loop
->inner
, level
+1, next
, infos
);
1766 cloog_equal_del(infos
->equal
,level
);
1767 cloog_constraint_set_free(constraints
);
1769 /* Go to the next loop on the same level. */
1771 top
= &(*top
)->next
;
1772 if (loop
->next
!= NULL
)
1773 insert_loop(loop
->next
, level
, &top
,infos
);
1777 struct clast_stmt
*cloog_clast_create(CloogProgram
*program
,
1778 CloogOptions
*options
)
1780 CloogInfos
*infos
= ALLOC(CloogInfos
);
1782 struct clast_stmt
*root
= &new_clast_root(program
->names
)->stmt
;
1783 struct clast_stmt
**next
= &root
->next
;
1785 infos
->state
= options
->state
;
1786 infos
->names
= program
->names
;
1787 infos
->options
= options
;
1788 infos
->scaldims
= program
->scaldims
;
1789 infos
->nb_scattdims
= program
->nb_scattdims
;
1791 /* Allocation for the array of strides, there is a +1 since the statement can
1792 * be included inside an external loop without iteration domain.
1794 nb_levels
= program
->names
->nb_scattering
+program
->names
->nb_iterators
+1;
1795 infos
->stride
= ALLOCN(CloogStride
*, nb_levels
);
1797 infos
->equal
= cloog_equal_alloc(nb_levels
,
1798 nb_levels
, program
->names
->nb_parameters
);
1800 insert_loop(program
->loop
, 0, &next
, infos
);
1802 cloog_equal_free(infos
->equal
);
1804 free(infos
->stride
);
1811 struct clast_stmt
*cloog_clast_create_from_input(CloogInput
*input
,
1812 CloogOptions
*options
)
1814 CloogProgram
*program
;
1815 struct clast_stmt
*root
;
1817 program
= cloog_program_alloc(input
->context
, input
->ud
, options
);
1820 program
= cloog_program_generate(program
, options
);
1822 root
= cloog_clast_create(program
, options
);
1823 cloog_program_free(program
);