From: Kyrylo Tkachov <[email protected]>
When a diamond tests the same condition as a dominating diamond, jump
threading resolves the later branch by duplicating the blocks between
the two diamonds. The selects of both diamonds then sit on different
paths, and RTL if-conversion cannot turn them into conditional selects.
If the arms of the later diamond are empty and its PHI arguments are
available at the earlier condition, move its PHIs to the join of the
earlier diamond and leave the empty diamond to CFG cleanup. For
int x = c > 0 ? a : b;
int z = d > 0 ? r : s;
int y = c > 0 ? p : q;
the first full threader now sees
<bb 4> :
# x_2 = PHI <a_7(D)(2), b_6(D)(3)>
# y_4 = PHI <p_12(D)(2), q_11(D)(3)>
if (d_8(D) > 0)
...
<bb 6> :
# z_3 = PHI <r_10(D)(4), s_9(D)(5)>
instead of a third diamond on c_5(D) > 0 after bb 6. On aarch64 the
function becomes three csel instructions, where trunk branches on c and
duplicates the rest of the function.
Do this in the back_threader constructor of the first full threader,
after the loop tree is set up and before the ranger is created, followed
by a CFG cleanup. A dominator walk keeps the nearest diamond of the same
loop for each condition. As DOM does with conditions, it records each
diamond for its condition and, with the arms swapped, for the inverse
condition. Only the later diamond needs empty arms. The moved PHIs keep
their results, whose flow-sensitive information is reset. Debug binds of
the later arms move to its join with unknown values. The later
condition may trap: the earlier one already raised the same exception,
as when FRE or the threader remove it.
This moves the later PHIs to the earlier join, as suggested in comment
12 of the PR. The arguments of a moved PHI are available at the earlier
condition, so without the merge both of them are live from there to the
later diamond. After the merge only the PHI result is live in that
region. The merge adds one live value only when the arguments are
constants or have other uses after the earlier condition. The first
full threader does not run at -Og, so neither does the merge.
Bootstrapped and tested on aarch64-linux-gnu and x86_64-linux-gnu.
With the early-phiopt masks of the previous patch, the rate of SPEC
CPU2026 731.astcenc_r on NVIDIA Vera improves by 20.6% at -O3
-ffast-math -mcpu=olympus -flto=auto and by 15.3% at -O3 -mcpu=olympus
-flto=auto. Its hot function compute_quantized_weights_for_decimation has
41% fewer instructions, not counting alignment nops, and 78% fewer
conditional branches, with and without -ffast-math.
gcc/ChangeLog:
PR tree-optimization/125672
* dbgcnt.def (cond_diamond_merge): New counter.
* tree-ssa-threadbackward.cc: Include domwalk.h and
tree-hash-traits.h.
(cond_diamond_p): New function.
(available_at_p): Likewise.
(reset_arm_debug_stmts): Likewise.
(struct cond_diamond): New type.
(merge_cond_diamond): New function.
(class cond_diamond_merger): New class.
(merge_cond_diamonds): New function.
(back_threader::back_threader): Merge same-condition diamonds
before the first full threader.
gcc/testsuite/ChangeLog:
PR tree-optimization/125672
* gcc.dg/tree-ssa/pr125672-1.c: New test.
* gcc.dg/tree-ssa/pr125672-2.c: New test.
* gcc.dg/tree-ssa/pr125672-3.c: New test.
* gcc.dg/tree-ssa/pr125672-4.c: New test.
* gcc.dg/tree-ssa/pr125672-5.c: New test.
* gcc.dg/tree-ssa/pr125672-6.c: New test.
* gcc.dg/tree-ssa/pr125672-7.c: New test.
* gcc.dg/tree-ssa/pr125672-8.c: New test.
* gcc.dg/tree-ssa/pr125672-9.c: New test.
Signed-off-by: Kyrylo Tkachov <[email protected]>
---
gcc/dbgcnt.def | 1 +
gcc/testsuite/gcc.dg/tree-ssa/pr125672-1.c | 37 +++
gcc/testsuite/gcc.dg/tree-ssa/pr125672-2.c | 95 +++++++
gcc/testsuite/gcc.dg/tree-ssa/pr125672-3.c | 37 +++
gcc/testsuite/gcc.dg/tree-ssa/pr125672-4.c | 30 +++
gcc/testsuite/gcc.dg/tree-ssa/pr125672-5.c | 43 +++
gcc/testsuite/gcc.dg/tree-ssa/pr125672-6.c | 73 +++++
gcc/testsuite/gcc.dg/tree-ssa/pr125672-7.c | 112 ++++++++
gcc/testsuite/gcc.dg/tree-ssa/pr125672-8.c | 21 ++
gcc/testsuite/gcc.dg/tree-ssa/pr125672-9.c | 57 ++++
gcc/tree-ssa-threadbackward.cc | 297 +++++++++++++++++++++
11 files changed, 803 insertions(+)
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr125672-1.c
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr125672-2.c
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr125672-3.c
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr125672-4.c
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr125672-5.c
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr125672-6.c
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr125672-7.c
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr125672-8.c
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr125672-9.c
diff --git a/gcc/dbgcnt.def b/gcc/dbgcnt.def
index ac6a8b77119..b1181167da2 100644
--- a/gcc/dbgcnt.def
+++ b/gcc/dbgcnt.def
@@ -150,6 +150,7 @@ DEBUG_COUNTER (back_threadfull1)
DEBUG_COUNTER (back_threadfull2)
DEBUG_COUNTER (ccp)
DEBUG_COUNTER (cfg_cleanup)
+DEBUG_COUNTER (cond_diamond_merge)
DEBUG_COUNTER (cprop)
DEBUG_COUNTER (cse2_move2add)
DEBUG_COUNTER (dce)
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr125672-1.c
b/gcc/testsuite/gcc.dg/tree-ssa/pr125672-1.c
new file mode 100644
index 00000000000..ea60f3e166a
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr125672-1.c
@@ -0,0 +1,37 @@
+/* PR tree-optimization/125672 */
+/* Each select on aa[i] is repeated. The repeated diamonds are merged
+ instead of threaded. */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-threadfull1-details" } */
+
+void
+f (int *a, int *b, int *c, int *d, int *e, int *rr0, int *rr1)
+{
+ int aa[4] = { a[0], a[1], a[2], a[3] };
+ int bb[4] = { b[0], b[1], b[2], b[3] };
+ int cc[4] = { c[0], c[1], c[2], c[3] };
+ int dd[4] = { d[0], d[1], d[2], d[3] };
+ int ee[4] = { e[0], e[1], e[2], e[3] };
+ int r0[4];
+ int r1[4];
+ if (aa[0]) r0[0] = bb[0]; else r0[0] = cc[0];
+ if (aa[1]) r0[1] = bb[1]; else r0[1] = cc[1];
+ if (aa[2]) r0[2] = bb[2]; else r0[2] = cc[2];
+ if (aa[3]) r0[3] = bb[3]; else r0[3] = cc[3];
+
+ if (aa[0]) r1[0] = dd[0]; else r1[0] = ee[0];
+ if (aa[1]) r1[1] = dd[1]; else r1[1] = ee[1];
+ if (aa[2]) r1[2] = dd[2]; else r1[2] = ee[2];
+ if (aa[3]) r1[3] = dd[3]; else r1[3] = ee[3];
+ rr0[0] = r0[0];
+ rr0[1] = r0[1];
+ rr0[2] = r0[2];
+ rr0[3] = r0[3];
+ rr1[0] = r1[0];
+ rr1[1] = r1[1];
+ rr1[2] = r1[2];
+ rr1[3] = r1[3];
+}
+
+/* { dg-final { scan-tree-dump-times "Merging diamond" 4 "threadfull1" } } */
+/* { dg-final { scan-tree-dump-not "Registering jump thread" "threadfull1" } }
*/
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr125672-2.c
b/gcc/testsuite/gcc.dg/tree-ssa/pr125672-2.c
new file mode 100644
index 00000000000..6e50ca17ead
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr125672-2.c
@@ -0,0 +1,95 @@
+/* PR tree-optimization/125672 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-threadfull1-details" } */
+
+/* The join of the earlier diamond holds the later condition, a float
+ comparison that can trap. */
+
+float
+f1 (float c, float d, float a, float b, float p, float q, float *o)
+{
+ float x = c > d ? a : b;
+ o[0] = x * a;
+ o[1] = x * b;
+ o[2] = x * p;
+ o[3] = x * q;
+ float y = c > d ? p : q;
+ return x + y;
+}
+
+/* The earlier diamond computes values in its arms. */
+
+int
+f2 (int c, int d, int a, int b, int r, int s, int p, int q)
+{
+ int x, y, z;
+ if (c > 0)
+ x = a * 3;
+ else
+ x = b * 7;
+ if (d > 0)
+ z = r;
+ else
+ z = s;
+ if (c > 0)
+ y = p;
+ else
+ y = q;
+ return x + y + z;
+}
+
+/* Merging R before the first D condition makes the later D diamond
+ eligible in the same walk. */
+
+int
+f3 (int c, int d, int a, int b, int x, int y, int p, int q, int u)
+{
+ int c0, d0, r, s;
+ if (c > 0)
+ c0 = a;
+ else
+ c0 = b;
+ if (d > 0)
+ d0 = x;
+ else
+ d0 = y;
+ goto C1;
+
+D1:
+ if (d > 0)
+ s = r;
+ else
+ s = u;
+ return c0 + d0 + s;
+
+C1:
+ if (c > 0)
+ r = p;
+ else
+ r = q;
+ goto D1;
+}
+
+/* A diamond on the same condition that cannot be merged, because its arm
+ has a call, does not hide the earlier diamond from a later diamond that
+ it does not dominate. */
+
+void g (void);
+void h (int);
+
+int
+f4 (int c, int d, int a, int b, int p, int q)
+{
+ int x = c > 0 ? a : b;
+ if (d)
+ {
+ if (c > 0)
+ g ();
+ h (1);
+ h (2);
+ }
+ int y = c > 0 ? p : q;
+ return x + y;
+}
+
+/* { dg-final { scan-tree-dump-times "Merging diamond" 5 "threadfull1" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr125672-3.c
b/gcc/testsuite/gcc.dg/tree-ssa/pr125672-3.c
new file mode 100644
index 00000000000..c84226dc8e5
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr125672-3.c
@@ -0,0 +1,37 @@
+/* PR tree-optimization/125672 */
+/* A diamond is merged only into a diamond of the same loop, and a diamond
+ in an inner loop does not hide an outer diamond from a later one. */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-threadfull1-details -fdump-tree-mergephi2" }
*/
+
+int
+f (int c, int n, int a, int b, int p, int q, int *out)
+{
+ int x = c > 0 ? a : b;
+ for (int i = 0; i < n; ++i)
+ {
+ int y = c > 0 ? p : q;
+ out[i] = x + y;
+ }
+ return x;
+}
+
+int
+g (int c, int n, int a, int b, int p, int q, int r, int s, int *out)
+{
+ int x = c > 0 ? a : b;
+ int i = 0;
+ do
+ {
+ int y = c > 0 ? p : q;
+ out[i] = y;
+ }
+ while (++i < n);
+ int z = c > 0 ? r : s;
+ return x + z;
+}
+
+/* All five diamonds reach the first full threader, and only z is merged
+ into x. */
+/* { dg-final { scan-tree-dump-times "if \\(c_\[0-9\]+\\(D\\) > 0\\)" 5
"mergephi2" } } */
+/* { dg-final { scan-tree-dump-times "Merging diamond" 1 "threadfull1" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr125672-4.c
b/gcc/testsuite/gcc.dg/tree-ssa/pr125672-4.c
new file mode 100644
index 00000000000..9e159191653
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr125672-4.c
@@ -0,0 +1,30 @@
+/* PR tree-optimization/125672 */
+/* A PHI moved above an intervening range guard must not retain the range
+ information that was derived at its original definition. */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-evrp-details -fdump-tree-threadfull1-details
-fdump-tree-vrp1-details" } */
+
+int
+f (int c, int x)
+{
+ int first;
+ if (c > 0)
+ first = 1;
+ else
+ first = 2;
+
+ if ((unsigned int) x > 10)
+ return first;
+
+ int moved;
+ if (c > 0)
+ moved = x;
+ else
+ moved = 100;
+
+ return first + moved;
+}
+
+/* { dg-final { scan-tree-dump-times "Merging diamond" 1 "threadfull1" } } */
+/* { dg-final { scan-tree-dump "Global Exported: moved_.* = .*int .0, 10..100,
100." "evrp" } } */
+/* { dg-final { scan-tree-dump-not "moved_.* *: .*int .0, 10..100, 100."
"vrp1" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr125672-5.c
b/gcc/testsuite/gcc.dg/tree-ssa/pr125672-5.c
new file mode 100644
index 00000000000..2c81ce30461
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr125672-5.c
@@ -0,0 +1,43 @@
+/* PR tree-optimization/125672 */
+/* A diamond on the inverse of a dominating condition is merged with its
+ PHI arguments swapped. With NaNs the inverse of a < b is not a >= b. */
+/* { dg-do run } */
+/* { dg-options "-O2 -fdump-tree-threadfull1-details" } */
+/* { dg-add-options ieee } */
+
+__attribute__ ((noipa)) int
+f (int a, int b, int p, int q, int r, int s, int *o)
+{
+ int x = a < b ? p : q;
+ *o = r > 0 ? q : p;
+ int y = a >= b ? r : s;
+ return x * 16 + y;
+}
+
+__attribute__ ((noipa)) int
+g (float a, float b, int p, int q, int r, int s, int *o)
+{
+ int x = a < b ? p : q;
+ *o = r > 0 ? q : p;
+ int y = a >= b ? r : s;
+ return x * 16 + y;
+}
+
+int
+main (void)
+{
+ int o;
+ if (f (1, 2, 1, 2, 3, 4, &o) != 1 * 16 + 4 || o != 2)
+ __builtin_abort ();
+ if (f (2, 1, 1, 2, 3, 4, &o) != 2 * 16 + 3)
+ __builtin_abort ();
+ if (g (1.0f, 2.0f, 1, 2, 3, 4, &o) != 1 * 16 + 4)
+ __builtin_abort ();
+ if (g (2.0f, 1.0f, 1, 2, 3, 4, &o) != 2 * 16 + 3)
+ __builtin_abort ();
+ if (g (__builtin_nanf (""), 1.0f, 1, 2, 3, 4, &o) != 2 * 16 + 4)
+ __builtin_abort ();
+ return 0;
+}
+
+/* { dg-final { scan-tree-dump-times "Merging diamond" 1 "threadfull1" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr125672-6.c
b/gcc/testsuite/gcc.dg/tree-ssa/pr125672-6.c
new file mode 100644
index 00000000000..a5988ea6b63
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr125672-6.c
@@ -0,0 +1,73 @@
+/* PR tree-optimization/125672 */
+/* The later diamond reaches its join through an arm on the true path (f)
+ or on the false path (g). The debug binds of the arms hold only on
+ their path and are reset. A join with a third predecessor (h) is not
+ a diamond. */
+/* { dg-do run } */
+/* { dg-options "-O2 -g -fcompare-debug -fdump-tree-threadfull1-details" } */
+
+__attribute__ ((noipa)) int
+f (int c, int d, int a, int b, int p, int q, int r, int s)
+{
+ int x = c > 0 ? a : b;
+ int z = d > 0 ? r : s;
+ int y = q, w = 0;
+ if (c > 0)
+ {
+ y = p;
+ w = 1;
+ }
+ return x * 100 + y * 10 + z;
+}
+
+__attribute__ ((noipa)) int
+g (int c, int d, int a, int b, int p, int q, int r, int s)
+{
+ int x = c > 0 ? a : b;
+ int z = d > 0 ? r : s;
+ int y, v = 0;
+ if (c > 0)
+ y = p;
+ else
+ {
+ y = q;
+ v = 2;
+ }
+ return x * 100 + y * 10 + z;
+}
+
+__attribute__ ((noipa)) int
+h (int c, int d, int a, int b, int p, int q, int r)
+{
+ int x = c > 0 ? a : b;
+ int y;
+ if (d)
+ {
+ y = r;
+ goto J;
+ }
+ if (c > 0)
+ y = p;
+ else
+ y = q;
+ J:
+ return x * 10 + y;
+}
+
+int
+main (void)
+{
+ if (f (1, 1, 1, 2, 3, 4, 5, 6) != 135
+ || f (0, 0, 1, 2, 3, 4, 5, 6) != 246
+ || g (1, 1, 1, 2, 3, 4, 5, 6) != 135
+ || g (0, 0, 1, 2, 3, 4, 5, 6) != 246
+ || h (1, 1, 1, 2, 3, 4, 5) != 15
+ || h (0, 1, 1, 2, 3, 4, 5) != 25
+ || h (0, 0, 1, 2, 3, 4, 5) != 24)
+ __builtin_abort ();
+ return 0;
+}
+
+/* { dg-final { scan-tree-dump-times "Merging diamond" 2 "threadfull1" } } */
+/* { dg-final { scan-tree-dump-not "# DEBUG w => 1" "threadfull1" } } */
+/* { dg-final { scan-tree-dump "# DEBUG v => NULL" "threadfull1" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr125672-7.c
b/gcc/testsuite/gcc.dg/tree-ssa/pr125672-7.c
new file mode 100644
index 00000000000..b5af87e23e9
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr125672-7.c
@@ -0,0 +1,112 @@
+/* PR tree-optimization/125672 */
+/* { dg-do run } */
+/* { dg-options "-O2 -fgimple -fdump-tree-threadfull1-details" } */
+
+/* The later diamond reaches its join through arms on both paths. */
+
+[[gnu::noipa]]
+int __GIMPLE (ssa,startwith("threadfull1"))
+f (int c, int a, int b, int p, int q)
+{
+ int x;
+ int y;
+ int _1;
+ int _2;
+
+ __BB(2):
+ if (c_3(D) > 0)
+ goto __BB4;
+ else
+ goto __BB3;
+
+ __BB(3):
+ goto __BB4;
+
+ __BB(4):
+ x_4 = __PHI (__BB2: a_5(D), __BB3: b_6(D));
+ if (c_3(D) > 0)
+ goto __BB5;
+ else
+ goto __BB6;
+
+ __BB(5):
+ goto __BB7;
+
+ __BB(6):
+ goto __BB7;
+
+ __BB(7):
+ y_7 = __PHI (__BB5: p_8(D), __BB6: q_9(D));
+ _1 = x_4 * 10;
+ _2 = _1 + y_7;
+ return _2;
+}
+
+/* A block on the same condition that does not head a diamond (bb 4) does
+ not hide the earlier diamond from a later one. */
+
+[[gnu::noipa]]
+int __GIMPLE (ssa,startwith("threadfull1"))
+g (int c, int a, int b, int p, int q, int * o)
+{
+ int y;
+ int x;
+ int _1;
+ int _4;
+ int _16;
+
+ __BB(2):
+ if (c_7(D) > 0)
+ goto __BB4;
+ else
+ goto __BB3;
+
+ __BB(3):
+ goto __BB4;
+
+ __BB(4):
+ x_2 = __PHI (__BB2: a_9(D), __BB3: b_8(D));
+ __MEM <int> (o_11(D)) = x_2;
+ if (c_7(D) > 0)
+ goto __BB5;
+ else
+ goto __BB6;
+
+ __BB(5):
+ __MEM <int> (o_11(D) + _Literal (int *) 4) = a_9(D);
+ _1 = __MEM <int> (o_11(D) + _Literal (int *) 8);
+ if (_1 == 0)
+ goto __BB9;
+ else
+ goto __BB6;
+
+ __BB(6):
+ if (c_7(D) > 0)
+ goto __BB8;
+ else
+ goto __BB7;
+
+ __BB(7):
+ goto __BB8;
+
+ __BB(8):
+ y_3 = __PHI (__BB6: p_15(D), __BB7: q_14(D));
+ _16 = x_2 + y_3;
+ goto __BB9;
+
+ __BB(9):
+ _4 = __PHI (__BB5: 1, __BB8: _16);
+ return _4;
+}
+
+int
+main (void)
+{
+ int o[3] = { 0, 0, 1 };
+ if (f (1, 1, 2, 3, 4) != 13 || f (0, 1, 2, 3, 4) != 24
+ || g (1, 1, 2, 3, 4, o) != 4 || g (0, 1, 2, 3, 4, o) != 6)
+ __builtin_abort ();
+ return 0;
+}
+
+/* { dg-final { scan-tree-dump-times "Merging diamond" 2 "threadfull1" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr125672-8.c
b/gcc/testsuite/gcc.dg/tree-ssa/pr125672-8.c
new file mode 100644
index 00000000000..b8152366ccc
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr125672-8.c
@@ -0,0 +1,21 @@
+/* PR tree-optimization/125672 */
+/* A diamond whose PHI argument is computed after the earlier condition is
+ not merged. */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-threadfull1-details -fdump-tree-mergephi2" }
*/
+
+float
+f (float c, float d, float a, float b, float p, float *o)
+{
+ float x = c > d ? a : b;
+ o[0] = x * a;
+ o[1] = x * b;
+ float s = x * p;
+ o[2] = s;
+ o[3] = s * s;
+ float y = c > d ? s : p;
+ return x + y;
+}
+
+/* { dg-final { scan-tree-dump-times "if \\(c_\[0-9\]+\\(D\\) > d_" 2
"mergephi2" } } */
+/* { dg-final { scan-tree-dump-not "Merging diamond" "threadfull1" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr125672-9.c
b/gcc/testsuite/gcc.dg/tree-ssa/pr125672-9.c
new file mode 100644
index 00000000000..73e0fe05bc1
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr125672-9.c
@@ -0,0 +1,57 @@
+/* PR tree-optimization/125672 */
+/* { dg-do compile } */
+/* { dg-require-effective-target label_values } */
+/* { dg-require-effective-target indirect_jumps } */
+/* { dg-require-effective-target nonlocal_goto } */
+/* { dg-options "-O2" } */
+
+#include <setjmp.h>
+
+void *g (void *);
+void h (int);
+
+/* An arm that reaches the join through an abnormal edge does not make a
+ diamond. */
+
+int
+f1 (int c, int p, int q)
+{
+ void *t = g (&&L);
+ if (c)
+ goto *t;
+ L:
+ h (0);
+ h (0);
+ return (c ? p : q) * p;
+}
+
+int
+f2 (int c, int q)
+{
+ void *t = g (&&L);
+ if (c)
+ goto *t;
+ L:
+ h (0);
+ h (0);
+ return c ? 1234 : q;
+}
+
+/* The result of the later PHI occurs in an abnormal PHI. Its live range
+ must not start at the earlier join, where the previous value of Y is
+ still live. */
+
+jmp_buf buf;
+
+int
+f3 (int c, int d, int a, int b, int p, int q)
+{
+ int y = d;
+ if (setjmp (buf))
+ return y;
+ int x = c > 0 ? a : b;
+ h (y);
+ y = c > 0 ? p : q;
+ h (y);
+ return x + y;
+}
diff --git a/gcc/tree-ssa-threadbackward.cc b/gcc/tree-ssa-threadbackward.cc
index ce086a55190..b3ddf8dcb57 100644
--- a/gcc/tree-ssa-threadbackward.cc
+++ b/gcc/tree-ssa-threadbackward.cc
@@ -46,6 +46,8 @@ along with GCC; see the file COPYING3. If not see
#include "tree-pretty-print.h"
#include "cfghooks.h"
#include "dbgcnt.h"
+#include "domwalk.h"
+#include "tree-hash-traits.h"
// Class to abstract the profitability code for the backwards threader.
@@ -190,6 +192,295 @@ back_threader_profitability::pop_bb ()
// in a the given direction.
const edge back_threader::UNREACHABLE_EDGE = (edge) -1;
+// Same-condition diamond merging.
+//
+// When a diamond tests the same condition as a dominating diamond, the
+// full threader removes the later branch by duplicating the blocks
+// between the two diamonds, which hides both selects from if-conversion.
+// If the later diamond only selects between values that are available at
+// the earlier condition, move its PHIs to the earlier join instead, which
+// leaves an empty diamond for CFG cleanup to remove. For example
+//
+// if (c)
+// ...
+// # x = PHI <a, b>
+// ...
+// if (c)
+// ;
+// # y = PHI <p, q>
+//
+// becomes
+//
+// if (c)
+// ...
+// # x = PHI <a, b>
+// # y = PHI <p, q>
+// ...
+//
+// The moved PHI result becomes live from the earlier join, while its
+// arguments are no longer needed after the earlier condition.
+
+// BB ends in a GIMPLE_COND. Return true if both outcomes reach a common
+// block JOIN with two predecessors through normal edges, each directly or
+// through an arm block that has BB as its only predecessor and JOIN as its
+// only successor. In that case set *TRUE_E and *FALSE_E to the edges into
+// JOIN on the true and the false path.
+
+static bool
+cond_diamond_p (basic_block bb, edge *true_e, edge *false_e)
+{
+ edge t, f;
+ extract_true_false_edges_from_block (bb, &t, &f);
+ if (single_pred_p (t->dest) && single_succ_p (t->dest))
+ t = single_succ_edge (t->dest);
+ if (single_pred_p (f->dest) && single_succ_p (f->dest))
+ f = single_succ_edge (f->dest);
+ // An arm that ends in a computed goto reaches JOIN through an abnormal
+ // edge, for example.
+ if (t->dest != f->dest
+ || EDGE_COUNT (t->dest->preds) != 2
+ || ((t->flags | f->flags) & EDGE_COMPLEX))
+ return false;
+ *true_e = t;
+ *false_e = f;
+ return true;
+}
+
+// Return true if the value VAL is available at the end of BB.
+
+static bool
+available_at_p (tree val, basic_block bb)
+{
+ if (TREE_CODE (val) != SSA_NAME || SSA_NAME_IS_DEFAULT_DEF (val))
+ return true;
+ return dominated_by_p (CDI_DOMINATORS, bb,
+ gimple_bb (SSA_NAME_DEF_STMT (val)));
+}
+
+// Move the debug binds of the empty arm ARM to the start of JOIN and reset
+// them, because they hold only on the path through ARM. Remove the other
+// statements of ARM, such as debug markers, which also describe only that
+// path.
+
+static void
+reset_arm_debug_stmts (basic_block arm, basic_block join)
+{
+ gimple_stmt_iterator to = gsi_after_labels (join);
+ for (gimple_stmt_iterator from = gsi_after_labels (arm); !gsi_end_p (from);)
+ {
+ gimple *stmt = gsi_stmt (from);
+ if (gimple_debug_bind_p (stmt))
+ {
+ gimple_debug_bind_reset_value (stmt);
+ update_stmt (stmt);
+ gsi_move_before (&from, &to);
+ }
+ else
+ gsi_remove (&from, true);
+ }
+}
+
+namespace {
+
+// A diamond headed by the GIMPLE_COND that ends BB. T and F are the edges
+// into its join on the paths where the condition that keys the diamond is
+// true and false.
+
+struct cond_diamond
+{
+ basic_block bb;
+ edge t;
+ edge f;
+};
+
+// Try to merge diamond D2 into the dominating diamond D1, which tests the
+// same condition as D2 or its inverse. Return true on success, in which
+// case the non-virtual PHIs of the join of D2 have moved to the join of D1
+// and D2 is an empty diamond.
+
+bool
+merge_cond_diamond (const cond_diamond &d2, const cond_diamond &d1)
+{
+ basic_block b2 = d2.bb;
+ edge t2 = d2.t;
+ edge f2 = d2.f;
+ // The arms of D2 must be empty, so that D2 becomes an empty diamond.
+ if ((t2->src != b2 && !empty_block_p (t2->src))
+ || (f2->src != b2 && !empty_block_p (f2->src)))
+ return false;
+
+ basic_block join1 = d1.t->dest;
+ basic_block join2 = t2->dest;
+ // Every path from D1 to B2 passes JOIN1, so JOIN1 dominates all uses of
+ // the moved PHI results.
+ gcc_checking_assert (dominated_by_p (CDI_DOMINATORS, b2, join1)
+ && join1 != join2);
+
+ // The arguments of a moved PHI must be available at D1. Its result
+ // becomes live from JOIN1, which must not extend an abnormal live range.
+ for (gphi_iterator gsi = gsi_start_nonvirtual_phis (join2); !gsi_end_p (gsi);
+ gsi_next_nonvirtual_phi (&gsi))
+ {
+ gphi *phi = gsi.phi ();
+ if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_phi_result (phi))
+ || !available_at_p (gimple_phi_arg_def_from_edge (phi, t2), d1.bb)
+ || !available_at_p (gimple_phi_arg_def_from_edge (phi, f2), d1.bb))
+ return false;
+ }
+
+ if (!dbg_cnt (cond_diamond_merge))
+ return false;
+
+ if (dump_file && (dump_flags & TDF_DETAILS))
+ fprintf (dump_file, "Merging diamond bb%d into same-condition diamond "
+ "bb%d\n", b2->index, d1.bb->index);
+
+ if (t2->src != b2)
+ reset_arm_debug_stmts (t2->src, join2);
+ if (f2->src != b2)
+ reset_arm_debug_stmts (f2->src, join2);
+
+ // Move the non-virtual PHIs and keep their results. Reset their
+ // flow-sensitive information, which may depend on conditions between the
+ // joins.
+ for (gphi_iterator gsi = gsi_start_phis (join2); !gsi_end_p (gsi);)
+ {
+ gphi *phi = gsi.phi ();
+ tree res = gimple_phi_result (phi);
+ if (virtual_operand_p (res))
+ {
+ gsi_next (&gsi);
+ continue;
+ }
+ tree t = gimple_phi_arg_def_from_edge (phi, t2);
+ tree f = gimple_phi_arg_def_from_edge (phi, f2);
+ location_t tloc = gimple_phi_arg_location_from_edge (phi, t2);
+ location_t floc = gimple_phi_arg_location_from_edge (phi, f2);
+ remove_phi_node (&gsi, false);
+ reset_flow_sensitive_info (res);
+ gphi *moved = create_phi_node (res, join1);
+ add_phi_arg (moved, t, d1.t, tloc);
+ add_phi_arg (moved, f, d1.f, floc);
+ }
+ return true;
+}
+
+// Walk the dominator tree and merge each diamond into the nearest
+// dominating diamond of the same loop that tests the same condition or its
+// inverse. LIM hoists invariant selects out of a loop, and merging into an
+// inner loop would compute the selects on each iteration. The dominator
+// children can be visited in any order, because each subtree restores the
+// diamonds that it hides.
+
+class cond_diamond_merger : public dom_walker
+{
+public:
+ cond_diamond_merger ()
+ : dom_walker (CDI_DOMINATORS, ALL_BLOCKS, (int *) (uintptr_t) -1)
+ {}
+
+ edge before_dom_children (basic_block) final override;
+ void after_dom_children (basic_block) final override;
+
+ // The number of merged diamonds.
+ unsigned m_merged = 0;
+
+private:
+ // A condition is keyed by the number of its loop, its code and its
+ // operands.
+ typedef pair_hash <pair_hash <int_hash <int, -1, -2>,
+ int_hash <int, -1, -2>>,
+ pair_hash <tree_operand_hash, tree_operand_hash>>
+ cond_hash;
+
+ // The innermost active diamond for each condition, which has a null
+ // block if there is none.
+ hash_map<cond_hash, cond_diamond> m_active;
+ // A block whose diamond is active, the key of its condition, and the
+ // diamond that it hides.
+ struct hidden_diamond
+ {
+ basic_block bb;
+ cond_hash::value_type key;
+ cond_diamond diamond;
+ };
+ auto_vec<hidden_diamond> m_hidden;
+
+ // Make D the active diamond for KEY and hide the previous one.
+ void record (const cond_hash::value_type &key, const cond_diamond &d)
+ {
+ cond_diamond &active = m_active.get_or_insert (key);
+ m_hidden.safe_push ({ d.bb, key, active });
+ active = d;
+ }
+};
+
+// Merge the diamond that BB heads, if any, into the active diamond for its
+// condition. Otherwise make it the active diamond for its condition, and
+// with its arms swapped for the inverse condition, while visiting the blocks
+// that BB dominates. DOM records conditions and their inverses the same way.
+
+edge
+cond_diamond_merger::before_dom_children (basic_block bb)
+{
+ gcond *cond = safe_dyn_cast <gcond *> (*gsi_last_bb (bb));
+ cond_diamond d;
+ if (!cond || !cond_diamond_p (bb, &d.t, &d.f))
+ return NULL;
+
+ d.bb = bb;
+ tree lhs = gimple_cond_lhs (cond);
+ tree rhs = gimple_cond_rhs (cond);
+ auto key = [&] (tree_code code)
+ {
+ return cond_hash::value_type (std::make_pair (bb->loop_father->num,
+ code),
+ std::make_pair (lhs, rhs));
+ };
+ tree_code code = gimple_cond_code (cond);
+ cond_diamond *active = m_active.get (key (code));
+ if (active && active->bb && merge_cond_diamond (d, *active))
+ {
+ m_merged++;
+ return NULL;
+ }
+
+ record (key (code), d);
+ tree_code inv_code = invert_tree_comparison (code, HONOR_NANS (lhs));
+ if (inv_code != ERROR_MARK)
+ record (key (inv_code), { bb, d.f, d.t });
+ return NULL;
+}
+
+// Make the diamonds that BB hides active again.
+
+void
+cond_diamond_merger::after_dom_children (basic_block bb)
+{
+ while (!m_hidden.is_empty () && m_hidden.last ().bb == bb)
+ {
+ hidden_diamond hidden = m_hidden.pop ();
+ *m_active.get (hidden.key) = hidden.diamond;
+ }
+}
+
+} // namespace {
+
+// Merge same-condition diamonds in FUN. Return true if any diamond was
+// merged, in which case the CFG must be cleaned up to remove the empty
+// diamonds.
+
+static bool
+merge_cond_diamonds (function *fun)
+{
+ calculate_dominance_info (CDI_DOMINATORS);
+ cond_diamond_merger merger;
+ merger.walk (ENTRY_BLOCK_PTR_FOR_FN (fun));
+ statistics_counter_event (fun, "Same-condition diamonds merged",
+ merger.m_merged);
+ return merger.m_merged != 0;
+}
+
back_threader::back_threader (function *fun, unsigned flags, bool first)
: m_visited_flag (fun), m_first (first)
{
@@ -198,6 +489,12 @@ back_threader::back_threader (function *fun, unsigned
flags, bool first)
else
loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
+ // Merge same-condition diamonds before the first full threader
+ // duplicates the blocks between them. The merge uses the loop tree set
+ // up above, and the CFG is cleaned up before the ranger is created.
+ if (first && (flags & BT_RESOLVE) && merge_cond_diamonds (fun))
+ cleanup_tree_cfg ();
+
m_fun = fun;
m_flags = flags;
m_last_stmt = NULL;
--
2.54.0 (Apple Git-157)