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
);
708 /* Compute the type for the induction variable at LEVEL for the
709 statement PBB, based on the transformed schedule of PBB. */
712 compute_type_for_level (poly_bb_p pbb
, int level
)
720 compute_bounds_for_level (pbb
, level
, low
, up
);
721 type
= gcc_type_for_interval (low
, up
);
728 /* Walks a CLAST and returns the first statement in the body of a
731 static struct clast_user_stmt
*
732 clast_get_body_of_loop (struct clast_stmt
*stmt
)
735 || CLAST_STMT_IS_A (stmt
, stmt_user
))
736 return (struct clast_user_stmt
*) stmt
;
738 if (CLAST_STMT_IS_A (stmt
, stmt_for
))
739 return clast_get_body_of_loop (((struct clast_for
*) stmt
)->body
);
741 if (CLAST_STMT_IS_A (stmt
, stmt_guard
))
742 return clast_get_body_of_loop (((struct clast_guard
*) stmt
)->then
);
744 if (CLAST_STMT_IS_A (stmt
, stmt_block
))
745 return clast_get_body_of_loop (((struct clast_block
*) stmt
)->body
);
750 /* Returns the type for the induction variable for the loop translated
754 gcc_type_for_iv_of_clast_loop (struct clast_for
*stmt_for
, int level
,
755 tree lb_type
, tree ub_type
)
757 struct clast_stmt
*stmt
= (struct clast_stmt
*) stmt_for
;
758 struct clast_user_stmt
*body
= clast_get_body_of_loop (stmt
);
759 CloogStatement
*cs
= body
->statement
;
760 poly_bb_p pbb
= (poly_bb_p
) cloog_statement_usr (cs
);
762 return max_signed_precision_type (lb_type
, max_precision_type
763 (ub_type
, compute_type_for_level
767 /* Creates a new LOOP corresponding to Cloog's STMT. Inserts an
768 induction variable for the new LOOP. New LOOP is attached to CFG
769 starting at ENTRY_EDGE. LOOP is inserted into the loop tree and
770 becomes the child loop of the OUTER_LOOP. NEWIVS_INDEX binds
771 CLooG's scattering name to the induction variable created for the
772 loop of STMT. The new induction variable is inserted in the NEWIVS
776 graphite_create_new_loop (sese region
, edge entry_edge
,
777 struct clast_for
*stmt
,
778 loop_p outer
, VEC (tree
, heap
) **newivs
,
779 htab_t newivs_index
, htab_t params_index
, int level
)
781 tree lb_type
= gcc_type_for_clast_expr (stmt
->LB
, region
, *newivs
,
782 newivs_index
, params_index
);
783 tree ub_type
= gcc_type_for_clast_expr (stmt
->UB
, region
, *newivs
,
784 newivs_index
, params_index
);
785 tree type
= gcc_type_for_iv_of_clast_loop (stmt
, level
, lb_type
, ub_type
);
786 tree lb
= clast_to_gcc_expression (type
, stmt
->LB
, region
, *newivs
,
787 newivs_index
, params_index
);
788 tree ub
= clast_to_gcc_expression (type
, stmt
->UB
, region
, *newivs
,
789 newivs_index
, params_index
);
790 tree stride
= gmp_cst_to_tree (type
, stmt
->stride
);
791 tree ivvar
= create_tmp_var (type
, "graphite_IV");
792 tree iv
, iv_after_increment
;
793 loop_p loop
= create_empty_loop_on_edge
794 (entry_edge
, lb
, stride
, ub
, ivvar
, &iv
, &iv_after_increment
,
795 outer
? outer
: entry_edge
->src
->loop_father
);
797 add_referenced_var (ivvar
);
799 save_clast_name_index (newivs_index
, stmt
->iterator
,
800 VEC_length (tree
, *newivs
));
801 VEC_safe_push (tree
, heap
, *newivs
, iv
);
805 /* Inserts in iv_map a tuple (OLD_LOOP->num, NEW_NAME) for the
806 induction variables of the loops around GBB in SESE. */
809 build_iv_mapping (VEC (tree
, heap
) *iv_map
, sese region
,
810 VEC (tree
, heap
) *newivs
, htab_t newivs_index
,
811 struct clast_user_stmt
*user_stmt
,
814 struct clast_stmt
*t
;
816 CloogStatement
*cs
= user_stmt
->statement
;
817 poly_bb_p pbb
= (poly_bb_p
) cloog_statement_usr (cs
);
818 gimple_bb_p gbb
= PBB_BLACK_BOX (pbb
);
820 for (t
= user_stmt
->substitutions
; t
; t
= t
->next
, depth
++)
822 struct clast_expr
*expr
= (struct clast_expr
*)
823 ((struct clast_assignment
*)t
)->RHS
;
824 tree type
= gcc_type_for_clast_expr (expr
, region
, newivs
,
825 newivs_index
, params_index
);
826 tree new_name
= clast_to_gcc_expression (type
, expr
, region
, newivs
,
827 newivs_index
, params_index
);
828 loop_p old_loop
= gbb_loop_at_index (gbb
, region
, depth
);
830 VEC_replace (tree
, iv_map
, old_loop
->num
, new_name
);
834 /* Construct bb_pbb_def with BB and PBB. */
837 new_bb_pbb_def (basic_block bb
, poly_bb_p pbb
)
839 bb_pbb_def
*bb_pbb_p
;
841 bb_pbb_p
= XNEW (bb_pbb_def
);
848 /* Mark BB with it's relevant PBB via hashing table BB_PBB_MAPPING. */
851 mark_bb_with_pbb (poly_bb_p pbb
, basic_block bb
, htab_t bb_pbb_mapping
)
857 x
= htab_find_slot (bb_pbb_mapping
, &tmp
, INSERT
);
860 *x
= new_bb_pbb_def (bb
, pbb
);
863 /* Find BB's related poly_bb_p in hash table BB_PBB_MAPPING. */
866 find_pbb_via_hash (htab_t bb_pbb_mapping
, basic_block bb
)
872 slot
= htab_find_slot (bb_pbb_mapping
, &tmp
, NO_INSERT
);
875 return ((bb_pbb_def
*) *slot
)->pbb
;
880 /* Check data dependency in LOOP at scattering level LEVEL.
881 BB_PBB_MAPPING is a basic_block and it's related poly_bb_p
885 dependency_in_loop_p (loop_p loop
, htab_t bb_pbb_mapping
, int level
)
888 basic_block
*bbs
= get_loop_body_in_dom_order (loop
);
890 for (i
= 0; i
< loop
->num_nodes
; i
++)
892 poly_bb_p pbb1
= find_pbb_via_hash (bb_pbb_mapping
, bbs
[i
]);
897 for (j
= 0; j
< loop
->num_nodes
; j
++)
899 poly_bb_p pbb2
= find_pbb_via_hash (bb_pbb_mapping
, bbs
[j
]);
904 if (dependency_between_pbbs_p (pbb1
, pbb2
, level
))
917 /* Translates a clast user statement STMT to gimple.
919 - REGION is the sese region we used to generate the scop.
920 - NEXT_E is the edge where new generated code should be attached.
921 - CONTEXT_LOOP is the loop in which the generated code will be placed
922 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
923 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
926 translate_clast_user (sese region
, struct clast_user_stmt
*stmt
, edge next_e
,
927 VEC (tree
, heap
) **newivs
,
928 htab_t newivs_index
, htab_t bb_pbb_mapping
,
933 poly_bb_p pbb
= (poly_bb_p
) cloog_statement_usr (stmt
->statement
);
934 gimple_bb_p gbb
= PBB_BLACK_BOX (pbb
);
935 VEC (tree
, heap
) *iv_map
;
937 if (GBB_BB (gbb
) == ENTRY_BLOCK_PTR
)
940 nb_loops
= number_of_loops ();
941 iv_map
= VEC_alloc (tree
, heap
, nb_loops
);
942 for (i
= 0; i
< nb_loops
; i
++)
943 VEC_quick_push (tree
, iv_map
, NULL_TREE
);
945 build_iv_mapping (iv_map
, region
, *newivs
, newivs_index
, stmt
, params_index
);
946 next_e
= copy_bb_and_scalar_dependences (GBB_BB (gbb
), region
,
948 VEC_free (tree
, heap
, iv_map
);
950 new_bb
= next_e
->src
;
951 mark_bb_with_pbb (pbb
, new_bb
, bb_pbb_mapping
);
952 update_ssa (TODO_update_ssa
);
957 /* Creates a new if region protecting the loop to be executed, if the execution
958 count is zero (lb > ub). */
961 graphite_create_new_loop_guard (sese region
, edge entry_edge
,
962 struct clast_for
*stmt
,
963 VEC (tree
, heap
) *newivs
,
964 htab_t newivs_index
, htab_t params_index
)
968 tree lb_type
= gcc_type_for_clast_expr (stmt
->LB
, region
, newivs
,
969 newivs_index
, params_index
);
970 tree ub_type
= gcc_type_for_clast_expr (stmt
->UB
, region
, newivs
,
971 newivs_index
, params_index
);
972 tree type
= max_precision_type (lb_type
, ub_type
);
973 tree lb
= clast_to_gcc_expression (type
, stmt
->LB
, region
, newivs
,
974 newivs_index
, params_index
);
975 tree ub
= clast_to_gcc_expression (type
, stmt
->UB
, region
, newivs
,
976 newivs_index
, params_index
);
977 tree one
= POINTER_TYPE_P (type
) ? size_one_node
978 : fold_convert (type
, integer_one_node
);
979 /* Adding +1 and using LT_EXPR helps with loop latches that have a
980 loop iteration count of "PARAMETER - 1". For PARAMETER == 0 this becomes
981 2^{32|64}, and the condition lb <= ub is true, even if we do not want this.
982 However lb < ub + 1 is false, as expected. */
983 tree ub_one
= fold_build2 (POINTER_TYPE_P (type
) ? POINTER_PLUS_EXPR
984 : PLUS_EXPR
, type
, ub
, one
);
986 /* When ub + 1 wraps around, use lb <= ub. */
987 if (integer_zerop (ub_one
))
988 cond_expr
= fold_build2 (LE_EXPR
, boolean_type_node
, lb
, ub
);
990 cond_expr
= fold_build2 (LT_EXPR
, boolean_type_node
, lb
, ub_one
);
992 exit_edge
= create_empty_if_region_on_edge (entry_edge
, cond_expr
);
998 translate_clast (sese
, loop_p
, struct clast_stmt
*, edge
,
999 VEC (tree
, heap
) **, htab_t
, htab_t
, int, htab_t
);
1001 /* Create the loop for a clast for statement.
1003 - REGION is the sese region we used to generate the scop.
1004 - NEXT_E is the edge where new generated code should be attached.
1005 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
1006 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
1009 translate_clast_for_loop (sese region
, loop_p context_loop
,
1010 struct clast_for
*stmt
, edge next_e
,
1011 VEC (tree
, heap
) **newivs
,
1012 htab_t newivs_index
, htab_t bb_pbb_mapping
,
1013 int level
, htab_t params_index
)
1015 struct loop
*loop
= graphite_create_new_loop (region
, next_e
, stmt
,
1016 context_loop
, newivs
,
1017 newivs_index
, params_index
,
1019 edge last_e
= single_exit (loop
);
1020 edge to_body
= single_succ_edge (loop
->header
);
1021 basic_block after
= to_body
->dest
;
1023 /* Create a basic block for loop close phi nodes. */
1024 last_e
= single_succ_edge (split_edge (last_e
));
1026 /* Translate the body of the loop. */
1027 next_e
= translate_clast (region
, loop
, stmt
->body
, to_body
,
1028 newivs
, newivs_index
, bb_pbb_mapping
, level
+ 1,
1030 redirect_edge_succ_nodup (next_e
, after
);
1031 set_immediate_dominator (CDI_DOMINATORS
, next_e
->dest
, next_e
->src
);
1033 if (flag_loop_parallelize_all
1034 && !dependency_in_loop_p (loop
, bb_pbb_mapping
,
1035 get_scattering_level (level
)))
1036 loop
->can_be_parallel
= true;
1041 /* Translates a clast for statement STMT to gimple. First a guard is created
1042 protecting the loop, if it is executed zero times. In this guard we create
1043 the real loop structure.
1045 - REGION is the sese region we used to generate the scop.
1046 - NEXT_E is the edge where new generated code should be attached.
1047 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
1048 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
1051 translate_clast_for (sese region
, loop_p context_loop
, struct clast_for
*stmt
,
1052 edge next_e
, VEC (tree
, heap
) **newivs
,
1053 htab_t newivs_index
, htab_t bb_pbb_mapping
, int level
,
1054 htab_t params_index
)
1056 edge last_e
= graphite_create_new_loop_guard (region
, next_e
, stmt
, *newivs
,
1057 newivs_index
, params_index
);
1058 edge true_e
= get_true_edge_from_guard_bb (next_e
->dest
);
1060 translate_clast_for_loop (region
, context_loop
, stmt
, true_e
, newivs
,
1061 newivs_index
, bb_pbb_mapping
, level
,
1066 /* Translates a clast guard statement STMT to gimple.
1068 - REGION is the sese region we used to generate the scop.
1069 - NEXT_E is the edge where new generated code should be attached.
1070 - CONTEXT_LOOP is the loop in which the generated code will be placed
1071 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
1072 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
1075 translate_clast_guard (sese region
, loop_p context_loop
,
1076 struct clast_guard
*stmt
, edge next_e
,
1077 VEC (tree
, heap
) **newivs
,
1078 htab_t newivs_index
, htab_t bb_pbb_mapping
, int level
,
1079 htab_t params_index
)
1081 edge last_e
= graphite_create_new_guard (region
, next_e
, stmt
, *newivs
,
1082 newivs_index
, params_index
);
1083 edge true_e
= get_true_edge_from_guard_bb (next_e
->dest
);
1085 translate_clast (region
, context_loop
, stmt
->then
, true_e
,
1086 newivs
, newivs_index
, bb_pbb_mapping
,
1087 level
, params_index
);
1091 /* Translates a CLAST statement STMT to GCC representation in the
1094 - NEXT_E is the edge where new generated code should be attached.
1095 - CONTEXT_LOOP is the loop in which the generated code will be placed
1096 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping. */
1098 translate_clast (sese region
, loop_p context_loop
, struct clast_stmt
*stmt
,
1099 edge next_e
, VEC (tree
, heap
) **newivs
,
1100 htab_t newivs_index
, htab_t bb_pbb_mapping
, int level
,
1101 htab_t params_index
)
1106 if (CLAST_STMT_IS_A (stmt
, stmt_root
))
1109 else if (CLAST_STMT_IS_A (stmt
, stmt_user
))
1110 next_e
= translate_clast_user (region
, (struct clast_user_stmt
*) stmt
,
1111 next_e
, newivs
, newivs_index
,
1112 bb_pbb_mapping
, params_index
);
1114 else if (CLAST_STMT_IS_A (stmt
, stmt_for
))
1115 next_e
= translate_clast_for (region
, context_loop
,
1116 (struct clast_for
*) stmt
, next_e
,
1117 newivs
, newivs_index
,
1118 bb_pbb_mapping
, level
, params_index
);
1120 else if (CLAST_STMT_IS_A (stmt
, stmt_guard
))
1121 next_e
= translate_clast_guard (region
, context_loop
,
1122 (struct clast_guard
*) stmt
, next_e
,
1123 newivs
, newivs_index
,
1124 bb_pbb_mapping
, level
, params_index
);
1126 else if (CLAST_STMT_IS_A (stmt
, stmt_block
))
1127 next_e
= translate_clast (region
, context_loop
,
1128 ((struct clast_block
*) stmt
)->body
,
1129 next_e
, newivs
, newivs_index
,
1130 bb_pbb_mapping
, level
, params_index
);
1134 recompute_all_dominators ();
1137 return translate_clast (region
, context_loop
, stmt
->next
, next_e
,
1138 newivs
, newivs_index
,
1139 bb_pbb_mapping
, level
, params_index
);
1142 /* Free the SCATTERING domain list. */
1145 free_scattering (CloogScatteringList
*scattering
)
1149 CloogScattering
*dom
= cloog_scattering (scattering
);
1150 CloogScatteringList
*next
= cloog_next_scattering (scattering
);
1152 cloog_scattering_free (dom
);
1158 /* Initialize Cloog's parameter names from the names used in GIMPLE.
1159 Initialize Cloog's iterator names, using 'graphite_iterator_%d'
1160 from 0 to scop_nb_loops (scop). */
1163 initialize_cloog_names (scop_p scop
, CloogProgram
*prog
)
1165 sese region
= SCOP_REGION (scop
);
1167 int nb_iterators
= scop_max_loop_depth (scop
);
1168 int nb_scattering
= cloog_program_nb_scattdims (prog
);
1169 int nb_parameters
= VEC_length (tree
, SESE_PARAMS (region
));
1170 char **iterators
= XNEWVEC (char *, nb_iterators
* 2);
1171 char **scattering
= XNEWVEC (char *, nb_scattering
);
1172 char **parameters
= XNEWVEC (char *, nb_parameters
);
1174 cloog_program_set_names (prog
, cloog_names_malloc ());
1176 for (i
= 0; i
< nb_parameters
; i
++)
1178 tree param
= VEC_index (tree
, SESE_PARAMS(region
), i
);
1179 const char *name
= get_name (param
);
1185 len
= strlen (name
);
1187 parameters
[i
] = XNEWVEC (char, len
+ 1);
1188 snprintf (parameters
[i
], len
, "%s_%d", name
, SSA_NAME_VERSION (param
));
1191 cloog_names_set_nb_parameters (cloog_program_names (prog
), nb_parameters
);
1192 cloog_names_set_parameters (cloog_program_names (prog
), parameters
);
1194 for (i
= 0; i
< nb_iterators
; i
++)
1197 iterators
[i
] = XNEWVEC (char, len
);
1198 snprintf (iterators
[i
], len
, "git_%d", i
);
1201 cloog_names_set_nb_iterators (cloog_program_names (prog
),
1203 cloog_names_set_iterators (cloog_program_names (prog
),
1206 for (i
= 0; i
< nb_scattering
; i
++)
1209 scattering
[i
] = XNEWVEC (char, len
);
1210 snprintf (scattering
[i
], len
, "scat_%d", i
);
1213 cloog_names_set_nb_scattering (cloog_program_names (prog
),
1215 cloog_names_set_scattering (cloog_program_names (prog
),
1219 /* Build cloog program for SCoP. */
1222 build_cloog_prog (scop_p scop
, CloogProgram
*prog
,
1223 CloogOptions
*options
, CloogState
*state ATTRIBUTE_UNUSED
)
1226 int max_nb_loops
= scop_max_loop_depth (scop
);
1228 CloogLoop
*loop_list
= NULL
;
1229 CloogBlockList
*block_list
= NULL
;
1230 CloogScatteringList
*scattering
= NULL
;
1231 int nbs
= 2 * max_nb_loops
+ 1;
1234 cloog_program_set_context
1235 (prog
, new_Cloog_Domain_from_ppl_Pointset_Powerset (SCOP_CONTEXT (scop
),
1236 scop_nb_params (scop
), state
));
1237 nbs
= unify_scattering_dimensions (scop
);
1238 scaldims
= (int *) xmalloc (nbs
* (sizeof (int)));
1239 cloog_program_set_nb_scattdims (prog
, nbs
);
1240 initialize_cloog_names (scop
, prog
);
1242 FOR_EACH_VEC_ELT (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
)
1244 CloogStatement
*stmt
;
1248 /* Dead code elimination: when the domain of a PBB is empty,
1249 don't generate code for the PBB. */
1250 if (ppl_Pointset_Powerset_C_Polyhedron_is_empty (PBB_DOMAIN (pbb
)))
1253 /* Build the new statement and its block. */
1254 stmt
= cloog_statement_alloc (state
, pbb_index (pbb
));
1255 dom
= new_Cloog_Domain_from_ppl_Pointset_Powerset (PBB_DOMAIN (pbb
),
1256 scop_nb_params (scop
),
1258 block
= cloog_block_alloc (stmt
, 0, NULL
, pbb_dim_iter_domain (pbb
));
1259 cloog_statement_set_usr (stmt
, pbb
);
1261 /* Build loop list. */
1263 CloogLoop
*new_loop_list
= cloog_loop_malloc (state
);
1264 cloog_loop_set_next (new_loop_list
, loop_list
);
1265 cloog_loop_set_domain (new_loop_list
, dom
);
1266 cloog_loop_set_block (new_loop_list
, block
);
1267 loop_list
= new_loop_list
;
1270 /* Build block list. */
1272 CloogBlockList
*new_block_list
= cloog_block_list_malloc ();
1274 cloog_block_list_set_next (new_block_list
, block_list
);
1275 cloog_block_list_set_block (new_block_list
, block
);
1276 block_list
= new_block_list
;
1279 /* Build scattering list. */
1281 /* XXX: Replace with cloog_domain_list_alloc(), when available. */
1282 CloogScatteringList
*new_scattering
1283 = (CloogScatteringList
*) xmalloc (sizeof (CloogScatteringList
));
1284 ppl_Polyhedron_t scat
;
1285 CloogScattering
*dom
;
1287 scat
= PBB_TRANSFORMED_SCATTERING (pbb
);
1288 dom
= new_Cloog_Scattering_from_ppl_Polyhedron
1289 (scat
, scop_nb_params (scop
), pbb_nb_scattering_transform (pbb
),
1292 cloog_set_next_scattering (new_scattering
, scattering
);
1293 cloog_set_scattering (new_scattering
, dom
);
1294 scattering
= new_scattering
;
1298 cloog_program_set_loop (prog
, loop_list
);
1299 cloog_program_set_blocklist (prog
, block_list
);
1301 for (i
= 0; i
< nbs
; i
++)
1304 cloog_program_set_scaldims (prog
, scaldims
);
1306 /* Extract scalar dimensions to simplify the code generation problem. */
1307 cloog_program_extract_scalars (prog
, scattering
, options
);
1309 /* Apply scattering. */
1310 cloog_program_scatter (prog
, scattering
, options
);
1311 free_scattering (scattering
);
1313 /* Iterators corresponding to scalar dimensions have to be extracted. */
1314 cloog_names_scalarize (cloog_program_names (prog
), nbs
,
1315 cloog_program_scaldims (prog
));
1317 /* Free blocklist. */
1319 CloogBlockList
*next
= cloog_program_blocklist (prog
);
1323 CloogBlockList
*toDelete
= next
;
1324 next
= cloog_block_list_next (next
);
1325 cloog_block_list_set_next (toDelete
, NULL
);
1326 cloog_block_list_set_block (toDelete
, NULL
);
1327 cloog_block_list_free (toDelete
);
1329 cloog_program_set_blocklist (prog
, NULL
);
1333 /* Return the options that will be used in GLOOG. */
1335 static CloogOptions
*
1336 set_cloog_options (CloogState
*state ATTRIBUTE_UNUSED
)
1338 CloogOptions
*options
= cloog_options_malloc (state
);
1340 /* Change cloog output language to C. If we do use FORTRAN instead, cloog
1341 will stop e.g. with "ERROR: unbounded loops not allowed in FORTRAN.", if
1342 we pass an incomplete program to cloog. */
1343 options
->language
= LANGUAGE_C
;
1345 /* Enable complex equality spreading: removes dummy statements
1346 (assignments) in the generated code which repeats the
1347 substitution equations for statements. This is useless for
1352 /* Silence CLooG to avoid failing tests due to debug output to stderr. */
1355 /* Enable C pretty-printing mode: normalizes the substitution
1356 equations for statements. */
1360 /* Allow cloog to build strides with a stride width different to one.
1361 This example has stride = 4:
1363 for (i = 0; i < 20; i += 4)
1365 options
->strides
= 1;
1367 /* Disable optimizations and make cloog generate source code closer to the
1368 input. This is useful for debugging, but later we want the optimized
1371 XXX: We can not disable optimizations, as loop blocking is not working
1376 options
->l
= INT_MAX
;
1382 /* Prints STMT to STDERR. */
1385 print_clast_stmt (FILE *file
, struct clast_stmt
*stmt
)
1387 CloogState
*state
= cloog_state_malloc ();
1388 CloogOptions
*options
= set_cloog_options (state
);
1390 clast_pprint (file
, stmt
, 0, options
);
1391 cloog_options_free (options
);
1392 cloog_state_free (state
);
1395 /* Prints STMT to STDERR. */
1398 debug_clast_stmt (struct clast_stmt
*stmt
)
1400 print_clast_stmt (stderr
, stmt
);
1403 /* Translate SCOP to a CLooG program and clast. These two
1404 representations should be freed together: a clast cannot be used
1405 without a program. */
1408 scop_to_clast (scop_p scop
, CloogState
*state
)
1410 CloogOptions
*options
= set_cloog_options (state
);
1411 cloog_prog_clast pc
;
1413 /* Connect new cloog prog generation to graphite. */
1414 pc
.prog
= cloog_program_malloc ();
1415 build_cloog_prog (scop
, pc
.prog
, options
, state
);
1416 pc
.prog
= cloog_program_generate (pc
.prog
, options
);
1417 pc
.stmt
= cloog_clast_create (pc
.prog
, options
);
1419 cloog_options_free (options
);
1423 /* Prints to FILE the code generated by CLooG for SCOP. */
1426 print_generated_program (FILE *file
, scop_p scop
)
1428 CloogState
*state
= cloog_state_malloc ();
1429 CloogOptions
*options
= set_cloog_options (state
);
1431 cloog_prog_clast pc
= scop_to_clast (scop
, state
);
1433 fprintf (file
, " (prog: \n");
1434 cloog_program_print (file
, pc
.prog
);
1435 fprintf (file
, " )\n");
1437 fprintf (file
, " (clast: \n");
1438 clast_pprint (file
, pc
.stmt
, 0, options
);
1439 fprintf (file
, " )\n");
1441 cloog_options_free (options
);
1442 cloog_clast_free (pc
.stmt
);
1443 cloog_program_free (pc
.prog
);
1446 /* Prints to STDERR the code generated by CLooG for SCOP. */
1449 debug_generated_program (scop_p scop
)
1451 print_generated_program (stderr
, scop
);
1454 /* Add CLooG names to parameter index. The index is used to translate
1455 back from CLooG names to GCC trees. */
1458 create_params_index (htab_t index_table
, CloogProgram
*prog
) {
1459 CloogNames
* names
= cloog_program_names (prog
);
1460 int nb_parameters
= cloog_names_nb_parameters (names
);
1461 char **parameters
= cloog_names_parameters (names
);
1464 for (i
= 0; i
< nb_parameters
; i
++)
1465 save_clast_name_index (index_table
, parameters
[i
], i
);
1468 /* GIMPLE Loop Generator: generates loops from STMT in GIMPLE form for
1469 the given SCOP. Return true if code generation succeeded.
1470 BB_PBB_MAPPING is a basic_block and it's related poly_bb_p mapping.
1474 gloog (scop_p scop
, htab_t bb_pbb_mapping
)
1476 VEC (tree
, heap
) *newivs
= VEC_alloc (tree
, heap
, 10);
1477 loop_p context_loop
;
1478 sese region
= SCOP_REGION (scop
);
1479 ifsese if_region
= NULL
;
1480 htab_t newivs_index
, params_index
;
1481 cloog_prog_clast pc
;
1484 state
= cloog_state_malloc ();
1485 timevar_push (TV_GRAPHITE_CODE_GEN
);
1486 gloog_error
= false;
1488 pc
= scop_to_clast (scop
, state
);
1490 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1492 fprintf (dump_file
, "\nCLAST generated by CLooG: \n");
1493 print_clast_stmt (dump_file
, pc
.stmt
);
1494 fprintf (dump_file
, "\n");
1497 recompute_all_dominators ();
1500 if_region
= move_sese_in_condition (region
);
1501 sese_insert_phis_for_liveouts (region
,
1502 if_region
->region
->exit
->src
,
1503 if_region
->false_region
->exit
,
1504 if_region
->true_region
->exit
);
1505 recompute_all_dominators ();
1508 context_loop
= SESE_ENTRY (region
)->src
->loop_father
;
1509 newivs_index
= htab_create (10, clast_name_index_elt_info
,
1510 eq_clast_name_indexes
, free
);
1511 params_index
= htab_create (10, clast_name_index_elt_info
,
1512 eq_clast_name_indexes
, free
);
1514 create_params_index (params_index
, pc
.prog
);
1516 translate_clast (region
, context_loop
, pc
.stmt
,
1517 if_region
->true_region
->entry
,
1518 &newivs
, newivs_index
,
1519 bb_pbb_mapping
, 1, params_index
);
1522 recompute_all_dominators ();
1526 set_ifsese_condition (if_region
, integer_zero_node
);
1528 free (if_region
->true_region
);
1529 free (if_region
->region
);
1532 htab_delete (newivs_index
);
1533 htab_delete (params_index
);
1534 VEC_free (tree
, heap
, newivs
);
1535 cloog_clast_free (pc
.stmt
);
1536 cloog_program_free (pc
.prog
);
1537 timevar_pop (TV_GRAPHITE_CODE_GEN
);
1539 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1543 int num_no_dependency
= 0;
1545 FOR_EACH_LOOP (li
, loop
, 0)
1546 if (loop
->can_be_parallel
)
1547 num_no_dependency
++;
1549 fprintf (dump_file
, "\n%d loops carried no dependency.\n",
1553 cloog_state_free (state
);
1555 return !gloog_error
;