document cloog_isl_state_malloc
[cloog/uuh.git] / source / clast.c
blob0b67532e5fa2435ee27dd088e49afa72da0ccc1a
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, int no_earlier,
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 int no_earlier;
759 CloogInfos *infos;
760 int n;
761 struct clast_reduction *r;
765 /* Should constraint "c" be considered by clast_minmax?
767 * If d->no_earlier is set, then the constraint may not involve
768 * any earlier variables.
770 static int valid_bound(CloogConstraint *c, struct clast_minmax_data *d)
772 int i;
774 if (d->max && !cloog_constraint_is_lower_bound(c, d->level - 1))
775 return 0;
776 if (!d->max && !cloog_constraint_is_upper_bound(c, d->level - 1))
777 return 0;
778 if (cloog_constraint_is_equality(c))
779 return 0;
780 if (d->guard && cloog_constraint_involves(c, d->guard - 1))
781 return 0;
783 if (d->no_earlier)
784 for (i = 0; i < d->level - 1; ++i)
785 if (cloog_constraint_involves(c, i))
786 return 0;
788 return 1;
792 /* Increment n for each bound that should be considered by clast_minmax.
794 static int count_bounds(CloogConstraint *c, void *user)
796 struct clast_minmax_data *d = (struct clast_minmax_data *) user;
798 if (!valid_bound(c, d))
799 return 0;
801 d->n++;
803 return 0;
807 /* Update the given lower bound based on stride information,
808 * for those cases where the stride offset is represented by
809 * a constraint.
810 * Note that cloog_loop_stride may have already performed a
811 * similar update of the lower bounds, but the updated lower
812 * bounds may have been eliminated because they are redundant
813 * by definition. On the other hand, performing the update
814 * on an already updated constraint is an identity operation
815 * and is therefore harmless.
817 static CloogConstraint *update_lower_bound_c(CloogConstraint *c, int level,
818 CloogStride *stride)
820 if (!stride->constraint)
821 return c;
822 return cloog_constraint_stride_lower_bound(c, level, stride);
826 /* Update the given lower bound based on stride information.
827 * If the stride offset is represented by a constraint,
828 * then we have already performed the update in update_lower_bound_c.
829 * Otherwise, the original lower bound is known to be a constant.
830 * If the bound has already been updated and it just happens
831 * to be a constant, then this function performs an identity
832 * operation on the constant.
834 static void update_lower_bound(struct clast_expr *expr, int level,
835 CloogStride *stride)
837 struct clast_term *t;
838 if (stride->constraint)
839 return;
840 if (expr->type != clast_expr_term)
841 return;
842 t = (struct clast_term *)expr;
843 if (t->var)
844 return;
845 cloog_int_sub(t->val, t->val, stride->offset);
846 cloog_int_cdiv_q(t->val, t->val, stride->stride);
847 cloog_int_mul(t->val, t->val, stride->stride);
848 cloog_int_add(t->val, t->val, stride->offset);
852 /* Add all relevant bounds to r->elts and update lower bounds
853 * based on stride information.
855 static int collect_bounds(CloogConstraint *c, void *user)
857 struct clast_minmax_data *d = (struct clast_minmax_data *) user;
859 if (!valid_bound(c, d))
860 return 0;
862 c = cloog_constraint_copy(c);
864 if (d->lower_bound && d->infos->stride[d->level - 1])
865 c = update_lower_bound_c(c, d->level, d->infos->stride[d->level - 1]);
867 d->r->elts[d->n] = clast_bound_from_constraint(c, d->level,
868 d->infos->names);
869 if (d->lower_bound && d->infos->stride[d->level - 1]) {
870 update_lower_bound(d->r->elts[d->n], d->level,
871 d->infos->stride[d->level - 1]);
874 cloog_constraint_release(c);
876 d->n++;
878 return 0;
883 * clast_minmax function:
884 * This function returns a clast_expr containing the printing of a minimum or a
885 * maximum of the 'right parts' of all constraints according to an element.
886 * For instance consider the constraints:
887 * -3*i +2*j -M >= 0
888 * 2*i +j >= 0
889 * -i -j +2*M >= 0
890 * if we are looking for the minimum for the element j, the function should
891 * return 'max(ceild(3*i+M,2),-2*i)'.
892 * - constraints is the constraints,
893 * - level is the column number in domain of the element we want to use,
894 * - max is a boolean set to 1 if we are looking for a maximum, 0 for a minimum,
895 * - guard is set to 0 if there is no guard, and set to the level of the element
896 * with a guard otherwise (then the function gives the max or the min only
897 * for the constraint where the guarded coefficient is 0),
898 * - lower is set to 1 if the maximum is to be used a lower bound on a loop
899 * - no_earlier is set if no constraints should be used that involve
900 * earlier dimensions,
901 * - the infos structure gives the user some options about code printing,
902 * the number of parameters in domain (nb_par), and the arrays of iterator
903 * names and parameters (iters and params).
905 * - November 2nd 2001: first version.
907 static struct clast_expr *clast_minmax(CloogConstraintSet *constraints,
908 int level, int max, int guard,
909 int lower_bound, int no_earlier,
910 CloogInfos *infos)
912 struct clast_minmax_data data = { level, max, guard, lower_bound,
913 no_earlier, infos };
915 data.n = 0;
917 cloog_constraint_set_foreach_constraint(constraints, count_bounds, &data);
919 if (!data.n)
920 return NULL;
921 data.r = new_clast_reduction(max ? clast_red_max : clast_red_min, data.n);
923 data.n = 0;
924 cloog_constraint_set_foreach_constraint(constraints, collect_bounds, &data);
926 clast_reduction_sort(data.r);
927 return &data.r->expr;
932 * Insert modulo guards defined by existentially quantified dimensions,
933 * not involving the given level.
935 * This function is called from within insert_guard.
936 * Any constraint used in constructing a modulo guard is removed
937 * from the constraint set to avoid insert_guard
938 * adding a duplicate (pair of) constraint(s).
940 * Return the updated CloogConstraintSet.
942 static CloogConstraintSet *insert_extra_modulo_guards(
943 CloogConstraintSet *constraints, int level,
944 struct clast_stmt ***next, CloogInfos *infos)
946 int i;
947 int nb_iter;
948 int total_dim;
949 CloogConstraint *upper, *lower;
951 total_dim = cloog_constraint_set_total_dimension(constraints);
952 nb_iter = cloog_constraint_set_n_iterators(constraints,
953 infos->names->nb_parameters);
955 for (i = total_dim - infos->names->nb_parameters; i >= nb_iter + 1; i--) {
956 if (cloog_constraint_is_valid(upper =
957 cloog_constraint_set_defining_equality(constraints, i))) {
958 if (!level || (nb_iter < level) ||
959 !cloog_constraint_involves(upper, level-1)) {
960 insert_modulo_guard(upper,
961 cloog_constraint_invalid(), i, next, infos);
962 constraints = cloog_constraint_set_drop_constraint(constraints,
963 upper);
965 cloog_constraint_release(upper);
966 } else if (cloog_constraint_is_valid(upper =
967 cloog_constraint_set_defining_inequalities(constraints,
968 i, &lower, infos->names->nb_parameters))) {
969 if (!level || (nb_iter < level) ||
970 !cloog_constraint_involves(upper, level-1)) {
971 insert_modulo_guard(upper, lower, i, next, infos);
972 constraints = cloog_constraint_set_drop_constraint(constraints,
973 upper);
974 constraints = cloog_constraint_set_drop_constraint(constraints,
975 lower);
977 cloog_constraint_release(upper);
978 cloog_constraint_release(lower);
982 return constraints;
986 /* Temporary structure for communication between insert_guard and
987 * its cloog_constraint_set_foreach_constraint callback function.
989 struct clast_guard_data {
990 int level;
991 CloogInfos *infos;
992 int n;
993 int i;
994 int nb_iter;
995 CloogConstraintSet *copy;
996 struct clast_guard *g;
998 int min;
999 int max;
1003 static int guard_count_bounds(CloogConstraint *c, void *user)
1005 struct clast_guard_data *d = (struct clast_guard_data *) user;
1007 d->n++;
1009 return 0;
1013 /* Insert a guard, if necesessary, for constraint j.
1015 * If the constraint involves any earlier dimensions, then we have
1016 * already considered it during a previous iteration over the constraints.
1018 * If we have already generated a min [max] for the current level d->i
1019 * and if the current constraint is an upper [lower] bound, then we
1020 * can skip the constraint as it will already have been used
1021 * in that previously generated min [max].
1023 static int insert_guard_constraint(CloogConstraint *j, void *user)
1025 int i;
1026 struct clast_guard_data *d = (struct clast_guard_data *) user;
1027 int minmax = -1;
1028 int individual_constraint;
1029 struct clast_expr *v;
1030 struct clast_term *t;
1032 if (!cloog_constraint_involves(j, d->i - 1))
1033 return 0;
1035 for (i = 0; i < d->i - 1; ++i)
1036 if (cloog_constraint_involves(j, i))
1037 return 0;
1039 if (d->level && d->nb_iter >= d->level &&
1040 cloog_constraint_involves(j, d->level - 1))
1041 return 0;
1043 individual_constraint = !d->level || cloog_constraint_is_equality(j);
1044 if (!individual_constraint) {
1045 if (d->max && cloog_constraint_is_lower_bound(j, d->i - 1))
1046 return 0;
1047 if (d->min && cloog_constraint_is_upper_bound(j, d->i - 1))
1048 return 0;
1051 v = cloog_constraint_variable_expr(j, d->i, d->infos->names);
1052 d->g->eq[d->n].LHS = &(t = new_clast_term(d->infos->state->one, v))->expr;
1053 if (individual_constraint) {
1054 /* put the "denominator" in the LHS */
1055 cloog_constraint_coefficient_get(j, d->i - 1, &t->val);
1056 cloog_constraint_coefficient_set(j, d->i - 1, d->infos->state->one);
1057 if (cloog_int_is_neg(t->val)) {
1058 cloog_int_neg(t->val, t->val);
1059 cloog_constraint_coefficient_set(j, d->i - 1, d->infos->state->negone);
1061 if (d->level || cloog_constraint_is_equality(j))
1062 d->g->eq[d->n].sign = 0;
1063 else if (cloog_constraint_is_lower_bound(j, d->i - 1))
1064 d->g->eq[d->n].sign = 1;
1065 else
1066 d->g->eq[d->n].sign = -1;
1067 d->g->eq[d->n].RHS = clast_bound_from_constraint(j, d->i, d->infos->names);
1068 } else {
1069 int guarded;
1071 if (cloog_constraint_is_lower_bound(j, d->i - 1)) {
1072 minmax = 1;
1073 d->max = 1;
1074 d->g->eq[d->n].sign = 1;
1075 } else {
1076 minmax = 0;
1077 d->min = 1;
1078 d->g->eq[d->n].sign = -1;
1081 guarded = (d->nb_iter >= d->level) ? d->level : 0 ;
1082 d->g->eq[d->n].RHS = clast_minmax(d->copy, d->i, minmax, guarded, 0, 1,
1083 d->infos);
1085 d->n++;
1087 return 0;
1092 * insert_guard function:
1093 * This function inserts a guard in the clast.
1094 * A guard on an element (level) is :
1095 * -> the conjunction of all the existing constraints where the coefficient of
1096 * this element is 0 if the element is an iterator,
1097 * -> the conjunction of all the existing constraints if the element isn't an
1098 * iterator.
1099 * For instance, considering these constraints and the element j:
1100 * -3*i +2*j -M >= 0
1101 * 2*i +M >= 0
1102 * this function should return 'if (2*i+M>=0) {'.
1103 * - matrix is the polyhedron containing all the constraints,
1104 * - level is the column number of the element in matrix we want to use,
1105 * - the infos structure gives the user some options about code printing,
1106 * the number of parameters in matrix (nb_par), and the arrays of iterator
1107 * names and parameters (iters and params).
1109 * - November 3rd 2001: first version.
1110 * - November 14th 2001: a lot of 'purifications'.
1111 * - July 31th 2002: (debug) some guard parts are no more redundants.
1112 * - August 12th 2002: polyhedra union ('or' conditions) are now supported.
1113 * - October 27th 2005: polyhedra union ('or' conditions) are no more supported
1114 * (the need came from loop_simplify that may result in
1115 * domain unions, now it should be fixed directly in
1116 * cloog_loop_simplify).
1118 static void insert_guard(CloogConstraintSet *constraints, int level,
1119 struct clast_stmt ***next, CloogInfos *infos)
1121 int total_dim;
1122 struct clast_guard_data data = { level, infos, 0 };
1124 if (!constraints)
1125 return;
1127 data.copy = cloog_constraint_set_copy(constraints);
1129 data.copy = insert_extra_modulo_guards(data.copy, level, next, infos);
1131 cloog_constraint_set_foreach_constraint(constraints,
1132 guard_count_bounds, &data);
1134 data.g = new_clast_guard(data.n);
1135 data.n = 0;
1137 /* Well, it looks complicated because I wanted to have a particular, more
1138 * readable, ordering, obviously this function may be far much simpler !
1140 data.nb_iter = cloog_constraint_set_n_iterators(constraints,
1141 infos->names->nb_parameters);
1143 /* We search for guard parts. */
1144 total_dim = cloog_constraint_set_total_dimension(constraints);
1145 for (data.i = 1; data.i <= total_dim; data.i++) {
1146 data.min = 0;
1147 data.max = 0;
1148 cloog_constraint_set_foreach_constraint(data.copy,
1149 insert_guard_constraint, &data);
1152 cloog_constraint_set_free(data.copy);
1154 data.g->n = data.n;
1155 if (data.n) {
1156 clast_guard_sort(data.g);
1157 **next = &data.g->stmt;
1158 *next = &data.g->then;
1159 } else
1160 free_clast_stmt(&data.g->stmt);
1164 * Check if the constant "cst" satisfies the modulo guard that
1165 * would be introduced by insert_computed_modulo_guard.
1166 * The constant is assumed to have been reduced prior to calling
1167 * this function.
1169 static int constant_modulo_guard_is_satisfied(CloogConstraint *lower,
1170 cloog_int_t bound, cloog_int_t cst)
1172 if (cloog_constraint_is_valid(lower))
1173 return cloog_int_le(cst, bound);
1174 else
1175 return cloog_int_is_zero(cst);
1179 * Insert a modulo guard "r % mod == 0" or "r % mod <= bound",
1180 * depending on whether lower represents a valid constraint.
1182 static void insert_computed_modulo_guard(struct clast_reduction *r,
1183 CloogConstraint *lower, cloog_int_t mod, cloog_int_t bound,
1184 struct clast_stmt ***next)
1186 struct clast_expr *e;
1187 struct clast_guard *g;
1189 e = &new_clast_binary(clast_bin_mod, &r->expr, mod)->expr;
1190 g = new_clast_guard(1);
1191 if (!cloog_constraint_is_valid(lower)) {
1192 g->eq[0].LHS = e;
1193 cloog_int_set_si(bound, 0);
1194 g->eq[0].RHS = &new_clast_term(bound, NULL)->expr;
1195 g->eq[0].sign = 0;
1196 } else {
1197 g->eq[0].LHS = e;
1198 g->eq[0].RHS = &new_clast_term(bound, NULL)->expr;
1199 g->eq[0].sign = -1;
1202 **next = &g->stmt;
1203 *next = &g->then;
1207 /* Try and eliminate coefficients from a modulo constraint based on
1208 * stride information of an earlier level.
1209 * The modulo of the constraint being constructed is "m".
1210 * The stride information at level "level" is given by "stride"
1211 * and indicated that the iterator i at level "level" is equal to
1212 * some expression modulo stride->stride.
1213 * If stride->stride is a multiple of "m' then i is also equal to
1214 * the expression modulo m and so we can eliminate the coefficient of i.
1216 * If stride->constraint is NULL, then i has a constant value modulo m, stored
1217 * stride->offset. We simply multiply this constant with the coefficient
1218 * of i and add the result to the constant term, reducing it modulo m.
1220 * If stride->constraint is not NULL, then it is a constraint of the form
1222 * e + k i = s a
1224 * with s equal to stride->stride, e an expression in terms of the
1225 * parameters and earlier iterators and a some arbitrary expression
1226 * in terms of existentially quantified variables.
1227 * stride->factor is a value f such that f * k = -1 mod s.
1228 * Adding stride->constraint f * c times to the current modulo constraint,
1229 * with c the coefficient of i eliminates i in favor of parameters and
1230 * earlier variables.
1232 static void eliminate_using_stride_constraint(cloog_int_t *line, int len,
1233 int nb_iter, CloogStride *stride, int level, cloog_int_t m)
1235 if (!stride)
1236 return;
1237 if (!cloog_int_is_divisible_by(stride->stride, m))
1238 return;
1240 if (stride->constraint) {
1241 int i, s_len;
1242 cloog_int_t t, v;
1244 cloog_int_init(t);
1245 cloog_int_init(v);
1246 cloog_int_mul(t, line[level], stride->factor);
1247 for (i = 1; i < level; ++i) {
1248 cloog_constraint_coefficient_get(stride->constraint,
1249 i - 1, &v);
1250 cloog_int_addmul(line[i], t, v);
1251 cloog_int_fdiv_r(line[i], line[i], m);
1253 s_len = cloog_constraint_total_dimension(stride->constraint)+2;
1254 for (i = nb_iter + 1; i <= len - 2; ++i) {
1255 cloog_constraint_coefficient_get(stride->constraint,
1256 i - (len - s_len) - 1, &v);
1257 cloog_int_addmul(line[i], t, v);
1258 cloog_int_fdiv_r(line[i], line[i], m);
1260 cloog_constraint_constant_get(stride->constraint, &v);
1261 cloog_int_addmul(line[len - 1], t, v);
1262 cloog_int_fdiv_r(line[len - 1], line[len - 1], m);
1263 cloog_int_clear(v);
1264 cloog_int_clear(t);
1265 } else {
1266 cloog_int_addmul(line[len - 1], line[level], stride->offset);
1267 cloog_int_fdiv_r(line[len - 1], line[len - 1], m);
1270 cloog_int_set_si(line[level], 0);
1274 /* Temporary structure for communication between insert_modulo_guard and
1275 * its cloog_constraint_set_foreach_constraint callback function.
1277 struct clast_modulo_guard_data {
1278 CloogConstraint *lower;
1279 int level;
1280 struct clast_stmt ***next;
1281 CloogInfos *infos;
1282 int empty;
1283 cloog_int_t val, bound;
1287 /* Insert a modulo guard for constraint c.
1288 * The constraint may be either an equality or an inequality.
1289 * Since this function returns -1, it is only called on a single constraint.
1290 * In case of an inequality, the constraint is usually an upper bound
1291 * on d->level. However, if this variable is an existentially
1292 * quantified variable, the upper bound constraint may get removed
1293 * as trivially holding and then this function is called with
1294 * a lower bound instead. In this case, we need to adjust the constraint
1295 * based on the sum of the constant terms of the lower and upper bound
1296 * stored in d->bound.
1298 static int insert_modulo_guard_constraint(CloogConstraint *c, void *user)
1300 struct clast_modulo_guard_data *d = (struct clast_modulo_guard_data *) user;
1301 int level = d->level;
1302 CloogInfos *infos = d->infos;
1303 int i, nb_elts = 0, len, nb_iter, nb_par;
1304 int constant;
1305 struct cloog_vec *line_vector;
1306 cloog_int_t *line;
1308 len = cloog_constraint_total_dimension(c) + 2;
1309 nb_par = infos->names->nb_parameters;
1310 nb_iter = len - 2 - nb_par;
1312 line_vector = cloog_vec_alloc(len);
1313 line = line_vector->p;
1314 cloog_constraint_copy_coefficients(c, line + 1);
1316 if (cloog_int_is_pos(line[level])) {
1317 cloog_seq_neg(line + 1, line + 1, len - 1);
1318 if (!cloog_constraint_is_equality(c))
1319 cloog_int_add(line[len - 1], line[len - 1], d->bound);
1321 cloog_int_neg(line[level], line[level]);
1322 assert(cloog_int_is_pos(line[level]));
1324 nb_elts = 0;
1325 for (i = 1; i <= len-1; ++i) {
1326 if (i == level)
1327 continue;
1328 cloog_int_fdiv_r(line[i], line[i], line[level]);
1329 if (cloog_int_is_zero(line[i]))
1330 continue;
1331 if (i == len-1)
1332 continue;
1334 nb_elts++;
1337 if (nb_elts || !cloog_int_is_zero(line[len-1])) {
1338 struct clast_reduction *r;
1339 const char *name;
1341 r = new_clast_reduction(clast_red_sum, nb_elts + 1);
1342 nb_elts = 0;
1344 /* First, the modulo guard : the iterators... */
1345 i = level - 1;
1346 if (i > infos->stride_level)
1347 i = infos->stride_level;
1348 for (; i >= 1; --i)
1349 eliminate_using_stride_constraint(line, len, nb_iter,
1350 infos->stride[i - 1], i, line[level]);
1351 for (i=1;i<=nb_iter;i++) {
1352 if (i == level || cloog_int_is_zero(line[i]))
1353 continue;
1355 name = cloog_names_name_at_level(infos->names, i);
1357 r->elts[nb_elts++] = &new_clast_term(line[i],
1358 &new_clast_name(name)->expr)->expr;
1361 /* ...the parameters... */
1362 for (i=nb_iter+1;i<=len-2;i++) {
1363 if (cloog_int_is_zero(line[i]))
1364 continue;
1366 name = infos->names->parameters[i-nb_iter-1] ;
1367 r->elts[nb_elts++] = &new_clast_term(line[i],
1368 &new_clast_name(name)->expr)->expr;
1371 constant = nb_elts == 0;
1372 /* ...the constant. */
1373 if (!cloog_int_is_zero(line[len-1]))
1374 r->elts[nb_elts++] = &new_clast_term(line[len-1], NULL)->expr;
1376 /* our initial computation may have been an overestimate */
1377 r->n = nb_elts;
1379 if (constant) {
1380 d->empty = !constant_modulo_guard_is_satisfied(d->lower, d->bound,
1381 line[len - 1]);
1382 free_clast_reduction(r);
1383 } else
1384 insert_computed_modulo_guard(r, d->lower, line[level], d->bound,
1385 d->next);
1388 cloog_vec_free(line_vector);
1390 return -1;
1395 * insert_modulo_guard:
1396 * This function inserts a modulo guard corresponding to an equality
1397 * or a pair of inequalities.
1398 * Returns 0 if the modulo guard is discovered to be unsatisfiable.
1400 * See insert_equation.
1401 * - matrix is the polyhedron containing all the constraints,
1402 * - upper and lower are the line numbers of the constraint in matrix
1403 * we want to print; in particular, if we want to print an equality,
1404 * then lower == -1 and upper is the row of the equality; if we want
1405 * to print an inequality, then upper is the row of the upper bound
1406 * and lower in the row of the lower bound
1407 * - level is the column number of the element in matrix we want to use,
1408 * - the infos structure gives the user some options about code printing,
1409 * the number of parameters in matrix (nb_par), and the arrays of iterator
1410 * names and parameters (iters and params).
1412 static int insert_modulo_guard(CloogConstraint *upper,
1413 CloogConstraint *lower, int level,
1414 struct clast_stmt ***next, CloogInfos *infos)
1416 int nb_par;
1417 CloogConstraintSet *set;
1418 struct clast_modulo_guard_data data = { lower, level, next, infos, 0 };
1420 cloog_int_init(data.val);
1421 cloog_constraint_coefficient_get(upper, level-1, &data.val);
1422 if (cloog_int_is_one(data.val) || cloog_int_is_neg_one(data.val)) {
1423 cloog_int_clear(data.val);
1424 return 1;
1427 nb_par = infos->names->nb_parameters;
1429 cloog_int_init(data.bound);
1430 /* Check if would be emitting the redundant constraint mod(e,m) <= m-1 */
1431 if (cloog_constraint_is_valid(lower)) {
1432 cloog_constraint_constant_get(upper, &data.val);
1433 cloog_constraint_constant_get(lower, &data.bound);
1434 cloog_int_add(data.bound, data.val, data.bound);
1435 cloog_constraint_coefficient_get(lower, level-1, &data.val);
1436 cloog_int_sub_ui(data.val, data.val, 1);
1437 if (cloog_int_eq(data.val, data.bound)) {
1438 cloog_int_clear(data.val);
1439 cloog_int_clear(data.bound);
1440 return 1;
1444 if (cloog_constraint_needs_reduction(upper, level)) {
1445 set = cloog_constraint_set_for_reduction(upper, lower);
1446 set = cloog_constraint_set_reduce(set, level, infos->equal,
1447 nb_par, &data.bound);
1448 cloog_constraint_set_foreach_constraint(set,
1449 insert_modulo_guard_constraint, &data);
1450 cloog_constraint_set_free(set);
1451 } else
1452 insert_modulo_guard_constraint(upper, &data);
1454 cloog_int_clear(data.val);
1455 cloog_int_clear(data.bound);
1457 return !data.empty;
1462 * We found an equality or a pair of inequalities identifying
1463 * a loop with a single iteration, but the user wants us to generate
1464 * a loop anyway, so we do it here.
1466 static int insert_equation_as_loop(CloogDomain *domain, CloogConstraint *upper,
1467 CloogConstraint *lower, int level, struct clast_stmt ***next,
1468 CloogInfos *infos)
1470 const char *iterator = cloog_names_name_at_level(infos->names, level);
1471 struct clast_expr *e1, *e2;
1472 struct clast_for *f;
1474 e2 = clast_bound_from_constraint(upper, level, infos->names);
1475 if (!cloog_constraint_is_valid(lower))
1476 e1 = clast_expr_copy(e2);
1477 else
1478 e1 = clast_bound_from_constraint(lower, level, infos->names);
1480 f = new_clast_for(domain, iterator, e1, e2, infos->stride[level-1]);
1481 **next = &f->stmt;
1482 *next = &f->body;
1484 cloog_constraint_release(lower);
1485 cloog_constraint_release(upper);
1486 return 1;
1491 * insert_equation function:
1492 * This function inserts an equality
1493 * constraint according to an element in the clast.
1494 * Returns 1 if the calling function should recurse into inner loops.
1496 * An equality can be preceded by a 'modulo guard'.
1497 * For instance, consider the constraint i -2*j = 0 and the
1498 * element j: pprint_equality should return 'if(i%2==0) { j = i/2 ;'.
1499 * - matrix is the polyhedron containing all the constraints,
1500 * - num is the line number of the constraint in matrix we want to print,
1501 * - level is the column number of the element in matrix we want to use,
1502 * - the infos structure gives the user some options about code printing,
1503 * the number of parameters in matrix (nb_par), and the arrays of iterator
1504 * names and parameters (iters and params).
1506 * - November 13th 2001: first version.
1507 * - June 26th 2003: simplification of the modulo guards (remove parts such as
1508 * modulo is 0, compare vivien or vivien2 with a previous
1509 * version for an idea).
1510 * - June 29th 2003: non-unit strides support.
1511 * - July 14th 2003: (debug) no more print the constant in the modulo guard when
1512 * it was previously included in a stride calculation.
1514 static int insert_equation(CloogDomain *domain, CloogConstraint *upper,
1515 CloogConstraint *lower, int level, struct clast_stmt
1516 ***next, CloogInfos *infos)
1518 struct clast_expr *e;
1519 struct clast_assignment *ass;
1521 if (!infos->options->otl)
1522 return insert_equation_as_loop(domain, upper, lower, level, next, infos);
1524 if (!insert_modulo_guard(upper, lower, level, next, infos)) {
1525 cloog_constraint_release(lower);
1526 cloog_constraint_release(upper);
1528 return 0;
1531 if (cloog_constraint_is_valid(lower) ||
1532 !clast_equal_add(infos->equal, NULL, level, upper, infos))
1533 { /* Finally, the equality. */
1535 /* If we have to make a block by dimension, we start the block. Function
1536 * pprint knows if there is an equality, if this is the case, it checks
1537 * for the same following condition to close the brace.
1539 if (infos->options->block) {
1540 struct clast_block *b = new_clast_block();
1541 **next = &b->stmt;
1542 *next = &b->body;
1545 e = clast_bound_from_constraint(upper, level, infos->names);
1546 ass = new_clast_assignment(cloog_names_name_at_level(infos->names, level), e);
1548 **next = &ass->stmt;
1549 *next = &(**next)->next;
1552 cloog_constraint_release(lower);
1553 cloog_constraint_release(upper);
1555 return 1;
1560 * Insert a loop that is executed exactly once as an assignment.
1561 * In particular, the loop
1563 * for (i = e; i <= e; ++i) {
1564 * S;
1567 * is generated as
1569 * i = e;
1570 * S;
1573 static void insert_otl_for(CloogConstraintSet *constraints, int level,
1574 struct clast_expr *e, struct clast_stmt ***next, CloogInfos *infos)
1576 const char *iterator;
1578 iterator = cloog_names_name_at_level(infos->names, level);
1580 if (!clast_equal_add(infos->equal, constraints, level,
1581 cloog_constraint_invalid(), infos)) {
1582 struct clast_assignment *ass;
1583 if (infos->options->block) {
1584 struct clast_block *b = new_clast_block();
1585 **next = &b->stmt;
1586 *next = &b->body;
1588 ass = new_clast_assignment(iterator, e);
1589 **next = &ass->stmt;
1590 *next = &(**next)->next;
1591 } else {
1592 free_clast_expr(e);
1598 * Insert a loop that is executed at most once as an assignment followed
1599 * by a guard. In particular, the loop
1601 * for (i = e1; i <= e2; ++i) {
1602 * S;
1605 * is generated as
1607 * i = e1;
1608 * if (i <= e2) {
1609 * S;
1613 static void insert_guarded_otl_for(CloogConstraintSet *constraints, int level,
1614 struct clast_expr *e1, struct clast_expr *e2,
1615 struct clast_stmt ***next, CloogInfos *infos)
1617 const char *iterator;
1618 struct clast_assignment *ass;
1619 struct clast_guard *guard;
1621 iterator = cloog_names_name_at_level(infos->names, level);
1623 if (infos->options->block) {
1624 struct clast_block *b = new_clast_block();
1625 **next = &b->stmt;
1626 *next = &b->body;
1628 ass = new_clast_assignment(iterator, e1);
1629 **next = &ass->stmt;
1630 *next = &(**next)->next;
1632 guard = new_clast_guard(1);
1633 guard->eq[0].sign = -1;
1634 guard->eq[0].LHS = &new_clast_term(infos->state->one,
1635 &new_clast_name(iterator)->expr)->expr;
1636 guard->eq[0].RHS = e2;
1638 **next = &guard->stmt;
1639 *next = &guard->then;
1644 * insert_for function:
1645 * This function inserts a for loop in the clast.
1646 * Returns 1 if the calling function should recurse into inner loops.
1648 * A loop header according to an element is the conjunction of a minimum and a
1649 * maximum on a given element (they give the loop bounds).
1650 * For instance, considering these constraints and the element j:
1651 * i + j -9*M >= 0
1652 * -j +5*M >= 0
1653 * j -4*M >= 0
1654 * this function should return 'for (j=max(-i+9*M,4*M),j<=5*M;j++) {'.
1655 * - constraints contains all constraints,
1656 * - level is the column number of the element in matrix we want to use,
1657 * - otl is set if the loop is executed at most once,
1658 * - the infos structure gives the user some options about code printing,
1659 * the number of parameters in matrix (nb_par), and the arrays of iterator
1660 * names and parameters (iters and params).
1662 static int insert_for(CloogDomain *domain, CloogConstraintSet *constraints,
1663 int level, int otl, struct clast_stmt ***next,
1664 CloogInfos *infos)
1666 const char *iterator;
1667 struct clast_expr *e1;
1668 struct clast_expr *e2;
1670 e1 = clast_minmax(constraints, level, 1, 0, 1, 0, infos);
1671 e2 = clast_minmax(constraints, level, 0, 0, 0, 0, infos);
1673 if (clast_expr_is_bigger_constant(e1, e2)) {
1674 free_clast_expr(e1);
1675 free_clast_expr(e2);
1676 return 0;
1679 /* If min and max are not equal there is a 'for' else, there is a '='.
1680 * In the special case e1 = e2 = NULL, this is an infinite loop
1681 * so this is not a '='.
1683 if (e1 && e2 && infos->options->otl && clast_expr_equal(e1, e2)) {
1684 free_clast_expr(e2);
1685 insert_otl_for(constraints, level, e1, next, infos);
1686 } else if (otl) {
1687 insert_guarded_otl_for(constraints, level, e1, e2, next, infos);
1688 } else {
1689 struct clast_for *f;
1690 iterator = cloog_names_name_at_level(infos->names, level);
1692 f = new_clast_for(domain, iterator, e1, e2, infos->stride[level-1]);
1693 **next = &f->stmt;
1694 *next = &f->body;
1697 return 1;
1702 * insert_block function:
1703 * This function inserts a statement block.
1704 * - block is the statement block,
1705 * - level is the number of loops enclosing the statement,
1706 * - the infos structure gives the user some options about code printing,
1707 * the number of parameters in domain (nb_par), and the arrays of iterator
1708 * names and parameters (iters and params).
1710 * - September 21th 2003: first version (pick from pprint function).
1712 static void insert_block(CloogDomain *domain, CloogBlock *block, int level,
1713 struct clast_stmt ***next, CloogInfos *infos)
1715 CloogStatement * statement ;
1716 struct clast_stmt *subs;
1718 if (!block)
1719 return;
1721 for (statement = block->statement; statement; statement = statement->next) {
1722 CloogStatement *s_next = statement->next;
1724 subs = clast_equal(level,infos);
1726 statement->next = NULL;
1727 **next = &new_clast_user_stmt(domain, statement, subs)->stmt;
1728 statement->next = s_next;
1729 *next = &(**next)->next;
1735 * insert_loop function:
1736 * This function converts the content of a CloogLoop structure (loop) into a
1737 * clast_stmt (inserted at **next).
1738 * The iterator (level) of
1739 * the current loop is given by 'level': this is the column number of the
1740 * domain corresponding to the current loop iterator. The data of a loop are
1741 * written in this order:
1742 * 1. The guard of the loop, i.e. each constraint in the domain that does not
1743 * depend on the iterator (when the entry in the column 'level' is 0).
1744 * 2. The iteration domain of the iterator, given by the constraints in the
1745 * domain depending on the iterator, i.e.:
1746 * * an equality if the iterator has only one value (possibly preceded by
1747 * a guard verifying if this value is integral), *OR*
1748 * * a loop from the minimum possible value of the iterator to the maximum
1749 * possible value.
1750 * 3. The included statement block.
1751 * 4. The inner loops (recursive call).
1752 * 5. The following loops (recursive call).
1753 * - level is the recursion level or the iteration level that we are printing,
1754 * - the infos structure gives the user some options about code printing,
1755 * the number of parameters in domain (nb_par), and the arrays of iterator
1756 * names and parameters (iters and params).
1758 * - November 2nd 2001: first version.
1759 * - March 6th 2003: infinite domain support.
1760 * - April 19th 2003: (debug) NULL loop support.
1761 * - June 29th 2003: non-unit strides support.
1762 * - April 28th 2005: (debug) level is level+equality when print statement!
1763 * - June 16th 2005: (debug) the N. Vasilache normalization step has been
1764 * added to avoid iteration duplication (see DaeGon Kim
1765 * bug in cloog_program_generate). Try vasilache.cloog
1766 * with and without the call to cloog_polylib_matrix_normalize,
1767 * using -f 8 -l 9 options for an idea.
1768 * - September 15th 2005: (debug) don't close equality braces when unnecessary.
1769 * - October 16th 2005: (debug) scalar value is saved for next loops.
1771 static void insert_loop(CloogLoop * loop, int level,
1772 struct clast_stmt ***next, CloogInfos *infos)
1774 int equality = 0;
1775 CloogConstraintSet *constraints, *temp;
1776 struct clast_stmt **top = *next;
1777 CloogConstraint *i, *j;
1778 int empty_loop = 0;
1780 /* It can happen that loop be NULL when an input polyhedron is empty. */
1781 if (loop == NULL)
1782 return;
1784 /* The constraints do not always have a shape that allows us to generate code from it,
1785 * thus we normalize it, we also simplify it with the equalities.
1787 temp = cloog_domain_constraints(loop->domain);
1788 cloog_constraint_set_normalize(temp,level);
1789 constraints = cloog_constraint_set_simplify(temp,infos->equal,level,
1790 infos->names->nb_parameters);
1791 cloog_constraint_set_free(temp);
1792 if (level) {
1793 infos->stride[level - 1] = loop->stride;
1794 infos->stride_level++;
1797 /* First of all we have to print the guard. */
1798 insert_guard(constraints,level, next, infos);
1800 if (level && cloog_constraint_set_contains_level(constraints, level,
1801 infos->names->nb_parameters)) {
1802 /* We scan all the constraints to know in which case we are :
1803 * [[if] equation] or [for].
1805 if (cloog_constraint_is_valid(i =
1806 cloog_constraint_set_defining_equality(constraints, level))) {
1807 empty_loop = !insert_equation(loop->unsimplified, i,
1808 cloog_constraint_invalid(), level, next,
1809 infos);
1810 equality = 1 ;
1811 } else if (cloog_constraint_is_valid(i =
1812 cloog_constraint_set_defining_inequalities(constraints,
1813 level, &j, infos->names->nb_parameters))) {
1814 empty_loop = !insert_equation(loop->unsimplified, i, j, level, next,
1815 infos);
1816 } else
1817 empty_loop = !insert_for(loop->unsimplified, constraints, level,
1818 loop->otl, next, infos);
1821 if (!empty_loop) {
1822 /* Finally, if there is an included statement block, print it. */
1823 insert_block(loop->unsimplified, loop->block, level+equality, next, infos);
1825 /* Go to the next level. */
1826 if (loop->inner != NULL)
1827 insert_loop(loop->inner, level+1, next, infos);
1830 if (level) {
1831 cloog_equal_del(infos->equal,level);
1832 infos->stride_level--;
1834 cloog_constraint_set_free(constraints);
1836 /* Go to the next loop on the same level. */
1837 while (*top)
1838 top = &(*top)->next;
1839 if (loop->next != NULL)
1840 insert_loop(loop->next, level, &top,infos);
1844 struct clast_stmt *cloog_clast_create(CloogProgram *program,
1845 CloogOptions *options)
1847 CloogInfos *infos = ALLOC(CloogInfos);
1848 int nb_levels;
1849 struct clast_stmt *root = &new_clast_root(program->names)->stmt;
1850 struct clast_stmt **next = &root->next;
1852 infos->state = options->state;
1853 infos->names = program->names;
1854 infos->options = options;
1855 infos->scaldims = program->scaldims;
1856 infos->nb_scattdims = program->nb_scattdims;
1858 /* Allocation for the array of strides, there is a +1 since the statement can
1859 * be included inside an external loop without iteration domain.
1861 nb_levels = program->names->nb_scattering+program->names->nb_iterators+1;
1862 infos->stride = ALLOCN(CloogStride *, nb_levels);
1863 infos->stride_level = 0;
1865 infos->equal = cloog_equal_alloc(nb_levels,
1866 nb_levels, program->names->nb_parameters);
1868 insert_loop(program->loop, 0, &next, infos);
1870 cloog_equal_free(infos->equal);
1872 free(infos->stride);
1873 free(infos);
1875 return root;
1879 struct clast_stmt *cloog_clast_create_from_input(CloogInput *input,
1880 CloogOptions *options)
1882 CloogProgram *program;
1883 struct clast_stmt *root;
1885 program = cloog_program_alloc(input->context, input->ud, options);
1886 free(input);
1888 program = cloog_program_generate(program, options);
1890 root = cloog_clast_create(program, options);
1891 cloog_program_free(program);
1893 return root;