OSDN Git Service

New flag: -fgraphite-dump-cloog.
[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 }
707
708 /* Compute the type for the induction variable at LEVEL for the
709    statement PBB, based on the transformed schedule of PBB.  */
710
711 static tree
712 compute_type_for_level (poly_bb_p pbb, int level)
713 {
714   mpz_t low, up;
715   tree type;
716
717   mpz_init (low);
718   mpz_init (up);
719
720   compute_bounds_for_level (pbb, level, low, up);
721   type = gcc_type_for_interval (low, up);
722
723   mpz_clear (low);
724   mpz_clear (up);
725   return type;
726 }
727
728 /* Walks a CLAST and returns the first statement in the body of a
729    loop.  */
730
731 static struct clast_user_stmt *
732 clast_get_body_of_loop (struct clast_stmt *stmt)
733 {
734   if (!stmt
735       || CLAST_STMT_IS_A (stmt, stmt_user))
736     return (struct clast_user_stmt *) stmt;
737
738   if (CLAST_STMT_IS_A (stmt, stmt_for))
739     return clast_get_body_of_loop (((struct clast_for *) stmt)->body);
740
741   if (CLAST_STMT_IS_A (stmt, stmt_guard))
742     return clast_get_body_of_loop (((struct clast_guard *) stmt)->then);
743
744   if (CLAST_STMT_IS_A (stmt, stmt_block))
745     return clast_get_body_of_loop (((struct clast_block *) stmt)->body);
746
747   gcc_unreachable ();
748 }
749
750 /* Returns the type for the induction variable for the loop translated
751    from STMT_FOR.  */
752
753 static tree
754 gcc_type_for_iv_of_clast_loop (struct clast_for *stmt_for, int level,
755                                tree lb_type, tree ub_type)
756 {
757   struct clast_stmt *stmt = (struct clast_stmt *) stmt_for;
758   struct clast_user_stmt *body = clast_get_body_of_loop (stmt);
759   CloogStatement *cs = body->statement;
760   poly_bb_p pbb = (poly_bb_p) cloog_statement_usr (cs);
761
762   return max_signed_precision_type (lb_type, max_precision_type
763                                     (ub_type, compute_type_for_level
764                                      (pbb, level - 1)));
765 }
766
767 /* Creates a new LOOP corresponding to Cloog's STMT.  Inserts an
768    induction variable for the new LOOP.  New LOOP is attached to CFG
769    starting at ENTRY_EDGE.  LOOP is inserted into the loop tree and
770    becomes the child loop of the OUTER_LOOP.  NEWIVS_INDEX binds
771    CLooG's scattering name to the induction variable created for the
772    loop of STMT.  The new induction variable is inserted in the NEWIVS
773    vector.  */
774
775 static struct loop *
776 graphite_create_new_loop (sese region, edge entry_edge,
777                           struct clast_for *stmt,
778                           loop_p outer, VEC (tree, heap) **newivs,
779                           htab_t newivs_index, htab_t params_index, int level)
780 {
781   tree lb_type = gcc_type_for_clast_expr (stmt->LB, region, *newivs,
782                                           newivs_index, params_index);
783   tree ub_type = gcc_type_for_clast_expr (stmt->UB, region, *newivs,
784                                           newivs_index, params_index);
785   tree type = gcc_type_for_iv_of_clast_loop (stmt, level, lb_type, ub_type);
786   tree lb = clast_to_gcc_expression (type, stmt->LB, region, *newivs,
787                                      newivs_index, params_index);
788   tree ub = clast_to_gcc_expression (type, stmt->UB, region, *newivs,
789                                      newivs_index, params_index);
790   tree stride = gmp_cst_to_tree (type, stmt->stride);
791   tree ivvar = create_tmp_var (type, "graphite_IV");
792   tree iv, iv_after_increment;
793   loop_p loop = create_empty_loop_on_edge
794     (entry_edge, lb, stride, ub, ivvar, &iv, &iv_after_increment,
795      outer ? outer : entry_edge->src->loop_father);
796
797   add_referenced_var (ivvar);
798
799   save_clast_name_index (newivs_index, stmt->iterator,
800                          VEC_length (tree, *newivs));
801   VEC_safe_push (tree, heap, *newivs, iv);
802   return loop;
803 }
804
805 /* Inserts in iv_map a tuple (OLD_LOOP->num, NEW_NAME) for the
806    induction variables of the loops around GBB in SESE.  */
807
808 static void
809 build_iv_mapping (VEC (tree, heap) *iv_map, sese region,
810                   VEC (tree, heap) *newivs, htab_t newivs_index,
811                   struct clast_user_stmt *user_stmt,
812                   htab_t params_index)
813 {
814   struct clast_stmt *t;
815   int depth = 0;
816   CloogStatement *cs = user_stmt->statement;
817   poly_bb_p pbb = (poly_bb_p) cloog_statement_usr (cs);
818   gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
819
820   for (t = user_stmt->substitutions; t; t = t->next, depth++)
821     {
822       struct clast_expr *expr = (struct clast_expr *)
823        ((struct clast_assignment *)t)->RHS;
824       tree type = gcc_type_for_clast_expr (expr, region, newivs,
825                                            newivs_index, params_index);
826       tree new_name = clast_to_gcc_expression (type, expr, region, newivs,
827                                                newivs_index, params_index);
828       loop_p old_loop = gbb_loop_at_index (gbb, region, depth);
829
830       VEC_replace (tree, iv_map, old_loop->num, new_name);
831     }
832 }
833
834 /* Construct bb_pbb_def with BB and PBB.  */
835
836 static bb_pbb_def *
837 new_bb_pbb_def (basic_block bb, poly_bb_p pbb)
838 {
839   bb_pbb_def *bb_pbb_p;
840
841   bb_pbb_p = XNEW (bb_pbb_def);
842   bb_pbb_p->bb = bb;
843   bb_pbb_p->pbb = pbb;
844
845   return bb_pbb_p;
846 }
847
848 /* Mark BB with it's relevant PBB via hashing table BB_PBB_MAPPING.  */
849
850 static void
851 mark_bb_with_pbb (poly_bb_p pbb, basic_block bb, htab_t bb_pbb_mapping)
852 {
853   bb_pbb_def tmp;
854   PTR *x;
855
856   tmp.bb = bb;
857   x = htab_find_slot (bb_pbb_mapping, &tmp, INSERT);
858
859   if (x && !*x)
860     *x = new_bb_pbb_def (bb, pbb);
861 }
862
863 /* Find BB's related poly_bb_p in hash table BB_PBB_MAPPING.  */
864
865 static poly_bb_p
866 find_pbb_via_hash (htab_t bb_pbb_mapping, basic_block bb)
867 {
868   bb_pbb_def tmp;
869   PTR *slot;
870
871   tmp.bb = bb;
872   slot = htab_find_slot (bb_pbb_mapping, &tmp, NO_INSERT);
873
874   if (slot && *slot)
875     return ((bb_pbb_def *) *slot)->pbb;
876
877   return NULL;
878 }
879
880 /* Check data dependency in LOOP at scattering level LEVEL.
881    BB_PBB_MAPPING is a basic_block and it's related poly_bb_p
882    mapping.  */
883
884 static bool
885 dependency_in_loop_p (loop_p loop, htab_t bb_pbb_mapping, int level)
886 {
887   unsigned i,j;
888   basic_block *bbs = get_loop_body_in_dom_order (loop);
889
890   for (i = 0; i < loop->num_nodes; i++)
891     {
892       poly_bb_p pbb1 = find_pbb_via_hash (bb_pbb_mapping, bbs[i]);
893
894       if (pbb1 == NULL)
895        continue;
896
897       for (j = 0; j < loop->num_nodes; j++)
898        {
899          poly_bb_p pbb2 = find_pbb_via_hash (bb_pbb_mapping, bbs[j]);
900
901          if (pbb2 == NULL)
902            continue;
903
904          if (dependency_between_pbbs_p (pbb1, pbb2, level))
905            {
906              free (bbs);
907              return true;
908            }
909        }
910     }
911
912   free (bbs);
913
914   return false;
915 }
916
917 /* Translates a clast user statement STMT to gimple.
918
919    - REGION is the sese region we used to generate the scop.
920    - NEXT_E is the edge where new generated code should be attached.
921    - CONTEXT_LOOP is the loop in which the generated code will be placed
922    - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
923    - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
924      the sese region.  */
925 static edge
926 translate_clast_user (sese region, struct clast_user_stmt *stmt, edge next_e,
927                       VEC (tree, heap) **newivs,
928                       htab_t newivs_index, htab_t bb_pbb_mapping,
929                       htab_t params_index)
930 {
931   int i, nb_loops;
932   basic_block new_bb;
933   poly_bb_p pbb = (poly_bb_p) cloog_statement_usr (stmt->statement);
934   gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
935   VEC (tree, heap) *iv_map;
936
937   if (GBB_BB (gbb) == ENTRY_BLOCK_PTR)
938     return next_e;
939
940   nb_loops = number_of_loops ();
941   iv_map = VEC_alloc (tree, heap, nb_loops);
942   for (i = 0; i < nb_loops; i++)
943     VEC_quick_push (tree, iv_map, NULL_TREE);
944
945   build_iv_mapping (iv_map, region, *newivs, newivs_index, stmt, params_index);
946   next_e = copy_bb_and_scalar_dependences (GBB_BB (gbb), region,
947                                            next_e, iv_map);
948   VEC_free (tree, heap, iv_map);
949
950   new_bb = next_e->src;
951   mark_bb_with_pbb (pbb, new_bb, bb_pbb_mapping);
952   update_ssa (TODO_update_ssa);
953
954   return next_e;
955 }
956
957 /* Creates a new if region protecting the loop to be executed, if the execution
958    count is zero (lb > ub).  */
959
960 static edge
961 graphite_create_new_loop_guard (sese region, edge entry_edge,
962                                 struct clast_for *stmt,
963                                 VEC (tree, heap) *newivs,
964                                 htab_t newivs_index, htab_t params_index)
965 {
966   tree cond_expr;
967   edge exit_edge;
968   tree lb_type = gcc_type_for_clast_expr (stmt->LB, region, newivs,
969                                           newivs_index, params_index);
970   tree ub_type = gcc_type_for_clast_expr (stmt->UB, region, newivs,
971                                           newivs_index, params_index);
972   tree type = max_precision_type (lb_type, ub_type);
973   tree lb = clast_to_gcc_expression (type, stmt->LB, region, newivs,
974                                      newivs_index, params_index);
975   tree ub = clast_to_gcc_expression (type, stmt->UB, region, newivs,
976                                      newivs_index, params_index);
977   tree one = POINTER_TYPE_P (type) ? size_one_node
978     : fold_convert (type, integer_one_node);
979   /* Adding +1 and using LT_EXPR helps with loop latches that have a
980      loop iteration count of "PARAMETER - 1".  For PARAMETER == 0 this becomes
981      2^{32|64}, and the condition lb <= ub is true, even if we do not want this.
982      However lb < ub + 1 is false, as expected.  */
983   tree ub_one = fold_build2 (POINTER_TYPE_P (type) ? POINTER_PLUS_EXPR
984                              : PLUS_EXPR, type, ub, one);
985
986   /* When ub + 1 wraps around, use lb <= ub.  */
987   if (integer_zerop (ub_one))
988     cond_expr = fold_build2 (LE_EXPR, boolean_type_node, lb, ub);
989   else
990     cond_expr = fold_build2 (LT_EXPR, boolean_type_node, lb, ub_one);
991
992   exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr);
993
994   return exit_edge;
995 }
996
997 static edge
998 translate_clast (sese, loop_p, struct clast_stmt *, edge,
999                  VEC (tree, heap) **, htab_t, htab_t, int, htab_t);
1000
1001 /* Create the loop for a clast for statement.
1002
1003    - REGION is the sese region we used to generate the scop.
1004    - NEXT_E is the edge where new generated code should be attached.
1005    - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
1006    - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
1007      the sese region.  */
1008 static edge
1009 translate_clast_for_loop (sese region, loop_p context_loop,
1010                           struct clast_for *stmt, edge next_e,
1011                           VEC (tree, heap) **newivs,
1012                           htab_t newivs_index, htab_t bb_pbb_mapping,
1013                           int level, htab_t params_index)
1014 {
1015   struct loop *loop = graphite_create_new_loop (region, next_e, stmt,
1016                                                 context_loop, newivs,
1017                                                 newivs_index, params_index,
1018                                                 level);
1019   edge last_e = single_exit (loop);
1020   edge to_body = single_succ_edge (loop->header);
1021   basic_block after = to_body->dest;
1022
1023   /* Create a basic block for loop close phi nodes.  */
1024   last_e = single_succ_edge (split_edge (last_e));
1025
1026   /* Translate the body of the loop.  */
1027   next_e = translate_clast (region, loop, stmt->body, to_body,
1028                             newivs, newivs_index, bb_pbb_mapping, level + 1,
1029                             params_index);
1030   redirect_edge_succ_nodup (next_e, after);
1031   set_immediate_dominator (CDI_DOMINATORS, next_e->dest, next_e->src);
1032
1033   if (flag_loop_parallelize_all
1034       && !dependency_in_loop_p (loop, bb_pbb_mapping,
1035                                 get_scattering_level (level)))
1036     loop->can_be_parallel = true;
1037
1038   return last_e;
1039 }
1040
1041 /* Translates a clast for statement STMT to gimple.  First a guard is created
1042    protecting the loop, if it is executed zero times.  In this guard we create
1043    the real loop structure.
1044
1045    - REGION is the sese region we used to generate the scop.
1046    - NEXT_E is the edge where new generated code should be attached.
1047    - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
1048    - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
1049      the sese region.  */
1050 static edge
1051 translate_clast_for (sese region, loop_p context_loop, struct clast_for *stmt,
1052                      edge next_e, VEC (tree, heap) **newivs,
1053                      htab_t newivs_index, htab_t bb_pbb_mapping, int level,
1054                      htab_t params_index)
1055 {
1056   edge last_e = graphite_create_new_loop_guard (region, next_e, stmt, *newivs,
1057                                                 newivs_index, params_index);
1058   edge true_e = get_true_edge_from_guard_bb (next_e->dest);
1059
1060   translate_clast_for_loop (region, context_loop, stmt, true_e, newivs,
1061                             newivs_index, bb_pbb_mapping, level,
1062                             params_index);
1063   return last_e;
1064 }
1065
1066 /* Translates a clast guard statement STMT to gimple.
1067
1068    - REGION is the sese region we used to generate the scop.
1069    - NEXT_E is the edge where new generated code should be attached.
1070    - CONTEXT_LOOP is the loop in which the generated code will be placed
1071    - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.
1072    - PARAMS_INDEX connects the cloog parameters with the gimple parameters in
1073      the sese region.  */
1074 static edge
1075 translate_clast_guard (sese region, loop_p context_loop,
1076                        struct clast_guard *stmt, edge next_e,
1077                        VEC (tree, heap) **newivs,
1078                        htab_t newivs_index, htab_t bb_pbb_mapping, int level,
1079                        htab_t params_index)
1080 {
1081   edge last_e = graphite_create_new_guard (region, next_e, stmt, *newivs,
1082                                            newivs_index, params_index);
1083   edge true_e = get_true_edge_from_guard_bb (next_e->dest);
1084
1085   translate_clast (region, context_loop, stmt->then, true_e,
1086                    newivs, newivs_index, bb_pbb_mapping,
1087                    level, params_index);
1088   return last_e;
1089 }
1090
1091 /* Translates a CLAST statement STMT to GCC representation in the
1092    context of a SESE.
1093
1094    - NEXT_E is the edge where new generated code should be attached.
1095    - CONTEXT_LOOP is the loop in which the generated code will be placed
1096    - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping.  */
1097 static edge
1098 translate_clast (sese region, loop_p context_loop, struct clast_stmt *stmt,
1099                  edge next_e, VEC (tree, heap) **newivs,
1100                  htab_t newivs_index, htab_t bb_pbb_mapping, int level,
1101                  htab_t params_index)
1102 {
1103   if (!stmt)
1104     return next_e;
1105
1106   if (CLAST_STMT_IS_A (stmt, stmt_root))
1107     ; /* Do nothing.  */
1108
1109   else if (CLAST_STMT_IS_A (stmt, stmt_user))
1110     next_e = translate_clast_user (region, (struct clast_user_stmt *) stmt,
1111                                    next_e, newivs, newivs_index,
1112                                    bb_pbb_mapping, params_index);
1113
1114   else if (CLAST_STMT_IS_A (stmt, stmt_for))
1115     next_e = translate_clast_for (region, context_loop,
1116                                   (struct clast_for *) stmt, next_e,
1117                                   newivs, newivs_index,
1118                                   bb_pbb_mapping, level, params_index);
1119
1120   else if (CLAST_STMT_IS_A (stmt, stmt_guard))
1121     next_e = translate_clast_guard (region, context_loop,
1122                                     (struct clast_guard *) stmt, next_e,
1123                                     newivs, newivs_index,
1124                                     bb_pbb_mapping, level, params_index);
1125
1126   else if (CLAST_STMT_IS_A (stmt, stmt_block))
1127     next_e = translate_clast (region, context_loop,
1128                               ((struct clast_block *) stmt)->body,
1129                               next_e, newivs, newivs_index,
1130                               bb_pbb_mapping, level, params_index);
1131   else
1132     gcc_unreachable();
1133
1134   recompute_all_dominators ();
1135   graphite_verify ();
1136
1137   return translate_clast (region, context_loop, stmt->next, next_e,
1138                           newivs, newivs_index,
1139                           bb_pbb_mapping, level, params_index);
1140 }
1141
1142 /* Free the SCATTERING domain list.  */
1143
1144 static void
1145 free_scattering (CloogScatteringList *scattering)
1146 {
1147   while (scattering)
1148     {
1149       CloogScattering *dom = cloog_scattering (scattering);
1150       CloogScatteringList *next = cloog_next_scattering (scattering);
1151
1152       cloog_scattering_free (dom);
1153       free (scattering);
1154       scattering = next;
1155     }
1156 }
1157
1158 /* Initialize Cloog's parameter names from the names used in GIMPLE.
1159    Initialize Cloog's iterator names, using 'graphite_iterator_%d'
1160    from 0 to scop_nb_loops (scop).  */
1161
1162 static void
1163 initialize_cloog_names (scop_p scop, CloogProgram *prog)
1164 {
1165   sese region = SCOP_REGION (scop);
1166   int i;
1167   int nb_iterators = scop_max_loop_depth (scop);
1168   int nb_scattering = cloog_program_nb_scattdims (prog);
1169   int nb_parameters = VEC_length (tree, SESE_PARAMS (region));
1170   char **iterators = XNEWVEC (char *, nb_iterators * 2);
1171   char **scattering = XNEWVEC (char *, nb_scattering);
1172   char **parameters= XNEWVEC (char *, nb_parameters);
1173
1174   cloog_program_set_names (prog, cloog_names_malloc ());
1175
1176   for (i = 0; i < nb_parameters; i++)
1177     {
1178       tree param = VEC_index (tree, SESE_PARAMS(region), i);
1179       const char *name = get_name (param);
1180       int len;
1181
1182       if (!name)
1183         name = "T";
1184
1185       len = strlen (name);
1186       len += 17;
1187       parameters[i] = XNEWVEC (char, len + 1);
1188       snprintf (parameters[i], len, "%s_%d", name, SSA_NAME_VERSION (param));
1189     }
1190
1191   cloog_names_set_nb_parameters (cloog_program_names (prog), nb_parameters);
1192   cloog_names_set_parameters (cloog_program_names (prog), parameters);
1193
1194   for (i = 0; i < nb_iterators; i++)
1195     {
1196       int len = 4 + 16;
1197       iterators[i] = XNEWVEC (char, len);
1198       snprintf (iterators[i], len, "git_%d", i);
1199     }
1200
1201   cloog_names_set_nb_iterators (cloog_program_names (prog),
1202                                 nb_iterators);
1203   cloog_names_set_iterators (cloog_program_names (prog),
1204                              iterators);
1205
1206   for (i = 0; i < nb_scattering; i++)
1207     {
1208       int len = 5 + 16;
1209       scattering[i] = XNEWVEC (char, len);
1210       snprintf (scattering[i], len, "scat_%d", i);
1211     }
1212
1213   cloog_names_set_nb_scattering (cloog_program_names (prog),
1214                                  nb_scattering);
1215   cloog_names_set_scattering (cloog_program_names (prog),
1216                               scattering);
1217 }
1218
1219 /* Initialize a CLooG input file.  */
1220
1221 static FILE *
1222 init_cloog_input_file (int scop_number)
1223 {
1224   FILE *graphite_out_file;
1225   int len = strlen (dump_base_name);
1226   char *dumpname = XNEWVEC (char, len + 25);
1227   char *s_scop_number = XNEWVEC (char, 15);
1228
1229   memcpy (dumpname, dump_base_name, len + 1);
1230   strip_off_ending (dumpname, len);
1231   sprintf (s_scop_number, ".%d", scop_number);
1232   strcat (dumpname, s_scop_number);
1233   strcat (dumpname, ".cloog");
1234   graphite_out_file = fopen (dumpname, "w+b");
1235
1236   if (graphite_out_file == 0)
1237     fatal_error ("can%'t open %s for writing: %m", dumpname);
1238
1239   free (dumpname);
1240
1241   return graphite_out_file;
1242 }
1243
1244 /* Build cloog program for SCoP.  */
1245
1246 static void
1247 build_cloog_prog (scop_p scop, CloogProgram *prog,
1248                   CloogOptions *options, CloogState *state ATTRIBUTE_UNUSED)
1249 {
1250   int i;
1251   int max_nb_loops = scop_max_loop_depth (scop);
1252   poly_bb_p pbb;
1253   CloogLoop *loop_list = NULL;
1254   CloogBlockList *block_list = NULL;
1255   CloogScatteringList *scattering = NULL;
1256   int nbs = 2 * max_nb_loops + 1;
1257   int *scaldims;
1258
1259   cloog_program_set_context
1260     (prog, new_Cloog_Domain_from_ppl_Pointset_Powerset (SCOP_CONTEXT (scop),
1261       scop_nb_params (scop), state));
1262   nbs = unify_scattering_dimensions (scop);
1263   scaldims = (int *) xmalloc (nbs * (sizeof (int)));
1264   cloog_program_set_nb_scattdims (prog, nbs);
1265   initialize_cloog_names (scop, prog);
1266
1267   FOR_EACH_VEC_ELT (poly_bb_p, SCOP_BBS (scop), i, pbb)
1268     {
1269       CloogStatement *stmt;
1270       CloogBlock *block;
1271       CloogDomain *dom;
1272
1273       /* Dead code elimination: when the domain of a PBB is empty,
1274          don't generate code for the PBB.  */
1275       if (ppl_Pointset_Powerset_C_Polyhedron_is_empty (PBB_DOMAIN (pbb)))
1276         continue;
1277
1278       /* Build the new statement and its block.  */
1279       stmt = cloog_statement_alloc (state, pbb_index (pbb));
1280       dom = new_Cloog_Domain_from_ppl_Pointset_Powerset (PBB_DOMAIN (pbb),
1281                                                          scop_nb_params (scop),
1282                                                          state);
1283       block = cloog_block_alloc (stmt, 0, NULL, pbb_dim_iter_domain (pbb));
1284       cloog_statement_set_usr (stmt, pbb);
1285
1286       /* Build loop list.  */
1287       {
1288         CloogLoop *new_loop_list = cloog_loop_malloc (state);
1289         cloog_loop_set_next (new_loop_list, loop_list);
1290         cloog_loop_set_domain (new_loop_list, dom);
1291         cloog_loop_set_block (new_loop_list, block);
1292         loop_list = new_loop_list;
1293       }
1294
1295       /* Build block list.  */
1296       {
1297         CloogBlockList *new_block_list = cloog_block_list_malloc ();
1298
1299         cloog_block_list_set_next (new_block_list, block_list);
1300         cloog_block_list_set_block (new_block_list, block);
1301         block_list = new_block_list;
1302       }
1303
1304       /* Build scattering list.  */
1305       {
1306         /* XXX: Replace with cloog_domain_list_alloc(), when available.  */
1307         CloogScatteringList *new_scattering
1308           = (CloogScatteringList *) xmalloc (sizeof (CloogScatteringList));
1309         ppl_Polyhedron_t scat;
1310         CloogScattering *dom;
1311
1312         scat = PBB_TRANSFORMED_SCATTERING (pbb);
1313         dom = new_Cloog_Scattering_from_ppl_Polyhedron
1314           (scat, scop_nb_params (scop), pbb_nb_scattering_transform (pbb),
1315            state);
1316
1317         cloog_set_next_scattering (new_scattering, scattering);
1318         cloog_set_scattering (new_scattering, dom);
1319         scattering = new_scattering;
1320       }
1321     }
1322
1323   cloog_program_set_loop (prog, loop_list);
1324   cloog_program_set_blocklist (prog, block_list);
1325
1326   for (i = 0; i < nbs; i++)
1327     scaldims[i] = 0 ;
1328
1329   cloog_program_set_scaldims (prog, scaldims);
1330
1331   /* Extract scalar dimensions to simplify the code generation problem.  */
1332   cloog_program_extract_scalars (prog, scattering, options);
1333
1334   /* Dump a .cloog input file, if requested.  This feature is only
1335      enabled in the Graphite branch.  */
1336   if (0)
1337     {
1338       static size_t file_scop_number = 0;
1339       FILE *cloog_file = init_cloog_input_file (file_scop_number);
1340
1341       cloog_program_dump_cloog (cloog_file, prog, scattering);
1342       ++file_scop_number;
1343     }
1344
1345   /* Apply scattering.  */
1346   cloog_program_scatter (prog, scattering, options);
1347   free_scattering (scattering);
1348
1349   /* Iterators corresponding to scalar dimensions have to be extracted.  */
1350   cloog_names_scalarize (cloog_program_names (prog), nbs,
1351                          cloog_program_scaldims (prog));
1352
1353   /* Free blocklist.  */
1354   {
1355     CloogBlockList *next = cloog_program_blocklist (prog);
1356
1357     while (next)
1358       {
1359         CloogBlockList *toDelete = next;
1360         next = cloog_block_list_next (next);
1361         cloog_block_list_set_next (toDelete, NULL);
1362         cloog_block_list_set_block (toDelete, NULL);
1363         cloog_block_list_free (toDelete);
1364       }
1365     cloog_program_set_blocklist (prog, NULL);
1366   }
1367 }
1368
1369 /* Return the options that will be used in GLOOG.  */
1370
1371 static CloogOptions *
1372 set_cloog_options (CloogState *state ATTRIBUTE_UNUSED)
1373 {
1374   CloogOptions *options = cloog_options_malloc (state);
1375
1376   /* Change cloog output language to C.  If we do use FORTRAN instead, cloog
1377      will stop e.g. with "ERROR: unbounded loops not allowed in FORTRAN.", if
1378      we pass an incomplete program to cloog.  */
1379   options->language = LANGUAGE_C;
1380
1381   /* Enable complex equality spreading: removes dummy statements
1382      (assignments) in the generated code which repeats the
1383      substitution equations for statements.  This is useless for
1384      GLooG.  */
1385   options->esp = 1;
1386
1387 #ifdef CLOOG_ORG
1388   /* Silence CLooG to avoid failing tests due to debug output to stderr.  */
1389   options->quiet = 1;
1390 #else
1391   /* Enable C pretty-printing mode: normalizes the substitution
1392      equations for statements.  */
1393   options->cpp = 1;
1394 #endif
1395
1396   /* Allow cloog to build strides with a stride width different to one.
1397      This example has stride = 4:
1398
1399      for (i = 0; i < 20; i += 4)
1400        A  */
1401   options->strides = 1;
1402
1403   /* Disable optimizations and make cloog generate source code closer to the
1404      input.  This is useful for debugging,  but later we want the optimized
1405      code.
1406
1407      XXX: We can not disable optimizations, as loop blocking is not working
1408      without them.  */
1409   if (0)
1410     {
1411       options->f = -1;
1412       options->l = INT_MAX;
1413     }
1414
1415   return options;
1416 }
1417
1418 /* Prints STMT to STDERR.  */
1419
1420 void
1421 print_clast_stmt (FILE *file, struct clast_stmt *stmt)
1422 {
1423   CloogState *state = cloog_state_malloc ();
1424   CloogOptions *options = set_cloog_options (state);
1425
1426   clast_pprint (file, stmt, 0, options);
1427   cloog_options_free (options);
1428   cloog_state_free (state);
1429 }
1430
1431 /* Prints STMT to STDERR.  */
1432
1433 DEBUG_FUNCTION void
1434 debug_clast_stmt (struct clast_stmt *stmt)
1435 {
1436   print_clast_stmt (stderr, stmt);
1437 }
1438
1439 /* Translate SCOP to a CLooG program and clast.  These two
1440    representations should be freed together: a clast cannot be used
1441    without a program.  */
1442
1443 cloog_prog_clast
1444 scop_to_clast (scop_p scop, CloogState *state)
1445 {
1446   CloogOptions *options = set_cloog_options (state);
1447   cloog_prog_clast pc;
1448
1449   /* Connect new cloog prog generation to graphite.  */
1450   pc.prog = cloog_program_malloc ();
1451   build_cloog_prog (scop, pc.prog, options, state);
1452   pc.prog = cloog_program_generate (pc.prog, options);
1453   pc.stmt = cloog_clast_create (pc.prog, options);
1454
1455   cloog_options_free (options);
1456   return pc;
1457 }
1458
1459 /* Prints to FILE the code generated by CLooG for SCOP.  */
1460
1461 void
1462 print_generated_program (FILE *file, scop_p scop)
1463 {
1464   CloogState *state = cloog_state_malloc ();
1465   CloogOptions *options = set_cloog_options (state);
1466
1467   cloog_prog_clast pc = scop_to_clast (scop, state);
1468
1469   fprintf (file, "       (prog: \n");
1470   cloog_program_print (file, pc.prog);
1471   fprintf (file, "       )\n");
1472
1473   fprintf (file, "       (clast: \n");
1474   clast_pprint (file, pc.stmt, 0, options);
1475   fprintf (file, "       )\n");
1476
1477   cloog_options_free (options);
1478   cloog_clast_free (pc.stmt);
1479   cloog_program_free (pc.prog);
1480 }
1481
1482 /* Prints to STDERR the code generated by CLooG for SCOP.  */
1483
1484 DEBUG_FUNCTION void
1485 debug_generated_program (scop_p scop)
1486 {
1487   print_generated_program (stderr, scop);
1488 }
1489
1490 /* Add CLooG names to parameter index.  The index is used to translate
1491    back from CLooG names to GCC trees.  */
1492
1493 static void
1494 create_params_index (htab_t index_table, CloogProgram *prog) {
1495   CloogNames* names = cloog_program_names (prog);
1496   int nb_parameters = cloog_names_nb_parameters (names);
1497   char **parameters = cloog_names_parameters (names);
1498   int i;
1499
1500   for (i = 0; i < nb_parameters; i++)
1501     save_clast_name_index (index_table, parameters[i], i);
1502 }
1503
1504 /* GIMPLE Loop Generator: generates loops from STMT in GIMPLE form for
1505    the given SCOP.  Return true if code generation succeeded.
1506    BB_PBB_MAPPING is a basic_block and it's related poly_bb_p mapping.
1507 */
1508
1509 bool
1510 gloog (scop_p scop, htab_t bb_pbb_mapping)
1511 {
1512   VEC (tree, heap) *newivs = VEC_alloc (tree, heap, 10);
1513   loop_p context_loop;
1514   sese region = SCOP_REGION (scop);
1515   ifsese if_region = NULL;
1516   htab_t newivs_index, params_index;
1517   cloog_prog_clast pc;
1518   CloogState *state;
1519
1520   state = cloog_state_malloc ();
1521   timevar_push (TV_GRAPHITE_CODE_GEN);
1522   gloog_error = false;
1523
1524   pc = scop_to_clast (scop, state);
1525
1526   if (dump_file && (dump_flags & TDF_DETAILS))
1527     {
1528       fprintf (dump_file, "\nCLAST generated by CLooG: \n");
1529       print_clast_stmt (dump_file, pc.stmt);
1530       fprintf (dump_file, "\n");
1531     }
1532
1533   recompute_all_dominators ();
1534   graphite_verify ();
1535
1536   if_region = move_sese_in_condition (region);
1537   sese_insert_phis_for_liveouts (region,
1538                                  if_region->region->exit->src,
1539                                  if_region->false_region->exit,
1540                                  if_region->true_region->exit);
1541   recompute_all_dominators ();
1542   graphite_verify ();
1543
1544   context_loop = SESE_ENTRY (region)->src->loop_father;
1545   newivs_index = htab_create (10, clast_name_index_elt_info,
1546                               eq_clast_name_indexes, free);
1547   params_index = htab_create (10, clast_name_index_elt_info,
1548                               eq_clast_name_indexes, free);
1549
1550   create_params_index (params_index, pc.prog);
1551
1552   translate_clast (region, context_loop, pc.stmt,
1553                    if_region->true_region->entry,
1554                    &newivs, newivs_index,
1555                    bb_pbb_mapping, 1, params_index);
1556   graphite_verify ();
1557   scev_reset_htab ();
1558   recompute_all_dominators ();
1559   graphite_verify ();
1560
1561   if (gloog_error)
1562     set_ifsese_condition (if_region, integer_zero_node);
1563
1564   free (if_region->true_region);
1565   free (if_region->region);
1566   free (if_region);
1567
1568   htab_delete (newivs_index);
1569   htab_delete (params_index);
1570   VEC_free (tree, heap, newivs);
1571   cloog_clast_free (pc.stmt);
1572   cloog_program_free (pc.prog);
1573   timevar_pop (TV_GRAPHITE_CODE_GEN);
1574
1575   if (dump_file && (dump_flags & TDF_DETAILS))
1576     {
1577       loop_p loop;
1578       loop_iterator li;
1579       int num_no_dependency = 0;
1580
1581       FOR_EACH_LOOP (li, loop, 0)
1582         if (loop->can_be_parallel)
1583           num_no_dependency++;
1584
1585       fprintf (dump_file, "\n%d loops carried no dependency.\n",
1586                num_no_dependency);
1587     }
1588
1589   cloog_state_free (state);
1590
1591   return !gloog_error;
1592 }
1593 #endif