re PR tree-optimization/82525 ([GRAPHITE] codegen error for modulo operations we...
[gcc.git] / gcc / graphite-isl-ast-to-gimple.c
1 /* Translation of isl AST to Gimple.
2 Copyright (C) 2014-2017 Free Software Foundation, Inc.
3 Contributed by Roman Gareev <gareevroman@gmail.com>.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #define USES_ISL
22
23 #include "config.h"
24
25 #ifdef HAVE_isl
26
27 #define INCLUDE_MAP
28 #include "system.h"
29 #include "coretypes.h"
30 #include "backend.h"
31 #include "cfghooks.h"
32 #include "tree.h"
33 #include "gimple.h"
34 #include "ssa.h"
35 #include "params.h"
36 #include "fold-const.h"
37 #include "gimple-fold.h"
38 #include "gimple-iterator.h"
39 #include "gimplify.h"
40 #include "gimplify-me.h"
41 #include "tree-eh.h"
42 #include "tree-ssa-loop.h"
43 #include "tree-ssa-operands.h"
44 #include "tree-ssa-propagate.h"
45 #include "tree-pass.h"
46 #include "cfgloop.h"
47 #include "tree-data-ref.h"
48 #include "tree-ssa-loop-manip.h"
49 #include "tree-scalar-evolution.h"
50 #include "gimple-ssa.h"
51 #include "tree-phinodes.h"
52 #include "tree-into-ssa.h"
53 #include "ssa-iterators.h"
54 #include "tree-cfg.h"
55 #include "gimple-pretty-print.h"
56 #include "cfganal.h"
57 #include "value-prof.h"
58 #include "tree-ssa.h"
59 #include "graphite.h"
60
61 /* We always try to use signed 128 bit types, but fall back to smaller types
62 in case a platform does not provide types of these sizes. In the future we
63 should use isl to derive the optimal type for each subexpression. */
64
65 static int max_mode_int_precision =
66 GET_MODE_PRECISION (int_mode_for_size (MAX_FIXED_MODE_SIZE, 0).require ());
67 static int graphite_expression_type_precision = 128 <= max_mode_int_precision ?
68 128 : max_mode_int_precision;
69
70 struct ast_build_info
71 {
72 ast_build_info()
73 : is_parallelizable(false)
74 { }
75 bool is_parallelizable;
76 };
77
78 /* IVS_PARAMS maps isl's scattering and parameter identifiers
79 to corresponding trees. */
80
81 typedef std::map<isl_id *, tree> ivs_params;
82
83 /* Free all memory allocated for isl's identifiers. */
84
85 static void ivs_params_clear (ivs_params &ip)
86 {
87 std::map<isl_id *, tree>::iterator it;
88 for (it = ip.begin ();
89 it != ip.end (); it++)
90 {
91 isl_id_free (it->first);
92 }
93 }
94
95 /* Set the "separate" option for the schedule node. */
96
97 static isl_schedule_node *
98 set_separate_option (__isl_take isl_schedule_node *node, void *user)
99 {
100 if (user)
101 return node;
102
103 if (isl_schedule_node_get_type (node) != isl_schedule_node_band)
104 return node;
105
106 /* Set the "separate" option unless it is set earlier to another option. */
107 if (isl_schedule_node_band_member_get_ast_loop_type (node, 0)
108 == isl_ast_loop_default)
109 return isl_schedule_node_band_member_set_ast_loop_type
110 (node, 0, isl_ast_loop_separate);
111
112 return node;
113 }
114
115 /* Print SCHEDULE under an AST form on file F. */
116
117 void
118 print_schedule_ast (FILE *f, __isl_keep isl_schedule *schedule, scop_p scop)
119 {
120 isl_set *set = isl_set_params (isl_set_copy (scop->param_context));
121 isl_ast_build *context = isl_ast_build_from_context (set);
122 isl_ast_node *ast
123 = isl_ast_build_node_from_schedule (context, isl_schedule_copy (schedule));
124 isl_ast_build_free (context);
125 print_isl_ast (f, ast);
126 isl_ast_node_free (ast);
127 }
128
129 DEBUG_FUNCTION void
130 debug_schedule_ast (__isl_keep isl_schedule *s, scop_p scop)
131 {
132 print_schedule_ast (stderr, s, scop);
133 }
134
135 enum phi_node_kind
136 {
137 unknown_phi,
138 loop_phi,
139 close_phi,
140 cond_phi
141 };
142
143 class translate_isl_ast_to_gimple
144 {
145 public:
146 translate_isl_ast_to_gimple (sese_info_p r)
147 : region (r), codegen_error (false) { }
148 edge translate_isl_ast (loop_p context_loop, __isl_keep isl_ast_node *node,
149 edge next_e, ivs_params &ip);
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 edge translate_isl_ast_for_loop (loop_p context_loop,
154 __isl_keep isl_ast_node *node_for,
155 edge next_e,
156 tree type, tree lb, tree ub,
157 ivs_params &ip);
158 edge translate_isl_ast_node_if (loop_p context_loop,
159 __isl_keep isl_ast_node *node,
160 edge next_e, ivs_params &ip);
161 edge translate_isl_ast_node_user (__isl_keep isl_ast_node *node,
162 edge next_e, ivs_params &ip);
163 edge translate_isl_ast_node_block (loop_p context_loop,
164 __isl_keep isl_ast_node *node,
165 edge next_e, ivs_params &ip);
166 tree unary_op_to_tree (tree type, __isl_take isl_ast_expr *expr,
167 ivs_params &ip);
168 tree binary_op_to_tree (tree type, __isl_take isl_ast_expr *expr,
169 ivs_params &ip);
170 tree ternary_op_to_tree (tree type, __isl_take isl_ast_expr *expr,
171 ivs_params &ip);
172 tree nary_op_to_tree (tree type, __isl_take isl_ast_expr *expr,
173 ivs_params &ip);
174 tree gcc_expression_from_isl_expression (tree type,
175 __isl_take isl_ast_expr *,
176 ivs_params &ip);
177 tree gcc_expression_from_isl_ast_expr_id (tree type,
178 __isl_keep isl_ast_expr *expr_id,
179 ivs_params &ip);
180 widest_int widest_int_from_isl_expr_int (__isl_keep isl_ast_expr *expr);
181 tree gcc_expression_from_isl_expr_int (tree type,
182 __isl_take isl_ast_expr *expr);
183 tree gcc_expression_from_isl_expr_op (tree type,
184 __isl_take isl_ast_expr *expr,
185 ivs_params &ip);
186 struct loop *graphite_create_new_loop (edge entry_edge,
187 __isl_keep isl_ast_node *node_for,
188 loop_p outer, tree type,
189 tree lb, tree ub, ivs_params &ip);
190 edge graphite_create_new_guard (edge entry_edge,
191 __isl_take isl_ast_expr *if_cond,
192 ivs_params &ip);
193 void build_iv_mapping (vec<tree> iv_map, gimple_poly_bb_p gbb,
194 __isl_keep isl_ast_expr *user_expr, ivs_params &ip,
195 sese_l &region);
196 void add_parameters_to_ivs_params (scop_p scop, ivs_params &ip);
197 __isl_give isl_ast_build *generate_isl_context (scop_p scop);
198
199 __isl_give isl_ast_node * scop_to_isl_ast (scop_p scop);
200
201 tree get_rename_from_scev (tree old_name, gimple_seq *stmts, loop_p loop,
202 basic_block new_bb, basic_block old_bb,
203 vec<tree> iv_map);
204 bool graphite_copy_stmts_from_block (basic_block bb, basic_block new_bb,
205 vec<tree> iv_map);
206 edge copy_bb_and_scalar_dependences (basic_block bb, edge next_e,
207 vec<tree> iv_map);
208 void set_rename (tree old_name, tree expr);
209 void set_rename_for_each_def (gimple *stmt);
210 void gsi_insert_earliest (gimple_seq seq);
211 bool codegen_error_p () const { return codegen_error; }
212
213 void set_codegen_error ()
214 {
215 codegen_error = true;
216 gcc_assert (! flag_checking
217 || PARAM_VALUE (PARAM_GRAPHITE_ALLOW_CODEGEN_ERRORS));
218 }
219
220 bool is_constant (tree op) const
221 {
222 return TREE_CODE (op) == INTEGER_CST
223 || TREE_CODE (op) == REAL_CST
224 || TREE_CODE (op) == COMPLEX_CST
225 || TREE_CODE (op) == VECTOR_CST;
226 }
227
228 private:
229 /* The region to be translated. */
230 sese_info_p region;
231
232 /* This flag is set when an error occurred during the translation of isl AST
233 to Gimple. */
234 bool codegen_error;
235
236 /* A vector of all the edges at if_condition merge points. */
237 auto_vec<edge, 2> merge_points;
238 };
239
240 /* Return the tree variable that corresponds to the given isl ast identifier
241 expression (an isl_ast_expr of type isl_ast_expr_id).
242
243 FIXME: We should replace blind conversion of id's type with derivation
244 of the optimal type when we get the corresponding isl support. Blindly
245 converting type sizes may be problematic when we switch to smaller
246 types. */
247
248 tree translate_isl_ast_to_gimple::
249 gcc_expression_from_isl_ast_expr_id (tree type,
250 __isl_take isl_ast_expr *expr_id,
251 ivs_params &ip)
252 {
253 gcc_assert (isl_ast_expr_get_type (expr_id) == isl_ast_expr_id);
254 isl_id *tmp_isl_id = isl_ast_expr_get_id (expr_id);
255 std::map<isl_id *, tree>::iterator res;
256 res = ip.find (tmp_isl_id);
257 isl_id_free (tmp_isl_id);
258 gcc_assert (res != ip.end () &&
259 "Could not map isl_id to tree expression");
260 isl_ast_expr_free (expr_id);
261 tree t = res->second;
262 tree *val = region->parameter_rename_map->get(t);
263
264 if (!val)
265 val = &t;
266 return fold_convert (type, *val);
267 }
268
269 /* Converts an isl_ast_expr_int expression E to a widest_int.
270 Raises a code generation error when the constant doesn't fit. */
271
272 widest_int translate_isl_ast_to_gimple::
273 widest_int_from_isl_expr_int (__isl_keep isl_ast_expr *expr)
274 {
275 gcc_assert (isl_ast_expr_get_type (expr) == isl_ast_expr_int);
276 isl_val *val = isl_ast_expr_get_val (expr);
277 size_t n = isl_val_n_abs_num_chunks (val, sizeof (HOST_WIDE_INT));
278 HOST_WIDE_INT *chunks = XALLOCAVEC (HOST_WIDE_INT, n);
279 if (n > WIDE_INT_MAX_ELTS
280 || isl_val_get_abs_num_chunks (val, sizeof (HOST_WIDE_INT), chunks) == -1)
281 {
282 isl_val_free (val);
283 set_codegen_error ();
284 return 0;
285 }
286 widest_int wi = widest_int::from_array (chunks, n, true);
287 if (isl_val_is_neg (val))
288 wi = -wi;
289 isl_val_free (val);
290 return wi;
291 }
292
293 /* Converts an isl_ast_expr_int expression E to a GCC expression tree of
294 type TYPE. Raises a code generation error when the constant doesn't fit. */
295
296 tree translate_isl_ast_to_gimple::
297 gcc_expression_from_isl_expr_int (tree type, __isl_take isl_ast_expr *expr)
298 {
299 widest_int wi = widest_int_from_isl_expr_int (expr);
300 isl_ast_expr_free (expr);
301 if (codegen_error_p ())
302 return NULL_TREE;
303 if (wi::min_precision (wi, TYPE_SIGN (type)) > TYPE_PRECISION (type))
304 {
305 set_codegen_error ();
306 return NULL_TREE;
307 }
308 return wide_int_to_tree (type, wi);
309 }
310
311 /* Converts a binary isl_ast_expr_op expression E to a GCC expression tree of
312 type TYPE. */
313
314 tree translate_isl_ast_to_gimple::
315 binary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
316 {
317 enum isl_ast_op_type expr_type = isl_ast_expr_get_op_type (expr);
318 isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0);
319 tree tree_lhs_expr = gcc_expression_from_isl_expression (type, arg_expr, ip);
320 arg_expr = isl_ast_expr_get_op_arg (expr, 1);
321 isl_ast_expr_free (expr);
322
323 /* From our constraint generation we may get modulo operations that
324 we cannot represent explicitely but that are no-ops for TYPE.
325 Elide those. */
326 if (expr_type == isl_ast_op_pdiv_r
327 && isl_ast_expr_get_type (arg_expr) == isl_ast_expr_int
328 && (wi::exact_log2 (widest_int_from_isl_expr_int (arg_expr))
329 >= TYPE_PRECISION (type)))
330 {
331 isl_ast_expr_free (arg_expr);
332 return tree_lhs_expr;
333 }
334
335 tree tree_rhs_expr = gcc_expression_from_isl_expression (type, arg_expr, ip);
336 if (codegen_error_p ())
337 return NULL_TREE;
338
339 switch (expr_type)
340 {
341 case isl_ast_op_add:
342 return fold_build2 (PLUS_EXPR, type, tree_lhs_expr, tree_rhs_expr);
343
344 case isl_ast_op_sub:
345 return fold_build2 (MINUS_EXPR, type, tree_lhs_expr, tree_rhs_expr);
346
347 case isl_ast_op_mul:
348 return fold_build2 (MULT_EXPR, type, tree_lhs_expr, tree_rhs_expr);
349
350 case isl_ast_op_div:
351 return fold_build2 (EXACT_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr);
352
353 case isl_ast_op_pdiv_q:
354 return fold_build2 (TRUNC_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr);
355
356 case isl_ast_op_zdiv_r:
357 case isl_ast_op_pdiv_r:
358 return fold_build2 (TRUNC_MOD_EXPR, type, tree_lhs_expr, tree_rhs_expr);
359
360 case isl_ast_op_fdiv_q:
361 return fold_build2 (FLOOR_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr);
362
363 case isl_ast_op_and:
364 return fold_build2 (TRUTH_ANDIF_EXPR, type,
365 tree_lhs_expr, tree_rhs_expr);
366
367 case isl_ast_op_or:
368 return fold_build2 (TRUTH_ORIF_EXPR, type, tree_lhs_expr, tree_rhs_expr);
369
370 case isl_ast_op_eq:
371 return fold_build2 (EQ_EXPR, type, tree_lhs_expr, tree_rhs_expr);
372
373 case isl_ast_op_le:
374 return fold_build2 (LE_EXPR, type, tree_lhs_expr, tree_rhs_expr);
375
376 case isl_ast_op_lt:
377 return fold_build2 (LT_EXPR, type, tree_lhs_expr, tree_rhs_expr);
378
379 case isl_ast_op_ge:
380 return fold_build2 (GE_EXPR, type, tree_lhs_expr, tree_rhs_expr);
381
382 case isl_ast_op_gt:
383 return fold_build2 (GT_EXPR, type, tree_lhs_expr, tree_rhs_expr);
384
385 default:
386 gcc_unreachable ();
387 }
388 }
389
390 /* Converts a ternary isl_ast_expr_op expression E to a GCC expression tree of
391 type TYPE. */
392
393 tree translate_isl_ast_to_gimple::
394 ternary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
395 {
396 enum isl_ast_op_type t = isl_ast_expr_get_op_type (expr);
397 gcc_assert (t == isl_ast_op_cond || t == isl_ast_op_select);
398 isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0);
399 tree a = gcc_expression_from_isl_expression (type, arg_expr, ip);
400 arg_expr = isl_ast_expr_get_op_arg (expr, 1);
401 tree b = gcc_expression_from_isl_expression (type, arg_expr, ip);
402 arg_expr = isl_ast_expr_get_op_arg (expr, 2);
403 tree c = gcc_expression_from_isl_expression (type, arg_expr, ip);
404 isl_ast_expr_free (expr);
405
406 if (codegen_error_p ())
407 return NULL_TREE;
408
409 return fold_build3 (COND_EXPR, type, a, b, c);
410 }
411
412 /* Converts a unary isl_ast_expr_op expression E to a GCC expression tree of
413 type TYPE. */
414
415 tree translate_isl_ast_to_gimple::
416 unary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
417 {
418 gcc_assert (isl_ast_expr_get_op_type (expr) == isl_ast_op_minus);
419 isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0);
420 tree tree_expr = gcc_expression_from_isl_expression (type, arg_expr, ip);
421 isl_ast_expr_free (expr);
422 return codegen_error_p () ? NULL_TREE
423 : fold_build1 (NEGATE_EXPR, type, tree_expr);
424 }
425
426 /* Converts an isl_ast_expr_op expression E with unknown number of arguments
427 to a GCC expression tree of type TYPE. */
428
429 tree translate_isl_ast_to_gimple::
430 nary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
431 {
432 enum tree_code op_code;
433 switch (isl_ast_expr_get_op_type (expr))
434 {
435 case isl_ast_op_max:
436 op_code = MAX_EXPR;
437 break;
438
439 case isl_ast_op_min:
440 op_code = MIN_EXPR;
441 break;
442
443 default:
444 gcc_unreachable ();
445 }
446 isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0);
447 tree res = gcc_expression_from_isl_expression (type, arg_expr, ip);
448
449 if (codegen_error_p ())
450 {
451 isl_ast_expr_free (expr);
452 return NULL_TREE;
453 }
454
455 int i;
456 for (i = 1; i < isl_ast_expr_get_op_n_arg (expr); i++)
457 {
458 arg_expr = isl_ast_expr_get_op_arg (expr, i);
459 tree t = gcc_expression_from_isl_expression (type, arg_expr, ip);
460
461 if (codegen_error_p ())
462 {
463 isl_ast_expr_free (expr);
464 return NULL_TREE;
465 }
466
467 res = fold_build2 (op_code, type, res, t);
468 }
469 isl_ast_expr_free (expr);
470 return res;
471 }
472
473 /* Converts an isl_ast_expr_op expression E to a GCC expression tree of
474 type TYPE. */
475
476 tree translate_isl_ast_to_gimple::
477 gcc_expression_from_isl_expr_op (tree type, __isl_take isl_ast_expr *expr,
478 ivs_params &ip)
479 {
480 if (codegen_error_p ())
481 {
482 isl_ast_expr_free (expr);
483 return NULL_TREE;
484 }
485
486 gcc_assert (isl_ast_expr_get_type (expr) == isl_ast_expr_op);
487 switch (isl_ast_expr_get_op_type (expr))
488 {
489 /* These isl ast expressions are not supported yet. */
490 case isl_ast_op_error:
491 case isl_ast_op_call:
492 case isl_ast_op_and_then:
493 case isl_ast_op_or_else:
494 gcc_unreachable ();
495
496 case isl_ast_op_max:
497 case isl_ast_op_min:
498 return nary_op_to_tree (type, expr, ip);
499
500 case isl_ast_op_add:
501 case isl_ast_op_sub:
502 case isl_ast_op_mul:
503 case isl_ast_op_div:
504 case isl_ast_op_pdiv_q:
505 case isl_ast_op_pdiv_r:
506 case isl_ast_op_fdiv_q:
507 case isl_ast_op_zdiv_r:
508 case isl_ast_op_and:
509 case isl_ast_op_or:
510 case isl_ast_op_eq:
511 case isl_ast_op_le:
512 case isl_ast_op_lt:
513 case isl_ast_op_ge:
514 case isl_ast_op_gt:
515 return binary_op_to_tree (type, expr, ip);
516
517 case isl_ast_op_minus:
518 return unary_op_to_tree (type, expr, ip);
519
520 case isl_ast_op_cond:
521 case isl_ast_op_select:
522 return ternary_op_to_tree (type, expr, ip);
523
524 default:
525 gcc_unreachable ();
526 }
527
528 return NULL_TREE;
529 }
530
531 /* Converts an isl AST expression E back to a GCC expression tree of
532 type TYPE. */
533
534 tree translate_isl_ast_to_gimple::
535 gcc_expression_from_isl_expression (tree type, __isl_take isl_ast_expr *expr,
536 ivs_params &ip)
537 {
538 if (codegen_error_p ())
539 {
540 isl_ast_expr_free (expr);
541 return NULL_TREE;
542 }
543
544 switch (isl_ast_expr_get_type (expr))
545 {
546 case isl_ast_expr_id:
547 return gcc_expression_from_isl_ast_expr_id (type, expr, ip);
548
549 case isl_ast_expr_int:
550 return gcc_expression_from_isl_expr_int (type, expr);
551
552 case isl_ast_expr_op:
553 return gcc_expression_from_isl_expr_op (type, expr, ip);
554
555 default:
556 gcc_unreachable ();
557 }
558
559 return NULL_TREE;
560 }
561
562 /* Creates a new LOOP corresponding to isl_ast_node_for. Inserts an
563 induction variable for the new LOOP. New LOOP is attached to CFG
564 starting at ENTRY_EDGE. LOOP is inserted into the loop tree and
565 becomes the child loop of the OUTER_LOOP. NEWIVS_INDEX binds
566 isl's scattering name to the induction variable created for the
567 loop of STMT. The new induction variable is inserted in the NEWIVS
568 vector and is of type TYPE. */
569
570 struct loop *translate_isl_ast_to_gimple::
571 graphite_create_new_loop (edge entry_edge, __isl_keep isl_ast_node *node_for,
572 loop_p outer, tree type, tree lb, tree ub,
573 ivs_params &ip)
574 {
575 isl_ast_expr *for_inc = isl_ast_node_for_get_inc (node_for);
576 tree stride = gcc_expression_from_isl_expression (type, for_inc, ip);
577
578 /* To fail code generation, we generate wrong code until we discard it. */
579 if (codegen_error_p ())
580 stride = integer_zero_node;
581
582 tree ivvar = create_tmp_var (type, "graphite_IV");
583 tree iv, iv_after_increment;
584 loop_p loop = create_empty_loop_on_edge
585 (entry_edge, lb, stride, ub, ivvar, &iv, &iv_after_increment,
586 outer ? outer : entry_edge->src->loop_father);
587
588 isl_ast_expr *for_iterator = isl_ast_node_for_get_iterator (node_for);
589 isl_id *id = isl_ast_expr_get_id (for_iterator);
590 std::map<isl_id *, tree>::iterator res;
591 res = ip.find (id);
592 if (ip.count (id))
593 isl_id_free (res->first);
594 ip[id] = iv;
595 isl_ast_expr_free (for_iterator);
596 return loop;
597 }
598
599 /* Create the loop for a isl_ast_node_for.
600
601 - NEXT_E is the edge where new generated code should be attached. */
602
603 edge translate_isl_ast_to_gimple::
604 translate_isl_ast_for_loop (loop_p context_loop,
605 __isl_keep isl_ast_node *node_for, edge next_e,
606 tree type, tree lb, tree ub,
607 ivs_params &ip)
608 {
609 gcc_assert (isl_ast_node_get_type (node_for) == isl_ast_node_for);
610 struct loop *loop = graphite_create_new_loop (next_e, node_for, context_loop,
611 type, lb, ub, ip);
612 edge last_e = single_exit (loop);
613 edge to_body = single_succ_edge (loop->header);
614 basic_block after = to_body->dest;
615
616 /* Translate the body of the loop. */
617 isl_ast_node *for_body = isl_ast_node_for_get_body (node_for);
618 next_e = translate_isl_ast (loop, for_body, to_body, ip);
619 isl_ast_node_free (for_body);
620
621 /* Early return if we failed to translate loop body. */
622 if (!next_e || codegen_error_p ())
623 return NULL;
624
625 if (next_e->dest != after)
626 redirect_edge_succ_nodup (next_e, after);
627 set_immediate_dominator (CDI_DOMINATORS, next_e->dest, next_e->src);
628
629 if (flag_loop_parallelize_all)
630 {
631 isl_id *id = isl_ast_node_get_annotation (node_for);
632 gcc_assert (id);
633 ast_build_info *for_info = (ast_build_info *) isl_id_get_user (id);
634 loop->can_be_parallel = for_info->is_parallelizable;
635 free (for_info);
636 isl_id_free (id);
637 }
638
639 return last_e;
640 }
641
642 /* We use this function to get the upper bound because of the form,
643 which is used by isl to represent loops:
644
645 for (iterator = init; cond; iterator += inc)
646
647 {
648
649 ...
650
651 }
652
653 The loop condition is an arbitrary expression, which contains the
654 current loop iterator.
655
656 (e.g. iterator + 3 < B && C > iterator + A)
657
658 We have to know the upper bound of the iterator to generate a loop
659 in Gimple form. It can be obtained from the special representation
660 of the loop condition, which is generated by isl,
661 if the ast_build_atomic_upper_bound option is set. In this case,
662 isl generates a loop condition that consists of the current loop
663 iterator, + an operator (< or <=) and an expression not involving
664 the iterator, which is processed and returned by this function.
665
666 (e.g iterator <= upper-bound-expression-without-iterator) */
667
668 static __isl_give isl_ast_expr *
669 get_upper_bound (__isl_keep isl_ast_node *node_for)
670 {
671 gcc_assert (isl_ast_node_get_type (node_for) == isl_ast_node_for);
672 isl_ast_expr *for_cond = isl_ast_node_for_get_cond (node_for);
673 gcc_assert (isl_ast_expr_get_type (for_cond) == isl_ast_expr_op);
674 isl_ast_expr *res;
675 switch (isl_ast_expr_get_op_type (for_cond))
676 {
677 case isl_ast_op_le:
678 res = isl_ast_expr_get_op_arg (for_cond, 1);
679 break;
680
681 case isl_ast_op_lt:
682 {
683 /* (iterator < ub) => (iterator <= ub - 1). */
684 isl_val *one =
685 isl_val_int_from_si (isl_ast_expr_get_ctx (for_cond), 1);
686 isl_ast_expr *ub = isl_ast_expr_get_op_arg (for_cond, 1);
687 res = isl_ast_expr_sub (ub, isl_ast_expr_from_val (one));
688 break;
689 }
690
691 default:
692 gcc_unreachable ();
693 }
694 isl_ast_expr_free (for_cond);
695 return res;
696 }
697
698 /* Translates an isl_ast_node_for to Gimple. */
699
700 edge translate_isl_ast_to_gimple::
701 translate_isl_ast_node_for (loop_p context_loop, __isl_keep isl_ast_node *node,
702 edge next_e, ivs_params &ip)
703 {
704 gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_for);
705 tree type
706 = build_nonstandard_integer_type (graphite_expression_type_precision, 0);
707
708 isl_ast_expr *for_init = isl_ast_node_for_get_init (node);
709 tree lb = gcc_expression_from_isl_expression (type, for_init, ip);
710 /* To fail code generation, we generate wrong code until we discard it. */
711 if (codegen_error_p ())
712 lb = integer_zero_node;
713
714 isl_ast_expr *upper_bound = get_upper_bound (node);
715 tree ub = gcc_expression_from_isl_expression (type, upper_bound, ip);
716 /* To fail code generation, we generate wrong code until we discard it. */
717 if (codegen_error_p ())
718 ub = integer_zero_node;
719
720 edge last_e = single_succ_edge (split_edge (next_e));
721 translate_isl_ast_for_loop (context_loop, node, next_e,
722 type, lb, ub, ip);
723 return last_e;
724 }
725
726 /* Inserts in iv_map a tuple (OLD_LOOP->num, NEW_NAME) for the induction
727 variables of the loops around GBB in SESE.
728
729 FIXME: Instead of using a vec<tree> that maps each loop id to a possible
730 chrec, we could consider using a map<int, tree> that maps loop ids to the
731 corresponding tree expressions. */
732
733 void translate_isl_ast_to_gimple::
734 build_iv_mapping (vec<tree> iv_map, gimple_poly_bb_p gbb,
735 __isl_keep isl_ast_expr *user_expr, ivs_params &ip,
736 sese_l &region)
737 {
738 gcc_assert (isl_ast_expr_get_type (user_expr) == isl_ast_expr_op &&
739 isl_ast_expr_get_op_type (user_expr) == isl_ast_op_call);
740 int i;
741 isl_ast_expr *arg_expr;
742 for (i = 1; i < isl_ast_expr_get_op_n_arg (user_expr); i++)
743 {
744 arg_expr = isl_ast_expr_get_op_arg (user_expr, i);
745 tree type =
746 build_nonstandard_integer_type (graphite_expression_type_precision, 0);
747 tree t = gcc_expression_from_isl_expression (type, arg_expr, ip);
748
749 /* To fail code generation, we generate wrong code until we discard it. */
750 if (codegen_error_p ())
751 t = integer_zero_node;
752
753 loop_p old_loop = gbb_loop_at_index (gbb, region, i - 2);
754 /* Record sth only for real loops. */
755 if (loop_in_sese_p (old_loop, region))
756 iv_map[old_loop->num] = t;
757 }
758 }
759
760 /* Translates an isl_ast_node_user to Gimple.
761
762 FIXME: We should remove iv_map.create (loop->num + 1), if it is possible. */
763
764 edge translate_isl_ast_to_gimple::
765 translate_isl_ast_node_user (__isl_keep isl_ast_node *node,
766 edge next_e, ivs_params &ip)
767 {
768 gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_user);
769
770 isl_ast_expr *user_expr = isl_ast_node_user_get_expr (node);
771 isl_ast_expr *name_expr = isl_ast_expr_get_op_arg (user_expr, 0);
772 gcc_assert (isl_ast_expr_get_type (name_expr) == isl_ast_expr_id);
773
774 isl_id *name_id = isl_ast_expr_get_id (name_expr);
775 poly_bb_p pbb = (poly_bb_p) isl_id_get_user (name_id);
776 gcc_assert (pbb);
777
778 gimple_poly_bb_p gbb = PBB_BLACK_BOX (pbb);
779
780 isl_ast_expr_free (name_expr);
781 isl_id_free (name_id);
782
783 gcc_assert (GBB_BB (gbb) != ENTRY_BLOCK_PTR_FOR_FN (cfun) &&
784 "The entry block should not even appear within a scop");
785
786 const int nb_loops = number_of_loops (cfun);
787 vec<tree> iv_map;
788 iv_map.create (nb_loops);
789 iv_map.safe_grow_cleared (nb_loops);
790
791 build_iv_mapping (iv_map, gbb, user_expr, ip, pbb->scop->scop_info->region);
792 isl_ast_expr_free (user_expr);
793
794 basic_block old_bb = GBB_BB (gbb);
795 if (dump_file)
796 {
797 fprintf (dump_file,
798 "[codegen] copying from bb_%d on edge (bb_%d, bb_%d)\n",
799 old_bb->index, next_e->src->index, next_e->dest->index);
800 print_loops_bb (dump_file, GBB_BB (gbb), 0, 3);
801
802 }
803
804 next_e = copy_bb_and_scalar_dependences (old_bb, next_e, iv_map);
805
806 iv_map.release ();
807
808 if (codegen_error_p ())
809 return NULL;
810
811 if (dump_file)
812 {
813 fprintf (dump_file, "[codegen] (after copy) new basic block\n");
814 print_loops_bb (dump_file, next_e->src, 0, 3);
815 }
816
817 return next_e;
818 }
819
820 /* Translates an isl_ast_node_block to Gimple. */
821
822 edge translate_isl_ast_to_gimple::
823 translate_isl_ast_node_block (loop_p context_loop,
824 __isl_keep isl_ast_node *node,
825 edge next_e, ivs_params &ip)
826 {
827 gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_block);
828 isl_ast_node_list *node_list = isl_ast_node_block_get_children (node);
829 int i;
830 for (i = 0; i < isl_ast_node_list_n_ast_node (node_list); i++)
831 {
832 isl_ast_node *tmp_node = isl_ast_node_list_get_ast_node (node_list, i);
833 next_e = translate_isl_ast (context_loop, tmp_node, next_e, ip);
834 isl_ast_node_free (tmp_node);
835 }
836 isl_ast_node_list_free (node_list);
837 return next_e;
838 }
839
840 /* Creates a new if region corresponding to isl's cond. */
841
842 edge translate_isl_ast_to_gimple::
843 graphite_create_new_guard (edge entry_edge, __isl_take isl_ast_expr *if_cond,
844 ivs_params &ip)
845 {
846 tree type =
847 build_nonstandard_integer_type (graphite_expression_type_precision, 0);
848 tree cond_expr = gcc_expression_from_isl_expression (type, if_cond, ip);
849
850 /* To fail code generation, we generate wrong code until we discard it. */
851 if (codegen_error_p ())
852 cond_expr = integer_zero_node;
853
854 edge exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr);
855 return exit_edge;
856 }
857
858 /* Translates an isl_ast_node_if to Gimple. */
859
860 edge translate_isl_ast_to_gimple::
861 translate_isl_ast_node_if (loop_p context_loop,
862 __isl_keep isl_ast_node *node,
863 edge next_e, ivs_params &ip)
864 {
865 gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_if);
866 isl_ast_expr *if_cond = isl_ast_node_if_get_cond (node);
867 edge last_e = graphite_create_new_guard (next_e, if_cond, ip);
868 edge true_e = get_true_edge_from_guard_bb (next_e->dest);
869 merge_points.safe_push (last_e);
870
871 isl_ast_node *then_node = isl_ast_node_if_get_then (node);
872 translate_isl_ast (context_loop, then_node, true_e, ip);
873 isl_ast_node_free (then_node);
874
875 edge false_e = get_false_edge_from_guard_bb (next_e->dest);
876 isl_ast_node *else_node = isl_ast_node_if_get_else (node);
877 if (isl_ast_node_get_type (else_node) != isl_ast_node_error)
878 translate_isl_ast (context_loop, else_node, false_e, ip);
879
880 isl_ast_node_free (else_node);
881 return last_e;
882 }
883
884 /* Translates an isl AST node NODE to GCC representation in the
885 context of a SESE. */
886
887 edge translate_isl_ast_to_gimple::
888 translate_isl_ast (loop_p context_loop, __isl_keep isl_ast_node *node,
889 edge next_e, ivs_params &ip)
890 {
891 if (codegen_error_p ())
892 return NULL;
893
894 switch (isl_ast_node_get_type (node))
895 {
896 case isl_ast_node_error:
897 gcc_unreachable ();
898
899 case isl_ast_node_for:
900 return translate_isl_ast_node_for (context_loop, node,
901 next_e, ip);
902
903 case isl_ast_node_if:
904 return translate_isl_ast_node_if (context_loop, node,
905 next_e, ip);
906
907 case isl_ast_node_user:
908 return translate_isl_ast_node_user (node, next_e, ip);
909
910 case isl_ast_node_block:
911 return translate_isl_ast_node_block (context_loop, node,
912 next_e, ip);
913
914 case isl_ast_node_mark:
915 {
916 isl_ast_node *n = isl_ast_node_mark_get_node (node);
917 edge e = translate_isl_ast (context_loop, n, next_e, ip);
918 isl_ast_node_free (n);
919 return e;
920 }
921
922 default:
923 gcc_unreachable ();
924 }
925 }
926
927 /* Register in RENAME_MAP the rename tuple (OLD_NAME, EXPR).
928 When OLD_NAME and EXPR are the same we assert. */
929
930 void translate_isl_ast_to_gimple::
931 set_rename (tree old_name, tree expr)
932 {
933 if (dump_file)
934 {
935 fprintf (dump_file, "[codegen] setting rename: old_name = ");
936 print_generic_expr (dump_file, old_name);
937 fprintf (dump_file, ", new_name = ");
938 print_generic_expr (dump_file, expr);
939 fprintf (dump_file, "\n");
940 }
941
942 if (old_name == expr)
943 return;
944
945 vec <tree> *renames = region->rename_map->get (old_name);
946
947 if (renames)
948 renames->safe_push (expr);
949 else
950 {
951 vec<tree> r;
952 r.create (2);
953 r.safe_push (expr);
954 region->rename_map->put (old_name, r);
955 }
956
957 tree t;
958 int i;
959 /* For a parameter of a scop we don't want to rename it. */
960 FOR_EACH_VEC_ELT (region->params, i, t)
961 if (old_name == t)
962 region->parameter_rename_map->put(old_name, expr);
963 }
964
965 /* Return an iterator to the instructions comes last in the execution order.
966 Either GSI1 and GSI2 should belong to the same basic block or one of their
967 respective basic blocks should dominate the other. */
968
969 gimple_stmt_iterator
970 later_of_the_two (gimple_stmt_iterator gsi1, gimple_stmt_iterator gsi2)
971 {
972 basic_block bb1 = gsi_bb (gsi1);
973 basic_block bb2 = gsi_bb (gsi2);
974
975 /* Find the iterator which is the latest. */
976 if (bb1 == bb2)
977 {
978 gimple *stmt1 = gsi_stmt (gsi1);
979 gimple *stmt2 = gsi_stmt (gsi2);
980
981 if (stmt1 != NULL && stmt2 != NULL)
982 {
983 bool is_phi1 = gimple_code (stmt1) == GIMPLE_PHI;
984 bool is_phi2 = gimple_code (stmt2) == GIMPLE_PHI;
985
986 if (is_phi1 != is_phi2)
987 return is_phi1 ? gsi2 : gsi1;
988 }
989
990 /* For empty basic blocks gsis point to the end of the sequence. Since
991 there is no operator== defined for gimple_stmt_iterator and for gsis
992 not pointing to a valid statement gsi_next would assert. */
993 gimple_stmt_iterator gsi = gsi1;
994 do {
995 if (gsi_stmt (gsi) == gsi_stmt (gsi2))
996 return gsi2;
997 gsi_next (&gsi);
998 } while (!gsi_end_p (gsi));
999
1000 return gsi1;
1001 }
1002
1003 /* Find the basic block closest to the basic block which defines stmt. */
1004 if (dominated_by_p (CDI_DOMINATORS, bb1, bb2))
1005 return gsi1;
1006
1007 gcc_assert (dominated_by_p (CDI_DOMINATORS, bb2, bb1));
1008 return gsi2;
1009 }
1010
1011 /* Insert each statement from SEQ at its earliest insertion p. */
1012
1013 void translate_isl_ast_to_gimple::
1014 gsi_insert_earliest (gimple_seq seq)
1015 {
1016 update_modified_stmts (seq);
1017 sese_l &codegen_region = region->if_region->true_region->region;
1018 basic_block begin_bb = get_entry_bb (codegen_region);
1019
1020 /* Inserting the gimple statements in a vector because gimple_seq behave
1021 in strage ways when inserting the stmts from it into different basic
1022 blocks one at a time. */
1023 auto_vec<gimple *, 3> stmts;
1024 for (gimple_stmt_iterator gsi = gsi_start (seq); !gsi_end_p (gsi);
1025 gsi_next (&gsi))
1026 stmts.safe_push (gsi_stmt (gsi));
1027
1028 int i;
1029 gimple *use_stmt;
1030 FOR_EACH_VEC_ELT (stmts, i, use_stmt)
1031 {
1032 gcc_assert (gimple_code (use_stmt) != GIMPLE_PHI);
1033 gimple_stmt_iterator gsi_def_stmt = gsi_start_bb_nondebug (begin_bb);
1034
1035 use_operand_p use_p;
1036 ssa_op_iter op_iter;
1037 FOR_EACH_SSA_USE_OPERAND (use_p, use_stmt, op_iter, SSA_OP_USE)
1038 {
1039 /* Iterator to the current def of use_p. For function parameters or
1040 anything where def is not found, insert at the beginning of the
1041 generated region. */
1042 gimple_stmt_iterator gsi_stmt = gsi_def_stmt;
1043
1044 tree op = USE_FROM_PTR (use_p);
1045 gimple *stmt = SSA_NAME_DEF_STMT (op);
1046 if (stmt && (gimple_code (stmt) != GIMPLE_NOP))
1047 gsi_stmt = gsi_for_stmt (stmt);
1048
1049 /* For region parameters, insert at the beginning of the generated
1050 region. */
1051 if (!bb_in_sese_p (gsi_bb (gsi_stmt), codegen_region))
1052 gsi_stmt = gsi_def_stmt;
1053
1054 gsi_def_stmt = later_of_the_two (gsi_stmt, gsi_def_stmt);
1055 }
1056
1057 if (!gsi_stmt (gsi_def_stmt))
1058 {
1059 gimple_stmt_iterator gsi = gsi_after_labels (gsi_bb (gsi_def_stmt));
1060 gsi_insert_before (&gsi, use_stmt, GSI_NEW_STMT);
1061 }
1062 else if (gimple_code (gsi_stmt (gsi_def_stmt)) == GIMPLE_PHI)
1063 {
1064 gimple_stmt_iterator bsi
1065 = gsi_start_bb_nondebug (gsi_bb (gsi_def_stmt));
1066 /* Insert right after the PHI statements. */
1067 gsi_insert_before (&bsi, use_stmt, GSI_NEW_STMT);
1068 }
1069 else
1070 gsi_insert_after (&gsi_def_stmt, use_stmt, GSI_NEW_STMT);
1071
1072 if (dump_file)
1073 {
1074 fprintf (dump_file, "[codegen] inserting statement: ");
1075 print_gimple_stmt (dump_file, use_stmt, 0, TDF_VOPS | TDF_MEMSYMS);
1076 print_loops_bb (dump_file, gimple_bb (use_stmt), 0, 3);
1077 }
1078 }
1079 }
1080
1081 /* For ops which are scev_analyzeable, we can regenerate a new name from its
1082 scalar evolution around LOOP. */
1083
1084 tree translate_isl_ast_to_gimple::
1085 get_rename_from_scev (tree old_name, gimple_seq *stmts, loop_p loop,
1086 basic_block new_bb, basic_block,
1087 vec<tree> iv_map)
1088 {
1089 tree scev = scalar_evolution_in_region (region->region, loop, old_name);
1090
1091 /* At this point we should know the exact scev for each
1092 scalar SSA_NAME used in the scop: all the other scalar
1093 SSA_NAMEs should have been translated out of SSA using
1094 arrays with one element. */
1095 tree new_expr;
1096 if (chrec_contains_undetermined (scev))
1097 {
1098 set_codegen_error ();
1099 return build_zero_cst (TREE_TYPE (old_name));
1100 }
1101
1102 new_expr = chrec_apply_map (scev, iv_map);
1103
1104 /* The apply should produce an expression tree containing
1105 the uses of the new induction variables. We should be
1106 able to use new_expr instead of the old_name in the newly
1107 generated loop nest. */
1108 if (chrec_contains_undetermined (new_expr)
1109 || tree_contains_chrecs (new_expr, NULL))
1110 {
1111 set_codegen_error ();
1112 return build_zero_cst (TREE_TYPE (old_name));
1113 }
1114
1115 if (TREE_CODE (new_expr) == SSA_NAME)
1116 {
1117 basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (new_expr));
1118 if (bb && !dominated_by_p (CDI_DOMINATORS, new_bb, bb))
1119 {
1120 set_codegen_error ();
1121 return build_zero_cst (TREE_TYPE (old_name));
1122 }
1123 }
1124
1125 /* Replace the old_name with the new_expr. */
1126 return force_gimple_operand (unshare_expr (new_expr), stmts,
1127 true, NULL_TREE);
1128 }
1129
1130
1131 /* Return true if STMT should be copied from region to the new code-generated
1132 region. LABELs, CONDITIONS, induction-variables and region parameters need
1133 not be copied. */
1134
1135 static bool
1136 should_copy_to_new_region (gimple *stmt, sese_info_p region)
1137 {
1138 /* Do not copy labels or conditions. */
1139 if (gimple_code (stmt) == GIMPLE_LABEL
1140 || gimple_code (stmt) == GIMPLE_COND)
1141 return false;
1142
1143 tree lhs;
1144 /* Do not copy induction variables. */
1145 if (is_gimple_assign (stmt)
1146 && (lhs = gimple_assign_lhs (stmt))
1147 && TREE_CODE (lhs) == SSA_NAME
1148 && is_gimple_reg (lhs)
1149 && scev_analyzable_p (lhs, region->region))
1150 return false;
1151
1152 /* Do not copy parameters that have been generated in the header of the
1153 scop. */
1154 if (is_gimple_assign (stmt)
1155 && (lhs = gimple_assign_lhs (stmt))
1156 && TREE_CODE (lhs) == SSA_NAME
1157 && region->parameter_rename_map->get(lhs))
1158 return false;
1159
1160 return true;
1161 }
1162
1163 /* Create new names for all the definitions created by COPY and add replacement
1164 mappings for each new name. */
1165
1166 void translate_isl_ast_to_gimple::
1167 set_rename_for_each_def (gimple *stmt)
1168 {
1169 def_operand_p def_p;
1170 ssa_op_iter op_iter;
1171 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, op_iter, SSA_OP_ALL_DEFS)
1172 {
1173 tree old_name = DEF_FROM_PTR (def_p);
1174 create_new_def_for (old_name, stmt, def_p);
1175 }
1176 }
1177
1178 /* Duplicates the statements of basic block BB into basic block NEW_BB
1179 and compute the new induction variables according to the IV_MAP. */
1180
1181 bool translate_isl_ast_to_gimple::
1182 graphite_copy_stmts_from_block (basic_block bb, basic_block new_bb,
1183 vec<tree> iv_map)
1184 {
1185 /* Iterator poining to the place where new statement (s) will be inserted. */
1186 gimple_stmt_iterator gsi_tgt = gsi_last_bb (new_bb);
1187
1188 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
1189 gsi_next (&gsi))
1190 {
1191 gimple *stmt = gsi_stmt (gsi);
1192 if (!should_copy_to_new_region (stmt, region))
1193 continue;
1194
1195 /* Create a new copy of STMT and duplicate STMT's virtual
1196 operands. */
1197 gimple *copy = gimple_copy (stmt);
1198 gsi_insert_after (&gsi_tgt, copy, GSI_NEW_STMT);
1199
1200 /* Rather than not copying debug stmts we reset them.
1201 ??? Where we can rewrite uses without inserting new
1202 stmts we could simply do that. */
1203 if (is_gimple_debug (copy))
1204 {
1205 if (gimple_debug_bind_p (copy))
1206 gimple_debug_bind_reset_value (copy);
1207 else if (gimple_debug_source_bind_p (copy))
1208 ;
1209 else
1210 gcc_unreachable ();
1211 }
1212
1213 if (dump_file)
1214 {
1215 fprintf (dump_file, "[codegen] inserting statement: ");
1216 print_gimple_stmt (dump_file, copy, 0);
1217 }
1218
1219 maybe_duplicate_eh_stmt (copy, stmt);
1220 gimple_duplicate_stmt_histograms (cfun, copy, cfun, stmt);
1221
1222 /* Crete new names for each def in the copied stmt. */
1223 set_rename_for_each_def (copy);
1224
1225 if (codegen_error_p ())
1226 return false;
1227
1228 /* For each SSA_NAME in the parameter_rename_map rename their usage. */
1229 ssa_op_iter iter;
1230 use_operand_p use_p;
1231 if (!is_gimple_debug (copy))
1232 FOR_EACH_SSA_USE_OPERAND (use_p, copy, iter, SSA_OP_USE)
1233 {
1234 tree old_name = USE_FROM_PTR (use_p);
1235
1236 if (TREE_CODE (old_name) != SSA_NAME
1237 || SSA_NAME_IS_DEFAULT_DEF (old_name))
1238 continue;
1239
1240 tree *new_expr = region->parameter_rename_map->get (old_name);
1241 tree new_name;
1242 if (!new_expr
1243 && scev_analyzable_p (old_name, region->region))
1244 {
1245 gimple_seq stmts = NULL;
1246 new_name = get_rename_from_scev (old_name, &stmts,
1247 bb->loop_father,
1248 new_bb, bb, iv_map);
1249 if (! codegen_error_p ())
1250 gsi_insert_earliest (stmts);
1251 new_expr = &new_name;
1252 }
1253
1254 if (!new_expr)
1255 continue;
1256
1257 replace_exp (use_p, *new_expr);
1258 }
1259
1260 update_stmt (copy);
1261 }
1262
1263 return true;
1264 }
1265
1266
1267 /* Copies BB and includes in the copied BB all the statements that can
1268 be reached following the use-def chains from the memory accesses,
1269 and returns the next edge following this new block. */
1270
1271 edge translate_isl_ast_to_gimple::
1272 copy_bb_and_scalar_dependences (basic_block bb, edge next_e, vec<tree> iv_map)
1273 {
1274 basic_block new_bb = split_edge (next_e);
1275 gimple_stmt_iterator gsi_tgt = gsi_last_bb (new_bb);
1276 for (gphi_iterator psi = gsi_start_phis (bb); !gsi_end_p (psi);
1277 gsi_next (&psi))
1278 {
1279 gphi *phi = psi.phi ();
1280 tree res = gimple_phi_result (phi);
1281 if (virtual_operand_p (res)
1282 || scev_analyzable_p (res, region->region))
1283 continue;
1284
1285 tree new_phi_def;
1286 vec <tree> *renames = region->rename_map->get (res);
1287 if (! renames || renames->is_empty ())
1288 {
1289 new_phi_def = create_tmp_reg (TREE_TYPE (res));
1290 set_rename (res, new_phi_def);
1291 }
1292 else
1293 {
1294 gcc_assert (renames->length () == 1);
1295 new_phi_def = (*renames)[0];
1296 }
1297
1298 gassign *ass = gimple_build_assign (NULL_TREE, new_phi_def);
1299 create_new_def_for (res, ass, NULL);
1300 gsi_insert_after (&gsi_tgt, ass, GSI_NEW_STMT);
1301 }
1302
1303 vec <basic_block> *copied_bbs = region->copied_bb_map->get (bb);
1304 if (copied_bbs)
1305 copied_bbs->safe_push (new_bb);
1306 else
1307 {
1308 vec<basic_block> bbs;
1309 bbs.create (2);
1310 bbs.safe_push (new_bb);
1311 region->copied_bb_map->put (bb, bbs);
1312 }
1313
1314 if (!graphite_copy_stmts_from_block (bb, new_bb, iv_map))
1315 {
1316 set_codegen_error ();
1317 return NULL;
1318 }
1319
1320 /* Insert out-of SSA copies on the original BB outgoing edges. */
1321 gsi_tgt = gsi_last_bb (new_bb);
1322 basic_block bb_for_succs = bb;
1323 if (bb_for_succs == bb_for_succs->loop_father->latch
1324 && bb_in_sese_p (bb_for_succs, region->region)
1325 && sese_trivially_empty_bb_p (bb_for_succs))
1326 bb_for_succs = NULL;
1327 while (bb_for_succs)
1328 {
1329 basic_block latch = NULL;
1330 edge_iterator ei;
1331 edge e;
1332 FOR_EACH_EDGE (e, ei, bb_for_succs->succs)
1333 {
1334 for (gphi_iterator psi = gsi_start_phis (e->dest); !gsi_end_p (psi);
1335 gsi_next (&psi))
1336 {
1337 gphi *phi = psi.phi ();
1338 tree res = gimple_phi_result (phi);
1339 if (virtual_operand_p (res)
1340 || scev_analyzable_p (res, region->region))
1341 continue;
1342
1343 tree new_phi_def;
1344 vec <tree> *renames = region->rename_map->get (res);
1345 if (! renames || renames->is_empty ())
1346 {
1347 new_phi_def = create_tmp_reg (TREE_TYPE (res));
1348 set_rename (res, new_phi_def);
1349 }
1350 else
1351 {
1352 gcc_assert (renames->length () == 1);
1353 new_phi_def = (*renames)[0];
1354 }
1355
1356 tree arg = PHI_ARG_DEF_FROM_EDGE (phi, e);
1357 if (TREE_CODE (arg) == SSA_NAME
1358 && scev_analyzable_p (arg, region->region))
1359 {
1360 gimple_seq stmts = NULL;
1361 tree new_name = get_rename_from_scev (arg, &stmts,
1362 bb->loop_father,
1363 new_bb, bb, iv_map);
1364 if (! codegen_error_p ())
1365 gsi_insert_earliest (stmts);
1366 arg = new_name;
1367 }
1368 gassign *ass = gimple_build_assign (new_phi_def, arg);
1369 gsi_insert_after (&gsi_tgt, ass, GSI_NEW_STMT);
1370 }
1371 if (e->dest == bb_for_succs->loop_father->latch
1372 && bb_in_sese_p (e->dest, region->region)
1373 && sese_trivially_empty_bb_p (e->dest))
1374 latch = e->dest;
1375 }
1376 bb_for_succs = latch;
1377 }
1378
1379 return single_succ_edge (new_bb);
1380 }
1381
1382 /* Add isl's parameter identifiers and corresponding trees to ivs_params. */
1383
1384 void translate_isl_ast_to_gimple::
1385 add_parameters_to_ivs_params (scop_p scop, ivs_params &ip)
1386 {
1387 sese_info_p region = scop->scop_info;
1388 unsigned nb_parameters = isl_set_dim (scop->param_context, isl_dim_param);
1389 gcc_assert (nb_parameters == region->params.length ());
1390 unsigned i;
1391 for (i = 0; i < nb_parameters; i++)
1392 {
1393 isl_id *tmp_id = isl_set_get_dim_id (scop->param_context,
1394 isl_dim_param, i);
1395 ip[tmp_id] = region->params[i];
1396 }
1397 }
1398
1399
1400 /* Generates a build, which specifies the constraints on the parameters. */
1401
1402 __isl_give isl_ast_build *translate_isl_ast_to_gimple::
1403 generate_isl_context (scop_p scop)
1404 {
1405 isl_set *context_isl = isl_set_params (isl_set_copy (scop->param_context));
1406 return isl_ast_build_from_context (context_isl);
1407 }
1408
1409 /* This method is executed before the construction of a for node. */
1410 __isl_give isl_id *
1411 ast_build_before_for (__isl_keep isl_ast_build *build, void *user)
1412 {
1413 isl_union_map *dependences = (isl_union_map *) user;
1414 ast_build_info *for_info = XNEW (struct ast_build_info);
1415 isl_union_map *schedule = isl_ast_build_get_schedule (build);
1416 isl_space *schedule_space = isl_ast_build_get_schedule_space (build);
1417 int dimension = isl_space_dim (schedule_space, isl_dim_out);
1418 for_info->is_parallelizable =
1419 !carries_deps (schedule, dependences, dimension);
1420 isl_union_map_free (schedule);
1421 isl_space_free (schedule_space);
1422 isl_id *id = isl_id_alloc (isl_ast_build_get_ctx (build), "", for_info);
1423 return id;
1424 }
1425
1426 /* Generate isl AST from schedule of SCOP. */
1427
1428 __isl_give isl_ast_node *translate_isl_ast_to_gimple::
1429 scop_to_isl_ast (scop_p scop)
1430 {
1431 gcc_assert (scop->transformed_schedule);
1432
1433 /* Set the separate option to reduce control flow overhead. */
1434 isl_schedule *schedule = isl_schedule_map_schedule_node_bottom_up
1435 (isl_schedule_copy (scop->transformed_schedule), set_separate_option, NULL);
1436 isl_ast_build *context_isl = generate_isl_context (scop);
1437
1438 if (flag_loop_parallelize_all)
1439 {
1440 scop_get_dependences (scop);
1441 context_isl =
1442 isl_ast_build_set_before_each_for (context_isl, ast_build_before_for,
1443 scop->dependence);
1444 }
1445
1446 isl_ast_node *ast_isl = isl_ast_build_node_from_schedule
1447 (context_isl, schedule);
1448 isl_ast_build_free (context_isl);
1449 return ast_isl;
1450 }
1451
1452 /* Copy def from sese REGION to the newly created TO_REGION. TR is defined by
1453 DEF_STMT. GSI points to entry basic block of the TO_REGION. */
1454
1455 static void
1456 copy_def (tree tr, gimple *def_stmt, sese_info_p region, sese_info_p to_region,
1457 gimple_stmt_iterator *gsi)
1458 {
1459 if (!defined_in_sese_p (tr, region->region))
1460 return;
1461
1462 ssa_op_iter iter;
1463 use_operand_p use_p;
1464 FOR_EACH_SSA_USE_OPERAND (use_p, def_stmt, iter, SSA_OP_USE)
1465 {
1466 tree use_tr = USE_FROM_PTR (use_p);
1467
1468 /* Do not copy parameters that have been generated in the header of the
1469 scop. */
1470 if (region->parameter_rename_map->get(use_tr))
1471 continue;
1472
1473 gimple *def_of_use = SSA_NAME_DEF_STMT (use_tr);
1474 if (!def_of_use)
1475 continue;
1476
1477 copy_def (use_tr, def_of_use, region, to_region, gsi);
1478 }
1479
1480 gimple *copy = gimple_copy (def_stmt);
1481 gsi_insert_after (gsi, copy, GSI_NEW_STMT);
1482
1483 /* Create new names for all the definitions created by COPY and
1484 add replacement mappings for each new name. */
1485 def_operand_p def_p;
1486 ssa_op_iter op_iter;
1487 FOR_EACH_SSA_DEF_OPERAND (def_p, copy, op_iter, SSA_OP_ALL_DEFS)
1488 {
1489 tree old_name = DEF_FROM_PTR (def_p);
1490 tree new_name = create_new_def_for (old_name, copy, def_p);
1491 region->parameter_rename_map->put(old_name, new_name);
1492 }
1493
1494 update_stmt (copy);
1495 }
1496
1497 static void
1498 copy_internal_parameters (sese_info_p region, sese_info_p to_region)
1499 {
1500 /* For all the parameters which definitino is in the if_region->false_region,
1501 insert code on true_region (if_region->true_region->entry). */
1502
1503 int i;
1504 tree tr;
1505 gimple_stmt_iterator gsi = gsi_start_bb(to_region->region.entry->dest);
1506
1507 FOR_EACH_VEC_ELT (region->params, i, tr)
1508 {
1509 // If def is not in region.
1510 gimple *def_stmt = SSA_NAME_DEF_STMT (tr);
1511 if (def_stmt)
1512 copy_def (tr, def_stmt, region, to_region, &gsi);
1513 }
1514 }
1515
1516 /* GIMPLE Loop Generator: generates loops in GIMPLE form for the given SCOP.
1517 Return true if code generation succeeded. */
1518
1519 bool
1520 graphite_regenerate_ast_isl (scop_p scop)
1521 {
1522 sese_info_p region = scop->scop_info;
1523 translate_isl_ast_to_gimple t (region);
1524
1525 ifsese if_region = NULL;
1526 isl_ast_node *root_node;
1527 ivs_params ip;
1528
1529 timevar_push (TV_GRAPHITE_CODE_GEN);
1530 t.add_parameters_to_ivs_params (scop, ip);
1531 root_node = t.scop_to_isl_ast (scop);
1532
1533 if (dump_file && (dump_flags & TDF_DETAILS))
1534 {
1535 fprintf (dump_file, "[scheduler] original schedule:\n");
1536 print_isl_schedule (dump_file, scop->original_schedule);
1537 fprintf (dump_file, "[scheduler] isl transformed schedule:\n");
1538 print_isl_schedule (dump_file, scop->transformed_schedule);
1539
1540 fprintf (dump_file, "[scheduler] original ast:\n");
1541 print_schedule_ast (dump_file, scop->original_schedule, scop);
1542 fprintf (dump_file, "[scheduler] AST generated by isl:\n");
1543 print_isl_ast (dump_file, root_node);
1544 }
1545
1546 if_region = move_sese_in_condition (region);
1547 region->if_region = if_region;
1548
1549 loop_p context_loop = region->region.entry->src->loop_father;
1550
1551 /* Copy all the parameters which are defined in the region. */
1552 copy_internal_parameters(if_region->false_region, if_region->true_region);
1553
1554 edge e = single_succ_edge (if_region->true_region->region.entry->dest);
1555 basic_block bb = split_edge (e);
1556
1557 /* Update the true_region exit edge. */
1558 region->if_region->true_region->region.exit = single_succ_edge (bb);
1559
1560 t.translate_isl_ast (context_loop, root_node, e, ip);
1561 if (! t.codegen_error_p ())
1562 {
1563 sese_insert_phis_for_liveouts (region,
1564 if_region->region->region.exit->src,
1565 if_region->false_region->region.exit,
1566 if_region->true_region->region.exit);
1567 if (dump_file)
1568 fprintf (dump_file, "[codegen] isl AST to Gimple succeeded.\n");
1569
1570 mark_virtual_operands_for_renaming (cfun);
1571 update_ssa (TODO_update_ssa);
1572 checking_verify_ssa (true, true);
1573 rewrite_into_loop_closed_ssa (NULL, 0);
1574 }
1575
1576 if (t.codegen_error_p ())
1577 {
1578 if (dump_file)
1579 fprintf (dump_file, "codegen error: "
1580 "reverting back to the original code.\n");
1581 set_ifsese_condition (if_region, integer_zero_node);
1582
1583 /* We registered new names, scrap that. */
1584 if (need_ssa_update_p (cfun))
1585 delete_update_ssa ();
1586 /* Remove the unreachable region. */
1587 remove_edge_and_dominated_blocks (if_region->true_region->region.entry);
1588 basic_block ifb = if_region->false_region->region.entry->src;
1589 gimple_stmt_iterator gsi = gsi_last_bb (ifb);
1590 gsi_remove (&gsi, true);
1591 if_region->false_region->region.entry->flags &= ~EDGE_FALSE_VALUE;
1592 if_region->false_region->region.entry->flags |= EDGE_FALLTHRU;
1593 /* remove_edge_and_dominated_blocks marks loops for removal but
1594 doesn't actually remove them (fix that...). */
1595 loop_p loop;
1596 FOR_EACH_LOOP (loop, LI_FROM_INNERMOST)
1597 if (! loop->header)
1598 delete_loop (loop);
1599 }
1600
1601 /* Verifies properties that GRAPHITE should maintain during translation. */
1602 checking_verify_loop_structure ();
1603 checking_verify_loop_closed_ssa (true);
1604
1605 free (if_region->true_region);
1606 free (if_region->region);
1607 free (if_region);
1608
1609 ivs_params_clear (ip);
1610 isl_ast_node_free (root_node);
1611 timevar_pop (TV_GRAPHITE_CODE_GEN);
1612
1613 return !t.codegen_error_p ();
1614 }
1615
1616 #endif /* HAVE_isl */