OSDN Git Service

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