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