graphite-isl-ast-to-gimple.c (set_codegen_error): With -fchecking and --param graphit...
[gcc.git] / gcc / graphite-isl-ast-to-gimple.c
1 /* Translation of isl AST to Gimple.
2 Copyright (C) 2014-2017 Free Software Foundation, Inc.
3 Contributed by Roman Gareev <gareevroman@gmail.com>.
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
21 #define USES_ISL
22
23 #include "config.h"
24
25 #ifdef HAVE_isl
26
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"
58
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. */
62
63 static int max_mode_int_precision =
64 GET_MODE_PRECISION (int_mode_for_size (MAX_FIXED_MODE_SIZE, 0).require ());
65 static int graphite_expression_type_precision = 128 <= max_mode_int_precision ?
66 128 : max_mode_int_precision;
67
68 struct ast_build_info
69 {
70 ast_build_info()
71 : is_parallelizable(false)
72 { }
73 bool is_parallelizable;
74 };
75
76 /* IVS_PARAMS maps isl's scattering and parameter identifiers
77 to corresponding trees. */
78
79 typedef std::map<isl_id *, tree> ivs_params;
80
81 /* Free all memory allocated for isl's identifiers. */
82
83 static void ivs_params_clear (ivs_params &ip)
84 {
85 std::map<isl_id *, tree>::iterator it;
86 for (it = ip.begin ();
87 it != ip.end (); it++)
88 {
89 isl_id_free (it->first);
90 }
91 }
92
93 /* Set the "separate" option for the schedule node. */
94
95 static isl_schedule_node *
96 set_separate_option (__isl_take isl_schedule_node *node, void *user)
97 {
98 if (user)
99 return node;
100
101 if (isl_schedule_node_get_type (node) != isl_schedule_node_band)
102 return node;
103
104 /* Set the "separate" option unless it is set earlier to another option. */
105 if (isl_schedule_node_band_member_get_ast_loop_type (node, 0)
106 == isl_ast_loop_default)
107 return isl_schedule_node_band_member_set_ast_loop_type
108 (node, 0, isl_ast_loop_separate);
109
110 return node;
111 }
112
113 /* Print SCHEDULE under an AST form on file F. */
114
115 void
116 print_schedule_ast (FILE *f, __isl_keep isl_schedule *schedule, scop_p scop)
117 {
118 isl_set *set = isl_set_params (isl_set_copy (scop->param_context));
119 isl_ast_build *context = isl_ast_build_from_context (set);
120 isl_ast_node *ast
121 = isl_ast_build_node_from_schedule (context, isl_schedule_copy (schedule));
122 isl_ast_build_free (context);
123 print_isl_ast (f, ast);
124 isl_ast_node_free (ast);
125 }
126
127 DEBUG_FUNCTION void
128 debug_schedule_ast (__isl_keep isl_schedule *s, scop_p scop)
129 {
130 print_schedule_ast (stderr, s, scop);
131 }
132
133 enum phi_node_kind
134 {
135 unknown_phi,
136 loop_phi,
137 close_phi,
138 cond_phi
139 };
140
141 class translate_isl_ast_to_gimple
142 {
143 public:
144 translate_isl_ast_to_gimple (sese_info_p r)
145 : region (r), codegen_error (false) { }
146 edge translate_isl_ast (loop_p context_loop, __isl_keep isl_ast_node *node,
147 edge next_e, ivs_params &ip);
148 edge translate_isl_ast_node_for (loop_p context_loop,
149 __isl_keep isl_ast_node *node,
150 edge next_e, ivs_params &ip);
151 edge translate_isl_ast_for_loop (loop_p context_loop,
152 __isl_keep isl_ast_node *node_for,
153 edge next_e,
154 tree type, tree lb, tree ub,
155 ivs_params &ip);
156 edge translate_isl_ast_node_if (loop_p context_loop,
157 __isl_keep isl_ast_node *node,
158 edge next_e, ivs_params &ip);
159 edge translate_isl_ast_node_user (__isl_keep isl_ast_node *node,
160 edge next_e, ivs_params &ip);
161 edge translate_isl_ast_node_block (loop_p context_loop,
162 __isl_keep isl_ast_node *node,
163 edge next_e, ivs_params &ip);
164 tree unary_op_to_tree (tree type, __isl_take isl_ast_expr *expr,
165 ivs_params &ip);
166 tree binary_op_to_tree (tree type, __isl_take isl_ast_expr *expr,
167 ivs_params &ip);
168 tree ternary_op_to_tree (tree type, __isl_take isl_ast_expr *expr,
169 ivs_params &ip);
170 tree nary_op_to_tree (tree type, __isl_take isl_ast_expr *expr,
171 ivs_params &ip);
172 tree gcc_expression_from_isl_expression (tree type,
173 __isl_take isl_ast_expr *,
174 ivs_params &ip);
175 tree gcc_expression_from_isl_ast_expr_id (tree type,
176 __isl_keep isl_ast_expr *expr_id,
177 ivs_params &ip);
178 tree gcc_expression_from_isl_expr_int (tree type,
179 __isl_take isl_ast_expr *expr);
180 tree gcc_expression_from_isl_expr_op (tree type,
181 __isl_take isl_ast_expr *expr,
182 ivs_params &ip);
183 struct loop *graphite_create_new_loop (edge entry_edge,
184 __isl_keep isl_ast_node *node_for,
185 loop_p outer, tree type,
186 tree lb, tree ub, ivs_params &ip);
187 edge graphite_create_new_guard (edge entry_edge,
188 __isl_take isl_ast_expr *if_cond,
189 ivs_params &ip);
190 void build_iv_mapping (vec<tree> iv_map, gimple_poly_bb_p gbb,
191 __isl_keep isl_ast_expr *user_expr, ivs_params &ip,
192 sese_l &region);
193 void translate_pending_phi_nodes (void);
194 void add_parameters_to_ivs_params (scop_p scop, ivs_params &ip);
195 __isl_give isl_ast_build *generate_isl_context (scop_p scop);
196
197 __isl_give isl_ast_node * scop_to_isl_ast (scop_p scop);
198
199 bool is_valid_rename (tree rename, basic_block def_bb, basic_block use_bb,
200 phi_node_kind, tree old_name, basic_block old_bb) const;
201 tree get_rename (basic_block new_bb, tree old_name,
202 basic_block old_bb, phi_node_kind) const;
203 tree get_rename_from_scev (tree old_name, gimple_seq *stmts, loop_p loop,
204 basic_block new_bb, basic_block old_bb,
205 vec<tree> iv_map);
206 basic_block get_def_bb_for_const (basic_block bb, basic_block old_bb) const;
207 tree get_new_name (basic_block new_bb, tree op,
208 basic_block old_bb, phi_node_kind) const;
209 void collect_all_ssa_names (tree new_expr, vec<tree> *vec_ssa);
210 bool copy_loop_phi_args (gphi *old_phi, init_back_edge_pair_t &ibp_old_bb,
211 gphi *new_phi, init_back_edge_pair_t &ibp_new_bb,
212 bool postpone);
213 bool copy_loop_phi_nodes (basic_block bb, basic_block new_bb);
214 bool add_close_phis_to_merge_points (gphi *old_phi, gphi *new_phi,
215 tree default_value);
216 tree add_close_phis_to_outer_loops (tree last_merge_name, edge merge_e,
217 gimple *old_close_phi);
218 bool copy_loop_close_phi_args (basic_block old_bb, basic_block new_bb,
219 vec<tree> iv_map, bool postpone);
220 bool copy_loop_close_phi_nodes (basic_block old_bb, basic_block new_bb,
221 vec<tree> iv_map);
222 bool copy_cond_phi_args (gphi *phi, gphi *new_phi, vec<tree> iv_map,
223 bool postpone);
224 bool copy_cond_phi_nodes (basic_block bb, basic_block new_bb,
225 vec<tree> iv_map);
226 bool graphite_copy_stmts_from_block (basic_block bb, basic_block new_bb,
227 vec<tree> iv_map);
228 edge copy_bb_and_scalar_dependences (basic_block bb, edge next_e,
229 vec<tree> iv_map);
230 edge edge_for_new_close_phis (basic_block bb);
231 bool add_phi_arg_for_new_expr (tree old_phi_args[2], tree new_phi_args[2],
232 edge old_bb_dominating_edge,
233 edge old_bb_non_dominating_edge,
234 gphi *phi, gphi *new_phi,
235 basic_block new_bb);
236 bool rename_uses (gimple *copy, gimple_stmt_iterator *gsi_tgt,
237 basic_block old_bb, loop_p loop, vec<tree> iv_map);
238 void set_rename (tree old_name, tree expr);
239 void set_rename_for_each_def (gimple *stmt);
240 void gsi_insert_earliest (gimple_seq seq);
241 tree rename_all_uses (tree new_expr, basic_block new_bb, basic_block old_bb);
242 bool codegen_error_p () const { return codegen_error; }
243
244 void set_codegen_error ()
245 {
246 codegen_error = true;
247 gcc_assert (! flag_checking
248 || PARAM_VALUE (PARAM_GRAPHITE_ALLOW_CODEGEN_ERRORS));
249 }
250
251 bool is_constant (tree op) const
252 {
253 return TREE_CODE (op) == INTEGER_CST
254 || TREE_CODE (op) == REAL_CST
255 || TREE_CODE (op) == COMPLEX_CST
256 || TREE_CODE (op) == VECTOR_CST;
257 }
258
259 private:
260 /* The region to be translated. */
261 sese_info_p region;
262
263 /* This flag is set when an error occurred during the translation of isl AST
264 to Gimple. */
265 bool codegen_error;
266
267 /* A vector of all the edges at if_condition merge points. */
268 auto_vec<edge, 2> merge_points;
269 };
270
271 /* Return the tree variable that corresponds to the given isl ast identifier
272 expression (an isl_ast_expr of type isl_ast_expr_id).
273
274 FIXME: We should replace blind conversion of id's type with derivation
275 of the optimal type when we get the corresponding isl support. Blindly
276 converting type sizes may be problematic when we switch to smaller
277 types. */
278
279 tree translate_isl_ast_to_gimple::
280 gcc_expression_from_isl_ast_expr_id (tree type,
281 __isl_take isl_ast_expr *expr_id,
282 ivs_params &ip)
283 {
284 gcc_assert (isl_ast_expr_get_type (expr_id) == isl_ast_expr_id);
285 isl_id *tmp_isl_id = isl_ast_expr_get_id (expr_id);
286 std::map<isl_id *, tree>::iterator res;
287 res = ip.find (tmp_isl_id);
288 isl_id_free (tmp_isl_id);
289 gcc_assert (res != ip.end () &&
290 "Could not map isl_id to tree expression");
291 isl_ast_expr_free (expr_id);
292 tree t = res->second;
293 tree *val = region->parameter_rename_map->get(t);
294
295 if (!val)
296 val = &t;
297 return fold_convert (type, *val);
298 }
299
300 /* Converts an isl_ast_expr_int expression E to a GCC expression tree of
301 type TYPE. */
302
303 tree translate_isl_ast_to_gimple::
304 gcc_expression_from_isl_expr_int (tree type, __isl_take isl_ast_expr *expr)
305 {
306 gcc_assert (isl_ast_expr_get_type (expr) == isl_ast_expr_int);
307 isl_val *val = isl_ast_expr_get_val (expr);
308 size_t n = isl_val_n_abs_num_chunks (val, sizeof (HOST_WIDE_INT));
309 HOST_WIDE_INT *chunks = XALLOCAVEC (HOST_WIDE_INT, n);
310 tree res;
311 if (isl_val_get_abs_num_chunks (val, sizeof (HOST_WIDE_INT), chunks) == -1)
312 res = NULL_TREE;
313 else
314 {
315 widest_int wi = widest_int::from_array (chunks, n, true);
316 if (isl_val_is_neg (val))
317 wi = -wi;
318 res = wide_int_to_tree (type, wi);
319 }
320 isl_val_free (val);
321 isl_ast_expr_free (expr);
322 return res;
323 }
324
325 /* Converts a binary isl_ast_expr_op expression E to a GCC expression tree of
326 type TYPE. */
327
328 tree translate_isl_ast_to_gimple::
329 binary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
330 {
331 isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0);
332 tree tree_lhs_expr = gcc_expression_from_isl_expression (type, arg_expr, ip);
333 arg_expr = isl_ast_expr_get_op_arg (expr, 1);
334 tree tree_rhs_expr = gcc_expression_from_isl_expression (type, arg_expr, ip);
335
336 enum isl_ast_op_type expr_type = isl_ast_expr_get_op_type (expr);
337 isl_ast_expr_free (expr);
338
339 if (codegen_error_p ())
340 return NULL_TREE;
341
342 switch (expr_type)
343 {
344 case isl_ast_op_add:
345 return fold_build2 (PLUS_EXPR, type, tree_lhs_expr, tree_rhs_expr);
346
347 case isl_ast_op_sub:
348 return fold_build2 (MINUS_EXPR, type, tree_lhs_expr, tree_rhs_expr);
349
350 case isl_ast_op_mul:
351 return fold_build2 (MULT_EXPR, type, tree_lhs_expr, tree_rhs_expr);
352
353 case isl_ast_op_div:
354 /* As isl operates on arbitrary precision numbers, we may end up with
355 division by 2^64 that is folded to 0. */
356 if (integer_zerop (tree_rhs_expr))
357 {
358 set_codegen_error ();
359 return NULL_TREE;
360 }
361 return fold_build2 (EXACT_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr);
362
363 case isl_ast_op_pdiv_q:
364 /* As isl operates on arbitrary precision numbers, we may end up with
365 division by 2^64 that is folded to 0. */
366 if (integer_zerop (tree_rhs_expr))
367 {
368 set_codegen_error ();
369 return NULL_TREE;
370 }
371 return fold_build2 (TRUNC_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr);
372
373 case isl_ast_op_zdiv_r:
374 case isl_ast_op_pdiv_r:
375 /* As isl operates on arbitrary precision numbers, we may end up with
376 division by 2^64 that is folded to 0. */
377 if (integer_zerop (tree_rhs_expr))
378 {
379 set_codegen_error ();
380 return NULL_TREE;
381 }
382 return fold_build2 (TRUNC_MOD_EXPR, type, tree_lhs_expr, tree_rhs_expr);
383
384 case isl_ast_op_fdiv_q:
385 /* As isl operates on arbitrary precision numbers, we may end up with
386 division by 2^64 that is folded to 0. */
387 if (integer_zerop (tree_rhs_expr))
388 {
389 set_codegen_error ();
390 return NULL_TREE;
391 }
392 return fold_build2 (FLOOR_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr);
393
394 case isl_ast_op_and:
395 return fold_build2 (TRUTH_ANDIF_EXPR, type,
396 tree_lhs_expr, tree_rhs_expr);
397
398 case isl_ast_op_or:
399 return fold_build2 (TRUTH_ORIF_EXPR, type, tree_lhs_expr, tree_rhs_expr);
400
401 case isl_ast_op_eq:
402 return fold_build2 (EQ_EXPR, type, tree_lhs_expr, tree_rhs_expr);
403
404 case isl_ast_op_le:
405 return fold_build2 (LE_EXPR, type, tree_lhs_expr, tree_rhs_expr);
406
407 case isl_ast_op_lt:
408 return fold_build2 (LT_EXPR, type, tree_lhs_expr, tree_rhs_expr);
409
410 case isl_ast_op_ge:
411 return fold_build2 (GE_EXPR, type, tree_lhs_expr, tree_rhs_expr);
412
413 case isl_ast_op_gt:
414 return fold_build2 (GT_EXPR, type, tree_lhs_expr, tree_rhs_expr);
415
416 default:
417 gcc_unreachable ();
418 }
419 }
420
421 /* Converts a ternary isl_ast_expr_op expression E to a GCC expression tree of
422 type TYPE. */
423
424 tree translate_isl_ast_to_gimple::
425 ternary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
426 {
427 enum isl_ast_op_type t = isl_ast_expr_get_op_type (expr);
428 gcc_assert (t == isl_ast_op_cond || t == isl_ast_op_select);
429 isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0);
430 tree a = gcc_expression_from_isl_expression (type, arg_expr, ip);
431 arg_expr = isl_ast_expr_get_op_arg (expr, 1);
432 tree b = gcc_expression_from_isl_expression (type, arg_expr, ip);
433 arg_expr = isl_ast_expr_get_op_arg (expr, 2);
434 tree c = gcc_expression_from_isl_expression (type, arg_expr, ip);
435 isl_ast_expr_free (expr);
436
437 if (codegen_error_p ())
438 return NULL_TREE;
439
440 return fold_build3 (COND_EXPR, type, a, b, c);
441 }
442
443 /* Converts a unary isl_ast_expr_op expression E to a GCC expression tree of
444 type TYPE. */
445
446 tree translate_isl_ast_to_gimple::
447 unary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
448 {
449 gcc_assert (isl_ast_expr_get_op_type (expr) == isl_ast_op_minus);
450 isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0);
451 tree tree_expr = gcc_expression_from_isl_expression (type, arg_expr, ip);
452 isl_ast_expr_free (expr);
453 return codegen_error_p () ? NULL_TREE
454 : fold_build1 (NEGATE_EXPR, type, tree_expr);
455 }
456
457 /* Converts an isl_ast_expr_op expression E with unknown number of arguments
458 to a GCC expression tree of type TYPE. */
459
460 tree translate_isl_ast_to_gimple::
461 nary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
462 {
463 enum tree_code op_code;
464 switch (isl_ast_expr_get_op_type (expr))
465 {
466 case isl_ast_op_max:
467 op_code = MAX_EXPR;
468 break;
469
470 case isl_ast_op_min:
471 op_code = MIN_EXPR;
472 break;
473
474 default:
475 gcc_unreachable ();
476 }
477 isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0);
478 tree res = gcc_expression_from_isl_expression (type, arg_expr, ip);
479
480 if (codegen_error_p ())
481 {
482 isl_ast_expr_free (expr);
483 return NULL_TREE;
484 }
485
486 int i;
487 for (i = 1; i < isl_ast_expr_get_op_n_arg (expr); i++)
488 {
489 arg_expr = isl_ast_expr_get_op_arg (expr, i);
490 tree t = gcc_expression_from_isl_expression (type, arg_expr, ip);
491
492 if (codegen_error_p ())
493 {
494 isl_ast_expr_free (expr);
495 return NULL_TREE;
496 }
497
498 res = fold_build2 (op_code, type, res, t);
499 }
500 isl_ast_expr_free (expr);
501 return res;
502 }
503
504 /* Converts an isl_ast_expr_op expression E to a GCC expression tree of
505 type TYPE. */
506
507 tree translate_isl_ast_to_gimple::
508 gcc_expression_from_isl_expr_op (tree type, __isl_take isl_ast_expr *expr,
509 ivs_params &ip)
510 {
511 if (codegen_error_p ())
512 {
513 isl_ast_expr_free (expr);
514 return NULL_TREE;
515 }
516
517 gcc_assert (isl_ast_expr_get_type (expr) == isl_ast_expr_op);
518 switch (isl_ast_expr_get_op_type (expr))
519 {
520 /* These isl ast expressions are not supported yet. */
521 case isl_ast_op_error:
522 case isl_ast_op_call:
523 case isl_ast_op_and_then:
524 case isl_ast_op_or_else:
525 gcc_unreachable ();
526
527 case isl_ast_op_max:
528 case isl_ast_op_min:
529 return nary_op_to_tree (type, expr, ip);
530
531 case isl_ast_op_add:
532 case isl_ast_op_sub:
533 case isl_ast_op_mul:
534 case isl_ast_op_div:
535 case isl_ast_op_pdiv_q:
536 case isl_ast_op_pdiv_r:
537 case isl_ast_op_fdiv_q:
538 case isl_ast_op_zdiv_r:
539 case isl_ast_op_and:
540 case isl_ast_op_or:
541 case isl_ast_op_eq:
542 case isl_ast_op_le:
543 case isl_ast_op_lt:
544 case isl_ast_op_ge:
545 case isl_ast_op_gt:
546 return binary_op_to_tree (type, expr, ip);
547
548 case isl_ast_op_minus:
549 return unary_op_to_tree (type, expr, ip);
550
551 case isl_ast_op_cond:
552 case isl_ast_op_select:
553 return ternary_op_to_tree (type, expr, ip);
554
555 default:
556 gcc_unreachable ();
557 }
558
559 return NULL_TREE;
560 }
561
562 /* Converts an isl AST expression E back to a GCC expression tree of
563 type TYPE. */
564
565 tree translate_isl_ast_to_gimple::
566 gcc_expression_from_isl_expression (tree type, __isl_take isl_ast_expr *expr,
567 ivs_params &ip)
568 {
569 if (codegen_error_p ())
570 {
571 isl_ast_expr_free (expr);
572 return NULL_TREE;
573 }
574
575 switch (isl_ast_expr_get_type (expr))
576 {
577 case isl_ast_expr_id:
578 return gcc_expression_from_isl_ast_expr_id (type, expr, ip);
579
580 case isl_ast_expr_int:
581 return gcc_expression_from_isl_expr_int (type, expr);
582
583 case isl_ast_expr_op:
584 return gcc_expression_from_isl_expr_op (type, expr, ip);
585
586 default:
587 gcc_unreachable ();
588 }
589
590 return NULL_TREE;
591 }
592
593 /* Creates a new LOOP corresponding to isl_ast_node_for. Inserts an
594 induction variable for the new LOOP. New LOOP is attached to CFG
595 starting at ENTRY_EDGE. LOOP is inserted into the loop tree and
596 becomes the child loop of the OUTER_LOOP. NEWIVS_INDEX binds
597 isl's scattering name to the induction variable created for the
598 loop of STMT. The new induction variable is inserted in the NEWIVS
599 vector and is of type TYPE. */
600
601 struct loop *translate_isl_ast_to_gimple::
602 graphite_create_new_loop (edge entry_edge, __isl_keep isl_ast_node *node_for,
603 loop_p outer, tree type, tree lb, tree ub,
604 ivs_params &ip)
605 {
606 isl_ast_expr *for_inc = isl_ast_node_for_get_inc (node_for);
607 tree stride = gcc_expression_from_isl_expression (type, for_inc, ip);
608
609 /* To fail code generation, we generate wrong code until we discard it. */
610 if (codegen_error_p ())
611 stride = integer_zero_node;
612
613 tree ivvar = create_tmp_var (type, "graphite_IV");
614 tree iv, iv_after_increment;
615 loop_p loop = create_empty_loop_on_edge
616 (entry_edge, lb, stride, ub, ivvar, &iv, &iv_after_increment,
617 outer ? outer : entry_edge->src->loop_father);
618
619 isl_ast_expr *for_iterator = isl_ast_node_for_get_iterator (node_for);
620 isl_id *id = isl_ast_expr_get_id (for_iterator);
621 std::map<isl_id *, tree>::iterator res;
622 res = ip.find (id);
623 if (ip.count (id))
624 isl_id_free (res->first);
625 ip[id] = iv;
626 isl_ast_expr_free (for_iterator);
627 return loop;
628 }
629
630 /* Create the loop for a isl_ast_node_for.
631
632 - NEXT_E is the edge where new generated code should be attached. */
633
634 edge translate_isl_ast_to_gimple::
635 translate_isl_ast_for_loop (loop_p context_loop,
636 __isl_keep isl_ast_node *node_for, edge next_e,
637 tree type, tree lb, tree ub,
638 ivs_params &ip)
639 {
640 gcc_assert (isl_ast_node_get_type (node_for) == isl_ast_node_for);
641 struct loop *loop = graphite_create_new_loop (next_e, node_for, context_loop,
642 type, lb, ub, ip);
643 edge last_e = single_exit (loop);
644 edge to_body = single_succ_edge (loop->header);
645 basic_block after = to_body->dest;
646
647 /* Translate the body of the loop. */
648 isl_ast_node *for_body = isl_ast_node_for_get_body (node_for);
649 next_e = translate_isl_ast (loop, for_body, to_body, ip);
650 isl_ast_node_free (for_body);
651
652 /* Early return if we failed to translate loop body. */
653 if (!next_e || codegen_error_p ())
654 return NULL;
655
656 if (next_e->dest != after)
657 redirect_edge_succ_nodup (next_e, after);
658 set_immediate_dominator (CDI_DOMINATORS, next_e->dest, next_e->src);
659
660 if (flag_loop_parallelize_all)
661 {
662 isl_id *id = isl_ast_node_get_annotation (node_for);
663 gcc_assert (id);
664 ast_build_info *for_info = (ast_build_info *) isl_id_get_user (id);
665 loop->can_be_parallel = for_info->is_parallelizable;
666 free (for_info);
667 isl_id_free (id);
668 }
669
670 return last_e;
671 }
672
673 /* We use this function to get the upper bound because of the form,
674 which is used by isl to represent loops:
675
676 for (iterator = init; cond; iterator += inc)
677
678 {
679
680 ...
681
682 }
683
684 The loop condition is an arbitrary expression, which contains the
685 current loop iterator.
686
687 (e.g. iterator + 3 < B && C > iterator + A)
688
689 We have to know the upper bound of the iterator to generate a loop
690 in Gimple form. It can be obtained from the special representation
691 of the loop condition, which is generated by isl,
692 if the ast_build_atomic_upper_bound option is set. In this case,
693 isl generates a loop condition that consists of the current loop
694 iterator, + an operator (< or <=) and an expression not involving
695 the iterator, which is processed and returned by this function.
696
697 (e.g iterator <= upper-bound-expression-without-iterator) */
698
699 static __isl_give isl_ast_expr *
700 get_upper_bound (__isl_keep isl_ast_node *node_for)
701 {
702 gcc_assert (isl_ast_node_get_type (node_for) == isl_ast_node_for);
703 isl_ast_expr *for_cond = isl_ast_node_for_get_cond (node_for);
704 gcc_assert (isl_ast_expr_get_type (for_cond) == isl_ast_expr_op);
705 isl_ast_expr *res;
706 switch (isl_ast_expr_get_op_type (for_cond))
707 {
708 case isl_ast_op_le:
709 res = isl_ast_expr_get_op_arg (for_cond, 1);
710 break;
711
712 case isl_ast_op_lt:
713 {
714 /* (iterator < ub) => (iterator <= ub - 1). */
715 isl_val *one =
716 isl_val_int_from_si (isl_ast_expr_get_ctx (for_cond), 1);
717 isl_ast_expr *ub = isl_ast_expr_get_op_arg (for_cond, 1);
718 res = isl_ast_expr_sub (ub, isl_ast_expr_from_val (one));
719 break;
720 }
721
722 default:
723 gcc_unreachable ();
724 }
725 isl_ast_expr_free (for_cond);
726 return res;
727 }
728
729 /* Translates an isl_ast_node_for to Gimple. */
730
731 edge translate_isl_ast_to_gimple::
732 translate_isl_ast_node_for (loop_p context_loop, __isl_keep isl_ast_node *node,
733 edge next_e, ivs_params &ip)
734 {
735 gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_for);
736 tree type
737 = build_nonstandard_integer_type (graphite_expression_type_precision, 0);
738
739 isl_ast_expr *for_init = isl_ast_node_for_get_init (node);
740 tree lb = gcc_expression_from_isl_expression (type, for_init, ip);
741 /* To fail code generation, we generate wrong code until we discard it. */
742 if (codegen_error_p ())
743 lb = integer_zero_node;
744
745 isl_ast_expr *upper_bound = get_upper_bound (node);
746 tree ub = gcc_expression_from_isl_expression (type, upper_bound, ip);
747 /* To fail code generation, we generate wrong code until we discard it. */
748 if (codegen_error_p ())
749 ub = integer_zero_node;
750
751 edge last_e = single_succ_edge (split_edge (next_e));
752 translate_isl_ast_for_loop (context_loop, node, next_e,
753 type, lb, ub, ip);
754 return last_e;
755 }
756
757 /* Inserts in iv_map a tuple (OLD_LOOP->num, NEW_NAME) for the induction
758 variables of the loops around GBB in SESE.
759
760 FIXME: Instead of using a vec<tree> that maps each loop id to a possible
761 chrec, we could consider using a map<int, tree> that maps loop ids to the
762 corresponding tree expressions. */
763
764 void translate_isl_ast_to_gimple::
765 build_iv_mapping (vec<tree> iv_map, gimple_poly_bb_p gbb,
766 __isl_keep isl_ast_expr *user_expr, ivs_params &ip,
767 sese_l &region)
768 {
769 gcc_assert (isl_ast_expr_get_type (user_expr) == isl_ast_expr_op &&
770 isl_ast_expr_get_op_type (user_expr) == isl_ast_op_call);
771 int i;
772 isl_ast_expr *arg_expr;
773 for (i = 1; i < isl_ast_expr_get_op_n_arg (user_expr); i++)
774 {
775 arg_expr = isl_ast_expr_get_op_arg (user_expr, i);
776 tree type =
777 build_nonstandard_integer_type (graphite_expression_type_precision, 0);
778 tree t = gcc_expression_from_isl_expression (type, arg_expr, ip);
779
780 /* To fail code generation, we generate wrong code until we discard it. */
781 if (codegen_error_p ())
782 t = integer_zero_node;
783
784 loop_p old_loop = gbb_loop_at_index (gbb, region, i - 2);
785 /* Record sth only for real loops. */
786 if (loop_in_sese_p (old_loop, region))
787 iv_map[old_loop->num] = t;
788 }
789 }
790
791 /* Translates an isl_ast_node_user to Gimple.
792
793 FIXME: We should remove iv_map.create (loop->num + 1), if it is possible. */
794
795 edge translate_isl_ast_to_gimple::
796 translate_isl_ast_node_user (__isl_keep isl_ast_node *node,
797 edge next_e, ivs_params &ip)
798 {
799 gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_user);
800
801 isl_ast_expr *user_expr = isl_ast_node_user_get_expr (node);
802 isl_ast_expr *name_expr = isl_ast_expr_get_op_arg (user_expr, 0);
803 gcc_assert (isl_ast_expr_get_type (name_expr) == isl_ast_expr_id);
804
805 isl_id *name_id = isl_ast_expr_get_id (name_expr);
806 poly_bb_p pbb = (poly_bb_p) isl_id_get_user (name_id);
807 gcc_assert (pbb);
808
809 gimple_poly_bb_p gbb = PBB_BLACK_BOX (pbb);
810
811 isl_ast_expr_free (name_expr);
812 isl_id_free (name_id);
813
814 gcc_assert (GBB_BB (gbb) != ENTRY_BLOCK_PTR_FOR_FN (cfun) &&
815 "The entry block should not even appear within a scop");
816
817 const int nb_loops = number_of_loops (cfun);
818 vec<tree> iv_map;
819 iv_map.create (nb_loops);
820 iv_map.safe_grow_cleared (nb_loops);
821
822 build_iv_mapping (iv_map, gbb, user_expr, ip, pbb->scop->scop_info->region);
823 isl_ast_expr_free (user_expr);
824
825 basic_block old_bb = GBB_BB (gbb);
826 if (dump_file)
827 {
828 fprintf (dump_file,
829 "[codegen] copying from bb_%d on edge (bb_%d, bb_%d)\n",
830 old_bb->index, next_e->src->index, next_e->dest->index);
831 print_loops_bb (dump_file, GBB_BB (gbb), 0, 3);
832
833 }
834
835 next_e = copy_bb_and_scalar_dependences (old_bb, next_e, iv_map);
836
837 iv_map.release ();
838
839 if (codegen_error_p ())
840 return NULL;
841
842 if (dump_file)
843 {
844 fprintf (dump_file, "[codegen] (after copy) new basic block\n");
845 print_loops_bb (dump_file, next_e->src, 0, 3);
846 }
847
848 return next_e;
849 }
850
851 /* Translates an isl_ast_node_block to Gimple. */
852
853 edge translate_isl_ast_to_gimple::
854 translate_isl_ast_node_block (loop_p context_loop,
855 __isl_keep isl_ast_node *node,
856 edge next_e, ivs_params &ip)
857 {
858 gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_block);
859 isl_ast_node_list *node_list = isl_ast_node_block_get_children (node);
860 int i;
861 for (i = 0; i < isl_ast_node_list_n_ast_node (node_list); i++)
862 {
863 isl_ast_node *tmp_node = isl_ast_node_list_get_ast_node (node_list, i);
864 next_e = translate_isl_ast (context_loop, tmp_node, next_e, ip);
865 isl_ast_node_free (tmp_node);
866 }
867 isl_ast_node_list_free (node_list);
868 return next_e;
869 }
870
871 /* Creates a new if region corresponding to isl's cond. */
872
873 edge translate_isl_ast_to_gimple::
874 graphite_create_new_guard (edge entry_edge, __isl_take isl_ast_expr *if_cond,
875 ivs_params &ip)
876 {
877 tree type =
878 build_nonstandard_integer_type (graphite_expression_type_precision, 0);
879 tree cond_expr = gcc_expression_from_isl_expression (type, if_cond, ip);
880
881 /* To fail code generation, we generate wrong code until we discard it. */
882 if (codegen_error_p ())
883 cond_expr = integer_zero_node;
884
885 edge exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr);
886 return exit_edge;
887 }
888
889 /* Translates an isl_ast_node_if to Gimple. */
890
891 edge translate_isl_ast_to_gimple::
892 translate_isl_ast_node_if (loop_p context_loop,
893 __isl_keep isl_ast_node *node,
894 edge next_e, ivs_params &ip)
895 {
896 gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_if);
897 isl_ast_expr *if_cond = isl_ast_node_if_get_cond (node);
898 edge last_e = graphite_create_new_guard (next_e, if_cond, ip);
899 edge true_e = get_true_edge_from_guard_bb (next_e->dest);
900 merge_points.safe_push (last_e);
901
902 isl_ast_node *then_node = isl_ast_node_if_get_then (node);
903 translate_isl_ast (context_loop, then_node, true_e, ip);
904 isl_ast_node_free (then_node);
905
906 edge false_e = get_false_edge_from_guard_bb (next_e->dest);
907 isl_ast_node *else_node = isl_ast_node_if_get_else (node);
908 if (isl_ast_node_get_type (else_node) != isl_ast_node_error)
909 translate_isl_ast (context_loop, else_node, false_e, ip);
910
911 isl_ast_node_free (else_node);
912 return last_e;
913 }
914
915 /* Translates an isl AST node NODE to GCC representation in the
916 context of a SESE. */
917
918 edge translate_isl_ast_to_gimple::
919 translate_isl_ast (loop_p context_loop, __isl_keep isl_ast_node *node,
920 edge next_e, ivs_params &ip)
921 {
922 if (codegen_error_p ())
923 return NULL;
924
925 switch (isl_ast_node_get_type (node))
926 {
927 case isl_ast_node_error:
928 gcc_unreachable ();
929
930 case isl_ast_node_for:
931 return translate_isl_ast_node_for (context_loop, node,
932 next_e, ip);
933
934 case isl_ast_node_if:
935 return translate_isl_ast_node_if (context_loop, node,
936 next_e, ip);
937
938 case isl_ast_node_user:
939 return translate_isl_ast_node_user (node, next_e, ip);
940
941 case isl_ast_node_block:
942 return translate_isl_ast_node_block (context_loop, node,
943 next_e, ip);
944
945 case isl_ast_node_mark:
946 {
947 isl_ast_node *n = isl_ast_node_mark_get_node (node);
948 edge e = translate_isl_ast (context_loop, n, next_e, ip);
949 isl_ast_node_free (n);
950 return e;
951 }
952
953 default:
954 gcc_unreachable ();
955 }
956 }
957
958 /* Return true when BB contains loop close phi nodes. A loop close phi node is
959 at the exit of loop which takes one argument that is the last value of the
960 variable being used out of the loop. */
961
962 static bool
963 bb_contains_loop_close_phi_nodes (basic_block bb)
964 {
965 return single_pred_p (bb)
966 && bb->loop_father != single_pred_edge (bb)->src->loop_father;
967 }
968
969 /* Return true when BB contains loop phi nodes. A loop phi node is the loop
970 header containing phi nodes which has one init-edge and one back-edge. */
971
972 static bool
973 bb_contains_loop_phi_nodes (basic_block bb)
974 {
975 if (EDGE_COUNT (bb->preds) != 2)
976 return false;
977
978 unsigned depth = loop_depth (bb->loop_father);
979
980 edge preds[2] = { (*bb->preds)[0], (*bb->preds)[1] };
981
982 if (depth > loop_depth (preds[0]->src->loop_father)
983 || depth > loop_depth (preds[1]->src->loop_father))
984 return true;
985
986 /* When one of the edges correspond to the same loop father and other
987 doesn't. */
988 if (bb->loop_father != preds[0]->src->loop_father
989 && bb->loop_father == preds[1]->src->loop_father)
990 return true;
991
992 if (bb->loop_father != preds[1]->src->loop_father
993 && bb->loop_father == preds[0]->src->loop_father)
994 return true;
995
996 return false;
997 }
998
999 /* Check if USE is defined in a basic block from where the definition of USE can
1000 propagate from all the paths. FIXME: Verify checks for virtual operands. */
1001
1002 static bool
1003 is_loop_closed_ssa_use (basic_block bb, tree use)
1004 {
1005 if (TREE_CODE (use) != SSA_NAME || virtual_operand_p (use))
1006 return true;
1007
1008 /* For close-phi nodes def always comes from a loop which has a back-edge. */
1009 if (bb_contains_loop_close_phi_nodes (bb))
1010 return true;
1011
1012 gimple *def = SSA_NAME_DEF_STMT (use);
1013 basic_block def_bb = gimple_bb (def);
1014 return (!def_bb
1015 || flow_bb_inside_loop_p (def_bb->loop_father, bb));
1016 }
1017
1018 /* Return the number of phi nodes in BB. */
1019
1020 static int
1021 number_of_phi_nodes (basic_block bb)
1022 {
1023 int num_phis = 0;
1024 for (gphi_iterator psi = gsi_start_phis (bb); !gsi_end_p (psi);
1025 gsi_next (&psi))
1026 num_phis++;
1027 return num_phis;
1028 }
1029
1030 /* Returns true if BB uses name in one of its PHIs. */
1031
1032 static bool
1033 phi_uses_name (basic_block bb, tree name)
1034 {
1035 for (gphi_iterator psi = gsi_start_phis (bb); !gsi_end_p (psi);
1036 gsi_next (&psi))
1037 {
1038 gphi *phi = psi.phi ();
1039 for (unsigned i = 0; i < gimple_phi_num_args (phi); i++)
1040 {
1041 tree use_arg = gimple_phi_arg_def (phi, i);
1042 if (use_arg == name)
1043 return true;
1044 }
1045 }
1046 return false;
1047 }
1048
1049 /* Return true if RENAME (defined in BB) is a valid use in NEW_BB. The
1050 definition should flow into use, and the use should respect the loop-closed
1051 SSA form. */
1052
1053 bool translate_isl_ast_to_gimple::
1054 is_valid_rename (tree rename, basic_block def_bb, basic_block use_bb,
1055 phi_node_kind phi_kind, tree old_name, basic_block old_bb) const
1056 {
1057 if (SSA_NAME_IS_DEFAULT_DEF (rename))
1058 return true;
1059
1060 /* The def of the rename must either dominate the uses or come from a
1061 back-edge. Also the def must respect the loop closed ssa form. */
1062 if (!is_loop_closed_ssa_use (use_bb, rename))
1063 {
1064 if (dump_file)
1065 {
1066 fprintf (dump_file, "[codegen] rename not in loop closed ssa: ");
1067 print_generic_expr (dump_file, rename);
1068 fprintf (dump_file, "\n");
1069 }
1070 return false;
1071 }
1072
1073 if (dominated_by_p (CDI_DOMINATORS, use_bb, def_bb))
1074 return true;
1075
1076 if (bb_contains_loop_phi_nodes (use_bb) && phi_kind == loop_phi)
1077 {
1078 /* The loop-header dominates the loop-body. */
1079 if (!dominated_by_p (CDI_DOMINATORS, def_bb, use_bb))
1080 return false;
1081
1082 /* RENAME would be used in loop-phi. */
1083 gcc_assert (number_of_phi_nodes (use_bb));
1084
1085 /* For definitions coming from back edges, we should check that
1086 old_name is used in a loop PHI node.
1087 FIXME: Verify if this is true. */
1088 if (phi_uses_name (old_bb, old_name))
1089 return true;
1090 }
1091 return false;
1092 }
1093
1094 /* Returns the expression associated to OLD_NAME (which is used in OLD_BB), in
1095 NEW_BB from RENAME_MAP. PHI_KIND determines the kind of phi node. */
1096
1097 tree translate_isl_ast_to_gimple::
1098 get_rename (basic_block new_bb, tree old_name, basic_block old_bb,
1099 phi_node_kind phi_kind) const
1100 {
1101 gcc_assert (TREE_CODE (old_name) == SSA_NAME);
1102 vec <tree> *renames = region->rename_map->get (old_name);
1103
1104 if (!renames || renames->is_empty ())
1105 return NULL_TREE;
1106
1107 if (1 == renames->length ())
1108 {
1109 tree rename = (*renames)[0];
1110 if (TREE_CODE (rename) == SSA_NAME)
1111 {
1112 basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (rename));
1113 if (is_valid_rename (rename, bb, new_bb, phi_kind, old_name, old_bb)
1114 && (phi_kind == close_phi
1115 || ! bb
1116 || flow_bb_inside_loop_p (bb->loop_father, new_bb)))
1117 return rename;
1118 return NULL_TREE;
1119 }
1120
1121 if (is_constant (rename))
1122 return rename;
1123
1124 return NULL_TREE;
1125 }
1126
1127 /* More than one renames corresponding to the old_name. Find the rename for
1128 which the definition flows into usage at new_bb. */
1129 int i;
1130 tree t1 = NULL_TREE, t2;
1131 basic_block t1_bb = NULL;
1132 FOR_EACH_VEC_ELT (*renames, i, t2)
1133 {
1134 basic_block t2_bb = gimple_bb (SSA_NAME_DEF_STMT (t2));
1135
1136 /* Defined in the same basic block as used. */
1137 if (t2_bb == new_bb)
1138 return t2;
1139
1140 /* NEW_BB and T2_BB are in two unrelated if-clauses. */
1141 if (!dominated_by_p (CDI_DOMINATORS, new_bb, t2_bb))
1142 continue;
1143
1144 if (!flow_bb_inside_loop_p (t2_bb->loop_father, new_bb))
1145 continue;
1146
1147 /* Compute the nearest dominator. */
1148 if (!t1 || dominated_by_p (CDI_DOMINATORS, t2_bb, t1_bb))
1149 {
1150 t1_bb = t2_bb;
1151 t1 = t2;
1152 }
1153 }
1154
1155 return t1;
1156 }
1157
1158 /* Register in RENAME_MAP the rename tuple (OLD_NAME, EXPR).
1159 When OLD_NAME and EXPR are the same we assert. */
1160
1161 void translate_isl_ast_to_gimple::
1162 set_rename (tree old_name, tree expr)
1163 {
1164 if (dump_file)
1165 {
1166 fprintf (dump_file, "[codegen] setting rename: old_name = ");
1167 print_generic_expr (dump_file, old_name);
1168 fprintf (dump_file, ", new_name = ");
1169 print_generic_expr (dump_file, expr);
1170 fprintf (dump_file, "\n");
1171 }
1172
1173 if (old_name == expr)
1174 return;
1175
1176 vec <tree> *renames = region->rename_map->get (old_name);
1177
1178 if (renames)
1179 renames->safe_push (expr);
1180 else
1181 {
1182 vec<tree> r;
1183 r.create (2);
1184 r.safe_push (expr);
1185 region->rename_map->put (old_name, r);
1186 }
1187
1188 tree t;
1189 int i;
1190 /* For a parameter of a scop we don't want to rename it. */
1191 FOR_EACH_VEC_ELT (region->params, i, t)
1192 if (old_name == t)
1193 region->parameter_rename_map->put(old_name, expr);
1194 }
1195
1196 /* Return an iterator to the instructions comes last in the execution order.
1197 Either GSI1 and GSI2 should belong to the same basic block or one of their
1198 respective basic blocks should dominate the other. */
1199
1200 gimple_stmt_iterator
1201 later_of_the_two (gimple_stmt_iterator gsi1, gimple_stmt_iterator gsi2)
1202 {
1203 basic_block bb1 = gsi_bb (gsi1);
1204 basic_block bb2 = gsi_bb (gsi2);
1205
1206 /* Find the iterator which is the latest. */
1207 if (bb1 == bb2)
1208 {
1209 gimple *stmt1 = gsi_stmt (gsi1);
1210 gimple *stmt2 = gsi_stmt (gsi2);
1211
1212 if (stmt1 != NULL && stmt2 != NULL)
1213 {
1214 bool is_phi1 = gimple_code (stmt1) == GIMPLE_PHI;
1215 bool is_phi2 = gimple_code (stmt2) == GIMPLE_PHI;
1216
1217 if (is_phi1 != is_phi2)
1218 return is_phi1 ? gsi2 : gsi1;
1219 }
1220
1221 /* For empty basic blocks gsis point to the end of the sequence. Since
1222 there is no operator== defined for gimple_stmt_iterator and for gsis
1223 not pointing to a valid statement gsi_next would assert. */
1224 gimple_stmt_iterator gsi = gsi1;
1225 do {
1226 if (gsi_stmt (gsi) == gsi_stmt (gsi2))
1227 return gsi2;
1228 gsi_next (&gsi);
1229 } while (!gsi_end_p (gsi));
1230
1231 return gsi1;
1232 }
1233
1234 /* Find the basic block closest to the basic block which defines stmt. */
1235 if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
1236 return gsi1;
1237
1238 gcc_assert (dominated_by_p (CDI_DOMINATORS, bb2, bb1));
1239 return gsi2;
1240 }
1241
1242 /* Insert each statement from SEQ at its earliest insertion p. */
1243
1244 void translate_isl_ast_to_gimple::
1245 gsi_insert_earliest (gimple_seq seq)
1246 {
1247 update_modified_stmts (seq);
1248 sese_l &codegen_region = region->if_region->true_region->region;
1249 basic_block begin_bb = get_entry_bb (codegen_region);
1250
1251 /* Inserting the gimple statements in a vector because gimple_seq behave
1252 in strage ways when inserting the stmts from it into different basic
1253 blocks one at a time. */
1254 auto_vec<gimple *, 3> stmts;
1255 for (gimple_stmt_iterator gsi = gsi_start (seq); !gsi_end_p (gsi);
1256 gsi_next (&gsi))
1257 stmts.safe_push (gsi_stmt (gsi));
1258
1259 int i;
1260 gimple *use_stmt;
1261 FOR_EACH_VEC_ELT (stmts, i, use_stmt)
1262 {
1263 gcc_assert (gimple_code (use_stmt) != GIMPLE_PHI);
1264 gimple_stmt_iterator gsi_def_stmt = gsi_start_bb_nondebug (begin_bb);
1265
1266 use_operand_p use_p;
1267 ssa_op_iter op_iter;
1268 FOR_EACH_SSA_USE_OPERAND (use_p, use_stmt, op_iter, SSA_OP_USE)
1269 {
1270 /* Iterator to the current def of use_p. For function parameters or
1271 anything where def is not found, insert at the beginning of the
1272 generated region. */
1273 gimple_stmt_iterator gsi_stmt = gsi_def_stmt;
1274
1275 tree op = USE_FROM_PTR (use_p);
1276 gimple *stmt = SSA_NAME_DEF_STMT (op);
1277 if (stmt && (gimple_code (stmt) != GIMPLE_NOP))
1278 gsi_stmt = gsi_for_stmt (stmt);
1279
1280 /* For region parameters, insert at the beginning of the generated
1281 region. */
1282 if (!bb_in_sese_p (gsi_bb (gsi_stmt), codegen_region))
1283 gsi_stmt = gsi_def_stmt;
1284
1285 gsi_def_stmt = later_of_the_two (gsi_stmt, gsi_def_stmt);
1286 }
1287
1288 if (!gsi_stmt (gsi_def_stmt))
1289 {
1290 gimple_stmt_iterator gsi = gsi_after_labels (gsi_bb (gsi_def_stmt));
1291 gsi_insert_before (&gsi, use_stmt, GSI_NEW_STMT);
1292 }
1293 else if (gimple_code (gsi_stmt (gsi_def_stmt)) == GIMPLE_PHI)
1294 {
1295 gimple_stmt_iterator bsi
1296 = gsi_start_bb_nondebug (gsi_bb (gsi_def_stmt));
1297 /* Insert right after the PHI statements. */
1298 gsi_insert_before (&bsi, use_stmt, GSI_NEW_STMT);
1299 }
1300 else
1301 gsi_insert_after (&gsi_def_stmt, use_stmt, GSI_NEW_STMT);
1302
1303 if (dump_file)
1304 {
1305 fprintf (dump_file, "[codegen] inserting statement: ");
1306 print_gimple_stmt (dump_file, use_stmt, 0, TDF_VOPS | TDF_MEMSYMS);
1307 print_loops_bb (dump_file, gimple_bb (use_stmt), 0, 3);
1308 }
1309 }
1310 }
1311
1312 /* Collect all the operands of NEW_EXPR by recursively visiting each
1313 operand. */
1314
1315 void translate_isl_ast_to_gimple::
1316 collect_all_ssa_names (tree new_expr, vec<tree> *vec_ssa)
1317 {
1318 if (new_expr == NULL_TREE)
1319 return;
1320
1321 /* Rename all uses in new_expr. */
1322 if (TREE_CODE (new_expr) == SSA_NAME)
1323 {
1324 vec_ssa->safe_push (new_expr);
1325 return;
1326 }
1327
1328 /* Iterate over SSA_NAMES in NEW_EXPR. */
1329 for (int i = 0; i < (TREE_CODE_LENGTH (TREE_CODE (new_expr))); i++)
1330 {
1331 tree op = TREE_OPERAND (new_expr, i);
1332 collect_all_ssa_names (op, vec_ssa);
1333 }
1334 }
1335
1336 /* This is abridged version of the function copied from:
1337 tree.c:substitute_in_expr (tree exp, tree f, tree r). */
1338
1339 static tree
1340 substitute_ssa_name (tree exp, tree f, tree r)
1341 {
1342 enum tree_code code = TREE_CODE (exp);
1343 tree op0, op1, op2, op3;
1344 tree new_tree;
1345
1346 /* We handle TREE_LIST and COMPONENT_REF separately. */
1347 if (code == TREE_LIST)
1348 {
1349 op0 = substitute_ssa_name (TREE_CHAIN (exp), f, r);
1350 op1 = substitute_ssa_name (TREE_VALUE (exp), f, r);
1351 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
1352 return exp;
1353
1354 return tree_cons (TREE_PURPOSE (exp), op1, op0);
1355 }
1356 else if (code == COMPONENT_REF)
1357 {
1358 tree inner;
1359
1360 /* If this expression is getting a value from a PLACEHOLDER_EXPR
1361 and it is the right field, replace it with R. */
1362 for (inner = TREE_OPERAND (exp, 0);
1363 REFERENCE_CLASS_P (inner);
1364 inner = TREE_OPERAND (inner, 0))
1365 ;
1366
1367 /* The field. */
1368 op1 = TREE_OPERAND (exp, 1);
1369
1370 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && op1 == f)
1371 return r;
1372
1373 /* If this expression hasn't been completed let, leave it alone. */
1374 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && !TREE_TYPE (inner))
1375 return exp;
1376
1377 op0 = substitute_ssa_name (TREE_OPERAND (exp, 0), f, r);
1378 if (op0 == TREE_OPERAND (exp, 0))
1379 return exp;
1380
1381 new_tree
1382 = fold_build3 (COMPONENT_REF, TREE_TYPE (exp), op0, op1, NULL_TREE);
1383 }
1384 else
1385 switch (TREE_CODE_CLASS (code))
1386 {
1387 case tcc_constant:
1388 return exp;
1389
1390 case tcc_declaration:
1391 if (exp == f)
1392 return r;
1393 else
1394 return exp;
1395
1396 case tcc_expression:
1397 if (exp == f)
1398 return r;
1399
1400 /* Fall through. */
1401
1402 case tcc_exceptional:
1403 case tcc_unary:
1404 case tcc_binary:
1405 case tcc_comparison:
1406 case tcc_reference:
1407 switch (TREE_CODE_LENGTH (code))
1408 {
1409 case 0:
1410 if (exp == f)
1411 return r;
1412 return exp;
1413
1414 case 1:
1415 op0 = substitute_ssa_name (TREE_OPERAND (exp, 0), f, r);
1416 if (op0 == TREE_OPERAND (exp, 0))
1417 return exp;
1418
1419 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
1420 break;
1421
1422 case 2:
1423 op0 = substitute_ssa_name (TREE_OPERAND (exp, 0), f, r);
1424 op1 = substitute_ssa_name (TREE_OPERAND (exp, 1), f, r);
1425
1426 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
1427 return exp;
1428
1429 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
1430 break;
1431
1432 case 3:
1433 op0 = substitute_ssa_name (TREE_OPERAND (exp, 0), f, r);
1434 op1 = substitute_ssa_name (TREE_OPERAND (exp, 1), f, r);
1435 op2 = substitute_ssa_name (TREE_OPERAND (exp, 2), f, r);
1436
1437 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
1438 && op2 == TREE_OPERAND (exp, 2))
1439 return exp;
1440
1441 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
1442 break;
1443
1444 case 4:
1445 op0 = substitute_ssa_name (TREE_OPERAND (exp, 0), f, r);
1446 op1 = substitute_ssa_name (TREE_OPERAND (exp, 1), f, r);
1447 op2 = substitute_ssa_name (TREE_OPERAND (exp, 2), f, r);
1448 op3 = substitute_ssa_name (TREE_OPERAND (exp, 3), f, r);
1449
1450 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
1451 && op2 == TREE_OPERAND (exp, 2)
1452 && op3 == TREE_OPERAND (exp, 3))
1453 return exp;
1454
1455 new_tree
1456 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
1457 break;
1458
1459 default:
1460 gcc_unreachable ();
1461 }
1462 break;
1463
1464 case tcc_vl_exp:
1465 default:
1466 gcc_unreachable ();
1467 }
1468
1469 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
1470
1471 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
1472 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
1473
1474 return new_tree;
1475 }
1476
1477 /* Rename all the operands of NEW_EXPR by recursively visiting each operand. */
1478
1479 tree translate_isl_ast_to_gimple::
1480 rename_all_uses (tree new_expr, basic_block new_bb, basic_block old_bb)
1481 {
1482 auto_vec<tree, 2> ssa_names;
1483 collect_all_ssa_names (new_expr, &ssa_names);
1484 tree t;
1485 int i;
1486 FOR_EACH_VEC_ELT (ssa_names, i, t)
1487 if (tree r = get_rename (new_bb, t, old_bb, unknown_phi))
1488 new_expr = substitute_ssa_name (new_expr, t, r);
1489
1490 return new_expr;
1491 }
1492
1493 /* For ops which are scev_analyzeable, we can regenerate a new name from its
1494 scalar evolution around LOOP. */
1495
1496 tree translate_isl_ast_to_gimple::
1497 get_rename_from_scev (tree old_name, gimple_seq *stmts, loop_p loop,
1498 basic_block new_bb, basic_block old_bb,
1499 vec<tree> iv_map)
1500 {
1501 tree scev = scalar_evolution_in_region (region->region, loop, old_name);
1502
1503 /* At this point we should know the exact scev for each
1504 scalar SSA_NAME used in the scop: all the other scalar
1505 SSA_NAMEs should have been translated out of SSA using
1506 arrays with one element. */
1507 tree new_expr;
1508 if (chrec_contains_undetermined (scev))
1509 {
1510 set_codegen_error ();
1511 return build_zero_cst (TREE_TYPE (old_name));
1512 }
1513
1514 new_expr = chrec_apply_map (scev, iv_map);
1515
1516 /* The apply should produce an expression tree containing
1517 the uses of the new induction variables. We should be
1518 able to use new_expr instead of the old_name in the newly
1519 generated loop nest. */
1520 if (chrec_contains_undetermined (new_expr)
1521 || tree_contains_chrecs (new_expr, NULL))
1522 {
1523 set_codegen_error ();
1524 return build_zero_cst (TREE_TYPE (old_name));
1525 }
1526
1527 if (TREE_CODE (new_expr) == SSA_NAME)
1528 {
1529 basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (new_expr));
1530 if (bb && !dominated_by_p (CDI_DOMINATORS, new_bb, bb))
1531 {
1532 set_codegen_error ();
1533 return build_zero_cst (TREE_TYPE (old_name));
1534 }
1535 }
1536
1537 new_expr = rename_all_uses (new_expr, new_bb, old_bb);
1538
1539 /* We check all the operands and all of them should dominate the use at
1540 new_expr. */
1541 auto_vec <tree, 2> new_ssa_names;
1542 collect_all_ssa_names (new_expr, &new_ssa_names);
1543 int i;
1544 tree new_ssa_name;
1545 FOR_EACH_VEC_ELT (new_ssa_names, i, new_ssa_name)
1546 {
1547 if (TREE_CODE (new_ssa_name) == SSA_NAME)
1548 {
1549 basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (new_ssa_name));
1550 if (bb && !dominated_by_p (CDI_DOMINATORS, new_bb, bb))
1551 {
1552 set_codegen_error ();
1553 return build_zero_cst (TREE_TYPE (old_name));
1554 }
1555 }
1556 }
1557
1558 /* Replace the old_name with the new_expr. */
1559 return force_gimple_operand (unshare_expr (new_expr), stmts,
1560 true, NULL_TREE);
1561 }
1562
1563 /* Renames the scalar uses of the statement COPY, using the
1564 substitution map RENAME_MAP, inserting the gimplification code at
1565 GSI_TGT, for the translation REGION, with the original copied
1566 statement in LOOP, and using the induction variable renaming map
1567 IV_MAP. Returns true when something has been renamed. */
1568
1569 bool translate_isl_ast_to_gimple::
1570 rename_uses (gimple *copy, gimple_stmt_iterator *gsi_tgt, basic_block old_bb,
1571 loop_p loop, vec<tree> iv_map)
1572 {
1573 bool changed = false;
1574
1575 if (is_gimple_debug (copy))
1576 {
1577 if (gimple_debug_bind_p (copy))
1578 gimple_debug_bind_reset_value (copy);
1579 else if (gimple_debug_source_bind_p (copy))
1580 return false;
1581 else
1582 gcc_unreachable ();
1583
1584 return false;
1585 }
1586
1587 if (dump_file)
1588 {
1589 fprintf (dump_file, "[codegen] renaming uses of stmt: ");
1590 print_gimple_stmt (dump_file, copy, 0);
1591 }
1592
1593 use_operand_p use_p;
1594 ssa_op_iter op_iter;
1595 FOR_EACH_SSA_USE_OPERAND (use_p, copy, op_iter, SSA_OP_USE)
1596 {
1597 tree old_name = USE_FROM_PTR (use_p);
1598
1599 if (dump_file)
1600 {
1601 fprintf (dump_file, "[codegen] renaming old_name = ");
1602 print_generic_expr (dump_file, old_name);
1603 fprintf (dump_file, "\n");
1604 }
1605
1606 if (TREE_CODE (old_name) != SSA_NAME
1607 || SSA_NAME_IS_DEFAULT_DEF (old_name))
1608 continue;
1609
1610 changed = true;
1611 tree new_expr = get_rename (gsi_tgt->bb, old_name,
1612 old_bb, unknown_phi);
1613
1614 if (new_expr)
1615 {
1616 tree type_old_name = TREE_TYPE (old_name);
1617 tree type_new_expr = TREE_TYPE (new_expr);
1618
1619 if (dump_file)
1620 {
1621 fprintf (dump_file, "[codegen] from rename_map: new_name = ");
1622 print_generic_expr (dump_file, new_expr);
1623 fprintf (dump_file, "\n");
1624 }
1625
1626 if (type_old_name != type_new_expr
1627 || TREE_CODE (new_expr) != SSA_NAME)
1628 {
1629 tree var = create_tmp_var (type_old_name, "var");
1630
1631 if (!useless_type_conversion_p (type_old_name, type_new_expr))
1632 new_expr = fold_convert (type_old_name, new_expr);
1633
1634 gimple_seq stmts;
1635 new_expr = force_gimple_operand (new_expr, &stmts, true, var);
1636 gsi_insert_earliest (stmts);
1637 }
1638
1639 replace_exp (use_p, new_expr);
1640 continue;
1641 }
1642
1643 gimple_seq stmts;
1644 new_expr = get_rename_from_scev (old_name, &stmts, loop, gimple_bb (copy),
1645 old_bb, iv_map);
1646 if (!new_expr || codegen_error_p ())
1647 return false;
1648
1649 if (dump_file)
1650 {
1651 fprintf (dump_file, "[codegen] not in rename map, scev: ");
1652 print_generic_expr (dump_file, new_expr);
1653 fprintf (dump_file, "\n");
1654 }
1655
1656 gsi_insert_earliest (stmts);
1657 replace_exp (use_p, new_expr);
1658
1659 if (TREE_CODE (new_expr) == INTEGER_CST
1660 && is_gimple_assign (copy))
1661 {
1662 tree rhs = gimple_assign_rhs1 (copy);
1663
1664 if (TREE_CODE (rhs) == ADDR_EXPR)
1665 recompute_tree_invariant_for_addr_expr (rhs);
1666 }
1667
1668 set_rename (old_name, new_expr);
1669 }
1670
1671 return changed;
1672 }
1673
1674 /* Returns a basic block that could correspond to where a constant was defined
1675 in the original code. In the original code OLD_BB had the definition, we
1676 need to find which basic block out of the copies of old_bb, in the new
1677 region, should a definition correspond to if it has to reach BB. */
1678
1679 basic_block translate_isl_ast_to_gimple::
1680 get_def_bb_for_const (basic_block bb, basic_block old_bb) const
1681 {
1682 vec <basic_block> *bbs = region->copied_bb_map->get (old_bb);
1683
1684 if (!bbs || bbs->is_empty ())
1685 return NULL;
1686
1687 if (1 == bbs->length ())
1688 return (*bbs)[0];
1689
1690 int i;
1691 basic_block b1 = NULL, b2;
1692 FOR_EACH_VEC_ELT (*bbs, i, b2)
1693 {
1694 if (b2 == bb)
1695 return bb;
1696
1697 /* BB and B2 are in two unrelated if-clauses. */
1698 if (!dominated_by_p (CDI_DOMINATORS, bb, b2))
1699 continue;
1700
1701 /* Compute the nearest dominator. */
1702 if (!b1 || dominated_by_p (CDI_DOMINATORS, b2, b1))
1703 b1 = b2;
1704 }
1705
1706 return b1;
1707 }
1708
1709 /* Get the new name of OP (from OLD_BB) to be used in NEW_BB. PHI_KIND
1710 determines the kind of phi node. */
1711
1712 tree translate_isl_ast_to_gimple::
1713 get_new_name (basic_block new_bb, tree op,
1714 basic_block old_bb, phi_node_kind phi_kind) const
1715 {
1716 /* For constants the names are the same. */
1717 if (TREE_CODE (op) != SSA_NAME)
1718 return op;
1719
1720 return get_rename (new_bb, op, old_bb, phi_kind);
1721 }
1722
1723 /* Return a debug location for OP. */
1724
1725 static location_t
1726 get_loc (tree op)
1727 {
1728 location_t loc = UNKNOWN_LOCATION;
1729
1730 if (TREE_CODE (op) == SSA_NAME)
1731 loc = gimple_location (SSA_NAME_DEF_STMT (op));
1732 return loc;
1733 }
1734
1735 /* Returns the incoming edges of basic_block BB in the pair. The first edge is
1736 the init edge (from outside the loop) and the second one is the back edge
1737 from the same loop. */
1738
1739 std::pair<edge, edge>
1740 get_edges (basic_block bb)
1741 {
1742 std::pair<edge, edge> edges;
1743 edge e;
1744 edge_iterator ei;
1745 FOR_EACH_EDGE (e, ei, bb->preds)
1746 if (bb->loop_father != e->src->loop_father)
1747 edges.first = e;
1748 else
1749 edges.second = e;
1750 return edges;
1751 }
1752
1753 /* Copy the PHI arguments from OLD_PHI to the NEW_PHI. The arguments to NEW_PHI
1754 must be found unless they can be POSTPONEd for later. */
1755
1756 bool translate_isl_ast_to_gimple::
1757 copy_loop_phi_args (gphi *old_phi, init_back_edge_pair_t &ibp_old_bb,
1758 gphi *new_phi, init_back_edge_pair_t &ibp_new_bb,
1759 bool postpone)
1760 {
1761 gcc_assert (gimple_phi_num_args (old_phi) == gimple_phi_num_args (new_phi));
1762
1763 basic_block new_bb = gimple_bb (new_phi);
1764 for (unsigned i = 0; i < gimple_phi_num_args (old_phi); i++)
1765 {
1766 edge e;
1767 if (gimple_phi_arg_edge (old_phi, i) == ibp_old_bb.first)
1768 e = ibp_new_bb.first;
1769 else
1770 e = ibp_new_bb.second;
1771
1772 tree old_name = gimple_phi_arg_def (old_phi, i);
1773 tree new_name = get_new_name (new_bb, old_name,
1774 gimple_bb (old_phi), loop_phi);
1775 if (new_name)
1776 {
1777 add_phi_arg (new_phi, new_name, e, get_loc (old_name));
1778 continue;
1779 }
1780
1781 gimple *old_def_stmt = SSA_NAME_DEF_STMT (old_name);
1782 if (!old_def_stmt || gimple_code (old_def_stmt) == GIMPLE_NOP)
1783 /* If the phi arg was a function arg, or wasn't defined, just use the
1784 old name. */
1785 add_phi_arg (new_phi, old_name, e, get_loc (old_name));
1786 else if (postpone)
1787 {
1788 /* Postpone code gen for later for those back-edges we don't have the
1789 names yet. */
1790 region->incomplete_phis.safe_push (std::make_pair (old_phi, new_phi));
1791 if (dump_file)
1792 fprintf (dump_file, "[codegen] postpone loop phi nodes.\n");
1793 }
1794 else
1795 /* Either we should add the arg to phi or, we should postpone. */
1796 return false;
1797 }
1798 return true;
1799 }
1800
1801 /* Copy loop phi nodes from BB to NEW_BB. */
1802
1803 bool translate_isl_ast_to_gimple::
1804 copy_loop_phi_nodes (basic_block bb, basic_block new_bb)
1805 {
1806 if (dump_file)
1807 fprintf (dump_file, "[codegen] copying loop phi nodes in bb_%d.\n",
1808 new_bb->index);
1809
1810 /* Loop phi nodes should have only two arguments. */
1811 gcc_assert (2 == EDGE_COUNT (bb->preds));
1812
1813 /* First edge is the init edge and second is the back edge. */
1814 init_back_edge_pair_t ibp_old_bb = get_edges (bb);
1815
1816 /* First edge is the init edge and second is the back edge. */
1817 init_back_edge_pair_t ibp_new_bb = get_edges (new_bb);
1818
1819 for (gphi_iterator psi = gsi_start_phis (bb); !gsi_end_p (psi);
1820 gsi_next (&psi))
1821 {
1822 gphi *phi = psi.phi ();
1823 tree res = gimple_phi_result (phi);
1824 if (virtual_operand_p (res))
1825 continue;
1826 if (is_gimple_reg (res) && scev_analyzable_p (res, region->region))
1827 continue;
1828
1829 gphi *new_phi = create_phi_node (NULL_TREE, new_bb);
1830 tree new_res = create_new_def_for (res, new_phi,
1831 gimple_phi_result_ptr (new_phi));
1832 set_rename (res, new_res);
1833 if (!copy_loop_phi_args (phi, ibp_old_bb, new_phi, ibp_new_bb, true))
1834 set_codegen_error ();
1835 update_stmt (new_phi);
1836
1837 if (dump_file)
1838 {
1839 fprintf (dump_file, "[codegen] creating loop-phi node: ");
1840 print_gimple_stmt (dump_file, new_phi, 0);
1841 }
1842 }
1843
1844 return true;
1845 }
1846
1847 /* Return the init value of PHI, the value coming from outside the loop. */
1848
1849 static tree
1850 get_loop_init_value (gphi *phi)
1851 {
1852
1853 loop_p loop = gimple_bb (phi)->loop_father;
1854
1855 edge e;
1856 edge_iterator ei;
1857 FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
1858 if (e->src->loop_father != loop)
1859 return gimple_phi_arg_def (phi, e->dest_idx);
1860
1861 return NULL_TREE;
1862 }
1863
1864 /* Find the init value (the value which comes from outside the loop), of one of
1865 the operands of DEF which is defined by a loop phi. */
1866
1867 static tree
1868 find_init_value (gimple *def)
1869 {
1870 if (gimple_code (def) == GIMPLE_PHI)
1871 return get_loop_init_value (as_a <gphi*> (def));
1872
1873 if (gimple_vuse (def))
1874 return NULL_TREE;
1875
1876 ssa_op_iter iter;
1877 use_operand_p use_p;
1878 FOR_EACH_SSA_USE_OPERAND (use_p, def, iter, SSA_OP_USE)
1879 {
1880 tree use = USE_FROM_PTR (use_p);
1881 if (TREE_CODE (use) == SSA_NAME)
1882 {
1883 if (tree res = find_init_value (SSA_NAME_DEF_STMT (use)))
1884 return res;
1885 }
1886 }
1887
1888 return NULL_TREE;
1889 }
1890
1891 /* Return the init value, the value coming from outside the loop. */
1892
1893 static tree
1894 find_init_value_close_phi (gphi *phi)
1895 {
1896 gcc_assert (gimple_phi_num_args (phi) == 1);
1897 tree use_arg = gimple_phi_arg_def (phi, 0);
1898 gimple *def = SSA_NAME_DEF_STMT (use_arg);
1899 return find_init_value (def);
1900 }
1901
1902
1903 tree translate_isl_ast_to_gimple::
1904 add_close_phis_to_outer_loops (tree last_merge_name, edge last_e,
1905 gimple *old_close_phi)
1906 {
1907 sese_l &codegen_region = region->if_region->true_region->region;
1908 gimple *stmt = SSA_NAME_DEF_STMT (last_merge_name);
1909 basic_block bb = gimple_bb (stmt);
1910 if (!bb_in_sese_p (bb, codegen_region))
1911 return last_merge_name;
1912
1913 loop_p loop = bb->loop_father;
1914 if (!loop_in_sese_p (loop, codegen_region))
1915 return last_merge_name;
1916
1917 edge e = single_exit (loop);
1918
1919 if (dominated_by_p (CDI_DOMINATORS, e->dest, last_e->src))
1920 return last_merge_name;
1921
1922 tree old_name = gimple_phi_arg_def (old_close_phi, 0);
1923 tree old_close_phi_name = gimple_phi_result (old_close_phi);
1924
1925 bb = e->dest;
1926 if (!bb_contains_loop_close_phi_nodes (bb) || !single_succ_p (bb))
1927 bb = split_edge (e);
1928
1929 gphi *close_phi = create_phi_node (NULL_TREE, bb);
1930 tree res = create_new_def_for (last_merge_name, close_phi,
1931 gimple_phi_result_ptr (close_phi));
1932 set_rename (old_close_phi_name, res);
1933 add_phi_arg (close_phi, last_merge_name, e, get_loc (old_name));
1934 last_merge_name = res;
1935
1936 return add_close_phis_to_outer_loops (last_merge_name, last_e, old_close_phi);
1937 }
1938
1939 /* Add phi nodes to all merge points of all the diamonds enclosing the loop of
1940 the close phi node PHI. */
1941
1942 bool translate_isl_ast_to_gimple::
1943 add_close_phis_to_merge_points (gphi *old_close_phi, gphi *new_close_phi,
1944 tree default_value)
1945 {
1946 sese_l &codegen_region = region->if_region->true_region->region;
1947 basic_block default_value_bb = get_entry_bb (codegen_region);
1948 if (SSA_NAME == TREE_CODE (default_value))
1949 {
1950 gimple *stmt = SSA_NAME_DEF_STMT (default_value);
1951 if (!stmt || gimple_code (stmt) == GIMPLE_NOP)
1952 return false;
1953 default_value_bb = gimple_bb (stmt);
1954 }
1955
1956 basic_block new_close_phi_bb = gimple_bb (new_close_phi);
1957
1958 tree old_close_phi_name = gimple_phi_result (old_close_phi);
1959 tree new_close_phi_name = gimple_phi_result (new_close_phi);
1960 tree last_merge_name = new_close_phi_name;
1961 tree old_name = gimple_phi_arg_def (old_close_phi, 0);
1962
1963 int i;
1964 edge merge_e;
1965 FOR_EACH_VEC_ELT_REVERSE (merge_points, i, merge_e)
1966 {
1967 basic_block new_merge_bb = merge_e->src;
1968 if (!dominated_by_p (CDI_DOMINATORS, new_merge_bb, default_value_bb))
1969 continue;
1970
1971 last_merge_name = add_close_phis_to_outer_loops (last_merge_name, merge_e,
1972 old_close_phi);
1973
1974 gphi *merge_phi = create_phi_node (NULL_TREE, new_merge_bb);
1975 tree merge_res = create_new_def_for (old_close_phi_name, merge_phi,
1976 gimple_phi_result_ptr (merge_phi));
1977 set_rename (old_close_phi_name, merge_res);
1978
1979 edge from_loop = NULL, from_default_value = NULL;
1980 edge e;
1981 edge_iterator ei;
1982 FOR_EACH_EDGE (e, ei, new_merge_bb->preds)
1983 if (dominated_by_p (CDI_DOMINATORS, e->src, new_close_phi_bb))
1984 from_loop = e;
1985 else
1986 from_default_value = e;
1987
1988 /* Because CDI_POST_DOMINATORS are not updated, we only rely on
1989 CDI_DOMINATORS, which may not handle all cases where new_close_phi_bb
1990 is contained in another condition. */
1991 if (!from_default_value || !from_loop)
1992 return false;
1993
1994 add_phi_arg (merge_phi, last_merge_name, from_loop, get_loc (old_name));
1995 add_phi_arg (merge_phi, default_value, from_default_value, get_loc (old_name));
1996
1997 if (dump_file)
1998 {
1999 fprintf (dump_file, "[codegen] Adding guard-phi: ");
2000 print_gimple_stmt (dump_file, merge_phi, 0);
2001 }
2002
2003 update_stmt (merge_phi);
2004 last_merge_name = merge_res;
2005 }
2006
2007 return true;
2008 }
2009
2010 /* Copy all the loop-close phi args from BB to NEW_BB. */
2011
2012 bool translate_isl_ast_to_gimple::
2013 copy_loop_close_phi_args (basic_block old_bb, basic_block new_bb,
2014 vec<tree> iv_map, bool postpone)
2015 {
2016 for (gphi_iterator psi = gsi_start_phis (old_bb); !gsi_end_p (psi);
2017 gsi_next (&psi))
2018 {
2019 gphi *old_close_phi = psi.phi ();
2020 tree res = gimple_phi_result (old_close_phi);
2021 if (virtual_operand_p (res))
2022 continue;
2023
2024 gphi *new_close_phi = create_phi_node (NULL_TREE, new_bb);
2025 tree new_res = create_new_def_for (res, new_close_phi,
2026 gimple_phi_result_ptr (new_close_phi));
2027 set_rename (res, new_res);
2028
2029 tree old_name = gimple_phi_arg_def (old_close_phi, 0);
2030 tree new_name;
2031 if (is_gimple_reg (res) && scev_analyzable_p (res, region->region))
2032 {
2033 gimple_seq stmts;
2034 new_name = get_rename_from_scev (old_name, &stmts,
2035 old_bb->loop_father,
2036 new_bb, old_bb, iv_map);
2037 if (! codegen_error_p ())
2038 gsi_insert_earliest (stmts);
2039 }
2040 else
2041 new_name = get_new_name (new_bb, old_name, old_bb, close_phi);
2042
2043 /* Predecessor basic blocks of a loop close phi should have been code
2044 generated before. FIXME: This is fixable by merging PHIs from inner
2045 loops as well. See: gfortran.dg/graphite/interchange-3.f90. */
2046 if (!new_name || codegen_error_p ())
2047 return false;
2048
2049 add_phi_arg (new_close_phi, new_name, single_pred_edge (new_bb),
2050 get_loc (old_name));
2051 if (dump_file)
2052 {
2053 fprintf (dump_file, "[codegen] Adding loop close phi: ");
2054 print_gimple_stmt (dump_file, new_close_phi, 0);
2055 }
2056
2057 update_stmt (new_close_phi);
2058
2059 /* When there is no loop guard around this codegenerated loop, there is no
2060 need to collect the close-phi arg. */
2061 if (merge_points.is_empty ())
2062 continue;
2063
2064 /* Add a PHI in the succ_new_bb for each close phi of the loop. */
2065 tree default_value = find_init_value_close_phi (new_close_phi);
2066
2067 /* A close phi must come from a loop-phi having a default value. */
2068 if (!default_value)
2069 {
2070 if (!postpone)
2071 return false;
2072
2073 region->incomplete_phis.safe_push (std::make_pair (old_close_phi,
2074 new_close_phi));
2075 if (dump_file)
2076 {
2077 fprintf (dump_file, "[codegen] postpone close phi nodes: ");
2078 print_gimple_stmt (dump_file, new_close_phi, 0);
2079 }
2080 continue;
2081 }
2082
2083 if (!add_close_phis_to_merge_points (old_close_phi, new_close_phi,
2084 default_value))
2085 return false;
2086 }
2087
2088 return true;
2089 }
2090
2091 /* Copy loop close phi nodes from BB to NEW_BB. */
2092
2093 bool translate_isl_ast_to_gimple::
2094 copy_loop_close_phi_nodes (basic_block old_bb, basic_block new_bb,
2095 vec<tree> iv_map)
2096 {
2097 if (dump_file)
2098 fprintf (dump_file, "[codegen] copying loop close phi nodes in bb_%d.\n",
2099 new_bb->index);
2100 /* Loop close phi nodes should have only one argument. */
2101 gcc_assert (1 == EDGE_COUNT (old_bb->preds));
2102
2103 return copy_loop_close_phi_args (old_bb, new_bb, iv_map, true);
2104 }
2105
2106
2107 /* Add NEW_NAME as the ARGNUM-th arg of NEW_PHI which is in NEW_BB.
2108 DOMINATING_PRED is the predecessor basic block of OLD_BB which dominates the
2109 other pred of OLD_BB as well. If no such basic block exists then it is NULL.
2110 NON_DOMINATING_PRED is a pred which does not dominate OLD_BB, it cannot be
2111 NULL.
2112
2113 Case1: OLD_BB->preds {BB1, BB2} and BB1 does not dominate BB2 and vice versa.
2114 In this case DOMINATING_PRED = NULL.
2115
2116 Case2: OLD_BB->preds {BB1, BB2} and BB1 dominates BB2.
2117
2118 Returns true on successful copy of the args, false otherwise. */
2119
2120 bool translate_isl_ast_to_gimple::
2121 add_phi_arg_for_new_expr (tree old_phi_args[2], tree new_phi_args[2],
2122 edge old_bb_dominating_edge,
2123 edge old_bb_non_dominating_edge,
2124 gphi *phi, gphi *new_phi,
2125 basic_block new_bb)
2126 {
2127 basic_block def_pred[2] = { NULL, NULL };
2128 int not_found_bb_index = -1;
2129 for (int i = 0; i < 2; i++)
2130 {
2131 /* If the corresponding def_bb could not be found the entry will be
2132 NULL. */
2133 if (TREE_CODE (old_phi_args[i]) == INTEGER_CST)
2134 def_pred[i] = get_def_bb_for_const (new_bb,
2135 gimple_phi_arg_edge (phi, i)->src);
2136 else if (new_phi_args[i] && (TREE_CODE (new_phi_args[i]) == SSA_NAME))
2137 def_pred[i] = gimple_bb (SSA_NAME_DEF_STMT (new_phi_args[i]));
2138
2139 if (!def_pred[i])
2140 {
2141 /* When non are available bail out. */
2142 if (not_found_bb_index != -1)
2143 return false;
2144 not_found_bb_index = i;
2145 }
2146 }
2147
2148 /* Here we are pattern matching on the structure of CFG w.r.t. old one. */
2149 if (old_bb_dominating_edge)
2150 {
2151 if (not_found_bb_index != -1)
2152 return false;
2153
2154 basic_block new_pred1 = (*new_bb->preds)[0]->src;
2155 basic_block new_pred2 = (*new_bb->preds)[1]->src;
2156 vec <basic_block> *bbs
2157 = region->copied_bb_map->get (old_bb_non_dominating_edge->src);
2158
2159 /* Could not find a mapping. */
2160 if (!bbs)
2161 return false;
2162
2163 basic_block new_pred = NULL;
2164 basic_block b;
2165 int i;
2166 FOR_EACH_VEC_ELT (*bbs, i, b)
2167 {
2168 if (dominated_by_p (CDI_DOMINATORS, new_pred1, b))
2169 {
2170 /* FIXME: If we have already found new_pred then we have to
2171 disambiguate, bail out for now. */
2172 if (new_pred)
2173 return false;
2174 new_pred = new_pred1;
2175 }
2176 if (dominated_by_p (CDI_DOMINATORS, new_pred2, b))
2177 {
2178 /* FIXME: If we have already found new_pred then we have to either
2179 it dominates both or we have to disambiguate, bail out. */
2180 if (new_pred)
2181 return false;
2182 new_pred = new_pred2;
2183 }
2184 }
2185
2186 if (!new_pred)
2187 return false;
2188
2189 edge new_non_dominating_edge = find_edge (new_pred, new_bb);
2190 gcc_assert (new_non_dominating_edge);
2191 /* FIXME: Validate each args just like in loop-phis. */
2192 /* By the process of elimination we first insert insert phi-edge for
2193 non-dominating pred which is computed above and then we insert the
2194 remaining one. */
2195 int inserted_edge = 0;
2196 for (; inserted_edge < 2; inserted_edge++)
2197 {
2198 edge new_bb_pred_edge = gimple_phi_arg_edge (new_phi, inserted_edge);
2199 if (new_non_dominating_edge == new_bb_pred_edge)
2200 {
2201 add_phi_arg (new_phi, new_phi_args[inserted_edge],
2202 new_non_dominating_edge,
2203 get_loc (old_phi_args[inserted_edge]));
2204 break;
2205 }
2206 }
2207 if (inserted_edge == 2)
2208 return false;
2209
2210 int edge_dominating = inserted_edge == 0 ? 1 : 0;
2211
2212 edge new_dominating_edge = NULL;
2213 for (inserted_edge = 0; inserted_edge < 2; inserted_edge++)
2214 {
2215 edge e = gimple_phi_arg_edge (new_phi, inserted_edge);
2216 if (e != new_non_dominating_edge)
2217 {
2218 new_dominating_edge = e;
2219 add_phi_arg (new_phi, new_phi_args[edge_dominating],
2220 new_dominating_edge,
2221 get_loc (old_phi_args[inserted_edge]));
2222 break;
2223 }
2224 }
2225 gcc_assert (new_dominating_edge);
2226 }
2227 else
2228 {
2229 /* Classic diamond structure: both edges are non-dominating. We need to
2230 find one unique edge then the other can be found be elimination. If
2231 any definition (def_pred) dominates both the preds of new_bb then we
2232 bail out. Entries of def_pred maybe NULL, in that case we must
2233 uniquely find pred with help of only one entry. */
2234 edge new_e[2] = { NULL, NULL };
2235 for (int i = 0; i < 2; i++)
2236 {
2237 edge e;
2238 edge_iterator ei;
2239 FOR_EACH_EDGE (e, ei, new_bb->preds)
2240 if (def_pred[i]
2241 && dominated_by_p (CDI_DOMINATORS, e->src, def_pred[i]))
2242 {
2243 if (new_e[i])
2244 /* We do not know how to handle the case when def_pred
2245 dominates more than a predecessor. */
2246 return false;
2247 new_e[i] = e;
2248 }
2249 }
2250
2251 gcc_assert (new_e[0] || new_e[1]);
2252
2253 /* Find the other edge by process of elimination. */
2254 if (not_found_bb_index != -1)
2255 {
2256 gcc_assert (!new_e[not_found_bb_index]);
2257 int found_bb_index = not_found_bb_index == 1 ? 0 : 1;
2258 edge e;
2259 edge_iterator ei;
2260 FOR_EACH_EDGE (e, ei, new_bb->preds)
2261 {
2262 if (new_e[found_bb_index] == e)
2263 continue;
2264 new_e[not_found_bb_index] = e;
2265 }
2266 }
2267
2268 /* Add edges to phi args. */
2269 for (int i = 0; i < 2; i++)
2270 add_phi_arg (new_phi, new_phi_args[i], new_e[i],
2271 get_loc (old_phi_args[i]));
2272 }
2273
2274 return true;
2275 }
2276
2277 /* Copy the arguments of cond-phi node PHI, to NEW_PHI in the codegenerated
2278 region. If postpone is true and it isn't possible to copy any arg of PHI,
2279 the PHI is added to the REGION->INCOMPLETE_PHIS to be codegenerated later.
2280 Returns false if the copying was unsuccessful. */
2281
2282 bool translate_isl_ast_to_gimple::
2283 copy_cond_phi_args (gphi *phi, gphi *new_phi, vec<tree> iv_map, bool postpone)
2284 {
2285 if (dump_file)
2286 fprintf (dump_file, "[codegen] copying cond phi args.\n");
2287 gcc_assert (2 == gimple_phi_num_args (phi));
2288
2289 basic_block new_bb = gimple_bb (new_phi);
2290 loop_p loop = gimple_bb (phi)->loop_father;
2291
2292 basic_block old_bb = gimple_bb (phi);
2293 edge old_bb_non_dominating_edge = NULL, old_bb_dominating_edge = NULL;
2294
2295 edge e;
2296 edge_iterator ei;
2297 FOR_EACH_EDGE (e, ei, old_bb->preds)
2298 if (!dominated_by_p (CDI_DOMINATORS, old_bb, e->src))
2299 old_bb_non_dominating_edge = e;
2300 else
2301 old_bb_dominating_edge = e;
2302
2303 gcc_assert (!dominated_by_p (CDI_DOMINATORS, old_bb,
2304 old_bb_non_dominating_edge->src));
2305
2306 tree new_phi_args[2];
2307 tree old_phi_args[2];
2308
2309 for (unsigned i = 0; i < gimple_phi_num_args (phi); i++)
2310 {
2311 tree old_name = gimple_phi_arg_def (phi, i);
2312 tree new_name = get_new_name (new_bb, old_name, old_bb, cond_phi);
2313 old_phi_args[i] = old_name;
2314 if (new_name)
2315 {
2316 new_phi_args [i] = new_name;
2317 continue;
2318 }
2319
2320 /* If the phi-arg was a parameter. */
2321 if (vec_find (region->params, old_name) != -1)
2322 {
2323 new_phi_args [i] = old_name;
2324 if (dump_file)
2325 {
2326 fprintf (dump_file,
2327 "[codegen] parameter argument to phi, new_expr: ");
2328 print_generic_expr (dump_file, new_phi_args[i]);
2329 fprintf (dump_file, "\n");
2330 }
2331 continue;
2332 }
2333
2334 gimple *old_def_stmt = SSA_NAME_DEF_STMT (old_name);
2335 if (!old_def_stmt || gimple_code (old_def_stmt) == GIMPLE_NOP)
2336 /* FIXME: If the phi arg was a function arg, or wasn't defined, just use
2337 the old name. */
2338 return false;
2339
2340 if (postpone)
2341 {
2342 /* If the phi-arg is scev-analyzeable but only in the first stage. */
2343 if (is_gimple_reg (old_name)
2344 && scev_analyzable_p (old_name, region->region))
2345 {
2346 gimple_seq stmts;
2347 tree new_expr = get_rename_from_scev (old_name, &stmts, loop,
2348 new_bb, old_bb, iv_map);
2349 if (codegen_error_p ())
2350 return false;
2351
2352 gcc_assert (new_expr);
2353 if (dump_file)
2354 {
2355 fprintf (dump_file,
2356 "[codegen] scev analyzeable, new_expr: ");
2357 print_generic_expr (dump_file, new_expr);
2358 fprintf (dump_file, "\n");
2359 }
2360 gsi_insert_earliest (stmts);
2361 new_phi_args[i] = new_expr;
2362 continue;
2363 }
2364
2365 /* Postpone code gen for later for back-edges. */
2366 region->incomplete_phis.safe_push (std::make_pair (phi, new_phi));
2367
2368 if (dump_file)
2369 {
2370 fprintf (dump_file, "[codegen] postpone cond phi nodes: ");
2371 print_gimple_stmt (dump_file, new_phi, 0);
2372 }
2373
2374 new_phi_args [i] = NULL_TREE;
2375 continue;
2376 }
2377 else
2378 /* Either we should add the arg to phi or, we should postpone. */
2379 return false;
2380 }
2381
2382 /* If none of the args have been determined in the first stage then wait until
2383 later. */
2384 if (postpone && !new_phi_args[0] && !new_phi_args[1])
2385 return true;
2386
2387 return add_phi_arg_for_new_expr (old_phi_args, new_phi_args,
2388 old_bb_dominating_edge,
2389 old_bb_non_dominating_edge,
2390 phi, new_phi, new_bb);
2391 }
2392
2393 /* Copy cond phi nodes from BB to NEW_BB. A cond-phi node is a basic block
2394 containing phi nodes coming from two predecessors, and none of them are back
2395 edges. */
2396
2397 bool translate_isl_ast_to_gimple::
2398 copy_cond_phi_nodes (basic_block bb, basic_block new_bb, vec<tree> iv_map)
2399 {
2400
2401 gcc_assert (!bb_contains_loop_close_phi_nodes (bb));
2402
2403 /* TODO: Handle cond phi nodes with more than 2 predecessors. */
2404 if (EDGE_COUNT (bb->preds) != 2)
2405 return false;
2406
2407 if (dump_file)
2408 fprintf (dump_file, "[codegen] copying cond phi nodes in bb_%d.\n",
2409 new_bb->index);
2410
2411 for (gphi_iterator psi = gsi_start_phis (bb); !gsi_end_p (psi);
2412 gsi_next (&psi))
2413 {
2414 gphi *phi = psi.phi ();
2415 tree res = gimple_phi_result (phi);
2416 if (virtual_operand_p (res))
2417 continue;
2418
2419 gphi *new_phi = create_phi_node (NULL_TREE, new_bb);
2420 tree new_res = create_new_def_for (res, new_phi,
2421 gimple_phi_result_ptr (new_phi));
2422 set_rename (res, new_res);
2423
2424 if (!copy_cond_phi_args (phi, new_phi, iv_map, true))
2425 return false;
2426
2427 update_stmt (new_phi);
2428 }
2429
2430 return true;
2431 }
2432
2433 /* Return true if STMT should be copied from region to the new code-generated
2434 region. LABELs, CONDITIONS, induction-variables and region parameters need
2435 not be copied. */
2436
2437 static bool
2438 should_copy_to_new_region (gimple *stmt, sese_info_p region)
2439 {
2440 /* Do not copy labels or conditions. */
2441 if (gimple_code (stmt) == GIMPLE_LABEL
2442 || gimple_code (stmt) == GIMPLE_COND)
2443 return false;
2444
2445 tree lhs;
2446 /* Do not copy induction variables. */
2447 if (is_gimple_assign (stmt)
2448 && (lhs = gimple_assign_lhs (stmt))
2449 && TREE_CODE (lhs) == SSA_NAME
2450 && is_gimple_reg (lhs)
2451 && scev_analyzable_p (lhs, region->region))
2452 return false;
2453
2454 /* Do not copy parameters that have been generated in the header of the
2455 scop. */
2456 if (is_gimple_assign (stmt)
2457 && (lhs = gimple_assign_lhs (stmt))
2458 && TREE_CODE (lhs) == SSA_NAME
2459 && region->parameter_rename_map->get(lhs))
2460 return false;
2461
2462 return true;
2463 }
2464
2465 /* Create new names for all the definitions created by COPY and add replacement
2466 mappings for each new name. */
2467
2468 void translate_isl_ast_to_gimple::
2469 set_rename_for_each_def (gimple *stmt)
2470 {
2471 def_operand_p def_p;
2472 ssa_op_iter op_iter;
2473 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, op_iter, SSA_OP_ALL_DEFS)
2474 {
2475 tree old_name = DEF_FROM_PTR (def_p);
2476 tree new_name = create_new_def_for (old_name, stmt, def_p);
2477 set_rename (old_name, new_name);
2478 }
2479 }
2480
2481 /* Duplicates the statements of basic block BB into basic block NEW_BB
2482 and compute the new induction variables according to the IV_MAP. */
2483
2484 bool translate_isl_ast_to_gimple::
2485 graphite_copy_stmts_from_block (basic_block bb, basic_block new_bb,
2486 vec<tree> iv_map)
2487 {
2488 /* Iterator poining to the place where new statement (s) will be inserted. */
2489 gimple_stmt_iterator gsi_tgt = gsi_last_bb (new_bb);
2490
2491 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
2492 gsi_next (&gsi))
2493 {
2494 gimple *stmt = gsi_stmt (gsi);
2495 if (!should_copy_to_new_region (stmt, region))
2496 continue;
2497
2498 /* Create a new copy of STMT and duplicate STMT's virtual
2499 operands. */
2500 gimple *copy = gimple_copy (stmt);
2501 gsi_insert_after (&gsi_tgt, copy, GSI_NEW_STMT);
2502
2503 if (dump_file)
2504 {
2505 fprintf (dump_file, "[codegen] inserting statement: ");
2506 print_gimple_stmt (dump_file, copy, 0);
2507 }
2508
2509 maybe_duplicate_eh_stmt (copy, stmt);
2510 gimple_duplicate_stmt_histograms (cfun, copy, cfun, stmt);
2511
2512 /* Crete new names for each def in the copied stmt. */
2513 set_rename_for_each_def (copy);
2514
2515 loop_p loop = bb->loop_father;
2516 if (rename_uses (copy, &gsi_tgt, bb, loop, iv_map))
2517 {
2518 fold_stmt_inplace (&gsi_tgt);
2519 gcc_assert (gsi_stmt (gsi_tgt) == copy);
2520 }
2521
2522 if (codegen_error_p ())
2523 return false;
2524
2525 /* For each SSA_NAME in the parameter_rename_map rename their usage. */
2526 ssa_op_iter iter;
2527 use_operand_p use_p;
2528 if (!is_gimple_debug (copy))
2529 FOR_EACH_SSA_USE_OPERAND (use_p, copy, iter, SSA_OP_USE)
2530 {
2531 tree old_name = USE_FROM_PTR (use_p);
2532
2533 if (TREE_CODE (old_name) != SSA_NAME
2534 || SSA_NAME_IS_DEFAULT_DEF (old_name))
2535 continue;
2536
2537 tree *new_expr = region->parameter_rename_map->get (old_name);
2538 if (!new_expr)
2539 continue;
2540
2541 replace_exp (use_p, *new_expr);
2542 }
2543
2544 update_stmt (copy);
2545 }
2546
2547 return true;
2548 }
2549
2550
2551 /* Given a basic block containing close-phi it returns the new basic block where
2552 to insert a copy of the close-phi nodes. All the uses in close phis should
2553 come from a single loop otherwise it returns NULL. */
2554
2555 edge translate_isl_ast_to_gimple::
2556 edge_for_new_close_phis (basic_block bb)
2557 {
2558 /* Make sure that NEW_BB is the new_loop->exit->dest. We find the definition
2559 of close phi in the original code and then find the mapping of basic block
2560 defining that variable. If there are multiple close-phis and they are
2561 defined in different loops (in the original or in the new code) because of
2562 loop splitting, then we bail out. */
2563 loop_p new_loop = NULL;
2564 for (gphi_iterator psi = gsi_start_phis (bb); !gsi_end_p (psi);
2565 gsi_next (&psi))
2566 {
2567 gphi *phi = psi.phi ();
2568 tree name = gimple_phi_arg_def (phi, 0);
2569 basic_block old_loop_bb = gimple_bb (SSA_NAME_DEF_STMT (name));
2570
2571 vec <basic_block> *bbs = region->copied_bb_map->get (old_loop_bb);
2572 if (!bbs || bbs->length () != 1)
2573 /* This is one of the places which shows preserving original structure
2574 is not always possible, as we may need to insert close PHI for a loop
2575 where the latch does not have any mapping, or the mapping is
2576 ambiguous. */
2577 return NULL;
2578
2579 if (!new_loop)
2580 new_loop = (*bbs)[0]->loop_father;
2581 else if (new_loop != (*bbs)[0]->loop_father)
2582 return NULL;
2583 }
2584
2585 if (!new_loop)
2586 return NULL;
2587
2588 return single_exit (new_loop);
2589 }
2590
2591 /* Copies BB and includes in the copied BB all the statements that can
2592 be reached following the use-def chains from the memory accesses,
2593 and returns the next edge following this new block. */
2594
2595 edge translate_isl_ast_to_gimple::
2596 copy_bb_and_scalar_dependences (basic_block bb, edge next_e, vec<tree> iv_map)
2597 {
2598 int num_phis = number_of_phi_nodes (bb);
2599 basic_block new_bb = NULL;
2600 if (bb_contains_loop_close_phi_nodes (bb))
2601 {
2602 if (dump_file)
2603 fprintf (dump_file, "[codegen] bb_%d contains close phi nodes.\n",
2604 bb->index);
2605
2606 edge e = edge_for_new_close_phis (bb);
2607 if (!e)
2608 {
2609 set_codegen_error ();
2610 return NULL;
2611 }
2612
2613 basic_block phi_bb = e->dest;
2614
2615 if (!bb_contains_loop_close_phi_nodes (phi_bb) || !single_succ_p (phi_bb))
2616 phi_bb = split_edge (e);
2617
2618 gcc_assert (single_pred_edge (phi_bb)->src->loop_father
2619 != single_pred_edge (phi_bb)->dest->loop_father);
2620
2621 if (!copy_loop_close_phi_nodes (bb, phi_bb, iv_map))
2622 {
2623 set_codegen_error ();
2624 return NULL;
2625 }
2626
2627 if (e == next_e)
2628 new_bb = phi_bb;
2629 else
2630 new_bb = split_edge (next_e);
2631 }
2632 else
2633 {
2634 new_bb = split_edge (next_e);
2635 if (num_phis > 0 && bb_contains_loop_phi_nodes (bb))
2636 {
2637 basic_block phi_bb = next_e->dest->loop_father->header;
2638
2639 /* At this point we are unable to codegenerate by still preserving the SSA
2640 structure because maybe the loop is completely unrolled and the PHIs
2641 and cross-bb scalar dependencies are untrackable w.r.t. the original
2642 code. See gfortran.dg/graphite/pr29832.f90. */
2643 if (EDGE_COUNT (bb->preds) != EDGE_COUNT (phi_bb->preds))
2644 {
2645 set_codegen_error ();
2646 return NULL;
2647 }
2648
2649 /* In case isl did some loop peeling, like this:
2650
2651 S_8(0);
2652 for (int c1 = 1; c1 <= 5; c1 += 1) {
2653 S_8(c1);
2654 }
2655 S_8(6);
2656
2657 there should be no loop-phi nodes in S_8(0).
2658
2659 FIXME: We need to reason about dynamic instances of S_8, i.e., the
2660 values of all scalar variables: for the moment we instantiate only
2661 SCEV analyzable expressions on the iteration domain, and we need to
2662 extend that to reductions that cannot be analyzed by SCEV. */
2663 if (!bb_in_sese_p (phi_bb, region->if_region->true_region->region))
2664 {
2665 set_codegen_error ();
2666 return NULL;
2667 }
2668
2669 if (dump_file)
2670 fprintf (dump_file, "[codegen] bb_%d contains loop phi nodes.\n",
2671 bb->index);
2672 if (!copy_loop_phi_nodes (bb, phi_bb))
2673 {
2674 set_codegen_error ();
2675 return NULL;
2676 }
2677 }
2678 else if (num_phis > 0)
2679 {
2680 if (dump_file)
2681 fprintf (dump_file, "[codegen] bb_%d contains cond phi nodes.\n",
2682 bb->index);
2683
2684 basic_block phi_bb = single_pred (new_bb);
2685 loop_p loop_father = new_bb->loop_father;
2686
2687 /* Move back until we find the block with two predecessors. */
2688 while (single_pred_p (phi_bb))
2689 phi_bb = single_pred_edge (phi_bb)->src;
2690
2691 /* If a corresponding merge-point was not found, then abort codegen. */
2692 if (phi_bb->loop_father != loop_father
2693 || !bb_in_sese_p (phi_bb, region->if_region->true_region->region)
2694 || !copy_cond_phi_nodes (bb, phi_bb, iv_map))
2695 {
2696 set_codegen_error ();
2697 return NULL;
2698 }
2699 }
2700 }
2701
2702 if (dump_file)
2703 fprintf (dump_file, "[codegen] copying from bb_%d to bb_%d.\n",
2704 bb->index, new_bb->index);
2705
2706 vec <basic_block> *copied_bbs = region->copied_bb_map->get (bb);
2707 if (copied_bbs)
2708 copied_bbs->safe_push (new_bb);
2709 else
2710 {
2711 vec<basic_block> bbs;
2712 bbs.create (2);
2713 bbs.safe_push (new_bb);
2714 region->copied_bb_map->put (bb, bbs);
2715 }
2716
2717 if (!graphite_copy_stmts_from_block (bb, new_bb, iv_map))
2718 {
2719 set_codegen_error ();
2720 return NULL;
2721 }
2722
2723 return single_succ_edge (new_bb);
2724 }
2725
2726 /* Patch the missing arguments of the phi nodes. */
2727
2728 void translate_isl_ast_to_gimple::
2729 translate_pending_phi_nodes ()
2730 {
2731 int i;
2732 phi_rename *rename;
2733 FOR_EACH_VEC_ELT (region->incomplete_phis, i, rename)
2734 {
2735 gphi *old_phi = rename->first;
2736 gphi *new_phi = rename->second;
2737 basic_block old_bb = gimple_bb (old_phi);
2738 basic_block new_bb = gimple_bb (new_phi);
2739
2740 /* First edge is the init edge and second is the back edge. */
2741 init_back_edge_pair_t ibp_old_bb = get_edges (old_bb);
2742 init_back_edge_pair_t ibp_new_bb = get_edges (new_bb);
2743
2744 if (dump_file)
2745 {
2746 fprintf (dump_file, "[codegen] translating pending old-phi: ");
2747 print_gimple_stmt (dump_file, old_phi, 0);
2748 }
2749
2750 auto_vec <tree, 1> iv_map;
2751 if (bb_contains_loop_phi_nodes (new_bb)
2752 && bb_contains_loop_phi_nodes (old_bb))
2753 {
2754 if (!copy_loop_phi_args (old_phi, ibp_old_bb, new_phi,
2755 ibp_new_bb, false))
2756 set_codegen_error ();
2757 }
2758 else if (bb_contains_loop_close_phi_nodes (new_bb))
2759 {
2760 if (!copy_loop_close_phi_args (old_bb, new_bb, iv_map, false))
2761 set_codegen_error ();
2762 }
2763 else if (!copy_cond_phi_args (old_phi, new_phi, iv_map, false))
2764 set_codegen_error ();
2765
2766 if (dump_file)
2767 {
2768 fprintf (dump_file, "[codegen] to new-phi: ");
2769 print_gimple_stmt (dump_file, new_phi, 0);
2770 }
2771 if (codegen_error_p ())
2772 return;
2773 }
2774 }
2775
2776 /* Add isl's parameter identifiers and corresponding trees to ivs_params. */
2777
2778 void translate_isl_ast_to_gimple::
2779 add_parameters_to_ivs_params (scop_p scop, ivs_params &ip)
2780 {
2781 sese_info_p region = scop->scop_info;
2782 unsigned nb_parameters = isl_set_dim (scop->param_context, isl_dim_param);
2783 gcc_assert (nb_parameters == region->params.length ());
2784 unsigned i;
2785 for (i = 0; i < nb_parameters; i++)
2786 {
2787 isl_id *tmp_id = isl_set_get_dim_id (scop->param_context,
2788 isl_dim_param, i);
2789 ip[tmp_id] = region->params[i];
2790 }
2791 }
2792
2793
2794 /* Generates a build, which specifies the constraints on the parameters. */
2795
2796 __isl_give isl_ast_build *translate_isl_ast_to_gimple::
2797 generate_isl_context (scop_p scop)
2798 {
2799 isl_set *context_isl = isl_set_params (isl_set_copy (scop->param_context));
2800 return isl_ast_build_from_context (context_isl);
2801 }
2802
2803 /* This method is executed before the construction of a for node. */
2804 __isl_give isl_id *
2805 ast_build_before_for (__isl_keep isl_ast_build *build, void *user)
2806 {
2807 isl_union_map *dependences = (isl_union_map *) user;
2808 ast_build_info *for_info = XNEW (struct ast_build_info);
2809 isl_union_map *schedule = isl_ast_build_get_schedule (build);
2810 isl_space *schedule_space = isl_ast_build_get_schedule_space (build);
2811 int dimension = isl_space_dim (schedule_space, isl_dim_out);
2812 for_info->is_parallelizable =
2813 !carries_deps (schedule, dependences, dimension);
2814 isl_union_map_free (schedule);
2815 isl_space_free (schedule_space);
2816 isl_id *id = isl_id_alloc (isl_ast_build_get_ctx (build), "", for_info);
2817 return id;
2818 }
2819
2820 /* Generate isl AST from schedule of SCOP. */
2821
2822 __isl_give isl_ast_node *translate_isl_ast_to_gimple::
2823 scop_to_isl_ast (scop_p scop)
2824 {
2825 gcc_assert (scop->transformed_schedule);
2826
2827 /* Set the separate option to reduce control flow overhead. */
2828 isl_schedule *schedule = isl_schedule_map_schedule_node_bottom_up
2829 (isl_schedule_copy (scop->transformed_schedule), set_separate_option, NULL);
2830 isl_ast_build *context_isl = generate_isl_context (scop);
2831
2832 if (flag_loop_parallelize_all)
2833 {
2834 scop_get_dependences (scop);
2835 context_isl =
2836 isl_ast_build_set_before_each_for (context_isl, ast_build_before_for,
2837 scop->dependence);
2838 }
2839
2840 isl_ast_node *ast_isl = isl_ast_build_node_from_schedule
2841 (context_isl, schedule);
2842 isl_ast_build_free (context_isl);
2843 return ast_isl;
2844 }
2845
2846 /* Copy def from sese REGION to the newly created TO_REGION. TR is defined by
2847 DEF_STMT. GSI points to entry basic block of the TO_REGION. */
2848
2849 static void
2850 copy_def (tree tr, gimple *def_stmt, sese_info_p region, sese_info_p to_region,
2851 gimple_stmt_iterator *gsi)
2852 {
2853 if (!defined_in_sese_p (tr, region->region))
2854 return;
2855
2856 ssa_op_iter iter;
2857 use_operand_p use_p;
2858 FOR_EACH_SSA_USE_OPERAND (use_p, def_stmt, iter, SSA_OP_USE)
2859 {
2860 tree use_tr = USE_FROM_PTR (use_p);
2861
2862 /* Do not copy parameters that have been generated in the header of the
2863 scop. */
2864 if (region->parameter_rename_map->get(use_tr))
2865 continue;
2866
2867 gimple *def_of_use = SSA_NAME_DEF_STMT (use_tr);
2868 if (!def_of_use)
2869 continue;
2870
2871 copy_def (use_tr, def_of_use, region, to_region, gsi);
2872 }
2873
2874 gimple *copy = gimple_copy (def_stmt);
2875 gsi_insert_after (gsi, copy, GSI_NEW_STMT);
2876
2877 /* Create new names for all the definitions created by COPY and
2878 add replacement mappings for each new name. */
2879 def_operand_p def_p;
2880 ssa_op_iter op_iter;
2881 FOR_EACH_SSA_DEF_OPERAND (def_p, copy, op_iter, SSA_OP_ALL_DEFS)
2882 {
2883 tree old_name = DEF_FROM_PTR (def_p);
2884 tree new_name = create_new_def_for (old_name, copy, def_p);
2885 region->parameter_rename_map->put(old_name, new_name);
2886 }
2887
2888 update_stmt (copy);
2889 }
2890
2891 static void
2892 copy_internal_parameters (sese_info_p region, sese_info_p to_region)
2893 {
2894 /* For all the parameters which definitino is in the if_region->false_region,
2895 insert code on true_region (if_region->true_region->entry). */
2896
2897 int i;
2898 tree tr;
2899 gimple_stmt_iterator gsi = gsi_start_bb(to_region->region.entry->dest);
2900
2901 FOR_EACH_VEC_ELT (region->params, i, tr)
2902 {
2903 // If def is not in region.
2904 gimple *def_stmt = SSA_NAME_DEF_STMT (tr);
2905 if (def_stmt)
2906 copy_def (tr, def_stmt, region, to_region, &gsi);
2907 }
2908 }
2909
2910 /* GIMPLE Loop Generator: generates loops in GIMPLE form for the given SCOP.
2911 Return true if code generation succeeded. */
2912
2913 bool
2914 graphite_regenerate_ast_isl (scop_p scop)
2915 {
2916 sese_info_p region = scop->scop_info;
2917 translate_isl_ast_to_gimple t (region);
2918
2919 ifsese if_region = NULL;
2920 isl_ast_node *root_node;
2921 ivs_params ip;
2922
2923 timevar_push (TV_GRAPHITE_CODE_GEN);
2924 t.add_parameters_to_ivs_params (scop, ip);
2925 root_node = t.scop_to_isl_ast (scop);
2926
2927 if (dump_file && (dump_flags & TDF_DETAILS))
2928 {
2929 fprintf (dump_file, "[scheduler] original schedule:\n");
2930 print_isl_schedule (dump_file, scop->original_schedule);
2931 fprintf (dump_file, "[scheduler] isl transformed schedule:\n");
2932 print_isl_schedule (dump_file, scop->transformed_schedule);
2933
2934 fprintf (dump_file, "[scheduler] original ast:\n");
2935 print_schedule_ast (dump_file, scop->original_schedule, scop);
2936 fprintf (dump_file, "[scheduler] AST generated by isl:\n");
2937 print_isl_ast (dump_file, root_node);
2938 }
2939
2940 if_region = move_sese_in_condition (region);
2941 region->if_region = if_region;
2942
2943 loop_p context_loop = region->region.entry->src->loop_father;
2944
2945 /* Copy all the parameters which are defined in the region. */
2946 copy_internal_parameters(if_region->false_region, if_region->true_region);
2947
2948 edge e = single_succ_edge (if_region->true_region->region.entry->dest);
2949 basic_block bb = split_edge (e);
2950
2951 /* Update the true_region exit edge. */
2952 region->if_region->true_region->region.exit = single_succ_edge (bb);
2953
2954 t.translate_isl_ast (context_loop, root_node, e, ip);
2955 if (! t.codegen_error_p ())
2956 t.translate_pending_phi_nodes ();
2957 if (! t.codegen_error_p ())
2958 {
2959 sese_insert_phis_for_liveouts (region,
2960 if_region->region->region.exit->src,
2961 if_region->false_region->region.exit,
2962 if_region->true_region->region.exit);
2963 if (dump_file)
2964 fprintf (dump_file, "[codegen] isl AST to Gimple succeeded.\n");
2965
2966 mark_virtual_operands_for_renaming (cfun);
2967 update_ssa (TODO_update_ssa);
2968 }
2969
2970 if (t.codegen_error_p ())
2971 {
2972 if (dump_file)
2973 fprintf (dump_file, "codegen error: "
2974 "reverting back to the original code.\n");
2975 set_ifsese_condition (if_region, integer_zero_node);
2976
2977 /* We registered new names, scrap that. */
2978 if (need_ssa_update_p (cfun))
2979 delete_update_ssa ();
2980 /* Remove the unreachable region. */
2981 remove_edge_and_dominated_blocks (if_region->true_region->region.entry);
2982 basic_block ifb = if_region->false_region->region.entry->src;
2983 gimple_stmt_iterator gsi = gsi_last_bb (ifb);
2984 gsi_remove (&gsi, true);
2985 if_region->false_region->region.entry->flags &= ~EDGE_FALSE_VALUE;
2986 if_region->false_region->region.entry->flags |= EDGE_FALLTHRU;
2987 /* remove_edge_and_dominated_blocks marks loops for removal but
2988 doesn't actually remove them (fix that...). */
2989 loop_p loop;
2990 FOR_EACH_LOOP (loop, LI_FROM_INNERMOST)
2991 if (! loop->header)
2992 delete_loop (loop);
2993 }
2994
2995 /* Verifies properties that GRAPHITE should maintain during translation. */
2996 checking_verify_loop_structure ();
2997 checking_verify_loop_closed_ssa (true);
2998
2999 free (if_region->true_region);
3000 free (if_region->region);
3001 free (if_region);
3002
3003 ivs_params_clear (ip);
3004 isl_ast_node_free (root_node);
3005 timevar_pop (TV_GRAPHITE_CODE_GEN);
3006
3007 return !t.codegen_error_p ();
3008 }
3009
3010 #endif /* HAVE_isl */