1 /* Translation of CLAST (CLooG AST) to Gimple.
2 Copyright (C) 2009, 2010 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)
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/>. */
23 #include "coretypes.h"
28 #include "basic-block.h"
29 #include "diagnostic.h"
30 #include "tree-flow.h"
32 #include "tree-dump.h"
35 #include "tree-chrec.h"
36 #include "tree-data-ref.h"
37 #include "tree-scalar-evolution.h"
38 #include "tree-pass.h"
40 #include "value-prof.h"
41 #include "pointer-set.h"
43 #include "langhooks.h"
47 #include "cloog/cloog.h"
49 #include "graphite-cloog-util.h"
50 #include "graphite-ppl.h"
52 #include "graphite-poly.h"
53 #include "graphite-scop-detection.h"
54 #include "graphite-clast-to-gimple.h"
55 #include "graphite-dependences.h"
56 #include "graphite-cloog-compat.h"
58 /* This flag is set when an error occurred during the translation of
60 static bool gloog_error;
62 /* Verifies properties that GRAPHITE should maintain during translation. */
65 graphite_verify (void)
67 #ifdef ENABLE_CHECKING
68 verify_loop_structure ();
69 verify_dominators (CDI_DOMINATORS);
70 verify_dominators (CDI_POST_DOMINATORS);
71 verify_loop_closed_ssa (true);
75 /* Stores the INDEX in a vector for a given clast NAME. */
77 typedef struct clast_name_index {
80 } *clast_name_index_p;
82 /* Returns a pointer to a new element of type clast_name_index_p built
83 from NAME and INDEX. */
85 static inline clast_name_index_p
86 new_clast_name_index (const char *name, int index)
88 clast_name_index_p res = XNEW (struct clast_name_index);
95 /* For a given clast NAME, returns -1 if it does not correspond to any
96 parameter, or otherwise, returns the index in the PARAMS or
97 SCATTERING_DIMENSIONS vector. */
100 clast_name_to_index (const char *name, htab_t index_table)
102 struct clast_name_index tmp;
106 slot = htab_find_slot (index_table, &tmp, NO_INSERT);
109 return ((struct clast_name_index *) *slot)->index;
114 /* Records in INDEX_TABLE the INDEX for NAME. */
117 save_clast_name_index (htab_t index_table, const char *name, int index)
119 struct clast_name_index tmp;
123 slot = htab_find_slot (index_table, &tmp, INSERT);
130 *slot = new_clast_name_index (name, index);
134 /* Computes a hash function for database element ELT. */
136 static inline hashval_t
137 clast_name_index_elt_info (const void *elt)
139 return htab_hash_pointer (((const struct clast_name_index *) elt)->name);
142 /* Compares database elements E1 and E2. */
145 eq_clast_name_indexes (const void *e1, const void *e2)
147 const struct clast_name_index *elt1 = (const struct clast_name_index *) e1;
148 const struct clast_name_index *elt2 = (const struct clast_name_index *) e2;
150 return (elt1->name == elt2->name);
153 /* For a given scattering dimension, return the new induction variable
157 newivs_to_depth_to_newiv (VEC (tree, heap) *newivs, int depth)
159 return VEC_index (tree, newivs, depth);
164 /* Returns the tree variable from the name NAME that was given in
165 Cloog representation. */
168 clast_name_to_gcc (const char *name, sese region, VEC (tree, heap) *newivs,
169 htab_t newivs_index, htab_t params_index)
172 VEC (tree, heap) *params = SESE_PARAMS (region);
174 if (params && params_index)
176 index = clast_name_to_index (name, params_index);
179 return VEC_index (tree, params, index);
182 gcc_assert (newivs && newivs_index);
183 index = clast_name_to_index (name, newivs_index);
184 gcc_assert (index >= 0);
186 return newivs_to_depth_to_newiv (newivs, index);
189 /* Returns the signed maximal precision type for expressions TYPE1 and TYPE2. */
192 max_signed_precision_type (tree type1, tree type2)
194 int p1 = TYPE_PRECISION (type1);
195 int p2 = TYPE_PRECISION (type2);
200 precision = TYPE_UNSIGNED (type1) ? p1 * 2 : p1;
202 precision = TYPE_UNSIGNED (type2) ? p2 * 2 : p2;
204 type = lang_hooks.types.type_for_size (precision, false);
209 return integer_type_node;
214 /* Returns the maximal precision type for expressions TYPE1 and TYPE2. */
217 max_precision_type (tree type1, tree type2)
219 if (POINTER_TYPE_P (type1))
222 if (POINTER_TYPE_P (type2))
225 if (!TYPE_UNSIGNED (type1)
226 || !TYPE_UNSIGNED (type2))
227 return max_signed_precision_type (type1, type2);
229 return TYPE_PRECISION (type1) > TYPE_PRECISION (type2) ? type1 : type2;
233 clast_to_gcc_expression (tree, struct clast_expr *, sese, VEC (tree, heap) *,
236 /* Converts a Cloog reduction expression R with reduction operation OP
237 to a GCC expression tree of type TYPE. */
240 clast_to_gcc_expression_red (tree type, enum tree_code op,
241 struct clast_reduction *r,
242 sese region, VEC (tree, heap) *newivs,
243 htab_t newivs_index, htab_t params_index)
246 tree res = clast_to_gcc_expression (type, r->elts[0], region, newivs,
247 newivs_index, params_index);
248 tree operand_type = (op == POINTER_PLUS_EXPR) ? sizetype : type;
250 for (i = 1; i < r->n; i++)
252 tree t = clast_to_gcc_expression (operand_type, r->elts[i], region,
253 newivs, newivs_index, params_index);
254 res = fold_build2 (op, type, res, t);
260 /* Converts a Cloog AST expression E back to a GCC expression tree of
264 clast_to_gcc_expression (tree type, struct clast_expr *e,
265 sese region, VEC (tree, heap) *newivs,
266 htab_t newivs_index, htab_t params_index)
272 struct clast_term *t = (struct clast_term *) e;
276 if (mpz_cmp_si (t->val, 1) == 0)
278 tree name = clast_name_to_gcc (t->var, region, newivs,
279 newivs_index, params_index);
281 if (POINTER_TYPE_P (TREE_TYPE (name)) != POINTER_TYPE_P (type))
282 name = fold_convert (sizetype, name);
284 name = fold_convert (type, name);
288 else if (mpz_cmp_si (t->val, -1) == 0)
290 tree name = clast_name_to_gcc (t->var, region, newivs,
291 newivs_index, params_index);
293 if (POINTER_TYPE_P (TREE_TYPE (name)) != POINTER_TYPE_P (type))
294 name = fold_convert (sizetype, name);
296 name = fold_convert (type, name);
298 return fold_build1 (NEGATE_EXPR, type, name);
302 tree name = clast_name_to_gcc (t->var, region, newivs,
303 newivs_index, params_index);
304 tree cst = gmp_cst_to_tree (type, t->val);
306 if (POINTER_TYPE_P (TREE_TYPE (name)) != POINTER_TYPE_P (type))
307 name = fold_convert (sizetype, name);
309 name = fold_convert (type, name);
311 if (!POINTER_TYPE_P (type))
312 return fold_build2 (MULT_EXPR, type, cst, name);
319 return gmp_cst_to_tree (type, t->val);
324 struct clast_reduction *r = (struct clast_reduction *) e;
329 return clast_to_gcc_expression_red
330 (type, POINTER_TYPE_P (type) ? POINTER_PLUS_EXPR : PLUS_EXPR,
331 r, region, newivs, newivs_index, params_index);
334 return clast_to_gcc_expression_red (type, MIN_EXPR, r, region,
335 newivs, newivs_index,
339 return clast_to_gcc_expression_red (type, MAX_EXPR, r, region,
340 newivs, newivs_index,
351 struct clast_binary *b = (struct clast_binary *) e;
352 struct clast_expr *lhs = (struct clast_expr *) b->LHS;
353 tree tl = clast_to_gcc_expression (type, lhs, region, newivs,
354 newivs_index, params_index);
355 tree tr = gmp_cst_to_tree (type, b->RHS);
360 return fold_build2 (FLOOR_DIV_EXPR, type, tl, tr);
363 return fold_build2 (CEIL_DIV_EXPR, type, tl, tr);
366 return fold_build2 (EXACT_DIV_EXPR, type, tl, tr);
369 return fold_build2 (TRUNC_MOD_EXPR, type, tl, tr);
383 /* Return the precision needed to represent the value VAL. */
386 precision_for_value (mpz_t val)
402 while (mpz_cmp (y, x) > 0)
415 /* Return the precision needed to represent the values between LOW and
419 precision_for_interval (mpz_t low, mpz_t up)
424 gcc_assert (mpz_cmp (low, up) <= 0);
427 mpz_sub (diff, up, low);
428 precision = precision_for_value (diff);
434 /* Return a type that could represent the integer value VAL. */
437 gcc_type_for_interval (mpz_t low, mpz_t up)
439 bool unsigned_p = true;
440 int precision, prec_up, prec_int;
442 enum machine_mode mode;
444 gcc_assert (mpz_cmp (low, up) <= 0);
446 if (mpz_sgn (low) < 0)
449 prec_up = precision_for_value (up);
450 prec_int = precision_for_interval (low, up);
451 precision = MAX (prec_up, prec_int);
453 if (precision > BITS_PER_WORD)
456 return integer_type_node;
459 mode = smallest_mode_for_size (precision, MODE_INT);
460 precision = GET_MODE_PRECISION (mode);
461 type = build_nonstandard_integer_type (precision, unsigned_p);
466 return integer_type_node;
472 /* Return a type that could represent the integer value VAL, or
473 otherwise return NULL_TREE. */
476 gcc_type_for_value (mpz_t val)
478 return gcc_type_for_interval (val, val);
481 /* Return the type for the clast_term T used in STMT. */
484 gcc_type_for_clast_term (struct clast_term *t,
485 sese region, VEC (tree, heap) *newivs,
486 htab_t newivs_index, htab_t params_index)
488 gcc_assert (t->expr.type == expr_term);
491 return gcc_type_for_value (t->val);
493 return TREE_TYPE (clast_name_to_gcc (t->var, region, newivs,
494 newivs_index, params_index));
498 gcc_type_for_clast_expr (struct clast_expr *, sese,
499 VEC (tree, heap) *, htab_t, htab_t);
501 /* Return the type for the clast_reduction R used in STMT. */
504 gcc_type_for_clast_red (struct clast_reduction *r, sese region,
505 VEC (tree, heap) *newivs,
506 htab_t newivs_index, htab_t params_index)
509 tree type = NULL_TREE;
512 return gcc_type_for_clast_expr (r->elts[0], region, newivs,
513 newivs_index, params_index);
520 type = gcc_type_for_clast_expr (r->elts[0], region, newivs,
521 newivs_index, params_index);
522 for (i = 1; i < r->n; i++)
523 type = max_precision_type (type, gcc_type_for_clast_expr
524 (r->elts[i], region, newivs,
525 newivs_index, params_index));
537 /* Return the type for the clast_binary B used in STMT. */
540 gcc_type_for_clast_bin (struct clast_binary *b,
541 sese region, VEC (tree, heap) *newivs,
542 htab_t newivs_index, htab_t params_index)
544 tree l = gcc_type_for_clast_expr ((struct clast_expr *) b->LHS, region,
545 newivs, newivs_index, params_index);
546 tree r = gcc_type_for_value (b->RHS);
547 return max_signed_precision_type (l, r);
550 /* Returns the type for the CLAST expression E when used in statement
554 gcc_type_for_clast_expr (struct clast_expr *e,
555 sese region, VEC (tree, heap) *newivs,
556 htab_t newivs_index, htab_t params_index)
561 return gcc_type_for_clast_term ((struct clast_term *) e, region,
562 newivs, newivs_index, params_index);
565 return gcc_type_for_clast_red ((struct clast_reduction *) e, region,
566 newivs, newivs_index, params_index);
569 return gcc_type_for_clast_bin ((struct clast_binary *) e, region,
570 newivs, newivs_index, params_index);
579 /* Returns the type for the equation CLEQ. */
582 gcc_type_for_clast_eq (struct clast_equation *cleq,
583 sese region, VEC (tree, heap) *newivs,
584 htab_t newivs_index, htab_t params_index)
586 tree l = gcc_type_for_clast_expr (cleq->LHS, region, newivs,
587 newivs_index, params_index);
588 tree r = gcc_type_for_clast_expr (cleq->RHS, region, newivs,
589 newivs_index, params_index);
590 return max_precision_type (l, r);
593 /* Translates a clast equation CLEQ to a tree. */
596 graphite_translate_clast_equation (sese region,
597 struct clast_equation *cleq,
598 VEC (tree, heap) *newivs,
599 htab_t newivs_index, htab_t params_index)
602 tree type = gcc_type_for_clast_eq (cleq, region, newivs, newivs_index,
604 tree lhs = clast_to_gcc_expression (type, cleq->LHS, region, newivs,
605 newivs_index, params_index);
606 tree rhs = clast_to_gcc_expression (type, cleq->RHS, region, newivs,
607 newivs_index, params_index);
612 else if (cleq->sign > 0)
618 return fold_build2 (comp, boolean_type_node, lhs, rhs);
621 /* Creates the test for the condition in STMT. */
624 graphite_create_guard_cond_expr (sese region, struct clast_guard *stmt,
625 VEC (tree, heap) *newivs,
626 htab_t newivs_index, htab_t params_index)
631 for (i = 0; i < stmt->n; i++)
633 tree eq = graphite_translate_clast_equation (region, &stmt->eq[i],
634 newivs, newivs_index,
638 cond = fold_build2 (TRUTH_AND_EXPR, TREE_TYPE (eq), cond, eq);
646 /* Creates a new if region corresponding to Cloog's guard. */
649 graphite_create_new_guard (sese region, edge entry_edge,
650 struct clast_guard *stmt,
651 VEC (tree, heap) *newivs,
652 htab_t newivs_index, htab_t params_index)
654 tree cond_expr = graphite_create_guard_cond_expr (region, stmt, newivs,
655 newivs_index, params_index);
656 edge exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr);
660 /* Compute the lower bound LOW and upper bound UP for the induction
661 variable at LEVEL for the statement PBB, based on the transformed
662 scattering of PBB: T|I|G|Cst, with T the scattering transform, I
663 the iteration domain, and G the context parameters. */
666 compute_bounds_for_level (poly_bb_p pbb, int level, mpz_t low, mpz_t up)
668 ppl_Pointset_Powerset_C_Polyhedron_t ps;
669 ppl_Linear_Expression_t le;
671 combine_context_id_scat (&ps, pbb, false);
673 /* Prepare the linear expression corresponding to the level that we
674 want to maximize/minimize. */
676 ppl_dimension_type dim = pbb_nb_scattering_transform (pbb)
677 + pbb_dim_iter_domain (pbb) + pbb_nb_params (pbb);
679 ppl_new_Linear_Expression_with_dimension (&le, dim);
680 ppl_set_coef (le, 2 * level + 1, 1);
683 ppl_max_for_le_pointset (ps, le, up);
684 ppl_min_for_le_pointset (ps, le, low);
687 /* Compute the type for the induction variable at LEVEL for the
688 statement PBB, based on the transformed schedule of PBB. */
691 compute_type_for_level (poly_bb_p pbb, int level)
699 compute_bounds_for_level (pbb, level, low, up);
700 type = gcc_type_for_interval (low, up);
707 /* Walks a CLAST and returns the first statement in the body of a
710 static struct clast_user_stmt *
711 clast_get_body_of_loop (struct clast_stmt *stmt)
714 || CLAST_STMT_IS_A (stmt, stmt_user))
715 return (struct clast_user_stmt *) stmt;
717 if (CLAST_STMT_IS_A (stmt, stmt_for))
718 return clast_get_body_of_loop (((struct clast_for *) stmt)->body);
720 if (CLAST_STMT_IS_A (stmt, stmt_guard))
721 return clast_get_body_of_loop (((struct clast_guard *) stmt)->then);
723 if (CLAST_STMT_IS_A (stmt, stmt_block))
724 return clast_get_body_of_loop (((struct clast_block *) stmt)->body);
729 /* Returns the type for the induction variable for the loop translated
733 gcc_type_for_iv_of_clast_loop (struct clast_for *stmt_for, int level,
734 tree lb_type, tree ub_type)
736 struct clast_stmt *stmt = (struct clast_stmt *) stmt_for;
737 struct clast_user_stmt *body = clast_get_body_of_loop (stmt);
738 CloogStatement *cs = body->statement;
739 poly_bb_p pbb = (poly_bb_p) cloog_statement_usr (cs);
741 return max_signed_precision_type (lb_type, max_precision_type
742 (ub_type, compute_type_for_level
746 /* Creates a new LOOP corresponding to Cloog's STMT. Inserts an
747 induction variable for the new LOOP. New LOOP is attached to CFG
748 starting at ENTRY_EDGE. LOOP is inserted into the loop tree and
749 becomes the child loop of the OUTER_LOOP. NEWIVS_INDEX binds
750 CLooG's scattering name to the induction variable created for the
751 loop of STMT. The new induction variable is inserted in the NEWIVS
755 graphite_create_new_loop (sese region, edge entry_edge,
756 struct clast_for *stmt,
757 loop_p outer, VEC (tree, heap) **newivs,
758 htab_t newivs_index, htab_t params_index, int level)
760 tree lb_type = gcc_type_for_clast_expr (stmt->LB, region, *newivs,
761 newivs_index, params_index);
762 tree ub_type = gcc_type_for_clast_expr (stmt->UB, region, *newivs,
763 newivs_index, params_index);
764 tree type = gcc_type_for_iv_of_clast_loop (stmt, level, lb_type, ub_type);
765 tree lb = clast_to_gcc_expression (type, stmt->LB, region, *newivs,
766 newivs_index, params_index);
767 tree ub = clast_to_gcc_expression (type, stmt->UB, region, *newivs,
768 newivs_index, params_index);
769 tree stride = gmp_cst_to_tree (type, stmt->stride);
770 tree ivvar = create_tmp_var (type, "graphite_IV");
771 tree iv, iv_after_increment;
772 loop_p loop = create_empty_loop_on_edge
773 (entry_edge, lb, stride, ub, ivvar, &iv, &iv_after_increment,
774 outer ? outer : entry_edge->src->loop_father);
776 add_referenced_var (ivvar);
778 save_clast_name_index (newivs_index, stmt->iterator,
779 VEC_length (tree, *newivs));
780 VEC_safe_push (tree, heap, *newivs, iv);
784 /* Inserts in iv_map a tuple (OLD_LOOP->num, NEW_NAME) for the
785 induction variables of the loops around GBB in SESE. */
788 build_iv_mapping (VEC (tree, heap) *iv_map, sese region,
789 VEC (tree, heap) *newivs, htab_t newivs_index,
790 struct clast_user_stmt *user_stmt,
793 struct clast_stmt *t;
795 CloogStatement *cs = user_stmt->statement;
796 poly_bb_p pbb = (poly_bb_p) cloog_statement_usr (cs);
797 gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
799 for (t = user_stmt->substitutions; t; t = t->next, depth++)
801 struct clast_expr *expr = (struct clast_expr *)
802 ((struct clast_assignment *)t)->RHS;
803 tree type = gcc_type_for_clast_expr (expr, region, newivs,
804 newivs_index, params_index);
805 tree new_name = clast_to_gcc_expression (type, expr, region, newivs,
806 newivs_index, params_index);
807 loop_p old_loop = gbb_loop_at_index (gbb, region, depth);
809 VEC_replace (tree, iv_map, old_loop->num, new_name);
813 /* Construct bb_pbb_def with BB and PBB. */
816 new_bb_pbb_def (basic_block bb, poly_bb_p pbb)
818 bb_pbb_def *bb_pbb_p;
820 bb_pbb_p = XNEW (bb_pbb_def);
827 /* Mark BB with it's relevant PBB via hashing table BB_PBB_MAPPING. */
830 mark_bb_with_pbb (poly_bb_p pbb, basic_block bb, htab_t bb_pbb_mapping)
836 x = htab_find_slot (bb_pbb_mapping, &tmp, INSERT);
839 *x = new_bb_pbb_def (bb, pbb);
842 /* Find BB's related poly_bb_p in hash table BB_PBB_MAPPING. */
845 find_pbb_via_hash (htab_t bb_pbb_mapping, basic_block bb)
851 slot = htab_find_slot (bb_pbb_mapping, &tmp, NO_INSERT);
854 return ((bb_pbb_def *) *slot)->pbb;
859 /* Check data dependency in LOOP at scattering level LEVEL.
860 BB_PBB_MAPPING is a basic_block and it's related poly_bb_p
864 dependency_in_loop_p (loop_p loop, htab_t bb_pbb_mapping, int level)
867 basic_block *bbs = get_loop_body_in_dom_order (loop);
869 for (i = 0; i < loop->num_nodes; i++)
871 poly_bb_p pbb1 = find_pbb_via_hash (bb_pbb_mapping, bbs[i]);
876 for (j = 0; j < loop->num_nodes; j++)
878 poly_bb_p pbb2 = find_pbb_via_hash (bb_pbb_mapping, bbs[j]);
883 if (dependency_between_pbbs_p (pbb1, pbb2, level))
896 /* Translates a clast user statement STMT to gimple.
898 - REGION is the sese region we used to generate the scop.
899 - NEXT_E is the edge where new generated code should be attached.
900 - CONTEXT_LOOP is the loop in which the generated code will be placed
901 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
902 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
905 translate_clast_user (sese region, struct clast_user_stmt *stmt, edge next_e,
906 VEC (tree, heap) **newivs,
907 htab_t newivs_index, htab_t bb_pbb_mapping,
912 poly_bb_p pbb = (poly_bb_p) cloog_statement_usr (stmt->statement);
913 gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
914 VEC (tree, heap) *iv_map;
916 if (GBB_BB (gbb) == ENTRY_BLOCK_PTR)
919 nb_loops = number_of_loops ();
920 iv_map = VEC_alloc (tree, heap, nb_loops);
921 for (i = 0; i < nb_loops; i++)
922 VEC_quick_push (tree, iv_map, NULL_TREE);
924 build_iv_mapping (iv_map, region, *newivs, newivs_index, stmt, params_index);
925 next_e = copy_bb_and_scalar_dependences (GBB_BB (gbb), region,
927 VEC_free (tree, heap, iv_map);
929 new_bb = next_e->src;
930 mark_bb_with_pbb (pbb, new_bb, bb_pbb_mapping);
931 update_ssa (TODO_update_ssa);
936 /* Creates a new if region protecting the loop to be executed, if the execution
937 count is zero (lb > ub). */
940 graphite_create_new_loop_guard (sese region, edge entry_edge,
941 struct clast_for *stmt,
942 VEC (tree, heap) *newivs,
943 htab_t newivs_index, htab_t params_index)
947 tree lb_type = gcc_type_for_clast_expr (stmt->LB, region, newivs,
948 newivs_index, params_index);
949 tree ub_type = gcc_type_for_clast_expr (stmt->UB, region, newivs,
950 newivs_index, params_index);
951 tree type = max_precision_type (lb_type, ub_type);
952 tree lb = clast_to_gcc_expression (type, stmt->LB, region, newivs,
953 newivs_index, params_index);
954 tree ub = clast_to_gcc_expression (type, stmt->UB, region, newivs,
955 newivs_index, params_index);
956 tree one = POINTER_TYPE_P (type) ? size_one_node
957 : fold_convert (type, integer_one_node);
958 /* Adding +1 and using LT_EXPR helps with loop latches that have a
959 loop iteration count of "PARAMETER - 1". For PARAMETER == 0 this becomes
960 2^{32|64}, and the condition lb <= ub is true, even if we do not want this.
961 However lb < ub + 1 is false, as expected. */
962 tree ub_one = fold_build2 (POINTER_TYPE_P (type) ? POINTER_PLUS_EXPR
963 : PLUS_EXPR, type, ub, one);
965 /* When ub + 1 wraps around, use lb <= ub. */
966 if (integer_zerop (ub_one))
967 cond_expr = fold_build2 (LE_EXPR, boolean_type_node, lb, ub);
969 cond_expr = fold_build2 (LT_EXPR, boolean_type_node, lb, ub_one);
971 exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr);
977 translate_clast (sese, loop_p, struct clast_stmt *, edge,
978 VEC (tree, heap) **, htab_t, htab_t, int, htab_t);
980 /* Create the loop for a clast for statement.
982 - REGION is the sese region we used to generate the scop.
983 - NEXT_E is the edge where new generated code should be attached.
984 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
985 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
988 translate_clast_for_loop (sese region, loop_p context_loop,
989 struct clast_for *stmt, edge next_e,
990 VEC (tree, heap) **newivs,
991 htab_t newivs_index, htab_t bb_pbb_mapping,
992 int level, htab_t params_index)
994 struct loop *loop = graphite_create_new_loop (region, next_e, stmt,
995 context_loop, newivs,
996 newivs_index, params_index,
998 edge last_e = single_exit (loop);
999 edge to_body = single_succ_edge (loop->header);
1000 basic_block after = to_body->dest;
1002 /* Create a basic block for loop close phi nodes. */
1003 last_e = single_succ_edge (split_edge (last_e));
1005 /* Translate the body of the loop. */
1006 next_e = translate_clast (region, loop, stmt->body, to_body,
1007 newivs, newivs_index, bb_pbb_mapping, level + 1,
1009 redirect_edge_succ_nodup (next_e, after);
1010 set_immediate_dominator (CDI_DOMINATORS, next_e->dest, next_e->src);
1012 if (flag_loop_parallelize_all
1013 && !dependency_in_loop_p (loop, bb_pbb_mapping,
1014 get_scattering_level (level)))
1015 loop->can_be_parallel = true;
1020 /* Translates a clast for statement STMT to gimple. First a guard is created
1021 protecting the loop, if it is executed zero times. In this guard we create
1022 the real loop structure.
1024 - REGION is the sese region we used to generate the scop.
1025 - NEXT_E is the edge where new generated code should be attached.
1026 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
1027 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
1030 translate_clast_for (sese region, loop_p context_loop, struct clast_for *stmt,
1031 edge next_e, VEC (tree, heap) **newivs,
1032 htab_t newivs_index, htab_t bb_pbb_mapping, int level,
1033 htab_t params_index)
1035 edge last_e = graphite_create_new_loop_guard (region, next_e, stmt, *newivs,
1036 newivs_index, params_index);
1037 edge true_e = get_true_edge_from_guard_bb (next_e->dest);
1039 translate_clast_for_loop (region, context_loop, stmt, true_e, newivs,
1040 newivs_index, bb_pbb_mapping, level,
1045 /* Translates a clast guard statement STMT to gimple.
1047 - REGION is the sese region we used to generate the scop.
1048 - NEXT_E is the edge where new generated code should be attached.
1049 - CONTEXT_LOOP is the loop in which the generated code will be placed
1050 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
1051 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
1054 translate_clast_guard (sese region, loop_p context_loop,
1055 struct clast_guard *stmt, edge next_e,
1056 VEC (tree, heap) **newivs,
1057 htab_t newivs_index, htab_t bb_pbb_mapping, int level,
1058 htab_t params_index)
1060 edge last_e = graphite_create_new_guard (region, next_e, stmt, *newivs,
1061 newivs_index, params_index);
1062 edge true_e = get_true_edge_from_guard_bb (next_e->dest);
1064 translate_clast (region, context_loop, stmt->then, true_e,
1065 newivs, newivs_index, bb_pbb_mapping,
1066 level, params_index);
1070 /* Translates a CLAST statement STMT to GCC representation in the
1073 - NEXT_E is the edge where new generated code should be attached.
1074 - CONTEXT_LOOP is the loop in which the generated code will be placed
1075 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping. */
1077 translate_clast (sese region, loop_p context_loop, struct clast_stmt *stmt,
1078 edge next_e, VEC (tree, heap) **newivs,
1079 htab_t newivs_index, htab_t bb_pbb_mapping, int level,
1080 htab_t params_index)
1085 if (CLAST_STMT_IS_A (stmt, stmt_root))
1088 else if (CLAST_STMT_IS_A (stmt, stmt_user))
1089 next_e = translate_clast_user (region, (struct clast_user_stmt *) stmt,
1090 next_e, newivs, newivs_index,
1091 bb_pbb_mapping, params_index);
1093 else if (CLAST_STMT_IS_A (stmt, stmt_for))
1094 next_e = translate_clast_for (region, context_loop,
1095 (struct clast_for *) stmt, next_e,
1096 newivs, newivs_index,
1097 bb_pbb_mapping, level, params_index);
1099 else if (CLAST_STMT_IS_A (stmt, stmt_guard))
1100 next_e = translate_clast_guard (region, context_loop,
1101 (struct clast_guard *) stmt, next_e,
1102 newivs, newivs_index,
1103 bb_pbb_mapping, level, params_index);
1105 else if (CLAST_STMT_IS_A (stmt, stmt_block))
1106 next_e = translate_clast (region, context_loop,
1107 ((struct clast_block *) stmt)->body,
1108 next_e, newivs, newivs_index,
1109 bb_pbb_mapping, level, params_index);
1113 recompute_all_dominators ();
1116 return translate_clast (region, context_loop, stmt->next, next_e,
1117 newivs, newivs_index,
1118 bb_pbb_mapping, level, params_index);
1121 /* Free the SCATTERING domain list. */
1124 free_scattering (CloogDomainList *scattering)
1128 CloogDomain *dom = cloog_domain (scattering);
1129 CloogDomainList *next = cloog_next_domain (scattering);
1131 cloog_domain_free (dom);
1137 /* Initialize Cloog's parameter names from the names used in GIMPLE.
1138 Initialize Cloog's iterator names, using 'graphite_iterator_%d'
1139 from 0 to scop_nb_loops (scop). */
1142 initialize_cloog_names (scop_p scop, CloogProgram *prog)
1144 sese region = SCOP_REGION (scop);
1146 int nb_iterators = scop_max_loop_depth (scop);
1147 int nb_scattering = cloog_program_nb_scattdims (prog);
1148 int nb_parameters = VEC_length (tree, SESE_PARAMS (region));
1149 char **iterators = XNEWVEC (char *, nb_iterators * 2);
1150 char **scattering = XNEWVEC (char *, nb_scattering);
1151 char **parameters= XNEWVEC (char *, nb_parameters);
1153 cloog_program_set_names (prog, cloog_names_malloc ());
1155 for (i = 0; i < nb_parameters; i++)
1157 tree param = VEC_index (tree, SESE_PARAMS(region), i);
1158 const char *name = get_name (param);
1164 len = strlen (name);
1166 parameters[i] = XNEWVEC (char, len + 1);
1167 snprintf (parameters[i], len, "%s_%d", name, SSA_NAME_VERSION (param));
1170 cloog_names_set_nb_parameters (cloog_program_names (prog), nb_parameters);
1171 cloog_names_set_parameters (cloog_program_names (prog), parameters);
1173 for (i = 0; i < nb_iterators; i++)
1176 iterators[i] = XNEWVEC (char, len);
1177 snprintf (iterators[i], len, "git_%d", i);
1180 cloog_names_set_nb_iterators (cloog_program_names (prog),
1182 cloog_names_set_iterators (cloog_program_names (prog),
1185 for (i = 0; i < nb_scattering; i++)
1188 scattering[i] = XNEWVEC (char, len);
1189 snprintf (scattering[i], len, "scat_%d", i);
1192 cloog_names_set_nb_scattering (cloog_program_names (prog),
1194 cloog_names_set_scattering (cloog_program_names (prog),
1198 /* Build cloog program for SCoP. */
1201 build_cloog_prog (scop_p scop, CloogProgram *prog)
1204 int max_nb_loops = scop_max_loop_depth (scop);
1206 CloogLoop *loop_list = NULL;
1207 CloogBlockList *block_list = NULL;
1208 CloogDomainList *scattering = NULL;
1209 int nbs = 2 * max_nb_loops + 1;
1212 cloog_program_set_context
1213 (prog, new_Cloog_Domain_from_ppl_Pointset_Powerset (SCOP_CONTEXT (scop)));
1214 nbs = unify_scattering_dimensions (scop);
1215 scaldims = (int *) xmalloc (nbs * (sizeof (int)));
1216 cloog_program_set_nb_scattdims (prog, nbs);
1217 initialize_cloog_names (scop, prog);
1219 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
1221 CloogStatement *stmt;
1224 /* Dead code elimination: when the domain of a PBB is empty,
1225 don't generate code for the PBB. */
1226 if (ppl_Pointset_Powerset_C_Polyhedron_is_empty (PBB_DOMAIN (pbb)))
1229 /* Build the new statement and its block. */
1230 stmt = cloog_statement_alloc (pbb_index (pbb));
1231 block = cloog_block_alloc (stmt, 0, NULL, pbb_dim_iter_domain (pbb));
1232 cloog_statement_set_usr (stmt, pbb);
1234 /* Build loop list. */
1236 CloogLoop *new_loop_list = cloog_loop_malloc ();
1237 cloog_loop_set_next (new_loop_list, loop_list);
1238 cloog_loop_set_domain
1240 new_Cloog_Domain_from_ppl_Pointset_Powerset (PBB_DOMAIN (pbb)));
1241 cloog_loop_set_block (new_loop_list, block);
1242 loop_list = new_loop_list;
1245 /* Build block list. */
1247 CloogBlockList *new_block_list = cloog_block_list_malloc ();
1249 cloog_block_list_set_next (new_block_list, block_list);
1250 cloog_block_list_set_block (new_block_list, block);
1251 block_list = new_block_list;
1254 /* Build scattering list. */
1256 /* XXX: Replace with cloog_domain_list_alloc(), when available. */
1257 CloogDomainList *new_scattering
1258 = (CloogDomainList *) xmalloc (sizeof (CloogDomainList));
1259 ppl_Polyhedron_t scat;
1262 scat = PBB_TRANSFORMED_SCATTERING (pbb);
1263 dom = new_Cloog_Domain_from_ppl_Polyhedron (scat);
1265 cloog_set_next_domain (new_scattering, scattering);
1266 cloog_set_domain (new_scattering, dom);
1267 scattering = new_scattering;
1271 cloog_program_set_loop (prog, loop_list);
1272 cloog_program_set_blocklist (prog, block_list);
1274 for (i = 0; i < nbs; i++)
1277 cloog_program_set_scaldims (prog, scaldims);
1279 /* Extract scalar dimensions to simplify the code generation problem. */
1280 cloog_program_extract_scalars (prog, scattering);
1282 /* Apply scattering. */
1283 cloog_program_scatter (prog, scattering);
1284 free_scattering (scattering);
1286 /* Iterators corresponding to scalar dimensions have to be extracted. */
1287 cloog_names_scalarize (cloog_program_names (prog), nbs,
1288 cloog_program_scaldims (prog));
1290 /* Free blocklist. */
1292 CloogBlockList *next = cloog_program_blocklist (prog);
1296 CloogBlockList *toDelete = next;
1297 next = cloog_block_list_next (next);
1298 cloog_block_list_set_next (toDelete, NULL);
1299 cloog_block_list_set_block (toDelete, NULL);
1300 cloog_block_list_free (toDelete);
1302 cloog_program_set_blocklist (prog, NULL);
1306 /* Return the options that will be used in GLOOG. */
1308 static CloogOptions *
1309 set_cloog_options (void)
1311 CloogOptions *options = cloog_options_malloc ();
1313 /* Change cloog output language to C. If we do use FORTRAN instead, cloog
1314 will stop e.g. with "ERROR: unbounded loops not allowed in FORTRAN.", if
1315 we pass an incomplete program to cloog. */
1316 options->language = LANGUAGE_C;
1318 /* Enable complex equality spreading: removes dummy statements
1319 (assignments) in the generated code which repeats the
1320 substitution equations for statements. This is useless for
1324 /* Enable C pretty-printing mode: normalizes the substitution
1325 equations for statements. */
1328 /* Allow cloog to build strides with a stride width different to one.
1329 This example has stride = 4:
1331 for (i = 0; i < 20; i += 4)
1333 options->strides = 1;
1335 /* Disable optimizations and make cloog generate source code closer to the
1336 input. This is useful for debugging, but later we want the optimized
1339 XXX: We can not disable optimizations, as loop blocking is not working
1344 options->l = INT_MAX;
1350 /* Prints STMT to STDERR. */
1353 print_clast_stmt (FILE *file, struct clast_stmt *stmt)
1355 CloogOptions *options = set_cloog_options ();
1357 pprint (file, stmt, 0, options);
1358 cloog_options_free (options);
1361 /* Prints STMT to STDERR. */
1364 debug_clast_stmt (struct clast_stmt *stmt)
1366 print_clast_stmt (stderr, stmt);
1369 /* Translate SCOP to a CLooG program and clast. These two
1370 representations should be freed together: a clast cannot be used
1371 without a program. */
1374 scop_to_clast (scop_p scop)
1376 CloogOptions *options = set_cloog_options ();
1377 cloog_prog_clast pc;
1379 /* Connect new cloog prog generation to graphite. */
1380 pc.prog = cloog_program_malloc ();
1381 build_cloog_prog (scop, pc.prog);
1382 pc.prog = cloog_program_generate (pc.prog, options);
1383 pc.stmt = cloog_clast_create (pc.prog, options);
1385 cloog_options_free (options);
1389 /* Prints to FILE the code generated by CLooG for SCOP. */
1392 print_generated_program (FILE *file, scop_p scop)
1394 CloogOptions *options = set_cloog_options ();
1395 cloog_prog_clast pc = scop_to_clast (scop);
1397 fprintf (file, " (prog: \n");
1398 cloog_program_print (file, pc.prog);
1399 fprintf (file, " )\n");
1401 fprintf (file, " (clast: \n");
1402 pprint (file, pc.stmt, 0, options);
1403 fprintf (file, " )\n");
1405 cloog_options_free (options);
1406 cloog_clast_free (pc.stmt);
1407 cloog_program_free (pc.prog);
1410 /* Prints to STDERR the code generated by CLooG for SCOP. */
1413 debug_generated_program (scop_p scop)
1415 print_generated_program (stderr, scop);
1418 /* Add CLooG names to parameter index. The index is used to translate
1419 back from CLooG names to GCC trees. */
1422 create_params_index (htab_t index_table, CloogProgram *prog) {
1423 CloogNames* names = cloog_program_names (prog);
1424 int nb_parameters = cloog_names_nb_parameters (names);
1425 char **parameters = cloog_names_parameters (names);
1428 for (i = 0; i < nb_parameters; i++)
1429 save_clast_name_index (index_table, parameters[i], i);
1432 /* GIMPLE Loop Generator: generates loops from STMT in GIMPLE form for
1433 the given SCOP. Return true if code generation succeeded.
1434 BB_PBB_MAPPING is a basic_block and it's related poly_bb_p mapping.
1438 gloog (scop_p scop, htab_t bb_pbb_mapping)
1440 VEC (tree, heap) *newivs = VEC_alloc (tree, heap, 10);
1441 loop_p context_loop;
1442 sese region = SCOP_REGION (scop);
1443 ifsese if_region = NULL;
1444 htab_t newivs_index, params_index;
1445 cloog_prog_clast pc;
1447 timevar_push (TV_GRAPHITE_CODE_GEN);
1448 gloog_error = false;
1450 pc = scop_to_clast (scop);
1452 if (dump_file && (dump_flags & TDF_DETAILS))
1454 fprintf (dump_file, "\nCLAST generated by CLooG: \n");
1455 print_clast_stmt (dump_file, pc.stmt);
1456 fprintf (dump_file, "\n");
1459 recompute_all_dominators ();
1462 if_region = move_sese_in_condition (region);
1463 sese_insert_phis_for_liveouts (region,
1464 if_region->region->exit->src,
1465 if_region->false_region->exit,
1466 if_region->true_region->exit);
1467 recompute_all_dominators ();
1470 context_loop = SESE_ENTRY (region)->src->loop_father;
1471 newivs_index = htab_create (10, clast_name_index_elt_info,
1472 eq_clast_name_indexes, free);
1473 params_index = htab_create (10, clast_name_index_elt_info,
1474 eq_clast_name_indexes, free);
1476 create_params_index (params_index, pc.prog);
1478 translate_clast (region, context_loop, pc.stmt,
1479 if_region->true_region->entry,
1480 &newivs, newivs_index,
1481 bb_pbb_mapping, 1, params_index);
1484 recompute_all_dominators ();
1488 set_ifsese_condition (if_region, integer_zero_node);
1490 free (if_region->true_region);
1491 free (if_region->region);
1494 htab_delete (newivs_index);
1495 htab_delete (params_index);
1496 VEC_free (tree, heap, newivs);
1497 cloog_clast_free (pc.stmt);
1498 cloog_program_free (pc.prog);
1499 timevar_pop (TV_GRAPHITE_CODE_GEN);
1501 if (dump_file && (dump_flags & TDF_DETAILS))
1505 int num_no_dependency = 0;
1507 FOR_EACH_LOOP (li, loop, 0)
1508 if (loop->can_be_parallel)
1509 num_no_dependency++;
1511 fprintf (dump_file, "\n%d loops carried no dependency.\n",
1515 return !gloog_error;