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). */
939 graphite_create_new_loop_guard (sese region
, edge entry_edge
,
940 struct clast_for
*stmt
,
941 VEC (tree
, heap
) *newivs
,
942 htab_t newivs_index
, htab_t params_index
)
946 tree lb_type
= gcc_type_for_clast_expr (stmt
->LB
, region
, newivs
,
947 newivs_index
, params_index
);
948 tree ub_type
= gcc_type_for_clast_expr (stmt
->UB
, region
, newivs
,
949 newivs_index
, params_index
);
950 tree type
= max_precision_type (lb_type
, ub_type
);
951 tree lb
= clast_to_gcc_expression (type
, stmt
->LB
, region
, newivs
,
952 newivs_index
, params_index
);
953 tree ub
= clast_to_gcc_expression (type
, stmt
->UB
, region
, newivs
,
954 newivs_index
, params_index
);
957 /* Adding +1 and using LT_EXPR helps with loop latches that have a
958 loop iteration count of "PARAMETER - 1". For PARAMETER == 0 this becomes
959 2^{32|64}, and the condition lb <= ub is true, even if we do not want this.
960 However lb < ub + 1 is false, as expected. */
961 if (POINTER_TYPE_P (type
))
962 ub_one
= fold_build2 (POINTER_PLUS_EXPR
, type
, ub
, size_one_node
);
964 ub_one
= fold_build2 (PLUS_EXPR
, type
, ub
,
965 fold_convert (type
, integer_one_node
));
967 /* When ub + 1 wraps around, use lb <= ub. */
968 if (integer_zerop (ub_one
))
969 cond_expr
= fold_build2 (LE_EXPR
, boolean_type_node
, lb
, ub
);
971 cond_expr
= fold_build2 (LT_EXPR
, boolean_type_node
, lb
, ub_one
);
973 exit_edge
= create_empty_if_region_on_edge (entry_edge
, cond_expr
);
979 translate_clast (sese
, loop_p
, struct clast_stmt
*, edge
,
980 VEC (tree
, heap
) **, htab_t
, htab_t
, int, htab_t
);
982 /* Create the loop for a clast for statement.
984 - REGION is the sese region we used to generate the scop.
985 - NEXT_E is the edge where new generated code should be attached.
986 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
987 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
990 translate_clast_for_loop (sese region
, loop_p context_loop
,
991 struct clast_for
*stmt
, edge next_e
,
992 VEC (tree
, heap
) **newivs
,
993 htab_t newivs_index
, htab_t bb_pbb_mapping
,
994 int level
, htab_t params_index
)
996 struct loop
*loop
= graphite_create_new_loop (region
, next_e
, stmt
,
997 context_loop
, newivs
,
998 newivs_index
, params_index
,
1000 edge last_e
= single_exit (loop
);
1001 edge to_body
= single_succ_edge (loop
->header
);
1002 basic_block after
= to_body
->dest
;
1004 /* Create a basic block for loop close phi nodes. */
1005 last_e
= single_succ_edge (split_edge (last_e
));
1007 /* Translate the body of the loop. */
1008 next_e
= translate_clast (region
, loop
, stmt
->body
, to_body
,
1009 newivs
, newivs_index
, bb_pbb_mapping
, level
+ 1,
1011 redirect_edge_succ_nodup (next_e
, after
);
1012 set_immediate_dominator (CDI_DOMINATORS
, next_e
->dest
, next_e
->src
);
1014 if (flag_loop_parallelize_all
1015 && !dependency_in_loop_p (loop
, bb_pbb_mapping
,
1016 get_scattering_level (level
)))
1017 loop
->can_be_parallel
= true;
1022 /* Translates a clast for statement STMT to gimple. First a guard is created
1023 protecting the loop, if it is executed zero times. In this guard we create
1024 the real loop structure.
1026 - REGION is the sese region we used to generate the scop.
1027 - NEXT_E is the edge where new generated code should be attached.
1028 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
1029 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
1032 translate_clast_for (sese region
, loop_p context_loop
, struct clast_for
*stmt
,
1033 edge next_e
, VEC (tree
, heap
) **newivs
,
1034 htab_t newivs_index
, htab_t bb_pbb_mapping
, int level
,
1035 htab_t params_index
)
1037 edge last_e
= graphite_create_new_loop_guard (region
, next_e
, stmt
, *newivs
,
1038 newivs_index
, params_index
);
1039 edge true_e
= get_true_edge_from_guard_bb (next_e
->dest
);
1041 translate_clast_for_loop (region
, context_loop
, stmt
, true_e
, newivs
,
1042 newivs_index
, bb_pbb_mapping
, level
,
1047 /* Translates a clast guard statement STMT to gimple.
1049 - REGION is the sese region we used to generate the scop.
1050 - NEXT_E is the edge where new generated code should be attached.
1051 - CONTEXT_LOOP is the loop in which the generated code will be placed
1052 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
1053 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
1056 translate_clast_guard (sese region
, loop_p context_loop
,
1057 struct clast_guard
*stmt
, edge next_e
,
1058 VEC (tree
, heap
) **newivs
,
1059 htab_t newivs_index
, htab_t bb_pbb_mapping
, int level
,
1060 htab_t params_index
)
1062 edge last_e
= graphite_create_new_guard (region
, next_e
, stmt
, *newivs
,
1063 newivs_index
, params_index
);
1064 edge true_e
= get_true_edge_from_guard_bb (next_e
->dest
);
1066 translate_clast (region
, context_loop
, stmt
->then
, true_e
,
1067 newivs
, newivs_index
, bb_pbb_mapping
,
1068 level
, params_index
);
1072 /* Translates a CLAST statement STMT to GCC representation in the
1075 - NEXT_E is the edge where new generated code should be attached.
1076 - CONTEXT_LOOP is the loop in which the generated code will be placed
1077 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping. */
1079 translate_clast (sese region
, loop_p context_loop
, struct clast_stmt
*stmt
,
1080 edge next_e
, VEC (tree
, heap
) **newivs
,
1081 htab_t newivs_index
, htab_t bb_pbb_mapping
, int level
,
1082 htab_t params_index
)
1087 if (CLAST_STMT_IS_A (stmt
, stmt_root
))
1090 else if (CLAST_STMT_IS_A (stmt
, stmt_user
))
1091 next_e
= translate_clast_user (region
, (struct clast_user_stmt
*) stmt
,
1092 next_e
, newivs
, newivs_index
,
1093 bb_pbb_mapping
, params_index
);
1095 else if (CLAST_STMT_IS_A (stmt
, stmt_for
))
1096 next_e
= translate_clast_for (region
, context_loop
,
1097 (struct clast_for
*) stmt
, next_e
,
1098 newivs
, newivs_index
,
1099 bb_pbb_mapping
, level
, params_index
);
1101 else if (CLAST_STMT_IS_A (stmt
, stmt_guard
))
1102 next_e
= translate_clast_guard (region
, context_loop
,
1103 (struct clast_guard
*) stmt
, next_e
,
1104 newivs
, newivs_index
,
1105 bb_pbb_mapping
, level
, params_index
);
1107 else if (CLAST_STMT_IS_A (stmt
, stmt_block
))
1108 next_e
= translate_clast (region
, context_loop
,
1109 ((struct clast_block
*) stmt
)->body
,
1110 next_e
, newivs
, newivs_index
,
1111 bb_pbb_mapping
, level
, params_index
);
1115 recompute_all_dominators ();
1118 return translate_clast (region
, context_loop
, stmt
->next
, next_e
,
1119 newivs
, newivs_index
,
1120 bb_pbb_mapping
, level
, params_index
);
1123 /* Free the SCATTERING domain list. */
1126 free_scattering (CloogDomainList
*scattering
)
1130 CloogDomain
*dom
= cloog_domain (scattering
);
1131 CloogDomainList
*next
= cloog_next_domain (scattering
);
1133 cloog_domain_free (dom
);
1139 /* Initialize Cloog's parameter names from the names used in GIMPLE.
1140 Initialize Cloog's iterator names, using 'graphite_iterator_%d'
1141 from 0 to scop_nb_loops (scop). */
1144 initialize_cloog_names (scop_p scop
, CloogProgram
*prog
)
1146 sese region
= SCOP_REGION (scop
);
1148 int nb_iterators
= scop_max_loop_depth (scop
);
1149 int nb_scattering
= cloog_program_nb_scattdims (prog
);
1150 int nb_parameters
= VEC_length (tree
, SESE_PARAMS (region
));
1151 char **iterators
= XNEWVEC (char *, nb_iterators
* 2);
1152 char **scattering
= XNEWVEC (char *, nb_scattering
);
1153 char **parameters
= XNEWVEC (char *, nb_parameters
);
1155 cloog_program_set_names (prog
, cloog_names_malloc ());
1157 for (i
= 0; i
< nb_parameters
; i
++)
1159 tree param
= VEC_index (tree
, SESE_PARAMS(region
), i
);
1160 const char *name
= get_name (param
);
1166 len
= strlen (name
);
1168 parameters
[i
] = XNEWVEC (char, len
+ 1);
1169 snprintf (parameters
[i
], len
, "%s_%d", name
, SSA_NAME_VERSION (param
));
1172 cloog_names_set_nb_parameters (cloog_program_names (prog
), nb_parameters
);
1173 cloog_names_set_parameters (cloog_program_names (prog
), parameters
);
1175 for (i
= 0; i
< nb_iterators
; i
++)
1178 iterators
[i
] = XNEWVEC (char, len
);
1179 snprintf (iterators
[i
], len
, "git_%d", i
);
1182 cloog_names_set_nb_iterators (cloog_program_names (prog
),
1184 cloog_names_set_iterators (cloog_program_names (prog
),
1187 for (i
= 0; i
< nb_scattering
; i
++)
1190 scattering
[i
] = XNEWVEC (char, len
);
1191 snprintf (scattering
[i
], len
, "scat_%d", i
);
1194 cloog_names_set_nb_scattering (cloog_program_names (prog
),
1196 cloog_names_set_scattering (cloog_program_names (prog
),
1200 /* Build cloog program for SCoP. */
1203 build_cloog_prog (scop_p scop
, CloogProgram
*prog
)
1206 int max_nb_loops
= scop_max_loop_depth (scop
);
1208 CloogLoop
*loop_list
= NULL
;
1209 CloogBlockList
*block_list
= NULL
;
1210 CloogDomainList
*scattering
= NULL
;
1211 int nbs
= 2 * max_nb_loops
+ 1;
1214 cloog_program_set_context
1215 (prog
, new_Cloog_Domain_from_ppl_Pointset_Powerset (SCOP_CONTEXT (scop
)));
1216 nbs
= unify_scattering_dimensions (scop
);
1217 scaldims
= (int *) xmalloc (nbs
* (sizeof (int)));
1218 cloog_program_set_nb_scattdims (prog
, nbs
);
1219 initialize_cloog_names (scop
, prog
);
1221 for (i
= 0; VEC_iterate (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
); i
++)
1223 CloogStatement
*stmt
;
1226 /* Dead code elimination: when the domain of a PBB is empty,
1227 don't generate code for the PBB. */
1228 if (ppl_Pointset_Powerset_C_Polyhedron_is_empty (PBB_DOMAIN (pbb
)))
1231 /* Build the new statement and its block. */
1232 stmt
= cloog_statement_alloc (pbb_index (pbb
));
1233 block
= cloog_block_alloc (stmt
, 0, NULL
, pbb_dim_iter_domain (pbb
));
1234 cloog_statement_set_usr (stmt
, pbb
);
1236 /* Build loop list. */
1238 CloogLoop
*new_loop_list
= cloog_loop_malloc ();
1239 cloog_loop_set_next (new_loop_list
, loop_list
);
1240 cloog_loop_set_domain
1242 new_Cloog_Domain_from_ppl_Pointset_Powerset (PBB_DOMAIN (pbb
)));
1243 cloog_loop_set_block (new_loop_list
, block
);
1244 loop_list
= new_loop_list
;
1247 /* Build block list. */
1249 CloogBlockList
*new_block_list
= cloog_block_list_malloc ();
1251 cloog_block_list_set_next (new_block_list
, block_list
);
1252 cloog_block_list_set_block (new_block_list
, block
);
1253 block_list
= new_block_list
;
1256 /* Build scattering list. */
1258 /* XXX: Replace with cloog_domain_list_alloc(), when available. */
1259 CloogDomainList
*new_scattering
1260 = (CloogDomainList
*) xmalloc (sizeof (CloogDomainList
));
1261 ppl_Polyhedron_t scat
;
1264 scat
= PBB_TRANSFORMED_SCATTERING (pbb
);
1265 dom
= new_Cloog_Domain_from_ppl_Polyhedron (scat
);
1267 cloog_set_next_domain (new_scattering
, scattering
);
1268 cloog_set_domain (new_scattering
, dom
);
1269 scattering
= new_scattering
;
1273 cloog_program_set_loop (prog
, loop_list
);
1274 cloog_program_set_blocklist (prog
, block_list
);
1276 for (i
= 0; i
< nbs
; i
++)
1279 cloog_program_set_scaldims (prog
, scaldims
);
1281 /* Extract scalar dimensions to simplify the code generation problem. */
1282 cloog_program_extract_scalars (prog
, scattering
);
1284 /* Apply scattering. */
1285 cloog_program_scatter (prog
, scattering
);
1286 free_scattering (scattering
);
1288 /* Iterators corresponding to scalar dimensions have to be extracted. */
1289 cloog_names_scalarize (cloog_program_names (prog
), nbs
,
1290 cloog_program_scaldims (prog
));
1292 /* Free blocklist. */
1294 CloogBlockList
*next
= cloog_program_blocklist (prog
);
1298 CloogBlockList
*toDelete
= next
;
1299 next
= cloog_block_list_next (next
);
1300 cloog_block_list_set_next (toDelete
, NULL
);
1301 cloog_block_list_set_block (toDelete
, NULL
);
1302 cloog_block_list_free (toDelete
);
1304 cloog_program_set_blocklist (prog
, NULL
);
1308 /* Return the options that will be used in GLOOG. */
1310 static CloogOptions
*
1311 set_cloog_options (void)
1313 CloogOptions
*options
= cloog_options_malloc ();
1315 /* Change cloog output language to C. If we do use FORTRAN instead, cloog
1316 will stop e.g. with "ERROR: unbounded loops not allowed in FORTRAN.", if
1317 we pass an incomplete program to cloog. */
1318 options
->language
= LANGUAGE_C
;
1320 /* Enable complex equality spreading: removes dummy statements
1321 (assignments) in the generated code which repeats the
1322 substitution equations for statements. This is useless for
1326 /* Enable C pretty-printing mode: normalizes the substitution
1327 equations for statements. */
1330 /* Allow cloog to build strides with a stride width different to one.
1331 This example has stride = 4:
1333 for (i = 0; i < 20; i += 4)
1335 options
->strides
= 1;
1337 /* Disable optimizations and make cloog generate source code closer to the
1338 input. This is useful for debugging, but later we want the optimized
1341 XXX: We can not disable optimizations, as loop blocking is not working
1343 if (!flag_graphite_cloog_opts
)
1346 options
->l
= INT_MAX
;
1352 /* Prints STMT to STDERR. */
1355 print_clast_stmt (FILE *file
, struct clast_stmt
*stmt
)
1357 CloogOptions
*options
= set_cloog_options ();
1359 pprint (file
, stmt
, 0, options
);
1360 cloog_options_free (options
);
1363 /* Prints STMT to STDERR. */
1366 debug_clast_stmt (struct clast_stmt
*stmt
)
1368 print_clast_stmt (stderr
, stmt
);
1371 /* Translate SCOP to a CLooG program and clast. These two
1372 representations should be freed together: a clast cannot be used
1373 without a program. */
1376 scop_to_clast (scop_p scop
)
1378 CloogOptions
*options
= set_cloog_options ();
1379 cloog_prog_clast pc
;
1381 /* Connect new cloog prog generation to graphite. */
1382 pc
.prog
= cloog_program_malloc ();
1383 build_cloog_prog (scop
, pc
.prog
);
1384 pc
.prog
= cloog_program_generate (pc
.prog
, options
);
1385 pc
.stmt
= cloog_clast_create (pc
.prog
, options
);
1387 cloog_options_free (options
);
1391 /* Prints to FILE the code generated by CLooG for SCOP. */
1394 print_generated_program (FILE *file
, scop_p scop
)
1396 CloogOptions
*options
= set_cloog_options ();
1397 cloog_prog_clast pc
= scop_to_clast (scop
);
1399 fprintf (file
, " (prog: \n");
1400 cloog_program_print (file
, pc
.prog
);
1401 fprintf (file
, " )\n");
1403 fprintf (file
, " (clast: \n");
1404 pprint (file
, pc
.stmt
, 0, options
);
1405 fprintf (file
, " )\n");
1407 cloog_options_free (options
);
1408 cloog_clast_free (pc
.stmt
);
1409 cloog_program_free (pc
.prog
);
1412 /* Prints to STDERR the code generated by CLooG for SCOP. */
1415 debug_generated_program (scop_p scop
)
1417 print_generated_program (stderr
, scop
);
1420 /* Add CLooG names to parameter index. The index is used to translate
1421 back from CLooG names to GCC trees. */
1424 create_params_index (htab_t index_table
, CloogProgram
*prog
) {
1425 CloogNames
* names
= cloog_program_names (prog
);
1426 int nb_parameters
= cloog_names_nb_parameters (names
);
1427 char **parameters
= cloog_names_parameters (names
);
1430 for (i
= 0; i
< nb_parameters
; i
++)
1431 save_clast_name_index (index_table
, parameters
[i
], i
);
1434 /* GIMPLE Loop Generator: generates loops from STMT in GIMPLE form for
1435 the given SCOP. Return true if code generation succeeded.
1436 BB_PBB_MAPPING is a basic_block and it's related poly_bb_p mapping.
1440 gloog (scop_p scop
, htab_t bb_pbb_mapping
)
1442 VEC (tree
, heap
) *newivs
= VEC_alloc (tree
, heap
, 10);
1443 loop_p context_loop
;
1444 sese region
= SCOP_REGION (scop
);
1445 ifsese if_region
= NULL
;
1446 htab_t newivs_index
, params_index
;
1447 cloog_prog_clast pc
;
1449 timevar_push (TV_GRAPHITE_CODE_GEN
);
1450 gloog_error
= false;
1452 pc
= scop_to_clast (scop
);
1454 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1456 fprintf (dump_file
, "\nCLAST generated by CLooG: \n");
1457 print_clast_stmt (dump_file
, pc
.stmt
);
1458 fprintf (dump_file
, "\n");
1461 recompute_all_dominators ();
1464 if_region
= move_sese_in_condition (region
);
1465 sese_insert_phis_for_liveouts (region
,
1466 if_region
->region
->exit
->src
,
1467 if_region
->false_region
->exit
,
1468 if_region
->true_region
->exit
);
1469 recompute_all_dominators ();
1472 context_loop
= SESE_ENTRY (region
)->src
->loop_father
;
1473 newivs_index
= htab_create (10, clast_name_index_elt_info
,
1474 eq_clast_name_indexes
, free
);
1475 params_index
= htab_create (10, clast_name_index_elt_info
,
1476 eq_clast_name_indexes
, free
);
1478 create_params_index (params_index
, pc
.prog
);
1480 translate_clast (region
, context_loop
, pc
.stmt
,
1481 if_region
->true_region
->entry
,
1482 &newivs
, newivs_index
,
1483 bb_pbb_mapping
, 1, params_index
);
1486 recompute_all_dominators ();
1490 set_ifsese_condition (if_region
, integer_zero_node
);
1492 free (if_region
->true_region
);
1493 free (if_region
->region
);
1496 htab_delete (newivs_index
);
1497 htab_delete (params_index
);
1498 VEC_free (tree
, heap
, newivs
);
1499 cloog_clast_free (pc
.stmt
);
1500 cloog_program_free (pc
.prog
);
1501 timevar_pop (TV_GRAPHITE_CODE_GEN
);
1503 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1507 int num_no_dependency
= 0;
1509 FOR_EACH_LOOP (li
, loop
, 0)
1510 if (loop
->can_be_parallel
)
1511 num_no_dependency
++;
1513 fprintf (dump_file
, "\n%d loops carried no dependency.\n",
1517 return !gloog_error
;