update isl for change in isl_map_identity
[cloog/uuh.git] / source / clast.c
blob33e0a7b12fa4db282d543de340462e62a49d8f6a
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 stride_level; /**< Number of valid entries in stride array. */
21 int nb_scattdims ; /**< Scattering dimension number. */
22 int * scaldims ; /**< Boolean array saying whether a given
23 * scattering dimension is scalar or not.
25 CloogNames * names ; /**< Names of iterators and parameters. */
26 CloogOptions * options ; /**< Options on CLooG's behaviour. */
27 CloogEqualities *equal; /**< Matrix of equalities. */
28 } ;
30 typedef struct clooginfos CloogInfos ;
32 static int clast_expr_cmp(struct clast_expr *e1, struct clast_expr *e2);
33 static int clast_term_cmp(struct clast_term *t1, struct clast_term *t2);
34 static int clast_binary_cmp(struct clast_binary *b1, struct clast_binary *b2);
35 static int clast_reduction_cmp(struct clast_reduction *r1,
36 struct clast_reduction *r2);
38 static struct clast_expr *clast_expr_copy(struct clast_expr *e);
40 static int clast_equal_add(CloogEqualities *equal,
41 CloogConstraintSet *constraints,
42 int level, CloogConstraint *constraint,
43 CloogInfos *infos);
45 static struct clast_stmt *clast_equal(int level, CloogInfos *infos);
46 static struct clast_expr *clast_minmax(CloogConstraintSet *constraints,
47 int level, int max, int guard,
48 int lower_bound,
49 CloogInfos *infos);
50 static void insert_guard(CloogConstraintSet *constraints, int level,
51 struct clast_stmt ***next, CloogInfos *infos);
52 static int insert_modulo_guard(CloogConstraint *upper,
53 CloogConstraint *lower, int level,
54 struct clast_stmt ***next, CloogInfos *infos);
55 static int insert_equation(CloogDomain *domain, CloogConstraint *upper,
56 CloogConstraint *lower, int level,
57 struct clast_stmt ***next, CloogInfos *infos);
58 static int insert_for(CloogDomain *domain, CloogConstraintSet *constraints,
59 int level, int otl, struct clast_stmt ***next,
60 CloogInfos *infos);
61 static void insert_block(CloogDomain *domain, CloogBlock *block, int level,
62 struct clast_stmt ***next, CloogInfos *infos);
63 static void insert_loop(CloogLoop * loop, int level,
64 struct clast_stmt ***next, CloogInfos *infos);
67 struct clast_name *new_clast_name(const char *name)
69 struct clast_name *n = malloc(sizeof(struct clast_name));
70 n->expr.type = clast_expr_name;
71 n->name = name;
72 return n;
75 struct clast_term *new_clast_term(cloog_int_t c, struct clast_expr *v)
77 struct clast_term *t = malloc(sizeof(struct clast_term));
78 t->expr.type = clast_expr_term;
79 cloog_int_init(t->val);
80 cloog_int_set(t->val, c);
81 t->var = v;
82 return t;
85 struct clast_binary *new_clast_binary(enum clast_bin_type t,
86 struct clast_expr *lhs, cloog_int_t rhs)
88 struct clast_binary *b = malloc(sizeof(struct clast_binary));
89 b->expr.type = clast_expr_bin;
90 b->type = t;
91 b->LHS = lhs;
92 cloog_int_init(b->RHS);
93 cloog_int_set(b->RHS, rhs);
94 return b;
97 struct clast_reduction *new_clast_reduction(enum clast_red_type t, int n)
99 int i;
100 struct clast_reduction *r;
101 r = malloc(sizeof(struct clast_reduction)+(n-1)*sizeof(struct clast_expr *));
102 r->expr.type = clast_expr_red;
103 r->type = t;
104 r->n = n;
105 for (i = 0; i < n; ++i)
106 r->elts[i] = NULL;
107 return r;
110 static void free_clast_root(struct clast_stmt *s);
112 const struct clast_stmt_op stmt_root = { free_clast_root };
114 static void free_clast_root(struct clast_stmt *s)
116 struct clast_root *r = (struct clast_root *)s;
117 assert(CLAST_STMT_IS_A(s, stmt_root));
118 cloog_names_free(r->names);
119 free(r);
122 struct clast_root *new_clast_root(CloogNames *names)
124 struct clast_root *r = malloc(sizeof(struct clast_root));
125 r->stmt.op = &stmt_root;
126 r->stmt.next = NULL;
127 r->names = cloog_names_copy(names);
128 return r;
131 static void free_clast_assignment(struct clast_stmt *s);
133 const struct clast_stmt_op stmt_ass = { free_clast_assignment };
135 static void free_clast_assignment(struct clast_stmt *s)
137 struct clast_assignment *a = (struct clast_assignment *)s;
138 assert(CLAST_STMT_IS_A(s, stmt_ass));
139 free_clast_expr(a->RHS);
140 free(a);
143 struct clast_assignment *new_clast_assignment(const char *lhs,
144 struct clast_expr *rhs)
146 struct clast_assignment *a = malloc(sizeof(struct clast_assignment));
147 a->stmt.op = &stmt_ass;
148 a->stmt.next = NULL;
149 a->LHS = lhs;
150 a->RHS = rhs;
151 return a;
154 static void free_clast_user_stmt(struct clast_stmt *s);
156 const struct clast_stmt_op stmt_user = { free_clast_user_stmt };
158 static void free_clast_user_stmt(struct clast_stmt *s)
160 struct clast_user_stmt *u = (struct clast_user_stmt *)s;
161 assert(CLAST_STMT_IS_A(s, stmt_user));
162 cloog_domain_free(u->domain);
163 cloog_statement_free(u->statement);
164 cloog_clast_free(u->substitutions);
165 free(u);
168 struct clast_user_stmt *new_clast_user_stmt(CloogDomain *domain,
169 CloogStatement *stmt, struct clast_stmt *subs)
171 struct clast_user_stmt *u = malloc(sizeof(struct clast_user_stmt));
172 u->stmt.op = &stmt_user;
173 u->stmt.next = NULL;
174 u->domain = cloog_domain_copy(domain);
175 u->statement = cloog_statement_copy(stmt);
176 u->substitutions = subs;
177 return u;
180 static void free_clast_block(struct clast_stmt *b);
182 const struct clast_stmt_op stmt_block = { free_clast_block };
184 static void free_clast_block(struct clast_stmt *s)
186 struct clast_block *b = (struct clast_block *)s;
187 assert(CLAST_STMT_IS_A(s, stmt_block));
188 cloog_clast_free(b->body);
189 free(b);
192 struct clast_block *new_clast_block()
194 struct clast_block *b = malloc(sizeof(struct clast_block));
195 b->stmt.op = &stmt_block;
196 b->stmt.next = NULL;
197 b->body = NULL;
198 return b;
201 static void free_clast_for(struct clast_stmt *s);
203 const struct clast_stmt_op stmt_for = { free_clast_for };
205 static void free_clast_for(struct clast_stmt *s)
207 struct clast_for *f = (struct clast_for *)s;
208 assert(CLAST_STMT_IS_A(s, stmt_for));
209 cloog_domain_free(f->domain);
210 free_clast_expr(f->LB);
211 free_clast_expr(f->UB);
212 cloog_int_clear(f->stride);
213 cloog_clast_free(f->body);
214 free(f);
217 struct clast_for *new_clast_for(CloogDomain *domain, const char *it,
218 struct clast_expr *LB, struct clast_expr *UB,
219 CloogStride *stride)
221 struct clast_for *f = malloc(sizeof(struct clast_for));
222 f->stmt.op = &stmt_for;
223 f->stmt.next = NULL;
224 f->domain = cloog_domain_copy(domain);
225 f->iterator = it;
226 f->LB = LB;
227 f->UB = UB;
228 f->body = NULL;
229 cloog_int_init(f->stride);
230 if (stride)
231 cloog_int_set(f->stride, stride->stride);
232 else
233 cloog_int_set_si(f->stride, 1);
234 return f;
237 static void free_clast_guard(struct clast_stmt *s);
239 const struct clast_stmt_op stmt_guard = { free_clast_guard };
241 static void free_clast_guard(struct clast_stmt *s)
243 int i;
244 struct clast_guard *g = (struct clast_guard *)s;
245 assert(CLAST_STMT_IS_A(s, stmt_guard));
246 cloog_clast_free(g->then);
247 for (i = 0; i < g->n; ++i) {
248 free_clast_expr(g->eq[i].LHS);
249 free_clast_expr(g->eq[i].RHS);
251 free(g);
254 struct clast_guard *new_clast_guard(int n)
256 int i;
257 struct clast_guard *g = malloc(sizeof(struct clast_guard) +
258 (n-1) * sizeof(struct clast_equation));
259 g->stmt.op = &stmt_guard;
260 g->stmt.next = NULL;
261 g->then = NULL;
262 g->n = n;
263 for (i = 0; i < n; ++i) {
264 g->eq[i].LHS = NULL;
265 g->eq[i].RHS = NULL;
267 return g;
270 void free_clast_name(struct clast_name *n)
272 free(n);
275 void free_clast_term(struct clast_term *t)
277 cloog_int_clear(t->val);
278 free_clast_expr(t->var);
279 free(t);
282 void free_clast_binary(struct clast_binary *b)
284 cloog_int_clear(b->RHS);
285 free_clast_expr(b->LHS);
286 free(b);
289 void free_clast_reduction(struct clast_reduction *r)
291 int i;
292 for (i = 0; i < r->n; ++i)
293 free_clast_expr(r->elts[i]);
294 free(r);
297 void free_clast_expr(struct clast_expr *e)
299 if (!e)
300 return;
301 switch (e->type) {
302 case clast_expr_name:
303 free_clast_name((struct clast_name*) e);
304 break;
305 case clast_expr_term:
306 free_clast_term((struct clast_term*) e);
307 break;
308 case clast_expr_red:
309 free_clast_reduction((struct clast_reduction*) e);
310 break;
311 case clast_expr_bin:
312 free_clast_binary((struct clast_binary*) e);
313 break;
314 default:
315 assert(0);
319 void free_clast_stmt(struct clast_stmt *s)
321 assert(s->op);
322 assert(s->op->free);
323 s->op->free(s);
326 void cloog_clast_free(struct clast_stmt *s)
328 struct clast_stmt *next;
329 while (s) {
330 next = s->next;
331 free_clast_stmt(s);
332 s = next;
336 static int clast_name_cmp(struct clast_name *n1, struct clast_name *n2)
338 return n1->name == n2->name ? 0 : strcmp(n1->name, n2->name);
341 static int clast_term_cmp(struct clast_term *t1, struct clast_term *t2)
343 int c;
344 if (!t1->var && t2->var)
345 return -1;
346 if (t1->var && !t2->var)
347 return 1;
348 c = clast_expr_cmp(t1->var, t2->var);
349 if (c)
350 return c;
351 return cloog_int_cmp(t1->val, t2->val);
354 static int clast_binary_cmp(struct clast_binary *b1, struct clast_binary *b2)
356 int c;
358 if (b1->type != b2->type)
359 return b1->type - b2->type;
360 if ((c = cloog_int_cmp(b1->RHS, b2->RHS)))
361 return c;
362 return clast_expr_cmp(b1->LHS, b2->LHS);
365 static int clast_reduction_cmp(struct clast_reduction *r1, struct clast_reduction *r2)
367 int i;
368 int c;
370 if (r1->n == 1 && r2->n == 1)
371 return clast_expr_cmp(r1->elts[0], r2->elts[0]);
372 if (r1->type != r2->type)
373 return r1->type - r2->type;
374 if (r1->n != r2->n)
375 return r1->n - r2->n;
376 for (i = 0; i < r1->n; ++i)
377 if ((c = clast_expr_cmp(r1->elts[i], r2->elts[i])))
378 return c;
379 return 0;
382 static int clast_expr_cmp(struct clast_expr *e1, struct clast_expr *e2)
384 if (!e1 && !e2)
385 return 0;
386 if (!e1)
387 return -1;
388 if (!e2)
389 return 1;
390 if (e1->type != e2->type)
391 return e1->type - e2->type;
392 switch (e1->type) {
393 case clast_expr_name:
394 return clast_name_cmp((struct clast_name*) e1,
395 (struct clast_name*) e2);
396 case clast_expr_term:
397 return clast_term_cmp((struct clast_term*) e1,
398 (struct clast_term*) e2);
399 case clast_expr_bin:
400 return clast_binary_cmp((struct clast_binary*) e1,
401 (struct clast_binary*) e2);
402 case clast_expr_red:
403 return clast_reduction_cmp((struct clast_reduction*) e1,
404 (struct clast_reduction*) e2);
405 default:
406 assert(0);
410 int clast_expr_equal(struct clast_expr *e1, struct clast_expr *e2)
412 return clast_expr_cmp(e1, e2) == 0;
416 * Return 1 is both expressions are constant terms and e1 is bigger than e2.
418 int clast_expr_is_bigger_constant(struct clast_expr *e1, struct clast_expr *e2)
420 struct clast_term *t1, *t2;
421 struct clast_reduction *r;
423 if (!e1 || !e2)
424 return 0;
425 if (e1->type == clast_expr_red) {
426 r = (struct clast_reduction *)e1;
427 return r->n == 1 && clast_expr_is_bigger_constant(r->elts[0], e2);
429 if (e2->type == clast_expr_red) {
430 r = (struct clast_reduction *)e2;
431 return r->n == 1 && clast_expr_is_bigger_constant(e1, r->elts[0]);
433 if (e1->type != clast_expr_term || e2->type != clast_expr_term)
434 return 0;
435 t1 = (struct clast_term *)e1;
436 t2 = (struct clast_term *)e2;
437 if (t1->var || t2->var)
438 return 0;
439 return cloog_int_gt(t1->val, t2->val);
442 static int qsort_expr_cmp(const void *p1, const void *p2)
444 return clast_expr_cmp(*(struct clast_expr **)p1, *(struct clast_expr **)p2);
447 static void clast_reduction_sort(struct clast_reduction *r)
449 qsort(&r->elts[0], r->n, sizeof(struct clast_expr *), qsort_expr_cmp);
452 static int qsort_eq_cmp(const void *p1, const void *p2)
454 struct clast_equation *eq1 = (struct clast_equation *)p1;
455 struct clast_equation *eq2 = (struct clast_equation *)p2;
456 int cmp;
458 cmp = clast_expr_cmp(eq1->LHS, eq2->LHS);
459 if (cmp)
460 return cmp;
462 cmp = clast_expr_cmp(eq1->RHS, eq2->RHS);
463 if (cmp)
464 return cmp;
466 return eq1->sign - eq2->sign;
470 * Sort equations in a clast_guard.
472 static void clast_guard_sort(struct clast_guard *g)
474 qsort(&g->eq[0], g->n, sizeof(struct clast_equation), qsort_eq_cmp);
479 * Construct a (deep) copy of an expression clast.
481 static struct clast_expr *clast_expr_copy(struct clast_expr *e)
483 if (!e)
484 return NULL;
485 switch (e->type) {
486 case clast_expr_name: {
487 struct clast_name* n = (struct clast_name*) e;
488 return &new_clast_name(n->name)->expr;
490 case clast_expr_term: {
491 struct clast_term* t = (struct clast_term*) e;
492 return &new_clast_term(t->val, clast_expr_copy(t->var))->expr;
494 case clast_expr_red: {
495 int i;
496 struct clast_reduction *r = (struct clast_reduction*) e;
497 struct clast_reduction *r2 = new_clast_reduction(r->type, r->n);
498 for (i = 0; i < r->n; ++i)
499 r2->elts[i] = clast_expr_copy(r->elts[i]);
500 return &r2->expr;
502 case clast_expr_bin: {
503 struct clast_binary *b = (struct clast_binary*) e;
504 return &new_clast_binary(b->type, clast_expr_copy(b->LHS), b->RHS)->expr;
506 default:
507 assert(0);
512 /******************************************************************************
513 * Equalities spreading functions *
514 ******************************************************************************/
518 * clast_equal_allow function:
519 * This function checks whether the options allow us to spread the equality or
520 * not. It returns 1 if so, 0 otherwise.
521 * - equal is the matrix of equalities,
522 * - level is the column number in equal of the element which is 'equal to',
523 * - line is the line number in equal of the constraint we want to study,
524 * - the infos structure gives the user all options on code printing and more.
526 * - October 27th 2005: first version (extracted from old pprint_equal_add).
528 static int clast_equal_allow(CloogEqualities *equal, int level, int line,
529 CloogInfos *infos)
531 if (level < infos->options->fsp)
532 return 0 ;
534 if ((cloog_equal_type(equal, level) == EQTYPE_EXAFFINE) &&
535 !infos->options->esp)
536 return 0 ;
538 return 1 ;
543 * clast_equal_add function:
544 * This function updates the row (level-1) of the equality matrix (equal) with
545 * the row that corresponds to the row (line) of the matrix (matrix). It returns
546 * 1 if the row can be updated, 0 otherwise.
547 * - equal is the matrix of equalities,
548 * - matrix is the matrix of constraints,
549 * - level is the column number in matrix of the element which is 'equal to',
550 * - line is the line number in matrix of the constraint we want to study,
551 * - the infos structure gives the user all options on code printing and more.
553 static int clast_equal_add(CloogEqualities *equal,
554 CloogConstraintSet *constraints,
555 int level, CloogConstraint *constraint,
556 CloogInfos *infos)
558 cloog_equal_add(equal, constraints, level, constraint,
559 infos->names->nb_parameters);
561 return clast_equal_allow(equal, level, level-1, infos);
567 * clast_equal function:
568 * This function prints the substitution data of a statement into a clast_stmt.
569 * Using this function instead of pprint_equal is useful for generating
570 * a compilable pseudo-code by using preprocessor macro for each statement.
571 * By opposition to pprint_equal, the result is less human-readable. For
572 * instance this function will print (i,i+3,k,3) where pprint_equal would
573 * return (j=i+3,l=3).
574 * - level is the number of loops enclosing the statement,
575 * - the infos structure gives the user all options on code printing and more.
577 * - March 12th 2004: first version.
578 * - November 21th 2005: (debug) now works well with GMP version.
580 static struct clast_stmt *clast_equal(int level, CloogInfos *infos)
582 int i ;
583 struct clast_expr *e;
584 struct clast_stmt *a = NULL;
585 struct clast_stmt **next = &a;
586 CloogEqualities *equal = infos->equal;
587 CloogConstraint *equal_constraint;
589 for (i=infos->names->nb_scattering;i<level-1;i++)
590 { if (cloog_equal_type(equal, i+1)) {
591 equal_constraint = cloog_equal_constraint(equal, i);
592 e = clast_bound_from_constraint(equal_constraint, i+1, infos->names);
593 cloog_constraint_release(equal_constraint);
594 } else {
595 e = &new_clast_term(infos->state->one, &new_clast_name(
596 cloog_names_name_at_level(infos->names, i+1))->expr)->expr;
598 *next = &new_clast_assignment(NULL, e)->stmt;
599 next = &(*next)->next;
602 return a;
607 * clast_bound_from_constraint function:
608 * This function returns a clast_expr containing the printing of the
609 * 'right part' of a constraint according to an element.
610 * For instance, for the constraint -3*i + 2*j - M >=0 and the element j,
611 * we have j >= (3*i + M)/2. As we are looking for integral solutions, this
612 * function should return 'ceild(3*i+M,2)'.
613 * - matrix is the polyhedron containing all the constraints,
614 * - line_num is the line number in domain of the constraint we want to print,
615 * - level is the column number in domain of the element we want to use,
616 * - names structure gives the user some options about code printing,
617 * the number of parameters in domain (nb_par), and the arrays of iterator
618 * names and parameters (iters and params).
620 * - November 2nd 2001: first version.
621 * - June 27th 2003: 64 bits version ready.
623 struct clast_expr *clast_bound_from_constraint(CloogConstraint *constraint,
624 int level, CloogNames *names)
626 int i, sign, nb_elts=0, len;
627 cloog_int_t *line, numerator, denominator, temp, division;
628 struct clast_expr *e = NULL;
629 struct cloog_vec *line_vector;
631 len = cloog_constraint_total_dimension(constraint) + 2;
632 line_vector = cloog_vec_alloc(len);
633 line = line_vector->p;
634 cloog_constraint_copy_coefficients(constraint, line+1);
635 cloog_int_init(temp);
636 cloog_int_init(numerator);
637 cloog_int_init(denominator);
639 if (!cloog_int_is_zero(line[level])) {
640 struct clast_reduction *r;
641 /* Maybe we need to invert signs in such a way that the element sign is>0.*/
642 sign = -cloog_int_sgn(line[level]);
644 for (i = 1, nb_elts = 0; i <= len - 1; ++i)
645 if (i != level && !cloog_int_is_zero(line[i]))
646 nb_elts++;
647 r = new_clast_reduction(clast_red_sum, nb_elts);
648 nb_elts = 0;
650 /* First, we have to print the iterators and the parameters. */
651 for (i = 1; i <= len - 2; i++) {
652 struct clast_expr *v;
654 if (i == level || cloog_int_is_zero(line[i]))
655 continue;
657 v = cloog_constraint_variable_expr(constraint, i, names);
659 if (sign == -1)
660 cloog_int_neg(temp,line[i]);
661 else
662 cloog_int_set(temp,line[i]);
664 r->elts[nb_elts++] = &new_clast_term(temp, v)->expr;
667 if (sign == -1) {
668 cloog_int_neg(numerator, line[len - 1]);
669 cloog_int_set(denominator, line[level]);
671 else {
672 cloog_int_set(numerator, line[len - 1]);
673 cloog_int_neg(denominator, line[level]);
676 /* Finally, the constant, and the final printing. */
677 if (nb_elts) {
678 if (!cloog_int_is_zero(numerator))
679 r->elts[nb_elts++] = &new_clast_term(numerator, NULL)->expr;
681 if (!cloog_int_is_one(line[level]) && !cloog_int_is_neg_one(line[level]))
682 { if (!cloog_constraint_is_equality(constraint))
683 { if (cloog_int_is_pos(line[level]))
684 e = &new_clast_binary(clast_bin_cdiv, &r->expr, denominator)->expr;
685 else
686 e = &new_clast_binary(clast_bin_fdiv, &r->expr, denominator)->expr;
687 } else
688 e = &new_clast_binary(clast_bin_div, &r->expr, denominator)->expr;
690 else
691 e = &r->expr;
692 } else {
693 free_clast_reduction(r);
694 if (cloog_int_is_zero(numerator))
695 e = &new_clast_term(numerator, NULL)->expr;
696 else
697 { if (!cloog_int_is_one(denominator))
698 { if (!cloog_constraint_is_equality(constraint)) { /* useful? */
699 if (cloog_int_is_divisible_by(numerator, denominator)) {
700 cloog_int_divexact(temp, numerator, denominator);
701 e = &new_clast_term(temp, NULL)->expr;
703 else {
704 cloog_int_init(division);
705 cloog_int_tdiv_q(division, numerator, denominator);
706 if (cloog_int_is_neg(numerator)) {
707 if (cloog_int_is_pos(line[level])) {
708 /* nb<0 need max */
709 e = &new_clast_term(division, NULL)->expr;
710 } else {
711 /* nb<0 need min */
712 cloog_int_sub_ui(temp, division, 1);
713 e = &new_clast_term(temp, NULL)->expr;
716 else
717 { if (cloog_int_is_pos(line[level]))
718 { /* nb>0 need max */
719 cloog_int_add_ui(temp, division, 1);
720 e = &new_clast_term(temp, NULL)->expr;
722 else
723 /* nb>0 need min */
724 e = &new_clast_term(division, NULL)->expr;
726 cloog_int_clear(division);
729 else
730 e = &new_clast_binary(clast_bin_div,
731 &new_clast_term(numerator, NULL)->expr,
732 denominator)->expr;
734 else
735 e = &new_clast_term(numerator, NULL)->expr;
740 cloog_vec_free(line_vector);
742 cloog_int_clear(temp);
743 cloog_int_clear(numerator);
744 cloog_int_clear(denominator);
746 return e;
750 /* Temporary structure for communication between clast_minmax and
751 * its cloog_constraint_set_foreach_constraint callback functions.
753 struct clast_minmax_data {
754 int level;
755 int max;
756 int guard;
757 int lower_bound;
758 CloogInfos *infos;
759 int n;
760 struct clast_reduction *r;
764 /* Should constraint "c" be considered by clast_minmax?
766 static int valid_bound(CloogConstraint *c, struct clast_minmax_data *d)
768 if (d->max && !cloog_constraint_is_lower_bound(c, d->level - 1))
769 return 0;
770 if (!d->max && !cloog_constraint_is_upper_bound(c, d->level - 1))
771 return 0;
772 if (cloog_constraint_is_equality(c))
773 return 0;
774 if (d->guard && cloog_constraint_involves(c, d->guard - 1))
775 return 0;
777 return 1;
781 /* Increment n for each bound that should be considered by clast_minmax.
783 static int count_bounds(CloogConstraint *c, void *user)
785 struct clast_minmax_data *d = (struct clast_minmax_data *) user;
787 if (!valid_bound(c, d))
788 return 0;
790 d->n++;
792 return 0;
796 /* Update the given lower bound based on stride information.
797 * In some backends, the lower bounds are updated from within
798 * cloog_loop_stride, but other backends leave the updating to
799 * this function. In the later case, the original lower bound
800 * is known to be a constant.
801 * If the bound turns out not to be a constant, we know we
802 * are in the former case and nothing needs to be done.
803 * If the bound has already been updated and it just happens
804 * to be a constant, then this function performs an identity
805 * operation on the constant.
807 static void update_lower_bound(struct clast_expr *expr, int level,
808 CloogStride *stride)
810 struct clast_term *t;
811 if (stride->constraint)
812 return;
813 if (expr->type != clast_expr_term)
814 return;
815 t = (struct clast_term *)expr;
816 if (t->var)
817 return;
818 cloog_int_sub(t->val, t->val, stride->offset);
819 cloog_int_cdiv_q(t->val, t->val, stride->stride);
820 cloog_int_mul(t->val, t->val, stride->stride);
821 cloog_int_add(t->val, t->val, stride->offset);
825 /* Add all relevant bounds to r->elts and update lower bounds
826 * based on stride information.
828 static int collect_bounds(CloogConstraint *c, void *user)
830 struct clast_minmax_data *d = (struct clast_minmax_data *) user;
832 if (!valid_bound(c, d))
833 return 0;
835 d->r->elts[d->n] = clast_bound_from_constraint(c, d->level,
836 d->infos->names);
837 if (d->lower_bound && d->infos->stride[d->level - 1]) {
838 update_lower_bound(d->r->elts[d->n], d->level,
839 d->infos->stride[d->level - 1]);
842 d->n++;
844 return 0;
849 * clast_minmax function:
850 * This function returns a clast_expr containing the printing of a minimum or a
851 * maximum of the 'right parts' of all constraints according to an element.
852 * For instance consider the constraints:
853 * -3*i +2*j -M >= 0
854 * 2*i +j >= 0
855 * -i -j +2*M >= 0
856 * if we are looking for the minimum for the element j, the function should
857 * return 'max(ceild(3*i+M,2),-2*i)'.
858 * - constraints is the constraints,
859 * - level is the column number in domain of the element we want to use,
860 * - max is a boolean set to 1 if we are looking for a maximum, 0 for a minimum,
861 * - guard is set to 0 if there is no guard, and set to the level of the element
862 * with a guard otherwise (then the function gives the max or the min only
863 * for the constraint where the guarded coefficient is 0),
864 * - lower is set to 1 if the maximum is to be used a lower bound on a loop
865 * - the infos structure gives the user some options about code printing,
866 * the number of parameters in domain (nb_par), and the arrays of iterator
867 * names and parameters (iters and params).
869 * - November 2nd 2001: first version.
871 static struct clast_expr *clast_minmax(CloogConstraintSet *constraints,
872 int level, int max, int guard,
873 int lower_bound,
874 CloogInfos *infos)
876 struct clast_minmax_data data = { level, max, guard, lower_bound, infos };
878 data.n = 0;
880 cloog_constraint_set_foreach_constraint(constraints, count_bounds, &data);
882 if (!data.n)
883 return NULL;
884 data.r = new_clast_reduction(max ? clast_red_max : clast_red_min, data.n);
886 data.n = 0;
887 cloog_constraint_set_foreach_constraint(constraints, collect_bounds, &data);
889 clast_reduction_sort(data.r);
890 return &data.r->expr;
895 * Insert modulo guards defined by existentially quantified dimensions,
896 * not involving the given level.
898 * This function is called from within insert_guard.
899 * Any constraint used in constructing a modulo guard is removed
900 * from the constraint set to avoid insert_guard
901 * adding a duplicate (pair of) constraint(s).
903 static void insert_extra_modulo_guards(CloogConstraintSet *constraints,
904 int level, struct clast_stmt ***next, CloogInfos *infos)
906 int i;
907 int nb_iter;
908 int total_dim;
909 CloogConstraint *upper, *lower;
911 total_dim = cloog_constraint_set_total_dimension(constraints);
912 nb_iter = cloog_constraint_set_n_iterators(constraints,
913 infos->names->nb_parameters);
915 for (i = total_dim - infos->names->nb_parameters; i >= nb_iter + 1; i--) {
916 if (cloog_constraint_is_valid(upper =
917 cloog_constraint_set_defining_equality(constraints, i))) {
918 if (!level || (nb_iter < level) ||
919 !cloog_constraint_involves(upper, level-1)) {
920 insert_modulo_guard(upper,
921 cloog_constraint_invalid(), i, next, infos);
922 cloog_constraint_clear(upper);
924 cloog_constraint_release(upper);
925 } else if (cloog_constraint_is_valid(upper =
926 cloog_constraint_set_defining_inequalities(constraints,
927 i, &lower, infos->names->nb_parameters))) {
928 if (!level || (nb_iter < level) ||
929 !cloog_constraint_involves(upper, level-1)) {
930 insert_modulo_guard(upper, lower, i, next, infos);
931 cloog_constraint_clear(upper);
932 cloog_constraint_clear(lower);
934 cloog_constraint_release(upper);
935 cloog_constraint_release(lower);
941 static int clear_lower_bound_at_level(CloogConstraint *c, void *user)
943 int level = *(int *)user;
945 if (cloog_constraint_is_lower_bound(c, level - 1))
946 cloog_constraint_clear(c);
948 return 0;
952 static int clear_upper_bound_at_level(CloogConstraint *c, void *user)
954 int level = *(int *)user;
956 if (cloog_constraint_is_upper_bound(c, level - 1))
957 cloog_constraint_clear(c);
959 return 0;
963 /* Temporary structure for communication between insert_guard and
964 * its cloog_constraint_set_foreach_constraint callback function.
966 struct clast_guard_data {
967 int level;
968 CloogInfos *infos;
969 int n;
970 int i;
971 int nb_iter;
972 CloogConstraintSet *copy;
973 struct clast_guard *g;
977 static int guard_count_bounds(CloogConstraint *c, void *user)
979 struct clast_guard_data *d = (struct clast_guard_data *) user;
981 d->n++;
983 return 0;
987 /* Insert a guard, if necesessary, for constraint j.
989 static int insert_guard_constraint(CloogConstraint *j, void *user)
991 struct clast_guard_data *d = (struct clast_guard_data *) user;
992 int minmax = -1;
993 struct clast_expr *v;
994 struct clast_term *t;
996 if (!cloog_constraint_involves(j, d->i - 1))
997 return 0;
999 if (d->level && d->nb_iter >= d->level &&
1000 cloog_constraint_involves(j, d->level - 1))
1001 return 0;
1003 v = cloog_constraint_variable_expr(j, d->i, d->infos->names);
1004 d->g->eq[d->n].LHS = &(t = new_clast_term(d->infos->state->one, v))->expr;
1005 if (!d->level || cloog_constraint_is_equality(j)) {
1006 /* put the "denominator" in the LHS */
1007 cloog_constraint_coefficient_get(j, d->i - 1, &t->val);
1008 cloog_constraint_coefficient_set(j, d->i - 1, d->infos->state->one);
1009 if (cloog_int_is_neg(t->val)) {
1010 cloog_int_neg(t->val, t->val);
1011 cloog_constraint_coefficient_set(j, d->i - 1, d->infos->state->negone);
1013 if (d->level || cloog_constraint_is_equality(j))
1014 d->g->eq[d->n].sign = 0;
1015 else if (cloog_constraint_is_lower_bound(j, d->i - 1))
1016 d->g->eq[d->n].sign = 1;
1017 else
1018 d->g->eq[d->n].sign = -1;
1019 d->g->eq[d->n].RHS = clast_bound_from_constraint(j, d->i, d->infos->names);
1020 } else {
1021 int guarded;
1023 if (cloog_constraint_is_lower_bound(j, d->i - 1)) {
1024 minmax = 1;
1025 d->g->eq[d->n].sign = 1;
1026 } else {
1027 minmax = 0;
1028 d->g->eq[d->n].sign = -1;
1031 guarded = (d->nb_iter >= d->level) ? d->level : 0 ;
1032 d->g->eq[d->n].RHS = clast_minmax(d->copy, d->i, minmax, guarded, 0,
1033 d->infos);
1035 d->n++;
1037 /* 'elimination' of the current constraint, this avoid to use one
1038 * constraint more than once. The current line is always eliminated,
1039 * and the next lines if they are in a min or a max.
1041 cloog_constraint_clear(j);
1043 if (minmax == -1)
1044 return 0;
1045 if (minmax == 1)
1046 cloog_constraint_set_foreach_constraint(d->copy,
1047 clear_lower_bound_at_level, &d->i);
1048 else if (minmax == 0)
1049 cloog_constraint_set_foreach_constraint(d->copy,
1050 clear_upper_bound_at_level, &d->i);
1052 return 0;
1057 * insert_guard function:
1058 * This function inserts a guard in the clast.
1059 * A guard on an element (level) is :
1060 * -> the conjunction of all the existing constraints where the coefficient of
1061 * this element is 0 if the element is an iterator,
1062 * -> the conjunction of all the existing constraints if the element isn't an
1063 * iterator.
1064 * For instance, considering these constraints and the element j:
1065 * -3*i +2*j -M >= 0
1066 * 2*i +M >= 0
1067 * this function should return 'if (2*i+M>=0) {'.
1068 * - matrix is the polyhedron containing all the constraints,
1069 * - level is the column number of the element in matrix we want to use,
1070 * - the infos structure gives the user some options about code printing,
1071 * the number of parameters in matrix (nb_par), and the arrays of iterator
1072 * names and parameters (iters and params).
1074 * - November 3rd 2001: first version.
1075 * - November 14th 2001: a lot of 'purifications'.
1076 * - July 31th 2002: (debug) some guard parts are no more redundants.
1077 * - August 12th 2002: polyhedra union ('or' conditions) are now supported.
1078 * - October 27th 2005: polyhedra union ('or' conditions) are no more supported
1079 * (the need came from loop_simplify that may result in
1080 * domain unions, now it should be fixed directly in
1081 * cloog_loop_simplify).
1083 static void insert_guard(CloogConstraintSet *constraints, int level,
1084 struct clast_stmt ***next, CloogInfos *infos)
1086 int total_dim;
1087 struct clast_guard_data data = { level, infos, 0 };
1089 if (!constraints)
1090 return;
1092 data.copy = cloog_constraint_set_copy(constraints);
1094 insert_extra_modulo_guards(data.copy, level, next, infos);
1096 cloog_constraint_set_foreach_constraint(constraints,
1097 guard_count_bounds, &data);
1099 data.g = new_clast_guard(data.n);
1100 data.n = 0;
1102 /* Well, it looks complicated because I wanted to have a particular, more
1103 * readable, ordering, obviously this function may be far much simpler !
1105 data.nb_iter = cloog_constraint_set_n_iterators(constraints,
1106 infos->names->nb_parameters);
1108 /* We search for guard parts. */
1109 total_dim = cloog_constraint_set_total_dimension(constraints);
1110 for (data.i = 1; data.i <= total_dim; data.i++)
1111 cloog_constraint_set_foreach_constraint(data.copy,
1112 insert_guard_constraint, &data);
1114 cloog_constraint_set_free(data.copy);
1116 data.g->n = data.n;
1117 if (data.n) {
1118 clast_guard_sort(data.g);
1119 **next = &data.g->stmt;
1120 *next = &data.g->then;
1121 } else
1122 free_clast_stmt(&data.g->stmt);
1126 * Check if the constant "cst" satisfies the modulo guard that
1127 * would be introduced by insert_computed_modulo_guard.
1128 * The constant is assumed to have been reduced prior to calling
1129 * this function.
1131 static int constant_modulo_guard_is_satisfied(CloogConstraint *lower,
1132 cloog_int_t bound, cloog_int_t cst)
1134 if (cloog_constraint_is_valid(lower))
1135 return cloog_int_le(cst, bound);
1136 else
1137 return cloog_int_is_zero(cst);
1141 * Insert a modulo guard "r % mod == 0" or "r % mod <= bound",
1142 * depending on whether lower represents a valid constraint.
1144 static void insert_computed_modulo_guard(struct clast_reduction *r,
1145 CloogConstraint *lower, cloog_int_t mod, cloog_int_t bound,
1146 struct clast_stmt ***next)
1148 struct clast_expr *e;
1149 struct clast_guard *g;
1151 e = &new_clast_binary(clast_bin_mod, &r->expr, mod)->expr;
1152 g = new_clast_guard(1);
1153 if (!cloog_constraint_is_valid(lower)) {
1154 g->eq[0].LHS = e;
1155 cloog_int_set_si(bound, 0);
1156 g->eq[0].RHS = &new_clast_term(bound, NULL)->expr;
1157 g->eq[0].sign = 0;
1158 } else {
1159 g->eq[0].LHS = e;
1160 g->eq[0].RHS = &new_clast_term(bound, NULL)->expr;
1161 g->eq[0].sign = -1;
1164 **next = &g->stmt;
1165 *next = &g->then;
1169 /* Try and eliminate coefficients from a modulo constraint based on
1170 * stride information of an earlier level.
1171 * The modulo of the constraint being constructed is "m".
1172 * The stride information at level "level" is given by "stride"
1173 * and indicated that the iterator i at level "level" is equal to
1174 * some expression modulo stride->stride.
1175 * If stride->stride is a multiple of "m' then i is also equal to
1176 * the expression modulo m and so we can eliminate the coefficient of i.
1178 * If stride->constraint is NULL, then i has a constant value modulo m, stored
1179 * stride->offset. We simply multiply this constant with the coefficient
1180 * of i and add the result to the constant term, reducing it modulo m.
1182 * If stride->constraint is not NULL, then it is a constraint of the form
1184 * e + k i = s a
1186 * with s equal to stride->stride, e an expression in terms of the
1187 * parameters and earlier iterators and a some arbitrary expression
1188 * in terms of existentially quantified variables.
1189 * stride->factor is a value f such that f * k = -1 mod s.
1190 * Adding stride->constraint f * c times to the current modulo constraint,
1191 * with c the coefficient of i eliminates i in favor of parameters and
1192 * earlier variables.
1194 static void eliminate_using_stride_constraint(cloog_int_t *line, int len,
1195 int nb_iter, CloogStride *stride, int level, cloog_int_t m)
1197 if (!stride)
1198 return;
1199 if (!cloog_int_is_divisible_by(stride->stride, m))
1200 return;
1202 if (stride->constraint) {
1203 int i, s_len;
1204 cloog_int_t t, v;
1206 cloog_int_init(t);
1207 cloog_int_init(v);
1208 cloog_int_mul(t, line[level], stride->factor);
1209 for (i = 1; i < level; ++i) {
1210 cloog_constraint_coefficient_get(stride->constraint,
1211 i - 1, &v);
1212 cloog_int_addmul(line[i], t, v);
1213 cloog_int_fdiv_r(line[i], line[i], m);
1215 s_len = cloog_constraint_total_dimension(stride->constraint)+2;
1216 for (i = nb_iter + 1; i <= len - 2; ++i) {
1217 cloog_constraint_coefficient_get(stride->constraint,
1218 i - (len - s_len) - 1, &v);
1219 cloog_int_addmul(line[i], t, v);
1220 cloog_int_fdiv_r(line[i], line[i], m);
1222 cloog_constraint_constant_get(stride->constraint, &v);
1223 cloog_int_addmul(line[len - 1], t, v);
1224 cloog_int_fdiv_r(line[len - 1], line[len - 1], m);
1225 cloog_int_clear(v);
1226 cloog_int_clear(t);
1227 } else {
1228 cloog_int_addmul(line[len - 1], line[level], stride->offset);
1229 cloog_int_fdiv_r(line[len - 1], line[len - 1], m);
1232 cloog_int_set_si(line[level], 0);
1236 /* Temporary structure for communication between insert_modulo_guard and
1237 * its cloog_constraint_set_foreach_constraint callback function.
1239 struct clast_modulo_guard_data {
1240 CloogConstraint *lower;
1241 int level;
1242 struct clast_stmt ***next;
1243 CloogInfos *infos;
1244 int empty;
1245 cloog_int_t val, bound;
1249 /* Insert a modulo guard for constraint c.
1250 * The constraint may be either an equality or an inequality.
1251 * Since this function returns -1, it is only called on a single constraint.
1252 * In case of an inequality, the constraint is usually an upper bound
1253 * on d->level. However, if this variable is an existentially
1254 * quantified variable, the upper bound constraint may get removed
1255 * as trivially holding and then this function is called with
1256 * a lower bound instead. In this case, we need to adjust the constraint
1257 * based on the sum of the constant terms of the lower and upper bound
1258 * stored in d->bound.
1260 static int insert_modulo_guard_constraint(CloogConstraint *c, void *user)
1262 struct clast_modulo_guard_data *d = (struct clast_modulo_guard_data *) user;
1263 int level = d->level;
1264 CloogInfos *infos = d->infos;
1265 int i, nb_elts = 0, len, len2, nb_iter, nb_par;
1266 int constant;
1267 struct cloog_vec *line_vector;
1268 cloog_int_t *line;
1270 len = cloog_constraint_total_dimension(c) + 2;
1271 len2 = cloog_equal_total_dimension(infos->equal) + 2;
1272 nb_par = infos->names->nb_parameters;
1273 nb_iter = len - 2 - nb_par;
1275 line_vector = cloog_vec_alloc(len);
1276 line = line_vector->p;
1277 cloog_constraint_copy_coefficients(c, line + 1);
1279 if (cloog_int_is_pos(line[level])) {
1280 cloog_seq_neg(line + 1, line + 1, len - 1);
1281 if (!cloog_constraint_is_equality(c))
1282 cloog_int_add(line[len - 1], line[len - 1], d->bound);
1284 cloog_int_neg(line[level], line[level]);
1285 assert(cloog_int_is_pos(line[level]));
1287 nb_elts = 0;
1288 for (i = 1; i <= len-1; ++i) {
1289 if (i == level)
1290 continue;
1291 cloog_int_fdiv_r(line[i], line[i], line[level]);
1292 if (cloog_int_is_zero(line[i]))
1293 continue;
1294 if (i == len-1)
1295 continue;
1297 nb_elts++;
1300 if (nb_elts || !cloog_int_is_zero(line[len-1])) {
1301 struct clast_reduction *r;
1302 const char *name;
1304 r = new_clast_reduction(clast_red_sum, nb_elts + 1);
1305 nb_elts = 0;
1307 /* First, the modulo guard : the iterators... */
1308 i = level - 1;
1309 if (i > infos->stride_level)
1310 i = infos->stride_level;
1311 for (; i >= 1; --i)
1312 eliminate_using_stride_constraint(line, len, nb_iter,
1313 infos->stride[i - 1], i, line[level]);
1314 for (i=1;i<=nb_iter;i++) {
1315 if (i == level || cloog_int_is_zero(line[i]))
1316 continue;
1318 name = cloog_names_name_at_level(infos->names, i);
1320 r->elts[nb_elts++] = &new_clast_term(line[i],
1321 &new_clast_name(name)->expr)->expr;
1324 /* ...the parameters... */
1325 for (i=nb_iter+1;i<=len-2;i++) {
1326 if (cloog_int_is_zero(line[i]))
1327 continue;
1329 name = infos->names->parameters[i-nb_iter-1] ;
1330 r->elts[nb_elts++] = &new_clast_term(line[i],
1331 &new_clast_name(name)->expr)->expr;
1334 constant = nb_elts == 0;
1335 /* ...the constant. */
1336 if (!cloog_int_is_zero(line[len-1]))
1337 r->elts[nb_elts++] = &new_clast_term(line[len-1], NULL)->expr;
1339 /* our initial computation may have been an overestimate */
1340 r->n = nb_elts;
1342 if (constant) {
1343 d->empty = !constant_modulo_guard_is_satisfied(d->lower, d->bound,
1344 line[len - 1]);
1345 free_clast_reduction(r);
1346 } else
1347 insert_computed_modulo_guard(r, d->lower, line[level], d->bound,
1348 d->next);
1351 cloog_vec_free(line_vector);
1353 return -1;
1358 * insert_modulo_guard:
1359 * This function inserts a modulo guard corresponding to an equality
1360 * or a pair of inequalities.
1361 * Returns 0 if the modulo guard is discovered to be unsatisfiable.
1363 * See insert_equation.
1364 * - matrix is the polyhedron containing all the constraints,
1365 * - upper and lower are the line numbers of the constraint in matrix
1366 * we want to print; in particular, if we want to print an equality,
1367 * then lower == -1 and upper is the row of the equality; if we want
1368 * to print an inequality, then upper is the row of the upper bound
1369 * and lower in the row of the lower bound
1370 * - level is the column number of the element in matrix we want to use,
1371 * - the infos structure gives the user some options about code printing,
1372 * the number of parameters in matrix (nb_par), and the arrays of iterator
1373 * names and parameters (iters and params).
1375 static int insert_modulo_guard(CloogConstraint *upper,
1376 CloogConstraint *lower, int level,
1377 struct clast_stmt ***next, CloogInfos *infos)
1379 int nb_par;
1380 CloogConstraintSet *set;
1381 struct clast_modulo_guard_data data = { lower, level, next, infos, 0 };
1383 cloog_int_init(data.val);
1384 cloog_constraint_coefficient_get(upper, level-1, &data.val);
1385 if (cloog_int_is_one(data.val) || cloog_int_is_neg_one(data.val)) {
1386 cloog_int_clear(data.val);
1387 return 1;
1390 nb_par = infos->names->nb_parameters;
1392 cloog_int_init(data.bound);
1393 /* Check if would be emitting the redundant constraint mod(e,m) <= m-1 */
1394 if (cloog_constraint_is_valid(lower)) {
1395 cloog_constraint_constant_get(upper, &data.val);
1396 cloog_constraint_constant_get(lower, &data.bound);
1397 cloog_int_add(data.bound, data.val, data.bound);
1398 cloog_constraint_coefficient_get(lower, level-1, &data.val);
1399 cloog_int_sub_ui(data.val, data.val, 1);
1400 if (cloog_int_eq(data.val, data.bound)) {
1401 cloog_int_clear(data.val);
1402 cloog_int_clear(data.bound);
1403 return 1;
1407 if (cloog_constraint_needs_reduction(upper, level)) {
1408 set = cloog_constraint_set_for_reduction(upper, lower);
1409 set = cloog_constraint_set_reduce(set, level, infos->equal,
1410 nb_par, &data.bound);
1411 cloog_constraint_set_foreach_constraint(set,
1412 insert_modulo_guard_constraint, &data);
1413 cloog_constraint_set_free(set);
1414 } else
1415 insert_modulo_guard_constraint(upper, &data);
1417 cloog_int_clear(data.val);
1418 cloog_int_clear(data.bound);
1420 return !data.empty;
1425 * We found an equality or a pair of inequalities identifying
1426 * a loop with a single iteration, but the user wants us to generate
1427 * a loop anyway, so we do it here.
1429 static int insert_equation_as_loop(CloogDomain *domain, CloogConstraint *upper,
1430 CloogConstraint *lower, int level, struct clast_stmt ***next,
1431 CloogInfos *infos)
1433 const char *iterator = cloog_names_name_at_level(infos->names, level);
1434 struct clast_expr *e1, *e2;
1435 struct clast_for *f;
1437 e2 = clast_bound_from_constraint(upper, level, infos->names);
1438 if (!cloog_constraint_is_valid(lower))
1439 e1 = clast_expr_copy(e2);
1440 else
1441 e1 = clast_bound_from_constraint(lower, level, infos->names);
1443 f = new_clast_for(domain, iterator, e1, e2, infos->stride[level-1]);
1444 **next = &f->stmt;
1445 *next = &f->body;
1447 cloog_constraint_release(lower);
1448 cloog_constraint_release(upper);
1449 return 1;
1454 * insert_equation function:
1455 * This function inserts an equality
1456 * constraint according to an element in the clast.
1457 * Returns 1 if the calling function should recurse into inner loops.
1459 * An equality can be preceded by a 'modulo guard'.
1460 * For instance, consider the constraint i -2*j = 0 and the
1461 * element j: pprint_equality should return 'if(i%2==0) { j = i/2 ;'.
1462 * - matrix is the polyhedron containing all the constraints,
1463 * - num is the line number of the constraint in matrix we want to print,
1464 * - level is the column number of the element in matrix we want to use,
1465 * - the infos structure gives the user some options about code printing,
1466 * the number of parameters in matrix (nb_par), and the arrays of iterator
1467 * names and parameters (iters and params).
1469 * - November 13th 2001: first version.
1470 * - June 26th 2003: simplification of the modulo guards (remove parts such as
1471 * modulo is 0, compare vivien or vivien2 with a previous
1472 * version for an idea).
1473 * - June 29th 2003: non-unit strides support.
1474 * - July 14th 2003: (debug) no more print the constant in the modulo guard when
1475 * it was previously included in a stride calculation.
1477 static int insert_equation(CloogDomain *domain, CloogConstraint *upper,
1478 CloogConstraint *lower, int level, struct clast_stmt
1479 ***next, CloogInfos *infos)
1481 struct clast_expr *e;
1482 struct clast_assignment *ass;
1484 if (!infos->options->otl)
1485 return insert_equation_as_loop(domain, upper, lower, level, next, infos);
1487 if (!insert_modulo_guard(upper, lower, level, next, infos)) {
1488 cloog_constraint_release(lower);
1489 cloog_constraint_release(upper);
1491 return 0;
1494 if (cloog_constraint_is_valid(lower) ||
1495 !clast_equal_add(infos->equal, NULL, level, upper, infos))
1496 { /* Finally, the equality. */
1498 /* If we have to make a block by dimension, we start the block. Function
1499 * pprint knows if there is an equality, if this is the case, it checks
1500 * for the same following condition to close the brace.
1502 if (infos->options->block) {
1503 struct clast_block *b = new_clast_block();
1504 **next = &b->stmt;
1505 *next = &b->body;
1508 e = clast_bound_from_constraint(upper, level, infos->names);
1509 ass = new_clast_assignment(cloog_names_name_at_level(infos->names, level), e);
1511 **next = &ass->stmt;
1512 *next = &(**next)->next;
1515 cloog_constraint_release(lower);
1516 cloog_constraint_release(upper);
1518 return 1;
1523 * Insert a loop that is executed exactly once as an assignment.
1524 * In particular, the loop
1526 * for (i = e; i <= e; ++i) {
1527 * S;
1530 * is generated as
1532 * i = e;
1533 * S;
1536 static void insert_otl_for(CloogConstraintSet *constraints, int level,
1537 struct clast_expr *e, struct clast_stmt ***next, CloogInfos *infos)
1539 const char *iterator;
1541 iterator = cloog_names_name_at_level(infos->names, level);
1543 if (!clast_equal_add(infos->equal, constraints, level,
1544 cloog_constraint_invalid(), infos)) {
1545 struct clast_assignment *ass;
1546 if (infos->options->block) {
1547 struct clast_block *b = new_clast_block();
1548 **next = &b->stmt;
1549 *next = &b->body;
1551 ass = new_clast_assignment(iterator, e);
1552 **next = &ass->stmt;
1553 *next = &(**next)->next;
1554 } else {
1555 free_clast_expr(e);
1561 * Insert a loop that is executed at most once as an assignment followed
1562 * by a guard. In particular, the loop
1564 * for (i = e1; i <= e2; ++i) {
1565 * S;
1568 * is generated as
1570 * i = e1;
1571 * if (i <= e2) {
1572 * S;
1576 static void insert_guarded_otl_for(CloogConstraintSet *constraints, int level,
1577 struct clast_expr *e1, struct clast_expr *e2,
1578 struct clast_stmt ***next, CloogInfos *infos)
1580 const char *iterator;
1581 struct clast_assignment *ass;
1582 struct clast_guard *guard;
1584 iterator = cloog_names_name_at_level(infos->names, level);
1586 if (infos->options->block) {
1587 struct clast_block *b = new_clast_block();
1588 **next = &b->stmt;
1589 *next = &b->body;
1591 ass = new_clast_assignment(iterator, e1);
1592 **next = &ass->stmt;
1593 *next = &(**next)->next;
1595 guard = new_clast_guard(1);
1596 guard->eq[0].sign = -1;
1597 guard->eq[0].LHS = &new_clast_term(infos->state->one,
1598 &new_clast_name(iterator)->expr)->expr;
1599 guard->eq[0].RHS = e2;
1601 **next = &guard->stmt;
1602 *next = &guard->then;
1607 * insert_for function:
1608 * This function inserts a for loop in the clast.
1609 * Returns 1 if the calling function should recurse into inner loops.
1611 * A loop header according to an element is the conjunction of a minimum and a
1612 * maximum on a given element (they give the loop bounds).
1613 * For instance, considering these constraints and the element j:
1614 * i + j -9*M >= 0
1615 * -j +5*M >= 0
1616 * j -4*M >= 0
1617 * this function should return 'for (j=max(-i+9*M,4*M),j<=5*M;j++) {'.
1618 * - constraints contains all constraints,
1619 * - level is the column number of the element in matrix we want to use,
1620 * - otl is set if the loop is executed at most once,
1621 * - the infos structure gives the user some options about code printing,
1622 * the number of parameters in matrix (nb_par), and the arrays of iterator
1623 * names and parameters (iters and params).
1625 static int insert_for(CloogDomain *domain, CloogConstraintSet *constraints,
1626 int level, int otl, struct clast_stmt ***next,
1627 CloogInfos *infos)
1629 const char *iterator;
1630 struct clast_expr *e1;
1631 struct clast_expr *e2;
1633 e1 = clast_minmax(constraints, level, 1, 0, 1, infos);
1634 e2 = clast_minmax(constraints, level, 0, 0, 0, infos);
1636 if (clast_expr_is_bigger_constant(e1, e2)) {
1637 free_clast_expr(e1);
1638 free_clast_expr(e2);
1639 return 0;
1642 /* If min and max are not equal there is a 'for' else, there is a '='.
1643 * In the special case e1 = e2 = NULL, this is an infinite loop
1644 * so this is not a '='.
1646 if (e1 && e2 && infos->options->otl && clast_expr_equal(e1, e2)) {
1647 free_clast_expr(e2);
1648 insert_otl_for(constraints, level, e1, next, infos);
1649 } else if (otl) {
1650 insert_guarded_otl_for(constraints, level, e1, e2, next, infos);
1651 } else {
1652 struct clast_for *f;
1653 iterator = cloog_names_name_at_level(infos->names, level);
1655 f = new_clast_for(domain, iterator, e1, e2, infos->stride[level-1]);
1656 **next = &f->stmt;
1657 *next = &f->body;
1660 return 1;
1665 * insert_block function:
1666 * This function inserts a statement block.
1667 * - block is the statement block,
1668 * - level is the number of loops enclosing the statement,
1669 * - the infos structure gives the user some options about code printing,
1670 * the number of parameters in domain (nb_par), and the arrays of iterator
1671 * names and parameters (iters and params).
1673 * - September 21th 2003: first version (pick from pprint function).
1675 static void insert_block(CloogDomain *domain, CloogBlock *block, int level,
1676 struct clast_stmt ***next, CloogInfos *infos)
1678 CloogStatement * statement ;
1679 struct clast_stmt *subs;
1681 if (!block)
1682 return;
1684 for (statement = block->statement; statement; statement = statement->next) {
1685 CloogStatement *s_next = statement->next;
1687 subs = clast_equal(level,infos);
1689 statement->next = NULL;
1690 **next = &new_clast_user_stmt(domain, statement, subs)->stmt;
1691 statement->next = s_next;
1692 *next = &(**next)->next;
1698 * insert_loop function:
1699 * This function converts the content of a CloogLoop structure (loop) into a
1700 * clast_stmt (inserted at **next).
1701 * The iterator (level) of
1702 * the current loop is given by 'level': this is the column number of the
1703 * domain corresponding to the current loop iterator. The data of a loop are
1704 * written in this order:
1705 * 1. The guard of the loop, i.e. each constraint in the domain that does not
1706 * depend on the iterator (when the entry in the column 'level' is 0).
1707 * 2. The iteration domain of the iterator, given by the constraints in the
1708 * domain depending on the iterator, i.e.:
1709 * * an equality if the iterator has only one value (possibly preceded by
1710 * a guard verifying if this value is integral), *OR*
1711 * * a loop from the minimum possible value of the iterator to the maximum
1712 * possible value.
1713 * 3. The included statement block.
1714 * 4. The inner loops (recursive call).
1715 * 5. The following loops (recursive call).
1716 * - level is the recursion level or the iteration level that we are printing,
1717 * - the infos structure gives the user some options about code printing,
1718 * the number of parameters in domain (nb_par), and the arrays of iterator
1719 * names and parameters (iters and params).
1721 * - November 2nd 2001: first version.
1722 * - March 6th 2003: infinite domain support.
1723 * - April 19th 2003: (debug) NULL loop support.
1724 * - June 29th 2003: non-unit strides support.
1725 * - April 28th 2005: (debug) level is level+equality when print statement!
1726 * - June 16th 2005: (debug) the N. Vasilache normalization step has been
1727 * added to avoid iteration duplication (see DaeGon Kim
1728 * bug in cloog_program_generate). Try vasilache.cloog
1729 * with and without the call to cloog_polylib_matrix_normalize,
1730 * using -f 8 -l 9 options for an idea.
1731 * - September 15th 2005: (debug) don't close equality braces when unnecessary.
1732 * - October 16th 2005: (debug) scalar value is saved for next loops.
1734 static void insert_loop(CloogLoop * loop, int level,
1735 struct clast_stmt ***next, CloogInfos *infos)
1737 int equality = 0;
1738 CloogConstraintSet *constraints, *temp;
1739 struct clast_stmt **top = *next;
1740 CloogConstraint *i, *j;
1741 int empty_loop = 0;
1743 /* It can happen that loop be NULL when an input polyhedron is empty. */
1744 if (loop == NULL)
1745 return;
1747 /* The constraints do not always have a shape that allows us to generate code from it,
1748 * thus we normalize it, we also simplify it with the equalities.
1750 temp = cloog_domain_constraints(loop->domain);
1751 cloog_constraint_set_normalize(temp,level);
1752 constraints = cloog_constraint_set_simplify(temp,infos->equal,level,
1753 infos->names->nb_parameters);
1754 cloog_constraint_set_free(temp);
1755 if (level) {
1756 infos->stride[level - 1] = loop->stride;
1757 infos->stride_level++;
1760 /* First of all we have to print the guard. */
1761 insert_guard(constraints,level, next, infos);
1763 if (level && cloog_constraint_set_contains_level(constraints, level,
1764 infos->names->nb_parameters)) {
1765 /* We scan all the constraints to know in which case we are :
1766 * [[if] equation] or [for].
1768 if (cloog_constraint_is_valid(i =
1769 cloog_constraint_set_defining_equality(constraints, level))) {
1770 empty_loop = !insert_equation(loop->unsimplified, i,
1771 cloog_constraint_invalid(), level, next,
1772 infos);
1773 equality = 1 ;
1774 } else if (cloog_constraint_is_valid(i =
1775 cloog_constraint_set_defining_inequalities(constraints,
1776 level, &j, infos->names->nb_parameters))) {
1777 empty_loop = !insert_equation(loop->unsimplified, i, j, level, next,
1778 infos);
1779 } else
1780 empty_loop = !insert_for(loop->unsimplified, constraints, level,
1781 loop->otl, next, infos);
1784 if (!empty_loop) {
1785 /* Finally, if there is an included statement block, print it. */
1786 insert_block(loop->unsimplified, loop->block, level+equality, next, infos);
1788 /* Go to the next level. */
1789 if (loop->inner != NULL)
1790 insert_loop(loop->inner, level+1, next, infos);
1793 if (level) {
1794 cloog_equal_del(infos->equal,level);
1795 infos->stride_level--;
1797 cloog_constraint_set_free(constraints);
1799 /* Go to the next loop on the same level. */
1800 while (*top)
1801 top = &(*top)->next;
1802 if (loop->next != NULL)
1803 insert_loop(loop->next, level, &top,infos);
1807 struct clast_stmt *cloog_clast_create(CloogProgram *program,
1808 CloogOptions *options)
1810 CloogInfos *infos = ALLOC(CloogInfos);
1811 int nb_levels;
1812 struct clast_stmt *root = &new_clast_root(program->names)->stmt;
1813 struct clast_stmt **next = &root->next;
1815 infos->state = options->state;
1816 infos->names = program->names;
1817 infos->options = options;
1818 infos->scaldims = program->scaldims;
1819 infos->nb_scattdims = program->nb_scattdims;
1821 /* Allocation for the array of strides, there is a +1 since the statement can
1822 * be included inside an external loop without iteration domain.
1824 nb_levels = program->names->nb_scattering+program->names->nb_iterators+1;
1825 infos->stride = ALLOCN(CloogStride *, nb_levels);
1826 infos->stride_level = 0;
1828 infos->equal = cloog_equal_alloc(nb_levels,
1829 nb_levels, program->names->nb_parameters);
1831 insert_loop(program->loop, 0, &next, infos);
1833 cloog_equal_free(infos->equal);
1835 free(infos->stride);
1836 free(infos);
1838 return root;
1842 struct clast_stmt *cloog_clast_create_from_input(CloogInput *input,
1843 CloogOptions *options)
1845 CloogProgram *program;
1846 struct clast_stmt *root;
1848 program = cloog_program_alloc(input->context, input->ud, options);
1849 free(input);
1851 program = cloog_program_generate(program, options);
1853 root = cloog_clast_create(program, options);
1854 cloog_program_free(program);
1856 return root;