clast.c: insert_modulo_guard_constraint: properly handle existentials
[cloog.git] / source / clast.c
blobb8dea97dbe7dd629ca2f59949219d86233180273
1 #include <stdlib.h>
2 #include <string.h>
3 #include <assert.h>
4 #include "../include/cloog/cloog.h"
6 #define ALLOC(type) (type*)malloc(sizeof(type))
7 #define ALLOCN(type,n) (type*)malloc((n)*sizeof(type))
9 /**
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.
17 struct clooginfos {
18 CloogState *state; /**< State. */
19 CloogStride **stride;
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. */
27 } ;
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,
42 CloogInfos *infos);
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,
47 int lower_bound,
48 CloogInfos *infos);
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;
68 n->name = name;
69 return n;
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);
78 t->var = v;
79 return t;
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;
87 b->type = t;
88 b->LHS = lhs;
89 cloog_int_init(b->RHS);
90 cloog_int_set(b->RHS, rhs);
91 return b;
94 struct clast_reduction *new_clast_reduction(enum clast_red_type t, int n)
96 int i;
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;
100 r->type = t;
101 r->n = n;
102 for (i = 0; i < n; ++i)
103 r->elts[i] = NULL;
104 return r;
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);
116 free(r);
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;
123 r->stmt.next = NULL;
124 r->names = cloog_names_copy(names);
125 return r;
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);
137 free(a);
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;
145 a->stmt.next = NULL;
146 a->LHS = lhs;
147 a->RHS = rhs;
148 return a;
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);
161 free(u);
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;
169 u->stmt.next = NULL;
170 u->statement = cloog_statement_copy(stmt);
171 u->substitutions = subs;
172 return u;
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);
184 free(b);
187 struct clast_block *new_clast_block()
189 struct clast_block *b = malloc(sizeof(struct clast_block));
190 b->stmt.op = &stmt_block;
191 b->stmt.next = NULL;
192 b->body = NULL;
193 return b;
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);
208 free(f);
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;
216 f->stmt.next = NULL;
217 f->iterator = it;
218 f->LB = LB;
219 f->UB = UB;
220 f->body = NULL;
221 cloog_int_init(f->stride);
222 if (stride)
223 cloog_int_set(f->stride, stride->stride);
224 else
225 cloog_int_set_si(f->stride, 1);
226 return f;
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)
235 int i;
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);
243 free(g);
246 struct clast_guard *new_clast_guard(int n)
248 int i;
249 struct clast_guard *g = malloc(sizeof(struct clast_guard) +
250 (n-1) * sizeof(struct clast_equation));
251 g->stmt.op = &stmt_guard;
252 g->stmt.next = NULL;
253 g->then = NULL;
254 g->n = n;
255 for (i = 0; i < n; ++i) {
256 g->eq[i].LHS = NULL;
257 g->eq[i].RHS = NULL;
259 return g;
262 void free_clast_name(struct clast_name *n)
264 free(n);
267 void free_clast_term(struct clast_term *t)
269 cloog_int_clear(t->val);
270 free_clast_expr(t->var);
271 free(t);
274 void free_clast_binary(struct clast_binary *b)
276 cloog_int_clear(b->RHS);
277 free_clast_expr(b->LHS);
278 free(b);
281 void free_clast_reduction(struct clast_reduction *r)
283 int i;
284 for (i = 0; i < r->n; ++i)
285 free_clast_expr(r->elts[i]);
286 free(r);
289 void free_clast_expr(struct clast_expr *e)
291 if (!e)
292 return;
293 switch (e->type) {
294 case clast_expr_name:
295 free_clast_name((struct clast_name*) e);
296 break;
297 case clast_expr_term:
298 free_clast_term((struct clast_term*) e);
299 break;
300 case clast_expr_red:
301 free_clast_reduction((struct clast_reduction*) e);
302 break;
303 case clast_expr_bin:
304 free_clast_binary((struct clast_binary*) e);
305 break;
306 default:
307 assert(0);
311 void free_clast_stmt(struct clast_stmt *s)
313 assert(s->op);
314 assert(s->op->free);
315 s->op->free(s);
318 void cloog_clast_free(struct clast_stmt *s)
320 struct clast_stmt *next;
321 while (s) {
322 next = s->next;
323 free_clast_stmt(s);
324 s = 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)
335 int c;
336 if (!t1->var && t2->var)
337 return -1;
338 if (t1->var && !t2->var)
339 return 1;
340 c = clast_expr_cmp(t1->var, t2->var);
341 if (c)
342 return c;
343 return cloog_int_cmp(t1->val, t2->val);
346 static int clast_binary_cmp(struct clast_binary *b1, struct clast_binary *b2)
348 int c;
350 if (b1->type != b2->type)
351 return b1->type - b2->type;
352 if ((c = cloog_int_cmp(b1->RHS, b2->RHS)))
353 return c;
354 return clast_expr_cmp(b1->LHS, b2->LHS);
357 static int clast_reduction_cmp(struct clast_reduction *r1, struct clast_reduction *r2)
359 int i;
360 int c;
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;
366 if (r1->n != r2->n)
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])))
370 return c;
371 return 0;
374 static int clast_expr_cmp(struct clast_expr *e1, struct clast_expr *e2)
376 if (!e1 && !e2)
377 return 0;
378 if (!e1)
379 return -1;
380 if (!e2)
381 return 1;
382 if (e1->type != e2->type)
383 return e1->type - e2->type;
384 switch (e1->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);
391 case clast_expr_bin:
392 return clast_binary_cmp((struct clast_binary*) e1,
393 (struct clast_binary*) e2);
394 case clast_expr_red:
395 return clast_reduction_cmp((struct clast_reduction*) e1,
396 (struct clast_reduction*) e2);
397 default:
398 assert(0);
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;
415 if (!e1 || !e2)
416 return 0;
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)
426 return 0;
427 t1 = (struct clast_term *)e1;
428 t2 = (struct clast_term *)e2;
429 if (t1->var || t2->var)
430 return 0;
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;
448 int cmp;
450 cmp = clast_expr_cmp(eq1->LHS, eq2->LHS);
451 if (cmp)
452 return cmp;
454 cmp = clast_expr_cmp(eq1->RHS, eq2->RHS);
455 if (cmp)
456 return cmp;
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)
475 if (!e)
476 return NULL;
477 switch (e->type) {
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: {
487 int i;
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]);
492 return &r2->expr;
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;
498 default:
499 assert(0);
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,
521 CloogInfos *infos)
523 if (level < infos->options->fsp)
524 return 0 ;
526 if ((cloog_equal_type(equal, level) == EQTYPE_EXAFFINE) &&
527 !infos->options->esp)
528 return 0 ;
530 return 1 ;
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,
548 CloogInfos *infos)
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)
574 int i ;
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);
586 } else {
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;
594 return a;
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]))
638 nb_elts++;
639 r = new_clast_reduction(clast_red_sum, nb_elts);
640 nb_elts = 0;
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]))
647 continue;
649 v = cloog_constraint_variable_expr(constraint, i, names);
651 if (sign == -1)
652 cloog_int_neg(temp,line[i]);
653 else
654 cloog_int_set(temp,line[i]);
656 r->elts[nb_elts++] = &new_clast_term(temp, v)->expr;
659 if (sign == -1) {
660 cloog_int_neg(numerator, line[len - 1]);
661 cloog_int_set(denominator, line[level]);
663 else {
664 cloog_int_set(numerator, line[len - 1]);
665 cloog_int_neg(denominator, line[level]);
668 /* Finally, the constant, and the final printing. */
669 if (nb_elts) {
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;
677 else
678 e = &new_clast_binary(clast_bin_fdiv, &r->expr, denominator)->expr;
679 } else
680 e = &new_clast_binary(clast_bin_div, &r->expr, denominator)->expr;
682 else
683 e = &r->expr;
684 } else {
685 free_clast_reduction(r);
686 if (cloog_int_is_zero(numerator))
687 e = &new_clast_term(numerator, NULL)->expr;
688 else
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;
695 else {
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])) {
700 /* nb<0 need max */
701 e = &new_clast_term(division, NULL)->expr;
702 } else {
703 /* nb<0 need min */
704 cloog_int_sub_ui(temp, division, 1);
705 e = &new_clast_term(temp, NULL)->expr;
708 else
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;
714 else
715 /* nb>0 need min */
716 e = &new_clast_term(division, NULL)->expr;
718 cloog_int_clear(division);
721 else
722 e = &new_clast_binary(clast_bin_div,
723 &new_clast_term(numerator, NULL)->expr,
724 denominator)->expr;
726 else
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);
738 return e;
742 /* Temporary structure for communication between clast_minmax and
743 * its cloog_constraint_set_foreach_constraint callback functions.
745 struct clast_minmax_data {
746 int level;
747 int max;
748 int guard;
749 int lower_bound;
750 CloogInfos *infos;
751 int n;
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))
761 return 0;
762 if (!d->max && !cloog_constraint_is_upper_bound(c, d->level - 1))
763 return 0;
764 if (cloog_constraint_is_equality(c))
765 return 0;
766 if (d->guard && cloog_constraint_involves(c, d->guard - 1))
767 return 0;
769 return 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))
780 return 0;
782 d->n++;
784 return 0;
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,
800 CloogStride *stride)
802 struct clast_term *t;
803 if (stride->constraint)
804 return;
805 if (expr->type != clast_expr_term)
806 return;
807 t = (struct clast_term *)expr;
808 if (t->var)
809 return;
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))
825 return 0;
827 d->r->elts[d->n] = clast_bound_from_constraint(c, d->level,
828 d->infos->names);
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]);
834 d->n++;
836 return 0;
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:
845 * -3*i +2*j -M >= 0
846 * 2*i +j >= 0
847 * -i -j +2*M >= 0
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,
865 int lower_bound,
866 CloogInfos *infos)
868 struct clast_minmax_data data = { level, max, guard, lower_bound, infos };
870 data.n = 0;
872 cloog_constraint_set_foreach_constraint(constraints, count_bounds, &data);
874 if (!data.n)
875 return NULL;
876 data.r = new_clast_reduction(max ? clast_red_max : clast_red_min, data.n);
878 data.n = 0;
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.
891 * Any constraint used in constructing a modulo guard is removed
892 * from the constraint set to avoid insert_guard
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)
898 int i;
899 int nb_iter;
900 int total_dim;
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);
940 return 0;
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);
951 return 0;
955 /* Temporary structure for communication between insert_guard and
956 * its cloog_constraint_set_foreach_constraint callback function.
958 struct clast_guard_data {
959 int level;
960 CloogInfos *infos;
961 int n;
962 int i;
963 int nb_iter;
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;
973 d->n++;
975 return 0;
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;
984 int minmax = -1;
985 struct clast_expr *v;
986 struct clast_term *t;
988 if (!cloog_constraint_involves(j, d->i - 1))
989 return 0;
991 if (d->level && d->nb_iter >= d->level &&
992 cloog_constraint_involves(j, d->level - 1))
993 return 0;
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;
1009 else
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);
1012 } else {
1013 int guarded;
1015 if (cloog_constraint_is_lower_bound(j, d->i - 1)) {
1016 minmax = 1;
1017 d->g->eq[d->n].sign = 1;
1018 } else {
1019 minmax = 0;
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,
1025 d->infos);
1027 d->n++;
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);
1035 if (minmax == -1)
1036 return 0;
1037 if (minmax == 1)
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);
1044 return 0;
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
1055 * iterator.
1056 * For instance, considering these constraints and the element j:
1057 * -3*i +2*j -M >= 0
1058 * 2*i +M >= 0
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)
1078 int total_dim;
1079 struct clast_guard_data data = { level, infos, 0 };
1081 if (!constraints)
1082 return;
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);
1092 data.n = 0;
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);
1108 data.g->n = data.n;
1109 if (data.n) {
1110 clast_guard_sort(data.g);
1111 **next = &data.g->stmt;
1112 *next = &data.g->then;
1113 } else
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
1121 * this function.
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);
1128 else
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)) {
1146 g->eq[0].LHS = e;
1147 cloog_int_set_si(bound, 0);
1148 g->eq[0].RHS = &new_clast_term(bound, NULL)->expr;
1149 g->eq[0].sign = 0;
1150 } else {
1151 g->eq[0].LHS = e;
1152 g->eq[0].RHS = &new_clast_term(bound, NULL)->expr;
1153 g->eq[0].sign = -1;
1156 **next = &g->stmt;
1157 *next = &g->then;
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
1176 * e + k i = s a
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)
1189 if (!stride)
1190 return;
1191 if (!cloog_int_is_divisible_by(stride->stride, m))
1192 return;
1194 if (stride->constraint) {
1195 int i;
1196 cloog_int_t t, v;
1198 cloog_int_init(t);
1199 cloog_int_init(v);
1200 cloog_int_mul(t, line[level], stride->factor);
1201 for (i = 1; i < level; ++i) {
1202 cloog_constraint_coefficient_get(stride->constraint,
1203 i - 1, &v);
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);
1216 cloog_int_clear(v);
1217 cloog_int_clear(t);
1218 } else {
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;
1232 int level;
1233 struct clast_stmt ***next;
1234 CloogInfos *infos;
1235 int empty;
1236 cloog_int_t val, bound;
1240 /* Insert a modulo guard for constraint c.
1241 * The constraint may be either an equality or an inequality.
1242 * Since this function returns -1, it is only called on a single constraint.
1243 * In case of an inequality, the constraint is usually an upper bound
1244 * on d->level. However, if this variable is an existentially
1245 * quantified variable, the upper bound constraint may get removed
1246 * as trivially holding and then this function is called with
1247 * a lower bound instead. In this case, we need to adjust the constraint
1248 * based on the sum of the constant terms of the lower and upper bound
1249 * stored in d->bound.
1251 static int insert_modulo_guard_constraint(CloogConstraint *c, void *user)
1253 struct clast_modulo_guard_data *d = (struct clast_modulo_guard_data *) user;
1254 int level = d->level;
1255 CloogInfos *infos = d->infos;
1256 int i, nb_elts = 0, len, len2, nb_iter, nb_par;
1257 int constant;
1258 struct cloog_vec *line_vector;
1259 cloog_int_t *line;
1261 len = cloog_constraint_total_dimension(c) + 2;
1262 len2 = cloog_equal_total_dimension(infos->equal) + 2;
1263 nb_par = infos->names->nb_parameters;
1264 nb_iter = len - 2 - nb_par;
1266 line_vector = cloog_vec_alloc(len);
1267 line = line_vector->p;
1268 cloog_constraint_copy_coefficients(c, line + 1);
1270 if (cloog_int_is_pos(line[level])) {
1271 cloog_seq_neg(line + 1, line + 1, len - 1);
1272 if (!cloog_constraint_is_equality(c))
1273 cloog_int_add(line[len - 1], line[len - 1], d->bound);
1275 cloog_int_neg(line[level], line[level]);
1276 assert(cloog_int_is_pos(line[level]));
1278 nb_elts = 0;
1279 for (i = 1; i <= len-1; ++i) {
1280 if (i == level)
1281 continue;
1282 cloog_int_fdiv_r(line[i], line[i], line[level]);
1283 if (cloog_int_is_zero(line[i]))
1284 continue;
1285 if (i == len-1)
1286 continue;
1288 nb_elts++;
1291 if (nb_elts || !cloog_int_is_zero(line[len-1])) {
1292 struct clast_reduction *r;
1293 const char *name;
1295 r = new_clast_reduction(clast_red_sum, nb_elts + 1);
1296 nb_elts = 0;
1298 /* First, the modulo guard : the iterators... */
1299 for (i = level - 1; i >= 1; --i)
1300 eliminate_using_stride_constraint(line, len, nb_iter,
1301 infos->stride[i - 1], i, line[level]);
1302 for (i=1;i<=nb_iter;i++) {
1303 if (i == level || cloog_int_is_zero(line[i]))
1304 continue;
1306 name = cloog_names_name_at_level(infos->names, i);
1308 r->elts[nb_elts++] = &new_clast_term(line[i],
1309 &new_clast_name(name)->expr)->expr;
1312 /* ...the parameters... */
1313 for (i=nb_iter+1;i<=len-2;i++) {
1314 if (cloog_int_is_zero(line[i]))
1315 continue;
1317 name = infos->names->parameters[i-nb_iter-1] ;
1318 r->elts[nb_elts++] = &new_clast_term(line[i],
1319 &new_clast_name(name)->expr)->expr;
1322 constant = nb_elts == 0;
1323 /* ...the constant. */
1324 if (!cloog_int_is_zero(line[len-1]))
1325 r->elts[nb_elts++] = &new_clast_term(line[len-1], NULL)->expr;
1327 /* our initial computation may have been an overestimate */
1328 r->n = nb_elts;
1330 if (constant) {
1331 d->empty = !constant_modulo_guard_is_satisfied(d->lower, d->bound,
1332 line[len - 1]);
1333 free_clast_reduction(r);
1334 } else
1335 insert_computed_modulo_guard(r, d->lower, line[level], d->bound,
1336 d->next);
1339 cloog_vec_free(line_vector);
1341 return -1;
1346 * insert_modulo_guard:
1347 * This function inserts a modulo guard corresponding to an equality
1348 * or a pair of inequalities.
1349 * Returns 0 if the modulo guard is discovered to be unsatisfiable.
1351 * See insert_equation.
1352 * - matrix is the polyhedron containing all the constraints,
1353 * - upper and lower are the line numbers of the constraint in matrix
1354 * we want to print; in particular, if we want to print an equality,
1355 * then lower == -1 and upper is the row of the equality; if we want
1356 * to print an inequality, then upper is the row of the upper bound
1357 * and lower in the row of the lower bound
1358 * - level is the column number of the element in matrix we want to use,
1359 * - the infos structure gives the user some options about code printing,
1360 * the number of parameters in matrix (nb_par), and the arrays of iterator
1361 * names and parameters (iters and params).
1363 static int insert_modulo_guard(CloogConstraint *upper,
1364 CloogConstraint *lower, int level,
1365 struct clast_stmt ***next, CloogInfos *infos)
1367 int nb_par;
1368 CloogConstraintSet *set;
1369 struct clast_modulo_guard_data data = { lower, level, next, infos, 0 };
1371 cloog_int_init(data.val);
1372 cloog_constraint_coefficient_get(upper, level-1, &data.val);
1373 if (cloog_int_is_one(data.val) || cloog_int_is_neg_one(data.val)) {
1374 cloog_int_clear(data.val);
1375 return 1;
1378 nb_par = infos->names->nb_parameters;
1380 cloog_int_init(data.bound);
1381 /* Check if would be emitting the redundant constraint mod(e,m) <= m-1 */
1382 if (cloog_constraint_is_valid(lower)) {
1383 cloog_constraint_constant_get(upper, &data.val);
1384 cloog_constraint_constant_get(lower, &data.bound);
1385 cloog_int_add(data.bound, data.val, data.bound);
1386 cloog_constraint_coefficient_get(lower, level-1, &data.val);
1387 cloog_int_sub_ui(data.val, data.val, 1);
1388 if (cloog_int_eq(data.val, data.bound)) {
1389 cloog_int_clear(data.val);
1390 cloog_int_clear(data.bound);
1391 return 1;
1395 set = cloog_constraint_set_for_reduction(upper, lower);
1396 set = cloog_constraint_set_reduce(set, level, infos->equal, nb_par, &data.bound);
1397 cloog_constraint_set_foreach_constraint(set,
1398 insert_modulo_guard_constraint, &data);
1400 cloog_constraint_set_free(set);
1401 cloog_int_clear(data.val);
1402 cloog_int_clear(data.bound);
1404 return !data.empty;
1409 * We found an equality or a pair of inequalities identifying
1410 * a loop with a single iteration, but the user wants us to generate
1411 * a loop anyway, so we do it here.
1413 static int insert_equation_as_loop(CloogConstraint *upper,
1414 CloogConstraint *lower, int level, struct clast_stmt ***next,
1415 CloogInfos *infos)
1417 const char *iterator = cloog_names_name_at_level(infos->names, level);
1418 struct clast_expr *e1, *e2;
1419 struct clast_for *f;
1421 e2 = clast_bound_from_constraint(upper, level, infos->names);
1422 if (!cloog_constraint_is_valid(lower))
1423 e1 = clast_expr_copy(e2);
1424 else
1425 e1 = clast_bound_from_constraint(lower, level, infos->names);
1426 f = new_clast_for(iterator, e1, e2, infos->stride[level-1]);
1427 **next = &f->stmt;
1428 *next = &f->body;
1430 cloog_constraint_release(lower);
1431 cloog_constraint_release(upper);
1432 return 1;
1437 * insert_equation function:
1438 * This function inserts an equality
1439 * constraint according to an element in the clast.
1440 * Returns 1 if the calling function should recurse into inner loops.
1442 * An equality can be preceded by a 'modulo guard'.
1443 * For instance, consider the constraint i -2*j = 0 and the
1444 * element j: pprint_equality should return 'if(i%2==0) { j = i/2 ;'.
1445 * - matrix is the polyhedron containing all the constraints,
1446 * - num is the line number of the constraint in matrix we want to print,
1447 * - level is the column number of the element in matrix we want to use,
1448 * - the infos structure gives the user some options about code printing,
1449 * the number of parameters in matrix (nb_par), and the arrays of iterator
1450 * names and parameters (iters and params).
1452 * - November 13th 2001: first version.
1453 * - June 26th 2003: simplification of the modulo guards (remove parts such as
1454 * modulo is 0, compare vivien or vivien2 with a previous
1455 * version for an idea).
1456 * - June 29th 2003: non-unit strides support.
1457 * - July 14th 2003: (debug) no more print the constant in the modulo guard when
1458 * it was previously included in a stride calculation.
1460 static int insert_equation(CloogConstraint *upper, CloogConstraint *lower,
1461 int level, struct clast_stmt ***next, CloogInfos *infos)
1463 struct clast_expr *e;
1464 struct clast_assignment *ass;
1466 if (!infos->options->otl)
1467 return insert_equation_as_loop(upper, lower, level, next, infos);
1469 if (!insert_modulo_guard(upper, lower, level, next, infos)) {
1470 cloog_constraint_release(lower);
1471 cloog_constraint_release(upper);
1473 return 0;
1476 if (cloog_constraint_is_valid(lower) ||
1477 !clast_equal_add(infos->equal, NULL, level, upper, infos))
1478 { /* Finally, the equality. */
1480 /* If we have to make a block by dimension, we start the block. Function
1481 * pprint knows if there is an equality, if this is the case, it checks
1482 * for the same following condition to close the brace.
1484 if (infos->options->block) {
1485 struct clast_block *b = new_clast_block();
1486 **next = &b->stmt;
1487 *next = &b->body;
1490 e = clast_bound_from_constraint(upper, level, infos->names);
1491 ass = new_clast_assignment(cloog_names_name_at_level(infos->names, level), e);
1493 **next = &ass->stmt;
1494 *next = &(**next)->next;
1497 cloog_constraint_release(lower);
1498 cloog_constraint_release(upper);
1500 return 1;
1505 * Insert a loop that is executed exactly once as an assignment.
1506 * In particular, the loop
1508 * for (i = e; i <= e; ++i) {
1509 * S;
1512 * is generated as
1514 * i = e;
1515 * S;
1518 static void insert_otl_for(CloogConstraintSet *constraints, int level,
1519 struct clast_expr *e, struct clast_stmt ***next, CloogInfos *infos)
1521 const char *iterator;
1523 iterator = cloog_names_name_at_level(infos->names, level);
1525 if (!clast_equal_add(infos->equal, constraints, level,
1526 cloog_constraint_invalid(), infos)) {
1527 struct clast_assignment *ass;
1528 if (infos->options->block) {
1529 struct clast_block *b = new_clast_block();
1530 **next = &b->stmt;
1531 *next = &b->body;
1533 ass = new_clast_assignment(iterator, e);
1534 **next = &ass->stmt;
1535 *next = &(**next)->next;
1536 } else {
1537 free_clast_expr(e);
1543 * Insert a loop that is executed at most once as an assignment followed
1544 * by a guard. In particular, the loop
1546 * for (i = e1; i <= e2; ++i) {
1547 * S;
1550 * is generated as
1552 * i = e1;
1553 * if (i <= e2) {
1554 * S;
1558 static void insert_guarded_otl_for(CloogConstraintSet *constraints, int level,
1559 struct clast_expr *e1, struct clast_expr *e2,
1560 struct clast_stmt ***next, CloogInfos *infos)
1562 const char *iterator;
1563 struct clast_assignment *ass;
1564 struct clast_guard *guard;
1566 iterator = cloog_names_name_at_level(infos->names, level);
1568 if (infos->options->block) {
1569 struct clast_block *b = new_clast_block();
1570 **next = &b->stmt;
1571 *next = &b->body;
1573 ass = new_clast_assignment(iterator, e1);
1574 **next = &ass->stmt;
1575 *next = &(**next)->next;
1577 guard = new_clast_guard(1);
1578 guard->eq[0].sign = -1;
1579 guard->eq[0].LHS = &new_clast_term(infos->state->one,
1580 &new_clast_name(iterator)->expr)->expr;
1581 guard->eq[0].RHS = e2;
1583 **next = &guard->stmt;
1584 *next = &guard->then;
1589 * insert_for function:
1590 * This function inserts a for loop in the clast.
1591 * Returns 1 if the calling function should recurse into inner loops.
1593 * A loop header according to an element is the conjunction of a minimum and a
1594 * maximum on a given element (they give the loop bounds).
1595 * For instance, considering these constraints and the element j:
1596 * i + j -9*M >= 0
1597 * -j +5*M >= 0
1598 * j -4*M >= 0
1599 * this function should return 'for (j=max(-i+9*M,4*M),j<=5*M;j++) {'.
1600 * - constraints contains all constraints,
1601 * - level is the column number of the element in matrix we want to use,
1602 * - otl is set if the loop is executed at most once,
1603 * - the infos structure gives the user some options about code printing,
1604 * the number of parameters in matrix (nb_par), and the arrays of iterator
1605 * names and parameters (iters and params).
1607 static int insert_for(CloogConstraintSet *constraints, int level, int otl,
1608 struct clast_stmt ***next, CloogInfos *infos)
1610 const char *iterator;
1611 struct clast_expr *e1;
1612 struct clast_expr *e2;
1614 e1 = clast_minmax(constraints, level, 1, 0, 1, infos);
1615 e2 = clast_minmax(constraints, level, 0, 0, 0, infos);
1617 if (clast_expr_is_bigger_constant(e1, e2)) {
1618 free_clast_expr(e1);
1619 free_clast_expr(e2);
1620 return 0;
1623 /* If min and max are not equal there is a 'for' else, there is a '='.
1624 * In the special case e1 = e2 = NULL, this is an infinite loop
1625 * so this is not a '='.
1627 if (e1 && e2 && infos->options->otl && clast_expr_equal(e1, e2)) {
1628 free_clast_expr(e2);
1629 insert_otl_for(constraints, level, e1, next, infos);
1630 } else if (otl) {
1631 insert_guarded_otl_for(constraints, level, e1, e2, next, infos);
1632 } else {
1633 struct clast_for *f;
1634 iterator = cloog_names_name_at_level(infos->names, level);
1635 f = new_clast_for(iterator, e1, e2, infos->stride[level-1]);
1636 **next = &f->stmt;
1637 *next = &f->body;
1640 return 1;
1645 * insert_block function:
1646 * This function inserts a statement block.
1647 * - block is the statement block,
1648 * - level is the number of loops enclosing the statement,
1649 * - the infos structure gives the user some options about code printing,
1650 * the number of parameters in domain (nb_par), and the arrays of iterator
1651 * names and parameters (iters and params).
1653 * - September 21th 2003: first version (pick from pprint function).
1655 static void insert_block(CloogBlock *block, int level,
1656 struct clast_stmt ***next, CloogInfos *infos)
1658 CloogStatement * statement ;
1659 struct clast_stmt *subs;
1661 if (!block)
1662 return;
1664 for (statement = block->statement; statement; statement = statement->next) {
1665 CloogStatement *s_next = statement->next;
1667 subs = clast_equal(level,infos);
1669 statement->next = NULL;
1670 **next = &new_clast_user_stmt(statement, subs)->stmt;
1671 statement->next = s_next;
1672 *next = &(**next)->next;
1678 * insert_loop function:
1679 * This function converts the content of a CloogLoop structure (loop) into a
1680 * clast_stmt (inserted at **next).
1681 * The iterator (level) of
1682 * the current loop is given by 'level': this is the column number of the
1683 * domain corresponding to the current loop iterator. The data of a loop are
1684 * written in this order:
1685 * 1. The guard of the loop, i.e. each constraint in the domain that does not
1686 * depend on the iterator (when the entry in the column 'level' is 0).
1687 * 2. The iteration domain of the iterator, given by the constraints in the
1688 * domain depending on the iterator, i.e.:
1689 * * an equality if the iterator has only one value (possibly preceded by
1690 * a guard verifying if this value is integral), *OR*
1691 * * a loop from the minimum possible value of the iterator to the maximum
1692 * possible value.
1693 * 3. The included statement block.
1694 * 4. The inner loops (recursive call).
1695 * 5. The following loops (recursive call).
1696 * - level is the recursion level or the iteration level that we are printing,
1697 * - the infos structure gives the user some options about code printing,
1698 * the number of parameters in domain (nb_par), and the arrays of iterator
1699 * names and parameters (iters and params).
1701 * - November 2nd 2001: first version.
1702 * - March 6th 2003: infinite domain support.
1703 * - April 19th 2003: (debug) NULL loop support.
1704 * - June 29th 2003: non-unit strides support.
1705 * - April 28th 2005: (debug) level is level+equality when print statement!
1706 * - June 16th 2005: (debug) the N. Vasilache normalization step has been
1707 * added to avoid iteration duplication (see DaeGon Kim
1708 * bug in cloog_program_generate). Try vasilache.cloog
1709 * with and without the call to cloog_polylib_matrix_normalize,
1710 * using -f 8 -l 9 options for an idea.
1711 * - September 15th 2005: (debug) don't close equality braces when unnecessary.
1712 * - October 16th 2005: (debug) scalar value is saved for next loops.
1714 static void insert_loop(CloogLoop * loop, int level,
1715 struct clast_stmt ***next, CloogInfos *infos)
1717 int equality = 0;
1718 CloogConstraintSet *constraints, *temp;
1719 struct clast_stmt **top = *next;
1720 CloogConstraint *i, *j;
1721 int empty_loop = 0;
1723 /* It can happen that loop be NULL when an input polyhedron is empty. */
1724 if (loop == NULL)
1725 return;
1727 /* The constraints do not always have a shape that allows us to generate code from it,
1728 * thus we normalize it, we also simplify it with the equalities.
1730 temp = cloog_domain_constraints(loop->domain);
1731 cloog_constraint_set_normalize(temp,level);
1732 constraints = cloog_constraint_set_simplify(temp,infos->equal,level,
1733 infos->names->nb_parameters);
1734 cloog_constraint_set_free(temp);
1735 if (level)
1736 infos->stride[level - 1] = loop->stride;
1738 /* First of all we have to print the guard. */
1739 insert_guard(constraints,level, next, infos);
1741 if (level && cloog_constraint_set_contains_level(constraints, level,
1742 infos->names->nb_parameters)) {
1743 /* We scan all the constraints to know in which case we are :
1744 * [[if] equation] or [for].
1746 if (cloog_constraint_is_valid(i =
1747 cloog_constraint_set_defining_equality(constraints, level))) {
1748 empty_loop = !insert_equation(i, cloog_constraint_invalid(),
1749 level, next, infos);
1750 equality = 1 ;
1751 } else if (cloog_constraint_is_valid(i =
1752 cloog_constraint_set_defining_inequalities(constraints,
1753 level, &j, infos->names->nb_parameters))) {
1754 empty_loop = !insert_equation(i, j, level, next, infos);
1755 } else
1756 empty_loop = !insert_for(constraints, level, loop->otl, next, infos);
1759 if (!empty_loop) {
1760 /* Finally, if there is an included statement block, print it. */
1761 insert_block(loop->block, level+equality, next, infos);
1763 /* Go to the next level. */
1764 if (loop->inner != NULL)
1765 insert_loop(loop->inner, level+1, next, infos);
1768 if (level)
1769 cloog_equal_del(infos->equal,level);
1770 cloog_constraint_set_free(constraints);
1772 /* Go to the next loop on the same level. */
1773 while (*top)
1774 top = &(*top)->next;
1775 if (loop->next != NULL)
1776 insert_loop(loop->next, level, &top,infos);
1780 struct clast_stmt *cloog_clast_create(CloogProgram *program,
1781 CloogOptions *options)
1783 CloogInfos *infos = ALLOC(CloogInfos);
1784 int nb_levels;
1785 struct clast_stmt *root = &new_clast_root(program->names)->stmt;
1786 struct clast_stmt **next = &root->next;
1788 infos->state = options->state;
1789 infos->names = program->names;
1790 infos->options = options;
1791 infos->scaldims = program->scaldims;
1792 infos->nb_scattdims = program->nb_scattdims;
1794 /* Allocation for the array of strides, there is a +1 since the statement can
1795 * be included inside an external loop without iteration domain.
1797 nb_levels = program->names->nb_scattering+program->names->nb_iterators+1;
1798 infos->stride = ALLOCN(CloogStride *, nb_levels);
1800 infos->equal = cloog_equal_alloc(nb_levels,
1801 nb_levels, program->names->nb_parameters);
1803 insert_loop(program->loop, 0, &next, infos);
1805 cloog_equal_free(infos->equal);
1807 free(infos->stride);
1808 free(infos);
1810 return root;
1814 struct clast_stmt *cloog_clast_create_from_input(CloogInput *input,
1815 CloogOptions *options)
1817 CloogProgram *program;
1818 struct clast_stmt *root;
1820 program = cloog_program_alloc(input->context, input->ud, options);
1821 free(input);
1823 program = cloog_program_generate(program, options);
1825 root = cloog_clast_create(program, options);
1826 cloog_program_free(program);
1828 return root;