tree-ssa-uncprop.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /* Routines for discovering and unpropagating edge equivalences.
  2. Copyright (C) 2005-2015 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. GCC is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GCC; see the file COPYING3. If not see
  14. <http://www.gnu.org/licenses/>. */
  15. #include "config.h"
  16. #include "system.h"
  17. #include "coretypes.h"
  18. #include "tm.h"
  19. #include "hash-set.h"
  20. #include "machmode.h"
  21. #include "vec.h"
  22. #include "double-int.h"
  23. #include "input.h"
  24. #include "alias.h"
  25. #include "symtab.h"
  26. #include "wide-int.h"
  27. #include "inchash.h"
  28. #include "real.h"
  29. #include "tree.h"
  30. #include "fold-const.h"
  31. #include "stor-layout.h"
  32. #include "flags.h"
  33. #include "tm_p.h"
  34. #include "predict.h"
  35. #include "hard-reg-set.h"
  36. #include "input.h"
  37. #include "function.h"
  38. #include "dominance.h"
  39. #include "cfg.h"
  40. #include "cfganal.h"
  41. #include "basic-block.h"
  42. #include "hash-table.h"
  43. #include "hash-map.h"
  44. #include "tree-ssa-alias.h"
  45. #include "internal-fn.h"
  46. #include "gimple-expr.h"
  47. #include "is-a.h"
  48. #include "gimple.h"
  49. #include "gimple-iterator.h"
  50. #include "gimple-ssa.h"
  51. #include "tree-cfg.h"
  52. #include "tree-phinodes.h"
  53. #include "ssa-iterators.h"
  54. #include "domwalk.h"
  55. #include "tree-pass.h"
  56. #include "tree-ssa-propagate.h"
  57. /* The basic structure describing an equivalency created by traversing
  58. an edge. Traversing the edge effectively means that we can assume
  59. that we've seen an assignment LHS = RHS. */
  60. struct edge_equivalency
  61. {
  62. tree rhs;
  63. tree lhs;
  64. };
  65. /* This routine finds and records edge equivalences for every edge
  66. in the CFG.
  67. When complete, each edge that creates an equivalency will have an
  68. EDGE_EQUIVALENCY structure hanging off the edge's AUX field.
  69. The caller is responsible for freeing the AUX fields. */
  70. static void
  71. associate_equivalences_with_edges (void)
  72. {
  73. basic_block bb;
  74. /* Walk over each block. If the block ends with a control statement,
  75. then it might create a useful equivalence. */
  76. FOR_EACH_BB_FN (bb, cfun)
  77. {
  78. gimple_stmt_iterator gsi = gsi_last_bb (bb);
  79. gimple stmt;
  80. /* If the block does not end with a COND_EXPR or SWITCH_EXPR
  81. then there is nothing to do. */
  82. if (gsi_end_p (gsi))
  83. continue;
  84. stmt = gsi_stmt (gsi);
  85. if (!stmt)
  86. continue;
  87. /* A COND_EXPR may create an equivalency in a variety of different
  88. ways. */
  89. if (gimple_code (stmt) == GIMPLE_COND)
  90. {
  91. edge true_edge;
  92. edge false_edge;
  93. struct edge_equivalency *equivalency;
  94. enum tree_code code = gimple_cond_code (stmt);
  95. extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
  96. /* Equality tests may create one or two equivalences. */
  97. if (code == EQ_EXPR || code == NE_EXPR)
  98. {
  99. tree op0 = gimple_cond_lhs (stmt);
  100. tree op1 = gimple_cond_rhs (stmt);
  101. /* Special case comparing booleans against a constant as we
  102. know the value of OP0 on both arms of the branch. i.e., we
  103. can record an equivalence for OP0 rather than COND. */
  104. if (TREE_CODE (op0) == SSA_NAME
  105. && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op0)
  106. && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
  107. && is_gimple_min_invariant (op1))
  108. {
  109. if (code == EQ_EXPR)
  110. {
  111. equivalency = XNEW (struct edge_equivalency);
  112. equivalency->lhs = op0;
  113. equivalency->rhs = (integer_zerop (op1)
  114. ? boolean_false_node
  115. : boolean_true_node);
  116. true_edge->aux = equivalency;
  117. equivalency = XNEW (struct edge_equivalency);
  118. equivalency->lhs = op0;
  119. equivalency->rhs = (integer_zerop (op1)
  120. ? boolean_true_node
  121. : boolean_false_node);
  122. false_edge->aux = equivalency;
  123. }
  124. else
  125. {
  126. equivalency = XNEW (struct edge_equivalency);
  127. equivalency->lhs = op0;
  128. equivalency->rhs = (integer_zerop (op1)
  129. ? boolean_true_node
  130. : boolean_false_node);
  131. true_edge->aux = equivalency;
  132. equivalency = XNEW (struct edge_equivalency);
  133. equivalency->lhs = op0;
  134. equivalency->rhs = (integer_zerop (op1)
  135. ? boolean_false_node
  136. : boolean_true_node);
  137. false_edge->aux = equivalency;
  138. }
  139. }
  140. else if (TREE_CODE (op0) == SSA_NAME
  141. && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op0)
  142. && (is_gimple_min_invariant (op1)
  143. || (TREE_CODE (op1) == SSA_NAME
  144. && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op1))))
  145. {
  146. /* For IEEE, -0.0 == 0.0, so we don't necessarily know
  147. the sign of a variable compared against zero. If
  148. we're honoring signed zeros, then we cannot record
  149. this value unless we know that the value is nonzero. */
  150. if (HONOR_SIGNED_ZEROS (op0)
  151. && (TREE_CODE (op1) != REAL_CST
  152. || REAL_VALUES_EQUAL (dconst0, TREE_REAL_CST (op1))))
  153. continue;
  154. equivalency = XNEW (struct edge_equivalency);
  155. equivalency->lhs = op0;
  156. equivalency->rhs = op1;
  157. if (code == EQ_EXPR)
  158. true_edge->aux = equivalency;
  159. else
  160. false_edge->aux = equivalency;
  161. }
  162. }
  163. /* ??? TRUTH_NOT_EXPR can create an equivalence too. */
  164. }
  165. /* For a SWITCH_EXPR, a case label which represents a single
  166. value and which is the only case label which reaches the
  167. target block creates an equivalence. */
  168. else if (gimple_code (stmt) == GIMPLE_SWITCH)
  169. {
  170. gswitch *switch_stmt = as_a <gswitch *> (stmt);
  171. tree cond = gimple_switch_index (switch_stmt);
  172. if (TREE_CODE (cond) == SSA_NAME
  173. && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (cond))
  174. {
  175. int i, n_labels = gimple_switch_num_labels (switch_stmt);
  176. tree *info = XCNEWVEC (tree, last_basic_block_for_fn (cfun));
  177. /* Walk over the case label vector. Record blocks
  178. which are reached by a single case label which represents
  179. a single value. */
  180. for (i = 0; i < n_labels; i++)
  181. {
  182. tree label = gimple_switch_label (switch_stmt, i);
  183. basic_block bb = label_to_block (CASE_LABEL (label));
  184. if (CASE_HIGH (label)
  185. || !CASE_LOW (label)
  186. || info[bb->index])
  187. info[bb->index] = error_mark_node;
  188. else
  189. info[bb->index] = label;
  190. }
  191. /* Now walk over the blocks to determine which ones were
  192. marked as being reached by a useful case label. */
  193. for (i = 0; i < n_basic_blocks_for_fn (cfun); i++)
  194. {
  195. tree node = info[i];
  196. if (node != NULL
  197. && node != error_mark_node)
  198. {
  199. tree x = fold_convert (TREE_TYPE (cond), CASE_LOW (node));
  200. struct edge_equivalency *equivalency;
  201. /* Record an equivalency on the edge from BB to basic
  202. block I. */
  203. equivalency = XNEW (struct edge_equivalency);
  204. equivalency->rhs = x;
  205. equivalency->lhs = cond;
  206. find_edge (bb, BASIC_BLOCK_FOR_FN (cfun, i))->aux =
  207. equivalency;
  208. }
  209. }
  210. free (info);
  211. }
  212. }
  213. }
  214. }
  215. /* Translating out of SSA sometimes requires inserting copies and
  216. constant initializations on edges to eliminate PHI nodes.
  217. In some cases those copies and constant initializations are
  218. redundant because the target already has the value on the
  219. RHS of the assignment.
  220. We previously tried to catch these cases after translating
  221. out of SSA form. However, that code often missed cases. Worse
  222. yet, the cases it missed were also often missed by the RTL
  223. optimizers. Thus the resulting code had redundant instructions.
  224. This pass attempts to detect these situations before translating
  225. out of SSA form.
  226. The key concept that this pass is built upon is that these
  227. redundant copies and constant initializations often occur
  228. due to constant/copy propagating equivalences resulting from
  229. COND_EXPRs and SWITCH_EXPRs.
  230. We want to do those propagations as they can sometimes allow
  231. the SSA optimizers to do a better job. However, in the cases
  232. where such propagations do not result in further optimization,
  233. we would like to "undo" the propagation to avoid the redundant
  234. copies and constant initializations.
  235. This pass works by first associating equivalences with edges in
  236. the CFG. For example, the edge leading from a SWITCH_EXPR to
  237. its associated CASE_LABEL will have an equivalency between
  238. SWITCH_COND and the value in the case label.
  239. Once we have found the edge equivalences, we proceed to walk
  240. the CFG in dominator order. As we traverse edges we record
  241. equivalences associated with those edges we traverse.
  242. When we encounter a PHI node, we walk its arguments to see if we
  243. have an equivalence for the PHI argument. If so, then we replace
  244. the argument.
  245. Equivalences are looked up based on their value (think of it as
  246. the RHS of an assignment). A value may be an SSA_NAME or an
  247. invariant. We may have several SSA_NAMEs with the same value,
  248. so with each value we have a list of SSA_NAMEs that have the
  249. same value. */
  250. /* Main structure for recording equivalences into our hash table. */
  251. struct equiv_hash_elt
  252. {
  253. /* The value/key of this entry. */
  254. tree value;
  255. /* List of SSA_NAMEs which have the same value/key. */
  256. vec<tree> equivalences;
  257. };
  258. /* Value to ssa name equivalence hashtable helpers. */
  259. struct val_ssa_equiv_hash_traits : default_hashmap_traits
  260. {
  261. static inline hashval_t hash (tree);
  262. static inline bool equal_keys (tree, tree);
  263. template<typename T> static inline void remove (T &);
  264. };
  265. inline hashval_t
  266. val_ssa_equiv_hash_traits::hash (tree value)
  267. {
  268. return iterative_hash_expr (value, 0);
  269. }
  270. inline bool
  271. val_ssa_equiv_hash_traits::equal_keys (tree value1, tree value2)
  272. {
  273. return operand_equal_p (value1, value2, 0);
  274. }
  275. /* Free an instance of equiv_hash_elt. */
  276. template<typename T>
  277. inline void
  278. val_ssa_equiv_hash_traits::remove (T &elt)
  279. {
  280. elt.m_value.release ();
  281. }
  282. /* Global hash table implementing a mapping from invariant values
  283. to a list of SSA_NAMEs which have the same value. We might be
  284. able to reuse tree-vn for this code. */
  285. static hash_map<tree, vec<tree>, val_ssa_equiv_hash_traits> *val_ssa_equiv;
  286. static void uncprop_into_successor_phis (basic_block);
  287. /* Remove the most recently recorded equivalency for VALUE. */
  288. static void
  289. remove_equivalence (tree value)
  290. {
  291. val_ssa_equiv->get (value)->pop ();
  292. }
  293. /* Record EQUIVALENCE = VALUE into our hash table. */
  294. static void
  295. record_equiv (tree value, tree equivalence)
  296. {
  297. val_ssa_equiv->get_or_insert (value).safe_push (equivalence);
  298. }
  299. class uncprop_dom_walker : public dom_walker
  300. {
  301. public:
  302. uncprop_dom_walker (cdi_direction direction) : dom_walker (direction) {}
  303. virtual void before_dom_children (basic_block);
  304. virtual void after_dom_children (basic_block);
  305. private:
  306. /* As we enter each block we record the value for any edge equivalency
  307. leading to this block. If no such edge equivalency exists, then we
  308. record NULL. These equivalences are live until we leave the dominator
  309. subtree rooted at the block where we record the equivalency. */
  310. auto_vec<tree, 2> m_equiv_stack;
  311. };
  312. /* We have finished processing the dominator children of BB, perform
  313. any finalization actions in preparation for leaving this node in
  314. the dominator tree. */
  315. void
  316. uncprop_dom_walker::after_dom_children (basic_block bb ATTRIBUTE_UNUSED)
  317. {
  318. /* Pop the topmost value off the equiv stack. */
  319. tree value = m_equiv_stack.pop ();
  320. /* If that value was non-null, then pop the topmost equivalency off
  321. its equivalency stack. */
  322. if (value != NULL)
  323. remove_equivalence (value);
  324. }
  325. /* Unpropagate values from PHI nodes in successor blocks of BB. */
  326. static void
  327. uncprop_into_successor_phis (basic_block bb)
  328. {
  329. edge e;
  330. edge_iterator ei;
  331. /* For each successor edge, first temporarily record any equivalence
  332. on that edge. Then unpropagate values in any PHI nodes at the
  333. destination of the edge. Then remove the temporary equivalence. */
  334. FOR_EACH_EDGE (e, ei, bb->succs)
  335. {
  336. gimple_seq phis = phi_nodes (e->dest);
  337. gimple_stmt_iterator gsi;
  338. /* If there are no PHI nodes in this destination, then there is
  339. no sense in recording any equivalences. */
  340. if (gimple_seq_empty_p (phis))
  341. continue;
  342. /* Record any equivalency associated with E. */
  343. if (e->aux)
  344. {
  345. struct edge_equivalency *equiv = (struct edge_equivalency *) e->aux;
  346. record_equiv (equiv->rhs, equiv->lhs);
  347. }
  348. /* Walk over the PHI nodes, unpropagating values. */
  349. for (gsi = gsi_start (phis) ; !gsi_end_p (gsi); gsi_next (&gsi))
  350. {
  351. gimple phi = gsi_stmt (gsi);
  352. tree arg = PHI_ARG_DEF (phi, e->dest_idx);
  353. tree res = PHI_RESULT (phi);
  354. /* If the argument is not an invariant and can be potentially
  355. coalesced with the result, then there's no point in
  356. un-propagating the argument. */
  357. if (!is_gimple_min_invariant (arg)
  358. && gimple_can_coalesce_p (arg, res))
  359. continue;
  360. /* Lookup this argument's value in the hash table. */
  361. vec<tree> *equivalences = val_ssa_equiv->get (arg);
  362. if (equivalences)
  363. {
  364. /* Walk every equivalence with the same value. If we find
  365. one that can potentially coalesce with the PHI rsult,
  366. then replace the value in the argument with its equivalent
  367. SSA_NAME. Use the most recent equivalence as hopefully
  368. that results in shortest lifetimes. */
  369. for (int j = equivalences->length () - 1; j >= 0; j--)
  370. {
  371. tree equiv = (*equivalences)[j];
  372. if (gimple_can_coalesce_p (equiv, res))
  373. {
  374. SET_PHI_ARG_DEF (phi, e->dest_idx, equiv);
  375. break;
  376. }
  377. }
  378. }
  379. }
  380. /* If we had an equivalence associated with this edge, remove it. */
  381. if (e->aux)
  382. {
  383. struct edge_equivalency *equiv = (struct edge_equivalency *) e->aux;
  384. remove_equivalence (equiv->rhs);
  385. }
  386. }
  387. }
  388. /* Ignoring loop backedges, if BB has precisely one incoming edge then
  389. return that edge. Otherwise return NULL. */
  390. static edge
  391. single_incoming_edge_ignoring_loop_edges (basic_block bb)
  392. {
  393. edge retval = NULL;
  394. edge e;
  395. edge_iterator ei;
  396. FOR_EACH_EDGE (e, ei, bb->preds)
  397. {
  398. /* A loop back edge can be identified by the destination of
  399. the edge dominating the source of the edge. */
  400. if (dominated_by_p (CDI_DOMINATORS, e->src, e->dest))
  401. continue;
  402. /* If we have already seen a non-loop edge, then we must have
  403. multiple incoming non-loop edges and thus we return NULL. */
  404. if (retval)
  405. return NULL;
  406. /* This is the first non-loop incoming edge we have found. Record
  407. it. */
  408. retval = e;
  409. }
  410. return retval;
  411. }
  412. void
  413. uncprop_dom_walker::before_dom_children (basic_block bb)
  414. {
  415. basic_block parent;
  416. edge e;
  417. bool recorded = false;
  418. /* If this block is dominated by a single incoming edge and that edge
  419. has an equivalency, then record the equivalency and push the
  420. VALUE onto EQUIV_STACK. Else push a NULL entry on EQUIV_STACK. */
  421. parent = get_immediate_dominator (CDI_DOMINATORS, bb);
  422. if (parent)
  423. {
  424. e = single_incoming_edge_ignoring_loop_edges (bb);
  425. if (e && e->src == parent && e->aux)
  426. {
  427. struct edge_equivalency *equiv = (struct edge_equivalency *) e->aux;
  428. record_equiv (equiv->rhs, equiv->lhs);
  429. m_equiv_stack.safe_push (equiv->rhs);
  430. recorded = true;
  431. }
  432. }
  433. if (!recorded)
  434. m_equiv_stack.safe_push (NULL_TREE);
  435. uncprop_into_successor_phis (bb);
  436. }
  437. namespace {
  438. const pass_data pass_data_uncprop =
  439. {
  440. GIMPLE_PASS, /* type */
  441. "uncprop", /* name */
  442. OPTGROUP_NONE, /* optinfo_flags */
  443. TV_TREE_SSA_UNCPROP, /* tv_id */
  444. ( PROP_cfg | PROP_ssa ), /* properties_required */
  445. 0, /* properties_provided */
  446. 0, /* properties_destroyed */
  447. 0, /* todo_flags_start */
  448. 0, /* todo_flags_finish */
  449. };
  450. class pass_uncprop : public gimple_opt_pass
  451. {
  452. public:
  453. pass_uncprop (gcc::context *ctxt)
  454. : gimple_opt_pass (pass_data_uncprop, ctxt)
  455. {}
  456. /* opt_pass methods: */
  457. opt_pass * clone () { return new pass_uncprop (m_ctxt); }
  458. virtual bool gate (function *) { return flag_tree_dom != 0; }
  459. virtual unsigned int execute (function *);
  460. }; // class pass_uncprop
  461. unsigned int
  462. pass_uncprop::execute (function *fun)
  463. {
  464. basic_block bb;
  465. associate_equivalences_with_edges ();
  466. /* Create our global data structures. */
  467. val_ssa_equiv
  468. = new hash_map<tree, vec<tree>, val_ssa_equiv_hash_traits> (1024);
  469. /* We're going to do a dominator walk, so ensure that we have
  470. dominance information. */
  471. calculate_dominance_info (CDI_DOMINATORS);
  472. /* Recursively walk the dominator tree undoing unprofitable
  473. constant/copy propagations. */
  474. uncprop_dom_walker (CDI_DOMINATORS).walk (fun->cfg->x_entry_block_ptr);
  475. /* we just need to empty elements out of the hash table, and cleanup the
  476. AUX field on the edges. */
  477. delete val_ssa_equiv;
  478. val_ssa_equiv = NULL;
  479. FOR_EACH_BB_FN (bb, fun)
  480. {
  481. edge e;
  482. edge_iterator ei;
  483. FOR_EACH_EDGE (e, ei, bb->succs)
  484. {
  485. if (e->aux)
  486. {
  487. free (e->aux);
  488. e->aux = NULL;
  489. }
  490. }
  491. }
  492. return 0;
  493. }
  494. } // anon namespace
  495. gimple_opt_pass *
  496. make_pass_uncprop (gcc::context *ctxt)
  497. {
  498. return new pass_uncprop (ctxt);
  499. }