[AArch64][14/14] Reuse target_option_current_node when passing pragma string to targe...
[official-gcc.git] / gcc / graphite-isl-ast-to-gimple.c
blobdfb012f75b18636fb5aca86b4b738f2bd958cca2
1 /* Translation of ISL AST to Gimple.
2 Copyright (C) 2014-2015 Free Software Foundation, Inc.
3 Contributed by Roman Gareev <gareevroman@gmail.com>.
5 This file is part of GCC.
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.
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.
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/>. */
21 #include "config.h"
23 #ifdef HAVE_isl
24 /* Workaround for GMP 5.1.3 bug, see PR56019. */
25 #include <stddef.h>
27 #include <isl/constraint.h>
28 #include <isl/set.h>
29 #include <isl/union_set.h>
30 #include <isl/map.h>
31 #include <isl/union_map.h>
32 #include <isl/ast_build.h>
34 /* Since ISL-0.13, the extern is in val_gmp.h. */
35 #if !defined(HAVE_ISL_SCHED_CONSTRAINTS_COMPUTE_SCHEDULE) && defined(__cplusplus)
36 extern "C" {
37 #endif
38 #include <isl/val_gmp.h>
39 #if !defined(HAVE_ISL_SCHED_CONSTRAINTS_COMPUTE_SCHEDULE) && defined(__cplusplus)
41 #endif
43 #include "system.h"
44 #include "coretypes.h"
45 #include "backend.h"
46 #include "cfghooks.h"
47 #include "tree.h"
48 #include "gimple.h"
49 #include "params.h"
50 #include "fold-const.h"
51 #include "gimple-iterator.h"
52 #include "tree-ssa-loop.h"
53 #include "tree-pass.h"
54 #include "cfgloop.h"
55 #include "tree-data-ref.h"
56 #include "graphite-poly.h"
57 #include "tree-ssa-loop-manip.h"
58 #include "tree-scalar-evolution.h"
59 #include "gimple-ssa.h"
60 #include "tree-into-ssa.h"
61 #include <map>
62 #include "graphite-isl-ast-to-gimple.h"
64 /* This flag is set when an error occurred during the translation of
65 ISL AST to Gimple. */
67 static bool graphite_regenerate_error;
69 /* We always try to use signed 128 bit types, but fall back to smaller types
70 in case a platform does not provide types of these sizes. In the future we
71 should use isl to derive the optimal type for each subexpression. */
73 static int max_mode_int_precision =
74 GET_MODE_PRECISION (mode_for_size (MAX_FIXED_MODE_SIZE, MODE_INT, 0));
75 static int graphite_expression_type_precision = 128 <= max_mode_int_precision ?
76 128 : max_mode_int_precision;
78 struct ast_build_info
80 ast_build_info()
81 : is_parallelizable(false)
82 { };
83 bool is_parallelizable;
86 /* Converts a GMP constant VAL to a tree and returns it. */
88 static tree
89 gmp_cst_to_tree (tree type, mpz_t val)
91 tree t = type ? type : integer_type_node;
92 mpz_t tmp;
94 mpz_init (tmp);
95 mpz_set (tmp, val);
96 wide_int wi = wi::from_mpz (t, tmp, true);
97 mpz_clear (tmp);
99 return wide_int_to_tree (t, wi);
102 /* Verifies properties that GRAPHITE should maintain during translation. */
104 static inline void
105 graphite_verify (void)
107 #ifdef ENABLE_CHECKING
108 verify_loop_structure ();
109 verify_loop_closed_ssa (true);
110 #endif
113 /* IVS_PARAMS maps ISL's scattering and parameter identifiers
114 to corresponding trees. */
116 typedef std::map<isl_id *, tree> ivs_params;
118 /* Free all memory allocated for ISL's identifiers. */
120 void ivs_params_clear (ivs_params &ip)
122 std::map<isl_id *, tree>::iterator it;
123 for (it = ip.begin ();
124 it != ip.end (); it++)
126 isl_id_free (it->first);
130 class translate_isl_ast_to_gimple
132 public:
133 translate_isl_ast_to_gimple (sese r)
134 : region (r)
137 /* Translates an ISL AST node NODE to GCC representation in the
138 context of a SESE. */
139 edge translate_isl_ast (loop_p context_loop, __isl_keep isl_ast_node *node,
140 edge next_e, ivs_params &ip);
142 /* Translates an isl_ast_node_for to Gimple. */
143 edge translate_isl_ast_node_for (loop_p context_loop,
144 __isl_keep isl_ast_node *node,
145 edge next_e, ivs_params &ip);
147 /* Create the loop for a isl_ast_node_for.
149 - NEXT_E is the edge where new generated code should be attached. */
150 edge translate_isl_ast_for_loop (loop_p context_loop,
151 __isl_keep isl_ast_node *node_for,
152 edge next_e,
153 tree type, tree lb, tree ub,
154 ivs_params &ip);
156 /* Translates an isl_ast_node_if to Gimple. */
157 edge translate_isl_ast_node_if (loop_p context_loop,
158 __isl_keep isl_ast_node *node,
159 edge next_e, ivs_params &ip);
161 /* Translates an isl_ast_node_user to Gimple.
163 FIXME: We should remove iv_map.create (loop->num + 1), if it is
164 possible. */
165 edge translate_isl_ast_node_user (__isl_keep isl_ast_node *node,
166 edge next_e, ivs_params &ip);
168 /* Translates an isl_ast_node_block to Gimple. */
169 edge translate_isl_ast_node_block (loop_p context_loop,
170 __isl_keep isl_ast_node *node,
171 edge next_e, ivs_params &ip);
173 /* Converts a unary isl_ast_expr_op expression E to a GCC expression tree of
174 type TYPE. */
175 tree unary_op_to_tree (tree type, __isl_take isl_ast_expr *expr,
176 ivs_params &ip);
178 /* Converts a binary isl_ast_expr_op expression E to a GCC expression tree of
179 type TYPE. */
180 tree binary_op_to_tree (tree type, __isl_take isl_ast_expr *expr,
181 ivs_params &ip);
183 /* Converts a ternary isl_ast_expr_op expression E to a GCC expression tree of
184 type TYPE. */
185 tree ternary_op_to_tree (tree type, __isl_take isl_ast_expr *expr,
186 ivs_params &ip);
188 /* Converts an isl_ast_expr_op expression E with unknown number of arguments
189 to a GCC expression tree of type TYPE. */
190 tree nary_op_to_tree (tree type, __isl_take isl_ast_expr *expr,
191 ivs_params &ip);
193 /* Converts an ISL AST expression E back to a GCC expression tree of
194 type TYPE. */
195 tree gcc_expression_from_isl_expression (tree type,
196 __isl_take isl_ast_expr *,
197 ivs_params &ip);
199 /* Return the tree variable that corresponds to the given isl ast identifier
200 expression (an isl_ast_expr of type isl_ast_expr_id).
202 FIXME: We should replace blind conversation of id's type with derivation
203 of the optimal type when we get the corresponding isl support. Blindly
204 converting type sizes may be problematic when we switch to smaller
205 types. */
206 tree gcc_expression_from_isl_ast_expr_id (tree type,
207 __isl_keep isl_ast_expr *expr_id,
208 ivs_params &ip);
210 /* Converts an isl_ast_expr_int expression E to a GCC expression tree of
211 type TYPE. */
212 tree gcc_expression_from_isl_expr_int (tree type,
213 __isl_take isl_ast_expr *expr);
215 /* Converts an isl_ast_expr_op expression E to a GCC expression tree of
216 type TYPE. */
217 tree gcc_expression_from_isl_expr_op (tree type,
218 __isl_take isl_ast_expr *expr,
219 ivs_params &ip);
221 /* Creates a new LOOP corresponding to isl_ast_node_for. Inserts an
222 induction variable for the new LOOP. New LOOP is attached to CFG
223 starting at ENTRY_EDGE. LOOP is inserted into the loop tree and
224 becomes the child loop of the OUTER_LOOP. NEWIVS_INDEX binds
225 ISL's scattering name to the induction variable created for the
226 loop of STMT. The new induction variable is inserted in the NEWIVS
227 vector and is of type TYPE. */
228 struct loop *graphite_create_new_loop (edge entry_edge,
229 __isl_keep isl_ast_node *node_for,
230 loop_p outer, tree type,
231 tree lb, tree ub, ivs_params &ip);
233 /* All loops generated by create_empty_loop_on_edge have the form of
234 a post-test loop:
239 body of the loop;
240 } while (lower bound < upper bound);
242 We create a new if region protecting the loop to be executed, if
243 the execution count is zero (lower bound > upper bound). */
244 edge graphite_create_new_loop_guard (edge entry_edge,
245 __isl_keep isl_ast_node *node_for,
246 tree *type,
247 tree *lb, tree *ub, ivs_params &ip);
249 /* Creates a new if region corresponding to ISL's cond. */
250 edge graphite_create_new_guard (edge entry_edge,
251 __isl_take isl_ast_expr *if_cond,
252 ivs_params &ip);
254 /* Inserts in iv_map a tuple (OLD_LOOP->num, NEW_NAME) for the induction
255 variables of the loops around GBB in SESE.
257 FIXME: Instead of using a vec<tree> that maps each loop id to a possible
258 chrec, we could consider using a map<int, tree> that maps loop ids to the
259 corresponding tree expressions. */
260 void build_iv_mapping (vec<tree> iv_map, gimple_bb_p gbb,
261 __isl_keep isl_ast_expr *user_expr, ivs_params &ip,
262 sese region);
263 private:
264 sese region;
267 /* Return the tree variable that corresponds to the given isl ast identifier
268 expression (an isl_ast_expr of type isl_ast_expr_id).
270 FIXME: We should replace blind conversation of id's type with derivation
271 of the optimal type when we get the corresponding isl support. Blindly
272 converting type sizes may be problematic when we switch to smaller
273 types. */
275 tree
276 translate_isl_ast_to_gimple::
277 gcc_expression_from_isl_ast_expr_id (tree type,
278 __isl_keep isl_ast_expr *expr_id,
279 ivs_params &ip)
281 gcc_assert (isl_ast_expr_get_type (expr_id) == isl_ast_expr_id);
282 isl_id *tmp_isl_id = isl_ast_expr_get_id (expr_id);
283 std::map<isl_id *, tree>::iterator res;
284 res = ip.find (tmp_isl_id);
285 isl_id_free (tmp_isl_id);
286 gcc_assert (res != ip.end () &&
287 "Could not map isl_id to tree expression");
288 isl_ast_expr_free (expr_id);
289 return fold_convert (type, res->second);
292 /* Converts an isl_ast_expr_int expression E to a GCC expression tree of
293 type TYPE. */
295 tree
296 translate_isl_ast_to_gimple::
297 gcc_expression_from_isl_expr_int (tree type, __isl_take isl_ast_expr *expr)
299 gcc_assert (isl_ast_expr_get_type (expr) == isl_ast_expr_int);
300 isl_val *val = isl_ast_expr_get_val (expr);
301 mpz_t val_mpz_t;
302 mpz_init (val_mpz_t);
303 tree res;
304 if (isl_val_get_num_gmp (val, val_mpz_t) == -1)
305 res = NULL_TREE;
306 else
307 res = gmp_cst_to_tree (type, val_mpz_t);
308 isl_val_free (val);
309 isl_ast_expr_free (expr);
310 mpz_clear (val_mpz_t);
311 return res;
314 /* Converts a binary isl_ast_expr_op expression E to a GCC expression tree of
315 type TYPE. */
317 tree
318 translate_isl_ast_to_gimple::
319 binary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
321 isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0);
322 tree tree_lhs_expr = gcc_expression_from_isl_expression (type, arg_expr, ip);
323 arg_expr = isl_ast_expr_get_op_arg (expr, 1);
324 tree tree_rhs_expr = gcc_expression_from_isl_expression (type, arg_expr, ip);
325 enum isl_ast_op_type expr_type = isl_ast_expr_get_op_type (expr);
326 isl_ast_expr_free (expr);
327 switch (expr_type)
329 case isl_ast_op_add:
330 return fold_build2 (PLUS_EXPR, type, tree_lhs_expr, tree_rhs_expr);
332 case isl_ast_op_sub:
333 return fold_build2 (MINUS_EXPR, type, tree_lhs_expr, tree_rhs_expr);
335 case isl_ast_op_mul:
336 return fold_build2 (MULT_EXPR, type, tree_lhs_expr, tree_rhs_expr);
338 case isl_ast_op_div:
339 return fold_build2 (EXACT_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr);
341 case isl_ast_op_pdiv_q:
342 return fold_build2 (TRUNC_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr);
344 case isl_ast_op_pdiv_r:
345 return fold_build2 (TRUNC_MOD_EXPR, type, tree_lhs_expr, tree_rhs_expr);
347 case isl_ast_op_fdiv_q:
348 return fold_build2 (FLOOR_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr);
350 case isl_ast_op_and:
351 return fold_build2 (TRUTH_ANDIF_EXPR, type,
352 tree_lhs_expr, tree_rhs_expr);
354 case isl_ast_op_or:
355 return fold_build2 (TRUTH_ORIF_EXPR, type, tree_lhs_expr, tree_rhs_expr);
357 case isl_ast_op_eq:
358 return fold_build2 (EQ_EXPR, type, tree_lhs_expr, tree_rhs_expr);
360 case isl_ast_op_le:
361 return fold_build2 (LE_EXPR, type, tree_lhs_expr, tree_rhs_expr);
363 case isl_ast_op_lt:
364 return fold_build2 (LT_EXPR, type, tree_lhs_expr, tree_rhs_expr);
366 case isl_ast_op_ge:
367 return fold_build2 (GE_EXPR, type, tree_lhs_expr, tree_rhs_expr);
369 case isl_ast_op_gt:
370 return fold_build2 (GT_EXPR, type, tree_lhs_expr, tree_rhs_expr);
372 default:
373 gcc_unreachable ();
377 /* Converts a ternary isl_ast_expr_op expression E to a GCC expression tree of
378 type TYPE. */
380 tree
381 translate_isl_ast_to_gimple::
382 ternary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
384 gcc_assert (isl_ast_expr_get_op_type (expr) == isl_ast_op_minus);
385 isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0);
386 tree tree_first_expr
387 = gcc_expression_from_isl_expression (type, arg_expr, ip);
388 arg_expr = isl_ast_expr_get_op_arg (expr, 1);
389 tree tree_second_expr
390 = gcc_expression_from_isl_expression (type, arg_expr, ip);
391 arg_expr = isl_ast_expr_get_op_arg (expr, 2);
392 tree tree_third_expr
393 = gcc_expression_from_isl_expression (type, arg_expr, ip);
394 isl_ast_expr_free (expr);
395 return fold_build3 (COND_EXPR, type, tree_first_expr,
396 tree_second_expr, tree_third_expr);
399 /* Converts a unary isl_ast_expr_op expression E to a GCC expression tree of
400 type TYPE. */
402 tree
403 translate_isl_ast_to_gimple::
404 unary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
406 gcc_assert (isl_ast_expr_get_op_type (expr) == isl_ast_op_minus);
407 isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0);
408 tree tree_expr = gcc_expression_from_isl_expression (type, arg_expr, ip);
409 isl_ast_expr_free (expr);
410 return fold_build1 (NEGATE_EXPR, type, tree_expr);
413 /* Converts an isl_ast_expr_op expression E with unknown number of arguments
414 to a GCC expression tree of type TYPE. */
416 tree
417 translate_isl_ast_to_gimple::
418 nary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
420 enum tree_code op_code;
421 switch (isl_ast_expr_get_op_type (expr))
423 case isl_ast_op_max:
424 op_code = MAX_EXPR;
425 break;
427 case isl_ast_op_min:
428 op_code = MIN_EXPR;
429 break;
431 default:
432 gcc_unreachable ();
434 isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0);
435 tree res = gcc_expression_from_isl_expression (type, arg_expr, ip);
436 int i;
437 for (i = 1; i < isl_ast_expr_get_op_n_arg (expr); i++)
439 arg_expr = isl_ast_expr_get_op_arg (expr, i);
440 tree t = gcc_expression_from_isl_expression (type, arg_expr, ip);
441 res = fold_build2 (op_code, type, res, t);
443 isl_ast_expr_free (expr);
444 return res;
447 /* Converts an isl_ast_expr_op expression E to a GCC expression tree of
448 type TYPE. */
450 tree
451 translate_isl_ast_to_gimple::
452 gcc_expression_from_isl_expr_op (tree type, __isl_take isl_ast_expr *expr,
453 ivs_params &ip)
455 gcc_assert (isl_ast_expr_get_type (expr) == isl_ast_expr_op);
456 switch (isl_ast_expr_get_op_type (expr))
458 /* These isl ast expressions are not supported yet. */
459 case isl_ast_op_error:
460 case isl_ast_op_call:
461 case isl_ast_op_and_then:
462 case isl_ast_op_or_else:
463 case isl_ast_op_select:
464 gcc_unreachable ();
466 case isl_ast_op_max:
467 case isl_ast_op_min:
468 return nary_op_to_tree (type, expr, ip);
470 case isl_ast_op_add:
471 case isl_ast_op_sub:
472 case isl_ast_op_mul:
473 case isl_ast_op_div:
474 case isl_ast_op_pdiv_q:
475 case isl_ast_op_pdiv_r:
476 case isl_ast_op_fdiv_q:
477 case isl_ast_op_and:
478 case isl_ast_op_or:
479 case isl_ast_op_eq:
480 case isl_ast_op_le:
481 case isl_ast_op_lt:
482 case isl_ast_op_ge:
483 case isl_ast_op_gt:
484 return binary_op_to_tree (type, expr, ip);
486 case isl_ast_op_minus:
487 return unary_op_to_tree (type, expr, ip);
489 case isl_ast_op_cond:
490 return ternary_op_to_tree (type, expr, ip);
492 default:
493 gcc_unreachable ();
496 return NULL_TREE;
499 /* Converts an ISL AST expression E back to a GCC expression tree of
500 type TYPE. */
502 tree
503 translate_isl_ast_to_gimple::
504 gcc_expression_from_isl_expression (tree type, __isl_take isl_ast_expr *expr,
505 ivs_params &ip)
507 switch (isl_ast_expr_get_type (expr))
509 case isl_ast_expr_id:
510 return gcc_expression_from_isl_ast_expr_id (type, expr, ip);
512 case isl_ast_expr_int:
513 return gcc_expression_from_isl_expr_int (type, expr);
515 case isl_ast_expr_op:
516 return gcc_expression_from_isl_expr_op (type, expr, ip);
518 default:
519 gcc_unreachable ();
522 return NULL_TREE;
525 /* Creates a new LOOP corresponding to isl_ast_node_for. Inserts an
526 induction variable for the new LOOP. New LOOP is attached to CFG
527 starting at ENTRY_EDGE. LOOP is inserted into the loop tree and
528 becomes the child loop of the OUTER_LOOP. NEWIVS_INDEX binds
529 ISL's scattering name to the induction variable created for the
530 loop of STMT. The new induction variable is inserted in the NEWIVS
531 vector and is of type TYPE. */
533 struct loop *
534 translate_isl_ast_to_gimple::
535 graphite_create_new_loop (edge entry_edge, __isl_keep isl_ast_node *node_for,
536 loop_p outer, tree type, tree lb, tree ub,
537 ivs_params &ip)
539 isl_ast_expr *for_inc = isl_ast_node_for_get_inc (node_for);
540 tree stride = gcc_expression_from_isl_expression (type, for_inc, ip);
541 tree ivvar = create_tmp_var (type, "graphite_IV");
542 tree iv, iv_after_increment;
543 loop_p loop = create_empty_loop_on_edge
544 (entry_edge, lb, stride, ub, ivvar, &iv, &iv_after_increment,
545 outer ? outer : entry_edge->src->loop_father);
547 isl_ast_expr *for_iterator = isl_ast_node_for_get_iterator (node_for);
548 isl_id *id = isl_ast_expr_get_id (for_iterator);
549 std::map<isl_id *, tree>::iterator res;
550 res = ip.find (id);
551 if (ip.count (id))
552 isl_id_free (res->first);
553 ip[id] = iv;
554 isl_ast_expr_free (for_iterator);
555 return loop;
558 /* Create the loop for a isl_ast_node_for.
560 - NEXT_E is the edge where new generated code should be attached. */
562 edge
563 translate_isl_ast_to_gimple::
564 translate_isl_ast_for_loop (loop_p context_loop,
565 __isl_keep isl_ast_node *node_for, edge next_e,
566 tree type, tree lb, tree ub,
567 ivs_params &ip)
569 gcc_assert (isl_ast_node_get_type (node_for) == isl_ast_node_for);
570 struct loop *loop = graphite_create_new_loop (next_e, node_for, context_loop,
571 type, lb, ub, ip);
572 edge last_e = single_exit (loop);
573 edge to_body = single_succ_edge (loop->header);
574 basic_block after = to_body->dest;
576 /* Create a basic block for loop close phi nodes. */
577 last_e = single_succ_edge (split_edge (last_e));
579 /* Translate the body of the loop. */
580 isl_ast_node *for_body = isl_ast_node_for_get_body (node_for);
581 next_e = translate_isl_ast (loop, for_body, to_body, ip);
582 isl_ast_node_free (for_body);
583 redirect_edge_succ_nodup (next_e, after);
584 set_immediate_dominator (CDI_DOMINATORS, next_e->dest, next_e->src);
586 if (flag_loop_parallelize_all)
588 isl_id *id = isl_ast_node_get_annotation (node_for);
589 gcc_assert (id);
590 ast_build_info *for_info = (ast_build_info *) isl_id_get_user (id);
591 loop->can_be_parallel = for_info->is_parallelizable;
592 free (for_info);
593 isl_id_free (id);
596 return last_e;
599 /* We use this function to get the upper bound because of the form,
600 which is used by isl to represent loops:
602 for (iterator = init; cond; iterator += inc)
610 The loop condition is an arbitrary expression, which contains the
611 current loop iterator.
613 (e.g. iterator + 3 < B && C > iterator + A)
615 We have to know the upper bound of the iterator to generate a loop
616 in Gimple form. It can be obtained from the special representation
617 of the loop condition, which is generated by isl,
618 if the ast_build_atomic_upper_bound option is set. In this case,
619 isl generates a loop condition that consists of the current loop
620 iterator, + an operator (< or <=) and an expression not involving
621 the iterator, which is processed and returned by this function.
623 (e.g iterator <= upper-bound-expression-without-iterator) */
625 static __isl_give isl_ast_expr *
626 get_upper_bound (__isl_keep isl_ast_node *node_for)
628 gcc_assert (isl_ast_node_get_type (node_for) == isl_ast_node_for);
629 isl_ast_expr *for_cond = isl_ast_node_for_get_cond (node_for);
630 gcc_assert (isl_ast_expr_get_type (for_cond) == isl_ast_expr_op);
631 isl_ast_expr *res;
632 switch (isl_ast_expr_get_op_type (for_cond))
634 case isl_ast_op_le:
635 res = isl_ast_expr_get_op_arg (for_cond, 1);
636 break;
638 case isl_ast_op_lt:
640 // (iterator < ub) => (iterator <= ub - 1)
641 isl_val *one =
642 isl_val_int_from_si (isl_ast_expr_get_ctx (for_cond), 1);
643 isl_ast_expr *ub = isl_ast_expr_get_op_arg (for_cond, 1);
644 res = isl_ast_expr_sub (ub, isl_ast_expr_from_val (one));
645 break;
648 default:
649 gcc_unreachable ();
651 isl_ast_expr_free (for_cond);
652 return res;
655 /* All loops generated by create_empty_loop_on_edge have the form of
656 a post-test loop:
661 body of the loop;
662 } while (lower bound < upper bound);
664 We create a new if region protecting the loop to be executed, if
665 the execution count is zero (lower bound > upper bound). */
667 edge
668 translate_isl_ast_to_gimple::
669 graphite_create_new_loop_guard (edge entry_edge,
670 __isl_keep isl_ast_node *node_for, tree *type,
671 tree *lb, tree *ub, ivs_params &ip)
673 gcc_assert (isl_ast_node_get_type (node_for) == isl_ast_node_for);
674 tree cond_expr;
675 edge exit_edge;
677 *type =
678 build_nonstandard_integer_type (graphite_expression_type_precision, 0);
679 isl_ast_expr *for_init = isl_ast_node_for_get_init (node_for);
680 *lb = gcc_expression_from_isl_expression (*type, for_init, ip);
681 isl_ast_expr *upper_bound = get_upper_bound (node_for);
682 *ub = gcc_expression_from_isl_expression (*type, upper_bound, ip);
684 /* When ub is simply a constant or a parameter, use lb <= ub. */
685 if (TREE_CODE (*ub) == INTEGER_CST || TREE_CODE (*ub) == SSA_NAME)
686 cond_expr = fold_build2 (LE_EXPR, boolean_type_node, *lb, *ub);
687 else
689 tree one = (POINTER_TYPE_P (*type)
690 ? convert_to_ptrofftype (integer_one_node)
691 : fold_convert (*type, integer_one_node));
692 /* Adding +1 and using LT_EXPR helps with loop latches that have a
693 loop iteration count of "PARAMETER - 1". For PARAMETER == 0 this
694 becomes 2^k-1 due to integer overflow, and the condition lb <= ub
695 is true, even if we do not want this. However lb < ub + 1 is false,
696 as expected. */
697 tree ub_one = fold_build2 (POINTER_TYPE_P (*type) ? POINTER_PLUS_EXPR
698 : PLUS_EXPR, *type, *ub, one);
700 cond_expr = fold_build2 (LT_EXPR, boolean_type_node, *lb, ub_one);
703 exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr);
705 return exit_edge;
708 /* Translates an isl_ast_node_for to Gimple. */
710 edge
711 translate_isl_ast_to_gimple::
712 translate_isl_ast_node_for (loop_p context_loop, __isl_keep isl_ast_node *node,
713 edge next_e, ivs_params &ip)
715 gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_for);
716 tree type, lb, ub;
717 edge last_e = graphite_create_new_loop_guard (next_e, node, &type,
718 &lb, &ub, ip);
719 edge true_e = get_true_edge_from_guard_bb (next_e->dest);
721 translate_isl_ast_for_loop (context_loop, node, true_e,
722 type, lb, ub, ip);
723 return last_e;
726 /* Inserts in iv_map a tuple (OLD_LOOP->num, NEW_NAME) for the induction
727 variables of the loops around GBB in SESE.
729 FIXME: Instead of using a vec<tree> that maps each loop id to a possible
730 chrec, we could consider using a map<int, tree> that maps loop ids to the
731 corresponding tree expressions. */
733 void
734 translate_isl_ast_to_gimple::
735 build_iv_mapping (vec<tree> iv_map, gimple_bb_p gbb,
736 __isl_keep isl_ast_expr *user_expr, ivs_params &ip,
737 sese region)
739 gcc_assert (isl_ast_expr_get_type (user_expr) == isl_ast_expr_op &&
740 isl_ast_expr_get_op_type (user_expr) == isl_ast_op_call);
741 int i;
742 isl_ast_expr *arg_expr;
743 for (i = 1; i < isl_ast_expr_get_op_n_arg (user_expr); i++)
745 arg_expr = isl_ast_expr_get_op_arg (user_expr, i);
746 tree type =
747 build_nonstandard_integer_type (graphite_expression_type_precision, 0);
748 tree t = gcc_expression_from_isl_expression (type, arg_expr, ip);
749 loop_p old_loop = gbb_loop_at_index (gbb, region, i - 1);
750 iv_map[old_loop->num] = t;
755 /* Translates an isl_ast_node_user to Gimple.
757 FIXME: We should remove iv_map.create (loop->num + 1), if it is possible. */
759 edge
760 translate_isl_ast_to_gimple::
761 translate_isl_ast_node_user (__isl_keep isl_ast_node *node,
762 edge next_e, ivs_params &ip)
764 gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_user);
765 isl_ast_expr *user_expr = isl_ast_node_user_get_expr (node);
766 isl_ast_expr *name_expr = isl_ast_expr_get_op_arg (user_expr, 0);
767 gcc_assert (isl_ast_expr_get_type (name_expr) == isl_ast_expr_id);
768 isl_id *name_id = isl_ast_expr_get_id (name_expr);
769 poly_bb_p pbb = (poly_bb_p) isl_id_get_user (name_id);
770 gcc_assert (pbb);
771 gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
772 vec<tree> iv_map;
773 isl_ast_expr_free (name_expr);
774 isl_id_free (name_id);
776 gcc_assert (GBB_BB (gbb) != ENTRY_BLOCK_PTR_FOR_FN (cfun) &&
777 "The entry block should not even appear within a scop");
779 int nb_loops = number_of_loops (cfun);
780 iv_map.create (nb_loops);
781 iv_map.safe_grow_cleared (nb_loops);
783 build_iv_mapping (iv_map, gbb, user_expr, ip, SCOP_REGION (pbb->scop));
784 isl_ast_expr_free (user_expr);
785 next_e = copy_bb_and_scalar_dependences (GBB_BB (gbb),
786 SCOP_REGION (pbb->scop), next_e,
787 iv_map,
788 &graphite_regenerate_error);
789 iv_map.release ();
790 mark_virtual_operands_for_renaming (cfun);
791 update_ssa (TODO_update_ssa);
792 return next_e;
795 /* Translates an isl_ast_node_block to Gimple. */
797 edge
798 translate_isl_ast_to_gimple::
799 translate_isl_ast_node_block (loop_p context_loop,
800 __isl_keep isl_ast_node *node,
801 edge next_e, ivs_params &ip)
803 gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_block);
804 isl_ast_node_list *node_list = isl_ast_node_block_get_children (node);
805 int i;
806 for (i = 0; i < isl_ast_node_list_n_ast_node (node_list); i++)
808 isl_ast_node *tmp_node = isl_ast_node_list_get_ast_node (node_list, i);
809 next_e = translate_isl_ast (context_loop, tmp_node, next_e, ip);
810 isl_ast_node_free (tmp_node);
812 isl_ast_node_list_free (node_list);
813 return next_e;
816 /* Creates a new if region corresponding to ISL's cond. */
818 edge
819 translate_isl_ast_to_gimple::
820 graphite_create_new_guard (edge entry_edge, __isl_take isl_ast_expr *if_cond,
821 ivs_params &ip)
823 tree type =
824 build_nonstandard_integer_type (graphite_expression_type_precision, 0);
825 tree cond_expr = gcc_expression_from_isl_expression (type, if_cond, ip);
826 edge exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr);
827 return exit_edge;
830 /* Translates an isl_ast_node_if to Gimple. */
832 edge
833 translate_isl_ast_to_gimple::
834 translate_isl_ast_node_if (loop_p context_loop,
835 __isl_keep isl_ast_node *node,
836 edge next_e, ivs_params &ip)
838 gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_if);
839 isl_ast_expr *if_cond = isl_ast_node_if_get_cond (node);
840 edge last_e = graphite_create_new_guard (next_e, if_cond, ip);
842 edge true_e = get_true_edge_from_guard_bb (next_e->dest);
843 isl_ast_node *then_node = isl_ast_node_if_get_then (node);
844 translate_isl_ast (context_loop, then_node, true_e, ip);
845 isl_ast_node_free (then_node);
847 edge false_e = get_false_edge_from_guard_bb (next_e->dest);
848 isl_ast_node *else_node = isl_ast_node_if_get_else (node);
849 if (isl_ast_node_get_type (else_node) != isl_ast_node_error)
850 translate_isl_ast (context_loop, else_node, false_e, ip);
851 isl_ast_node_free (else_node);
852 return last_e;
855 /* Translates an ISL AST node NODE to GCC representation in the
856 context of a SESE. */
858 edge
859 translate_isl_ast_to_gimple::translate_isl_ast (loop_p context_loop,
860 __isl_keep isl_ast_node *node,
861 edge next_e, ivs_params &ip)
863 switch (isl_ast_node_get_type (node))
865 case isl_ast_node_error:
866 gcc_unreachable ();
868 case isl_ast_node_for:
869 return translate_isl_ast_node_for (context_loop, node,
870 next_e, ip);
872 case isl_ast_node_if:
873 return translate_isl_ast_node_if (context_loop, node,
874 next_e, ip);
876 case isl_ast_node_user:
877 return translate_isl_ast_node_user (node, next_e, ip);
879 case isl_ast_node_block:
880 return translate_isl_ast_node_block (context_loop, node,
881 next_e, ip);
883 default:
884 gcc_unreachable ();
888 /* Prints NODE to FILE. */
890 void
891 print_isl_ast_node (FILE *file, __isl_keep isl_ast_node *node,
892 __isl_keep isl_ctx *ctx)
894 isl_printer *prn = isl_printer_to_file (ctx, file);
895 prn = isl_printer_set_output_format (prn, ISL_FORMAT_C);
896 prn = isl_printer_print_ast_node (prn, node);
897 prn = isl_printer_print_str (prn, "\n");
898 isl_printer_free (prn);
901 /* Add ISL's parameter identifiers and corresponding.trees to ivs_params */
903 static void
904 add_parameters_to_ivs_params (scop_p scop, ivs_params &ip)
906 sese region = SCOP_REGION (scop);
907 unsigned nb_parameters = isl_set_dim (scop->context, isl_dim_param);
908 gcc_assert (nb_parameters == SESE_PARAMS (region).length ());
909 unsigned i;
910 for (i = 0; i < nb_parameters; i++)
912 isl_id *tmp_id = isl_set_get_dim_id (scop->context, isl_dim_param, i);
913 ip[tmp_id] = SESE_PARAMS (region)[i];
918 /* Generates a build, which specifies the constraints on the parameters. */
920 static __isl_give isl_ast_build *
921 generate_isl_context (scop_p scop)
923 isl_set *context_isl = isl_set_params (isl_set_copy (scop->context));
924 return isl_ast_build_from_context (context_isl);
927 /* Get the maximal number of schedule dimensions in the scop SCOP. */
929 static
930 int get_max_schedule_dimensions (scop_p scop)
932 int i;
933 poly_bb_p pbb;
934 int schedule_dims = 0;
936 FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
938 int pbb_schedule_dims = isl_map_dim (pbb->transformed, isl_dim_out);
939 if (pbb_schedule_dims > schedule_dims)
940 schedule_dims = pbb_schedule_dims;
943 return schedule_dims;
946 /* Extend the schedule to NB_SCHEDULE_DIMS schedule dimensions.
948 For schedules with different dimensionality, the isl AST generator can not
949 define an order and will just randomly choose an order. The solution to this
950 problem is to extend all schedules to the maximal number of schedule
951 dimensions (using '0's for the remaining values). */
953 static __isl_give isl_map *
954 extend_schedule (__isl_take isl_map *schedule, int nb_schedule_dims)
956 int tmp_dims = isl_map_dim (schedule, isl_dim_out);
957 schedule =
958 isl_map_add_dims (schedule, isl_dim_out, nb_schedule_dims - tmp_dims);
959 isl_val *zero =
960 isl_val_int_from_si (isl_map_get_ctx (schedule), 0);
961 int i;
962 for (i = tmp_dims; i < nb_schedule_dims; i++)
964 schedule =
965 isl_map_fix_val (schedule, isl_dim_out, i, isl_val_copy (zero));
967 isl_val_free (zero);
968 return schedule;
971 /* Set the separation_class option for unroll and jam. */
973 static __isl_give isl_union_map *
974 generate_luj_sepclass_opt (scop_p scop, __isl_take isl_union_set *domain,
975 int dim, int cl)
977 isl_map *map;
978 isl_space *space, *space_sep;
979 isl_ctx *ctx;
980 isl_union_map *mapu;
981 int nsched = get_max_schedule_dimensions (scop);
983 ctx = scop->ctx;
984 space_sep = isl_space_alloc (ctx, 0, 1, 1);
985 space_sep = isl_space_wrap (space_sep);
986 space_sep = isl_space_set_tuple_name (space_sep, isl_dim_set,
987 "separation_class");
988 space = isl_set_get_space (scop->context);
989 space_sep = isl_space_align_params (space_sep, isl_space_copy(space));
990 space = isl_space_map_from_domain_and_range (space, space_sep);
991 space = isl_space_add_dims (space,isl_dim_in, nsched);
992 map = isl_map_universe (space);
993 isl_map_fix_si (map,isl_dim_out,0,dim);
994 isl_map_fix_si (map,isl_dim_out,1,cl);
996 mapu = isl_union_map_intersect_domain (isl_union_map_from_map (map),
997 domain);
998 return (mapu);
1001 /* Compute the separation class for loop unroll and jam. */
1003 static __isl_give isl_union_set *
1004 generate_luj_sepclass (scop_p scop)
1006 int i;
1007 poly_bb_p pbb;
1008 isl_union_set *domain_isl;
1010 domain_isl = isl_union_set_empty (isl_set_get_space (scop->context));
1012 FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
1014 isl_set *bb_domain;
1015 isl_set *bb_domain_s;
1017 if (pbb->map_sepclass == NULL)
1018 continue;
1020 if (isl_set_is_empty (pbb->domain))
1021 continue;
1023 bb_domain = isl_set_copy (pbb->domain);
1024 bb_domain_s = isl_set_apply (bb_domain, pbb->map_sepclass);
1025 pbb->map_sepclass = NULL;
1027 domain_isl =
1028 isl_union_set_union (domain_isl, isl_union_set_from_set (bb_domain_s));
1031 return domain_isl;
1034 /* Set the AST built options for loop unroll and jam. */
1036 static __isl_give isl_union_map *
1037 generate_luj_options (scop_p scop)
1039 isl_union_set *domain_isl;
1040 isl_union_map *options_isl_ss;
1041 isl_union_map *options_isl =
1042 isl_union_map_empty (isl_set_get_space (scop->context));
1043 int dim = get_max_schedule_dimensions (scop) - 1;
1044 int dim1 = dim - PARAM_VALUE (PARAM_LOOP_UNROLL_JAM_DEPTH);
1046 if (!flag_loop_unroll_jam)
1047 return options_isl;
1049 domain_isl = generate_luj_sepclass (scop);
1051 options_isl_ss = generate_luj_sepclass_opt (scop, domain_isl, dim1, 0);
1052 options_isl = isl_union_map_union (options_isl, options_isl_ss);
1054 return options_isl;
1057 /* Generates a schedule, which specifies an order used to
1058 visit elements in a domain. */
1060 static __isl_give isl_union_map *
1061 generate_isl_schedule (scop_p scop)
1063 int nb_schedule_dims = get_max_schedule_dimensions (scop);
1064 int i;
1065 poly_bb_p pbb;
1066 isl_union_map *schedule_isl =
1067 isl_union_map_empty (isl_set_get_space (scop->context));
1069 FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
1071 /* Dead code elimination: when the domain of a PBB is empty,
1072 don't generate code for the PBB. */
1073 if (isl_set_is_empty (pbb->domain))
1074 continue;
1076 isl_map *bb_schedule = isl_map_copy (pbb->transformed);
1077 bb_schedule = isl_map_intersect_domain (bb_schedule,
1078 isl_set_copy (pbb->domain));
1079 bb_schedule = extend_schedule (bb_schedule, nb_schedule_dims);
1080 schedule_isl =
1081 isl_union_map_union (schedule_isl,
1082 isl_union_map_from_map (bb_schedule));
1084 return schedule_isl;
1087 /* This method is executed before the construction of a for node. */
1088 static __isl_give isl_id *
1089 ast_build_before_for (__isl_keep isl_ast_build *build, void *user)
1091 isl_union_map *dependences = (isl_union_map *) user;
1092 ast_build_info *for_info = XNEW (struct ast_build_info);
1093 isl_union_map *schedule = isl_ast_build_get_schedule (build);
1094 isl_space *schedule_space = isl_ast_build_get_schedule_space (build);
1095 int dimension = isl_space_dim (schedule_space, isl_dim_out);
1096 for_info->is_parallelizable =
1097 !carries_deps (schedule, dependences, dimension);
1098 isl_union_map_free (schedule);
1099 isl_space_free (schedule_space);
1100 isl_id *id = isl_id_alloc (isl_ast_build_get_ctx (build), "", for_info);
1101 return id;
1104 /* Set the separate option for all dimensions.
1105 This helps to reduce control overhead.
1106 Set the options for unroll and jam. */
1108 static __isl_give isl_ast_build *
1109 set_options (__isl_take isl_ast_build *control,
1110 __isl_keep isl_union_map *schedule,
1111 __isl_take isl_union_map *opt_luj)
1113 isl_ctx *ctx = isl_union_map_get_ctx (schedule);
1114 isl_space *range_space = isl_space_set_alloc (ctx, 0, 1);
1115 range_space =
1116 isl_space_set_tuple_name (range_space, isl_dim_set, "separate");
1117 isl_union_set *range =
1118 isl_union_set_from_set (isl_set_universe (range_space));
1119 isl_union_set *domain = isl_union_map_range (isl_union_map_copy (schedule));
1120 domain = isl_union_set_universe (domain);
1121 isl_union_map *options = isl_union_map_from_domain_and_range (domain, range);
1123 options = isl_union_map_union (options, opt_luj);
1125 return isl_ast_build_set_options (control, options);
1128 static __isl_give isl_ast_node *
1129 scop_to_isl_ast (scop_p scop, ivs_params &ip)
1131 /* Generate loop upper bounds that consist of the current loop iterator,
1132 an operator (< or <=) and an expression not involving the iterator.
1133 If this option is not set, then the current loop iterator may appear several
1134 times in the upper bound. See the isl manual for more details. */
1135 isl_options_set_ast_build_atomic_upper_bound (scop->ctx, true);
1137 add_parameters_to_ivs_params (scop, ip);
1139 isl_union_map *options_luj = generate_luj_options (scop);
1141 isl_union_map *schedule_isl = generate_isl_schedule (scop);
1142 isl_ast_build *context_isl = generate_isl_context (scop);
1144 context_isl = set_options (context_isl, schedule_isl, options_luj);
1146 isl_union_map *dependences = NULL;
1147 if (flag_loop_parallelize_all)
1149 dependences = scop_get_dependences (scop);
1150 context_isl =
1151 isl_ast_build_set_before_each_for (context_isl, ast_build_before_for,
1152 dependences);
1154 isl_ast_node *ast_isl = isl_ast_build_ast_from_schedule (context_isl,
1155 schedule_isl);
1156 if(dependences)
1157 isl_union_map_free (dependences);
1158 isl_ast_build_free (context_isl);
1159 return ast_isl;
1162 /* GIMPLE Loop Generator: generates loops from STMT in GIMPLE form for
1163 the given SCOP. Return true if code generation succeeded.
1165 FIXME: This is not yet a full implementation of the code generator
1166 with ISL ASTs. Generation of GIMPLE code has to be completed. */
1168 bool
1169 graphite_regenerate_ast_isl (scop_p scop)
1171 loop_p context_loop;
1172 sese region = SCOP_REGION (scop);
1173 ifsese if_region = NULL;
1174 isl_ast_node *root_node;
1175 ivs_params ip;
1177 timevar_push (TV_GRAPHITE_CODE_GEN);
1178 graphite_regenerate_error = false;
1179 root_node = scop_to_isl_ast (scop, ip);
1181 if (dump_file && (dump_flags & TDF_DETAILS))
1183 fprintf (dump_file, "\nISL AST generated by ISL: \n");
1184 print_isl_ast_node (dump_file, root_node, scop->ctx);
1185 fprintf (dump_file, "\n");
1188 recompute_all_dominators ();
1189 graphite_verify ();
1191 if_region = move_sese_in_condition (region);
1192 sese_insert_phis_for_liveouts (region,
1193 if_region->region->exit->src,
1194 if_region->false_region->exit,
1195 if_region->true_region->exit);
1196 recompute_all_dominators ();
1197 graphite_verify ();
1199 context_loop = SESE_ENTRY (region)->src->loop_father;
1201 translate_isl_ast_to_gimple t (region);
1203 t.translate_isl_ast (context_loop, root_node, if_region->true_region->entry,
1204 ip);
1206 mark_virtual_operands_for_renaming (cfun);
1207 update_ssa (TODO_update_ssa);
1209 graphite_verify ();
1210 scev_reset ();
1211 recompute_all_dominators ();
1212 graphite_verify ();
1214 if (graphite_regenerate_error)
1215 set_ifsese_condition (if_region, integer_zero_node);
1217 free (if_region->true_region);
1218 free (if_region->region);
1219 free (if_region);
1221 ivs_params_clear (ip);
1222 isl_ast_node_free (root_node);
1223 timevar_pop (TV_GRAPHITE_CODE_GEN);
1225 if (dump_file && (dump_flags & TDF_DETAILS))
1227 loop_p loop;
1228 int num_no_dependency = 0;
1230 FOR_EACH_LOOP (loop, 0)
1231 if (loop->can_be_parallel)
1232 num_no_dependency++;
1234 fprintf (dump_file, "\n%d loops carried no dependency.\n",
1235 num_no_dependency);
1238 return !graphite_regenerate_error;
1240 #endif /* HAVE_isl */