https://gcc.gnu.org/g:90fe402a89910d4fbe0efc15cf05cf6c0306586a

commit r15-2648-g90fe402a89910d4fbe0efc15cf05cf6c0306586a
Author: Jakub Jelinek <ja...@redhat.com>
Date:   Thu Aug 1 20:23:15 2024 +0200

    fortran: Fix up pasto in gfc_get_array_descr_info
    
    A static analyzer found a pasto in gfc_get_array_descr_info.
    The code does
          t = base_decl;
          if (!integer_zerop (dtype_off))
            t = fold_build_pointer_plus (t, dtype_off);
          dtype = TYPE_MAIN_VARIANT (get_dtype_type_node ());
          field = gfc_advance_chain (TYPE_FIELDS (dtype), GFC_DTYPE_RANK);
          rank_off = byte_position (field);
          if (!integer_zerop (dtype_off))
            t = fold_build_pointer_plus (t, rank_off);
    i.e. uses the same !integer_zerop check between both, while it should
    be checking rank_off in the latter case.
    This actually doesn't change anything on the generated code, because
    both the dtype_off and rank_off aren't zero,
    typedef struct dtype_type
    {
      size_t elem_len;
      int version;
      signed char rank;
      signed char type;
      signed short attribute;
    }
    dtype_type;
    struct {
      type *base_addr;
      size_t offset;
      dtype_type dtype;
      index_type span;
      descriptor_dimension dim[];
    };
    dtype_off is 16 on 64-bit arches and 8 on 32-bit ones and rank_off is
    12 on 64-bit arches and 8 on 32-bit arches, so this patch is just to
    pacify those static analyzers or be prepared if the ABI changes in the
    future.  Because in the current ABI both of those are actually non-zero,
    doing
    if (!integer_zerop (something)) t = fold_build_pointer_plus (t, something);
    actually isn't an optimization, it will consume more compile time.  If
    the ABI changes and we forget to readd it, nothing bad happens,
    fold_build_pointer_plus handles 0 addends fine, just takes some compile
    time to handle that.
    I've kept this if (!integer_zerop (data_off)) guard earlier because
    data_off is 0 in the current ABI, so it is an optimization there.
    
    2024-08-01  Jakub Jelinek  <ja...@redhat.com>
    
            * trans-types.cc (gfc_get_array_descr_info): Don't test if
            !integer_zerop (dtype_off), use fold_build_pointer_plus
            unconditionally.

Diff:
---
 gcc/fortran/trans-types.cc | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/gcc/fortran/trans-types.cc b/gcc/fortran/trans-types.cc
index 59d72136a0de..e6da8e1a58b7 100644
--- a/gcc/fortran/trans-types.cc
+++ b/gcc/fortran/trans-types.cc
@@ -3599,14 +3599,11 @@ gfc_get_array_descr_info (const_tree type, struct 
array_descr_info *info)
     {
       rank = 1;
       info->ndimensions = 1;
-      t = base_decl;
-      if (!integer_zerop (dtype_off))
-       t = fold_build_pointer_plus (t, dtype_off);
+      t = fold_build_pointer_plus (base_decl, dtype_off);
       dtype = TYPE_MAIN_VARIANT (get_dtype_type_node ());
       field = gfc_advance_chain (TYPE_FIELDS (dtype), GFC_DTYPE_RANK);
       rank_off = byte_position (field);
-      if (!integer_zerop (dtype_off))
-       t = fold_build_pointer_plus (t, rank_off);
+      t = fold_build_pointer_plus (t, rank_off);
 
       t = build1 (NOP_EXPR, build_pointer_type (TREE_TYPE (field)), t);
       t = build1 (INDIRECT_REF, TREE_TYPE (field), t);

Reply via email to