1 /* Header file for the value range relational processing.
2 Copyright (C) 2020-2021 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 // These VREL codes are arranged such that VREL_NONE is the first
36 // code, and all the rest are contiguous up to and including VREL_LAST.
38 #define VREL_FIRST VREL_NONE
39 #define VREL_LAST NE_EXPR
40 #define VREL_COUNT (VREL_LAST - VREL_FIRST + 1)
42 // vrel_range_assert will either assert that the tree code passed is valid,
43 // or mark invalid codes as unreachable to help with table optimation.
45 #define vrel_range_assert(c) \
46 gcc_checking_assert ((c) >= VREL_FIRST && (c) <= VREL_LAST)
48 #define vrel_range_assert(c) \
49 if ((c) < VREL_FIRST || (c) > VREL_LAST) \
53 static const char *kind_string
[VREL_COUNT
] =
54 { "none", "<", "<=", ">", ">=", "empty", "==", "!=" };
56 // Print a relation_kind REL to file F.
59 print_relation (FILE *f
, relation_kind rel
)
61 vrel_range_assert (rel
);
62 fprintf (f
, " %s ", kind_string
[rel
- VREL_FIRST
]);
65 // This table is used to negate the operands. op1 REL op2 -> !(op1 REL op2).
66 relation_kind rr_negate_table
[VREL_COUNT
] = {
67 // NONE, LT_EXPR, LE_EXPR, GT_EXPR, GE_EXPR, EMPTY, EQ_EXPR, NE_EXPR
68 VREL_NONE
, GE_EXPR
, GT_EXPR
, LE_EXPR
, LT_EXPR
, VREL_EMPTY
, NE_EXPR
, EQ_EXPR
};
70 // Negate the relation, as in logical negation.
73 relation_negate (relation_kind r
)
75 vrel_range_assert (r
);
76 return rr_negate_table
[r
- VREL_FIRST
];
79 // This table is used to swap the operands. op1 REL op2 -> op2 REL op1.
80 relation_kind rr_swap_table
[VREL_COUNT
] = {
81 // NONE, LT_EXPR, LE_EXPR, GT_EXPR, GE_EXPR, EMPTY, EQ_EXPR, NE_EXPR
82 VREL_NONE
, GT_EXPR
, GE_EXPR
, LT_EXPR
, LE_EXPR
, VREL_EMPTY
, EQ_EXPR
, NE_EXPR
};
84 // Return the relation as if the operands were swapped.
87 relation_swap (relation_kind r
)
89 vrel_range_assert (r
);
90 return rr_swap_table
[r
- VREL_FIRST
];
93 // This table is used to perform an intersection between 2 relations.
95 relation_kind rr_intersect_table
[VREL_COUNT
][VREL_COUNT
] = {
96 // NONE, LT_EXPR, LE_EXPR, GT_EXPR, GE_EXPR, EMPTY, EQ_EXPR, NE_EXPR
98 { VREL_NONE
, LT_EXPR
, LE_EXPR
, GT_EXPR
, GE_EXPR
, VREL_EMPTY
, EQ_EXPR
, NE_EXPR
},
100 { LT_EXPR
, LT_EXPR
, LT_EXPR
, VREL_EMPTY
, VREL_EMPTY
, VREL_EMPTY
, VREL_EMPTY
, LT_EXPR
},
102 { LE_EXPR
, LT_EXPR
, LE_EXPR
, VREL_EMPTY
, EQ_EXPR
, VREL_EMPTY
, EQ_EXPR
, LT_EXPR
},
104 { GT_EXPR
, VREL_EMPTY
, VREL_EMPTY
, GT_EXPR
, GT_EXPR
, VREL_EMPTY
, VREL_EMPTY
, GT_EXPR
},
106 { GE_EXPR
, VREL_EMPTY
, EQ_EXPR
, GT_EXPR
, GE_EXPR
, VREL_EMPTY
, EQ_EXPR
, GT_EXPR
},
108 { VREL_EMPTY
, VREL_EMPTY
, VREL_EMPTY
, VREL_EMPTY
, VREL_EMPTY
, VREL_EMPTY
, VREL_EMPTY
, VREL_EMPTY
},
110 { EQ_EXPR
, VREL_EMPTY
, EQ_EXPR
, VREL_EMPTY
, EQ_EXPR
, VREL_EMPTY
, EQ_EXPR
, VREL_EMPTY
},
112 { NE_EXPR
, LT_EXPR
, LT_EXPR
, GT_EXPR
, GT_EXPR
, VREL_EMPTY
, VREL_EMPTY
, NE_EXPR
} };
115 // Intersect relation R1 with relation R2 and return the resulting relation.
118 relation_intersect (relation_kind r1
, relation_kind r2
)
120 vrel_range_assert (r1
);
121 vrel_range_assert (r2
);
122 return rr_intersect_table
[r1
- VREL_FIRST
][r2
- VREL_FIRST
];
126 // This table is used to perform a union between 2 relations.
128 relation_kind rr_union_table
[VREL_COUNT
][VREL_COUNT
] = {
129 // NONE, LT_EXPR, LE_EXPR, GT_EXPR, GE_EXPR, EMPTY, EQ_EXPR, NE_EXPR
131 { VREL_NONE
, VREL_NONE
, VREL_NONE
, VREL_NONE
, VREL_NONE
, VREL_NONE
, VREL_NONE
, VREL_NONE
},
133 { VREL_NONE
, LT_EXPR
, LE_EXPR
, NE_EXPR
, VREL_NONE
, LT_EXPR
, LE_EXPR
, NE_EXPR
},
135 { VREL_NONE
, LE_EXPR
, LE_EXPR
, VREL_NONE
, VREL_NONE
, LE_EXPR
, LE_EXPR
, VREL_NONE
},
137 { VREL_NONE
, NE_EXPR
, VREL_NONE
, GT_EXPR
, GE_EXPR
, GT_EXPR
, GE_EXPR
, NE_EXPR
},
139 { VREL_NONE
, VREL_NONE
, VREL_NONE
, GE_EXPR
, GE_EXPR
, GE_EXPR
, GE_EXPR
, VREL_NONE
},
141 { VREL_NONE
, LT_EXPR
, LE_EXPR
, GT_EXPR
, GE_EXPR
, VREL_EMPTY
, EQ_EXPR
, NE_EXPR
},
143 { VREL_NONE
, LE_EXPR
, LE_EXPR
, GE_EXPR
, GE_EXPR
, EQ_EXPR
, EQ_EXPR
, VREL_NONE
},
145 { VREL_NONE
, NE_EXPR
, VREL_NONE
, NE_EXPR
, VREL_NONE
, NE_EXPR
, VREL_NONE
, NE_EXPR
} };
147 // Union relation R1 with relation R2 and return the result.
150 relation_union (relation_kind r1
, relation_kind r2
)
152 vrel_range_assert (r1
);
153 vrel_range_assert (r2
);
154 return rr_union_table
[r1
- VREL_FIRST
][r2
- VREL_FIRST
];
158 // This table is used to determine transitivity between 2 relations.
159 // (A relation0 B) and (B relation1 C) implies (A result C)
161 relation_kind rr_transitive_table
[VREL_COUNT
][VREL_COUNT
] = {
162 // NONE, LT_EXPR, LE_EXPR, GT_EXPR, GE_EXPR, EMPTY, EQ_EXPR, NE_EXPR
164 { VREL_NONE
, VREL_NONE
, VREL_NONE
, VREL_NONE
, VREL_NONE
, VREL_NONE
, VREL_NONE
, VREL_NONE
},
166 { VREL_NONE
, LT_EXPR
, LT_EXPR
, VREL_NONE
, VREL_NONE
, VREL_NONE
, LT_EXPR
, VREL_NONE
},
168 { VREL_NONE
, LT_EXPR
, LE_EXPR
, VREL_NONE
, VREL_NONE
, VREL_NONE
, LE_EXPR
, VREL_NONE
},
170 { VREL_NONE
, VREL_NONE
, VREL_NONE
, GT_EXPR
, GT_EXPR
, VREL_NONE
, GT_EXPR
, VREL_NONE
},
172 { VREL_NONE
, VREL_NONE
, VREL_NONE
, GT_EXPR
, GE_EXPR
, VREL_NONE
, GE_EXPR
, VREL_NONE
},
174 { VREL_NONE
, VREL_NONE
, VREL_NONE
, VREL_NONE
, VREL_NONE
, VREL_NONE
, VREL_NONE
, VREL_NONE
},
176 { VREL_NONE
, LT_EXPR
, LE_EXPR
, GT_EXPR
, GE_EXPR
, VREL_NONE
, EQ_EXPR
, VREL_NONE
},
178 { VREL_NONE
, VREL_NONE
, VREL_NONE
, VREL_NONE
, VREL_NONE
, VREL_NONE
, VREL_NONE
, VREL_NONE
} };
180 // Apply transitive operation between relation R1 and relation R2, and
181 // return the resulting relation, if any.
184 relation_transitive (relation_kind r1
, relation_kind r2
)
186 vrel_range_assert (r1
);
187 vrel_range_assert (r2
);
188 return rr_transitive_table
[r1
- VREL_FIRST
][r2
- VREL_FIRST
];
191 // -------------------------------------------------------------------------
193 // This class represents an equivalency set, and contains a link to the next
194 // one in the list to be searched.
196 // The very first element in the m_equiv chain is actually just a summary
197 // element in which the m_names bitmap is used to indicate that an ssa_name
198 // has an equivalence set in this block.
199 // This allows for much faster traversal of the DOM chain, as a search for
200 // SSA_NAME simply requires walking the DOM chain until a block is found
201 // which has the bit for SSA_NAME set. Then scan for the equivalency set in
202 // that block. No previous blcoks need be searched.
207 bitmap m_names
; // ssa-names in equiv set.
208 basic_block m_bb
; // Block this belongs to
209 equiv_chain
*m_next
; // Next in block list.
210 void dump (FILE *f
) const; // Show names in this list.
214 // Dump the names in this equivalence set.
217 equiv_chain::dump (FILE *f
) const
224 fprintf (f
, "Equivalence set : [");
226 EXECUTE_IF_SET_IN_BITMAP (m_names
, 0, i
, bi
)
232 print_generic_expr (f
, ssa_name (i
), TDF_SLIM
);
238 // Instantiate an equivalency oracle.
240 equiv_oracle::equiv_oracle ()
242 bitmap_obstack_initialize (&m_bitmaps
);
244 m_equiv
.safe_grow_cleared (last_basic_block_for_fn (cfun
) + 1);
245 m_equiv_set
= BITMAP_ALLOC (&m_bitmaps
);
246 obstack_init (&m_chain_obstack
);
249 // Destruct an equivalency oracle.
251 equiv_oracle::~equiv_oracle ()
253 obstack_free (&m_chain_obstack
, NULL
);
255 bitmap_obstack_release (&m_bitmaps
);
258 // Find and return the equivalency set for SSA along the dominators of BB.
259 // This is the external API.
262 equiv_oracle::equiv_set (tree ssa
, basic_block bb
) const
264 // Search the dominator tree for an equivalency.
265 equiv_chain
*equiv
= find_equiv_dom (ssa
, bb
);
267 return equiv
->m_names
;
273 // If SSA has an equivalence in block BB, find and return it.
274 // Otherwise return NULL.
277 equiv_oracle::find_equiv_block (unsigned ssa
, int bb
) const
279 equiv_chain
*ptr
= NULL
;
280 if (bb
>= (int)m_equiv
.length ())
283 // If there are equiv sets and SSA is in one in this block, find it.
284 // Otherwise return NULL.
285 if (m_equiv
[bb
] && bitmap_bit_p (m_equiv
[bb
]->m_names
, ssa
))
287 for (ptr
= m_equiv
[bb
]->m_next
; ptr
; ptr
= ptr
->m_next
)
288 if (bitmap_bit_p (ptr
->m_names
, ssa
))
294 // Starting at block BB, walk the dominator chain looking for the nearest
295 // equivalence set containing NAME.
298 equiv_oracle::find_equiv_dom (tree name
, basic_block bb
) const
300 unsigned v
= SSA_NAME_VERSION (name
);
301 // Short circuit looking for names which have no equivalences.
302 // Saves time looking for something which does not exist.
303 if (!bitmap_bit_p (m_equiv_set
, v
))
306 // NAME has at least once equivalence set, check to see if it has one along
307 // the dominator tree.
308 for ( ; bb
; bb
= get_immediate_dominator (CDI_DOMINATORS
, bb
))
310 equiv_chain
*ptr
= find_equiv_block (v
, bb
->index
);
317 // Register equivalance between ssa_name V and set EQUIV in block BB,
320 equiv_oracle::register_equiv (basic_block bb
, unsigned v
, equiv_chain
*equiv
)
322 // V will have an equivalency now.
323 bitmap_set_bit (m_equiv_set
, v
);
325 // If that equiv chain is in this block, simply use it.
326 if (equiv
->m_bb
== bb
)
328 bitmap_set_bit (equiv
->m_names
, v
);
329 bitmap_set_bit (m_equiv
[bb
->index
]->m_names
, v
);
333 // Otherwise create an equivalence for this block which is a copy
334 // of equiv, the add V to the set.
335 bitmap b
= BITMAP_ALLOC (&m_bitmaps
);
336 bitmap_copy (b
, equiv
->m_names
);
337 bitmap_set_bit (b
, v
);
341 // Register equivalence between set equiv_1 and equiv_2 in block BB.
342 // Return NULL if either name can be merged with the other. Otherwise
343 // return a pointer to the combined bitmap of names. This allows the
344 // caller to do any setup required for a new element.
347 equiv_oracle::register_equiv (basic_block bb
, equiv_chain
*equiv_1
,
348 equiv_chain
*equiv_2
)
350 // If equiv_1 is alreayd in BB, use it as the combined set.
351 if (equiv_1
->m_bb
== bb
)
353 bitmap_ior_into (equiv_1
->m_names
, equiv_2
->m_names
);
354 // Its hard to delete from a single linked list, so
355 // just clear the second one.
356 if (equiv_2
->m_bb
== bb
)
357 bitmap_clear (equiv_2
->m_names
);
359 // Ensure equiv_2s names are in the summary for BB.
360 bitmap_ior_into (m_equiv
[bb
->index
]->m_names
, equiv_2
->m_names
);
363 // If equiv_2 is in BB, use it for the combined set.
364 if (equiv_2
->m_bb
== bb
)
366 bitmap_ior_into (equiv_2
->m_names
, equiv_1
->m_names
);
367 // Add equiv_1 names into the summary.
368 bitmap_ior_into (m_equiv
[bb
->index
]->m_names
, equiv_1
->m_names
);
372 // At this point, neither equivalence is from this block.
373 bitmap b
= BITMAP_ALLOC (&m_bitmaps
);
374 bitmap_copy (b
, equiv_1
->m_names
);
375 bitmap_ior_into (b
, equiv_2
->m_names
);
380 // Register an equivalence between SSA1 and SSA2 in block BB.
381 // The equivalence oracle maintains a vector of equivalencies indexed by basic
382 // block. When an equivalence bteween SSA1 and SSA2 is registered in block BB,
383 // a query is made as to what equivalences both names have already, and
384 // any preexisting equivalences are merged to create a single equivalence
385 // containing all the ssa_names in this basic block.
388 equiv_oracle::register_equiv (basic_block bb
, tree ssa1
, tree ssa2
)
390 unsigned v1
= SSA_NAME_VERSION (ssa1
);
391 unsigned v2
= SSA_NAME_VERSION (ssa2
);
392 equiv_chain
*equiv_1
= find_equiv_dom (ssa1
, bb
);
393 equiv_chain
*equiv_2
= find_equiv_dom (ssa2
, bb
);
395 // Check if they are the same set
396 if (equiv_1
&& equiv_1
== equiv_2
)
401 // Case where we have 2 SSA_NAMEs that are not in any set.
402 if (!equiv_1
&& !equiv_2
)
404 bitmap_set_bit (m_equiv_set
, v1
);
405 bitmap_set_bit (m_equiv_set
, v2
);
407 equiv_set
= BITMAP_ALLOC (&m_bitmaps
);
408 bitmap_set_bit (equiv_set
, v1
);
409 bitmap_set_bit (equiv_set
, v2
);
411 else if (!equiv_1
&& equiv_2
)
412 equiv_set
= register_equiv (bb
, v1
, equiv_2
);
413 else if (equiv_1
&& !equiv_2
)
414 equiv_set
= register_equiv (bb
, v2
, equiv_1
);
416 equiv_set
= register_equiv (bb
, equiv_1
, equiv_2
);
418 // A non-null return is a bitmap that is to be added to the current
419 // block as a new equivalence.
425 // Check if this is the first time a block has an equivalence added.
426 // and create a header block. And set the summary for this block.
427 if (!m_equiv
[bb
->index
])
429 ptr
= (equiv_chain
*) obstack_alloc (&m_chain_obstack
,
430 sizeof (equiv_chain
));
431 ptr
->m_names
= BITMAP_ALLOC (&m_bitmaps
);
432 bitmap_copy (ptr
->m_names
, equiv_set
);
435 m_equiv
[bb
->index
] = ptr
;
438 // Now create the element for this equiv set and initialize it.
439 ptr
= (equiv_chain
*) obstack_alloc (&m_chain_obstack
, sizeof (equiv_chain
));
440 ptr
->m_names
= equiv_set
;
442 gcc_checking_assert (bb
->index
< (int)m_equiv
.length ());
443 ptr
->m_next
= m_equiv
[bb
->index
]->m_next
;
444 m_equiv
[bb
->index
]->m_next
= ptr
;
445 bitmap_ior_into (m_equiv
[bb
->index
]->m_names
, equiv_set
);
448 // Make sure the BB vector is big enough and grow it if needed.
451 equiv_oracle::limit_check (basic_block bb
)
453 int i
= (bb
) ? bb
->index
: last_basic_block_for_fn (cfun
);
454 if (i
>= (int)m_equiv
.length ())
455 m_equiv
.safe_grow_cleared (last_basic_block_for_fn (cfun
) + 1);
458 // Dump the equivalence sets in BB to file F.
461 equiv_oracle::dump (FILE *f
, basic_block bb
) const
463 if (bb
->index
>= (int)m_equiv
.length ())
465 if (!m_equiv
[bb
->index
])
468 equiv_chain
*ptr
= m_equiv
[bb
->index
]->m_next
;
469 for (; ptr
; ptr
= ptr
->m_next
)
473 // Dump all equivalence sets known to the oracle.
476 equiv_oracle::dump (FILE *f
) const
478 fprintf (f
, "Equivalency dump\n");
479 for (unsigned i
= 0; i
< m_equiv
.length (); i
++)
480 if (m_equiv
[i
] && BASIC_BLOCK_FOR_FN (cfun
, i
))
482 fprintf (f
, "BB%d\n", i
);
483 dump (f
, BASIC_BLOCK_FOR_FN (cfun
, i
));
488 // --------------------------------------------------------------------------
490 // The value-relation class is used to encapsulate the represention of an
491 // individual relation between 2 ssa-names, and to facilitate operating on
498 value_relation (relation_kind kind
, tree n1
, tree n2
);
499 void set_relation (relation_kind kind
, tree n1
, tree n2
);
501 inline relation_kind
kind () const { return related
; }
502 inline tree
op1 () const { return name1
; }
503 inline tree
op2 () const { return name2
; }
505 bool union_ (value_relation
&p
);
506 bool intersect (value_relation
&p
);
508 bool apply_transitive (const value_relation
&rel
);
510 void dump (FILE *f
) const;
512 relation_kind related
;
516 // Set relation R between ssa_name N1 and N2.
519 value_relation::set_relation (relation_kind r
, tree n1
, tree n2
)
521 gcc_checking_assert (SSA_NAME_VERSION (n1
) != SSA_NAME_VERSION (n2
));
527 // Default constructor.
530 value_relation::value_relation ()
537 // Constructor for relation R between SSA version N1 nd N2.
540 value_relation::value_relation (relation_kind kind
, tree n1
, tree n2
)
542 set_relation (kind
, n1
, n2
);
545 // Negate the current relation.
548 value_relation::negate ()
550 related
= relation_negate (related
);
553 // Perform an intersection between 2 relations. *this &&= p.
556 value_relation::intersect (value_relation
&p
)
558 // Save previous value
559 relation_kind old
= related
;
561 if (p
.op1 () == op1 () && p
.op2 () == op2 ())
562 related
= relation_intersect (kind (), p
.kind ());
563 else if (p
.op2 () == op1 () && p
.op1 () == op2 ())
564 related
= relation_intersect (kind (), relation_swap (p
.kind ()));
568 return old
!= related
;
571 // Perform a union between 2 relations. *this ||= p.
574 value_relation::union_ (value_relation
&p
)
576 // Save previous value
577 relation_kind old
= related
;
579 if (p
.op1 () == op1 () && p
.op2 () == op2 ())
580 related
= relation_union (kind(), p
.kind());
581 else if (p
.op2 () == op1 () && p
.op1 () == op2 ())
582 related
= relation_union (kind(), relation_swap (p
.kind ()));
586 return old
!= related
;
589 // Identify and apply any transitive relations between REL
590 // and THIS. Return true if there was a transformation.
593 value_relation::apply_transitive (const value_relation
&rel
)
595 relation_kind k
= VREL_NONE
;
597 // Idenity any common operand, and notrmalize the relations to
598 // the form : A < B B < C produces A < C
599 if (rel
.op1 () == name2
)
602 if (rel
.op2 () == name1
)
604 k
= relation_transitive (kind (), rel
.kind ());
612 else if (rel
.op1 () == name1
)
615 if (rel
.op2 () == name2
)
617 k
= relation_transitive (relation_swap (kind ()), rel
.kind ());
626 else if (rel
.op2 () == name2
)
629 if (rel
.op1 () == name1
)
631 k
= relation_transitive (kind (), relation_swap (rel
.kind ()));
639 else if (rel
.op2 () == name1
)
642 if (rel
.op1 () == name2
)
644 k
= relation_transitive (relation_swap (kind ()),
645 relation_swap (rel
.kind ()));
657 // Dump the relation to file F.
660 value_relation::dump (FILE *f
) const
662 if (!name1
|| !name2
)
664 fprintf (f
, "uninitialized");
668 print_generic_expr (f
, op1 (), TDF_SLIM
);
669 print_relation (f
, kind ());
670 print_generic_expr (f
, op2 (), TDF_SLIM
);
674 // This container is used to link relations in a chain.
676 class relation_chain
: public value_relation
679 relation_chain
*m_next
;
682 // ------------------------------------------------------------------------
684 // Instantiate a relation oracle.
686 relation_oracle::relation_oracle ()
688 m_relations
.create (0);
689 m_relations
.safe_grow_cleared (last_basic_block_for_fn (cfun
) + 1);
690 m_relation_set
= BITMAP_ALLOC (&m_bitmaps
);
691 m_tmp
= BITMAP_ALLOC (&m_bitmaps
);
692 m_tmp2
= BITMAP_ALLOC (&m_bitmaps
);
695 // Destruct a relation oracle.
697 relation_oracle::~relation_oracle ()
699 m_relations
.release ();
702 // Register relation K between ssa_name OP1 and OP2 on STMT.
705 relation_oracle::register_relation (gimple
*stmt
, relation_kind k
, tree op1
,
708 gcc_checking_assert (TREE_CODE (op1
) == SSA_NAME
);
709 gcc_checking_assert (TREE_CODE (op2
) == SSA_NAME
);
710 gcc_checking_assert (stmt
&& gimple_bb (stmt
));
712 // Don't register lack of a relation.
716 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
718 value_relation
vr (k
, op1
, op2
);
719 fprintf (dump_file
, " Registering value_relation ");
721 fprintf (dump_file
, " (bb%d) at ", gimple_bb (stmt
)->index
);
722 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
725 // This relation applies to the entire block, use STMT's block.
726 // Equivalencies are handled by the equivalence oracle.
728 register_equiv (gimple_bb (stmt
), op1
, op2
);
730 register_relation (gimple_bb (stmt
), k
, op1
, op2
);
733 // Register relation K between ssa_name OP1 and OP2 on edge E.
736 relation_oracle::register_relation (edge e
, relation_kind k
, tree op1
,
739 gcc_checking_assert (TREE_CODE (op1
) == SSA_NAME
);
740 gcc_checking_assert (TREE_CODE (op2
) == SSA_NAME
);
742 // Do not register lack of relation, or blocks which have more than
743 // edge E for a predecessor.
744 if (k
== VREL_NONE
|| !single_pred_p (e
->dest
))
747 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
749 value_relation
vr (k
, op1
, op2
);
750 fprintf (dump_file
, " Registering value_relation ");
752 fprintf (dump_file
, " on (%d->%d)\n", e
->src
->index
, e
->dest
->index
);
755 // Equivalencies are handled by the equivalence oracle.
757 register_equiv (e
->dest
, op1
, op2
);
759 register_relation (e
->dest
, k
, op1
, op2
);
762 // Register relation K between OP! and OP2 in block BB.
763 // This creates the record and searches for existing records in the dominator
764 // tree to merge with.
765 // TRANSITIVE_P is true if this is being registered as a transitive operation,
766 // and should not try to register further transitives.
769 relation_oracle::register_relation (basic_block bb
, relation_kind k
, tree op1
,
770 tree op2
, bool transitive_p
)
772 gcc_checking_assert (k
!= VREL_NONE
);
774 value_relation
vr(k
, op1
, op2
);
777 if (bbi
>= (int)m_relations
.length())
778 m_relations
.safe_grow_cleared (last_basic_block_for_fn (cfun
) + 1);
780 // Summary bitmap indicating what ssa_names have relations in this BB.
781 bitmap bm
= m_relations
[bbi
].m_names
;
783 bm
= m_relations
[bbi
].m_names
= BITMAP_ALLOC (&m_bitmaps
);
784 unsigned v1
= SSA_NAME_VERSION (op1
);
785 unsigned v2
= SSA_NAME_VERSION (op2
);
789 curr
= find_relation_block (bbi
, v1
, v2
, &ptr
);
790 // There is an existing relation in this block, just intersect with it.
791 if (curr
!= VREL_NONE
)
793 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
795 fprintf (dump_file
, " Intersecting with existing ");
796 ptr
->dump (dump_file
);
798 // Check into whether we can simply replace the relation rather than
799 // intersecting it. THis may help with some optimistic iterative
800 // updating algorithms.
802 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
804 fprintf (dump_file
, " to produce ");
805 ptr
->dump (dump_file
);
806 fprintf (dump_file
, "\n");
811 // Check for an existing relation further up the DOM chain.
812 // By including dominating relations, The first one found in any search
813 // will be the aggregate of all the previous ones.
814 curr
= find_relation_dom (bb
, v1
, v2
);
815 if (curr
!= VREL_NONE
)
816 k
= relation_intersect (curr
, k
);
818 bitmap_set_bit (bm
, v1
);
819 bitmap_set_bit (bm
, v2
);
820 bitmap_set_bit (m_relation_set
, v1
);
821 bitmap_set_bit (m_relation_set
, v2
);
823 ptr
= (relation_chain
*) obstack_alloc (&m_chain_obstack
,
824 sizeof (relation_chain
));
825 ptr
->set_relation (k
, op1
, op2
);
826 ptr
->m_next
= m_relations
[bbi
].m_head
;
827 m_relations
[bbi
].m_head
= ptr
;;
831 register_transitives (bb
, *ptr
);
834 // Starting at ROOT_BB search the DOM tree looking for relations which
835 // may produce transitive relations to RELATION. EQUIV1 and EQUIV2 are
836 // bitmaps for op1/op2 and any of their equivalences that should also be
840 relation_oracle::register_transitives (basic_block root_bb
,
841 const value_relation
&relation
,
846 for (bb
= root_bb
; bb
; bb
= get_immediate_dominator (CDI_DOMINATORS
, bb
))
849 if (bbi
>= (int)m_relations
.length())
851 const_bitmap bm
= m_relations
[bbi
].m_names
;
854 if (!bitmap_intersect_p (bm
, equiv1
) && !bitmap_intersect_p (bm
, equiv2
))
856 // At least one of the 2 ops has a relation in this block.
858 for (ptr
= m_relations
[bbi
].m_head
; ptr
; ptr
= ptr
->m_next
)
860 // In the presence of an equivalence, 2 operands may do not
861 // naturally match. ie with equivalence a_2 == b_3
862 // given c_1 < a_2 && b_3 < d_4
863 // convert the second relation (b_3 < d_4) to match any
864 // equivalences to found in the first relation.
865 // ie convert b_3 < d_4 to a_2 < d_4, which then exposes the
866 // transitive operation: c_1 < a_2 && a_2 < d_4 -> c_1 < d_4
869 tree p1
= ptr
->op1 ();
870 tree p2
= ptr
->op2 ();
871 // Find which equivalence is in the first operand.
872 if (bitmap_bit_p (equiv1
, SSA_NAME_VERSION (p1
)))
874 else if (bitmap_bit_p (equiv1
, SSA_NAME_VERSION (p2
)))
879 // Find which equivalence is in the second operand.
880 if (bitmap_bit_p (equiv2
, SSA_NAME_VERSION (p1
)))
882 else if (bitmap_bit_p (equiv2
, SSA_NAME_VERSION (p2
)))
887 // Ignore if both NULL (not relevant relation) or the same,
891 // Any operand not an equivalence, just take the real operand.
893 r1
= relation
.op1 ();
895 r2
= relation
.op2 ();
897 value_relation
nr (relation
.kind (), r1
, r2
);
898 if (nr
.apply_transitive (*ptr
))
900 register_relation (root_bb
, nr
.kind (), nr
.op1 (), nr
.op2 (),
902 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
904 fprintf (dump_file
, " Registering transitive relation ");
906 fputc ('\n', dump_file
);
914 // Find adn register any transitive relations implied by RELATION occuring
918 relation_oracle::register_transitives (basic_block bb
,
919 const value_relation
&relation
)
921 // Only apply transitives to certain kinds of operations.
922 switch (relation
.kind ())
933 // Set up the bitmaps for op1 and op2, and if there are no equivalencies,
934 // set just op1 or op2 in their own bitmap.
935 const_bitmap equiv1
= equiv_set (relation
.op1 (), bb
);
936 const_bitmap equiv2
= equiv_set (relation
.op2 (), bb
);
940 register_transitives (bb
, relation
, equiv1
, equiv2
);
943 bitmap_clear (m_tmp
);
944 bitmap_set_bit (m_tmp
, SSA_NAME_VERSION (relation
.op2 ()));
945 register_transitives (bb
, relation
, equiv1
, m_tmp
);
950 bitmap_clear (m_tmp
);
951 bitmap_set_bit (m_tmp
, SSA_NAME_VERSION (relation
.op1 ()));
952 register_transitives (bb
, relation
, m_tmp
, equiv2
);
956 bitmap_clear (m_tmp
);
957 bitmap_clear (m_tmp2
);
958 bitmap_set_bit (m_tmp
, SSA_NAME_VERSION (relation
.op1 ()));
959 bitmap_set_bit (m_tmp2
, SSA_NAME_VERSION (relation
.op2 ()));
960 register_transitives (bb
, relation
, m_tmp
, m_tmp2
);
964 // Find the relation between any ssa_name in B1 and any name in B2 in block BB.
965 // This will allow equivalencies to be applied to any SSA_NAME in a relation.
968 relation_oracle::find_relation_block (unsigned bb
, const_bitmap b1
,
972 if (bb
>= m_relations
.length())
975 bm
= m_relations
[bb
].m_names
;
979 // If both b1 and b2 aren't referenced in thie block, cant be a relation
980 if (!bitmap_intersect_p (bm
, b1
) || !bitmap_intersect_p (bm
, b2
))
983 // Search for the fiorst relation that contains BOTH an element from B1
984 // and B2, and return that relation.
985 for (relation_chain
*ptr
= m_relations
[bb
].m_head
; ptr
; ptr
= ptr
->m_next
)
987 unsigned op1
= SSA_NAME_VERSION (ptr
->op1 ());
988 unsigned op2
= SSA_NAME_VERSION (ptr
->op2 ());
989 if (bitmap_bit_p (b1
, op1
) && bitmap_bit_p (b2
, op2
))
991 if (bitmap_bit_p (b1
, op2
) && bitmap_bit_p (b2
, op1
))
992 return relation_swap (ptr
->kind ());
998 // Search the DOM tree for a relation between an element of B1 and B2, starting
1002 relation_oracle::find_relation_dom (basic_block bb
, const_bitmap b1
,
1006 // If either name does not occur in a relation anywhere, there isnt one.
1007 if (!bitmap_intersect_p (m_relation_set
, b1
)
1008 || !bitmap_intersect_p (m_relation_set
, b2
))
1011 // Search each block in the DOM tree checking.
1012 for ( ; bb
; bb
= get_immediate_dominator (CDI_DOMINATORS
, bb
))
1014 r
= find_relation_block (bb
->index
, b1
, b2
);
1022 // Find a relation in block BB between ssa version V1 and V2. If a relation
1023 // is found, return a pointer to the chain object in OBJ.
1026 relation_oracle::find_relation_block (int bb
, unsigned v1
, unsigned v2
,
1027 relation_chain
**obj
)
1029 if (bb
>= (int)m_relations
.length())
1032 const_bitmap bm
= m_relations
[bb
].m_names
;
1036 // If both b1 and b2 aren't referenced in thie block, cant be a relation
1037 if (!bitmap_bit_p (bm
, v1
) || !bitmap_bit_p (bm
, v2
))
1040 relation_chain
*ptr
;
1041 for (ptr
= m_relations
[bb
].m_head
; ptr
; ptr
= ptr
->m_next
)
1043 unsigned op1
= SSA_NAME_VERSION (ptr
->op1 ());
1044 unsigned op2
= SSA_NAME_VERSION (ptr
->op2 ());
1045 if (v1
== op1
&& v2
== op2
)
1049 return ptr
->kind ();
1051 if (v1
== op2
&& v2
== op1
)
1055 return relation_swap (ptr
->kind ());
1062 // Find a relation between SSA version V1 and V2 in the dominator tree
1063 // starting with block BB
1066 relation_oracle::find_relation_dom (basic_block bb
, unsigned v1
, unsigned v2
)
1069 // IF either name does not occur in a relation anywhere, there isnt one.
1070 if (!bitmap_bit_p (m_relation_set
, v1
) || !bitmap_bit_p (m_relation_set
, v2
))
1073 for ( ; bb
; bb
= get_immediate_dominator (CDI_DOMINATORS
, bb
))
1075 r
= find_relation_block (bb
->index
, v1
, v2
);
1083 // Query if there is a relation between SSA1 and SS2 in block BB or a
1087 relation_oracle::query_relation (basic_block bb
, tree ssa1
, tree ssa2
)
1090 unsigned v1
= SSA_NAME_VERSION (ssa1
);
1091 unsigned v2
= SSA_NAME_VERSION (ssa2
);
1095 // Check for equivalence first.
1096 const_bitmap equiv1
= equiv_set (ssa1
, bb
);
1097 if (equiv1
&& bitmap_bit_p (equiv1
, v2
))
1100 // Initially look for a direct relationship and just return that.
1101 kind
= find_relation_dom (bb
, v1
, v2
);
1102 if (kind
!= VREL_NONE
)
1105 // If v2 isn't in v1s equiv set, then v1 shouldn't be in v2's set either.
1106 // It is possible for out-of-order dominator processing to have an out of
1107 // sync set of equivalences.. Down the road, when we do full updates,
1108 // change this to an assert to ensure everything is in sync.
1109 const_bitmap equiv2
= equiv_set (ssa2
, bb
);
1110 if (equiv2
&& bitmap_bit_p (equiv2
, v1
))
1113 // If not equal, see if there is a relationship between equivalences.
1114 if (!equiv1
&& !equiv2
)
1118 bitmap_clear (m_tmp
);
1119 bitmap_set_bit (m_tmp
, v1
);
1120 kind
= find_relation_dom (bb
, m_tmp
, equiv2
);
1124 bitmap_clear (m_tmp
);
1125 bitmap_set_bit (m_tmp
, v2
);
1126 kind
= find_relation_dom (bb
, equiv1
, m_tmp
);
1129 kind
= find_relation_dom (bb
, equiv1
, equiv2
);
1133 // Dump all the relations in block BB to file F.
1136 relation_oracle::dump (FILE *f
, basic_block bb
) const
1138 equiv_oracle::dump (f
,bb
);
1140 if (bb
->index
>= (int)m_relations
.length ())
1142 if (!m_relations
[bb
->index
].m_names
)
1145 relation_chain
*ptr
= m_relations
[bb
->index
].m_head
;
1146 for (; ptr
; ptr
= ptr
->m_next
)
1148 fprintf (f
, "Relational : ");
1154 // Dump all the relations known to file F.
1157 relation_oracle::dump (FILE *f
) const
1159 fprintf (f
, "Relation dump\n");
1160 for (unsigned i
= 0; i
< m_relations
.length (); i
++)
1161 if (BASIC_BLOCK_FOR_FN (cfun
, i
))
1163 fprintf (f
, "BB%d\n", i
);
1164 dump (f
, BASIC_BLOCK_FOR_FN (cfun
, i
));