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