tree-ssa-scopedtables.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /* Header file for SSA dominator optimizations.
  2. Copyright (C) 2013-2019 Free Software Foundation, Inc.
  3. This file is part of GCC.
  4. GCC is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free
  6. Software Foundation; either version 3, or (at your option) any later
  7. version.
  8. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  11. 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. #ifndef GCC_TREE_SSA_SCOPED_TABLES_H
  16. #define GCC_TREE_SSA_SCOPED_TABLES_H
  17. /* Representation of a "naked" right-hand-side expression, to be used
  18. in recording available expressions in the expression hash table. */
  19. enum expr_kind
  20. {
  21. EXPR_SINGLE,
  22. EXPR_UNARY,
  23. EXPR_BINARY,
  24. EXPR_TERNARY,
  25. EXPR_CALL,
  26. EXPR_PHI
  27. };
  28. struct hashable_expr
  29. {
  30. tree type;
  31. enum expr_kind kind;
  32. union {
  33. struct { tree rhs; } single;
  34. struct { enum tree_code op; tree opnd; } unary;
  35. struct { enum tree_code op; tree opnd0, opnd1; } binary;
  36. struct { enum tree_code op; tree opnd0, opnd1, opnd2; } ternary;
  37. struct { gcall *fn_from; bool pure; size_t nargs; tree *args; } call;
  38. struct { size_t nargs; tree *args; } phi;
  39. } ops;
  40. };
  41. /* Structure for recording known value of a conditional expression.
  42. Clients build vectors of these objects to record known values
  43. that occur on edges. */
  44. struct cond_equivalence
  45. {
  46. /* The condition, in a HASHABLE_EXPR form. */
  47. struct hashable_expr cond;
  48. /* The result of the condition (true or false. */
  49. tree value;
  50. };
  51. /* Structure for entries in the expression hash table. */
  52. typedef class expr_hash_elt * expr_hash_elt_t;
  53. class expr_hash_elt
  54. {
  55. public:
  56. expr_hash_elt (gimple *, tree);
  57. expr_hash_elt (tree);
  58. expr_hash_elt (struct hashable_expr *, tree);
  59. expr_hash_elt (class expr_hash_elt &);
  60. ~expr_hash_elt ();
  61. void print (FILE *);
  62. tree vop (void) { return m_vop; }
  63. tree lhs (void) { return m_lhs; }
  64. struct hashable_expr *expr (void) { return &m_expr; }
  65. expr_hash_elt *stamp (void) { return m_stamp; }
  66. hashval_t hash (void) { return m_hash; }
  67. private:
  68. /* The expression (rhs) we want to record. */
  69. struct hashable_expr m_expr;
  70. /* The value (lhs) of this expression. */
  71. tree m_lhs;
  72. /* The virtual operand associated with the nearest dominating stmt
  73. loading from or storing to expr. */
  74. tree m_vop;
  75. /* The hash value for RHS. */
  76. hashval_t m_hash;
  77. /* A unique stamp, typically the address of the hash
  78. element itself, used in removing entries from the table. */
  79. struct expr_hash_elt *m_stamp;
  80. /* We should never be making assignments between objects in this class.
  81. Though it might allow us to exploit C++11 move semantics if we
  82. defined the move constructor and move assignment operator. */
  83. expr_hash_elt& operator= (const expr_hash_elt&);
  84. };
  85. /* Hashtable helpers. */
  86. struct expr_elt_hasher : pointer_hash <expr_hash_elt>
  87. {
  88. static inline hashval_t hash (const value_type &p)
  89. { return p->hash (); }
  90. static bool equal (const value_type &, const compare_type &);
  91. static inline void remove (value_type &element)
  92. { delete element; }
  93. };
  94. /* This class defines a unwindable expression equivalence table
  95. layered on top of the expression hash table.
  96. Essentially it's just a stack of available expression value pairs with
  97. a special marker (NULL, NULL) to indicate unwind points. */
  98. class avail_exprs_stack
  99. {
  100. public:
  101. /* We need access to the AVAIL_EXPR hash table so that we can
  102. remove entries from the hash table when unwinding the stack. */
  103. avail_exprs_stack (hash_table<expr_elt_hasher> *table)
  104. { m_stack.create (20); m_avail_exprs = table; }
  105. ~avail_exprs_stack (void) { m_stack.release (); }
  106. /* Push the unwinding marker onto the stack. */
  107. void push_marker (void) { record_expr (NULL, NULL, 'M'); }
  108. /* Restore the AVAIL_EXPRs table to its state when the last marker
  109. was pushed. */
  110. void pop_to_marker (void);
  111. /* Record a single available expression that can be unwound. */
  112. void record_expr (expr_hash_elt_t, expr_hash_elt_t, char);
  113. /* Get the underlying hash table. Would this be better as
  114. class inheritance? */
  115. hash_table<expr_elt_hasher> *avail_exprs (void)
  116. { return m_avail_exprs; }
  117. /* Lookup and conditionally insert an expression into the table,
  118. recording enough information to unwind as needed. */
  119. tree lookup_avail_expr (gimple *, bool, bool);
  120. void record_cond (cond_equivalence *);
  121. private:
  122. vec<std::pair<expr_hash_elt_t, expr_hash_elt_t> > m_stack;
  123. hash_table<expr_elt_hasher> *m_avail_exprs;
  124. /* For some assignments where the RHS is a binary operator, if we know
  125. a equality relationship between the operands, we may be able to compute
  126. a result, even if we don't know the exact value of the operands. */
  127. tree simplify_binary_operation (gimple *, class expr_hash_elt);
  128. /* We do not allow copying this object or initializing one
  129. from another. */
  130. avail_exprs_stack& operator= (const avail_exprs_stack&);
  131. avail_exprs_stack (class avail_exprs_stack &);
  132. };
  133. /* This class defines an unwindable const/copy equivalence table
  134. layered on top of SSA_NAME_VALUE/set_ssa_name_value.
  135. Essentially it's just a stack of name,prev value pairs with a
  136. special marker (NULL) to indicate unwind points. */
  137. class const_and_copies
  138. {
  139. public:
  140. const_and_copies (void) { m_stack.create (20); };
  141. ~const_and_copies (void) { m_stack.release (); }
  142. /* Push the unwinding marker onto the stack. */
  143. void push_marker (void) { m_stack.safe_push (NULL_TREE); }
  144. /* Restore the const/copies table to its state when the last marker
  145. was pushed. */
  146. void pop_to_marker (void);
  147. /* Record a single const/copy pair that can be unwound. This version
  148. may follow the value chain for the RHS. */
  149. void record_const_or_copy (tree, tree);
  150. /* Special entry point when we want to provide an explicit previous
  151. value for the first argument. Try to get rid of this in the future.
  152. This version may also follow the value chain for the RHS. */
  153. void record_const_or_copy (tree, tree, tree);
  154. private:
  155. /* Record a single const/copy pair that can be unwound. This version
  156. does not follow the value chain for the RHS. */
  157. void record_const_or_copy_raw (tree, tree, tree);
  158. vec<tree> m_stack;
  159. const_and_copies& operator= (const const_and_copies&);
  160. const_and_copies (class const_and_copies &);
  161. };
  162. void initialize_expr_from_cond (tree cond, struct hashable_expr *expr);
  163. void record_conditions (vec<cond_equivalence> *p, tree, tree);
  164. #endif /* GCC_TREE_SSA_SCOPED_TABLES_H */