]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/graphite-isl-ast-to-gimple.c
gimple-predict.h: New file.
[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
21#include "config.h"
22
eae1a5d4 23#ifdef HAVE_isl
4bc190dc
JW
24/* Workaround for GMP 5.1.3 bug, see PR56019. */
25#include <stddef.h>
26
f6cc3103
RG
27#include <isl/set.h>
28#include <isl/map.h>
29#include <isl/union_map.h>
30#include <isl/ast_build.h>
797d8858
TB
31
32/* Since ISL-0.13, the extern is in val_gmp.h. */
33#if !defined(HAVE_ISL_SCHED_CONSTRAINTS_COMPUTE_SCHEDULE) && defined(__cplusplus)
a78cfa7f
RG
34extern "C" {
35#endif
36#include <isl/val_gmp.h>
797d8858 37#if !defined(HAVE_ISL_SCHED_CONSTRAINTS_COMPUTE_SCHEDULE) && defined(__cplusplus)
a78cfa7f
RG
38}
39#endif
7d1ceb93 40#endif
f6cc3103
RG
41
42#include "system.h"
43#include "coretypes.h"
40e23961 44#include "alias.h"
c7131fb2 45#include "backend.h"
9fdcd34e 46#include "cfghooks.h"
40e23961 47#include "tree.h"
c7131fb2 48#include "gimple.h"
60393bbc 49#include "hard-reg-set.h"
c7131fb2
AM
50#include "options.h"
51#include "fold-const.h"
f6cc3103 52#include "internal-fn.h"
f6cc3103
RG
53#include "gimple-iterator.h"
54#include "tree-ssa-loop.h"
55#include "tree-pass.h"
56#include "cfgloop.h"
57#include "tree-data-ref.h"
58#include "sese.h"
a78cfa7f
RG
59#include "tree-ssa-loop-manip.h"
60#include "tree-scalar-evolution.h"
5493d313
RG
61#include "gimple-ssa.h"
62#include "tree-into-ssa.h"
a78cfa7f 63#include <map>
f6cc3103 64
eae1a5d4 65#ifdef HAVE_isl
f6cc3103
RG
66#include "graphite-poly.h"
67#include "graphite-isl-ast-to-gimple.h"
68
69/* This flag is set when an error occurred during the translation of
70 ISL AST to Gimple. */
71
72static bool graphite_regenerate_error;
73
55d1bd59
RG
74/* We always try to use signed 128 bit types, but fall back to smaller types
75 in case a platform does not provide types of these sizes. In the future we
76 should use isl to derive the optimal type for each subexpression. */
a78cfa7f 77
55d1bd59
RG
78static int max_mode_int_precision =
79 GET_MODE_PRECISION (mode_for_size (MAX_FIXED_MODE_SIZE, MODE_INT, 0));
80static int graphite_expression_type_precision = 128 <= max_mode_int_precision ?
81 128 : max_mode_int_precision;
a78cfa7f 82
574921c2
RG
83struct ast_build_info
84{
85 ast_build_info()
86 : is_parallelizable(false)
87 { };
88 bool is_parallelizable;
89};
90
a78cfa7f
RG
91/* Converts a GMP constant VAL to a tree and returns it. */
92
93static tree
94gmp_cst_to_tree (tree type, mpz_t val)
95{
96 tree t = type ? type : integer_type_node;
97 mpz_t tmp;
98
99 mpz_init (tmp);
100 mpz_set (tmp, val);
101 wide_int wi = wi::from_mpz (t, tmp, true);
102 mpz_clear (tmp);
103
104 return wide_int_to_tree (t, wi);
105}
106
107/* Verifies properties that GRAPHITE should maintain during translation. */
108
109static inline void
110graphite_verify (void)
111{
112#ifdef ENABLE_CHECKING
113 verify_loop_structure ();
114 verify_loop_closed_ssa (true);
115#endif
116}
117
118/* IVS_PARAMS maps ISL's scattering and parameter identifiers
119 to corresponding trees. */
120
121typedef std::map<isl_id *, tree> ivs_params;
122
123/* Free all memory allocated for ISL's identifiers. */
124
125void ivs_params_clear (ivs_params &ip)
126{
127 std::map<isl_id *, tree>::iterator it;
128 for (it = ip.begin ();
129 it != ip.end (); it++)
130 {
131 isl_id_free (it->first);
132 }
133}
134
135static tree
136gcc_expression_from_isl_expression (tree type, __isl_take isl_ast_expr *,
137 ivs_params &ip);
138
139/* Return the tree variable that corresponds to the given isl ast identifier
6a7d8936
RG
140 expression (an isl_ast_expr of type isl_ast_expr_id).
141
142 FIXME: We should replace blind conversation of id's type with derivation
143 of the optimal type when we get the corresponding isl support. Blindly
144 converting type sizes may be problematic when we switch to smaller
145 types. */
a78cfa7f
RG
146
147static tree
6a7d8936
RG
148gcc_expression_from_isl_ast_expr_id (tree type,
149 __isl_keep isl_ast_expr *expr_id,
a78cfa7f
RG
150 ivs_params &ip)
151{
152 gcc_assert (isl_ast_expr_get_type (expr_id) == isl_ast_expr_id);
153 isl_id *tmp_isl_id = isl_ast_expr_get_id (expr_id);
154 std::map<isl_id *, tree>::iterator res;
155 res = ip.find (tmp_isl_id);
156 isl_id_free (tmp_isl_id);
157 gcc_assert (res != ip.end () &&
158 "Could not map isl_id to tree expression");
159 isl_ast_expr_free (expr_id);
6a7d8936 160 return fold_convert (type, res->second);
a78cfa7f
RG
161}
162
163/* Converts an isl_ast_expr_int expression E to a GCC expression tree of
164 type TYPE. */
165
166static tree
167gcc_expression_from_isl_expr_int (tree type, __isl_take isl_ast_expr *expr)
168{
169 gcc_assert (isl_ast_expr_get_type (expr) == isl_ast_expr_int);
170 isl_val *val = isl_ast_expr_get_val (expr);
171 mpz_t val_mpz_t;
172 mpz_init (val_mpz_t);
173 tree res;
174 if (isl_val_get_num_gmp (val, val_mpz_t) == -1)
175 res = NULL_TREE;
176 else
177 res = gmp_cst_to_tree (type, val_mpz_t);
178 isl_val_free (val);
179 isl_ast_expr_free (expr);
180 mpz_clear (val_mpz_t);
181 return res;
182}
183
184/* Converts a binary isl_ast_expr_op expression E to a GCC expression tree of
185 type TYPE. */
186
187static tree
188binary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
189{
190 isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0);
191 tree tree_lhs_expr = gcc_expression_from_isl_expression (type, arg_expr, ip);
192 arg_expr = isl_ast_expr_get_op_arg (expr, 1);
193 tree tree_rhs_expr = gcc_expression_from_isl_expression (type, arg_expr, ip);
194 enum isl_ast_op_type expr_type = isl_ast_expr_get_op_type (expr);
195 isl_ast_expr_free (expr);
196 switch (expr_type)
197 {
198 case isl_ast_op_add:
199 return fold_build2 (PLUS_EXPR, type, tree_lhs_expr, tree_rhs_expr);
200
201 case isl_ast_op_sub:
202 return fold_build2 (MINUS_EXPR, type, tree_lhs_expr, tree_rhs_expr);
203
204 case isl_ast_op_mul:
205 return fold_build2 (MULT_EXPR, type, tree_lhs_expr, tree_rhs_expr);
206
207 case isl_ast_op_div:
208 return fold_build2 (EXACT_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr);
209
c4bc3399
RG
210 case isl_ast_op_pdiv_q:
211 return fold_build2 (TRUNC_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr);
212
213 case isl_ast_op_pdiv_r:
214 return fold_build2 (TRUNC_MOD_EXPR, type, tree_lhs_expr, tree_rhs_expr);
215
a78cfa7f
RG
216 case isl_ast_op_fdiv_q:
217 return fold_build2 (FLOOR_DIV_EXPR, type, tree_lhs_expr, tree_rhs_expr);
218
219 case isl_ast_op_and:
220 return fold_build2 (TRUTH_ANDIF_EXPR, type,
221 tree_lhs_expr, tree_rhs_expr);
222
223 case isl_ast_op_or:
224 return fold_build2 (TRUTH_ORIF_EXPR, type, tree_lhs_expr, tree_rhs_expr);
225
226 case isl_ast_op_eq:
227 return fold_build2 (EQ_EXPR, type, tree_lhs_expr, tree_rhs_expr);
228
229 case isl_ast_op_le:
230 return fold_build2 (LE_EXPR, type, tree_lhs_expr, tree_rhs_expr);
231
232 case isl_ast_op_lt:
233 return fold_build2 (LT_EXPR, type, tree_lhs_expr, tree_rhs_expr);
234
235 case isl_ast_op_ge:
236 return fold_build2 (GE_EXPR, type, tree_lhs_expr, tree_rhs_expr);
237
238 case isl_ast_op_gt:
239 return fold_build2 (GT_EXPR, type, tree_lhs_expr, tree_rhs_expr);
240
241 default:
242 gcc_unreachable ();
243 }
244}
245
246/* Converts a ternary isl_ast_expr_op expression E to a GCC expression tree of
247 type TYPE. */
248
249static tree
250ternary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
251{
252 gcc_assert (isl_ast_expr_get_op_type (expr) == isl_ast_op_minus);
253 isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0);
254 tree tree_first_expr
255 = gcc_expression_from_isl_expression (type, arg_expr, ip);
256 arg_expr = isl_ast_expr_get_op_arg (expr, 1);
257 tree tree_second_expr
258 = gcc_expression_from_isl_expression (type, arg_expr, ip);
259 arg_expr = isl_ast_expr_get_op_arg (expr, 2);
260 tree tree_third_expr
261 = gcc_expression_from_isl_expression (type, arg_expr, ip);
262 isl_ast_expr_free (expr);
263 return fold_build3 (COND_EXPR, type, tree_first_expr,
264 tree_second_expr, tree_third_expr);
265}
266
267/* Converts a unary isl_ast_expr_op expression E to a GCC expression tree of
268 type TYPE. */
269
270static tree
271unary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
272{
273 gcc_assert (isl_ast_expr_get_op_type (expr) == isl_ast_op_minus);
274 isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0);
275 tree tree_expr = gcc_expression_from_isl_expression (type, arg_expr, ip);
276 isl_ast_expr_free (expr);
277 return fold_build1 (NEGATE_EXPR, type, tree_expr);
278}
279
280/* Converts an isl_ast_expr_op expression E with unknown number of arguments
281 to a GCC expression tree of type TYPE. */
282
283static tree
284nary_op_to_tree (tree type, __isl_take isl_ast_expr *expr, ivs_params &ip)
285{
286 enum tree_code op_code;
287 switch (isl_ast_expr_get_op_type (expr))
288 {
289 case isl_ast_op_max:
290 op_code = MAX_EXPR;
291 break;
292
293 case isl_ast_op_min:
294 op_code = MIN_EXPR;
295 break;
296
297 default:
298 gcc_unreachable ();
299 }
300 isl_ast_expr *arg_expr = isl_ast_expr_get_op_arg (expr, 0);
301 tree res = gcc_expression_from_isl_expression (type, arg_expr, ip);
302 int i;
303 for (i = 1; i < isl_ast_expr_get_op_n_arg (expr); i++)
304 {
305 arg_expr = isl_ast_expr_get_op_arg (expr, i);
306 tree t = gcc_expression_from_isl_expression (type, arg_expr, ip);
307 res = fold_build2 (op_code, type, res, t);
308 }
309 isl_ast_expr_free (expr);
310 return res;
311}
312
313
314/* Converts an isl_ast_expr_op expression E to a GCC expression tree of
315 type TYPE. */
316
317static tree
318gcc_expression_from_isl_expr_op (tree type, __isl_take isl_ast_expr *expr,
319 ivs_params &ip)
320{
321 gcc_assert (isl_ast_expr_get_type (expr) == isl_ast_expr_op);
322 switch (isl_ast_expr_get_op_type (expr))
323 {
324 /* These isl ast expressions are not supported yet. */
325 case isl_ast_op_error:
326 case isl_ast_op_call:
327 case isl_ast_op_and_then:
328 case isl_ast_op_or_else:
a78cfa7f
RG
329 case isl_ast_op_select:
330 gcc_unreachable ();
331
332 case isl_ast_op_max:
333 case isl_ast_op_min:
334 return nary_op_to_tree (type, expr, ip);
335
336 case isl_ast_op_add:
337 case isl_ast_op_sub:
338 case isl_ast_op_mul:
339 case isl_ast_op_div:
c4bc3399
RG
340 case isl_ast_op_pdiv_q:
341 case isl_ast_op_pdiv_r:
a78cfa7f
RG
342 case isl_ast_op_fdiv_q:
343 case isl_ast_op_and:
344 case isl_ast_op_or:
345 case isl_ast_op_eq:
346 case isl_ast_op_le:
347 case isl_ast_op_lt:
348 case isl_ast_op_ge:
349 case isl_ast_op_gt:
350 return binary_op_to_tree (type, expr, ip);
351
352 case isl_ast_op_minus:
353 return unary_op_to_tree (type, expr, ip);
354
355 case isl_ast_op_cond:
356 return ternary_op_to_tree (type, expr, ip);
357
358 default:
359 gcc_unreachable ();
360 }
361
362 return NULL_TREE;
363}
364
365/* Converts an ISL AST expression E back to a GCC expression tree of
366 type TYPE. */
367
368static tree
369gcc_expression_from_isl_expression (tree type, __isl_take isl_ast_expr *expr,
370 ivs_params &ip)
371{
372 switch (isl_ast_expr_get_type (expr))
373 {
374 case isl_ast_expr_id:
6a7d8936 375 return gcc_expression_from_isl_ast_expr_id (type, expr, ip);
a78cfa7f
RG
376
377 case isl_ast_expr_int:
378 return gcc_expression_from_isl_expr_int (type, expr);
379
380 case isl_ast_expr_op:
381 return gcc_expression_from_isl_expr_op (type, expr, ip);
382
383 default:
384 gcc_unreachable ();
385 }
386
387 return NULL_TREE;
388}
389
390/* Creates a new LOOP corresponding to isl_ast_node_for. Inserts an
391 induction variable for the new LOOP. New LOOP is attached to CFG
392 starting at ENTRY_EDGE. LOOP is inserted into the loop tree and
393 becomes the child loop of the OUTER_LOOP. NEWIVS_INDEX binds
394 ISL's scattering name to the induction variable created for the
395 loop of STMT. The new induction variable is inserted in the NEWIVS
396 vector and is of type TYPE. */
397
398static struct loop *
399graphite_create_new_loop (edge entry_edge, __isl_keep isl_ast_node *node_for,
400 loop_p outer, tree type, tree lb, tree ub,
401 ivs_params &ip)
402{
403 isl_ast_expr *for_inc = isl_ast_node_for_get_inc (node_for);
404 tree stride = gcc_expression_from_isl_expression (type, for_inc, ip);
405 tree ivvar = create_tmp_var (type, "graphite_IV");
406 tree iv, iv_after_increment;
407 loop_p loop = create_empty_loop_on_edge
408 (entry_edge, lb, stride, ub, ivvar, &iv, &iv_after_increment,
409 outer ? outer : entry_edge->src->loop_father);
410
411 isl_ast_expr *for_iterator = isl_ast_node_for_get_iterator (node_for);
412 isl_id *id = isl_ast_expr_get_id (for_iterator);
a6631027
RG
413 std::map<isl_id *, tree>::iterator res;
414 res = ip.find (id);
415 if (ip.count (id))
416 isl_id_free (res->first);
a78cfa7f
RG
417 ip[id] = iv;
418 isl_ast_expr_free (for_iterator);
419 return loop;
420}
421
422static edge
423translate_isl_ast (loop_p context_loop, __isl_keep isl_ast_node *node,
424 edge next_e, ivs_params &ip);
425
426/* Create the loop for a isl_ast_node_for.
427
428 - NEXT_E is the edge where new generated code should be attached. */
429
430static edge
431translate_isl_ast_for_loop (loop_p context_loop,
432 __isl_keep isl_ast_node *node_for, edge next_e,
433 tree type, tree lb, tree ub,
434 ivs_params &ip)
435{
436 gcc_assert (isl_ast_node_get_type (node_for) == isl_ast_node_for);
437 struct loop *loop = graphite_create_new_loop (next_e, node_for, context_loop,
438 type, lb, ub, ip);
439 edge last_e = single_exit (loop);
440 edge to_body = single_succ_edge (loop->header);
441 basic_block after = to_body->dest;
442
443 /* Create a basic block for loop close phi nodes. */
444 last_e = single_succ_edge (split_edge (last_e));
445
446 /* Translate the body of the loop. */
447 isl_ast_node *for_body = isl_ast_node_for_get_body (node_for);
448 next_e = translate_isl_ast (loop, for_body, to_body, ip);
449 isl_ast_node_free (for_body);
450 redirect_edge_succ_nodup (next_e, after);
451 set_immediate_dominator (CDI_DOMINATORS, next_e->dest, next_e->src);
452
574921c2
RG
453 if (flag_loop_parallelize_all)
454 {
455 isl_id *id = isl_ast_node_get_annotation (node_for);
456 gcc_assert (id);
457 ast_build_info *for_info = (ast_build_info *) isl_id_get_user (id);
458 loop->can_be_parallel = for_info->is_parallelizable;
459 free (for_info);
460 isl_id_free (id);
461 }
a78cfa7f
RG
462
463 return last_e;
464}
465
466/* We use this function to get the upper bound because of the form,
467 which is used by isl to represent loops:
468
469 for (iterator = init; cond; iterator += inc)
470
471 {
472
473 ...
474
475 }
476
477 The loop condition is an arbitrary expression, which contains the
478 current loop iterator.
479
480 (e.g. iterator + 3 < B && C > iterator + A)
481
482 We have to know the upper bound of the iterator to generate a loop
483 in Gimple form. It can be obtained from the special representation
484 of the loop condition, which is generated by isl,
485 if the ast_build_atomic_upper_bound option is set. In this case,
486 isl generates a loop condition that consists of the current loop
487 iterator, + an operator (< or <=) and an expression not involving
488 the iterator, which is processed and returned by this function.
489
490 (e.g iterator <= upper-bound-expression-without-iterator) */
491
492static __isl_give isl_ast_expr *
493get_upper_bound (__isl_keep isl_ast_node *node_for)
494{
495 gcc_assert (isl_ast_node_get_type (node_for) == isl_ast_node_for);
496 isl_ast_expr *for_cond = isl_ast_node_for_get_cond (node_for);
497 gcc_assert (isl_ast_expr_get_type (for_cond) == isl_ast_expr_op);
498 isl_ast_expr *res;
499 switch (isl_ast_expr_get_op_type (for_cond))
500 {
501 case isl_ast_op_le:
502 res = isl_ast_expr_get_op_arg (for_cond, 1);
503 break;
504
505 case isl_ast_op_lt:
506 {
507 // (iterator < ub) => (iterator <= ub - 1)
2a466686
RG
508 isl_val *one =
509 isl_val_int_from_si (isl_ast_expr_get_ctx (for_cond), 1);
a78cfa7f
RG
510 isl_ast_expr *ub = isl_ast_expr_get_op_arg (for_cond, 1);
511 res = isl_ast_expr_sub (ub, isl_ast_expr_from_val (one));
512 break;
513 }
514
515 default:
516 gcc_unreachable ();
517 }
518 isl_ast_expr_free (for_cond);
519 return res;
520}
521
522/* All loops generated by create_empty_loop_on_edge have the form of
523 a post-test loop:
524
525 do
526
527 {
528 body of the loop;
529 } while (lower bound < upper bound);
530
531 We create a new if region protecting the loop to be executed, if
532 the execution count is zero (lower bound > upper bound). */
533
534static edge
535graphite_create_new_loop_guard (edge entry_edge,
536 __isl_keep isl_ast_node *node_for, tree *type,
537 tree *lb, tree *ub, ivs_params &ip)
538{
539 gcc_assert (isl_ast_node_get_type (node_for) == isl_ast_node_for);
540 tree cond_expr;
541 edge exit_edge;
542
55d1bd59
RG
543 *type =
544 build_nonstandard_integer_type (graphite_expression_type_precision, 0);
a78cfa7f
RG
545 isl_ast_expr *for_init = isl_ast_node_for_get_init (node_for);
546 *lb = gcc_expression_from_isl_expression (*type, for_init, ip);
547 isl_ast_expr *upper_bound = get_upper_bound (node_for);
548 *ub = gcc_expression_from_isl_expression (*type, upper_bound, ip);
549
550 /* When ub is simply a constant or a parameter, use lb <= ub. */
551 if (TREE_CODE (*ub) == INTEGER_CST || TREE_CODE (*ub) == SSA_NAME)
552 cond_expr = fold_build2 (LE_EXPR, boolean_type_node, *lb, *ub);
553 else
554 {
555 tree one = (POINTER_TYPE_P (*type)
556 ? convert_to_ptrofftype (integer_one_node)
557 : fold_convert (*type, integer_one_node));
558 /* Adding +1 and using LT_EXPR helps with loop latches that have a
559 loop iteration count of "PARAMETER - 1". For PARAMETER == 0 this
560 becomes 2^k-1 due to integer overflow, and the condition lb <= ub
561 is true, even if we do not want this. However lb < ub + 1 is false,
562 as expected. */
563 tree ub_one = fold_build2 (POINTER_TYPE_P (*type) ? POINTER_PLUS_EXPR
564 : PLUS_EXPR, *type, *ub, one);
565
566 cond_expr = fold_build2 (LT_EXPR, boolean_type_node, *lb, ub_one);
567 }
568
569 exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr);
570
571 return exit_edge;
572}
573
574/* Translates an isl_ast_node_for to Gimple. */
575
576static edge
577translate_isl_ast_node_for (loop_p context_loop, __isl_keep isl_ast_node *node,
578 edge next_e, ivs_params &ip)
579{
580 gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_for);
581 tree type, lb, ub;
582 edge last_e = graphite_create_new_loop_guard (next_e, node, &type,
583 &lb, &ub, ip);
584 edge true_e = get_true_edge_from_guard_bb (next_e->dest);
585
586 translate_isl_ast_for_loop (context_loop, node, true_e,
587 type, lb, ub, ip);
588 return last_e;
589}
590
5493d313
RG
591/* Inserts in iv_map a tuple (OLD_LOOP->num, NEW_NAME) for the induction
592 variables of the loops around GBB in SESE.
593
594 FIXME: Instead of using a vec<tree> that maps each loop id to a possible
595 chrec, we could consider using a map<int, tree> that maps loop ids to the
596 corresponding tree expressions. */
597
598static void
599build_iv_mapping (vec<tree> iv_map, gimple_bb_p gbb,
600 __isl_keep isl_ast_expr *user_expr, ivs_params &ip,
601 sese region)
602{
603 gcc_assert (isl_ast_expr_get_type (user_expr) == isl_ast_expr_op &&
604 isl_ast_expr_get_op_type (user_expr) == isl_ast_op_call);
605 int i;
606 isl_ast_expr *arg_expr;
607 for (i = 1; i < isl_ast_expr_get_op_n_arg (user_expr); i++)
608 {
609 arg_expr = isl_ast_expr_get_op_arg (user_expr, i);
610 tree type =
611 build_nonstandard_integer_type (graphite_expression_type_precision, 0);
612 tree t = gcc_expression_from_isl_expression (type, arg_expr, ip);
613 loop_p old_loop = gbb_loop_at_index (gbb, region, i - 1);
614 iv_map[old_loop->num] = t;
615 }
616
617}
618
619/* Translates an isl_ast_node_user to Gimple.
620
621 FIXME: We should remove iv_map.create (loop->num + 1), if it is possible. */
622
623static edge
624translate_isl_ast_node_user (__isl_keep isl_ast_node *node,
625 edge next_e, ivs_params &ip)
626{
627 gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_user);
628 isl_ast_expr *user_expr = isl_ast_node_user_get_expr (node);
629 isl_ast_expr *name_expr = isl_ast_expr_get_op_arg (user_expr, 0);
630 gcc_assert (isl_ast_expr_get_type (name_expr) == isl_ast_expr_id);
631 isl_id *name_id = isl_ast_expr_get_id (name_expr);
632 poly_bb_p pbb = (poly_bb_p) isl_id_get_user (name_id);
633 gcc_assert (pbb);
634 gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
635 vec<tree> iv_map;
636 isl_ast_expr_free (name_expr);
637 isl_id_free (name_id);
638
639 gcc_assert (GBB_BB (gbb) != ENTRY_BLOCK_PTR_FOR_FN (cfun) &&
640 "The entry block should not even appear within a scop");
641
b5bdf598
RG
642 int nb_loops = number_of_loops (cfun);
643 iv_map.create (nb_loops);
644 iv_map.safe_grow_cleared (nb_loops);
5493d313
RG
645
646 build_iv_mapping (iv_map, gbb, user_expr, ip, SCOP_REGION (pbb->scop));
647 isl_ast_expr_free (user_expr);
648 next_e = copy_bb_and_scalar_dependences (GBB_BB (gbb),
649 SCOP_REGION (pbb->scop), next_e,
650 iv_map,
651 &graphite_regenerate_error);
652 iv_map.release ();
653 mark_virtual_operands_for_renaming (cfun);
654 update_ssa (TODO_update_ssa);
655 return next_e;
656}
657
322a0b39
RG
658/* Translates an isl_ast_node_block to Gimple. */
659
660static edge
661translate_isl_ast_node_block (loop_p context_loop,
662 __isl_keep isl_ast_node *node,
663 edge next_e, ivs_params &ip)
664{
665 gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_block);
666 isl_ast_node_list *node_list = isl_ast_node_block_get_children (node);
667 int i;
668 for (i = 0; i < isl_ast_node_list_n_ast_node (node_list); i++)
669 {
670 isl_ast_node *tmp_node = isl_ast_node_list_get_ast_node (node_list, i);
671 next_e = translate_isl_ast (context_loop, tmp_node, next_e, ip);
672 isl_ast_node_free (tmp_node);
673 }
674 isl_ast_node_list_free (node_list);
675 return next_e;
676}
333cc518
RG
677
678/* Creates a new if region corresponding to ISL's cond. */
679
680static edge
681graphite_create_new_guard (edge entry_edge, __isl_take isl_ast_expr *if_cond,
682 ivs_params &ip)
683{
684 tree type =
685 build_nonstandard_integer_type (graphite_expression_type_precision, 0);
686 tree cond_expr = gcc_expression_from_isl_expression (type, if_cond, ip);
687 edge exit_edge = create_empty_if_region_on_edge (entry_edge, cond_expr);
688 return exit_edge;
689}
690
691/* Translates an isl_ast_node_if to Gimple. */
692
693static edge
694translate_isl_ast_node_if (loop_p context_loop,
695 __isl_keep isl_ast_node *node,
696 edge next_e, ivs_params &ip)
697{
698 gcc_assert (isl_ast_node_get_type (node) == isl_ast_node_if);
699 isl_ast_expr *if_cond = isl_ast_node_if_get_cond (node);
700 edge last_e = graphite_create_new_guard (next_e, if_cond, ip);
701
702 edge true_e = get_true_edge_from_guard_bb (next_e->dest);
703 isl_ast_node *then_node = isl_ast_node_if_get_then (node);
704 translate_isl_ast (context_loop, then_node, true_e, ip);
705 isl_ast_node_free (then_node);
706
707 edge false_e = get_false_edge_from_guard_bb (next_e->dest);
708 isl_ast_node *else_node = isl_ast_node_if_get_else (node);
709 if (isl_ast_node_get_type (else_node) != isl_ast_node_error)
710 translate_isl_ast (context_loop, else_node, false_e, ip);
711 isl_ast_node_free (else_node);
712 return last_e;
713}
322a0b39 714
a78cfa7f
RG
715/* Translates an ISL AST node NODE to GCC representation in the
716 context of a SESE. */
717
718static edge
719translate_isl_ast (loop_p context_loop, __isl_keep isl_ast_node *node,
720 edge next_e, ivs_params &ip)
721{
722 switch (isl_ast_node_get_type (node))
723 {
724 case isl_ast_node_error:
725 gcc_unreachable ();
726
727 case isl_ast_node_for:
728 return translate_isl_ast_node_for (context_loop, node,
729 next_e, ip);
730
731 case isl_ast_node_if:
333cc518
RG
732 return translate_isl_ast_node_if (context_loop, node,
733 next_e, ip);
a78cfa7f
RG
734
735 case isl_ast_node_user:
5493d313 736 return translate_isl_ast_node_user (node, next_e, ip);
a78cfa7f
RG
737
738 case isl_ast_node_block:
322a0b39
RG
739 return translate_isl_ast_node_block (context_loop, node,
740 next_e, ip);
a78cfa7f
RG
741
742 default:
743 gcc_unreachable ();
744 }
745}
746
f6cc3103
RG
747/* Prints NODE to FILE. */
748
749void
750print_isl_ast_node (FILE *file, __isl_keep isl_ast_node *node,
751 __isl_keep isl_ctx *ctx)
752{
753 isl_printer *prn = isl_printer_to_file (ctx, file);
754 prn = isl_printer_set_output_format (prn, ISL_FORMAT_C);
755 prn = isl_printer_print_ast_node (prn, node);
756 prn = isl_printer_print_str (prn, "\n");
757 isl_printer_free (prn);
758}
759
a78cfa7f
RG
760/* Add ISL's parameter identifiers and corresponding.trees to ivs_params */
761
762static void
763add_parameters_to_ivs_params (scop_p scop, ivs_params &ip)
764{
765 sese region = SCOP_REGION (scop);
766 unsigned nb_parameters = isl_set_dim (scop->context, isl_dim_param);
767 gcc_assert (nb_parameters == SESE_PARAMS (region).length ());
768 unsigned i;
769 for (i = 0; i < nb_parameters; i++)
770 {
771 isl_id *tmp_id = isl_set_get_dim_id (scop->context, isl_dim_param, i);
772 ip[tmp_id] = SESE_PARAMS (region)[i];
773 }
774}
775
776
f6cc3103
RG
777/* Generates a build, which specifies the constraints on the parameters. */
778
e4a452b2 779static __isl_give isl_ast_build *
f6cc3103
RG
780generate_isl_context (scop_p scop)
781{
782 isl_set *context_isl = isl_set_params (isl_set_copy (scop->context));
783 return isl_ast_build_from_context (context_isl);
784}
785
fb3764d1
RG
786/* Get the maximal number of schedule dimensions in the scop SCOP. */
787
788static
789int get_max_schedule_dimensions (scop_p scop)
790{
791 int i;
792 poly_bb_p pbb;
793 int schedule_dims = 0;
794
795 FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
796 {
797 int pbb_schedule_dims = isl_map_dim (pbb->transformed, isl_dim_out);
798 if (pbb_schedule_dims > schedule_dims)
799 schedule_dims = pbb_schedule_dims;
800 }
801
802 return schedule_dims;
803}
804
805/* Extend the schedule to NB_SCHEDULE_DIMS schedule dimensions.
806
807 For schedules with different dimensionality, the isl AST generator can not
808 define an order and will just randomly choose an order. The solution to this
809 problem is to extend all schedules to the maximal number of schedule
810 dimensions (using '0's for the remaining values). */
811
812static __isl_give isl_map *
813extend_schedule (__isl_take isl_map *schedule, int nb_schedule_dims)
814{
815 int tmp_dims = isl_map_dim (schedule, isl_dim_out);
816 schedule =
817 isl_map_add_dims (schedule, isl_dim_out, nb_schedule_dims - tmp_dims);
818 isl_val *zero =
819 isl_val_int_from_si (isl_map_get_ctx (schedule), 0);
820 int i;
821 for (i = tmp_dims; i < nb_schedule_dims; i++)
822 {
823 schedule =
824 isl_map_fix_val (schedule, isl_dim_out, i, isl_val_copy (zero));
825 }
826 isl_val_free (zero);
827 return schedule;
828}
829
20d3465b
MN
830/* Set the separation_class option for unroll and jam. */
831
832static __isl_give isl_union_map *
833generate_luj_sepclass_opt (scop_p scop, __isl_take isl_union_set *domain,
834 int dim, int cl)
835{
836 isl_map *map;
837 isl_space *space, *space_sep;
838 isl_ctx *ctx;
839 isl_union_map *mapu;
840 int nsched = get_max_schedule_dimensions (scop);
841
842 ctx = scop->ctx;
843 space_sep = isl_space_alloc (ctx, 0, 1, 1);
844 space_sep = isl_space_wrap (space_sep);
845 space_sep = isl_space_set_tuple_name (space_sep, isl_dim_set,
846 "separation_class");
847 space = isl_set_get_space (scop->context);
848 space_sep = isl_space_align_params (space_sep, isl_space_copy(space));
849 space = isl_space_map_from_domain_and_range (space, space_sep);
850 space = isl_space_add_dims (space,isl_dim_in, nsched);
851 map = isl_map_universe (space);
852 isl_map_fix_si (map,isl_dim_out,0,dim);
853 isl_map_fix_si (map,isl_dim_out,1,cl);
854
855 mapu = isl_union_map_intersect_domain (isl_union_map_from_map (map),
856 domain);
857 return (mapu);
858}
859
860/* Compute the separation class for loop unroll and jam. */
861
862static __isl_give isl_union_set *
863generate_luj_sepclass (scop_p scop)
864{
865 int i;
866 poly_bb_p pbb;
867 isl_union_set *domain_isl;
868
869 domain_isl = isl_union_set_empty (isl_set_get_space (scop->context));
870
871 FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
872 {
873 isl_set *bb_domain;
874 isl_set *bb_domain_s;
875
876 if (pbb->map_sepclass == NULL)
877 continue;
878
879 if (isl_set_is_empty (pbb->domain))
880 continue;
881
882 bb_domain = isl_set_copy (pbb->domain);
883 bb_domain_s = isl_set_apply (bb_domain, pbb->map_sepclass);
884 pbb->map_sepclass = NULL;
885
886 domain_isl =
887 isl_union_set_union (domain_isl, isl_union_set_from_set (bb_domain_s));
888 }
889
890 return domain_isl;
891}
892
893/* Set the AST built options for loop unroll and jam. */
894
895static __isl_give isl_union_map *
896generate_luj_options (scop_p scop)
897{
898 isl_union_set *domain_isl;
899 isl_union_map *options_isl_ss;
900 isl_union_map *options_isl =
901 isl_union_map_empty (isl_set_get_space (scop->context));
902 int dim = get_max_schedule_dimensions (scop) - 1;
903 int dim1 = dim - PARAM_VALUE (PARAM_LOOP_UNROLL_JAM_DEPTH);
904
905 if (!flag_loop_unroll_jam)
906 return options_isl;
907
908 domain_isl = generate_luj_sepclass (scop);
909
910 options_isl_ss = generate_luj_sepclass_opt (scop, domain_isl, dim1, 0);
911 options_isl = isl_union_map_union (options_isl, options_isl_ss);
912
913 return options_isl;
914}
915
f6cc3103
RG
916/* Generates a schedule, which specifies an order used to
917 visit elements in a domain. */
918
e4a452b2 919static __isl_give isl_union_map *
f6cc3103
RG
920generate_isl_schedule (scop_p scop)
921{
fb3764d1 922 int nb_schedule_dims = get_max_schedule_dimensions (scop);
f6cc3103
RG
923 int i;
924 poly_bb_p pbb;
925 isl_union_map *schedule_isl =
926 isl_union_map_empty (isl_set_get_space (scop->context));
927
928 FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
929 {
930 /* Dead code elimination: when the domain of a PBB is empty,
931 don't generate code for the PBB. */
932 if (isl_set_is_empty (pbb->domain))
933 continue;
934
935 isl_map *bb_schedule = isl_map_copy (pbb->transformed);
936 bb_schedule = isl_map_intersect_domain (bb_schedule,
937 isl_set_copy (pbb->domain));
fb3764d1 938 bb_schedule = extend_schedule (bb_schedule, nb_schedule_dims);
f6cc3103
RG
939 schedule_isl =
940 isl_union_map_union (schedule_isl,
941 isl_union_map_from_map (bb_schedule));
942 }
943 return schedule_isl;
944}
945
574921c2
RG
946/* This method is executed before the construction of a for node. */
947static __isl_give isl_id *
948ast_build_before_for (__isl_keep isl_ast_build *build, void *user)
949{
950 isl_union_map *dependences = (isl_union_map *) user;
951 ast_build_info *for_info = XNEW (struct ast_build_info);
952 isl_union_map *schedule = isl_ast_build_get_schedule (build);
953 isl_space *schedule_space = isl_ast_build_get_schedule_space (build);
954 int dimension = isl_space_dim (schedule_space, isl_dim_out);
955 for_info->is_parallelizable =
956 !carries_deps (schedule, dependences, dimension);
957 isl_union_map_free (schedule);
958 isl_space_free (schedule_space);
959 isl_id *id = isl_id_alloc (isl_ast_build_get_ctx (build), "", for_info);
960 return id;
961}
962
6a12a004 963/* Set the separate option for all dimensions.
20d3465b
MN
964 This helps to reduce control overhead.
965 Set the options for unroll and jam. */
6a12a004
RG
966
967static __isl_give isl_ast_build *
968set_options (__isl_take isl_ast_build *control,
20d3465b
MN
969 __isl_keep isl_union_map *schedule,
970 __isl_take isl_union_map *opt_luj)
6a12a004
RG
971{
972 isl_ctx *ctx = isl_union_map_get_ctx (schedule);
973 isl_space *range_space = isl_space_set_alloc (ctx, 0, 1);
974 range_space =
975 isl_space_set_tuple_name (range_space, isl_dim_set, "separate");
976 isl_union_set *range =
977 isl_union_set_from_set (isl_set_universe (range_space));
978 isl_union_set *domain = isl_union_map_range (isl_union_map_copy (schedule));
979 domain = isl_union_set_universe (domain);
980 isl_union_map *options = isl_union_map_from_domain_and_range (domain, range);
20d3465b
MN
981
982 options = isl_union_map_union (options, opt_luj);
983
6a12a004
RG
984 return isl_ast_build_set_options (control, options);
985}
986
e4a452b2 987static __isl_give isl_ast_node *
a78cfa7f 988scop_to_isl_ast (scop_p scop, ivs_params &ip)
f6cc3103 989{
a78cfa7f
RG
990 /* Generate loop upper bounds that consist of the current loop iterator,
991 an operator (< or <=) and an expression not involving the iterator.
992 If this option is not set, then the current loop iterator may appear several
993 times in the upper bound. See the isl manual for more details. */
994 isl_options_set_ast_build_atomic_upper_bound (scop->ctx, true);
995
996 add_parameters_to_ivs_params (scop, ip);
20d3465b
MN
997
998 isl_union_map *options_luj = generate_luj_options (scop);
999
f6cc3103
RG
1000 isl_union_map *schedule_isl = generate_isl_schedule (scop);
1001 isl_ast_build *context_isl = generate_isl_context (scop);
20d3465b
MN
1002
1003 context_isl = set_options (context_isl, schedule_isl, options_luj);
1004
574921c2
RG
1005 isl_union_map *dependences = NULL;
1006 if (flag_loop_parallelize_all)
1007 {
1008 dependences = scop_get_dependences (scop);
1009 context_isl =
1010 isl_ast_build_set_before_each_for (context_isl, ast_build_before_for,
1011 dependences);
1012 }
f6cc3103
RG
1013 isl_ast_node *ast_isl = isl_ast_build_ast_from_schedule (context_isl,
1014 schedule_isl);
574921c2
RG
1015 if(dependences)
1016 isl_union_map_free (dependences);
f6cc3103
RG
1017 isl_ast_build_free (context_isl);
1018 return ast_isl;
1019}
1020
1021/* GIMPLE Loop Generator: generates loops from STMT in GIMPLE form for
1022 the given SCOP. Return true if code generation succeeded.
1023
1024 FIXME: This is not yet a full implementation of the code generator
a78cfa7f 1025 with ISL ASTs. Generation of GIMPLE code has to be completed. */
f6cc3103
RG
1026
1027bool
1028graphite_regenerate_ast_isl (scop_p scop)
1029{
a78cfa7f
RG
1030 loop_p context_loop;
1031 sese region = SCOP_REGION (scop);
1032 ifsese if_region = NULL;
1033 isl_ast_node *root_node;
1034 ivs_params ip;
1035
f6cc3103
RG
1036 timevar_push (TV_GRAPHITE_CODE_GEN);
1037 graphite_regenerate_error = false;
a78cfa7f
RG
1038 root_node = scop_to_isl_ast (scop, ip);
1039
f6cc3103
RG
1040 if (dump_file && (dump_flags & TDF_DETAILS))
1041 {
1042 fprintf (dump_file, "\nISL AST generated by ISL: \n");
1043 print_isl_ast_node (dump_file, root_node, scop->ctx);
a78cfa7f 1044 fprintf (dump_file, "\n");
f6cc3103 1045 }
a78cfa7f
RG
1046
1047 recompute_all_dominators ();
1048 graphite_verify ();
1049
1050 if_region = move_sese_in_condition (region);
1051 sese_insert_phis_for_liveouts (region,
1052 if_region->region->exit->src,
1053 if_region->false_region->exit,
1054 if_region->true_region->exit);
1055 recompute_all_dominators ();
1056 graphite_verify ();
1057
1058 context_loop = SESE_ENTRY (region)->src->loop_father;
1059
1060 translate_isl_ast (context_loop, root_node, if_region->true_region->entry,
1061 ip);
d819fedb
RB
1062
1063 mark_virtual_operands_for_renaming (cfun);
1064 update_ssa (TODO_update_ssa);
1065
a78cfa7f
RG
1066 graphite_verify ();
1067 scev_reset ();
1068 recompute_all_dominators ();
1069 graphite_verify ();
1070
1071 if (graphite_regenerate_error)
1072 set_ifsese_condition (if_region, integer_zero_node);
1073
1074 free (if_region->true_region);
1075 free (if_region->region);
1076 free (if_region);
1077
1078 ivs_params_clear (ip);
f6cc3103
RG
1079 isl_ast_node_free (root_node);
1080 timevar_pop (TV_GRAPHITE_CODE_GEN);
574921c2
RG
1081
1082 if (dump_file && (dump_flags & TDF_DETAILS))
1083 {
1084 loop_p loop;
1085 int num_no_dependency = 0;
1086
1087 FOR_EACH_LOOP (loop, 0)
1088 if (loop->can_be_parallel)
1089 num_no_dependency++;
1090
1091 fprintf (dump_file, "\n%d loops carried no dependency.\n",
1092 num_no_dependency);
1093 }
1094
f6cc3103
RG
1095 return !graphite_regenerate_error;
1096}
7d1ceb93 1097#endif