https://gcc.gnu.org/g:3e468676c402207ad0e14831084f8dfcdfa61883

commit r17-6331-g3e468676c402207ad0e14831084f8dfcdfa61883
Author: Sendao Yan <[email protected]>
Date:   Mon Nov 16 15:46:27 2026 -0700

    predicate::simplify_4() copies a pred_chain with
    
      pred_chain a_chain = m_preds[i];
    
    pred_chain is a vec<T, va_heap, vl_ptr>, whose copy constructor is the
    default one, so the copy shares the pointer to the elements.  Releasing
    the copy frees the buffer and leaves m_preds[i] pointing to freed
    memory.  The clean-up loop immediately below then reads
    m_preds[i].is_empty() through that dangling pointer, and if the freed
    header still looks non-empty the chain is pushed into the new union and
    freed a second time in ~predicate().  Refer to the chain in place
    instead.
    
    For the same reason simplify() cannot tell the two ways in which
    simplify_1b() reports success: an empty chain is a tautology, which
    makes the whole disjunction TRUE, while a non-empty chain whose
    conjuncts folded to zero is a contradiction, which just drops that
    disjunct.  simplify() releases the chain in both cases, so a disjunction
    that folds to TRUE is turned into the OR of the remaining disjuncts, and
    a predicate all of whose disjuncts are contradictions keeps the empty
    value it was constructed with -- TRUE for the use predicate, which is
    built to mean "unconditional use" and then filled in by init_use_preds.
    Handle both cases and set m_cval accordingly.
    
    Both are latent: predicate::simplify() has so far only been called
    on predicates built from comparisons and before normalize(), so
    simplify_4() never matched and simplify_1b() never folded a chain.  The
    PR128102 fix calls simplify() on an already normalized predicate, which
    makes the first one reachable.
    
    gcc/
            * gimple-predicate-analysis.cc (predicate::simplify_4): Do not
            release a shallow copy of a chain.
            (predicate::simplify): Set m_cval when a disjunct or the whole
            predicate folds to a constant.
    
    Signed-off-by: Sendao Yan <[email protected]>

Diff:
---
 gcc/gimple-predicate-analysis.cc | 36 +++++++++++++++++++++++++++++++-----
 1 file changed, 31 insertions(+), 5 deletions(-)

diff --git a/gcc/gimple-predicate-analysis.cc b/gcc/gimple-predicate-analysis.cc
index 384fe32078e1..41c04965fd0d 100644
--- a/gcc/gimple-predicate-analysis.cc
+++ b/gcc/gimple-predicate-analysis.cc
@@ -1473,7 +1473,11 @@ predicate::simplify_4 ()
   unsigned n = m_preds.length ();
   for (unsigned i = 0; i < n; i++)
     {
-      pred_chain a_chain = m_preds[i];
+      /* pred_chain is a vec<> whose copy constructor is the default one, so
+        copying the chain copies the pointer to the elements, not the
+        elements themselves.  Releasing such a copy frees the storage that
+        m_preds[i] still points to, so refer to the chain in place.  */
+      pred_chain &a_chain = m_preds[i];
       if (a_chain.length () != 1)
        continue;
 
@@ -1543,15 +1547,37 @@ predicate::simplify (gimple *use_or_def, bool is_use)
       dump (dump_file, use_or_def, is_use ? "[USE]:\n" : "[DEF]:\n");
     }
 
+  bool dropped = false;
   for (unsigned i = 0; i < m_preds.length (); i++)
     {
       ::simplify_1a (m_preds[i]);
-      if (::simplify_1b (m_preds[i]))
+      if (!::simplify_1b (m_preds[i]))
+       continue;
+
+      if (m_preds[i].is_empty ())
        {
-         m_preds[i].release ();
-         m_preds.ordered_remove (i);
-         i--;
+         /* The chain is a tautology, and so is the whole disjunction.  */
+         for (unsigned k = 0; k < m_preds.length (); ++k)
+           m_preds[k].release ();
+         m_preds.truncate (0);
+         m_cval = true;
+         return;
        }
+
+      /* The chain is a contradiction, i.e. a false disjunct: drop it.  */
+      m_preds[i].release ();
+      m_preds.ordered_remove (i);
+      dropped = true;
+      i--;
+    }
+
+  /* If every disjunct was a contradiction the predicate is false, even
+     for a predicate whose empty value is true (the use predicate is
+     initialized to true and then filled in by init_use_preds).  */
+  if (dropped && m_preds.is_empty ())
+    {
+      m_cval = false;
+      return;
     }
 
   if (m_preds.length () < 2)

Reply via email to