[Ada] Empty CUDA_Global procedures when compiling for host
[official-gcc.git] / gcc / graphite-isl-ast-to-gimple.c
blob1ad68a1d4735e76b3fc8a71e12273ae1d6047587
1 /* Translation of isl AST to Gimple.
2 Copyright (C) 2014-2021 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 #define INCLUDE_ISL
23 #include "config.h"
25 #ifdef HAVE_isl
27 #include "system.h"
28 #include "coretypes.h"
29 #include "backend.h"
30 #include "cfghooks.h"
31 #include "tree.h"
32 #include "gimple.h"
33 #include "ssa.h"
34 #include "fold-const.h"
35 #include "gimple-fold.h"
36 #include "gimple-iterator.h"
37 #include "gimplify.h"
38 #include "gimplify-me.h"
39 #include "tree-eh.h"
40 #include "tree-ssa-loop.h"
41 #include "tree-ssa-operands.h"
42 #include "tree-ssa-propagate.h"
43 #include "tree-pass.h"
44 #include "cfgloop.h"
45 #include "tree-data-ref.h"
46 #include "tree-ssa-loop-manip.h"
47 #include "tree-scalar-evolution.h"
48 #include "gimple-ssa.h"
49 #include "tree-phinodes.h"
50 #include "tree-into-ssa.h"
51 #include "ssa-iterators.h"
52 #include "tree-cfg.h"
53 #include "gimple-pretty-print.h"
54 #include "cfganal.h"
55 #include "value-prof.h"
56 #include "tree-ssa.h"
57 #include "tree-vectorizer.h"
58 #include "graphite.h"
60 struct ast_build_info
62 ast_build_info()
63 : is_parallelizable(false)
64 { }
65 bool is_parallelizable;
68 /* IVS_PARAMS maps isl's scattering and parameter identifiers
69 to corresponding trees. */
71 typedef hash_map<isl_id *, tree> ivs_params;
73 /* Free all memory allocated for isl's identifiers. */
75 static void ivs_params_clear (ivs_params &ip)
77 for (auto it = ip.begin (); it != ip.end (); ++it)
78 isl_id_free ((*it).first);
81 /* Set the "separate" option for the schedule node. */
83 static isl_schedule_node *
84 set_separate_option (__isl_take isl_schedule_node *node, void *user)
86 if (user)
87 return node;
89 if (isl_schedule_node_get_type (node) != isl_schedule_node_band)
90 return node;
92 /* Set the "separate" option unless it is set earlier to another option. */
93 if (isl_schedule_node_band_member_get_ast_loop_type (node, 0)
94 == isl_ast_loop_default)
95 return isl_schedule_node_band_member_set_ast_loop_type
96 (node, 0, isl_ast_loop_separate);
98 return node;
101 /* Print SCHEDULE under an AST form on file F. */
103 void
104 print_schedule_ast (FILE *f, __isl_keep isl_schedule *schedule, scop_p scop)
106 isl_set *set = isl_set_params (isl_set_copy (scop->param_context));
107 isl_ast_build *context = isl_ast_build_from_context (set);
108 isl_ast_node *ast
109 = isl_ast_build_node_from_schedule (context, isl_schedule_copy (schedule));
110 isl_ast_build_free (context);
111 print_isl_ast (f, ast);
112 isl_ast_node_free (ast);
115 DEBUG_FUNCTION void
116 debug_schedule_ast (__isl_keep isl_schedule *s, scop_p scop)
118 print_schedule_ast (stderr, s, scop);
121 enum phi_node_kind
123 unknown_phi,
124 loop_phi,
125 close_phi,
126 cond_phi
129 class translate_isl_ast_to_gimple
131 public:
132 translate_isl_ast_to_gimple (sese_info_p r);
133 edge translate_isl_ast (loop_p context_loop, __isl_keep isl_ast_node *node,
134 edge next_e, ivs_params &ip);
135 edge translate_isl_ast_node_for (loop_p context_loop,
136 __isl_keep isl_ast_node *node,
137 edge next_e, ivs_params &ip);
138 edge translate_isl_ast_for_loop (loop_p context_loop,
139 __isl_keep isl_ast_node *node_for,
140 edge next_e,
141 tree type, tree lb, tree ub,
142 ivs_params &ip);
143 edge translate_isl_ast_node_if (loop_p context_loop,
144 __isl_keep isl_ast_node *node,
145 edge next_e, ivs_params &ip);
146 edge translate_isl_ast_node_user (__isl_keep isl_ast_node *node,
147 edge next_e, ivs_params &ip);
148 edge translate_isl_ast_node_block (loop_p context_loop,
149 __isl_keep isl_ast_node *node,
150 edge next_e, ivs_params &ip);
151 tree unary_op_to_tree (tree type, __isl_take isl_ast_expr *expr,
152 ivs_params &ip);
153 tree binary_op_to_tree (tree type, __isl_take isl_ast_expr *expr,
154 ivs_params &ip);
155 tree ternary_op_to_tree (tree type, __isl_take isl_ast_expr *expr,
156 ivs_params &ip);
157 tree nary_op_to_tree (tree type, __isl_take isl_ast_expr *expr,
158 ivs_params &ip);
159 tree gcc_expression_from_isl_expression (tree type,
160 __isl_take isl_ast_expr *,
161 ivs_params &ip);
162 tree gcc_expression_from_isl_ast_expr_id (tree type,
163 __isl_keep isl_ast_expr *expr_id,
164 ivs_params &ip);
165 widest_int widest_int_from_isl_expr_int (__isl_keep isl_ast_expr *expr);
166 tree gcc_expression_from_isl_expr_int (tree type,
167 __isl_take isl_ast_expr *expr);
168 tree gcc_expression_from_isl_expr_op (tree type,
169 __isl_take isl_ast_expr *expr,
170 ivs_params &ip);
171 struct loop *graphite_create_new_loop (edge entry_edge,
172 __isl_keep isl_ast_node *node_for,
173 loop_p outer, tree type,
174 tree lb, tree ub, ivs_params &ip);
175 edge graphite_create_new_guard (edge entry_edge,
176 __isl_take isl_ast_expr *if_cond,
177 ivs_params &ip);
178 void build_iv_mapping (vec<tree> iv_map, gimple_poly_bb_p gbb,
179 __isl_keep isl_ast_expr *user_expr, ivs_params &ip,
180 sese_l &region);
181 void add_parameters_to_ivs_params (scop_p scop, ivs_params &ip);
182 __isl_give isl_ast_build *generate_isl_context (scop_p scop);
184 __isl_give isl_ast_node * scop_to_isl_ast (scop_p scop);
186 tree get_rename_from_scev (tree old_name, gimple_seq *stmts, loop_p loop,
187 vec<tree> iv_map);
188 void graphite_copy_stmts_from_block (basic_block bb, basic_block new_bb,
189 vec<tree> iv_map);
190 edge copy_bb_and_scalar_dependences (basic_block bb, edge next_e,
191 vec<tree> iv_map);
192 void set_rename (tree old_name, tree expr);
193 void gsi_insert_earliest (gimple_seq seq);
194 bool codegen_error_p () const { return codegen_error; }
196 void set_codegen_error ()
198 codegen_error = true;
199 gcc_assert (! flag_checking
200 || param_graphite_allow_codegen_errors);
203 bool is_constant (tree op) const
205 return TREE_CODE (op) == INTEGER_CST
206 || TREE_CODE (op) == REAL_CST
207 || TREE_CODE (op) == COMPLEX_CST
208 || TREE_CODE (op) == VECTOR_CST;
211 private:
212 /* The region to be translated. */
213 sese_info_p region;
215 /* This flag is set when an error occurred during the translation of isl AST
216 to Gimple. */
217 bool codegen_error;
219 /* A vector of all the edges at if_condition merge points. */
220 auto_vec<edge, 2> merge_points;
222 tree graphite_expr_type;
225 translate_isl_ast_to_gimple::translate_isl_ast_to_gimple (sese_info_p r)
226 : region (r), codegen_error (false)
228 /* We always try to use signed 128 bit types, but fall back to smaller types
229 in case a platform does not provide types of these sizes. In the future we
230 should use isl to derive the optimal type for each subexpression. */
231 int max_mode_int_precision
232 = GET_MODE_PRECISION (int_mode_for_size (MAX_FIXED_MODE_SIZE, 0).require ());
233 int graphite_expr_type_precision
234 = 128 <= max_mode_int_precision ? 128 : max_mode_int_precision;
235 graphite_expr_type
236 = build_nonstandard_integer_type (graphite_expr_type_precision, 0);
239 /* Return the tree variable that corresponds to the given isl ast identifier
240 expression (an isl_ast_expr of type isl_ast_expr_id).
242 FIXME: We should replace blind conversion of id's type with derivation
243 of the optimal type when we get the corresponding isl support. Blindly
244 converting type sizes may be problematic when we switch to smaller
245 types. */
247 tree translate_isl_ast_to_gimple::
248 gcc_expression_from_isl_ast_expr_id (tree type,
249 __isl_take isl_ast_expr *expr_id,
250 ivs_params &ip)
252 gcc_assert (isl_ast_expr_get_type (expr_id) == isl_ast_expr_id);
253 isl_id *tmp_isl_id = isl_ast_expr_get_id (expr_id);
254 tree *tp = ip.get (tmp_isl_id);
255 isl_id_free (tmp_isl_id);
256 gcc_assert (tp && "Could not map isl_id to tree expression");
257 isl_ast_expr_free (expr_id);
258 tree t = *tp;
259 if (useless_type_conversion_p (type, TREE_TYPE (t)))
260 return t;
261 if (POINTER_TYPE_P (TREE_TYPE (t))
262 && !POINTER_TYPE_P (type) && !ptrofftype_p (type))
263 t = fold_convert (sizetype, t);
264 return fold_convert (type, t);
267 /* Converts an isl_ast_expr_int expression E to a widest_int.
268 Raises a code generation error when the constant doesn't fit. */
270 widest_int translate_isl_ast_to_gimple::
271 widest_int_from_isl_expr_int (__isl_keep isl_ast_expr *expr)
273 gcc_assert (isl_ast_expr_get_type (expr) == isl_ast_expr_int);
274 isl_val *val = isl_ast_expr_get_val (expr);
275 size_t n = isl_val_n_abs_num_chunks (val, sizeof (HOST_WIDE_INT));
276 HOST_WIDE_INT *chunks = XALLOCAVEC (HOST_WIDE_INT, n);
277 if (n > WIDE_INT_MAX_ELTS
278 || isl_val_get_abs_num_chunks (val, sizeof (HOST_WIDE_INT), chunks) == -1)
280 isl_val_free (val);
281 set_codegen_error ();
282 return 0;
284 widest_int wi = widest_int::from_array (chunks, n, true);
285 if (isl_val_is_neg (val))
286 wi = -wi;
287 isl_val_free (val);
288 return wi;
291 /* Converts an isl_ast_expr_int expression E to a GCC expression tree of
292 type TYPE. Raises a code generation error when the constant doesn't fit. */
294 tree translate_isl_ast_to_gimple::
295 gcc_expression_from_isl_expr_int (tree type, __isl_take isl_ast_expr *expr)
297 widest_int wi = widest_int_from_isl_expr_int (expr);
298 isl_ast_expr_free (expr);
299 if (codegen_error_p ())
300 return NULL_TREE;
301 if (wi::min_precision (wi, TYPE_SIGN (type)) > TYPE_PRECISION (type))
303 set_codegen_error ();
304 return NULL_TREE;
306 return wide_int_to_tree (type, wi);
309 /* Converts a binary isl_ast_expr_op expression E to a GCC expression tree of
310 type TYPE. */
312 tree translate_isl_ast_to_gimple::
313 binary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
315 enum isl_ast_op_type expr_type = isl_ast_expr_get_op_type (expr);
316 isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0);
317 tree tree_lhs_expr = gcc_expression_from_isl_expression (type, arg_expr, ip);
318 arg_expr = isl_ast_expr_get_op_arg (expr, 1);
319 isl_ast_expr_free (expr);
321 /* From our constraint generation we may get modulo operations that
322 we cannot represent explicitely but that are no-ops for TYPE.
323 Elide those. */
324 if ((expr_type == isl_ast_op_pdiv_r
325 || expr_type == isl_ast_op_zdiv_r
326 || expr_type == isl_ast_op_add)
327 && isl_ast_expr_get_type (arg_expr) == isl_ast_expr_int
328 && (wi::exact_log2 (widest_int_from_isl_expr_int (arg_expr))
329 >= TYPE_PRECISION (type)))
331 isl_ast_expr_free (arg_expr);
332 return tree_lhs_expr;
335 tree tree_rhs_expr = gcc_expression_from_isl_expression (type, arg_expr, ip);
336 if (codegen_error_p ())
337 return NULL_TREE;
339 switch (expr_type)
341 case isl_ast_op_add:
342 return fold_build2 (PLUS_EXPR, type, tree_lhs_expr, tree_rhs_expr);
344 case isl_ast_op_sub:
345 return fold_build2 (MINUS_EXPR, type, tree_lhs_expr, tree_rhs_expr);
347 case isl_ast_op_mul:
348 return fold_build2 (MULT_EXPR, type, tree_lhs_expr, tree_rhs_expr);
350 case isl_ast_op_div:
351 return fold_build2 (EXACT_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr);
353 case isl_ast_op_pdiv_q:
354 return fold_build2 (TRUNC_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr);
356 case isl_ast_op_zdiv_r:
357 case isl_ast_op_pdiv_r:
358 return fold_build2 (TRUNC_MOD_EXPR, type, tree_lhs_expr, tree_rhs_expr);
360 case isl_ast_op_fdiv_q:
361 return fold_build2 (FLOOR_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr);
363 case isl_ast_op_and:
364 return fold_build2 (TRUTH_ANDIF_EXPR, type,
365 tree_lhs_expr, tree_rhs_expr);
367 case isl_ast_op_or:
368 return fold_build2 (TRUTH_ORIF_EXPR, type, tree_lhs_expr, tree_rhs_expr);
370 case isl_ast_op_eq:
371 return fold_build2 (EQ_EXPR, type, tree_lhs_expr, tree_rhs_expr);
373 case isl_ast_op_le:
374 return fold_build2 (LE_EXPR, type, tree_lhs_expr, tree_rhs_expr);
376 case isl_ast_op_lt:
377 return fold_build2 (LT_EXPR, type, tree_lhs_expr, tree_rhs_expr);
379 case isl_ast_op_ge:
380 return fold_build2 (GE_EXPR, type, tree_lhs_expr, tree_rhs_expr);
382 case isl_ast_op_gt:
383 return fold_build2 (GT_EXPR, type, tree_lhs_expr, tree_rhs_expr);
385 default:
386 gcc_unreachable ();
390 /* Converts a ternary isl_ast_expr_op expression E to a GCC expression tree of
391 type TYPE. */
393 tree translate_isl_ast_to_gimple::
394 ternary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
396 enum isl_ast_op_type t = isl_ast_expr_get_op_type (expr);
397 gcc_assert (t == isl_ast_op_cond || t == isl_ast_op_select);
398 isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0);
399 tree a = gcc_expression_from_isl_expression (type, arg_expr, ip);
400 arg_expr = isl_ast_expr_get_op_arg (expr, 1);
401 tree b = gcc_expression_from_isl_expression (type, arg_expr, ip);
402 arg_expr = isl_ast_expr_get_op_arg (expr, 2);
403 tree c = gcc_expression_from_isl_expression (type, arg_expr, ip);
404 isl_ast_expr_free (expr);
406 if (codegen_error_p ())
407 return NULL_TREE;
409 return fold_build3 (COND_EXPR, type, a,
410 rewrite_to_non_trapping_overflow (b),
411 rewrite_to_non_trapping_overflow (c));
414 /* Converts a unary isl_ast_expr_op expression E to a GCC expression tree of
415 type TYPE. */
417 tree translate_isl_ast_to_gimple::
418 unary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
420 gcc_assert (isl_ast_expr_get_op_type (expr) == isl_ast_op_minus);
421 isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0);
422 tree tree_expr = gcc_expression_from_isl_expression (type, arg_expr, ip);
423 isl_ast_expr_free (expr);
424 return codegen_error_p () ? NULL_TREE
425 : fold_build1 (NEGATE_EXPR, type, tree_expr);
428 /* Converts an isl_ast_expr_op expression E with unknown number of arguments
429 to a GCC expression tree of type TYPE. */
431 tree translate_isl_ast_to_gimple::
432 nary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
434 enum tree_code op_code;
435 switch (isl_ast_expr_get_op_type (expr))
437 case isl_ast_op_max:
438 op_code = MAX_EXPR;
439 break;
441 case isl_ast_op_min:
442 op_code = MIN_EXPR;
443 break;
445 default:
446 gcc_unreachable ();
448 isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0);
449 tree res = gcc_expression_from_isl_expression (type, arg_expr, ip);
451 if (codegen_error_p ())
453 isl_ast_expr_free (expr);
454 return NULL_TREE;
457 int i;
458 for (i = 1; i < isl_ast_expr_get_op_n_arg (expr); i++)
460 arg_expr = isl_ast_expr_get_op_arg (expr, i);
461 tree t = gcc_expression_from_isl_expression (type, arg_expr, ip);
463 if (codegen_error_p ())
465 isl_ast_expr_free (expr);
466 return NULL_TREE;
469 res = fold_build2 (op_code, type, res, t);
471 isl_ast_expr_free (expr);
472 return res;
475 /* Converts an isl_ast_expr_op expression E to a GCC expression tree of
476 type TYPE. */
478 tree translate_isl_ast_to_gimple::
479 gcc_expression_from_isl_expr_op (tree type, __isl_take isl_ast_expr *expr,
480 ivs_params &ip)
482 if (codegen_error_p ())
484 isl_ast_expr_free (expr);
485 return NULL_TREE;
488 gcc_assert (isl_ast_expr_get_type (expr) == isl_ast_expr_op);
489 switch (isl_ast_expr_get_op_type (expr))
491 /* These isl ast expressions are not supported yet. */
492 case isl_ast_op_error:
493 case isl_ast_op_call:
494 case isl_ast_op_and_then:
495 case isl_ast_op_or_else:
496 gcc_unreachable ();
498 case isl_ast_op_max:
499 case isl_ast_op_min:
500 return nary_op_to_tree (type, expr, ip);
502 case isl_ast_op_add:
503 case isl_ast_op_sub:
504 case isl_ast_op_mul:
505 case isl_ast_op_div:
506 case isl_ast_op_pdiv_q:
507 case isl_ast_op_pdiv_r:
508 case isl_ast_op_fdiv_q:
509 case isl_ast_op_zdiv_r:
510 case isl_ast_op_and:
511 case isl_ast_op_or:
512 case isl_ast_op_eq:
513 case isl_ast_op_le:
514 case isl_ast_op_lt:
515 case isl_ast_op_ge:
516 case isl_ast_op_gt:
517 return binary_op_to_tree (type, expr, ip);
519 case isl_ast_op_minus:
520 return unary_op_to_tree (type, expr, ip);
522 case isl_ast_op_cond:
523 case isl_ast_op_select:
524 return ternary_op_to_tree (type, expr, ip);
526 default:
527 gcc_unreachable ();
530 return NULL_TREE;
533 /* Converts an isl AST expression E back to a GCC expression tree of
534 type TYPE. */
536 tree translate_isl_ast_to_gimple::
537 gcc_expression_from_isl_expression (tree type, __isl_take isl_ast_expr *expr,
538 ivs_params &ip)
540 if (codegen_error_p ())
542 isl_ast_expr_free (expr);
543 return NULL_TREE;
546 switch (isl_ast_expr_get_type (expr))
548 case isl_ast_expr_id:
549 return gcc_expression_from_isl_ast_expr_id (type, expr, ip);
551 case isl_ast_expr_int:
552 return gcc_expression_from_isl_expr_int (type, expr);
554 case isl_ast_expr_op:
555 return gcc_expression_from_isl_expr_op (type, expr, ip);
557 default:
558 gcc_unreachable ();
561 return NULL_TREE;
564 /* Creates a new LOOP corresponding to isl_ast_node_for. Inserts an
565 induction variable for the new LOOP. New LOOP is attached to CFG
566 starting at ENTRY_EDGE. LOOP is inserted into the loop tree and
567 becomes the child loop of the OUTER_LOOP. NEWIVS_INDEX binds
568 isl's scattering name to the induction variable created for the
569 loop of STMT. The new induction variable is inserted in the NEWIVS
570 vector and is of type TYPE. */
572 struct loop *translate_isl_ast_to_gimple::
573 graphite_create_new_loop (edge entry_edge, __isl_keep isl_ast_node *node_for,
574 loop_p outer, tree type, tree lb, tree ub,
575 ivs_params &ip)
577 isl_ast_expr *for_inc = isl_ast_node_for_get_inc (node_for);
578 tree stride = gcc_expression_from_isl_expression (type, for_inc, ip);
580 /* To fail code generation, we generate wrong code until we discard it. */
581 if (codegen_error_p ())
582 stride = integer_zero_node;
584 tree ivvar = create_tmp_var (type, "graphite_IV");
585 tree iv, iv_after_increment;
586 loop_p loop = create_empty_loop_on_edge
587 (entry_edge, lb, stride, ub, ivvar, &iv, &iv_after_increment,
588 outer ? outer : entry_edge->src->loop_father);
590 isl_ast_expr *for_iterator = isl_ast_node_for_get_iterator (node_for);
591 isl_id *id = isl_ast_expr_get_id (for_iterator);
592 bool existed_p = ip.put (id, iv);
593 if (existed_p)
594 isl_id_free (id);
595 isl_ast_expr_free (for_iterator);
596 return loop;
599 /* Create the loop for a isl_ast_node_for.
601 - NEXT_E is the edge where new generated code should be attached. */
603 edge translate_isl_ast_to_gimple::
604 translate_isl_ast_for_loop (loop_p context_loop,
605 __isl_keep isl_ast_node *node_for, edge next_e,
606 tree type, tree lb, tree ub,
607 ivs_params &ip)
609 gcc_assert (isl_ast_node_get_type (node_for) == isl_ast_node_for);
610 struct loop *loop = graphite_create_new_loop (next_e, node_for, context_loop,
611 type, lb, ub, ip);
612 edge last_e = single_exit (loop);
613 edge to_body = single_succ_edge (loop->header);
614 basic_block after = to_body->dest;
616 /* Translate the body of the loop. */
617 isl_ast_node *for_body = isl_ast_node_for_get_body (node_for);
618 next_e = translate_isl_ast (loop, for_body, to_body, ip);
619 isl_ast_node_free (for_body);
621 /* Early return if we failed to translate loop body. */
622 if (!next_e || codegen_error_p ())
623 return NULL;
625 if (next_e->dest != after)
626 redirect_edge_succ_nodup (next_e, after);
627 set_immediate_dominator (CDI_DOMINATORS, next_e->dest, next_e->src);
629 if (flag_loop_parallelize_all)
631 isl_id *id = isl_ast_node_get_annotation (node_for);
632 gcc_assert (id);
633 ast_build_info *for_info = (ast_build_info *) isl_id_get_user (id);
634 loop->can_be_parallel = for_info->is_parallelizable;
635 free (for_info);
636 isl_id_free (id);
639 return last_e;
642 /* We use this function to get the upper bound because of the form,
643 which is used by isl to represent loops:
645 for (iterator = init; cond; iterator += inc)
653 The loop condition is an arbitrary expression, which contains the
654 current loop iterator.
656 (e.g. iterator + 3 < B && C > iterator + A)
658 We have to know the upper bound of the iterator to generate a loop
659 in Gimple form. It can be obtained from the special representation
660 of the loop condition, which is generated by isl,
661 if the ast_build_atomic_upper_bound option is set. In this case,
662 isl generates a loop condition that consists of the current loop
663 iterator, + an operator (< or <=) and an expression not involving
664 the iterator, which is processed and returned by this function.
666 (e.g iterator <= upper-bound-expression-without-iterator) */
668 static __isl_give isl_ast_expr *
669 get_upper_bound (__isl_keep isl_ast_node *node_for)
671 gcc_assert (isl_ast_node_get_type (node_for) == isl_ast_node_for);
672 isl_ast_expr *for_cond = isl_ast_node_for_get_cond (node_for);
673 gcc_assert (isl_ast_expr_get_type (for_cond) == isl_ast_expr_op);
674 isl_ast_expr *res;
675 switch (isl_ast_expr_get_op_type (for_cond))
677 case isl_ast_op_le:
678 res = isl_ast_expr_get_op_arg (for_cond, 1);
679 break;
681 case isl_ast_op_lt:
683 /* (iterator < ub) => (iterator <= ub - 1). */
684 isl_val *one =
685 isl_val_int_from_si (isl_ast_expr_get_ctx (for_cond), 1);
686 isl_ast_expr *ub = isl_ast_expr_get_op_arg (for_cond, 1);
687 res = isl_ast_expr_sub (ub, isl_ast_expr_from_val (one));
688 break;
691 default:
692 gcc_unreachable ();
694 isl_ast_expr_free (for_cond);
695 return res;
698 /* Translates an isl_ast_node_for to Gimple. */
700 edge translate_isl_ast_to_gimple::
701 translate_isl_ast_node_for (loop_p context_loop, __isl_keep isl_ast_node *node,
702 edge next_e, ivs_params &ip)
704 gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_for);
705 tree type = graphite_expr_type;
707 isl_ast_expr *for_init = isl_ast_node_for_get_init (node);
708 tree lb = gcc_expression_from_isl_expression (type, for_init, ip);
709 /* To fail code generation, we generate wrong code until we discard it. */
710 if (codegen_error_p ())
711 lb = integer_zero_node;
713 isl_ast_expr *upper_bound = get_upper_bound (node);
714 tree ub = gcc_expression_from_isl_expression (type, upper_bound, ip);
715 /* To fail code generation, we generate wrong code until we discard it. */
716 if (codegen_error_p ())
717 ub = integer_zero_node;
719 edge last_e = single_succ_edge (split_edge (next_e));
721 /* Compensate for the fact that we emit a do { } while loop from
722 a for ISL AST.
723 ??? We often miss constraints on niter because the SESE region
724 doesn't cover loop header copies. Ideally we'd add constraints
725 for all relevant dominating conditions. */
726 if (TREE_CODE (lb) == INTEGER_CST && TREE_CODE (ub) == INTEGER_CST
727 && tree_int_cst_compare (lb, ub) <= 0)
729 else
731 tree one = build_one_cst (POINTER_TYPE_P (type) ? sizetype : type);
732 /* Adding +1 and using LT_EXPR helps with loop latches that have a
733 loop iteration count of "PARAMETER - 1". For PARAMETER == 0 this
734 becomes 2^k-1 due to integer overflow, and the condition lb <= ub
735 is true, even if we do not want this. However lb < ub + 1 is false,
736 as expected. */
737 tree ub_one = fold_build2 (POINTER_TYPE_P (type)
738 ? POINTER_PLUS_EXPR : PLUS_EXPR,
739 type, unshare_expr (ub), one);
740 create_empty_if_region_on_edge (next_e,
741 fold_build2 (LT_EXPR, boolean_type_node,
742 unshare_expr (lb), ub_one));
743 next_e = get_true_edge_from_guard_bb (next_e->dest);
746 translate_isl_ast_for_loop (context_loop, node, next_e,
747 type, lb, ub, ip);
748 return last_e;
751 /* Inserts in iv_map a tuple (OLD_LOOP->num, NEW_NAME) for the induction
752 variables of the loops around GBB in SESE.
754 FIXME: Instead of using a vec<tree> that maps each loop id to a possible
755 chrec, we could consider using a map<int, tree> that maps loop ids to the
756 corresponding tree expressions. */
758 void translate_isl_ast_to_gimple::
759 build_iv_mapping (vec<tree> iv_map, gimple_poly_bb_p gbb,
760 __isl_keep isl_ast_expr *user_expr, ivs_params &ip,
761 sese_l &region)
763 gcc_assert (isl_ast_expr_get_type (user_expr) == isl_ast_expr_op &&
764 isl_ast_expr_get_op_type (user_expr) == isl_ast_op_call);
765 int i;
766 isl_ast_expr *arg_expr;
767 for (i = 1; i < isl_ast_expr_get_op_n_arg (user_expr); i++)
769 arg_expr = isl_ast_expr_get_op_arg (user_expr, i);
770 tree type = graphite_expr_type;
771 tree t = gcc_expression_from_isl_expression (type, arg_expr, ip);
773 /* To fail code generation, we generate wrong code until we discard it. */
774 if (codegen_error_p ())
775 t = integer_zero_node;
777 loop_p old_loop = gbb_loop_at_index (gbb, region, i - 1);
778 iv_map[old_loop->num] = t;
782 /* Translates an isl_ast_node_user to Gimple.
784 FIXME: We should remove iv_map.create (loop->num + 1), if it is possible. */
786 edge translate_isl_ast_to_gimple::
787 translate_isl_ast_node_user (__isl_keep isl_ast_node *node,
788 edge next_e, ivs_params &ip)
790 gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_user);
792 isl_ast_expr *user_expr = isl_ast_node_user_get_expr (node);
793 isl_ast_expr *name_expr = isl_ast_expr_get_op_arg (user_expr, 0);
794 gcc_assert (isl_ast_expr_get_type (name_expr) == isl_ast_expr_id);
796 isl_id *name_id = isl_ast_expr_get_id (name_expr);
797 poly_bb_p pbb = (poly_bb_p) isl_id_get_user (name_id);
798 gcc_assert (pbb);
800 gimple_poly_bb_p gbb = PBB_BLACK_BOX (pbb);
802 isl_ast_expr_free (name_expr);
803 isl_id_free (name_id);
805 gcc_assert (GBB_BB (gbb) != ENTRY_BLOCK_PTR_FOR_FN (cfun) &&
806 "The entry block should not even appear within a scop");
808 const int nb_loops = number_of_loops (cfun);
809 vec<tree> iv_map;
810 iv_map.create (nb_loops);
811 iv_map.safe_grow_cleared (nb_loops, true);
813 build_iv_mapping (iv_map, gbb, user_expr, ip, pbb->scop->scop_info->region);
814 isl_ast_expr_free (user_expr);
816 basic_block old_bb = GBB_BB (gbb);
817 if (dump_file && (dump_flags & TDF_DETAILS))
819 fprintf (dump_file,
820 "[codegen] copying from bb_%d on edge (bb_%d, bb_%d)\n",
821 old_bb->index, next_e->src->index, next_e->dest->index);
822 print_loops_bb (dump_file, GBB_BB (gbb), 0, 3);
825 next_e = copy_bb_and_scalar_dependences (old_bb, next_e, iv_map);
827 iv_map.release ();
829 if (codegen_error_p ())
830 return NULL;
832 if (dump_file && (dump_flags & TDF_DETAILS))
834 fprintf (dump_file, "[codegen] (after copy) new basic block\n");
835 print_loops_bb (dump_file, next_e->src, 0, 3);
838 return next_e;
841 /* Translates an isl_ast_node_block to Gimple. */
843 edge translate_isl_ast_to_gimple::
844 translate_isl_ast_node_block (loop_p context_loop,
845 __isl_keep isl_ast_node *node,
846 edge next_e, ivs_params &ip)
848 gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_block);
849 isl_ast_node_list *node_list = isl_ast_node_block_get_children (node);
850 int i;
851 for (i = 0; i < isl_ast_node_list_n_ast_node (node_list); i++)
853 isl_ast_node *tmp_node = isl_ast_node_list_get_ast_node (node_list, i);
854 next_e = translate_isl_ast (context_loop, tmp_node, next_e, ip);
855 isl_ast_node_free (tmp_node);
857 isl_ast_node_list_free (node_list);
858 return next_e;
861 /* Creates a new if region corresponding to isl's cond. */
863 edge translate_isl_ast_to_gimple::
864 graphite_create_new_guard (edge entry_edge, __isl_take isl_ast_expr *if_cond,
865 ivs_params &ip)
867 tree type = graphite_expr_type;
868 tree cond_expr = gcc_expression_from_isl_expression (type, if_cond, ip);
870 /* To fail code generation, we generate wrong code until we discard it. */
871 if (codegen_error_p ())
872 cond_expr = integer_zero_node;
874 edge exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr);
875 return exit_edge;
878 /* Translates an isl_ast_node_if to Gimple. */
880 edge translate_isl_ast_to_gimple::
881 translate_isl_ast_node_if (loop_p context_loop,
882 __isl_keep isl_ast_node *node,
883 edge next_e, ivs_params &ip)
885 gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_if);
886 isl_ast_expr *if_cond = isl_ast_node_if_get_cond (node);
887 edge last_e = graphite_create_new_guard (next_e, if_cond, ip);
888 edge true_e = get_true_edge_from_guard_bb (next_e->dest);
889 merge_points.safe_push (last_e);
891 isl_ast_node *then_node = isl_ast_node_if_get_then (node);
892 translate_isl_ast (context_loop, then_node, true_e, ip);
893 isl_ast_node_free (then_node);
895 edge false_e = get_false_edge_from_guard_bb (next_e->dest);
896 isl_ast_node *else_node = isl_ast_node_if_get_else (node);
897 if (isl_ast_node_get_type (else_node) != isl_ast_node_error)
898 translate_isl_ast (context_loop, else_node, false_e, ip);
900 isl_ast_node_free (else_node);
901 return last_e;
904 /* Translates an isl AST node NODE to GCC representation in the
905 context of a SESE. */
907 edge translate_isl_ast_to_gimple::
908 translate_isl_ast (loop_p context_loop, __isl_keep isl_ast_node *node,
909 edge next_e, ivs_params &ip)
911 if (codegen_error_p ())
912 return NULL;
914 switch (isl_ast_node_get_type (node))
916 case isl_ast_node_error:
917 gcc_unreachable ();
919 case isl_ast_node_for:
920 return translate_isl_ast_node_for (context_loop, node,
921 next_e, ip);
923 case isl_ast_node_if:
924 return translate_isl_ast_node_if (context_loop, node,
925 next_e, ip);
927 case isl_ast_node_user:
928 return translate_isl_ast_node_user (node, next_e, ip);
930 case isl_ast_node_block:
931 return translate_isl_ast_node_block (context_loop, node,
932 next_e, ip);
934 case isl_ast_node_mark:
936 isl_ast_node *n = isl_ast_node_mark_get_node (node);
937 edge e = translate_isl_ast (context_loop, n, next_e, ip);
938 isl_ast_node_free (n);
939 return e;
942 default:
943 gcc_unreachable ();
947 /* Register in RENAME_MAP the rename tuple (OLD_NAME, EXPR).
948 When OLD_NAME and EXPR are the same we assert. */
950 void translate_isl_ast_to_gimple::
951 set_rename (tree old_name, tree expr)
953 if (dump_file)
955 fprintf (dump_file, "[codegen] setting rename: old_name = ");
956 print_generic_expr (dump_file, old_name);
957 fprintf (dump_file, ", new decl = ");
958 print_generic_expr (dump_file, expr);
959 fprintf (dump_file, "\n");
961 bool res = region->rename_map->put (old_name, expr);
962 gcc_assert (! res);
965 /* Return an iterator to the instructions comes last in the execution order.
966 Either GSI1 and GSI2 should belong to the same basic block or one of their
967 respective basic blocks should dominate the other. */
969 gimple_stmt_iterator
970 later_of_the_two (gimple_stmt_iterator gsi1, gimple_stmt_iterator gsi2)
972 basic_block bb1 = gsi_bb (gsi1);
973 basic_block bb2 = gsi_bb (gsi2);
975 /* Find the iterator which is the latest. */
976 if (bb1 == bb2)
978 gimple *stmt1 = gsi_stmt (gsi1);
979 gimple *stmt2 = gsi_stmt (gsi2);
981 if (stmt1 != NULL && stmt2 != NULL)
983 bool is_phi1 = gimple_code (stmt1) == GIMPLE_PHI;
984 bool is_phi2 = gimple_code (stmt2) == GIMPLE_PHI;
986 if (is_phi1 != is_phi2)
987 return is_phi1 ? gsi2 : gsi1;
990 /* For empty basic blocks gsis point to the end of the sequence. Since
991 there is no operator== defined for gimple_stmt_iterator and for gsis
992 not pointing to a valid statement gsi_next would assert. */
993 gimple_stmt_iterator gsi = gsi1;
994 do {
995 if (gsi_stmt (gsi) == gsi_stmt (gsi2))
996 return gsi2;
997 gsi_next (&gsi);
998 } while (!gsi_end_p (gsi));
1000 return gsi1;
1003 /* Find the basic block closest to the basic block which defines stmt. */
1004 if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
1005 return gsi1;
1007 gcc_assert (dominated_by_p (CDI_DOMINATORS, bb2, bb1));
1008 return gsi2;
1011 /* Insert each statement from SEQ at its earliest insertion p. */
1013 void translate_isl_ast_to_gimple::
1014 gsi_insert_earliest (gimple_seq seq)
1016 update_modified_stmts (seq);
1017 sese_l &codegen_region = region->if_region->true_region->region;
1018 basic_block begin_bb = get_entry_bb (codegen_region);
1020 /* Inserting the gimple statements in a vector because gimple_seq behave
1021 in strage ways when inserting the stmts from it into different basic
1022 blocks one at a time. */
1023 auto_vec<gimple *, 3> stmts;
1024 for (gimple_stmt_iterator gsi = gsi_start (seq); !gsi_end_p (gsi);
1025 gsi_next (&gsi))
1026 stmts.safe_push (gsi_stmt (gsi));
1028 int i;
1029 gimple *use_stmt;
1030 FOR_EACH_VEC_ELT (stmts, i, use_stmt)
1032 gcc_assert (gimple_code (use_stmt) != GIMPLE_PHI);
1033 gimple_stmt_iterator gsi_def_stmt = gsi_start_nondebug_bb (begin_bb);
1035 use_operand_p use_p;
1036 ssa_op_iter op_iter;
1037 FOR_EACH_SSA_USE_OPERAND (use_p, use_stmt, op_iter, SSA_OP_USE)
1039 /* Iterator to the current def of use_p. For function parameters or
1040 anything where def is not found, insert at the beginning of the
1041 generated region. */
1042 gimple_stmt_iterator gsi_stmt = gsi_def_stmt;
1044 tree op = USE_FROM_PTR (use_p);
1045 gimple *stmt = SSA_NAME_DEF_STMT (op);
1046 if (stmt && (gimple_code (stmt) != GIMPLE_NOP))
1047 gsi_stmt = gsi_for_stmt (stmt);
1049 /* For region parameters, insert at the beginning of the generated
1050 region. */
1051 if (!bb_in_sese_p (gsi_bb (gsi_stmt), codegen_region))
1052 gsi_stmt = gsi_def_stmt;
1054 gsi_def_stmt = later_of_the_two (gsi_stmt, gsi_def_stmt);
1057 if (!gsi_stmt (gsi_def_stmt))
1059 gimple_stmt_iterator gsi = gsi_after_labels (gsi_bb (gsi_def_stmt));
1060 gsi_insert_before (&gsi, use_stmt, GSI_NEW_STMT);
1062 else if (gimple_code (gsi_stmt (gsi_def_stmt)) == GIMPLE_PHI)
1064 gimple_stmt_iterator bsi
1065 = gsi_start_nondebug_bb (gsi_bb (gsi_def_stmt));
1066 /* Insert right after the PHI statements. */
1067 gsi_insert_before (&bsi, use_stmt, GSI_NEW_STMT);
1069 else
1070 gsi_insert_after (&gsi_def_stmt, use_stmt, GSI_NEW_STMT);
1072 if (dump_file)
1074 fprintf (dump_file, "[codegen] inserting statement in BB %d: ",
1075 gimple_bb (use_stmt)->index);
1076 print_gimple_stmt (dump_file, use_stmt, 0, TDF_VOPS | TDF_MEMSYMS);
1081 /* For ops which are scev_analyzeable, we can regenerate a new name from its
1082 scalar evolution around LOOP. */
1084 tree translate_isl_ast_to_gimple::
1085 get_rename_from_scev (tree old_name, gimple_seq *stmts, loop_p loop,
1086 vec<tree> iv_map)
1088 tree scev = cached_scalar_evolution_in_region (region->region,
1089 loop, old_name);
1091 /* At this point we should know the exact scev for each
1092 scalar SSA_NAME used in the scop: all the other scalar
1093 SSA_NAMEs should have been translated out of SSA using
1094 arrays with one element. */
1095 tree new_expr;
1096 if (chrec_contains_undetermined (scev))
1098 set_codegen_error ();
1099 return build_zero_cst (TREE_TYPE (old_name));
1102 new_expr = chrec_apply_map (scev, iv_map);
1104 /* The apply should produce an expression tree containing
1105 the uses of the new induction variables. We should be
1106 able to use new_expr instead of the old_name in the newly
1107 generated loop nest. */
1108 if (chrec_contains_undetermined (new_expr)
1109 || tree_contains_chrecs (new_expr, NULL))
1111 set_codegen_error ();
1112 return build_zero_cst (TREE_TYPE (old_name));
1115 /* Replace the old_name with the new_expr. */
1116 return force_gimple_operand (unshare_expr (new_expr), stmts,
1117 true, NULL_TREE);
1121 /* Return true if STMT should be copied from region to the new code-generated
1122 region. LABELs, CONDITIONS, induction-variables and region parameters need
1123 not be copied. */
1125 static bool
1126 should_copy_to_new_region (gimple *stmt, sese_info_p region)
1128 /* Do not copy labels or conditions. */
1129 if (gimple_code (stmt) == GIMPLE_LABEL
1130 || gimple_code (stmt) == GIMPLE_COND)
1131 return false;
1133 tree lhs;
1134 /* Do not copy induction variables. */
1135 if (is_gimple_assign (stmt)
1136 && (lhs = gimple_assign_lhs (stmt))
1137 && TREE_CODE (lhs) == SSA_NAME
1138 && scev_analyzable_p (lhs, region->region)
1139 /* But to code-generate liveouts - liveout PHI generation is
1140 in generic sese.c code that cannot do code generation. */
1141 && ! bitmap_bit_p (region->liveout, SSA_NAME_VERSION (lhs)))
1142 return false;
1144 return true;
1147 /* Duplicates the statements of basic block BB into basic block NEW_BB
1148 and compute the new induction variables according to the IV_MAP. */
1150 void translate_isl_ast_to_gimple::
1151 graphite_copy_stmts_from_block (basic_block bb, basic_block new_bb,
1152 vec<tree> iv_map)
1154 /* Iterator poining to the place where new statement (s) will be inserted. */
1155 gimple_stmt_iterator gsi_tgt = gsi_last_bb (new_bb);
1157 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
1158 gsi_next (&gsi))
1160 gimple *stmt = gsi_stmt (gsi);
1161 if (!should_copy_to_new_region (stmt, region))
1162 continue;
1164 /* Create a new copy of STMT and duplicate STMT's virtual
1165 operands. */
1166 gimple *copy = gimple_copy (stmt);
1168 /* Rather than not copying debug stmts we reset them.
1169 ??? Where we can rewrite uses without inserting new
1170 stmts we could simply do that. */
1171 if (is_gimple_debug (copy))
1173 if (gimple_debug_bind_p (copy))
1174 gimple_debug_bind_reset_value (copy);
1175 else if (gimple_debug_source_bind_p (copy)
1176 || gimple_debug_nonbind_marker_p (copy))
1178 else
1179 gcc_unreachable ();
1182 maybe_duplicate_eh_stmt (copy, stmt);
1183 gimple_duplicate_stmt_histograms (cfun, copy, cfun, stmt);
1185 /* Crete new names for each def in the copied stmt. */
1186 def_operand_p def_p;
1187 ssa_op_iter op_iter;
1188 FOR_EACH_SSA_DEF_OPERAND (def_p, copy, op_iter, SSA_OP_ALL_DEFS)
1190 tree old_name = DEF_FROM_PTR (def_p);
1191 create_new_def_for (old_name, copy, def_p);
1194 gsi_insert_after (&gsi_tgt, copy, GSI_NEW_STMT);
1195 if (dump_file)
1197 fprintf (dump_file, "[codegen] inserting statement: ");
1198 print_gimple_stmt (dump_file, copy, 0);
1201 /* For each SCEV analyzable SSA_NAME, rename their usage. */
1202 ssa_op_iter iter;
1203 use_operand_p use_p;
1204 if (!is_gimple_debug (copy))
1206 bool changed = false;
1207 FOR_EACH_SSA_USE_OPERAND (use_p, copy, iter, SSA_OP_USE)
1209 tree old_name = USE_FROM_PTR (use_p);
1211 if (TREE_CODE (old_name) != SSA_NAME
1212 || SSA_NAME_IS_DEFAULT_DEF (old_name)
1213 || ! scev_analyzable_p (old_name, region->region))
1214 continue;
1216 gimple_seq stmts = NULL;
1217 tree new_name = get_rename_from_scev (old_name, &stmts,
1218 bb->loop_father, iv_map);
1219 if (! codegen_error_p ())
1220 gsi_insert_earliest (stmts);
1221 replace_exp (use_p, new_name);
1222 changed = true;
1224 if (changed)
1225 fold_stmt_inplace (&gsi_tgt);
1228 update_stmt (copy);
1233 /* Copies BB and includes in the copied BB all the statements that can
1234 be reached following the use-def chains from the memory accesses,
1235 and returns the next edge following this new block. */
1237 edge translate_isl_ast_to_gimple::
1238 copy_bb_and_scalar_dependences (basic_block bb, edge next_e, vec<tree> iv_map)
1240 basic_block new_bb = split_edge (next_e);
1241 gimple_stmt_iterator gsi_tgt = gsi_last_bb (new_bb);
1242 for (gphi_iterator psi = gsi_start_phis (bb); !gsi_end_p (psi);
1243 gsi_next (&psi))
1245 gphi *phi = psi.phi ();
1246 tree res = gimple_phi_result (phi);
1247 if (virtual_operand_p (res)
1248 || scev_analyzable_p (res, region->region))
1249 continue;
1251 tree new_phi_def;
1252 tree *rename = region->rename_map->get (res);
1253 if (! rename)
1255 new_phi_def = create_tmp_reg (TREE_TYPE (res));
1256 set_rename (res, new_phi_def);
1258 else
1259 new_phi_def = *rename;
1261 gassign *ass = gimple_build_assign (NULL_TREE, new_phi_def);
1262 create_new_def_for (res, ass, NULL);
1263 gsi_insert_after (&gsi_tgt, ass, GSI_NEW_STMT);
1266 graphite_copy_stmts_from_block (bb, new_bb, iv_map);
1268 /* Insert out-of SSA copies on the original BB outgoing edges. */
1269 gsi_tgt = gsi_last_bb (new_bb);
1270 basic_block bb_for_succs = bb;
1271 if (bb_for_succs == bb_for_succs->loop_father->latch
1272 && bb_in_sese_p (bb_for_succs, region->region)
1273 && sese_trivially_empty_bb_p (bb_for_succs))
1274 bb_for_succs = NULL;
1275 while (bb_for_succs)
1277 basic_block latch = NULL;
1278 edge_iterator ei;
1279 edge e;
1280 FOR_EACH_EDGE (e, ei, bb_for_succs->succs)
1282 for (gphi_iterator psi = gsi_start_phis (e->dest); !gsi_end_p (psi);
1283 gsi_next (&psi))
1285 gphi *phi = psi.phi ();
1286 tree res = gimple_phi_result (phi);
1287 if (virtual_operand_p (res)
1288 || scev_analyzable_p (res, region->region))
1289 continue;
1291 tree new_phi_def;
1292 tree *rename = region->rename_map->get (res);
1293 if (! rename)
1295 new_phi_def = create_tmp_reg (TREE_TYPE (res));
1296 set_rename (res, new_phi_def);
1298 else
1299 new_phi_def = *rename;
1301 tree arg = PHI_ARG_DEF_FROM_EDGE (phi, e);
1302 if (TREE_CODE (arg) == SSA_NAME
1303 && scev_analyzable_p (arg, region->region))
1305 gimple_seq stmts = NULL;
1306 tree new_name = get_rename_from_scev (arg, &stmts,
1307 bb->loop_father,
1308 iv_map);
1309 if (! codegen_error_p ())
1310 gsi_insert_earliest (stmts);
1311 arg = new_name;
1313 gassign *ass = gimple_build_assign (new_phi_def, arg);
1314 gsi_insert_after (&gsi_tgt, ass, GSI_NEW_STMT);
1316 if (e->dest == bb_for_succs->loop_father->latch
1317 && bb_in_sese_p (e->dest, region->region)
1318 && sese_trivially_empty_bb_p (e->dest))
1319 latch = e->dest;
1321 bb_for_succs = latch;
1324 return single_succ_edge (new_bb);
1327 /* Add isl's parameter identifiers and corresponding trees to ivs_params. */
1329 void translate_isl_ast_to_gimple::
1330 add_parameters_to_ivs_params (scop_p scop, ivs_params &ip)
1332 sese_info_p region = scop->scop_info;
1333 unsigned nb_parameters = isl_set_dim (scop->param_context, isl_dim_param);
1334 gcc_assert (nb_parameters == sese_nb_params (region));
1335 unsigned i;
1336 tree param;
1337 FOR_EACH_VEC_ELT (region->params, i, param)
1339 isl_id *tmp_id = isl_set_get_dim_id (scop->param_context,
1340 isl_dim_param, i);
1341 bool existed_p = ip.put (tmp_id, param);
1342 gcc_assert (!existed_p);
1347 /* Generates a build, which specifies the constraints on the parameters. */
1349 __isl_give isl_ast_build *translate_isl_ast_to_gimple::
1350 generate_isl_context (scop_p scop)
1352 isl_set *context_isl = isl_set_params (isl_set_copy (scop->param_context));
1353 return isl_ast_build_from_context (context_isl);
1356 /* This method is executed before the construction of a for node. */
1357 __isl_give isl_id *
1358 ast_build_before_for (__isl_keep isl_ast_build *build, void *user)
1360 isl_union_map *dependences = (isl_union_map *) user;
1361 ast_build_info *for_info = XNEW (struct ast_build_info);
1362 isl_union_map *schedule = isl_ast_build_get_schedule (build);
1363 isl_space *schedule_space = isl_ast_build_get_schedule_space (build);
1364 int dimension = isl_space_dim (schedule_space, isl_dim_out);
1365 for_info->is_parallelizable =
1366 !carries_deps (schedule, dependences, dimension);
1367 isl_union_map_free (schedule);
1368 isl_space_free (schedule_space);
1369 isl_id *id = isl_id_alloc (isl_ast_build_get_ctx (build), "", for_info);
1370 return id;
1373 /* Generate isl AST from schedule of SCOP. */
1375 __isl_give isl_ast_node *translate_isl_ast_to_gimple::
1376 scop_to_isl_ast (scop_p scop)
1378 int old_err = isl_options_get_on_error (scop->isl_context);
1379 int old_max_operations = isl_ctx_get_max_operations (scop->isl_context);
1380 int max_operations = param_max_isl_operations;
1381 if (max_operations)
1382 isl_ctx_set_max_operations (scop->isl_context, max_operations);
1383 isl_options_set_on_error (scop->isl_context, ISL_ON_ERROR_CONTINUE);
1385 gcc_assert (scop->transformed_schedule);
1387 /* Set the separate option to reduce control flow overhead. */
1388 isl_schedule *schedule = isl_schedule_map_schedule_node_bottom_up
1389 (isl_schedule_copy (scop->transformed_schedule), set_separate_option, NULL);
1390 isl_ast_build *context_isl = generate_isl_context (scop);
1392 if (flag_loop_parallelize_all)
1394 scop_get_dependences (scop);
1395 context_isl =
1396 isl_ast_build_set_before_each_for (context_isl, ast_build_before_for,
1397 scop->dependence);
1400 isl_ast_node *ast_isl = isl_ast_build_node_from_schedule
1401 (context_isl, schedule);
1402 isl_ast_build_free (context_isl);
1404 isl_options_set_on_error (scop->isl_context, old_err);
1405 isl_ctx_reset_operations (scop->isl_context);
1406 isl_ctx_set_max_operations (scop->isl_context, old_max_operations);
1407 if (isl_ctx_last_error (scop->isl_context) != isl_error_none)
1409 if (dump_enabled_p ())
1411 dump_user_location_t loc = find_loop_location
1412 (scop->scop_info->region.entry->dest->loop_father);
1413 if (isl_ctx_last_error (scop->isl_context) == isl_error_quota)
1414 dump_printf_loc (MSG_MISSED_OPTIMIZATION, loc,
1415 "loop nest not optimized, AST generation timed out "
1416 "after %d operations [--param max-isl-operations]\n",
1417 max_operations);
1418 else
1419 dump_printf_loc (MSG_MISSED_OPTIMIZATION, loc,
1420 "loop nest not optimized, ISL AST generation "
1421 "signalled an error\n");
1423 isl_ast_node_free (ast_isl);
1424 return NULL;
1427 return ast_isl;
1430 /* Generate out-of-SSA copies for the entry edge FALSE_ENTRY/TRUE_ENTRY
1431 in REGION. */
1433 static void
1434 generate_entry_out_of_ssa_copies (edge false_entry,
1435 edge true_entry,
1436 sese_info_p region)
1438 gimple_stmt_iterator gsi_tgt = gsi_start_bb (true_entry->dest);
1439 for (gphi_iterator psi = gsi_start_phis (false_entry->dest);
1440 !gsi_end_p (psi); gsi_next (&psi))
1442 gphi *phi = psi.phi ();
1443 tree res = gimple_phi_result (phi);
1444 if (virtual_operand_p (res))
1445 continue;
1446 /* When there's no out-of-SSA var registered do not bother
1447 to create one. */
1448 tree *rename = region->rename_map->get (res);
1449 if (! rename)
1450 continue;
1451 tree new_phi_def = *rename;
1452 gassign *ass = gimple_build_assign (new_phi_def,
1453 PHI_ARG_DEF_FROM_EDGE (phi,
1454 false_entry));
1455 gsi_insert_after (&gsi_tgt, ass, GSI_NEW_STMT);
1459 /* GIMPLE Loop Generator: generates loops in GIMPLE form for the given SCOP.
1460 Return true if code generation succeeded. */
1462 bool
1463 graphite_regenerate_ast_isl (scop_p scop)
1465 sese_info_p region = scop->scop_info;
1466 translate_isl_ast_to_gimple t (region);
1468 ifsese if_region = NULL;
1469 isl_ast_node *root_node;
1470 ivs_params ip;
1472 timevar_push (TV_GRAPHITE_CODE_GEN);
1473 t.add_parameters_to_ivs_params (scop, ip);
1474 root_node = t.scop_to_isl_ast (scop);
1475 if (! root_node)
1477 ivs_params_clear (ip);
1478 timevar_pop (TV_GRAPHITE_CODE_GEN);
1479 return false;
1482 if (dump_file && (dump_flags & TDF_DETAILS))
1484 fprintf (dump_file, "[scheduler] original schedule:\n");
1485 print_isl_schedule (dump_file, scop->original_schedule);
1486 fprintf (dump_file, "[scheduler] isl transformed schedule:\n");
1487 print_isl_schedule (dump_file, scop->transformed_schedule);
1489 fprintf (dump_file, "[scheduler] original ast:\n");
1490 print_schedule_ast (dump_file, scop->original_schedule, scop);
1491 fprintf (dump_file, "[scheduler] AST generated by isl:\n");
1492 print_isl_ast (dump_file, root_node);
1495 if_region = move_sese_in_condition (region);
1496 region->if_region = if_region;
1498 loop_p context_loop = region->region.entry->src->loop_father;
1499 edge e = single_succ_edge (if_region->true_region->region.entry->dest);
1500 basic_block bb = split_edge (e);
1502 /* Update the true_region exit edge. */
1503 region->if_region->true_region->region.exit = single_succ_edge (bb);
1505 t.translate_isl_ast (context_loop, root_node, e, ip);
1506 if (! t.codegen_error_p ())
1508 generate_entry_out_of_ssa_copies (if_region->false_region->region.entry,
1509 if_region->true_region->region.entry,
1510 region);
1511 sese_insert_phis_for_liveouts (region,
1512 if_region->region->region.exit->src,
1513 if_region->false_region->region.exit,
1514 if_region->true_region->region.exit);
1515 if (dump_file)
1516 fprintf (dump_file, "[codegen] isl AST to Gimple succeeded.\n");
1519 if (t.codegen_error_p ())
1521 if (dump_enabled_p ())
1523 dump_user_location_t loc = find_loop_location
1524 (scop->scop_info->region.entry->dest->loop_father);
1525 dump_printf_loc (MSG_MISSED_OPTIMIZATION, loc,
1526 "loop nest not optimized, code generation error\n");
1529 /* Remove the unreachable region. */
1530 remove_edge_and_dominated_blocks (if_region->true_region->region.entry);
1531 basic_block ifb = if_region->false_region->region.entry->src;
1532 gimple_stmt_iterator gsi = gsi_last_bb (ifb);
1533 gsi_remove (&gsi, true);
1534 if_region->false_region->region.entry->flags &= ~EDGE_FALSE_VALUE;
1535 if_region->false_region->region.entry->flags |= EDGE_FALLTHRU;
1536 /* remove_edge_and_dominated_blocks marks loops for removal but
1537 doesn't actually remove them (fix that...). */
1538 for (auto loop : loops_list (cfun, LI_FROM_INNERMOST))
1539 if (!loop->header)
1540 delete_loop (loop);
1543 /* We are delaying SSA update to after code-generating all SCOPs.
1544 This is because we analyzed DRs and parameters on the unmodified
1545 IL and thus rely on SSA update to pick up new dominating definitions
1546 from for example SESE liveout PHIs. This is also for efficiency
1547 as SSA update does work depending on the size of the function. */
1549 free (if_region->true_region);
1550 free (if_region->region);
1551 free (if_region);
1553 ivs_params_clear (ip);
1554 isl_ast_node_free (root_node);
1555 timevar_pop (TV_GRAPHITE_CODE_GEN);
1557 return !t.codegen_error_p ();
1560 #endif /* HAVE_isl */