S/390: Testsuite: Add asm scan patterns for -m31.
[official-gcc.git] / gcc / graphite-isl-ast-to-gimple.c
blob33423dd2bab56533bb82ed805c685225f857d1e0
1 /* Translation of ISL AST to Gimple.
2 Copyright (C) 2014-2015 Free Software Foundation, Inc.
3 Contributed by Roman Gareev <gareevroman@gmail.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #define USES_ISL
23 #include "config.h"
25 #ifdef HAVE_isl
27 #include "system.h"
28 #include "coretypes.h"
29 #include "backend.h"
30 #include "cfghooks.h"
31 #include "tree.h"
32 #include "gimple.h"
33 #include "params.h"
34 #include "fold-const.h"
35 #include "gimple-fold.h"
36 #include "gimple-iterator.h"
37 #include "gimplify.h"
38 #include "gimplify-me.h"
39 #include "tree-eh.h"
40 #include "tree-ssa-loop.h"
41 #include "tree-ssa-operands.h"
42 #include "tree-ssa-propagate.h"
43 #include "tree-pass.h"
44 #include "cfgloop.h"
45 #include "tree-data-ref.h"
46 #include "tree-ssa-loop-manip.h"
47 #include "tree-scalar-evolution.h"
48 #include "gimple-ssa.h"
49 #include "tree-phinodes.h"
50 #include "tree-into-ssa.h"
51 #include "ssa-iterators.h"
52 #include "tree-cfg.h"
53 #include "gimple-pretty-print.h"
54 #include "cfganal.h"
55 #include "value-prof.h"
57 #include <isl/constraint.h>
58 #include <isl/set.h>
59 #include <isl/union_set.h>
60 #include <isl/map.h>
61 #include <isl/union_map.h>
62 #include <isl/ast_build.h>
64 /* Since ISL-0.13, the extern is in val_gmp.h. */
65 #if !defined(HAVE_ISL_SCHED_CONSTRAINTS_COMPUTE_SCHEDULE) && defined(__cplusplus)
66 extern "C" {
67 #endif
68 #include <isl/val_gmp.h>
69 #if !defined(HAVE_ISL_SCHED_CONSTRAINTS_COMPUTE_SCHEDULE) && defined(__cplusplus)
71 #endif
73 #include "graphite.h"
75 #include <map>
77 /* We always try to use signed 128 bit types, but fall back to smaller types
78 in case a platform does not provide types of these sizes. In the future we
79 should use isl to derive the optimal type for each subexpression. */
81 static int max_mode_int_precision =
82 GET_MODE_PRECISION (mode_for_size (MAX_FIXED_MODE_SIZE, MODE_INT, 0));
83 static int graphite_expression_type_precision = 128 <= max_mode_int_precision ?
84 128 : max_mode_int_precision;
86 struct ast_build_info
88 ast_build_info()
89 : is_parallelizable(false)
90 { }
91 bool is_parallelizable;
94 /* Converts a GMP constant VAL to a tree and returns it. */
96 static tree
97 gmp_cst_to_tree (tree type, mpz_t val)
99 tree t = type ? type : integer_type_node;
100 mpz_t tmp;
102 mpz_init (tmp);
103 mpz_set (tmp, val);
104 wide_int wi = wi::from_mpz (t, tmp, true);
105 mpz_clear (tmp);
107 return wide_int_to_tree (t, wi);
110 /* Verifies properties that GRAPHITE should maintain during translation. */
112 static inline void
113 graphite_verify (void)
115 checking_verify_loop_structure ();
116 checking_verify_loop_closed_ssa (true);
119 /* IVS_PARAMS maps ISL's scattering and parameter identifiers
120 to corresponding trees. */
122 typedef std::map<isl_id *, tree> ivs_params;
124 /* Free all memory allocated for ISL's identifiers. */
126 void ivs_params_clear (ivs_params &ip)
128 std::map<isl_id *, tree>::iterator it;
129 for (it = ip.begin ();
130 it != ip.end (); it++)
132 isl_id_free (it->first);
136 class translate_isl_ast_to_gimple
138 public:
139 translate_isl_ast_to_gimple (sese_info_p r)
140 : region (r), codegen_error (false)
143 /* Translates an ISL AST node NODE to GCC representation in the
144 context of a SESE. */
145 edge translate_isl_ast (loop_p context_loop, __isl_keep isl_ast_node *node,
146 edge next_e, ivs_params &ip);
148 /* Translates an isl_ast_node_for to Gimple. */
149 edge translate_isl_ast_node_for (loop_p context_loop,
150 __isl_keep isl_ast_node *node,
151 edge next_e, ivs_params &ip);
153 /* Create the loop for a isl_ast_node_for.
155 - NEXT_E is the edge where new generated code should be attached. */
156 edge translate_isl_ast_for_loop (loop_p context_loop,
157 __isl_keep isl_ast_node *node_for,
158 edge next_e,
159 tree type, tree lb, tree ub,
160 ivs_params &ip);
162 /* Translates an isl_ast_node_if to Gimple. */
163 edge translate_isl_ast_node_if (loop_p context_loop,
164 __isl_keep isl_ast_node *node,
165 edge next_e, ivs_params &ip);
167 /* Translates an isl_ast_node_user to Gimple.
169 FIXME: We should remove iv_map.create (loop->num + 1), if it is
170 possible. */
171 edge translate_isl_ast_node_user (__isl_keep isl_ast_node *node,
172 edge next_e, ivs_params &ip);
174 /* Translates an isl_ast_node_block to Gimple. */
175 edge translate_isl_ast_node_block (loop_p context_loop,
176 __isl_keep isl_ast_node *node,
177 edge next_e, ivs_params &ip);
179 /* Converts a unary isl_ast_expr_op expression E to a GCC expression tree of
180 type TYPE. */
181 tree unary_op_to_tree (tree type, __isl_take isl_ast_expr *expr,
182 ivs_params &ip);
184 /* Converts a binary isl_ast_expr_op expression E to a GCC expression tree of
185 type TYPE. */
186 tree binary_op_to_tree (tree type, __isl_take isl_ast_expr *expr,
187 ivs_params &ip);
189 /* Converts a ternary isl_ast_expr_op expression E to a GCC expression tree of
190 type TYPE. */
191 tree ternary_op_to_tree (tree type, __isl_take isl_ast_expr *expr,
192 ivs_params &ip);
194 /* Converts an isl_ast_expr_op expression E with unknown number of arguments
195 to a GCC expression tree of type TYPE. */
196 tree nary_op_to_tree (tree type, __isl_take isl_ast_expr *expr,
197 ivs_params &ip);
199 /* Converts an ISL AST expression E back to a GCC expression tree of
200 type TYPE. */
201 tree gcc_expression_from_isl_expression (tree type,
202 __isl_take isl_ast_expr *,
203 ivs_params &ip);
205 /* Return the tree variable that corresponds to the given isl ast identifier
206 expression (an isl_ast_expr of type isl_ast_expr_id).
208 FIXME: We should replace blind conversation of id's type with derivation
209 of the optimal type when we get the corresponding isl support. Blindly
210 converting type sizes may be problematic when we switch to smaller
211 types. */
212 tree gcc_expression_from_isl_ast_expr_id (tree type,
213 __isl_keep isl_ast_expr *expr_id,
214 ivs_params &ip);
216 /* Converts an isl_ast_expr_int expression E to a GCC expression tree of
217 type TYPE. */
218 tree gcc_expression_from_isl_expr_int (tree type,
219 __isl_take isl_ast_expr *expr);
221 /* Converts an isl_ast_expr_op expression E to a GCC expression tree of
222 type TYPE. */
223 tree gcc_expression_from_isl_expr_op (tree type,
224 __isl_take isl_ast_expr *expr,
225 ivs_params &ip);
227 /* Creates a new LOOP corresponding to isl_ast_node_for. Inserts an
228 induction variable for the new LOOP. New LOOP is attached to CFG
229 starting at ENTRY_EDGE. LOOP is inserted into the loop tree and
230 becomes the child loop of the OUTER_LOOP. NEWIVS_INDEX binds
231 ISL's scattering name to the induction variable created for the
232 loop of STMT. The new induction variable is inserted in the NEWIVS
233 vector and is of type TYPE. */
234 struct loop *graphite_create_new_loop (edge entry_edge,
235 __isl_keep isl_ast_node *node_for,
236 loop_p outer, tree type,
237 tree lb, tree ub, ivs_params &ip);
239 /* All loops generated by create_empty_loop_on_edge have the form of
240 a post-test loop:
245 body of the loop;
246 } while (lower bound < upper bound);
248 We create a new if region protecting the loop to be executed, if
249 the execution count is zero (lower bound > upper bound). */
250 edge graphite_create_new_loop_guard (edge entry_edge,
251 __isl_keep isl_ast_node *node_for,
252 tree *type,
253 tree *lb, tree *ub, ivs_params &ip);
255 /* Creates a new if region corresponding to ISL's cond. */
256 edge graphite_create_new_guard (edge entry_edge,
257 __isl_take isl_ast_expr *if_cond,
258 ivs_params &ip);
260 /* Inserts in iv_map a tuple (OLD_LOOP->num, NEW_NAME) for the induction
261 variables of the loops around GBB in SESE.
263 FIXME: Instead of using a vec<tree> that maps each loop id to a possible
264 chrec, we could consider using a map<int, tree> that maps loop ids to the
265 corresponding tree expressions. */
266 void build_iv_mapping (vec<tree> iv_map, gimple_poly_bb_p gbb,
267 __isl_keep isl_ast_expr *user_expr, ivs_params &ip,
268 sese_l &region);
270 /* Patch the missing arguments of the phi nodes. */
272 void translate_pending_phi_nodes (void);
274 /* Add ISL's parameter identifiers and corresponding trees to ivs_params. */
276 void add_parameters_to_ivs_params (scop_p scop, ivs_params &ip);
278 /* Get the maximal number of schedule dimensions in the scop SCOP. */
280 int get_max_schedule_dimensions (scop_p scop);
282 /* Generates a build, which specifies the constraints on the parameters. */
284 __isl_give isl_ast_build *generate_isl_context (scop_p scop);
286 /* Extend the schedule to NB_SCHEDULE_DIMS schedule dimensions.
288 For schedules with different dimensionality, the isl AST generator can not
289 define an order and will just randomly choose an order. The solution to
290 this problem is to extend all schedules to the maximal number of schedule
291 dimensions (using '0's for the remaining values). */
293 __isl_give isl_map *extend_schedule (__isl_take isl_map *schedule,
294 int nb_schedule_dims);
296 /* Generates a schedule, which specifies an order used to
297 visit elements in a domain. */
299 __isl_give isl_union_map *generate_isl_schedule (scop_p scop);
301 /* Set the separate option for all dimensions.
302 This helps to reduce control overhead. */
304 __isl_give isl_ast_build * set_options (__isl_take isl_ast_build *control,
305 __isl_keep isl_union_map *schedule);
307 /* Generate isl AST from schedule of SCOP. Also, collects IVS_PARAMS in
308 IP. */
310 __isl_give isl_ast_node * scop_to_isl_ast (scop_p scop, ivs_params &ip);
313 /* Return true if RENAME (defined in BB) is a valid use in NEW_BB. The
314 definition should flow into use, and the use should respect the loop-closed
315 SSA form. */
317 bool is_valid_rename (tree rename, basic_block def_bb, basic_block use_bb,
318 bool loop_phi, tree old_name, basic_block old_bb) const;
320 /* Returns the expression associated to OLD_NAME (which is used in OLD_BB), in
321 NEW_BB from RENAME_MAP. LOOP_PHI is true when we want to rename OLD_NAME
322 within a loop PHI instruction. */
324 tree get_rename (basic_block new_bb, tree old_name,
325 basic_block old_bb, bool loop_phi) const;
327 /* For ops which are scev_analyzeable, we can regenerate a new name from
328 its scalar evolution around LOOP. */
330 tree get_rename_from_scev (tree old_name, gimple_seq *stmts, loop_p loop,
331 basic_block new_bb, basic_block old_bb,
332 vec<tree> iv_map);
334 /* Returns a basic block that could correspond to where a constant was defined
335 in the original code. In the original code OLD_BB had the definition, we
336 need to find which basic block out of the copies of old_bb, in the new
337 region, should a definition correspond to if it has to reach BB. */
339 basic_block get_def_bb_for_const (basic_block bb, basic_block old_bb) const;
341 /* Get the new name of OP (from OLD_BB) to be used in NEW_BB. LOOP_PHI is
342 true when we want to rename an OP within a loop PHI instruction. */
344 tree get_new_name (basic_block new_bb, tree op,
345 basic_block old_bb, bool loop_phi) const;
347 /* Collect all the operands of NEW_EXPR by recursively visiting each
348 operand. */
350 void collect_all_ssa_names (tree new_expr, vec<tree> *vec_ssa);
352 /* Copy the PHI arguments from OLD_PHI to the NEW_PHI. The arguments to
353 NEW_PHI must be found unless they can be POSTPONEd for later. */
355 bool copy_loop_phi_args (gphi *old_phi, init_back_edge_pair_t &ibp_old_bb,
356 gphi *new_phi, init_back_edge_pair_t &ibp_new_bb,
357 bool postpone);
359 /* Copy loop phi nodes from BB to NEW_BB. */
361 bool copy_loop_phi_nodes (basic_block bb, basic_block new_bb);
363 /* Add phi nodes to all merge points of all the diamonds enclosing the loop of
364 the close phi node PHI. */
366 bool add_close_phis_to_merge_points (gphi *old_phi, gphi *new_phi,
367 tree default_value);
369 tree add_close_phis_to_outer_loops (tree last_merge_name, edge merge_e,
370 gimple *old_close_phi);
372 /* Copy all the loop-close phi args from BB to NEW_BB. */
374 bool copy_loop_close_phi_args (basic_block old_bb, basic_block new_bb,
375 bool postpone);
377 /* Copy loop close phi nodes from BB to NEW_BB. */
379 bool copy_loop_close_phi_nodes (basic_block old_bb, basic_block new_bb);
381 /* Copy the arguments of cond-phi node PHI, to NEW_PHI in the codegenerated
382 region. If postpone is true and it isn't possible to copy any arg of PHI,
383 the PHI is added to the REGION->INCOMPLETE_PHIS to be codegenerated later.
384 Returns false if the copying was unsuccessful. */
386 bool copy_cond_phi_args (gphi *phi, gphi *new_phi, vec<tree> iv_map,
387 bool postpone);
389 /* Copy cond phi nodes from BB to NEW_BB. A cond-phi node is a basic block
390 containing phi nodes coming from two predecessors, and none of them are back
391 edges. */
393 bool copy_cond_phi_nodes (basic_block bb, basic_block new_bb,
394 vec<tree> iv_map);
396 /* Duplicates the statements of basic block BB into basic block NEW_BB
397 and compute the new induction variables according to the IV_MAP.
398 CODEGEN_ERROR is set when the code generation cannot continue. */
400 bool graphite_copy_stmts_from_block (basic_block bb, basic_block new_bb,
401 vec<tree> iv_map);
403 /* Copies BB and includes in the copied BB all the statements that can
404 be reached following the use-def chains from the memory accesses,
405 and returns the next edge following this new block. codegen_error is
406 set when the code generation cannot continue. */
408 edge copy_bb_and_scalar_dependences (basic_block bb, edge next_e,
409 vec<tree> iv_map);
411 /* Given a basic block containing close-phi it returns the new basic block
412 where to insert a copy of the close-phi nodes. All the uses in close phis
413 should come from a single loop otherwise it returns NULL. */
414 edge edge_for_new_close_phis (basic_block bb);
416 /* Add NEW_NAME as the ARGNUM-th arg of NEW_PHI which is in NEW_BB.
417 DOMINATING_PRED is the predecessor basic block of OLD_BB which dominates
418 the other pred of OLD_BB as well. If no such basic block exists then it is
419 NULL. NON_DOMINATING_PRED is a pred which does not dominate OLD_BB, it
420 cannot be NULL.
422 Case1: OLD_BB->preds {BB1, BB2} and BB1 does not dominate BB2 and vice
423 versa. In this case DOMINATING_PRED = NULL.
425 Case2: OLD_BB->preds {BB1, BB2} and BB1 dominates BB2.
427 Returns true on successful copy of the args, false otherwise. */
429 bool add_phi_arg_for_new_expr (tree old_phi_args[2], tree new_phi_args[2],
430 edge old_bb_dominating_edge,
431 edge old_bb_non_dominating_edge,
432 gphi *phi, gphi *new_phi,
433 basic_block new_bb);
435 /* Renames the scalar uses of the statement COPY, using the substitution map
436 RENAME_MAP, inserting the gimplification code at GSI_TGT, for the
437 translation REGION, with the original copied statement in LOOP, and using
438 the induction variable renaming map IV_MAP. Returns true when something
439 has been renamed. codegen_error is set when the code generation cannot
440 continue. */
442 bool rename_uses (gimple *copy, gimple_stmt_iterator *gsi_tgt,
443 basic_block old_bb, loop_p loop, vec<tree> iv_map);
445 /* Register in RENAME_MAP the rename tuple (OLD_NAME, EXPR).
446 When OLD_NAME and EXPR are the same we assert. */
448 void set_rename (tree old_name, tree expr);
450 /* Create new names for all the definitions created by COPY and add
451 replacement mappings for each new name. */
453 void set_rename_for_each_def (gimple *stmt);
455 /* Insert each statement from SEQ at its earliest insertion p. */
457 void gsi_insert_earliest (gimple_seq seq);
459 /* Rename all the operands of NEW_EXPR by recursively visiting each
460 operand. */
462 tree rename_all_uses (tree new_expr, basic_block new_bb, basic_block old_bb);
464 bool codegen_error_p () const
465 { return codegen_error; }
467 /* Prints NODE to FILE. */
469 void print_isl_ast_node (FILE *file, __isl_keep isl_ast_node *node,
470 __isl_keep isl_ctx *ctx) const;
472 /* Return true when OP is a constant tree. */
474 bool is_constant (tree op) const
476 return TREE_CODE (op) == INTEGER_CST
477 || TREE_CODE (op) == REAL_CST
478 || TREE_CODE (op) == COMPLEX_CST
479 || TREE_CODE (op) == VECTOR_CST;
482 private:
483 /* The region to be translated. */
484 sese_info_p region;
486 /* This flag is set when an error occurred during the translation of ISL AST
487 to Gimple. */
488 bool codegen_error;
490 /* A vector of all the edges at if_condition merge points. */
491 auto_vec<edge, 2> merge_points;
494 /* Return the tree variable that corresponds to the given isl ast identifier
495 expression (an isl_ast_expr of type isl_ast_expr_id).
497 FIXME: We should replace blind conversation of id's type with derivation
498 of the optimal type when we get the corresponding isl support. Blindly
499 converting type sizes may be problematic when we switch to smaller
500 types. */
502 tree
503 translate_isl_ast_to_gimple::
504 gcc_expression_from_isl_ast_expr_id (tree type,
505 __isl_keep isl_ast_expr *expr_id,
506 ivs_params &ip)
508 gcc_assert (isl_ast_expr_get_type (expr_id) == isl_ast_expr_id);
509 isl_id *tmp_isl_id = isl_ast_expr_get_id (expr_id);
510 std::map<isl_id *, tree>::iterator res;
511 res = ip.find (tmp_isl_id);
512 isl_id_free (tmp_isl_id);
513 gcc_assert (res != ip.end () &&
514 "Could not map isl_id to tree expression");
515 isl_ast_expr_free (expr_id);
516 tree t = res->second;
517 return fold_convert (type, t);
520 /* Converts an isl_ast_expr_int expression E to a GCC expression tree of
521 type TYPE. */
523 tree
524 translate_isl_ast_to_gimple::
525 gcc_expression_from_isl_expr_int (tree type, __isl_take isl_ast_expr *expr)
527 gcc_assert (isl_ast_expr_get_type (expr) == isl_ast_expr_int);
528 isl_val *val = isl_ast_expr_get_val (expr);
529 mpz_t val_mpz_t;
530 mpz_init (val_mpz_t);
531 tree res;
532 if (isl_val_get_num_gmp (val, val_mpz_t) == -1)
533 res = NULL_TREE;
534 else
535 res = gmp_cst_to_tree (type, val_mpz_t);
536 isl_val_free (val);
537 isl_ast_expr_free (expr);
538 mpz_clear (val_mpz_t);
539 return res;
542 /* Converts a binary isl_ast_expr_op expression E to a GCC expression tree of
543 type TYPE. */
545 tree
546 translate_isl_ast_to_gimple::
547 binary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
549 isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0);
550 tree tree_lhs_expr = gcc_expression_from_isl_expression (type, arg_expr, ip);
551 arg_expr = isl_ast_expr_get_op_arg (expr, 1);
552 tree tree_rhs_expr = gcc_expression_from_isl_expression (type, arg_expr, ip);
553 enum isl_ast_op_type expr_type = isl_ast_expr_get_op_type (expr);
554 isl_ast_expr_free (expr);
555 switch (expr_type)
557 case isl_ast_op_add:
558 return fold_build2 (PLUS_EXPR, type, tree_lhs_expr, tree_rhs_expr);
560 case isl_ast_op_sub:
561 return fold_build2 (MINUS_EXPR, type, tree_lhs_expr, tree_rhs_expr);
563 case isl_ast_op_mul:
564 return fold_build2 (MULT_EXPR, type, tree_lhs_expr, tree_rhs_expr);
566 case isl_ast_op_div:
567 return fold_build2 (EXACT_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr);
569 case isl_ast_op_pdiv_q:
570 return fold_build2 (TRUNC_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr);
572 case isl_ast_op_pdiv_r:
573 return fold_build2 (TRUNC_MOD_EXPR, type, tree_lhs_expr, tree_rhs_expr);
575 case isl_ast_op_fdiv_q:
576 return fold_build2 (FLOOR_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr);
578 case isl_ast_op_and:
579 return fold_build2 (TRUTH_ANDIF_EXPR, type,
580 tree_lhs_expr, tree_rhs_expr);
582 case isl_ast_op_or:
583 return fold_build2 (TRUTH_ORIF_EXPR, type, tree_lhs_expr, tree_rhs_expr);
585 case isl_ast_op_eq:
586 return fold_build2 (EQ_EXPR, type, tree_lhs_expr, tree_rhs_expr);
588 case isl_ast_op_le:
589 return fold_build2 (LE_EXPR, type, tree_lhs_expr, tree_rhs_expr);
591 case isl_ast_op_lt:
592 return fold_build2 (LT_EXPR, type, tree_lhs_expr, tree_rhs_expr);
594 case isl_ast_op_ge:
595 return fold_build2 (GE_EXPR, type, tree_lhs_expr, tree_rhs_expr);
597 case isl_ast_op_gt:
598 return fold_build2 (GT_EXPR, type, tree_lhs_expr, tree_rhs_expr);
600 default:
601 gcc_unreachable ();
605 /* Converts a ternary isl_ast_expr_op expression E to a GCC expression tree of
606 type TYPE. */
608 tree
609 translate_isl_ast_to_gimple::
610 ternary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
612 gcc_assert (isl_ast_expr_get_op_type (expr) == isl_ast_op_minus);
613 isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0);
614 tree tree_first_expr
615 = gcc_expression_from_isl_expression (type, arg_expr, ip);
616 arg_expr = isl_ast_expr_get_op_arg (expr, 1);
617 tree tree_second_expr
618 = gcc_expression_from_isl_expression (type, arg_expr, ip);
619 arg_expr = isl_ast_expr_get_op_arg (expr, 2);
620 tree tree_third_expr
621 = gcc_expression_from_isl_expression (type, arg_expr, ip);
622 isl_ast_expr_free (expr);
623 return fold_build3 (COND_EXPR, type, tree_first_expr,
624 tree_second_expr, tree_third_expr);
627 /* Converts a unary isl_ast_expr_op expression E to a GCC expression tree of
628 type TYPE. */
630 tree
631 translate_isl_ast_to_gimple::
632 unary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
634 gcc_assert (isl_ast_expr_get_op_type (expr) == isl_ast_op_minus);
635 isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0);
636 tree tree_expr = gcc_expression_from_isl_expression (type, arg_expr, ip);
637 isl_ast_expr_free (expr);
638 return fold_build1 (NEGATE_EXPR, type, tree_expr);
641 /* Converts an isl_ast_expr_op expression E with unknown number of arguments
642 to a GCC expression tree of type TYPE. */
644 tree
645 translate_isl_ast_to_gimple::
646 nary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
648 enum tree_code op_code;
649 switch (isl_ast_expr_get_op_type (expr))
651 case isl_ast_op_max:
652 op_code = MAX_EXPR;
653 break;
655 case isl_ast_op_min:
656 op_code = MIN_EXPR;
657 break;
659 default:
660 gcc_unreachable ();
662 isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0);
663 tree res = gcc_expression_from_isl_expression (type, arg_expr, ip);
664 int i;
665 for (i = 1; i < isl_ast_expr_get_op_n_arg (expr); i++)
667 arg_expr = isl_ast_expr_get_op_arg (expr, i);
668 tree t = gcc_expression_from_isl_expression (type, arg_expr, ip);
669 res = fold_build2 (op_code, type, res, t);
671 isl_ast_expr_free (expr);
672 return res;
675 /* Converts an isl_ast_expr_op expression E to a GCC expression tree of
676 type TYPE. */
678 tree
679 translate_isl_ast_to_gimple::
680 gcc_expression_from_isl_expr_op (tree type, __isl_take isl_ast_expr *expr,
681 ivs_params &ip)
683 gcc_assert (isl_ast_expr_get_type (expr) == isl_ast_expr_op);
684 switch (isl_ast_expr_get_op_type (expr))
686 /* These isl ast expressions are not supported yet. */
687 case isl_ast_op_error:
688 case isl_ast_op_call:
689 case isl_ast_op_and_then:
690 case isl_ast_op_or_else:
691 case isl_ast_op_select:
692 gcc_unreachable ();
694 case isl_ast_op_max:
695 case isl_ast_op_min:
696 return nary_op_to_tree (type, expr, ip);
698 case isl_ast_op_add:
699 case isl_ast_op_sub:
700 case isl_ast_op_mul:
701 case isl_ast_op_div:
702 case isl_ast_op_pdiv_q:
703 case isl_ast_op_pdiv_r:
704 case isl_ast_op_fdiv_q:
705 case isl_ast_op_and:
706 case isl_ast_op_or:
707 case isl_ast_op_eq:
708 case isl_ast_op_le:
709 case isl_ast_op_lt:
710 case isl_ast_op_ge:
711 case isl_ast_op_gt:
712 return binary_op_to_tree (type, expr, ip);
714 case isl_ast_op_minus:
715 return unary_op_to_tree (type, expr, ip);
717 case isl_ast_op_cond:
718 return ternary_op_to_tree (type, expr, ip);
720 default:
721 gcc_unreachable ();
724 return NULL_TREE;
727 /* Converts an ISL AST expression E back to a GCC expression tree of
728 type TYPE. */
730 tree
731 translate_isl_ast_to_gimple::
732 gcc_expression_from_isl_expression (tree type, __isl_take isl_ast_expr *expr,
733 ivs_params &ip)
735 switch (isl_ast_expr_get_type (expr))
737 case isl_ast_expr_id:
738 return gcc_expression_from_isl_ast_expr_id (type, expr, ip);
740 case isl_ast_expr_int:
741 return gcc_expression_from_isl_expr_int (type, expr);
743 case isl_ast_expr_op:
744 return gcc_expression_from_isl_expr_op (type, expr, ip);
746 default:
747 gcc_unreachable ();
750 return NULL_TREE;
753 /* Creates a new LOOP corresponding to isl_ast_node_for. Inserts an
754 induction variable for the new LOOP. New LOOP is attached to CFG
755 starting at ENTRY_EDGE. LOOP is inserted into the loop tree and
756 becomes the child loop of the OUTER_LOOP. NEWIVS_INDEX binds
757 ISL's scattering name to the induction variable created for the
758 loop of STMT. The new induction variable is inserted in the NEWIVS
759 vector and is of type TYPE. */
761 struct loop *
762 translate_isl_ast_to_gimple::
763 graphite_create_new_loop (edge entry_edge, __isl_keep isl_ast_node *node_for,
764 loop_p outer, tree type, tree lb, tree ub,
765 ivs_params &ip)
767 isl_ast_expr *for_inc = isl_ast_node_for_get_inc (node_for);
768 tree stride = gcc_expression_from_isl_expression (type, for_inc, ip);
769 tree ivvar = create_tmp_var (type, "graphite_IV");
770 tree iv, iv_after_increment;
771 loop_p loop = create_empty_loop_on_edge
772 (entry_edge, lb, stride, ub, ivvar, &iv, &iv_after_increment,
773 outer ? outer : entry_edge->src->loop_father);
775 isl_ast_expr *for_iterator = isl_ast_node_for_get_iterator (node_for);
776 isl_id *id = isl_ast_expr_get_id (for_iterator);
777 std::map<isl_id *, tree>::iterator res;
778 res = ip.find (id);
779 if (ip.count (id))
780 isl_id_free (res->first);
781 ip[id] = iv;
782 isl_ast_expr_free (for_iterator);
783 return loop;
786 /* Create the loop for a isl_ast_node_for.
788 - NEXT_E is the edge where new generated code should be attached. */
790 edge
791 translate_isl_ast_to_gimple::
792 translate_isl_ast_for_loop (loop_p context_loop,
793 __isl_keep isl_ast_node *node_for, edge next_e,
794 tree type, tree lb, tree ub,
795 ivs_params &ip)
797 gcc_assert (isl_ast_node_get_type (node_for) == isl_ast_node_for);
798 struct loop *loop = graphite_create_new_loop (next_e, node_for, context_loop,
799 type, lb, ub, ip);
800 edge last_e = single_exit (loop);
801 edge to_body = single_succ_edge (loop->header);
802 basic_block after = to_body->dest;
804 /* Translate the body of the loop. */
805 isl_ast_node *for_body = isl_ast_node_for_get_body (node_for);
806 next_e = translate_isl_ast (loop, for_body, to_body, ip);
807 isl_ast_node_free (for_body);
809 /* Early return if we failed to translate loop body. */
810 if (!next_e || codegen_error_p ())
811 return NULL;
813 if (next_e->dest != after)
814 redirect_edge_succ_nodup (next_e, after);
815 set_immediate_dominator (CDI_DOMINATORS, next_e->dest, next_e->src);
817 if (flag_loop_parallelize_all)
819 isl_id *id = isl_ast_node_get_annotation (node_for);
820 gcc_assert (id);
821 ast_build_info *for_info = (ast_build_info *) isl_id_get_user (id);
822 loop->can_be_parallel = for_info->is_parallelizable;
823 free (for_info);
824 isl_id_free (id);
827 return last_e;
830 /* We use this function to get the upper bound because of the form,
831 which is used by isl to represent loops:
833 for (iterator = init; cond; iterator += inc)
841 The loop condition is an arbitrary expression, which contains the
842 current loop iterator.
844 (e.g. iterator + 3 < B && C > iterator + A)
846 We have to know the upper bound of the iterator to generate a loop
847 in Gimple form. It can be obtained from the special representation
848 of the loop condition, which is generated by isl,
849 if the ast_build_atomic_upper_bound option is set. In this case,
850 isl generates a loop condition that consists of the current loop
851 iterator, + an operator (< or <=) and an expression not involving
852 the iterator, which is processed and returned by this function.
854 (e.g iterator <= upper-bound-expression-without-iterator) */
856 static __isl_give isl_ast_expr *
857 get_upper_bound (__isl_keep isl_ast_node *node_for)
859 gcc_assert (isl_ast_node_get_type (node_for) == isl_ast_node_for);
860 isl_ast_expr *for_cond = isl_ast_node_for_get_cond (node_for);
861 gcc_assert (isl_ast_expr_get_type (for_cond) == isl_ast_expr_op);
862 isl_ast_expr *res;
863 switch (isl_ast_expr_get_op_type (for_cond))
865 case isl_ast_op_le:
866 res = isl_ast_expr_get_op_arg (for_cond, 1);
867 break;
869 case isl_ast_op_lt:
871 /* (iterator < ub) => (iterator <= ub - 1). */
872 isl_val *one =
873 isl_val_int_from_si (isl_ast_expr_get_ctx (for_cond), 1);
874 isl_ast_expr *ub = isl_ast_expr_get_op_arg (for_cond, 1);
875 res = isl_ast_expr_sub (ub, isl_ast_expr_from_val (one));
876 break;
879 default:
880 gcc_unreachable ();
882 isl_ast_expr_free (for_cond);
883 return res;
886 /* All loops generated by create_empty_loop_on_edge have the form of
887 a post-test loop:
892 body of the loop;
893 } while (lower bound < upper bound);
895 We create a new if region protecting the loop to be executed, if
896 the execution count is zero (lower bound > upper bound). */
898 edge
899 translate_isl_ast_to_gimple::
900 graphite_create_new_loop_guard (edge entry_edge,
901 __isl_keep isl_ast_node *node_for, tree *type,
902 tree *lb, tree *ub, ivs_params &ip)
904 gcc_assert (isl_ast_node_get_type (node_for) == isl_ast_node_for);
905 tree cond_expr;
906 edge exit_edge;
908 *type =
909 build_nonstandard_integer_type (graphite_expression_type_precision, 0);
910 isl_ast_expr *for_init = isl_ast_node_for_get_init (node_for);
911 *lb = gcc_expression_from_isl_expression (*type, for_init, ip);
912 isl_ast_expr *upper_bound = get_upper_bound (node_for);
913 *ub = gcc_expression_from_isl_expression (*type, upper_bound, ip);
915 /* When ub is simply a constant or a parameter, use lb <= ub. */
916 if (TREE_CODE (*ub) == INTEGER_CST || TREE_CODE (*ub) == SSA_NAME)
917 cond_expr = fold_build2 (LE_EXPR, boolean_type_node, *lb, *ub);
918 else
920 tree one = (POINTER_TYPE_P (*type)
921 ? convert_to_ptrofftype (integer_one_node)
922 : fold_convert (*type, integer_one_node));
923 /* Adding +1 and using LT_EXPR helps with loop latches that have a
924 loop iteration count of "PARAMETER - 1". For PARAMETER == 0 this
925 becomes 2^k-1 due to integer overflow, and the condition lb <= ub
926 is true, even if we do not want this. However lb < ub + 1 is false,
927 as expected. */
928 tree ub_one = fold_build2 (POINTER_TYPE_P (*type) ? POINTER_PLUS_EXPR
929 : PLUS_EXPR, *type, *ub, one);
931 cond_expr = fold_build2 (LT_EXPR, boolean_type_node, *lb, ub_one);
934 if (integer_onep (cond_expr))
935 exit_edge = entry_edge;
936 else
937 exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr);
939 return exit_edge;
942 /* Translates an isl_ast_node_for to Gimple. */
944 edge
945 translate_isl_ast_to_gimple::
946 translate_isl_ast_node_for (loop_p context_loop, __isl_keep isl_ast_node *node,
947 edge next_e, ivs_params &ip)
949 gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_for);
950 tree type, lb, ub;
951 edge last_e = graphite_create_new_loop_guard (next_e, node, &type,
952 &lb, &ub, ip);
954 if (last_e == next_e)
956 /* There was no guard generated. */
957 last_e = single_succ_edge (split_edge (last_e));
959 translate_isl_ast_for_loop (context_loop, node, next_e,
960 type, lb, ub, ip);
961 return last_e;
964 edge true_e = get_true_edge_from_guard_bb (next_e->dest);
965 merge_points.safe_push (last_e);
967 last_e = single_succ_edge (split_edge (last_e));
968 translate_isl_ast_for_loop (context_loop, node, true_e, type, lb, ub, ip);
970 return last_e;
973 /* Inserts in iv_map a tuple (OLD_LOOP->num, NEW_NAME) for the induction
974 variables of the loops around GBB in SESE.
976 FIXME: Instead of using a vec<tree> that maps each loop id to a possible
977 chrec, we could consider using a map<int, tree> that maps loop ids to the
978 corresponding tree expressions. */
980 void
981 translate_isl_ast_to_gimple::
982 build_iv_mapping (vec<tree> iv_map, gimple_poly_bb_p gbb,
983 __isl_keep isl_ast_expr *user_expr, ivs_params &ip,
984 sese_l &region)
986 gcc_assert (isl_ast_expr_get_type (user_expr) == isl_ast_expr_op &&
987 isl_ast_expr_get_op_type (user_expr) == isl_ast_op_call);
988 int i;
989 isl_ast_expr *arg_expr;
990 for (i = 1; i < isl_ast_expr_get_op_n_arg (user_expr); i++)
992 arg_expr = isl_ast_expr_get_op_arg (user_expr, i);
993 tree type =
994 build_nonstandard_integer_type (graphite_expression_type_precision, 0);
995 tree t = gcc_expression_from_isl_expression (type, arg_expr, ip);
996 loop_p old_loop = gbb_loop_at_index (gbb, region, i - 1);
997 iv_map[old_loop->num] = t;
1001 /* Translates an isl_ast_node_user to Gimple.
1003 FIXME: We should remove iv_map.create (loop->num + 1), if it is possible. */
1005 edge
1006 translate_isl_ast_to_gimple::
1007 translate_isl_ast_node_user (__isl_keep isl_ast_node *node,
1008 edge next_e, ivs_params &ip)
1010 gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_user);
1012 isl_ast_expr *user_expr = isl_ast_node_user_get_expr (node);
1013 isl_ast_expr *name_expr = isl_ast_expr_get_op_arg (user_expr, 0);
1014 gcc_assert (isl_ast_expr_get_type (name_expr) == isl_ast_expr_id);
1016 isl_id *name_id = isl_ast_expr_get_id (name_expr);
1017 poly_bb_p pbb = (poly_bb_p) isl_id_get_user (name_id);
1018 gcc_assert (pbb);
1020 gimple_poly_bb_p gbb = PBB_BLACK_BOX (pbb);
1022 isl_ast_expr_free (name_expr);
1023 isl_id_free (name_id);
1025 gcc_assert (GBB_BB (gbb) != ENTRY_BLOCK_PTR_FOR_FN (cfun) &&
1026 "The entry block should not even appear within a scop");
1028 const int nb_loops = number_of_loops (cfun);
1029 vec<tree> iv_map;
1030 iv_map.create (nb_loops);
1031 iv_map.safe_grow_cleared (nb_loops);
1033 build_iv_mapping (iv_map, gbb, user_expr, ip, pbb->scop->scop_info->region);
1034 isl_ast_expr_free (user_expr);
1036 if (dump_file)
1038 fprintf (dump_file, "[codegen] copying from basic block\n");
1039 print_loops_bb (dump_file, GBB_BB (gbb), 0, 3);
1040 fprintf (dump_file, "[codegen] to new basic block\n");
1041 print_loops_bb (dump_file, next_e->src, 0, 3);
1044 next_e = copy_bb_and_scalar_dependences (GBB_BB (gbb), next_e,
1045 iv_map);
1047 iv_map.release ();
1049 if (codegen_error_p ())
1050 return NULL;
1052 if (dump_file)
1054 fprintf (dump_file, "[codegen] (after copy) new basic block\n");
1055 print_loops_bb (dump_file, next_e->src, 0, 3);
1058 return next_e;
1061 /* Translates an isl_ast_node_block to Gimple. */
1063 edge
1064 translate_isl_ast_to_gimple::
1065 translate_isl_ast_node_block (loop_p context_loop,
1066 __isl_keep isl_ast_node *node,
1067 edge next_e, ivs_params &ip)
1069 gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_block);
1070 isl_ast_node_list *node_list = isl_ast_node_block_get_children (node);
1071 int i;
1072 for (i = 0; i < isl_ast_node_list_n_ast_node (node_list); i++)
1074 isl_ast_node *tmp_node = isl_ast_node_list_get_ast_node (node_list, i);
1075 next_e = translate_isl_ast (context_loop, tmp_node, next_e, ip);
1076 isl_ast_node_free (tmp_node);
1078 isl_ast_node_list_free (node_list);
1079 return next_e;
1082 /* Creates a new if region corresponding to ISL's cond. */
1084 edge
1085 translate_isl_ast_to_gimple::
1086 graphite_create_new_guard (edge entry_edge, __isl_take isl_ast_expr *if_cond,
1087 ivs_params &ip)
1089 tree type =
1090 build_nonstandard_integer_type (graphite_expression_type_precision, 0);
1091 tree cond_expr = gcc_expression_from_isl_expression (type, if_cond, ip);
1092 edge exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr);
1093 return exit_edge;
1096 /* Translates an isl_ast_node_if to Gimple. */
1098 edge
1099 translate_isl_ast_to_gimple::
1100 translate_isl_ast_node_if (loop_p context_loop,
1101 __isl_keep isl_ast_node *node,
1102 edge next_e, ivs_params &ip)
1104 gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_if);
1105 isl_ast_expr *if_cond = isl_ast_node_if_get_cond (node);
1106 edge last_e = graphite_create_new_guard (next_e, if_cond, ip);
1107 edge true_e = get_true_edge_from_guard_bb (next_e->dest);
1108 merge_points.safe_push (last_e);
1110 isl_ast_node *then_node = isl_ast_node_if_get_then (node);
1111 translate_isl_ast (context_loop, then_node, true_e, ip);
1112 isl_ast_node_free (then_node);
1114 edge false_e = get_false_edge_from_guard_bb (next_e->dest);
1115 isl_ast_node *else_node = isl_ast_node_if_get_else (node);
1116 if (isl_ast_node_get_type (else_node) != isl_ast_node_error)
1117 translate_isl_ast (context_loop, else_node, false_e, ip);
1119 isl_ast_node_free (else_node);
1120 return last_e;
1123 /* Translates an ISL AST node NODE to GCC representation in the
1124 context of a SESE. */
1126 edge
1127 translate_isl_ast_to_gimple::translate_isl_ast (loop_p context_loop,
1128 __isl_keep isl_ast_node *node,
1129 edge next_e, ivs_params &ip)
1131 if (codegen_error_p ())
1132 return NULL;
1134 switch (isl_ast_node_get_type (node))
1136 case isl_ast_node_error:
1137 gcc_unreachable ();
1139 case isl_ast_node_for:
1140 return translate_isl_ast_node_for (context_loop, node,
1141 next_e, ip);
1143 case isl_ast_node_if:
1144 return translate_isl_ast_node_if (context_loop, node,
1145 next_e, ip);
1147 case isl_ast_node_user:
1148 return translate_isl_ast_node_user (node, next_e, ip);
1150 case isl_ast_node_block:
1151 return translate_isl_ast_node_block (context_loop, node,
1152 next_e, ip);
1154 default:
1155 gcc_unreachable ();
1159 /* Return true when BB contains loop close phi nodes. A loop close phi node is
1160 at the exit of loop which takes one argument that is the last value of the
1161 variable being used out of the loop. */
1163 static bool
1164 bb_contains_loop_close_phi_nodes (basic_block bb)
1166 return single_pred_p (bb)
1167 && bb->loop_father != single_pred_edge (bb)->src->loop_father;
1170 /* Return true when BB contains loop phi nodes. A loop phi node is the loop
1171 header containing phi nodes which has one init-edge and one back-edge. */
1173 static bool
1174 bb_contains_loop_phi_nodes (basic_block bb)
1176 gcc_assert (EDGE_COUNT (bb->preds) <= 2);
1178 if (bb->preds->length () == 1)
1179 return false;
1181 unsigned depth = loop_depth (bb->loop_father);
1183 edge preds[2] = { (*bb->preds)[0], (*bb->preds)[1] };
1185 if (depth > loop_depth (preds[0]->src->loop_father)
1186 || depth > loop_depth (preds[1]->src->loop_father))
1187 return true;
1189 /* When one of the edges correspond to the same loop father and other
1190 doesn't. */
1191 if (bb->loop_father != preds[0]->src->loop_father
1192 && bb->loop_father == preds[1]->src->loop_father)
1193 return true;
1195 if (bb->loop_father != preds[1]->src->loop_father
1196 && bb->loop_father == preds[0]->src->loop_father)
1197 return true;
1199 return false;
1202 /* Check if USE is defined in a basic block from where the definition of USE can
1203 propagate from all the paths. FIXME: Verify checks for virtual operands. */
1205 static bool
1206 is_loop_closed_ssa_use (basic_block bb, tree use)
1208 if (TREE_CODE (use) != SSA_NAME || virtual_operand_p (use))
1209 return true;
1211 /* For close-phi nodes def always comes from a loop which has a back-edge. */
1212 if (bb_contains_loop_close_phi_nodes (bb))
1213 return true;
1215 gimple *def = SSA_NAME_DEF_STMT (use);
1216 basic_block def_bb = gimple_bb (def);
1217 return (!def_bb
1218 || flow_bb_inside_loop_p (def_bb->loop_father, bb));
1221 /* Return the number of phi nodes in BB. */
1223 static int
1224 number_of_phi_nodes (basic_block bb)
1226 int num_phis = 0;
1227 for (gphi_iterator psi = gsi_start_phis (bb); !gsi_end_p (psi);
1228 gsi_next (&psi))
1229 num_phis++;
1230 return num_phis;
1233 /* Returns true if BB uses name in one of its PHIs. */
1235 static bool
1236 phi_uses_name (basic_block bb, tree name)
1238 for (gphi_iterator psi = gsi_start_phis (bb); !gsi_end_p (psi);
1239 gsi_next (&psi))
1241 gphi *phi = psi.phi ();
1242 for (unsigned i = 0; i < gimple_phi_num_args (phi); i++)
1244 tree use_arg = gimple_phi_arg_def (phi, i);
1245 if (use_arg == name)
1246 return true;
1249 return false;
1252 /* Return true if RENAME (defined in BB) is a valid use in NEW_BB. The
1253 definition should flow into use, and the use should respect the loop-closed
1254 SSA form. */
1256 bool
1257 translate_isl_ast_to_gimple::
1258 is_valid_rename (tree rename, basic_block def_bb, basic_block use_bb,
1259 bool loop_phi, tree old_name, basic_block old_bb) const
1261 /* The def of the rename must either dominate the uses or come from a
1262 back-edge. Also the def must respect the loop closed ssa form. */
1263 if (!is_loop_closed_ssa_use (use_bb, rename))
1265 if (dump_file)
1267 fprintf (dump_file, "[codegen] rename not in loop closed ssa:");
1268 print_generic_expr (dump_file, rename, 0);
1269 fprintf (dump_file, "\n");
1271 return false;
1274 if (dominated_by_p (CDI_DOMINATORS, use_bb, def_bb))
1275 return true;
1277 if (bb_contains_loop_phi_nodes (use_bb) && loop_phi)
1279 /* The loop-header dominates the loop-body. */
1280 if (!dominated_by_p (CDI_DOMINATORS, def_bb, use_bb))
1281 return false;
1283 /* RENAME would be used in loop-phi. */
1284 gcc_assert (number_of_phi_nodes (use_bb));
1286 /* For definitions coming from back edges, we should check that
1287 old_name is used in a loop PHI node.
1288 FIXME: Verify if this is true. */
1289 if (phi_uses_name (old_bb, old_name))
1290 return true;
1292 return false;
1295 /* Returns the expression associated to OLD_NAME (which is used in OLD_BB), in
1296 NEW_BB from RENAME_MAP. LOOP_PHI is true when we want to rename OLD_NAME
1297 within a loop PHI instruction. */
1299 tree
1300 translate_isl_ast_to_gimple::get_rename (basic_block new_bb,
1301 tree old_name,
1302 basic_block old_bb,
1303 bool loop_phi) const
1305 gcc_assert (TREE_CODE (old_name) == SSA_NAME);
1306 vec <tree> *renames = region->rename_map->get (old_name);
1308 if (!renames || renames->is_empty ())
1309 return NULL_TREE;
1311 if (1 == renames->length ())
1313 tree rename = (*renames)[0];
1314 if (TREE_CODE (rename) == SSA_NAME)
1316 basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (rename));
1317 if (is_valid_rename (rename, bb, new_bb, loop_phi, old_name, old_bb))
1318 return rename;
1319 return NULL_TREE;
1322 if (is_constant (rename))
1323 return rename;
1325 return NULL_TREE;
1328 /* More than one renames corresponding to the old_name. Find the rename for
1329 which the definition flows into usage at new_bb. */
1330 int i;
1331 tree t1 = NULL_TREE, t2;
1332 basic_block t1_bb = NULL;
1333 FOR_EACH_VEC_ELT (*renames, i, t2)
1335 basic_block t2_bb = gimple_bb (SSA_NAME_DEF_STMT (t2));
1337 /* Defined in the same basic block as used. */
1338 if (t2_bb == new_bb)
1339 return t2;
1341 /* NEW_BB and T2_BB are in two unrelated if-clauses. */
1342 if (!dominated_by_p (CDI_DOMINATORS, new_bb, t2_bb))
1343 continue;
1345 /* Compute the nearest dominator. */
1346 if (!t1 || dominated_by_p (CDI_DOMINATORS, t2_bb, t1_bb))
1348 t1_bb = t2_bb;
1349 t1 = t2;
1353 return t1;
1356 /* Register in RENAME_MAP the rename tuple (OLD_NAME, EXPR).
1357 When OLD_NAME and EXPR are the same we assert. */
1359 void
1360 translate_isl_ast_to_gimple::set_rename (tree old_name, tree expr)
1362 if (dump_file)
1364 fprintf (dump_file, "[codegen] setting rename: old_name = ");
1365 print_generic_expr (dump_file, old_name, 0);
1366 fprintf (dump_file, ", new_name = ");
1367 print_generic_expr (dump_file, expr, 0);
1368 fprintf (dump_file, "\n");
1371 if (old_name == expr)
1372 return;
1374 vec <tree> *renames = region->rename_map->get (old_name);
1376 if (renames)
1377 renames->safe_push (expr);
1378 else
1380 vec<tree> r;
1381 r.create (2);
1382 r.safe_push (expr);
1383 region->rename_map->put (old_name, r);
1387 /* Return an iterator to the instructions comes last in the execution order.
1388 Either GSI1 and GSI2 should belong to the same basic block or one of their
1389 respective basic blocks should dominate the other. */
1391 gimple_stmt_iterator
1392 later_of_the_two (gimple_stmt_iterator gsi1, gimple_stmt_iterator gsi2)
1394 basic_block bb1 = gsi_bb (gsi1);
1395 basic_block bb2 = gsi_bb (gsi2);
1397 /* Find the iterator which is the latest. */
1398 if (bb1 == bb2)
1400 /* For empty basic blocks gsis point to the end of the sequence. Since
1401 there is no operator== defined for gimple_stmt_iterator and for gsis
1402 not pointing to a valid statement gsi_next would assert. */
1403 gimple_stmt_iterator gsi = gsi1;
1404 do {
1405 if (gsi_stmt (gsi) == gsi_stmt (gsi2))
1406 return gsi2;
1407 gsi_next (&gsi);
1408 } while (!gsi_end_p (gsi));
1410 return gsi1;
1413 /* Find the basic block closest to the basic block which defines stmt. */
1414 if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
1415 return gsi1;
1417 gcc_assert (dominated_by_p (CDI_DOMINATORS, bb2, bb1));
1418 return gsi2;
1421 /* Insert each statement from SEQ at its earliest insertion p. */
1423 void
1424 translate_isl_ast_to_gimple::gsi_insert_earliest (gimple_seq seq)
1426 update_modified_stmts (seq);
1427 sese_l &codegen_region = region->if_region->true_region->region;
1428 basic_block begin_bb = get_entry_bb (codegen_region);
1430 /* Inserting the gimple statements in a vector because gimple_seq behave
1431 in strage ways when inserting the stmts from it into different basic
1432 blocks one at a time. */
1433 auto_vec<gimple *, 3> stmts;
1434 for (gimple_stmt_iterator gsi = gsi_start (seq); !gsi_end_p (gsi);
1435 gsi_next (&gsi))
1436 stmts.safe_push (gsi_stmt (gsi));
1438 int i;
1439 gimple *use_stmt;
1440 FOR_EACH_VEC_ELT (stmts, i, use_stmt)
1442 gcc_assert (gimple_code (use_stmt) != GIMPLE_PHI);
1443 gimple_stmt_iterator gsi_def_stmt = gsi_start_bb_nondebug (begin_bb);
1445 use_operand_p use_p;
1446 ssa_op_iter op_iter;
1447 FOR_EACH_SSA_USE_OPERAND (use_p, use_stmt, op_iter, SSA_OP_USE)
1449 /* Iterator to the current def of use_p. For function parameters or
1450 anything where def is not found, insert at the beginning of the
1451 generated region. */
1452 gimple_stmt_iterator gsi_stmt = gsi_def_stmt;
1454 tree op = USE_FROM_PTR (use_p);
1455 gimple *stmt = SSA_NAME_DEF_STMT (op);
1456 if (stmt && (gimple_code (stmt) != GIMPLE_NOP))
1457 gsi_stmt = gsi_for_stmt (stmt);
1459 /* For region parameters, insert at the beginning of the generated
1460 region. */
1461 if (!bb_in_sese_p (gsi_bb (gsi_stmt), codegen_region))
1462 gsi_stmt = gsi_def_stmt;
1464 gsi_def_stmt = later_of_the_two (gsi_stmt, gsi_def_stmt);
1467 if (!gsi_stmt (gsi_def_stmt))
1469 gimple_stmt_iterator gsi = gsi_after_labels (gsi_bb (gsi_def_stmt));
1470 gsi_insert_before (&gsi, use_stmt, GSI_NEW_STMT);
1472 else if (gimple_code (gsi_stmt (gsi_def_stmt)) == GIMPLE_PHI)
1474 gimple_stmt_iterator bsi
1475 = gsi_start_bb_nondebug (gsi_bb (gsi_def_stmt));
1476 /* Insert right after the PHI statements. */
1477 gsi_insert_before (&bsi, use_stmt, GSI_NEW_STMT);
1479 else
1480 gsi_insert_after (&gsi_def_stmt, use_stmt, GSI_NEW_STMT);
1482 if (dump_file)
1484 fprintf (dump_file, "[codegen] inserting statement: ");
1485 print_gimple_stmt (dump_file, use_stmt, 0, TDF_VOPS | TDF_MEMSYMS);
1486 print_loops_bb (dump_file, gimple_bb (use_stmt), 0, 3);
1491 /* Collect all the operands of NEW_EXPR by recursively visiting each
1492 operand. */
1494 void
1495 translate_isl_ast_to_gimple::collect_all_ssa_names (tree new_expr,
1496 vec<tree> *vec_ssa)
1499 /* Rename all uses in new_expr. */
1500 if (TREE_CODE (new_expr) == SSA_NAME)
1502 vec_ssa->safe_push (new_expr);
1503 return;
1506 /* Iterate over SSA_NAMES in NEW_EXPR. */
1507 for (int i = 0; i < (TREE_CODE_LENGTH (TREE_CODE (new_expr))); i++)
1509 tree op = TREE_OPERAND (new_expr, i);
1510 collect_all_ssa_names (op, vec_ssa);
1514 /* This is abridged version of the function:
1515 tree.c:substitute_in_expr (tree exp, tree f, tree r). */
1517 static tree
1518 substitute_ssa_name (tree exp, tree f, tree r)
1520 enum tree_code code = TREE_CODE (exp);
1521 tree op0, op1, op2, op3;
1522 tree new_tree;
1524 /* We handle TREE_LIST and COMPONENT_REF separately. */
1525 if (code == TREE_LIST)
1527 op0 = substitute_ssa_name (TREE_CHAIN (exp), f, r);
1528 op1 = substitute_ssa_name (TREE_VALUE (exp), f, r);
1529 if (op0 == TREE_CHAIN (exp) && op1 == TREE_VALUE (exp))
1530 return exp;
1532 return tree_cons (TREE_PURPOSE (exp), op1, op0);
1534 else if (code == COMPONENT_REF)
1536 tree inner;
1538 /* If this expression is getting a value from a PLACEHOLDER_EXPR
1539 and it is the right field, replace it with R. */
1540 for (inner = TREE_OPERAND (exp, 0);
1541 REFERENCE_CLASS_P (inner);
1542 inner = TREE_OPERAND (inner, 0))
1545 /* The field. */
1546 op1 = TREE_OPERAND (exp, 1);
1548 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && op1 == f)
1549 return r;
1551 /* If this expression hasn't been completed let, leave it alone. */
1552 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && !TREE_TYPE (inner))
1553 return exp;
1555 op0 = substitute_ssa_name (TREE_OPERAND (exp, 0), f, r);
1556 if (op0 == TREE_OPERAND (exp, 0))
1557 return exp;
1559 new_tree
1560 = fold_build3 (COMPONENT_REF, TREE_TYPE (exp), op0, op1, NULL_TREE);
1562 else
1563 switch (TREE_CODE_CLASS (code))
1565 case tcc_constant:
1566 return exp;
1568 case tcc_declaration:
1569 if (exp == f)
1570 return r;
1571 else
1572 return exp;
1574 case tcc_expression:
1575 if (exp == f)
1576 return r;
1578 /* Fall through... */
1580 case tcc_exceptional:
1581 case tcc_unary:
1582 case tcc_binary:
1583 case tcc_comparison:
1584 case tcc_reference:
1585 switch (TREE_CODE_LENGTH (code))
1587 case 0:
1588 if (exp == f)
1589 return r;
1590 return exp;
1592 case 1:
1593 op0 = substitute_ssa_name (TREE_OPERAND (exp, 0), f, r);
1594 if (op0 == TREE_OPERAND (exp, 0))
1595 return exp;
1597 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
1598 break;
1600 case 2:
1601 op0 = substitute_ssa_name (TREE_OPERAND (exp, 0), f, r);
1602 op1 = substitute_ssa_name (TREE_OPERAND (exp, 1), f, r);
1604 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
1605 return exp;
1607 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
1608 break;
1610 case 3:
1611 op0 = substitute_ssa_name (TREE_OPERAND (exp, 0), f, r);
1612 op1 = substitute_ssa_name (TREE_OPERAND (exp, 1), f, r);
1613 op2 = substitute_ssa_name (TREE_OPERAND (exp, 2), f, r);
1615 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
1616 && op2 == TREE_OPERAND (exp, 2))
1617 return exp;
1619 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
1620 break;
1622 case 4:
1623 op0 = substitute_ssa_name (TREE_OPERAND (exp, 0), f, r);
1624 op1 = substitute_ssa_name (TREE_OPERAND (exp, 1), f, r);
1625 op2 = substitute_ssa_name (TREE_OPERAND (exp, 2), f, r);
1626 op3 = substitute_ssa_name (TREE_OPERAND (exp, 3), f, r);
1628 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
1629 && op2 == TREE_OPERAND (exp, 2)
1630 && op3 == TREE_OPERAND (exp, 3))
1631 return exp;
1633 new_tree
1634 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
1635 break;
1637 default:
1638 gcc_unreachable ();
1640 break;
1642 case tcc_vl_exp:
1643 default:
1644 gcc_unreachable ();
1647 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
1649 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
1650 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
1652 return new_tree;
1655 /* Rename all the operands of NEW_EXPR by recursively visiting each operand. */
1657 tree
1658 translate_isl_ast_to_gimple::rename_all_uses (tree new_expr, basic_block new_bb,
1659 basic_block old_bb)
1661 auto_vec<tree, 2> ssa_names;
1662 collect_all_ssa_names (new_expr, &ssa_names);
1663 tree t;
1664 int i;
1665 FOR_EACH_VEC_ELT (ssa_names, i, t)
1666 if (tree r = get_rename (new_bb, t, old_bb, false))
1667 new_expr = substitute_ssa_name (new_expr, t, r);
1669 return new_expr;
1672 /* For ops which are scev_analyzeable, we can regenerate a new name from its
1673 scalar evolution around LOOP. */
1675 tree
1676 translate_isl_ast_to_gimple::
1677 get_rename_from_scev (tree old_name, gimple_seq *stmts, loop_p loop,
1678 basic_block new_bb, basic_block old_bb,
1679 vec<tree> iv_map)
1681 tree scev = scalar_evolution_in_region (region->region, loop, old_name);
1683 /* At this point we should know the exact scev for each
1684 scalar SSA_NAME used in the scop: all the other scalar
1685 SSA_NAMEs should have been translated out of SSA using
1686 arrays with one element. */
1687 tree new_expr;
1688 if (chrec_contains_undetermined (scev))
1690 codegen_error = true;
1691 return build_zero_cst (TREE_TYPE (old_name));
1694 new_expr = chrec_apply_map (scev, iv_map);
1696 /* The apply should produce an expression tree containing
1697 the uses of the new induction variables. We should be
1698 able to use new_expr instead of the old_name in the newly
1699 generated loop nest. */
1700 if (chrec_contains_undetermined (new_expr)
1701 || tree_contains_chrecs (new_expr, NULL))
1703 codegen_error = true;
1704 return build_zero_cst (TREE_TYPE (old_name));
1707 /* We should check all the operands and all of them should dominate the use at
1708 new_expr. */
1709 if (TREE_CODE (new_expr) == SSA_NAME)
1711 basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (new_expr));
1712 if (bb && !dominated_by_p (CDI_DOMINATORS, new_bb, bb))
1714 codegen_error = true;
1715 return build_zero_cst (TREE_TYPE (old_name));
1719 new_expr = rename_all_uses (new_expr, new_bb, old_bb);
1720 /* We should check all the operands and all of them should dominate the use at
1721 new_expr. */
1722 if (TREE_CODE (new_expr) == SSA_NAME)
1724 basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (new_expr));
1725 if (bb && !dominated_by_p (CDI_DOMINATORS, new_bb, bb))
1727 codegen_error = true;
1728 return build_zero_cst (TREE_TYPE (old_name));
1732 /* Replace the old_name with the new_expr. */
1733 return force_gimple_operand (unshare_expr (new_expr), stmts,
1734 true, NULL_TREE);
1737 /* Renames the scalar uses of the statement COPY, using the
1738 substitution map RENAME_MAP, inserting the gimplification code at
1739 GSI_TGT, for the translation REGION, with the original copied
1740 statement in LOOP, and using the induction variable renaming map
1741 IV_MAP. Returns true when something has been renamed. codegen_error
1742 is set when the code generation cannot continue. */
1744 bool
1745 translate_isl_ast_to_gimple::rename_uses (gimple *copy,
1746 gimple_stmt_iterator *gsi_tgt,
1747 basic_block old_bb,
1748 loop_p loop, vec<tree> iv_map)
1750 bool changed = false;
1752 if (is_gimple_debug (copy))
1754 if (gimple_debug_bind_p (copy))
1755 gimple_debug_bind_reset_value (copy);
1756 else if (gimple_debug_source_bind_p (copy))
1757 return false;
1758 else
1759 gcc_unreachable ();
1761 return false;
1764 if (dump_file)
1766 fprintf (dump_file, "[codegen] renaming uses of stmt: ");
1767 print_gimple_stmt (dump_file, copy, 0, 0);
1770 use_operand_p use_p;
1771 ssa_op_iter op_iter;
1772 FOR_EACH_SSA_USE_OPERAND (use_p, copy, op_iter, SSA_OP_USE)
1774 tree old_name = USE_FROM_PTR (use_p);
1776 if (dump_file)
1778 fprintf (dump_file, "[codegen] renaming old_name = ");
1779 print_generic_expr (dump_file, old_name, 0);
1780 fprintf (dump_file, "\n");
1783 if (TREE_CODE (old_name) != SSA_NAME
1784 || SSA_NAME_IS_DEFAULT_DEF (old_name))
1785 continue;
1787 changed = true;
1788 tree new_expr = get_rename (gsi_tgt->bb, old_name,
1789 old_bb, false);
1791 if (new_expr)
1793 tree type_old_name = TREE_TYPE (old_name);
1794 tree type_new_expr = TREE_TYPE (new_expr);
1796 if (dump_file)
1798 fprintf (dump_file, "[codegen] from rename_map: new_name = ");
1799 print_generic_expr (dump_file, new_expr, 0);
1800 fprintf (dump_file, "\n");
1803 if (type_old_name != type_new_expr
1804 || TREE_CODE (new_expr) != SSA_NAME)
1806 tree var = create_tmp_var (type_old_name, "var");
1808 if (!useless_type_conversion_p (type_old_name, type_new_expr))
1809 new_expr = fold_convert (type_old_name, new_expr);
1811 gimple_seq stmts;
1812 new_expr = force_gimple_operand (new_expr, &stmts, true, var);
1813 gsi_insert_earliest (stmts);
1816 replace_exp (use_p, new_expr);
1817 continue;
1820 gimple_seq stmts;
1821 new_expr = get_rename_from_scev (old_name, &stmts, loop, gimple_bb (copy),
1822 old_bb, iv_map);
1823 if (!new_expr || codegen_error_p ())
1824 return false;
1826 if (dump_file)
1828 fprintf (dump_file, "[codegen] not in rename map, scev: ");
1829 print_generic_expr (dump_file, new_expr, 0);
1830 fprintf (dump_file, "\n");
1833 gsi_insert_earliest (stmts);
1834 replace_exp (use_p, new_expr);
1836 if (TREE_CODE (new_expr) == INTEGER_CST
1837 && is_gimple_assign (copy))
1839 tree rhs = gimple_assign_rhs1 (copy);
1841 if (TREE_CODE (rhs) == ADDR_EXPR)
1842 recompute_tree_invariant_for_addr_expr (rhs);
1845 set_rename (old_name, new_expr);
1848 return changed;
1851 /* Returns a basic block that could correspond to where a constant was defined
1852 in the original code. In the original code OLD_BB had the definition, we
1853 need to find which basic block out of the copies of old_bb, in the new
1854 region, should a definition correspond to if it has to reach BB. */
1856 basic_block
1857 translate_isl_ast_to_gimple::get_def_bb_for_const (basic_block bb,
1858 basic_block old_bb) const
1860 vec <basic_block> *bbs = region->copied_bb_map->get (old_bb);
1862 if (!bbs || bbs->is_empty ())
1863 return NULL;
1865 if (1 == bbs->length ())
1866 return (*bbs)[0];
1868 int i;
1869 basic_block b1 = NULL, b2;
1870 FOR_EACH_VEC_ELT (*bbs, i, b2)
1872 if (b2 == bb)
1873 return bb;
1875 /* BB and B2 are in two unrelated if-clauses. */
1876 if (!dominated_by_p (CDI_DOMINATORS, bb, b2))
1877 continue;
1879 /* Compute the nearest dominator. */
1880 if (!b1 || dominated_by_p (CDI_DOMINATORS, b2, b1))
1881 b1 = b2;
1884 gcc_assert (b1);
1885 return b1;
1888 /* Get the new name of OP (from OLD_BB) to be used in NEW_BB. LOOP_PHI is true
1889 when we want to rename an OP within a loop PHI instruction. */
1891 tree
1892 translate_isl_ast_to_gimple::
1893 get_new_name (basic_block new_bb, tree op,
1894 basic_block old_bb, bool loop_phi) const
1896 /* For constants the names are the same. */
1897 if (is_constant (op))
1898 return op;
1900 return get_rename (new_bb, op, old_bb, loop_phi);
1903 /* Return a debug location for OP. */
1905 static location_t
1906 get_loc (tree op)
1908 location_t loc = UNKNOWN_LOCATION;
1910 if (TREE_CODE (op) == SSA_NAME)
1911 loc = gimple_location (SSA_NAME_DEF_STMT (op));
1912 return loc;
1915 /* Returns the incoming edges of basic_block BB in the pair. The first edge is
1916 the init edge (from outside the loop) and the second one is the back edge
1917 from the same loop. */
1919 std::pair<edge, edge>
1920 get_edges (basic_block bb)
1922 std::pair<edge, edge> edges;
1923 edge e;
1924 edge_iterator ei;
1925 FOR_EACH_EDGE (e, ei, bb->preds)
1926 if (bb->loop_father != e->src->loop_father)
1927 edges.first = e;
1928 else
1929 edges.second = e;
1930 return edges;
1933 /* Copy the PHI arguments from OLD_PHI to the NEW_PHI. The arguments to NEW_PHI
1934 must be found unless they can be POSTPONEd for later. */
1936 bool
1937 translate_isl_ast_to_gimple::
1938 copy_loop_phi_args (gphi *old_phi, init_back_edge_pair_t &ibp_old_bb,
1939 gphi *new_phi, init_back_edge_pair_t &ibp_new_bb,
1940 bool postpone)
1942 gcc_assert (gimple_phi_num_args (old_phi) == gimple_phi_num_args (new_phi));
1944 basic_block new_bb = gimple_bb (new_phi);
1945 for (unsigned i = 0; i < gimple_phi_num_args (old_phi); i++)
1947 edge e;
1948 if (gimple_phi_arg_edge (old_phi, i) == ibp_old_bb.first)
1949 e = ibp_new_bb.first;
1950 else
1951 e = ibp_new_bb.second;
1953 tree old_name = gimple_phi_arg_def (old_phi, i);
1954 tree new_name = get_new_name (new_bb, old_name,
1955 gimple_bb (old_phi), true);
1956 if (new_name)
1958 add_phi_arg (new_phi, new_name, e, get_loc (old_name));
1959 continue;
1962 gimple *old_def_stmt = SSA_NAME_DEF_STMT (old_name);
1963 if (!old_def_stmt || gimple_code (old_def_stmt) == GIMPLE_NOP)
1964 /* If the phi arg was a function arg, or wasn't defined, just use the
1965 old name. */
1966 add_phi_arg (new_phi, old_name, e, get_loc (old_name));
1967 else if (postpone)
1969 /* Postpone code gen for later for those back-edges we don't have the
1970 names yet. */
1971 region->incomplete_phis.safe_push (std::make_pair (old_phi, new_phi));
1972 if (dump_file)
1973 fprintf (dump_file, "[codegen] postpone loop phi nodes.\n");
1975 else
1976 /* Either we should add the arg to phi or, we should postpone. */
1977 return false;
1979 return true;
1982 /* Copy loop phi nodes from BB to NEW_BB. */
1984 bool
1985 translate_isl_ast_to_gimple::copy_loop_phi_nodes (basic_block bb,
1986 basic_block new_bb)
1988 if (dump_file)
1989 fprintf (dump_file, "[codegen] copying loop phi nodes in bb_%d.\n",
1990 new_bb->index);
1992 /* Loop phi nodes should have only two arguments. */
1993 gcc_assert (2 == EDGE_COUNT (bb->preds));
1995 /* First edge is the init edge and second is the back edge. */
1996 init_back_edge_pair_t ibp_old_bb = get_edges (bb);
1998 /* First edge is the init edge and second is the back edge. */
1999 init_back_edge_pair_t ibp_new_bb = get_edges (new_bb);
2001 for (gphi_iterator psi = gsi_start_phis (bb); !gsi_end_p (psi);
2002 gsi_next (&psi))
2004 gphi *phi = psi.phi ();
2005 tree res = gimple_phi_result (phi);
2006 if (virtual_operand_p (res))
2007 continue;
2008 if (is_gimple_reg (res) && scev_analyzable_p (res, region->region))
2009 continue;
2011 gphi *new_phi = create_phi_node (SSA_NAME_VAR (res), new_bb);
2012 tree new_res = create_new_def_for (res, new_phi,
2013 gimple_phi_result_ptr (new_phi));
2014 set_rename (res, new_res);
2015 codegen_error = !copy_loop_phi_args (phi, ibp_old_bb, new_phi,
2016 ibp_new_bb, true);
2017 update_stmt (new_phi);
2020 return true;
2023 /* Return the init value of PHI, the value coming from outside the loop. */
2025 static tree
2026 get_loop_init_value (gphi *phi)
2029 loop_p loop = gimple_bb (phi)->loop_father;
2031 edge e;
2032 edge_iterator ei;
2033 FOR_EACH_EDGE (e, ei, gimple_bb (phi)->preds)
2034 if (e->src->loop_father != loop)
2035 return gimple_phi_arg_def (phi, e->dest_idx);
2037 return NULL_TREE;
2040 /* Find the init value (the value which comes from outside the loop), of one of
2041 the operands of DEF which is defined by a loop phi. */
2043 static tree
2044 find_init_value (gimple *def)
2046 if (gimple_code (def) == GIMPLE_PHI)
2047 return get_loop_init_value (as_a <gphi*> (def));
2049 if (gimple_vuse (def))
2050 return NULL_TREE;
2052 ssa_op_iter iter;
2053 use_operand_p use_p;
2054 FOR_EACH_SSA_USE_OPERAND (use_p, def, iter, SSA_OP_USE)
2056 tree use = USE_FROM_PTR (use_p);
2057 if (TREE_CODE (use) == SSA_NAME)
2059 if (tree res = find_init_value (SSA_NAME_DEF_STMT (use)))
2060 return res;
2064 return NULL_TREE;
2067 /* Return the init value, the value coming from outside the loop. */
2069 static tree
2070 find_init_value_close_phi (gphi *phi)
2072 gcc_assert (gimple_phi_num_args (phi) == 1);
2073 tree use_arg = gimple_phi_arg_def (phi, 0);
2074 gimple *def = SSA_NAME_DEF_STMT (use_arg);
2075 return find_init_value (def);
2079 tree translate_isl_ast_to_gimple::
2080 add_close_phis_to_outer_loops (tree last_merge_name, edge last_e,
2081 gimple *old_close_phi)
2083 sese_l &codegen_region = region->if_region->true_region->region;
2084 gimple *stmt = SSA_NAME_DEF_STMT (last_merge_name);
2085 basic_block bb = gimple_bb (stmt);
2086 if (!bb_in_sese_p (bb, codegen_region))
2087 return last_merge_name;
2089 loop_p loop = bb->loop_father;
2090 if (!loop_in_sese_p (loop, codegen_region))
2091 return last_merge_name;
2093 edge e = single_exit (loop);
2095 if (dominated_by_p (CDI_DOMINATORS, e->dest, last_e->src))
2096 return last_merge_name;
2098 tree old_name = gimple_phi_arg_def (old_close_phi, 0);
2099 tree old_close_phi_name = gimple_phi_result (old_close_phi);
2101 bb = e->dest;
2102 if (!bb_contains_loop_close_phi_nodes (bb) || !single_succ_p (bb))
2103 bb = split_edge (e);
2105 gphi *close_phi = create_phi_node (SSA_NAME_VAR (last_merge_name), bb);
2106 tree res = create_new_def_for (last_merge_name, close_phi,
2107 gimple_phi_result_ptr (close_phi));
2108 set_rename (old_close_phi_name, res);
2109 add_phi_arg (close_phi, last_merge_name, e, get_loc (old_name));
2110 last_merge_name = res;
2112 return add_close_phis_to_outer_loops (last_merge_name, last_e, old_close_phi);
2115 /* Add phi nodes to all merge points of all the diamonds enclosing the loop of
2116 the close phi node PHI. */
2118 bool translate_isl_ast_to_gimple::
2119 add_close_phis_to_merge_points (gphi *old_close_phi, gphi *new_close_phi,
2120 tree default_value)
2122 sese_l &codegen_region = region->if_region->true_region->region;
2123 basic_block default_value_bb = get_entry_bb (codegen_region);
2124 if (SSA_NAME == TREE_CODE (default_value))
2126 gimple *stmt = SSA_NAME_DEF_STMT (default_value);
2127 if (!stmt || gimple_code (stmt) == GIMPLE_NOP)
2128 return false;
2129 default_value_bb = gimple_bb (stmt);
2132 basic_block new_close_phi_bb = gimple_bb (new_close_phi);
2134 tree old_close_phi_name = gimple_phi_result (old_close_phi);
2135 tree new_close_phi_name = gimple_phi_result (new_close_phi);
2136 tree last_merge_name = new_close_phi_name;
2137 tree old_name = gimple_phi_arg_def (old_close_phi, 0);
2139 int i;
2140 edge merge_e;
2141 FOR_EACH_VEC_ELT_REVERSE (merge_points, i, merge_e)
2143 basic_block new_merge_bb = merge_e->src;
2144 if (!dominated_by_p (CDI_DOMINATORS, new_merge_bb, default_value_bb))
2145 continue;
2147 last_merge_name = add_close_phis_to_outer_loops (last_merge_name, merge_e,
2148 old_close_phi);
2150 gphi *merge_phi = create_phi_node (SSA_NAME_VAR (old_close_phi_name), new_merge_bb);
2151 tree merge_res = create_new_def_for (old_close_phi_name, merge_phi,
2152 gimple_phi_result_ptr (merge_phi));
2153 set_rename (old_close_phi_name, merge_res);
2155 edge from_loop = NULL, from_default_value = NULL;
2156 edge e;
2157 edge_iterator ei;
2158 FOR_EACH_EDGE (e, ei, new_merge_bb->preds)
2159 if (dominated_by_p (CDI_DOMINATORS, e->src, new_close_phi_bb))
2160 from_loop = e;
2161 else
2162 from_default_value = e;
2164 /* Because CDI_POST_DOMINATORS are not updated, we only rely on
2165 CDI_DOMINATORS, which may not handle all cases where new_close_phi_bb
2166 is contained in another condition. */
2167 if (!from_default_value || !from_loop)
2168 return false;
2170 add_phi_arg (merge_phi, last_merge_name, from_loop, get_loc (old_name));
2171 add_phi_arg (merge_phi, default_value, from_default_value, get_loc (old_name));
2173 if (dump_file)
2175 fprintf (dump_file, "[codegen] Adding guard-phi: ");
2176 print_gimple_stmt (dump_file, merge_phi, 0, 0);
2179 update_stmt (merge_phi);
2180 last_merge_name = merge_res;
2183 return true;
2186 /* Copy all the loop-close phi args from BB to NEW_BB. */
2188 bool
2189 translate_isl_ast_to_gimple::copy_loop_close_phi_args (basic_block old_bb,
2190 basic_block new_bb,
2191 bool postpone)
2193 for (gphi_iterator psi = gsi_start_phis (old_bb); !gsi_end_p (psi);
2194 gsi_next (&psi))
2196 gphi *old_close_phi = psi.phi ();
2197 tree res = gimple_phi_result (old_close_phi);
2198 if (virtual_operand_p (res))
2199 continue;
2201 if (is_gimple_reg (res) && scev_analyzable_p (res, region->region))
2202 /* Loop close phi nodes should not be scev_analyzable_p. */
2203 gcc_unreachable ();
2205 gphi *new_close_phi = create_phi_node (SSA_NAME_VAR (res), new_bb);
2206 tree new_res = create_new_def_for (res, new_close_phi,
2207 gimple_phi_result_ptr (new_close_phi));
2208 set_rename (res, new_res);
2210 tree old_name = gimple_phi_arg_def (old_close_phi, 0);
2211 tree new_name = get_new_name (new_bb, old_name, old_bb, false);
2213 /* Predecessor basic blocks of a loop close phi should have been code
2214 generated before. FIXME: This is fixable by merging PHIs from inner
2215 loops as well. See: gfortran.dg/graphite/interchange-3.f90. */
2216 if (!new_name)
2217 return false;
2219 add_phi_arg (new_close_phi, new_name, single_pred_edge (new_bb),
2220 get_loc (old_name));
2221 if (dump_file)
2223 fprintf (dump_file, "[codegen] Adding loop-closed phi: ");
2224 print_gimple_stmt (dump_file, new_close_phi, 0, 0);
2227 update_stmt (new_close_phi);
2229 /* When there is no loop guard around this codegenerated loop, there is no
2230 need to collect the close-phi arg. */
2231 if (merge_points.is_empty ())
2232 continue;
2234 /* Add a PHI in the succ_new_bb for each close phi of the loop. */
2235 tree default_value = find_init_value_close_phi (new_close_phi);
2237 /* A close phi must come from a loop-phi having a default value. */
2238 if (!default_value)
2240 if (!postpone)
2241 return false;
2243 region->incomplete_phis.safe_push (std::make_pair (old_close_phi,
2244 new_close_phi));
2245 if (dump_file)
2247 fprintf (dump_file, "[codegen] postpone close phi nodes: ");
2248 print_gimple_stmt (dump_file, new_close_phi, 0, 0);
2250 continue;
2253 if (!add_close_phis_to_merge_points (old_close_phi, new_close_phi,
2254 default_value))
2255 return false;
2258 return true;
2261 /* Copy loop close phi nodes from BB to NEW_BB. */
2263 bool
2264 translate_isl_ast_to_gimple::copy_loop_close_phi_nodes (basic_block old_bb,
2265 basic_block new_bb)
2267 if (dump_file)
2268 fprintf (dump_file, "[codegen] copying loop closed phi nodes in bb_%d.\n",
2269 new_bb->index);
2270 /* Loop close phi nodes should have only one argument. */
2271 gcc_assert (1 == EDGE_COUNT (old_bb->preds));
2273 return copy_loop_close_phi_args (old_bb, new_bb, true);
2277 /* Add NEW_NAME as the ARGNUM-th arg of NEW_PHI which is in NEW_BB.
2278 DOMINATING_PRED is the predecessor basic block of OLD_BB which dominates the
2279 other pred of OLD_BB as well. If no such basic block exists then it is NULL.
2280 NON_DOMINATING_PRED is a pred which does not dominate OLD_BB, it cannot be
2281 NULL.
2283 Case1: OLD_BB->preds {BB1, BB2} and BB1 does not dominate BB2 and vice versa.
2284 In this case DOMINATING_PRED = NULL.
2286 Case2: OLD_BB->preds {BB1, BB2} and BB1 dominates BB2.
2288 Returns true on successful copy of the args, false otherwise. */
2290 bool
2291 translate_isl_ast_to_gimple::
2292 add_phi_arg_for_new_expr (tree old_phi_args[2], tree new_phi_args[2],
2293 edge old_bb_dominating_edge,
2294 edge old_bb_non_dominating_edge,
2295 gphi *phi, gphi *new_phi,
2296 basic_block new_bb)
2298 basic_block def_pred[2] = { NULL, NULL };
2299 int not_found_bb_index = -1;
2300 for (int i = 0; i < 2; i++)
2302 /* If the corresponding def_bb could not be found the entry will be
2303 NULL. */
2304 if (TREE_CODE (old_phi_args[i]) == INTEGER_CST)
2305 def_pred[i] = get_def_bb_for_const (new_bb,
2306 gimple_phi_arg_edge (phi, i)->src);
2307 else if (new_phi_args[i] && (TREE_CODE (new_phi_args[i]) == SSA_NAME))
2308 def_pred[i] = gimple_bb (SSA_NAME_DEF_STMT (new_phi_args[i]));
2310 if (!def_pred[i])
2312 /* When non are available bail out. */
2313 if (not_found_bb_index != -1)
2314 return false;
2315 not_found_bb_index = i;
2319 /* Here we are pattern matching on the structure of CFG w.r.t. old one. */
2320 if (old_bb_dominating_edge)
2322 if (not_found_bb_index != -1)
2323 return false;
2325 basic_block new_pred1 = (*new_bb->preds)[0]->src;
2326 basic_block new_pred2 = (*new_bb->preds)[1]->src;
2327 vec <basic_block> *bbs
2328 = region->copied_bb_map->get (old_bb_non_dominating_edge->src);
2330 /* Could not find a mapping. */
2331 if (!bbs)
2332 return false;
2334 basic_block new_pred = NULL;
2335 basic_block b;
2336 int i;
2337 FOR_EACH_VEC_ELT (*bbs, i, b)
2339 if (dominated_by_p (CDI_DOMINATORS, new_pred1, b))
2341 /* FIXME: If we have already found new_pred then we have to
2342 disambiguate, bail out for now. */
2343 if (new_pred)
2344 return false;
2345 new_pred = new_pred1;
2347 if (dominated_by_p (CDI_DOMINATORS, new_pred2, b))
2349 /* FIXME: If we have already found new_pred then we have to either
2350 it dominates both or we have to disambiguate, bail out. */
2351 if (new_pred)
2352 return false;
2353 new_pred = new_pred2;
2357 if (!new_pred)
2358 return false;
2360 edge new_non_dominating_edge = find_edge (new_pred, new_bb);
2361 gcc_assert (new_non_dominating_edge);
2362 /* FIXME: Validate each args just like in loop-phis. */
2363 /* By the process of elimination we first insert insert phi-edge for
2364 non-dominating pred which is computed above and then we insert the
2365 remaining one. */
2366 int inserted_edge = 0;
2367 for (; inserted_edge < 2; inserted_edge++)
2369 edge new_bb_pred_edge = gimple_phi_arg_edge (new_phi, inserted_edge);
2370 if (new_non_dominating_edge == new_bb_pred_edge)
2372 add_phi_arg (new_phi, new_phi_args[inserted_edge],
2373 new_non_dominating_edge,
2374 get_loc (old_phi_args[inserted_edge]));
2375 break;
2378 if (inserted_edge == 2)
2379 return false;
2381 int edge_dominating = inserted_edge == 0 ? 1 : 0;
2383 edge new_dominating_edge = NULL;
2384 for (inserted_edge = 0; inserted_edge < 2; inserted_edge++)
2386 edge e = gimple_phi_arg_edge (new_phi, inserted_edge);
2387 if (e != new_non_dominating_edge)
2389 new_dominating_edge = e;
2390 add_phi_arg (new_phi, new_phi_args[edge_dominating],
2391 new_dominating_edge,
2392 get_loc (old_phi_args[inserted_edge]));
2393 break;
2396 gcc_assert (new_dominating_edge);
2398 else
2400 /* Classic diamond structure: both edges are non-dominating. We need to
2401 find one unique edge then the other can be found be elimination. If
2402 any definition (def_pred) dominates both the preds of new_bb then we
2403 bail out. Entries of def_pred maybe NULL, in that case we must
2404 uniquely find pred with help of only one entry. */
2405 edge new_e[2] = { NULL, NULL };
2406 for (int i = 0; i < 2; i++)
2408 edge e;
2409 edge_iterator ei;
2410 FOR_EACH_EDGE (e, ei, new_bb->preds)
2411 if (def_pred[i]
2412 && dominated_by_p (CDI_DOMINATORS, e->src, def_pred[i]))
2414 if (new_e[i])
2415 /* We do not know how to handle the case when def_pred
2416 dominates more than a predecessor. */
2417 return false;
2418 new_e[i] = e;
2422 gcc_assert (new_e[0] || new_e[1]);
2424 /* Find the other edge by process of elimination. */
2425 if (not_found_bb_index != -1)
2427 gcc_assert (!new_e[not_found_bb_index]);
2428 int found_bb_index = not_found_bb_index == 1 ? 0 : 1;
2429 edge e;
2430 edge_iterator ei;
2431 FOR_EACH_EDGE (e, ei, new_bb->preds)
2433 if (new_e[found_bb_index] == e)
2434 continue;
2435 new_e[not_found_bb_index] = e;
2439 /* Add edges to phi args. */
2440 for (int i = 0; i < 2; i++)
2441 add_phi_arg (new_phi, new_phi_args[i], new_e[i],
2442 get_loc (old_phi_args[i]));
2445 return true;
2448 /* Copy the arguments of cond-phi node PHI, to NEW_PHI in the codegenerated
2449 region. If postpone is true and it isn't possible to copy any arg of PHI,
2450 the PHI is added to the REGION->INCOMPLETE_PHIS to be codegenerated later.
2451 Returns false if the copying was unsuccessful. */
2453 bool
2454 translate_isl_ast_to_gimple::copy_cond_phi_args (gphi *phi, gphi *new_phi,
2455 vec<tree> iv_map,
2456 bool postpone)
2458 if (dump_file)
2459 fprintf (dump_file, "[codegen] copying cond phi args.\n");
2460 gcc_assert (2 == gimple_phi_num_args (phi));
2462 basic_block new_bb = gimple_bb (new_phi);
2463 loop_p loop = gimple_bb (phi)->loop_father;
2465 basic_block old_bb = gimple_bb (phi);
2466 edge old_bb_non_dominating_edge = NULL, old_bb_dominating_edge = NULL;
2468 edge e;
2469 edge_iterator ei;
2470 FOR_EACH_EDGE (e, ei, old_bb->preds)
2471 if (!dominated_by_p (CDI_DOMINATORS, old_bb, e->src))
2472 old_bb_non_dominating_edge = e;
2473 else
2474 old_bb_dominating_edge = e;
2476 gcc_assert (!dominated_by_p (CDI_DOMINATORS, old_bb,
2477 old_bb_non_dominating_edge->src));
2479 tree new_phi_args[2];
2480 tree old_phi_args[2];
2482 for (unsigned i = 0; i < gimple_phi_num_args (phi); i++)
2484 tree old_name = gimple_phi_arg_def (phi, i);
2485 tree new_name = get_new_name (new_bb, old_name, old_bb, false);
2486 old_phi_args[i] = old_name;
2487 if (new_name)
2489 new_phi_args [i] = new_name;
2490 continue;
2493 /* If the phi-arg was a parameter. */
2494 if (vec_find (region->params, old_name) != -1)
2496 new_phi_args [i] = old_name;
2497 if (dump_file)
2499 fprintf (dump_file,
2500 "[codegen] parameter argument to phi, new_expr: ");
2501 print_generic_expr (dump_file, new_phi_args[i], 0);
2502 fprintf (dump_file, "\n");
2504 continue;
2507 gimple *old_def_stmt = SSA_NAME_DEF_STMT (old_name);
2508 if (!old_def_stmt || gimple_code (old_def_stmt) == GIMPLE_NOP)
2509 /* FIXME: If the phi arg was a function arg, or wasn't defined, just use
2510 the old name. */
2511 return false;
2513 if (postpone)
2515 /* If the phi-arg is scev-analyzeable but only in the first stage. */
2516 if (is_gimple_reg (old_name)
2517 && scev_analyzable_p (old_name, region->region))
2519 gimple_seq stmts;
2520 tree new_expr = get_rename_from_scev (old_name, &stmts, loop,
2521 new_bb, old_bb, iv_map);
2522 if (codegen_error_p ())
2523 return false;
2525 gcc_assert (new_expr);
2526 if (dump_file)
2528 fprintf (dump_file,
2529 "[codegen] scev analyzeable, new_expr: ");
2530 print_generic_expr (dump_file, new_expr, 0);
2531 fprintf (dump_file, "\n");
2533 gsi_insert_earliest (stmts);
2534 new_phi_args [i] = new_name;
2535 continue;
2538 /* Postpone code gen for later for back-edges. */
2539 region->incomplete_phis.safe_push (std::make_pair (phi, new_phi));
2541 if (dump_file)
2543 fprintf (dump_file, "[codegen] postpone cond phi nodes: ");
2544 print_gimple_stmt (dump_file, new_phi, 0, 0);
2547 new_phi_args [i] = NULL_TREE;
2548 continue;
2550 else
2551 /* Either we should add the arg to phi or, we should postpone. */
2552 return false;
2555 /* If none of the args have been determined in the first stage then wait until
2556 later. */
2557 if (postpone && !new_phi_args[0] && !new_phi_args[1])
2558 return true;
2560 return add_phi_arg_for_new_expr (old_phi_args, new_phi_args,
2561 old_bb_dominating_edge,
2562 old_bb_non_dominating_edge,
2563 phi, new_phi, new_bb);
2566 /* Copy cond phi nodes from BB to NEW_BB. A cond-phi node is a basic block
2567 containing phi nodes coming from two predecessors, and none of them are back
2568 edges. */
2570 bool
2571 translate_isl_ast_to_gimple::copy_cond_phi_nodes (basic_block bb,
2572 basic_block new_bb,
2573 vec<tree> iv_map)
2576 gcc_assert (!bb_contains_loop_close_phi_nodes (bb));
2578 if (dump_file)
2579 fprintf (dump_file, "[codegen] copying cond phi nodes in bb_%d.\n",
2580 new_bb->index);
2582 /* Cond phi nodes should have exactly two arguments. */
2583 gcc_assert (2 == EDGE_COUNT (bb->preds));
2585 for (gphi_iterator psi = gsi_start_phis (bb); !gsi_end_p (psi);
2586 gsi_next (&psi))
2588 gphi *phi = psi.phi ();
2589 tree res = gimple_phi_result (phi);
2590 if (virtual_operand_p (res))
2591 continue;
2592 if (is_gimple_reg (res) && scev_analyzable_p (res, region->region))
2593 /* Cond phi nodes should not be scev_analyzable_p. */
2594 gcc_unreachable ();
2596 gphi *new_phi = create_phi_node (SSA_NAME_VAR (res), new_bb);
2597 tree new_res = create_new_def_for (res, new_phi,
2598 gimple_phi_result_ptr (new_phi));
2599 set_rename (res, new_res);
2601 if (!copy_cond_phi_args (phi, new_phi, iv_map, true))
2602 return false;
2604 update_stmt (new_phi);
2607 return true;
2610 /* Return true if STMT should be copied from region to the new code-generated
2611 region. LABELs, CONDITIONS, induction-variables and region parameters need
2612 not be copied. */
2614 static bool
2615 should_copy_to_new_region (gimple *stmt, sese_info_p region)
2617 /* Do not copy labels or conditions. */
2618 if (gimple_code (stmt) == GIMPLE_LABEL
2619 || gimple_code (stmt) == GIMPLE_COND)
2620 return false;
2622 tree lhs;
2623 /* Do not copy induction variables. */
2624 if (is_gimple_assign (stmt)
2625 && (lhs = gimple_assign_lhs (stmt))
2626 && TREE_CODE (lhs) == SSA_NAME
2627 && is_gimple_reg (lhs)
2628 && scev_analyzable_p (lhs, region->region))
2629 return false;
2631 return true;
2634 /* Create new names for all the definitions created by COPY and add replacement
2635 mappings for each new name. */
2637 void
2638 translate_isl_ast_to_gimple::set_rename_for_each_def (gimple *stmt)
2640 def_operand_p def_p;
2641 ssa_op_iter op_iter;
2642 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, op_iter, SSA_OP_ALL_DEFS)
2644 tree old_name = DEF_FROM_PTR (def_p);
2645 tree new_name = create_new_def_for (old_name, stmt, def_p);
2646 set_rename (old_name, new_name);
2650 /* Duplicates the statements of basic block BB into basic block NEW_BB
2651 and compute the new induction variables according to the IV_MAP.
2652 CODEGEN_ERROR is set when the code generation cannot continue. */
2654 bool
2655 translate_isl_ast_to_gimple::graphite_copy_stmts_from_block (basic_block bb,
2656 basic_block new_bb,
2657 vec<tree> iv_map)
2659 /* Iterator poining to the place where new statement (s) will be inserted. */
2660 gimple_stmt_iterator gsi_tgt = gsi_last_bb (new_bb);
2662 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
2663 gsi_next (&gsi))
2665 gimple *stmt = gsi_stmt (gsi);
2666 if (!should_copy_to_new_region (stmt, region))
2667 continue;
2669 /* Create a new copy of STMT and duplicate STMT's virtual
2670 operands. */
2671 gimple *copy = gimple_copy (stmt);
2672 gsi_insert_after (&gsi_tgt, copy, GSI_NEW_STMT);
2674 if (dump_file)
2676 fprintf (dump_file, "[codegen] inserting statement: ");
2677 print_gimple_stmt (dump_file, copy, 0, 0);
2680 maybe_duplicate_eh_stmt (copy, stmt);
2681 gimple_duplicate_stmt_histograms (cfun, copy, cfun, stmt);
2683 /* Crete new names for each def in the copied stmt. */
2684 set_rename_for_each_def (copy);
2686 loop_p loop = bb->loop_father;
2687 if (rename_uses (copy, &gsi_tgt, bb, loop, iv_map))
2689 fold_stmt_inplace (&gsi_tgt);
2690 gcc_assert (gsi_stmt (gsi_tgt) == copy);
2693 if (codegen_error_p ())
2694 return false;
2696 update_stmt (copy);
2699 return true;
2703 /* Given a basic block containing close-phi it returns the new basic block where
2704 to insert a copy of the close-phi nodes. All the uses in close phis should
2705 come from a single loop otherwise it returns NULL. */
2707 edge
2708 translate_isl_ast_to_gimple::edge_for_new_close_phis (basic_block bb)
2710 /* Make sure that NEW_BB is the new_loop->exit->dest. We find the definition
2711 of close phi in the original code and then find the mapping of basic block
2712 defining that variable. If there are multiple close-phis and they are
2713 defined in different loops (in the original or in the new code) because of
2714 loop splitting, then we bail out. */
2715 loop_p new_loop = NULL;
2716 for (gphi_iterator psi = gsi_start_phis (bb); !gsi_end_p (psi);
2717 gsi_next (&psi))
2719 gphi *phi = psi.phi ();
2720 tree name = gimple_phi_arg_def (phi, 0);
2721 basic_block old_loop_bb = gimple_bb (SSA_NAME_DEF_STMT (name));
2723 vec <basic_block> *bbs = region->copied_bb_map->get (old_loop_bb);
2724 if (!bbs || bbs->length () != 1)
2725 /* This is one of the places which shows preserving original structure
2726 is not always possible, as we may need to insert close PHI for a loop
2727 where the latch does not have any mapping, or the mapping is
2728 ambiguous. */
2729 return NULL;
2731 if (!new_loop)
2732 new_loop = (*bbs)[0]->loop_father;
2733 else if (new_loop != (*bbs)[0]->loop_father)
2734 return NULL;
2737 if (!new_loop)
2738 return NULL;
2740 return single_exit (new_loop);
2743 /* Copies BB and includes in the copied BB all the statements that can
2744 be reached following the use-def chains from the memory accesses,
2745 and returns the next edge following this new block. codegen_error is
2746 set when the code generation cannot continue. */
2748 edge
2749 translate_isl_ast_to_gimple::copy_bb_and_scalar_dependences (basic_block bb,
2750 edge next_e,
2751 vec<tree> iv_map)
2753 int num_phis = number_of_phi_nodes (bb);
2755 if (region->copied_bb_map->get (bb))
2757 /* FIXME: we should be able to handle phi nodes with args coming from
2758 outside the region. */
2759 if (num_phis)
2761 codegen_error = true;
2762 return NULL;
2766 basic_block new_bb = NULL;
2767 if (bb_contains_loop_close_phi_nodes (bb))
2769 if (dump_file)
2770 fprintf (dump_file, "[codegen] bb_%d contains close phi nodes.\n",
2771 bb->index);
2773 edge e = edge_for_new_close_phis (bb);
2774 if (!e)
2776 codegen_error = true;
2777 return NULL;
2780 basic_block phi_bb = e->dest;
2782 if (!bb_contains_loop_close_phi_nodes (phi_bb) || !single_succ_p (phi_bb))
2783 phi_bb = split_edge (e);
2785 gcc_assert (single_pred_edge (phi_bb)->src->loop_father
2786 != single_pred_edge (phi_bb)->dest->loop_father);
2788 if (!copy_loop_close_phi_nodes (bb, phi_bb))
2790 codegen_error = true;
2791 return NULL;
2794 if (e == next_e)
2795 new_bb = phi_bb;
2796 else
2797 new_bb = split_edge (next_e);
2799 else
2801 new_bb = split_edge (next_e);
2802 if (num_phis > 0 && bb_contains_loop_phi_nodes (bb))
2804 basic_block phi_bb = next_e->dest->loop_father->header;
2806 /* At this point we are unable to codegenerate by still preserving the SSA
2807 structure because maybe the loop is completely unrolled and the PHIs
2808 and cross-bb scalar dependencies are untrackable w.r.t. the original
2809 code. See gfortran.dg/graphite/pr29832.f90. */
2810 if (EDGE_COUNT (bb->preds) != EDGE_COUNT (phi_bb->preds))
2812 codegen_error = true;
2813 return NULL;
2816 if (dump_file)
2817 fprintf (dump_file, "[codegen] bb_%d contains loop phi nodes.\n",
2818 bb->index);
2819 if (!copy_loop_phi_nodes (bb, phi_bb))
2821 codegen_error = true;
2822 return NULL;
2825 else if (num_phis > 0)
2827 if (dump_file)
2828 fprintf (dump_file, "[codegen] bb_%d contains cond phi nodes.\n",
2829 bb->index);
2831 basic_block phi_bb = single_pred (new_bb);
2832 loop_p loop_father = new_bb->loop_father;
2834 /* Move back until we find the block with two predecessors. */
2835 while (single_pred_p (phi_bb))
2836 phi_bb = single_pred_edge (phi_bb)->src;
2838 /* If a corresponding merge-point was not found, then abort codegen. */
2839 if (phi_bb->loop_father != loop_father
2840 || !copy_cond_phi_nodes (bb, phi_bb, iv_map))
2842 codegen_error = true;
2843 return NULL;
2848 if (dump_file)
2849 fprintf (dump_file, "[codegen] copying from bb_%d to bb_%d.\n",
2850 bb->index, new_bb->index);
2852 vec <basic_block> *copied_bbs = region->copied_bb_map->get (bb);
2853 if (copied_bbs)
2854 copied_bbs->safe_push (new_bb);
2855 else
2857 vec<basic_block> bbs;
2858 bbs.create (2);
2859 bbs.safe_push (new_bb);
2860 region->copied_bb_map->put (bb, bbs);
2863 if (!graphite_copy_stmts_from_block (bb, new_bb, iv_map))
2865 codegen_error = true;
2866 return NULL;
2869 return single_succ_edge (new_bb);
2872 /* Patch the missing arguments of the phi nodes. */
2874 void
2875 translate_isl_ast_to_gimple::translate_pending_phi_nodes ()
2877 int i;
2878 phi_rename *rename;
2879 FOR_EACH_VEC_ELT (region->incomplete_phis, i, rename)
2881 gphi *old_phi = rename->first;
2882 gphi *new_phi = rename->second;
2883 basic_block old_bb = gimple_bb (old_phi);
2884 basic_block new_bb = gimple_bb (new_phi);
2886 /* First edge is the init edge and second is the back edge. */
2887 init_back_edge_pair_t ibp_old_bb = get_edges (old_bb);
2888 init_back_edge_pair_t ibp_new_bb = get_edges (new_bb);
2890 if (dump_file)
2892 fprintf (dump_file, "[codegen] translating pending old-phi: ");
2893 print_gimple_stmt (dump_file, old_phi, 0, 0);
2896 auto_vec <tree, 1> iv_map;
2897 if (bb_contains_loop_phi_nodes (new_bb))
2898 codegen_error = !copy_loop_phi_args (old_phi, ibp_old_bb, new_phi,
2899 ibp_new_bb, false);
2900 else if (bb_contains_loop_close_phi_nodes (new_bb))
2901 codegen_error = !copy_loop_close_phi_args (old_bb, new_bb, false);
2902 else
2903 codegen_error = !copy_cond_phi_args (old_phi, new_phi, iv_map, false);
2905 if (dump_file)
2907 fprintf (dump_file, "[codegen] to new-phi: ");
2908 print_gimple_stmt (dump_file, new_phi, 0, 0);
2910 if (codegen_error)
2911 return;
2915 /* Prints NODE to FILE. */
2917 void
2918 translate_isl_ast_to_gimple::print_isl_ast_node (FILE *file,
2919 __isl_keep isl_ast_node *node,
2920 __isl_keep isl_ctx *ctx) const
2922 isl_printer *prn = isl_printer_to_file (ctx, file);
2923 prn = isl_printer_set_output_format (prn, ISL_FORMAT_C);
2924 prn = isl_printer_print_ast_node (prn, node);
2925 prn = isl_printer_print_str (prn, "\n");
2926 isl_printer_free (prn);
2929 /* Add ISL's parameter identifiers and corresponding trees to ivs_params. */
2931 void
2932 translate_isl_ast_to_gimple::add_parameters_to_ivs_params (scop_p scop,
2933 ivs_params &ip)
2935 sese_info_p region = scop->scop_info;
2936 unsigned nb_parameters = isl_set_dim (scop->param_context, isl_dim_param);
2937 gcc_assert (nb_parameters == region->params.length ());
2938 unsigned i;
2939 for (i = 0; i < nb_parameters; i++)
2941 isl_id *tmp_id = isl_set_get_dim_id (scop->param_context,
2942 isl_dim_param, i);
2943 ip[tmp_id] = region->params[i];
2948 /* Generates a build, which specifies the constraints on the parameters. */
2950 __isl_give isl_ast_build *
2951 translate_isl_ast_to_gimple::generate_isl_context (scop_p scop)
2953 isl_set *context_isl = isl_set_params (isl_set_copy (scop->param_context));
2954 return isl_ast_build_from_context (context_isl);
2957 /* Get the maximal number of schedule dimensions in the scop SCOP. */
2960 translate_isl_ast_to_gimple::get_max_schedule_dimensions (scop_p scop)
2962 int i;
2963 poly_bb_p pbb;
2964 int schedule_dims = 0;
2966 FOR_EACH_VEC_ELT (scop->pbbs, i, pbb)
2968 int pbb_schedule_dims = isl_map_dim (pbb->transformed, isl_dim_out);
2969 if (pbb_schedule_dims > schedule_dims)
2970 schedule_dims = pbb_schedule_dims;
2973 return schedule_dims;
2976 /* Extend the schedule to NB_SCHEDULE_DIMS schedule dimensions.
2978 For schedules with different dimensionality, the isl AST generator can not
2979 define an order and will just randomly choose an order. The solution to this
2980 problem is to extend all schedules to the maximal number of schedule
2981 dimensions (using '0's for the remaining values). */
2983 __isl_give isl_map *
2984 translate_isl_ast_to_gimple::extend_schedule (__isl_take isl_map *schedule,
2985 int nb_schedule_dims)
2987 int tmp_dims = isl_map_dim (schedule, isl_dim_out);
2988 schedule =
2989 isl_map_add_dims (schedule, isl_dim_out, nb_schedule_dims - tmp_dims);
2990 isl_val *zero =
2991 isl_val_int_from_si (isl_map_get_ctx (schedule), 0);
2992 int i;
2993 for (i = tmp_dims; i < nb_schedule_dims; i++)
2995 schedule
2996 = isl_map_fix_val (schedule, isl_dim_out, i, isl_val_copy (zero));
2998 isl_val_free (zero);
2999 return schedule;
3002 /* Generates a schedule, which specifies an order used to
3003 visit elements in a domain. */
3005 __isl_give isl_union_map *
3006 translate_isl_ast_to_gimple::generate_isl_schedule (scop_p scop)
3008 int nb_schedule_dims = get_max_schedule_dimensions (scop);
3009 int i;
3010 poly_bb_p pbb;
3011 isl_union_map *schedule_isl =
3012 isl_union_map_empty (isl_set_get_space (scop->param_context));
3014 FOR_EACH_VEC_ELT (scop->pbbs, i, pbb)
3016 /* Dead code elimination: when the domain of a PBB is empty,
3017 don't generate code for the PBB. */
3018 if (isl_set_is_empty (pbb->domain))
3019 continue;
3021 isl_map *bb_schedule = isl_map_copy (pbb->transformed);
3022 bb_schedule = isl_map_intersect_domain (bb_schedule,
3023 isl_set_copy (pbb->domain));
3024 bb_schedule = extend_schedule (bb_schedule, nb_schedule_dims);
3025 schedule_isl
3026 = isl_union_map_union (schedule_isl,
3027 isl_union_map_from_map (bb_schedule));
3029 return schedule_isl;
3032 /* This method is executed before the construction of a for node. */
3033 __isl_give isl_id *
3034 ast_build_before_for (__isl_keep isl_ast_build *build, void *user)
3036 isl_union_map *dependences = (isl_union_map *) user;
3037 ast_build_info *for_info = XNEW (struct ast_build_info);
3038 isl_union_map *schedule = isl_ast_build_get_schedule (build);
3039 isl_space *schedule_space = isl_ast_build_get_schedule_space (build);
3040 int dimension = isl_space_dim (schedule_space, isl_dim_out);
3041 for_info->is_parallelizable =
3042 !carries_deps (schedule, dependences, dimension);
3043 isl_union_map_free (schedule);
3044 isl_space_free (schedule_space);
3045 isl_id *id = isl_id_alloc (isl_ast_build_get_ctx (build), "", for_info);
3046 return id;
3049 /* Set the separate option for all dimensions.
3050 This helps to reduce control overhead. */
3052 __isl_give isl_ast_build *
3053 translate_isl_ast_to_gimple::set_options (__isl_take isl_ast_build *control,
3054 __isl_keep isl_union_map *schedule)
3056 isl_ctx *ctx = isl_union_map_get_ctx (schedule);
3057 isl_space *range_space = isl_space_set_alloc (ctx, 0, 1);
3058 range_space =
3059 isl_space_set_tuple_name (range_space, isl_dim_set, "separate");
3060 isl_union_set *range =
3061 isl_union_set_from_set (isl_set_universe (range_space));
3062 isl_union_set *domain = isl_union_map_range (isl_union_map_copy (schedule));
3063 domain = isl_union_set_universe (domain);
3064 isl_union_map *options = isl_union_map_from_domain_and_range (domain, range);
3065 return isl_ast_build_set_options (control, options);
3068 /* Generate isl AST from schedule of SCOP. Also, collects IVS_PARAMS in IP. */
3070 __isl_give isl_ast_node *
3071 translate_isl_ast_to_gimple::scop_to_isl_ast (scop_p scop, ivs_params &ip)
3073 /* Generate loop upper bounds that consist of the current loop iterator, an
3074 operator (< or <=) and an expression not involving the iterator. If this
3075 option is not set, then the current loop iterator may appear several times
3076 in the upper bound. See the isl manual for more details. */
3077 isl_options_set_ast_build_atomic_upper_bound (scop->isl_context, true);
3079 add_parameters_to_ivs_params (scop, ip);
3080 isl_union_map *schedule_isl = generate_isl_schedule (scop);
3081 isl_ast_build *context_isl = generate_isl_context (scop);
3082 context_isl = set_options (context_isl, schedule_isl);
3083 isl_union_map *dependences = NULL;
3084 if (flag_loop_parallelize_all)
3086 dependences = scop_get_dependences (scop);
3087 context_isl =
3088 isl_ast_build_set_before_each_for (context_isl, ast_build_before_for,
3089 dependences);
3091 isl_ast_node *ast_isl = isl_ast_build_ast_from_schedule (context_isl,
3092 schedule_isl);
3093 if (dependences)
3094 isl_union_map_free (dependences);
3095 isl_ast_build_free (context_isl);
3096 return ast_isl;
3099 /* GIMPLE Loop Generator: generates loops from STMT in GIMPLE form for
3100 the given SCOP. Return true if code generation succeeded.
3102 FIXME: This is not yet a full implementation of the code generator
3103 with ISL ASTs. Generation of GIMPLE code has to be completed. */
3105 bool
3106 graphite_regenerate_ast_isl (scop_p scop)
3108 sese_info_p region = scop->scop_info;
3109 translate_isl_ast_to_gimple t (region);
3111 ifsese if_region = NULL;
3112 isl_ast_node *root_node;
3113 ivs_params ip;
3115 timevar_push (TV_GRAPHITE_CODE_GEN);
3116 root_node = t.scop_to_isl_ast (scop, ip);
3118 if (dump_file && (dump_flags & TDF_DETAILS))
3120 fprintf (dump_file, "ISL AST generated by ISL: \n");
3121 t.print_isl_ast_node (dump_file, root_node, scop->isl_context);
3124 recompute_all_dominators ();
3125 graphite_verify ();
3127 if_region = move_sese_in_condition (region);
3128 region->if_region = if_region;
3129 recompute_all_dominators ();
3131 loop_p context_loop = region->region.entry->src->loop_father;
3133 edge e = single_succ_edge (if_region->true_region->region.entry->dest);
3134 basic_block bb = split_edge (e);
3136 /* Update the true_region exit edge. */
3137 region->if_region->true_region->region.exit = single_succ_edge (bb);
3139 t.translate_isl_ast (context_loop, root_node, e, ip);
3140 if (t.codegen_error_p ())
3142 if (dump_file)
3143 fprintf (dump_file, "[codegen] unsuccessful,"
3144 " reverting back to the original code.\n");
3145 set_ifsese_condition (if_region, integer_zero_node);
3147 else
3149 t.translate_pending_phi_nodes ();
3150 if (!t.codegen_error_p ())
3152 sese_insert_phis_for_liveouts (region,
3153 if_region->region->region.exit->src,
3154 if_region->false_region->region.exit,
3155 if_region->true_region->region.exit);
3156 mark_virtual_operands_for_renaming (cfun);
3157 update_ssa (TODO_update_ssa);
3160 graphite_verify ();
3161 scev_reset ();
3162 recompute_all_dominators ();
3163 graphite_verify ();
3165 else
3167 if (dump_file)
3168 fprintf (dump_file, "[codegen] unsuccessful in translating"
3169 " pending phis, reverting back to the original code.\n");
3170 set_ifsese_condition (if_region, integer_zero_node);
3174 free (if_region->true_region);
3175 free (if_region->region);
3176 free (if_region);
3178 ivs_params_clear (ip);
3179 isl_ast_node_free (root_node);
3180 timevar_pop (TV_GRAPHITE_CODE_GEN);
3182 if (dump_file && (dump_flags & TDF_DETAILS))
3184 loop_p loop;
3185 int num_no_dependency = 0;
3187 FOR_EACH_LOOP (loop, 0)
3188 if (loop->can_be_parallel)
3189 num_no_dependency++;
3191 fprintf (dump_file, "%d loops carried no dependency.\n",
3192 num_no_dependency);
3195 return !t.codegen_error_p ();
3198 #endif /* HAVE_isl */