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