https://gcc.gnu.org/g:e75a9e33b97cfdcc76b6bfea3e7ca6184be5d5c0

commit r16-5607-ge75a9e33b97cfdcc76b6bfea3e7ca6184be5d5c0
Author: Andrew Pinski <[email protected]>
Date:   Tue Nov 25 14:19:18 2025 -0800

    phiprop: Make sure types of the load match the inserted phi [PR122847]
    
    This was introduced with r16-5556-ge94e91d6f3775, but the type
    check for the delay was not happen because the type at the point
    of delaying was set to NULL. It is only until a non-delayed load
    when the phi is created, the type is set.
    
    This adds the type check to the replacement for the delayed statements.
    
    Pushed as obvious.
    
            PR tree-optimization/122847
    
    gcc/ChangeLog:
    
            * tree-ssa-phiprop.cc (propagate_with_phi): Add type
            check for reuse of the phi for the delayed statements.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.dg/torture/pr122847-1.c: New test.
    
    Signed-off-by: Andrew Pinski <[email protected]>

Diff:
---
 gcc/testsuite/gcc.dg/torture/pr122847-1.c | 17 +++++++++++++++++
 gcc/tree-ssa-phiprop.cc                   |  3 +++
 2 files changed, 20 insertions(+)

diff --git a/gcc/testsuite/gcc.dg/torture/pr122847-1.c 
b/gcc/testsuite/gcc.dg/torture/pr122847-1.c
new file mode 100644
index 000000000000..9ec4360e329c
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr122847-1.c
@@ -0,0 +1,17 @@
+/* { dg-do compile } */
+/* PR tree-optimization/122847 */      
+
+struct {
+  char x[6];
+} *a, b;
+
+int c;
+
+int d() {
+  /* `a->x` might trap. */
+  char *p = a ? a->x : b.x;
+  char e = *p;
+  if (c)
+    return *(short *)p & e;
+  return 0;
+}
diff --git a/gcc/tree-ssa-phiprop.cc b/gcc/tree-ssa-phiprop.cc
index d24613d58938..04aa138f521c 100644
--- a/gcc/tree-ssa-phiprop.cc
+++ b/gcc/tree-ssa-phiprop.cc
@@ -506,6 +506,9 @@ next:;
   if (phi_inserted)
     for (auto use_stmt : delayed_uses)
       {
+       /* The types must match of the inserted phi.  */
+       if (!types_compatible_p (type, TREE_TYPE (gimple_assign_lhs 
(use_stmt))))
+         continue;
        gimple_assign_set_rhs1 (use_stmt, res);
        update_stmt (use_stmt);
       }

Reply via email to