tree-vrp.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /* Support routines for Value Range Propagation (VRP).
  2. Copyright (C) 2016-2019 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. #ifndef GCC_TREE_VRP_H
  16. #define GCC_TREE_VRP_H
  17. /* Types of value ranges. */
  18. enum value_range_kind
  19. {
  20. /* Empty range. */
  21. VR_UNDEFINED,
  22. /* Range spans the entire domain. */
  23. VR_VARYING,
  24. /* Range is [MIN, MAX]. */
  25. VR_RANGE,
  26. /* Range is ~[MIN, MAX]. */
  27. VR_ANTI_RANGE,
  28. /* Range is a nice guy. */
  29. VR_LAST
  30. };
  31. /* Range of values that can be associated with an SSA_NAME after VRP
  32. has executed. */
  33. class GTY((for_user)) value_range_base
  34. {
  35. public:
  36. value_range_base ();
  37. value_range_base (value_range_kind, tree, tree);
  38. void set (value_range_kind, tree, tree);
  39. void set (tree);
  40. void set_nonnull (tree);
  41. void set_null (tree);
  42. enum value_range_kind kind () const;
  43. tree min () const;
  44. tree max () const;
  45. /* Types of value ranges. */
  46. bool symbolic_p () const;
  47. bool constant_p () const;
  48. bool undefined_p () const;
  49. bool varying_p () const;
  50. void set_varying ();
  51. void set_undefined ();
  52. void union_ (const value_range_base *);
  53. bool operator== (const value_range_base &) const /* = delete */;
  54. bool operator!= (const value_range_base &) const /* = delete */;
  55. bool equal_p (const value_range_base &) const;
  56. /* Misc methods. */
  57. tree type () const;
  58. bool may_contain_p (tree) const;
  59. void set_and_canonicalize (enum value_range_kind, tree, tree);
  60. bool zero_p () const;
  61. bool singleton_p (tree *result = NULL) const;
  62. void dump (FILE *) const;
  63. protected:
  64. void check ();
  65. static value_range_base union_helper (const value_range_base *,
  66. const value_range_base *);
  67. enum value_range_kind m_kind;
  68. tree m_min;
  69. tree m_max;
  70. friend void gt_ggc_mx_value_range_base (void *);
  71. friend void gt_pch_p_16value_range_base (void *, void *,
  72. gt_pointer_operator, void *);
  73. friend void gt_pch_nx_value_range_base (void *);
  74. friend void gt_ggc_mx (value_range_base &);
  75. friend void gt_ggc_mx (value_range_base *&);
  76. friend void gt_pch_nx (value_range_base &);
  77. friend void gt_pch_nx (value_range_base *, gt_pointer_operator, void *);
  78. };
  79. /* Note value_range cannot currently be used with GC memory, only
  80. value_range_base is fully set up for this. */
  81. class GTY((user)) value_range : public value_range_base
  82. {
  83. public:
  84. value_range ();
  85. value_range (const value_range_base &);
  86. /* Deep-copies equiv bitmap argument. */
  87. value_range (value_range_kind, tree, tree, bitmap = NULL);
  88. /* Shallow-copies equiv bitmap. */
  89. value_range (const value_range &) /* = delete */;
  90. /* Shallow-copies equiv bitmap. */
  91. value_range& operator=(const value_range&) /* = delete */;
  92. /* Move equiv bitmap from source range. */
  93. void move (value_range *);
  94. /* Leaves equiv bitmap alone. */
  95. void update (value_range_kind, tree, tree);
  96. /* Deep-copies equiv bitmap argument. */
  97. void set (value_range_kind, tree, tree, bitmap = NULL);
  98. void set (tree);
  99. void set_nonnull (tree);
  100. void set_null (tree);
  101. bool operator== (const value_range &) const /* = delete */;
  102. bool operator!= (const value_range &) const /* = delete */;
  103. void intersect (const value_range *);
  104. void union_ (const value_range *);
  105. bool equal_p (const value_range &, bool ignore_equivs) const;
  106. /* Types of value ranges. */
  107. void set_undefined ();
  108. void set_varying ();
  109. /* Equivalence bitmap methods. */
  110. bitmap equiv () const;
  111. void equiv_clear ();
  112. void equiv_add (const_tree, const value_range *, bitmap_obstack * = NULL);
  113. /* Misc methods. */
  114. void deep_copy (const value_range *);
  115. void set_and_canonicalize (enum value_range_kind, tree, tree, bitmap = NULL);
  116. void dump (FILE *) const;
  117. private:
  118. /* Deep-copies bitmap argument. */
  119. void set_equiv (bitmap);
  120. void check ();
  121. void intersect_helper (value_range *, const value_range *);
  122. /* Set of SSA names whose value ranges are equivalent to this one.
  123. This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE. */
  124. bitmap m_equiv;
  125. };
  126. inline
  127. value_range_base::value_range_base ()
  128. {
  129. m_kind = VR_UNDEFINED;
  130. m_min = m_max = NULL;
  131. }
  132. inline
  133. value_range::value_range ()
  134. : value_range_base ()
  135. {
  136. m_equiv = NULL;
  137. }
  138. /* Return the kind of this range. */
  139. inline value_range_kind
  140. value_range_base::kind () const
  141. {
  142. return m_kind;
  143. }
  144. inline bitmap
  145. value_range::equiv () const
  146. {
  147. return m_equiv;
  148. }
  149. /* Return the lower bound. */
  150. inline tree
  151. value_range_base::min () const
  152. {
  153. return m_min;
  154. }
  155. /* Return the upper bound. */
  156. inline tree
  157. value_range_base::max () const
  158. {
  159. return m_max;
  160. }
  161. /* Return TRUE if range spans the entire possible domain. */
  162. inline bool
  163. value_range_base::varying_p () const
  164. {
  165. return m_kind == VR_VARYING;
  166. }
  167. /* Return TRUE if range is undefined (essentially the empty set). */
  168. inline bool
  169. value_range_base::undefined_p () const
  170. {
  171. return m_kind == VR_UNDEFINED;
  172. }
  173. /* Return TRUE if range is the constant zero. */
  174. inline bool
  175. value_range_base::zero_p () const
  176. {
  177. return (m_kind == VR_RANGE
  178. && integer_zerop (m_min)
  179. && integer_zerop (m_max));
  180. }
  181. extern void dump_value_range (FILE *, const value_range *);
  182. extern void dump_value_range (FILE *, const value_range_base *);
  183. struct assert_info
  184. {
  185. /* Predicate code for the ASSERT_EXPR. Must be COMPARISON_CLASS_P. */
  186. enum tree_code comp_code;
  187. /* Name to register the assert for. */
  188. tree name;
  189. /* Value being compared against. */
  190. tree val;
  191. /* Expression to compare. */
  192. tree expr;
  193. };
  194. extern void register_edge_assert_for (tree, edge, enum tree_code,
  195. tree, tree, vec<assert_info> &);
  196. extern bool stmt_interesting_for_vrp (gimple *);
  197. extern bool range_includes_p (const value_range_base *, HOST_WIDE_INT);
  198. extern bool infer_value_range (gimple *, tree, tree_code *, tree *);
  199. extern bool vrp_bitmap_equal_p (const_bitmap, const_bitmap);
  200. extern tree value_range_constant_singleton (const value_range_base *);
  201. extern bool range_int_cst_p (const value_range_base *);
  202. extern bool range_int_cst_singleton_p (const value_range_base *);
  203. extern int compare_values (tree, tree);
  204. extern int compare_values_warnv (tree, tree, bool *);
  205. extern int operand_less_p (tree, tree);
  206. extern bool vrp_val_is_min (const_tree);
  207. extern bool vrp_val_is_max (const_tree);
  208. extern int value_inside_range (tree, tree, tree);
  209. extern tree vrp_val_min (const_tree);
  210. extern tree vrp_val_max (const_tree);
  211. extern void extract_range_from_unary_expr (value_range_base *vr,
  212. enum tree_code code,
  213. tree type,
  214. const value_range_base *vr0_,
  215. tree op0_type);
  216. extern void extract_range_from_binary_expr (value_range_base *,
  217. enum tree_code,
  218. tree, const value_range_base *,
  219. const value_range_base *);
  220. extern bool vrp_operand_equal_p (const_tree, const_tree);
  221. extern enum value_range_kind intersect_range_with_nonzero_bits
  222. (enum value_range_kind, wide_int *, wide_int *, const wide_int &, signop);
  223. extern bool vrp_set_zero_nonzero_bits (const tree, const value_range_base *,
  224. wide_int *, wide_int *);
  225. extern bool find_case_label_range (gswitch *, tree, tree, size_t *, size_t *);
  226. extern bool find_case_label_index (gswitch *, size_t, tree, size_t *);
  227. extern bool overflow_comparison_p (tree_code, tree, tree, bool, tree *);
  228. extern tree get_single_symbol (tree, bool *, tree *);
  229. extern void maybe_set_nonzero_bits (edge, tree);
  230. extern value_range_kind determine_value_range (tree, wide_int *, wide_int *);
  231. /* Return TRUE if *VR includes the value zero. */
  232. inline bool
  233. range_includes_zero_p (const value_range_base *vr)
  234. {
  235. return range_includes_p (vr, 0);
  236. }
  237. #endif /* GCC_TREE_VRP_H */