summaryrefslogtreecommitdiff
path: root/0302-Added-param-for-optimization-for-merging-bb-s-with-c.patch
blob: da25a9e25c950f625e6e27963f2ab5c54f33d32f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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