1 /* Translation of CLAST (CLooG AST) to Gimple.
2 Copyright (C) 2009, 2010 Free Software Foundation, Inc.
3 Contributed by Sebastian Pop <sebastian.pop@amd.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
28 #include "basic-block.h"
29 #include "diagnostic.h"
30 #include "tree-flow.h"
32 #include "tree-dump.h"
35 #include "tree-chrec.h"
36 #include "tree-data-ref.h"
37 #include "tree-scalar-evolution.h"
38 #include "tree-pass.h"
40 #include "value-prof.h"
41 #include "pointer-set.h"
43 #include "langhooks.h"
47 #include "cloog/cloog.h"
49 #include "graphite-cloog-util.h"
50 #include "graphite-ppl.h"
52 #include "graphite-poly.h"
53 #include "graphite-scop-detection.h"
54 #include "graphite-clast-to-gimple.h"
55 #include "graphite-dependences.h"
57 /* This flag is set when an error occurred during the translation of
59 static bool gloog_error
;
61 /* Verifies properties that GRAPHITE should maintain during translation. */
64 graphite_verify (void)
66 #ifdef ENABLE_CHECKING
67 verify_loop_structure ();
68 verify_dominators (CDI_DOMINATORS
);
69 verify_dominators (CDI_POST_DOMINATORS
);
70 verify_loop_closed_ssa (true);
74 /* Stores the INDEX in a vector for a given clast NAME. */
76 typedef struct clast_name_index
{
79 } *clast_name_index_p
;
81 /* Returns a pointer to a new element of type clast_name_index_p built
82 from NAME and INDEX. */
84 static inline clast_name_index_p
85 new_clast_name_index (const char *name
, int index
)
87 clast_name_index_p res
= XNEW (struct clast_name_index
);
94 /* For a given clast NAME, returns -1 if it does not correspond to any
95 parameter, or otherwise, returns the index in the PARAMS or
96 SCATTERING_DIMENSIONS vector. */
99 clast_name_to_index (const char *name
, htab_t index_table
)
101 struct clast_name_index tmp
;
105 slot
= htab_find_slot (index_table
, &tmp
, NO_INSERT
);
108 return ((struct clast_name_index
*) *slot
)->index
;
113 /* Records in INDEX_TABLE the INDEX for NAME. */
116 save_clast_name_index (htab_t index_table
, const char *name
, int index
)
118 struct clast_name_index tmp
;
122 slot
= htab_find_slot (index_table
, &tmp
, INSERT
);
129 *slot
= new_clast_name_index (name
, index
);
133 /* Print to stderr the element ELT. */
136 debug_clast_name_index (clast_name_index_p elt
)
138 fprintf (stderr
, "(index = %d, name = %s)\n", elt
->index
, elt
->name
);
141 /* Helper function for debug_rename_map. */
144 debug_clast_name_indexes_1 (void **slot
, void *s ATTRIBUTE_UNUSED
)
146 struct clast_name_index
*entry
= (struct clast_name_index
*) *slot
;
147 debug_clast_name_index (entry
);
151 /* Print to stderr all the elements of MAP. */
154 debug_clast_name_indexes (htab_t map
)
156 htab_traverse (map
, debug_clast_name_indexes_1
, NULL
);
159 /* Computes a hash function for database element ELT. */
161 static inline hashval_t
162 clast_name_index_elt_info (const void *elt
)
164 return htab_hash_pointer (((const struct clast_name_index
*) elt
)->name
);
167 /* Compares database elements E1 and E2. */
170 eq_clast_name_indexes (const void *e1
, const void *e2
)
172 const struct clast_name_index
*elt1
= (const struct clast_name_index
*) e1
;
173 const struct clast_name_index
*elt2
= (const struct clast_name_index
*) e2
;
175 return (elt1
->name
== elt2
->name
);
179 /* For a given loop DEPTH in the loop nest of the original black box
180 PBB, return the old induction variable associated to that loop. */
183 pbb_to_depth_to_oldiv (poly_bb_p pbb
, int depth
)
185 gimple_bb_p gbb
= PBB_BLACK_BOX (pbb
);
186 sese region
= SCOP_REGION (PBB_SCOP (pbb
));
187 loop_p loop
= gbb_loop_at_index (gbb
, region
, depth
);
189 return loop
->single_iv
;
192 /* For a given scattering dimension, return the new induction variable
196 newivs_to_depth_to_newiv (VEC (tree
, heap
) *newivs
, int depth
)
198 return VEC_index (tree
, newivs
, depth
);
203 /* Returns the tree variable from the name NAME that was given in
204 Cloog representation. */
207 clast_name_to_gcc (const char *name
, sese region
, VEC (tree
, heap
) *newivs
,
208 htab_t newivs_index
, htab_t params_index
)
211 VEC (tree
, heap
) *params
= SESE_PARAMS (region
);
213 if (params
&& params_index
)
215 index
= clast_name_to_index (name
, params_index
);
218 return VEC_index (tree
, params
, index
);
221 gcc_assert (newivs
&& newivs_index
);
222 index
= clast_name_to_index (name
, newivs_index
);
223 gcc_assert (index
>= 0);
225 return newivs_to_depth_to_newiv (newivs
, index
);
228 /* Returns the signed maximal precision type for expressions TYPE1 and TYPE2. */
231 max_signed_precision_type (tree type1
, tree type2
)
233 int p1
= TYPE_PRECISION (type1
);
234 int p2
= TYPE_PRECISION (type2
);
239 precision
= TYPE_UNSIGNED (type1
) ? p1
* 2 : p1
;
241 precision
= TYPE_UNSIGNED (type2
) ? p2
* 2 : p2
;
243 type
= lang_hooks
.types
.type_for_size (precision
, false);
248 return integer_type_node
;
253 /* Returns the maximal precision type for expressions TYPE1 and TYPE2. */
256 max_precision_type (tree type1
, tree type2
)
258 if (POINTER_TYPE_P (type1
))
261 if (POINTER_TYPE_P (type2
))
264 if (!TYPE_UNSIGNED (type1
)
265 || !TYPE_UNSIGNED (type2
))
266 return max_signed_precision_type (type1
, type2
);
268 return TYPE_PRECISION (type1
) > TYPE_PRECISION (type2
) ? type1
: type2
;
272 clast_to_gcc_expression (tree
, struct clast_expr
*, sese
, VEC (tree
, heap
) *,
275 /* Converts a Cloog reduction expression R with reduction operation OP
276 to a GCC expression tree of type TYPE. */
279 clast_to_gcc_expression_red (tree type
, enum tree_code op
,
280 struct clast_reduction
*r
,
281 sese region
, VEC (tree
, heap
) *newivs
,
282 htab_t newivs_index
, htab_t params_index
)
285 tree res
= clast_to_gcc_expression (type
, r
->elts
[0], region
, newivs
,
286 newivs_index
, params_index
);
287 tree operand_type
= (op
== POINTER_PLUS_EXPR
) ? sizetype
: type
;
289 for (i
= 1; i
< r
->n
; i
++)
291 tree t
= clast_to_gcc_expression (operand_type
, r
->elts
[i
], region
,
292 newivs
, newivs_index
, params_index
);
293 res
= fold_build2 (op
, type
, res
, t
);
299 /* Converts a Cloog AST expression E back to a GCC expression tree of
303 clast_to_gcc_expression (tree type
, struct clast_expr
*e
,
304 sese region
, VEC (tree
, heap
) *newivs
,
305 htab_t newivs_index
, htab_t params_index
)
311 struct clast_term
*t
= (struct clast_term
*) e
;
315 if (mpz_cmp_si (t
->val
, 1) == 0)
317 tree name
= clast_name_to_gcc (t
->var
, region
, newivs
,
318 newivs_index
, params_index
);
320 if (POINTER_TYPE_P (TREE_TYPE (name
)) != POINTER_TYPE_P (type
))
321 name
= fold_convert (sizetype
, name
);
323 name
= fold_convert (type
, name
);
327 else if (mpz_cmp_si (t
->val
, -1) == 0)
329 tree name
= clast_name_to_gcc (t
->var
, region
, newivs
,
330 newivs_index
, params_index
);
332 if (POINTER_TYPE_P (TREE_TYPE (name
)) != POINTER_TYPE_P (type
))
333 name
= fold_convert (sizetype
, name
);
335 name
= fold_convert (type
, name
);
337 return fold_build1 (NEGATE_EXPR
, type
, name
);
341 tree name
= clast_name_to_gcc (t
->var
, region
, newivs
,
342 newivs_index
, params_index
);
343 tree cst
= gmp_cst_to_tree (type
, t
->val
);
345 if (POINTER_TYPE_P (TREE_TYPE (name
)) != POINTER_TYPE_P (type
))
346 name
= fold_convert (sizetype
, name
);
348 name
= fold_convert (type
, name
);
350 if (!POINTER_TYPE_P (type
))
351 return fold_build2 (MULT_EXPR
, type
, cst
, name
);
358 return gmp_cst_to_tree (type
, t
->val
);
363 struct clast_reduction
*r
= (struct clast_reduction
*) e
;
368 return clast_to_gcc_expression_red
369 (type
, POINTER_TYPE_P (type
) ? POINTER_PLUS_EXPR
: PLUS_EXPR
,
370 r
, region
, newivs
, newivs_index
, params_index
);
373 return clast_to_gcc_expression_red (type
, MIN_EXPR
, r
, region
,
374 newivs
, newivs_index
,
378 return clast_to_gcc_expression_red (type
, MAX_EXPR
, r
, region
,
379 newivs
, newivs_index
,
390 struct clast_binary
*b
= (struct clast_binary
*) e
;
391 struct clast_expr
*lhs
= (struct clast_expr
*) b
->LHS
;
392 tree tl
= clast_to_gcc_expression (type
, lhs
, region
, newivs
,
393 newivs_index
, params_index
);
394 tree tr
= gmp_cst_to_tree (type
, b
->RHS
);
399 return fold_build2 (FLOOR_DIV_EXPR
, type
, tl
, tr
);
402 return fold_build2 (CEIL_DIV_EXPR
, type
, tl
, tr
);
405 return fold_build2 (EXACT_DIV_EXPR
, type
, tl
, tr
);
408 return fold_build2 (TRUNC_MOD_EXPR
, type
, tl
, tr
);
422 /* Return the precision needed to represent the value VAL. */
425 precision_for_value (mpz_t val
)
441 while (mpz_cmp (y
, x
) > 0)
454 /* Return the precision needed to represent the values between LOW and
458 precision_for_interval (mpz_t low
, mpz_t up
)
463 gcc_assert (mpz_cmp (low
, up
) <= 0);
466 mpz_sub (diff
, up
, low
);
467 precision
= precision_for_value (diff
);
473 /* Return a type that could represent the integer value VAL. */
476 gcc_type_for_interval (mpz_t low
, mpz_t up
)
478 bool unsigned_p
= true;
479 int precision
, prec_up
, prec_int
;
481 enum machine_mode mode
;
483 gcc_assert (mpz_cmp (low
, up
) <= 0);
485 if (mpz_sgn (low
) < 0)
488 prec_up
= precision_for_value (up
);
489 prec_int
= precision_for_interval (low
, up
);
490 precision
= MAX (prec_up
, prec_int
);
492 if (precision
> BITS_PER_WORD
)
495 return integer_type_node
;
498 mode
= smallest_mode_for_size (precision
, MODE_INT
);
499 precision
= GET_MODE_PRECISION (mode
);
500 type
= build_nonstandard_integer_type (precision
, unsigned_p
);
505 return integer_type_node
;
511 /* Return a type that could represent the integer value VAL, or
512 otherwise return NULL_TREE. */
515 gcc_type_for_value (mpz_t val
)
517 return gcc_type_for_interval (val
, val
);
520 /* Return the type for the clast_term T used in STMT. */
523 gcc_type_for_clast_term (struct clast_term
*t
,
524 sese region
, VEC (tree
, heap
) *newivs
,
525 htab_t newivs_index
, htab_t params_index
)
527 gcc_assert (t
->expr
.type
== expr_term
);
530 return gcc_type_for_value (t
->val
);
532 return TREE_TYPE (clast_name_to_gcc (t
->var
, region
, newivs
,
533 newivs_index
, params_index
));
537 gcc_type_for_clast_expr (struct clast_expr
*, sese
,
538 VEC (tree
, heap
) *, htab_t
, htab_t
);
540 /* Return the type for the clast_reduction R used in STMT. */
543 gcc_type_for_clast_red (struct clast_reduction
*r
, sese region
,
544 VEC (tree
, heap
) *newivs
,
545 htab_t newivs_index
, htab_t params_index
)
548 tree type
= NULL_TREE
;
551 return gcc_type_for_clast_expr (r
->elts
[0], region
, newivs
,
552 newivs_index
, params_index
);
559 type
= gcc_type_for_clast_expr (r
->elts
[0], region
, newivs
,
560 newivs_index
, params_index
);
561 for (i
= 1; i
< r
->n
; i
++)
562 type
= max_precision_type (type
, gcc_type_for_clast_expr
563 (r
->elts
[i
], region
, newivs
,
564 newivs_index
, params_index
));
576 /* Return the type for the clast_binary B used in STMT. */
579 gcc_type_for_clast_bin (struct clast_binary
*b
,
580 sese region
, VEC (tree
, heap
) *newivs
,
581 htab_t newivs_index
, htab_t params_index
)
583 tree l
= gcc_type_for_clast_expr ((struct clast_expr
*) b
->LHS
, region
,
584 newivs
, newivs_index
, params_index
);
585 tree r
= gcc_type_for_value (b
->RHS
);
586 return max_signed_precision_type (l
, r
);
589 /* Returns the type for the CLAST expression E when used in statement
593 gcc_type_for_clast_expr (struct clast_expr
*e
,
594 sese region
, VEC (tree
, heap
) *newivs
,
595 htab_t newivs_index
, htab_t params_index
)
600 return gcc_type_for_clast_term ((struct clast_term
*) e
, region
,
601 newivs
, newivs_index
, params_index
);
604 return gcc_type_for_clast_red ((struct clast_reduction
*) e
, region
,
605 newivs
, newivs_index
, params_index
);
608 return gcc_type_for_clast_bin ((struct clast_binary
*) e
, region
,
609 newivs
, newivs_index
, params_index
);
618 /* Returns the type for the equation CLEQ. */
621 gcc_type_for_clast_eq (struct clast_equation
*cleq
,
622 sese region
, VEC (tree
, heap
) *newivs
,
623 htab_t newivs_index
, htab_t params_index
)
625 tree l
= gcc_type_for_clast_expr (cleq
->LHS
, region
, newivs
,
626 newivs_index
, params_index
);
627 tree r
= gcc_type_for_clast_expr (cleq
->RHS
, region
, newivs
,
628 newivs_index
, params_index
);
629 return max_precision_type (l
, r
);
632 /* Translates a clast equation CLEQ to a tree. */
635 graphite_translate_clast_equation (sese region
,
636 struct clast_equation
*cleq
,
637 VEC (tree
, heap
) *newivs
,
638 htab_t newivs_index
, htab_t params_index
)
641 tree type
= gcc_type_for_clast_eq (cleq
, region
, newivs
, newivs_index
,
643 tree lhs
= clast_to_gcc_expression (type
, cleq
->LHS
, region
, newivs
,
644 newivs_index
, params_index
);
645 tree rhs
= clast_to_gcc_expression (type
, cleq
->RHS
, region
, newivs
,
646 newivs_index
, params_index
);
651 else if (cleq
->sign
> 0)
657 return fold_build2 (comp
, boolean_type_node
, lhs
, rhs
);
660 /* Creates the test for the condition in STMT. */
663 graphite_create_guard_cond_expr (sese region
, struct clast_guard
*stmt
,
664 VEC (tree
, heap
) *newivs
,
665 htab_t newivs_index
, htab_t params_index
)
670 for (i
= 0; i
< stmt
->n
; i
++)
672 tree eq
= graphite_translate_clast_equation (region
, &stmt
->eq
[i
],
673 newivs
, newivs_index
,
677 cond
= fold_build2 (TRUTH_AND_EXPR
, TREE_TYPE (eq
), cond
, eq
);
685 /* Creates a new if region corresponding to Cloog's guard. */
688 graphite_create_new_guard (sese region
, edge entry_edge
,
689 struct clast_guard
*stmt
,
690 VEC (tree
, heap
) *newivs
,
691 htab_t newivs_index
, htab_t params_index
)
693 tree cond_expr
= graphite_create_guard_cond_expr (region
, stmt
, newivs
,
694 newivs_index
, params_index
);
695 edge exit_edge
= create_empty_if_region_on_edge (entry_edge
, cond_expr
);
699 /* Compute the lower bound LOW and upper bound UP for the induction
700 variable at LEVEL for the statement PBB, based on the transformed
701 scattering of PBB: T|I|G|Cst, with T the scattering transform, I
702 the iteration domain, and G the context parameters. */
705 compute_bounds_for_level (poly_bb_p pbb
, int level
, mpz_t low
, mpz_t up
)
707 ppl_Pointset_Powerset_C_Polyhedron_t ps
;
708 ppl_Linear_Expression_t le
;
710 combine_context_id_scat (&ps
, pbb
, false);
712 /* Prepare the linear expression corresponding to the level that we
713 want to maximize/minimize. */
715 ppl_dimension_type dim
= pbb_nb_scattering_transform (pbb
)
716 + pbb_dim_iter_domain (pbb
) + pbb_nb_params (pbb
);
718 ppl_new_Linear_Expression_with_dimension (&le
, dim
);
719 ppl_set_coef (le
, 2 * level
+ 1, 1);
722 ppl_max_for_le_pointset (ps
, le
, up
);
723 ppl_min_for_le_pointset (ps
, le
, low
);
726 /* Compute the type for the induction variable at LEVEL for the
727 statement PBB, based on the transformed schedule of PBB. */
730 compute_type_for_level (poly_bb_p pbb
, int level
)
738 compute_bounds_for_level (pbb
, level
, low
, up
);
739 type
= gcc_type_for_interval (low
, up
);
746 /* Walks a CLAST and returns the first statement in the body of a
749 static struct clast_user_stmt
*
750 clast_get_body_of_loop (struct clast_stmt
*stmt
)
753 || CLAST_STMT_IS_A (stmt
, stmt_user
))
754 return (struct clast_user_stmt
*) stmt
;
756 if (CLAST_STMT_IS_A (stmt
, stmt_for
))
757 return clast_get_body_of_loop (((struct clast_for
*) stmt
)->body
);
759 if (CLAST_STMT_IS_A (stmt
, stmt_guard
))
760 return clast_get_body_of_loop (((struct clast_guard
*) stmt
)->then
);
762 if (CLAST_STMT_IS_A (stmt
, stmt_block
))
763 return clast_get_body_of_loop (((struct clast_block
*) stmt
)->body
);
768 /* Returns the type for the induction variable for the loop translated
772 gcc_type_for_iv_of_clast_loop (struct clast_for
*stmt_for
, int level
,
773 tree lb_type
, tree ub_type
)
775 struct clast_stmt
*stmt
= (struct clast_stmt
*) stmt_for
;
776 struct clast_user_stmt
*body
= clast_get_body_of_loop (stmt
);
777 CloogStatement
*cs
= body
->statement
;
778 poly_bb_p pbb
= (poly_bb_p
) cloog_statement_usr (cs
);
780 return max_signed_precision_type (lb_type
, max_precision_type
781 (ub_type
, compute_type_for_level
785 /* Creates a new LOOP corresponding to Cloog's STMT. Inserts an
786 induction variable for the new LOOP. New LOOP is attached to CFG
787 starting at ENTRY_EDGE. LOOP is inserted into the loop tree and
788 becomes the child loop of the OUTER_LOOP. NEWIVS_INDEX binds
789 CLooG's scattering name to the induction variable created for the
790 loop of STMT. The new induction variable is inserted in the NEWIVS
794 graphite_create_new_loop (sese region
, edge entry_edge
,
795 struct clast_for
*stmt
,
796 loop_p outer
, VEC (tree
, heap
) **newivs
,
797 htab_t newivs_index
, htab_t params_index
, int level
)
799 tree lb_type
= gcc_type_for_clast_expr (stmt
->LB
, region
, *newivs
,
800 newivs_index
, params_index
);
801 tree ub_type
= gcc_type_for_clast_expr (stmt
->UB
, region
, *newivs
,
802 newivs_index
, params_index
);
803 tree type
= gcc_type_for_iv_of_clast_loop (stmt
, level
, lb_type
, ub_type
);
804 tree lb
= clast_to_gcc_expression (type
, stmt
->LB
, region
, *newivs
,
805 newivs_index
, params_index
);
806 tree ub
= clast_to_gcc_expression (type
, stmt
->UB
, region
, *newivs
,
807 newivs_index
, params_index
);
808 tree stride
= gmp_cst_to_tree (type
, stmt
->stride
);
809 tree ivvar
= create_tmp_var (type
, "graphite_IV");
810 tree iv
, iv_after_increment
;
811 loop_p loop
= create_empty_loop_on_edge
812 (entry_edge
, lb
, stride
, ub
, ivvar
, &iv
, &iv_after_increment
,
813 outer
? outer
: entry_edge
->src
->loop_father
);
815 add_referenced_var (ivvar
);
817 save_clast_name_index (newivs_index
, stmt
->iterator
,
818 VEC_length (tree
, *newivs
));
819 VEC_safe_push (tree
, heap
, *newivs
, iv
);
823 /* Inserts in RENAME_MAP a tuple (OLD_NAME, NEW_NAME) for the induction
824 variables of the loops around GBB in SESE. */
827 build_iv_mapping (htab_t rename_map
, sese region
,
828 VEC (tree
, heap
) *newivs
, htab_t newivs_index
,
829 struct clast_user_stmt
*user_stmt
,
832 struct clast_stmt
*t
;
834 CloogStatement
*cs
= user_stmt
->statement
;
835 poly_bb_p pbb
= (poly_bb_p
) cloog_statement_usr (cs
);
837 for (t
= user_stmt
->substitutions
; t
; t
= t
->next
, index
++)
839 struct clast_expr
*expr
= (struct clast_expr
*)
840 ((struct clast_assignment
*)t
)->RHS
;
841 tree type
= gcc_type_for_clast_expr (expr
, region
, newivs
,
842 newivs_index
, params_index
);
843 tree old_name
= pbb_to_depth_to_oldiv (pbb
, index
);
844 tree e
= clast_to_gcc_expression (type
, expr
, region
, newivs
,
845 newivs_index
, params_index
);
846 set_rename (rename_map
, old_name
, e
);
850 /* Construct bb_pbb_def with BB and PBB. */
853 new_bb_pbb_def (basic_block bb
, poly_bb_p pbb
)
855 bb_pbb_def
*bb_pbb_p
;
857 bb_pbb_p
= XNEW (bb_pbb_def
);
864 /* Mark BB with it's relevant PBB via hashing table BB_PBB_MAPPING. */
867 mark_bb_with_pbb (poly_bb_p pbb
, basic_block bb
, htab_t bb_pbb_mapping
)
873 x
= htab_find_slot (bb_pbb_mapping
, &tmp
, INSERT
);
876 *x
= new_bb_pbb_def (bb
, pbb
);
879 /* Find BB's related poly_bb_p in hash table BB_PBB_MAPPING. */
882 find_pbb_via_hash (htab_t bb_pbb_mapping
, basic_block bb
)
888 slot
= htab_find_slot (bb_pbb_mapping
, &tmp
, NO_INSERT
);
891 return ((bb_pbb_def
*) *slot
)->pbb
;
896 /* Check data dependency in LOOP at scattering level LEVEL.
897 BB_PBB_MAPPING is a basic_block and it's related poly_bb_p
901 dependency_in_loop_p (loop_p loop
, htab_t bb_pbb_mapping
, int level
)
904 basic_block
*bbs
= get_loop_body_in_dom_order (loop
);
906 for (i
= 0; i
< loop
->num_nodes
; i
++)
908 poly_bb_p pbb1
= find_pbb_via_hash (bb_pbb_mapping
, bbs
[i
]);
913 for (j
= 0; j
< loop
->num_nodes
; j
++)
915 poly_bb_p pbb2
= find_pbb_via_hash (bb_pbb_mapping
, bbs
[j
]);
920 if (dependency_between_pbbs_p (pbb1
, pbb2
, level
))
934 translate_clast (sese
, loop_p
, struct clast_stmt
*, edge
, htab_t
,
935 VEC (tree
, heap
) **, htab_t
, htab_t
, int, htab_t
);
937 /* Translates a clast user statement STMT to gimple.
939 - REGION is the sese region we used to generate the scop.
940 - NEXT_E is the edge where new generated code should be attached.
941 - CONTEXT_LOOP is the loop in which the generated code will be placed
942 - RENAME_MAP contains a set of tuples of new names associated to
943 the original variables names.
944 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
945 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
948 translate_clast_user (sese region
, struct clast_user_stmt
*stmt
, edge next_e
,
949 htab_t rename_map
, VEC (tree
, heap
) **newivs
,
950 htab_t newivs_index
, htab_t bb_pbb_mapping
,
955 poly_bb_p pbb
= (poly_bb_p
) cloog_statement_usr (stmt
->statement
);
956 gbb
= PBB_BLACK_BOX (pbb
);
958 if (GBB_BB (gbb
) == ENTRY_BLOCK_PTR
)
961 build_iv_mapping (rename_map
, region
, *newivs
, newivs_index
, stmt
,
963 next_e
= copy_bb_and_scalar_dependences (GBB_BB (gbb
), region
,
965 new_bb
= next_e
->src
;
966 mark_bb_with_pbb (pbb
, new_bb
, bb_pbb_mapping
);
967 update_ssa (TODO_update_ssa
);
972 /* Creates a new if region protecting the loop to be executed, if the execution
973 count is zero (lb > ub). */
975 graphite_create_new_loop_guard (sese region
, edge entry_edge
,
976 struct clast_for
*stmt
,
977 VEC (tree
, heap
) *newivs
,
978 htab_t newivs_index
, htab_t params_index
)
982 tree lb_type
= gcc_type_for_clast_expr (stmt
->LB
, region
, newivs
,
983 newivs_index
, params_index
);
984 tree ub_type
= gcc_type_for_clast_expr (stmt
->UB
, region
, newivs
,
985 newivs_index
, params_index
);
986 tree type
= max_precision_type (lb_type
, ub_type
);
987 tree lb
= clast_to_gcc_expression (type
, stmt
->LB
, region
, newivs
,
988 newivs_index
, params_index
);
989 tree ub
= clast_to_gcc_expression (type
, stmt
->UB
, region
, newivs
,
990 newivs_index
, params_index
);
993 /* Adding +1 and using LT_EXPR helps with loop latches that have a
994 loop iteration count of "PARAMETER - 1". For PARAMETER == 0 this becomes
995 2^{32|64}, and the condition lb <= ub is true, even if we do not want this.
996 However lb < ub + 1 is false, as expected. */
997 if (POINTER_TYPE_P (type
))
998 ub_one
= fold_build2 (POINTER_PLUS_EXPR
, type
, ub
, size_one_node
);
1000 ub_one
= fold_build2 (PLUS_EXPR
, type
, ub
,
1001 fold_convert (type
, integer_one_node
));
1003 /* When ub + 1 wraps around, use lb <= ub. */
1004 if (integer_zerop (ub_one
))
1005 cond_expr
= fold_build2 (LE_EXPR
, boolean_type_node
, lb
, ub
);
1007 cond_expr
= fold_build2 (LT_EXPR
, boolean_type_node
, lb
, ub_one
);
1009 exit_edge
= create_empty_if_region_on_edge (entry_edge
, cond_expr
);
1015 /* Create the loop for a clast for statement.
1017 - REGION is the sese region we used to generate the scop.
1018 - NEXT_E is the edge where new generated code should be attached.
1019 - RENAME_MAP contains a set of tuples of new names associated to
1020 the original variables names.
1021 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
1022 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
1025 translate_clast_for_loop (sese region
, loop_p context_loop
,
1026 struct clast_for
*stmt
, edge next_e
,
1027 htab_t rename_map
, VEC (tree
, heap
) **newivs
,
1028 htab_t newivs_index
, htab_t bb_pbb_mapping
,
1029 int level
, htab_t params_index
)
1031 struct loop
*loop
= graphite_create_new_loop (region
, next_e
, stmt
,
1032 context_loop
, newivs
,
1033 newivs_index
, params_index
,
1035 edge last_e
= single_exit (loop
);
1036 edge to_body
= single_succ_edge (loop
->header
);
1037 basic_block after
= to_body
->dest
;
1039 /* Create a basic block for loop close phi nodes. */
1040 last_e
= single_succ_edge (split_edge (last_e
));
1042 /* Translate the body of the loop. */
1043 next_e
= translate_clast (region
, loop
, stmt
->body
, to_body
, rename_map
,
1044 newivs
, newivs_index
, bb_pbb_mapping
, level
+ 1,
1046 redirect_edge_succ_nodup (next_e
, after
);
1047 set_immediate_dominator (CDI_DOMINATORS
, next_e
->dest
, next_e
->src
);
1049 if (flag_loop_parallelize_all
1050 && !dependency_in_loop_p (loop
, bb_pbb_mapping
,
1051 get_scattering_level (level
)))
1052 loop
->can_be_parallel
= true;
1057 /* Translates a clast for statement STMT to gimple. First a guard is created
1058 protecting the loop, if it is executed zero times. In this guard we create
1059 the real loop structure.
1061 - REGION is the sese region we used to generate the scop.
1062 - NEXT_E is the edge where new generated code should be attached.
1063 - RENAME_MAP contains a set of tuples of new names associated to
1064 the original variables names.
1065 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
1066 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
1069 translate_clast_for (sese region
, loop_p context_loop
, struct clast_for
*stmt
,
1070 edge next_e
, htab_t rename_map
, VEC (tree
, heap
) **newivs
,
1071 htab_t newivs_index
, htab_t bb_pbb_mapping
, int level
,
1072 htab_t params_index
)
1074 edge last_e
= graphite_create_new_loop_guard (region
, next_e
, stmt
, *newivs
,
1075 newivs_index
, params_index
);
1076 edge true_e
= get_true_edge_from_guard_bb (next_e
->dest
);
1078 translate_clast_for_loop (region
, context_loop
, stmt
, true_e
,
1080 newivs_index
, bb_pbb_mapping
, level
,
1085 /* Translates a clast guard statement STMT to gimple.
1087 - REGION is the sese region we used to generate the scop.
1088 - NEXT_E is the edge where new generated code should be attached.
1089 - CONTEXT_LOOP is the loop in which the generated code will be placed
1090 - RENAME_MAP contains a set of tuples of new names associated to
1091 the original variables names.
1092 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
1093 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
1096 translate_clast_guard (sese region
, loop_p context_loop
,
1097 struct clast_guard
*stmt
, edge next_e
,
1098 htab_t rename_map
, VEC (tree
, heap
) **newivs
,
1099 htab_t newivs_index
, htab_t bb_pbb_mapping
, int level
,
1100 htab_t params_index
)
1102 edge last_e
= graphite_create_new_guard (region
, next_e
, stmt
, *newivs
,
1103 newivs_index
, params_index
);
1104 edge true_e
= get_true_edge_from_guard_bb (next_e
->dest
);
1106 translate_clast (region
, context_loop
, stmt
->then
, true_e
,
1107 rename_map
, newivs
, newivs_index
, bb_pbb_mapping
,
1108 level
, params_index
);
1112 /* Translates a CLAST statement STMT to GCC representation in the
1115 - NEXT_E is the edge where new generated code should be attached.
1116 - CONTEXT_LOOP is the loop in which the generated code will be placed
1117 - RENAME_MAP contains a set of tuples of new names associated to
1118 the original variables names.
1119 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping. */
1121 translate_clast (sese region
, loop_p context_loop
, struct clast_stmt
*stmt
,
1122 edge next_e
, htab_t rename_map
, VEC (tree
, heap
) **newivs
,
1123 htab_t newivs_index
, htab_t bb_pbb_mapping
, int level
,
1124 htab_t params_index
)
1129 if (CLAST_STMT_IS_A (stmt
, stmt_root
))
1132 else if (CLAST_STMT_IS_A (stmt
, stmt_user
))
1133 next_e
= translate_clast_user (region
, (struct clast_user_stmt
*) stmt
,
1134 next_e
, rename_map
, newivs
, newivs_index
,
1135 bb_pbb_mapping
, params_index
);
1137 else if (CLAST_STMT_IS_A (stmt
, stmt_for
))
1138 next_e
= translate_clast_for (region
, context_loop
,
1139 (struct clast_for
*) stmt
, next_e
,
1140 rename_map
, newivs
, newivs_index
,
1141 bb_pbb_mapping
, level
, params_index
);
1143 else if (CLAST_STMT_IS_A (stmt
, stmt_guard
))
1144 next_e
= translate_clast_guard (region
, context_loop
,
1145 (struct clast_guard
*) stmt
, next_e
,
1146 rename_map
, newivs
, newivs_index
,
1147 bb_pbb_mapping
, level
, params_index
);
1149 else if (CLAST_STMT_IS_A (stmt
, stmt_block
))
1150 next_e
= translate_clast (region
, context_loop
,
1151 ((struct clast_block
*) stmt
)->body
,
1152 next_e
, rename_map
, newivs
, newivs_index
,
1153 bb_pbb_mapping
, level
, params_index
);
1157 recompute_all_dominators ();
1160 return translate_clast (region
, context_loop
, stmt
->next
, next_e
,
1161 rename_map
, newivs
, newivs_index
,
1162 bb_pbb_mapping
, level
, params_index
);
1165 /* Free the SCATTERING domain list. */
1168 free_scattering (CloogDomainList
*scattering
)
1172 CloogDomain
*dom
= cloog_domain (scattering
);
1173 CloogDomainList
*next
= cloog_next_domain (scattering
);
1175 cloog_domain_free (dom
);
1181 /* Initialize Cloog's parameter names from the names used in GIMPLE.
1182 Initialize Cloog's iterator names, using 'graphite_iterator_%d'
1183 from 0 to scop_nb_loops (scop). */
1186 initialize_cloog_names (scop_p scop
, CloogProgram
*prog
)
1188 sese region
= SCOP_REGION (scop
);
1190 int nb_iterators
= scop_max_loop_depth (scop
);
1191 int nb_scattering
= cloog_program_nb_scattdims (prog
);
1192 int nb_parameters
= VEC_length (tree
, SESE_PARAMS (region
));
1193 char **iterators
= XNEWVEC (char *, nb_iterators
* 2);
1194 char **scattering
= XNEWVEC (char *, nb_scattering
);
1195 char **parameters
= XNEWVEC (char *, nb_parameters
);
1197 cloog_program_set_names (prog
, cloog_names_malloc ());
1199 for (i
= 0; i
< nb_parameters
; i
++)
1201 tree param
= VEC_index (tree
, SESE_PARAMS(region
), i
);
1202 const char *name
= get_name (param
);
1208 len
= strlen (name
);
1210 parameters
[i
] = XNEWVEC (char, len
+ 1);
1211 snprintf (parameters
[i
], len
, "%s_%d", name
, SSA_NAME_VERSION (param
));
1214 cloog_names_set_nb_parameters (cloog_program_names (prog
), nb_parameters
);
1215 cloog_names_set_parameters (cloog_program_names (prog
), parameters
);
1217 for (i
= 0; i
< nb_iterators
; i
++)
1220 iterators
[i
] = XNEWVEC (char, len
);
1221 snprintf (iterators
[i
], len
, "git_%d", i
);
1224 cloog_names_set_nb_iterators (cloog_program_names (prog
),
1226 cloog_names_set_iterators (cloog_program_names (prog
),
1229 for (i
= 0; i
< nb_scattering
; i
++)
1232 scattering
[i
] = XNEWVEC (char, len
);
1233 snprintf (scattering
[i
], len
, "scat_%d", i
);
1236 cloog_names_set_nb_scattering (cloog_program_names (prog
),
1238 cloog_names_set_scattering (cloog_program_names (prog
),
1242 /* Build cloog program for SCoP. */
1245 build_cloog_prog (scop_p scop
, CloogProgram
*prog
)
1248 int max_nb_loops
= scop_max_loop_depth (scop
);
1250 CloogLoop
*loop_list
= NULL
;
1251 CloogBlockList
*block_list
= NULL
;
1252 CloogDomainList
*scattering
= NULL
;
1253 int nbs
= 2 * max_nb_loops
+ 1;
1256 cloog_program_set_context
1257 (prog
, new_Cloog_Domain_from_ppl_Pointset_Powerset (SCOP_CONTEXT (scop
)));
1258 nbs
= unify_scattering_dimensions (scop
);
1259 scaldims
= (int *) xmalloc (nbs
* (sizeof (int)));
1260 cloog_program_set_nb_scattdims (prog
, nbs
);
1261 initialize_cloog_names (scop
, prog
);
1263 for (i
= 0; VEC_iterate (poly_bb_p
, SCOP_BBS (scop
), i
, pbb
); i
++)
1265 CloogStatement
*stmt
;
1268 /* Dead code elimination: when the domain of a PBB is empty,
1269 don't generate code for the PBB. */
1270 if (ppl_Pointset_Powerset_C_Polyhedron_is_empty (PBB_DOMAIN (pbb
)))
1273 /* Build the new statement and its block. */
1274 stmt
= cloog_statement_alloc (pbb_index (pbb
));
1275 block
= cloog_block_alloc (stmt
, 0, NULL
, pbb_dim_iter_domain (pbb
));
1276 cloog_statement_set_usr (stmt
, pbb
);
1278 /* Build loop list. */
1280 CloogLoop
*new_loop_list
= cloog_loop_malloc ();
1281 cloog_loop_set_next (new_loop_list
, loop_list
);
1282 cloog_loop_set_domain
1284 new_Cloog_Domain_from_ppl_Pointset_Powerset (PBB_DOMAIN (pbb
)));
1285 cloog_loop_set_block (new_loop_list
, block
);
1286 loop_list
= new_loop_list
;
1289 /* Build block list. */
1291 CloogBlockList
*new_block_list
= cloog_block_list_malloc ();
1293 cloog_block_list_set_next (new_block_list
, block_list
);
1294 cloog_block_list_set_block (new_block_list
, block
);
1295 block_list
= new_block_list
;
1298 /* Build scattering list. */
1300 /* XXX: Replace with cloog_domain_list_alloc(), when available. */
1301 CloogDomainList
*new_scattering
1302 = (CloogDomainList
*) xmalloc (sizeof (CloogDomainList
));
1303 ppl_Polyhedron_t scat
;
1306 scat
= PBB_TRANSFORMED_SCATTERING (pbb
);
1307 dom
= new_Cloog_Domain_from_ppl_Polyhedron (scat
);
1309 cloog_set_next_domain (new_scattering
, scattering
);
1310 cloog_set_domain (new_scattering
, dom
);
1311 scattering
= new_scattering
;
1315 cloog_program_set_loop (prog
, loop_list
);
1316 cloog_program_set_blocklist (prog
, block_list
);
1318 for (i
= 0; i
< nbs
; i
++)
1321 cloog_program_set_scaldims (prog
, scaldims
);
1323 /* Extract scalar dimensions to simplify the code generation problem. */
1324 cloog_program_extract_scalars (prog
, scattering
);
1326 /* Apply scattering. */
1327 cloog_program_scatter (prog
, scattering
);
1328 free_scattering (scattering
);
1330 /* Iterators corresponding to scalar dimensions have to be extracted. */
1331 cloog_names_scalarize (cloog_program_names (prog
), nbs
,
1332 cloog_program_scaldims (prog
));
1334 /* Free blocklist. */
1336 CloogBlockList
*next
= cloog_program_blocklist (prog
);
1340 CloogBlockList
*toDelete
= next
;
1341 next
= cloog_block_list_next (next
);
1342 cloog_block_list_set_next (toDelete
, NULL
);
1343 cloog_block_list_set_block (toDelete
, NULL
);
1344 cloog_block_list_free (toDelete
);
1346 cloog_program_set_blocklist (prog
, NULL
);
1350 /* Return the options that will be used in GLOOG. */
1352 static CloogOptions
*
1353 set_cloog_options (void)
1355 CloogOptions
*options
= cloog_options_malloc ();
1357 /* Change cloog output language to C. If we do use FORTRAN instead, cloog
1358 will stop e.g. with "ERROR: unbounded loops not allowed in FORTRAN.", if
1359 we pass an incomplete program to cloog. */
1360 options
->language
= LANGUAGE_C
;
1362 /* Enable complex equality spreading: removes dummy statements
1363 (assignments) in the generated code which repeats the
1364 substitution equations for statements. This is useless for
1368 /* Enable C pretty-printing mode: normalizes the substitution
1369 equations for statements. */
1372 /* Allow cloog to build strides with a stride width different to one.
1373 This example has stride = 4:
1375 for (i = 0; i < 20; i += 4)
1377 options
->strides
= 1;
1379 /* Disable optimizations and make cloog generate source code closer to the
1380 input. This is useful for debugging, but later we want the optimized
1383 XXX: We can not disable optimizations, as loop blocking is not working
1385 if (!flag_graphite_cloog_opts
)
1388 options
->l
= INT_MAX
;
1394 /* Prints STMT to STDERR. */
1397 print_clast_stmt (FILE *file
, struct clast_stmt
*stmt
)
1399 CloogOptions
*options
= set_cloog_options ();
1401 pprint (file
, stmt
, 0, options
);
1402 cloog_options_free (options
);
1405 /* Prints STMT to STDERR. */
1408 debug_clast_stmt (struct clast_stmt
*stmt
)
1410 print_clast_stmt (stderr
, stmt
);
1413 /* Translate SCOP to a CLooG program and clast. These two
1414 representations should be freed together: a clast cannot be used
1415 without a program. */
1418 scop_to_clast (scop_p scop
)
1420 CloogOptions
*options
= set_cloog_options ();
1421 cloog_prog_clast pc
;
1423 /* Connect new cloog prog generation to graphite. */
1424 pc
.prog
= cloog_program_malloc ();
1425 build_cloog_prog (scop
, pc
.prog
);
1426 pc
.prog
= cloog_program_generate (pc
.prog
, options
);
1427 pc
.stmt
= cloog_clast_create (pc
.prog
, options
);
1429 cloog_options_free (options
);
1433 /* Prints to FILE the code generated by CLooG for SCOP. */
1436 print_generated_program (FILE *file
, scop_p scop
)
1438 CloogOptions
*options
= set_cloog_options ();
1439 cloog_prog_clast pc
= scop_to_clast (scop
);
1441 fprintf (file
, " (prog: \n");
1442 cloog_program_print (file
, pc
.prog
);
1443 fprintf (file
, " )\n");
1445 fprintf (file
, " (clast: \n");
1446 pprint (file
, pc
.stmt
, 0, options
);
1447 fprintf (file
, " )\n");
1449 cloog_options_free (options
);
1450 cloog_clast_free (pc
.stmt
);
1451 cloog_program_free (pc
.prog
);
1454 /* Prints to STDERR the code generated by CLooG for SCOP. */
1457 debug_generated_program (scop_p scop
)
1459 print_generated_program (stderr
, scop
);
1462 /* Add CLooG names to parameter index. The index is used to translate
1463 back from CLooG names to GCC trees. */
1466 create_params_index (htab_t index_table
, CloogProgram
*prog
) {
1467 CloogNames
* names
= cloog_program_names (prog
);
1468 int nb_parameters
= cloog_names_nb_parameters (names
);
1469 char **parameters
= cloog_names_parameters (names
);
1472 for (i
= 0; i
< nb_parameters
; i
++)
1473 save_clast_name_index (index_table
, parameters
[i
], i
);
1476 /* GIMPLE Loop Generator: generates loops from STMT in GIMPLE form for
1477 the given SCOP. Return true if code generation succeeded.
1478 BB_PBB_MAPPING is a basic_block and it's related poly_bb_p mapping.
1482 gloog (scop_p scop
, htab_t bb_pbb_mapping
)
1484 VEC (tree
, heap
) *newivs
= VEC_alloc (tree
, heap
, 10);
1485 loop_p context_loop
;
1486 sese region
= SCOP_REGION (scop
);
1487 ifsese if_region
= NULL
;
1488 htab_t rename_map
, newivs_index
, params_index
;
1489 cloog_prog_clast pc
;
1491 timevar_push (TV_GRAPHITE_CODE_GEN
);
1492 gloog_error
= false;
1494 pc
= scop_to_clast (scop
);
1496 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1498 fprintf (dump_file
, "\nCLAST generated by CLooG: \n");
1499 print_clast_stmt (dump_file
, pc
.stmt
);
1500 fprintf (dump_file
, "\n");
1503 recompute_all_dominators ();
1506 if_region
= move_sese_in_condition (region
);
1507 sese_insert_phis_for_liveouts (region
,
1508 if_region
->region
->exit
->src
,
1509 if_region
->false_region
->exit
,
1510 if_region
->true_region
->exit
);
1511 recompute_all_dominators ();
1514 context_loop
= SESE_ENTRY (region
)->src
->loop_father
;
1515 rename_map
= htab_create (10, rename_map_elt_info
, eq_rename_map_elts
, free
);
1516 newivs_index
= htab_create (10, clast_name_index_elt_info
,
1517 eq_clast_name_indexes
, free
);
1518 params_index
= htab_create (10, clast_name_index_elt_info
,
1519 eq_clast_name_indexes
, free
);
1521 create_params_index (params_index
, pc
.prog
);
1523 translate_clast (region
, context_loop
, pc
.stmt
,
1524 if_region
->true_region
->entry
,
1525 rename_map
, &newivs
, newivs_index
,
1526 bb_pbb_mapping
, 1, params_index
);
1529 recompute_all_dominators ();
1533 set_ifsese_condition (if_region
, integer_zero_node
);
1535 free (if_region
->true_region
);
1536 free (if_region
->region
);
1539 htab_delete (rename_map
);
1540 htab_delete (newivs_index
);
1541 htab_delete (params_index
);
1542 VEC_free (tree
, heap
, newivs
);
1543 cloog_clast_free (pc
.stmt
);
1544 cloog_program_free (pc
.prog
);
1545 timevar_pop (TV_GRAPHITE_CODE_GEN
);
1547 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1551 int num_no_dependency
= 0;
1553 FOR_EACH_LOOP (li
, loop
, 0)
1554 if (loop
->can_be_parallel
)
1555 num_no_dependency
++;
1557 fprintf (dump_file
, "\n%d loops carried no dependency.\n",
1561 return !gloog_error
;