Re: [Patch, fortran] PR79685 - [12/13/14/15 Regression] ICE on valid code in gfc_match_structure_constructor

2024-07-28 Thread Paul Richard Thomas
I have attached the updated and rather simpler patch.

Thanks for the prod, Mikael!

Paul

Fortran: Fix ICE with structure constructor in data statement [PR79685]

2024-07-28  Paul Thomas  

gcc/fortran
PR fortran/79685
* decl.cc (match_data_constant): Find the symtree instead of
the symbol so the use renamed symbols are found. Pass this and
the derived type to gfc_match_structure_constructor.
* match.h: Update prototype of gfc_match_structure_contructor.
* primary.cc (gfc_match_structure_constructor): Remove call to
gfc_get_ha_sym_tree and use caller supplied symtree instead.

gcc/testsuite/
PR fortran/79685
* gfortran.dg/use_rename_12.f90: New test.


On Sat, 27 Jul 2024 at 22:04, Paul Richard Thomas <
paul.richard.tho...@gmail.com> wrote:

> Hi Mikael,
>
> You were absolutely right. I looked at the caller and "just didn't get
> it". Thanks. I will resubmit when I get back from a business trip.
>
> Cordialement
>
> Paul
>
>
>
> On Sat, 27 Jul 2024 at 12:35, Mikael Morin  wrote:
>
>> Hello,
>>
>> Le 27/07/2024 à 11:25, Paul Richard Thomas a écrit :
>> > This patch is straightforward but I am still puzzled as to why it is
>> > necessary for the particular case. Having looked at all the other
>> chunks
>> > of frontend code involving use renaming, it seems that the process just
>> > works everywhere else. I tried putting the new code in gfc_find_symtree
>> > but it caused some regressions unless I pinned it down to the specific
>> > case of a structure constructor.
>> >
>> I think it works as expected, symtrees have the local names, and symbols
>> the original name, so if all that is available is the symbol, name
>> lookup may not work correctly with renaming.  And I think that there are
>> other places where it happens.
>>
>> In this case, it seems the caller can provide the local name, which
>> would avoid processing the use statements.  Did you try that?
>>
>> Mikael
>>
>
diff --git a/gcc/fortran/decl.cc b/gcc/fortran/decl.cc
index b8308aeee55..119c9dffa03 100644
--- a/gcc/fortran/decl.cc
+++ b/gcc/fortran/decl.cc
@@ -376,6 +376,7 @@ match_data_constant (gfc_expr **result)
   gfc_expr *expr;
   match m;
   locus old_loc;
+  gfc_symtree *symtree;
 
   m = gfc_match_literal_constant (&expr, 1);
   if (m == MATCH_YES)
@@ -436,9 +437,11 @@ match_data_constant (gfc_expr **result)
   if (m != MATCH_YES)
 return m;
 
-  if (gfc_find_symbol (name, NULL, 1, &sym))
+  if (gfc_find_sym_tree (name, NULL, 1, &symtree))
 return MATCH_ERROR;
 
+  sym = symtree->n.sym;
+
   if (sym && sym->attr.generic)
 dt_sym = gfc_find_dt_in_generic (sym);
 
@@ -452,7 +455,7 @@ match_data_constant (gfc_expr **result)
   return MATCH_ERROR;
 }
   else if (dt_sym && gfc_fl_struct (dt_sym->attr.flavor))
-return gfc_match_structure_constructor (dt_sym, result);
+return gfc_match_structure_constructor (dt_sym, symtree, result);
 
   /* Check to see if the value is an initialization array expression.  */
   if (sym->value->expr_type == EXPR_ARRAY)
diff --git a/gcc/fortran/match.h b/gcc/fortran/match.h
index c2b7d69c37c..519ad194a15 100644
--- a/gcc/fortran/match.h
+++ b/gcc/fortran/match.h
@@ -101,7 +101,7 @@ match gfc_match_call (void);
 /* We want to use this function to check for a common-block-name
that can exist in a bind statement, so removed the "static"
declaration of the function in match.cc. */
- 
+
 match gfc_match_common_name (char *name);
 
 match gfc_match_common (void);
@@ -302,7 +302,7 @@ match gfc_match_bind_c_stmt (void);
 match gfc_match_bind_c (gfc_symbol *, bool);
 
 /* primary.cc.  */
-match gfc_match_structure_constructor (gfc_symbol *, gfc_expr **);
+match gfc_match_structure_constructor (gfc_symbol *, gfc_symtree *, gfc_expr **);
 match gfc_match_variable (gfc_expr **, int);
 match gfc_match_equiv_variable (gfc_expr **);
 match gfc_match_actual_arglist (int, gfc_actual_arglist **, bool = false);
diff --git a/gcc/fortran/primary.cc b/gcc/fortran/primary.cc
index 76f6bcb8a78..9000b9cbbbc 100644
--- a/gcc/fortran/primary.cc
+++ b/gcc/fortran/primary.cc
@@ -3520,18 +3520,16 @@ gfc_convert_to_structure_constructor (gfc_expr *e, gfc_symbol *sym, gfc_expr **c
 
 
 match
-gfc_match_structure_constructor (gfc_symbol *sym, gfc_expr **result)
+gfc_match_structure_constructor (gfc_symbol *sym, gfc_symtree *symtree,
+ gfc_expr **result)
 {
   match m;
   gfc_expr *e;
-  gfc_symtree *symtree;
   bool t = true;
 
-  gfc_get_ha_sym_tree (sym->name, &symtree);
-
   e = gfc_get_expr ();
-  e->symtree = symtree;
   e->expr_type = EXPR_FUNCTION;
+  e->symtree = symtree;
   e->where = gfc_current_locus;
 
   gcc_assert (gfc_fl_struct (sym->attr.flavor)
diff --git a/gcc/testsuite/gfortran.dg/use_rename_12.f90 b/gcc/testsuite/gfortran.dg/use_rename_12.f90
new file mode 100644
index 000..97f26f42f76
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/use_rename_12.f90
@@ -0,0 +1,37 @@
+! { dg-do compile }
+!
+! Test the fix for pr79685, which failed as in the comments below.
+!
+! Contributed by J

[committed v2] Add config file so b4 uses inbox.sourceware.org automatically

2024-07-28 Thread Jonathan Wakely
Jeff approved an earlier version of this as trivially OK (since it
doesn't affect anybody not using b4). The v2 patch that I've pushed adds
the send-series-to config, as suggested by the b4 maintainer here:
https://lore.kernel.org/tools/20240523143752.385810-1-jwak...@redhat.com/T/#mb21ebe04b433442f40548acc4394554e65a8d1be

Pushed to trunk.

-- >8 --

This makes b4 use inbox.sourceware.org instead of the default host
lore.kernel.org, so that every b4 user doesn't have to configure this
themselves.

ChangeLog:

* .b4-config: New file.
---
 .b4-config | 4 
 1 file changed, 4 insertions(+)
 create mode 100644 .b4-config

diff --git a/.b4-config b/.b4-config
new file mode 100644
index 000..eec2261deb6
--- /dev/null
+++ b/.b4-config
@@ -0,0 +1,4 @@
+[b4]
+midmask = https://inbox.sourceware.org/%s
+linkmask = https://inbox.sourceware.org/%s
+send-series-to = gcc-patches@gcc.gnu.org
-- 
2.45.2



[PATCH] PR tree-optimization/57371: Optimize (float)i == 16777222.0f sometimes.

2024-07-28 Thread Roger Sayle

This patch improves the tree-level optimization of (fptype)ivar != CST
in match.pd (historically tracked under PR 57371).  Joseph Myers'
description in comment #1 provides an excellent overview of the issues,
that historically it's the trapping behaviour of (fptype)ivar conversion
that is the primary concern, which is why the current code in match.pd
checks fmt.can_represent_integral_type_p (itype).  The first of the
improvements with this patch is to check flag_trapping_math to control
whether FP_OVERFLOW/FP_INEXACT needs to be preserved, and to use
ranger to determine whether the bounds on ivar confirm that these
traps aren't possible.  For example, the expression (int)var & 15
can't overflow conversion to IEEE float, even though the type of a
32-bit int could potentially overflow the significant of a 32-bit
float.

The next of the optimizations concern checking whether the comparison
against CST is unambiguous allowing it to be replaced with a integer
comparison.  For reference, consider the table below which shows the
default conversion of integers to IEEE 32-bit float.

(float)16777211 => 16777211.0f;
(float)16777212 => 16777212.0f;
(float)16777213 => 16777213.0f;
(float)16777214 => 16777214.0f;
(float)16777215 => 16777215.0f;
(float)16777216 => 16777216.0f;
(float)16777217 => 16777216.0f;  // rounded
(float)16777218 => 16777218.0f;
(float)16777219 => 16777220.0f;  // rounded
(float)16777220 => 16777220.0f;
(float)16777221 => 16777220.0f;  // rounded
(float)16777222 => 16777222.0f;
(float)16777223 => 16777224.0f;  // rounded
(float)16777224 => 16777224.0f;
(float)16777225 => 16777224.0f;  // rounded
(float)16777226 => 16777226.0f;

Observe that it's safe to optimize (float)i == 16777212.0f to the
equivalent i == 16777212 (as this is the only integer that can
convert to that floating point constant), but that it's unsafe to
optimize (float)i == 16777220.0f, as with default rounding there
are three possible integer values that FLOAT_EXPR to 16777220.0f.
The pragmatic check used in this patch is to confirm that (float)(i-1)
and (float)(i+1) are both distinct from (float)i before simplifying
the comparison to an integer-typed comparison.

Finally, this patch also handles non-default rounding modes.
In the table above, it's safe to optimize (float)i == 16777222.0f
in IEEE's default rounding mode, but not in all FP rounding modes.
This eventuality is handled by testing whether the (float)i, the
(float)(i-1) and the (float)(i+1) are all exactly rounded when
-frounding-math is specified.

This patch has been tested on x86_64-pc-linux-gnu with make bootstrap
and make -k check, both with and without --target_board=unix{-m32}
with no new failures.  Ok for mainline?  If the testcases need to be
tweaked for non-IEEE targets (the transformations themselves should
be portable to VAX and IBM floating point formats) hopefully that
can be done as follow-up patches by folks with the appropriate
effective-targets?


2024-07-28  Roger Sayle  

gcc/ChangeLog
PR tree-optimization/57371
* fold-const.cc (fold_cmp_float_cst_p): New helper function.
* fold-const.h (fold_cmp_float_cst_p): Prototype here.
* match.pd ((FTYPE) N CMP CST): Use ranger to determine
whether value is exactly representable by floating point type,
and check flag_trapping_math if not.  Use the new helper
fold_cmp_float_cst_p to check that transformation to an integer
comparison is safe.

gcc/testsuite/ChangeLog
PR tree-optimization/57371
* c-c++-common/pr57371-6.c: New test case.
* c-c++-common/pr57371-7.c: Likewise.
* c-c++-common/pr57371-8.c: Likewise.
* c-c++-common/pr57371-9.c: Likewise.
* c-c++-common/pr57371-10.c: Likewise.


Thanks in advance,
Roger
--

diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc
index 710d697..3dd6dae 100644
--- a/gcc/fold-const.cc
+++ b/gcc/fold-const.cc
@@ -7531,6 +7531,92 @@ fold_real_zero_addition_p (const_tree type, const_tree 
arg,
   return negate || (arg && !tree_expr_maybe_real_minus_zero_p (arg));
 }
 
+/* Subroutine of match.pd that determines if it is safe to optimize
+   a floating point comparison of an integer value, known to be between
+   LO and HI, using comparison operator CMP, against the real constant
+   R in floating point type FMT, as the same integer comparison against
+   the integer constant I, with sign ISIGN.
+
+   For example, with IEEE-754, (float)x == 2.0f may replaced with x == 2
+   because the floating point representations of the neighboring integers
+   (float)1 and (float)3 are distinct from 2.0f, having values 1.0f and
+   3.0f respectively.  On the other hand (float)x == 16777220.0f can't
+   be replaced by x == 16777220 as (float)16777221 is also 1677220.0f
+   due to truncation/rounding.
+*/
+bool
+fold_cmp_float_cst_p (wide_int lo, wide_int hi, enum tree_code cmp,
+ const REAL_VALUE_TYPE *r, format_helper fmt,
+ wide_int i, signop i

[RFC v1 0/2] c: Add _Lengthof operator

2024-07-28 Thread Alejandro Colomar
Hi!

I've got something working:

$ cat len.c 
#include 

int
main(int argc, char *argv[argc + 1])
{
int a[42];
size_t  n;

(void) argv;

//n = _Lengthof(argv);
//printf("_Lengthof(argv) == %zu\n", n);

n = _Lengthof(a);
printf("%zu\n", n);

n = _Lengthof(long [99]);
printf("%zu\n", n);

n = _Lengthof(short [n - 10]);
printf("%zu\n", n);

int  b[n / 2];
n = _Lengthof(b);
printf("%zu\n", n);
}
$ /opt/local/gnu/gcc/lengthof/bin/gcc -Wall -Wextra len.c 
$ ./a.out 
42
99
89
44

I'd like to be able to uncomment those two lines in the future, but we
probably want to do that in a separate commit.

Does anyone know if we have the information available for getting that
value from the 'tree' object?  Or do we need some refactor first in
order to keep that information?

Also, I've reused much of sizeof's code, and maybe I should change some
of that.  It's my first contribution to gcc of this size, and I don't
yet know what some of those internals mean.  While my implementation
seems to be working (for the test above), I guess it's not ideal in
terms of readability, and may also not work well with C++ or some corner
cases.  Can somebody with more experience have a look at the code?

I suspect this is not yet ready, and thus doesn't have a ChangeLog.

Have a lovely day!
Alex


Alejandro Colomar (2):
  Merge definitions of array_type_nelts_top()
  c: Add _Lengthof() operator

 gcc/Makefile.in   |  1 +
 gcc/c-family/c-common.cc  | 20 +
 gcc/c-family/c-common.def |  4 ++
 gcc/c-family/c-common.h   |  2 +
 gcc/c/c-parser.cc | 35 +++
 gcc/c/c-tree.h|  4 ++
 gcc/c/c-typeck.cc | 84 +++
 gcc/cp/cp-tree.h  |  1 -
 gcc/cp/operators.def  |  1 +
 gcc/cp/tree.cc| 13 --
 gcc/ginclude/stdlength.h  | 35 +++
 gcc/rust/backend/rust-tree.cc | 13 --
 gcc/rust/backend/rust-tree.h  |  2 -
 gcc/target.h  |  3 ++
 gcc/tree.cc   | 13 ++
 gcc/tree.h|  1 +
 16 files changed, 195 insertions(+), 37 deletions(-)
 create mode 100644 gcc/ginclude/stdlength.h

Range-diff against v0:
-:  --- > 1:  507f5a51e17 Merge definitions of array_type_nelts_top()
-:  --- > 2:  e5835b982af c: Add _Lengthof() operator
-- 
2.45.2



signature.asc
Description: PGP signature


[RFC v1 1/2] Merge definitions of array_type_nelts_top()

2024-07-28 Thread Alejandro Colomar
There were two identical definitions, and none of them are available
where they are needed for implementing _Lengthof().  Merge them, and
provide the single definition in gcc/tree.{h,cc}, where it's available
for _Lengthof().

Signed-off-by: Alejandro Colomar 
---
 gcc/cp/cp-tree.h  |  1 -
 gcc/cp/tree.cc| 13 -
 gcc/rust/backend/rust-tree.cc | 13 -
 gcc/rust/backend/rust-tree.h  |  2 --
 gcc/tree.cc   | 13 +
 gcc/tree.h|  1 +
 6 files changed, 14 insertions(+), 29 deletions(-)

diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index c1a371bc721..e6c1c63f872 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -8099,7 +8099,6 @@ extern tree build_exception_variant   (tree, 
tree);
 extern void fixup_deferred_exception_variants   (tree, tree);
 extern tree bind_template_template_parm(tree, tree);
 extern tree array_type_nelts_total (tree);
-extern tree array_type_nelts_top   (tree);
 extern bool array_of_unknown_bound_p   (const_tree);
 extern tree break_out_target_exprs (tree, bool = false);
 extern tree build_ctor_subob_ref   (tree, tree, tree);
diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc
index dfd4a3a948b..1f3ecff1a21 100644
--- a/gcc/cp/tree.cc
+++ b/gcc/cp/tree.cc
@@ -3071,19 +3071,6 @@ cxx_print_statistics (void)
 depth_reached);
 }
 
-/* Return, as an INTEGER_CST node, the number of elements for TYPE
-   (which is an ARRAY_TYPE).  This counts only elements of the top
-   array.  */
-
-tree
-array_type_nelts_top (tree type)
-{
-  return fold_build2_loc (input_location,
- PLUS_EXPR, sizetype,
- array_type_nelts (type),
- size_one_node);
-}
-
 /* Return, as an INTEGER_CST node, the number of elements for TYPE
(which is an ARRAY_TYPE).  This one is a recursive count of all
ARRAY_TYPEs that are clumped together.  */
diff --git a/gcc/rust/backend/rust-tree.cc b/gcc/rust/backend/rust-tree.cc
index 2a5ffcbf895..dd8eda84f9b 100644
--- a/gcc/rust/backend/rust-tree.cc
+++ b/gcc/rust/backend/rust-tree.cc
@@ -859,19 +859,6 @@ is_empty_class (tree type)
   return CLASSTYPE_EMPTY_P (type);
 }
 
-// forked from gcc/cp/tree.cc array_type_nelts_top
-
-/* Return, as an INTEGER_CST node, the number of elements for TYPE
-   (which is an ARRAY_TYPE).  This counts only elements of the top
-   array.  */
-
-tree
-array_type_nelts_top (tree type)
-{
-  return fold_build2_loc (input_location, PLUS_EXPR, sizetype,
- array_type_nelts (type), size_one_node);
-}
-
 // forked from gcc/cp/tree.cc builtin_valid_in_constant_expr_p
 
 /* Test whether DECL is a builtin that may appear in a
diff --git a/gcc/rust/backend/rust-tree.h b/gcc/rust/backend/rust-tree.h
index 26c8b653ac6..e597c3ab81d 100644
--- a/gcc/rust/backend/rust-tree.h
+++ b/gcc/rust/backend/rust-tree.h
@@ -2993,8 +2993,6 @@ extern location_t rs_expr_location (const_tree);
 extern int
 is_empty_class (tree type);
 
-extern tree array_type_nelts_top (tree);
-
 extern bool
 is_really_empty_class (tree, bool);
 
diff --git a/gcc/tree.cc b/gcc/tree.cc
index 2d2d5b6db6e..3b0adb4cd9f 100644
--- a/gcc/tree.cc
+++ b/gcc/tree.cc
@@ -3729,6 +3729,19 @@ array_type_nelts (const_tree type)
  ? max
  : fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, min));
 }
+
+/* Return, as an INTEGER_CST node, the number of elements for TYPE
+   (which is an ARRAY_TYPE).  This counts only elements of the top
+   array.  */
+
+tree
+array_type_nelts_top (tree type)
+{
+  return fold_build2_loc (input_location,
+ PLUS_EXPR, sizetype,
+ array_type_nelts (type),
+ size_one_node);
+}
 
 /* If arg is static -- a reference to an object in static storage -- then
return the object.  This is not the same as the C meaning of `static'.
diff --git a/gcc/tree.h b/gcc/tree.h
index 28e8e71b036..c620e55b68d 100644
--- a/gcc/tree.h
+++ b/gcc/tree.h
@@ -4922,6 +4922,7 @@ extern tree build_method_type (tree, tree);
 extern tree build_offset_type (tree, tree);
 extern tree build_complex_type (tree, bool named = false);
 extern tree array_type_nelts (const_tree);
+extern tree array_type_nelts_top (tree);
 
 extern tree value_member (tree, tree);
 extern tree purpose_member (const_tree, tree);
-- 
2.45.2



signature.asc
Description: PGP signature


[RFC v1 2/2] c: Add _Lengthof() operator

2024-07-28 Thread Alejandro Colomar
This operator is similar to sizeof() but can only be applied to an
array, and returns its length (number of elements).

FUTURE DIRECTIONS:

We could make it work with array parameters to functions, and
somehow magically return the length designator of the array,
regardless of it being really a pointer.

Link: 
Cc: Gabriel Ravier 
Cc: Martin Uecker 
Cc: Joseph Myers 
Signed-off-by: Alejandro Colomar 
---
 gcc/Makefile.in   |  1 +
 gcc/c-family/c-common.cc  | 20 ++
 gcc/c-family/c-common.def |  4 ++
 gcc/c-family/c-common.h   |  2 +
 gcc/c/c-parser.cc | 35 
 gcc/c/c-tree.h|  4 ++
 gcc/c/c-typeck.cc | 84 +++
 gcc/cp/operators.def  |  1 +
 gcc/ginclude/stdlength.h  | 35 
 gcc/target.h  |  3 ++
 10 files changed, 181 insertions(+), 8 deletions(-)
 create mode 100644 gcc/ginclude/stdlength.h

diff --git a/gcc/Makefile.in b/gcc/Makefile.in
index f4bb4a88cf3..88496edbdc8 100644
--- a/gcc/Makefile.in
+++ b/gcc/Makefile.in
@@ -477,6 +477,7 @@ USER_H = $(srcdir)/ginclude/float.h \
 $(srcdir)/ginclude/stdalign.h \
 $(srcdir)/ginclude/stdatomic.h \
 $(srcdir)/ginclude/stdckdint.h \
+$(srcdir)/ginclude/stdlength.h \
 $(EXTRA_HEADERS)
 
 USER_H_INC_NEXT_PRE = @user_headers_inc_next_pre@
diff --git a/gcc/c-family/c-common.cc b/gcc/c-family/c-common.cc
index e7e371fd26f..c9020a7faef 100644
--- a/gcc/c-family/c-common.cc
+++ b/gcc/c-family/c-common.cc
@@ -407,6 +407,7 @@ const struct c_common_resword c_common_reswords[] =
   { "_Decimal64",   RID_DFLOAT64,  D_CONLY },
   { "_Decimal128",  RID_DFLOAT128, D_CONLY },
   { "_Fract",   RID_FRACT, D_CONLY | D_EXT },
+  { "_Lengthof",RID_LENGTHOF,  D_CONLY | D_EXT },
   { "_Accum",   RID_ACCUM, D_CONLY | D_EXT },
   { "_Sat", RID_SAT,   D_CONLY | D_EXT },
   { "_Static_assert",   RID_STATIC_ASSERT, D_CONLY },
@@ -523,6 +524,7 @@ const struct c_common_resword c_common_reswords[] =
   { "if",  RID_IF, 0 },
   { "inline",  RID_INLINE, D_EXT89 },
   { "int", RID_INT,0 },
+  { "lengthof",RID_LENGTHOF,   D_EXT },
   { "long",RID_LONG,   0 },
   { "mutable", RID_MUTABLE,D_CXXONLY | D_CXXWARN },
   { "namespace",   RID_NAMESPACE,  D_CXXONLY | D_CXXWARN },
@@ -4070,6 +4072,24 @@ c_alignof_expr (location_t loc, tree expr)
 
   return fold_convert_loc (loc, size_type_node, t);
 }
+
+/* Implement the _Lengthof keyword: Return the length of an array,
+   that is, the number of elements in the array.  */
+
+tree
+c_lengthof_type (location_t loc, tree type)
+{
+  enum tree_code type_code;
+
+  type_code = TREE_CODE (type);
+  if (type_code != ARRAY_TYPE)
+{
+  error_at (loc, "invalid application of %<_Lengthof%> to type %qT", type);
+  return error_mark_node;
+}
+
+  return array_type_nelts_top (type);
+}
 
 /* Handle C and C++ default attributes.  */
 
diff --git a/gcc/c-family/c-common.def b/gcc/c-family/c-common.def
index 5de96e5d4a8..46e128072b5 100644
--- a/gcc/c-family/c-common.def
+++ b/gcc/c-family/c-common.def
@@ -50,6 +50,10 @@ DEFTREECODE (EXCESS_PRECISION_EXPR, "excess_precision_expr", 
tcc_expression, 1)
number.  */
 DEFTREECODE (USERDEF_LITERAL, "userdef_literal", tcc_exceptional, 3)
 
+/* Represents a 'sizeof' expression during C++ template expansion,
+   or for the purpose of -Wsizeof-pointer-memaccess warning.  */
+DEFTREECODE (LENGTHOF_EXPR, "lengthof_expr", tcc_expression, 1)
+
 /* Represents a 'sizeof' expression during C++ template expansion,
or for the purpose of -Wsizeof-pointer-memaccess warning.  */
 DEFTREECODE (SIZEOF_EXPR, "sizeof_expr", tcc_expression, 1)
diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h
index ccaea27c2b9..f815a4cf3bc 100644
--- a/gcc/c-family/c-common.h
+++ b/gcc/c-family/c-common.h
@@ -105,6 +105,7 @@ enum rid
 
   /* C extensions */
   RID_ASM,   RID_TYPEOF,   RID_TYPEOF_UNQUAL, RID_ALIGNOF,  RID_ATTRIBUTE,
+  RID_LENGTHOF,
   RID_VA_ARG,
   RID_EXTENSION, RID_IMAGPART, RID_REALPART, RID_LABEL,RID_CHOOSE_EXPR,
   RID_TYPES_COMPATIBLE_P,  RID_BUILTIN_COMPLEX,   RID_BUILTIN_SHUFFLE,
@@ -885,6 +886,7 @@ extern tree c_common_truthvalue_conversion (location_t, 
tree);
 extern void c_apply_type_quals_to_decl (int, tree);
 extern tree c_sizeof_or_alignof_type (location_t, tree, bool, bool, int);
 extern tree c_alignof_expr (location_t, tree);
+extern tree c_lengthof_type (location_t, tree);
 /* Print an error message for invalid operands to arith operation CODE.
NOP_EXPR is used as a special case (see truthvalue_conversion).  */
 extern void binary_op_error (rich_location *, enum tree_code, tree, tree);
diff --git a/gcc/c/c-parser.cc b/gcc/c/c-parser.cc
index 12c5ed5d92c..eb1d8b932fd 100644
--- a/

[PATCH] gcc: Make exec-tool.in handle missing Binutils more gracefully

2024-07-28 Thread Jonathan Wakely
Bootstrapped on x86_64-linux and for msp430-elf cross with and without
binutils for the target to verify the error is printed as expected.

The $invoked variable will be one of as, collect-ld, nm, or dsymutil,
i.e. the tool that the script is invoking and wants to exec.

OK for trunk?

-- >8 --

When users try to build a cross-compiler without first installing
binutils they get confusing errors like:
/tmp/gcc-obj/./gcc/as: line 114: exec: -m: invalid option

This is an incredibly common source of questions on gcc-help and IRC,
and bogus bug reports e.g. see PR 116119 for the latest example.

This change adds an explicit check for an empty $original variable and
exits with a user-friendly error.

gcc/ChangeLog:

* exec-tool.in: Exit with an error if $original is empty.
---
 gcc/exec-tool.in | 8 
 1 file changed, 8 insertions(+)

diff --git a/gcc/exec-tool.in b/gcc/exec-tool.in
index 7d152e083a7..2767e1019b8 100644
--- a/gcc/exec-tool.in
+++ b/gcc/exec-tool.in
@@ -81,6 +81,14 @@ case "$invoked" in
 ;;
 esac
 
+if test -z "$original"
+then
+  echo "$0: \$original is unset" >&2
+  echo "This probably means there is no '$invoked' for the target." >&2
+  echo "For a cross-compiler, you probably need to install Binutils first." >&2
+  exit 1
+fi
+
 case "$original" in
   ../*)
 # compute absolute path of the location of this script
-- 
2.45.2



Re: [RFC v1 0/2] c: Add _Lengthof operator

2024-07-28 Thread Martin Uecker
Am Sonntag, dem 28.07.2024 um 16:15 +0200 schrieb Alejandro Colomar:

...
> 
> Does anyone know if we have the information available for getting that
> value from the 'tree' object?  Or do we need some refactor first in
> order to keep that information?

What I wanted to try is to not immediately adjust the type to a
pointer and keep it represented as an array, so that the size is
preserved.  We currently already have a flag that tells us that
the type came from a parameter declared as an array, so we would
do it the other way round and have a flag that tells us that it
is really a pointer.  

In most cases the array would then decay and nothing needs to be
changed, but in some cases we would special case it to get the 
correct result (addressof / sizeof / typeof and maybe others).  
Those are also cases which should have a warning anyway. So this
*should* be simple, but I haven't tried.

I will look at you code later, but I would recommend to avoid
refactoring that touches different parts of the compiler at this
point.

I also wonder whether it would make sense to propose a GNU
extension first instead of implementing a full prototype for
the standard feature? I do not think we could merge the
former without having an accepted proposal.

Martin

> 
> Also, I've reused much of sizeof's code, and maybe I should change some
> of that.  It's my first contribution to gcc of this size, and I don't
> yet know what some of those internals mean.  While my implementation
> seems to be working (for the test above), I guess it's not ideal in
> terms of readability, and may also not work well with C++ or some corner
> cases.  Can somebody with more experience have a look at the code?
> 
> I suspect this is not yet ready, and thus doesn't have a ChangeLog.
> 
> Have a lovely day!
> Alex
> 
> 
> Alejandro Colomar (2):
>   Merge definitions of array_type_nelts_top()
>   c: Add _Lengthof() operator
> 
>  gcc/Makefile.in   |  1 +
>  gcc/c-family/c-common.cc  | 20 +
>  gcc/c-family/c-common.def |  4 ++
>  gcc/c-family/c-common.h   |  2 +
>  gcc/c/c-parser.cc | 35 +++
>  gcc/c/c-tree.h|  4 ++
>  gcc/c/c-typeck.cc | 84 +++
>  gcc/cp/cp-tree.h  |  1 -
>  gcc/cp/operators.def  |  1 +
>  gcc/cp/tree.cc| 13 --
>  gcc/ginclude/stdlength.h  | 35 +++
>  gcc/rust/backend/rust-tree.cc | 13 --
>  gcc/rust/backend/rust-tree.h  |  2 -
>  gcc/target.h  |  3 ++
>  gcc/tree.cc   | 13 ++
>  gcc/tree.h|  1 +
>  16 files changed, 195 insertions(+), 37 deletions(-)
>  create mode 100644 gcc/ginclude/stdlength.h
> 
> Range-diff against v0:
> -:  --- > 1:  507f5a51e17 Merge definitions of array_type_nelts_top()
> -:  --- > 2:  e5835b982af c: Add _Lengthof() operator



Re: [RFC v1 0/2] c: Add _Lengthof operator

2024-07-28 Thread Alejandro Colomar
Hi Martin,

On Sun, Jul 28, 2024 at 04:42:26PM GMT, Martin Uecker wrote:
> Am Sonntag, dem 28.07.2024 um 16:15 +0200 schrieb Alejandro Colomar:
> 
> ...
> > 
> > Does anyone know if we have the information available for getting that
> > value from the 'tree' object?  Or do we need some refactor first in
> > order to keep that information?
> 
> What I wanted to try is to not immediately adjust the type to a
> pointer and keep it represented as an array, so that the size is
> preserved.  We currently already have a flag that tells us that
> the type came from a parameter declared as an array, so we would
> do it the other way round and have a flag that tells us that it
> is really a pointer.  

Thanks!  Thanks makes sense.  I suspected you had something in mind.  :)

Do you remember that flag's name?  I can have a look at inverting that.

> In most cases the array would then decay and nothing needs to be
> changed, but in some cases we would special case it to get the 
> correct result (addressof / sizeof / typeof and maybe others).  
> Those are also cases which should have a warning anyway. So this
> *should* be simple, but I haven't tried.
> 
> I will look at you code later, but I would recommend to avoid
> refactoring that touches different parts of the compiler at this
> point.

I had to do patch 1/2 for having the function that gives me the nelts
value; other than that, I've tried to keep it minimal.

> I also wonder whether it would make sense to propose a GNU
> extension first instead of implementing a full prototype for
> the standard feature? I do not think we could merge the
> former without having an accepted proposal.

Do you mean a __lengthof__ operator first, without using the _L name
or ?

If so, I agree.

> Martin

Cheers,
Alex

> > Alejandro Colomar (2):
> >   Merge definitions of array_type_nelts_top()
> >   c: Add _Lengthof() operator
> > 
> >  gcc/Makefile.in   |  1 +
> >  gcc/c-family/c-common.cc  | 20 +
> >  gcc/c-family/c-common.def |  4 ++
> >  gcc/c-family/c-common.h   |  2 +
> >  gcc/c/c-parser.cc | 35 +++
> >  gcc/c/c-tree.h|  4 ++
> >  gcc/c/c-typeck.cc | 84 +++
> >  gcc/cp/cp-tree.h  |  1 -
> >  gcc/cp/operators.def  |  1 +
> >  gcc/cp/tree.cc| 13 --
> >  gcc/ginclude/stdlength.h  | 35 +++
> >  gcc/rust/backend/rust-tree.cc | 13 --
> >  gcc/rust/backend/rust-tree.h  |  2 -
> >  gcc/target.h  |  3 ++
> >  gcc/tree.cc   | 13 ++
> >  gcc/tree.h|  1 +
> >  16 files changed, 195 insertions(+), 37 deletions(-)
> >  create mode 100644 gcc/ginclude/stdlength.h
> > 
> > Range-diff against v0:
> > -:  --- > 1:  507f5a51e17 Merge definitions of 
> > array_type_nelts_top()
> > -:  --- > 2:  e5835b982af c: Add _Lengthof() operator
> 

-- 



signature.asc
Description: PGP signature


Re: [PATCH] gcc: Make exec-tool.in handle missing Binutils more gracefully

2024-07-28 Thread Richard Biener



> Am 28.07.2024 um 16:27 schrieb Jonathan Wakely :
> 
> Bootstrapped on x86_64-linux and for msp430-elf cross with and without
> binutils for the target to verify the error is printed as expected.
> 
> The $invoked variable will be one of as, collect-ld, nm, or dsymutil,
> i.e. the tool that the script is invoking and wants to exec.
> 
> OK for trunk?
> 

Ok

Richard 
> -- >8 --
> 
> When users try to build a cross-compiler without first installing
> binutils they get confusing errors like:
> /tmp/gcc-obj/./gcc/as: line 114: exec: -m: invalid option
> 
> This is an incredibly common source of questions on gcc-help and IRC,
> and bogus bug reports e.g. see PR 116119 for the latest example.
> 
> This change adds an explicit check for an empty $original variable and
> exits with a user-friendly error.
> 
> gcc/ChangeLog:
> 
>* exec-tool.in: Exit with an error if $original is empty.
> ---
> gcc/exec-tool.in | 8 
> 1 file changed, 8 insertions(+)
> 
> diff --git a/gcc/exec-tool.in b/gcc/exec-tool.in
> index 7d152e083a7..2767e1019b8 100644
> --- a/gcc/exec-tool.in
> +++ b/gcc/exec-tool.in
> @@ -81,6 +81,14 @@ case "$invoked" in
> ;;
> esac
> 
> +if test -z "$original"
> +then
> +  echo "$0: \$original is unset" >&2
> +  echo "This probably means there is no '$invoked' for the target." >&2
> +  echo "For a cross-compiler, you probably need to install Binutils first." 
> >&2
> +  exit 1
> +fi
> +
> case "$original" in
>   ../*)
> # compute absolute path of the location of this script
> --
> 2.45.2
> 


[RFC v2 0/2] c: Add __lengthof__ operator

2024-07-28 Thread Alejandro Colomar
v2:

-  Add a GNU extension first; not using ISO C-reserved names.  That is,
   add __lengthof__ instead of _Lengthof/lengthof.
-  Use 'in_lengthof' instead of 'in_sizeof'.

Alejandro Colomar (2):
  Merge definitions of array_type_nelts_top()
  c: Add __lengthof__() operator

 gcc/c-family/c-common.cc  | 19 +++
 gcc/c-family/c-common.def |  3 ++
 gcc/c-family/c-common.h   |  2 +
 gcc/c/c-decl.cc   | 20 +---
 gcc/c/c-parser.cc | 61 --
 gcc/c/c-tree.h|  4 ++
 gcc/c/c-typeck.cc | 95 +--
 gcc/cp/cp-tree.h  |  1 -
 gcc/cp/operators.def  |  1 +
 gcc/cp/tree.cc| 13 -
 gcc/rust/backend/rust-tree.cc | 13 -
 gcc/rust/backend/rust-tree.h  |  2 -
 gcc/target.h  |  3 ++
 gcc/tree.cc   | 13 +
 gcc/tree.h|  1 +
 15 files changed, 198 insertions(+), 53 deletions(-)

Range-diff against v1:
1:  507f5a51e17 = 1:  507f5a51e17 Merge definitions of array_type_nelts_top()
2:  e5835b982af ! 2:  6b48d48ecdd c: Add _Lengthof() operator
@@ Metadata
 Author: Alejandro Colomar 
 
  ## Commit message ##
-c: Add _Lengthof() operator
+c: Add __lengthof__() operator
 
 This operator is similar to sizeof() but can only be applied to an
 array, and returns its length (number of elements).
@@ Commit message
 Cc: Joseph Myers 
 Signed-off-by: Alejandro Colomar 
 
- ## gcc/Makefile.in ##
-@@ gcc/Makefile.in: USER_H = $(srcdir)/ginclude/float.h \
-$(srcdir)/ginclude/stdalign.h \
-$(srcdir)/ginclude/stdatomic.h \
-$(srcdir)/ginclude/stdckdint.h \
-+   $(srcdir)/ginclude/stdlength.h \
-$(EXTRA_HEADERS)
- 
- USER_H_INC_NEXT_PRE = @user_headers_inc_next_pre@
-
  ## gcc/c-family/c-common.cc ##
 @@ gcc/c-family/c-common.cc: const struct c_common_resword 
c_common_reswords[] =
-   { "_Decimal64",   RID_DFLOAT64,  D_CONLY },
-   { "_Decimal128",  RID_DFLOAT128, D_CONLY },
-   { "_Fract",   RID_FRACT, D_CONLY | D_EXT },
-+  { "_Lengthof",RID_LENGTHOF,  D_CONLY | D_EXT },
-   { "_Accum",   RID_ACCUM, D_CONLY | D_EXT },
-   { "_Sat", RID_SAT,   D_CONLY | D_EXT },
-   { "_Static_assert",   RID_STATIC_ASSERT, D_CONLY },
-@@ gcc/c-family/c-common.cc: const struct c_common_resword 
c_common_reswords[] =
-   { "if", RID_IF, 0 },
-   { "inline", RID_INLINE, D_EXT89 },
-   { "int",RID_INT,0 },
-+  { "lengthof",   RID_LENGTHOF,   D_EXT },
-   { "long",   RID_LONG,   0 },
-   { "mutable",RID_MUTABLE,D_CXXONLY | D_CXXWARN },
-   { "namespace",  RID_NAMESPACE,  D_CXXONLY | D_CXXWARN },
+   { "__inline",   RID_INLINE, 0 },
+   { "__inline__", RID_INLINE, 0 },
+   { "__label__",  RID_LABEL,  0 },
++  { "__lengthof__",   RID_LENGTHOF, 0 },
+   { "__null", RID_NULL,   0 },
+   { "__real", RID_REALPART,   0 },
+   { "__real__",   RID_REALPART,   0 },
 @@ gcc/c-family/c-common.cc: c_alignof_expr (location_t loc, tree expr)
  
return fold_convert_loc (loc, size_type_node, t);
  }
 +
-+/* Implement the _Lengthof keyword: Return the length of an array,
++/* Implement the lengthof keyword: Return the length of an array,
 +   that is, the number of elements in the array.  */
 +
 +tree
@@ gcc/c-family/c-common.cc: c_alignof_expr (location_t loc, tree expr)
 +  type_code = TREE_CODE (type);
 +  if (type_code != ARRAY_TYPE)
 +{
-+  error_at (loc, "invalid application of %<_Lengthof%> to type %qT", 
type);
++  error_at (loc, "invalid application of % to type %qT", 
type);
 +  return error_mark_node;
 +}
 +
@@ gcc/c-family/c-common.def: DEFTREECODE (EXCESS_PRECISION_EXPR, 
"excess_precision
 number.  */
  DEFTREECODE (USERDEF_LITERAL, "userdef_literal", tcc_exceptional, 3)
  
-+/* Represents a 'sizeof' expression during C++ template expansion,
-+   or for the purpose of -Wsizeof-pointer-memaccess warning.  */
++/* Represents a 'lengthof' expression.  */
 +DEFTREECODE (LENGTHOF_EXPR, "lengthof_expr", tcc_expression, 1)
 +
  /* Represents a 'sizeof' expression during C++ template expansion,
@@ gcc/c-family/c-common.h: extern tree c_common_truthvalue_conversion 
(location_t,
 NOP_EXPR is used as a special case (see truthvalue_conversion).  */
  extern void binary_op_error (rich_location *, enum tree_code, tree, tree);
 
+ ## gcc/c/c-decl.cc ##
+@@ gcc/c/c-decl.cc: start_struct (location_t loc, enum tree_code code, 
tree name,
+  within a statement expr used

[RFC v2 1/2] Merge definitions of array_type_nelts_top()

2024-07-28 Thread Alejandro Colomar
There were two identical definitions, and none of them are available
where they are needed for implementing _Lengthof().  Merge them, and
provide the single definition in gcc/tree.{h,cc}, where it's available
for _Lengthof().

Signed-off-by: Alejandro Colomar 
---
 gcc/cp/cp-tree.h  |  1 -
 gcc/cp/tree.cc| 13 -
 gcc/rust/backend/rust-tree.cc | 13 -
 gcc/rust/backend/rust-tree.h  |  2 --
 gcc/tree.cc   | 13 +
 gcc/tree.h|  1 +
 6 files changed, 14 insertions(+), 29 deletions(-)

diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index c1a371bc721..e6c1c63f872 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -8099,7 +8099,6 @@ extern tree build_exception_variant   (tree, 
tree);
 extern void fixup_deferred_exception_variants   (tree, tree);
 extern tree bind_template_template_parm(tree, tree);
 extern tree array_type_nelts_total (tree);
-extern tree array_type_nelts_top   (tree);
 extern bool array_of_unknown_bound_p   (const_tree);
 extern tree break_out_target_exprs (tree, bool = false);
 extern tree build_ctor_subob_ref   (tree, tree, tree);
diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc
index dfd4a3a948b..1f3ecff1a21 100644
--- a/gcc/cp/tree.cc
+++ b/gcc/cp/tree.cc
@@ -3071,19 +3071,6 @@ cxx_print_statistics (void)
 depth_reached);
 }
 
-/* Return, as an INTEGER_CST node, the number of elements for TYPE
-   (which is an ARRAY_TYPE).  This counts only elements of the top
-   array.  */
-
-tree
-array_type_nelts_top (tree type)
-{
-  return fold_build2_loc (input_location,
- PLUS_EXPR, sizetype,
- array_type_nelts (type),
- size_one_node);
-}
-
 /* Return, as an INTEGER_CST node, the number of elements for TYPE
(which is an ARRAY_TYPE).  This one is a recursive count of all
ARRAY_TYPEs that are clumped together.  */
diff --git a/gcc/rust/backend/rust-tree.cc b/gcc/rust/backend/rust-tree.cc
index 2a5ffcbf895..dd8eda84f9b 100644
--- a/gcc/rust/backend/rust-tree.cc
+++ b/gcc/rust/backend/rust-tree.cc
@@ -859,19 +859,6 @@ is_empty_class (tree type)
   return CLASSTYPE_EMPTY_P (type);
 }
 
-// forked from gcc/cp/tree.cc array_type_nelts_top
-
-/* Return, as an INTEGER_CST node, the number of elements for TYPE
-   (which is an ARRAY_TYPE).  This counts only elements of the top
-   array.  */
-
-tree
-array_type_nelts_top (tree type)
-{
-  return fold_build2_loc (input_location, PLUS_EXPR, sizetype,
- array_type_nelts (type), size_one_node);
-}
-
 // forked from gcc/cp/tree.cc builtin_valid_in_constant_expr_p
 
 /* Test whether DECL is a builtin that may appear in a
diff --git a/gcc/rust/backend/rust-tree.h b/gcc/rust/backend/rust-tree.h
index 26c8b653ac6..e597c3ab81d 100644
--- a/gcc/rust/backend/rust-tree.h
+++ b/gcc/rust/backend/rust-tree.h
@@ -2993,8 +2993,6 @@ extern location_t rs_expr_location (const_tree);
 extern int
 is_empty_class (tree type);
 
-extern tree array_type_nelts_top (tree);
-
 extern bool
 is_really_empty_class (tree, bool);
 
diff --git a/gcc/tree.cc b/gcc/tree.cc
index 2d2d5b6db6e..3b0adb4cd9f 100644
--- a/gcc/tree.cc
+++ b/gcc/tree.cc
@@ -3729,6 +3729,19 @@ array_type_nelts (const_tree type)
  ? max
  : fold_build2 (MINUS_EXPR, TREE_TYPE (max), max, min));
 }
+
+/* Return, as an INTEGER_CST node, the number of elements for TYPE
+   (which is an ARRAY_TYPE).  This counts only elements of the top
+   array.  */
+
+tree
+array_type_nelts_top (tree type)
+{
+  return fold_build2_loc (input_location,
+ PLUS_EXPR, sizetype,
+ array_type_nelts (type),
+ size_one_node);
+}
 
 /* If arg is static -- a reference to an object in static storage -- then
return the object.  This is not the same as the C meaning of `static'.
diff --git a/gcc/tree.h b/gcc/tree.h
index 28e8e71b036..c620e55b68d 100644
--- a/gcc/tree.h
+++ b/gcc/tree.h
@@ -4922,6 +4922,7 @@ extern tree build_method_type (tree, tree);
 extern tree build_offset_type (tree, tree);
 extern tree build_complex_type (tree, bool named = false);
 extern tree array_type_nelts (const_tree);
+extern tree array_type_nelts_top (tree);
 
 extern tree value_member (tree, tree);
 extern tree purpose_member (const_tree, tree);
-- 
2.45.2



signature.asc
Description: PGP signature


[RFC v2 2/2] c: Add __lengthof__() operator

2024-07-28 Thread Alejandro Colomar
This operator is similar to sizeof() but can only be applied to an
array, and returns its length (number of elements).

FUTURE DIRECTIONS:

We could make it work with array parameters to functions, and
somehow magically return the length designator of the array,
regardless of it being really a pointer.

Link: 
Cc: Gabriel Ravier 
Cc: Martin Uecker 
Cc: Joseph Myers 
Signed-off-by: Alejandro Colomar 
---
 gcc/c-family/c-common.cc  | 19 
 gcc/c-family/c-common.def |  3 ++
 gcc/c-family/c-common.h   |  2 +
 gcc/c/c-decl.cc   | 20 ++---
 gcc/c/c-parser.cc | 61 +++--
 gcc/c/c-tree.h|  4 ++
 gcc/c/c-typeck.cc | 95 +--
 gcc/cp/operators.def  |  1 +
 gcc/target.h  |  3 ++
 9 files changed, 184 insertions(+), 24 deletions(-)

diff --git a/gcc/c-family/c-common.cc b/gcc/c-family/c-common.cc
index e7e371fd26f..c0d6239c1f8 100644
--- a/gcc/c-family/c-common.cc
+++ b/gcc/c-family/c-common.cc
@@ -465,6 +465,7 @@ const struct c_common_resword c_common_reswords[] =
   { "__inline",RID_INLINE, 0 },
   { "__inline__",  RID_INLINE, 0 },
   { "__label__",   RID_LABEL,  0 },
+  { "__lengthof__",RID_LENGTHOF, 0 },
   { "__null",  RID_NULL,   0 },
   { "__real",  RID_REALPART,   0 },
   { "__real__",RID_REALPART,   0 },
@@ -4070,6 +4071,24 @@ c_alignof_expr (location_t loc, tree expr)
 
   return fold_convert_loc (loc, size_type_node, t);
 }
+
+/* Implement the lengthof keyword: Return the length of an array,
+   that is, the number of elements in the array.  */
+
+tree
+c_lengthof_type (location_t loc, tree type)
+{
+  enum tree_code type_code;
+
+  type_code = TREE_CODE (type);
+  if (type_code != ARRAY_TYPE)
+{
+  error_at (loc, "invalid application of % to type %qT", type);
+  return error_mark_node;
+}
+
+  return array_type_nelts_top (type);
+}
 
 /* Handle C and C++ default attributes.  */
 
diff --git a/gcc/c-family/c-common.def b/gcc/c-family/c-common.def
index 5de96e5d4a8..6d162f67104 100644
--- a/gcc/c-family/c-common.def
+++ b/gcc/c-family/c-common.def
@@ -50,6 +50,9 @@ DEFTREECODE (EXCESS_PRECISION_EXPR, "excess_precision_expr", 
tcc_expression, 1)
number.  */
 DEFTREECODE (USERDEF_LITERAL, "userdef_literal", tcc_exceptional, 3)
 
+/* Represents a 'lengthof' expression.  */
+DEFTREECODE (LENGTHOF_EXPR, "lengthof_expr", tcc_expression, 1)
+
 /* Represents a 'sizeof' expression during C++ template expansion,
or for the purpose of -Wsizeof-pointer-memaccess warning.  */
 DEFTREECODE (SIZEOF_EXPR, "sizeof_expr", tcc_expression, 1)
diff --git a/gcc/c-family/c-common.h b/gcc/c-family/c-common.h
index ccaea27c2b9..f815a4cf3bc 100644
--- a/gcc/c-family/c-common.h
+++ b/gcc/c-family/c-common.h
@@ -105,6 +105,7 @@ enum rid
 
   /* C extensions */
   RID_ASM,   RID_TYPEOF,   RID_TYPEOF_UNQUAL, RID_ALIGNOF,  RID_ATTRIBUTE,
+  RID_LENGTHOF,
   RID_VA_ARG,
   RID_EXTENSION, RID_IMAGPART, RID_REALPART, RID_LABEL,RID_CHOOSE_EXPR,
   RID_TYPES_COMPATIBLE_P,  RID_BUILTIN_COMPLEX,   RID_BUILTIN_SHUFFLE,
@@ -885,6 +886,7 @@ extern tree c_common_truthvalue_conversion (location_t, 
tree);
 extern void c_apply_type_quals_to_decl (int, tree);
 extern tree c_sizeof_or_alignof_type (location_t, tree, bool, bool, int);
 extern tree c_alignof_expr (location_t, tree);
+extern tree c_lengthof_type (location_t, tree);
 /* Print an error message for invalid operands to arith operation CODE.
NOP_EXPR is used as a special case (see truthvalue_conversion).  */
 extern void binary_op_error (rich_location *, enum tree_code, tree, tree);
diff --git a/gcc/c/c-decl.cc b/gcc/c/c-decl.cc
index 97f1d346835..1836151fc41 100644
--- a/gcc/c/c-decl.cc
+++ b/gcc/c/c-decl.cc
@@ -8937,12 +8937,16 @@ start_struct (location_t loc, enum tree_code code, tree 
name,
  within a statement expr used within sizeof, et. al.  This is not
  terribly serious as C++ doesn't permit statement exprs within
  sizeof anyhow.  */
-  if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof))
+  if (warn_cxx_compat && (in_sizeof || in_typeof || in_alignof || in_lengthof))
 warning_at (loc, OPT_Wc___compat,
"defining type in %qs expression is invalid in C++",
(in_sizeof
 ? "sizeof"
-: (in_typeof ? "typeof" : "alignof")));
+: (in_typeof
+   ? "typeof"
+   : (in_alignof
+  ? "alignof"
+  : "lengthof";
 
   if (in_underspecified_init)
 error_at (loc, "%qT defined in underspecified object initializer", ref);
@@ -9897,7 +9901,7 @@ finish_struct (location_t loc, tree t, tree fieldlist, 
tree attributes,
 struct_types.  */
   if (warn_cxx_compat
  && struct_parse_info != NULL
-  

[committed] testsuite: Fix unaligned accesses in ipa-sra-8.c and ipa-sra-9.c

2024-07-28 Thread John David Anglin
Committed as obvious to trunk.  Noticed originally on hppa2.0w-hp-hpux11.11.
Tested on hppa-unknown-linux-gnu.

Dave
---

testsuite: Fix unaligned accesses in ipa-sra-8.c and ipa-sra-9.c

2024-07-28  John David Anglin  

gcc/testsuite/ChangeLog:

PR testsuite/92550
* gcc.dg/ipa/ipa-sra-8.c: Change get_a argument type to SSS.
* gcc.dg/ipa/ipa-sra-9.c: Likewise.

diff --git a/gcc/testsuite/gcc.dg/ipa/ipa-sra-8.c 
b/gcc/testsuite/gcc.dg/ipa/ipa-sra-8.c
index 9e6e40ac54d..dd5c5d0c32b 100644
--- a/gcc/testsuite/gcc.dg/ipa/ipa-sra-8.c
+++ b/gcc/testsuite/gcc.dg/ipa/ipa-sra-8.c
@@ -11,7 +11,7 @@ typedef SS __attribute__((aligned(1))) SSS;
 
 
 static unsigned int __attribute__ ((noinline))
-get_a (SS s)
+get_a (SSS s)
 {
   return s.a;
 };
diff --git a/gcc/testsuite/gcc.dg/ipa/ipa-sra-9.c 
b/gcc/testsuite/gcc.dg/ipa/ipa-sra-9.c
index c5468cfbb76..41d7ddd9fec 100644
--- a/gcc/testsuite/gcc.dg/ipa/ipa-sra-9.c
+++ b/gcc/testsuite/gcc.dg/ipa/ipa-sra-9.c
@@ -7,6 +7,8 @@ typedef struct S {
   unsigned a, b, c;
 } SS;
 
+typedef SS __attribute__((aligned(1))) SSS;
+
 typedef struct U {
   SS s[2];
 } UU;
@@ -14,7 +16,7 @@ typedef struct U {
 typedef UU __attribute__((aligned(1))) UUU;
 
 static unsigned int __attribute__ ((noinline))
-get_a (SS s)
+get_a (SSS s)
 {
   return s.a;
 };


signature.asc
Description: PGP signature


Re: [patch,avr] Implement PR116056: attribute signal(n) and interrupt(n)

2024-07-28 Thread Georg-Johann Lay

Applied with the addendum below.

Am 23.07.24 um 22:19 schrieb Georg-Johann Lay:

This patch adds support for arguments to the signal and interrupt
function attributes.  It allows to specify the ISR by means of the
associated IRQ number, in extension to the current attributes that
require to specify the ISR name like "__vector_1" as (assembly) name
for the function.  The new feature is more convenient, e.g. when the
ISR is implemented by a class method or in a namespace.  There is no
requirement that the ISR is externally visible.  The syntax is like:

__attribute__((signal(1, 2, ...), signal(3, 4, ...)))
[static] void isr_function (void)
{
     // Code
}

Ok for trunk?

Johann

--

AVR target 116056 - Support attribute signal(n) and interrupt(n).

This patch adds support for arguments to the signal and interrupt
function attributes.  It allows to specify the ISR by means of the
associated IRQ number, in extension to the current attributes that
require to specify the ISR name like "__vector_1" as (assembly) name
for the function.  The new feature is more convenient, e.g. when the
ISR is implemented by a class method or in a namespace.  There is no
requirement that the ISR is externally visible.  The syntax is like:

__attribute__((signal(1, 2, ...), signal(3, 4, ...)))
[static] void isr_function (void)
{
     // Code
}

 PR target/116056
gcc/
 * config/avr/avr.h (ASM_DECLARE_FUNCTION_NAME): New define.
 * config/avr/avr-protos.h (avr_declare_function_name): New proto.
 * config/avr/avr-c.cc (avr_cpu_cpp_builtins) <__HAVE_SIGNAL_N__>: New
 built-in macro.
 * config/avr/avr.cc (avr_declare_function_name): New function.
 (avr_attribute_table) : Allow any number of args.
 (avr_insert_attributes): Check validity of "signal" and "interrupt"
 arguments.
 (avr_foreach_function_attribute, avr_interrupt_signal_function)
 (avr_isr_number, avr_asm_isr_alias, avr_handle_isr_attribute):
 New static functions.
 (avr_interrupt_function): New from avr_interrupt_function_p.
 Adjust callers.
 (avr_signal_function): New from avr_signal_function_p.
 Adjust callers.
 (avr_set_current_function): Only diagnose non-__vector ISR names
 when "signal" or "interrupt" attribute has no args.
 (struct avr_fun_cookie): New.
 * doc/extend.texi (AVR Function Attributes): Document
 signal(num) and interrupt(num).
 * doc/invoke.texi (AVR Built-in Macros) <__HAVE_SIGNAL_N__>: Document.
gcc/testsuite/
 * gcc.target/avr/torture/signal_n-1.c: New test.
 * gcc.target/avr/torture/signal_n-2.c: New test.
 * gcc.target/avr/torture/signal_n-3.c: New test.
 * gcc.target/avr/torture/signal_n-4.cpp: New test.diff --git a/gcc/config/avr/avr-c.cc b/gcc/config/avr/avr-c.cc
index 2c5cfb34df6..ca484f26132 100644
--- a/gcc/config/avr/avr-c.cc
+++ b/gcc/config/avr/avr-c.cc
@@ -391,9 +391,10 @@ avr_cpu_cpp_builtins (struct cpp_reader *pfile)
   cpp_define (pfile, "__WITH_AVRLIBC__");
 #endif /* WITH_AVRLIBC */
 
-  // We support __attribute__((signal (n1, n2, ...))).
+  // We support __attribute__((signal/interrupt (n1, n2, ...)[, noblock]))
   cpp_define (pfile, "__HAVE_SIGNAL_N__");
 
+
   // From configure --with-libf7={|libgcc|math|math-symbols|yes|no}
 
 #ifdef WITH_LIBF7_LIBGCC
diff --git a/gcc/config/avr/avr.cc b/gcc/config/avr/avr.cc
index 63643b24cbd..dffb7e056be 100644
--- a/gcc/config/avr/avr.cc
+++ b/gcc/config/avr/avr.cc
@@ -1391,6 +1391,14 @@ avr_naked_function_p (tree func)
   return avr_lookup_function_attribute1 (func, "naked");
 }
 
+/* Return nonzero if FUNC is a noblock function.  */
+
+static bool
+avr_noblock_function_p (tree func)
+{
+  return avr_lookup_function_attribute1 (func, "noblock");
+}
+
 /* Return 1 if FUNC is a function that has a "ATTR_NAME" attribute
(and perhaps also "ATTR_NAME(num)" attributes.  Return -1 if FUNC has
"ATTR_NAME(num)" attribute(s) but no "ATTR_NAME" attribute.
@@ -1492,6 +1500,7 @@ avr_set_current_function (tree decl)
   cfun->machine->is_naked = avr_naked_function_p (decl);
   cfun->machine->is_signal = avr_signal_function (decl);
   cfun->machine->is_interrupt = avr_interrupt_function (decl);
+  cfun->machine->is_noblock = avr_noblock_function_p (decl);
   cfun->machine->is_OS_task = avr_OS_task_function_p (decl);
   cfun->machine->is_OS_main = avr_OS_main_function_p (decl);
   cfun->machine->is_no_gccisr = avr_no_gccisr_function_p (decl);
@@ -1551,6 +1560,11 @@ avr_set_current_function (tree decl)
 		"%qs handler, missing %<__vector%> prefix", name, isr);
 #endif // AVR-LibC naming conventions
 }
+  else if (cfun->machine->is_noblock)
+{
+  warning (OPT_Wattributes, "%qs attribute ignored on non-ISR function",
+	   "noblock");
+}
 
 #if defined WITH_AVRLIBC
   // Common problem is using "ISR" without first including avr/interrupt.h.
@@ -2848,11 +2862,14 @@ avr_prologue_setup_frame (HOST_WIDE_INT size, HARD_REG_SET set)
 	 Always move through unspec, see PR50063.
 	  

Re: [Patch, fortran] PR79685 - [12/13/14/15 Regression] ICE on valid code in gfc_match_structure_constructor

2024-07-28 Thread Mikael Morin

Le 28/07/2024 à 11:24, Paul Richard Thomas a écrit :

I have attached the updated and rather simpler patch.


OK for master and backport.
Thanks for the patch.


Thanks for the prod, Mikael!

Paul

Fortran: Fix ICE with structure constructor in data statement [PR79685]

2024-07-28  Paul Thomas  mailto:pa...@gcc.gnu.org>>

gcc/fortran
PR fortran/79685
* decl.cc (match_data_constant): Find the symtree instead of
the symbol so the use renamed symbols are found. Pass this and
the derived type to gfc_match_structure_constructor.
* match.h: Update prototype of gfc_match_structure_contructor.
* primary.cc (gfc_match_structure_constructor): Remove call to
gfc_get_ha_sym_tree and use caller supplied symtree instead.

gcc/testsuite/
PR fortran/79685
* gfortran.dg/use_rename_12.f90: New test.


On Sat, 27 Jul 2024 at 22:04, Paul Richard Thomas 
mailto:paul.richard.tho...@gmail.com>> 
wrote:


Hi Mikael,

You were absolutely right. I looked at the caller and "just didn't
get it". Thanks. I will resubmit when I get back from a business trip.

Cordialement

Paul



On Sat, 27 Jul 2024 at 12:35, Mikael Morin mailto:morin-mik...@orange.fr>> wrote:

Hello,

Le 27/07/2024 à 11:25, Paul Richard Thomas a écrit :
 > This patch is straightforward but I am still puzzled as to
why it is
 > necessary for the particular case. Having looked at all the
other chunks
 > of frontend code involving use renaming, it seems that the
process just
 > works everywhere else. I tried putting the new code in
gfc_find_symtree
 > but it caused some regressions unless I pinned it down to the
specific
 > case of a structure constructor.
 >
I think it works as expected, symtrees have the local names, and
symbols
the original name, so if all that is available is the symbol, name
lookup may not work correctly with renaming.  And I think that
there are
other places where it happens.

In this case, it seems the caller can provide the local name, which
would avoid processing the use statements.  Did you try that?

Mikael





[pushed] c++: if consteval and consteval propagation [PR115583]

2024-07-28 Thread Jason Merrill
Tested x86_64-pc-linux-gnu, applying to trunk.

-- 8< --

During speculative constant folding of an if consteval, we take the false
branch, but the true branch is an immediate function context, so we don't
want to to cp_fold_immediate it.  So we could check IF_STMT_CONSTEVAL_P
here.  But beyond that, we don't want to do this inside a call, only when
first parsing a function.

PR c++/115583

gcc/cp/ChangeLog:

* constexpr.cc (cxx_eval_conditional_expression): Don't
cp_fold_immediate for if consteval.

gcc/testsuite/ChangeLog:

* g++.dg/cpp23/consteval-if13.C: New test.
---
 gcc/cp/constexpr.cc |  7 +--
 gcc/testsuite/g++.dg/cpp23/consteval-if13.C | 17 +
 2 files changed, 22 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp23/consteval-if13.C

diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index abd3b04ea7f..8277b3b79ba 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -3974,10 +3974,13 @@ cxx_eval_conditional_expression (const constexpr_ctx 
*ctx, tree t,
   if (TREE_CODE (t) == IF_STMT && !val)
 val = void_node;
 
-  /* P2564: a subexpression of a manifestly constant-evaluated expression
- or conversion is an immediate function context.  */
+  /* P2564: If we aren't in immediate function context (including a manifestly
+ constant-evaluated expression), check any uses of immediate functions in
+ the arm we're discarding.  But don't do this inside a call; we already
+ checked when parsing the function.  */
   if (ctx->manifestly_const_eval != mce_true
   && !in_immediate_context ()
+  && !ctx->call
   && cp_fold_immediate (&TREE_OPERAND (t, zero_p ? 1 : 2),
ctx->manifestly_const_eval))
 {
diff --git a/gcc/testsuite/g++.dg/cpp23/consteval-if13.C 
b/gcc/testsuite/g++.dg/cpp23/consteval-if13.C
new file mode 100644
index 000..b98bbc33d13
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp23/consteval-if13.C
@@ -0,0 +1,17 @@
+// PR c++/115583
+// { dg-do compile { target c++23 } }
+
+consteval int f(int i) {
+  return i;
+}
+const bool b = 0;
+constexpr int g(int i) {
+  if consteval {
+return f(i);
+  } else {
+return i;
+  }
+}
+int main() {
+  return g(1);
+}

base-commit: 027bde11d44d58e6ad473af3a69dc21b451bcbc0
-- 
2.45.2



[PATCH] Fix ICE when using -gcodeview with empty struct

2024-07-28 Thread Mark Harmstone
Empty structs result in empty LF_FIELDLIST types, which are valid, but
we weren't accounting for this and assuming they had to contain
subtypes.

gcc/
* dwarf2codeview.cc (get_type_num_struct): Fix NULL pointer dereference.
---
 gcc/dwarf2codeview.cc | 7 +--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/gcc/dwarf2codeview.cc b/gcc/dwarf2codeview.cc
index b16c6960f63..470cbae7110 100644
--- a/gcc/dwarf2codeview.cc
+++ b/gcc/dwarf2codeview.cc
@@ -2858,8 +2858,11 @@ get_type_num_struct (dw_die_ref type, bool in_struct, 
bool *is_fwd_ref)
   ct2 = ct->next;
   ct->next = NULL;
 
-  if (ct->lf_fieldlist.last_subtype->kind == LF_INDEX)
-   ct->lf_fieldlist.last_subtype->lf_index.type_num = last_type;
+  if (ct->lf_fieldlist.last_subtype
+ && ct->lf_fieldlist.last_subtype->kind == LF_INDEX)
+   {
+ ct->lf_fieldlist.last_subtype->lf_index.type_num = last_type;
+   }
 
   add_custom_type (ct);
   last_type = ct->num;
-- 
2.44.2



Re: [PATCH] LoongArch: Use iorn and andn standard pattern names.

2024-07-28 Thread Lulu Cheng



在 2024/7/28 上午3:30, Andrew Pinski 写道:

On Sat, Jul 27, 2024 at 1:55 AM Lulu Cheng  wrote:

gcc/ChangeLog:

 * config/loongarch/lasx.md (xvandn3): Rename to ...
 (andn3): This.
 (xvorn3): Rename to ...
 (iorn3): This.
 * config/loongarch/loongarch-builtins.cc (CODE_FOR_lsx_vandn_v):
 Defined as the modified name.
 (CODE_FOR_lsx_vorn_v): Likewise.
 (CODE_FOR_lasx_xvandn_v): Likewise.
 (CODE_FOR_lasx_xvorn_v): Likewise.
 * config/loongarch/loongarch.md (n): Rename to ...
 (n3): This.
 * config/loongarch/lsx.md (vandn3): Rename to ...
 (andn3): This. .
 (vorn3): Rename to ...
 (iorn3): This.


You might want to add a testcase like I did for aarch64:
https://gcc.gnu.org/pipermail/gcc-patches/2024-July/658219.html .

Thanks,
Andrew Pinski


Ok, I'll add a test case for this modification.

Thanks.




---
  gcc/config/loongarch/lasx.md   | 4 ++--
  gcc/config/loongarch/loongarch-builtins.cc | 8 
  gcc/config/loongarch/loongarch.md  | 2 +-
  gcc/config/loongarch/lsx.md| 4 ++--
  4 files changed, 9 insertions(+), 9 deletions(-)

diff --git a/gcc/config/loongarch/lasx.md b/gcc/config/loongarch/lasx.md
index 7bd61f8ed5b..c5fe04de86c 100644
--- a/gcc/config/loongarch/lasx.md
+++ b/gcc/config/loongarch/lasx.md
@@ -2716,7 +2716,7 @@ (define_insn "lasx_vext2xv_d_b"
 (set_attr "mode" "V4DI")])

  ;; Extend loongson-sx to loongson-asx.
-(define_insn "xvandn3"
+(define_insn "andn3"
[(set (match_operand:LASX 0 "register_operand" "=f")
 (and:LASX (not:LASX (match_operand:LASX 1 "register_operand" "f"))
 (match_operand:LASX 2 "register_operand" "f")))]
@@ -4637,7 +4637,7 @@ (define_insn "lasx_xvssrlrn__"
[(set_attr "type" "simd_int_arith")
 (set_attr "mode" "")])

-(define_insn "xvorn3"
+(define_insn "iorn3"
[(set (match_operand:ILASX 0 "register_operand" "=f")
 (ior:ILASX (not:ILASX (match_operand:ILASX 2 "register_operand" "f"))
(match_operand:ILASX 1 "register_operand" "f")))]
diff --git a/gcc/config/loongarch/loongarch-builtins.cc 
b/gcc/config/loongarch/loongarch-builtins.cc
index fbe46833c9b..f0de80d767b 100644
--- a/gcc/config/loongarch/loongarch-builtins.cc
+++ b/gcc/config/loongarch/loongarch-builtins.cc
@@ -458,8 +458,8 @@ AVAIL_ALL (lasx_frecipe, ISA_HAS_LASX && ISA_HAS_FRECIPE)
  #define CODE_FOR_lsx_vabsd_du CODE_FOR_lsx_vabsd_u_du
  #define CODE_FOR_lsx_vftint_wu_s CODE_FOR_lsx_vftint_u_wu_s
  #define CODE_FOR_lsx_vftint_lu_d CODE_FOR_lsx_vftint_u_lu_d
-#define CODE_FOR_lsx_vandn_v CODE_FOR_vandnv16qi3
-#define CODE_FOR_lsx_vorn_v CODE_FOR_vornv16qi3
+#define CODE_FOR_lsx_vandn_v CODE_FOR_andnv16qi3
+#define CODE_FOR_lsx_vorn_v CODE_FOR_iornv16qi3
  #define CODE_FOR_lsx_vneg_b CODE_FOR_vnegv16qi2
  #define CODE_FOR_lsx_vneg_h CODE_FOR_vnegv8hi2
  #define CODE_FOR_lsx_vneg_w CODE_FOR_vnegv4si2
@@ -692,8 +692,8 @@ AVAIL_ALL (lasx_frecipe, ISA_HAS_LASX && ISA_HAS_FRECIPE)
  #define CODE_FOR_lasx_xvrepli_w CODE_FOR_lasx_xvrepliv8si
  #define CODE_FOR_lasx_xvrepli_d CODE_FOR_lasx_xvrepliv4di

-#define CODE_FOR_lasx_xvandn_v CODE_FOR_xvandnv32qi3
-#define CODE_FOR_lasx_xvorn_v CODE_FOR_xvornv32qi3
+#define CODE_FOR_lasx_xvandn_v CODE_FOR_andnv32qi3
+#define CODE_FOR_lasx_xvorn_v CODE_FOR_iornv32qi3
  #define CODE_FOR_lasx_xvneg_b CODE_FOR_negv32qi2
  #define CODE_FOR_lasx_xvneg_h CODE_FOR_negv16hi2
  #define CODE_FOR_lasx_xvneg_w CODE_FOR_negv8si2
diff --git a/gcc/config/loongarch/loongarch.md 
b/gcc/config/loongarch/loongarch.md
index 459ad30b9bb..4e4ddd515c9 100644
--- a/gcc/config/loongarch/loongarch.md
+++ b/gcc/config/loongarch/loongarch.md
@@ -1668,7 +1668,7 @@ (define_insn "*norsi3_internal"
[(set_attr "type" "logical")
 (set_attr "mode" "SI")])

-(define_insn "n"
+(define_insn "n3"
[(set (match_operand:X 0 "register_operand" "=r")
 (neg_bitwise:X
 (not:X (match_operand:X 1 "register_operand" "r"))
diff --git a/gcc/config/loongarch/lsx.md b/gcc/config/loongarch/lsx.md
index 454cda47876..dcb667a6ce5 100644
--- a/gcc/config/loongarch/lsx.md
+++ b/gcc/config/loongarch/lsx.md
@@ -2344,7 +2344,7 @@ (define_insn_and_split "vec_concatv4sf"
  }
[(set_attr "mode" "V4SF")])

-(define_insn "vandn3"
+(define_insn "andn3"
[(set (match_operand:LSX 0 "register_operand" "=f")
 (and:LSX (not:LSX (match_operand:LSX 1 "register_operand" "f"))
  (match_operand:LSX 2 "register_operand" "f")))]
@@ -3028,7 +3028,7 @@ (define_insn "lsx_vssrlrn__"
[(set_attr "type" "simd_int_arith")
 (set_attr "mode" "")])

-(define_insn "vorn3"
+(define_insn "iorn3"
[(set (match_operand:ILSX 0 "register_operand" "=f")
 (ior:ILSX (not:ILSX (match_operand:ILSX 2 "register_operand" "f"))
   (match_operand:ILSX 1 "register_operand" "f")))]
--
2.39.3





[PATCH] RISC-V: NFC: Do not use zicond for pr105314 testcases

2024-07-28 Thread Xiao Zeng
gcc/testsuite/ChangeLog:

* gcc.target/riscv/pr105314-rtl.c: Skip zicond.
* gcc.target/riscv/pr105314-rtl32.c: Dotto.
* gcc.target/riscv/pr105314.c: Dotto.

Signed-off-by: Xiao Zeng 
---
 gcc/testsuite/gcc.target/riscv/pr105314-rtl.c   | 2 +-
 gcc/testsuite/gcc.target/riscv/pr105314-rtl32.c | 2 +-
 gcc/testsuite/gcc.target/riscv/pr105314.c   | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/gcc/testsuite/gcc.target/riscv/pr105314-rtl.c 
b/gcc/testsuite/gcc.target/riscv/pr105314-rtl.c
index 693291f4dbd..570918f9d9a 100644
--- a/gcc/testsuite/gcc.target/riscv/pr105314-rtl.c
+++ b/gcc/testsuite/gcc.target/riscv/pr105314-rtl.c
@@ -1,7 +1,7 @@
 /* PR rtl-optimization/105314 */
 /* { dg-do compile } */
 /* { dg-require-effective-target rv64 } */
-/* { dg-skip-if "" { *-*-* } { "-O0" "-Og" "-Os" "-Oz" "-flto" } } */
+/* { dg-skip-if "" { *-*-* } { "-march=*zicond*" "-O0" "-Og" "-Os" "-Oz" 
"-flto" } } */
 /* { dg-options "-fdump-rtl-ce1" } */
 
 long __RTL (startwith ("ce1"))
diff --git a/gcc/testsuite/gcc.target/riscv/pr105314-rtl32.c 
b/gcc/testsuite/gcc.target/riscv/pr105314-rtl32.c
index 9f9600f7679..018b6c43095 100644
--- a/gcc/testsuite/gcc.target/riscv/pr105314-rtl32.c
+++ b/gcc/testsuite/gcc.target/riscv/pr105314-rtl32.c
@@ -1,7 +1,7 @@
 /* PR rtl-optimization/105314 */
 /* { dg-do compile } */
 /* { dg-require-effective-target rv32 } */
-/* { dg-skip-if "" { *-*-* } { "-O0" "-Og" "-Os" "-Oz" "-flto" } } */
+/* { dg-skip-if "" { *-*-* } { "-march=*zicond*" "-O0" "-Og" "-Os" "-Oz" 
"-flto" } } */
 /* { dg-options "-fdump-rtl-ce1" } */
 
 long __RTL (startwith ("ce1"))
diff --git a/gcc/testsuite/gcc.target/riscv/pr105314.c 
b/gcc/testsuite/gcc.target/riscv/pr105314.c
index 1a7ea671791..75f6ecda2bb 100644
--- a/gcc/testsuite/gcc.target/riscv/pr105314.c
+++ b/gcc/testsuite/gcc.target/riscv/pr105314.c
@@ -1,6 +1,6 @@
 /* PR rtl-optimization/105314 */
 /* { dg-do compile } */
-/* { dg-skip-if "" { *-*-* } { "-O0" "-Og" "-Os" "-Oz" } } */
+/* { dg-skip-if "" { *-*-* } { "-march=*zicond*" "-O0" "-Og" "-Os" "-Oz" } } */
 /* { dg-options "-fdump-rtl-ce1" } */
 
 long
-- 
2.43.0



RE: [PATCH v2] i386: Fix AVX512 intrin macro typo

2024-07-28 Thread Jiang, Haochen
> -Original Message-
> From: Jakub Jelinek 
> Sent: Friday, July 26, 2024 7:59 PM
> To: Jiang, Haochen 
> Cc: gcc-patches@gcc.gnu.org; Liu, Hongtao ;
> ubiz...@gmail.com
> Subject: Re: [PATCH v2] i386: Fix AVX512 intrin macro typo
> 
> On Fri, Jul 26, 2024 at 04:10:48PM +0800, Haochen Jiang wrote:
> > * config/i386/avx512dqintrin.h
> > (_mm_mask_fpclass_ss_mask): Correct operand order.
> > (_mm_mask_fpclass_sd_mask): Ditto.
> > (_mm_reduce_round_sd): Use -1 as mask since it is non-mask.
> > (_mm_reduce_round_ss): Ditto.
> 
> You haven't mentioned the
>   (_mm_maskz_reduce_round_ss): Use
> __builtin_ia32_reducess_mask_round
>   instead of __builtin_ia32_reducesd_mask_round.
> change here.
> 
> > * config/i386/avx512vlbwintrin.h
> > (_mm256_mask_alignr_epi8): Correct operand usage.
> > (_mm_mask_alignr_epi8): Ditto.
> > * config/i386/avx512vlintrin.h (_mm_mask_alignr_epi64): Ditto.
> >
> > gcc/testsuite/ChangeLog:
> >
> > * gcc.target/i386/avx512bw-vpalignr-1b.c: New test.
> > * gcc.target/i386/avx512dq-vfpclasssd-1b.c: Ditto.
> > * gcc.target/i386/avx512dq-vfpcla-1b.c: Ditto.
> > * gcc.target/i386/avx512dq-vreducesd-1b.c: Ditto.
> > * gcc.target/i386/avx512dq-vreducess-1b.c: Ditto.
> > * gcc.target/i386/avx512vl-valignq-1b.c: Ditto.
> 
> I went through all the cases and agree with all the changes.
> 
> LGTM with the above ChangeLog nit fixed, for trunk/release branches, even for
> 14.2 if committed RSN.

Ok. I will commit them and backport them to GCC13 and GCC12 now. For GCC14,
we could wait for GCC14.3 since it has been a weekend passed and not that RSN.
But if it could be in GCC14.2, I will also happy for that.

Thx,
Haochen

> 
>   Jakub



Re: [PATCH] [x86]Refine constraint "Bk" to define_special_memory_constraint.

2024-07-28 Thread Hongtao Liu
On Thu, Jul 25, 2024 at 3:23 PM Hongtao Liu  wrote:
>
> On Wed, Jul 24, 2024 at 3:57 PM liuhongt  wrote:
> >
> > For below pattern, RA may still allocate r162 as v/k register, try to
> > reload for address with leaq __libc_tsd_CTYPE_B@gottpoff(%rip), %rsi
> > which result a linker error.
> >
> > (set (reg:DI 162)
> >  (mem/u/c:DI
> >(const:DI (unspec:DI
> >  [(symbol_ref:DI ("a") [flags 0x60]   > 0x7f621f6e1c60 a>)]
> >  UNSPEC_GOTNTPOFF))
> >
> > Quote from H.J for why linker issue an error.
> > >What do these do:
> > >
> > >leaq__libc_tsd_CTYPE_B@gottpoff(%rip), %rax
> > >vmovq   (%rax), %xmm0
> > >
> > >From x86-64 TLS psABI:
> > >
> > >The assembler generates for the x@gottpoff(%rip) expressions a R X86
> > >64 GOTTPOFF relocation for the symbol x which requests the linker to
> > >generate a GOT entry with a R X86 64 TPOFF64 relocation. The offset of
> > >the GOT entry relative to the end of the instruction is then used in
> > >the instruction. The R X86 64 TPOFF64 relocation is pro- cessed at
> > >program startup time by the dynamic linker by looking up the symbol x
> > >in the modules loaded at that point. The offset is written in the GOT
> > >entry and later loaded by the addq instruction.
> > >
> > >The above code sequence looks wrong to me.
> >
> > Bootstrapped and regtested on x86_64-pc-linux-gnu{-m32,}.
> > Ok for trunk and backport?
Committed and will backport after gcc14.2 is released.
> >
> > gcc/ChangeLog:
> >
> > PR target/116043
> > * config/i386/constraints.md (Bk): Refine to
> > define_special_memory_constraint.
> >
> > gcc/testsuite/ChangeLog:
> >
> > * gcc.target/i386/pr116043.c: New test.
> > ---
> >  gcc/config/i386/constraints.md   |  2 +-
> >  gcc/testsuite/gcc.target/i386/pr116043.c | 33 
> >  2 files changed, 34 insertions(+), 1 deletion(-)
> >  create mode 100644 gcc/testsuite/gcc.target/i386/pr116043.c
> >
> > diff --git a/gcc/config/i386/constraints.md b/gcc/config/i386/constraints.md
> > index 7508d7a58bd..b760e7c221a 100644
> > --- a/gcc/config/i386/constraints.md
> > +++ b/gcc/config/i386/constraints.md
> > @@ -187,7 +187,7 @@ (define_special_memory_constraint "Bm"
> >"@internal Vector memory operand."
> >(match_operand 0 "vector_memory_operand"))
> >
> > -(define_memory_constraint "Bk"
> > +(define_special_memory_constraint "Bk"
> >"@internal TLS address that allows insn using non-integer registers."
> >(and (match_operand 0 "memory_operand")
> > (not (match_test "ix86_gpr_tls_address_pattern_p (op)"
> > diff --git a/gcc/testsuite/gcc.target/i386/pr116043.c 
> > b/gcc/testsuite/gcc.target/i386/pr116043.c
> > new file mode 100644
> > index 000..76553496c10
> > --- /dev/null
> > +++ b/gcc/testsuite/gcc.target/i386/pr116043.c
> > @@ -0,0 +1,33 @@
> > +/* { dg-do compile } */
> > +/* { dg-options "-mavx512bf16 -O3" } */
> > +/* { dg-final { scan-assembler-not {(?n)lea.*@gottpoff} } } */
> > +
> > +extern __thread int a, c, i, j, k, l;
> > +int *b;
> > +struct d {
> > +  int e;
> > +} f, g;
> > +char *h;
> > +
> > +void m(struct d *n) {
> > +  b = &k;
> > +  for (; n->e; b++, n--) {
> > +i = b && a;
> > +if (i)
> > +  j = c;
> > +  }
> > +}
> > +
> > +char *o(struct d *n) {
> > +  for (; n->e;)
> > +return h;
> > +}
> > +
> > +int q() {
> > +  if (l)
> > +return 1;
> > +  int p = *o(&g);
> > +  m(&f);
> > +  m(&g);
> > +  l = p;
> > +}
> > --
> > 2.31.1
> >
>
>
> --
> BR,
> Hongtao



-- 
BR,
Hongtao


[RFC] RISC-V: Add support for Profiles RVA/B23.

2024-07-28 Thread Jiawei
This patch adds support for RISC-V RVA23 and RVB23 Profiles[1], 
which depend on the base RISC-V Profiles support[2].

[1] 
https://github.com/riscv/riscv-profiles/releases/tag/rva23-v0.4-rvb23-v0.1-internal-review
[2] https://gcc.gnu.org/pipermail/gcc-patches/2024-July/658082.html


gcc/ChangeLog:

* common/config/riscv/riscv-common.cc: New Profiles.

gcc/testsuite/ChangeLog:

* gcc.target/riscv/attribute-22.c: New test.
* gcc.target/riscv/attribute-23.c: New test.

---
 gcc/common/config/riscv/riscv-common.cc   | 20 +++
 gcc/testsuite/gcc.target/riscv/attribute-22.c | 11 ++
 gcc/testsuite/gcc.target/riscv/attribute-23.c | 10 ++
 3 files changed, 41 insertions(+)
 create mode 100644 gcc/testsuite/gcc.target/riscv/attribute-22.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/attribute-23.c

diff --git a/gcc/common/config/riscv/riscv-common.cc 
b/gcc/common/config/riscv/riscv-common.cc
index 23ae07fe2f3..e6e8adf5e1b 100644
--- a/gcc/common/config/riscv/riscv-common.cc
+++ b/gcc/common/config/riscv/riscv-common.cc
@@ -323,6 +323,9 @@ static const struct riscv_ext_version 
riscv_ext_version_table[] =
   {"zicclsm",  ISA_SPEC_CLASS_NONE, 1, 0},
   {"ziccrse",  ISA_SPEC_CLASS_NONE, 1, 0},
 
+  {"zimop",  ISA_SPEC_CLASS_NONE, 1, 0},
+  {"zcmop",  ISA_SPEC_CLASS_NONE, 1, 0},
+
   {"zicntr", ISA_SPEC_CLASS_NONE, 2, 0},
   {"zihpm",  ISA_SPEC_CLASS_NONE, 2, 0},
 
@@ -467,6 +470,23 @@ static const riscv_profiles riscv_profiles_table[] =
"_zicclsm_zic64b_za64rs_zihintpause_zba_zbb_zbs_zicbom_zicbop"
"_zicboz_zfhmin_zkt"},
 
+  /* RVA23 contains all mandatory base ISA for RVA22U64 and the new extension
+ 'v,zihintntl,zvfhmin,zvbb,zvkt,zicond,zimop,zcmop,zfa,zawrs' as mandatory
+ extensions.  */
+  {"RVA23U64", "rv64imafdcv_zicsr_zicntr_zihpm_ziccif_ziccrse_ziccamoa"
+   "_zicclsm_zic64b_za64rs_zihintpause_zba_zbb_zbs_zicbom_zicbop"
+   "_zicboz_zfhmin_zkt_zvfhmin_zvbb_zvkt_zihintntl_zicond_zimop_zcmop_zcb"
+   "_zfa_zawrs"},
+
+  
+  /* RVB23 contains all mandatory base ISA for RVA22U64 and the new extension
+ 'zihintntl,zicond,zimop,zcmop,zfa,zawrs' as mandatory
+ extensions.  */
+  {"RVB23U64", "rv64imafdc_zicsr_zicntr_zihpm_ziccif_ziccrse_ziccamoa"
+   "_zicclsm_zic64b_za64rs_zihintpause_zba_zbb_zbs_zicbom_zicbop"
+   "_zicboz_zfhmin_zkt_zihintntl_zicond_zimop_zcmop_zcb"
+   "_zfa_zawrs"},
+
   /* Currently we do not define S/M mode Profiles in gcc part.  */
 
   /* Terminate the list.  */
diff --git a/gcc/testsuite/gcc.target/riscv/attribute-22.c 
b/gcc/testsuite/gcc.target/riscv/attribute-22.c
new file mode 100644
index 000..0bbb3242ddd
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/attribute-22.c
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+/* { dg-options "-march=RVA23U64 -mabi=lp64" } */
+
+void foo(){}
+
+/* { dg-final { scan-assembler ".attribute arch, 
\"rv64i2p1_m2p0_a2p1_f2p2_d2p2_c2p0"
+"_b1p0_v1p0_zic64b1p0_zicbom1p0_zicbop1p0_zicboz1p0_ziccamoa1p0_ziccif1p0_zicclsm1p0"
+_ziccrse1p0_zicntr2p0_zicond1p0_zicsr2p0_zihintntl1p0_zihintpause2p0_zihpm2p0_zimop1p0"
+_za64rs1p0_zaamo1p0_zalrsc1p0_zawrs1p0_zfa1p0_zfhmin1p0_zca1p0_zcb1p0_zcd1p0_zcmop1p0"
+_zba1p0_zbb1p0_zbs1p0_zkt1p0_zvbb1p0_zve32f1p0_zve32x1p0_zve64d1p0_zve64f1p0_zve64x1p0"
+_zvfhmin1p0_zvkb1p0_zvkt1p0_zvl128b1p0_zvl32b1p0_zvl64b1p0\"" } } */
diff --git a/gcc/testsuite/gcc.target/riscv/attribute-23.c 
b/gcc/testsuite/gcc.target/riscv/attribute-23.c
new file mode 100644
index 000..459b5641ca3
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/attribute-23.c
@@ -0,0 +1,10 @@
+/* { dg-do compile } */
+/* { dg-options "-march=RVB23U64 -mabi=lp64" } */
+
+void foo(){}
+
+/* { dg-final { scan-assembler ".attribute arch, 
\"rv64i2p1_m2p0_a2p1_f2p2_d2p2_c2p0"
+"_b1p0_zic64b1p0_zicbom1p0_zicbop1p0_zicboz1p0_ziccamoa1p0_ziccif1p0_zicclsm1p0_ziccrse1p0"
+"_zicntr2p0_zicond1p0_zicsr2p0_zihintntl1p0_zihintpause2p0_zihpm2p0_zimop1p0_za64rs1p0"
+"_zaamo1p0_zalrsc1p0_zawrs1p0_zfa1p0_zfhmin1p0_zca1p0_zcb1p0_zcd1p0_zcmop1p0_zba1p0"
+"_zbb1p0_zbs1p0_zkt1p0\"" } } */
-- 
2.25.1



[PATCH V7 1/3] split complicate 64bit constant to memory

2024-07-28 Thread Jiufu Guo
Hi,

Sometimes, a complicated constant is built via 3(or more)
instructions.  Generally speaking, it would not be as fast
as loading it from the constant pool (as the discussions in
PR63281):
"ld" is one instruction.  If consider "address/toc" adjust,
we may count it as 2 instructions. And "pld" may need fewer
cycles.

Adding --param=rs6000-min-insns-constant-in-pool helps to
control the instruction number threshold for different scenarios.

As known, because the constant is load from memory by this
patch,  so this functionality may affect the cache missing.
While, IMHO, this patch would be still do the right thing.

Compare with the previous version:
This patch serie adds one more patch to tune the threshold
for power10.

Boostrap & regtest pass on ppc64{,le}.
Is this ok for trunk?

BR,
Jeff (Jiufu Guo)

PR target/63281

gcc/ChangeLog:

* config/rs6000/rs6000.cc (rs6000_emit_set_const): Split constant to
memory for -m64.
* config/rs6000/rs6000.opt (rs6000-min-insns-constant-in-pool): New
parameter.

gcc/testsuite/ChangeLog:

* gcc.target/powerpc/const_anchors.c: Test final-rtl.
* gcc.target/powerpc/parall_5insn_const.c: Add option
--param=rs6000-min-insns-constant-in-pool=5 to keep the original test.
* gcc.target/powerpc/pr106550.c: Likewise.
* gcc.target/powerpc/pr106550_1.c: Likewise.
* gcc.target/powerpc/pr93012.c: Likewise.
* gcc.target/powerpc/pr87870.c: Update instruction counts.
* gcc.target/powerpc/pr63281.c: New test.


---
 gcc/config/rs6000/rs6000.cc   | 19 +++
 gcc/config/rs6000/rs6000.opt  |  5 +
 .../gcc.target/powerpc/const_anchors.c|  5 +++--
 .../gcc.target/powerpc/parall_5insn_const.c   |  2 +-
 gcc/testsuite/gcc.target/powerpc/pr106550.c   |  2 +-
 gcc/testsuite/gcc.target/powerpc/pr106550_1.c |  2 +-
 gcc/testsuite/gcc.target/powerpc/pr63281.c| 11 +++
 gcc/testsuite/gcc.target/powerpc/pr87870.c|  5 -
 gcc/testsuite/gcc.target/powerpc/pr93012.c|  2 +-
 9 files changed, 46 insertions(+), 7 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/powerpc/pr63281.c

diff --git a/gcc/config/rs6000/rs6000.cc b/gcc/config/rs6000/rs6000.cc
index 2046a831938..ec384e87868 100644
--- a/gcc/config/rs6000/rs6000.cc
+++ b/gcc/config/rs6000/rs6000.cc
@@ -10240,6 +10240,25 @@ rs6000_emit_set_const (rtx dest, rtx source)
  c = sext_hwi (c, 32);
  emit_move_insn (lo, GEN_INT (c));
}
+
+  /* Use base_reg_operand to avoid spliting "r0=xxx" to "r0=[r0+off]"
+after RA when reusing the DEST register to build the value.  */
+  else if ((can_create_pseudo_p () || base_reg_operand (dest, mode))
+  && num_insns_constant (source, mode)
+   > rs6000_min_insns_constant_in_pool
+  && TARGET_64BIT)
+   {
+ rtx sym = force_const_mem (mode, source);
+ if (TARGET_TOC && SYMBOL_REF_P (XEXP (sym, 0))
+ && use_toc_relative_ref (XEXP (sym, 0), mode))
+   {
+ rtx toc = create_TOC_reference (XEXP (sym, 0), dest);
+ sym = gen_const_mem (mode, toc);
+ set_mem_alias_set (sym, get_TOC_alias_set ());
+   }
+
+ emit_move_insn (dest, sym);
+   }
   else
rs6000_emit_set_long_const (dest, c);
   break;
diff --git a/gcc/config/rs6000/rs6000.opt b/gcc/config/rs6000/rs6000.opt
index e8ca70340df..a1c0d1e89c5 100644
--- a/gcc/config/rs6000/rs6000.opt
+++ b/gcc/config/rs6000/rs6000.opt
@@ -679,3 +679,8 @@ default value is 4.
 Target Undocumented Joined UInteger Var(rs6000_vect_unroll_reduc_threshold) 
Init(1) Param
 When reduction factor computed for a loop exceeds the threshold specified by
 this parameter, prefer to unroll this loop.  The default value is 1.
+
+-param=rs6000-min-insns-constant-in-pool=
+Target Undocumented Joined UInteger Var(rs6000_min_insns_constant_in_pool) 
Init(2) IntegerRange(2, 5) Param
+The minimum instruction number of building a constant to force loading it from
+the constant pool.
\ No newline at end of file
diff --git a/gcc/testsuite/gcc.target/powerpc/const_anchors.c 
b/gcc/testsuite/gcc.target/powerpc/const_anchors.c
index 542e2674b12..682e773d506 100644
--- a/gcc/testsuite/gcc.target/powerpc/const_anchors.c
+++ b/gcc/testsuite/gcc.target/powerpc/const_anchors.c
@@ -1,5 +1,5 @@
 /* { dg-do compile { target has_arch_ppc64 } } */
-/* { dg-options "-O2" } */
+/* { dg-options "-O2 -fdump-rtl-final" } */
 
 #define C1 0x2351847027482577ULL
 #define C2 0x2351847027482578ULL
@@ -17,4 +17,5 @@ void __attribute__ ((noinline)) foo1 (long long *a, long long 
b)
 *a++ = C2;
 }
 
-/* { dg-final { scan-assembler-times {\maddi\M} 2 } } */
+/* { dg-final { scan-rtl-dump-times {\madddi3\M} 2 "final" } } */
+
diff --git a/gcc/testsuite/gcc.target/powerpc/parall_5insn_const.c 
b/gcc/testsuite/gcc.target/powerpc/parall_5insn_const.c
index e3a9a7264cf

[PATCH V7 3/3] slight tune heuristic min_insns_constant_in_pool

2024-07-28 Thread Jiufu Guo
Hi,

The default rs6000_min_insns_constant_in_pool is 2, which would suitable
for a few cases.  While for different targets, we can tune this threshold
slightly.

For example, 'pld' could load a constant value from memory as faster as
building a constant by one instruction, and faster than build a constant
through two instructions.

This patch tune the parameter rs6000_min_insns_constant_in_pool for
TARGET_PREFIXED and TARGET_32BIT.

Bootstrap and regtest pass on ppc64 and ppc64le.
Is this patch ok for trunk?

BR,
Jeff(Jiufu) Guo

gcc/ChangeLog:

* config/rs6000/rs6000.cc (rs6000_option_override_internal): Tune
rs6000_min_insns_constant_in_pool.

gcc/testsuite/ChangeLog:

* gcc.target/powerpc/const-build-1.c: Keep original test point
by using rs6000_min_insns_constant_in_pool.
* gcc.target/powerpc/const-build.c: Likewise.

---
 gcc/config/rs6000/rs6000.cc  | 15 +++
 gcc/testsuite/gcc.target/powerpc/const-build-1.c |  2 +-
 gcc/testsuite/gcc.target/powerpc/const-build.c   |  2 +-
 3 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/gcc/config/rs6000/rs6000.cc b/gcc/config/rs6000/rs6000.cc
index c785fb20b1b..632760dc814 100644
--- a/gcc/config/rs6000/rs6000.cc
+++ b/gcc/config/rs6000/rs6000.cc
@@ -4847,6 +4847,21 @@ rs6000_option_override_internal (bool global_init_p)
 warning (0, "%qs is deprecated and not recommended in any circumstances",
 "-mno-speculate-indirect-jumps");
 
+  /* Set the threshold for how complicated constant should be put
+into constant pool.  */
+  if (!OPTION_SET_P (rs6000_min_insns_constant_in_pool))
+{
+  /* one insn could load for the constant: pld 3, .LC1@pcrel
+ld 0,.LC1@toc(2). */
+  if (TARGET_PREFIXED || (TARGET_64BIT && TARGET_CMODEL == CMODEL_SMALL))
+   rs6000_min_insns_constant_in_pool = 1;
+
+  /* Consider address addjust, slight increase the threshold:
+   add 30,0,30; lwz 9,.LC1-.LCTOC1(30); ld 9,0(9); or
+   lis 9,.LC0@ha; la 9,.LC0@l(9); ld 9,0(9)*/
+  else if (TARGET_32BIT && flag_pic != 1)
+   rs6000_min_insns_constant_in_pool = 3;
+}
   return ret;
 }
 
diff --git a/gcc/testsuite/gcc.target/powerpc/const-build-1.c 
b/gcc/testsuite/gcc.target/powerpc/const-build-1.c
index 7e35f8c507f..e7061709fea 100644
--- a/gcc/testsuite/gcc.target/powerpc/const-build-1.c
+++ b/gcc/testsuite/gcc.target/powerpc/const-build-1.c
@@ -1,5 +1,5 @@
 /* { dg-do compile { target lp64 } } */
-/* { dg-options "-O2 -mdejagnu-cpu=power10" } */
+/* { dg-options "-O2 -mdejagnu-cpu=power10 
--param=rs6000-min-insns-constant-in-pool=2" } */
 /* { dg-require-effective-target power10_ok } */
 
 unsigned long long msk66() { return 0xULL; }
diff --git a/gcc/testsuite/gcc.target/powerpc/const-build.c 
b/gcc/testsuite/gcc.target/powerpc/const-build.c
index 52941ca4c2f..31e1a611243 100644
--- a/gcc/testsuite/gcc.target/powerpc/const-build.c
+++ b/gcc/testsuite/gcc.target/powerpc/const-build.c
@@ -1,5 +1,5 @@
 /* { dg-do run } */
-/* { dg-options "-O2 -save-temps" } */
+/* { dg-options "-O2 -save-temps --param=rs6000-min-insns-constant-in-pool=2" 
} */
 /* { dg-require-effective-target has_arch_ppc64 } */
 
 /* Verify that two instructions are successfully used to build constants.
-- 
2.43.0



[PATCH V7 2/3] split complicate 64bit constant to memory for -m32 -mpowerpc64

2024-07-28 Thread Jiufu Guo
Hi,

For "-m32 -mpowerpc64", it is also ok to use fewer instruciton (p?ld)
to loading 64bit constant from memory. So, splitting the complicate 64bit
constant to constant pool should also work for this case.

Bootstrap and regtest pass on ppc64{,le}.
Also no regression for "-m32 -mpowerpc64" variation on ppc64.
Is this ok for trunk?

BR,
Jeff(Jiufu) Guo

gcc/ChangeLog:

* config/rs6000/rs6000.cc (rs6000_emit_set_const): Split constant to
pool for "-m32 -mpowerpc64".

gcc/testsuite/ChangeLog:

* gcc.target/powerpc/pr63281.c: Allow checking -m32.

---
 gcc/config/rs6000/rs6000.cc| 21 +++--
 gcc/testsuite/gcc.target/powerpc/pr63281.c |  2 +-
 2 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/gcc/config/rs6000/rs6000.cc b/gcc/config/rs6000/rs6000.cc
index ec384e87868..c785fb20b1b 100644
--- a/gcc/config/rs6000/rs6000.cc
+++ b/gcc/config/rs6000/rs6000.cc
@@ -10245,8 +10245,7 @@ rs6000_emit_set_const (rtx dest, rtx source)
 after RA when reusing the DEST register to build the value.  */
   else if ((can_create_pseudo_p () || base_reg_operand (dest, mode))
   && num_insns_constant (source, mode)
-   > rs6000_min_insns_constant_in_pool
-  && TARGET_64BIT)
+   > rs6000_min_insns_constant_in_pool)
{
  rtx sym = force_const_mem (mode, source);
  if (TARGET_TOC && SYMBOL_REF_P (XEXP (sym, 0))
@@ -10256,6 +10255,24 @@ rs6000_emit_set_const (rtx dest, rtx source)
  sym = gen_const_mem (mode, toc);
  set_mem_alias_set (sym, get_TOC_alias_set ());
}
+ else if (TARGET_32BIT)
+   {
+ /* After RA, reuse 'DEST' reg.  */
+ rtx addr = can_create_pseudo_p ()
+  ? gen_reg_rtx (Pmode)
+  : gen_rtx_REG (Pmode, REGNO (dest));
+ rtx sym_ref = XEXP (sym, 0);
+ if (flag_pic)
+   emit_move_insn (addr, sym_ref);
+ else
+   {
+ emit_move_insn (addr, gen_rtx_HIGH (Pmode, sym_ref));
+ emit_move_insn (addr, gen_rtx_LO_SUM (Pmode, addr, sym_ref));
+   }
+ rtx mem = gen_rtx_MEM (mode, addr);
+ MEM_COPY_ATTRIBUTES (mem, sym);
+ sym = mem;
+   }
 
  emit_move_insn (dest, sym);
}
diff --git a/gcc/testsuite/gcc.target/powerpc/pr63281.c 
b/gcc/testsuite/gcc.target/powerpc/pr63281.c
index 9763a7181fc..16a93b78606 100644
--- a/gcc/testsuite/gcc.target/powerpc/pr63281.c
+++ b/gcc/testsuite/gcc.target/powerpc/pr63281.c
@@ -7,5 +7,5 @@ foo (unsigned long long *a)
   *a++ = 0x2351847027482577ULL;
 }
 
-/* { dg-final { scan-assembler-times {\mp?ld\M} 1 { target { lp64 } } } } */
+/* { dg-final { scan-assembler-times {\mp?ld\M} 1 { target { has_arch_ppc64 } 
} } } */
 
-- 
2.45.2



[r13-8949 Regression] FAIL: gcc.target/i386/avx512dq-vreducess-1b.c (test for excess errors) on Linux/x86_64

2024-07-28 Thread haochen.jiang
On Linux/x86_64,

bb15c4cf21dbe76df5a225342d1fbe8ecd3c7971 is the first bad commit
commit bb15c4cf21dbe76df5a225342d1fbe8ecd3c7971
Author: Haochen Jiang 
Date:   Thu Jul 25 16:12:20 2024 +0800

i386: Fix AVX512 intrin macro typo

caused

FAIL: gcc.target/i386/avx512dq-vreducesd-1b.c (test for excess errors)
FAIL: gcc.target/i386/avx512dq-vreducess-1b.c (test for excess errors)

with GCC configured with

../../gcc/configure 
--prefix=/export/users/haochenj/src/gcc-bisect/gcc-13/releases/gcc-13/r13-8949/usr
 --enable-clocale=gnu --with-system-zlib --with-demangler-in-ld 
--with-fpmath=sse --enable-languages=c,c++,fortran --enable-cet --without-isl 
--enable-libmpx x86_64-linux --disable-bootstrap

To reproduce:

$ cd {build_dir}/gcc && make check 
RUNTESTFLAGS="i386.exp=gcc.target/i386/avx512dq-vreducesd-1b.c 
--target_board='unix{-m32}'"
$ cd {build_dir}/gcc && make check 
RUNTESTFLAGS="i386.exp=gcc.target/i386/avx512dq-vreducesd-1b.c 
--target_board='unix{-m32\ -march=cascadelake}'"
$ cd {build_dir}/gcc && make check 
RUNTESTFLAGS="i386.exp=gcc.target/i386/avx512dq-vreducesd-1b.c 
--target_board='unix{-m64}'"
$ cd {build_dir}/gcc && make check 
RUNTESTFLAGS="i386.exp=gcc.target/i386/avx512dq-vreducesd-1b.c 
--target_board='unix{-m64\ -march=cascadelake}'"
$ cd {build_dir}/gcc && make check 
RUNTESTFLAGS="i386.exp=gcc.target/i386/avx512dq-vreducess-1b.c 
--target_board='unix{-m32}'"
$ cd {build_dir}/gcc && make check 
RUNTESTFLAGS="i386.exp=gcc.target/i386/avx512dq-vreducess-1b.c 
--target_board='unix{-m32\ -march=cascadelake}'"
$ cd {build_dir}/gcc && make check 
RUNTESTFLAGS="i386.exp=gcc.target/i386/avx512dq-vreducess-1b.c 
--target_board='unix{-m64}'"
$ cd {build_dir}/gcc && make check 
RUNTESTFLAGS="i386.exp=gcc.target/i386/avx512dq-vreducess-1b.c 
--target_board='unix{-m64\ -march=cascadelake}'"

(Please do not reply to this email, for question about this report, contact me 
at haochen dot jiang at intel.com.)
(If you met problems with cascadelake related, disabling AVX512F in command 
line might save that.)
(However, please make sure that there is no potential problems with AVX512.)


RE: [r13-8949 Regression] FAIL: gcc.target/i386/avx512dq-vreducess-1b.c (test for excess errors) on Linux/x86_64

2024-07-28 Thread Jiang, Haochen
Ah... When I solved the huge conflict this morning due to AVX10 refactor for
GCC13/12, I forgot that it is in GCC14 AVX10.1 refactor when I added the
_mm_avx512_setzero_ps/pd. Should use _mm_setzero_ps/pd instead.

Never do something tweak quickly on a sleepy Monday morning.

Thx,
Haochen

> -Original Message-
> From: haochen.jiang 
> Sent: Monday, July 29, 2024 1:28 PM
> To: Jiang, Haochen ; gcc-regress...@gcc.gnu.org;
> gcc-patches@gcc.gnu.org
> Subject: [r13-8949 Regression] FAIL: gcc.target/i386/avx512dq-vreducess-
> 1b.c (test for excess errors) on Linux/x86_64
> 
> On Linux/x86_64,
> 
> bb15c4cf21dbe76df5a225342d1fbe8ecd3c7971 is the first bad commit
> commit bb15c4cf21dbe76df5a225342d1fbe8ecd3c7971
> Author: Haochen Jiang 
> Date:   Thu Jul 25 16:12:20 2024 +0800
> 
> i386: Fix AVX512 intrin macro typo
> 
> caused
> 
> FAIL: gcc.target/i386/avx512dq-vreducesd-1b.c (test for excess errors)
> FAIL: gcc.target/i386/avx512dq-vreducess-1b.c (test for excess errors)
> 
> with GCC configured with
> 
> ../../gcc/configure --prefix=/export/users/haochenj/src/gcc-bisect/gcc-
> 13/releases/gcc-13/r13-8949/usr --enable-clocale=gnu --with-system-zlib --
> with-demangler-in-ld --with-fpmath=sse --enable-languages=c,c++,fortran --
> enable-cet --without-isl --enable-libmpx x86_64-linux --disable-bootstrap
> 
> To reproduce:
> 
> $ cd {build_dir}/gcc && make check
> RUNTESTFLAGS="i386.exp=gcc.target/i386/avx512dq-vreducesd-1b.c --
> target_board='unix{-m32}'"
> $ cd {build_dir}/gcc && make check
> RUNTESTFLAGS="i386.exp=gcc.target/i386/avx512dq-vreducesd-1b.c --
> target_board='unix{-m32\ -march=cascadelake}'"
> $ cd {build_dir}/gcc && make check
> RUNTESTFLAGS="i386.exp=gcc.target/i386/avx512dq-vreducesd-1b.c --
> target_board='unix{-m64}'"
> $ cd {build_dir}/gcc && make check
> RUNTESTFLAGS="i386.exp=gcc.target/i386/avx512dq-vreducesd-1b.c --
> target_board='unix{-m64\ -march=cascadelake}'"
> $ cd {build_dir}/gcc && make check
> RUNTESTFLAGS="i386.exp=gcc.target/i386/avx512dq-vreducess-1b.c --
> target_board='unix{-m32}'"
> $ cd {build_dir}/gcc && make check
> RUNTESTFLAGS="i386.exp=gcc.target/i386/avx512dq-vreducess-1b.c --
> target_board='unix{-m32\ -march=cascadelake}'"
> $ cd {build_dir}/gcc && make check
> RUNTESTFLAGS="i386.exp=gcc.target/i386/avx512dq-vreducess-1b.c --
> target_board='unix{-m64}'"
> $ cd {build_dir}/gcc && make check
> RUNTESTFLAGS="i386.exp=gcc.target/i386/avx512dq-vreducess-1b.c --
> target_board='unix{-m64\ -march=cascadelake}'"
> 
> (Please do not reply to this email, for question about this report, contact 
> me at
> haochen dot jiang at intel.com.) (If you met problems with cascadelake
> related, disabling AVX512F in command line might save that.) (However,
> please make sure that there is no potential problems with AVX512.)


[GCC12/13 PATCH] i386: Use _mm_setzero_ps/d instead of _mm_avx512_setzero_ps/d for GCC13/12

2024-07-28 Thread Haochen Jiang
Hi all,

In GCC13/12, there is no _mm_avx512_setzero_ps/d since it is introduced
in GCC14.

Fix the backport issue as obvious in:
https://gcc.gnu.org/pipermail/gcc-regression/2024-July/080385.html

Thx,
Haochen

gcc/ChangeLog:

* config/i386/avx512dqintrin.h (_mm_reduce_round_sd): Use
_mm_setzero_pd instead of _mm_avx512_setzero_pd.
(_mm_reduce_round_ss): Use _mm_setzero_ps instead of
_mm_avx512_setzero_ps.
---
 gcc/config/i386/avx512dqintrin.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/gcc/config/i386/avx512dqintrin.h b/gcc/config/i386/avx512dqintrin.h
index 4383ff14670..82f4acc7d2e 100644
--- a/gcc/config/i386/avx512dqintrin.h
+++ b/gcc/config/i386/avx512dqintrin.h
@@ -2840,7 +2840,7 @@ _mm512_fpclass_ps_mask (__m512 __A, const int __imm)
 
 #define _mm_reduce_round_sd(A, B, C, R)   \
   ((__m128d) __builtin_ia32_reducesd_mask_round ((__v2df)(__m128d)(A), \
-(__v2df)(__m128d)(B), (int)(C), (__v2df) _mm_avx512_setzero_pd (), \
+(__v2df)(__m128d)(B), (int)(C), (__v2df) _mm_setzero_pd (),   \
 (__mmask8)(-1), (int)(R)))
 
 #define _mm_mask_reduce_round_sd(W, U, A, B, C, R)\
@@ -2869,7 +2869,7 @@ _mm512_fpclass_ps_mask (__m512 __A, const int __imm)
 
 #define _mm_reduce_round_ss(A, B, C, R)   \
   ((__m128) __builtin_ia32_reducess_mask_round ((__v4sf)(__m128)(A),   \
-(__v4sf)(__m128)(B), (int)(C), (__v4sf) _mm_avx512_setzero_ps (),  \
+(__v4sf)(__m128)(B), (int)(C), (__v4sf) _mm_setzero_ps (),\
 (__mmask8)(-1), (int)(R)))
 
 #define _mm_mask_reduce_round_ss(W, U, A, B, C, R)\
-- 
2.31.1