gcc/0302-Added-param-for-optimization-for-merging-bb-s-with-c.patch
2024-11-21 11:46:51 +08:00

159 lines
5.9 KiB
Diff

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