]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - gcc/fold-const-call.c
Correct a function pre/postcondition [PR102403].
[thirdparty/gcc.git] / gcc / fold-const-call.c
index c4f52fb3c1576c3f205326c1024f4579a4ac5378..d6cb9b11a318fd2ea71868a62fc2004687d6e047 100644 (file)
@@ -1,5 +1,5 @@
 /* Constant folding for calls to built-in and internal functions.
-   Copyright (C) 1988-2019 Free Software Foundation, Inc.
+   Copyright (C) 1988-2021 Free Software Foundation, Inc.
 
 This file is part of GCC.
 
@@ -53,16 +53,15 @@ complex_cst_p (tree t)
   return TREE_CODE (t) == COMPLEX_CST;
 }
 
-/* Return true if ARG is a constant in the range of the host size_t.
+/* Return true if ARG is a size_type_node constant.
    Store it in *SIZE_OUT if so.  */
 
 static inline bool
-host_size_t_cst_p (tree t, size_t *size_out)
+size_t_cst_p (tree t, unsigned HOST_WIDE_INT *size_out)
 {
   if (types_compatible_p (size_type_node, TREE_TYPE (t))
       && integer_cst_p (t)
-      && (wi::min_precision (wi::to_wide (t), UNSIGNED)
-         <= sizeof (size_t) * CHAR_BIT))
+      && tree_fits_uhwi_p (t))
     {
       *size_out = tree_to_uhwi (t);
       return true;
@@ -1033,6 +1032,7 @@ fold_const_call_ss (wide_int *result, combined_fn fn, const wide_int_ref &arg,
     case CFN_BUILT_IN_BSWAP16:
     case CFN_BUILT_IN_BSWAP32:
     case CFN_BUILT_IN_BSWAP64:
+    case CFN_BUILT_IN_BSWAP128:
       *result = wide_int::from (arg, precision, TYPE_SIGN (arg_type)).bswap ();
       return true;
 
@@ -1299,6 +1299,9 @@ fold_const_call (combined_fn fn, tree type, tree arg)
 
     CASE_CFN_NANS:
     CASE_FLT_FN_FLOATN_NX (CFN_BUILT_IN_NANS):
+    case CFN_BUILT_IN_NANSD32:
+    case CFN_BUILT_IN_NANSD64:
+    case CFN_BUILT_IN_NANSD128:
       return fold_const_builtin_nan (type, arg, false);
 
     case CFN_REDUC_PLUS:
@@ -1372,6 +1375,9 @@ fold_const_call_sss (real_value *result, combined_fn fn,
     CASE_CFN_FDIM:
       return do_mpfr_arg2 (result, mpfr_dim, arg0, arg1, format);
 
+    CASE_CFN_FMOD:
+      return do_mpfr_arg2 (result, mpfr_fmod, arg0, arg1, format);
+
     CASE_CFN_HYPOT:
       return do_mpfr_arg2 (result, mpfr_hypot, arg0, arg1, format);
 
@@ -1763,23 +1769,22 @@ fold_const_call (combined_fn fn, tree type, tree arg0, tree arg1, tree arg2)
 {
   const char *p0, *p1;
   char c;
-  unsigned HOST_WIDE_INT s0, s1;
-  size_t s2 = 0;
+  unsigned HOST_WIDE_INT s0, s1, s2 = 0;
   switch (fn)
     {
     case CFN_BUILT_IN_STRNCMP:
-      if (!host_size_t_cst_p (arg2, &s2))
+      if (!size_t_cst_p (arg2, &s2))
        return NULL_TREE;
       if (s2 == 0
          && !TREE_SIDE_EFFECTS (arg0)
          && !TREE_SIDE_EFFECTS (arg1))
        return build_int_cst (type, 0);
       else if ((p0 = c_getstr (arg0)) && (p1 = c_getstr (arg1)))
-       return build_int_cst (type, strncmp (p0, p1, s2));
+       return build_int_cst (type, strncmp (p0, p1, MIN (s2, SIZE_MAX)));
       return NULL_TREE;
 
     case CFN_BUILT_IN_STRNCASECMP:
-      if (!host_size_t_cst_p (arg2, &s2))
+      if (!size_t_cst_p (arg2, &s2))
        return NULL_TREE;
       if (s2 == 0
          && !TREE_SIDE_EFFECTS (arg0)
@@ -1787,33 +1792,33 @@ fold_const_call (combined_fn fn, tree type, tree arg0, tree arg1, tree arg2)
        return build_int_cst (type, 0);
       else if ((p0 = c_getstr (arg0))
               && (p1 = c_getstr (arg1))
-              && strncmp (p0, p1, s2) == 0)
+              && strncmp (p0, p1, MIN (s2, SIZE_MAX)) == 0)
        return build_int_cst (type, 0);
       return NULL_TREE;
 
     case CFN_BUILT_IN_BCMP:
     case CFN_BUILT_IN_MEMCMP:
-      if (!host_size_t_cst_p (arg2, &s2))
+      if (!size_t_cst_p (arg2, &s2))
        return NULL_TREE;
       if (s2 == 0
          && !TREE_SIDE_EFFECTS (arg0)
          && !TREE_SIDE_EFFECTS (arg1))
        return build_int_cst (type, 0);
-      if ((p0 = c_getstr (arg0, &s0))
-         && (p1 = c_getstr (arg1, &s1))
+      if ((p0 = getbyterep (arg0, &s0))
+         && (p1 = getbyterep (arg1, &s1))
          && s2 <= s0
          && s2 <= s1)
        return build_cmp_result (type, memcmp (p0, p1, s2));
       return NULL_TREE;
 
     case CFN_BUILT_IN_MEMCHR:
-      if (!host_size_t_cst_p (arg2, &s2))
+      if (!size_t_cst_p (arg2, &s2))
        return NULL_TREE;
       if (s2 == 0
          && !TREE_SIDE_EFFECTS (arg0)
          && !TREE_SIDE_EFFECTS (arg1))
        return build_int_cst (type, 0);
-      if ((p0 = c_getstr (arg0, &s0))
+      if ((p0 = getbyterep (arg0, &s0))
          && s2 <= s0
          && target_char_cst_p (arg1, &c))
        {