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 (clast_name_p name, htab_t index_table)
102 struct clast_name_index tmp;
106 gcc_assert (name->type == clast_expr_name);
107 tmp.name = ((const struct clast_name*) name)->name;
112 slot = htab_find_slot (index_table, &tmp, NO_INSERT);
115 return ((struct clast_name_index *) *slot)->index;
120 /* Records in INDEX_TABLE the INDEX for NAME. */
123 save_clast_name_index (htab_t index_table, const char *name, int index)
125 struct clast_name_index tmp;
129 slot = htab_find_slot (index_table, &tmp, INSERT);
136 *slot = new_clast_name_index (name, index);
140 /* Computes a hash function for database element ELT. */
142 static inline hashval_t
143 clast_name_index_elt_info (const void *elt)
145 return htab_hash_pointer (((const struct clast_name_index *) elt)->name);
148 /* Compares database elements E1 and E2. */
151 eq_clast_name_indexes (const void *e1, const void *e2)
153 const struct clast_name_index *elt1 = (const struct clast_name_index *) e1;
154 const struct clast_name_index *elt2 = (const struct clast_name_index *) e2;
156 return (elt1->name == elt2->name);
159 /* For a given scattering dimension, return the new induction variable
163 newivs_to_depth_to_newiv (VEC (tree, heap) *newivs, int depth)
165 return VEC_index (tree, newivs, depth);
170 /* Returns the tree variable from the name NAME that was given in
171 Cloog representation. */
174 clast_name_to_gcc (clast_name_p name, sese region, VEC (tree, heap) *newivs,
175 htab_t newivs_index, htab_t params_index)
178 VEC (tree, heap) *params = SESE_PARAMS (region);
180 if (params && params_index)
182 index = clast_name_to_index (name, params_index);
185 return VEC_index (tree, params, index);
188 gcc_assert (newivs && newivs_index);
189 index = clast_name_to_index (name, newivs_index);
190 gcc_assert (index >= 0);
192 return newivs_to_depth_to_newiv (newivs, index);
195 /* Returns the signed maximal precision type for expressions TYPE1 and TYPE2. */
198 max_signed_precision_type (tree type1, tree type2)
200 int p1 = TYPE_PRECISION (type1);
201 int p2 = TYPE_PRECISION (type2);
206 precision = TYPE_UNSIGNED (type1) ? p1 * 2 : p1;
208 precision = TYPE_UNSIGNED (type2) ? p2 * 2 : p2;
210 type = lang_hooks.types.type_for_size (precision, false);
215 return integer_type_node;
220 /* Returns the maximal precision type for expressions TYPE1 and TYPE2. */
223 max_precision_type (tree type1, tree type2)
225 if (POINTER_TYPE_P (type1))
228 if (POINTER_TYPE_P (type2))
231 if (!TYPE_UNSIGNED (type1)
232 || !TYPE_UNSIGNED (type2))
233 return max_signed_precision_type (type1, type2);
235 return TYPE_PRECISION (type1) > TYPE_PRECISION (type2) ? type1 : type2;
239 clast_to_gcc_expression (tree, struct clast_expr *, sese, VEC (tree, heap) *,
242 /* Converts a Cloog reduction expression R with reduction operation OP
243 to a GCC expression tree of type TYPE. */
246 clast_to_gcc_expression_red (tree type, enum tree_code op,
247 struct clast_reduction *r,
248 sese region, VEC (tree, heap) *newivs,
249 htab_t newivs_index, htab_t params_index)
252 tree res = clast_to_gcc_expression (type, r->elts[0], region, newivs,
253 newivs_index, params_index);
254 tree operand_type = (op == POINTER_PLUS_EXPR) ? sizetype : type;
256 for (i = 1; i < r->n; i++)
258 tree t = clast_to_gcc_expression (operand_type, r->elts[i], region,
259 newivs, newivs_index, params_index);
260 res = fold_build2 (op, type, res, t);
266 /* Converts a Cloog AST expression E back to a GCC expression tree of
270 clast_to_gcc_expression (tree type, struct clast_expr *e,
271 sese region, VEC (tree, heap) *newivs,
272 htab_t newivs_index, htab_t params_index)
276 case clast_expr_term:
278 struct clast_term *t = (struct clast_term *) e;
282 if (mpz_cmp_si (t->val, 1) == 0)
284 tree name = clast_name_to_gcc (t->var, region, newivs,
285 newivs_index, params_index);
287 if (POINTER_TYPE_P (TREE_TYPE (name)) != POINTER_TYPE_P (type))
288 name = fold_convert (sizetype, name);
290 name = fold_convert (type, name);
294 else if (mpz_cmp_si (t->val, -1) == 0)
296 tree name = clast_name_to_gcc (t->var, region, newivs,
297 newivs_index, params_index);
299 if (POINTER_TYPE_P (TREE_TYPE (name)) != POINTER_TYPE_P (type))
300 name = fold_convert (sizetype, name);
302 name = fold_convert (type, name);
304 return fold_build1 (NEGATE_EXPR, type, name);
308 tree name = clast_name_to_gcc (t->var, region, newivs,
309 newivs_index, params_index);
310 tree cst = gmp_cst_to_tree (type, t->val);
312 if (POINTER_TYPE_P (TREE_TYPE (name)) != POINTER_TYPE_P (type))
313 name = fold_convert (sizetype, name);
315 name = fold_convert (type, name);
317 if (!POINTER_TYPE_P (type))
318 return fold_build2 (MULT_EXPR, type, cst, name);
325 return gmp_cst_to_tree (type, t->val);
330 struct clast_reduction *r = (struct clast_reduction *) e;
335 return clast_to_gcc_expression_red
336 (type, POINTER_TYPE_P (type) ? POINTER_PLUS_EXPR : PLUS_EXPR,
337 r, region, newivs, newivs_index, params_index);
340 return clast_to_gcc_expression_red (type, MIN_EXPR, r, region,
341 newivs, newivs_index,
345 return clast_to_gcc_expression_red (type, MAX_EXPR, r, region,
346 newivs, newivs_index,
357 struct clast_binary *b = (struct clast_binary *) e;
358 struct clast_expr *lhs = (struct clast_expr *) b->LHS;
359 tree tl = clast_to_gcc_expression (type, lhs, region, newivs,
360 newivs_index, params_index);
361 tree tr = gmp_cst_to_tree (type, b->RHS);
366 return fold_build2 (FLOOR_DIV_EXPR, type, tl, tr);
369 return fold_build2 (CEIL_DIV_EXPR, type, tl, tr);
372 return fold_build2 (EXACT_DIV_EXPR, type, tl, tr);
375 return fold_build2 (TRUNC_MOD_EXPR, type, tl, tr);
389 /* Return the precision needed to represent the value VAL. */
392 precision_for_value (mpz_t val)
408 while (mpz_cmp (y, x) > 0)
421 /* Return the precision needed to represent the values between LOW and
425 precision_for_interval (mpz_t low, mpz_t up)
430 gcc_assert (mpz_cmp (low, up) <= 0);
433 mpz_sub (diff, up, low);
434 precision = precision_for_value (diff);
440 /* Return a type that could represent the integer value VAL. */
443 gcc_type_for_interval (mpz_t low, mpz_t up)
445 bool unsigned_p = true;
446 int precision, prec_up, prec_int;
448 enum machine_mode mode;
450 gcc_assert (mpz_cmp (low, up) <= 0);
452 if (mpz_sgn (low) < 0)
455 prec_up = precision_for_value (up);
456 prec_int = precision_for_interval (low, up);
457 precision = MAX (prec_up, prec_int);
459 if (precision > BITS_PER_WORD)
462 return integer_type_node;
465 mode = smallest_mode_for_size (precision, MODE_INT);
466 precision = GET_MODE_PRECISION (mode);
467 type = build_nonstandard_integer_type (precision, unsigned_p);
472 return integer_type_node;
478 /* Return a type that could represent the integer value VAL, or
479 otherwise return NULL_TREE. */
482 gcc_type_for_value (mpz_t val)
484 return gcc_type_for_interval (val, val);
487 /* Return the type for the clast_term T used in STMT. */
490 gcc_type_for_clast_term (struct clast_term *t,
491 sese region, VEC (tree, heap) *newivs,
492 htab_t newivs_index, htab_t params_index)
494 gcc_assert (t->expr.type == clast_expr_term);
497 return gcc_type_for_value (t->val);
499 return TREE_TYPE (clast_name_to_gcc (t->var, region, newivs,
500 newivs_index, params_index));
504 gcc_type_for_clast_expr (struct clast_expr *, sese,
505 VEC (tree, heap) *, htab_t, htab_t);
507 /* Return the type for the clast_reduction R used in STMT. */
510 gcc_type_for_clast_red (struct clast_reduction *r, sese region,
511 VEC (tree, heap) *newivs,
512 htab_t newivs_index, htab_t params_index)
515 tree type = NULL_TREE;
518 return gcc_type_for_clast_expr (r->elts[0], region, newivs,
519 newivs_index, params_index);
526 type = gcc_type_for_clast_expr (r->elts[0], region, newivs,
527 newivs_index, params_index);
528 for (i = 1; i < r->n; i++)
529 type = max_precision_type (type, gcc_type_for_clast_expr
530 (r->elts[i], region, newivs,
531 newivs_index, params_index));
543 /* Return the type for the clast_binary B used in STMT. */
546 gcc_type_for_clast_bin (struct clast_binary *b,
547 sese region, VEC (tree, heap) *newivs,
548 htab_t newivs_index, htab_t params_index)
550 tree l = gcc_type_for_clast_expr ((struct clast_expr *) b->LHS, region,
551 newivs, newivs_index, params_index);
552 tree r = gcc_type_for_value (b->RHS);
553 return max_signed_precision_type (l, r);
556 /* Returns the type for the CLAST expression E when used in statement
560 gcc_type_for_clast_expr (struct clast_expr *e,
561 sese region, VEC (tree, heap) *newivs,
562 htab_t newivs_index, htab_t params_index)
566 case clast_expr_term:
567 return gcc_type_for_clast_term ((struct clast_term *) e, region,
568 newivs, newivs_index, params_index);
571 return gcc_type_for_clast_red ((struct clast_reduction *) e, region,
572 newivs, newivs_index, params_index);
575 return gcc_type_for_clast_bin ((struct clast_binary *) e, region,
576 newivs, newivs_index, params_index);
585 /* Returns the type for the equation CLEQ. */
588 gcc_type_for_clast_eq (struct clast_equation *cleq,
589 sese region, VEC (tree, heap) *newivs,
590 htab_t newivs_index, htab_t params_index)
592 tree l = gcc_type_for_clast_expr (cleq->LHS, region, newivs,
593 newivs_index, params_index);
594 tree r = gcc_type_for_clast_expr (cleq->RHS, region, newivs,
595 newivs_index, params_index);
596 return max_precision_type (l, r);
599 /* Translates a clast equation CLEQ to a tree. */
602 graphite_translate_clast_equation (sese region,
603 struct clast_equation *cleq,
604 VEC (tree, heap) *newivs,
605 htab_t newivs_index, htab_t params_index)
608 tree type = gcc_type_for_clast_eq (cleq, region, newivs, newivs_index,
610 tree lhs = clast_to_gcc_expression (type, cleq->LHS, region, newivs,
611 newivs_index, params_index);
612 tree rhs = clast_to_gcc_expression (type, cleq->RHS, region, newivs,
613 newivs_index, params_index);
618 else if (cleq->sign > 0)
624 return fold_build2 (comp, boolean_type_node, lhs, rhs);
627 /* Creates the test for the condition in STMT. */
630 graphite_create_guard_cond_expr (sese region, struct clast_guard *stmt,
631 VEC (tree, heap) *newivs,
632 htab_t newivs_index, htab_t params_index)
637 for (i = 0; i < stmt->n; i++)
639 tree eq = graphite_translate_clast_equation (region, &stmt->eq[i],
640 newivs, newivs_index,
644 cond = fold_build2 (TRUTH_AND_EXPR, TREE_TYPE (eq), cond, eq);
652 /* Creates a new if region corresponding to Cloog's guard. */
655 graphite_create_new_guard (sese region, edge entry_edge,
656 struct clast_guard *stmt,
657 VEC (tree, heap) *newivs,
658 htab_t newivs_index, htab_t params_index)
660 tree cond_expr = graphite_create_guard_cond_expr (region, stmt, newivs,
661 newivs_index, params_index);
662 edge exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr);
666 /* Compute the lower bound LOW and upper bound UP for the induction
667 variable at LEVEL for the statement PBB, based on the transformed
668 scattering of PBB: T|I|G|Cst, with T the scattering transform, I
669 the iteration domain, and G the context parameters. */
672 compute_bounds_for_level (poly_bb_p pbb, int level, mpz_t low, mpz_t up)
674 ppl_Pointset_Powerset_C_Polyhedron_t ps;
675 ppl_Linear_Expression_t le;
677 combine_context_id_scat (&ps, pbb, false);
679 /* Prepare the linear expression corresponding to the level that we
680 want to maximize/minimize. */
682 ppl_dimension_type dim = pbb_nb_scattering_transform (pbb)
683 + pbb_dim_iter_domain (pbb) + pbb_nb_params (pbb);
685 ppl_new_Linear_Expression_with_dimension (&le, dim);
686 ppl_set_coef (le, 2 * level + 1, 1);
689 ppl_max_for_le_pointset (ps, le, up);
690 ppl_min_for_le_pointset (ps, le, low);
693 /* Compute the type for the induction variable at LEVEL for the
694 statement PBB, based on the transformed schedule of PBB. */
697 compute_type_for_level (poly_bb_p pbb, int level)
705 compute_bounds_for_level (pbb, level, low, up);
706 type = gcc_type_for_interval (low, up);
713 /* Walks a CLAST and returns the first statement in the body of a
716 static struct clast_user_stmt *
717 clast_get_body_of_loop (struct clast_stmt *stmt)
720 || CLAST_STMT_IS_A (stmt, stmt_user))
721 return (struct clast_user_stmt *) stmt;
723 if (CLAST_STMT_IS_A (stmt, stmt_for))
724 return clast_get_body_of_loop (((struct clast_for *) stmt)->body);
726 if (CLAST_STMT_IS_A (stmt, stmt_guard))
727 return clast_get_body_of_loop (((struct clast_guard *) stmt)->then);
729 if (CLAST_STMT_IS_A (stmt, stmt_block))
730 return clast_get_body_of_loop (((struct clast_block *) stmt)->body);
735 /* Returns the type for the induction variable for the loop translated
739 gcc_type_for_iv_of_clast_loop (struct clast_for *stmt_for, int level,
740 tree lb_type, tree ub_type)
742 struct clast_stmt *stmt = (struct clast_stmt *) stmt_for;
743 struct clast_user_stmt *body = clast_get_body_of_loop (stmt);
744 CloogStatement *cs = body->statement;
745 poly_bb_p pbb = (poly_bb_p) cloog_statement_usr (cs);
747 return max_signed_precision_type (lb_type, max_precision_type
748 (ub_type, compute_type_for_level
752 /* Creates a new LOOP corresponding to Cloog's STMT. Inserts an
753 induction variable for the new LOOP. New LOOP is attached to CFG
754 starting at ENTRY_EDGE. LOOP is inserted into the loop tree and
755 becomes the child loop of the OUTER_LOOP. NEWIVS_INDEX binds
756 CLooG's scattering name to the induction variable created for the
757 loop of STMT. The new induction variable is inserted in the NEWIVS
761 graphite_create_new_loop (sese region, edge entry_edge,
762 struct clast_for *stmt,
763 loop_p outer, VEC (tree, heap) **newivs,
764 htab_t newivs_index, htab_t params_index, int level)
766 tree lb_type = gcc_type_for_clast_expr (stmt->LB, region, *newivs,
767 newivs_index, params_index);
768 tree ub_type = gcc_type_for_clast_expr (stmt->UB, region, *newivs,
769 newivs_index, params_index);
770 tree type = gcc_type_for_iv_of_clast_loop (stmt, level, lb_type, ub_type);
771 tree lb = clast_to_gcc_expression (type, stmt->LB, region, *newivs,
772 newivs_index, params_index);
773 tree ub = clast_to_gcc_expression (type, stmt->UB, region, *newivs,
774 newivs_index, params_index);
775 tree stride = gmp_cst_to_tree (type, stmt->stride);
776 tree ivvar = create_tmp_var (type, "graphite_IV");
777 tree iv, iv_after_increment;
778 loop_p loop = create_empty_loop_on_edge
779 (entry_edge, lb, stride, ub, ivvar, &iv, &iv_after_increment,
780 outer ? outer : entry_edge->src->loop_father);
782 add_referenced_var (ivvar);
784 save_clast_name_index (newivs_index, stmt->iterator,
785 VEC_length (tree, *newivs));
786 VEC_safe_push (tree, heap, *newivs, iv);
790 /* Inserts in iv_map a tuple (OLD_LOOP->num, NEW_NAME) for the
791 induction variables of the loops around GBB in SESE. */
794 build_iv_mapping (VEC (tree, heap) *iv_map, sese region,
795 VEC (tree, heap) *newivs, htab_t newivs_index,
796 struct clast_user_stmt *user_stmt,
799 struct clast_stmt *t;
801 CloogStatement *cs = user_stmt->statement;
802 poly_bb_p pbb = (poly_bb_p) cloog_statement_usr (cs);
803 gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
805 for (t = user_stmt->substitutions; t; t = t->next, depth++)
807 struct clast_expr *expr = (struct clast_expr *)
808 ((struct clast_assignment *)t)->RHS;
809 tree type = gcc_type_for_clast_expr (expr, region, newivs,
810 newivs_index, params_index);
811 tree new_name = clast_to_gcc_expression (type, expr, region, newivs,
812 newivs_index, params_index);
813 loop_p old_loop = gbb_loop_at_index (gbb, region, depth);
815 VEC_replace (tree, iv_map, old_loop->num, new_name);
819 /* Construct bb_pbb_def with BB and PBB. */
822 new_bb_pbb_def (basic_block bb, poly_bb_p pbb)
824 bb_pbb_def *bb_pbb_p;
826 bb_pbb_p = XNEW (bb_pbb_def);
833 /* Mark BB with it's relevant PBB via hashing table BB_PBB_MAPPING. */
836 mark_bb_with_pbb (poly_bb_p pbb, basic_block bb, htab_t bb_pbb_mapping)
842 x = htab_find_slot (bb_pbb_mapping, &tmp, INSERT);
845 *x = new_bb_pbb_def (bb, pbb);
848 /* Find BB's related poly_bb_p in hash table BB_PBB_MAPPING. */
851 find_pbb_via_hash (htab_t bb_pbb_mapping, basic_block bb)
857 slot = htab_find_slot (bb_pbb_mapping, &tmp, NO_INSERT);
860 return ((bb_pbb_def *) *slot)->pbb;
865 /* Check data dependency in LOOP at scattering level LEVEL.
866 BB_PBB_MAPPING is a basic_block and it's related poly_bb_p
870 dependency_in_loop_p (loop_p loop, htab_t bb_pbb_mapping, int level)
873 basic_block *bbs = get_loop_body_in_dom_order (loop);
875 for (i = 0; i < loop->num_nodes; i++)
877 poly_bb_p pbb1 = find_pbb_via_hash (bb_pbb_mapping, bbs[i]);
882 for (j = 0; j < loop->num_nodes; j++)
884 poly_bb_p pbb2 = find_pbb_via_hash (bb_pbb_mapping, bbs[j]);
889 if (dependency_between_pbbs_p (pbb1, pbb2, level))
902 /* Translates a clast user statement STMT to gimple.
904 - REGION is the sese region we used to generate the scop.
905 - NEXT_E is the edge where new generated code should be attached.
906 - CONTEXT_LOOP is the loop in which the generated code will be placed
907 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
908 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
911 translate_clast_user (sese region, struct clast_user_stmt *stmt, edge next_e,
912 VEC (tree, heap) **newivs,
913 htab_t newivs_index, htab_t bb_pbb_mapping,
918 poly_bb_p pbb = (poly_bb_p) cloog_statement_usr (stmt->statement);
919 gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
920 VEC (tree, heap) *iv_map;
922 if (GBB_BB (gbb) == ENTRY_BLOCK_PTR)
925 nb_loops = number_of_loops ();
926 iv_map = VEC_alloc (tree, heap, nb_loops);
927 for (i = 0; i < nb_loops; i++)
928 VEC_quick_push (tree, iv_map, NULL_TREE);
930 build_iv_mapping (iv_map, region, *newivs, newivs_index, stmt, params_index);
931 next_e = copy_bb_and_scalar_dependences (GBB_BB (gbb), region,
933 VEC_free (tree, heap, iv_map);
935 new_bb = next_e->src;
936 mark_bb_with_pbb (pbb, new_bb, bb_pbb_mapping);
937 update_ssa (TODO_update_ssa);
942 /* Creates a new if region protecting the loop to be executed, if the execution
943 count is zero (lb > ub). */
946 graphite_create_new_loop_guard (sese region, edge entry_edge,
947 struct clast_for *stmt,
948 VEC (tree, heap) *newivs,
949 htab_t newivs_index, htab_t params_index)
953 tree lb_type = gcc_type_for_clast_expr (stmt->LB, region, newivs,
954 newivs_index, params_index);
955 tree ub_type = gcc_type_for_clast_expr (stmt->UB, region, newivs,
956 newivs_index, params_index);
957 tree type = max_precision_type (lb_type, ub_type);
958 tree lb = clast_to_gcc_expression (type, stmt->LB, region, newivs,
959 newivs_index, params_index);
960 tree ub = clast_to_gcc_expression (type, stmt->UB, region, newivs,
961 newivs_index, params_index);
962 tree one = POINTER_TYPE_P (type) ? size_one_node
963 : fold_convert (type, integer_one_node);
964 /* Adding +1 and using LT_EXPR helps with loop latches that have a
965 loop iteration count of "PARAMETER - 1". For PARAMETER == 0 this becomes
966 2^{32|64}, and the condition lb <= ub is true, even if we do not want this.
967 However lb < ub + 1 is false, as expected. */
968 tree ub_one = fold_build2 (POINTER_TYPE_P (type) ? POINTER_PLUS_EXPR
969 : PLUS_EXPR, type, ub, one);
971 /* When ub + 1 wraps around, use lb <= ub. */
972 if (integer_zerop (ub_one))
973 cond_expr = fold_build2 (LE_EXPR, boolean_type_node, lb, ub);
975 cond_expr = fold_build2 (LT_EXPR, boolean_type_node, lb, ub_one);
977 exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr);
983 translate_clast (sese, loop_p, struct clast_stmt *, edge,
984 VEC (tree, heap) **, htab_t, htab_t, int, htab_t);
986 /* Create the loop for a clast for statement.
988 - REGION is the sese region we used to generate the scop.
989 - NEXT_E is the edge where new generated code should be attached.
990 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
991 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
994 translate_clast_for_loop (sese region, loop_p context_loop,
995 struct clast_for *stmt, edge next_e,
996 VEC (tree, heap) **newivs,
997 htab_t newivs_index, htab_t bb_pbb_mapping,
998 int level, htab_t params_index)
1000 struct loop *loop = graphite_create_new_loop (region, next_e, stmt,
1001 context_loop, newivs,
1002 newivs_index, params_index,
1004 edge last_e = single_exit (loop);
1005 edge to_body = single_succ_edge (loop->header);
1006 basic_block after = to_body->dest;
1008 /* Create a basic block for loop close phi nodes. */
1009 last_e = single_succ_edge (split_edge (last_e));
1011 /* Translate the body of the loop. */
1012 next_e = translate_clast (region, loop, stmt->body, to_body,
1013 newivs, newivs_index, bb_pbb_mapping, level + 1,
1015 redirect_edge_succ_nodup (next_e, after);
1016 set_immediate_dominator (CDI_DOMINATORS, next_e->dest, next_e->src);
1018 if (flag_loop_parallelize_all
1019 && !dependency_in_loop_p (loop, bb_pbb_mapping,
1020 get_scattering_level (level)))
1021 loop->can_be_parallel = true;
1026 /* Translates a clast for statement STMT to gimple. First a guard is created
1027 protecting the loop, if it is executed zero times. In this guard we create
1028 the real loop structure.
1030 - REGION is the sese region we used to generate the scop.
1031 - NEXT_E is the edge where new generated code should be attached.
1032 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
1033 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
1036 translate_clast_for (sese region, loop_p context_loop, struct clast_for *stmt,
1037 edge next_e, VEC (tree, heap) **newivs,
1038 htab_t newivs_index, htab_t bb_pbb_mapping, int level,
1039 htab_t params_index)
1041 edge last_e = graphite_create_new_loop_guard (region, next_e, stmt, *newivs,
1042 newivs_index, params_index);
1043 edge true_e = get_true_edge_from_guard_bb (next_e->dest);
1045 translate_clast_for_loop (region, context_loop, stmt, true_e, newivs,
1046 newivs_index, bb_pbb_mapping, level,
1051 /* Translates a clast guard statement STMT to gimple.
1053 - REGION is the sese region we used to generate the scop.
1054 - NEXT_E is the edge where new generated code should be attached.
1055 - CONTEXT_LOOP is the loop in which the generated code will be placed
1056 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
1057 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
1060 translate_clast_guard (sese region, loop_p context_loop,
1061 struct clast_guard *stmt, edge next_e,
1062 VEC (tree, heap) **newivs,
1063 htab_t newivs_index, htab_t bb_pbb_mapping, int level,
1064 htab_t params_index)
1066 edge last_e = graphite_create_new_guard (region, next_e, stmt, *newivs,
1067 newivs_index, params_index);
1068 edge true_e = get_true_edge_from_guard_bb (next_e->dest);
1070 translate_clast (region, context_loop, stmt->then, true_e,
1071 newivs, newivs_index, bb_pbb_mapping,
1072 level, params_index);
1076 /* Translates a CLAST statement STMT to GCC representation in the
1079 - NEXT_E is the edge where new generated code should be attached.
1080 - CONTEXT_LOOP is the loop in which the generated code will be placed
1081 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping. */
1083 translate_clast (sese region, loop_p context_loop, struct clast_stmt *stmt,
1084 edge next_e, VEC (tree, heap) **newivs,
1085 htab_t newivs_index, htab_t bb_pbb_mapping, int level,
1086 htab_t params_index)
1091 if (CLAST_STMT_IS_A (stmt, stmt_root))
1094 else if (CLAST_STMT_IS_A (stmt, stmt_user))
1095 next_e = translate_clast_user (region, (struct clast_user_stmt *) stmt,
1096 next_e, newivs, newivs_index,
1097 bb_pbb_mapping, params_index);
1099 else if (CLAST_STMT_IS_A (stmt, stmt_for))
1100 next_e = translate_clast_for (region, context_loop,
1101 (struct clast_for *) stmt, next_e,
1102 newivs, newivs_index,
1103 bb_pbb_mapping, level, params_index);
1105 else if (CLAST_STMT_IS_A (stmt, stmt_guard))
1106 next_e = translate_clast_guard (region, context_loop,
1107 (struct clast_guard *) stmt, next_e,
1108 newivs, newivs_index,
1109 bb_pbb_mapping, level, params_index);
1111 else if (CLAST_STMT_IS_A (stmt, stmt_block))
1112 next_e = translate_clast (region, context_loop,
1113 ((struct clast_block *) stmt)->body,
1114 next_e, newivs, newivs_index,
1115 bb_pbb_mapping, level, params_index);
1119 recompute_all_dominators ();
1122 return translate_clast (region, context_loop, stmt->next, next_e,
1123 newivs, newivs_index,
1124 bb_pbb_mapping, level, params_index);
1127 /* Free the SCATTERING domain list. */
1130 free_scattering (CloogDomainList *scattering)
1134 CloogDomain *dom = cloog_domain (scattering);
1135 CloogDomainList *next = cloog_next_domain (scattering);
1137 cloog_domain_free (dom);
1143 /* Initialize Cloog's parameter names from the names used in GIMPLE.
1144 Initialize Cloog's iterator names, using 'graphite_iterator_%d'
1145 from 0 to scop_nb_loops (scop). */
1148 initialize_cloog_names (scop_p scop, CloogProgram *prog)
1150 sese region = SCOP_REGION (scop);
1152 int nb_iterators = scop_max_loop_depth (scop);
1153 int nb_scattering = cloog_program_nb_scattdims (prog);
1154 int nb_parameters = VEC_length (tree, SESE_PARAMS (region));
1155 char **iterators = XNEWVEC (char *, nb_iterators * 2);
1156 char **scattering = XNEWVEC (char *, nb_scattering);
1157 char **parameters= XNEWVEC (char *, nb_parameters);
1159 cloog_program_set_names (prog, cloog_names_malloc ());
1161 for (i = 0; i < nb_parameters; i++)
1163 tree param = VEC_index (tree, SESE_PARAMS(region), i);
1164 const char *name = get_name (param);
1170 len = strlen (name);
1172 parameters[i] = XNEWVEC (char, len + 1);
1173 snprintf (parameters[i], len, "%s_%d", name, SSA_NAME_VERSION (param));
1176 cloog_names_set_nb_parameters (cloog_program_names (prog), nb_parameters);
1177 cloog_names_set_parameters (cloog_program_names (prog), parameters);
1179 for (i = 0; i < nb_iterators; i++)
1182 iterators[i] = XNEWVEC (char, len);
1183 snprintf (iterators[i], len, "git_%d", i);
1186 cloog_names_set_nb_iterators (cloog_program_names (prog),
1188 cloog_names_set_iterators (cloog_program_names (prog),
1191 for (i = 0; i < nb_scattering; i++)
1194 scattering[i] = XNEWVEC (char, len);
1195 snprintf (scattering[i], len, "scat_%d", i);
1198 cloog_names_set_nb_scattering (cloog_program_names (prog),
1200 cloog_names_set_scattering (cloog_program_names (prog),
1204 /* Build cloog program for SCoP. */
1207 build_cloog_prog (scop_p scop, CloogProgram *prog,
1208 CloogOptions *options, CloogState *state ATTRIBUTE_UNUSED)
1211 int max_nb_loops = scop_max_loop_depth (scop);
1213 CloogLoop *loop_list = NULL;
1214 CloogBlockList *block_list = NULL;
1215 CloogDomainList *scattering = NULL;
1216 int nbs = 2 * max_nb_loops + 1;
1219 cloog_program_set_context
1220 (prog, new_Cloog_Domain_from_ppl_Pointset_Powerset (SCOP_CONTEXT (scop),
1221 scop_nb_params (scop), state));
1222 nbs = unify_scattering_dimensions (scop);
1223 scaldims = (int *) xmalloc (nbs * (sizeof (int)));
1224 cloog_program_set_nb_scattdims (prog, nbs);
1225 initialize_cloog_names (scop, prog);
1227 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
1229 CloogStatement *stmt;
1233 /* Dead code elimination: when the domain of a PBB is empty,
1234 don't generate code for the PBB. */
1235 if (ppl_Pointset_Powerset_C_Polyhedron_is_empty (PBB_DOMAIN (pbb)))
1238 /* Build the new statement and its block. */
1239 stmt = cloog_statement_alloc (state, pbb_index (pbb));
1240 dom = new_Cloog_Domain_from_ppl_Pointset_Powerset (PBB_DOMAIN (pbb),
1241 scop_nb_params (scop),
1243 block = cloog_block_alloc (stmt, 0, NULL, pbb_dim_iter_domain (pbb));
1244 cloog_statement_set_usr (stmt, pbb);
1246 /* Build loop list. */
1248 CloogLoop *new_loop_list = cloog_loop_malloc (state);
1249 cloog_loop_set_next (new_loop_list, loop_list);
1250 cloog_loop_set_domain (new_loop_list, dom);
1251 cloog_loop_set_block (new_loop_list, block);
1252 loop_list = new_loop_list;
1255 /* Build block list. */
1257 CloogBlockList *new_block_list = cloog_block_list_malloc ();
1259 cloog_block_list_set_next (new_block_list, block_list);
1260 cloog_block_list_set_block (new_block_list, block);
1261 block_list = new_block_list;
1264 /* Build scattering list. */
1266 /* XXX: Replace with cloog_domain_list_alloc(), when available. */
1267 CloogDomainList *new_scattering
1268 = (CloogDomainList *) xmalloc (sizeof (CloogDomainList));
1269 ppl_Polyhedron_t scat;
1272 scat = PBB_TRANSFORMED_SCATTERING (pbb);
1273 dom = new_Cloog_Domain_from_ppl_Polyhedron (scat, scop_nb_params (scop),
1276 cloog_set_next_domain (new_scattering, scattering);
1277 cloog_set_domain (new_scattering, dom);
1278 scattering = new_scattering;
1282 cloog_program_set_loop (prog, loop_list);
1283 cloog_program_set_blocklist (prog, block_list);
1285 for (i = 0; i < nbs; i++)
1288 cloog_program_set_scaldims (prog, scaldims);
1290 /* Extract scalar dimensions to simplify the code generation problem. */
1291 cloog_program_extract_scalars (prog, scattering, options);
1293 /* Apply scattering. */
1294 cloog_program_scatter (prog, scattering, options);
1295 free_scattering (scattering);
1297 /* Iterators corresponding to scalar dimensions have to be extracted. */
1298 cloog_names_scalarize (cloog_program_names (prog), nbs,
1299 cloog_program_scaldims (prog));
1301 /* Free blocklist. */
1303 CloogBlockList *next = cloog_program_blocklist (prog);
1307 CloogBlockList *toDelete = next;
1308 next = cloog_block_list_next (next);
1309 cloog_block_list_set_next (toDelete, NULL);
1310 cloog_block_list_set_block (toDelete, NULL);
1311 cloog_block_list_free (toDelete);
1313 cloog_program_set_blocklist (prog, NULL);
1317 /* Return the options that will be used in GLOOG. */
1319 static CloogOptions *
1320 set_cloog_options (CloogState *state ATTRIBUTE_UNUSED)
1322 CloogOptions *options = cloog_options_malloc (state);
1324 /* Change cloog output language to C. If we do use FORTRAN instead, cloog
1325 will stop e.g. with "ERROR: unbounded loops not allowed in FORTRAN.", if
1326 we pass an incomplete program to cloog. */
1327 options->language = LANGUAGE_C;
1329 /* Enable complex equality spreading: removes dummy statements
1330 (assignments) in the generated code which repeats the
1331 substitution equations for statements. This is useless for
1336 /* Silence CLooG to avoid failing tests due to debug output to stderr. */
1339 /* Enable C pretty-printing mode: normalizes the substitution
1340 equations for statements. */
1344 /* Allow cloog to build strides with a stride width different to one.
1345 This example has stride = 4:
1347 for (i = 0; i < 20; i += 4)
1349 options->strides = 1;
1351 /* Disable optimizations and make cloog generate source code closer to the
1352 input. This is useful for debugging, but later we want the optimized
1355 XXX: We can not disable optimizations, as loop blocking is not working
1360 options->l = INT_MAX;
1366 /* Prints STMT to STDERR. */
1369 print_clast_stmt (FILE *file, struct clast_stmt *stmt)
1371 CloogState *state = cloog_state_malloc ();
1372 CloogOptions *options = set_cloog_options (state);
1374 clast_pprint (file, stmt, 0, options);
1375 cloog_options_free (options);
1376 cloog_state_free (state);
1379 /* Prints STMT to STDERR. */
1382 debug_clast_stmt (struct clast_stmt *stmt)
1384 print_clast_stmt (stderr, stmt);
1387 /* Translate SCOP to a CLooG program and clast. These two
1388 representations should be freed together: a clast cannot be used
1389 without a program. */
1392 scop_to_clast (scop_p scop, CloogState *state)
1394 CloogOptions *options = set_cloog_options (state);
1395 cloog_prog_clast pc;
1397 /* Connect new cloog prog generation to graphite. */
1398 pc.prog = cloog_program_malloc ();
1399 build_cloog_prog (scop, pc.prog, options, state);
1400 pc.prog = cloog_program_generate (pc.prog, options);
1401 pc.stmt = cloog_clast_create (pc.prog, options);
1403 cloog_options_free (options);
1407 /* Prints to FILE the code generated by CLooG for SCOP. */
1410 print_generated_program (FILE *file, scop_p scop)
1412 CloogState *state = cloog_state_malloc ();
1413 CloogOptions *options = set_cloog_options (state);
1415 cloog_prog_clast pc = scop_to_clast (scop, state);
1417 fprintf (file, " (prog: \n");
1418 cloog_program_print (file, pc.prog);
1419 fprintf (file, " )\n");
1421 fprintf (file, " (clast: \n");
1422 clast_pprint (file, pc.stmt, 0, options);
1423 fprintf (file, " )\n");
1425 cloog_options_free (options);
1426 cloog_clast_free (pc.stmt);
1427 cloog_program_free (pc.prog);
1430 /* Prints to STDERR the code generated by CLooG for SCOP. */
1433 debug_generated_program (scop_p scop)
1435 print_generated_program (stderr, scop);
1438 /* Add CLooG names to parameter index. The index is used to translate
1439 back from CLooG names to GCC trees. */
1442 create_params_index (htab_t index_table, CloogProgram *prog) {
1443 CloogNames* names = cloog_program_names (prog);
1444 int nb_parameters = cloog_names_nb_parameters (names);
1445 char **parameters = cloog_names_parameters (names);
1448 for (i = 0; i < nb_parameters; i++)
1449 save_clast_name_index (index_table, parameters[i], i);
1452 /* GIMPLE Loop Generator: generates loops from STMT in GIMPLE form for
1453 the given SCOP. Return true if code generation succeeded.
1454 BB_PBB_MAPPING is a basic_block and it's related poly_bb_p mapping.
1458 gloog (scop_p scop, htab_t bb_pbb_mapping)
1460 VEC (tree, heap) *newivs = VEC_alloc (tree, heap, 10);
1461 loop_p context_loop;
1462 sese region = SCOP_REGION (scop);
1463 ifsese if_region = NULL;
1464 htab_t newivs_index, params_index;
1465 cloog_prog_clast pc;
1468 state = cloog_state_malloc ();
1469 timevar_push (TV_GRAPHITE_CODE_GEN);
1470 gloog_error = false;
1472 pc = scop_to_clast (scop, state);
1474 if (dump_file && (dump_flags & TDF_DETAILS))
1476 fprintf (dump_file, "\nCLAST generated by CLooG: \n");
1477 print_clast_stmt (dump_file, pc.stmt);
1478 fprintf (dump_file, "\n");
1481 recompute_all_dominators ();
1484 if_region = move_sese_in_condition (region);
1485 sese_insert_phis_for_liveouts (region,
1486 if_region->region->exit->src,
1487 if_region->false_region->exit,
1488 if_region->true_region->exit);
1489 recompute_all_dominators ();
1492 context_loop = SESE_ENTRY (region)->src->loop_father;
1493 newivs_index = htab_create (10, clast_name_index_elt_info,
1494 eq_clast_name_indexes, free);
1495 params_index = htab_create (10, clast_name_index_elt_info,
1496 eq_clast_name_indexes, free);
1498 create_params_index (params_index, pc.prog);
1500 translate_clast (region, context_loop, pc.stmt,
1501 if_region->true_region->entry,
1502 &newivs, newivs_index,
1503 bb_pbb_mapping, 1, params_index);
1506 recompute_all_dominators ();
1510 set_ifsese_condition (if_region, integer_zero_node);
1512 free (if_region->true_region);
1513 free (if_region->region);
1516 htab_delete (newivs_index);
1517 htab_delete (params_index);
1518 VEC_free (tree, heap, newivs);
1519 cloog_clast_free (pc.stmt);
1520 cloog_program_free (pc.prog);
1521 timevar_pop (TV_GRAPHITE_CODE_GEN);
1523 if (dump_file && (dump_flags & TDF_DETAILS))
1527 int num_no_dependency = 0;
1529 FOR_EACH_LOOP (li, loop, 0)
1530 if (loop->can_be_parallel)
1531 num_no_dependency++;
1533 fprintf (dump_file, "\n%d loops carried no dependency.\n",
1537 cloog_state_free (state);
1539 return !gloog_error;