OSDN Git Service

0937fc8eb94ab2ec6192cee1ecd1b82b315703e7
[pf3gnuchains/gcc-fork.git] / gcc / graphite-clast-to-gimple.c
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>.
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>.  */
20
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 "tree-dump.h"
32 #include "timevar.h"
33 #include "cfgloop.h"
34 #include "tree-chrec.h"
35 #include "tree-data-ref.h"
36 #include "tree-scalar-evolution.h"
37 #include "tree-pass.h"
38 #include "domwalk.h"
39 #include "value-prof.h"
40 #include "pointer-set.h"
41 #include "gimple.h"
42 #include "langhooks.h"
43 #include "sese.h"
44
45 #ifdef HAVE_cloog
46 #include "cloog/cloog.h"
47 #include "ppl_c.h"
48 #include "graphite-cloog-util.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"
55 #include "graphite-cloog-compat.h"
56
57 /* This flag is set when an error occurred during the translation of
58    CLAST to Gimple.  */
59 static bool gloog_error;
60
61 /* Verifies properties that GRAPHITE should maintain during translation.  */
62
63 static inline void
64 graphite_verify (void)
65 {
66 #ifdef ENABLE_CHECKING
67   verify_loop_structure ();
68   verify_dominators (CDI_DOMINATORS);
69   verify_loop_closed_ssa (true);
70 #endif
71 }
72
73 /* Stores the INDEX in a vector for a given clast NAME.  */
74
75 typedef struct clast_name_index {
76   int index;
77   const char *name;
78 } *clast_name_index_p;
79
80 /* Returns a pointer to a new element of type clast_name_index_p built
81    from NAME and INDEX.  */
82
83 static inline clast_name_index_p
84 new_clast_name_index (const char *name, int index)
85 {
86   clast_name_index_p res = XNEW (struct clast_name_index);
87
88   res->name = name;
89   res->index = index;
90   return res;
91 }
92
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.  */
96
97 static inline int
98 clast_name_to_index (clast_name_p name, htab_t index_table)
99 {
100   struct clast_name_index tmp;
101   PTR *slot;
102
103 #ifdef CLOOG_ORG
104   gcc_assert (name->type == clast_expr_name);
105   tmp.name = ((const struct clast_name*) name)->name;
106 #else
107   tmp.name = name;
108 #endif
109
110   slot = htab_find_slot (index_table, &tmp, NO_INSERT);
111
112   if (slot && *slot)
113     return ((struct clast_name_index *) *slot)->index;
114
115   return -1;
116 }
117
118 /* Records in INDEX_TABLE the INDEX for NAME.  */
119
120 static inline void
121 save_clast_name_index (htab_t index_table, const char *name, int index)
122 {
123   struct clast_name_index tmp;
124   PTR *slot;
125
126   tmp.name = name;
127   slot = htab_find_slot (index_table, &tmp, INSERT);
128
129   if (slot)
130     {
131       if (*slot)
132         free (*slot);
133
134       *slot = new_clast_name_index (name, index);
135     }
136 }
137
138 /* Computes a hash function for database element ELT.  */
139
140 static inline hashval_t
141 clast_name_index_elt_info (const void *elt)
142 {
143   return htab_hash_pointer (((const struct clast_name_index *) elt)->name);
144 }
145
146 /* Compares database elements E1 and E2.  */
147
148 static inline int
149 eq_clast_name_indexes (const void *e1, const void *e2)
150 {
151   const struct clast_name_index *elt1 = (const struct clast_name_index *) e1;
152   const struct clast_name_index *elt2 = (const struct clast_name_index *) e2;
153
154   return (elt1->name == elt2->name);
155 }
156
157 /* For a given scattering dimension, return the new induction variable
158    associated to it.  */
159
160 static inline tree
161 newivs_to_depth_to_newiv (VEC (tree, heap) *newivs, int depth)
162 {
163   return VEC_index (tree, newivs, depth);
164 }
165
166 \f
167
168 /* Returns the tree variable from the name NAME that was given in
169    Cloog representation.  */
170
171 static tree
172 clast_name_to_gcc (clast_name_p name, sese region, VEC (tree, heap) *newivs,
173                    htab_t newivs_index, htab_t params_index)
174 {
175   int index;
176   VEC (tree, heap) *params = SESE_PARAMS (region);
177
178   if (params && params_index)
179     {
180       index = clast_name_to_index (name, params_index);
181
182       if (index >= 0)
183         return VEC_index (tree, params, index);
184     }
185
186   gcc_assert (newivs && newivs_index);
187   index = clast_name_to_index (name, newivs_index);
188   gcc_assert (index >= 0);
189
190   return newivs_to_depth_to_newiv (newivs, index);
191 }
192
193 /* Returns the signed maximal precision type for expressions TYPE1 and TYPE2.  */
194
195 static tree
196 max_signed_precision_type (tree type1, tree type2)
197 {
198   int p1 = TYPE_PRECISION (type1);
199   int p2 = TYPE_PRECISION (type2);
200   int precision;
201   tree type;
202   enum machine_mode mode;
203
204   if (p1 > p2)
205     precision = TYPE_UNSIGNED (type1) ? p1 * 2 : p1;
206   else
207     precision = TYPE_UNSIGNED (type2) ? p2 * 2 : p2;
208
209   if (precision > BITS_PER_WORD)
210     {
211       gloog_error = true;
212       return integer_type_node;
213     }
214
215   mode = smallest_mode_for_size (precision, MODE_INT);
216   precision = GET_MODE_PRECISION (mode);
217   type = build_nonstandard_integer_type (precision, false);
218
219   if (!type)
220     {
221       gloog_error = true;
222       return integer_type_node;
223     }
224
225   return type;
226 }
227
228 /* Returns the maximal precision type for expressions TYPE1 and TYPE2.  */
229
230 static tree
231 max_precision_type (tree type1, tree type2)
232 {
233   if (POINTER_TYPE_P (type1))
234     return type1;
235
236   if (POINTER_TYPE_P (type2))
237     return type2;
238
239   if (!TYPE_UNSIGNED (type1)
240       || !TYPE_UNSIGNED (type2))
241     return max_signed_precision_type (type1, type2);
242
243   return TYPE_PRECISION (type1) > TYPE_PRECISION (type2) ? type1 : type2;
244 }
245
246 static tree
247 clast_to_gcc_expression (tree, struct clast_expr *, sese, VEC (tree, heap) *,
248                          htab_t, htab_t);
249
250 /* Converts a Cloog reduction expression R with reduction operation OP
251    to a GCC expression tree of type TYPE.  */
252
253 static tree
254 clast_to_gcc_expression_red (tree type, enum tree_code op,
255                              struct clast_reduction *r,
256                              sese region, VEC (tree, heap) *newivs,
257                              htab_t newivs_index, htab_t params_index)
258 {
259   int i;
260   tree res = clast_to_gcc_expression (type, r->elts[0], region, newivs,
261                                       newivs_index, params_index);
262   tree operand_type = (op == POINTER_PLUS_EXPR) ? sizetype : type;
263
264   for (i = 1; i < r->n; i++)
265     {
266       tree t = clast_to_gcc_expression (operand_type, r->elts[i], region,
267                                         newivs, newivs_index, params_index);
268       res = fold_build2 (op, type, res, t);
269     }
270
271   return res;
272 }
273
274 /* Converts a Cloog AST expression E back to a GCC expression tree of
275    type TYPE.  */
276
277 static tree
278 clast_to_gcc_expression (tree type, struct clast_expr *e,
279                          sese region, VEC (tree, heap) *newivs,
280                          htab_t newivs_index, htab_t params_index)
281 {
282   switch (e->type)
283     {
284     case clast_expr_term:
285       {
286         struct clast_term *t = (struct clast_term *) e;
287
288         if (t->var)
289           {
290             if (mpz_cmp_si (t->val, 1) == 0)
291               {
292                 tree name = clast_name_to_gcc (t->var, region, newivs,
293                                                newivs_index, params_index);
294
295                 if (POINTER_TYPE_P (TREE_TYPE (name)) != POINTER_TYPE_P (type))
296                   name = fold_convert (sizetype, name);
297
298                 name = fold_convert (type, name);
299                 return name;
300               }
301
302             else if (mpz_cmp_si (t->val, -1) == 0)
303               {
304                 tree name = clast_name_to_gcc (t->var, region, newivs,
305                                                newivs_index, params_index);
306
307                 if (POINTER_TYPE_P (TREE_TYPE (name)) != POINTER_TYPE_P (type))
308                   name = fold_convert (sizetype, name);
309
310                 name = fold_convert (type, name);
311
312                 return fold_build1 (NEGATE_EXPR, type, name);
313               }
314             else
315               {
316                 tree name = clast_name_to_gcc (t->var, region, newivs,
317                                                newivs_index, params_index);
318                 tree cst = gmp_cst_to_tree (type, t->val);
319
320                 if (POINTER_TYPE_P (TREE_TYPE (name)) != POINTER_TYPE_P (type))
321                   name = fold_convert (sizetype, name);
322
323                 name = fold_convert (type, name);
324
325                 if (!POINTER_TYPE_P (type))
326                   return fold_build2 (MULT_EXPR, type, cst, name);
327
328                 gloog_error = true;
329                 return cst;
330               }
331           }
332         else
333           return gmp_cst_to_tree (type, t->val);
334       }
335
336     case clast_expr_red:
337       {
338         struct clast_reduction *r = (struct clast_reduction *) e;
339
340         switch (r->type)
341           {
342           case clast_red_sum:
343             return clast_to_gcc_expression_red
344               (type, POINTER_TYPE_P (type) ? POINTER_PLUS_EXPR : PLUS_EXPR,
345                r, region, newivs, newivs_index, params_index);
346
347           case clast_red_min:
348             return clast_to_gcc_expression_red (type, MIN_EXPR, r, region,
349                                                 newivs, newivs_index,
350                                                 params_index);
351
352           case clast_red_max:
353             return clast_to_gcc_expression_red (type, MAX_EXPR, r, region,
354                                                 newivs, newivs_index,
355                                                 params_index);
356
357           default:
358             gcc_unreachable ();
359           }
360         break;
361       }
362
363     case clast_expr_bin:
364       {
365         struct clast_binary *b = (struct clast_binary *) e;
366         struct clast_expr *lhs = (struct clast_expr *) b->LHS;
367         tree tl = clast_to_gcc_expression (type, lhs, region, newivs,
368                                            newivs_index, params_index);
369         tree tr = gmp_cst_to_tree (type, b->RHS);
370
371         switch (b->type)
372           {
373           case clast_bin_fdiv:
374             return fold_build2 (FLOOR_DIV_EXPR, type, tl, tr);
375
376           case clast_bin_cdiv:
377             return fold_build2 (CEIL_DIV_EXPR, type, tl, tr);
378
379           case clast_bin_div:
380             return fold_build2 (EXACT_DIV_EXPR, type, tl, tr);
381
382           case clast_bin_mod:
383             return fold_build2 (TRUNC_MOD_EXPR, type, tl, tr);
384
385           default:
386             gcc_unreachable ();
387           }
388       }
389
390     default:
391       gcc_unreachable ();
392     }
393
394   return NULL_TREE;
395 }
396
397 /* Return the precision needed to represent the value VAL.  */
398
399 static int
400 precision_for_value (mpz_t val)
401 {
402   mpz_t x, y, two;
403   int precision;
404
405   mpz_init (x);
406   mpz_init (y);
407   mpz_init (two);
408   mpz_set_si (x, 2);
409   mpz_set (y, val);
410   mpz_set_si (two, 2);
411   precision = 1;
412
413   if (mpz_sgn (y) < 0)
414     mpz_neg (y, y);
415
416   while (mpz_cmp (y, x) >= 0)
417     {
418       mpz_mul (x, x, two);
419       precision++;
420     }
421
422   mpz_clear (x);
423   mpz_clear (y);
424   mpz_clear (two);
425
426   return precision;
427 }
428
429 /* Return the precision needed to represent the values between LOW and
430    UP.  */
431
432 static int
433 precision_for_interval (mpz_t low, mpz_t up)
434 {
435   mpz_t diff;
436   int precision;
437
438   gcc_assert (mpz_cmp (low, up) <= 0);
439
440   mpz_init (diff);
441   mpz_sub (diff, up, low);
442   precision = precision_for_value (diff);
443   mpz_clear (diff);
444
445   return precision;
446 }
447
448 /* Return a type that could represent the integer value VAL.  */
449
450 static tree
451 gcc_type_for_interval (mpz_t low, mpz_t up)
452 {
453   bool unsigned_p = true;
454   int precision, prec_up, prec_int;
455   tree type;
456   enum machine_mode mode;
457
458   gcc_assert (mpz_cmp (low, up) <= 0);
459
460   prec_up = precision_for_value (up);
461   prec_int = precision_for_interval (low, up);
462   precision = MAX (prec_up, prec_int);
463
464   if (precision > BITS_PER_WORD)
465     {
466       gloog_error = true;
467       return integer_type_node;
468     }
469
470   if (mpz_sgn (low) <= 0)
471     unsigned_p = false;
472
473   else if (precision < BITS_PER_WORD)
474     {
475       unsigned_p = false;
476       precision++;
477     }
478
479   mode = smallest_mode_for_size (precision, MODE_INT);
480   precision = GET_MODE_PRECISION (mode);
481   type = build_nonstandard_integer_type (precision, unsigned_p);
482
483   if (!type)
484     {
485       gloog_error = true;
486       return integer_type_node;
487     }
488
489   return type;
490 }
491
492 /* Return a type that could represent the integer value VAL, or
493    otherwise return NULL_TREE.  */
494
495 static tree
496 gcc_type_for_value (mpz_t val)
497 {
498   return gcc_type_for_interval (val, val);
499 }
500
501 /* Return the type for the clast_term T used in STMT.  */
502
503 static tree
504 gcc_type_for_clast_term (struct clast_term *t,
505                          sese region, VEC (tree, heap) *newivs,
506                          htab_t newivs_index, htab_t params_index)
507 {
508   gcc_assert (t->expr.type == clast_expr_term);
509
510   if (!t->var)
511     return gcc_type_for_value (t->val);
512
513   return TREE_TYPE (clast_name_to_gcc (t->var, region, newivs,
514                                        newivs_index, params_index));
515 }
516
517 static tree
518 gcc_type_for_clast_expr (struct clast_expr *, sese,
519                          VEC (tree, heap) *, htab_t, htab_t);
520
521 /* Return the type for the clast_reduction R used in STMT.  */
522
523 static tree
524 gcc_type_for_clast_red (struct clast_reduction *r, sese region,
525                         VEC (tree, heap) *newivs,
526                         htab_t newivs_index, htab_t params_index)
527 {
528   int i;
529   tree type = NULL_TREE;
530
531   if (r->n == 1)
532     return gcc_type_for_clast_expr (r->elts[0], region, newivs,
533                                     newivs_index, params_index);
534
535   switch (r->type)
536     {
537     case clast_red_sum:
538     case clast_red_min:
539     case clast_red_max:
540       type = gcc_type_for_clast_expr (r->elts[0], region, newivs,
541                                       newivs_index, params_index);
542       for (i = 1; i < r->n; i++)
543         type = max_precision_type (type, gcc_type_for_clast_expr
544                                    (r->elts[i], region, newivs,
545                                     newivs_index, params_index));
546
547       return type;
548
549     default:
550       break;
551     }
552
553   gcc_unreachable ();
554   return NULL_TREE;
555 }
556
557 /* Return the type for the clast_binary B used in STMT.  */
558
559 static tree
560 gcc_type_for_clast_bin (struct clast_binary *b,
561                         sese region, VEC (tree, heap) *newivs,
562                         htab_t newivs_index, htab_t params_index)
563 {
564   tree l = gcc_type_for_clast_expr ((struct clast_expr *) b->LHS, region,
565                                     newivs, newivs_index, params_index);
566   tree r = gcc_type_for_value (b->RHS);
567   return max_signed_precision_type (l, r);
568 }
569
570 /* Returns the type for the CLAST expression E when used in statement
571    STMT.  */
572
573 static tree
574 gcc_type_for_clast_expr (struct clast_expr *e,
575                          sese region, VEC (tree, heap) *newivs,
576                          htab_t newivs_index, htab_t params_index)
577 {
578   switch (e->type)
579     {
580     case clast_expr_term:
581       return gcc_type_for_clast_term ((struct clast_term *) e, region,
582                                       newivs, newivs_index, params_index);
583
584     case clast_expr_red:
585       return gcc_type_for_clast_red ((struct clast_reduction *) e, region,
586                                      newivs, newivs_index, params_index);
587
588     case clast_expr_bin:
589       return gcc_type_for_clast_bin ((struct clast_binary *) e, region,
590                                      newivs, newivs_index, params_index);
591
592     default:
593       gcc_unreachable ();
594     }
595
596   return NULL_TREE;
597 }
598
599 /* Returns the type for the equation CLEQ.  */
600
601 static tree
602 gcc_type_for_clast_eq (struct clast_equation *cleq,
603                        sese region, VEC (tree, heap) *newivs,
604                        htab_t newivs_index, htab_t params_index)
605 {
606   tree l = gcc_type_for_clast_expr (cleq->LHS, region, newivs,
607                                     newivs_index, params_index);
608   tree r = gcc_type_for_clast_expr (cleq->RHS, region, newivs,
609                                     newivs_index, params_index);
610   return max_precision_type (l, r);
611 }
612
613 /* Translates a clast equation CLEQ to a tree.  */
614
615 static tree
616 graphite_translate_clast_equation (sese region,
617                                    struct clast_equation *cleq,
618                                    VEC (tree, heap) *newivs,
619                                    htab_t newivs_index, htab_t params_index)
620 {
621   enum tree_code comp;
622   tree type = gcc_type_for_clast_eq (cleq, region, newivs, newivs_index,
623                                      params_index);
624   tree lhs = clast_to_gcc_expression (type, cleq->LHS, region, newivs,
625                                       newivs_index, params_index);
626   tree rhs = clast_to_gcc_expression (type, cleq->RHS, region, newivs,
627                                       newivs_index, params_index);
628
629   if (cleq->sign == 0)
630     comp = EQ_EXPR;
631
632   else if (cleq->sign > 0)
633     comp = GE_EXPR;
634
635   else
636     comp = LE_EXPR;
637
638   return fold_build2 (comp, boolean_type_node, lhs, rhs);
639 }
640
641 /* Creates the test for the condition in STMT.  */
642
643 static tree
644 graphite_create_guard_cond_expr (sese region, struct clast_guard *stmt,
645                                  VEC (tree, heap) *newivs,
646                                  htab_t newivs_index, htab_t params_index)
647 {
648   tree cond = NULL;
649   int i;
650
651   for (i = 0; i < stmt->n; i++)
652     {
653       tree eq = graphite_translate_clast_equation (region, &stmt->eq[i],
654                                                    newivs, newivs_index,
655                                                    params_index);
656
657       if (cond)
658         cond = fold_build2 (TRUTH_AND_EXPR, TREE_TYPE (eq), cond, eq);
659       else
660         cond = eq;
661     }
662
663   return cond;
664 }
665
666 /* Creates a new if region corresponding to Cloog's guard.  */
667
668 static edge
669 graphite_create_new_guard (sese region, edge entry_edge,
670                            struct clast_guard *stmt,
671                            VEC (tree, heap) *newivs,
672                            htab_t newivs_index, htab_t params_index)
673 {
674   tree cond_expr = graphite_create_guard_cond_expr (region, stmt, newivs,
675                                                     newivs_index, params_index);
676   edge exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr);
677   return exit_edge;
678 }
679
680 /* Compute the lower bound LOW and upper bound UP for the induction
681    variable at LEVEL for the statement PBB, based on the transformed
682    scattering of PBB: T|I|G|Cst, with T the scattering transform, I
683    the iteration domain, and G the context parameters.  */
684
685 static void
686 compute_bounds_for_level (poly_bb_p pbb, int level, mpz_t low, mpz_t up)
687 {
688   ppl_Pointset_Powerset_C_Polyhedron_t ps;
689   ppl_Linear_Expression_t le;
690
691   combine_context_id_scat (&ps, pbb, false);
692
693   /* Prepare the linear expression corresponding to the level that we
694      want to maximize/minimize.  */
695   {
696     ppl_dimension_type dim = pbb_nb_scattering_transform (pbb)
697       + pbb_dim_iter_domain (pbb) + pbb_nb_params (pbb);
698
699     ppl_new_Linear_Expression_with_dimension (&le, dim);
700     ppl_set_coef (le, 2 * level + 1, 1);
701   }
702
703   ppl_max_for_le_pointset (ps, le, up);
704   ppl_min_for_le_pointset (ps, le, low);
705   ppl_delete_Linear_Expression (le);
706   ppl_delete_Pointset_Powerset_C_Polyhedron (ps);
707 }
708
709 /* Compute the type for the induction variable at LEVEL for the
710    statement PBB, based on the transformed schedule of PBB.  */
711
712 static tree
713 compute_type_for_level (poly_bb_p pbb, int level)
714 {
715   mpz_t low, up;
716   tree type;
717
718   mpz_init (low);
719   mpz_init (up);
720
721   compute_bounds_for_level (pbb, level, low, up);
722   type = gcc_type_for_interval (low, up);
723
724   mpz_clear (low);
725   mpz_clear (up);
726   return type;
727 }
728
729 /* Walks a CLAST and returns the first statement in the body of a
730    loop.  */
731
732 static struct clast_user_stmt *
733 clast_get_body_of_loop (struct clast_stmt *stmt)
734 {
735   if (!stmt
736       || CLAST_STMT_IS_A (stmt, stmt_user))
737     return (struct clast_user_stmt *) stmt;
738
739   if (CLAST_STMT_IS_A (stmt, stmt_for))
740     return clast_get_body_of_loop (((struct clast_for *) stmt)->body);
741
742   if (CLAST_STMT_IS_A (stmt, stmt_guard))
743     return clast_get_body_of_loop (((struct clast_guard *) stmt)->then);
744
745   if (CLAST_STMT_IS_A (stmt, stmt_block))
746     return clast_get_body_of_loop (((struct clast_block *) stmt)->body);
747
748   gcc_unreachable ();
749 }
750
751 /* Returns the type for the induction variable for the loop translated
752    from STMT_FOR.  */
753
754 static tree
755 gcc_type_for_iv_of_clast_loop (struct clast_for *stmt_for, int level,
756                                tree lb_type, tree ub_type)
757 {
758   struct clast_stmt *stmt = (struct clast_stmt *) stmt_for;
759   struct clast_user_stmt *body = clast_get_body_of_loop (stmt);
760   CloogStatement *cs = body->statement;
761   poly_bb_p pbb = (poly_bb_p) cloog_statement_usr (cs);
762
763   return max_signed_precision_type (lb_type, max_precision_type
764                                     (ub_type, compute_type_for_level
765                                      (pbb, level - 1)));
766 }
767
768 /* Creates a new LOOP corresponding to Cloog's STMT.  Inserts an
769    induction variable for the new LOOP.  New LOOP is attached to CFG
770    starting at ENTRY_EDGE.  LOOP is inserted into the loop tree and
771    becomes the child loop of the OUTER_LOOP.  NEWIVS_INDEX binds
772    CLooG's scattering name to the induction variable created for the
773    loop of STMT.  The new induction variable is inserted in the NEWIVS
774    vector.  */
775
776 static struct loop *
777 graphite_create_new_loop (sese region, edge entry_edge,
778                           struct clast_for *stmt,
779                           loop_p outer, VEC (tree, heap) **newivs,
780                           htab_t newivs_index, htab_t params_index, int level)
781 {
782   tree lb_type = gcc_type_for_clast_expr (stmt->LB, region, *newivs,
783                                           newivs_index, params_index);
784   tree ub_type = gcc_type_for_clast_expr (stmt->UB, region, *newivs,
785                                           newivs_index, params_index);
786   tree type = gcc_type_for_iv_of_clast_loop (stmt, level, lb_type, ub_type);
787   tree lb = clast_to_gcc_expression (type, stmt->LB, region, *newivs,
788                                      newivs_index, params_index);
789   tree ub = clast_to_gcc_expression (type, stmt->UB, region, *newivs,
790                                      newivs_index, params_index);
791   tree stride = gmp_cst_to_tree (type, stmt->stride);
792   tree ivvar = create_tmp_var (type, "graphite_IV");
793   tree iv, iv_after_increment;
794   loop_p loop = create_empty_loop_on_edge
795     (entry_edge, lb, stride, ub, ivvar, &iv, &iv_after_increment,
796      outer ? outer : entry_edge->src->loop_father);
797
798   add_referenced_var (ivvar);
799
800   save_clast_name_index (newivs_index, stmt->iterator,
801                          VEC_length (tree, *newivs));
802   VEC_safe_push (tree, heap, *newivs, iv);
803   return loop;
804 }
805
806 /* Inserts in iv_map a tuple (OLD_LOOP->num, NEW_NAME) for the
807    induction variables of the loops around GBB in SESE.  */
808
809 static void
810 build_iv_mapping (VEC (tree, heap) *iv_map, sese region,
811                   VEC (tree, heap) *newivs, htab_t newivs_index,
812                   struct clast_user_stmt *user_stmt,
813                   htab_t params_index)
814 {
815   struct clast_stmt *t;
816   int depth = 0;
817   CloogStatement *cs = user_stmt->statement;
818   poly_bb_p pbb = (poly_bb_p) cloog_statement_usr (cs);
819   gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
820
821   for (t = user_stmt->substitutions; t; t = t->next, depth++)
822     {
823       struct clast_expr *expr = (struct clast_expr *)
824        ((struct clast_assignment *)t)->RHS;
825       tree type = gcc_type_for_clast_expr (expr, region, newivs,
826                                            newivs_index, params_index);
827       tree new_name = clast_to_gcc_expression (type, expr, region, newivs,
828                                                newivs_index, params_index);
829       loop_p old_loop = gbb_loop_at_index (gbb, region, depth);
830
831       VEC_replace (tree, iv_map, old_loop->num, new_name);
832     }
833 }
834
835 /* Construct bb_pbb_def with BB and PBB.  */
836
837 static bb_pbb_def *
838 new_bb_pbb_def (basic_block bb, poly_bb_p pbb)
839 {
840   bb_pbb_def *bb_pbb_p;
841
842   bb_pbb_p = XNEW (bb_pbb_def);
843   bb_pbb_p->bb = bb;
844   bb_pbb_p->pbb = pbb;
845
846   return bb_pbb_p;
847 }
848
849 /* Mark BB with it's relevant PBB via hashing table BB_PBB_MAPPING.  */
850
851 static void
852 mark_bb_with_pbb (poly_bb_p pbb, basic_block bb, htab_t bb_pbb_mapping)
853 {
854   bb_pbb_def tmp;
855   PTR *x;
856
857   tmp.bb = bb;
858   x = htab_find_slot (bb_pbb_mapping, &tmp, INSERT);
859
860   if (x && !*x)
861     *x = new_bb_pbb_def (bb, pbb);
862 }
863
864 /* Find BB's related poly_bb_p in hash table BB_PBB_MAPPING.  */
865
866 static poly_bb_p
867 find_pbb_via_hash (htab_t bb_pbb_mapping, basic_block bb)
868 {
869   bb_pbb_def tmp;
870   PTR *slot;
871
872   tmp.bb = bb;
873   slot = htab_find_slot (bb_pbb_mapping, &tmp, NO_INSERT);
874
875   if (slot && *slot)
876     return ((bb_pbb_def *) *slot)->pbb;
877
878   return NULL;
879 }
880
881 /* Check data dependency in LOOP at scattering level LEVEL.
882    BB_PBB_MAPPING is a basic_block and it's related poly_bb_p
883    mapping.  */
884
885 static bool
886 dependency_in_loop_p (loop_p loop, htab_t bb_pbb_mapping, int level)
887 {
888   unsigned i,j;
889   basic_block *bbs = get_loop_body_in_dom_order (loop);
890
891   for (i = 0; i < loop->num_nodes; i++)
892     {
893       poly_bb_p pbb1 = find_pbb_via_hash (bb_pbb_mapping, bbs[i]);
894
895       if (pbb1 == NULL)
896        continue;
897
898       for (j = 0; j < loop->num_nodes; j++)
899        {
900          poly_bb_p pbb2 = find_pbb_via_hash (bb_pbb_mapping, bbs[j]);
901
902          if (pbb2 == NULL)
903            continue;
904
905          if (dependency_between_pbbs_p (pbb1, pbb2, level))
906            {
907              free (bbs);
908              return true;
909            }
910        }
911     }
912
913   free (bbs);
914
915   return false;
916 }
917
918 /* Translates a clast user statement STMT to gimple.
919
920    - REGION is the sese region we used to generate the scop.
921    - NEXT_E is the edge where new generated code should be attached.
922    - CONTEXT_LOOP is the loop in which the generated code will be placed
923    - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
924    - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
925      the sese region.  */
926 static edge
927 translate_clast_user (sese region, struct clast_user_stmt *stmt, edge next_e,
928                       VEC (tree, heap) **newivs,
929                       htab_t newivs_index, htab_t bb_pbb_mapping,
930                       htab_t params_index)
931 {
932   int i, nb_loops;
933   basic_block new_bb;
934   poly_bb_p pbb = (poly_bb_p) cloog_statement_usr (stmt->statement);
935   gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
936   VEC (tree, heap) *iv_map;
937
938   if (GBB_BB (gbb) == ENTRY_BLOCK_PTR)
939     return next_e;
940
941   nb_loops = number_of_loops ();
942   iv_map = VEC_alloc (tree, heap, nb_loops);
943   for (i = 0; i < nb_loops; i++)
944     VEC_quick_push (tree, iv_map, NULL_TREE);
945
946   build_iv_mapping (iv_map, region, *newivs, newivs_index, stmt, params_index);
947   next_e = copy_bb_and_scalar_dependences (GBB_BB (gbb), region,
948                                            next_e, iv_map);
949   VEC_free (tree, heap, iv_map);
950
951   new_bb = next_e->src;
952   mark_bb_with_pbb (pbb, new_bb, bb_pbb_mapping);
953   update_ssa (TODO_update_ssa);
954
955   return next_e;
956 }
957
958 /* Creates a new if region protecting the loop to be executed, if the execution
959    count is zero (lb > ub).  */
960
961 static edge
962 graphite_create_new_loop_guard (sese region, edge entry_edge,
963                                 struct clast_for *stmt,
964                                 VEC (tree, heap) *newivs,
965                                 htab_t newivs_index, htab_t params_index)
966 {
967   tree cond_expr;
968   edge exit_edge;
969   tree lb_type = gcc_type_for_clast_expr (stmt->LB, region, newivs,
970                                           newivs_index, params_index);
971   tree ub_type = gcc_type_for_clast_expr (stmt->UB, region, newivs,
972                                           newivs_index, params_index);
973   tree type = max_precision_type (lb_type, ub_type);
974   tree lb = clast_to_gcc_expression (type, stmt->LB, region, newivs,
975                                      newivs_index, params_index);
976   tree ub = clast_to_gcc_expression (type, stmt->UB, region, newivs,
977                                      newivs_index, params_index);
978   /* When ub is simply a constant or a parameter, use lb <= ub.  */
979   if (TREE_CODE (ub) == INTEGER_CST || TREE_CODE (ub) == SSA_NAME)
980     cond_expr = fold_build2 (LE_EXPR, boolean_type_node, lb, ub);
981   else
982     {
983       tree one = (POINTER_TYPE_P (type)
984                   ? size_one_node
985                   : fold_convert (type, integer_one_node));
986       /* Adding +1 and using LT_EXPR helps with loop latches that have a
987          loop iteration count of "PARAMETER - 1".  For PARAMETER == 0 this becomes
988          2^k-1 due to integer overflow, and the condition lb <= ub is true,
989          even if we do not want this.  However lb < ub + 1 is false, as
990          expected.  */
991       tree ub_one = fold_build2 (POINTER_TYPE_P (type) ? POINTER_PLUS_EXPR
992                                  : PLUS_EXPR, type, ub, one);
993
994       cond_expr = fold_build2 (LT_EXPR, boolean_type_node, lb, ub_one);
995     }
996
997   exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr);
998
999   return exit_edge;
1000 }
1001
1002 static edge
1003 translate_clast (sese, loop_p, struct clast_stmt *, edge,
1004                  VEC (tree, heap) **, htab_t, htab_t, int, htab_t);
1005
1006 /* Create the loop for a clast for statement.
1007
1008    - REGION is the sese region we used to generate the scop.
1009    - NEXT_E is the edge where new generated code should be attached.
1010    - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
1011    - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
1012      the sese region.  */
1013 static edge
1014 translate_clast_for_loop (sese region, loop_p context_loop,
1015                           struct clast_for *stmt, edge next_e,
1016                           VEC (tree, heap) **newivs,
1017                           htab_t newivs_index, htab_t bb_pbb_mapping,
1018                           int level, htab_t params_index)
1019 {
1020   struct loop *loop = graphite_create_new_loop (region, next_e, stmt,
1021                                                 context_loop, newivs,
1022                                                 newivs_index, params_index,
1023                                                 level);
1024   edge last_e = single_exit (loop);
1025   edge to_body = single_succ_edge (loop->header);
1026   basic_block after = to_body->dest;
1027
1028   /* Create a basic block for loop close phi nodes.  */
1029   last_e = single_succ_edge (split_edge (last_e));
1030
1031   /* Translate the body of the loop.  */
1032   next_e = translate_clast (region, loop, stmt->body, to_body,
1033                             newivs, newivs_index, bb_pbb_mapping, level + 1,
1034                             params_index);
1035   redirect_edge_succ_nodup (next_e, after);
1036   set_immediate_dominator (CDI_DOMINATORS, next_e->dest, next_e->src);
1037
1038   if (flag_loop_parallelize_all
1039       && !dependency_in_loop_p (loop, bb_pbb_mapping,
1040                                 get_scattering_level (level)))
1041     loop->can_be_parallel = true;
1042
1043   return last_e;
1044 }
1045
1046 /* Translates a clast for statement STMT to gimple.  First a guard is created
1047    protecting the loop, if it is executed zero times.  In this guard we create
1048    the real loop structure.
1049
1050    - REGION is the sese region we used to generate the scop.
1051    - NEXT_E is the edge where new generated code should be attached.
1052    - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
1053    - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
1054      the sese region.  */
1055 static edge
1056 translate_clast_for (sese region, loop_p context_loop, struct clast_for *stmt,
1057                      edge next_e, VEC (tree, heap) **newivs,
1058                      htab_t newivs_index, htab_t bb_pbb_mapping, int level,
1059                      htab_t params_index)
1060 {
1061   edge last_e = graphite_create_new_loop_guard (region, next_e, stmt, *newivs,
1062                                                 newivs_index, params_index);
1063   edge true_e = get_true_edge_from_guard_bb (next_e->dest);
1064
1065   translate_clast_for_loop (region, context_loop, stmt, true_e, newivs,
1066                             newivs_index, bb_pbb_mapping, level,
1067                             params_index);
1068   return last_e;
1069 }
1070
1071 /* Translates a clast guard statement STMT to gimple.
1072
1073    - REGION is the sese region we used to generate the scop.
1074    - NEXT_E is the edge where new generated code should be attached.
1075    - CONTEXT_LOOP is the loop in which the generated code will be placed
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_guard (sese region, loop_p context_loop,
1081                        struct clast_guard *stmt, edge next_e,
1082                        VEC (tree, heap) **newivs,
1083                        htab_t newivs_index, htab_t bb_pbb_mapping, int level,
1084                        htab_t params_index)
1085 {
1086   edge last_e = graphite_create_new_guard (region, next_e, stmt, *newivs,
1087                                            newivs_index, params_index);
1088   edge true_e = get_true_edge_from_guard_bb (next_e->dest);
1089
1090   translate_clast (region, context_loop, stmt->then, true_e,
1091                    newivs, newivs_index, bb_pbb_mapping,
1092                    level, params_index);
1093   return last_e;
1094 }
1095
1096 /* Translates a CLAST statement STMT to GCC representation in the
1097    context of a SESE.
1098
1099    - NEXT_E is the edge where new generated code should be attached.
1100    - CONTEXT_LOOP is the loop in which the generated code will be placed
1101    - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.  */
1102 static edge
1103 translate_clast (sese region, loop_p context_loop, struct clast_stmt *stmt,
1104                  edge next_e, VEC (tree, heap) **newivs,
1105                  htab_t newivs_index, htab_t bb_pbb_mapping, int level,
1106                  htab_t params_index)
1107 {
1108   if (!stmt)
1109     return next_e;
1110
1111   if (CLAST_STMT_IS_A (stmt, stmt_root))
1112     ; /* Do nothing.  */
1113
1114   else if (CLAST_STMT_IS_A (stmt, stmt_user))
1115     next_e = translate_clast_user (region, (struct clast_user_stmt *) stmt,
1116                                    next_e, newivs, newivs_index,
1117                                    bb_pbb_mapping, params_index);
1118
1119   else if (CLAST_STMT_IS_A (stmt, stmt_for))
1120     next_e = translate_clast_for (region, context_loop,
1121                                   (struct clast_for *) stmt, next_e,
1122                                   newivs, newivs_index,
1123                                   bb_pbb_mapping, level, params_index);
1124
1125   else if (CLAST_STMT_IS_A (stmt, stmt_guard))
1126     next_e = translate_clast_guard (region, context_loop,
1127                                     (struct clast_guard *) stmt, next_e,
1128                                     newivs, newivs_index,
1129                                     bb_pbb_mapping, level, params_index);
1130
1131   else if (CLAST_STMT_IS_A (stmt, stmt_block))
1132     next_e = translate_clast (region, context_loop,
1133                               ((struct clast_block *) stmt)->body,
1134                               next_e, newivs, newivs_index,
1135                               bb_pbb_mapping, level, params_index);
1136   else
1137     gcc_unreachable();
1138
1139   recompute_all_dominators ();
1140   graphite_verify ();
1141
1142   return translate_clast (region, context_loop, stmt->next, next_e,
1143                           newivs, newivs_index,
1144                           bb_pbb_mapping, level, params_index);
1145 }
1146
1147 /* Free the SCATTERING domain list.  */
1148
1149 static void
1150 free_scattering (CloogScatteringList *scattering)
1151 {
1152   while (scattering)
1153     {
1154       CloogScattering *dom = cloog_scattering (scattering);
1155       CloogScatteringList *next = cloog_next_scattering (scattering);
1156
1157       cloog_scattering_free (dom);
1158       free (scattering);
1159       scattering = next;
1160     }
1161 }
1162
1163 /* Initialize Cloog's parameter names from the names used in GIMPLE.
1164    Initialize Cloog's iterator names, using 'graphite_iterator_%d'
1165    from 0 to scop_nb_loops (scop).  */
1166
1167 static void
1168 initialize_cloog_names (scop_p scop, CloogProgram *prog)
1169 {
1170   sese region = SCOP_REGION (scop);
1171   int i;
1172   int nb_iterators = scop_max_loop_depth (scop);
1173   int nb_scattering = cloog_program_nb_scattdims (prog);
1174   int nb_parameters = VEC_length (tree, SESE_PARAMS (region));
1175   char **iterators = XNEWVEC (char *, nb_iterators * 2);
1176   char **scattering = XNEWVEC (char *, nb_scattering);
1177   char **parameters= XNEWVEC (char *, nb_parameters);
1178
1179   cloog_program_set_names (prog, cloog_names_malloc ());
1180
1181   for (i = 0; i < nb_parameters; i++)
1182     {
1183       tree param = VEC_index (tree, SESE_PARAMS(region), i);
1184       const char *name = get_name (param);
1185       int len;
1186
1187       if (!name)
1188         name = "T";
1189
1190       len = strlen (name);
1191       len += 17;
1192       parameters[i] = XNEWVEC (char, len + 1);
1193       snprintf (parameters[i], len, "%s_%d", name, SSA_NAME_VERSION (param));
1194     }
1195
1196   cloog_names_set_nb_parameters (cloog_program_names (prog), nb_parameters);
1197   cloog_names_set_parameters (cloog_program_names (prog), parameters);
1198
1199   for (i = 0; i < nb_iterators; i++)
1200     {
1201       int len = 4 + 16;
1202       iterators[i] = XNEWVEC (char, len);
1203       snprintf (iterators[i], len, "git_%d", i);
1204     }
1205
1206   cloog_names_set_nb_iterators (cloog_program_names (prog),
1207                                 nb_iterators);
1208   cloog_names_set_iterators (cloog_program_names (prog),
1209                              iterators);
1210
1211   for (i = 0; i < nb_scattering; i++)
1212     {
1213       int len = 5 + 16;
1214       scattering[i] = XNEWVEC (char, len);
1215       snprintf (scattering[i], len, "scat_%d", i);
1216     }
1217
1218   cloog_names_set_nb_scattering (cloog_program_names (prog),
1219                                  nb_scattering);
1220   cloog_names_set_scattering (cloog_program_names (prog),
1221                               scattering);
1222 }
1223
1224 /* Initialize a CLooG input file.  */
1225
1226 static FILE *
1227 init_cloog_input_file (int scop_number)
1228 {
1229   FILE *graphite_out_file;
1230   int len = strlen (dump_base_name);
1231   char *dumpname = XNEWVEC (char, len + 25);
1232   char *s_scop_number = XNEWVEC (char, 15);
1233
1234   memcpy (dumpname, dump_base_name, len + 1);
1235   strip_off_ending (dumpname, len);
1236   sprintf (s_scop_number, ".%d", scop_number);
1237   strcat (dumpname, s_scop_number);
1238   strcat (dumpname, ".cloog");
1239   graphite_out_file = fopen (dumpname, "w+b");
1240
1241   if (graphite_out_file == 0)
1242     fatal_error ("can%'t open %s for writing: %m", dumpname);
1243
1244   free (dumpname);
1245
1246   return graphite_out_file;
1247 }
1248
1249 /* Build cloog program for SCoP.  */
1250
1251 static void
1252 build_cloog_prog (scop_p scop, CloogProgram *prog,
1253                   CloogOptions *options, CloogState *state ATTRIBUTE_UNUSED)
1254 {
1255   int i;
1256   int max_nb_loops = scop_max_loop_depth (scop);
1257   poly_bb_p pbb;
1258   CloogLoop *loop_list = NULL;
1259   CloogBlockList *block_list = NULL;
1260   CloogScatteringList *scattering = NULL;
1261   int nbs = 2 * max_nb_loops + 1;
1262   int *scaldims;
1263
1264   cloog_program_set_context
1265     (prog, new_Cloog_Domain_from_ppl_Pointset_Powerset (SCOP_CONTEXT (scop),
1266       scop_nb_params (scop), state));
1267   nbs = unify_scattering_dimensions (scop);
1268   scaldims = (int *) xmalloc (nbs * (sizeof (int)));
1269   cloog_program_set_nb_scattdims (prog, nbs);
1270   initialize_cloog_names (scop, prog);
1271
1272   FOR_EACH_VEC_ELT (poly_bb_p, SCOP_BBS (scop), i, pbb)
1273     {
1274       CloogStatement *stmt;
1275       CloogBlock *block;
1276       CloogDomain *dom;
1277
1278       /* Dead code elimination: when the domain of a PBB is empty,
1279          don't generate code for the PBB.  */
1280       if (ppl_Pointset_Powerset_C_Polyhedron_is_empty (PBB_DOMAIN (pbb)))
1281         continue;
1282
1283       /* Build the new statement and its block.  */
1284       stmt = cloog_statement_alloc (state, pbb_index (pbb));
1285       dom = new_Cloog_Domain_from_ppl_Pointset_Powerset (PBB_DOMAIN (pbb),
1286                                                          scop_nb_params (scop),
1287                                                          state);
1288       block = cloog_block_alloc (stmt, 0, NULL, pbb_dim_iter_domain (pbb));
1289       cloog_statement_set_usr (stmt, pbb);
1290
1291       /* Build loop list.  */
1292       {
1293         CloogLoop *new_loop_list = cloog_loop_malloc (state);
1294         cloog_loop_set_next (new_loop_list, loop_list);
1295         cloog_loop_set_domain (new_loop_list, dom);
1296         cloog_loop_set_block (new_loop_list, block);
1297         loop_list = new_loop_list;
1298       }
1299
1300       /* Build block list.  */
1301       {
1302         CloogBlockList *new_block_list = cloog_block_list_malloc ();
1303
1304         cloog_block_list_set_next (new_block_list, block_list);
1305         cloog_block_list_set_block (new_block_list, block);
1306         block_list = new_block_list;
1307       }
1308
1309       /* Build scattering list.  */
1310       {
1311         /* XXX: Replace with cloog_domain_list_alloc(), when available.  */
1312         CloogScatteringList *new_scattering
1313           = (CloogScatteringList *) xmalloc (sizeof (CloogScatteringList));
1314         ppl_Polyhedron_t scat;
1315         CloogScattering *dom;
1316
1317         scat = PBB_TRANSFORMED_SCATTERING (pbb);
1318         dom = new_Cloog_Scattering_from_ppl_Polyhedron
1319           (scat, scop_nb_params (scop), pbb_nb_scattering_transform (pbb),
1320            state);
1321
1322         cloog_set_next_scattering (new_scattering, scattering);
1323         cloog_set_scattering (new_scattering, dom);
1324         scattering = new_scattering;
1325       }
1326     }
1327
1328   cloog_program_set_loop (prog, loop_list);
1329   cloog_program_set_blocklist (prog, block_list);
1330
1331   for (i = 0; i < nbs; i++)
1332     scaldims[i] = 0 ;
1333
1334   cloog_program_set_scaldims (prog, scaldims);
1335
1336   /* Extract scalar dimensions to simplify the code generation problem.  */
1337   cloog_program_extract_scalars (prog, scattering, options);
1338
1339   /* Dump a .cloog input file, if requested.  This feature is only
1340      enabled in the Graphite branch.  */
1341   if (0)
1342     {
1343       static size_t file_scop_number = 0;
1344       FILE *cloog_file = init_cloog_input_file (file_scop_number);
1345
1346       cloog_program_dump_cloog (cloog_file, prog, scattering);
1347       ++file_scop_number;
1348     }
1349
1350   /* Apply scattering.  */
1351   cloog_program_scatter (prog, scattering, options);
1352   free_scattering (scattering);
1353
1354   /* Iterators corresponding to scalar dimensions have to be extracted.  */
1355   cloog_names_scalarize (cloog_program_names (prog), nbs,
1356                          cloog_program_scaldims (prog));
1357
1358   /* Free blocklist.  */
1359   {
1360     CloogBlockList *next = cloog_program_blocklist (prog);
1361
1362     while (next)
1363       {
1364         CloogBlockList *toDelete = next;
1365         next = cloog_block_list_next (next);
1366         cloog_block_list_set_next (toDelete, NULL);
1367         cloog_block_list_set_block (toDelete, NULL);
1368         cloog_block_list_free (toDelete);
1369       }
1370     cloog_program_set_blocklist (prog, NULL);
1371   }
1372 }
1373
1374 /* Return the options that will be used in GLOOG.  */
1375
1376 static CloogOptions *
1377 set_cloog_options (CloogState *state ATTRIBUTE_UNUSED)
1378 {
1379   CloogOptions *options = cloog_options_malloc (state);
1380
1381   /* Change cloog output language to C.  If we do use FORTRAN instead, cloog
1382      will stop e.g. with "ERROR: unbounded loops not allowed in FORTRAN.", if
1383      we pass an incomplete program to cloog.  */
1384   options->language = LANGUAGE_C;
1385
1386   /* Enable complex equality spreading: removes dummy statements
1387      (assignments) in the generated code which repeats the
1388      substitution equations for statements.  This is useless for
1389      GLooG.  */
1390   options->esp = 1;
1391
1392 #ifdef CLOOG_ORG
1393   /* Silence CLooG to avoid failing tests due to debug output to stderr.  */
1394   options->quiet = 1;
1395 #else
1396   /* Enable C pretty-printing mode: normalizes the substitution
1397      equations for statements.  */
1398   options->cpp = 1;
1399 #endif
1400
1401   /* Allow cloog to build strides with a stride width different to one.
1402      This example has stride = 4:
1403
1404      for (i = 0; i < 20; i += 4)
1405        A  */
1406   options->strides = 1;
1407
1408   /* Disable optimizations and make cloog generate source code closer to the
1409      input.  This is useful for debugging,  but later we want the optimized
1410      code.
1411
1412      XXX: We can not disable optimizations, as loop blocking is not working
1413      without them.  */
1414   if (0)
1415     {
1416       options->f = -1;
1417       options->l = INT_MAX;
1418     }
1419
1420   return options;
1421 }
1422
1423 /* Prints STMT to STDERR.  */
1424
1425 void
1426 print_clast_stmt (FILE *file, struct clast_stmt *stmt)
1427 {
1428   CloogState *state = cloog_state_malloc ();
1429   CloogOptions *options = set_cloog_options (state);
1430
1431   clast_pprint (file, stmt, 0, options);
1432   cloog_options_free (options);
1433   cloog_state_free (state);
1434 }
1435
1436 /* Prints STMT to STDERR.  */
1437
1438 DEBUG_FUNCTION void
1439 debug_clast_stmt (struct clast_stmt *stmt)
1440 {
1441   print_clast_stmt (stderr, stmt);
1442 }
1443
1444 /* Translate SCOP to a CLooG program and clast.  These two
1445    representations should be freed together: a clast cannot be used
1446    without a program.  */
1447
1448 cloog_prog_clast
1449 scop_to_clast (scop_p scop, CloogState *state)
1450 {
1451   CloogOptions *options = set_cloog_options (state);
1452   cloog_prog_clast pc;
1453
1454   /* Connect new cloog prog generation to graphite.  */
1455   pc.prog = cloog_program_malloc ();
1456   build_cloog_prog (scop, pc.prog, options, state);
1457   pc.prog = cloog_program_generate (pc.prog, options);
1458   pc.stmt = cloog_clast_create (pc.prog, options);
1459
1460   cloog_options_free (options);
1461   return pc;
1462 }
1463
1464 /* Prints to FILE the code generated by CLooG for SCOP.  */
1465
1466 void
1467 print_generated_program (FILE *file, scop_p scop)
1468 {
1469   CloogState *state = cloog_state_malloc ();
1470   CloogOptions *options = set_cloog_options (state);
1471
1472   cloog_prog_clast pc = scop_to_clast (scop, state);
1473
1474   fprintf (file, "       (prog: \n");
1475   cloog_program_print (file, pc.prog);
1476   fprintf (file, "       )\n");
1477
1478   fprintf (file, "       (clast: \n");
1479   clast_pprint (file, pc.stmt, 0, options);
1480   fprintf (file, "       )\n");
1481
1482   cloog_options_free (options);
1483   cloog_clast_free (pc.stmt);
1484   cloog_program_free (pc.prog);
1485 }
1486
1487 /* Prints to STDERR the code generated by CLooG for SCOP.  */
1488
1489 DEBUG_FUNCTION void
1490 debug_generated_program (scop_p scop)
1491 {
1492   print_generated_program (stderr, scop);
1493 }
1494
1495 /* Add CLooG names to parameter index.  The index is used to translate
1496    back from CLooG names to GCC trees.  */
1497
1498 static void
1499 create_params_index (htab_t index_table, CloogProgram *prog) {
1500   CloogNames* names = cloog_program_names (prog);
1501   int nb_parameters = cloog_names_nb_parameters (names);
1502   char **parameters = cloog_names_parameters (names);
1503   int i;
1504
1505   for (i = 0; i < nb_parameters; i++)
1506     save_clast_name_index (index_table, parameters[i], i);
1507 }
1508
1509 /* GIMPLE Loop Generator: generates loops from STMT in GIMPLE form for
1510    the given SCOP.  Return true if code generation succeeded.
1511    BB_PBB_MAPPING is a basic_block and it's related poly_bb_p mapping.
1512 */
1513
1514 bool
1515 gloog (scop_p scop, htab_t bb_pbb_mapping)
1516 {
1517   VEC (tree, heap) *newivs = VEC_alloc (tree, heap, 10);
1518   loop_p context_loop;
1519   sese region = SCOP_REGION (scop);
1520   ifsese if_region = NULL;
1521   htab_t newivs_index, params_index;
1522   cloog_prog_clast pc;
1523   CloogState *state;
1524
1525   state = cloog_state_malloc ();
1526   timevar_push (TV_GRAPHITE_CODE_GEN);
1527   gloog_error = false;
1528
1529   pc = scop_to_clast (scop, state);
1530
1531   if (dump_file && (dump_flags & TDF_DETAILS))
1532     {
1533       fprintf (dump_file, "\nCLAST generated by CLooG: \n");
1534       print_clast_stmt (dump_file, pc.stmt);
1535       fprintf (dump_file, "\n");
1536     }
1537
1538   recompute_all_dominators ();
1539   graphite_verify ();
1540
1541   if_region = move_sese_in_condition (region);
1542   sese_insert_phis_for_liveouts (region,
1543                                  if_region->region->exit->src,
1544                                  if_region->false_region->exit,
1545                                  if_region->true_region->exit);
1546   recompute_all_dominators ();
1547   graphite_verify ();
1548
1549   context_loop = SESE_ENTRY (region)->src->loop_father;
1550   newivs_index = htab_create (10, clast_name_index_elt_info,
1551                               eq_clast_name_indexes, free);
1552   params_index = htab_create (10, clast_name_index_elt_info,
1553                               eq_clast_name_indexes, free);
1554
1555   create_params_index (params_index, pc.prog);
1556
1557   translate_clast (region, context_loop, pc.stmt,
1558                    if_region->true_region->entry,
1559                    &newivs, newivs_index,
1560                    bb_pbb_mapping, 1, params_index);
1561   graphite_verify ();
1562   scev_reset ();
1563   recompute_all_dominators ();
1564   graphite_verify ();
1565
1566   if (gloog_error)
1567     set_ifsese_condition (if_region, integer_zero_node);
1568
1569   free (if_region->true_region);
1570   free (if_region->region);
1571   free (if_region);
1572
1573   htab_delete (newivs_index);
1574   htab_delete (params_index);
1575   VEC_free (tree, heap, newivs);
1576   cloog_clast_free (pc.stmt);
1577   cloog_program_free (pc.prog);
1578   timevar_pop (TV_GRAPHITE_CODE_GEN);
1579
1580   if (dump_file && (dump_flags & TDF_DETAILS))
1581     {
1582       loop_p loop;
1583       loop_iterator li;
1584       int num_no_dependency = 0;
1585
1586       FOR_EACH_LOOP (li, loop, 0)
1587         if (loop->can_be_parallel)
1588           num_no_dependency++;
1589
1590       fprintf (dump_file, "\n%d loops carried no dependency.\n",
1591                num_no_dependency);
1592     }
1593
1594   cloog_state_free (state);
1595
1596   return !gloog_error;
1597 }
1598 #endif