tree-ssa-ter.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827
  1. /* Routines for performing Temporary Expression Replacement (TER) in SSA trees.
  2. Copyright (C) 2003-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 "gimple-pretty-print.h"
  32. #include "bitmap.h"
  33. #include "predict.h"
  34. #include "vec.h"
  35. #include "hashtab.h"
  36. #include "hash-set.h"
  37. #include "machmode.h"
  38. #include "hard-reg-set.h"
  39. #include "input.h"
  40. #include "function.h"
  41. #include "dominance.h"
  42. #include "cfg.h"
  43. #include "basic-block.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-phinodes.h"
  52. #include "ssa-iterators.h"
  53. #include "stringpool.h"
  54. #include "tree-ssanames.h"
  55. #include "dumpfile.h"
  56. #include "tree-ssa-live.h"
  57. #include "tree-ssa-ter.h"
  58. #include "tree-outof-ssa.h"
  59. #include "flags.h"
  60. #include "gimple-walk.h"
  61. /* Temporary Expression Replacement (TER)
  62. Replace SSA version variables during out-of-ssa with their defining
  63. expression if there is only one use of the variable.
  64. This pass is required in order for the RTL expansion pass to see larger
  65. chunks of code. This allows it to make better choices on RTL pattern
  66. selection. When expand is rewritten and merged with out-of-ssa, and
  67. understands SSA, this should be eliminated.
  68. A pass is made through the function, one block at a time. No cross block
  69. information is tracked.
  70. Variables which only have one use, and whose defining stmt is considered
  71. a replaceable expression (see ssa_is_replaceable_p) are tracked to see whether
  72. they can be replaced at their use location.
  73. n_12 = C * 10
  74. a_2 = b_5 + 6
  75. v_9 = a_2 * n_12
  76. if there are the only use of n_12 and a_2, TER will substitute in their
  77. expressions in v_9, and end up with:
  78. v_9 = (b_5 + 6) * (C * 10)
  79. which will then have the ssa_name assigned to regular variables, and the
  80. resulting code which will be passed to the expander looks something like:
  81. v = (b + 6) * (C * 10)
  82. This requires ensuring that none of the variables used by the expression
  83. change between the def point and where it is used. Furthermore, if any
  84. of the ssa_names used in this expression are themselves replaceable, we
  85. have to ensure none of that expressions' arguments change either.
  86. Although SSA_NAMES themselves don't change, this pass is performed after
  87. coalescing has coalesced different SSA_NAMES together, so there could be a
  88. definition of an SSA_NAME which is coalesced with a use that causes a
  89. problem, i.e.,
  90. PHI b_5 = <b_8(2), b_14(1)>
  91. <...>
  92. a_2 = b_5 + 6
  93. b_8 = c_4 + 4
  94. v_9 = a_2 * n_12
  95. <...>
  96. If b_5, b_8 and b_14 are all coalesced together...
  97. The expression b_5 + 6 CANNOT replace the use in the statement defining v_9
  98. because b_8 is in fact killing the value of b_5 since they share a partition
  99. and will be assigned the same memory or register location.
  100. TER implements this but stepping through the instructions in a block and
  101. tracking potential expressions for replacement, and the partitions they are
  102. dependent on. Expressions are represented by the SSA_NAME_VERSION of the
  103. DEF on the LHS of a GIMPLE_ASSIGN and the expression is the RHS.
  104. When a stmt is determined to be a possible replacement expression, the
  105. following steps are taken:
  106. EXPR_DECL_UID bitmap is allocated and set to the base variable UID of the
  107. def and any uses in the expression. non-NULL means the expression is being
  108. tracked. The UID's themselves are used to prevent TER substitution into
  109. accumulating sequences, i.e.,
  110. x = x + y
  111. x = x + z
  112. x = x + w
  113. etc.
  114. this can result in very large expressions which don't accomplish anything
  115. see PR tree-optimization/17549.
  116. PARTITION_DEPENDENCIES is another bitmap array, and it has a bit set for any
  117. partition which is used in the expression. This is primarily used to remove
  118. an expression from the partition kill lists when a decision is made whether
  119. to replace it or not. This is indexed by ssa version number as well, and
  120. indicates a partition number. virtual operands are not tracked individually,
  121. but they are summarized by an artificial partition called VIRTUAL_PARTITION.
  122. This means a MAY or MUST def will kill *ALL* expressions that are dependent
  123. on a virtual operand.
  124. Note that the EXPR_DECL_UID and this bitmap represent very similar
  125. information, but the info in one is not easy to obtain from the other.
  126. KILL_LIST is yet another bitmap array, this time it is indexed by partition
  127. number, and represents a list of active expressions which will will no
  128. longer be valid if a definition into this partition takes place.
  129. PARTITION_IN_USE is simply a bitmap which is used to track which partitions
  130. currently have something in their kill list. This is used at the end of
  131. a block to clear out the KILL_LIST bitmaps at the end of each block.
  132. NEW_REPLACEABLE_DEPENDENCIES is used as a temporary place to store
  133. dependencies which will be reused by the current definition. All the uses
  134. on an expression are processed before anything else is done. If a use is
  135. determined to be a replaceable expression AND the current stmt is also going
  136. to be replaceable, all the dependencies of this replaceable use will be
  137. picked up by the current stmt's expression. Rather than recreate them, they
  138. are simply copied here and then copied into the new expression when it is
  139. processed.
  140. a_2 = b_5 + 6
  141. v_8 = a_2 + c_4
  142. a_2's expression 'b_5 + 6' is determined to be replaceable at the use
  143. location. It is dependent on the partition 'b_5' is in. This is cached into
  144. the NEW_REPLACEABLE_DEPENDENCIES bitmap, and when v_8 is examined for
  145. replaceability, it is a candidate, and it is dependent on the partition
  146. b_5 is in *NOT* a_2, as well as c_4's partition.
  147. if v_8 is also replaceable:
  148. x_9 = v_8 * 5
  149. x_9 is dependent on partitions b_5, and c_4
  150. if a statement is found which has either of those partitions written to
  151. before x_9 is used, then x_9 itself is NOT replaceable. */
  152. /* Temporary Expression Replacement (TER) table information. */
  153. typedef struct temp_expr_table_d
  154. {
  155. var_map map;
  156. bitmap *partition_dependencies; /* Partitions expr is dependent on. */
  157. bitmap replaceable_expressions; /* Replacement expression table. */
  158. bitmap *expr_decl_uids; /* Base uids of exprs. */
  159. bitmap *kill_list; /* Expr's killed by a partition. */
  160. int virtual_partition; /* Pseudo partition for virtual ops. */
  161. bitmap partition_in_use; /* Partitions with kill entries. */
  162. bitmap new_replaceable_dependencies; /* Holding place for pending dep's. */
  163. int *num_in_part; /* # of ssa_names in a partition. */
  164. int *call_cnt; /* Call count at definition. */
  165. } *temp_expr_table_p;
  166. /* Used to indicate a dependency on VDEFs. */
  167. #define VIRTUAL_PARTITION(table) (table->virtual_partition)
  168. /* A place for the many, many bitmaps we create. */
  169. static bitmap_obstack ter_bitmap_obstack;
  170. #ifdef ENABLE_CHECKING
  171. extern void debug_ter (FILE *, temp_expr_table_p);
  172. #endif
  173. /* Create a new TER table for MAP. */
  174. static temp_expr_table_p
  175. new_temp_expr_table (var_map map)
  176. {
  177. temp_expr_table_p t;
  178. unsigned x;
  179. t = XNEW (struct temp_expr_table_d);
  180. t->map = map;
  181. t->partition_dependencies = XCNEWVEC (bitmap, num_ssa_names + 1);
  182. t->expr_decl_uids = XCNEWVEC (bitmap, num_ssa_names + 1);
  183. t->kill_list = XCNEWVEC (bitmap, num_var_partitions (map) + 1);
  184. t->partition_in_use = BITMAP_ALLOC (&ter_bitmap_obstack);
  185. t->virtual_partition = num_var_partitions (map);
  186. t->new_replaceable_dependencies = BITMAP_ALLOC (&ter_bitmap_obstack);
  187. t->replaceable_expressions = NULL;
  188. t->num_in_part = XCNEWVEC (int, num_var_partitions (map));
  189. for (x = 1; x < num_ssa_names; x++)
  190. {
  191. int p;
  192. tree name = ssa_name (x);
  193. if (!name)
  194. continue;
  195. p = var_to_partition (map, name);
  196. if (p != NO_PARTITION)
  197. t->num_in_part[p]++;
  198. }
  199. t->call_cnt = XCNEWVEC (int, num_ssa_names + 1);
  200. return t;
  201. }
  202. /* Free TER table T. If there are valid replacements, return the expression
  203. vector. */
  204. static bitmap
  205. free_temp_expr_table (temp_expr_table_p t)
  206. {
  207. bitmap ret = NULL;
  208. #ifdef ENABLE_CHECKING
  209. unsigned x;
  210. for (x = 0; x <= num_var_partitions (t->map); x++)
  211. gcc_assert (!t->kill_list[x]);
  212. for (x = 0; x < num_ssa_names; x++)
  213. {
  214. gcc_assert (t->expr_decl_uids[x] == NULL);
  215. gcc_assert (t->partition_dependencies[x] == NULL);
  216. }
  217. #endif
  218. BITMAP_FREE (t->partition_in_use);
  219. BITMAP_FREE (t->new_replaceable_dependencies);
  220. free (t->expr_decl_uids);
  221. free (t->kill_list);
  222. free (t->partition_dependencies);
  223. free (t->num_in_part);
  224. free (t->call_cnt);
  225. if (t->replaceable_expressions)
  226. ret = t->replaceable_expressions;
  227. free (t);
  228. return ret;
  229. }
  230. /* Return TRUE if VERSION is to be replaced by an expression in TAB. */
  231. static inline bool
  232. version_to_be_replaced_p (temp_expr_table_p tab, int version)
  233. {
  234. if (!tab->replaceable_expressions)
  235. return false;
  236. return bitmap_bit_p (tab->replaceable_expressions, version);
  237. }
  238. /* Add partition P to the list if partitions VERSION is dependent on. TAB is
  239. the expression table */
  240. static inline void
  241. make_dependent_on_partition (temp_expr_table_p tab, int version, int p)
  242. {
  243. if (!tab->partition_dependencies[version])
  244. tab->partition_dependencies[version] = BITMAP_ALLOC (&ter_bitmap_obstack);
  245. bitmap_set_bit (tab->partition_dependencies[version], p);
  246. }
  247. /* Add VER to the kill list for P. TAB is the expression table */
  248. static inline void
  249. add_to_partition_kill_list (temp_expr_table_p tab, int p, int ver)
  250. {
  251. if (!tab->kill_list[p])
  252. {
  253. tab->kill_list[p] = BITMAP_ALLOC (&ter_bitmap_obstack);
  254. bitmap_set_bit (tab->partition_in_use, p);
  255. }
  256. bitmap_set_bit (tab->kill_list[p], ver);
  257. }
  258. /* Remove VER from the partition kill list for P. TAB is the expression
  259. table. */
  260. static inline void
  261. remove_from_partition_kill_list (temp_expr_table_p tab, int p, int version)
  262. {
  263. gcc_checking_assert (tab->kill_list[p]);
  264. bitmap_clear_bit (tab->kill_list[p], version);
  265. if (bitmap_empty_p (tab->kill_list[p]))
  266. {
  267. bitmap_clear_bit (tab->partition_in_use, p);
  268. BITMAP_FREE (tab->kill_list[p]);
  269. }
  270. }
  271. /* Add a dependency between the def of ssa VERSION and VAR. If VAR is
  272. replaceable by an expression, add a dependence each of the elements of the
  273. expression. These are contained in the new_replaceable list. TAB is the
  274. expression table. */
  275. static void
  276. add_dependence (temp_expr_table_p tab, int version, tree var)
  277. {
  278. int i;
  279. bitmap_iterator bi;
  280. unsigned x;
  281. i = SSA_NAME_VERSION (var);
  282. if (version_to_be_replaced_p (tab, i))
  283. {
  284. if (!bitmap_empty_p (tab->new_replaceable_dependencies))
  285. {
  286. /* Version will now be killed by a write to any partition the
  287. substituted expression would have been killed by. */
  288. EXECUTE_IF_SET_IN_BITMAP (tab->new_replaceable_dependencies, 0, x, bi)
  289. add_to_partition_kill_list (tab, x, version);
  290. /* Rather than set partition_dependencies and in_use lists bit by
  291. bit, simply OR in the new_replaceable_dependencies bits. */
  292. if (!tab->partition_dependencies[version])
  293. tab->partition_dependencies[version] =
  294. BITMAP_ALLOC (&ter_bitmap_obstack);
  295. bitmap_ior_into (tab->partition_dependencies[version],
  296. tab->new_replaceable_dependencies);
  297. bitmap_ior_into (tab->partition_in_use,
  298. tab->new_replaceable_dependencies);
  299. /* It is only necessary to add these once. */
  300. bitmap_clear (tab->new_replaceable_dependencies);
  301. }
  302. }
  303. else
  304. {
  305. i = var_to_partition (tab->map, var);
  306. gcc_checking_assert (i != NO_PARTITION);
  307. gcc_checking_assert (tab->num_in_part[i] != 0);
  308. /* Only dependencies on ssa_names which are coalesced with something need
  309. to be tracked. Partitions with containing only a single SSA_NAME
  310. *cannot* have their value changed. */
  311. if (tab->num_in_part[i] > 1)
  312. {
  313. add_to_partition_kill_list (tab, i, version);
  314. make_dependent_on_partition (tab, version, i);
  315. }
  316. }
  317. }
  318. /* This function will remove the expression for VERSION from replacement
  319. consideration in table TAB. If FREE_EXPR is true, then remove the
  320. expression from consideration as well by freeing the decl uid bitmap. */
  321. static void
  322. finished_with_expr (temp_expr_table_p tab, int version, bool free_expr)
  323. {
  324. unsigned i;
  325. bitmap_iterator bi;
  326. /* Remove this expression from its dependent lists. The partition dependence
  327. list is retained and transferred later to whomever uses this version. */
  328. if (tab->partition_dependencies[version])
  329. {
  330. EXECUTE_IF_SET_IN_BITMAP (tab->partition_dependencies[version], 0, i, bi)
  331. remove_from_partition_kill_list (tab, i, version);
  332. BITMAP_FREE (tab->partition_dependencies[version]);
  333. }
  334. if (free_expr)
  335. BITMAP_FREE (tab->expr_decl_uids[version]);
  336. }
  337. /* Return TRUE if expression STMT is suitable for replacement.
  338. In addition to ssa_is_replaceable_p, require the same bb, and for -O0
  339. same locus and same BLOCK), Considers memory loads as replaceable if aliasing
  340. is available. */
  341. static inline bool
  342. ter_is_replaceable_p (gimple stmt)
  343. {
  344. if (ssa_is_replaceable_p (stmt))
  345. {
  346. use_operand_p use_p;
  347. tree def;
  348. gimple use_stmt;
  349. location_t locus1, locus2;
  350. tree block1, block2;
  351. /* Only consider definitions which have a single use. ssa_is_replaceable_p
  352. already performed this check, but the use stmt pointer is required for
  353. further checks. */
  354. def = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_DEF);
  355. if (!single_imm_use (def, &use_p, &use_stmt))
  356. return false;
  357. /* If the use isn't in this block, it wont be replaced either. */
  358. if (gimple_bb (use_stmt) != gimple_bb (stmt))
  359. return false;
  360. locus1 = gimple_location (stmt);
  361. block1 = LOCATION_BLOCK (locus1);
  362. locus1 = LOCATION_LOCUS (locus1);
  363. if (gphi *phi = dyn_cast <gphi *> (use_stmt))
  364. locus2 = gimple_phi_arg_location (phi,
  365. PHI_ARG_INDEX_FROM_USE (use_p));
  366. else
  367. locus2 = gimple_location (use_stmt);
  368. block2 = LOCATION_BLOCK (locus2);
  369. locus2 = LOCATION_LOCUS (locus2);
  370. if ((!optimize || optimize_debug)
  371. && ((locus1 != UNKNOWN_LOCATION && locus1 != locus2)
  372. || (block1 != NULL_TREE && block1 != block2)))
  373. return false;
  374. return true;
  375. }
  376. return false;
  377. }
  378. /* Create an expression entry for a replaceable expression. */
  379. static void
  380. process_replaceable (temp_expr_table_p tab, gimple stmt, int call_cnt)
  381. {
  382. tree var, def, basevar;
  383. int version;
  384. ssa_op_iter iter;
  385. bitmap def_vars, use_vars;
  386. gcc_checking_assert (ter_is_replaceable_p (stmt));
  387. def = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_DEF);
  388. version = SSA_NAME_VERSION (def);
  389. def_vars = BITMAP_ALLOC (&ter_bitmap_obstack);
  390. basevar = SSA_NAME_VAR (def);
  391. if (basevar)
  392. bitmap_set_bit (def_vars, DECL_UID (basevar));
  393. /* Add this expression to the dependency list for each use partition. */
  394. FOR_EACH_SSA_TREE_OPERAND (var, stmt, iter, SSA_OP_USE)
  395. {
  396. int var_version = SSA_NAME_VERSION (var);
  397. use_vars = tab->expr_decl_uids[var_version];
  398. add_dependence (tab, version, var);
  399. if (use_vars)
  400. {
  401. bitmap_ior_into (def_vars, use_vars);
  402. BITMAP_FREE (tab->expr_decl_uids[var_version]);
  403. }
  404. else if (SSA_NAME_VAR (var))
  405. bitmap_set_bit (def_vars, DECL_UID (SSA_NAME_VAR (var)));
  406. }
  407. tab->expr_decl_uids[version] = def_vars;
  408. /* If there are VUSES, add a dependence on virtual defs. */
  409. if (gimple_vuse (stmt))
  410. {
  411. make_dependent_on_partition (tab, version, VIRTUAL_PARTITION (tab));
  412. add_to_partition_kill_list (tab, VIRTUAL_PARTITION (tab), version);
  413. }
  414. tab->call_cnt[version] = call_cnt;
  415. }
  416. /* This function removes any expression in TAB which is dependent on PARTITION
  417. from consideration, making it not replaceable. */
  418. static inline void
  419. kill_expr (temp_expr_table_p tab, int partition)
  420. {
  421. unsigned version;
  422. /* Mark every active expr dependent on this var as not replaceable.
  423. finished_with_expr can modify the bitmap, so we can't execute over it. */
  424. while (tab->kill_list[partition])
  425. {
  426. version = bitmap_first_set_bit (tab->kill_list[partition]);
  427. finished_with_expr (tab, version, true);
  428. }
  429. gcc_checking_assert (!tab->kill_list[partition]);
  430. }
  431. /* This function kills all expressions in TAB which are dependent on virtual
  432. partitions. */
  433. static inline void
  434. kill_virtual_exprs (temp_expr_table_p tab)
  435. {
  436. kill_expr (tab, VIRTUAL_PARTITION (tab));
  437. }
  438. /* Mark the expression associated with VAR as replaceable, and enter
  439. the defining stmt into the partition_dependencies table TAB. If
  440. MORE_REPLACING is true, accumulate the pending partition dependencies. */
  441. static void
  442. mark_replaceable (temp_expr_table_p tab, tree var, bool more_replacing)
  443. {
  444. int version = SSA_NAME_VERSION (var);
  445. /* Move the dependence list to the pending listpending. */
  446. if (more_replacing && tab->partition_dependencies[version])
  447. bitmap_ior_into (tab->new_replaceable_dependencies,
  448. tab->partition_dependencies[version]);
  449. finished_with_expr (tab, version, !more_replacing);
  450. /* Set the replaceable expression.
  451. The bitmap for this "escapes" from this file so it's allocated
  452. on the default obstack. */
  453. if (!tab->replaceable_expressions)
  454. tab->replaceable_expressions = BITMAP_ALLOC (NULL);
  455. bitmap_set_bit (tab->replaceable_expressions, version);
  456. }
  457. /* Helper function for find_ssaname_in_stores. Called via walk_tree to
  458. find a SSA_NAME DATA somewhere in *TP. */
  459. static tree
  460. find_ssaname (tree *tp, int *walk_subtrees, void *data)
  461. {
  462. tree var = (tree) data;
  463. if (*tp == var)
  464. return var;
  465. else if (IS_TYPE_OR_DECL_P (*tp))
  466. *walk_subtrees = 0;
  467. return NULL_TREE;
  468. }
  469. /* Helper function for find_replaceable_in_bb. Return true if SSA_NAME DATA
  470. is used somewhere in T, which is a store in the statement. Called via
  471. walk_stmt_load_store_addr_ops. */
  472. static bool
  473. find_ssaname_in_store (gimple, tree, tree t, void *data)
  474. {
  475. return walk_tree (&t, find_ssaname, data, NULL) != NULL_TREE;
  476. }
  477. /* This function processes basic block BB, and looks for variables which can
  478. be replaced by their expressions. Results are stored in the table TAB. */
  479. static void
  480. find_replaceable_in_bb (temp_expr_table_p tab, basic_block bb)
  481. {
  482. gimple_stmt_iterator bsi;
  483. gimple stmt;
  484. tree def, use, fndecl;
  485. int partition;
  486. var_map map = tab->map;
  487. ssa_op_iter iter;
  488. bool stmt_replaceable;
  489. int cur_call_cnt = 0;
  490. for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
  491. {
  492. stmt = gsi_stmt (bsi);
  493. if (is_gimple_debug (stmt))
  494. continue;
  495. stmt_replaceable = ter_is_replaceable_p (stmt);
  496. /* Determine if this stmt finishes an existing expression. */
  497. FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
  498. {
  499. unsigned ver = SSA_NAME_VERSION (use);
  500. /* If this use is a potential replacement variable, process it. */
  501. if (tab->expr_decl_uids[ver])
  502. {
  503. bool same_root_var = false;
  504. ssa_op_iter iter2;
  505. bitmap vars = tab->expr_decl_uids[ver];
  506. /* See if the root variables are the same. If they are, we
  507. do not want to do the replacement to avoid problems with
  508. code size, see PR tree-optimization/17549. */
  509. if (!bitmap_empty_p (vars))
  510. FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter2, SSA_OP_DEF)
  511. {
  512. if (SSA_NAME_VAR (def)
  513. && bitmap_bit_p (vars, DECL_UID (SSA_NAME_VAR (def))))
  514. {
  515. same_root_var = true;
  516. break;
  517. }
  518. }
  519. /* If the stmt does a memory store and the replacement
  520. is a load aliasing it avoid creating overlapping
  521. assignments which we cannot expand correctly. */
  522. if (gimple_vdef (stmt))
  523. {
  524. gimple def_stmt = SSA_NAME_DEF_STMT (use);
  525. while (is_gimple_assign (def_stmt)
  526. && gimple_assign_rhs_code (def_stmt) == SSA_NAME)
  527. def_stmt
  528. = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (def_stmt));
  529. if (gimple_vuse (def_stmt)
  530. && gimple_assign_single_p (def_stmt)
  531. && stmt_may_clobber_ref_p (stmt,
  532. gimple_assign_rhs1 (def_stmt)))
  533. {
  534. /* For calls, it is not a problem if USE is among
  535. call's arguments or say OBJ_TYPE_REF argument,
  536. all those necessarily need to be evaluated before
  537. the call that may clobber the memory. But if
  538. LHS of the call refers to USE, expansion might
  539. evaluate it after the call, prevent TER in that
  540. case.
  541. For inline asm, allow TER of loads into input
  542. arguments, but disallow TER for USEs that occur
  543. somewhere in outputs. */
  544. if (is_gimple_call (stmt)
  545. || gimple_code (stmt) == GIMPLE_ASM)
  546. {
  547. if (walk_stmt_load_store_ops (stmt, use, NULL,
  548. find_ssaname_in_store))
  549. same_root_var = true;
  550. }
  551. else
  552. same_root_var = true;
  553. }
  554. }
  555. /* Mark expression as replaceable unless stmt is volatile, or the
  556. def variable has the same root variable as something in the
  557. substitution list, or the def and use span a call such that
  558. we'll expand lifetimes across a call. */
  559. if (gimple_has_volatile_ops (stmt) || same_root_var
  560. || (tab->call_cnt[ver] != cur_call_cnt
  561. && SINGLE_SSA_USE_OPERAND (SSA_NAME_DEF_STMT (use), SSA_OP_USE)
  562. == NULL_USE_OPERAND_P))
  563. finished_with_expr (tab, ver, true);
  564. else
  565. mark_replaceable (tab, use, stmt_replaceable);
  566. }
  567. }
  568. /* Next, see if this stmt kills off an active expression. */
  569. FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
  570. {
  571. partition = var_to_partition (map, def);
  572. if (partition != NO_PARTITION && tab->kill_list[partition])
  573. kill_expr (tab, partition);
  574. }
  575. /* Increment counter if this is a non BUILT_IN call. We allow
  576. replacement over BUILT_IN calls since many will expand to inline
  577. insns instead of a true call. */
  578. if (is_gimple_call (stmt)
  579. && !((fndecl = gimple_call_fndecl (stmt))
  580. && DECL_BUILT_IN (fndecl)))
  581. cur_call_cnt++;
  582. /* Now see if we are creating a new expression or not. */
  583. if (stmt_replaceable)
  584. process_replaceable (tab, stmt, cur_call_cnt);
  585. /* Free any unused dependency lists. */
  586. bitmap_clear (tab->new_replaceable_dependencies);
  587. /* A V_{MAY,MUST}_DEF kills any expression using a virtual operand,
  588. including the current stmt. */
  589. if (gimple_vdef (stmt))
  590. kill_virtual_exprs (tab);
  591. }
  592. }
  593. /* This function is the driver routine for replacement of temporary expressions
  594. in the SSA->normal phase, operating on MAP. If there are replaceable
  595. expressions, a table is returned which maps SSA versions to the
  596. expressions they should be replaced with. A NULL_TREE indicates no
  597. replacement should take place. If there are no replacements at all,
  598. NULL is returned by the function, otherwise an expression vector indexed
  599. by SSA_NAME version numbers. */
  600. bitmap
  601. find_replaceable_exprs (var_map map)
  602. {
  603. basic_block bb;
  604. temp_expr_table_p table;
  605. bitmap ret;
  606. bitmap_obstack_initialize (&ter_bitmap_obstack);
  607. table = new_temp_expr_table (map);
  608. FOR_EACH_BB_FN (bb, cfun)
  609. {
  610. find_replaceable_in_bb (table, bb);
  611. gcc_checking_assert (bitmap_empty_p (table->partition_in_use));
  612. }
  613. ret = free_temp_expr_table (table);
  614. bitmap_obstack_release (&ter_bitmap_obstack);
  615. return ret;
  616. }
  617. /* Dump TER expression table EXPR to file F. */
  618. void
  619. dump_replaceable_exprs (FILE *f, bitmap expr)
  620. {
  621. tree var;
  622. unsigned x;
  623. fprintf (f, "\nReplacing Expressions\n");
  624. for (x = 0; x < num_ssa_names; x++)
  625. if (bitmap_bit_p (expr, x))
  626. {
  627. var = ssa_name (x);
  628. print_generic_expr (f, var, TDF_SLIM);
  629. fprintf (f, " replace with --> ");
  630. print_gimple_stmt (f, SSA_NAME_DEF_STMT (var), 0, TDF_SLIM);
  631. fprintf (f, "\n");
  632. }
  633. fprintf (f, "\n");
  634. }
  635. #ifdef ENABLE_CHECKING
  636. /* Dump the status of the various tables in the expression table. This is used
  637. exclusively to debug TER. F is the place to send debug info and T is the
  638. table being debugged. */
  639. DEBUG_FUNCTION void
  640. debug_ter (FILE *f, temp_expr_table_p t)
  641. {
  642. unsigned x, y;
  643. bitmap_iterator bi;
  644. fprintf (f, "\nDumping current state of TER\n virtual partition = %d\n",
  645. VIRTUAL_PARTITION (t));
  646. if (t->replaceable_expressions)
  647. dump_replaceable_exprs (f, t->replaceable_expressions);
  648. fprintf (f, "Currently tracking the following expressions:\n");
  649. for (x = 1; x < num_ssa_names; x++)
  650. if (t->expr_decl_uids[x])
  651. {
  652. print_generic_expr (f, ssa_name (x), TDF_SLIM);
  653. fprintf (f, " dep-parts : ");
  654. if (t->partition_dependencies[x]
  655. && !bitmap_empty_p (t->partition_dependencies[x]))
  656. {
  657. EXECUTE_IF_SET_IN_BITMAP (t->partition_dependencies[x], 0, y, bi)
  658. fprintf (f, "P%d ",y);
  659. }
  660. fprintf (f, " basedecls: ");
  661. EXECUTE_IF_SET_IN_BITMAP (t->expr_decl_uids[x], 0, y, bi)
  662. fprintf (f, "%d ",y);
  663. fprintf (f, " call_cnt : %d",t->call_cnt[x]);
  664. fprintf (f, "\n");
  665. }
  666. bitmap_print (f, t->partition_in_use, "Partitions in use ",
  667. "\npartition KILL lists:\n");
  668. for (x = 0; x <= num_var_partitions (t->map); x++)
  669. if (t->kill_list[x])
  670. {
  671. fprintf (f, "Partition %d : ", x);
  672. EXECUTE_IF_SET_IN_BITMAP (t->kill_list[x], 0, y, bi)
  673. fprintf (f, "_%d ",y);
  674. }
  675. fprintf (f, "\n----------\n");
  676. }
  677. #endif