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"
57 /* This flag is set when an error occurred during the translation of
59 static bool gloog_error
;
61 /* Verifies properties that GRAPHITE should maintain during translation. */
64 graphite_verify (void)
66 #ifdef ENABLE_CHECKING
67 verify_loop_structure ();
68 verify_dominators (CDI_DOMINATORS
);
69 verify_dominators (CDI_POST_DOMINATORS
);
70 verify_loop_closed_ssa (true);
74 /* Stores the INDEX in a vector for a given clast NAME. */
76 typedef struct clast_name_index
{
79 } *clast_name_index_p
;
81 /* Returns a pointer to a new element of type clast_name_index_p built
82 from NAME and INDEX. */
84 static inline clast_name_index_p
85 new_clast_name_index (const char *name
, int index
)
87 clast_name_index_p res
= XNEW (struct clast_name_index
);
94 /* For a given clast NAME, returns -1 if it does not correspond to any
95 parameter, or otherwise, returns the index in the PARAMS or
96 SCATTERING_DIMENSIONS vector. */
99 clast_name_to_index (const char *name
, htab_t index_table
)
101 struct clast_name_index tmp
;
105 slot
= htab_find_slot (index_table
, &tmp
, NO_INSERT
);
108 return ((struct clast_name_index
*) *slot
)->index
;
113 /* Records in INDEX_TABLE the INDEX for NAME. */
116 save_clast_name_index (htab_t index_table
, const char *name
, int index
)
118 struct clast_name_index tmp
;
122 slot
= htab_find_slot (index_table
, &tmp
, INSERT
);
129 *slot
= new_clast_name_index (name
, index
);
133 /* Computes a hash function for database element ELT. */
135 static inline hashval_t
136 clast_name_index_elt_info (const void *elt
)
138 return htab_hash_pointer (((const struct clast_name_index
*) elt
)->name
);
141 /* Compares database elements E1 and E2. */
144 eq_clast_name_indexes (const void *e1
, const void *e2
)
146 const struct clast_name_index
*elt1
= (const struct clast_name_index
*) e1
;
147 const struct clast_name_index
*elt2
= (const struct clast_name_index
*) e2
;
149 return (elt1
->name
== elt2
->name
);
152 /* For a given scattering dimension, return the new induction variable
156 newivs_to_depth_to_newiv (VEC (tree
, heap
) *newivs
, int depth
)
158 return VEC_index (tree
, newivs
, depth
);
163 /* Returns the tree variable from the name NAME that was given in
164 Cloog representation. */
167 clast_name_to_gcc (const char *name
, sese region
, VEC (tree
, heap
) *newivs
,
168 htab_t newivs_index
, htab_t params_index
)
171 VEC (tree
, heap
) *params
= SESE_PARAMS (region
);
173 if (params
&& params_index
)
175 index
= clast_name_to_index (name
, params_index
);
178 return VEC_index (tree
, params
, index
);
181 gcc_assert (newivs
&& newivs_index
);
182 index
= clast_name_to_index (name
, newivs_index
);
183 gcc_assert (index
>= 0);
185 return newivs_to_depth_to_newiv (newivs
, index
);
188 /* Returns the signed maximal precision type for expressions TYPE1 and TYPE2. */
191 max_signed_precision_type (tree type1
, tree type2
)
193 int p1
= TYPE_PRECISION (type1
);
194 int p2
= TYPE_PRECISION (type2
);
199 precision
= TYPE_UNSIGNED (type1
) ? p1
* 2 : p1
;
201 precision
= TYPE_UNSIGNED (type2
) ? p2
* 2 : p2
;
203 type
= lang_hooks
.types
.type_for_size (precision
, false);
208 return integer_type_node
;
213 /* Returns the maximal precision type for expressions TYPE1 and TYPE2. */
216 max_precision_type (tree type1
, tree type2
)
218 if (POINTER_TYPE_P (type1
))
221 if (POINTER_TYPE_P (type2
))
224 if (!TYPE_UNSIGNED (type1
)
225 || !TYPE_UNSIGNED (type2
))
226 return max_signed_precision_type (type1
, type2
);
228 return TYPE_PRECISION (type1
) > TYPE_PRECISION (type2
) ? type1
: type2
;
232 clast_to_gcc_expression (tree
, struct clast_expr
*, sese
, VEC (tree
, heap
) *,
235 /* Converts a Cloog reduction expression R with reduction operation OP
236 to a GCC expression tree of type TYPE. */
239 clast_to_gcc_expression_red (tree type
, enum tree_code op
,
240 struct clast_reduction
*r
,
241 sese region
, VEC (tree
, heap
) *newivs
,
242 htab_t newivs_index
, htab_t params_index
)
245 tree res
= clast_to_gcc_expression (type
, r
->elts
[0], region
, newivs
,
246 newivs_index
, params_index
);
247 tree operand_type
= (op
== POINTER_PLUS_EXPR
) ? sizetype
: type
;
249 for (i
= 1; i
< r
->n
; i
++)
251 tree t
= clast_to_gcc_expression (operand_type
, r
->elts
[i
], region
,
252 newivs
, newivs_index
, params_index
);
253 res
= fold_build2 (op
, type
, res
, t
);
259 /* Converts a Cloog AST expression E back to a GCC expression tree of
263 clast_to_gcc_expression (tree type
, struct clast_expr
*e
,
264 sese region
, VEC (tree
, heap
) *newivs
,
265 htab_t newivs_index
, htab_t params_index
)
271 struct clast_term
*t
= (struct clast_term
*) e
;
275 if (mpz_cmp_si (t
->val
, 1) == 0)
277 tree name
= clast_name_to_gcc (t
->var
, region
, newivs
,
278 newivs_index
, params_index
);
280 if (POINTER_TYPE_P (TREE_TYPE (name
)) != POINTER_TYPE_P (type
))
281 name
= fold_convert (sizetype
, name
);
283 name
= fold_convert (type
, name
);
287 else if (mpz_cmp_si (t
->val
, -1) == 0)
289 tree name
= clast_name_to_gcc (t
->var
, region
, newivs
,
290 newivs_index
, params_index
);
292 if (POINTER_TYPE_P (TREE_TYPE (name
)) != POINTER_TYPE_P (type
))
293 name
= fold_convert (sizetype
, name
);
295 name
= fold_convert (type
, name
);
297 return fold_build1 (NEGATE_EXPR
, type
, name
);
301 tree name
= clast_name_to_gcc (t
->var
, region
, newivs
,
302 newivs_index
, params_index
);
303 tree cst
= gmp_cst_to_tree (type
, t
->val
);
305 if (POINTER_TYPE_P (TREE_TYPE (name
)) != POINTER_TYPE_P (type
))
306 name
= fold_convert (sizetype
, name
);
308 name
= fold_convert (type
, name
);
310 if (!POINTER_TYPE_P (type
))
311 return fold_build2 (MULT_EXPR
, type
, cst
, name
);
318 return gmp_cst_to_tree (type
, t
->val
);
323 struct clast_reduction
*r
= (struct clast_reduction
*) e
;
328 return clast_to_gcc_expression_red
329 (type
, POINTER_TYPE_P (type
) ? POINTER_PLUS_EXPR
: PLUS_EXPR
,
330 r
, region
, newivs
, newivs_index
, params_index
);
333 return clast_to_gcc_expression_red (type
, MIN_EXPR
, r
, region
,
334 newivs
, newivs_index
,
338 return clast_to_gcc_expression_red (type
, MAX_EXPR
, r
, region
,
339 newivs
, newivs_index
,
350 struct clast_binary
*b
= (struct clast_binary
*) e
;
351 struct clast_expr
*lhs
= (struct clast_expr
*) b
->LHS
;
352 tree tl
= clast_to_gcc_expression (type
, lhs
, region
, newivs
,
353 newivs_index
, params_index
);
354 tree tr
= gmp_cst_to_tree (type
, b
->RHS
);
359 return fold_build2 (FLOOR_DIV_EXPR
, type
, tl
, tr
);
362 return fold_build2 (CEIL_DIV_EXPR
, type
, tl
, tr
);
365 return fold_build2 (EXACT_DIV_EXPR
, type
, tl
, tr
);
368 return fold_build2 (TRUNC_MOD_EXPR
, type
, tl
, tr
);
382 /* Return the precision needed to represent the value VAL. */
385 precision_for_value (mpz_t val
)
401 while (mpz_cmp (y
, x
) > 0)
414 /* Return the precision needed to represent the values between LOW and
418 precision_for_interval (mpz_t low
, mpz_t up
)
423 gcc_assert (mpz_cmp (low
, up
) <= 0);
426 mpz_sub (diff
, up
, low
);
427 precision
= precision_for_value (diff
);
433 /* Return a type that could represent the integer value VAL. */
436 gcc_type_for_interval (mpz_t low
, mpz_t up
)
438 bool unsigned_p
= true;
439 int precision
, prec_up
, prec_int
;
441 enum machine_mode mode
;
443 gcc_assert (mpz_cmp (low
, up
) <= 0);
445 if (mpz_sgn (low
) < 0)
448 prec_up
= precision_for_value (up
);
449 prec_int
= precision_for_interval (low
, up
);
450 precision
= MAX (prec_up
, prec_int
);
452 if (precision
> BITS_PER_WORD
)
455 return integer_type_node
;
458 mode
= smallest_mode_for_size (precision
, MODE_INT
);
459 precision
= GET_MODE_PRECISION (mode
);
460 type
= build_nonstandard_integer_type (precision
, unsigned_p
);
465 return integer_type_node
;
471 /* Return a type that could represent the integer value VAL, or
472 otherwise return NULL_TREE. */
475 gcc_type_for_value (mpz_t val
)
477 return gcc_type_for_interval (val
, val
);
480 /* Return the type for the clast_term T used in STMT. */
483 gcc_type_for_clast_term (struct clast_term
*t
,
484 sese region
, VEC (tree
, heap
) *newivs
,
485 htab_t newivs_index
, htab_t params_index
)
487 gcc_assert (t
->expr
.type
== expr_term
);
490 return gcc_type_for_value (t
->val
);
492 return TREE_TYPE (clast_name_to_gcc (t
->var
, region
, newivs
,
493 newivs_index
, params_index
));
497 gcc_type_for_clast_expr (struct clast_expr
*, sese
,
498 VEC (tree
, heap
) *, htab_t
, htab_t
);
500 /* Return the type for the clast_reduction R used in STMT. */
503 gcc_type_for_clast_red (struct clast_reduction
*r
, sese region
,
504 VEC (tree
, heap
) *newivs
,
505 htab_t newivs_index
, htab_t params_index
)
508 tree type
= NULL_TREE
;
511 return gcc_type_for_clast_expr (r
->elts
[0], region
, newivs
,
512 newivs_index
, params_index
);
519 type
= gcc_type_for_clast_expr (r
->elts
[0], region
, newivs
,
520 newivs_index
, params_index
);
521 for (i
= 1; i
< r
->n
; i
++)
522 type
= max_precision_type (type
, gcc_type_for_clast_expr
523 (r
->elts
[i
], region
, newivs
,
524 newivs_index
, params_index
));
536 /* Return the type for the clast_binary B used in STMT. */
539 gcc_type_for_clast_bin (struct clast_binary
*b
,
540 sese region
, VEC (tree
, heap
) *newivs
,
541 htab_t newivs_index
, htab_t params_index
)
543 tree l
= gcc_type_for_clast_expr ((struct clast_expr
*) b
->LHS
, region
,
544 newivs
, newivs_index
, params_index
);
545 tree r
= gcc_type_for_value (b
->RHS
);
546 return max_signed_precision_type (l
, r
);
549 /* Returns the type for the CLAST expression E when used in statement
553 gcc_type_for_clast_expr (struct clast_expr
*e
,
554 sese region
, VEC (tree
, heap
) *newivs
,
555 htab_t newivs_index
, htab_t params_index
)
560 return gcc_type_for_clast_term ((struct clast_term
*) e
, region
,
561 newivs
, newivs_index
, params_index
);
564 return gcc_type_for_clast_red ((struct clast_reduction
*) e
, region
,
565 newivs
, newivs_index
, params_index
);
568 return gcc_type_for_clast_bin ((struct clast_binary
*) e
, region
,
569 newivs
, newivs_index
, params_index
);
578 /* Returns the type for the equation CLEQ. */
581 gcc_type_for_clast_eq (struct clast_equation
*cleq
,
582 sese region
, VEC (tree
, heap
) *newivs
,
583 htab_t newivs_index
, htab_t params_index
)
585 tree l
= gcc_type_for_clast_expr (cleq
->LHS
, region
, newivs
,
586 newivs_index
, params_index
);
587 tree r
= gcc_type_for_clast_expr (cleq
->RHS
, region
, newivs
,
588 newivs_index
, params_index
);
589 return max_precision_type (l
, r
);
592 /* Translates a clast equation CLEQ to a tree. */
595 graphite_translate_clast_equation (sese region
,
596 struct clast_equation
*cleq
,
597 VEC (tree
, heap
) *newivs
,
598 htab_t newivs_index
, htab_t params_index
)
601 tree type
= gcc_type_for_clast_eq (cleq
, region
, newivs
, newivs_index
,
603 tree lhs
= clast_to_gcc_expression (type
, cleq
->LHS
, region
, newivs
,
604 newivs_index
, params_index
);
605 tree rhs
= clast_to_gcc_expression (type
, cleq
->RHS
, region
, newivs
,
606 newivs_index
, params_index
);
611 else if (cleq
->sign
> 0)
617 return fold_build2 (comp
, boolean_type_node
, lhs
, rhs
);
620 /* Creates the test for the condition in STMT. */
623 graphite_create_guard_cond_expr (sese region
, struct clast_guard
*stmt
,
624 VEC (tree
, heap
) *newivs
,
625 htab_t newivs_index
, htab_t params_index
)
630 for (i
= 0; i
< stmt
->n
; i
++)
632 tree eq
= graphite_translate_clast_equation (region
, &stmt
->eq
[i
],
633 newivs
, newivs_index
,
637 cond
= fold_build2 (TRUTH_AND_EXPR
, TREE_TYPE (eq
), cond
, eq
);
645 /* Creates a new if region corresponding to Cloog's guard. */
648 graphite_create_new_guard (sese region
, edge entry_edge
,
649 struct clast_guard
*stmt
,
650 VEC (tree
, heap
) *newivs
,
651 htab_t newivs_index
, htab_t params_index
)
653 tree cond_expr
= graphite_create_guard_cond_expr (region
, stmt
, newivs
,
654 newivs_index
, params_index
);
655 edge exit_edge
= create_empty_if_region_on_edge (entry_edge
, cond_expr
);
659 /* Compute the lower bound LOW and upper bound UP for the induction
660 variable at LEVEL for the statement PBB, based on the transformed
661 scattering of PBB: T|I|G|Cst, with T the scattering transform, I
662 the iteration domain, and G the context parameters. */
665 compute_bounds_for_level (poly_bb_p pbb
, int level
, mpz_t low
, mpz_t up
)
667 ppl_Pointset_Powerset_C_Polyhedron_t ps
;
668 ppl_Linear_Expression_t le
;
670 combine_context_id_scat (&ps
, pbb
, false);
672 /* Prepare the linear expression corresponding to the level that we
673 want to maximize/minimize. */
675 ppl_dimension_type dim
= pbb_nb_scattering_transform (pbb
)
676 + pbb_dim_iter_domain (pbb
) + pbb_nb_params (pbb
);
678 ppl_new_Linear_Expression_with_dimension (&le
, dim
);
679 ppl_set_coef (le
, 2 * level
+ 1, 1);
682 ppl_max_for_le_pointset (ps
, le
, up
);
683 ppl_min_for_le_pointset (ps
, le
, low
);
686 /* Compute the type for the induction variable at LEVEL for the
687 statement PBB, based on the transformed schedule of PBB. */
690 compute_type_for_level (poly_bb_p pbb
, int level
)
698 compute_bounds_for_level (pbb
, level
, low
, up
);
699 type
= gcc_type_for_interval (low
, up
);
706 /* Walks a CLAST and returns the first statement in the body of a
709 static struct clast_user_stmt
*
710 clast_get_body_of_loop (struct clast_stmt
*stmt
)
713 || CLAST_STMT_IS_A (stmt
, stmt_user
))
714 return (struct clast_user_stmt
*) stmt
;
716 if (CLAST_STMT_IS_A (stmt
, stmt_for
))
717 return clast_get_body_of_loop (((struct clast_for
*) stmt
)->body
);
719 if (CLAST_STMT_IS_A (stmt
, stmt_guard
))
720 return clast_get_body_of_loop (((struct clast_guard
*) stmt
)->then
);
722 if (CLAST_STMT_IS_A (stmt
, stmt_block
))
723 return clast_get_body_of_loop (((struct clast_block
*) stmt
)->body
);
728 /* Returns the type for the induction variable for the loop translated
732 gcc_type_for_iv_of_clast_loop (struct clast_for
*stmt_for
, int level
,
733 tree lb_type
, tree ub_type
)
735 struct clast_stmt
*stmt
= (struct clast_stmt
*) stmt_for
;
736 struct clast_user_stmt
*body
= clast_get_body_of_loop (stmt
);
737 CloogStatement
*cs
= body
->statement
;
738 poly_bb_p pbb
= (poly_bb_p
) cloog_statement_usr (cs
);
740 return max_signed_precision_type (lb_type
, max_precision_type
741 (ub_type
, compute_type_for_level
745 /* Creates a new LOOP corresponding to Cloog's STMT. Inserts an
746 induction variable for the new LOOP. New LOOP is attached to CFG
747 starting at ENTRY_EDGE. LOOP is inserted into the loop tree and
748 becomes the child loop of the OUTER_LOOP. NEWIVS_INDEX binds
749 CLooG's scattering name to the induction variable created for the
750 loop of STMT. The new induction variable is inserted in the NEWIVS
754 graphite_create_new_loop (sese region
, edge entry_edge
,
755 struct clast_for
*stmt
,
756 loop_p outer
, VEC (tree
, heap
) **newivs
,
757 htab_t newivs_index
, htab_t params_index
, int level
)
759 tree lb_type
= gcc_type_for_clast_expr (stmt
->LB
, region
, *newivs
,
760 newivs_index
, params_index
);
761 tree ub_type
= gcc_type_for_clast_expr (stmt
->UB
, region
, *newivs
,
762 newivs_index
, params_index
);
763 tree type
= gcc_type_for_iv_of_clast_loop (stmt
, level
, lb_type
, ub_type
);
764 tree lb
= clast_to_gcc_expression (type
, stmt
->LB
, region
, *newivs
,
765 newivs_index
, params_index
);
766 tree ub
= clast_to_gcc_expression (type
, stmt
->UB
, region
, *newivs
,
767 newivs_index
, params_index
);
768 tree stride
= gmp_cst_to_tree (type
, stmt
->stride
);
769 tree ivvar
= create_tmp_var (type
, "graphite_IV");
770 tree iv
, iv_after_increment
;
771 loop_p loop
= create_empty_loop_on_edge
772 (entry_edge
, lb
, stride
, ub
, ivvar
, &iv
, &iv_after_increment
,
773 outer
? outer
: entry_edge
->src
->loop_father
);
775 add_referenced_var (ivvar
);
777 save_clast_name_index (newivs_index
, stmt
->iterator
,
778 VEC_length (tree
, *newivs
));
779 VEC_safe_push (tree
, heap
, *newivs
, iv
);
783 /* Inserts in iv_map a tuple (OLD_LOOP->num, NEW_NAME) for the
784 induction variables of the loops around GBB in SESE. */
787 build_iv_mapping (VEC (tree
, heap
) *iv_map
, sese region
,
788 VEC (tree
, heap
) *newivs
, htab_t newivs_index
,
789 struct clast_user_stmt
*user_stmt
,
792 struct clast_stmt
*t
;
794 CloogStatement
*cs
= user_stmt
->statement
;
795 poly_bb_p pbb
= (poly_bb_p
) cloog_statement_usr (cs
);
796 gimple_bb_p gbb
= PBB_BLACK_BOX (pbb
);
798 for (t
= user_stmt
->substitutions
; t
; t
= t
->next
, depth
++)
800 struct clast_expr
*expr
= (struct clast_expr
*)
801 ((struct clast_assignment
*)t
)->RHS
;
802 tree type
= gcc_type_for_clast_expr (expr
, region
, newivs
,
803 newivs_index
, params_index
);
804 tree new_name
= clast_to_gcc_expression (type
, expr
, region
, newivs
,
805 newivs_index
, params_index
);
806 loop_p old_loop
= gbb_loop_at_index (gbb
, region
, depth
);
808 VEC_replace (tree
, iv_map
, old_loop
->num
, new_name
);
812 /* Construct bb_pbb_def with BB and PBB. */
815 new_bb_pbb_def (basic_block bb
, poly_bb_p pbb
)
817 bb_pbb_def
*bb_pbb_p
;
819 bb_pbb_p
= XNEW (bb_pbb_def
);
826 /* Mark BB with it's relevant PBB via hashing table BB_PBB_MAPPING. */
829 mark_bb_with_pbb (poly_bb_p pbb
, basic_block bb
, htab_t bb_pbb_mapping
)
835 x
= htab_find_slot (bb_pbb_mapping
, &tmp
, INSERT
);
838 *x
= new_bb_pbb_def (bb
, pbb
);
841 /* Find BB's related poly_bb_p in hash table BB_PBB_MAPPING. */
844 find_pbb_via_hash (htab_t bb_pbb_mapping
, basic_block bb
)
850 slot
= htab_find_slot (bb_pbb_mapping
, &tmp
, NO_INSERT
);
853 return ((bb_pbb_def
*) *slot
)->pbb
;
858 /* Check data dependency in LOOP at scattering level LEVEL.
859 BB_PBB_MAPPING is a basic_block and it's related poly_bb_p
863 dependency_in_loop_p (loop_p loop
, htab_t bb_pbb_mapping
, int level
)
866 basic_block
*bbs
= get_loop_body_in_dom_order (loop
);
868 for (i
= 0; i
< loop
->num_nodes
; i
++)
870 poly_bb_p pbb1
= find_pbb_via_hash (bb_pbb_mapping
, bbs
[i
]);
875 for (j
= 0; j
< loop
->num_nodes
; j
++)
877 poly_bb_p pbb2
= find_pbb_via_hash (bb_pbb_mapping
, bbs
[j
]);
882 if (dependency_between_pbbs_p (pbb1
, pbb2
, level
))
895 /* Translates a clast user statement STMT to gimple.
897 - REGION is the sese region we used to generate the scop.
898 - NEXT_E is the edge where new generated code should be attached.
899 - CONTEXT_LOOP is the loop in which the generated code will be placed
900 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
901 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
904 translate_clast_user (sese region
, struct clast_user_stmt
*stmt
, edge next_e
,
905 VEC (tree
, heap
) **newivs
,
906 htab_t newivs_index
, htab_t bb_pbb_mapping
,
911 poly_bb_p pbb
= (poly_bb_p
) cloog_statement_usr (stmt
->statement
);
912 gimple_bb_p gbb
= PBB_BLACK_BOX (pbb
);
913 VEC (tree
, heap
) *iv_map
;
915 if (GBB_BB (gbb
) == ENTRY_BLOCK_PTR
)
918 nb_loops
= number_of_loops ();
919 iv_map
= VEC_alloc (tree
, heap
, nb_loops
);
920 for (i
= 0; i
< nb_loops
; i
++)
921 VEC_quick_push (tree
, iv_map
, NULL_TREE
);
923 build_iv_mapping (iv_map
, region
, *newivs
, newivs_index
, stmt
, params_index
);
924 next_e
= copy_bb_and_scalar_dependences (GBB_BB (gbb
), region
,
926 VEC_free (tree
, heap
, iv_map
);
928 new_bb
= next_e
->src
;
929 mark_bb_with_pbb (pbb
, new_bb
, bb_pbb_mapping
);
930 update_ssa (TODO_update_ssa
);
935 /* Creates a new if region protecting the loop to be executed, if the execution
936 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
);
955 tree one
= POINTER_TYPE_P (type
) ? size_one_node
956 : fold_convert (type
, integer_one_node
);
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 tree ub_one
= fold_build2 (POINTER_TYPE_P (type
) ? POINTER_PLUS_EXPR
962 : PLUS_EXPR
, type
, ub
, one
);
964 /* When ub + 1 wraps around, use lb <= ub. */
965 if (integer_zerop (ub_one
))
966 cond_expr
= fold_build2 (LE_EXPR
, boolean_type_node
, lb
, ub
);
968 cond_expr
= fold_build2 (LT_EXPR
, boolean_type_node
, lb
, ub_one
);
970 exit_edge
= create_empty_if_region_on_edge (entry_edge
, cond_expr
);
976 translate_clast (sese
, loop_p
, struct clast_stmt
*, edge
,
977 VEC (tree
, heap
) **, htab_t
, htab_t
, int, htab_t
);
979 /* Create the loop for a clast for statement.
981 - REGION is the sese region we used to generate the scop.
982 - NEXT_E is the edge where new generated code should be attached.
983 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
984 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
987 translate_clast_for_loop (sese region
, loop_p context_loop
,
988 struct clast_for
*stmt
, edge next_e
,
989 VEC (tree
, heap
) **newivs
,
990 htab_t newivs_index
, htab_t bb_pbb_mapping
,
991 int level
, htab_t params_index
)
993 struct loop
*loop
= graphite_create_new_loop (region
, next_e
, stmt
,
994 context_loop
, newivs
,
995 newivs_index
, params_index
,
997 edge last_e
= single_exit (loop
);
998 edge to_body
= single_succ_edge (loop
->header
);
999 basic_block after
= to_body
->dest
;
1001 /* Create a basic block for loop close phi nodes. */
1002 last_e
= single_succ_edge (split_edge (last_e
));
1004 /* Translate the body of the loop. */
1005 next_e
= translate_clast (region
, loop
, stmt
->body
, to_body
,
1006 newivs
, newivs_index
, bb_pbb_mapping
, level
+ 1,
1008 redirect_edge_succ_nodup (next_e
, after
);
1009 set_immediate_dominator (CDI_DOMINATORS
, next_e
->dest
, next_e
->src
);
1011 if (flag_loop_parallelize_all
1012 && !dependency_in_loop_p (loop
, bb_pbb_mapping
,
1013 get_scattering_level (level
)))
1014 loop
->can_be_parallel
= true;
1019 /* Translates a clast for statement STMT to gimple. First a guard is created
1020 protecting the loop, if it is executed zero times. In this guard we create
1021 the real loop structure.
1023 - REGION is the sese region we used to generate the scop.
1024 - NEXT_E is the edge where new generated code should be attached.
1025 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
1026 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
1029 translate_clast_for (sese region
, loop_p context_loop
, struct clast_for
*stmt
,
1030 edge next_e
, VEC (tree
, heap
) **newivs
,
1031 htab_t newivs_index
, htab_t bb_pbb_mapping
, int level
,
1032 htab_t params_index
)
1034 edge last_e
= graphite_create_new_loop_guard (region
, next_e
, stmt
, *newivs
,
1035 newivs_index
, params_index
);
1036 edge true_e
= get_true_edge_from_guard_bb (next_e
->dest
);
1038 translate_clast_for_loop (region
, context_loop
, stmt
, true_e
, newivs
,
1039 newivs_index
, bb_pbb_mapping
, level
,
1044 /* Translates a clast guard statement STMT to gimple.
1046 - REGION is the sese region we used to generate the scop.
1047 - NEXT_E is the edge where new generated code should be attached.
1048 - CONTEXT_LOOP is the loop in which the generated code will be placed
1049 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
1050 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
1053 translate_clast_guard (sese region
, loop_p context_loop
,
1054 struct clast_guard
*stmt
, edge next_e
,
1055 VEC (tree
, heap
) **newivs
,
1056 htab_t newivs_index
, htab_t bb_pbb_mapping
, int level
,
1057 htab_t params_index
)
1059 edge last_e
= graphite_create_new_guard (region
, next_e
, stmt
, *newivs
,
1060 newivs_index
, params_index
);
1061 edge true_e
= get_true_edge_from_guard_bb (next_e
->dest
);
1063 translate_clast (region
, context_loop
, stmt
->then
, true_e
,
1064 newivs
, newivs_index
, bb_pbb_mapping
,
1065 level
, params_index
);
1069 /* Translates a CLAST statement STMT to GCC representation in the
1072 - NEXT_E is the edge where new generated code should be attached.
1073 - CONTEXT_LOOP is the loop in which the generated code will be placed
1074 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping. */
1076 translate_clast (sese region
, loop_p context_loop
, struct clast_stmt
*stmt
,
1077 edge next_e
, VEC (tree
, heap
) **newivs
,
1078 htab_t newivs_index
, htab_t bb_pbb_mapping
, int level
,
1079 htab_t params_index
)
1084 if (CLAST_STMT_IS_A (stmt
, stmt_root
))
1087 else if (CLAST_STMT_IS_A (stmt
, stmt_user
))
1088 next_e
= translate_clast_user (region
, (struct clast_user_stmt
*) stmt
,
1089 next_e
, newivs
, newivs_index
,
1090 bb_pbb_mapping
, params_index
);
1092 else if (CLAST_STMT_IS_A (stmt
, stmt_for
))
1093 next_e
= translate_clast_for (region
, context_loop
,
1094 (struct clast_for
*) stmt
, next_e
,
1095 newivs
, newivs_index
,
1096 bb_pbb_mapping
, level
, params_index
);
1098 else if (CLAST_STMT_IS_A (stmt
, stmt_guard
))
1099 next_e
= translate_clast_guard (region
, context_loop
,
1100 (struct clast_guard
*) stmt
, next_e
,
1101 newivs
, newivs_index
,
1102 bb_pbb_mapping
, level
, params_index
);
1104 else if (CLAST_STMT_IS_A (stmt
, stmt_block
))
1105 next_e
= translate_clast (region
, context_loop
,
1106 ((struct clast_block
*) stmt
)->body
,
1107 next_e
, newivs
, newivs_index
,
1108 bb_pbb_mapping
, level
, params_index
);
1112 recompute_all_dominators ();
1115 return translate_clast (region
, context_loop
, stmt
->next
, next_e
,
1116 newivs
, newivs_index
,
1117 bb_pbb_mapping
, level
, params_index
);
1120 /* Free the SCATTERING domain list. */
1123 free_scattering (CloogDomainList
*scattering
)
1127 CloogDomain
*dom
= cloog_domain (scattering
);
1128 CloogDomainList
*next
= cloog_next_domain (scattering
);
1130 cloog_domain_free (dom
);
1136 /* Initialize Cloog's parameter names from the names used in GIMPLE.
1137 Initialize Cloog's iterator names, using 'graphite_iterator_%d'
1138 from 0 to scop_nb_loops (scop). */
1141 initialize_cloog_names (scop_p scop
, CloogProgram
*prog
)
1143 sese region
= SCOP_REGION (scop
);
1145 int nb_iterators
= scop_max_loop_depth (scop
);
1146 int nb_scattering
= cloog_program_nb_scattdims (prog
);
1147 int nb_parameters
= VEC_length (tree
, SESE_PARAMS (region
));
1148 char **iterators
= XNEWVEC (char *, nb_iterators
* 2);
1149 char **scattering
= XNEWVEC (char *, nb_scattering
);
1150 char **parameters
= XNEWVEC (char *, nb_parameters
);
1152 cloog_program_set_names (prog
, cloog_names_malloc ());
1154 for (i
= 0; i
< nb_parameters
; i
++)
1156 tree param
= VEC_index (tree
, SESE_PARAMS(region
), i
);
1157 const char *name
= get_name (param
);
1163 len
= strlen (name
);
1165 parameters
[i
] = XNEWVEC (char, len
+ 1);
1166 snprintf (parameters
[i
], len
, "%s_%d", name
, SSA_NAME_VERSION (param
));
1169 cloog_names_set_nb_parameters (cloog_program_names (prog
), nb_parameters
);
1170 cloog_names_set_parameters (cloog_program_names (prog
), parameters
);
1172 for (i
= 0; i
< nb_iterators
; i
++)
1175 iterators
[i
] = XNEWVEC (char, len
);
1176 snprintf (iterators
[i
], len
, "git_%d", i
);
1179 cloog_names_set_nb_iterators (cloog_program_names (prog
),
1181 cloog_names_set_iterators (cloog_program_names (prog
),
1184 for (i
= 0; i
< nb_scattering
; i
++)
1187 scattering
[i
] = XNEWVEC (char, len
);
1188 snprintf (scattering
[i
], len
, "scat_%d", i
);
1191 cloog_names_set_nb_scattering (cloog_program_names (prog
),
1193 cloog_names_set_scattering (cloog_program_names (prog
),
1197 /* Build cloog program for SCoP. */
1200 build_cloog_prog (scop_p scop
, CloogProgram
*prog
)
1203 int max_nb_loops
= scop_max_loop_depth (scop
);
1205 CloogLoop
*loop_list
= NULL
;
1206 CloogBlockList
*block_list
= NULL
;
1207 CloogDomainList
*scattering
= NULL
;
1208 int nbs
= 2 * max_nb_loops
+ 1;
1211 cloog_program_set_context
1212 (prog
, new_Cloog_Domain_from_ppl_Pointset_Powerset (SCOP_CONTEXT (scop
)));
1213 nbs
= unify_scattering_dimensions (scop
);
1214 scaldims
= (int *) xmalloc (nbs
* (sizeof (int)));
1215 cloog_program_set_nb_scattdims (prog
, nbs
);
1216 initialize_cloog_names (scop
, prog
);
1218 for (i
= 0; VEC_iterate (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
); i
++)
1220 CloogStatement
*stmt
;
1223 /* Dead code elimination: when the domain of a PBB is empty,
1224 don't generate code for the PBB. */
1225 if (ppl_Pointset_Powerset_C_Polyhedron_is_empty (PBB_DOMAIN (pbb
)))
1228 /* Build the new statement and its block. */
1229 stmt
= cloog_statement_alloc (pbb_index (pbb
));
1230 block
= cloog_block_alloc (stmt
, 0, NULL
, pbb_dim_iter_domain (pbb
));
1231 cloog_statement_set_usr (stmt
, pbb
);
1233 /* Build loop list. */
1235 CloogLoop
*new_loop_list
= cloog_loop_malloc ();
1236 cloog_loop_set_next (new_loop_list
, loop_list
);
1237 cloog_loop_set_domain
1239 new_Cloog_Domain_from_ppl_Pointset_Powerset (PBB_DOMAIN (pbb
)));
1240 cloog_loop_set_block (new_loop_list
, block
);
1241 loop_list
= new_loop_list
;
1244 /* Build block list. */
1246 CloogBlockList
*new_block_list
= cloog_block_list_malloc ();
1248 cloog_block_list_set_next (new_block_list
, block_list
);
1249 cloog_block_list_set_block (new_block_list
, block
);
1250 block_list
= new_block_list
;
1253 /* Build scattering list. */
1255 /* XXX: Replace with cloog_domain_list_alloc(), when available. */
1256 CloogDomainList
*new_scattering
1257 = (CloogDomainList
*) xmalloc (sizeof (CloogDomainList
));
1258 ppl_Polyhedron_t scat
;
1261 scat
= PBB_TRANSFORMED_SCATTERING (pbb
);
1262 dom
= new_Cloog_Domain_from_ppl_Polyhedron (scat
);
1264 cloog_set_next_domain (new_scattering
, scattering
);
1265 cloog_set_domain (new_scattering
, dom
);
1266 scattering
= new_scattering
;
1270 cloog_program_set_loop (prog
, loop_list
);
1271 cloog_program_set_blocklist (prog
, block_list
);
1273 for (i
= 0; i
< nbs
; i
++)
1276 cloog_program_set_scaldims (prog
, scaldims
);
1278 /* Extract scalar dimensions to simplify the code generation problem. */
1279 cloog_program_extract_scalars (prog
, scattering
);
1281 /* Apply scattering. */
1282 cloog_program_scatter (prog
, scattering
);
1283 free_scattering (scattering
);
1285 /* Iterators corresponding to scalar dimensions have to be extracted. */
1286 cloog_names_scalarize (cloog_program_names (prog
), nbs
,
1287 cloog_program_scaldims (prog
));
1289 /* Free blocklist. */
1291 CloogBlockList
*next
= cloog_program_blocklist (prog
);
1295 CloogBlockList
*toDelete
= next
;
1296 next
= cloog_block_list_next (next
);
1297 cloog_block_list_set_next (toDelete
, NULL
);
1298 cloog_block_list_set_block (toDelete
, NULL
);
1299 cloog_block_list_free (toDelete
);
1301 cloog_program_set_blocklist (prog
, NULL
);
1305 /* Return the options that will be used in GLOOG. */
1307 static CloogOptions
*
1308 set_cloog_options (void)
1310 CloogOptions
*options
= cloog_options_malloc ();
1312 /* Change cloog output language to C. If we do use FORTRAN instead, cloog
1313 will stop e.g. with "ERROR: unbounded loops not allowed in FORTRAN.", if
1314 we pass an incomplete program to cloog. */
1315 options
->language
= LANGUAGE_C
;
1317 /* Enable complex equality spreading: removes dummy statements
1318 (assignments) in the generated code which repeats the
1319 substitution equations for statements. This is useless for
1323 /* Enable C pretty-printing mode: normalizes the substitution
1324 equations for statements. */
1327 /* Allow cloog to build strides with a stride width different to one.
1328 This example has stride = 4:
1330 for (i = 0; i < 20; i += 4)
1332 options
->strides
= 1;
1334 /* Disable optimizations and make cloog generate source code closer to the
1335 input. This is useful for debugging, but later we want the optimized
1338 XXX: We can not disable optimizations, as loop blocking is not working
1343 options
->l
= INT_MAX
;
1349 /* Prints STMT to STDERR. */
1352 print_clast_stmt (FILE *file
, struct clast_stmt
*stmt
)
1354 CloogOptions
*options
= set_cloog_options ();
1356 pprint (file
, stmt
, 0, options
);
1357 cloog_options_free (options
);
1360 /* Prints STMT to STDERR. */
1363 debug_clast_stmt (struct clast_stmt
*stmt
)
1365 print_clast_stmt (stderr
, stmt
);
1368 /* Translate SCOP to a CLooG program and clast. These two
1369 representations should be freed together: a clast cannot be used
1370 without a program. */
1373 scop_to_clast (scop_p scop
)
1375 CloogOptions
*options
= set_cloog_options ();
1376 cloog_prog_clast pc
;
1378 /* Connect new cloog prog generation to graphite. */
1379 pc
.prog
= cloog_program_malloc ();
1380 build_cloog_prog (scop
, pc
.prog
);
1381 pc
.prog
= cloog_program_generate (pc
.prog
, options
);
1382 pc
.stmt
= cloog_clast_create (pc
.prog
, options
);
1384 cloog_options_free (options
);
1388 /* Prints to FILE the code generated by CLooG for SCOP. */
1391 print_generated_program (FILE *file
, scop_p scop
)
1393 CloogOptions
*options
= set_cloog_options ();
1394 cloog_prog_clast pc
= scop_to_clast (scop
);
1396 fprintf (file
, " (prog: \n");
1397 cloog_program_print (file
, pc
.prog
);
1398 fprintf (file
, " )\n");
1400 fprintf (file
, " (clast: \n");
1401 pprint (file
, pc
.stmt
, 0, options
);
1402 fprintf (file
, " )\n");
1404 cloog_options_free (options
);
1405 cloog_clast_free (pc
.stmt
);
1406 cloog_program_free (pc
.prog
);
1409 /* Prints to STDERR the code generated by CLooG for SCOP. */
1412 debug_generated_program (scop_p scop
)
1414 print_generated_program (stderr
, scop
);
1417 /* Add CLooG names to parameter index. The index is used to translate
1418 back from CLooG names to GCC trees. */
1421 create_params_index (htab_t index_table
, CloogProgram
*prog
) {
1422 CloogNames
* names
= cloog_program_names (prog
);
1423 int nb_parameters
= cloog_names_nb_parameters (names
);
1424 char **parameters
= cloog_names_parameters (names
);
1427 for (i
= 0; i
< nb_parameters
; i
++)
1428 save_clast_name_index (index_table
, parameters
[i
], i
);
1431 /* GIMPLE Loop Generator: generates loops from STMT in GIMPLE form for
1432 the given SCOP. Return true if code generation succeeded.
1433 BB_PBB_MAPPING is a basic_block and it's related poly_bb_p mapping.
1437 gloog (scop_p scop
, htab_t bb_pbb_mapping
)
1439 VEC (tree
, heap
) *newivs
= VEC_alloc (tree
, heap
, 10);
1440 loop_p context_loop
;
1441 sese region
= SCOP_REGION (scop
);
1442 ifsese if_region
= NULL
;
1443 htab_t newivs_index
, params_index
;
1444 cloog_prog_clast pc
;
1446 timevar_push (TV_GRAPHITE_CODE_GEN
);
1447 gloog_error
= false;
1449 pc
= scop_to_clast (scop
);
1451 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1453 fprintf (dump_file
, "\nCLAST generated by CLooG: \n");
1454 print_clast_stmt (dump_file
, pc
.stmt
);
1455 fprintf (dump_file
, "\n");
1458 recompute_all_dominators ();
1461 if_region
= move_sese_in_condition (region
);
1462 sese_insert_phis_for_liveouts (region
,
1463 if_region
->region
->exit
->src
,
1464 if_region
->false_region
->exit
,
1465 if_region
->true_region
->exit
);
1466 recompute_all_dominators ();
1469 context_loop
= SESE_ENTRY (region
)->src
->loop_father
;
1470 newivs_index
= htab_create (10, clast_name_index_elt_info
,
1471 eq_clast_name_indexes
, free
);
1472 params_index
= htab_create (10, clast_name_index_elt_info
,
1473 eq_clast_name_indexes
, free
);
1475 create_params_index (params_index
, pc
.prog
);
1477 translate_clast (region
, context_loop
, pc
.stmt
,
1478 if_region
->true_region
->entry
,
1479 &newivs
, newivs_index
,
1480 bb_pbb_mapping
, 1, params_index
);
1483 recompute_all_dominators ();
1487 set_ifsese_condition (if_region
, integer_zero_node
);
1489 free (if_region
->true_region
);
1490 free (if_region
->region
);
1493 htab_delete (newivs_index
);
1494 htab_delete (params_index
);
1495 VEC_free (tree
, heap
, newivs
);
1496 cloog_clast_free (pc
.stmt
);
1497 cloog_program_free (pc
.prog
);
1498 timevar_pop (TV_GRAPHITE_CODE_GEN
);
1500 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1504 int num_no_dependency
= 0;
1506 FOR_EACH_LOOP (li
, loop
, 0)
1507 if (loop
->can_be_parallel
)
1508 num_no_dependency
++;
1510 fprintf (dump_file
, "\n%d loops carried no dependency.\n",
1514 return !gloog_error
;