1 /* Header file for the value range relational processing.
2 Copyright (C) 2020-2024 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 // This vector maps a relation to the equivalent tree code.
207 static const tree_code relation_to_code
[VREL_LAST
] = {
208 ERROR_MARK
, ERROR_MARK
, LT_EXPR
, LE_EXPR
, GT_EXPR
, GE_EXPR
, EQ_EXPR
,
211 // Given an equivalence set EQUIV, set all the bits in B that are still valid
212 // members of EQUIV in basic block BB.
215 relation_oracle::valid_equivs (bitmap b
, const_bitmap equivs
, basic_block bb
)
219 EXECUTE_IF_SET_IN_BITMAP (equivs
, 0, i
, bi
)
221 tree ssa
= ssa_name (i
);
222 if (ssa
&& !SSA_NAME_IN_FREE_LIST (ssa
))
224 const_bitmap ssa_equiv
= equiv_set (ssa
, bb
);
225 if (ssa_equiv
== equivs
)
226 bitmap_set_bit (b
, i
);
231 // Return any known relation between SSA1 and SSA2 before stmt S is executed.
232 // If GET_RANGE is true, query the range of both operands first to ensure
233 // the definitions have been processed and any relations have be created.
236 relation_oracle::query (gimple
*s
, tree ssa1
, tree ssa2
)
238 if (TREE_CODE (ssa1
) != SSA_NAME
|| TREE_CODE (ssa2
) != SSA_NAME
)
240 return query (gimple_bb (s
), ssa1
, ssa2
);
243 // Return any known relation between SSA1 and SSA2 on edge E.
244 // If GET_RANGE is true, query the range of both operands first to ensure
245 // the definitions have been processed and any relations have be created.
248 relation_oracle::query (edge e
, tree ssa1
, tree ssa2
)
251 if (TREE_CODE (ssa1
) != SSA_NAME
|| TREE_CODE (ssa2
) != SSA_NAME
)
254 // Use destination block if it has a single predecessor, and this picks
255 // up any relation on the edge.
256 // Otherwise choose the src edge and the result is the same as on-exit.
257 if (!single_pred_p (e
->dest
))
262 return query (bb
, ssa1
, ssa2
);
264 // -------------------------------------------------------------------------
266 // The very first element in the m_equiv chain is actually just a summary
267 // element in which the m_names bitmap is used to indicate that an ssa_name
268 // has an equivalence set in this block.
269 // This allows for much faster traversal of the DOM chain, as a search for
270 // SSA_NAME simply requires walking the DOM chain until a block is found
271 // which has the bit for SSA_NAME set. Then scan for the equivalency set in
272 // that block. No previous lists need be searched.
274 // If SSA has an equivalence in this list, find and return it.
275 // Otherwise return NULL.
278 equiv_chain::find (unsigned ssa
)
280 equiv_chain
*ptr
= NULL
;
281 // If there are equiv sets and SSA is in one in this list, find it.
282 // Otherwise return NULL.
283 if (bitmap_bit_p (m_names
, ssa
))
285 for (ptr
= m_next
; ptr
; ptr
= ptr
->m_next
)
286 if (bitmap_bit_p (ptr
->m_names
, ssa
))
292 // Dump the names in this equivalence set.
295 equiv_chain::dump (FILE *f
) const
300 if (!m_names
|| bitmap_empty_p (m_names
))
302 fprintf (f
, "Equivalence set : [");
304 EXECUTE_IF_SET_IN_BITMAP (m_names
, 0, i
, bi
)
310 print_generic_expr (f
, ssa_name (i
), TDF_SLIM
);
316 // Instantiate an equivalency oracle.
318 equiv_oracle::equiv_oracle ()
320 bitmap_obstack_initialize (&m_bitmaps
);
322 m_equiv
.safe_grow_cleared (last_basic_block_for_fn (cfun
) + 1);
323 m_equiv_set
= BITMAP_ALLOC (&m_bitmaps
);
324 bitmap_tree_view (m_equiv_set
);
325 obstack_init (&m_chain_obstack
);
326 m_self_equiv
.create (0);
327 m_self_equiv
.safe_grow_cleared (num_ssa_names
+ 1);
328 m_partial
.create (0);
329 m_partial
.safe_grow_cleared (num_ssa_names
+ 1);
332 // Destruct an equivalency oracle.
334 equiv_oracle::~equiv_oracle ()
336 m_partial
.release ();
337 m_self_equiv
.release ();
338 obstack_free (&m_chain_obstack
, NULL
);
340 bitmap_obstack_release (&m_bitmaps
);
343 // Add a partial equivalence R between OP1 and OP2.
346 equiv_oracle::add_partial_equiv (relation_kind r
, tree op1
, tree op2
)
348 int v1
= SSA_NAME_VERSION (op1
);
349 int v2
= SSA_NAME_VERSION (op2
);
350 int prec2
= TYPE_PRECISION (TREE_TYPE (op2
));
351 int bits
= pe_to_bits (r
);
352 gcc_checking_assert (bits
&& prec2
>= bits
);
354 if (v1
>= (int)m_partial
.length () || v2
>= (int)m_partial
.length ())
355 m_partial
.safe_grow_cleared (num_ssa_names
+ 1);
356 gcc_checking_assert (v1
< (int)m_partial
.length ()
357 && v2
< (int)m_partial
.length ());
359 pe_slice
&pe1
= m_partial
[v1
];
360 pe_slice
&pe2
= m_partial
[v2
];
364 // If the definition pe1 already has an entry, either the stmt is
365 // being re-evaluated, or the def was used before being registered.
366 // In either case, if PE2 has an entry, we simply do nothing.
369 // If there are no uses of op2, do not register.
370 if (has_zero_uses (op2
))
372 // PE1 is the LHS and already has members, so everything in the set
373 // should be a slice of PE2 rather than PE1.
374 pe2
.code
= pe_min (r
, pe1
.code
);
376 pe2
.members
= pe1
.members
;
379 EXECUTE_IF_SET_IN_BITMAP (pe1
.members
, 0, x
, bi
)
381 m_partial
[x
].ssa_base
= op2
;
382 m_partial
[x
].code
= pe_min (m_partial
[x
].code
, pe2
.code
);
384 bitmap_set_bit (pe1
.members
, v2
);
389 // If there are no uses of op1, do not register.
390 if (has_zero_uses (op1
))
392 pe1
.ssa_base
= pe2
.ssa_base
;
393 // If pe2 is a 16 bit value, but only an 8 bit copy, we can't be any
394 // more than an 8 bit equivalence here, so choose MIN value.
395 pe1
.code
= pe_min (r
, pe2
.code
);
396 pe1
.members
= pe2
.members
;
397 bitmap_set_bit (pe1
.members
, v1
);
401 // If there are no uses of either operand, do not register.
402 if (has_zero_uses (op1
) || has_zero_uses (op2
))
404 // Neither name has an entry, simply create op1 as slice of op2.
405 pe2
.code
= bits_to_pe (TYPE_PRECISION (TREE_TYPE (op2
)));
406 if (pe2
.code
== VREL_VARYING
)
409 pe2
.members
= BITMAP_ALLOC (&m_bitmaps
);
410 bitmap_set_bit (pe2
.members
, v2
);
413 pe1
.members
= pe2
.members
;
414 bitmap_set_bit (pe1
.members
, v1
);
418 // Return the set of partial equivalences associated with NAME. The bitmap
419 // will be NULL if there are none.
422 equiv_oracle::partial_equiv_set (tree name
)
424 int v
= SSA_NAME_VERSION (name
);
425 if (v
>= (int)m_partial
.length ())
427 return &m_partial
[v
];
430 // Query if there is a partial equivalence between SSA1 and SSA2. Return
431 // VREL_VARYING if there is not one. If BASE is non-null, return the base
432 // ssa-name this is a slice of.
435 equiv_oracle::partial_equiv (tree ssa1
, tree ssa2
, tree
*base
) const
437 int v1
= SSA_NAME_VERSION (ssa1
);
438 int v2
= SSA_NAME_VERSION (ssa2
);
440 if (v1
>= (int)m_partial
.length () || v2
>= (int)m_partial
.length ())
443 const pe_slice
&pe1
= m_partial
[v1
];
444 const pe_slice
&pe2
= m_partial
[v2
];
445 if (pe1
.members
&& pe2
.members
== pe1
.members
)
448 *base
= pe1
.ssa_base
;
449 return pe_min (pe1
.code
, pe2
.code
);
455 // Find and return the equivalency set for SSA along the dominators of BB.
456 // This is the external API.
459 equiv_oracle::equiv_set (tree ssa
, basic_block bb
)
461 // Search the dominator tree for an equivalency.
462 equiv_chain
*equiv
= find_equiv_dom (ssa
, bb
);
464 return equiv
->m_names
;
466 // Otherwise return a cached equiv set containing just this SSA.
467 unsigned v
= SSA_NAME_VERSION (ssa
);
468 if (v
>= m_self_equiv
.length ())
469 m_self_equiv
.safe_grow_cleared (num_ssa_names
+ 1);
471 if (!m_self_equiv
[v
])
473 m_self_equiv
[v
] = BITMAP_ALLOC (&m_bitmaps
);
474 bitmap_set_bit (m_self_equiv
[v
], v
);
476 return m_self_equiv
[v
];
479 // Query if there is a relation (equivalence) between 2 SSA_NAMEs.
482 equiv_oracle::query (basic_block bb
, tree ssa1
, tree ssa2
)
484 // If the 2 ssa names share the same equiv set, they are equal.
485 if (equiv_set (ssa1
, bb
) == equiv_set (ssa2
, bb
))
488 // Check if there is a partial equivalence.
489 return partial_equiv (ssa1
, ssa2
);
492 // Query if there is a relation (equivalence) between 2 SSA_NAMEs.
495 equiv_oracle::query (basic_block bb ATTRIBUTE_UNUSED
, const_bitmap e1
,
498 // If the 2 ssa names share the same equiv set, they are equal.
499 if (bitmap_equal_p (e1
, e2
))
504 // If SSA has an equivalence in block BB, find and return it.
505 // Otherwise return NULL.
508 equiv_oracle::find_equiv_block (unsigned ssa
, int bb
) const
510 if (bb
>= (int)m_equiv
.length () || !m_equiv
[bb
])
513 return m_equiv
[bb
]->find (ssa
);
516 // Starting at block BB, walk the dominator chain looking for the nearest
517 // equivalence set containing NAME.
520 equiv_oracle::find_equiv_dom (tree name
, basic_block bb
) const
522 unsigned v
= SSA_NAME_VERSION (name
);
523 // Short circuit looking for names which have no equivalences.
524 // Saves time looking for something which does not exist.
525 if (!bitmap_bit_p (m_equiv_set
, v
))
528 // NAME has at least once equivalence set, check to see if it has one along
529 // the dominator tree.
530 for ( ; bb
; bb
= get_immediate_dominator (CDI_DOMINATORS
, bb
))
532 equiv_chain
*ptr
= find_equiv_block (v
, bb
->index
);
539 // Register equivalence between ssa_name V and set EQUIV in block BB,
542 equiv_oracle::register_equiv (basic_block bb
, unsigned v
, equiv_chain
*equiv
)
544 // V will have an equivalency now.
545 bitmap_set_bit (m_equiv_set
, v
);
547 // If that equiv chain is in this block, simply use it.
548 if (equiv
->m_bb
== bb
)
550 bitmap_set_bit (equiv
->m_names
, v
);
551 bitmap_set_bit (m_equiv
[bb
->index
]->m_names
, v
);
555 // Otherwise create an equivalence for this block which is a copy
556 // of equiv, the add V to the set.
557 bitmap b
= BITMAP_ALLOC (&m_bitmaps
);
558 valid_equivs (b
, equiv
->m_names
, bb
);
559 bitmap_set_bit (b
, v
);
563 // Register equivalence between set equiv_1 and equiv_2 in block BB.
564 // Return NULL if either name can be merged with the other. Otherwise
565 // return a pointer to the combined bitmap of names. This allows the
566 // caller to do any setup required for a new element.
569 equiv_oracle::register_equiv (basic_block bb
, equiv_chain
*equiv_1
,
570 equiv_chain
*equiv_2
)
572 // If equiv_1 is already in BB, use it as the combined set.
573 if (equiv_1
->m_bb
== bb
)
575 valid_equivs (equiv_1
->m_names
, equiv_2
->m_names
, bb
);
576 // Its hard to delete from a single linked list, so
577 // just clear the second one.
578 if (equiv_2
->m_bb
== bb
)
579 bitmap_clear (equiv_2
->m_names
);
581 // Ensure the new names are in the summary for BB.
582 bitmap_ior_into (m_equiv
[bb
->index
]->m_names
, equiv_1
->m_names
);
585 // If equiv_2 is in BB, use it for the combined set.
586 if (equiv_2
->m_bb
== bb
)
588 valid_equivs (equiv_2
->m_names
, equiv_1
->m_names
, bb
);
589 // Ensure the new names are in the summary.
590 bitmap_ior_into (m_equiv
[bb
->index
]->m_names
, equiv_2
->m_names
);
594 // At this point, neither equivalence is from this block.
595 bitmap b
= BITMAP_ALLOC (&m_bitmaps
);
596 valid_equivs (b
, equiv_1
->m_names
, bb
);
597 valid_equivs (b
, equiv_2
->m_names
, bb
);
601 // Create an equivalency set containing only SSA in its definition block.
602 // This is done the first time SSA is registered in an equivalency and blocks
603 // any DOM searches past the definition.
606 equiv_oracle::register_initial_def (tree ssa
)
608 if (SSA_NAME_IS_DEFAULT_DEF (ssa
))
610 basic_block bb
= gimple_bb (SSA_NAME_DEF_STMT (ssa
));
612 // If defining stmt is not in the IL, simply return.
615 gcc_checking_assert (!find_equiv_dom (ssa
, bb
));
617 unsigned v
= SSA_NAME_VERSION (ssa
);
618 bitmap_set_bit (m_equiv_set
, v
);
619 bitmap equiv_set
= BITMAP_ALLOC (&m_bitmaps
);
620 bitmap_set_bit (equiv_set
, v
);
621 add_equiv_to_block (bb
, equiv_set
);
624 // Register an equivalence between SSA1 and SSA2 in block BB.
625 // The equivalence oracle maintains a vector of equivalencies indexed by basic
626 // block. When an equivalence between SSA1 and SSA2 is registered in block BB,
627 // a query is made as to what equivalences both names have already, and
628 // any preexisting equivalences are merged to create a single equivalence
629 // containing all the ssa_names in this basic block.
632 equiv_oracle::record (basic_block bb
, relation_kind k
, tree ssa1
, tree ssa2
)
634 // Process partial equivalencies.
635 if (relation_partial_equiv_p (k
))
637 add_partial_equiv (k
, ssa1
, ssa2
);
640 // Only handle equality relations.
644 unsigned v1
= SSA_NAME_VERSION (ssa1
);
645 unsigned v2
= SSA_NAME_VERSION (ssa2
);
647 // If this is the first time an ssa_name has an equivalency registered
648 // create a self-equivalency record in the def block.
649 if (!bitmap_bit_p (m_equiv_set
, v1
))
650 register_initial_def (ssa1
);
651 if (!bitmap_bit_p (m_equiv_set
, v2
))
652 register_initial_def (ssa2
);
654 equiv_chain
*equiv_1
= find_equiv_dom (ssa1
, bb
);
655 equiv_chain
*equiv_2
= find_equiv_dom (ssa2
, bb
);
657 // Check if they are the same set
658 if (equiv_1
&& equiv_1
== equiv_2
)
663 // Case where we have 2 SSA_NAMEs that are not in any set.
664 if (!equiv_1
&& !equiv_2
)
666 bitmap_set_bit (m_equiv_set
, v1
);
667 bitmap_set_bit (m_equiv_set
, v2
);
669 equiv_set
= BITMAP_ALLOC (&m_bitmaps
);
670 bitmap_set_bit (equiv_set
, v1
);
671 bitmap_set_bit (equiv_set
, v2
);
673 else if (!equiv_1
&& equiv_2
)
674 equiv_set
= register_equiv (bb
, v1
, equiv_2
);
675 else if (equiv_1
&& !equiv_2
)
676 equiv_set
= register_equiv (bb
, v2
, equiv_1
);
678 equiv_set
= register_equiv (bb
, equiv_1
, equiv_2
);
680 // A non-null return is a bitmap that is to be added to the current
681 // block as a new equivalence.
685 add_equiv_to_block (bb
, equiv_set
);
688 // Add an equivalency record in block BB containing bitmap EQUIV_SET.
689 // Note the internal caller is responsible for allocating EQUIV_SET properly.
692 equiv_oracle::add_equiv_to_block (basic_block bb
, bitmap equiv_set
)
696 // Check if this is the first time a block has an equivalence added.
697 // and create a header block. And set the summary for this block.
699 if (!m_equiv
[bb
->index
])
701 ptr
= (equiv_chain
*) obstack_alloc (&m_chain_obstack
,
702 sizeof (equiv_chain
));
703 ptr
->m_names
= BITMAP_ALLOC (&m_bitmaps
);
704 bitmap_copy (ptr
->m_names
, equiv_set
);
707 m_equiv
[bb
->index
] = ptr
;
710 // Now create the element for this equiv set and initialize it.
711 ptr
= (equiv_chain
*) obstack_alloc (&m_chain_obstack
, sizeof (equiv_chain
));
712 ptr
->m_names
= equiv_set
;
714 gcc_checking_assert (bb
->index
< (int)m_equiv
.length ());
715 ptr
->m_next
= m_equiv
[bb
->index
]->m_next
;
716 m_equiv
[bb
->index
]->m_next
= ptr
;
717 bitmap_ior_into (m_equiv
[bb
->index
]->m_names
, equiv_set
);
720 // Make sure the BB vector is big enough and grow it if needed.
723 equiv_oracle::limit_check (basic_block bb
)
725 int i
= (bb
) ? bb
->index
: last_basic_block_for_fn (cfun
);
726 if (i
>= (int)m_equiv
.length ())
727 m_equiv
.safe_grow_cleared (last_basic_block_for_fn (cfun
) + 1);
730 // Dump the equivalence sets in BB to file F.
733 equiv_oracle::dump (FILE *f
, basic_block bb
) const
735 if (bb
->index
>= (int)m_equiv
.length ())
737 // Process equivalences.
738 if (m_equiv
[bb
->index
])
740 equiv_chain
*ptr
= m_equiv
[bb
->index
]->m_next
;
741 for (; ptr
; ptr
= ptr
->m_next
)
744 // Look for partial equivalences defined in this block..
745 for (unsigned i
= 0; i
< num_ssa_names
; i
++)
747 tree name
= ssa_name (i
);
748 if (!gimple_range_ssa_p (name
) || !SSA_NAME_DEF_STMT (name
))
750 if (i
>= m_partial
.length ())
752 tree base
= m_partial
[i
].ssa_base
;
753 if (base
&& name
!= base
&& gimple_bb (SSA_NAME_DEF_STMT (name
)) == bb
)
755 relation_kind k
= partial_equiv (name
, base
);
756 if (k
!= VREL_VARYING
)
758 value_relation
vr (k
, name
, base
);
759 fprintf (f
, "Partial equiv ");
767 // Dump all equivalence sets known to the oracle.
770 equiv_oracle::dump (FILE *f
) const
772 fprintf (f
, "Equivalency dump\n");
773 for (unsigned i
= 0; i
< m_equiv
.length (); i
++)
774 if (m_equiv
[i
] && BASIC_BLOCK_FOR_FN (cfun
, i
))
776 fprintf (f
, "BB%d\n", i
);
777 dump (f
, BASIC_BLOCK_FOR_FN (cfun
, i
));
782 // --------------------------------------------------------------------------
783 // Negate the current relation.
786 value_relation::negate ()
788 related
= relation_negate (related
);
791 // Perform an intersection between 2 relations. *this &&= p.
794 value_relation::intersect (value_relation
&p
)
796 // Save previous value
797 relation_kind old
= related
;
799 if (p
.op1 () == op1 () && p
.op2 () == op2 ())
800 related
= relation_intersect (kind (), p
.kind ());
801 else if (p
.op2 () == op1 () && p
.op1 () == op2 ())
802 related
= relation_intersect (kind (), relation_swap (p
.kind ()));
806 return old
!= related
;
809 // Perform a union between 2 relations. *this ||= p.
812 value_relation::union_ (value_relation
&p
)
814 // Save previous value
815 relation_kind old
= related
;
817 if (p
.op1 () == op1 () && p
.op2 () == op2 ())
818 related
= relation_union (kind(), p
.kind());
819 else if (p
.op2 () == op1 () && p
.op1 () == op2 ())
820 related
= relation_union (kind(), relation_swap (p
.kind ()));
824 return old
!= related
;
827 // Identify and apply any transitive relations between REL
828 // and THIS. Return true if there was a transformation.
831 value_relation::apply_transitive (const value_relation
&rel
)
833 relation_kind k
= VREL_VARYING
;
835 // Identify any common operand, and normalize the relations to
836 // the form : A < B B < C produces A < C
837 if (rel
.op1 () == name2
)
840 if (rel
.op2 () == name1
)
842 k
= relation_transitive (kind (), rel
.kind ());
843 if (k
!= VREL_VARYING
)
850 else if (rel
.op1 () == name1
)
853 if (rel
.op2 () == name2
)
855 k
= relation_transitive (relation_swap (kind ()), rel
.kind ());
856 if (k
!= VREL_VARYING
)
864 else if (rel
.op2 () == name2
)
867 if (rel
.op1 () == name1
)
869 k
= relation_transitive (kind (), relation_swap (rel
.kind ()));
870 if (k
!= VREL_VARYING
)
877 else if (rel
.op2 () == name1
)
880 if (rel
.op1 () == name2
)
882 k
= relation_transitive (relation_swap (kind ()),
883 relation_swap (rel
.kind ()));
884 if (k
!= VREL_VARYING
)
895 // Create a trio from this value relation given LHS, OP1 and OP2.
898 value_relation::create_trio (tree lhs
, tree op1
, tree op2
)
901 if (lhs
== name1
&& op1
== name2
)
903 else if (lhs
== name2
&& op1
== name1
)
904 lhs_1
= relation_swap (related
);
906 lhs_1
= VREL_VARYING
;
909 if (lhs
== name1
&& op2
== name2
)
911 else if (lhs
== name2
&& op2
== name1
)
912 lhs_2
= relation_swap (related
);
914 lhs_2
= VREL_VARYING
;
917 if (op1
== name1
&& op2
== name2
)
919 else if (op1
== name2
&& op2
== name1
)
920 op_op
= relation_swap (related
);
924 op_op
= VREL_VARYING
;
926 return relation_trio (lhs_1
, lhs_2
, op_op
);
929 // Dump the relation to file F.
932 value_relation::dump (FILE *f
) const
934 if (!name1
|| !name2
)
936 fprintf (f
, "no relation registered");
940 print_generic_expr (f
, op1 (), TDF_SLIM
);
941 print_relation (f
, kind ());
942 print_generic_expr (f
, op2 (), TDF_SLIM
);
946 // This container is used to link relations in a chain.
948 class relation_chain
: public value_relation
951 relation_chain
*m_next
;
954 // ------------------------------------------------------------------------
956 // Find the relation between any ssa_name in B1 and any name in B2 in LIST.
957 // This will allow equivalencies to be applied to any SSA_NAME in a relation.
960 relation_chain_head::find_relation (const_bitmap b1
, const_bitmap b2
) const
965 // If both b1 and b2 aren't referenced in this block, cant be a relation
966 if (!bitmap_intersect_p (m_names
, b1
) || !bitmap_intersect_p (m_names
, b2
))
969 // Search for the first relation that contains BOTH an element from B1
970 // and B2, and return that relation.
971 for (relation_chain
*ptr
= m_head
; ptr
; ptr
= ptr
->m_next
)
973 unsigned op1
= SSA_NAME_VERSION (ptr
->op1 ());
974 unsigned op2
= SSA_NAME_VERSION (ptr
->op2 ());
975 if (bitmap_bit_p (b1
, op1
) && bitmap_bit_p (b2
, op2
))
977 if (bitmap_bit_p (b1
, op2
) && bitmap_bit_p (b2
, op1
))
978 return relation_swap (ptr
->kind ());
984 // Instantiate a relation oracle.
986 dom_oracle::dom_oracle (bool do_trans_p
)
988 m_do_trans_p
= do_trans_p
;
989 m_relations
.create (0);
990 m_relations
.safe_grow_cleared (last_basic_block_for_fn (cfun
) + 1);
991 m_relation_set
= BITMAP_ALLOC (&m_bitmaps
);
992 m_tmp
= BITMAP_ALLOC (&m_bitmaps
);
993 m_tmp2
= BITMAP_ALLOC (&m_bitmaps
);
996 // Destruct a relation oracle.
998 dom_oracle::~dom_oracle ()
1000 m_relations
.release ();
1003 // Register relation K between ssa_name OP1 and OP2 on STMT.
1006 relation_oracle::record (gimple
*stmt
, relation_kind k
, tree op1
, tree op2
)
1008 gcc_checking_assert (TREE_CODE (op1
) == SSA_NAME
);
1009 gcc_checking_assert (TREE_CODE (op2
) == SSA_NAME
);
1010 gcc_checking_assert (stmt
&& gimple_bb (stmt
));
1012 // Don't register lack of a relation.
1013 if (k
== VREL_VARYING
)
1016 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1018 value_relation
vr (k
, op1
, op2
);
1019 fprintf (dump_file
, " Registering value_relation ");
1020 vr
.dump (dump_file
);
1021 fprintf (dump_file
, " (bb%d) at ", gimple_bb (stmt
)->index
);
1022 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1025 // If an equivalence is being added between a PHI and one of its arguments
1026 // make sure that that argument is not defined in the same block.
1027 // This can happen along back edges and the equivalence will not be
1028 // applicable as it would require a use before def.
1029 if (k
== VREL_EQ
&& is_a
<gphi
*> (stmt
))
1031 tree phi_def
= gimple_phi_result (stmt
);
1032 gcc_checking_assert (phi_def
== op1
|| phi_def
== op2
);
1036 if (gimple_bb (stmt
) == gimple_bb (SSA_NAME_DEF_STMT (arg
)))
1038 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1040 fprintf (dump_file
, " Not registered due to ");
1041 print_generic_expr (dump_file
, arg
, TDF_SLIM
);
1042 fprintf (dump_file
, " being defined in the same block.\n");
1047 record (gimple_bb (stmt
), k
, op1
, op2
);
1050 // Register relation K between ssa_name OP1 and OP2 on edge E.
1053 relation_oracle::record (edge e
, relation_kind k
, tree op1
, tree op2
)
1055 gcc_checking_assert (TREE_CODE (op1
) == SSA_NAME
);
1056 gcc_checking_assert (TREE_CODE (op2
) == SSA_NAME
);
1058 // Do not register lack of relation, or blocks which have more than
1059 // edge E for a predecessor.
1060 if (k
== VREL_VARYING
|| !single_pred_p (e
->dest
))
1063 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1065 value_relation
vr (k
, op1
, op2
);
1066 fprintf (dump_file
, " Registering value_relation ");
1067 vr
.dump (dump_file
);
1068 fprintf (dump_file
, " on (%d->%d)\n", e
->src
->index
, e
->dest
->index
);
1071 record (e
->dest
, k
, op1
, op2
);
1074 // Register relation K between OP! and OP2 in block BB.
1075 // This creates the record and searches for existing records in the dominator
1076 // tree to merge with.
1079 dom_oracle::record (basic_block bb
, relation_kind k
, tree op1
, tree op2
)
1081 // If the 2 ssa_names are the same, do nothing. An equivalence is implied,
1082 // and no other relation makes sense.
1086 // Equivalencies are handled by the equivalence oracle.
1087 if (relation_equiv_p (k
))
1088 equiv_oracle::record (bb
, k
, op1
, op2
);
1091 // if neither op1 nor op2 are in a relation before this is registered,
1092 // there will be no transitive.
1093 bool check
= bitmap_bit_p (m_relation_set
, SSA_NAME_VERSION (op1
))
1094 || bitmap_bit_p (m_relation_set
, SSA_NAME_VERSION (op2
));
1095 relation_chain
*ptr
= set_one_relation (bb
, k
, op1
, op2
);
1097 && (m_relations
[bb
->index
].m_num_relations
1098 < param_relation_block_limit
))
1099 register_transitives (bb
, *ptr
);
1103 // Register relation K between OP! and OP2 in block BB.
1104 // This creates the record and searches for existing records in the dominator
1105 // tree to merge with. Return the record, or NULL if no record was created.
1108 dom_oracle::set_one_relation (basic_block bb
, relation_kind k
, tree op1
,
1111 gcc_checking_assert (k
!= VREL_VARYING
&& k
!= VREL_EQ
);
1113 value_relation
vr(k
, op1
, op2
);
1114 int bbi
= bb
->index
;
1116 if (bbi
>= (int)m_relations
.length())
1117 m_relations
.safe_grow_cleared (last_basic_block_for_fn (cfun
) + 1);
1119 // Summary bitmap indicating what ssa_names have relations in this BB.
1120 bitmap bm
= m_relations
[bbi
].m_names
;
1122 bm
= m_relations
[bbi
].m_names
= BITMAP_ALLOC (&m_bitmaps
);
1123 unsigned v1
= SSA_NAME_VERSION (op1
);
1124 unsigned v2
= SSA_NAME_VERSION (op2
);
1127 relation_chain
*ptr
;
1128 curr
= find_relation_block (bbi
, v1
, v2
, &ptr
);
1129 // There is an existing relation in this block, just intersect with it.
1130 if (curr
!= VREL_VARYING
)
1132 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1134 fprintf (dump_file
, " Intersecting with existing ");
1135 ptr
->dump (dump_file
);
1137 // Check into whether we can simply replace the relation rather than
1138 // intersecting it. This may help with some optimistic iterative
1139 // updating algorithms.
1140 bool new_rel
= ptr
->intersect (vr
);
1141 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1143 fprintf (dump_file
, " to produce ");
1144 ptr
->dump (dump_file
);
1145 fprintf (dump_file
, " %s.\n", new_rel
? "Updated" : "No Change");
1147 // If there was no change, return no record..
1153 if (m_relations
[bbi
].m_num_relations
>= param_relation_block_limit
)
1155 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1156 fprintf (dump_file
, " Not registered due to bb being full\n");
1159 m_relations
[bbi
].m_num_relations
++;
1160 // Check for an existing relation further up the DOM chain.
1161 // By including dominating relations, The first one found in any search
1162 // will be the aggregate of all the previous ones.
1163 curr
= find_relation_dom (bb
, v1
, v2
);
1164 if (curr
!= VREL_VARYING
)
1165 k
= relation_intersect (curr
, k
);
1167 bitmap_set_bit (bm
, v1
);
1168 bitmap_set_bit (bm
, v2
);
1169 bitmap_set_bit (m_relation_set
, v1
);
1170 bitmap_set_bit (m_relation_set
, v2
);
1172 ptr
= (relation_chain
*) obstack_alloc (&m_chain_obstack
,
1173 sizeof (relation_chain
));
1174 ptr
->set_relation (k
, op1
, op2
);
1175 ptr
->m_next
= m_relations
[bbi
].m_head
;
1176 m_relations
[bbi
].m_head
= ptr
;
1181 // Starting at ROOT_BB search the DOM tree looking for relations which
1182 // may produce transitive relations to RELATION. EQUIV1 and EQUIV2 are
1183 // bitmaps for op1/op2 and any of their equivalences that should also be
1187 dom_oracle::register_transitives (basic_block root_bb
,
1188 const value_relation
&relation
)
1190 // Only register transitives if they are requested.
1194 // Only apply transitives to certain kinds of operations.
1195 switch (relation
.kind ())
1206 const_bitmap equiv1
= equiv_set (relation
.op1 (), root_bb
);
1207 const_bitmap equiv2
= equiv_set (relation
.op2 (), root_bb
);
1209 const unsigned work_budget
= param_transitive_relations_work_bound
;
1210 unsigned avail_budget
= work_budget
;
1211 for (bb
= root_bb
; bb
;
1212 /* Advancing to the next immediate dominator eats from the budget,
1213 if none is left after that there's no point to continue. */
1214 bb
= (--avail_budget
> 0
1215 ? get_immediate_dominator (CDI_DOMINATORS
, bb
) : nullptr))
1217 int bbi
= bb
->index
;
1218 if (bbi
>= (int)m_relations
.length())
1220 const_bitmap bm
= m_relations
[bbi
].m_names
;
1223 if (!bitmap_intersect_p (bm
, equiv1
) && !bitmap_intersect_p (bm
, equiv2
))
1225 // At least one of the 2 ops has a relation in this block.
1226 relation_chain
*ptr
;
1227 for (ptr
= m_relations
[bbi
].m_head
; ptr
; ptr
= ptr
->m_next
)
1229 // In the presence of an equivalence, 2 operands may do not
1230 // naturally match. ie with equivalence a_2 == b_3
1231 // given c_1 < a_2 && b_3 < d_4
1232 // convert the second relation (b_3 < d_4) to match any
1233 // equivalences to found in the first relation.
1234 // ie convert b_3 < d_4 to a_2 < d_4, which then exposes the
1235 // transitive operation: c_1 < a_2 && a_2 < d_4 -> c_1 < d_4
1238 tree p1
= ptr
->op1 ();
1239 tree p2
= ptr
->op2 ();
1240 // Find which equivalence is in the first operand.
1241 if (bitmap_bit_p (equiv1
, SSA_NAME_VERSION (p1
)))
1243 else if (bitmap_bit_p (equiv1
, SSA_NAME_VERSION (p2
)))
1248 // Find which equivalence is in the second operand.
1249 if (bitmap_bit_p (equiv2
, SSA_NAME_VERSION (p1
)))
1251 else if (bitmap_bit_p (equiv2
, SSA_NAME_VERSION (p2
)))
1256 // Ignore if both NULL (not relevant relation) or the same,
1262 // Any operand not an equivalence, just take the real operand.
1264 r1
= relation
.op1 ();
1266 r2
= relation
.op2 ();
1268 value_relation
nr (relation
.kind (), r1
, r2
);
1269 if (nr
.apply_transitive (*ptr
))
1271 // If the new relation is already present we know any
1272 // further processing is already reflected above it.
1273 // When we ran into the limit of relations on root_bb
1274 // we can give up as well.
1275 if (!set_one_relation (root_bb
, nr
.kind (),
1276 nr
.op1 (), nr
.op2 ()))
1278 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1281 " Registering transitive relation ");
1282 nr
.dump (dump_file
);
1283 fputc ('\n', dump_file
);
1287 /* Processed one relation, abort if we've eaten up our budget. */
1288 if (--avail_budget
== 0)
1294 // Find the relation between any ssa_name in B1 and any name in B2 in block BB.
1295 // This will allow equivalencies to be applied to any SSA_NAME in a relation.
1298 dom_oracle::find_relation_block (unsigned bb
, const_bitmap b1
,
1299 const_bitmap b2
) const
1301 if (bb
>= m_relations
.length())
1302 return VREL_VARYING
;
1304 return m_relations
[bb
].find_relation (b1
, b2
);
1307 // Search the DOM tree for a relation between an element of equivalency set B1
1308 // and B2, starting with block BB.
1311 dom_oracle::query (basic_block bb
, const_bitmap b1
, const_bitmap b2
)
1314 if (bitmap_equal_p (b1
, b2
))
1317 // If either name does not occur in a relation anywhere, there isn't one.
1318 if (!bitmap_intersect_p (m_relation_set
, b1
)
1319 || !bitmap_intersect_p (m_relation_set
, b2
))
1320 return VREL_VARYING
;
1322 // Search each block in the DOM tree checking.
1323 for ( ; bb
; bb
= get_immediate_dominator (CDI_DOMINATORS
, bb
))
1325 r
= find_relation_block (bb
->index
, b1
, b2
);
1326 if (r
!= VREL_VARYING
)
1329 return VREL_VARYING
;
1333 // Find a relation in block BB between ssa version V1 and V2. If a relation
1334 // is found, return a pointer to the chain object in OBJ.
1337 dom_oracle::find_relation_block (int bb
, unsigned v1
, unsigned v2
,
1338 relation_chain
**obj
) const
1340 if (bb
>= (int)m_relations
.length())
1341 return VREL_VARYING
;
1343 const_bitmap bm
= m_relations
[bb
].m_names
;
1345 return VREL_VARYING
;
1347 // If both b1 and b2 aren't referenced in this block, cant be a relation
1348 if (!bitmap_bit_p (bm
, v1
) || !bitmap_bit_p (bm
, v2
))
1349 return VREL_VARYING
;
1351 relation_chain
*ptr
;
1352 for (ptr
= m_relations
[bb
].m_head
; ptr
; ptr
= ptr
->m_next
)
1354 unsigned op1
= SSA_NAME_VERSION (ptr
->op1 ());
1355 unsigned op2
= SSA_NAME_VERSION (ptr
->op2 ());
1356 if (v1
== op1
&& v2
== op2
)
1360 return ptr
->kind ();
1362 if (v1
== op2
&& v2
== op1
)
1366 return relation_swap (ptr
->kind ());
1370 return VREL_VARYING
;
1373 // Find a relation between SSA version V1 and V2 in the dominator tree
1374 // starting with block BB
1377 dom_oracle::find_relation_dom (basic_block bb
, unsigned v1
, unsigned v2
) const
1380 // IF either name does not occur in a relation anywhere, there isn't one.
1381 if (!bitmap_bit_p (m_relation_set
, v1
) || !bitmap_bit_p (m_relation_set
, v2
))
1382 return VREL_VARYING
;
1384 for ( ; bb
; bb
= get_immediate_dominator (CDI_DOMINATORS
, bb
))
1386 r
= find_relation_block (bb
->index
, v1
, v2
);
1387 if (r
!= VREL_VARYING
)
1390 return VREL_VARYING
;
1394 // Query if there is a relation between SSA1 and SS2 in block BB or a
1398 dom_oracle::query (basic_block bb
, tree ssa1
, tree ssa2
)
1401 unsigned v1
= SSA_NAME_VERSION (ssa1
);
1402 unsigned v2
= SSA_NAME_VERSION (ssa2
);
1406 // If v1 or v2 do not have any relations or equivalences, a partial
1407 // equivalence is the only possibility.
1408 if ((!bitmap_bit_p (m_relation_set
, v1
) && !has_equiv_p (v1
))
1409 || (!bitmap_bit_p (m_relation_set
, v2
) && !has_equiv_p (v2
)))
1410 return partial_equiv (ssa1
, ssa2
);
1412 // Check for equivalence first. They must be in each equivalency set.
1413 const_bitmap equiv1
= equiv_set (ssa1
, bb
);
1414 const_bitmap equiv2
= equiv_set (ssa2
, bb
);
1415 if (bitmap_bit_p (equiv1
, v2
) && bitmap_bit_p (equiv2
, v1
))
1418 kind
= partial_equiv (ssa1
, ssa2
);
1419 if (kind
!= VREL_VARYING
)
1422 // Initially look for a direct relationship and just return that.
1423 kind
= find_relation_dom (bb
, v1
, v2
);
1424 if (kind
!= VREL_VARYING
)
1427 // Query using the equivalence sets.
1428 kind
= query (bb
, equiv1
, equiv2
);
1432 // Dump all the relations in block BB to file F.
1435 dom_oracle::dump (FILE *f
, basic_block bb
) const
1437 equiv_oracle::dump (f
,bb
);
1439 if (bb
->index
>= (int)m_relations
.length ())
1441 if (!m_relations
[bb
->index
].m_names
)
1444 relation_chain
*ptr
= m_relations
[bb
->index
].m_head
;
1445 for (; ptr
; ptr
= ptr
->m_next
)
1447 fprintf (f
, "Relational : ");
1453 // Dump all the relations known to file F.
1456 dom_oracle::dump (FILE *f
) const
1458 fprintf (f
, "Relation dump\n");
1459 for (unsigned i
= 0; i
< m_relations
.length (); i
++)
1460 if (BASIC_BLOCK_FOR_FN (cfun
, i
))
1462 fprintf (f
, "BB%d\n", i
);
1463 dump (f
, BASIC_BLOCK_FOR_FN (cfun
, i
));
1468 relation_oracle::debug () const
1473 path_oracle::path_oracle (relation_oracle
*oracle
)
1475 set_root_oracle (oracle
);
1476 bitmap_obstack_initialize (&m_bitmaps
);
1477 obstack_init (&m_chain_obstack
);
1479 // Initialize header records.
1480 m_equiv
.m_names
= BITMAP_ALLOC (&m_bitmaps
);
1481 m_equiv
.m_bb
= NULL
;
1482 m_equiv
.m_next
= NULL
;
1483 m_relations
.m_names
= BITMAP_ALLOC (&m_bitmaps
);
1484 m_relations
.m_head
= NULL
;
1485 m_killed_defs
= BITMAP_ALLOC (&m_bitmaps
);
1488 path_oracle::~path_oracle ()
1490 obstack_free (&m_chain_obstack
, NULL
);
1491 bitmap_obstack_release (&m_bitmaps
);
1494 // Return the equiv set for SSA, and if there isn't one, check for equivs
1495 // starting in block BB.
1498 path_oracle::equiv_set (tree ssa
, basic_block bb
)
1500 // Check the list first.
1501 equiv_chain
*ptr
= m_equiv
.find (SSA_NAME_VERSION (ssa
));
1503 return ptr
->m_names
;
1505 // Otherwise defer to the root oracle.
1507 return m_root
->equiv_set (ssa
, bb
);
1509 // Allocate a throw away bitmap if there isn't a root oracle.
1510 bitmap tmp
= BITMAP_ALLOC (&m_bitmaps
);
1511 bitmap_set_bit (tmp
, SSA_NAME_VERSION (ssa
));
1515 // Register an equivalence between SSA1 and SSA2 resolving unknowns from
1519 path_oracle::register_equiv (basic_block bb
, tree ssa1
, tree ssa2
)
1521 const_bitmap equiv_1
= equiv_set (ssa1
, bb
);
1522 const_bitmap equiv_2
= equiv_set (ssa2
, bb
);
1524 // Check if they are the same set, if so, we're done.
1525 if (bitmap_equal_p (equiv_1
, equiv_2
))
1528 // Don't mess around, simply create a new record and insert it first.
1529 bitmap b
= BITMAP_ALLOC (&m_bitmaps
);
1530 valid_equivs (b
, equiv_1
, bb
);
1531 valid_equivs (b
, equiv_2
, bb
);
1533 equiv_chain
*ptr
= (equiv_chain
*) obstack_alloc (&m_chain_obstack
,
1534 sizeof (equiv_chain
));
1537 ptr
->m_next
= m_equiv
.m_next
;
1538 m_equiv
.m_next
= ptr
;
1539 bitmap_ior_into (m_equiv
.m_names
, b
);
1542 // Register killing definition of an SSA_NAME.
1545 path_oracle::killing_def (tree ssa
)
1547 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1549 fprintf (dump_file
, " Registering killing_def (path_oracle) ");
1550 print_generic_expr (dump_file
, ssa
, TDF_SLIM
);
1551 fprintf (dump_file
, "\n");
1554 unsigned v
= SSA_NAME_VERSION (ssa
);
1556 bitmap_set_bit (m_killed_defs
, v
);
1557 bitmap_set_bit (m_equiv
.m_names
, v
);
1559 // Now add an equivalency with itself so we don't look to the root oracle.
1560 bitmap b
= BITMAP_ALLOC (&m_bitmaps
);
1561 bitmap_set_bit (b
, v
);
1562 equiv_chain
*ptr
= (equiv_chain
*) obstack_alloc (&m_chain_obstack
,
1563 sizeof (equiv_chain
));
1566 ptr
->m_next
= m_equiv
.m_next
;
1567 m_equiv
.m_next
= ptr
;
1569 // Walk the relation list and remove SSA from any relations.
1570 if (!bitmap_bit_p (m_relations
.m_names
, v
))
1573 bitmap_clear_bit (m_relations
.m_names
, v
);
1574 relation_chain
**prev
= &(m_relations
.m_head
);
1575 relation_chain
*next
= NULL
;
1576 for (relation_chain
*ptr
= m_relations
.m_head
; ptr
; ptr
= next
)
1578 gcc_checking_assert (*prev
== ptr
);
1580 if (SSA_NAME_VERSION (ptr
->op1 ()) == v
1581 || SSA_NAME_VERSION (ptr
->op2 ()) == v
)
1582 *prev
= ptr
->m_next
;
1584 prev
= &(ptr
->m_next
);
1588 // Register relation K between SSA1 and SSA2, resolving unknowns by
1589 // querying from BB.
1592 path_oracle::record (basic_block bb
, relation_kind k
, tree ssa1
, tree ssa2
)
1594 // If the 2 ssa_names are the same, do nothing. An equivalence is implied,
1595 // and no other relation makes sense.
1599 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1601 value_relation
vr (k
, ssa1
, ssa2
);
1602 fprintf (dump_file
, " Registering value_relation (path_oracle) ");
1603 vr
.dump (dump_file
);
1604 fprintf (dump_file
, " (root: bb%d)\n", bb
->index
);
1607 relation_kind curr
= query (bb
, ssa1
, ssa2
);
1608 if (curr
!= VREL_VARYING
)
1609 k
= relation_intersect (curr
, k
);
1613 register_equiv (bb
, ssa1
, ssa2
);
1617 bitmap_set_bit (m_relations
.m_names
, SSA_NAME_VERSION (ssa1
));
1618 bitmap_set_bit (m_relations
.m_names
, SSA_NAME_VERSION (ssa2
));
1619 relation_chain
*ptr
= (relation_chain
*) obstack_alloc (&m_chain_obstack
,
1620 sizeof (relation_chain
));
1621 ptr
->set_relation (k
, ssa1
, ssa2
);
1622 ptr
->m_next
= m_relations
.m_head
;
1623 m_relations
.m_head
= ptr
;
1626 // Query for a relationship between equiv set B1 and B2, resolving unknowns
1627 // starting at block BB.
1630 path_oracle::query (basic_block bb
, const_bitmap b1
, const_bitmap b2
)
1632 if (bitmap_equal_p (b1
, b2
))
1635 relation_kind k
= m_relations
.find_relation (b1
, b2
);
1637 // Do not look at the root oracle for names that have been killed
1639 if (bitmap_intersect_p (m_killed_defs
, b1
)
1640 || bitmap_intersect_p (m_killed_defs
, b2
))
1643 if (k
== VREL_VARYING
&& m_root
)
1644 k
= m_root
->query (bb
, b1
, b2
);
1649 // Query for a relationship between SSA1 and SSA2, resolving unknowns
1650 // starting at block BB.
1653 path_oracle::query (basic_block bb
, tree ssa1
, tree ssa2
)
1655 unsigned v1
= SSA_NAME_VERSION (ssa1
);
1656 unsigned v2
= SSA_NAME_VERSION (ssa2
);
1661 const_bitmap equiv_1
= equiv_set (ssa1
, bb
);
1662 const_bitmap equiv_2
= equiv_set (ssa2
, bb
);
1663 if (bitmap_bit_p (equiv_1
, v2
) && bitmap_bit_p (equiv_2
, v1
))
1666 return query (bb
, equiv_1
, equiv_2
);
1669 // Reset any relations registered on this path. ORACLE is the root
1673 path_oracle::reset_path (relation_oracle
*oracle
)
1675 set_root_oracle (oracle
);
1676 m_equiv
.m_next
= NULL
;
1677 bitmap_clear (m_equiv
.m_names
);
1678 m_relations
.m_head
= NULL
;
1679 bitmap_clear (m_relations
.m_names
);
1680 bitmap_clear (m_killed_defs
);
1683 // Dump relation in basic block... Do nothing here.
1686 path_oracle::dump (FILE *, basic_block
) const
1690 // Dump the relations and equivalencies found in the path.
1693 path_oracle::dump (FILE *f
) const
1695 equiv_chain
*ptr
= m_equiv
.m_next
;
1696 relation_chain
*ptr2
= m_relations
.m_head
;
1699 fprintf (f
, "\npath_oracle:\n");
1701 for (; ptr
; ptr
= ptr
->m_next
)
1704 for (; ptr2
; ptr2
= ptr2
->m_next
)
1706 fprintf (f
, "Relational : ");
1712 // ------------------------------------------------------------------------
1713 // EQUIV iterator. Although we have bitmap iterators, don't expose that it
1714 // is currently a bitmap. Use an export iterator to hide future changes.
1716 // Construct a basic iterator over an equivalence bitmap.
1718 equiv_relation_iterator::equiv_relation_iterator (relation_oracle
*oracle
,
1719 basic_block bb
, tree name
,
1720 bool full
, bool partial
)
1724 m_pe
= partial
? oracle
->partial_equiv_set (name
) : NULL
;
1727 m_bm
= oracle
->equiv_set (name
, bb
);
1729 m_bm
= m_pe
->members
;
1731 bmp_iter_set_init (&m_bi
, m_bm
, 1, &m_y
);
1734 // Move to the next export bitmap spot.
1737 equiv_relation_iterator::next ()
1739 bmp_iter_next (&m_bi
, &m_y
);
1742 // Fetch the name of the next export in the export list. Return NULL if
1743 // iteration is done.
1746 equiv_relation_iterator::get_name (relation_kind
*rel
)
1751 while (bmp_iter_set (&m_bi
, &m_y
))
1753 // Do not return self.
1754 tree t
= ssa_name (m_y
);
1755 if (t
&& t
!= m_name
)
1757 relation_kind k
= VREL_EQ
;
1758 if (m_pe
&& m_bm
== m_pe
->members
)
1760 const pe_slice
*equiv_pe
= m_oracle
->partial_equiv_set (t
);
1761 if (equiv_pe
&& equiv_pe
->members
== m_pe
->members
)
1762 k
= pe_min (m_pe
->code
, equiv_pe
->code
);
1766 if (relation_equiv_p (k
))
1776 // Process partial equivs after full equivs if both were requested.
1777 if (m_pe
&& m_bm
!= m_pe
->members
)
1779 m_bm
= m_pe
->members
;
1782 // Recursively call back to process First PE.
1783 bmp_iter_set_init (&m_bi
, m_bm
, 1, &m_y
);
1784 return get_name (rel
);
1791 #include "selftest.h"
1798 // rr_*_table tables use unsigned char rather than relation_kind.
1799 ASSERT_LT (VREL_LAST
, UCHAR_MAX
);
1800 // Verify commutativity of relation_intersect and relation_union.
1801 for (relation_kind r1
= VREL_VARYING
; r1
< VREL_PE8
;
1802 r1
= relation_kind (r1
+ 1))
1803 for (relation_kind r2
= VREL_VARYING
; r2
< VREL_PE8
;
1804 r2
= relation_kind (r2
+ 1))
1806 ASSERT_EQ (relation_intersect (r1
, r2
), relation_intersect (r2
, r1
));
1807 ASSERT_EQ (relation_union (r1
, r2
), relation_union (r2
, r1
));
1811 } // namespace selftest
1813 #endif // CHECKING_P