1 /* Gimple Represented as Polyhedra.
2 Copyright (C) 2006-2013 Free Software Foundation, Inc.
3 Contributed by Sebastian Pop <sebastian.pop@inria.fr>.
5 This file is part of GCC.
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)
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.
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/>. */
21 /* This pass converts GIMPLE to GRAPHITE, performs some loop
22 transformations and then converts the resulting representation back
25 An early description of this pass can be found in the GCC Summit'06
26 paper "GRAPHITE: Polyhedral Analyses and Optimizations for GCC".
27 The wiki page http://gcc.gnu.org/wiki/Graphite contains pointers to
30 One important document to read is CLooG's internal manual:
31 http://repo.or.cz/w/cloog-ppl.git?a=blob_plain;f=doc/cloog.texi;hb=HEAD
32 that describes the data structure of loops used in this file, and
33 the functions that are used for transforming the code. */
40 #include <isl/options.h>
41 #include <isl/union_map.h>
42 #include <cloog/cloog.h>
43 #include <cloog/isl/domain.h>
44 #include <cloog/isl/cloog.h>
48 #include "coretypes.h"
49 #include "diagnostic-core.h"
53 #include "tree-ssa-loop.h"
54 #include "tree-dump.h"
56 #include "tree-chrec.h"
57 #include "tree-data-ref.h"
58 #include "tree-scalar-evolution.h"
61 #include "tree-parloops.h"
62 #include "tree-pass.h"
63 #include "tree-cfgcleanup.h"
67 #include "graphite-poly.h"
68 #include "graphite-scop-detection.h"
69 #include "graphite-clast-to-gimple.h"
70 #include "graphite-sese-to-poly.h"
71 #include "graphite-htab.h"
73 CloogState
*cloog_state
;
75 /* Print global statistics to FILE. */
78 print_global_statistics (FILE* file
)
83 long n_conditions
= 0;
87 long n_p_conditions
= 0;
93 gimple_stmt_iterator psi
;
98 /* Ignore artificial surrounding loop. */
99 if (bb
== bb
->loop_father
->header
103 n_p_loops
+= bb
->count
;
106 if (EDGE_COUNT (bb
->succs
) > 1)
109 n_p_conditions
+= bb
->count
;
112 for (psi
= gsi_start_bb (bb
); !gsi_end_p (psi
); gsi_next (&psi
))
115 n_p_stmts
+= bb
->count
;
119 fprintf (file
, "\nGlobal statistics (");
120 fprintf (file
, "BBS:%ld, ", n_bbs
);
121 fprintf (file
, "LOOPS:%ld, ", n_loops
);
122 fprintf (file
, "CONDITIONS:%ld, ", n_conditions
);
123 fprintf (file
, "STMTS:%ld)\n", n_stmts
);
124 fprintf (file
, "\nGlobal profiling statistics (");
125 fprintf (file
, "BBS:%ld, ", n_p_bbs
);
126 fprintf (file
, "LOOPS:%ld, ", n_p_loops
);
127 fprintf (file
, "CONDITIONS:%ld, ", n_p_conditions
);
128 fprintf (file
, "STMTS:%ld)\n", n_p_stmts
);
131 /* Print statistics for SCOP to FILE. */
134 print_graphite_scop_statistics (FILE* file
, scop_p scop
)
139 long n_conditions
= 0;
143 long n_p_conditions
= 0;
149 gimple_stmt_iterator psi
;
150 loop_p loop
= bb
->loop_father
;
152 if (!bb_in_sese_p (bb
, SCOP_REGION (scop
)))
156 n_p_bbs
+= bb
->count
;
158 if (EDGE_COUNT (bb
->succs
) > 1)
161 n_p_conditions
+= bb
->count
;
164 for (psi
= gsi_start_bb (bb
); !gsi_end_p (psi
); gsi_next (&psi
))
167 n_p_stmts
+= bb
->count
;
170 if (loop
->header
== bb
&& loop_in_sese_p (loop
, SCOP_REGION (scop
)))
173 n_p_loops
+= bb
->count
;
177 fprintf (file
, "\nSCoP statistics (");
178 fprintf (file
, "BBS:%ld, ", n_bbs
);
179 fprintf (file
, "LOOPS:%ld, ", n_loops
);
180 fprintf (file
, "CONDITIONS:%ld, ", n_conditions
);
181 fprintf (file
, "STMTS:%ld)\n", n_stmts
);
182 fprintf (file
, "\nSCoP profiling statistics (");
183 fprintf (file
, "BBS:%ld, ", n_p_bbs
);
184 fprintf (file
, "LOOPS:%ld, ", n_p_loops
);
185 fprintf (file
, "CONDITIONS:%ld, ", n_p_conditions
);
186 fprintf (file
, "STMTS:%ld)\n", n_p_stmts
);
189 /* Print statistics for SCOPS to FILE. */
192 print_graphite_statistics (FILE* file
, vec
<scop_p
> scops
)
198 FOR_EACH_VEC_ELT (scops
, i
, scop
)
199 print_graphite_scop_statistics (file
, scop
);
202 /* Initialize graphite: when there are no loops returns false. */
205 graphite_initialize (isl_ctx
*ctx
)
207 if (number_of_loops (cfun
) <= 1
208 /* FIXME: This limit on the number of basic blocks of a function
209 should be removed when the SCOP detection is faster. */
210 || n_basic_blocks
> PARAM_VALUE (PARAM_GRAPHITE_MAX_BBS_PER_FUNCTION
))
212 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
213 print_global_statistics (dump_file
);
220 recompute_all_dominators ();
221 initialize_original_copy_tables ();
223 cloog_state
= cloog_isl_state_malloc (ctx
);
225 if (dump_file
&& dump_flags
)
226 dump_function_to_file (current_function_decl
, dump_file
, dump_flags
);
231 /* Finalize graphite: perform CFG cleanup when NEED_CFG_CLEANUP_P is
235 graphite_finalize (bool need_cfg_cleanup_p
)
237 if (need_cfg_cleanup_p
)
241 profile_status
= PROFILE_ABSENT
;
242 release_recorded_exits ();
243 tree_estimate_probability ();
246 cloog_state_free (cloog_state
);
247 free_original_copy_tables ();
249 if (dump_file
&& dump_flags
)
250 print_loops (dump_file
, 3);
253 isl_ctx
*the_isl_ctx
;
255 /* Perform a set of linear transforms on the loops of the current
259 graphite_transform_loops (void)
263 bool need_cfg_cleanup_p
= false;
264 vec
<scop_p
> scops
= vNULL
;
265 bb_pbb_htab_type bb_pbb_mapping
;
268 /* If a function is parallel it was most probably already run through graphite
269 once. No need to run again. */
270 if (parallelized_function_p (cfun
->decl
))
273 ctx
= isl_ctx_alloc ();
274 isl_options_set_on_error (ctx
, ISL_ON_ERROR_ABORT
);
275 if (!graphite_initialize (ctx
))
279 build_scops (&scops
);
281 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
283 print_graphite_statistics (dump_file
, scops
);
284 print_global_statistics (dump_file
);
287 bb_pbb_mapping
.create (10);
289 FOR_EACH_VEC_ELT (scops
, i
, scop
)
290 if (dbg_cnt (graphite_scop
))
293 build_poly_scop (scop
);
295 if (POLY_SCOP_P (scop
)
296 && apply_poly_transforms (scop
)
297 && gloog (scop
, bb_pbb_mapping
))
298 need_cfg_cleanup_p
= true;
301 bb_pbb_mapping
.dispose ();
303 graphite_finalize (need_cfg_cleanup_p
);
308 #else /* If Cloog is not available: #ifndef HAVE_cloog. */
311 graphite_transform_loops (void)
313 sorry ("Graphite loop optimizations cannot be used");
320 graphite_transforms (void)
325 graphite_transform_loops ();
331 gate_graphite_transforms (void)
333 /* Enable -fgraphite pass if any one of the graphite optimization flags
336 || flag_loop_interchange
337 || flag_loop_strip_mine
338 || flag_graphite_identity
339 || flag_loop_parallelize_all
340 || flag_loop_optimize_isl
)
343 return flag_graphite
!= 0;
348 const pass_data pass_data_graphite
=
350 GIMPLE_PASS
, /* type */
351 "graphite0", /* name */
352 OPTGROUP_LOOP
, /* optinfo_flags */
354 false, /* has_execute */
355 TV_GRAPHITE
, /* tv_id */
356 ( PROP_cfg
| PROP_ssa
), /* properties_required */
357 0, /* properties_provided */
358 0, /* properties_destroyed */
359 0, /* todo_flags_start */
360 0, /* todo_flags_finish */
363 class pass_graphite
: public gimple_opt_pass
366 pass_graphite (gcc::context
*ctxt
)
367 : gimple_opt_pass (pass_data_graphite
, ctxt
)
370 /* opt_pass methods: */
371 bool gate () { return gate_graphite_transforms (); }
373 }; // class pass_graphite
378 make_pass_graphite (gcc::context
*ctxt
)
380 return new pass_graphite (ctxt
);
385 const pass_data pass_data_graphite_transforms
=
387 GIMPLE_PASS
, /* type */
388 "graphite", /* name */
389 OPTGROUP_LOOP
, /* optinfo_flags */
391 true, /* has_execute */
392 TV_GRAPHITE_TRANSFORMS
, /* tv_id */
393 ( PROP_cfg
| PROP_ssa
), /* properties_required */
394 0, /* properties_provided */
395 0, /* properties_destroyed */
396 0, /* todo_flags_start */
397 0, /* todo_flags_finish */
400 class pass_graphite_transforms
: public gimple_opt_pass
403 pass_graphite_transforms (gcc::context
*ctxt
)
404 : gimple_opt_pass (pass_data_graphite_transforms
, ctxt
)
407 /* opt_pass methods: */
408 bool gate () { return gate_graphite_transforms (); }
409 unsigned int execute () { return graphite_transforms (); }
411 }; // class pass_graphite_transforms
416 make_pass_graphite_transforms (gcc::context
*ctxt
)
418 return new pass_graphite_transforms (ctxt
);