PR c/81233
[official-gcc.git] / gcc / graphite-isl-ast-to-gimple.c
blob5b2bc1c399f7836edf9377c3a5a28d357724206a
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 "params.h"
35 #include "fold-const.h"
36 #include "gimple-fold.h"
37 #include "gimple-iterator.h"
38 #include "gimplify.h"
39 #include "gimplify-me.h"
40 #include "tree-eh.h"
41 #include "tree-ssa-loop.h"
42 #include "tree-ssa-operands.h"
43 #include "tree-ssa-propagate.h"
44 #include "tree-pass.h"
45 #include "cfgloop.h"
46 #include "tree-data-ref.h"
47 #include "tree-ssa-loop-manip.h"
48 #include "tree-scalar-evolution.h"
49 #include "gimple-ssa.h"
50 #include "tree-phinodes.h"
51 #include "tree-into-ssa.h"
52 #include "ssa-iterators.h"
53 #include "tree-cfg.h"
54 #include "gimple-pretty-print.h"
55 #include "cfganal.h"
56 #include "value-prof.h"
57 #include "graphite.h"
59 /* We always try to use signed 128 bit types, but fall back to smaller types
60 in case a platform does not provide types of these sizes. In the future we
61 should use isl to derive the optimal type for each subexpression. */
63 static int max_mode_int_precision =
64 GET_MODE_PRECISION (mode_for_size (MAX_FIXED_MODE_SIZE, MODE_INT, 0));
65 static int graphite_expression_type_precision = 128 <= max_mode_int_precision ?
66 128 : max_mode_int_precision;
68 struct ast_build_info
70 ast_build_info()
71 : is_parallelizable(false)
72 { }
73 bool is_parallelizable;
76 /* Verifies properties that GRAPHITE should maintain during translation. */
78 static inline void
79 graphite_verify (void)
81 checking_verify_loop_structure ();
82 checking_verify_loop_closed_ssa (true);
85 /* IVS_PARAMS maps isl's scattering and parameter identifiers
86 to corresponding trees. */
88 typedef std::map<isl_id *, tree> ivs_params;
90 /* Free all memory allocated for isl's identifiers. */
92 static void ivs_params_clear (ivs_params &ip)
94 std::map<isl_id *, tree>::iterator it;
95 for (it = ip.begin ();
96 it != ip.end (); it++)
98 isl_id_free (it->first);
102 /* Set the "separate" option for the schedule node. */
104 static isl_schedule_node *
105 set_separate_option (__isl_take isl_schedule_node *node, void *user)
107 if (user)
108 return node;
110 if (isl_schedule_node_get_type (node) != isl_schedule_node_band)
111 return node;
113 /* Set the "separate" option unless it is set earlier to another option. */
114 if (isl_schedule_node_band_member_get_ast_loop_type (node, 0)
115 == isl_ast_loop_default)
116 return isl_schedule_node_band_member_set_ast_loop_type
117 (node, 0, isl_ast_loop_separate);
119 return node;
122 /* Print SCHEDULE under an AST form on file F. */
124 void
125 print_schedule_ast (FILE *f, __isl_keep isl_schedule *schedule, scop_p scop)
127 isl_set *set = isl_set_params (isl_set_copy (scop->param_context));
128 isl_ast_build *context = isl_ast_build_from_context (set);
129 isl_ast_node *ast
130 = isl_ast_build_node_from_schedule (context, isl_schedule_copy (schedule));
131 isl_ast_build_free (context);
132 print_isl_ast (f, ast);
133 isl_ast_node_free (ast);
136 DEBUG_FUNCTION void
137 debug_schedule_ast (__isl_keep isl_schedule *s, scop_p scop)
139 print_schedule_ast (stderr, s, scop);
142 enum phi_node_kind
144 unknown_phi,
145 loop_phi,
146 close_phi,
147 cond_phi
150 class translate_isl_ast_to_gimple
152 public:
153 translate_isl_ast_to_gimple (sese_info_p r)
154 : region (r), codegen_error (false) { }
155 edge translate_isl_ast (loop_p context_loop, __isl_keep isl_ast_node *node,
156 edge next_e, ivs_params &ip);
157 edge translate_isl_ast_node_for (loop_p context_loop,
158 __isl_keep isl_ast_node *node,
159 edge next_e, ivs_params &ip);
160 edge translate_isl_ast_for_loop (loop_p context_loop,
161 __isl_keep isl_ast_node *node_for,
162 edge next_e,
163 tree type, tree lb, tree ub,
164 ivs_params &ip);
165 edge translate_isl_ast_node_if (loop_p context_loop,
166 __isl_keep isl_ast_node *node,
167 edge next_e, ivs_params &ip);
168 edge translate_isl_ast_node_user (__isl_keep isl_ast_node *node,
169 edge next_e, ivs_params &ip);
170 edge translate_isl_ast_node_block (loop_p context_loop,
171 __isl_keep isl_ast_node *node,
172 edge next_e, ivs_params &ip);
173 tree unary_op_to_tree (tree type, __isl_take isl_ast_expr *expr,
174 ivs_params &ip);
175 tree binary_op_to_tree (tree type, __isl_take isl_ast_expr *expr,
176 ivs_params &ip);
177 tree ternary_op_to_tree (tree type, __isl_take isl_ast_expr *expr,
178 ivs_params &ip);
179 tree nary_op_to_tree (tree type, __isl_take isl_ast_expr *expr,
180 ivs_params &ip);
181 tree gcc_expression_from_isl_expression (tree type,
182 __isl_take isl_ast_expr *,
183 ivs_params &ip);
184 tree gcc_expression_from_isl_ast_expr_id (tree type,
185 __isl_keep isl_ast_expr *expr_id,
186 ivs_params &ip);
187 tree gcc_expression_from_isl_expr_int (tree type,
188 __isl_take isl_ast_expr *expr);
189 tree gcc_expression_from_isl_expr_op (tree type,
190 __isl_take isl_ast_expr *expr,
191 ivs_params &ip);
192 struct loop *graphite_create_new_loop (edge entry_edge,
193 __isl_keep isl_ast_node *node_for,
194 loop_p outer, tree type,
195 tree lb, tree ub, ivs_params &ip);
196 edge graphite_create_new_loop_guard (edge entry_edge,
197 __isl_keep isl_ast_node *node_for,
198 tree *type,
199 tree *lb, tree *ub, ivs_params &ip);
200 edge graphite_create_new_guard (edge entry_edge,
201 __isl_take isl_ast_expr *if_cond,
202 ivs_params &ip);
203 void build_iv_mapping (vec<tree> iv_map, gimple_poly_bb_p gbb,
204 __isl_keep isl_ast_expr *user_expr, ivs_params &ip,
205 sese_l &region);
206 void translate_pending_phi_nodes (void);
207 void add_parameters_to_ivs_params (scop_p scop, ivs_params &ip);
208 __isl_give isl_ast_build *generate_isl_context (scop_p scop);
210 __isl_give isl_ast_node * scop_to_isl_ast (scop_p scop);
212 bool is_valid_rename (tree rename, basic_block def_bb, basic_block use_bb,
213 phi_node_kind, tree old_name, basic_block old_bb) const;
214 tree get_rename (basic_block new_bb, tree old_name,
215 basic_block old_bb, phi_node_kind) const;
216 tree get_rename_from_scev (tree old_name, gimple_seq *stmts, loop_p loop,
217 basic_block new_bb, basic_block old_bb,
218 vec<tree> iv_map);
219 basic_block get_def_bb_for_const (basic_block bb, basic_block old_bb) const;
220 tree get_new_name (basic_block new_bb, tree op,
221 basic_block old_bb, phi_node_kind) const;
222 void collect_all_ssa_names (tree new_expr, vec<tree> *vec_ssa);
223 bool copy_loop_phi_args (gphi *old_phi, init_back_edge_pair_t &ibp_old_bb,
224 gphi *new_phi, init_back_edge_pair_t &ibp_new_bb,
225 bool postpone);
226 bool copy_loop_phi_nodes (basic_block bb, basic_block new_bb);
227 bool add_close_phis_to_merge_points (gphi *old_phi, gphi *new_phi,
228 tree default_value);
229 tree add_close_phis_to_outer_loops (tree last_merge_name, edge merge_e,
230 gimple *old_close_phi);
231 bool copy_loop_close_phi_args (basic_block old_bb, basic_block new_bb,
232 vec<tree> iv_map, bool postpone);
233 bool copy_loop_close_phi_nodes (basic_block old_bb, basic_block new_bb,
234 vec<tree> iv_map);
235 bool copy_cond_phi_args (gphi *phi, gphi *new_phi, vec<tree> iv_map,
236 bool postpone);
237 bool copy_cond_phi_nodes (basic_block bb, basic_block new_bb,
238 vec<tree> iv_map);
239 bool graphite_copy_stmts_from_block (basic_block bb, basic_block new_bb,
240 vec<tree> iv_map);
241 edge copy_bb_and_scalar_dependences (basic_block bb, edge next_e,
242 vec<tree> iv_map);
243 edge edge_for_new_close_phis (basic_block bb);
244 bool add_phi_arg_for_new_expr (tree old_phi_args[2], tree new_phi_args[2],
245 edge old_bb_dominating_edge,
246 edge old_bb_non_dominating_edge,
247 gphi *phi, gphi *new_phi,
248 basic_block new_bb);
249 bool rename_uses (gimple *copy, gimple_stmt_iterator *gsi_tgt,
250 basic_block old_bb, loop_p loop, vec<tree> iv_map);
251 void set_rename (tree old_name, tree expr);
252 void set_rename_for_each_def (gimple *stmt);
253 void gsi_insert_earliest (gimple_seq seq);
254 tree rename_all_uses (tree new_expr, basic_block new_bb, basic_block old_bb);
255 bool codegen_error_p () const { return codegen_error; }
256 bool is_constant (tree op) const
258 return TREE_CODE (op) == INTEGER_CST
259 || TREE_CODE (op) == REAL_CST
260 || TREE_CODE (op) == COMPLEX_CST
261 || TREE_CODE (op) == VECTOR_CST;
264 private:
265 /* The region to be translated. */
266 sese_info_p region;
268 /* This flag is set when an error occurred during the translation of isl AST
269 to Gimple. */
270 bool codegen_error;
272 /* A vector of all the edges at if_condition merge points. */
273 auto_vec<edge, 2> merge_points;
276 /* Return the tree variable that corresponds to the given isl ast identifier
277 expression (an isl_ast_expr of type isl_ast_expr_id).
279 FIXME: We should replace blind conversion of id's type with derivation
280 of the optimal type when we get the corresponding isl support. Blindly
281 converting type sizes may be problematic when we switch to smaller
282 types. */
284 tree translate_isl_ast_to_gimple::
285 gcc_expression_from_isl_ast_expr_id (tree type,
286 __isl_take isl_ast_expr *expr_id,
287 ivs_params &ip)
289 gcc_assert (isl_ast_expr_get_type (expr_id) == isl_ast_expr_id);
290 isl_id *tmp_isl_id = isl_ast_expr_get_id (expr_id);
291 std::map<isl_id *, tree>::iterator res;
292 res = ip.find (tmp_isl_id);
293 isl_id_free (tmp_isl_id);
294 gcc_assert (res != ip.end () &&
295 "Could not map isl_id to tree expression");
296 isl_ast_expr_free (expr_id);
297 tree t = res->second;
298 tree *val = region->parameter_rename_map->get(t);
300 if (!val)
301 val = &t;
302 return fold_convert (type, *val);
305 /* Converts an isl_ast_expr_int expression E to a GCC expression tree of
306 type TYPE. */
308 tree translate_isl_ast_to_gimple::
309 gcc_expression_from_isl_expr_int (tree type, __isl_take isl_ast_expr *expr)
311 gcc_assert (isl_ast_expr_get_type (expr) == isl_ast_expr_int);
312 isl_val *val = isl_ast_expr_get_val (expr);
313 size_t n = isl_val_n_abs_num_chunks (val, sizeof (HOST_WIDE_INT));
314 HOST_WIDE_INT *chunks = XALLOCAVEC (HOST_WIDE_INT, n);
315 tree res;
316 if (isl_val_get_abs_num_chunks (val, sizeof (HOST_WIDE_INT), chunks) == -1)
317 res = NULL_TREE;
318 else
320 widest_int wi = widest_int::from_array (chunks, n, true);
321 if (isl_val_is_neg (val))
322 wi = -wi;
323 res = wide_int_to_tree (type, wi);
325 isl_val_free (val);
326 isl_ast_expr_free (expr);
327 return res;
330 /* Converts a binary isl_ast_expr_op expression E to a GCC expression tree of
331 type TYPE. */
333 tree translate_isl_ast_to_gimple::
334 binary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
336 isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0);
337 tree tree_lhs_expr = gcc_expression_from_isl_expression (type, arg_expr, ip);
338 arg_expr = isl_ast_expr_get_op_arg (expr, 1);
339 tree tree_rhs_expr = gcc_expression_from_isl_expression (type, arg_expr, ip);
341 enum isl_ast_op_type expr_type = isl_ast_expr_get_op_type (expr);
342 isl_ast_expr_free (expr);
344 if (codegen_error_p ())
345 return NULL_TREE;
347 switch (expr_type)
349 case isl_ast_op_add:
350 return fold_build2 (PLUS_EXPR, type, tree_lhs_expr, tree_rhs_expr);
352 case isl_ast_op_sub:
353 return fold_build2 (MINUS_EXPR, type, tree_lhs_expr, tree_rhs_expr);
355 case isl_ast_op_mul:
356 return fold_build2 (MULT_EXPR, type, tree_lhs_expr, tree_rhs_expr);
358 case isl_ast_op_div:
359 /* As isl operates on arbitrary precision numbers, we may end up with
360 division by 2^64 that is folded to 0. */
361 if (integer_zerop (tree_rhs_expr))
363 codegen_error = true;
364 return NULL_TREE;
366 return fold_build2 (EXACT_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr);
368 case isl_ast_op_pdiv_q:
369 /* As isl operates on arbitrary precision numbers, we may end up with
370 division by 2^64 that is folded to 0. */
371 if (integer_zerop (tree_rhs_expr))
373 codegen_error = true;
374 return NULL_TREE;
376 return fold_build2 (TRUNC_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr);
378 case isl_ast_op_zdiv_r:
379 case isl_ast_op_pdiv_r:
380 /* As isl operates on arbitrary precision numbers, we may end up with
381 division by 2^64 that is folded to 0. */
382 if (integer_zerop (tree_rhs_expr))
384 codegen_error = true;
385 return NULL_TREE;
387 return fold_build2 (TRUNC_MOD_EXPR, type, tree_lhs_expr, tree_rhs_expr);
389 case isl_ast_op_fdiv_q:
390 /* As isl operates on arbitrary precision numbers, we may end up with
391 division by 2^64 that is folded to 0. */
392 if (integer_zerop (tree_rhs_expr))
394 codegen_error = true;
395 return NULL_TREE;
397 return fold_build2 (FLOOR_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr);
399 case isl_ast_op_and:
400 return fold_build2 (TRUTH_ANDIF_EXPR, type,
401 tree_lhs_expr, tree_rhs_expr);
403 case isl_ast_op_or:
404 return fold_build2 (TRUTH_ORIF_EXPR, type, tree_lhs_expr, tree_rhs_expr);
406 case isl_ast_op_eq:
407 return fold_build2 (EQ_EXPR, type, tree_lhs_expr, tree_rhs_expr);
409 case isl_ast_op_le:
410 return fold_build2 (LE_EXPR, type, tree_lhs_expr, tree_rhs_expr);
412 case isl_ast_op_lt:
413 return fold_build2 (LT_EXPR, type, tree_lhs_expr, tree_rhs_expr);
415 case isl_ast_op_ge:
416 return fold_build2 (GE_EXPR, type, tree_lhs_expr, tree_rhs_expr);
418 case isl_ast_op_gt:
419 return fold_build2 (GT_EXPR, type, tree_lhs_expr, tree_rhs_expr);
421 default:
422 gcc_unreachable ();
426 /* Converts a ternary isl_ast_expr_op expression E to a GCC expression tree of
427 type TYPE. */
429 tree translate_isl_ast_to_gimple::
430 ternary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
432 enum isl_ast_op_type t = isl_ast_expr_get_op_type (expr);
433 gcc_assert (t == isl_ast_op_cond || t == isl_ast_op_select);
434 isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0);
435 tree a = gcc_expression_from_isl_expression (type, arg_expr, ip);
436 arg_expr = isl_ast_expr_get_op_arg (expr, 1);
437 tree b = gcc_expression_from_isl_expression (type, arg_expr, ip);
438 arg_expr = isl_ast_expr_get_op_arg (expr, 2);
439 tree c = gcc_expression_from_isl_expression (type, arg_expr, ip);
440 isl_ast_expr_free (expr);
442 if (codegen_error_p ())
443 return NULL_TREE;
445 return fold_build3 (COND_EXPR, type, a, b, c);
448 /* Converts a unary isl_ast_expr_op expression E to a GCC expression tree of
449 type TYPE. */
451 tree translate_isl_ast_to_gimple::
452 unary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
454 gcc_assert (isl_ast_expr_get_op_type (expr) == isl_ast_op_minus);
455 isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0);
456 tree tree_expr = gcc_expression_from_isl_expression (type, arg_expr, ip);
457 isl_ast_expr_free (expr);
458 return codegen_error_p () ? NULL_TREE
459 : fold_build1 (NEGATE_EXPR, type, tree_expr);
462 /* Converts an isl_ast_expr_op expression E with unknown number of arguments
463 to a GCC expression tree of type TYPE. */
465 tree translate_isl_ast_to_gimple::
466 nary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
468 enum tree_code op_code;
469 switch (isl_ast_expr_get_op_type (expr))
471 case isl_ast_op_max:
472 op_code = MAX_EXPR;
473 break;
475 case isl_ast_op_min:
476 op_code = MIN_EXPR;
477 break;
479 default:
480 gcc_unreachable ();
482 isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0);
483 tree res = gcc_expression_from_isl_expression (type, arg_expr, ip);
485 if (codegen_error_p ())
487 isl_ast_expr_free (expr);
488 return NULL_TREE;
491 int i;
492 for (i = 1; i < isl_ast_expr_get_op_n_arg (expr); i++)
494 arg_expr = isl_ast_expr_get_op_arg (expr, i);
495 tree t = gcc_expression_from_isl_expression (type, arg_expr, ip);
497 if (codegen_error_p ())
499 isl_ast_expr_free (expr);
500 return NULL_TREE;
503 res = fold_build2 (op_code, type, res, t);
505 isl_ast_expr_free (expr);
506 return res;
509 /* Converts an isl_ast_expr_op expression E to a GCC expression tree of
510 type TYPE. */
512 tree translate_isl_ast_to_gimple::
513 gcc_expression_from_isl_expr_op (tree type, __isl_take isl_ast_expr *expr,
514 ivs_params &ip)
516 if (codegen_error_p ())
518 isl_ast_expr_free (expr);
519 return NULL_TREE;
522 gcc_assert (isl_ast_expr_get_type (expr) == isl_ast_expr_op);
523 switch (isl_ast_expr_get_op_type (expr))
525 /* These isl ast expressions are not supported yet. */
526 case isl_ast_op_error:
527 case isl_ast_op_call:
528 case isl_ast_op_and_then:
529 case isl_ast_op_or_else:
530 gcc_unreachable ();
532 case isl_ast_op_max:
533 case isl_ast_op_min:
534 return nary_op_to_tree (type, expr, ip);
536 case isl_ast_op_add:
537 case isl_ast_op_sub:
538 case isl_ast_op_mul:
539 case isl_ast_op_div:
540 case isl_ast_op_pdiv_q:
541 case isl_ast_op_pdiv_r:
542 case isl_ast_op_fdiv_q:
543 case isl_ast_op_zdiv_r:
544 case isl_ast_op_and:
545 case isl_ast_op_or:
546 case isl_ast_op_eq:
547 case isl_ast_op_le:
548 case isl_ast_op_lt:
549 case isl_ast_op_ge:
550 case isl_ast_op_gt:
551 return binary_op_to_tree (type, expr, ip);
553 case isl_ast_op_minus:
554 return unary_op_to_tree (type, expr, ip);
556 case isl_ast_op_cond:
557 case isl_ast_op_select:
558 return ternary_op_to_tree (type, expr, ip);
560 default:
561 gcc_unreachable ();
564 return NULL_TREE;
567 /* Converts an isl AST expression E back to a GCC expression tree of
568 type TYPE. */
570 tree translate_isl_ast_to_gimple::
571 gcc_expression_from_isl_expression (tree type, __isl_take isl_ast_expr *expr,
572 ivs_params &ip)
574 if (codegen_error_p ())
576 isl_ast_expr_free (expr);
577 return NULL_TREE;
580 switch (isl_ast_expr_get_type (expr))
582 case isl_ast_expr_id:
583 return gcc_expression_from_isl_ast_expr_id (type, expr, ip);
585 case isl_ast_expr_int:
586 return gcc_expression_from_isl_expr_int (type, expr);
588 case isl_ast_expr_op:
589 return gcc_expression_from_isl_expr_op (type, expr, ip);
591 default:
592 gcc_unreachable ();
595 return NULL_TREE;
598 /* Creates a new LOOP corresponding to isl_ast_node_for. Inserts an
599 induction variable for the new LOOP. New LOOP is attached to CFG
600 starting at ENTRY_EDGE. LOOP is inserted into the loop tree and
601 becomes the child loop of the OUTER_LOOP. NEWIVS_INDEX binds
602 isl's scattering name to the induction variable created for the
603 loop of STMT. The new induction variable is inserted in the NEWIVS
604 vector and is of type TYPE. */
606 struct loop *translate_isl_ast_to_gimple::
607 graphite_create_new_loop (edge entry_edge, __isl_keep isl_ast_node *node_for,
608 loop_p outer, tree type, tree lb, tree ub,
609 ivs_params &ip)
611 isl_ast_expr *for_inc = isl_ast_node_for_get_inc (node_for);
612 tree stride = gcc_expression_from_isl_expression (type, for_inc, ip);
614 /* To fail code generation, we generate wrong code until we discard it. */
615 if (codegen_error_p ())
616 stride = integer_zero_node;
618 tree ivvar = create_tmp_var (type, "graphite_IV");
619 tree iv, iv_after_increment;
620 loop_p loop = create_empty_loop_on_edge
621 (entry_edge, lb, stride, ub, ivvar, &iv, &iv_after_increment,
622 outer ? outer : entry_edge->src->loop_father);
624 isl_ast_expr *for_iterator = isl_ast_node_for_get_iterator (node_for);
625 isl_id *id = isl_ast_expr_get_id (for_iterator);
626 std::map<isl_id *, tree>::iterator res;
627 res = ip.find (id);
628 if (ip.count (id))
629 isl_id_free (res->first);
630 ip[id] = iv;
631 isl_ast_expr_free (for_iterator);
632 return loop;
635 /* Create the loop for a isl_ast_node_for.
637 - NEXT_E is the edge where new generated code should be attached. */
639 edge translate_isl_ast_to_gimple::
640 translate_isl_ast_for_loop (loop_p context_loop,
641 __isl_keep isl_ast_node *node_for, edge next_e,
642 tree type, tree lb, tree ub,
643 ivs_params &ip)
645 gcc_assert (isl_ast_node_get_type (node_for) == isl_ast_node_for);
646 struct loop *loop = graphite_create_new_loop (next_e, node_for, context_loop,
647 type, lb, ub, ip);
648 edge last_e = single_exit (loop);
649 edge to_body = single_succ_edge (loop->header);
650 basic_block after = to_body->dest;
652 /* Translate the body of the loop. */
653 isl_ast_node *for_body = isl_ast_node_for_get_body (node_for);
654 next_e = translate_isl_ast (loop, for_body, to_body, ip);
655 isl_ast_node_free (for_body);
657 /* Early return if we failed to translate loop body. */
658 if (!next_e || codegen_error_p ())
659 return NULL;
661 if (next_e->dest != after)
662 redirect_edge_succ_nodup (next_e, after);
663 set_immediate_dominator (CDI_DOMINATORS, next_e->dest, next_e->src);
665 if (flag_loop_parallelize_all)
667 isl_id *id = isl_ast_node_get_annotation (node_for);
668 gcc_assert (id);
669 ast_build_info *for_info = (ast_build_info *) isl_id_get_user (id);
670 loop->can_be_parallel = for_info->is_parallelizable;
671 free (for_info);
672 isl_id_free (id);
675 return last_e;
678 /* We use this function to get the upper bound because of the form,
679 which is used by isl to represent loops:
681 for (iterator = init; cond; iterator += inc)
689 The loop condition is an arbitrary expression, which contains the
690 current loop iterator.
692 (e.g. iterator + 3 < B && C > iterator + A)
694 We have to know the upper bound of the iterator to generate a loop
695 in Gimple form. It can be obtained from the special representation
696 of the loop condition, which is generated by isl,
697 if the ast_build_atomic_upper_bound option is set. In this case,
698 isl generates a loop condition that consists of the current loop
699 iterator, + an operator (< or <=) and an expression not involving
700 the iterator, which is processed and returned by this function.
702 (e.g iterator <= upper-bound-expression-without-iterator) */
704 static __isl_give isl_ast_expr *
705 get_upper_bound (__isl_keep isl_ast_node *node_for)
707 gcc_assert (isl_ast_node_get_type (node_for) == isl_ast_node_for);
708 isl_ast_expr *for_cond = isl_ast_node_for_get_cond (node_for);
709 gcc_assert (isl_ast_expr_get_type (for_cond) == isl_ast_expr_op);
710 isl_ast_expr *res;
711 switch (isl_ast_expr_get_op_type (for_cond))
713 case isl_ast_op_le:
714 res = isl_ast_expr_get_op_arg (for_cond, 1);
715 break;
717 case isl_ast_op_lt:
719 /* (iterator < ub) => (iterator <= ub - 1). */
720 isl_val *one =
721 isl_val_int_from_si (isl_ast_expr_get_ctx (for_cond), 1);
722 isl_ast_expr *ub = isl_ast_expr_get_op_arg (for_cond, 1);
723 res = isl_ast_expr_sub (ub, isl_ast_expr_from_val (one));
724 break;
727 default:
728 gcc_unreachable ();
730 isl_ast_expr_free (for_cond);
731 return res;
734 /* All loops generated by create_empty_loop_on_edge have the form of
735 a post-test loop:
740 body of the loop;
741 } while (lower bound < upper bound);
743 We create a new if region protecting the loop to be executed, if
744 the execution count is zero (lower bound > upper bound). */
746 edge translate_isl_ast_to_gimple::
747 graphite_create_new_loop_guard (edge entry_edge,
748 __isl_keep isl_ast_node *node_for, tree *type,
749 tree *lb, tree *ub, ivs_params &ip)
751 gcc_assert (isl_ast_node_get_type (node_for) == isl_ast_node_for);
752 tree cond_expr;
753 edge exit_edge;
755 *type =
756 build_nonstandard_integer_type (graphite_expression_type_precision, 0);
757 isl_ast_expr *for_init = isl_ast_node_for_get_init (node_for);
758 *lb = gcc_expression_from_isl_expression (*type, for_init, ip);
760 /* To fail code generation, we generate wrong code until we discard it. */
761 if (codegen_error_p ())
762 *lb = integer_zero_node;
764 isl_ast_expr *upper_bound = get_upper_bound (node_for);
765 *ub = gcc_expression_from_isl_expression (*type, upper_bound, ip);
767 /* To fail code generation, we generate wrong code until we discard it. */
768 if (codegen_error_p ())
769 *ub = integer_zero_node;
771 /* When ub is simply a constant or a parameter, use lb <= ub. */
772 if (TREE_CODE (*ub) == INTEGER_CST || TREE_CODE (*ub) == SSA_NAME)
773 cond_expr = fold_build2 (LE_EXPR, boolean_type_node, *lb, *ub);
774 else
776 tree one = (POINTER_TYPE_P (*type)
777 ? convert_to_ptrofftype (integer_one_node)
778 : fold_convert (*type, integer_one_node));
779 /* Adding +1 and using LT_EXPR helps with loop latches that have a
780 loop iteration count of "PARAMETER - 1". For PARAMETER == 0 this
781 becomes 2^k-1 due to integer overflow, and the condition lb <= ub
782 is true, even if we do not want this. However lb < ub + 1 is false,
783 as expected. */
784 tree ub_one = fold_build2 (POINTER_TYPE_P (*type) ? POINTER_PLUS_EXPR
785 : PLUS_EXPR, *type, *ub, one);
787 cond_expr = fold_build2 (LT_EXPR, boolean_type_node, *lb, ub_one);
790 if (integer_onep (cond_expr))
791 exit_edge = entry_edge;
792 else
793 exit_edge = create_empty_if_region_on_edge (entry_edge,
794 unshare_expr (cond_expr));
796 return exit_edge;
799 /* Translates an isl_ast_node_for to Gimple. */
801 edge translate_isl_ast_to_gimple::
802 translate_isl_ast_node_for (loop_p context_loop, __isl_keep isl_ast_node *node,
803 edge next_e, ivs_params &ip)
805 gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_for);
806 tree type, lb, ub;
807 edge last_e = graphite_create_new_loop_guard (next_e, node, &type,
808 &lb, &ub, ip);
810 if (last_e == next_e)
812 /* There was no guard generated. */
813 last_e = single_succ_edge (split_edge (last_e));
815 translate_isl_ast_for_loop (context_loop, node, next_e,
816 type, lb, ub, ip);
817 return last_e;
820 edge true_e = get_true_edge_from_guard_bb (next_e->dest);
821 merge_points.safe_push (last_e);
823 last_e = single_succ_edge (split_edge (last_e));
824 translate_isl_ast_for_loop (context_loop, node, true_e, type, lb, ub, ip);
826 return last_e;
829 /* Inserts in iv_map a tuple (OLD_LOOP->num, NEW_NAME) for the induction
830 variables of the loops around GBB in SESE.
832 FIXME: Instead of using a vec<tree> that maps each loop id to a possible
833 chrec, we could consider using a map<int, tree> that maps loop ids to the
834 corresponding tree expressions. */
836 void translate_isl_ast_to_gimple::
837 build_iv_mapping (vec<tree> iv_map, gimple_poly_bb_p gbb,
838 __isl_keep isl_ast_expr *user_expr, ivs_params &ip,
839 sese_l &region)
841 gcc_assert (isl_ast_expr_get_type (user_expr) == isl_ast_expr_op &&
842 isl_ast_expr_get_op_type (user_expr) == isl_ast_op_call);
843 int i;
844 isl_ast_expr *arg_expr;
845 for (i = 1; i < isl_ast_expr_get_op_n_arg (user_expr); i++)
847 arg_expr = isl_ast_expr_get_op_arg (user_expr, i);
848 tree type =
849 build_nonstandard_integer_type (graphite_expression_type_precision, 0);
850 tree t = gcc_expression_from_isl_expression (type, arg_expr, ip);
852 /* To fail code generation, we generate wrong code until we discard it. */
853 if (codegen_error_p ())
854 t = integer_zero_node;
856 loop_p old_loop = gbb_loop_at_index (gbb, region, i - 1);
857 iv_map[old_loop->num] = t;
861 /* Translates an isl_ast_node_user to Gimple.
863 FIXME: We should remove iv_map.create (loop->num + 1), if it is possible. */
865 edge translate_isl_ast_to_gimple::
866 translate_isl_ast_node_user (__isl_keep isl_ast_node *node,
867 edge next_e, ivs_params &ip)
869 gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_user);
871 isl_ast_expr *user_expr = isl_ast_node_user_get_expr (node);
872 isl_ast_expr *name_expr = isl_ast_expr_get_op_arg (user_expr, 0);
873 gcc_assert (isl_ast_expr_get_type (name_expr) == isl_ast_expr_id);
875 isl_id *name_id = isl_ast_expr_get_id (name_expr);
876 poly_bb_p pbb = (poly_bb_p) isl_id_get_user (name_id);
877 gcc_assert (pbb);
879 gimple_poly_bb_p gbb = PBB_BLACK_BOX (pbb);
881 isl_ast_expr_free (name_expr);
882 isl_id_free (name_id);
884 gcc_assert (GBB_BB (gbb) != ENTRY_BLOCK_PTR_FOR_FN (cfun) &&
885 "The entry block should not even appear within a scop");
887 const int nb_loops = number_of_loops (cfun);
888 vec<tree> iv_map;
889 iv_map.create (nb_loops);
890 iv_map.safe_grow_cleared (nb_loops);
892 build_iv_mapping (iv_map, gbb, user_expr, ip, pbb->scop->scop_info->region);
893 isl_ast_expr_free (user_expr);
895 basic_block old_bb = GBB_BB (gbb);
896 if (dump_file)
898 fprintf (dump_file,
899 "[codegen] copying from bb_%d on edge (bb_%d, bb_%d)\n",
900 old_bb->index, next_e->src->index, next_e->dest->index);
901 print_loops_bb (dump_file, GBB_BB (gbb), 0, 3);
905 next_e = copy_bb_and_scalar_dependences (old_bb, next_e, iv_map);
907 iv_map.release ();
909 if (codegen_error_p ())
910 return NULL;
912 if (dump_file)
914 fprintf (dump_file, "[codegen] (after copy) new basic block\n");
915 print_loops_bb (dump_file, next_e->src, 0, 3);
918 return next_e;
921 /* Translates an isl_ast_node_block to Gimple. */
923 edge translate_isl_ast_to_gimple::
924 translate_isl_ast_node_block (loop_p context_loop,
925 __isl_keep isl_ast_node *node,
926 edge next_e, ivs_params &ip)
928 gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_block);
929 isl_ast_node_list *node_list = isl_ast_node_block_get_children (node);
930 int i;
931 for (i = 0; i < isl_ast_node_list_n_ast_node (node_list); i++)
933 isl_ast_node *tmp_node = isl_ast_node_list_get_ast_node (node_list, i);
934 next_e = translate_isl_ast (context_loop, tmp_node, next_e, ip);
935 isl_ast_node_free (tmp_node);
937 isl_ast_node_list_free (node_list);
938 return next_e;
941 /* Creates a new if region corresponding to isl's cond. */
943 edge translate_isl_ast_to_gimple::
944 graphite_create_new_guard (edge entry_edge, __isl_take isl_ast_expr *if_cond,
945 ivs_params &ip)
947 tree type =
948 build_nonstandard_integer_type (graphite_expression_type_precision, 0);
949 tree cond_expr = gcc_expression_from_isl_expression (type, if_cond, ip);
951 /* To fail code generation, we generate wrong code until we discard it. */
952 if (codegen_error_p ())
953 cond_expr = integer_zero_node;
955 edge exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr);
956 return exit_edge;
959 /* Translates an isl_ast_node_if to Gimple. */
961 edge translate_isl_ast_to_gimple::
962 translate_isl_ast_node_if (loop_p context_loop,
963 __isl_keep isl_ast_node *node,
964 edge next_e, ivs_params &ip)
966 gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_if);
967 isl_ast_expr *if_cond = isl_ast_node_if_get_cond (node);
968 edge last_e = graphite_create_new_guard (next_e, if_cond, ip);
969 edge true_e = get_true_edge_from_guard_bb (next_e->dest);
970 merge_points.safe_push (last_e);
972 isl_ast_node *then_node = isl_ast_node_if_get_then (node);
973 translate_isl_ast (context_loop, then_node, true_e, ip);
974 isl_ast_node_free (then_node);
976 edge false_e = get_false_edge_from_guard_bb (next_e->dest);
977 isl_ast_node *else_node = isl_ast_node_if_get_else (node);
978 if (isl_ast_node_get_type (else_node) != isl_ast_node_error)
979 translate_isl_ast (context_loop, else_node, false_e, ip);
981 isl_ast_node_free (else_node);
982 return last_e;
985 /* Translates an isl AST node NODE to GCC representation in the
986 context of a SESE. */
988 edge translate_isl_ast_to_gimple::
989 translate_isl_ast (loop_p context_loop, __isl_keep isl_ast_node *node,
990 edge next_e, ivs_params &ip)
992 if (codegen_error_p ())
993 return NULL;
995 switch (isl_ast_node_get_type (node))
997 case isl_ast_node_error:
998 gcc_unreachable ();
1000 case isl_ast_node_for:
1001 return translate_isl_ast_node_for (context_loop, node,
1002 next_e, ip);
1004 case isl_ast_node_if:
1005 return translate_isl_ast_node_if (context_loop, node,
1006 next_e, ip);
1008 case isl_ast_node_user:
1009 return translate_isl_ast_node_user (node, next_e, ip);
1011 case isl_ast_node_block:
1012 return translate_isl_ast_node_block (context_loop, node,
1013 next_e, ip);
1015 case isl_ast_node_mark:
1017 isl_ast_node *n = isl_ast_node_mark_get_node (node);
1018 edge e = translate_isl_ast (context_loop, n, next_e, ip);
1019 isl_ast_node_free (n);
1020 return e;
1023 default:
1024 gcc_unreachable ();
1028 /* Return true when BB contains loop close phi nodes. A loop close phi node is
1029 at the exit of loop which takes one argument that is the last value of the
1030 variable being used out of the loop. */
1032 static bool
1033 bb_contains_loop_close_phi_nodes (basic_block bb)
1035 return single_pred_p (bb)
1036 && bb->loop_father != single_pred_edge (bb)->src->loop_father;
1039 /* Return true when BB contains loop phi nodes. A loop phi node is the loop
1040 header containing phi nodes which has one init-edge and one back-edge. */
1042 static bool
1043 bb_contains_loop_phi_nodes (basic_block bb)
1045 if (EDGE_COUNT (bb->preds) != 2)
1046 return false;
1048 unsigned depth = loop_depth (bb->loop_father);
1050 edge preds[2] = { (*bb->preds)[0], (*bb->preds)[1] };
1052 if (depth > loop_depth (preds[0]->src->loop_father)
1053 || depth > loop_depth (preds[1]->src->loop_father))
1054 return true;
1056 /* When one of the edges correspond to the same loop father and other
1057 doesn't. */
1058 if (bb->loop_father != preds[0]->src->loop_father
1059 && bb->loop_father == preds[1]->src->loop_father)
1060 return true;
1062 if (bb->loop_father != preds[1]->src->loop_father
1063 && bb->loop_father == preds[0]->src->loop_father)
1064 return true;
1066 return false;
1069 /* Check if USE is defined in a basic block from where the definition of USE can
1070 propagate from all the paths. FIXME: Verify checks for virtual operands. */
1072 static bool
1073 is_loop_closed_ssa_use (basic_block bb, tree use)
1075 if (TREE_CODE (use) != SSA_NAME || virtual_operand_p (use))
1076 return true;
1078 /* For close-phi nodes def always comes from a loop which has a back-edge. */
1079 if (bb_contains_loop_close_phi_nodes (bb))
1080 return true;
1082 gimple *def = SSA_NAME_DEF_STMT (use);
1083 basic_block def_bb = gimple_bb (def);
1084 return (!def_bb
1085 || flow_bb_inside_loop_p (def_bb->loop_father, bb));
1088 /* Return the number of phi nodes in BB. */
1090 static int
1091 number_of_phi_nodes (basic_block bb)
1093 int num_phis = 0;
1094 for (gphi_iterator psi = gsi_start_phis (bb); !gsi_end_p (psi);
1095 gsi_next (&psi))
1096 num_phis++;
1097 return num_phis;
1100 /* Returns true if BB uses name in one of its PHIs. */
1102 static bool
1103 phi_uses_name (basic_block bb, tree name)
1105 for (gphi_iterator psi = gsi_start_phis (bb); !gsi_end_p (psi);
1106 gsi_next (&psi))
1108 gphi *phi = psi.phi ();
1109 for (unsigned i = 0; i < gimple_phi_num_args (phi); i++)
1111 tree use_arg = gimple_phi_arg_def (phi, i);
1112 if (use_arg == name)
1113 return true;
1116 return false;
1119 /* Return true if RENAME (defined in BB) is a valid use in NEW_BB. The
1120 definition should flow into use, and the use should respect the loop-closed
1121 SSA form. */
1123 bool translate_isl_ast_to_gimple::
1124 is_valid_rename (tree rename, basic_block def_bb, basic_block use_bb,
1125 phi_node_kind phi_kind, tree old_name, basic_block old_bb) const
1127 if (SSA_NAME_IS_DEFAULT_DEF (rename))
1128 return true;
1130 /* The def of the rename must either dominate the uses or come from a
1131 back-edge. Also the def must respect the loop closed ssa form. */
1132 if (!is_loop_closed_ssa_use (use_bb, rename))
1134 if (dump_file)
1136 fprintf (dump_file, "[codegen] rename not in loop closed ssa: ");
1137 print_generic_expr (dump_file, rename);
1138 fprintf (dump_file, "\n");
1140 return false;
1143 if (dominated_by_p (CDI_DOMINATORS, use_bb, def_bb))
1144 return true;
1146 if (bb_contains_loop_phi_nodes (use_bb) && phi_kind == loop_phi)
1148 /* The loop-header dominates the loop-body. */
1149 if (!dominated_by_p (CDI_DOMINATORS, def_bb, use_bb))
1150 return false;
1152 /* RENAME would be used in loop-phi. */
1153 gcc_assert (number_of_phi_nodes (use_bb));
1155 /* For definitions coming from back edges, we should check that
1156 old_name is used in a loop PHI node.
1157 FIXME: Verify if this is true. */
1158 if (phi_uses_name (old_bb, old_name))
1159 return true;
1161 return false;
1164 /* Returns the expression associated to OLD_NAME (which is used in OLD_BB), in
1165 NEW_BB from RENAME_MAP. PHI_KIND determines the kind of phi node. */
1167 tree translate_isl_ast_to_gimple::
1168 get_rename (basic_block new_bb, tree old_name, basic_block old_bb,
1169 phi_node_kind phi_kind) const
1171 gcc_assert (TREE_CODE (old_name) == SSA_NAME);
1172 vec <tree> *renames = region->rename_map->get (old_name);
1174 if (!renames || renames->is_empty ())
1175 return NULL_TREE;
1177 if (1 == renames->length ())
1179 tree rename = (*renames)[0];
1180 if (TREE_CODE (rename) == SSA_NAME)
1182 basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (rename));
1183 if (is_valid_rename (rename, bb, new_bb, phi_kind, old_name, old_bb)
1184 && (phi_kind == close_phi
1185 || ! bb
1186 || flow_bb_inside_loop_p (bb->loop_father, new_bb)))
1187 return rename;
1188 return NULL_TREE;
1191 if (is_constant (rename))
1192 return rename;
1194 return NULL_TREE;
1197 /* More than one renames corresponding to the old_name. Find the rename for
1198 which the definition flows into usage at new_bb. */
1199 int i;
1200 tree t1 = NULL_TREE, t2;
1201 basic_block t1_bb = NULL;
1202 FOR_EACH_VEC_ELT (*renames, i, t2)
1204 basic_block t2_bb = gimple_bb (SSA_NAME_DEF_STMT (t2));
1206 /* Defined in the same basic block as used. */
1207 if (t2_bb == new_bb)
1208 return t2;
1210 /* NEW_BB and T2_BB are in two unrelated if-clauses. */
1211 if (!dominated_by_p (CDI_DOMINATORS, new_bb, t2_bb))
1212 continue;
1214 if (!flow_bb_inside_loop_p (t2_bb->loop_father, new_bb))
1215 continue;
1217 /* Compute the nearest dominator. */
1218 if (!t1 || dominated_by_p (CDI_DOMINATORS, t2_bb, t1_bb))
1220 t1_bb = t2_bb;
1221 t1 = t2;
1225 return t1;
1228 /* Register in RENAME_MAP the rename tuple (OLD_NAME, EXPR).
1229 When OLD_NAME and EXPR are the same we assert. */
1231 void translate_isl_ast_to_gimple::
1232 set_rename (tree old_name, tree expr)
1234 if (dump_file)
1236 fprintf (dump_file, "[codegen] setting rename: old_name = ");
1237 print_generic_expr (dump_file, old_name);
1238 fprintf (dump_file, ", new_name = ");
1239 print_generic_expr (dump_file, expr);
1240 fprintf (dump_file, "\n");
1243 if (old_name == expr)
1244 return;
1246 vec <tree> *renames = region->rename_map->get (old_name);
1248 if (renames)
1249 renames->safe_push (expr);
1250 else
1252 vec<tree> r;
1253 r.create (2);
1254 r.safe_push (expr);
1255 region->rename_map->put (old_name, r);
1258 tree t;
1259 int i;
1260 /* For a parameter of a scop we don't want to rename it. */
1261 FOR_EACH_VEC_ELT (region->params, i, t)
1262 if (old_name == t)
1263 region->parameter_rename_map->put(old_name, expr);
1266 /* Return an iterator to the instructions comes last in the execution order.
1267 Either GSI1 and GSI2 should belong to the same basic block or one of their
1268 respective basic blocks should dominate the other. */
1270 gimple_stmt_iterator
1271 later_of_the_two (gimple_stmt_iterator gsi1, gimple_stmt_iterator gsi2)
1273 basic_block bb1 = gsi_bb (gsi1);
1274 basic_block bb2 = gsi_bb (gsi2);
1276 /* Find the iterator which is the latest. */
1277 if (bb1 == bb2)
1279 gimple *stmt1 = gsi_stmt (gsi1);
1280 gimple *stmt2 = gsi_stmt (gsi2);
1282 if (stmt1 != NULL && stmt2 != NULL)
1284 bool is_phi1 = gimple_code (stmt1) == GIMPLE_PHI;
1285 bool is_phi2 = gimple_code (stmt2) == GIMPLE_PHI;
1287 if (is_phi1 != is_phi2)
1288 return is_phi1 ? gsi2 : gsi1;
1291 /* For empty basic blocks gsis point to the end of the sequence. Since
1292 there is no operator== defined for gimple_stmt_iterator and for gsis
1293 not pointing to a valid statement gsi_next would assert. */
1294 gimple_stmt_iterator gsi = gsi1;
1295 do {
1296 if (gsi_stmt (gsi) == gsi_stmt (gsi2))
1297 return gsi2;
1298 gsi_next (&gsi);
1299 } while (!gsi_end_p (gsi));
1301 return gsi1;
1304 /* Find the basic block closest to the basic block which defines stmt. */
1305 if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
1306 return gsi1;
1308 gcc_assert (dominated_by_p (CDI_DOMINATORS, bb2, bb1));
1309 return gsi2;
1312 /* Insert each statement from SEQ at its earliest insertion p. */
1314 void translate_isl_ast_to_gimple::
1315 gsi_insert_earliest (gimple_seq seq)
1317 update_modified_stmts (seq);
1318 sese_l &codegen_region = region->if_region->true_region->region;
1319 basic_block begin_bb = get_entry_bb (codegen_region);
1321 /* Inserting the gimple statements in a vector because gimple_seq behave
1322 in strage ways when inserting the stmts from it into different basic
1323 blocks one at a time. */
1324 auto_vec<gimple *, 3> stmts;
1325 for (gimple_stmt_iterator gsi = gsi_start (seq); !gsi_end_p (gsi);
1326 gsi_next (&gsi))
1327 stmts.safe_push (gsi_stmt (gsi));
1329 int i;
1330 gimple *use_stmt;
1331 FOR_EACH_VEC_ELT (stmts, i, use_stmt)
1333 gcc_assert (gimple_code (use_stmt) != GIMPLE_PHI);
1334 gimple_stmt_iterator gsi_def_stmt = gsi_start_bb_nondebug (begin_bb);
1336 use_operand_p use_p;
1337 ssa_op_iter op_iter;
1338 FOR_EACH_SSA_USE_OPERAND (use_p, use_stmt, op_iter, SSA_OP_USE)
1340 /* Iterator to the current def of use_p. For function parameters or
1341 anything where def is not found, insert at the beginning of the
1342 generated region. */
1343 gimple_stmt_iterator gsi_stmt = gsi_def_stmt;
1345 tree op = USE_FROM_PTR (use_p);
1346 gimple *stmt = SSA_NAME_DEF_STMT (op);
1347 if (stmt && (gimple_code (stmt) != GIMPLE_NOP))
1348 gsi_stmt = gsi_for_stmt (stmt);
1350 /* For region parameters, insert at the beginning of the generated
1351 region. */
1352 if (!bb_in_sese_p (gsi_bb (gsi_stmt), codegen_region))
1353 gsi_stmt = gsi_def_stmt;
1355 gsi_def_stmt = later_of_the_two (gsi_stmt, gsi_def_stmt);
1358 if (!gsi_stmt (gsi_def_stmt))
1360 gimple_stmt_iterator gsi = gsi_after_labels (gsi_bb (gsi_def_stmt));
1361 gsi_insert_before (&gsi, use_stmt, GSI_NEW_STMT);
1363 else if (gimple_code (gsi_stmt (gsi_def_stmt)) == GIMPLE_PHI)
1365 gimple_stmt_iterator bsi
1366 = gsi_start_bb_nondebug (gsi_bb (gsi_def_stmt));
1367 /* Insert right after the PHI statements. */
1368 gsi_insert_before (&bsi, use_stmt, GSI_NEW_STMT);
1370 else
1371 gsi_insert_after (&gsi_def_stmt, use_stmt, GSI_NEW_STMT);
1373 if (dump_file)
1375 fprintf (dump_file, "[codegen] inserting statement: ");
1376 print_gimple_stmt (dump_file, use_stmt, 0, TDF_VOPS | TDF_MEMSYMS);
1377 print_loops_bb (dump_file, gimple_bb (use_stmt), 0, 3);
1382 /* Collect all the operands of NEW_EXPR by recursively visiting each
1383 operand. */
1385 void translate_isl_ast_to_gimple::
1386 collect_all_ssa_names (tree new_expr, vec<tree> *vec_ssa)
1388 if (new_expr == NULL_TREE)
1389 return;
1391 /* Rename all uses in new_expr. */
1392 if (TREE_CODE (new_expr) == SSA_NAME)
1394 vec_ssa->safe_push (new_expr);
1395 return;
1398 /* Iterate over SSA_NAMES in NEW_EXPR. */
1399 for (int i = 0; i < (TREE_CODE_LENGTH (TREE_CODE (new_expr))); i++)
1401 tree op = TREE_OPERAND (new_expr, i);
1402 collect_all_ssa_names (op, vec_ssa);
1406 /* This is abridged version of the function copied from:
1407 tree.c:substitute_in_expr (tree exp, tree f, tree r). */
1409 static tree
1410 substitute_ssa_name (tree exp, tree f, tree r)
1412 enum tree_code code = TREE_CODE (exp);
1413 tree op0, op1, op2, op3;
1414 tree new_tree;
1416 /* We handle TREE_LIST and COMPONENT_REF separately. */
1417 if (code == TREE_LIST)
1419 op0 = substitute_ssa_name (TREE_CHAIN (exp), f, r);
1420 op1 = substitute_ssa_name (TREE_VALUE (exp), f, r);
1421 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
1422 return exp;
1424 return tree_cons (TREE_PURPOSE (exp), op1, op0);
1426 else if (code == COMPONENT_REF)
1428 tree inner;
1430 /* If this expression is getting a value from a PLACEHOLDER_EXPR
1431 and it is the right field, replace it with R. */
1432 for (inner = TREE_OPERAND (exp, 0);
1433 REFERENCE_CLASS_P (inner);
1434 inner = TREE_OPERAND (inner, 0))
1437 /* The field. */
1438 op1 = TREE_OPERAND (exp, 1);
1440 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && op1 == f)
1441 return r;
1443 /* If this expression hasn't been completed let, leave it alone. */
1444 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && !TREE_TYPE (inner))
1445 return exp;
1447 op0 = substitute_ssa_name (TREE_OPERAND (exp, 0), f, r);
1448 if (op0 == TREE_OPERAND (exp, 0))
1449 return exp;
1451 new_tree
1452 = fold_build3 (COMPONENT_REF, TREE_TYPE (exp), op0, op1, NULL_TREE);
1454 else
1455 switch (TREE_CODE_CLASS (code))
1457 case tcc_constant:
1458 return exp;
1460 case tcc_declaration:
1461 if (exp == f)
1462 return r;
1463 else
1464 return exp;
1466 case tcc_expression:
1467 if (exp == f)
1468 return r;
1470 /* Fall through. */
1472 case tcc_exceptional:
1473 case tcc_unary:
1474 case tcc_binary:
1475 case tcc_comparison:
1476 case tcc_reference:
1477 switch (TREE_CODE_LENGTH (code))
1479 case 0:
1480 if (exp == f)
1481 return r;
1482 return exp;
1484 case 1:
1485 op0 = substitute_ssa_name (TREE_OPERAND (exp, 0), f, r);
1486 if (op0 == TREE_OPERAND (exp, 0))
1487 return exp;
1489 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
1490 break;
1492 case 2:
1493 op0 = substitute_ssa_name (TREE_OPERAND (exp, 0), f, r);
1494 op1 = substitute_ssa_name (TREE_OPERAND (exp, 1), f, r);
1496 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
1497 return exp;
1499 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
1500 break;
1502 case 3:
1503 op0 = substitute_ssa_name (TREE_OPERAND (exp, 0), f, r);
1504 op1 = substitute_ssa_name (TREE_OPERAND (exp, 1), f, r);
1505 op2 = substitute_ssa_name (TREE_OPERAND (exp, 2), f, r);
1507 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
1508 && op2 == TREE_OPERAND (exp, 2))
1509 return exp;
1511 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
1512 break;
1514 case 4:
1515 op0 = substitute_ssa_name (TREE_OPERAND (exp, 0), f, r);
1516 op1 = substitute_ssa_name (TREE_OPERAND (exp, 1), f, r);
1517 op2 = substitute_ssa_name (TREE_OPERAND (exp, 2), f, r);
1518 op3 = substitute_ssa_name (TREE_OPERAND (exp, 3), f, r);
1520 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
1521 && op2 == TREE_OPERAND (exp, 2)
1522 && op3 == TREE_OPERAND (exp, 3))
1523 return exp;
1525 new_tree
1526 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
1527 break;
1529 default:
1530 gcc_unreachable ();
1532 break;
1534 case tcc_vl_exp:
1535 default:
1536 gcc_unreachable ();
1539 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
1541 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
1542 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
1544 return new_tree;
1547 /* Rename all the operands of NEW_EXPR by recursively visiting each operand. */
1549 tree translate_isl_ast_to_gimple::
1550 rename_all_uses (tree new_expr, basic_block new_bb, basic_block old_bb)
1552 auto_vec<tree, 2> ssa_names;
1553 collect_all_ssa_names (new_expr, &ssa_names);
1554 tree t;
1555 int i;
1556 FOR_EACH_VEC_ELT (ssa_names, i, t)
1557 if (tree r = get_rename (new_bb, t, old_bb, unknown_phi))
1558 new_expr = substitute_ssa_name (new_expr, t, r);
1560 return new_expr;
1563 /* For ops which are scev_analyzeable, we can regenerate a new name from its
1564 scalar evolution around LOOP. */
1566 tree translate_isl_ast_to_gimple::
1567 get_rename_from_scev (tree old_name, gimple_seq *stmts, loop_p loop,
1568 basic_block new_bb, basic_block old_bb,
1569 vec<tree> iv_map)
1571 tree scev = scalar_evolution_in_region (region->region, loop, old_name);
1573 /* At this point we should know the exact scev for each
1574 scalar SSA_NAME used in the scop: all the other scalar
1575 SSA_NAMEs should have been translated out of SSA using
1576 arrays with one element. */
1577 tree new_expr;
1578 if (chrec_contains_undetermined (scev))
1580 codegen_error = true;
1581 return build_zero_cst (TREE_TYPE (old_name));
1584 new_expr = chrec_apply_map (scev, iv_map);
1586 /* The apply should produce an expression tree containing
1587 the uses of the new induction variables. We should be
1588 able to use new_expr instead of the old_name in the newly
1589 generated loop nest. */
1590 if (chrec_contains_undetermined (new_expr)
1591 || tree_contains_chrecs (new_expr, NULL))
1593 codegen_error = true;
1594 return build_zero_cst (TREE_TYPE (old_name));
1597 if (TREE_CODE (new_expr) == SSA_NAME)
1599 basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (new_expr));
1600 if (bb && !dominated_by_p (CDI_DOMINATORS, new_bb, bb))
1602 codegen_error = true;
1603 return build_zero_cst (TREE_TYPE (old_name));
1607 new_expr = rename_all_uses (new_expr, new_bb, old_bb);
1609 /* We check all the operands and all of them should dominate the use at
1610 new_expr. */
1611 auto_vec <tree, 2> new_ssa_names;
1612 collect_all_ssa_names (new_expr, &new_ssa_names);
1613 int i;
1614 tree new_ssa_name;
1615 FOR_EACH_VEC_ELT (new_ssa_names, i, new_ssa_name)
1617 if (TREE_CODE (new_ssa_name) == SSA_NAME)
1619 basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (new_ssa_name));
1620 if (bb && !dominated_by_p (CDI_DOMINATORS, new_bb, bb))
1622 codegen_error = true;
1623 return build_zero_cst (TREE_TYPE (old_name));
1628 /* Replace the old_name with the new_expr. */
1629 return force_gimple_operand (unshare_expr (new_expr), stmts,
1630 true, NULL_TREE);
1633 /* Renames the scalar uses of the statement COPY, using the
1634 substitution map RENAME_MAP, inserting the gimplification code at
1635 GSI_TGT, for the translation REGION, with the original copied
1636 statement in LOOP, and using the induction variable renaming map
1637 IV_MAP. Returns true when something has been renamed. */
1639 bool translate_isl_ast_to_gimple::
1640 rename_uses (gimple *copy, gimple_stmt_iterator *gsi_tgt, basic_block old_bb,
1641 loop_p loop, vec<tree> iv_map)
1643 bool changed = false;
1645 if (is_gimple_debug (copy))
1647 if (gimple_debug_bind_p (copy))
1648 gimple_debug_bind_reset_value (copy);
1649 else if (gimple_debug_source_bind_p (copy))
1650 return false;
1651 else
1652 gcc_unreachable ();
1654 return false;
1657 if (dump_file)
1659 fprintf (dump_file, "[codegen] renaming uses of stmt: ");
1660 print_gimple_stmt (dump_file, copy, 0);
1663 use_operand_p use_p;
1664 ssa_op_iter op_iter;
1665 FOR_EACH_SSA_USE_OPERAND (use_p, copy, op_iter, SSA_OP_USE)
1667 tree old_name = USE_FROM_PTR (use_p);
1669 if (dump_file)
1671 fprintf (dump_file, "[codegen] renaming old_name = ");
1672 print_generic_expr (dump_file, old_name);
1673 fprintf (dump_file, "\n");
1676 if (TREE_CODE (old_name) != SSA_NAME
1677 || SSA_NAME_IS_DEFAULT_DEF (old_name))
1678 continue;
1680 changed = true;
1681 tree new_expr = get_rename (gsi_tgt->bb, old_name,
1682 old_bb, unknown_phi);
1684 if (new_expr)
1686 tree type_old_name = TREE_TYPE (old_name);
1687 tree type_new_expr = TREE_TYPE (new_expr);
1689 if (dump_file)
1691 fprintf (dump_file, "[codegen] from rename_map: new_name = ");
1692 print_generic_expr (dump_file, new_expr);
1693 fprintf (dump_file, "\n");
1696 if (type_old_name != type_new_expr
1697 || TREE_CODE (new_expr) != SSA_NAME)
1699 tree var = create_tmp_var (type_old_name, "var");
1701 if (!useless_type_conversion_p (type_old_name, type_new_expr))
1702 new_expr = fold_convert (type_old_name, new_expr);
1704 gimple_seq stmts;
1705 new_expr = force_gimple_operand (new_expr, &stmts, true, var);
1706 gsi_insert_earliest (stmts);
1709 replace_exp (use_p, new_expr);
1710 continue;
1713 gimple_seq stmts;
1714 new_expr = get_rename_from_scev (old_name, &stmts, loop, gimple_bb (copy),
1715 old_bb, iv_map);
1716 if (!new_expr || codegen_error_p ())
1717 return false;
1719 if (dump_file)
1721 fprintf (dump_file, "[codegen] not in rename map, scev: ");
1722 print_generic_expr (dump_file, new_expr);
1723 fprintf (dump_file, "\n");
1726 gsi_insert_earliest (stmts);
1727 replace_exp (use_p, new_expr);
1729 if (TREE_CODE (new_expr) == INTEGER_CST
1730 && is_gimple_assign (copy))
1732 tree rhs = gimple_assign_rhs1 (copy);
1734 if (TREE_CODE (rhs) == ADDR_EXPR)
1735 recompute_tree_invariant_for_addr_expr (rhs);
1738 set_rename (old_name, new_expr);
1741 return changed;
1744 /* Returns a basic block that could correspond to where a constant was defined
1745 in the original code. In the original code OLD_BB had the definition, we
1746 need to find which basic block out of the copies of old_bb, in the new
1747 region, should a definition correspond to if it has to reach BB. */
1749 basic_block translate_isl_ast_to_gimple::
1750 get_def_bb_for_const (basic_block bb, basic_block old_bb) const
1752 vec <basic_block> *bbs = region->copied_bb_map->get (old_bb);
1754 if (!bbs || bbs->is_empty ())
1755 return NULL;
1757 if (1 == bbs->length ())
1758 return (*bbs)[0];
1760 int i;
1761 basic_block b1 = NULL, b2;
1762 FOR_EACH_VEC_ELT (*bbs, i, b2)
1764 if (b2 == bb)
1765 return bb;
1767 /* BB and B2 are in two unrelated if-clauses. */
1768 if (!dominated_by_p (CDI_DOMINATORS, bb, b2))
1769 continue;
1771 /* Compute the nearest dominator. */
1772 if (!b1 || dominated_by_p (CDI_DOMINATORS, b2, b1))
1773 b1 = b2;
1776 return b1;
1779 /* Get the new name of OP (from OLD_BB) to be used in NEW_BB. PHI_KIND
1780 determines the kind of phi node. */
1782 tree translate_isl_ast_to_gimple::
1783 get_new_name (basic_block new_bb, tree op,
1784 basic_block old_bb, phi_node_kind phi_kind) const
1786 /* For constants the names are the same. */
1787 if (TREE_CODE (op) != SSA_NAME)
1788 return op;
1790 return get_rename (new_bb, op, old_bb, phi_kind);
1793 /* Return a debug location for OP. */
1795 static location_t
1796 get_loc (tree op)
1798 location_t loc = UNKNOWN_LOCATION;
1800 if (TREE_CODE (op) == SSA_NAME)
1801 loc = gimple_location (SSA_NAME_DEF_STMT (op));
1802 return loc;
1805 /* Returns the incoming edges of basic_block BB in the pair. The first edge is
1806 the init edge (from outside the loop) and the second one is the back edge
1807 from the same loop. */
1809 std::pair<edge, edge>
1810 get_edges (basic_block bb)
1812 std::pair<edge, edge> edges;
1813 edge e;
1814 edge_iterator ei;
1815 FOR_EACH_EDGE (e, ei, bb->preds)
1816 if (bb->loop_father != e->src->loop_father)
1817 edges.first = e;
1818 else
1819 edges.second = e;
1820 return edges;
1823 /* Copy the PHI arguments from OLD_PHI to the NEW_PHI. The arguments to NEW_PHI
1824 must be found unless they can be POSTPONEd for later. */
1826 bool translate_isl_ast_to_gimple::
1827 copy_loop_phi_args (gphi *old_phi, init_back_edge_pair_t &ibp_old_bb,
1828 gphi *new_phi, init_back_edge_pair_t &ibp_new_bb,
1829 bool postpone)
1831 gcc_assert (gimple_phi_num_args (old_phi) == gimple_phi_num_args (new_phi));
1833 basic_block new_bb = gimple_bb (new_phi);
1834 for (unsigned i = 0; i < gimple_phi_num_args (old_phi); i++)
1836 edge e;
1837 if (gimple_phi_arg_edge (old_phi, i) == ibp_old_bb.first)
1838 e = ibp_new_bb.first;
1839 else
1840 e = ibp_new_bb.second;
1842 tree old_name = gimple_phi_arg_def (old_phi, i);
1843 tree new_name = get_new_name (new_bb, old_name,
1844 gimple_bb (old_phi), loop_phi);
1845 if (new_name)
1847 add_phi_arg (new_phi, new_name, e, get_loc (old_name));
1848 continue;
1851 gimple *old_def_stmt = SSA_NAME_DEF_STMT (old_name);
1852 if (!old_def_stmt || gimple_code (old_def_stmt) == GIMPLE_NOP)
1853 /* If the phi arg was a function arg, or wasn't defined, just use the
1854 old name. */
1855 add_phi_arg (new_phi, old_name, e, get_loc (old_name));
1856 else if (postpone)
1858 /* Postpone code gen for later for those back-edges we don't have the
1859 names yet. */
1860 region->incomplete_phis.safe_push (std::make_pair (old_phi, new_phi));
1861 if (dump_file)
1862 fprintf (dump_file, "[codegen] postpone loop phi nodes.\n");
1864 else
1865 /* Either we should add the arg to phi or, we should postpone. */
1866 return false;
1868 return true;
1871 /* Copy loop phi nodes from BB to NEW_BB. */
1873 bool translate_isl_ast_to_gimple::
1874 copy_loop_phi_nodes (basic_block bb, basic_block new_bb)
1876 if (dump_file)
1877 fprintf (dump_file, "[codegen] copying loop phi nodes in bb_%d.\n",
1878 new_bb->index);
1880 /* Loop phi nodes should have only two arguments. */
1881 gcc_assert (2 == EDGE_COUNT (bb->preds));
1883 /* First edge is the init edge and second is the back edge. */
1884 init_back_edge_pair_t ibp_old_bb = get_edges (bb);
1886 /* First edge is the init edge and second is the back edge. */
1887 init_back_edge_pair_t ibp_new_bb = get_edges (new_bb);
1889 for (gphi_iterator psi = gsi_start_phis (bb); !gsi_end_p (psi);
1890 gsi_next (&psi))
1892 gphi *phi = psi.phi ();
1893 tree res = gimple_phi_result (phi);
1894 if (virtual_operand_p (res))
1895 continue;
1896 if (is_gimple_reg (res) && scev_analyzable_p (res, region->region))
1897 continue;
1899 gphi *new_phi = create_phi_node (NULL_TREE, new_bb);
1900 tree new_res = create_new_def_for (res, new_phi,
1901 gimple_phi_result_ptr (new_phi));
1902 set_rename (res, new_res);
1903 codegen_error = !copy_loop_phi_args (phi, ibp_old_bb, new_phi,
1904 ibp_new_bb, true);
1905 update_stmt (new_phi);
1907 if (dump_file)
1909 fprintf (dump_file, "[codegen] creating loop-phi node: ");
1910 print_gimple_stmt (dump_file, new_phi, 0);
1914 return true;
1917 /* Return the init value of PHI, the value coming from outside the loop. */
1919 static tree
1920 get_loop_init_value (gphi *phi)
1923 loop_p loop = gimple_bb (phi)->loop_father;
1925 edge e;
1926 edge_iterator ei;
1927 FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
1928 if (e->src->loop_father != loop)
1929 return gimple_phi_arg_def (phi, e->dest_idx);
1931 return NULL_TREE;
1934 /* Find the init value (the value which comes from outside the loop), of one of
1935 the operands of DEF which is defined by a loop phi. */
1937 static tree
1938 find_init_value (gimple *def)
1940 if (gimple_code (def) == GIMPLE_PHI)
1941 return get_loop_init_value (as_a <gphi*> (def));
1943 if (gimple_vuse (def))
1944 return NULL_TREE;
1946 ssa_op_iter iter;
1947 use_operand_p use_p;
1948 FOR_EACH_SSA_USE_OPERAND (use_p, def, iter, SSA_OP_USE)
1950 tree use = USE_FROM_PTR (use_p);
1951 if (TREE_CODE (use) == SSA_NAME)
1953 if (tree res = find_init_value (SSA_NAME_DEF_STMT (use)))
1954 return res;
1958 return NULL_TREE;
1961 /* Return the init value, the value coming from outside the loop. */
1963 static tree
1964 find_init_value_close_phi (gphi *phi)
1966 gcc_assert (gimple_phi_num_args (phi) == 1);
1967 tree use_arg = gimple_phi_arg_def (phi, 0);
1968 gimple *def = SSA_NAME_DEF_STMT (use_arg);
1969 return find_init_value (def);
1973 tree translate_isl_ast_to_gimple::
1974 add_close_phis_to_outer_loops (tree last_merge_name, edge last_e,
1975 gimple *old_close_phi)
1977 sese_l &codegen_region = region->if_region->true_region->region;
1978 gimple *stmt = SSA_NAME_DEF_STMT (last_merge_name);
1979 basic_block bb = gimple_bb (stmt);
1980 if (!bb_in_sese_p (bb, codegen_region))
1981 return last_merge_name;
1983 loop_p loop = bb->loop_father;
1984 if (!loop_in_sese_p (loop, codegen_region))
1985 return last_merge_name;
1987 edge e = single_exit (loop);
1989 if (dominated_by_p (CDI_DOMINATORS, e->dest, last_e->src))
1990 return last_merge_name;
1992 tree old_name = gimple_phi_arg_def (old_close_phi, 0);
1993 tree old_close_phi_name = gimple_phi_result (old_close_phi);
1995 bb = e->dest;
1996 if (!bb_contains_loop_close_phi_nodes (bb) || !single_succ_p (bb))
1997 bb = split_edge (e);
1999 gphi *close_phi = create_phi_node (NULL_TREE, bb);
2000 tree res = create_new_def_for (last_merge_name, close_phi,
2001 gimple_phi_result_ptr (close_phi));
2002 set_rename (old_close_phi_name, res);
2003 add_phi_arg (close_phi, last_merge_name, e, get_loc (old_name));
2004 last_merge_name = res;
2006 return add_close_phis_to_outer_loops (last_merge_name, last_e, old_close_phi);
2009 /* Add phi nodes to all merge points of all the diamonds enclosing the loop of
2010 the close phi node PHI. */
2012 bool translate_isl_ast_to_gimple::
2013 add_close_phis_to_merge_points (gphi *old_close_phi, gphi *new_close_phi,
2014 tree default_value)
2016 sese_l &codegen_region = region->if_region->true_region->region;
2017 basic_block default_value_bb = get_entry_bb (codegen_region);
2018 if (SSA_NAME == TREE_CODE (default_value))
2020 gimple *stmt = SSA_NAME_DEF_STMT (default_value);
2021 if (!stmt || gimple_code (stmt) == GIMPLE_NOP)
2022 return false;
2023 default_value_bb = gimple_bb (stmt);
2026 basic_block new_close_phi_bb = gimple_bb (new_close_phi);
2028 tree old_close_phi_name = gimple_phi_result (old_close_phi);
2029 tree new_close_phi_name = gimple_phi_result (new_close_phi);
2030 tree last_merge_name = new_close_phi_name;
2031 tree old_name = gimple_phi_arg_def (old_close_phi, 0);
2033 int i;
2034 edge merge_e;
2035 FOR_EACH_VEC_ELT_REVERSE (merge_points, i, merge_e)
2037 basic_block new_merge_bb = merge_e->src;
2038 if (!dominated_by_p (CDI_DOMINATORS, new_merge_bb, default_value_bb))
2039 continue;
2041 last_merge_name = add_close_phis_to_outer_loops (last_merge_name, merge_e,
2042 old_close_phi);
2044 gphi *merge_phi = create_phi_node (NULL_TREE, new_merge_bb);
2045 tree merge_res = create_new_def_for (old_close_phi_name, merge_phi,
2046 gimple_phi_result_ptr (merge_phi));
2047 set_rename (old_close_phi_name, merge_res);
2049 edge from_loop = NULL, from_default_value = NULL;
2050 edge e;
2051 edge_iterator ei;
2052 FOR_EACH_EDGE (e, ei, new_merge_bb->preds)
2053 if (dominated_by_p (CDI_DOMINATORS, e->src, new_close_phi_bb))
2054 from_loop = e;
2055 else
2056 from_default_value = e;
2058 /* Because CDI_POST_DOMINATORS are not updated, we only rely on
2059 CDI_DOMINATORS, which may not handle all cases where new_close_phi_bb
2060 is contained in another condition. */
2061 if (!from_default_value || !from_loop)
2062 return false;
2064 add_phi_arg (merge_phi, last_merge_name, from_loop, get_loc (old_name));
2065 add_phi_arg (merge_phi, default_value, from_default_value, get_loc (old_name));
2067 if (dump_file)
2069 fprintf (dump_file, "[codegen] Adding guard-phi: ");
2070 print_gimple_stmt (dump_file, merge_phi, 0);
2073 update_stmt (merge_phi);
2074 last_merge_name = merge_res;
2077 return true;
2080 /* Copy all the loop-close phi args from BB to NEW_BB. */
2082 bool translate_isl_ast_to_gimple::
2083 copy_loop_close_phi_args (basic_block old_bb, basic_block new_bb,
2084 vec<tree> iv_map, bool postpone)
2086 for (gphi_iterator psi = gsi_start_phis (old_bb); !gsi_end_p (psi);
2087 gsi_next (&psi))
2089 gphi *old_close_phi = psi.phi ();
2090 tree res = gimple_phi_result (old_close_phi);
2091 if (virtual_operand_p (res))
2092 continue;
2094 gphi *new_close_phi = create_phi_node (NULL_TREE, new_bb);
2095 tree new_res = create_new_def_for (res, new_close_phi,
2096 gimple_phi_result_ptr (new_close_phi));
2097 set_rename (res, new_res);
2099 tree old_name = gimple_phi_arg_def (old_close_phi, 0);
2100 tree new_name;
2101 if (is_gimple_reg (res) && scev_analyzable_p (res, region->region))
2103 gimple_seq stmts;
2104 new_name = get_rename_from_scev (old_name, &stmts,
2105 old_bb->loop_father,
2106 new_bb, old_bb, iv_map);
2107 if (! codegen_error_p ())
2108 gsi_insert_earliest (stmts);
2110 else
2111 new_name = get_new_name (new_bb, old_name, old_bb, close_phi);
2113 /* Predecessor basic blocks of a loop close phi should have been code
2114 generated before. FIXME: This is fixable by merging PHIs from inner
2115 loops as well. See: gfortran.dg/graphite/interchange-3.f90. */
2116 if (!new_name || codegen_error_p ())
2117 return false;
2119 add_phi_arg (new_close_phi, new_name, single_pred_edge (new_bb),
2120 get_loc (old_name));
2121 if (dump_file)
2123 fprintf (dump_file, "[codegen] Adding loop close phi: ");
2124 print_gimple_stmt (dump_file, new_close_phi, 0);
2127 update_stmt (new_close_phi);
2129 /* When there is no loop guard around this codegenerated loop, there is no
2130 need to collect the close-phi arg. */
2131 if (merge_points.is_empty ())
2132 continue;
2134 /* Add a PHI in the succ_new_bb for each close phi of the loop. */
2135 tree default_value = find_init_value_close_phi (new_close_phi);
2137 /* A close phi must come from a loop-phi having a default value. */
2138 if (!default_value)
2140 if (!postpone)
2141 return false;
2143 region->incomplete_phis.safe_push (std::make_pair (old_close_phi,
2144 new_close_phi));
2145 if (dump_file)
2147 fprintf (dump_file, "[codegen] postpone close phi nodes: ");
2148 print_gimple_stmt (dump_file, new_close_phi, 0);
2150 continue;
2153 if (!add_close_phis_to_merge_points (old_close_phi, new_close_phi,
2154 default_value))
2155 return false;
2158 return true;
2161 /* Copy loop close phi nodes from BB to NEW_BB. */
2163 bool translate_isl_ast_to_gimple::
2164 copy_loop_close_phi_nodes (basic_block old_bb, basic_block new_bb,
2165 vec<tree> iv_map)
2167 if (dump_file)
2168 fprintf (dump_file, "[codegen] copying loop close phi nodes in bb_%d.\n",
2169 new_bb->index);
2170 /* Loop close phi nodes should have only one argument. */
2171 gcc_assert (1 == EDGE_COUNT (old_bb->preds));
2173 return copy_loop_close_phi_args (old_bb, new_bb, iv_map, true);
2177 /* Add NEW_NAME as the ARGNUM-th arg of NEW_PHI which is in NEW_BB.
2178 DOMINATING_PRED is the predecessor basic block of OLD_BB which dominates the
2179 other pred of OLD_BB as well. If no such basic block exists then it is NULL.
2180 NON_DOMINATING_PRED is a pred which does not dominate OLD_BB, it cannot be
2181 NULL.
2183 Case1: OLD_BB->preds {BB1, BB2} and BB1 does not dominate BB2 and vice versa.
2184 In this case DOMINATING_PRED = NULL.
2186 Case2: OLD_BB->preds {BB1, BB2} and BB1 dominates BB2.
2188 Returns true on successful copy of the args, false otherwise. */
2190 bool translate_isl_ast_to_gimple::
2191 add_phi_arg_for_new_expr (tree old_phi_args[2], tree new_phi_args[2],
2192 edge old_bb_dominating_edge,
2193 edge old_bb_non_dominating_edge,
2194 gphi *phi, gphi *new_phi,
2195 basic_block new_bb)
2197 basic_block def_pred[2] = { NULL, NULL };
2198 int not_found_bb_index = -1;
2199 for (int i = 0; i < 2; i++)
2201 /* If the corresponding def_bb could not be found the entry will be
2202 NULL. */
2203 if (TREE_CODE (old_phi_args[i]) == INTEGER_CST)
2204 def_pred[i] = get_def_bb_for_const (new_bb,
2205 gimple_phi_arg_edge (phi, i)->src);
2206 else if (new_phi_args[i] && (TREE_CODE (new_phi_args[i]) == SSA_NAME))
2207 def_pred[i] = gimple_bb (SSA_NAME_DEF_STMT (new_phi_args[i]));
2209 if (!def_pred[i])
2211 /* When non are available bail out. */
2212 if (not_found_bb_index != -1)
2213 return false;
2214 not_found_bb_index = i;
2218 /* Here we are pattern matching on the structure of CFG w.r.t. old one. */
2219 if (old_bb_dominating_edge)
2221 if (not_found_bb_index != -1)
2222 return false;
2224 basic_block new_pred1 = (*new_bb->preds)[0]->src;
2225 basic_block new_pred2 = (*new_bb->preds)[1]->src;
2226 vec <basic_block> *bbs
2227 = region->copied_bb_map->get (old_bb_non_dominating_edge->src);
2229 /* Could not find a mapping. */
2230 if (!bbs)
2231 return false;
2233 basic_block new_pred = NULL;
2234 basic_block b;
2235 int i;
2236 FOR_EACH_VEC_ELT (*bbs, i, b)
2238 if (dominated_by_p (CDI_DOMINATORS, new_pred1, b))
2240 /* FIXME: If we have already found new_pred then we have to
2241 disambiguate, bail out for now. */
2242 if (new_pred)
2243 return false;
2244 new_pred = new_pred1;
2246 if (dominated_by_p (CDI_DOMINATORS, new_pred2, b))
2248 /* FIXME: If we have already found new_pred then we have to either
2249 it dominates both or we have to disambiguate, bail out. */
2250 if (new_pred)
2251 return false;
2252 new_pred = new_pred2;
2256 if (!new_pred)
2257 return false;
2259 edge new_non_dominating_edge = find_edge (new_pred, new_bb);
2260 gcc_assert (new_non_dominating_edge);
2261 /* FIXME: Validate each args just like in loop-phis. */
2262 /* By the process of elimination we first insert insert phi-edge for
2263 non-dominating pred which is computed above and then we insert the
2264 remaining one. */
2265 int inserted_edge = 0;
2266 for (; inserted_edge < 2; inserted_edge++)
2268 edge new_bb_pred_edge = gimple_phi_arg_edge (new_phi, inserted_edge);
2269 if (new_non_dominating_edge == new_bb_pred_edge)
2271 add_phi_arg (new_phi, new_phi_args[inserted_edge],
2272 new_non_dominating_edge,
2273 get_loc (old_phi_args[inserted_edge]));
2274 break;
2277 if (inserted_edge == 2)
2278 return false;
2280 int edge_dominating = inserted_edge == 0 ? 1 : 0;
2282 edge new_dominating_edge = NULL;
2283 for (inserted_edge = 0; inserted_edge < 2; inserted_edge++)
2285 edge e = gimple_phi_arg_edge (new_phi, inserted_edge);
2286 if (e != new_non_dominating_edge)
2288 new_dominating_edge = e;
2289 add_phi_arg (new_phi, new_phi_args[edge_dominating],
2290 new_dominating_edge,
2291 get_loc (old_phi_args[inserted_edge]));
2292 break;
2295 gcc_assert (new_dominating_edge);
2297 else
2299 /* Classic diamond structure: both edges are non-dominating. We need to
2300 find one unique edge then the other can be found be elimination. If
2301 any definition (def_pred) dominates both the preds of new_bb then we
2302 bail out. Entries of def_pred maybe NULL, in that case we must
2303 uniquely find pred with help of only one entry. */
2304 edge new_e[2] = { NULL, NULL };
2305 for (int i = 0; i < 2; i++)
2307 edge e;
2308 edge_iterator ei;
2309 FOR_EACH_EDGE (e, ei, new_bb->preds)
2310 if (def_pred[i]
2311 && dominated_by_p (CDI_DOMINATORS, e->src, def_pred[i]))
2313 if (new_e[i])
2314 /* We do not know how to handle the case when def_pred
2315 dominates more than a predecessor. */
2316 return false;
2317 new_e[i] = e;
2321 gcc_assert (new_e[0] || new_e[1]);
2323 /* Find the other edge by process of elimination. */
2324 if (not_found_bb_index != -1)
2326 gcc_assert (!new_e[not_found_bb_index]);
2327 int found_bb_index = not_found_bb_index == 1 ? 0 : 1;
2328 edge e;
2329 edge_iterator ei;
2330 FOR_EACH_EDGE (e, ei, new_bb->preds)
2332 if (new_e[found_bb_index] == e)
2333 continue;
2334 new_e[not_found_bb_index] = e;
2338 /* Add edges to phi args. */
2339 for (int i = 0; i < 2; i++)
2340 add_phi_arg (new_phi, new_phi_args[i], new_e[i],
2341 get_loc (old_phi_args[i]));
2344 return true;
2347 /* Copy the arguments of cond-phi node PHI, to NEW_PHI in the codegenerated
2348 region. If postpone is true and it isn't possible to copy any arg of PHI,
2349 the PHI is added to the REGION->INCOMPLETE_PHIS to be codegenerated later.
2350 Returns false if the copying was unsuccessful. */
2352 bool translate_isl_ast_to_gimple::
2353 copy_cond_phi_args (gphi *phi, gphi *new_phi, vec<tree> iv_map, bool postpone)
2355 if (dump_file)
2356 fprintf (dump_file, "[codegen] copying cond phi args.\n");
2357 gcc_assert (2 == gimple_phi_num_args (phi));
2359 basic_block new_bb = gimple_bb (new_phi);
2360 loop_p loop = gimple_bb (phi)->loop_father;
2362 basic_block old_bb = gimple_bb (phi);
2363 edge old_bb_non_dominating_edge = NULL, old_bb_dominating_edge = NULL;
2365 edge e;
2366 edge_iterator ei;
2367 FOR_EACH_EDGE (e, ei, old_bb->preds)
2368 if (!dominated_by_p (CDI_DOMINATORS, old_bb, e->src))
2369 old_bb_non_dominating_edge = e;
2370 else
2371 old_bb_dominating_edge = e;
2373 gcc_assert (!dominated_by_p (CDI_DOMINATORS, old_bb,
2374 old_bb_non_dominating_edge->src));
2376 tree new_phi_args[2];
2377 tree old_phi_args[2];
2379 for (unsigned i = 0; i < gimple_phi_num_args (phi); i++)
2381 tree old_name = gimple_phi_arg_def (phi, i);
2382 tree new_name = get_new_name (new_bb, old_name, old_bb, cond_phi);
2383 old_phi_args[i] = old_name;
2384 if (new_name)
2386 new_phi_args [i] = new_name;
2387 continue;
2390 /* If the phi-arg was a parameter. */
2391 if (vec_find (region->params, old_name) != -1)
2393 new_phi_args [i] = old_name;
2394 if (dump_file)
2396 fprintf (dump_file,
2397 "[codegen] parameter argument to phi, new_expr: ");
2398 print_generic_expr (dump_file, new_phi_args[i]);
2399 fprintf (dump_file, "\n");
2401 continue;
2404 gimple *old_def_stmt = SSA_NAME_DEF_STMT (old_name);
2405 if (!old_def_stmt || gimple_code (old_def_stmt) == GIMPLE_NOP)
2406 /* FIXME: If the phi arg was a function arg, or wasn't defined, just use
2407 the old name. */
2408 return false;
2410 if (postpone)
2412 /* If the phi-arg is scev-analyzeable but only in the first stage. */
2413 if (is_gimple_reg (old_name)
2414 && scev_analyzable_p (old_name, region->region))
2416 gimple_seq stmts;
2417 tree new_expr = get_rename_from_scev (old_name, &stmts, loop,
2418 new_bb, old_bb, iv_map);
2419 if (codegen_error_p ())
2420 return false;
2422 gcc_assert (new_expr);
2423 if (dump_file)
2425 fprintf (dump_file,
2426 "[codegen] scev analyzeable, new_expr: ");
2427 print_generic_expr (dump_file, new_expr);
2428 fprintf (dump_file, "\n");
2430 gsi_insert_earliest (stmts);
2431 new_phi_args[i] = new_expr;
2432 continue;
2435 /* Postpone code gen for later for back-edges. */
2436 region->incomplete_phis.safe_push (std::make_pair (phi, new_phi));
2438 if (dump_file)
2440 fprintf (dump_file, "[codegen] postpone cond phi nodes: ");
2441 print_gimple_stmt (dump_file, new_phi, 0);
2444 new_phi_args [i] = NULL_TREE;
2445 continue;
2447 else
2448 /* Either we should add the arg to phi or, we should postpone. */
2449 return false;
2452 /* If none of the args have been determined in the first stage then wait until
2453 later. */
2454 if (postpone && !new_phi_args[0] && !new_phi_args[1])
2455 return true;
2457 return add_phi_arg_for_new_expr (old_phi_args, new_phi_args,
2458 old_bb_dominating_edge,
2459 old_bb_non_dominating_edge,
2460 phi, new_phi, new_bb);
2463 /* Copy cond phi nodes from BB to NEW_BB. A cond-phi node is a basic block
2464 containing phi nodes coming from two predecessors, and none of them are back
2465 edges. */
2467 bool translate_isl_ast_to_gimple::
2468 copy_cond_phi_nodes (basic_block bb, basic_block new_bb, vec<tree> iv_map)
2471 gcc_assert (!bb_contains_loop_close_phi_nodes (bb));
2473 /* TODO: Handle cond phi nodes with more than 2 predecessors. */
2474 if (EDGE_COUNT (bb->preds) != 2)
2475 return false;
2477 if (dump_file)
2478 fprintf (dump_file, "[codegen] copying cond phi nodes in bb_%d.\n",
2479 new_bb->index);
2481 for (gphi_iterator psi = gsi_start_phis (bb); !gsi_end_p (psi);
2482 gsi_next (&psi))
2484 gphi *phi = psi.phi ();
2485 tree res = gimple_phi_result (phi);
2486 if (virtual_operand_p (res))
2487 continue;
2489 gphi *new_phi = create_phi_node (NULL_TREE, new_bb);
2490 tree new_res = create_new_def_for (res, new_phi,
2491 gimple_phi_result_ptr (new_phi));
2492 set_rename (res, new_res);
2494 if (!copy_cond_phi_args (phi, new_phi, iv_map, true))
2495 return false;
2497 update_stmt (new_phi);
2500 return true;
2503 /* Return true if STMT should be copied from region to the new code-generated
2504 region. LABELs, CONDITIONS, induction-variables and region parameters need
2505 not be copied. */
2507 static bool
2508 should_copy_to_new_region (gimple *stmt, sese_info_p region)
2510 /* Do not copy labels or conditions. */
2511 if (gimple_code (stmt) == GIMPLE_LABEL
2512 || gimple_code (stmt) == GIMPLE_COND)
2513 return false;
2515 tree lhs;
2516 /* Do not copy induction variables. */
2517 if (is_gimple_assign (stmt)
2518 && (lhs = gimple_assign_lhs (stmt))
2519 && TREE_CODE (lhs) == SSA_NAME
2520 && is_gimple_reg (lhs)
2521 && scev_analyzable_p (lhs, region->region))
2522 return false;
2524 /* Do not copy parameters that have been generated in the header of the
2525 scop. */
2526 if (is_gimple_assign (stmt)
2527 && (lhs = gimple_assign_lhs (stmt))
2528 && TREE_CODE (lhs) == SSA_NAME
2529 && region->parameter_rename_map->get(lhs))
2530 return false;
2532 return true;
2535 /* Create new names for all the definitions created by COPY and add replacement
2536 mappings for each new name. */
2538 void translate_isl_ast_to_gimple::
2539 set_rename_for_each_def (gimple *stmt)
2541 def_operand_p def_p;
2542 ssa_op_iter op_iter;
2543 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, op_iter, SSA_OP_ALL_DEFS)
2545 tree old_name = DEF_FROM_PTR (def_p);
2546 tree new_name = create_new_def_for (old_name, stmt, def_p);
2547 set_rename (old_name, new_name);
2551 /* Duplicates the statements of basic block BB into basic block NEW_BB
2552 and compute the new induction variables according to the IV_MAP. */
2554 bool translate_isl_ast_to_gimple::
2555 graphite_copy_stmts_from_block (basic_block bb, basic_block new_bb,
2556 vec<tree> iv_map)
2558 /* Iterator poining to the place where new statement (s) will be inserted. */
2559 gimple_stmt_iterator gsi_tgt = gsi_last_bb (new_bb);
2561 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
2562 gsi_next (&gsi))
2564 gimple *stmt = gsi_stmt (gsi);
2565 if (!should_copy_to_new_region (stmt, region))
2566 continue;
2568 /* Create a new copy of STMT and duplicate STMT's virtual
2569 operands. */
2570 gimple *copy = gimple_copy (stmt);
2571 gsi_insert_after (&gsi_tgt, copy, GSI_NEW_STMT);
2573 if (dump_file)
2575 fprintf (dump_file, "[codegen] inserting statement: ");
2576 print_gimple_stmt (dump_file, copy, 0);
2579 maybe_duplicate_eh_stmt (copy, stmt);
2580 gimple_duplicate_stmt_histograms (cfun, copy, cfun, stmt);
2582 /* Crete new names for each def in the copied stmt. */
2583 set_rename_for_each_def (copy);
2585 loop_p loop = bb->loop_father;
2586 if (rename_uses (copy, &gsi_tgt, bb, loop, iv_map))
2588 fold_stmt_inplace (&gsi_tgt);
2589 gcc_assert (gsi_stmt (gsi_tgt) == copy);
2592 if (codegen_error_p ())
2593 return false;
2595 /* For each SSA_NAME in the parameter_rename_map rename their usage. */
2596 ssa_op_iter iter;
2597 use_operand_p use_p;
2598 if (!is_gimple_debug (copy))
2599 FOR_EACH_SSA_USE_OPERAND (use_p, copy, iter, SSA_OP_USE)
2601 tree old_name = USE_FROM_PTR (use_p);
2603 if (TREE_CODE (old_name) != SSA_NAME
2604 || SSA_NAME_IS_DEFAULT_DEF (old_name))
2605 continue;
2607 tree *new_expr = region->parameter_rename_map->get (old_name);
2608 if (!new_expr)
2609 continue;
2611 replace_exp (use_p, *new_expr);
2614 update_stmt (copy);
2617 return true;
2621 /* Given a basic block containing close-phi it returns the new basic block where
2622 to insert a copy of the close-phi nodes. All the uses in close phis should
2623 come from a single loop otherwise it returns NULL. */
2625 edge translate_isl_ast_to_gimple::
2626 edge_for_new_close_phis (basic_block bb)
2628 /* Make sure that NEW_BB is the new_loop->exit->dest. We find the definition
2629 of close phi in the original code and then find the mapping of basic block
2630 defining that variable. If there are multiple close-phis and they are
2631 defined in different loops (in the original or in the new code) because of
2632 loop splitting, then we bail out. */
2633 loop_p new_loop = NULL;
2634 for (gphi_iterator psi = gsi_start_phis (bb); !gsi_end_p (psi);
2635 gsi_next (&psi))
2637 gphi *phi = psi.phi ();
2638 tree name = gimple_phi_arg_def (phi, 0);
2639 basic_block old_loop_bb = gimple_bb (SSA_NAME_DEF_STMT (name));
2641 vec <basic_block> *bbs = region->copied_bb_map->get (old_loop_bb);
2642 if (!bbs || bbs->length () != 1)
2643 /* This is one of the places which shows preserving original structure
2644 is not always possible, as we may need to insert close PHI for a loop
2645 where the latch does not have any mapping, or the mapping is
2646 ambiguous. */
2647 return NULL;
2649 if (!new_loop)
2650 new_loop = (*bbs)[0]->loop_father;
2651 else if (new_loop != (*bbs)[0]->loop_father)
2652 return NULL;
2655 if (!new_loop)
2656 return NULL;
2658 return single_exit (new_loop);
2661 /* Copies BB and includes in the copied BB all the statements that can
2662 be reached following the use-def chains from the memory accesses,
2663 and returns the next edge following this new block. */
2665 edge translate_isl_ast_to_gimple::
2666 copy_bb_and_scalar_dependences (basic_block bb, edge next_e, vec<tree> iv_map)
2668 int num_phis = number_of_phi_nodes (bb);
2670 if (region->copied_bb_map->get (bb))
2672 /* FIXME: we should be able to handle phi nodes with args coming from
2673 outside the region. */
2674 if (num_phis)
2676 codegen_error = true;
2677 return NULL;
2681 basic_block new_bb = NULL;
2682 if (bb_contains_loop_close_phi_nodes (bb))
2684 if (dump_file)
2685 fprintf (dump_file, "[codegen] bb_%d contains close phi nodes.\n",
2686 bb->index);
2688 edge e = edge_for_new_close_phis (bb);
2689 if (!e)
2691 codegen_error = true;
2692 return NULL;
2695 basic_block phi_bb = e->dest;
2697 if (!bb_contains_loop_close_phi_nodes (phi_bb) || !single_succ_p (phi_bb))
2698 phi_bb = split_edge (e);
2700 gcc_assert (single_pred_edge (phi_bb)->src->loop_father
2701 != single_pred_edge (phi_bb)->dest->loop_father);
2703 if (!copy_loop_close_phi_nodes (bb, phi_bb, iv_map))
2705 codegen_error = true;
2706 return NULL;
2709 if (e == next_e)
2710 new_bb = phi_bb;
2711 else
2712 new_bb = split_edge (next_e);
2714 else
2716 new_bb = split_edge (next_e);
2717 if (num_phis > 0 && bb_contains_loop_phi_nodes (bb))
2719 basic_block phi_bb = next_e->dest->loop_father->header;
2721 /* At this point we are unable to codegenerate by still preserving the SSA
2722 structure because maybe the loop is completely unrolled and the PHIs
2723 and cross-bb scalar dependencies are untrackable w.r.t. the original
2724 code. See gfortran.dg/graphite/pr29832.f90. */
2725 if (EDGE_COUNT (bb->preds) != EDGE_COUNT (phi_bb->preds))
2727 codegen_error = true;
2728 return NULL;
2731 /* In case isl did some loop peeling, like this:
2733 S_8(0);
2734 for (int c1 = 1; c1 <= 5; c1 += 1) {
2735 S_8(c1);
2737 S_8(6);
2739 there should be no loop-phi nodes in S_8(0).
2741 FIXME: We need to reason about dynamic instances of S_8, i.e., the
2742 values of all scalar variables: for the moment we instantiate only
2743 SCEV analyzable expressions on the iteration domain, and we need to
2744 extend that to reductions that cannot be analyzed by SCEV. */
2745 if (!bb_in_sese_p (phi_bb, region->if_region->true_region->region))
2747 codegen_error = true;
2748 return NULL;
2751 if (dump_file)
2752 fprintf (dump_file, "[codegen] bb_%d contains loop phi nodes.\n",
2753 bb->index);
2754 if (!copy_loop_phi_nodes (bb, phi_bb))
2756 codegen_error = true;
2757 return NULL;
2760 else if (num_phis > 0)
2762 if (dump_file)
2763 fprintf (dump_file, "[codegen] bb_%d contains cond phi nodes.\n",
2764 bb->index);
2766 basic_block phi_bb = single_pred (new_bb);
2767 loop_p loop_father = new_bb->loop_father;
2769 /* Move back until we find the block with two predecessors. */
2770 while (single_pred_p (phi_bb))
2771 phi_bb = single_pred_edge (phi_bb)->src;
2773 /* If a corresponding merge-point was not found, then abort codegen. */
2774 if (phi_bb->loop_father != loop_father
2775 || !bb_in_sese_p (phi_bb, region->if_region->true_region->region)
2776 || !copy_cond_phi_nodes (bb, phi_bb, iv_map))
2778 codegen_error = true;
2779 return NULL;
2784 if (dump_file)
2785 fprintf (dump_file, "[codegen] copying from bb_%d to bb_%d.\n",
2786 bb->index, new_bb->index);
2788 vec <basic_block> *copied_bbs = region->copied_bb_map->get (bb);
2789 if (copied_bbs)
2790 copied_bbs->safe_push (new_bb);
2791 else
2793 vec<basic_block> bbs;
2794 bbs.create (2);
2795 bbs.safe_push (new_bb);
2796 region->copied_bb_map->put (bb, bbs);
2799 if (!graphite_copy_stmts_from_block (bb, new_bb, iv_map))
2801 codegen_error = true;
2802 return NULL;
2805 return single_succ_edge (new_bb);
2808 /* Patch the missing arguments of the phi nodes. */
2810 void translate_isl_ast_to_gimple::
2811 translate_pending_phi_nodes ()
2813 int i;
2814 phi_rename *rename;
2815 FOR_EACH_VEC_ELT (region->incomplete_phis, i, rename)
2817 gphi *old_phi = rename->first;
2818 gphi *new_phi = rename->second;
2819 basic_block old_bb = gimple_bb (old_phi);
2820 basic_block new_bb = gimple_bb (new_phi);
2822 /* First edge is the init edge and second is the back edge. */
2823 init_back_edge_pair_t ibp_old_bb = get_edges (old_bb);
2824 init_back_edge_pair_t ibp_new_bb = get_edges (new_bb);
2826 if (dump_file)
2828 fprintf (dump_file, "[codegen] translating pending old-phi: ");
2829 print_gimple_stmt (dump_file, old_phi, 0);
2832 auto_vec <tree, 1> iv_map;
2833 if (bb_contains_loop_phi_nodes (new_bb))
2834 codegen_error = !copy_loop_phi_args (old_phi, ibp_old_bb, new_phi,
2835 ibp_new_bb, false);
2836 else if (bb_contains_loop_close_phi_nodes (new_bb))
2837 codegen_error = !copy_loop_close_phi_args (old_bb, new_bb, iv_map, false);
2838 else
2839 codegen_error = !copy_cond_phi_args (old_phi, new_phi, iv_map, false);
2841 if (dump_file)
2843 fprintf (dump_file, "[codegen] to new-phi: ");
2844 print_gimple_stmt (dump_file, new_phi, 0);
2846 if (codegen_error_p ())
2847 return;
2851 /* Add isl's parameter identifiers and corresponding trees to ivs_params. */
2853 void translate_isl_ast_to_gimple::
2854 add_parameters_to_ivs_params (scop_p scop, ivs_params &ip)
2856 sese_info_p region = scop->scop_info;
2857 unsigned nb_parameters = isl_set_dim (scop->param_context, isl_dim_param);
2858 gcc_assert (nb_parameters == region->params.length ());
2859 unsigned i;
2860 for (i = 0; i < nb_parameters; i++)
2862 isl_id *tmp_id = isl_set_get_dim_id (scop->param_context,
2863 isl_dim_param, i);
2864 ip[tmp_id] = region->params[i];
2869 /* Generates a build, which specifies the constraints on the parameters. */
2871 __isl_give isl_ast_build *translate_isl_ast_to_gimple::
2872 generate_isl_context (scop_p scop)
2874 isl_set *context_isl = isl_set_params (isl_set_copy (scop->param_context));
2875 return isl_ast_build_from_context (context_isl);
2878 /* This method is executed before the construction of a for node. */
2879 __isl_give isl_id *
2880 ast_build_before_for (__isl_keep isl_ast_build *build, void *user)
2882 isl_union_map *dependences = (isl_union_map *) user;
2883 ast_build_info *for_info = XNEW (struct ast_build_info);
2884 isl_union_map *schedule = isl_ast_build_get_schedule (build);
2885 isl_space *schedule_space = isl_ast_build_get_schedule_space (build);
2886 int dimension = isl_space_dim (schedule_space, isl_dim_out);
2887 for_info->is_parallelizable =
2888 !carries_deps (schedule, dependences, dimension);
2889 isl_union_map_free (schedule);
2890 isl_space_free (schedule_space);
2891 isl_id *id = isl_id_alloc (isl_ast_build_get_ctx (build), "", for_info);
2892 return id;
2895 /* Generate isl AST from schedule of SCOP. */
2897 __isl_give isl_ast_node *translate_isl_ast_to_gimple::
2898 scop_to_isl_ast (scop_p scop)
2900 gcc_assert (scop->transformed_schedule);
2902 /* Set the separate option to reduce control flow overhead. */
2903 isl_schedule *schedule = isl_schedule_map_schedule_node_bottom_up
2904 (isl_schedule_copy (scop->transformed_schedule), set_separate_option, NULL);
2905 isl_ast_build *context_isl = generate_isl_context (scop);
2907 if (flag_loop_parallelize_all)
2909 scop_get_dependences (scop);
2910 context_isl =
2911 isl_ast_build_set_before_each_for (context_isl, ast_build_before_for,
2912 scop->dependence);
2915 isl_ast_node *ast_isl = isl_ast_build_node_from_schedule
2916 (context_isl, schedule);
2917 isl_ast_build_free (context_isl);
2918 return ast_isl;
2921 /* Copy def from sese REGION to the newly created TO_REGION. TR is defined by
2922 DEF_STMT. GSI points to entry basic block of the TO_REGION. */
2924 static void
2925 copy_def (tree tr, gimple *def_stmt, sese_info_p region, sese_info_p to_region,
2926 gimple_stmt_iterator *gsi)
2928 if (!defined_in_sese_p (tr, region->region))
2929 return;
2931 ssa_op_iter iter;
2932 use_operand_p use_p;
2933 FOR_EACH_SSA_USE_OPERAND (use_p, def_stmt, iter, SSA_OP_USE)
2935 tree use_tr = USE_FROM_PTR (use_p);
2937 /* Do not copy parameters that have been generated in the header of the
2938 scop. */
2939 if (region->parameter_rename_map->get(use_tr))
2940 continue;
2942 gimple *def_of_use = SSA_NAME_DEF_STMT (use_tr);
2943 if (!def_of_use)
2944 continue;
2946 copy_def (use_tr, def_of_use, region, to_region, gsi);
2949 gimple *copy = gimple_copy (def_stmt);
2950 gsi_insert_after (gsi, copy, GSI_NEW_STMT);
2952 /* Create new names for all the definitions created by COPY and
2953 add replacement mappings for each new name. */
2954 def_operand_p def_p;
2955 ssa_op_iter op_iter;
2956 FOR_EACH_SSA_DEF_OPERAND (def_p, copy, op_iter, SSA_OP_ALL_DEFS)
2958 tree old_name = DEF_FROM_PTR (def_p);
2959 tree new_name = create_new_def_for (old_name, copy, def_p);
2960 region->parameter_rename_map->put(old_name, new_name);
2963 update_stmt (copy);
2966 static void
2967 copy_internal_parameters (sese_info_p region, sese_info_p to_region)
2969 /* For all the parameters which definitino is in the if_region->false_region,
2970 insert code on true_region (if_region->true_region->entry). */
2972 int i;
2973 tree tr;
2974 gimple_stmt_iterator gsi = gsi_start_bb(to_region->region.entry->dest);
2976 FOR_EACH_VEC_ELT (region->params, i, tr)
2978 // If def is not in region.
2979 gimple *def_stmt = SSA_NAME_DEF_STMT (tr);
2980 if (def_stmt)
2981 copy_def (tr, def_stmt, region, to_region, &gsi);
2985 /* GIMPLE Loop Generator: generates loops in GIMPLE form for the given SCOP.
2986 Return true if code generation succeeded. */
2988 bool
2989 graphite_regenerate_ast_isl (scop_p scop)
2991 sese_info_p region = scop->scop_info;
2992 translate_isl_ast_to_gimple t (region);
2994 ifsese if_region = NULL;
2995 isl_ast_node *root_node;
2996 ivs_params ip;
2998 timevar_push (TV_GRAPHITE_CODE_GEN);
2999 t.add_parameters_to_ivs_params (scop, ip);
3000 root_node = t.scop_to_isl_ast (scop);
3002 if (dump_file && (dump_flags & TDF_DETAILS))
3004 fprintf (dump_file, "[scheduler] original schedule:\n");
3005 print_isl_schedule (dump_file, scop->original_schedule);
3006 fprintf (dump_file, "[scheduler] isl transformed schedule:\n");
3007 print_isl_schedule (dump_file, scop->transformed_schedule);
3009 fprintf (dump_file, "[scheduler] original ast:\n");
3010 print_schedule_ast (dump_file, scop->original_schedule, scop);
3011 fprintf (dump_file, "[scheduler] AST generated by isl:\n");
3012 print_isl_ast (dump_file, root_node);
3015 recompute_all_dominators ();
3016 graphite_verify ();
3018 if_region = move_sese_in_condition (region);
3019 region->if_region = if_region;
3020 recompute_all_dominators ();
3022 loop_p context_loop = region->region.entry->src->loop_father;
3024 /* Copy all the parameters which are defined in the region. */
3025 copy_internal_parameters(if_region->false_region, if_region->true_region);
3027 edge e = single_succ_edge (if_region->true_region->region.entry->dest);
3028 basic_block bb = split_edge (e);
3030 /* Update the true_region exit edge. */
3031 region->if_region->true_region->region.exit = single_succ_edge (bb);
3033 t.translate_isl_ast (context_loop, root_node, e, ip);
3034 if (t.codegen_error_p ())
3036 if (dump_file)
3037 fprintf (dump_file, "codegen error: "
3038 "reverting back to the original code.\n");
3039 set_ifsese_condition (if_region, integer_zero_node);
3041 else
3043 t.translate_pending_phi_nodes ();
3044 if (!t.codegen_error_p ())
3046 sese_insert_phis_for_liveouts (region,
3047 if_region->region->region.exit->src,
3048 if_region->false_region->region.exit,
3049 if_region->true_region->region.exit);
3050 mark_virtual_operands_for_renaming (cfun);
3051 update_ssa (TODO_update_ssa);
3054 graphite_verify ();
3055 scev_reset ();
3056 recompute_all_dominators ();
3057 graphite_verify ();
3059 if (dump_file)
3060 fprintf (dump_file, "[codegen] isl AST to Gimple succeeded.\n");
3062 else
3064 if (dump_file)
3065 fprintf (dump_file, "[codegen] unsuccessful in translating"
3066 " pending phis, reverting back to the original code.\n");
3067 set_ifsese_condition (if_region, integer_zero_node);
3071 free (if_region->true_region);
3072 free (if_region->region);
3073 free (if_region);
3075 ivs_params_clear (ip);
3076 isl_ast_node_free (root_node);
3077 timevar_pop (TV_GRAPHITE_CODE_GEN);
3079 if (dump_file && (dump_flags & TDF_DETAILS))
3081 loop_p loop;
3082 int num_no_dependency = 0;
3084 FOR_EACH_LOOP (loop, 0)
3085 if (loop->can_be_parallel)
3086 num_no_dependency++;
3088 fprintf (dump_file, "%d loops carried no dependency.\n",
3089 num_no_dependency);
3092 return !t.codegen_error_p ();
3095 #endif /* HAVE_isl */