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"
24 #include "diagnostic-core.h"
25 #include "tree-flow.h"
26 #include "tree-dump.h"
28 #include "tree-chrec.h"
29 #include "tree-data-ref.h"
30 #include "tree-scalar-evolution.h"
34 #include "cloog/cloog.h"
36 #include "graphite-cloog-util.h"
37 #include "graphite-ppl.h"
38 #include "graphite-poly.h"
39 #include "graphite-clast-to-gimple.h"
40 #include "graphite-dependences.h"
41 #include "graphite-cloog-compat.h"
43 /* This flag is set when an error occurred during the translation of
45 static bool gloog_error
;
47 /* Verifies properties that GRAPHITE should maintain during translation. */
50 graphite_verify (void)
52 #ifdef ENABLE_CHECKING
53 verify_loop_structure ();
54 verify_dominators (CDI_DOMINATORS
);
55 verify_loop_closed_ssa (true);
59 /* Stores the INDEX in a vector for a given clast NAME. */
61 typedef struct clast_name_index
{
64 } *clast_name_index_p
;
66 /* Returns a pointer to a new element of type clast_name_index_p built
67 from NAME and INDEX. */
69 static inline clast_name_index_p
70 new_clast_name_index (const char *name
, int index
)
72 clast_name_index_p res
= XNEW (struct clast_name_index
);
79 /* For a given clast NAME, returns -1 if it does not correspond to any
80 parameter, or otherwise, returns the index in the PARAMS or
81 SCATTERING_DIMENSIONS vector. */
84 clast_name_to_index (clast_name_p name
, htab_t index_table
)
86 struct clast_name_index tmp
;
90 gcc_assert (name
->type
== clast_expr_name
);
91 tmp
.name
= ((const struct clast_name
*) name
)->name
;
96 slot
= htab_find_slot (index_table
, &tmp
, NO_INSERT
);
99 return ((struct clast_name_index
*) *slot
)->index
;
104 /* Records in INDEX_TABLE the INDEX for NAME. */
107 save_clast_name_index (htab_t index_table
, const char *name
, int index
)
109 struct clast_name_index tmp
;
113 slot
= htab_find_slot (index_table
, &tmp
, INSERT
);
120 *slot
= new_clast_name_index (name
, index
);
124 /* Computes a hash function for database element ELT. */
126 static inline hashval_t
127 clast_name_index_elt_info (const void *elt
)
129 return htab_hash_pointer (((const struct clast_name_index
*) elt
)->name
);
132 /* Compares database elements E1 and E2. */
135 eq_clast_name_indexes (const void *e1
, const void *e2
)
137 const struct clast_name_index
*elt1
= (const struct clast_name_index
*) e1
;
138 const struct clast_name_index
*elt2
= (const struct clast_name_index
*) e2
;
140 return (elt1
->name
== elt2
->name
);
143 /* For a given scattering dimension, return the new induction variable
147 newivs_to_depth_to_newiv (VEC (tree
, heap
) *newivs
, int depth
)
149 return VEC_index (tree
, newivs
, depth
);
154 /* Returns the tree variable from the name NAME that was given in
155 Cloog representation. */
158 clast_name_to_gcc (clast_name_p name
, sese region
, VEC (tree
, heap
) *newivs
,
159 htab_t newivs_index
, htab_t params_index
)
162 VEC (tree
, heap
) *params
= SESE_PARAMS (region
);
164 if (params
&& params_index
)
166 index
= clast_name_to_index (name
, params_index
);
169 return VEC_index (tree
, params
, index
);
172 gcc_assert (newivs
&& newivs_index
);
173 index
= clast_name_to_index (name
, newivs_index
);
174 gcc_assert (index
>= 0);
176 return newivs_to_depth_to_newiv (newivs
, index
);
179 /* Returns the signed maximal precision type for expressions TYPE1 and TYPE2. */
182 max_signed_precision_type (tree type1
, tree type2
)
184 int p1
= TYPE_PRECISION (type1
);
185 int p2
= TYPE_PRECISION (type2
);
188 enum machine_mode mode
;
191 precision
= TYPE_UNSIGNED (type1
) ? p1
* 2 : p1
;
193 precision
= TYPE_UNSIGNED (type2
) ? p2
* 2 : p2
;
195 if (precision
> BITS_PER_WORD
)
198 return integer_type_node
;
201 mode
= smallest_mode_for_size (precision
, MODE_INT
);
202 precision
= GET_MODE_PRECISION (mode
);
203 type
= build_nonstandard_integer_type (precision
, false);
208 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
)
270 case clast_expr_term
:
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 prec_up
= precision_for_value (up
);
447 prec_int
= precision_for_interval (low
, up
);
448 precision
= MAX (prec_up
, prec_int
);
450 if (precision
> BITS_PER_WORD
)
453 return integer_type_node
;
456 if (mpz_sgn (low
) <= 0)
459 else if (precision
< BITS_PER_WORD
)
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
);
691 ppl_delete_Linear_Expression (le
);
692 ppl_delete_Pointset_Powerset_C_Polyhedron (ps
);
695 /* Compute the type for the induction variable at LEVEL for the
696 statement PBB, based on the transformed schedule of PBB. */
699 compute_type_for_level (poly_bb_p pbb
, int level
)
707 compute_bounds_for_level (pbb
, level
, low
, up
);
708 type
= gcc_type_for_interval (low
, up
);
715 /* Walks a CLAST and returns the first statement in the body of a
718 static struct clast_user_stmt
*
719 clast_get_body_of_loop (struct clast_stmt
*stmt
)
722 || CLAST_STMT_IS_A (stmt
, stmt_user
))
723 return (struct clast_user_stmt
*) stmt
;
725 if (CLAST_STMT_IS_A (stmt
, stmt_for
))
726 return clast_get_body_of_loop (((struct clast_for
*) stmt
)->body
);
728 if (CLAST_STMT_IS_A (stmt
, stmt_guard
))
729 return clast_get_body_of_loop (((struct clast_guard
*) stmt
)->then
);
731 if (CLAST_STMT_IS_A (stmt
, stmt_block
))
732 return clast_get_body_of_loop (((struct clast_block
*) stmt
)->body
);
737 /* Returns the type for the induction variable for the loop translated
741 gcc_type_for_iv_of_clast_loop (struct clast_for
*stmt_for
, int level
,
742 tree lb_type
, tree ub_type
)
744 struct clast_stmt
*stmt
= (struct clast_stmt
*) stmt_for
;
745 struct clast_user_stmt
*body
= clast_get_body_of_loop (stmt
);
746 CloogStatement
*cs
= body
->statement
;
747 poly_bb_p pbb
= (poly_bb_p
) cloog_statement_usr (cs
);
749 return max_signed_precision_type (lb_type
, max_precision_type
750 (ub_type
, compute_type_for_level
754 /* Creates a new LOOP corresponding to Cloog's STMT. Inserts an
755 induction variable for the new LOOP. New LOOP is attached to CFG
756 starting at ENTRY_EDGE. LOOP is inserted into the loop tree and
757 becomes the child loop of the OUTER_LOOP. NEWIVS_INDEX binds
758 CLooG's scattering name to the induction variable created for the
759 loop of STMT. The new induction variable is inserted in the NEWIVS
763 graphite_create_new_loop (sese region
, edge entry_edge
,
764 struct clast_for
*stmt
,
765 loop_p outer
, VEC (tree
, heap
) **newivs
,
766 htab_t newivs_index
, htab_t params_index
, int level
)
768 tree lb_type
= gcc_type_for_clast_expr (stmt
->LB
, region
, *newivs
,
769 newivs_index
, params_index
);
770 tree ub_type
= gcc_type_for_clast_expr (stmt
->UB
, region
, *newivs
,
771 newivs_index
, params_index
);
772 tree type
= gcc_type_for_iv_of_clast_loop (stmt
, level
, lb_type
, ub_type
);
773 tree lb
= clast_to_gcc_expression (type
, stmt
->LB
, region
, *newivs
,
774 newivs_index
, params_index
);
775 tree ub
= clast_to_gcc_expression (type
, stmt
->UB
, region
, *newivs
,
776 newivs_index
, params_index
);
777 tree stride
= gmp_cst_to_tree (type
, stmt
->stride
);
778 tree ivvar
= create_tmp_var (type
, "graphite_IV");
779 tree iv
, iv_after_increment
;
780 loop_p loop
= create_empty_loop_on_edge
781 (entry_edge
, lb
, stride
, ub
, ivvar
, &iv
, &iv_after_increment
,
782 outer
? outer
: entry_edge
->src
->loop_father
);
784 add_referenced_var (ivvar
);
786 save_clast_name_index (newivs_index
, stmt
->iterator
,
787 VEC_length (tree
, *newivs
));
788 VEC_safe_push (tree
, heap
, *newivs
, iv
);
792 /* Inserts in iv_map a tuple (OLD_LOOP->num, NEW_NAME) for the
793 induction variables of the loops around GBB in SESE. */
796 build_iv_mapping (VEC (tree
, heap
) *iv_map
, sese region
,
797 VEC (tree
, heap
) *newivs
, htab_t newivs_index
,
798 struct clast_user_stmt
*user_stmt
,
801 struct clast_stmt
*t
;
803 CloogStatement
*cs
= user_stmt
->statement
;
804 poly_bb_p pbb
= (poly_bb_p
) cloog_statement_usr (cs
);
805 gimple_bb_p gbb
= PBB_BLACK_BOX (pbb
);
807 for (t
= user_stmt
->substitutions
; t
; t
= t
->next
, depth
++)
809 struct clast_expr
*expr
= (struct clast_expr
*)
810 ((struct clast_assignment
*)t
)->RHS
;
811 tree type
= gcc_type_for_clast_expr (expr
, region
, newivs
,
812 newivs_index
, params_index
);
813 tree new_name
= clast_to_gcc_expression (type
, expr
, region
, newivs
,
814 newivs_index
, params_index
);
815 loop_p old_loop
= gbb_loop_at_index (gbb
, region
, depth
);
817 VEC_replace (tree
, iv_map
, old_loop
->num
, new_name
);
821 /* Construct bb_pbb_def with BB and PBB. */
824 new_bb_pbb_def (basic_block bb
, poly_bb_p pbb
)
826 bb_pbb_def
*bb_pbb_p
;
828 bb_pbb_p
= XNEW (bb_pbb_def
);
835 /* Mark BB with it's relevant PBB via hashing table BB_PBB_MAPPING. */
838 mark_bb_with_pbb (poly_bb_p pbb
, basic_block bb
, htab_t bb_pbb_mapping
)
844 x
= htab_find_slot (bb_pbb_mapping
, &tmp
, INSERT
);
847 *x
= new_bb_pbb_def (bb
, pbb
);
850 /* Find BB's related poly_bb_p in hash table BB_PBB_MAPPING. */
853 find_pbb_via_hash (htab_t bb_pbb_mapping
, basic_block bb
)
859 slot
= htab_find_slot (bb_pbb_mapping
, &tmp
, NO_INSERT
);
862 return ((bb_pbb_def
*) *slot
)->pbb
;
867 /* Check data dependency in LOOP at scattering level LEVEL.
868 BB_PBB_MAPPING is a basic_block and it's related poly_bb_p
872 dependency_in_loop_p (loop_p loop
, htab_t bb_pbb_mapping
, int level
)
875 basic_block
*bbs
= get_loop_body_in_dom_order (loop
);
877 for (i
= 0; i
< loop
->num_nodes
; i
++)
879 poly_bb_p pbb1
= find_pbb_via_hash (bb_pbb_mapping
, bbs
[i
]);
884 for (j
= 0; j
< loop
->num_nodes
; j
++)
886 poly_bb_p pbb2
= find_pbb_via_hash (bb_pbb_mapping
, bbs
[j
]);
891 if (dependency_between_pbbs_p (pbb1
, pbb2
, level
))
904 /* Translates a clast user statement STMT to gimple.
906 - REGION is the sese region we used to generate the scop.
907 - NEXT_E is the edge where new generated code should be attached.
908 - CONTEXT_LOOP is the loop in which the generated code will be placed
909 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
910 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
913 translate_clast_user (sese region
, struct clast_user_stmt
*stmt
, edge next_e
,
914 VEC (tree
, heap
) **newivs
,
915 htab_t newivs_index
, htab_t bb_pbb_mapping
,
920 poly_bb_p pbb
= (poly_bb_p
) cloog_statement_usr (stmt
->statement
);
921 gimple_bb_p gbb
= PBB_BLACK_BOX (pbb
);
922 VEC (tree
, heap
) *iv_map
;
924 if (GBB_BB (gbb
) == ENTRY_BLOCK_PTR
)
927 nb_loops
= number_of_loops ();
928 iv_map
= VEC_alloc (tree
, heap
, nb_loops
);
929 for (i
= 0; i
< nb_loops
; i
++)
930 VEC_quick_push (tree
, iv_map
, NULL_TREE
);
932 build_iv_mapping (iv_map
, region
, *newivs
, newivs_index
, stmt
, params_index
);
933 next_e
= copy_bb_and_scalar_dependences (GBB_BB (gbb
), region
,
935 VEC_free (tree
, heap
, iv_map
);
937 new_bb
= next_e
->src
;
938 mark_bb_with_pbb (pbb
, new_bb
, bb_pbb_mapping
);
939 update_ssa (TODO_update_ssa
);
944 /* Creates a new if region protecting the loop to be executed, if the execution
945 count is zero (lb > ub). */
948 graphite_create_new_loop_guard (sese region
, edge entry_edge
,
949 struct clast_for
*stmt
,
950 VEC (tree
, heap
) *newivs
,
951 htab_t newivs_index
, htab_t params_index
)
955 tree lb_type
= gcc_type_for_clast_expr (stmt
->LB
, region
, newivs
,
956 newivs_index
, params_index
);
957 tree ub_type
= gcc_type_for_clast_expr (stmt
->UB
, region
, newivs
,
958 newivs_index
, params_index
);
959 tree type
= max_precision_type (lb_type
, ub_type
);
960 tree lb
= clast_to_gcc_expression (type
, stmt
->LB
, region
, newivs
,
961 newivs_index
, params_index
);
962 tree ub
= clast_to_gcc_expression (type
, stmt
->UB
, region
, newivs
,
963 newivs_index
, params_index
);
964 /* When ub is simply a constant or a parameter, use lb <= ub. */
965 if (TREE_CODE (ub
) == INTEGER_CST
|| TREE_CODE (ub
) == SSA_NAME
)
966 cond_expr
= fold_build2 (LE_EXPR
, boolean_type_node
, lb
, ub
);
969 tree one
= (POINTER_TYPE_P (type
)
971 : fold_convert (type
, integer_one_node
));
972 /* Adding +1 and using LT_EXPR helps with loop latches that have a
973 loop iteration count of "PARAMETER - 1". For PARAMETER == 0 this becomes
974 2^k-1 due to integer overflow, and the condition lb <= ub is true,
975 even if we do not want this. However lb < ub + 1 is false, as
977 tree ub_one
= fold_build2 (POINTER_TYPE_P (type
) ? POINTER_PLUS_EXPR
978 : PLUS_EXPR
, type
, ub
, one
);
980 cond_expr
= fold_build2 (LT_EXPR
, boolean_type_node
, lb
, ub_one
);
983 exit_edge
= create_empty_if_region_on_edge (entry_edge
, cond_expr
);
989 translate_clast (sese
, loop_p
, struct clast_stmt
*, edge
,
990 VEC (tree
, heap
) **, htab_t
, htab_t
, int, htab_t
);
992 /* Create the loop for a clast for statement.
994 - REGION is the sese region we used to generate the scop.
995 - NEXT_E is the edge where new generated code should be attached.
996 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
997 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
1000 translate_clast_for_loop (sese region
, loop_p context_loop
,
1001 struct clast_for
*stmt
, edge next_e
,
1002 VEC (tree
, heap
) **newivs
,
1003 htab_t newivs_index
, htab_t bb_pbb_mapping
,
1004 int level
, htab_t params_index
)
1006 struct loop
*loop
= graphite_create_new_loop (region
, next_e
, stmt
,
1007 context_loop
, newivs
,
1008 newivs_index
, params_index
,
1010 edge last_e
= single_exit (loop
);
1011 edge to_body
= single_succ_edge (loop
->header
);
1012 basic_block after
= to_body
->dest
;
1014 /* Create a basic block for loop close phi nodes. */
1015 last_e
= single_succ_edge (split_edge (last_e
));
1017 /* Translate the body of the loop. */
1018 next_e
= translate_clast (region
, loop
, stmt
->body
, to_body
,
1019 newivs
, newivs_index
, bb_pbb_mapping
, level
+ 1,
1021 redirect_edge_succ_nodup (next_e
, after
);
1022 set_immediate_dominator (CDI_DOMINATORS
, next_e
->dest
, next_e
->src
);
1024 if (flag_loop_parallelize_all
1025 && !dependency_in_loop_p (loop
, bb_pbb_mapping
,
1026 get_scattering_level (level
)))
1027 loop
->can_be_parallel
= true;
1032 /* Translates a clast for statement STMT to gimple. First a guard is created
1033 protecting the loop, if it is executed zero times. In this guard we create
1034 the real loop structure.
1036 - REGION is the sese region we used to generate the scop.
1037 - NEXT_E is the edge where new generated code should be attached.
1038 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
1039 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
1042 translate_clast_for (sese region
, loop_p context_loop
, struct clast_for
*stmt
,
1043 edge next_e
, VEC (tree
, heap
) **newivs
,
1044 htab_t newivs_index
, htab_t bb_pbb_mapping
, int level
,
1045 htab_t params_index
)
1047 edge last_e
= graphite_create_new_loop_guard (region
, next_e
, stmt
, *newivs
,
1048 newivs_index
, params_index
);
1049 edge true_e
= get_true_edge_from_guard_bb (next_e
->dest
);
1051 translate_clast_for_loop (region
, context_loop
, stmt
, true_e
, newivs
,
1052 newivs_index
, bb_pbb_mapping
, level
,
1057 /* Translates a clast guard statement STMT to gimple.
1059 - REGION is the sese region we used to generate the scop.
1060 - NEXT_E is the edge where new generated code should be attached.
1061 - CONTEXT_LOOP is the loop in which the generated code will be placed
1062 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
1063 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
1066 translate_clast_guard (sese region
, loop_p context_loop
,
1067 struct clast_guard
*stmt
, edge next_e
,
1068 VEC (tree
, heap
) **newivs
,
1069 htab_t newivs_index
, htab_t bb_pbb_mapping
, int level
,
1070 htab_t params_index
)
1072 edge last_e
= graphite_create_new_guard (region
, next_e
, stmt
, *newivs
,
1073 newivs_index
, params_index
);
1074 edge true_e
= get_true_edge_from_guard_bb (next_e
->dest
);
1076 translate_clast (region
, context_loop
, stmt
->then
, true_e
,
1077 newivs
, newivs_index
, bb_pbb_mapping
,
1078 level
, params_index
);
1082 /* Translates a CLAST statement STMT to GCC representation in the
1085 - NEXT_E is the edge where new generated code should be attached.
1086 - CONTEXT_LOOP is the loop in which the generated code will be placed
1087 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping. */
1089 translate_clast (sese region
, loop_p context_loop
, struct clast_stmt
*stmt
,
1090 edge next_e
, VEC (tree
, heap
) **newivs
,
1091 htab_t newivs_index
, htab_t bb_pbb_mapping
, int level
,
1092 htab_t params_index
)
1097 if (CLAST_STMT_IS_A (stmt
, stmt_root
))
1100 else if (CLAST_STMT_IS_A (stmt
, stmt_user
))
1101 next_e
= translate_clast_user (region
, (struct clast_user_stmt
*) stmt
,
1102 next_e
, newivs
, newivs_index
,
1103 bb_pbb_mapping
, params_index
);
1105 else if (CLAST_STMT_IS_A (stmt
, stmt_for
))
1106 next_e
= translate_clast_for (region
, context_loop
,
1107 (struct clast_for
*) stmt
, next_e
,
1108 newivs
, newivs_index
,
1109 bb_pbb_mapping
, level
, params_index
);
1111 else if (CLAST_STMT_IS_A (stmt
, stmt_guard
))
1112 next_e
= translate_clast_guard (region
, context_loop
,
1113 (struct clast_guard
*) stmt
, next_e
,
1114 newivs
, newivs_index
,
1115 bb_pbb_mapping
, level
, params_index
);
1117 else if (CLAST_STMT_IS_A (stmt
, stmt_block
))
1118 next_e
= translate_clast (region
, context_loop
,
1119 ((struct clast_block
*) stmt
)->body
,
1120 next_e
, newivs
, newivs_index
,
1121 bb_pbb_mapping
, level
, params_index
);
1125 recompute_all_dominators ();
1128 return translate_clast (region
, context_loop
, stmt
->next
, next_e
,
1129 newivs
, newivs_index
,
1130 bb_pbb_mapping
, level
, params_index
);
1133 /* Free the SCATTERING domain list. */
1136 free_scattering (CloogScatteringList
*scattering
)
1140 CloogScattering
*dom
= cloog_scattering (scattering
);
1141 CloogScatteringList
*next
= cloog_next_scattering (scattering
);
1143 cloog_scattering_free (dom
);
1149 /* Initialize Cloog's parameter names from the names used in GIMPLE.
1150 Initialize Cloog's iterator names, using 'graphite_iterator_%d'
1151 from 0 to scop_nb_loops (scop). */
1154 initialize_cloog_names (scop_p scop
, CloogProgram
*prog
)
1156 sese region
= SCOP_REGION (scop
);
1158 int nb_iterators
= scop_max_loop_depth (scop
);
1159 int nb_scattering
= cloog_program_nb_scattdims (prog
);
1160 int nb_parameters
= VEC_length (tree
, SESE_PARAMS (region
));
1161 char **iterators
= XNEWVEC (char *, nb_iterators
* 2);
1162 char **scattering
= XNEWVEC (char *, nb_scattering
);
1163 char **parameters
= XNEWVEC (char *, nb_parameters
);
1165 cloog_program_set_names (prog
, cloog_names_malloc ());
1167 for (i
= 0; i
< nb_parameters
; i
++)
1169 tree param
= VEC_index (tree
, SESE_PARAMS(region
), i
);
1170 const char *name
= get_name (param
);
1176 len
= strlen (name
);
1178 parameters
[i
] = XNEWVEC (char, len
+ 1);
1179 snprintf (parameters
[i
], len
, "%s_%d", name
, SSA_NAME_VERSION (param
));
1182 cloog_names_set_nb_parameters (cloog_program_names (prog
), nb_parameters
);
1183 cloog_names_set_parameters (cloog_program_names (prog
), parameters
);
1185 for (i
= 0; i
< nb_iterators
; i
++)
1188 iterators
[i
] = XNEWVEC (char, len
);
1189 snprintf (iterators
[i
], len
, "git_%d", i
);
1192 cloog_names_set_nb_iterators (cloog_program_names (prog
),
1194 cloog_names_set_iterators (cloog_program_names (prog
),
1197 for (i
= 0; i
< nb_scattering
; i
++)
1200 scattering
[i
] = XNEWVEC (char, len
);
1201 snprintf (scattering
[i
], len
, "scat_%d", i
);
1204 cloog_names_set_nb_scattering (cloog_program_names (prog
),
1206 cloog_names_set_scattering (cloog_program_names (prog
),
1210 /* Initialize a CLooG input file. */
1213 init_cloog_input_file (int scop_number
)
1215 FILE *graphite_out_file
;
1216 int len
= strlen (dump_base_name
);
1217 char *dumpname
= XNEWVEC (char, len
+ 25);
1218 char *s_scop_number
= XNEWVEC (char, 15);
1220 memcpy (dumpname
, dump_base_name
, len
+ 1);
1221 strip_off_ending (dumpname
, len
);
1222 sprintf (s_scop_number
, ".%d", scop_number
);
1223 strcat (dumpname
, s_scop_number
);
1224 strcat (dumpname
, ".cloog");
1225 graphite_out_file
= fopen (dumpname
, "w+b");
1227 if (graphite_out_file
== 0)
1228 fatal_error ("can%'t open %s for writing: %m", dumpname
);
1232 return graphite_out_file
;
1235 /* Build cloog program for SCoP. */
1238 build_cloog_prog (scop_p scop
, CloogProgram
*prog
,
1239 CloogOptions
*options
, CloogState
*state ATTRIBUTE_UNUSED
)
1242 int max_nb_loops
= scop_max_loop_depth (scop
);
1244 CloogLoop
*loop_list
= NULL
;
1245 CloogBlockList
*block_list
= NULL
;
1246 CloogScatteringList
*scattering
= NULL
;
1247 int nbs
= 2 * max_nb_loops
+ 1;
1250 cloog_program_set_context
1251 (prog
, new_Cloog_Domain_from_ppl_Pointset_Powerset (SCOP_CONTEXT (scop
),
1252 scop_nb_params (scop
), state
));
1253 nbs
= unify_scattering_dimensions (scop
);
1254 scaldims
= (int *) xmalloc (nbs
* (sizeof (int)));
1255 cloog_program_set_nb_scattdims (prog
, nbs
);
1256 initialize_cloog_names (scop
, prog
);
1258 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
1260 CloogStatement
*stmt
;
1264 /* Dead code elimination: when the domain of a PBB is empty,
1265 don't generate code for the PBB. */
1266 if (ppl_Pointset_Powerset_C_Polyhedron_is_empty (PBB_DOMAIN (pbb
)))
1269 /* Build the new statement and its block. */
1270 stmt
= cloog_statement_alloc (state
, pbb_index (pbb
));
1271 dom
= new_Cloog_Domain_from_ppl_Pointset_Powerset (PBB_DOMAIN (pbb
),
1272 scop_nb_params (scop
),
1274 block
= cloog_block_alloc (stmt
, 0, NULL
, pbb_dim_iter_domain (pbb
));
1275 cloog_statement_set_usr (stmt
, pbb
);
1277 /* Build loop list. */
1279 CloogLoop
*new_loop_list
= cloog_loop_malloc (state
);
1280 cloog_loop_set_next (new_loop_list
, loop_list
);
1281 cloog_loop_set_domain (new_loop_list
, dom
);
1282 cloog_loop_set_block (new_loop_list
, block
);
1283 loop_list
= new_loop_list
;
1286 /* Build block list. */
1288 CloogBlockList
*new_block_list
= cloog_block_list_malloc ();
1290 cloog_block_list_set_next (new_block_list
, block_list
);
1291 cloog_block_list_set_block (new_block_list
, block
);
1292 block_list
= new_block_list
;
1295 /* Build scattering list. */
1297 /* XXX: Replace with cloog_domain_list_alloc(), when available. */
1298 CloogScatteringList
*new_scattering
1299 = (CloogScatteringList
*) xmalloc (sizeof (CloogScatteringList
));
1300 ppl_Polyhedron_t scat
;
1301 CloogScattering
*dom
;
1303 scat
= PBB_TRANSFORMED_SCATTERING (pbb
);
1304 dom
= new_Cloog_Scattering_from_ppl_Polyhedron
1305 (scat
, scop_nb_params (scop
), pbb_nb_scattering_transform (pbb
),
1308 cloog_set_next_scattering (new_scattering
, scattering
);
1309 cloog_set_scattering (new_scattering
, dom
);
1310 scattering
= new_scattering
;
1314 cloog_program_set_loop (prog
, loop_list
);
1315 cloog_program_set_blocklist (prog
, block_list
);
1317 for (i
= 0; i
< nbs
; i
++)
1320 cloog_program_set_scaldims (prog
, scaldims
);
1322 /* Extract scalar dimensions to simplify the code generation problem. */
1323 cloog_program_extract_scalars (prog
, scattering
, options
);
1325 /* Dump a .cloog input file, if requested. This feature is only
1326 enabled in the Graphite branch. */
1329 static size_t file_scop_number
= 0;
1330 FILE *cloog_file
= init_cloog_input_file (file_scop_number
);
1332 cloog_program_dump_cloog (cloog_file
, prog
, scattering
);
1336 /* Apply scattering. */
1337 cloog_program_scatter (prog
, scattering
, options
);
1338 free_scattering (scattering
);
1340 /* Iterators corresponding to scalar dimensions have to be extracted. */
1341 cloog_names_scalarize (cloog_program_names (prog
), nbs
,
1342 cloog_program_scaldims (prog
));
1344 /* Free blocklist. */
1346 CloogBlockList
*next
= cloog_program_blocklist (prog
);
1350 CloogBlockList
*toDelete
= next
;
1351 next
= cloog_block_list_next (next
);
1352 cloog_block_list_set_next (toDelete
, NULL
);
1353 cloog_block_list_set_block (toDelete
, NULL
);
1354 cloog_block_list_free (toDelete
);
1356 cloog_program_set_blocklist (prog
, NULL
);
1360 /* Return the options that will be used in GLOOG. */
1362 static CloogOptions
*
1363 set_cloog_options (CloogState
*state ATTRIBUTE_UNUSED
)
1365 CloogOptions
*options
= cloog_options_malloc (state
);
1367 /* Change cloog output language to C. If we do use FORTRAN instead, cloog
1368 will stop e.g. with "ERROR: unbounded loops not allowed in FORTRAN.", if
1369 we pass an incomplete program to cloog. */
1370 options
->language
= LANGUAGE_C
;
1372 /* Enable complex equality spreading: removes dummy statements
1373 (assignments) in the generated code which repeats the
1374 substitution equations for statements. This is useless for
1379 /* Silence CLooG to avoid failing tests due to debug output to stderr. */
1382 /* Enable C pretty-printing mode: normalizes the substitution
1383 equations for statements. */
1387 /* Allow cloog to build strides with a stride width different to one.
1388 This example has stride = 4:
1390 for (i = 0; i < 20; i += 4)
1392 options
->strides
= 1;
1394 /* Disable optimizations and make cloog generate source code closer to the
1395 input. This is useful for debugging, but later we want the optimized
1398 XXX: We can not disable optimizations, as loop blocking is not working
1403 options
->l
= INT_MAX
;
1409 /* Prints STMT to STDERR. */
1412 print_clast_stmt (FILE *file
, struct clast_stmt
*stmt
)
1414 CloogState
*state
= cloog_state_malloc ();
1415 CloogOptions
*options
= set_cloog_options (state
);
1417 clast_pprint (file
, stmt
, 0, options
);
1418 cloog_options_free (options
);
1419 cloog_state_free (state
);
1422 /* Prints STMT to STDERR. */
1425 debug_clast_stmt (struct clast_stmt
*stmt
)
1427 print_clast_stmt (stderr
, stmt
);
1430 /* Translate SCOP to a CLooG program and clast. These two
1431 representations should be freed together: a clast cannot be used
1432 without a program. */
1435 scop_to_clast (scop_p scop
, CloogState
*state
)
1437 CloogOptions
*options
= set_cloog_options (state
);
1438 cloog_prog_clast pc
;
1440 /* Connect new cloog prog generation to graphite. */
1441 pc
.prog
= cloog_program_malloc ();
1442 build_cloog_prog (scop
, pc
.prog
, options
, state
);
1443 pc
.prog
= cloog_program_generate (pc
.prog
, options
);
1444 pc
.stmt
= cloog_clast_create (pc
.prog
, options
);
1446 cloog_options_free (options
);
1450 /* Prints to FILE the code generated by CLooG for SCOP. */
1453 print_generated_program (FILE *file
, scop_p scop
)
1455 CloogState
*state
= cloog_state_malloc ();
1456 CloogOptions
*options
= set_cloog_options (state
);
1458 cloog_prog_clast pc
= scop_to_clast (scop
, state
);
1460 fprintf (file
, " (prog: \n");
1461 cloog_program_print (file
, pc
.prog
);
1462 fprintf (file
, " )\n");
1464 fprintf (file
, " (clast: \n");
1465 clast_pprint (file
, pc
.stmt
, 0, options
);
1466 fprintf (file
, " )\n");
1468 cloog_options_free (options
);
1469 cloog_clast_free (pc
.stmt
);
1470 cloog_program_free (pc
.prog
);
1473 /* Prints to STDERR the code generated by CLooG for SCOP. */
1476 debug_generated_program (scop_p scop
)
1478 print_generated_program (stderr
, scop
);
1481 /* Add CLooG names to parameter index. The index is used to translate
1482 back from CLooG names to GCC trees. */
1485 create_params_index (htab_t index_table
, CloogProgram
*prog
) {
1486 CloogNames
* names
= cloog_program_names (prog
);
1487 int nb_parameters
= cloog_names_nb_parameters (names
);
1488 char **parameters
= cloog_names_parameters (names
);
1491 for (i
= 0; i
< nb_parameters
; i
++)
1492 save_clast_name_index (index_table
, parameters
[i
], i
);
1495 /* GIMPLE Loop Generator: generates loops from STMT in GIMPLE form for
1496 the given SCOP. Return true if code generation succeeded.
1497 BB_PBB_MAPPING is a basic_block and it's related poly_bb_p mapping.
1501 gloog (scop_p scop
, htab_t bb_pbb_mapping
)
1503 VEC (tree
, heap
) *newivs
= VEC_alloc (tree
, heap
, 10);
1504 loop_p context_loop
;
1505 sese region
= SCOP_REGION (scop
);
1506 ifsese if_region
= NULL
;
1507 htab_t newivs_index
, params_index
;
1508 cloog_prog_clast pc
;
1511 state
= cloog_state_malloc ();
1512 timevar_push (TV_GRAPHITE_CODE_GEN
);
1513 gloog_error
= false;
1515 pc
= scop_to_clast (scop
, state
);
1517 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1519 fprintf (dump_file
, "\nCLAST generated by CLooG: \n");
1520 print_clast_stmt (dump_file
, pc
.stmt
);
1521 fprintf (dump_file
, "\n");
1524 recompute_all_dominators ();
1527 if_region
= move_sese_in_condition (region
);
1528 sese_insert_phis_for_liveouts (region
,
1529 if_region
->region
->exit
->src
,
1530 if_region
->false_region
->exit
,
1531 if_region
->true_region
->exit
);
1532 recompute_all_dominators ();
1535 context_loop
= SESE_ENTRY (region
)->src
->loop_father
;
1536 newivs_index
= htab_create (10, clast_name_index_elt_info
,
1537 eq_clast_name_indexes
, free
);
1538 params_index
= htab_create (10, clast_name_index_elt_info
,
1539 eq_clast_name_indexes
, free
);
1541 create_params_index (params_index
, pc
.prog
);
1543 translate_clast (region
, context_loop
, pc
.stmt
,
1544 if_region
->true_region
->entry
,
1545 &newivs
, newivs_index
,
1546 bb_pbb_mapping
, 1, params_index
);
1549 recompute_all_dominators ();
1553 set_ifsese_condition (if_region
, integer_zero_node
);
1555 free (if_region
->true_region
);
1556 free (if_region
->region
);
1559 htab_delete (newivs_index
);
1560 htab_delete (params_index
);
1561 VEC_free (tree
, heap
, newivs
);
1562 cloog_clast_free (pc
.stmt
);
1563 cloog_program_free (pc
.prog
);
1564 timevar_pop (TV_GRAPHITE_CODE_GEN
);
1566 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1570 int num_no_dependency
= 0;
1572 FOR_EACH_LOOP (li
, loop
, 0)
1573 if (loop
->can_be_parallel
)
1574 num_no_dependency
++;
1576 fprintf (dump_file
, "\n%d loops carried no dependency.\n",
1580 cloog_state_free (state
);
1582 return !gloog_error
;