1 /* Header file for the value range relational processing.
2 Copyright (C) 2020-2025 Free Software Foundation, Inc.
3 Contributed by Andrew MacLeod <amacleod@redhat.com>
5 This file is part of GCC.
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
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
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/>. */
23 #include "coretypes.h"
29 #include "gimple-range.h"
30 #include "tree-pretty-print.h"
31 #include "gimple-pretty-print.h"
32 #include "alloc-pool.h"
33 #include "dominance.h"
35 static const char *const kind_string
[VREL_LAST
] =
36 { "varying", "undefined", "<", "<=", ">", ">=", "==", "!=", "pe8", "pe16",
39 // Print a relation_kind REL to file F.
42 print_relation (FILE *f
, relation_kind rel
)
44 fprintf (f
, " %s ", kind_string
[rel
]);
47 // This table is used to negate the operands. op1 REL op2 -> !(op1 REL op2).
48 static const unsigned char rr_negate_table
[VREL_LAST
] = {
49 VREL_VARYING
, VREL_UNDEFINED
, VREL_GE
, VREL_GT
, VREL_LE
, VREL_LT
, VREL_NE
,
52 // Negate the relation, as in logical negation.
55 relation_negate (relation_kind r
)
57 return relation_kind (rr_negate_table
[r
]);
60 // This table is used to swap the operands. op1 REL op2 -> op2 REL op1.
61 static const unsigned char rr_swap_table
[VREL_LAST
] = {
62 VREL_VARYING
, VREL_UNDEFINED
, VREL_GT
, VREL_GE
, VREL_LT
, VREL_LE
, VREL_EQ
,
65 // Return the relation as if the operands were swapped.
68 relation_swap (relation_kind r
)
70 return relation_kind (rr_swap_table
[r
]);
73 // This table is used to perform an intersection between 2 relations.
75 static const unsigned char rr_intersect_table
[VREL_LAST
][VREL_LAST
] = {
77 { VREL_VARYING
, VREL_UNDEFINED
, VREL_LT
, VREL_LE
, VREL_GT
, VREL_GE
, VREL_EQ
,
80 { VREL_UNDEFINED
, VREL_UNDEFINED
, VREL_UNDEFINED
, VREL_UNDEFINED
,
81 VREL_UNDEFINED
, VREL_UNDEFINED
, VREL_UNDEFINED
, VREL_UNDEFINED
},
83 { VREL_LT
, VREL_UNDEFINED
, VREL_LT
, VREL_LT
, VREL_UNDEFINED
, VREL_UNDEFINED
,
84 VREL_UNDEFINED
, VREL_LT
},
86 { VREL_LE
, VREL_UNDEFINED
, VREL_LT
, VREL_LE
, VREL_UNDEFINED
, VREL_EQ
,
89 { VREL_GT
, VREL_UNDEFINED
, VREL_UNDEFINED
, VREL_UNDEFINED
, VREL_GT
, VREL_GT
,
90 VREL_UNDEFINED
, VREL_GT
},
92 { VREL_GE
, VREL_UNDEFINED
, VREL_UNDEFINED
, VREL_EQ
, VREL_GT
, VREL_GE
,
95 { VREL_EQ
, VREL_UNDEFINED
, VREL_UNDEFINED
, VREL_EQ
, VREL_UNDEFINED
, VREL_EQ
,
96 VREL_EQ
, VREL_UNDEFINED
},
98 { VREL_NE
, VREL_UNDEFINED
, VREL_LT
, VREL_LT
, VREL_GT
, VREL_GT
,
99 VREL_UNDEFINED
, VREL_NE
} };
102 // Intersect relation R1 with relation R2 and return the resulting relation.
105 relation_intersect (relation_kind r1
, relation_kind r2
)
107 return relation_kind (rr_intersect_table
[r1
][r2
]);
111 // This table is used to perform a union between 2 relations.
113 static const unsigned char rr_union_table
[VREL_LAST
][VREL_LAST
] = {
115 { VREL_VARYING
, VREL_VARYING
, VREL_VARYING
, VREL_VARYING
, VREL_VARYING
,
116 VREL_VARYING
, VREL_VARYING
, VREL_VARYING
},
118 { VREL_VARYING
, VREL_UNDEFINED
, VREL_LT
, VREL_LE
, VREL_GT
, VREL_GE
,
121 { VREL_VARYING
, VREL_LT
, VREL_LT
, VREL_LE
, VREL_NE
, VREL_VARYING
, VREL_LE
,
124 { VREL_VARYING
, VREL_LE
, VREL_LE
, VREL_LE
, VREL_VARYING
, VREL_VARYING
,
125 VREL_LE
, VREL_VARYING
},
127 { VREL_VARYING
, VREL_GT
, VREL_NE
, VREL_VARYING
, VREL_GT
, VREL_GE
, VREL_GE
,
130 { VREL_VARYING
, VREL_GE
, VREL_VARYING
, VREL_VARYING
, VREL_GE
, VREL_GE
,
131 VREL_GE
, VREL_VARYING
},
133 { VREL_VARYING
, VREL_EQ
, VREL_LE
, VREL_LE
, VREL_GE
, VREL_GE
, VREL_EQ
,
136 { VREL_VARYING
, VREL_NE
, VREL_NE
, VREL_VARYING
, VREL_NE
, VREL_VARYING
,
137 VREL_VARYING
, VREL_NE
} };
139 // Union relation R1 with relation R2 and return the result.
142 relation_union (relation_kind r1
, relation_kind r2
)
144 return relation_kind (rr_union_table
[r1
][r2
]);
148 // This table is used to determine transitivity between 2 relations.
149 // (A relation0 B) and (B relation1 C) implies (A result C)
151 static const unsigned char rr_transitive_table
[VREL_LAST
][VREL_LAST
] = {
153 { VREL_VARYING
, VREL_VARYING
, VREL_VARYING
, VREL_VARYING
, VREL_VARYING
,
154 VREL_VARYING
, VREL_VARYING
, VREL_VARYING
},
156 { VREL_VARYING
, VREL_VARYING
, VREL_VARYING
, VREL_VARYING
, VREL_VARYING
,
157 VREL_VARYING
, VREL_VARYING
, VREL_VARYING
},
159 { VREL_VARYING
, VREL_VARYING
, VREL_LT
, VREL_LT
, VREL_VARYING
, VREL_VARYING
,
160 VREL_LT
, VREL_VARYING
},
162 { VREL_VARYING
, VREL_VARYING
, VREL_LT
, VREL_LE
, VREL_VARYING
, VREL_VARYING
,
163 VREL_LE
, VREL_VARYING
},
165 { VREL_VARYING
, VREL_VARYING
, VREL_VARYING
, VREL_VARYING
, VREL_GT
, VREL_GT
,
166 VREL_GT
, VREL_VARYING
},
168 { VREL_VARYING
, VREL_VARYING
, VREL_VARYING
, VREL_VARYING
, VREL_GT
, VREL_GE
,
169 VREL_GE
, VREL_VARYING
},
171 { VREL_VARYING
, VREL_VARYING
, VREL_LT
, VREL_LE
, VREL_GT
, VREL_GE
, VREL_EQ
,
174 { VREL_VARYING
, VREL_VARYING
, VREL_VARYING
, VREL_VARYING
, VREL_VARYING
,
175 VREL_VARYING
, VREL_VARYING
, VREL_VARYING
} };
177 // Apply transitive operation between relation R1 and relation R2, and
178 // return the resulting relation, if any.
181 relation_transitive (relation_kind r1
, relation_kind r2
)
183 return relation_kind (rr_transitive_table
[r1
][r2
]);
186 // When one name is an equivalence of another, ensure the equivalence
187 // range is correct. Specifically for floating point, a +0 is also
188 // equivalent to a -0 which may not be reflected. See PR 111694.
191 adjust_equivalence_range (vrange
&range
)
193 if (range
.undefined_p () || !is_a
<frange
> (range
))
196 frange fr
= as_a
<frange
> (range
);
197 // If range includes 0 make sure both signs of zero are included.
198 if (fr
.contains_p (dconst0
) || fr
.contains_p (dconstm0
))
200 frange
zeros (range
.type (), dconstm0
, dconst0
);
201 range
.union_ (zeros
);
205 // Given an equivalence set EQUIV, set all the bits in B that are still valid
206 // members of EQUIV in basic block BB.
209 relation_oracle::valid_equivs (bitmap b
, const_bitmap equivs
, basic_block bb
)
213 EXECUTE_IF_SET_IN_BITMAP (equivs
, 0, i
, bi
)
215 tree ssa
= ssa_name (i
);
216 if (ssa
&& !SSA_NAME_IN_FREE_LIST (ssa
))
218 const_bitmap ssa_equiv
= equiv_set (ssa
, bb
);
219 if (ssa_equiv
== equivs
)
220 bitmap_set_bit (b
, i
);
225 // Return any known relation between SSA1 and SSA2 before stmt S is executed.
226 // If GET_RANGE is true, query the range of both operands first to ensure
227 // the definitions have been processed and any relations have be created.
230 relation_oracle::query (gimple
*s
, tree ssa1
, tree ssa2
)
232 if (TREE_CODE (ssa1
) != SSA_NAME
|| TREE_CODE (ssa2
) != SSA_NAME
)
234 return query (gimple_bb (s
), ssa1
, ssa2
);
237 // Return any known relation between SSA1 and SSA2 on edge E.
238 // If GET_RANGE is true, query the range of both operands first to ensure
239 // the definitions have been processed and any relations have be created.
242 relation_oracle::query (edge e
, tree ssa1
, tree ssa2
)
245 if (TREE_CODE (ssa1
) != SSA_NAME
|| TREE_CODE (ssa2
) != SSA_NAME
)
248 // Use destination block if it has a single predecessor, and this picks
249 // up any relation on the edge.
250 // Otherwise choose the src edge and the result is the same as on-exit.
251 if (!single_pred_p (e
->dest
))
256 return query (bb
, ssa1
, ssa2
);
258 // -------------------------------------------------------------------------
260 // The very first element in the m_equiv chain is actually just a summary
261 // element in which the m_names bitmap is used to indicate that an ssa_name
262 // has an equivalence set in this block.
263 // This allows for much faster traversal of the DOM chain, as a search for
264 // SSA_NAME simply requires walking the DOM chain until a block is found
265 // which has the bit for SSA_NAME set. Then scan for the equivalency set in
266 // that block. No previous lists need be searched.
268 // If SSA has an equivalence in this list, find and return it.
269 // Otherwise return NULL.
272 equiv_chain::find (unsigned ssa
)
274 equiv_chain
*ptr
= NULL
;
275 // If there are equiv sets and SSA is in one in this list, find it.
276 // Otherwise return NULL.
277 if (bitmap_bit_p (m_names
, ssa
))
279 for (ptr
= m_next
; ptr
; ptr
= ptr
->m_next
)
280 if (bitmap_bit_p (ptr
->m_names
, ssa
))
286 // Dump the names in this equivalence set.
289 equiv_chain::dump (FILE *f
) const
294 if (!m_names
|| bitmap_empty_p (m_names
))
296 fprintf (f
, "Equivalence set : [");
298 EXECUTE_IF_SET_IN_BITMAP (m_names
, 0, i
, bi
)
304 print_generic_expr (f
, ssa_name (i
), TDF_SLIM
);
310 // Instantiate an equivalency oracle.
312 equiv_oracle::equiv_oracle ()
314 bitmap_obstack_initialize (&m_bitmaps
);
316 m_equiv
.safe_grow_cleared (last_basic_block_for_fn (cfun
) + 1);
317 m_equiv_set
= BITMAP_ALLOC (&m_bitmaps
);
318 bitmap_tree_view (m_equiv_set
);
319 obstack_init (&m_chain_obstack
);
320 m_self_equiv
.create (0);
321 m_self_equiv
.safe_grow_cleared (num_ssa_names
+ 1);
322 m_partial
.create (0);
323 m_partial
.safe_grow_cleared (num_ssa_names
+ 1);
326 // Destruct an equivalency oracle.
328 equiv_oracle::~equiv_oracle ()
330 m_partial
.release ();
331 m_self_equiv
.release ();
332 obstack_free (&m_chain_obstack
, NULL
);
334 bitmap_obstack_release (&m_bitmaps
);
337 // Add a partial equivalence R between OP1 and OP2.
340 equiv_oracle::add_partial_equiv (relation_kind r
, tree op1
, tree op2
)
342 int v1
= SSA_NAME_VERSION (op1
);
343 int v2
= SSA_NAME_VERSION (op2
);
344 int prec2
= TYPE_PRECISION (TREE_TYPE (op2
));
345 int bits
= pe_to_bits (r
);
346 gcc_checking_assert (bits
&& prec2
>= bits
);
348 if (v1
>= (int)m_partial
.length () || v2
>= (int)m_partial
.length ())
349 m_partial
.safe_grow_cleared (num_ssa_names
+ 1);
350 gcc_checking_assert (v1
< (int)m_partial
.length ()
351 && v2
< (int)m_partial
.length ());
353 pe_slice
&pe1
= m_partial
[v1
];
354 pe_slice
&pe2
= m_partial
[v2
];
358 // If the definition pe1 already has an entry, either the stmt is
359 // being re-evaluated, or the def was used before being registered.
360 // In either case, if PE2 has an entry, we simply do nothing.
363 // If there are no uses of op2, do not register.
364 if (has_zero_uses (op2
))
366 // PE1 is the LHS and already has members, so everything in the set
367 // should be a slice of PE2 rather than PE1.
368 pe2
.code
= pe_min (r
, pe1
.code
);
370 pe2
.members
= pe1
.members
;
373 EXECUTE_IF_SET_IN_BITMAP (pe1
.members
, 0, x
, bi
)
375 m_partial
[x
].ssa_base
= op2
;
376 m_partial
[x
].code
= pe_min (m_partial
[x
].code
, pe2
.code
);
378 bitmap_set_bit (pe1
.members
, v2
);
383 // If there are no uses of op1, do not register.
384 if (has_zero_uses (op1
))
386 pe1
.ssa_base
= pe2
.ssa_base
;
387 // If pe2 is a 16 bit value, but only an 8 bit copy, we can't be any
388 // more than an 8 bit equivalence here, so choose MIN value.
389 pe1
.code
= pe_min (r
, pe2
.code
);
390 pe1
.members
= pe2
.members
;
391 bitmap_set_bit (pe1
.members
, v1
);
395 // If there are no uses of either operand, do not register.
396 if (has_zero_uses (op1
) || has_zero_uses (op2
))
398 // Neither name has an entry, simply create op1 as slice of op2.
399 pe2
.code
= bits_to_pe (TYPE_PRECISION (TREE_TYPE (op2
)));
400 if (pe2
.code
== VREL_VARYING
)
403 pe2
.members
= BITMAP_ALLOC (&m_bitmaps
);
404 bitmap_set_bit (pe2
.members
, v2
);
407 pe1
.members
= pe2
.members
;
408 bitmap_set_bit (pe1
.members
, v1
);
412 // Return the set of partial equivalences associated with NAME. The bitmap
413 // will be NULL if there are none.
416 equiv_oracle::partial_equiv_set (tree name
)
418 int v
= SSA_NAME_VERSION (name
);
419 if (v
>= (int)m_partial
.length ())
421 return &m_partial
[v
];
424 // Query if there is a partial equivalence between SSA1 and SSA2. Return
425 // VREL_VARYING if there is not one. If BASE is non-null, return the base
426 // ssa-name this is a slice of.
429 equiv_oracle::partial_equiv (tree ssa1
, tree ssa2
, tree
*base
) const
431 int v1
= SSA_NAME_VERSION (ssa1
);
432 int v2
= SSA_NAME_VERSION (ssa2
);
434 if (v1
>= (int)m_partial
.length () || v2
>= (int)m_partial
.length ())
437 const pe_slice
&pe1
= m_partial
[v1
];
438 const pe_slice
&pe2
= m_partial
[v2
];
439 if (pe1
.members
&& pe2
.members
== pe1
.members
)
442 *base
= pe1
.ssa_base
;
443 return pe_min (pe1
.code
, pe2
.code
);
449 // Find and return the equivalency set for SSA along the dominators of BB.
450 // This is the external API.
453 equiv_oracle::equiv_set (tree ssa
, basic_block bb
)
455 // Search the dominator tree for an equivalency.
456 equiv_chain
*equiv
= find_equiv_dom (ssa
, bb
);
458 return equiv
->m_names
;
460 // Otherwise return a cached equiv set containing just this SSA.
461 unsigned v
= SSA_NAME_VERSION (ssa
);
462 if (v
>= m_self_equiv
.length ())
463 m_self_equiv
.safe_grow_cleared (num_ssa_names
+ 1);
465 if (!m_self_equiv
[v
])
467 m_self_equiv
[v
] = BITMAP_ALLOC (&m_bitmaps
);
468 bitmap_set_bit (m_self_equiv
[v
], v
);
470 return m_self_equiv
[v
];
473 // Query if there is a relation (equivalence) between 2 SSA_NAMEs.
476 equiv_oracle::query (basic_block bb
, tree ssa1
, tree ssa2
)
478 // If the 2 ssa names share the same equiv set, they are equal.
479 if (equiv_set (ssa1
, bb
) == equiv_set (ssa2
, bb
))
482 // Check if there is a partial equivalence.
483 return partial_equiv (ssa1
, ssa2
);
486 // Query if there is a relation (equivalence) between 2 SSA_NAMEs.
489 equiv_oracle::query (basic_block bb ATTRIBUTE_UNUSED
, const_bitmap e1
,
492 // If the 2 ssa names share the same equiv set, they are equal.
493 if (bitmap_equal_p (e1
, e2
))
498 // If SSA has an equivalence in block BB, find and return it.
499 // Otherwise return NULL.
502 equiv_oracle::find_equiv_block (unsigned ssa
, int bb
) const
504 if (bb
>= (int)m_equiv
.length () || !m_equiv
[bb
])
507 return m_equiv
[bb
]->find (ssa
);
510 // Starting at block BB, walk the dominator chain looking for the nearest
511 // equivalence set containing NAME.
514 equiv_oracle::find_equiv_dom (tree name
, basic_block bb
) const
516 unsigned v
= SSA_NAME_VERSION (name
);
517 // Short circuit looking for names which have no equivalences.
518 // Saves time looking for something which does not exist.
519 if (!bitmap_bit_p (m_equiv_set
, v
))
522 // NAME has at least once equivalence set, check to see if it has one along
523 // the dominator tree.
524 for ( ; bb
; bb
= get_immediate_dominator (CDI_DOMINATORS
, bb
))
526 equiv_chain
*ptr
= find_equiv_block (v
, bb
->index
);
533 // Register equivalence between ssa_name V and set EQUIV in block BB,
536 equiv_oracle::register_equiv (basic_block bb
, unsigned v
, equiv_chain
*equiv
)
538 // V will have an equivalency now.
539 bitmap_set_bit (m_equiv_set
, v
);
541 // If that equiv chain is in this block, simply use it.
542 if (equiv
->m_bb
== bb
)
544 bitmap_set_bit (equiv
->m_names
, v
);
545 bitmap_set_bit (m_equiv
[bb
->index
]->m_names
, v
);
549 // Otherwise create an equivalence for this block which is a copy
550 // of equiv, the add V to the set.
551 bitmap b
= BITMAP_ALLOC (&m_bitmaps
);
552 valid_equivs (b
, equiv
->m_names
, bb
);
553 bitmap_set_bit (b
, v
);
557 // Register equivalence between set equiv_1 and equiv_2 in block BB.
558 // Return NULL if either name can be merged with the other. Otherwise
559 // return a pointer to the combined bitmap of names. This allows the
560 // caller to do any setup required for a new element.
563 equiv_oracle::register_equiv (basic_block bb
, equiv_chain
*equiv_1
,
564 equiv_chain
*equiv_2
)
566 // If equiv_1 is already in BB, use it as the combined set.
567 if (equiv_1
->m_bb
== bb
)
569 valid_equivs (equiv_1
->m_names
, equiv_2
->m_names
, bb
);
570 // Its hard to delete from a single linked list, so
571 // just clear the second one.
572 if (equiv_2
->m_bb
== bb
)
573 bitmap_clear (equiv_2
->m_names
);
575 // Ensure the new names are in the summary for BB.
576 bitmap_ior_into (m_equiv
[bb
->index
]->m_names
, equiv_1
->m_names
);
579 // If equiv_2 is in BB, use it for the combined set.
580 if (equiv_2
->m_bb
== bb
)
582 valid_equivs (equiv_2
->m_names
, equiv_1
->m_names
, bb
);
583 // Ensure the new names are in the summary.
584 bitmap_ior_into (m_equiv
[bb
->index
]->m_names
, equiv_2
->m_names
);
588 // At this point, neither equivalence is from this block.
589 bitmap b
= BITMAP_ALLOC (&m_bitmaps
);
590 valid_equivs (b
, equiv_1
->m_names
, bb
);
591 valid_equivs (b
, equiv_2
->m_names
, bb
);
595 // Create an equivalency set containing only SSA in its definition block.
596 // This is done the first time SSA is registered in an equivalency and blocks
597 // any DOM searches past the definition.
600 equiv_oracle::register_initial_def (tree ssa
)
602 if (SSA_NAME_IS_DEFAULT_DEF (ssa
))
604 basic_block bb
= gimple_bb (SSA_NAME_DEF_STMT (ssa
));
606 // If defining stmt is not in the IL, simply return.
609 gcc_checking_assert (!find_equiv_dom (ssa
, bb
));
611 unsigned v
= SSA_NAME_VERSION (ssa
);
612 bitmap_set_bit (m_equiv_set
, v
);
613 bitmap equiv_set
= BITMAP_ALLOC (&m_bitmaps
);
614 bitmap_set_bit (equiv_set
, v
);
615 add_equiv_to_block (bb
, equiv_set
);
618 // Register an equivalence between SSA1 and SSA2 in block BB.
619 // The equivalence oracle maintains a vector of equivalencies indexed by basic
620 // block. When an equivalence between SSA1 and SSA2 is registered in block BB,
621 // a query is made as to what equivalences both names have already, and
622 // any preexisting equivalences are merged to create a single equivalence
623 // containing all the ssa_names in this basic block.
626 equiv_oracle::record (basic_block bb
, relation_kind k
, tree ssa1
, tree ssa2
)
628 // Process partial equivalencies.
629 if (relation_partial_equiv_p (k
))
631 add_partial_equiv (k
, ssa1
, ssa2
);
634 // Only handle equality relations.
638 unsigned v1
= SSA_NAME_VERSION (ssa1
);
639 unsigned v2
= SSA_NAME_VERSION (ssa2
);
641 // If this is the first time an ssa_name has an equivalency registered
642 // create a self-equivalency record in the def block.
643 if (!bitmap_bit_p (m_equiv_set
, v1
))
644 register_initial_def (ssa1
);
645 if (!bitmap_bit_p (m_equiv_set
, v2
))
646 register_initial_def (ssa2
);
648 equiv_chain
*equiv_1
= find_equiv_dom (ssa1
, bb
);
649 equiv_chain
*equiv_2
= find_equiv_dom (ssa2
, bb
);
651 // Check if they are the same set
652 if (equiv_1
&& equiv_1
== equiv_2
)
657 // Case where we have 2 SSA_NAMEs that are not in any set.
658 if (!equiv_1
&& !equiv_2
)
660 bitmap_set_bit (m_equiv_set
, v1
);
661 bitmap_set_bit (m_equiv_set
, v2
);
663 equiv_set
= BITMAP_ALLOC (&m_bitmaps
);
664 bitmap_set_bit (equiv_set
, v1
);
665 bitmap_set_bit (equiv_set
, v2
);
667 else if (!equiv_1
&& equiv_2
)
668 equiv_set
= register_equiv (bb
, v1
, equiv_2
);
669 else if (equiv_1
&& !equiv_2
)
670 equiv_set
= register_equiv (bb
, v2
, equiv_1
);
672 equiv_set
= register_equiv (bb
, equiv_1
, equiv_2
);
674 // A non-null return is a bitmap that is to be added to the current
675 // block as a new equivalence.
679 add_equiv_to_block (bb
, equiv_set
);
682 // Add an equivalency record in block BB containing bitmap EQUIV_SET.
683 // Note the internal caller is responsible for allocating EQUIV_SET properly.
686 equiv_oracle::add_equiv_to_block (basic_block bb
, bitmap equiv_set
)
690 // Check if this is the first time a block has an equivalence added.
691 // and create a header block. And set the summary for this block.
693 if (!m_equiv
[bb
->index
])
695 ptr
= (equiv_chain
*) obstack_alloc (&m_chain_obstack
,
696 sizeof (equiv_chain
));
697 ptr
->m_names
= BITMAP_ALLOC (&m_bitmaps
);
698 bitmap_copy (ptr
->m_names
, equiv_set
);
701 m_equiv
[bb
->index
] = ptr
;
704 // Now create the element for this equiv set and initialize it.
705 ptr
= (equiv_chain
*) obstack_alloc (&m_chain_obstack
, sizeof (equiv_chain
));
706 ptr
->m_names
= equiv_set
;
708 gcc_checking_assert (bb
->index
< (int)m_equiv
.length ());
709 ptr
->m_next
= m_equiv
[bb
->index
]->m_next
;
710 m_equiv
[bb
->index
]->m_next
= ptr
;
711 bitmap_ior_into (m_equiv
[bb
->index
]->m_names
, equiv_set
);
714 // Make sure the BB vector is big enough and grow it if needed.
717 equiv_oracle::limit_check (basic_block bb
)
719 int i
= (bb
) ? bb
->index
: last_basic_block_for_fn (cfun
);
720 if (i
>= (int)m_equiv
.length ())
721 m_equiv
.safe_grow_cleared (last_basic_block_for_fn (cfun
) + 1);
724 // Dump the equivalence sets in BB to file F.
727 equiv_oracle::dump (FILE *f
, basic_block bb
) const
729 if (bb
->index
>= (int)m_equiv
.length ())
731 // Process equivalences.
732 if (m_equiv
[bb
->index
])
734 equiv_chain
*ptr
= m_equiv
[bb
->index
]->m_next
;
735 for (; ptr
; ptr
= ptr
->m_next
)
738 // Look for partial equivalences defined in this block..
739 for (unsigned i
= 0; i
< num_ssa_names
; i
++)
741 tree name
= ssa_name (i
);
742 if (!gimple_range_ssa_p (name
) || !SSA_NAME_DEF_STMT (name
))
744 if (i
>= m_partial
.length ())
746 tree base
= m_partial
[i
].ssa_base
;
747 if (base
&& name
!= base
&& gimple_bb (SSA_NAME_DEF_STMT (name
)) == bb
)
749 relation_kind k
= partial_equiv (name
, base
);
750 if (k
!= VREL_VARYING
)
752 value_relation
vr (k
, name
, base
);
753 fprintf (f
, "Partial equiv ");
761 // Dump all equivalence sets known to the oracle.
764 equiv_oracle::dump (FILE *f
) const
766 fprintf (f
, "Equivalency dump\n");
767 for (unsigned i
= 0; i
< m_equiv
.length (); i
++)
768 if (m_equiv
[i
] && BASIC_BLOCK_FOR_FN (cfun
, i
))
770 fprintf (f
, "BB%d\n", i
);
771 dump (f
, BASIC_BLOCK_FOR_FN (cfun
, i
));
776 // --------------------------------------------------------------------------
778 // Adjust the relation by Swapping the operands and relation.
781 value_relation::swap ()
783 related
= relation_swap (related
);
789 // Perform an intersection between 2 relations. *this &&= p.
790 // Return false if the relations cannot be intersected.
793 value_relation::intersect (value_relation
&p
)
795 // Save previous value
796 relation_kind old
= related
;
798 if (p
.op1 () == op1 () && p
.op2 () == op2 ())
799 related
= relation_intersect (kind (), p
.kind ());
800 else if (p
.op2 () == op1 () && p
.op1 () == op2 ())
801 related
= relation_intersect (kind (), relation_swap (p
.kind ()));
805 return old
!= related
;
808 // Perform a union between 2 relations. *this ||= p.
811 value_relation::union_ (value_relation
&p
)
813 // Save previous value
814 relation_kind old
= related
;
816 if (p
.op1 () == op1 () && p
.op2 () == op2 ())
817 related
= relation_union (kind(), p
.kind());
818 else if (p
.op2 () == op1 () && p
.op1 () == op2 ())
819 related
= relation_union (kind(), relation_swap (p
.kind ()));
823 return old
!= related
;
826 // Identify and apply any transitive relations between REL
827 // and THIS. Return true if there was a transformation.
830 value_relation::apply_transitive (const value_relation
&rel
)
832 relation_kind k
= VREL_VARYING
;
834 // Identify any common operand, and normalize the relations to
835 // the form : A < B B < C produces A < C
836 if (rel
.op1 () == name2
)
839 if (rel
.op2 () == name1
)
841 k
= relation_transitive (kind (), rel
.kind ());
842 if (k
!= VREL_VARYING
)
849 else if (rel
.op1 () == name1
)
852 if (rel
.op2 () == name2
)
854 k
= relation_transitive (relation_swap (kind ()), rel
.kind ());
855 if (k
!= VREL_VARYING
)
863 else if (rel
.op2 () == name2
)
866 if (rel
.op1 () == name1
)
868 k
= relation_transitive (kind (), relation_swap (rel
.kind ()));
869 if (k
!= VREL_VARYING
)
876 else if (rel
.op2 () == name1
)
879 if (rel
.op1 () == name2
)
881 k
= relation_transitive (relation_swap (kind ()),
882 relation_swap (rel
.kind ()));
883 if (k
!= VREL_VARYING
)
894 // Create a trio from this value relation given LHS, OP1 and OP2.
897 value_relation::create_trio (tree lhs
, tree op1
, tree op2
)
900 if (lhs
== name1
&& op1
== name2
)
902 else if (lhs
== name2
&& op1
== name1
)
903 lhs_1
= relation_swap (related
);
905 lhs_1
= VREL_VARYING
;
908 if (lhs
== name1
&& op2
== name2
)
910 else if (lhs
== name2
&& op2
== name1
)
911 lhs_2
= relation_swap (related
);
913 lhs_2
= VREL_VARYING
;
916 if (op1
== name1
&& op2
== name2
)
918 else if (op1
== name2
&& op2
== name1
)
919 op_op
= relation_swap (related
);
923 op_op
= VREL_VARYING
;
925 return relation_trio (lhs_1
, lhs_2
, op_op
);
928 // Dump the relation to file F.
931 value_relation::dump (FILE *f
) const
933 if (!name1
|| !name2
)
935 fprintf (f
, "no relation registered");
939 print_generic_expr (f
, op1 (), TDF_SLIM
);
940 print_relation (f
, kind ());
941 print_generic_expr (f
, op2 (), TDF_SLIM
);
945 // This container is used to link relations in a chain.
947 class relation_chain
: public value_relation
950 relation_chain
*m_next
;
953 // Given relation record PTR in block BB, return the next relation in the
954 // list. If PTR is NULL, retreive the first relation in BB.
955 // If NAME is sprecified, return only relations which include NAME.
956 // Return NULL when there are no relations left.
959 dom_oracle::next_relation (basic_block bb
, relation_chain
*ptr
,
963 // No value_relation pointer is used to intialize the iterator.
967 if (bbi
>= (int)m_relations
.length())
970 p
= m_relations
[bbi
].m_head
;
976 for ( ; p
; p
= p
->m_next
)
977 if (p
->op1 () == name
|| p
->op2 () == name
)
982 // Instatiate a block relation iterator to iterate over the relations
983 // on exit from block BB in ORACLE. Limit this to relations involving NAME
984 // if specified. Return the first such relation in VR if there is one.
986 block_relation_iterator::block_relation_iterator (const relation_oracle
*oracle
,
994 m_ptr
= oracle
->next_relation (bb
, NULL
, m_name
);
1004 // Retreive the next relation from the iterator and return it in VR.
1007 block_relation_iterator::get_next_relation (value_relation
&vr
)
1009 m_ptr
= m_oracle
->next_relation (m_bb
, m_ptr
, m_name
);
1015 if (vr
.op1 () != m_name
)
1017 gcc_checking_assert (vr
.op2 () == m_name
);
1026 // ------------------------------------------------------------------------
1028 // Find the relation between any ssa_name in B1 and any name in B2 in LIST.
1029 // This will allow equivalencies to be applied to any SSA_NAME in a relation.
1032 relation_chain_head::find_relation (const_bitmap b1
, const_bitmap b2
) const
1035 return VREL_VARYING
;
1037 // If both b1 and b2 aren't referenced in this block, cant be a relation
1038 if (!bitmap_intersect_p (m_names
, b1
) || !bitmap_intersect_p (m_names
, b2
))
1039 return VREL_VARYING
;
1041 // Search for the first relation that contains BOTH an element from B1
1042 // and B2, and return that relation.
1043 for (relation_chain
*ptr
= m_head
; ptr
; ptr
= ptr
->m_next
)
1045 unsigned op1
= SSA_NAME_VERSION (ptr
->op1 ());
1046 unsigned op2
= SSA_NAME_VERSION (ptr
->op2 ());
1047 if (bitmap_bit_p (b1
, op1
) && bitmap_bit_p (b2
, op2
))
1048 return ptr
->kind ();
1049 if (bitmap_bit_p (b1
, op2
) && bitmap_bit_p (b2
, op1
))
1050 return relation_swap (ptr
->kind ());
1053 return VREL_VARYING
;
1056 // Instantiate a relation oracle.
1058 dom_oracle::dom_oracle (bool do_trans_p
)
1060 m_do_trans_p
= do_trans_p
;
1061 m_relations
.create (0);
1062 m_relations
.safe_grow_cleared (last_basic_block_for_fn (cfun
) + 1);
1063 m_relation_set
= BITMAP_ALLOC (&m_bitmaps
);
1064 m_tmp
= BITMAP_ALLOC (&m_bitmaps
);
1065 m_tmp2
= BITMAP_ALLOC (&m_bitmaps
);
1068 // Destruct a relation oracle.
1070 dom_oracle::~dom_oracle ()
1072 m_relations
.release ();
1075 // Register relation K between ssa_name OP1 and OP2 on STMT.
1078 relation_oracle::record (gimple
*stmt
, relation_kind k
, tree op1
, tree op2
)
1080 gcc_checking_assert (TREE_CODE (op1
) == SSA_NAME
);
1081 gcc_checking_assert (TREE_CODE (op2
) == SSA_NAME
);
1082 gcc_checking_assert (stmt
&& gimple_bb (stmt
));
1084 // Don't register lack of a relation.
1085 if (k
== VREL_VARYING
)
1088 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1090 value_relation
vr (k
, op1
, op2
);
1091 fprintf (dump_file
, " Registering value_relation ");
1092 vr
.dump (dump_file
);
1093 fprintf (dump_file
, " (bb%d) at ", gimple_bb (stmt
)->index
);
1094 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1097 // If an equivalence is being added between a PHI and one of its arguments
1098 // make sure that that argument is not defined in the same block.
1099 // This can happen along back edges and the equivalence will not be
1100 // applicable as it would require a use before def.
1101 if (k
== VREL_EQ
&& is_a
<gphi
*> (stmt
))
1103 tree phi_def
= gimple_phi_result (stmt
);
1104 gcc_checking_assert (phi_def
== op1
|| phi_def
== op2
);
1108 if (gimple_bb (stmt
) == gimple_bb (SSA_NAME_DEF_STMT (arg
)))
1110 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1112 fprintf (dump_file
, " Not registered due to ");
1113 print_generic_expr (dump_file
, arg
, TDF_SLIM
);
1114 fprintf (dump_file
, " being defined in the same block.\n");
1119 record (gimple_bb (stmt
), k
, op1
, op2
);
1122 // Register relation K between ssa_name OP1 and OP2 on edge E.
1125 relation_oracle::record (edge e
, relation_kind k
, tree op1
, tree op2
)
1127 gcc_checking_assert (TREE_CODE (op1
) == SSA_NAME
);
1128 gcc_checking_assert (TREE_CODE (op2
) == SSA_NAME
);
1130 // Do not register lack of relation, or blocks which have more than
1131 // edge E for a predecessor.
1132 if (k
== VREL_VARYING
|| !single_pred_p (e
->dest
))
1135 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1137 value_relation
vr (k
, op1
, op2
);
1138 fprintf (dump_file
, " Registering value_relation ");
1139 vr
.dump (dump_file
);
1140 fprintf (dump_file
, " on (%d->%d)\n", e
->src
->index
, e
->dest
->index
);
1143 record (e
->dest
, k
, op1
, op2
);
1146 // Register relation K between OP! and OP2 in block BB.
1147 // This creates the record and searches for existing records in the dominator
1148 // tree to merge with.
1151 dom_oracle::record (basic_block bb
, relation_kind k
, tree op1
, tree op2
)
1153 // If the 2 ssa_names are the same, do nothing. An equivalence is implied,
1154 // and no other relation makes sense.
1158 // Equivalencies are handled by the equivalence oracle.
1159 if (relation_equiv_p (k
))
1160 equiv_oracle::record (bb
, k
, op1
, op2
);
1163 // if neither op1 nor op2 are in a relation before this is registered,
1164 // there will be no transitive.
1165 bool check
= bitmap_bit_p (m_relation_set
, SSA_NAME_VERSION (op1
))
1166 || bitmap_bit_p (m_relation_set
, SSA_NAME_VERSION (op2
));
1167 relation_chain
*ptr
= set_one_relation (bb
, k
, op1
, op2
);
1169 && (m_relations
[bb
->index
].m_num_relations
1170 < param_relation_block_limit
))
1171 register_transitives (bb
, *ptr
);
1175 // Register relation K between OP! and OP2 in block BB.
1176 // This creates the record and searches for existing records in the dominator
1177 // tree to merge with. Return the record, or NULL if no record was created.
1180 dom_oracle::set_one_relation (basic_block bb
, relation_kind k
, tree op1
,
1183 gcc_checking_assert (k
!= VREL_VARYING
&& k
!= VREL_EQ
);
1185 value_relation
vr(k
, op1
, op2
);
1186 int bbi
= bb
->index
;
1188 if (bbi
>= (int)m_relations
.length())
1189 m_relations
.safe_grow_cleared (last_basic_block_for_fn (cfun
) + 1);
1191 // Summary bitmap indicating what ssa_names have relations in this BB.
1192 bitmap bm
= m_relations
[bbi
].m_names
;
1194 bm
= m_relations
[bbi
].m_names
= BITMAP_ALLOC (&m_bitmaps
);
1195 unsigned v1
= SSA_NAME_VERSION (op1
);
1196 unsigned v2
= SSA_NAME_VERSION (op2
);
1199 relation_chain
*ptr
;
1200 curr
= find_relation_block (bbi
, v1
, v2
, &ptr
);
1201 // There is an existing relation in this block, just intersect with it.
1202 if (curr
!= VREL_VARYING
)
1204 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1206 fprintf (dump_file
, " Intersecting with existing ");
1207 ptr
->dump (dump_file
);
1209 // Check into whether we can simply replace the relation rather than
1210 // intersecting it. This may help with some optimistic iterative
1211 // updating algorithms.
1212 bool new_rel
= ptr
->intersect (vr
);
1213 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1215 fprintf (dump_file
, " to produce ");
1216 ptr
->dump (dump_file
);
1217 fprintf (dump_file
, " %s.\n", new_rel
? "Updated" : "No Change");
1219 // If there was no change, return no record..
1225 if (m_relations
[bbi
].m_num_relations
>= param_relation_block_limit
)
1227 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1228 fprintf (dump_file
, " Not registered due to bb being full\n");
1231 m_relations
[bbi
].m_num_relations
++;
1232 // Check for an existing relation further up the DOM chain.
1233 // By including dominating relations, The first one found in any search
1234 // will be the aggregate of all the previous ones.
1235 curr
= find_relation_dom (bb
, v1
, v2
);
1236 if (curr
!= VREL_VARYING
)
1237 k
= relation_intersect (curr
, k
);
1239 bitmap_set_bit (bm
, v1
);
1240 bitmap_set_bit (bm
, v2
);
1241 bitmap_set_bit (m_relation_set
, v1
);
1242 bitmap_set_bit (m_relation_set
, v2
);
1244 ptr
= (relation_chain
*) obstack_alloc (&m_chain_obstack
,
1245 sizeof (relation_chain
));
1246 ptr
->set_relation (k
, op1
, op2
);
1247 ptr
->m_next
= m_relations
[bbi
].m_head
;
1248 m_relations
[bbi
].m_head
= ptr
;
1253 // Starting at ROOT_BB search the DOM tree looking for relations which
1254 // may produce transitive relations to RELATION. EQUIV1 and EQUIV2 are
1255 // bitmaps for op1/op2 and any of their equivalences that should also be
1259 dom_oracle::register_transitives (basic_block root_bb
,
1260 const value_relation
&relation
)
1262 // Only register transitives if they are requested.
1266 // Only apply transitives to certain kinds of operations.
1267 switch (relation
.kind ())
1278 const_bitmap equiv1
= equiv_set (relation
.op1 (), root_bb
);
1279 const_bitmap equiv2
= equiv_set (relation
.op2 (), root_bb
);
1281 const unsigned work_budget
= param_transitive_relations_work_bound
;
1282 unsigned avail_budget
= work_budget
;
1283 for (bb
= root_bb
; bb
;
1284 /* Advancing to the next immediate dominator eats from the budget,
1285 if none is left after that there's no point to continue. */
1286 bb
= (--avail_budget
> 0
1287 ? get_immediate_dominator (CDI_DOMINATORS
, bb
) : nullptr))
1289 int bbi
= bb
->index
;
1290 if (bbi
>= (int)m_relations
.length())
1292 const_bitmap bm
= m_relations
[bbi
].m_names
;
1295 if (!bitmap_intersect_p (bm
, equiv1
) && !bitmap_intersect_p (bm
, equiv2
))
1297 // At least one of the 2 ops has a relation in this block.
1298 relation_chain
*ptr
;
1299 for (ptr
= m_relations
[bbi
].m_head
; ptr
; ptr
= ptr
->m_next
)
1301 // In the presence of an equivalence, 2 operands may do not
1302 // naturally match. ie with equivalence a_2 == b_3
1303 // given c_1 < a_2 && b_3 < d_4
1304 // convert the second relation (b_3 < d_4) to match any
1305 // equivalences to found in the first relation.
1306 // ie convert b_3 < d_4 to a_2 < d_4, which then exposes the
1307 // transitive operation: c_1 < a_2 && a_2 < d_4 -> c_1 < d_4
1310 tree p1
= ptr
->op1 ();
1311 tree p2
= ptr
->op2 ();
1312 // Find which equivalence is in the first operand.
1313 if (bitmap_bit_p (equiv1
, SSA_NAME_VERSION (p1
)))
1315 else if (bitmap_bit_p (equiv1
, SSA_NAME_VERSION (p2
)))
1320 // Find which equivalence is in the second operand.
1321 if (bitmap_bit_p (equiv2
, SSA_NAME_VERSION (p1
)))
1323 else if (bitmap_bit_p (equiv2
, SSA_NAME_VERSION (p2
)))
1328 // Ignore if both NULL (not relevant relation) or the same,
1334 // Any operand not an equivalence, just take the real operand.
1336 r1
= relation
.op1 ();
1338 r2
= relation
.op2 ();
1340 value_relation
nr (relation
.kind (), r1
, r2
);
1341 if (nr
.apply_transitive (*ptr
))
1343 // If the new relation is already present we know any
1344 // further processing is already reflected above it.
1345 // When we ran into the limit of relations on root_bb
1346 // we can give up as well.
1347 if (!set_one_relation (root_bb
, nr
.kind (),
1348 nr
.op1 (), nr
.op2 ()))
1350 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1353 " Registering transitive relation ");
1354 nr
.dump (dump_file
);
1355 fputc ('\n', dump_file
);
1359 /* Processed one relation, abort if we've eaten up our budget. */
1360 if (--avail_budget
== 0)
1366 // Find the relation between any ssa_name in B1 and any name in B2 in block BB.
1367 // This will allow equivalencies to be applied to any SSA_NAME in a relation.
1370 dom_oracle::find_relation_block (unsigned bb
, const_bitmap b1
,
1371 const_bitmap b2
) const
1373 if (bb
>= m_relations
.length())
1374 return VREL_VARYING
;
1376 return m_relations
[bb
].find_relation (b1
, b2
);
1379 // Search the DOM tree for a relation between an element of equivalency set B1
1380 // and B2, starting with block BB.
1383 dom_oracle::query (basic_block bb
, const_bitmap b1
, const_bitmap b2
)
1386 if (bitmap_equal_p (b1
, b2
))
1389 // If either name does not occur in a relation anywhere, there isn't one.
1390 if (!bitmap_intersect_p (m_relation_set
, b1
)
1391 || !bitmap_intersect_p (m_relation_set
, b2
))
1392 return VREL_VARYING
;
1394 // Search each block in the DOM tree checking.
1395 for ( ; bb
; bb
= get_immediate_dominator (CDI_DOMINATORS
, bb
))
1397 r
= find_relation_block (bb
->index
, b1
, b2
);
1398 if (r
!= VREL_VARYING
)
1401 return VREL_VARYING
;
1405 // Find a relation in block BB between ssa version V1 and V2. If a relation
1406 // is found, return a pointer to the chain object in OBJ.
1409 dom_oracle::find_relation_block (int bb
, unsigned v1
, unsigned v2
,
1410 relation_chain
**obj
) const
1412 if (bb
>= (int)m_relations
.length())
1413 return VREL_VARYING
;
1415 const_bitmap bm
= m_relations
[bb
].m_names
;
1417 return VREL_VARYING
;
1419 // If both b1 and b2 aren't referenced in this block, cant be a relation
1420 if (!bitmap_bit_p (bm
, v1
) || !bitmap_bit_p (bm
, v2
))
1421 return VREL_VARYING
;
1423 relation_chain
*ptr
;
1424 for (ptr
= m_relations
[bb
].m_head
; ptr
; ptr
= ptr
->m_next
)
1426 unsigned op1
= SSA_NAME_VERSION (ptr
->op1 ());
1427 unsigned op2
= SSA_NAME_VERSION (ptr
->op2 ());
1428 if (v1
== op1
&& v2
== op2
)
1432 return ptr
->kind ();
1434 if (v1
== op2
&& v2
== op1
)
1438 return relation_swap (ptr
->kind ());
1442 return VREL_VARYING
;
1445 // Find a relation between SSA version V1 and V2 in the dominator tree
1446 // starting with block BB
1449 dom_oracle::find_relation_dom (basic_block bb
, unsigned v1
, unsigned v2
) const
1452 // IF either name does not occur in a relation anywhere, there isn't one.
1453 if (!bitmap_bit_p (m_relation_set
, v1
) || !bitmap_bit_p (m_relation_set
, v2
))
1454 return VREL_VARYING
;
1456 for ( ; bb
; bb
= get_immediate_dominator (CDI_DOMINATORS
, bb
))
1458 r
= find_relation_block (bb
->index
, v1
, v2
);
1459 if (r
!= VREL_VARYING
)
1462 return VREL_VARYING
;
1466 // Query if there is a relation between SSA1 and SS2 in block BB or a
1470 dom_oracle::query (basic_block bb
, tree ssa1
, tree ssa2
)
1473 unsigned v1
= SSA_NAME_VERSION (ssa1
);
1474 unsigned v2
= SSA_NAME_VERSION (ssa2
);
1478 // If v1 or v2 do not have any relations or equivalences, a partial
1479 // equivalence is the only possibility.
1480 if ((!bitmap_bit_p (m_relation_set
, v1
) && !has_equiv_p (v1
))
1481 || (!bitmap_bit_p (m_relation_set
, v2
) && !has_equiv_p (v2
)))
1482 return partial_equiv (ssa1
, ssa2
);
1484 // Check for equivalence first. They must be in each equivalency set.
1485 const_bitmap equiv1
= equiv_set (ssa1
, bb
);
1486 const_bitmap equiv2
= equiv_set (ssa2
, bb
);
1487 if (bitmap_bit_p (equiv1
, v2
) && bitmap_bit_p (equiv2
, v1
))
1490 kind
= partial_equiv (ssa1
, ssa2
);
1491 if (kind
!= VREL_VARYING
)
1494 // Initially look for a direct relationship and just return that.
1495 kind
= find_relation_dom (bb
, v1
, v2
);
1496 if (kind
!= VREL_VARYING
)
1499 // Query using the equivalence sets.
1500 kind
= query (bb
, equiv1
, equiv2
);
1504 // Dump all the relations in block BB to file F.
1507 dom_oracle::dump (FILE *f
, basic_block bb
) const
1509 equiv_oracle::dump (f
,bb
);
1511 if (bb
->index
>= (int)m_relations
.length ())
1513 if (!m_relations
[bb
->index
].m_names
)
1517 FOR_EACH_RELATION_BB (this, bb
, vr
)
1519 fprintf (f
, "Relational : ");
1525 // Dump all the relations known to file F.
1528 dom_oracle::dump (FILE *f
) const
1530 fprintf (f
, "Relation dump\n");
1531 for (unsigned i
= 0; i
< m_relations
.length (); i
++)
1532 if (BASIC_BLOCK_FOR_FN (cfun
, i
))
1534 fprintf (f
, "BB%d\n", i
);
1535 dump (f
, BASIC_BLOCK_FOR_FN (cfun
, i
));
1540 relation_oracle::debug () const
1545 path_oracle::path_oracle (relation_oracle
*oracle
)
1547 set_root_oracle (oracle
);
1548 bitmap_obstack_initialize (&m_bitmaps
);
1549 obstack_init (&m_chain_obstack
);
1551 // Initialize header records.
1552 m_equiv
.m_names
= BITMAP_ALLOC (&m_bitmaps
);
1553 m_equiv
.m_bb
= NULL
;
1554 m_equiv
.m_next
= NULL
;
1555 m_relations
.m_names
= BITMAP_ALLOC (&m_bitmaps
);
1556 m_relations
.m_head
= NULL
;
1557 m_killed_defs
= BITMAP_ALLOC (&m_bitmaps
);
1560 path_oracle::~path_oracle ()
1562 obstack_free (&m_chain_obstack
, NULL
);
1563 bitmap_obstack_release (&m_bitmaps
);
1566 // Return the equiv set for SSA, and if there isn't one, check for equivs
1567 // starting in block BB.
1570 path_oracle::equiv_set (tree ssa
, basic_block bb
)
1572 // Check the list first.
1573 equiv_chain
*ptr
= m_equiv
.find (SSA_NAME_VERSION (ssa
));
1575 return ptr
->m_names
;
1577 // Otherwise defer to the root oracle.
1579 return m_root
->equiv_set (ssa
, bb
);
1581 // Allocate a throw away bitmap if there isn't a root oracle.
1582 bitmap tmp
= BITMAP_ALLOC (&m_bitmaps
);
1583 bitmap_set_bit (tmp
, SSA_NAME_VERSION (ssa
));
1587 // Register an equivalence between SSA1 and SSA2 resolving unknowns from
1591 path_oracle::register_equiv (basic_block bb
, tree ssa1
, tree ssa2
)
1593 const_bitmap equiv_1
= equiv_set (ssa1
, bb
);
1594 const_bitmap equiv_2
= equiv_set (ssa2
, bb
);
1596 // Check if they are the same set, if so, we're done.
1597 if (bitmap_equal_p (equiv_1
, equiv_2
))
1600 // Don't mess around, simply create a new record and insert it first.
1601 bitmap b
= BITMAP_ALLOC (&m_bitmaps
);
1602 valid_equivs (b
, equiv_1
, bb
);
1603 valid_equivs (b
, equiv_2
, bb
);
1605 equiv_chain
*ptr
= (equiv_chain
*) obstack_alloc (&m_chain_obstack
,
1606 sizeof (equiv_chain
));
1609 ptr
->m_next
= m_equiv
.m_next
;
1610 m_equiv
.m_next
= ptr
;
1611 bitmap_ior_into (m_equiv
.m_names
, b
);
1614 // Register killing definition of an SSA_NAME.
1617 path_oracle::killing_def (tree ssa
)
1619 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1621 fprintf (dump_file
, " Registering killing_def (path_oracle) ");
1622 print_generic_expr (dump_file
, ssa
, TDF_SLIM
);
1623 fprintf (dump_file
, "\n");
1626 unsigned v
= SSA_NAME_VERSION (ssa
);
1628 bitmap_set_bit (m_killed_defs
, v
);
1629 bitmap_set_bit (m_equiv
.m_names
, v
);
1631 // Now add an equivalency with itself so we don't look to the root oracle.
1632 bitmap b
= BITMAP_ALLOC (&m_bitmaps
);
1633 bitmap_set_bit (b
, v
);
1634 equiv_chain
*ptr
= (equiv_chain
*) obstack_alloc (&m_chain_obstack
,
1635 sizeof (equiv_chain
));
1638 ptr
->m_next
= m_equiv
.m_next
;
1639 m_equiv
.m_next
= ptr
;
1641 // Walk the relation list and remove SSA from any relations.
1642 if (!bitmap_bit_p (m_relations
.m_names
, v
))
1645 bitmap_clear_bit (m_relations
.m_names
, v
);
1646 relation_chain
**prev
= &(m_relations
.m_head
);
1647 relation_chain
*next
= NULL
;
1648 for (relation_chain
*ptr
= m_relations
.m_head
; ptr
; ptr
= next
)
1650 gcc_checking_assert (*prev
== ptr
);
1652 if (SSA_NAME_VERSION (ptr
->op1 ()) == v
1653 || SSA_NAME_VERSION (ptr
->op2 ()) == v
)
1654 *prev
= ptr
->m_next
;
1656 prev
= &(ptr
->m_next
);
1660 // Register relation K between SSA1 and SSA2, resolving unknowns by
1661 // querying from BB.
1664 path_oracle::record (basic_block bb
, relation_kind k
, tree ssa1
, tree ssa2
)
1666 // If the 2 ssa_names are the same, do nothing. An equivalence is implied,
1667 // and no other relation makes sense.
1671 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1673 value_relation
vr (k
, ssa1
, ssa2
);
1674 fprintf (dump_file
, " Registering value_relation (path_oracle) ");
1675 vr
.dump (dump_file
);
1676 fprintf (dump_file
, " (root: bb%d)\n", bb
->index
);
1679 relation_kind curr
= query (bb
, ssa1
, ssa2
);
1680 if (curr
!= VREL_VARYING
)
1681 k
= relation_intersect (curr
, k
);
1685 register_equiv (bb
, ssa1
, ssa2
);
1689 bitmap_set_bit (m_relations
.m_names
, SSA_NAME_VERSION (ssa1
));
1690 bitmap_set_bit (m_relations
.m_names
, SSA_NAME_VERSION (ssa2
));
1691 relation_chain
*ptr
= (relation_chain
*) obstack_alloc (&m_chain_obstack
,
1692 sizeof (relation_chain
));
1693 ptr
->set_relation (k
, ssa1
, ssa2
);
1694 ptr
->m_next
= m_relations
.m_head
;
1695 m_relations
.m_head
= ptr
;
1698 // Query for a relationship between equiv set B1 and B2, resolving unknowns
1699 // starting at block BB.
1702 path_oracle::query (basic_block bb
, const_bitmap b1
, const_bitmap b2
)
1704 if (bitmap_equal_p (b1
, b2
))
1707 relation_kind k
= m_relations
.find_relation (b1
, b2
);
1709 // Do not look at the root oracle for names that have been killed
1711 if (bitmap_intersect_p (m_killed_defs
, b1
)
1712 || bitmap_intersect_p (m_killed_defs
, b2
))
1715 if (k
== VREL_VARYING
&& m_root
)
1716 k
= m_root
->query (bb
, b1
, b2
);
1721 // Query for a relationship between SSA1 and SSA2, resolving unknowns
1722 // starting at block BB.
1725 path_oracle::query (basic_block bb
, tree ssa1
, tree ssa2
)
1727 unsigned v1
= SSA_NAME_VERSION (ssa1
);
1728 unsigned v2
= SSA_NAME_VERSION (ssa2
);
1733 const_bitmap equiv_1
= equiv_set (ssa1
, bb
);
1734 const_bitmap equiv_2
= equiv_set (ssa2
, bb
);
1735 if (bitmap_bit_p (equiv_1
, v2
) && bitmap_bit_p (equiv_2
, v1
))
1738 return query (bb
, equiv_1
, equiv_2
);
1741 // Reset any relations registered on this path. ORACLE is the root
1745 path_oracle::reset_path (relation_oracle
*oracle
)
1747 set_root_oracle (oracle
);
1748 m_equiv
.m_next
= NULL
;
1749 bitmap_clear (m_equiv
.m_names
);
1750 m_relations
.m_head
= NULL
;
1751 bitmap_clear (m_relations
.m_names
);
1752 bitmap_clear (m_killed_defs
);
1755 // Dump relation in basic block... Do nothing here.
1758 path_oracle::dump (FILE *, basic_block
) const
1762 // Dump the relations and equivalencies found in the path.
1765 path_oracle::dump (FILE *f
) const
1767 equiv_chain
*ptr
= m_equiv
.m_next
;
1768 relation_chain
*ptr2
= m_relations
.m_head
;
1771 fprintf (f
, "\npath_oracle:\n");
1773 for (; ptr
; ptr
= ptr
->m_next
)
1776 for (; ptr2
; ptr2
= ptr2
->m_next
)
1778 fprintf (f
, "Relational : ");
1784 // ------------------------------------------------------------------------
1785 // EQUIV iterator. Although we have bitmap iterators, don't expose that it
1786 // is currently a bitmap. Use an export iterator to hide future changes.
1788 // Construct a basic iterator over an equivalence bitmap.
1790 equiv_relation_iterator::equiv_relation_iterator (relation_oracle
*oracle
,
1791 basic_block bb
, tree name
,
1792 bool full
, bool partial
)
1796 m_pe
= partial
? oracle
->partial_equiv_set (name
) : NULL
;
1799 m_bm
= oracle
->equiv_set (name
, bb
);
1801 m_bm
= m_pe
->members
;
1803 bmp_iter_set_init (&m_bi
, m_bm
, 1, &m_y
);
1806 // Move to the next export bitmap spot.
1809 equiv_relation_iterator::next ()
1811 bmp_iter_next (&m_bi
, &m_y
);
1814 // Fetch the name of the next export in the export list. Return NULL if
1815 // iteration is done.
1818 equiv_relation_iterator::get_name (relation_kind
*rel
)
1823 while (bmp_iter_set (&m_bi
, &m_y
))
1825 // Do not return self.
1826 tree t
= ssa_name (m_y
);
1827 if (t
&& t
!= m_name
)
1829 relation_kind k
= VREL_EQ
;
1830 if (m_pe
&& m_bm
== m_pe
->members
)
1832 const pe_slice
*equiv_pe
= m_oracle
->partial_equiv_set (t
);
1833 if (equiv_pe
&& equiv_pe
->members
== m_pe
->members
)
1834 k
= pe_min (m_pe
->code
, equiv_pe
->code
);
1838 if (relation_equiv_p (k
))
1848 // Process partial equivs after full equivs if both were requested.
1849 if (m_pe
&& m_bm
!= m_pe
->members
)
1851 m_bm
= m_pe
->members
;
1854 // Recursively call back to process First PE.
1855 bmp_iter_set_init (&m_bi
, m_bm
, 1, &m_y
);
1856 return get_name (rel
);
1863 #include "selftest.h"
1870 // rr_*_table tables use unsigned char rather than relation_kind.
1871 ASSERT_LT (VREL_LAST
, UCHAR_MAX
);
1872 // Verify commutativity of relation_intersect and relation_union.
1873 for (relation_kind r1
= VREL_VARYING
; r1
< VREL_PE8
;
1874 r1
= relation_kind (r1
+ 1))
1875 for (relation_kind r2
= VREL_VARYING
; r2
< VREL_PE8
;
1876 r2
= relation_kind (r2
+ 1))
1878 ASSERT_EQ (relation_intersect (r1
, r2
), relation_intersect (r2
, r1
));
1879 ASSERT_EQ (relation_union (r1
, r2
), relation_union (r2
, r1
));
1883 } // namespace selftest
1885 #endif // CHECKING_P