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