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_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 (clast_name_p name
, htab_t index_table
)
101 struct clast_name_index tmp
;
105 gcc_assert (name
->type
== clast_expr_name
);
106 tmp
.name
= ((const struct clast_name
*) name
)->name
;
111 slot
= htab_find_slot (index_table
, &tmp
, NO_INSERT
);
114 return ((struct clast_name_index
*) *slot
)->index
;
119 /* Records in INDEX_TABLE the INDEX for NAME. */
122 save_clast_name_index (htab_t index_table
, const char *name
, int index
)
124 struct clast_name_index tmp
;
128 slot
= htab_find_slot (index_table
, &tmp
, INSERT
);
135 *slot
= new_clast_name_index (name
, index
);
139 /* Computes a hash function for database element ELT. */
141 static inline hashval_t
142 clast_name_index_elt_info (const void *elt
)
144 return htab_hash_pointer (((const struct clast_name_index
*) elt
)->name
);
147 /* Compares database elements E1 and E2. */
150 eq_clast_name_indexes (const void *e1
, const void *e2
)
152 const struct clast_name_index
*elt1
= (const struct clast_name_index
*) e1
;
153 const struct clast_name_index
*elt2
= (const struct clast_name_index
*) e2
;
155 return (elt1
->name
== elt2
->name
);
158 /* For a given scattering dimension, return the new induction variable
162 newivs_to_depth_to_newiv (VEC (tree
, heap
) *newivs
, int depth
)
164 return VEC_index (tree
, newivs
, depth
);
169 /* Returns the tree variable from the name NAME that was given in
170 Cloog representation. */
173 clast_name_to_gcc (clast_name_p name
, sese region
, VEC (tree
, heap
) *newivs
,
174 htab_t newivs_index
, htab_t params_index
)
177 VEC (tree
, heap
) *params
= SESE_PARAMS (region
);
179 if (params
&& params_index
)
181 index
= clast_name_to_index (name
, params_index
);
184 return VEC_index (tree
, params
, index
);
187 gcc_assert (newivs
&& newivs_index
);
188 index
= clast_name_to_index (name
, newivs_index
);
189 gcc_assert (index
>= 0);
191 return newivs_to_depth_to_newiv (newivs
, index
);
194 /* Returns the signed maximal precision type for expressions TYPE1 and TYPE2. */
197 max_signed_precision_type (tree type1
, tree type2
)
199 int p1
= TYPE_PRECISION (type1
);
200 int p2
= TYPE_PRECISION (type2
);
203 enum machine_mode mode
;
206 precision
= TYPE_UNSIGNED (type1
) ? p1
* 2 : p1
;
208 precision
= TYPE_UNSIGNED (type2
) ? p2
* 2 : p2
;
210 if (precision
> BITS_PER_WORD
)
213 return integer_type_node
;
216 mode
= smallest_mode_for_size (precision
, MODE_INT
);
217 precision
= GET_MODE_PRECISION (mode
);
218 type
= build_nonstandard_integer_type (precision
, false);
223 return integer_type_node
;
229 /* Returns the maximal precision type for expressions TYPE1 and TYPE2. */
232 max_precision_type (tree type1
, tree type2
)
234 if (POINTER_TYPE_P (type1
))
237 if (POINTER_TYPE_P (type2
))
240 if (!TYPE_UNSIGNED (type1
)
241 || !TYPE_UNSIGNED (type2
))
242 return max_signed_precision_type (type1
, type2
);
244 return TYPE_PRECISION (type1
) > TYPE_PRECISION (type2
) ? type1
: type2
;
248 clast_to_gcc_expression (tree
, struct clast_expr
*, sese
, VEC (tree
, heap
) *,
251 /* Converts a Cloog reduction expression R with reduction operation OP
252 to a GCC expression tree of type TYPE. */
255 clast_to_gcc_expression_red (tree type
, enum tree_code op
,
256 struct clast_reduction
*r
,
257 sese region
, VEC (tree
, heap
) *newivs
,
258 htab_t newivs_index
, htab_t params_index
)
261 tree res
= clast_to_gcc_expression (type
, r
->elts
[0], region
, newivs
,
262 newivs_index
, params_index
);
263 tree operand_type
= (op
== POINTER_PLUS_EXPR
) ? sizetype
: type
;
265 for (i
= 1; i
< r
->n
; i
++)
267 tree t
= clast_to_gcc_expression (operand_type
, r
->elts
[i
], region
,
268 newivs
, newivs_index
, params_index
);
269 res
= fold_build2 (op
, type
, res
, t
);
275 /* Converts a Cloog AST expression E back to a GCC expression tree of
279 clast_to_gcc_expression (tree type
, struct clast_expr
*e
,
280 sese region
, VEC (tree
, heap
) *newivs
,
281 htab_t newivs_index
, htab_t params_index
)
285 case clast_expr_term
:
287 struct clast_term
*t
= (struct clast_term
*) e
;
291 if (mpz_cmp_si (t
->val
, 1) == 0)
293 tree name
= clast_name_to_gcc (t
->var
, region
, newivs
,
294 newivs_index
, params_index
);
296 if (POINTER_TYPE_P (TREE_TYPE (name
)) != POINTER_TYPE_P (type
))
297 name
= fold_convert (sizetype
, name
);
299 name
= fold_convert (type
, name
);
303 else if (mpz_cmp_si (t
->val
, -1) == 0)
305 tree name
= clast_name_to_gcc (t
->var
, region
, newivs
,
306 newivs_index
, params_index
);
308 if (POINTER_TYPE_P (TREE_TYPE (name
)) != POINTER_TYPE_P (type
))
309 name
= fold_convert (sizetype
, name
);
311 name
= fold_convert (type
, name
);
313 return fold_build1 (NEGATE_EXPR
, type
, name
);
317 tree name
= clast_name_to_gcc (t
->var
, region
, newivs
,
318 newivs_index
, params_index
);
319 tree cst
= gmp_cst_to_tree (type
, t
->val
);
321 if (POINTER_TYPE_P (TREE_TYPE (name
)) != POINTER_TYPE_P (type
))
322 name
= fold_convert (sizetype
, name
);
324 name
= fold_convert (type
, name
);
326 if (!POINTER_TYPE_P (type
))
327 return fold_build2 (MULT_EXPR
, type
, cst
, name
);
334 return gmp_cst_to_tree (type
, t
->val
);
339 struct clast_reduction
*r
= (struct clast_reduction
*) e
;
344 return clast_to_gcc_expression_red
345 (type
, POINTER_TYPE_P (type
) ? POINTER_PLUS_EXPR
: PLUS_EXPR
,
346 r
, region
, newivs
, newivs_index
, params_index
);
349 return clast_to_gcc_expression_red (type
, MIN_EXPR
, r
, region
,
350 newivs
, newivs_index
,
354 return clast_to_gcc_expression_red (type
, MAX_EXPR
, r
, region
,
355 newivs
, newivs_index
,
366 struct clast_binary
*b
= (struct clast_binary
*) e
;
367 struct clast_expr
*lhs
= (struct clast_expr
*) b
->LHS
;
368 tree tl
= clast_to_gcc_expression (type
, lhs
, region
, newivs
,
369 newivs_index
, params_index
);
370 tree tr
= gmp_cst_to_tree (type
, b
->RHS
);
375 return fold_build2 (FLOOR_DIV_EXPR
, type
, tl
, tr
);
378 return fold_build2 (CEIL_DIV_EXPR
, type
, tl
, tr
);
381 return fold_build2 (EXACT_DIV_EXPR
, type
, tl
, tr
);
384 return fold_build2 (TRUNC_MOD_EXPR
, type
, tl
, tr
);
398 /* Return the precision needed to represent the value VAL. */
401 precision_for_value (mpz_t val
)
417 while (mpz_cmp (y
, x
) >= 0)
430 /* Return the precision needed to represent the values between LOW and
434 precision_for_interval (mpz_t low
, mpz_t up
)
439 gcc_assert (mpz_cmp (low
, up
) <= 0);
442 mpz_sub (diff
, up
, low
);
443 precision
= precision_for_value (diff
);
449 /* Return a type that could represent the integer value VAL. */
452 gcc_type_for_interval (mpz_t low
, mpz_t up
)
454 bool unsigned_p
= true;
455 int precision
, prec_up
, prec_int
;
457 enum machine_mode mode
;
459 gcc_assert (mpz_cmp (low
, up
) <= 0);
461 prec_up
= precision_for_value (up
);
462 prec_int
= precision_for_interval (low
, up
);
463 precision
= MAX (prec_up
, prec_int
);
465 if (precision
> BITS_PER_WORD
)
468 return integer_type_node
;
471 if (mpz_sgn (low
) <= 0)
474 else if (precision
< BITS_PER_WORD
)
480 mode
= smallest_mode_for_size (precision
, MODE_INT
);
481 precision
= GET_MODE_PRECISION (mode
);
482 type
= build_nonstandard_integer_type (precision
, unsigned_p
);
487 return integer_type_node
;
493 /* Return a type that could represent the integer value VAL, or
494 otherwise return NULL_TREE. */
497 gcc_type_for_value (mpz_t val
)
499 return gcc_type_for_interval (val
, val
);
502 /* Return the type for the clast_term T used in STMT. */
505 gcc_type_for_clast_term (struct clast_term
*t
,
506 sese region
, VEC (tree
, heap
) *newivs
,
507 htab_t newivs_index
, htab_t params_index
)
509 gcc_assert (t
->expr
.type
== clast_expr_term
);
512 return gcc_type_for_value (t
->val
);
514 return TREE_TYPE (clast_name_to_gcc (t
->var
, region
, newivs
,
515 newivs_index
, params_index
));
519 gcc_type_for_clast_expr (struct clast_expr
*, sese
,
520 VEC (tree
, heap
) *, htab_t
, htab_t
);
522 /* Return the type for the clast_reduction R used in STMT. */
525 gcc_type_for_clast_red (struct clast_reduction
*r
, sese region
,
526 VEC (tree
, heap
) *newivs
,
527 htab_t newivs_index
, htab_t params_index
)
530 tree type
= NULL_TREE
;
533 return gcc_type_for_clast_expr (r
->elts
[0], region
, newivs
,
534 newivs_index
, params_index
);
541 type
= gcc_type_for_clast_expr (r
->elts
[0], region
, newivs
,
542 newivs_index
, params_index
);
543 for (i
= 1; i
< r
->n
; i
++)
544 type
= max_precision_type (type
, gcc_type_for_clast_expr
545 (r
->elts
[i
], region
, newivs
,
546 newivs_index
, params_index
));
558 /* Return the type for the clast_binary B used in STMT. */
561 gcc_type_for_clast_bin (struct clast_binary
*b
,
562 sese region
, VEC (tree
, heap
) *newivs
,
563 htab_t newivs_index
, htab_t params_index
)
565 tree l
= gcc_type_for_clast_expr ((struct clast_expr
*) b
->LHS
, region
,
566 newivs
, newivs_index
, params_index
);
567 tree r
= gcc_type_for_value (b
->RHS
);
568 return max_signed_precision_type (l
, r
);
571 /* Returns the type for the CLAST expression E when used in statement
575 gcc_type_for_clast_expr (struct clast_expr
*e
,
576 sese region
, VEC (tree
, heap
) *newivs
,
577 htab_t newivs_index
, htab_t params_index
)
581 case clast_expr_term
:
582 return gcc_type_for_clast_term ((struct clast_term
*) e
, region
,
583 newivs
, newivs_index
, params_index
);
586 return gcc_type_for_clast_red ((struct clast_reduction
*) e
, region
,
587 newivs
, newivs_index
, params_index
);
590 return gcc_type_for_clast_bin ((struct clast_binary
*) e
, region
,
591 newivs
, newivs_index
, params_index
);
600 /* Returns the type for the equation CLEQ. */
603 gcc_type_for_clast_eq (struct clast_equation
*cleq
,
604 sese region
, VEC (tree
, heap
) *newivs
,
605 htab_t newivs_index
, htab_t params_index
)
607 tree l
= gcc_type_for_clast_expr (cleq
->LHS
, region
, newivs
,
608 newivs_index
, params_index
);
609 tree r
= gcc_type_for_clast_expr (cleq
->RHS
, region
, newivs
,
610 newivs_index
, params_index
);
611 return max_precision_type (l
, r
);
614 /* Translates a clast equation CLEQ to a tree. */
617 graphite_translate_clast_equation (sese region
,
618 struct clast_equation
*cleq
,
619 VEC (tree
, heap
) *newivs
,
620 htab_t newivs_index
, htab_t params_index
)
623 tree type
= gcc_type_for_clast_eq (cleq
, region
, newivs
, newivs_index
,
625 tree lhs
= clast_to_gcc_expression (type
, cleq
->LHS
, region
, newivs
,
626 newivs_index
, params_index
);
627 tree rhs
= clast_to_gcc_expression (type
, cleq
->RHS
, region
, newivs
,
628 newivs_index
, params_index
);
633 else if (cleq
->sign
> 0)
639 return fold_build2 (comp
, boolean_type_node
, lhs
, rhs
);
642 /* Creates the test for the condition in STMT. */
645 graphite_create_guard_cond_expr (sese region
, struct clast_guard
*stmt
,
646 VEC (tree
, heap
) *newivs
,
647 htab_t newivs_index
, htab_t params_index
)
652 for (i
= 0; i
< stmt
->n
; i
++)
654 tree eq
= graphite_translate_clast_equation (region
, &stmt
->eq
[i
],
655 newivs
, newivs_index
,
659 cond
= fold_build2 (TRUTH_AND_EXPR
, TREE_TYPE (eq
), cond
, eq
);
667 /* Creates a new if region corresponding to Cloog's guard. */
670 graphite_create_new_guard (sese region
, edge entry_edge
,
671 struct clast_guard
*stmt
,
672 VEC (tree
, heap
) *newivs
,
673 htab_t newivs_index
, htab_t params_index
)
675 tree cond_expr
= graphite_create_guard_cond_expr (region
, stmt
, newivs
,
676 newivs_index
, params_index
);
677 edge exit_edge
= create_empty_if_region_on_edge (entry_edge
, cond_expr
);
681 /* Compute the lower bound LOW and upper bound UP for the induction
682 variable at LEVEL for the statement PBB, based on the transformed
683 scattering of PBB: T|I|G|Cst, with T the scattering transform, I
684 the iteration domain, and G the context parameters. */
687 compute_bounds_for_level (poly_bb_p pbb
, int level
, mpz_t low
, mpz_t up
)
689 ppl_Pointset_Powerset_C_Polyhedron_t ps
;
690 ppl_Linear_Expression_t le
;
692 combine_context_id_scat (&ps
, pbb
, false);
694 /* Prepare the linear expression corresponding to the level that we
695 want to maximize/minimize. */
697 ppl_dimension_type dim
= pbb_nb_scattering_transform (pbb
)
698 + pbb_dim_iter_domain (pbb
) + pbb_nb_params (pbb
);
700 ppl_new_Linear_Expression_with_dimension (&le
, dim
);
701 ppl_set_coef (le
, 2 * level
+ 1, 1);
704 ppl_max_for_le_pointset (ps
, le
, up
);
705 ppl_min_for_le_pointset (ps
, le
, low
);
706 ppl_delete_Linear_Expression (le
);
707 ppl_delete_Pointset_Powerset_C_Polyhedron (ps
);
710 /* Compute the type for the induction variable at LEVEL for the
711 statement PBB, based on the transformed schedule of PBB. */
714 compute_type_for_level (poly_bb_p pbb
, int level
)
722 compute_bounds_for_level (pbb
, level
, low
, up
);
723 type
= gcc_type_for_interval (low
, up
);
730 /* Walks a CLAST and returns the first statement in the body of a
733 static struct clast_user_stmt
*
734 clast_get_body_of_loop (struct clast_stmt
*stmt
)
737 || CLAST_STMT_IS_A (stmt
, stmt_user
))
738 return (struct clast_user_stmt
*) stmt
;
740 if (CLAST_STMT_IS_A (stmt
, stmt_for
))
741 return clast_get_body_of_loop (((struct clast_for
*) stmt
)->body
);
743 if (CLAST_STMT_IS_A (stmt
, stmt_guard
))
744 return clast_get_body_of_loop (((struct clast_guard
*) stmt
)->then
);
746 if (CLAST_STMT_IS_A (stmt
, stmt_block
))
747 return clast_get_body_of_loop (((struct clast_block
*) stmt
)->body
);
752 /* Returns the type for the induction variable for the loop translated
756 gcc_type_for_iv_of_clast_loop (struct clast_for
*stmt_for
, int level
,
757 tree lb_type
, tree ub_type
)
759 struct clast_stmt
*stmt
= (struct clast_stmt
*) stmt_for
;
760 struct clast_user_stmt
*body
= clast_get_body_of_loop (stmt
);
761 CloogStatement
*cs
= body
->statement
;
762 poly_bb_p pbb
= (poly_bb_p
) cloog_statement_usr (cs
);
764 return max_signed_precision_type (lb_type
, max_precision_type
765 (ub_type
, compute_type_for_level
769 /* Creates a new LOOP corresponding to Cloog's STMT. Inserts an
770 induction variable for the new LOOP. New LOOP is attached to CFG
771 starting at ENTRY_EDGE. LOOP is inserted into the loop tree and
772 becomes the child loop of the OUTER_LOOP. NEWIVS_INDEX binds
773 CLooG's scattering name to the induction variable created for the
774 loop of STMT. The new induction variable is inserted in the NEWIVS
778 graphite_create_new_loop (sese region
, edge entry_edge
,
779 struct clast_for
*stmt
,
780 loop_p outer
, VEC (tree
, heap
) **newivs
,
781 htab_t newivs_index
, htab_t params_index
, int level
)
783 tree lb_type
= gcc_type_for_clast_expr (stmt
->LB
, region
, *newivs
,
784 newivs_index
, params_index
);
785 tree ub_type
= gcc_type_for_clast_expr (stmt
->UB
, region
, *newivs
,
786 newivs_index
, params_index
);
787 tree type
= gcc_type_for_iv_of_clast_loop (stmt
, level
, lb_type
, ub_type
);
788 tree lb
= clast_to_gcc_expression (type
, stmt
->LB
, region
, *newivs
,
789 newivs_index
, params_index
);
790 tree ub
= clast_to_gcc_expression (type
, stmt
->UB
, region
, *newivs
,
791 newivs_index
, params_index
);
792 tree stride
= gmp_cst_to_tree (type
, stmt
->stride
);
793 tree ivvar
= create_tmp_var (type
, "graphite_IV");
794 tree iv
, iv_after_increment
;
795 loop_p loop
= create_empty_loop_on_edge
796 (entry_edge
, lb
, stride
, ub
, ivvar
, &iv
, &iv_after_increment
,
797 outer
? outer
: entry_edge
->src
->loop_father
);
799 add_referenced_var (ivvar
);
801 save_clast_name_index (newivs_index
, stmt
->iterator
,
802 VEC_length (tree
, *newivs
));
803 VEC_safe_push (tree
, heap
, *newivs
, iv
);
807 /* Inserts in iv_map a tuple (OLD_LOOP->num, NEW_NAME) for the
808 induction variables of the loops around GBB in SESE. */
811 build_iv_mapping (VEC (tree
, heap
) *iv_map
, sese region
,
812 VEC (tree
, heap
) *newivs
, htab_t newivs_index
,
813 struct clast_user_stmt
*user_stmt
,
816 struct clast_stmt
*t
;
818 CloogStatement
*cs
= user_stmt
->statement
;
819 poly_bb_p pbb
= (poly_bb_p
) cloog_statement_usr (cs
);
820 gimple_bb_p gbb
= PBB_BLACK_BOX (pbb
);
822 for (t
= user_stmt
->substitutions
; t
; t
= t
->next
, depth
++)
824 struct clast_expr
*expr
= (struct clast_expr
*)
825 ((struct clast_assignment
*)t
)->RHS
;
826 tree type
= gcc_type_for_clast_expr (expr
, region
, newivs
,
827 newivs_index
, params_index
);
828 tree new_name
= clast_to_gcc_expression (type
, expr
, region
, newivs
,
829 newivs_index
, params_index
);
830 loop_p old_loop
= gbb_loop_at_index (gbb
, region
, depth
);
832 VEC_replace (tree
, iv_map
, old_loop
->num
, new_name
);
836 /* Construct bb_pbb_def with BB and PBB. */
839 new_bb_pbb_def (basic_block bb
, poly_bb_p pbb
)
841 bb_pbb_def
*bb_pbb_p
;
843 bb_pbb_p
= XNEW (bb_pbb_def
);
850 /* Mark BB with it's relevant PBB via hashing table BB_PBB_MAPPING. */
853 mark_bb_with_pbb (poly_bb_p pbb
, basic_block bb
, htab_t bb_pbb_mapping
)
859 x
= htab_find_slot (bb_pbb_mapping
, &tmp
, INSERT
);
862 *x
= new_bb_pbb_def (bb
, pbb
);
865 /* Find BB's related poly_bb_p in hash table BB_PBB_MAPPING. */
868 find_pbb_via_hash (htab_t bb_pbb_mapping
, basic_block bb
)
874 slot
= htab_find_slot (bb_pbb_mapping
, &tmp
, NO_INSERT
);
877 return ((bb_pbb_def
*) *slot
)->pbb
;
882 /* Check data dependency in LOOP at scattering level LEVEL.
883 BB_PBB_MAPPING is a basic_block and it's related poly_bb_p
887 dependency_in_loop_p (loop_p loop
, htab_t bb_pbb_mapping
, int level
)
890 basic_block
*bbs
= get_loop_body_in_dom_order (loop
);
892 for (i
= 0; i
< loop
->num_nodes
; i
++)
894 poly_bb_p pbb1
= find_pbb_via_hash (bb_pbb_mapping
, bbs
[i
]);
899 for (j
= 0; j
< loop
->num_nodes
; j
++)
901 poly_bb_p pbb2
= find_pbb_via_hash (bb_pbb_mapping
, bbs
[j
]);
906 if (dependency_between_pbbs_p (pbb1
, pbb2
, level
))
919 /* Translates a clast user statement STMT to gimple.
921 - REGION is the sese region we used to generate the scop.
922 - NEXT_E is the edge where new generated code should be attached.
923 - CONTEXT_LOOP is the loop in which the generated code will be placed
924 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
925 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
928 translate_clast_user (sese region
, struct clast_user_stmt
*stmt
, edge next_e
,
929 VEC (tree
, heap
) **newivs
,
930 htab_t newivs_index
, htab_t bb_pbb_mapping
,
935 poly_bb_p pbb
= (poly_bb_p
) cloog_statement_usr (stmt
->statement
);
936 gimple_bb_p gbb
= PBB_BLACK_BOX (pbb
);
937 VEC (tree
, heap
) *iv_map
;
939 if (GBB_BB (gbb
) == ENTRY_BLOCK_PTR
)
942 nb_loops
= number_of_loops ();
943 iv_map
= VEC_alloc (tree
, heap
, nb_loops
);
944 for (i
= 0; i
< nb_loops
; i
++)
945 VEC_quick_push (tree
, iv_map
, NULL_TREE
);
947 build_iv_mapping (iv_map
, region
, *newivs
, newivs_index
, stmt
, params_index
);
948 next_e
= copy_bb_and_scalar_dependences (GBB_BB (gbb
), region
,
950 VEC_free (tree
, heap
, iv_map
);
952 new_bb
= next_e
->src
;
953 mark_bb_with_pbb (pbb
, new_bb
, bb_pbb_mapping
);
954 update_ssa (TODO_update_ssa
);
959 /* Creates a new if region protecting the loop to be executed, if the execution
960 count is zero (lb > ub). */
963 graphite_create_new_loop_guard (sese region
, edge entry_edge
,
964 struct clast_for
*stmt
,
965 VEC (tree
, heap
) *newivs
,
966 htab_t newivs_index
, htab_t params_index
)
970 tree lb_type
= gcc_type_for_clast_expr (stmt
->LB
, region
, newivs
,
971 newivs_index
, params_index
);
972 tree ub_type
= gcc_type_for_clast_expr (stmt
->UB
, region
, newivs
,
973 newivs_index
, params_index
);
974 tree type
= max_precision_type (lb_type
, ub_type
);
975 tree lb
= clast_to_gcc_expression (type
, stmt
->LB
, region
, newivs
,
976 newivs_index
, params_index
);
977 tree ub
= clast_to_gcc_expression (type
, stmt
->UB
, region
, newivs
,
978 newivs_index
, params_index
);
979 tree one
= POINTER_TYPE_P (type
) ? size_one_node
980 : fold_convert (type
, integer_one_node
);
981 /* Adding +1 and using LT_EXPR helps with loop latches that have a
982 loop iteration count of "PARAMETER - 1". For PARAMETER == 0 this becomes
983 2^{32|64}, and the condition lb <= ub is true, even if we do not want this.
984 However lb < ub + 1 is false, as expected. */
985 tree ub_one
= fold_build2 (POINTER_TYPE_P (type
) ? POINTER_PLUS_EXPR
986 : PLUS_EXPR
, type
, ub
, one
);
988 /* When ub + 1 wraps around, use lb <= ub. */
989 if (integer_zerop (ub_one
))
990 cond_expr
= fold_build2 (LE_EXPR
, boolean_type_node
, lb
, ub
);
992 cond_expr
= fold_build2 (LT_EXPR
, boolean_type_node
, lb
, ub_one
);
994 exit_edge
= create_empty_if_region_on_edge (entry_edge
, cond_expr
);
1000 translate_clast (sese
, loop_p
, struct clast_stmt
*, edge
,
1001 VEC (tree
, heap
) **, htab_t
, htab_t
, int, htab_t
);
1003 /* Create the loop for a clast for statement.
1005 - REGION is the sese region we used to generate the scop.
1006 - NEXT_E is the edge where new generated code should be attached.
1007 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
1008 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
1011 translate_clast_for_loop (sese region
, loop_p context_loop
,
1012 struct clast_for
*stmt
, edge next_e
,
1013 VEC (tree
, heap
) **newivs
,
1014 htab_t newivs_index
, htab_t bb_pbb_mapping
,
1015 int level
, htab_t params_index
)
1017 struct loop
*loop
= graphite_create_new_loop (region
, next_e
, stmt
,
1018 context_loop
, newivs
,
1019 newivs_index
, params_index
,
1021 edge last_e
= single_exit (loop
);
1022 edge to_body
= single_succ_edge (loop
->header
);
1023 basic_block after
= to_body
->dest
;
1025 /* Create a basic block for loop close phi nodes. */
1026 last_e
= single_succ_edge (split_edge (last_e
));
1028 /* Translate the body of the loop. */
1029 next_e
= translate_clast (region
, loop
, stmt
->body
, to_body
,
1030 newivs
, newivs_index
, bb_pbb_mapping
, level
+ 1,
1032 redirect_edge_succ_nodup (next_e
, after
);
1033 set_immediate_dominator (CDI_DOMINATORS
, next_e
->dest
, next_e
->src
);
1035 if (flag_loop_parallelize_all
1036 && !dependency_in_loop_p (loop
, bb_pbb_mapping
,
1037 get_scattering_level (level
)))
1038 loop
->can_be_parallel
= true;
1043 /* Translates a clast for statement STMT to gimple. First a guard is created
1044 protecting the loop, if it is executed zero times. In this guard we create
1045 the real loop structure.
1047 - REGION is the sese region we used to generate the scop.
1048 - NEXT_E is the edge where new generated code should be attached.
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_for (sese region
, loop_p context_loop
, struct clast_for
*stmt
,
1054 edge next_e
, VEC (tree
, heap
) **newivs
,
1055 htab_t newivs_index
, htab_t bb_pbb_mapping
, int level
,
1056 htab_t params_index
)
1058 edge last_e
= graphite_create_new_loop_guard (region
, next_e
, stmt
, *newivs
,
1059 newivs_index
, params_index
);
1060 edge true_e
= get_true_edge_from_guard_bb (next_e
->dest
);
1062 translate_clast_for_loop (region
, context_loop
, stmt
, true_e
, newivs
,
1063 newivs_index
, bb_pbb_mapping
, level
,
1068 /* Translates a clast guard statement STMT to gimple.
1070 - REGION is the sese region we used to generate the scop.
1071 - NEXT_E is the edge where new generated code should be attached.
1072 - CONTEXT_LOOP is the loop in which the generated code will be placed
1073 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
1074 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
1077 translate_clast_guard (sese region
, loop_p context_loop
,
1078 struct clast_guard
*stmt
, edge next_e
,
1079 VEC (tree
, heap
) **newivs
,
1080 htab_t newivs_index
, htab_t bb_pbb_mapping
, int level
,
1081 htab_t params_index
)
1083 edge last_e
= graphite_create_new_guard (region
, next_e
, stmt
, *newivs
,
1084 newivs_index
, params_index
);
1085 edge true_e
= get_true_edge_from_guard_bb (next_e
->dest
);
1087 translate_clast (region
, context_loop
, stmt
->then
, true_e
,
1088 newivs
, newivs_index
, bb_pbb_mapping
,
1089 level
, params_index
);
1093 /* Translates a CLAST statement STMT to GCC representation in the
1096 - NEXT_E is the edge where new generated code should be attached.
1097 - CONTEXT_LOOP is the loop in which the generated code will be placed
1098 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping. */
1100 translate_clast (sese region
, loop_p context_loop
, struct clast_stmt
*stmt
,
1101 edge next_e
, VEC (tree
, heap
) **newivs
,
1102 htab_t newivs_index
, htab_t bb_pbb_mapping
, int level
,
1103 htab_t params_index
)
1108 if (CLAST_STMT_IS_A (stmt
, stmt_root
))
1111 else if (CLAST_STMT_IS_A (stmt
, stmt_user
))
1112 next_e
= translate_clast_user (region
, (struct clast_user_stmt
*) stmt
,
1113 next_e
, newivs
, newivs_index
,
1114 bb_pbb_mapping
, params_index
);
1116 else if (CLAST_STMT_IS_A (stmt
, stmt_for
))
1117 next_e
= translate_clast_for (region
, context_loop
,
1118 (struct clast_for
*) stmt
, next_e
,
1119 newivs
, newivs_index
,
1120 bb_pbb_mapping
, level
, params_index
);
1122 else if (CLAST_STMT_IS_A (stmt
, stmt_guard
))
1123 next_e
= translate_clast_guard (region
, context_loop
,
1124 (struct clast_guard
*) stmt
, next_e
,
1125 newivs
, newivs_index
,
1126 bb_pbb_mapping
, level
, params_index
);
1128 else if (CLAST_STMT_IS_A (stmt
, stmt_block
))
1129 next_e
= translate_clast (region
, context_loop
,
1130 ((struct clast_block
*) stmt
)->body
,
1131 next_e
, newivs
, newivs_index
,
1132 bb_pbb_mapping
, level
, params_index
);
1136 recompute_all_dominators ();
1139 return translate_clast (region
, context_loop
, stmt
->next
, next_e
,
1140 newivs
, newivs_index
,
1141 bb_pbb_mapping
, level
, params_index
);
1144 /* Free the SCATTERING domain list. */
1147 free_scattering (CloogScatteringList
*scattering
)
1151 CloogScattering
*dom
= cloog_scattering (scattering
);
1152 CloogScatteringList
*next
= cloog_next_scattering (scattering
);
1154 cloog_scattering_free (dom
);
1160 /* Initialize Cloog's parameter names from the names used in GIMPLE.
1161 Initialize Cloog's iterator names, using 'graphite_iterator_%d'
1162 from 0 to scop_nb_loops (scop). */
1165 initialize_cloog_names (scop_p scop
, CloogProgram
*prog
)
1167 sese region
= SCOP_REGION (scop
);
1169 int nb_iterators
= scop_max_loop_depth (scop
);
1170 int nb_scattering
= cloog_program_nb_scattdims (prog
);
1171 int nb_parameters
= VEC_length (tree
, SESE_PARAMS (region
));
1172 char **iterators
= XNEWVEC (char *, nb_iterators
* 2);
1173 char **scattering
= XNEWVEC (char *, nb_scattering
);
1174 char **parameters
= XNEWVEC (char *, nb_parameters
);
1176 cloog_program_set_names (prog
, cloog_names_malloc ());
1178 for (i
= 0; i
< nb_parameters
; i
++)
1180 tree param
= VEC_index (tree
, SESE_PARAMS(region
), i
);
1181 const char *name
= get_name (param
);
1187 len
= strlen (name
);
1189 parameters
[i
] = XNEWVEC (char, len
+ 1);
1190 snprintf (parameters
[i
], len
, "%s_%d", name
, SSA_NAME_VERSION (param
));
1193 cloog_names_set_nb_parameters (cloog_program_names (prog
), nb_parameters
);
1194 cloog_names_set_parameters (cloog_program_names (prog
), parameters
);
1196 for (i
= 0; i
< nb_iterators
; i
++)
1199 iterators
[i
] = XNEWVEC (char, len
);
1200 snprintf (iterators
[i
], len
, "git_%d", i
);
1203 cloog_names_set_nb_iterators (cloog_program_names (prog
),
1205 cloog_names_set_iterators (cloog_program_names (prog
),
1208 for (i
= 0; i
< nb_scattering
; i
++)
1211 scattering
[i
] = XNEWVEC (char, len
);
1212 snprintf (scattering
[i
], len
, "scat_%d", i
);
1215 cloog_names_set_nb_scattering (cloog_program_names (prog
),
1217 cloog_names_set_scattering (cloog_program_names (prog
),
1221 /* Initialize a CLooG input file. */
1224 init_cloog_input_file (int scop_number
)
1226 FILE *graphite_out_file
;
1227 int len
= strlen (dump_base_name
);
1228 char *dumpname
= XNEWVEC (char, len
+ 25);
1229 char *s_scop_number
= XNEWVEC (char, 15);
1231 memcpy (dumpname
, dump_base_name
, len
+ 1);
1232 strip_off_ending (dumpname
, len
);
1233 sprintf (s_scop_number
, ".%d", scop_number
);
1234 strcat (dumpname
, s_scop_number
);
1235 strcat (dumpname
, ".cloog");
1236 graphite_out_file
= fopen (dumpname
, "w+b");
1238 if (graphite_out_file
== 0)
1239 fatal_error ("can%'t open %s for writing: %m", dumpname
);
1243 return graphite_out_file
;
1246 /* Build cloog program for SCoP. */
1249 build_cloog_prog (scop_p scop
, CloogProgram
*prog
,
1250 CloogOptions
*options
, CloogState
*state ATTRIBUTE_UNUSED
)
1253 int max_nb_loops
= scop_max_loop_depth (scop
);
1255 CloogLoop
*loop_list
= NULL
;
1256 CloogBlockList
*block_list
= NULL
;
1257 CloogScatteringList
*scattering
= NULL
;
1258 int nbs
= 2 * max_nb_loops
+ 1;
1261 cloog_program_set_context
1262 (prog
, new_Cloog_Domain_from_ppl_Pointset_Powerset (SCOP_CONTEXT (scop
),
1263 scop_nb_params (scop
), state
));
1264 nbs
= unify_scattering_dimensions (scop
);
1265 scaldims
= (int *) xmalloc (nbs
* (sizeof (int)));
1266 cloog_program_set_nb_scattdims (prog
, nbs
);
1267 initialize_cloog_names (scop
, prog
);
1269 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
1271 CloogStatement
*stmt
;
1275 /* Dead code elimination: when the domain of a PBB is empty,
1276 don't generate code for the PBB. */
1277 if (ppl_Pointset_Powerset_C_Polyhedron_is_empty (PBB_DOMAIN (pbb
)))
1280 /* Build the new statement and its block. */
1281 stmt
= cloog_statement_alloc (state
, pbb_index (pbb
));
1282 dom
= new_Cloog_Domain_from_ppl_Pointset_Powerset (PBB_DOMAIN (pbb
),
1283 scop_nb_params (scop
),
1285 block
= cloog_block_alloc (stmt
, 0, NULL
, pbb_dim_iter_domain (pbb
));
1286 cloog_statement_set_usr (stmt
, pbb
);
1288 /* Build loop list. */
1290 CloogLoop
*new_loop_list
= cloog_loop_malloc (state
);
1291 cloog_loop_set_next (new_loop_list
, loop_list
);
1292 cloog_loop_set_domain (new_loop_list
, dom
);
1293 cloog_loop_set_block (new_loop_list
, block
);
1294 loop_list
= new_loop_list
;
1297 /* Build block list. */
1299 CloogBlockList
*new_block_list
= cloog_block_list_malloc ();
1301 cloog_block_list_set_next (new_block_list
, block_list
);
1302 cloog_block_list_set_block (new_block_list
, block
);
1303 block_list
= new_block_list
;
1306 /* Build scattering list. */
1308 /* XXX: Replace with cloog_domain_list_alloc(), when available. */
1309 CloogScatteringList
*new_scattering
1310 = (CloogScatteringList
*) xmalloc (sizeof (CloogScatteringList
));
1311 ppl_Polyhedron_t scat
;
1312 CloogScattering
*dom
;
1314 scat
= PBB_TRANSFORMED_SCATTERING (pbb
);
1315 dom
= new_Cloog_Scattering_from_ppl_Polyhedron
1316 (scat
, scop_nb_params (scop
), pbb_nb_scattering_transform (pbb
),
1319 cloog_set_next_scattering (new_scattering
, scattering
);
1320 cloog_set_scattering (new_scattering
, dom
);
1321 scattering
= new_scattering
;
1325 cloog_program_set_loop (prog
, loop_list
);
1326 cloog_program_set_blocklist (prog
, block_list
);
1328 for (i
= 0; i
< nbs
; i
++)
1331 cloog_program_set_scaldims (prog
, scaldims
);
1333 /* Extract scalar dimensions to simplify the code generation problem. */
1334 cloog_program_extract_scalars (prog
, scattering
, options
);
1336 /* Dump a .cloog input file, if requested. This feature is only
1337 enabled in the Graphite branch. */
1340 static size_t file_scop_number
= 0;
1341 FILE *cloog_file
= init_cloog_input_file (file_scop_number
);
1343 cloog_program_dump_cloog (cloog_file
, prog
, scattering
);
1347 /* Apply scattering. */
1348 cloog_program_scatter (prog
, scattering
, options
);
1349 free_scattering (scattering
);
1351 /* Iterators corresponding to scalar dimensions have to be extracted. */
1352 cloog_names_scalarize (cloog_program_names (prog
), nbs
,
1353 cloog_program_scaldims (prog
));
1355 /* Free blocklist. */
1357 CloogBlockList
*next
= cloog_program_blocklist (prog
);
1361 CloogBlockList
*toDelete
= next
;
1362 next
= cloog_block_list_next (next
);
1363 cloog_block_list_set_next (toDelete
, NULL
);
1364 cloog_block_list_set_block (toDelete
, NULL
);
1365 cloog_block_list_free (toDelete
);
1367 cloog_program_set_blocklist (prog
, NULL
);
1371 /* Return the options that will be used in GLOOG. */
1373 static CloogOptions
*
1374 set_cloog_options (CloogState
*state ATTRIBUTE_UNUSED
)
1376 CloogOptions
*options
= cloog_options_malloc (state
);
1378 /* Change cloog output language to C. If we do use FORTRAN instead, cloog
1379 will stop e.g. with "ERROR: unbounded loops not allowed in FORTRAN.", if
1380 we pass an incomplete program to cloog. */
1381 options
->language
= LANGUAGE_C
;
1383 /* Enable complex equality spreading: removes dummy statements
1384 (assignments) in the generated code which repeats the
1385 substitution equations for statements. This is useless for
1390 /* Silence CLooG to avoid failing tests due to debug output to stderr. */
1393 /* Enable C pretty-printing mode: normalizes the substitution
1394 equations for statements. */
1398 /* Allow cloog to build strides with a stride width different to one.
1399 This example has stride = 4:
1401 for (i = 0; i < 20; i += 4)
1403 options
->strides
= 1;
1405 /* Disable optimizations and make cloog generate source code closer to the
1406 input. This is useful for debugging, but later we want the optimized
1409 XXX: We can not disable optimizations, as loop blocking is not working
1414 options
->l
= INT_MAX
;
1420 /* Prints STMT to STDERR. */
1423 print_clast_stmt (FILE *file
, struct clast_stmt
*stmt
)
1425 CloogState
*state
= cloog_state_malloc ();
1426 CloogOptions
*options
= set_cloog_options (state
);
1428 clast_pprint (file
, stmt
, 0, options
);
1429 cloog_options_free (options
);
1430 cloog_state_free (state
);
1433 /* Prints STMT to STDERR. */
1436 debug_clast_stmt (struct clast_stmt
*stmt
)
1438 print_clast_stmt (stderr
, stmt
);
1441 /* Translate SCOP to a CLooG program and clast. These two
1442 representations should be freed together: a clast cannot be used
1443 without a program. */
1446 scop_to_clast (scop_p scop
, CloogState
*state
)
1448 CloogOptions
*options
= set_cloog_options (state
);
1449 cloog_prog_clast pc
;
1451 /* Connect new cloog prog generation to graphite. */
1452 pc
.prog
= cloog_program_malloc ();
1453 build_cloog_prog (scop
, pc
.prog
, options
, state
);
1454 pc
.prog
= cloog_program_generate (pc
.prog
, options
);
1455 pc
.stmt
= cloog_clast_create (pc
.prog
, options
);
1457 cloog_options_free (options
);
1461 /* Prints to FILE the code generated by CLooG for SCOP. */
1464 print_generated_program (FILE *file
, scop_p scop
)
1466 CloogState
*state
= cloog_state_malloc ();
1467 CloogOptions
*options
= set_cloog_options (state
);
1469 cloog_prog_clast pc
= scop_to_clast (scop
, state
);
1471 fprintf (file
, " (prog: \n");
1472 cloog_program_print (file
, pc
.prog
);
1473 fprintf (file
, " )\n");
1475 fprintf (file
, " (clast: \n");
1476 clast_pprint (file
, pc
.stmt
, 0, options
);
1477 fprintf (file
, " )\n");
1479 cloog_options_free (options
);
1480 cloog_clast_free (pc
.stmt
);
1481 cloog_program_free (pc
.prog
);
1484 /* Prints to STDERR the code generated by CLooG for SCOP. */
1487 debug_generated_program (scop_p scop
)
1489 print_generated_program (stderr
, scop
);
1492 /* Add CLooG names to parameter index. The index is used to translate
1493 back from CLooG names to GCC trees. */
1496 create_params_index (htab_t index_table
, CloogProgram
*prog
) {
1497 CloogNames
* names
= cloog_program_names (prog
);
1498 int nb_parameters
= cloog_names_nb_parameters (names
);
1499 char **parameters
= cloog_names_parameters (names
);
1502 for (i
= 0; i
< nb_parameters
; i
++)
1503 save_clast_name_index (index_table
, parameters
[i
], i
);
1506 /* GIMPLE Loop Generator: generates loops from STMT in GIMPLE form for
1507 the given SCOP. Return true if code generation succeeded.
1508 BB_PBB_MAPPING is a basic_block and it's related poly_bb_p mapping.
1512 gloog (scop_p scop
, htab_t bb_pbb_mapping
)
1514 VEC (tree
, heap
) *newivs
= VEC_alloc (tree
, heap
, 10);
1515 loop_p context_loop
;
1516 sese region
= SCOP_REGION (scop
);
1517 ifsese if_region
= NULL
;
1518 htab_t newivs_index
, params_index
;
1519 cloog_prog_clast pc
;
1522 state
= cloog_state_malloc ();
1523 timevar_push (TV_GRAPHITE_CODE_GEN
);
1524 gloog_error
= false;
1526 pc
= scop_to_clast (scop
, state
);
1528 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1530 fprintf (dump_file
, "\nCLAST generated by CLooG: \n");
1531 print_clast_stmt (dump_file
, pc
.stmt
);
1532 fprintf (dump_file
, "\n");
1535 recompute_all_dominators ();
1538 if_region
= move_sese_in_condition (region
);
1539 sese_insert_phis_for_liveouts (region
,
1540 if_region
->region
->exit
->src
,
1541 if_region
->false_region
->exit
,
1542 if_region
->true_region
->exit
);
1543 recompute_all_dominators ();
1546 context_loop
= SESE_ENTRY (region
)->src
->loop_father
;
1547 newivs_index
= htab_create (10, clast_name_index_elt_info
,
1548 eq_clast_name_indexes
, free
);
1549 params_index
= htab_create (10, clast_name_index_elt_info
,
1550 eq_clast_name_indexes
, free
);
1552 create_params_index (params_index
, pc
.prog
);
1554 translate_clast (region
, context_loop
, pc
.stmt
,
1555 if_region
->true_region
->entry
,
1556 &newivs
, newivs_index
,
1557 bb_pbb_mapping
, 1, params_index
);
1560 recompute_all_dominators ();
1564 set_ifsese_condition (if_region
, integer_zero_node
);
1566 free (if_region
->true_region
);
1567 free (if_region
->region
);
1570 htab_delete (newivs_index
);
1571 htab_delete (params_index
);
1572 VEC_free (tree
, heap
, newivs
);
1573 cloog_clast_free (pc
.stmt
);
1574 cloog_program_free (pc
.prog
);
1575 timevar_pop (TV_GRAPHITE_CODE_GEN
);
1577 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1581 int num_no_dependency
= 0;
1583 FOR_EACH_LOOP (li
, loop
, 0)
1584 if (loop
->can_be_parallel
)
1585 num_no_dependency
++;
1587 fprintf (dump_file
, "\n%d loops carried no dependency.\n",
1591 cloog_state_free (state
);
1593 return !gloog_error
;