Line data Source code
1 : /* Header file for the value range relational processing.
2 : Copyright (C) 2020-2026 Free Software Foundation, Inc.
3 : Contributed by Andrew MacLeod <amacleod@redhat.com>
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify it under
8 : the terms of the GNU General Public License as published by the Free
9 : Software Foundation; either version 3, or (at your option) any later
10 : version.
11 :
12 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 : for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 : #ifndef GCC_VALUE_RELATION_H
22 : #define GCC_VALUE_RELATION_H
23 :
24 :
25 : // This file provides access to a relation oracle which can be used to
26 : // maintain and query relations and equivalences between SSA_NAMES.
27 : //
28 : // The general range_query object provided in value-query.h provides
29 : // access to an oracle, if one is available, via the oracle() method.
30 : // There are also a couple of access routines provided, which even if there is
31 : // no oracle, will return the default VREL_VARYING no relation.
32 : //
33 : // Typically, when a ranger object is active, there will be an oracle, and
34 : // any information available can be directly queried. Ranger also sets and
35 : // utilizes the relation information to enhance it's range calculations, this
36 : // is totally transparent to the client, and they are free to make queries.
37 : //
38 : // relation_kind is a new enum which represents the different relations,
39 : // often with a direct mapping to tree codes. ie VREL_EQ is equivalent to
40 : // EQ_EXPR.
41 : //
42 : // A query is made requesting the relation between SSA1 and SSA@ in a basic
43 : // block, or on an edge, the possible return values are:
44 : //
45 : // VREL_EQ, VREL_NE, VREL_LT, VREL_LE, VREL_GT, and VREL_GE mean the same.
46 : // VREL_VARYING : No relation between the 2 names.
47 : // VREL_UNDEFINED : Impossible relation (ie, A < B && A > B)
48 : //
49 : // The oracle maintains VREL_EQ relations with equivalency sets, so if a
50 : // relation comes back VREL_EQ, it is also possible to query the set of
51 : // equivalencies. These are basically bitmaps over ssa_names. An iterator is
52 : // provided later for this activity.
53 : //
54 : // Relations are maintained via the dominance trees and are optimized assuming
55 : // they are registered in dominance order. When a new relation is added, it
56 : // is intersected with whatever existing relation exists in the dominance tree
57 : // and registered at the specified block.
58 :
59 :
60 : // These codes are arranged such that VREL_VARYING is the first code, and all
61 : // the rest are contiguous.
62 :
63 : typedef enum relation_kind_t
64 : {
65 : VREL_VARYING = 0, // No known relation, AKA varying.
66 : VREL_UNDEFINED, // Impossible relation, ie (r1 < r2) && (r2 > r1)
67 : VREL_LT, // r1 < r2
68 : VREL_LE, // r1 <= r2
69 : VREL_GT, // r1 > r2
70 : VREL_GE, // r1 >= r2
71 : VREL_EQ, // r1 == r2
72 : VREL_NE, // r1 != r2
73 : VREL_PE8, // 8 bit partial equivalency
74 : VREL_PE16, // 16 bit partial equivalency
75 : VREL_PE32, // 32 bit partial equivalency
76 : VREL_PE64, // 64 bit partial equivalency
77 : VREL_LAST // terminate, not a real relation.
78 : } relation_kind;
79 :
80 : // General relation kind transformations.
81 : relation_kind relation_union (relation_kind r1, relation_kind r2);
82 : relation_kind relation_intersect (relation_kind r1, relation_kind r2);
83 : relation_kind relation_negate (relation_kind r);
84 : relation_kind relation_swap (relation_kind r);
85 5617833 : inline bool relation_lt_le_gt_ge_p (relation_kind r)
86 5617833 : { return (r >= VREL_LT && r <= VREL_GE); }
87 203025309 : inline bool relation_partial_equiv_p (relation_kind r)
88 116409187 : { return (r >= VREL_PE8 && r <= VREL_PE64); }
89 179504573 : inline bool relation_equiv_p (relation_kind r)
90 179504573 : { return r == VREL_EQ || relation_partial_equiv_p (r); }
91 :
92 : void print_relation (FILE *f, relation_kind rel);
93 :
94 : // Adjust range as an equivalence.
95 : void adjust_equivalence_range (vrange &range);
96 :
97 : class relation_oracle
98 : {
99 : public:
100 54353788 : relation_oracle () { m_lhs_equiv_set_p = NULL; }
101 54641432 : virtual ~relation_oracle () { }
102 :
103 : // register a relation between 2 ssa names.
104 : bool record (gimple *, relation_kind, tree, tree);
105 : bool record (edge, relation_kind, tree, tree);
106 1121508 : virtual bool record (basic_block, relation_kind, tree, tree) { return false; }
107 :
108 : // Query if there is any relation between SSA1 and SSA2.
109 : relation_kind query (gimple *s, tree ssa1, tree ssa2);
110 : relation_kind query (edge e, tree ssa1, tree ssa2);
111 13897737 : virtual relation_kind query (basic_block, tree, tree) { return VREL_VARYING; }
112 :
113 0 : virtual void dump (FILE *, basic_block) const { }
114 0 : virtual void dump (FILE *) const { }
115 : void debug () const;
116 : protected:
117 : friend class equiv_relation_iterator;
118 : friend class block_relation_iterator;
119 0 : virtual class relation_chain *next_relation (basic_block,
120 : relation_chain *,
121 : tree) const
122 0 : { return NULL; }
123 : // Return equivalency set for an SSA name in a basic block.
124 1 : virtual const_bitmap equiv_set (tree, basic_block) { return NULL; }
125 : // Return partial equivalency record for an SSA name.
126 1 : virtual const class pe_slice *partial_equiv_set (tree) { return NULL; }
127 : void valid_equivs (bitmap b, const_bitmap equivs, basic_block bb);
128 : // Query for a relation between two equivalency sets in a basic block.
129 0 : virtual relation_kind query (basic_block, const_bitmap, const_bitmap)
130 0 : { return VREL_VARYING; }
131 : friend class path_oracle;
132 : // Used to Avoid registering multiple eqiuvalences from the same statement.
133 : bitmap m_lhs_equiv_set_p;
134 : };
135 :
136 : // Instance with no storage used for default queries with no active oracle.
137 : extern relation_oracle default_relation_oracle;
138 :
139 : // This class represents an equivalency set, and contains a link to the next
140 : // one in the list to be searched.
141 :
142 : class equiv_chain
143 : {
144 : public:
145 : bitmap m_names; // ssa-names in equiv set.
146 : basic_block m_bb; // Block this belongs to
147 : equiv_chain *m_next; // Next in block list.
148 : void dump (FILE *f) const; // Show names in this list.
149 : equiv_chain *find (unsigned ssa);
150 : };
151 :
152 : class pe_slice
153 : {
154 : public:
155 : tree ssa_base; // Slice of this name.
156 : relation_kind code; // bits that are equivalent.
157 : bitmap members; // Other members in the partial equivalency.
158 : };
159 :
160 : // The equivalency oracle maintains equivalencies using the dominator tree.
161 : // Equivalencies apply to an entire basic block. Equivalencies on edges
162 : // can be represented only on edges whose destination is a single-pred block,
163 : // and the equivalence is simply applied to that successor block.
164 :
165 : class equiv_oracle : public relation_oracle
166 : {
167 : public:
168 : equiv_oracle ();
169 : ~equiv_oracle ();
170 :
171 : const_bitmap equiv_set (tree ssa, basic_block bb) final override;
172 : bool record (basic_block bb, relation_kind k, tree ssa1, tree ssa2) override;
173 :
174 : relation_kind partial_equiv (tree ssa1, tree ssa2, tree *base = NULL) const;
175 : relation_kind query (basic_block, tree, tree) override;
176 : relation_kind query (basic_block, const_bitmap, const_bitmap) override;
177 : void dump (FILE *f, basic_block bb) const override;
178 : void dump (FILE *f) const override;
179 :
180 : protected:
181 : bool add_partial_equiv (relation_kind, tree, tree);
182 : const pe_slice *partial_equiv_set (tree name) final override;
183 48368656 : inline bool has_equiv_p (unsigned v) { return bitmap_bit_p (m_equiv_set, v); }
184 : bitmap_obstack m_bitmaps;
185 : struct obstack m_chain_obstack;
186 : private:
187 : bitmap m_equiv_set; // Index by ssa-name. true if an equivalence exists.
188 : vec <equiv_chain *> m_equiv; // Index by BB. list of equivalences.
189 : vec <bitmap> m_self_equiv; // Index by ssa-name, self equivalency set.
190 : vec <pe_slice> m_partial; // Partial equivalencies.
191 :
192 : void limit_check (basic_block bb = NULL);
193 : equiv_chain *find_equiv_block (unsigned ssa, int bb) const;
194 : equiv_chain *find_equiv_dom (tree name, basic_block bb) const;
195 :
196 : bitmap register_equiv (basic_block bb, unsigned v, equiv_chain *equiv_1);
197 : bitmap register_equiv (basic_block bb, equiv_chain *equiv_1,
198 : equiv_chain *equiv_2);
199 : void register_initial_def (tree ssa);
200 : void add_equiv_to_block (basic_block bb, bitmap equiv);
201 : };
202 :
203 : // Summary block header for relations.
204 :
205 : class relation_chain_head
206 : {
207 : public:
208 : bitmap m_names; // ssa_names with relations in this block.
209 : class relation_chain *m_head; // List of relations in block.
210 : int m_num_relations; // Number of relations in block.
211 : relation_kind find_relation (const_bitmap b1, const_bitmap b2) const;
212 : };
213 :
214 : // A relation oracle maintains a set of relations between ssa_names using the
215 : // dominator tree structures. Equivalencies are considered a subset of
216 : // a general relation and maintained by an equivalence oracle by transparently
217 : // passing any EQ_EXPR relations to it.
218 : // Relations are handled at the basic block level. All relations apply to
219 : // an entire block, and are thus kept in a summary index by block.
220 : // Similar to the equivalence oracle, edges are handled by applying the
221 : // relation to the destination block of the edge, but ONLY if that block
222 : // has a single successor. For now.
223 :
224 : class dom_oracle : public equiv_oracle
225 : {
226 : public:
227 : dom_oracle (bool do_trans_p = true);
228 : ~dom_oracle ();
229 :
230 : bool record (basic_block bb, relation_kind k, tree op1, tree op2)
231 : final override;
232 :
233 : relation_kind query (basic_block bb, tree ssa1, tree ssa2) final override;
234 : relation_kind query (basic_block bb, const_bitmap b1, const_bitmap b2)
235 : final override;
236 :
237 : void dump (FILE *f, basic_block bb) const final override;
238 : void dump (FILE *f) const final override;
239 : protected:
240 : virtual relation_chain *next_relation (basic_block, relation_chain *,
241 : tree) const override;
242 : bool m_do_trans_p;
243 : bitmap m_tmp, m_tmp2;
244 : bitmap m_relation_set; // Index by ssa-name. True if a relation exists
245 : vec <relation_chain_head> m_relations; // Index by BB, list of relations.
246 : relation_kind find_relation_block (unsigned bb, const_bitmap b1,
247 : const_bitmap b2) const;
248 : relation_kind find_relation_block (int bb, unsigned v1, unsigned v2,
249 : relation_chain **obj = NULL) const;
250 : relation_kind find_relation_dom (basic_block bb, unsigned v1, unsigned v2) const;
251 : relation_chain *set_one_relation (basic_block bb, relation_kind k, tree op1,
252 : tree op2);
253 : void register_transitives (basic_block, const class value_relation &);
254 :
255 : };
256 :
257 : // A path_oracle implements relations in a list. The only sense of ordering
258 : // is the latest registered relation is the first found during a search.
259 : // It can be constructed with an optional "root" oracle which will be used
260 : // to look up any relations not found in the list.
261 : // This allows the client to walk paths starting at some block and register
262 : // and query relations along that path, ignoring other edges.
263 : //
264 : // For registering a relation, a query if made of the root oracle if there is
265 : // any known relationship at block BB, and it is combined with this new
266 : // relation and entered in the list.
267 : //
268 : // Queries are resolved by looking first in the list, and only if nothing is
269 : // found is the root oracle queried at block BB.
270 : //
271 : // reset_path is used to clear all locally registered paths to initial state.
272 :
273 : class path_oracle : public relation_oracle
274 : {
275 : public:
276 : path_oracle (relation_oracle *oracle = NULL);
277 : ~path_oracle ();
278 : const_bitmap equiv_set (tree, basic_block) final override;
279 : bool record (basic_block, relation_kind, tree, tree) final override;
280 : void killing_def (tree);
281 : relation_kind query (basic_block, tree, tree) final override;
282 : relation_kind query (basic_block, const_bitmap, const_bitmap) final override;
283 : void reset_path (relation_oracle *oracle = NULL);
284 52193554 : void set_root_oracle (relation_oracle *oracle) { m_root = oracle; }
285 : void dump (FILE *, basic_block) const final override;
286 : void dump (FILE *) const final override;
287 : private:
288 : bool register_equiv (basic_block bb, tree ssa1, tree ssa2);
289 : equiv_chain m_equiv;
290 : relation_chain_head m_relations;
291 : relation_oracle *m_root;
292 : bitmap m_killed_defs;
293 :
294 : bitmap_obstack m_bitmaps;
295 : struct obstack m_chain_obstack;
296 : };
297 :
298 : // Used to assist with iterating over the equivalence list.
299 : class equiv_relation_iterator {
300 : public:
301 : equiv_relation_iterator (relation_oracle *oracle, basic_block bb, tree name,
302 : bool full = true, bool partial = false);
303 : void next ();
304 : tree get_name (relation_kind *rel = NULL);
305 : protected:
306 : relation_oracle *m_oracle;
307 : const_bitmap m_bm;
308 : const pe_slice *m_pe;
309 : bitmap_iterator m_bi;
310 : unsigned m_y;
311 : tree m_name;
312 : };
313 :
314 : #define FOR_EACH_EQUIVALENCE(oracle, bb, name, equiv_name) \
315 : for (equiv_relation_iterator iter (oracle, bb, name, true, false); \
316 : ((equiv_name) = iter.get_name ()); \
317 : iter.next ())
318 :
319 : #define FOR_EACH_PARTIAL_EQUIV(oracle, bb, name, equiv_name, equiv_rel) \
320 : for (equiv_relation_iterator iter (oracle, bb, name, false, true); \
321 : ((equiv_name) = iter.get_name (&equiv_rel)); \
322 : iter.next ())
323 :
324 : #define FOR_EACH_PARTIAL_AND_FULL_EQUIV(oracle, bb, name, equiv_name, \
325 : equiv_rel) \
326 : for (equiv_relation_iterator iter (oracle, bb, name, true, true); \
327 : ((equiv_name) = iter.get_name (&equiv_rel)); \
328 : iter.next ())
329 :
330 : // -----------------------------------------------------------------------
331 :
332 : // Range-ops deals with a LHS and 2 operands. A relation trio is a set of
333 : // 3 potential relations packed into a single unsigned value.
334 : // 1 - LHS relation OP1
335 : // 2 - LHS relation OP2
336 : // 3 - OP1 relation OP2
337 : // VREL_VARYING is a value of 0, and is the default for each position.
338 : class relation_trio
339 : {
340 : public:
341 : relation_trio ();
342 : relation_trio (relation_kind lhs_op1, relation_kind lhs_op2,
343 : relation_kind op1_op2);
344 : relation_kind lhs_op1 ();
345 : relation_kind lhs_op2 ();
346 : relation_kind op1_op2 ();
347 : relation_trio swap_op1_op2 ();
348 :
349 : static relation_trio lhs_op1 (relation_kind k);
350 : static relation_trio lhs_op2 (relation_kind k);
351 : static relation_trio op1_op2 (relation_kind k);
352 :
353 : protected:
354 : unsigned m_val;
355 : };
356 :
357 : // Default VREL_VARYING for all 3 relations.
358 : #define TRIO_VARYING relation_trio ()
359 :
360 : #define TRIO_SHIFT 4
361 : #define TRIO_MASK 0x000F
362 :
363 : // These 3 classes are shortcuts for when a caller has a single relation to
364 : // pass as a trio, it can simply construct the appropriate one. The other
365 : // unspecified relations will be VREL_VARYING.
366 :
367 257177253 : inline relation_trio::relation_trio ()
368 : {
369 257177253 : STATIC_ASSERT (VREL_LAST <= (1 << TRIO_SHIFT));
370 257177253 : m_val = 0;
371 : }
372 :
373 237677359 : inline relation_trio::relation_trio (relation_kind lhs_op1,
374 : relation_kind lhs_op2,
375 : relation_kind op1_op2)
376 : {
377 237677359 : STATIC_ASSERT (VREL_LAST <= (1 << TRIO_SHIFT));
378 237677359 : unsigned i1 = (unsigned) lhs_op1;
379 237677359 : unsigned i2 = ((unsigned) lhs_op2) << TRIO_SHIFT;
380 237677359 : unsigned i3 = ((unsigned) op1_op2) << (TRIO_SHIFT * 2);
381 237677359 : m_val = i1 | i2 | i3;
382 : }
383 :
384 : inline relation_trio
385 774266 : relation_trio::lhs_op1 (relation_kind k)
386 : {
387 774266 : return relation_trio (k, VREL_VARYING, VREL_VARYING);
388 : }
389 : inline relation_trio
390 614600 : relation_trio::lhs_op2 (relation_kind k)
391 : {
392 614600 : return relation_trio (VREL_VARYING, k, VREL_VARYING);
393 : }
394 : inline relation_trio
395 170565712 : relation_trio::op1_op2 (relation_kind k)
396 : {
397 170565712 : return relation_trio (VREL_VARYING, VREL_VARYING, k);
398 : }
399 :
400 : inline relation_kind
401 21954142 : relation_trio::lhs_op1 ()
402 : {
403 12357722 : return (relation_kind) (m_val & TRIO_MASK);
404 : }
405 :
406 : inline relation_kind
407 9848652 : relation_trio::lhs_op2 ()
408 : {
409 9986996 : return (relation_kind) ((m_val >> TRIO_SHIFT) & TRIO_MASK);
410 : }
411 :
412 : inline relation_kind
413 318504623 : relation_trio::op1_op2 ()
414 : {
415 308655972 : return (relation_kind) ((m_val >> (TRIO_SHIFT * 2)) & TRIO_MASK);
416 : }
417 :
418 : inline relation_trio
419 9848651 : relation_trio::swap_op1_op2 ()
420 : {
421 9848651 : return relation_trio (lhs_op2 (), lhs_op1 (), relation_swap (op1_op2 ()));
422 : }
423 :
424 : // -----------------------------------------------------------------------
425 :
426 : // The value-relation class is used to encapsulate the representation of an
427 : // individual relation between 2 ssa-names, and to facilitate operating on
428 : // the relation.
429 :
430 : class value_relation
431 : {
432 : public:
433 : value_relation ();
434 : value_relation (relation_kind kind, tree n1, tree n2);
435 : void set_relation (relation_kind kind, tree n1, tree n2);
436 :
437 33846768 : inline relation_kind kind () const { return related; }
438 153752840 : inline tree op1 () const { return name1; }
439 151074257 : inline tree op2 () const { return name2; }
440 :
441 : relation_trio create_trio (tree lhs, tree op1, tree op2);
442 : bool union_ (value_relation &p);
443 : bool intersect (value_relation &p);
444 : void swap ();
445 : bool apply_transitive (const value_relation &rel);
446 :
447 : void dump (FILE *f) const;
448 : private:
449 : relation_kind related;
450 : tree name1, name2;
451 : };
452 :
453 : // Set relation R between ssa_name N1 and N2.
454 :
455 : inline void
456 119947803 : value_relation::set_relation (relation_kind r, tree n1, tree n2)
457 : {
458 119947803 : gcc_checking_assert (TREE_CODE (n1) == SSA_NAME
459 : && TREE_CODE (n2) == SSA_NAME);
460 119947803 : related = r;
461 119947803 : name1 = n1;
462 119947803 : name2 = n2;
463 119947803 : }
464 :
465 : // Default constructor.
466 :
467 : inline
468 124673884 : value_relation::value_relation ()
469 : {
470 124673884 : related = VREL_VARYING;
471 124673884 : name1 = NULL_TREE;
472 124673884 : name2 = NULL_TREE;
473 : }
474 :
475 : // Constructor for relation R between SSA version N1 and N2.
476 :
477 : inline
478 39959953 : value_relation::value_relation (relation_kind kind, tree n1, tree n2)
479 : {
480 39959953 : set_relation (kind, n1, n2);
481 : }
482 :
483 :
484 : class block_relation_iterator {
485 : public:
486 : block_relation_iterator (const relation_oracle *oracle, basic_block bb,
487 : value_relation &, tree name = NULL);
488 : void get_next_relation (value_relation &vr);
489 : const relation_oracle *m_oracle;
490 : basic_block m_bb;
491 : relation_chain *m_ptr;
492 : bool m_done;
493 : tree m_name;
494 : };
495 :
496 : #define FOR_EACH_RELATION_BB(oracle, bb, vr) \
497 : for (block_relation_iterator iter (oracle, bb, vr); \
498 : !iter.m_done; \
499 : iter.get_next_relation (vr))
500 :
501 : #define FOR_EACH_RELATION_NAME(oracle, bb, name, vr) \
502 : for (block_relation_iterator iter (oracle, bb, vr, name); \
503 : !iter.m_done; \
504 : iter.get_next_relation (vr))
505 :
506 :
507 : // Return the number of bits associated with partial equivalency T.
508 : // Return 0 if this is not a supported partial equivalency relation.
509 :
510 : inline int
511 17181939 : pe_to_bits (relation_kind t)
512 : {
513 17181939 : switch (t)
514 : {
515 : case VREL_PE8:
516 : return 8;
517 : case VREL_PE16:
518 : return 16;
519 : case VREL_PE32:
520 : return 32;
521 : case VREL_PE64:
522 : return 64;
523 : default:
524 : return 0;
525 : }
526 : }
527 :
528 : // Return the partial equivalency code associated with the number of BITS.
529 : // return VREL_VARYING if there is no exact match.
530 :
531 : inline relation_kind
532 37014228 : bits_to_pe (int bits)
533 : {
534 37014228 : switch (bits)
535 : {
536 : case 8:
537 : return VREL_PE8;
538 : case 16:
539 : return VREL_PE16;
540 : case 32:
541 : return VREL_PE32;
542 : case 64:
543 : return VREL_PE64;
544 : default:
545 : return VREL_VARYING;
546 : }
547 : }
548 :
549 : // Given partial equivalencies T1 and T2, return the smallest kind.
550 :
551 : inline relation_kind
552 8129019 : pe_min (relation_kind t1, relation_kind t2)
553 : {
554 8129019 : gcc_checking_assert (relation_partial_equiv_p (t1));
555 8129019 : gcc_checking_assert (relation_partial_equiv_p (t2));
556 : // VREL_PE are declared small to large, so simple min will suffice.
557 8129019 : return MIN (t1, t2);
558 : }
559 : #endif /* GCC_VALUE_RELATION_H */
|