1 /* Translation of CLAST (CLooG AST) to Gimple.
2 Copyright (C) 2009 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"
46 #include "cloog/cloog.h"
48 #include "graphite-ppl.h"
50 #include "graphite-poly.h"
51 #include "graphite-scop-detection.h"
52 #include "graphite-clast-to-gimple.h"
53 #include "graphite-dependences.h"
55 /* This flag is set when an error occurred during the translation of
57 static bool gloog_error;
59 /* Verifies properties that GRAPHITE should maintain during translation. */
62 graphite_verify (void)
64 #ifdef ENABLE_CHECKING
65 verify_loop_structure ();
66 verify_dominators (CDI_DOMINATORS);
67 verify_dominators (CDI_POST_DOMINATORS);
69 verify_loop_closed_ssa ();
73 /* Stores the INDEX in a vector for a given clast NAME. */
75 typedef struct clast_name_index {
78 } *clast_name_index_p;
80 /* Returns a pointer to a new element of type clast_name_index_p built
81 from NAME and INDEX. */
83 static inline clast_name_index_p
84 new_clast_name_index (const char *name, int index)
86 clast_name_index_p res = XNEW (struct clast_name_index);
93 /* For a given clast NAME, returns -1 if it does not correspond to any
94 parameter, or otherwise, returns the index in the PARAMS or
95 SCATTERING_DIMENSIONS vector. */
98 clast_name_to_index (const char *name, htab_t index_table)
100 struct clast_name_index tmp;
104 slot = htab_find_slot (index_table, &tmp, NO_INSERT);
107 return ((struct clast_name_index *) *slot)->index;
112 /* Records in INDEX_TABLE the INDEX for NAME. */
115 save_clast_name_index (htab_t index_table, const char *name, int index)
117 struct clast_name_index tmp;
121 slot = htab_find_slot (index_table, &tmp, INSERT);
128 *slot = new_clast_name_index (name, index);
132 /* Print to stderr the element ELT. */
135 debug_clast_name_index (clast_name_index_p elt)
137 fprintf (stderr, "(index = %d, name = %s)\n", elt->index, elt->name);
140 /* Helper function for debug_rename_map. */
143 debug_clast_name_indexes_1 (void **slot, void *s ATTRIBUTE_UNUSED)
145 struct clast_name_index *entry = (struct clast_name_index *) *slot;
146 debug_clast_name_index (entry);
150 /* Print to stderr all the elements of MAP. */
153 debug_clast_name_indexes (htab_t map)
155 htab_traverse (map, debug_clast_name_indexes_1, NULL);
158 /* Computes a hash function for database element ELT. */
160 static inline hashval_t
161 clast_name_index_elt_info (const void *elt)
163 return htab_hash_pointer (((const struct clast_name_index *) elt)->name);
166 /* Compares database elements E1 and E2. */
169 eq_clast_name_indexes (const void *e1, const void *e2)
171 const struct clast_name_index *elt1 = (const struct clast_name_index *) e1;
172 const struct clast_name_index *elt2 = (const struct clast_name_index *) e2;
174 return (elt1->name == elt2->name);
178 /* For a given loop DEPTH in the loop nest of the original black box
179 PBB, return the old induction variable associated to that loop. */
182 pbb_to_depth_to_oldiv (poly_bb_p pbb, int depth)
184 gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
185 sese region = SCOP_REGION (PBB_SCOP (pbb));
186 loop_p loop = gbb_loop_at_index (gbb, region, depth);
188 return loop->single_iv;
191 /* For a given scattering dimension, return the new induction variable
195 newivs_to_depth_to_newiv (VEC (tree, heap) *newivs, int depth)
197 return VEC_index (tree, newivs, depth);
202 /* Returns the tree variable from the name NAME that was given in
203 Cloog representation. */
206 clast_name_to_gcc (const char *name, sese region, VEC (tree, heap) *newivs,
207 htab_t newivs_index, htab_t params_index)
210 VEC (tree, heap) *params = SESE_PARAMS (region);
212 if (params && params_index)
214 index = clast_name_to_index (name, params_index);
217 return VEC_index (tree, params, index);
220 gcc_assert (newivs && newivs_index);
221 index = clast_name_to_index (name, newivs_index);
222 gcc_assert (index >= 0);
224 return newivs_to_depth_to_newiv (newivs, index);
227 /* Returns the maximal precision type for expressions E1 and E2. */
230 max_precision_type (tree e1, tree e2)
232 tree type1 = TREE_TYPE (e1);
233 tree type2 = TREE_TYPE (e2);
234 return TYPE_PRECISION (type1) > TYPE_PRECISION (type2) ? type1 : type2;
238 clast_to_gcc_expression (tree, struct clast_expr *, sese, VEC (tree, heap) *,
241 /* Converts a Cloog reduction expression R with reduction operation OP
242 to a GCC expression tree of type TYPE. */
245 clast_to_gcc_expression_red (tree type, enum tree_code op,
246 struct clast_reduction *r,
247 sese region, VEC (tree, heap) *newivs,
248 htab_t newivs_index, htab_t params_index)
251 tree res = clast_to_gcc_expression (type, r->elts[0], region, newivs,
252 newivs_index, params_index);
253 tree operand_type = (op == POINTER_PLUS_EXPR) ? sizetype : type;
255 for (i = 1; i < r->n; i++)
257 tree t = clast_to_gcc_expression (operand_type, r->elts[i], region,
258 newivs, newivs_index, params_index);
259 res = fold_build2 (op, type, res, t);
265 /* Converts a Cloog AST expression E back to a GCC expression tree of
269 clast_to_gcc_expression (tree type, struct clast_expr *e,
270 sese region, VEC (tree, heap) *newivs,
271 htab_t newivs_index, htab_t params_index)
277 struct clast_term *t = (struct clast_term *) e;
281 if (value_one_p (t->val))
283 tree name = clast_name_to_gcc (t->var, region, newivs,
284 newivs_index, params_index);
286 if (POINTER_TYPE_P (TREE_TYPE (name)) != POINTER_TYPE_P (type))
287 name = fold_convert (sizetype, name);
289 name = fold_convert (type, name);
293 else if (value_mone_p (t->val))
295 tree name = clast_name_to_gcc (t->var, region, newivs,
296 newivs_index, params_index);
298 if (POINTER_TYPE_P (TREE_TYPE (name)) != POINTER_TYPE_P (type))
299 name = fold_convert (sizetype, name);
301 name = fold_convert (type, name);
303 return fold_build1 (NEGATE_EXPR, type, name);
307 tree name = clast_name_to_gcc (t->var, region, newivs,
308 newivs_index, params_index);
309 tree cst = gmp_cst_to_tree (type, t->val);
311 if (POINTER_TYPE_P (TREE_TYPE (name)) != POINTER_TYPE_P (type))
312 name = fold_convert (sizetype, name);
314 name = fold_convert (type, name);
316 if (!POINTER_TYPE_P (type))
317 return fold_build2 (MULT_EXPR, type, cst, name);
324 return gmp_cst_to_tree (type, t->val);
329 struct clast_reduction *r = (struct clast_reduction *) e;
334 return clast_to_gcc_expression_red
335 (type, POINTER_TYPE_P (type) ? POINTER_PLUS_EXPR : PLUS_EXPR,
336 r, region, newivs, newivs_index, params_index);
339 return clast_to_gcc_expression_red (type, MIN_EXPR, r, region,
340 newivs, newivs_index,
344 return clast_to_gcc_expression_red (type, MAX_EXPR, r, region,
345 newivs, newivs_index,
356 struct clast_binary *b = (struct clast_binary *) e;
357 struct clast_expr *lhs = (struct clast_expr *) b->LHS;
358 tree tl = clast_to_gcc_expression (type, lhs, region, newivs,
359 newivs_index, params_index);
360 tree tr = gmp_cst_to_tree (type, b->RHS);
365 return fold_build2 (FLOOR_DIV_EXPR, type, tl, tr);
368 return fold_build2 (CEIL_DIV_EXPR, type, tl, tr);
371 return fold_build2 (EXACT_DIV_EXPR, type, tl, tr);
374 return fold_build2 (TRUNC_MOD_EXPR, type, tl, tr);
388 /* Returns the type for the expression E. */
391 gcc_type_for_clast_expr (struct clast_expr *e,
392 sese region, VEC (tree, heap) *newivs,
393 htab_t newivs_index, htab_t params_index)
399 struct clast_term *t = (struct clast_term *) e;
402 return TREE_TYPE (clast_name_to_gcc (t->var, region, newivs,
403 newivs_index, params_index));
410 struct clast_reduction *r = (struct clast_reduction *) e;
413 return gcc_type_for_clast_expr (r->elts[0], region, newivs,
414 newivs_index, params_index);
418 for (i = 0; i < r->n; i++)
420 tree type = gcc_type_for_clast_expr (r->elts[i], region,
421 newivs, newivs_index,
432 struct clast_binary *b = (struct clast_binary *) e;
433 struct clast_expr *lhs = (struct clast_expr *) b->LHS;
434 return gcc_type_for_clast_expr (lhs, region, newivs,
435 newivs_index, params_index);
445 /* Returns the type for the equation CLEQ. */
448 gcc_type_for_clast_eq (struct clast_equation *cleq,
449 sese region, VEC (tree, heap) *newivs,
450 htab_t newivs_index, htab_t params_index)
452 tree type = gcc_type_for_clast_expr (cleq->LHS, region, newivs,
453 newivs_index, params_index);
457 return gcc_type_for_clast_expr (cleq->RHS, region, newivs, newivs_index,
461 /* Translates a clast equation CLEQ to a tree. */
464 graphite_translate_clast_equation (sese region,
465 struct clast_equation *cleq,
466 VEC (tree, heap) *newivs,
467 htab_t newivs_index, htab_t params_index)
470 tree type = gcc_type_for_clast_eq (cleq, region, newivs, newivs_index,
472 tree lhs = clast_to_gcc_expression (type, cleq->LHS, region, newivs,
473 newivs_index, params_index);
474 tree rhs = clast_to_gcc_expression (type, cleq->RHS, region, newivs,
475 newivs_index, params_index);
480 else if (cleq->sign > 0)
486 return fold_build2 (comp, boolean_type_node, lhs, rhs);
489 /* Creates the test for the condition in STMT. */
492 graphite_create_guard_cond_expr (sese region, struct clast_guard *stmt,
493 VEC (tree, heap) *newivs,
494 htab_t newivs_index, htab_t params_index)
499 for (i = 0; i < stmt->n; i++)
501 tree eq = graphite_translate_clast_equation (region, &stmt->eq[i],
502 newivs, newivs_index,
506 cond = fold_build2 (TRUTH_AND_EXPR, TREE_TYPE (eq), cond, eq);
514 /* Creates a new if region corresponding to Cloog's guard. */
517 graphite_create_new_guard (sese region, edge entry_edge,
518 struct clast_guard *stmt,
519 VEC (tree, heap) *newivs,
520 htab_t newivs_index, htab_t params_index)
522 tree cond_expr = graphite_create_guard_cond_expr (region, stmt, newivs,
523 newivs_index, params_index);
524 edge exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr);
528 /* Walks a CLAST and returns the first statement in the body of a
531 static struct clast_user_stmt *
532 clast_get_body_of_loop (struct clast_stmt *stmt)
535 || CLAST_STMT_IS_A (stmt, stmt_user))
536 return (struct clast_user_stmt *) stmt;
538 if (CLAST_STMT_IS_A (stmt, stmt_for))
539 return clast_get_body_of_loop (((struct clast_for *) stmt)->body);
541 if (CLAST_STMT_IS_A (stmt, stmt_guard))
542 return clast_get_body_of_loop (((struct clast_guard *) stmt)->then);
544 if (CLAST_STMT_IS_A (stmt, stmt_block))
545 return clast_get_body_of_loop (((struct clast_block *) stmt)->body);
550 /* Given a CLOOG_IV, return the type that CLOOG_IV should have in GCC
551 land. The selected type is big enough to include the original loop
552 iteration variable, but signed to work with the subtractions CLooG
553 may have introduced. If such a type is not available, we fail.
555 TODO: Do not always return long_long, but the smallest possible
556 type, that still holds the original type.
558 TODO: Get the types using CLooG instead. This enables further
559 optimizations, but needs CLooG support. */
562 gcc_type_for_cloog_iv (const char *cloog_iv, gimple_bb_p gbb)
564 struct ivtype_map_elt_s tmp;
567 tmp.cloog_iv = cloog_iv;
568 slot = htab_find_slot (GBB_CLOOG_IV_TYPES (gbb), &tmp, NO_INSERT);
572 tree type = ((ivtype_map_elt) *slot)->type;
573 int type_precision = TYPE_PRECISION (type);
575 /* Find the smallest signed type possible. */
576 if (!TYPE_UNSIGNED (type))
578 if (type_precision <= TYPE_PRECISION (integer_type_node))
579 return integer_type_node;
581 if (type_precision <= TYPE_PRECISION (long_integer_type_node))
582 return long_integer_type_node;
584 if (type_precision <= TYPE_PRECISION (long_long_integer_type_node))
585 return long_long_integer_type_node;
590 if (type_precision < TYPE_PRECISION (integer_type_node))
591 return integer_type_node;
593 if (type_precision < TYPE_PRECISION (long_integer_type_node))
594 return long_integer_type_node;
596 if (type_precision < TYPE_PRECISION (long_long_integer_type_node))
597 return long_long_integer_type_node;
599 /* There is no signed type available, that is large enough to hold the
604 return long_long_integer_type_node;
607 /* Returns the induction variable for the loop that gets translated to
611 gcc_type_for_iv_of_clast_loop (struct clast_for *stmt_for)
613 struct clast_stmt *stmt = (struct clast_stmt *) stmt_for;
614 struct clast_user_stmt *body = clast_get_body_of_loop (stmt);
615 const char *cloog_iv = stmt_for->iterator;
616 CloogStatement *cs = body->statement;
617 poly_bb_p pbb = (poly_bb_p) cloog_statement_usr (cs);
619 return gcc_type_for_cloog_iv (cloog_iv, PBB_BLACK_BOX (pbb));
622 /* Creates a new LOOP corresponding to Cloog's STMT. Inserts an
623 induction variable for the new LOOP. New LOOP is attached to CFG
624 starting at ENTRY_EDGE. LOOP is inserted into the loop tree and
625 becomes the child loop of the OUTER_LOOP. NEWIVS_INDEX binds
626 CLooG's scattering name to the induction variable created for the
627 loop of STMT. The new induction variable is inserted in the NEWIVS
631 graphite_create_new_loop (sese region, edge entry_edge,
632 struct clast_for *stmt,
633 loop_p outer, VEC (tree, heap) **newivs,
634 htab_t newivs_index, htab_t params_index)
636 tree type = gcc_type_for_iv_of_clast_loop (stmt);
637 tree lb = clast_to_gcc_expression (type, stmt->LB, region, *newivs,
638 newivs_index, params_index);
639 tree ub = clast_to_gcc_expression (type, stmt->UB, region, *newivs,
640 newivs_index, params_index);
641 tree stride = gmp_cst_to_tree (type, stmt->stride);
642 tree ivvar = create_tmp_var (type, "graphite_IV");
643 tree iv, iv_after_increment;
644 loop_p loop = create_empty_loop_on_edge
645 (entry_edge, lb, stride, ub, ivvar, &iv, &iv_after_increment,
646 outer ? outer : entry_edge->src->loop_father);
648 add_referenced_var (ivvar);
650 save_clast_name_index (newivs_index, stmt->iterator,
651 VEC_length (tree, *newivs));
652 VEC_safe_push (tree, heap, *newivs, iv);
656 /* Inserts in MAP a tuple (OLD_NAME, NEW_NAME) for the induction
657 variables of the loops around GBB in SESE. */
660 build_iv_mapping (htab_t map, sese region,
661 VEC (tree, heap) *newivs, htab_t newivs_index,
662 struct clast_user_stmt *user_stmt,
665 struct clast_stmt *t;
667 CloogStatement *cs = user_stmt->statement;
668 poly_bb_p pbb = (poly_bb_p) cloog_statement_usr (cs);
670 for (t = user_stmt->substitutions; t; t = t->next, index++)
672 struct clast_expr *expr = (struct clast_expr *)
673 ((struct clast_assignment *)t)->RHS;
674 tree type = gcc_type_for_clast_expr (expr, region, newivs,
675 newivs_index, params_index);
676 tree old_name = pbb_to_depth_to_oldiv (pbb, index);
677 tree e = clast_to_gcc_expression (type, expr, region, newivs,
678 newivs_index, params_index);
679 set_rename (map, old_name, e);
683 /* Helper function for htab_traverse. */
686 copy_renames (void **slot, void *s)
688 struct rename_map_elt_s *entry = (struct rename_map_elt_s *) *slot;
689 htab_t res = (htab_t) s;
690 tree old_name = entry->old_name;
691 tree expr = entry->expr;
692 struct rename_map_elt_s tmp;
695 tmp.old_name = old_name;
696 x = htab_find_slot (res, &tmp, INSERT);
699 *x = new_rename_map_elt (old_name, expr);
704 /* Construct bb_pbb_def with BB and PBB. */
707 new_bb_pbb_def (basic_block bb, poly_bb_p pbb)
709 bb_pbb_def *bb_pbb_p;
711 bb_pbb_p = XNEW (bb_pbb_def);
718 /* Mark BB with it's relevant PBB via hashing table BB_PBB_MAPPING. */
721 mark_bb_with_pbb (poly_bb_p pbb, basic_block bb, htab_t bb_pbb_mapping)
727 x = htab_find_slot (bb_pbb_mapping, &tmp, INSERT);
730 *x = new_bb_pbb_def (bb, pbb);
733 /* Find BB's related poly_bb_p in hash table BB_PBB_MAPPING. */
736 find_pbb_via_hash (htab_t bb_pbb_mapping, basic_block bb)
742 slot = htab_find_slot (bb_pbb_mapping, &tmp, NO_INSERT);
745 return ((bb_pbb_def *) *slot)->pbb;
750 /* Check data dependency in LOOP at scattering level LEVEL.
751 BB_PBB_MAPPING is a basic_block and it's related poly_bb_p
755 dependency_in_loop_p (loop_p loop, htab_t bb_pbb_mapping, int level)
758 basic_block *bbs = get_loop_body_in_dom_order (loop);
760 for (i = 0; i < loop->num_nodes; i++)
762 poly_bb_p pbb1 = find_pbb_via_hash (bb_pbb_mapping, bbs[i]);
767 for (j = 0; j < loop->num_nodes; j++)
769 poly_bb_p pbb2 = find_pbb_via_hash (bb_pbb_mapping, bbs[j]);
774 if (dependency_between_pbbs_p (pbb1, pbb2, level))
788 translate_clast (sese, loop_p, struct clast_stmt *, edge, htab_t,
789 VEC (tree, heap) **, htab_t, htab_t, int, htab_t);
791 /* Translates a clast user statement STMT to gimple.
793 - REGION is the sese region we used to generate the scop.
794 - NEXT_E is the edge where new generated code should be attached.
795 - CONTEXT_LOOP is the loop in which the generated code will be placed
796 - RENAME_MAP contains a set of tuples of new names associated to
797 the original variables names.
798 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
799 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
802 translate_clast_user (sese region, struct clast_user_stmt *stmt, edge next_e,
803 htab_t rename_map, VEC (tree, heap) **newivs,
804 htab_t newivs_index, htab_t bb_pbb_mapping,
809 poly_bb_p pbb = (poly_bb_p) cloog_statement_usr (stmt->statement);
810 gbb = PBB_BLACK_BOX (pbb);
812 if (GBB_BB (gbb) == ENTRY_BLOCK_PTR)
815 build_iv_mapping (rename_map, region, *newivs, newivs_index, stmt,
817 next_e = copy_bb_and_scalar_dependences (GBB_BB (gbb), region,
819 new_bb = next_e->src;
820 mark_bb_with_pbb (pbb, new_bb, bb_pbb_mapping);
821 update_ssa (TODO_update_ssa);
826 /* Creates a new if region protecting the loop to be executed, if the execution
827 count is zero (lb > ub). */
829 graphite_create_new_loop_guard (sese region, edge entry_edge,
830 struct clast_for *stmt,
831 VEC (tree, heap) *newivs,
832 htab_t newivs_index, htab_t params_index)
836 tree type = gcc_type_for_iv_of_clast_loop (stmt);
837 tree lb = clast_to_gcc_expression (type, stmt->LB, region, newivs,
838 newivs_index, params_index);
839 tree ub = clast_to_gcc_expression (type, stmt->UB, region, newivs,
840 newivs_index, params_index);
842 /* XXX: Adding +1 and using LT_EXPR helps with loop latches that have a
843 loop iteration count of "PARAMETER - 1". For PARAMETER == 0 this becomes
844 2^{32|64}, and the condition lb <= ub is true, even if we do not want this.
845 However lb < ub + 1 is false, as expected.
846 There might be a problem with cases where ub is 2^32. */
849 value_init (gmp_one);
850 value_set_si (gmp_one, 1);
851 one = gmp_cst_to_tree (type, gmp_one);
852 value_clear (gmp_one);
854 ub = fold_build2 (PLUS_EXPR, type, ub, one);
855 cond_expr = fold_build2 (LT_EXPR, boolean_type_node, lb, ub);
857 exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr);
863 /* Create the loop for a clast for statement.
865 - REGION is the sese region we used to generate the scop.
866 - NEXT_E is the edge where new generated code should be attached.
867 - RENAME_MAP contains a set of tuples of new names associated to
868 the original variables names.
869 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
870 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
873 translate_clast_for_loop (sese region, loop_p context_loop,
874 struct clast_for *stmt, edge next_e,
875 htab_t rename_map, VEC (tree, heap) **newivs,
876 htab_t newivs_index, htab_t bb_pbb_mapping,
877 int level, htab_t params_index)
879 struct loop *loop = graphite_create_new_loop (region, next_e, stmt,
880 context_loop, newivs,
881 newivs_index, params_index);
882 edge last_e = single_exit (loop);
883 edge to_body = single_succ_edge (loop->header);
884 basic_block after = to_body->dest;
886 /* Create a basic block for loop close phi nodes. */
887 last_e = single_succ_edge (split_edge (last_e));
889 /* Translate the body of the loop. */
890 next_e = translate_clast (region, loop, stmt->body, to_body, rename_map,
891 newivs, newivs_index, bb_pbb_mapping, level + 1,
893 redirect_edge_succ_nodup (next_e, after);
894 set_immediate_dominator (CDI_DOMINATORS, next_e->dest, next_e->src);
896 /* Remove from rename_map all the tuples containing variables
897 defined in loop's body. */
898 insert_loop_close_phis (rename_map, loop);
900 if (flag_loop_parallelize_all
901 && !dependency_in_loop_p (loop, bb_pbb_mapping,
902 get_scattering_level (level)))
903 loop->can_be_parallel = true;
908 /* Translates a clast for statement STMT to gimple. First a guard is created
909 protecting the loop, if it is executed zero times. In this guard we create
910 the real loop structure.
912 - REGION is the sese region we used to generate the scop.
913 - NEXT_E is the edge where new generated code should be attached.
914 - RENAME_MAP contains a set of tuples of new names associated to
915 the original variables names.
916 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
917 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
920 translate_clast_for (sese region, loop_p context_loop, struct clast_for *stmt,
921 edge next_e, htab_t rename_map, VEC (tree, heap) **newivs,
922 htab_t newivs_index, htab_t bb_pbb_mapping, int level,
925 edge last_e = graphite_create_new_loop_guard (region, next_e, stmt, *newivs,
926 newivs_index, params_index);
928 edge true_e = get_true_edge_from_guard_bb (next_e->dest);
929 edge false_e = get_false_edge_from_guard_bb (next_e->dest);
930 edge exit_true_e = single_succ_edge (true_e->dest);
931 edge exit_false_e = single_succ_edge (false_e->dest);
933 htab_t before_guard = htab_create (10, rename_map_elt_info,
934 eq_rename_map_elts, free);
935 htab_traverse (rename_map, copy_renames, before_guard);
937 next_e = translate_clast_for_loop (region, context_loop, stmt, true_e,
939 newivs_index, bb_pbb_mapping, level,
942 insert_guard_phis (last_e->src, exit_true_e, exit_false_e,
943 before_guard, rename_map);
945 htab_delete (before_guard);
950 /* Translates a clast guard statement STMT to gimple.
952 - REGION is the sese region we used to generate the scop.
953 - NEXT_E is the edge where new generated code should be attached.
954 - CONTEXT_LOOP is the loop in which the generated code will be placed
955 - RENAME_MAP contains a set of tuples of new names associated to
956 the original variables names.
957 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
958 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
961 translate_clast_guard (sese region, loop_p context_loop,
962 struct clast_guard *stmt, edge next_e,
963 htab_t rename_map, VEC (tree, heap) **newivs,
964 htab_t newivs_index, htab_t bb_pbb_mapping, int level,
967 edge last_e = graphite_create_new_guard (region, next_e, stmt, *newivs,
968 newivs_index, params_index);
970 edge true_e = get_true_edge_from_guard_bb (next_e->dest);
971 edge false_e = get_false_edge_from_guard_bb (next_e->dest);
972 edge exit_true_e = single_succ_edge (true_e->dest);
973 edge exit_false_e = single_succ_edge (false_e->dest);
975 htab_t before_guard = htab_create (10, rename_map_elt_info,
976 eq_rename_map_elts, free);
977 htab_traverse (rename_map, copy_renames, before_guard);
979 next_e = translate_clast (region, context_loop, stmt->then, true_e,
980 rename_map, newivs, newivs_index, bb_pbb_mapping,
981 level, params_index);
983 insert_guard_phis (last_e->src, exit_true_e, exit_false_e,
984 before_guard, rename_map);
986 htab_delete (before_guard);
991 /* Translates a CLAST statement STMT to GCC representation in the
994 - NEXT_E is the edge where new generated code should be attached.
995 - CONTEXT_LOOP is the loop in which the generated code will be placed
996 - RENAME_MAP contains a set of tuples of new names associated to
997 the original variables names.
998 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping. */
1000 translate_clast (sese region, loop_p context_loop, struct clast_stmt *stmt,
1001 edge next_e, htab_t rename_map, VEC (tree, heap) **newivs,
1002 htab_t newivs_index, htab_t bb_pbb_mapping, int level,
1003 htab_t params_index)
1008 if (CLAST_STMT_IS_A (stmt, stmt_root))
1011 else if (CLAST_STMT_IS_A (stmt, stmt_user))
1012 next_e = translate_clast_user (region, (struct clast_user_stmt *) stmt,
1013 next_e, rename_map, newivs, newivs_index,
1014 bb_pbb_mapping, params_index);
1016 else if (CLAST_STMT_IS_A (stmt, stmt_for))
1017 next_e = translate_clast_for (region, context_loop,
1018 (struct clast_for *) stmt, next_e,
1019 rename_map, newivs, newivs_index,
1020 bb_pbb_mapping, level, params_index);
1022 else if (CLAST_STMT_IS_A (stmt, stmt_guard))
1023 next_e = translate_clast_guard (region, context_loop,
1024 (struct clast_guard *) stmt, next_e,
1025 rename_map, newivs, newivs_index,
1026 bb_pbb_mapping, level, params_index);
1028 else if (CLAST_STMT_IS_A (stmt, stmt_block))
1029 next_e = translate_clast (region, context_loop,
1030 ((struct clast_block *) stmt)->body,
1031 next_e, rename_map, newivs, newivs_index,
1032 bb_pbb_mapping, level, params_index);
1036 recompute_all_dominators ();
1039 return translate_clast (region, context_loop, stmt->next, next_e,
1040 rename_map, newivs, newivs_index,
1041 bb_pbb_mapping, level, params_index);
1044 /* Returns the first cloog name used in EXPR. */
1047 find_cloog_iv_in_expr (struct clast_expr *expr)
1049 struct clast_term *term = (struct clast_term *) expr;
1050 struct clast_reduction *red;
1053 if (expr->type == expr_term)
1056 if (expr->type != expr_red)
1059 red = (struct clast_reduction *) expr;
1060 for (i = 0; i < red->n; i++)
1062 const char *res = find_cloog_iv_in_expr (red->elts[i]);
1071 /* Build for USER_STMT a map between the CLAST induction variables and
1072 the corresponding GCC old induction variables. This information is
1073 stored on each GRAPHITE_BB. */
1076 compute_cloog_iv_types_1 (poly_bb_p pbb, struct clast_user_stmt *user_stmt)
1078 gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
1079 struct clast_stmt *t;
1082 for (t = user_stmt->substitutions; t; t = t->next, index++)
1085 struct ivtype_map_elt_s tmp;
1086 struct clast_expr *expr = (struct clast_expr *)
1087 ((struct clast_assignment *)t)->RHS;
1089 /* Create an entry (clast_var, type). */
1090 tmp.cloog_iv = find_cloog_iv_in_expr (expr);
1094 slot = htab_find_slot (GBB_CLOOG_IV_TYPES (gbb), &tmp, INSERT);
1098 tree oldiv = pbb_to_depth_to_oldiv (pbb, index);
1099 tree type = TREE_TYPE (oldiv);
1100 *slot = new_ivtype_map_elt (tmp.cloog_iv, type);
1105 /* Walk the CLAST tree starting from STMT and build for each
1106 clast_user_stmt a map between the CLAST induction variables and the
1107 corresponding GCC old induction variables. This information is
1108 stored on each GRAPHITE_BB. */
1111 compute_cloog_iv_types (struct clast_stmt *stmt)
1116 if (CLAST_STMT_IS_A (stmt, stmt_root))
1119 if (CLAST_STMT_IS_A (stmt, stmt_user))
1121 CloogStatement *cs = ((struct clast_user_stmt *) stmt)->statement;
1122 poly_bb_p pbb = (poly_bb_p) cloog_statement_usr (cs);
1123 gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
1125 if (!GBB_CLOOG_IV_TYPES (gbb))
1126 GBB_CLOOG_IV_TYPES (gbb) = htab_create (10, ivtype_map_elt_info,
1127 eq_ivtype_map_elts, free);
1129 compute_cloog_iv_types_1 (pbb, (struct clast_user_stmt *) stmt);
1133 if (CLAST_STMT_IS_A (stmt, stmt_for))
1135 struct clast_stmt *s = ((struct clast_for *) stmt)->body;
1136 compute_cloog_iv_types (s);
1140 if (CLAST_STMT_IS_A (stmt, stmt_guard))
1142 struct clast_stmt *s = ((struct clast_guard *) stmt)->then;
1143 compute_cloog_iv_types (s);
1147 if (CLAST_STMT_IS_A (stmt, stmt_block))
1149 struct clast_stmt *s = ((struct clast_block *) stmt)->body;
1150 compute_cloog_iv_types (s);
1157 compute_cloog_iv_types (stmt->next);
1160 /* Free the SCATTERING domain list. */
1163 free_scattering (CloogDomainList *scattering)
1167 CloogDomain *dom = cloog_domain (scattering);
1168 CloogDomainList *next = cloog_next_domain (scattering);
1170 cloog_domain_free (dom);
1176 /* Initialize Cloog's parameter names from the names used in GIMPLE.
1177 Initialize Cloog's iterator names, using 'graphite_iterator_%d'
1178 from 0 to scop_nb_loops (scop). */
1181 initialize_cloog_names (scop_p scop, CloogProgram *prog)
1183 sese region = SCOP_REGION (scop);
1185 int nb_iterators = scop_max_loop_depth (scop);
1186 int nb_scattering = cloog_program_nb_scattdims (prog);
1187 int nb_parameters = VEC_length (tree, SESE_PARAMS (region));
1188 char **iterators = XNEWVEC (char *, nb_iterators * 2);
1189 char **scattering = XNEWVEC (char *, nb_scattering);
1190 char **parameters= XNEWVEC (char *, nb_parameters);
1192 cloog_program_set_names (prog, cloog_names_malloc ());
1194 for (i = 0; i < nb_parameters; i++)
1196 tree param = VEC_index (tree, SESE_PARAMS(region), i);
1197 const char *name = get_name (param);
1203 len = strlen (name);
1205 parameters[i] = XNEWVEC (char, len + 1);
1206 snprintf (parameters[i], len, "%s_%d", name, SSA_NAME_VERSION (param));
1209 cloog_names_set_nb_parameters (cloog_program_names (prog), nb_parameters);
1210 cloog_names_set_parameters (cloog_program_names (prog), parameters);
1212 for (i = 0; i < nb_iterators; i++)
1215 iterators[i] = XNEWVEC (char, len);
1216 snprintf (iterators[i], len, "git_%d", i);
1219 cloog_names_set_nb_iterators (cloog_program_names (prog),
1221 cloog_names_set_iterators (cloog_program_names (prog),
1224 for (i = 0; i < nb_scattering; i++)
1227 scattering[i] = XNEWVEC (char, len);
1228 snprintf (scattering[i], len, "scat_%d", i);
1231 cloog_names_set_nb_scattering (cloog_program_names (prog),
1233 cloog_names_set_scattering (cloog_program_names (prog),
1237 /* Build cloog program for SCoP. */
1240 build_cloog_prog (scop_p scop, CloogProgram *prog)
1243 int max_nb_loops = scop_max_loop_depth (scop);
1245 CloogLoop *loop_list = NULL;
1246 CloogBlockList *block_list = NULL;
1247 CloogDomainList *scattering = NULL;
1248 int nbs = 2 * max_nb_loops + 1;
1251 cloog_program_set_context
1252 (prog, new_Cloog_Domain_from_ppl_Pointset_Powerset (SCOP_CONTEXT (scop)));
1253 nbs = unify_scattering_dimensions (scop);
1254 scaldims = (int *) xmalloc (nbs * (sizeof (int)));
1255 cloog_program_set_nb_scattdims (prog, nbs);
1256 initialize_cloog_names (scop, prog);
1258 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
1260 CloogStatement *stmt;
1263 /* Dead code elimination: when the domain of a PBB is empty,
1264 don't generate code for the PBB. */
1265 if (ppl_Pointset_Powerset_C_Polyhedron_is_empty (PBB_DOMAIN (pbb)))
1268 /* Build the new statement and its block. */
1269 stmt = cloog_statement_alloc (pbb_index (pbb));
1270 block = cloog_block_alloc (stmt, 0, NULL, pbb_dim_iter_domain (pbb));
1271 cloog_statement_set_usr (stmt, pbb);
1273 /* Build loop list. */
1275 CloogLoop *new_loop_list = cloog_loop_malloc ();
1276 cloog_loop_set_next (new_loop_list, loop_list);
1277 cloog_loop_set_domain
1279 new_Cloog_Domain_from_ppl_Pointset_Powerset (PBB_DOMAIN (pbb)));
1280 cloog_loop_set_block (new_loop_list, block);
1281 loop_list = new_loop_list;
1284 /* Build block list. */
1286 CloogBlockList *new_block_list = cloog_block_list_malloc ();
1288 cloog_block_list_set_next (new_block_list, block_list);
1289 cloog_block_list_set_block (new_block_list, block);
1290 block_list = new_block_list;
1293 /* Build scattering list. */
1295 /* XXX: Replace with cloog_domain_list_alloc(), when available. */
1296 CloogDomainList *new_scattering
1297 = (CloogDomainList *) xmalloc (sizeof (CloogDomainList));
1298 ppl_Polyhedron_t scat;
1301 scat = PBB_TRANSFORMED_SCATTERING (pbb);
1302 dom = new_Cloog_Domain_from_ppl_Polyhedron (scat);
1304 cloog_set_next_domain (new_scattering, scattering);
1305 cloog_set_domain (new_scattering, dom);
1306 scattering = new_scattering;
1310 cloog_program_set_loop (prog, loop_list);
1311 cloog_program_set_blocklist (prog, block_list);
1313 for (i = 0; i < nbs; i++)
1316 cloog_program_set_scaldims (prog, scaldims);
1318 /* Extract scalar dimensions to simplify the code generation problem. */
1319 cloog_program_extract_scalars (prog, scattering);
1321 /* Apply scattering. */
1322 cloog_program_scatter (prog, scattering);
1323 free_scattering (scattering);
1325 /* Iterators corresponding to scalar dimensions have to be extracted. */
1326 cloog_names_scalarize (cloog_program_names (prog), nbs,
1327 cloog_program_scaldims (prog));
1329 /* Free blocklist. */
1331 CloogBlockList *next = cloog_program_blocklist (prog);
1335 CloogBlockList *toDelete = next;
1336 next = cloog_block_list_next (next);
1337 cloog_block_list_set_next (toDelete, NULL);
1338 cloog_block_list_set_block (toDelete, NULL);
1339 cloog_block_list_free (toDelete);
1341 cloog_program_set_blocklist (prog, NULL);
1345 /* Return the options that will be used in GLOOG. */
1347 static CloogOptions *
1348 set_cloog_options (void)
1350 CloogOptions *options = cloog_options_malloc ();
1352 /* Change cloog output language to C. If we do use FORTRAN instead, cloog
1353 will stop e.g. with "ERROR: unbounded loops not allowed in FORTRAN.", if
1354 we pass an incomplete program to cloog. */
1355 options->language = LANGUAGE_C;
1357 /* Enable complex equality spreading: removes dummy statements
1358 (assignments) in the generated code which repeats the
1359 substitution equations for statements. This is useless for
1363 /* Enable C pretty-printing mode: normalizes the substitution
1364 equations for statements. */
1367 /* Allow cloog to build strides with a stride width different to one.
1368 This example has stride = 4:
1370 for (i = 0; i < 20; i += 4)
1372 options->strides = 1;
1374 /* Disable optimizations and make cloog generate source code closer to the
1375 input. This is useful for debugging, but later we want the optimized
1378 XXX: We can not disable optimizations, as loop blocking is not working
1383 options->l = INT_MAX;
1389 /* Prints STMT to STDERR. */
1392 print_clast_stmt (FILE *file, struct clast_stmt *stmt)
1394 CloogOptions *options = set_cloog_options ();
1396 pprint (file, stmt, 0, options);
1397 cloog_options_free (options);
1400 /* Prints STMT to STDERR. */
1403 debug_clast_stmt (struct clast_stmt *stmt)
1405 print_clast_stmt (stderr, stmt);
1408 /* Translate SCOP to a CLooG program and clast. These two
1409 representations should be freed together: a clast cannot be used
1410 without a program. */
1413 scop_to_clast (scop_p scop)
1415 CloogOptions *options = set_cloog_options ();
1416 cloog_prog_clast pc;
1418 /* Connect new cloog prog generation to graphite. */
1419 pc.prog = cloog_program_malloc ();
1420 build_cloog_prog (scop, pc.prog);
1421 pc.prog = cloog_program_generate (pc.prog, options);
1422 pc.stmt = cloog_clast_create (pc.prog, options);
1424 cloog_options_free (options);
1428 /* Prints to FILE the code generated by CLooG for SCOP. */
1431 print_generated_program (FILE *file, scop_p scop)
1433 CloogOptions *options = set_cloog_options ();
1434 cloog_prog_clast pc = scop_to_clast (scop);
1436 fprintf (file, " (prog: \n");
1437 cloog_program_print (file, pc.prog);
1438 fprintf (file, " )\n");
1440 fprintf (file, " (clast: \n");
1441 pprint (file, pc.stmt, 0, options);
1442 fprintf (file, " )\n");
1444 cloog_options_free (options);
1445 cloog_clast_free (pc.stmt);
1446 cloog_program_free (pc.prog);
1449 /* Prints to STDERR the code generated by CLooG for SCOP. */
1452 debug_generated_program (scop_p scop)
1454 print_generated_program (stderr, scop);
1457 /* Add CLooG names to parameter index. The index is used to translate
1458 back from CLooG names to GCC trees. */
1461 create_params_index (htab_t index_table, CloogProgram *prog) {
1462 CloogNames* names = cloog_program_names (prog);
1463 int nb_parameters = cloog_names_nb_parameters (names);
1464 char **parameters = cloog_names_parameters (names);
1467 for (i = 0; i < nb_parameters; i++)
1468 save_clast_name_index (index_table, parameters[i], i);
1471 /* GIMPLE Loop Generator: generates loops from STMT in GIMPLE form for
1472 the given SCOP. Return true if code generation succeeded.
1473 BB_PBB_MAPPING is a basic_block and it's related poly_bb_p mapping.
1477 gloog (scop_p scop, VEC (scop_p, heap) *scops, htab_t bb_pbb_mapping)
1479 VEC (tree, heap) *newivs = VEC_alloc (tree, heap, 10);
1480 loop_p context_loop;
1481 sese region = SCOP_REGION (scop);
1482 ifsese if_region = NULL;
1483 htab_t rename_map, newivs_index, params_index;
1484 cloog_prog_clast pc;
1487 timevar_push (TV_GRAPHITE_CODE_GEN);
1488 gloog_error = false;
1490 pc = scop_to_clast (scop);
1492 if (dump_file && (dump_flags & TDF_DETAILS))
1494 fprintf (dump_file, "\nCLAST generated by CLooG: \n");
1495 print_clast_stmt (dump_file, pc.stmt);
1496 fprintf (dump_file, "\n");
1499 recompute_all_dominators ();
1502 if_region = move_sese_in_condition (region);
1503 sese_insert_phis_for_liveouts (region,
1504 if_region->region->exit->src,
1505 if_region->false_region->exit,
1506 if_region->true_region->exit);
1507 recompute_all_dominators ();
1510 context_loop = SESE_ENTRY (region)->src->loop_father;
1511 compute_cloog_iv_types (pc.stmt);
1512 rename_map = htab_create (10, rename_map_elt_info, eq_rename_map_elts, free);
1513 newivs_index = htab_create (10, clast_name_index_elt_info,
1514 eq_clast_name_indexes, free);
1515 params_index = htab_create (10, clast_name_index_elt_info,
1516 eq_clast_name_indexes, free);
1518 create_params_index (params_index, pc.prog);
1520 translate_clast (region, context_loop, pc.stmt,
1521 if_region->true_region->entry,
1522 rename_map, &newivs, newivs_index,
1523 bb_pbb_mapping, 1, params_index);
1525 sese_adjust_liveout_phis (region, rename_map,
1526 if_region->region->exit->src,
1527 if_region->false_region->exit,
1528 if_region->true_region->exit);
1530 rename_nb_iterations (rename_map);
1532 for (i = 0; VEC_iterate (scop_p, scops, i, scop); i++)
1533 rename_sese_parameters (rename_map, SCOP_REGION (scop));
1535 recompute_all_dominators ();
1539 set_ifsese_condition (if_region, integer_zero_node);
1541 free (if_region->true_region);
1542 free (if_region->region);
1545 htab_delete (rename_map);
1546 htab_delete (newivs_index);
1547 htab_delete (params_index);
1548 VEC_free (tree, heap, newivs);
1549 cloog_clast_free (pc.stmt);
1550 cloog_program_free (pc.prog);
1551 timevar_pop (TV_GRAPHITE_CODE_GEN);
1553 if (dump_file && (dump_flags & TDF_DETAILS))
1557 int num_no_dependency = 0;
1559 FOR_EACH_LOOP (li, loop, 0)
1560 if (loop->can_be_parallel)
1561 num_no_dependency++;
1563 fprintf (dump_file, "\n%d loops carried no dependency.\n",
1567 return !gloog_error;