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