Sync patches from openeuler/gcc

This commit is contained in:
huangzifeng 2024-11-21 11:46:51 +08:00
parent 845ea89e1a
commit 407b086b9e
6 changed files with 518 additions and 1 deletions

View File

@ -0,0 +1,99 @@
From a252bbd11d22481a1e719ed36d800e2192abb369 Mon Sep 17 00:00:00 2001
From: Pronin Alexander <pronin.alexander@huawei.com>
Date: Thu, 31 Oct 2024 15:49:27 +0800
Subject: [PATCH 1/6] Mark prefetch builtin as willreturn
Signed-off-by: Pronin Alexander <pronin.alexander@huawei.com>
---
gcc/common.opt | 4 ++++
gcc/gimple.cc | 30 ++++++++++++++++++++++++++++++
gcc/gimple.h | 1 +
gcc/tree-ssa-pre.cc | 4 +---
4 files changed, 36 insertions(+), 3 deletions(-)
diff --git a/gcc/common.opt b/gcc/common.opt
index 688d65e4d..be5fcc681 100644
--- a/gcc/common.opt
+++ b/gcc/common.opt
@@ -1313,6 +1313,10 @@ fdelete-null-pointer-checks
Common Var(flag_delete_null_pointer_checks) Init(-1) Optimization
Delete useless null pointer checks.
+fbuiltin-will-return
+Common Var(flag_builtin_will_return) Optimization
+Consider some of the builtins as definitely returning.
+
fdevirtualize-at-ltrans
Common Var(flag_ltrans_devirtualize)
Stream extra data to support more aggressive devirtualization in LTO local transformation mode.
diff --git a/gcc/gimple.cc b/gcc/gimple.cc
index 9e62da426..04ca9f161 100644
--- a/gcc/gimple.cc
+++ b/gcc/gimple.cc
@@ -2998,6 +2998,36 @@ nonbarrier_call_p (gimple *call)
return false;
}
+static inline bool
+will_return_builtin_p (gimple *call)
+{
+ if (!flag_builtin_will_return)
+ return false;
+
+ if (!gimple_call_builtin_p (call, BUILT_IN_NORMAL))
+ return false;
+
+ switch (DECL_FUNCTION_CODE (gimple_call_fndecl (call)))
+ {
+ case BUILT_IN_PREFETCH:
+ return true;
+ default:
+ return false;
+ }
+}
+
+bool
+will_return_call_p (gimple *call, function *fun)
+{
+ int flags = gimple_call_flags (call);
+ if (!(flags & (ECF_CONST|ECF_PURE))
+ || (flags & ECF_LOOPING_CONST_OR_PURE)
+ || stmt_can_throw_external (fun, call))
+ return will_return_builtin_p (call);
+
+ return true;
+}
+
/* Callback for walk_stmt_load_store_ops.
Return TRUE if OP will dereference the tree stored in DATA, FALSE
diff --git a/gcc/gimple.h b/gcc/gimple.h
index 77a5a07e9..bb05a7664 100644
--- a/gcc/gimple.h
+++ b/gcc/gimple.h
@@ -1628,6 +1628,7 @@ extern bool gimple_asm_clobbers_memory_p (const gasm *);
extern void dump_decl_set (FILE *, bitmap);
extern bool nonfreeing_call_p (gimple *);
extern bool nonbarrier_call_p (gimple *);
+extern bool will_return_call_p (gimple *, function *);
extern bool infer_nonnull_range (gimple *, tree);
extern bool infer_nonnull_range_by_dereference (gimple *, tree);
extern bool infer_nonnull_range_by_attribute (gimple *, tree);
diff --git a/gcc/tree-ssa-pre.cc b/gcc/tree-ssa-pre.cc
index 98134b5d3..b5264133a 100644
--- a/gcc/tree-ssa-pre.cc
+++ b/gcc/tree-ssa-pre.cc
@@ -3988,9 +3988,7 @@ compute_avail (function *fun)
that forbids hoisting possibly trapping expressions
before it. */
int flags = gimple_call_flags (stmt);
- if (!(flags & (ECF_CONST|ECF_PURE))
- || (flags & ECF_LOOPING_CONST_OR_PURE)
- || stmt_can_throw_external (fun, stmt))
+ if (!will_return_call_p (stmt, fun))
/* Defer setting of BB_MAY_NOTRETURN to avoid it
influencing the processing of the call itself. */
set_bb_may_notreturn = true;
--
2.33.0

View File

@ -0,0 +1,156 @@
From 3b109376d057342a31267ea4c9bd422d940874cb Mon Sep 17 00:00:00 2001
From: Jakub Jelinek <jakub@redhat.com>
Date: Thu, 31 Oct 2024 16:09:43 +0800
Subject: [PATCH 2/6] [Backport]Disallow pointer operands for |,^ and partly
&[PR106878]
Signed-off-by: Jakub Jelinek <jakub@redhat.com>
---
gcc/match.pd | 6 ++++-
.../gcc.c-torture/compile/pr106878.c | 15 +++++++++++++
gcc/tree-cfg.cc | 22 ++++++++++++++++---
gcc/tree-ssa-reassoc.cc | 16 +++++++++++++-
4 files changed, 54 insertions(+), 5 deletions(-)
create mode 100644 gcc/testsuite/gcc.c-torture/compile/pr106878.c
diff --git a/gcc/match.pd b/gcc/match.pd
index 8f41c292f..822e065e8 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -1655,6 +1655,8 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
&& (int_fits_type_p (@1, TREE_TYPE (@0))
|| tree_nop_conversion_p (TREE_TYPE (@0), type)))
|| types_match (@0, @1))
+ && !POINTER_TYPE_P (TREE_TYPE (@0))
+ && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE
/* ??? This transform conflicts with fold-const.cc doing
Convert (T)(x & c) into (T)x & (T)c, if c is an integer
constants (if x has signed type, the sign bit cannot be set
@@ -1691,7 +1693,9 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
(if (GIMPLE
&& TREE_CODE (@1) != INTEGER_CST
&& tree_nop_conversion_p (type, TREE_TYPE (@2))
- && types_match (type, @0))
+ && types_match (type, @0)
+ && !POINTER_TYPE_P (TREE_TYPE (@0))
+ && TREE_CODE (TREE_TYPE (@0)) != OFFSET_TYPE)
(bitop @0 (convert @1)))))
(for bitop (bit_and bit_ior)
diff --git a/gcc/testsuite/gcc.c-torture/compile/pr106878.c b/gcc/testsuite/gcc.c-torture/compile/pr106878.c
new file mode 100644
index 000000000..c84571894
--- /dev/null
+++ b/gcc/testsuite/gcc.c-torture/compile/pr106878.c
@@ -0,0 +1,15 @@
+/* PR tree-optimization/106878 */
+
+typedef __INTPTR_TYPE__ intptr_t;
+typedef __UINTPTR_TYPE__ uintptr_t;
+int a;
+
+int
+foo (const int *c)
+{
+ uintptr_t d = ((intptr_t) c | (intptr_t) &a) & 65535 << 16;
+ intptr_t e = (intptr_t) c;
+ if (d != (e & 65535 << 16))
+ return 1;
+ return 0;
+}
diff --git a/gcc/tree-cfg.cc b/gcc/tree-cfg.cc
index 48b52f785..d33aaec8c 100644
--- a/gcc/tree-cfg.cc
+++ b/gcc/tree-cfg.cc
@@ -4163,7 +4163,9 @@ verify_gimple_assign_binary (gassign *stmt)
case ROUND_MOD_EXPR:
case RDIV_EXPR:
case EXACT_DIV_EXPR:
- /* Disallow pointer and offset types for many of the binary gimple. */
+ case BIT_IOR_EXPR:
+ case BIT_XOR_EXPR:
+ /* Disallow pointer and offset types for many of the binary gimple. */
if (POINTER_TYPE_P (lhs_type)
|| TREE_CODE (lhs_type) == OFFSET_TYPE)
{
@@ -4178,9 +4180,23 @@ verify_gimple_assign_binary (gassign *stmt)
case MIN_EXPR:
case MAX_EXPR:
- case BIT_IOR_EXPR:
- case BIT_XOR_EXPR:
+ /* Continue with generic binary expression handling. */
+ break;
+
case BIT_AND_EXPR:
+ if (POINTER_TYPE_P (lhs_type)
+ && TREE_CODE (rhs2) == INTEGER_CST)
+ break;
+ /* Disallow pointer and offset types for many of the binary gimple. */
+ if (POINTER_TYPE_P (lhs_type)
+ || TREE_CODE (lhs_type) == OFFSET_TYPE)
+ {
+ error ("invalid types for %qs", code_name);
+ debug_generic_expr (lhs_type);
+ debug_generic_expr (rhs1_type);
+ debug_generic_expr (rhs2_type);
+ return true;
+ }
/* Continue with generic binary expression handling. */
break;
diff --git a/gcc/tree-ssa-reassoc.cc b/gcc/tree-ssa-reassoc.cc
index e3d521e32..6baef4764 100644
--- a/gcc/tree-ssa-reassoc.cc
+++ b/gcc/tree-ssa-reassoc.cc
@@ -3617,10 +3617,14 @@ optimize_range_tests_cmp_bitwise (enum tree_code opcode, int first, int length,
tree type2 = NULL_TREE;
bool strict_overflow_p = false;
candidates.truncate (0);
+ if (POINTER_TYPE_P (type1))
+ type1 = pointer_sized_int_node;
for (j = i; j; j = chains[j - 1])
{
tree type = TREE_TYPE (ranges[j - 1].exp);
strict_overflow_p |= ranges[j - 1].strict_overflow_p;
+ if (POINTER_TYPE_P (type))
+ type = pointer_sized_int_node;
if ((b % 4) == 3)
{
/* For the signed < 0 cases, the types should be
@@ -3651,6 +3655,8 @@ optimize_range_tests_cmp_bitwise (enum tree_code opcode, int first, int length,
tree type = TREE_TYPE (ranges[j - 1].exp);
if (j == k)
continue;
+ if (POINTER_TYPE_P (type))
+ type = pointer_sized_int_node;
if ((b % 4) == 3)
{
if (!useless_type_conversion_p (type1, type))
@@ -3680,7 +3686,7 @@ optimize_range_tests_cmp_bitwise (enum tree_code opcode, int first, int length,
op = r->exp;
continue;
}
- if (id == l)
+ if (id == l || POINTER_TYPE_P (TREE_TYPE (op)))
{
code = (b % 4) == 3 ? BIT_NOT_EXPR : NOP_EXPR;
g = gimple_build_assign (make_ssa_name (type1), code, op);
@@ -3704,6 +3710,14 @@ optimize_range_tests_cmp_bitwise (enum tree_code opcode, int first, int length,
gimple_seq_add_stmt_without_update (&seq, g);
op = gimple_assign_lhs (g);
}
+ type1 = TREE_TYPE (ranges[k - 1].exp);
+ if (POINTER_TYPE_P (type1))
+ {
+ gimple *g
+ = gimple_build_assign (make_ssa_name (type1), NOP_EXPR, op);
+ gimple_seq_add_stmt_without_update (&seq, g);
+ op = gimple_assign_lhs (g);
+ }
candidates.pop ();
if (update_range_test (&ranges[k - 1], NULL, candidates.address (),
candidates.length (), opcode, ops, op,
--
2.33.0

View File

@ -0,0 +1,55 @@
From 91ef8899a80e493042fd2687ad89064c9f90cf17 Mon Sep 17 00:00:00 2001
From: Pronin Alexander <pronin.alexander@huawei.com>
Date: Thu, 31 Oct 2024 16:14:34 +0800
Subject: [PATCH 3/6] Remove erroneous pattern from gimple ifcvt
Signed-off-by: Pronin Alexander <pronin.alexander@huawei.com>
---
gcc/match.pd | 2 +-
gcc/testsuite/gcc.dg/ifcvt-gimple-1.c | 21 +++++++++++++++++++++
2 files changed, 22 insertions(+), 1 deletion(-)
create mode 100644 gcc/testsuite/gcc.dg/ifcvt-gimple-1.c
diff --git a/gcc/match.pd b/gcc/match.pd
index 8f41c292f..2dd6581d1 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -4276,7 +4276,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
)
(if (flag_if_conversion_gimple)
- (for simple_op (plus minus bit_and bit_ior bit_xor)
+ (for simple_op (plus minus bit_ior bit_xor)
(simplify
(cond @0 (simple_op @1 INTEGER_CST@2) @1)
(switch
diff --git a/gcc/testsuite/gcc.dg/ifcvt-gimple-1.c b/gcc/testsuite/gcc.dg/ifcvt-gimple-1.c
new file mode 100644
index 000000000..381a4ad51
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/ifcvt-gimple-1.c
@@ -0,0 +1,21 @@
+/* { dg-do run } */
+/* { dg-options "-O2 -fno-inline -fif-conversion-gimple" } */
+
+#include <stdlib.h>
+
+void foo(int a, int *p) {
+ *p = a;
+}
+
+void verify (int a) {
+ if (a != 3)
+ abort ();
+}
+
+int main() {
+ int a = 0;
+ foo (3, &a);
+ int tmp = (a > 7) ? a & 1 : a;
+ verify (tmp);
+ return 0;
+}
--
2.33.0

View File

@ -0,0 +1,33 @@
From ca24d352e98e357f4f7b8f0d262201765705a08a Mon Sep 17 00:00:00 2001
From: Pronin Alexander <pronin.alexander@huawei.com>
Date: Thu, 31 Oct 2024 16:31:33 +0800
Subject: [PATCH 4/6] Add required check for iteration through uses
Signed-off-by: Pronin Alexander <pronin.alexander@huawei.com>
---
gcc/tree-ssa-math-opts.cc | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/gcc/tree-ssa-math-opts.cc b/gcc/tree-ssa-math-opts.cc
index 2c06b8a60..80c06fa01 100644
--- a/gcc/tree-ssa-math-opts.cc
+++ b/gcc/tree-ssa-math-opts.cc
@@ -4938,8 +4938,13 @@ convert_double_size_mul (gimple_stmt_iterator *gsi, gimple *stmt)
/* Find the mult low part getter. */
FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, match[3])
- if (gimple_assign_rhs_code (use_stmt) == REALPART_EXPR)
- break;
+ {
+ if (!is_gimple_assign (use_stmt))
+ continue;
+
+ if (gimple_assign_rhs_code (use_stmt) == REALPART_EXPR)
+ break;
+ }
/* Create high and low (if needed) parts extractors. */
/* Low part. */
--
2.33.0

View File

@ -0,0 +1,158 @@
From 210147e28d542a03588ba3c3fa473301a03bb687 Mon Sep 17 00:00:00 2001
From: Gmyrikov Konstantin <gmyrikov.konstantin@huawei-partners.com>
Date: Thu, 31 Oct 2024 16:45:15 +0800
Subject: [PATCH 6/6] Added param for optimization for merging bb's with cheap
insns.Zero param means turned off optimization(default implementation),One
means turned on
Signed-off-by: Gmyrikov Konstantin <gmyrikov.konstantin@huawei-partners.com>
---
gcc/params.opt | 4 +++
gcc/testsuite/gcc.dg/if_comb1.c | 13 +++++++++
gcc/testsuite/gcc.dg/if_comb2.c | 13 +++++++++
gcc/testsuite/gcc.dg/if_comb3.c | 12 +++++++++
gcc/tree-ssa-ifcombine.cc | 47 ++++++++++++++++++++++++++++++---
5 files changed, 86 insertions(+), 3 deletions(-)
create mode 100644 gcc/testsuite/gcc.dg/if_comb1.c
create mode 100644 gcc/testsuite/gcc.dg/if_comb2.c
create mode 100644 gcc/testsuite/gcc.dg/if_comb3.c
diff --git a/gcc/params.opt b/gcc/params.opt
index fc700ab79..3ddfaf5b2 100644
--- a/gcc/params.opt
+++ b/gcc/params.opt
@@ -789,6 +789,10 @@ Maximum number of VALUEs handled during a single find_base_term call.
Common Joined UInteger Var(param_max_vrp_switch_assertions) Init(10) Param Optimization
Maximum number of assertions to add along the default edge of a switch statement during VRP.
+-param=merge-assign-stmts-ifcombine=
+Common Joined UInteger Var(param_merge_assign_stmts_ifcombine) Init(0) IntegerRange(0, 1) Param Optimization
+Whether bb's with cheap gimple_assign stmts should be merged in the ifcombine pass.
+
-param=min-crossjump-insns=
Common Joined UInteger Var(param_min_crossjump_insns) Init(5) IntegerRange(1, 65536) Param Optimization
The minimum number of matching instructions to consider for crossjumping.
diff --git a/gcc/testsuite/gcc.dg/if_comb1.c b/gcc/testsuite/gcc.dg/if_comb1.c
new file mode 100644
index 000000000..e00adc37d
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/if_comb1.c
@@ -0,0 +1,13 @@
+/* { dg-do compile } */
+/* { dg-options "-Ofast -S --param=merge-assign-stmts-ifcombine=1 -fdump-tree-ifcombine" } */
+
+int foo (double a, double b, int c)
+{
+ if (c < 10 || a - b > 1.0)
+ return 0;
+ else
+ return 1;
+}
+
+/* { dg-final { scan-tree-dump "optimizing two comparisons" "ifcombine"} } */
+/* { dg-final { scan-tree-dump "Merging blocks" "ifcombine"} } */
diff --git a/gcc/testsuite/gcc.dg/if_comb2.c b/gcc/testsuite/gcc.dg/if_comb2.c
new file mode 100644
index 000000000..176e7e726
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/if_comb2.c
@@ -0,0 +1,13 @@
+/* { dg-do compile } */
+/* { dg-options "-Ofast -S --param=merge-assign-stmts-ifcombine=1 -fdump-tree-ifcombine" } */
+
+int foo (int a, int b, int c)
+{
+ if (a > 1 || b * c < 10)
+ return 0;
+ else
+ return 1;
+}
+
+/* { dg-final { scan-tree-dump "optimizing two comparisons" "ifcombine"} } */
+/* { dg-final { scan-tree-dump "Merging blocks" "ifcombine"} } */
diff --git a/gcc/testsuite/gcc.dg/if_comb3.c b/gcc/testsuite/gcc.dg/if_comb3.c
new file mode 100644
index 000000000..aa2e4510c
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/if_comb3.c
@@ -0,0 +1,12 @@
+/* { dg-do compile } */
+/* { dg-options "-Ofast -S --param=merge-assign-stmts-ifcombine=1 -fdump-tree-ifcombine" } */
+
+int foo (int a, int b, int c)
+{
+ if (a > 1 && b + c < 10)
+ a++;
+ return a;
+}
+
+/* { dg-final { scan-tree-dump "optimizing two comparisons" "ifcombine"} } */
+/* { dg-final { scan-tree-dump "Merging blocks" "ifcombine"} } */
diff --git a/gcc/tree-ssa-ifcombine.cc b/gcc/tree-ssa-ifcombine.cc
index ce9bbebf9..264a8bcae 100644
--- a/gcc/tree-ssa-ifcombine.cc
+++ b/gcc/tree-ssa-ifcombine.cc
@@ -110,6 +110,18 @@ recognize_if_then_else (basic_block cond_bb,
return true;
}
+/* Verify if gimple insn cheap for param=merge-assign-stmts-ifcombine
+ optimization. */
+
+bool is_insn_cheap (enum tree_code t)
+{
+ static enum tree_code cheap_insns[] = {MULT_EXPR, PLUS_EXPR, MINUS_EXPR};
+ for (int i = 0; i < sizeof (cheap_insns)/sizeof (enum tree_code); i++)
+ if (t == cheap_insns[i])
+ return 1;
+ return 0;
+}
+
/* Verify if the basic block BB does not have side-effects. Return
true in this case, else false. */
@@ -572,9 +584,38 @@ ifcombine_ifandif (basic_block inner_cond_bb, bool inner_inv,
= param_logical_op_non_short_circuit;
if (!logical_op_non_short_circuit || sanitize_coverage_p ())
return false;
- /* Only do this optimization if the inner bb contains only the conditional. */
- if (!gsi_one_before_end_p (gsi_start_nondebug_after_labels_bb (inner_cond_bb)))
- return false;
+ if (param_merge_assign_stmts_ifcombine)
+ {
+ int number_cheap_insns = 0;
+ int number_conds = 0;
+ for (auto i = gsi_start_nondebug_after_labels_bb
+ (outer_cond_bb); !gsi_end_p (i); gsi_next_nondebug (&i))
+ if (gimple_code (gsi_stmt (i)) == GIMPLE_ASSIGN
+ && is_insn_cheap (gimple_assign_rhs_code (gsi_stmt (i))))
+ number_cheap_insns++;
+ else if (gimple_code (gsi_stmt (i)) == GIMPLE_COND)
+ number_conds++;
+ for (auto i = gsi_start_nondebug_after_labels_bb
+ (inner_cond_bb); !gsi_end_p (i); gsi_next_nondebug (&i))
+ if (gimple_code (gsi_stmt (i)) == GIMPLE_ASSIGN
+ && is_insn_cheap (gimple_assign_rhs_code (gsi_stmt (i))))
+ number_cheap_insns++;
+ else if (gimple_code (gsi_stmt (i)) == GIMPLE_COND)
+ number_conds++;
+ if (!(number_cheap_insns == 1 && number_conds == 2)
+ && !gsi_one_before_end_p (gsi_start_nondebug_after_labels_bb
+ (inner_cond_bb)))
+ return false;
+ }
+ else
+ {
+ /* Only do this optimization if the inner bb contains
+ only the conditional. */
+ if (!gsi_one_before_end_p (gsi_start_nondebug_after_labels_bb
+ (inner_cond_bb)))
+ return false;
+ }
+
t1 = fold_build2_loc (gimple_location (inner_cond),
inner_cond_code,
boolean_type_node,
--
2.33.0

View File

@ -2,7 +2,7 @@
%global gcc_major 12
# Note, gcc_release must be integer, if you want to add suffixes to
# %%{release}, append them after %%{gcc_release} on Release: line.
%global gcc_release 38
%global gcc_release 39
%global _unpackaged_files_terminate_build 0
%global _performance_build 1
@ -404,6 +404,11 @@ Patch294: 0294-Fix-errors-in-ipa-prefetch-IAO50J-and-IAO5H7.patch
Patch295: 0295-Fix-error-with-grouped_load-merge-in-slp-transpose-v.patch
Patch296: 0296-Fix-error-in-slp-transpose-vectorize-for-IAQFM3.patch
Patch297: 0297-Fix-grouped-load-merging-error-in-SLP-transpose-vectorization.patch
Patch298: 0298-Mark-prefetch-builtin-as-willreturn.patch
Patch299: 0299-Backport-Disallow-pointer-operands-for-and-partly-PR.patch
Patch300: 0300-Remove-erroneous-pattern-from-gimple-ifcvt.patch
Patch301: 0301-Add-required-check-for-iteration-through-uses.patch
Patch302: 0302-Added-param-for-optimization-for-merging-bb-s-with-c.patch
# Part 3000 ~ 4999
@ -1465,6 +1470,11 @@ not stable, so plugins must be rebuilt any time GCC is updated.
%patch295 -p1
%patch296 -p1
%patch297 -p1
%patch298 -p1
%patch299 -p1
%patch300 -p1
%patch301 -p1
%patch302 -p1
%ifarch loongarch64
@ -4054,6 +4064,12 @@ end
%doc rpm.doc/changelogs/libcc1/ChangeLog*
%changelog
* Thu Nov 21 2024 huangzifeng <huangzifeng6@huawei.com> - 12.3.1-39
- Type:Sync
- ID:NA
- SUG:NA
- DESC:Sync patches from openeuler/gcc
* Thu Nov 21 2024 huangzifeng <huangzifeng6@huawei.com> - 12.3.1-38
- Type:Sync
- ID:NA