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