1 /* Support routines for Value Range Propagation (VRP).
2    Copyright (C) 2016-2019 Free Software Foundation, Inc.
3 
4 This file is part of GCC.
5 
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
10 
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15 
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3.  If not see
18 <http://www.gnu.org/licenses/>.  */
19 
20 #ifndef GCC_TREE_VRP_H
21 #define GCC_TREE_VRP_H
22 
23 /* Types of value ranges.  */
24 enum value_range_kind
25 {
26   /* Empty range.  */
27   VR_UNDEFINED,
28   /* Range spans the entire domain.  */
29   VR_VARYING,
30   /* Range is [MIN, MAX].  */
31   VR_RANGE,
32   /* Range is ~[MIN, MAX].  */
33   VR_ANTI_RANGE,
34   /* Range is a nice guy.  */
35   VR_LAST
36 };
37 
38 
39 /* Range of values that can be associated with an SSA_NAME after VRP
40    has executed.  */
class(for_user)41 class GTY((for_user)) value_range_base
42 {
43 public:
44   value_range_base ();
45   value_range_base (value_range_kind, tree, tree);
46 
47   void set (value_range_kind, tree, tree);
48   void set (tree);
49   void set_nonnull (tree);
50   void set_null (tree);
51 
52   enum value_range_kind kind () const;
53   tree min () const;
54   tree max () const;
55 
56   /* Types of value ranges.  */
57   bool symbolic_p () const;
58   bool constant_p () const;
59   bool undefined_p () const;
60   bool varying_p () const;
61   void set_varying ();
62   void set_undefined ();
63 
64   void union_ (const value_range_base *);
65 
66   bool operator== (const value_range_base &) const /* = delete */;
67   bool operator!= (const value_range_base &) const /* = delete */;
68   bool equal_p (const value_range_base &) const;
69 
70   /* Misc methods.  */
71   tree type () const;
72   bool may_contain_p (tree) const;
73   void set_and_canonicalize (enum value_range_kind, tree, tree);
74   bool zero_p () const;
75   bool singleton_p (tree *result = NULL) const;
76   void dump (FILE *) const;
77 
78 protected:
79   void check ();
80   static value_range_base union_helper (const value_range_base *,
81 					const value_range_base *);
82 
83   enum value_range_kind m_kind;
84 
85   tree m_min;
86   tree m_max;
87 
88   friend void gt_ggc_mx_value_range_base (void *);
89   friend void gt_pch_p_16value_range_base (void *, void *,
90 					   gt_pointer_operator, void *);
91   friend void gt_pch_nx_value_range_base (void *);
92   friend void gt_ggc_mx (value_range_base &);
93   friend void gt_ggc_mx (value_range_base *&);
94   friend void gt_pch_nx (value_range_base &);
95   friend void gt_pch_nx (value_range_base *, gt_pointer_operator, void *);
96 };
97 
98 /* Note value_range cannot currently be used with GC memory, only
99    value_range_base is fully set up for this.  */
class(user)100 class GTY((user)) value_range : public value_range_base
101 {
102  public:
103   value_range ();
104   value_range (const value_range_base &);
105   /* Deep-copies equiv bitmap argument.  */
106   value_range (value_range_kind, tree, tree, bitmap = NULL);
107 
108   /* Shallow-copies equiv bitmap.  */
109   value_range (const value_range &) /* = delete */;
110   /* Shallow-copies equiv bitmap.  */
111   value_range& operator=(const value_range&) /* = delete */;
112 
113   /* Move equiv bitmap from source range.  */
114   void move (value_range *);
115 
116   /* Leaves equiv bitmap alone.  */
117   void update (value_range_kind, tree, tree);
118   /* Deep-copies equiv bitmap argument.  */
119   void set (value_range_kind, tree, tree, bitmap = NULL);
120   void set (tree);
121   void set_nonnull (tree);
122   void set_null (tree);
123 
124   bool operator== (const value_range &) const /* = delete */;
125   bool operator!= (const value_range &) const /* = delete */;
126   void intersect (const value_range *);
127   void union_ (const value_range *);
128   bool equal_p (const value_range &, bool ignore_equivs) const;
129 
130   /* Types of value ranges.  */
131   void set_undefined ();
132   void set_varying ();
133 
134   /* Equivalence bitmap methods.  */
135   bitmap equiv () const;
136   void equiv_clear ();
137   void equiv_add (const_tree, const value_range *, bitmap_obstack * = NULL);
138 
139   /* Misc methods.  */
140   void deep_copy (const value_range *);
141   void set_and_canonicalize (enum value_range_kind, tree, tree, bitmap = NULL);
142   void dump (FILE *) const;
143 
144  private:
145   /* Deep-copies bitmap argument.  */
146   void set_equiv (bitmap);
147   void check ();
148   void intersect_helper (value_range *, const value_range *);
149 
150   /* Set of SSA names whose value ranges are equivalent to this one.
151      This set is only valid when TYPE is VR_RANGE or VR_ANTI_RANGE.  */
152   bitmap m_equiv;
153 };
154 
155 inline
value_range_base()156 value_range_base::value_range_base ()
157 {
158   m_kind = VR_UNDEFINED;
159   m_min = m_max = NULL;
160 }
161 
162 inline
value_range()163 value_range::value_range ()
164   : value_range_base ()
165 {
166   m_equiv = NULL;
167 }
168 
169 /* Return the kind of this range.  */
170 
171 inline value_range_kind
kind()172 value_range_base::kind () const
173 {
174   return m_kind;
175 }
176 
177 inline bitmap
equiv()178 value_range::equiv () const
179 {
180   return m_equiv;
181 }
182 
183 /* Return the lower bound.  */
184 
185 inline tree
min()186 value_range_base::min () const
187 {
188   return m_min;
189 }
190 
191 /* Return the upper bound.  */
192 
193 inline tree
max()194 value_range_base::max () const
195 {
196   return m_max;
197 }
198 
199 /* Return TRUE if range spans the entire possible domain.  */
200 
201 inline bool
varying_p()202 value_range_base::varying_p () const
203 {
204   return m_kind == VR_VARYING;
205 }
206 
207 /* Return TRUE if range is undefined (essentially the empty set).  */
208 
209 inline bool
undefined_p()210 value_range_base::undefined_p () const
211 {
212   return m_kind == VR_UNDEFINED;
213 }
214 
215 /* Return TRUE if range is the constant zero.  */
216 
217 inline bool
zero_p()218 value_range_base::zero_p () const
219 {
220   return (m_kind == VR_RANGE
221 	  && integer_zerop (m_min)
222 	  && integer_zerop (m_max));
223 }
224 
225 extern void dump_value_range (FILE *, const value_range *);
226 extern void dump_value_range (FILE *, const value_range_base *);
227 
228 struct assert_info
229 {
230   /* Predicate code for the ASSERT_EXPR.  Must be COMPARISON_CLASS_P.  */
231   enum tree_code comp_code;
232 
233   /* Name to register the assert for.  */
234   tree name;
235 
236   /* Value being compared against.  */
237   tree val;
238 
239   /* Expression to compare.  */
240   tree expr;
241 };
242 
243 extern void register_edge_assert_for (tree, edge, enum tree_code,
244 				      tree, tree, vec<assert_info> &);
245 extern bool stmt_interesting_for_vrp (gimple *);
246 extern bool range_includes_p (const value_range_base *, HOST_WIDE_INT);
247 extern bool infer_value_range (gimple *, tree, tree_code *, tree *);
248 
249 extern bool vrp_bitmap_equal_p (const_bitmap, const_bitmap);
250 
251 extern tree value_range_constant_singleton (const value_range_base *);
252 extern bool range_int_cst_p (const value_range_base *);
253 extern bool range_int_cst_singleton_p (const value_range_base *);
254 
255 extern int compare_values (tree, tree);
256 extern int compare_values_warnv (tree, tree, bool *);
257 extern int operand_less_p (tree, tree);
258 extern bool vrp_val_is_min (const_tree);
259 extern bool vrp_val_is_max (const_tree);
260 extern int value_inside_range (tree, tree, tree);
261 
262 extern tree vrp_val_min (const_tree);
263 extern tree vrp_val_max (const_tree);
264 
265 extern void extract_range_from_unary_expr (value_range_base *vr,
266 					   enum tree_code code,
267 					   tree type,
268 					   const value_range_base *vr0_,
269 					   tree op0_type);
270 extern void extract_range_from_binary_expr (value_range_base *,
271 					    enum tree_code,
272 					    tree, const value_range_base *,
273 					    const value_range_base *);
274 
275 extern bool vrp_operand_equal_p (const_tree, const_tree);
276 extern enum value_range_kind intersect_range_with_nonzero_bits
277   (enum value_range_kind, wide_int *, wide_int *, const wide_int &, signop);
278 extern bool vrp_set_zero_nonzero_bits (const tree, const value_range_base *,
279 				       wide_int *, wide_int *);
280 
281 extern bool find_case_label_range (gswitch *, tree, tree, size_t *, size_t *);
282 extern bool find_case_label_index (gswitch *, size_t, tree, size_t *);
283 extern bool overflow_comparison_p (tree_code, tree, tree, bool, tree *);
284 extern tree get_single_symbol (tree, bool *, tree *);
285 extern void maybe_set_nonzero_bits (edge, tree);
286 extern value_range_kind determine_value_range (tree, wide_int *, wide_int *);
287 
288 /* Return TRUE if *VR includes the value zero.  */
289 
290 inline bool
range_includes_zero_p(const value_range_base * vr)291 range_includes_zero_p (const value_range_base *vr)
292 {
293   return range_includes_p (vr, 0);
294 }
295 
296 #endif /* GCC_TREE_VRP_H */
297