GCC Middle and Back End API Reference
constraint-manager.h
Go to the documentation of this file.
1/* Tracking equivalence classes and constraints at a point on an execution path.
2 Copyright (C) 2019-2024 Free Software Foundation, Inc.
3 Contributed by David Malcolm <dmalcolm@redhat.com>.
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it
8under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 3, or (at your option)
10any later version.
11
12GCC is distributed in the hope that it will be useful, but
13WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3. If not see
19<http://www.gnu.org/licenses/>. */
20
21#ifndef GCC_ANALYZER_CONSTRAINT_MANAGER_H
22#define GCC_ANALYZER_CONSTRAINT_MANAGER_H
23
24namespace ana {
25
26class constraint_manager;
27
33
34/* One of the end-points of a range. */
35
36struct bound
37{
39 bound (tree constant, bool closed)
40 : m_constant (constant), m_closed (closed) {}
41
43
44 const char * get_relation_as_str () const;
45
48};
49
50/* A range of values, used for determining if a value has been
51 constrained to just one possible constant value. */
52
77
78/* A closed range of values with constant integer bounds
79 e.g. [3, 5] for the set {3, 4, 5}. */
80
82{
84
85 void dump_to_pp (pretty_printer *pp, bool show_types) const;
86 void dump (bool show_types) const;
87
89
90 bool contains_p (tree cst) const;
91
92 bool intersects_p (const bounded_range &other,
93 bounded_range *out) const;
94
95 bool operator== (const bounded_range &other) const;
96 bool operator!= (const bounded_range &other) const
97 {
98 return !(*this == other);
99 }
100
101 static int cmp (const bounded_range &a, const bounded_range &b);
102
103 bool singleton_p () const
104 {
106 }
107
110
111private:
112 static void set_json_attr (json::object *obj, const char *name, tree value);
113};
114
115/* A collection of bounded_range instances, suitable
116 for representing the ranges on a case label within a switch
117 statement. */
118
120{
121public:
123
127
128 bool operator== (const bounded_ranges &other) const;
129
130 hashval_t get_hash () const { return m_hash; }
131
132 void dump_to_pp (pretty_printer *pp, bool show_types) const;
133 void dump (bool show_types) const;
134
136
140
141 bool contain_p (tree cst) const;
142 bool empty_p () const { return m_ranges.length () == 0; }
143
144 static int cmp (const bounded_ranges *a, const bounded_ranges *b);
145
146 unsigned get_count () const { return m_ranges.length (); }
147 const bounded_range &get_range (unsigned idx) const { return m_ranges[idx]; }
148
149private:
151 void validate () const;
152
154
157};
158
159} // namespace ana
160
161template <> struct default_hash_traits<bounded_ranges::key_t>
162: public member_function_hash_traits<bounded_ranges::key_t>
163{
164 static const bool empty_zero_p = true;
165};
166
167namespace ana {
168
169/* An object to own and consolidate bounded_ranges instances.
170 This also caches the mapping from switch_cfg_superedge
171 bounded_ranges instances, so that get_or_create_ranges_for_switch is
172 memoized. */
173
175{
176public:
178
179 const bounded_ranges *
181 const gswitch *switch_stmt);
182
186 const_tree upper_bound);
187 const bounded_ranges *
189 const bounded_ranges *
191 const bounded_ranges *b);
192 const bounded_ranges *
194
195 void log_stats (logger *logger, bool show_objs) const;
196
197private:
198 const bounded_ranges *
200 const gswitch *switch_stmt);
201
202 const bounded_ranges *
205
207
208 struct hash_traits_t : public typed_noop_remove<bounded_ranges *>
209 {
212
213 static inline bool
214 equal (const key_type &k1, const key_type &k2)
215 {
216 return *k1 == *k2;
217 }
218 static inline hashval_t
219 hash (const key_type &k)
220 {
221 return k->get_hash ();
222 }
223 static inline bool is_empty (key_type k) { return k == NULL; }
224 static inline void mark_empty (key_type &k) { k = NULL; }
225 static inline bool is_deleted (key_type k)
226 {
227 return k == reinterpret_cast<key_type> (1);
228 }
229
230 static const bool empty_zero_p = true;
231 };
232 struct traits_t : public simple_hashmap_traits<hash_traits_t,
233 bounded_ranges *>
234 {
235 };
238
239 typedef hash_map<const switch_cfg_superedge *,
242};
243
244/* An equivalence class within a constraint manager: a set of
245 svalues that are known to all be equal to each other,
246 together with an optional tree constant that they are equal to. */
247
249{
250public:
252 equiv_class (const equiv_class &other);
253
254 hashval_t hash () const;
255 bool operator== (const equiv_class &other);
256
257 void add (const svalue *sval);
258 bool del (const svalue *sval);
259
260 tree get_any_constant () const { return m_constant; }
261
262 const svalue *get_representative () const;
263
265
266 void print (pretty_printer *pp) const;
267
269
271
272 /* An equivalence class can contain multiple constants (e.g. multiple
273 different zeroes, for different types); these are just for the last
274 constant added. */
277
278 // TODO: should this be a set rather than a vec?
280};
281
282/* The various kinds of constraint. */
283
290
292
293/* An ID for an equiv_class within a constraint_manager. Internally, this
294 is an index into a vector of equiv_class * within the constraint_manager. */
295
297{
298public:
299 static equiv_class_id null () { return equiv_class_id (-1); }
300
301 equiv_class_id (unsigned idx) : m_idx (idx) {}
304
305 bool operator== (const equiv_class_id &other) const
306 {
307 return m_idx == other.m_idx;
308 }
309 bool operator!= (const equiv_class_id &other) const
310 {
311 return m_idx != other.m_idx;
312 }
313
314 bool null_p () const { return m_idx == -1; }
315
316 static equiv_class_id from_int (int idx) { return equiv_class_id (idx); }
317 int as_int () const { return m_idx; }
318
319 void print (pretty_printer *pp) const;
320
322 {
323 if (m_idx > other.m_idx)
324 m_idx--;
325 }
326
327 int m_idx;
328};
329
330/* A relationship between two equivalence classes in a constraint_manager. */
331
333{
334 public:
336 : m_lhs (lhs), m_op (c_op), m_rhs (rhs)
337 {
338 gcc_assert (!lhs.null_p ());
339 gcc_assert (!rhs.null_p ());
340 }
341
342 void print (pretty_printer *pp, const constraint_manager &cm) const;
343
345
346 hashval_t hash () const;
347 bool operator== (const constraint &other) const;
348
349 /* Is this an ordering, rather than a "!=". */
350 bool is_ordering_p () const
351 {
352 return m_op != CONSTRAINT_NE;
353 }
354
355 bool implied_by (const constraint &other,
356 const constraint_manager &cm) const;
357
361};
362
363/* An abstract base class for use with constraint_manager::for_each_fact. */
364
366{
367 public:
368 virtual ~fact_visitor () {}
369 virtual void on_fact (const svalue *lhs,
370 enum tree_code,
371 const svalue *rhs) = 0;
372 virtual void on_ranges (const svalue *lhs,
373 const bounded_ranges *ranges) = 0;
374};
375
377{
378public:
384
385 void print (pretty_printer *pp, const constraint_manager &cm) const;
386
388
389 bool operator== (const bounded_ranges_constraint &other) const;
390 bool operator!= (const bounded_ranges_constraint &other) const
391 {
392 return !(*this == other);
393 }
394
396
399};
400
401/* A collection of equivalence classes and constraints on them.
402
403 Given N svalues, this can be thought of as representing a subset of
404 N-dimensional space. When we call add_constraint,
405 we are effectively taking an intersection with that constraint. */
406
408{
409public:
413
415
416 hashval_t hash () const;
417 bool operator== (const constraint_manager &other) const;
418 bool operator!= (const constraint_manager &other) const
419 {
420 return !(*this == other);
421 }
422
423 void print (pretty_printer *pp) const;
424 void dump_to_pp (pretty_printer *pp, bool multiline) const;
425 void dump (FILE *fp) const;
426 void dump () const;
427
429
430 const equiv_class &get_equiv_class_by_index (unsigned idx) const
431 {
432 return *m_equiv_classes[idx];
433 }
435 {
436 return *m_equiv_classes[idx];
437 }
438
440 {
442 return ec_id.get_obj (*this);
443 }
444
445 bool add_constraint (const svalue *lhs,
446 enum tree_code op,
447 const svalue *rhs);
448
450 enum tree_code op,
452
454 enum tree_code op,
456
457 bool add_bounded_ranges (const svalue *sval,
458 const bounded_ranges *ranges);
459
461 equiv_class_id *out) const;
462 bool sval_constrained_p (const svalue *sval) const;
465 enum tree_code op,
466 equiv_class_id rhs) const;
468 enum tree_code op,
469 tree rhs_const) const;
471 enum tree_code op,
472 const svalue *rhs) const;
474
475 /* PurgeCriteria should have:
476 bool should_purge_p (const svalue *sval) const. */
477 template <typename PurgeCriteria>
479
481 const region_model *model);
482 void purge_state_involving (const svalue *sval);
483
485
486 static void merge (const constraint_manager &cm_a,
488 constraint_manager *out);
489
491
492 void validate () const;
493
495
498
502
503 private:
505 enum constraint_op c_op,
508 const svalue *rhs) const;
509
511};
512
513} // namespace ana
514
515#endif /* GCC_ANALYZER_CONSTRAINT_MANAGER_H */
Definition constraint-manager.h:377
bool operator!=(const bounded_ranges_constraint &other) const
Definition constraint-manager.h:390
const bounded_ranges * m_ranges
Definition constraint-manager.h:398
bounded_ranges_constraint(equiv_class_id ec_id, const bounded_ranges *ranges)
Definition constraint-manager.h:379
equiv_class_id m_ec_id
Definition constraint-manager.h:397
json::object * to_json() const
void add_to_hash(inchash::hash *hstate) const
void print(pretty_printer *pp, const constraint_manager &cm) const
bool operator==(const bounded_ranges_constraint &other) const
Definition constraint-manager.h:175
hash_map< bounded_ranges *, bounded_ranges *, traits_t > map_t
Definition constraint-manager.h:236
const bounded_ranges * get_or_create_inverse(const bounded_ranges *other, tree type)
const bounded_ranges * get_or_create_intersection(const bounded_ranges *a, const bounded_ranges *b)
const bounded_ranges * create_ranges_for_switch(const switch_cfg_superedge &edge, const gswitch *switch_stmt)
map_t m_map
Definition constraint-manager.h:237
const bounded_ranges * get_or_create_empty()
const bounded_ranges * get_or_create_range(const_tree lower_bound, const_tree upper_bound)
const bounded_ranges * get_or_create_union(const vec< const bounded_ranges * > &others)
hash_map< const switch_cfg_superedge *, const bounded_ranges * > edge_cache_t
Definition constraint-manager.h:240
void log_stats(logger *logger, bool show_objs) const
edge_cache_t m_edge_cache
Definition constraint-manager.h:241
const bounded_ranges * get_or_create_point(const_tree value)
const bounded_ranges * consolidate(bounded_ranges *)
const bounded_ranges * get_or_create_ranges_for_switch(const switch_cfg_superedge *edge, const gswitch *switch_stmt)
const bounded_ranges * make_case_label_ranges(const gswitch *switch_stmt, tree case_label)
Definition call-summary.h:68
Definition constraint-manager.h:408
bool add_constraint(const svalue *lhs, enum tree_code op, const svalue *rhs)
bool add_bounded_ranges(const svalue *sval, const bounded_ranges *ranges)
constraint_manager(const constraint_manager &other)
tristate eval_condition(equiv_class_id lhs, enum tree_code op, equiv_class_id rhs) const
region_model_manager * m_mgr
Definition constraint-manager.h:510
void print(pretty_printer *pp) const
bool get_equiv_class_by_svalue(const svalue *sval, equiv_class_id *out) const
range get_ec_bounds(equiv_class_id ec_id) const
void add_unknown_constraint(equiv_class_id lhs_ec_id, enum tree_code op, equiv_class_id rhs_ec_id)
void dump(FILE *fp) const
virtual ~constraint_manager()
Definition constraint-manager.h:412
json::object * to_json() const
void add_constraint_internal(equiv_class_id lhs_id, enum constraint_op c_op, equiv_class_id rhs_id)
constraint_manager(region_model_manager *mgr)
Definition constraint-manager.h:410
constraint_manager & operator=(const constraint_manager &other)
void purge_state_involving(const svalue *sval)
const equiv_class & get_equiv_class_by_index(unsigned idx) const
Definition constraint-manager.h:430
hashval_t hash() const
void for_each_fact(fact_visitor *) const
auto_vec< constraint > m_constraints
Definition constraint-manager.h:500
tristate eval_condition(const svalue *lhs, enum tree_code op, const svalue *rhs) const
void dump_to_pp(pretty_printer *pp, bool multiline) const
equiv_class & get_equiv_class_by_index(unsigned idx)
Definition constraint-manager.h:434
static void merge(const constraint_manager &cm_a, const constraint_manager &cm_b, constraint_manager *out)
auto_vec< bounded_ranges_constraint > m_bounded_ranges_constraints
Definition constraint-manager.h:501
void purge(const PurgeCriteria &p, purge_stats *stats)
bool operator==(const constraint_manager &other) const
equiv_class_id get_or_add_equiv_class(const svalue *sval)
equiv_class & get_equiv_class(const svalue *sval)
Definition constraint-manager.h:439
auto_delete_vec< equiv_class > m_equiv_classes
Definition constraint-manager.h:499
tristate eval_condition(equiv_class_id lhs_ec, enum tree_code op, tree rhs_const) const
bool impossible_derived_conditions_p(const svalue *lhs, const svalue *rhs) const
bool replay_call_summary(call_summary_replay &r, const constraint_manager &summary)
bounded_ranges_manager * get_range_manager() const
bool operator!=(const constraint_manager &other) const
Definition constraint-manager.h:418
bool add_constraint(equiv_class_id lhs_ec_id, enum tree_code op, equiv_class_id rhs_ec_id)
bool sval_constrained_p(const svalue *sval) const
void on_liveness_change(const svalue_set &live_svalues, const region_model *model)
Definition constraint-manager.h:333
json::object * to_json() const
equiv_class_id m_rhs
Definition constraint-manager.h:360
bool operator==(const constraint &other) const
enum constraint_op m_op
Definition constraint-manager.h:359
equiv_class_id m_lhs
Definition constraint-manager.h:358
bool is_ordering_p() const
Definition constraint-manager.h:350
hashval_t hash() const
constraint(equiv_class_id lhs, enum constraint_op c_op, equiv_class_id rhs)
Definition constraint-manager.h:335
void print(pretty_printer *pp, const constraint_manager &cm) const
bool implied_by(const constraint &other, const constraint_manager &cm) const
Definition constraint-manager.h:297
equiv_class_id(unsigned idx)
Definition constraint-manager.h:301
static equiv_class_id from_int(int idx)
Definition constraint-manager.h:316
bool operator!=(const equiv_class_id &other) const
Definition constraint-manager.h:309
const equiv_class & get_obj(const constraint_manager &cm) const
bool operator==(const equiv_class_id &other) const
Definition constraint-manager.h:305
int as_int() const
Definition constraint-manager.h:317
void print(pretty_printer *pp) const
int m_idx
Definition constraint-manager.h:327
bool null_p() const
Definition constraint-manager.h:314
void update_for_removal(equiv_class_id other)
Definition constraint-manager.h:321
static equiv_class_id null()
Definition constraint-manager.h:299
equiv_class & get_obj(constraint_manager &cm) const
Definition constraint-manager.h:249
json::object * to_json() const
tree m_constant
Definition constraint-manager.h:275
bool contains_non_constant_p() const
const svalue * get_representative() const
const svalue * m_cst_sval
Definition constraint-manager.h:276
auto_vec< const svalue * > m_vars
Definition constraint-manager.h:279
hashval_t hash() const
void add(const svalue *sval)
bool del(const svalue *sval)
tree get_any_constant() const
Definition constraint-manager.h:260
void print(pretty_printer *pp) const
equiv_class(const equiv_class &other)
bool operator==(const equiv_class &other)
Definition constraint-manager.h:366
virtual void on_ranges(const svalue *lhs, const bounded_ranges *ranges)=0
virtual void on_fact(const svalue *lhs, enum tree_code, const svalue *rhs)=0
virtual ~fact_visitor()
Definition constraint-manager.h:368
Definition analyzer-logging.h:34
Definition constraint-manager.h:54
void dump_to_pp(pretty_printer *pp) const
bound m_lower_bound
Definition constraint-manager.h:74
tree constrained_to_single_element()
bool add_bound(bound b, enum bound_kind bound_kind)
bool add_bound(enum tree_code op, tree rhs_const)
bound m_upper_bound
Definition constraint-manager.h:75
range(const bound &lower, const bound &upper)
Definition constraint-manager.h:57
void dump() const
range()
Definition constraint-manager.h:56
bool below_lower_bound(tree rhs_const) const
bool above_upper_bound(tree rhs_const) const
tristate eval_condition(enum tree_code op, tree rhs_const) const
Definition region-model-manager.h:32
Definition region-model.h:258
Definition svalue.h:90
Definition supergraph.h:557
Definition vec.h:1802
Definition vec.h:1656
Definition hash-map.h:40
Definition inchash.h:38
Definition json.h:95
Definition json.h:79
Definition pretty-print.h:244
Definition tristate.h:26
class edge_def * edge
Definition coretypes.h:342
const union tree_node * const_tree
Definition coretypes.h:98
union tree_node * tree
Definition coretypes.h:97
static void lower(vec< simplify * > &simplifiers, bool gimple)
Definition genmatch.cc:1699
tree_code
Definition genmatch.cc:347
T * ggc_alloc(ALONE_CXX_MEM_STAT_INFO)
Definition ggc.h:184
Definition access-diagram.h:30
constraint_op
Definition constraint-manager.h:285
@ CONSTRAINT_NE
Definition constraint-manager.h:286
@ CONSTRAINT_LE
Definition constraint-manager.h:288
@ CONSTRAINT_LT
Definition constraint-manager.h:287
bound_kind
Definition constraint-manager.h:29
@ BK_LOWER
Definition constraint-manager.h:30
@ BK_UPPER
Definition constraint-manager.h:31
const char * constraint_op_code(enum constraint_op c_op)
poly_int< N, C > r
Definition poly-int.h:770
Ca const poly_int< N, Cb > & b
Definition poly-int.h:767
Ca & a
Definition poly-int.h:766
Definition constraint-manager.h:37
bool m_closed
Definition constraint-manager.h:47
bound()
Definition constraint-manager.h:38
tree m_constant
Definition constraint-manager.h:46
bound(tree constant, bool closed)
Definition constraint-manager.h:39
void ensure_closed(enum bound_kind bound_kind)
const char * get_relation_as_str() const
Definition constraint-manager.h:82
void dump_to_pp(pretty_printer *pp, bool show_types) const
static void set_json_attr(json::object *obj, const char *name, tree value)
bool singleton_p() const
Definition constraint-manager.h:103
tree m_upper
Definition constraint-manager.h:109
json::object * to_json() const
bool contains_p(tree cst) const
bool operator==(const bounded_range &other) const
void dump(bool show_types) const
bounded_range(const_tree lower, const_tree upper)
static int cmp(const bounded_range &a, const bounded_range &b)
tree m_lower
Definition constraint-manager.h:108
bool intersects_p(const bounded_range &other, bounded_range *out) const
bool operator!=(const bounded_range &other) const
Definition constraint-manager.h:96
Definition constraint-manager.h:209
static bool is_empty(key_type k)
Definition constraint-manager.h:223
static hashval_t hash(const key_type &k)
Definition constraint-manager.h:219
static void mark_empty(key_type &k)
Definition constraint-manager.h:224
static bool is_deleted(key_type k)
Definition constraint-manager.h:225
static bool equal(const key_type &k1, const key_type &k2)
Definition constraint-manager.h:214
static const bool empty_zero_p
Definition constraint-manager.h:230
bounded_ranges * key_type
Definition constraint-manager.h:210
bounded_ranges * value_type
Definition constraint-manager.h:211
Definition constraint-manager.h:234
Definition constraint-manager.h:120
void validate() const
json::value * to_json() const
bounded_ranges(enum tree_code op, tree rhs_const)
bounded_ranges key_t
Definition constraint-manager.h:122
bounded_ranges(const bounded_range &range)
static int cmp(const bounded_ranges *a, const bounded_ranges *b)
hashval_t m_hash
Definition constraint-manager.h:156
auto_vec< bounded_range > m_ranges
Definition constraint-manager.h:155
unsigned get_count() const
Definition constraint-manager.h:146
bool contain_p(tree cst) const
bounded_ranges(const vec< bounded_range > &ranges)
void dump(bool show_types) const
tristate eval_condition(enum tree_code op, tree rhs_const, bounded_ranges_manager *mgr) const
hashval_t get_hash() const
Definition constraint-manager.h:130
void dump_to_pp(pretty_printer *pp, bool show_types) const
bool empty_p() const
Definition constraint-manager.h:142
const bounded_range & get_range(unsigned idx) const
Definition constraint-manager.h:147
bool operator==(const bounded_ranges &other) const
Definition region-model.h:195
Definition exploded-graph.h:512
Definition hash-traits.h:466
Definition gimple.h:895
Definition analyzer.h:508
Definition hash-map-traits.h:33
Definition gengtype.h:252
Definition hash-traits.h:75
Definition vec.h:450
#define NULL
Definition system.h:50
#define gcc_assert(EXPR)
Definition system.h:821
#define false
Definition system.h:895
bool tree_int_cst_equal(const_tree t1, const_tree t2)
Definition tree.cc:6382
#define NULL_TREE
Definition tree.h:317