https://gcc.gnu.org/g:7ce404d9c953b0a1b51e1501ca7332b037170481

commit r17-6277-g7ce404d9c953b0a1b51e1501ca7332b037170481
Author: Jan Hubicka <[email protected]>
Date:   Sat Nov 14 19:01:33 2026 +0100

    Add support for unknown offset accesses in ipa-predicates.
    
    This patch extends ipa_predicates to handle situation where a conditional
    involved aggregate parameter with unknown offset and the parameter points
    to STRING_CST.  In this case we can inspect string for stuff the function
    look for.  In cppcheck this is, for example, special character '%' which
    is not passed on caller site.
    
    This is just bare bones of the support.  We need to handle PHIs and jump
    functions which is done incrementally.
    
    Testcases are by Google Carbon.
    
    Bootstrapped/regtested x86_64-linux.
    
    gcc/ChangeLog:
    
            PR ipa/127171
            * ipa-fnsummary.cc (struct string_char_set_hashmap_traits): New.
            (get_string_char_set): New function.
            (char_set_any_in_range_p): New function.
            (fold_string_char_compare): New function.
            (evaluate_unknown_offset_condition): Evaluate unknown offsets.
            (evaluate_conditions_for_known_args): Handle unknown_offset.
            (unmodified_parm_or_parm_agg_item): Compute unknown_offset.
            (set_cond_stmt_execution_predicate):  Update.
            (will_be_nonconstant_predicate): Update.
            (inline_read_section): Update.
            (ipa_fn_summary_write): Stream uknonwn_offset.
            (ipa_fnsummary_cc_finalize): Stream uknonwn_offset.
            * ipa-fnsummary.h (struct agg_position_info): Add unknown_offset.
            * ipa-predicate.cc (ipa_predicate::add_clause): Handle 
unknown_offset.
            (ipa_predicate::probability): Handle unknown_offset conservatively.
            (dump_condition): Dump unkonwn_offset.
            (ipa_predicate::remap_after_inlining): Update unknown_offset.
            (add_condition): Handle unkonwn_offset.
            * ipa-predicate.h (struct condition): Add unknown_offset.
            * ipa-prop.cc (ipa_load_from_parm_agg_unknown_offset): New function.
            (parm_ref_data_pass_through_p): Use it.
            (ipa_load_from_parm_agg): Handle unknown_offset.
            * ipa-prop.h (ipa_load_from_parm_agg): Likewise.
    
    gcc/testsuite/ChangeLog:
    
            * gcc.dg/ipa/fnsummary-10.c: New test.
            * gcc.dg/ipa/fnsummary-7.c: New test.
            * gcc.dg/ipa/fnsummary-8.c: New test.
            * gcc.dg/ipa/fnsummary-9.c: New test.
            Assisted by Gemini Carbon

Diff:
---
 gcc/ipa-fnsummary.cc                    | 372 ++++++++++++++++++++++++++++++--
 gcc/ipa-fnsummary.h                     |   1 +
 gcc/ipa-predicate.cc                    |  38 +++-
 gcc/ipa-predicate.h                     |   4 +-
 gcc/ipa-prop.cc                         | 224 +++++++++++++------
 gcc/ipa-prop.h                          |   3 +-
 gcc/testsuite/gcc.dg/ipa/fnsummary-10.c | 263 ++++++++++++++++++++++
 gcc/testsuite/gcc.dg/ipa/fnsummary-7.c  |  37 ++++
 gcc/testsuite/gcc.dg/ipa/fnsummary-8.c  | 125 +++++++++++
 gcc/testsuite/gcc.dg/ipa/fnsummary-9.c  | 347 +++++++++++++++++++++++++++++
 10 files changed, 1328 insertions(+), 86 deletions(-)

diff --git a/gcc/ipa-fnsummary.cc b/gcc/ipa-fnsummary.cc
index 7c37802e8bb8..e194a8ca3d93 100644
--- a/gcc/ipa-fnsummary.cc
+++ b/gcc/ipa-fnsummary.cc
@@ -71,6 +71,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "gimple-pretty-print.h"
 #include "cfganal.h"
 #include "gimple-iterator.h"
+#include "gimple-fold.h"
 #include "tree-cfg.h"
 #include "tree-ssa-loop-niter.h"
 #include "tree-ssa-loop.h"
@@ -378,6 +379,318 @@ add_freqcounting_predicate 
(vec<ipa_freqcounting_predicate, va_gc> **v,
 
    ERROR_MARK value of an argument means compile time invariant.  */
 
+/* Hashmap traits that free the cached sbitmap when an entry is removed.  */
+
+struct string_char_set_hashmap_traits
+  : simple_hashmap_traits<default_hash_traits<tree>, sbitmap>
+{
+  template <typename T>
+  static inline void
+  remove (T &entry)
+  {
+    sbitmap_free (entry.m_value);
+    simple_hashmap_traits<default_hash_traits<tree>, sbitmap>::remove (entry);
+  }
+};
+
+/* Map from unique STRING_CST tree nodes (longer than 256 bytes) to an
+   sbitmap recording which byte values appear in the string.  */
+
+static hash_map<tree, sbitmap, string_char_set_hashmap_traits>
+  *string_char_set_cache;
+
+/* Return the character presence bitmap for STR (with TREE_STRING_LENGTH (STR)
+   > 256), populating and memoizing it on first lookup.  PREC is the character
+   type precision.  */
+
+static const_sbitmap
+get_string_char_set (tree str, unsigned int prec)
+{
+  if (!string_char_set_cache)
+    string_char_set_cache
+      = new hash_map<tree, sbitmap, string_char_set_hashmap_traits>;
+
+  bool existed;
+  sbitmap &bmp = string_char_set_cache->get_or_insert (str, &existed);
+  if (!existed)
+    {
+      unsigned int full = 1U << prec;
+      unsigned int mask = full - 1;
+      bmp = sbitmap_alloc (full);
+      bitmap_clear (bmp);
+      unsigned int len = TREE_STRING_LENGTH (str);
+      const char *p = TREE_STRING_POINTER (str);
+      for (unsigned int i = 0; i < len; i++)
+       bitmap_set_bit (bmp, (unsigned char) p[i] & mask);
+    }
+  return bmp;
+}
+
+/* Return true if BMP contains any character whose ordered value with
+   signedness SGN and precision PREC falls in the inclusive unsigned
+   coordinate range [U_LO, U_HI].
+
+   For UNSIGNED, ordered coordinate == raw byte value.
+   For SIGNED, ordered coordinate == raw byte value ^ (1 << (PREC - 1)), so
+   negative bytes map to [0, half - 1] (raw [half, 2*half - 1]) and
+   non-negative bytes map to [half, 2*half - 1] (raw [0, half - 1]).  */
+
+static bool
+char_set_any_in_range_p (const_sbitmap bmp, unsigned int prec, signop sgn,
+                         unsigned int u_lo, unsigned int u_hi)
+{
+  if (u_lo > u_hi)
+    return false;
+  if (sgn == UNSIGNED)
+    return bitmap_any_bit_in_range_p (bmp, u_lo, u_hi);
+
+  unsigned int half = 1U << (prec - 1);
+  /* Check negative byte values in [u_lo, min (u_hi, half - 1)].  */
+  if (u_lo < half
+      && bitmap_any_bit_in_range_p (bmp, u_lo + half,
+                                   MIN (u_hi, half - 1) + half))
+    return true;
+  /* Check non-negative byte values in [max (u_lo, half), u_hi].  */
+  if (u_hi >= half
+      && bitmap_any_bit_in_range_p (bmp, MAX (u_lo, half) - half, u_hi - half))
+    return true;
+  return false;
+}
+
+/* Fold comparison (ch CODE VAL) across every byte ch of constant string STR
+   (interpreted with type CHAR_TYPE, defaulting to the element type of STR when
+   NULL).  Return boolean_true_node or boolean_false_node if the comparison has
+   the same boolean result for all bytes of STR, or NULL_TREE otherwise.  */
+
+static tree
+fold_string_char_compare (tree str, enum tree_code code, tree val,
+                         tree char_type)
+{
+  if (!char_type && str && TREE_TYPE (str)
+      && TREE_CODE (TREE_TYPE (str)) == ARRAY_TYPE)
+    char_type = TREE_TYPE (TREE_TYPE (str));
+
+  if (!str
+      || TREE_CODE (str) != STRING_CST
+      || !val
+      || TREE_CODE (val) != INTEGER_CST
+      || !char_type
+      || TREE_CODE (char_type) != INTEGER_TYPE
+      || TYPE_PRECISION (char_type) != BITS_PER_UNIT)
+    return NULL_TREE;
+
+  unsigned prec = TYPE_PRECISION (char_type);
+  signop sgn = TYPE_SIGN (char_type);
+  tree val_type = TREE_TYPE (val);
+  unsigned val_prec = TYPE_PRECISION (val_type);
+  signop val_sgn = TYPE_SIGN (val_type);
+
+  /* Require VAL to be an integral constant whose type preserves the range
+     and sign of CHAR_TYPE (either the same precision and sign, or a wider
+     type that cannot reinterpret a negative signed char as large positive).  
*/
+  if (!INTEGRAL_TYPE_P (val_type)
+      || val_prec < prec
+      || (val_prec == prec ? val_sgn != sgn
+                          : (sgn == SIGNED && val_sgn == UNSIGNED)))
+    return NULL_TREE;
+
+  int len = TREE_STRING_LENGTH (str);
+  if (len <= 0)
+    return NULL_TREE;
+
+  /* First test VAL against the full representable [c_min, c_max] range of
+     CHAR_TYPE so out-of-range or boundary comparisons fold without scanning
+     STR.  */
+  wide_int wval = wi::to_wide (val);
+  wide_int c_min = wide_int::from (wi::min_value (prec, sgn), val_prec, sgn);
+  wide_int c_max = wide_int::from (wi::max_value (prec, sgn), val_prec, sgn);
+
+  if (wi::lt_p (wval, c_min, val_sgn))
+    {
+      if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
+       return constant_boolean_node (false, boolean_type_node);
+      gcc_checking_assert (code == NE_EXPR || code == GT_EXPR
+                          || code == GE_EXPR);
+      return constant_boolean_node (true, boolean_type_node);
+    }
+  if (wi::gt_p (wval, c_max, val_sgn))
+    {
+      if (code == EQ_EXPR || code == GT_EXPR || code == GE_EXPR)
+       return constant_boolean_node (false, boolean_type_node);
+      gcc_checking_assert (code == NE_EXPR || code == LT_EXPR
+                          || code == LE_EXPR);
+      return constant_boolean_node (true, boolean_type_node);
+    }
+  if (wi::eq_p (wval, c_min))
+    {
+      if (code == LT_EXPR)
+       return constant_boolean_node (false, boolean_type_node);
+      if (code == GE_EXPR)
+       return constant_boolean_node (true, boolean_type_node);
+    }
+  if (wi::eq_p (wval, c_max))
+    {
+      if (code == GT_EXPR)
+       return constant_boolean_node (false, boolean_type_node);
+      if (code == LE_EXPR)
+       return constant_boolean_node (true, boolean_type_node);
+    }
+
+  /* For strings longer than 256 bytes, build and memoize an sbitmap of all
+     present byte values and answer queries by probing the target bit or
+     lower/upper ordered coordinate ranges.  */
+  if (len > 256)
+    {
+      const_sbitmap bmp = get_string_char_set (str, prec);
+      unsigned int full = 1U << prec;
+
+      if (code == EQ_EXPR || code == NE_EXPR)
+       {
+         unsigned int raw_target
+           = wide_int::from (wval, prec, UNSIGNED).to_uhwi ();
+         bool has_target = bitmap_bit_p (bmp, raw_target);
+         if (!has_target)
+           return constant_boolean_node (code == NE_EXPR, boolean_type_node);
+
+         bool has_other
+           = ((raw_target > 0
+               && bitmap_any_bit_in_range_p (bmp, 0, raw_target - 1))
+              || (raw_target + 1 < full
+                  && bitmap_any_bit_in_range_p (bmp, raw_target + 1,
+                                                 full - 1)));
+         if (!has_other)
+           return constant_boolean_node (code == EQ_EXPR, boolean_type_node);
+         return NULL_TREE;
+       }
+
+      gcc_checking_assert (code == LT_EXPR || code == LE_EXPR
+                          || code == GT_EXPR || code == GE_EXPR);
+
+      /* Partition the ordered coordinate domain [0, full - 1] into [0, split]
+        (values <= wval for LE/GT, or < wval for LT/GE) and
+        [split + 1, full - 1].  Boundary wval == c_min (with LT) and
+        wval == c_max (with GT) already folded above, so split >= 0 and
+        split + 1 <= full - 1 hold.  */
+      unsigned int half = (sgn == SIGNED) ? (full >> 1) : 0;
+      unsigned int u_val
+       = wide_int::from (wval, prec, UNSIGNED).to_uhwi () ^ half;
+      unsigned int split
+       = (code == LE_EXPR || code == GT_EXPR) ? u_val : u_val - 1;
+      bool any_lo = char_set_any_in_range_p (bmp, prec, sgn, 0, split);
+      bool any_hi
+       = char_set_any_in_range_p (bmp, prec, sgn, split + 1, full - 1);
+
+      bool known_true = ((code == LT_EXPR || code == LE_EXPR) ? !any_hi
+                                                             : !any_lo);
+      bool known_false = ((code == LT_EXPR || code == LE_EXPR) ? !any_lo
+                                                              : !any_hi);
+      if (known_true)
+       return constant_boolean_node (true, boolean_type_node);
+      if (known_false)
+       return constant_boolean_node (false, boolean_type_node);
+      return NULL_TREE;
+    }
+
+  /* For short strings, check EQ/NE via memchr and inequality comparisons by
+     computing the actual [wmin, wmax] byte bounds of STR.  */
+  const char *ptr = TREE_STRING_POINTER (str);
+  if (code == EQ_EXPR || code == NE_EXPR)
+    {
+      unsigned target = wide_int::from (wval, prec, UNSIGNED).to_uhwi ();
+      bool has_target = memchr (ptr, target, len) != NULL;
+      if (!has_target)
+       return constant_boolean_node (code == NE_EXPR, boolean_type_node);
+
+      bool has_other = false;
+      for (int k = 0; k < len; k++)
+       if ((unsigned char) ptr[k] != target)
+         {
+           has_other = true;
+           break;
+         }
+      if (!has_other)
+       return constant_boolean_node (code == EQ_EXPR, boolean_type_node);
+      return NULL_TREE;
+    }
+
+  wide_int wmin = wi::uhwi ((unsigned char) ptr[0], prec);
+  wide_int wmax = wmin;
+  for (int k = 1; k < len; k++)
+    {
+      wide_int wk = wi::uhwi ((unsigned char) ptr[k], prec);
+      if (wi::lt_p (wk, wmin, sgn))
+       wmin = wk;
+      if (wi::gt_p (wk, wmax, sgn))
+       wmax = wk;
+    }
+
+  wmin = wide_int::from (wmin, val_prec, sgn);
+  wmax = wide_int::from (wmax, val_prec, sgn);
+
+  bool known_true = false;
+  bool known_false = false;
+  switch (code)
+    {
+    case LT_EXPR:
+      known_true = wi::lt_p (wmax, wval, val_sgn);
+      known_false = wi::ge_p (wmin, wval, val_sgn);
+      break;
+    case LE_EXPR:
+      known_true = wi::le_p (wmax, wval, val_sgn);
+      known_false = wi::gt_p (wmin, wval, val_sgn);
+      break;
+    case GT_EXPR:
+      known_true = wi::gt_p (wmin, wval, val_sgn);
+      known_false = wi::le_p (wmax, wval, val_sgn);
+      break;
+    case GE_EXPR:
+      known_true = wi::ge_p (wmin, wval, val_sgn);
+      known_false = wi::lt_p (wmax, wval, val_sgn);
+      break;
+    default:
+      gcc_unreachable ();
+    }
+
+  if (known_true)
+    return constant_boolean_node (true, boolean_type_node);
+  if (known_false)
+    return constant_boolean_node (false, boolean_type_node);
+  return NULL_TREE;
+}
+
+/* Evaluate condition C (with c->unknown_offset set) against the argument
+   values in AVALS.  If the caller argument resolves to a STRING_CST, try to
+   fold the character comparison over all bytes of the string and return
+   boolean_true_node or boolean_false_node when the result is independent of
+   the offset, or NULL_TREE otherwise.  */
+
+static tree
+evaluate_unknown_offset_condition (struct condition *c,
+                                  ipa_auto_call_arg_values *avals)
+{
+  gcc_checking_assert (c->agg_contents && c->unknown_offset);
+
+  /* TODO: Support conversions in c->param_ops.  */
+  if (!vec_safe_is_empty (c->param_ops))
+    return NULL_TREE;
+
+  if (!c->by_ref || !c->val || TREE_CODE (c->val) != INTEGER_CST)
+    return NULL_TREE;
+
+  tree sval = avals->safe_sval_at (c->operand_num);
+  if (!sval || TREE_CODE (sval) != ADDR_EXPR)
+    return NULL_TREE;
+
+  poly_int64 byte_off;
+  tree base = get_addr_base_and_unit_offset (TREE_OPERAND (sval, 0),
+                                            &byte_off);
+  /* TODO: Support general constructors of readonly variables.  */
+  if (!base || TREE_CODE (base) != STRING_CST)
+    return NULL_TREE;
+
+  return fold_string_char_compare (base, c->code, c->val, c->type);
+}
+
 static void
 evaluate_conditions_for_known_args (struct cgraph_node *node,
                                    bool inline_p,
@@ -414,20 +727,38 @@ evaluate_conditions_for_known_args (struct cgraph_node 
*node,
        {
          if (c->code == ipa_predicate::changed
              && !c->by_ref
+             && !c->unknown_offset
              && (avals->safe_sval_at(c->operand_num) == error_mark_node))
            continue;
 
-         if (tree sval = avals->safe_sval_at (c->operand_num))
-           val = ipa_find_agg_cst_from_init (sval, c->offset, c->by_ref, 
c->type);
-         /* ipa_argagg_value_list is indexed by byte offsets, so a condition
-            which does not start at a byte boundary (a bit-field) cannot be
-            looked up in it; the containing byte would be reinterpreted as
-            the whole field below.  */
-         if (!val && (c->offset % BITS_PER_UNIT) == 0)
+         if (c->unknown_offset)
+           {
+             res = evaluate_unknown_offset_condition (c, avals);
+             if (res && integer_zerop (res))
+               continue;
+             if (res && integer_onep (res))
+               {
+                 clause |= 1 << (i + ipa_predicate::first_dynamic_condition);
+                 nonspec_clause
+                   |= 1 << (i + ipa_predicate::first_dynamic_condition);
+                 continue;
+               }
+           }
+         else
            {
-             ipa_argagg_value_list avs (avals);
-             val = avs.get_value (c->operand_num, c->offset / BITS_PER_UNIT,
-                                  c->by_ref, c->maybe_modified);
+             if (tree sval = avals->safe_sval_at (c->operand_num))
+               val = ipa_find_agg_cst_from_init (sval, c->offset, c->by_ref,
+                                                 c->type);
+             /* ipa_argagg_value_list is indexed by byte offsets, so a
+                condition which does not start at a byte boundary (a bit-field)
+                cannot be looked up in it; the containing byte would be
+                reinterpreted as the whole field below.  */
+             if (!val && (c->offset % BITS_PER_UNIT) == 0)
+               {
+                 ipa_argagg_value_list avs (avals);
+                 val = avs.get_value (c->operand_num, c->offset / 
BITS_PER_UNIT,
+                                      c->by_ref, c->maybe_modified);
+               }
            }
        }
       else
@@ -1315,9 +1646,11 @@ unmodified_parm_or_parm_agg_item (struct 
ipa_func_body_info *fbi,
       *index_p = ipa_get_param_decl_index (fbi->info, res);
       if (*index_p < 0)
        return false;
+      aggpos->offset = 0;
       aggpos->agg_contents = false;
       aggpos->by_ref = false;
       aggpos->maybe_modified = false;
+      aggpos->unknown_offset = false;
       return true;
     }
 
@@ -1338,10 +1671,11 @@ unmodified_parm_or_parm_agg_item (struct 
ipa_func_body_info *fbi,
   if (ipa_load_from_parm_agg (fbi, fbi->info->descriptors,
                              stmt, op, index_p, &aggpos->offset,
                              size_p, &aggpos->by_ref,
-                             &guaranteed_unmodified))
+                             &guaranteed_unmodified, &aggpos->unknown_offset))
     {
       aggpos->maybe_modified = !guaranteed_unmodified;
       gcc_checking_assert (!aggpos->maybe_modified || aggpos->by_ref);
+      gcc_checking_assert (!aggpos->unknown_offset || aggpos->offset == -1);
       return true;
     }
   return false;
@@ -1738,7 +2072,8 @@ set_cond_stmt_execution_predicate (struct 
ipa_func_body_info *fbi,
       || gimple_call_num_args (set_stmt) != 1)
     return;
   op2 = gimple_call_arg (set_stmt, 0);
-  if (!decompose_param_expr (fbi, set_stmt, op2, &index, &param_type, &aggpos))
+  if (!decompose_param_expr (fbi, set_stmt, op2, &index, &param_type, &aggpos)
+      || aggpos.unknown_offset)
     return;
   if (!aggpos.by_ref)
     add_builtin_constant_p_parm (summary, index);
@@ -2184,7 +2519,8 @@ will_be_nonconstant_predicate (struct ipa_func_body_info 
*fbi,
     {
       tree op = gimple_assign_rhs1 (stmt);
       if (!decompose_param_expr (fbi, stmt, op, &base_index, &param_type,
-                                &aggpos))
+                                &aggpos)
+         || aggpos.unknown_offset)
        return p;
     }
   else
@@ -4915,7 +5251,10 @@ inline_read_section (struct lto_file_decl_data 
*file_data, const char *data,
          c.agg_contents = bp_unpack_value (&bp, 1);
          c.by_ref = bp_unpack_value (&bp, 1);
          c.maybe_modified = bp_unpack_value (&bp, 1);
-         if (c.agg_contents)
+         c.unknown_offset = bp_unpack_value (&bp, 1);
+         if (c.unknown_offset)
+           c.offset = -1;
+         else if (c.agg_contents)
            c.offset = streamer_read_uhwi (&ib);
          else
            c.offset = 0;
@@ -5154,8 +5493,9 @@ ipa_fn_summary_write (void)
              bp_pack_value (&bp, c->agg_contents, 1);
              bp_pack_value (&bp, c->by_ref, 1);
              bp_pack_value (&bp, c->maybe_modified, 1);
+             bp_pack_value (&bp, c->unknown_offset, 1);
              streamer_write_bitpack (&bp);
-             if (c->agg_contents)
+             if (c->agg_contents && !c->unknown_offset)
                streamer_write_uhwi (ob, c->offset);
              streamer_write_uhwi (ob, vec_safe_length (c->param_ops));
              for (j = 0; vec_safe_iterate (c->param_ops, j, &op); j++)
@@ -5394,4 +5734,6 @@ ipa_fnsummary_cc_finalize (void)
 {
   ipa_free_fn_summary ();
   ipa_free_size_summary ();
+  delete string_char_set_cache;
+  string_char_set_cache = NULL;
 }
diff --git a/gcc/ipa-fnsummary.h b/gcc/ipa-fnsummary.h
index 59d1bd937b1b..0b7aaaa6d3cd 100644
--- a/gcc/ipa-fnsummary.h
+++ b/gcc/ipa-fnsummary.h
@@ -68,6 +68,7 @@ struct agg_position_info
   bool agg_contents;
   bool by_ref;
   bool maybe_modified;
+  bool unknown_offset;
 };
 
 /* Representation of function body size and time depending on the call
diff --git a/gcc/ipa-predicate.cc b/gcc/ipa-predicate.cc
index e07138958147..6a7ba6cb77a1 100644
--- a/gcc/ipa-predicate.cc
+++ b/gcc/ipa-predicate.cc
@@ -150,7 +150,8 @@ ipa_predicate::add_clause (conditions conditions, clause_t 
new_clause)
                  && cc2->agg_contents == cc1->agg_contents
                  && cc2->by_ref == cc1->by_ref
                  && (!cc1->agg_contents
-                     || (cc2->offset == cc1->offset
+                     || (cc1->unknown_offset == cc2->unknown_offset
+                         && (!cc1->unknown_offset || cc2->offset == 
cc1->offset)
                          && cc2->maybe_modified == cc1->maybe_modified))
                  && types_compatible_p (cc2->type, cc1->type)
                  && cc1->code == invert_tree_comparison (cc2->code,
@@ -297,6 +298,7 @@ ipa_predicate::probability (conditions conds,
                      &(*conds)[i2 - ipa_predicate::first_dynamic_condition];
                    if (c->code == ipa_predicate::changed
                        && !c->maybe_modified
+                       && !c->unknown_offset
                        && (c->operand_num <
                            (int) inline_param_summary.length ()))
                      {
@@ -334,10 +336,17 @@ dump_condition (FILE *f, conditions conditions, int cond)
       c = &(*conditions)[cond - ipa_predicate::first_dynamic_condition];
       fprintf (f, "op%i", c->operand_num);
       if (c->agg_contents)
-       fprintf (f, "[%s%soffset: " HOST_WIDE_INT_PRINT_DEC "]",
-                c->by_ref ? "ref " : "",
-                c->maybe_modified ? "maybe_modified " : "",
-                c->offset);
+       {
+         if (c->unknown_offset)
+           fprintf (f, "[%s%sunknown_offset]",
+                    c->by_ref ? "ref " : "",
+                    c->maybe_modified ? "maybe_modified " : "");
+         else
+           fprintf (f, "[%s%soffset: " HOST_WIDE_INT_PRINT_DEC "]",
+                    c->by_ref ? "ref " : "",
+                    c->maybe_modified ? "maybe_modified " : "",
+                    c->offset);
+       }
 
       for (unsigned i = 0; i < vec_safe_length (c->param_ops); i++)
        {
@@ -565,6 +574,7 @@ ipa_predicate::remap_after_inlining (class ipa_fn_summary 
*info,
                    || ((!c->agg_contents || !c->by_ref)
                        && offset_map[c->operand_num] > 0)
                    || (c->agg_contents
+                       && !c->unknown_offset
                        && !wi::fits_shwi_p (offset_int (c->offset)
                                             + offset_map[c->operand_num])))
                  cond_predicate = true;
@@ -575,13 +585,17 @@ ipa_predicate::remap_after_inlining (class ipa_fn_summary 
*info,
                    gcc_checking_assert (offset_delta >= 0);
                    gcc_assert (!c->agg_contents
                                || c->by_ref || offset_delta == 0);
-                   ap.offset = c->agg_contents ? c->offset + offset_delta : 0;
+                   if (c->unknown_offset)
+                     ap.offset = -1;
+                   else
+                     ap.offset = c->agg_contents ? c->offset + offset_delta : 
0;
                    ap.agg_contents = c->agg_contents;
                    ap.by_ref = c->by_ref;
                    ap.maybe_modified
                      = c->maybe_modified
                        || (c->agg_contents && c->by_ref
                            && !agg_preserved_map[c->operand_num]);
+                   ap.unknown_offset = c->unknown_offset;
                    cond_predicate = add_condition (info, params_summary,
                                                    operand_map[c->operand_num],
                                                    c->type, &ap, c->code,
@@ -655,7 +669,7 @@ add_condition (class ipa_fn_summary *summary,
   struct condition *c;
   struct condition new_cond;
   HOST_WIDE_INT offset;
-  bool agg_contents, by_ref, maybe_modified;
+  bool agg_contents, by_ref, maybe_modified, unknown_offset;
   expr_eval_op *op;
 
   if (params_summary)
@@ -663,10 +677,11 @@ add_condition (class ipa_fn_summary *summary,
 
   if (aggpos)
     {
-      offset = aggpos->offset;
       agg_contents = aggpos->agg_contents;
+      offset = agg_contents ? aggpos->offset : 0;
       by_ref = aggpos->by_ref;
       maybe_modified = aggpos->maybe_modified;
+      unknown_offset = aggpos->unknown_offset;
     }
   else
     {
@@ -674,10 +689,13 @@ add_condition (class ipa_fn_summary *summary,
       agg_contents = false;
       by_ref = false;
       maybe_modified = false;
+      unknown_offset = false;
     }
 
   gcc_checking_assert (operand_num >= 0);
   gcc_checking_assert (!maybe_modified || (agg_contents && by_ref));
+  gcc_checking_assert (!unknown_offset || (agg_contents && offset == -1));
+  gcc_checking_assert (!agg_contents || unknown_offset || offset >= 0);
   for (i = 0; vec_safe_iterate (summary->conds, i, &c); i++)
     {
       if (c->operand_num == operand_num
@@ -689,7 +707,8 @@ add_condition (class ipa_fn_summary *summary,
          && (!agg_contents
              || (c->offset == offset
                  && c->by_ref == by_ref
-                 && c->maybe_modified == maybe_modified)))
+                 && c->maybe_modified == maybe_modified
+                 && c->unknown_offset == unknown_offset)))
        return ipa_predicate::predicate_testing_cond (i);
     }
   /* Too many conditions.  Give up and return constant true.  */
@@ -703,6 +722,7 @@ add_condition (class ipa_fn_summary *summary,
   new_cond.agg_contents = agg_contents;
   new_cond.by_ref = by_ref;
   new_cond.maybe_modified = maybe_modified;
+  new_cond.unknown_offset = unknown_offset;
   new_cond.offset = offset;
   new_cond.param_ops = vec_safe_copy (param_ops);
 
diff --git a/gcc/ipa-predicate.h b/gcc/ipa-predicate.h
index 19e106dfea8a..26e1b8e73c1a 100644
--- a/gcc/ipa-predicate.h
+++ b/gcc/ipa-predicate.h
@@ -48,7 +48,7 @@ typedef vec<expr_eval_op, va_gc> *expr_eval_ops;
 struct GTY(()) condition
 {
   /* If agg_contents is set, this is the offset from which the used data was
-     loaded.  */
+     loaded, or -1 if unknown_offset is set.  */
   HOST_WIDE_INT offset;
   /* Type of the access reading the data (or the PARM_DECL SSA_NAME).  */
   tree type;
@@ -64,6 +64,8 @@ struct GTY(()) condition
   /* If by_ref is set, this is set when we cannot prove that the value
      pointed to is unmodified from the beginning of the function to use.  */
   unsigned maybe_modified : 1;
+  /* If agg_contents is set, this is set when the offset is unknown.  */
+  unsigned unknown_offset : 1;
   /* A set of sequential operations on the parameter, which can be seen as
      a mathematical function on the parameter.  */
   expr_eval_ops param_ops;
diff --git a/gcc/ipa-prop.cc b/gcc/ipa-prop.cc
index e4f2d676c5f0..28d58018fdfe 100644
--- a/gcc/ipa-prop.cc
+++ b/gcc/ipa-prop.cc
@@ -1277,6 +1277,94 @@ parm_ref_data_pass_through_p (struct ipa_func_body_info 
*fbi, int index,
   return !modified;
 }
 
+/* Fallback for ipa_load_from_parm_agg when OP is a 1-byte integral (char)
+   load whose byte offset is unknown (for example due to a variable ARRAY_REF
+   index or pointer arithmetic from a parameter).  Use get_base_address to
+   strip component references down to a MEM_REF and walk backwards through
+   pointer adjustments to locate the parameter; if found, record *OFFSET_P as
+   -1 and set *UNKNOWN_OFFSET_P to true.  */
+
+static bool
+ipa_load_from_parm_agg_unknown_offset (struct ipa_func_body_info *fbi,
+                                      vec<ipa_param_descriptor, va_gc>
+                                        *descriptors,
+                                      gimple *stmt, tree op, int *index_p,
+                                      HOST_WIDE_INT *offset_p,
+                                      poly_int64 *size_p, bool *by_ref_p,
+                                      bool *guaranteed_unmodified,
+                                      bool *unknown_offset_p)
+{
+  /* Currently we only work on STRING_CST when evaluating unknown-offset
+     conditions, so we do not record other cases (such as by-value
+     aggregates or general constructors).
+     TODO: Support general constructors.  */
+  tree type = TREE_TYPE (op);
+  if (TREE_CODE (type) != INTEGER_TYPE
+      || TYPE_PRECISION (type) != BITS_PER_UNIT)
+    return false;
+
+  tree base = get_base_address (op);
+  if (!base || TREE_CODE (base) != MEM_REF)
+    return false;
+
+  /* Strip pointer arithmetic and pointer conversions from the MEM_REF base
+     pointer (e.g. p[i] lowers to _2 = p_1(D) + _1; *_2).  */
+  tree ptr = TREE_OPERAND (base, 0);
+  int steps = 0;
+  while (TREE_CODE (ptr) == SSA_NAME && !SSA_NAME_IS_DEFAULT_DEF (ptr))
+    {
+      if (++steps > param_ipa_jump_function_lookups)
+       return false;
+
+      gimple *def = SSA_NAME_DEF_STMT (ptr);
+      if (is_gimple_assign (def)
+         && (gimple_assign_rhs_code (def) == POINTER_PLUS_EXPR
+             || gimple_assign_rhs_code (def) == SSA_NAME
+             || (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def))
+                 && POINTER_TYPE_P (TREE_TYPE (gimple_assign_rhs1 (def)))
+                 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (ptr)))
+                     == TYPE_ADDR_SPACE (TREE_TYPE
+                                         (TREE_TYPE
+                                          (gimple_assign_rhs1 (def))))))))
+       ptr = gimple_assign_rhs1 (def);
+      else
+       break;
+    }
+
+  if (TREE_CODE (ptr) != SSA_NAME)
+    return false;
+
+  int index = -1;
+  if (SSA_NAME_IS_DEFAULT_DEF (ptr))
+    {
+      tree parm = SSA_NAME_VAR (ptr);
+      if (parm && TREE_CODE (parm) == PARM_DECL)
+       index = ipa_get_param_decl_index_1 (descriptors, parm);
+    }
+  else
+    index = load_from_unmodified_param (fbi, descriptors,
+                                       SSA_NAME_DEF_STMT (ptr));
+
+  if (index < 0)
+    return false;
+
+  bool data_preserved = (gimple_vuse (stmt)
+                        ? parm_ref_data_preserved_p (fbi, index, stmt, op)
+                        : false);
+  if (!data_preserved && !guaranteed_unmodified)
+    return false;
+
+  *index_p = index;
+  *offset_p = -1;
+  *by_ref_p = true;
+  if (size_p)
+    *size_p = BITS_PER_UNIT;
+  if (guaranteed_unmodified)
+    *guaranteed_unmodified = data_preserved;
+  *unknown_offset_p = true;
+  return true;
+}
+
 /* Return true if we can prove that OP is a memory reference loading
    data from an aggregate passed as a parameter.
 
@@ -1286,6 +1374,10 @@ parm_ref_data_pass_through_p (struct ipa_func_body_info 
*fbi, int index,
    if it cannot prove the value has not been modified, in that case it will
    store false to *GUARANTEED_UNMODIFIED, otherwise it will store true there.
 
+   If UNKNOWN_OFFSET_P is non-NULL, also accept 1-byte integral (char) loads at
+   unknown (variable) offsets through a parameter pointer; in that case store
+   true to *UNKNOWN_OFFSET_P and -1 to *OFFSET_P.
+
    INFO and PARMS_AINFO describe parameters of the current function (but the
    latter can be NULL), STMT is the load statement.  If function returns true,
    *INDEX_P, *OFFSET_P and *BY_REF is filled with the parameter index, offset
@@ -1300,84 +1392,96 @@ ipa_load_from_parm_agg (struct ipa_func_body_info *fbi,
                        vec<ipa_param_descriptor, va_gc> *descriptors,
                        gimple *stmt, tree op, int *index_p,
                        HOST_WIDE_INT *offset_p, poly_int64 *size_p,
-                       bool *by_ref_p, bool *guaranteed_unmodified)
+                       bool *by_ref_p, bool *guaranteed_unmodified,
+                       bool *unknown_offset_p)
 {
   int index;
   HOST_WIDE_INT size;
   bool reverse;
-  tree base = get_ref_base_and_extent_hwi (op, offset_p, &size, &reverse);
 
-  if (!base
-      || (*offset_p / BITS_PER_UNIT) > UINT_MAX)
-    return false;
+  if (unknown_offset_p)
+    *unknown_offset_p = false;
 
   /* We can not propagate across volatile loads.  */
   if (TREE_THIS_VOLATILE (op))
     return false;
 
-  if (DECL_P (base))
+  tree base = get_ref_base_and_extent_hwi (op, offset_p, &size, &reverse);
+
+  if (base && (*offset_p / BITS_PER_UNIT) <= UINT_MAX)
     {
-      int index = ipa_get_param_decl_index_1 (descriptors, base);
-      if (index >= 0
-         && parm_preserved_before_stmt_p (fbi, index, stmt, op))
+      if (DECL_P (base))
        {
-         *index_p = index;
-         *by_ref_p = false;
-         if (size_p)
-           *size_p = size;
-         if (guaranteed_unmodified)
-           *guaranteed_unmodified = true;
-         return true;
+         int index = ipa_get_param_decl_index_1 (descriptors, base);
+         if (index >= 0
+             && parm_preserved_before_stmt_p (fbi, index, stmt, op))
+           {
+             *index_p = index;
+             *by_ref_p = false;
+             if (size_p)
+               *size_p = size;
+             if (guaranteed_unmodified)
+               *guaranteed_unmodified = true;
+             return true;
+           }
+         return false;
        }
-      return false;
-    }
-
-  if (TREE_CODE (base) != MEM_REF
-          || TREE_CODE (TREE_OPERAND (base, 0)) != SSA_NAME
-          || !integer_zerop (TREE_OPERAND (base, 1)))
-    return false;
-
-  if (SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (base, 0)))
-    {
-      tree parm = SSA_NAME_VAR (TREE_OPERAND (base, 0));
-      index = ipa_get_param_decl_index_1 (descriptors, parm);
-    }
-  else
-    {
-      /* This branch catches situations where a pointer parameter is not a
-        gimple register, for example:
 
-        void hip7(S*) (struct S * p)
-        {
-        void (*<T2e4>) (struct S *) D.1867;
-        struct S * p.1;
-
-        <bb 2>:
-        p.1_1 = p;
-        D.1867_2 = p.1_1->f;
-        D.1867_2 ();
-        gdp = &p;
-      */
+      if (TREE_CODE (base) == MEM_REF
+         && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME
+         && integer_zerop (TREE_OPERAND (base, 1)))
+       {
+         if (SSA_NAME_IS_DEFAULT_DEF (TREE_OPERAND (base, 0)))
+           {
+             tree parm = SSA_NAME_VAR (TREE_OPERAND (base, 0));
+             index = ipa_get_param_decl_index_1 (descriptors, parm);
+           }
+         else
+           {
+             /* This branch catches situations where a pointer parameter is
+                not a gimple register, for example:
+
+                void hip7(S*) (struct S * p)
+                {
+                void (*<T2e4>) (struct S *) D.1867;
+                struct S * p.1;
+
+                <bb 2>:
+                p.1_1 = p;
+                D.1867_2 = p.1_1->f;
+                D.1867_2 ();
+                gdp = &p;
+             */
+
+             gimple *def = SSA_NAME_DEF_STMT (TREE_OPERAND (base, 0));
+             index = load_from_unmodified_param (fbi, descriptors, def);
+           }
 
-      gimple *def = SSA_NAME_DEF_STMT (TREE_OPERAND (base, 0));
-      index = load_from_unmodified_param (fbi, descriptors, def);
+         if (index >= 0)
+           {
+             bool data_preserved = parm_ref_data_preserved_p (fbi, index, stmt,
+                                                              op);
+             if (!data_preserved && !guaranteed_unmodified)
+               return false;
+
+             *index_p = index;
+             *by_ref_p = true;
+             if (size_p)
+               *size_p = size;
+             if (guaranteed_unmodified)
+               *guaranteed_unmodified = data_preserved;
+             return true;
+           }
+       }
     }
 
-  if (index >= 0)
-    {
-      bool data_preserved = parm_ref_data_preserved_p (fbi, index, stmt, op);
-      if (!data_preserved && !guaranteed_unmodified)
-       return false;
+  if (!unknown_offset_p)
+    return false;
 
-      *index_p = index;
-      *by_ref_p = true;
-      if (size_p)
-       *size_p = size;
-      if (guaranteed_unmodified)
-       *guaranteed_unmodified = data_preserved;
-      return true;
-    }
-  return false;
+  return ipa_load_from_parm_agg_unknown_offset (fbi, descriptors, stmt, op,
+                                               index_p, offset_p, size_p,
+                                               by_ref_p, guaranteed_unmodified,
+                                               unknown_offset_p);
 }
 
 /* If STMT is an assignment that loads a value from a parameter declaration,
diff --git a/gcc/ipa-prop.h b/gcc/ipa-prop.h
index 176701f2614e..47b93404f9ef 100644
--- a/gcc/ipa-prop.h
+++ b/gcc/ipa-prop.h
@@ -1223,7 +1223,8 @@ bool ipa_load_from_parm_agg (struct ipa_func_body_info 
*fbi,
                             vec<ipa_param_descriptor, va_gc> *descriptors,
                             gimple *stmt, tree op, int *index_p,
                             HOST_WIDE_INT *offset_p, poly_int64 *size_p,
-                            bool *by_ref, bool *guaranteed_unmodified = NULL);
+                            bool *by_ref, bool *guaranteed_unmodified = NULL,
+                            bool *unknown_offset_p = NULL);
 
 /* Debugging interface.  */
 void ipa_print_node_params (FILE *, struct cgraph_node *node);
diff --git a/gcc/testsuite/gcc.dg/ipa/fnsummary-10.c 
b/gcc/testsuite/gcc.dg/ipa/fnsummary-10.c
new file mode 100644
index 000000000000..fc9117d19cfa
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/ipa/fnsummary-10.c
@@ -0,0 +1,263 @@
+/* { dg-do run } */
+/* { dg-options "-Os -fdump-tree-einline-details -fdump-tree-optimized" } */
+
+#define STR10 "abcdefghij"
+#define STR40 STR10 STR10 STR10 STR10
+#define STR160 STR40 STR40 STR40 STR40
+#define STR320 STR160 STR160
+
+#define A10 "aaaaaaaaaa"
+#define A50 A10 A10 A10 A10 A10
+#define A300 A50 A50 A50 A50 A50 A50
+
+#define Z10 "\0\0\0\0\0\0\0\0\0\0"
+#define Z50 Z10 Z10 Z10 Z10 Z10
+#define Z300 Z50 Z50 Z50 Z50 Z50 Z50
+
+#define NEG10 "\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89"
+#define NEG50 NEG10 NEG10 NEG10 NEG10 NEG10
+#define NEG300 NEG50 NEG50 NEG50 NEG50 NEG50 NEG50
+
+__attribute__((noipa)) void
+long_call (void)
+{
+  volatile int x = 1;
+  (void) x;
+}
+
+#define COLD_WORK()                                                    \
+  do                                                                   \
+    {                                                                  \
+      long_call ();                                                    \
+      long_call ();                                                    \
+      long_call ();                                                    \
+      long_call ();                                                    \
+      long_call ();                                                    \
+      long_call ();                                                    \
+      long_call ();                                                    \
+      long_call ();                                                    \
+      long_call ();                                                    \
+      long_call ();                                                    \
+      long_call ();                                                    \
+    }                                                                  \
+  while (0)
+
+int
+test_eq (const char *p, int i)
+{
+  if (p[i] == '%')
+    {
+      COLD_WORK ();
+      return 1;
+    }
+  return 0;
+}
+
+int
+test_eq_neg (const signed char *p, int i)
+{
+  if (p[i] == -50)
+    {
+      COLD_WORK ();
+      return 1;
+    }
+  return 0;
+}
+
+int
+test_ne_nul (const char *p, int i)
+{
+  if (p[i] != '\0')
+    {
+      COLD_WORK ();
+      return 1;
+    }
+  return 0;
+}
+
+int
+test_lt0 (const signed char *p, int i)
+{
+  if (p[i] < 0)
+    {
+      COLD_WORK ();
+      return 1;
+    }
+  return 0;
+}
+
+int
+test_gt0 (const signed char *p, int i)
+{
+  if (p[i] > 0)
+    {
+      COLD_WORK ();
+      return 1;
+    }
+  return 0;
+}
+
+int
+test_gt_j (const signed char *p, int i)
+{
+  if (p[i] > 'j')
+    {
+      COLD_WORK ();
+      return 1;
+    }
+  return 0;
+}
+
+int
+test_le_m1 (const signed char *p, int i)
+{
+  if (p[i] <= -1)
+    {
+      COLD_WORK ();
+      return 1;
+    }
+  return 0;
+}
+
+int
+test_ge130 (const unsigned char *p, int i)
+{
+  if (p[i] >= 130)
+    {
+      COLD_WORK ();
+      return 1;
+    }
+  return 0;
+}
+
+int
+test_ge200 (const unsigned char *p, int i)
+{
+  if (p[i] >= 200)
+    {
+      COLD_WORK ();
+      return 1;
+    }
+  return 0;
+}
+
+int
+test_else_ge0 (const signed char *p, int i)
+{
+  if (p[i] >= 0)
+    return 0;
+  else
+    {
+      COLD_WORK ();
+      return 1;
+    }
+}
+
+int
+test_else_le0 (const signed char *p, int i)
+{
+  if (p[i] <= 0)
+    return 0;
+  else
+    {
+      COLD_WORK ();
+      return 1;
+    }
+}
+
+int
+test_else_le_j (const signed char *p, int i)
+{
+  if (p[i] <= 'j')
+    return 0;
+  else
+    {
+      COLD_WORK ();
+      return 1;
+    }
+}
+
+int
+test_else_lt130 (const unsigned char *p, int i)
+{
+  if (p[i] < 130)
+    return 0;
+  else
+    {
+      COLD_WORK ();
+      return 1;
+    }
+}
+
+int
+main (void)
+{
+  volatile int i = 5;
+  volatile int i301 = 301;
+
+  if (test_eq (STR320, i) != 0)
+    __builtin_abort ();
+  if (test_eq_neg ((const signed char *) NEG300, i) != 0)
+    __builtin_abort ();
+  if (test_ne_nul (Z300, i) != 0)
+    __builtin_abort ();
+  if (test_lt0 ((const signed char *) STR320, i) != 0)
+    __builtin_abort ();
+  if (test_gt0 ((const signed char *) NEG300, i) != 0)
+    __builtin_abort ();
+  if (test_gt_j ((const signed char *) STR320, i) != 0)
+    __builtin_abort ();
+  if (test_le_m1 ((const signed char *) STR320, i) != 0)
+    __builtin_abort ();
+  if (test_ge130 ((const unsigned char *) STR320, i) != 0)
+    __builtin_abort ();
+  if (test_ge200 ((const unsigned char *) STR320, i) != 0)
+    __builtin_abort ();
+  if (test_else_ge0 ((const signed char *) STR320, i) != 0)
+    __builtin_abort ();
+  if (test_else_le0 ((const signed char *) NEG300, i) != 0)
+    __builtin_abort ();
+  if (test_else_le_j ((const signed char *) STR320, i) != 0)
+    __builtin_abort ();
+  if (test_else_lt130 ((const unsigned char *) STR320, i) != 0)
+    __builtin_abort ();
+
+  /* Long string (> 256 chars) with embedded NUL followed by '%'.  */
+  if (test_eq (A300 "\0%bcdef", i301) != 1)
+    __builtin_abort ();
+
+  return 0;
+}
+
+/* { dg-final { scan-tree-dump "Known to be false:.*op0\\\[ref 
unknown_offset\\\] == 37" "einline" } } */
+/* { dg-final { scan-tree-dump "Known to be false:.*op0\\\[ref 
unknown_offset\\\] == -50" "einline" } } */
+/* { dg-final { scan-tree-dump "Known to be false:.*op0\\\[ref 
unknown_offset\\\] != 0" "einline" } } */
+/* { dg-final { scan-tree-dump "Known to be false:.*op0\\\[ref 
unknown_offset\\\] (?:< 0|<= -1)" "einline" } } */
+/* { dg-final { scan-tree-dump "Known to be false:.*op0\\\[ref 
unknown_offset\\\] (?:> 0|>= 1)" "einline" } } */
+/* { dg-final { scan-tree-dump "Known to be false:.*op0\\\[ref 
unknown_offset\\\] (?:> 106|>= 107)" "einline" } } */
+/* { dg-final { scan-tree-dump "Known to be false:.*op0\\\[ref 
unknown_offset\\\] (?:> 199|>= 200)" "einline" } } */
+/* { dg-final { scan-tree-dump-times "optimized:  Inlining test_eq/" 1 
"einline" } } */
+/* { dg-final { scan-tree-dump "optimized:  Inlining test_eq_neg/" "einline" } 
} */
+/* { dg-final { scan-tree-dump "optimized:  Inlining test_ne_nul/" "einline" } 
} */
+/* { dg-final { scan-tree-dump "optimized:  Inlining test_lt0/" "einline" } } 
*/
+/* { dg-final { scan-tree-dump "optimized:  Inlining test_gt0/" "einline" } } 
*/
+/* { dg-final { scan-tree-dump "optimized:  Inlining test_gt_j/" "einline" } } 
*/
+/* { dg-final { scan-tree-dump "optimized:  Inlining test_le_m1/" "einline" } 
} */
+/* { dg-final { scan-tree-dump "optimized:  Inlining test_ge130/" "einline" } 
} */
+/* { dg-final { scan-tree-dump "optimized:  Inlining test_ge200/" "einline" } 
} */
+/* { dg-final { scan-tree-dump "optimized:  Inlining test_else_ge0/" "einline" 
} } */
+/* { dg-final { scan-tree-dump "optimized:  Inlining test_else_le0/" "einline" 
} } */
+/* { dg-final { scan-tree-dump "optimized:  Inlining test_else_le_j/" 
"einline" } } */
+/* { dg-final { scan-tree-dump "optimized:  Inlining test_else_lt130/" 
"einline" } } */
+/* { dg-final { scan-tree-dump-not "  test_eq_neg \\(" "optimized" } } */
+/* { dg-final { scan-tree-dump-not "  test_ne_nul \\(" "optimized" } } */
+/* { dg-final { scan-tree-dump-not "  test_lt0 \\(" "optimized" } } */
+/* { dg-final { scan-tree-dump-not "  test_gt0 \\(" "optimized" } } */
+/* { dg-final { scan-tree-dump-not "  test_gt_j \\(" "optimized" } } */
+/* { dg-final { scan-tree-dump-not "  test_le_m1 \\(" "optimized" } } */
+/* { dg-final { scan-tree-dump-not "  test_ge130 \\(" "optimized" } } */
+/* { dg-final { scan-tree-dump-not "  test_ge200 \\(" "optimized" } } */
+/* { dg-final { scan-tree-dump-not "  test_else_ge0 \\(" "optimized" } } */
+/* { dg-final { scan-tree-dump-not "  test_else_le0 \\(" "optimized" } } */
+/* { dg-final { scan-tree-dump-not "  test_else_le_j \\(" "optimized" } } */
+/* { dg-final { scan-tree-dump-not "  test_else_lt130 \\(" "optimized" } } */
diff --git a/gcc/testsuite/gcc.dg/ipa/fnsummary-7.c 
b/gcc/testsuite/gcc.dg/ipa/fnsummary-7.c
new file mode 100644
index 000000000000..c8b55de84bdd
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/ipa/fnsummary-7.c
@@ -0,0 +1,37 @@
+/* { dg-do compile } */
+/* { dg-options "-Os -fdump-tree-einline-details -fdump-tree-optimized" } */
+
+void long_call (void);
+
+int
+test (const char *p, int i)
+{
+  if (p[i] == '%')
+    {
+      long_call ();
+      long_call ();
+      long_call ();
+      long_call ();
+      long_call ();
+      long_call ();
+      long_call ();
+      long_call ();
+      long_call ();
+      long_call ();
+      long_call ();
+      return 1;
+    }
+  return 0;
+}
+
+int
+main (void)
+{
+  volatile int i = 0;
+  return test ("test", i) + test ("xtest" + 1, i);
+}
+
+/* { dg-final { scan-tree-dump "Known to be false:.*op0\\\[ref 
unknown_offset\\\] == 37" "einline" } } */
+/* { dg-final { scan-tree-dump-times "Inlining test/" 2 "einline" } } */
+/* { dg-final { scan-tree-dump-not "  test \\(" "optimized" } } */
+
diff --git a/gcc/testsuite/gcc.dg/ipa/fnsummary-8.c 
b/gcc/testsuite/gcc.dg/ipa/fnsummary-8.c
new file mode 100644
index 000000000000..e04eb31a5446
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/ipa/fnsummary-8.c
@@ -0,0 +1,125 @@
+/* { dg-do run } */
+/* { dg-require-effective-target lto } */
+/* { dg-options "-O2 -flto -fno-partial-inlining -fno-ipa-sra 
-fno-early-inlining -fdump-ipa-inline-details" } */
+
+#define COLD_WORK() \
+  do { cold_heavy (); cold_heavy (); cold_heavy (); cold_heavy (); } while (0)
+
+struct Wrap
+{
+  int pad;
+  char buf[8];
+};
+
+char gbuf[16] = "hello";
+
+__attribute__((noipa)) void
+clobber (void)
+{
+  gbuf[3] = '%';
+}
+
+__attribute__((noipa)) void
+cold_heavy (void)
+{
+  volatile int x = 1;
+  (void) x;
+}
+
+static int
+leaf_clean (const char *p, int i)
+{
+  if (p[i] == '%')
+    {
+      COLD_WORK ();
+      return 1;
+    }
+  return 0;
+}
+
+static int
+leaf_idx (const char *p, int i)
+{
+  clobber ();
+  if (p[i] == '%')
+    {
+      COLD_WORK ();
+      return 1;
+    }
+  return 0;
+}
+
+static int
+leaf_ne (const char *p, int i)
+{
+  if (p[i] != '\0')
+    {
+      COLD_WORK ();
+      return 1;
+    }
+  return 0;
+}
+
+static inline int
+leaf_anc (const char *p, int i)
+{
+  if (p[i] == '%')
+    {
+      COLD_WORK ();
+      return 1;
+    }
+  return 0;
+}
+
+static int
+mid_anc (struct Wrap *w, int i)
+{
+  clobber ();
+  return leaf_anc (w->buf, i);
+}
+
+static int
+mid_pass (const char *p, int i)
+{
+  clobber ();
+  return leaf_anc (p, i);
+}
+
+static const struct Wrap w_ro = { 1, "test" };
+
+int
+main (void)
+{
+  volatile int i = 0;
+  volatile int i3 = 3;
+  struct Wrap w_rw = { 1, "test" };
+
+  if (leaf_clean ("hello world", i) != 0)
+    __builtin_abort ();
+  if (leaf_clean ("foo%bar", i3) != 1)
+    __builtin_abort ();
+  if (leaf_idx ("abcdef", i) != 0)
+    __builtin_abort ();
+  if (leaf_idx ("ab\0%cd", i3) != 1)
+    __builtin_abort ();
+  gbuf[3] = 'x';
+  if (leaf_idx (gbuf, i3) != 1)
+    __builtin_abort ();
+  if (leaf_ne ("", i) != 0)
+    __builtin_abort ();
+  if (leaf_ne ("x", i) != 1)
+    __builtin_abort ();
+  if (mid_anc ((struct Wrap *) &w_ro, i) != 0)
+    __builtin_abort ();
+  if (mid_anc (&w_rw, i) != 0)
+    __builtin_abort ();
+  if (mid_pass ("abcdef", i) != 0)
+    __builtin_abort ();
+  if (mid_pass ("ab%def", 2) != 1)
+    __builtin_abort ();
+  return 0;
+}
+
+/* { dg-final { scan-wpa-ipa-dump "Known to be false:.*op0\\\[ref 
unknown_offset\\\] == 37" "inline" } } */
+/* { dg-final { scan-wpa-ipa-dump "Known to be false:.*op0\\\[ref 
maybe_modified unknown_offset\\\] == 37" "inline" } } */
+/* { dg-final { scan-wpa-ipa-dump "Known to be false:.*op0\\\[ref 
unknown_offset\\\] != 0" "inline" } } */
diff --git a/gcc/testsuite/gcc.dg/ipa/fnsummary-9.c 
b/gcc/testsuite/gcc.dg/ipa/fnsummary-9.c
new file mode 100644
index 000000000000..4eb112d2a070
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/ipa/fnsummary-9.c
@@ -0,0 +1,347 @@
+/* { dg-do run } */
+/* { dg-options "-Os -fno-early-inlining -fno-ipa-cp -fno-partial-inlining 
-fno-ipa-sra -fno-ipa-icf -fdump-ipa-inline-details" } */
+
+#define COLD_WORK()                                                    \
+  do                                                                   \
+    {                                                                  \
+      cold_heavy ();                                                   \
+      cold_heavy ();                                                   \
+      cold_heavy ();                                                   \
+      cold_heavy ();                                                   \
+      cold_heavy ();                                                   \
+      cold_heavy ();                                                   \
+      cold_heavy ();                                                   \
+      cold_heavy ();                                                   \
+    }                                                                  \
+  while (0)
+
+__attribute__((noipa)) void
+cold_heavy (void)
+{
+  volatile int x = 1;
+  (void) x;
+}
+
+int
+chk_eq_pct (const char *p, int i)
+{
+  if (p[i] == '%')
+    {
+      COLD_WORK ();
+      return 1;
+    }
+  return 0;
+}
+
+int
+chk_eq_a (const char *p, int i)
+{
+  if (p[i] == 'a')
+    {
+      COLD_WORK ();
+      return 1;
+    }
+  return 0;
+}
+
+int
+chk_ne_nul (const char *p, int i)
+{
+  if (p[i] != '\0')
+    {
+      COLD_WORK ();
+      return 1;
+    }
+  return 0;
+}
+
+int
+chk_s_lt0 (const signed char *p, int i)
+{
+  if (p[i] < 0)
+    {
+      COLD_WORK ();
+      return 1;
+    }
+  return 0;
+}
+
+int
+chk_s_gt0 (const signed char *p, int i)
+{
+  if (p[i] > 0)
+    {
+      COLD_WORK ();
+      return 1;
+    }
+  return 0;
+}
+
+int
+chk_s_gt_f (const signed char *p, int i)
+{
+  if (p[i] > 'f')
+    {
+      COLD_WORK ();
+      return 1;
+    }
+  return 0;
+}
+
+int
+chk_s_le_m1 (const signed char *p, int i)
+{
+  if (p[i] <= -1)
+    {
+      COLD_WORK ();
+      return 1;
+    }
+  return 0;
+}
+
+int
+chk_u_gt_f (const unsigned char *p, int i)
+{
+  if (p[i] > 'f')
+    {
+      COLD_WORK ();
+      return 1;
+    }
+  return 0;
+}
+
+int
+chk_u_gt120 (const unsigned char *p, int i)
+{
+  if (p[i] > 120)
+    {
+      COLD_WORK ();
+      return 1;
+    }
+  return 0;
+}
+
+int
+chk_u_ge130 (const unsigned char *p, int i)
+{
+  if (p[i] >= 130)
+    {
+      COLD_WORK ();
+      return 1;
+    }
+  return 0;
+}
+
+int
+chk_u_ge200 (const unsigned char *p, int i)
+{
+  if (p[i] >= 200)
+    {
+      COLD_WORK ();
+      return 1;
+    }
+  return 0;
+}
+
+int
+chk_u_ge128_cast (const unsigned char *p, int i)
+{
+  if (p[i] >= 128)
+    {
+      COLD_WORK ();
+      return 1;
+    }
+  return 0;
+}
+
+int
+chk_else_ge0 (const signed char *p, int i)
+{
+  if (p[i] >= 0)
+    return 0;
+  else
+    {
+      COLD_WORK ();
+      return 1;
+    }
+}
+
+int
+chk_else_s_le0 (const signed char *p, int i)
+{
+  if (p[i] <= 0)
+    return 0;
+  else
+    {
+      COLD_WORK ();
+      return 1;
+    }
+}
+
+int
+chk_else_u_le_f (const unsigned char *p, int i)
+{
+  if (p[i] <= 'f')
+    return 0;
+  else
+    {
+      COLD_WORK ();
+      return 1;
+    }
+}
+
+int
+chk_arr_ptr (const char (*p)[7], int i)
+{
+  if ((*p)[i] == '%')
+    {
+      COLD_WORK ();
+      return 1;
+    }
+  return 0;
+}
+
+int
+chk_switch (const char *p, int i)
+{
+  switch (p[i])
+    {
+    case '%':
+    case '(':
+    case '*':
+    case '/':
+      COLD_WORK ();
+      return 1;
+    default:
+      return 0;
+    }
+}
+
+int
+chk_signed_to_uint (const signed char *p, int i)
+{
+  if ((unsigned int) p[i] > 127u)
+    {
+      COLD_WORK ();
+      return 1;
+    }
+  return 0;
+}
+
+int
+main (void)
+{
+  volatile int i = 0;
+  volatile int im1 = -1;
+  volatile int i2 = 2;
+  volatile int i3 = 3;
+
+  /* Valid foldable EQ_EXPR cases (3 calls inlined).  */
+  if (chk_eq_pct ("abcdef", i) != 0)
+    __builtin_abort ();
+  if (chk_eq_pct ("xyabcdef" + 2, i) != 0)
+    __builtin_abort ();
+  if (chk_eq_pct ((const char *) u"abcdef", i) != 0)
+    __builtin_abort ();
+
+  /* Runtime safety checks for EQ_EXPR (must not fold away '%').  */
+  if (chk_eq_pct ("x%abcdef" + 2, im1) != 1)
+    __builtin_abort ();
+  if (chk_eq_pct ("ab\0%def", i3) != 1)
+    __builtin_abort ();
+
+  /* EQ_EXPR missing vs present character.  */
+  if (chk_eq_a ("bcdef", i) != 0)
+    __builtin_abort ();
+  if (chk_eq_a ("abcdef", i) != 1)
+    __builtin_abort ();
+
+  /* NE_EXPR on empty and all-NUL string literals.  */
+  if (chk_ne_nul ("", i) != 0)
+    __builtin_abort ();
+  if (chk_ne_nul ("\0\0\0", i) != 0)
+    __builtin_abort ();
+  if (chk_ne_nul ("x", i) != 1)
+    __builtin_abort ();
+
+  /* Signed LT_EXPR, GT_EXPR, LE_EXPR.  */
+  if (chk_s_lt0 ((const signed char *) "abcdef", i) != 0)
+    __builtin_abort ();
+  if (chk_s_lt0 ((const signed char *) "\xff\x01\x02", i) != 1)
+    __builtin_abort ();
+
+  if (chk_s_gt0 ((const signed char *) "\x80\x90", i) != 0)
+    __builtin_abort ();
+  if (chk_s_gt0 ((const signed char *) "\x01\x02", i) != 1)
+    __builtin_abort ();
+
+  if (chk_s_gt_f ((const signed char *) "abcdef", i) != 0)
+    __builtin_abort ();
+
+  if (chk_s_le_m1 ((const signed char *) "abcdef", i) != 0)
+    __builtin_abort ();
+  if (chk_s_le_m1 ((const signed char *) "\x80\x90", i) != 1)
+    __builtin_abort ();
+
+  /* Unsigned GT_EXPR and GE_EXPR (including wide UTF-16 string bytes).  */
+  if (chk_u_gt_f ((const unsigned char *) "abcdef", i) != 0)
+    __builtin_abort ();
+  if (chk_u_gt120 ((const unsigned char *) "abcdef", i) != 0)
+    __builtin_abort ();
+  if (chk_u_ge130 ((const unsigned char *) "abcdef", i) != 0)
+    __builtin_abort ();
+  if (chk_u_ge130 ((const unsigned char *) u"abcdef", i) != 0)
+    __builtin_abort ();
+  if (chk_u_ge200 ((const unsigned char *) "abcdef", i) != 0)
+    __builtin_abort ();
+  if (chk_u_ge200 ((const unsigned char *) u"abcdef", i) != 0)
+    __builtin_abort ();
+
+  /* >= 128 is canonicalized to ((signed char) #) < 0 in c->param_ops and
+     must not fold yet.  */
+  if (chk_u_ge128_cast ((const unsigned char *) "abcdef", i) != 0)
+    __builtin_abort ();
+
+  /* Else-branch folding (GE_EXPR, LE_EXPR true -> inverted comparison false). 
 */
+  if (chk_else_ge0 ((const signed char *) "abcdef", i) != 0)
+    __builtin_abort ();
+  if (chk_else_s_le0 ((const signed char *) "\x80\x90", i) != 0)
+    __builtin_abort ();
+  if (chk_else_u_le_f ((const unsigned char *) "abcdef", i) != 0)
+    __builtin_abort ();
+
+  /* Variable-index ARRAY_REF on pointer to array and switch predicate.  */
+  if (chk_arr_ptr (&"abcdef", i) != 0)
+    __builtin_abort ();
+  if (chk_switch ("abcdef", i) != 0)
+    __builtin_abort ();
+  if (chk_switch ("ab%def", i2) != 1)
+    __builtin_abort ();
+
+  return 0;
+}
+
+/* { dg-final { scan-ipa-dump "Known to be false:.*unknown_offset\\\] == 37" 
"inline" } } */
+/* { dg-final { scan-ipa-dump "Known to be false:.*unknown_offset\\\] == 97" 
"inline" } } */
+/* { dg-final { scan-ipa-dump "Known to be false:.*unknown_offset\\\] != 0" 
"inline" } } */
+/* { dg-final { scan-ipa-dump "Known to be false:.*unknown_offset\\\] (?:< 
0|<= -1)" "inline" } } */
+/* { dg-final { scan-ipa-dump "Known to be false:.*unknown_offset\\\] (?:> 
0|>= 1)" "inline" } } */
+/* { dg-final { scan-ipa-dump "Known to be false:.*unknown_offset\\\] (?:> 
102|>= 103)" "inline" } } */
+/* { dg-final { scan-ipa-dump "Known to be false:.*unknown_offset\\\] (?:> 
199|>= 200)" "inline" } } */
+/* { dg-final { scan-ipa-dump-times "Inlined chk_eq_pct/" 3 "inline" } } */
+/* { dg-final { scan-ipa-dump-times "Inlined chk_eq_a/" 1 "inline" } } */
+/* { dg-final { scan-ipa-dump-times "Inlined chk_ne_nul/" 2 "inline" } } */
+/* { dg-final { scan-ipa-dump-times "Inlined chk_s_lt0/" 1 "inline" } } */
+/* { dg-final { scan-ipa-dump-times "Inlined chk_s_gt0/" 1 "inline" } } */
+/* { dg-final { scan-ipa-dump-times "Inlined chk_s_gt_f/" 1 "inline" } } */
+/* { dg-final { scan-ipa-dump-times "Inlined chk_s_le_m1/" 1 "inline" } } */
+/* { dg-final { scan-ipa-dump-times "Inlined chk_u_gt_f/" 1 "inline" } } */
+/* { dg-final { scan-ipa-dump-times "Inlined chk_u_gt120/" 1 "inline" } } */
+/* { dg-final { scan-ipa-dump-times "Inlined chk_u_ge130/" 2 "inline" } } */
+/* { dg-final { scan-ipa-dump-times "Inlined chk_u_ge200/" 2 "inline" } } */
+/* { dg-final { scan-ipa-dump-times "Inlined chk_else_ge0/" 1 "inline" } } */
+/* { dg-final { scan-ipa-dump-times "Inlined chk_else_s_le0/" 1 "inline" } } */
+/* { dg-final { scan-ipa-dump-times "Inlined chk_else_u_le_f/" 1 "inline" } } 
*/
+/* { dg-final { scan-ipa-dump-times "Inlined chk_arr_ptr/" 1 "inline" } } */
+/* { dg-final { scan-ipa-dump "Known to be false:.*unknown_offset\\\] == 40" 
"inline" } } */
+/* { dg-final { scan-ipa-dump-not "Inlined chk_u_ge128_cast/" "inline" } } */

Reply via email to