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-ppl.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
58 static bool gloog_error;
60 /* Verifies properties that GRAPHITE should maintain during translation. */
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_loop_closed_ssa (true);
73 /* Stores the INDEX in a vector for a given clast NAME. */
75 typedef struct clast_name_index {
78 } *clast_name_index_p;
80 /* Returns a pointer to a new element of type clast_name_index_p built
81 from NAME and INDEX. */
83 static inline clast_name_index_p
84 new_clast_name_index (const char *name, int index)
86 clast_name_index_p res = XNEW (struct clast_name_index);
93 /* For a given clast NAME, returns -1 if it does not correspond to any
94 parameter, or otherwise, returns the index in the PARAMS or
95 SCATTERING_DIMENSIONS vector. */
98 clast_name_to_index (const char *name, htab_t index_table)
100 struct clast_name_index tmp;
104 slot = htab_find_slot (index_table, &tmp, NO_INSERT);
107 return ((struct clast_name_index *) *slot)->index;
112 /* Records in INDEX_TABLE the INDEX for NAME. */
115 save_clast_name_index (htab_t index_table, const char *name, int index)
117 struct clast_name_index tmp;
121 slot = htab_find_slot (index_table, &tmp, INSERT);
128 *slot = new_clast_name_index (name, index);
132 /* Print to stderr the element ELT. */
135 debug_clast_name_index (clast_name_index_p elt)
137 fprintf (stderr, "(index = %d, name = %s)\n", elt->index, elt->name);
140 /* Helper function for debug_rename_map. */
143 debug_clast_name_indexes_1 (void **slot, void *s ATTRIBUTE_UNUSED)
145 struct clast_name_index *entry = (struct clast_name_index *) *slot;
146 debug_clast_name_index (entry);
150 /* Print to stderr all the elements of MAP. */
153 debug_clast_name_indexes (htab_t map)
155 htab_traverse (map, debug_clast_name_indexes_1, NULL);
158 /* Computes a hash function for database element ELT. */
160 static inline hashval_t
161 clast_name_index_elt_info (const void *elt)
163 return htab_hash_pointer (((const struct clast_name_index *) elt)->name);
166 /* Compares database elements E1 and E2. */
169 eq_clast_name_indexes (const void *e1, const void *e2)
171 const struct clast_name_index *elt1 = (const struct clast_name_index *) e1;
172 const struct clast_name_index *elt2 = (const struct clast_name_index *) e2;
174 return (elt1->name == elt2->name);
178 /* For a given loop DEPTH in the loop nest of the original black box
179 PBB, return the old induction variable associated to that loop. */
182 pbb_to_depth_to_oldiv (poly_bb_p pbb, int depth)
184 gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
185 sese region = SCOP_REGION (PBB_SCOP (pbb));
186 loop_p loop = gbb_loop_at_index (gbb, region, depth);
188 return loop->single_iv;
191 /* For a given scattering dimension, return the new induction variable
195 newivs_to_depth_to_newiv (VEC (tree, heap) *newivs, int depth)
197 return VEC_index (tree, newivs, depth);
202 /* Returns the tree variable from the name NAME that was given in
203 Cloog representation. */
206 clast_name_to_gcc (const char *name, sese region, VEC (tree, heap) *newivs,
207 htab_t newivs_index, htab_t params_index)
210 VEC (tree, heap) *params = SESE_PARAMS (region);
212 if (params && params_index)
214 index = clast_name_to_index (name, params_index);
217 return VEC_index (tree, params, index);
220 gcc_assert (newivs && newivs_index);
221 index = clast_name_to_index (name, newivs_index);
222 gcc_assert (index >= 0);
224 return newivs_to_depth_to_newiv (newivs, index);
227 /* Returns the signed maximal precision type for expressions TYPE1 and TYPE2. */
230 max_signed_precision_type (tree type1, tree type2)
232 int p1 = TYPE_PRECISION (type1);
233 int p2 = TYPE_PRECISION (type2);
238 precision = TYPE_UNSIGNED (type1) ? p1 * 2 : p1;
240 precision = TYPE_UNSIGNED (type2) ? p2 * 2 : p2;
242 type = lang_hooks.types.type_for_size (precision, false);
247 return integer_type_node;
252 /* Returns the maximal precision type for expressions TYPE1 and TYPE2. */
255 max_precision_type (tree type1, tree type2)
257 if (POINTER_TYPE_P (type1))
260 if (POINTER_TYPE_P (type2))
263 if (!TYPE_UNSIGNED (type1)
264 || !TYPE_UNSIGNED (type2))
265 return max_signed_precision_type (type1, type2);
267 return TYPE_PRECISION (type1) > TYPE_PRECISION (type2) ? type1 : type2;
271 clast_to_gcc_expression (tree, struct clast_expr *, sese, VEC (tree, heap) *,
274 /* Converts a Cloog reduction expression R with reduction operation OP
275 to a GCC expression tree of type TYPE. */
278 clast_to_gcc_expression_red (tree type, enum tree_code op,
279 struct clast_reduction *r,
280 sese region, VEC (tree, heap) *newivs,
281 htab_t newivs_index, htab_t params_index)
284 tree res = clast_to_gcc_expression (type, r->elts[0], region, newivs,
285 newivs_index, params_index);
286 tree operand_type = (op == POINTER_PLUS_EXPR) ? sizetype : type;
288 for (i = 1; i < r->n; i++)
290 tree t = clast_to_gcc_expression (operand_type, r->elts[i], region,
291 newivs, newivs_index, params_index);
292 res = fold_build2 (op, type, res, t);
298 /* Converts a Cloog AST expression E back to a GCC expression tree of
302 clast_to_gcc_expression (tree type, struct clast_expr *e,
303 sese region, VEC (tree, heap) *newivs,
304 htab_t newivs_index, htab_t params_index)
310 struct clast_term *t = (struct clast_term *) e;
314 if (value_one_p (t->val))
316 tree name = clast_name_to_gcc (t->var, region, newivs,
317 newivs_index, params_index);
319 if (POINTER_TYPE_P (TREE_TYPE (name)) != POINTER_TYPE_P (type))
320 name = fold_convert (sizetype, name);
322 name = fold_convert (type, name);
326 else if (value_mone_p (t->val))
328 tree name = clast_name_to_gcc (t->var, region, newivs,
329 newivs_index, params_index);
331 if (POINTER_TYPE_P (TREE_TYPE (name)) != POINTER_TYPE_P (type))
332 name = fold_convert (sizetype, name);
334 name = fold_convert (type, name);
336 return fold_build1 (NEGATE_EXPR, type, name);
340 tree name = clast_name_to_gcc (t->var, region, newivs,
341 newivs_index, params_index);
342 tree cst = gmp_cst_to_tree (type, t->val);
344 if (POINTER_TYPE_P (TREE_TYPE (name)) != POINTER_TYPE_P (type))
345 name = fold_convert (sizetype, name);
347 name = fold_convert (type, name);
349 if (!POINTER_TYPE_P (type))
350 return fold_build2 (MULT_EXPR, type, cst, name);
357 return gmp_cst_to_tree (type, t->val);
362 struct clast_reduction *r = (struct clast_reduction *) e;
367 return clast_to_gcc_expression_red
368 (type, POINTER_TYPE_P (type) ? POINTER_PLUS_EXPR : PLUS_EXPR,
369 r, region, newivs, newivs_index, params_index);
372 return clast_to_gcc_expression_red (type, MIN_EXPR, r, region,
373 newivs, newivs_index,
377 return clast_to_gcc_expression_red (type, MAX_EXPR, r, region,
378 newivs, newivs_index,
389 struct clast_binary *b = (struct clast_binary *) e;
390 struct clast_expr *lhs = (struct clast_expr *) b->LHS;
391 tree tl = clast_to_gcc_expression (type, lhs, region, newivs,
392 newivs_index, params_index);
393 tree tr = gmp_cst_to_tree (type, b->RHS);
398 return fold_build2 (FLOOR_DIV_EXPR, type, tl, tr);
401 return fold_build2 (CEIL_DIV_EXPR, type, tl, tr);
404 return fold_build2 (EXACT_DIV_EXPR, type, tl, tr);
407 return fold_build2 (TRUNC_MOD_EXPR, type, tl, tr);
421 /* Return the precision needed to represent the value VAL. */
424 precision_for_value (Value val)
433 value_assign (y, val);
434 value_set_si (two, 2);
440 while (value_gt (y, x))
442 value_multiply (x, x, two);
453 /* Return the precision needed to represent the values between LOW and
457 precision_for_interval (Value low, Value up)
462 gcc_assert (value_le (low, up));
465 value_subtract (diff, up, low);
466 precision = precision_for_value (diff);
472 /* Return a type that could represent the integer value VAL, or
473 otherwise return NULL_TREE. */
476 gcc_type_for_interval (Value low, Value up, tree old_type)
478 bool unsigned_p = true;
479 int precision, prec_up, prec_int;
482 gcc_assert (value_le (low, up));
484 /* Preserve the signedness of the old IV. */
485 if ((old_type && !TYPE_UNSIGNED (old_type))
486 || value_neg_p (low))
489 prec_up = precision_for_value (up);
490 prec_int = precision_for_interval (low, up);
491 precision = prec_up > prec_int ? prec_up : prec_int;
493 type = lang_hooks.types.type_for_size (precision, unsigned_p);
497 return integer_type_node;
503 /* Return a type that could represent the integer value VAL, or
504 otherwise return NULL_TREE. */
507 gcc_type_for_value (Value val)
509 return gcc_type_for_interval (val, val, NULL_TREE);
512 /* Return the type for the clast_term T used in STMT. */
515 gcc_type_for_clast_term (struct clast_term *t,
516 sese region, VEC (tree, heap) *newivs,
517 htab_t newivs_index, htab_t params_index)
519 gcc_assert (t->expr.type == expr_term);
522 return gcc_type_for_value (t->val);
524 return TREE_TYPE (clast_name_to_gcc (t->var, region, newivs,
525 newivs_index, params_index));
529 gcc_type_for_clast_expr (struct clast_expr *, sese,
530 VEC (tree, heap) *, htab_t, htab_t);
532 /* Return the type for the clast_reduction R used in STMT. */
535 gcc_type_for_clast_red (struct clast_reduction *r, sese region,
536 VEC (tree, heap) *newivs,
537 htab_t newivs_index, htab_t params_index)
540 tree type = NULL_TREE;
543 return gcc_type_for_clast_expr (r->elts[0], region, newivs,
544 newivs_index, params_index);
551 type = gcc_type_for_clast_expr (r->elts[0], region, newivs,
552 newivs_index, params_index);
553 for (i = 1; i < r->n; i++)
554 type = max_precision_type (type, gcc_type_for_clast_expr
555 (r->elts[i], region, newivs,
556 newivs_index, params_index));
568 /* Return the type for the clast_binary B used in STMT. */
571 gcc_type_for_clast_bin (struct clast_binary *b,
572 sese region, VEC (tree, heap) *newivs,
573 htab_t newivs_index, htab_t params_index)
575 tree l = gcc_type_for_clast_expr ((struct clast_expr *) b->LHS, region,
576 newivs, newivs_index, params_index);
577 tree r = gcc_type_for_value (b->RHS);
578 return max_signed_precision_type (l, r);
581 /* Returns the type for the CLAST expression E when used in statement
585 gcc_type_for_clast_expr (struct clast_expr *e,
586 sese region, VEC (tree, heap) *newivs,
587 htab_t newivs_index, htab_t params_index)
592 return gcc_type_for_clast_term ((struct clast_term *) e, region,
593 newivs, newivs_index, params_index);
596 return gcc_type_for_clast_red ((struct clast_reduction *) e, region,
597 newivs, newivs_index, params_index);
600 return gcc_type_for_clast_bin ((struct clast_binary *) e, region,
601 newivs, newivs_index, params_index);
610 /* Returns the type for the equation CLEQ. */
613 gcc_type_for_clast_eq (struct clast_equation *cleq,
614 sese region, VEC (tree, heap) *newivs,
615 htab_t newivs_index, htab_t params_index)
617 tree l = gcc_type_for_clast_expr (cleq->LHS, region, newivs,
618 newivs_index, params_index);
619 tree r = gcc_type_for_clast_expr (cleq->RHS, region, newivs,
620 newivs_index, params_index);
621 return max_precision_type (l, r);
624 /* Translates a clast equation CLEQ to a tree. */
627 graphite_translate_clast_equation (sese region,
628 struct clast_equation *cleq,
629 VEC (tree, heap) *newivs,
630 htab_t newivs_index, htab_t params_index)
633 tree type = gcc_type_for_clast_eq (cleq, region, newivs, newivs_index,
635 tree lhs = clast_to_gcc_expression (type, cleq->LHS, region, newivs,
636 newivs_index, params_index);
637 tree rhs = clast_to_gcc_expression (type, cleq->RHS, region, newivs,
638 newivs_index, params_index);
643 else if (cleq->sign > 0)
649 return fold_build2 (comp, boolean_type_node, lhs, rhs);
652 /* Creates the test for the condition in STMT. */
655 graphite_create_guard_cond_expr (sese region, struct clast_guard *stmt,
656 VEC (tree, heap) *newivs,
657 htab_t newivs_index, htab_t params_index)
662 for (i = 0; i < stmt->n; i++)
664 tree eq = graphite_translate_clast_equation (region, &stmt->eq[i],
665 newivs, newivs_index,
669 cond = fold_build2 (TRUTH_AND_EXPR, TREE_TYPE (eq), cond, eq);
677 /* Creates a new if region corresponding to Cloog's guard. */
680 graphite_create_new_guard (sese region, edge entry_edge,
681 struct clast_guard *stmt,
682 VEC (tree, heap) *newivs,
683 htab_t newivs_index, htab_t params_index)
685 tree cond_expr = graphite_create_guard_cond_expr (region, stmt, newivs,
686 newivs_index, params_index);
687 edge exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr);
691 /* Compute the lower bound LOW and upper bound UP for the induction
692 variable at LEVEL for the statement PBB, based on the transformed
693 scattering of PBB: T|I|G|Cst, with T the scattering transform, I
694 the iteration domain, and G the context parameters. */
697 compute_bounds_for_level (poly_bb_p pbb, int level, Value low, Value up)
699 ppl_Pointset_Powerset_C_Polyhedron_t ps;
700 ppl_Linear_Expression_t le;
702 combine_context_id_scat (&ps, pbb, false);
704 /* Prepare the linear expression corresponding to the level that we
705 want to maximize/minimize. */
707 ppl_dimension_type dim = pbb_nb_scattering_transform (pbb)
708 + pbb_dim_iter_domain (pbb) + pbb_nb_params (pbb);
710 ppl_new_Linear_Expression_with_dimension (&le, dim);
711 ppl_set_coef (le, 2 * level + 1, 1);
714 ppl_max_for_le_pointset (ps, le, up);
715 ppl_min_for_le_pointset (ps, le, low);
718 /* Compute the type for the induction variable at LEVEL for the
719 statement PBB, based on the transformed schedule of PBB. OLD_TYPE
720 is the type of the old induction variable for that loop. */
723 compute_type_for_level_1 (poly_bb_p pbb, int level, tree old_type)
731 compute_bounds_for_level (pbb, level, low, up);
732 type = gcc_type_for_interval (low, up, old_type);
739 /* Compute the type for the induction variable at LEVEL for the
740 statement PBB, based on the transformed schedule of PBB. */
743 compute_type_for_level (poly_bb_p pbb, int level)
745 tree oldiv = pbb_to_depth_to_oldiv (pbb, level);
746 tree type = TREE_TYPE (oldiv);
748 if (type && POINTER_TYPE_P (type))
750 #ifdef ENABLE_CHECKING
751 tree ctype = compute_type_for_level_1 (pbb, level, type);
753 /* In the case of a pointer type, check that after the loop
754 transform, the lower and the upper bounds of the type fit the
755 oldiv pointer type. */
756 gcc_assert (TYPE_PRECISION (type) >= TYPE_PRECISION (ctype)
757 && integer_zerop (lower_bound_in_type (ctype, ctype)));
762 return compute_type_for_level_1 (pbb, level, type);
765 /* Walks a CLAST and returns the first statement in the body of a
768 static struct clast_user_stmt *
769 clast_get_body_of_loop (struct clast_stmt *stmt)
772 || CLAST_STMT_IS_A (stmt, stmt_user))
773 return (struct clast_user_stmt *) stmt;
775 if (CLAST_STMT_IS_A (stmt, stmt_for))
776 return clast_get_body_of_loop (((struct clast_for *) stmt)->body);
778 if (CLAST_STMT_IS_A (stmt, stmt_guard))
779 return clast_get_body_of_loop (((struct clast_guard *) stmt)->then);
781 if (CLAST_STMT_IS_A (stmt, stmt_block))
782 return clast_get_body_of_loop (((struct clast_block *) stmt)->body);
787 /* Returns the type for the induction variable for the loop translated
791 gcc_type_for_iv_of_clast_loop (struct clast_for *stmt_for, int level,
792 tree lb_type, tree ub_type)
794 struct clast_stmt *stmt = (struct clast_stmt *) stmt_for;
795 struct clast_user_stmt *body = clast_get_body_of_loop (stmt);
796 CloogStatement *cs = body->statement;
797 poly_bb_p pbb = (poly_bb_p) cloog_statement_usr (cs);
799 return max_signed_precision_type (lb_type, max_precision_type
800 (ub_type, compute_type_for_level
804 /* Creates a new LOOP corresponding to Cloog's STMT. Inserts an
805 induction variable for the new LOOP. New LOOP is attached to CFG
806 starting at ENTRY_EDGE. LOOP is inserted into the loop tree and
807 becomes the child loop of the OUTER_LOOP. NEWIVS_INDEX binds
808 CLooG's scattering name to the induction variable created for the
809 loop of STMT. The new induction variable is inserted in the NEWIVS
813 graphite_create_new_loop (sese region, edge entry_edge,
814 struct clast_for *stmt,
815 loop_p outer, VEC (tree, heap) **newivs,
816 htab_t newivs_index, htab_t params_index, int level)
818 tree lb_type = gcc_type_for_clast_expr (stmt->LB, region, *newivs,
819 newivs_index, params_index);
820 tree ub_type = gcc_type_for_clast_expr (stmt->UB, region, *newivs,
821 newivs_index, params_index);
822 tree type = gcc_type_for_iv_of_clast_loop (stmt, level, lb_type, ub_type);
823 tree lb = clast_to_gcc_expression (type, stmt->LB, region, *newivs,
824 newivs_index, params_index);
825 tree ub = clast_to_gcc_expression (type, stmt->UB, region, *newivs,
826 newivs_index, params_index);
827 tree stride = gmp_cst_to_tree (type, stmt->stride);
828 tree ivvar = create_tmp_var (type, "graphite_IV");
829 tree iv, iv_after_increment;
830 loop_p loop = create_empty_loop_on_edge
831 (entry_edge, lb, stride, ub, ivvar, &iv, &iv_after_increment,
832 outer ? outer : entry_edge->src->loop_father);
834 add_referenced_var (ivvar);
836 save_clast_name_index (newivs_index, stmt->iterator,
837 VEC_length (tree, *newivs));
838 VEC_safe_push (tree, heap, *newivs, iv);
842 /* Inserts in MAP a tuple (OLD_NAME, NEW_NAME) for the induction
843 variables of the loops around GBB in SESE. */
846 build_iv_mapping (htab_t map, sese region,
847 VEC (tree, heap) *newivs, htab_t newivs_index,
848 struct clast_user_stmt *user_stmt,
851 struct clast_stmt *t;
853 CloogStatement *cs = user_stmt->statement;
854 poly_bb_p pbb = (poly_bb_p) cloog_statement_usr (cs);
856 for (t = user_stmt->substitutions; t; t = t->next, index++)
858 struct clast_expr *expr = (struct clast_expr *)
859 ((struct clast_assignment *)t)->RHS;
860 tree type = gcc_type_for_clast_expr (expr, region, newivs,
861 newivs_index, params_index);
862 tree old_name = pbb_to_depth_to_oldiv (pbb, index);
863 tree e = clast_to_gcc_expression (type, expr, region, newivs,
864 newivs_index, params_index);
865 set_rename (map, old_name, e);
869 /* Helper function for htab_traverse. */
872 copy_renames (void **slot, void *s)
874 struct rename_map_elt_s *entry = (struct rename_map_elt_s *) *slot;
875 htab_t res = (htab_t) s;
876 tree old_name = entry->old_name;
877 tree expr = entry->expr;
878 struct rename_map_elt_s tmp;
881 tmp.old_name = old_name;
882 x = htab_find_slot (res, &tmp, INSERT);
885 *x = new_rename_map_elt (old_name, expr);
890 /* Construct bb_pbb_def with BB and PBB. */
893 new_bb_pbb_def (basic_block bb, poly_bb_p pbb)
895 bb_pbb_def *bb_pbb_p;
897 bb_pbb_p = XNEW (bb_pbb_def);
904 /* Mark BB with it's relevant PBB via hashing table BB_PBB_MAPPING. */
907 mark_bb_with_pbb (poly_bb_p pbb, basic_block bb, htab_t bb_pbb_mapping)
913 x = htab_find_slot (bb_pbb_mapping, &tmp, INSERT);
916 *x = new_bb_pbb_def (bb, pbb);
919 /* Find BB's related poly_bb_p in hash table BB_PBB_MAPPING. */
922 find_pbb_via_hash (htab_t bb_pbb_mapping, basic_block bb)
928 slot = htab_find_slot (bb_pbb_mapping, &tmp, NO_INSERT);
931 return ((bb_pbb_def *) *slot)->pbb;
936 /* Check data dependency in LOOP at scattering level LEVEL.
937 BB_PBB_MAPPING is a basic_block and it's related poly_bb_p
941 dependency_in_loop_p (loop_p loop, htab_t bb_pbb_mapping, int level)
944 basic_block *bbs = get_loop_body_in_dom_order (loop);
946 for (i = 0; i < loop->num_nodes; i++)
948 poly_bb_p pbb1 = find_pbb_via_hash (bb_pbb_mapping, bbs[i]);
953 for (j = 0; j < loop->num_nodes; j++)
955 poly_bb_p pbb2 = find_pbb_via_hash (bb_pbb_mapping, bbs[j]);
960 if (dependency_between_pbbs_p (pbb1, pbb2, level))
974 translate_clast (sese, loop_p, struct clast_stmt *, edge, htab_t,
975 VEC (tree, heap) **, htab_t, htab_t, int, htab_t);
977 /* Translates a clast user statement STMT to gimple.
979 - REGION is the sese region we used to generate the scop.
980 - NEXT_E is the edge where new generated code should be attached.
981 - CONTEXT_LOOP is the loop in which the generated code will be placed
982 - RENAME_MAP contains a set of tuples of new names associated to
983 the original variables names.
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_user (sese region, struct clast_user_stmt *stmt, edge next_e,
989 htab_t rename_map, VEC (tree, heap) **newivs,
990 htab_t newivs_index, htab_t bb_pbb_mapping,
995 poly_bb_p pbb = (poly_bb_p) cloog_statement_usr (stmt->statement);
996 gbb = PBB_BLACK_BOX (pbb);
998 if (GBB_BB (gbb) == ENTRY_BLOCK_PTR)
1001 build_iv_mapping (rename_map, region, *newivs, newivs_index, stmt,
1003 next_e = copy_bb_and_scalar_dependences (GBB_BB (gbb), region,
1004 next_e, rename_map);
1005 new_bb = next_e->src;
1006 mark_bb_with_pbb (pbb, new_bb, bb_pbb_mapping);
1007 update_ssa (TODO_update_ssa);
1012 /* Creates a new if region protecting the loop to be executed, if the execution
1013 count is zero (lb > ub). */
1015 graphite_create_new_loop_guard (sese region, edge entry_edge,
1016 struct clast_for *stmt,
1017 VEC (tree, heap) *newivs,
1018 htab_t newivs_index, htab_t params_index)
1022 tree lb_type = gcc_type_for_clast_expr (stmt->LB, region, newivs,
1023 newivs_index, params_index);
1024 tree ub_type = gcc_type_for_clast_expr (stmt->UB, region, newivs,
1025 newivs_index, params_index);
1026 tree type = max_precision_type (lb_type, ub_type);
1027 tree lb = clast_to_gcc_expression (type, stmt->LB, region, newivs,
1028 newivs_index, params_index);
1029 tree ub = clast_to_gcc_expression (type, stmt->UB, region, newivs,
1030 newivs_index, params_index);
1033 /* Adding +1 and using LT_EXPR helps with loop latches that have a
1034 loop iteration count of "PARAMETER - 1". For PARAMETER == 0 this becomes
1035 2^{32|64}, and the condition lb <= ub is true, even if we do not want this.
1036 However lb < ub + 1 is false, as expected. */
1040 value_init (gmp_one);
1041 value_set_si (gmp_one, 1);
1042 one = gmp_cst_to_tree (type, gmp_one);
1043 value_clear (gmp_one);
1045 ub_one = fold_build2 (POINTER_TYPE_P (type) ? POINTER_PLUS_EXPR : PLUS_EXPR,
1048 /* When ub + 1 wraps around, use lb <= ub. */
1049 if (integer_zerop (ub_one))
1050 cond_expr = fold_build2 (LE_EXPR, boolean_type_node, lb, ub);
1052 cond_expr = fold_build2 (LT_EXPR, boolean_type_node, lb, ub_one);
1054 exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr);
1060 /* Create the loop for a clast for statement.
1062 - REGION is the sese region we used to generate the scop.
1063 - NEXT_E is the edge where new generated code should be attached.
1064 - RENAME_MAP contains a set of tuples of new names associated to
1065 the original variables names.
1066 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
1067 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
1070 translate_clast_for_loop (sese region, loop_p context_loop,
1071 struct clast_for *stmt, edge next_e,
1072 htab_t rename_map, VEC (tree, heap) **newivs,
1073 htab_t newivs_index, htab_t bb_pbb_mapping,
1074 int level, htab_t params_index)
1076 struct loop *loop = graphite_create_new_loop (region, next_e, stmt,
1077 context_loop, newivs,
1078 newivs_index, params_index,
1080 edge last_e = single_exit (loop);
1081 edge to_body = single_succ_edge (loop->header);
1082 basic_block after = to_body->dest;
1084 /* Create a basic block for loop close phi nodes. */
1085 last_e = single_succ_edge (split_edge (last_e));
1087 /* Translate the body of the loop. */
1088 next_e = translate_clast (region, loop, stmt->body, to_body, rename_map,
1089 newivs, newivs_index, bb_pbb_mapping, level + 1,
1091 redirect_edge_succ_nodup (next_e, after);
1092 set_immediate_dominator (CDI_DOMINATORS, next_e->dest, next_e->src);
1094 /* Remove from rename_map all the tuples containing variables
1095 defined in loop's body. */
1096 insert_loop_close_phis (rename_map, loop);
1098 if (flag_loop_parallelize_all
1099 && !dependency_in_loop_p (loop, bb_pbb_mapping,
1100 get_scattering_level (level)))
1101 loop->can_be_parallel = true;
1106 /* Translates a clast for statement STMT to gimple. First a guard is created
1107 protecting the loop, if it is executed zero times. In this guard we create
1108 the real loop structure.
1110 - REGION is the sese region we used to generate the scop.
1111 - NEXT_E is the edge where new generated code should be attached.
1112 - RENAME_MAP contains a set of tuples of new names associated to
1113 the original variables names.
1114 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
1115 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
1118 translate_clast_for (sese region, loop_p context_loop, struct clast_for *stmt,
1119 edge next_e, htab_t rename_map, VEC (tree, heap) **newivs,
1120 htab_t newivs_index, htab_t bb_pbb_mapping, int level,
1121 htab_t params_index)
1123 edge last_e = graphite_create_new_loop_guard (region, next_e, stmt, *newivs,
1124 newivs_index, params_index);
1126 edge true_e = get_true_edge_from_guard_bb (next_e->dest);
1127 edge false_e = get_false_edge_from_guard_bb (next_e->dest);
1128 edge exit_true_e = single_succ_edge (true_e->dest);
1129 edge exit_false_e = single_succ_edge (false_e->dest);
1131 htab_t before_guard = htab_create (10, rename_map_elt_info,
1132 eq_rename_map_elts, free);
1133 htab_traverse (rename_map, copy_renames, before_guard);
1135 next_e = translate_clast_for_loop (region, context_loop, stmt, true_e,
1137 newivs_index, bb_pbb_mapping, level,
1140 insert_guard_phis (last_e->src, exit_true_e, exit_false_e,
1141 before_guard, rename_map);
1143 htab_delete (before_guard);
1148 /* Translates a clast guard statement STMT to gimple.
1150 - REGION is the sese region we used to generate the scop.
1151 - NEXT_E is the edge where new generated code should be attached.
1152 - CONTEXT_LOOP is the loop in which the generated code will be placed
1153 - RENAME_MAP contains a set of tuples of new names associated to
1154 the original variables names.
1155 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
1156 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
1159 translate_clast_guard (sese region, loop_p context_loop,
1160 struct clast_guard *stmt, edge next_e,
1161 htab_t rename_map, VEC (tree, heap) **newivs,
1162 htab_t newivs_index, htab_t bb_pbb_mapping, int level,
1163 htab_t params_index)
1165 edge last_e = graphite_create_new_guard (region, next_e, stmt, *newivs,
1166 newivs_index, params_index);
1168 edge true_e = get_true_edge_from_guard_bb (next_e->dest);
1169 edge false_e = get_false_edge_from_guard_bb (next_e->dest);
1170 edge exit_true_e = single_succ_edge (true_e->dest);
1171 edge exit_false_e = single_succ_edge (false_e->dest);
1173 htab_t before_guard = htab_create (10, rename_map_elt_info,
1174 eq_rename_map_elts, free);
1175 htab_traverse (rename_map, copy_renames, before_guard);
1177 next_e = translate_clast (region, context_loop, stmt->then, true_e,
1178 rename_map, newivs, newivs_index, bb_pbb_mapping,
1179 level, params_index);
1181 insert_guard_phis (last_e->src, exit_true_e, exit_false_e,
1182 before_guard, rename_map);
1184 htab_delete (before_guard);
1189 /* Translates a CLAST statement STMT to GCC representation in the
1192 - NEXT_E is the edge where new generated code should be attached.
1193 - CONTEXT_LOOP is the loop in which the generated code will be placed
1194 - RENAME_MAP contains a set of tuples of new names associated to
1195 the original variables names.
1196 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping. */
1198 translate_clast (sese region, loop_p context_loop, struct clast_stmt *stmt,
1199 edge next_e, htab_t rename_map, VEC (tree, heap) **newivs,
1200 htab_t newivs_index, htab_t bb_pbb_mapping, int level,
1201 htab_t params_index)
1206 if (CLAST_STMT_IS_A (stmt, stmt_root))
1209 else if (CLAST_STMT_IS_A (stmt, stmt_user))
1210 next_e = translate_clast_user (region, (struct clast_user_stmt *) stmt,
1211 next_e, rename_map, newivs, newivs_index,
1212 bb_pbb_mapping, params_index);
1214 else if (CLAST_STMT_IS_A (stmt, stmt_for))
1215 next_e = translate_clast_for (region, context_loop,
1216 (struct clast_for *) stmt, next_e,
1217 rename_map, newivs, newivs_index,
1218 bb_pbb_mapping, level, params_index);
1220 else if (CLAST_STMT_IS_A (stmt, stmt_guard))
1221 next_e = translate_clast_guard (region, context_loop,
1222 (struct clast_guard *) stmt, next_e,
1223 rename_map, newivs, newivs_index,
1224 bb_pbb_mapping, level, params_index);
1226 else if (CLAST_STMT_IS_A (stmt, stmt_block))
1227 next_e = translate_clast (region, context_loop,
1228 ((struct clast_block *) stmt)->body,
1229 next_e, rename_map, newivs, newivs_index,
1230 bb_pbb_mapping, level, params_index);
1234 recompute_all_dominators ();
1237 return translate_clast (region, context_loop, stmt->next, next_e,
1238 rename_map, newivs, newivs_index,
1239 bb_pbb_mapping, level, params_index);
1242 /* Free the SCATTERING domain list. */
1245 free_scattering (CloogDomainList *scattering)
1249 CloogDomain *dom = cloog_domain (scattering);
1250 CloogDomainList *next = cloog_next_domain (scattering);
1252 cloog_domain_free (dom);
1258 /* Initialize Cloog's parameter names from the names used in GIMPLE.
1259 Initialize Cloog's iterator names, using 'graphite_iterator_%d'
1260 from 0 to scop_nb_loops (scop). */
1263 initialize_cloog_names (scop_p scop, CloogProgram *prog)
1265 sese region = SCOP_REGION (scop);
1267 int nb_iterators = scop_max_loop_depth (scop);
1268 int nb_scattering = cloog_program_nb_scattdims (prog);
1269 int nb_parameters = VEC_length (tree, SESE_PARAMS (region));
1270 char **iterators = XNEWVEC (char *, nb_iterators * 2);
1271 char **scattering = XNEWVEC (char *, nb_scattering);
1272 char **parameters= XNEWVEC (char *, nb_parameters);
1274 cloog_program_set_names (prog, cloog_names_malloc ());
1276 for (i = 0; i < nb_parameters; i++)
1278 tree param = VEC_index (tree, SESE_PARAMS(region), i);
1279 const char *name = get_name (param);
1285 len = strlen (name);
1287 parameters[i] = XNEWVEC (char, len + 1);
1288 snprintf (parameters[i], len, "%s_%d", name, SSA_NAME_VERSION (param));
1291 cloog_names_set_nb_parameters (cloog_program_names (prog), nb_parameters);
1292 cloog_names_set_parameters (cloog_program_names (prog), parameters);
1294 for (i = 0; i < nb_iterators; i++)
1297 iterators[i] = XNEWVEC (char, len);
1298 snprintf (iterators[i], len, "git_%d", i);
1301 cloog_names_set_nb_iterators (cloog_program_names (prog),
1303 cloog_names_set_iterators (cloog_program_names (prog),
1306 for (i = 0; i < nb_scattering; i++)
1309 scattering[i] = XNEWVEC (char, len);
1310 snprintf (scattering[i], len, "scat_%d", i);
1313 cloog_names_set_nb_scattering (cloog_program_names (prog),
1315 cloog_names_set_scattering (cloog_program_names (prog),
1319 /* Build cloog program for SCoP. */
1322 build_cloog_prog (scop_p scop, CloogProgram *prog)
1325 int max_nb_loops = scop_max_loop_depth (scop);
1327 CloogLoop *loop_list = NULL;
1328 CloogBlockList *block_list = NULL;
1329 CloogDomainList *scattering = NULL;
1330 int nbs = 2 * max_nb_loops + 1;
1333 cloog_program_set_context
1334 (prog, new_Cloog_Domain_from_ppl_Pointset_Powerset (SCOP_CONTEXT (scop)));
1335 nbs = unify_scattering_dimensions (scop);
1336 scaldims = (int *) xmalloc (nbs * (sizeof (int)));
1337 cloog_program_set_nb_scattdims (prog, nbs);
1338 initialize_cloog_names (scop, prog);
1340 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
1342 CloogStatement *stmt;
1345 /* Dead code elimination: when the domain of a PBB is empty,
1346 don't generate code for the PBB. */
1347 if (ppl_Pointset_Powerset_C_Polyhedron_is_empty (PBB_DOMAIN (pbb)))
1350 /* Build the new statement and its block. */
1351 stmt = cloog_statement_alloc (pbb_index (pbb));
1352 block = cloog_block_alloc (stmt, 0, NULL, pbb_dim_iter_domain (pbb));
1353 cloog_statement_set_usr (stmt, pbb);
1355 /* Build loop list. */
1357 CloogLoop *new_loop_list = cloog_loop_malloc ();
1358 cloog_loop_set_next (new_loop_list, loop_list);
1359 cloog_loop_set_domain
1361 new_Cloog_Domain_from_ppl_Pointset_Powerset (PBB_DOMAIN (pbb)));
1362 cloog_loop_set_block (new_loop_list, block);
1363 loop_list = new_loop_list;
1366 /* Build block list. */
1368 CloogBlockList *new_block_list = cloog_block_list_malloc ();
1370 cloog_block_list_set_next (new_block_list, block_list);
1371 cloog_block_list_set_block (new_block_list, block);
1372 block_list = new_block_list;
1375 /* Build scattering list. */
1377 /* XXX: Replace with cloog_domain_list_alloc(), when available. */
1378 CloogDomainList *new_scattering
1379 = (CloogDomainList *) xmalloc (sizeof (CloogDomainList));
1380 ppl_Polyhedron_t scat;
1383 scat = PBB_TRANSFORMED_SCATTERING (pbb);
1384 dom = new_Cloog_Domain_from_ppl_Polyhedron (scat);
1386 cloog_set_next_domain (new_scattering, scattering);
1387 cloog_set_domain (new_scattering, dom);
1388 scattering = new_scattering;
1392 cloog_program_set_loop (prog, loop_list);
1393 cloog_program_set_blocklist (prog, block_list);
1395 for (i = 0; i < nbs; i++)
1398 cloog_program_set_scaldims (prog, scaldims);
1400 /* Extract scalar dimensions to simplify the code generation problem. */
1401 cloog_program_extract_scalars (prog, scattering);
1403 /* Apply scattering. */
1404 cloog_program_scatter (prog, scattering);
1405 free_scattering (scattering);
1407 /* Iterators corresponding to scalar dimensions have to be extracted. */
1408 cloog_names_scalarize (cloog_program_names (prog), nbs,
1409 cloog_program_scaldims (prog));
1411 /* Free blocklist. */
1413 CloogBlockList *next = cloog_program_blocklist (prog);
1417 CloogBlockList *toDelete = next;
1418 next = cloog_block_list_next (next);
1419 cloog_block_list_set_next (toDelete, NULL);
1420 cloog_block_list_set_block (toDelete, NULL);
1421 cloog_block_list_free (toDelete);
1423 cloog_program_set_blocklist (prog, NULL);
1427 /* Return the options that will be used in GLOOG. */
1429 static CloogOptions *
1430 set_cloog_options (void)
1432 CloogOptions *options = cloog_options_malloc ();
1434 /* Change cloog output language to C. If we do use FORTRAN instead, cloog
1435 will stop e.g. with "ERROR: unbounded loops not allowed in FORTRAN.", if
1436 we pass an incomplete program to cloog. */
1437 options->language = LANGUAGE_C;
1439 /* Enable complex equality spreading: removes dummy statements
1440 (assignments) in the generated code which repeats the
1441 substitution equations for statements. This is useless for
1445 /* Enable C pretty-printing mode: normalizes the substitution
1446 equations for statements. */
1449 /* Allow cloog to build strides with a stride width different to one.
1450 This example has stride = 4:
1452 for (i = 0; i < 20; i += 4)
1454 options->strides = 1;
1456 /* Disable optimizations and make cloog generate source code closer to the
1457 input. This is useful for debugging, but later we want the optimized
1460 XXX: We can not disable optimizations, as loop blocking is not working
1465 options->l = INT_MAX;
1471 /* Prints STMT to STDERR. */
1474 print_clast_stmt (FILE *file, struct clast_stmt *stmt)
1476 CloogOptions *options = set_cloog_options ();
1478 pprint (file, stmt, 0, options);
1479 cloog_options_free (options);
1482 /* Prints STMT to STDERR. */
1485 debug_clast_stmt (struct clast_stmt *stmt)
1487 print_clast_stmt (stderr, stmt);
1490 /* Translate SCOP to a CLooG program and clast. These two
1491 representations should be freed together: a clast cannot be used
1492 without a program. */
1495 scop_to_clast (scop_p scop)
1497 CloogOptions *options = set_cloog_options ();
1498 cloog_prog_clast pc;
1500 /* Connect new cloog prog generation to graphite. */
1501 pc.prog = cloog_program_malloc ();
1502 build_cloog_prog (scop, pc.prog);
1503 pc.prog = cloog_program_generate (pc.prog, options);
1504 pc.stmt = cloog_clast_create (pc.prog, options);
1506 cloog_options_free (options);
1510 /* Prints to FILE the code generated by CLooG for SCOP. */
1513 print_generated_program (FILE *file, scop_p scop)
1515 CloogOptions *options = set_cloog_options ();
1516 cloog_prog_clast pc = scop_to_clast (scop);
1518 fprintf (file, " (prog: \n");
1519 cloog_program_print (file, pc.prog);
1520 fprintf (file, " )\n");
1522 fprintf (file, " (clast: \n");
1523 pprint (file, pc.stmt, 0, options);
1524 fprintf (file, " )\n");
1526 cloog_options_free (options);
1527 cloog_clast_free (pc.stmt);
1528 cloog_program_free (pc.prog);
1531 /* Prints to STDERR the code generated by CLooG for SCOP. */
1534 debug_generated_program (scop_p scop)
1536 print_generated_program (stderr, scop);
1539 /* Add CLooG names to parameter index. The index is used to translate
1540 back from CLooG names to GCC trees. */
1543 create_params_index (htab_t index_table, CloogProgram *prog) {
1544 CloogNames* names = cloog_program_names (prog);
1545 int nb_parameters = cloog_names_nb_parameters (names);
1546 char **parameters = cloog_names_parameters (names);
1549 for (i = 0; i < nb_parameters; i++)
1550 save_clast_name_index (index_table, parameters[i], i);
1553 /* GIMPLE Loop Generator: generates loops from STMT in GIMPLE form for
1554 the given SCOP. Return true if code generation succeeded.
1555 BB_PBB_MAPPING is a basic_block and it's related poly_bb_p mapping.
1559 gloog (scop_p scop, VEC (scop_p, heap) *scops, htab_t bb_pbb_mapping)
1561 VEC (tree, heap) *newivs = VEC_alloc (tree, heap, 10);
1562 loop_p context_loop;
1563 sese region = SCOP_REGION (scop);
1564 ifsese if_region = NULL;
1565 htab_t rename_map, newivs_index, params_index;
1566 cloog_prog_clast pc;
1569 timevar_push (TV_GRAPHITE_CODE_GEN);
1570 gloog_error = false;
1572 pc = scop_to_clast (scop);
1574 if (dump_file && (dump_flags & TDF_DETAILS))
1576 fprintf (dump_file, "\nCLAST generated by CLooG: \n");
1577 print_clast_stmt (dump_file, pc.stmt);
1578 fprintf (dump_file, "\n");
1581 recompute_all_dominators ();
1584 if_region = move_sese_in_condition (region);
1585 sese_insert_phis_for_liveouts (region,
1586 if_region->region->exit->src,
1587 if_region->false_region->exit,
1588 if_region->true_region->exit);
1589 recompute_all_dominators ();
1592 context_loop = SESE_ENTRY (region)->src->loop_father;
1593 rename_map = htab_create (10, rename_map_elt_info, eq_rename_map_elts, free);
1594 newivs_index = htab_create (10, clast_name_index_elt_info,
1595 eq_clast_name_indexes, free);
1596 params_index = htab_create (10, clast_name_index_elt_info,
1597 eq_clast_name_indexes, free);
1599 create_params_index (params_index, pc.prog);
1601 translate_clast (region, context_loop, pc.stmt,
1602 if_region->true_region->entry,
1603 rename_map, &newivs, newivs_index,
1604 bb_pbb_mapping, 1, params_index);
1606 sese_adjust_liveout_phis (region, rename_map,
1607 if_region->region->exit->src,
1608 if_region->false_region->exit,
1609 if_region->true_region->exit);
1611 rename_nb_iterations (rename_map);
1613 for (i = 0; VEC_iterate (scop_p, scops, i, scop); i++)
1614 rename_sese_parameters (rename_map, SCOP_REGION (scop));
1616 recompute_all_dominators ();
1620 set_ifsese_condition (if_region, integer_zero_node);
1622 free (if_region->true_region);
1623 free (if_region->region);
1626 htab_delete (rename_map);
1627 htab_delete (newivs_index);
1628 htab_delete (params_index);
1629 VEC_free (tree, heap, newivs);
1630 cloog_clast_free (pc.stmt);
1631 cloog_program_free (pc.prog);
1632 timevar_pop (TV_GRAPHITE_CODE_GEN);
1634 if (dump_file && (dump_flags & TDF_DETAILS))
1638 int num_no_dependency = 0;
1640 FOR_EACH_LOOP (li, loop, 0)
1641 if (loop->can_be_parallel)
1642 num_no_dependency++;
1644 fprintf (dump_file, "\n%d loops carried no dependency.\n",
1648 return !gloog_error;