1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
|
/* Graphite polyhedral representation.
Copyright (C) 2009 Free Software Foundation, Inc.
Contributed by Sebastian Pop <sebastian.pop@amd.com> and
Tobias Grosser <grosser@fim.uni-passau.de>.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GCC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
#include "ggc.h"
#include "tree.h"
#include "rtl.h"
#include "output.h"
#include "basic-block.h"
#include "diagnostic.h"
#include "tree-flow.h"
#include "toplev.h"
#include "tree-dump.h"
#include "timevar.h"
#include "cfgloop.h"
#include "tree-chrec.h"
#include "tree-data-ref.h"
#include "tree-scalar-evolution.h"
#include "tree-pass.h"
#include "domwalk.h"
#include "value-prof.h"
#include "pointer-set.h"
#include "gimple.h"
#include "params.h"
#ifdef HAVE_cloog
#include "cloog/cloog.h"
#include "ppl_c.h"
#include "sese.h"
#include "graphite-ppl.h"
#include "graphite.h"
#include "graphite-poly.h"
#include "graphite-dependences.h"
/* Return the maximal loop depth in SCOP. */
int
scop_max_loop_depth (scop_p scop)
{
int i;
poly_bb_p pbb;
int max_nb_loops = 0;
for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
{
int nb_loops = pbb_dim_iter_domain (pbb);
if (max_nb_loops < nb_loops)
max_nb_loops = nb_loops;
}
return max_nb_loops;
}
/* Extend the scattering matrix of PBB to MAX_SCATTERING scattering
dimensions. */
static void
extend_scattering (poly_bb_p pbb, int max_scattering)
{
ppl_dimension_type nb_old_dims, nb_new_dims;
int nb_added_dims, i;
ppl_Coefficient_t coef;
Value one;
nb_added_dims = max_scattering - pbb_nb_scattering_transform (pbb);
value_init (one);
value_set_si (one, 1);
ppl_new_Coefficient (&coef);
ppl_assign_Coefficient_from_mpz_t (coef, one);
gcc_assert (nb_added_dims >= 0);
nb_old_dims = pbb_nb_scattering_transform (pbb) + pbb_dim_iter_domain (pbb)
+ scop_nb_params (PBB_SCOP (pbb));
nb_new_dims = nb_old_dims + nb_added_dims;
ppl_insert_dimensions (PBB_TRANSFORMED_SCATTERING (pbb),
pbb_nb_scattering_transform (pbb), nb_added_dims);
PBB_NB_SCATTERING_TRANSFORM (pbb) += nb_added_dims;
/* Add identity matrix for the added dimensions. */
for (i = max_scattering - nb_added_dims; i < max_scattering; i++)
{
ppl_Constraint_t cstr;
ppl_Linear_Expression_t expr;
ppl_new_Linear_Expression_with_dimension (&expr, nb_new_dims);
ppl_Linear_Expression_add_to_coefficient (expr, i, coef);
ppl_new_Constraint (&cstr, expr, PPL_CONSTRAINT_TYPE_EQUAL);
ppl_Polyhedron_add_constraint (PBB_TRANSFORMED_SCATTERING (pbb), cstr);
ppl_delete_Constraint (cstr);
ppl_delete_Linear_Expression (expr);
}
ppl_delete_Coefficient (coef);
value_clear (one);
}
/* All scattering matrices in SCOP will have the same number of scattering
dimensions. */
int
unify_scattering_dimensions (scop_p scop)
{
int i;
poly_bb_p pbb;
graphite_dim_t max_scattering = 0;
for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
max_scattering = MAX (pbb_nb_scattering_transform (pbb), max_scattering);
for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
extend_scattering (pbb, max_scattering);
return max_scattering;
}
/* Prints to FILE the scattering function of PBB. */
void
print_scattering_function (FILE *file, poly_bb_p pbb)
{
graphite_dim_t i;
if (!PBB_TRANSFORMED (pbb))
return;
fprintf (file, "scattering bb_%d (\n", GBB_BB (PBB_BLACK_BOX (pbb))->index);
fprintf (file, "# eq");
for (i = 0; i < pbb_nb_scattering_transform (pbb); i++)
fprintf (file, " s%d", (int) i);
for (i = 0; i < pbb_nb_local_vars (pbb); i++)
fprintf (file, " lv%d", (int) i);
for (i = 0; i < pbb_dim_iter_domain (pbb); i++)
fprintf (file, " i%d", (int) i);
for (i = 0; i < pbb_nb_params (pbb); i++)
fprintf (file, " p%d", (int) i);
fprintf (file, " cst\n");
ppl_print_polyhedron_matrix (file, PBB_TRANSFORMED_SCATTERING (pbb));
fprintf (file, ")\n");
}
/* Prints to FILE the iteration domain of PBB. */
void
print_iteration_domain (FILE *file, poly_bb_p pbb)
{
print_pbb_domain (file, pbb);
}
/* Prints to FILE the scattering functions of every PBB of SCOP. */
void
print_scattering_functions (FILE *file, scop_p scop)
{
int i;
poly_bb_p pbb;
for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
print_scattering_function (file, pbb);
}
/* Prints to FILE the iteration domains of every PBB of SCOP. */
void
print_iteration_domains (FILE *file, scop_p scop)
{
int i;
poly_bb_p pbb;
for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
print_iteration_domain (file, pbb);
}
/* Prints to STDERR the scattering function of PBB. */
void
debug_scattering_function (poly_bb_p pbb)
{
print_scattering_function (stderr, pbb);
}
/* Prints to STDERR the iteration domain of PBB. */
void
debug_iteration_domain (poly_bb_p pbb)
{
print_iteration_domain (stderr, pbb);
}
/* Prints to STDERR the scattering functions of every PBB of SCOP. */
void
debug_scattering_functions (scop_p scop)
{
print_scattering_functions (stderr, scop);
}
/* Prints to STDERR the iteration domains of every PBB of SCOP. */
void
debug_iteration_domains (scop_p scop)
{
print_iteration_domains (stderr, scop);
}
/* Apply graphite transformations to all the basic blocks of SCOP. */
bool
apply_poly_transforms (scop_p scop)
{
bool transform_done = false;
gcc_assert (graphite_legal_transform (scop));
/* Generate code even if we did not apply any real transformation.
This also allows to check the performance for the identity
transformation: GIMPLE -> GRAPHITE -> GIMPLE
Keep in mind that CLooG optimizes in control, so the loop structure
may change, even if we only use -fgraphite-identity. */
if (flag_graphite_identity)
transform_done = true;
if (flag_loop_parallelize_all)
transform_done = true;
if (flag_loop_block)
gcc_unreachable (); /* Not yet supported. */
if (flag_loop_strip_mine)
transform_done |= scop_do_strip_mine (scop);
if (flag_loop_interchange)
transform_done |= scop_do_interchange (scop);
return transform_done;
}
/* Create a new polyhedral data reference and add it to PBB. It is
defined by its ACCESSES, its TYPE, and the number of subscripts
NB_SUBSCRIPTS. */
void
new_poly_dr (poly_bb_p pbb,
ppl_Pointset_Powerset_C_Polyhedron_t accesses,
enum POLY_DR_TYPE type, void *cdr, int nb_subscripts)
{
poly_dr_p pdr = XNEW (struct poly_dr);
PDR_PBB (pdr) = pbb;
PDR_ACCESSES (pdr) = accesses;
PDR_TYPE (pdr) = type;
PDR_CDR (pdr) = cdr;
PDR_NB_SUBSCRIPTS (pdr) = nb_subscripts;
VEC_safe_push (poly_dr_p, heap, PBB_DRS (pbb), pdr);
}
/* Free polyhedral data reference PDR. */
void
free_poly_dr (poly_dr_p pdr)
{
ppl_delete_Pointset_Powerset_C_Polyhedron (PDR_ACCESSES (pdr));
XDELETE (pdr);
}
/* Create a new polyhedral black box. */
void
new_poly_bb (scop_p scop, void *black_box)
{
poly_bb_p pbb = XNEW (struct poly_bb);
PBB_DOMAIN (pbb) = NULL;
PBB_SCOP (pbb) = scop;
pbb_set_black_box (pbb, black_box);
PBB_TRANSFORMED (pbb) = NULL;
PBB_SAVED (pbb) = NULL;
PBB_ORIGINAL (pbb) = NULL;
PBB_DRS (pbb) = VEC_alloc (poly_dr_p, heap, 3);
VEC_safe_push (poly_bb_p, heap, SCOP_BBS (scop), pbb);
}
/* Free polyhedral black box. */
void
free_poly_bb (poly_bb_p pbb)
{
int i;
poly_dr_p pdr;
ppl_delete_Pointset_Powerset_C_Polyhedron (PBB_DOMAIN (pbb));
if (PBB_TRANSFORMED (pbb))
poly_scattering_free (PBB_TRANSFORMED (pbb));
if (PBB_SAVED (pbb))
poly_scattering_free (PBB_SAVED (pbb));
if (PBB_ORIGINAL (pbb))
poly_scattering_free (PBB_ORIGINAL (pbb));
if (PBB_DRS (pbb))
for (i = 0; VEC_iterate (poly_dr_p, PBB_DRS (pbb), i, pdr); i++)
free_poly_dr (pdr);
VEC_free (poly_dr_p, heap, PBB_DRS (pbb));
XDELETE (pbb);
}
static void
print_pdr_access_layout (FILE *file, poly_dr_p pdr)
{
graphite_dim_t i;
fprintf (file, "# eq");
for (i = 0; i < pdr_dim_iter_domain (pdr); i++)
fprintf (file, " i%d", (int) i);
for (i = 0; i < pdr_nb_params (pdr); i++)
fprintf (file, " p%d", (int) i);
fprintf (file, " alias");
for (i = 0; i < PDR_NB_SUBSCRIPTS (pdr); i++)
fprintf (file, " sub%d", (int) i);
fprintf (file, " cst\n");
}
/* Prints to FILE the polyhedral data reference PDR. */
void
print_pdr (FILE *file, poly_dr_p pdr)
{
fprintf (file, "pdr (");
switch (PDR_TYPE (pdr))
{
case PDR_READ:
fprintf (file, "read \n");
break;
case PDR_WRITE:
fprintf (file, "write \n");
break;
case PDR_MAY_WRITE:
fprintf (file, "may_write \n");
break;
default:
gcc_unreachable ();
}
dump_data_reference (file, (data_reference_p) PDR_CDR (pdr));
fprintf (file, "data accesses (\n");
print_pdr_access_layout (file, pdr);
ppl_print_powerset_matrix (file, PDR_ACCESSES (pdr));
fprintf (file, ")\n");
fprintf (file, ")\n");
}
/* Prints to STDERR the polyhedral data reference PDR. */
void
debug_pdr (poly_dr_p pdr)
{
print_pdr (stderr, pdr);
}
/* Creates a new SCOP containing REGION. */
scop_p
new_scop (void *region)
{
scop_p scop = XNEW (struct scop);
SCOP_DEP_GRAPH (scop) = NULL;
SCOP_CONTEXT (scop) = NULL;
scop_set_region (scop, region);
SCOP_BBS (scop) = VEC_alloc (poly_bb_p, heap, 3);
SCOP_ORIGINAL_PDR_PAIRS (scop) = htab_create (10, hash_poly_dr_pair_p,
eq_poly_dr_pair_p, free);
return scop;
}
/* Deletes SCOP. */
void
free_scop (scop_p scop)
{
int i;
poly_bb_p pbb;
for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
free_poly_bb (pbb);
VEC_free (poly_bb_p, heap, SCOP_BBS (scop));
if (SCOP_CONTEXT (scop))
ppl_delete_Pointset_Powerset_C_Polyhedron (SCOP_CONTEXT (scop));
htab_delete (SCOP_ORIGINAL_PDR_PAIRS (scop));
XDELETE (scop);
}
/* Print to FILE the domain of PBB. */
void
print_pbb_domain (FILE *file, poly_bb_p pbb)
{
graphite_dim_t i;
gimple_bb_p gbb = PBB_BLACK_BOX (pbb);
if (!PBB_DOMAIN (pbb))
return;
fprintf (file, "domains bb_%d (\n", GBB_BB (gbb)->index);
fprintf (file, "# eq");
for (i = 0; i < pbb_dim_iter_domain (pbb); i++)
fprintf (file, " i%d", (int) i);
for (i = 0; i < pbb_nb_params (pbb); i++)
fprintf (file, " p%d", (int) i);
fprintf (file, " cst\n");
if (PBB_DOMAIN (pbb))
ppl_print_powerset_matrix (file, PBB_DOMAIN (pbb));
fprintf (file, ")\n");
}
/* Dump the cases of a graphite basic block GBB on FILE. */
static void
dump_gbb_cases (FILE *file, gimple_bb_p gbb)
{
int i;
gimple stmt;
VEC (gimple, heap) *cases;
if (!gbb)
return;
cases = GBB_CONDITION_CASES (gbb);
if (VEC_empty (gimple, cases))
return;
fprintf (file, "cases bb_%d (", GBB_BB (gbb)->index);
for (i = 0; VEC_iterate (gimple, cases, i, stmt); i++)
print_gimple_stmt (file, stmt, 0, 0);
fprintf (file, ")\n");
}
/* Dump conditions of a graphite basic block GBB on FILE. */
static void
dump_gbb_conditions (FILE *file, gimple_bb_p gbb)
{
int i;
gimple stmt;
VEC (gimple, heap) *conditions;
if (!gbb)
return;
conditions = GBB_CONDITIONS (gbb);
if (VEC_empty (gimple, conditions))
return;
fprintf (file, "conditions bb_%d (", GBB_BB (gbb)->index);
for (i = 0; VEC_iterate (gimple, conditions, i, stmt); i++)
print_gimple_stmt (file, stmt, 0, 0);
fprintf (file, ")\n");
}
/* Print to FILE all the data references of PBB. */
void
print_pdrs (FILE *file, poly_bb_p pbb)
{
int i;
poly_dr_p pdr;
for (i = 0; VEC_iterate (poly_dr_p, PBB_DRS (pbb), i, pdr); i++)
print_pdr (file, pdr);
}
/* Print to STDERR all the data references of PBB. */
void
debug_pdrs (poly_bb_p pbb)
{
print_pdrs (stderr, pbb);
}
/* Print to FILE the domain and scattering function of PBB. */
void
print_pbb (FILE *file, poly_bb_p pbb)
{
fprintf (file, "pbb_%d (\n", GBB_BB (PBB_BLACK_BOX (pbb))->index);
dump_gbb_conditions (file, PBB_BLACK_BOX (pbb));
dump_gbb_cases (file, PBB_BLACK_BOX (pbb));
print_pdrs (file, pbb);
print_pbb_domain (file, pbb);
print_scattering_function (file, pbb);
fprintf (file, ")\n");
}
/* Print to FILE the parameters of SCOP. */
void
print_scop_params (FILE *file, scop_p scop)
{
int i;
tree t;
fprintf (file, "parameters (\n");
for (i = 0; VEC_iterate (tree, SESE_PARAMS (SCOP_REGION (scop)), i, t); i++)
{
fprintf (file, "p_%d -> ", i);
print_generic_expr (file, t, 0);
fprintf (file, "\n");
}
fprintf (file, ")\n");
}
/* Print to FILE the context of SCoP. */
void
print_scop_context (FILE *file, scop_p scop)
{
graphite_dim_t i;
fprintf (file, "context (\n");
fprintf (file, "# eq");
for (i = 0; i < scop_nb_params (scop); i++)
fprintf (file, " p%d", (int) i);
fprintf (file, " cst\n");
if (SCOP_CONTEXT (scop))
ppl_print_powerset_matrix (file, SCOP_CONTEXT (scop));
fprintf (file, ")\n");
}
/* Print to FILE the SCOP. */
void
print_scop (FILE *file, scop_p scop)
{
int i;
poly_bb_p pbb;
fprintf (file, "scop (\n");
print_scop_params (file, scop);
print_scop_context (file, scop);
for (i = 0; VEC_iterate (poly_bb_p, SCOP_BBS (scop), i, pbb); i++)
print_pbb (file, pbb);
fprintf (file, ")\n");
}
/* Print to STDERR the domain of PBB. */
void
debug_pbb_domain (poly_bb_p pbb)
{
print_pbb_domain (stderr, pbb);
}
/* Print to FILE the domain and scattering function of PBB. */
void
debug_pbb (poly_bb_p pbb)
{
print_pbb (stderr, pbb);
}
/* Print to STDERR the context of SCOP. */
void
debug_scop_context (scop_p scop)
{
print_scop_context (stderr, scop);
}
/* Print to STDERR the SCOP. */
void
debug_scop (scop_p scop)
{
print_scop (stderr, scop);
}
/* Print to STDERR the parameters of SCOP. */
void
debug_scop_params (scop_p scop)
{
print_scop_params (stderr, scop);
}
/* The dimension in the transformed scattering polyhedron of PBB
containing the scattering iterator for the loop at depth LOOP_DEPTH. */
ppl_dimension_type
psct_scattering_dim_for_loop_depth (poly_bb_p pbb, graphite_dim_t loop_depth)
{
ppl_const_Constraint_System_t pcs;
ppl_Constraint_System_const_iterator_t cit, cend;
ppl_const_Constraint_t cstr;
ppl_Polyhedron_t ph = PBB_TRANSFORMED_SCATTERING (pbb);
ppl_dimension_type iter = psct_iterator_dim (pbb, loop_depth);
ppl_Linear_Expression_t expr;
ppl_Coefficient_t coef;
Value val;
graphite_dim_t i;
value_init (val);
ppl_new_Coefficient (&coef);
ppl_Polyhedron_get_constraints (ph, &pcs);
ppl_new_Constraint_System_const_iterator (&cit);
ppl_new_Constraint_System_const_iterator (&cend);
for (ppl_Constraint_System_begin (pcs, cit),
ppl_Constraint_System_end (pcs, cend);
!ppl_Constraint_System_const_iterator_equal_test (cit, cend);
ppl_Constraint_System_const_iterator_increment (cit))
{
ppl_Constraint_System_const_iterator_dereference (cit, &cstr);
ppl_new_Linear_Expression_from_Constraint (&expr, cstr);
ppl_Linear_Expression_coefficient (expr, iter, coef);
ppl_Coefficient_to_mpz_t (coef, val);
if (value_zero_p (val))
{
ppl_delete_Linear_Expression (expr);
continue;
}
for (i = 0; i < pbb_nb_scattering_transform (pbb); i++)
{
ppl_dimension_type scatter = psct_scattering_dim (pbb, i);
ppl_Linear_Expression_coefficient (expr, scatter, coef);
ppl_Coefficient_to_mpz_t (coef, val);
if (value_notzero_p (val))
{
value_clear (val);
ppl_delete_Linear_Expression (expr);
ppl_delete_Coefficient (coef);
ppl_delete_Constraint_System_const_iterator (cit);
ppl_delete_Constraint_System_const_iterator (cend);
return scatter;
}
}
}
gcc_unreachable ();
}
/* Returns the number of iterations NITER of the loop around PBB at
depth LOOP_DEPTH. */
void
pbb_number_of_iterations (poly_bb_p pbb,
graphite_dim_t loop_depth,
Value niter)
{
ppl_Linear_Expression_t le;
ppl_dimension_type dim;
ppl_Pointset_Powerset_C_Polyhedron_space_dimension (PBB_DOMAIN (pbb), &dim);
ppl_new_Linear_Expression_with_dimension (&le, dim);
ppl_set_coef (le, pbb_iterator_dim (pbb, loop_depth), 1);
value_set_si (niter, -1);
ppl_max_for_le (PBB_DOMAIN (pbb), le, niter);
ppl_delete_Linear_Expression (le);
}
#endif
|