[gcc r15-3672] c++: crash with anon VAR_DECL [PR116676]

2024-09-17 Thread Marek Polacek via Gcc-cvs
https://gcc.gnu.org/g:dfe0d4389a3ce43179563a63046ad3e74d615a08

commit r15-3672-gdfe0d4389a3ce43179563a63046ad3e74d615a08
Author: Marek Polacek 
Date:   Mon Sep 16 16:42:38 2024 -0400

c++: crash with anon VAR_DECL [PR116676]

r12-3495 added maybe_warn_about_constant_value which will crash if
it gets a nameless VAR_DECL, which is what happens in this PR.

We created this VAR_DECL in cp_parser_decomposition_declaration.

PR c++/116676

gcc/cp/ChangeLog:

* constexpr.cc (maybe_warn_about_constant_value): Check DECL_NAME.

gcc/testsuite/ChangeLog:

* g++.dg/cpp1z/constexpr-116676.C: New test.

Reviewed-by: Jason Merrill 

Diff:
---
 gcc/cp/constexpr.cc   |  1 +
 gcc/testsuite/g++.dg/cpp1z/constexpr-116676.C | 57 +++
 2 files changed, 58 insertions(+)

diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index d0f617481413..c3668b0d7d3f 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -7222,6 +7222,7 @@ maybe_warn_about_constant_value (location_t loc, tree 
decl)
   && warn_interference_size
   && !OPTION_SET_P (param_destruct_interfere_size)
   && DECL_CONTEXT (decl) == std_node
+  && DECL_NAME (decl)
   && id_equal (DECL_NAME (decl), "hardware_destructive_interference_size")
   && (LOCATION_FILE (input_location) != main_input_filename
  || module_exporting_p ())
diff --git a/gcc/testsuite/g++.dg/cpp1z/constexpr-116676.C 
b/gcc/testsuite/g++.dg/cpp1z/constexpr-116676.C
new file mode 100644
index ..1cb65f10a1d1
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/constexpr-116676.C
@@ -0,0 +1,57 @@
+// PR c++/116676
+// { dg-do compile { target c++17 } }
+
+namespace std {
+typedef __SIZE_TYPE__ size_t;
+
+  template
+struct remove_reference
+{ typedef _Tp type; };
+
+  template
+struct remove_reference<_Tp&>
+{ typedef _Tp type; };
+
+  template
+struct remove_reference<_Tp&&>
+{ typedef _Tp type; };
+
+template 
+constexpr typename std::remove_reference<_Tp>::type &&
+move(_Tp &&__t) noexcept {
+  return static_cast::type &&>(__t);
+}
+template  struct tuple_size;
+template  struct tuple_element;
+template  class __pair_base {};
+template 
+struct pair {
+  _T1 first;
+  _T2 second;
+  template 
+  explicit constexpr pair(const _T1 &__a, const _T2 &__b)
+  : first(__a), second(__b) {}
+};
+template 
+struct tuple_size>
+{
+static constexpr size_t value = 2;
+};
+template 
+struct tuple_element<0, pair<_Tp1, _Tp2>> {
+  typedef _Tp1 type;
+};
+template 
+struct tuple_element<1, pair<_Tp1, _Tp2>> {
+  typedef _Tp2 type;
+};
+
+template 
+constexpr typename tuple_element<_Int, pair<_Tp1, _Tp2>>::type &
+get(pair<_Tp1, _Tp2> &&__in) noexcept {
+  return (std::move(__in).first);
+}
+int t;
+auto [a, b] = std::pair{t, 1};
+}
+


[gcc r14-10675] c++: crash with anon VAR_DECL [PR116676]

2024-09-17 Thread Marek Polacek via Gcc-cvs
https://gcc.gnu.org/g:f1dc18250d82cd123fcf9aef0a95608e4ec63d58

commit r14-10675-gf1dc18250d82cd123fcf9aef0a95608e4ec63d58
Author: Marek Polacek 
Date:   Mon Sep 16 16:42:38 2024 -0400

c++: crash with anon VAR_DECL [PR116676]

r12-3495 added maybe_warn_about_constant_value which will crash if
it gets a nameless VAR_DECL, which is what happens in this PR.

We created this VAR_DECL in cp_parser_decomposition_declaration.

PR c++/116676

gcc/cp/ChangeLog:

* constexpr.cc (maybe_warn_about_constant_value): Check DECL_NAME.

gcc/testsuite/ChangeLog:

* g++.dg/cpp1z/constexpr-116676.C: New test.

Reviewed-by: Jason Merrill 
(cherry picked from commit dfe0d4389a3ce43179563a63046ad3e74d615a08)

Diff:
---
 gcc/cp/constexpr.cc   |  1 +
 gcc/testsuite/g++.dg/cpp1z/constexpr-116676.C | 57 +++
 2 files changed, 58 insertions(+)

diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index 9b36f7628f36..853694d78a56 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -7225,6 +7225,7 @@ maybe_warn_about_constant_value (location_t loc, tree 
decl)
   && warn_interference_size
   && !OPTION_SET_P (param_destruct_interfere_size)
   && DECL_CONTEXT (decl) == std_node
+  && DECL_NAME (decl)
   && id_equal (DECL_NAME (decl), "hardware_destructive_interference_size")
   && (LOCATION_FILE (input_location) != main_input_filename
  || module_exporting_p ())
diff --git a/gcc/testsuite/g++.dg/cpp1z/constexpr-116676.C 
b/gcc/testsuite/g++.dg/cpp1z/constexpr-116676.C
new file mode 100644
index ..1cb65f10a1d1
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/constexpr-116676.C
@@ -0,0 +1,57 @@
+// PR c++/116676
+// { dg-do compile { target c++17 } }
+
+namespace std {
+typedef __SIZE_TYPE__ size_t;
+
+  template
+struct remove_reference
+{ typedef _Tp type; };
+
+  template
+struct remove_reference<_Tp&>
+{ typedef _Tp type; };
+
+  template
+struct remove_reference<_Tp&&>
+{ typedef _Tp type; };
+
+template 
+constexpr typename std::remove_reference<_Tp>::type &&
+move(_Tp &&__t) noexcept {
+  return static_cast::type &&>(__t);
+}
+template  struct tuple_size;
+template  struct tuple_element;
+template  class __pair_base {};
+template 
+struct pair {
+  _T1 first;
+  _T2 second;
+  template 
+  explicit constexpr pair(const _T1 &__a, const _T2 &__b)
+  : first(__a), second(__b) {}
+};
+template 
+struct tuple_size>
+{
+static constexpr size_t value = 2;
+};
+template 
+struct tuple_element<0, pair<_Tp1, _Tp2>> {
+  typedef _Tp1 type;
+};
+template 
+struct tuple_element<1, pair<_Tp1, _Tp2>> {
+  typedef _Tp2 type;
+};
+
+template 
+constexpr typename tuple_element<_Int, pair<_Tp1, _Tp2>>::type &
+get(pair<_Tp1, _Tp2> &&__in) noexcept {
+  return (std::move(__in).first);
+}
+int t;
+auto [a, b] = std::pair{t, 1};
+}
+


[gcc r13-9031] c++: crash with anon VAR_DECL [PR116676]

2024-09-17 Thread Marek Polacek via Gcc-cvs
https://gcc.gnu.org/g:c8682dd76e31ee4bc4dede23c78d6d66de350e83

commit r13-9031-gc8682dd76e31ee4bc4dede23c78d6d66de350e83
Author: Marek Polacek 
Date:   Mon Sep 16 16:42:38 2024 -0400

c++: crash with anon VAR_DECL [PR116676]

r12-3495 added maybe_warn_about_constant_value which will crash if
it gets a nameless VAR_DECL, which is what happens in this PR.

We created this VAR_DECL in cp_parser_decomposition_declaration.

PR c++/116676

gcc/cp/ChangeLog:

* constexpr.cc (maybe_warn_about_constant_value): Check DECL_NAME.

gcc/testsuite/ChangeLog:

* g++.dg/cpp1z/constexpr-116676.C: New test.

Reviewed-by: Jason Merrill 
(cherry picked from commit dfe0d4389a3ce43179563a63046ad3e74d615a08)

Diff:
---
 gcc/cp/constexpr.cc   |  1 +
 gcc/testsuite/g++.dg/cpp1z/constexpr-116676.C | 57 +++
 2 files changed, 58 insertions(+)

diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index 12dd9010148e..fb8a1023b222 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -6835,6 +6835,7 @@ maybe_warn_about_constant_value (location_t loc, tree 
decl)
   && warn_interference_size
   && !OPTION_SET_P (param_destruct_interfere_size)
   && DECL_CONTEXT (decl) == std_node
+  && DECL_NAME (decl)
   && id_equal (DECL_NAME (decl), "hardware_destructive_interference_size")
   && (LOCATION_FILE (input_location) != main_input_filename
  || module_exporting_p ())
diff --git a/gcc/testsuite/g++.dg/cpp1z/constexpr-116676.C 
b/gcc/testsuite/g++.dg/cpp1z/constexpr-116676.C
new file mode 100644
index ..1cb65f10a1d1
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/constexpr-116676.C
@@ -0,0 +1,57 @@
+// PR c++/116676
+// { dg-do compile { target c++17 } }
+
+namespace std {
+typedef __SIZE_TYPE__ size_t;
+
+  template
+struct remove_reference
+{ typedef _Tp type; };
+
+  template
+struct remove_reference<_Tp&>
+{ typedef _Tp type; };
+
+  template
+struct remove_reference<_Tp&&>
+{ typedef _Tp type; };
+
+template 
+constexpr typename std::remove_reference<_Tp>::type &&
+move(_Tp &&__t) noexcept {
+  return static_cast::type &&>(__t);
+}
+template  struct tuple_size;
+template  struct tuple_element;
+template  class __pair_base {};
+template 
+struct pair {
+  _T1 first;
+  _T2 second;
+  template 
+  explicit constexpr pair(const _T1 &__a, const _T2 &__b)
+  : first(__a), second(__b) {}
+};
+template 
+struct tuple_size>
+{
+static constexpr size_t value = 2;
+};
+template 
+struct tuple_element<0, pair<_Tp1, _Tp2>> {
+  typedef _Tp1 type;
+};
+template 
+struct tuple_element<1, pair<_Tp1, _Tp2>> {
+  typedef _Tp2 type;
+};
+
+template 
+constexpr typename tuple_element<_Int, pair<_Tp1, _Tp2>>::type &
+get(pair<_Tp1, _Tp2> &&__in) noexcept {
+  return (std::move(__in).first);
+}
+int t;
+auto [a, b] = std::pair{t, 1};
+}
+


[gcc r12-10713] c++: crash with anon VAR_DECL [PR116676]

2024-09-17 Thread Marek Polacek via Gcc-cvs
https://gcc.gnu.org/g:9046f9aeae0f926e7365d39809a80855e7dc184a

commit r12-10713-g9046f9aeae0f926e7365d39809a80855e7dc184a
Author: Marek Polacek 
Date:   Mon Sep 16 16:42:38 2024 -0400

c++: crash with anon VAR_DECL [PR116676]

r12-3495 added maybe_warn_about_constant_value which will crash if
it gets a nameless VAR_DECL, which is what happens in this PR.

We created this VAR_DECL in cp_parser_decomposition_declaration.

PR c++/116676

gcc/cp/ChangeLog:

* constexpr.cc (maybe_warn_about_constant_value): Check DECL_NAME.

gcc/testsuite/ChangeLog:

* g++.dg/cpp1z/constexpr-116676.C: New test.

Reviewed-by: Jason Merrill 
(cherry picked from commit dfe0d4389a3ce43179563a63046ad3e74d615a08)

Diff:
---
 gcc/cp/constexpr.cc   |  1 +
 gcc/testsuite/g++.dg/cpp1z/constexpr-116676.C | 57 +++
 2 files changed, 58 insertions(+)

diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index 41f862e7056e..20abbee3600e 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -6434,6 +6434,7 @@ maybe_warn_about_constant_value (location_t loc, tree 
decl)
   && warn_interference_size
   && !OPTION_SET_P (param_destruct_interfere_size)
   && DECL_CONTEXT (decl) == std_node
+  && DECL_NAME (decl)
   && id_equal (DECL_NAME (decl), "hardware_destructive_interference_size")
   && (LOCATION_FILE (input_location) != main_input_filename
  || module_exporting_p ())
diff --git a/gcc/testsuite/g++.dg/cpp1z/constexpr-116676.C 
b/gcc/testsuite/g++.dg/cpp1z/constexpr-116676.C
new file mode 100644
index ..1cb65f10a1d1
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/constexpr-116676.C
@@ -0,0 +1,57 @@
+// PR c++/116676
+// { dg-do compile { target c++17 } }
+
+namespace std {
+typedef __SIZE_TYPE__ size_t;
+
+  template
+struct remove_reference
+{ typedef _Tp type; };
+
+  template
+struct remove_reference<_Tp&>
+{ typedef _Tp type; };
+
+  template
+struct remove_reference<_Tp&&>
+{ typedef _Tp type; };
+
+template 
+constexpr typename std::remove_reference<_Tp>::type &&
+move(_Tp &&__t) noexcept {
+  return static_cast::type &&>(__t);
+}
+template  struct tuple_size;
+template  struct tuple_element;
+template  class __pair_base {};
+template 
+struct pair {
+  _T1 first;
+  _T2 second;
+  template 
+  explicit constexpr pair(const _T1 &__a, const _T2 &__b)
+  : first(__a), second(__b) {}
+};
+template 
+struct tuple_size>
+{
+static constexpr size_t value = 2;
+};
+template 
+struct tuple_element<0, pair<_Tp1, _Tp2>> {
+  typedef _Tp1 type;
+};
+template 
+struct tuple_element<1, pair<_Tp1, _Tp2>> {
+  typedef _Tp2 type;
+};
+
+template 
+constexpr typename tuple_element<_Int, pair<_Tp1, _Tp2>>::type &
+get(pair<_Tp1, _Tp2> &&__in) noexcept {
+  return (std::move(__in).first);
+}
+int t;
+auto [a, b] = std::pair{t, 1};
+}
+


[gcc r15-3673] c++: ICE with -Wtautological-compare in template [PR116534]

2024-09-17 Thread Marek Polacek via Gcc-cvs
https://gcc.gnu.org/g:7ca486889b1b1c7e7bcbbca3b6caa103294ec07d

commit r15-3673-g7ca486889b1b1c7e7bcbbca3b6caa103294ec07d
Author: Marek Polacek 
Date:   Thu Aug 29 10:40:50 2024 -0400

c++: ICE with -Wtautological-compare in template [PR116534]

Pre r14-4793, we'd call warn_tautological_cmp -> operand_equal_p
with operands wrapped in NON_DEPENDENT_EXPR, which works, since
o_e_p bails for codes it doesn't know.  But now we pass operands
not encapsulated in NON_DEPENDENT_EXPR, and crash, because the
template tree for &a[x] has null DECL_FIELD_OFFSET.

This patch extends r12-7797 to cover the case when DECL_FIELD_OFFSET
is null.

PR c++/116534

gcc/ChangeLog:

* fold-const.cc (operand_compare::operand_equal_p): If either
field's DECL_FIELD_OFFSET is null, compare the fields with ==.

gcc/testsuite/ChangeLog:

* g++.dg/warn/Wtautological-compare4.C: New test.

Reviewed-by: Jason Merrill 

Diff:
---
 gcc/fold-const.cc  | 12 +---
 gcc/testsuite/g++.dg/warn/Wtautological-compare4.C | 21 +
 2 files changed, 30 insertions(+), 3 deletions(-)

diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc
index 70db16759d04..0578f42ac0c5 100644
--- a/gcc/fold-const.cc
+++ b/gcc/fold-const.cc
@@ -3601,9 +3601,15 @@ operand_compare::operand_equal_p (const_tree arg0, 
const_tree arg1,
 
/* Non-FIELD_DECL operands can appear in C++ templates.  */
if (TREE_CODE (field0) != FIELD_DECL
-   || TREE_CODE (field1) != FIELD_DECL
-   || !operand_equal_p (DECL_FIELD_OFFSET (field0),
-DECL_FIELD_OFFSET (field1), flags)
+   || TREE_CODE (field1) != FIELD_DECL)
+ return false;
+
+   if (!DECL_FIELD_OFFSET (field0)
+   || !DECL_FIELD_OFFSET (field1))
+ return field0 == field1;
+
+   if (!operand_equal_p (DECL_FIELD_OFFSET (field0),
+ DECL_FIELD_OFFSET (field1), flags)
|| !operand_equal_p (DECL_FIELD_BIT_OFFSET (field0),
 DECL_FIELD_BIT_OFFSET (field1),
 flags))
diff --git a/gcc/testsuite/g++.dg/warn/Wtautological-compare4.C 
b/gcc/testsuite/g++.dg/warn/Wtautological-compare4.C
new file mode 100644
index ..96308f49a429
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wtautological-compare4.C
@@ -0,0 +1,21 @@
+// PR c++/116534
+// { dg-do compile }
+// { dg-options "-Wall" }
+
+template 
+struct Test {
+bool foo(unsigned x, unsigned y) {
+bool test = &a[x] == &b[y];
+   return test;
+}
+unsigned *a;
+unsigned *b;
+};
+
+void
+g ()
+{
+  Test t;
+  t.foo (0u, 1u);
+  t.foo (0u, 0u);
+}


[gcc r15-3674] c++: fix constexpr cast from void* diag issue [PR116741]

2024-09-17 Thread Marek Polacek via Gcc-cvs
https://gcc.gnu.org/g:d6d8445c85509b66a59aa6247ad7b2cfeab17725

commit r15-3674-gd6d8445c85509b66a59aa6247ad7b2cfeab17725
Author: Marek Polacek 
Date:   Tue Sep 17 14:34:30 2024 -0400

c++: fix constexpr cast from void* diag issue [PR116741]

The result of build_fold_indirect_ref can be a COMPONENT_REF in
which case using DECL_SOURCE_LOCATION will crash.  Look at its op1
instead.

PR c++/116741

gcc/cp/ChangeLog:

* constexpr.cc (cxx_eval_constant_expression) : 
If
the result of build_fold_indirect_ref is a COMPONENT_REF, use its 
op1.
Check DECL_P before calling inform.

gcc/testsuite/ChangeLog:

* g++.dg/cpp26/constexpr-voidptr4.C: New test.

Reviewed-by: Jason Merrill 

Diff:
---
 gcc/cp/constexpr.cc |  7 --
 gcc/testsuite/g++.dg/cpp26/constexpr-voidptr4.C | 29 +
 2 files changed, 34 insertions(+), 2 deletions(-)

diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index c3668b0d7d3f..f6fd059be466 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -8201,8 +8201,11 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx, 
tree t,
  TREE_TYPE (op), TREE_TYPE (TREE_TYPE (sop)),
  TREE_TYPE (type));
tree obj = build_fold_indirect_ref (sop);
-   inform (DECL_SOURCE_LOCATION (obj),
-   "pointed-to object declared here");
+   if (TREE_CODE (obj) == COMPONENT_REF)
+ obj = TREE_OPERAND (obj, 1);
+   if (DECL_P (obj))
+ inform (DECL_SOURCE_LOCATION (obj),
+ "pointed-to object declared here");
  }
*non_constant_p = true;
return t;
diff --git a/gcc/testsuite/g++.dg/cpp26/constexpr-voidptr4.C 
b/gcc/testsuite/g++.dg/cpp26/constexpr-voidptr4.C
new file mode 100644
index ..53563c928f27
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp26/constexpr-voidptr4.C
@@ -0,0 +1,29 @@
+// PR c++/116741
+// { dg-do compile { target c++26 } }
+
+struct S {
+  int foo;// { dg-message "pointed-to object" }
+};
+
+struct S2 {
+  int foo;// { dg-message "pointed-to object" }
+};
+
+struct X {
+  S2 s;
+};
+
+constexpr float f1() {
+  S s;
+  void* p = &s.foo;
+  return *static_cast(p); // { dg-error "not allowed in a constant 
expression" }
+}
+
+constexpr float f2() {
+  X x;
+  void* p = &x.s.foo;
+  return *static_cast(p); // { dg-error "not allowed in a constant 
expression" }
+}
+
+constexpr auto x1 = f1();
+constexpr auto x2 = f2();


[gcc(refs/users/meissner/heads/work178-bugs)] PR 89213: Address review comments.

2024-09-17 Thread Michael Meissner via Gcc-cvs
https://gcc.gnu.org/g:6d1247a9a00e05a24f90ef46afc9c3326c689242

commit 6d1247a9a00e05a24f90ef46afc9c3326c689242
Author: Michael Meissner 
Date:   Tue Sep 17 20:08:49 2024 -0400

PR 89213: Address review comments.

2024-09-17  Michael Meissner  

gcc/

PR target/89213
* config/rs6000/altivec.md (altivec__shift_const): Remove 
extra
()'s.

gcc/testsuite/

PR target/89213
* gcc.target/powerpc/pr89213.c: Allow running test on 32-bit.

Diff:
---
 gcc/config/rs6000/altivec.md   | 6 +++---
 gcc/testsuite/gcc.target/powerpc/pr89213.c | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/gcc/config/rs6000/altivec.md b/gcc/config/rs6000/altivec.md
index 8faece984e9f..e4576c6d0967 100644
--- a/gcc/config/rs6000/altivec.md
+++ b/gcc/config/rs6000/altivec.md
@@ -2208,9 +2208,9 @@
   if (GET_CODE (operands[3]) == SCRATCH)
 operands[3] = gen_reg_rtx (mode);
 
-  operands[4] = ((GET_CODE (operands[2]) == CONST_VECTOR)
-? CONST_VECTOR_ELT (operands[2], 0)
-: XEXP (operands[2], 0));
+  operands[4] = GET_CODE (operands[2]) == CONST_VECTOR
+   ? CONST_VECTOR_ELT (operands[2], 0)
+   : XEXP (operands[2], 0);
 })
 
 (define_insn "*altivec__shift_const"
diff --git a/gcc/testsuite/gcc.target/powerpc/pr89213.c 
b/gcc/testsuite/gcc.target/powerpc/pr89213.c
index 8f5fae2c3ef9..90a8b1b5787b 100644
--- a/gcc/testsuite/gcc.target/powerpc/pr89213.c
+++ b/gcc/testsuite/gcc.target/powerpc/pr89213.c
@@ -1,4 +1,4 @@
-/* { dg-do compile { target { lp64 } } } */
+/* { dg-do compile } */
 /* { dg-require-effective-target powerpc_vsx } */
 /* { dg-options "-mcpu=power9 -O2" } */


[gcc(refs/users/meissner/heads/work178-bugs)] Update ChangeLog.*

2024-09-17 Thread Michael Meissner via Gcc-cvs
https://gcc.gnu.org/g:4be2a0e003cb0e155df460408c06e069969215cb

commit 4be2a0e003cb0e155df460408c06e069969215cb
Author: Michael Meissner 
Date:   Tue Sep 17 20:12:20 2024 -0400

Update ChangeLog.*

Diff:
---
 gcc/ChangeLog.bugs | 17 +
 1 file changed, 17 insertions(+)

diff --git a/gcc/ChangeLog.bugs b/gcc/ChangeLog.bugs
index 31668b76dbc5..00867f376c2f 100644
--- a/gcc/ChangeLog.bugs
+++ b/gcc/ChangeLog.bugs
@@ -1,3 +1,20 @@
+ Branch work178-bugs, patch #202 
+
+PR 89213: Address review comments.
+
+2024-09-17  Michael Meissner  
+
+gcc/
+
+   PR target/89213
+   * config/rs6000/altivec.md (altivec__shift_const): Remove extra
+   ()'s.
+
+gcc/testsuite/
+
+   PR target/89213
+   * gcc.target/powerpc/pr89213.c: Allow running test on 32-bit.
+
  Branch work178-bugs, patch #201 
 
 PR 99293: Optimize splat of a V2DF/V2DI extract with constant element


[gcc r15-3676] PR 89213: Add better support for shifting vectors with 64-bit elements

2024-09-17 Thread Michael Meissner via Gcc-cvs
https://gcc.gnu.org/g:9a07ac151327f61963b092062eb8566dd0c6f0cd

commit r15-3676-g9a07ac151327f61963b092062eb8566dd0c6f0cd
Author: Michael Meissner 
Date:   Tue Sep 17 21:05:27 2024 -0400

PR 89213: Add better support for shifting vectors with 64-bit elements

This patch fixes PR target/89213 to allow better code to be generated to do
constant shifts of V2DI/V2DF vectors.  Previously GCC would do constant 
shifts
of vectors with 64-bit elements by using:

XXSPLTIB 32,4
VEXTSB2D 0,0
VSRAD 2,2,0

I.e., the PowerPC does not have a VSPLTISD instruction to load -15..14 for 
the
64-bit shift count in one instruction.  Instead, it would need to load a 
byte
and then convert it to 64-bit.

With this patch, GCC now realizes that the vector shift instructions will 
look
at the bottom 6 bits for the shift count, and it can use either a VSPLTISW 
or
XXSPLTIB instruction to load the shift count.

2024-09-17  Michael Meissner  

gcc/

PR target/89213
* config/rs6000/altivec.md (UNSPEC_VECTOR_SHIFT): New unspec.
(VSHIFT_MODE): New mode iterator.
(vshift_code): New code iterator.
(vshift_attr): New code attribute.
(altivec___const): New pattern to optimize
vector long long/int shifts by a constant.
(altivec__shift_const): New helper insn to load up a
constant used by the shift operation.
* config/rs6000/predicates.md (vector_shift_constant): New
predicate.

gcc/testsuite/

PR target/89213
* gcc.target/powerpc/pr89213.c: New test.
* gcc.target/powerpc/vec-rlmi-rlnm.c: Update instruction count.

Diff:
---
 gcc/config/rs6000/altivec.md |  51 +++
 gcc/config/rs6000/predicates.md  |  63 ++
 gcc/testsuite/gcc.target/powerpc/pr89213.c   | 106 +++
 gcc/testsuite/gcc.target/powerpc/vec-rlmi-rlnm.c |   4 +-
 4 files changed, 222 insertions(+), 2 deletions(-)

diff --git a/gcc/config/rs6000/altivec.md b/gcc/config/rs6000/altivec.md
index 1f5489b974f6..e4576c6d0967 100644
--- a/gcc/config/rs6000/altivec.md
+++ b/gcc/config/rs6000/altivec.md
@@ -170,6 +170,7 @@
UNSPEC_VSTRIL
UNSPEC_SLDB
UNSPEC_SRDB
+   UNSPEC_VECTOR_SHIFT
 ])
 
 (define_c_enum "unspecv"
@@ -2176,6 +2177,56 @@
   "vsro %0,%1,%2"
   [(set_attr "type" "vecperm")])
 
+;; Optimize V2DI shifts by constants.  This relies on the shift instructions
+;; only looking at the bits needed to do the shift.  This means we can use
+;; VSPLTISW or XXSPLTIB to load up the constant, and not worry about the bits
+;; that the vector shift instructions will not use.
+(define_mode_iterator VSHIFT_MODE  [(V4SI "TARGET_P9_VECTOR")
+(V2DI "TARGET_P8_VECTOR")])
+
+(define_code_iterator vshift_code  [ashift ashiftrt lshiftrt])
+(define_code_attr vshift_attr  [(ashift   "ashift")
+(ashiftrt "ashiftrt")
+(lshiftrt "lshiftrt")])
+
+(define_insn_and_split "*altivec___const"
+  [(set (match_operand:VSHIFT_MODE 0 "register_operand" "=v")
+   (vshift_code:VSHIFT_MODE
+(match_operand:VSHIFT_MODE 1 "register_operand" "v")
+(match_operand:VSHIFT_MODE 2 "vector_shift_constant" "")))
+   (clobber (match_scratch:VSHIFT_MODE 3 "=&v"))]
+  "((mode == V2DImode && TARGET_P8_VECTOR)
+|| (mode == V4SImode && TARGET_P9_VECTOR))"
+  "#"
+  "&& 1"
+  [(set (match_dup 3)
+   (unspec:VSHIFT_MODE [(match_dup 4)] UNSPEC_VECTOR_SHIFT))
+   (set (match_dup 0)
+   (vshift_code:VSHIFT_MODE (match_dup 1)
+(match_dup 3)))]
+{
+  if (GET_CODE (operands[3]) == SCRATCH)
+operands[3] = gen_reg_rtx (mode);
+
+  operands[4] = GET_CODE (operands[2]) == CONST_VECTOR
+   ? CONST_VECTOR_ELT (operands[2], 0)
+   : XEXP (operands[2], 0);
+})
+
+(define_insn "*altivec__shift_const"
+  [(set (match_operand:VSHIFT_MODE 0 "register_operand" "=v")
+   (unspec:VSHIFT_MODE [(match_operand 1 "const_int_operand" "n")]
+   UNSPEC_VECTOR_SHIFT))]
+  "TARGET_P8_VECTOR"
+{
+  if (UINTVAL (operands[1]) <= 15)
+return "vspltisw %0,%1";
+  else if (TARGET_P9_VECTOR)
+return "xxspltib %x0,%1";
+  else
+gcc_unreachable ();
+})
+
 (define_insn "altivec_vsum4ubs"
   [(set (match_operand:V4SI 0 "register_operand" "=v")
 (unspec:V4SI [(match_operand:V16QI 1 "register_operand" "v")
diff --git a/gcc/config/rs6000/predicates.md b/gcc/config/rs6000/predicates.md
index 7f0b4ab61e65..0b78901e94be 100644
--- a/gcc/config/rs6000/predicates.md
+++ b/gcc/config/rs6000/predicates.md
@@ -861,6 +861,69 @@
 return op == CONST0_RTX (mode) || op == CONSTM1_RTX (mode);
 })
 
+;; Return 1 if the operand is a V2DI or V4SI const_v

[gcc r15-3677] RISC-V: Implement SAT_ADD for signed integer vector

2024-09-17 Thread Pan Li via Gcc-cvs
https://gcc.gnu.org/g:a82896ed7bbdb5f64ae741433bba4c1db2f2a562

commit r15-3677-ga82896ed7bbdb5f64ae741433bba4c1db2f2a562
Author: Pan Li 
Date:   Thu Sep 12 10:43:46 2024 +0800

RISC-V: Implement SAT_ADD for signed integer vector

This patch would like to implement the ssadd for vector integer.  Aka
form 1 of ssadd vector.

Form 1:
  #define DEF_VEC_SAT_S_ADD_FMT_1(T, UT, MIN, MAX) \
  void __attribute__((noinline))   \
  vec_sat_s_add_##T##_fmt_1 (T *out, T *op_1, T *op_2, unsigned limit) \
  {\
unsigned i;\
for (i = 0; i < limit; i++)\
  {\
T x = op_1[i]; \
T y = op_2[i]; \
T sum = (UT)x + (UT)y; \
out[i] = (x ^ y) < 0   \
  ? sum\
  : (sum ^ x) >= 0 \
? sum  \
: x < 0 ? MIN : MAX;   \
  }\
  }

DEF_VEC_SAT_S_ADD_FMT_1(int64_t, uint64_t, INT64_MIN, INT64_MAX)

Before this patch:
vec_sat_s_add_int64_t_fmt_1:
  ...
  vsetvli  t1,zero,e64,m1,ta,mu
  vadd.vv  v3,v1,v2
  vxor.vv  v0,v1,v3
  vmslt.vi v0,v0,0
  vxor.vv  v2,v1,v2
  vmsge.vi v2,v2,0
  vmand.mm v0,v0,v2
  vsra.vx  v1,v1,t3
  vxor.vv  v3,v1,v4,v0.t
  ...

After this patch:
vec_sat_s_add_int64_t_fmt_1:
  ...
  vsetvli  a6,zero,e64,m1,ta,ma
  vsadd.vv v1,v1,v2
  ...

The below test suites are passed for this patch.
* The rv64gcv fully regression test.

gcc/ChangeLog:

* config/riscv/autovec.md (ssadd3): Add new pattern for
signed integer vector SAT_ADD.
* config/riscv/riscv-protos.h (expand_vec_ssadd): Add new func
decl for vector ssadd expanding.
* config/riscv/riscv-v.cc (expand_vec_ssadd): Add new func impl
to expand vector ssadd pattern.

gcc/testsuite/ChangeLog:

* gcc.target/riscv/rvv/autovec/binop/vec_sat_data.h: Add test
data for vector ssadd.
* gcc.target/riscv/rvv/autovec/vec_sat_arith.h: Add test helper
macros.
* gcc.target/riscv/rvv/autovec/binop/vec_sat_s_add-1.c: New test.
* gcc.target/riscv/rvv/autovec/binop/vec_sat_s_add-2.c: New test.
* gcc.target/riscv/rvv/autovec/binop/vec_sat_s_add-3.c: New test.
* gcc.target/riscv/rvv/autovec/binop/vec_sat_s_add-4.c: New test.
* gcc.target/riscv/rvv/autovec/binop/vec_sat_s_add-run-1.c: New 
test.
* gcc.target/riscv/rvv/autovec/binop/vec_sat_s_add-run-2.c: New 
test.
* gcc.target/riscv/rvv/autovec/binop/vec_sat_s_add-run-3.c: New 
test.
* gcc.target/riscv/rvv/autovec/binop/vec_sat_s_add-run-4.c: New 
test.

Signed-off-by: Pan Li 

Diff:
---
 gcc/config/riscv/autovec.md|  11 +
 gcc/config/riscv/riscv-protos.h|   1 +
 gcc/config/riscv/riscv-v.cc|   9 +
 .../riscv/rvv/autovec/binop/vec_sat_data.h | 264 +
 .../riscv/rvv/autovec/binop/vec_sat_s_add-1.c  |  18 ++
 .../riscv/rvv/autovec/binop/vec_sat_s_add-2.c  |  18 ++
 .../riscv/rvv/autovec/binop/vec_sat_s_add-3.c  |  18 ++
 .../riscv/rvv/autovec/binop/vec_sat_s_add-4.c  |  18 ++
 .../riscv/rvv/autovec/binop/vec_sat_s_add-run-1.c  |  17 ++
 .../riscv/rvv/autovec/binop/vec_sat_s_add-run-2.c  |  17 ++
 .../riscv/rvv/autovec/binop/vec_sat_s_add-run-3.c  |  17 ++
 .../riscv/rvv/autovec/binop/vec_sat_s_add-run-4.c  |  17 ++
 .../gcc.target/riscv/rvv/autovec/vec_sat_arith.h   |  25 ++
 13 files changed, 450 insertions(+)

diff --git a/gcc/config/riscv/autovec.md b/gcc/config/riscv/autovec.md
index a4e108268b44..a53c44659f0f 100644
--- a/gcc/config/riscv/autovec.md
+++ b/gcc/config/riscv/autovec.md
@@ -2684,6 +2684,17 @@
   }
 )
 
+(define_expand "ssadd3"
+  [(match_operand:V_VLSI 0 "register_operand")
+   (match_operand:V_VLSI 1 "register_operand")
+   (match_operand:V_VLSI 2 "register_operand")]
+  "TARGET_VECTOR"
+  {
+riscv_vector::expand_vec_ssadd (operands[0], operands[1], operands[2], 
mode);
+DONE;
+  }
+)
+
 (define_expand "ussub3"
   [(match_operand:V_VLSI 0 "register_operand")
(match_operand:V_VLSI 1 "regis

[gcc r15-3678] phiopt: Add some details dump to cselim

2024-09-17 Thread Andrew Pinski via Gcc-cvs
https://gcc.gnu.org/g:8590dcd318151336261f8381e1a24caece9e2375

commit r15-3678-g8590dcd318151336261f8381e1a24caece9e2375
Author: Andrew Pinski 
Date:   Tue Sep 17 14:26:54 2024 -0700

phiopt: Add some details dump to cselim

While trying to debug PR 116747, I noticed there was no dump
saying what was done. So this adds the debug dump and it helps
debug what is going on in PR 116747 too.

Bootstrapped and tested on x86_64-linux-gnu.

gcc/ChangeLog:

* tree-ssa-phiopt.cc (cond_if_else_store_replacement_1): Add debug 
dump.

Signed-off-by: Andrew Pinski 

Diff:
---
 gcc/tree-ssa-phiopt.cc | 21 +
 1 file changed, 21 insertions(+)

diff --git a/gcc/tree-ssa-phiopt.cc b/gcc/tree-ssa-phiopt.cc
index 7b12692237e5..488b45015e90 100644
--- a/gcc/tree-ssa-phiopt.cc
+++ b/gcc/tree-ssa-phiopt.cc
@@ -3469,6 +3469,17 @@ cond_if_else_store_replacement_1 (basic_block then_bb, 
basic_block else_bb,
   then_locus = gimple_location (then_assign);
   else_locus = gimple_location (else_assign);
 
+  if (dump_file && (dump_flags & TDF_DETAILS))
+{
+  fprintf(dump_file, "factoring out stores:\n\tthen:\n");
+  print_gimple_stmt (dump_file, then_assign, 0,
+TDF_VOPS|TDF_MEMSYMS);
+  fprintf(dump_file, "\telse:\n");
+  print_gimple_stmt (dump_file, else_assign, 0,
+TDF_VOPS|TDF_MEMSYMS);
+  fprintf (dump_file, "\n");
+}
+
   /* Now we've checked the constraints, so do the transformation:
  1) Remove the stores.  */
   gsi = gsi_for_stmt (then_assign);
@@ -3490,6 +3501,16 @@ cond_if_else_store_replacement_1 (basic_block then_bb, 
basic_block else_bb,
   add_phi_arg (newphi, else_rhs, EDGE_SUCC (else_bb, 0), else_locus);
 
   new_stmt = gimple_build_assign (lhs, gimple_phi_result (newphi));
+  if (dump_file && (dump_flags & TDF_DETAILS))
+{
+  fprintf(dump_file, "to use phi:\n");
+  print_gimple_stmt (dump_file, newphi, 0,
+TDF_VOPS|TDF_MEMSYMS);
+  fprintf(dump_file, "\n");
+  print_gimple_stmt (dump_file, new_stmt, 0,
+TDF_VOPS|TDF_MEMSYMS);
+  fprintf(dump_file, "\n\n");
+}
 
   /* 3) Insert that PHI node.  */
   gsi = gsi_after_labels (join_bb);


[gcc r15-3679] phiopt: C++ify cond_if_else_store_replacement

2024-09-17 Thread Andrew Pinski via Gcc-cvs
https://gcc.gnu.org/g:45cacfe7325bdbed4a2393927812561f64b9afd1

commit r15-3679-g45cacfe7325bdbed4a2393927812561f64b9afd1
Author: Andrew Pinski 
Date:   Tue Sep 17 15:03:21 2024 -0700

phiopt: C++ify cond_if_else_store_replacement

This C++ify cond_if_else_store_replacement by using range fors
and changing using a std::pair instead of 2 vecs.
I had a hard time understanding the code when there was 2 vecs
so having a vec of a pair makes it easier to understand the relationship
between the 2.

gcc/ChangeLog:

* tree-ssa-phiopt.cc (cond_if_else_store_replacement): Use
range fors and use one vec for then/else stores instead of 2.

Signed-off-by: Andrew Pinski 

Diff:
---
 gcc/tree-ssa-phiopt.cc | 25 +++--
 1 file changed, 11 insertions(+), 14 deletions(-)

diff --git a/gcc/tree-ssa-phiopt.cc b/gcc/tree-ssa-phiopt.cc
index 488b45015e90..d43832b390ba 100644
--- a/gcc/tree-ssa-phiopt.cc
+++ b/gcc/tree-ssa-phiopt.cc
@@ -3587,9 +3587,6 @@ cond_if_else_store_replacement (basic_block then_bb, 
basic_block else_bb,
   vec then_ddrs, else_ddrs;
   gimple *then_store, *else_store;
   bool found, ok = false, res;
-  struct data_dependence_relation *ddr;
-  data_reference_p then_dr, else_dr;
-  int i, j;
   tree then_lhs, else_lhs;
   basic_block blocks[3];
 
@@ -3640,8 +3637,8 @@ cond_if_else_store_replacement (basic_block then_bb, 
basic_block else_bb,
 }
 
   /* Find pairs of stores with equal LHS.  */
-  auto_vec then_stores, else_stores;
-  FOR_EACH_VEC_ELT (then_datarefs, i, then_dr)
+  auto_vec, 1> stores_pairs;
+  for (auto then_dr : then_datarefs)
 {
   if (DR_IS_READ (then_dr))
 continue;
@@ -3652,7 +3649,7 @@ cond_if_else_store_replacement (basic_block then_bb, 
basic_block else_bb,
continue;
   found = false;
 
-  FOR_EACH_VEC_ELT (else_datarefs, j, else_dr)
+  for (auto else_dr : else_datarefs)
 {
   if (DR_IS_READ (else_dr))
 continue;
@@ -3672,13 +3669,12 @@ cond_if_else_store_replacement (basic_block then_bb, 
basic_block else_bb,
   if (!found)
 continue;
 
-  then_stores.safe_push (then_store);
-  else_stores.safe_push (else_store);
+  stores_pairs.safe_push (std::make_pair (then_store, else_store));
 }
 
   /* No pairs of stores found.  */
-  if (!then_stores.length ()
-  || then_stores.length () > (unsigned) param_max_stores_to_sink)
+  if (!stores_pairs.length ()
+  || stores_pairs.length () > (unsigned) param_max_stores_to_sink)
 {
   free_data_refs (then_datarefs);
   free_data_refs (else_datarefs);
@@ -3706,7 +3702,7 @@ cond_if_else_store_replacement (basic_block then_bb, 
basic_block else_bb,
 
   /* Check that there are no read-after-write or write-after-write dependencies
  in THEN_BB.  */
-  FOR_EACH_VEC_ELT (then_ddrs, i, ddr)
+  for (auto ddr : then_ddrs)
 {
   struct data_reference *dra = DDR_A (ddr);
   struct data_reference *drb = DDR_B (ddr);
@@ -3728,7 +3724,7 @@ cond_if_else_store_replacement (basic_block then_bb, 
basic_block else_bb,
 
   /* Check that there are no read-after-write or write-after-write dependencies
  in ELSE_BB.  */
-  FOR_EACH_VEC_ELT (else_ddrs, i, ddr)
+  for (auto ddr : else_ddrs)
 {
   struct data_reference *dra = DDR_A (ddr);
   struct data_reference *drb = DDR_B (ddr);
@@ -3749,9 +3745,10 @@ cond_if_else_store_replacement (basic_block then_bb, 
basic_block else_bb,
 }
 
   /* Sink stores with same LHS.  */
-  FOR_EACH_VEC_ELT (then_stores, i, then_store)
+  for (auto &store_pair : stores_pairs)
 {
-  else_store = else_stores[i];
+  then_store = store_pair.first;
+  else_store = store_pair.second;
   res = cond_if_else_store_replacement_1 (then_bb, else_bb, join_bb,
   then_store, else_store);
   ok = ok || res;