lto-cgraph.c (get_alias_symbol): Remove weakref sanity check.
[gcc.git] / gcc / graphite-clast-to-gimple.c
1 /* Translation of CLAST (CLooG AST) to Gimple.
2 Copyright (C) 2009-2013 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
23 #ifdef HAVE_cloog
24 #include <isl/set.h>
25 #include <isl/map.h>
26 #include <isl/union_map.h>
27 #include <isl/list.h>
28 #include <isl/constraint.h>
29 #include <isl/ilp.h>
30 #include <isl/aff.h>
31 #include <cloog/cloog.h>
32 #include <cloog/isl/domain.h>
33 #endif
34
35 #include "system.h"
36 #include "coretypes.h"
37 #include "diagnostic-core.h"
38 #include "tree-flow.h"
39 #include "tree-pass.h"
40 #include "cfgloop.h"
41 #include "tree-chrec.h"
42 #include "tree-data-ref.h"
43 #include "tree-scalar-evolution.h"
44 #include "sese.h"
45
46 #ifdef HAVE_cloog
47 #include "cloog/cloog.h"
48 #include "graphite-poly.h"
49 #include "graphite-clast-to-gimple.h"
50 #include "graphite-htab.h"
51
52 typedef const struct clast_expr *clast_name_p;
53
54 #ifndef CLOOG_LANGUAGE_C
55 #define CLOOG_LANGUAGE_C LANGUAGE_C
56 #endif
57
58
59 /* Converts a GMP constant VAL to a tree and returns it. */
60
61 static tree
62 gmp_cst_to_tree (tree type, mpz_t val)
63 {
64 tree t = type ? type : integer_type_node;
65 mpz_t tmp;
66 double_int di;
67
68 mpz_init (tmp);
69 mpz_set (tmp, val);
70 di = mpz_get_double_int (t, tmp, true);
71 mpz_clear (tmp);
72
73 return double_int_to_tree (t, di);
74 }
75
76 /* Sets RES to the min of V1 and V2. */
77
78 static void
79 value_min (mpz_t res, mpz_t v1, mpz_t v2)
80 {
81 if (mpz_cmp (v1, v2) < 0)
82 mpz_set (res, v1);
83 else
84 mpz_set (res, v2);
85 }
86
87 /* Sets RES to the max of V1 and V2. */
88
89 static void
90 value_max (mpz_t res, mpz_t v1, mpz_t v2)
91 {
92 if (mpz_cmp (v1, v2) < 0)
93 mpz_set (res, v2);
94 else
95 mpz_set (res, v1);
96 }
97
98
99 /* This flag is set when an error occurred during the translation of
100 CLAST to Gimple. */
101 static bool gloog_error;
102
103 /* Verifies properties that GRAPHITE should maintain during translation. */
104
105 static inline void
106 graphite_verify (void)
107 {
108 #ifdef ENABLE_CHECKING
109 verify_loop_structure ();
110 verify_loop_closed_ssa (true);
111 #endif
112 }
113
114 /* Stores the INDEX in a vector and the loop nesting LEVEL for a given
115 clast NAME. BOUND_ONE and BOUND_TWO represent the exact lower and
116 upper bounds that can be inferred from the polyhedral representation. */
117
118 typedef struct clast_name_index {
119 int index;
120 int level;
121 mpz_t bound_one, bound_two;
122 const char *name;
123 /* If free_name is set, the content of name was allocated by us and needs
124 to be freed. */
125 char *free_name;
126 } *clast_name_index_p;
127
128 /* Helper for hashing clast_name_index. */
129
130 struct clast_index_hasher
131 {
132 typedef clast_name_index value_type;
133 typedef clast_name_index compare_type;
134 static inline hashval_t hash (const value_type *);
135 static inline bool equal (const value_type *, const compare_type *);
136 static inline void remove (value_type *);
137 };
138
139 /* Computes a hash function for database element E. */
140
141 inline hashval_t
142 clast_index_hasher::hash (const value_type *e)
143 {
144 hashval_t hash = 0;
145
146 int length = strlen (e->name);
147 int i;
148
149 for (i = 0; i < length; ++i)
150 hash = hash | (e->name[i] << (i % 4));
151
152 return hash;
153 }
154
155 /* Compares database elements ELT1 and ELT2. */
156
157 inline bool
158 clast_index_hasher::equal (const value_type *elt1, const compare_type *elt2)
159 {
160 return strcmp (elt1->name, elt2->name) == 0;
161 }
162
163 /* Free the memory taken by a clast_name_index struct. */
164
165 inline void
166 clast_index_hasher::remove (value_type *c)
167 {
168 if (c->free_name)
169 free (c->free_name);
170 mpz_clear (c->bound_one);
171 mpz_clear (c->bound_two);
172 free (c);
173 }
174
175 typedef hash_table <clast_index_hasher> clast_index_htab_type;
176
177 /* Returns a pointer to a new element of type clast_name_index_p built
178 from NAME, INDEX, LEVEL, BOUND_ONE, and BOUND_TWO. */
179
180 static inline clast_name_index_p
181 new_clast_name_index (const char *name, int index, int level,
182 mpz_t bound_one, mpz_t bound_two)
183 {
184 clast_name_index_p res = XNEW (struct clast_name_index);
185 char *new_name = XNEWVEC (char, strlen (name) + 1);
186 strcpy (new_name, name);
187
188 res->name = new_name;
189 res->free_name = new_name;
190 res->level = level;
191 res->index = index;
192 mpz_init (res->bound_one);
193 mpz_init (res->bound_two);
194 mpz_set (res->bound_one, bound_one);
195 mpz_set (res->bound_two, bound_two);
196 return res;
197 }
198
199 /* For a given clast NAME, returns -1 if NAME is not in the
200 INDEX_TABLE, otherwise returns the loop level for the induction
201 variable NAME, or if it is a parameter, the parameter number in the
202 vector of parameters. */
203
204 static inline int
205 clast_name_to_level (clast_name_p name, clast_index_htab_type index_table)
206 {
207 struct clast_name_index tmp;
208 clast_name_index **slot;
209
210 gcc_assert (name->type == clast_expr_name);
211 tmp.name = ((const struct clast_name *) name)->name;
212 tmp.free_name = NULL;
213
214 slot = index_table.find_slot (&tmp, NO_INSERT);
215
216 if (slot && *slot)
217 return ((struct clast_name_index *) *slot)->level;
218
219 return -1;
220 }
221
222 /* For a given clast NAME, returns -1 if it does not correspond to any
223 parameter, or otherwise, returns the index in the PARAMS or
224 SCATTERING_DIMENSIONS vector. */
225
226 static inline int
227 clast_name_to_index (struct clast_name *name, clast_index_htab_type index_table)
228 {
229 struct clast_name_index tmp;
230 clast_name_index **slot;
231
232 tmp.name = ((const struct clast_name *) name)->name;
233 tmp.free_name = NULL;
234
235 slot = index_table.find_slot (&tmp, NO_INSERT);
236
237 if (slot && *slot)
238 return (*slot)->index;
239
240 return -1;
241 }
242
243 /* For a given clast NAME, initializes the lower and upper bounds BOUND_ONE
244 and BOUND_TWO stored in the INDEX_TABLE. Returns true when NAME has been
245 found in the INDEX_TABLE, false otherwise. */
246
247 static inline bool
248 clast_name_to_lb_ub (struct clast_name *name, clast_index_htab_type index_table,
249 mpz_t bound_one, mpz_t bound_two)
250 {
251 struct clast_name_index tmp;
252 clast_name_index **slot;
253
254 tmp.name = name->name;
255 tmp.free_name = NULL;
256
257 slot = index_table.find_slot (&tmp, NO_INSERT);
258
259 if (slot && *slot)
260 {
261 mpz_set (bound_one, ((struct clast_name_index *) *slot)->bound_one);
262 mpz_set (bound_two, ((struct clast_name_index *) *slot)->bound_two);
263 return true;
264 }
265
266 return false;
267 }
268
269 /* Records in INDEX_TABLE the INDEX and LEVEL for NAME. */
270
271 static inline void
272 save_clast_name_index (clast_index_htab_type index_table, const char *name,
273 int index, int level, mpz_t bound_one, mpz_t bound_two)
274 {
275 struct clast_name_index tmp;
276 clast_name_index **slot;
277
278 tmp.name = name;
279 tmp.free_name = NULL;
280 slot = index_table.find_slot (&tmp, INSERT);
281
282 if (slot)
283 {
284 free (*slot);
285
286 *slot = new_clast_name_index (name, index, level, bound_one, bound_two);
287 }
288 }
289 \f
290
291 /* NEWIVS_INDEX binds CLooG's scattering name to the index of the tree
292 induction variable in NEWIVS.
293
294 PARAMS_INDEX binds CLooG's parameter name to the index of the tree
295 parameter in PARAMS. */
296
297 typedef struct ivs_params {
298 vec<tree> params, *newivs;
299 clast_index_htab_type newivs_index, params_index;
300 sese region;
301 } *ivs_params_p;
302
303 /* Returns the tree variable from the name NAME that was given in
304 Cloog representation. */
305
306 static tree
307 clast_name_to_gcc (struct clast_name *name, ivs_params_p ip)
308 {
309 int index;
310
311 if (ip->params.exists () && ip->params_index.is_created ())
312 {
313 index = clast_name_to_index (name, ip->params_index);
314
315 if (index >= 0)
316 return ip->params[index];
317 }
318
319 gcc_assert (ip->newivs && ip->newivs_index.is_created ());
320 index = clast_name_to_index (name, ip->newivs_index);
321 gcc_assert (index >= 0);
322
323 return (*ip->newivs)[index];
324 }
325
326 /* Returns the maximal precision type for expressions TYPE1 and TYPE2. */
327
328 static tree
329 max_precision_type (tree type1, tree type2)
330 {
331 enum machine_mode mode;
332 int p1, p2, precision;
333 tree type;
334
335 if (POINTER_TYPE_P (type1))
336 return type1;
337
338 if (POINTER_TYPE_P (type2))
339 return type2;
340
341 if (TYPE_UNSIGNED (type1)
342 && TYPE_UNSIGNED (type2))
343 return TYPE_PRECISION (type1) > TYPE_PRECISION (type2) ? type1 : type2;
344
345 p1 = TYPE_PRECISION (type1);
346 p2 = TYPE_PRECISION (type2);
347
348 if (p1 > p2)
349 precision = TYPE_UNSIGNED (type1) ? p1 * 2 : p1;
350 else
351 precision = TYPE_UNSIGNED (type2) ? p2 * 2 : p2;
352
353 if (precision > BITS_PER_WORD)
354 {
355 gloog_error = true;
356 return integer_type_node;
357 }
358
359 mode = smallest_mode_for_size (precision, MODE_INT);
360 precision = GET_MODE_PRECISION (mode);
361 type = build_nonstandard_integer_type (precision, false);
362
363 if (!type)
364 {
365 gloog_error = true;
366 return integer_type_node;
367 }
368
369 return type;
370 }
371
372 static tree
373 clast_to_gcc_expression (tree, struct clast_expr *, ivs_params_p);
374
375 /* Converts a Cloog reduction expression R with reduction operation OP
376 to a GCC expression tree of type TYPE. */
377
378 static tree
379 clast_to_gcc_expression_red (tree type, enum tree_code op,
380 struct clast_reduction *r, ivs_params_p ip)
381 {
382 int i;
383 tree res = clast_to_gcc_expression (type, r->elts[0], ip);
384 tree operand_type = (op == POINTER_PLUS_EXPR) ? sizetype : type;
385
386 for (i = 1; i < r->n; i++)
387 {
388 tree t = clast_to_gcc_expression (operand_type, r->elts[i], ip);
389 res = fold_build2 (op, type, res, t);
390 }
391
392 return res;
393 }
394
395 /* Converts a Cloog AST expression E back to a GCC expression tree of
396 type TYPE. */
397
398 static tree
399 clast_to_gcc_expression (tree type, struct clast_expr *e, ivs_params_p ip)
400 {
401 switch (e->type)
402 {
403 case clast_expr_name:
404 {
405 return clast_name_to_gcc ((struct clast_name *) e, ip);
406 }
407 case clast_expr_term:
408 {
409 struct clast_term *t = (struct clast_term *) e;
410
411 if (t->var)
412 {
413 if (mpz_cmp_si (t->val, 1) == 0)
414 {
415 tree name = clast_to_gcc_expression (type, t->var, ip);
416
417 if (POINTER_TYPE_P (TREE_TYPE (name)) != POINTER_TYPE_P (type))
418 name = convert_to_ptrofftype (name);
419
420 name = fold_convert (type, name);
421 return name;
422 }
423
424 else if (mpz_cmp_si (t->val, -1) == 0)
425 {
426 tree name = clast_to_gcc_expression (type, t->var, ip);
427
428 if (POINTER_TYPE_P (TREE_TYPE (name)) != POINTER_TYPE_P (type))
429 name = convert_to_ptrofftype (name);
430
431 name = fold_convert (type, name);
432
433 return fold_build1 (NEGATE_EXPR, type, name);
434 }
435 else
436 {
437 tree name = clast_to_gcc_expression (type, t->var, ip);
438 tree cst = gmp_cst_to_tree (type, t->val);
439
440 if (POINTER_TYPE_P (TREE_TYPE (name)) != POINTER_TYPE_P (type))
441 name = convert_to_ptrofftype (name);
442
443 name = fold_convert (type, name);
444
445 if (!POINTER_TYPE_P (type))
446 return fold_build2 (MULT_EXPR, type, cst, name);
447
448 gloog_error = true;
449 return cst;
450 }
451 }
452 else
453 return gmp_cst_to_tree (type, t->val);
454 }
455
456 case clast_expr_red:
457 {
458 struct clast_reduction *r = (struct clast_reduction *) e;
459
460 switch (r->type)
461 {
462 case clast_red_sum:
463 return clast_to_gcc_expression_red
464 (type, POINTER_TYPE_P (type) ? POINTER_PLUS_EXPR : PLUS_EXPR,
465 r, ip);
466
467 case clast_red_min:
468 return clast_to_gcc_expression_red (type, MIN_EXPR, r, ip);
469
470 case clast_red_max:
471 return clast_to_gcc_expression_red (type, MAX_EXPR, r, ip);
472
473 default:
474 gcc_unreachable ();
475 }
476 break;
477 }
478
479 case clast_expr_bin:
480 {
481 struct clast_binary *b = (struct clast_binary *) e;
482 struct clast_expr *lhs = (struct clast_expr *) b->LHS;
483 tree tl = clast_to_gcc_expression (type, lhs, ip);
484 tree tr = gmp_cst_to_tree (type, b->RHS);
485
486 switch (b->type)
487 {
488 case clast_bin_fdiv:
489 return fold_build2 (FLOOR_DIV_EXPR, type, tl, tr);
490
491 case clast_bin_cdiv:
492 return fold_build2 (CEIL_DIV_EXPR, type, tl, tr);
493
494 case clast_bin_div:
495 return fold_build2 (EXACT_DIV_EXPR, type, tl, tr);
496
497 case clast_bin_mod:
498 return fold_build2 (TRUNC_MOD_EXPR, type, tl, tr);
499
500 default:
501 gcc_unreachable ();
502 }
503 }
504
505 default:
506 gcc_unreachable ();
507 }
508
509 return NULL_TREE;
510 }
511
512 /* Return a type that could represent the values between BOUND_ONE and
513 BOUND_TWO. */
514
515 static tree
516 type_for_interval (mpz_t bound_one, mpz_t bound_two)
517 {
518 bool unsigned_p;
519 tree type;
520 enum machine_mode mode;
521 int wider_precision;
522 int precision = MAX (mpz_sizeinbase (bound_one, 2),
523 mpz_sizeinbase (bound_two, 2));
524
525 if (precision > BITS_PER_WORD)
526 {
527 gloog_error = true;
528 return integer_type_node;
529 }
530
531 if (mpz_cmp (bound_one, bound_two) <= 0)
532 unsigned_p = (mpz_sgn (bound_one) >= 0);
533 else
534 unsigned_p = (mpz_sgn (bound_two) >= 0);
535
536 mode = smallest_mode_for_size (precision, MODE_INT);
537 wider_precision = GET_MODE_PRECISION (mode);
538
539 /* As we want to generate signed types as much as possible, try to
540 fit the interval [bound_one, bound_two] in a signed type. For example,
541 supposing that we have the interval [0, 100], instead of
542 generating unsigned char, we want to generate a signed char. */
543 if (unsigned_p && precision < wider_precision)
544 unsigned_p = false;
545
546 type = build_nonstandard_integer_type (wider_precision, unsigned_p);
547
548 if (!type)
549 {
550 gloog_error = true;
551 return integer_type_node;
552 }
553
554 return type;
555 }
556
557 /* Return a type that could represent the integer value VAL, or
558 otherwise return NULL_TREE. */
559
560 static tree
561 type_for_value (mpz_t val)
562 {
563 return type_for_interval (val, val);
564 }
565
566 static tree
567 type_for_clast_expr (struct clast_expr *, ivs_params_p, mpz_t, mpz_t);
568
569 /* Return the type for the clast_term T. Initializes BOUND_ONE and
570 BOUND_TWO to the bounds of the term. */
571
572 static tree
573 type_for_clast_term (struct clast_term *t, ivs_params_p ip, mpz_t bound_one,
574 mpz_t bound_two)
575 {
576 tree type;
577 gcc_assert (t->expr.type == clast_expr_term);
578
579 if (!t->var)
580 {
581 mpz_set (bound_one, t->val);
582 mpz_set (bound_two, t->val);
583 return type_for_value (t->val);
584 }
585
586 type = type_for_clast_expr (t->var, ip, bound_one, bound_two);
587
588 mpz_mul (bound_one, bound_one, t->val);
589 mpz_mul (bound_two, bound_two, t->val);
590
591 return max_precision_type (type, type_for_interval (bound_one, bound_two));
592 }
593
594 /* Return the type for the clast_reduction R. Initializes BOUND_ONE
595 and BOUND_TWO to the bounds of the reduction expression. */
596
597 static tree
598 type_for_clast_red (struct clast_reduction *r, ivs_params_p ip,
599 mpz_t bound_one, mpz_t bound_two)
600 {
601 int i;
602 tree type = type_for_clast_expr (r->elts[0], ip, bound_one, bound_two);
603 mpz_t b1, b2, m1, m2;
604
605 if (r->n == 1)
606 return type;
607
608 mpz_init (b1);
609 mpz_init (b2);
610 mpz_init (m1);
611 mpz_init (m2);
612
613 for (i = 1; i < r->n; i++)
614 {
615 tree t = type_for_clast_expr (r->elts[i], ip, b1, b2);
616 type = max_precision_type (type, t);
617
618 switch (r->type)
619 {
620 case clast_red_sum:
621 value_min (m1, bound_one, bound_two);
622 value_min (m2, b1, b2);
623 mpz_add (bound_one, m1, m2);
624
625 value_max (m1, bound_one, bound_two);
626 value_max (m2, b1, b2);
627 mpz_add (bound_two, m1, m2);
628 break;
629
630 case clast_red_min:
631 value_min (bound_one, bound_one, bound_two);
632 value_min (bound_two, b1, b2);
633 break;
634
635 case clast_red_max:
636 value_max (bound_one, bound_one, bound_two);
637 value_max (bound_two, b1, b2);
638 break;
639
640 default:
641 gcc_unreachable ();
642 break;
643 }
644 }
645
646 mpz_clear (b1);
647 mpz_clear (b2);
648 mpz_clear (m1);
649 mpz_clear (m2);
650
651 /* Return a type that can represent the result of the reduction. */
652 return max_precision_type (type, type_for_interval (bound_one, bound_two));
653 }
654
655 /* Return the type for the clast_binary B used in STMT. */
656
657 static tree
658 type_for_clast_bin (struct clast_binary *b, ivs_params_p ip, mpz_t bound_one,
659 mpz_t bound_two)
660 {
661 mpz_t one;
662 tree l = type_for_clast_expr ((struct clast_expr *) b->LHS, ip,
663 bound_one, bound_two);
664 tree r = type_for_value (b->RHS);
665 tree type = max_precision_type (l, r);
666
667 switch (b->type)
668 {
669 case clast_bin_fdiv:
670 mpz_mdiv (bound_one, bound_one, b->RHS);
671 mpz_mdiv (bound_two, bound_two, b->RHS);
672 break;
673
674 case clast_bin_cdiv:
675 mpz_mdiv (bound_one, bound_one, b->RHS);
676 mpz_mdiv (bound_two, bound_two, b->RHS);
677 mpz_init (one);
678 mpz_add (bound_one, bound_one, one);
679 mpz_add (bound_two, bound_two, one);
680 mpz_clear (one);
681 break;
682
683 case clast_bin_div:
684 mpz_div (bound_one, bound_one, b->RHS);
685 mpz_div (bound_two, bound_two, b->RHS);
686 break;
687
688 case clast_bin_mod:
689 mpz_mod (bound_one, bound_one, b->RHS);
690 mpz_mod (bound_two, bound_two, b->RHS);
691 break;
692
693 default:
694 gcc_unreachable ();
695 }
696
697 /* Return a type that can represent the result of the reduction. */
698 return max_precision_type (type, type_for_interval (bound_one, bound_two));
699 }
700
701 /* Return the type for the clast_name NAME. Initializes BOUND_ONE and
702 BOUND_TWO to the bounds of the term. */
703
704 static tree
705 type_for_clast_name (struct clast_name *name, ivs_params_p ip, mpz_t bound_one,
706 mpz_t bound_two)
707 {
708 bool found = false;
709
710 if (ip->params.exists () && ip->params_index.is_created ())
711 found = clast_name_to_lb_ub (name, ip->params_index, bound_one, bound_two);
712
713 if (!found)
714 {
715 gcc_assert (ip->newivs && ip->newivs_index.is_created ());
716 found = clast_name_to_lb_ub (name, ip->newivs_index, bound_one,
717 bound_two);
718 gcc_assert (found);
719 }
720
721 return TREE_TYPE (clast_name_to_gcc (name, ip));
722 }
723
724 /* Returns the type for the CLAST expression E when used in statement
725 STMT. */
726
727 static tree
728 type_for_clast_expr (struct clast_expr *e, ivs_params_p ip, mpz_t bound_one,
729 mpz_t bound_two)
730 {
731 switch (e->type)
732 {
733 case clast_expr_term:
734 return type_for_clast_term ((struct clast_term *) e, ip,
735 bound_one, bound_two);
736
737 case clast_expr_red:
738 return type_for_clast_red ((struct clast_reduction *) e, ip,
739 bound_one, bound_two);
740
741 case clast_expr_bin:
742 return type_for_clast_bin ((struct clast_binary *) e, ip,
743 bound_one, bound_two);
744
745 case clast_expr_name:
746 return type_for_clast_name ((struct clast_name *) e, ip,
747 bound_one, bound_two);
748
749 default:
750 gcc_unreachable ();
751 }
752
753 return NULL_TREE;
754 }
755
756 /* Returns true if the clast expression E is a constant with VALUE. */
757
758 static bool
759 clast_expr_const_value_p (struct clast_expr *e, int value)
760 {
761 struct clast_term *t;
762 if (e->type != clast_expr_term)
763 return false;
764 t = (struct clast_term *)e;
765 if (t->var)
766 return false;
767 return 0 == mpz_cmp_si (t->val, value);
768 }
769
770 /* Translates a clast equation CLEQ to a tree. */
771
772 static tree
773 graphite_translate_clast_equation (struct clast_equation *cleq,
774 ivs_params_p ip)
775 {
776 enum tree_code comp;
777 tree type, lhs, rhs, ltype, rtype;
778 mpz_t bound_one, bound_two;
779 struct clast_expr *clhs, *crhs;
780
781 clhs = cleq->LHS;
782 crhs = cleq->RHS;
783 if (cleq->sign == 0)
784 comp = EQ_EXPR;
785 else if (cleq->sign > 0)
786 comp = GE_EXPR;
787 else
788 comp = LE_EXPR;
789
790 /* Special cases to reduce range of arguments to hopefully
791 don't need types with larger precision than the input. */
792 if (crhs->type == clast_expr_red
793 && comp != EQ_EXPR)
794 {
795 struct clast_reduction *r = (struct clast_reduction *) crhs;
796 /* X >= A+1 --> X > A and
797 X <= A-1 --> X < A */
798 if (r->n == 2
799 && r->type == clast_red_sum
800 && clast_expr_const_value_p (r->elts[1], comp == GE_EXPR ? 1 : -1))
801 {
802 crhs = r->elts[0];
803 comp = comp == GE_EXPR ? GT_EXPR : LT_EXPR;
804 }
805 }
806
807 mpz_init (bound_one);
808 mpz_init (bound_two);
809
810 ltype = type_for_clast_expr (clhs, ip, bound_one, bound_two);
811 rtype = type_for_clast_expr (crhs, ip, bound_one, bound_two);
812
813 mpz_clear (bound_one);
814 mpz_clear (bound_two);
815 type = max_precision_type (ltype, rtype);
816
817 lhs = clast_to_gcc_expression (type, clhs, ip);
818 rhs = clast_to_gcc_expression (type, crhs, ip);
819
820 return fold_build2 (comp, boolean_type_node, lhs, rhs);
821 }
822
823 /* Creates the test for the condition in STMT. */
824
825 static tree
826 graphite_create_guard_cond_expr (struct clast_guard *stmt,
827 ivs_params_p ip)
828 {
829 tree cond = NULL;
830 int i;
831
832 for (i = 0; i < stmt->n; i++)
833 {
834 tree eq = graphite_translate_clast_equation (&stmt->eq[i], ip);
835
836 if (cond)
837 cond = fold_build2 (TRUTH_AND_EXPR, TREE_TYPE (eq), cond, eq);
838 else
839 cond = eq;
840 }
841
842 return cond;
843 }
844
845 /* Creates a new if region corresponding to Cloog's guard. */
846
847 static edge
848 graphite_create_new_guard (edge entry_edge, struct clast_guard *stmt,
849 ivs_params_p ip)
850 {
851 tree cond_expr = graphite_create_guard_cond_expr (stmt, ip);
852 edge exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr);
853 return exit_edge;
854 }
855
856 /* Compute the lower bound LOW and upper bound UP for the parameter
857 PARAM in scop SCOP based on the constraints in the context. */
858
859 static void
860 compute_bounds_for_param (scop_p scop, int param, mpz_t low, mpz_t up)
861 {
862 isl_int v;
863 isl_aff *aff = isl_aff_zero_on_domain
864 (isl_local_space_from_space (isl_set_get_space (scop->context)));
865
866 aff = isl_aff_add_coefficient_si (aff, isl_dim_param, param, 1);
867
868 isl_int_init (v);
869 isl_set_min (scop->context, aff, &v);
870 isl_int_get_gmp (v, low);
871 isl_set_max (scop->context, aff, &v);
872 isl_int_get_gmp (v, up);
873 isl_int_clear (v);
874 isl_aff_free (aff);
875 }
876
877 /* Compute the lower bound LOW and upper bound UP for the induction
878 variable of loop LOOP.
879
880 FIXME: This one is not entirely correct, as min/max expressions in the
881 calculation can yield to incorrect results. To be completely
882 correct, we need to evaluate each subexpression generated by
883 CLooG. CLooG does not yet support this, so this is as good as
884 it can be. */
885
886 static void
887 compute_bounds_for_loop (struct clast_for *loop, mpz_t low, mpz_t up)
888 {
889 isl_set *domain;
890 isl_aff *dimension;
891 isl_local_space *local_space;
892 isl_int isl_value;
893 enum isl_lp_result lp_result;
894
895 domain = isl_set_copy (isl_set_from_cloog_domain (loop->domain));
896 local_space = isl_local_space_from_space (isl_set_get_space (domain));
897 dimension = isl_aff_zero_on_domain (local_space);
898 dimension = isl_aff_add_coefficient_si (dimension, isl_dim_in,
899 isl_set_dim (domain, isl_dim_set) - 1,
900 1);
901
902 isl_int_init (isl_value);
903
904 lp_result = isl_set_min (domain, dimension, &isl_value);
905 assert (lp_result == isl_lp_ok);
906 isl_int_get_gmp (isl_value, low);
907
908 lp_result = isl_set_max (domain, dimension, &isl_value);
909 assert (lp_result == isl_lp_ok);
910 isl_int_get_gmp (isl_value, up);
911
912 isl_int_clear (isl_value);
913 isl_set_free (domain);
914 isl_aff_free (dimension);
915 }
916
917 /* Returns the type for the induction variable for the loop translated
918 from STMT_FOR. */
919
920 static tree
921 type_for_clast_for (struct clast_for *stmt_for, ivs_params_p ip)
922 {
923 mpz_t bound_one, bound_two;
924 tree lb_type, ub_type;
925
926 mpz_init (bound_one);
927 mpz_init (bound_two);
928
929 lb_type = type_for_clast_expr (stmt_for->LB, ip, bound_one, bound_two);
930 ub_type = type_for_clast_expr (stmt_for->UB, ip, bound_one, bound_two);
931
932 mpz_clear (bound_one);
933 mpz_clear (bound_two);
934
935 return max_precision_type (lb_type, ub_type);
936 }
937
938 /* Creates a new LOOP corresponding to Cloog's STMT. Inserts an
939 induction variable for the new LOOP. New LOOP is attached to CFG
940 starting at ENTRY_EDGE. LOOP is inserted into the loop tree and
941 becomes the child loop of the OUTER_LOOP. NEWIVS_INDEX binds
942 CLooG's scattering name to the induction variable created for the
943 loop of STMT. The new induction variable is inserted in the NEWIVS
944 vector and is of type TYPE. */
945
946 static struct loop *
947 graphite_create_new_loop (edge entry_edge, struct clast_for *stmt,
948 loop_p outer, tree type, tree lb, tree ub,
949 int level, ivs_params_p ip)
950 {
951 mpz_t low, up;
952
953 tree stride = gmp_cst_to_tree (type, stmt->stride);
954 tree ivvar = create_tmp_var (type, "graphite_IV");
955 tree iv, iv_after_increment;
956 loop_p loop = create_empty_loop_on_edge
957 (entry_edge, lb, stride, ub, ivvar, &iv, &iv_after_increment,
958 outer ? outer : entry_edge->src->loop_father);
959
960 mpz_init (low);
961 mpz_init (up);
962 compute_bounds_for_loop (stmt, low, up);
963 save_clast_name_index (ip->newivs_index, stmt->iterator,
964 (*ip->newivs).length (), level, low, up);
965 mpz_clear (low);
966 mpz_clear (up);
967 (*ip->newivs).safe_push (iv);
968 return loop;
969 }
970
971 /* Inserts in iv_map a tuple (OLD_LOOP->num, NEW_NAME) for the
972 induction variables of the loops around GBB in SESE. */
973
974 static void
975 build_iv_mapping (vec<tree> iv_map, struct clast_user_stmt *user_stmt,
976 ivs_params_p ip)
977 {
978 struct clast_stmt *t;
979 int depth = 0;
980 CloogStatement *cs = user_stmt->statement;
981 poly_bb_p pbb = (poly_bb_p) cs->usr;
982 gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
983 mpz_t bound_one, bound_two;
984
985 mpz_init (bound_one);
986 mpz_init (bound_two);
987
988 for (t = user_stmt->substitutions; t; t = t->next, depth++)
989 {
990 struct clast_expr *expr = (struct clast_expr *)
991 ((struct clast_assignment *)t)->RHS;
992 tree type = type_for_clast_expr (expr, ip, bound_one, bound_two);
993 tree new_name = clast_to_gcc_expression (type, expr, ip);
994 loop_p old_loop = gbb_loop_at_index (gbb, ip->region, depth);
995
996 iv_map[old_loop->num] = new_name;
997 }
998
999 mpz_clear (bound_one);
1000 mpz_clear (bound_two);
1001 }
1002
1003 /* Construct bb_pbb_def with BB and PBB. */
1004
1005 static bb_pbb_def *
1006 new_bb_pbb_def (basic_block bb, poly_bb_p pbb)
1007 {
1008 bb_pbb_def *bb_pbb_p;
1009
1010 bb_pbb_p = XNEW (bb_pbb_def);
1011 bb_pbb_p->bb = bb;
1012 bb_pbb_p->pbb = pbb;
1013
1014 return bb_pbb_p;
1015 }
1016
1017 /* Mark BB with it's relevant PBB via hashing table BB_PBB_MAPPING. */
1018
1019 static void
1020 mark_bb_with_pbb (poly_bb_p pbb, basic_block bb,
1021 bb_pbb_htab_type bb_pbb_mapping)
1022 {
1023 bb_pbb_def tmp;
1024 bb_pbb_def **x;
1025
1026 tmp.bb = bb;
1027 x = bb_pbb_mapping.find_slot (&tmp, INSERT);
1028
1029 if (x && !*x)
1030 *x = new_bb_pbb_def (bb, pbb);
1031 }
1032
1033 /* Find BB's related poly_bb_p in hash table BB_PBB_MAPPING. */
1034
1035 poly_bb_p
1036 find_pbb_via_hash (bb_pbb_htab_type bb_pbb_mapping, basic_block bb)
1037 {
1038 bb_pbb_def tmp;
1039 bb_pbb_def **slot;
1040
1041 tmp.bb = bb;
1042 slot = bb_pbb_mapping.find_slot (&tmp, NO_INSERT);
1043
1044 if (slot && *slot)
1045 return ((bb_pbb_def *) *slot)->pbb;
1046
1047 return NULL;
1048 }
1049
1050 /* Return the scop of the loop and initialize PBBS the set of
1051 poly_bb_p that belong to the LOOP. BB_PBB_MAPPING is a map created
1052 by the CLAST code generator between a generated basic_block and its
1053 related poly_bb_p. */
1054
1055 scop_p
1056 get_loop_body_pbbs (loop_p loop, bb_pbb_htab_type bb_pbb_mapping,
1057 vec<poly_bb_p> *pbbs)
1058 {
1059 unsigned i;
1060 basic_block *bbs = get_loop_body_in_dom_order (loop);
1061 scop_p scop = NULL;
1062
1063 for (i = 0; i < loop->num_nodes; i++)
1064 {
1065 poly_bb_p pbb = find_pbb_via_hash (bb_pbb_mapping, bbs[i]);
1066
1067 if (pbb == NULL)
1068 continue;
1069
1070 scop = PBB_SCOP (pbb);
1071 (*pbbs).safe_push (pbb);
1072 }
1073
1074 free (bbs);
1075 return scop;
1076 }
1077
1078 /* Translates a clast user statement STMT to gimple.
1079
1080 - NEXT_E is the edge where new generated code should be attached.
1081 - CONTEXT_LOOP is the loop in which the generated code will be placed
1082 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping. */
1083
1084 static edge
1085 translate_clast_user (struct clast_user_stmt *stmt, edge next_e,
1086 bb_pbb_htab_type bb_pbb_mapping, ivs_params_p ip)
1087 {
1088 int i, nb_loops;
1089 basic_block new_bb;
1090 poly_bb_p pbb = (poly_bb_p) stmt->statement->usr;
1091 gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
1092 vec<tree> iv_map;
1093
1094 if (GBB_BB (gbb) == ENTRY_BLOCK_PTR)
1095 return next_e;
1096
1097 nb_loops = number_of_loops (cfun);
1098 iv_map.create (nb_loops);
1099 for (i = 0; i < nb_loops; i++)
1100 iv_map.quick_push (NULL_TREE);
1101
1102 build_iv_mapping (iv_map, stmt, ip);
1103 next_e = copy_bb_and_scalar_dependences (GBB_BB (gbb), ip->region,
1104 next_e, iv_map, &gloog_error);
1105 iv_map.release ();
1106
1107 new_bb = next_e->src;
1108 mark_bb_with_pbb (pbb, new_bb, bb_pbb_mapping);
1109 mark_virtual_operands_for_renaming (cfun);
1110 update_ssa (TODO_update_ssa);
1111
1112 return next_e;
1113 }
1114
1115 /* Creates a new if region protecting the loop to be executed, if the execution
1116 count is zero (lb > ub). */
1117
1118 static edge
1119 graphite_create_new_loop_guard (edge entry_edge, struct clast_for *stmt,
1120 tree *type, tree *lb, tree *ub,
1121 ivs_params_p ip)
1122 {
1123 tree cond_expr;
1124 edge exit_edge;
1125
1126 *type = type_for_clast_for (stmt, ip);
1127 *lb = clast_to_gcc_expression (*type, stmt->LB, ip);
1128 *ub = clast_to_gcc_expression (*type, stmt->UB, ip);
1129
1130 /* When ub is simply a constant or a parameter, use lb <= ub. */
1131 if (TREE_CODE (*ub) == INTEGER_CST || TREE_CODE (*ub) == SSA_NAME)
1132 cond_expr = fold_build2 (LE_EXPR, boolean_type_node, *lb, *ub);
1133 else
1134 {
1135 tree one = (POINTER_TYPE_P (*type)
1136 ? convert_to_ptrofftype (integer_one_node)
1137 : fold_convert (*type, integer_one_node));
1138 /* Adding +1 and using LT_EXPR helps with loop latches that have a
1139 loop iteration count of "PARAMETER - 1". For PARAMETER == 0 this becomes
1140 2^k-1 due to integer overflow, and the condition lb <= ub is true,
1141 even if we do not want this. However lb < ub + 1 is false, as
1142 expected. */
1143 tree ub_one = fold_build2 (POINTER_TYPE_P (*type) ? POINTER_PLUS_EXPR
1144 : PLUS_EXPR, *type, *ub, one);
1145
1146 cond_expr = fold_build2 (LT_EXPR, boolean_type_node, *lb, ub_one);
1147 }
1148
1149 exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr);
1150
1151 return exit_edge;
1152 }
1153
1154 static edge
1155 translate_clast (loop_p, struct clast_stmt *, edge, bb_pbb_htab_type,
1156 int, ivs_params_p);
1157
1158 /* Create the loop for a clast for statement.
1159
1160 - NEXT_E is the edge where new generated code should be attached.
1161 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping. */
1162
1163 static edge
1164 translate_clast_for_loop (loop_p context_loop, struct clast_for *stmt,
1165 edge next_e, bb_pbb_htab_type bb_pbb_mapping,
1166 int level, tree type, tree lb, tree ub,
1167 ivs_params_p ip)
1168 {
1169 struct loop *loop = graphite_create_new_loop (next_e, stmt, context_loop,
1170 type, lb, ub, level, ip);
1171 edge last_e = single_exit (loop);
1172 edge to_body = single_succ_edge (loop->header);
1173 basic_block after = to_body->dest;
1174
1175 /* Create a basic block for loop close phi nodes. */
1176 last_e = single_succ_edge (split_edge (last_e));
1177
1178 /* Translate the body of the loop. */
1179 next_e = translate_clast (loop, stmt->body, to_body, bb_pbb_mapping,
1180 level + 1, ip);
1181 redirect_edge_succ_nodup (next_e, after);
1182 set_immediate_dominator (CDI_DOMINATORS, next_e->dest, next_e->src);
1183
1184 if (flag_loop_parallelize_all
1185 && loop_is_parallel_p (loop, bb_pbb_mapping, level))
1186 loop->can_be_parallel = true;
1187
1188 return last_e;
1189 }
1190
1191 /* Translates a clast for statement STMT to gimple. First a guard is created
1192 protecting the loop, if it is executed zero times. In this guard we create
1193 the real loop structure.
1194
1195 - NEXT_E is the edge where new generated code should be attached.
1196 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping. */
1197
1198 static edge
1199 translate_clast_for (loop_p context_loop, struct clast_for *stmt, edge next_e,
1200 bb_pbb_htab_type bb_pbb_mapping, int level,
1201 ivs_params_p ip)
1202 {
1203 tree type, lb, ub;
1204 edge last_e = graphite_create_new_loop_guard (next_e, stmt, &type,
1205 &lb, &ub, ip);
1206 edge true_e = get_true_edge_from_guard_bb (next_e->dest);
1207
1208 translate_clast_for_loop (context_loop, stmt, true_e, bb_pbb_mapping, level,
1209 type, lb, ub, ip);
1210 return last_e;
1211 }
1212
1213 /* Translates a clast assignment STMT to gimple.
1214
1215 - NEXT_E is the edge where new generated code should be attached.
1216 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping. */
1217
1218 static edge
1219 translate_clast_assignment (struct clast_assignment *stmt, edge next_e,
1220 int level, ivs_params_p ip)
1221 {
1222 gimple_seq stmts;
1223 mpz_t bound_one, bound_two;
1224 tree type, new_name, var;
1225 edge res = single_succ_edge (split_edge (next_e));
1226 struct clast_expr *expr = (struct clast_expr *) stmt->RHS;
1227
1228 mpz_init (bound_one);
1229 mpz_init (bound_two);
1230 type = type_for_clast_expr (expr, ip, bound_one, bound_two);
1231 var = create_tmp_var (type, "graphite_var");
1232 new_name = force_gimple_operand (clast_to_gcc_expression (type, expr, ip),
1233 &stmts, true, var);
1234 if (stmts)
1235 {
1236 gsi_insert_seq_on_edge (next_e, stmts);
1237 gsi_commit_edge_inserts ();
1238 }
1239
1240 save_clast_name_index (ip->newivs_index, stmt->LHS,
1241 (*ip->newivs).length (), level,
1242 bound_one, bound_two);
1243 (*ip->newivs).safe_push (new_name);
1244
1245 mpz_clear (bound_one);
1246 mpz_clear (bound_two);
1247
1248 return res;
1249 }
1250
1251 /* Translates a clast guard statement STMT to gimple.
1252
1253 - NEXT_E is the edge where new generated code should be attached.
1254 - CONTEXT_LOOP is the loop in which the generated code will be placed
1255 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping. */
1256
1257 static edge
1258 translate_clast_guard (loop_p context_loop, struct clast_guard *stmt,
1259 edge next_e, bb_pbb_htab_type bb_pbb_mapping, int level,
1260 ivs_params_p ip)
1261 {
1262 edge last_e = graphite_create_new_guard (next_e, stmt, ip);
1263 edge true_e = get_true_edge_from_guard_bb (next_e->dest);
1264
1265 translate_clast (context_loop, stmt->then, true_e, bb_pbb_mapping, level, ip);
1266 return last_e;
1267 }
1268
1269 /* Translates a CLAST statement STMT to GCC representation in the
1270 context of a SESE.
1271
1272 - NEXT_E is the edge where new generated code should be attached.
1273 - CONTEXT_LOOP is the loop in which the generated code will be placed
1274 - BB_PBB_MAPPING is is a basic_block and it's related poly_bb_p mapping. */
1275
1276 static edge
1277 translate_clast (loop_p context_loop, struct clast_stmt *stmt, edge next_e,
1278 bb_pbb_htab_type bb_pbb_mapping, int level, ivs_params_p ip)
1279 {
1280 if (!stmt)
1281 return next_e;
1282
1283 if (CLAST_STMT_IS_A (stmt, stmt_root))
1284 ; /* Do nothing. */
1285
1286 else if (CLAST_STMT_IS_A (stmt, stmt_user))
1287 next_e = translate_clast_user ((struct clast_user_stmt *) stmt,
1288 next_e, bb_pbb_mapping, ip);
1289
1290 else if (CLAST_STMT_IS_A (stmt, stmt_for))
1291 next_e = translate_clast_for (context_loop, (struct clast_for *) stmt,
1292 next_e, bb_pbb_mapping, level, ip);
1293
1294 else if (CLAST_STMT_IS_A (stmt, stmt_guard))
1295 next_e = translate_clast_guard (context_loop, (struct clast_guard *) stmt,
1296 next_e, bb_pbb_mapping, level, ip);
1297
1298 else if (CLAST_STMT_IS_A (stmt, stmt_block))
1299 next_e = translate_clast (context_loop, ((struct clast_block *) stmt)->body,
1300 next_e, bb_pbb_mapping, level, ip);
1301
1302 else if (CLAST_STMT_IS_A (stmt, stmt_ass))
1303 next_e = translate_clast_assignment ((struct clast_assignment *) stmt,
1304 next_e, level, ip);
1305 else
1306 gcc_unreachable();
1307
1308 recompute_all_dominators ();
1309 graphite_verify ();
1310
1311 return translate_clast (context_loop, stmt->next, next_e, bb_pbb_mapping,
1312 level, ip);
1313 }
1314
1315 /* Add parameter and iterator names to the CloogUnionDomain. */
1316
1317 static CloogUnionDomain *
1318 add_names_to_union_domain (scop_p scop, CloogUnionDomain *union_domain,
1319 int nb_scattering_dims,
1320 clast_index_htab_type params_index)
1321 {
1322 sese region = SCOP_REGION (scop);
1323 int i;
1324 int nb_iterators = scop_max_loop_depth (scop);
1325 int nb_parameters = SESE_PARAMS (region).length ();
1326 mpz_t bound_one, bound_two;
1327
1328 mpz_init (bound_one);
1329 mpz_init (bound_two);
1330
1331 for (i = 0; i < nb_parameters; i++)
1332 {
1333 tree param = SESE_PARAMS (region)[i];
1334 const char *name = get_name (param);
1335 int len;
1336 char *parameter;
1337
1338 if (!name)
1339 name = "T";
1340
1341 len = strlen (name);
1342 len += 17;
1343 parameter = XNEWVEC (char, len + 1);
1344 snprintf (parameter, len, "%s_%d", name, SSA_NAME_VERSION (param));
1345 save_clast_name_index (params_index, parameter, i, i, bound_one,
1346 bound_two);
1347 union_domain = cloog_union_domain_set_name (union_domain, CLOOG_PARAM, i,
1348 parameter);
1349 compute_bounds_for_param (scop, i, bound_one, bound_two);
1350 free (parameter);
1351 }
1352
1353 mpz_clear (bound_one);
1354 mpz_clear (bound_two);
1355
1356 for (i = 0; i < nb_iterators; i++)
1357 {
1358 int len = 4 + 16;
1359 char *iterator;
1360 iterator = XNEWVEC (char, len);
1361 snprintf (iterator, len, "git_%d", i);
1362 union_domain = cloog_union_domain_set_name (union_domain, CLOOG_ITER, i,
1363 iterator);
1364 free (iterator);
1365 }
1366
1367 for (i = 0; i < nb_scattering_dims; i++)
1368 {
1369 int len = 5 + 16;
1370 char *scattering;
1371 scattering = XNEWVEC (char, len);
1372 snprintf (scattering, len, "scat_%d", i);
1373 union_domain = cloog_union_domain_set_name (union_domain, CLOOG_SCAT, i,
1374 scattering);
1375 free (scattering);
1376 }
1377
1378 return union_domain;
1379 }
1380
1381 /* Initialize a CLooG input file. */
1382
1383 static FILE *
1384 init_cloog_input_file (int scop_number)
1385 {
1386 FILE *graphite_out_file;
1387 int len = strlen (dump_base_name);
1388 char *dumpname = XNEWVEC (char, len + 25);
1389 char *s_scop_number = XNEWVEC (char, 15);
1390
1391 memcpy (dumpname, dump_base_name, len + 1);
1392 strip_off_ending (dumpname, len);
1393 sprintf (s_scop_number, ".%d", scop_number);
1394 strcat (dumpname, s_scop_number);
1395 strcat (dumpname, ".cloog");
1396 graphite_out_file = fopen (dumpname, "w+b");
1397
1398 if (graphite_out_file == 0)
1399 fatal_error ("can%'t open %s for writing: %m", dumpname);
1400
1401 free (dumpname);
1402
1403 return graphite_out_file;
1404 }
1405
1406 /* Extend the scattering to NEW_DIMS scattering dimensions. */
1407
1408 static
1409 isl_map *extend_scattering(isl_map *scattering, int new_dims)
1410 {
1411 int old_dims, i;
1412 isl_space *space;
1413 isl_basic_map *change_scattering;
1414 isl_map *change_scattering_map;
1415
1416 old_dims = isl_map_dim (scattering, isl_dim_out);
1417
1418 space = isl_space_alloc (isl_map_get_ctx (scattering), 0, old_dims, new_dims);
1419 change_scattering = isl_basic_map_universe (isl_space_copy (space));
1420
1421 for (i = 0; i < old_dims; i++)
1422 {
1423 isl_constraint *c;
1424 c = isl_equality_alloc
1425 (isl_local_space_from_space (isl_space_copy (space)));
1426 isl_constraint_set_coefficient_si (c, isl_dim_in, i, 1);
1427 isl_constraint_set_coefficient_si (c, isl_dim_out, i, -1);
1428 change_scattering = isl_basic_map_add_constraint (change_scattering, c);
1429 }
1430
1431 for (i = old_dims; i < new_dims; i++)
1432 {
1433 isl_constraint *c;
1434 c = isl_equality_alloc
1435 (isl_local_space_from_space (isl_space_copy (space)));
1436 isl_constraint_set_coefficient_si (c, isl_dim_out, i, 1);
1437 change_scattering = isl_basic_map_add_constraint (change_scattering, c);
1438 }
1439
1440 change_scattering_map = isl_map_from_basic_map (change_scattering);
1441 change_scattering_map = isl_map_align_params (change_scattering_map, space);
1442 return isl_map_apply_range (scattering, change_scattering_map);
1443 }
1444
1445 /* Build cloog union domain for SCoP. */
1446
1447 static CloogUnionDomain *
1448 build_cloog_union_domain (scop_p scop, int nb_scattering_dims)
1449 {
1450 int i;
1451 poly_bb_p pbb;
1452 CloogUnionDomain *union_domain =
1453 cloog_union_domain_alloc (scop_nb_params (scop));
1454
1455 FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
1456 {
1457 CloogDomain *domain;
1458 CloogScattering *scattering;
1459
1460 /* Dead code elimination: when the domain of a PBB is empty,
1461 don't generate code for the PBB. */
1462 if (isl_set_is_empty(pbb->domain))
1463 continue;
1464
1465 domain = cloog_domain_from_isl_set(isl_set_copy(pbb->domain));
1466 scattering = cloog_scattering_from_isl_map(extend_scattering(isl_map_copy(pbb->transformed),
1467 nb_scattering_dims));
1468
1469 union_domain = cloog_union_domain_add_domain (union_domain, "", domain,
1470 scattering, pbb);
1471 }
1472
1473 return union_domain;
1474 }
1475
1476 /* Return the options that will be used in GLOOG. */
1477
1478 static CloogOptions *
1479 set_cloog_options (void)
1480 {
1481 CloogOptions *options = cloog_options_malloc (cloog_state);
1482
1483 /* Change cloog output language to C. If we do use FORTRAN instead, cloog
1484 will stop e.g. with "ERROR: unbounded loops not allowed in FORTRAN.", if
1485 we pass an incomplete program to cloog. */
1486 options->language = CLOOG_LANGUAGE_C;
1487
1488 /* Enable complex equality spreading: removes dummy statements
1489 (assignments) in the generated code which repeats the
1490 substitution equations for statements. This is useless for
1491 GLooG. */
1492 options->esp = 1;
1493
1494 /* Silence CLooG to avoid failing tests due to debug output to stderr. */
1495 options->quiet = 1;
1496
1497 /* Allow cloog to build strides with a stride width different to one.
1498 This example has stride = 4:
1499
1500 for (i = 0; i < 20; i += 4)
1501 A */
1502 options->strides = 1;
1503
1504 /* We want the clast to provide the iteration domains of the executed loops.
1505 This allows us to derive minimal/maximal values for the induction
1506 variables. */
1507 options->save_domains = 1;
1508
1509 /* Disable optimizations and make cloog generate source code closer to the
1510 input. This is useful for debugging, but later we want the optimized
1511 code.
1512
1513 XXX: We can not disable optimizations, as loop blocking is not working
1514 without them. */
1515 if (0)
1516 {
1517 options->f = -1;
1518 options->l = INT_MAX;
1519 }
1520
1521 return options;
1522 }
1523
1524 /* Prints STMT to STDERR. */
1525
1526 void
1527 print_clast_stmt (FILE *file, struct clast_stmt *stmt)
1528 {
1529 CloogOptions *options = set_cloog_options ();
1530
1531 clast_pprint (file, stmt, 0, options);
1532 cloog_options_free (options);
1533 }
1534
1535 /* Prints STMT to STDERR. */
1536
1537 DEBUG_FUNCTION void
1538 debug_clast_stmt (struct clast_stmt *stmt)
1539 {
1540 print_clast_stmt (stderr, stmt);
1541 }
1542
1543 /* Get the maximal number of scattering dimensions in the scop SCOP. */
1544
1545 static
1546 int get_max_scattering_dimensions (scop_p scop)
1547 {
1548 int i;
1549 poly_bb_p pbb;
1550 int scattering_dims = 0;
1551
1552 FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
1553 {
1554 int pbb_scatt_dims = isl_map_dim (pbb->transformed, isl_dim_out);
1555 if (pbb_scatt_dims > scattering_dims)
1556 scattering_dims = pbb_scatt_dims;
1557 }
1558
1559 return scattering_dims;
1560 }
1561
1562 static CloogInput *
1563 generate_cloog_input (scop_p scop, clast_index_htab_type params_index)
1564 {
1565 CloogUnionDomain *union_domain;
1566 CloogInput *cloog_input;
1567 CloogDomain *context;
1568 int nb_scattering_dims = get_max_scattering_dimensions (scop);
1569
1570 union_domain = build_cloog_union_domain (scop, nb_scattering_dims);
1571 union_domain = add_names_to_union_domain (scop, union_domain,
1572 nb_scattering_dims,
1573 params_index);
1574 context = cloog_domain_from_isl_set (isl_set_copy (scop->context));
1575
1576 cloog_input = cloog_input_alloc (context, union_domain);
1577
1578 return cloog_input;
1579 }
1580
1581 /* Translate SCOP to a CLooG program and clast. These two
1582 representations should be freed together: a clast cannot be used
1583 without a program. */
1584
1585 static struct clast_stmt *
1586 scop_to_clast (scop_p scop, clast_index_htab_type params_index)
1587 {
1588 CloogInput *cloog_input;
1589 struct clast_stmt *clast;
1590 CloogOptions *options = set_cloog_options ();
1591
1592 cloog_input = generate_cloog_input (scop, params_index);
1593
1594 /* Dump a .cloog input file, if requested. This feature is only
1595 enabled in the Graphite branch. */
1596 if (0)
1597 {
1598 static size_t file_scop_number = 0;
1599 FILE *cloog_file = init_cloog_input_file (file_scop_number);
1600 cloog_input_dump_cloog (cloog_file, cloog_input, options);
1601 }
1602
1603 clast = cloog_clast_create_from_input (cloog_input, options);
1604
1605 cloog_options_free (options);
1606 return clast;
1607 }
1608
1609 /* Prints to FILE the code generated by CLooG for SCOP. */
1610
1611 void
1612 print_generated_program (FILE *file, scop_p scop)
1613 {
1614 CloogOptions *options = set_cloog_options ();
1615 clast_index_htab_type params_index;
1616 struct clast_stmt *clast;
1617
1618 params_index.create (10);
1619
1620 clast = scop_to_clast (scop, params_index);
1621
1622 fprintf (file, " (clast: \n");
1623 clast_pprint (file, clast, 0, options);
1624 fprintf (file, " )\n");
1625
1626 cloog_options_free (options);
1627 cloog_clast_free (clast);
1628 }
1629
1630 /* Prints to STDERR the code generated by CLooG for SCOP. */
1631
1632 DEBUG_FUNCTION void
1633 debug_generated_program (scop_p scop)
1634 {
1635 print_generated_program (stderr, scop);
1636 }
1637
1638 /* GIMPLE Loop Generator: generates loops from STMT in GIMPLE form for
1639 the given SCOP. Return true if code generation succeeded.
1640 BB_PBB_MAPPING is a basic_block and it's related poly_bb_p mapping.
1641 */
1642
1643 bool
1644 gloog (scop_p scop, bb_pbb_htab_type bb_pbb_mapping)
1645 {
1646 vec<tree> newivs;
1647 newivs.create (10);
1648 loop_p context_loop;
1649 sese region = SCOP_REGION (scop);
1650 ifsese if_region = NULL;
1651 clast_index_htab_type newivs_index, params_index;
1652 struct clast_stmt *clast;
1653 struct ivs_params ip;
1654
1655 timevar_push (TV_GRAPHITE_CODE_GEN);
1656 gloog_error = false;
1657
1658 params_index.create (10);
1659
1660 clast = scop_to_clast (scop, params_index);
1661
1662 if (dump_file && (dump_flags & TDF_DETAILS))
1663 {
1664 fprintf (dump_file, "\nCLAST generated by CLooG: \n");
1665 print_clast_stmt (dump_file, clast);
1666 fprintf (dump_file, "\n");
1667 }
1668
1669 recompute_all_dominators ();
1670 graphite_verify ();
1671
1672 if_region = move_sese_in_condition (region);
1673 sese_insert_phis_for_liveouts (region,
1674 if_region->region->exit->src,
1675 if_region->false_region->exit,
1676 if_region->true_region->exit);
1677 recompute_all_dominators ();
1678 graphite_verify ();
1679
1680 context_loop = SESE_ENTRY (region)->src->loop_father;
1681 newivs_index.create (10);
1682
1683 ip.newivs = &newivs;
1684 ip.newivs_index = newivs_index;
1685 ip.params = SESE_PARAMS (region);
1686 ip.params_index = params_index;
1687 ip.region = region;
1688
1689 translate_clast (context_loop, clast, if_region->true_region->entry,
1690 bb_pbb_mapping, 0, &ip);
1691 graphite_verify ();
1692 scev_reset ();
1693 recompute_all_dominators ();
1694 graphite_verify ();
1695
1696 if (gloog_error)
1697 set_ifsese_condition (if_region, integer_zero_node);
1698
1699 free (if_region->true_region);
1700 free (if_region->region);
1701 free (if_region);
1702
1703 newivs_index.dispose ();
1704 params_index.dispose ();
1705 newivs.release ();
1706 cloog_clast_free (clast);
1707 timevar_pop (TV_GRAPHITE_CODE_GEN);
1708
1709 if (dump_file && (dump_flags & TDF_DETAILS))
1710 {
1711 loop_p loop;
1712 loop_iterator li;
1713 int num_no_dependency = 0;
1714
1715 FOR_EACH_LOOP (li, loop, 0)
1716 if (loop->can_be_parallel)
1717 num_no_dependency++;
1718
1719 fprintf (dump_file, "\n%d loops carried no dependency.\n",
1720 num_no_dependency);
1721 }
1722
1723 return !gloog_error;
1724 }
1725 #endif