OSDN Git Service

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