tree-ssa-copyrename.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /* Rename SSA copies.
  2. Copyright (C) 2004-2015 Free Software Foundation, Inc.
  3. Contributed by Andrew MacLeod <amacleod@redhat.com>
  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. #include "config.h"
  17. #include "system.h"
  18. #include "coretypes.h"
  19. #include "tm.h"
  20. #include "hash-set.h"
  21. #include "machmode.h"
  22. #include "vec.h"
  23. #include "double-int.h"
  24. #include "input.h"
  25. #include "alias.h"
  26. #include "symtab.h"
  27. #include "wide-int.h"
  28. #include "inchash.h"
  29. #include "tree.h"
  30. #include "fold-const.h"
  31. #include "predict.h"
  32. #include "hard-reg-set.h"
  33. #include "function.h"
  34. #include "dominance.h"
  35. #include "cfg.h"
  36. #include "basic-block.h"
  37. #include "tree-ssa-alias.h"
  38. #include "internal-fn.h"
  39. #include "gimple-expr.h"
  40. #include "is-a.h"
  41. #include "gimple.h"
  42. #include "gimple-iterator.h"
  43. #include "flags.h"
  44. #include "tree-pretty-print.h"
  45. #include "bitmap.h"
  46. #include "gimple-ssa.h"
  47. #include "stringpool.h"
  48. #include "tree-ssanames.h"
  49. #include "hashtab.h"
  50. #include "rtl.h"
  51. #include "statistics.h"
  52. #include "real.h"
  53. #include "fixed-value.h"
  54. #include "insn-config.h"
  55. #include "expmed.h"
  56. #include "dojump.h"
  57. #include "explow.h"
  58. #include "calls.h"
  59. #include "emit-rtl.h"
  60. #include "varasm.h"
  61. #include "stmt.h"
  62. #include "expr.h"
  63. #include "tree-dfa.h"
  64. #include "tree-inline.h"
  65. #include "tree-ssa-live.h"
  66. #include "tree-pass.h"
  67. #include "langhooks.h"
  68. static struct
  69. {
  70. /* Number of copies coalesced. */
  71. int coalesced;
  72. } stats;
  73. /* The following routines implement the SSA copy renaming phase.
  74. This optimization looks for copies between 2 SSA_NAMES, either through a
  75. direct copy, or an implicit one via a PHI node result and its arguments.
  76. Each copy is examined to determine if it is possible to rename the base
  77. variable of one of the operands to the same variable as the other operand.
  78. i.e.
  79. T.3_5 = <blah>
  80. a_1 = T.3_5
  81. If this copy couldn't be copy propagated, it could possibly remain in the
  82. program throughout the optimization phases. After SSA->normal, it would
  83. become:
  84. T.3 = <blah>
  85. a = T.3
  86. Since T.3_5 is distinct from all other SSA versions of T.3, there is no
  87. fundamental reason why the base variable needs to be T.3, subject to
  88. certain restrictions. This optimization attempts to determine if we can
  89. change the base variable on copies like this, and result in code such as:
  90. a_5 = <blah>
  91. a_1 = a_5
  92. This gives the SSA->normal pass a shot at coalescing a_1 and a_5. If it is
  93. possible, the copy goes away completely. If it isn't possible, a new temp
  94. will be created for a_5, and you will end up with the exact same code:
  95. a.8 = <blah>
  96. a = a.8
  97. The other benefit of performing this optimization relates to what variables
  98. are chosen in copies. Gimplification of the program uses temporaries for
  99. a lot of things. expressions like
  100. a_1 = <blah>
  101. <blah2> = a_1
  102. get turned into
  103. T.3_5 = <blah>
  104. a_1 = T.3_5
  105. <blah2> = a_1
  106. Copy propagation is done in a forward direction, and if we can propagate
  107. through the copy, we end up with:
  108. T.3_5 = <blah>
  109. <blah2> = T.3_5
  110. The copy is gone, but so is all reference to the user variable 'a'. By
  111. performing this optimization, we would see the sequence:
  112. a_5 = <blah>
  113. a_1 = a_5
  114. <blah2> = a_1
  115. which copy propagation would then turn into:
  116. a_5 = <blah>
  117. <blah2> = a_5
  118. and so we still retain the user variable whenever possible. */
  119. /* Coalesce the partitions in MAP representing VAR1 and VAR2 if it is valid.
  120. Choose a representative for the partition, and send debug info to DEBUG. */
  121. static void
  122. copy_rename_partition_coalesce (var_map map, tree var1, tree var2, FILE *debug)
  123. {
  124. int p1, p2, p3;
  125. tree root1, root2;
  126. tree rep1, rep2;
  127. bool ign1, ign2, abnorm;
  128. gcc_assert (TREE_CODE (var1) == SSA_NAME);
  129. gcc_assert (TREE_CODE (var2) == SSA_NAME);
  130. register_ssa_partition (map, var1);
  131. register_ssa_partition (map, var2);
  132. p1 = partition_find (map->var_partition, SSA_NAME_VERSION (var1));
  133. p2 = partition_find (map->var_partition, SSA_NAME_VERSION (var2));
  134. if (debug)
  135. {
  136. fprintf (debug, "Try : ");
  137. print_generic_expr (debug, var1, TDF_SLIM);
  138. fprintf (debug, "(P%d) & ", p1);
  139. print_generic_expr (debug, var2, TDF_SLIM);
  140. fprintf (debug, "(P%d)", p2);
  141. }
  142. gcc_assert (p1 != NO_PARTITION);
  143. gcc_assert (p2 != NO_PARTITION);
  144. if (p1 == p2)
  145. {
  146. if (debug)
  147. fprintf (debug, " : Already coalesced.\n");
  148. return;
  149. }
  150. rep1 = partition_to_var (map, p1);
  151. rep2 = partition_to_var (map, p2);
  152. root1 = SSA_NAME_VAR (rep1);
  153. root2 = SSA_NAME_VAR (rep2);
  154. if (!root1 && !root2)
  155. return;
  156. /* Don't coalesce if one of the variables occurs in an abnormal PHI. */
  157. abnorm = (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rep1)
  158. || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rep2));
  159. if (abnorm)
  160. {
  161. if (debug)
  162. fprintf (debug, " : Abnormal PHI barrier. No coalesce.\n");
  163. return;
  164. }
  165. /* Partitions already have the same root, simply merge them. */
  166. if (root1 == root2)
  167. {
  168. p1 = partition_union (map->var_partition, p1, p2);
  169. if (debug)
  170. fprintf (debug, " : Same root, coalesced --> P%d.\n", p1);
  171. return;
  172. }
  173. /* Never attempt to coalesce 2 different parameters. */
  174. if ((root1 && TREE_CODE (root1) == PARM_DECL)
  175. && (root2 && TREE_CODE (root2) == PARM_DECL))
  176. {
  177. if (debug)
  178. fprintf (debug, " : 2 different PARM_DECLS. No coalesce.\n");
  179. return;
  180. }
  181. if ((root1 && TREE_CODE (root1) == RESULT_DECL)
  182. != (root2 && TREE_CODE (root2) == RESULT_DECL))
  183. {
  184. if (debug)
  185. fprintf (debug, " : One root a RESULT_DECL. No coalesce.\n");
  186. return;
  187. }
  188. ign1 = !root1 || (TREE_CODE (root1) == VAR_DECL && DECL_IGNORED_P (root1));
  189. ign2 = !root2 || (TREE_CODE (root2) == VAR_DECL && DECL_IGNORED_P (root2));
  190. /* Refrain from coalescing user variables, if requested. */
  191. if (!ign1 && !ign2)
  192. {
  193. if (flag_ssa_coalesce_vars && DECL_FROM_INLINE (root2))
  194. ign2 = true;
  195. else if (flag_ssa_coalesce_vars && DECL_FROM_INLINE (root1))
  196. ign1 = true;
  197. else if (flag_ssa_coalesce_vars != 2)
  198. {
  199. if (debug)
  200. fprintf (debug, " : 2 different USER vars. No coalesce.\n");
  201. return;
  202. }
  203. else
  204. ign2 = true;
  205. }
  206. /* If both values have default defs, we can't coalesce. If only one has a
  207. tag, make sure that variable is the new root partition. */
  208. if (root1 && ssa_default_def (cfun, root1))
  209. {
  210. if (root2 && ssa_default_def (cfun, root2))
  211. {
  212. if (debug)
  213. fprintf (debug, " : 2 default defs. No coalesce.\n");
  214. return;
  215. }
  216. else
  217. {
  218. ign2 = true;
  219. ign1 = false;
  220. }
  221. }
  222. else if (root2 && ssa_default_def (cfun, root2))
  223. {
  224. ign1 = true;
  225. ign2 = false;
  226. }
  227. /* Do not coalesce if we cannot assign a symbol to the partition. */
  228. if (!(!ign2 && root2)
  229. && !(!ign1 && root1))
  230. {
  231. if (debug)
  232. fprintf (debug, " : Choosen variable has no root. No coalesce.\n");
  233. return;
  234. }
  235. /* Don't coalesce if the new chosen root variable would be read-only.
  236. If both ign1 && ign2, then the root var of the larger partition
  237. wins, so reject in that case if any of the root vars is TREE_READONLY.
  238. Otherwise reject only if the root var, on which replace_ssa_name_symbol
  239. will be called below, is readonly. */
  240. if (((root1 && TREE_READONLY (root1)) && ign2)
  241. || ((root2 && TREE_READONLY (root2)) && ign1))
  242. {
  243. if (debug)
  244. fprintf (debug, " : Readonly variable. No coalesce.\n");
  245. return;
  246. }
  247. /* Don't coalesce if the two variables aren't type compatible . */
  248. if (!types_compatible_p (TREE_TYPE (var1), TREE_TYPE (var2))
  249. /* There is a disconnect between the middle-end type-system and
  250. VRP, avoid coalescing enum types with different bounds. */
  251. || ((TREE_CODE (TREE_TYPE (var1)) == ENUMERAL_TYPE
  252. || TREE_CODE (TREE_TYPE (var2)) == ENUMERAL_TYPE)
  253. && TREE_TYPE (var1) != TREE_TYPE (var2)))
  254. {
  255. if (debug)
  256. fprintf (debug, " : Incompatible types. No coalesce.\n");
  257. return;
  258. }
  259. /* Merge the two partitions. */
  260. p3 = partition_union (map->var_partition, p1, p2);
  261. /* Set the root variable of the partition to the better choice, if there is
  262. one. */
  263. if (!ign2 && root2)
  264. replace_ssa_name_symbol (partition_to_var (map, p3), root2);
  265. else if (!ign1 && root1)
  266. replace_ssa_name_symbol (partition_to_var (map, p3), root1);
  267. else
  268. gcc_unreachable ();
  269. if (debug)
  270. {
  271. fprintf (debug, " --> P%d ", p3);
  272. print_generic_expr (debug, SSA_NAME_VAR (partition_to_var (map, p3)),
  273. TDF_SLIM);
  274. fprintf (debug, "\n");
  275. }
  276. }
  277. namespace {
  278. const pass_data pass_data_rename_ssa_copies =
  279. {
  280. GIMPLE_PASS, /* type */
  281. "copyrename", /* name */
  282. OPTGROUP_NONE, /* optinfo_flags */
  283. TV_TREE_COPY_RENAME, /* tv_id */
  284. ( PROP_cfg | PROP_ssa ), /* properties_required */
  285. 0, /* properties_provided */
  286. 0, /* properties_destroyed */
  287. 0, /* todo_flags_start */
  288. 0, /* todo_flags_finish */
  289. };
  290. class pass_rename_ssa_copies : public gimple_opt_pass
  291. {
  292. public:
  293. pass_rename_ssa_copies (gcc::context *ctxt)
  294. : gimple_opt_pass (pass_data_rename_ssa_copies, ctxt)
  295. {}
  296. /* opt_pass methods: */
  297. opt_pass * clone () { return new pass_rename_ssa_copies (m_ctxt); }
  298. virtual bool gate (function *) { return flag_tree_copyrename != 0; }
  299. virtual unsigned int execute (function *);
  300. }; // class pass_rename_ssa_copies
  301. /* This function will make a pass through the IL, and attempt to coalesce any
  302. SSA versions which occur in PHI's or copies. Coalescing is accomplished by
  303. changing the underlying root variable of all coalesced version. This will
  304. then cause the SSA->normal pass to attempt to coalesce them all to the same
  305. variable. */
  306. unsigned int
  307. pass_rename_ssa_copies::execute (function *fun)
  308. {
  309. var_map map;
  310. basic_block bb;
  311. tree var, part_var;
  312. gimple stmt;
  313. unsigned x;
  314. FILE *debug;
  315. memset (&stats, 0, sizeof (stats));
  316. if (dump_file && (dump_flags & TDF_DETAILS))
  317. debug = dump_file;
  318. else
  319. debug = NULL;
  320. map = init_var_map (num_ssa_names);
  321. FOR_EACH_BB_FN (bb, fun)
  322. {
  323. /* Scan for real copies. */
  324. for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
  325. gsi_next (&gsi))
  326. {
  327. stmt = gsi_stmt (gsi);
  328. if (gimple_assign_ssa_name_copy_p (stmt))
  329. {
  330. tree lhs = gimple_assign_lhs (stmt);
  331. tree rhs = gimple_assign_rhs1 (stmt);
  332. copy_rename_partition_coalesce (map, lhs, rhs, debug);
  333. }
  334. }
  335. }
  336. FOR_EACH_BB_FN (bb, fun)
  337. {
  338. /* Treat PHI nodes as copies between the result and each argument. */
  339. for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
  340. gsi_next (&gsi))
  341. {
  342. size_t i;
  343. tree res;
  344. gphi *phi = gsi.phi ();
  345. res = gimple_phi_result (phi);
  346. /* Do not process virtual SSA_NAMES. */
  347. if (virtual_operand_p (res))
  348. continue;
  349. /* Make sure to only use the same partition for an argument
  350. as the result but never the other way around. */
  351. if (SSA_NAME_VAR (res)
  352. && !DECL_IGNORED_P (SSA_NAME_VAR (res)))
  353. for (i = 0; i < gimple_phi_num_args (phi); i++)
  354. {
  355. tree arg = PHI_ARG_DEF (phi, i);
  356. if (TREE_CODE (arg) == SSA_NAME)
  357. copy_rename_partition_coalesce (map, res, arg,
  358. debug);
  359. }
  360. /* Else if all arguments are in the same partition try to merge
  361. it with the result. */
  362. else
  363. {
  364. int all_p_same = -1;
  365. int p = -1;
  366. for (i = 0; i < gimple_phi_num_args (phi); i++)
  367. {
  368. tree arg = PHI_ARG_DEF (phi, i);
  369. if (TREE_CODE (arg) != SSA_NAME)
  370. {
  371. all_p_same = 0;
  372. break;
  373. }
  374. else if (all_p_same == -1)
  375. {
  376. p = partition_find (map->var_partition,
  377. SSA_NAME_VERSION (arg));
  378. all_p_same = 1;
  379. }
  380. else if (all_p_same == 1
  381. && p != partition_find (map->var_partition,
  382. SSA_NAME_VERSION (arg)))
  383. {
  384. all_p_same = 0;
  385. break;
  386. }
  387. }
  388. if (all_p_same == 1)
  389. copy_rename_partition_coalesce (map, res,
  390. PHI_ARG_DEF (phi, 0),
  391. debug);
  392. }
  393. }
  394. }
  395. if (debug)
  396. dump_var_map (debug, map);
  397. /* Now one more pass to make all elements of a partition share the same
  398. root variable. */
  399. for (x = 1; x < num_ssa_names; x++)
  400. {
  401. part_var = partition_to_var (map, x);
  402. if (!part_var)
  403. continue;
  404. var = ssa_name (x);
  405. if (SSA_NAME_VAR (var) == SSA_NAME_VAR (part_var))
  406. continue;
  407. if (debug)
  408. {
  409. fprintf (debug, "Coalesced ");
  410. print_generic_expr (debug, var, TDF_SLIM);
  411. fprintf (debug, " to ");
  412. print_generic_expr (debug, part_var, TDF_SLIM);
  413. fprintf (debug, "\n");
  414. }
  415. stats.coalesced++;
  416. replace_ssa_name_symbol (var, SSA_NAME_VAR (part_var));
  417. }
  418. statistics_counter_event (fun, "copies coalesced",
  419. stats.coalesced);
  420. delete_var_map (map);
  421. return 0;
  422. }
  423. } // anon namespace
  424. gimple_opt_pass *
  425. make_pass_rename_ssa_copies (gcc::context *ctxt)
  426. {
  427. return new pass_rename_ssa_copies (ctxt);
  428. }