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