]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/graphite-sese-to-poly.c
Makefile.in: Remove the compilation of graphite-clast-to-gimple.o.
[thirdparty/gcc.git] / gcc / graphite-sese-to-poly.c
1 /* Conversion of SESE regions to Polyhedra.
2 Copyright (C) 2009-2014 Free Software Foundation, Inc.
3 Contributed by Sebastian Pop <sebastian.pop@amd.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 #include "config.h"
22
23 #ifdef HAVE_isl
24 #include <isl/set.h>
25 #include <isl/map.h>
26 #include <isl/union_map.h>
27 #include <isl/constraint.h>
28 #include <isl/aff.h>
29 #include <isl/val.h>
30 /* For C++ linkage of C functions.
31 Missing from isl/val_gmp.h in isl 0.12 versions.
32 Appearing in isl/val_gmp.h in isl 0.13.
33 To be removed when passing to isl 0.13. */
34 #if defined(__cplusplus)
35 extern "C" {
36 #endif
37 #include <isl/val_gmp.h>
38 #if defined(__cplusplus)
39 }
40 #endif
41 #endif
42
43 #include "system.h"
44 #include "coretypes.h"
45 #include "tree.h"
46 #include "predict.h"
47 #include "vec.h"
48 #include "hashtab.h"
49 #include "hash-set.h"
50 #include "machmode.h"
51 #include "tm.h"
52 #include "hard-reg-set.h"
53 #include "input.h"
54 #include "function.h"
55 #include "dominance.h"
56 #include "cfg.h"
57 #include "basic-block.h"
58 #include "tree-ssa-alias.h"
59 #include "internal-fn.h"
60 #include "gimple-expr.h"
61 #include "is-a.h"
62 #include "gimple.h"
63 #include "gimple-iterator.h"
64 #include "gimplify.h"
65 #include "gimplify-me.h"
66 #include "gimple-ssa.h"
67 #include "tree-cfg.h"
68 #include "tree-phinodes.h"
69 #include "ssa-iterators.h"
70 #include "stringpool.h"
71 #include "tree-ssanames.h"
72 #include "tree-ssa-loop-manip.h"
73 #include "tree-ssa-loop-niter.h"
74 #include "tree-ssa-loop.h"
75 #include "tree-into-ssa.h"
76 #include "tree-pass.h"
77 #include "cfgloop.h"
78 #include "tree-chrec.h"
79 #include "tree-data-ref.h"
80 #include "tree-scalar-evolution.h"
81 #include "domwalk.h"
82 #include "sese.h"
83 #include "tree-ssa-propagate.h"
84
85 #ifdef HAVE_isl
86 #include "expr.h"
87 #include "graphite-poly.h"
88 #include "graphite-sese-to-poly.h"
89
90
91 /* Assigns to RES the value of the INTEGER_CST T. */
92
93 static inline void
94 tree_int_to_gmp (tree t, mpz_t res)
95 {
96 wi::to_mpz (t, res, TYPE_SIGN (TREE_TYPE (t)));
97 }
98
99 /* Returns the index of the PHI argument defined in the outermost
100 loop. */
101
102 static size_t
103 phi_arg_in_outermost_loop (gimple phi)
104 {
105 loop_p loop = gimple_bb (phi)->loop_father;
106 size_t i, res = 0;
107
108 for (i = 0; i < gimple_phi_num_args (phi); i++)
109 if (!flow_bb_inside_loop_p (loop, gimple_phi_arg_edge (phi, i)->src))
110 {
111 loop = gimple_phi_arg_edge (phi, i)->src->loop_father;
112 res = i;
113 }
114
115 return res;
116 }
117
118 /* Removes a simple copy phi node "RES = phi (INIT, RES)" at position
119 PSI by inserting on the loop ENTRY edge assignment "RES = INIT". */
120
121 static void
122 remove_simple_copy_phi (gimple_stmt_iterator *psi)
123 {
124 gimple phi = gsi_stmt (*psi);
125 tree res = gimple_phi_result (phi);
126 size_t entry = phi_arg_in_outermost_loop (phi);
127 tree init = gimple_phi_arg_def (phi, entry);
128 gimple stmt = gimple_build_assign (res, init);
129 edge e = gimple_phi_arg_edge (phi, entry);
130
131 remove_phi_node (psi, false);
132 gsi_insert_on_edge_immediate (e, stmt);
133 }
134
135 /* Removes an invariant phi node at position PSI by inserting on the
136 loop ENTRY edge the assignment RES = INIT. */
137
138 static void
139 remove_invariant_phi (sese region, gimple_stmt_iterator *psi)
140 {
141 gimple phi = gsi_stmt (*psi);
142 loop_p loop = loop_containing_stmt (phi);
143 tree res = gimple_phi_result (phi);
144 tree scev = scalar_evolution_in_region (region, loop, res);
145 size_t entry = phi_arg_in_outermost_loop (phi);
146 edge e = gimple_phi_arg_edge (phi, entry);
147 tree var;
148 gimple stmt;
149 gimple_seq stmts = NULL;
150
151 if (tree_contains_chrecs (scev, NULL))
152 scev = gimple_phi_arg_def (phi, entry);
153
154 var = force_gimple_operand (scev, &stmts, true, NULL_TREE);
155 stmt = gimple_build_assign (res, var);
156 remove_phi_node (psi, false);
157
158 gimple_seq_add_stmt (&stmts, stmt);
159 gsi_insert_seq_on_edge (e, stmts);
160 gsi_commit_edge_inserts ();
161 SSA_NAME_DEF_STMT (res) = stmt;
162 }
163
164 /* Returns true when the phi node at PSI is of the form "a = phi (a, x)". */
165
166 static inline bool
167 simple_copy_phi_p (gimple phi)
168 {
169 tree res;
170
171 if (gimple_phi_num_args (phi) != 2)
172 return false;
173
174 res = gimple_phi_result (phi);
175 return (res == gimple_phi_arg_def (phi, 0)
176 || res == gimple_phi_arg_def (phi, 1));
177 }
178
179 /* Returns true when the phi node at position PSI is a reduction phi
180 node in REGION. Otherwise moves the pointer PSI to the next phi to
181 be considered. */
182
183 static bool
184 reduction_phi_p (sese region, gimple_stmt_iterator *psi)
185 {
186 loop_p loop;
187 gimple phi = gsi_stmt (*psi);
188 tree res = gimple_phi_result (phi);
189
190 loop = loop_containing_stmt (phi);
191
192 if (simple_copy_phi_p (phi))
193 {
194 /* PRE introduces phi nodes like these, for an example,
195 see id-5.f in the fortran graphite testsuite:
196
197 # prephitmp.85_265 = PHI <prephitmp.85_258(33), prephitmp.85_265(18)>
198 */
199 remove_simple_copy_phi (psi);
200 return false;
201 }
202
203 if (scev_analyzable_p (res, region))
204 {
205 tree scev = scalar_evolution_in_region (region, loop, res);
206
207 if (evolution_function_is_invariant_p (scev, loop->num))
208 remove_invariant_phi (region, psi);
209 else
210 gsi_next (psi);
211
212 return false;
213 }
214
215 /* All the other cases are considered reductions. */
216 return true;
217 }
218
219 /* Store the GRAPHITE representation of BB. */
220
221 static gimple_bb_p
222 new_gimple_bb (basic_block bb, vec<data_reference_p> drs)
223 {
224 struct gimple_bb *gbb;
225
226 gbb = XNEW (struct gimple_bb);
227 bb->aux = gbb;
228 GBB_BB (gbb) = bb;
229 GBB_DATA_REFS (gbb) = drs;
230 GBB_CONDITIONS (gbb).create (0);
231 GBB_CONDITION_CASES (gbb).create (0);
232
233 return gbb;
234 }
235
236 static void
237 free_data_refs_aux (vec<data_reference_p> datarefs)
238 {
239 unsigned int i;
240 struct data_reference *dr;
241
242 FOR_EACH_VEC_ELT (datarefs, i, dr)
243 if (dr->aux)
244 {
245 base_alias_pair *bap = (base_alias_pair *)(dr->aux);
246
247 free (bap->alias_set);
248
249 free (bap);
250 dr->aux = NULL;
251 }
252 }
253 /* Frees GBB. */
254
255 static void
256 free_gimple_bb (struct gimple_bb *gbb)
257 {
258 free_data_refs_aux (GBB_DATA_REFS (gbb));
259 free_data_refs (GBB_DATA_REFS (gbb));
260
261 GBB_CONDITIONS (gbb).release ();
262 GBB_CONDITION_CASES (gbb).release ();
263 GBB_BB (gbb)->aux = 0;
264 XDELETE (gbb);
265 }
266
267 /* Deletes all gimple bbs in SCOP. */
268
269 static void
270 remove_gbbs_in_scop (scop_p scop)
271 {
272 int i;
273 poly_bb_p pbb;
274
275 FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
276 free_gimple_bb (PBB_BLACK_BOX (pbb));
277 }
278
279 /* Deletes all scops in SCOPS. */
280
281 void
282 free_scops (vec<scop_p> scops)
283 {
284 int i;
285 scop_p scop;
286
287 FOR_EACH_VEC_ELT (scops, i, scop)
288 {
289 remove_gbbs_in_scop (scop);
290 free_sese (SCOP_REGION (scop));
291 free_scop (scop);
292 }
293
294 scops.release ();
295 }
296
297 /* Same as outermost_loop_in_sese, returns the outermost loop
298 containing BB in REGION, but makes sure that the returned loop
299 belongs to the REGION, and so this returns the first loop in the
300 REGION when the loop containing BB does not belong to REGION. */
301
302 static loop_p
303 outermost_loop_in_sese_1 (sese region, basic_block bb)
304 {
305 loop_p nest = outermost_loop_in_sese (region, bb);
306
307 if (loop_in_sese_p (nest, region))
308 return nest;
309
310 /* When the basic block BB does not belong to a loop in the region,
311 return the first loop in the region. */
312 nest = nest->inner;
313 while (nest)
314 if (loop_in_sese_p (nest, region))
315 break;
316 else
317 nest = nest->next;
318
319 gcc_assert (nest);
320 return nest;
321 }
322
323 /* Generates a polyhedral black box only if the bb contains interesting
324 information. */
325
326 static gimple_bb_p
327 try_generate_gimple_bb (scop_p scop, basic_block bb)
328 {
329 vec<data_reference_p> drs;
330 drs.create (5);
331 sese region = SCOP_REGION (scop);
332 loop_p nest = outermost_loop_in_sese_1 (region, bb);
333 gimple_stmt_iterator gsi;
334
335 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
336 {
337 gimple stmt = gsi_stmt (gsi);
338 loop_p loop;
339
340 if (is_gimple_debug (stmt))
341 continue;
342
343 loop = loop_containing_stmt (stmt);
344 if (!loop_in_sese_p (loop, region))
345 loop = nest;
346
347 graphite_find_data_references_in_stmt (nest, loop, stmt, &drs);
348 }
349
350 return new_gimple_bb (bb, drs);
351 }
352
353 /* Returns true if all predecessors of BB, that are not dominated by BB, are
354 marked in MAP. The predecessors dominated by BB are loop latches and will
355 be handled after BB. */
356
357 static bool
358 all_non_dominated_preds_marked_p (basic_block bb, sbitmap map)
359 {
360 edge e;
361 edge_iterator ei;
362
363 FOR_EACH_EDGE (e, ei, bb->preds)
364 if (!bitmap_bit_p (map, e->src->index)
365 && !dominated_by_p (CDI_DOMINATORS, e->src, bb))
366 return false;
367
368 return true;
369 }
370
371 /* Compare the depth of two basic_block's P1 and P2. */
372
373 static int
374 compare_bb_depths (const void *p1, const void *p2)
375 {
376 const_basic_block const bb1 = *(const_basic_block const*)p1;
377 const_basic_block const bb2 = *(const_basic_block const*)p2;
378 int d1 = loop_depth (bb1->loop_father);
379 int d2 = loop_depth (bb2->loop_father);
380
381 if (d1 < d2)
382 return 1;
383
384 if (d1 > d2)
385 return -1;
386
387 return 0;
388 }
389
390 /* Sort the basic blocks from DOM such that the first are the ones at
391 a deepest loop level. */
392
393 static void
394 graphite_sort_dominated_info (vec<basic_block> dom)
395 {
396 dom.qsort (compare_bb_depths);
397 }
398
399 /* Recursive helper function for build_scops_bbs. */
400
401 static void
402 build_scop_bbs_1 (scop_p scop, sbitmap visited, basic_block bb)
403 {
404 sese region = SCOP_REGION (scop);
405 vec<basic_block> dom;
406 poly_bb_p pbb;
407
408 if (bitmap_bit_p (visited, bb->index)
409 || !bb_in_sese_p (bb, region))
410 return;
411
412 pbb = new_poly_bb (scop, try_generate_gimple_bb (scop, bb));
413 SCOP_BBS (scop).safe_push (pbb);
414 bitmap_set_bit (visited, bb->index);
415
416 dom = get_dominated_by (CDI_DOMINATORS, bb);
417
418 if (!dom.exists ())
419 return;
420
421 graphite_sort_dominated_info (dom);
422
423 while (!dom.is_empty ())
424 {
425 int i;
426 basic_block dom_bb;
427
428 FOR_EACH_VEC_ELT (dom, i, dom_bb)
429 if (all_non_dominated_preds_marked_p (dom_bb, visited))
430 {
431 build_scop_bbs_1 (scop, visited, dom_bb);
432 dom.unordered_remove (i);
433 break;
434 }
435 }
436
437 dom.release ();
438 }
439
440 /* Gather the basic blocks belonging to the SCOP. */
441
442 static void
443 build_scop_bbs (scop_p scop)
444 {
445 sbitmap visited = sbitmap_alloc (last_basic_block_for_fn (cfun));
446 sese region = SCOP_REGION (scop);
447
448 bitmap_clear (visited);
449 build_scop_bbs_1 (scop, visited, SESE_ENTRY_BB (region));
450 sbitmap_free (visited);
451 }
452
453 /* Return an ISL identifier for the polyhedral basic block PBB. */
454
455 static isl_id *
456 isl_id_for_pbb (scop_p s, poly_bb_p pbb)
457 {
458 char name[50];
459 snprintf (name, sizeof (name), "S_%d", pbb_index (pbb));
460 return isl_id_alloc (s->ctx, name, pbb);
461 }
462
463 /* Converts the STATIC_SCHEDULE of PBB into a scattering polyhedron.
464 We generate SCATTERING_DIMENSIONS scattering dimensions.
465
466 CLooG 0.15.0 and previous versions require, that all
467 scattering functions of one CloogProgram have the same number of
468 scattering dimensions, therefore we allow to specify it. This
469 should be removed in future versions of CLooG.
470
471 The scattering polyhedron consists of these dimensions: scattering,
472 loop_iterators, parameters.
473
474 Example:
475
476 | scattering_dimensions = 5
477 | used_scattering_dimensions = 3
478 | nb_iterators = 1
479 | scop_nb_params = 2
480 |
481 | Schedule:
482 | i
483 | 4 5
484 |
485 | Scattering polyhedron:
486 |
487 | scattering: {s1, s2, s3, s4, s5}
488 | loop_iterators: {i}
489 | parameters: {p1, p2}
490 |
491 | s1 s2 s3 s4 s5 i p1 p2 1
492 | 1 0 0 0 0 0 0 0 -4 = 0
493 | 0 1 0 0 0 -1 0 0 0 = 0
494 | 0 0 1 0 0 0 0 0 -5 = 0 */
495
496 static void
497 build_pbb_scattering_polyhedrons (isl_aff *static_sched,
498 poly_bb_p pbb, int scattering_dimensions)
499 {
500 int i;
501 int nb_iterators = pbb_dim_iter_domain (pbb);
502 int used_scattering_dimensions = nb_iterators * 2 + 1;
503 isl_val *val;
504 isl_space *dc, *dm;
505
506 gcc_assert (scattering_dimensions >= used_scattering_dimensions);
507
508 dc = isl_set_get_space (pbb->domain);
509 dm = isl_space_add_dims (isl_space_from_domain (dc),
510 isl_dim_out, scattering_dimensions);
511 pbb->schedule = isl_map_universe (dm);
512
513 for (i = 0; i < scattering_dimensions; i++)
514 {
515 /* Textual order inside this loop. */
516 if ((i % 2) == 0)
517 {
518 isl_constraint *c = isl_equality_alloc
519 (isl_local_space_from_space (isl_map_get_space (pbb->schedule)));
520
521 val = isl_aff_get_coefficient_val (static_sched, isl_dim_in, i / 2);
522
523 val = isl_val_neg (val);
524 c = isl_constraint_set_constant_val (c, val);
525 c = isl_constraint_set_coefficient_si (c, isl_dim_out, i, 1);
526 pbb->schedule = isl_map_add_constraint (pbb->schedule, c);
527 }
528
529 /* Iterations of this loop. */
530 else /* if ((i % 2) == 1) */
531 {
532 int loop = (i - 1) / 2;
533 pbb->schedule = isl_map_equate (pbb->schedule, isl_dim_in, loop,
534 isl_dim_out, i);
535 }
536 }
537
538 pbb->transformed = isl_map_copy (pbb->schedule);
539 }
540
541 /* Build for BB the static schedule.
542
543 The static schedule is a Dewey numbering of the abstract syntax
544 tree: http://en.wikipedia.org/wiki/Dewey_Decimal_Classification
545
546 The following example informally defines the static schedule:
547
548 A
549 for (i: ...)
550 {
551 for (j: ...)
552 {
553 B
554 C
555 }
556
557 for (k: ...)
558 {
559 D
560 E
561 }
562 }
563 F
564
565 Static schedules for A to F:
566
567 DEPTH
568 0 1 2
569 A 0
570 B 1 0 0
571 C 1 0 1
572 D 1 1 0
573 E 1 1 1
574 F 2
575 */
576
577 static void
578 build_scop_scattering (scop_p scop)
579 {
580 int i;
581 poly_bb_p pbb;
582 gimple_bb_p previous_gbb = NULL;
583 isl_space *dc = isl_set_get_space (scop->context);
584 isl_aff *static_sched;
585
586 dc = isl_space_add_dims (dc, isl_dim_set, number_of_loops (cfun));
587 static_sched = isl_aff_zero_on_domain (isl_local_space_from_space (dc));
588
589 /* We have to start schedules at 0 on the first component and
590 because we cannot compare_prefix_loops against a previous loop,
591 prefix will be equal to zero, and that index will be
592 incremented before copying. */
593 static_sched = isl_aff_add_coefficient_si (static_sched, isl_dim_in, 0, -1);
594
595 FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
596 {
597 gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
598 int prefix;
599 int nb_scat_dims = pbb_dim_iter_domain (pbb) * 2 + 1;
600
601 if (previous_gbb)
602 prefix = nb_common_loops (SCOP_REGION (scop), previous_gbb, gbb);
603 else
604 prefix = 0;
605
606 previous_gbb = gbb;
607
608 static_sched = isl_aff_add_coefficient_si (static_sched, isl_dim_in,
609 prefix, 1);
610 build_pbb_scattering_polyhedrons (static_sched, pbb, nb_scat_dims);
611 }
612
613 isl_aff_free (static_sched);
614 }
615
616 static isl_pw_aff *extract_affine (scop_p, tree, __isl_take isl_space *space);
617
618 /* Extract an affine expression from the chain of recurrence E. */
619
620 static isl_pw_aff *
621 extract_affine_chrec (scop_p s, tree e, __isl_take isl_space *space)
622 {
623 isl_pw_aff *lhs = extract_affine (s, CHREC_LEFT (e), isl_space_copy (space));
624 isl_pw_aff *rhs = extract_affine (s, CHREC_RIGHT (e), isl_space_copy (space));
625 isl_local_space *ls = isl_local_space_from_space (space);
626 unsigned pos = sese_loop_depth ((sese) s->region, get_chrec_loop (e)) - 1;
627 isl_aff *loop = isl_aff_set_coefficient_si
628 (isl_aff_zero_on_domain (ls), isl_dim_in, pos, 1);
629 isl_pw_aff *l = isl_pw_aff_from_aff (loop);
630
631 /* Before multiplying, make sure that the result is affine. */
632 gcc_assert (isl_pw_aff_is_cst (rhs)
633 || isl_pw_aff_is_cst (l));
634
635 return isl_pw_aff_add (lhs, isl_pw_aff_mul (rhs, l));
636 }
637
638 /* Extract an affine expression from the mult_expr E. */
639
640 static isl_pw_aff *
641 extract_affine_mul (scop_p s, tree e, __isl_take isl_space *space)
642 {
643 isl_pw_aff *lhs = extract_affine (s, TREE_OPERAND (e, 0),
644 isl_space_copy (space));
645 isl_pw_aff *rhs = extract_affine (s, TREE_OPERAND (e, 1), space);
646
647 if (!isl_pw_aff_is_cst (lhs)
648 && !isl_pw_aff_is_cst (rhs))
649 {
650 isl_pw_aff_free (lhs);
651 isl_pw_aff_free (rhs);
652 return NULL;
653 }
654
655 return isl_pw_aff_mul (lhs, rhs);
656 }
657
658 /* Return an ISL identifier from the name of the ssa_name E. */
659
660 static isl_id *
661 isl_id_for_ssa_name (scop_p s, tree e)
662 {
663 const char *name = get_name (e);
664 isl_id *id;
665
666 if (name)
667 id = isl_id_alloc (s->ctx, name, e);
668 else
669 {
670 char name1[50];
671 snprintf (name1, sizeof (name1), "P_%d", SSA_NAME_VERSION (e));
672 id = isl_id_alloc (s->ctx, name1, e);
673 }
674
675 return id;
676 }
677
678 /* Return an ISL identifier for the data reference DR. */
679
680 static isl_id *
681 isl_id_for_dr (scop_p s, data_reference_p dr ATTRIBUTE_UNUSED)
682 {
683 /* Data references all get the same isl_id. They need to be comparable
684 and are distinguished through the first dimension, which contains the
685 alias set number. */
686 return isl_id_alloc (s->ctx, "", 0);
687 }
688
689 /* Extract an affine expression from the ssa_name E. */
690
691 static isl_pw_aff *
692 extract_affine_name (scop_p s, tree e, __isl_take isl_space *space)
693 {
694 isl_aff *aff;
695 isl_set *dom;
696 isl_id *id;
697 int dimension;
698
699 id = isl_id_for_ssa_name (s, e);
700 dimension = isl_space_find_dim_by_id (space, isl_dim_param, id);
701 isl_id_free (id);
702 dom = isl_set_universe (isl_space_copy (space));
703 aff = isl_aff_zero_on_domain (isl_local_space_from_space (space));
704 aff = isl_aff_add_coefficient_si (aff, isl_dim_param, dimension, 1);
705 return isl_pw_aff_alloc (dom, aff);
706 }
707
708 /* Extract an affine expression from the gmp constant G. */
709
710 static isl_pw_aff *
711 extract_affine_gmp (mpz_t g, __isl_take isl_space *space)
712 {
713 isl_local_space *ls = isl_local_space_from_space (isl_space_copy (space));
714 isl_aff *aff = isl_aff_zero_on_domain (ls);
715 isl_set *dom = isl_set_universe (space);
716 isl_val *v;
717 isl_ctx *ct;
718
719 ct = isl_aff_get_ctx (aff);
720 v = isl_val_int_from_gmp (ct, g);
721 aff = isl_aff_add_constant_val (aff, v);
722
723 return isl_pw_aff_alloc (dom, aff);
724 }
725
726 /* Extract an affine expression from the integer_cst E. */
727
728 static isl_pw_aff *
729 extract_affine_int (tree e, __isl_take isl_space *space)
730 {
731 isl_pw_aff *res;
732 mpz_t g;
733
734 mpz_init (g);
735 tree_int_to_gmp (e, g);
736 res = extract_affine_gmp (g, space);
737 mpz_clear (g);
738
739 return res;
740 }
741
742 /* Compute pwaff mod 2^width. */
743
744 extern isl_ctx *the_isl_ctx;
745
746 static isl_pw_aff *
747 wrap (isl_pw_aff *pwaff, unsigned width)
748 {
749 isl_val *mod;
750
751 mod = isl_val_int_from_ui(the_isl_ctx, width);
752 mod = isl_val_2exp (mod);
753 pwaff = isl_pw_aff_mod_val (pwaff, mod);
754
755 return pwaff;
756 }
757
758 /* When parameter NAME is in REGION, returns its index in SESE_PARAMS.
759 Otherwise returns -1. */
760
761 static inline int
762 parameter_index_in_region_1 (tree name, sese region)
763 {
764 int i;
765 tree p;
766
767 gcc_assert (TREE_CODE (name) == SSA_NAME);
768
769 FOR_EACH_VEC_ELT (SESE_PARAMS (region), i, p)
770 if (p == name)
771 return i;
772
773 return -1;
774 }
775
776 /* When the parameter NAME is in REGION, returns its index in
777 SESE_PARAMS. Otherwise this function inserts NAME in SESE_PARAMS
778 and returns the index of NAME. */
779
780 static int
781 parameter_index_in_region (tree name, sese region)
782 {
783 int i;
784
785 gcc_assert (TREE_CODE (name) == SSA_NAME);
786
787 i = parameter_index_in_region_1 (name, region);
788 if (i != -1)
789 return i;
790
791 gcc_assert (SESE_ADD_PARAMS (region));
792
793 i = SESE_PARAMS (region).length ();
794 SESE_PARAMS (region).safe_push (name);
795 return i;
796 }
797
798 /* Extract an affine expression from the tree E in the scop S. */
799
800 static isl_pw_aff *
801 extract_affine (scop_p s, tree e, __isl_take isl_space *space)
802 {
803 isl_pw_aff *lhs, *rhs, *res;
804 tree type;
805
806 if (e == chrec_dont_know) {
807 isl_space_free (space);
808 return NULL;
809 }
810
811 switch (TREE_CODE (e))
812 {
813 case POLYNOMIAL_CHREC:
814 res = extract_affine_chrec (s, e, space);
815 break;
816
817 case MULT_EXPR:
818 res = extract_affine_mul (s, e, space);
819 break;
820
821 case PLUS_EXPR:
822 case POINTER_PLUS_EXPR:
823 lhs = extract_affine (s, TREE_OPERAND (e, 0), isl_space_copy (space));
824 rhs = extract_affine (s, TREE_OPERAND (e, 1), space);
825 res = isl_pw_aff_add (lhs, rhs);
826 break;
827
828 case MINUS_EXPR:
829 lhs = extract_affine (s, TREE_OPERAND (e, 0), isl_space_copy (space));
830 rhs = extract_affine (s, TREE_OPERAND (e, 1), space);
831 res = isl_pw_aff_sub (lhs, rhs);
832 break;
833
834 case NEGATE_EXPR:
835 case BIT_NOT_EXPR:
836 lhs = extract_affine (s, TREE_OPERAND (e, 0), isl_space_copy (space));
837 rhs = extract_affine (s, integer_minus_one_node, space);
838 res = isl_pw_aff_mul (lhs, rhs);
839 break;
840
841 case SSA_NAME:
842 gcc_assert (-1 != parameter_index_in_region_1 (e, SCOP_REGION (s)));
843 res = extract_affine_name (s, e, space);
844 break;
845
846 case INTEGER_CST:
847 res = extract_affine_int (e, space);
848 /* No need to wrap a single integer. */
849 return res;
850
851 CASE_CONVERT:
852 case NON_LVALUE_EXPR:
853 res = extract_affine (s, TREE_OPERAND (e, 0), space);
854 break;
855
856 default:
857 gcc_unreachable ();
858 break;
859 }
860
861 type = TREE_TYPE (e);
862 if (TYPE_UNSIGNED (type))
863 res = wrap (res, TYPE_PRECISION (type));
864
865 return res;
866 }
867
868 /* In the context of sese S, scan the expression E and translate it to
869 a linear expression C. When parsing a symbolic multiplication, K
870 represents the constant multiplier of an expression containing
871 parameters. */
872
873 static void
874 scan_tree_for_params (sese s, tree e)
875 {
876 if (e == chrec_dont_know)
877 return;
878
879 switch (TREE_CODE (e))
880 {
881 case POLYNOMIAL_CHREC:
882 scan_tree_for_params (s, CHREC_LEFT (e));
883 break;
884
885 case MULT_EXPR:
886 if (chrec_contains_symbols (TREE_OPERAND (e, 0)))
887 scan_tree_for_params (s, TREE_OPERAND (e, 0));
888 else
889 scan_tree_for_params (s, TREE_OPERAND (e, 1));
890 break;
891
892 case PLUS_EXPR:
893 case POINTER_PLUS_EXPR:
894 case MINUS_EXPR:
895 scan_tree_for_params (s, TREE_OPERAND (e, 0));
896 scan_tree_for_params (s, TREE_OPERAND (e, 1));
897 break;
898
899 case NEGATE_EXPR:
900 case BIT_NOT_EXPR:
901 CASE_CONVERT:
902 case NON_LVALUE_EXPR:
903 scan_tree_for_params (s, TREE_OPERAND (e, 0));
904 break;
905
906 case SSA_NAME:
907 parameter_index_in_region (e, s);
908 break;
909
910 case INTEGER_CST:
911 case ADDR_EXPR:
912 break;
913
914 default:
915 gcc_unreachable ();
916 break;
917 }
918 }
919
920 /* Find parameters with respect to REGION in BB. We are looking in memory
921 access functions, conditions and loop bounds. */
922
923 static void
924 find_params_in_bb (sese region, gimple_bb_p gbb)
925 {
926 int i;
927 unsigned j;
928 data_reference_p dr;
929 gimple stmt;
930 loop_p loop = GBB_BB (gbb)->loop_father;
931
932 /* Find parameters in the access functions of data references. */
933 FOR_EACH_VEC_ELT (GBB_DATA_REFS (gbb), i, dr)
934 for (j = 0; j < DR_NUM_DIMENSIONS (dr); j++)
935 scan_tree_for_params (region, DR_ACCESS_FN (dr, j));
936
937 /* Find parameters in conditional statements. */
938 FOR_EACH_VEC_ELT (GBB_CONDITIONS (gbb), i, stmt)
939 {
940 tree lhs = scalar_evolution_in_region (region, loop,
941 gimple_cond_lhs (stmt));
942 tree rhs = scalar_evolution_in_region (region, loop,
943 gimple_cond_rhs (stmt));
944
945 scan_tree_for_params (region, lhs);
946 scan_tree_for_params (region, rhs);
947 }
948 }
949
950 /* Record the parameters used in the SCOP. A variable is a parameter
951 in a scop if it does not vary during the execution of that scop. */
952
953 static void
954 find_scop_parameters (scop_p scop)
955 {
956 poly_bb_p pbb;
957 unsigned i;
958 sese region = SCOP_REGION (scop);
959 struct loop *loop;
960 int nbp;
961
962 /* Find the parameters used in the loop bounds. */
963 FOR_EACH_VEC_ELT (SESE_LOOP_NEST (region), i, loop)
964 {
965 tree nb_iters = number_of_latch_executions (loop);
966
967 if (!chrec_contains_symbols (nb_iters))
968 continue;
969
970 nb_iters = scalar_evolution_in_region (region, loop, nb_iters);
971 scan_tree_for_params (region, nb_iters);
972 }
973
974 /* Find the parameters used in data accesses. */
975 FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
976 find_params_in_bb (region, PBB_BLACK_BOX (pbb));
977
978 nbp = sese_nb_params (region);
979 scop_set_nb_params (scop, nbp);
980 SESE_ADD_PARAMS (region) = false;
981
982 {
983 tree e;
984 isl_space *space = isl_space_set_alloc (scop->ctx, nbp, 0);
985
986 FOR_EACH_VEC_ELT (SESE_PARAMS (region), i, e)
987 space = isl_space_set_dim_id (space, isl_dim_param, i,
988 isl_id_for_ssa_name (scop, e));
989
990 scop->context = isl_set_universe (space);
991 }
992 }
993
994 /* Builds the constraint polyhedra for LOOP in SCOP. OUTER_PH gives
995 the constraints for the surrounding loops. */
996
997 static void
998 build_loop_iteration_domains (scop_p scop, struct loop *loop,
999 int nb,
1000 isl_set *outer, isl_set **doms)
1001 {
1002 tree nb_iters = number_of_latch_executions (loop);
1003 sese region = SCOP_REGION (scop);
1004
1005 isl_set *inner = isl_set_copy (outer);
1006 isl_space *space;
1007 isl_constraint *c;
1008 int pos = isl_set_dim (outer, isl_dim_set);
1009 isl_val *v;
1010 mpz_t g;
1011
1012 mpz_init (g);
1013
1014 inner = isl_set_add_dims (inner, isl_dim_set, 1);
1015 space = isl_set_get_space (inner);
1016
1017 /* 0 <= loop_i */
1018 c = isl_inequality_alloc
1019 (isl_local_space_from_space (isl_space_copy (space)));
1020 c = isl_constraint_set_coefficient_si (c, isl_dim_set, pos, 1);
1021 inner = isl_set_add_constraint (inner, c);
1022
1023 /* loop_i <= cst_nb_iters */
1024 if (TREE_CODE (nb_iters) == INTEGER_CST)
1025 {
1026 c = isl_inequality_alloc
1027 (isl_local_space_from_space (isl_space_copy (space)));
1028 c = isl_constraint_set_coefficient_si (c, isl_dim_set, pos, -1);
1029 tree_int_to_gmp (nb_iters, g);
1030 v = isl_val_int_from_gmp (the_isl_ctx, g);
1031 c = isl_constraint_set_constant_val (c, v);
1032 inner = isl_set_add_constraint (inner, c);
1033 }
1034
1035 /* loop_i <= expr_nb_iters */
1036 else if (!chrec_contains_undetermined (nb_iters))
1037 {
1038 widest_int nit;
1039 isl_pw_aff *aff;
1040 isl_set *valid;
1041 isl_local_space *ls;
1042 isl_aff *al;
1043 isl_set *le;
1044
1045 nb_iters = scalar_evolution_in_region (region, loop, nb_iters);
1046
1047 aff = extract_affine (scop, nb_iters, isl_set_get_space (inner));
1048 valid = isl_pw_aff_nonneg_set (isl_pw_aff_copy (aff));
1049 valid = isl_set_project_out (valid, isl_dim_set, 0,
1050 isl_set_dim (valid, isl_dim_set));
1051 scop->context = isl_set_intersect (scop->context, valid);
1052
1053 ls = isl_local_space_from_space (isl_space_copy (space));
1054 al = isl_aff_set_coefficient_si (isl_aff_zero_on_domain (ls),
1055 isl_dim_in, pos, 1);
1056 le = isl_pw_aff_le_set (isl_pw_aff_from_aff (al),
1057 isl_pw_aff_copy (aff));
1058 inner = isl_set_intersect (inner, le);
1059
1060 if (max_stmt_executions (loop, &nit))
1061 {
1062 /* Insert in the context the constraints from the
1063 estimation of the number of iterations NIT and the
1064 symbolic number of iterations (involving parameter
1065 names) NB_ITERS. First, build the affine expression
1066 "NIT - NB_ITERS" and then say that it is positive,
1067 i.e., NIT approximates NB_ITERS: "NIT >= NB_ITERS". */
1068 isl_pw_aff *approx;
1069 mpz_t g;
1070 isl_set *x;
1071 isl_constraint *c;
1072
1073 mpz_init (g);
1074 wi::to_mpz (nit, g, SIGNED);
1075 mpz_sub_ui (g, g, 1);
1076 approx = extract_affine_gmp (g, isl_set_get_space (inner));
1077 x = isl_pw_aff_ge_set (approx, aff);
1078 x = isl_set_project_out (x, isl_dim_set, 0,
1079 isl_set_dim (x, isl_dim_set));
1080 scop->context = isl_set_intersect (scop->context, x);
1081
1082 c = isl_inequality_alloc
1083 (isl_local_space_from_space (isl_space_copy (space)));
1084 c = isl_constraint_set_coefficient_si (c, isl_dim_set, pos, -1);
1085 v = isl_val_int_from_gmp (the_isl_ctx, g);
1086 mpz_clear (g);
1087 c = isl_constraint_set_constant_val (c, v);
1088 inner = isl_set_add_constraint (inner, c);
1089 }
1090 else
1091 isl_pw_aff_free (aff);
1092 }
1093 else
1094 gcc_unreachable ();
1095
1096 if (loop->inner && loop_in_sese_p (loop->inner, region))
1097 build_loop_iteration_domains (scop, loop->inner, nb + 1,
1098 isl_set_copy (inner), doms);
1099
1100 if (nb != 0
1101 && loop->next
1102 && loop_in_sese_p (loop->next, region))
1103 build_loop_iteration_domains (scop, loop->next, nb,
1104 isl_set_copy (outer), doms);
1105
1106 doms[loop->num] = inner;
1107
1108 isl_set_free (outer);
1109 isl_space_free (space);
1110 mpz_clear (g);
1111 }
1112
1113 /* Returns a linear expression for tree T evaluated in PBB. */
1114
1115 static isl_pw_aff *
1116 create_pw_aff_from_tree (poly_bb_p pbb, tree t)
1117 {
1118 scop_p scop = PBB_SCOP (pbb);
1119
1120 t = scalar_evolution_in_region (SCOP_REGION (scop), pbb_loop (pbb), t);
1121 gcc_assert (!automatically_generated_chrec_p (t));
1122
1123 return extract_affine (scop, t, isl_set_get_space (pbb->domain));
1124 }
1125
1126 /* Add conditional statement STMT to pbb. CODE is used as the comparison
1127 operator. This allows us to invert the condition or to handle
1128 inequalities. */
1129
1130 static void
1131 add_condition_to_pbb (poly_bb_p pbb, gimple stmt, enum tree_code code)
1132 {
1133 isl_pw_aff *lhs = create_pw_aff_from_tree (pbb, gimple_cond_lhs (stmt));
1134 isl_pw_aff *rhs = create_pw_aff_from_tree (pbb, gimple_cond_rhs (stmt));
1135 isl_set *cond;
1136
1137 switch (code)
1138 {
1139 case LT_EXPR:
1140 cond = isl_pw_aff_lt_set (lhs, rhs);
1141 break;
1142
1143 case GT_EXPR:
1144 cond = isl_pw_aff_gt_set (lhs, rhs);
1145 break;
1146
1147 case LE_EXPR:
1148 cond = isl_pw_aff_le_set (lhs, rhs);
1149 break;
1150
1151 case GE_EXPR:
1152 cond = isl_pw_aff_ge_set (lhs, rhs);
1153 break;
1154
1155 case EQ_EXPR:
1156 cond = isl_pw_aff_eq_set (lhs, rhs);
1157 break;
1158
1159 case NE_EXPR:
1160 cond = isl_pw_aff_ne_set (lhs, rhs);
1161 break;
1162
1163 default:
1164 isl_pw_aff_free (lhs);
1165 isl_pw_aff_free (rhs);
1166 return;
1167 }
1168
1169 cond = isl_set_coalesce (cond);
1170 cond = isl_set_set_tuple_id (cond, isl_set_get_tuple_id (pbb->domain));
1171 pbb->domain = isl_set_intersect (pbb->domain, cond);
1172 }
1173
1174 /* Add conditions to the domain of PBB. */
1175
1176 static void
1177 add_conditions_to_domain (poly_bb_p pbb)
1178 {
1179 unsigned int i;
1180 gimple stmt;
1181 gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
1182
1183 if (GBB_CONDITIONS (gbb).is_empty ())
1184 return;
1185
1186 FOR_EACH_VEC_ELT (GBB_CONDITIONS (gbb), i, stmt)
1187 switch (gimple_code (stmt))
1188 {
1189 case GIMPLE_COND:
1190 {
1191 enum tree_code code = gimple_cond_code (stmt);
1192
1193 /* The conditions for ELSE-branches are inverted. */
1194 if (!GBB_CONDITION_CASES (gbb)[i])
1195 code = invert_tree_comparison (code, false);
1196
1197 add_condition_to_pbb (pbb, stmt, code);
1198 break;
1199 }
1200
1201 case GIMPLE_SWITCH:
1202 /* Switch statements are not supported right now - fall through. */
1203
1204 default:
1205 gcc_unreachable ();
1206 break;
1207 }
1208 }
1209
1210 /* Traverses all the GBBs of the SCOP and add their constraints to the
1211 iteration domains. */
1212
1213 static void
1214 add_conditions_to_constraints (scop_p scop)
1215 {
1216 int i;
1217 poly_bb_p pbb;
1218
1219 FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
1220 add_conditions_to_domain (pbb);
1221 }
1222
1223 /* Returns a COND_EXPR statement when BB has a single predecessor, the
1224 edge between BB and its predecessor is not a loop exit edge, and
1225 the last statement of the single predecessor is a COND_EXPR. */
1226
1227 static gimple
1228 single_pred_cond_non_loop_exit (basic_block bb)
1229 {
1230 if (single_pred_p (bb))
1231 {
1232 edge e = single_pred_edge (bb);
1233 basic_block pred = e->src;
1234 gimple stmt;
1235
1236 if (loop_depth (pred->loop_father) > loop_depth (bb->loop_father))
1237 return NULL;
1238
1239 stmt = last_stmt (pred);
1240
1241 if (stmt && gimple_code (stmt) == GIMPLE_COND)
1242 return stmt;
1243 }
1244
1245 return NULL;
1246 }
1247
1248 class sese_dom_walker : public dom_walker
1249 {
1250 public:
1251 sese_dom_walker (cdi_direction, sese);
1252
1253 virtual void before_dom_children (basic_block);
1254 virtual void after_dom_children (basic_block);
1255
1256 private:
1257 auto_vec<gimple, 3> m_conditions, m_cases;
1258 sese m_region;
1259 };
1260
1261 sese_dom_walker::sese_dom_walker (cdi_direction direction, sese region)
1262 : dom_walker (direction), m_region (region)
1263 {
1264 }
1265
1266 /* Call-back for dom_walk executed before visiting the dominated
1267 blocks. */
1268
1269 void
1270 sese_dom_walker::before_dom_children (basic_block bb)
1271 {
1272 gimple_bb_p gbb;
1273 gimple stmt;
1274
1275 if (!bb_in_sese_p (bb, m_region))
1276 return;
1277
1278 stmt = single_pred_cond_non_loop_exit (bb);
1279
1280 if (stmt)
1281 {
1282 edge e = single_pred_edge (bb);
1283
1284 m_conditions.safe_push (stmt);
1285
1286 if (e->flags & EDGE_TRUE_VALUE)
1287 m_cases.safe_push (stmt);
1288 else
1289 m_cases.safe_push (NULL);
1290 }
1291
1292 gbb = gbb_from_bb (bb);
1293
1294 if (gbb)
1295 {
1296 GBB_CONDITIONS (gbb) = m_conditions.copy ();
1297 GBB_CONDITION_CASES (gbb) = m_cases.copy ();
1298 }
1299 }
1300
1301 /* Call-back for dom_walk executed after visiting the dominated
1302 blocks. */
1303
1304 void
1305 sese_dom_walker::after_dom_children (basic_block bb)
1306 {
1307 if (!bb_in_sese_p (bb, m_region))
1308 return;
1309
1310 if (single_pred_cond_non_loop_exit (bb))
1311 {
1312 m_conditions.pop ();
1313 m_cases.pop ();
1314 }
1315 }
1316
1317 /* Add constraints on the possible values of parameter P from the type
1318 of P. */
1319
1320 static void
1321 add_param_constraints (scop_p scop, graphite_dim_t p)
1322 {
1323 tree parameter = SESE_PARAMS (SCOP_REGION (scop))[p];
1324 tree type = TREE_TYPE (parameter);
1325 tree lb = NULL_TREE;
1326 tree ub = NULL_TREE;
1327
1328 if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type))
1329 lb = lower_bound_in_type (type, type);
1330 else
1331 lb = TYPE_MIN_VALUE (type);
1332
1333 if (POINTER_TYPE_P (type) || !TYPE_MAX_VALUE (type))
1334 ub = upper_bound_in_type (type, type);
1335 else
1336 ub = TYPE_MAX_VALUE (type);
1337
1338 if (lb)
1339 {
1340 isl_space *space = isl_set_get_space (scop->context);
1341 isl_constraint *c;
1342 mpz_t g;
1343 isl_val *v;
1344
1345 c = isl_inequality_alloc (isl_local_space_from_space (space));
1346 mpz_init (g);
1347 tree_int_to_gmp (lb, g);
1348 v = isl_val_int_from_gmp (the_isl_ctx, g);
1349 v = isl_val_neg (v);
1350 mpz_clear (g);
1351 c = isl_constraint_set_constant_val (c, v);
1352 c = isl_constraint_set_coefficient_si (c, isl_dim_param, p, 1);
1353
1354 scop->context = isl_set_add_constraint (scop->context, c);
1355 }
1356
1357 if (ub)
1358 {
1359 isl_space *space = isl_set_get_space (scop->context);
1360 isl_constraint *c;
1361 mpz_t g;
1362 isl_val *v;
1363
1364 c = isl_inequality_alloc (isl_local_space_from_space (space));
1365
1366 mpz_init (g);
1367 tree_int_to_gmp (ub, g);
1368 v = isl_val_int_from_gmp (the_isl_ctx, g);
1369 mpz_clear (g);
1370 c = isl_constraint_set_constant_val (c, v);
1371 c = isl_constraint_set_coefficient_si (c, isl_dim_param, p, -1);
1372
1373 scop->context = isl_set_add_constraint (scop->context, c);
1374 }
1375 }
1376
1377 /* Build the context of the SCOP. The context usually contains extra
1378 constraints that are added to the iteration domains that constrain
1379 some parameters. */
1380
1381 static void
1382 build_scop_context (scop_p scop)
1383 {
1384 graphite_dim_t p, n = scop_nb_params (scop);
1385
1386 for (p = 0; p < n; p++)
1387 add_param_constraints (scop, p);
1388 }
1389
1390 /* Build the iteration domains: the loops belonging to the current
1391 SCOP, and that vary for the execution of the current basic block.
1392 Returns false if there is no loop in SCOP. */
1393
1394 static void
1395 build_scop_iteration_domain (scop_p scop)
1396 {
1397 struct loop *loop;
1398 sese region = SCOP_REGION (scop);
1399 int i;
1400 poly_bb_p pbb;
1401 int nb_loops = number_of_loops (cfun);
1402 isl_set **doms = XCNEWVEC (isl_set *, nb_loops);
1403
1404 FOR_EACH_VEC_ELT (SESE_LOOP_NEST (region), i, loop)
1405 if (!loop_in_sese_p (loop_outer (loop), region))
1406 build_loop_iteration_domains (scop, loop, 0,
1407 isl_set_copy (scop->context), doms);
1408
1409 FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
1410 {
1411 loop = pbb_loop (pbb);
1412
1413 if (doms[loop->num])
1414 pbb->domain = isl_set_copy (doms[loop->num]);
1415 else
1416 pbb->domain = isl_set_copy (scop->context);
1417
1418 pbb->domain = isl_set_set_tuple_id (pbb->domain,
1419 isl_id_for_pbb (scop, pbb));
1420 }
1421
1422 for (i = 0; i < nb_loops; i++)
1423 if (doms[i])
1424 isl_set_free (doms[i]);
1425
1426 free (doms);
1427 }
1428
1429 /* Add a constrain to the ACCESSES polyhedron for the alias set of
1430 data reference DR. ACCESSP_NB_DIMS is the dimension of the
1431 ACCESSES polyhedron, DOM_NB_DIMS is the dimension of the iteration
1432 domain. */
1433
1434 static isl_map *
1435 pdr_add_alias_set (isl_map *acc, data_reference_p dr)
1436 {
1437 isl_constraint *c;
1438 int alias_set_num = 0;
1439 base_alias_pair *bap = (base_alias_pair *)(dr->aux);
1440
1441 if (bap && bap->alias_set)
1442 alias_set_num = *(bap->alias_set);
1443
1444 c = isl_equality_alloc
1445 (isl_local_space_from_space (isl_map_get_space (acc)));
1446 c = isl_constraint_set_constant_si (c, -alias_set_num);
1447 c = isl_constraint_set_coefficient_si (c, isl_dim_out, 0, 1);
1448
1449 return isl_map_add_constraint (acc, c);
1450 }
1451
1452 /* Assign the affine expression INDEX to the output dimension POS of
1453 MAP and return the result. */
1454
1455 static isl_map *
1456 set_index (isl_map *map, int pos, isl_pw_aff *index)
1457 {
1458 isl_map *index_map;
1459 int len = isl_map_dim (map, isl_dim_out);
1460 isl_id *id;
1461
1462 index_map = isl_map_from_pw_aff (index);
1463 index_map = isl_map_insert_dims (index_map, isl_dim_out, 0, pos);
1464 index_map = isl_map_add_dims (index_map, isl_dim_out, len - pos - 1);
1465
1466 id = isl_map_get_tuple_id (map, isl_dim_out);
1467 index_map = isl_map_set_tuple_id (index_map, isl_dim_out, id);
1468 id = isl_map_get_tuple_id (map, isl_dim_in);
1469 index_map = isl_map_set_tuple_id (index_map, isl_dim_in, id);
1470
1471 return isl_map_intersect (map, index_map);
1472 }
1473
1474 /* Add to ACCESSES polyhedron equalities defining the access functions
1475 to the memory. ACCESSP_NB_DIMS is the dimension of the ACCESSES
1476 polyhedron, DOM_NB_DIMS is the dimension of the iteration domain.
1477 PBB is the poly_bb_p that contains the data reference DR. */
1478
1479 static isl_map *
1480 pdr_add_memory_accesses (isl_map *acc, data_reference_p dr, poly_bb_p pbb)
1481 {
1482 int i, nb_subscripts = DR_NUM_DIMENSIONS (dr);
1483 scop_p scop = PBB_SCOP (pbb);
1484
1485 for (i = 0; i < nb_subscripts; i++)
1486 {
1487 isl_pw_aff *aff;
1488 tree afn = DR_ACCESS_FN (dr, nb_subscripts - 1 - i);
1489
1490 aff = extract_affine (scop, afn,
1491 isl_space_domain (isl_map_get_space (acc)));
1492 acc = set_index (acc, i + 1, aff);
1493 }
1494
1495 return acc;
1496 }
1497
1498 /* Add constrains representing the size of the accessed data to the
1499 ACCESSES polyhedron. ACCESSP_NB_DIMS is the dimension of the
1500 ACCESSES polyhedron, DOM_NB_DIMS is the dimension of the iteration
1501 domain. */
1502
1503 static isl_set *
1504 pdr_add_data_dimensions (isl_set *extent, scop_p scop, data_reference_p dr)
1505 {
1506 tree ref = DR_REF (dr);
1507 int i, nb_subscripts = DR_NUM_DIMENSIONS (dr);
1508
1509 for (i = nb_subscripts - 1; i >= 0; i--, ref = TREE_OPERAND (ref, 0))
1510 {
1511 tree low, high;
1512
1513 if (TREE_CODE (ref) != ARRAY_REF)
1514 break;
1515
1516 low = array_ref_low_bound (ref);
1517 high = array_ref_up_bound (ref);
1518
1519 /* XXX The PPL code dealt separately with
1520 subscript - low >= 0 and high - subscript >= 0 in case one of
1521 the two bounds isn't known. Do the same here? */
1522
1523 if (tree_fits_shwi_p (low)
1524 && high
1525 && tree_fits_shwi_p (high)
1526 /* 1-element arrays at end of structures may extend over
1527 their declared size. */
1528 && !(array_at_struct_end_p (ref)
1529 && operand_equal_p (low, high, 0)))
1530 {
1531 isl_id *id;
1532 isl_aff *aff;
1533 isl_set *univ, *lbs, *ubs;
1534 isl_pw_aff *index;
1535 isl_space *space;
1536 isl_set *valid;
1537 isl_pw_aff *lb = extract_affine_int (low, isl_set_get_space (extent));
1538 isl_pw_aff *ub = extract_affine_int (high, isl_set_get_space (extent));
1539
1540 /* high >= 0 */
1541 valid = isl_pw_aff_nonneg_set (isl_pw_aff_copy (ub));
1542 valid = isl_set_project_out (valid, isl_dim_set, 0,
1543 isl_set_dim (valid, isl_dim_set));
1544 scop->context = isl_set_intersect (scop->context, valid);
1545
1546 space = isl_set_get_space (extent);
1547 aff = isl_aff_zero_on_domain (isl_local_space_from_space (space));
1548 aff = isl_aff_add_coefficient_si (aff, isl_dim_in, i + 1, 1);
1549 univ = isl_set_universe (isl_space_domain (isl_aff_get_space (aff)));
1550 index = isl_pw_aff_alloc (univ, aff);
1551
1552 id = isl_set_get_tuple_id (extent);
1553 lb = isl_pw_aff_set_tuple_id (lb, isl_dim_in, isl_id_copy (id));
1554 ub = isl_pw_aff_set_tuple_id (ub, isl_dim_in, id);
1555
1556 /* low <= sub_i <= high */
1557 lbs = isl_pw_aff_ge_set (isl_pw_aff_copy (index), lb);
1558 ubs = isl_pw_aff_le_set (index, ub);
1559 extent = isl_set_intersect (extent, lbs);
1560 extent = isl_set_intersect (extent, ubs);
1561 }
1562 }
1563
1564 return extent;
1565 }
1566
1567 /* Build data accesses for DR in PBB. */
1568
1569 static void
1570 build_poly_dr (data_reference_p dr, poly_bb_p pbb)
1571 {
1572 int dr_base_object_set;
1573 isl_map *acc;
1574 isl_set *extent;
1575 scop_p scop = PBB_SCOP (pbb);
1576
1577 {
1578 isl_space *dc = isl_set_get_space (pbb->domain);
1579 int nb_out = 1 + DR_NUM_DIMENSIONS (dr);
1580 isl_space *space = isl_space_add_dims (isl_space_from_domain (dc),
1581 isl_dim_out, nb_out);
1582
1583 acc = isl_map_universe (space);
1584 acc = isl_map_set_tuple_id (acc, isl_dim_out, isl_id_for_dr (scop, dr));
1585 }
1586
1587 acc = pdr_add_alias_set (acc, dr);
1588 acc = pdr_add_memory_accesses (acc, dr, pbb);
1589
1590 {
1591 isl_id *id = isl_id_for_dr (scop, dr);
1592 int nb = 1 + DR_NUM_DIMENSIONS (dr);
1593 isl_space *space = isl_space_set_alloc (scop->ctx, 0, nb);
1594 int alias_set_num = 0;
1595 base_alias_pair *bap = (base_alias_pair *)(dr->aux);
1596
1597 if (bap && bap->alias_set)
1598 alias_set_num = *(bap->alias_set);
1599
1600 space = isl_space_set_tuple_id (space, isl_dim_set, id);
1601 extent = isl_set_nat_universe (space);
1602 extent = isl_set_fix_si (extent, isl_dim_set, 0, alias_set_num);
1603 extent = pdr_add_data_dimensions (extent, scop, dr);
1604 }
1605
1606 gcc_assert (dr->aux);
1607 dr_base_object_set = ((base_alias_pair *)(dr->aux))->base_obj_set;
1608
1609 new_poly_dr (pbb, dr_base_object_set,
1610 DR_IS_READ (dr) ? PDR_READ : PDR_WRITE,
1611 dr, DR_NUM_DIMENSIONS (dr), acc, extent);
1612 }
1613
1614 /* Write to FILE the alias graph of data references in DIMACS format. */
1615
1616 static inline bool
1617 write_alias_graph_to_ascii_dimacs (FILE *file, char *comment,
1618 vec<data_reference_p> drs)
1619 {
1620 int num_vertex = drs.length ();
1621 int edge_num = 0;
1622 data_reference_p dr1, dr2;
1623 int i, j;
1624
1625 if (num_vertex == 0)
1626 return true;
1627
1628 FOR_EACH_VEC_ELT (drs, i, dr1)
1629 for (j = i + 1; drs.iterate (j, &dr2); j++)
1630 if (dr_may_alias_p (dr1, dr2, true))
1631 edge_num++;
1632
1633 fprintf (file, "$\n");
1634
1635 if (comment)
1636 fprintf (file, "c %s\n", comment);
1637
1638 fprintf (file, "p edge %d %d\n", num_vertex, edge_num);
1639
1640 FOR_EACH_VEC_ELT (drs, i, dr1)
1641 for (j = i + 1; drs.iterate (j, &dr2); j++)
1642 if (dr_may_alias_p (dr1, dr2, true))
1643 fprintf (file, "e %d %d\n", i + 1, j + 1);
1644
1645 return true;
1646 }
1647
1648 /* Write to FILE the alias graph of data references in DOT format. */
1649
1650 static inline bool
1651 write_alias_graph_to_ascii_dot (FILE *file, char *comment,
1652 vec<data_reference_p> drs)
1653 {
1654 int num_vertex = drs.length ();
1655 data_reference_p dr1, dr2;
1656 int i, j;
1657
1658 if (num_vertex == 0)
1659 return true;
1660
1661 fprintf (file, "$\n");
1662
1663 if (comment)
1664 fprintf (file, "c %s\n", comment);
1665
1666 /* First print all the vertices. */
1667 FOR_EACH_VEC_ELT (drs, i, dr1)
1668 fprintf (file, "n%d;\n", i);
1669
1670 FOR_EACH_VEC_ELT (drs, i, dr1)
1671 for (j = i + 1; drs.iterate (j, &dr2); j++)
1672 if (dr_may_alias_p (dr1, dr2, true))
1673 fprintf (file, "n%d n%d\n", i, j);
1674
1675 return true;
1676 }
1677
1678 /* Write to FILE the alias graph of data references in ECC format. */
1679
1680 static inline bool
1681 write_alias_graph_to_ascii_ecc (FILE *file, char *comment,
1682 vec<data_reference_p> drs)
1683 {
1684 int num_vertex = drs.length ();
1685 data_reference_p dr1, dr2;
1686 int i, j;
1687
1688 if (num_vertex == 0)
1689 return true;
1690
1691 fprintf (file, "$\n");
1692
1693 if (comment)
1694 fprintf (file, "c %s\n", comment);
1695
1696 FOR_EACH_VEC_ELT (drs, i, dr1)
1697 for (j = i + 1; drs.iterate (j, &dr2); j++)
1698 if (dr_may_alias_p (dr1, dr2, true))
1699 fprintf (file, "%d %d\n", i, j);
1700
1701 return true;
1702 }
1703
1704 /* Check if DR1 and DR2 are in the same object set. */
1705
1706 static bool
1707 dr_same_base_object_p (const struct data_reference *dr1,
1708 const struct data_reference *dr2)
1709 {
1710 return operand_equal_p (DR_BASE_OBJECT (dr1), DR_BASE_OBJECT (dr2), 0);
1711 }
1712
1713 /* Uses DFS component number as representative of alias-sets. Also tests for
1714 optimality by verifying if every connected component is a clique. Returns
1715 true (1) if the above test is true, and false (0) otherwise. */
1716
1717 static int
1718 build_alias_set_optimal_p (vec<data_reference_p> drs)
1719 {
1720 int num_vertices = drs.length ();
1721 struct graph *g = new_graph (num_vertices);
1722 data_reference_p dr1, dr2;
1723 int i, j;
1724 int num_connected_components;
1725 int v_indx1, v_indx2, num_vertices_in_component;
1726 int *all_vertices;
1727 int *vertices;
1728 struct graph_edge *e;
1729 int this_component_is_clique;
1730 int all_components_are_cliques = 1;
1731
1732 FOR_EACH_VEC_ELT (drs, i, dr1)
1733 for (j = i+1; drs.iterate (j, &dr2); j++)
1734 if (dr_may_alias_p (dr1, dr2, true))
1735 {
1736 add_edge (g, i, j);
1737 add_edge (g, j, i);
1738 }
1739
1740 all_vertices = XNEWVEC (int, num_vertices);
1741 vertices = XNEWVEC (int, num_vertices);
1742 for (i = 0; i < num_vertices; i++)
1743 all_vertices[i] = i;
1744
1745 num_connected_components = graphds_dfs (g, all_vertices, num_vertices,
1746 NULL, true, NULL);
1747 for (i = 0; i < g->n_vertices; i++)
1748 {
1749 data_reference_p dr = drs[i];
1750 base_alias_pair *bap;
1751
1752 gcc_assert (dr->aux);
1753 bap = (base_alias_pair *)(dr->aux);
1754
1755 bap->alias_set = XNEW (int);
1756 *(bap->alias_set) = g->vertices[i].component + 1;
1757 }
1758
1759 /* Verify if the DFS numbering results in optimal solution. */
1760 for (i = 0; i < num_connected_components; i++)
1761 {
1762 num_vertices_in_component = 0;
1763 /* Get all vertices whose DFS component number is the same as i. */
1764 for (j = 0; j < num_vertices; j++)
1765 if (g->vertices[j].component == i)
1766 vertices[num_vertices_in_component++] = j;
1767
1768 /* Now test if the vertices in 'vertices' form a clique, by testing
1769 for edges among each pair. */
1770 this_component_is_clique = 1;
1771 for (v_indx1 = 0; v_indx1 < num_vertices_in_component; v_indx1++)
1772 {
1773 for (v_indx2 = v_indx1+1; v_indx2 < num_vertices_in_component; v_indx2++)
1774 {
1775 /* Check if the two vertices are connected by iterating
1776 through all the edges which have one of these are source. */
1777 e = g->vertices[vertices[v_indx2]].pred;
1778 while (e)
1779 {
1780 if (e->src == vertices[v_indx1])
1781 break;
1782 e = e->pred_next;
1783 }
1784 if (!e)
1785 {
1786 this_component_is_clique = 0;
1787 break;
1788 }
1789 }
1790 if (!this_component_is_clique)
1791 all_components_are_cliques = 0;
1792 }
1793 }
1794
1795 free (all_vertices);
1796 free (vertices);
1797 free_graph (g);
1798 return all_components_are_cliques;
1799 }
1800
1801 /* Group each data reference in DRS with its base object set num. */
1802
1803 static void
1804 build_base_obj_set_for_drs (vec<data_reference_p> drs)
1805 {
1806 int num_vertex = drs.length ();
1807 struct graph *g = new_graph (num_vertex);
1808 data_reference_p dr1, dr2;
1809 int i, j;
1810 int *queue;
1811
1812 FOR_EACH_VEC_ELT (drs, i, dr1)
1813 for (j = i + 1; drs.iterate (j, &dr2); j++)
1814 if (dr_same_base_object_p (dr1, dr2))
1815 {
1816 add_edge (g, i, j);
1817 add_edge (g, j, i);
1818 }
1819
1820 queue = XNEWVEC (int, num_vertex);
1821 for (i = 0; i < num_vertex; i++)
1822 queue[i] = i;
1823
1824 graphds_dfs (g, queue, num_vertex, NULL, true, NULL);
1825
1826 for (i = 0; i < g->n_vertices; i++)
1827 {
1828 data_reference_p dr = drs[i];
1829 base_alias_pair *bap;
1830
1831 gcc_assert (dr->aux);
1832 bap = (base_alias_pair *)(dr->aux);
1833
1834 bap->base_obj_set = g->vertices[i].component + 1;
1835 }
1836
1837 free (queue);
1838 free_graph (g);
1839 }
1840
1841 /* Build the data references for PBB. */
1842
1843 static void
1844 build_pbb_drs (poly_bb_p pbb)
1845 {
1846 int j;
1847 data_reference_p dr;
1848 vec<data_reference_p> gbb_drs = GBB_DATA_REFS (PBB_BLACK_BOX (pbb));
1849
1850 FOR_EACH_VEC_ELT (gbb_drs, j, dr)
1851 build_poly_dr (dr, pbb);
1852 }
1853
1854 /* Dump to file the alias graphs for the data references in DRS. */
1855
1856 static void
1857 dump_alias_graphs (vec<data_reference_p> drs)
1858 {
1859 char comment[100];
1860 FILE *file_dimacs, *file_ecc, *file_dot;
1861
1862 file_dimacs = fopen ("/tmp/dr_alias_graph_dimacs", "ab");
1863 if (file_dimacs)
1864 {
1865 snprintf (comment, sizeof (comment), "%s %s", main_input_filename,
1866 current_function_name ());
1867 write_alias_graph_to_ascii_dimacs (file_dimacs, comment, drs);
1868 fclose (file_dimacs);
1869 }
1870
1871 file_ecc = fopen ("/tmp/dr_alias_graph_ecc", "ab");
1872 if (file_ecc)
1873 {
1874 snprintf (comment, sizeof (comment), "%s %s", main_input_filename,
1875 current_function_name ());
1876 write_alias_graph_to_ascii_ecc (file_ecc, comment, drs);
1877 fclose (file_ecc);
1878 }
1879
1880 file_dot = fopen ("/tmp/dr_alias_graph_dot", "ab");
1881 if (file_dot)
1882 {
1883 snprintf (comment, sizeof (comment), "%s %s", main_input_filename,
1884 current_function_name ());
1885 write_alias_graph_to_ascii_dot (file_dot, comment, drs);
1886 fclose (file_dot);
1887 }
1888 }
1889
1890 /* Build data references in SCOP. */
1891
1892 static void
1893 build_scop_drs (scop_p scop)
1894 {
1895 int i, j;
1896 poly_bb_p pbb;
1897 data_reference_p dr;
1898 auto_vec<data_reference_p, 3> drs;
1899
1900 /* Remove all the PBBs that do not have data references: these basic
1901 blocks are not handled in the polyhedral representation. */
1902 for (i = 0; SCOP_BBS (scop).iterate (i, &pbb); i++)
1903 if (GBB_DATA_REFS (PBB_BLACK_BOX (pbb)).is_empty ())
1904 {
1905 free_gimple_bb (PBB_BLACK_BOX (pbb));
1906 free_poly_bb (pbb);
1907 SCOP_BBS (scop).ordered_remove (i);
1908 i--;
1909 }
1910
1911 FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
1912 for (j = 0; GBB_DATA_REFS (PBB_BLACK_BOX (pbb)).iterate (j, &dr); j++)
1913 drs.safe_push (dr);
1914
1915 FOR_EACH_VEC_ELT (drs, i, dr)
1916 dr->aux = XNEW (base_alias_pair);
1917
1918 if (!build_alias_set_optimal_p (drs))
1919 {
1920 /* TODO: Add support when building alias set is not optimal. */
1921 ;
1922 }
1923
1924 build_base_obj_set_for_drs (drs);
1925
1926 /* When debugging, enable the following code. This cannot be used
1927 in production compilers. */
1928 if (0)
1929 dump_alias_graphs (drs);
1930
1931 drs.release ();
1932
1933 FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
1934 build_pbb_drs (pbb);
1935 }
1936
1937 /* Return a gsi at the position of the phi node STMT. */
1938
1939 static gimple_stmt_iterator
1940 gsi_for_phi_node (gimple stmt)
1941 {
1942 gimple_stmt_iterator psi;
1943 basic_block bb = gimple_bb (stmt);
1944
1945 for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi))
1946 if (stmt == gsi_stmt (psi))
1947 return psi;
1948
1949 gcc_unreachable ();
1950 return psi;
1951 }
1952
1953 /* Analyze all the data references of STMTS and add them to the
1954 GBB_DATA_REFS vector of BB. */
1955
1956 static void
1957 analyze_drs_in_stmts (scop_p scop, basic_block bb, vec<gimple> stmts)
1958 {
1959 loop_p nest;
1960 gimple_bb_p gbb;
1961 gimple stmt;
1962 int i;
1963 sese region = SCOP_REGION (scop);
1964
1965 if (!bb_in_sese_p (bb, region))
1966 return;
1967
1968 nest = outermost_loop_in_sese_1 (region, bb);
1969 gbb = gbb_from_bb (bb);
1970
1971 FOR_EACH_VEC_ELT (stmts, i, stmt)
1972 {
1973 loop_p loop;
1974
1975 if (is_gimple_debug (stmt))
1976 continue;
1977
1978 loop = loop_containing_stmt (stmt);
1979 if (!loop_in_sese_p (loop, region))
1980 loop = nest;
1981
1982 graphite_find_data_references_in_stmt (nest, loop, stmt,
1983 &GBB_DATA_REFS (gbb));
1984 }
1985 }
1986
1987 /* Insert STMT at the end of the STMTS sequence and then insert the
1988 statements from STMTS at INSERT_GSI and call analyze_drs_in_stmts
1989 on STMTS. */
1990
1991 static void
1992 insert_stmts (scop_p scop, gimple stmt, gimple_seq stmts,
1993 gimple_stmt_iterator insert_gsi)
1994 {
1995 gimple_stmt_iterator gsi;
1996 auto_vec<gimple, 3> x;
1997
1998 gimple_seq_add_stmt (&stmts, stmt);
1999 for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
2000 x.safe_push (gsi_stmt (gsi));
2001
2002 gsi_insert_seq_before (&insert_gsi, stmts, GSI_SAME_STMT);
2003 analyze_drs_in_stmts (scop, gsi_bb (insert_gsi), x);
2004 }
2005
2006 /* Insert the assignment "RES := EXPR" just after AFTER_STMT. */
2007
2008 static void
2009 insert_out_of_ssa_copy (scop_p scop, tree res, tree expr, gimple after_stmt)
2010 {
2011 gimple_seq stmts;
2012 gimple_stmt_iterator gsi;
2013 tree var = force_gimple_operand (expr, &stmts, true, NULL_TREE);
2014 gimple stmt = gimple_build_assign (unshare_expr (res), var);
2015 auto_vec<gimple, 3> x;
2016
2017 gimple_seq_add_stmt (&stmts, stmt);
2018 for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
2019 x.safe_push (gsi_stmt (gsi));
2020
2021 if (gimple_code (after_stmt) == GIMPLE_PHI)
2022 {
2023 gsi = gsi_after_labels (gimple_bb (after_stmt));
2024 gsi_insert_seq_before (&gsi, stmts, GSI_NEW_STMT);
2025 }
2026 else
2027 {
2028 gsi = gsi_for_stmt (after_stmt);
2029 gsi_insert_seq_after (&gsi, stmts, GSI_NEW_STMT);
2030 }
2031
2032 analyze_drs_in_stmts (scop, gimple_bb (after_stmt), x);
2033 }
2034
2035 /* Creates a poly_bb_p for basic_block BB from the existing PBB. */
2036
2037 static void
2038 new_pbb_from_pbb (scop_p scop, poly_bb_p pbb, basic_block bb)
2039 {
2040 vec<data_reference_p> drs;
2041 drs.create (3);
2042 gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
2043 gimple_bb_p gbb1 = new_gimple_bb (bb, drs);
2044 poly_bb_p pbb1 = new_poly_bb (scop, gbb1);
2045 int index, n = SCOP_BBS (scop).length ();
2046
2047 /* The INDEX of PBB in SCOP_BBS. */
2048 for (index = 0; index < n; index++)
2049 if (SCOP_BBS (scop)[index] == pbb)
2050 break;
2051
2052 pbb1->domain = isl_set_copy (pbb->domain);
2053 pbb1->domain = isl_set_set_tuple_id (pbb1->domain,
2054 isl_id_for_pbb (scop, pbb1));
2055
2056 GBB_PBB (gbb1) = pbb1;
2057 GBB_CONDITIONS (gbb1) = GBB_CONDITIONS (gbb).copy ();
2058 GBB_CONDITION_CASES (gbb1) = GBB_CONDITION_CASES (gbb).copy ();
2059 SCOP_BBS (scop).safe_insert (index + 1, pbb1);
2060 }
2061
2062 /* Insert on edge E the assignment "RES := EXPR". */
2063
2064 static void
2065 insert_out_of_ssa_copy_on_edge (scop_p scop, edge e, tree res, tree expr)
2066 {
2067 gimple_stmt_iterator gsi;
2068 gimple_seq stmts = NULL;
2069 tree var = force_gimple_operand (expr, &stmts, true, NULL_TREE);
2070 gimple stmt = gimple_build_assign (unshare_expr (res), var);
2071 basic_block bb;
2072 auto_vec<gimple, 3> x;
2073
2074 gimple_seq_add_stmt (&stmts, stmt);
2075 for (gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
2076 x.safe_push (gsi_stmt (gsi));
2077
2078 gsi_insert_seq_on_edge (e, stmts);
2079 gsi_commit_edge_inserts ();
2080 bb = gimple_bb (stmt);
2081
2082 if (!bb_in_sese_p (bb, SCOP_REGION (scop)))
2083 return;
2084
2085 if (!gbb_from_bb (bb))
2086 new_pbb_from_pbb (scop, pbb_from_bb (e->src), bb);
2087
2088 analyze_drs_in_stmts (scop, bb, x);
2089 }
2090
2091 /* Creates a zero dimension array of the same type as VAR. */
2092
2093 static tree
2094 create_zero_dim_array (tree var, const char *base_name)
2095 {
2096 tree index_type = build_index_type (integer_zero_node);
2097 tree elt_type = TREE_TYPE (var);
2098 tree array_type = build_array_type (elt_type, index_type);
2099 tree base = create_tmp_var (array_type, base_name);
2100
2101 return build4 (ARRAY_REF, elt_type, base, integer_zero_node, NULL_TREE,
2102 NULL_TREE);
2103 }
2104
2105 /* Returns true when PHI is a loop close phi node. */
2106
2107 static bool
2108 scalar_close_phi_node_p (gimple phi)
2109 {
2110 if (gimple_code (phi) != GIMPLE_PHI
2111 || virtual_operand_p (gimple_phi_result (phi)))
2112 return false;
2113
2114 /* Note that loop close phi nodes should have a single argument
2115 because we translated the representation into a canonical form
2116 before Graphite: see canonicalize_loop_closed_ssa_form. */
2117 return (gimple_phi_num_args (phi) == 1);
2118 }
2119
2120 /* For a definition DEF in REGION, propagates the expression EXPR in
2121 all the uses of DEF outside REGION. */
2122
2123 static void
2124 propagate_expr_outside_region (tree def, tree expr, sese region)
2125 {
2126 imm_use_iterator imm_iter;
2127 gimple use_stmt;
2128 gimple_seq stmts;
2129 bool replaced_once = false;
2130
2131 gcc_assert (TREE_CODE (def) == SSA_NAME);
2132
2133 expr = force_gimple_operand (unshare_expr (expr), &stmts, true,
2134 NULL_TREE);
2135
2136 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, def)
2137 if (!is_gimple_debug (use_stmt)
2138 && !bb_in_sese_p (gimple_bb (use_stmt), region))
2139 {
2140 ssa_op_iter iter;
2141 use_operand_p use_p;
2142
2143 FOR_EACH_PHI_OR_STMT_USE (use_p, use_stmt, iter, SSA_OP_ALL_USES)
2144 if (operand_equal_p (def, USE_FROM_PTR (use_p), 0)
2145 && (replaced_once = true))
2146 replace_exp (use_p, expr);
2147
2148 update_stmt (use_stmt);
2149 }
2150
2151 if (replaced_once)
2152 {
2153 gsi_insert_seq_on_edge (SESE_ENTRY (region), stmts);
2154 gsi_commit_edge_inserts ();
2155 }
2156 }
2157
2158 /* Rewrite out of SSA the reduction phi node at PSI by creating a zero
2159 dimension array for it. */
2160
2161 static void
2162 rewrite_close_phi_out_of_ssa (scop_p scop, gimple_stmt_iterator *psi)
2163 {
2164 sese region = SCOP_REGION (scop);
2165 gimple phi = gsi_stmt (*psi);
2166 tree res = gimple_phi_result (phi);
2167 basic_block bb = gimple_bb (phi);
2168 gimple_stmt_iterator gsi = gsi_after_labels (bb);
2169 tree arg = gimple_phi_arg_def (phi, 0);
2170 gimple stmt;
2171
2172 /* Note that loop close phi nodes should have a single argument
2173 because we translated the representation into a canonical form
2174 before Graphite: see canonicalize_loop_closed_ssa_form. */
2175 gcc_assert (gimple_phi_num_args (phi) == 1);
2176
2177 /* The phi node can be a non close phi node, when its argument is
2178 invariant, or a default definition. */
2179 if (is_gimple_min_invariant (arg)
2180 || SSA_NAME_IS_DEFAULT_DEF (arg))
2181 {
2182 propagate_expr_outside_region (res, arg, region);
2183 gsi_next (psi);
2184 return;
2185 }
2186
2187 else if (gimple_bb (SSA_NAME_DEF_STMT (arg))->loop_father == bb->loop_father)
2188 {
2189 propagate_expr_outside_region (res, arg, region);
2190 stmt = gimple_build_assign (res, arg);
2191 remove_phi_node (psi, false);
2192 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
2193 return;
2194 }
2195
2196 /* If res is scev analyzable and is not a scalar value, it is safe
2197 to ignore the close phi node: it will be code generated in the
2198 out of Graphite pass. */
2199 else if (scev_analyzable_p (res, region))
2200 {
2201 loop_p loop = loop_containing_stmt (SSA_NAME_DEF_STMT (res));
2202 tree scev;
2203
2204 if (!loop_in_sese_p (loop, region))
2205 {
2206 loop = loop_containing_stmt (SSA_NAME_DEF_STMT (arg));
2207 scev = scalar_evolution_in_region (region, loop, arg);
2208 scev = compute_overall_effect_of_inner_loop (loop, scev);
2209 }
2210 else
2211 scev = scalar_evolution_in_region (region, loop, res);
2212
2213 if (tree_does_not_contain_chrecs (scev))
2214 propagate_expr_outside_region (res, scev, region);
2215
2216 gsi_next (psi);
2217 return;
2218 }
2219 else
2220 {
2221 tree zero_dim_array = create_zero_dim_array (res, "Close_Phi");
2222
2223 stmt = gimple_build_assign (res, unshare_expr (zero_dim_array));
2224
2225 if (TREE_CODE (arg) == SSA_NAME)
2226 insert_out_of_ssa_copy (scop, zero_dim_array, arg,
2227 SSA_NAME_DEF_STMT (arg));
2228 else
2229 insert_out_of_ssa_copy_on_edge (scop, single_pred_edge (bb),
2230 zero_dim_array, arg);
2231 }
2232
2233 remove_phi_node (psi, false);
2234 SSA_NAME_DEF_STMT (res) = stmt;
2235
2236 insert_stmts (scop, stmt, NULL, gsi_after_labels (bb));
2237 }
2238
2239 /* Rewrite out of SSA the reduction phi node at PSI by creating a zero
2240 dimension array for it. */
2241
2242 static void
2243 rewrite_phi_out_of_ssa (scop_p scop, gimple_stmt_iterator *psi)
2244 {
2245 size_t i;
2246 gimple phi = gsi_stmt (*psi);
2247 basic_block bb = gimple_bb (phi);
2248 tree res = gimple_phi_result (phi);
2249 tree zero_dim_array = create_zero_dim_array (res, "phi_out_of_ssa");
2250 gimple stmt;
2251
2252 for (i = 0; i < gimple_phi_num_args (phi); i++)
2253 {
2254 tree arg = gimple_phi_arg_def (phi, i);
2255 edge e = gimple_phi_arg_edge (phi, i);
2256
2257 /* Avoid the insertion of code in the loop latch to please the
2258 pattern matching of the vectorizer. */
2259 if (TREE_CODE (arg) == SSA_NAME
2260 && !SSA_NAME_IS_DEFAULT_DEF (arg)
2261 && e->src == bb->loop_father->latch)
2262 insert_out_of_ssa_copy (scop, zero_dim_array, arg,
2263 SSA_NAME_DEF_STMT (arg));
2264 else
2265 insert_out_of_ssa_copy_on_edge (scop, e, zero_dim_array, arg);
2266 }
2267
2268 stmt = gimple_build_assign (res, unshare_expr (zero_dim_array));
2269 remove_phi_node (psi, false);
2270 insert_stmts (scop, stmt, NULL, gsi_after_labels (bb));
2271 }
2272
2273 /* Rewrite the degenerate phi node at position PSI from the degenerate
2274 form "x = phi (y, y, ..., y)" to "x = y". */
2275
2276 static void
2277 rewrite_degenerate_phi (gimple_stmt_iterator *psi)
2278 {
2279 tree rhs;
2280 gimple stmt;
2281 gimple_stmt_iterator gsi;
2282 gimple phi = gsi_stmt (*psi);
2283 tree res = gimple_phi_result (phi);
2284 basic_block bb;
2285
2286 bb = gimple_bb (phi);
2287 rhs = degenerate_phi_result (phi);
2288 gcc_assert (rhs);
2289
2290 stmt = gimple_build_assign (res, rhs);
2291 remove_phi_node (psi, false);
2292
2293 gsi = gsi_after_labels (bb);
2294 gsi_insert_before (&gsi, stmt, GSI_NEW_STMT);
2295 }
2296
2297 /* Rewrite out of SSA all the reduction phi nodes of SCOP. */
2298
2299 static void
2300 rewrite_reductions_out_of_ssa (scop_p scop)
2301 {
2302 basic_block bb;
2303 gimple_stmt_iterator psi;
2304 sese region = SCOP_REGION (scop);
2305
2306 FOR_EACH_BB_FN (bb, cfun)
2307 if (bb_in_sese_p (bb, region))
2308 for (psi = gsi_start_phis (bb); !gsi_end_p (psi);)
2309 {
2310 gimple phi = gsi_stmt (psi);
2311
2312 if (virtual_operand_p (gimple_phi_result (phi)))
2313 {
2314 gsi_next (&psi);
2315 continue;
2316 }
2317
2318 if (gimple_phi_num_args (phi) > 1
2319 && degenerate_phi_result (phi))
2320 rewrite_degenerate_phi (&psi);
2321
2322 else if (scalar_close_phi_node_p (phi))
2323 rewrite_close_phi_out_of_ssa (scop, &psi);
2324
2325 else if (reduction_phi_p (region, &psi))
2326 rewrite_phi_out_of_ssa (scop, &psi);
2327 }
2328
2329 update_ssa (TODO_update_ssa);
2330 #ifdef ENABLE_CHECKING
2331 verify_loop_closed_ssa (true);
2332 #endif
2333 }
2334
2335 /* Rewrite the scalar dependence of DEF used in USE_STMT with a memory
2336 read from ZERO_DIM_ARRAY. */
2337
2338 static void
2339 rewrite_cross_bb_scalar_dependence (scop_p scop, tree zero_dim_array,
2340 tree def, gimple use_stmt)
2341 {
2342 gimple name_stmt;
2343 tree name;
2344 ssa_op_iter iter;
2345 use_operand_p use_p;
2346
2347 gcc_assert (gimple_code (use_stmt) != GIMPLE_PHI);
2348
2349 name = copy_ssa_name (def, NULL);
2350 name_stmt = gimple_build_assign (name, zero_dim_array);
2351
2352 gimple_assign_set_lhs (name_stmt, name);
2353 insert_stmts (scop, name_stmt, NULL, gsi_for_stmt (use_stmt));
2354
2355 FOR_EACH_SSA_USE_OPERAND (use_p, use_stmt, iter, SSA_OP_ALL_USES)
2356 if (operand_equal_p (def, USE_FROM_PTR (use_p), 0))
2357 replace_exp (use_p, name);
2358
2359 update_stmt (use_stmt);
2360 }
2361
2362 /* For every definition DEF in the SCOP that is used outside the scop,
2363 insert a closing-scop definition in the basic block just after this
2364 SCOP. */
2365
2366 static void
2367 handle_scalar_deps_crossing_scop_limits (scop_p scop, tree def, gimple stmt)
2368 {
2369 tree var = create_tmp_reg (TREE_TYPE (def), NULL);
2370 tree new_name = make_ssa_name (var, stmt);
2371 bool needs_copy = false;
2372 use_operand_p use_p;
2373 imm_use_iterator imm_iter;
2374 gimple use_stmt;
2375 sese region = SCOP_REGION (scop);
2376
2377 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, def)
2378 {
2379 if (!bb_in_sese_p (gimple_bb (use_stmt), region))
2380 {
2381 FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
2382 {
2383 SET_USE (use_p, new_name);
2384 }
2385 update_stmt (use_stmt);
2386 needs_copy = true;
2387 }
2388 }
2389
2390 /* Insert in the empty BB just after the scop a use of DEF such
2391 that the rewrite of cross_bb_scalar_dependences won't insert
2392 arrays everywhere else. */
2393 if (needs_copy)
2394 {
2395 gimple assign = gimple_build_assign (new_name, def);
2396 gimple_stmt_iterator psi = gsi_after_labels (SESE_EXIT (region)->dest);
2397
2398 update_stmt (assign);
2399 gsi_insert_before (&psi, assign, GSI_SAME_STMT);
2400 }
2401 }
2402
2403 /* Rewrite the scalar dependences crossing the boundary of the BB
2404 containing STMT with an array. Return true when something has been
2405 changed. */
2406
2407 static bool
2408 rewrite_cross_bb_scalar_deps (scop_p scop, gimple_stmt_iterator *gsi)
2409 {
2410 sese region = SCOP_REGION (scop);
2411 gimple stmt = gsi_stmt (*gsi);
2412 imm_use_iterator imm_iter;
2413 tree def;
2414 basic_block def_bb;
2415 tree zero_dim_array = NULL_TREE;
2416 gimple use_stmt;
2417 bool res = false;
2418
2419 switch (gimple_code (stmt))
2420 {
2421 case GIMPLE_ASSIGN:
2422 def = gimple_assign_lhs (stmt);
2423 break;
2424
2425 case GIMPLE_CALL:
2426 def = gimple_call_lhs (stmt);
2427 break;
2428
2429 default:
2430 return false;
2431 }
2432
2433 if (!def
2434 || !is_gimple_reg (def))
2435 return false;
2436
2437 if (scev_analyzable_p (def, region))
2438 {
2439 loop_p loop = loop_containing_stmt (SSA_NAME_DEF_STMT (def));
2440 tree scev = scalar_evolution_in_region (region, loop, def);
2441
2442 if (tree_contains_chrecs (scev, NULL))
2443 return false;
2444
2445 propagate_expr_outside_region (def, scev, region);
2446 return true;
2447 }
2448
2449 def_bb = gimple_bb (stmt);
2450
2451 handle_scalar_deps_crossing_scop_limits (scop, def, stmt);
2452
2453 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, def)
2454 if (gimple_code (use_stmt) == GIMPLE_PHI
2455 && (res = true))
2456 {
2457 gimple_stmt_iterator psi = gsi_for_stmt (use_stmt);
2458
2459 if (scalar_close_phi_node_p (gsi_stmt (psi)))
2460 rewrite_close_phi_out_of_ssa (scop, &psi);
2461 else
2462 rewrite_phi_out_of_ssa (scop, &psi);
2463 }
2464
2465 FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, def)
2466 if (gimple_code (use_stmt) != GIMPLE_PHI
2467 && def_bb != gimple_bb (use_stmt)
2468 && !is_gimple_debug (use_stmt)
2469 && (res = true))
2470 {
2471 if (!zero_dim_array)
2472 {
2473 zero_dim_array = create_zero_dim_array
2474 (def, "Cross_BB_scalar_dependence");
2475 insert_out_of_ssa_copy (scop, zero_dim_array, def,
2476 SSA_NAME_DEF_STMT (def));
2477 gsi_next (gsi);
2478 }
2479
2480 rewrite_cross_bb_scalar_dependence (scop, unshare_expr (zero_dim_array),
2481 def, use_stmt);
2482 }
2483
2484 return res;
2485 }
2486
2487 /* Rewrite out of SSA all the reduction phi nodes of SCOP. */
2488
2489 static void
2490 rewrite_cross_bb_scalar_deps_out_of_ssa (scop_p scop)
2491 {
2492 basic_block bb;
2493 gimple_stmt_iterator psi;
2494 sese region = SCOP_REGION (scop);
2495 bool changed = false;
2496
2497 /* Create an extra empty BB after the scop. */
2498 split_edge (SESE_EXIT (region));
2499
2500 FOR_EACH_BB_FN (bb, cfun)
2501 if (bb_in_sese_p (bb, region))
2502 for (psi = gsi_start_bb (bb); !gsi_end_p (psi); gsi_next (&psi))
2503 changed |= rewrite_cross_bb_scalar_deps (scop, &psi);
2504
2505 if (changed)
2506 {
2507 scev_reset_htab ();
2508 update_ssa (TODO_update_ssa);
2509 #ifdef ENABLE_CHECKING
2510 verify_loop_closed_ssa (true);
2511 #endif
2512 }
2513 }
2514
2515 /* Returns the number of pbbs that are in loops contained in SCOP. */
2516
2517 static int
2518 nb_pbbs_in_loops (scop_p scop)
2519 {
2520 int i;
2521 poly_bb_p pbb;
2522 int res = 0;
2523
2524 FOR_EACH_VEC_ELT (SCOP_BBS (scop), i, pbb)
2525 if (loop_in_sese_p (gbb_loop (PBB_BLACK_BOX (pbb)), SCOP_REGION (scop)))
2526 res++;
2527
2528 return res;
2529 }
2530
2531 /* Return the number of data references in BB that write in
2532 memory. */
2533
2534 static int
2535 nb_data_writes_in_bb (basic_block bb)
2536 {
2537 int res = 0;
2538 gimple_stmt_iterator gsi;
2539
2540 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2541 if (gimple_vdef (gsi_stmt (gsi)))
2542 res++;
2543
2544 return res;
2545 }
2546
2547 /* Splits at STMT the basic block BB represented as PBB in the
2548 polyhedral form. */
2549
2550 static edge
2551 split_pbb (scop_p scop, poly_bb_p pbb, basic_block bb, gimple stmt)
2552 {
2553 edge e1 = split_block (bb, stmt);
2554 new_pbb_from_pbb (scop, pbb, e1->dest);
2555 return e1;
2556 }
2557
2558 /* Splits STMT out of its current BB. This is done for reduction
2559 statements for which we want to ignore data dependences. */
2560
2561 static basic_block
2562 split_reduction_stmt (scop_p scop, gimple stmt)
2563 {
2564 basic_block bb = gimple_bb (stmt);
2565 poly_bb_p pbb = pbb_from_bb (bb);
2566 gimple_bb_p gbb = gbb_from_bb (bb);
2567 edge e1;
2568 int i;
2569 data_reference_p dr;
2570
2571 /* Do not split basic blocks with no writes to memory: the reduction
2572 will be the only write to memory. */
2573 if (nb_data_writes_in_bb (bb) == 0
2574 /* Or if we have already marked BB as a reduction. */
2575 || PBB_IS_REDUCTION (pbb_from_bb (bb)))
2576 return bb;
2577
2578 e1 = split_pbb (scop, pbb, bb, stmt);
2579
2580 /* Split once more only when the reduction stmt is not the only one
2581 left in the original BB. */
2582 if (!gsi_one_before_end_p (gsi_start_nondebug_bb (bb)))
2583 {
2584 gimple_stmt_iterator gsi = gsi_last_bb (bb);
2585 gsi_prev (&gsi);
2586 e1 = split_pbb (scop, pbb, bb, gsi_stmt (gsi));
2587 }
2588
2589 /* A part of the data references will end in a different basic block
2590 after the split: move the DRs from the original GBB to the newly
2591 created GBB1. */
2592 FOR_EACH_VEC_ELT (GBB_DATA_REFS (gbb), i, dr)
2593 {
2594 basic_block bb1 = gimple_bb (DR_STMT (dr));
2595
2596 if (bb1 != bb)
2597 {
2598 gimple_bb_p gbb1 = gbb_from_bb (bb1);
2599 GBB_DATA_REFS (gbb1).safe_push (dr);
2600 GBB_DATA_REFS (gbb).ordered_remove (i);
2601 i--;
2602 }
2603 }
2604
2605 return e1->dest;
2606 }
2607
2608 /* Return true when stmt is a reduction operation. */
2609
2610 static inline bool
2611 is_reduction_operation_p (gimple stmt)
2612 {
2613 enum tree_code code;
2614
2615 gcc_assert (is_gimple_assign (stmt));
2616 code = gimple_assign_rhs_code (stmt);
2617
2618 return flag_associative_math
2619 && commutative_tree_code (code)
2620 && associative_tree_code (code);
2621 }
2622
2623 /* Returns true when PHI contains an argument ARG. */
2624
2625 static bool
2626 phi_contains_arg (gimple phi, tree arg)
2627 {
2628 size_t i;
2629
2630 for (i = 0; i < gimple_phi_num_args (phi); i++)
2631 if (operand_equal_p (arg, gimple_phi_arg_def (phi, i), 0))
2632 return true;
2633
2634 return false;
2635 }
2636
2637 /* Return a loop phi node that corresponds to a reduction containing LHS. */
2638
2639 static gimple
2640 follow_ssa_with_commutative_ops (tree arg, tree lhs)
2641 {
2642 gimple stmt;
2643
2644 if (TREE_CODE (arg) != SSA_NAME)
2645 return NULL;
2646
2647 stmt = SSA_NAME_DEF_STMT (arg);
2648
2649 if (gimple_code (stmt) == GIMPLE_NOP
2650 || gimple_code (stmt) == GIMPLE_CALL)
2651 return NULL;
2652
2653 if (gimple_code (stmt) == GIMPLE_PHI)
2654 {
2655 if (phi_contains_arg (stmt, lhs))
2656 return stmt;
2657 return NULL;
2658 }
2659
2660 if (!is_gimple_assign (stmt))
2661 return NULL;
2662
2663 if (gimple_num_ops (stmt) == 2)
2664 return follow_ssa_with_commutative_ops (gimple_assign_rhs1 (stmt), lhs);
2665
2666 if (is_reduction_operation_p (stmt))
2667 {
2668 gimple res = follow_ssa_with_commutative_ops (gimple_assign_rhs1 (stmt), lhs);
2669
2670 return res ? res :
2671 follow_ssa_with_commutative_ops (gimple_assign_rhs2 (stmt), lhs);
2672 }
2673
2674 return NULL;
2675 }
2676
2677 /* Detect commutative and associative scalar reductions starting at
2678 the STMT. Return the phi node of the reduction cycle, or NULL. */
2679
2680 static gimple
2681 detect_commutative_reduction_arg (tree lhs, gimple stmt, tree arg,
2682 vec<gimple> *in,
2683 vec<gimple> *out)
2684 {
2685 gimple phi = follow_ssa_with_commutative_ops (arg, lhs);
2686
2687 if (!phi)
2688 return NULL;
2689
2690 in->safe_push (stmt);
2691 out->safe_push (stmt);
2692 return phi;
2693 }
2694
2695 /* Detect commutative and associative scalar reductions starting at
2696 STMT. Return the phi node of the reduction cycle, or NULL. */
2697
2698 static gimple
2699 detect_commutative_reduction_assign (gimple stmt, vec<gimple> *in,
2700 vec<gimple> *out)
2701 {
2702 tree lhs = gimple_assign_lhs (stmt);
2703
2704 if (gimple_num_ops (stmt) == 2)
2705 return detect_commutative_reduction_arg (lhs, stmt,
2706 gimple_assign_rhs1 (stmt),
2707 in, out);
2708
2709 if (is_reduction_operation_p (stmt))
2710 {
2711 gimple res = detect_commutative_reduction_arg (lhs, stmt,
2712 gimple_assign_rhs1 (stmt),
2713 in, out);
2714 return res ? res
2715 : detect_commutative_reduction_arg (lhs, stmt,
2716 gimple_assign_rhs2 (stmt),
2717 in, out);
2718 }
2719
2720 return NULL;
2721 }
2722
2723 /* Return a loop phi node that corresponds to a reduction containing LHS. */
2724
2725 static gimple
2726 follow_inital_value_to_phi (tree arg, tree lhs)
2727 {
2728 gimple stmt;
2729
2730 if (!arg || TREE_CODE (arg) != SSA_NAME)
2731 return NULL;
2732
2733 stmt = SSA_NAME_DEF_STMT (arg);
2734
2735 if (gimple_code (stmt) == GIMPLE_PHI
2736 && phi_contains_arg (stmt, lhs))
2737 return stmt;
2738
2739 return NULL;
2740 }
2741
2742
2743 /* Return the argument of the loop PHI that is the initial value coming
2744 from outside the loop. */
2745
2746 static edge
2747 edge_initial_value_for_loop_phi (gimple phi)
2748 {
2749 size_t i;
2750
2751 for (i = 0; i < gimple_phi_num_args (phi); i++)
2752 {
2753 edge e = gimple_phi_arg_edge (phi, i);
2754
2755 if (loop_depth (e->src->loop_father)
2756 < loop_depth (e->dest->loop_father))
2757 return e;
2758 }
2759
2760 return NULL;
2761 }
2762
2763 /* Return the argument of the loop PHI that is the initial value coming
2764 from outside the loop. */
2765
2766 static tree
2767 initial_value_for_loop_phi (gimple phi)
2768 {
2769 size_t i;
2770
2771 for (i = 0; i < gimple_phi_num_args (phi); i++)
2772 {
2773 edge e = gimple_phi_arg_edge (phi, i);
2774
2775 if (loop_depth (e->src->loop_father)
2776 < loop_depth (e->dest->loop_father))
2777 return gimple_phi_arg_def (phi, i);
2778 }
2779
2780 return NULL_TREE;
2781 }
2782
2783 /* Returns true when DEF is used outside the reduction cycle of
2784 LOOP_PHI. */
2785
2786 static bool
2787 used_outside_reduction (tree def, gimple loop_phi)
2788 {
2789 use_operand_p use_p;
2790 imm_use_iterator imm_iter;
2791 loop_p loop = loop_containing_stmt (loop_phi);
2792
2793 /* In LOOP, DEF should be used only in LOOP_PHI. */
2794 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, def)
2795 {
2796 gimple stmt = USE_STMT (use_p);
2797
2798 if (stmt != loop_phi
2799 && !is_gimple_debug (stmt)
2800 && flow_bb_inside_loop_p (loop, gimple_bb (stmt)))
2801 return true;
2802 }
2803
2804 return false;
2805 }
2806
2807 /* Detect commutative and associative scalar reductions belonging to
2808 the SCOP starting at the loop closed phi node STMT. Return the phi
2809 node of the reduction cycle, or NULL. */
2810
2811 static gimple
2812 detect_commutative_reduction (scop_p scop, gimple stmt, vec<gimple> *in,
2813 vec<gimple> *out)
2814 {
2815 if (scalar_close_phi_node_p (stmt))
2816 {
2817 gimple def, loop_phi, phi, close_phi = stmt;
2818 tree init, lhs, arg = gimple_phi_arg_def (close_phi, 0);
2819
2820 if (TREE_CODE (arg) != SSA_NAME)
2821 return NULL;
2822
2823 /* Note that loop close phi nodes should have a single argument
2824 because we translated the representation into a canonical form
2825 before Graphite: see canonicalize_loop_closed_ssa_form. */
2826 gcc_assert (gimple_phi_num_args (close_phi) == 1);
2827
2828 def = SSA_NAME_DEF_STMT (arg);
2829 if (!stmt_in_sese_p (def, SCOP_REGION (scop))
2830 || !(loop_phi = detect_commutative_reduction (scop, def, in, out)))
2831 return NULL;
2832
2833 lhs = gimple_phi_result (close_phi);
2834 init = initial_value_for_loop_phi (loop_phi);
2835 phi = follow_inital_value_to_phi (init, lhs);
2836
2837 if (phi && (used_outside_reduction (lhs, phi)
2838 || !has_single_use (gimple_phi_result (phi))))
2839 return NULL;
2840
2841 in->safe_push (loop_phi);
2842 out->safe_push (close_phi);
2843 return phi;
2844 }
2845
2846 if (gimple_code (stmt) == GIMPLE_ASSIGN)
2847 return detect_commutative_reduction_assign (stmt, in, out);
2848
2849 return NULL;
2850 }
2851
2852 /* Translate the scalar reduction statement STMT to an array RED
2853 knowing that its recursive phi node is LOOP_PHI. */
2854
2855 static void
2856 translate_scalar_reduction_to_array_for_stmt (scop_p scop, tree red,
2857 gimple stmt, gimple loop_phi)
2858 {
2859 tree res = gimple_phi_result (loop_phi);
2860 gimple assign = gimple_build_assign (res, unshare_expr (red));
2861 gimple_stmt_iterator gsi;
2862
2863 insert_stmts (scop, assign, NULL, gsi_after_labels (gimple_bb (loop_phi)));
2864
2865 assign = gimple_build_assign (unshare_expr (red), gimple_assign_lhs (stmt));
2866 gsi = gsi_for_stmt (stmt);
2867 gsi_next (&gsi);
2868 insert_stmts (scop, assign, NULL, gsi);
2869 }
2870
2871 /* Removes the PHI node and resets all the debug stmts that are using
2872 the PHI_RESULT. */
2873
2874 static void
2875 remove_phi (gimple phi)
2876 {
2877 imm_use_iterator imm_iter;
2878 tree def;
2879 use_operand_p use_p;
2880 gimple_stmt_iterator gsi;
2881 auto_vec<gimple, 3> update;
2882 unsigned int i;
2883 gimple stmt;
2884
2885 def = PHI_RESULT (phi);
2886 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, def)
2887 {
2888 stmt = USE_STMT (use_p);
2889
2890 if (is_gimple_debug (stmt))
2891 {
2892 gimple_debug_bind_reset_value (stmt);
2893 update.safe_push (stmt);
2894 }
2895 }
2896
2897 FOR_EACH_VEC_ELT (update, i, stmt)
2898 update_stmt (stmt);
2899
2900 gsi = gsi_for_phi_node (phi);
2901 remove_phi_node (&gsi, false);
2902 }
2903
2904 /* Helper function for for_each_index. For each INDEX of the data
2905 reference REF, returns true when its indices are valid in the loop
2906 nest LOOP passed in as DATA. */
2907
2908 static bool
2909 dr_indices_valid_in_loop (tree ref ATTRIBUTE_UNUSED, tree *index, void *data)
2910 {
2911 loop_p loop;
2912 basic_block header, def_bb;
2913 gimple stmt;
2914
2915 if (TREE_CODE (*index) != SSA_NAME)
2916 return true;
2917
2918 loop = *((loop_p *) data);
2919 header = loop->header;
2920 stmt = SSA_NAME_DEF_STMT (*index);
2921
2922 if (!stmt)
2923 return true;
2924
2925 def_bb = gimple_bb (stmt);
2926
2927 if (!def_bb)
2928 return true;
2929
2930 return dominated_by_p (CDI_DOMINATORS, header, def_bb);
2931 }
2932
2933 /* When the result of a CLOSE_PHI is written to a memory location,
2934 return a pointer to that memory reference, otherwise return
2935 NULL_TREE. */
2936
2937 static tree
2938 close_phi_written_to_memory (gimple close_phi)
2939 {
2940 imm_use_iterator imm_iter;
2941 use_operand_p use_p;
2942 gimple stmt;
2943 tree res, def = gimple_phi_result (close_phi);
2944
2945 FOR_EACH_IMM_USE_FAST (use_p, imm_iter, def)
2946 if ((stmt = USE_STMT (use_p))
2947 && gimple_code (stmt) == GIMPLE_ASSIGN
2948 && (res = gimple_assign_lhs (stmt)))
2949 {
2950 switch (TREE_CODE (res))
2951 {
2952 case VAR_DECL:
2953 case PARM_DECL:
2954 case RESULT_DECL:
2955 return res;
2956
2957 case ARRAY_REF:
2958 case MEM_REF:
2959 {
2960 tree arg = gimple_phi_arg_def (close_phi, 0);
2961 loop_p nest = loop_containing_stmt (SSA_NAME_DEF_STMT (arg));
2962
2963 /* FIXME: this restriction is for id-{24,25}.f and
2964 could be handled by duplicating the computation of
2965 array indices before the loop of the close_phi. */
2966 if (for_each_index (&res, dr_indices_valid_in_loop, &nest))
2967 return res;
2968 }
2969 /* Fallthru. */
2970
2971 default:
2972 continue;
2973 }
2974 }
2975 return NULL_TREE;
2976 }
2977
2978 /* Rewrite out of SSA the reduction described by the loop phi nodes
2979 IN, and the close phi nodes OUT. IN and OUT are structured by loop
2980 levels like this:
2981
2982 IN: stmt, loop_n, ..., loop_0
2983 OUT: stmt, close_n, ..., close_0
2984
2985 the first element is the reduction statement, and the next elements
2986 are the loop and close phi nodes of each of the outer loops. */
2987
2988 static void
2989 translate_scalar_reduction_to_array (scop_p scop,
2990 vec<gimple> in,
2991 vec<gimple> out)
2992 {
2993 gimple loop_phi;
2994 unsigned int i = out.length () - 1;
2995 tree red = close_phi_written_to_memory (out[i]);
2996
2997 FOR_EACH_VEC_ELT (in, i, loop_phi)
2998 {
2999 gimple close_phi = out[i];
3000
3001 if (i == 0)
3002 {
3003 gimple stmt = loop_phi;
3004 basic_block bb = split_reduction_stmt (scop, stmt);
3005 poly_bb_p pbb = pbb_from_bb (bb);
3006 PBB_IS_REDUCTION (pbb) = true;
3007 gcc_assert (close_phi == loop_phi);
3008
3009 if (!red)
3010 red = create_zero_dim_array
3011 (gimple_assign_lhs (stmt), "Commutative_Associative_Reduction");
3012
3013 translate_scalar_reduction_to_array_for_stmt (scop, red, stmt, in[1]);
3014 continue;
3015 }
3016
3017 if (i == in.length () - 1)
3018 {
3019 insert_out_of_ssa_copy (scop, gimple_phi_result (close_phi),
3020 unshare_expr (red), close_phi);
3021 insert_out_of_ssa_copy_on_edge
3022 (scop, edge_initial_value_for_loop_phi (loop_phi),
3023 unshare_expr (red), initial_value_for_loop_phi (loop_phi));
3024 }
3025
3026 remove_phi (loop_phi);
3027 remove_phi (close_phi);
3028 }
3029 }
3030
3031 /* Rewrites out of SSA a commutative reduction at CLOSE_PHI. Returns
3032 true when something has been changed. */
3033
3034 static bool
3035 rewrite_commutative_reductions_out_of_ssa_close_phi (scop_p scop,
3036 gimple close_phi)
3037 {
3038 bool res;
3039 auto_vec<gimple, 10> in;
3040 auto_vec<gimple, 10> out;
3041
3042 detect_commutative_reduction (scop, close_phi, &in, &out);
3043 res = in.length () > 1;
3044 if (res)
3045 translate_scalar_reduction_to_array (scop, in, out);
3046
3047 return res;
3048 }
3049
3050 /* Rewrites all the commutative reductions from LOOP out of SSA.
3051 Returns true when something has been changed. */
3052
3053 static bool
3054 rewrite_commutative_reductions_out_of_ssa_loop (scop_p scop,
3055 loop_p loop)
3056 {
3057 gimple_stmt_iterator gsi;
3058 edge exit = single_exit (loop);
3059 tree res;
3060 bool changed = false;
3061
3062 if (!exit)
3063 return false;
3064
3065 for (gsi = gsi_start_phis (exit->dest); !gsi_end_p (gsi); gsi_next (&gsi))
3066 if ((res = gimple_phi_result (gsi_stmt (gsi)))
3067 && !virtual_operand_p (res)
3068 && !scev_analyzable_p (res, SCOP_REGION (scop)))
3069 changed |= rewrite_commutative_reductions_out_of_ssa_close_phi
3070 (scop, gsi_stmt (gsi));
3071
3072 return changed;
3073 }
3074
3075 /* Rewrites all the commutative reductions from SCOP out of SSA. */
3076
3077 static void
3078 rewrite_commutative_reductions_out_of_ssa (scop_p scop)
3079 {
3080 loop_p loop;
3081 bool changed = false;
3082 sese region = SCOP_REGION (scop);
3083
3084 FOR_EACH_LOOP (loop, 0)
3085 if (loop_in_sese_p (loop, region))
3086 changed |= rewrite_commutative_reductions_out_of_ssa_loop (scop, loop);
3087
3088 if (changed)
3089 {
3090 scev_reset_htab ();
3091 gsi_commit_edge_inserts ();
3092 update_ssa (TODO_update_ssa);
3093 #ifdef ENABLE_CHECKING
3094 verify_loop_closed_ssa (true);
3095 #endif
3096 }
3097 }
3098
3099 /* Can all ivs be represented by a signed integer?
3100 As CLooG might generate negative values in its expressions, signed loop ivs
3101 are required in the backend. */
3102
3103 static bool
3104 scop_ivs_can_be_represented (scop_p scop)
3105 {
3106 loop_p loop;
3107 gimple_stmt_iterator psi;
3108 bool result = true;
3109
3110 FOR_EACH_LOOP (loop, 0)
3111 {
3112 if (!loop_in_sese_p (loop, SCOP_REGION (scop)))
3113 continue;
3114
3115 for (psi = gsi_start_phis (loop->header);
3116 !gsi_end_p (psi); gsi_next (&psi))
3117 {
3118 gimple phi = gsi_stmt (psi);
3119 tree res = PHI_RESULT (phi);
3120 tree type = TREE_TYPE (res);
3121
3122 if (TYPE_UNSIGNED (type)
3123 && TYPE_PRECISION (type) >= TYPE_PRECISION (long_long_integer_type_node))
3124 {
3125 result = false;
3126 break;
3127 }
3128 }
3129 if (!result)
3130 break;
3131 }
3132
3133 return result;
3134 }
3135
3136 /* Builds the polyhedral representation for a SESE region. */
3137
3138 void
3139 build_poly_scop (scop_p scop)
3140 {
3141 sese region = SCOP_REGION (scop);
3142 graphite_dim_t max_dim;
3143
3144 build_scop_bbs (scop);
3145
3146 /* FIXME: This restriction is needed to avoid a problem in CLooG.
3147 Once CLooG is fixed, remove this guard. Anyways, it makes no
3148 sense to optimize a scop containing only PBBs that do not belong
3149 to any loops. */
3150 if (nb_pbbs_in_loops (scop) == 0)
3151 return;
3152
3153 if (!scop_ivs_can_be_represented (scop))
3154 return;
3155
3156 if (flag_associative_math)
3157 rewrite_commutative_reductions_out_of_ssa (scop);
3158
3159 build_sese_loop_nests (region);
3160 /* Record all conditions in REGION. */
3161 sese_dom_walker (CDI_DOMINATORS, region).walk (cfun->cfg->x_entry_block_ptr);
3162 find_scop_parameters (scop);
3163
3164 max_dim = PARAM_VALUE (PARAM_GRAPHITE_MAX_NB_SCOP_PARAMS);
3165 if (scop_nb_params (scop) > max_dim)
3166 return;
3167
3168 build_scop_iteration_domain (scop);
3169 build_scop_context (scop);
3170 add_conditions_to_constraints (scop);
3171
3172 /* Rewrite out of SSA only after having translated the
3173 representation to the polyhedral representation to avoid scev
3174 analysis failures. That means that these functions will insert
3175 new data references that they create in the right place. */
3176 rewrite_reductions_out_of_ssa (scop);
3177 rewrite_cross_bb_scalar_deps_out_of_ssa (scop);
3178
3179 build_scop_drs (scop);
3180 scop_to_lst (scop);
3181 build_scop_scattering (scop);
3182
3183 /* This SCoP has been translated to the polyhedral
3184 representation. */
3185 POLY_SCOP_P (scop) = true;
3186 }
3187 #endif