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