[ARM] Add source mode to coprocessor pattern SETs
[official-gcc.git] / gcc / graphite-isl-ast-to-gimple.c
blob54d07592d2dd8962f97aade223dc46acca94be52
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 bool postpone);
233 bool copy_loop_close_phi_nodes (basic_block old_bb, basic_block new_bb);
234 bool copy_cond_phi_args (gphi *phi, gphi *new_phi, vec<tree> iv_map,
235 bool postpone);
236 bool copy_cond_phi_nodes (basic_block bb, basic_block new_bb,
237 vec<tree> iv_map);
238 bool graphite_copy_stmts_from_block (basic_block bb, basic_block new_bb,
239 vec<tree> iv_map);
240 edge copy_bb_and_scalar_dependences (basic_block bb, edge next_e,
241 vec<tree> iv_map);
242 edge edge_for_new_close_phis (basic_block bb);
243 bool add_phi_arg_for_new_expr (tree old_phi_args[2], tree new_phi_args[2],
244 edge old_bb_dominating_edge,
245 edge old_bb_non_dominating_edge,
246 gphi *phi, gphi *new_phi,
247 basic_block new_bb);
248 bool rename_uses (gimple *copy, gimple_stmt_iterator *gsi_tgt,
249 basic_block old_bb, loop_p loop, vec<tree> iv_map);
250 void set_rename (tree old_name, tree expr);
251 void set_rename_for_each_def (gimple *stmt);
252 void gsi_insert_earliest (gimple_seq seq);
253 tree rename_all_uses (tree new_expr, basic_block new_bb, basic_block old_bb);
254 bool codegen_error_p () const { return codegen_error; }
255 bool is_constant (tree op) const
257 return TREE_CODE (op) == INTEGER_CST
258 || TREE_CODE (op) == REAL_CST
259 || TREE_CODE (op) == COMPLEX_CST
260 || TREE_CODE (op) == VECTOR_CST;
263 private:
264 /* The region to be translated. */
265 sese_info_p region;
267 /* This flag is set when an error occurred during the translation of isl AST
268 to Gimple. */
269 bool codegen_error;
271 /* A vector of all the edges at if_condition merge points. */
272 auto_vec<edge, 2> merge_points;
275 /* Return the tree variable that corresponds to the given isl ast identifier
276 expression (an isl_ast_expr of type isl_ast_expr_id).
278 FIXME: We should replace blind conversion of id's type with derivation
279 of the optimal type when we get the corresponding isl support. Blindly
280 converting type sizes may be problematic when we switch to smaller
281 types. */
283 tree translate_isl_ast_to_gimple::
284 gcc_expression_from_isl_ast_expr_id (tree type,
285 __isl_take isl_ast_expr *expr_id,
286 ivs_params &ip)
288 gcc_assert (isl_ast_expr_get_type (expr_id) == isl_ast_expr_id);
289 isl_id *tmp_isl_id = isl_ast_expr_get_id (expr_id);
290 std::map<isl_id *, tree>::iterator res;
291 res = ip.find (tmp_isl_id);
292 isl_id_free (tmp_isl_id);
293 gcc_assert (res != ip.end () &&
294 "Could not map isl_id to tree expression");
295 isl_ast_expr_free (expr_id);
296 tree t = res->second;
297 tree *val = region->parameter_rename_map->get(t);
299 if (!val)
300 val = &t;
301 return fold_convert (type, *val);
304 /* Converts an isl_ast_expr_int expression E to a GCC expression tree of
305 type TYPE. */
307 tree translate_isl_ast_to_gimple::
308 gcc_expression_from_isl_expr_int (tree type, __isl_take isl_ast_expr *expr)
310 gcc_assert (isl_ast_expr_get_type (expr) == isl_ast_expr_int);
311 isl_val *val = isl_ast_expr_get_val (expr);
312 size_t n = isl_val_n_abs_num_chunks (val, sizeof (HOST_WIDE_INT));
313 HOST_WIDE_INT *chunks = XALLOCAVEC (HOST_WIDE_INT, n);
314 tree res;
315 if (isl_val_get_abs_num_chunks (val, sizeof (HOST_WIDE_INT), chunks) == -1)
316 res = NULL_TREE;
317 else
319 widest_int wi = widest_int::from_array (chunks, n, true);
320 if (isl_val_is_neg (val))
321 wi = -wi;
322 res = wide_int_to_tree (type, wi);
324 isl_val_free (val);
325 isl_ast_expr_free (expr);
326 return res;
329 /* Converts a binary isl_ast_expr_op expression E to a GCC expression tree of
330 type TYPE. */
332 tree translate_isl_ast_to_gimple::
333 binary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
335 isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0);
336 tree tree_lhs_expr = gcc_expression_from_isl_expression (type, arg_expr, ip);
337 arg_expr = isl_ast_expr_get_op_arg (expr, 1);
338 tree tree_rhs_expr = gcc_expression_from_isl_expression (type, arg_expr, ip);
340 enum isl_ast_op_type expr_type = isl_ast_expr_get_op_type (expr);
341 isl_ast_expr_free (expr);
343 if (codegen_error_p ())
344 return NULL_TREE;
346 switch (expr_type)
348 case isl_ast_op_add:
349 return fold_build2 (PLUS_EXPR, type, tree_lhs_expr, tree_rhs_expr);
351 case isl_ast_op_sub:
352 return fold_build2 (MINUS_EXPR, type, tree_lhs_expr, tree_rhs_expr);
354 case isl_ast_op_mul:
355 return fold_build2 (MULT_EXPR, type, tree_lhs_expr, tree_rhs_expr);
357 case isl_ast_op_div:
358 /* As isl operates on arbitrary precision numbers, we may end up with
359 division by 2^64 that is folded to 0. */
360 if (integer_zerop (tree_rhs_expr))
362 codegen_error = true;
363 return NULL_TREE;
365 return fold_build2 (EXACT_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr);
367 case isl_ast_op_pdiv_q:
368 /* As isl operates on arbitrary precision numbers, we may end up with
369 division by 2^64 that is folded to 0. */
370 if (integer_zerop (tree_rhs_expr))
372 codegen_error = true;
373 return NULL_TREE;
375 return fold_build2 (TRUNC_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr);
377 case isl_ast_op_zdiv_r:
378 case isl_ast_op_pdiv_r:
379 /* As isl operates on arbitrary precision numbers, we may end up with
380 division by 2^64 that is folded to 0. */
381 if (integer_zerop (tree_rhs_expr))
383 codegen_error = true;
384 return NULL_TREE;
386 return fold_build2 (TRUNC_MOD_EXPR, type, tree_lhs_expr, tree_rhs_expr);
388 case isl_ast_op_fdiv_q:
389 /* As isl operates on arbitrary precision numbers, we may end up with
390 division by 2^64 that is folded to 0. */
391 if (integer_zerop (tree_rhs_expr))
393 codegen_error = true;
394 return NULL_TREE;
396 return fold_build2 (FLOOR_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr);
398 case isl_ast_op_and:
399 return fold_build2 (TRUTH_ANDIF_EXPR, type,
400 tree_lhs_expr, tree_rhs_expr);
402 case isl_ast_op_or:
403 return fold_build2 (TRUTH_ORIF_EXPR, type, tree_lhs_expr, tree_rhs_expr);
405 case isl_ast_op_eq:
406 return fold_build2 (EQ_EXPR, type, tree_lhs_expr, tree_rhs_expr);
408 case isl_ast_op_le:
409 return fold_build2 (LE_EXPR, type, tree_lhs_expr, tree_rhs_expr);
411 case isl_ast_op_lt:
412 return fold_build2 (LT_EXPR, type, tree_lhs_expr, tree_rhs_expr);
414 case isl_ast_op_ge:
415 return fold_build2 (GE_EXPR, type, tree_lhs_expr, tree_rhs_expr);
417 case isl_ast_op_gt:
418 return fold_build2 (GT_EXPR, type, tree_lhs_expr, tree_rhs_expr);
420 default:
421 gcc_unreachable ();
425 /* Converts a ternary isl_ast_expr_op expression E to a GCC expression tree of
426 type TYPE. */
428 tree translate_isl_ast_to_gimple::
429 ternary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
431 enum isl_ast_op_type t = isl_ast_expr_get_op_type (expr);
432 gcc_assert (t == isl_ast_op_cond || t == isl_ast_op_select);
433 isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0);
434 tree a = gcc_expression_from_isl_expression (type, arg_expr, ip);
435 arg_expr = isl_ast_expr_get_op_arg (expr, 1);
436 tree b = gcc_expression_from_isl_expression (type, arg_expr, ip);
437 arg_expr = isl_ast_expr_get_op_arg (expr, 2);
438 tree c = gcc_expression_from_isl_expression (type, arg_expr, ip);
439 isl_ast_expr_free (expr);
441 if (codegen_error_p ())
442 return NULL_TREE;
444 return fold_build3 (COND_EXPR, type, a, b, c);
447 /* Converts a unary isl_ast_expr_op expression E to a GCC expression tree of
448 type TYPE. */
450 tree translate_isl_ast_to_gimple::
451 unary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
453 gcc_assert (isl_ast_expr_get_op_type (expr) == isl_ast_op_minus);
454 isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0);
455 tree tree_expr = gcc_expression_from_isl_expression (type, arg_expr, ip);
456 isl_ast_expr_free (expr);
457 return codegen_error_p () ? NULL_TREE
458 : fold_build1 (NEGATE_EXPR, type, tree_expr);
461 /* Converts an isl_ast_expr_op expression E with unknown number of arguments
462 to a GCC expression tree of type TYPE. */
464 tree translate_isl_ast_to_gimple::
465 nary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
467 enum tree_code op_code;
468 switch (isl_ast_expr_get_op_type (expr))
470 case isl_ast_op_max:
471 op_code = MAX_EXPR;
472 break;
474 case isl_ast_op_min:
475 op_code = MIN_EXPR;
476 break;
478 default:
479 gcc_unreachable ();
481 isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0);
482 tree res = gcc_expression_from_isl_expression (type, arg_expr, ip);
484 if (codegen_error_p ())
486 isl_ast_expr_free (expr);
487 return NULL_TREE;
490 int i;
491 for (i = 1; i < isl_ast_expr_get_op_n_arg (expr); i++)
493 arg_expr = isl_ast_expr_get_op_arg (expr, i);
494 tree t = gcc_expression_from_isl_expression (type, arg_expr, ip);
496 if (codegen_error_p ())
498 isl_ast_expr_free (expr);
499 return NULL_TREE;
502 res = fold_build2 (op_code, type, res, t);
504 isl_ast_expr_free (expr);
505 return res;
508 /* Converts an isl_ast_expr_op expression E to a GCC expression tree of
509 type TYPE. */
511 tree translate_isl_ast_to_gimple::
512 gcc_expression_from_isl_expr_op (tree type, __isl_take isl_ast_expr *expr,
513 ivs_params &ip)
515 if (codegen_error_p ())
517 isl_ast_expr_free (expr);
518 return NULL_TREE;
521 gcc_assert (isl_ast_expr_get_type (expr) == isl_ast_expr_op);
522 switch (isl_ast_expr_get_op_type (expr))
524 /* These isl ast expressions are not supported yet. */
525 case isl_ast_op_error:
526 case isl_ast_op_call:
527 case isl_ast_op_and_then:
528 case isl_ast_op_or_else:
529 gcc_unreachable ();
531 case isl_ast_op_max:
532 case isl_ast_op_min:
533 return nary_op_to_tree (type, expr, ip);
535 case isl_ast_op_add:
536 case isl_ast_op_sub:
537 case isl_ast_op_mul:
538 case isl_ast_op_div:
539 case isl_ast_op_pdiv_q:
540 case isl_ast_op_pdiv_r:
541 case isl_ast_op_fdiv_q:
542 case isl_ast_op_zdiv_r:
543 case isl_ast_op_and:
544 case isl_ast_op_or:
545 case isl_ast_op_eq:
546 case isl_ast_op_le:
547 case isl_ast_op_lt:
548 case isl_ast_op_ge:
549 case isl_ast_op_gt:
550 return binary_op_to_tree (type, expr, ip);
552 case isl_ast_op_minus:
553 return unary_op_to_tree (type, expr, ip);
555 case isl_ast_op_cond:
556 case isl_ast_op_select:
557 return ternary_op_to_tree (type, expr, ip);
559 default:
560 gcc_unreachable ();
563 return NULL_TREE;
566 /* Converts an isl AST expression E back to a GCC expression tree of
567 type TYPE. */
569 tree translate_isl_ast_to_gimple::
570 gcc_expression_from_isl_expression (tree type, __isl_take isl_ast_expr *expr,
571 ivs_params &ip)
573 if (codegen_error_p ())
575 isl_ast_expr_free (expr);
576 return NULL_TREE;
579 switch (isl_ast_expr_get_type (expr))
581 case isl_ast_expr_id:
582 return gcc_expression_from_isl_ast_expr_id (type, expr, ip);
584 case isl_ast_expr_int:
585 return gcc_expression_from_isl_expr_int (type, expr);
587 case isl_ast_expr_op:
588 return gcc_expression_from_isl_expr_op (type, expr, ip);
590 default:
591 gcc_unreachable ();
594 return NULL_TREE;
597 /* Creates a new LOOP corresponding to isl_ast_node_for. Inserts an
598 induction variable for the new LOOP. New LOOP is attached to CFG
599 starting at ENTRY_EDGE. LOOP is inserted into the loop tree and
600 becomes the child loop of the OUTER_LOOP. NEWIVS_INDEX binds
601 isl's scattering name to the induction variable created for the
602 loop of STMT. The new induction variable is inserted in the NEWIVS
603 vector and is of type TYPE. */
605 struct loop *translate_isl_ast_to_gimple::
606 graphite_create_new_loop (edge entry_edge, __isl_keep isl_ast_node *node_for,
607 loop_p outer, tree type, tree lb, tree ub,
608 ivs_params &ip)
610 isl_ast_expr *for_inc = isl_ast_node_for_get_inc (node_for);
611 tree stride = gcc_expression_from_isl_expression (type, for_inc, ip);
613 /* To fail code generation, we generate wrong code until we discard it. */
614 if (codegen_error_p ())
615 stride = integer_zero_node;
617 tree ivvar = create_tmp_var (type, "graphite_IV");
618 tree iv, iv_after_increment;
619 loop_p loop = create_empty_loop_on_edge
620 (entry_edge, lb, stride, ub, ivvar, &iv, &iv_after_increment,
621 outer ? outer : entry_edge->src->loop_father);
623 isl_ast_expr *for_iterator = isl_ast_node_for_get_iterator (node_for);
624 isl_id *id = isl_ast_expr_get_id (for_iterator);
625 std::map<isl_id *, tree>::iterator res;
626 res = ip.find (id);
627 if (ip.count (id))
628 isl_id_free (res->first);
629 ip[id] = iv;
630 isl_ast_expr_free (for_iterator);
631 return loop;
634 /* Create the loop for a isl_ast_node_for.
636 - NEXT_E is the edge where new generated code should be attached. */
638 edge translate_isl_ast_to_gimple::
639 translate_isl_ast_for_loop (loop_p context_loop,
640 __isl_keep isl_ast_node *node_for, edge next_e,
641 tree type, tree lb, tree ub,
642 ivs_params &ip)
644 gcc_assert (isl_ast_node_get_type (node_for) == isl_ast_node_for);
645 struct loop *loop = graphite_create_new_loop (next_e, node_for, context_loop,
646 type, lb, ub, ip);
647 edge last_e = single_exit (loop);
648 edge to_body = single_succ_edge (loop->header);
649 basic_block after = to_body->dest;
651 /* Translate the body of the loop. */
652 isl_ast_node *for_body = isl_ast_node_for_get_body (node_for);
653 next_e = translate_isl_ast (loop, for_body, to_body, ip);
654 isl_ast_node_free (for_body);
656 /* Early return if we failed to translate loop body. */
657 if (!next_e || codegen_error_p ())
658 return NULL;
660 if (next_e->dest != after)
661 redirect_edge_succ_nodup (next_e, after);
662 set_immediate_dominator (CDI_DOMINATORS, next_e->dest, next_e->src);
664 if (flag_loop_parallelize_all)
666 isl_id *id = isl_ast_node_get_annotation (node_for);
667 gcc_assert (id);
668 ast_build_info *for_info = (ast_build_info *) isl_id_get_user (id);
669 loop->can_be_parallel = for_info->is_parallelizable;
670 free (for_info);
671 isl_id_free (id);
674 return last_e;
677 /* We use this function to get the upper bound because of the form,
678 which is used by isl to represent loops:
680 for (iterator = init; cond; iterator += inc)
688 The loop condition is an arbitrary expression, which contains the
689 current loop iterator.
691 (e.g. iterator + 3 < B && C > iterator + A)
693 We have to know the upper bound of the iterator to generate a loop
694 in Gimple form. It can be obtained from the special representation
695 of the loop condition, which is generated by isl,
696 if the ast_build_atomic_upper_bound option is set. In this case,
697 isl generates a loop condition that consists of the current loop
698 iterator, + an operator (< or <=) and an expression not involving
699 the iterator, which is processed and returned by this function.
701 (e.g iterator <= upper-bound-expression-without-iterator) */
703 static __isl_give isl_ast_expr *
704 get_upper_bound (__isl_keep isl_ast_node *node_for)
706 gcc_assert (isl_ast_node_get_type (node_for) == isl_ast_node_for);
707 isl_ast_expr *for_cond = isl_ast_node_for_get_cond (node_for);
708 gcc_assert (isl_ast_expr_get_type (for_cond) == isl_ast_expr_op);
709 isl_ast_expr *res;
710 switch (isl_ast_expr_get_op_type (for_cond))
712 case isl_ast_op_le:
713 res = isl_ast_expr_get_op_arg (for_cond, 1);
714 break;
716 case isl_ast_op_lt:
718 /* (iterator < ub) => (iterator <= ub - 1). */
719 isl_val *one =
720 isl_val_int_from_si (isl_ast_expr_get_ctx (for_cond), 1);
721 isl_ast_expr *ub = isl_ast_expr_get_op_arg (for_cond, 1);
722 res = isl_ast_expr_sub (ub, isl_ast_expr_from_val (one));
723 break;
726 default:
727 gcc_unreachable ();
729 isl_ast_expr_free (for_cond);
730 return res;
733 /* All loops generated by create_empty_loop_on_edge have the form of
734 a post-test loop:
739 body of the loop;
740 } while (lower bound < upper bound);
742 We create a new if region protecting the loop to be executed, if
743 the execution count is zero (lower bound > upper bound). */
745 edge translate_isl_ast_to_gimple::
746 graphite_create_new_loop_guard (edge entry_edge,
747 __isl_keep isl_ast_node *node_for, tree *type,
748 tree *lb, tree *ub, ivs_params &ip)
750 gcc_assert (isl_ast_node_get_type (node_for) == isl_ast_node_for);
751 tree cond_expr;
752 edge exit_edge;
754 *type =
755 build_nonstandard_integer_type (graphite_expression_type_precision, 0);
756 isl_ast_expr *for_init = isl_ast_node_for_get_init (node_for);
757 *lb = gcc_expression_from_isl_expression (*type, for_init, ip);
759 /* To fail code generation, we generate wrong code until we discard it. */
760 if (codegen_error_p ())
761 *lb = integer_zero_node;
763 isl_ast_expr *upper_bound = get_upper_bound (node_for);
764 *ub = gcc_expression_from_isl_expression (*type, upper_bound, ip);
766 /* To fail code generation, we generate wrong code until we discard it. */
767 if (codegen_error_p ())
768 *ub = integer_zero_node;
770 /* When ub is simply a constant or a parameter, use lb <= ub. */
771 if (TREE_CODE (*ub) == INTEGER_CST || TREE_CODE (*ub) == SSA_NAME)
772 cond_expr = fold_build2 (LE_EXPR, boolean_type_node, *lb, *ub);
773 else
775 tree one = (POINTER_TYPE_P (*type)
776 ? convert_to_ptrofftype (integer_one_node)
777 : fold_convert (*type, integer_one_node));
778 /* Adding +1 and using LT_EXPR helps with loop latches that have a
779 loop iteration count of "PARAMETER - 1". For PARAMETER == 0 this
780 becomes 2^k-1 due to integer overflow, and the condition lb <= ub
781 is true, even if we do not want this. However lb < ub + 1 is false,
782 as expected. */
783 tree ub_one = fold_build2 (POINTER_TYPE_P (*type) ? POINTER_PLUS_EXPR
784 : PLUS_EXPR, *type, *ub, one);
786 cond_expr = fold_build2 (LT_EXPR, boolean_type_node, *lb, ub_one);
789 if (integer_onep (cond_expr))
790 exit_edge = entry_edge;
791 else
792 exit_edge = create_empty_if_region_on_edge (entry_edge,
793 unshare_expr (cond_expr));
795 return exit_edge;
798 /* Translates an isl_ast_node_for to Gimple. */
800 edge translate_isl_ast_to_gimple::
801 translate_isl_ast_node_for (loop_p context_loop, __isl_keep isl_ast_node *node,
802 edge next_e, ivs_params &ip)
804 gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_for);
805 tree type, lb, ub;
806 edge last_e = graphite_create_new_loop_guard (next_e, node, &type,
807 &lb, &ub, ip);
809 if (last_e == next_e)
811 /* There was no guard generated. */
812 last_e = single_succ_edge (split_edge (last_e));
814 translate_isl_ast_for_loop (context_loop, node, next_e,
815 type, lb, ub, ip);
816 return last_e;
819 edge true_e = get_true_edge_from_guard_bb (next_e->dest);
820 merge_points.safe_push (last_e);
822 last_e = single_succ_edge (split_edge (last_e));
823 translate_isl_ast_for_loop (context_loop, node, true_e, type, lb, ub, ip);
825 return last_e;
828 /* Inserts in iv_map a tuple (OLD_LOOP->num, NEW_NAME) for the induction
829 variables of the loops around GBB in SESE.
831 FIXME: Instead of using a vec<tree> that maps each loop id to a possible
832 chrec, we could consider using a map<int, tree> that maps loop ids to the
833 corresponding tree expressions. */
835 void translate_isl_ast_to_gimple::
836 build_iv_mapping (vec<tree> iv_map, gimple_poly_bb_p gbb,
837 __isl_keep isl_ast_expr *user_expr, ivs_params &ip,
838 sese_l &region)
840 gcc_assert (isl_ast_expr_get_type (user_expr) == isl_ast_expr_op &&
841 isl_ast_expr_get_op_type (user_expr) == isl_ast_op_call);
842 int i;
843 isl_ast_expr *arg_expr;
844 for (i = 1; i < isl_ast_expr_get_op_n_arg (user_expr); i++)
846 arg_expr = isl_ast_expr_get_op_arg (user_expr, i);
847 tree type =
848 build_nonstandard_integer_type (graphite_expression_type_precision, 0);
849 tree t = gcc_expression_from_isl_expression (type, arg_expr, ip);
851 /* To fail code generation, we generate wrong code until we discard it. */
852 if (codegen_error_p ())
853 t = integer_zero_node;
855 loop_p old_loop = gbb_loop_at_index (gbb, region, i - 1);
856 iv_map[old_loop->num] = t;
860 /* Translates an isl_ast_node_user to Gimple.
862 FIXME: We should remove iv_map.create (loop->num + 1), if it is possible. */
864 edge translate_isl_ast_to_gimple::
865 translate_isl_ast_node_user (__isl_keep isl_ast_node *node,
866 edge next_e, ivs_params &ip)
868 gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_user);
870 isl_ast_expr *user_expr = isl_ast_node_user_get_expr (node);
871 isl_ast_expr *name_expr = isl_ast_expr_get_op_arg (user_expr, 0);
872 gcc_assert (isl_ast_expr_get_type (name_expr) == isl_ast_expr_id);
874 isl_id *name_id = isl_ast_expr_get_id (name_expr);
875 poly_bb_p pbb = (poly_bb_p) isl_id_get_user (name_id);
876 gcc_assert (pbb);
878 gimple_poly_bb_p gbb = PBB_BLACK_BOX (pbb);
880 isl_ast_expr_free (name_expr);
881 isl_id_free (name_id);
883 gcc_assert (GBB_BB (gbb) != ENTRY_BLOCK_PTR_FOR_FN (cfun) &&
884 "The entry block should not even appear within a scop");
886 const int nb_loops = number_of_loops (cfun);
887 vec<tree> iv_map;
888 iv_map.create (nb_loops);
889 iv_map.safe_grow_cleared (nb_loops);
891 build_iv_mapping (iv_map, gbb, user_expr, ip, pbb->scop->scop_info->region);
892 isl_ast_expr_free (user_expr);
894 basic_block old_bb = GBB_BB (gbb);
895 if (dump_file)
897 fprintf (dump_file,
898 "[codegen] copying from bb_%d on edge (bb_%d, bb_%d)\n",
899 old_bb->index, next_e->src->index, next_e->dest->index);
900 print_loops_bb (dump_file, GBB_BB (gbb), 0, 3);
904 next_e = copy_bb_and_scalar_dependences (old_bb, next_e, iv_map);
906 iv_map.release ();
908 if (codegen_error_p ())
909 return NULL;
911 if (dump_file)
913 fprintf (dump_file, "[codegen] (after copy) new basic block\n");
914 print_loops_bb (dump_file, next_e->src, 0, 3);
917 return next_e;
920 /* Translates an isl_ast_node_block to Gimple. */
922 edge translate_isl_ast_to_gimple::
923 translate_isl_ast_node_block (loop_p context_loop,
924 __isl_keep isl_ast_node *node,
925 edge next_e, ivs_params &ip)
927 gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_block);
928 isl_ast_node_list *node_list = isl_ast_node_block_get_children (node);
929 int i;
930 for (i = 0; i < isl_ast_node_list_n_ast_node (node_list); i++)
932 isl_ast_node *tmp_node = isl_ast_node_list_get_ast_node (node_list, i);
933 next_e = translate_isl_ast (context_loop, tmp_node, next_e, ip);
934 isl_ast_node_free (tmp_node);
936 isl_ast_node_list_free (node_list);
937 return next_e;
940 /* Creates a new if region corresponding to isl's cond. */
942 edge translate_isl_ast_to_gimple::
943 graphite_create_new_guard (edge entry_edge, __isl_take isl_ast_expr *if_cond,
944 ivs_params &ip)
946 tree type =
947 build_nonstandard_integer_type (graphite_expression_type_precision, 0);
948 tree cond_expr = gcc_expression_from_isl_expression (type, if_cond, ip);
950 /* To fail code generation, we generate wrong code until we discard it. */
951 if (codegen_error_p ())
952 cond_expr = integer_zero_node;
954 edge exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr);
955 return exit_edge;
958 /* Translates an isl_ast_node_if to Gimple. */
960 edge translate_isl_ast_to_gimple::
961 translate_isl_ast_node_if (loop_p context_loop,
962 __isl_keep isl_ast_node *node,
963 edge next_e, ivs_params &ip)
965 gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_if);
966 isl_ast_expr *if_cond = isl_ast_node_if_get_cond (node);
967 edge last_e = graphite_create_new_guard (next_e, if_cond, ip);
968 edge true_e = get_true_edge_from_guard_bb (next_e->dest);
969 merge_points.safe_push (last_e);
971 isl_ast_node *then_node = isl_ast_node_if_get_then (node);
972 translate_isl_ast (context_loop, then_node, true_e, ip);
973 isl_ast_node_free (then_node);
975 edge false_e = get_false_edge_from_guard_bb (next_e->dest);
976 isl_ast_node *else_node = isl_ast_node_if_get_else (node);
977 if (isl_ast_node_get_type (else_node) != isl_ast_node_error)
978 translate_isl_ast (context_loop, else_node, false_e, ip);
980 isl_ast_node_free (else_node);
981 return last_e;
984 /* Translates an isl AST node NODE to GCC representation in the
985 context of a SESE. */
987 edge translate_isl_ast_to_gimple::
988 translate_isl_ast (loop_p context_loop, __isl_keep isl_ast_node *node,
989 edge next_e, ivs_params &ip)
991 if (codegen_error_p ())
992 return NULL;
994 switch (isl_ast_node_get_type (node))
996 case isl_ast_node_error:
997 gcc_unreachable ();
999 case isl_ast_node_for:
1000 return translate_isl_ast_node_for (context_loop, node,
1001 next_e, ip);
1003 case isl_ast_node_if:
1004 return translate_isl_ast_node_if (context_loop, node,
1005 next_e, ip);
1007 case isl_ast_node_user:
1008 return translate_isl_ast_node_user (node, next_e, ip);
1010 case isl_ast_node_block:
1011 return translate_isl_ast_node_block (context_loop, node,
1012 next_e, ip);
1014 case isl_ast_node_mark:
1016 isl_ast_node *n = isl_ast_node_mark_get_node (node);
1017 edge e = translate_isl_ast (context_loop, n, next_e, ip);
1018 isl_ast_node_free (n);
1019 return e;
1022 default:
1023 gcc_unreachable ();
1027 /* Return true when BB contains loop close phi nodes. A loop close phi node is
1028 at the exit of loop which takes one argument that is the last value of the
1029 variable being used out of the loop. */
1031 static bool
1032 bb_contains_loop_close_phi_nodes (basic_block bb)
1034 return single_pred_p (bb)
1035 && bb->loop_father != single_pred_edge (bb)->src->loop_father;
1038 /* Return true when BB contains loop phi nodes. A loop phi node is the loop
1039 header containing phi nodes which has one init-edge and one back-edge. */
1041 static bool
1042 bb_contains_loop_phi_nodes (basic_block bb)
1044 if (EDGE_COUNT (bb->preds) != 2)
1045 return false;
1047 unsigned depth = loop_depth (bb->loop_father);
1049 edge preds[2] = { (*bb->preds)[0], (*bb->preds)[1] };
1051 if (depth > loop_depth (preds[0]->src->loop_father)
1052 || depth > loop_depth (preds[1]->src->loop_father))
1053 return true;
1055 /* When one of the edges correspond to the same loop father and other
1056 doesn't. */
1057 if (bb->loop_father != preds[0]->src->loop_father
1058 && bb->loop_father == preds[1]->src->loop_father)
1059 return true;
1061 if (bb->loop_father != preds[1]->src->loop_father
1062 && bb->loop_father == preds[0]->src->loop_father)
1063 return true;
1065 return false;
1068 /* Check if USE is defined in a basic block from where the definition of USE can
1069 propagate from all the paths. FIXME: Verify checks for virtual operands. */
1071 static bool
1072 is_loop_closed_ssa_use (basic_block bb, tree use)
1074 if (TREE_CODE (use) != SSA_NAME || virtual_operand_p (use))
1075 return true;
1077 /* For close-phi nodes def always comes from a loop which has a back-edge. */
1078 if (bb_contains_loop_close_phi_nodes (bb))
1079 return true;
1081 gimple *def = SSA_NAME_DEF_STMT (use);
1082 basic_block def_bb = gimple_bb (def);
1083 return (!def_bb
1084 || flow_bb_inside_loop_p (def_bb->loop_father, bb));
1087 /* Return the number of phi nodes in BB. */
1089 static int
1090 number_of_phi_nodes (basic_block bb)
1092 int num_phis = 0;
1093 for (gphi_iterator psi = gsi_start_phis (bb); !gsi_end_p (psi);
1094 gsi_next (&psi))
1095 num_phis++;
1096 return num_phis;
1099 /* Returns true if BB uses name in one of its PHIs. */
1101 static bool
1102 phi_uses_name (basic_block bb, tree name)
1104 for (gphi_iterator psi = gsi_start_phis (bb); !gsi_end_p (psi);
1105 gsi_next (&psi))
1107 gphi *phi = psi.phi ();
1108 for (unsigned i = 0; i < gimple_phi_num_args (phi); i++)
1110 tree use_arg = gimple_phi_arg_def (phi, i);
1111 if (use_arg == name)
1112 return true;
1115 return false;
1118 /* Return true if RENAME (defined in BB) is a valid use in NEW_BB. The
1119 definition should flow into use, and the use should respect the loop-closed
1120 SSA form. */
1122 bool translate_isl_ast_to_gimple::
1123 is_valid_rename (tree rename, basic_block def_bb, basic_block use_bb,
1124 phi_node_kind phi_kind, tree old_name, basic_block old_bb) const
1126 if (SSA_NAME_IS_DEFAULT_DEF (rename))
1127 return true;
1129 /* The def of the rename must either dominate the uses or come from a
1130 back-edge. Also the def must respect the loop closed ssa form. */
1131 if (!is_loop_closed_ssa_use (use_bb, rename))
1133 if (dump_file)
1135 fprintf (dump_file, "[codegen] rename not in loop closed ssa: ");
1136 print_generic_expr (dump_file, rename, 0);
1137 fprintf (dump_file, "\n");
1139 return false;
1142 if (dominated_by_p (CDI_DOMINATORS, use_bb, def_bb))
1143 return true;
1145 if (bb_contains_loop_phi_nodes (use_bb) && phi_kind == loop_phi)
1147 /* The loop-header dominates the loop-body. */
1148 if (!dominated_by_p (CDI_DOMINATORS, def_bb, use_bb))
1149 return false;
1151 /* RENAME would be used in loop-phi. */
1152 gcc_assert (number_of_phi_nodes (use_bb));
1154 /* For definitions coming from back edges, we should check that
1155 old_name is used in a loop PHI node.
1156 FIXME: Verify if this is true. */
1157 if (phi_uses_name (old_bb, old_name))
1158 return true;
1160 return false;
1163 /* Returns the expression associated to OLD_NAME (which is used in OLD_BB), in
1164 NEW_BB from RENAME_MAP. PHI_KIND determines the kind of phi node. */
1166 tree translate_isl_ast_to_gimple::
1167 get_rename (basic_block new_bb, tree old_name, basic_block old_bb,
1168 phi_node_kind phi_kind) const
1170 gcc_assert (TREE_CODE (old_name) == SSA_NAME);
1171 vec <tree> *renames = region->rename_map->get (old_name);
1173 if (!renames || renames->is_empty ())
1174 return NULL_TREE;
1176 if (1 == renames->length ())
1178 tree rename = (*renames)[0];
1179 if (TREE_CODE (rename) == SSA_NAME)
1181 basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (rename));
1182 if (is_valid_rename (rename, bb, new_bb, phi_kind, old_name, old_bb)
1183 && (phi_kind == close_phi
1184 || ! bb
1185 || flow_bb_inside_loop_p (bb->loop_father, new_bb)))
1186 return rename;
1187 return NULL_TREE;
1190 if (is_constant (rename))
1191 return rename;
1193 return NULL_TREE;
1196 /* More than one renames corresponding to the old_name. Find the rename for
1197 which the definition flows into usage at new_bb. */
1198 int i;
1199 tree t1 = NULL_TREE, t2;
1200 basic_block t1_bb = NULL;
1201 FOR_EACH_VEC_ELT (*renames, i, t2)
1203 basic_block t2_bb = gimple_bb (SSA_NAME_DEF_STMT (t2));
1205 /* Defined in the same basic block as used. */
1206 if (t2_bb == new_bb)
1207 return t2;
1209 /* NEW_BB and T2_BB are in two unrelated if-clauses. */
1210 if (!dominated_by_p (CDI_DOMINATORS, new_bb, t2_bb))
1211 continue;
1213 if (!flow_bb_inside_loop_p (t2_bb->loop_father, new_bb))
1214 continue;
1216 /* Compute the nearest dominator. */
1217 if (!t1 || dominated_by_p (CDI_DOMINATORS, t2_bb, t1_bb))
1219 t1_bb = t2_bb;
1220 t1 = t2;
1224 return t1;
1227 /* Register in RENAME_MAP the rename tuple (OLD_NAME, EXPR).
1228 When OLD_NAME and EXPR are the same we assert. */
1230 void translate_isl_ast_to_gimple::
1231 set_rename (tree old_name, tree expr)
1233 if (dump_file)
1235 fprintf (dump_file, "[codegen] setting rename: old_name = ");
1236 print_generic_expr (dump_file, old_name, 0);
1237 fprintf (dump_file, ", new_name = ");
1238 print_generic_expr (dump_file, expr, 0);
1239 fprintf (dump_file, "\n");
1242 if (old_name == expr)
1243 return;
1245 vec <tree> *renames = region->rename_map->get (old_name);
1247 if (renames)
1248 renames->safe_push (expr);
1249 else
1251 vec<tree> r;
1252 r.create (2);
1253 r.safe_push (expr);
1254 region->rename_map->put (old_name, r);
1257 tree t;
1258 int i;
1259 /* For a parameter of a scop we don't want to rename it. */
1260 FOR_EACH_VEC_ELT (region->params, i, t)
1261 if (old_name == t)
1262 region->parameter_rename_map->put(old_name, expr);
1265 /* Return an iterator to the instructions comes last in the execution order.
1266 Either GSI1 and GSI2 should belong to the same basic block or one of their
1267 respective basic blocks should dominate the other. */
1269 gimple_stmt_iterator
1270 later_of_the_two (gimple_stmt_iterator gsi1, gimple_stmt_iterator gsi2)
1272 basic_block bb1 = gsi_bb (gsi1);
1273 basic_block bb2 = gsi_bb (gsi2);
1275 /* Find the iterator which is the latest. */
1276 if (bb1 == bb2)
1278 gimple *stmt1 = gsi_stmt (gsi1);
1279 gimple *stmt2 = gsi_stmt (gsi2);
1281 if (stmt1 != NULL && stmt2 != NULL)
1283 bool is_phi1 = gimple_code (stmt1) == GIMPLE_PHI;
1284 bool is_phi2 = gimple_code (stmt2) == GIMPLE_PHI;
1286 if (is_phi1 != is_phi2)
1287 return is_phi1 ? gsi2 : gsi1;
1290 /* For empty basic blocks gsis point to the end of the sequence. Since
1291 there is no operator== defined for gimple_stmt_iterator and for gsis
1292 not pointing to a valid statement gsi_next would assert. */
1293 gimple_stmt_iterator gsi = gsi1;
1294 do {
1295 if (gsi_stmt (gsi) == gsi_stmt (gsi2))
1296 return gsi2;
1297 gsi_next (&gsi);
1298 } while (!gsi_end_p (gsi));
1300 return gsi1;
1303 /* Find the basic block closest to the basic block which defines stmt. */
1304 if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
1305 return gsi1;
1307 gcc_assert (dominated_by_p (CDI_DOMINATORS, bb2, bb1));
1308 return gsi2;
1311 /* Insert each statement from SEQ at its earliest insertion p. */
1313 void translate_isl_ast_to_gimple::
1314 gsi_insert_earliest (gimple_seq seq)
1316 update_modified_stmts (seq);
1317 sese_l &codegen_region = region->if_region->true_region->region;
1318 basic_block begin_bb = get_entry_bb (codegen_region);
1320 /* Inserting the gimple statements in a vector because gimple_seq behave
1321 in strage ways when inserting the stmts from it into different basic
1322 blocks one at a time. */
1323 auto_vec<gimple *, 3> stmts;
1324 for (gimple_stmt_iterator gsi = gsi_start (seq); !gsi_end_p (gsi);
1325 gsi_next (&gsi))
1326 stmts.safe_push (gsi_stmt (gsi));
1328 int i;
1329 gimple *use_stmt;
1330 FOR_EACH_VEC_ELT (stmts, i, use_stmt)
1332 gcc_assert (gimple_code (use_stmt) != GIMPLE_PHI);
1333 gimple_stmt_iterator gsi_def_stmt = gsi_start_bb_nondebug (begin_bb);
1335 use_operand_p use_p;
1336 ssa_op_iter op_iter;
1337 FOR_EACH_SSA_USE_OPERAND (use_p, use_stmt, op_iter, SSA_OP_USE)
1339 /* Iterator to the current def of use_p. For function parameters or
1340 anything where def is not found, insert at the beginning of the
1341 generated region. */
1342 gimple_stmt_iterator gsi_stmt = gsi_def_stmt;
1344 tree op = USE_FROM_PTR (use_p);
1345 gimple *stmt = SSA_NAME_DEF_STMT (op);
1346 if (stmt && (gimple_code (stmt) != GIMPLE_NOP))
1347 gsi_stmt = gsi_for_stmt (stmt);
1349 /* For region parameters, insert at the beginning of the generated
1350 region. */
1351 if (!bb_in_sese_p (gsi_bb (gsi_stmt), codegen_region))
1352 gsi_stmt = gsi_def_stmt;
1354 gsi_def_stmt = later_of_the_two (gsi_stmt, gsi_def_stmt);
1357 if (!gsi_stmt (gsi_def_stmt))
1359 gimple_stmt_iterator gsi = gsi_after_labels (gsi_bb (gsi_def_stmt));
1360 gsi_insert_before (&gsi, use_stmt, GSI_NEW_STMT);
1362 else if (gimple_code (gsi_stmt (gsi_def_stmt)) == GIMPLE_PHI)
1364 gimple_stmt_iterator bsi
1365 = gsi_start_bb_nondebug (gsi_bb (gsi_def_stmt));
1366 /* Insert right after the PHI statements. */
1367 gsi_insert_before (&bsi, use_stmt, GSI_NEW_STMT);
1369 else
1370 gsi_insert_after (&gsi_def_stmt, use_stmt, GSI_NEW_STMT);
1372 if (dump_file)
1374 fprintf (dump_file, "[codegen] inserting statement: ");
1375 print_gimple_stmt (dump_file, use_stmt, 0, TDF_VOPS | TDF_MEMSYMS);
1376 print_loops_bb (dump_file, gimple_bb (use_stmt), 0, 3);
1381 /* Collect all the operands of NEW_EXPR by recursively visiting each
1382 operand. */
1384 void translate_isl_ast_to_gimple::
1385 collect_all_ssa_names (tree new_expr, vec<tree> *vec_ssa)
1387 if (new_expr == NULL_TREE)
1388 return;
1390 /* Rename all uses in new_expr. */
1391 if (TREE_CODE (new_expr) == SSA_NAME)
1393 vec_ssa->safe_push (new_expr);
1394 return;
1397 /* Iterate over SSA_NAMES in NEW_EXPR. */
1398 for (int i = 0; i < (TREE_CODE_LENGTH (TREE_CODE (new_expr))); i++)
1400 tree op = TREE_OPERAND (new_expr, i);
1401 collect_all_ssa_names (op, vec_ssa);
1405 /* This is abridged version of the function copied from:
1406 tree.c:substitute_in_expr (tree exp, tree f, tree r). */
1408 static tree
1409 substitute_ssa_name (tree exp, tree f, tree r)
1411 enum tree_code code = TREE_CODE (exp);
1412 tree op0, op1, op2, op3;
1413 tree new_tree;
1415 /* We handle TREE_LIST and COMPONENT_REF separately. */
1416 if (code == TREE_LIST)
1418 op0 = substitute_ssa_name (TREE_CHAIN (exp), f, r);
1419 op1 = substitute_ssa_name (TREE_VALUE (exp), f, r);
1420 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
1421 return exp;
1423 return tree_cons (TREE_PURPOSE (exp), op1, op0);
1425 else if (code == COMPONENT_REF)
1427 tree inner;
1429 /* If this expression is getting a value from a PLACEHOLDER_EXPR
1430 and it is the right field, replace it with R. */
1431 for (inner = TREE_OPERAND (exp, 0);
1432 REFERENCE_CLASS_P (inner);
1433 inner = TREE_OPERAND (inner, 0))
1436 /* The field. */
1437 op1 = TREE_OPERAND (exp, 1);
1439 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && op1 == f)
1440 return r;
1442 /* If this expression hasn't been completed let, leave it alone. */
1443 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && !TREE_TYPE (inner))
1444 return exp;
1446 op0 = substitute_ssa_name (TREE_OPERAND (exp, 0), f, r);
1447 if (op0 == TREE_OPERAND (exp, 0))
1448 return exp;
1450 new_tree
1451 = fold_build3 (COMPONENT_REF, TREE_TYPE (exp), op0, op1, NULL_TREE);
1453 else
1454 switch (TREE_CODE_CLASS (code))
1456 case tcc_constant:
1457 return exp;
1459 case tcc_declaration:
1460 if (exp == f)
1461 return r;
1462 else
1463 return exp;
1465 case tcc_expression:
1466 if (exp == f)
1467 return r;
1469 /* Fall through. */
1471 case tcc_exceptional:
1472 case tcc_unary:
1473 case tcc_binary:
1474 case tcc_comparison:
1475 case tcc_reference:
1476 switch (TREE_CODE_LENGTH (code))
1478 case 0:
1479 if (exp == f)
1480 return r;
1481 return exp;
1483 case 1:
1484 op0 = substitute_ssa_name (TREE_OPERAND (exp, 0), f, r);
1485 if (op0 == TREE_OPERAND (exp, 0))
1486 return exp;
1488 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
1489 break;
1491 case 2:
1492 op0 = substitute_ssa_name (TREE_OPERAND (exp, 0), f, r);
1493 op1 = substitute_ssa_name (TREE_OPERAND (exp, 1), f, r);
1495 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
1496 return exp;
1498 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
1499 break;
1501 case 3:
1502 op0 = substitute_ssa_name (TREE_OPERAND (exp, 0), f, r);
1503 op1 = substitute_ssa_name (TREE_OPERAND (exp, 1), f, r);
1504 op2 = substitute_ssa_name (TREE_OPERAND (exp, 2), f, r);
1506 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
1507 && op2 == TREE_OPERAND (exp, 2))
1508 return exp;
1510 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
1511 break;
1513 case 4:
1514 op0 = substitute_ssa_name (TREE_OPERAND (exp, 0), f, r);
1515 op1 = substitute_ssa_name (TREE_OPERAND (exp, 1), f, r);
1516 op2 = substitute_ssa_name (TREE_OPERAND (exp, 2), f, r);
1517 op3 = substitute_ssa_name (TREE_OPERAND (exp, 3), f, r);
1519 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
1520 && op2 == TREE_OPERAND (exp, 2)
1521 && op3 == TREE_OPERAND (exp, 3))
1522 return exp;
1524 new_tree
1525 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
1526 break;
1528 default:
1529 gcc_unreachable ();
1531 break;
1533 case tcc_vl_exp:
1534 default:
1535 gcc_unreachable ();
1538 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
1540 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
1541 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
1543 return new_tree;
1546 /* Rename all the operands of NEW_EXPR by recursively visiting each operand. */
1548 tree translate_isl_ast_to_gimple::
1549 rename_all_uses (tree new_expr, basic_block new_bb, basic_block old_bb)
1551 auto_vec<tree, 2> ssa_names;
1552 collect_all_ssa_names (new_expr, &ssa_names);
1553 tree t;
1554 int i;
1555 FOR_EACH_VEC_ELT (ssa_names, i, t)
1556 if (tree r = get_rename (new_bb, t, old_bb, unknown_phi))
1557 new_expr = substitute_ssa_name (new_expr, t, r);
1559 return new_expr;
1562 /* For ops which are scev_analyzeable, we can regenerate a new name from its
1563 scalar evolution around LOOP. */
1565 tree translate_isl_ast_to_gimple::
1566 get_rename_from_scev (tree old_name, gimple_seq *stmts, loop_p loop,
1567 basic_block new_bb, basic_block old_bb,
1568 vec<tree> iv_map)
1570 tree scev = scalar_evolution_in_region (region->region, loop, old_name);
1572 /* At this point we should know the exact scev for each
1573 scalar SSA_NAME used in the scop: all the other scalar
1574 SSA_NAMEs should have been translated out of SSA using
1575 arrays with one element. */
1576 tree new_expr;
1577 if (chrec_contains_undetermined (scev))
1579 codegen_error = true;
1580 return build_zero_cst (TREE_TYPE (old_name));
1583 new_expr = chrec_apply_map (scev, iv_map);
1585 /* The apply should produce an expression tree containing
1586 the uses of the new induction variables. We should be
1587 able to use new_expr instead of the old_name in the newly
1588 generated loop nest. */
1589 if (chrec_contains_undetermined (new_expr)
1590 || tree_contains_chrecs (new_expr, NULL))
1592 codegen_error = true;
1593 return build_zero_cst (TREE_TYPE (old_name));
1596 if (TREE_CODE (new_expr) == SSA_NAME)
1598 basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (new_expr));
1599 if (bb && !dominated_by_p (CDI_DOMINATORS, new_bb, bb))
1601 codegen_error = true;
1602 return build_zero_cst (TREE_TYPE (old_name));
1606 new_expr = rename_all_uses (new_expr, new_bb, old_bb);
1608 /* We check all the operands and all of them should dominate the use at
1609 new_expr. */
1610 auto_vec <tree, 2> new_ssa_names;
1611 collect_all_ssa_names (new_expr, &new_ssa_names);
1612 int i;
1613 tree new_ssa_name;
1614 FOR_EACH_VEC_ELT (new_ssa_names, i, new_ssa_name)
1616 if (TREE_CODE (new_ssa_name) == SSA_NAME)
1618 basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (new_ssa_name));
1619 if (bb && !dominated_by_p (CDI_DOMINATORS, new_bb, bb))
1621 codegen_error = true;
1622 return build_zero_cst (TREE_TYPE (old_name));
1627 /* Replace the old_name with the new_expr. */
1628 return force_gimple_operand (unshare_expr (new_expr), stmts,
1629 true, NULL_TREE);
1632 /* Renames the scalar uses of the statement COPY, using the
1633 substitution map RENAME_MAP, inserting the gimplification code at
1634 GSI_TGT, for the translation REGION, with the original copied
1635 statement in LOOP, and using the induction variable renaming map
1636 IV_MAP. Returns true when something has been renamed. */
1638 bool translate_isl_ast_to_gimple::
1639 rename_uses (gimple *copy, gimple_stmt_iterator *gsi_tgt, basic_block old_bb,
1640 loop_p loop, vec<tree> iv_map)
1642 bool changed = false;
1644 if (is_gimple_debug (copy))
1646 if (gimple_debug_bind_p (copy))
1647 gimple_debug_bind_reset_value (copy);
1648 else if (gimple_debug_source_bind_p (copy))
1649 return false;
1650 else
1651 gcc_unreachable ();
1653 return false;
1656 if (dump_file)
1658 fprintf (dump_file, "[codegen] renaming uses of stmt: ");
1659 print_gimple_stmt (dump_file, copy, 0, 0);
1662 use_operand_p use_p;
1663 ssa_op_iter op_iter;
1664 FOR_EACH_SSA_USE_OPERAND (use_p, copy, op_iter, SSA_OP_USE)
1666 tree old_name = USE_FROM_PTR (use_p);
1668 if (dump_file)
1670 fprintf (dump_file, "[codegen] renaming old_name = ");
1671 print_generic_expr (dump_file, old_name, 0);
1672 fprintf (dump_file, "\n");
1675 if (TREE_CODE (old_name) != SSA_NAME
1676 || SSA_NAME_IS_DEFAULT_DEF (old_name))
1677 continue;
1679 changed = true;
1680 tree new_expr = get_rename (gsi_tgt->bb, old_name,
1681 old_bb, unknown_phi);
1683 if (new_expr)
1685 tree type_old_name = TREE_TYPE (old_name);
1686 tree type_new_expr = TREE_TYPE (new_expr);
1688 if (dump_file)
1690 fprintf (dump_file, "[codegen] from rename_map: new_name = ");
1691 print_generic_expr (dump_file, new_expr, 0);
1692 fprintf (dump_file, "\n");
1695 if (type_old_name != type_new_expr
1696 || TREE_CODE (new_expr) != SSA_NAME)
1698 tree var = create_tmp_var (type_old_name, "var");
1700 if (!useless_type_conversion_p (type_old_name, type_new_expr))
1701 new_expr = fold_convert (type_old_name, new_expr);
1703 gimple_seq stmts;
1704 new_expr = force_gimple_operand (new_expr, &stmts, true, var);
1705 gsi_insert_earliest (stmts);
1708 replace_exp (use_p, new_expr);
1709 continue;
1712 gimple_seq stmts;
1713 new_expr = get_rename_from_scev (old_name, &stmts, loop, gimple_bb (copy),
1714 old_bb, iv_map);
1715 if (!new_expr || codegen_error_p ())
1716 return false;
1718 if (dump_file)
1720 fprintf (dump_file, "[codegen] not in rename map, scev: ");
1721 print_generic_expr (dump_file, new_expr, 0);
1722 fprintf (dump_file, "\n");
1725 gsi_insert_earliest (stmts);
1726 replace_exp (use_p, new_expr);
1728 if (TREE_CODE (new_expr) == INTEGER_CST
1729 && is_gimple_assign (copy))
1731 tree rhs = gimple_assign_rhs1 (copy);
1733 if (TREE_CODE (rhs) == ADDR_EXPR)
1734 recompute_tree_invariant_for_addr_expr (rhs);
1737 set_rename (old_name, new_expr);
1740 return changed;
1743 /* Returns a basic block that could correspond to where a constant was defined
1744 in the original code. In the original code OLD_BB had the definition, we
1745 need to find which basic block out of the copies of old_bb, in the new
1746 region, should a definition correspond to if it has to reach BB. */
1748 basic_block translate_isl_ast_to_gimple::
1749 get_def_bb_for_const (basic_block bb, basic_block old_bb) const
1751 vec <basic_block> *bbs = region->copied_bb_map->get (old_bb);
1753 if (!bbs || bbs->is_empty ())
1754 return NULL;
1756 if (1 == bbs->length ())
1757 return (*bbs)[0];
1759 int i;
1760 basic_block b1 = NULL, b2;
1761 FOR_EACH_VEC_ELT (*bbs, i, b2)
1763 if (b2 == bb)
1764 return bb;
1766 /* BB and B2 are in two unrelated if-clauses. */
1767 if (!dominated_by_p (CDI_DOMINATORS, bb, b2))
1768 continue;
1770 /* Compute the nearest dominator. */
1771 if (!b1 || dominated_by_p (CDI_DOMINATORS, b2, b1))
1772 b1 = b2;
1775 return b1;
1778 /* Get the new name of OP (from OLD_BB) to be used in NEW_BB. PHI_KIND
1779 determines the kind of phi node. */
1781 tree translate_isl_ast_to_gimple::
1782 get_new_name (basic_block new_bb, tree op,
1783 basic_block old_bb, phi_node_kind phi_kind) const
1785 /* For constants the names are the same. */
1786 if (TREE_CODE (op) != SSA_NAME)
1787 return op;
1789 return get_rename (new_bb, op, old_bb, phi_kind);
1792 /* Return a debug location for OP. */
1794 static location_t
1795 get_loc (tree op)
1797 location_t loc = UNKNOWN_LOCATION;
1799 if (TREE_CODE (op) == SSA_NAME)
1800 loc = gimple_location (SSA_NAME_DEF_STMT (op));
1801 return loc;
1804 /* Returns the incoming edges of basic_block BB in the pair. The first edge is
1805 the init edge (from outside the loop) and the second one is the back edge
1806 from the same loop. */
1808 std::pair<edge, edge>
1809 get_edges (basic_block bb)
1811 std::pair<edge, edge> edges;
1812 edge e;
1813 edge_iterator ei;
1814 FOR_EACH_EDGE (e, ei, bb->preds)
1815 if (bb->loop_father != e->src->loop_father)
1816 edges.first = e;
1817 else
1818 edges.second = e;
1819 return edges;
1822 /* Copy the PHI arguments from OLD_PHI to the NEW_PHI. The arguments to NEW_PHI
1823 must be found unless they can be POSTPONEd for later. */
1825 bool translate_isl_ast_to_gimple::
1826 copy_loop_phi_args (gphi *old_phi, init_back_edge_pair_t &ibp_old_bb,
1827 gphi *new_phi, init_back_edge_pair_t &ibp_new_bb,
1828 bool postpone)
1830 gcc_assert (gimple_phi_num_args (old_phi) == gimple_phi_num_args (new_phi));
1832 basic_block new_bb = gimple_bb (new_phi);
1833 for (unsigned i = 0; i < gimple_phi_num_args (old_phi); i++)
1835 edge e;
1836 if (gimple_phi_arg_edge (old_phi, i) == ibp_old_bb.first)
1837 e = ibp_new_bb.first;
1838 else
1839 e = ibp_new_bb.second;
1841 tree old_name = gimple_phi_arg_def (old_phi, i);
1842 tree new_name = get_new_name (new_bb, old_name,
1843 gimple_bb (old_phi), loop_phi);
1844 if (new_name)
1846 add_phi_arg (new_phi, new_name, e, get_loc (old_name));
1847 continue;
1850 gimple *old_def_stmt = SSA_NAME_DEF_STMT (old_name);
1851 if (!old_def_stmt || gimple_code (old_def_stmt) == GIMPLE_NOP)
1852 /* If the phi arg was a function arg, or wasn't defined, just use the
1853 old name. */
1854 add_phi_arg (new_phi, old_name, e, get_loc (old_name));
1855 else if (postpone)
1857 /* Postpone code gen for later for those back-edges we don't have the
1858 names yet. */
1859 region->incomplete_phis.safe_push (std::make_pair (old_phi, new_phi));
1860 if (dump_file)
1861 fprintf (dump_file, "[codegen] postpone loop phi nodes.\n");
1863 else
1864 /* Either we should add the arg to phi or, we should postpone. */
1865 return false;
1867 return true;
1870 /* Copy loop phi nodes from BB to NEW_BB. */
1872 bool translate_isl_ast_to_gimple::
1873 copy_loop_phi_nodes (basic_block bb, basic_block new_bb)
1875 if (dump_file)
1876 fprintf (dump_file, "[codegen] copying loop phi nodes in bb_%d.\n",
1877 new_bb->index);
1879 /* Loop phi nodes should have only two arguments. */
1880 gcc_assert (2 == EDGE_COUNT (bb->preds));
1882 /* First edge is the init edge and second is the back edge. */
1883 init_back_edge_pair_t ibp_old_bb = get_edges (bb);
1885 /* First edge is the init edge and second is the back edge. */
1886 init_back_edge_pair_t ibp_new_bb = get_edges (new_bb);
1888 for (gphi_iterator psi = gsi_start_phis (bb); !gsi_end_p (psi);
1889 gsi_next (&psi))
1891 gphi *phi = psi.phi ();
1892 tree res = gimple_phi_result (phi);
1893 if (virtual_operand_p (res))
1894 continue;
1895 if (is_gimple_reg (res) && scev_analyzable_p (res, region->region))
1896 continue;
1898 gphi *new_phi = create_phi_node (NULL_TREE, new_bb);
1899 tree new_res = create_new_def_for (res, new_phi,
1900 gimple_phi_result_ptr (new_phi));
1901 set_rename (res, new_res);
1902 codegen_error = !copy_loop_phi_args (phi, ibp_old_bb, new_phi,
1903 ibp_new_bb, true);
1904 update_stmt (new_phi);
1906 if (dump_file)
1908 fprintf (dump_file, "[codegen] creating loop-phi node: ");
1909 print_gimple_stmt (dump_file, new_phi, 0, 0);
1913 return true;
1916 /* Return the init value of PHI, the value coming from outside the loop. */
1918 static tree
1919 get_loop_init_value (gphi *phi)
1922 loop_p loop = gimple_bb (phi)->loop_father;
1924 edge e;
1925 edge_iterator ei;
1926 FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
1927 if (e->src->loop_father != loop)
1928 return gimple_phi_arg_def (phi, e->dest_idx);
1930 return NULL_TREE;
1933 /* Find the init value (the value which comes from outside the loop), of one of
1934 the operands of DEF which is defined by a loop phi. */
1936 static tree
1937 find_init_value (gimple *def)
1939 if (gimple_code (def) == GIMPLE_PHI)
1940 return get_loop_init_value (as_a <gphi*> (def));
1942 if (gimple_vuse (def))
1943 return NULL_TREE;
1945 ssa_op_iter iter;
1946 use_operand_p use_p;
1947 FOR_EACH_SSA_USE_OPERAND (use_p, def, iter, SSA_OP_USE)
1949 tree use = USE_FROM_PTR (use_p);
1950 if (TREE_CODE (use) == SSA_NAME)
1952 if (tree res = find_init_value (SSA_NAME_DEF_STMT (use)))
1953 return res;
1957 return NULL_TREE;
1960 /* Return the init value, the value coming from outside the loop. */
1962 static tree
1963 find_init_value_close_phi (gphi *phi)
1965 gcc_assert (gimple_phi_num_args (phi) == 1);
1966 tree use_arg = gimple_phi_arg_def (phi, 0);
1967 gimple *def = SSA_NAME_DEF_STMT (use_arg);
1968 return find_init_value (def);
1972 tree translate_isl_ast_to_gimple::
1973 add_close_phis_to_outer_loops (tree last_merge_name, edge last_e,
1974 gimple *old_close_phi)
1976 sese_l &codegen_region = region->if_region->true_region->region;
1977 gimple *stmt = SSA_NAME_DEF_STMT (last_merge_name);
1978 basic_block bb = gimple_bb (stmt);
1979 if (!bb_in_sese_p (bb, codegen_region))
1980 return last_merge_name;
1982 loop_p loop = bb->loop_father;
1983 if (!loop_in_sese_p (loop, codegen_region))
1984 return last_merge_name;
1986 edge e = single_exit (loop);
1988 if (dominated_by_p (CDI_DOMINATORS, e->dest, last_e->src))
1989 return last_merge_name;
1991 tree old_name = gimple_phi_arg_def (old_close_phi, 0);
1992 tree old_close_phi_name = gimple_phi_result (old_close_phi);
1994 bb = e->dest;
1995 if (!bb_contains_loop_close_phi_nodes (bb) || !single_succ_p (bb))
1996 bb = split_edge (e);
1998 gphi *close_phi = create_phi_node (NULL_TREE, bb);
1999 tree res = create_new_def_for (last_merge_name, close_phi,
2000 gimple_phi_result_ptr (close_phi));
2001 set_rename (old_close_phi_name, res);
2002 add_phi_arg (close_phi, last_merge_name, e, get_loc (old_name));
2003 last_merge_name = res;
2005 return add_close_phis_to_outer_loops (last_merge_name, last_e, old_close_phi);
2008 /* Add phi nodes to all merge points of all the diamonds enclosing the loop of
2009 the close phi node PHI. */
2011 bool translate_isl_ast_to_gimple::
2012 add_close_phis_to_merge_points (gphi *old_close_phi, gphi *new_close_phi,
2013 tree default_value)
2015 sese_l &codegen_region = region->if_region->true_region->region;
2016 basic_block default_value_bb = get_entry_bb (codegen_region);
2017 if (SSA_NAME == TREE_CODE (default_value))
2019 gimple *stmt = SSA_NAME_DEF_STMT (default_value);
2020 if (!stmt || gimple_code (stmt) == GIMPLE_NOP)
2021 return false;
2022 default_value_bb = gimple_bb (stmt);
2025 basic_block new_close_phi_bb = gimple_bb (new_close_phi);
2027 tree old_close_phi_name = gimple_phi_result (old_close_phi);
2028 tree new_close_phi_name = gimple_phi_result (new_close_phi);
2029 tree last_merge_name = new_close_phi_name;
2030 tree old_name = gimple_phi_arg_def (old_close_phi, 0);
2032 int i;
2033 edge merge_e;
2034 FOR_EACH_VEC_ELT_REVERSE (merge_points, i, merge_e)
2036 basic_block new_merge_bb = merge_e->src;
2037 if (!dominated_by_p (CDI_DOMINATORS, new_merge_bb, default_value_bb))
2038 continue;
2040 last_merge_name = add_close_phis_to_outer_loops (last_merge_name, merge_e,
2041 old_close_phi);
2043 gphi *merge_phi = create_phi_node (NULL_TREE, new_merge_bb);
2044 tree merge_res = create_new_def_for (old_close_phi_name, merge_phi,
2045 gimple_phi_result_ptr (merge_phi));
2046 set_rename (old_close_phi_name, merge_res);
2048 edge from_loop = NULL, from_default_value = NULL;
2049 edge e;
2050 edge_iterator ei;
2051 FOR_EACH_EDGE (e, ei, new_merge_bb->preds)
2052 if (dominated_by_p (CDI_DOMINATORS, e->src, new_close_phi_bb))
2053 from_loop = e;
2054 else
2055 from_default_value = e;
2057 /* Because CDI_POST_DOMINATORS are not updated, we only rely on
2058 CDI_DOMINATORS, which may not handle all cases where new_close_phi_bb
2059 is contained in another condition. */
2060 if (!from_default_value || !from_loop)
2061 return false;
2063 add_phi_arg (merge_phi, last_merge_name, from_loop, get_loc (old_name));
2064 add_phi_arg (merge_phi, default_value, from_default_value, get_loc (old_name));
2066 if (dump_file)
2068 fprintf (dump_file, "[codegen] Adding guard-phi: ");
2069 print_gimple_stmt (dump_file, merge_phi, 0, 0);
2072 update_stmt (merge_phi);
2073 last_merge_name = merge_res;
2076 return true;
2079 /* Copy all the loop-close phi args from BB to NEW_BB. */
2081 bool translate_isl_ast_to_gimple::
2082 copy_loop_close_phi_args (basic_block old_bb, basic_block new_bb, bool postpone)
2084 for (gphi_iterator psi = gsi_start_phis (old_bb); !gsi_end_p (psi);
2085 gsi_next (&psi))
2087 gphi *old_close_phi = psi.phi ();
2088 tree res = gimple_phi_result (old_close_phi);
2089 if (virtual_operand_p (res))
2090 continue;
2092 if (is_gimple_reg (res) && scev_analyzable_p (res, region->region))
2093 /* Loop close phi nodes should not be scev_analyzable_p. */
2094 gcc_unreachable ();
2096 gphi *new_close_phi = create_phi_node (NULL_TREE, new_bb);
2097 tree new_res = create_new_def_for (res, new_close_phi,
2098 gimple_phi_result_ptr (new_close_phi));
2099 set_rename (res, new_res);
2101 tree old_name = gimple_phi_arg_def (old_close_phi, 0);
2102 tree new_name = get_new_name (new_bb, old_name, old_bb, close_phi);
2104 /* Predecessor basic blocks of a loop close phi should have been code
2105 generated before. FIXME: This is fixable by merging PHIs from inner
2106 loops as well. See: gfortran.dg/graphite/interchange-3.f90. */
2107 if (!new_name)
2108 return false;
2110 add_phi_arg (new_close_phi, new_name, single_pred_edge (new_bb),
2111 get_loc (old_name));
2112 if (dump_file)
2114 fprintf (dump_file, "[codegen] Adding loop close phi: ");
2115 print_gimple_stmt (dump_file, new_close_phi, 0, 0);
2118 update_stmt (new_close_phi);
2120 /* When there is no loop guard around this codegenerated loop, there is no
2121 need to collect the close-phi arg. */
2122 if (merge_points.is_empty ())
2123 continue;
2125 /* Add a PHI in the succ_new_bb for each close phi of the loop. */
2126 tree default_value = find_init_value_close_phi (new_close_phi);
2128 /* A close phi must come from a loop-phi having a default value. */
2129 if (!default_value)
2131 if (!postpone)
2132 return false;
2134 region->incomplete_phis.safe_push (std::make_pair (old_close_phi,
2135 new_close_phi));
2136 if (dump_file)
2138 fprintf (dump_file, "[codegen] postpone close phi nodes: ");
2139 print_gimple_stmt (dump_file, new_close_phi, 0, 0);
2141 continue;
2144 if (!add_close_phis_to_merge_points (old_close_phi, new_close_phi,
2145 default_value))
2146 return false;
2149 return true;
2152 /* Copy loop close phi nodes from BB to NEW_BB. */
2154 bool translate_isl_ast_to_gimple::
2155 copy_loop_close_phi_nodes (basic_block old_bb, basic_block new_bb)
2157 if (dump_file)
2158 fprintf (dump_file, "[codegen] copying loop close phi nodes in bb_%d.\n",
2159 new_bb->index);
2160 /* Loop close phi nodes should have only one argument. */
2161 gcc_assert (1 == EDGE_COUNT (old_bb->preds));
2163 return copy_loop_close_phi_args (old_bb, new_bb, true);
2167 /* Add NEW_NAME as the ARGNUM-th arg of NEW_PHI which is in NEW_BB.
2168 DOMINATING_PRED is the predecessor basic block of OLD_BB which dominates the
2169 other pred of OLD_BB as well. If no such basic block exists then it is NULL.
2170 NON_DOMINATING_PRED is a pred which does not dominate OLD_BB, it cannot be
2171 NULL.
2173 Case1: OLD_BB->preds {BB1, BB2} and BB1 does not dominate BB2 and vice versa.
2174 In this case DOMINATING_PRED = NULL.
2176 Case2: OLD_BB->preds {BB1, BB2} and BB1 dominates BB2.
2178 Returns true on successful copy of the args, false otherwise. */
2180 bool translate_isl_ast_to_gimple::
2181 add_phi_arg_for_new_expr (tree old_phi_args[2], tree new_phi_args[2],
2182 edge old_bb_dominating_edge,
2183 edge old_bb_non_dominating_edge,
2184 gphi *phi, gphi *new_phi,
2185 basic_block new_bb)
2187 basic_block def_pred[2] = { NULL, NULL };
2188 int not_found_bb_index = -1;
2189 for (int i = 0; i < 2; i++)
2191 /* If the corresponding def_bb could not be found the entry will be
2192 NULL. */
2193 if (TREE_CODE (old_phi_args[i]) == INTEGER_CST)
2194 def_pred[i] = get_def_bb_for_const (new_bb,
2195 gimple_phi_arg_edge (phi, i)->src);
2196 else if (new_phi_args[i] && (TREE_CODE (new_phi_args[i]) == SSA_NAME))
2197 def_pred[i] = gimple_bb (SSA_NAME_DEF_STMT (new_phi_args[i]));
2199 if (!def_pred[i])
2201 /* When non are available bail out. */
2202 if (not_found_bb_index != -1)
2203 return false;
2204 not_found_bb_index = i;
2208 /* Here we are pattern matching on the structure of CFG w.r.t. old one. */
2209 if (old_bb_dominating_edge)
2211 if (not_found_bb_index != -1)
2212 return false;
2214 basic_block new_pred1 = (*new_bb->preds)[0]->src;
2215 basic_block new_pred2 = (*new_bb->preds)[1]->src;
2216 vec <basic_block> *bbs
2217 = region->copied_bb_map->get (old_bb_non_dominating_edge->src);
2219 /* Could not find a mapping. */
2220 if (!bbs)
2221 return false;
2223 basic_block new_pred = NULL;
2224 basic_block b;
2225 int i;
2226 FOR_EACH_VEC_ELT (*bbs, i, b)
2228 if (dominated_by_p (CDI_DOMINATORS, new_pred1, b))
2230 /* FIXME: If we have already found new_pred then we have to
2231 disambiguate, bail out for now. */
2232 if (new_pred)
2233 return false;
2234 new_pred = new_pred1;
2236 if (dominated_by_p (CDI_DOMINATORS, new_pred2, b))
2238 /* FIXME: If we have already found new_pred then we have to either
2239 it dominates both or we have to disambiguate, bail out. */
2240 if (new_pred)
2241 return false;
2242 new_pred = new_pred2;
2246 if (!new_pred)
2247 return false;
2249 edge new_non_dominating_edge = find_edge (new_pred, new_bb);
2250 gcc_assert (new_non_dominating_edge);
2251 /* FIXME: Validate each args just like in loop-phis. */
2252 /* By the process of elimination we first insert insert phi-edge for
2253 non-dominating pred which is computed above and then we insert the
2254 remaining one. */
2255 int inserted_edge = 0;
2256 for (; inserted_edge < 2; inserted_edge++)
2258 edge new_bb_pred_edge = gimple_phi_arg_edge (new_phi, inserted_edge);
2259 if (new_non_dominating_edge == new_bb_pred_edge)
2261 add_phi_arg (new_phi, new_phi_args[inserted_edge],
2262 new_non_dominating_edge,
2263 get_loc (old_phi_args[inserted_edge]));
2264 break;
2267 if (inserted_edge == 2)
2268 return false;
2270 int edge_dominating = inserted_edge == 0 ? 1 : 0;
2272 edge new_dominating_edge = NULL;
2273 for (inserted_edge = 0; inserted_edge < 2; inserted_edge++)
2275 edge e = gimple_phi_arg_edge (new_phi, inserted_edge);
2276 if (e != new_non_dominating_edge)
2278 new_dominating_edge = e;
2279 add_phi_arg (new_phi, new_phi_args[edge_dominating],
2280 new_dominating_edge,
2281 get_loc (old_phi_args[inserted_edge]));
2282 break;
2285 gcc_assert (new_dominating_edge);
2287 else
2289 /* Classic diamond structure: both edges are non-dominating. We need to
2290 find one unique edge then the other can be found be elimination. If
2291 any definition (def_pred) dominates both the preds of new_bb then we
2292 bail out. Entries of def_pred maybe NULL, in that case we must
2293 uniquely find pred with help of only one entry. */
2294 edge new_e[2] = { NULL, NULL };
2295 for (int i = 0; i < 2; i++)
2297 edge e;
2298 edge_iterator ei;
2299 FOR_EACH_EDGE (e, ei, new_bb->preds)
2300 if (def_pred[i]
2301 && dominated_by_p (CDI_DOMINATORS, e->src, def_pred[i]))
2303 if (new_e[i])
2304 /* We do not know how to handle the case when def_pred
2305 dominates more than a predecessor. */
2306 return false;
2307 new_e[i] = e;
2311 gcc_assert (new_e[0] || new_e[1]);
2313 /* Find the other edge by process of elimination. */
2314 if (not_found_bb_index != -1)
2316 gcc_assert (!new_e[not_found_bb_index]);
2317 int found_bb_index = not_found_bb_index == 1 ? 0 : 1;
2318 edge e;
2319 edge_iterator ei;
2320 FOR_EACH_EDGE (e, ei, new_bb->preds)
2322 if (new_e[found_bb_index] == e)
2323 continue;
2324 new_e[not_found_bb_index] = e;
2328 /* Add edges to phi args. */
2329 for (int i = 0; i < 2; i++)
2330 add_phi_arg (new_phi, new_phi_args[i], new_e[i],
2331 get_loc (old_phi_args[i]));
2334 return true;
2337 /* Copy the arguments of cond-phi node PHI, to NEW_PHI in the codegenerated
2338 region. If postpone is true and it isn't possible to copy any arg of PHI,
2339 the PHI is added to the REGION->INCOMPLETE_PHIS to be codegenerated later.
2340 Returns false if the copying was unsuccessful. */
2342 bool translate_isl_ast_to_gimple::
2343 copy_cond_phi_args (gphi *phi, gphi *new_phi, vec<tree> iv_map, bool postpone)
2345 if (dump_file)
2346 fprintf (dump_file, "[codegen] copying cond phi args.\n");
2347 gcc_assert (2 == gimple_phi_num_args (phi));
2349 basic_block new_bb = gimple_bb (new_phi);
2350 loop_p loop = gimple_bb (phi)->loop_father;
2352 basic_block old_bb = gimple_bb (phi);
2353 edge old_bb_non_dominating_edge = NULL, old_bb_dominating_edge = NULL;
2355 edge e;
2356 edge_iterator ei;
2357 FOR_EACH_EDGE (e, ei, old_bb->preds)
2358 if (!dominated_by_p (CDI_DOMINATORS, old_bb, e->src))
2359 old_bb_non_dominating_edge = e;
2360 else
2361 old_bb_dominating_edge = e;
2363 gcc_assert (!dominated_by_p (CDI_DOMINATORS, old_bb,
2364 old_bb_non_dominating_edge->src));
2366 tree new_phi_args[2];
2367 tree old_phi_args[2];
2369 for (unsigned i = 0; i < gimple_phi_num_args (phi); i++)
2371 tree old_name = gimple_phi_arg_def (phi, i);
2372 tree new_name = get_new_name (new_bb, old_name, old_bb, cond_phi);
2373 old_phi_args[i] = old_name;
2374 if (new_name)
2376 new_phi_args [i] = new_name;
2377 continue;
2380 /* If the phi-arg was a parameter. */
2381 if (vec_find (region->params, old_name) != -1)
2383 new_phi_args [i] = old_name;
2384 if (dump_file)
2386 fprintf (dump_file,
2387 "[codegen] parameter argument to phi, new_expr: ");
2388 print_generic_expr (dump_file, new_phi_args[i], 0);
2389 fprintf (dump_file, "\n");
2391 continue;
2394 gimple *old_def_stmt = SSA_NAME_DEF_STMT (old_name);
2395 if (!old_def_stmt || gimple_code (old_def_stmt) == GIMPLE_NOP)
2396 /* FIXME: If the phi arg was a function arg, or wasn't defined, just use
2397 the old name. */
2398 return false;
2400 if (postpone)
2402 /* If the phi-arg is scev-analyzeable but only in the first stage. */
2403 if (is_gimple_reg (old_name)
2404 && scev_analyzable_p (old_name, region->region))
2406 gimple_seq stmts;
2407 tree new_expr = get_rename_from_scev (old_name, &stmts, loop,
2408 new_bb, old_bb, iv_map);
2409 if (codegen_error_p ())
2410 return false;
2412 gcc_assert (new_expr);
2413 if (dump_file)
2415 fprintf (dump_file,
2416 "[codegen] scev analyzeable, new_expr: ");
2417 print_generic_expr (dump_file, new_expr, 0);
2418 fprintf (dump_file, "\n");
2420 gsi_insert_earliest (stmts);
2421 new_phi_args[i] = new_expr;
2422 continue;
2425 /* Postpone code gen for later for back-edges. */
2426 region->incomplete_phis.safe_push (std::make_pair (phi, new_phi));
2428 if (dump_file)
2430 fprintf (dump_file, "[codegen] postpone cond phi nodes: ");
2431 print_gimple_stmt (dump_file, new_phi, 0, 0);
2434 new_phi_args [i] = NULL_TREE;
2435 continue;
2437 else
2438 /* Either we should add the arg to phi or, we should postpone. */
2439 return false;
2442 /* If none of the args have been determined in the first stage then wait until
2443 later. */
2444 if (postpone && !new_phi_args[0] && !new_phi_args[1])
2445 return true;
2447 return add_phi_arg_for_new_expr (old_phi_args, new_phi_args,
2448 old_bb_dominating_edge,
2449 old_bb_non_dominating_edge,
2450 phi, new_phi, new_bb);
2453 /* Copy cond phi nodes from BB to NEW_BB. A cond-phi node is a basic block
2454 containing phi nodes coming from two predecessors, and none of them are back
2455 edges. */
2457 bool translate_isl_ast_to_gimple::
2458 copy_cond_phi_nodes (basic_block bb, basic_block new_bb, vec<tree> iv_map)
2461 gcc_assert (!bb_contains_loop_close_phi_nodes (bb));
2463 /* TODO: Handle cond phi nodes with more than 2 predecessors. */
2464 if (EDGE_COUNT (bb->preds) != 2)
2465 return false;
2467 if (dump_file)
2468 fprintf (dump_file, "[codegen] copying cond phi nodes in bb_%d.\n",
2469 new_bb->index);
2471 for (gphi_iterator psi = gsi_start_phis (bb); !gsi_end_p (psi);
2472 gsi_next (&psi))
2474 gphi *phi = psi.phi ();
2475 tree res = gimple_phi_result (phi);
2476 if (virtual_operand_p (res))
2477 continue;
2479 gphi *new_phi = create_phi_node (NULL_TREE, new_bb);
2480 tree new_res = create_new_def_for (res, new_phi,
2481 gimple_phi_result_ptr (new_phi));
2482 set_rename (res, new_res);
2484 if (!copy_cond_phi_args (phi, new_phi, iv_map, true))
2485 return false;
2487 update_stmt (new_phi);
2490 return true;
2493 /* Return true if STMT should be copied from region to the new code-generated
2494 region. LABELs, CONDITIONS, induction-variables and region parameters need
2495 not be copied. */
2497 static bool
2498 should_copy_to_new_region (gimple *stmt, sese_info_p region)
2500 /* Do not copy labels or conditions. */
2501 if (gimple_code (stmt) == GIMPLE_LABEL
2502 || gimple_code (stmt) == GIMPLE_COND)
2503 return false;
2505 tree lhs;
2506 /* Do not copy induction variables. */
2507 if (is_gimple_assign (stmt)
2508 && (lhs = gimple_assign_lhs (stmt))
2509 && TREE_CODE (lhs) == SSA_NAME
2510 && is_gimple_reg (lhs)
2511 && scev_analyzable_p (lhs, region->region))
2512 return false;
2514 /* Do not copy parameters that have been generated in the header of the
2515 scop. */
2516 if (is_gimple_assign (stmt)
2517 && (lhs = gimple_assign_lhs (stmt))
2518 && TREE_CODE (lhs) == SSA_NAME
2519 && region->parameter_rename_map->get(lhs))
2520 return false;
2522 return true;
2525 /* Create new names for all the definitions created by COPY and add replacement
2526 mappings for each new name. */
2528 void translate_isl_ast_to_gimple::
2529 set_rename_for_each_def (gimple *stmt)
2531 def_operand_p def_p;
2532 ssa_op_iter op_iter;
2533 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, op_iter, SSA_OP_ALL_DEFS)
2535 tree old_name = DEF_FROM_PTR (def_p);
2536 tree new_name = create_new_def_for (old_name, stmt, def_p);
2537 set_rename (old_name, new_name);
2541 /* Duplicates the statements of basic block BB into basic block NEW_BB
2542 and compute the new induction variables according to the IV_MAP. */
2544 bool translate_isl_ast_to_gimple::
2545 graphite_copy_stmts_from_block (basic_block bb, basic_block new_bb,
2546 vec<tree> iv_map)
2548 /* Iterator poining to the place where new statement (s) will be inserted. */
2549 gimple_stmt_iterator gsi_tgt = gsi_last_bb (new_bb);
2551 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
2552 gsi_next (&gsi))
2554 gimple *stmt = gsi_stmt (gsi);
2555 if (!should_copy_to_new_region (stmt, region))
2556 continue;
2558 /* Create a new copy of STMT and duplicate STMT's virtual
2559 operands. */
2560 gimple *copy = gimple_copy (stmt);
2561 gsi_insert_after (&gsi_tgt, copy, GSI_NEW_STMT);
2563 if (dump_file)
2565 fprintf (dump_file, "[codegen] inserting statement: ");
2566 print_gimple_stmt (dump_file, copy, 0, 0);
2569 maybe_duplicate_eh_stmt (copy, stmt);
2570 gimple_duplicate_stmt_histograms (cfun, copy, cfun, stmt);
2572 /* Crete new names for each def in the copied stmt. */
2573 set_rename_for_each_def (copy);
2575 loop_p loop = bb->loop_father;
2576 if (rename_uses (copy, &gsi_tgt, bb, loop, iv_map))
2578 fold_stmt_inplace (&gsi_tgt);
2579 gcc_assert (gsi_stmt (gsi_tgt) == copy);
2582 if (codegen_error_p ())
2583 return false;
2585 /* For each SSA_NAME in the parameter_rename_map rename their usage. */
2586 ssa_op_iter iter;
2587 use_operand_p use_p;
2588 if (!is_gimple_debug (copy))
2589 FOR_EACH_SSA_USE_OPERAND (use_p, copy, iter, SSA_OP_USE)
2591 tree old_name = USE_FROM_PTR (use_p);
2593 if (TREE_CODE (old_name) != SSA_NAME
2594 || SSA_NAME_IS_DEFAULT_DEF (old_name))
2595 continue;
2597 tree *new_expr = region->parameter_rename_map->get (old_name);
2598 if (!new_expr)
2599 continue;
2601 replace_exp (use_p, *new_expr);
2604 update_stmt (copy);
2607 return true;
2611 /* Given a basic block containing close-phi it returns the new basic block where
2612 to insert a copy of the close-phi nodes. All the uses in close phis should
2613 come from a single loop otherwise it returns NULL. */
2615 edge translate_isl_ast_to_gimple::
2616 edge_for_new_close_phis (basic_block bb)
2618 /* Make sure that NEW_BB is the new_loop->exit->dest. We find the definition
2619 of close phi in the original code and then find the mapping of basic block
2620 defining that variable. If there are multiple close-phis and they are
2621 defined in different loops (in the original or in the new code) because of
2622 loop splitting, then we bail out. */
2623 loop_p new_loop = NULL;
2624 for (gphi_iterator psi = gsi_start_phis (bb); !gsi_end_p (psi);
2625 gsi_next (&psi))
2627 gphi *phi = psi.phi ();
2628 tree name = gimple_phi_arg_def (phi, 0);
2629 basic_block old_loop_bb = gimple_bb (SSA_NAME_DEF_STMT (name));
2631 vec <basic_block> *bbs = region->copied_bb_map->get (old_loop_bb);
2632 if (!bbs || bbs->length () != 1)
2633 /* This is one of the places which shows preserving original structure
2634 is not always possible, as we may need to insert close PHI for a loop
2635 where the latch does not have any mapping, or the mapping is
2636 ambiguous. */
2637 return NULL;
2639 if (!new_loop)
2640 new_loop = (*bbs)[0]->loop_father;
2641 else if (new_loop != (*bbs)[0]->loop_father)
2642 return NULL;
2645 if (!new_loop)
2646 return NULL;
2648 return single_exit (new_loop);
2651 /* Copies BB and includes in the copied BB all the statements that can
2652 be reached following the use-def chains from the memory accesses,
2653 and returns the next edge following this new block. */
2655 edge translate_isl_ast_to_gimple::
2656 copy_bb_and_scalar_dependences (basic_block bb, edge next_e, vec<tree> iv_map)
2658 int num_phis = number_of_phi_nodes (bb);
2660 if (region->copied_bb_map->get (bb))
2662 /* FIXME: we should be able to handle phi nodes with args coming from
2663 outside the region. */
2664 if (num_phis)
2666 codegen_error = true;
2667 return NULL;
2671 basic_block new_bb = NULL;
2672 if (bb_contains_loop_close_phi_nodes (bb))
2674 if (dump_file)
2675 fprintf (dump_file, "[codegen] bb_%d contains close phi nodes.\n",
2676 bb->index);
2678 edge e = edge_for_new_close_phis (bb);
2679 if (!e)
2681 codegen_error = true;
2682 return NULL;
2685 basic_block phi_bb = e->dest;
2687 if (!bb_contains_loop_close_phi_nodes (phi_bb) || !single_succ_p (phi_bb))
2688 phi_bb = split_edge (e);
2690 gcc_assert (single_pred_edge (phi_bb)->src->loop_father
2691 != single_pred_edge (phi_bb)->dest->loop_father);
2693 if (!copy_loop_close_phi_nodes (bb, phi_bb))
2695 codegen_error = true;
2696 return NULL;
2699 if (e == next_e)
2700 new_bb = phi_bb;
2701 else
2702 new_bb = split_edge (next_e);
2704 else
2706 new_bb = split_edge (next_e);
2707 if (num_phis > 0 && bb_contains_loop_phi_nodes (bb))
2709 basic_block phi_bb = next_e->dest->loop_father->header;
2711 /* At this point we are unable to codegenerate by still preserving the SSA
2712 structure because maybe the loop is completely unrolled and the PHIs
2713 and cross-bb scalar dependencies are untrackable w.r.t. the original
2714 code. See gfortran.dg/graphite/pr29832.f90. */
2715 if (EDGE_COUNT (bb->preds) != EDGE_COUNT (phi_bb->preds))
2717 codegen_error = true;
2718 return NULL;
2721 /* In case isl did some loop peeling, like this:
2723 S_8(0);
2724 for (int c1 = 1; c1 <= 5; c1 += 1) {
2725 S_8(c1);
2727 S_8(6);
2729 there should be no loop-phi nodes in S_8(0).
2731 FIXME: We need to reason about dynamic instances of S_8, i.e., the
2732 values of all scalar variables: for the moment we instantiate only
2733 SCEV analyzable expressions on the iteration domain, and we need to
2734 extend that to reductions that cannot be analyzed by SCEV. */
2735 if (!bb_in_sese_p (phi_bb, region->if_region->true_region->region))
2737 codegen_error = true;
2738 return NULL;
2741 if (dump_file)
2742 fprintf (dump_file, "[codegen] bb_%d contains loop phi nodes.\n",
2743 bb->index);
2744 if (!copy_loop_phi_nodes (bb, phi_bb))
2746 codegen_error = true;
2747 return NULL;
2750 else if (num_phis > 0)
2752 if (dump_file)
2753 fprintf (dump_file, "[codegen] bb_%d contains cond phi nodes.\n",
2754 bb->index);
2756 basic_block phi_bb = single_pred (new_bb);
2757 loop_p loop_father = new_bb->loop_father;
2759 /* Move back until we find the block with two predecessors. */
2760 while (single_pred_p (phi_bb))
2761 phi_bb = single_pred_edge (phi_bb)->src;
2763 /* If a corresponding merge-point was not found, then abort codegen. */
2764 if (phi_bb->loop_father != loop_father
2765 || !bb_in_sese_p (phi_bb, region->if_region->true_region->region)
2766 || !copy_cond_phi_nodes (bb, phi_bb, iv_map))
2768 codegen_error = true;
2769 return NULL;
2774 if (dump_file)
2775 fprintf (dump_file, "[codegen] copying from bb_%d to bb_%d.\n",
2776 bb->index, new_bb->index);
2778 vec <basic_block> *copied_bbs = region->copied_bb_map->get (bb);
2779 if (copied_bbs)
2780 copied_bbs->safe_push (new_bb);
2781 else
2783 vec<basic_block> bbs;
2784 bbs.create (2);
2785 bbs.safe_push (new_bb);
2786 region->copied_bb_map->put (bb, bbs);
2789 if (!graphite_copy_stmts_from_block (bb, new_bb, iv_map))
2791 codegen_error = true;
2792 return NULL;
2795 return single_succ_edge (new_bb);
2798 /* Patch the missing arguments of the phi nodes. */
2800 void translate_isl_ast_to_gimple::
2801 translate_pending_phi_nodes ()
2803 int i;
2804 phi_rename *rename;
2805 FOR_EACH_VEC_ELT (region->incomplete_phis, i, rename)
2807 gphi *old_phi = rename->first;
2808 gphi *new_phi = rename->second;
2809 basic_block old_bb = gimple_bb (old_phi);
2810 basic_block new_bb = gimple_bb (new_phi);
2812 /* First edge is the init edge and second is the back edge. */
2813 init_back_edge_pair_t ibp_old_bb = get_edges (old_bb);
2814 init_back_edge_pair_t ibp_new_bb = get_edges (new_bb);
2816 if (dump_file)
2818 fprintf (dump_file, "[codegen] translating pending old-phi: ");
2819 print_gimple_stmt (dump_file, old_phi, 0, 0);
2822 auto_vec <tree, 1> iv_map;
2823 if (bb_contains_loop_phi_nodes (new_bb))
2824 codegen_error = !copy_loop_phi_args (old_phi, ibp_old_bb, new_phi,
2825 ibp_new_bb, false);
2826 else if (bb_contains_loop_close_phi_nodes (new_bb))
2827 codegen_error = !copy_loop_close_phi_args (old_bb, new_bb, false);
2828 else
2829 codegen_error = !copy_cond_phi_args (old_phi, new_phi, iv_map, false);
2831 if (dump_file)
2833 fprintf (dump_file, "[codegen] to new-phi: ");
2834 print_gimple_stmt (dump_file, new_phi, 0, 0);
2836 if (codegen_error_p ())
2837 return;
2841 /* Add isl's parameter identifiers and corresponding trees to ivs_params. */
2843 void translate_isl_ast_to_gimple::
2844 add_parameters_to_ivs_params (scop_p scop, ivs_params &ip)
2846 sese_info_p region = scop->scop_info;
2847 unsigned nb_parameters = isl_set_dim (scop->param_context, isl_dim_param);
2848 gcc_assert (nb_parameters == region->params.length ());
2849 unsigned i;
2850 for (i = 0; i < nb_parameters; i++)
2852 isl_id *tmp_id = isl_set_get_dim_id (scop->param_context,
2853 isl_dim_param, i);
2854 ip[tmp_id] = region->params[i];
2859 /* Generates a build, which specifies the constraints on the parameters. */
2861 __isl_give isl_ast_build *translate_isl_ast_to_gimple::
2862 generate_isl_context (scop_p scop)
2864 isl_set *context_isl = isl_set_params (isl_set_copy (scop->param_context));
2865 return isl_ast_build_from_context (context_isl);
2868 /* This method is executed before the construction of a for node. */
2869 __isl_give isl_id *
2870 ast_build_before_for (__isl_keep isl_ast_build *build, void *user)
2872 isl_union_map *dependences = (isl_union_map *) user;
2873 ast_build_info *for_info = XNEW (struct ast_build_info);
2874 isl_union_map *schedule = isl_ast_build_get_schedule (build);
2875 isl_space *schedule_space = isl_ast_build_get_schedule_space (build);
2876 int dimension = isl_space_dim (schedule_space, isl_dim_out);
2877 for_info->is_parallelizable =
2878 !carries_deps (schedule, dependences, dimension);
2879 isl_union_map_free (schedule);
2880 isl_space_free (schedule_space);
2881 isl_id *id = isl_id_alloc (isl_ast_build_get_ctx (build), "", for_info);
2882 return id;
2885 /* Generate isl AST from schedule of SCOP. */
2887 __isl_give isl_ast_node *translate_isl_ast_to_gimple::
2888 scop_to_isl_ast (scop_p scop)
2890 gcc_assert (scop->transformed_schedule);
2892 /* Set the separate option to reduce control flow overhead. */
2893 isl_schedule *schedule = isl_schedule_map_schedule_node_bottom_up
2894 (isl_schedule_copy (scop->transformed_schedule), set_separate_option, NULL);
2895 isl_ast_build *context_isl = generate_isl_context (scop);
2897 if (flag_loop_parallelize_all)
2899 scop_get_dependences (scop);
2900 context_isl =
2901 isl_ast_build_set_before_each_for (context_isl, ast_build_before_for,
2902 scop->dependence);
2905 isl_ast_node *ast_isl = isl_ast_build_node_from_schedule
2906 (context_isl, schedule);
2907 isl_ast_build_free (context_isl);
2908 return ast_isl;
2911 /* Copy def from sese REGION to the newly created TO_REGION. TR is defined by
2912 DEF_STMT. GSI points to entry basic block of the TO_REGION. */
2914 static void
2915 copy_def (tree tr, gimple *def_stmt, sese_info_p region, sese_info_p to_region,
2916 gimple_stmt_iterator *gsi)
2918 if (!defined_in_sese_p (tr, region->region))
2919 return;
2921 ssa_op_iter iter;
2922 use_operand_p use_p;
2923 FOR_EACH_SSA_USE_OPERAND (use_p, def_stmt, iter, SSA_OP_USE)
2925 tree use_tr = USE_FROM_PTR (use_p);
2927 /* Do not copy parameters that have been generated in the header of the
2928 scop. */
2929 if (region->parameter_rename_map->get(use_tr))
2930 continue;
2932 gimple *def_of_use = SSA_NAME_DEF_STMT (use_tr);
2933 if (!def_of_use)
2934 continue;
2936 copy_def (use_tr, def_of_use, region, to_region, gsi);
2939 gimple *copy = gimple_copy (def_stmt);
2940 gsi_insert_after (gsi, copy, GSI_NEW_STMT);
2942 /* Create new names for all the definitions created by COPY and
2943 add replacement mappings for each new name. */
2944 def_operand_p def_p;
2945 ssa_op_iter op_iter;
2946 FOR_EACH_SSA_DEF_OPERAND (def_p, copy, op_iter, SSA_OP_ALL_DEFS)
2948 tree old_name = DEF_FROM_PTR (def_p);
2949 tree new_name = create_new_def_for (old_name, copy, def_p);
2950 region->parameter_rename_map->put(old_name, new_name);
2953 update_stmt (copy);
2956 static void
2957 copy_internal_parameters (sese_info_p region, sese_info_p to_region)
2959 /* For all the parameters which definitino is in the if_region->false_region,
2960 insert code on true_region (if_region->true_region->entry). */
2962 int i;
2963 tree tr;
2964 gimple_stmt_iterator gsi = gsi_start_bb(to_region->region.entry->dest);
2966 FOR_EACH_VEC_ELT (region->params, i, tr)
2968 // If def is not in region.
2969 gimple *def_stmt = SSA_NAME_DEF_STMT (tr);
2970 if (def_stmt)
2971 copy_def (tr, def_stmt, region, to_region, &gsi);
2975 /* GIMPLE Loop Generator: generates loops in GIMPLE form for the given SCOP.
2976 Return true if code generation succeeded. */
2978 bool
2979 graphite_regenerate_ast_isl (scop_p scop)
2981 sese_info_p region = scop->scop_info;
2982 translate_isl_ast_to_gimple t (region);
2984 ifsese if_region = NULL;
2985 isl_ast_node *root_node;
2986 ivs_params ip;
2988 timevar_push (TV_GRAPHITE_CODE_GEN);
2989 t.add_parameters_to_ivs_params (scop, ip);
2990 root_node = t.scop_to_isl_ast (scop);
2992 if (dump_file && (dump_flags & TDF_DETAILS))
2994 fprintf (dump_file, "[scheduler] original schedule:\n");
2995 print_isl_schedule (dump_file, scop->original_schedule);
2996 fprintf (dump_file, "[scheduler] isl transformed schedule:\n");
2997 print_isl_schedule (dump_file, scop->transformed_schedule);
2999 fprintf (dump_file, "[scheduler] original ast:\n");
3000 print_schedule_ast (dump_file, scop->original_schedule, scop);
3001 fprintf (dump_file, "[scheduler] AST generated by isl:\n");
3002 print_isl_ast (dump_file, root_node);
3005 recompute_all_dominators ();
3006 graphite_verify ();
3008 if_region = move_sese_in_condition (region);
3009 region->if_region = if_region;
3010 recompute_all_dominators ();
3012 loop_p context_loop = region->region.entry->src->loop_father;
3014 /* Copy all the parameters which are defined in the region. */
3015 copy_internal_parameters(if_region->false_region, if_region->true_region);
3017 edge e = single_succ_edge (if_region->true_region->region.entry->dest);
3018 basic_block bb = split_edge (e);
3020 /* Update the true_region exit edge. */
3021 region->if_region->true_region->region.exit = single_succ_edge (bb);
3023 t.translate_isl_ast (context_loop, root_node, e, ip);
3024 if (t.codegen_error_p ())
3026 if (dump_file)
3027 fprintf (dump_file, "codegen error: "
3028 "reverting back to the original code.\n");
3029 set_ifsese_condition (if_region, integer_zero_node);
3031 else
3033 t.translate_pending_phi_nodes ();
3034 if (!t.codegen_error_p ())
3036 sese_insert_phis_for_liveouts (region,
3037 if_region->region->region.exit->src,
3038 if_region->false_region->region.exit,
3039 if_region->true_region->region.exit);
3040 mark_virtual_operands_for_renaming (cfun);
3041 update_ssa (TODO_update_ssa);
3044 graphite_verify ();
3045 scev_reset ();
3046 recompute_all_dominators ();
3047 graphite_verify ();
3049 if (dump_file)
3050 fprintf (dump_file, "[codegen] isl AST to Gimple succeeded.\n");
3052 else
3054 if (dump_file)
3055 fprintf (dump_file, "[codegen] unsuccessful in translating"
3056 " pending phis, reverting back to the original code.\n");
3057 set_ifsese_condition (if_region, integer_zero_node);
3061 free (if_region->true_region);
3062 free (if_region->region);
3063 free (if_region);
3065 ivs_params_clear (ip);
3066 isl_ast_node_free (root_node);
3067 timevar_pop (TV_GRAPHITE_CODE_GEN);
3069 if (dump_file && (dump_flags & TDF_DETAILS))
3071 loop_p loop;
3072 int num_no_dependency = 0;
3074 FOR_EACH_LOOP (loop, 0)
3075 if (loop->can_be_parallel)
3076 num_no_dependency++;
3078 fprintf (dump_file, "%d loops carried no dependency.\n",
3079 num_no_dependency);
3082 return !t.codegen_error_p ();
3085 #endif /* HAVE_isl */