Compute min and max bounds for IVs and infer types.
[official-gcc/graphite-test-results.git] / gcc / graphite-clast-to-gimple.c
blob58fb2aef971a2f7ea0271630e4f57e562d9f5369
1 /* Translation of CLAST (CLooG AST) to Gimple.
2 Copyright (C) 2009 Free Software Foundation, Inc.
3 Contributed by Sebastian Pop <sebastian.pop@amd.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "ggc.h"
26 #include "tree.h"
27 #include "rtl.h"
28 #include "basic-block.h"
29 #include "diagnostic.h"
30 #include "tree-flow.h"
31 #include "toplev.h"
32 #include "tree-dump.h"
33 #include "timevar.h"
34 #include "cfgloop.h"
35 #include "tree-chrec.h"
36 #include "tree-data-ref.h"
37 #include "tree-scalar-evolution.h"
38 #include "tree-pass.h"
39 #include "domwalk.h"
40 #include "value-prof.h"
41 #include "pointer-set.h"
42 #include "gimple.h"
43 #include "langhooks.h"
44 #include "sese.h"
46 #ifdef HAVE_cloog
47 #include "cloog/cloog.h"
48 #include "ppl_c.h"
49 #include "graphite-ppl.h"
50 #include "graphite.h"
51 #include "graphite-poly.h"
52 #include "graphite-scop-detection.h"
53 #include "graphite-clast-to-gimple.h"
54 #include "graphite-dependences.h"
56 /* This flag is set when an error occurred during the translation of
57 CLAST to Gimple. */
58 static bool gloog_error;
60 /* Verifies properties that GRAPHITE should maintain during translation. */
62 static inline void
63 graphite_verify (void)
65 #ifdef ENABLE_CHECKING
66 verify_loop_structure ();
67 verify_dominators (CDI_DOMINATORS);
68 verify_dominators (CDI_POST_DOMINATORS);
69 verify_ssa (false);
70 verify_loop_closed_ssa ();
71 #endif
74 /* Stores the INDEX in a vector for a given clast NAME. */
76 typedef struct clast_name_index {
77 int index;
78 const char *name;
79 } *clast_name_index_p;
81 /* Returns a pointer to a new element of type clast_name_index_p built
82 from NAME and INDEX. */
84 static inline clast_name_index_p
85 new_clast_name_index (const char *name, int index)
87 clast_name_index_p res = XNEW (struct clast_name_index);
89 res->name = name;
90 res->index = index;
91 return res;
94 /* For a given clast NAME, returns -1 if it does not correspond to any
95 parameter, or otherwise, returns the index in the PARAMS or
96 SCATTERING_DIMENSIONS vector. */
98 static inline int
99 clast_name_to_index (const char *name, htab_t index_table)
101 struct clast_name_index tmp;
102 PTR *slot;
104 tmp.name = name;
105 slot = htab_find_slot (index_table, &tmp, NO_INSERT);
107 if (slot && *slot)
108 return ((struct clast_name_index *) *slot)->index;
110 return -1;
113 /* Records in INDEX_TABLE the INDEX for NAME. */
115 static inline void
116 save_clast_name_index (htab_t index_table, const char *name, int index)
118 struct clast_name_index tmp;
119 PTR *slot;
121 tmp.name = name;
122 slot = htab_find_slot (index_table, &tmp, INSERT);
124 if (slot)
126 if (*slot)
127 free (*slot);
129 *slot = new_clast_name_index (name, index);
133 /* Print to stderr the element ELT. */
135 static inline void
136 debug_clast_name_index (clast_name_index_p elt)
138 fprintf (stderr, "(index = %d, name = %s)\n", elt->index, elt->name);
141 /* Helper function for debug_rename_map. */
143 static inline int
144 debug_clast_name_indexes_1 (void **slot, void *s ATTRIBUTE_UNUSED)
146 struct clast_name_index *entry = (struct clast_name_index *) *slot;
147 debug_clast_name_index (entry);
148 return 1;
151 /* Print to stderr all the elements of MAP. */
153 void
154 debug_clast_name_indexes (htab_t map)
156 htab_traverse (map, debug_clast_name_indexes_1, NULL);
159 /* Computes a hash function for database element ELT. */
161 static inline hashval_t
162 clast_name_index_elt_info (const void *elt)
164 return htab_hash_pointer (((const struct clast_name_index *) elt)->name);
167 /* Compares database elements E1 and E2. */
169 static inline int
170 eq_clast_name_indexes (const void *e1, const void *e2)
172 const struct clast_name_index *elt1 = (const struct clast_name_index *) e1;
173 const struct clast_name_index *elt2 = (const struct clast_name_index *) e2;
175 return (elt1->name == elt2->name);
179 /* For a given loop DEPTH in the loop nest of the original black box
180 PBB, return the old induction variable associated to that loop. */
182 static inline tree
183 pbb_to_depth_to_oldiv (poly_bb_p pbb, int depth)
185 gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
186 sese region = SCOP_REGION (PBB_SCOP (pbb));
187 loop_p loop = gbb_loop_at_index (gbb, region, depth);
189 return loop->single_iv;
192 /* For a given scattering dimension, return the new induction variable
193 associated to it. */
195 static inline tree
196 newivs_to_depth_to_newiv (VEC (tree, heap) *newivs, int depth)
198 return VEC_index (tree, newivs, depth);
203 /* Returns the tree variable from the name NAME that was given in
204 Cloog representation. */
206 static tree
207 clast_name_to_gcc (const char *name, sese region, VEC (tree, heap) *newivs,
208 htab_t newivs_index, htab_t params_index)
210 int index;
211 VEC (tree, heap) *params = SESE_PARAMS (region);
213 if (params && params_index)
215 index = clast_name_to_index (name, params_index);
217 if (index >= 0)
218 return VEC_index (tree, params, index);
221 gcc_assert (newivs && newivs_index);
222 index = clast_name_to_index (name, newivs_index);
223 gcc_assert (index >= 0);
225 return newivs_to_depth_to_newiv (newivs, index);
228 /* Returns the signed maximal precision type for expressions TYPE1 and TYPE2. */
230 static tree
231 max_signed_precision_type (tree type1, tree type2)
233 int p1 = TYPE_PRECISION (type1);
234 int p2 = TYPE_PRECISION (type2);
235 int precision = p1 > p2 ? p1 : p2;
236 tree type = lang_hooks.types.type_for_size (precision, false);
238 if (!type)
240 gloog_error = true;
241 return integer_type_node;
243 return type;
246 /* Returns the maximal precision type for expressions TYPE1 and TYPE2. */
248 static tree
249 max_precision_type (tree type1, tree type2)
252 if (POINTER_TYPE_P (type1))
253 return type1;
255 if (POINTER_TYPE_P (type2))
256 return type2;
258 if (!TYPE_UNSIGNED (type1)
259 || !TYPE_UNSIGNED (type2))
260 return max_signed_precision_type (type1, type2);
262 return TYPE_PRECISION (type1) > TYPE_PRECISION (type2) ? type1 : type2;
265 static tree
266 clast_to_gcc_expression (tree, struct clast_expr *, sese, VEC (tree, heap) *,
267 htab_t, htab_t);
269 /* Converts a Cloog reduction expression R with reduction operation OP
270 to a GCC expression tree of type TYPE. */
272 static tree
273 clast_to_gcc_expression_red (tree type, enum tree_code op,
274 struct clast_reduction *r,
275 sese region, VEC (tree, heap) *newivs,
276 htab_t newivs_index, htab_t params_index)
278 int i;
279 tree res = clast_to_gcc_expression (type, r->elts[0], region, newivs,
280 newivs_index, params_index);
281 tree operand_type = (op == POINTER_PLUS_EXPR) ? sizetype : type;
283 for (i = 1; i < r->n; i++)
285 tree t = clast_to_gcc_expression (operand_type, r->elts[i], region,
286 newivs, newivs_index, params_index);
287 res = fold_build2 (op, type, res, t);
290 return res;
293 /* Converts a Cloog AST expression E back to a GCC expression tree of
294 type TYPE. */
296 static tree
297 clast_to_gcc_expression (tree type, struct clast_expr *e,
298 sese region, VEC (tree, heap) *newivs,
299 htab_t newivs_index, htab_t params_index)
301 switch (e->type)
303 case expr_term:
305 struct clast_term *t = (struct clast_term *) e;
307 if (t->var)
309 if (value_one_p (t->val))
311 tree name = clast_name_to_gcc (t->var, region, newivs,
312 newivs_index, params_index);
314 if (POINTER_TYPE_P (TREE_TYPE (name)) != POINTER_TYPE_P (type))
315 name = fold_convert (sizetype, name);
317 name = fold_convert (type, name);
318 return name;
321 else if (value_mone_p (t->val))
323 tree name = clast_name_to_gcc (t->var, region, newivs,
324 newivs_index, params_index);
326 if (POINTER_TYPE_P (TREE_TYPE (name)) != POINTER_TYPE_P (type))
327 name = fold_convert (sizetype, name);
329 name = fold_convert (type, name);
331 return fold_build1 (NEGATE_EXPR, type, name);
333 else
335 tree name = clast_name_to_gcc (t->var, region, newivs,
336 newivs_index, params_index);
337 tree cst = gmp_cst_to_tree (type, t->val);
339 if (POINTER_TYPE_P (TREE_TYPE (name)) != POINTER_TYPE_P (type))
340 name = fold_convert (sizetype, name);
342 name = fold_convert (type, name);
344 if (!POINTER_TYPE_P (type))
345 return fold_build2 (MULT_EXPR, type, cst, name);
347 gloog_error = true;
348 return cst;
351 else
352 return gmp_cst_to_tree (type, t->val);
355 case expr_red:
357 struct clast_reduction *r = (struct clast_reduction *) e;
359 switch (r->type)
361 case clast_red_sum:
362 return clast_to_gcc_expression_red
363 (type, POINTER_TYPE_P (type) ? POINTER_PLUS_EXPR : PLUS_EXPR,
364 r, region, newivs, newivs_index, params_index);
366 case clast_red_min:
367 return clast_to_gcc_expression_red (type, MIN_EXPR, r, region,
368 newivs, newivs_index,
369 params_index);
371 case clast_red_max:
372 return clast_to_gcc_expression_red (type, MAX_EXPR, r, region,
373 newivs, newivs_index,
374 params_index);
376 default:
377 gcc_unreachable ();
379 break;
382 case expr_bin:
384 struct clast_binary *b = (struct clast_binary *) e;
385 struct clast_expr *lhs = (struct clast_expr *) b->LHS;
386 tree tl = clast_to_gcc_expression (type, lhs, region, newivs,
387 newivs_index, params_index);
388 tree tr = gmp_cst_to_tree (type, b->RHS);
390 switch (b->type)
392 case clast_bin_fdiv:
393 return fold_build2 (FLOOR_DIV_EXPR, type, tl, tr);
395 case clast_bin_cdiv:
396 return fold_build2 (CEIL_DIV_EXPR, type, tl, tr);
398 case clast_bin_div:
399 return fold_build2 (EXACT_DIV_EXPR, type, tl, tr);
401 case clast_bin_mod:
402 return fold_build2 (TRUNC_MOD_EXPR, type, tl, tr);
404 default:
405 gcc_unreachable ();
409 default:
410 gcc_unreachable ();
413 return NULL_TREE;
416 /* Return the precision needed to represent the value VAL. */
418 static int
419 precision_for_value (Value val)
421 Value x, y, two;
422 int precision;
424 value_init (x);
425 value_init (y);
426 value_init (two);
427 value_set_si (x, 2);
428 value_assign (y, val);
429 value_set_si (two, 2);
430 precision = 1;
432 if (value_neg_p (y))
433 value_oppose (y, y);
435 while (value_gt (y, x))
437 value_multiply (x, x, two);
438 precision++;
441 value_clear (x);
442 value_clear (y);
443 value_clear (two);
445 return precision;
448 /* Return the precision needed to represent the values between LOW and
449 UP. */
451 static int
452 precision_for_interval (Value low, Value up)
454 Value diff;
455 int precision;
457 gcc_assert (value_le (low, up));
459 value_init (diff);
460 value_subtract (diff, up, low);
461 precision = precision_for_value (diff);
462 value_clear (diff);
464 return precision;
467 /* Return a type that could represent the integer value VAL, or
468 otherwise return NULL_TREE. */
470 static tree
471 gcc_type_for_interval (Value low, Value up, tree old_type)
473 bool unsigned_p = true;
474 int precision, prec_up, prec_int;
475 tree type;
477 gcc_assert (value_le (low, up));
479 /* Preserve the signedness of the old IV. */
480 if ((old_type && !TYPE_UNSIGNED (old_type))
481 || value_neg_p (low))
482 unsigned_p = false;
484 prec_up = precision_for_value (up);
485 prec_int = precision_for_interval (low, up);
486 precision = prec_up > prec_int ? prec_up : prec_int;
488 type = lang_hooks.types.type_for_size (precision, unsigned_p);
489 if (!type)
491 gloog_error = true;
492 return integer_type_node;
495 return type;
498 /* Return a type that could represent the integer value VAL, or
499 otherwise return NULL_TREE. */
501 static tree
502 gcc_type_for_value (Value val)
504 return gcc_type_for_interval (val, val, NULL_TREE);
507 /* Return the type for the clast_term T used in STMT. */
509 static tree
510 gcc_type_for_clast_term (struct clast_term *t,
511 sese region, VEC (tree, heap) *newivs,
512 htab_t newivs_index, htab_t params_index)
514 gcc_assert (t->expr.type == expr_term);
516 if (!t->var)
517 return gcc_type_for_value (t->val);
519 return TREE_TYPE (clast_name_to_gcc (t->var, region, newivs,
520 newivs_index, params_index));
523 static tree
524 gcc_type_for_clast_expr (struct clast_expr *, sese,
525 VEC (tree, heap) *, htab_t, htab_t);
527 /* Return the type for the clast_reduction R used in STMT. */
529 static tree
530 gcc_type_for_clast_red (struct clast_reduction *r, sese region,
531 VEC (tree, heap) *newivs,
532 htab_t newivs_index, htab_t params_index)
534 int i;
535 tree type = NULL_TREE;
537 if (r->n == 1)
538 return gcc_type_for_clast_expr (r->elts[0], region, newivs,
539 newivs_index, params_index);
541 switch (r->type)
543 case clast_red_sum:
544 case clast_red_min:
545 case clast_red_max:
546 type = gcc_type_for_clast_expr (r->elts[0], region, newivs,
547 newivs_index, params_index);
548 for (i = 1; i < r->n; i++)
549 type = max_precision_type (type, gcc_type_for_clast_expr
550 (r->elts[i], region, newivs,
551 newivs_index, params_index));
553 return type;
555 default:
556 break;
559 gcc_unreachable ();
560 return NULL_TREE;
563 /* Return the type for the clast_binary B used in STMT. */
565 static tree
566 gcc_type_for_clast_bin (struct clast_binary *b,
567 sese region, VEC (tree, heap) *newivs,
568 htab_t newivs_index, htab_t params_index)
570 tree l = gcc_type_for_clast_expr ((struct clast_expr *) b->LHS, region,
571 newivs, newivs_index, params_index);
572 tree r = gcc_type_for_value (b->RHS);
573 return max_signed_precision_type (l, r);
576 /* Returns the type for the CLAST expression E when used in statement
577 STMT. */
579 static tree
580 gcc_type_for_clast_expr (struct clast_expr *e,
581 sese region, VEC (tree, heap) *newivs,
582 htab_t newivs_index, htab_t params_index)
584 switch (e->type)
586 case expr_term:
587 return gcc_type_for_clast_term ((struct clast_term *) e, region,
588 newivs, newivs_index, params_index);
590 case expr_red:
591 return gcc_type_for_clast_red ((struct clast_reduction *) e, region,
592 newivs, newivs_index, params_index);
594 case expr_bin:
595 return gcc_type_for_clast_bin ((struct clast_binary *) e, region,
596 newivs, newivs_index, params_index);
598 default:
599 gcc_unreachable ();
602 return NULL_TREE;
605 /* Returns the type for the equation CLEQ. */
607 static tree
608 gcc_type_for_clast_eq (struct clast_equation *cleq,
609 sese region, VEC (tree, heap) *newivs,
610 htab_t newivs_index, htab_t params_index)
612 tree l = gcc_type_for_clast_expr (cleq->LHS, region, newivs,
613 newivs_index, params_index);
614 tree r = gcc_type_for_clast_expr (cleq->RHS, region, newivs,
615 newivs_index, params_index);
616 return max_precision_type (l, r);
619 /* Translates a clast equation CLEQ to a tree. */
621 static tree
622 graphite_translate_clast_equation (sese region,
623 struct clast_equation *cleq,
624 VEC (tree, heap) *newivs,
625 htab_t newivs_index, htab_t params_index)
627 enum tree_code comp;
628 tree type = gcc_type_for_clast_eq (cleq, region, newivs, newivs_index,
629 params_index);
630 tree lhs = clast_to_gcc_expression (type, cleq->LHS, region, newivs,
631 newivs_index, params_index);
632 tree rhs = clast_to_gcc_expression (type, cleq->RHS, region, newivs,
633 newivs_index, params_index);
635 if (cleq->sign == 0)
636 comp = EQ_EXPR;
638 else if (cleq->sign > 0)
639 comp = GE_EXPR;
641 else
642 comp = LE_EXPR;
644 return fold_build2 (comp, boolean_type_node, lhs, rhs);
647 /* Creates the test for the condition in STMT. */
649 static tree
650 graphite_create_guard_cond_expr (sese region, struct clast_guard *stmt,
651 VEC (tree, heap) *newivs,
652 htab_t newivs_index, htab_t params_index)
654 tree cond = NULL;
655 int i;
657 for (i = 0; i < stmt->n; i++)
659 tree eq = graphite_translate_clast_equation (region, &stmt->eq[i],
660 newivs, newivs_index,
661 params_index);
663 if (cond)
664 cond = fold_build2 (TRUTH_AND_EXPR, TREE_TYPE (eq), cond, eq);
665 else
666 cond = eq;
669 return cond;
672 /* Creates a new if region corresponding to Cloog's guard. */
674 static edge
675 graphite_create_new_guard (sese region, edge entry_edge,
676 struct clast_guard *stmt,
677 VEC (tree, heap) *newivs,
678 htab_t newivs_index, htab_t params_index)
680 tree cond_expr = graphite_create_guard_cond_expr (region, stmt, newivs,
681 newivs_index, params_index);
682 edge exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr);
683 return exit_edge;
686 /* Compute the lower bound LOW and upper bound UP for the induction
687 variable at LEVEL for the statement PBB, based on the transformed
688 scattering of PBB: T|I|G|Cst, with T the scattering transform, I
689 the iteration domain, and G the context parameters. */
691 static void
692 compute_bounds_for_level (poly_bb_p pbb, int level, Value low, Value up)
694 ppl_Pointset_Powerset_C_Polyhedron_t ps;
695 ppl_Linear_Expression_t le;
697 combine_context_id_scat (&ps, pbb, false);
699 /* Prepare the linear expression corresponding to the level that we
700 want to maximize/minimize. */
702 ppl_dimension_type dim = pbb_nb_scattering_transform (pbb)
703 + pbb_dim_iter_domain (pbb) + pbb_nb_params (pbb);
705 ppl_new_Linear_Expression_with_dimension (&le, dim);
706 ppl_set_coef (le, 2 * level + 1, 1);
709 ppl_max_for_le_pointset (ps, le, up);
710 ppl_min_for_le_pointset (ps, le, low);
713 /* Compute the type for the induction variable at LEVEL for the
714 statement PBB, based on the transformed schedule of PBB. OLD_TYPE
715 is the type of the old induction variable for that loop. */
717 static tree
718 compute_type_for_level_1 (poly_bb_p pbb, int level, tree old_type)
720 Value low, up;
721 tree type;
723 value_init (low);
724 value_init (up);
726 compute_bounds_for_level (pbb, level, low, up);
727 type = gcc_type_for_interval (low, up, old_type);
729 value_clear (low);
730 value_clear (up);
731 return type;
734 /* Compute the type for the induction variable at LEVEL for the
735 statement PBB, based on the transformed schedule of PBB. */
737 static tree
738 compute_type_for_level (poly_bb_p pbb, int level)
740 tree oldiv = pbb_to_depth_to_oldiv (pbb, level);
741 tree type = TREE_TYPE (oldiv);
743 if (type && POINTER_TYPE_P (type))
745 #ifdef ENABLE_CHECKING
746 tree ctype = compute_type_for_level_1 (pbb, level, type);
748 /* In the case of a pointer type, check that after the loop
749 transform, the lower and the upper bounds of the type fit the
750 oldiv pointer type. */
751 gcc_assert (TYPE_PRECISION (type) >= TYPE_PRECISION (ctype)
752 && integer_zerop (lower_bound_in_type (ctype, ctype)));
753 #endif
754 return type;
757 return compute_type_for_level_1 (pbb, level, type);
760 /* Walks a CLAST and returns the first statement in the body of a
761 loop. */
763 static struct clast_user_stmt *
764 clast_get_body_of_loop (struct clast_stmt *stmt)
766 if (!stmt
767 || CLAST_STMT_IS_A (stmt, stmt_user))
768 return (struct clast_user_stmt *) stmt;
770 if (CLAST_STMT_IS_A (stmt, stmt_for))
771 return clast_get_body_of_loop (((struct clast_for *) stmt)->body);
773 if (CLAST_STMT_IS_A (stmt, stmt_guard))
774 return clast_get_body_of_loop (((struct clast_guard *) stmt)->then);
776 if (CLAST_STMT_IS_A (stmt, stmt_block))
777 return clast_get_body_of_loop (((struct clast_block *) stmt)->body);
779 gcc_unreachable ();
782 /* Returns the type for the induction variable for the loop translated
783 from STMT_FOR. */
785 static tree
786 gcc_type_for_iv_of_clast_loop (struct clast_for *stmt_for, int level,
787 tree lb_type, tree ub_type)
789 struct clast_stmt *stmt = (struct clast_stmt *) stmt_for;
790 struct clast_user_stmt *body = clast_get_body_of_loop (stmt);
791 CloogStatement *cs = body->statement;
792 poly_bb_p pbb = (poly_bb_p) cloog_statement_usr (cs);
794 return max_precision_type (lb_type, max_precision_type
795 (ub_type, compute_type_for_level (pbb,
796 level - 1)));
799 /* Creates a new LOOP corresponding to Cloog's STMT. Inserts an
800 induction variable for the new LOOP. New LOOP is attached to CFG
801 starting at ENTRY_EDGE. LOOP is inserted into the loop tree and
802 becomes the child loop of the OUTER_LOOP. NEWIVS_INDEX binds
803 CLooG's scattering name to the induction variable created for the
804 loop of STMT. The new induction variable is inserted in the NEWIVS
805 vector. */
807 static struct loop *
808 graphite_create_new_loop (sese region, edge entry_edge,
809 struct clast_for *stmt,
810 loop_p outer, VEC (tree, heap) **newivs,
811 htab_t newivs_index, htab_t params_index, int level)
813 tree lb_type = gcc_type_for_clast_expr (stmt->LB, region, *newivs,
814 newivs_index, params_index);
815 tree ub_type = gcc_type_for_clast_expr (stmt->UB, region, *newivs,
816 newivs_index, params_index);
817 tree type = gcc_type_for_iv_of_clast_loop (stmt, level, lb_type, ub_type);
818 tree lb = clast_to_gcc_expression (type, stmt->LB, region, *newivs,
819 newivs_index, params_index);
820 tree ub = clast_to_gcc_expression (type, stmt->UB, region, *newivs,
821 newivs_index, params_index);
822 tree stride = gmp_cst_to_tree (type, stmt->stride);
823 tree ivvar = create_tmp_var (type, "graphite_IV");
824 tree iv, iv_after_increment;
825 loop_p loop = create_empty_loop_on_edge
826 (entry_edge, lb, stride, ub, ivvar, &iv, &iv_after_increment,
827 outer ? outer : entry_edge->src->loop_father);
829 add_referenced_var (ivvar);
831 save_clast_name_index (newivs_index, stmt->iterator,
832 VEC_length (tree, *newivs));
833 VEC_safe_push (tree, heap, *newivs, iv);
834 return loop;
837 /* Inserts in MAP a tuple (OLD_NAME, NEW_NAME) for the induction
838 variables of the loops around GBB in SESE. */
840 static void
841 build_iv_mapping (htab_t map, sese region,
842 VEC (tree, heap) *newivs, htab_t newivs_index,
843 struct clast_user_stmt *user_stmt,
844 htab_t params_index)
846 struct clast_stmt *t;
847 int index = 0;
848 CloogStatement *cs = user_stmt->statement;
849 poly_bb_p pbb = (poly_bb_p) cloog_statement_usr (cs);
851 for (t = user_stmt->substitutions; t; t = t->next, index++)
853 struct clast_expr *expr = (struct clast_expr *)
854 ((struct clast_assignment *)t)->RHS;
855 tree type = gcc_type_for_clast_expr (expr, region, newivs,
856 newivs_index, params_index);
857 tree old_name = pbb_to_depth_to_oldiv (pbb, index);
858 tree e = clast_to_gcc_expression (type, expr, region, newivs,
859 newivs_index, params_index);
860 set_rename (map, old_name, e);
864 /* Helper function for htab_traverse. */
866 static int
867 copy_renames (void **slot, void *s)
869 struct rename_map_elt_s *entry = (struct rename_map_elt_s *) *slot;
870 htab_t res = (htab_t) s;
871 tree old_name = entry->old_name;
872 tree expr = entry->expr;
873 struct rename_map_elt_s tmp;
874 PTR *x;
876 tmp.old_name = old_name;
877 x = htab_find_slot (res, &tmp, INSERT);
879 if (x && !*x)
880 *x = new_rename_map_elt (old_name, expr);
882 return 1;
885 /* Construct bb_pbb_def with BB and PBB. */
887 static bb_pbb_def *
888 new_bb_pbb_def (basic_block bb, poly_bb_p pbb)
890 bb_pbb_def *bb_pbb_p;
892 bb_pbb_p = XNEW (bb_pbb_def);
893 bb_pbb_p->bb = bb;
894 bb_pbb_p->pbb = pbb;
896 return bb_pbb_p;
899 /* Mark BB with it's relevant PBB via hashing table BB_PBB_MAPPING. */
901 static void
902 mark_bb_with_pbb (poly_bb_p pbb, basic_block bb, htab_t bb_pbb_mapping)
904 bb_pbb_def tmp;
905 PTR *x;
907 tmp.bb = bb;
908 x = htab_find_slot (bb_pbb_mapping, &tmp, INSERT);
910 if (x && !*x)
911 *x = new_bb_pbb_def (bb, pbb);
914 /* Find BB's related poly_bb_p in hash table BB_PBB_MAPPING. */
916 static poly_bb_p
917 find_pbb_via_hash (htab_t bb_pbb_mapping, basic_block bb)
919 bb_pbb_def tmp;
920 PTR *slot;
922 tmp.bb = bb;
923 slot = htab_find_slot (bb_pbb_mapping, &tmp, NO_INSERT);
925 if (slot && *slot)
926 return ((bb_pbb_def *) *slot)->pbb;
928 return NULL;
931 /* Check data dependency in LOOP at scattering level LEVEL.
932 BB_PBB_MAPPING is a basic_block and it's related poly_bb_p
933 mapping. */
935 static bool
936 dependency_in_loop_p (loop_p loop, htab_t bb_pbb_mapping, int level)
938 unsigned i,j;
939 basic_block *bbs = get_loop_body_in_dom_order (loop);
941 for (i = 0; i < loop->num_nodes; i++)
943 poly_bb_p pbb1 = find_pbb_via_hash (bb_pbb_mapping, bbs[i]);
945 if (pbb1 == NULL)
946 continue;
948 for (j = 0; j < loop->num_nodes; j++)
950 poly_bb_p pbb2 = find_pbb_via_hash (bb_pbb_mapping, bbs[j]);
952 if (pbb2 == NULL)
953 continue;
955 if (dependency_between_pbbs_p (pbb1, pbb2, level))
957 free (bbs);
958 return true;
963 free (bbs);
965 return false;
968 static edge
969 translate_clast (sese, loop_p, struct clast_stmt *, edge, htab_t,
970 VEC (tree, heap) **, htab_t, htab_t, int, htab_t);
972 /* Translates a clast user statement STMT to gimple.
974 - REGION is the sese region we used to generate the scop.
975 - NEXT_E is the edge where new generated code should be attached.
976 - CONTEXT_LOOP is the loop in which the generated code will be placed
977 - RENAME_MAP contains a set of tuples of new names associated to
978 the original variables names.
979 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
980 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
981 the sese region. */
982 static edge
983 translate_clast_user (sese region, struct clast_user_stmt *stmt, edge next_e,
984 htab_t rename_map, VEC (tree, heap) **newivs,
985 htab_t newivs_index, htab_t bb_pbb_mapping,
986 htab_t params_index)
988 gimple_bb_p gbb;
989 basic_block new_bb;
990 poly_bb_p pbb = (poly_bb_p) cloog_statement_usr (stmt->statement);
991 gbb = PBB_BLACK_BOX (pbb);
993 if (GBB_BB (gbb) == ENTRY_BLOCK_PTR)
994 return next_e;
996 build_iv_mapping (rename_map, region, *newivs, newivs_index, stmt,
997 params_index);
998 next_e = copy_bb_and_scalar_dependences (GBB_BB (gbb), region,
999 next_e, rename_map);
1000 new_bb = next_e->src;
1001 mark_bb_with_pbb (pbb, new_bb, bb_pbb_mapping);
1002 update_ssa (TODO_update_ssa);
1004 return next_e;
1007 /* Creates a new if region protecting the loop to be executed, if the execution
1008 count is zero (lb > ub). */
1009 static edge
1010 graphite_create_new_loop_guard (sese region, edge entry_edge,
1011 struct clast_for *stmt,
1012 VEC (tree, heap) *newivs,
1013 htab_t newivs_index, htab_t params_index)
1015 tree cond_expr;
1016 edge exit_edge;
1017 tree lb_type = gcc_type_for_clast_expr (stmt->LB, region, newivs,
1018 newivs_index, params_index);
1019 tree ub_type = gcc_type_for_clast_expr (stmt->UB, region, newivs,
1020 newivs_index, params_index);
1021 tree type = max_precision_type (lb_type, ub_type);
1022 tree lb = clast_to_gcc_expression (type, stmt->LB, region, newivs,
1023 newivs_index, params_index);
1024 tree ub = clast_to_gcc_expression (type, stmt->UB, region, newivs,
1025 newivs_index, params_index);
1027 /* XXX: Adding +1 and using LT_EXPR helps with loop latches that have a
1028 loop iteration count of "PARAMETER - 1". For PARAMETER == 0 this becomes
1029 2^{32|64}, and the condition lb <= ub is true, even if we do not want this.
1030 However lb < ub + 1 is false, as expected.
1031 There might be a problem with cases where ub is 2^32. */
1032 tree one;
1033 Value gmp_one;
1034 value_init (gmp_one);
1035 value_set_si (gmp_one, 1);
1036 one = gmp_cst_to_tree (type, gmp_one);
1037 value_clear (gmp_one);
1039 ub = fold_build2 (PLUS_EXPR, type, ub, one);
1040 cond_expr = fold_build2 (LT_EXPR, boolean_type_node, lb, ub);
1042 exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr);
1044 return exit_edge;
1048 /* Create the loop for a clast for statement.
1050 - REGION is the sese region we used to generate the scop.
1051 - NEXT_E is the edge where new generated code should be attached.
1052 - RENAME_MAP contains a set of tuples of new names associated to
1053 the original variables names.
1054 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
1055 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
1056 the sese region. */
1057 static edge
1058 translate_clast_for_loop (sese region, loop_p context_loop,
1059 struct clast_for *stmt, edge next_e,
1060 htab_t rename_map, VEC (tree, heap) **newivs,
1061 htab_t newivs_index, htab_t bb_pbb_mapping,
1062 int level, htab_t params_index)
1064 struct loop *loop = graphite_create_new_loop (region, next_e, stmt,
1065 context_loop, newivs,
1066 newivs_index, params_index,
1067 level);
1068 edge last_e = single_exit (loop);
1069 edge to_body = single_succ_edge (loop->header);
1070 basic_block after = to_body->dest;
1072 /* Create a basic block for loop close phi nodes. */
1073 last_e = single_succ_edge (split_edge (last_e));
1075 /* Translate the body of the loop. */
1076 next_e = translate_clast (region, loop, stmt->body, to_body, rename_map,
1077 newivs, newivs_index, bb_pbb_mapping, level + 1,
1078 params_index);
1079 redirect_edge_succ_nodup (next_e, after);
1080 set_immediate_dominator (CDI_DOMINATORS, next_e->dest, next_e->src);
1082 /* Remove from rename_map all the tuples containing variables
1083 defined in loop's body. */
1084 insert_loop_close_phis (rename_map, loop);
1086 if (flag_loop_parallelize_all
1087 && !dependency_in_loop_p (loop, bb_pbb_mapping,
1088 get_scattering_level (level)))
1089 loop->can_be_parallel = true;
1091 return last_e;
1094 /* Translates a clast for statement STMT to gimple. First a guard is created
1095 protecting the loop, if it is executed zero times. In this guard we create
1096 the real loop structure.
1098 - REGION is the sese region we used to generate the scop.
1099 - NEXT_E is the edge where new generated code should be attached.
1100 - RENAME_MAP contains a set of tuples of new names associated to
1101 the original variables names.
1102 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
1103 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
1104 the sese region. */
1105 static edge
1106 translate_clast_for (sese region, loop_p context_loop, struct clast_for *stmt,
1107 edge next_e, htab_t rename_map, VEC (tree, heap) **newivs,
1108 htab_t newivs_index, htab_t bb_pbb_mapping, int level,
1109 htab_t params_index)
1111 edge last_e = graphite_create_new_loop_guard (region, next_e, stmt, *newivs,
1112 newivs_index, params_index);
1114 edge true_e = get_true_edge_from_guard_bb (next_e->dest);
1115 edge false_e = get_false_edge_from_guard_bb (next_e->dest);
1116 edge exit_true_e = single_succ_edge (true_e->dest);
1117 edge exit_false_e = single_succ_edge (false_e->dest);
1119 htab_t before_guard = htab_create (10, rename_map_elt_info,
1120 eq_rename_map_elts, free);
1121 htab_traverse (rename_map, copy_renames, before_guard);
1123 next_e = translate_clast_for_loop (region, context_loop, stmt, true_e,
1124 rename_map, newivs,
1125 newivs_index, bb_pbb_mapping, level,
1126 params_index);
1128 insert_guard_phis (last_e->src, exit_true_e, exit_false_e,
1129 before_guard, rename_map);
1131 htab_delete (before_guard);
1133 return last_e;
1136 /* Translates a clast guard statement STMT to gimple.
1138 - REGION is the sese region we used to generate the scop.
1139 - NEXT_E is the edge where new generated code should be attached.
1140 - CONTEXT_LOOP is the loop in which the generated code will be placed
1141 - RENAME_MAP contains a set of tuples of new names associated to
1142 the original variables names.
1143 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
1144 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
1145 the sese region. */
1146 static edge
1147 translate_clast_guard (sese region, loop_p context_loop,
1148 struct clast_guard *stmt, edge next_e,
1149 htab_t rename_map, VEC (tree, heap) **newivs,
1150 htab_t newivs_index, htab_t bb_pbb_mapping, int level,
1151 htab_t params_index)
1153 edge last_e = graphite_create_new_guard (region, next_e, stmt, *newivs,
1154 newivs_index, params_index);
1156 edge true_e = get_true_edge_from_guard_bb (next_e->dest);
1157 edge false_e = get_false_edge_from_guard_bb (next_e->dest);
1158 edge exit_true_e = single_succ_edge (true_e->dest);
1159 edge exit_false_e = single_succ_edge (false_e->dest);
1161 htab_t before_guard = htab_create (10, rename_map_elt_info,
1162 eq_rename_map_elts, free);
1163 htab_traverse (rename_map, copy_renames, before_guard);
1165 next_e = translate_clast (region, context_loop, stmt->then, true_e,
1166 rename_map, newivs, newivs_index, bb_pbb_mapping,
1167 level, params_index);
1169 insert_guard_phis (last_e->src, exit_true_e, exit_false_e,
1170 before_guard, rename_map);
1172 htab_delete (before_guard);
1174 return last_e;
1177 /* Translates a CLAST statement STMT to GCC representation in the
1178 context of a SESE.
1180 - NEXT_E is the edge where new generated code should be attached.
1181 - CONTEXT_LOOP is the loop in which the generated code will be placed
1182 - RENAME_MAP contains a set of tuples of new names associated to
1183 the original variables names.
1184 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping. */
1185 static edge
1186 translate_clast (sese region, loop_p context_loop, struct clast_stmt *stmt,
1187 edge next_e, htab_t rename_map, VEC (tree, heap) **newivs,
1188 htab_t newivs_index, htab_t bb_pbb_mapping, int level,
1189 htab_t params_index)
1191 if (!stmt)
1192 return next_e;
1194 if (CLAST_STMT_IS_A (stmt, stmt_root))
1195 ; /* Do nothing. */
1197 else if (CLAST_STMT_IS_A (stmt, stmt_user))
1198 next_e = translate_clast_user (region, (struct clast_user_stmt *) stmt,
1199 next_e, rename_map, newivs, newivs_index,
1200 bb_pbb_mapping, params_index);
1202 else if (CLAST_STMT_IS_A (stmt, stmt_for))
1203 next_e = translate_clast_for (region, context_loop,
1204 (struct clast_for *) stmt, next_e,
1205 rename_map, newivs, newivs_index,
1206 bb_pbb_mapping, level, params_index);
1208 else if (CLAST_STMT_IS_A (stmt, stmt_guard))
1209 next_e = translate_clast_guard (region, context_loop,
1210 (struct clast_guard *) stmt, next_e,
1211 rename_map, newivs, newivs_index,
1212 bb_pbb_mapping, level, params_index);
1214 else if (CLAST_STMT_IS_A (stmt, stmt_block))
1215 next_e = translate_clast (region, context_loop,
1216 ((struct clast_block *) stmt)->body,
1217 next_e, rename_map, newivs, newivs_index,
1218 bb_pbb_mapping, level, params_index);
1219 else
1220 gcc_unreachable();
1222 recompute_all_dominators ();
1223 graphite_verify ();
1225 return translate_clast (region, context_loop, stmt->next, next_e,
1226 rename_map, newivs, newivs_index,
1227 bb_pbb_mapping, level, params_index);
1230 /* Free the SCATTERING domain list. */
1232 static void
1233 free_scattering (CloogDomainList *scattering)
1235 while (scattering)
1237 CloogDomain *dom = cloog_domain (scattering);
1238 CloogDomainList *next = cloog_next_domain (scattering);
1240 cloog_domain_free (dom);
1241 free (scattering);
1242 scattering = next;
1246 /* Initialize Cloog's parameter names from the names used in GIMPLE.
1247 Initialize Cloog's iterator names, using 'graphite_iterator_%d'
1248 from 0 to scop_nb_loops (scop). */
1250 static void
1251 initialize_cloog_names (scop_p scop, CloogProgram *prog)
1253 sese region = SCOP_REGION (scop);
1254 int i;
1255 int nb_iterators = scop_max_loop_depth (scop);
1256 int nb_scattering = cloog_program_nb_scattdims (prog);
1257 int nb_parameters = VEC_length (tree, SESE_PARAMS (region));
1258 char **iterators = XNEWVEC (char *, nb_iterators * 2);
1259 char **scattering = XNEWVEC (char *, nb_scattering);
1260 char **parameters= XNEWVEC (char *, nb_parameters);
1262 cloog_program_set_names (prog, cloog_names_malloc ());
1264 for (i = 0; i < nb_parameters; i++)
1266 tree param = VEC_index (tree, SESE_PARAMS(region), i);
1267 const char *name = get_name (param);
1268 int len;
1270 if (!name)
1271 name = "T";
1273 len = strlen (name);
1274 len += 17;
1275 parameters[i] = XNEWVEC (char, len + 1);
1276 snprintf (parameters[i], len, "%s_%d", name, SSA_NAME_VERSION (param));
1279 cloog_names_set_nb_parameters (cloog_program_names (prog), nb_parameters);
1280 cloog_names_set_parameters (cloog_program_names (prog), parameters);
1282 for (i = 0; i < nb_iterators; i++)
1284 int len = 4 + 16;
1285 iterators[i] = XNEWVEC (char, len);
1286 snprintf (iterators[i], len, "git_%d", i);
1289 cloog_names_set_nb_iterators (cloog_program_names (prog),
1290 nb_iterators);
1291 cloog_names_set_iterators (cloog_program_names (prog),
1292 iterators);
1294 for (i = 0; i < nb_scattering; i++)
1296 int len = 5 + 16;
1297 scattering[i] = XNEWVEC (char, len);
1298 snprintf (scattering[i], len, "scat_%d", i);
1301 cloog_names_set_nb_scattering (cloog_program_names (prog),
1302 nb_scattering);
1303 cloog_names_set_scattering (cloog_program_names (prog),
1304 scattering);
1307 /* Build cloog program for SCoP. */
1309 static void
1310 build_cloog_prog (scop_p scop, CloogProgram *prog)
1312 int i;
1313 int max_nb_loops = scop_max_loop_depth (scop);
1314 poly_bb_p pbb;
1315 CloogLoop *loop_list = NULL;
1316 CloogBlockList *block_list = NULL;
1317 CloogDomainList *scattering = NULL;
1318 int nbs = 2 * max_nb_loops + 1;
1319 int *scaldims;
1321 cloog_program_set_context
1322 (prog, new_Cloog_Domain_from_ppl_Pointset_Powerset (SCOP_CONTEXT (scop)));
1323 nbs = unify_scattering_dimensions (scop);
1324 scaldims = (int *) xmalloc (nbs * (sizeof (int)));
1325 cloog_program_set_nb_scattdims (prog, nbs);
1326 initialize_cloog_names (scop, prog);
1328 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
1330 CloogStatement *stmt;
1331 CloogBlock *block;
1333 /* Dead code elimination: when the domain of a PBB is empty,
1334 don't generate code for the PBB. */
1335 if (ppl_Pointset_Powerset_C_Polyhedron_is_empty (PBB_DOMAIN (pbb)))
1336 continue;
1338 /* Build the new statement and its block. */
1339 stmt = cloog_statement_alloc (pbb_index (pbb));
1340 block = cloog_block_alloc (stmt, 0, NULL, pbb_dim_iter_domain (pbb));
1341 cloog_statement_set_usr (stmt, pbb);
1343 /* Build loop list. */
1345 CloogLoop *new_loop_list = cloog_loop_malloc ();
1346 cloog_loop_set_next (new_loop_list, loop_list);
1347 cloog_loop_set_domain
1348 (new_loop_list,
1349 new_Cloog_Domain_from_ppl_Pointset_Powerset (PBB_DOMAIN (pbb)));
1350 cloog_loop_set_block (new_loop_list, block);
1351 loop_list = new_loop_list;
1354 /* Build block list. */
1356 CloogBlockList *new_block_list = cloog_block_list_malloc ();
1358 cloog_block_list_set_next (new_block_list, block_list);
1359 cloog_block_list_set_block (new_block_list, block);
1360 block_list = new_block_list;
1363 /* Build scattering list. */
1365 /* XXX: Replace with cloog_domain_list_alloc(), when available. */
1366 CloogDomainList *new_scattering
1367 = (CloogDomainList *) xmalloc (sizeof (CloogDomainList));
1368 ppl_Polyhedron_t scat;
1369 CloogDomain *dom;
1371 scat = PBB_TRANSFORMED_SCATTERING (pbb);
1372 dom = new_Cloog_Domain_from_ppl_Polyhedron (scat);
1374 cloog_set_next_domain (new_scattering, scattering);
1375 cloog_set_domain (new_scattering, dom);
1376 scattering = new_scattering;
1380 cloog_program_set_loop (prog, loop_list);
1381 cloog_program_set_blocklist (prog, block_list);
1383 for (i = 0; i < nbs; i++)
1384 scaldims[i] = 0 ;
1386 cloog_program_set_scaldims (prog, scaldims);
1388 /* Extract scalar dimensions to simplify the code generation problem. */
1389 cloog_program_extract_scalars (prog, scattering);
1391 /* Apply scattering. */
1392 cloog_program_scatter (prog, scattering);
1393 free_scattering (scattering);
1395 /* Iterators corresponding to scalar dimensions have to be extracted. */
1396 cloog_names_scalarize (cloog_program_names (prog), nbs,
1397 cloog_program_scaldims (prog));
1399 /* Free blocklist. */
1401 CloogBlockList *next = cloog_program_blocklist (prog);
1403 while (next)
1405 CloogBlockList *toDelete = next;
1406 next = cloog_block_list_next (next);
1407 cloog_block_list_set_next (toDelete, NULL);
1408 cloog_block_list_set_block (toDelete, NULL);
1409 cloog_block_list_free (toDelete);
1411 cloog_program_set_blocklist (prog, NULL);
1415 /* Return the options that will be used in GLOOG. */
1417 static CloogOptions *
1418 set_cloog_options (void)
1420 CloogOptions *options = cloog_options_malloc ();
1422 /* Change cloog output language to C. If we do use FORTRAN instead, cloog
1423 will stop e.g. with "ERROR: unbounded loops not allowed in FORTRAN.", if
1424 we pass an incomplete program to cloog. */
1425 options->language = LANGUAGE_C;
1427 /* Enable complex equality spreading: removes dummy statements
1428 (assignments) in the generated code which repeats the
1429 substitution equations for statements. This is useless for
1430 GLooG. */
1431 options->esp = 1;
1433 /* Enable C pretty-printing mode: normalizes the substitution
1434 equations for statements. */
1435 options->cpp = 1;
1437 /* Allow cloog to build strides with a stride width different to one.
1438 This example has stride = 4:
1440 for (i = 0; i < 20; i += 4)
1441 A */
1442 options->strides = 1;
1444 /* Disable optimizations and make cloog generate source code closer to the
1445 input. This is useful for debugging, but later we want the optimized
1446 code.
1448 XXX: We can not disable optimizations, as loop blocking is not working
1449 without them. */
1450 if (!flag_graphite_cloog_opts)
1452 options->f = -1;
1453 options->l = INT_MAX;
1456 return options;
1459 /* Prints STMT to STDERR. */
1461 void
1462 print_clast_stmt (FILE *file, struct clast_stmt *stmt)
1464 CloogOptions *options = set_cloog_options ();
1466 pprint (file, stmt, 0, options);
1467 cloog_options_free (options);
1470 /* Prints STMT to STDERR. */
1472 void
1473 debug_clast_stmt (struct clast_stmt *stmt)
1475 print_clast_stmt (stderr, stmt);
1478 /* Translate SCOP to a CLooG program and clast. These two
1479 representations should be freed together: a clast cannot be used
1480 without a program. */
1482 cloog_prog_clast
1483 scop_to_clast (scop_p scop)
1485 CloogOptions *options = set_cloog_options ();
1486 cloog_prog_clast pc;
1488 /* Connect new cloog prog generation to graphite. */
1489 pc.prog = cloog_program_malloc ();
1490 build_cloog_prog (scop, pc.prog);
1491 pc.prog = cloog_program_generate (pc.prog, options);
1492 pc.stmt = cloog_clast_create (pc.prog, options);
1494 cloog_options_free (options);
1495 return pc;
1498 /* Prints to FILE the code generated by CLooG for SCOP. */
1500 void
1501 print_generated_program (FILE *file, scop_p scop)
1503 CloogOptions *options = set_cloog_options ();
1504 cloog_prog_clast pc = scop_to_clast (scop);
1506 fprintf (file, " (prog: \n");
1507 cloog_program_print (file, pc.prog);
1508 fprintf (file, " )\n");
1510 fprintf (file, " (clast: \n");
1511 pprint (file, pc.stmt, 0, options);
1512 fprintf (file, " )\n");
1514 cloog_options_free (options);
1515 cloog_clast_free (pc.stmt);
1516 cloog_program_free (pc.prog);
1519 /* Prints to STDERR the code generated by CLooG for SCOP. */
1521 void
1522 debug_generated_program (scop_p scop)
1524 print_generated_program (stderr, scop);
1527 /* Add CLooG names to parameter index. The index is used to translate
1528 back from CLooG names to GCC trees. */
1530 static void
1531 create_params_index (htab_t index_table, CloogProgram *prog) {
1532 CloogNames* names = cloog_program_names (prog);
1533 int nb_parameters = cloog_names_nb_parameters (names);
1534 char **parameters = cloog_names_parameters (names);
1535 int i;
1537 for (i = 0; i < nb_parameters; i++)
1538 save_clast_name_index (index_table, parameters[i], i);
1541 /* GIMPLE Loop Generator: generates loops from STMT in GIMPLE form for
1542 the given SCOP. Return true if code generation succeeded.
1543 BB_PBB_MAPPING is a basic_block and it's related poly_bb_p mapping.
1546 bool
1547 gloog (scop_p scop, VEC (scop_p, heap) *scops, htab_t bb_pbb_mapping)
1549 VEC (tree, heap) *newivs = VEC_alloc (tree, heap, 10);
1550 loop_p context_loop;
1551 sese region = SCOP_REGION (scop);
1552 ifsese if_region = NULL;
1553 htab_t rename_map, newivs_index, params_index;
1554 cloog_prog_clast pc;
1555 int i;
1557 timevar_push (TV_GRAPHITE_CODE_GEN);
1558 gloog_error = false;
1560 pc = scop_to_clast (scop);
1562 if (dump_file && (dump_flags & TDF_DETAILS))
1564 fprintf (dump_file, "\nCLAST generated by CLooG: \n");
1565 print_clast_stmt (dump_file, pc.stmt);
1566 fprintf (dump_file, "\n");
1569 recompute_all_dominators ();
1570 graphite_verify ();
1572 if_region = move_sese_in_condition (region);
1573 sese_insert_phis_for_liveouts (region,
1574 if_region->region->exit->src,
1575 if_region->false_region->exit,
1576 if_region->true_region->exit);
1577 recompute_all_dominators ();
1578 graphite_verify ();
1580 context_loop = SESE_ENTRY (region)->src->loop_father;
1581 rename_map = htab_create (10, rename_map_elt_info, eq_rename_map_elts, free);
1582 newivs_index = htab_create (10, clast_name_index_elt_info,
1583 eq_clast_name_indexes, free);
1584 params_index = htab_create (10, clast_name_index_elt_info,
1585 eq_clast_name_indexes, free);
1587 create_params_index (params_index, pc.prog);
1589 translate_clast (region, context_loop, pc.stmt,
1590 if_region->true_region->entry,
1591 rename_map, &newivs, newivs_index,
1592 bb_pbb_mapping, 1, params_index);
1593 graphite_verify ();
1594 sese_adjust_liveout_phis (region, rename_map,
1595 if_region->region->exit->src,
1596 if_region->false_region->exit,
1597 if_region->true_region->exit);
1598 scev_reset_htab ();
1599 rename_nb_iterations (rename_map);
1601 for (i = 0; VEC_iterate (scop_p, scops, i, scop); i++)
1602 rename_sese_parameters (rename_map, SCOP_REGION (scop));
1604 recompute_all_dominators ();
1605 graphite_verify ();
1607 if (gloog_error)
1608 set_ifsese_condition (if_region, integer_zero_node);
1610 free (if_region->true_region);
1611 free (if_region->region);
1612 free (if_region);
1614 htab_delete (rename_map);
1615 htab_delete (newivs_index);
1616 htab_delete (params_index);
1617 VEC_free (tree, heap, newivs);
1618 cloog_clast_free (pc.stmt);
1619 cloog_program_free (pc.prog);
1620 timevar_pop (TV_GRAPHITE_CODE_GEN);
1622 if (dump_file && (dump_flags & TDF_DETAILS))
1624 loop_p loop;
1625 loop_iterator li;
1626 int num_no_dependency = 0;
1628 FOR_EACH_LOOP (li, loop, 0)
1629 if (loop->can_be_parallel)
1630 num_no_dependency++;
1632 fprintf (dump_file, "\n%d loops carried no dependency.\n",
1633 num_no_dependency);
1636 return !gloog_error;
1639 #endif