OSDN Git Service

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