gcc/
[official-gcc.git] / gcc / graphite-clast-to-gimple.c
blob3240c37f8cf43cd6815e92d38c8cb40ddc5f29cd
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)
10 any later version.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "ggc.h"
26 #include "tree.h"
27 #include "rtl.h"
28 #include "basic-block.h"
29 #include "diagnostic.h"
30 #include "tree-flow.h"
31 #include "toplev.h"
32 #include "tree-dump.h"
33 #include "timevar.h"
34 #include "cfgloop.h"
35 #include "tree-chrec.h"
36 #include "tree-data-ref.h"
37 #include "tree-scalar-evolution.h"
38 #include "tree-pass.h"
39 #include "domwalk.h"
40 #include "value-prof.h"
41 #include "pointer-set.h"
42 #include "gimple.h"
43 #include "langhooks.h"
44 #include "sese.h"
46 #ifdef HAVE_cloog
47 #include "cloog/cloog.h"
48 #include "ppl_c.h"
49 #include "graphite-ppl.h"
50 #include "graphite.h"
51 #include "graphite-poly.h"
52 #include "graphite-scop-detection.h"
53 #include "graphite-clast-to-gimple.h"
54 #include "graphite-dependences.h"
56 /* This flag is set when an error occurred during the translation of
57 CLAST to Gimple. */
58 static bool gloog_error;
60 /* Verifies properties that GRAPHITE should maintain during translation. */
62 static inline void
63 graphite_verify (void)
65 #ifdef ENABLE_CHECKING
66 verify_loop_structure ();
67 verify_dominators (CDI_DOMINATORS);
68 verify_dominators (CDI_POST_DOMINATORS);
69 verify_loop_closed_ssa (true);
70 #endif
73 /* Stores the INDEX in a vector for a given clast NAME. */
75 typedef struct clast_name_index {
76 int index;
77 const char *name;
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);
88 res->name = name;
89 res->index = index;
90 return res;
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. */
97 static inline int
98 clast_name_to_index (const char *name, htab_t index_table)
100 struct clast_name_index tmp;
101 PTR *slot;
103 tmp.name = name;
104 slot = htab_find_slot (index_table, &tmp, NO_INSERT);
106 if (slot && *slot)
107 return ((struct clast_name_index *) *slot)->index;
109 return -1;
112 /* Records in INDEX_TABLE the INDEX for NAME. */
114 static inline void
115 save_clast_name_index (htab_t index_table, const char *name, int index)
117 struct clast_name_index tmp;
118 PTR *slot;
120 tmp.name = name;
121 slot = htab_find_slot (index_table, &tmp, INSERT);
123 if (slot)
125 if (*slot)
126 free (*slot);
128 *slot = new_clast_name_index (name, index);
132 /* Print to stderr the element ELT. */
134 static inline void
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. */
142 static inline int
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);
147 return 1;
150 /* Print to stderr all the elements of MAP. */
152 DEBUG_FUNCTION void
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. */
168 static inline int
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. */
181 static inline tree
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
192 associated to it. */
194 static inline tree
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. */
205 static tree
206 clast_name_to_gcc (const char *name, sese region, VEC (tree, heap) *newivs,
207 htab_t newivs_index, htab_t params_index)
209 int index;
210 VEC (tree, heap) *params = SESE_PARAMS (region);
212 if (params && params_index)
214 index = clast_name_to_index (name, params_index);
216 if (index >= 0)
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 signed maximal precision type for expressions TYPE1 and TYPE2. */
229 static tree
230 max_signed_precision_type (tree type1, tree type2)
232 int p1 = TYPE_PRECISION (type1);
233 int p2 = TYPE_PRECISION (type2);
234 int precision;
235 tree type;
237 if (p1 > p2)
238 precision = TYPE_UNSIGNED (type1) ? p1 * 2 : p1;
239 else
240 precision = TYPE_UNSIGNED (type2) ? p2 * 2 : p2;
242 type = lang_hooks.types.type_for_size (precision, false);
244 if (!type)
246 gloog_error = true;
247 return integer_type_node;
249 return type;
252 /* Returns the maximal precision type for expressions TYPE1 and TYPE2. */
254 static tree
255 max_precision_type (tree type1, tree type2)
257 if (POINTER_TYPE_P (type1))
258 return type1;
260 if (POINTER_TYPE_P (type2))
261 return type2;
263 if (!TYPE_UNSIGNED (type1)
264 || !TYPE_UNSIGNED (type2))
265 return max_signed_precision_type (type1, type2);
267 return TYPE_PRECISION (type1) > TYPE_PRECISION (type2) ? type1 : type2;
270 static tree
271 clast_to_gcc_expression (tree, struct clast_expr *, sese, VEC (tree, heap) *,
272 htab_t, htab_t);
274 /* Converts a Cloog reduction expression R with reduction operation OP
275 to a GCC expression tree of type TYPE. */
277 static tree
278 clast_to_gcc_expression_red (tree type, enum tree_code op,
279 struct clast_reduction *r,
280 sese region, VEC (tree, heap) *newivs,
281 htab_t newivs_index, htab_t params_index)
283 int i;
284 tree res = clast_to_gcc_expression (type, r->elts[0], region, newivs,
285 newivs_index, params_index);
286 tree operand_type = (op == POINTER_PLUS_EXPR) ? sizetype : type;
288 for (i = 1; i < r->n; i++)
290 tree t = clast_to_gcc_expression (operand_type, r->elts[i], region,
291 newivs, newivs_index, params_index);
292 res = fold_build2 (op, type, res, t);
295 return res;
298 /* Converts a Cloog AST expression E back to a GCC expression tree of
299 type TYPE. */
301 static tree
302 clast_to_gcc_expression (tree type, struct clast_expr *e,
303 sese region, VEC (tree, heap) *newivs,
304 htab_t newivs_index, htab_t params_index)
306 switch (e->type)
308 case expr_term:
310 struct clast_term *t = (struct clast_term *) e;
312 if (t->var)
314 if (mpz_cmp_si (t->val, 1) == 0)
316 tree name = clast_name_to_gcc (t->var, region, newivs,
317 newivs_index, params_index);
319 if (POINTER_TYPE_P (TREE_TYPE (name)) != POINTER_TYPE_P (type))
320 name = fold_convert (sizetype, name);
322 name = fold_convert (type, name);
323 return name;
326 else if (mpz_cmp_si (t->val, -1) == 0)
328 tree name = clast_name_to_gcc (t->var, region, newivs,
329 newivs_index, params_index);
331 if (POINTER_TYPE_P (TREE_TYPE (name)) != POINTER_TYPE_P (type))
332 name = fold_convert (sizetype, name);
334 name = fold_convert (type, name);
336 return fold_build1 (NEGATE_EXPR, type, name);
338 else
340 tree name = clast_name_to_gcc (t->var, region, newivs,
341 newivs_index, params_index);
342 tree cst = gmp_cst_to_tree (type, t->val);
344 if (POINTER_TYPE_P (TREE_TYPE (name)) != POINTER_TYPE_P (type))
345 name = fold_convert (sizetype, name);
347 name = fold_convert (type, name);
349 if (!POINTER_TYPE_P (type))
350 return fold_build2 (MULT_EXPR, type, cst, name);
352 gloog_error = true;
353 return cst;
356 else
357 return gmp_cst_to_tree (type, t->val);
360 case expr_red:
362 struct clast_reduction *r = (struct clast_reduction *) e;
364 switch (r->type)
366 case clast_red_sum:
367 return clast_to_gcc_expression_red
368 (type, POINTER_TYPE_P (type) ? POINTER_PLUS_EXPR : PLUS_EXPR,
369 r, region, newivs, newivs_index, params_index);
371 case clast_red_min:
372 return clast_to_gcc_expression_red (type, MIN_EXPR, r, region,
373 newivs, newivs_index,
374 params_index);
376 case clast_red_max:
377 return clast_to_gcc_expression_red (type, MAX_EXPR, r, region,
378 newivs, newivs_index,
379 params_index);
381 default:
382 gcc_unreachable ();
384 break;
387 case expr_bin:
389 struct clast_binary *b = (struct clast_binary *) e;
390 struct clast_expr *lhs = (struct clast_expr *) b->LHS;
391 tree tl = clast_to_gcc_expression (type, lhs, region, newivs,
392 newivs_index, params_index);
393 tree tr = gmp_cst_to_tree (type, b->RHS);
395 switch (b->type)
397 case clast_bin_fdiv:
398 return fold_build2 (FLOOR_DIV_EXPR, type, tl, tr);
400 case clast_bin_cdiv:
401 return fold_build2 (CEIL_DIV_EXPR, type, tl, tr);
403 case clast_bin_div:
404 return fold_build2 (EXACT_DIV_EXPR, type, tl, tr);
406 case clast_bin_mod:
407 return fold_build2 (TRUNC_MOD_EXPR, type, tl, tr);
409 default:
410 gcc_unreachable ();
414 default:
415 gcc_unreachable ();
418 return NULL_TREE;
421 /* Return the precision needed to represent the value VAL. */
423 static int
424 precision_for_value (mpz_t val)
426 mpz_t x, y, two;
427 int precision;
429 value_init (x);
430 value_init (y);
431 value_init (two);
432 value_set_si (x, 2);
433 value_assign (y, val);
434 value_set_si (two, 2);
435 precision = 1;
437 if (value_neg_p (y))
438 value_oppose (y, y);
440 while (value_gt (y, x))
442 value_multiply (x, x, two);
443 precision++;
446 value_clear (x);
447 value_clear (y);
448 value_clear (two);
450 return precision;
453 /* Return the precision needed to represent the values between LOW and
454 UP. */
456 static int
457 precision_for_interval (mpz_t low, mpz_t up)
459 mpz_t diff;
460 int precision;
462 gcc_assert (value_le (low, up));
464 value_init (diff);
465 value_subtract (diff, up, low);
466 precision = precision_for_value (diff);
467 value_clear (diff);
469 return precision;
472 /* Return a type that could represent the integer value VAL, or
473 otherwise return NULL_TREE. */
475 static tree
476 gcc_type_for_interval (mpz_t low, mpz_t up, tree old_type)
478 bool unsigned_p = true;
479 int precision, prec_up, prec_int;
480 tree type;
481 enum machine_mode mode;
483 gcc_assert (value_le (low, up));
485 /* Preserve the signedness of the old IV. */
486 if ((old_type && !TYPE_UNSIGNED (old_type))
487 || value_neg_p (low))
488 unsigned_p = false;
490 prec_up = precision_for_value (up);
491 prec_int = precision_for_interval (low, up);
492 precision = prec_up > prec_int ? prec_up : prec_int;
494 if (precision > BITS_PER_WORD)
496 gloog_error = true;
497 return integer_type_node;
500 mode = smallest_mode_for_size (precision, MODE_INT);
501 precision = GET_MODE_PRECISION (mode);
502 type = build_nonstandard_integer_type (precision, unsigned_p);
504 if (!type)
506 gloog_error = true;
507 return integer_type_node;
510 return type;
513 /* Return a type that could represent the integer value VAL, or
514 otherwise return NULL_TREE. */
516 static tree
517 gcc_type_for_value (mpz_t val)
519 return gcc_type_for_interval (val, val, NULL_TREE);
522 /* Return the type for the clast_term T used in STMT. */
524 static tree
525 gcc_type_for_clast_term (struct clast_term *t,
526 sese region, VEC (tree, heap) *newivs,
527 htab_t newivs_index, htab_t params_index)
529 gcc_assert (t->expr.type == expr_term);
531 if (!t->var)
532 return gcc_type_for_value (t->val);
534 return TREE_TYPE (clast_name_to_gcc (t->var, region, newivs,
535 newivs_index, params_index));
538 static tree
539 gcc_type_for_clast_expr (struct clast_expr *, sese,
540 VEC (tree, heap) *, htab_t, htab_t);
542 /* Return the type for the clast_reduction R used in STMT. */
544 static tree
545 gcc_type_for_clast_red (struct clast_reduction *r, sese region,
546 VEC (tree, heap) *newivs,
547 htab_t newivs_index, htab_t params_index)
549 int i;
550 tree type = NULL_TREE;
552 if (r->n == 1)
553 return gcc_type_for_clast_expr (r->elts[0], region, newivs,
554 newivs_index, params_index);
556 switch (r->type)
558 case clast_red_sum:
559 case clast_red_min:
560 case clast_red_max:
561 type = gcc_type_for_clast_expr (r->elts[0], region, newivs,
562 newivs_index, params_index);
563 for (i = 1; i < r->n; i++)
564 type = max_precision_type (type, gcc_type_for_clast_expr
565 (r->elts[i], region, newivs,
566 newivs_index, params_index));
568 return type;
570 default:
571 break;
574 gcc_unreachable ();
575 return NULL_TREE;
578 /* Return the type for the clast_binary B used in STMT. */
580 static tree
581 gcc_type_for_clast_bin (struct clast_binary *b,
582 sese region, VEC (tree, heap) *newivs,
583 htab_t newivs_index, htab_t params_index)
585 tree l = gcc_type_for_clast_expr ((struct clast_expr *) b->LHS, region,
586 newivs, newivs_index, params_index);
587 tree r = gcc_type_for_value (b->RHS);
588 return max_signed_precision_type (l, r);
591 /* Returns the type for the CLAST expression E when used in statement
592 STMT. */
594 static tree
595 gcc_type_for_clast_expr (struct clast_expr *e,
596 sese region, VEC (tree, heap) *newivs,
597 htab_t newivs_index, htab_t params_index)
599 switch (e->type)
601 case expr_term:
602 return gcc_type_for_clast_term ((struct clast_term *) e, region,
603 newivs, newivs_index, params_index);
605 case expr_red:
606 return gcc_type_for_clast_red ((struct clast_reduction *) e, region,
607 newivs, newivs_index, params_index);
609 case expr_bin:
610 return gcc_type_for_clast_bin ((struct clast_binary *) e, region,
611 newivs, newivs_index, params_index);
613 default:
614 gcc_unreachable ();
617 return NULL_TREE;
620 /* Returns the type for the equation CLEQ. */
622 static tree
623 gcc_type_for_clast_eq (struct clast_equation *cleq,
624 sese region, VEC (tree, heap) *newivs,
625 htab_t newivs_index, htab_t params_index)
627 tree l = gcc_type_for_clast_expr (cleq->LHS, region, newivs,
628 newivs_index, params_index);
629 tree r = gcc_type_for_clast_expr (cleq->RHS, region, newivs,
630 newivs_index, params_index);
631 return max_precision_type (l, r);
634 /* Translates a clast equation CLEQ to a tree. */
636 static tree
637 graphite_translate_clast_equation (sese region,
638 struct clast_equation *cleq,
639 VEC (tree, heap) *newivs,
640 htab_t newivs_index, htab_t params_index)
642 enum tree_code comp;
643 tree type = gcc_type_for_clast_eq (cleq, region, newivs, newivs_index,
644 params_index);
645 tree lhs = clast_to_gcc_expression (type, cleq->LHS, region, newivs,
646 newivs_index, params_index);
647 tree rhs = clast_to_gcc_expression (type, cleq->RHS, region, newivs,
648 newivs_index, params_index);
650 if (cleq->sign == 0)
651 comp = EQ_EXPR;
653 else if (cleq->sign > 0)
654 comp = GE_EXPR;
656 else
657 comp = LE_EXPR;
659 return fold_build2 (comp, boolean_type_node, lhs, rhs);
662 /* Creates the test for the condition in STMT. */
664 static tree
665 graphite_create_guard_cond_expr (sese region, struct clast_guard *stmt,
666 VEC (tree, heap) *newivs,
667 htab_t newivs_index, htab_t params_index)
669 tree cond = NULL;
670 int i;
672 for (i = 0; i < stmt->n; i++)
674 tree eq = graphite_translate_clast_equation (region, &stmt->eq[i],
675 newivs, newivs_index,
676 params_index);
678 if (cond)
679 cond = fold_build2 (TRUTH_AND_EXPR, TREE_TYPE (eq), cond, eq);
680 else
681 cond = eq;
684 return cond;
687 /* Creates a new if region corresponding to Cloog's guard. */
689 static edge
690 graphite_create_new_guard (sese region, edge entry_edge,
691 struct clast_guard *stmt,
692 VEC (tree, heap) *newivs,
693 htab_t newivs_index, htab_t params_index)
695 tree cond_expr = graphite_create_guard_cond_expr (region, stmt, newivs,
696 newivs_index, params_index);
697 edge exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr);
698 return exit_edge;
701 /* Compute the lower bound LOW and upper bound UP for the induction
702 variable at LEVEL for the statement PBB, based on the transformed
703 scattering of PBB: T|I|G|Cst, with T the scattering transform, I
704 the iteration domain, and G the context parameters. */
706 static void
707 compute_bounds_for_level (poly_bb_p pbb, int level, mpz_t low, mpz_t up)
709 ppl_Pointset_Powerset_C_Polyhedron_t ps;
710 ppl_Linear_Expression_t le;
712 combine_context_id_scat (&ps, pbb, false);
714 /* Prepare the linear expression corresponding to the level that we
715 want to maximize/minimize. */
717 ppl_dimension_type dim = pbb_nb_scattering_transform (pbb)
718 + pbb_dim_iter_domain (pbb) + pbb_nb_params (pbb);
720 ppl_new_Linear_Expression_with_dimension (&le, dim);
721 ppl_set_coef (le, 2 * level + 1, 1);
724 ppl_max_for_le_pointset (ps, le, up);
725 ppl_min_for_le_pointset (ps, le, low);
728 /* Compute the type for the induction variable at LEVEL for the
729 statement PBB, based on the transformed schedule of PBB. OLD_TYPE
730 is the type of the old induction variable for that loop. */
732 static tree
733 compute_type_for_level_1 (poly_bb_p pbb, int level, tree old_type)
735 mpz_t low, up;
736 tree type;
738 value_init (low);
739 value_init (up);
741 compute_bounds_for_level (pbb, level, low, up);
742 type = gcc_type_for_interval (low, up, old_type);
744 value_clear (low);
745 value_clear (up);
746 return type;
749 /* Compute the type for the induction variable at LEVEL for the
750 statement PBB, based on the transformed schedule of PBB. */
752 static tree
753 compute_type_for_level (poly_bb_p pbb, int level)
755 tree oldiv = pbb_to_depth_to_oldiv (pbb, level);
756 tree type = TREE_TYPE (oldiv);
758 if (type && POINTER_TYPE_P (type))
760 #ifdef ENABLE_CHECKING
761 tree ctype = compute_type_for_level_1 (pbb, level, type);
763 /* In the case of a pointer type, check that after the loop
764 transform, the lower and the upper bounds of the type fit the
765 oldiv pointer type. */
766 gcc_assert (TYPE_PRECISION (type) >= TYPE_PRECISION (ctype)
767 && integer_zerop (lower_bound_in_type (ctype, ctype)));
768 #endif
769 return type;
772 return compute_type_for_level_1 (pbb, level, type);
775 /* Walks a CLAST and returns the first statement in the body of a
776 loop. */
778 static struct clast_user_stmt *
779 clast_get_body_of_loop (struct clast_stmt *stmt)
781 if (!stmt
782 || CLAST_STMT_IS_A (stmt, stmt_user))
783 return (struct clast_user_stmt *) stmt;
785 if (CLAST_STMT_IS_A (stmt, stmt_for))
786 return clast_get_body_of_loop (((struct clast_for *) stmt)->body);
788 if (CLAST_STMT_IS_A (stmt, stmt_guard))
789 return clast_get_body_of_loop (((struct clast_guard *) stmt)->then);
791 if (CLAST_STMT_IS_A (stmt, stmt_block))
792 return clast_get_body_of_loop (((struct clast_block *) stmt)->body);
794 gcc_unreachable ();
797 /* Returns the type for the induction variable for the loop translated
798 from STMT_FOR. */
800 static tree
801 gcc_type_for_iv_of_clast_loop (struct clast_for *stmt_for, int level,
802 tree lb_type, tree ub_type)
804 struct clast_stmt *stmt = (struct clast_stmt *) stmt_for;
805 struct clast_user_stmt *body = clast_get_body_of_loop (stmt);
806 CloogStatement *cs = body->statement;
807 poly_bb_p pbb = (poly_bb_p) cloog_statement_usr (cs);
809 return max_signed_precision_type (lb_type, max_precision_type
810 (ub_type, compute_type_for_level
811 (pbb, level - 1)));
814 /* Creates a new LOOP corresponding to Cloog's STMT. Inserts an
815 induction variable for the new LOOP. New LOOP is attached to CFG
816 starting at ENTRY_EDGE. LOOP is inserted into the loop tree and
817 becomes the child loop of the OUTER_LOOP. NEWIVS_INDEX binds
818 CLooG's scattering name to the induction variable created for the
819 loop of STMT. The new induction variable is inserted in the NEWIVS
820 vector. */
822 static struct loop *
823 graphite_create_new_loop (sese region, edge entry_edge,
824 struct clast_for *stmt,
825 loop_p outer, VEC (tree, heap) **newivs,
826 htab_t newivs_index, htab_t params_index, int level)
828 tree lb_type = gcc_type_for_clast_expr (stmt->LB, region, *newivs,
829 newivs_index, params_index);
830 tree ub_type = gcc_type_for_clast_expr (stmt->UB, region, *newivs,
831 newivs_index, params_index);
832 tree type = gcc_type_for_iv_of_clast_loop (stmt, level, lb_type, ub_type);
833 tree lb = clast_to_gcc_expression (type, stmt->LB, region, *newivs,
834 newivs_index, params_index);
835 tree ub = clast_to_gcc_expression (type, stmt->UB, region, *newivs,
836 newivs_index, params_index);
837 tree stride = gmp_cst_to_tree (type, stmt->stride);
838 tree ivvar = create_tmp_var (type, "graphite_IV");
839 tree iv, iv_after_increment;
840 loop_p loop = create_empty_loop_on_edge
841 (entry_edge, lb, stride, ub, ivvar, &iv, &iv_after_increment,
842 outer ? outer : entry_edge->src->loop_father);
844 add_referenced_var (ivvar);
846 save_clast_name_index (newivs_index, stmt->iterator,
847 VEC_length (tree, *newivs));
848 VEC_safe_push (tree, heap, *newivs, iv);
849 return loop;
852 /* Inserts in MAP a tuple (OLD_NAME, NEW_NAME) for the induction
853 variables of the loops around GBB in SESE. */
855 static void
856 build_iv_mapping (htab_t map, sese region,
857 VEC (tree, heap) *newivs, htab_t newivs_index,
858 struct clast_user_stmt *user_stmt,
859 htab_t params_index)
861 struct clast_stmt *t;
862 int index = 0;
863 CloogStatement *cs = user_stmt->statement;
864 poly_bb_p pbb = (poly_bb_p) cloog_statement_usr (cs);
866 for (t = user_stmt->substitutions; t; t = t->next, index++)
868 struct clast_expr *expr = (struct clast_expr *)
869 ((struct clast_assignment *)t)->RHS;
870 tree type = gcc_type_for_clast_expr (expr, region, newivs,
871 newivs_index, params_index);
872 tree old_name = pbb_to_depth_to_oldiv (pbb, index);
873 tree e = clast_to_gcc_expression (type, expr, region, newivs,
874 newivs_index, params_index);
875 set_rename (map, old_name, e);
879 /* Helper function for htab_traverse. */
881 static int
882 copy_renames (void **slot, void *s)
884 struct rename_map_elt_s *entry = (struct rename_map_elt_s *) *slot;
885 htab_t res = (htab_t) s;
886 tree old_name = entry->old_name;
887 tree expr = entry->expr;
888 struct rename_map_elt_s tmp;
889 PTR *x;
891 tmp.old_name = old_name;
892 x = htab_find_slot (res, &tmp, INSERT);
894 if (x && !*x)
895 *x = new_rename_map_elt (old_name, expr);
897 return 1;
900 /* Construct bb_pbb_def with BB and PBB. */
902 static bb_pbb_def *
903 new_bb_pbb_def (basic_block bb, poly_bb_p pbb)
905 bb_pbb_def *bb_pbb_p;
907 bb_pbb_p = XNEW (bb_pbb_def);
908 bb_pbb_p->bb = bb;
909 bb_pbb_p->pbb = pbb;
911 return bb_pbb_p;
914 /* Mark BB with it's relevant PBB via hashing table BB_PBB_MAPPING. */
916 static void
917 mark_bb_with_pbb (poly_bb_p pbb, basic_block bb, htab_t bb_pbb_mapping)
919 bb_pbb_def tmp;
920 PTR *x;
922 tmp.bb = bb;
923 x = htab_find_slot (bb_pbb_mapping, &tmp, INSERT);
925 if (x && !*x)
926 *x = new_bb_pbb_def (bb, pbb);
929 /* Find BB's related poly_bb_p in hash table BB_PBB_MAPPING. */
931 static poly_bb_p
932 find_pbb_via_hash (htab_t bb_pbb_mapping, basic_block bb)
934 bb_pbb_def tmp;
935 PTR *slot;
937 tmp.bb = bb;
938 slot = htab_find_slot (bb_pbb_mapping, &tmp, NO_INSERT);
940 if (slot && *slot)
941 return ((bb_pbb_def *) *slot)->pbb;
943 return NULL;
946 /* Check data dependency in LOOP at scattering level LEVEL.
947 BB_PBB_MAPPING is a basic_block and it's related poly_bb_p
948 mapping. */
950 static bool
951 dependency_in_loop_p (loop_p loop, htab_t bb_pbb_mapping, int level)
953 unsigned i,j;
954 basic_block *bbs = get_loop_body_in_dom_order (loop);
956 for (i = 0; i < loop->num_nodes; i++)
958 poly_bb_p pbb1 = find_pbb_via_hash (bb_pbb_mapping, bbs[i]);
960 if (pbb1 == NULL)
961 continue;
963 for (j = 0; j < loop->num_nodes; j++)
965 poly_bb_p pbb2 = find_pbb_via_hash (bb_pbb_mapping, bbs[j]);
967 if (pbb2 == NULL)
968 continue;
970 if (dependency_between_pbbs_p (pbb1, pbb2, level))
972 free (bbs);
973 return true;
978 free (bbs);
980 return false;
983 static edge
984 translate_clast (sese, loop_p, struct clast_stmt *, edge, htab_t,
985 VEC (tree, heap) **, htab_t, htab_t, int, htab_t);
987 /* Translates a clast user statement STMT to gimple.
989 - REGION is the sese region we used to generate the scop.
990 - NEXT_E is the edge where new generated code should be attached.
991 - CONTEXT_LOOP is the loop in which the generated code will be placed
992 - RENAME_MAP contains a set of tuples of new names associated to
993 the original variables names.
994 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
995 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
996 the sese region. */
997 static edge
998 translate_clast_user (sese region, struct clast_user_stmt *stmt, edge next_e,
999 htab_t rename_map, VEC (tree, heap) **newivs,
1000 htab_t newivs_index, htab_t bb_pbb_mapping,
1001 htab_t params_index)
1003 gimple_bb_p gbb;
1004 basic_block new_bb;
1005 poly_bb_p pbb = (poly_bb_p) cloog_statement_usr (stmt->statement);
1006 gbb = PBB_BLACK_BOX (pbb);
1008 if (GBB_BB (gbb) == ENTRY_BLOCK_PTR)
1009 return next_e;
1011 build_iv_mapping (rename_map, region, *newivs, newivs_index, stmt,
1012 params_index);
1013 next_e = copy_bb_and_scalar_dependences (GBB_BB (gbb), region,
1014 next_e, rename_map);
1015 new_bb = next_e->src;
1016 mark_bb_with_pbb (pbb, new_bb, bb_pbb_mapping);
1017 update_ssa (TODO_update_ssa);
1019 return next_e;
1022 /* Creates a new if region protecting the loop to be executed, if the execution
1023 count is zero (lb > ub). */
1024 static edge
1025 graphite_create_new_loop_guard (sese region, edge entry_edge,
1026 struct clast_for *stmt,
1027 VEC (tree, heap) *newivs,
1028 htab_t newivs_index, htab_t params_index)
1030 tree cond_expr;
1031 edge exit_edge;
1032 tree lb_type = gcc_type_for_clast_expr (stmt->LB, region, newivs,
1033 newivs_index, params_index);
1034 tree ub_type = gcc_type_for_clast_expr (stmt->UB, region, newivs,
1035 newivs_index, params_index);
1036 tree type = max_precision_type (lb_type, ub_type);
1037 tree lb = clast_to_gcc_expression (type, stmt->LB, region, newivs,
1038 newivs_index, params_index);
1039 tree ub = clast_to_gcc_expression (type, stmt->UB, region, newivs,
1040 newivs_index, params_index);
1041 tree ub_one;
1043 /* Adding +1 and using LT_EXPR helps with loop latches that have a
1044 loop iteration count of "PARAMETER - 1". For PARAMETER == 0 this becomes
1045 2^{32|64}, and the condition lb <= ub is true, even if we do not want this.
1046 However lb < ub + 1 is false, as expected. */
1047 tree one;
1048 mpz_t gmp_one;
1050 mpz_init (gmp_one);
1051 mpz_set_si (gmp_one, 1);
1052 one = gmp_cst_to_tree (type, gmp_one);
1053 mpz_clear (gmp_one);
1055 ub_one = fold_build2 (POINTER_TYPE_P (type) ? POINTER_PLUS_EXPR : PLUS_EXPR,
1056 type, ub, one);
1058 /* When ub + 1 wraps around, use lb <= ub. */
1059 if (integer_zerop (ub_one))
1060 cond_expr = fold_build2 (LE_EXPR, boolean_type_node, lb, ub);
1061 else
1062 cond_expr = fold_build2 (LT_EXPR, boolean_type_node, lb, ub_one);
1064 exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr);
1066 return exit_edge;
1070 /* Create the loop for a clast for statement.
1072 - REGION is the sese region we used to generate the scop.
1073 - NEXT_E is the edge where new generated code should be attached.
1074 - RENAME_MAP contains a set of tuples of new names associated to
1075 the original variables names.
1076 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
1077 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
1078 the sese region. */
1079 static edge
1080 translate_clast_for_loop (sese region, loop_p context_loop,
1081 struct clast_for *stmt, edge next_e,
1082 htab_t rename_map, VEC (tree, heap) **newivs,
1083 htab_t newivs_index, htab_t bb_pbb_mapping,
1084 int level, htab_t params_index)
1086 struct loop *loop = graphite_create_new_loop (region, next_e, stmt,
1087 context_loop, newivs,
1088 newivs_index, params_index,
1089 level);
1090 edge last_e = single_exit (loop);
1091 edge to_body = single_succ_edge (loop->header);
1092 basic_block after = to_body->dest;
1094 /* Create a basic block for loop close phi nodes. */
1095 last_e = single_succ_edge (split_edge (last_e));
1097 /* Translate the body of the loop. */
1098 next_e = translate_clast (region, loop, stmt->body, to_body, rename_map,
1099 newivs, newivs_index, bb_pbb_mapping, level + 1,
1100 params_index);
1101 redirect_edge_succ_nodup (next_e, after);
1102 set_immediate_dominator (CDI_DOMINATORS, next_e->dest, next_e->src);
1104 /* Remove from rename_map all the tuples containing variables
1105 defined in loop's body. */
1106 insert_loop_close_phis (rename_map, loop);
1108 if (flag_loop_parallelize_all
1109 && !dependency_in_loop_p (loop, bb_pbb_mapping,
1110 get_scattering_level (level)))
1111 loop->can_be_parallel = true;
1113 return last_e;
1116 /* Translates a clast for statement STMT to gimple. First a guard is created
1117 protecting the loop, if it is executed zero times. In this guard we create
1118 the real loop structure.
1120 - REGION is the sese region we used to generate the scop.
1121 - NEXT_E is the edge where new generated code should be attached.
1122 - RENAME_MAP contains a set of tuples of new names associated to
1123 the original variables names.
1124 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
1125 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
1126 the sese region. */
1127 static edge
1128 translate_clast_for (sese region, loop_p context_loop, struct clast_for *stmt,
1129 edge next_e, htab_t rename_map, VEC (tree, heap) **newivs,
1130 htab_t newivs_index, htab_t bb_pbb_mapping, int level,
1131 htab_t params_index)
1133 edge last_e = graphite_create_new_loop_guard (region, next_e, stmt, *newivs,
1134 newivs_index, params_index);
1136 edge true_e = get_true_edge_from_guard_bb (next_e->dest);
1137 edge false_e = get_false_edge_from_guard_bb (next_e->dest);
1138 edge exit_true_e = single_succ_edge (true_e->dest);
1139 edge exit_false_e = single_succ_edge (false_e->dest);
1141 htab_t before_guard = htab_create (10, rename_map_elt_info,
1142 eq_rename_map_elts, free);
1143 htab_traverse (rename_map, copy_renames, before_guard);
1145 next_e = translate_clast_for_loop (region, context_loop, stmt, true_e,
1146 rename_map, newivs,
1147 newivs_index, bb_pbb_mapping, level,
1148 params_index);
1150 insert_guard_phis (last_e->src, exit_true_e, exit_false_e,
1151 before_guard, rename_map);
1153 htab_delete (before_guard);
1155 return last_e;
1158 /* Translates a clast guard statement STMT to gimple.
1160 - REGION is the sese region we used to generate the scop.
1161 - NEXT_E is the edge where new generated code should be attached.
1162 - CONTEXT_LOOP is the loop in which the generated code will be placed
1163 - RENAME_MAP contains a set of tuples of new names associated to
1164 the original variables names.
1165 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
1166 - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
1167 the sese region. */
1168 static edge
1169 translate_clast_guard (sese region, loop_p context_loop,
1170 struct clast_guard *stmt, edge next_e,
1171 htab_t rename_map, VEC (tree, heap) **newivs,
1172 htab_t newivs_index, htab_t bb_pbb_mapping, int level,
1173 htab_t params_index)
1175 edge last_e = graphite_create_new_guard (region, next_e, stmt, *newivs,
1176 newivs_index, params_index);
1178 edge true_e = get_true_edge_from_guard_bb (next_e->dest);
1179 edge false_e = get_false_edge_from_guard_bb (next_e->dest);
1180 edge exit_true_e = single_succ_edge (true_e->dest);
1181 edge exit_false_e = single_succ_edge (false_e->dest);
1183 htab_t before_guard = htab_create (10, rename_map_elt_info,
1184 eq_rename_map_elts, free);
1185 htab_traverse (rename_map, copy_renames, before_guard);
1187 next_e = translate_clast (region, context_loop, stmt->then, true_e,
1188 rename_map, newivs, newivs_index, bb_pbb_mapping,
1189 level, params_index);
1191 insert_guard_phis (last_e->src, exit_true_e, exit_false_e,
1192 before_guard, rename_map);
1194 htab_delete (before_guard);
1196 return last_e;
1199 /* Translates a CLAST statement STMT to GCC representation in the
1200 context of a SESE.
1202 - NEXT_E is the edge where new generated code should be attached.
1203 - CONTEXT_LOOP is the loop in which the generated code will be placed
1204 - RENAME_MAP contains a set of tuples of new names associated to
1205 the original variables names.
1206 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping. */
1207 static edge
1208 translate_clast (sese region, loop_p context_loop, struct clast_stmt *stmt,
1209 edge next_e, htab_t rename_map, VEC (tree, heap) **newivs,
1210 htab_t newivs_index, htab_t bb_pbb_mapping, int level,
1211 htab_t params_index)
1213 if (!stmt)
1214 return next_e;
1216 if (CLAST_STMT_IS_A (stmt, stmt_root))
1217 ; /* Do nothing. */
1219 else if (CLAST_STMT_IS_A (stmt, stmt_user))
1220 next_e = translate_clast_user (region, (struct clast_user_stmt *) stmt,
1221 next_e, rename_map, newivs, newivs_index,
1222 bb_pbb_mapping, params_index);
1224 else if (CLAST_STMT_IS_A (stmt, stmt_for))
1225 next_e = translate_clast_for (region, context_loop,
1226 (struct clast_for *) stmt, next_e,
1227 rename_map, newivs, newivs_index,
1228 bb_pbb_mapping, level, params_index);
1230 else if (CLAST_STMT_IS_A (stmt, stmt_guard))
1231 next_e = translate_clast_guard (region, context_loop,
1232 (struct clast_guard *) stmt, next_e,
1233 rename_map, newivs, newivs_index,
1234 bb_pbb_mapping, level, params_index);
1236 else if (CLAST_STMT_IS_A (stmt, stmt_block))
1237 next_e = translate_clast (region, context_loop,
1238 ((struct clast_block *) stmt)->body,
1239 next_e, rename_map, newivs, newivs_index,
1240 bb_pbb_mapping, level, params_index);
1241 else
1242 gcc_unreachable();
1244 recompute_all_dominators ();
1245 graphite_verify ();
1247 return translate_clast (region, context_loop, stmt->next, next_e,
1248 rename_map, newivs, newivs_index,
1249 bb_pbb_mapping, level, params_index);
1252 /* Free the SCATTERING domain list. */
1254 static void
1255 free_scattering (CloogDomainList *scattering)
1257 while (scattering)
1259 CloogDomain *dom = cloog_domain (scattering);
1260 CloogDomainList *next = cloog_next_domain (scattering);
1262 cloog_domain_free (dom);
1263 free (scattering);
1264 scattering = next;
1268 /* Initialize Cloog's parameter names from the names used in GIMPLE.
1269 Initialize Cloog's iterator names, using 'graphite_iterator_%d'
1270 from 0 to scop_nb_loops (scop). */
1272 static void
1273 initialize_cloog_names (scop_p scop, CloogProgram *prog)
1275 sese region = SCOP_REGION (scop);
1276 int i;
1277 int nb_iterators = scop_max_loop_depth (scop);
1278 int nb_scattering = cloog_program_nb_scattdims (prog);
1279 int nb_parameters = VEC_length (tree, SESE_PARAMS (region));
1280 char **iterators = XNEWVEC (char *, nb_iterators * 2);
1281 char **scattering = XNEWVEC (char *, nb_scattering);
1282 char **parameters= XNEWVEC (char *, nb_parameters);
1284 cloog_program_set_names (prog, cloog_names_malloc ());
1286 for (i = 0; i < nb_parameters; i++)
1288 tree param = VEC_index (tree, SESE_PARAMS(region), i);
1289 const char *name = get_name (param);
1290 int len;
1292 if (!name)
1293 name = "T";
1295 len = strlen (name);
1296 len += 17;
1297 parameters[i] = XNEWVEC (char, len + 1);
1298 snprintf (parameters[i], len, "%s_%d", name, SSA_NAME_VERSION (param));
1301 cloog_names_set_nb_parameters (cloog_program_names (prog), nb_parameters);
1302 cloog_names_set_parameters (cloog_program_names (prog), parameters);
1304 for (i = 0; i < nb_iterators; i++)
1306 int len = 4 + 16;
1307 iterators[i] = XNEWVEC (char, len);
1308 snprintf (iterators[i], len, "git_%d", i);
1311 cloog_names_set_nb_iterators (cloog_program_names (prog),
1312 nb_iterators);
1313 cloog_names_set_iterators (cloog_program_names (prog),
1314 iterators);
1316 for (i = 0; i < nb_scattering; i++)
1318 int len = 5 + 16;
1319 scattering[i] = XNEWVEC (char, len);
1320 snprintf (scattering[i], len, "scat_%d", i);
1323 cloog_names_set_nb_scattering (cloog_program_names (prog),
1324 nb_scattering);
1325 cloog_names_set_scattering (cloog_program_names (prog),
1326 scattering);
1329 /* Build cloog program for SCoP. */
1331 static void
1332 build_cloog_prog (scop_p scop, CloogProgram *prog)
1334 int i;
1335 int max_nb_loops = scop_max_loop_depth (scop);
1336 poly_bb_p pbb;
1337 CloogLoop *loop_list = NULL;
1338 CloogBlockList *block_list = NULL;
1339 CloogDomainList *scattering = NULL;
1340 int nbs = 2 * max_nb_loops + 1;
1341 int *scaldims;
1343 cloog_program_set_context
1344 (prog, new_Cloog_Domain_from_ppl_Pointset_Powerset (SCOP_CONTEXT (scop)));
1345 nbs = unify_scattering_dimensions (scop);
1346 scaldims = (int *) xmalloc (nbs * (sizeof (int)));
1347 cloog_program_set_nb_scattdims (prog, nbs);
1348 initialize_cloog_names (scop, prog);
1350 for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
1352 CloogStatement *stmt;
1353 CloogBlock *block;
1355 /* Dead code elimination: when the domain of a PBB is empty,
1356 don't generate code for the PBB. */
1357 if (ppl_Pointset_Powerset_C_Polyhedron_is_empty (PBB_DOMAIN (pbb)))
1358 continue;
1360 /* Build the new statement and its block. */
1361 stmt = cloog_statement_alloc (pbb_index (pbb));
1362 block = cloog_block_alloc (stmt, 0, NULL, pbb_dim_iter_domain (pbb));
1363 cloog_statement_set_usr (stmt, pbb);
1365 /* Build loop list. */
1367 CloogLoop *new_loop_list = cloog_loop_malloc ();
1368 cloog_loop_set_next (new_loop_list, loop_list);
1369 cloog_loop_set_domain
1370 (new_loop_list,
1371 new_Cloog_Domain_from_ppl_Pointset_Powerset (PBB_DOMAIN (pbb)));
1372 cloog_loop_set_block (new_loop_list, block);
1373 loop_list = new_loop_list;
1376 /* Build block list. */
1378 CloogBlockList *new_block_list = cloog_block_list_malloc ();
1380 cloog_block_list_set_next (new_block_list, block_list);
1381 cloog_block_list_set_block (new_block_list, block);
1382 block_list = new_block_list;
1385 /* Build scattering list. */
1387 /* XXX: Replace with cloog_domain_list_alloc(), when available. */
1388 CloogDomainList *new_scattering
1389 = (CloogDomainList *) xmalloc (sizeof (CloogDomainList));
1390 ppl_Polyhedron_t scat;
1391 CloogDomain *dom;
1393 scat = PBB_TRANSFORMED_SCATTERING (pbb);
1394 dom = new_Cloog_Domain_from_ppl_Polyhedron (scat);
1396 cloog_set_next_domain (new_scattering, scattering);
1397 cloog_set_domain (new_scattering, dom);
1398 scattering = new_scattering;
1402 cloog_program_set_loop (prog, loop_list);
1403 cloog_program_set_blocklist (prog, block_list);
1405 for (i = 0; i < nbs; i++)
1406 scaldims[i] = 0 ;
1408 cloog_program_set_scaldims (prog, scaldims);
1410 /* Extract scalar dimensions to simplify the code generation problem. */
1411 cloog_program_extract_scalars (prog, scattering);
1413 /* Apply scattering. */
1414 cloog_program_scatter (prog, scattering);
1415 free_scattering (scattering);
1417 /* Iterators corresponding to scalar dimensions have to be extracted. */
1418 cloog_names_scalarize (cloog_program_names (prog), nbs,
1419 cloog_program_scaldims (prog));
1421 /* Free blocklist. */
1423 CloogBlockList *next = cloog_program_blocklist (prog);
1425 while (next)
1427 CloogBlockList *toDelete = next;
1428 next = cloog_block_list_next (next);
1429 cloog_block_list_set_next (toDelete, NULL);
1430 cloog_block_list_set_block (toDelete, NULL);
1431 cloog_block_list_free (toDelete);
1433 cloog_program_set_blocklist (prog, NULL);
1437 /* Return the options that will be used in GLOOG. */
1439 static CloogOptions *
1440 set_cloog_options (void)
1442 CloogOptions *options = cloog_options_malloc ();
1444 /* Change cloog output language to C. If we do use FORTRAN instead, cloog
1445 will stop e.g. with "ERROR: unbounded loops not allowed in FORTRAN.", if
1446 we pass an incomplete program to cloog. */
1447 options->language = LANGUAGE_C;
1449 /* Enable complex equality spreading: removes dummy statements
1450 (assignments) in the generated code which repeats the
1451 substitution equations for statements. This is useless for
1452 GLooG. */
1453 options->esp = 1;
1455 /* Enable C pretty-printing mode: normalizes the substitution
1456 equations for statements. */
1457 options->cpp = 1;
1459 /* Allow cloog to build strides with a stride width different to one.
1460 This example has stride = 4:
1462 for (i = 0; i < 20; i += 4)
1463 A */
1464 options->strides = 1;
1466 /* Disable optimizations and make cloog generate source code closer to the
1467 input. This is useful for debugging, but later we want the optimized
1468 code.
1470 XXX: We can not disable optimizations, as loop blocking is not working
1471 without them. */
1472 if (0)
1474 options->f = -1;
1475 options->l = INT_MAX;
1478 return options;
1481 /* Prints STMT to STDERR. */
1483 void
1484 print_clast_stmt (FILE *file, struct clast_stmt *stmt)
1486 CloogOptions *options = set_cloog_options ();
1488 pprint (file, stmt, 0, options);
1489 cloog_options_free (options);
1492 /* Prints STMT to STDERR. */
1494 DEBUG_FUNCTION void
1495 debug_clast_stmt (struct clast_stmt *stmt)
1497 print_clast_stmt (stderr, stmt);
1500 /* Translate SCOP to a CLooG program and clast. These two
1501 representations should be freed together: a clast cannot be used
1502 without a program. */
1504 cloog_prog_clast
1505 scop_to_clast (scop_p scop)
1507 CloogOptions *options = set_cloog_options ();
1508 cloog_prog_clast pc;
1510 /* Connect new cloog prog generation to graphite. */
1511 pc.prog = cloog_program_malloc ();
1512 build_cloog_prog (scop, pc.prog);
1513 pc.prog = cloog_program_generate (pc.prog, options);
1514 pc.stmt = cloog_clast_create (pc.prog, options);
1516 cloog_options_free (options);
1517 return pc;
1520 /* Prints to FILE the code generated by CLooG for SCOP. */
1522 void
1523 print_generated_program (FILE *file, scop_p scop)
1525 CloogOptions *options = set_cloog_options ();
1526 cloog_prog_clast pc = scop_to_clast (scop);
1528 fprintf (file, " (prog: \n");
1529 cloog_program_print (file, pc.prog);
1530 fprintf (file, " )\n");
1532 fprintf (file, " (clast: \n");
1533 pprint (file, pc.stmt, 0, options);
1534 fprintf (file, " )\n");
1536 cloog_options_free (options);
1537 cloog_clast_free (pc.stmt);
1538 cloog_program_free (pc.prog);
1541 /* Prints to STDERR the code generated by CLooG for SCOP. */
1543 DEBUG_FUNCTION void
1544 debug_generated_program (scop_p scop)
1546 print_generated_program (stderr, scop);
1549 /* Add CLooG names to parameter index. The index is used to translate
1550 back from CLooG names to GCC trees. */
1552 static void
1553 create_params_index (htab_t index_table, CloogProgram *prog) {
1554 CloogNames* names = cloog_program_names (prog);
1555 int nb_parameters = cloog_names_nb_parameters (names);
1556 char **parameters = cloog_names_parameters (names);
1557 int i;
1559 for (i = 0; i < nb_parameters; i++)
1560 save_clast_name_index (index_table, parameters[i], i);
1563 /* GIMPLE Loop Generator: generates loops from STMT in GIMPLE form for
1564 the given SCOP. Return true if code generation succeeded.
1565 BB_PBB_MAPPING is a basic_block and it's related poly_bb_p mapping.
1568 bool
1569 gloog (scop_p scop, VEC (scop_p, heap) *scops, htab_t bb_pbb_mapping)
1571 VEC (tree, heap) *newivs = VEC_alloc (tree, heap, 10);
1572 loop_p context_loop;
1573 sese region = SCOP_REGION (scop);
1574 ifsese if_region = NULL;
1575 htab_t rename_map, newivs_index, params_index;
1576 cloog_prog_clast pc;
1577 int i;
1579 timevar_push (TV_GRAPHITE_CODE_GEN);
1580 gloog_error = false;
1582 pc = scop_to_clast (scop);
1584 if (dump_file && (dump_flags & TDF_DETAILS))
1586 fprintf (dump_file, "\nCLAST generated by CLooG: \n");
1587 print_clast_stmt (dump_file, pc.stmt);
1588 fprintf (dump_file, "\n");
1591 recompute_all_dominators ();
1592 graphite_verify ();
1594 if_region = move_sese_in_condition (region);
1595 sese_insert_phis_for_liveouts (region,
1596 if_region->region->exit->src,
1597 if_region->false_region->exit,
1598 if_region->true_region->exit);
1599 recompute_all_dominators ();
1600 graphite_verify ();
1602 context_loop = SESE_ENTRY (region)->src->loop_father;
1603 rename_map = htab_create (10, rename_map_elt_info, eq_rename_map_elts, free);
1604 newivs_index = htab_create (10, clast_name_index_elt_info,
1605 eq_clast_name_indexes, free);
1606 params_index = htab_create (10, clast_name_index_elt_info,
1607 eq_clast_name_indexes, free);
1609 create_params_index (params_index, pc.prog);
1611 translate_clast (region, context_loop, pc.stmt,
1612 if_region->true_region->entry,
1613 rename_map, &newivs, newivs_index,
1614 bb_pbb_mapping, 1, params_index);
1615 graphite_verify ();
1616 sese_adjust_liveout_phis (region, rename_map,
1617 if_region->region->exit->src,
1618 if_region->false_region->exit,
1619 if_region->true_region->exit);
1620 scev_reset_htab ();
1621 rename_nb_iterations (rename_map);
1623 for (i = 0; VEC_iterate (scop_p, scops, i, scop); i++)
1624 rename_sese_parameters (rename_map, SCOP_REGION (scop));
1626 recompute_all_dominators ();
1627 graphite_verify ();
1629 if (gloog_error)
1630 set_ifsese_condition (if_region, integer_zero_node);
1632 free (if_region->true_region);
1633 free (if_region->region);
1634 free (if_region);
1636 htab_delete (rename_map);
1637 htab_delete (newivs_index);
1638 htab_delete (params_index);
1639 VEC_free (tree, heap, newivs);
1640 cloog_clast_free (pc.stmt);
1641 cloog_program_free (pc.prog);
1642 timevar_pop (TV_GRAPHITE_CODE_GEN);
1644 if (dump_file && (dump_flags & TDF_DETAILS))
1646 loop_p loop;
1647 loop_iterator li;
1648 int num_no_dependency = 0;
1650 FOR_EACH_LOOP (li, loop, 0)
1651 if (loop->can_be_parallel)
1652 num_no_dependency++;
1654 fprintf (dump_file, "\n%d loops carried no dependency.\n",
1655 num_no_dependency);
1658 return !gloog_error;
1661 #endif