graphite.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /* Gimple Represented as Polyhedra.
  2. Copyright (C) 2006-2015 Free Software Foundation, Inc.
  3. Contributed by Sebastian Pop <sebastian.pop@inria.fr>.
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. GCC is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. /* This pass converts GIMPLE to GRAPHITE, performs some loop
  17. transformations and then converts the resulting representation back
  18. to GIMPLE.
  19. An early description of this pass can be found in the GCC Summit'06
  20. paper "GRAPHITE: Polyhedral Analyses and Optimizations for GCC".
  21. The wiki page http://gcc.gnu.org/wiki/Graphite contains pointers to
  22. the related work.
  23. One important document to read is CLooG's internal manual:
  24. http://repo.or.cz/w/cloog-ppl.git?a=blob_plain;f=doc/cloog.texi;hb=HEAD
  25. that describes the data structure of loops used in this file, and
  26. the functions that are used for transforming the code. */
  27. #include "config.h"
  28. #ifdef HAVE_isl
  29. #include <isl/set.h>
  30. #include <isl/map.h>
  31. #include <isl/options.h>
  32. #include <isl/union_map.h>
  33. #endif
  34. #include "system.h"
  35. #include "coretypes.h"
  36. #include "diagnostic-core.h"
  37. #include "hash-set.h"
  38. #include "machmode.h"
  39. #include "vec.h"
  40. #include "double-int.h"
  41. #include "input.h"
  42. #include "alias.h"
  43. #include "symtab.h"
  44. #include "options.h"
  45. #include "wide-int.h"
  46. #include "inchash.h"
  47. #include "tree.h"
  48. #include "fold-const.h"
  49. #include "predict.h"
  50. #include "tm.h"
  51. #include "hard-reg-set.h"
  52. #include "input.h"
  53. #include "function.h"
  54. #include "dominance.h"
  55. #include "cfg.h"
  56. #include "basic-block.h"
  57. #include "tree-ssa-alias.h"
  58. #include "internal-fn.h"
  59. #include "gimple-expr.h"
  60. #include "is-a.h"
  61. #include "gimple.h"
  62. #include "gimple-iterator.h"
  63. #include "tree-cfg.h"
  64. #include "tree-ssa-loop.h"
  65. #include "tree-dump.h"
  66. #include "cfgloop.h"
  67. #include "tree-chrec.h"
  68. #include "tree-data-ref.h"
  69. #include "tree-scalar-evolution.h"
  70. #include "sese.h"
  71. #include "dbgcnt.h"
  72. #include "tree-parloops.h"
  73. #include "tree-pass.h"
  74. #include "tree-cfgcleanup.h"
  75. #ifdef HAVE_isl
  76. #include "graphite-poly.h"
  77. #include "graphite-scop-detection.h"
  78. #include "graphite-isl-ast-to-gimple.h"
  79. #include "graphite-sese-to-poly.h"
  80. /* Print global statistics to FILE. */
  81. static void
  82. print_global_statistics (FILE* file)
  83. {
  84. long n_bbs = 0;
  85. long n_loops = 0;
  86. long n_stmts = 0;
  87. long n_conditions = 0;
  88. long n_p_bbs = 0;
  89. long n_p_loops = 0;
  90. long n_p_stmts = 0;
  91. long n_p_conditions = 0;
  92. basic_block bb;
  93. FOR_ALL_BB_FN (bb, cfun)
  94. {
  95. gimple_stmt_iterator psi;
  96. n_bbs++;
  97. n_p_bbs += bb->count;
  98. /* Ignore artificial surrounding loop. */
  99. if (bb == bb->loop_father->header
  100. && bb->index != 0)
  101. {
  102. n_loops++;
  103. n_p_loops += bb->count;
  104. }
  105. if (EDGE_COUNT (bb->succs) > 1)
  106. {
  107. n_conditions++;
  108. n_p_conditions += bb->count;
  109. }
  110. for (psi = gsi_start_bb (bb); !gsi_end_p (psi); gsi_next (&psi))
  111. {
  112. n_stmts++;
  113. n_p_stmts += bb->count;
  114. }
  115. }
  116. fprintf (file, "\nGlobal statistics (");
  117. fprintf (file, "BBS:%ld, ", n_bbs);
  118. fprintf (file, "LOOPS:%ld, ", n_loops);
  119. fprintf (file, "CONDITIONS:%ld, ", n_conditions);
  120. fprintf (file, "STMTS:%ld)\n", n_stmts);
  121. fprintf (file, "\nGlobal profiling statistics (");
  122. fprintf (file, "BBS:%ld, ", n_p_bbs);
  123. fprintf (file, "LOOPS:%ld, ", n_p_loops);
  124. fprintf (file, "CONDITIONS:%ld, ", n_p_conditions);
  125. fprintf (file, "STMTS:%ld)\n", n_p_stmts);
  126. }
  127. /* Print statistics for SCOP to FILE. */
  128. static void
  129. print_graphite_scop_statistics (FILE* file, scop_p scop)
  130. {
  131. long n_bbs = 0;
  132. long n_loops = 0;
  133. long n_stmts = 0;
  134. long n_conditions = 0;
  135. long n_p_bbs = 0;
  136. long n_p_loops = 0;
  137. long n_p_stmts = 0;
  138. long n_p_conditions = 0;
  139. basic_block bb;
  140. FOR_ALL_BB_FN (bb, cfun)
  141. {
  142. gimple_stmt_iterator psi;
  143. loop_p loop = bb->loop_father;
  144. if (!bb_in_sese_p (bb, SCOP_REGION (scop)))
  145. continue;
  146. n_bbs++;
  147. n_p_bbs += bb->count;
  148. if (EDGE_COUNT (bb->succs) > 1)
  149. {
  150. n_conditions++;
  151. n_p_conditions += bb->count;
  152. }
  153. for (psi = gsi_start_bb (bb); !gsi_end_p (psi); gsi_next (&psi))
  154. {
  155. n_stmts++;
  156. n_p_stmts += bb->count;
  157. }
  158. if (loop->header == bb && loop_in_sese_p (loop, SCOP_REGION (scop)))
  159. {
  160. n_loops++;
  161. n_p_loops += bb->count;
  162. }
  163. }
  164. fprintf (file, "\nSCoP statistics (");
  165. fprintf (file, "BBS:%ld, ", n_bbs);
  166. fprintf (file, "LOOPS:%ld, ", n_loops);
  167. fprintf (file, "CONDITIONS:%ld, ", n_conditions);
  168. fprintf (file, "STMTS:%ld)\n", n_stmts);
  169. fprintf (file, "\nSCoP profiling statistics (");
  170. fprintf (file, "BBS:%ld, ", n_p_bbs);
  171. fprintf (file, "LOOPS:%ld, ", n_p_loops);
  172. fprintf (file, "CONDITIONS:%ld, ", n_p_conditions);
  173. fprintf (file, "STMTS:%ld)\n", n_p_stmts);
  174. }
  175. /* Print statistics for SCOPS to FILE. */
  176. static void
  177. print_graphite_statistics (FILE* file, vec<scop_p> scops)
  178. {
  179. int i;
  180. scop_p scop;
  181. FOR_EACH_VEC_ELT (scops, i, scop)
  182. print_graphite_scop_statistics (file, scop);
  183. }
  184. /* Initialize graphite: when there are no loops returns false. */
  185. static bool
  186. graphite_initialize (isl_ctx *ctx)
  187. {
  188. if (number_of_loops (cfun) <= 1
  189. /* FIXME: This limit on the number of basic blocks of a function
  190. should be removed when the SCOP detection is faster. */
  191. || (n_basic_blocks_for_fn (cfun) >
  192. PARAM_VALUE (PARAM_GRAPHITE_MAX_BBS_PER_FUNCTION)))
  193. {
  194. if (dump_file && (dump_flags & TDF_DETAILS))
  195. print_global_statistics (dump_file);
  196. isl_ctx_free (ctx);
  197. return false;
  198. }
  199. scev_reset ();
  200. recompute_all_dominators ();
  201. initialize_original_copy_tables ();
  202. if (dump_file && dump_flags)
  203. dump_function_to_file (current_function_decl, dump_file, dump_flags);
  204. return true;
  205. }
  206. /* Finalize graphite: perform CFG cleanup when NEED_CFG_CLEANUP_P is
  207. true. */
  208. static void
  209. graphite_finalize (bool need_cfg_cleanup_p)
  210. {
  211. if (need_cfg_cleanup_p)
  212. {
  213. scev_reset ();
  214. cleanup_tree_cfg ();
  215. profile_status_for_fn (cfun) = PROFILE_ABSENT;
  216. release_recorded_exits ();
  217. tree_estimate_probability ();
  218. }
  219. free_original_copy_tables ();
  220. if (dump_file && dump_flags)
  221. print_loops (dump_file, 3);
  222. }
  223. isl_ctx *the_isl_ctx;
  224. /* Perform a set of linear transforms on the loops of the current
  225. function. */
  226. void
  227. graphite_transform_loops (void)
  228. {
  229. int i;
  230. scop_p scop;
  231. bool need_cfg_cleanup_p = false;
  232. vec<scop_p> scops = vNULL;
  233. isl_ctx *ctx;
  234. /* If a function is parallel it was most probably already run through graphite
  235. once. No need to run again. */
  236. if (parallelized_function_p (cfun->decl))
  237. return;
  238. ctx = isl_ctx_alloc ();
  239. isl_options_set_on_error (ctx, ISL_ON_ERROR_ABORT);
  240. if (!graphite_initialize (ctx))
  241. return;
  242. the_isl_ctx = ctx;
  243. build_scops (&scops);
  244. if (dump_file && (dump_flags & TDF_DETAILS))
  245. {
  246. print_graphite_statistics (dump_file, scops);
  247. print_global_statistics (dump_file);
  248. }
  249. FOR_EACH_VEC_ELT (scops, i, scop)
  250. if (dbg_cnt (graphite_scop))
  251. {
  252. scop->ctx = ctx;
  253. build_poly_scop (scop);
  254. if (POLY_SCOP_P (scop)
  255. && apply_poly_transforms (scop)
  256. && graphite_regenerate_ast_isl (scop))
  257. need_cfg_cleanup_p = true;
  258. }
  259. free_scops (scops);
  260. graphite_finalize (need_cfg_cleanup_p);
  261. the_isl_ctx = NULL;
  262. isl_ctx_free (ctx);
  263. }
  264. #else /* If ISL is not available: #ifndef HAVE_isl. */
  265. static void
  266. graphite_transform_loops (void)
  267. {
  268. sorry ("Graphite loop optimizations cannot be used (ISL is not available).");
  269. }
  270. #endif
  271. static unsigned int
  272. graphite_transforms (struct function *fun)
  273. {
  274. if (number_of_loops (fun) <= 1)
  275. return 0;
  276. graphite_transform_loops ();
  277. return 0;
  278. }
  279. static bool
  280. gate_graphite_transforms (void)
  281. {
  282. /* Enable -fgraphite pass if any one of the graphite optimization flags
  283. is turned on. */
  284. if (flag_loop_block
  285. || flag_loop_interchange
  286. || flag_loop_strip_mine
  287. || flag_graphite_identity
  288. || flag_loop_parallelize_all
  289. || flag_loop_optimize_isl
  290. || flag_loop_unroll_jam)
  291. flag_graphite = 1;
  292. return flag_graphite != 0;
  293. }
  294. namespace {
  295. const pass_data pass_data_graphite =
  296. {
  297. GIMPLE_PASS, /* type */
  298. "graphite0", /* name */
  299. OPTGROUP_LOOP, /* optinfo_flags */
  300. TV_GRAPHITE, /* tv_id */
  301. ( PROP_cfg | PROP_ssa ), /* properties_required */
  302. 0, /* properties_provided */
  303. 0, /* properties_destroyed */
  304. 0, /* todo_flags_start */
  305. 0, /* todo_flags_finish */
  306. };
  307. class pass_graphite : public gimple_opt_pass
  308. {
  309. public:
  310. pass_graphite (gcc::context *ctxt)
  311. : gimple_opt_pass (pass_data_graphite, ctxt)
  312. {}
  313. /* opt_pass methods: */
  314. virtual bool gate (function *) { return gate_graphite_transforms (); }
  315. }; // class pass_graphite
  316. } // anon namespace
  317. gimple_opt_pass *
  318. make_pass_graphite (gcc::context *ctxt)
  319. {
  320. return new pass_graphite (ctxt);
  321. }
  322. namespace {
  323. const pass_data pass_data_graphite_transforms =
  324. {
  325. GIMPLE_PASS, /* type */
  326. "graphite", /* name */
  327. OPTGROUP_LOOP, /* optinfo_flags */
  328. TV_GRAPHITE_TRANSFORMS, /* tv_id */
  329. ( PROP_cfg | PROP_ssa ), /* properties_required */
  330. 0, /* properties_provided */
  331. 0, /* properties_destroyed */
  332. 0, /* todo_flags_start */
  333. 0, /* todo_flags_finish */
  334. };
  335. class pass_graphite_transforms : public gimple_opt_pass
  336. {
  337. public:
  338. pass_graphite_transforms (gcc::context *ctxt)
  339. : gimple_opt_pass (pass_data_graphite_transforms, ctxt)
  340. {}
  341. /* opt_pass methods: */
  342. virtual bool gate (function *) { return gate_graphite_transforms (); }
  343. virtual unsigned int execute (function *fun) { return graphite_transforms (fun); }
  344. }; // class pass_graphite_transforms
  345. } // anon namespace
  346. gimple_opt_pass *
  347. make_pass_graphite_transforms (gcc::context *ctxt)
  348. {
  349. return new pass_graphite_transforms (ctxt);
  350. }