PR c++/82357 - bit-field in template
[official-gcc.git] / gcc / graphite-isl-ast-to-gimple.c
blob2a583aba63bd5ebdb25fec324325dd92fc8d7ee3
1 /* Translation of isl AST to Gimple.
2 Copyright (C) 2014-2017 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 USES_ISL
23 #include "config.h"
25 #ifdef HAVE_isl
27 #define INCLUDE_MAP
28 #include "system.h"
29 #include "coretypes.h"
30 #include "backend.h"
31 #include "cfghooks.h"
32 #include "tree.h"
33 #include "gimple.h"
34 #include "ssa.h"
35 #include "params.h"
36 #include "fold-const.h"
37 #include "gimple-fold.h"
38 #include "gimple-iterator.h"
39 #include "gimplify.h"
40 #include "gimplify-me.h"
41 #include "tree-eh.h"
42 #include "tree-ssa-loop.h"
43 #include "tree-ssa-operands.h"
44 #include "tree-ssa-propagate.h"
45 #include "tree-pass.h"
46 #include "cfgloop.h"
47 #include "tree-data-ref.h"
48 #include "tree-ssa-loop-manip.h"
49 #include "tree-scalar-evolution.h"
50 #include "gimple-ssa.h"
51 #include "tree-phinodes.h"
52 #include "tree-into-ssa.h"
53 #include "ssa-iterators.h"
54 #include "tree-cfg.h"
55 #include "gimple-pretty-print.h"
56 #include "cfganal.h"
57 #include "value-prof.h"
58 #include "tree-ssa.h"
59 #include "graphite.h"
61 struct ast_build_info
63 ast_build_info()
64 : is_parallelizable(false)
65 { }
66 bool is_parallelizable;
69 /* IVS_PARAMS maps isl's scattering and parameter identifiers
70 to corresponding trees. */
72 typedef std::map<isl_id *, tree> ivs_params;
74 /* Free all memory allocated for isl's identifiers. */
76 static void ivs_params_clear (ivs_params &ip)
78 std::map<isl_id *, tree>::iterator it;
79 for (it = ip.begin ();
80 it != ip.end (); it++)
82 isl_id_free (it->first);
86 /* Set the "separate" option for the schedule node. */
88 static isl_schedule_node *
89 set_separate_option (__isl_take isl_schedule_node *node, void *user)
91 if (user)
92 return node;
94 if (isl_schedule_node_get_type (node) != isl_schedule_node_band)
95 return node;
97 /* Set the "separate" option unless it is set earlier to another option. */
98 if (isl_schedule_node_band_member_get_ast_loop_type (node, 0)
99 == isl_ast_loop_default)
100 return isl_schedule_node_band_member_set_ast_loop_type
101 (node, 0, isl_ast_loop_separate);
103 return node;
106 /* Print SCHEDULE under an AST form on file F. */
108 void
109 print_schedule_ast (FILE *f, __isl_keep isl_schedule *schedule, scop_p scop)
111 isl_set *set = isl_set_params (isl_set_copy (scop->param_context));
112 isl_ast_build *context = isl_ast_build_from_context (set);
113 isl_ast_node *ast
114 = isl_ast_build_node_from_schedule (context, isl_schedule_copy (schedule));
115 isl_ast_build_free (context);
116 print_isl_ast (f, ast);
117 isl_ast_node_free (ast);
120 DEBUG_FUNCTION void
121 debug_schedule_ast (__isl_keep isl_schedule *s, scop_p scop)
123 print_schedule_ast (stderr, s, scop);
126 enum phi_node_kind
128 unknown_phi,
129 loop_phi,
130 close_phi,
131 cond_phi
134 class translate_isl_ast_to_gimple
136 public:
137 translate_isl_ast_to_gimple (sese_info_p r);
138 edge translate_isl_ast (loop_p context_loop, __isl_keep isl_ast_node *node,
139 edge next_e, ivs_params &ip);
140 edge translate_isl_ast_node_for (loop_p context_loop,
141 __isl_keep isl_ast_node *node,
142 edge next_e, ivs_params &ip);
143 edge translate_isl_ast_for_loop (loop_p context_loop,
144 __isl_keep isl_ast_node *node_for,
145 edge next_e,
146 tree type, tree lb, tree ub,
147 ivs_params &ip);
148 edge translate_isl_ast_node_if (loop_p context_loop,
149 __isl_keep isl_ast_node *node,
150 edge next_e, ivs_params &ip);
151 edge translate_isl_ast_node_user (__isl_keep isl_ast_node *node,
152 edge next_e, ivs_params &ip);
153 edge translate_isl_ast_node_block (loop_p context_loop,
154 __isl_keep isl_ast_node *node,
155 edge next_e, ivs_params &ip);
156 tree unary_op_to_tree (tree type, __isl_take isl_ast_expr *expr,
157 ivs_params &ip);
158 tree binary_op_to_tree (tree type, __isl_take isl_ast_expr *expr,
159 ivs_params &ip);
160 tree ternary_op_to_tree (tree type, __isl_take isl_ast_expr *expr,
161 ivs_params &ip);
162 tree nary_op_to_tree (tree type, __isl_take isl_ast_expr *expr,
163 ivs_params &ip);
164 tree gcc_expression_from_isl_expression (tree type,
165 __isl_take isl_ast_expr *,
166 ivs_params &ip);
167 tree gcc_expression_from_isl_ast_expr_id (tree type,
168 __isl_keep isl_ast_expr *expr_id,
169 ivs_params &ip);
170 widest_int widest_int_from_isl_expr_int (__isl_keep isl_ast_expr *expr);
171 tree gcc_expression_from_isl_expr_int (tree type,
172 __isl_take isl_ast_expr *expr);
173 tree gcc_expression_from_isl_expr_op (tree type,
174 __isl_take isl_ast_expr *expr,
175 ivs_params &ip);
176 struct loop *graphite_create_new_loop (edge entry_edge,
177 __isl_keep isl_ast_node *node_for,
178 loop_p outer, tree type,
179 tree lb, tree ub, ivs_params &ip);
180 edge graphite_create_new_guard (edge entry_edge,
181 __isl_take isl_ast_expr *if_cond,
182 ivs_params &ip);
183 void build_iv_mapping (vec<tree> iv_map, gimple_poly_bb_p gbb,
184 __isl_keep isl_ast_expr *user_expr, ivs_params &ip,
185 sese_l &region);
186 void add_parameters_to_ivs_params (scop_p scop, ivs_params &ip);
187 __isl_give isl_ast_build *generate_isl_context (scop_p scop);
189 __isl_give isl_ast_node * scop_to_isl_ast (scop_p scop);
191 tree get_rename_from_scev (tree old_name, gimple_seq *stmts, loop_p loop,
192 vec<tree> iv_map);
193 bool graphite_copy_stmts_from_block (basic_block bb, basic_block new_bb,
194 vec<tree> iv_map);
195 edge copy_bb_and_scalar_dependences (basic_block bb, edge next_e,
196 vec<tree> iv_map);
197 void set_rename (tree old_name, tree expr);
198 void set_rename_for_each_def (gimple *stmt);
199 void gsi_insert_earliest (gimple_seq seq);
200 bool codegen_error_p () const { return codegen_error; }
202 void set_codegen_error ()
204 codegen_error = true;
205 gcc_assert (! flag_checking
206 || PARAM_VALUE (PARAM_GRAPHITE_ALLOW_CODEGEN_ERRORS));
209 bool is_constant (tree op) const
211 return TREE_CODE (op) == INTEGER_CST
212 || TREE_CODE (op) == REAL_CST
213 || TREE_CODE (op) == COMPLEX_CST
214 || TREE_CODE (op) == VECTOR_CST;
217 private:
218 /* The region to be translated. */
219 sese_info_p region;
221 /* This flag is set when an error occurred during the translation of isl AST
222 to Gimple. */
223 bool codegen_error;
225 /* A vector of all the edges at if_condition merge points. */
226 auto_vec<edge, 2> merge_points;
228 tree graphite_expr_type;
231 translate_isl_ast_to_gimple::translate_isl_ast_to_gimple (sese_info_p r)
232 : region (r), codegen_error (false)
234 /* We always try to use signed 128 bit types, but fall back to smaller types
235 in case a platform does not provide types of these sizes. In the future we
236 should use isl to derive the optimal type for each subexpression. */
237 int max_mode_int_precision
238 = GET_MODE_PRECISION (int_mode_for_size (MAX_FIXED_MODE_SIZE, 0).require ());
239 int graphite_expr_type_precision
240 = 128 <= max_mode_int_precision ? 128 : max_mode_int_precision;
241 graphite_expr_type
242 = build_nonstandard_integer_type (graphite_expr_type_precision, 0);
245 /* Return the tree variable that corresponds to the given isl ast identifier
246 expression (an isl_ast_expr of type isl_ast_expr_id).
248 FIXME: We should replace blind conversion of id's type with derivation
249 of the optimal type when we get the corresponding isl support. Blindly
250 converting type sizes may be problematic when we switch to smaller
251 types. */
253 tree translate_isl_ast_to_gimple::
254 gcc_expression_from_isl_ast_expr_id (tree type,
255 __isl_take isl_ast_expr *expr_id,
256 ivs_params &ip)
258 gcc_assert (isl_ast_expr_get_type (expr_id) == isl_ast_expr_id);
259 isl_id *tmp_isl_id = isl_ast_expr_get_id (expr_id);
260 std::map<isl_id *, tree>::iterator res;
261 res = ip.find (tmp_isl_id);
262 isl_id_free (tmp_isl_id);
263 gcc_assert (res != ip.end () &&
264 "Could not map isl_id to tree expression");
265 isl_ast_expr_free (expr_id);
266 tree t = res->second;
267 tree *val = region->parameter_rename_map->get(t);
269 if (!val)
270 val = &t;
271 return fold_convert (type, *val);
274 /* Converts an isl_ast_expr_int expression E to a widest_int.
275 Raises a code generation error when the constant doesn't fit. */
277 widest_int translate_isl_ast_to_gimple::
278 widest_int_from_isl_expr_int (__isl_keep isl_ast_expr *expr)
280 gcc_assert (isl_ast_expr_get_type (expr) == isl_ast_expr_int);
281 isl_val *val = isl_ast_expr_get_val (expr);
282 size_t n = isl_val_n_abs_num_chunks (val, sizeof (HOST_WIDE_INT));
283 HOST_WIDE_INT *chunks = XALLOCAVEC (HOST_WIDE_INT, n);
284 if (n > WIDE_INT_MAX_ELTS
285 || isl_val_get_abs_num_chunks (val, sizeof (HOST_WIDE_INT), chunks) == -1)
287 isl_val_free (val);
288 set_codegen_error ();
289 return 0;
291 widest_int wi = widest_int::from_array (chunks, n, true);
292 if (isl_val_is_neg (val))
293 wi = -wi;
294 isl_val_free (val);
295 return wi;
298 /* Converts an isl_ast_expr_int expression E to a GCC expression tree of
299 type TYPE. Raises a code generation error when the constant doesn't fit. */
301 tree translate_isl_ast_to_gimple::
302 gcc_expression_from_isl_expr_int (tree type, __isl_take isl_ast_expr *expr)
304 widest_int wi = widest_int_from_isl_expr_int (expr);
305 isl_ast_expr_free (expr);
306 if (codegen_error_p ())
307 return NULL_TREE;
308 if (wi::min_precision (wi, TYPE_SIGN (type)) > TYPE_PRECISION (type))
310 set_codegen_error ();
311 return NULL_TREE;
313 return wide_int_to_tree (type, wi);
316 /* Converts a binary isl_ast_expr_op expression E to a GCC expression tree of
317 type TYPE. */
319 tree translate_isl_ast_to_gimple::
320 binary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
322 enum isl_ast_op_type expr_type = isl_ast_expr_get_op_type (expr);
323 isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0);
324 tree tree_lhs_expr = gcc_expression_from_isl_expression (type, arg_expr, ip);
325 arg_expr = isl_ast_expr_get_op_arg (expr, 1);
326 isl_ast_expr_free (expr);
328 /* From our constraint generation we may get modulo operations that
329 we cannot represent explicitely but that are no-ops for TYPE.
330 Elide those. */
331 if (expr_type == isl_ast_op_pdiv_r
332 && isl_ast_expr_get_type (arg_expr) == isl_ast_expr_int
333 && (wi::exact_log2 (widest_int_from_isl_expr_int (arg_expr))
334 >= TYPE_PRECISION (type)))
336 isl_ast_expr_free (arg_expr);
337 return tree_lhs_expr;
340 tree tree_rhs_expr = gcc_expression_from_isl_expression (type, arg_expr, ip);
341 if (codegen_error_p ())
342 return NULL_TREE;
344 switch (expr_type)
346 case isl_ast_op_add:
347 return fold_build2 (PLUS_EXPR, type, tree_lhs_expr, tree_rhs_expr);
349 case isl_ast_op_sub:
350 return fold_build2 (MINUS_EXPR, type, tree_lhs_expr, tree_rhs_expr);
352 case isl_ast_op_mul:
353 return fold_build2 (MULT_EXPR, type, tree_lhs_expr, tree_rhs_expr);
355 case isl_ast_op_div:
356 return fold_build2 (EXACT_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr);
358 case isl_ast_op_pdiv_q:
359 return fold_build2 (TRUNC_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr);
361 case isl_ast_op_zdiv_r:
362 case isl_ast_op_pdiv_r:
363 return fold_build2 (TRUNC_MOD_EXPR, type, tree_lhs_expr, tree_rhs_expr);
365 case isl_ast_op_fdiv_q:
366 return fold_build2 (FLOOR_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr);
368 case isl_ast_op_and:
369 return fold_build2 (TRUTH_ANDIF_EXPR, type,
370 tree_lhs_expr, tree_rhs_expr);
372 case isl_ast_op_or:
373 return fold_build2 (TRUTH_ORIF_EXPR, type, tree_lhs_expr, tree_rhs_expr);
375 case isl_ast_op_eq:
376 return fold_build2 (EQ_EXPR, type, tree_lhs_expr, tree_rhs_expr);
378 case isl_ast_op_le:
379 return fold_build2 (LE_EXPR, type, tree_lhs_expr, tree_rhs_expr);
381 case isl_ast_op_lt:
382 return fold_build2 (LT_EXPR, type, tree_lhs_expr, tree_rhs_expr);
384 case isl_ast_op_ge:
385 return fold_build2 (GE_EXPR, type, tree_lhs_expr, tree_rhs_expr);
387 case isl_ast_op_gt:
388 return fold_build2 (GT_EXPR, type, tree_lhs_expr, tree_rhs_expr);
390 default:
391 gcc_unreachable ();
395 /* Converts a ternary isl_ast_expr_op expression E to a GCC expression tree of
396 type TYPE. */
398 tree translate_isl_ast_to_gimple::
399 ternary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
401 enum isl_ast_op_type t = isl_ast_expr_get_op_type (expr);
402 gcc_assert (t == isl_ast_op_cond || t == isl_ast_op_select);
403 isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0);
404 tree a = gcc_expression_from_isl_expression (type, arg_expr, ip);
405 arg_expr = isl_ast_expr_get_op_arg (expr, 1);
406 tree b = gcc_expression_from_isl_expression (type, arg_expr, ip);
407 arg_expr = isl_ast_expr_get_op_arg (expr, 2);
408 tree c = gcc_expression_from_isl_expression (type, arg_expr, ip);
409 isl_ast_expr_free (expr);
411 if (codegen_error_p ())
412 return NULL_TREE;
414 return fold_build3 (COND_EXPR, type, a, b, c);
417 /* Converts a unary isl_ast_expr_op expression E to a GCC expression tree of
418 type TYPE. */
420 tree translate_isl_ast_to_gimple::
421 unary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
423 gcc_assert (isl_ast_expr_get_op_type (expr) == isl_ast_op_minus);
424 isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0);
425 tree tree_expr = gcc_expression_from_isl_expression (type, arg_expr, ip);
426 isl_ast_expr_free (expr);
427 return codegen_error_p () ? NULL_TREE
428 : fold_build1 (NEGATE_EXPR, type, tree_expr);
431 /* Converts an isl_ast_expr_op expression E with unknown number of arguments
432 to a GCC expression tree of type TYPE. */
434 tree translate_isl_ast_to_gimple::
435 nary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
437 enum tree_code op_code;
438 switch (isl_ast_expr_get_op_type (expr))
440 case isl_ast_op_max:
441 op_code = MAX_EXPR;
442 break;
444 case isl_ast_op_min:
445 op_code = MIN_EXPR;
446 break;
448 default:
449 gcc_unreachable ();
451 isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0);
452 tree res = gcc_expression_from_isl_expression (type, arg_expr, ip);
454 if (codegen_error_p ())
456 isl_ast_expr_free (expr);
457 return NULL_TREE;
460 int i;
461 for (i = 1; i < isl_ast_expr_get_op_n_arg (expr); i++)
463 arg_expr = isl_ast_expr_get_op_arg (expr, i);
464 tree t = gcc_expression_from_isl_expression (type, arg_expr, ip);
466 if (codegen_error_p ())
468 isl_ast_expr_free (expr);
469 return NULL_TREE;
472 res = fold_build2 (op_code, type, res, t);
474 isl_ast_expr_free (expr);
475 return res;
478 /* Converts an isl_ast_expr_op expression E to a GCC expression tree of
479 type TYPE. */
481 tree translate_isl_ast_to_gimple::
482 gcc_expression_from_isl_expr_op (tree type, __isl_take isl_ast_expr *expr,
483 ivs_params &ip)
485 if (codegen_error_p ())
487 isl_ast_expr_free (expr);
488 return NULL_TREE;
491 gcc_assert (isl_ast_expr_get_type (expr) == isl_ast_expr_op);
492 switch (isl_ast_expr_get_op_type (expr))
494 /* These isl ast expressions are not supported yet. */
495 case isl_ast_op_error:
496 case isl_ast_op_call:
497 case isl_ast_op_and_then:
498 case isl_ast_op_or_else:
499 gcc_unreachable ();
501 case isl_ast_op_max:
502 case isl_ast_op_min:
503 return nary_op_to_tree (type, expr, ip);
505 case isl_ast_op_add:
506 case isl_ast_op_sub:
507 case isl_ast_op_mul:
508 case isl_ast_op_div:
509 case isl_ast_op_pdiv_q:
510 case isl_ast_op_pdiv_r:
511 case isl_ast_op_fdiv_q:
512 case isl_ast_op_zdiv_r:
513 case isl_ast_op_and:
514 case isl_ast_op_or:
515 case isl_ast_op_eq:
516 case isl_ast_op_le:
517 case isl_ast_op_lt:
518 case isl_ast_op_ge:
519 case isl_ast_op_gt:
520 return binary_op_to_tree (type, expr, ip);
522 case isl_ast_op_minus:
523 return unary_op_to_tree (type, expr, ip);
525 case isl_ast_op_cond:
526 case isl_ast_op_select:
527 return ternary_op_to_tree (type, expr, ip);
529 default:
530 gcc_unreachable ();
533 return NULL_TREE;
536 /* Converts an isl AST expression E back to a GCC expression tree of
537 type TYPE. */
539 tree translate_isl_ast_to_gimple::
540 gcc_expression_from_isl_expression (tree type, __isl_take isl_ast_expr *expr,
541 ivs_params &ip)
543 if (codegen_error_p ())
545 isl_ast_expr_free (expr);
546 return NULL_TREE;
549 switch (isl_ast_expr_get_type (expr))
551 case isl_ast_expr_id:
552 return gcc_expression_from_isl_ast_expr_id (type, expr, ip);
554 case isl_ast_expr_int:
555 return gcc_expression_from_isl_expr_int (type, expr);
557 case isl_ast_expr_op:
558 return gcc_expression_from_isl_expr_op (type, expr, ip);
560 default:
561 gcc_unreachable ();
564 return NULL_TREE;
567 /* Creates a new LOOP corresponding to isl_ast_node_for. Inserts an
568 induction variable for the new LOOP. New LOOP is attached to CFG
569 starting at ENTRY_EDGE. LOOP is inserted into the loop tree and
570 becomes the child loop of the OUTER_LOOP. NEWIVS_INDEX binds
571 isl's scattering name to the induction variable created for the
572 loop of STMT. The new induction variable is inserted in the NEWIVS
573 vector and is of type TYPE. */
575 struct loop *translate_isl_ast_to_gimple::
576 graphite_create_new_loop (edge entry_edge, __isl_keep isl_ast_node *node_for,
577 loop_p outer, tree type, tree lb, tree ub,
578 ivs_params &ip)
580 isl_ast_expr *for_inc = isl_ast_node_for_get_inc (node_for);
581 tree stride = gcc_expression_from_isl_expression (type, for_inc, ip);
583 /* To fail code generation, we generate wrong code until we discard it. */
584 if (codegen_error_p ())
585 stride = integer_zero_node;
587 tree ivvar = create_tmp_var (type, "graphite_IV");
588 tree iv, iv_after_increment;
589 loop_p loop = create_empty_loop_on_edge
590 (entry_edge, lb, stride, ub, ivvar, &iv, &iv_after_increment,
591 outer ? outer : entry_edge->src->loop_father);
593 isl_ast_expr *for_iterator = isl_ast_node_for_get_iterator (node_for);
594 isl_id *id = isl_ast_expr_get_id (for_iterator);
595 std::map<isl_id *, tree>::iterator res;
596 res = ip.find (id);
597 if (ip.count (id))
598 isl_id_free (res->first);
599 ip[id] = iv;
600 isl_ast_expr_free (for_iterator);
601 return loop;
604 /* Create the loop for a isl_ast_node_for.
606 - NEXT_E is the edge where new generated code should be attached. */
608 edge translate_isl_ast_to_gimple::
609 translate_isl_ast_for_loop (loop_p context_loop,
610 __isl_keep isl_ast_node *node_for, edge next_e,
611 tree type, tree lb, tree ub,
612 ivs_params &ip)
614 gcc_assert (isl_ast_node_get_type (node_for) == isl_ast_node_for);
615 struct loop *loop = graphite_create_new_loop (next_e, node_for, context_loop,
616 type, lb, ub, ip);
617 edge last_e = single_exit (loop);
618 edge to_body = single_succ_edge (loop->header);
619 basic_block after = to_body->dest;
621 /* Translate the body of the loop. */
622 isl_ast_node *for_body = isl_ast_node_for_get_body (node_for);
623 next_e = translate_isl_ast (loop, for_body, to_body, ip);
624 isl_ast_node_free (for_body);
626 /* Early return if we failed to translate loop body. */
627 if (!next_e || codegen_error_p ())
628 return NULL;
630 if (next_e->dest != after)
631 redirect_edge_succ_nodup (next_e, after);
632 set_immediate_dominator (CDI_DOMINATORS, next_e->dest, next_e->src);
634 if (flag_loop_parallelize_all)
636 isl_id *id = isl_ast_node_get_annotation (node_for);
637 gcc_assert (id);
638 ast_build_info *for_info = (ast_build_info *) isl_id_get_user (id);
639 loop->can_be_parallel = for_info->is_parallelizable;
640 free (for_info);
641 isl_id_free (id);
644 return last_e;
647 /* We use this function to get the upper bound because of the form,
648 which is used by isl to represent loops:
650 for (iterator = init; cond; iterator += inc)
658 The loop condition is an arbitrary expression, which contains the
659 current loop iterator.
661 (e.g. iterator + 3 < B && C > iterator + A)
663 We have to know the upper bound of the iterator to generate a loop
664 in Gimple form. It can be obtained from the special representation
665 of the loop condition, which is generated by isl,
666 if the ast_build_atomic_upper_bound option is set. In this case,
667 isl generates a loop condition that consists of the current loop
668 iterator, + an operator (< or <=) and an expression not involving
669 the iterator, which is processed and returned by this function.
671 (e.g iterator <= upper-bound-expression-without-iterator) */
673 static __isl_give isl_ast_expr *
674 get_upper_bound (__isl_keep isl_ast_node *node_for)
676 gcc_assert (isl_ast_node_get_type (node_for) == isl_ast_node_for);
677 isl_ast_expr *for_cond = isl_ast_node_for_get_cond (node_for);
678 gcc_assert (isl_ast_expr_get_type (for_cond) == isl_ast_expr_op);
679 isl_ast_expr *res;
680 switch (isl_ast_expr_get_op_type (for_cond))
682 case isl_ast_op_le:
683 res = isl_ast_expr_get_op_arg (for_cond, 1);
684 break;
686 case isl_ast_op_lt:
688 /* (iterator < ub) => (iterator <= ub - 1). */
689 isl_val *one =
690 isl_val_int_from_si (isl_ast_expr_get_ctx (for_cond), 1);
691 isl_ast_expr *ub = isl_ast_expr_get_op_arg (for_cond, 1);
692 res = isl_ast_expr_sub (ub, isl_ast_expr_from_val (one));
693 break;
696 default:
697 gcc_unreachable ();
699 isl_ast_expr_free (for_cond);
700 return res;
703 /* Translates an isl_ast_node_for to Gimple. */
705 edge translate_isl_ast_to_gimple::
706 translate_isl_ast_node_for (loop_p context_loop, __isl_keep isl_ast_node *node,
707 edge next_e, ivs_params &ip)
709 gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_for);
710 tree type = graphite_expr_type;
712 isl_ast_expr *for_init = isl_ast_node_for_get_init (node);
713 tree lb = gcc_expression_from_isl_expression (type, for_init, ip);
714 /* To fail code generation, we generate wrong code until we discard it. */
715 if (codegen_error_p ())
716 lb = integer_zero_node;
718 isl_ast_expr *upper_bound = get_upper_bound (node);
719 tree ub = gcc_expression_from_isl_expression (type, upper_bound, ip);
720 /* To fail code generation, we generate wrong code until we discard it. */
721 if (codegen_error_p ())
722 ub = integer_zero_node;
724 edge last_e = single_succ_edge (split_edge (next_e));
725 translate_isl_ast_for_loop (context_loop, node, next_e,
726 type, lb, ub, ip);
727 return last_e;
730 /* Inserts in iv_map a tuple (OLD_LOOP->num, NEW_NAME) for the induction
731 variables of the loops around GBB in SESE.
733 FIXME: Instead of using a vec<tree> that maps each loop id to a possible
734 chrec, we could consider using a map<int, tree> that maps loop ids to the
735 corresponding tree expressions. */
737 void translate_isl_ast_to_gimple::
738 build_iv_mapping (vec<tree> iv_map, gimple_poly_bb_p gbb,
739 __isl_keep isl_ast_expr *user_expr, ivs_params &ip,
740 sese_l &region)
742 gcc_assert (isl_ast_expr_get_type (user_expr) == isl_ast_expr_op &&
743 isl_ast_expr_get_op_type (user_expr) == isl_ast_op_call);
744 int i;
745 isl_ast_expr *arg_expr;
746 for (i = 1; i < isl_ast_expr_get_op_n_arg (user_expr); i++)
748 arg_expr = isl_ast_expr_get_op_arg (user_expr, i);
749 tree type = graphite_expr_type;
750 tree t = gcc_expression_from_isl_expression (type, arg_expr, ip);
752 /* To fail code generation, we generate wrong code until we discard it. */
753 if (codegen_error_p ())
754 t = integer_zero_node;
756 loop_p old_loop = gbb_loop_at_index (gbb, region, i - 1);
757 iv_map[old_loop->num] = t;
761 /* Translates an isl_ast_node_user to Gimple.
763 FIXME: We should remove iv_map.create (loop->num + 1), if it is possible. */
765 edge translate_isl_ast_to_gimple::
766 translate_isl_ast_node_user (__isl_keep isl_ast_node *node,
767 edge next_e, ivs_params &ip)
769 gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_user);
771 isl_ast_expr *user_expr = isl_ast_node_user_get_expr (node);
772 isl_ast_expr *name_expr = isl_ast_expr_get_op_arg (user_expr, 0);
773 gcc_assert (isl_ast_expr_get_type (name_expr) == isl_ast_expr_id);
775 isl_id *name_id = isl_ast_expr_get_id (name_expr);
776 poly_bb_p pbb = (poly_bb_p) isl_id_get_user (name_id);
777 gcc_assert (pbb);
779 gimple_poly_bb_p gbb = PBB_BLACK_BOX (pbb);
781 isl_ast_expr_free (name_expr);
782 isl_id_free (name_id);
784 gcc_assert (GBB_BB (gbb) != ENTRY_BLOCK_PTR_FOR_FN (cfun) &&
785 "The entry block should not even appear within a scop");
787 const int nb_loops = number_of_loops (cfun);
788 vec<tree> iv_map;
789 iv_map.create (nb_loops);
790 iv_map.safe_grow_cleared (nb_loops);
792 build_iv_mapping (iv_map, gbb, user_expr, ip, pbb->scop->scop_info->region);
793 isl_ast_expr_free (user_expr);
795 basic_block old_bb = GBB_BB (gbb);
796 if (dump_file)
798 fprintf (dump_file,
799 "[codegen] copying from bb_%d on edge (bb_%d, bb_%d)\n",
800 old_bb->index, next_e->src->index, next_e->dest->index);
801 print_loops_bb (dump_file, GBB_BB (gbb), 0, 3);
805 next_e = copy_bb_and_scalar_dependences (old_bb, next_e, iv_map);
807 iv_map.release ();
809 if (codegen_error_p ())
810 return NULL;
812 if (dump_file)
814 fprintf (dump_file, "[codegen] (after copy) new basic block\n");
815 print_loops_bb (dump_file, next_e->src, 0, 3);
818 return next_e;
821 /* Translates an isl_ast_node_block to Gimple. */
823 edge translate_isl_ast_to_gimple::
824 translate_isl_ast_node_block (loop_p context_loop,
825 __isl_keep isl_ast_node *node,
826 edge next_e, ivs_params &ip)
828 gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_block);
829 isl_ast_node_list *node_list = isl_ast_node_block_get_children (node);
830 int i;
831 for (i = 0; i < isl_ast_node_list_n_ast_node (node_list); i++)
833 isl_ast_node *tmp_node = isl_ast_node_list_get_ast_node (node_list, i);
834 next_e = translate_isl_ast (context_loop, tmp_node, next_e, ip);
835 isl_ast_node_free (tmp_node);
837 isl_ast_node_list_free (node_list);
838 return next_e;
841 /* Creates a new if region corresponding to isl's cond. */
843 edge translate_isl_ast_to_gimple::
844 graphite_create_new_guard (edge entry_edge, __isl_take isl_ast_expr *if_cond,
845 ivs_params &ip)
847 tree type = graphite_expr_type;
848 tree cond_expr = gcc_expression_from_isl_expression (type, if_cond, ip);
850 /* To fail code generation, we generate wrong code until we discard it. */
851 if (codegen_error_p ())
852 cond_expr = integer_zero_node;
854 edge exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr);
855 return exit_edge;
858 /* Translates an isl_ast_node_if to Gimple. */
860 edge translate_isl_ast_to_gimple::
861 translate_isl_ast_node_if (loop_p context_loop,
862 __isl_keep isl_ast_node *node,
863 edge next_e, ivs_params &ip)
865 gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_if);
866 isl_ast_expr *if_cond = isl_ast_node_if_get_cond (node);
867 edge last_e = graphite_create_new_guard (next_e, if_cond, ip);
868 edge true_e = get_true_edge_from_guard_bb (next_e->dest);
869 merge_points.safe_push (last_e);
871 isl_ast_node *then_node = isl_ast_node_if_get_then (node);
872 translate_isl_ast (context_loop, then_node, true_e, ip);
873 isl_ast_node_free (then_node);
875 edge false_e = get_false_edge_from_guard_bb (next_e->dest);
876 isl_ast_node *else_node = isl_ast_node_if_get_else (node);
877 if (isl_ast_node_get_type (else_node) != isl_ast_node_error)
878 translate_isl_ast (context_loop, else_node, false_e, ip);
880 isl_ast_node_free (else_node);
881 return last_e;
884 /* Translates an isl AST node NODE to GCC representation in the
885 context of a SESE. */
887 edge translate_isl_ast_to_gimple::
888 translate_isl_ast (loop_p context_loop, __isl_keep isl_ast_node *node,
889 edge next_e, ivs_params &ip)
891 if (codegen_error_p ())
892 return NULL;
894 switch (isl_ast_node_get_type (node))
896 case isl_ast_node_error:
897 gcc_unreachable ();
899 case isl_ast_node_for:
900 return translate_isl_ast_node_for (context_loop, node,
901 next_e, ip);
903 case isl_ast_node_if:
904 return translate_isl_ast_node_if (context_loop, node,
905 next_e, ip);
907 case isl_ast_node_user:
908 return translate_isl_ast_node_user (node, next_e, ip);
910 case isl_ast_node_block:
911 return translate_isl_ast_node_block (context_loop, node,
912 next_e, ip);
914 case isl_ast_node_mark:
916 isl_ast_node *n = isl_ast_node_mark_get_node (node);
917 edge e = translate_isl_ast (context_loop, n, next_e, ip);
918 isl_ast_node_free (n);
919 return e;
922 default:
923 gcc_unreachable ();
927 /* Register in RENAME_MAP the rename tuple (OLD_NAME, EXPR).
928 When OLD_NAME and EXPR are the same we assert. */
930 void translate_isl_ast_to_gimple::
931 set_rename (tree old_name, tree expr)
933 if (dump_file)
935 fprintf (dump_file, "[codegen] setting rename: old_name = ");
936 print_generic_expr (dump_file, old_name);
937 fprintf (dump_file, ", new_name = ");
938 print_generic_expr (dump_file, expr);
939 fprintf (dump_file, "\n");
942 if (old_name == expr)
943 return;
945 vec <tree> *renames = region->rename_map->get (old_name);
947 if (renames)
948 renames->safe_push (expr);
949 else
951 vec<tree> r;
952 r.create (2);
953 r.safe_push (expr);
954 region->rename_map->put (old_name, r);
957 tree t;
958 int i;
959 /* For a parameter of a scop we don't want to rename it. */
960 FOR_EACH_VEC_ELT (region->params, i, t)
961 if (old_name == t)
962 region->parameter_rename_map->put(old_name, expr);
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_bb_nondebug (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_bb_nondebug (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: ");
1075 print_gimple_stmt (dump_file, use_stmt, 0, TDF_VOPS | TDF_MEMSYMS);
1076 print_loops_bb (dump_file, gimple_bb (use_stmt), 0, 3);
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 = scalar_evolution_in_region (region->region, loop, old_name);
1090 /* At this point we should know the exact scev for each
1091 scalar SSA_NAME used in the scop: all the other scalar
1092 SSA_NAMEs should have been translated out of SSA using
1093 arrays with one element. */
1094 tree new_expr;
1095 if (chrec_contains_undetermined (scev))
1097 set_codegen_error ();
1098 return build_zero_cst (TREE_TYPE (old_name));
1101 new_expr = chrec_apply_map (scev, iv_map);
1103 /* The apply should produce an expression tree containing
1104 the uses of the new induction variables. We should be
1105 able to use new_expr instead of the old_name in the newly
1106 generated loop nest. */
1107 if (chrec_contains_undetermined (new_expr)
1108 || tree_contains_chrecs (new_expr, NULL))
1110 set_codegen_error ();
1111 return build_zero_cst (TREE_TYPE (old_name));
1114 /* Replace the old_name with the new_expr. */
1115 return force_gimple_operand (unshare_expr (new_expr), stmts,
1116 true, NULL_TREE);
1120 /* Return true if STMT should be copied from region to the new code-generated
1121 region. LABELs, CONDITIONS, induction-variables and region parameters need
1122 not be copied. */
1124 static bool
1125 should_copy_to_new_region (gimple *stmt, sese_info_p region)
1127 /* Do not copy labels or conditions. */
1128 if (gimple_code (stmt) == GIMPLE_LABEL
1129 || gimple_code (stmt) == GIMPLE_COND)
1130 return false;
1132 tree lhs;
1133 /* Do not copy induction variables. */
1134 if (is_gimple_assign (stmt)
1135 && (lhs = gimple_assign_lhs (stmt))
1136 && TREE_CODE (lhs) == SSA_NAME
1137 && is_gimple_reg (lhs)
1138 && scev_analyzable_p (lhs, region->region))
1139 return false;
1141 /* Do not copy parameters that have been generated in the header of the
1142 scop. */
1143 if (is_gimple_assign (stmt)
1144 && (lhs = gimple_assign_lhs (stmt))
1145 && TREE_CODE (lhs) == SSA_NAME
1146 && region->parameter_rename_map->get(lhs))
1147 return false;
1149 return true;
1152 /* Create new names for all the definitions created by COPY and add replacement
1153 mappings for each new name. */
1155 void translate_isl_ast_to_gimple::
1156 set_rename_for_each_def (gimple *stmt)
1158 def_operand_p def_p;
1159 ssa_op_iter op_iter;
1160 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, op_iter, SSA_OP_ALL_DEFS)
1162 tree old_name = DEF_FROM_PTR (def_p);
1163 create_new_def_for (old_name, stmt, def_p);
1167 /* Duplicates the statements of basic block BB into basic block NEW_BB
1168 and compute the new induction variables according to the IV_MAP. */
1170 bool translate_isl_ast_to_gimple::
1171 graphite_copy_stmts_from_block (basic_block bb, basic_block new_bb,
1172 vec<tree> iv_map)
1174 /* Iterator poining to the place where new statement (s) will be inserted. */
1175 gimple_stmt_iterator gsi_tgt = gsi_last_bb (new_bb);
1177 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
1178 gsi_next (&gsi))
1180 gimple *stmt = gsi_stmt (gsi);
1181 if (!should_copy_to_new_region (stmt, region))
1182 continue;
1184 /* Create a new copy of STMT and duplicate STMT's virtual
1185 operands. */
1186 gimple *copy = gimple_copy (stmt);
1187 gsi_insert_after (&gsi_tgt, copy, GSI_NEW_STMT);
1189 /* Rather than not copying debug stmts we reset them.
1190 ??? Where we can rewrite uses without inserting new
1191 stmts we could simply do that. */
1192 if (is_gimple_debug (copy))
1194 if (gimple_debug_bind_p (copy))
1195 gimple_debug_bind_reset_value (copy);
1196 else if (gimple_debug_source_bind_p (copy))
1198 else
1199 gcc_unreachable ();
1202 if (dump_file)
1204 fprintf (dump_file, "[codegen] inserting statement: ");
1205 print_gimple_stmt (dump_file, copy, 0);
1208 maybe_duplicate_eh_stmt (copy, stmt);
1209 gimple_duplicate_stmt_histograms (cfun, copy, cfun, stmt);
1211 /* Crete new names for each def in the copied stmt. */
1212 set_rename_for_each_def (copy);
1214 if (codegen_error_p ())
1215 return false;
1217 /* For each SSA_NAME in the parameter_rename_map rename their usage. */
1218 ssa_op_iter iter;
1219 use_operand_p use_p;
1220 if (!is_gimple_debug (copy))
1221 FOR_EACH_SSA_USE_OPERAND (use_p, copy, iter, SSA_OP_USE)
1223 tree old_name = USE_FROM_PTR (use_p);
1225 if (TREE_CODE (old_name) != SSA_NAME
1226 || SSA_NAME_IS_DEFAULT_DEF (old_name))
1227 continue;
1229 tree *new_expr = region->parameter_rename_map->get (old_name);
1230 tree new_name;
1231 if (!new_expr
1232 && scev_analyzable_p (old_name, region->region))
1234 gimple_seq stmts = NULL;
1235 new_name = get_rename_from_scev (old_name, &stmts,
1236 bb->loop_father, iv_map);
1237 if (! codegen_error_p ())
1238 gsi_insert_earliest (stmts);
1239 new_expr = &new_name;
1242 if (!new_expr)
1243 continue;
1245 replace_exp (use_p, *new_expr);
1248 update_stmt (copy);
1251 return true;
1255 /* Copies BB and includes in the copied BB all the statements that can
1256 be reached following the use-def chains from the memory accesses,
1257 and returns the next edge following this new block. */
1259 edge translate_isl_ast_to_gimple::
1260 copy_bb_and_scalar_dependences (basic_block bb, edge next_e, vec<tree> iv_map)
1262 basic_block new_bb = split_edge (next_e);
1263 gimple_stmt_iterator gsi_tgt = gsi_last_bb (new_bb);
1264 for (gphi_iterator psi = gsi_start_phis (bb); !gsi_end_p (psi);
1265 gsi_next (&psi))
1267 gphi *phi = psi.phi ();
1268 tree res = gimple_phi_result (phi);
1269 if (virtual_operand_p (res)
1270 || scev_analyzable_p (res, region->region))
1271 continue;
1273 tree new_phi_def;
1274 vec <tree> *renames = region->rename_map->get (res);
1275 if (! renames || renames->is_empty ())
1277 new_phi_def = create_tmp_reg (TREE_TYPE (res));
1278 set_rename (res, new_phi_def);
1280 else
1282 gcc_assert (renames->length () == 1);
1283 new_phi_def = (*renames)[0];
1286 gassign *ass = gimple_build_assign (NULL_TREE, new_phi_def);
1287 create_new_def_for (res, ass, NULL);
1288 gsi_insert_after (&gsi_tgt, ass, GSI_NEW_STMT);
1291 vec <basic_block> *copied_bbs = region->copied_bb_map->get (bb);
1292 if (copied_bbs)
1293 copied_bbs->safe_push (new_bb);
1294 else
1296 vec<basic_block> bbs;
1297 bbs.create (2);
1298 bbs.safe_push (new_bb);
1299 region->copied_bb_map->put (bb, bbs);
1302 if (!graphite_copy_stmts_from_block (bb, new_bb, iv_map))
1304 set_codegen_error ();
1305 return NULL;
1308 /* Insert out-of SSA copies on the original BB outgoing edges. */
1309 gsi_tgt = gsi_last_bb (new_bb);
1310 basic_block bb_for_succs = bb;
1311 if (bb_for_succs == bb_for_succs->loop_father->latch
1312 && bb_in_sese_p (bb_for_succs, region->region)
1313 && sese_trivially_empty_bb_p (bb_for_succs))
1314 bb_for_succs = NULL;
1315 while (bb_for_succs)
1317 basic_block latch = NULL;
1318 edge_iterator ei;
1319 edge e;
1320 FOR_EACH_EDGE (e, ei, bb_for_succs->succs)
1322 for (gphi_iterator psi = gsi_start_phis (e->dest); !gsi_end_p (psi);
1323 gsi_next (&psi))
1325 gphi *phi = psi.phi ();
1326 tree res = gimple_phi_result (phi);
1327 if (virtual_operand_p (res)
1328 || scev_analyzable_p (res, region->region))
1329 continue;
1331 tree new_phi_def;
1332 vec <tree> *renames = region->rename_map->get (res);
1333 if (! renames || renames->is_empty ())
1335 new_phi_def = create_tmp_reg (TREE_TYPE (res));
1336 set_rename (res, new_phi_def);
1338 else
1340 gcc_assert (renames->length () == 1);
1341 new_phi_def = (*renames)[0];
1344 tree arg = PHI_ARG_DEF_FROM_EDGE (phi, e);
1345 if (TREE_CODE (arg) == SSA_NAME
1346 && scev_analyzable_p (arg, region->region))
1348 gimple_seq stmts = NULL;
1349 tree new_name = get_rename_from_scev (arg, &stmts,
1350 bb->loop_father,
1351 iv_map);
1352 if (! codegen_error_p ())
1353 gsi_insert_earliest (stmts);
1354 arg = new_name;
1356 gassign *ass = gimple_build_assign (new_phi_def, arg);
1357 gsi_insert_after (&gsi_tgt, ass, GSI_NEW_STMT);
1359 if (e->dest == bb_for_succs->loop_father->latch
1360 && bb_in_sese_p (e->dest, region->region)
1361 && sese_trivially_empty_bb_p (e->dest))
1362 latch = e->dest;
1364 bb_for_succs = latch;
1367 return single_succ_edge (new_bb);
1370 /* Add isl's parameter identifiers and corresponding trees to ivs_params. */
1372 void translate_isl_ast_to_gimple::
1373 add_parameters_to_ivs_params (scop_p scop, ivs_params &ip)
1375 sese_info_p region = scop->scop_info;
1376 unsigned nb_parameters = isl_set_dim (scop->param_context, isl_dim_param);
1377 gcc_assert (nb_parameters == region->params.length ());
1378 unsigned i;
1379 for (i = 0; i < nb_parameters; i++)
1381 isl_id *tmp_id = isl_set_get_dim_id (scop->param_context,
1382 isl_dim_param, i);
1383 ip[tmp_id] = region->params[i];
1388 /* Generates a build, which specifies the constraints on the parameters. */
1390 __isl_give isl_ast_build *translate_isl_ast_to_gimple::
1391 generate_isl_context (scop_p scop)
1393 isl_set *context_isl = isl_set_params (isl_set_copy (scop->param_context));
1394 return isl_ast_build_from_context (context_isl);
1397 /* This method is executed before the construction of a for node. */
1398 __isl_give isl_id *
1399 ast_build_before_for (__isl_keep isl_ast_build *build, void *user)
1401 isl_union_map *dependences = (isl_union_map *) user;
1402 ast_build_info *for_info = XNEW (struct ast_build_info);
1403 isl_union_map *schedule = isl_ast_build_get_schedule (build);
1404 isl_space *schedule_space = isl_ast_build_get_schedule_space (build);
1405 int dimension = isl_space_dim (schedule_space, isl_dim_out);
1406 for_info->is_parallelizable =
1407 !carries_deps (schedule, dependences, dimension);
1408 isl_union_map_free (schedule);
1409 isl_space_free (schedule_space);
1410 isl_id *id = isl_id_alloc (isl_ast_build_get_ctx (build), "", for_info);
1411 return id;
1414 /* Generate isl AST from schedule of SCOP. */
1416 __isl_give isl_ast_node *translate_isl_ast_to_gimple::
1417 scop_to_isl_ast (scop_p scop)
1419 gcc_assert (scop->transformed_schedule);
1421 /* Set the separate option to reduce control flow overhead. */
1422 isl_schedule *schedule = isl_schedule_map_schedule_node_bottom_up
1423 (isl_schedule_copy (scop->transformed_schedule), set_separate_option, NULL);
1424 isl_ast_build *context_isl = generate_isl_context (scop);
1426 if (flag_loop_parallelize_all)
1428 scop_get_dependences (scop);
1429 context_isl =
1430 isl_ast_build_set_before_each_for (context_isl, ast_build_before_for,
1431 scop->dependence);
1434 isl_ast_node *ast_isl = isl_ast_build_node_from_schedule
1435 (context_isl, schedule);
1436 isl_ast_build_free (context_isl);
1437 return ast_isl;
1440 /* Copy def from sese REGION to the newly created TO_REGION. TR is defined by
1441 DEF_STMT. GSI points to entry basic block of the TO_REGION. */
1443 static void
1444 copy_def (tree tr, gimple *def_stmt, sese_info_p region, sese_info_p to_region,
1445 gimple_stmt_iterator *gsi)
1447 if (!defined_in_sese_p (tr, region->region))
1448 return;
1450 ssa_op_iter iter;
1451 use_operand_p use_p;
1452 FOR_EACH_SSA_USE_OPERAND (use_p, def_stmt, iter, SSA_OP_USE)
1454 tree use_tr = USE_FROM_PTR (use_p);
1456 /* Do not copy parameters that have been generated in the header of the
1457 scop. */
1458 if (region->parameter_rename_map->get(use_tr))
1459 continue;
1461 gimple *def_of_use = SSA_NAME_DEF_STMT (use_tr);
1462 if (!def_of_use)
1463 continue;
1465 copy_def (use_tr, def_of_use, region, to_region, gsi);
1468 gimple *copy = gimple_copy (def_stmt);
1469 gsi_insert_after (gsi, copy, GSI_NEW_STMT);
1471 /* Create new names for all the definitions created by COPY and
1472 add replacement mappings for each new name. */
1473 def_operand_p def_p;
1474 ssa_op_iter op_iter;
1475 FOR_EACH_SSA_DEF_OPERAND (def_p, copy, op_iter, SSA_OP_ALL_DEFS)
1477 tree old_name = DEF_FROM_PTR (def_p);
1478 tree new_name = create_new_def_for (old_name, copy, def_p);
1479 region->parameter_rename_map->put(old_name, new_name);
1482 update_stmt (copy);
1485 static void
1486 copy_internal_parameters (sese_info_p region, sese_info_p to_region)
1488 /* For all the parameters which definitino is in the if_region->false_region,
1489 insert code on true_region (if_region->true_region->entry). */
1491 int i;
1492 tree tr;
1493 gimple_stmt_iterator gsi = gsi_start_bb(to_region->region.entry->dest);
1495 FOR_EACH_VEC_ELT (region->params, i, tr)
1497 // If def is not in region.
1498 gimple *def_stmt = SSA_NAME_DEF_STMT (tr);
1499 if (def_stmt)
1500 copy_def (tr, def_stmt, region, to_region, &gsi);
1504 /* GIMPLE Loop Generator: generates loops in GIMPLE form for the given SCOP.
1505 Return true if code generation succeeded. */
1507 bool
1508 graphite_regenerate_ast_isl (scop_p scop)
1510 sese_info_p region = scop->scop_info;
1511 translate_isl_ast_to_gimple t (region);
1513 ifsese if_region = NULL;
1514 isl_ast_node *root_node;
1515 ivs_params ip;
1517 timevar_push (TV_GRAPHITE_CODE_GEN);
1518 t.add_parameters_to_ivs_params (scop, ip);
1519 root_node = t.scop_to_isl_ast (scop);
1521 if (dump_file && (dump_flags & TDF_DETAILS))
1523 fprintf (dump_file, "[scheduler] original schedule:\n");
1524 print_isl_schedule (dump_file, scop->original_schedule);
1525 fprintf (dump_file, "[scheduler] isl transformed schedule:\n");
1526 print_isl_schedule (dump_file, scop->transformed_schedule);
1528 fprintf (dump_file, "[scheduler] original ast:\n");
1529 print_schedule_ast (dump_file, scop->original_schedule, scop);
1530 fprintf (dump_file, "[scheduler] AST generated by isl:\n");
1531 print_isl_ast (dump_file, root_node);
1534 if_region = move_sese_in_condition (region);
1535 region->if_region = if_region;
1537 loop_p context_loop = region->region.entry->src->loop_father;
1539 /* Copy all the parameters which are defined in the region. */
1540 copy_internal_parameters(if_region->false_region, if_region->true_region);
1542 edge e = single_succ_edge (if_region->true_region->region.entry->dest);
1543 basic_block bb = split_edge (e);
1545 /* Update the true_region exit edge. */
1546 region->if_region->true_region->region.exit = single_succ_edge (bb);
1548 t.translate_isl_ast (context_loop, root_node, e, ip);
1549 if (! t.codegen_error_p ())
1551 sese_insert_phis_for_liveouts (region,
1552 if_region->region->region.exit->src,
1553 if_region->false_region->region.exit,
1554 if_region->true_region->region.exit);
1555 if (dump_file)
1556 fprintf (dump_file, "[codegen] isl AST to Gimple succeeded.\n");
1559 if (t.codegen_error_p ())
1561 if (dump_file)
1562 fprintf (dump_file, "codegen error: "
1563 "reverting back to the original code.\n");
1564 set_ifsese_condition (if_region, integer_zero_node);
1566 /* Remove the unreachable region. */
1567 remove_edge_and_dominated_blocks (if_region->true_region->region.entry);
1568 basic_block ifb = if_region->false_region->region.entry->src;
1569 gimple_stmt_iterator gsi = gsi_last_bb (ifb);
1570 gsi_remove (&gsi, true);
1571 if_region->false_region->region.entry->flags &= ~EDGE_FALSE_VALUE;
1572 if_region->false_region->region.entry->flags |= EDGE_FALLTHRU;
1573 /* remove_edge_and_dominated_blocks marks loops for removal but
1574 doesn't actually remove them (fix that...). */
1575 loop_p loop;
1576 FOR_EACH_LOOP (loop, LI_FROM_INNERMOST)
1577 if (! loop->header)
1578 delete_loop (loop);
1581 /* We are delaying SSA update to after code-generating all SCOPs.
1582 This is because we analyzed DRs and parameters on the unmodified
1583 IL and thus rely on SSA update to pick up new dominating definitions
1584 from for example SESE liveout PHIs. This is also for efficiency
1585 as SSA update does work depending on the size of the function. */
1587 free (if_region->true_region);
1588 free (if_region->region);
1589 free (if_region);
1591 ivs_params_clear (ip);
1592 isl_ast_node_free (root_node);
1593 timevar_pop (TV_GRAPHITE_CODE_GEN);
1595 return !t.codegen_error_p ();
1598 #endif /* HAVE_isl */