https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127917
--- Comment #3 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The master branch has been updated by Martin Jambor <[email protected]>: https://gcc.gnu.org/g:ebd81e6e396d3d50e4ac89404e45f5d2029a186d commit r17-5258-gebd81e6e396d3d50e4ac89404e45f5d2029a186d Author: Hugo Sales <[email protected]> Date: Fri Oct 16 11:50:48 2026 +0100 ipa-cp: Check polymorphic contexts before redirecting to a clone [PR127917] perhaps_add_new_callers redirects a call to an existing specialized clone when the call passes the value the clone was made for, and all the scalar and aggregate constants the clone assumes. It does not compare polymorphic contexts. A clone made for one parameter can also assume a dynamic type for another parameter, recorded in its known_contexts, and IPA-CP devirtualizes the calls in the clone's body for that type. A call that passes a different dynamic type must not be redirected to it, or the program runs the wrong virtual method. Since IPA-CP makes several decision sweeps, this happens with self-recursive functions. In the new test, the first sweeps clone Tree::Select (int, Selector &) for a context of THIS, and that clone assumes S is a Bad. A later sweep makes another clone, in which S is a plain Selector. The recursive call in the new clone passes the THIS context the old clone was made for, and there are no scalars or aggregates to compare, so IPA-CP redirects it to the old clone. That clone calls Bad::Accept directly, and the test aborts. This patch adds cgraph_edge_brings_all_contexts_for_node, which does for polymorphic contexts what cgraph_edge_brings_all_scalars_for_node does for scalars, and requires it in perhaps_add_new_callers. As in ipcp_val_replacement_ok_p, a context more precise than the one the clone was specialized for is still accepted. gcc/ChangeLog: PR ipa/127917 * ipa-cp.cc (cgraph_edge_brings_all_contexts_for_node): New function. (perhaps_add_new_callers): Use it. gcc/testsuite/ChangeLog: PR ipa/127917 * g++.dg/ipa/pr127917.C: New test. Signed-off-by: Hugo Sales <[email protected]>
