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