]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/graphite-isl-ast-to-gimple.c
16cb5faacda77ba061a3046059966a49913f90d2
[thirdparty/gcc.git] / gcc / graphite-isl-ast-to-gimple.c
1 /* Translation of ISL AST to Gimple.
2 Copyright (C) 2014-2015 Free Software Foundation, Inc.
3 Contributed by Roman Gareev <gareevroman@gmail.com>.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #define USES_ISL
22
23 #include "config.h"
24
25 #ifdef HAVE_isl
26
27 #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"
56
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>
63
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)
70 }
71 #endif
72
73 #include "graphite.h"
74
75 #include <map>
76
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. */
80
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;
85
86 struct ast_build_info
87 {
88 ast_build_info()
89 : is_parallelizable(false)
90 { }
91 bool is_parallelizable;
92 };
93
94 /* Converts a GMP constant VAL to a tree and returns it. */
95
96 static tree
97 gmp_cst_to_tree (tree type, mpz_t val)
98 {
99 tree t = type ? type : integer_type_node;
100 mpz_t tmp;
101
102 mpz_init (tmp);
103 mpz_set (tmp, val);
104 wide_int wi = wi::from_mpz (t, tmp, true);
105 mpz_clear (tmp);
106
107 return wide_int_to_tree (t, wi);
108 }
109
110 /* Verifies properties that GRAPHITE should maintain during translation. */
111
112 static inline void
113 graphite_verify (void)
114 {
115 checking_verify_loop_structure ();
116 checking_verify_loop_closed_ssa (true);
117 }
118
119 /* IVS_PARAMS maps ISL's scattering and parameter identifiers
120 to corresponding trees. */
121
122 typedef std::map<isl_id *, tree> ivs_params;
123
124 /* Free all memory allocated for ISL's identifiers. */
125
126 void ivs_params_clear (ivs_params &ip)
127 {
128 std::map<isl_id *, tree>::iterator it;
129 for (it = ip.begin ();
130 it != ip.end (); it++)
131 {
132 isl_id_free (it->first);
133 }
134 }
135
136 class translate_isl_ast_to_gimple
137 {
138 public:
139 translate_isl_ast_to_gimple (sese_info_p r)
140 : region (r), codegen_error (false)
141 { }
142
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);
147
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);
152
153 /* Create the loop for a isl_ast_node_for.
154
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);
161
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);
166
167 /* Translates an isl_ast_node_user to Gimple.
168
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);
173
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);
178
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);
183
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);
188
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);
193
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);
198
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);
204
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).
207
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);
215
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);
220
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);
226
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);
238
239 /* All loops generated by create_empty_loop_on_edge have the form of
240 a post-test loop:
241
242 do
243
244 {
245 body of the loop;
246 } while (lower bound < upper bound);
247
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);
254
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);
259
260 /* Inserts in iv_map a tuple (OLD_LOOP->num, NEW_NAME) for the induction
261 variables of the loops around GBB in SESE.
262
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);
269
270 /* Patch the missing arguments of the phi nodes. */
271
272 void translate_pending_phi_nodes (void);
273
274 /* Add ISL's parameter identifiers and corresponding trees to ivs_params. */
275
276 void add_parameters_to_ivs_params (scop_p scop, ivs_params &ip);
277
278 /* Get the maximal number of schedule dimensions in the scop SCOP. */
279
280 int get_max_schedule_dimensions (scop_p scop);
281
282 /* Generates a build, which specifies the constraints on the parameters. */
283
284 __isl_give isl_ast_build *generate_isl_context (scop_p scop);
285
286 /* Extend the schedule to NB_SCHEDULE_DIMS schedule dimensions.
287
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). */
292
293 __isl_give isl_map *extend_schedule (__isl_take isl_map *schedule,
294 int nb_schedule_dims);
295
296 /* Generates a schedule, which specifies an order used to
297 visit elements in a domain. */
298
299 __isl_give isl_union_map *generate_isl_schedule (scop_p scop);
300
301 /* Set the separate option for all dimensions.
302 This helps to reduce control overhead. */
303
304 __isl_give isl_ast_build * set_options (__isl_take isl_ast_build *control,
305 __isl_keep isl_union_map *schedule);
306
307 /* Generate isl AST from schedule of SCOP. Also, collects IVS_PARAMS in
308 IP. */
309
310 __isl_give isl_ast_node * scop_to_isl_ast (scop_p scop, ivs_params &ip);
311
312
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. */
316
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;
319
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. */
323
324 tree get_rename (basic_block new_bb, tree old_name,
325 basic_block old_bb, bool loop_phi) const;
326
327 /* For ops which are scev_analyzeable, we can regenerate a new name from
328 its scalar evolution around LOOP. */
329
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);
333
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. */
338
339 basic_block get_def_bb_for_const (basic_block bb, basic_block old_bb) const;
340
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. */
343
344 tree get_new_name (basic_block new_bb, tree op,
345 basic_block old_bb, bool loop_phi) const;
346
347 /* Collect all the operands of NEW_EXPR by recursively visiting each
348 operand. */
349
350 void collect_all_ssa_names (tree new_expr, vec<tree> *vec_ssa);
351
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. */
354
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);
358
359 /* Copy loop phi nodes from BB to NEW_BB. */
360
361 bool copy_loop_phi_nodes (basic_block bb, basic_block new_bb);
362
363 /* Add phi nodes to all merge points of all the diamonds enclosing the loop of
364 the close phi node PHI. */
365
366 bool add_close_phis_to_merge_points (gphi *old_phi, gphi *new_phi,
367 tree default_value);
368
369 tree add_close_phis_to_outer_loops (tree last_merge_name, edge merge_e,
370 gimple *old_close_phi);
371
372 /* Copy all the loop-close phi args from BB to NEW_BB. */
373
374 bool copy_loop_close_phi_args (basic_block old_bb, basic_block new_bb,
375 bool postpone);
376
377 /* Copy loop close phi nodes from BB to NEW_BB. */
378
379 bool copy_loop_close_phi_nodes (basic_block old_bb, basic_block new_bb);
380
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. */
385
386 bool copy_cond_phi_args (gphi *phi, gphi *new_phi, vec<tree> iv_map,
387 bool postpone);
388
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. */
392
393 bool copy_cond_phi_nodes (basic_block bb, basic_block new_bb,
394 vec<tree> iv_map);
395
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. */
399
400 bool graphite_copy_stmts_from_block (basic_block bb, basic_block new_bb,
401 vec<tree> iv_map);
402
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. */
407
408 edge copy_bb_and_scalar_dependences (basic_block bb, edge next_e,
409 vec<tree> iv_map);
410
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);
415
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.
421
422 Case1: OLD_BB->preds {BB1, BB2} and BB1 does not dominate BB2 and vice
423 versa. In this case DOMINATING_PRED = NULL.
424
425 Case2: OLD_BB->preds {BB1, BB2} and BB1 dominates BB2.
426
427 Returns true on successful copy of the args, false otherwise. */
428
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);
434
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. */
441
442 bool rename_uses (gimple *copy, gimple_stmt_iterator *gsi_tgt,
443 basic_block old_bb, loop_p loop, vec<tree> iv_map);
444
445 /* Register in RENAME_MAP the rename tuple (OLD_NAME, EXPR).
446 When OLD_NAME and EXPR are the same we assert. */
447
448 void set_rename (tree old_name, tree expr);
449
450 /* Create new names for all the definitions created by COPY and add
451 replacement mappings for each new name. */
452
453 void set_rename_for_each_def (gimple *stmt);
454
455 /* Insert each statement from SEQ at its earliest insertion p. */
456
457 void gsi_insert_earliest (gimple_seq seq);
458
459 /* Rename all the operands of NEW_EXPR by recursively visiting each
460 operand. */
461
462 tree rename_all_uses (tree new_expr, basic_block new_bb, basic_block old_bb);
463
464 bool codegen_error_p () const
465 { return codegen_error; }
466
467 /* Prints NODE to FILE. */
468
469 void print_isl_ast_node (FILE *file, __isl_keep isl_ast_node *node,
470 __isl_keep isl_ctx *ctx) const;
471
472 /* Return true when OP is a constant tree. */
473
474 bool is_constant (tree op) const
475 {
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;
480 }
481
482 private:
483 /* The region to be translated. */
484 sese_info_p region;
485
486 /* This flag is set when an error occurred during the translation of ISL AST
487 to Gimple. */
488 bool codegen_error;
489
490 /* A vector of all the edges at if_condition merge points. */
491 auto_vec<edge, 2> merge_points;
492 };
493
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).
496
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. */
501
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)
507 {
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);
518 }
519
520 /* Converts an isl_ast_expr_int expression E to a GCC expression tree of
521 type TYPE. */
522
523 tree
524 translate_isl_ast_to_gimple::
525 gcc_expression_from_isl_expr_int (tree type, __isl_take isl_ast_expr *expr)
526 {
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;
540 }
541
542 /* Converts a binary isl_ast_expr_op expression E to a GCC expression tree of
543 type TYPE. */
544
545 tree
546 translate_isl_ast_to_gimple::
547 binary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
548 {
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)
556 {
557 case isl_ast_op_add:
558 return fold_build2 (PLUS_EXPR, type, tree_lhs_expr, tree_rhs_expr);
559
560 case isl_ast_op_sub:
561 return fold_build2 (MINUS_EXPR, type, tree_lhs_expr, tree_rhs_expr);
562
563 case isl_ast_op_mul:
564 return fold_build2 (MULT_EXPR, type, tree_lhs_expr, tree_rhs_expr);
565
566 case isl_ast_op_div:
567 return fold_build2 (EXACT_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr);
568
569 case isl_ast_op_pdiv_q:
570 return fold_build2 (TRUNC_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr);
571
572 case isl_ast_op_pdiv_r:
573 return fold_build2 (TRUNC_MOD_EXPR, type, tree_lhs_expr, tree_rhs_expr);
574
575 case isl_ast_op_fdiv_q:
576 return fold_build2 (FLOOR_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr);
577
578 case isl_ast_op_and:
579 return fold_build2 (TRUTH_ANDIF_EXPR, type,
580 tree_lhs_expr, tree_rhs_expr);
581
582 case isl_ast_op_or:
583 return fold_build2 (TRUTH_ORIF_EXPR, type, tree_lhs_expr, tree_rhs_expr);
584
585 case isl_ast_op_eq:
586 return fold_build2 (EQ_EXPR, type, tree_lhs_expr, tree_rhs_expr);
587
588 case isl_ast_op_le:
589 return fold_build2 (LE_EXPR, type, tree_lhs_expr, tree_rhs_expr);
590
591 case isl_ast_op_lt:
592 return fold_build2 (LT_EXPR, type, tree_lhs_expr, tree_rhs_expr);
593
594 case isl_ast_op_ge:
595 return fold_build2 (GE_EXPR, type, tree_lhs_expr, tree_rhs_expr);
596
597 case isl_ast_op_gt:
598 return fold_build2 (GT_EXPR, type, tree_lhs_expr, tree_rhs_expr);
599
600 default:
601 gcc_unreachable ();
602 }
603 }
604
605 /* Converts a ternary isl_ast_expr_op expression E to a GCC expression tree of
606 type TYPE. */
607
608 tree
609 translate_isl_ast_to_gimple::
610 ternary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
611 {
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);
625 }
626
627 /* Converts a unary isl_ast_expr_op expression E to a GCC expression tree of
628 type TYPE. */
629
630 tree
631 translate_isl_ast_to_gimple::
632 unary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
633 {
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);
639 }
640
641 /* Converts an isl_ast_expr_op expression E with unknown number of arguments
642 to a GCC expression tree of type TYPE. */
643
644 tree
645 translate_isl_ast_to_gimple::
646 nary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
647 {
648 enum tree_code op_code;
649 switch (isl_ast_expr_get_op_type (expr))
650 {
651 case isl_ast_op_max:
652 op_code = MAX_EXPR;
653 break;
654
655 case isl_ast_op_min:
656 op_code = MIN_EXPR;
657 break;
658
659 default:
660 gcc_unreachable ();
661 }
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++)
666 {
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);
670 }
671 isl_ast_expr_free (expr);
672 return res;
673 }
674
675 /* Converts an isl_ast_expr_op expression E to a GCC expression tree of
676 type TYPE. */
677
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)
682 {
683 gcc_assert (isl_ast_expr_get_type (expr) == isl_ast_expr_op);
684 switch (isl_ast_expr_get_op_type (expr))
685 {
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 ();
693
694 case isl_ast_op_max:
695 case isl_ast_op_min:
696 return nary_op_to_tree (type, expr, ip);
697
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);
713
714 case isl_ast_op_minus:
715 return unary_op_to_tree (type, expr, ip);
716
717 case isl_ast_op_cond:
718 return ternary_op_to_tree (type, expr, ip);
719
720 default:
721 gcc_unreachable ();
722 }
723
724 return NULL_TREE;
725 }
726
727 /* Converts an ISL AST expression E back to a GCC expression tree of
728 type TYPE. */
729
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)
734 {
735 switch (isl_ast_expr_get_type (expr))
736 {
737 case isl_ast_expr_id:
738 return gcc_expression_from_isl_ast_expr_id (type, expr, ip);
739
740 case isl_ast_expr_int:
741 return gcc_expression_from_isl_expr_int (type, expr);
742
743 case isl_ast_expr_op:
744 return gcc_expression_from_isl_expr_op (type, expr, ip);
745
746 default:
747 gcc_unreachable ();
748 }
749
750 return NULL_TREE;
751 }
752
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. */
760
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)
766 {
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);
774
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;
784 }
785
786 /* Create the loop for a isl_ast_node_for.
787
788 - NEXT_E is the edge where new generated code should be attached. */
789
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)
796 {
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;
803
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);
808
809 /* Early return if we failed to translate loop body. */
810 if (!next_e || codegen_error_p ())
811 return NULL;
812
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);
816
817 if (flag_loop_parallelize_all)
818 {
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);
825 }
826
827 return last_e;
828 }
829
830 /* We use this function to get the upper bound because of the form,
831 which is used by isl to represent loops:
832
833 for (iterator = init; cond; iterator += inc)
834
835 {
836
837 ...
838
839 }
840
841 The loop condition is an arbitrary expression, which contains the
842 current loop iterator.
843
844 (e.g. iterator + 3 < B && C > iterator + A)
845
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.
853
854 (e.g iterator <= upper-bound-expression-without-iterator) */
855
856 static __isl_give isl_ast_expr *
857 get_upper_bound (__isl_keep isl_ast_node *node_for)
858 {
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))
864 {
865 case isl_ast_op_le:
866 res = isl_ast_expr_get_op_arg (for_cond, 1);
867 break;
868
869 case isl_ast_op_lt:
870 {
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;
877 }
878
879 default:
880 gcc_unreachable ();
881 }
882 isl_ast_expr_free (for_cond);
883 return res;
884 }
885
886 /* All loops generated by create_empty_loop_on_edge have the form of
887 a post-test loop:
888
889 do
890
891 {
892 body of the loop;
893 } while (lower bound < upper bound);
894
895 We create a new if region protecting the loop to be executed, if
896 the execution count is zero (lower bound > upper bound). */
897
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)
903 {
904 gcc_assert (isl_ast_node_get_type (node_for) == isl_ast_node_for);
905 tree cond_expr;
906 edge exit_edge;
907
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);
914
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
919 {
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);
930
931 cond_expr = fold_build2 (LT_EXPR, boolean_type_node, *lb, ub_one);
932 }
933
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);
938
939 return exit_edge;
940 }
941
942 /* Translates an isl_ast_node_for to Gimple. */
943
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)
948 {
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);
953
954 if (last_e == next_e)
955 {
956 /* There was no guard generated. */
957 last_e = single_succ_edge (split_edge (last_e));
958
959 translate_isl_ast_for_loop (context_loop, node, next_e,
960 type, lb, ub, ip);
961 return last_e;
962 }
963
964 edge true_e = get_true_edge_from_guard_bb (next_e->dest);
965 merge_points.safe_push (last_e);
966
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);
969
970 return last_e;
971 }
972
973 /* Inserts in iv_map a tuple (OLD_LOOP->num, NEW_NAME) for the induction
974 variables of the loops around GBB in SESE.
975
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. */
979
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)
985 {
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++)
991 {
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;
998 }
999 }
1000
1001 /* Translates an isl_ast_node_user to Gimple.
1002
1003 FIXME: We should remove iv_map.create (loop->num + 1), if it is possible. */
1004
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)
1009 {
1010 gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_user);
1011
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);
1015
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);
1019
1020 gimple_poly_bb_p gbb = PBB_BLACK_BOX (pbb);
1021
1022 isl_ast_expr_free (name_expr);
1023 isl_id_free (name_id);
1024
1025 gcc_assert (GBB_BB (gbb) != ENTRY_BLOCK_PTR_FOR_FN (cfun) &&
1026 "The entry block should not even appear within a scop");
1027
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);
1032
1033 build_iv_mapping (iv_map, gbb, user_expr, ip, pbb->scop->scop_info->region);
1034 isl_ast_expr_free (user_expr);
1035
1036 if (dump_file)
1037 {
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);
1042 }
1043
1044 next_e = copy_bb_and_scalar_dependences (GBB_BB (gbb), next_e,
1045 iv_map);
1046
1047 iv_map.release ();
1048
1049 if (codegen_error_p ())
1050 return NULL;
1051
1052 if (dump_file)
1053 {
1054 fprintf (dump_file, "[codegen] (after copy) new basic block\n");
1055 print_loops_bb (dump_file, next_e->src, 0, 3);
1056 }
1057
1058 return next_e;
1059 }
1060
1061 /* Translates an isl_ast_node_block to Gimple. */
1062
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)
1068 {
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++)
1073 {
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);
1077 }
1078 isl_ast_node_list_free (node_list);
1079 return next_e;
1080 }
1081
1082 /* Creates a new if region corresponding to ISL's cond. */
1083
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)
1088 {
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;
1094 }
1095
1096 /* Translates an isl_ast_node_if to Gimple. */
1097
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)
1103 {
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);
1109
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);
1113
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);
1118
1119 isl_ast_node_free (else_node);
1120 return last_e;
1121 }
1122
1123 /* Translates an ISL AST node NODE to GCC representation in the
1124 context of a SESE. */
1125
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)
1130 {
1131 if (codegen_error_p ())
1132 return NULL;
1133
1134 switch (isl_ast_node_get_type (node))
1135 {
1136 case isl_ast_node_error:
1137 gcc_unreachable ();
1138
1139 case isl_ast_node_for:
1140 return translate_isl_ast_node_for (context_loop, node,
1141 next_e, ip);
1142
1143 case isl_ast_node_if:
1144 return translate_isl_ast_node_if (context_loop, node,
1145 next_e, ip);
1146
1147 case isl_ast_node_user:
1148 return translate_isl_ast_node_user (node, next_e, ip);
1149
1150 case isl_ast_node_block:
1151 return translate_isl_ast_node_block (context_loop, node,
1152 next_e, ip);
1153
1154 default:
1155 gcc_unreachable ();
1156 }
1157 }
1158
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. */
1162
1163 static bool
1164 bb_contains_loop_close_phi_nodes (basic_block bb)
1165 {
1166 return single_pred_p (bb)
1167 && bb->loop_father != single_pred_edge (bb)->src->loop_father;
1168 }
1169
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. */
1172
1173 static bool
1174 bb_contains_loop_phi_nodes (basic_block bb)
1175 {
1176 gcc_assert (EDGE_COUNT (bb->preds) <= 2);
1177
1178 if (bb->preds->length () == 1)
1179 return false;
1180
1181 unsigned depth = loop_depth (bb->loop_father);
1182
1183 edge preds[2] = { (*bb->preds)[0], (*bb->preds)[1] };
1184
1185 if (depth > loop_depth (preds[0]->src->loop_father)
1186 || depth > loop_depth (preds[1]->src->loop_father))
1187 return true;
1188
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;
1194
1195 if (bb->loop_father != preds[1]->src->loop_father
1196 && bb->loop_father == preds[0]->src->loop_father)
1197 return true;
1198
1199 return false;
1200 }
1201
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. */
1204
1205 static bool
1206 is_loop_closed_ssa_use (basic_block bb, tree use)
1207 {
1208 if (TREE_CODE (use) != SSA_NAME || virtual_operand_p (use))
1209 return true;
1210
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;
1214
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));
1219 }
1220
1221 /* Return the number of phi nodes in BB. */
1222
1223 static int
1224 number_of_phi_nodes (basic_block bb)
1225 {
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;
1231 }
1232
1233 /* Returns true if BB uses name in one of its PHIs. */
1234
1235 static bool
1236 phi_uses_name (basic_block bb, tree name)
1237 {
1238 for (gphi_iterator psi = gsi_start_phis (bb); !gsi_end_p (psi);
1239 gsi_next (&psi))
1240 {
1241 gphi *phi = psi.phi ();
1242 for (unsigned i = 0; i < gimple_phi_num_args (phi); i++)
1243 {
1244 tree use_arg = gimple_phi_arg_def (phi, i);
1245 if (use_arg == name)
1246 return true;
1247 }
1248 }
1249 return false;
1250 }
1251
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. */
1255
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
1260 {
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))
1264 {
1265 if (dump_file)
1266 {
1267 fprintf (dump_file, "[codegen] rename not in loop closed ssa:");
1268 print_generic_expr (dump_file, rename, 0);
1269 fprintf (dump_file, "\n");
1270 }
1271 return false;
1272 }
1273
1274 if (dominated_by_p (CDI_DOMINATORS, use_bb, def_bb))
1275 return true;
1276
1277 if (bb_contains_loop_phi_nodes (use_bb) && loop_phi)
1278 {
1279 /* The loop-header dominates the loop-body. */
1280 if (!dominated_by_p (CDI_DOMINATORS, def_bb, use_bb))
1281 return false;
1282
1283 /* RENAME would be used in loop-phi. */
1284 gcc_assert (number_of_phi_nodes (use_bb));
1285
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;
1291 }
1292 return false;
1293 }
1294
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. */
1298
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
1304 {
1305 gcc_assert (TREE_CODE (old_name) == SSA_NAME);
1306 vec <tree> *renames = region->rename_map->get (old_name);
1307
1308 if (!renames || renames->is_empty ())
1309 return NULL_TREE;
1310
1311 if (1 == renames->length ())
1312 {
1313 tree rename = (*renames)[0];
1314 if (TREE_CODE (rename) == SSA_NAME)
1315 {
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;
1320 }
1321
1322 if (is_constant (rename))
1323 return rename;
1324
1325 return NULL_TREE;
1326 }
1327
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)
1334 {
1335 basic_block t2_bb = gimple_bb (SSA_NAME_DEF_STMT (t2));
1336
1337 /* Defined in the same basic block as used. */
1338 if (t2_bb == new_bb)
1339 return t2;
1340
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;
1344
1345 /* Compute the nearest dominator. */
1346 if (!t1 || dominated_by_p (CDI_DOMINATORS, t2_bb, t1_bb))
1347 {
1348 t1_bb = t2_bb;
1349 t1 = t2;
1350 }
1351 }
1352
1353 return t1;
1354 }
1355
1356 /* Register in RENAME_MAP the rename tuple (OLD_NAME, EXPR).
1357 When OLD_NAME and EXPR are the same we assert. */
1358
1359 void
1360 translate_isl_ast_to_gimple::set_rename (tree old_name, tree expr)
1361 {
1362 if (dump_file)
1363 {
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");
1369 }
1370
1371 if (old_name == expr)
1372 return;
1373
1374 vec <tree> *renames = region->rename_map->get (old_name);
1375
1376 if (renames)
1377 renames->safe_push (expr);
1378 else
1379 {
1380 vec<tree> r;
1381 r.create (2);
1382 r.safe_push (expr);
1383 region->rename_map->put (old_name, r);
1384 }
1385 }
1386
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. */
1390
1391 gimple_stmt_iterator
1392 later_of_the_two (gimple_stmt_iterator gsi1, gimple_stmt_iterator gsi2)
1393 {
1394 basic_block bb1 = gsi_bb (gsi1);
1395 basic_block bb2 = gsi_bb (gsi2);
1396
1397 /* Find the iterator which is the latest. */
1398 if (bb1 == bb2)
1399 {
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));
1409
1410 return gsi1;
1411 }
1412
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;
1416
1417 gcc_assert (dominated_by_p (CDI_DOMINATORS, bb2, bb1));
1418 return gsi2;
1419 }
1420
1421 /* Insert each statement from SEQ at its earliest insertion p. */
1422
1423 void
1424 translate_isl_ast_to_gimple::gsi_insert_earliest (gimple_seq seq)
1425 {
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);
1429
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));
1437
1438 int i;
1439 gimple *use_stmt;
1440 FOR_EACH_VEC_ELT (stmts, i, use_stmt)
1441 {
1442 gcc_assert (gimple_code (use_stmt) != GIMPLE_PHI);
1443 gimple_stmt_iterator gsi_def_stmt = gsi_start_bb_nondebug (begin_bb);
1444
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)
1448 {
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;
1453
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);
1458
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;
1463
1464 gsi_def_stmt = later_of_the_two (gsi_stmt, gsi_def_stmt);
1465 }
1466
1467 if (!gsi_stmt (gsi_def_stmt))
1468 {
1469 gimple_stmt_iterator gsi = gsi_after_labels (gsi_bb (gsi_def_stmt));
1470 gsi_insert_before (&gsi, use_stmt, GSI_NEW_STMT);
1471 }
1472 else if (gimple_code (gsi_stmt (gsi_def_stmt)) == GIMPLE_PHI)
1473 {
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);
1478 }
1479 else
1480 gsi_insert_after (&gsi_def_stmt, use_stmt, GSI_NEW_STMT);
1481
1482 if (dump_file)
1483 {
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);
1487 }
1488 }
1489 }
1490
1491 /* Collect all the operands of NEW_EXPR by recursively visiting each
1492 operand. */
1493
1494 void
1495 translate_isl_ast_to_gimple::collect_all_ssa_names (tree new_expr,
1496 vec<tree> *vec_ssa)
1497 {
1498
1499 /* Rename all uses in new_expr. */
1500 if (TREE_CODE (new_expr) == SSA_NAME)
1501 {
1502 vec_ssa->safe_push (new_expr);
1503 return;
1504 }
1505
1506 /* Iterate over SSA_NAMES in NEW_EXPR. */
1507 for (int i = 0; i < (TREE_CODE_LENGTH (TREE_CODE (new_expr))); i++)
1508 {
1509 tree op = TREE_OPERAND (new_expr, i);
1510 collect_all_ssa_names (op, vec_ssa);
1511 }
1512 }
1513
1514 /* This is abridged version of the function:
1515 tree.c:substitute_in_expr (tree exp, tree f, tree r). */
1516
1517 static tree
1518 substitute_ssa_name (tree exp, tree f, tree r)
1519 {
1520 enum tree_code code = TREE_CODE (exp);
1521 tree op0, op1, op2, op3;
1522 tree new_tree;
1523
1524 /* We handle TREE_LIST and COMPONENT_REF separately. */
1525 if (code == TREE_LIST)
1526 {
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;
1531
1532 return tree_cons (TREE_PURPOSE (exp), op1, op0);
1533 }
1534 else if (code == COMPONENT_REF)
1535 {
1536 tree inner;
1537
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))
1543 ;
1544
1545 /* The field. */
1546 op1 = TREE_OPERAND (exp, 1);
1547
1548 if (TREE_CODE (inner) == PLACEHOLDER_EXPR && op1 == f)
1549 return r;
1550
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;
1554
1555 op0 = substitute_ssa_name (TREE_OPERAND (exp, 0), f, r);
1556 if (op0 == TREE_OPERAND (exp, 0))
1557 return exp;
1558
1559 new_tree
1560 = fold_build3 (COMPONENT_REF, TREE_TYPE (exp), op0, op1, NULL_TREE);
1561 }
1562 else
1563 switch (TREE_CODE_CLASS (code))
1564 {
1565 case tcc_constant:
1566 return exp;
1567
1568 case tcc_declaration:
1569 if (exp == f)
1570 return r;
1571 else
1572 return exp;
1573
1574 case tcc_expression:
1575 if (exp == f)
1576 return r;
1577
1578 /* Fall through... */
1579
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))
1586 {
1587 case 0:
1588 if (exp == f)
1589 return r;
1590 return exp;
1591
1592 case 1:
1593 op0 = substitute_ssa_name (TREE_OPERAND (exp, 0), f, r);
1594 if (op0 == TREE_OPERAND (exp, 0))
1595 return exp;
1596
1597 new_tree = fold_build1 (code, TREE_TYPE (exp), op0);
1598 break;
1599
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);
1603
1604 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1))
1605 return exp;
1606
1607 new_tree = fold_build2 (code, TREE_TYPE (exp), op0, op1);
1608 break;
1609
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);
1614
1615 if (op0 == TREE_OPERAND (exp, 0) && op1 == TREE_OPERAND (exp, 1)
1616 && op2 == TREE_OPERAND (exp, 2))
1617 return exp;
1618
1619 new_tree = fold_build3 (code, TREE_TYPE (exp), op0, op1, op2);
1620 break;
1621
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);
1627
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;
1632
1633 new_tree
1634 = fold (build4 (code, TREE_TYPE (exp), op0, op1, op2, op3));
1635 break;
1636
1637 default:
1638 gcc_unreachable ();
1639 }
1640 break;
1641
1642 case tcc_vl_exp:
1643 default:
1644 gcc_unreachable ();
1645 }
1646
1647 TREE_READONLY (new_tree) |= TREE_READONLY (exp);
1648
1649 if (code == INDIRECT_REF || code == ARRAY_REF || code == ARRAY_RANGE_REF)
1650 TREE_THIS_NOTRAP (new_tree) |= TREE_THIS_NOTRAP (exp);
1651
1652 return new_tree;
1653 }
1654
1655 /* Rename all the operands of NEW_EXPR by recursively visiting each operand. */
1656
1657 tree
1658 translate_isl_ast_to_gimple::rename_all_uses (tree new_expr, basic_block new_bb,
1659 basic_block old_bb)
1660 {
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);
1668
1669 return new_expr;
1670 }
1671
1672 /* For ops which are scev_analyzeable, we can regenerate a new name from its
1673 scalar evolution around LOOP. */
1674
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)
1680 {
1681 tree scev = scalar_evolution_in_region (region->region, loop, old_name);
1682
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))
1689 {
1690 codegen_error = true;
1691 return build_zero_cst (TREE_TYPE (old_name));
1692 }
1693
1694 new_expr = chrec_apply_map (scev, iv_map);
1695
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))
1702 {
1703 codegen_error = true;
1704 return build_zero_cst (TREE_TYPE (old_name));
1705 }
1706
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)
1710 {
1711 basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (new_expr));
1712 if (bb && !dominated_by_p (CDI_DOMINATORS, new_bb, bb))
1713 {
1714 codegen_error = true;
1715 return build_zero_cst (TREE_TYPE (old_name));
1716 }
1717 }
1718
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)
1723 {
1724 basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (new_expr));
1725 if (bb && !dominated_by_p (CDI_DOMINATORS, new_bb, bb))
1726 {
1727 codegen_error = true;
1728 return build_zero_cst (TREE_TYPE (old_name));
1729 }
1730 }
1731
1732 /* Replace the old_name with the new_expr. */
1733 return force_gimple_operand (unshare_expr (new_expr), stmts,
1734 true, NULL_TREE);
1735 }
1736
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. */
1743
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)
1749 {
1750 bool changed = false;
1751
1752 if (is_gimple_debug (copy))
1753 {
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 ();
1760
1761 return false;
1762 }
1763
1764 if (dump_file)
1765 {
1766 fprintf (dump_file, "[codegen] renaming uses of stmt: ");
1767 print_gimple_stmt (dump_file, copy, 0, 0);
1768 }
1769
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)
1773 {
1774 tree old_name = USE_FROM_PTR (use_p);
1775
1776 if (dump_file)
1777 {
1778 fprintf (dump_file, "[codegen] renaming old_name = ");
1779 print_generic_expr (dump_file, old_name, 0);
1780 fprintf (dump_file, "\n");
1781 }
1782
1783 if (TREE_CODE (old_name) != SSA_NAME
1784 || SSA_NAME_IS_DEFAULT_DEF (old_name))
1785 continue;
1786
1787 changed = true;
1788 tree new_expr = get_rename (gsi_tgt->bb, old_name,
1789 old_bb, false);
1790
1791 if (new_expr)
1792 {
1793 tree type_old_name = TREE_TYPE (old_name);
1794 tree type_new_expr = TREE_TYPE (new_expr);
1795
1796 if (dump_file)
1797 {
1798 fprintf (dump_file, "[codegen] from rename_map: new_name = ");
1799 print_generic_expr (dump_file, new_expr, 0);
1800 fprintf (dump_file, "\n");
1801 }
1802
1803 if (type_old_name != type_new_expr
1804 || TREE_CODE (new_expr) != SSA_NAME)
1805 {
1806 tree var = create_tmp_var (type_old_name, "var");
1807
1808 if (!useless_type_conversion_p (type_old_name, type_new_expr))
1809 new_expr = fold_convert (type_old_name, new_expr);
1810
1811 gimple_seq stmts;
1812 new_expr = force_gimple_operand (new_expr, &stmts, true, var);
1813 gsi_insert_earliest (stmts);
1814 }
1815
1816 replace_exp (use_p, new_expr);
1817 continue;
1818 }
1819
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;
1825
1826 if (dump_file)
1827 {
1828 fprintf (dump_file, "[codegen] not in rename map, scev: ");
1829 print_generic_expr (dump_file, new_expr, 0);
1830 fprintf (dump_file, "\n");
1831 }
1832
1833 gsi_insert_earliest (stmts);
1834 replace_exp (use_p, new_expr);
1835
1836 if (TREE_CODE (new_expr) == INTEGER_CST
1837 && is_gimple_assign (copy))
1838 {
1839 tree rhs = gimple_assign_rhs1 (copy);
1840
1841 if (TREE_CODE (rhs) == ADDR_EXPR)
1842 recompute_tree_invariant_for_addr_expr (rhs);
1843 }
1844
1845 set_rename (old_name, new_expr);
1846 }
1847
1848 return changed;
1849 }
1850
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. */
1855
1856 basic_block
1857 translate_isl_ast_to_gimple::get_def_bb_for_const (basic_block bb,
1858 basic_block old_bb) const
1859 {
1860 vec <basic_block> *bbs = region->copied_bb_map->get (old_bb);
1861
1862 if (!bbs || bbs->is_empty ())
1863 return NULL;
1864
1865 if (1 == bbs->length ())
1866 return (*bbs)[0];
1867
1868 int i;
1869 basic_block b1 = NULL, b2;
1870 FOR_EACH_VEC_ELT (*bbs, i, b2)
1871 {
1872 if (b2 == bb)
1873 return bb;
1874
1875 /* BB and B2 are in two unrelated if-clauses. */
1876 if (!dominated_by_p (CDI_DOMINATORS, bb, b2))
1877 continue;
1878
1879 /* Compute the nearest dominator. */
1880 if (!b1 || dominated_by_p (CDI_DOMINATORS, b2, b1))
1881 b1 = b2;
1882 }
1883
1884 gcc_assert (b1);
1885 return b1;
1886 }
1887
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. */
1890
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
1895 {
1896 /* For constants the names are the same. */
1897 if (is_constant (op))
1898 return op;
1899
1900 return get_rename (new_bb, op, old_bb, loop_phi);
1901 }
1902
1903 /* Return a debug location for OP. */
1904
1905 static location_t
1906 get_loc (tree op)
1907 {
1908 location_t loc = UNKNOWN_LOCATION;
1909
1910 if (TREE_CODE (op) == SSA_NAME)
1911 loc = gimple_location (SSA_NAME_DEF_STMT (op));
1912 return loc;
1913 }
1914
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. */
1918
1919 std::pair<edge, edge>
1920 get_edges (basic_block bb)
1921 {
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;
1931 }
1932
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. */
1935
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)
1941 {
1942 gcc_assert (gimple_phi_num_args (old_phi) == gimple_phi_num_args (new_phi));
1943
1944 basic_block new_bb = gimple_bb (new_phi);
1945 for (unsigned i = 0; i < gimple_phi_num_args (old_phi); i++)
1946 {
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;
1952
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)
1957 {
1958 add_phi_arg (new_phi, new_name, e, get_loc (old_name));
1959 continue;
1960 }
1961
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)
1968 {
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");
1974 }
1975 else
1976 /* Either we should add the arg to phi or, we should postpone. */
1977 return false;
1978 }
1979 return true;
1980 }
1981
1982 /* Copy loop phi nodes from BB to NEW_BB. */
1983
1984 bool
1985 translate_isl_ast_to_gimple::copy_loop_phi_nodes (basic_block bb,
1986 basic_block new_bb)
1987 {
1988 if (dump_file)
1989 fprintf (dump_file, "[codegen] copying loop phi nodes in bb_%d.\n",
1990 new_bb->index);
1991
1992 /* Loop phi nodes should have only two arguments. */
1993 gcc_assert (2 == EDGE_COUNT (bb->preds));
1994
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);
1997
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);
2000
2001 for (gphi_iterator psi = gsi_start_phis (bb); !gsi_end_p (psi);
2002 gsi_next (&psi))
2003 {
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;
2010
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);
2018 }
2019
2020 return true;
2021 }
2022
2023 /* Return the init value of PHI, the value coming from outside the loop. */
2024
2025 static tree
2026 get_loop_init_value (gphi *phi)
2027 {
2028
2029 loop_p loop = gimple_bb (phi)->loop_father;
2030
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);
2036
2037 return NULL_TREE;
2038 }
2039
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. */
2042
2043 static tree
2044 find_init_value (gimple *def)
2045 {
2046 if (gimple_code (def) == GIMPLE_PHI)
2047 return get_loop_init_value (as_a <gphi*> (def));
2048
2049 if (gimple_vuse (def))
2050 return NULL_TREE;
2051
2052 ssa_op_iter iter;
2053 use_operand_p use_p;
2054 FOR_EACH_SSA_USE_OPERAND (use_p, def, iter, SSA_OP_USE)
2055 {
2056 tree use = USE_FROM_PTR (use_p);
2057 if (TREE_CODE (use) == SSA_NAME)
2058 {
2059 if (tree res = find_init_value (SSA_NAME_DEF_STMT (use)))
2060 return res;
2061 }
2062 }
2063
2064 return NULL_TREE;
2065 }
2066
2067 /* Return the init value, the value coming from outside the loop. */
2068
2069 static tree
2070 find_init_value_close_phi (gphi *phi)
2071 {
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);
2076 }
2077
2078
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)
2082 {
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;
2088
2089 loop_p loop = bb->loop_father;
2090 if (!loop_in_sese_p (loop, codegen_region))
2091 return last_merge_name;
2092
2093 edge e = single_exit (loop);
2094
2095 if (dominated_by_p (CDI_DOMINATORS, e->dest, last_e->src))
2096 return last_merge_name;
2097
2098 tree old_name = gimple_phi_arg_def (old_close_phi, 0);
2099 tree old_close_phi_name = gimple_phi_result (old_close_phi);
2100
2101 bb = e->dest;
2102 if (!bb_contains_loop_close_phi_nodes (bb) || !single_succ_p (bb))
2103 bb = split_edge (e);
2104
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;
2111
2112 return add_close_phis_to_outer_loops (last_merge_name, last_e, old_close_phi);
2113 }
2114
2115 /* Add phi nodes to all merge points of all the diamonds enclosing the loop of
2116 the close phi node PHI. */
2117
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)
2121 {
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))
2125 {
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);
2130 }
2131
2132 basic_block new_close_phi_bb = gimple_bb (new_close_phi);
2133
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);
2138
2139 int i;
2140 edge merge_e;
2141 FOR_EACH_VEC_ELT_REVERSE (merge_points, i, merge_e)
2142 {
2143 basic_block new_merge_bb = merge_e->src;
2144 if (!dominated_by_p (CDI_DOMINATORS, new_merge_bb, default_value_bb))
2145 continue;
2146
2147 last_merge_name = add_close_phis_to_outer_loops (last_merge_name, merge_e,
2148 old_close_phi);
2149
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);
2154
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;
2163
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;
2169
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));
2172
2173 if (dump_file)
2174 {
2175 fprintf (dump_file, "[codegen] Adding guard-phi: ");
2176 print_gimple_stmt (dump_file, merge_phi, 0, 0);
2177 }
2178
2179 update_stmt (merge_phi);
2180 last_merge_name = merge_res;
2181 }
2182
2183 return true;
2184 }
2185
2186 /* Copy all the loop-close phi args from BB to NEW_BB. */
2187
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)
2192 {
2193 for (gphi_iterator psi = gsi_start_phis (old_bb); !gsi_end_p (psi);
2194 gsi_next (&psi))
2195 {
2196 gphi *old_close_phi = psi.phi ();
2197 tree res = gimple_phi_result (old_close_phi);
2198 if (virtual_operand_p (res))
2199 continue;
2200
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 ();
2204
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);
2209
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);
2212
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;
2218
2219 add_phi_arg (new_close_phi, new_name, single_pred_edge (new_bb),
2220 get_loc (old_name));
2221 if (dump_file)
2222 {
2223 fprintf (dump_file, "[codegen] Adding loop close phi: ");
2224 print_gimple_stmt (dump_file, new_close_phi, 0, 0);
2225 }
2226
2227 update_stmt (new_close_phi);
2228
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;
2233
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);
2236
2237 /* A close phi must come from a loop-phi having a default value. */
2238 if (!default_value)
2239 {
2240 if (!postpone)
2241 return false;
2242
2243 region->incomplete_phis.safe_push (std::make_pair (old_close_phi,
2244 new_close_phi));
2245 if (dump_file)
2246 {
2247 fprintf (dump_file, "[codegen] postpone close phi nodes: ");
2248 print_gimple_stmt (dump_file, new_close_phi, 0, 0);
2249 }
2250 continue;
2251 }
2252
2253 if (!add_close_phis_to_merge_points (old_close_phi, new_close_phi,
2254 default_value))
2255 return false;
2256 }
2257
2258 return true;
2259 }
2260
2261 /* Copy loop close phi nodes from BB to NEW_BB. */
2262
2263 bool
2264 translate_isl_ast_to_gimple::copy_loop_close_phi_nodes (basic_block old_bb,
2265 basic_block new_bb)
2266 {
2267 if (dump_file)
2268 fprintf (dump_file, "[codegen] copying loop close 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));
2272
2273 return copy_loop_close_phi_args (old_bb, new_bb, true);
2274 }
2275
2276
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.
2282
2283 Case1: OLD_BB->preds {BB1, BB2} and BB1 does not dominate BB2 and vice versa.
2284 In this case DOMINATING_PRED = NULL.
2285
2286 Case2: OLD_BB->preds {BB1, BB2} and BB1 dominates BB2.
2287
2288 Returns true on successful copy of the args, false otherwise. */
2289
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)
2297 {
2298 basic_block def_pred[2] = { NULL, NULL };
2299 int not_found_bb_index = -1;
2300 for (int i = 0; i < 2; i++)
2301 {
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]));
2309
2310 if (!def_pred[i])
2311 {
2312 /* When non are available bail out. */
2313 if (not_found_bb_index != -1)
2314 return false;
2315 not_found_bb_index = i;
2316 }
2317 }
2318
2319 /* Here we are pattern matching on the structure of CFG w.r.t. old one. */
2320 if (old_bb_dominating_edge)
2321 {
2322 if (not_found_bb_index != -1)
2323 return false;
2324
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);
2329
2330 /* Could not find a mapping. */
2331 if (!bbs)
2332 return false;
2333
2334 basic_block new_pred = NULL;
2335 basic_block b;
2336 int i;
2337 FOR_EACH_VEC_ELT (*bbs, i, b)
2338 {
2339 if (dominated_by_p (CDI_DOMINATORS, new_pred1, b))
2340 {
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;
2346 }
2347 if (dominated_by_p (CDI_DOMINATORS, new_pred2, b))
2348 {
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;
2354 }
2355 }
2356
2357 if (!new_pred)
2358 return false;
2359
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++)
2368 {
2369 edge new_bb_pred_edge = gimple_phi_arg_edge (new_phi, inserted_edge);
2370 if (new_non_dominating_edge == new_bb_pred_edge)
2371 {
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;
2376 }
2377 }
2378 if (inserted_edge == 2)
2379 return false;
2380
2381 int edge_dominating = inserted_edge == 0 ? 1 : 0;
2382
2383 edge new_dominating_edge = NULL;
2384 for (inserted_edge = 0; inserted_edge < 2; inserted_edge++)
2385 {
2386 edge e = gimple_phi_arg_edge (new_phi, inserted_edge);
2387 if (e != new_non_dominating_edge)
2388 {
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;
2394 }
2395 }
2396 gcc_assert (new_dominating_edge);
2397 }
2398 else
2399 {
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++)
2407 {
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]))
2413 {
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;
2419 }
2420 }
2421
2422 gcc_assert (new_e[0] || new_e[1]);
2423
2424 /* Find the other edge by process of elimination. */
2425 if (not_found_bb_index != -1)
2426 {
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)
2432 {
2433 if (new_e[found_bb_index] == e)
2434 continue;
2435 new_e[not_found_bb_index] = e;
2436 }
2437 }
2438
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]));
2443 }
2444
2445 return true;
2446 }
2447
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. */
2452
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)
2457 {
2458 if (dump_file)
2459 fprintf (dump_file, "[codegen] copying cond phi args.\n");
2460 gcc_assert (2 == gimple_phi_num_args (phi));
2461
2462 basic_block new_bb = gimple_bb (new_phi);
2463 loop_p loop = gimple_bb (phi)->loop_father;
2464
2465 basic_block old_bb = gimple_bb (phi);
2466 edge old_bb_non_dominating_edge = NULL, old_bb_dominating_edge = NULL;
2467
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;
2475
2476 gcc_assert (!dominated_by_p (CDI_DOMINATORS, old_bb,
2477 old_bb_non_dominating_edge->src));
2478
2479 tree new_phi_args[2];
2480 tree old_phi_args[2];
2481
2482 for (unsigned i = 0; i < gimple_phi_num_args (phi); i++)
2483 {
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)
2488 {
2489 new_phi_args [i] = new_name;
2490 continue;
2491 }
2492
2493 /* If the phi-arg was a parameter. */
2494 if (vec_find (region->params, old_name) != -1)
2495 {
2496 new_phi_args [i] = old_name;
2497 if (dump_file)
2498 {
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");
2503 }
2504 continue;
2505 }
2506
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;
2512
2513 if (postpone)
2514 {
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))
2518 {
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;
2524
2525 gcc_assert (new_expr);
2526 if (dump_file)
2527 {
2528 fprintf (dump_file,
2529 "[codegen] scev analyzeable, new_expr: ");
2530 print_generic_expr (dump_file, new_expr, 0);
2531 fprintf (dump_file, "\n");
2532 }
2533 gsi_insert_earliest (stmts);
2534 new_phi_args [i] = new_name;
2535 continue;
2536 }
2537
2538 /* Postpone code gen for later for back-edges. */
2539 region->incomplete_phis.safe_push (std::make_pair (phi, new_phi));
2540
2541 if (dump_file)
2542 {
2543 fprintf (dump_file, "[codegen] postpone cond phi nodes: ");
2544 print_gimple_stmt (dump_file, new_phi, 0, 0);
2545 }
2546
2547 new_phi_args [i] = NULL_TREE;
2548 continue;
2549 }
2550 else
2551 /* Either we should add the arg to phi or, we should postpone. */
2552 return false;
2553 }
2554
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;
2559
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);
2564 }
2565
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. */
2569
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)
2574 {
2575
2576 gcc_assert (!bb_contains_loop_close_phi_nodes (bb));
2577
2578 if (dump_file)
2579 fprintf (dump_file, "[codegen] copying cond phi nodes in bb_%d.\n",
2580 new_bb->index);
2581
2582 /* Cond phi nodes should have exactly two arguments. */
2583 gcc_assert (2 == EDGE_COUNT (bb->preds));
2584
2585 for (gphi_iterator psi = gsi_start_phis (bb); !gsi_end_p (psi);
2586 gsi_next (&psi))
2587 {
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 ();
2595
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);
2600
2601 if (!copy_cond_phi_args (phi, new_phi, iv_map, true))
2602 return false;
2603
2604 update_stmt (new_phi);
2605 }
2606
2607 return true;
2608 }
2609
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. */
2613
2614 static bool
2615 should_copy_to_new_region (gimple *stmt, sese_info_p region)
2616 {
2617 /* Do not copy labels or conditions. */
2618 if (gimple_code (stmt) == GIMPLE_LABEL
2619 || gimple_code (stmt) == GIMPLE_COND)
2620 return false;
2621
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;
2630
2631 return true;
2632 }
2633
2634 /* Create new names for all the definitions created by COPY and add replacement
2635 mappings for each new name. */
2636
2637 void
2638 translate_isl_ast_to_gimple::set_rename_for_each_def (gimple *stmt)
2639 {
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)
2643 {
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);
2647 }
2648 }
2649
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. */
2653
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)
2658 {
2659 /* Iterator poining to the place where new statement (s) will be inserted. */
2660 gimple_stmt_iterator gsi_tgt = gsi_last_bb (new_bb);
2661
2662 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
2663 gsi_next (&gsi))
2664 {
2665 gimple *stmt = gsi_stmt (gsi);
2666 if (!should_copy_to_new_region (stmt, region))
2667 continue;
2668
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);
2673
2674 if (dump_file)
2675 {
2676 fprintf (dump_file, "[codegen] inserting statement: ");
2677 print_gimple_stmt (dump_file, copy, 0, 0);
2678 }
2679
2680 maybe_duplicate_eh_stmt (copy, stmt);
2681 gimple_duplicate_stmt_histograms (cfun, copy, cfun, stmt);
2682
2683 /* Crete new names for each def in the copied stmt. */
2684 set_rename_for_each_def (copy);
2685
2686 loop_p loop = bb->loop_father;
2687 if (rename_uses (copy, &gsi_tgt, bb, loop, iv_map))
2688 {
2689 fold_stmt_inplace (&gsi_tgt);
2690 gcc_assert (gsi_stmt (gsi_tgt) == copy);
2691 }
2692
2693 if (codegen_error_p ())
2694 return false;
2695
2696 update_stmt (copy);
2697 }
2698
2699 return true;
2700 }
2701
2702
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. */
2706
2707 edge
2708 translate_isl_ast_to_gimple::edge_for_new_close_phis (basic_block bb)
2709 {
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))
2718 {
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));
2722
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;
2730
2731 if (!new_loop)
2732 new_loop = (*bbs)[0]->loop_father;
2733 else if (new_loop != (*bbs)[0]->loop_father)
2734 return NULL;
2735 }
2736
2737 if (!new_loop)
2738 return NULL;
2739
2740 return single_exit (new_loop);
2741 }
2742
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. */
2747
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)
2752 {
2753 int num_phis = number_of_phi_nodes (bb);
2754
2755 if (region->copied_bb_map->get (bb))
2756 {
2757 /* FIXME: we should be able to handle phi nodes with args coming from
2758 outside the region. */
2759 if (num_phis)
2760 {
2761 codegen_error = true;
2762 return NULL;
2763 }
2764 }
2765
2766 basic_block new_bb = NULL;
2767 if (bb_contains_loop_close_phi_nodes (bb))
2768 {
2769 if (dump_file)
2770 fprintf (dump_file, "[codegen] bb_%d contains close phi nodes.\n",
2771 bb->index);
2772
2773 edge e = edge_for_new_close_phis (bb);
2774 if (!e)
2775 {
2776 codegen_error = true;
2777 return NULL;
2778 }
2779
2780 basic_block phi_bb = e->dest;
2781
2782 if (!bb_contains_loop_close_phi_nodes (phi_bb) || !single_succ_p (phi_bb))
2783 phi_bb = split_edge (e);
2784
2785 gcc_assert (single_pred_edge (phi_bb)->src->loop_father
2786 != single_pred_edge (phi_bb)->dest->loop_father);
2787
2788 if (!copy_loop_close_phi_nodes (bb, phi_bb))
2789 {
2790 codegen_error = true;
2791 return NULL;
2792 }
2793
2794 if (e == next_e)
2795 new_bb = phi_bb;
2796 else
2797 new_bb = split_edge (next_e);
2798 }
2799 else
2800 {
2801 new_bb = split_edge (next_e);
2802 if (num_phis > 0 && bb_contains_loop_phi_nodes (bb))
2803 {
2804 basic_block phi_bb = next_e->dest->loop_father->header;
2805
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))
2811 {
2812 codegen_error = true;
2813 return NULL;
2814 }
2815
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))
2820 {
2821 codegen_error = true;
2822 return NULL;
2823 }
2824 }
2825 else if (num_phis > 0)
2826 {
2827 if (dump_file)
2828 fprintf (dump_file, "[codegen] bb_%d contains cond phi nodes.\n",
2829 bb->index);
2830
2831 basic_block phi_bb = single_pred (new_bb);
2832 loop_p loop_father = new_bb->loop_father;
2833
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;
2837
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))
2841 {
2842 codegen_error = true;
2843 return NULL;
2844 }
2845 }
2846 }
2847
2848 if (dump_file)
2849 fprintf (dump_file, "[codegen] copying from bb_%d to bb_%d.\n",
2850 bb->index, new_bb->index);
2851
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
2856 {
2857 vec<basic_block> bbs;
2858 bbs.create (2);
2859 bbs.safe_push (new_bb);
2860 region->copied_bb_map->put (bb, bbs);
2861 }
2862
2863 if (!graphite_copy_stmts_from_block (bb, new_bb, iv_map))
2864 {
2865 codegen_error = true;
2866 return NULL;
2867 }
2868
2869 return single_succ_edge (new_bb);
2870 }
2871
2872 /* Patch the missing arguments of the phi nodes. */
2873
2874 void
2875 translate_isl_ast_to_gimple::translate_pending_phi_nodes ()
2876 {
2877 int i;
2878 phi_rename *rename;
2879 FOR_EACH_VEC_ELT (region->incomplete_phis, i, rename)
2880 {
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);
2885
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);
2889
2890 if (dump_file)
2891 {
2892 fprintf (dump_file, "[codegen] translating pending old-phi: ");
2893 print_gimple_stmt (dump_file, old_phi, 0, 0);
2894 }
2895
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);
2904
2905 if (dump_file)
2906 {
2907 fprintf (dump_file, "[codegen] to new-phi: ");
2908 print_gimple_stmt (dump_file, new_phi, 0, 0);
2909 }
2910 if (codegen_error)
2911 return;
2912 }
2913 }
2914
2915 /* Prints NODE to FILE. */
2916
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
2921 {
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);
2927 }
2928
2929 /* Add ISL's parameter identifiers and corresponding trees to ivs_params. */
2930
2931 void
2932 translate_isl_ast_to_gimple::add_parameters_to_ivs_params (scop_p scop,
2933 ivs_params &ip)
2934 {
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++)
2940 {
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];
2944 }
2945 }
2946
2947
2948 /* Generates a build, which specifies the constraints on the parameters. */
2949
2950 __isl_give isl_ast_build *
2951 translate_isl_ast_to_gimple::generate_isl_context (scop_p scop)
2952 {
2953 isl_set *context_isl = isl_set_params (isl_set_copy (scop->param_context));
2954 return isl_ast_build_from_context (context_isl);
2955 }
2956
2957 /* Get the maximal number of schedule dimensions in the scop SCOP. */
2958
2959 int
2960 translate_isl_ast_to_gimple::get_max_schedule_dimensions (scop_p scop)
2961 {
2962 int i;
2963 poly_bb_p pbb;
2964 int schedule_dims = 0;
2965
2966 FOR_EACH_VEC_ELT (scop->pbbs, i, pbb)
2967 {
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;
2971 }
2972
2973 return schedule_dims;
2974 }
2975
2976 /* Extend the schedule to NB_SCHEDULE_DIMS schedule dimensions.
2977
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). */
2982
2983 __isl_give isl_map *
2984 translate_isl_ast_to_gimple::extend_schedule (__isl_take isl_map *schedule,
2985 int nb_schedule_dims)
2986 {
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++)
2994 {
2995 schedule
2996 = isl_map_fix_val (schedule, isl_dim_out, i, isl_val_copy (zero));
2997 }
2998 isl_val_free (zero);
2999 return schedule;
3000 }
3001
3002 /* Generates a schedule, which specifies an order used to
3003 visit elements in a domain. */
3004
3005 __isl_give isl_union_map *
3006 translate_isl_ast_to_gimple::generate_isl_schedule (scop_p scop)
3007 {
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));
3013
3014 FOR_EACH_VEC_ELT (scop->pbbs, i, pbb)
3015 {
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;
3020
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));
3028 }
3029 return schedule_isl;
3030 }
3031
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)
3035 {
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;
3047 }
3048
3049 /* Set the separate option for all dimensions.
3050 This helps to reduce control overhead. */
3051
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)
3055 {
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);
3066 }
3067
3068 /* Generate isl AST from schedule of SCOP. Also, collects IVS_PARAMS in IP. */
3069
3070 __isl_give isl_ast_node *
3071 translate_isl_ast_to_gimple::scop_to_isl_ast (scop_p scop, ivs_params &ip)
3072 {
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);
3078
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)
3085 {
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);
3090 }
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;
3097 }
3098
3099 /* GIMPLE Loop Generator: generates loops from STMT in GIMPLE form for
3100 the given SCOP. Return true if code generation succeeded.
3101
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. */
3104
3105 bool
3106 graphite_regenerate_ast_isl (scop_p scop)
3107 {
3108 sese_info_p region = scop->scop_info;
3109 translate_isl_ast_to_gimple t (region);
3110
3111 ifsese if_region = NULL;
3112 isl_ast_node *root_node;
3113 ivs_params ip;
3114
3115 timevar_push (TV_GRAPHITE_CODE_GEN);
3116 root_node = t.scop_to_isl_ast (scop, ip);
3117
3118 if (dump_file && (dump_flags & TDF_DETAILS))
3119 {
3120 fprintf (dump_file, "ISL AST generated by ISL: \n");
3121 t.print_isl_ast_node (dump_file, root_node, scop->isl_context);
3122 }
3123
3124 recompute_all_dominators ();
3125 graphite_verify ();
3126
3127 if_region = move_sese_in_condition (region);
3128 region->if_region = if_region;
3129 recompute_all_dominators ();
3130
3131 loop_p context_loop = region->region.entry->src->loop_father;
3132
3133 edge e = single_succ_edge (if_region->true_region->region.entry->dest);
3134 basic_block bb = split_edge (e);
3135
3136 /* Update the true_region exit edge. */
3137 region->if_region->true_region->region.exit = single_succ_edge (bb);
3138
3139 t.translate_isl_ast (context_loop, root_node, e, ip);
3140 if (t.codegen_error_p ())
3141 {
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);
3146 }
3147 else
3148 {
3149 t.translate_pending_phi_nodes ();
3150 if (!t.codegen_error_p ())
3151 {
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);
3158
3159
3160 graphite_verify ();
3161 scev_reset ();
3162 recompute_all_dominators ();
3163 graphite_verify ();
3164 }
3165 else
3166 {
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);
3171 }
3172 }
3173
3174 free (if_region->true_region);
3175 free (if_region->region);
3176 free (if_region);
3177
3178 ivs_params_clear (ip);
3179 isl_ast_node_free (root_node);
3180 timevar_pop (TV_GRAPHITE_CODE_GEN);
3181
3182 if (dump_file && (dump_flags & TDF_DETAILS))
3183 {
3184 loop_p loop;
3185 int num_no_dependency = 0;
3186
3187 FOR_EACH_LOOP (loop, 0)
3188 if (loop->can_be_parallel)
3189 num_no_dependency++;
3190
3191 fprintf (dump_file, "%d loops carried no dependency.\n",
3192 num_no_dependency);
3193 }
3194
3195 return !t.codegen_error_p ();
3196 }
3197
3198 #endif /* HAVE_isl */